QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#527439 | #8237. Sugar Sweet II | AmiyaCast | WA | 109ms | 26020kb | C++14 | 3.6kb | 2024-08-22 15:19:09 | 2024-08-22 15:19:10 |
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) {
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: 100
Accepted
time: 0ms
memory: 22128kb
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: 109ms
memory: 26020kb
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...
result:
wrong answer 25th numbers differ - expected: '665922935', found: '902284038'