博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ1995 ZOJ2150 Raising Modulo Numbers【快速模幂】
阅读量:7227 次
发布时间:2019-06-29

本文共 2620 字,大约阅读时间需要 8 分钟。

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 8010   Accepted: 4875

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 this market segment was so far underestimated and that there is lack of such games. This kind of game was thus included into the KOKODáKH. The rules follow:  
Each player chooses two numbers Ai and Bi and writes them on a slip of paper. Others cannot see the numbers. In a given moment all players show their numbers to the others. The goal is to determine the sum of all expressions Ai
Bi  from all players including oneself and determine the remainder after division by a given number M. The winner is the one who first determines the correct result. According to the players' experience it is possible to increase the difficulty by choosing higher numbers.  
You should write a program that calculates the result and is able to find out who won the game.  

Input

The input consists of Z assignments. The number of them is given by the single positive integer Z appearing on the first line of input. Then the assignements follow. Each assignement begins with line containing an integer M (1 <= M <= 45000). The sum will be divided by this number. Next line contains number of players H (1 <= H <= 45000). Next exactly H lines follow. On each line, there are exactly two numbers Ai and Bi separated by space. Both numbers cannot be equal zero at the same time.

Output

For each assingnement there is the only one line of output. On this line, there is a number, the result of expression  

(A1B1+A2B2+ ... +AHBH)mod M.

Sample Input

31642 33 44 55 63612312374859 30293821713 18132

Sample Output

21319513

Source

问题链接:。

问题简述:参见上述链接。

问题分析

是一个单纯的模幂计算和求和问题,调用模幂计算函数即可

不用快速模幂计算的话,时间上会超时。

程序说明:函数powermod()是模幂计算函数。

AC的C++语言程序如下

/* POJ1995 ZOJ2150 Raising Modulo Numbers */#include 
using namespace std;typedef unsigned long long ULL;// 模幂计算ULL powermod(ULL a, ULL n, ULL m){ ULL res = 1LL; while(n) { if(n & 1LL) { // n % 2 == 1 res *= a; res %= m; } a *= a; a %= m; n >>= 1; } return res;}int main(){ int z, m, h, ai, bi, ans; cin >> z; while(z--) { cin >> m >> h; ans = 0; while(h--) { cin >> ai >> bi; ans += (int)powermod(ai, bi, m); } ans %= m; cout << ans << endl; } return 0;}

转载于:https://www.cnblogs.com/tigerisland/p/7563763.html

你可能感兴趣的文章
linux下mysql的root密码忘记解决方法
查看>>
7.索引的性能分析
查看>>
在 Delphi 下使用 DirectSound (17): 频率均衡效果器 IDirectSoundFXParamEq8
查看>>
文件操作命令一cp 2
查看>>
Multi-Mechanize工程目录结构说明
查看>>
halt
查看>>
标准ACL+扩展ACL+命名ACL
查看>>
Meteor应用的启动过程分析
查看>>
九曲黄河万里沙,浪淘风簸自天涯 — 正则表达式
查看>>
欲哭无泪,联想笔记本性价比
查看>>
很简单的在Ubuntu系统下安装字体和切换默认字体的方法
查看>>
我的友情链接
查看>>
dojo框架用hitch实现函数与上下文的绑定
查看>>
ubuntu编译安装vim7.4
查看>>
python之利用PIL库实现页面的图片验证码及缩略图
查看>>
IP-COM设置×××
查看>>
VPC配置案例
查看>>
十年IT运维谈(五):要专业化还是平台化?
查看>>
分享超级给力的一个外发光Shader
查看>>
oblog_4.6_SQL 语句
查看>>