画图找规律
f[1] = 2
f[n] = f[n-1] + 1 + f[n-1] + 1 + f[n-1]
f[n] = 3^n - 1 快速幂
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| #include <iostream> #include <cmath> #include <cstdio>
using namespace std; typedef long long ll;
ll quick_mod(ll n, ll k, ll mod){ ll ret = 1; while (k){ if (k&1) ret = (ret * n) % mod; k >>= 1; n = (n*n) % mod; } return ret; }
int main(){ ll n,m; cin >> n >> m; cout << (quick_mod(3,n,m) - 1 + m) % m<< endl; return 0; }
|