(快速幂) 求(A1B1+A2B2+ ... +AHBH)mod M

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 Ai Bi 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

结合律

((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

交换律

(a + b) mod p = (b+a) mod p

(a × b) mod p = (b × a) mod p

分配律

((a +b)mod p × c) mod p = ((a × c) mod p + (b × c) mod p) mod p

(a×b) mod c=(a mod c * b mod c) mod c

(a+b) mod c=(a mod c+ b mod c) mod c

(a-b) 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         {
 9             sun=sun*a%m;
10         }
11         a=a*a%m;
12         b/=2;
13     }
14         return sun%m;
15 }
16 int main()
17 {
18     int t,i;
19     __int64 n,m,sum,a,b;
20     scanf("%d",&t);
21     while(t--)
22     {
23         sum=0;
24         scanf("%I64d",&m);
25         scanf("%I64d",&n);
26         for(i = 0 ; i < n; i++)
27         {
28             scanf("%I64d %I64d",&a,&b);
29             sum+=f(a,b,m);
30     //        printf("---%d--\n",f(a,b,m));
31         }
32         printf("%I64d\n",sum%m);
33     }
34 }
时间: 2024-07-28 20:55:40

(快速幂) 求(A1B1+A2B2+ ... +AHBH)mod M的相关文章

POJ 3070-Fibonacci(矩阵快速幂求斐波那契数列)

Fibonacci Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3070 Appoint description:  System Crawler  (2015-02-28) Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn ? 1 +

poj 3070 Fibonacci (矩阵快速幂求斐波那契数列的第n项)

题意就是用矩阵乘法来求斐波那契数列的第n项的后四位数.如果后四位全为0,则输出0,否则 输出后四位去掉前导0,也...就...是...说...输出Fn%10000. 题目说的如此清楚..我居然还在%和/来找后四位还判断是不是全为0还输出时判断是否为0然后 去掉前导0.o(╯□╰)o 还有矩阵快速幂的幂是0时要特判. P.S:今天下午就想好今天学一下矩阵乘法方面的知识,这题是我的第一道正式接触矩阵乘法的题,欧耶! #include<cstdio> #include<iostream>

数论——快速幂,模运算及快速幂求逆元

一.快速幂 原理: 快速幂的原理十分简单. ak=a2^0*a2^1*a2^2*…a2^x,其中k=20+21+22+…+2x. 这显然是正确的.因为任何一个数都可以表示成二进制. 接下去利用位运算实现即可. 代码实现 模板题链接:快速幂 代码模板如下:时间复杂度O(logk) int qmi(int a,int k,int p) { int res=1%p; while(k) { if(k&1)res=(long long)res*a%p; a=(long long)a*a%p; k>&g

hdu 3221 Brute-force Algorithm(快速幂取模,矩阵快速幂求fib)

http://acm.hdu.edu.cn/showproblem.php?pid=3221 一晚上搞出来这么一道题..Mark. 给出这么一个程序,问funny函数调用了多少次. 我们定义数组为所求:f[1] = a,f[2] = b, f[3] = f[2]*f[3]......f[n] = f[n-1]*f[n-2].对应的值表示也可为a^1*b^0%p,a^0*b^1%p,a^1*b^1%p,.....a^fib[n-3]*b^fib[n-2]%p.即a,b的指数从n=3以后与fib数列

poj 3070 Fibonacci 【矩阵快速幂 求第N个斐波那契数%1000】

Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11123   Accepted: 7913 Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn ? 1 + Fn ? 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequenc

hdu4686 简单的矩阵快速幂求前n项和

HDU4686 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4686 题意:题目说的很清楚了,英语不好的猜也该猜懂了,就是求一个表达式的前n项和,矩阵快速幂一般多加一行一列来完成这个加的操作.具体看代码吧.比较简单,唯一有一点坑的地方,就是ax和bx可能比较大,在求ax*bx的时候,要考虑溢出的问题,需要先mod.其他没有了,直接看代码吧! //Author: xiaowuga #include <bits/stdc++.h> #define

poj 3070 Fibonacci(矩阵快速幂求Fibonacci数列)

题目链接: http://poj.org/problem?id=3070 题意: 我们知道斐波那契数列0 1 1 2 3 5 8 13-- 数列中的第i位为第i-1位和第i-2位的和(规定第0位为0,第一位为1). 求斐波那契数列中的第n位mod 10000的值. 思路: 这里的n很大,有10^9,for一遍肯定是不可以的. 所以要用矩阵乘法求斐波那契数列. f[n+1] = f[n]+f[n-1] f[n] = f[n] 构造以下矩阵,n次幂,mat[1][0] 就是答案 1 1 1 0 求矩

hdu 2837 Calculation【欧拉函数,快速幂求指数循环节】

欢迎关注__Xiong的博客: http://blog.csdn.net/acmore_xiong?viewmode=list Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1912    Accepted Submission(s): 413 链接:click me Problem Description A

a^b(快速幂) 求 a 的 b 次方对 p 取模的值。

题目详情 求 a 的 b 次方对 p 取模的值. 输入格式 三个整数 a,b,p在同一行用空格隔开. 输出格式 输出一个整数,表示a^b mod p的值. 数据范围 0≤a,b,p≤10^9 输入样例: 3 2 7 输出样例: 2 问题解决 正常来说,计算机每秒可运算10^7-10^8次(以c++语言来说)所以这题如果用循环一个个的来乘,最大有10^9的运算量,有可能超时.应采用快速幂算法,运算量则会降为log级别的! 以上面提供的数据来说明 快速幂: 7转化为二进制为1112^0=12^1=2