POJ1995 Raising Modulo Numbers(快速幂取模)
2018-02-26 17:05
351 查看
题目链接:http://poj.org/problem?id=1995
题目大意:给定ZZ组数据,每组数据给定一个MM(作为取模值),给定一个HH表示多项式有多少项,每一项给定两个数字A、BA、B表示该项为ABAB,输出结果 : ∑i=1H(Ai∗Bi)modM∑i=1H(Ai∗Bi)modM
数据范围: A、BA、B为int范围整数, 1≤1≤H,MH,M≤45000≤45000
题目解答:快速幂取模(复杂度:O(∑i=1Hlog2Ai)O(∑i=1Hlog2Ai))
题目代码:
题目大意:给定ZZ组数据,每组数据给定一个MM(作为取模值),给定一个HH表示多项式有多少项,每一项给定两个数字A、BA、B表示该项为ABAB,输出结果 : ∑i=1H(Ai∗Bi)modM∑i=1H(Ai∗Bi)modM
数据范围: A、BA、B为int范围整数, 1≤1≤H,MH,M≤45000≤45000
题目解答:快速幂取模(复杂度:O(∑i=1Hlog2Ai)O(∑i=1Hlog2Ai))
题目代码:
#include <cstdio> #include <cstring> #include <iostream> #include <bitset> using namespace std; //快速幂 int power(int a, int b, int p) { int ans = 1 % p; for( ; b; b >>= 1) { if(b & 1) ans = (long long) ans * a % p; a = (long long)a * a % p; } return ans; } int main () { int z; cin >> z; while(z--) { int m, h, a, b; cin >> m >> h; int ans = 0; for(int i = 0; i < h; ++i) { cin >> a >> b; ans = ((long long)ans + power(a, b, m)) % m; } cout << ans << endl; } }
相关文章推荐
- 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 快速幂水题)
- POJ1995 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【快速幂】
- POJ1995: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 (快速幂模板)