-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c5b7281
commit e7b0ac7
Showing
2 changed files
with
49 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# 050 - Stair Jump (★3) | ||
|
||
## 解答 | ||
|
||
```cpp | ||
#include <iostream> | ||
#include <vector> | ||
|
||
int main() | ||
{ | ||
// N 段の階段, 一歩で 1 段か L 段上る | ||
int N, L; | ||
std::cin >> N >> L; | ||
|
||
// 0 段目から N 段目までの通り数を記録 | ||
std::vector<int> dp(N + 1); | ||
|
||
// 0 段目では 1 通り | ||
dp[0] = 1; | ||
|
||
// i 段目 (i が 1~(L-1)) のときは | ||
// (i-1) 段目までの通り数 | ||
for (int i = 1; i < L; ++i) | ||
{ | ||
dp[i] = dp[i - 1]; | ||
} | ||
|
||
// i 段目 (i が L~N) のときは | ||
// (i-1) 段目までの通り数 + (i-L) 段目までの通り数 | ||
for (int i = L; i < (N + 1); ++i) | ||
{ | ||
dp[i] = (dp[i - 1] + dp[i - L]) % 1'000'000'007; | ||
} | ||
|
||
// 例: N = 5, L = 3 のとき | ||
// ___ | ||
// ___|[4] | ||
// ___|[3] + | ||
// ___|[2] + [2] | ||
// ___|[1] + [1] | ||
// ___|[0] [0] | ||
// = 1 = 1 = 1 = 2 = 3 = 4 | ||
// [0] [1] [2] [3] [4] [5] | ||
|
||
// 最終段までの通り数を出力 | ||
std::cout << dp.back() << '\n'; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters