[poj3696][数论]The Luckiest number

探讨了如何通过数学推导解决一个数论问题:找到构成特定整数倍数所需的最少8的数量。利用数论中的概念,将问题转化为求解同余方程,并通过BSGS算法或欧拉定理来寻找最小的指数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

给一个数n,问至少要用多少个8才能组成n的倍数,无解输出0

Input

The input consists of multiple test cases. Each test case contains
exactly one line containing L(1 ≤ L ≤ 2,000,000,000).

The last test case is followed by a line containing a zero.

Output

For each test case, print a line containing the test case number(
beginning with 1) followed by a integer which is the length of Bob’s
luckiest number. If Bob can’t construct his luckiest number, print a
zero.

Sample Input

8
11
16
0

Sample Output

Case 1: 1
Case 2: 2
Case 3: 0

题解

我的数论是真的烂啊
容易推出式子
8(10x+10x1+...+100)0(modn)8∗(10x+10x−1+...+100)≡0(modn)
中间是个等比数列可以写成
8(10x1)/90(modn)8∗(10x−1)/9≡0(modn)
改写成不定方程的形式可以推出
8(10x1)/9=nk8∗(10x−1)/9=n∗k
10x1=nk9/810x−1=n∗k∗9/8
观察到左边一定是整数,则右边也同为整数
对于n,他能贡献的因子是gcd(n,8)gcd(n,8),则k需要贡献8/gcd(n,8)8/gcd(n,8)这些因子
原式可写为
10x1=n9q/gcd(n,8)10x−1=n∗9∗q/gcd(n,8)
m=9n/gcd(n,8)m=9∗n/gcd(n,8),仍可改写为10x1=qm10x−1=q∗m,移项可得10x=1+qm10x=1+q∗m,这是个同余方程
10x1(modm)10x≡1(modm)
喜欢可以直接BSGS,或者有欧拉定理这个x一定为ϕnϕn的因数
证明不再概述
注意一下爆long long的问题

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<iostream>
using namespace std;
typedef long long LL;
int tt;
LL L;
LL gcd(LL a,LL b){return a==0?b:gcd(b%a,a);}
inline LL mul(LL a,LL b,LL mod)
{
    LL ret=0;
    while(b)
    {
        if(b&1)ret=(ret+a)%mod;
        a=(a+a)%mod;b>>=1;
    }
    return ret;
}
inline LL pow_mod(LL a,LL b,LL mod)
{
    LL ret=1;
    while(b)
    {
        if(b&1)ret=mul(ret,a,mod)%mod;
        a=mul(a,a,mod)%mod;b>>=1;
    }
    return ret;
}
int main()
{
    while(scanf("%lld",&L)!=EOF)
    {
        if(!L)break;
        LL m=L/gcd(L,8)*9;
        tt++;
        if(gcd(10,m)!=1){printf("Case %d: 0\n",tt);continue;}
        LL mpos=LL(sqrt(double(m+1)));
        LL phi=m;
        for(int i=2;i<=mpos;i++)
            if(m%i==0)
            {
                while(m%i==0)m/=i;
                phi=phi/i*(i-1);
            }
        if(m!=1)phi=phi/m*(m-1);
        m=L/gcd(L,8)*9;
        LL ans=(1LL<<63-1);mpos=LL(sqrt(double(phi+1)));
        for(LL i=1;i<=mpos;i++)
            if(phi%i==0)
            {
                if(pow_mod(10,i,m)==1)ans=min(ans,i);
                if(i!=phi/i)
                    if(pow_mod(10,phi/i,m)==1)ans=min(ans,phi/i);
            }
        printf("Case %d: %lld\n",tt,ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值