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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
| #include<bits/stdc++.h> #define LL long long #define PII pair<int, int> using namespace std; template <class T> inline void read(T &res) { res = 0; bool flag = 0; char c = getchar(); while(c < '0' || c > '9') { if(c == '-') flag = 1; c = getchar(); } while('0' <= c && c <= '9') res = (res << 3) + (res << 1) + (c ^ 48), c = getchar(); if(flag) res = -res; } template <class T, class ...ARC> inline void read(T &res, ARC &...com){ read(res), read(com...); } template <class T> inline void out(T res) { if(res > 9) out(res / 10); putchar(res % 10 + '0'); } template <class T> inline void write(T res) { if(res < 0) putchar('-'), res = -res; out(res); } template <> inline void write(char c){ putchar(c); } template <> inline void write(const char *s) { while(*s) putchar(*s ++); } template<class T, class ...ARC> inline void write(T res, ARC ...com){ write(res), write(com...);} const int N = 5e5 + 5, K = 11, mod = 1e9 + 7, Inf = 0x3f3f3f3f; struct Node{ int l, r; }seg[N][K]; inline int qpow(int x, int k) { int res = 1; while(k) { if(k & 1) res = 1ll * res * x % mod; x = 1ll * x * x % mod; k >>= 1; } return res; } inline int calc(int x, int k) { static int fac[N]; int res = 0; if(x <= k + 2) { for(int i = 1;i <= x;i ++) res = (res + qpow(i, k)) % mod; } else{ fac[0] = 1; for(int i = 1;i <= k + 1;i ++) fac[i] = 1ll * fac[i - 1] * i % mod; int t = 1; for(int i = 1;i <= k + 2;i ++) t = 1ll * t * (x - i) % mod; int v0 = ((k + 2) & 1) ? 1 : -1; for(int i = 1, sum = 0;i <= k + 2;i ++) { sum = (sum + qpow(i, k)) % mod; res = (res + 1ll * v0 * sum * t % mod * qpow(x - i, mod - 2) % mod * qpow(1ll * fac[i - 1] * fac[k + 2 - i] % mod, mod - 2) % mod) % mod; v0 = -v0; } res = (res % mod + mod) % mod; } return res; } int n, k, c[N], d[N], sum[K]; int w[K], delta[K]; int v1[K], v2[K], val[K][K]; int ans = 0; inline void solve1() { for(int i = 1;i <= k;i ++) v1[i] = w[i] - (seg[n][i].r - seg[n][i].l); for(int i = 0, sum;i <= n;i ++) { sum = 1; for(int j = 1;j <= k;j ++) sum = 1ll * sum * max(0, w[j] - (seg[i][j].r - seg[i][j].l)) % mod; ans = (ans + sum) % mod; } } inline void solve2() { for(int i = 1;i <= n;i ++) for(int j = 1;j <= k;j ++) { seg[i][j].l = min(0, seg[i][j].l + delta[j] - seg[n][j].l); seg[i][j].r = max(0, seg[i][j].r + delta[j] - seg[n][j].r); } for(int i = 1;i <= k;i ++) v2[i] = seg[n][i].r - seg[n][i].l; int last = -1; for(int i = 1;i <= n;i ++) { for(int j = 1;j <= k;j ++) val[0][j] = 0; val[0][0] = 1; int T = Inf; for(int j = 1;j <= k;j ++) { int x = v1[j] - (seg[i][j].r - seg[i][j].l); if(x <= 0) return ; if(v2[j] > 0) T = min(T, x / v2[j]); for(int K = 0;K <= k;K ++) { val[j][K] = 1ll * val[j - 1][K] * x % mod; if(K > 0) val[j][K] = (val[j][K] - 1ll * val[j - 1][K - 1] * v2[j] % mod) % mod; } } ans = (ans + 1ll * val[k][0] * (T + 1) % mod) % mod; if(T != last) { last = T; for(int j = 1;j <= k;j ++) sum[j] = calc(T, j); } for(int j = 1;j <= k;j ++) ans = (ans + 1ll * sum[j] * val[k][j] % mod) % mod; } } int main() { read(n, k); for(int i = 1;i <= k;i ++) read(w[i]); for(int i = 1;i <= n;i ++) { read(c[i], d[i]); delta[c[i]] += d[i]; for(int j = 1;j <= k;j ++) seg[i][j] = seg[i - 1][j]; seg[i][c[i]].l = min(seg[i][c[i]].l, delta[c[i]]); seg[i][c[i]].r = max(seg[i][c[i]].r, delta[c[i]]); } bool flag = 0; for(int i = 1;i <= k;i ++) if(delta[i] != 0 || seg[n][i].r - seg[n][i].l >= w[i]) { flag = 1; break; } if(!flag) return puts("-1"), 0; solve1(); solve2(); write((ans % mod + mod) % mod); return 0; }
|