I won't tell you this is about number theory

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 458    Accepted Submission(s): 142

Problem Description
To think of a beautiful problem description is so hard for me that let's just drop them off. :)
Given four integers a,m,n,k,and S = gcd(a^m-1,a^n-1)%k,calculate the S.

 
Input
The first line contain a t,then t cases followed.
Each case contain four integers a,m,n,k(1<=a,m,n,k<=10000).
 
Output
One line with a integer S.
 
Sample Input
1
1 1 1 1
 
Sample Output
0
 
Author
Teddy
 
Source
 
这道题要知道这个公式:
gcd(am-1,an-1) = agcd(m,n)-1
推广:
若 gcd(a,b)=1
gcd(am-bm,an-bn) = agcd(m,n)-bgcd(m,n)
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
LL pow_mod(LL a,LL n,LL mod){
LL ans = ;
while(n){
if(n&) ans = ans*a%mod;
a=a*a%mod;
n=n>>;
}
return ans;
}
LL gcd(LL a,LL b){
return b==?a:gcd(b,a%b);
}
int main()
{
int tcase;
scanf("%d",&tcase);
while(tcase--){
LL a,m,n,k;
scanf("%lld%lld%lld%lld",&a,&m,&n,&k);
LL t = gcd(m,n);
LL ans = (pow_mod(a,t,k)-+k)%k;
printf("%lld\n",ans);
}
return ;
}

hdu 2685(数论相关定理+欧几里德定理+快速取模)的更多相关文章

  1. Educational Codeforces Round 80 C. Two Arrays(组合数快速取模)

    You are given two integers nn and mm . Calculate the number of pairs of arrays (a,b)(a,b) such that: ...

  2. 组合数学中的常见定理&组合数的计算&取模

    组合数的性质: C(n,m)=C(n,n-m); C(n,m)=n!/(m!(n-m)!); 组合数的递推公式: C(n,m)=  C(n-1,m-1)+C(n-1,m); 组合数一般数值较大,题目会 ...

  3. lucas定理解决大组合数取模

    LL MyPow(LL a, LL b) { LL ret = ; while (b) { ) ret = ret * a % MOD; a = a * a % MOD; b >>= ; ...

  4. 位运算之——按位与(&)操作——(快速取模算法)

    学习redis 字典结构,hash找槽位 求槽位的索引值时,用到了 hash值 & sizemask操作, 其后的scan操作涉及扫描顺序逻辑,对同模的槽位 按一定规则扫描! 其中涉及位运算 ...

  5. 【Java基础】14、位运算之——按位与(&)操作——(快速取模算法)

    学习redis 字典结构,hash找槽位 求槽位的索引值时,用到了 hash值 & sizemask操作, 其后的scan操作涉及扫描顺序逻辑,对同模的槽位 按一定规则扫描! 其中涉及位运算 ...

  6. hdu 2582(数论相关定理+素数筛选+整数分解)

    f(n) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  7. ACM-ICPC 2018 焦作赛区网络预赛G Give Candies(隔板定理 + 小费马定理 + 大数取模,组合数求和)题解

    题意:给你n个东西,叫你把n分成任意段,这样的分法有几种(例如3:1 1 1,1 2,2 1,3 :所以3共有4种),n最多有1e5位,答案取模p = 1e9+7 思路:就是往n个东西中间插任意个板子 ...

  8. 【HDU 5832】A water problem(大数取模)

    1千万长度的数对73和137取模.(两个数有点像,不要写错了) 效率要高的话,每15位取一次模,因为取模后可能有3位,因此用ll就最多15位取一次. 一位一位取模也可以,但是比较慢,取模运算是个耗时的 ...

  9. HDU——1395 2^x mod n = 1(取模运算法则)

    2^x mod n = 1 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

随机推荐

  1. 科学计算库Numpy——运算

    np.multiply(array1,array2) 该函数用于数组中对应位置上的数相乘. 一维向量 二维数组 np.dot(array1,array2) 两个数组都是一维向量 数组中对应位置上的数相 ...

  2. g++编译器的使用(转载)

    关于g++ g++  是GNU组织开发出的编译器软件集合(GCC)下的一个C++编译器.它是Unix 和 Linux  系统下标配的 基于命令行的 C++编译器.如果你的系统是Windows,可以按照 ...

  3. Flow Problem HDU - 3549

    Flow Problem HDU - 3549 Network flow is a well-known difficult problem for ACMers. Given a graph, yo ...

  4. poj 2533Longest Ordered Subsequence

    Longest Ordered Subsequence Description A numeric sequence of ai is ordered if a1 < a2 < - < ...

  5. Session只读的影响

    .net中使用Session必须实现IRequiresSessionState接口,不过还有个只读的接口IReadOnlySessionState, 若是实现只读的接口,那么在该页面(如一般处理程序) ...

  6. 13 Java内存模型

    数据竞争 int a=0, b=0; public void method1() { int r2 = a; b = 1; } public void method2() { int r1 = b; ...

  7. s debug

    value stack contents   ognl 值栈 stack context           action上下文 action上下文是一个map对象,通过#key获得对象内容,在#re ...

  8. 最简单的动态代理实例(spring基于接口代理的AOP原理)

    JDK的动态代理是基于接口的 package com.open.aop; public interface BusinessInterface {     public void processBus ...

  9. 【转】iTween for Unity

    http://www.cnblogs.com/zhaoqingqing/p/3833321.html?utm_source=tuicool&utm_medium=referral 你曾经在你的 ...

  10. tcp协议 tcpip协议 http协议,IP,DNS,端口号

    每当看到HTTP协议,tcp/ip协议,TCP协议总是蒙圈:在这里先记录一下,方面自己查看 TCP协议:TCP(Transmission Control Protocol 传输控制协议)是一种面向连接 ...