QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#838503 | #7021. Counting Sheep in Ami Dongsuo | KellyWLJ | AC ✓ | 5696ms | 5528kb | C++17 | 3.8kb | 2024-12-31 13:16:36 | 2024-12-31 13:16:37 |
Judging History
answer
#pragma GCC optimize("Ofast", "inline")
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
using namespace std;
using ll = long long; using poly = vector<int>;
const int N = 10010, mod = 1e9 + 7, SZ = (1 << 18) + 5;
static char buf[SZ], *bgn = buf, *til = buf;
char getc() {
if(bgn == til) bgn = buf, til = buf + fread(buf, 1, SZ, stdin);
return bgn == til ? EOF : *bgn++;
}
template<typename T>
void read(T &x) {
char ch = getc(); T fh = 0; x = 0;
while(ch < '0' || ch > '9') fh |= (ch == '-'), ch = getc();
while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getc();
x = fh ? -x : x;
}
template<typename Type, typename... argc>
void read(Type &x, argc &...args) {read(x), read(args...);}
int n, m, w, u, v, a[N], ord[N], cnt, deg[N], iv[N], pw[N], sum[N], ans[N];
vector<int> edge[N];
poly f[N], tmp;
inline void Add(int &a, int b) {a += b, a >= mod && (a -= mod);}
inline int add(int a, int b) {a += b, a >= mod && (a -= mod); return a;}
ll qmi(ll a, ll b) {
ll res = 1;
for(; b; b >>= 1, a = a * a % mod) if(b & 1) res = res * a % mod;
return res;
}
void topu() {
queue<int> q;
for(int i = 1; i <= n; ++i) if(!deg[i]) q.push(i);
while(!q.empty()) {
int x = q.front(); q.pop();
ord[++cnt] = x;
for(int y : edge[x]) {
--deg[y];
if(deg[y] == 0) q.push(y);
}
}
}
poly mul(poly &f, poly &g) {
poly h(4, 0);
h[0] = 1ll * f[0] * g[0] % mod;
h[1] = (1ll * f[0] * g[1] + 1ll * f[1] * g[0]) % mod;
h[2] = (1ll * f[0] * g[2] + 1ll * f[1] * g[1] + 1ll * f[2] * g[0]) % mod;
h[3] = (1ll * f[0] * g[3] + 1ll * f[1] * g[2] + 1ll * f[2] * g[1] + 1ll * f[3] * g[0]) % mod;
// for(int i = 0; i < 4; ++i)
// for(int j = 0; j + i < 4; ++j) Add(h[i + j], 1ll * f[i] * g[j] % mod);
return h;
}
void work(int v) {
pw[0] = 1;
for(int i = 1; i <= w; ++i) pw[i] = 1ll * pw[i - 1] * v % mod;
for(int i = 1; i <= n; ++i) f[i].clear();
for(int i = cnt; i > 0; --i) {
int x = ord[i]; f[x].resize(4, 0);
f[x][0] = 1, f[x][1] = pw[a[x]];
for(int y : edge[x]) f[x] = mul(f[x], f[y]);
Add(sum[v], f[x][3]);
}
}
void mian(int T) {
read(n, m, w);
for(int i = 1; i <= n; ++i) edge[i].clear(), deg[i] = 0;
tmp.clear(), cnt = 0, tmp.resize(w * 3 + 2, 0), tmp[0] = 1;
for(int i = 0; i <= w * 3 + 1; ++i) sum[i] = ans[i] = 0;
for(int i = 1; i <= n; ++i) read(a[i]);
for(int i = 1; i <= m; ++i) read(u, v), edge[u].pb(v), ++deg[v];
topu();
for(int x = 1; x <= w * 3 + 1; ++x) work(x);
for(int i = 1; i <= w * 3 + 1; ++i) {
for(int j = w * 3 + 1; j > 0; --j) tmp[j] = add(tmp[j - 1], mod - 1ll * i * tmp[j] % mod);
tmp[0] = add(0, mod - 1ll * i * tmp[0] % mod);
}
for(int i = 1; i <= w * 3 + 1; ++i) {
poly nw = tmp; int inv = qmi(i, mod - 2);
nw[0] = 1ll * add(0, mod - nw[0]) * inv % mod;
for(int j = 1; j < (int)nw.size(); ++j) Add(nw[j], mod - nw[j - 1]), nw[j] = 1ll * add(0, mod - nw[j]) * inv % mod;
int val = sum[i];
for(int j = 1; j <= w * 3 + 1; ++j) if(j != i)
val = i > j ? (1ll * val * iv[i - j] % mod) : add(0, mod - 1ll * val * iv[j - i] % mod);
for(int j = 1; j <= w * 3; ++j) Add(ans[j], 1ll * nw[j] * val % mod);
}
printf("Case #%d: ", T);
for(int i = 1; i < w * 3; ++i) printf("%d ", ans[i]);
printf("%d\n", ans[w * 3]);
}
int main() {
#ifdef Kelly
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
freopen("err.txt", "w", stderr);
#endif
int T = 1; read(T);
iv[0] = 1;
for(int i = 1; i <= 1201; ++i) iv[i] = qmi(i, mod - 2);
for(int i = 1; i <= T; ++i) mian(i);
return 0;
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 4296kb
input:
2 4 3 4 1 2 3 4 1 2 1 3 1 4 4 6 4 1 2 3 4 1 2 1 3 1 4 2 3 2 4 3 4
output:
Case #1: 0 0 0 0 0 1 1 1 1 0 0 0 Case #2: 0 0 0 0 0 2 5 9 16 11 13 4
result:
ok 2 lines
Test #2:
score: 0
Accepted
time: 5696ms
memory: 5528kb
input:
5 9974 29992 400 170 290 309 152 96 55 128 315 95 69 301 193 9 316 45 192 132 360 53 370 342 110 130 228 300 189 293 327 208 306 46 162 280 167 107 163 335 289 330 354 192 44 393 47 185 330 53 278 91 229 194 276 72 159 126 75 48 205 371 263 76 375 309 145 59 186 76 255 73 352 271 318 77 144 27 398 9...
output:
Case #1: 0 0 274018431 297771390 14681274 83297566 129848413 317918129 382312947 164211443 744829668 972536660 161091713 584612649 283955998 299398877 929980460 603413890 576284480 584883426 86540784 41932768 7221264 355948077 425637438 215085800 934255450 960693559 165212095 499574284 678880682 802...
result:
ok 5 lines
Test #3:
score: 0
Accepted
time: 5403ms
memory: 5464kb
input:
5 10000 30000 400 114 284 235 163 116 221 345 291 156 69 342 31 207 320 285 261 146 213 255 231 321 70 320 244 236 124 361 366 205 131 254 240 78 98 105 78 358 308 213 26 294 280 68 171 260 100 70 147 245 363 321 289 115 388 124 205 51 167 312 346 298 9 363 128 128 57 396 287 298 2 101 313 282 247 1...
output:
Case #1: 0 0 93501181 286257603 572542724 958297287 437204841 15988619 688398011 460836807 327357817 293886104 355265135 516735084 773239441 130565300 868717238 999970718 511957216 417142626 703496479 384139200 446798372 904196097 743952806 978380746 597233623 610288520 7795743 801788862 265979979 4...
result:
ok 5 lines