DescriptionPeople 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 Ai Bi from all players including oneself and determine the remainder a Fter 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.
InputThe 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.OutputFor 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
Binding law |
((a+b) mod p + c) mod p = (A + (b+c) mod p) mod p ((a*b) mod p * c) mod p = (A * (b*c) mod p) mod p |
Exchange Law |
(A + b) mod p = (b+a) mod p (axb) mod p = (BXA) mod p |
Distribution Law |
((a +b) mod pxc) mod p = ((AXC) mod p + (BXC) mod p) mod p(AXB) mod c= (a mod c * b mod c) mod c(a+b) mod c= (a mod c+ b mod c) modC (-a) mod c= (a mod c-b mod c) MoD c |
1#include <cstdio>2 __int64 F (__int64 a,__int64 b,__int64 m)3 {4__int64 sun=1;5 while(b)6 {7 if(b%2!=0)8 {9sun=sun*a%m;Ten } Onea=a*a%m; AB/=2; - } - returnsun%m; the } - intMain () - { - intt,i; + __int64 n,m,sum,a,b; -scanf"%d",&t); + while(t--) A { atsum=0; -scanf"%i64d",&m); -scanf"%i64d",&n); - for(i =0; I < n; i++) - { -scanf"%i64d%i64d",&a,&b); insum+=f (a,b,m); - //printf ("---%d--\n", f (a,b,m)); to } +printf"%i64d\n", sum%m); - } the}
(Fast power) (a1b1+a2b2+ ... +ahbh) mod M