1. 程式人生 > >Poj3696 The Lukiest Number

Poj3696 The Lukiest Number

pre nbsp tchar space mod mes scanf b- show

傳送門

Solution

懶得寫啦

Code

技術分享圖片
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #define Rg register
 5 #define go(i,a,b) for(Rg int i=a;i<=b;i++)
 6 #define yes(i,a,b) for(Rg int i=a;i>=b;i--)
 7 #define ll long long
 8 using namespace std;
 9 int
read() 10 { 11 int x=0,y=1;char c=getchar(); 12 while(c<0||c>9){if(c==-)y=-1;c=getchar();} 13 while(c>=0&&c<=9){x=(x<<3)+(x<<1)+c-0;c=getchar();} 14 return x*y; 15 } 16 ll T,L,d,n,m,ans,c; 17 bool flag=0; 18 ll gcd(ll x,ll y){return y==0
?x:gcd(y,x%y);} 19 ll phi(ll x) 20 { 21 ll sm=x,y=x; 22 go(i,2,sqrt(y)) 23 { 24 if(x%i==0)sm=sm/i*(i-1); 25 while(x%i==0)x/=i; 26 } 27 if(x>1)sm=sm/x*(x-1); 28 return sm; 29 } 30 ll mul(ll x,ll y,ll mod) 31 { 32 ll sm=0; 33 while(y) 34 {
35 if(y&1)sm=(sm+x)%mod; 36 x=(x<<1)%mod;y>>=1; 37 } 38 return sm; 39 } 40 ll ksm(ll x,ll y,ll mod) 41 { 42 ll sm=1; 43 while(y) 44 { 45 if(y&1)sm=mul(sm,x,mod);//註意這裏直接乘起來會溢出 46 x=mul(x,x,mod);y>>=1; 47 } 48 return sm; 49 } 50 int main() 51 { 52 while(1) 53 { 54 scanf("%lld",&L);if(!L)break;T++;flag=0;ans=0; 55 if(8%L==0){printf("Case %lld: 1\n",T);continue;} 56 d=gcd(L,8);n=9*L/d;m=phi(n);c=sqrt(m); 57 go(i,2,c) 58 { 59 if(m%i==0&&ksm(10,i,n)==1){ans=i;flag=1;break;} 60 } 61 if(!flag) 62 yes(i,c,2) 63 { 64 if(m%i==0&&ksm(10,m/i,n)==1){ans=m/i;break;} 65 } 66 printf("Case %lld: %lld\n",T,ans); 67 } 68 return 0; 69 }
View Code

Poj3696 The Lukiest Number