Raising Modulo Numbers

Time Limit: 1000MS Memory Limit: 30000K

Total Submissions: 9512 Accepted: 5783

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

(A1^B1+A2^B2+ … +AH^BH)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


解题心得:

  1. 其实直接按照题目中给的公式计算就行了,只不过需要用一下快速幂,这个题主要也就考察了一个快速幂。

#include <algorithm>
#include <cstring>
#include <stdio.h>
#include <vector>
using namespace std;
typedef long long ll;
ll m,n; ll mod_mult(ll n,ll p) {
ll res = 1;
while(p) {
if(p & 1)
res = (res * n) % m;
n = (n * n) % m;
p >>= 1;
}
return res % m;
} void Solve() {
ll ans = 0;
scanf("%lld%lld",&m,&n);
for(int i=0;i<n;i++){
ll a,b;
scanf("%lld%lld",&a,&b);
ans += mod_mult(a,b);
ans %= m;
}
printf("%lld\n",ans);
} int main() {
int t;
scanf("%d",&t);
while(t--) {
Solve();
}
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: 5532   Accepted: ...

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

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

  4. POJ1995 Raising Modulo Numbers(快速幂)

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

  5. poj 1995 Raising Modulo Numbers 题解

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

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

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

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

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

  8. 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 ...

  9. ZOJ2150 Raising Modulo Numbers 快速幂

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

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

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

随机推荐

  1. PhoneGap&jQuery Mobile应用开发环境配置(For Android)

    关于移动应用为什么用PhoneGap和jQuery Mobile本文不再赘述,有兴趣的童鞋可以自行问“度娘”,有很多这方面的文章.本文主要介绍PhoneGap&jQuery Mobile移动应 ...

  2. Azure杯年会Cosplay大赛,速来围观!

    又是一年年会时,正值各家公司红人纷纷登场之际,Azure 自然也不会缺席.我们今年的主题是:Azure 杯年会 Cosplay,秀出你最爱的角色! 在过去的一年中,Azure 智能云取得了长足的发展, ...

  3. python3绘图示例6-1(基于matplotlib,绘图流程介绍及设置等)

    #!/usr/bin/env python# -*- coding:utf-8 -*- import os import pylab as pyimport numpy as npfrom matpl ...

  4. Python基础学习之变量赋值

    1.赋值操作符 Python语言中,等号(=)是主要的赋值操作符: >>> aInt=-100 >>> aString='this is a string' > ...

  5. Java I/O 工作机制(一) —— Java 的 I/O 类库的基本架构

    Java 的 I/O 类库的基本架构 Java 的 I/O 操作类在包 java.io 下,有将近 80 个类. 按数据格式分类: 面向字节(Byte)操作的 I/O 接口:InputStream 和 ...

  6. 如何处理Eclipse错误消息 The declared package does not match the expected package

    我从github下载了一个开源项目后,导入到自己Eclipse之后,遇到了这个烦人的错误消息: The declared package "com.sap.smartService" ...

  7. AFNetworking 初探

    AFNetworking 初探 繼ASIHTTPRequest發佈不再維護的訊息之後,如果我們不使用CDN(雲端伺服器),AFNetworking 會是一套不錯的選擇.下載網址:https://git ...

  8. php简单开启gzip压缩方法(zlib.output_compression)

    网上的教程基本是你抄我来我抄他,不外乎加头加尾或者自构函数两种写法.实际上每个php页面都要去加代码——当然也可以include引用,不过总显得略微麻烦   一般而言,页面文件开启gzip压缩以后,其 ...

  9. P1909 买铅笔

    题目描述 P老师需要去商店买n支铅笔作为小朋友们参加NOIP的礼物.她发现商店一共有 33种包装的铅笔,不同包装内的铅笔数量有可能不同,价格也有可能不同.为了公平起 见,P老师决定只买同一种包装的铅笔 ...

  10. vim 中的":wq"和":x"的区别

    ":x" 和 ":wq" 的区别如下:(1) :wq 强制性写入文件并退出(存盘并退出 write and quite).即使文件没有被修改也强制写入,并更新文 ...