您的位置:首页 > 其它

The Luckiest number POJ - 3696 (欧拉函数)

2017-06-07 21:29 381 查看
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

又是欧拉函数 。。。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
ll gcd(ll x,ll y)
{
return x%y?gcd(y,x%y):y;
}
ll euler(ll n)
{
ll res=n,a=n;
for(int 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;
}
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 poww(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;
}
int main()
{
ll n;
int count=1;
for(;;)
{
scanf("%lld",&n);
if(n==0)break;
ll d=gcd(8,n);
n=n*9/d;
if(gcd(10,n)!=1)
cout<<"Case "<<count++<<": 0"<<endl;
else
{
ll phi=euler(n);
ll up=sqrt((double)phi);
ll ans=phi;
bool sign=false;
for(int i=1;i<=up;i++)
if(phi%i==0&&poww(10,(ll)i,n)==1)
{
ans=i;
sign=true;
break;
}
if(!sign)
for(int i=up;i>=2;i--)
if(phi%i==0&&poww(10,phi/i,n)==1)
{
ans=phi/i;
break;
}
cout<<"Case "<<count++<<": "<<ans<<endl;
}
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: