Kaidul

Shubham

May 8th, 2014
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.84 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long i64;
  6. #define Max 45
  7.  
  8. /*
  9.     dp[i][j]: sum of all mask has j bit set from 1 to i
  10.     dp[i][j] = dp[i-1][j-1] + S[i]
  11. */
  12. int n, m, P[Max][Max], nball[Max], color[Max], cost[Max];
  13. i64 dp[Max][Max], Cnt[Max][Max], S[Max];
  14.  
  15. int main(void) {
  16. #ifndef ONLINE_JUDGE
  17.     freopen("input.txt", "r", stdin);
  18. #endif
  19.     int tcase;
  20.     scanf("%d", &tcase);
  21.     while(tcase--) {
  22.         scanf("%d %d", &n, &m);
  23.         memset(nball, 0, sizeof nball);
  24.         for(int i = 0; i < n; ++i) {
  25.             scanf("%d %d", color + i, cost + i);
  26.             // P[i][j] = cost of jth ball of ith color
  27.             // nball[i] = number of ball wuth color i
  28.             P[ color[i] ][nball[ color[i] ]++] = cost[i];
  29.         }
  30.         for(int i = 1; i <= 40; ++i) {
  31.             S[i] = 0;
  32.             for(int j = 0; j < nball[i]; ++j) {
  33.                 S[i] += P[i][j] * (1LL << (nball[i] - 1));
  34.             }
  35.         }
  36.         memset(Cnt, 0, sizeof Cnt);
  37.         memset(dp, 0, sizeof dp);
  38.         Cnt[0][0] = 1;
  39.         for(int i = 1; i <= 40; ++i) {
  40.             Cnt[i][0] = 1;
  41.             for(int j = 1; j <= i; ++j) {
  42.                 if(nball[i] > 0) {
  43.                     dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1] * ((1LL << nball[i]) - 1) + Cnt[i - 1][j - 1] * S[i];
  44.                     Cnt[i][j] = Cnt[i - 1][j] + Cnt[i - 1][j - 1] * ((1LL << nball[i]) - 1);
  45.                 } else {
  46.                     dp[i][j] = dp[i - 1][j];
  47.                     Cnt[i][j] = Cnt[i - 1][j];
  48.                 }
  49.             }
  50.         }
  51.         double total_sum = 0, total_cnt = 0, res;
  52.         for(int j = m; j <= n; ++j) {
  53.             total_sum += dp[40][j];
  54.             total_cnt += Cnt[40][j];
  55.         }
  56.         res = total_sum / total_cnt;
  57.         printf("%.9lf\n", res);
  58.     }
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment