AcWing 202. 最幸运的数字

传送门:202. 最幸运的数字 - AcWing题库

 题目大意:

多组测试数据,每组给定 1≤L≤2×10^9,求至少多少个 8连在一起组成正整数,使得它是 L 的倍数。若不存在输出0.

样例输入:

8
11
16
0

样例输出:

Case 1: 1
Case 2: 2
Case 3: 0

解题:

若一个数由n个8组成,这个数可以写成:

8*(10^{0}+10^{1}+...+10^{n-1})=8*\frac{10^{n}-1}{9}

要求能被L整除,即:

L|8*\frac{10^{n}-1}{9}\Rightarrow 9L|8(10^{n}-1)\Rightarrow\frac{9L}{(8,L)}|\frac{8(10^{n}-1)}{(8,L)}

\frac{9L}{(8,L)}\frac{8}{(8,L)}互素,故有:\frac{9L}{(8,L)}|(10^{n}-1)

C=\frac{9L}{(8,L)}C|(10^{n}-1)\Rightarrow 10^{n}\equiv 1(modC)

 根据题意需要求满足上式成立的最小n,即ord_{C}(10).

当(10,C)=1时有解,根据欧拉定理有:

10^{\varphi (C)}\equiv 1(modC)

ord_{C}(10)满足ord_{C}(10)|\varphi (C)

求出C以后,用公式求出\varphi (C),再枚举因数,用快速幂判断即可。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include<bitset>
#include<list> 
#include <algorithm>
#define pii pair<int,int>
#define pll pair<LL,LL>
#define pil pair<int,LL>
#define pli pair<LL,int>
#define pdd pair<db,db>
#define se second 
#define fi first
#define endl '\n'
#define rep(i,a,b) for (register int i=a;i<b;++i)
#define per(i,a,b) for (register int i=a;i>b;--i)
#define MEM(a,x) memset(a,x,sizeof(a))
#define M(x) ((x)%MOD)
#define db double
#define eps 1e-9
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
const LL MOD=998244353;
const int N=2e5+10;
LL get_phi(LL n)
{
	LL ans=n;
	rep(i,2,n/i+1)
	if(n%i==0){
		while(n%i==0) n/=i;
		ans=ans/i*(i-1);
	}
	if(n>1) ans=ans/n*(n-1);
	return ans;
}
LL qm(LL a,LL b,LL c)
{
	LL res=0;
	while(b){
		if(b&1) res=(res+a)%c;
		a=(a+a)%c;
		b>>=1;
	}
	return res%c;
}
LL qp(LL a,LL b,LL m)
{
	LL res=1;
	while(b){
		if(b&1) res=qm(res,a,m);
		a=qm(a,a,m);
		b>>=1;
	}
	return res%m;
}
void solve()
{
	int T=1,l;
	while(cin>>l&&l){
		cout<<"Case "<<T++<<": ";
		LL c=1ll*9*l/__gcd(l,8);
		if(__gcd(c,1ll*10)!=1){
			cout<<0<<endl;
			continue;
		}
		LL phi=get_phi(c);
		LL ans=1e18;
		rep(i,1,phi/i+1)
		if(phi%i==0){
			if(qp(10,i,c)==1) ans=min(ans,(LL)i);
			if(qp(10,phi/i,c)==1) ans=min(ans,phi/i);
		}
		cout<<ans<<endl;
	}
}
int main()
{
//	#ifndef ONLINE_JUDGE
//    freopen("title.in","r",stdin);
//    freopen("title.out","w",stdout);
//    #endif
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int _=1;
	//cin>>_;
	while(_--){
		solve();
	}
//	rep(i,1,_+1){
//		cout<<"Case "<<i<<": ";
//		solve();
//	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值