HDU 3307 Description has only two Sentences
数学实在是差到不行了……
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
#define LL __int64
const LL maxn=1001;
LL e[maxn],t;
LL gcd(LL a,LL b){return b==0?a:gcd(b,a%b);}
LL euler_phi(LL n)//求单个欧拉函数
{
LL m=(LL)sqrt(n+0.5);
LL i,ans=n;
for(i=2;i<=m;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;
}
void find(LL n)//找出所有因子
{
LL m=(LL)sqrt(n+0.5);
for(LL i=1;i<m;i++)
if(n%i==0){e[t++]=i;e[t++]=n/i;}
if(m*m==n)e[t++]=m;
}
LL pow_mod(LL a,LL b,LL mod)//快速幂
{
LL s=1;
while(b)
{
if(b&1)
s=(s*a)%mod;
a=(a*a)%mod;
b=b>>1;
}
return s;
}
int main()
{
LL a,x,y;
while(cin>>x>>y>>a)
{
LL m,phi,i;
if(y==0){cout<<"1"<<endl;continue;}
m=a/gcd(y/(x-1),a);
if(gcd(m,x)!=1){cout<<"Impossible!"<<endl;continue;}//不互质,则x^k%m必定是gcd(m,x)的倍数
phi=euler_phi(m);
t=0;
find(phi);
sort(e,e+t);
for(i=0;i<t;i++)
{
if(pow_mod(x,e[i],m)==1)
{
cout<<e[i]<<endl;
break;
}
}
}
return 0;
}
/*
euler_phi(i),欧拉函数,表示求不大于i且与i互质的正整数个数。 本题递推公式化简下可得到通项公式:ak=a0+Y/(X-1)*(X^k-1);后半部分是等比数列的和。
现在求ak%a0=0,即Y/(X-1)*(X^k-1)%a0==0,令m=a0/gcd(Y/(X-1),a0),则可推到求最小的k使得
(X^k-1)%m==0,即X^k==1(mod m).
根据欧拉定理得X^euler_phi(m)==1(mod m).(X与m互质)
又由抽屉原理可知,X^k的余数必定是根据euler_phi(m)的某个因子为循环节循环的。
所以求出最小的因子k使得X^k%m==1,即为答案
*/
HDU 3307 Description has only two Sentences的更多相关文章
- hdu 3307 Description has only two Sentences (欧拉函数+快速幂)
Description has only two SentencesTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- Description has only two Sentences(欧拉定理 +快速幂+分解质因数)
Description has only two Sentences Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- hdu 3307(欧拉函数+好题)
Description has only two Sentences Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/327 ...
- Description has only two Sentences(hdu3307)
Description has only two Sentences Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/327 ...
- hdu 3307 简单的指数循环节
#include<stdio.h>#include<string.h>#include<algorithm>#define LL __int64using name ...
- HDU题解索引
HDU 1000 A + B Problem I/O HDU 1001 Sum Problem 数学 HDU 1002 A + B Problem II 高精度加法 HDU 1003 Maxsu ...
- [SinGuLaRiTy] 数论题目复习
[SinGuLaRiTy-1020] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. [CQBZOJ 1464] Hankson 题目描述 H ...
- hdu3307 欧拉函数
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3307 Description has only two Sentences Time Limit: 3 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
随机推荐
- English - because of,due to ,thanks to ,owing to ,as a result of ,on account of解析
because of,due to ,thanks to ,owing to ,as a result of ,on account of 等都可以用来表示原因,但其用法却各有不同.下面就其用法分述如 ...
- 【转】在Spring中基于JDBC进行数据访问时怎么控制超时
http://www.myexception.cn/database/1651797.html 在Spring中基于JDBC进行数据访问时如何控制超时 超时分类 超时根据作用域可做如下层级划分: Tr ...
- @AutoWired使用
Spring MVC @autowired的使用: spring MVC使用注解配置方式相对于xml配置方式具有很多优势: 充分利用java的反射机制获取类的结构信息,这些信息可以减少配置的工作!Sp ...
- JQuery初识
一.什么是JQuery JQuery官方网站上是这样解释的:JQuery是一个快速简洁的JavaScript库,它可以简化HTML文档的元素遍历.事件处理.动画及Ajax交互,快速开发We ...
- 模块化定义JS,让统一文件夹内相同的变量不冲突
两种方法: 1.(function(){……编写代码……})() //先声明一个函数,声明完后直接调用 2.!function(){……编写代码……}()
- 一周学会Mootools 1.4中文教程:(1)Dom选择器
利器: 君欲善其事须先利其器,好吧因为我们的时间比较紧迫,只有六天而已,那么六天的时间用死记硬背的方式学会Mt犹如天方夜谭,因此我们需要借鉴一下Editplus的素材栏帮我们记忆就好了,当我们需要用到 ...
- robot framework环境搭建和简单示例
环境搭建 因为我的本机已经安装了python.selenium.pip等,所以还需安装以下程序 1.安装wxPythonhttp://downloads.sourceforge.net/wxpytho ...
- (Problem 39)Integer right triangles
If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exact ...
- HTML编码的用户输入
public string Browse(string genre) { returen HttpUtility.HtmlEncode(genre); } HttpUtility.HtmlEncode ...
- 类比的方法学习Performance_schema
引用自:http://www.javacoder.cn/?p=332 MySQL在5.6版本中包含了一个强大的特性——performance-schema,合理的使用这个数据库中的表,能为我们解决一些 ...