QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#527444 | #8237. Sugar Sweet II | AmiyaCast | WA | 0ms | 22172kb | C++14 | 3.6kb | 2024-08-22 15:22:30 | 2024-08-22 15:22:31 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define int long long
#define pii make_pair
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=b;i>=a;--i)
const ll inf = 1145141919810;
using namespace std;
inline ll read() {
ll x=0,f=1;
char c=getchar();
while (c<'0' || c>'9') {
if (c=='-') f=-1;
c=getchar();
}
while (c>='0' && c<='9') {
x=x*10+c-'0';
c=getchar();
}
return x*f;
}
inline void print(ll x) {
if(x < 0) putchar('-'), x = -x;
if(x > 9) print(x / 10);
putchar(x % 10 + '0');
return ;
}
inline void pprint(ll x) {
print(x);
puts("");
}
const ll N = 5e5 + 7;
int n;
struct Node {
ll a, b, w;
int id;
ll p1, p2;
ll ord;
int opt;
} t[N];
int rev[N];
bool cmp(Node a, Node b) {
if(a.a == b.a) return rev[a.b] > rev[b.b];
return a.a < b.a;
}
const int mod = 1e9 + 7;
ll qm(ll a, ll b) {
ll base = 1;
while(b) {
if(b & 1) {
base = base * a % mod;
}
a = a * a % mod;
b >>= 1;
}
return base % mod;
}
ll inv(ll x) {
return qm(x, mod - 2);
}
ll fac[N];
int vis[N];
int head[N], nxt[N << 1], to[N << 1], cnt = 1;
void add(int x, int y) {
nxt[++cnt] = head[x];
to[cnt] = y;
head[x] = cnt;
}
int dfn[N], low[N], dfncnt, s[N], in_stack[N], tp;
int scc[N], sc; // 结点 i 所在 SCC 的编号
int sz[N]; // 强连通 i 的大小
void tarjan(int u) {
low[u] = dfn[u] = ++dfncnt, s[++tp] = u, in_stack[u] = 1;
for(int i = head[u]; i; i = nxt[i]){
const int &v = to[i];
if (!dfn[v]) {
tarjan(v);
low[u] = min(low[u], low[v]);
} else if (in_stack[v]) {
low[u] = min(low[u], dfn[v]);
}
}
if (dfn[u] == low[u]) {
++sc;
while (s[tp] != u) {
scc[s[tp]] = sc;
sz[sc]++;
in_stack[s[tp]] = 0;
--tp;
}
scc[s[tp]] = sc;
sz[sc]++;
in_stack[s[tp]] = 0;
--tp;
}
}
void slv() {
n = read();
rep(i, 1, n) t[i].a = read();
rep(i, 1, n) t[i].b = read();
rep(i, 1, n) t[i].w = read() + t[i].a;
rep(i, 1, n) t[i].id = i;
rep(i, 1, n) t[i].ord = 1, t[i].opt = 0;
sort(t + 1, t + 1 + n, cmp);
rep(i, 1, n) rev[t[i].id] = i;
for(int i = 1; i <= n; ++i){
if(t[rev[t[i].b]].a == t[i].a){
// cout << t[i].id << " " << t[i].b << endl;
add(i, rev[t[i].b]);
}
}
rep(i, 1, n){
if(!scc[i]){
tarjan(i);
}
}
// rep(i, 1, n){
// cout << scc[i] << " && " << sz[scc[i]] << endl;
// }
rep(i, 1, n){
if(sz[scc[i]] > 1){
t[i].opt = 1;
t[i].p1 = 1;
t[i].p2 = 0;
t[i].w = t[i].a;
}
}
rep(i, 1, n) {
if(t[i].opt) continue;
if(rev[t[i].b] == i) {
t[i].p1 = 1ll;
t[i].p2 = 0ll;
continue;
}
if(t[ rev[t[i].b] ].a > t[i].a) {
t[i].p1 = 0ll;
t[i].p2 = 1ll;
} else {
t[i].ord = t[ rev[t[i].b] ].ord + 1;
ll w = t[ rev[t[i].b] ].w;
ll _p1 = t[ rev[t[i].b] ].p1;
ll _p2 = t[ rev[t[i].b] ].p2;
if(w > t[i].a && _p2 != 0) {
t[i].p2 = 1 * inv(fac[t[i].ord]);
t[i].p1 = ((1ll - 1ll * t[i].p2) % mod + mod) % mod;
} else {
t[i].p1 = 1ll;
t[i].p2 = 0ll;
}
}
}
rep(i, 1, n) {
ll now = (t[rev[i]].p1 * t[rev[i]].a % mod + t[rev[i]].p2 * t[rev[i]].w % mod) % mod;
print(now);
putchar(' ');
}
puts("");
rep(i, 0, n + 1)
head[i] = t[i].a = t[i].b = t[i].w = t[i].p1 = t[i].p2 = t[i].id = t[i].ord = t[i].opt = rev[i] = vis[i] = dfn[i] = scc[i] = sz[i] = s[i] = in_stack[i] = low[i] = 0;
cnt = 1; dfncnt = 0;
}
signed main() {
int T = read();
fac[0] = 1;
rep(i, 1, 500000) {
fac[i] = fac[i - 1] * i % mod;
}
while(T--) {
slv();
}
return 0;
}
/*
4
4
1 2 2 2
2 3 4 3
3 2 1 4
*/
詳細信息
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 22172kb
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:
2 5 5 6 5 10 9 166666673 5 6 500000006 4 3 4 5
result:
wrong answer 1st numbers differ - expected: '500000007', found: '2'