关闭

POJ 3696 The Luckiest Number 欧拉函数

标签: poj数论
97人阅读 评论(0) 收藏 举报
分类:
/*
    《数论及应用》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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:15501次
    • 积分:1372
    • 等级:
    • 排名:千里之外
    • 原创:128篇
    • 转载:0篇
    • 译文:0篇
    • 评论:15条
    最新评论