The Luckiest Number(POJ 3696,模板)

原题题面:The Luckiest Number
思路:

这题是非常好的一个模板题,考察了欧拉定理,快乘,快速幂,欧拉函数,筛质因数,
x x x 8 8 8 连在一起组成的正整数等价于 8 ( 1 0 x − 1 ) 9 \cfrac{8(10^x - 1)}{9} 98(10x1) 。其中 x x x 就是该正整数的十进制位数。而题目就是让我们求出这样一个 x x x,满足 L ∣ 8 ( 1 0 x − 1 ) 9 L|\cfrac{8(10^x - 1)}{9} L98(10x1)
在这里,我们令 d = g c d ( L , 8 ) d=gcd(L, 8) d=gcd(L,8)
推导可以得出: L ∣ 8 ( 1 0 x − 1 ) 9 ⇔ 9 L ∣ 8 ( 1 0 x − 1 ) ⇔ 9 L d ∣ 8 d ( 1 0 x − 1 ) ⇔ 9 L d ∣ ( 1 0 x − 1 ) ⇔ 1 0 x ≡ 1 ( m o d   9 L d ) L|\cfrac{8(10^x - 1)}{9} \Leftrightarrow 9L|8(10^{x} - 1) \Leftrightarrow \frac{9L}{d}|\frac{8}{d}(10^x - 1) \Leftrightarrow \frac{9L}{d}|(10^x - 1)\Leftrightarrow 10^x\equiv1(mod\ \frac{9L}{d}) L98(10x1)9L8(10x1)d9Ld8(10x1)d9L(10x1)10x1(mod d9L)

欧拉定理:
若正整数 a , n a,n a,n 互质,则 a ϕ ( n ) ≡ 1 ( m o d   n ) a^{\phi(n)}\equiv 1(mod\ n) aϕ(n)1(mod n),其中 ϕ ( n ) \phi(n) ϕ(n) n n n 的欧拉函数。
由欧拉定理可以看出, a = 10 a=10 a=10 n = 9 L d n=\cfrac{9L}{d} n=d9L,而 x x x 集合中的最小正整数 x 0 x_0 x0 ϕ ( n ) \phi(n) ϕ(n) 的一个约数。
所以,我们只要依次求出欧拉函数 ϕ ( 9 L d ) \phi(\cfrac{9L}{d}) ϕ(d9L),然后枚举它的所有约数,再用快速幂检查当前约数是否满足条件即可。

代码如下:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

typedef long long LL;
typedef pair<LL, int> PII;
const int N = 45000, M = 50;

LL primes[N];
int cnt;
bool st[N];

PII factor[M];
LL divider[N];
int cnt_f, cnt_d;

LL gcd(LL a, LL b)
{
    return b ? gcd(b, a % b) : a;
}
//线性筛法筛质数板子
void get_primes(LL n)
{
    for(int i = 2; i <= n; i++)
    {
        if(!st[i])  primes[cnt ++] = i;
        for(int j = 0; primes[j] <= n / i; j++)
        {
            st[i * primes[j]] = true;
            if(i % primes[j] == 0)  break;
        }
    }
}
//求欧拉函数板子
LL get_phi(LL n)
{
    LL res = n;
    for(int i = 0; i < cnt && primes[i] <= n / primes[i]; i++)
    {
        LL p = primes[i];
        if(n % p == 0)
        {
            res = res / p * (p - 1);
            while(n % p == 0)   n /= p;
        }
    }
    if(n > 1)   res = res / n * (n - 1);
    return res;
}
//筛质因数板子
void get_fac(LL n)
{
    cnt_f = 0;
    for(int i = 0; i < cnt && primes[i] <= n / primes[i]; i++)
    {
        LL p = primes[i];
        if(n % p == 0)
        {
            int s = 0;
            while(n % p == 0)
            {
                n /= p;
                s ++;
            }
            factor[++ cnt_f] = {p, s};
        }
    }
    if(n > 1)   factor[++ cnt_f] = {n, 1};
}
//深搜枚举约数板子
void dfs(int u, LL p)
{
    if(u > cnt_f)
    {
        divider[cnt_d ++] = p;
        return ;
    }
    for(int i = 0; i <= factor[u].second; i++)
    {
        dfs(u + 1, p);
        p *= factor[u].first;
    }
}
//快乘板子
LL qk_mul(LL a, LL b, LL p)
{
    LL ans = 0;
    while(b)
    {
        if(b & 1)   ans = (ans + a) % p;
        a = (a * 2) % p;
        b >>= 1;
    }
    return ans;
}
//快速幂板子
bool qmi(LL a, LL b, LL mod)
{
    LL ans = 1 % mod;
    a %= mod;
    while(b)
    {
        if(b & 1)   ans = qk_mul(ans, a, mod);
        a = qk_mul(a, a, mod);
        b >>= 1;
    }
    if(ans == 1)    return true;
    return false;
}

LL solve(LL l)
{
    LL n = l / gcd(8LL, l) * 9;
    if(gcd(10LL, n) != 1)   return 0;

    LL phi = get_phi(n);
    get_fac(phi);

    cnt_d = 0;
    dfs(1, 1);

    for(int i = 0; i < cnt_d; i++)
    {
        LL x = divider[i];
        if(qmi(10, x, n))
            return x;
    }
    return 0;
}

int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

    LL l;
    int Count = 1;
    get_primes(N);
    while(cin >> l)
    {
        if(l == 0)  break;
        cout << "Case " << Count++ << ": " << solve(l) << endl;
    }

    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值