[HDU2462] The Luckiest number [2008 Asia Hefei Regional Contest Online G]

题意

给定一个数,求这个数的某一个倍数的长度,其中,倍数的每一位都是8

题解

(10x1)8/90(modL)

(10x1)80(mod9L)

10x10(mod9L/gcd(9L,8))

10x1(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 ;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值