QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#454527 | #8821. Nightmare | PhantomThreshold | WA | 573ms | 15028kb | C++17 | 2.9kb | 2024-06-25 01:18:04 | 2024-06-25 01:18:04 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,mod;
inline ll ksm(ll a,ll x){
ll ret=1;
for (;x;x>>=1,a=a*a%mod) if (x&1) ret=ret*a%mod;
return ret;
}
inline ll inv(ll a){return ksm(a,mod-2);}
inline void add(ll &A,ll B){A+=B;if (A>=mod) A-=mod;}
inline void sub(ll &A,ll B){A-=B;if (A<0) A+=mod;}
struct matrix{
ll m[505][505];
matrix(){
memset(m,0,sizeof(m));
}
void diag(){
for (int i=1;i<=n;i++){
for (int j=1;j<=n;j++){
m[i][j]=(i==j);
}
}
}
friend matrix operator * (const matrix &A,const matrix &B){
matrix C;
for (int i=1;i<=n;i++){
for (int k=1;k<=n;k++){
for (int j=1;j<=n;j++) (C.m[i][j]+=A.m[i][k]*B.m[k][j])%=mod;
}
}
return C;
}
void add_row(int x,int y,ll k){
for (int i=1;i<=n;i++) (m[y][i]+=m[x][i]*k)%=mod;
}
void add_col(int x,int y,ll k){
for (int i=1;i<=n;i++) (m[i][y]+=m[i][x]*k)%=mod;
}
void swap_row(int x,int y){
for (int i=1;i<=n;i++) swap(m[y][i],m[x][i]);
}
void swap_col(int x,int y){
for (int i=1;i<=n;i++) swap(m[i][y],m[i][x]);
}
void mul_row(int x,ll k){
for (int i=1;i<=n;i++) m[x][i]=m[x][i]*k%mod;
}
void mul_col(int x,ll k){
for (int i=1;i<=n;i++) m[i][x]=m[i][x]*k%mod;
}
void turn(){
for (int i=1;i<=n;i++){
for (int j=i+1;j<=n;j++){
swap(m[i][j],m[j][i]);
}
}
}
void display(){
for (int i=1;i<=n;i++){
for (int j=1;j<=n;j++){
cerr << m[i][j] << " ";
}
cerr << endl;
}
}
};
matrix G,oriG;
matrix P;
matrix tmp,PTP;
const int maxn=1000000;
ll sq[maxn+50];
int main(){
// cerr << "ok" << endl;
cin >> n >> mod;
for (int i=0;i<=mod/2;i++) sq[i*i%mod]=i;
for (int i=1;i<=n;i++){
for (int j=1;j<=n;j++){
cin >> oriG.m[i][j];
G.m[i][j]=oriG.m[i][j];
}
}
P.diag();
int rk=0;
for (int i=1;i<=n;i++){
if (G.m[i][i]==0){
int pos=0;
for (int j=i+1;j<=n;j++){
if (G.m[j][j]!=0){
pos=j;
break;
}
}
if (pos==0){
rk=i-1;
break;
}
P.swap_col(i,pos);
G.swap_row(i,pos);
G.swap_col(i,pos);
}
ll iv=inv(G.m[i][i]);
for (int j=i+1;j<=n;j++){
ll coef=iv*G.m[j][i]%mod;
P.add_col(j,i,coef);
G.add_row(i,j,mod-coef);
G.add_col(i,j,mod-coef);
}
// cerr << "----------------------" << endl;
// cerr << "i : " << i << endl;
// cerr << "G : " << endl;
// G.display();
// cerr << "P : " << endl;
// P.display();
ll sqiv=sq[iv];
P.mul_col(i,sqiv);
G.mul_row(i,sqiv);
G.mul_col(i,sqiv);
// cerr << "G2 : " << endl;
// G.display();
// cerr << "P2 : " << endl;
// P.display();
}
for (int i=1;i<=n;i++){
for (int j=rk+1;j<=n;j++){
P.m[i][j]=0;
}
}
cout << rk << "\n";
for (int i=1;i<=n;i++){
for (int j=1;j<=rk;j++){
cout << P.m[i][j] << " \n"[j==rk];
}
}
// tmp=P;
// tmp.turn();
// PTP=P*tmp;
// cerr << "----------------" << endl;
// PTP.display();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 15028kb
input:
2 2 1 1 1 1
output:
1 1 1
result:
ok accepted
Test #2:
score: 0
Accepted
time: 2ms
memory: 14644kb
input:
3 5 4 4 3 4 4 3 3 3 2
output:
2 2 0 2 0 4 1
result:
ok accepted
Test #3:
score: 0
Accepted
time: 559ms
memory: 13852kb
input:
500 2 1 0 1 1 1 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