快速幂,就当个模板了

it2024-10-13  19

1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <cstdlib> 6 #include <algorithm> 7 #include <vector> 8 #include <stack> 9 #include <queue> 10 #include<cassert> 11 #include<set> 12 using namespace std ; 13 #ifdef DeBUG 14 #define bug assert 15 #else 16 #define bug // 17 #endif 18 int pow3(int a, int b) 19 { 20 int r = 1, base = a; 21 while(b != 0) 22 { 23 if(b & 1) 24 r =(r* base)%100; 25 base =(base* base)%100; 26 b >>= 1; 27 } 28 return r; 29 } 30 31 int main() 32 { 33 #ifdef DeBUG 34 freopen("C:\\Users\\Sky\\Desktop\\1.in","r",stdin); 35 #endif 36 37 int n; 38 int i,j,k; 39 int t; 40 int m; 41 int sum; 42 scanf("%d",&t); 43 while(t--) 44 { 45 sum=0; 46 int now=0; 47 scanf("%d%d",&n,&m); 48 for(i=1;i<=n;i++) 49 { 50 sum=(sum+pow3(i,m))%100; 51 } 52 if(sum<10) 53 printf("0%d\n",sum); 54 else 55 printf("%d\n",sum); 56 } 57 return 0; 58 } View Code

位操作版

int pow3(int a, int b){ int r = 1, base = a; while(b != 0) {  if(b & 1)   r *= base;  base *= base;  b >>= 1; } return r;}

 

int pow2(int a, int b){ int r = 1, base = a; while(b != 0) {  if(b % 2)   r *= base;  base *= base;  b /= 2; } return r;}

转载于:https://www.cnblogs.com/Skyxj/p/3187546.html

相关资源:c++快速幂模板
最新回复(0)