[题解](同余)POJ_3696_The Luckiest Number
还是挺难的吧......勉强看懂调了半天
首先表达式可以写成 8(10^x -1)/9,题意为求一个最小的x使L | 8(10^x -1)/9
设d=gcd(L,8)
L | 8(10^x -1)/9
<=>9L | 8(10^x -1)
<=>9L/d | 10^x -1 (因为 9L/d 和 8/d 互质了 所以 9L/d 能整除(8/d)*(10^x-1)和 8/d 无关,所以可以去掉)
<=>10^x 同余 1(mod 9L/d)
引理:
若a,n互质,则满足10^x同余1(mod n)的最小正整数x0是phi(n)的约数
反证法:
假设满足a^x 同余 1(mod n)的最小正整数x0不能整除phi(n)
设phi(n)=q*x0+r(0<r<x0),因为a^x0 同余1(mod n),所以a^(q*x0)同余1(mod n)
根据欧拉定理a^phi(n)同余1(mod n),所以a^r同余1(mod n),与x0最小矛盾
无解的时候就是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的倍数,显然矛盾。
注意:乘的时候会爆longlong,手写乘法,要用根号的试除法求约数,不然会T
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define ll long long
using namespace std;
ll n,cnt;
ll x[];
ll gcd(ll a,ll b){
return b?gcd(b,a%b):a;
}
ll eular(ll n){
ll ans=n;
for(ll i=;i*i<=n;i++){
if(n%i==){
ans=ans/i*(i-);
while(n%i==)n/=i;
}
}
if(n>)ans=ans/n*(n-);
return ans;
}
ll mul(ll a,ll b,ll mod){
ll ans=;
while(b){
if(b&)ans=(ans+a)%mod;
a=(a<<)%mod;
b>>=;
}
return ans;
}
ll qpow(ll a,ll b,ll mod){
ll base=a,ans=;
while(b){
if(b&)ans=mul(ans,base,mod);
base=mul(base,base,mod);
b>>=;
}
return ans%mod;
} int main(){
int t=;
while(){
int fl=;cnt=;
scanf("%lld",&n);
if(n==)break;
ll d=*n/gcd(n,);
if(gcd(,d)!=){
printf("Case %d: 0\n",++t);
}
else{
ll phi=eular(d);
for(ll i=;i*i<=phi;i++){
if(phi%i==){
x[++cnt]=i;
if(i*i!=phi)x[++cnt]=phi/i;
}
} sort(x+,x+cnt+);
for(int i=;i<=cnt;i++)
if(qpow(,x[i],d)==){
printf("Case %d: %lld\n",++t,x[i]);
break;
}
}
}
}
[题解](同余)POJ_3696_The Luckiest Number的更多相关文章
- poj_3696_The Luckiest number
Chinese people think of '8' as the lucky digit. Bob also likes digit '8'. Moreover, Bob has his own ...
- poj 3696 The Luckiest Number
The Luckiest Number 题目大意:给你一个int范围内的正整数n,求这样的最小的x,使得:连续的x个8可以被n整除. 注释:如果无解输出0.poj多组数据,第i组数据前面加上Case ...
- POJ3696 The Luckiest number
题意 Language:Default The Luckiest number Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7 ...
- POJ_3696 The Luckiest number 【欧拉定理+同余式+对取模的理解】
一.题目 Chinese people think of '8' as the lucky digit. Bob also likes digit '8'. Moreover, Bob has his ...
- POJ3696: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 ...
- LeetCode Continuous Subarray Sum 题解 同余前缀和 Hash表
文章目录 题意 思路 特殊情况k=0 Source Code 1 Source Code 2 题意 给定一个数组和一个整数k,返回是否存在一个长度至少为2的连续子数组的和为k的倍数. 思路 和上一篇博 ...
- POJ 3696 The Luckiest number (欧拉函数,好题)
该题没思路,参考了网上各种题解.... 注意到凡是那种11111..... 22222..... 33333.....之类的序列都可用这个式子来表示:k*(10^x-1)/9进而简化:8 * (10^ ...
随机推荐
- 51Nod XOR key —— 区间最大异或值 可持久化字典树
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1295 1295 XOR key 题目来源: HackerRa ...
- python to 可执行文件
cx_Freeze for Windows, Linux, and Mac OS X (Python 2.7, 3.x) pyinstaller for Windows, Linux, and Mac ...
- rand()与srand()
1.不用srand()的话 两次运行程序产生的随机数序列相同 2.用srand() 两次运行程序产生的随机数则不同 示例程序: #include<iostream> #include< ...
- malloc和new的区别是什么?
http://zhidao.baidu.com/link?url=iUDUZeJtj1o12PvUETLlJgvAMqzky5HxGCJRGnULpsO8HdWAdjKkQqGCJ9-o-aTu8NP ...
- Linux中vsftpd安装和配置
目录 Redhat/CentOS安装vsftp软件 Ubuntu/Debian安装vsftp软件 Redhat/CentOS安装vsftp软件 1. 安装vsftp $ yum install vsf ...
- RMAN兼容性、控制文件自动备份、保存时间、备份策略、备份脚本(二)
RMAN 程序的兼容性 RMAN 环境由以下5部分组成:(1) RMAN executable(2) Recovery catalog database(3) Recovery catalog sch ...
- BZOJ3127:[USACO2013OPEN]Yin and Yang
浅谈树分治:https://www.cnblogs.com/AKMer/p/10014803.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem. ...
- Oracle字段增删改方法总结
一.修改字段的语法:alter table tablename modify (字段名 类型 [default value][null/not null],….);有一个表名为tb,字段段名为name ...
- request.getSession()方法的应用
request.getSession(true):若存在会话则返回该会话,否则新建一个会话. request.getSession(false):若存在会话则返回该会话,否则返回NULL
- Session与Cookie(1)
Session session是一种位于服务端,用于存储一个会话(指打开浏览器访问某个域名,及其下面的资源,然后关掉浏览器的过程)中所需的配置信息.也就是在一个会话中,只存在一个session.对于J ...