QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#416492 | #8237. Sugar Sweet II | Godwang# | TL | 2ms | 18624kb | C++14 | 3.5kb | 2024-05-21 21:40:52 | 2024-05-21 21:40:53 |
Judging History
answer
#include <iostream>
using namespace std;
#include <set>
#include <algorithm>
#include <cmath>
#include <map>
#include <cstdio>
#include <string>
#include <cstring>
#include <string.h>
#include <stdlib.h>
#include <iomanip>
#include <fstream>
#include <stdio.h>
#include <stack>
#include <queue>
#include <ctype.h>
#include <vector>
#include <random>
#define ll long long
#define ull unsigned long long
#define pb push_back
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, a, n) for (int i = n; i >= a; i--)
#define pii pair<int, int>
#define pli pair<ll, int>
#define pil pair<int, ll>
#define pll pair<ll ,ll >
#define endl '\n'
const double pai = acos(-1);
ll extend_gcd(ll a, ll b, ll &x, ll &y)
{
if (b == 0)
{
x = 1;
y = 0;
return a;
}
ll d = extend_gcd(b, a % b, y, x);
y -= a / b * x;
return d;
}
ll fastpow(ll a, ll n, ll mod)
{
ll ans = 1;
a %= mod;
while (n)
{
if (n & 1)
ans = (ans * a)%mod; //% mod
a = (a * a)%mod; //% mod
n >>= 1;
}
return ans;
}
int dir[4][2] =
{
{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; // d a w s
const ll inf = 1000000000000000000ll;
const ll mod = 1e9 + 7, P1 = 13331;
const double eps = 1e-7;
const int N = 5e5 + 10, M = 1e6 + 10;
ll inverse(ll a)
{
return fastpow(a,mod-2,mod);
}
ll jiecheng[N];
ll gailv[N];
int t,n;
ll a[N],w[N];
ll b[N];
int zhuangtai[N];
bool vis[N];
stack<int > st;
ll qiwang[N];
void dfs(int u)
{
// cout<<"CUR: "<<u<<endl;
st.push(u);
if(a[b[u]]>a[u])//yiding
{
int temp=0;
while(st.size())
{
//cout<<st.top()<<endl;
temp++;
ll jia=w[st.top()]*inverse(jiecheng[temp])%mod;
qiwang[st.top()]=jia;
zhuangtai[st.top()]=2;
st.pop();
}
return;
}
if( a[b[u]]+w[b[u]]<=a[u] )//bukeneng
{
while(st.size())
{
zhuangtai[st.top()]=-1;
st.pop();
}
return;
}
// cout<<"CUR:xiamian "<<u<<endl;
if(vis[u]&&zhuangtai[u]==0)//huan
{
while(st.size())
{
zhuangtai[st.top()]=-1;
st.pop();
}
return;
}
//
//cout<<u<<endl;
vis[u]=1;
dfs(b[u]);
}
int main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
// freopen("ain.txt", "r", stdin);freopen("aout.txt", "w", stdout);
jiecheng[0]=1ll;
rep(i,1,500000)
{
jiecheng[i]=jiecheng[i-1]*1ll*i;
jiecheng[i]%=mod;
//
}
cin>>t;
while(t--)
{
cin>>n;
fill(vis+1,vis+n+1,0);
fill(zhuangtai+1,zhuangtai+n+1,0);
fill(gailv+1,gailv+n+1,0);
fill(qiwang+1,qiwang+n+1,0);
rep(i,1,n)
{
cin>>a[i];
}
rep(i,1,n)
{
cin>>b[i];
}
rep(i,1,n)
{
cin>>w[i];
}
rep(i,1,n)
{
if(zhuangtai[i]||vis[i])
{
continue;
}
dfs(i);
}
rep(i,1,n)
{
qiwang[i]+=a[i];
qiwang[i]%=mod;
cout<<qiwang[i]<<" ";
}
cout<<endl;
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 18624kb
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...