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

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

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 6530   Accepted: 3863

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
Bifrom 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

#include 
__int64 pow(__int64 x, __int64 y, __int64 mod) { __int64 res = 1; __int64 base = x; while (y) { if (y&1) res = base*res%mod; base = base*base%mod; y >>= 1; } return res;}int main() { __int64 n, m, p; int t, s; scanf("%d", &t); while (t--) { scanf("%I64d", &p); scanf("%d", &s); int ans = 0; for (int i = 0; i < s; i++) { scanf("%I64d%I64d", &n, &m); ans += pow(n, m, p); ans %= p; } printf("%d\n", ans); } return 0;}

转载于:https://www.cnblogs.com/cniwoq/p/6770920.html

你可能感兴趣的文章
Sharepoint学习笔记—error处理-- The user does not exist or is not unique.
查看>>
CSS占位隐藏(转)
查看>>
利用jsdoc-toolkit生成javascript文档
查看>>
javascript 判断浏览器客户端
查看>>
NSString 中包含中文字符时转换为NSURL
查看>>
排序总结
查看>>
PreferenceCategory背景颜色设置
查看>>
Cocos2d-xna : 横版战略游戏开发实验4 Layer构建丰富的交互
查看>>
给孩子增加学习生物的兴趣,买了个显微镜
查看>>
代码风格 2012/10/12
查看>>
Source Code Pro - 来自 Adobe的最佳编程字体!
查看>>
Uva 11300 Spreading the Wealth
查看>>
深度拷贝
查看>>
远程桌面时自动输入“c“的解决方法
查看>>
谨慎的沉默就是精明的回避
查看>>
音频采样位数问题
查看>>
Response.Clear() Response.ClearContent()和Response.ClearHeaders()之间的区别
查看>>
数字签名、数字证书、对称加密算法、非对称加密算法、单向加密(散列算法)...
查看>>
linux zip
查看>>
一个简单的统计图像主颜色的算法(C#源代码)
查看>>