Raising Modulo Numbers modulus + fast Power

Source: Internet
Author: User
Tags modulus

Raising Modulo Numberstitle address: http://poj.org/problem?id=1995

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 Aibifrom all players including oneself and determine the remainder aft Er 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

31642 33 44 55 63612312374859 30293821713 18132

Sample Output

21319513

Test instructions: First input z-group test data, and then input two integers, the former is M, the latter represents the input H line data, each line of two integers a, b, for all A's B to add after the M modulo

Idea: 1:a's B-order uses the fast power

2: (a+b)%mod = (A%mod + b%mod)%mod

The code is as follows:

1#include <cstdio>2__int64 Fun (intNintMintMoD)3 {4__int64 ans=1,Base=N;5      while(m)6     {7         if(m&1)/*true for odd time*/Ans= (Base*ans)%MoD;8         Base=(Base*Base)%MoD;9m>>=1;//equivalent to M=M/2Ten     } One     returnans; A } - intMain () - { the     intT; -scanf"%d",&t); -      while(t-- ) -     { +__int64 M, H, a, B, I, J, K, mod=0; -scanf"%i64d%i64d", &m, &H); +          for(i=0; i) A         { atscanf"%i64d%i64d", &a, &b); -K =Fun (A, B, M); -MoD = (mod%m + k)% M;//K has been in advance to the M to take surplus -         } -printf"%i64d\n", MoD); -     } in     return 0; -}

Raising Modulo Numbers modulus + 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.