QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#679133 | #8237. Sugar Sweet II | tsogsummit | WA | 50ms | 32112kb | C++14 | 2.4kb | 2024-10-26 16:57:16 | 2024-10-26 16:57:17 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 1e9 + 7;
const int N = 5e5 + 5;
ll fac[N], inv[N];
bool vis[N], vis2[N];
int d[N];
int par[N], a[N], w[N];
vector<int> g[N];
ll binpow(ll a, ll b, ll mod){
ll res = 1;
while(b){
if(b & 1)
res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
ll mul(ll a, ll b){
a %= mod;
b %= mod;
return a * b % mod;
}
ll sub(ll a, ll b){
a -= b;
while(a < 0) a += mod;
return a;
}
ll add(ll a, ll b){
a += b;
return a % mod;
}
void init(int n){
for(int i = 0; i < n; i++){
vis[i] = false;
vis2[i] = false;
d[i] = 0;
par[i] = -1;
g[i].clear();
}
}
int solve(int i){
if(par[i] == -1)return 0;
if(vis[i])return d[i];
vis[i] = true;
d[i] = 1 + solve(par[i]);
return d[i];
}
void dfs(int u, vector<int> &ans, bool is_positive){
if(vis2[u])return;
vis2[u] = true;
for(int v: g[u]){
if(a[u] + w[u] > a[v] && a[u] <= a[v]){
ans[v] = a[v] + (is_positive ? w[v] : 0);
dfs(v, ans, is_positive);
}
}
}
void solve(){
int n;
cin >> n;
init(n);
vector<int> b(n), ans(n, -1);
for(int i = 0; i < n; i++){
cin >> a[i];
}
for(int i = 0; i < n; i++){
cin >> b[i]; b[i]--;
g[i].push_back(b[i]);
}
for(int i = 0; i < n; i++){
cin >> w[i];
}
for(int i = 0; i < n; i++){
if(a[i] >= a[b[i]] + w[b[i]] || i == b[i]){
dfs(i, ans, false);
ans[i] = a[i] % mod;
continue;
}
if(a[i] < a[b[i]]){
ans[i] = (a[i] + w[i]) % mod;
dfs(i, ans, true);
continue;
}
par[i] = b[i];
}
for(int i = 0; i < n; i++){
solve(i);
}
for(int i = 0; i < n; i++){
if(ans[i] == -1){
ans[i] = add(mul(inv[d[i] + 1], a[i] + w[i]), mul(sub(1, inv[d[i] + 1]), a[i]));
}
}
for(int i = 0; i < n; i++){
cout << ans[i] << " \n"[i == n - 1];
}
}
int main() {
int t;
cin >> t;
fac[0] = 1;
for(int i = 1; i < N; i++){
fac[i] = (fac[i - 1] * i) % mod;
inv[i] = binpow(fac[i], mod - 2, mod);
}
while(t--){
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 50ms
memory: 32112kb
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 6 6 11 10 9 6 5 6 3 4 7 4 5
result:
wrong answer 3rd numbers differ - expected: '5', found: '6'