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\n");
        }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\n",ans);
        }
    }
    return 0;
}

 

 

转载于:https://www.cnblogs.com/liyinggang/p/5536093.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值