feat: alternative AC solution and TLE solution implemented, also wrote the tutorial for the problem and improved its description
This commit is contained in:
38
flowers/src/TLE.cpp
Normal file
38
flowers/src/TLE.cpp
Normal file
@@ -0,0 +1,38 @@
|
||||
#include <bits/stdc++.h>
|
||||
typedef long long ll;
|
||||
using namespace std;
|
||||
|
||||
int N, M;
|
||||
|
||||
const int MOD = 1e9 + 7;
|
||||
|
||||
ll flowers(int last, int count = 1, int size = 1) {
|
||||
if (size == N) return 1;
|
||||
|
||||
ll ans = 0;
|
||||
/*
|
||||
if sequences ends with X we have to cases:
|
||||
if count(X) < M we can continue the sequence by appending another X in its end
|
||||
we can also start and entirely new sequence starting with Y.
|
||||
*/
|
||||
|
||||
if (last == 0) {
|
||||
if (count < M)
|
||||
ans += flowers(0, count + 1, size + 1);
|
||||
ans += flowers(1, 1, size + 1);
|
||||
} else {
|
||||
if (count < M)
|
||||
ans += flowers(1, count + 1, size + 1);
|
||||
ans += flowers(0, 1, size + 1);
|
||||
}
|
||||
|
||||
return ans % MOD;
|
||||
}
|
||||
|
||||
int main(){
|
||||
cin >> N >> M;
|
||||
|
||||
cout << (flowers(0) + flowers(1)) % MOD << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
45
flowers/src/alternative_ac.cpp
Normal file
45
flowers/src/alternative_ac.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#include <bits/stdc++.h>
|
||||
typedef long long ll;
|
||||
using namespace std;
|
||||
|
||||
int N, M;
|
||||
|
||||
const int MOD = 1e9 + 7;
|
||||
|
||||
ll memo[10001][1001][2];
|
||||
|
||||
ll flowers(int last, int count = 1, int size = 1) {
|
||||
if (size == N) return 1;
|
||||
/*
|
||||
if sequences ends with X we have to cases:
|
||||
if count(X) < M we can continue the sequence by appending another X in its end
|
||||
we can also start and entirely new sequence starting with Y.
|
||||
*/
|
||||
if (memo[size][count][last] == 0) {
|
||||
if (last == 0) {
|
||||
if (count < M)
|
||||
memo[size][count][0] += flowers(0, count + 1, size + 1);
|
||||
memo[size][count][0] += flowers(1, 1, size + 1);
|
||||
} else {
|
||||
if (count < M)
|
||||
memo[size][count][1] += flowers(1, count + 1, size + 1);
|
||||
memo[size][count][1] += flowers(0, 1, size + 1);
|
||||
}
|
||||
}
|
||||
|
||||
return memo[size][count][last] %= MOD;
|
||||
}
|
||||
|
||||
int main(){
|
||||
cin >> N >> M;
|
||||
|
||||
for (int i = 0; i <= 10000; i++) {
|
||||
for (int j = 0; j <= 1000; j++) {
|
||||
memo[i][j][0] = memo[i][j][1] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
cout << (flowers(0) + flowers(1)) % MOD << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -24,7 +24,7 @@ vector<string> generate_sample_tests() {
|
||||
vector<string> tests;
|
||||
tests.push_back(output_tc(1, 1));
|
||||
tests.push_back(output_tc(2, 2));
|
||||
tests.push_back(output_tc(2, 3));
|
||||
tests.push_back(output_tc(2, 1));
|
||||
return tests;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user