poj Raising Modulo Numbers 快速幂模板(取膜)
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 8606 | Accepted: 5253 |
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
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
1 #include<stdio.h> 2 int read(); 3 int poww(int,int,int); 4 int n; 5 int main(){ 6 n=read(); 7 for(int i=1;i<=n;i++){ 8 int M=read(),H=read(); 9 int an=0; 10 for(int j=1;j<=H;j++){ 11 int a=read(),b=read(); 12 an+=poww(a,b,M); 13 an%=M; 14 } 15 printf("%d\n",an); 16 } 17 return 0; 18 } 19 int poww(int a,int b,int M){ 20 int base=a%M,an=1; 21 while(b){ 22 if(b&1) an=an%M*(base%=M); 23 base=base%M*(base%M); 24 b>>=1; 25 } 26 return an%M; 27 } 28 int read(){ 29 int ans=0,f=1;char c=getchar(); 30 while(\'0\'>c||c>\'9\'){if(c==\'-\')f=-1;c=getchar();} 31 while(\'0\'<=c&&c<=\'9\')ans=ans*10+c-48,c=getchar();return ans*f; 32 }
快速幂 取膜