QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#323982 | #8237. Sugar Sweet II | ucup-team1055# | TL | 20ms | 12536kb | C++20 | 4.9kb | 2024-02-10 14:58:03 | 2024-02-10 14:58:06 |
Judging History
answer
#include <bits/stdc++.h>
#define rep(i, s, n) for (int i = int(s); i < int(n); i++)
#define rrep(i, s, n) for (int i = int(n) - 1; i >= int(s); i--)
#define all(v) (v).begin(), (v).end()
using ll = long long;
using ull = unsigned long long;
using ld = long double;
template <class T> bool chmin(T &a, T b) {
if (a <= b) return false;
a = b;
return true;
}
template <class T> bool chmax(T &a, T b) {
if (a >= b) return false;
a = b;
return true;
}
using namespace std;
template <ll m> struct modint {
using mint = modint;
ll a;
modint(ll x = 0) : a((x % m + m) % m) {}
static constexpr ll mod() {
return m;
}
ll val() const {
return a;
}
ll &val() {
return a;
}
mint pow(ll n) const {
mint res = 1;
mint x = a;
while (n) {
if (n & 1) res *= x;
x *= x;
n >>= 1;
}
return res;
}
mint inv() const {
return pow(m - 2);
}
mint operator+=(const mint rhs) {
a += rhs.a;
if (a >= m) a -= m;
return *this;
}
mint operator-=(const mint rhs) {
if (a < rhs.a) a += m;
a -= rhs.a;
return *this;
}
mint operator*=(const mint rhs) {
a = a * rhs.a % m;
return *this;
}
mint operator/=(const mint rhs) {
*this *= rhs.inv();
return *this;
}
friend mint operator+(const mint &lhs, const mint &rhs) {
return mint(lhs) += rhs;
}
friend mint operator-(const mint &lhs, const mint &rhs) {
return mint(lhs) -= rhs;
}
friend mint operator*(const mint &lhs, const mint &rhs) {
return mint(lhs) *= rhs;
}
friend mint operator/(const mint &lhs, const mint &rhs) {
return mint(lhs) /= rhs;
}
friend bool operator==(const mint &lhs, const mint &rhs) {
return lhs.a == rhs.a;
}
friend bool operator!=(const mint &lhs, const mint &rhs) {
return !(lhs == rhs);
}
mint operator+() const {
return *this;
}
mint operator-() const {
return mint() - *this;
}
};
using mint = modint<1'000'000'007>;
template <typename T> struct Binom {
Binom(int lim = 600'000) {
if (kaijo.empty()) {
kaijo = {1, 1};
kainv = {1, 1};
}
kaijo.resize(lim + 1), kainv.resize(lim + 1);
for (int i = 2; i <= lim; i++) kaijo[i] = kaijo[i - 1] * T(i);
kainv[lim] = kaijo[lim].inv();
for (int i = lim - 1; i >= 2; i--) kainv[i] = kainv[i + 1] * T(i + 1);
}
static T fact(int x) {
if (x < 0) return T(0);
return kaijo[x];
}
static T ifact(int x) {
if (x < 0) return T(0);
return kainv[x];
}
static T C(int n, int r) {
if (n < 0 || n < r || r < 0) return T(0);
return kaijo[n] * kainv[r] * kainv[n - r];
}
static T P(int n, int r) {
if (n < 0 || n < r || r < 0) return T(0);
return kaijo[n] * kainv[n - r];
}
static T Inv(int n) {
assert(0 < n);
return ifact(n) * fact(n - 1);
}
T operator()(int n, int r) {
return C(n, r);
}
private:
static vector<T> kaijo, kainv;
};
template <typename T> vector<T> Binom<T>::kaijo = vector<T>(2, T(1));
template <typename T> vector<T> Binom<T>::kainv = vector<T>(2, T(1));
void solve() {
int n;
std::cin >> n;
std::vector<ll> a(n), b(n), w(n);
rep(i,0,n) {
std::cin >> a[i];
}
rep(i,0,n) {
std::cin >> b[i];
b[i]--;
}
rep(i,0,n) {
std::cin >> w[i];
}
Binom<mint> binom;
std::vector<mint> ans(n);
std::vector<int> nxt(n, -1);
std::vector rev(n, std::vector<int>());
std::vector<int> seen(n, -1);
rep(i,0,n) {
ans[i] = a[i];
if(i == b[i]) {
seen[i] = 0;
}
else if(a[i] < a[b[i]]) {
seen[i] = 1;
}
else if(a[b[i]] + w[b[i]] <= a[i]) {
seen[i] = 0;
}
else {
nxt[i] = b[i];
rev[b[i]].emplace_back(i);
}
}
std::queue<int> que;
std::vector<int> dp(n, 0);
rep(i,0,n) {
if(seen[i] == 1) {
dp[i] = 1;
que.push(i);
}
}
while(!que.empty()) {
int v = que.front();
que.pop();
ans[v] += mint(w[v]) * binom.C(n, dp[v]) * binom.fact(n - dp[v]) * binom.ifact(n);
for(auto nv: rev[v]) {
dp[nv] = dp[v] + 1;
que.push(nv);
}
}
rep(i,0,n) {
std::cout << ans[i].val() << " \n"[i == n-1];
}
}
int main() {
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
int t;
std::cin >> t;
while(t--) {
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 20ms
memory: 12536kb
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
Time Limit Exceeded
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 665922935 577926775 132646723 421298438 601054667 99438820 94...