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

#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. BHO启动IE调试

    如下图选择Web Browser Debugger, 输入启动网址

  2. Linux学习之挂载操作

    一.挂载U盘 1.使用命令:fdisk -l,查看系统硬盘和分区情况 2.插入优盘,再次用fdisk -l命令查看     大家可以发现多了一个硬盘/dev/sdb和它的一个分区/dev/sdb1 3 ...

  3. PHPEXCEL导入小技巧

    在导入excel的时候,单元格格式和公式经常让导入不顺畅.注意phpexcel文档说明,基本上就可以很顺利的导入. 1.忽略单元格格格式,并导入xls.xlsx两种格式 $objReader = PH ...

  4. SQL Server 查看数据表占用空间大小的SQL语句

    ) ) if object_id('tempdb..#space') is not null drop table #space ),rows ),data ),index_size ),unused ...

  5. C/C++中使用的正则表达式库

    正则表达式 正则表达式是对字符串操作的一种逻辑公式,就是用事先定义好的一些特定字符.及这些特定字符的组合,组成一个“规则字符串”,这个“规则字符串”用来表达对字符串的一种过滤逻辑. 正则引擎主要可以分 ...

  6. Tic-Tac-Toe游戏

    #Tic-Tac-Toe #机器人和人类下井字棋 #全局变量 import random X = "X" O = "O" EMPTY = " &quo ...

  7. js传参java接收乱码解决方案

    js传参处理 encodeURI(encodeURI(name)); java接收处理 URLDecoder.decode(request.getParameter("name") ...

  8. TMS X-Cloud Todolist with FNC

    Wednesday, June 22, 2016 It's almost three months since we released the first version of the TMS FNC ...

  9. javascript简单对象创建

    由于javascript中定义了一个函数就相当于定义了一个类,我们当然可以创建一个这个类的对象. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 ...

  10. Genymotion配置及使用教程(最新最完整版附各部分下载地址)

    Genymotion配置及使用教程(最新最完整版附各部分下载地址) FROM:http://blog.csdn.net/beiminglei/article/details/13776013 早都听说 ...