题目: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——斯特林数+递推求通项+扩域的更多相关文章

  1. LOJ 3090 「BJOI2019」勘破神机——斯特林数+递推式求通项+扩域

    题目:https://loj.ac/problem/3090 题解:https://www.luogu.org/blog/rqy/solution-p5320 1.用斯特林数把下降幂化为普通的幂次求和 ...

  2. NYOJ-301递推求值

    递推求值 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 给你一个递推公式: f(x)=a*f(x-2)+b*f(x-1)+c 并给你f(1),f(2)的值,请求出f ...

  3. 算法笔记_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- ...

  4. NYOJ——301递推求值(矩阵快速幂)

    递推求值 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 给你一个递推公式: f(x)=a*f(x-2)+b*f(x-1)+c 并给你f(1),f(2)的值,请求出f(n)的 ...

  5. poj 3744 Scout YYF I(递推求期望)

    poj 3744 Scout YYF I(递推求期望) 题链 题意:给出n个坑,一个人可能以p的概率一步一步地走,或者以1-p的概率跳过前面一步,问这个人安全通过的概率 解法: 递推式: 对于每个坑, ...

  6. Java实现 蓝桥杯 算法提高 递推求值

    算法提高 递推求值 时间限制:1.0s 内存限制:256.0MB 问题描述 已知递推公式: F(n, 1)=F(n-1, 2) + 2F(n-3, 1) + 5, F(n, 2)=F(n-1, 1) ...

  7. ACM_数数有多少(第二类Stirling数-递推dp)

    数数有多少 Time Limit: 2000/1000ms (Java/Others) Problem Description: 小财最近新开了一家公司,招了n个员工,但是因为资金问题,办公楼只有m间 ...

  8. @codeforces - 717A@ Festival Organization

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 一个长度为 n 的 01 序列是好的,当且仅当该序列任意两个 0 ...

  9. P1754 球迷购票问题 (卡特兰数,递推)

    题目背景 盛况空前的足球赛即将举行.球赛门票售票处排起了球迷购票长龙. 按售票处规定,每位购票者限购一张门票,且每张票售价为50元.在排成长龙的球迷中有N个人手持面值50元的钱币,另有N个人手持面值1 ...

随机推荐

  1. redis主从与集群搭建

    redis搭建主从 条件:yum安装(3.2.1)与编译安装(5.0.0)都可以 环境:我这里在同一台主机上搭建,当然也可以两台. 1) 复制redis.conf的主配置文件并命令为slave.con ...

  2. 微软手写识别模块sdk及delphi接口例子

    http://download.csdn.net/download/coolstar1204/2008061 微软手写识别模块sdk及delphi接口例子

  3. node+express解决前端跨域问题

    var express = require('express') , app = express(); //解决跨域 app.all('*',function (req, res, next) { r ...

  4. cannot open shared object file: No such file or directory解决

    cannot open shared object file: No such file or directory解决   ./move_db: error while loading shared ...

  5. PAT甲级【2019年9月考题】——A1164 DijkstraSequence【30】

    7-4 Dijkstra Sequence (30 分) Dijkstra's algorithm is one of the very famous greedy algorithms. It is ...

  6. BZOJ 1109 (LIS)

    题面 传送门 分析 设dp[i]是第i个积木在自己的位置上时,前i个积木中最多能回到自己位置的数目. \(dp[i]=max(dp[j])+1 (i>j,a[i]>a[j],a[i]-a[ ...

  7. python学习第二十天文件操作方法

    字符有的存储在内存,有的存储在硬盘,文件也有增删改查的操作方法,open()方法,read()方法,readline()方法,close()文件关闭,write()写的方法,seek() 指针移动方法 ...

  8. CSRF——跨站请求伪造

    一.CSRF是什么CSRF,全称:Corss-site request forgery,中文名称:跨站请求伪造.CSRF攻击比XSS攻击更具危险性,被安全界称为“沉睡的巨人”. 二.CSRF可以做什么 ...

  9. 如何判断元素是否在可视区域内--getBoundingClientRect

    介绍 Element.getBoundingClientRect()方法返回元素的大小及其相对于视口的位置. 根据MDN文档 getBoundingClientRect 方法返回的是一个DOMRect ...

  10. Android 虚线实现绘制 - DashPathEffect

    前言: 通过view绘制虚实线,采用Android自带API--DashPathEffect.具体使用请参考更多的链接,这里只是讲解. 构造函数 DashPathEffect 的构造函数有两个参数: ...