题意
给定一个数,求这个数的某一个倍数的长度,其中,倍数的每一位都是8
题解
(10x−1)∗8/9≡0(modL)
(10x−1)∗8≡0(mod9L)
10x−1≡0(mod9L/gcd(9L,8))
10x≡1(mod9L/gcd(9L,8))
-
欧拉定理,也称费马-欧拉定理。
-
若
n
,
a 为正整数,且 n ,a 互素,即 gcd(a,n)=1 ,则
aϕ(n)≡1(modn)
令
n=9L/gcd(9L,8)
,如果
(gcd(10,n)>1)
ans=0
否则
x|ϕ(n)
,枚举出一个符合要求的
x
<script id="MathJax-Element-675" type="math/tex">x</script>即可
代码
/****************************************\
* Author : ztx
* Title : G - The Luckiest number
* ALG : 欧拉定理
* CMT :
(10^x-1)*8/9 \equiv 0 (\mod L)
(10^x-1)*8 \equiv 0 (\mod 9L)
10^x-1 \equiv 0 (\mod 9L/gcd(9L,8))
10^x \equiv 1 (\mod 9L/gcd(9L,8))
令 n = 9L/gcd(9L,8)
if (gcd(10,n) > 1) ans = 0
x | \phi(n)
* Time :
\****************************************/
#include <cstdio>
#define Rep(i,l,r) for(i=(l);i<=(r);i++)
#define rep(i,l,r) for(i=(l);i< (r);i++)
#define Rev(i,r,l) for(i=(r);i>=(l);i--)
#define rev(i,r,l) for(i=(r);i> (l);i--)
typedef long long ll ;
typedef double lf ;
int CH , NEG ;
template <typename TP>inline void read(TP& ret) {
ret = NEG = 0 ; while (CH=getchar() , CH<'!') ;
if (CH == '-') NEG = true , CH = getchar() ;
while (ret = ret*10+CH-'0' , CH=getchar() , CH>'!') ;
if (NEG) ret = -ret ;
}
template <typename TP>inline void readc(TP& ret) {
while (ret=getchar() , ret<'!') ;
while (CH=getchar() , CH>'!') ;
}
template <typename TP>inline void reads(TP *ret) {
ret[0]=0;while (CH=getchar() , CH<'!') ;
while (ret[++ret[0]]=CH,CH=getchar(),CH>'!') ;
ret[ret[0]+1]=0;
}
#include <cmath>
#include <algorithm>
#define infi 1E18
ll gcd(ll a, ll b) { return b ? gcd(b , a % b) : a ; }
ll mul(ll a, ll b, ll mod) {
ll ret = 0 ;
for ( ; b ; b >>= 1 , a = (a+a) % mod) if (b & 1)
ret = (ret + a) % mod ;
return ret ;
}
ll pow(ll a, ll n, ll mod) {
ll ret = 1 ;
for ( ; n>0 ; a = mul(a,a,mod) , n >>= 1) if (n & 1)// ??
ret = mul(ret,a,mod) ;
return ret ;
}
ll phi(ll n) {
ll m = sqrt(n + 0.5) , ret = n , i ;
Rep (i,2,m) 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 L ;
bool ok(ll x) {
return pow(10 , x , L) == 1 ;
}
int main() {
int kiss = 0 ;
ll n , ans , i , R ;
// #define READ
#ifdef READ
freopen(".in" ,"r",stdin ) ;
freopen(".out","w",stdout) ;
#endif
while (++kiss , scanf("%I64d", &L)!=EOF && L) { /// I64d !!
L = 9 * L / gcd(8,9*L) ;
printf("Case %d: ", kiss) ;
if (gcd(10,L) > 1) puts("0") ;
else {
n = phi(L) , ans = infi , R = sqrt(n+0.5) ;
Rep (i,1,R) if (n % i == 0) {
if (ok(i)) ans = std::min(ans, i) ;
if (ok(n / i)) ans = std::min(ans , n / i) ;
}
printf("%I64d\n", ans >= infi ? 0 : ans) ;
}
}
#ifdef READ
fclose(stdin) ; fclose(stdout) ;
#else
getchar() ; getchar() ;
#endif
return 0 ;
}