题意:长度为n的序列,相邻两个或单独一个可以划分到一个组,每个元素最多处于一个组。

问恰好分割成k(1<=k<=m)段有多少种方案?

标程:

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=;
const int rt=;
const int N=;
int l,_n,pos[N],n,k,a[N],b[N],c[N],x1[N],x2[N],x3[N],x4[N],w[N],wn,inv_n;
int ksm(int x,int y)
{
int res=;
while (y) {if (y&) res=(ll)res*x%mod; x=(ll)x*x%mod; y>>=;}
return res;
}
void init(int n)
{
l=;
while ((<<l)<=n) l++;//注意位长的处理,判断条件是(1<<l)<=n
_n=<<l;wn=ksm(rt,<<-l);
w[]=;inv_n=ksm(_n,mod-);
for (int i=;i<_n;i++)
w[i]=(ll)w[i-]*wn%mod,pos[i]=(i&)?pos[i-]|(<<(l-)):pos[i>>]>>;
}
void fft(int *a,int op)
{
for (int i=;i<_n;i++) if (i<pos[i]) swap(a[i],a[pos[i]]);
int len=,id=_n;
for (int i=;i<l;i++)
{
int wn=w[id>>=];
for (int j=;j<_n;j+=len*)
for (int k=j,w=;k<j+len;k++)
{
int l=a[k],r=(ll)a[k+len]*w%mod;
a[k]=((ll)l+r)%mod;a[k+len]=((ll)l-r+mod)%mod;
w=(ll)w*wn%mod;
}
len<<=;
}
if (op==-) {
reverse(a+,a+_n);
for (int i=;i<_n;i++) a[i]=(ll)a[i]*inv_n%mod;
}
}
void merge(int *a,int *b,int *c)
{
c[]=;
for (int i=;i<=k;i++) c[i]=((ll)((ll)b[i]+b[i-])%mod+a[i-])%mod;
for (int i=k+;i<_n;i++) c[i]=;
}
void solve(int n)
{
if (n==)
{
a[]=;for (int i=;i<_n;i++) a[i]=;
b[]=b[]=;for (int i=;i<_n;i++) b[i]=;
return;
}
if (n==)
{
a[]=a[]=;for (int i=;i<_n;i++) a[i]=;
b[]=b[]=;b[]=;for (int i=;i<_n;i++) b[i]=;
return;
}
solve(n/-);
merge(a,b,c);
for (int i=k+;i<_n;i++) a[i]=b[i]=;
fft(a,);fft(b,);fft(c,);
for (int i=;i<_n;i++)
{
x1[i]=(ll)a[i]*a[i]%mod;
x2[i]=(ll)b[i]*b[i]%mod;
x3[i]=(ll)a[i]*b[i]%mod;
x4[i]=(ll)b[i]*c[i]%mod;
}
fft(x1,-);fft(x2,-);fft(x3,-);fft(x4,-);
for (int i=;i<_n;i++)//从1开始,注意边界
x2[i]=((ll)x2[i]+x1[i-])%mod,x4[i]=((ll)x4[i]+x3[i-])%mod;
if (n&)
{
merge(x2,x4,b);
for (int i=;i<_n;i++) a[i]=x4[i];
}else
for (int i=;i<_n;i++) a[i]=x2[i],b[i]=x4[i];
}
int main()
{
scanf("%d%d",&n,&k);
init(*k+); solve(n);
for (int i=;i<=k;i++) printf("%d ",a[i]);
puts("");
return ;
}

题解:fft+分治+dp

可以得到递推式:f[i(元素数)][j(段数)]=f[i-1][j-1]+f[i-2][j-1]+f[i-1][j]。

f(n)=f(n-1)*(1+x)+f(n-2)*x.

一个方法是带权斐波那契通项展开,并不会多项式开根。

另一个方法,考虑f(a+b)=f(a)*f(b)+f(a-1)*f(b-1)*x。(根据在a,b处能否断开讨论)

分治下去,f(i),f(i+1)->f(i+2) f(2i+2)=f(i+1)*f(i+1)+f(i)*f(i)*x

f(2i+3)=f(i+1)*f(i+2)+f(i)*f(i+1)*x  f(2i+4)=f(i+2)*f(i+2)+f(i+1)*f(i+1)*x

注意n的奇偶要讨论,m以后的k不用计算。时间复杂度O(mlog^2(m))。

CF755G PolandBall and Many Other Balls/soj 57送饮料的更多相关文章

  1. 题解-CF755G PolandBall and Many Other Balls

    题面 CF755G PolandBall and Many Other Balls 给定 \(n\) 和 \(m\).有一排 \(n\) 个球,求对于每个 \(1\le k\le m\),选出 \(k ...

  2. CF755G PolandBall and Many Other Balls 题解

    从神 Karry 的题单过来的,然后自己瞎 yy 了一个方法,看题解区里没有,便来写一个题解 一个常数和复杂度都很大的题解 令 \(dp_{i,j}\) 为 在 \(i\) 个球中选 \(j\) 组的 ...

  3. FFT/NTT复习笔记&多项式&生成函数学习笔记Ⅱ

    因为垃圾电脑太卡了就重开了一个... 前传:多项式Ⅰ u1s1 我预感还会有Ⅲ 多项式基础操作: 例题: 26. CF438E The Child and Binary Tree 感觉这题作为第一题还 ...

  4. POJ 3687 Labeling Balls()

    Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9641 Accepted: 2636 Descri ...

  5. Soj题目分类

    -----------------------------最优化问题------------------------------------- ----------------------常规动态规划 ...

  6. codeforces 755C. PolandBall and Forest

    C. PolandBall and Forest time limit per test 1 second memory limit per test 256 megabytes input stan ...

  7. codeforces 755F F. PolandBall and Gifts(贪心+多重背包)

    题目链接: F. PolandBall and Gifts time limit per test 1.5 seconds memory limit per test 256 megabytes in ...

  8. Codeforces 755 F. PolandBall and Gifts 多重背包+贪心

    F. PolandBall and Gifts   It's Christmas time! PolandBall and his friends will be giving themselves ...

  9. 【codeforces 755B】PolandBall and Game

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

随机推荐

  1. printf 格式化打印 awk 数据处理工具

    printf解析 这个玩意说白了,就是格式化打印输出. awk awk与sed都是处理数据的工具.sed是处理整行的数据,awk则比较倾向于一行当中分成数个[字段]来处理. 具体操作: 注意的几个点 ...

  2. Coin Slider

    题目描述 You are playing a coin puzzle. The rule of this puzzle is as follows: There are N coins on a ta ...

  3. Oracle数据库中文乱码问题解决

    1.查看服务器端编码select userenv('language') from dual;我实际查到的结果为:AMERICAN_AMERICA.ZHS16GBK 2.执行语句 select * f ...

  4. 【LeetCode 19】删除链表的倒数第N个节点

    题目链接 [题解] 经典的一道题. 让p1指向链表的第一个元素. 让p2指向链表的第二个元素. 然后让他们俩同时往后移动. 直到p2到达链表的尾巴. 这时p1和p2之间总是隔了n-1个元素. 所以p1 ...

  5. Android编程:解决异常“android.view.InflateException: Binary XML file line # : Error inflating class”

    今天写程序发现一个问题,就是XML中报出android.view.InflateException异常,可能的原因有: 1.XML中使用到得组件名称是否书写正确(包名+类名),可以使用crtl+鼠标点 ...

  6. Sqli labs系列-less-4 这关好坑!!!

    这章,可能我总结开会比较长,图比较多,因为,我在做了一半,走进了一个死胡同,脑子,一下子没想开到底为啥.... 然后我自己想了好长时间也没想开,我也不想直接就去看源码,所以就先去百度了一下,结果一下子 ...

  7. STM32嵌入式开发学习笔记(六):串口通信(上)

    本文我们将了解STM32与外部设备通过串口通信的方式. 所谓串口通信,其实是一个类似于计算机网络的概念,它有物理层,比如规定用什么线通信,几伏特算高电平,几伏特算低电平.传输层,通信前要发RTS,CT ...

  8. 14、java实现poi操作excel,包括读和写日期格式,并且设置字体样式

    1.首先大家来看导出的结果 下边就是导出的代码了 protected void testExcel() throws IOException{ String path=getServletContex ...

  9. 16. Jmeter-监听器

    jmeter-监听器介绍与使用 察看结果树 Summary Report 聚合报告 Backend Listener Aggregate Graph 断言结果 Comparison Assertion ...

  10. 洛谷 P1613 跑路 (倍增 + DP + 最短路)

    题目链接:P1613 跑路 题意 给定包含 \(n\) 个点和 \(m\) 条边的有向图,每条边的长度为 \(1\) 千米.每秒钟可以跑 \(2^k\) 千米,问从点 \(1\) 到点 \(n\) 最 ...