uva10837 A Research Problem

来源:互联网 发布:卡西欧授权的淘宝网店 编辑:程序博客网 时间:2024/06/03 07:00

A mad researcher was trying to get fund for his research project but
it is a pity that after a year he was able to collect 500 $ only. So
all his research work has gone to ashtray. The mad researcher now
wants his revenge, so he wants you to solve his un nished research
problem within a very limited time. You will be happy to know that his
research is related to Eulers phi function. Euler’s phi (or totient)
function of a positive integer n is the number of integers in f 1 ; 2
; 3 ; : : : ; n g which are relatively prime to n . This is usually
denoted as φ ( n ). The table below shows the value of phi function
for rst few numbers. integer n 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
φ ( n ) 1 1 2 2 4 2 6 4 6 4 10 4 12 6 8 8 Given the value of n , it is
very easy to nd the value of φ ( n ) using the formula below: φ ( n )
= n ∏ p j n (1

根据phi(n)=n * (p1-1)/p1 * (p2-1)/p2 * … * (pk-1)/pk,所有n的质因数-1一定是m的因数。找到sqrt(m)以内的所有质数,对每个质数是否取以及次数进行搜索,最后再判断一下大于sqrt(m)的因数【最多只有一个,次数最高为1】。

#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>using namespace std;#define LL long longconst int maxp=100000,oo=0x3f3f3f3f;int p[100000],tot,a[100000],cnt,m,ans;bool have[100010];bool is(int x){    int i;    int m=sqrt(x+0.1);    for (i=1;i<=tot&&p[i]<=m;i++)      if (x%p[i]==0)        return 0;    return 1;}void make(){    int i,j;    for (i=2;i<=maxp;i++)    {        if (!have[i]) p[++tot]=i;        for (j=1;j<=tot&&(LL)i*p[j]<=maxp;j++)        {            have[i*p[j]]=1;            if (i%p[j]==0) break;        }    }}void init(){    int i;    cnt=0;    for (i=1;i<=tot;i++)      if (m%(p[i]-1)==0)        a[++cnt]=p[i];}void dfs(int k,LL n,LL phi){    if (phi==m) ans=min(ans,(int)n);    if (k==cnt+1)    {        if (m%phi==0&&m/phi>a[cnt]&&is(m/phi+1))          ans=min(ans,(int)(n*(m/phi+1)));        return;    }    dfs(k+1,n,phi);    phi*=(a[k]-1);    for (n*=a[k];n<=ans;n*=a[k],phi*=a[k])      dfs(k+1,n,phi);}int main(){    int K=0;    make();    while (scanf("%d",&m)&&m)    {        printf("Case %d: %d ",++K,m);        init();        ans=oo;        dfs(1,1,1);        printf("%d\n",ans);    }}
0 0
原创粉丝点击