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 ...
随机推荐
- sql server 2000 和 sql server 2005 数据库连接字符串区别
//sql server 2000 <add name="Connection" connectionString="Data Source=.;Initial C ...
- UpdatePanel + 弹出框
ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", "alert('操作成功!') ...
- ORACLE恢复误删除的对象(表、存储过程等)
1.恢复存储过程 原理就是利用了oracle里所有的存储过程的源代码都是存在dba_source里,而drop某个存储过程的时候,oracle这里肯定要去dba_source里把相关的源代码给dele ...
- jsp建立错误页自动跳转
在各个常用的web站点中,经常会发现这样一个功能:当一个页面出错后,会自动跳转到一个页面上进行错误信息的提示. 想要完成错误页的操作,则一定要满足两个条件: 1.指定错误出现时的跳转页,通过error ...
- JAVA之GUI编程ACTION事件
package GUI; import java.awt.Button; import java.awt.FlowLayout; import java.awt.Frame; import java. ...
- 《转》使用JAVA如何对图片进行格式检查以及安全检查处理
本文出自冯立彬的博客,原地址:http://www.fenglibin.com/use_java_to_check_images_type_and_security.html 一.通常情况下,验证一个 ...
- Evernote Clearly :: Firefox 附加组件
Evernote Clearly :: Firefox 附加组件 Evernote Clearly 10.1.1.2 作者: Evernote Evernote Clearly 可使博客贴文.文章和网 ...
- nyist 202 红黑树(二叉树中序遍历)
旋转对中序遍历没有影响,直接中序输出即可. #include <iostream> #include <cstdio> using namespace std; int n; ...
- Bzoj2034 2009国家集训队试题 最大收益 贪心+各种优化+二分图
这个题真的是太神了... 从一開始枚举到最后n方的转化,各种优化基本都用到了极致.... FQW的题解写了好多,个人感觉我全然没有在这里废话的必要了 直接看这里 各种方法真的是应有尽有 大概说下 首先 ...
- Node.js笔记3
###Node.js核心模块 1. 全局对象 Node.js中能够访问到的都是global的属性 **process它用于描述当前 Node.js 进程状态的对象,提供了一个与操作系统的简单接口.** ...