QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#368091 | #8237. Sugar Sweet II | wdw | WA | 140ms | 11600kb | C++20 | 2.8kb | 2024-03-26 20:15:20 | 2024-03-26 20:15:20 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
const int N = 5e5 + 5;
const int mod = 1e9+7;
const double eps =0.00001;
#define int long long
#define endl '\n'
using i64 = int64_t;
int ksm(int a,int b){
int ans=1;
while(b){
if(b%2)ans=ans*a%mod;
a=a*a%mod;
b/=2;
}
return ans;
}
i64 fpow(i64 x, i64 r)
{
i64 result = 1;
while (r)
{
if (r & 1)result = result * x % mod;
r >>= 1;
x = x * x % mod;
}
return result;
}
namespace binom {
i64 fac[N], ifac[N];
int __ = []
{
fac[0] = 1;
for (int i = 1; i <= N - 5; i++)
fac[i] = fac[i - 1] * i % mod;
ifac[N - 5] = fpow(fac[N - 5], mod - 2);
for (int i = N - 5; i; i--)
ifac[i - 1] = ifac[i] * i % mod;
return 0;
}();
inline i64 C(int n, int m)
{
if (n < m || m < 0)return 0;
return fac[n] * ifac[m] % mod * ifac[n - m] % mod;
}
inline i64 A(int n, int m)
{
if (n < m || m < 0)return 0;
return fac[n] * ifac[n - m] % mod;
}
}
using namespace binom;
vector<vector<int>>q;
vector<int>d;
void dfs(int u,int step){
for(int i=0;i<q[u].size();i++){
int y=q[u][i];
d[y]=step+1;
dfs(y,step+1);
}
}
void solve() {
int n;
cin>>n;
vector<int>a(n+5),b(n+5),w(n+5);
d=vector<int>(n+5);
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1;i<=n;i++){
cin>>b[i];
}
for(int i=1;i<=n;i++){
cin>>w[i];
}
q=vector<vector<int>>(n+5);
vector<int>ans(n+5);
vector<int>in(n+5);
vector<int>vis(n+5,1);
for(int i=1;i<=n;i++){
ans[i]=a[i]%mod;
if(a[b[i]]>a[i]){
ans[i]=(a[i]+w[i])%mod;
}else{
if(b[i]==i){
ans[i]=(a[i])%mod;
vis[i]=0;
}else{
if(a[b[i]]+w[b[i]]<=a[i]){
ans[i]=a[i];
vis[i]=0;
}else{
in[i]++;
q[b[i]].push_back(i);
}
}
}
}
for(int i=1;i<=n;i++){
if(in[i]==0&&vis[i]){
dfs(i,1);
}
}
int mu=A(n,n);
int mu2=ksm(mu,mod-2);
for(int i=1;i<=n;i++){
if(d[i]){
ans[i]=(C(n,d[i])*A(n-d[i],n-d[i])%mod*(a[i]+w[i])%mod+(mu-C(n,d[i])*A(n-d[i],n-d[i])%mod)*a[i]%mod)*mu2%mod;
}
cout<<ans[i]<<" ";
}cout<<endl;
}
signed main() {
std::ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cout<<fixed<<setprecision(11);
int T = 1;
cin >> T;
while (T--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 6ms
memory: 11308kb
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: 140ms
memory: 11600kb
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 -982472957 449261413 -803240278 901433117 519383814 907574792 983760005 984444619 489899014 435736558 113628626 977360756 -517752854 963066959 665922935 577926775 132646723 421298438 601054667 9943...
result:
wrong answer 11th numbers differ - expected: '17527050', found: '-982472957'