https://vjudge.net/problem/UVA-10837
求最小的n,使phi(n)=m
#include<cstdio> #include<algorithm> #define N 10011 int prime[N],cnt; bool v[N],vis[N]; int p[N],tot,ans; void pre_prime() { for(int i=2;i<N;i++) { if(!v[i]) prime[++cnt]=i; for(int j=1;j<=cnt;j++) { if(i*prime[j]>=N) break; v[i*prime[j]]=1; if(i%prime[j]==0) break; } } } int judge(int n) { if(n==1) return 1; n++; for(int i=1;i<=cnt && prime[i]*prime[i]<=n;i++) if(n%prime[i]==0) return -1; for(int i=1;i<=tot;i++) if(vis[i] && n==p[i]) return -1; return n; } void dfs(int now,int left,int sum) { if(sum==tot+1) { int ret=judge(left); if(ret>0) ans=std::min(ans,now*ret); return; } dfs(now,left,sum+1); if(left%(p[sum]-1)==0) { vis[sum]=1; now*=p[sum]; left/=p[sum]-1; while(1) { dfs(now,left,sum+1); if(left%p[sum]) break; left/=p[sum];now*=p[sum]; } vis[sum]=0; } } void solve(int n) { tot=0; ans=2e9; for(int i=1;i<=cnt && n>=(prime[i]-1)*(prime[i]-1);i++) if(n%(prime[i]-1)==0) p[++tot]=prime[i]; dfs(1,n,1); } int main() { pre_prime(); int n,t=0; while(scanf("%d",&n)!=EOF) { if(!n) return 0; solve(n); printf("Case %d: %d %d ",++t,n,ans); } }