bzoj4403: 序列统计

我们很容易发现答案是C(R-L+N+1,N)-1

然后用一下lucas定理就行了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#define ll long long
#define P 1000003

using namespace std;
int pow2(int x,int y){
	int ret=1;
	while (y){
		if (y&1) ret=(ll)ret*x%P;
		x=(ll)x*x%P;
		y=y>>1; 
	}
	return ret;
}

int fact[P+233];
int c(int n,int m){
	if (n<m) return 0;
	return (ll)fact[n]*pow2(fact[m],P-2)%P*pow2(fact[n-m],P-2)%P;
}
int lucas(int n,int m){
	int ret=1;
	for (;n||m;n/=P,m/=P) ret=(ll)ret*c(n%P,m%P)%P;
	return ret;
}


int main(){
	for (int i=fact[0]=1;i<=P;++i) fact[i]=(ll)fact[i-1]*i%P;
	int testnumber;scanf("%d",&testnumber);
	while (testnumber--){
		int n,l,r;
		scanf("%d%d%d",&n,&l,&r);
		printf("%d
",(lucas(r-l+n+1,n)+P-1)%P);
	}
	return 0;
}

  

原文地址:https://www.cnblogs.com/wangyurzee7/p/5148769.html