Raising Modulo Numbers
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 5532   Accepted: 3210

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

题意就是求(A1B1+A2B2+ ... +AHBH)mod M.

原来求一个数A的B次幂都是一级一级循环,时间复杂度是O(n),且最后取余M的话很容易溢出。

快速幂:比方说求2的9次幂,它的思想是求2的4次幂*2的4次幂*2,这样2的4次幂只需求一次。2的4次幂怎么求,还是和原来一样,是2的2次幂*2的2次幂,2的2次幂等于2*2;

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; long long getresult(long long m,long long n,long long k)
{
long long b = 1;
while (n > 0)
{
if (n & 1)
b = (b*m) % k;
n = n >> 1;
m = (m*m) % k;
}
return b;
}
int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout); long long Test,i,n,k,temp1,temp2,result;
cin>>Test; while(Test--)
{
result = 0; cin >> k >> n;
for (i = 1; i <= n; i++)
{
cin >> temp1 >> temp2;
result += getresult(temp1, temp2, k);
result = result%k;
}
cout << (result%k) << endl;
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 1995:Raising Modulo Numbers 快速幂的更多相关文章

  1. POJ 1995 Raising Modulo Numbers (快速幂)

    题意: 思路: 对于每个幂次方,将幂指数的二进制形式表示,从右到左移位,每次底数自乘,循环内每步取模. #include <cstdio> typedef long long LL; LL ...

  2. poj 1995 Raising Modulo Numbers【快速幂】

    Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5477   Accepted: ...

  3. POJ1995 Raising Modulo Numbers(快速幂)

    POJ1995 Raising Modulo Numbers 计算(A1B1+A2B2+ ... +AHBH)mod M. 快速幂,套模板 /* * Created: 2016年03月30日 23时0 ...

  4. poj 1995 Raising Modulo Numbers 题解

    Raising Modulo Numbers Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6347   Accepted: ...

  5. POJ 1995 Raising Modulo Numbers 【快速幂取模】

    题目链接:http://poj.org/problem?id=1995 解题思路:用整数快速幂算法算出每一个 Ai^Bi,然后依次相加取模即可. #include<stdio.h> lon ...

  6. POJ 1995 Raising Modulo Numbers(快速幂)

    嗯... 题目链接:http://poj.org/problem?id=1995 快速幂模板... AC代码: #include<cstdio> #include<iostream& ...

  7. POJ 1995 Raising Modulo Numbers

    快速幂取模 #include<cstdio> int mod_exp(int a, int b, int c) { int res, t; res = % c; t = a % c; wh ...

  8. ZOJ2150 Raising Modulo Numbers 快速幂

    ZOJ2150 快速幂,但是用递归式的好像会栈溢出. #include<cstdio> #include<cstdlib> #include<iostream> # ...

  9. POJ1995:Raising Modulo Numbers(快速幂取余)

    题目:http://poj.org/problem?id=1995 题目解析:求(A1B1+A2B2+ ... +AHBH)mod M. 大水题. #include <iostream> ...

随机推荐

  1. mysql 命令行导入导出.sql文件

    window下 1.导出整个数据库mysqldump -u 用户名 -p 数据库名 > 导出的文件名mysqldump -u dbuser -p dbname > dbname.sql 2 ...

  2. grep -o -E

    sed 命令可以很好的进行行匹配,但从某一行中精确匹配某些内容,则使用 grep 命令并辅以 -o 和 -E 选项可达到此目的.其中 -o 表示“only-matching”,即“仅匹配”之意.光用它 ...

  3. WAFの基本防护透明流模式v1.0

                      一.WAFの透明流模式     1)首先先配置WAF的网络,配置一个网桥接口,设置IP便于带内管理.             2)当然,如果需要不同网段之间都能够管 ...

  4. dom与jq基础使用

    js有两种运行环境,一个是浏览器,一个是服务器(NodeJS) js的本质是es,因为运行环境的不同,为了操作环境内的api做了升级 在浏览器上js分为es + dom + bom 在服务器上js又有 ...

  5. JAVA学习笔记-数组的三种初始化方式

      package Study; public class TestArray02 { public static void main(String[] args){//声明 int[] a; int ...

  6. python 文件与文件夹相关

    1.判断文件夹是否存在,不存在则创建文件夹: if not os.path.exists(path): os.makedirs(path) 2.判断文件是否存在,存在就删除: os.path.exis ...

  7. UVA - 10891 Game of Sum (区间dp)

    题意:AB两人分别拿一列n个数字,只能从左端或右端拿,不能同时从两端拿,可拿一个或多个,问在两人尽可能多拿的情况下,A最多比B多拿多少. 分析: 1.枚举先手拿的分界线,要么从左端拿,要么从右端拿,比 ...

  8. Kafka--windows下简单使用kafka命令

    参考 https://www.cnblogs.com/cici20166/p/9426417.html 启动zookeeper 只需要保证有可用的zookeeper,可以使用kafka内置的,也可以自 ...

  9. 【pwnable.kr】 memcpy

    pwnable的新一题,和堆分配相关. http://pwnable.kr/bin/memcpy.c ssh memcpy@pwnable.kr -p2222 (pw:guest) 我觉得主要考察的是 ...

  10. 八十四、SAP中的ALV创建之三,创建ALV表格

    一.销售表是2个表,一个抬头表,一个是销售内容表,数据库查询语句如下, 二.我们添加相关LAYOUT的格式控制如下 三.需要报每个字段都用相应的LAYOUT控制一下 四.点击模式,在模式里面,添加RE ...