Raising Modulo Numbers +快速幂取模算法
来源:互联网 发布:sql必知必会 pdf 微盘 编辑:程序博客网 时间:2024/06/06 18:42
Raising Modulo Numbers
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 4444 Accepted: 2551
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.
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
31642 33 44 55 63612312374859 30293821713 18132
Sample Output
21319513
解决方案:快速幂取模的模板题,没什么好讲的,码一下练手感
code:#include<iostream>#include<cstdio>using namespace std;int KK(int a,int b,int n){ if(b==0) return 1; if(b==1) return a%n; long long t=KK(a,b>>1,n); t=(t*t)%n; if(b&0x1){ t=(t*a)%n; } return t;}int main(){ int t; scanf("%d",&t); while(t--){ int M,n; scanf("%d%d",&M,&n); long long sum=0; for(int i=0;i<n;i++){ int a,b; scanf("%d%d",&a,&b); sum+=(KK(a,b,M)%M); sum%=M; } cout<<sum<<endl; } return 0;}
0 0
- Raising Modulo Numbers +快速幂取模算法
- Raising Modulo Numbers 快速幂取模
- POJ 1995 Raising Modulo Numbers 快速幂取模
- Raising Modulo Numbers 快速幂取模基础
- poj 1995 Raising Modulo Numbers 快速幂取模
- POJ1995 Raising Modulo Numbers 快速幂取模
- POJ 1995 Raising Modulo Numbers(快速幂取模)
- Raising Modulo Numbers ---- 快速幂
- POJ 1995/ ZOJ 2150 : Raising Modulo Numbers - 快速幂取模
- Poj.1995 Raising Modulo Numbers【快速幂取模】 2015/04/22
- POJ 1995 Raising Modulo Numbers(快速幂取模)
- POJ1995 Raising Modulo Numbers(快速幂取模,模板题)
- Raising Modulo Numbers(poj1995快速模运算)
- POJ1995 Raising Modulo Numbers 快速幂
- Raising Modulo Numbers (poj 1995 快速幂水题)
- POJ1995 Raising Modulo Numbers【整数快速幂】
- POJ 1995 Raising Modulo Numbers【快速幂】
- POJ_1995(快速幂)(Raising Modulo Numbers)
- hduoj1096(A+B for Input-Output Practice (VIII))
- 去掉java反编译(JD-GUI)生成的源文件中注释
- Web开发108式
- I - The 3n + 1 problem(2.4.2)
- Linux通过该控制台的字符流 实现打印出的字符不同颜色
- Raising Modulo Numbers +快速幂取模算法
- android:clipChildren属性的分析——是否剪裁子View
- 如何优雅的研究 RGSS3 番外(一) ruby 实现的后缀自动机
- linux下搭建SVN服务器完全手册
- J - MANAGER(2.4.5)
- windows 如何查看端口占用情况
- Prim算法
- DataGridView 选中行、列、单元格
- 安卓学习笔记(十)数据存取——安卓第七天