Advertisement
goshansmails

Untitled

Apr 7th, 2020
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <unordered_map>
  4.  
  5. using namespace std;
  6.  
  7. typedef long long ll;
  8.  
  9. ll BIG_PRIME = 1000000007;
  10.  
  11. ll norm(ll d, ll MOD) {
  12.     return ((d % MOD) + MOD) % MOD;
  13. }
  14.  
  15. ll Madd (ll x, ll y, ll MOD) {
  16.     return norm (norm(x, MOD) + norm (y, MOD), MOD);
  17. }
  18.  
  19. ll Msub (ll x, ll y, ll MOD) {
  20.     return norm (norm(x, MOD) - norm (y, MOD), MOD);
  21. }
  22.  
  23. ll Mmul (ll x, ll y, ll MOD) {
  24.     return norm (norm(x, MOD) * norm (y, MOD), MOD);
  25. }
  26.  
  27. ll fastpow(ll a, ll n, ll MOD) {
  28.     if (n == 0LL) {
  29.         return 1LL;
  30.     }
  31.     if (n % 2 == 1) {
  32.         return (a * fastpow(a, n - 1, MOD)) % MOD;
  33.     }
  34.     ll tmp = fastpow(a, n / 2, MOD);
  35.     return (tmp * tmp) % MOD;
  36. }
  37.  
  38. ll Mdiv(ll a, ll b, ll PMOD) {
  39.     return Mmul(a, fastpow(b, PMOD - 2LL, PMOD), PMOD);
  40. }
  41.  
  42. unordered_map<ll, unordered_map<ll, unordered_map<ll, unordered_map<ll, ll>>>> dp;
  43. vector<ll> factorial;
  44. ll n = 0;
  45. vector<ll> a;
  46. ll total_cards;
  47. vector<ll> first_player_cards_of_type;
  48. vector<ll> second_player_cards_of_type;
  49. vector<ll> min_first_share_for_quantity(11, 0);
  50. vector<ll> count_types_for_quantity(11, 0);
  51.  
  52. ll permutations_first() {
  53.     ll numerator = factorial[total_cards / 2];
  54.     ll denominator = 1;
  55.     for (ll i = 1; i <= n; ++i) {
  56.         denominator = Mmul(denominator, factorial[first_player_cards_of_type[i]], BIG_PRIME);
  57.     }
  58.     return Mdiv(numerator, denominator, BIG_PRIME);
  59. }
  60.  
  61. ll permutations_second() {
  62.     ll numerator = factorial[total_cards / 2];
  63.     ll denominator = 1;
  64.     for (ll i = 1; i <= n; ++i) {
  65.         denominator = Mmul(denominator, factorial[second_player_cards_of_type[i]], BIG_PRIME);
  66.     }
  67.     return Mdiv(numerator, denominator, BIG_PRIME);
  68. }
  69.  
  70. ll total_permutations() {
  71.     ll numerator = factorial[total_cards];
  72.     ll denominator = 1;
  73.     for (ll i = 1; i <= n; ++i) {
  74.         denominator = Mmul(denominator, factorial[a[i]], BIG_PRIME);
  75.     }
  76.     return Mdiv(numerator, denominator, BIG_PRIME);
  77. }
  78.  
  79. ll f(ll idx, ll delta, ll real_delta, ll first_player_total_cards, ll second_player_total_cards) {
  80.     if (idx > n) {
  81.         if (first_player_total_cards == second_player_total_cards &&
  82.             second_player_total_cards == total_cards / 2)
  83.         {
  84.             /*
  85.             // Check delta is OK
  86.             ll real_delta = 0;
  87.             for (ll i = 1; i <= n; ++i) {
  88.                 if (first_player_cards_of_type[i] == a[i]) {
  89.                     ++real_delta;
  90.                 } else if (second_player_cards_of_type[i] == a[i]) {
  91.                     --real_delta;
  92.                 }
  93.             }
  94.             */
  95.             if (delta == real_delta) {
  96.                 vector<vector<ll>> count_for_quantity_and_first_share(11, vector<ll> (11, 0));
  97.                 for (int i = 1; i <= n; ++i) {
  98.                     ++count_for_quantity_and_first_share[a[i]][first_player_cards_of_type[i]];
  99.                 }
  100.  
  101.                 /*
  102.                 for (ll i = 1; i <= n; ++i) {
  103.                     cout << first_player_cards_of_type[i] << " ";
  104.                 }
  105.                 cout << endl;
  106.                  */
  107.  
  108.                 ll numerator = 1;
  109.                 ll denominator = 1;
  110.                 for (ll quantity = 1; quantity <= 10; ++quantity) {
  111.                     numerator = Mmul(
  112.                             numerator,
  113.                             factorial[count_types_for_quantity[quantity]],
  114.                             BIG_PRIME
  115.                     );
  116.                     for (ll first_share = 0; first_share <= quantity; ++first_share) {
  117.                         denominator = Mmul(
  118.                                 denominator,
  119.                                 factorial[count_for_quantity_and_first_share[quantity][first_share]],
  120.                                 BIG_PRIME
  121.                         );
  122.                     }
  123.                 }
  124.  
  125.                 ll number_of_type_permutations = Mdiv(numerator, denominator, BIG_PRIME);
  126.                 ll number_of_permutations = Mmul(permutations_first(), permutations_second(), BIG_PRIME);
  127.  
  128.                 ll res = Mmul(number_of_permutations, number_of_type_permutations, BIG_PRIME);
  129.                 //cout << res << endl;
  130.                 return res;
  131.             }
  132.         }
  133.         return 0;
  134.     }
  135.  
  136.     if (first_player_total_cards > total_cards / 2 || second_player_total_cards > total_cards / 2) {
  137.         return 0;
  138.     }
  139.  
  140.     if (abs(delta - real_delta) > 2 * (n + 1 - idx)) {
  141.         return 0;
  142.     }
  143.  
  144.     /*
  145.     first_player_cards_of_type[idx] = a[idx];
  146.     second_player_cards_of_type[idx] = 0;
  147.     ll first_win = f(
  148.             idx + 1,
  149.             delta - 1,
  150.             real_delta + 1,
  151.             first_player_total_cards + a[idx],
  152.             second_player_total_cards + 0
  153.     );
  154.  
  155.     first_player_cards_of_type[idx] = 0;
  156.     second_player_cards_of_type[idx] = a[idx];
  157.     ll first_lose = f(
  158.             idx + 1,
  159.             delta + 1,
  160.             real_delta - 1,
  161.             first_player_total_cards + 0,
  162.             second_player_total_cards + a[idx]
  163.     );
  164.     */
  165.  
  166.     ll res = 0;
  167.  
  168.     for (ll i = min_first_share_for_quantity[a[idx]]; i <= a[idx]; ++i) {
  169.         ll tmp = min_first_share_for_quantity[a[idx]];
  170.         min_first_share_for_quantity[a[idx]] = i;
  171.         first_player_cards_of_type[idx] = i;
  172.         second_player_cards_of_type[idx] = a[idx] - i;
  173.  
  174.         ll diff_delta = 0;
  175.         ll diff_real_delta = 0;
  176.         if (i == 0) {
  177.             diff_delta = 1;
  178.             diff_real_delta = -1;
  179.         } else if (i == a[idx]) {
  180.             diff_delta = -1;
  181.             diff_real_delta = 1;
  182.         }
  183.  
  184.         res = Madd(
  185.                 res,
  186.                 f(
  187.                         idx + 1,
  188.                         delta + diff_delta,
  189.                         real_delta + diff_real_delta,
  190.                         first_player_total_cards + i,
  191.                         second_player_total_cards + (a[idx] - i)
  192.                 ),
  193.                 BIG_PRIME
  194.         );
  195.         min_first_share_for_quantity[a[idx]] = tmp;
  196.     }
  197.  
  198.     return res;
  199. }
  200.  
  201. int main() {
  202.     cin >> n;
  203.     a.resize(n + 1);
  204.     first_player_cards_of_type.resize(n + 1);
  205.     second_player_cards_of_type.resize(n + 1);
  206.     for (ll i = 1; i <= n; ++i) {
  207.         cin >> a[i];
  208.         total_cards += a[i];
  209.         ++count_types_for_quantity[a[i]];
  210.     }
  211.  
  212.     factorial.resize(501);
  213.     factorial[0] = 1;
  214.     for (ll i = 1; i <= total_cards; ++i) {
  215.         factorial[i] = Mmul(factorial[i - 1], i, BIG_PRIME);
  216.     }
  217.  
  218.     ll numerator = f(1, 0, 0, 0, 0);
  219.     ll denominator = total_permutations();
  220.     cout << "Total card: " << total_cards << endl
  221.          << "Good permutations: " << numerator << endl
  222.          << "Total permutations: " << denominator << endl
  223.          << "Prob: " << Mdiv(numerator, denominator, BIG_PRIME) << endl;
  224.  
  225.     return 0;
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement