Created
December 15, 2025 08:13
-
-
Save hikariyo/c86a83dd765788df886dbbffdd8acac9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <bits/stdc++.h> | |
| using namespace std; | |
| #define int long long | |
| const int N = 1010, P = 9999991; | |
| int infact[N], fact[N]; | |
| int a[N], f[N]; | |
| int qpow(int a, int k) { | |
| int res = 1; | |
| while (k) { | |
| if (k & 1) res = res * a % P; | |
| a = a * a % P; | |
| k >>= 1; | |
| } | |
| return res; | |
| } | |
| int getsum(int x, int n) { | |
| // O(klog mod) | |
| if (x <= n) return f[x]; | |
| int ans = 0; | |
| int fac = 1; | |
| for (int i = 0; i <= n; i++) { | |
| int now = f[i] * infact[i] % P * infact[n-i] % P * qpow(x-i, P-2) % P; | |
| fac = fac * (x-i) % P; | |
| if ((n-i) % 2) now = (P - now) % P; | |
| ans = (ans + now) % P; | |
| } | |
| return ans * fac % P; | |
| } | |
| void solve() { | |
| int n, m; | |
| cin >> n >> m; | |
| for (int i = 0; i <= n; i++) { | |
| cin >> f[i]; | |
| } | |
| // f(n+1) | |
| int ans = 0, fac = 1; | |
| for (int i = 0; i <= n; i++) { | |
| int now = f[i] * infact[i] % P * infact[n-i] % P * qpow(n+1-i, P-2) % P; | |
| fac = fac * (n+1-i) % P; | |
| if ((n-i) % 2) now = (P - now) % P; | |
| ans = (ans + now) % P; | |
| } | |
| f[n+1] = ans * fac % P; | |
| for (int i = 1; i <= n+1; i++) f[i] = (f[i-1] + f[i]) % P; | |
| while (m--) { | |
| int l, r; | |
| cin >> l >> r; | |
| cout << (getsum(r, n+1) + P - getsum(l-1, n+1)) % P << '\n'; | |
| } | |
| } | |
| signed main() { | |
| ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); | |
| fact[0] = 1; | |
| for (int i = 1; i < N; i++) fact[i] = fact[i-1] * i % P; | |
| infact[N-1] = qpow(fact[N-1], P-2); | |
| for (int i = N-2; i >= 0; i--) infact[i] = infact[i+1] * (i+1) % P; | |
| int T; | |
| cin >> T; | |
| while (T--) solve(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment