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 题目化简如下:
  8*(10^x -1)/9=n*L
  d=gcd(8,n)
  8*(10^x  -1)/d=9*n*L/d
  p=8/d,q=9*n/d
  p*(10^x  -1)=L*q
  p,q互质,10^x=1 mod q
  

根据同余定理可知,10^x ≡1(mod q)

根据欧拉定理可知当gcd(a,b)==1时,a^φ(b)≡1(mod b);

即可得出:当gcd(10,q)==1时    10^φ(q)≡1(mod q)   即通过枚举φ(q)的因子(最小因子)就能得出结果


#include<iostream>
#include<cstdio>
#include<algorithm>
#define LL long long
#define ll long long
#define N 1000010
using namespace std;
int prime[N];
int pn=0;
bool vis[N];
int ans[N];
LL gcd(LL a,LL b)
{
return b==0?a:gcd(b,a%b);
}
long long P(long long n){ //返回euler(n)
long long res=n,a=n;
for(long long i=2;i*i<=a;i++){
if(a%i==0){
res=res/i*(i-1);//先进行除法是为了防止中间数据的溢出
while(a%i==0) a/=i;
}
}
if(a>1) res=res/a*(a-1);
return res;
}
ll mul(ll a,ll b,ll m)
{
ll res=0;
while(b)
{
if(b&1) res+=a;
if(res>m) res-=m;
a+=a;
if(a>m)
a-=m;
b>>=1;
}
return res;
}
ll pow(ll a,ll b,ll m)
{
ll res=1;
while(b)
{
if(b&1) res=mul(res,a,m);
a=mul(a,a,m);
b>>=1;
}
return res;
}
ll p[10000],cnt[10000];
ll fac[100000];
ll cc,s;
ll phi(ll n)
{
ll ans=1,i;
for(i=2;i*i<=n;i++)
{
if(n%i==0)
{
n/=i;
ans*=i-1;
while(n%i==0)
{
n/=i;ans*=i;
}
}
}
if(n>1)
ans*=n-1;
return ans;
}
void split(ll x)
{
cc=0;
for(ll i=2;i*i<=x;i++)
{
if(x%i==0)
{
p[cc]=i;
int count=0;
while(x%i==0)
{
count++;x/=i;
}
cnt[cc]=count;
cc++;
}
}
if(x>1)
{
p[cc]=x;cnt[cc]=1;cc++;
}
}
void dfs(ll count,ll step)
{
if(step==cc)
{
fac[s++]=count;
return ;
}
ll sum=1;
for(int i=0;i<cnt[step];i++)
{
sum*=p[step];
dfs(count*sum,step+1);
}
dfs(count,step+1);
}
int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout);
LL n;
int t=0;
while(~scanf("%lld",&n),n)
{
LL cnt=1;
int flag=0;
t++;
printf("Case %d: ",t);
LL q=9*n/gcd(8,n);
if(gcd(q,10)!=1)
{
puts("0");
continue;
}
LL phi=P(q);
split(phi);
s=0;
dfs(1,0);
sort(fac,fac+s);
//cout<<ans[0]<<endl;
for(int i=0;i<s;i++)
{
if(pow(10,fac[i],q)==1)
{
cout<<fac[i]<<endl;
break;
}
} }
}

  

poj_3696_The Luckiest number的更多相关文章

  1. [题解](同余)POJ_3696_The Luckiest Number

    还是挺难的吧......勉强看懂调了半天 首先表达式可以写成 8(10^x -1)/9,题意为求一个最小的x使L | 8(10^x -1)/9 设d=gcd(L,8) L | 8(10^x -1)/9 ...

  2. poj 3696 The Luckiest Number

    The Luckiest Number 题目大意:给你一个int范围内的正整数n,求这样的最小的x,使得:连续的x个8可以被n整除. 注释:如果无解输出0.poj多组数据,第i组数据前面加上Case ...

  3. POJ3696 The Luckiest number

    题意 Language:Default The Luckiest number Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7 ...

  4. POJ_3696 The Luckiest number 【欧拉定理+同余式+对取模的理解】

    一.题目 Chinese people think of '8' as the lucky digit. Bob also likes digit '8'. Moreover, Bob has his ...

  5. POJ3696:The Luckiest number(欧拉函数||求某数最小的满足题意的因子)

    Chinese people think of '8' as the lucky digit. Bob also likes digit '8'. Moreover, Bob has his own ...

  6. HDU 2462 The Luckiest number

    The Luckiest number Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Ori ...

  7. The Luckiest number(hdu2462)

    The Luckiest number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  8. [POJ3696]The Luckiest number(数论)

    题目:http://poj.org/problem?id=3696 题意:给你一个数字L,你要求出一个数N,使得N是L的倍数,且N的每位数都必须是8,输出N的位数(如果不存在输出0) 分析: 首先我们 ...

  9. POJ 3696 The Luckiest number (欧拉函数,好题)

    该题没思路,参考了网上各种题解.... 注意到凡是那种11111..... 22222..... 33333.....之类的序列都可用这个式子来表示:k*(10^x-1)/9进而简化:8 * (10^ ...

随机推荐

  1. (转)Memcached用法--参数和命令详解

    Memcached用法--参数和命令详解 1. memcached 参数说明: # memcached -h 1.1 memcached 的参数 常用参数 -p <num> 监听的TCP端 ...

  2. instancemethod, staticmethod, classmethod & abstractmethod

    实例方法.静态方法.类方法.抽象方法 1.  Python中方法的工作方式(How methods work in Python) A method is a function that is sto ...

  3. loadrunner如何设置所有虚拟用户只运行一次脚本?

    1,设置所有虚拟用户只运行一次脚本 进入场景conroller,如下图设置.初始化和启动Vuser设置都可以,看具体要求,但持续时间一定要选择“完成前一直运行”,这样设置就可以让所有Vuser只运行一 ...

  4. POJ 1861 ——Network——————【最小瓶颈生成树】

    Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15268   Accepted: 5987   Specia ...

  5. 导入的项目eclipse出现乱码的处理方法

    如果这样子还是出现乱码无法解决的话,则删掉当前项目重新开始项目.

  6. 划分用户故事(user-story)的原则

    在敏捷开发过程中是通过用户故事来将需求具体化成可以进行迭代开发的一个个现实的可见的开发任务.因此在敏捷软件的开发过程中,用户故事的划分对于迭代和开发起着举足轻重的作用. 用户故事从其名字来看是站在用户 ...

  7. asp.net 单元测试(转)

    最早接触单元测试是看了极限编程相关资料里边讲的测试驱动开发,然后下载了Nunit研究了一下,但并没产生多大的触动,因为那个时候做的都是些时间紧任务重的事情,对于单元测试的直接感觉就是有可能比较费时间. ...

  8. qingdao

    1001 #include <bits/stdc++.h> using namespace std; vector<long long> v; long long pow2(l ...

  9. linux文件按照行数切割

    公司小站的nginx服务器日志一直没有管理 到发现的时候已经有50G+的logs文件 查看起来十分不便 只能将其切割 接下来分享 具体方法 split -l 5000(行数) -a 5(增加文件名上限 ...

  10. android三大组件之Intent

    Android 应用程序中有三大核心组件: Activity, Service, Broadcast Receiver 都是通过被称之为意图的消息运行. Intent messaging is a f ...