POJ 3696 The Luckiest number (欧拉函数,好题)
该题没思路,参考了网上各种题解。。。。
注意到凡是那种11111..... 22222..... 33333.....之类的序列都可用这个式子来表示:k*(10^x-1)/9
进而简化:8 * (10^x-1)/9=L * k (k是一个整数)
8*(10^x-1)=9L*k
d=gcd(9L,8)=gcd(8,L)
8*(10^x-1)/d=9L/d*k
令p=8/d q=9L/d p*(10^x-1)=q*k
因为p,q互质,所以q|(10^x-1),即10^x-1=0(mod q),也就是10^x=1(mod 9*L/d)
由欧拉定理可知,当q与10互质的时候,10^(φ(q))=1 (mod q),即必定存在一个解x。
而题目中要求的是最小的解,设为min,那么有a^min=1%q,因为要满足a^φ(q)=1%q,那么a^φ(q)肯定能变换成(a^min)^i。
所以接下来只要枚举φ(q)的因子,找出符合条件的最小者即可。
无解的时候就是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的倍数,显然矛盾。
#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
#include <math.h> using namespace std;
long long L; long long gcd(long long a,long long b) {
return b==?a:gcd(b,a%b);
}
long long multi(long long a,long long b,long long mod) {
long long ret=;
while(b) {
if(b&)
ret=(ret+a)%mod;
a=(a<<)%mod;
b=b>>;
}
return ret;
}
long long quickPow(long long a,long long b,long long mod) {
long long ret=;
while(b) {
if(b&)
ret=multi(ret,a,mod); //直接相乘的话可能会溢出
a=multi(a,a,mod);
b=b>>;
}
return ret;
}
//求欧拉函数
long long eular(long long n) {
long long ret=,i;
for(i=; i*i<=n; i++) {
if(n%i==) {
n=n/i;
ret*=i-;
while(n%i==) {
n=n/i;
ret*=i;
}
}
}
if(n>)
ret*=n-;
return ret;
} int main() {
int t=;
while(scanf("%I64d",&L)!=EOF) {
if(L==)
break;
long long p=*L/gcd(L,);
long long d=gcd(,p);
if(d==) {
long long phi=eular(p);
long long ans=phi;
long long m=sqrt((double)phi);
bool flag=false;
//先枚举大小在1~sqrt(phi)之间的因子
for(int i=; i<=m; i++) {
if(phi%i== && quickPow(,i,p)==) {
ans=i;
flag=true;
break;
}
}
//若1~sqrt(phi)没找到符合的因子,那么枚举sqrt(phi)~phi之间的因子
if(!flag) {
for(int i=m; i>=; i--) {
if(phi%i== && quickPow(,phi/i,p)==) {
ans=phi/i;
break;
}
}
}
printf("Case %d: %I64d\n",++t,ans);
} else {
printf("Case %d: 0\n",++t);
}
}
return ;
}
POJ 3696 The Luckiest number (欧拉函数,好题)的更多相关文章
- poj 3696 The Luckiest number 欧拉函数在解a^x=1modm的应用
题意: 给一个L,求长度最小的全8数满足该数是L的倍数. 分析: 转化为求方程a^x==1modm. 之后就是各种数学论证了. 代码: //poj 3696 //sep9 #include <i ...
- POJ3696:The Luckiest number(欧拉函数||求某数最小的满足题意的因子)
Chinese people think of '8' as the lucky digit. Bob also likes digit '8'. Moreover, Bob has his own ...
- POJ 2407 Relatives(欧拉函数入门题)
Relatives Given n, a positive integer, how many positive integers less than n are relatively prime t ...
- POJ 2407:Relatives(欧拉函数模板)
Relatives AC代码 Relatives Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16186 Accept ...
- hdu 1286 找新朋友 欧拉函数模版题
找新朋友 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem Des ...
- (hdu step 7.2.1)The Euler function(欧拉函数模板题——求phi[a]到phi[b]的和)
题目: The Euler function Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- UVA 10820 欧拉函数模板题
这道题就是一道简单的欧拉函数模板题,需要注意的是,当(1,1)时只有一个,其他的都有一对.应该对欧拉函数做预处理,显然不会超时. #include<iostream> #include&l ...
- poj2407(欧拉函数模板题)
题目链接:https://vjudge.net/problem/POJ-2407 题意:给出n,求0..n-1中与n互质的数的个数. 思路:欧拉函数板子题,先根据唯一分解定理求出n的所有质因数p1,p ...
- POJ 2478 Farey Sequence(欧拉函数前n项和)
A - Farey Sequence Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
随机推荐
- [整理归档]30 common tasks you perform using the GUI that you can do faster in Windows PowerShell
主要内容来自于 http://channel9.msdn.com/Events/TechEd/Australia/2014/DCI316 可以下载PPT以及视频,个人只是整理一下平时常用的 NetWo ...
- Oracle结果集 (MSSQL存储过程写报表)
接触SQL Server比较多,写报表是用存储过程实现. 对Oracle实现像MSSQL那样,还是有很多疑问
- flask-cors 实现跨域请求
安装:pip install -U flask-cors from flask import Flask from flask.ext.cors import CORS app = Flask(__n ...
- JavaScript高级程序设计之EventUtil
简单的通用事件方法 var EventUtil = { getEvent: function (e) { return e || window.event; }, getTarget: functio ...
- simplexml_load_string获取xml节点里的属性值
http://stackoverflow.com/questions/14359658/get-xml-attribute-using-simplexml-load-string 问: I am us ...
- 63.Assignment to name ignored, since the identifier is never used
在文件中单独定义一些信号变量,如start_flag,即使进行(*KEEP=“TRUE”*)的声明,但在cdc文件上依然找不到start_flag变量,这是为什么呢?因为ISE综合器非常聪明,对一些没 ...
- JAVA类与对象(七)------继承
理解:继承可以理解为一个对象获取属性的过程.如果类A是类B的父类,而类B是类C的父类,我们也称C是A的子类,类C是从类A继承而来. 在java中,类的继承是单一继承,也就是说,一个子类只能拥有一 ...
- opencv颜色识别代码分享
android 平台 opencv 实现颜色识别代码:http://www.eyesourcecode.com/thread-40682-1-1.htmlopencv的颜色识别简单实现的代码:http ...
- 微软职位内部推荐-SW Engineer II for Cloud Servi
微软近期Open的职位: Do you have a passion for embedded devices and services?   Does the following m ...
- snmp安装配置
只说一下在ubuntu下如何进行基本的配置. ubuntu Server 12.04 apt-get install snmp snmpd 安装完成之后还要安装snmp-mibs-downloader ...