# Bzoj3629 [jloi2014]聪明的燕姿

Posted by yjjr's blog on February 6, 2018 2 minutes to read

Description

Input

Output

Sample Input

42

Sample Output

3

20 26 41

HINT

1.n-1为质数，那么n-1存放答案的num数组

2.对于每一个未被搜索过且平方小于当前数的质数，则枚举所有可能符合题意的情况进行递归搜索

Code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=5e4;
int prime[maxn],cnt=0,ans,num[maxn],n;
bool not_prime[maxn];
void getprime()
{
rep(i,2,maxn){
if(!not_prime[i])prime[++cnt]=i;
for(int j=1;prime[j]*i<=maxn&&j<=cnt;j++){
not_prime[i*prime[j]]=1;
if(i%prime[j]==0)break;
}
}
}
bool isprime(int x)
{
if(x==1)return false;
if(x<=maxn)return !not_prime[x];
for(int i=1;prime[i]*prime[i]<=x;i++)
if(x%prime[i]==0)return false;
return true;
}
void dfs(int last,int now,int tot)
{
if(tot==1){num[++ans]=now;return;}
if(tot-1>prime[last]&&isprime(tot-1))num[++ans]=now*(tot-1);
for(int i=last+1;prime[i]*prime[i]<=tot;i++)
for(int tnum=prime[i]+1,t=prime[i];tnum<=tot;t*=prime[i],tnum+=t)
if(tot%tnum==0)dfs(i,now*t,tot/tnum);
}
int main()
{
getprime();
while(scanf("%d",&n)!=EOF){
ans=0;dfs(0,1,n);
sort(num+1,num+1+ans);
printf("%d\n",ans);
rep(i,1,ans)printf("%d%c",num[i],i==ans?'\n':' ');
}
return 0;
}