fork(2) download
  1. #include <bits/stdc++.h>
  2.  
  3. #define MOD 1000000007
  4.  
  5. using namespace std;
  6.  
  7. long long dp[3][3],temp[3][3];
  8.  
  9. int main()
  10. {
  11. int t;
  12. scanf("%d",&t);
  13. while (t --) {
  14. int n,i,j,k,cur;
  15. long long ans;
  16. scanf("%d",&n);
  17. if (n == 1) {
  18. ans = 3;
  19. printf("%lld\n",ans);
  20. continue;
  21. }
  22. for (i = 0; i < 3; i++) {
  23. for (j = 0; j < 3; j++) {
  24. dp[i][j] = 1;
  25. }
  26. }
  27. for (i = 3; i <= n; i++) {
  28. for (j = 0; j < 3; j++) {
  29. for (k = 0; k < 3; k++) {
  30. temp[j][k] = 0;
  31. }
  32. }
  33. for (j = 0; j < 3; j++) {
  34. for (k = 0; k < 3; k++) {
  35. for (cur = 0; cur < 3; cur++) {
  36. temp[k][cur] = (temp[k][cur] + dp[j][k]) % MOD;
  37. }
  38. }
  39. }
  40. temp[1][2] = (temp[1][2] - dp[0][1] + MOD) % MOD;
  41. for (j = 0; j < 3; j++) {
  42. for (k = 0; k < 3; k++) {
  43. dp[j][k] = temp[j][k];
  44. }
  45. }
  46. }
  47. ans = 0;
  48. for (i = 0; i < 3; i++) {
  49. for (j = 0; j < 3; j++) {
  50. ans = (ans + dp[i][j]) % MOD;
  51. }
  52. }
  53. printf("%lld\n",ans);
  54. }
  55. return 0;
  56. }
Time limit exceeded #stdin #stdout 5s 3140KB
stdin
Standard input is empty
stdout
Standard output is empty