/*
《数论及应用》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;
}
POJ 3696 The Luckiest Number 欧拉函数
最新推荐文章于 2020-07-17 07:59:37 发布