题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2685

题意:求gcd(a^m - 1, a^n - 1) mod k

思路:gcd(a^m - 1, a^n - 1) = a^gcd(m, n) - 1

code:

 #include <stdio.h>

 int gcd(int a, int b)
{
return !b ? a : gcd(b, a%b);
} int mod_pow(int a, int x, int mod)
{
int tmp = a;
int ret = ;
while(x) {
if (x & ) {
ret = ret * tmp % mod;
}
tmp = tmp * tmp % mod;
x >>= ;
} return ret;
} int main()
{
int t, a, m, n, k;
scanf("%d", &t);
while (t--) {
scanf("%d %d %d %d", &a, &m, &n, &k);
int d = gcd(m, n);
int ans = mod_pow(a, d, k);
printf("%d\n", (ans - + k) % k);
} return ;
}

HDU 2685 I won't tell you this is about number theory的更多相关文章

  1. hdu 2685 I won't tell you this is about number theory 数论

    题目链接 根据公式 \[ gcd(a^m-1, a^n-1) = a^{gcd(m, n)}-1 \] 就可以很容易的做出来了. #include <iostream> #include ...

  2. HDU 2685 GCD推导

    求$(a^n-1,a^m-1) \mod k$,自己手推,或者直接引用结论$(a^n-1,a^m-1) \equiv a^{(n,m)}-1 \mod k$ /** @Date : 2017-09-2 ...

  3. hdu 2685(数论相关定理+欧几里德定理+快速取模)

    I won't tell you this is about number theory Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: ...

  4. HDU 1210 Eddy's 洗牌问题(foj1062) || FOJ1050 Number lengths水

    麻痹,感冒了. ------------------------------------------------感冒了的分割线------------------------------------- ...

  5. 2018HDU多校训练-3-Problem D. Euler Function

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=6322 Problem Description In number theory, Euler's toti ...

  6. HDU 3341 Lost's revenge(AC自动机+DP)

    Lost's revenge Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)T ...

  7. HDU 3336 Count the string(KMP的Next数组应用+DP)

    Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. HDU 5584 LCM Walk 数学

    LCM Walk Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5584 ...

  9. HDU 5778 abs (枚举)

    abs 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5778 Description Given a number x, ask positive ...

随机推荐

  1. [AS3]as3与JS的交互(AS3调用JS)实例说明

    一,AS3 vs JavaScript (1)AS3调用JS 函数: ExternalInterface.(functionName:, arguments): //AS3 Code 属性: 同上,通 ...

  2. Memcached安装卸载

    Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提供动态.数据库驱动网站的速度.Memcached ...

  3. UVA 12545 Bits Equalizer

    题意: 两个等长的字符串p和q,p有‘0’,‘1’,‘?’组成,q由‘0’,‘1’组成.有三种操作:1.将‘?’变成0:2.将‘?’变成‘1’:3.交换同一字符串任意两个位置上的字符.问有p变到q最少 ...

  4. [Linked List]Linked List Cycle,Linked List Cycle II

    一.Linked List Cycle Total Accepted: 85115 Total Submissions: 232388 Difficulty: Medium Given a linke ...

  5. apt-get命令学习

    在windows下安装软件,我们只需要有EXE文件,然后双击,下一步直接OK就可以了.但在LINUX下,不是这样的.每个LINUX的发行版,比如UBUNTU,都会维护一个自己的软件仓库,我们常用的几乎 ...

  6. jQuery 随滚动条滚动效果 (固定版)

    //侧栏随动 var rollStart = $('.feed-mail'), //滚动到此区块的时候开始随动 rollSet = $('.search,.weibo,.group,.feed-mai ...

  7. c语常用算法库(1)

    1,冒泡排序 #include <iostream> using namespace std; int main(){ ]; // 一共n个数, n不超过1000. a用来保存这些数. , ...

  8. Smarty3配置

    下载Smarty压缩包并解压,复制其中的libs文件夹到我们的PHP工程目录下(可将其改名为smarty).同时,在工程目录下新建三个文件夹,分别取名为templates.templates_c和sm ...

  9. 解决Delphi自带UTF8解码缺陷(使用API)

    因为Delphi自带的转换函数遇到其无法识别的字符串就返回空,下面函数可解决该问题. function DecodeUtf8Str(const S: UTF8String): WideString;v ...

  10. linux ssh 不用密码自动登录的几种方法

    1. 自动ssh/scp方法== A为本地主机(即用于控制其他主机的机器) ;B为远程主机(即被控制的机器Server), 假如ip为192.168.60.110;A和B的系统都是Linux 在A上运 ...