Given N and K we have to find the number of partitions of N into at most K parts modulo m.
I was wondering if there was any formula to calculate this type of query.
I know about the recurrence formula pk(n)=pk(n−k)+pk-1(n) also about the ferrers diagram
But this can be used when N and K are small
I need to find for 1 ≤N,K≤ 2∗105
For example:-
p3(5) = 5,14,23,113,122=5
p3(3) = 3,12,111=3
#include<bits/stdc++.h>
usingnamespace std;
#define ll long long
ll pk(ll n,ll k,ll m)
{
if(k==1)
return 1;
if(n<0)
return 0;
return (pk(n-k,k,m)%m+pk(n,k-1,m)%m)%m;
}
int main()
{
ll t,m;
cin>>t>>m;
while(t--)
{
ll n,k;
cin>>n>>k;
cout<<pk(n,k,m)<<endl;
}
return 0;
}
Here is my implementation using the recurrence but it is giving TLE for
large values as time complexity is 2n
I want to create a dp table to find the value something like
work with me a little. I am old and a little slow.
what is b? Your pseudocode reads into english like
"to get dp[index] compute it from n and k and then use b".
the compute from n and k looks sensible. b came out of nowhere and its use is unclear.
if you can explain the b thing and the last like of your computations, we can probably figure it out, though "something like this" to generate a very specific output is a bit disconcerting.
is b just a placeholder, and it really means
dp[n][k] = f(n-k,k)+f(n-1,k-1);
return; //void function, or returns dp, but certainly not a bool function?!
are you sure this table is right?
N=1, K=1, value = 1, but 3,3 gives 3, from the logic
if(n==k)
return 1;
> I want to create a dp table to find the value
your recurrence is p{k,n}=p{k,n−k}+p{k-1,n}
notice that k decrements by 1 every step, but n decrements by k
you don't need the full table as you are skipping rows