uva10837 A Research Problem

137 篇文章 0 订阅
23 篇文章 0 订阅

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 long
const 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值