Title Link: HDOJ-5160
Problem analysis
At first glance, it is required to count the contribution of all the different permutations to the answer. Well... No idea at all.
However, if we consider each number separately, it is much easier to calculate the contribution of this number to the total answer.
For a digital AI, there are ni. A number larger than it has p, and a number smaller than it has Q. All the numbers are in total N.
First of all, the number smaller than it will not affect it, so we just need to consider it and larger than the number. So we're going to select (Ni + P) positions in N positions and give it and a larger number.
Then the number larger than it has a X1 arrangement, the number of smaller than it has X2 species arrangement. How does this come to beg? This is a multiset arrangement.
Multi-set arrangement for a multiset a={a1*n1, a2*n2, A3*n3, Ak*nk}. Number of permutations is Sum (n1...nk)!/(n1!n2!n3!... nk!)
So how many of the AI will be written into the answer? We enumerate each case: There are NI entry answers, (ni-1), (ni-2) .... 1 of them.
If there is a K AI that cannot be answered, it means that there are larger numbers in front of them, that is, they are inserted in the p a number larger than they are behind or between. This p number is followed by a total of p positions, each position can be inserted into any AI, the number of cases is C (k + p-1, K) (equivalent to the P box allocated K ball, partition method).
Then the AI's contribution to the answer is C (N, ni + p) * X1 * X2 * Sigma ((ni-k) * C (k + p-1, K)) (0 <= K < ni) * ai.
warning!
Error:% priority is higher than +! If so ans = ans + temp% Mod. Ans is going to explode!! should be ans = (ans + Temp)% Mod.
Last night because of this error debug 3hours!!!!!!
Code
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath > #include <algorithm>using namespace std;const int maxn = 100000 + 15; typedef long LONG ll;const LL Mod = 1000000007ll;int T, n, Top; LL Ans; ll A[MAXN], F[MAXN], G[MAXN], SUM[MAXN], NSUM[MAXN], FAC[MAXN], ny_fac[maxn];struct es_num{LL Cnt, Num; Es_num () {} es_num (ll A, ll b) {Num = A; Cnt = b; }} ES[MAXN]; ll Pow (ll A, ll B) {ll F, ret; f = A; ret = 1LL; while (b) {if (b & 1) {ret *= F; RET%= Mod; } b >>= 1; F *= F; f%= Mod; } return ret;} void Init () {int max_num = 100000 + 5; Fac[0] = 1; for (int i = 1; I <= max_num; ++i) fac[i] = fac[i-1] * I% Mod; for (int i = 0; I <= max_num; ++i) Ny_fac[i] = Pow (Fac[i], Mod-2);} ll C (ll A, ll b) {if (b = = 0) return 1; if (a < b) return 0; LL ret; ret = Fac[a]; Ret= ret * Ny_fac[a-b]% Mod; RET = ret * Ny_fac[b]% Mod; return ret;} int main () {Init (); scanf ("%d", &t); for (int case = 1; Case <= T; ++case) {scanf ("%d", &n); Ans = 0; for (int i = 1; I <= n; ++i) scanf ("%d", &a[i]); Sort (A + 1, a + n + 1); Top = 0; for (int i = 1; I <= n; ++i) {if (I! = 1 && a[i] = = A[i-1]) + + (Es[top]. CNT); else Es[++top] = Es_num ((LL) a[i], 1); } Sum[0] = 0; for (int i = 1; I <= Top; ++i) sum[i] = Sum[i-1] + es[i]. Cnt; Nsum[top + 1] = 0; for (int i = TOP; I >= 1; i.) Nsum[i] = nsum[i + 1] + es[i]. Cnt; LL t = 1; F[0] = 1; for (int i = 1; I <= Top; ++i) {t = T * fac[sum[i]]% Mod * Ny_fac[es[i]. CNT]% Mod; F[i] = t; t = T * ny_fac[sum[i]]% Mod; } t = 1; G[top + 1] = 1; for (int i = TOP; I >= 1; i.){t = T * fac[nsum[i]]% Mod * Ny_fac[es[i]. CNT]% Mod; G[i] = t; t = T * ny_fac[nsum[i]]% Mod; } LL x, y; for (int i = 1; I <= Top; ++i) {x = 0; for (int j = 0; J < Es[i]. Cnt; ++J) {y = (es[i]. CNT-J) * C (nsum[i + 1] + j-1, j)% Mod; x = (x + y)% Mod; } x = x * Es[i]. Num% Mod; x = x * f[i-1]% Mod; x = x * g[i + 1]% Mod; x = x * C (nsum[1], nsum[i])% Mod; Ans = (ans + x)% Mod; } printf ("Case #%d:%d\n", case, (int) Ans); } return 0;}
[Bestcoder round#26] Apple "Combinatorial Math"