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. DedeCms中Channel用typeid无效

    DedeCms中channel 用typeid调用无法达目的吗?请换成type试试! {dede:channel type='son' typeid='19' row='1'} <a href= ...

  2. 百度BAE数据库连接问题

    今天第一次使用百度的开发平台BAE,按照入门文档上的操作一步步来,进行的很顺利,可是我在上传了一个cms系统后,进行安装时,卡在了数据库连接这个地方,弄了一下午,终于有了结果,在这里记录起来,希望能帮 ...

  3. C面向对象编程

    C语言面向对象编程 1. 定义一个SuperObject结构体, 里面包含最少的元素, 但是确实每一个对象都含有的, 这样可以实现多态 2. 每一个对象都是基于类的, 我们知道类都是单例对象, 所以我 ...

  4. HDU 5351——MZL's Border——————【高精度+找规律】

    MZL's Border Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  5. System.Net.Mail

    System.Net.Mail命名空间包含用于将电子邮件发送到简单邮件传输协议(SMTP)服务器进行传送的类. 在此命名空间中,有两个很重要的类:  MailMessage 表示可以使用SmtpCli ...

  6. Weblogic中配置Active Directory Authentication Provider

    其要点或者容易出错的关键点是:(<>及其中说明代表需要替换的内容)         Host: ads.yourdomain.com         Host填AD服务器的域名或IP    ...

  7. C#入门--字段与属性

    C#入门--字段与属性 “字段”,它是包含在类或结构中的对象或值.字段使类和结构可以封装数据. 属性是这样的成员:它们提供灵活的机制来读取.编写或计算私有字段的值.可以像使用公共数据成员一样使用属性, ...

  8. SQL简单语句(增删改查)

    简单的SQL语句增删改查操作 说明: 在mysql里面亲测结果正确    用到的表(学生表:studnets) 1.创建一个学生表,(学号,姓名,性别,家庭住址) mysql> create t ...

  9. poj 1155 输入输出问题

    http://acm.hust.edu.cn/vjudge/problem/16417 重做了一遍poj 1155 题目大意:给定一棵树,1为根结点表示电视台,有m个叶子节点表示客户,有n-m-1个中 ...

  10. XHML教会我的一些东西-3

    在寒假期间,隔几天就同学聚会,每天都是起床困难户.每天都想着要完成任务,要学习新的东西.但是总是被自己惰性占为上风.感觉自己很没用,但是又继续堕落.真的不能理解自己.呵呵.... 在放假一段时间之后, ...