POJ 1995 Calculates The Numbers
来源:互联网 发布:淘宝店铺轮播在线制作 编辑:程序博客网 时间:2024/06/05 17:11
D - Calculates The Numbers
Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64uDescription
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.
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
快速幂:加法和乘法对于模运算满足分配律。
#include <iostream>using namespace std;long long a[50000],b[50000],z,m,h;long long qm(long long a,long long b){ long long t; if (b==0) return 1; if (b==1) return a%m; if (b%2==0) { t=qm(a,b/2); return t*t%m; } else { t=qm(a,b/2); t=t*t%m; t=t*a%m; return t; }}int main(){ int i,j; long long ans; cin>>z; while (z--){ cin>>m>>h; ans=0; for (i=1;i<=h;i++) { cin>>a[i]>>b[i]; ans=(ans+qm(a[i],b[i])) %m; } cout<<ans<<endl; } return 0;}
0 0
- POJ 1995 Calculates The Numbers
- Calculates The Numbers
- poj 3899 The Lucky Numbers
- Calculates the distance on the surface of the earth within matlab
- poj-3899-The Lucky Numbers 模拟+数学
- POJ 1995 Raising Modulo Numbers
- poj 1995 Raising Modulo Numbers
- POJ-1995-Raising Modulo Numbers
- POJ - 1995 Raising Modulo Numbers
- poj 1995 Raising Modulo Numbers
- POJ 1995 Raising Modulo Numbers
- POJ--1995--Raising Modulo Numbers
- POJ-1995 Raising Modulo Numbers
- POJ 1995 Raising Modulo Numbers
- poj 1995Raising Modulo Numbers
- POJ 1995 Raising Modulo Numbers
- POJ 1995 Raising Modulo Numbers
- POJ -1995 Raising Modulo Numbers
- 《ZedBoard各种资料网址备份记录》
- 简单并查集,记录每个点有几个节点“杭电多校联赛签到题”
- HDU1542 Atlantis(面积并)
- Spring MVC详解(四)Controller接口控制器详解(3)
- JAVA 获取本地所有的IP地址,多网卡多IP,单网卡多个IP
- POJ 1995 Calculates The Numbers
- 进程与线程之间的关系
- Spring MVC详解(四)Controller接口控制器详解 (4)
- sql 2008 清除日志
- 深入分析Java ClassLoader原理
- 程序员,不要轻易重写代码。
- python基础教程学习笔记 — 基本概念
- Spring MVC详解(四)Controller接口控制器详解(5)
- 手动编译安装LAMP架构,并且实现nagios图形化监控(三)