【codechef】Arush Challenge(灵活题)

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:
2
1
6
3
006

Output:
53
53

 

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值