Codeforces Round #292 (Div. 2) C. Drazil and Factorial
题目链接: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)
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 using namespace std; 6 int n,A[100]; 7 char str[20]; 8 int main() 9 { 10 while(scanf("%d",&n)!=EOF) 11 { 12 scanf("%s",str); 13 int f = 0; 14 for(int i = 0;i < n;++i) 15 { 16 if(str[i] == '4') 17 { 18 A[f++] = 3; 19 A[f++] = 2; 20 A[f++] = 2; 21 } 22 else if(str[i] == '6') 23 { 24 A[f++] = 5; 25 A[f++] = 3; 26 } 27 else if(str[i] == '8') 28 { 29 A[f++] = 7; 30 A[f++] = 2; 31 A[f++] = 2; 32 A[f++] = 2; 33 } 34 else if(str[i] == '9') 35 { 36 A[f++] = 7; 37 A[f++] = 3; 38 A[f++] = 3; 39 A[f++] = 2; 40 } 41 else 42 { 43 if(str[i]-'0' > 1) 44 A[f++] = str[i] - '0'; 45 } 46 } 47 sort(A,A+f); 48 for(int i = f-1;i >= 0;--i) 49 printf("%d",A[i]); 50 puts(""); 51 } 52 return 0; 53 }