Codeforces 515C 题解(贪心+数论)(思维题)

本文介绍了一个基于贪心策略的算法问题解决方案,旨在找到一个最大整数x,其各位数字的阶乘之积等于给定数a,并且x不含0或1这两个数字。通过将0到9各数字拆解为其阶乘的组成因子,文章提供了一种有效的方法来构造目标数值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题面

传送门:http://codeforces.com/problemset/problem/515/C

Drazil is playing a math game with Varda.

Let’s define f(x) f ( x ) for positive integer x as a product of factorials of its digits. For example, f(135)=1!3!5! f ( 135 ) = 1 ! ∗ 3 ! ∗ 5 !

First, they choose a decimal number a consisting of n digits that contains at least one digit larger than 1. This number may possibly start with leading zeroes. Then they should find maximum positive number x satisfying following two conditions:

  1. x doesn’t contain neither digit 0 nor digit 1.

  2. f(x)=f(a) f ( x ) = f ( a )

Help friends find such number.

题目大意:
定义f(x)为x的每一位的阶乘之积
给出一个数a,求满足条件(x的每一位没有0或1)的最大x,使f(x)=f(a)

分析:

此题可用贪心求解
贪心的思路是很显然的,应该让x的位数尽量多,而每一位尽量小,最大的数应该排在最左边
这样,我们就可以把a的每一位拆开
如a=6
6!=6*5*4*3*2*1=6*5!=3!*5!
所以6可以被替换成35

所以我们把0~9的数字拆开(其实0,1应该直接舍去,因为不符合条件)
0!=0!
1!=1!
2!=2!
3!=3!
4!=3!*4=3!*2!*2!
5!=5!
6!=5!*6=5!*3!
7!=7!
8!=7!*8=7!*2!*2!*2!
9!=9*8*7!=7!*3!*3!*2!;

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const string convert[10]={"0","0","2","3","322","5","53","7","7222","7332"};
string s;
string ans;
int n;
int cmp(char x,char y){
    return x>y;
}
int main(){
    scanf("%d",&n);
    cin>>s;
    ans="";
    for(int i=0;i<n;i++){
        if(s[i]-'0'==0||s[i]-'0'==1) continue;
        ans=ans+convert[s[i]-'0'];
    }
//  cout<<ans<<endl;
    sort(ans.begin(),ans.end(),cmp);
    cout<<ans<<endl;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值