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}\ ...
随机推荐
- Centos 7 无法上网的解决办法
首先,鼠标右击桌面,点击“在终端中打开”. 然后如下图所示,输入:su,按回车后输入自己的root密码:注意,输密码的时候密码区域并不显示任何东西哦,自己输错了就多按几次backspace就行 ...
- PHP文件锁 解决并发问题
使用多线程或是多进程时. 难免会遇到并发问题. 处理简单的并发可以使用这个办法来解决 flock($fp = fopen($lock, 'w+'), LOCK_EX | LOCK_NB) or e ...
- js 变量、函数提升 与js的预编译有关
参考网址:http://www.codesec.net/view/178491.html 先简单理解下作用域的概念,方便对变量与函数提升的概念的理解 function foo() { var x = ...
- OpenCV/OpenCL/OpenGL区别
OpenCV/OpenCL/OpenGL区别: OpenGL(全写Open Graphics Library)是个定义了一个跨编程语言.跨平台的应用程序接口(API)的规格,它用于生成二维.三维图像. ...
- 关于spring框架工作原理的初解
一:spring基本概念 1)struts2是web框架,hibernate是orm框架 2)spring是容器框架,创建bean,维护bean之间的关系 3)spring可以管理web层,持久层,业 ...
- 在Github上搭建博客
貌似还是这个链接最靠谱呀 http://my.oschina.net/nark/blog/116299 如何利用github建立个人博客:之一 在线编辑器http://markable.in/ed ...
- python 浮点数转分数
from fractions import Fraction value = 4.2 print(Fraction(value).limit_denominator())
- QtTcp_资料
1.百度搜索关键字“Qt TCP” 2. 2.1.Qt学习之路_5(Qt TCP的初步使用) http://www.cnblogs.com/tornadomeet/archive/2012/06/30 ...
- 一次由webview报错引起的追根溯源
最近客户端那边需要搞了个h5嵌入app,想想是移动端的webview,前端这边也比较忙.就没想太多,直接用了async/await处理api数据,于是就不怪测试就来搞事情了... 一.error: 1 ...
- bzoj1143: [CTSC2008]祭祀river 最长反链
题意:在遥远的东方,有一个神秘的民族,自称Y族.他们世代居住在水面上,奉龙王为神.每逢重大庆典, Y族都会在水面上举办盛大的祭祀活动.我们可以把Y族居住地水系看成一个由岔口和河道组成的网络.每条河道连 ...