Advertisement
Dmaxiya

智力测试 参考代码

Mar 10th, 2025
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.94 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long LL;
  5. const int maxn = 200000 + 100;
  6. const LL MOD = 1000000007;
  7. int n, m, T, sr, sc, tr, tc;
  8. int r[maxn], c[maxn], sortR[maxn], sortC[maxn];
  9. LL multR[maxn], multC[maxn], addR[maxn], addC[maxn], fac[maxn], invFac[maxn];
  10.  
  11. LL fastPow(LL res, LL n) {
  12.     LL ans;
  13.     for (ans = 1; n != 0; n >>= 1) {
  14.         if ((n & 1) == 1) {
  15.             ans = ans * res % MOD;
  16.         }
  17.         res = res * res % MOD;
  18.     }
  19.     return ans;
  20. }
  21.  
  22. LL inv(LL x) {
  23.     return fastPow(x, MOD - 2);
  24. }
  25.  
  26. void init() {
  27.     fac[0] = 1;
  28.     for (int i = 1; i < maxn; ++i) {
  29.         fac[i] = fac[i - 1] * i % MOD;
  30.     }
  31.     invFac[maxn - 1] = inv(fac[maxn - 1]);
  32.     for (int i = maxn - 2; i >= 0; --i) {
  33.         invFac[i] = invFac[i + 1] * (i + 1) % MOD;
  34.     }
  35. }
  36.  
  37. LL C(LL n, LL m) {
  38.     return fac[n] * invFac[n - m] % MOD * invFac[m] % MOD;
  39. }
  40.  
  41. void solve(int *num, int n, LL *mult, LL *add) {
  42.     sort(num + 1, num + 1 + n);
  43.     mult[0] = 1;
  44.     int r = 1;
  45.     for (int l = 1; l <= n; l = r) {
  46.         while (r <= n && num[r] == num[l]) {
  47.             ++r;
  48.         }
  49.         mult[l] = mult[l - 1] * (r - l) % MOD;
  50.         add[l] = add[l - 1] + 1;
  51.         for (int i = l; i < r; ++i) {
  52.             mult[i] = mult[l];
  53.             add[i] = add[l];
  54.         }
  55.     }
  56. }
  57.  
  58. int main() {
  59. #ifdef ExRoc
  60.     freopen("test.txt", "r", stdin);
  61. #endif
  62.     ios::sync_with_stdio(false);
  63.  
  64.     init();
  65.     cin >> n >> m >> T;
  66.     for (int i = 1; i <= n; ++i) {
  67.         cin >> r[i];
  68.         sortR[i] = r[i];
  69.     }
  70.     for (int i = 1; i <= m; ++i) {
  71.         cin >> c[i];
  72.         sortC[i] = c[i];
  73.     }
  74.     solve(sortR, n, multR, addR);
  75.     solve(sortC, m, multC, addC);
  76.  
  77.     while (T--) {
  78.         cin >> sr >> sc >> tr >> tc;
  79.         if (r[sr] > r[tr] || c[sc] > c[tc]) {
  80.             cout << 0 << endl;
  81.             continue;
  82.         }
  83.         if (sr != tr && r[sr] == r[tr]) {
  84.             cout << 0 << endl;
  85.             continue;
  86.         }
  87.         if (sc != tc && c[sc] == c[tc]) {
  88.             cout << 0 << endl;
  89.             continue;
  90.         }
  91.         LL mult = 1;
  92.         int cn = 0;
  93.         int cm = 0;
  94.         if (sr != tr) {
  95.             int idx1 = upper_bound(sortR + 1, sortR + 1 + n, r[sr]) - sortR;
  96.             --idx1;
  97.             int idx2 = lower_bound(sortR + 1, sortR + 1 + n, r[tr]) - sortR;
  98.             --idx2;
  99.             mult *= multR[idx2] * inv(multR[idx1]) % MOD;
  100.             mult %= MOD;
  101.             cn = cm = addR[idx2] - addR[idx1] + 1;
  102.         }
  103.         if (sc != tc) {
  104.             int idx1 = upper_bound(sortC + 1, sortC + 1 + m, c[sc]) - sortC;
  105.             --idx1;
  106.             int idx2 = lower_bound(sortC + 1, sortC + 1 + m, c[tc]) - sortC;
  107.             --idx2;
  108.             mult *= multC[idx2] * inv(multC[idx1]) % MOD;
  109.             mult %= MOD;
  110.             cn += addC[idx2] - addC[idx1] + 1;
  111.         }
  112.         cout << mult * C(cn, cm) % MOD << endl;
  113.     }
  114.  
  115.     return 0;
  116. }
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement