题目大意:
多组测试数据,每组给定 1≤L≤2×10^9,求至少多少个 8连在一起组成正整数,使得它是 L 的倍数。若不存在输出0.
样例输入:
8
11
16
0
样例输出:
Case 1: 1
Case 2: 2
Case 3: 0
解题:
若一个数由n个8组成,这个数可以写成:
要求能被L整除,即:
又和互素,故有:
令,
根据题意需要求满足上式成立的最小n,即.
当(10,C)=1时有解,根据欧拉定理有:
满足
求出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;
}