QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#791155 | #8237. Sugar Sweet II | cdd | WA | 165ms | 14260kb | C++23 | 3.1kb | 2024-11-28 17:10:22 | 2024-11-28 17:10:24 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pii;
typedef long long LL;
typedef unsigned long long uLL;
const int inf32 = 1e9;
const LL inf64 = 1e18;
const int mod = 1e9 + 7;
int inv(int c) {
int a = c, b = mod, x = 1, y = 0;
while (b) {
int t = a / b;
a -= t * b; swap(a, b);
x -= t * y; swap(x, y);
}
x = (x % mod + mod) % mod;
return x;
}
int add(int a, int b) {
int c = a + b;
return c >= mod ? c - mod : c;
}
int sub(int a, int b) {
return add(a, mod - b);
}
int mul(int a, int b) {
return (a * b) % mod;
}
int frac(int a, int b) {
return mul(a, inv(b));
}
const int maxn = 1e6 + 5;
int fact[maxn], invfact[maxn];
void init(int n) {
fact[0] = invfact[0] = 1;
for (int i = 1; i <= n; i++) fact[i] = mul(fact[i - 1], i);
invfact[n] = inv(fact[n]);
for (int i = n - 1; i; i--)
invfact[i] = mul(invfact[i + 1], i + 1);
}
int C(int a, int b) {
if (b < 0) return 0;
if (a == b || !b) return 1;
if (a < b) return 0;
return mul(fact[a], mul(invfact[b], invfact[a - b]));
}
signed main()
{
cin.tie(0); cout.tie(0);
ios::sync_with_stdio(0);
init(5e5);
int T = 1;
cin >> T;
while (T--) {
int n;
cin >> n;
vector<int> a(n + 5, 0), b(n + 5, 0), w(n + 5, 0);
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) cin >> b[i];
for (int i = 1; i <= n; i++) cin >> w[i];
vector<int> p(n + 5, 0), vis(n + 5, 0), ll(n + 5, 0);
function<pii(int)> dfs = [&] (int cur) -> pii { // ff, len
if (vis[cur]) {
if (p[cur] == -1 || ll[cur] == 0) return {-1, 0};
return {0, ll[cur]};
}
int v = b[cur];
vis[cur] = 1;
if (a[cur] < a[v]) {
p[cur] = fact[n];
ll[cur] = 1;
return {0, 1};
}
if (a[cur] >= a[v] + w[v]) {
p[cur] = -1;
return {-1, 0};
}
auto [ff, len] = dfs(v);
// cerr << cur << ' ' << ff << ' ' << len << endl;
// cerr << cur << ' ' << v << ' ' << a[cur] << ' ' << a[v] << endl;
if (ff == -1) {
p[cur] = -1;
return {-1, 0};
}
// if (ff == 1) return p[cur] = fact[n], make_pair(1, 0);
p[cur] = mul(C(n, len + 1), fact[n - len - 1]);
ll[cur] = len + 1;
return {0, ll[cur]};
};
for (int i = 1; i <= n; i++)
if (!vis[i]) dfs(i);
// for (int i = 1; i <= n; i++) cerr << p[i] << ' '; cerr << endl;
int ans = 0;
for (int i = 1; i <= n; i++) {
int tmp = a[i];
if (p[i] == -1) p[i] = 0;
tmp += frac(mul(p[i], w[i]), fact[n]);
cout << tmp << ' ';
}
cout << "\n";
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 7ms
memory: 14260kb
input:
4 4 2 5 5 2 4 2 1 3 3 2 1 4 3 5 4 3 1 1 1 6 6 6 3 5 4 3 2 3 1 1 2 3 5 2 1 3 2 1 5 1 1 3 4 1 3 4 2 4
output:
500000007 5 5 6 5 10 9 166666673 5 6 500000006 4 3 4 5
result:
ok 15 numbers
Test #2:
score: -100
Wrong Answer
time: 165ms
memory: 13568kb
input:
50000 5 508432375 168140163 892620793 578579275 251380640 3 4 4 1 3 346232959 736203130 186940774 655629320 607743104 1 863886789 1 364158084 18 864679185 463975750 558804051 604216585 694033700 499417132 375390750 337590759 467353355 111206671 983760005 984444619 322277587 138763925 205122047 97736...
output:
854665334 904343293 1590444260 906393935 859123744 863886789 871186919 814243920 968784984 1206455481 1017527057 1449261420 1196759736 901433117 519383814 907574792 983760005 984444619 489899014 435736558 1113628633 977360756 1482247160 963066959 665922935 577926775 1132646730 421298438 601054667...
result:
wrong answer 3rd numbers differ - expected: '590444253', found: '1590444260'