【BZOJ4403】【lucas】【组合数】序列统计 题解

10 篇文章 0 订阅

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]。

C(r-l+1,r-l) + C(r-l+2,r-l) +…+ C(r-l+n,r-l) = C(r-l+n+1,r-l+1) - C(r-l,r-l) = C(r-l+n+1,n)-1
MOD较小,用Lucas求一下C。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <stack>
#define INF 2147483647
#define LL long long
#define clr(x) memset(x, 0, sizeof x)
#define digit (ch <  '0' || ch >  '9')
#ifdef WIN32
#define AUTO "%I64d"
#else
#define AUTO "%lld"
#endif

using namespace std;

template <class T> inline void read(T &x) {
    int flag = 1; x = 0;
    register char ch = getchar();
    while( digit) { if(ch == '-')  flag = -1; ch = getchar(); }
    while(!digit) { x = (x<<1)+(x<<3)+ch-'0'; ch = getchar(); }
    x *= flag;
}

const int MOD = 1e6+3;
LL t,n,l,r;
LL fac[MOD+5],vfac[MOD+5];

inline LL comb(LL n, LL m) { return m > n ? 0 : fac[n]*vfac[m]%MOD*vfac[n-m]%MOD; }

inline LL lucas(LL n, LL m) {
    if(m > n) return 0;
    if(n < MOD) return comb(n, m);
    return lucas(n/MOD, m/MOD)*comb(n%MOD, m%MOD)%MOD;
}

int main() {
    read(t); fac[0] = vfac[1] = vfac[0] = 1;
    for(LL i = 1; i <= MOD; i++) fac[i] = (fac[i-1]*i)%MOD;
    for(LL i = 2; i < MOD; i++) vfac[i] = (MOD-MOD/i)*vfac[MOD%i]%MOD;
    for(LL i = 1; i < MOD; i++) (vfac[i] *= vfac[i-1]) %= MOD;
    while(t--) read(n), read(l), read(r), cout << (lucas(r-l+1+n, n)+MOD-1)%MOD << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值