zoukankan      html  css  js  c++  java
  • 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

    二、题意分析

    1.首先要根据题意写出一个公式

    left ( 10^x-1 
ight ) =Lcdot kcdot 9/8

    有了这个公式,我们就可以进行下一步

    M =Lcdot 9/gcd(L,8)

    P =kcdot gcd(L,8)/8

    这样,得出,其中P其实可以发现是可以变为任意大小的整数的,所以直接不用管

    left ( 10^x-1 
ight ) =Mcdot P

    这个公式再联系同余式

    10^x-1equiv 0(mod M)

    变形

    10^xequiv 1(mod M)

    行吧,不服不行,这里我们也应该非常清楚,该同余方程有解的充分必要条件是gcd(10^x,M) = 1.根据大整数的素数分解,10的素因子只有2,5,所以进一步推出有解的条件为gcd(10,M)=1.

    那么我们其实已经分析出了没有x的条件就是gcd(10,M)!=1.

    此处结合

    欧拉定理:对任何两个互质的正整数a,m(m≥2)有a^φ(m)≡1(mod m).

    那么我们也可以得出,当gcd(10,M)=1时,有

    10^{varphi (M)}equiv 1(mod M)

    推到这里挺不容易的,但更加不幸的是,这并不意味着我们就成功了 - -!

    我们要找的是最小的x。这里我们需要知道指数的mod运算是有循环节的。我们假设上面这个欧拉定理公式的循环节长度是r。那么可以推出

    10^{varphi (M)\%r}equiv 1(mod M)

    再结合一个常识式子

    10^{0}equiv 1(mod M)

    OK,再开动我们的小脑筋,这不就是让我们求满足

    varphi (M)\%r= 0

    算你狠~

    接下来就是在M的欧拉函数值的所有因子F中,找到满足上面10^F≡1(mod M)的最小因子F,你就成功了!

    对于代码写法,我是先线性打sqrt(MAX)的素数表,然后再算欧拉函数值。时间250ms(我绝对不是二百五..)。

    三、AC代码

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    typedef long long LL;
    const int MAXN = 1e5+3;
    bool isPrime[MAXN];
    int Prime[MAXN], nPrime;
    LL Factor[MAXN], Cnt;
    
    LL Multi(LL a, LL b, LL mod)
    {
        LL ans = 0;
        while(b)
        {
            if(b&1)
            {
                ans = (ans + a)%mod;
            }
            b>>=1;
            a = (a+a)%mod;
        }
        return ans;
    }
    
    LL Pow(LL a, LL n, LL mod)
    {
        LL ans = 1;
        while(n)
        {
            if(n&1)
            {
                ans = Multi(ans, a, mod);
            }
            n>>=1;
            a = Multi(a, a, mod);
        }
        return ans;
    }
    
    LL gcd(LL a, LL b)
    {
        return b==0?a:gcd(b, a%b);
    }
    
    void getPrime() //线筛素数
    {
        memset(isPrime, 1, sizeof(isPrime));
        isPrime[0] = isPrime[1] = 0;
        nPrime = 0;
        for(int i = 2; i < MAXN; i++)
        {
            if(isPrime[i])
            {
                Prime[nPrime++] = i;
            }
            for(int j = 0; j < nPrime && (LL)i*Prime[j] < MAXN ; j++)
            {
                isPrime[ i*Prime[j] ] = 0;
                if(i%Prime[j])
                    break;
            }
        }
    }
    
    LL Euler(LL N)
    {
        LL Phi = N;
        for(int i = 0; Prime[i]*Prime[i] <= N; i++)
        {
            if(N%Prime[i] == 0)
            {
                Phi = Phi - Phi/Prime[i];
                do
                {
                    N/=Prime[i];
                }while(N%Prime[i] == 0);
            }
        }
        if(N>1)
            Phi = Phi - Phi/N;
        return Phi;
    }
    
    LL solve(LL N)
    {
        LL M = N/gcd(N, 8)*9;
        if(gcd(10, M) != 1)
        {
            return 0;
        }
        LL Phi = Euler(M);
        Cnt = 0;
        for(LL i = 1; i*i < Phi; i++)
        {
            if(Phi%i == 0)
            {
                Factor[Cnt++] = i;
                Factor[Cnt++] = Phi/i;
            }
        }
        sort(Factor, Factor+Cnt);
        for(LL i = 0; i < Cnt; i++)
        {
            if(Pow(10, Factor[i], M) == 1)
                return Factor[i];
        }
        return 0;
    }
    
    int main()
    {
        LL N;
        int cnt = 0;
        getPrime();
        while(scanf("%I64d", &N) && N)
        {
            cnt++;
            printf("Case %d: %I64d
    ", cnt, solve(N));
        }
        return 0;
    }
    

      

     

     

     

  • 相关阅读:
    SQLServer DBA 三十问
    sql server中的日期详解使用(convert)
    【转】对于表列数据类型选择的一点思考
    OLTP与OLAP
    Linux文件目录介绍及文件颜色区别
    [转贴]提问的智慧
    sql server作业管理查看/进程管理查看命令
    T-SQL中的十大注意事项
    正则表达式30分钟入门教程
    SQL中on条件与where条件的区别
  • 原文地址:https://www.cnblogs.com/dybala21/p/9749791.html
Copyright © 2011-2022 走看看