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 AiBi 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
3
16
4
2 3
3 4
4 5
5 6
36123
1
2374859 3029382
17
1
3 18132
Sample Output
2
13195
13
Source
Description
人是不同的。有些人偷偷看充满了有趣的女孩的杂志,有些人在地窖里制造原子弹,有些人喜欢使用WINDOWS,有些人喜欢数学游戏。最新的市场调查显示,这一细分市场目前被低估了,而且缺乏这样的游戏。这种游戏因此被纳入了科科达克运动。运动的遵循的规则:
每个玩家选择两个数字Ai和Bi,并把它们写在一张纸条上。其他人看不到这些数字。在一个特定的时刻,所有的玩家都会向其他玩家展示他们的数字。目标是确定包括自己在内的所有玩家的所有表达的总和,并用给定的数字M确定除法后的余数。获胜者是首先决定正确结果的人。根据玩家的经验,可以通过选择更高的数字来增加难度。
你应该编写一个程序来计算结果,并找出谁赢了这场比赛。
Input
输入由Z赋值组成。它们的数目由出现在输入的第一行上的单个正整数Z给出。然后分配如下。每个赋值都以包含一个整数M (1 <= M <= 45000)行开始。总数将除以这个数字。下一行包含玩家数H(1 <= H <= 45000)。接下来就是H行。在每一行上,正好有两个数字Ai和Bi被空间隔开。两个数不能同时等于零。
Output
对于每个组合,只有一条输出线。在这条线上,有个数字,是表达的结果。
SampleInput
3
16
4
2 3
3 4
4 5
5 6
36123
1
23748593029382
17
1
318132
SampleOutput
2
13195
13
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#define ll long long
using namespace std;
const int maxn=50000;
ll quick_pow(ll n,ll m,ll mod)
{
ll res=1;
while(m>0){
if(m&1)
res=res*n%mod;
n=n*n%mod;
m>>=1;
}
return res;
}
int main()
{
int z;
scanf("%d",&z);
while(z--){
ll mod,h;
scanf("%lld %lld",&mod,&h);
ll ans=0;
int a,b;
for(int i=0;i<h;i++)
{
scanf("%d%d",&a,&b);
ans=(ans+quick_pow(a,b,mod))%mod;
}
printf("%lld\n",ans);
}
return 0;
}