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
给出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; }
相关文章推荐
- raid write-bakc write-through的解释
- raid组的初始化详解
- Contains Duplicate
- 游戏人工智能开发之6种决策方法
- 游戏人工智能开发之6种决策方法
- oschinaIM/聊天/语音工具
- oschinaIM/聊天/语音工具
- awaitTermination() shutdown()
- mount: failed, reason given by server: Permission denied
- hdoj 1022 Train Problem I
- 1090. Highest Price in Supply Chain (25)
- Explain分析查询语句
- 1086. Tree Traversals Again (25)
- Netflix开源类库archaius(一)概述
- Contains Duplicate II
- 使用maven的toolchain
- AI设计之Behavior Designer
- BrainFuck解释器
- senrty 配置Email
- HDOJ 题目3487 Play with Chain(Splay Tree)