【2020省选模拟】题解
T1:
完全没想到系数就是
只会求逆矩阵做,但是要差一点跑不过。。。。
今天早上头昏脑涨写了半天写挂十万个地方
矩阵求逆的写法
#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define fi first
#define se second
#define bg begin
cs int RLEN=1<<20|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ib==ob)?EOF:*ib++;
}
inline int read(){
char ch=gc();
int res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
cs int mod=998244353;
inline int add(int a,int b){return (a+=b)>=mod?(a-mod):a;}
inline int dec(int a,int b){a-=b;return a+(a>>31&mod);}
inline int mul(int a,int b){static ll r;r=1ll*a*b;return (r>=mod)?(r%mod):r;}
inline void Add(int &a,int b){(a+=b)>=mod?(a-=mod):0;}
inline void Dec(int &a,int b){a-=b,a+=a>>31&mod;}
inline void Mul(int &a,int b){static ll r;r=1ll*a*b;a=(r>=mod)?(r%mod):r;}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,Mul(a,a))(b&1)&&(Mul(res,a),1);return res;}
inline int Inv(int x){return ksm(x,mod-2);}
inline int fix(int x){return (x<0)?x+mod:x;}
cs int N=(1<<20)|1,M=(1<<9)+1;
char xxx;
int n,m,k;
int f[N],lim,a[M],val[M];
int c[M][M],iv[M][M],xorall;
int *ans[N];
char yyy;
inline void calc(int res,int s){
val[res]=add(s,a[k]);
}
void dfs(int pos,int res,int s){
if(pos>=k)return calc(res,s);
dfs(pos+1,res,dec(s,a[pos]));
dfs(pos+1,res|(1<<(pos-1)),add(s,a[pos]));
}
int coef[M];
inline int calc(int *ans){
memset(coef,0,sizeof(int)*(lim));
for(int j=0;j<lim;j++)
for(int i=0;i<lim;i++)
Add(coef[i],mul(ans[j],iv[j][i]));
int ret=1;
for(int i=0;i<lim;i++)Mul(ret,ksm(val[i],coef[i]));
return ret;
}
int p[1000005][10];
int bit[21];
inline int get(int x,int y){
for(int i=0;i<k-1;i++)
if((1<<i)&x)bit[i]=1;
else bit[i]=-1;
int ret=1;
for(int i=0;i<k-1;i++)if(y&(1<<i))ret*=bit[i];
return ret==1?1:mod-1;
}
inline void Fwt(int *f,int lim){
for(int mid=1,a0,a1;mid<lim;mid<<=1)
for(int i=0;i<lim;i+=mid<<1)
for(int j=0;j<mid;j++)
a0=f[i+j],a1=f[i+j+mid],f[i+j]=add(a0,a1),f[i+j+mid]=dec(a0,a1);
}
inline void IFwt(int *f,int lim){
for(int mid=1,a0,a1;mid<lim;mid<<=1)
for(int i=0;i<lim;i+=mid<<1)
for(int j=0;j<mid;j++)
a0=f[i+j],a1=f[i+j+mid],f[i+j]=add(a0,a1),f[i+j+mid]=dec(a0,a1);
for(int i=0,v=Inv(lim);i<lim;i++)Mul(f[i],v);
}
inline void getans(int pos){
memset(f,0,sizeof(int)*(1<<m));
for(int i=1;i<=n;i++){
int xo=0;
for(int j=0;j<k-1;j++)if(pos&(1<<j))xo^=p[i][j+1];
f[xo]++;
}
Fwt(f,1<<m);
for(int i=0;i<(1<<m);i++)ans[i][pos]=f[i];
}
inline void getinv(){
for(int i=0;i<lim;i++)iv[i][i]=1;
for(int i=0;i<lim;i++){
int pos=i;
int v=Inv(c[i][i]);
for(int j=0;j<lim;j++)Mul(c[i][j],v),Mul(iv[i][j],v);
for(int j=0;j<lim;j++)if(j!=i){
int mt=c[j][i];
for(int k=0;k<lim;k++)
Dec(c[j][k],mul(c[i][k],mt)),Dec(iv[j][k],mul(iv[i][k],mt));
}
}
}
int main(){
#ifdef Stargazer
freopen("lx.in","r",stdin);
#endif
n=read(),m=read(),k=read();
for(int i=1;i<=k;i++)a[k-i+1]=read();
lim=1<<(k-1);
for(int i=0;i<(1<<m);i++)ans[i]=new int[lim];
dfs(1,0,0);
for(int i=1;i<=n;i++){
for(int j=1;j<=k;j++)p[i][k-j+1]=read();
xorall^=p[i][k];
for(int j=1;j<k;j++)p[i][j]^=p[i][k];
}
for(int i=0;i<lim;i++){
for(int j=1;j<lim;j++)
c[i][j]=get(i,j);
}
for(int i=1;i<=lim;i++)getans(i);
for(int i=0;i<lim;i++)c[i][0]=1;
for(int i=0;i<(1<<m);i++)ans[i][0]=n;
getinv();
memset(f,0,sizeof(int)*(1<<m));
for(int i=0;i<(1<<m);i++){
f[i]=calc(ans[i]);
}
IFwt(f,1<<m);
for(int i=0;i<(1<<m);i++)cout<<f[i^xorall]<<" ";
}
正解
#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define fi first
#define se second
#define bg begin
cs int RLEN=1<<20|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ib==ob)?EOF:*ib++;
}
inline int read(){
char ch=gc();
int res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
cs int mod=998244353;
inline int add(int a,int b){return (a+=b)>=mod?(a-mod):a;}
inline int dec(int a,int b){a-=b;return a+(a>>31&mod);}
inline int mul(int a,int b){static ll r;r=1ll*a*b;return (r>=mod)?(r%mod):r;}
inline void Add(int &a,int b){(a+=b)>=mod?(a-=mod):0;}
inline void Dec(int &a,int b){a-=b,a+=a>>31&mod;}
inline void Mul(int &a,int b){static ll r;r=1ll*a*b;a=(r>=mod)?(r%mod):r;}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,Mul(a,a))(b&1)&&(Mul(res,a),1);return res;}
inline int Inv(int x){return ksm(x,mod-2);}
inline int fix(int x){return (x<0)?x+mod:x;}
cs int N=(1<<20)|1,M=(1<<9)+1;
char xxx;
int n,m,k;
int f[N],lim,a[M],val[M];
int xorall;
int *ans[N];
char yyy;
inline void calc(int res,int s){
val[res]=add(s,a[k]);
}
void dfs(int pos,int res,int s){
if(pos>=k)return calc(res,s);
dfs(pos+1,res,add(s,a[pos]));
dfs(pos+1,res|(1<<(pos-1)),dec(s,a[pos]));
}
int p[1000005][10];
inline void Fwt(int *f,int lim){
for(int mid=1,a0,a1;mid<lim;mid<<=1)
for(int i=0;i<lim;i+=mid<<1)
for(int j=0;j<mid;j++)
a0=f[i+j],a1=f[i+j+mid],f[i+j]=add(a0,a1),f[i+j+mid]=dec(a0,a1);
}
inline void IFwt(int *f,int lim){
for(int mid=1,a0,a1;mid<lim;mid<<=1)
for(int i=0;i<lim;i+=mid<<1)
for(int j=0;j<mid;j++)
a0=f[i+j],a1=f[i+j+mid],f[i+j]=add(a0,a1),f[i+j+mid]=dec(a0,a1);
for(int i=0,v=Inv(lim);i<lim;i++)Mul(f[i],v);
}
inline void getans(int pos){
memset(f,0,sizeof(int)*(1<<m));
for(int i=1;i<=n;i++){
int xo=0;
for(int j=0;j<k-1;j++)if(pos&(1<<j))xo^=p[i][j+1];
f[xo]++;
}
Fwt(f,1<<m);
for(int i=0;i<(1<<m);i++)ans[i][pos]=f[i];
}
inline int calc(int *ans){
IFwt(ans,lim);
int ret=1;
for(int i=0;i<lim;i++)Mul(ret,ksm(val[i],ans[i]));
return ret;
}
int main(){
#ifdef Stargazer
freopen("lx.in","r",stdin);
#endif
n=read(),m=read(),k=read();
for(int i=1;i<=k;i++)a[k-i+1]=read();
lim=1<<(k-1);
for(int i=0;i<(1<<m);i++)ans[i]=new int[lim];
dfs(1,0,0);
for(int i=1;i<=n;i++){
for(int j=1;j<=k;j++)p[i][k-j+1]=read();
xorall^=p[i][k];
for(int j=1;j<k;j++)p[i][j]^=p[i][k];
}
for(int i=1;i<lim;i++)getans(i);
for(int i=0;i<(1<<m);i++)ans[i][0]=n;
for(int i=0;i<(1<<m);i++){
f[i]=calc(ans[i]);
}
IFwt(f,1<<m);
for(int i=0;i<(1<<m);i++)cout<<f[i^xorall]<<" ";
}
T2:
#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define int long long
#define fi first
#define se second
#define bg begin
cs int RLEN=1<<20|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ib==ob)?EOF:*ib++;
}
inline int read(){
char ch=gc();
int res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
inline ll readll(){
char ch=gc();
ll res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
namespace P{
cs int N=10000005;
bitset<N>vis;
int pr[N/10+1],mnp[N],tot;
inline void init(cs int len=N-5){
for(int i=2;i<=len;i++){
if(!vis[i])pr[++tot]=i,mnp[i]=i;
for(int j=1;j<=tot&&i*pr[j]<=len;j++){
vis[i*pr[j]]=1,mnp[i*pr[j]]=pr[j];
if(i%pr[j]==0)break;
}
}
}
inline ll mul(ll a,ll b,ll mod){
return (a*b-(ll)((long double)a/mod*b)*mod+mod)%mod;
}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll ksm(ll a,ll b,cs ll &mod){
ll res=1;
for(;b;b>>=1,a=mul(a,a,mod))if(b&1)res=mul(res,a,mod);
return res;
}
inline bool prime(ll x){
static int pr[17]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59};
for(int i=0;i<17;i++)if(x%pr[i]==0)return x==pr[i];
if(x<pr[16])return false;
ll t=x-1,s=0;
while(!(t&1))t>>=1,s++;
for(int i=0;i<17;i++){
ll a=pr[i],b=ksm(a,t,x);
for(int j=1;j<=s;j++){
ll k=mul(b,b,x);
if(k==1&&b!=1&&b!=x-1)return false;
b=k;
}
if(b!=1)return false;
}
return true;
}
inline ll f(ll x,ll c,cs ll mod){
return (mul(x,x,mod)+c)%mod;
}
inline ll pol(ll x){
ll s=0,t=0,c=1ll*rand()%(x-1)+1;
for(int goal=1;;goal<<=1,s=t){
ll val=1;
for(int stp=1;stp<=goal;stp++){
t=f(t,c,x);
val=mul(val,abs(t-s),x);
if(stp%127==0){
ll d=gcd(val,x);
if(d>1)return d;
}
}
ll d=gcd(val,x);
if(d>1)return d;
}
}
}
vector<ll> fac;
int n,top,ans;
cs int mod=1e7+7;
ll a[205],t[mod];
ll h[mod],stk[205];
map<ll,int> vis;
inline void div(ll x){
if(x<2||vis.count(x))return;
vis[x]=1;
if(P::prime(x)){fac.pb(x);return;}
ll p=P::pol(x);div(p),div(x/p);
}
inline void calc(ll x){
for(int i=1;i<=100;i++)if(x%P::pr[i]==0){
if(P::pr[i]>n)fac.pb(P::pr[i]);
while(x%P::pr[i]==0)x/=P::pr[i];
}div(x);
}
inline int pos(ll k){
int p=k%mod;
while(t[p]!=-1&&t[p]!=k)p++,p=(p==mod)?0:p;
return p;
}
inline void solve(ll len){
int res=0;
for(int i=1;i<=n;i++){
for(ll x=a[i]%len;;x++,x=(x==len)?0:x,res++){
int p=pos(x);
if(t[p]==-1){
stk[++top]=p,t[p]=x,h[p]=a[i];
break;
}
if(h[p]==a[i])break;
}
}
while(top)t[stk[top]]=h[stk[top]]=-1,top--;
chemx(ans,res);
}
signed main(){
#ifdef Stargazer
freopen("lx.in","r",stdin);
#endif
srand(time(NULL));
P::init();
memset(t,-1,sizeof(t));
memset(h,-1,sizeof(h));
read();
n=read();
for(int i=1;i<=n;i++)a[i]=readll();
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++){
ll x=abs(a[i]-a[j]);
if(x)calc(x);
}
for(int i=n;i<=n*n;i++)if(i/P::mnp[i]<=n)solve(i);
sort(fac.bg(),fac.end()),fac.erase(unique(fac.bg(),fac.end()),fac.end());
for(int i=0;i<fac.size();i++)if(fac[i]>n)solve(fac[i]);
cout<<ans<<'\n';
}
T3:
#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define fi first
#define se second
#define bg begin
cs int RLEN=1<<20|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ib==ob)?EOF:*ib++;
}
#define gc getchar
inline int read(){
char ch=gc();
int res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
cs int mod=1e9+7;
inline int add(int a,int b){return (a+=b)>=mod?(a-mod):a;}
inline int dec(int a,int b){a-=b;return a+(a>>31&mod);}
inline int mul(int a,int b){static ll r;r=1ll*a*b;return (r>=mod)?(r%mod):r;}
inline void Add(int &a,int b){(a+=b)>=mod?(a-=mod):0;}
inline void Dec(int &a,int b){a-=b,a+=a>>31&mod;}
inline void Mul(int &a,int b){static ll r;r=1ll*a*b;a=(r>=mod)?(r%mod):r;}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,Mul(a,a))(b&1)&&(Mul(res,a),1);return res;}
inline int Inv(int x){return ksm(x,mod-2);}
inline int fix(int x){return (x<0)?x+mod:x;}
cs int N=100005;
struct Seg{
int s[N<<2],n;
#define lc (u<<1)
#define rc ((u<<1)|1)
#define mid ((l+r)>>1)
void build(int u,int l,int r,int *k){
if(l==r)return s[u]=k[l],void();
build(lc,l,mid,k),build(rc,mid+1,r,k);
s[u]=add(s[lc],s[rc]);
}
inline void build(int *a,int _n){
n=_n,build(1,1,n,a);
}
void update(int u,int l,int r,int p,int k){
if(l==r)return s[u]=k,void();
if(p<=mid)update(lc,l,mid,p,k);
else update(rc,mid+1,r,p,k);
s[u]=add(s[lc],s[rc]);
}
inline void update(int p,int k){
update(1,1,n,p,k);
}
int query(int u,int l,int r,int st,int des){
if(st<=l&&r<=des)return s[u];
int ret=0;
if(st<=mid)Add(ret,query(lc,l,mid,st,des));
if(mid<des)Add(ret,query(rc,mid+1,r,st,des));
return ret;
}
inline int query(int l,int r){
return query(1,1,n,l,r);
}
}tx[11][2],ty[11][2];
int a[N],b[N],A[N],B[N];
int n,m,q;
int s[11][11],c[11][11],fac[11],ifac[11];
inline void init_inv(){
fac[0]=ifac[0]=1;
for(int i=1;i<=10;i++)fac[i]=mul(fac[i-1],i);
ifac[10]=Inv(fac[10]);
for(int i=9;i;i--)ifac[i]=mul(ifac[i+1],i+1);
s[0][0]=1;
for(int i=1;i<=10;i++)
for(int j=1;j<=10;j++)
s[i][j]=add(s[i-1][j-1],mul(s[i-1][j],i-1));
for(int i=0;i<=10;i++){
c[i][i]=c[i][0]=1;
for(int j=1;j<i;j++)
c[i][j]=add(c[i-1][j-1],c[i-1][j]);
}
}
int tp1[11],tp2[11];
char op[5];
int main(){
#ifdef Stargazer
freopen("lx.in","r",stdin);
#endif
n=read(),m=read(),q=read();
init_inv();
for(int i=1;i<=n;i++)a[i]=1;
for(int j=1;j<=m;j++)b[j]=1;
for(int i=0;i<=10;i++){
tx[i][0].build(a,n),ty[i][0].build(b,m);
for(int j=1;j<=n;j++)Mul(a[j],mod-j);
for(int j=1;j<=m;j++)Mul(b[j],mod-j);
}
for(int i=1;i<=n;i++)A[i]=a[i]=1ll*(i-1)*m%mod;
for(int i=1;i<=m;i++)B[i]=b[i]=i;
for(int i=0;i<=10;i++){
tx[i][1].build(a,n),ty[i][1].build(b,m);
for(int j=1;j<=n;j++)Mul(a[j],mod-j);
for(int j=1;j<=m;j++)Mul(b[j],mod-j);
}
while(q--){
scanf("%s",op);
if(op[0]=='Q'){
int x1=read(),y1=read(),x2=read(),y2=read(),k=read();
memset(tp1,0,sizeof(tp1)),memset(tp2,0,sizeof(tp2));
for(int i=0;i<=k;i++){
int pw1=1,pw2=1;
for(int j=0;j<=i;j++){
if((k-i)&1)Dec(tp1[i-j],mul(mul(s[k][i],c[i][j]),pw1)),Dec(tp2[i-j],mul(mul(s[k][i],c[i][j]),pw2));
else Add(tp1[i-j],mul(mul(s[k][i],c[i][j]),pw1)),Add(tp2[i-j],mul(mul(s[k][i],c[i][j]),pw2));
Mul(pw1,x2+k),Mul(pw2,y2+k);
}
}
int a=0,b=0,c=0,d=0;
for(int i=0;i<=k;i++){
Add(a,mul(tp1[i],tx[i][0].query(x1,x2)));
Add(b,mul(tp1[i],tx[i][1].query(x1,x2)));
Add(c,mul(tp2[i],ty[i][0].query(y1,y2)));
Add(d,mul(tp2[i],ty[i][1].query(y1,y2)));
}
int ret=add(mul(a,d),mul(b,c));
cout<<mul(ret,mul(ifac[k],ifac[k]))<<'\n';
}
if(op[0]=='R'){
int x=read(),y=read();
swap(A[x],A[y]);
int pw1=1,pw2=1;
for(int i=0;i<=10;i++){
tx[i][0].update(x,pw1),tx[i][1].update(x,mul(pw1,A[x]));
tx[i][0].update(y,pw2),tx[i][1].update(y,mul(pw2,A[y]));
Mul(pw1,mod-x),Mul(pw2,mod-y);
}
}
if(op[0]=='C'){
int x=read(),y=read();
swap(B[x],B[y]);
int pw1=1,pw2=1;
for(int i=0;i<=10;i++){
ty[i][0].update(x,pw1),ty[i][1].update(x,mul(pw1,B[x]));
ty[i][0].update(y,pw2),ty[i][1].update(y,mul(pw2,B[y]));
Mul(pw1,mod-x),Mul(pw2,mod-y);
}
}
}
}
我这写的锤子题解