Official

C - Not Equal Editorial by en_translator


When you run into a problem you cannot solve, it is always effective to consider specific examples.

For this editorial, let us consider special cases where \(C=(100,100,100)\).

First, there are \(100\) candidates of \(A_i\): \(1,2, \ldots,\) and \(100\).
Second, there are \(99\) candidates of \(A_i\): \(1,2, \ldots,\) and \(100\), except for that used as \(A_1\).
Last Second, there are \(98\) candidates of \(A_i\): \(1,2, \ldots,\) and \(100\), except for that used as \(A_1\) and \(A_2\).

Therefore, we can find the number of possible \(A\)’s as \(100 \times 99 \times 98\).

Similarly, if \(C\) is sorted in the increasing order, we can see that the number of possibilities for \(A\) is the product of \((C_i-i+1)\). We can prove that because, after determining the first \(i-1\) elements \(A_1, A_2, \ldots \), the candidates for \(A_i\) are \(1,2, \ldots C_i\), except for \(A_1, A_2, \ldots A_{i-1}\) (which are all pairwise distinct and between \(1\) and \(C_i\), inclusive); namely there are \((C_i-i+1)\) candidates. If however any \(i\) satisfies \(C_i-i+1<0\), then the answer is \(0\).

Note that you have to sort \(C\) given as inputs in the increasing order, since they are not necessarily sorted so.
Note that using 32-bit type may lead to an overflow during the computation.

Sample Code (C++)

#include<bits/stdc++.h>
using namespace std;

int main(){
    int N; cin >> N;
    vector<int> C(N);
    for(int i = 0; i < N; i++) cin >> C[i];
    sort(C.begin(),C.end());
    long long ans = 1;
    for(int i = 0; i < N; i++) ans = ans * max(0, C[i] - i) % 1000000007;
    cout << ans << endl;
}

Sample Code (Python)

N = int(input())
C = list(map(int, input().split()))
C.sort()
ans=1
for i in range(N):
    ans = ans * max(0, C[i] - i) % 1000000007
print(ans)

posted:
last update: