zoukankan      html  css  js  c++  java
  • hdu 2462(欧拉定理+高精度快速幂模)

    The Luckiest number

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 980    Accepted Submission(s): 301


    Problem 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
    做这个题的前提有两个公式:1.(a/b)%mod = a%(b*mod)/b%mod  2.待证明的公式: ax%b = 0 => a%(b/gcd(x,b)) 详情参见我的上一篇博客
    这个题我们可以化成 (8*(10^k-1)/9)%L = 0 ---->  8*(10^k-1)%(9*L) 求出 d = gcd(9*L)
    然后根据2化成 (10^k-1) % (9*L/d) => (10^k)%(9*L) = 1 又可以根据欧拉定理来求解了。。和我的上一题差不多。。但是这个题更坑,pow_mod是满足不了精度的,总之
    参考了n多博客和资料才弄出这题。。高精度快速幂模模板get
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <iostream>
    #include <stdlib.h>
    #include <math.h>
    using namespace std;
    typedef long long LL;
    LL e[1000][2];
    LL phi(LL x)
    {
        LL ans=x;
        for(LL i=2; i*i<=x; i++)
            if(x%i==0)
            {
                ans=ans/i*(i-1);
                while(x%i==0) x/=i;
            }
        if(x>1)
            ans=ans/x*(x-1);
        return ans;
    }
    LL gcd(LL a,LL b)
    {
        return b==0?a:gcd(b,a%b);
    }
    void devide(LL ans,int &id)
    {
        for(LL i=2; i*i<=ans; i++) ///分解质因数
        {
            if(ans%i==0)
            {
                e[id][0]=i;
                e[id][1]=0;
                while(ans%i==0) ans/=i,e[id][1]++;
                id++;
            }
        }
        if(ans>1)
        {
            e[id][0]=ans;
            e[id++][1]=1;
        }
    }
    LL modular_multi(LL a, LL b, LL c) {/// a * b % c
        LL res, temp;
        res = 0, temp = a % c;
        while (b) {
            if (b & 1) {
                res += temp;
                if (res >= c) {
                    res -= c;
                }
            }
            temp <<= 1;
            if (temp >= c) {
                temp -= c;
            }
            b >>= 1;
        }
        return res;
    }
    LL modular_exp(LL a, LL b, LL c) { ///a ^ b % c 改成mod_pow就不行,中间发生了溢出,还是这个模板靠谱
        LL res, temp;
        res = 1 % c, temp = a % c;
        while (b) {
            if (b & 1) {
                res = modular_multi(res, temp, c);
            }
            temp = modular_multi(temp, temp, c);
            b >>= 1;
        }
        return res;
    }
    int main()
    {
        LL l;
        int t= 1;
        while(~scanf("%lld",&l),l)
        {
            printf("Case %d: ",t++);
            LL d = gcd(8,9*l);
            LL a = 9*l/d;
            if(gcd(a,10)!=1){
                printf("0
    ");
            }else{
                LL ans = phi(a);
                int id = 0;
                devide(ans,id);
                for(int i=0;i<id;i++){
                    for(int j=0;j<e[i][1];j++){
                        if(modular_exp(10,ans/e[i][0],a)==1) ans/=e[i][0];
                    }
                }
                printf("%lld
    ",ans);
            }
        }
        return 0;
    }
     
  • 相关阅读:
    Spring学习总结(六)——Spring整合MyBatis完整示例
    Spring学习总结(五)——Spring整合MyBatis(Maven+MySQL)二
    Spring学习总结(五)——Spring整合MyBatis(Maven+MySQL)一
    Spring学习总结(四)——表达式语言 Spring Expression Language
    Spring学习总结(三)——Spring实现AOP的多种方式
    Spring学习总结(二)——静态代理、JDK与CGLIB动态代理、AOP+IoC
    Spring集成MyBatis完整示例
    数据库字符集的坑
    MYSQL中的UNION和UNION ALL
    MySQL的事务和锁
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5536093.html
Copyright © 2011-2022 走看看