您的位置:首页 > 其它

Codeforces Round #292 (Div. 2) C. Drazil and Factorial

2015-03-05 16:41 513 查看
题目链接:http://codeforces.com/contest/515/problem/C

给出一个公式例如:F(135) = 1! * 3! * 5!;

现在给你一个有n位的数字a,让你求这样一个x,满足x中没有0和1,F(a) = F(x),然后就是x要最大.

当x的位数比a多或者从高位开始x的数某一位要大于a的某一位,然后第二种显然是不可能的,所以我们寻找如何把a变长的方法.

例如数字

4! = 1 * 2 * 3 * 4

=3! * 2 * 2

=3! * 2! * 2!

所以当a中包含数字4时,我们可以通过把4拆成322来变大,同理:

F(6) = F(53)

F(8) = F(7222)

F(9) = F(7332)

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,A[100];
char str[20];
int main()
{
while(scanf("%d",&n)!=EOF)
{
scanf("%s",str);
int f = 0;
for(int i = 0;i < n;++i)
{
if(str[i] == '4')
{
A[f++] = 3;
A[f++] = 2;
A[f++] = 2;
}
else if(str[i] == '6')
{
A[f++] = 5;
A[f++] = 3;
}
else if(str[i] == '8')
{
A[f++] = 7;
A[f++] = 2;
A[f++] = 2;
A[f++] = 2;
}
else if(str[i] == '9')
{
A[f++] = 7;
A[f++] = 3;
A[f++] = 3;
A[f++] = 2;
}
else
{
if(str[i]-'0' > 1)
A[f++] = str[i] - '0';
}
}
sort(A,A+f);
for(int i = f-1;i >= 0;--i)
printf("%d",A[i]);
puts("");
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: