题目: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. python实现基于两张图片生成圆角图标效果的方法

    python实现基于两张图片生成圆角图标效果的方法 这篇文章主要介绍了python实现基于两张图片生成圆角图标效果的方法,实例分析了Python使用pil模块进行图片处理的技巧,分享给大家供大家参考. ...

  2. SVG绘制随机的柱形图+php

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. Bootstrap 学习笔记12 轮播插件

    轮播插件: <!-- data-ride="carousel"自动播放 --> <div id="myCarousel" class=&quo ...

  4. 理解jquery的$.extend()、$.fn.extend()【jQuery插件机制】

    /** * 操作数据 | DOM 操作 * @description jQuery类添加类方法,可以理解为添加静态方法,将一个或多个对象的内容合并到目标对象 * @use $.fn.zhang() | ...

  5. Aizu - ALDS1_4_C Dictionary

    Search III Your task is to write a program of a simple dictionary which implements the following ins ...

  6. lib.tcl

    #********************************************************************# 功能描述:定义公共的函数# 依赖关系:依赖于全局aitoo ...

  7. 2019 年「计算机科学与工程学院」新生赛 暨ACM集训队选拔赛 # 1

    T1 请问这还是纸牌游戏吗 https://scut.online/p/567 这道题正解据说是方根 这里先放着等以后填坑吧qwq 但是由于这道题数据是随机的 所以其实是有各种水法的(但是我比赛根本没 ...

  8. mysql数据库操作指令汇总

    1.mysql -u root -p 登录数据库 2.表结构相同两表数据对拷 insert into A select * from B(插入全部字段数据)   insert into A(字段1.字 ...

  9. 攻防世界--csaw2013reversing2

    测试文件:https://adworld.xctf.org.cn/media/task/attachments/3f35642056324371b913687e770e97e6.exe 1.准备 打开 ...

  10. elasticsearch 基础 —— URI搜索

    URI搜索 可以通过提供请求参数使用URI来执行搜索请求.使用此模式执行搜索时,并非所有搜索选项都会暴露.这是一个例子: GET twitter/_search?q=user:kimchy 示例响应: ...