poj3696 The Luckiest number
来源:互联网 发布:海上风电 知乎 编辑:程序博客网 时间:2024/05/24 07:10
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
811160
Sample Output
Case 1: 1Case 2: 2Case 3: 0//(10^x-1)=L*p*9/8;//所以m=(9*L)/(gcd(L,8))//所以10^x-1=m*p1//所以转化成求同余方程 (10^x=1)modm#include<vector> #include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;typedef long long LL;vector<LL>mp;bool book[50000];LL p[20000];LL gcd(LL a,LL b){return b==0?a:gcd(b,a%b);}void prim(){memset(book,false,sizeof(book));book[0]=book[1]=1;int k=0;for(int i=2;i<50000;i++){if(!book[i])p[k++]=i;for(int j=0;j<k&&i*p[j]<50000;j++){book[i*p[j]]=1;if(!(i%p[j]))break;}}}LL mul(LL a,LL b,LL MOD){LL ans=0;while(b){if(b&1)ans+=a,ans%=MOD;b>>=1;a+=a,a%=MOD;}return (ans%MOD+MOD)%MOD;}LL getphi(LL n){LL rea=n;for(int i=0;p[i]*p[i]<=n;i++)if(n%p[i]==0){rea-=rea/p[i];while(n%p[i]==0)n/=p[i];}if(n>1)rea=rea-rea/n;return rea;}LL quickpow(LL n,LL m,LL MOD){LL b=1;while(m){if(m&1)b=mul(b,n,MOD);m>>=1;n=mul(n,n,MOD);}return (b%MOD+MOD)%MOD;}void getfac(LL m){mp.clear();for(int i=0;p[i]*p[i]<=m;i++)if(m%p[i]==0)mp.push_back(p[i]),m/=p[i];if(m>1)mp.push_back(m); }int main(){LL L;prim();while(scanf("%lld",&L)==1){LL m=(9*L)/(gcd(L,8));LL phi=getphi(m);LL ans=phi;bool flag=true;while(flag){getfac(phi);flag=false;for(int i=0;i<mp.size();i++){if(quickpow(10,phi/mp[i],m)==1){flag=true;if(phi/mp[i]<ans)ans=(phi/mp[i]);} }phi=ans;}if(gcd(10,m)!=1)ans=0;printf("%lld\n",ans);}}
- poj3696 The Luckiest number
- #POJ3696# The Luckiest number
- poj3696 The Luckiest number 数论好题
- POJ3696 The Luckiest number【欧拉函数】
- poj3696 The Luckiest number By Assassin
- POJ3696 The Luckiest number 非常好的数论题目
- POJ3696 The Luckiest number 欧拉函数应用
- poj3696.The Luckiest number (阶 && 欧拉函数 && 欧几里德)
- 【POJ3696】The Luckiest Number-欧拉定理+快速幂
- poj The Luckiest number
- The Luckiest number POJ
- POJ 3696 The Luckiest number
- POJ 3696 The Luckiest number
- poj 3696 The Luckiest number
- poj 3696 The Luckiest number
- hdu 2462 The Luckiest number
- poj-3696 The Luckiest number
- HDU 2462 The Luckiest number
- 即将找工作的仿徨
- 夜间模式的简单实现
- centos7安装python-pip
- SIFT算法详解
- db2中like使用locate函数情况及解析
- poj3696 The Luckiest number
- 2016秋季练习
- 减少选择让你节省时间
- 建议117:使用SSL确保通信中的数据安全
- 【Go语言】基本类型排序和 slice 排序
- Linux diff 命令
- Java的for循环应用
- 黑客学习路线
- 类和对象的属性和方法