国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

#POJ3696# The Luckiest number

2019-11-11 00:35:50
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

  The Luckiest number

 

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'.InputThe 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.OutputFor 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
811160Sample Output
Case 1: 1Case 2: 2Case 3: 0

題意:

給出一個(gè)數(shù)L(L<2,000,000,000),找一個(gè)最小的L的倍數(shù),使其乘積的每個(gè)數(shù),字都是8,求這個(gè)乘積的位數(shù)。

設(shè)此乘積位數(shù)為x,倍數(shù)為k

則有

(10 ^ x - 1) * 8 / 9 = k * L

      10 ^ x - 1 = 9 / 8 * k * L

令 m = 9 * L / gcd (L , 8), y = k * gcd (L , 8) / 8

若此L有解,則

1:滿足上式

2:k為整數(shù)

3:y為整數(shù)

假設(shè)1,2成立,則3若成立,L即有解

則有

(10 ^ x - 1) % m = 0

 10 ^ x % m = 1

由歐拉公式得: 若上式有解,則必有10與m互素,且x = φ(m)

φ(m)肯定為L(zhǎng)有解的一個(gè)倍數(shù),但不一定是最小的,x的最小值肯定為φ(m)的約數(shù)

于是窮舉約數(shù)檢查是否符合10 ^ x % m = 1,找到最小x

注意:

1:枚舉時(shí)只枚舉到sqrt(φ(m))再倒回去通過(guò)前半段算出后半段即可,節(jié)省時(shí)間

2:此題檢驗(yàn)時(shí)由于是10的指數(shù),容易溢出,所以快速冪相乘時(shí)改成了加法做乘法,倍增加法,原理同倍增快速冪

Code:

StatusAccepted
Time156ms
Memory1728kB
Length1063
LangC++
Submitted
Shared

#include<iostream>#include<cstdio>#include<cmath>using namespace std;typedef long long LL;LL gcd(LL A,LL B){return B==0?A:gcd(B,A%B);}LL mul(LL a, LL b, LL mod){//考慮換成m個(gè)10相加,倍增+mod	LL rt=0;	while(b){		if(b&1)	rt=(rt+a)%mod;		a=(a<<1)%mod;		b>>=1;	}	return rt;}LL qmul(LL a, LL b, LL mod){	LL s=1,tmp=a;	while(b){		if(b&1)	s=mul(s,tmp,mod);		tmp=mul(tmp,tmp,mod);		b>>=1;	}	return s;}LL Euler(LL n){	LL m=(int)sqrt(n+0.5);	LL rt=n;	for(LL i=2; i<=m; ++i)if(n%i==0){		rt=rt/i*(i-1);		while(n%i==0)n/=i;	}	if(n>1)rt=rt/n*(n-1);	return rt;}int main(){	int tot=0,d;	LL L,m;	while(scanf("%I64d",&L)&&L){		m=9*L/gcd(L,8);		printf("Case %d: ",++tot);		d=gcd(10,m);		if(d==1){			LL phi=Euler(m);			LL ans=phi;			LL p=sqrt((double)phi);			bool kk=0;			for(int i=1; i<=p; ++i)if(phi%i==0&&qmul(10,i,m)==1){				ans=i,kk=1;				break;			}			if(!kk){				for(int i=p; i>=2; --i)if(phi%i==0&&qmul(10,phi/i,m)==1){					ans=phi/i,kk=1;					break;				}			}			printf("%I64d/n",ans);		}		else {printf("0/n");}	}}


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 福泉市| 宜兰县| 卓资县| 太原市| 乌拉特前旗| 鹿邑县| 罗源县| 托里县| 龙里县| 平昌县| 平利县| 墨江| 哈尔滨市| 衡阳市| 淅川县| 肥城市| 怀远县| 大安市| 商洛市| 阜南县| 德保县| 奉贤区| 禹城市| 威信县| 阜新市| 北碚区| 墨玉县| 长治县| 资中县| 东乡族自治县| 遂宁市| 定南县| 休宁县| 营口市| 册亨县| 望江县| 荔浦县| 阜城县| 京山县| 错那县| 洛南县|