CodeCraft-19 and Codeforces Round #537 (Div. 2) 题解
D. Destroy the Colony
首先明确题意:除了规定的两种(或一种)字母要在同侧以外,其他字母也必须在同侧。
发现当每种字母在左/右边确定之后,方案数就确定了,就是分组的方案数乘\(\frac{((n/2)!)^2}{\prod cnt_i!}\)。
分组的方案数考虑DP,设\(dp_{i,j}\)为前\(i\)个字母,占了左边\(j\)个位置的方案数,则有:
\]
当\(i\)是指定字母时特判即可。
这样复杂度为\(O(52^3n)\),无法通过。
考虑最多指定两次字母,而且字母顺序对结果没有关系,可以先把所有的DP出来,然后假装指定的两个字母是最后两个DP的,按照DP方程撤销即可。
#include<bits/stdc++.h>
clock_t t=clock();
namespace my_std{
using namespace std;
#define pii pair<int,int>
#define fir first
#define sec second
#define MP make_pair
#define rep(i,x,y) for (int i=(x);i<=(y);i++)
#define drep(i,x,y) for (int i=(x);i>=(y);i--)
#define go(x) for (int i=head[x];i;i=edge[i].nxt)
#define templ template<typename T>
#define sz 101101
#define mod 1000000007
typedef long long ll;
typedef double db;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
templ inline T rnd(T l,T r) {return uniform_int_distribution<T>(l,r)(rng);}
templ inline bool chkmax(T &x,T y){return x<y?x=y,1:0;}
templ inline bool chkmin(T &x,T y){return x>y?x=y,1:0;}
templ inline void read(T& t)
{
t=0;char f=0,ch=getchar();double d=0.1;
while(ch>'9'||ch<'0') f|=(ch=='-'),ch=getchar();
while(ch<='9'&&ch>='0') t=t*10+ch-48,ch=getchar();
if(ch=='.'){ch=getchar();while(ch<='9'&&ch>='0') t+=d*(ch^48),d*=0.1,ch=getchar();}
t=(f?-t:t);
}
template<typename T,typename... Args>inline void read(T& t,Args&... args){read(t); read(args...);}
char __sr[1<<21],__z[20];int __C=-1,__Z=0;
inline void Ot(){fwrite(__sr,1,__C+1,stdout),__C=-1;}
inline void print(register int x)
{
if(__C>1<<20)Ot();if(x<0)__sr[++__C]='-',x=-x;
while(__z[++__Z]=x%10+48,x/=10);
while(__sr[++__C]=__z[__Z],--__Z);__sr[++__C]='\n';
}
void file()
{
#ifndef ONLINE_JUDGE
freopen("a.in","r",stdin);
#endif
}
inline void chktime()
{
#ifndef ONLINE_JUDGE
cout<<(clock()-t)/1000.0<<'\n';
#endif
}
#ifdef mod
ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x%mod) if (y&1) ret=ret*x%mod;return ret;}
ll inv(ll x){return ksm(x,mod-2);}
#else
ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x) if (y&1) ret=ret*x;return ret;}
#endif
// inline ll mul(ll a,ll b){ll d=(ll)(a*(double)b/mod+0.5);ll ret=a*b-d*mod;if (ret<0) ret+=mod;return ret;}
}
using namespace my_std;
int n,m,K;
char s[sz];
int a[sz],aa[sz];
int cnt[233];
ll T;
ll fac[sz],_fac[sz];
void init(){fac[0]=_fac[0]=1;rep(i,1,sz-1) fac[i]=fac[i-1]*i%mod,_fac[i]=inv(fac[i]);}
ll C(int n,int m){return n>=m&&m>=0?fac[n]*_fac[m]%mod*_fac[n-m]%mod:0;}
int sum[66];
ll dp[sz],cur[sz];
ll ans[66][66];
void Init()
{
rep(i,1,K) sum[i]=sum[i-1]+cnt[i];
dp[0]=1;
int n=::n>>1;
rep(i,1,K)
drep(j,n,cnt[i])
dp[j]=(dp[j]+dp[j-cnt[i]])%mod;
}
ll solve(int x,int y)
{
int n=::n>>1;
if ((cnt[x]>n&&x==y)||(cnt[x]+cnt[y]>n&&x!=y)) return 0;
ll c=fac[n]*fac[n]%mod*inv(T)%mod;
if (x==y) return c*dp[n]%mod;
rep(i,0,n) cur[i]=dp[i];
rep(j,cnt[x],n)
cur[j]=(cur[j]-cur[j-cnt[x]]+mod)%mod;
rep(j,cnt[y],n)
cur[j]=(cur[j]-cur[j-cnt[y]]+mod)%mod;
return c*cur[n-cnt[x]-cnt[y]]*2%mod;
}
int main()
{
file();
cin>>(s+1);n=strlen(s+1);
rep(i,1,n) a[i]=aa[i]=s[i];
sort(aa+1,aa+n+1);K=unique(aa+1,aa+n+1)-aa-1;
rep(i,1,n) a[i]=lower_bound(aa+1,aa+K+1,a[i])-aa;
rep(i,1,n) ++cnt[a[i]];
init();Init();
T=1;rep(i,1,K) T=T*fac[cnt[i]]%mod;
rep(i,1,K) rep(j,1,K) ans[i][j]=solve(i,j);
read(m);
int x,y;
while (m--) read(x,y),printf("%lld\n",ans[a[x]][a[y]]);
return 0;
}
E. Tree
显然需要DP,但怎么DP呢?
考虑每个点只被自己的祖先限制,可以把要DP的点按某种方法排序,使得每个点的祖先都在自己之前处理完毕,就可以了。
设\(dp_{i,j}\)表示前\(i\)个点,分成\(j\)个集合的方案数,则有:
\]
其中\(f_x\)表示\(x\)的祖先数。
这时又发现按\(f_x\)从小到大排序,就可以满足要求,于是就做完了。
注意出题人卡空间,DP需要滚掉一维。
#include<bits/stdc++.h>
clock_t t=clock();
namespace my_std{
using namespace std;
#define pii pair<int,int>
#define fir first
#define sec second
#define MP make_pair
#define rep(i,x,y) for (int i=(x);i<=(y);i++)
#define drep(i,x,y) for (int i=(x);i>=(y);i--)
#define go(x) for (int i=head[x];i;i=edge[i].nxt)
#define templ template<typename T>
#define sz 101010
#define mod 1000000007
typedef long long ll;
typedef double db;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
templ inline T rnd(T l,T r) {return uniform_int_distribution<T>(l,r)(rng);}
templ inline bool chkmax(T &x,T y){return x<y?x=y,1:0;}
templ inline bool chkmin(T &x,T y){return x>y?x=y,1:0;}
templ inline void read(T& t)
{
t=0;char f=0,ch=getchar();double d=0.1;
while(ch>'9'||ch<'0') f|=(ch=='-'),ch=getchar();
while(ch<='9'&&ch>='0') t=t*10+ch-48,ch=getchar();
if(ch=='.'){ch=getchar();while(ch<='9'&&ch>='0') t+=d*(ch^48),d*=0.1,ch=getchar();}
t=(f?-t:t);
}
template<typename T,typename... Args>inline void read(T& t,Args&... args){read(t); read(args...);}
char sr[1<<21],z[20];int C=-1,Z=0;
inline void Ot(){fwrite(sr,1,C+1,stdout),C=-1;}
inline void print(register int x)
{
if(C>1<<20)Ot();if(x<0)sr[++C]='-',x=-x;
while(z[++Z]=x%10+48,x/=10);
while(sr[++C]=z[Z],--Z);sr[++C]='\n';
}
void file()
{
#ifndef ONLINE_JUDGE
freopen("a.in","r",stdin);
#endif
}
inline void chktime()
{
#ifndef ONLINE_JUDGE
cout<<(clock()-t)/1000.0<<'\n';
#endif
}
#ifdef mod
ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x%mod) if (y&1) ret=ret*x%mod;return ret;}
ll inv(ll x){return ksm(x,mod-2);}
#else
ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x) if (y&1) ret=ret*x;return ret;}
#endif
// inline ll mul(ll a,ll b){ll d=(ll)(a*(double)b/mod+0.5);ll ret=a*b-d*mod;if (ret<0) ret+=mod;return ret;}
}
using namespace my_std;
int n,Q;
struct hh{int t,nxt;}edge[sz<<1];
int head[sz],ecnt;
void make_edge(int f,int t)
{
edge[++ecnt]=(hh){t,head[f]};
head[f]=ecnt;
edge[++ecnt]=(hh){f,head[t]};
head[t]=ecnt;
}
int dfn[sz],son[sz],size[sz],dep[sz],top[sz],fa[sz],T;
#define v edge[i].t
void dfs1(int x,int fa)
{
::fa[x]=fa;dep[x]=dep[fa]+1;
size[x]=1;
go(x) if (v!=fa)
{
dfs1(v,x);
size[x]+=size[v];
if (size[v]>size[son[x]]) son[x]=v;
}
}
void dfs2(int x,int fa,int tp)
{
top[x]=tp;dfn[x]=++T;
if (son[x]) dfs2(son[x],x,tp);
go(x) if (v!=fa&&v!=son[x]) dfs2(v,x,v);
}
#undef v
int tr[sz];
void add(int x,int y){while (x<=n) tr[x]+=y,x+=(x&(-x));}
int query(int x){int ret=0;while (x) ret+=tr[x],x-=(x&(-x));return ret;}
int query(int x,int y){return query(y)-query(x-1);}
int Query(int x,int y)
{
int ret=0;
while (top[x]!=top[y])
{
if (dep[top[x]]<dep[top[y]]) swap(x,y);
ret+=query(dfn[top[x]],dfn[x]);
x=fa[top[x]];
}
if (dep[x]>dep[y]) swap(x,y);
ret+=query(dfn[x],dfn[y]);
return ret;
}
int a[sz];
int f[sz];
inline bool cmp(const int &x,const int &y){return f[x]<f[y];}
ll dp[333];
int main()
{
file();
int x,y,K,m,rt;
read(n,Q);
rep(i,1,n-1) read(x,y),make_edge(x,y);
dfs1(1,0);dfs2(1,0,1);
while (Q--)
{
read(K,m,rt);
rep(i,1,K) read(a[i]),add(dfn[a[i]],1);
rep(j,1,m) dp[j]=0;
dp[0]=1;
rep(i,1,K) f[a[i]]=Query(rt,a[i])-1;
sort(a+1,a+K+1,cmp);
rep(i,1,K)
{
x=a[i];
drep(j,m,f[x]+1) dp[j]=(dp[j-1]+dp[j]*(j-f[x])%mod)%mod;
rep(j,0,f[x]) dp[j]=0;
}
ll ans=0;
rep(i,1,m) (ans+=dp[i])%=mod;
printf("%lld\n",ans);
rep(i,1,K) add(dfn[a[i]],-1);
}
return 0;
}
CodeCraft-19 and Codeforces Round #537 (Div. 2) 题解的更多相关文章
- Codeforces Round #182 (Div. 1)题解【ABCD】
Codeforces Round #182 (Div. 1)题解 A题:Yaroslav and Sequence1 题意: 给你\(2*n+1\)个元素,你每次可以进行无数种操作,每次操作必须选择其 ...
- Codeforces Round #608 (Div. 2) 题解
目录 Codeforces Round #608 (Div. 2) 题解 前言 A. Suits 题意 做法 程序 B. Blocks 题意 做法 程序 C. Shawarma Tent 题意 做法 ...
- Codeforces Round #525 (Div. 2)题解
Codeforces Round #525 (Div. 2)题解 题解 CF1088A [Ehab and another construction problem] 依据题意枚举即可 # inclu ...
- Codeforces Round #528 (Div. 2)题解
Codeforces Round #528 (Div. 2)题解 A. Right-Left Cipher 很明显这道题按题意逆序解码即可 Code: # include <bits/stdc+ ...
- Codeforces Round #466 (Div. 2) 题解940A 940B 940C 940D 940E 940F
Codeforces Round #466 (Div. 2) 题解 A.Points on the line 题目大意: 给你一个数列,定义数列的权值为最大值减去最小值,问最少删除几个数,使得数列的权 ...
- Codeforces Round #677 (Div. 3) 题解
Codeforces Round #677 (Div. 3) 题解 A. Boring Apartments 题目 题解 简单签到题,直接数,小于这个数的\(+10\). 代码 #include &l ...
- Codeforces Round #665 (Div. 2) 题解
Codeforces Round #665 (Div. 2) 题解 写得有点晚了,估计都官方题解看完切掉了,没人看我的了qaq. 目录 Codeforces Round #665 (Div. 2) 题 ...
- Codeforces Round #160 (Div. 1) 题解【ABCD】
Codeforces Round #160 (Div. 1) A - Maxim and Discounts 题意 给你n个折扣,m个物品,每个折扣都可以使用无限次,每次你使用第i个折扣的时候,你必须 ...
- Codeforces Round #383 (Div. 2) 题解【ABCDE】
Codeforces Round #383 (Div. 2) A. Arpa's hard exam and Mehrdad's naive cheat 题意 求1378^n mod 10 题解 直接 ...
随机推荐
- python中的optionParser模块
Python 有两个内建的模块用于处理命令行参数:一个是 getopt,<Deep in python>一书中也有提到,只能简单处理 命令行参数:另一个是 optparse,它功能强大,而 ...
- nginx 相关命令
验证配置是否正确: nginx -t 查看Nginx的版本号:nginx -V 启动Nginx:start nginx 快速停止或关闭Nginx:nginx -s stop 正常停止或关闭Nginx: ...
- oracle 存储过程(包)的写法和执行
--in 代表输入参数,out 代表输出参数create or replace procedure myproc(id in int, v_message out varchar2) is--定义临时 ...
- js去掉某一属性
将某一属性赋值为空就行了. 例如: document.getElementById('second').style.color = '';
- A - 签到题
给定一个长度为N的数组A=[A1, A2, ... AN],已知其中每个元素Ai的值都只可能是1, 2或者3. 请求出有多少下标三元组(i, j, k)满足1 ≤ i < j < k ≤ ...
- SuperDiamond在JAVA项目中的三种应用方法实践总结
SuperDiamond在JAVA项目中的三种应用方法实践总结 1.直接读取如下: @Test public static void test_simple(){ PropertiesConfigur ...
- Django学习手册 - reverse()反转URL
前端: <h1>测试</h1> <a href="/ce_test/?id=1">1按键</a> <a href=" ...
- 管理并行SQL执行的进程
本节介绍的并行执行功能可用于Oracle数据库企业版 本节介绍如何管理SQL语句的并行处理.在此配置中,Oracle数据库可以将处理SQL语句的工作分为多个并行进程. 许多SQL语句的执行可以并行化. ...
- SSIS服务无法登录的解决方案
现象1:登录SSIS报权限认证失败. 授予对 Integration Services 服务的访问权限 运行 Dcomcnfg.exe. Dcomcnfg.exe 提供用于修改注册表中的某些设置的用户 ...
- 查询tensorflow中的函数用法
一下均在ubuntu环境下: (1)方法一,使用help()函数: 比如对于tf.placeholder(),在命令行中输入import tensorflow as tf , help(tf.plac ...