HDU - 6333(莫队,组合数)

传送门

要求C (n,0) + C (n,1) + C (n,2) + … + C (n,m)
即S (n,m)
很明显,S (n,m) = S (n,m-1) + C (n,m)
由杨辉三角可以得到,S (n,m) = 2 * S (n-1,m) - C (n-1, m)
然后就用莫队,暴力(优雅…)地求出每个答案

#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<set>
#include<map>
#include<bitset>
#include<unordered_map>
using namespace std;
#define LL long long
#define eps (1e-9)
typedef unsigned long long ull;

const int maxn = 1e5 + 10;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const int bas = 131;
const LL mod = 1e9 + 7;

int block;
LL fac[maxn + 10], inv[maxn + 10];
LL ans[maxn];

LL qpow(LL a, LL b)
{
	LL res = 1;
	while (b)
	{
		if (b & 1)res = res * a % mod;
		a = a * a % mod; 
		b >>= 1;
	}
	return res;
}
void init()
{
	block = sqrt(maxn);//块的大小

	fac[0] = 1, inv[0] = 1;
	for (LL i = 1; i <= maxn; i++)
	{
		fac[i] = fac[i - 1] * i % mod;
	}
	inv[maxn] = qpow(fac[maxn], mod - 2);
	for (int i = maxn - 1; i >= 1; i--)
	{
		inv[i] = inv[i + 1] * (i + 1) % mod;
	}
}

struct node
{
	int n, m, id;
}a[maxn];
bool cmp(node a, node b)
{
	if (a.m / block == b.m / block)return a.n < b.n;
	return a.m < b.m;
}

LL cal(int n, int m)
{
	return fac[n] * inv[n - m] % mod * inv[m] % mod;
}

int main()
{
	init();
	int t;
	scanf("%d", &t);
	for (int i = 1; i <= t; i++)
	{
		scanf("%d%d", &a[i].n, &a[i].m);
		a[i].id = i;
	}
	sort(a + 1, a + t + 1, cmp);
	int L = 0, r = 0;
	LL res = 1;
	for (int i = 1; i <= t; i++)
	{
		while (L < a[i].n)res = ((2 * res - cal(L, r)) % mod + mod) % mod, L++;
		while (L > a[i].n)L--, res = (res + cal(L, r)) % mod * inv[2] % mod;
		while (r < a[i].m)r++, res = (res + cal(L, r)) % mod;
		while (r > a[i].m)res = ((res - cal(L, r)) % mod + mod) % mod, r--;
		ans[a[i].id] = res;
	}

	for (int i = 1; i <= t; i++)
	{
		printf("%lld\n", ans[i]);
	}

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值