您的位置:首页 > 其它

POJ 3696 The Luckiest number 推理~~难

2014-02-19 16:40 387 查看
点击打开链接

The Luckiest number

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4018 Accepted: 1059
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

2008 Asia Hefei Regional Contest Online by USTC

题目的意思是说给你一个数l,让你求另外一个数answer,answer要求能够整除l,而且全部由8组成。求answer的最短长度。
要求这个数全部有8组成,那么怎么实现呢?只需要这样:(10^k-1)/9*8就可以了。如果全部是2的话,则(10^k-1)/9*2,是几就(10^k-1)/9*几。
这个数找到了,就可以建立等式:
(10^k-1)/9*8=l*p=>8*(10^k-1)=9*l*p    (p是任意正整数)
令t=gcd(8,l),就变成:8/t*(10^k-1)=9*p*l/t    令m=9*l/t,则存在p1,使得9*l*p/8=m*p1,最后方程就变为(10^x-1)=m*p1.
也就是求同余方程10^x=1(mod m)的最小解。
欧拉定理:对任何两个互质的正整数a,m(m>=2)有a^phi(m)=1(mod m).
根据欧拉公式10^phi(m)=1(mod m),因为要求最小解,所以答案肯定是phi(m)的因子,所以最后枚举它的因子,检查模是否为1即可。
当gcd(10,m)!=1时,输出0.
证明:10^x-1肯定既不能被5整除,也不能被2整除,若gcd(10,m)=2或者5,那么肯定就没有答案。
//396K	47MS
#include<stdio.h>
#include<string.h>
long long l,s[57][2],len;
long long gcd(long long a,long long b)
{
return b==0?a:gcd(b,a%b);
}
long long eular(long long n)//求n的欧拉函数
{
long long ret=1,i;
for (i=2; i*i<=n; i++)
if (n%i==0)
{
n/=i,ret*=i-1;
while (n%i==0)
n/=i,ret*=i;
}
if (n>1)
ret*=n-1;
return ret;//返回n的欧拉函数
}
long long get(long long n)
{
len=0;
for(long long i=2;i*i<=n;i++)
{
if(n%i==0)
{
s[len][0]=i;s[len][1]=0;
//printf("ssssss%d\n",s[len][1]);
do{n/=i;s[len][1]++;}while(n%i==0);
len++;
}
}
if(n>1){s[len][0]=n;s[len++][1]=1;}
}
long long multi(long long a,long long b,long long m)//a*b%m
{
long long ret=0;
while(b>0)
{
if(b&1)ret=(ret+a)%m;
b>>=1;
a=(a<<1)%m;
}
return ret;
}
long long quick_mod(long long a,long long b,long long m)//a^b%m
{
long long ans=1;
a%=m;
while(b)
{
if(b&1)
{
ans=multi(ans,a,m);
b--;
}
b/=2;
a=multi(a,a,m);
}
return ans;
}
int main()
{
int cas=1;
while(scanf("%lld",&l),l)
{
printf("Case %d: ",cas++);
long long m=9*l/gcd(8,l);
if(gcd(10,m)!=1){printf("0\n");continue;}
memset(s,0,sizeof(s));
long long phi_sum=eular(m);
get(phi_sum);
for(int i=0;i<len;i++)
for(int j=1;j<=s[i][1];j++)
if(quick_mod(10,phi_sum/s[i][0],m)==1)phi_sum/=s[i][0];
printf("%lld\n",phi_sum);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: