POJ 3696 The Luckiest Number 欧拉函数

/*
    《数论及应用》P164
*/
#pragma warning(disable:4786)
#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<cmath>
#include<string>
#include<sstream>
#define LL long long
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define mem(a,x) memset(a,x,sizeof(a))
#define lson l,m,x<<1
#define rson m+1,r,x<<1|1
using namespace std;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double PI = acos(-1.0);
const double eps=1e-6;
LL gcd(LL a , LL b)
{
    return b == 0 ? a : gcd( b , a % b) ;
}
LL phi(LL n)
{
    LL ret = n ;
    for(LL i = 2 ; i * i <= n ; i++){
        if(n % i == 0){
            ret = ret / i * ( i - 1 ) ;
            while(n % i == 0){
                n /= i ;
            }
        }
    }
    if(n > 1)       ret = ret / n * (n - 1);
    return ret;
}
LL mul(LL a , LL b , LL mod)  //两个长整型相乘可能发生长整型溢出的现象
{
    LL n = 0;
    while(b){
        if(b & 1)
            n = (n + a) % mod;
        a = a * 2 % mod ;
        b >>= 1;
    }
    return n;
}
LL quick_mod(LL x , LL n , LL mod)
{
    if(n == 0)      return 1;
    LL res = quick_mod( mul(x , x , mod) , n / 2 , mod);
    if(n & 1)   res = mul( res , x , mod ) ;
    return res;
}
int main()
{
    LL L ;
    int kase = 0 ;
    while(scanf("%lld",&L)==1 && L){
        LL d = gcd( (LL)8 , L );
        L = (LL)9  * L / d ;
        if(gcd(L , (LL) 10) != 1){
            printf("Case %d: 0\n",++kase);
            continue;
        }
        LL P = phi( L ) , ans = P ;
        for(LL i = 1 ; i * i <=P ; i++){
            if( P % i == 0 ){
                LL res = quick_mod( (LL) 10 , i ,  L );
                if(res ==  1)
                    ans = min(ans , i);
                res = quick_mod( (LL) 10 ,  (P / i) , L );
                if(res ==  1)
                    ans = min(ans ,  P / i );
            }
        }
        printf("Case %d: %lld\n",++kase , ans);
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值