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

8
11
16
0

Sample Output

Case 1: 1
Case 2: 2
Case 3: 0 参考网上的题解:
思路;

注意到凡是那种11111..... 22222..... 33333.....之类的序列都可用这个式子来表示:k*(10^x-1)/9
进而简化:8 * (10^x-1)/9=L * k (k是一个整数)
8*(10^x-1)=9L*k
d=gcd(9L,8)=gcd(8,L)
8*(10^x-1)/d=9L/d*k
令p=8/d q=9L/d p*(10^x-1)=q*k
因为p,q互质,所以q|(10^x-1),即10^x-1=0(mod q),也就是10^x=1(mod 9*L/d)
由欧拉定理可知,当q与10互质的时候,10^(φ(q))=1 (mod q),即必定存在一个解x。
而题目中要求的是最小的解,设为min,那么有a^min=1%q,因为要满足a^φ(q)=1%q,那么a^φ(q)肯定能变换成(a^min)^i。
所以接下来只要枚举φ(q)的因子,找出符合条件的最小者即可。

无解的时候就是q与10不互质的时候,因为若q与10有公因子d:
1.若d=2,q=2*k,那么10^x=2^x*5^x=1%2k
   即2^x*5^x=1+2k*m,左边为偶数,右边为奇数,显然矛盾。
2.若d=5,q=5*k,那么10^x=2^x*5^x=1%5k
   即2^x*5^x=1+5k*m,左边是5的倍数,右边不是5的倍数,显然矛盾。

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<map>
#include<cmath>
const int maxn=1e5+5;
typedef long long ll;
using namespace std;
ll L;
long long multi(long long a,long long b,long long mod) {
long long ret=0;
while(b) {
if(b&1)
ret=(ret+a)%mod;
a=(a<<1)%mod;
b=b>>1;
}
return ret;
}
long long quickPow(long long a,long long b,long long mod) {
long long ret=1;
while(b) {
if(b&1)
ret=multi(ret,a,mod);
a=multi(a,a,mod);
b=b>>1;
}
return ret;
} long long eular(long long n) {
long long ret=1,i;
for(i=2; i*i<=n; i++) {
if(n%i==0) {
n=n/i;
ret*=i-1;
while(n%i==0) {
n=n/i;
ret*=i;
}
}
}
if(n>1)
ret*=n-1;
return ret;
} int main() {
int t=0;
while(scanf("%lld",&L)!=EOF) {
if(L==0)
break;
long long p=9*L/__gcd(L,(ll)8);
long long d=__gcd((ll)10,p);
if(d==1) {
long long phi=eular(p);
long long ans=phi;
long long m=sqrt((double)phi);
bool flag=false;
for(int i=1; i<=m; i++) {
if(phi%i==0 && quickPow(10,i,p)==1) {
ans=i;
flag=true;
break;
}
}
if(!flag) {
for(int i=m; i>=2; i--) {
if(phi%i==0 && quickPow(10,phi/i,p)==1) {
ans=phi/i;
break;
}
}
}
printf("Case %d: %lld\n",++t,ans);
} else {
printf("Case %d: 0\n",++t);
}
}
return 0;
}

  

F - 丘 (欧拉函数)的更多相关文章

  1. Codeforces Round #538 (Div. 2) F 欧拉函数 + 区间修改线段树

    https://codeforces.com/contest/1114/problem/F 欧拉函数 + 区间更新线段树 题意 对一个序列(n<=4e5,a[i]<=300)两种操作: 1 ...

  2. Please, another Queries on Array?(Codeforces Round #538 (Div. 2)F+线段树+欧拉函数+bitset)

    题目链接 传送门 题面 思路 设\(x=\prod\limits_{i=l}^{r}a_i\)=\(\prod\limits_{i=1}^{n}p_i^{c_i}\) 由欧拉函数是积性函数得: \[ ...

  3. BZOJ 2818: Gcd [欧拉函数 质数 线性筛]【学习笔记】

    2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 4436  Solved: 1957[Submit][Status][Discuss ...

  4. COGS2531. [HZOI 2016]函数的美 打表+欧拉函数

    题目:http://cogs.pw/cogs/problem/problem.php?pid=2533 这道题考察打表观察规律. 发现对f的定义实际是递归式的 f(n,k) = f(0,f(n-1,k ...

  5. 欧拉函数 - HDU1286

    欧拉函数的作用: 有[1,2.....n]这样一个集合,f(n)=这个集合中与n互质的元素的个数.欧拉函数描述了一些列与这个f(n)有关的一些性质,如下: 1.令p为一个素数,n = p ^ k,则 ...

  6. uva11426 gcd、欧拉函数

    题意:给出N,求所有满足i<j<=N的gcd(i,j)之和 这题去年做过一次... 设f(n)=gcd(1,n)+gcd(2,n)+......+gcd(n-1,n),那么answer=S ...

  7. UVA11426 欧拉函数

    大白书P125 #include <iostream> #include <cstring> using namespace std; #define MMX 4000010 ...

  8. HDU 1695 GCD (欧拉函数+容斥原理)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  9. 欧拉函数 cojs 2181. 打表

    cojs 2181. 打表 ★☆   输入文件:sendtable.in   输出文件:sendtable.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] 有一道比赛题 ...

  10. BZOJ2186 欧拉函数

    欧拉函数:一般记作φ(n),表示1-n中与n互质的数的数量. 欧拉函数是积性函数,即φ(m*n)=φ(m)*φ(n) //这条定理基友面试时还遇到了= = 欧拉函数的值φ(n)=n*(1-p[1])* ...

随机推荐

  1. Python | 常见的反爬及解决方法,值得收藏

    我们都知道Python用来爬数据,为了不让自家的数据被别人随意的爬走,你知道怎么反爬吗?今天播妞带着大家一起见识见识常见的反爬技术. 很多人学习python,不知道从何学起.很多人学习python,掌 ...

  2. java多线程(三):多线程单例模式,双重检查,volatile关键字

    一.事先准备 首先准备一个运行用的代码: public class Singleton { public static void main(String[] args) { Thread[] thre ...

  3. 用Java制作斗地主

    首先,按照斗地主规则,完成洗牌发牌的动作.如图: 具体规则: 1. 组装54张扑克牌 2. 将54张牌顺序打乱 3. 三个玩家参与游戏,三人交替摸牌,每人17张牌,最后三张留作底牌. 4. 查看三人各 ...

  4. Java 方法的重载及引用数据类型(类)

    方法的重载 我们假设要在程序中实现一个对数字求和的方法,由于参与求和数字的个数和类型都不确定,因此要针对不同的情况去设计不同的方法. Java允许在一个类中定义多个名称相同的方法,但是参数的类型或个数 ...

  5. docker基础入门理解

    本文简单的介绍了一下docker的一些优点,以及使用方法 1. 理解docker 1.1 docker是什么? 1.2 为什么要使用Docker? 2. docker安装 3. docker-容器,镜 ...

  6. ubuntu 绝望事件

    @ubuntu.com hi!大家好,早上发生了很有意思的事情 显示器分辨率(x2) 系统 2560x1440 Ubuntu 20.04.1 LTS 上面的表格是现在的环境 开机进入锁屏页面,正常显示 ...

  7. eric4 中pyqt 字符串 输入 获取

    在eric4中使用pyqt需要注意: 输入 中文 时,前面加 u ,例如: from PyQt4.QtGui import * from PyQt4.QtCore import * QMessageB ...

  8. Kubernetes 的层级命名空间介绍

    原文链接:https://fuckcloudnative.io/posts/introducing-hierarchical-namespaces/ 在单个 Kubernetes 集群上安全托管大量用 ...

  9. vue watch 和 computed 区别与使用

    目录 computed 和 watch 的说明 与 区别 computed 计算属性说明: watch 监听属性说明: watch 和 computed 的区别是: 使用 参考官方文档 compute ...

  10. 创建SpringMVC项目

    学习SpringMVC框架第一步,先创建一个简单项目,了解流程.使用的是Eclipse+Tomcat9.0 创建项目springmvc 新建Dynamic Web Project File->N ...