Advertisement
Dmaxiya

黑客 参考代码

Apr 12th, 2025
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.59 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long LL;
  5. const LL MOD = 1000000007;
  6. const int maxn = 500000 + 100;
  7. int n, nn;
  8. LL ans;
  9. LL num[maxn];
  10. LL cnt[maxn], fac[maxn], facInv[maxn];
  11.  
  12. LL fastPow(LL res, LL n) {
  13.     LL ans;
  14.     for (ans = 1; n != 0; n >>= 1) {
  15.         if ((n & 1) == 1) {
  16.             ans = ans * res % MOD;
  17.         }
  18.         res = res * res % MOD;
  19.     }
  20.     return ans;
  21. }
  22.  
  23. LL inv(LL x) {
  24.     return fastPow(x, MOD - 2);
  25. }
  26.  
  27. void init() {
  28.     fac[0] = 1;
  29.     for (int i = 1; i < maxn; ++i) {
  30.         fac[i] = fac[i - 1] * i % MOD;
  31.     }
  32.     facInv[maxn - 1] = inv(fac[maxn - 1]);
  33.     for (int i = maxn - 2; i >= 0; --i) {
  34.         facInv[i] = facInv[i + 1] * (i + 1) % MOD;
  35.     }
  36. }
  37.  
  38. LL solve(int x, int y) {
  39.     if (cnt[x] < 1 || cnt[y] < 1) {
  40.         return 0;
  41.     }
  42.     LL ret = fac[nn];
  43.     --cnt[x];
  44.     --cnt[y];
  45.     for (int i = 0; i < n; ++i) {
  46.         ret = ret * facInv[cnt[num[i]]] % MOD;
  47.     }
  48.     ++cnt[x];
  49.     ++cnt[y];
  50.     return ret;
  51. }
  52.  
  53. int main() {
  54. #ifdef ExRoc
  55.     freopen("test.txt", "r", stdin);
  56. #endif // ExRoc
  57.  
  58.     init();
  59.     cin >> n;
  60.     for (int i = 0; i < n; ++i) {
  61.         cin >> num[i];
  62.         ++cnt[num[i]];
  63.     }
  64.     nn = n - 2;
  65.     sort(num, num + n);
  66.     n = unique(num, num + n) - num;
  67.     for (int i = 1; i <= nn / i; ++i) {
  68.         LL tmp = 0;
  69.         if (nn % i == 0) {
  70.             tmp = solve(i, nn / i) % MOD;
  71.             if (nn / i != i) {
  72.                 tmp = tmp * 2 % MOD;
  73.             }
  74.         }
  75.         ans = (ans + tmp) % MOD;
  76.     }
  77.     cout << ans << endl;
  78.  
  79.     return 0;
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement