1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| #include <chrono> #include <cstdio> #include <algorithm>
using namespace std;
int d[200005]; unsigned long long ps[200005], ss[200005]; unsigned long long fact[200005], ifact[200005]; unsigned long long irs[200005];
long long qpow(long long x, long long y) { long long p = x, ans = 1; do { if(y & 1) ans = ans * p % 998244353; p = p * p % 998244353; } while(y >>= 1); return ans; }
int main() { int n, m; scanf("%d%d", &n, &m); for(int i=1;i<=n;i++) { scanf("%d", d + i); } sort(d + 1, d + n + 1, [](int x, int y) { return x > y; }); for(int i=1;i<=n;i++) { ps[i] = (ps[i-1] + d[i]) % 998244353; } for(int i=n;i>=1;i--) { ss[i] = (ss[i+1] + d[i]) % 998244353; } fact[0] = 1; for(int i=1;i<=n+1;i++) { fact[i] = fact[i-1] * i % 998244353; } ifact[n+1] = qpow(fact[n+1], 998244351); for(int i=n;i>=0;i--) { ifact[i] = ifact[i+1] * (i+1) % 998244353; } for(int i=1;i<=n;i++) { irs[i] = fact[i-1] * fact[n-i+1] % 998244353; } auto comb = [](int x, int y) { return fact[x] * ifact[y] % 998244353 * ifact[x-y] % 998244353; }; auto calc = [&comb](int n, int r, int a) -> unsigned long long { if(r < a || a < 1) return 0ull; return comb(n, r + 1) * (r - a + 1) % 998244353 * fact[r] % 998244353 * fact[n-r-1] % 998244353; }; unsigned long long xorsum = 0; while(m--) { int a, b; scanf("%d%d", &a, &b); int c = upper_bound(d + 1, d + n + 1, b, [](int x, int y) { return x > y; }) - d - 1; long long v0 = calc(n, c, a) * ifact[n] % 998244353, v1 = calc(n, c-1, a) * ifact[n] % 998244353; printf("%lld\n", (v1 * ps[c] + v0 * ss[c+1]) % 998244353); } return 0; }
|