poj3696The Luckiest number【欧拉函数】

Language:
The Luckiest number
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4968 Accepted: 1332

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


题意:给出一个数N 求一个数M的长度L且M是N的倍数求只有8组成

参考博客:http://blog.csdn.net/huanghongxun/article/details/50966715

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<list>
#include<stack>
#include<vector>
using namespace std;
const int maxn = 100010;
long long num[2][60];
long long gcd(long long a,long long b){
    return b==0?a:gcd(b,a%b);
}
long long Euler(long long n){
     long long ret=1,i;
    for(i=2;i*i<=n;i++){
        if(n%i==0){
            n/=i;ret*=i-1;
            while(n%i==0){
                n/=i;ret*=i;
            }
        }
    }
    if(n>1)ret*=n-1;
    return ret;
}
int get_factor(long long n){
    int cnt=0;
    for(long long i=2;i*i<=n;++i){
        if(n%i==0){
            num[0][cnt]=i;num[1][cnt]=0;
            while(n%i==0){
                num[1][cnt]++;n/=i;
            }
            cnt++;
        }
    }
    if(n>1)num[0][cnt]=n;num[1][cnt++]=1;
    return cnt;
}
long long multi(long long a,long long b,long long c) {
    long long ans = 0;
    while (b) {
        if (b & 1) {
            ans += a;ans %= c;
        }
        a += a;a %= c;
        b >>= 1;
    }
    return ans;
}
long long Pow(long long a, long long b, long long c) {
    long long ans = 1;
    while (b) {
        if (b & 1) {
            ans = multi(ans, a, c);
        }
        a = multi(a, a, c);
        b >>= 1;
    }
    return ans;
}
int main()
{
    int test=1;
    long long n;
    while(scanf("%lld",&n),n){
        printf("Case %d: ",test++);
        long long q=9*n/gcd(n,8);
        if(gcd(10,q)!=1){
            printf("%d\n",0);
            continue;
        }
        long long phi_q=Euler(q);
        int cnt=get_factor(phi_q);
        for(int i=0;i<cnt;++i){
            for(int j=1;j<=num[1][i];++j){
                if(Pow(10,phi_q/num[0][i],q)==1)phi_q/=num[0][i];
            }
        }
        printf("%lld\n",phi_q);
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值