【codechef】Arush Challenge(灵活题)

来源:互联网 发布:网络摄像头ip搜索软件 编辑:程序博客网 时间:2024/05/24 15:36

Arush was not always poor at Mathematics but his recent performances had not been that good and he had lost his confidence. Now his elder brother was determined to bring back his confidence back in Mathematics.

So he made a tricky question and made sure that Arush would be able to do solve it. The factorial of a non-negative integer n, denoted by n! , is the product of all positive integers less than or equal to n.

n! = n * (n-1) * (n-2) * ...... * 1


Arush’s elder brother so defined a function F(x) for positive integer x as the product of factorials of its constituting digits.
For example, F(125) = 1! * 2! * 5!.


You are given a number N that contains d digits and contains at least one digit larger than 1.

The problem is to find maximum positive number M which should contain neither the digit 0 nor the digit 1 and also F(M) = F(N).

The number N may possibly start with leading zeroes.

Help Arush to bring his confidence back.

 

Input

  • The first line of input contains T, the number of testcases.
  • The first line contains an integer d, number of digits in N.
  • Next line contains d digits of N.

 

Output

  • Output the maximum possible integer M that satisfies the above conditions.

 

Constraints

Should contain all the constraints on the input data that you may have. Format it like:

  • 1 ≤ T ≤ 50
  • 1 ≤ d ≤ 15

 

Example

Input:2163006Output:5353

 

Explanation

Example case 1. d = 1, N = 6. Now F(6) = 6! = 720 and F(53) = 5! * 3! = 120 * 6 = 720.

http://www.codechef.com/problems/CLCO03

将当前数字转换,使得转换前后数字上每一位的阶乘的乘积相等。求转换后得到的最大的数字。
当前数的每一位(2-9)转换,在保证位数取到最大的情况下,数值取到最大。2,3,5,7都只能取到本身。

#include <bits/stdc++.h> using namespace std; int main(){    int t,n;<span style="font-family:Arial, sans-serif;color:#444444;"><span style="font-size: 12.307692527771px; line-height: 1.5em;"></span></span>    string s;    cin >> t;    while ( t-- ) {        cin >> n >> s;        string ans = "";        for ( int i = 0; i < n; i++ ) {            if ( s[i] == '0' || s[i] == '1' ) continue;            else if ( s[i] == '4' ) ans += "223";            else if ( s[i] == '6' ) ans += "53";            else if ( s[i] == '8' ) ans += "2227";            else if ( s[i] == '9' ) ans += "7332";            else ans += s[i];        }        sort(ans.begin(),ans.end());  //最后再从大到小排序,这样保证在位数最多的前提下,值也最大        reverse(ans.begin(),ans.end());        cout << ans << endl;    }    return 0;}
#include <bits/stdc++.h>using namespace std;int cnt[10];int main() {int t,d;long long n;scanf("%d",&t);while(t--){    scanf("%d",&d);    scanf("%lld",&n);    for(int i=0;i<10;++i) cnt[i]=0;    while(n>0){        int dig=n%10;        if(dig==2) cnt[2]++;        else if(dig==3) cnt[3]++;        else if(dig==4){            cnt[3]++;            cnt[2]+=2;        }        else if(dig==5) cnt[5]++;        else if(dig==6){            cnt[5]++;            cnt[3]++;        }        else if(dig==7) cnt[7]++;        else if(dig==8){            cnt[7]++;            cnt[2]+=3;        }        else if(dig==9){            cnt[7]++;            cnt[3]+=2;            cnt[2]++;        }        n/=10;    }    for(int i=7;i>=2;--i){        for(int j=1;j<=cnt[i];++j) printf("%d",i);    }    printf("\n");}return 0;}


0 0
原创粉丝点击