poj3696(欧拉定理+快速幂+快速乘)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qkoqhh/article/details/82057364

这个。。连续的8要被表示成8/9(10^x-1),然后原题变成求方程8/9(10^x-1)=pL的最小解,继续化简得

8/gcd(8,L)*(10^x-1)=9pL/gcd(8,L)

由于8/gcd(8,L)和9L/gcd(8,L)互质,所以10^x-1必能整除9L/gcd(8,L),令m=9L/gcd(8,L),化简成为求10^x\equiv1(mod\,\,m)的最小解

然后当m与10不互质的时候,10^x-1显然无法整除m(因为10^x-1与10互质),所以此时无解

当互质的时候可以发现φ(m)必是方程一解,而如果有更小的解x存在,x必是φ(m)的因子,否则x^(φ(m)%x)就不与1同余了

因此可以尝试性对φ(m)除以它的因子代入同余方程验证,逐步得到最小解。。

然后快速幂的时候由于余数达到10^10。。直接乘会爆ll。。而poj又没int128。。所以只能写快速乘。。

/**
 *          ┏┓    ┏┓
 *          ┏┛┗━━━━━━━┛┗━━━┓
 *          ┃       ┃  
 *          ┃   ━    ┃
 *          ┃ >   < ┃
 *          ┃       ┃
 *          ┃... ⌒ ...  ┃
 *          ┃              ┃
 *          ┗━┓          ┏━┛
 *          ┃          ┃ Code is far away from bug with the animal protecting          
 *          ┃          ┃   神兽保佑,代码无bug
 *          ┃          ┃           
 *          ┃          ┃        
 *          ┃          ┃
 *          ┃          ┃           
 *          ┃          ┗━━━┓
 *          ┃              ┣┓
 *          ┃              ┏┛
 *          ┗┓┓┏━━━━━━━━┳┓┏┛
 *           ┃┫┫       ┃┫┫
 *           ┗┻┛       ┗┻┛
 */
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<bitset>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1LL<<(x))
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 100005
#define nm 200005
#define N 1000005
#define M(x,y) x=max(x,y)
const double pi=acos(-1);
const ll inf=1e9+7;
using namespace std;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}
  







ll m,c[NM],tot,_t,n,ca;
ll plu(ll x,ll y){
    ll s=0;
    while(y){
	if(y&1)s=(s+x)%m;
	x<<=1;
	if(x>=m)x%=m;
	y>>=1;
    }
    return s;
}
ll qpow(ll x,ll t){return t?plu(qpow(plu(x,x),t>>1),(t&1?x:1)):1LL;}
ll ph(ll n){
    ll s=n;
    for(ll i=2;i*i<=n;i++)if(n%i==0){
	s-=s/i;while(n%i==0)n/=i;
    }
    if(n>1)s-=s/n;return s;
}

int main(){
    while(m=read()){
	m=m/__gcd(m,8LL);m*=9;_t=n=ph(m);tot=0;
	if(__gcd(m,10LL)>1){printf("Case %lld: 0\n",++ca);continue;}
	for(ll i=2;i*i<=_t;i++)if(_t%i==0){
	    _t/=i;c[++tot]=i--;
	}
	if(_t>2)c[++tot]=_t;
	inc(i,1,tot)if(qpow(10,n/c[i])==1)n/=c[i];
	printf("Case %lld: %lld\n",++ca,n);
    }
    return 0;
}

The Luckiest number

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6398   Accepted: 1720

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

[Submit]   [Go Back]   [Status]   [Discuss]

猜你喜欢

转载自blog.csdn.net/qkoqhh/article/details/82057364