POJ 1995/ ZOJ 2150 : Raising Modulo Numbers - 快速幂取模
来源:互联网 发布:java 自定义属性 编辑:程序博客网 时间:2024/06/08 12:29
题意:
给定表达式中各变量的值,计算表达式(a1^b1+a2^b2+...+an^bn)%M的值
思路:
整数快速幂。通过计算整数快速幂计算各个ai^bi%M的值,然后再求和。由数论可知,表达式的求余可以先对加法各项求余,再求和求余。所以分别计算各ai^bi时,应直接代入求余。
用整数快速幂算法算出每一个幂值,然后依次相加取模即可
#include<iostream>
#include<cstdio>
using namespace std;
#define LL long long
int quick_mod(LL a,LL n,LL MOD)
{
LL ans=1;
while(n){
if(n&1){
ans=(ans*a)%MOD;
n--;
}
n/=2;
a=a*a%MOD;
}
return ans;
}
int main(){
int T,i;
LL n,m,ans,a,b;
scanf("%d",&T);
while(T--){
scanf("%lld%lld",&m,&n);
ans=0;
for(i=1;i<=n;i++){
scanf("%lld%lld",&a,&b);
ans=(ans+quick_mod(a,b,m))%m;
}
printf("%lld\n",ans);
}
return 0;
}
0 0
- POJ 1995/ ZOJ 2150 : 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 快速幂
- Raising Modulo Numbers 【poj-1995】【快速幂】
- 【POJ 1995】Raising Modulo Numbers(快速幂)
- Poj.1995 Raising Modulo Numbers【快速幂取模】 2015/04/22
- POJ 1995 Raising Modulo Numbers(快速幂取模)
- Raising Modulo Numbers 快速幂取模
- POJ 2417:Discrete Logging——Baby Step, Giant Step
- Openstack Nova Failed to allocate network(s)
- POJ 3243: Clever Y 求A^X = B (mod C) / BabyStep_GiantStep
- POJ 1023 : The Fun Number System - 二进制
- LeetCode-Search Insert Position
- POJ 1995/ ZOJ 2150 : Raising Modulo Numbers - 快速幂取模
- POJ 1079 : Ratio - 分数逼近
- 优化的选择排序和折半查找法
- CDH5.3集群安装笔记-环境准备(2)
- POJ 2142 : The Balance-解不定方程,一元线性同余
- POJ 1305:Fermat vs. Pythagoras - 勾股定理/毕达哥拉斯三元组
- POJ 1320:Street Numbers——佩尔方程
- POJ 2262/ ZOJ 1951:Goldbach\'s Conjecture - 筛法打素数表
- 2015年10大web预测