poj 1995 Raising Modulo Numbers 题解
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 6347 | Accepted: 3740 |
Description
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
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
(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
Source
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<iomanip>
#include<cassert>
#include<climits>
#define maxn 50001
#define F(i,j,k) for(int i=j;i<=k;i++)
#define M(a,b) memset(a,b,sizeof(a))
#define FF(i,j,k) for(int i=j;i>=k;i--)
#define inf 0x7fffffff
#define p 23333333333333333
using namespace std;
int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int m,h;
long long ans;
long long kuaisumi(long long a,long long n)
{
long long ans=;
while(n) {
if(n&) ans=(ans*a)%m;
a=(a*a)%m;
n>>=;
}
return ans;
}
int main()
{
int t;
cin>>t;
while(t--){
ans=;
cin>>m>>h;
for(int i=;i<h;i++)
{
long long a,b;
cin>>a>>b;
ans=(ans+kuaisumi(a,b))%m;
}
cout<<ans<<endl;
}
return ;
}
poj 1995 Raising Modulo Numbers 题解的更多相关文章
- poj 1995 Raising Modulo Numbers【快速幂】
Raising Modulo Numbers Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5477 Accepted: ...
- POJ 1995 Raising Modulo Numbers 【快速幂取模】
题目链接:http://poj.org/problem?id=1995 解题思路:用整数快速幂算法算出每一个 Ai^Bi,然后依次相加取模即可. #include<stdio.h> lon ...
- POJ 1995 Raising Modulo Numbers(快速幂)
嗯... 题目链接:http://poj.org/problem?id=1995 快速幂模板... AC代码: #include<cstdio> #include<iostream& ...
- 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 ...
- POJ 1995 Raising Modulo Numbers (快速幂)
题意: 思路: 对于每个幂次方,将幂指数的二进制形式表示,从右到左移位,每次底数自乘,循环内每步取模. #include <cstdio> typedef long long LL; LL ...
- Raising Modulo Numbers(POJ 1995 快速幂)
Raising Modulo Numbers Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5934 Accepted: ...
- 【POJ - 1995】Raising Modulo Numbers(快速幂)
-->Raising Modulo Numbers Descriptions: 题目一大堆,真没什么用,大致题意 Z M H A1 B1 A2 B2 A3 B3 ......... AH ...
- POJ 1995:Raising Modulo Numbers 快速幂
Raising Modulo Numbers Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5532 Accepted: ...
- POJ1995 Raising Modulo Numbers
Raising Modulo Numbers Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6373 Accepted: ...
随机推荐
- n*m的矩阵,行和列都递增有序,求是否出现target元素(面试题)
题目描述:给定一个n*m的矩阵,矩阵的每一行都是递增的,每一列也是递增的,给定一个元素target,问该target是否在矩阵中出现. 思路:先从最左下角的元素开始找,三种情况: 1. 如果该元素大于 ...
- 使用Unity解耦你的系统—PART4——Unity&PIAB
在前面几篇有关Unity学习的文章中,我对Unity的一些常用功能进行介绍,包括:Unity的基本知识.管理对象之间的关系.生命周期.依赖注入等,今天则是要介绍Unity的另外一个重要功能——拦截(I ...
- Java 中 JDBC 基础配置
Java 中 JDBC 基础配置 <resource auth="Container" driverclassname="oracle.jdbc.driver.Or ...
- ref:Struts2 命令执行系列回顾
ref:http://www.zerokeeper.com/vul-analysis/struts2-command-execution-series-review.html Struts2 命令执行 ...
- 打开tcp_tw_recycle引起的一个问题
今天普空说了一个问题就是如果设置了tcp_tw_recycle ,那么如果客户端是NAT出来的,那么就可能会出现连接被直接rst的情况.然后我google了下,在内核列表也有人说了这个问题 https ...
- 获取token
获取token 提示:openstack 这个是获取N版的方法 ,主要区别在于这个路径上(http://192.168.0.228:35357/v3/auth/tokens ),以前版本可能会是v2 ...
- CodeForces 606C Sorting Railway Cars(最长连续上升子序列)
Description An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the n ...
- [AHOI2009]同类分布
题目大意: 问在区间[l,r]内的正整数中,有多少数能被其个位数字之和整除. 思路: 数位DP. 极端情况下,每一位都是9,所以各位数字之和不超过9*18.(为了方便这里用了9*19) f[i][j] ...
- SPFA cojs 176. [USACO Feb07] 奶牛聚会
cojs 176. [USACO Feb07] 奶牛聚会 ★☆ 输入文件:sparty.in 输出文件:sparty.out 简单对比时间限制:3 s 内存限制:16 MB N(1 ≤ ...
- [转]android中listview点击事件失效
首先说明一下我想实现的功能: 点击某个item之后,让其颜色发生变化.如果变化网上有很多例子,我就不班门弄斧了.Listview之所以点击没有反应是因为上图中绿色部分(自己定义的一个继承BaseAda ...