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

2008 Asia Hefei Regional Contest Online by USTC


给定一个数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=

(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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#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(a1)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<


你可能感兴趣的:(poj,数论)