1. 程式人生 > >poj 3696 The Luckiest number (數論-快速冪+尤拉定理)

poj 3696 The Luckiest number (數論-快速冪+尤拉定理)

The Luckiest number
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4894 Accepted: 1318

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求L的倍數,且這個數全部都是8組成並且位數最小; 即:      888.....888≡0(mod L) =>888.....888=k*L =>8*(10^(n-1)+10^(n-2)+.....+10+1)=K*L =>8*(10^n-1)/9=K*L =>8*(10^n-1)=9*k*L-------------(1) 取d=gcd(8,L); a=8/d,b=L/d; 易知:(a,b)=1; 因為(a,9)=1; 所以(a,9*b)=1 令p=9*b; 則(1)式兩邊同時除以d變為: a*(10^n-1)=k*p;
----------------------------------(**) =>a*(10^n-1)≡0(mod p) =>10^n-1≡0(mod p)--------同餘的條件可除性(因為(a,p)=1,滿足除法條件) =>10^n≡1(mod p) --------------------------(2) 此時我們就已經可以看到當初的問題轉變為求最小的n,使得 (2)式成立 我們可以看出只有(10,p)=1的時候(2)式才有解----(因為如果(10,p)=d>1,則(d,a)=1,上述(**)式兩邊同時模d得-1≡0(mod d),顯然矛盾,故假設不成立) 既然得到了(a,p)=1,可以聯想到尤拉定理:(a,p)=1,則a^(φ(p))≡1 (mod p) 數論上這樣定義所要求的數: 存在整數1=<K<=m,使得a^k≡1  (mod m),這樣的最小正整數稱為a模m的階。 當(a,m)=1時 a模m的階有以下性質(設a模m的階為k): (1) k是φ(m)的因子  (2)數列:a,a^2,a^3.......a^k,a^(k+1)......是模m的週期數列,且週期為k,並且a,a^2,a^3,.............,a^k模m互不同餘; 由以上定理可以知道我們所要的答案就是p的尤拉函式的因子,只要 列舉因子然後找到最小值即可 程式碼:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <math.h>
#include <map>
#include <stdlib.h>
#include <algorithm>

#define eps 1e-5
#define inf 0x3f3f3f3f
#define Linf 0x3f3f3f3f3f3f3f3f
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b)  ((a)<(b)?(a):(b))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1 | 1
#define lc rt<<1
#define rc rt<<1 | 1
#define getx2(a)  ((a)*(a))
#define Pi acos(-1.0)

typedef long long LL;
using namespace std;
#define MM 76543
LL gcd(LL a,LL b)
{
    if(a<b)swap(a,b);
    while(b)
    {
        LL t=b;
        b=a%b;
        a=t;
    }
    return a;
}
LL getEuler(LL n)
{
    LL res=n;
    for(int i=2; i*i<n; i++)
    {
        if(n%i==0)
        {
            res=res/i*(i-1);
            while(n%i==0)n/=i;
        }
    }
    if(n>1)res=res/n*(n-1);
    return res;
}
LL MUL_mod(LL a,LL b,LL m)
{
    LL res=0;
    a=a%m;
    while(b)
    {
        if(b&1)
            res=(res+a)%m;
        b>>=1;
        a=(a+a)%m;
    }
    return (res+m)%m;
}
LL pow_mod(LL a,LL n,LL m)
{
    LL res=1;
    a=a%m;
    while(n)
    {
        if(n&1)
            res=MUL_mod(res,a,m);
        n>>=1;
        a=MUL_mod(a,a,m);
    }
    return (res+m)%m;
}
int main()
{
    LL l;
    LL cnt=0;
    while(~scanf("%I64d",&l)&&l)
    {
        LL d;
        d=gcd(8,l);
        LL q=l/d*9;
        LL p=8/d;
        LL dd=gcd(q,10);
        printf("Case %d: ",++cnt);
        if(dd!=1)
            printf("0\n");
        else
        {

            LL EulerQ=getEuler(q);
            LL ans=1;
            for(LL i=1; i*i<=EulerQ; i++)
            {
                if(EulerQ%i==0)
                {
                    LL pp=pow_mod(10,i,q);
                    if(pp==1)
                    {
                        ans=i;
                        break;
                    }
                    else
                    {
                        pp=pow_mod(10,EulerQ/i,q);
                        if(pp==1)
                        {
                            ans=EulerQ/i;
                        }
                    }
                    ///cout<<EulerQ<<" "<<ans<<" "<<i<<endl;
                }
            }
            printf("%I64d\n",ans);
        }
    }
    return 0;
}