POJ 1995 Raising Modulo Numbers 【快速幂取模】

题目链接:http://poj.org/problem?id=1995

解题思路:用整数快速幂算法算出每一个 Ai^Bi,然后依次相加取模即可。

#include<stdio.h>
long long quick_mod(long long a,long long b,long long c)
{
	long long ans=1;
	while(b)
	{
		if(b&1)
		{
			ans=ans*a%c;
		}
		b>>=1;
		a=a*a%c;
	}
	return ans;
}
int main()
{
	int ncase;
	int m,h;
	int i;
	long long  a,b,s;
	scanf("%d",&ncase);
		while(ncase--)
		{
			s=0;
			scanf("%d",&m);
			scanf("%d",&h);
			for(i=1;i<=h;i++)
			{
				scanf("%lld %lld",&a,&b);
				s=(s+quick_mod(a,b,m))%m;
			}
			printf("%I64d\n",s);
		}
}
时间: 2024-08-09 02:05:33

POJ 1995 Raising Modulo Numbers 【快速幂取模】的相关文章

POJ 1995 Raising Modulo Numbers (快速幂模板)

Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4938   Accepted: 2864 Description People are different. Some secretly read magazines full of interesting girls' pictures, others create an A-bomb in their cellar, oth

POJ 1995 Raising Modulo Numbers (快速幂)

题意: 思路: 对于每个幂次方,将幂指数的二进制形式表示,从右到左移位,每次底数自乘,循环内每步取模. #include <cstdio> typedef long long LL; LL Ksm(LL a, LL b, LL p) { LL ans = 1; while(b) { if(b & 1) { ans = (ans * a) % p; } a = (a * a) % p; b >>= 1; } return ans; } int main() { LL p, a

Raising Modulo Numbers_快速幂取模算法

Description People are different. Some secretly read magazines full of interesting girls' pictures, others create an A-bomb in their cellar, others like using Windows, and some like difficult mathematical games. Latest marketing research shows, that

POJ1995:Raising Modulo Numbers(快速幂取余)

题目:http://poj.org/problem?id=1995 题目解析:求(A1B1+A2B2+ ... +AHBH)mod M. 大水题. #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> using namespace std; int n,mod,sum; int main() { int

POJ 1995 Raising Modulo Numbers (数论-整数快速幂)

Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4379   Accepted: 2516 Description People are different. Some secretly read magazines full of interesting girls' pictures, others create an A-bomb in their cellar, oth

poj Raising Modulo Numbers 快速幂模板

Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 8606   Accepted: 5253 Description People are different. Some secretly read magazines full of interesting girls' pictures, others create an A-bomb in their cellar, oth

POJ1995 Raising Modulo Numbers(快速幂)

POJ1995 Raising Modulo Numbers 计算(A1B1+A2B2+ ... +AHBH)mod M. 快速幂,套模板 /* * Created: 2016年03月30日 23时01分45秒 星期三 * Author: Akrusher * */ #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <ctime> #

poj 1995 Raising Modulo Numbers

Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4987   Accepted: 2887 Description People are different. Some secretly read magazines full of interesting girls' pictures, others create an A-bomb in their cellar, oth

poj 1995 Raising Modulo Numbers 题解

Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6347   Accepted: 3740 Description People are different. Some secretly read magazines full of interesting girls' pictures, others create an A-bomb in their cellar, oth