My issue
Why is it showing partially correct answer
My code
#include <bits/stdc++.h>
using namespace std;
int const M=1e9+7;
int const N=1e6+1;
void solve()
{int n,x;
cin >>n >>x;
if(n%2==1)
{cout <<0<<"\n";}
else
{vector<int> dp(n+1,0);// length of ribbon is i where each piece cut is even and and length of each piece is at most l
for(int i=2;i<=n;i+=2)
{if(x>=i)
{dp[i]=1;}
for(int j=2;j<=i-2;j+=2)
{dp[i]=(dp[i]+dp[j])%M;}}
cout <<dp[n]<<"\n";}}
int32_t main()
{
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
srand(chrono::high_resolution_clock::now().time_since_epoch().count());
int t = 1;
cin >> t;
while (t--)
solve();
return 0;
}
Learning course: Dynamic programming
Problem Link: CodeChef: Practical coding for everyone
Hello,
Your question seems like the problem involves cutting a ribbon of even length n into pieces of even length, and you’re allowed to cut pieces of at most length x. The goal is to count the number of ways to cut the ribbon.
Here’s a simplified explanation of your code:
#include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7;
const int N = 1e6 + 1;
void solve() {
int n, x;
cin >> n >> x;
if (n % 2 == 1) {
cout << 0 << "\n"; // If n is odd, no valid cuts are possible
} else {
vector<int> dp(n + 1, 0);
for (int i = 2; i <= n; i += 2) {
if (x >= i) {
dp[i] = 1; // Initialize base case: if x is greater than or equal to i, there's one way to cut the ribbon
}
for (int j = 2; j <= i - 2; j += 2) {
dp[i] = (dp[i] + dp[j]) % M; // Update the number of ways to cut the ribbon of length i
}
}
cout << dp[n] << "\n";
}
}
int32_t main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
srand(chrono::high_resolution_clock::now().time_since_epoch().count());
int t = 1;
cin >> t;
while (t--) solve();
return 0;
}
This code checks if the length of the ribbon is odd; if so, it prints 0 because no valid cuts are possible. I also check this : https://www.codechef.com/learn/course/dynamic-programmingcpq Otherwise, it uses dynamic programming to calculate the number of ways to cut the ribbon of even length n using pieces of even length, each at most x. The result is printed after processing all test cases.
I hope this will help you.
Thanks!