51Nod 1013 3的幂的和(快速幂+逆元)

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <string>
 4 
 5 #define MOD 1000000007
 6 using namespace std;
 7 
 8 long long quick_pow(long long a, long long b, long long c){
 9     long long ans = 1;
10     a = a%c;
11     while (b > 0){
12         if (b % 2 == 1){
13             ans = (ans*a) % c;
14         }
15         b = b / 2;
16         a = (a*a) % c;
17     }
18     return ans;
19 }
20 
21 int main(){
22     ios::sync_with_stdio(false);
23     /*
24     freopen("in.txt", "r", stdin);
25     freopen("out.txt", "w", stdout);
26     */
27     long long x;
28     while (cin >> x){
29         x++;
30         long long ans = quick_pow(3, x, MOD);
31         ans--;
32         ans = ans * 500000004 % MOD;
33         cout << ans << endl;
34     }
35     //fclose(stdin);
36     //fclose(stdout);
37     //system("pause");
38     return 0;
39 }

 

posted @ 2018-04-01 20:16  ouyang_wsgwz  阅读(155)  评论(0编辑  收藏  举报