正文
增加 33 行删除 14 行
12345678910111213141516171819202122232425262728```cpp#include <bits/stdc++.h>#define int long longusing namespace std;const int kN = 1e6 + 5, mod = 1e9 + 7;int n, m;int dp[kN], sum[kN];int check(int x) {int r = x - 1, l = max(x - m - 1, 0ll);return sum[r] - sum[l];}signed main() {cin >> n >> m;for (int i = 1; i <= n; i++) {dp[i] = check(i) % mod;if(i <= m) {dp[i] ++;}sum[i] = (sum[i - 1] + dp[i]) % mod;}if(dp[n] < 0) {dp[n] += mod;}cout << dp[n];return 0;}```1234567891011121314151617181920212223242526272829303132333435363738394041424344454647```cpp#include <bits/stdc++.h>using namespace std;const int kN = 1e4 + 5;int n, m;string ans;int a[kN], b[kN];bool dp[kN][kN];void print(int x, int y) {if (x == 0) {return;}if (y >= a[x] && dp[x - 1][y - a[x]] == 1) {print(x - 1, y - a[x]);ans += 'H';}else if (y >= b[x] && dp[x - 1][y - b[x]] == 1) {print(x - 1, y - b[x]);ans += 'T';}}int main() {cin >> n >> m;for (int i = 1; i <= n; i++) {cin >> a[i] >> b[i];}dp[0][0] = 1;for (int i = 1; i <= n; i++) {for (int j = 0; j <= m; j++) {if(dp[i - 1][j] == 1) {dp[i][j + a[i]] = 1;dp[i][j + b[i]] = 1;}}}if(dp[n][m] == 1) {cout << "Yes\n";print(n, m);cout << ans;}else {cout << "No";}return 0;}```