TOJ 2931 Raising Modulo Numbers 快速幂
来源:互联网 发布:淘宝开直通车教程 编辑:程序博客网 时间:2024/06/05 16:36
今天和王学长谈人生谈了两个小时后,假装大彻大悟。
(决定,要重新学一遍算法了...
快速幂详解:
对于任何一个整数的模幂运算 a^b%c
对于b我们可以拆成二进制的形式 b=b0+b1*2+b2*2^2+...+bn*2^n
这里我们的b0对应的是b二进制的第一位
那么我们的a^b运算就可以拆解成 a^b0*a^b1*2*...*a^(bn*2^n)
对于b来说,二进制位不是0就是1,那么对于bx为0的项我们的计算结果是1就不用考虑了,我们真正想要的其实是b的非0二进制位
那么假设除去了b的0的二进制位之后我们得到的式子是 a^(bx*2^x)*...*a(bn*2^n)
这里我们再应用我们一开始提到的公式,那么我们的a^b%c运算就可以转化为 (a^(bx*2^x)%c)*...*(a^(bn*2^n)%c)
这样的话,我们就很接近快速幂的本质了
(a^(bx*2^x)%c)*...*(a^(bn*2^n)%c)
我们会发现令
A1=(a^(bx*2^x)%c)
...
An=(a^(bn*2^n)%c)
这样的话,An始终是A(n-1)的平方倍(当然加进去了取模匀速那),依次递推
*模板*
int quick(int a,int b,int c) { int ans=1; //记录结果 a=a%c; //预处理,使得a处于c的数据范围之下 while(b!=0) { if(b&1) ans=(ans*a)%c; //如果b的二进制位不是0,那么我们的结果是要参与运算的 b>>=1; //二进制的移位操作,相当于每次除以2,用二进制看,就是我们不断的遍历b的二进制位 a=(a*a)%c; //不断的加倍 } return ans; }
TOJ 2931 or POJ 1995 Raising Modulo Numbers
Raising Modulo Numbers
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 5415 Accepted: 3157
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
快速幂取模裸模板过的(=゚ω゚)ノ
#include<stdio.h>#include<string.h>#include<string>#include<math.h>#include<iostream>using namespace std;int ans=0,a,b,c; //快速幂裸模板 int quick(int a,int b,int c) { int ans=1; a=a%c; while(b!=0) { if(b&1) ans=(ans*a)%c; b>>=1; a=(a*a)%c; } return ans; } int main() { int t,i,n; scanf("%d",&t); while(t--) { ans=0; scanf("%d%d",&c,&n); for(i=1;i<=n;i++) { scanf("%d%d",&a,&b); ans=(ans+quick(a,b,c))%c; } printf("%d\n",ans); } return 0; }
阅读全文
0 0
- TOJ 2931 Raising Modulo Numbers 快速幂
- Raising Modulo Numbers ---- 快速幂
- POJ1995 Raising Modulo Numbers 快速幂
- POJ1995 Raising Modulo Numbers【整数快速幂】
- POJ 1995 Raising Modulo Numbers【快速幂】
- POJ_1995(快速幂)(Raising Modulo Numbers)
- poj1995 Raising Modulo Numbers【快速幂】
- poj 1995 Raising Modulo Numbers 【快速幂】
- POJ 1995:Raising Modulo Numbers 快速幂
- poj 1995 Raising Modulo Numbers【快速幂】
- poj Raising Modulo Numbers 【快速幂 + 模取余】
- POJ - 1995 Raising Modulo Numbers 【快速幂】
- Raising Modulo Numbers(快速幂)
- |poj 1995|快速幂|Raising Modulo Numbers
- POJ.1995 Raising Modulo Numbers (快速幂)
- POJ 1995 Raising Modulo Numbers 快速幂
- Raising Modulo Numbers 【poj-1995】【快速幂】
- ZOJ2150 Raising Modulo Numbers 快速幂
- 项目
- Resource v. Autowired
- 数据结构与算法--线性表
- 一个简单的递归求目录的程序遇到的问题
- tomcat部署项目
- TOJ 2931 Raising Modulo Numbers 快速幂
- IOS11 光标错位问题
- 版本回退
- 【Android学习】IPC(跨进程通信,Inter-Process Communication)
- java初学Day1
- Go基础
- 利用visual studio2017进行C语言环境的编程
- 主流浏览器及其内核
- 开始写博客