Raising Modulo Numbers
Time Limit: 1000MS |
|
Memory Limit: 30000K |
Total Submissions: 6373 |
|
Accepted: 3760 |
Description
people is different. Some secretly read magazines full of interesting girls ' pictures, others create a a-bomb in their cellar, others like USI Ng Windows, and some like difficult mathematical games. Latest Marketing the shows, that's the market segment were so far underestimated and that there are lack of such games. This kind of game is thus included into the Kokodákh. The rules follow:
Each player chooses the 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 any expressions Aibi from all players including oneself and determine the remainder AF ter division by a given number M. The winner is the one who first determines the correct result. According to the players ' experience it's possible to increase the difficulty by choosing higher numbers.
You should write a program that calculates the result and are able to find out who won the game.
the above can not be looked at. Count the formula in the output.
Input
The input consists of Z assignments. The number of them is given by the "positive" integer Z appearing on the first line of input. Then the assignements follow. Each assignement begins with line containing a integer M (1 <= M <= 45000). The sum is divided by this number. Next line contains number of players H (1 <= H <= 45000). Next exactly H lines follow. On all line, there is exactly, numbers Ai and Bi separated by space. Both numbers cannot is equal zero at the same time.
Output
For each assingnement there are 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
CTU Open 1999
Quick power naked problem, violent calculation will be tle.
1 //Fast Power2#include <algorithm>3#include <iostream>4#include <cstdio>5#include <cstring>6#include <cmath>7 using namespacestd;8 intz,m;9 intN;Ten intsum; One intb; A intKsmintAintb) { - intnow=a%M; - intres=1; the while(b) { - if(b&1) res=res*now%M; -now=now*now%M; -b>>=1; + } - returnRes; + } A intMain () { atscanf"%d",&Z); - while(z--){ -sum=0; -scanf"%d",&M); -scanf"%d",&n); - inti,j; in for(i=1; i<=n;i++){ -scanf"%d%d",&a,&b); toSum= (SUM+KSM (b))%M;//Accumulate + } -printf"%d\n", sum); the } * return 0; $}
POJ1995 Raising Modulo Numbers