您的位置:首页 > 大数据 > 人工智能

POJ 1995 Raising Modulo Numbers(快速幂)

2015-09-09 08:52 507 查看
Description

给出A1,…,AH,B1,…,BH以及M,求(A1^B1+A2^B2+ … +AH^BH)mod M.

Input

第一行为用例组数T,每组用例第一行为一个整数M,第二行为一个整数H,之后H行每行两个整数Ai和Bi

Output

输出给出公式的值

Sample Input

3

16

4

2 3

3 4

4 5

5 6

36123

1

2374859 3029382

17

1

3 18132

Sample Output

2

13195

13

Solution

快速幂裸题

Code

#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;
ll mod_pow(ll a,ll b,ll p)
{
ll ans=1ll;
a%=p;
while(b)
{
if(b&1)
ans=(ans*a)%p;
a=(a*a)%p;
b>>=1;
}
return ans;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
ll M,a,b,ans=0ll;
scanf("%lld",&M);
int H;
scanf("%d",&H);
while(H--)
{
scanf("%lld%lld",&a,&b);
ans=(ans+mod_pow(a,b,M))%M;
}
printf("%lld\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: