你们去试试
2023-09-05 13:28:45
发布于:广东
21阅读
0回复
0点赞
#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;
}
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;
}
inline int readstring(char *s){
int top=0;char ch=gc();
while(isspace(ch))ch=gc();
while(!isspace(ch)&&ch!=EOF)s[++top]=ch,ch=gc();
return top;
}
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){if(a==0&&b==0)return 0;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=30005;
int n,k,tot,le[N][8],up[N][8];
map<vector<int>,int>id;
vector<pii>tf[305],tg[305];
vector<int>now;
int fa[15],idx[15],ff[15];
inline int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
inline void calcf(){
static int vt[15],vis[15];
for(int i=1;i<=k;i++)vt[i]=0;
vt[1]=1;
for(int i=0;i<now.size();i++)if(!vt[now[i]])vt[now[i]]=i+1;
for(int s=0;s<(1<<k);s++){
for(int i=1;i<=k*2;i++)vis[i]=idx[i]=0;
vector<int>tp;
for(int i=0,cnt=0,p;i<k;i++){
if(s&(1<<i))p=vt[now[i]],vis[p]=1;
else p=k+i+1;
if(!idx[p])idx[p]=++cnt;
tp.pb(idx[p]);
}
bool fg=1;
for(int i=1;i<=k;i++)if(vt[i]&&!vis[vt[i]]){fg=0;}
if(fg)tf[tot].pb(pii(s,id[tp]));
}
}
inline void calcg(){
for(int i=1;i<=k;i++)ff[i]=i;
for(int i=1;i<=k;i++){
for(int j=i-1;j;j--)if(now[j-1]==now[i-1]){
ff[i]=ff[j];break;
}
}
for(int s=0;s<(1<<(k-1));s++){
for(int i=1;i<=k;i++)fa[i]=ff[i],idx[i]=0;
bool fg=1;
for(int i=0;i<k-1;i++)if(s&(1<<i)){
if(find(i+1)==find(i+2)){fg=0;break;}
fa[find(i+2)]=find(i+1);
}
if(fg){
vector<int>tp;
for(int i=1,cnt=0;i<=k;i++){
if(!idx[find(i)])idx[find(i)]=++cnt;
tp.pb(idx[find(i)]);
}
tg[tot].pb(pii(s,id[tp]));
}
}
}
void dfs1(int pos,int mx){
if(pos==k+1){
id[now]=++tot;return;
}
for(int i=1;i<=mx+1;i++){
now.pb(i),dfs1(pos+1,max(i,mx));
now.pop_back();
}
}
void dfs2(int pos,int mx){
if(pos==k+1){
++tot;
calcf(),calcg();
return;
}
for(int i=1;i<=mx+1;i++){
now.pb(i),dfs2(pos+1,max(i,mx));
now.pop_back();
}
}
struct node{
ll v;int f;
node(ll a=1e18,int b=0):v(a),f(b){}
friend inline node operator +(cs node &a,cs node &b){
return a.v==b.v?(node(a.v,add(a.f,b.f))):(a.v<b.v?a:b);
}
}f[350],g[350];
ll coef[N];
int pt[N];
#define lb(x) (x&(-x))
inline void trans1(int *v){
coef[0]=1;
for(int i=1;i<(1<<(k-1));i++)coef[i]=coef[i^(lb(i))]+v[pt[lb(i)]];
for(int i=1;i<=tot;i++)if(g[i].f)
for(pii &x:tg[i]){
f[x.se]=f[x.se]+node(g[i].v+coef[x.fi],g[i].f);
}
}
inline void trans2(int *v){
coef[0]=1;
for(int i=1;i<(1<<k);i++)coef[i]=coef[i^(lb(i))]+v[pt[lb(i)]];
for(int i=1;i<=tot;i++)if(f[i].f)
for(pii &x:tf[i]){
g[x.se]=g[x.se]+node(f[i].v+coef[x.fi],f[i].f);
}
}
int main(){
#ifdef Stargazer
freopen("lx.in","r",stdin);
#endif
n=read(),k=read();
for(int i=0;i<=k;i++)pt[1<<i]=i;
for(int i=1;i<=n;i++)
for(int j=0;j<k-1;j++)up[i][j]=read();
for(int j=0;j<k;j++)
for(int i=1;i<n;i++)le[i][j]=read();
dfs1(1,0);
tot=0;
dfs2(1,0);
g[tot]=node(0,1);
for(int i=1;i<n;i++){
for(int j=1;j<=tot;j++)f[j]=node();
trans1(up[i]);
for(int j=1;j<=tot;j++)g[j]=node();
trans2(le[i]);
}
for(int j=1;j<=tot;j++)f[j]=node();
trans1(up[n]);
cout<<f[1].f<<'\n';
}
全部评论 3
nb
2023-09-06 来自 广东
0哦卖嘎!wow!
2023-09-05 来自 北京
0顶
2023-09-05 来自 广东
0
有帮助,赞一个