POJ3696 The Luckiest number

The Luckiest number

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6050   Accepted: 1620

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

题意:

       给定一个正整数 L,L <= 2 * 10 ^ 9。问至少多少个8连在一起的正整数是 L 的倍数?

解析:
       欧拉定理+快速幂。

       x 个 8 连在一起组成的正整数可以表示为 8(10 ^ x -1) / 9。题目就是让我们求出最小的 x,满足 L | 8(10 ^ x - 1) / 9。设 d = gcd(8 , n)。

       L | 8(10 ^ x - 1) / 9 ⇔ 9L | 8(10 ^ x -1) ⇔ 9L / d | 10 ^ x -1 ⇔ 10 ^ x ≡ 1 (mod 9L / d)。

       这里有一个结论:

若正整数 a , n 互质,则满足 a ^ x ≡ 1 (mod n) 的最小正整数 x0 是 φ(n) 的约数。

       所以我们只需要求出欧拉函数 φ(9L / d),枚举它的所有约数,用快速幂注意检查是否满足条件即可。时间复杂度O(√L log L)

代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <queue>
using namespace std;

const int Max=100100;
long long n,m,ans[Max];
int t,tot;;

inline long long get_int()
{
   long long x=0,f=1;
   char c;
   for(c=getchar();(!isdigit(c))&&(c!='-');c=getchar());
   if(c=='-') {f=-1;c=getchar();}
   for(;isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+c-'0';
   return x*f;
}

inline long long GCD(long long a,long long b)  //最大公约数 
{
   return b?GCD(b,a%b):a;
}

inline long long Euler(long long num)   //欧拉函数 
{
   long long ans=num;
   for(int i=2;i<=sqrt(num);i++)
   	 if(num % i == 0)
   	 {
   	   ans = ans / i * (i-1);
   	   while(num % i == 0) num/=i;
   	 }
   if(num > 1) ans = ans / num * (num-1);
   return ans;
}

inline long long ksc(long long a,long long b,long long c)    //快速乘 
{
   long long ans=0;
   while(b)
   {
   	 if(b&1) ans=(ans+a)%c;
   	 b=b>>1;
   	 a=(a+a)%c;
   }
   return ans;
}

inline int check(long long a,long long b,long long c)     //快速幂 
{
   long long ans=1;
   a%=c;
   while(b)
   {
   	 if(b & 1) ans = ksc(ans , a , c) %c;
   	 b = b >> 1;
   	 a=ksc(a , a , c) % c;
   }
   return ans;
}

int main()
{
   while(n=get_int())
   {
   	 if(!n) break;
   	 t++,tot=0;
   	 long long gcd=GCD(n,8);
   	 m = Euler(9 * n / gcd);
   	 int flag=0;
   	 for(int i=1;i<=sqrt(m);i++)
   	   if(m % i == 0) ans[++tot] = i,ans[++tot] = m/i;
   	 sort(ans+1,ans+tot+1);
   	 for(int i=1;i<=tot;i++)
   	   if(check(10,ans[i],9 * n / gcd) == 1)
	   {
	     flag=1, printf("Case %d: %I64d\n",t,ans[i]);
	     break;
	   }
     if(!flag) printf("Case %d: 0\n",t);
   }

   return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_38083668/article/details/81103879