UVA 10837(p345)----A Research Problem

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxp=10000+10;
bool vis[maxp],done[222];
int pri[maxp],pnum,cur_p[555],cnt_p[555];
int tot,ans,v[maxp];
void get_prime(int n)
{
    vis[1]=1;
    for(int i=2;i*i<=n;i++)
        if(!vis[i])
          for(int j=i*i;j<=n;j+=i)
            vis[j]=1;
    pnum=0;
    for(int i=2;i<=n;i++)
        if(!vis[i])
          pri[pnum++]=i;
}
void split(int n)
{
    tot=0;
    for(int i=0;i<pnum&&(pri[i]-1)*(pri[i]-1)<=n;i++)
        if(n%(pri[i]-1)==0)
           cur_p[tot++]=pri[i];
}
int judge(int n)
{
    if(n==1) return n;
    n++;
    for(int i=0;i<pnum&&pri[i]*pri[i]<=n;i++)
        if(n%pri[i]==0) return -1;
    for(int i=0;i<tot;i++)
        if(v[i]&&n==cur_p[i]) return -1;
    return n;
}
void dfs(int left,int now,int c)
{
    if(c==tot)
    {
        int ret=judge(now);
        if(ret>0)
            ans=min(ans,left*ret);
        return ;
    }
    dfs(left,now,c+1);
    if(now%(cur_p[c]-1)==0)
    {
        v[c]=1;
        left*=cur_p[c];
        now/=cur_p[c]-1;
        while(1)
        {
            dfs(left,now,c+1);
            if(now%cur_p[c]) return ;
            now/=cur_p[c];
            left*=cur_p[c];
        }
        v[c]=0;
    }
}
void solve(int n)
{
    memset(done,false,sizeof(done));
    memset(v,false,sizeof(v));
    ans=1e8*2;
    split(n);
    dfs(1,n,0);
}
int main()
{
    get_prime(10000);
    int n,cas=1;
    while(scanf("%d",&n)!=EOF&&n)
    {
        solve(n);
        printf("Case %d: %d %d\n",cas++,n,ans);
    }
    return 0;
}

你可能感兴趣的:(UVA 10837(p345)----A Research Problem)