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'.

InputThe 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.OutputFor 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 参考网上的题解:
思路;

注意到凡是那种11111..... 22222..... 33333.....之类的序列都可用这个式子来表示:k*(10^x-1)/9
进而简化:8 * (10^x-1)/9=L * k (k是一个整数)
8*(10^x-1)=9L*k
d=gcd(9L,8)=gcd(8,L)
8*(10^x-1)/d=9L/d*k
令p=8/d q=9L/d p*(10^x-1)=q*k
因为p,q互质,所以q|(10^x-1),即10^x-1=0(mod q),也就是10^x=1(mod 9*L/d)
由欧拉定理可知,当q与10互质的时候,10^(φ(q))=1 (mod q),即必定存在一个解x。
而题目中要求的是最小的解,设为min,那么有a^min=1%q,因为要满足a^φ(q)=1%q,那么a^φ(q)肯定能变换成(a^min)^i。
所以接下来只要枚举φ(q)的因子,找出符合条件的最小者即可。

无解的时候就是q与10不互质的时候,因为若q与10有公因子d:
1.若d=2,q=2*k,那么10^x=2^x*5^x=1%2k
   即2^x*5^x=1+2k*m,左边为偶数,右边为奇数,显然矛盾。
2.若d=5,q=5*k,那么10^x=2^x*5^x=1%5k
   即2^x*5^x=1+5k*m,左边是5的倍数,右边不是5的倍数,显然矛盾。

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<map>
#include<cmath>
const int maxn=1e5+5;
typedef long long ll;
using namespace std;
ll L;
long long multi(long long a,long long b,long long mod) {
long long ret=0;
while(b) {
if(b&1)
ret=(ret+a)%mod;
a=(a<<1)%mod;
b=b>>1;
}
return ret;
}
long long quickPow(long long a,long long b,long long mod) {
long long ret=1;
while(b) {
if(b&1)
ret=multi(ret,a,mod);
a=multi(a,a,mod);
b=b>>1;
}
return ret;
} long long eular(long long n) {
long long ret=1,i;
for(i=2; i*i<=n; i++) {
if(n%i==0) {
n=n/i;
ret*=i-1;
while(n%i==0) {
n=n/i;
ret*=i;
}
}
}
if(n>1)
ret*=n-1;
return ret;
} int main() {
int t=0;
while(scanf("%lld",&L)!=EOF) {
if(L==0)
break;
long long p=9*L/__gcd(L,(ll)8);
long long d=__gcd((ll)10,p);
if(d==1) {
long long phi=eular(p);
long long ans=phi;
long long m=sqrt((double)phi);
bool flag=false;
for(int i=1; i<=m; i++) {
if(phi%i==0 && quickPow(10,i,p)==1) {
ans=i;
flag=true;
break;
}
}
if(!flag) {
for(int i=m; i>=2; i--) {
if(phi%i==0 && quickPow(10,phi/i,p)==1) {
ans=phi/i;
break;
}
}
}
printf("Case %d: %lld\n",++t,ans);
} else {
printf("Case %d: 0\n",++t);
}
}
return 0;
}

  

最新文章

  1. 加载AssetBundle方法
  2. zabbix 用 LLD 完全自动化监控 Oracle
  3. MySQL数据库索引的4大类型以及相关的索引创建
  4. Winform ListView 元素拖动
  5. 转载java源代码阅读方法
  6. UIButton 一个控件 实现 左图标右文本的效果
  7. 菜鸟笔记之java中方法使用
  8. Delphi如何让程序最小化到任务栏(截取WM_SYSCOMMAND后,调用Shell_NotifyIcon)
  9. CUDA samples 2.3节 用CUDA示例来创建CUDA项目
  10. 开源来自百度商业前端数据可视化团队的超漂亮动态图表--ECharts
  11. Spring框架--AOP编程
  12. Lamp环境下设置绑定apache域名
  13. 标准IO:常用函数集合
  14. python相关资料
  15. 使用asyncsocket群聊
  16. logging模板日志格式
  17. redis 基本命令
  18. cpu高占用,线程堆栈,jstack,pstack,jmap, kill -3 pid,java(weblogic,tomcat)
  19. 大数据开发实战:Storm流计算开发
  20. Linux安装配置SVN服务器

热门文章

  1. SpringMvc web.xml配置详情
  2. Springboot 在@Configuration注解的勒种 使用@Autowired或者@value注解 读取.yml属性失败
  3. ES6 面向对象笔记
  4. xadmin 安装
  5. GitLab 系列文章
  6. 一个试图了解JVM内存模型的两年经验的初级程序员,透彻!
  7. Python中ansible的使用
  8. 学长小清新题表之UOJ 31.猪猪侠再战括号序列
  9. 完了,这个硬件成精了,它竟然绕过了 CPU...
  10. Centos7查看端口占用