「一本通 6.3 练习 2」聪明的燕姿

不会写,留坑...

#include<bits/stdc++.h>
using namespace std;
const int N=44725;
int s,p[N],ans[N];
bool vis[N];
void get_prime(){
    for(int i=2;i<=N;++i){
        if(!vis[i]) p[++p[0]]=i;
        for(int j=1;j<=p[0]&&i*p[j]<=N;++j){
            vis[i*p[j]]=1;
            if(i%p[j]==0) break;
        }
    }
}
inline bool pd(int x){
    if(x==1) return 0;
    int tmp=sqrt(x);
    for(int i=2;i<=tmp;++i)
    if(x%i==0) return 0;
    return 1;
}
void dfs(int x,int now,int res){
    if(now==1){ans[++ans[0]]=res; return;}
    if(now>p[x]&&pd(now-1))
        ans[++ans[0]]=res*(now-1);
    for(int i=x;p[i]*p[i]<=now;++i){
        for(int t=p[i],j=p[i]+1;j<=now;t*=p[i],j+=t)
        if(now%j==0) dfs(i+1,now/j,res*t);
    }
    
}
int main(){
    get_prime();
    while(~scanf("%d",&s)){
        ans[0]=0;
        dfs(1,s,1);
        sort(ans+1,ans+ans[0]+1);
        printf("%d
",ans[0]);
        for(int i=1;i<ans[0];++i)
        printf("%d ",ans[i]);
        if(ans[0]) printf("%d
",ans[ans[0]]);
    }
    return 0;    
} 
原文地址:https://www.cnblogs.com/huihao/p/11795768.html