• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
FightingForWorldFinal
博客园    首页    新随笔    联系   管理    订阅  订阅

快速幂,就当个模板了

 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;
}

posted @ 2013-07-13 08:11  Sky-J  阅读(230)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3