QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#661715 | #8237. Sugar Sweet II | Szang | WA | 180ms | 23832kb | C++20 | 2.7kb | 2024-10-20 17:44:27 | 2024-10-20 17:44:31 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int MOD = 1e9 + 7;
const int N = 5e5 + 5;
int a[N], b[N], w[N];
int f[N];
int ansx[N], ansy[N];
int ans[N];
vector<int> e[N];
int n;
int qpow(int a, int b, int p)
{
int tmp = 1;
a = a % p;
while (b)
{
if (1 & b)
tmp = tmp * a % p;
a = a * a % p;
b >>= 1;
}
return tmp % p;
}
int inv(int a, int p) // 费马小定理求逆元
{
return qpow(a, p - 2, p);
}
int fac[N];
int cnt = 0;
int dfs(int u)
{
for (auto v : e[u])
{
// cout<<u<<" "<<v<<endl;
if (a[u] < a[v])
{
ansx[u] = a[u] + w[u];
ansy[u] = 1;
f[u] = 1;
return 1;
}
else if (a[u] >= a[v] + w[v])
{
ansx[u] = a[u];
ansy[u] = 1;
f[u] = 1;
return 0;
}
else if (a[u] >= a[v])
{
int ok = dfs(v);
cnt++;
if (ok == 1)
{
// cout<<cnt<<" "<<n<<" "<<u<<endl;
int x = fac[cnt];
int y = fac[n];
x = y * inv(x, MOD) % MOD;
ansx[u] = a[u] * (y - x) % MOD + (w[u] + a[u]) * x % MOD;
ansy[u] = y;
// cout<<ansx[u]<<" "<<ansy[u]<<endl;
f[u] = 1;
return 1;
}
else
{
return 0;
}
}
}
return 0;
}
void solve()
{
cin >> n;
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];
e[i].clear();
f[i] = 0;
}
for (int i = 1; i <= n; i++)
{
if (i != b[i])
e[i].push_back(b[i]);
else
ansx[i] = a[i], ansy[i] = 1;
}
for (int i = 1; i <= n; i++)
{
// cout << "111" << endl;
cnt = 1;
if (f[i] == 0)
dfs(i);
}
// cout << "111" << endl;
for (int i = 1; i <= n; i++)
{
// cout<<i<<endl;
ans[i] = ansx[i] * inv(ansy[i], MOD) % MOD;
cout << ans[i] << " ";
// cout << ansx[i] << " " << ansy[i] << endl;
}
cout << endl;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
fac[0] = 1;
for (int i = 1; i <= N; ++i)
fac[i] = (1ll * fac[i - 1] * i) % MOD;
int t = 1;
cin >> t;
while (t--)
{
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 23200kb
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: 180ms
memory: 23832kb
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 590444253 906393935 859123744 863886789 871186919 814243920 968784984 206455474 -982472957 449261413 -803240278 901433117 519383814 907574792 983760005 984444619 489899014 435736558 113628626 977360756 -517752854 963066959 871186919 814243920 132646723 421298438 601054667 9943...
result:
wrong answer 11th numbers differ - expected: '17527050', found: '-982472957'