数学实在是差到不行了……

#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的更多相关文章

  1. hdu 3307 Description has only two Sentences (欧拉函数+快速幂)

    Description has only two SentencesTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

  2. Description has only two Sentences(欧拉定理 +快速幂+分解质因数)

    Description has only two Sentences Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...

  3. hdu 3307(欧拉函数+好题)

    Description has only two Sentences Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/327 ...

  4. Description has only two Sentences(hdu3307)

    Description has only two Sentences Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/327 ...

  5. hdu 3307 简单的指数循环节

    #include<stdio.h>#include<string.h>#include<algorithm>#define LL __int64using name ...

  6. HDU题解索引

    HDU 1000 A + B Problem  I/O HDU 1001 Sum Problem  数学 HDU 1002 A + B Problem II  高精度加法 HDU 1003 Maxsu ...

  7. [SinGuLaRiTy] 数论题目复习

    [SinGuLaRiTy-1020] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. [CQBZOJ 1464] Hankson 题目描述 H ...

  8. hdu3307 欧拉函数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3307 Description has only two Sentences Time Limit: 3 ...

  9. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

随机推荐

  1. GCD的使用和面试题集锦

    GCD 分为异步和同步 异步: dispatch_async ( 参数1 , { } 同步: dispatch_sync( 参数1 , { } 参数1 :队列 队列分为两种: dispatch_get ...

  2. isalpha函数,判断字符是否是字母

    头文件:<iostream> or  <cctype>  在c语言中<ctype.h> 功能:判断一个字符是否是英文字符,是大写返回1,是小写返回2,不是英文字符返 ...

  3. 一道面试题细说C++类型转换

    开篇先说这道面试题: class ClassA { public: virtual ~ ClassA() { } virtual void FunctionA() { } }; class Class ...

  4. R与数据分析旧笔记(二)随机抽样的一个综合例子

    题目:模拟产生统计专业同学的名单(学号区分),记录数学分析.线性代数.概率统计三科成绩,然后进行一些统计分析 > num=seq(10378001,10378100) > num [1] ...

  5. 从零开始PHP学习 - 第三天

    写这个系列文章主要是为了督促自己  每天定时 定量消化一些知识! 同时也为了让需要的人 学到点啥~! 本人技术实在不高!本文中可能会有错误!希望大家发现后能提醒一下我和大家! 偷偷说下 本教程最后的目 ...

  6. [C++]Infinite House of Pancakes——Google Code Jam 2015 Qualification Round

    Problem It’s opening night at the opera, and your friend is the prima donna (the lead female singer) ...

  7. IOS 特定于设备的开发:处理基本方向

    UIDevice类使用内置的orientation属性获取设备的物理方向.IOS设备支持这个属性的7个可能的值. >UIDeviceOrientationUnknown:方向目前未知. > ...

  8. 面试题之 query转为obj

    要注意处理编码后的字串  对于a=123要得到number形的值 function parseQueryString(url) { var obj = {}; var query = url.sear ...

  9. Umbraco隐藏属性

    Umbraco默认的Url地址,是根据Node路径来默认显示的,因此使用中文的话比较尴尬. 网上有传的方法,是修改源码,来实现Url的重写. 但实际大可不必如此麻烦,只需要增加两个类型为Textstr ...

  10. 淘宝数据库连接池 druid 性能评测

    想必大家知道淘宝在双十一惊人表现,搜索群里有哥们对淘宝的链接池进行了测试,废话不多说,直接上测试结果,结果如下:在Spring中使用方法<bean id="dataSource&quo ...