poj_3696_The Luckiest number

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

题目化简如下:
  8*(10^x -1)/9=n*L
  d=gcd(8,n)
  8*(10^x  -1)/d=9*n*L/d
  p=8/d,q=9*n/d
  p*(10^x  -1)=L*q
  p,q互质,10^x=1 mod q
  

根据同余定理可知,10^x ≡1(mod q)

根据欧拉定理可知当gcd(a,b)==1时,a^φ(b)≡1(mod b);

即可得出:当gcd(10,q)==1时    10^φ(q)≡1(mod q)   即通过枚举φ(q)的因子(最小因子)就能得出结果




#include<iostream>
#include<cstdio>
#include<algorithm>
#define LL long long
#define ll long long
#define N 1000010
using namespace std;
int prime[N];
int pn=0;
bool vis[N];
int ans[N];
LL gcd(LL a,LL b)
{
     return b==0?a:gcd(b,a%b);
}
long long P(long long  n){ //返回euler(n)
     long long  res=n,a=n;
     for(long long  i=2;i*i<=a;i++){
         if(a%i==0){
             res=res/i*(i-1);//先进行除法是为了防止中间数据的溢出
             while(a%i==0) a/=i;
         }
     }
     if(a>1) res=res/a*(a-1);
     return res;
}
ll mul(ll a,ll b,ll m)
{
    ll res=0;
    while(b)
    {
        if(b&1) res+=a;
        if(res>m) res-=m;
        a+=a;
        if(a>m)
        a-=m;
        b>>=1;
    }
    return res;
}
ll pow(ll a,ll b,ll m)
{
    ll res=1;
    while(b)
    {
        if(b&1) res=mul(res,a,m);
        a=mul(a,a,m);
        b>>=1;
    }
    return res;
}
ll p[10000],cnt[10000];
ll fac[100000];
ll cc,s;
ll phi(ll n)
{
    ll ans=1,i;
    for(i=2;i*i<=n;i++)
    {
        if(n%i==0)
        {
            n/=i;
            ans*=i-1;
            while(n%i==0)
            {
                n/=i;ans*=i;
            }
        }
    }
    if(n>1)
    ans*=n-1;
    return ans;
}
void split(ll x)
{
    cc=0;
    for(ll i=2;i*i<=x;i++)
    {
        if(x%i==0)
        {
            p[cc]=i;
            int count=0;
            while(x%i==0)
            {
                count++;x/=i;
            }
            cnt[cc]=count;
            cc++;
        }
    }
    if(x>1)
    {
        p[cc]=x;cnt[cc]=1;cc++;
    }
}
void dfs(ll count,ll step)
{
    if(step==cc)
    {
        fac[s++]=count;
        return ;
    }
    ll sum=1;
    for(int i=0;i<cnt[step];i++)
    {
        sum*=p[step];
        dfs(count*sum,step+1);
    }
    dfs(count,step+1);
}
int main()
{
    //freopen("i.txt","r",stdin);
    //freopen("o.txt","w",stdout);
    LL n;
    int t=0;
    while(~scanf("%lld",&n),n)
    {
        LL cnt=1;
        int flag=0;
        t++;
        printf("Case %d: ",t);
        LL q=9*n/gcd(8,n);
        if(gcd(q,10)!=1)
        {
            puts("0");
            continue;
        }
        LL phi=P(q);
        split(phi);
        s=0;
        dfs(1,0);
        sort(fac,fac+s);
        //cout<<ans[0]<<endl;
        for(int i=0;i<s;i++)
        {
            if(pow(10,fac[i],q)==1)
            {
                cout<<fac[i]<<endl;
                break;
            }
        }


    }
}

  

posted @ 2017-11-22 22:20  会飞的雅蠛蝶  阅读(138)  评论(0编辑  收藏  举报