QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#622488 | #8821. Nightmare | Crysfly | WA | 553ms | 9332kb | C++14 | 5.3kb | 2024-10-08 22:06:23 | 2024-10-08 22:06:25 |
Judging History
answer
// what is matter? never mind.
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,mmx,avx,avx2")
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
#define ll long long
#define ull unsigned long long
//#define int long long
#define SZ(x) ((int)((x).size()))
#define ALL(x) (x).begin(),(x).end()
using namespace std;
inline int read()
{
char c=getchar();int x=0;bool f=0;
for(;!isdigit(c);c=getchar())f^=!(c^45);
for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
return f?-x:x;
}
int mod;
struct modint{
unsigned int x;
modint(int o=0){x=o;}
modint &operator = (int o){return x=o,*this;}
modint &operator +=(modint o){return x=x+o.x>=mod?x+o.x-mod:x+o.x,*this;}
modint &operator -=(modint o){return x=x<o.x?x-o.x+mod:x-o.x,*this;}
modint &operator *=(modint o){return x=1ll*x*o.x%mod,*this;}
modint &operator ^=(int b){
modint a=*this,c=1;
for(;b;b>>=1,a*=a)if(b&1)c*=a;
return x=c.x,*this;
}
modint &operator /=(modint o){return *this *=o^=mod-2;}
friend modint operator +(modint a,modint b){return a+=b;}
friend modint operator -(modint a,modint b){return a-=b;}
friend modint operator *(modint a,modint b){return a*=b;}
friend modint operator /(modint a,modint b){return a/=b;}
friend modint operator ^(modint a,int b){return a^=b;}
friend bool operator ==(modint a,modint b){return a.x==b.x;}
friend bool operator !=(modint a,modint b){return a.x!=b.x;}
bool operator ! () {return !x;}
modint operator - () {return x?mod-x:0;}
bool operator <(const modint&b)const{return x<b.x;}
};
inline modint qpow(modint x,int y){return x^y;}
vector<modint> fac,ifac,iv;
inline void initC(int n)
{
if(iv.empty())fac=ifac=iv=vector<modint>(2,1);
int m=iv.size(); ++n;
if(m>=n)return;
iv.resize(n),fac.resize(n),ifac.resize(n);
For(i,m,n-1){
iv[i]=iv[mod%i]*(mod-mod/i);
fac[i]=fac[i-1]*i,ifac[i]=ifac[i-1]*iv[i];
}
}
inline modint C(int n,int m){
if(m<0||n<m)return 0;
return initC(n),fac[n]*ifac[m]*ifac[n-m];
}
inline modint sign(int n){return (n&1)?(mod-1):(1);}
#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;
#define maxn 2000005
#define inf 0x3f3f3f3f
int n;
modint g[505][505],res[505][505];
struct oper{
int op,i,j;
modint w;
};
vector<oper>qs;
void add(int i,int j,modint w){
qs.pb({1,i,j,w});
For(k,1,n) if(k!=i && k!=j) g[i][k]+=g[j][k]*w,g[k][i]+=g[k][j]*w;
g[i][i]+=(g[i][j]*w*2+g[j][j]*w*w);
g[i][j]+=g[j][j]*w;
g[j][i]+=g[j][j]*w;
}
void sw(int i,int j){
if(i==j)return;
qs.pb({0,i,j,0});
For(k,1,n) if(k!=i && k!=j) swap(g[i][k],g[j][k]),swap(g[k][i],g[k][j]);
swap(g[i][i],g[j][j]);
}
int sqr[maxn];
int p;
void out(){
reverse(ALL(qs));
for(auto [op,i,j,w]:qs){
if(op==0) swap(res[i],res[j]);
else{
For(k,1,p) res[i][k]-=res[j][k]*w;
}
}
cout<<p<<"\n";
For(i,1,n){
For(j,1,p)cout<<res[i][j].x<<" \n"[j==p];
}
exit(0);
}
signed main()
{
n=read(),mod=read();
For(i,1,n) For(j,1,n) g[i][j]=read();
For(i,0,mod) sqr[i]=-1;
For(i,0,mod-1){
modint t=i; t*=i;
sqr[t.x]=i;
}
bool flag=1;
int sums=0;
p=1;
while(1){
++sums;
if(sums>3000){
puts("????");
cout<<"flag,p "<<flag<<" "<<p<<"\n";
For(i,p,p+2)For(j,p,p+2)cout<<g[i][j].x<<" \n"[j==p+2];
exit(0);
}
bool ok=0;
For(i,p,n){
if(g[i][i].x){
sw(i,p);
modint I=1/g[p][p];
For(j,p+1,n) add(j,p,-I*g[j][p]);
++p; ok=1; break;
}
}
if(ok)continue;
auto find2=[&](){
For(i,p,n) For(j,i+1,n) if(g[i][j].x){
if(sums>2000) cout<<"find2 "<<i<<" "<<j<<"\n";
sw(i,p),sw(j,p+1);
return 1;
}
return 0;
};
if(!find2()) break;
if(mod>2){
add(p,p+1,1);
continue;
}
For(k,p+2,n){
if(g[p][k].x) add(k,p+1,1);
if(g[p+1][k].x) add(k,p,1);
}
if(p>1){
if(sums>2000){
For(k,p-1,p+1) For(kk,p-1,p+1) cout<<g[k][kk].x<<" \n"[kk==p+1]; puts("---");
}
add(p,p-1,1);
add(p+1,p-1,1);
add(p-1,p,1);
add(p-1,p+1,1);
if(sums>2000){
For(k,p-1,p+1) For(kk,p-1,p+1) cout<<g[k][kk].x<<" \n"[kk==p+1]; puts("---");
}
p-=1;
flag=1;
continue;
}
flag=0;
p+=2;
}
--p;
// For(i,1,n){
// For(j,1,n)cout<<g[i][j].x<<" ";
// cout<<"\n";
// }
if(mod==2 && !flag){
++p;
For(i,1,p-1){
For(j,1,i) res[i][j]=1;
res[i][p]=1;
if(i%2==0) res[i][i-1]=0;
}
out();
}
int z=0;
For(i,0,mod-1){
modint y=i; y*=i,y+=1;
if(sqr[y.x]==-1) z=i;
}
int lst=0;
For(i,1,p){
if(sqr[g[i][i].x]!=-1) res[i][i]=sqr[g[i][i].x];//cout<<"isok "<<i<<"\n";
else if(!lst) lst=i;
else {
int j=lst; lst=0;
res[i][i]=z;
res[j][j]=z;
res[i][j]=1;
res[j][i]=mod-1;
modint zz=sqr[(g[i][i]/(z*z+1)).x];
res[i][j]*=zz,res[i][i]*=zz;
zz=sqr[(g[j][j]/(z*z+1)).x];
res[j][j]*=zz,res[j][i]*=zz;
}
}
if(lst){
++p;
int i=lst;
res[i][i]=g[i][i];
res[i][p]=g[i][i]*z;
modint zz=sqr[(res[i][i]/(z*z+1)).x];
res[i][i]*=zz,res[i][p]*=zz;
}
// For(i,1,n) For(j,1,p) cout<<res[i][j].x<<" \n"[j==p];
out();
return 0;
}
/*
5 2
1 0 0 0 0
0 0 1 0 0
0 1 0 0 0
0 0 0 0 1
0 0 0 1 0
2 2
0 1
1 0
3 5
4 4 3
4 4 3
3 3 2
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 7344kb
input:
2 2 1 1 1 1
output:
1 1 1
result:
ok accepted
Test #2:
score: 0
Accepted
time: 0ms
memory: 6032kb
input:
3 5 4 4 3 4 4 3 3 3 2
output:
2 3 0 3 0 1 4
result:
ok accepted
Test #3:
score: 0
Accepted
time: 513ms
memory: 9032kb
input:
500 2 1 0 1 1 1 1 1 0 0 0 0 0 1 1 0 0 0 0 1 0 0 1 0 0 1 0 0 0 0 0 1 0 0 1 0 1 1 0 1 0 1 0 1 1 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 1 1 0 1 1 0 1 1 1 1 1 0 1 1 0 1 1 1 0 1 1 1 1 0 0 1 0 0 1 1 0 1 0 0 0 1 1 0 0 1 0 1 1 1 1 0 1 1 1 0 1 1 1 1 0 1 1 1 0 1 0 1 1 0 0 0 1 0 1 0 1 1 1 1 0 0 1 0 1 0 0 ...
output:
423 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
ok acc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0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
ok accepted
Test #9:
score: -100
Wrong Answer
time: 38ms
memory: 7772kb
input:
500 2 0 1 1 0 0 1 0 1 0 1 1 1 0 0 1 1 1 0 1 1 1 1 1 1 1 0 1 0 0 0 0 1 1 1 1 1 0 0 1 0 1 0 1 0 0 0 1 0 1 1 0 1 1 0 1 1 1 0 1 0 1 0 1 0 0 0 1 0 0 1 1 1 1 1 0 0 0 1 0 0 1 1 0 1 0 1 0 0 0 1 0 1 0 0 0 0 1 0 0 1 1 0 1 0 0 1 0 0 0 1 1 1 0 0 1 0 0 0 1 0 1 1 0 0 0 1 1 1 0 0 0 0 0 1 0 1 1 0 1 1 1 0 0 1 1 1 1 ...
output:
find2 2 3 0 1 1 1 0 1 1 1 0 --- 0 0 0 0 0 1 0 1 0 --- find2 1 4 find2 3 4 0 0 0 0 0 1 0 1 0 --- 0 1 1 1 0 1 1 1 0 --- find2 2 3 0 1 1 1 0 1 1 1 0 --- 0 0 0 0 0 1 0 1 0 --- find2 1 4 find2 3 4 0 0 0 0 0 1 0 1 0 --- 0 1 1 1 0 1 1 1 0 --- find2 2 3 0 1 1 1 0 1 1 1 0 --- 0 0 0 0 0 1 0 1 0 --- find2 1 4 ...
result:
wrong output format Expected integer, but "find2" found