fireLUFFY

CSUMD-CC.cpp

Jul 27th, 2021 (edited)
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.72 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define int long long
  5. #define inf 2e18
  6. int MOD=1000000007;//998244353;
  7. const int nn=1000050;
  8. bool prime[nn]; //array to store precalculated primes till 10^6
  9. void cal_primes(){memset(prime,true,sizeof(prime)); for(int i=2;i<=sqrt(nn);++i){ if(prime[i]==true){ for(int j=i*i;j<=nn;j+=i){prime[j]=false;}}}}
  10.  
  11. int m[2][2]={{2,2},{1,0}};
  12. int f[2][2]={{2,2},{1,0}};
  13.  
  14. void multiply(int A[2][2],int B[2][2])
  15. {
  16.     int x=((A[0][0]*B[0][0])+(A[0][1]*B[1][0]));
  17.     int y=((A[0][0]*B[0][1])+(A[0][1]*B[1][1]));
  18.     int z=((A[1][0]*B[0][0])+(A[1][1]*B[1][0]));
  19.     int w=((A[1][0]*B[0][1])+(A[1][1]*B[1][1]));
  20.  
  21.     A[0][0]=x%MOD;
  22.     A[0][1]=y%MOD;
  23.     A[1][0]=z%MOD;
  24.     A[1][1]=w%MOD;
  25. }
  26.  
  27. void power(int f[2][2],int n)
  28. {
  29.     if(n==0)
  30.     return;
  31.  
  32.     while(n>0)
  33.     {
  34.         if(n&1)
  35.             multiply(f,m);
  36.         multiply(m,m);
  37.         n=(n>>1);
  38.     }
  39. }
  40. void val(int n)
  41. {
  42.     if(n==1)
  43.         return;
  44.     power(f,n-1);
  45. }
  46. void solve(int t)
  47. {
  48.     int testcases=t;
  49.     while(t--)
  50.     {
  51.         //cerr<<"Case #"<<(testcases-t)<<": "<<endl;
  52.         int n;cin>>n;
  53.         f[0][0]=2;f[0][1]=2;f[1][0]=1;f[1][1]=0;
  54.         m[0][0]=2;m[0][1]=2;m[1][0]=1;m[1][1]=0;
  55.  
  56.         if(n<2)
  57.             cout<<1<<endl;
  58.         else if(n==2)
  59.             cout<<3<<endl;
  60.         else
  61.         {
  62.             val(n-2);
  63.             int val=(((3*f[0][0])+(f[0][1]))%MOD);
  64.             cout<<val<<endl;
  65.         }
  66.     }
  67. }
  68.  
  69. main()
  70. {
  71.     auto start=chrono::system_clock::now();
  72.     {
  73.         #ifndef ONLINE_JUDGE
  74.             freopen("input.txt","r",stdin);
  75.             freopen("output.txt","w",stdout);
  76.             freopen("error.txt","w",stderr);
  77.         #endif 
  78.         ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  79.         int t=1;
  80.         cin>>t;
  81.         solve(t);
  82.     }
  83.     auto end=chrono::system_clock::now();
  84.     chrono::duration<double> elapsed=end-start;
  85. //  cout<<endl<<"Time taken: "<<elapsed.count()<<" sec";
  86.     return 0;
  87. }
Add Comment
Please, Sign In to add comment