快速幂竞赛模板

int ans=1;
while(n)
{
  if(n&1)
    ans=ans*x;
  x*=x;
  n>>=1;
}

求x^n

原文地址:https://www.cnblogs.com/Tidoblogs/p/11219885.html

时间: 2024-08-30 18:05:01

快速幂竞赛模板的相关文章

poj 3070 Fibonacci (矩阵快速幂乘/模板)

题意:给你一个n,输出Fibonacci (n)%10000的结果 思路:裸矩阵快速幂乘,直接套模板 代码: #include <cstdio> #include <cstring> #include <iostream> using namespace std; typedef long long ll; const int N=2,M=2,P=2; const int MOD=10000; struct Matrix { ll m[N][N]; }; Matrix

快速幂(模板)

在这里看到了快速幂算法的有关推导(在此感谢~) 理解了这个算法本身之后,发现你忘了快速幂怎么打,对于noip2013 T1你也可以拿到80 所以看懂推导很重要(如果忘了,请认真看) 这里就mark一下模板好了(链接写的很详细,所以自己的推导就过了) #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; long lo

Fibonacci----poj3070(矩阵快速幂, 模板)

题目链接:http://poj.org/problem?id=3070 . 就是斐波那契的另一种表示方法是矩阵的幂: 所以是矩阵快速幂:矩阵快速幂学习 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include<math.h> using namespace std; #define N 10 struct node { int a[

快速幂小模板

long long quickpow(long long m,long long n) { long long ans=1; while(n) { if(n&1) ans=(ans%mod)*(m%mod)%mod; n=n>>1; m=(m*m)%mod; } return ans; } mod是要求的取mod值.至于矩阵的快速幂就把乘法和计算的对象换为矩阵的就行了.

HDU1575-Tr 【矩阵快速幂】(模板题)

<题目链接> A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input 数据的第一行是一个T,表示有T组数据. 每组数据的第一行有n(2 <= n <= 10)和k(2 <= k < 10^9)两个数据.接下来有n行,每行有n个数据,每个数据的范围是[0,9],表示方阵A的内容. Output 对应每组数据,输出Tr(A^k)%9973. Sample Input 2 2 2 1 0 0 1 3 99999999 1 2

快速幂运算模板

ll pow(ll a,ll b) //long long型 { ll ans=1; while(b!=0) { if(b%2==1) //if(b&1) ans=ans*a%mod;//如果是奇数次幂,因为b下面是除以2操作,会少一次乘,这里要提前乘上去. a=a*a%mod;//快速幂,每一次是上一次的平方倍 b=b/2; } return ans; } 分析: 将指数b看成二进制,b%2==1即判断当前b二进制最低位是否为1,是则将当前底数a与累积ans相乘,否则跳过.在每次循环中都将底数

Poj 3233 矩阵快速幂,暑假训练专题中的某一道题目,矩阵快速幂的模板

题目链接  请猛戳~ Description Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + - + Ak. Input The input contains exactly one test case. The first line of input contains three positive integers n (n ≤ 30), k (k ≤ 109) and m (m <

【快速幂】 模板

int quick_pow(int a, int n) { int ans = 1; while (n) { if (n & 1) { ans = (long long )ans * a % inf; } n >>= 1; a = (long long ) a * a % inf; } return ans; }

矩阵快速幂【模板】

const int MAXN = 110; struct Matrax { int m[MAXN][MAXN]; }a,per; int N,M; void Init() { for(int i = 0; i < N; ++i) for(int j = 0; j < N; ++j) { scanf("%d",&a.m[i][j]); a.m[i][j] %= M; per.m[i][j] = (i == j); } } Matrax Multi(Matrax a,M