思路:
模拟,数学。
实现:
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 6 const int MAXN = 100005; 7 8 typedef long long ll; 9 10 int a[MAXN], last[3], num[3], n; 11 12 ll cnm(int n, int m) 13 { 14 ll s = 1; 15 int k = 1; 16 if(m > n / 2) 17 m = n - m; 18 for(int i = n - m + 1; i <= n; i++) 19 { 20 s *= (ll)i; 21 while(k <= m && s % k == 0) 22 { 23 s /= (ll)k; 24 k++; 25 } 26 } 27 return s; 28 } 29 30 int main() 31 { 32 scanf("%d", &n); 33 for (int i = 0; i < n; i++) 34 scanf("%d", &a[i]); 35 sort(a, a + n); 36 int cnt = 0; 37 last[0] = 0; 38 int i = 1; 39 while (i < n) 40 { 41 if (a[i] != a[last[cnt]]) 42 { 43 num[cnt] = i - last[cnt]; 44 last[++cnt] = i; 45 } 46 if (cnt == 2) break; 47 i++; 48 } 49 while (a[i] == a[last[cnt]] && i < n) i++; 50 num[cnt] = i - last[cnt]; 51 if (num[0] >= 3) cout << cnm(num[0], 3) << endl; 52 else if (num[0] == 2) cout << num[1] << endl; 53 else if (cnt == 1 || num[1] >= 2) cout << cnm(num[1], 2) << endl; 54 else cout << num[2] << endl; 55 return 0; 56 }