QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#643039#8237. Sugar Sweet IIwsxcb#WA 0ms28764kbC++172.6kb2024-10-15 18:15:502024-10-15 18:15:52

Judging History

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

  • [2024-11-04 16:59:03]
  • hack成功,自动添加数据
  • (/hack/1109)
  • [2024-10-15 18:15:52]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:28764kb
  • [2024-10-15 18:15:50]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int N = 5e5 + 5, mod = 1e9 + 7;
int a[N], b[N], w[N], vis[N], d[N], inv2;
ll dp[N][2], A[N];
vector<int>e[N];

ll ksm(ll a, ll b) {
	ll ans = 1;
	while (b) {
		if (b & 1)
			ans = ans * a % mod;
		a = a * a % mod;
		b /= 2;
	}
	return ans % mod;
}

ll inv(ll a) {
	return ksm(a, mod - 2);
}

void add(int u, int v) {
	e[v].pb(u);
	d[u]++;
}

ll get(ll n, ll m) {
	return A[n] * inv(A[m]) % mod;
}

void solve() {
	int n;
	cin >> n;
	vector<ll>res(n + 1);
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		vector<int>().swap(e[i]);
		d[i] = 0;
		vis[i] = 0;
	}
	for (int i = 1; i <= n; i++)
		cin >> b[i];
	for (int i = 1; i <= n; i++)
		cin >> w[i];
	stack<int>st;
	for (int i = 1; i <= n; i++) {
		if (vis[i])
			continue;
		int x = i;
		set<int>stk;
		while (!vis[x]) {
			st.push(x);
			stk.insert(x);
			vis[x] = 1;
			x = b[x];
		}
		if (stk.find(x) != stk.end()) {
			vector<int>t;
			while (st.top() != x) {
				t.pb(st.top());
				st.pop();
			}
			t.pb(st.top());
			st.pop();
			set<int>s;
			int ok = 0;
			for (auto x : t) {
				s.insert(a[x]);
				if (s.size() > 1) {
					ok = 1;
					break;
				}
			}
			if (ok) {
				for (auto x : t) {
					int z = b[x];
					if (a[x] >= a[z])
						add(x, z);
				}
			} else {
				for (auto x : t)
					res[x] = a[x];
			}
		}
		while (st.size()) {
			int x = st.top();
			st.pop();
			add(x, b[x]);
		}
	}
//	for (int i = 1; i <= n; i++) {
//		cout << i << ' ';
//		for (auto x : e[i])
//			cout << x << ' ';
//		cout << '\n';
//	}
	//cout << res[2] << "  44444\n";
	queue<pair<int, int>>q;
	for (int i = 1; i <= n; i++) {
		if (d[i] == 0) {
			q.push({i, 1});
			if (!res[i]) {
				dp[i][0] = 0;
				dp[i][1] = 1;
			} else {
				dp[i][1] = 0;
				dp[i][0] = 1;
			}
		}
	}
	//return ;
	while (q.size()) {
		int u = q.front().first, c = q.front().second;
		q.pop();
		for (auto v : e[u]) {
			d[v]--;
			if (a[v] < a[u]) {
				dp[v][1] = 1;
				dp[v][0] = 0;
				q.push({v, 1});
			} else if (a[v] < a[u] + w[u]) {
				c++;
				dp[v][1] = get(n, c) * inv(A[n]);
				q.push({v, c});
			} else {
				dp[v][1] = 0;
				dp[v][0] = 1;
				q.push({v, 1});
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		cout << (a[i] + dp[i][1]*w[i]) % mod << ' ';
	}
	cout << '\n';
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	inv2 = ksm(2, mod - 2);
	A[0] = 1;
	for (int i = 1; i < N; i++) {
		A[i] = A[i - 1] * i % mod;
	}
	int t = 1;
	//cin >> t;
	while (t--)
		solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 28764kb

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:

500000009 5 5 166666673 

result:

wrong answer 1st numbers differ - expected: '500000007', found: '500000009'