hdu-2685 I won't tell you this is about number theory---gcd和快速幂的性质
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=2685
题目大意:
求gcd(am-1,an-1)%k
解题思路:
对于am-1 = (a - 1) * (1 + a + a2 + ... + am-1)
所以最开始的gcd就为a-1
对于两个1 + a + a2 + ... + am-1和1 + a + a2 + ... + an-1来说,可以找出gcd(m, n)那么久就可以提出gcd
比如:
1 + a + a2 + a3
1 + a + a2 + ... + a5
这两个可以写成(1+a)*(1 + a2) 和(1+a)*(1 + a2+ a4)
就提出公因式(1 + a)
这里公因式如何确定呢?
就是从0一直加到m和n的gcd-1次方,这样的话m和n才可以分解成多个从0---gcd-1的幂之和
所以,gcd(am-1,an-1) = (a-1)*(1 + a + a2 + a3 + ... + ag-1) = ag - 1
上式中g等于gcd(m, n)
也就是这个式子:


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int pow(int a, int b, int m)
{
int ans = ;
a %= m;
while(b)
{
if(b & )ans = ans * a % m;
a *= a;
a %= m;
b /= ;
}
return ans;
}
int main()
{
int T, a, m, n, k, g;
cin >> T;
while(T--)
{
cin >> a >> m >> n >> k;
g = __gcd(m, n);
int ans = (pow(a, g, k) - ) % k;
ans = (ans + k) % k;
cout<<ans<<endl;
}
return ;
}
hdu-2685 I won't tell you this is about number theory---gcd和快速幂的性质的更多相关文章
- HDU 2685 I won't tell you this is about number theory
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2685 题意:求gcd(a^m - 1, a^n - 1) mod k 思路:gcd(a^m - 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 ...
- HDU 5895 Mathematician QSC(矩阵乘法+循环节降幂+除法取模小技巧+快速幂)
传送门:HDU 5895 Mathematician QSC 这是一篇很好的题解,我想讲的他基本都讲了http://blog.csdn.net/queuelovestack/article/detai ...
- HDU 1005 Number Sequence:矩阵快速幂
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 题意: 数列{f(n)}: f(1) = 1, f(2) = 1, f(n) = ( A*f(n ...
- hdu 1005 Number Sequence(矩阵快速幂,找规律,模版更通用)
题目 第一次做是看了大牛的找规律结果,如下: //显然我看了答案,循环节点是48,但是为什么是48,据说是高手打表出来的 #include<stdio.h> int main() { ], ...
- HDU - 1005 Number Sequence (矩阵快速幂)
A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mo ...
- HDU - 6198 number number number(规律+矩阵快速幂)
题意:已知F0 = 0,F1 = 1,Fn = Fn - 1 + Fn - 2(n >= 2), 且若n=Fa1+Fa2+...+Fak where 0≤a1≤a2≤⋯≤a,n为正数,则n为mj ...
- 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 ...
- HDU 2855 斐波那契+矩阵快速幂
http://acm.hdu.edu.cn/showproblem.php?pid=2855 化简这个公式,多写出几组就会发现规律 d[n]=F[2*n] 后面的任务就是矩阵快速幂拍一个斐波那契模板出 ...
- HDU 2855 (矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2855 题目大意:求$S(n)=\sum_{k=0}^{n}C_{n}^{k}Fibonacci(k)$ ...
随机推荐
- 在windows服务器上设置301、伪静态(wordpress)
新建一个httpd.ini文件,插入代码: [ISAPI_Rewrite] RewriteCond Host: ^wuchao\.cc$ RewriteRule (.*) http\://www\.w ...
- JavaScript中类型检测
文章首发: http://www.cnblogs.com/sprying/p/4349426.html 本文罗列了一般Js类型检测的方法,是构建Js知识体系的一小块,这篇文章是我很早之前总结的. 一. ...
- RabbitMQ如何解决各种情况下丢数据的问题
1.生产者丢数据 生产者的消息没有投递到MQ中怎么办?从生产者弄丢数据这个角度来看,RabbitMQ提供transaction和confirm模式来确保生产者不丢消息. transaction机制就是 ...
- 前端小结(5)---- iframe
iframe对应的div: <div id="iframezone"> <iframe id="iframe" frameborder='0' ...
- C Primer Plus note7
这个程序是<C Primer Plus 中文版 第六版>书上198页的代码,是一个值的琢磨的程式. 有时间可以看一看: 尤其是下面这几句代码,很精妙: 用了很短的程式,得出了最大值和最小值 ...
- maven父子项目
maven搭建父子项目 1.先建立一个父项目,建立项目的时候,选择 Create a simple project 点击 next,填写以下信息 点击finish就可以了. 2.接下来要建立一个子项 ...
- mybatis向数据库插入数据 (传入的是一个实体类)
/** * 插入用户信息 user为实体类 * @param user */ public int insert( User user); //实体类不用@param标注 //mybatis的xml文 ...
- npm安装指定版本
今天犯了一个低级错误,在npm安装依赖时,命令写成下了格式 npm i --save iview 2.0.0 要安装指定版本应该使用 npm i --save iview@2.0.0 谨记
- css 元素居中各种办法
一:通过弹性布局<style> #container .box{ width: 80px; height: 80px; position: absolute; background:red ...
- Java 文件上传与下载、email
1. 文件上传与下载 1.1 文件上传 文件上传,要点: 前台: 1. 提交方式:post 2. 表单中有文件上传的表单项: <input type="file" /> ...