QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#678786#8237. Sugar Sweet IItsogsummitWA 395ms24268kbC++142.3kb2024-10-26 16:08:072024-10-26 16:08:09

Judging History

你现在查看的是最新测评结果

  • [2024-11-04 16:59:03]
  • hack成功,自动添加数据
  • (/hack/1109)
  • [2024-10-26 16:08:09]
  • 评测
  • 测评结果:WA
  • 用时:395ms
  • 内存:24268kb
  • [2024-10-26 16:08:07]
  • 提交

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];
vector<int> adj[N];
vector<int> order;
int d[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;
}


void dfs(int u, bool is_later){
    for(int v: adj[u]){
        if(!vis[v]){
            vis[v] = true;
            if(is_later)d[v] = d[u] + 1;
            dfs(v, is_later);
        }
    }
    if(!is_later)order.push_back(u);
}
ll mul(ll a, ll b){
    return a * b % mod;
}
ll sub(ll a, ll b){
    a -= b;
    if(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++){
        adj[i].clear();
        vis[i] = false;
        d[i] = 0;
    }

    order.clear();
}
void solve(){
    int n;
    cin >> n;
    init(n);
    vector<int> a(n), b(n), w(n), ans(n);
    for(auto &x: a)cin>>x;
    for(int i = 0; i < n; i++){
        cin >> b[i]; b[i]--;
    }
    for(auto &x: w)cin>>x;
    for(int i = 0; i < n; i++){
        if(a[i] >= a[b[i]] + w[b[i]] || i == b[i]){
            ans[i] = a[i] % mod;
            continue;
        }
        if(a[i] < a[b[i]]){
            ans[i] = (a[i] + w[i]) % mod;
            continue;
        }
        adj[b[i]].push_back(i);
    }
    for(int i = 0; i < n; i++){
        if(!vis[i]){
            vis[i] = true;
            dfs(i, 0);
        }
    }
    reverse(order.begin(), order.end());
    for(int i = 0; i < n; i++)vis[i] = false;
    
    for(auto &x: order){
        if(!vis[x]){
            vis[x] = true;
            dfs(x, 1);
        }
    }
    
    for(int i = 0; i < n; i++){
        if(!ans[i]){
            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();
    }
}

详细

Test #1:

score: 100
Accepted
time: 56ms
memory: 24184kb

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: 395ms
memory: 24268kb

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 17527050 449261413 196759729 901433117 519383814 907574792 983760005 984444619 489899014 435736558 113628626 977360756 482247153 963066959
902284038 240323331 132646723 421298438 601054667 99438820 94...

result:

wrong answer 25th numbers differ - expected: '665922935', found: '902284038'