Poj-3696 The Luckiest number(数论)

本文介绍了一个有趣的问题:如何找出由数字8组成的、最小的且能被特定整数整除的正整数。通过数学推导和算法实现,文章提供了一种有效的方法来解决这个问题。

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

Chinese people think of '8' as the lucky digit. Bob also likes digit '8'. Moreover, Bob has his own lucky number L. Now he wants to construct his luckiest number which is the minimum among all positive integers that are a multiple of L and consist of only digit '8'.

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


分析: WA到怀疑人生,最后找了网上的代码发现是取模时溢出了...

88888....可以表示为(10^k-1)/9*8,现在我们想让(10^k-1)/9*8 = L*m,也就是8*(10^k-1) = 9*L*m,去掉8和L的公约数,整个式子可以化简为p*(10^k-1) = m*q,其中p q互质,那么我们就是要找到最小的k使得
(10^k-1) % q = 0,也就是10^k   1 (% q),如果q和10不互质则无解,否则我们可以先用欧拉函数求出一个解,然后枚举这个解的所有因数来得到最小的解.

#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
#include <math.h>
using namespace std;
typedef long long  ll;
int Time;
ll l;
ll muti(ll a,ll b,ll MOD)
{
    ll ans = 0;
    while(b)
    {
        if(b & 1) ans = (ans + a) % MOD;
        a = (a<<1) % MOD;
        b>>=1;
    }
    return ans;
}
ll ksm(ll x,ll y,ll MOD)
{
    ll ans = 1;
    while(y)
    {
        if(y & 1) ans = muti(ans,x,MOD);
        x = muti(x,x,MOD);
        y >>= 1;
    }
    return ans;
}
ll euler_phi(ll n)
{
    ll ans = n;
    for(ll i = 2;i*i <= n;i++)
     if(n % i == 0)
     {
        ans = ans / i * (i-1);
        while(n % i == 0) n/=i;
     }
    if(n > 1) ans = ans / n * (n-1);
    return ans;
}
ll log_mod(ll a,ll b)
{
    ll temp = euler_phi(b),now = temp;
 //   cout<<temp<<endl;
    if(now == 1) return now;
    for(ll i = 1;i*i <= temp;i++)
     if(temp % i == 0)
     {
        if(ksm(a,i,b) == 1)
        {
            now = min(now,i);
            break;
        }
        if(ksm(a,temp/i,b) == 1) now = min(now,temp/i);
     }
    return now;
}
int main()
{
	while(cin>>l && l)
	{
		ll p = 9*l/__gcd(l,8ll);
        if(__gcd(p,10ll) != 1ll)  printf("Case %d: 0\n",++Time);
		else printf("Case %d: %I64d\n",++Time,log_mod(10,p));
	}
}
/*/
999999999
2000000000
/*/



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值