POJ 3696 The Luckiest number 推理~~难


The Luckiest number
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4018 Accepted: 1059

Description

Chinese people think of '8' as the lucky digit. Bob also likes digit '8'. Moreover, Bob has his own lucky number L. Now he wants to construct his luckiest number which is the minimum among all positive integers that are a multiple of L and consist of only digit '8'.

Input

The input consists of multiple test cases. Each test case contains exactly one line containing L(1 ≤ L ≤ 2,000,000,000).

The last test case is followed by a line containing a zero.

Output

For each test case, print a line containing the test case number( beginning with 1) followed by a integer which is the length of Bob's luckiest number. If Bob can't construct his luckiest number, print a zero.

Sample Input

8
11
16
0

Sample Output

Case 1: 1
Case 2: 2
Case 3: 0

Source


题目的意思是说给你一个数l,让你求另外一个数answer,answer要求能够整除l,而且全部由8组成。求answer的最短长度。
要求这个数全部有8组成,那么怎么实现呢?只需要这样:(10^k-1)/9*8就可以了。如果全部是2的话,则(10^k-1)/9*2,是几就(10^k-1)/9*几。
这个数找到了,就可以建立等式:
(10^k-1)/9*8=l*p=>8*(10^k-1)=9*l*p    (p是任意正整数)
令t=gcd(8,l),就变成:8/t*(10^k-1)=9*p*l/t    令m=9*l/t,则存在p1,使得9*l*p/8=m*p1,最后方程就变为(10^x-1)=m*p1.
也就是求同余方程10^x=1(mod m)的最小解。
欧拉定理:对任何两个互质的正整数a,m(m>=2)有a^phi(m)=1(mod m).
根据欧拉公式10^phi(m)=1(mod m),因为要求最小解,所以答案肯定是phi(m)的因子,所以最后枚举它的因子,检查模是否为1即可。
当gcd(10,m)!=1时,输出0.
证明:10^x-1肯定既不能被5整除,也不能被2整除,若gcd(10,m)=2或者5,那么肯定就没有答案。
//396K	47MS
#include<stdio.h>
#include<string.h>
long long l,s[57][2],len;
long long gcd(long long a,long long b)
{
    return b==0?a:gcd(b,a%b);
}
long long eular(long long n)//求n的欧拉函数
{
    long long ret=1,i;
    for (i=2; i*i<=n; i++)
        if (n%i==0)
        {
            n/=i,ret*=i-1;
            while (n%i==0)
                n/=i,ret*=i;
        }
    if (n>1)
        ret*=n-1;
    return ret;//返回n的欧拉函数
}
long long get(long long n)
{
    len=0;
    for(long long i=2;i*i<=n;i++)
    {
        if(n%i==0)
        {
            s[len][0]=i;s[len][1]=0;
            //printf("ssssss%d\n",s[len][1]);
            do{n/=i;s[len][1]++;}while(n%i==0);
            len++;
        }
    }
    if(n>1){s[len][0]=n;s[len++][1]=1;}
}
long long multi(long long a,long long b,long long m)//a*b%m
{
    long long ret=0;
    while(b>0)
    {
        if(b&1)ret=(ret+a)%m;
        b>>=1;
        a=(a<<1)%m;
    }
    return ret;
}
long long quick_mod(long long a,long long b,long long m)//a^b%m
{
    long long ans=1;
    a%=m;
    while(b)
    {
        if(b&1)
        {
            ans=multi(ans,a,m);
            b--;
        }
        b/=2;
        a=multi(a,a,m);
    }
    return ans;
}
int main()
{
    int cas=1;
    while(scanf("%lld",&l),l)
    {
        printf("Case %d: ",cas++);
        long long m=9*l/gcd(8,l);
        if(gcd(10,m)!=1){printf("0\n");continue;}
        memset(s,0,sizeof(s));
        long long phi_sum=eular(m);
        get(phi_sum);
        for(int i=0;i<len;i++)
            for(int j=1;j<=s[i][1];j++)
                if(quick_mod(10,phi_sum/s[i][0],m)==1)phi_sum/=s[i][0];
        printf("%lld\n",phi_sum);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值