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)

也就是这个式子:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 int pow(int a, int b, int m)
 5 {
 6     int ans = 1;
 7     a %= m;
 8     while(b)
 9     {
10         if(b & 1)ans = ans * a % m;
11         a *= a;
12         a %= m;
13         b /= 2;
14     }
15     return ans;
16 }
17 int main()
18 {
19     int T, a, m, n, k, g;
20     cin >> T;
21     while(T--)
22     {
23         cin >> a >> m >> n >> k;
24         g = __gcd(m, n);
25         int ans = (pow(a, g, k) - 1) % k;
26         ans = (ans + k) % k;
27         cout<<ans<<endl;
28     }
29     return 0;
30 }

hdu-2685 I won't tell you this is about number theory---gcd和快速幂的性质

原文地址:https://www.cnblogs.com/fzl194/p/9058366.html

时间: 2024-08-05 17:04:33

hdu-2685 I won't tell you this is about number theory---gcd和快速幂的性质的相关文章

hdu 2685 I won&#39;t tell you this is about number theory

#include<stdio.h>#include<string.h>#define LL __int64 LL mult_mod(LL a,LL b,LL c){    a%=c;    b%=c;    LL ret=0;    while(b)    {        if(b&1){ret+=a;ret%=c;}        a<<=1;        if(a>=c)a%=c;        b>>=1;    }    retur

HDU 5895 Mathematician QSC(矩阵乘法+循环节降幂+除法取模小技巧+快速幂)

传送门:HDU 5895 Mathematician QSC 这是一篇很好的题解,我想讲的他基本都讲了http://blog.csdn.net/queuelovestack/article/details/52577212 [分析]一开始想简单了,对于a^x mod p这种形式的直接用欧拉定理的数论定理降幂了 结果可想而知,肯定错,因为题目并没有保证gcd(x,s+1)=1,而欧拉定理的数论定理是明确规定的 所以得另谋出路 那么网上提供了一种指数循环节降幂的方法 具体证明可以自行从网上找一找 有

HDU - 1005 Number Sequence(简单矩阵快速幂)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 题意:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. 就是这道题目,然而找了一晚上的错误 \("▔□▔)/\("▔□▔)/\("▔□▔)/. 1 #include <iostream> 2 #include <cstring> 3 using namespace std;

HDU 5950 Recursive sequence 矩阵快速幂

http://acm.hdu.edu.cn/showproblem.php?pid=5950 一开始以为i^4不能矩阵快速幂,但是结论是可以得,那么要怎么递推呢? 矩阵快速幂的思路都是一样的,matrix_a * matrix_b ^ n 其中,想要维护什么,就在matrix_a写,比如现在是F[n - 1], F[n - 2],我想要递推到下一项,那么就 会变成F[n], F[n - 1],这个时候,你就要寻找一下F[n]和F[n - 1]有什么关系. i^4也一样,想要从i^4 递推到 (i

HDU 4990 Reading comprehension(找规律+矩阵快速幂)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4990 Problem Description Read the program below carefully then answer the question. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include<iostream> #include

HDU 5895 矩阵快速幂+高次幂取模

HDU 5895 Mathematician QSC 题意:已知f(n)=2*f(n-1)+f(n-2), g(n)=∑f(i)²(0<=i<=n), 给出n,x,y,s, 求x^(g(n*y))%(s+1); 思路:OEIS查到了g(n)=f(n)*f(n+1)/2, f(n)可以用矩阵快速幂求得, 有一个定理可以用于高次幂取模 x^n %k=x^(n%phi(k)+phi(k)) %k, 此处phi(x)为欧拉函数,但是在对幂次取模时存在一个除2, 又因为(a/b)%k=(a%bk)/b,

hdu 5667 Sequence 矩阵快速幂

题目链接:hdu 5667 Sequence 思路:因为fn均为a的幂,所以: 这样我们就可以利用快速幂来计算了 注意: 矩阵要定义为long long,不仅仅因为会爆,还会无限超时 要对a%p==0特判,以为可能出现指数%(p-1)==0的情况,那么在快速幂的时候返回的结果就是1而不是0了 /************************************************************** Problem:hdu 5667 User: youmi Language:

hdu 1757 A Simple Math Problem 矩阵快速幂

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 Lele now is thinking about a simple function f(x).If x < 10 f(x) = x.If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10);And ai(0<=i<=9) can only be 0 or 1 .Now, I w

HDU 4990 Reading comprehension (找规律+矩阵快速幂)

题目链接:HDU 4990 Reading comprehension 题目给的一个程序其实就是一个公式:当N=1时 f[n]=1,当n>1时,n为奇数f[n]=2*f[n-1]+1,n为偶数f[n]=2*f[n-1]. 先不取模,计算前十个找规律.得到一个递推公式:f[n]=2*f[n-2]+f[n-1]+1 然后快速幂解决之. 给出一个神奇的网站(找数列通项):http://oeis.org/ AC代码: #include<stdio.h> #include<string.h&

HDU 4896 Minimal Spanning Tree(矩阵快速幂)

题意: 给你一幅这样子生成的图,求最小生成树的边权和. 思路:对于i >= 6的点连回去的5条边,打表知907^53 mod 2333333 = 1,所以x的循环节长度为54,所以9个点为一个循环,接下来的9个点连回去的边都是一样的.预处理出5个点的所有连通状态,总共只有52种,然后对于新增加一个点和前面点的连边状态可以处理出所有状态的转移.然后转移矩阵可以处理出来了,快速幂一下就可以了,对于普通的矩阵乘法是sigma( a(i, k) * b(k, j) ) (1<=k<=N), 现在