QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#211078 | #59. Determinant of A+Bz | Thankto_zy6 | 0 | 0ms | 0kb | C++14 | 5.2kb | 2023-10-12 08:33:40 | 2024-03-04 04:46:38 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int mod=998244353;
class mint{
private:
int x;
long long ksm(long long w,int y)
{
if(!y)return 1;
if(y%2==0)return ksm(w*w%mod,y>>1);
return w*ksm(w*w%mod,y>>1)%mod;
}
public:
void read()
{
int fl=0;x=0;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')fl=1;
ch=getchar();
}
while('0'<=ch&&ch<='9')
{
x=(10ll*x+ch-'0')%mod;
ch=getchar();
}
if(fl&&x)ch=mod-x;
}
void print(){cout<<x;}
void operator=(long long y)
{
x=y%mod;
if(x<0)x+=mod;
}
mint operator+(mint y)const{mint a;a.x=x+y.x;if(a.x>=mod)a.x-=mod;return a;}
mint operator~(void){mint a;a.x=x;if(a.x)a.x=mod-a.x;return a;}
mint operator-(mint y)const{mint a;a=x;return a+(~y);}
mint operator*(mint y)const{mint a;a.x=1ll*x*y.x%mod;return a;}
mint inv(){mint a;a.x=ksm(x,mod-2);return a;}
mint operator/(mint y)const{mint a;a=x;return a*(y.inv());}
int val(){return x;}
mint &operator++(void){x++;if(x==mod)x=0;return *this;}
mint &operator--(void){x--;if(x==-1)x=mod-1;return *this;}
mint &operator+=(mint y){x+=y.x;if(x>=mod)x-=mod;return *this;}
mint &operator-=(mint y){x-=y.x;if(x<0)x+=mod;return *this;}
mint &operator*=(mint y){x=1ll*x*y.x%mod;return *this;}
mint &operator/=(mint y){x=1ll*x*(y.inv()).x%mod;return *this;}
friend istream &operator >>(istream&,mint&);
friend ostream &operator <<(ostream&,mint);
};
istream &operator >>(istream& input,mint& x){
x.read();
return input;
}
ostream &operator <<(ostream& output,mint x){
x.print();
return output;
}
mint m0[505][505],m1[505][505];
mint a[505][505];
mint pl[505][505];
mint ans[505];
//求a的特征多项式
void char_poly(int n)
{
//hessenberg
for(int i=1;i<=n;i++)
{
int p=-1;
for(int j=i+1;j<=n;j++)
{
if(a[j][i].val())
{
p=j;
break;
}
}
if(p==-1)continue;
swap(a[i+1],a[p]);
for(int j=1;j<=n;j++)swap(a[j][i+1],a[j][p]);
for(int j=i+2;j<=n;j++)
{
mint v=a[j][i]/a[i+1][i];
for(int k=1;k<=n;k++)a[j][k]-=a[i+1][k]*v;
for(int k=1;k<=n;k++)a[k][i+1]+=a[k][j]*v;
}
}
pl[0][0]=1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<i;j++)
{
mint v=~a[j][i];
for(int k=j+1;k<=i;k++)v*=a[k][k-1];
for(int k=0;k<=n;k++)pl[i][k]+=pl[j-1][k]*v;
}
for(int j=1;j<=i;j++)pl[i][j]+=pl[i-1][j-1];
for(int j=0;j<=i;j++)pl[i][j]-=pl[i-1][j]*a[i][i];
}
for(int i=0;i<=n;i++)ans[i]=pl[n][i];
}
mint g[505][505];
mint h[505][505];
//h=g^-1
void matriv(int n)
{
for(int i=1;i<=n;i++)h[i][i]=1;
for(int i=1;i<=n;i++)
{
int p=-1;
for(int j=i;j<=n;j++)
{
if(g[i][j].val())
{
p=j;
break;
}
}
for(int j=1;j<=n;j++)
{
swap(g[i][j],g[p][j]);
swap(h[i][j],h[p][j]);
}
mint iv=g[i][i].inv();
for(int j=1;j<=n;j++)
{
g[i][j]=g[i][j]*iv;
h[i][j]=h[i][j]*iv;
}
if(p==-1)continue;
for(int j=i+1;j<=n;j++)
{
mint v=g[j][i];
for(int k=1;k<=n;k++)
{
g[j][k]-=g[i][k]*v;
h[j][k]-=h[i][k]*v;
}
}
}
for(int i=n;i>=1;i--)
{
for(int j=i-1;j>=1;j--)
{
mint v=g[j][i];
for(int k=1;k<=n;k++)
{
g[j][k]-=g[i][k]*v;
h[j][k]-=h[i][k]*v;
}
}
}
}
mint r[505][505];
//r=g*h
void product(int n)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
r[i][j]=0;
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
for(int k=1;k<=n;k++)
{
r[i][j]+=g[i][k]*h[k][j];
}
}
}
}
//det of h
mint det(int n)
{
mint ret;
ret=1;
for(int i=1;i<=n;i++)
{
int p=-1;
for(int j=1;j<=n;j++)
{
if(h[i][j].val())
{
p=j;
break;
}
}
if(p==-1)continue;
if(p!=i)
{
swap(h[i],h[p]);
ret=~ret;
}
for(int j=i+1;j<=n;j++)
{
mint v=h[j][i]/h[i][i];
for(int k=1;k<=n;k++)
{
h[j][k]-=h[i][k]*v;
}
}
}
for(int i=1;i<=n;i++)ret*=h[i][i];
return ret;
}
mt19937 rnd(time(0));
mint sum[505];
mint cho[505][505];
mint mi[505];
int main()
{
int n;
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
cin>>n;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cin>>m0[i][j];
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cin>>m1[i][j];
}
}
mint w;
w=rnd()%(mod-1)+1;
// w=1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
g[i][j]=m0[i][j]-w*m1[i][j];
}
}
matriv(n);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
g[i][j]=m1[i][j];
}
}
product(n);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
a[i][j]=~r[i][j];
}
}
char_poly(n);
mint _v=det(n).inv();
for(int i=0;i<=n;i++)sum[i]=ans[n-i]*_v;
// for(int i=0;i<=n;i++)cout<<sum[i]<<" ";
// cout<<endl;
cho[0][0]=1;
for(int i=1;i<=n;i++)
{
cho[i][0]=1;
for(int j=1;j<=i;j++)
{
cho[i][j]=cho[i-1][j-1]+cho[i-1][j];
}
}
mi[0]=1;
for(int i=1;i<=n;i++)mi[i]=mi[i-1]*w;
for(int i=1;i<=n;i++)ans[i]=0;
ans[0]=sum[0];
for(int i=1;i<=n;i++)
{
for(int j=0;j<=i;j++)
{
ans[j]+=mi[i-j]*cho[i][j]*sum[i];
}
}
for(int i=0;i<=n;i++)cout<<ans[i].val()<<" ";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Time Limit Exceeded
Test #1:
score: 0
Time Limit Exceeded
input:
50 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 179093706 0 0 0 0 0 873235003 0 873022990 0 0 0 0 150372208 0 0 0 0 0 0 0 0 540031202 441544333 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 30490606 0 23238599 0 0 0 0 0 0 0 0 0 0 0 0 0...
output:
result:
Subtask #2:
score: 0
Time Limit Exceeded
Test #51:
score: 0
Time Limit Exceeded
input:
500 0 0 0 0 0 482890969 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 1077401 0 0 210804413 0 508044994 0 0 0 398405351 0 0 0 0 0 0 0 171917316 0 0 0 0 0 0 0 0 0 0 0 429230725 0 0 0 0 0 45665526 0 0 0 925078893 0 0 396149045 0 0 0 595858405 0 0 0 0 0 57208...
output:
result:
Subtask #3:
score: 0
Skipped
Dependency #1:
0%