POJ 1995 Raising modulo Numbers (fast power, congruence theorem) __ congruence theorem

Source: Internet
Author: User
Raising modulo Numbers
Time Limit: 1000MS Memory Limit: 30000K
Total submissions: 5510 accepted: 3193

Description people are 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 of the shows, that this market segment be so far underestimated and that there is lack of such. This kind of game is 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 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 AF ter division by a given number M. The winner is the "one who", the correct result. According to the players ' experience it are possible to increase the difficulty by choosing higher.

You are should write a program, that calculates, the result and are able to find out who won the game.

Input the input consists of Z assignments. The number of them is given through the single positive integer Z appearing on the Then the assignements follow. Each assignement begins with line containing of an 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 each line, the There are exactly two 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 are a number, the result of expression

(a1b1+a2b2+. +ahbh) mod M.

Sample Input

3
4 2 3 3 4 4 5 5 6
36123
1
2374859 3029382
1
3 18132

Sample Output

2
13195
13

Input a T, represent T Group data, input m, then input n, after N is n group of data, each group of data has two numbers a, B. Output the sum of the B-square of the A of this n group to M

The value of the remainder.

Solving: To find the sum of the B-square of A of n group and the value of M, according to the congruence theorem (a+b)%m= (a%m+b%m)%m that is to find the sum of the value of the B-th side of a of the N group to M. The value of the B-second square to M-remainder of a can be obtained by using a fast power template.

The specific code is as follows:

#include <cstdio>

int pow_mod (int a,int b,int m)/fast power modulo template, bearing in mind 
{
	int ans,t;
	Ans=1;
	t=a%m;
	while (b>0)
	{
		if (b&1)
		  ans= (ans*t)%m;
		b>>=1;
		t= (t*t)%m;
	}
	return ans;
}

int main ()
{
	int t,n,m,a,b,sum,cnt,i;
	scanf ("%d", &t);
	while (t--)
	{
		scanf ("%d%d", &m,&n);
		sum=0;
		for (I=0;i<n;++i)
		{
			scanf ("%d%d", &a,&b);
			Cnt=pow_mod (a,b,m);
			Sum= (sum+cnt)%m;
		}
		printf ("%d\n", sum);
	}
	return 0;


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.