QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#447087 | #7612. Matrix Inverse | Iratis | TL | 0ms | 4096kb | C++14 | 3.1kb | 2024-06-17 21:16:58 | 2024-06-17 21:16:58 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define md(a) a=(a%mod+mod)%mod
#define file(a) freopen(#a".in","r",stdin);freopen(#a".out","w",stdout)
namespace IO {
const int SIZE=(1<<21)+1;
char ibuf[SIZE],*iS,*iT,obuf[SIZE],*oS=obuf,*oT=oS+SIZE-1,c,qu[55];int f, qr;
#define gc() (iS==iT?(iT=(iS=ibuf)+fread(ibuf,1,SIZE,stdin),(iS==iT?EOF:*iS++)):*iS++)
inline void flush(){fwrite(obuf,1,oS-obuf,stdout),oS=obuf;}
inline void putc(char x){*oS++=x;if(oS==oT)flush();}
template <class I>
inline void read(I &x){
for(f=1,c=gc();c<'0'||c>'9';c=gc())if(c=='-')f=-1;
for(x=0;c<='9'&&c>='0';c=gc())x=x*10+(c&15);x*=f;
}
template <class I>
inline void print(I x){
if(!x)putc('0');if(x<0)putc('-'),x=-x;
while(x)qu[++qr]=x%10+'0',x/=10;
while(qr)putc(qu[qr --]);
}
struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
}
using IO::read;
using IO::putc;
using IO::print;
bool ST;
const int N=2005,mod=1e9+7;
namespace Math
{
inline void add(int &x,const int &y){x+=y;if(x>=mod)x-=mod;}
inline void dec(int &x,const int &y){x-=y;if(x<0)x+=mod;}
inline int qp(int a,int n){int b=1;while(n){if(n&1)b=1ll*b*a%mod;a=1ll*a*a%mod,n>>=1;}return b;}
};
using namespace Math;int ned,dir[N],dic[N],dif[N][N];
int n,a[N][N],b[N][N],vec[N],row[N],col[N];mt19937 R(time(0));
inline void rmul(int*a,int b[N][N])
{
static int c[N];
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)add(c[j],1ll*a[i]*b[i][j]%mod);
memcpy(a,c,sizeof(c)),memset(c,0,sizeof(c));
}
inline void lmul(int b[N][N],int*a)
{
static int c[N];
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)add(c[j],1ll*a[i]*b[j][1]%mod);
memcpy(a,c,sizeof(c)),memset(c,0,sizeof(c));
}
int f[N][14];
inline void calc(int c)
{
if(!ned)return ;
for(int i=1;i<=n;i++)
{
f[i][ned+1]=(i==c);
for(int j=1,k=1;j<=n;j++)
{
if(k<=ned&&dir[k]==j)f[i][k]=a[i][j],k++;
else dec(f[i][ned+1],1ll*a[i][j]*b[j][c]%mod);
}
}
for(int i=1;i<=min(n,ned);i++)
{
for(int j=i;j<=n;j++)if(f[j][i]){for(int k=1;k<=ned+1;k++)swap(f[i][k],f[j][k]);break;}
int iv=qp(f[i][i],mod-2);
for(int j=1;j<=n;j++)if(i!=j)
{
int w=1ll*f[j][i]*iv%mod;if(w<0)w=mod-w;
for(int k=1;k<=ned+1;k++)add(f[j][k],1ll*f[i][k]*w%mod);
}
}
for(int i=1;i<=ned;i++)dif[dir[i]][c]=1ll*f[i][ned+1]*qp(f[i][i],mod-2)%mod;
}
bool ED;
signed main()
{
int time_st=clock();
cerr<<(&ST-&ED)/1024.0/1024<<endl;ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
read(n);for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)read(a[i][j]);
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)read(b[i][j]),dif[i][j]=b[i][j];
for(int i=1;i<=n;i++)vec[i]=R(),row[i]=col[i]=vec[i];rmul(row,a),rmul(row,b),lmul(a,col),lmul(b,col);
for(int i=1,p=0,q=0;i<=n;i++){if(row[i]!=vec[i])dic[++q]=i;if(col[i]!=vec[i])dir[++p]=i;ned=max(p,q);}
for(int i=1;i<=ned;i++)calc(dic[i]);
vector<pair<int,int> >ans;for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)if(dif[i][j]!=b[i][j])ans.push_back({i,j});
cout<<(int)ans.size()<<'\n';
for(auto pa:ans){int x=pa.first,y=pa.second;print(x),putc(' '),print(y),putc(' '),print(dif[x][y]),putc('\n');}
cerr<<(clock()-time_st)/1e6<<endl;return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 4096kb
input:
1 953176428 107682094
output:
0
result:
ok single line: '0'
Test #2:
score: -100
Time Limit Exceeded
input:
1995 586309310 548144807 578573993 437893403 641164340 712256053 172321263 108058526 768610920 123320669 762746291 856047593 979279376 29067913 309867338 292286426 45124325 239705174 675003623 213743652 620561338 116308277 695369179 669459894 682522334 846995555 159510341 999359657 645579085 7499563...