求余

- 求余
序号: #107难度:困难时间限制:1000ms内存限制:10M

描述

f(N) = 3^0 + 3^1 + ... + 3^N 求 f(N) 除以 1000000007 的余数

输入

N(1 <= N <= 10^9 + 7)的值

输出

f(N) 除以 1000000007 的余数

输入样例

10

输出样例

88573


是我太年轻,竟然没注意到细节。

等比数列求和公式
  • formula
  • formula
 
   
 1 #include <bits/stdc++.h>
 2 #define LL long long int
 3 #define MOD 1000000007 
 4 using namespace std;
 5 
 6 LL quick(LL a,LL x){
 7     LL r = 1;
 8     while(x>0){
 9         if (x&1){
10             r = (r*a)%MOD;
11         }
12         a = (a*a)%MOD;
13         x>>=1;  
14     }
15     return r%MOD;
16 }
17 
18 LL n;
19 int main(){
20     while(cin >> n){
21         LL ans = quick(3, n + 1) - 1;
22         if (ans%2 == 0)
23             cout << ans/2 << endl;
24         else
25             cout << (ans+MOD)/2 << endl;
26     }
27     return 0;
28 }

猜你喜欢

转载自www.cnblogs.com/zllwxm123/p/10317519.html