zoukankan      html  css  js  c++  java
  • 快速幂,就当个模板了

     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
    ",sum);
    54         else
    55         printf("%d
    ",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;
    }

  • 相关阅读:
    Codeforces 543E. Listening to Music
    UOJ #138. 【UER #3】开学前的涂鸦
    bzoj 3569: DZY Loves Chinese II
    bzoj 2428: [HAOI2006]均分数据
    bzoj 4589: Hard Nim
    UOJ #119. 【UR #8】决战圆锥曲线
    spoj5973
    codeforces555E
    poj1275
    bzoj4152
  • 原文地址:https://www.cnblogs.com/Skyxj/p/3187546.html
Copyright © 2011-2022 走看看