Raising Modulo Numbers (POJ 1995 Fast Power)

Source: Internet
Author: User

Raising Modulo Numbers
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 5934 Accepted: 3461

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.

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

3//z16//m4//h2 3 h x a,b;3 44 55 6//Calculation (A1B1+A2B2+ ... +AHBH)mod M . 3612312374859 30293821713 18132

Sample Output

21319513
Quick Power Template problem
1#include <iostream>2#include <cstring>3#include <algorithm>4 using namespacestd;5 #defineLL Long Long6 #defineMax 45000+107 LL A[max],b[max];8 intm,h;9 LL ans;Ten intMod_pow (intNumintN) One { Anum=num%m; -LL res=1; -      while(n>0) the     { -         if(n&1) -res=res*num%m; -num=num*num%m; +n>>=1; -     } +     returnRes; A } at intMain () - { -     intZ; -     inti,j; -Freopen ("In.txt","R", stdin); -scanf"%d",&z); in      while(z--) -     { toans=0; +scanf"%d%d",&m,&h); -     //cout<<m<< "" < the          for(i=0; i) *scanf"%lld%lld",&a[i],&b[i]); $          for(i=0; i)Panax Notoginseng         { -ans+=Mod_pow (A[i],b[i]); theans=ans%m; +         } Aprintf"%d\n", ans%m); the     } +}

Raising Modulo Numbers (POJ 1995 Fast Power)

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.