[题解](同余)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^ ...
随机推荐
- sqlite:多线程操作数据库“database is locked”解决方法
1. 使sqlite支持多线程(不确定是否非加不可,暂且加上,以备后患) 可以在编译时/启动时/运行时选择线程模式,参考:http://www.cnblogs.com/liaj/p/4015219.h ...
- TEE&TrustZone
一.TEE(Trusted Execution Environment) 1 A look back 1)2009 OMTP(Open Mobile Terminal Platform),首次定义了T ...
- unity3d mvvm c#
using UnityEngine; using System.Collections; public interface IState { void BeforEnter(); void Befor ...
- fscanf和fgets用法
首先要对fscanf和fgets这两个文件函数的概念有深入的了解,对于字符串输入而言这两个函数有一个典型的区别是: fscanf读到空格或者回车时会把空格或回车转化为/(字符串结束符)而fgets函数 ...
- SpringBoot 版本升级后报错 Cannot instantiate interface org.springframework.context.ApplicationContextInitializer
本篇博客纯粹讲我遇到这个问题的解决以及思考,如果你想知道解决方法,可以直接看正确解决方案部分.因为是前端写的,所以可能有些明显的内容很容易就看出来了. 首先:升级后更新其他依赖,以及Applicati ...
- poj2492A Bug's Life——带权并查集
题目:http://poj.org/problem?id=2492 所有元素加入同一个并查集中,通过其偏移量%2将其分类为同性与异性,据此判断事件. 代码如下: #include<iostrea ...
- makefile 使用【转载】
该篇文章为转载,是对原作者系列文章的总汇加上标注. 支持原创,请移步陈浩大神博客: http://blog.csdn.net/haoel/article/details/2886 makefile很重 ...
- Windows平台Python编程必会模块之pywin32
在Windows平台上,从原来使用C/C++编写原生EXE程序,到使用Python编写一些常用脚本程序,成熟的模块的使用使得编程效率大大提高了. 不过,python模块虽多,也不可能满足开发者的所有需 ...
- 【244】◀▶IEW-Unit09
Unit 9 Food 1)Model1题目及范文讲解 In the world today, there is a problem with food production. As a result ...
- CF-811A
A. Vladik and Courtesy time limit per test 2 seconds memory limit per test 256 megabytes input stand ...