Advertisement
Guest User

Untitled

a guest
Sep 21st, 2014
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.05 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <sstream>
  4. #include <cstring>
  5. #include <string>
  6. #include <vector>
  7. #include <algorithm>
  8. #include <cmath>
  9. #include <queue>
  10. #include <utility>
  11. #include <set>
  12. #include <map>
  13.  
  14. #define reset(a , b) memset(a , b , sizeof(a))
  15. #define REP(i , A) for(int i = 0 ; i < (int)A ; i++)
  16.  
  17. using namespace std;
  18.  
  19. const long long MOD = 1000000000000007;
  20. const int MAX = 1000000000;
  21. const int N = 100100;
  22.  
  23. typedef vector<long long> VI;
  24. typedef vector<VI> matrix;
  25.  
  26. VI ans , f(4 , 0);
  27. matrix basic(4 , VI(4 , 0));
  28.  
  29. long long mul(long long A , long long B) {
  30.     if (A <= MAX && B <= MAX) return A * B % MOD;
  31.  
  32.     if (A < B) swap(A , B);
  33.     long long ret = 2 * mul(A / 2 , B) % MOD;
  34.     if (A & 1) ret = (ret + B) % MOD;
  35.     return ret;
  36. }
  37.  
  38. matrix operator *(matrix A , matrix B) {
  39.     matrix ans(4 , VI(4 , 0));
  40.     REP(i , 4) REP(j , 4) REP(k , 4)
  41.         ans[i][j] = (ans[i][j] + mul(A[i][k] , B[k][j])) % MOD;
  42.     return ans;
  43. }
  44.  
  45. VI operator *(VI A , matrix B) {
  46.     VI ans(4 , 0);
  47.     REP(i , 4) REP(j , 4) ans[i] = (ans[i] + mul(A[j] , B[j][i])) % MOD;
  48.     return ans;
  49. }
  50.  
  51. matrix pw(int n) {
  52.     if (n == 1) return basic;
  53.  
  54.     matrix tmp = pw(n / 2);
  55.     tmp = tmp * tmp;
  56.  
  57.     if (n&1) tmp = tmp * basic;
  58.  
  59.     return tmp;
  60. }
  61.  
  62. void print(matrix u) {
  63.     REP(i , 4) {
  64.         REP(j , 4) cout << u[i][j];
  65.         cout << endl;
  66.     }
  67. }
  68.  
  69. int main() {
  70.     //freopen("input.in" , "r" , stdin);
  71.     //freopen("output.out" , "w" , stdout);
  72.  
  73.     basic[1][0] = 1;
  74.     basic[2][1] = 1;
  75.     basic[0][2] = basic[1][2] = basic[2][2] = 1;
  76.     basic[0][3] = basic[1][3] = basic[2][3] = basic[3][3] = 1;
  77.  
  78.     f[0] = 1; f[1] = 2; f[2] = 3 , f[3] = 6;
  79.  
  80.     int T;
  81.     cin >> T;
  82.     while (T--){
  83.         int n;
  84.         cin >> n;
  85.  
  86.         if (n == 1)
  87.             cout << 1 << endl;
  88.         else if (n == 2) cout << 3 << endl;
  89.         else if (n == 3) cout << 6 << endl;
  90.         else {
  91.             //print(pw(n - 3));
  92.             ans = f * pw(n - 3);
  93.             cout << ans[3] << endl;
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement