T1:
对每个预处理一下到其他的距离然后状压即可
#include<bits/stdc++.h>
using namespace std;
#define cs const
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define ll long long
#define int long long
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 chemn(tp &a,tp b){a>b?a=b:0;}
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
#define pli pair<ll,int>
char xxx;
int n,k,t,lx,rx;
int r,b,g;
cs int N=505,M=(1<<12)|5;
char s[N][N];
ll f[12][M],tr[15][15];
int id[N][N],tot,str,cnt[M];
int px[12],py[12],num,vis[N*N];
ll val[N*N],dis[N*N];
vector<int> e[N*N];
char yyy;
inline void add(int u,int v){
e[u].pb(v);
}
priority_queue<pli,vector<pli>,greater<pli> > q;
inline void dijkstra(int st){
memset(dis,127/3,sizeof(dis));
memset(vis,0,sizeof(vis));
dis[st]=0,q.push(pli(0,st));
while(!q.empty()){
int u=q.top().se;q.pop();
if(vis[u])continue;
vis[u]=1;
for(int i=0;i<e[u].size();i++){
int v=e[u][i];
if(dis[v]>dis[u]+val[v]){
dis[v]=dis[u]+val[v];
q.push(pli(dis[v],v));
}
}
}
}
signed main(){
n=read(),k=read(),t=read(),lx=read(),rx=read();
r=read(),g=read(),b=read();
for(int i=1;i<=n;i++){
scanf("%s",s[i]+1);
for(int j=1;j<=n;j++){
id[i][j]=++tot;
if(s[i][j]=='T')px[++num]=i,py[num]=j;
if(s[i][j]=='S')str=tot;
if(s[i][j]=='R')val[tot]=r;
if(s[i][j]=='G')val[tot]=g;
if(s[i][j]=='B')val[tot]=b;
}
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++){
if(i>1)add(id[i][j],id[i-1][j]);
if(j>1)add(id[i][j],id[i][j-1]);
if(i<n)add(id[i][j],id[i+1][j]);
if(j<n)add(id[i][j],id[i][j+1]);
}
for(int i=1;i<=num;i++){
dijkstra(id[px[i]][py[i]]);
for(int j=1;j<=num;j++)
tr[i][j]=dis[id[px[j]][py[j]]];
}
dijkstra(str);int sta=1<<num;
for(int i=1;i<sta;i++)cnt[i]=cnt[i>>1]+(i&1);
memset(f,127/3,sizeof(f));
for(int i=1;i<=num;i++)f[i][1<<(i-1)]=dis[id[px[i]][py[i]]]-t;
for(int s=1;s<sta;s++){
for(int i=1;i<=num;i++)if(s&(1<<(i-1))){
for(int j=1;j<=num;j++)if(!(s&(1<<(j-1)))){
chemn(f[j][s|(1<<(j-1))],f[i][s]+tr[i][j]-t);
}
}
}
ll res=1e18;
for(int i=1;i<=num;i++)
for(int s=1;s<sta;s++)if(lx<=cnt[s]&&cnt[s]<=rx)chemn(res,f[i][s]);
cout<<-res;
return 0;
}
T2:
脑残写了一个的结果差点就跑过去了
我想的是对每个叶子做根然后倍增找变化的
其实用树剖+表可以做到
但可以直接在树上暴力维护所有变化的地方,然后删去无效的地方即可
复杂度但是并不想写了
我的考场垃圾代码
#include<bits/stdc++.h>
using namespace std;
#define cs const
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define ll long long
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 chemn(tp &a,tp b){a>b?a=b:0;}
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
cs int N=160005;
char xx;
int fa[N][19],g[N][19],dep[N],val[N];
int adj[N],nxt[N<<1],to[N<<1],cnt;
inline void addedge(int u,int v){
nxt[++cnt]=adj[u],adj[u]=cnt,to[cnt]=v;
}
inline int gcd(int a,int b){
return b?gcd(b,a-a/b*b):a;
}
int leaf[N],num,in[N],vis[N];
int n;
char yyy;
void dfs1(int u){
for(int i=1;i<=18;i++)fa[u][i]=0,g[u][i]=0;
vis[u]=0;
for(int e=adj[u];e;e=nxt[e]){
int v=to[e];
if(v==fa[u][0])continue;
fa[v][0]=u,g[v][0]=val[u],dep[v]=dep[u]+1;
dfs1(v);
}
}
inline void gettag(int u){
while(u&&!vis[u])vis[u]=1,u=fa[u][0];
}
void dfs2(int u){
for(int i=1;i<=18;i++)fa[u][i]=fa[fa[u][i-1]][i-1],g[u][i]=gcd(g[u][i-1],g[fa[u][i-1]][i-1]);
for(int e=adj[u];e;e=nxt[e]){
int v=to[e];
if(!vis[v]||v==fa[u][0])continue;
dfs2(v);
}
}
ll ans;
inline void calc(int u){
int pre=val[u],pp=u;
while(u){
for(int i=18;~i;i--)
if(fa[u][i]&&gcd(pre,g[u][i])==pre)u=fa[u][i];
chemx(ans,1ll*pre*(dep[pp]-dep[u]+1));
pre=gcd(pre,g[u][0]),u=fa[u][0];
}
}
int mxdep,root;
void getrt1(int u,int fa,int dep){
if(dep>mxdep)mxdep=dep,root=u;
for(int e=adj[u];e;e=nxt[e]){
int v=to[e];
if(v==fa)continue;
getrt1(v,u,dep+1);
}
}
inline void solve1(){
getrt1(1,0,1);mxdep=0;
getrt1(root,0,1);
cout<<1ll*val[1]*mxdep<<'
';
}
int main(){
n=read();bool flag=1;
for(int i=1;i<=n;i++){val[i]=read();if(val[i]!=val[i-1])flag=0;}
for(int i=1;i<n;i++){
int u=read(),v=read();
addedge(u,v),addedge(v,u);
in[u]++,in[v]++;
}
if(flag){solve1();return 0;}
for(int i=1;i<=n;i++)if(in[i]==1)leaf[++num]=i;
for(int i=1;i<=num;i++){
// cerr<<leaf[i]<<'
';
fa[leaf[i]][0]=0,dep[leaf[i]]=1,g[leaf[i]][0]=0;
dfs1(leaf[i]);
for(int j=i+1;j<=num;j++)gettag(leaf[j]);
// lim=log2(lim);
dfs2(leaf[i]);
for(int j=1;j<=n;j++)if(vis[j])calc(j);
}
cout<<ans<<'
';
return 0;
}
T3
首先考虑
表示前,有个连出来,个连进去的方案
枚举当前这个点接入接出即可
对于可以类似分类讨论
具体可以参照题解的讨论
#include<bits/stdc++.h>
using namespace std;
#define cs const
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define ll long long
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 chemn(tp &a,tp b){a>b?a=b:0;}
template<class tp>inline void chemx(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);}
int n;
char ss[2006];
cs int N=2005;
int f[N][N],str,des;
int main(){
#ifdef Stargazer
freopen("lx.cpp","r",stdin);
#endif
scanf("%s",ss+1);
n=strlen(ss+1);
str=read(),des=read();
f[0][0]=1;
for(int i=1;i<=n;i++)
for(int j=0;j<=i;j++)
if(f[i-1][j]){
int tmp=f[i-1][j],cnt=j-(i>str)-(i>des);
if(i==str){
if(ss[i]!='L')Add(f[i][j+1],tmp);
if(ss[i]!='R')Add(f[i][j],mul(tmp,cnt));
continue;
}
if(i==des){
Add(f[i][j+1],tmp);
Add(f[i][j],mul(tmp,cnt));
continue;
}
if(ss[i]!='L'){
Add(f[i][j+1],tmp);
Add(f[i][j],mul(tmp,cnt));
if(i>str)Add(f[i][j],tmp);
}
if(ss[i]!='R'){
Add(f[i][j],mul(cnt,tmp));
if(j)Add(f[i][j-1],mul(cnt*(cnt-1),tmp));
if(j&&i>des)Add(f[i][j-1],mul(cnt,tmp));
if(i>des)Add(f[i][j],tmp);
if(i>str&&j)Add(f[i][j-1],mul(cnt,tmp));
}
// cerr<<i<<" :
";
// for(int j=0;j<=n;j++)cerr<<j<<":"<<f[i][j]<<'
';
}
if(str==n||des==n)cout<<f[n-1][1]<<'
';
else cout<<f[n-1][2]<<'
';
return 0;
}