hdu 2462 The Luckiest number

            欧拉定理。这道数论题好凶残啊!

           首先,由题意可以得出,(10^x - 1)/ 9 * 8 = L * p(p是一个未知数,但必定是整数)。

           然后对上式进行移项处理,得:(10^x - 1) = 9 * L * p / 8。

           设m = 9 * L / gcd(L, 8),则有(10^x - 1) = m * p'。p’是必然存在的一个整数。

           然后问题就转化成为了 10^x = 1(mod m),观察此式,显然,m和10必定互质。

           于是根据欧拉定理,10^(Euler(m)) = 1(mod m) 。由于题目要求最小的解,解必然是Euler(m)的因子。

           需要注意的是,对于10^x,由于m太大,直接快速幂相乘的时候会超long long。。。。好bug,需要乘法转化一下。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<set>
#include<map>
#define LL long long
#define CLR(a, b) memset(a, b, sizeof(a))
#define REP(i, n) for(int i = 0; i < n; i ++)
using namespace std;
const int N = 400100;
bool isp[N];
vector<int> p;
vector<LL> hav;

void get_P()
{
    CLR(isp, true);p.clear();
    for(int i = 2; i < N; i ++)
    {
        if(isp[i])
        {
            p.push_back(i);
            if(i < 1111) for(int j = i * i; j < N; j += i)
            {
                isp[j] = false;
            }
        }
    }
}

LL Euler_phi(LL n)
{
    LL ret = n;
    for(int i = 0; (LL)p[i] * p[i] <= n; i ++) if(n % (LL)p[i] == 0)
    {
        ret = ret / p[i] * (p[i] - 1);
        while(n % p[i] == 0) n /= p[i];
    }
    if(n > 1) ret = ret / n * (n - 1);
    return ret;
}

LL Mul(LL a, LL b, LL mod)
{
    LL ret = 0;
    while(b)
    {
        if(b & 1)
            ret = (ret + a) % mod;
        a = a * 2 % mod;
        b >>= 1;
    }
    return ret;
}

LL Pow(LL a, LL b, LL mod)
{
    LL ret = 1;
    while(b)
    {
        if(b & 1) ret = Mul(ret, a, mod);
        a = Mul(a, a, mod);
        b >>= 1;
    }
    return ret;
}

LL gcd(LL a, LL b)
{
    return b ? gcd(b, a % b) : a;
}

void get_hav(LL n)
{
    hav.clear();
    for(int i = 0; i < p.size() && n > 1; i ++)
    {
        while(n % (LL)p[i] == 0)
        {
            n /= p[i];
            hav.push_back(p[i]);
        }
    }
    if(n > 1) hav.push_back(n);
}

int main()
{
    int cas = 1;
    LL L, ans, m, x;get_P();
    while(scanf("%I64d", &L), L)
    {
        m = 9 * L / gcd(L, 8LL);
        if(gcd(m, 10LL) != 1)
        {
            printf("Case %d: 0\n", cas ++);
            continue;
        }
        x = Euler_phi(m);
        get_hav(x);
        for(int i = 0; i < hav.size(); i ++)
        {
            if(Pow(10LL, x / hav[i], m) == 1)
                x /= hav[i];
        }
        printf("Case %d: %I64d\n", cas ++, x);
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值