Calculates The Numbers
2015-07-23 15:13
134 查看
D - Calculates The Numbers
Time Limit:1000MS Memory Limit:30000KB 64bit
IO Format:%I64d & %I64u
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
Sample Output
快速幂求模:
原理:((a^2)^i)%c=((a^2*(i-1)%c)*a^2*(i-1))%c
函数模板:
long MI(long a,long b,long c){
long temp=1;
while(b>=1){
if(b%2==1)
temp=a*temp%c;
a=a*a%c;
b=b/2;}
return temp;
}
代码:
Time Limit:1000MS Memory Limit:30000KB 64bit
IO Format:%I64d & %I64u
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
31643 3245 64 53 1 23612374859 3029382171 323 18 1
Sample Output
21319513
快速幂求模:
原理:((a^2)^i)%c=((a^2*(i-1)%c)*a^2*(i-1))%c
函数模板:
long MI(long a,long b,long c){
long temp=1;
while(b>=1){
if(b%2==1)
temp=a*temp%c;
a=a*a%c;
b=b/2;}
return temp;
}
代码:
#include<iostream> #include<cstdio> #include<cmath> using namespace std; int T; long long M,H,A,B,temp,sum; int main(){ cin>>T; while(T--){ cin>>M; cin>>H; sum=0; while(H--){ cin>>A>>B; temp=1; while(B>=1){ if(B%2==1) temp=temp*A%M; A=A*A%M; B=B/2; } sum=(sum+temp)%M; } cout<<sum<<endl; } return 0; }
相关文章推荐
- RTMFP协议& RTMP
- McAfee EPO3.61更改系统管理员密码后EPO服务启动失败,控制台无法登陆
- Java 基础笔记(一)
- seo优化必须知道的robots.txt写法
- MSSQL 排序函数 ROW_NUMBER() RANK() DENSE_RANK() NTILE()
- Window 下 VFW 视频采集与显示
- 在VS中让一个JS文件智能提示另一个JS文件中的成员
- java装饰者模式
- 网站发布
- 数据库事务正确执行的四个基本要素
- Activity的生命周期
- Node.js前端自动化工具:gulp
- C++ List的用法(整理)
- Vmstst
- OS X 10.10 编译APUE Third Edition 源码
- MySQL使用经验总结
- Leancloud-数据存储功能入门
- SVN插件
- PowerShell 管理VMWare
- 不同进程间切换