POJ 3696 HDU/HDOJ 2462 2008年合肥赛区

本文探讨了如何构造一个只包含数字8且为特定数字L的最小倍数的幸运数字,详细介绍了输入输出规范及算法实现,涉及了模运算、欧拉函数、中国剩余定理等数学概念。

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

The Luckiest number
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 2932 Accepted: 729

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

Source

题目思路:
注意到凡是那种1111111..... 2222222..... 33333.....
之类的序列都可用这个式子来表示:
k*(10^n-1)/9
进而简化:
这个题会变成:
8*(10^n-1)/9=k*m (k是一个整数)
如果gcd(8,m)=t的话
那么原始的式子可以改为:
8/t*(10^n-1)=9m/t*k
由于8/t和9m/t完全没有公约数
所以如果8/t*(10^n-1)要整除9m/t的话,必须是
10^n-1是9m/t的整数倍
转化一下就变成了:
10^n=1(mod m)的形式
可以参照我上一篇的博文进行求解
PS:上一篇的二分幂取模在这里不能用,因为这个题的数据范围太大了。
我的代码:
#include<stdio.h> #include<string.h> #include<stdlib.h> typedef __int64 ll; ll prime[50000]; bool flag[50000]; void init() { ll i,j,num=0; for(i=2;i<50000;i++) { if(!flag[i]) { prime[num++]=i; for(j=i*i;j<50000;j=j+i) flag[j]=true; } } } ll gcd(ll a,ll b) { if(b==0) return a; else return gcd(b,a%b); } ll eular(ll n) { ll i,res=1; for(i=2;i*i<=n;i++) { if(n%i==0) { n=n/i; res=res*(i-1); while(n%i==0) { n=n/i; res=res*i; } } if(n==1) break; } if(n>1) res=res*(n-1); return res; } ll solve(ll n,ll fac[]) { ll i,num=0; for(i=0;prime[i]*prime[i]<=n;i++) { if(n%prime[i]==0) { n=n/prime[i]; fac[num++]=prime[i]; while(n%prime[i]==0) { n=n/prime[i]; fac[num++]=prime[i]; } } if(n==1) break; } if(n>1) fac[num++]=n; return num; } ll mmod(ll a,ll b,ll n) { a=a%n; ll res=0; while(b) { if(b&1) { res=res+a; if(res>=n) res=res-n; } a=a<<1; if(a>=n) a=a-n; b=b>>1; } return res; } ll exmod(ll a,ll b,ll n) { a=a%n; ll res=1; while(b>=1) { if(b&1) res=mmod(res,a,n); a=mmod(a,a,n); b=b>>1; } return res; } void getans(ll n,ll mod) { ll i,num,fac[100],ans=n; bool loop=true; while(loop) { loop=false; num=solve(n,fac); for(i=0;i<num;i++) { if(exmod(10,n/fac[i],mod)==1) { loop=true; if(n/fac[i]<ans) ans=n/fac[i]; } } n=ans; } printf("%I64d\n",ans); } int main() { ll n,t,l,phi; int cnt=1; init(); while(scanf("%I64d",&n)!=EOF) { if(n==0) break; printf("Case %d: ",cnt++); t=gcd(n,8); l=9*n/t; if(gcd(10,l)!=1) { printf("0\n"); continue; } phi=eular(l); getans(phi,l); } return 0; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值