CF 717A Festival Organization——斯特林数+递推求通项+扩域
题目:http://codeforces.com/contest/717/problem/A
是 BJOI2019 勘破神机 的弱化版。
令 \( g[i] \) 表示长为 i 、以 1 结尾的方案数,有 \( g[i]=g[i-1]+g[i-2] , g[0]=g[1]=1 \) ;
令 \( f[i] \) 表示长为 i 的方案数,有 \( f[i]=g[i]+g[i-1] \)
发现 \( f[i]=f[i-1]+f[i-2] , f[0]=1 , f[1]=2 \)
那么令 l+=2 , r+=2 , f[ i ] 就是普通的斐波那契数。用 BJOI 那道题的套路即可。
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const int N=,mod=1e9+;
int upt(int x){while(x>=mod)x-=mod;while(x<)x+=mod;return x;}
int pw(int x,int k)
{int ret=;while(k){if(k&)ret=(ll)ret*x%mod;x=(ll)x*x%mod;k>>=;}return ret;} int k,s[N][N],c[N][N],ans;
int tlen;ll l,r,len;
struct Node{
int x,y;
Node(int x=,int y=):x(x),y(y) {}
Node operator+ (const Node &b)const
{return Node(upt(x+b.x),upt(y+b.y));}
Node operator- (const Node &b)const
{return Node(upt(x-b.x),upt(y-b.y));}
Node operator* (const Node &b)const
{return Node(((ll)x*b.x+(ll)y*b.y%mod*)%mod,((ll)x*b.y+(ll)y*b.x)%mod);}
}A[N],B[N],x1[N],x2[N],one;
Node pw(Node x,ll k)
{Node ret=one;while(k){if(k&)ret=ret*x;x=x*x;k>>=;}return ret;}
void init()
{
s[][]=;
for(int i=;i<=k;i++)
for(int j=;j<=i;j++)
s[i][j]=(s[i-][j-]+(ll)s[i-][j]*(i-))%mod;
for(int i=;i<=k;i++)c[i][]=;
for(int i=;i<=k;i++)
for(int j=;j<=i;j++)
c[i][j]=upt(c[i-][j-]+c[i-][j]); one=Node(,); int tp=pw(,mod-);
A[]=Node(,tp); B[]=Node(,upt(-tp));
tp=pw(,mod-); x1[]=Node(tp,tp); x2[]=Node(tp,upt(-tp));
A[]=B[]=x1[]=x2[]=one;
for(int i=;i<=k;i++)A[i]=A[i-]*A[];
for(int i=;i<=k;i++)B[i]=B[i-]*B[];
for(int i=;i<=k;i++)x1[i]=x1[i-]*x1[];
for(int i=;i<=k;i++)x2[i]=x2[i-]*x2[];
}
Node Inv(Node x)
{
int tp=upt(((ll)x.x*x.x-(ll)x.y*x.y%mod*)%mod);
tp=pw(tp,mod-);
return Node((ll)x.x*tp%mod,upt(-(ll)x.y*tp%mod));
}
Node cal(Node x)
{
if(x.x==&&x.y==)return Node(tlen,);
return pw(x,l)*(one-pw(x,len))*Inv(one-x);
}
int main()
{
scanf("%d%lld%lld",&k,&l,&r); l+=; r+=;
init(); len=r-l+; tlen=len%mod;
for(int j=,fx=((k&)?upt(-):);j<=k;j++,fx=upt(-fx))
{
int tp=;
for(int t=;t<=j;t++)
{
Node d=cal(x1[t]*x2[j-t]);
d=d*A[t]*B[j-t];
tp=(tp+(ll)c[j][t]*d.x)%mod;
}
ans=(ans+(ll)s[k][j]*fx%mod*tp)%mod;
}
int ml=;
for(int i=;i<=k;i++)ml=(ll)ml*i%mod;
ml=pw(ml,mod-);
ans=(ll)ans*ml%mod;
printf("%d\n",ans);
return ;
}
CF 717A Festival Organization——斯特林数+递推求通项+扩域的更多相关文章
- LOJ 3090 「BJOI2019」勘破神机——斯特林数+递推式求通项+扩域
题目:https://loj.ac/problem/3090 题解:https://www.luogu.org/blog/rqy/solution-p5320 1.用斯特林数把下降幂化为普通的幂次求和 ...
- NYOJ-301递推求值
递推求值 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 给你一个递推公式: f(x)=a*f(x-2)+b*f(x-1)+c 并给你f(1),f(2)的值,请求出f ...
- 算法笔记_091:蓝桥杯练习 递推求值(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 已知递推公式: F(n, 1)=F(n-1, 2) + 2F(n-3, 1) + 5, F(n, 2)=F(n-1, 1) + 3F(n- ...
- NYOJ——301递推求值(矩阵快速幂)
递推求值 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 给你一个递推公式: f(x)=a*f(x-2)+b*f(x-1)+c 并给你f(1),f(2)的值,请求出f(n)的 ...
- poj 3744 Scout YYF I(递推求期望)
poj 3744 Scout YYF I(递推求期望) 题链 题意:给出n个坑,一个人可能以p的概率一步一步地走,或者以1-p的概率跳过前面一步,问这个人安全通过的概率 解法: 递推式: 对于每个坑, ...
- Java实现 蓝桥杯 算法提高 递推求值
算法提高 递推求值 时间限制:1.0s 内存限制:256.0MB 问题描述 已知递推公式: F(n, 1)=F(n-1, 2) + 2F(n-3, 1) + 5, F(n, 2)=F(n-1, 1) ...
- ACM_数数有多少(第二类Stirling数-递推dp)
数数有多少 Time Limit: 2000/1000ms (Java/Others) Problem Description: 小财最近新开了一家公司,招了n个员工,但是因为资金问题,办公楼只有m间 ...
- @codeforces - 717A@ Festival Organization
目录 @description@ @solution@ @accepted code@ @details@ @description@ 一个长度为 n 的 01 序列是好的,当且仅当该序列任意两个 0 ...
- P1754 球迷购票问题 (卡特兰数,递推)
题目背景 盛况空前的足球赛即将举行.球赛门票售票处排起了球迷购票长龙. 按售票处规定,每位购票者限购一张门票,且每张票售价为50元.在排成长龙的球迷中有N个人手持面值50元的钱币,另有N个人手持面值1 ...
随机推荐
- Linux服务系列 MySQL安装(一)
yum 安装 MySQL5.7 最简单的方法! 正文 第一步 安装CentOS 略 CentOS 版本为6.5 第二步 安装 yum 仓库列表 使用yum 安装mysql,要使用mysql的yum仓库 ...
- ab工具进行压力测试
简介与安装 ab:Apache Benchmark,只要我们安装了Apache,就能够在Apache的安装目录中找到它. yum | apt 安装的Apache ab的目录一般为/usr/bin 也 ...
- 【ABAP系列】SAP ABAP模块-ABAP动态指针写法的精髓部分
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP模块-ABAP动 ...
- mooc-IDEA 应用快捷键自动创建测试类--010
十六.IntelliJ IDEA -应用快捷键自动创建测试类 Step1:在类或接口上,按ctrl+shift+t 选择Create New Test... 则在相应测试包下.创建该测试类. 测试类:
- eclipse的maven配置及本地仓库配置
一.下载maven并解压 下载地址:http://maven.apache.org/download.cgi 解压后如下: 二.配置环境变量 配置MAVEN_HOME 再path中添加 安装成功 三. ...
- pthread_cond_timedwait
该函数用于在同时等待条件变量时提供超时功能,不过该函数的超时时间是一个绝对时间.默认使用系统时间,这意味这,若修改系统时间,那么超时就不准确,有可能提前返回,也可能要几年才返回.这在某些需求下会导致b ...
- Newtonsoft.Json 转Json字符串为空不序列化
原文:Newtonsoft.Json 转Json字符串为空不序列化 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://bl ...
- Windows程序设计--(二)Unicode 简介
2.2 宽字符和C语言 2.2.2 更宽的字符 在C语言中的宽字符正是基于short型数据的, 这一数据类型在头文件WCHAR.H中的定义为: typedef unsigned short wchar ...
- Maven Filter与Profile隔离生产环境与开发环境
Maven Filter与Profile隔离生产环境与开发环境 在不同的开发阶段,我们一般用到不同的环境,开发阶段使用开发环境的一套东西,测试环境使用测试环境的东西,可能有多个测试环境,生产环境使用的 ...
- kafka2.3集群搭建
环境: 3台centos7.4 3台zookeeper3.4.14 1. wget http://mirror.bit.edu.cn/apache/kafka/2.3.0/kafka_2.11-2.3 ...