BZOJ

因为是本质不同,所以考虑以最小字典序计数。

假设序列最大值为\(m\),那么序列有这两种情况:

  1. \(1\ (1\ 2\ 1\ 2...)\ 2\ (3\ 2\ 3\ 2...)\ 3\ (4\ 3\ 4\ 3...)\ ...\ m\)
  2. \(1\ (1\ 2\ 1\ 2...)\ 2\ (3\ 2\ 3\ 2...)\ 3\ (4\ 3\ 4\ 3...)\ ...\ m\ m-1\)

如果序列长度为\(n\),那么可以看做我们有\(\frac{n-m}{2}\)个相同的球,将它们放进\(m-1\)个盒子,允许盒子有空的方案数,即\(C_{n+m-1}^{m-1}\)。

这里球的个数取\(\lfloor\frac{n-m}{2}\rfloor\)即可(第二种情况取\(\lfloor\frac{n-m-1}{2}\rfloor\))。如果多出来一个,那把它放到两种序列的后面仍是不同的。

\(n,m\)都是不确定的。因为\(\sum_{i=0}^nC_{i+m-1}^{m-1}=C_{n+m-1}^{m-1}\),所以枚举最大值\(m\),就可以\(O(m)\)得到答案啦。

预处理到\(2e6\)就可以啊,不需要\(4e6\),因为\(\frac{n-m}{2}+m=\frac{n+m}{2}\)。。

//16448kb	1008ms
#include <cstdio>
#include <algorithm>
#define mod 1000000007
typedef long long LL;
const int N=2e6+5; int fac[N],ifac[N]; #define F(n,m) (1ll*fac[(n)+(m)-1]*ifac[n]%mod*ifac[(m)-1]%mod)
inline int FP(int x,int k)
{
int t=1;
for(; k; k>>=1,x=1ll*x*x%mod)
if(k&1) t=1ll*t*x%mod;
return t;
} int main()
{
int n,m; scanf("%d%d",&n,&m);
fac[0]=fac[1]=1; int lim=n+m>>1;
for(int i=2; i<=lim; ++i) fac[i]=1ll*fac[i-1]*i%mod;
ifac[lim]=FP(fac[lim],mod-2);
for(int i=lim-1; ~i; --i) ifac[i]=1ll*ifac[i+1]*(i+1)%mod; LL ans=n&&m;//m=1特判下
for(int i=2,l=std::min(n,m); i<=l; ++i)
{
ans+=F((n-i)/2,i);
if(i+1<=n) ans+=F((n-i-1)/2,i);
}
printf("%lld\n",ans%mod); return 0;
}
// int i;//突然闲的
// for(i=2; i+3<=lim; i+=4)
// fac[i]=1ll*fac[i-1]*i%mod,
// fac[i+1]=1ll*fac[i]*(i+1)%mod,
// fac[i+2]=1ll*fac[i+1]*(i+2)%mod,
// fac[i+3]=1ll*fac[i+2]*(i+3)%mod;
// for(; i<=lim; ++i) fac[i]=1ll*fac[i-1]*i%mod;
// ifac[lim]=FP(fac[lim],mod-2);
// for(i=lim-1; i-3>=0; i-=4)
// ifac[i]=1ll*ifac[i+1]*(i+1)%mod,
// ifac[i-1]=1ll*ifac[i]*(i)%mod,
// ifac[i-2]=1ll*ifac[i-1]*(i-1)%mod,
// ifac[i-3]=1ll*ifac[i-2]*(i-2)%mod;
// for(; i>=0; --i) ifac[i]=1ll*ifac[i+1]*(i+1)%mod;

最新文章

  1. spring boot(六):如何优雅的使用mybatis
  2. centos下建立双机信任关系
  3. jQueryMobile示例页面代码
  4. webform分页
  5. ExtJs学习笔记之Window组件
  6. HDU 1753 大明A+B(字符串模拟,简单题)
  7. 使用文档注释(javadoc)
  8. Android(java)学习笔记77:网络编程的概述
  9. [序列化] SerializeHelper--序列化操作帮助类 (转载)
  10. TOMCAT之性能跟踪入门
  11. C++赋值函数详解
  12. Exp5 MSF基础应用 20164320 王浩
  13. Linux 系统的用户和组
  14. C#事务提交
  15. python内置函数整理
  16. 奇怪吸引子---LuChen
  17. RNN生产唐诗
  18. 解决VS2010连接VSS时,Access to file&quot;\\***\rights.dat&quot; denied
  19. Android编译系统环境初始化过程分析
  20. linux下so获得自己文件位置的路径

热门文章

  1. Nginx详解二十七:Nginx架构篇之安全篇
  2. 集腋成裘-06-angularJS -angular_02
  3. Ubuntu点击dash home就崩溃
  4. windows10的文件浏览器中无法搜索文件内容
  5. Redis 学习手册
  6. golang 的glide包管理使用技巧教程
  7. [转] mongoose学习笔记(超详细)
  8. Java基础知识➣发送Emai和访问MySQL数据库(七)
  9. 调整LaTeX文档页面的大小
  10. 【LGR-052】洛谷9月月赛II(加赛)