poj3696 The Luckiest numbe

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

题意:找出一个能整除l的数,这个数每一位都是8,不存在输出0。
考点:欧拉函数
这个题就是先求一个等比数列的和也就是8*(1+10+10^2+10^3+10^4..10^n-1)结果也就是an,然后我们就得到an=(8/9)(10^n-1),题目要求是an%l==0,那么我们就先求一下8和l的gcd,令l=l/gcd(l,8),那么我们就可以得到(10^n-1)%( 9 l)==0,进而得到10^n==1(mod 9* l),如果此式满足的话那么就是gcd(10,9*l)==1;最后我们再求一下phi(9*l),用快速幂求一下他的因子,找到最小满足的就行了。
在用快速幂的时候跟大佬学习了一下,套用了快速乘。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cstring>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#define PI acos(-1)
ll gcd(ll a,ll b)
{
    return b==0? a:gcd(b,a%b);
}
ll get_eul(ll x)
{
    ll ans=x;
    for(int i=2;i*i<=x;i++)
    {
        if(x%i==0)
        {
            ans=ans/i*(i-1);
            while(x%i==0) x=x/i;
        }
    }
    if(x>1) ans=ans/x*(x-1);
    return ans;
}
ll mul(ll a,ll b,ll mo)
{
    ll ans=0;
    while(b)
    {
        if(b&1)
        {
            ans+=a;
            if(ans>mo) ans=ans%mo;
        }
        a=a+a;
        if(a>mo)
            a=a%mo;
        b>>=1;
    }
    return ans;
}
ll ksm(ll a,ll b,ll mo)
{
    ll ans=1;
    while(b)
    {
        if(b&1) 
        {
            ans=mul(ans,a,mo);
        }
        a=mul(a,a,mo);
        b>>=1;
    }
    //cout<<ans<<endl;
    return ans;

}
ll l;
int main()
{
    int nn=1;
    while(~scanf("%lld",&l)&&l)
    {
        l=l/gcd(l,8);
        l=l*9;
        printf("Case %d: ",nn++);
        if(gcd(l,10)!=1)
        {
            printf("0\n");
        }
        else
        {
            ll cnt=get_eul(l);
            ll ans=cnt;
            for(ll i=1;i*i<=cnt;i++)
            {
                if(cnt%i==0)
                {
                    if(ksm(10,i,l)%l==1) ans=min(ans,i);
                    if(ksm(10,cnt/i,l)%l==1) ans=min(ans,cnt/i);
                }
            }
            printf("%lld\n",ans);
        }
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/mmk27_word/article/details/81226948