快速幂取模模板 && 51nod 1013 3的幂的和

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <vector>
 5 #include <cstring>
 6 #include <string>
 7 #include <algorithm>
 8 #include <string>
 9 #include <set>
10 #include <functional>
11 #include <numeric>
12 #include <sstream>
13 #include <stack>
14 #include <map>
15 #include <queue>
16 #pragma comment(linker, "/STACK:102400000,102400000")
17 #define CL(arr, val)    memset(arr, val, sizeof(arr))
18 
19 #define ll long long
20 #define inf 0x7f7f7f7f
21 #define lc l,m,rt<<1
22 #define rc m + 1,r,rt<<1|1
23 #define pi acos(-1.0)
24 
25 #define L(x)    (x) << 1
26 #define R(x)    (x) << 1 | 1
27 #define MID(l, r)   (l + r) >> 1
28 #define Min(x, y)   (x) < (y) ? (x) : (y)
29 #define Max(x, y)   (x) < (y) ? (y) : (x)
30 #define lson l,m,rt<<1
31 #define rson m+1,r,rt<<1|1
32 #define E(x)        (1 << (x))
33 #define iabs(x)     (x) < 0 ? -(x) : (x)
34 #define OUT(x)  printf("%I64d
", x)
35 #define lowbit(x)   (x)&(-x)
36 #define Read()  freopen("a.txt", "r", stdin)
37 #define Write() freopen("b.txt", "w", stdout);
38 #define maxn 100010
39 #define mod 1000000007
40 using namespace std;
41 
42 ll quick_mod(ll a,ll b,ll c)
43 {
44     ll ans=1;
45     while(b)
46     {
47         if(b&1)
48         {
49             ans=(ans*a)%c;
50             b--;
51         }
52         b/=2;
53         a=(a*a)%c;
54     }
55     return ans;
56 }
57 int main()
58 {
59    // freopen("a.txt","r",stdin);
60     ll a,b,c;
61     scanf("%lld%lld%lld",&a,&b,&c);
62     printf("%lld
",quick_mod(a,b,c));
63     return 0;
64 }

 http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1013

 这个题需要用到两次二分:第一次是在求3^n次方,第二次是求3^0+3^1+...3^n.

第一次二分就是上面的快速幂,第二次二分:

加入n=5       3^1+3^2+3^3+3^4+3^5 = 3^1+3^2 + 3^2*(3^1+3^2) +3^5  

这样就可以递归求解了,跟矩阵快速幂类似。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <vector>
 5 #include <cstring>
 6 #include <string>
 7 #include <algorithm>
 8 #include <string>
 9 #include <set>
10 #include <functional>
11 #include <numeric>
12 #include <sstream>
13 #include <stack>
14 #include <map>
15 #include <queue>
16 #pragma comment(linker, "/STACK:102400000,102400000")
17 #define CL(arr, val)    memset(arr, val, sizeof(arr))
18 
19 #define ll long long
20 #define inf 0x7f7f7f7f
21 #define lc l,m,rt<<1
22 #define rc m + 1,r,rt<<1|1
23 #define pi acos(-1.0)
24 
25 #define L(x)    (x) << 1
26 #define R(x)    (x) << 1 | 1
27 #define MID(l, r)   (l + r) >> 1
28 #define Min(x, y)   (x) < (y) ? (x) : (y)
29 #define Max(x, y)   (x) < (y) ? (y) : (x)
30 #define lson l,m,rt<<1
31 #define rson m+1,r,rt<<1|1
32 #define E(x)        (1 << (x))
33 #define iabs(x)     (x) < 0 ? -(x) : (x)
34 #define OUT(x)  printf("%I64d
", x)
35 #define lowbit(x)   (x)&(-x)
36 #define Read()  freopen("a.txt", "r", stdin)
37 #define Write() freopen("b.txt", "w", stdout);
38 #define maxn 100010
39 #define mod 1000000007
40 using namespace std;
41 
42 ll power(ll a,ll b)
43 {
44     ll ans=1;
45     while(b)
46     {
47         if(b&1)
48         {
49             ans=(ans*a)%mod;
50             b--;
51         }
52         b/=2;
53         a=(a*a)%mod;
54     }
55     return ans;
56 }
57 ll c;
58 ll sum(ll a,ll k)
59 {
60     if(k == 1) return a;  
61     c=sum(a,k>>1);
62     ll ans=(c+c*power(a,(k>>1)))%mod;  //每次 计算出两项 
63     if(k&1) ans=(ans+power(a,k))%mod;  //是奇数的话要加上最后那一项
64     return ans;
65 }
66 int main()
67 {
68    //freopen("a.txt","r",stdin);
69     ll n;
70     scanf("%lld",&n);
71     //printf("%lld
",(power(3,20)-1)/2%mod);
72     printf("%lld
",((sum(3,n)%mod))+1);
73     return 0;
74 }
原文地址:https://www.cnblogs.com/nowandforever/p/4581900.html