#define MAX 1000000007 #include #include typedef struct Node{ int step; struct Node* child[3];//n進む時その子は[n-1] } Node; int count(int n, int next, Node* root, int table[][3]){ //rootからnext進んだ時、あとに残された可能性の数 Node* node; root->child[next-1] = node; node->step = root->step + next; if(node->step == n){//next進むとぴったりの時 return 1; } else if(node->step > n){//行き過ぎのとき return 0; } else{//まだのとき int a = 0; int next1, next2; if(next == 1){ next1 = 2; next2 = 3; } else if(next == 2){ next1 = 1; next2 = 3; } else{ next1 = 1; next2 = 2; } if(table[node->step][next1] >= 0){//表の有無 a += table[node->step][next1]; } else{ a += count(n, next1, node, table); } if(table[node->step][next2] >= 0){ a += table[node->step][next2]; } else{ a += count(n, next2, node, table); } table[root->step][next] = a; return a % MAX; } } #include int main(void){ int n; scanf("%d", &n); int table[n][3]; //現在j段目、次進むstep数はkのとき、table[j][k] for(int i = 0; i < n; i++){ for(int j = 0; j < 3; j++){ table[i][j] = -1; } } Node* root; root->step = 0; int ans = 0; ans += count(n, 1, root, table); ans += count(n, 2, root, table); ans += count(n, 3, root, table); ans %= MAX; return 0; }