QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#617796 | #8237. Sugar Sweet II | E7k | WA | 163ms | 3732kb | C++20 | 2.3kb | 2024-10-06 17:09:29 | 2024-10-06 17:09:31 |
Judging History
answer
#include <bits/stdc++.h>
#define endl '\n'
#define int long long
#define gcd __gcd
using namespace std;
typedef pair<int,int> pii;
const int INF = 9e18;
const int inf = 2e9;
const int N = 2e5 + 10;
const int mod = 1e9 + 7;
int qmi(int a,int k)
{
int res = 1;
while(k)
{
if(k & 1) res = res * a % mod;
k >>= 1;
a =a * a % mod;
}
return res;
}
int inv(int x)
{
return qmi(x,mod-2);
}
int MOD(int x)
{
return (x % mod + mod) % mod;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >>t ;
while(t --)
{
int n;
cin >> n;
vector<int> a(n + 1),b(n + 1),w(n + 1),p(n + 1,-1),f(n + 1);
f[0] = 1;
for(int i = 1;i <= n;i ++) f[i] = f[i-1] * i % mod;
vector<array<int,2>> e(n + 1);
vector<vector<int>> g(n + 1);
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];
for(int i = 1;i <= n;i ++) e[i] = {a[i],i};
sort(e.begin()+1,e.end());
for(int i = 1;i <= n;i ++)
{
if(b[i] != i)
g[b[i]].push_back(i);
}
vector<int> ans(n + 1);
auto dfs = [&](auto &&dfs,int u,int fa,int cnt) -> void
{
if(ans[u]) return ;
int j = u;
u = b[u];
if(j == u)
{
ans[j] = a[j];
cnt = 1;
}
else if(a[j] < a[u])
{
ans[j] = a[j] + w[j];
cnt = 1;
}
else if(a[j] >= a[u] + w[u])
{
ans[j] = a[j];
cnt = 1;
}
else
{
int ip = inv(f[cnt]);
ans[j] = a[j] * MOD(1 - ip) % mod + (a[j] + w[j]) * ip % mod;
ans[j] %= mod;
}
u = j;
for(auto v : g[u])
{
if(v == fa || ans[v] > 0) continue;
dfs(dfs,v,u,cnt + 1);
}
};
for(int i = n;i >= 1;i --)
{
dfs(dfs,i,0,1);
}
for(int i = 1;i <= n;i ++) cout << ans[i] << ' ';
cout << endl;
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3584kb
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: 163ms
memory: 3732kb
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 986091180 234208588 859123744 863886789 871186919 814243920 968784984 1206455481 17527050 1449261420 196759729 901433117 519383814 907574792 983760005 984444619 489899014 435736558 1113628633 977360756 482247153 963066959 902284038 240323331 132646723 421298438 601054667 10994...
result:
wrong answer 3rd numbers differ - expected: '590444253', found: '986091180'