title: How to improve this Dynamic programming solution(Optimisation in algorithm)
body: <p><strong>The Problem statement:</strong></p>
  
  <p>Assurance Company of Moving (ACM) is a company of moving things for people. Recently, some schools want to move their computers to another place. So they ask ACM to help them. One school reserves K trucks for moving, and it has N computers to move. In order not to waste the trucks, the school ask ACM to use all the trucks. That is to say, there must be some computers in each truck, and there are no empty trucks. ACM wants to know how many partition shemes exists with moving N computers by K trucks, the ACM ask you to compute the number of different shemes with given N and K. You needn't care with the order. For example N=7,K=3, the the following 3 partition instances are regarded as the same one and should be counted as one sheme: "1 1 5","1 5 1","5 1 1". Each truck can carry almost unlimited computers!!</p>
  
  <p><strong>Save Time</strong> :</p>
  
  <p>You have to count how many sequences a[1..k] exist such that :</p>
  
  <p>1) a[i] + a[2] + .... + a[k] = N such that permutations dont matter</p>
  
  <p>My O(N*K^2) solution (Cannot figure out how to improve on it)</p>
  
  <pre><code>#include&lt;assert.h&gt;
  #include&lt;stdio.h&gt;
  #include&lt;algorithm&gt;
  using namespace std;
  int DP[5001][5001];
  void ini()
  {
      int i,j,k;
      DP[0][0]=1;
      for(k=1;k&lt;=500;k++)
          for(j=1;j&lt;=500;j++)
             for(i=1;i&lt;=500;i++)
              {
                  DP[i][j]+=j&gt;=k?DP[i-1][j-k]:0;
                  DP[i][j]%=1988;
              }
      return ;
  }
  int main()
  {
      ini();
      int N,K,i,j;
      while(1)
      {
          scanf("%d%d",&amp;N,&amp;K);
          if(N==0 &amp;&amp; K==0)
              return 0;
          int i;
          if(DP[K][N]==0)
          {assert(0);}
          printf("%d\n",DP[K][N]);
      }
      return 0;
  }
  </code></pre>
  
  <p>Explanation of my solution DP[i][j] represents the number of ways I can have total j computers using i Trucks only.
  The k represents the  number of computers with which I am dealing with that means I am just avoiding permutations!</p>
  
  <p>How can I improve it to O(N*K)?</p>
  
  <p>Problem constraints </p>
  
  <blockquote>
    <p>N (1&lt;=N&lt;=5000) and K(1&lt;=K&lt;=N)</p>
  </blockquote>
  
  <p>Problem Link: <a href="http://www.spoj.com/problems/COUNT/" rel="nofollow">Problem Spoj</a></p>