Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- #include <vector>
- using namespace std;
- #define forn(i, n) for (int i = 0; i < n; ++i)
- typedef long long LL;
- const LL p = 1e+9 + 7;
- // a *= b
- void mxmul(LL **a, LL **b)
- {
- LL t[4][4];
- forn(i, 4)
- forn(j, 4)
- {
- t[i][j] = 0;
- forn(k, 4)
- t[i][j] += ( (a[i][k] % p) * (b[k][j] % p) ) % p;
- }
- memcpy(a, t, sizeof t);
- }
- // r = a ^ n
- void mxbinpow(LL **r, LL **a, LL n)
- {
- forn(i, 4)
- forn(j, 4)
- r[i][j] = i - j ? 0 : 1;
- while (n)
- {
- if (n & 1)
- mxmul(r, a);
- mxmul(a, a);
- n >>= 1;
- }
- }
- int main()
- {
- LL n; cin >> n;
- LL r[4][4], a[4][4];
- forn(i, 4)
- forn(j, 4)
- a[i][j] = i - j ? 1 : 0;
- mxbinpow(r, a, n);
- cout << r[3][3] << endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment