Advertisement
RaFiN_

cf 403D

Aug 25th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. /* cf 403D */
  2. /* differences between any pairs should be distinct..so instead of k pairs we can assume k distinct numbers..bi - ai = ci..sum of all ci <= n..why ? coz otherwise we have to take a pair that is greater than 1000... as 1 + 2 +....+ 45 >1000..
  3. so if k > 44 then ans will b zero...
  4.  
  5. we will recursively iterate all numbers and try to take how many ways are there to divide i into k parts..here i is from 1 to n...but now the most interesting part comes here..we actually can increase the difference between any segments...but sum of all should b n...so stars and bars comes here...
  6. */
  7.  
  8. ll f[500];int dp[1001][50][1001];ll ncr[1001][50],ans[1001][50];
  9.  
  10. ll NCR(int n,int r){
  11.     if(n==r)return 1;
  12.     if(r==1)return n;
  13.     if(n<r)return 0;
  14.     if(ncr[n][r])return ncr[n][r];
  15.     return ncr[n][r] =( NCR(n-1,r-1) + NCR(n-1,r) )%mod;
  16. }
  17.  
  18.  
  19. void pre(){
  20.     f[0] = 1;
  21.     for(int i = 1;i<450;i++)f[i] = (f[i-1] * i )%mod;
  22. }
  23.  
  24. ll stars_and_bars(int n,int k){
  25.     return NCR(n+k-1,k-1);
  26. }
  27.  
  28. int giveres(int n,int k,int p){
  29.     if(n==0&&k==0)return 1;
  30.     if(n==0||k==0||p>n)return 0;
  31.     int &ret = dp[n][k][p];
  32.     if(ret!=-1)return ret;
  33.     ret = 0;
  34.     ret+=giveres(n-p,k-1,p+1);
  35.     ret %= mod;
  36.     ret+=giveres(n,k,p+1);
  37.     ret %= mod;
  38.     return ret;
  39. }
  40.  
  41. ll solve(int n,int k){
  42.     if(ans[n][k]!=-1)return ans[n][k];
  43.     ll ret = 0;
  44.     for(int i = n;i>=1;i--){
  45.         ret += ((ll)giveres(i,k,1) * stars_and_bars(n-i,k+1) )%mod;
  46.         ret %= mod;
  47.     }
  48.     return ans[n][k] = (ret * f[k])%mod;
  49. }
  50.  
  51.  
  52. int main()
  53. {
  54.     booster()
  55.    // read("input.txt");
  56.     pre();
  57.     mem(dp,-1);
  58.     mem(ans,-1);
  59.     int t;
  60.     cin>>t;
  61.     while(t--){
  62.         int n,k;
  63.         cin>>n>>k;
  64.         if(k>44)cout<<0<<endl;
  65.         else
  66.             cout<<solve(n,k)<<endl;
  67.     }
  68.  
  69.  
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement