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}\ ...
随机推荐
- FTRL与Online Optimization
1. 背景介绍 最优化求解问题可能是我们在工作中遇到的最多的一类问题了:从已有的数据中提炼出最适合的模型参数,从而对未知的数据进行预测.当我们面对高维高数据量的场景时,常见的批量处理的方式已经显得力不 ...
- Vue学习笔记之Webpack介绍
在这里我仅仅的是对webpack做个讲解,webpack这个工具非常强大,解决了我们前端很繁琐的一些工具流程繁琐的事情.如果感兴趣的同学,简易还是看官网吧. 中文链接地址:https://www.we ...
- InstallShield的工程类型的选择
转载:http://blog.csdn.net/wuxiaoqrs/article/details/45717695 InstallScript vs. Basic MSI InstallScript ...
- 简单使用grunt、bower工具合并压缩js和css
前段时间因为项目中的报表写了一个Jquery插件,开源到github上,参考以往大神们写的插件的姿势,决定搞了像模像样一点.言归正传.前端工程师对这些工具:Node,bower,grunt,npm这些 ...
- pycharm同时使用python2.7版本和python3.6版本
最近在看爬虫的专题,很多爬虫的教程是python2的,电脑上装的是3.6版本,而且python不向下兼容,这就很麻烦,最简单的print要加括号啊,等等.于是分享一个在windows环境下pychar ...
- python 判断字典是否为空
my_dict = {} if not bool(my_dict): print("Dictionary is empty")
- 过了所有技术面,却倒在 HR 一个问题上
面试问离职原因,这是我们广大程序员朋友面试时逃不开的问题,如果答得不好,可能就影响了你整个的面试结果. 最近在群里,我也看到大家在讨论这个问题,其中有个朋友的回复很有感触,我分享给大家看一下. 如图, ...
- thinkphp3.2笔记(2)调试模式,配置项C,创建模块, 四种URL模式,URL生成,跳转
一.调试模式 TP的调试模式其实就控制了TP关于配置信息以及函数的缓存功能 如果开启了调试模式,每次访问项目,Tp都会去加载最新的配置以及函数信息. 如果关闭了调试模式,当tp第一次访问时会降配置以及 ...
- windows java 环境变量配置
第一步 找到系统设置环境变量的位置(windows 10): 控制面板\系统和安全\系统 点击 ‘高级系统设置’ 就可以看到 “环境变量” 了 第二步 设置3个路径 1.path (配置JD ...
- thinkphp5的auth权限认证(转自thinkphp官方文档+自己总结)
thinkphp5的auth权限认证(转自thinkphp官方文档+自己总结) 一.总结 一句话总结:相当于就是用其它thinkphp的扩展一样,都是引入扩展,配置扩展,然后使用 引入 配置 使用 基 ...