QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#854191 | #9444. Again Permutation Problem | ucup-team134# | WA | 2ms | 3960kb | C++14 | 5.3kb | 2025-01-11 22:17:39 | 2025-01-11 22:17:43 |
Judging History
answer
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define f first
#define s second
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL)
#define ld long double
using namespace std;
mt19937 rng(time(NULL));
const int mod=998244353;
int add(int a,int b){
a+=b;
if(a>=mod)
a-=mod;
return a;
}
int sub(int a,int b){
a-=b;
if(a<0)
a+=mod;
return a;
}
int mul(int a,int b){
return (long long)a*b%mod;
}
int pwrmod(int x,int k){
int ans=1;
for(;k;k>>=1,x=mul(x,x))
if(k&1)
ans=mul(ans,x);
return ans;
}
int inv(int x){
return pwrmod(x,mod-2);
}
const int N=30;
int p[N];
set<vector<int>> cnt[N];
bool hasE[N];
vector<int> op[N];
int find(int tr){return tr==p[tr]?tr:p[tr]=find(p[tr]);}
void merge(int a,int b){
a=find(a);
b=find(b);
if(a==b)return;
p[a]=b;
for(auto p:cnt[a])cnt[b].insert(p);
hasE[b]=hasE[b]||hasE[a];
for(auto p:op[a])op[b].pb(p);
}
int fact[N],ifact[N];
bool even(vector<int> &p){
int cnt=0;
for(int i=0;i<sz(p);i++){
for(int j=i+1;j<sz(p);j++){
if(p[i]>p[j])cnt++;
}
}
return cnt%2==0;
}
vector<int> ap(vector<int> tr,vector<int> prm){
vector<int> novi(sz(tr));
vector<bool> dn(sz(tr));
for(int i=0;i<sz(tr);i++){
if(!dn[i]){
int t=i;
while(1){
dn[t]=1;
novi[t]=tr[prm[t]];
if(prm[t]==i)break;
t=prm[t];
}
}
}
return novi;
}
int main()
{
fact[0]=1;
for(int i=1;i<N;i++)fact[i]=mul(fact[i-1],i);
for(int i=0;i<N;i++)ifact[i]=pwrmod(fact[i],mod-2);
for(int i=0;i<N;i++)op[i].pb(i);
int n,m;
cin >> n >> m;
vector<int> ways(n,-1);
for(int i=0;i<n;i++)p[i]=i;
for(int k=0;k<m;k++){
vector<int> p(n);
for(int i=0;i<n;i++)cin >> p[i],p[i]--;
vector<bool> vs(n);
for(int i=0;i<n;i++){
if(!vs[i]){
vector<int> my={};
vs[i]=1;
int tr=p[i];
while(tr!=i){
my.pb(tr);
vs[tr]=1;
tr=p[tr];
}
my.pb(i);
for(int i=0;i<sz(my)-1;i++){
merge(my[i],my[i+1]);
}
if(sz(my)!=1)
cnt[find(i)].insert(p);
if(sz(my)%2==0){
hasE[find(i)]=1;
}
}
}
}
int inv2=inv(2);
for(int i=0;i<N;i++)sort(all(op[i]));
vector<vector<vector<int>>> ww(n);
vector<int> ost;
for(int i=0;i<n;i++){
if(ways[find(i)]==-1){
int my=find(i);
ways[my]=1;
int nums=sz(op[my]);
int cn=0;
map<vector<int>,int> vis;
queue<vector<int>> q;
vector<int> st(n,-1);
for(auto p:op[my])st[p]=p;
q.push(st);
vis[st]=1;
cn++;
vector<vector<int>> opcije;
for(auto p:cnt[my])opcije.pb(p);
while(sz(q)&&cn<=10*nums){
auto t=q.front();
/*for(auto p:t){
printf("%i ",p);
}
printf("\n");*/
q.pop();
for(auto tt:opcije){
/*for(auto p:tt){
printf("%i ",p);
}
printf("\n");*/
auto nv=ap(t,tt);
if(!vis[nv]){
vis[nv]=1;
q.push(nv);
cn++;
}
}
}
if(cn<=10*nums){
//printf("%i!\n",cn);
for(auto p:vis){
ww[my].pb(p.f);
}
ways[my]=cn;
}
else{
assert(sz(cnt[my])>1);
if(hasE[my]){
ways[my]=fact[nums];
}
else{
ways[my]=mul(fact[nums],inv2);
}
}
ost.pb(my);
}
}
int ans=0;
for(int i=0;i<n;i++){
for(int j=0;j<i;j++){
for(int pi=0;pi<n;pi++){
if(find(i)!=find(p[i]))continue;
for(int pj=pi+1;pj<n;pj++){
if(find(j)!=find(p[j]))continue;
int ci=find(i),cj=find(j);
int ws=1;
if(ci!=cj){
if(sz(ww[ci])){
int my=0;
int myInd=0;
for(int o=0;o<sz(op[ci]);o++){
if(op[ci][o]==pi){
myInd=o;
}
}
for(auto p:ww[ci]){
if(p[myInd]==i){
my++;
}
}
ws=mul(ws,my);
}
else{
if(hasE[ci]){
ws=mul(ws,fact[sz(op[ci])-1]);
}
else{
ws=mul(ws,mul(fact[sz(op[ci])-1],inv2));
}
}
if(sz(ww[cj])){
int my=0;
int myInd=0;
for(int o=0;o<sz(op[cj]);o++){
if(op[cj][o]==pj){
myInd=o;
}
}
for(auto p:ww[cj]){
if(p[myInd]==j){
my++;
}
}
ws=mul(ws,my);
}
else{
if(hasE[cj]){
ws=mul(ws,fact[sz(op[cj])-1]);
}
else{
ws=mul(ws,mul(fact[sz(op[cj])-1],inv2));
}
}
}
else{
//printf("Isto!\n");
if(sz(ww[ci])){
int posI,posJ;
for(int o=0;o<sz(op[ci]);o++){
if(op[ci][o]==pi){
posI=o;
}
if(op[ci][o]==pj){
posJ=o;
}
}
int cn=0;
for(auto p:ww[ci]){
if(p[posI]==i&&p[posJ]==j){
cn++;
}
}
ws=cn;
}
else{
// More than 1 cycle
if(hasE[ci]){
ws=fact[sz(op[ci])-2];
}
else{
ws=mul(fact[sz(op[ci])-2],inv2);
}
}
}
int old=ws;
for(auto p:ost){
if(p!=ci&&p!=cj){
ws=mul(ws,ways[p]);
}
}
//printf("%i %i %i %i: %i (%i)\n",i,j,pi,pj,ws,old);
ans=add(ans,ws);
}
}
}
}
printf("%i\n",ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3860kb
input:
3 2 1 2 3 2 3 1
output:
4
result:
ok 1 number(s): "4"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3844kb
input:
5 2 3 4 5 1 2 1 5 4 3 2
output:
50
result:
ok 1 number(s): "50"
Test #3:
score: -100
Wrong Answer
time: 2ms
memory: 3960kb
input:
30 12 1 2 9 4 5 6 7 8 3 10 11 12 19 14 15 25 17 18 20 26 21 22 23 24 16 29 27 28 13 30 9 2 27 4 5 10 7 8 1 25 11 12 24 14 15 16 17 18 19 20 21 22 23 28 6 26 3 13 29 30 1 5 3 29 2 6 7 8 9 10 11 12 13 16 15 18 17 14 19 20 21 22 28 27 25 26 24 23 4 30 7 2 3 25 5 6 1 28 21 15 11 12 13 14 10 17 16 18 19 ...
output:
404585645
result:
wrong answer 1st numbers differ - expected: '701414999', found: '404585645'