快速幂:quickpow】的更多相关文章

矩阵快速幂引入: 1.整数快速幂: 为了引出矩阵的快速幂,以及说明快速幂算法的好处,我们可以先求整数的幂.如果现在要算X^8:则 XXXXXXXX 按照寻常思路,一个一个往上面乘,则乘法运算进行7次.(XX)(XX)(XX)(XX)这种求法,先进行乘法得X^2,然后对X^2再执行三次乘法,这样去计算,则乘法运算执行4次.已经比七次要少.所以为了快速算的整数幂,就会考虑这种结合的思想.现在要考虑应该怎么分让计算比较快.接下来计算整数快速幂.例如:X^19次方.19的二进制为:1 0 0 1 1 .…
众所周知,快速幂是优化对数的次方运算的最普遍手段.在学习快速幂的思想时,其分治思想容易让大家用简单的递归实现. 但其实,除了递归之外,更好的方法会是简单的 WHILE循环.下面贴代码: #include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <vector> using namespace std; int quickpow(int…
洛谷例题 推荐自行脑补:百度百科 如果  ,那么 : 前言:快速幂就是快速算底数的n次幂.其时间复杂度为 O(log₂N), 与朴素的O(N)相比效率有了极大的提高. 拿题目样例 Input :2 10 9 Output:7 210 % 9 = 7 没毛病 问题不大 那么真正的问题是怎么算这个 普通幂:废物过程 可你有没有发现这个很烦? 可是 算到264就炸了qwq (__int128啥的给我走开) b=2,p=10,k=9 2^1=2 2%9=2 2^2=4 4%9=4 2^3=8 8%9=8…
所谓的快速幂: // 计算 m^n % k 的快速幂算法 int quickpow(int m,int n,int k) { ; ) { ) b = (b*m)%k; n = n >> ; m = (m*m)%k; } return b; } 借助于上面的函数,本题目很容易就可以解决: int main() { int n,m,x,k,data; scanf("%d%d%d%d",&n,&m,&k,&x); data=quickpow(,k,…
分析:求Map^k,刚开始没有用快速幂,TLE了   代码如下: ==================================================================================================== #include<stdio.h> #include<string.h> #include<algorithm> #include<queue> using namespace std; ; ;…
The Goddess Of The Moon Sample Input 2 10 50 12 1213 1212 1313231 12312413 12312 4123 1231 3 131 5 50 121 123 213 132 321   Sample Output 86814837 797922656 题意:给你n个字符串,若是一个的后缀与一个的前缀相同的大于1,则表示这两个可以连接到一起,问M个字符串相连的方案数 若a  b可以合并,可以让他们相连,然后求在一个图中走m-1步的方案数…
emmmmm..就是矩阵快速幂,直接附代码: #include <cstdio> using namespace std; ; ; struct Matrix { int m[maxn][maxn]; }ans,res; int n; Matrix mul(Matrix a,Matrix b) { Matrix tmp; ; i <= n; i++) ; j <= n; j++) tmp.m[i][j] = ; ; i <= n; i++) ; j <= n; j++)…
题意就是求第 n 个斐波那契数. 由于时间和内存限制,显然不能直接暴力解或者打表,想到用矩阵快速幂的做法. 代码如下: #include <cstdio> using namespace std; ; ; int a; struct Matrix { int m[maxn][maxn]; }ans,res,w,head; Matrix mul(Matrix a,Matrix b,int n) { Matrix tmp; ; i <= n; i++) ; j <= n; j++) t…
题目链接:http://47.93.249.116/problem.php?id=2182 题目描述 河神喜欢吃零食,有三种最喜欢的零食,鱼干,猪肉脯,巧克力.他每小时会选择一种吃一包. 不幸的是,医生告诉他,他吃这些零食的时候,如果在连续的三小时内他三种都吃了,并且在中间一小时 吃的是巧克力,他就会食物中毒.并且,如果河神在连续三小时内吃到相同种类的食物,他就会不开心. 假设每种类零食的数量都是无限的,那么如果经过n小时,让河神满意的零食吃法有多少种呢?(开心又不 会食物中毒的吃法)答案可能过…
题意:给你N个数,1~N分别为num[i],  以及T个 (i,j,P) 对于每组(i,j,P),让你将  num[i] 减去 P*num[i]  再把 P*num[i] 加到 num[j] 上.T个操作同时完成. 这T个操作执行M次(M<1e5).求最后一个点的值. 题解:将N个值排成一排放到矩阵 1*n B里,我们可以构造一个 n*n 矩阵A,使得 B*A (一个 1*n 的矩阵)的第i列为操作一次后的 第i个数的值.然后就能矩阵快速幂了! 如何构造? 一列一列构造. A的第i列会从上到下依…
a^b Description 求 aa 的 bb 次方对 pp 取模的值. 输入格式 三个整数 a,b,pa,b,p ,在同一行用空格隔开. 输出格式 输出一个整数,表示a^b mod p的值. 数据范围 1≤a,b,p≤109 输入样例:  3 2 7 输出样例: 2 题解 快速幂模板,交了结果WA了 需要注意的是 123456789 0 1 这个数据,1%1 = 0 代码 #include<bits/stdc++.h> using namespace std; typedef long…
我把自己演哭了... 心酸.jpg 写了很多个版本的,包括数学公式暴力,快速幂TLE等等,最后想到了优化快速幂里的乘法,因为会爆longlong,但是和别人优化的效率简直是千差万别...? 本题大意: 给定三个longlongint范围内的正整数a, b, c,求出a^b mod c 的结果并输出. 本题思路: 见代码吧. 下面贴出我的各种版本的代码... 参考代码: //这道题数据有点水,不明白为啥数据里1^0 mod 1 == 1 ?魔鬼... /* 数学公式 :: 超时版 #include…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4767 题意:求集合{1, 2, 3, ..., n}有多少种划分情况bell[n],最后结果bell[n] mod 95041567. 分析:首先了解三个概念:贝尔数   第二类斯特灵数   中国剩余定理 贝尔数是指基数为n的集合的划分方法的数目. 贝尔数适合递推公式: 每个贝尔数都是"第二类Stirling数"的和 贝尔数满足两个公式:(p为质数)             1) B[n+…
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4686 题意: 其中a0 = A0ai = ai-1*AX+AYb0 = B0bi = bi-1*BX+BY 最后的结果mod 1,000,000,007 n<=10^18. 分析:ai*bi=(ai-1 *ax+ay)*(bi-1 *bx+by) =(ai-1 * bi-1 *ax*bx)+(ai-1 *ax*by)+(bi-1 *bx*ay)+(ay*by) 设p=ax*bx,  q=ax*by, …
All X  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536/65536 K (Java/Others) Problem Description F(x, m)F(x,m) 代表一个全是由数字xx组成的mm位数字.请计算,以下式子是否成立: F(x,m)\ mod\ k\ \equiv \ cF(x,m) mod k ≡ c Input 第一行一个整数TT,表示TT组数据. 每组测试数据占一行,包含四个数字x,m,k,cx,…
Happy Necklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1146    Accepted Submission(s): 491 Problem Description Little Q wants to buy a necklace for his girlfriend. Necklaces are single…
Mathematician QSC Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description QSC dream of becoming a mathematician, he believes that everything in this world has a mathematical law. Through unremitting e…
http://acm.hdu.edu.cn/showproblem.php?pid=5667 这题的关键是处理指数,因为最后结果是a^t这种的,主要是如何计算t. 发现t是一个递推式,t(n) = c*t(n-1)+t(n-2)+b.这样的话就可以使用矩阵快速幂进行计算了. 设列矩阵[t(n), t(n-1), 1],它可以由[t(n-1), t(n-2), 1]乘上一个3*3的矩阵得到这个矩阵为:{[c, 1, b], [1, 0, 0], [0, 0, 1]},这样指数部分就可以矩阵快速幂了…
Problem Description Queues and Priority Queues are data structures which are known to most computer scientists. The Queue occurs often in our daily life. There are many people lined up at the lunch time.   Now we define that ‘f’ is short for female a…
Problem Description As we know, sequence in the form of an=a1+(n−1)d is called arithmetic progression and sequence in the form of bn=b1qn−1(q>1,b1≠0) is called geometric progression. Huazheng wants to use these two simple sequences to generate a simp…
Problem Description The so-called best problem solver can easily solve this problem, with his/her childhood sweetheart. It is known that y=(5+2√6)^(1+2^x).For a given integer x (0≤x<2^32) and a given prime number M (M≤46337) , print [y]%M . ([y] mean…
题目链接:http://poj.org/problem?id=3233 Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 28105   Accepted: 11461 Description Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + … + Ak. Input Th…
Description Assuming a finite – radius “ball” which is on an N dimension is cut with a “knife” of N-1 dimension. How many pieces will the “ball” be cut into most?However, it’s impossible to understand the following statement without any explanation.L…
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 will give a0 ~ a9 and two positive integers k…
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5667 题意: Lcomyn 是个很厉害的选手,除了喜欢写17kb+的代码题,偶尔还会写数学题.他找到了一个数列: fn= 1,ab,abfcn−1fn−2,n=1n=2otherwise 给定各个数,求fn. 分析: 可以发现最后都是a的倍数,这样我们让fn对a取对数,令tn=logafn方程就转化为b+ctn−1+tn−2,这样利用矩阵快速幂直接算幂数,最后快速幂一下就可以了. 注意: 由费马小…
传送门 zhx's contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 575    Accepted Submission(s): 181 Problem Description As one of the most powerful brushes, zhx is required to give his juniors…
地址 http://poj.org/problem?id=3233 大意是n维数组 最多k次方  结果模m的相加和是多少 Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + … + Ak. Sample Input 2 2 4 0 1 1 1 Sample Output 1 2 2 3 题解 矩阵逐步的相乘然后相加是不可以 但是矩阵也有类似快速幂的做法 /*A + A^2 =A(I+A)…
题目链接 https://atcoder.jp/contests/agc019/tasks/agc019_e 题解 tourist的神仙E题啊做不来做不来--这题我好像想歪了啊= =-- 首先我们可以考虑,什么样的操作序列才是合法的? 有用的位置只有两种,一种是两个序列在这个位置上都是1, 称作11型,另一种是一个0一个1, 称作01型.设两种位置分别有\(A\)个和\(2B\)个. 考虑一个操作序列,交换两个11型相当于没交换,每个11型只会被交换两次,每个01型只会被交换一次.这也就是说,如…
定义 快速求a^b%c的算法 原理 指数可以被二进制分解 那么a^b可以分解为a^2^k1*a^2^k2*…… 又显然a^2^(k+1)=a^(2^k*2)=(a^2^k)^2 所以可以将指数在二进制下从低位向高位递推,每次将底数平方,若该位是1就将答案乘上底数,直到指数为0. 实现时可以每次将指数/2方便处理 位运算优化 x&1:取x二进制下最后一位 x>>1:x/2 代码 int quickpow(int a,int b,const int c) { ; while(b) { )…
下面我们来看一个容易让人蒙圈的问题:N的阶乘 mod P. 51Nod 1008 N的阶乘 mod P 看到这个可能有的人会想起快速幂,快速幂是N的M次方 mod P,这里可能你就要说你不会做了,其实你会,为什么呢,只要你明白快速幂的原理,你就会发现他们两个其实差不多是同一个问题. 重要原理:积的取模=取模的积再取模. 快速幂不过是一直乘的相同的的数,这里仅仅是改成乘以不同的数而已. 题目: 输入N和P(P为质数),求N! Mod P = ? (Mod 就是求模 %) 例如:n = 10, P…