POJ3696 The Luckiest number
题意
Language:Default
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 8 Sample Output Case 1: 1 Source |
分析
\therefore 9L | 8(10^x-1) \\
\therefore \frac{9L}{\gcd(L,8)} | 10^x-1 \\
\therefore 10^x \equiv 1 (\bmod \frac{9L}{\gcd(L,8)}) \\
\because a^x \equiv 1 (\bmod n) \rightarrow \gcd(a,n)=1,x|\varphi(n) \\
\therefore x|\varphi(\frac{9L}{\gcd(L,8)})
\]
所以枚举即可。时间复杂度\(O(\sqrt{L} \log L)\)
代码
#include<iostream>
#include<cmath>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
rg T data=0,w=1;
rg char ch=getchar();
while(!isdigit(ch)){
if(ch=='-') w=-1;
ch=getchar();
}
while(isdigit(ch))
data=data*10+ch-'0',ch=getchar();
return data*w;
}
template<class T>il T read(rg T&x){
return x=read<T>();
}
typedef long long ll;
ll L,d,k,p,s,i,num=0;
ll gcd(ll x,ll y){
return y?gcd(y,x%y):x;
}
ll ksc(ll a,ll b,ll c){
ll ans=0;
while(b){
if(b&1) ans=(ans+a)%c;
a=a*2%c;
b>>=1;
}
return ans;
}
ll ksm(ll a,ll b,ll c){
ll ans=1%c;
a%=c;
while(b){
if(b&1) ans=ksc(ans,a,c);
a=ksc(a,a,c);
b>>=1;
}
return ans;
}
ll phi(ll n){
ll ans=n;
for(i=2;i*i<=n;++i)
if(n%i==0){
ans=ans/i*(i-1);
while(n%i==0) n/=i;
}
if(n>1) ans=ans/n*(n-1);
return ans;
}
ll number(){
d=gcd(L,8);
k=9*L/d;
if(gcd(k,10)!=1) return 0;
p=phi(k);
s=sqrt((double)p);
for(i=1;i<=s;++i)
if(p%i==0&&ksm(10,i,k)==1)
return i;
for(i=s-1;i;--i)
if(p%i==0&&ksm(10,p/i,k)==1)
return p/i;
return 0;
}
int main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
while(read(L)) printf("Case %lld: %lld\n",++num,number());
return 0;
}
POJ3696 The Luckiest number的更多相关文章
- POJ3696:The Luckiest number(欧拉函数||求某数最小的满足题意的因子)
Chinese people think of '8' as the lucky digit. Bob also likes digit '8'. Moreover, Bob has his own ...
- [POJ3696]The Luckiest number(数论)
题目:http://poj.org/problem?id=3696 题意:给你一个数字L,你要求出一个数N,使得N是L的倍数,且N的每位数都必须是8,输出N的位数(如果不存在输出0) 分析: 首先我们 ...
- POJ3696 The Luckiest Number 欧拉定理
昨天终于把欧拉定理的证明看明白了...于是兴冲冲地写了2道题,发现自己啥都不会qwq 题意:给定一个正整数L<=2E+9,求至少多少个8连在一起组成正整数是L的倍数. 这很有意思么... 首先, ...
- poj 3696 The Luckiest Number
The Luckiest Number 题目大意:给你一个int范围内的正整数n,求这样的最小的x,使得:连续的x个8可以被n整除. 注释:如果无解输出0.poj多组数据,第i组数据前面加上Case ...
- POJ_3696 The Luckiest number 【欧拉定理+同余式+对取模的理解】
一.题目 Chinese people think of '8' as the lucky digit. Bob also likes digit '8'. Moreover, Bob has his ...
- poj_3696_The Luckiest number
Chinese people think of '8' as the lucky digit. Bob also likes digit '8'. Moreover, Bob has his own ...
- HDU 2462 The Luckiest number
The Luckiest number Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Ori ...
- The Luckiest number(hdu2462)
The Luckiest number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- 「POJ3696」The Luckiest number【数论,欧拉函数】
# 题解 一道数论欧拉函数和欧拉定理的入门好题. 虽然我提交的时候POJ炸掉了,但是在hdu里面A掉了,应该是一样的吧. 首先我们需要求的这个数一定可以表示成\(\frac{(10^x-1)}{9}\ ...
随机推荐
- 001-mybatis框架
架构分析: 1. 读SqlMapConfig.xml. 当调用与数据库有关的操作的时候,会读SqlMapConfig.xml文件中的信息,里面配置了Mybatis的运行环境信息,加载mapper.xm ...
- Bootstrap抽样(自展法)
Bootstrap又称自展法,是用小样本估计总体值的一种非参数方法,在进化和生态学研究中应用十分广泛.例如进化树分化节点的自展支持率等. Bootstrap的思想,是生成一系列bootstrap伪样本 ...
- (ZT)谷歌大脑科学家 Caffe缔造者 贾扬清 微信讲座完整版
一.讲座正文:大家好!我是贾扬清,目前在Google Brain,今天有幸受雷鸣师兄邀请来和大家聊聊Caffe.没有太多准备,所以讲的不好的地方还请大家谅解.我用的ppt基本上和我们在CVPR上要做的 ...
- Linux笔记 #06# 在VPS上自建Git服务
参考: GitHub Help: Connecting to GitHub with SSH 廖雪峰的官方网站: 搭建Git服务器 菜鸟教程: Git 服务器搭建 1. 安装记录(可能有错...) 本 ...
- 在pom.xml中使用distributionManagement将项目打包上传到nexus私服
本文介绍 如何在pom.xml中使用distributionManagement将项目打包上传到nexus私服 1.pom.xml文件添加distributionManagement节点 <!- ...
- git clone时提示(gnome-ssh-askpass:29288): Gtk-WARNING **: cannot open display:
一.背景 在服务器上克隆源码 二.解决 unset SSH_ASKPSS
- 【热更新IK词典】ElasticSearch IK 自动热更新原理与实现
一.热更新原理 elasticsearch开启加载外部词典功功能后,会每60s间隔进行刷新字典.具体原理代码如下所示: public void loadDic(HttpServletRequest r ...
- js模拟类的创建以及继承的四部曲
<script> 1)创建父类 function Person(){ } Person.prototype.age = 18;//给父类添加属性 var p1 = new Person() ...
- Gym 101147A The game of Osho(SG找规律)
https://vjudge.net/problem/Gym-101147A 题意:给出G组数,每组数包括两个数B,N,两玩家轮流取数,使得N-num,num<=N并且num是N的整次幂.判断谁 ...
- install ros-indigo-tf
sudo apt-get install ros-indigo-tf