QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#760569 | #5047. Permutation | qwqUwU_ | WA | 16ms | 12112kb | C++14 | 2.1kb | 2024-11-18 17:37:58 | 2024-11-18 17:37:58 |
Judging History
answer
#include<bits/stdc++.h>
#define pb push_back
#define P make_pair
#define fi first
#define se second
#define bit(s,x) (((s)>>(x))&1)
#define pnp(s) __builtin_popcountll(s)
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
inline ll read(){
ll x=0,f=1,c=getchar();
while(c<'0'||c>'9')f=(c=='-'?-1:1),c=getchar();
while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
return x*f;
}
const int mod=998244353;
inline int fp(int x,ll p=mod-2,int m=mod,int res=1){
for(;p;p>>=1){if(p&1)res=1ll*res*x%m;x=1ll*x*x%m;}
return res;
}
inline void Ad(int &x,int y){x=x+y>=mod?x+y-mod:x+y;}
inline void Mi(int &x,int y){x=x-y<0?x-y+mod:x-y;}
const int N=5e5+3;
int n,C,a[N],rt[N],q[N],fac[N],p[N];
const int M=1e7;
int t[M],son[M][2],tt;
inline void ad(int &x,int l,int r,int nx){
int p=++tt;
t[p]=t[x]+1,son[p][0]=son[x][0],son[p][1]=son[x][1];
x=p;
if(l==r)return ;int mid=(l+r)>>1;
if(nx<=mid)ad(son[x][0],l,mid,nx);
else ad(son[x][1],mid+1,r,nx);
}
inline int qr(int x,int y,int l,int r,int k){
if(l==r)return q[l];int mid=(l+r)>>1;
if(t[son[x][0]]-t[son[y][0]]>=k)return qr(son[x][0],son[y][0],l,mid,k);
return qr(son[x][1],son[y][1],mid+1,r,k-t[son[x][0]]-t[son[y][0]]);
}
inline int solve(int l,int r,int a,int b){
if(r-l+1<=C)return 1;
int x=qr(rt[r],rt[l-1],1,n,a+b-1),lp=l+(a-1)*C,rp=r-(b-1)*C;
if(x>lp&&x<rp)return 1ll*solve(l,x,a,1)*solve(x,r,1,b)%mod;
if(lp+C>=rp){
if(x<=lp)++a;
else ++b;
return 1ll*p[a-1]*p[b-1]%mod*fac[r-l+1-a-b+2]%mod;
}
if(x<=lp)return solve(l,r,a+1,b);
if(x>=rp)return solve(l,r,a,b+1);
}
inline void solve(){
n=read(),C=read();tt=0;
int cnt=1; p[0]=1;
rep(i,1,n){
p[i]=1ll*p[i-1]*cnt%mod;
Ad(cnt,C-1);
}
rep(i,1,n)ad(rt[i]=rt[i-1],1,n,a[i]=read()),q[a[i]]=i;
printf("%d\n",solve(1,n,1,1));
}
int main() {
//freopen("data.in", "r", stdin);
// freopen("myans.out","w",stdout);
fac[0]=1;rep(i,1,N-1)fac[i]=1ll*fac[i-1]*i%mod;
int T=read();while(T--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 11904kb
input:
5 5 3 3 4 2 1 5 5 4 4 2 1 3 5 5 2 4 5 3 1 2 5 3 4 3 2 1 5 5 2 2 3 1 5 4
output:
6 1 4 6 4
result:
ok 5 number(s): "6 1 4 6 4"
Test #2:
score: 0
Accepted
time: 16ms
memory: 12112kb
input:
100000 5 4 1 3 2 5 4 5 3 5 1 4 2 3 5 2 1 4 5 3 2 5 4 5 2 4 3 1 5 4 2 5 4 1 3 5 4 1 2 3 5 4 5 4 4 3 2 5 1 5 3 1 5 4 3 2 5 3 3 2 5 4 1 5 4 4 3 1 5 2 5 4 4 3 5 2 1 5 2 3 2 1 4 5 5 3 2 4 5 1 3 5 3 2 1 4 3 5 5 3 2 1 5 4 3 5 2 2 1 3 4 5 5 4 2 3 1 4 5 5 2 1 2 4 5 3 5 3 2 4 1 5 3 5 3 2 4 3 5 1 5 3 4 1 3 5 2...
output:
24 6 6 24 1 24 24 6 18 1 24 4 6 6 6 4 1 12 1 6 6 24 18 2 18 4 6 6 18 6 4 1 6 18 1 6 24 18 6 1 12 18 6 4 2 24 12 4 24 4 4 24 6 1 1 1 1 6 1 4 1 18 1 18 4 4 6 24 6 4 6 1 12 1 4 4 6 24 18 6 2 6 1 12 6 24 1 4 6 1 1 6 1 1 24 12 18 1 4 18 1 4 24 6 4 24 6 24 1 1 6 1 18 24 1 4 1 1 2 6 1 6 4 18 1 24 6 6 4 24 ...
result:
ok 100000 numbers
Test #3:
score: -100
Wrong Answer
time: 7ms
memory: 11904kb
input:
10000 10 2 7 4 2 9 1 3 10 5 8 6 10 7 10 6 1 5 2 4 7 9 8 3 10 4 10 7 6 2 8 1 5 4 3 9 10 4 4 9 6 2 10 7 8 5 1 3 10 5 9 8 7 4 5 3 2 10 6 1 10 3 5 7 8 1 9 4 6 10 3 2 10 4 6 2 10 8 4 7 9 5 3 1 10 5 10 1 3 7 5 9 4 2 8 6 10 7 10 3 4 5 6 1 9 2 8 7 10 4 1 8 9 7 5 6 3 10 4 2 10 3 6 7 3 1 9 4 10 8 5 2 10 3 5 1...
output:
432 5040 2304 24 201600 720 5040 120 1 20160 720 120 1 360 120 120 2160 141120 192 1 480 1 108 24 1 432 25200 1 30240 35280 35280 720 1800 576 1296 120 35280 20160 432 432 8 201600 192 432 141120 201600 2304 720 2880 576 1 120 201600 576 360 241920 40320 480 1 1 1 35280 1 1 1 3600 720 108 720 1 1296...
result:
wrong answer 58th numbers differ - expected: '24', found: '480'