题目链接
思路
比赛时乱写了个爆搜就交上去了,题目数据真心水。。。居然乱搞出来50分。。。
后来发现其实优化一下,我本来搜出的是最优解,即用的约数最少的解,但是实际上题目只要输出可行解,找到一组可行解就可以直接退出了,稍微改下就是75,我擦。。。
比赛时zhw神犇在2min时直接搞掉这题,后来发现他的代码居然比我的爆搜还短好多。。。
实际上这个题是没有无解的情况的,因此贪心乱搞就行了,我们可以DFS随便找出一些 n! 的约数,然后从大到小对这些约数进行排序,从大到小选择约数加入答案即可。。。
代码
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long int LL;
LL n,m;
LL yueshu[10000000];
int tot=0;
void DFS(int pos,LL y)
{
if(pos==n+1)
{
yueshu[++tot]=y;
return;
}
DFS(pos+1,y);
DFS(pos+1,y*pos);
}
bool cmp(LL a,LL b)
{
return a>b;
}
int main()
{
scanf("%lld%lld",&n,&m);
DFS(1,1);
sort(yueshu+1,yueshu+tot+1,cmp);
for(int i=1;i<=tot;i++)
{
if(yueshu[i]<=m)
{
printf("%lld\n",yueshu[i]);
m-=yueshu[i];
}
}
return 0;
}