BZOJ4403: 序列统计【lucas定理+组合数学】
Description
给定三个正整数N、L和R,统计长度在1到N之间,元素大小都在L到R之间的单调不降序列的数量。输出答案对10^6+3取模的结果。
Input
输入第一行包含一个整数T,表示数据组数。
第2到第T+1行每行包含三个整数N、L和R,N、L和R的意义如题所述。
1≤N,L,R≤10^9,1≤T≤100,输入数据保证L≤R。
Output
输出包含T行,每行有一个数字,表示你所求出的答案对10^6+3取模的结果。
Sample Input
2
1 4 5
2 4 5
Sample Output
2
5
//【样例说明】满足条件的2个序列为[4]和[5]。
思路
推一下式子发现答案是\(\sum_{i=1}^{n}C_{r-l+i}^i=C_{r-l+n+1}^n-1\)
#include<bits/stdc++.h>
using namespace std;
const int Mod = 1e6 + 3;
int f[Mod + 10];
int inv[Mod + 10], fac[Mod + 10];
int add(int a, int b) {
return (a += b) >= Mod ? a - Mod : a;
}
int sub(int a, int b) {
return (a -= b) < 0 ? a + Mod : a;
}
int mul(int a, int b) {
return 1ll * a * b % Mod;
}
int fast_pow(int a, int b) {
int res = 1;
while (b) {
if (b & 1) res = mul(res, a);
b >>= 1;
a = mul(a, a);
}
return res;
}
void init() {
fac[0] = inv[0] = 1;
for (int i = 1; i < Mod; i++) fac[i] = mul(fac[i - 1], i);
inv[Mod - 1] = fast_pow(fac[Mod - 1], Mod - 2);
for (int i = Mod - 2; i >= 1; i--) inv[i] = mul(inv[i + 1], i + 1);
}
int C(int a, int b) {
return mul(fac[a], mul(inv[b], inv[a - b]));
}
int lucas(int a, int b) {
if (a < b) return 0;
if (a < Mod && b < Mod) return C(a, b);
return mul(C(a % Mod, b % Mod), lucas(a / Mod, b / Mod));
}
int main() {
freopen("input.txt", "r", stdin);
init();
int T; scanf("%d", &T);
while (T--) {
int n, l, r;
scanf("%d %d %d", &n, &l, &r);
printf("%d\n", sub(lucas(r - l + n + 1, n), 1));
}
return 0;
}
BZOJ4403: 序列统计【lucas定理+组合数学】的更多相关文章
- Bzoj 4403: 序列统计 Lucas定理,组合数学,数论
4403: 序列统计 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 328 Solved: 162[Submit][Status][Discuss] ...
- 【BZOJ4403】序列统计 Lucas定理
[BZOJ4403]序列统计 Description 给定三个正整数N.L和R,统计长度在1到N之间,元素大小都在L到R之间的单调不降序列的数量.输出答案对10^6+3取模的结果. Input 输入第 ...
- BZOJ4403 序列统计—Lucas你好
绝对是全网写的最详细的一篇题解 题目:序列统计 代码难度:简单 思维难度:提高+-省选 讲下题面:给定三个正整数N.L和R,统计长度在1到N之间,元素大小都在L到R之间的单调不降序列的数量.输出答案 ...
- bzoj 4403 序列统计 卢卡斯定理
4403:序列统计 Time Limit: 3 Sec Memory Limit: 128 MB Description 给定三个正整数N.L和R,统计长度在1到N之间,元素大小都在L到R之间的单调 ...
- bzoj4403: 序列统计
我们很容易发现答案是C(R-L+N+1,N)-1 然后用一下lucas定理就行了 #include <iostream> #include <cstdio> #include ...
- 2018.09.09 bzoj4403: 序列统计(Lucas定理)
传送门 感觉单调不降序列什么的不好做啊. 于是我们序列中下标为i的元素的值加上i,这样就构成了一个单调递增的序列. 问题就变成了: 求出构造长度分别为1 ~ n且每个元素的值在l+1 ~ r+n之间的 ...
- bzoj4403 序列统计——组合数学
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4403 一开始想了个 O(n) 的做法,不行啊... O(n)想法是这样的:先考虑递推,设 f ...
- 【BZOJ4403】序列统计(组合数学,卢卡斯定理)
[BZOJ4403]序列统计(组合数学,卢卡斯定理) 题面 Description 给定三个正整数N.L和R,统计长度在1到N之间,元素大小都在L到R之间的单调不降序列的数量.输出答案对10^6+3取 ...
- 【BZOJ4403】序列统计(Lucas定理,组合计数)
题意:给定三个正整数N.L和R, 统计长度在1到N之间,元素大小都在L到R之间的单调不降序列的数量. 输出答案对10^6+3取模的结果. 对于100%的数据,1≤N,L,R≤10^9,1≤T≤100, ...
随机推荐
- Android 获取本地外网IP、内网IP、计算机名等信息
一.获取本地外网IP public static String GetNetIp() { URL infoUrl = null; InputStream inStream = null; try { ...
- 雷林鹏分享:Ruby 块
Ruby 块 您已经知道 Ruby 如何定义方法以及您如何调用方法.类似地,Ruby 有一个块的概念. 块由大量的代码组成. 您需要给块取个名称. 块中的代码总是包含在大括号 {} 内. 块总是从与其 ...
- 12月15日 session:Ruby on Rails Security Guide//从第3节开始没有学习//关于find_by 和where的区别用法思考。
http://guides.rubyonrails.org/security.html#user-management 2.session笔记见13日的随笔. http://www.cnblogs.c ...
- 41 MYSQL 索引和慢查询优化
一 .索引mysql 索引 b+tree 本质:通过不断地缩小想要获取数据的范围来筛选出最终想要的结果,同时把随机的事件变成顺序的事件,也就是说,有了这种索引机制,我们可以总是用同一种查找方式来锁定数 ...
- mac 地址
- hdu1238 kmp
You are given a number of case-sensitive strings of alphabetic characters, find the largest string X ...
- vuecli3 引入script 针对没有cmd amd require等方式的js
最近做高德开发,需要引入高德的js,但是 说实话 高德官方的文档不知道大佬们有没有看懂,反正我是没看懂,写的都什么鬼?我怎么引都引入不了,迫不得已想到了如下方法: 一.准备一个能够在页面中插入js的方 ...
- iOS UI-创建空项目
一.创建工程 二.删除ViewController 三.在Supporting Files/Info.plist文件中清空值 四.创建新的控制器 五.写代码 1.在AppDelegate.h文件中 # ...
- react项目打包后路径找不到,项目打开后页面空白的问题
使用 npm install -g create-react-app快速生成项目脚手架打包后出现资源找不到的路径问题: 解决办法:在package.json设置homepage
- linux make virtual memory more efficient three components
Page Cache This is used to speed up access to images and data on disk. As pages are read into memory ...