The Luckiest number POJ - 3696 (欧拉函数)

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
又是欧拉函数 。。。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
ll gcd(ll x,ll y)
{
    return x%y?gcd(y,x%y):y;
}
ll euler(ll n)
{
    ll res=n,a=n;
    for(int i=2;i*i<a;i++)
        if(a%i==0)
    {
        res=res/i*(i-1);
        while(a%i==0)
        a/=i;
    }
    if(a>1)
        res=res/a*(a-1);
    return res;
}
long long multi(long long a,long long b,long long mod) {
    long long ret=0;
    while(b) {
        if(b&1)
            ret=(ret+a)%mod;
        a=(a<<1)%mod;
        b=b>>1;
    }
    return ret;
}
long long poww(long long a,long long b,long long mod) {
    long long ret=1;
    while(b) {
        if(b&1)
            ret=multi(ret,a,mod);  //直接相乘的话可能会溢出
        a=multi(a,a,mod);
        b=b>>1;
    }
    return ret;
}
int main()
{
    ll n;
    int count=1;
    for(;;)
    {
        scanf("%lld",&n);
        if(n==0)break;
        ll d=gcd(8,n);
        n=n*9/d;
        if(gcd(10,n)!=1)
            cout<<"Case "<<count++<<": 0"<<endl;
        else
        {
            ll phi=euler(n);
            ll up=sqrt((double)phi);
            ll ans=phi;
            bool sign=false;
            for(int i=1;i<=up;i++)
                if(phi%i==0&&poww(10,(ll)i,n)==1)
                {
                    ans=i;
                    sign=true;
                    break;
                }
            if(!sign)
            for(int i=up;i>=2;i--)
                if(phi%i==0&&poww(10,phi/i,n)==1)
                {
                    ans=phi/i;
                    break;
                }
            cout<<"Case "<<count++<<": "<<ans<<endl;
        }
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值