博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Poj 1995 Raising Modulo Numbers
阅读量:7279 次
发布时间:2019-06-30

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

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

题意

计算几个幂的和对M的余数

思路

典型快速幂的做法,没什么好说的...

 

#include
#include
using namespace std;int mod_mi(int a, int b, int m){ int tot = 1; while (b != 0) { a = a%m; if (b & 1) { tot = a*tot%m; } a = a*a; b=b >> 1; } return tot;}int main(){ int w; scanf("%d", &w); for (int k = 0; k < w; k++) { int n, M; scanf("%d%d", &M, &n); int a, b, m=0; for (int i = 0; i < n; i++) { scanf("%d%d", &a, &b); m = m + mod_mi(a, b, M); } m = m%M; printf("%d\n", m); }}

 

 

转载于:https://www.cnblogs.com/zhy-ang/p/6804743.html

你可能感兴趣的文章
一对一直播源码全套开源,二次开发有保障!
查看>>
NumPy 超详细教程(3):ndarray 的内部机理及高级迭代
查看>>
侃一侃WebSocket
查看>>
hanlp源码解析之中文分词算法
查看>>
把你的程序放到桌面——Android桌面部件Widget
查看>>
《图解HTTP》第3章_HTTP报文内的HTTP信息-思维导图
查看>>
分享一个冷门知识——文本框的选择文本在业务中的应用
查看>>
彻底理解浏览器的跨域
查看>>
1009 说反话 (20 分)
查看>>
Flutter Wrap & Chip
查看>>
Vue路由自动注入实践
查看>>
类数组转化成数组的方法
查看>>
Android屏幕适配方案
查看>>
使用Databinding轻松快速打造仿携程app筛选控件(二)
查看>>
AppCompatActivity怎么对View做的拦截
查看>>
记b站的一次react尝试
查看>>
Binder IPC
查看>>
mpvue开发小程序
查看>>
LINUX使用LDAP进行统一认证
查看>>
linux 下 ifcfg-eth0 配置
查看>>