POJ 3696 HDU/HDOJ 2462 2008年合肥赛区
2011-08-15 21:34
435 查看
The Luckiest number
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
Sample Output
Source
2008 Asia Hefei Regional Contest Online by USTC
题目思路:
注意到凡是那种1111111..... 2222222..... 33333.....
之类的序列都可用这个式子来表示:
k*(10^n-1)/9
进而简化:
这个题会变成:
8*(10^n-1)/9=k*m (k是一个整数)
如果gcd(8,m)=t的话
那么原始的式子可以改为:
8/t*(10^n-1)=9m/t*k
由于8/t和9m/t完全没有公约数
所以如果8/t*(10^n-1)要整除9m/t的话,必须是
10^n-1是9m/t的整数倍
转化一下就变成了:
10^n=1(mod m)的形式
可以参照我上一篇的博文进行求解
PS:上一篇的二分幂取模在这里不能用,因为这个题的数据范围太大了。
我的代码:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2932 | Accepted: 729 |
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
题目思路:
注意到凡是那种1111111..... 2222222..... 33333.....
之类的序列都可用这个式子来表示:
k*(10^n-1)/9
进而简化:
这个题会变成:
8*(10^n-1)/9=k*m (k是一个整数)
如果gcd(8,m)=t的话
那么原始的式子可以改为:
8/t*(10^n-1)=9m/t*k
由于8/t和9m/t完全没有公约数
所以如果8/t*(10^n-1)要整除9m/t的话,必须是
10^n-1是9m/t的整数倍
转化一下就变成了:
10^n=1(mod m)的形式
可以参照我上一篇的博文进行求解
PS:上一篇的二分幂取模在这里不能用,因为这个题的数据范围太大了。
我的代码:
#include<stdio.h> #include<string.h> #include<stdlib.h> typedef __int64 ll; ll prime[50000]; bool flag[50000]; void init() { ll i,j,num=0; for(i=2;i<50000;i++) { if(!flag[i]) { prime[num++]=i; for(j=i*i;j<50000;j=j+i) flag[j]=true; } } } ll gcd(ll a,ll b) { if(b==0) return a; else return gcd(b,a%b); } ll eular(ll n) { ll i,res=1; for(i=2;i*i<=n;i++) { if(n%i==0) { n=n/i; res=res*(i-1); while(n%i==0) { n=n/i; res=res*i; } } if(n==1) break; } if(n>1) res=res*(n-1); return res; } ll solve(ll n,ll fac[]) { ll i,num=0; for(i=0;prime[i]*prime[i]<=n;i++) { if(n%prime[i]==0) { n=n/prime[i]; fac[num++]=prime[i]; while(n%prime[i]==0) { n=n/prime[i]; fac[num++]=prime[i]; } } if(n==1) break; } if(n>1) fac[num++]=n; return num; } ll mmod(ll a,ll b,ll n) { a=a%n; ll res=0; while(b) { if(b&1) { res=res+a; if(res>=n) res=res-n; } a=a<<1; if(a>=n) a=a-n; b=b>>1; } return res; } ll exmod(ll a,ll b,ll n) { a=a%n; ll res=1; while(b>=1) { if(b&1) res=mmod(res,a,n); a=mmod(a,a,n); b=b>>1; } return res; } void getans(ll n,ll mod) { ll i,num,fac[100],ans=n; bool loop=true; while(loop) { loop=false; num=solve(n,fac); for(i=0;i<num;i++) { if(exmod(10,n/fac[i],mod)==1) { loop=true; if(n/fac[i]<ans) ans=n/fac[i]; } } n=ans; } printf("%I64d\n",ans); } int main() { ll n,t,l,phi; int cnt=1; init(); while(scanf("%I64d",&n)!=EOF) { if(n==0) break; printf("Case %d: ",cnt++); t=gcd(n,8); l=9*n/t; if(gcd(10,l)!=1) { printf("0\n"); continue; } phi=eular(l); getans(phi,l); } return 0; }
相关文章推荐
- POJ 3696 The Luckiest number(08合肥 数论)
- poj 3696 &&hdoj 2462 The Luckiest number(同余&欧拉) 好题
- HDU/HDOJ 4004 2011大连赛区网络赛 D题
- HDU/HDOJ 1159/POJ 1458 Common Subsequence(最长公共子序列LCS,滚动数组)
- HDU/HDOJ 3714 Error Curves 2010年成都赛区
- HDU/HDOJ 4038 2011成都赛区网络赛H题
- HDU/HDOJ 3718 成都赛区2010 Similarity
- HDU/HDOJ 3641 Treasure Hunting 2010年杭州赛区网络赛
- hdu 2462 poj 3696 The Luckiest number fzu 1017 zoj 1537 Playing with Calculator
- HDU/HDOJ 4002 2011大连赛区网络赛 数论
- POJ 3696/ HDU 2462 The Luckiest number (数论)
- POJ-1411 & HDOJ-1239 Calling Extraterrestrial Intelligence Again 解题报告
- HDU 4474 HDOJ Yet Another Multiple Problem 2012ACM亚洲赛成都赛区K题
- poj2462
- HDU/HDOJ1851 nim博弈和巴什博弈的结合
- HDOJ 1116/POJ 1386 - Play on Words 判断有向图哈密顿通路转化为判断有向图是否存在欧拉通路
- HDU/HDOJ 3829 Cat VS Dog
- hdu/hdoj 1058 Humble Numbers
- POJ 1053 Integer Inquiry && HDOJ 1047 Integer Inquiry (大数加法)
- hdu/hdoj 1081 To The Max