Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. #include<iostream>
  2. #include<algorithm>
  3. #include<vector>
  4. #include<map>
  5. #include <stdio.h>
  6. #include <string.h>
  7.  
  8. using namespace std;
  9. int ans[4][(int)1e7+1];
  10. int m=(int)1e9+7;
  11.  
  12. long long dp(char c,int n){
  13. if(ans[c-'A'][n]!=-1) return ans[c-'A'][n]%m;
  14.  
  15. if(n==0){
  16. if(c=='D'){
  17. ans[c-'A'][n]=1%m;
  18. return ans[c-'A'][n];
  19. }
  20. else {
  21. ans[c-'A'][n]=0%m;
  22. return ans[c-'A'][n]%m;
  23. }
  24. }
  25.  
  26. if(c=='A') {
  27. ans['A'-'A'][n]=(dp('B',n-1)+dp('C',n-1)+dp('D',n-1))%m;
  28. return ans['A'-'A'][n]%m;
  29. }
  30. if(c=='B'){
  31. ans['B'-'A'][n]= (dp('A',n-1)+dp('C',n-1)+dp('D',n-1))%m;
  32. return ans['B'-'A'][n]%m;
  33. }
  34. if(c=='C'){
  35. ans['C'-'A'][n]=(dp('A',n-1)+dp('B',n-1)+dp('D',n-1))%m;
  36. return ans['C'-'A'][n]%m;
  37. }
  38. if(c=='D'){
  39. ans['D'-'A'][n]= (dp('A',n-1)+dp('B',n-1)+dp('C',n-1))%m;
  40. return ans['D'-'A'][n];
  41. }
  42. return 0;
  43. }
  44. int main(){
  45. //freopen("input 1.txt","r",stdin);
  46. memset (ans,-1,sizeof(ans));
  47. int n;
  48. cin>>n;
  49. cout<<dp('D',n)<<endl;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement