Advertisement
shamiul93

1326 - Race

Mar 20th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1. /*
  2. @author - Rumman BUET CSE'15
  3. Problem - 1326 - Race
  4. Idea - DP. dekha lagbe je amar haate je koyta ache baki, ekhon er position e ek i sathe koyjon ke ei place ta deya jay? combination kore hishab korbo. but
  5. ek i remaining er jonno value ta kokhno change hobe na. so, amra dp[] array ta barbar initialize korbo na. ekbar korlei enough
  6. karon, array position er upor ashole depend kore na result ta.
  7. */
  8. #include <bits/stdc++.h>
  9. #include<vector>
  10. #define ll                                      long long int
  11. #define fi                                      freopen("in.txt", "r", stdin)
  12. #define fo                                      freopen("out.txt", "w", stdout)
  13. #define m0(a) memset(a , 0 , sizeof(a))
  14. #define m1(a) memset(a , -1 , sizeof(a))
  15. #define inf LLONG_MAX
  16. #define min3(a,b,c) min(a,min(b,c))
  17. #define max3(a,b,c) max(a,max(b,c))
  18. #define ones(mask)  __builtin_popcount(mask) /// __builtin_popcount : it's a built in function of GCC. Finds out the numbers of 1 in binary representation.
  19. #define mx 150000
  20. #define mod 10056
  21.  
  22. using namespace std;
  23.  
  24. ll N ;
  25. ll dp[1005] ;
  26. ll combination[1005][1005] ;
  27.  
  28. ll comb()
  29. {
  30.     for(ll i = 0 ; i < 1005 ; i++)
  31.     {
  32.         combination[i][0] = 1 ;
  33.         combination[i][i] = 1 ;
  34.         combination[i][1] = i ;
  35.     }
  36.  
  37.     for(ll n = 3 ; n < 1005 ; n++)
  38.     {
  39.         for(ll r = 2 ; r < n ; r++)
  40.         {
  41.             combination[n][r] = (combination[n-1][r] % mod + combination[n-1][r-1] % mod ) % mod;
  42.         }
  43.     }
  44. }
  45.  
  46.  
  47. ll solve(ll remaining)
  48. {
  49.     if(remaining == 0)
  50.         return 1 ;
  51.  
  52.     if(dp[remaining] != -1)
  53.         return dp[remaining] ;
  54.  
  55.     ll ans1, ans = 0 ;
  56.  
  57.  
  58.     for(ll myUse = 1 ; myUse <= remaining ; myUse++)
  59.     {
  60.         ll next = remaining - myUse ;
  61.  
  62.         ans1 = ((combination[remaining][myUse]) * (solve(next) % mod))  % mod ;
  63.  
  64.         ans = (ans % mod + ans1 % mod) % mod;
  65.     }
  66.  
  67.     return dp[remaining] = ans ;
  68. }
  69.  
  70. int main()
  71. {
  72. //    fi;
  73. //    fo;
  74.     m1(combination) ;
  75.     comb() ;
  76.     ll T, t = 0 ;
  77.     scanf("%lld",&T) ;
  78.  
  79.     m1(dp); /** ekbar korlei chole.*/
  80.  
  81.     while(T--)
  82.     {
  83.         t++ ;
  84.         cin >> N ;
  85.         printf("Case %lld: ",t) ;
  86.         ll ans ;
  87.         ans = solve(N) ;
  88.         cout << ans << endl ;
  89.     }
  90.     return 0 ;
  91. }
  92.  
  93.  
  94.  
  95.  
  96. /*
  97.  
  98. 3
  99. 1
  100. 2
  101. 3
  102.  
  103. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement