POJ.1995 Raising Modulo Numbers (快速幂)
2017-04-30 22:40
344 查看
POJ.1995 Raising Modulo Numbers (快速幂)
提议分析
快速幂裸题分别给出递归写法和位运算写法。
感觉位运算应该会更快一点,实际上这两个跑的一样快。不知道为什么(摊手
代码总览
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <sstream> #include <set> #include <map> #include <queue> #include <stack> #include <cmath> #define INF 0x3f3f3f3f #define nmax 200 #define MEM(x) memset(x,0,sizeof(x)) using namespace std; //long long pow_mod(int a, int n, int m) //{ // if(n == 0) return 1; // long long x = pow_mod(a,n/2,m);// 每次砍一半 // long long ans = x * x % m; // if(n%2 == 1) ans *= a % m; // return ans; //} int qpow_mod(int a,int n, int m) { int ans = 1; while(n){ if(n&1) ans = ans * a % m; n >>= 1; a = ((a%m)*(a%m))%m; } return ans; } int main() { //freopen("in.txt","r",stdin); int t; scanf("%d",&t); while(t--){ int m,n; scanf("%d %d",&m,&n); int ans = 0; for(int i = 0; i<n;++i){ int a,b; scanf("%d%d",&a,&b); ans+= qpow_mod(a,b,m); ans = ans % m; } printf("%d\n",ans); } return 0; }
相关文章推荐
- poj 1995 Raising Modulo Numbers 【快速幂 裸题】
- poj 1995 Raising Modulo Numbers【快速幂】
- 【POJ】-1995-Raising Modulo Numbers(快速幂,同余定理)
- POJ 题目1995 Raising Modulo Numbers(快速幂)
- Raising Modulo Numbers (poj 1995 快速幂水题)
- POJ 1995 Raising Modulo Numbers 【快速幂取模】
- poj 1995 Raising Modulo Numbers ——快速幂
- POJ 1995 Raising Modulo Numbers 【快速幂+同余定理】
- POJ - 1995 Raising Modulo Numbers 【快速幂】
- POJ-1995 Raising Modulo Numbers(快速幂简单应用)
- |poj 1995|快速幂|Raising Modulo Numbers
- POJ 1995:Raising Modulo Numbers 快速幂
- POJ 1995 Raising Modulo Numbers (快速幂 +同余定理)
- POJ 1995 Raising Modulo Numbers【快速幂】
- poj 1995 Raising Modulo Numbers 【(64位)快速幂】
- poj1995——Raising Modulo Numbers(快速幂取余)
- POJ 1995 Raising Modulo Numbers (数论-整数快速幂)
- poj1995 Raising Modulo Numbers 整数型快速幂 算法的应用
- POJ 1995:Raising Modulo Numbers 快速幂
- POJ1995 Raising Modulo Numbers(快速幂取模,模板题)