H(dp计数)

题意:

  有一颗树,最深的点的深度是n,每个深度为i的点都有ai个孩子。

  对于1<=k<=2n-2,回答树上有多少点对之间的距离是k,答案对1e9+7取模

  n<=5000,ai<=1e9

分析:

  考虑在lca处计数,发现时间复杂度是O(n^3),即使用卷积优化也仍旧是O(n^2logn)的,无法通过n=5000的情况

  考虑另一种计数方式,在端点处计数,分为两种,一种是down,一种是up,down就比较好处理,至于up考虑根据上一个深度来dp

  考虑up的时候只有两种决策,一种是挂一个下来,另一种是在当前深度的上一个进行转弯,分别计数即可

  时间复杂度O(n^2)

G(FFT+dsu)

题意:

  我们定义两个等长字符串x和y的距离就是将最少的字母让另一种字母替代,使得x=y

  现在给出两个字符串S,T,|S|>=|T|,问S的所有长度为T的子串,每个子串和T的距离分别是多少,都要输出

  |S|<=125000

  字符集只有abcdef

分析:

  考虑如何求两个等长字符串的距离,我们只需要给对应字母建个无向图,答案就是点数-连通块个数

  因为字符集很小,只有abcdef,所以连边情况只有36种,可以状态压缩

  我们可以枚举s中的某个字母a,t中的某个字母b,看看有哪些位置的S子串会被这个(a,b)贡献到

  这个东西可以用卷积来实现

  把s中a的对应位置抠出来赋值为1,其它为0,把t中b的对应位置抠出来赋值为1,其它为0,两个多项式卷积一下就行了

 #include<bits/stdc++.h>
using namespace std;
const int maxn=5e5;
const double pi=acos(-1.0);
char t[maxn+],s[maxn+];
int id[][];
pair<int,int> index[];
long long state[maxn+];
int n,m;
struct wjmzbmr
{
double r,i;
wjmzbmr(double real=0.0,double image=0.0){r=real;i=image;}
wjmzbmr operator + (const wjmzbmr o)
{
return wjmzbmr(r+o.r,i+o.i);
}
wjmzbmr operator - (const wjmzbmr o)
{
return wjmzbmr(r-o.r,i-o.i);
}
wjmzbmr operator * (const wjmzbmr o)
{
return wjmzbmr(r*o.r-i*o.i,r*o.i+i*o.r);
}
};
wjmzbmr x1[maxn+],x2[maxn+];
void brc(wjmzbmr *y,int l)
{
for(int i=,j=l/;i<l-;i++)
{
if(i<j) swap(y[i],y[j]);
int k=l/;
while(j>=k)j-=k,k/=;
if(j<k) j+=k;
}
}
void fft(wjmzbmr *y,int l,double on)
{
wjmzbmr u,t;
brc(y,l);
for(int h=;h<=l;h<<=)
{
wjmzbmr wn(cos(on**pi/h),sin(on**pi/h));
for(int j=;j<l;j+=h)
{
wjmzbmr w(,);
for(int k=j;k<j+h/;k++)
{
u=y[k];
t=w*y[k+h/];
y[k]=u+t;
y[k+h/]=u-t;
w=w*wn;
}
}
}
if(on==-)for(int i=;i<l;i++) y[i].r/=l;
}
void work(int x,int y)
{
int len=;
while(len<n+m) len<<=;
for(int i=;i<=len;++i) x1[i].r=x1[i].i=x2[i].i=x2[i].r=0.0;
for(int i=;i<n;++i) if(s[i]==x+'a') x1[i].r=;
for(int i=;i<m;++i) if(t[i]==y+'a') x2[i].r=;
reverse(x2,x2+m);
fft(x1,len,);
fft(x2,len,);
for(int i=;i<len;++i) x1[i]=x1[i]*x2[i];
fft(x1,len,-);
for(int i=;i<n;++i)
if((int)(x1[i+m-].r+0.5)>) state[i]|=(1LL<<id[x][y]);
}
int f[];
int find(int x)
{
if(f[x]==x) return x;
else return f[x]=find(f[x]);
}
void uni(int x,int y)
{
int u=find(x),v=find(y);
if(u==v) return ;
f[u]=v;
}
int cal(long long s)
{
for(int i=;i<;++i) f[i]=i;
for(int i=;i<;++i)
if(s&(1LL<<i))
{
int u=index[i].first,v=index[i].second;
uni(u,v);
}
int ans=;
for(int i=;i<;++i)
if(f[i]==i) --ans;
return ans;
}
int main()
{
scanf("%s%s",s,t);
n=strlen(s),m=strlen(t);
int sz=;
for(int i=;i<;++i)
for(int j=;j<;++j)
{
id[i][j]=sz;
index[sz]=make_pair(i,j);
++sz;
}
for(int i=;i<;++i)
for(int j=;j<;++j)
if(i!=j)
work(i,j);
for(int i=;i<=n-m;++i)
printf("%d ",cal(state[i]));
return ;
}

codeforces edu40的更多相关文章

  1. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  2. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  3. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  4. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  5. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  6. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  7. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  8. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

  9. CodeForces - 148D Bag of mice

    http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...

随机推荐

  1. PAT (Basic Level) Practise (中文)-1025. 反转链表 (25)

    PAT (Basic Level) Practise (中文)-1025. 反转链表 (25)   http://www.patest.cn/contests/pat-b-practise/1025 ...

  2. minGw64编译Qt时遇到too many sections问题

    minGw64编译Qt时遇到too many sections问题: 修改\Src\qtbase\mkspecs\win32-g++\qmake.conf中 QMAKE_CFLAGS         ...

  3. centOS下SVN安装和配置

    1>SVN服务器端文件可见问题 在平时使用SVN时候,一直以为在客户提交文件,那么在服务器对应的版本库下面就会有相同文件.在自己搭建后,发现提交到服务器端文件完全看不见.... 这是由于SVN服 ...

  4. Linux 中 MySQL 授权远程连接

    说明:当别的机子(IP )通过客户端的方式在没有授权的情况下是无法连接 MySQL 数据库的,如果需要远程连接 Linux 系统上的 MySQL 时,必须为其 IP 和具体用户进行授权.一般 root ...

  5. element使用心得

    Table Table 常用属性解释 数据过滤,filter过滤器 <el-table-column width="200" show-overflow-tooltip la ...

  6. RESTful API批量操作的实现

    要解决的问题 RESTful API对于批量操作存在一定的缺陷.例如资源的删除接口: DELETE /api/resourse/<id>/ 如果我们要删除100条数据怎么搞?难道要调用10 ...

  7. (原)剑指offer之栈和队列

    题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 设两个栈为stack1,stack2: 1:首先想到最简单的方法:是入栈时将数据压入stack1,出栈时 ...

  8. USB storage drivers分析之一

    /drivers/usb/storage/Makefile ## Makefile for the USB Mass Storage device drivers.## 15 Aug 2000, Ch ...

  9. LeetCode 653. Two Sum IV – Input is a BST

    Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...

  10. 【转】Sqlserver通过链接服务器访问Oracle的解决办法

    一.创建sqlserver链接服务(sqlserver链接oracle)  首先sqlserver 链接oracle可以通过两个访问接口: “MSDAORA” 和“OraOLEDB.Oracle” 1 ...