poj3696 The Luckiest number

本文介绍了一个有趣的数学问题,即如何找到最小的正整数,该整数仅由数字'8'组成,并且是给定幸运数字L的倍数。通过解析算法步骤,包括最大公约数、快速幂等技术的应用,展示了如何高效地解决这一问题。

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

The Luckiest number
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5120 Accepted: 1370

Description

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
//(10^x-1)=L*p*9/8;
//所以m=(9*L)/(gcd(L,8))
//所以10^x-1=m*p1
//所以转化成求同余方程 (10^x=1)modm
#include<vector> 
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
vector<LL>mp;
bool book[50000];
LL p[20000];
LL gcd(LL a,LL b){return b==0?a:gcd(b,a%b);}
void prim()
{
	memset(book,false,sizeof(book));
	book[0]=book[1]=1;
	int k=0;
	for(int i=2;i<50000;i++)
	{
		if(!book[i])p[k++]=i;
		for(int j=0;j<k&&i*p[j]<50000;j++)
		{
			book[i*p[j]]=1;
			if(!(i%p[j]))break;
		}
	}
}
LL mul(LL a,LL b,LL MOD)
{
	LL ans=0;
	while(b)
	{
		if(b&1)ans+=a,ans%=MOD;
		b>>=1;
		a+=a,a%=MOD;
	}
	return (ans%MOD+MOD)%MOD;
}
LL getphi(LL n)
{
	LL rea=n;
	for(int i=0;p[i]*p[i]<=n;i++)if(n%p[i]==0)
	{
		rea-=rea/p[i];
		while(n%p[i]==0)n/=p[i];
	}
	if(n>1)rea=rea-rea/n;
	return rea;
}
LL quickpow(LL n,LL m,LL MOD)
{
	LL b=1;
	while(m)
	{
		if(m&1)b=mul(b,n,MOD);
		m>>=1;
		n=mul(n,n,MOD);
	}
	return (b%MOD+MOD)%MOD;
}
void getfac(LL m)
{
	mp.clear();
	for(int i=0;p[i]*p[i]<=m;i++)if(m%p[i]==0)mp.push_back(p[i]),m/=p[i];
	if(m>1)mp.push_back(m);  
}
int main()
{
	LL L;
	prim();
	while(scanf("%lld",&L)==1)
	{
		LL m=(9*L)/(gcd(L,8));
		LL phi=getphi(m);
		LL ans=phi;
		bool flag=true;
		while(flag)
		{
			getfac(phi);
			flag=false;
			for(int i=0;i<mp.size();i++)
			{
				if(quickpow(10,phi/mp[i],m)==1){flag=true;if(phi/mp[i]<ans)ans=(phi/mp[i]);} 
			}
			phi=ans;
		}
		if(gcd(10,m)!=1)ans=0;
		printf("%lld\n",ans);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值