[题解](同余)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^ ...
随机推荐
- HDU3811 Permutation —— 状压DP
题目链接:https://vjudge.net/problem/HDU-3811 Permutation Time Limit: 6000/3000 MS (Java/Others) Memor ...
- laravel基础课程---12、lavarel的ajax操作2(lavarel的ajax使用总结)
laravel基础课程---12.lavarel的ajax操作2(lavarel的ajax使用总结) 一.总结 一句话总结: 比较简单:就是js请求ajax,然后控制器获取请求参数,返回数据即可 1. ...
- Java 的序列化Serializable接口介绍及应用
常看到类中有一串很长的 如 private static final long serialVersionUID = -4667619549931154146L;的数字声明.这些其实是对此类进行序列化 ...
- Day06:迭代器,生成器,生成表达式,面向过程编程,包及常用模块
今日内容:1.迭代器(****)2.生成器(***)3.生成器表达式(*****)4.面向过程编程(*****)5.包的使用(***)6.常用模块 logging (*****) re ( ...
- Iptables防火墙面试题
Iptables防火墙面试题 第1章 (一)基础口试题 1.1 详述 iptales 工作流程以及规则过滤顺序? 1.防火墙是一层层过滤的.实际是按照配置规则的顺序从上到下,从前到后进行过滤的. 2. ...
- std::ostringstream 转std::string
http://www.cplusplus.com/reference/sstream/ostringstream/ https://en.cppreference.com/w/cpp/io/basic ...
- wamp + wordpress 安装
WAMP是一个windows上的php开发集成环境,一键安装php,apache和mysql,非常方便. 双击wampserver2.2exxxxxxxxxx.exe文件进行安装,安装过程中直接下一步 ...
- 如何将ajax请求同步化
(function ($) { var a = ['test1', 'test2', 'test3', 'test4']; recursive(3, 'test').done(function (re ...
- Linux命令总结_touch创建文件
1.touch命令,用来创建文件或者修改文件时间戳 格式:touch [选项]... 文件... 选项 : -a 或--time=atime或--time=access或--time=use 只 ...
- [PE182]RSA encryption
https://projecteuler.net/problem=182 题意: 找出满足下列条件的所有$e$ 的和, - $1 < e < \varphi \left( {1009,36 ...