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
又是欧拉函数 。。。
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; }
相关文章推荐
- POJ 3696 The Luckiest number 欧拉函数 或 BSGS
- POJ 3696 : The Luckiest number - 欧拉函数,快速幂[数论好题]
- POJ 3696 The Luckiest number (欧拉函数,好题)
- poj 3696 The Luckiest number(欧拉函数+快速幂取模)
- poj 3696 The Luckiest number 欧拉函数在解a^x=1modm的应用
- poj 3696 The Luckiest number 欧拉函数在解a^x=1modm的应用
- POJ 3696 The Luckiest Number 欧拉函数
- poj 3696 The Luckiest number(欧拉函数)
- POJ 3696 The Luckiest number 欧拉定理+快速幂+GCD *
- poj 3696 The Luckiest number (数论-快速幂+欧拉定理)
- POJ 3696(The Luckiest number-MulMod-原根-乘法取模MulMod)
- POJ3696 The Luckiest number
- poj 3696 The Luckiest number——数论 (快速幂取模,乘法取模模版)
- POJ - 3696 The Luckiest number(求阶)
- POJ The Luckiest number 3696 数论(处理快速幂过程中超int64的情况)
- POJ 3696 The Luckiest number
- POJ 3696 The Luckiest number (整数的阶+欧拉定理)
- POJ 3696 The Luckiest number 推理~~难
- POJ 3696 The Luckiest number
- poj3696 The Luckiest number