博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
快速幂+gcd变形定理
阅读量:3953 次
发布时间:2019-05-24

本文共 978 字,大约阅读时间需要 3 分钟。

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

11 1 1 1

Sample Output

0

公式:

**gac(a^m−b^m,a^n−b^n)=a^gcd(m,n)−b^gcd(m,n)**

 gcd(a^m−1,a^n−1)=a^gcd(m,n)−1

#include 
#include
#include
using namespace std;typedef long long LL;//const int N=10005;LL gcd(LL m,LL n){ if(n==0) return m; else return gcd(n,m%n);}LL qmi(LL m,LL k,LL p){ LL res=1%p,t=m; while(k) { if(k&1) res=res*t%p; t=t*t%p; k>>=1; } return res;}int main(){ ios::sync_with_stdio(false); int t; LL a,m,n,k; cin>>t; while(t--) { cin>>a>>m>>n>>k; LL sum=qmi(a,gcd(m,n),k)+k-1;//防止出现负数 cout<
<

 

转载地址:http://xpyzi.baihongyu.com/

你可能感兴趣的文章
【Redis】Centos7下安装Redis
查看>>
【Redis】Centos7下搭建Redis集群
查看>>
【Redis】Centos7下搭建Redis集群——哨兵模式
查看>>
【Linux】本地ping不同VM虚拟机
查看>>
【SpringCloud】Hystrix
查看>>
快速阅读——《认知篇》
查看>>
【Asp.net】基本概念
查看>>
【Asp.net】Web服务器控件
查看>>
【Asp.net】内置对象
查看>>
C语言数据类型笔记 by STP
查看>>
C语言指针笔记 by STP
查看>>
CoreLocation笔记 by STP
查看>>
Application Transport Security has blocked a cleartext HTTP (http://) 解决方案
查看>>
The identity used to sign the executable is no longer valid.解决方案
查看>>
Xcode增加pch文件
查看>>
CocoaPods安装和使用笔记 by STP
查看>>
Could not find developer disk image-解决方案
查看>>
升级Xcode之后VVDocumenter-Xcode不能用的解决办法
查看>>
iOS开发常见报错及解决方案 by STP
查看>>
SVN(Cornerstone)屏蔽/忽略不需要版本控制的UserInterfaceState.xcuserstate
查看>>