Advertisement
Guest User

HANOI07

a guest
May 26th, 2015
725
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #define ll long long
  5. #define mp make_pair
  6. using namespace std;
  7. int N,H,M;
  8. const int maxn = 2500;
  9. const int maxh = 60;
  10. const int maxm = 70;
  11. ll dp[maxn+1][maxh+1][maxm+1];
  12. /* ll solve(ll ini,ll h,ll top){
  13.  
  14. //each step there are two options --> top +- 1
  15. if (h>H || top<=0 || ini>N)
  16. return (dp[ini][h][top]=0);
  17. else if (h==H && top>0 && ini<=N)
  18. return (dp[ini][h][top]=1);
  19. else
  20. return (dp[ini][h][top] = solve(ini+1+top,h+1,top+1) + solve(ini-1+top,h+1,top-1));
  21. } */
  22. ll solve1(){
  23.  
  24. for (int i=0;i<maxn;i++)
  25. for (int j=0;j<maxh;j++)
  26. for (int k=0;k<maxm;k++)
  27. dp[i][j][k] = 0LL;
  28.  
  29. //dp[i][j][k]--> total of i blocks of height j and top block being k
  30.  
  31. dp[M][1][M] = 1;//initially 1st level has m blocks
  32.  
  33. ll lim = min(maxn,N);
  34. for (int i=1; i <= lim ; i++){
  35. for (int j=2; j <= H ; j++){
  36. for (int k = 1 ; k <= maxm ; k++){
  37.  
  38. if ( (i >= k) && (k+1<=70) )
  39. dp[i][j][k] += dp[i-k][j-1][k+1];
  40. if ( (i >= k) && (k-1>=0) )
  41. dp[i][j][k] += dp[i-k][j-1][k-1];
  42.  
  43. //cout<<i<<" "<<j<<" "<<k<<" "<<dp[i][j][k]<<"\n";
  44. }
  45. }
  46. }
  47. ll ans = 0;
  48.  
  49. for (int i=1 ; i <= lim ; i++)
  50. for (int j = 1; j <= 70 ; j++)
  51. ans = ans + dp[i][H][j];
  52.  
  53. return ans;
  54. }
  55. int main(){
  56. //make a tower out of n cubes such that height is h and bottom has m cubes
  57. //thoughts- dp, think of a recurrence
  58. int t;
  59. scanf("%d",&t);
  60. while(t--){
  61. scanf("%d %d %d",&N,&H,&M);
  62. ll f =solve1();
  63. printf("%lld\n",f);
  64. }
  65. return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement