记录编号 248775 评测结果 AAAAAAAAAAAAAAAAAAAA
题目名称 [SDOI 2016] 排列计数 最终得分 100
用户昵称 Gravatar铁策 是否通过 通过
代码语言 C++ 运行时间 9.224 s
提交时间 2016-04-11 13:41:43 内存使用 7.92 MiB
显示代码纯文本
#include <cstdio>
#include <cctype>
#define MOD 1000000007
int readint()
{
	int ans = 0;
	char c;
	while (!isdigit(c = getchar()));
	do
	{
		ans = ans * 10 + c - '0';
		c = getchar();
	} while (isdigit(c));
	return ans;
}
int cuopai[1000001], jiecheng[1000001];
int T, n, m;
int gcd(long long x, long long y)
{
	return (y ? gcd(y, x % y) : x);
}
void ex_gcd(long long a, long long b, long long &x, long long &y)
{
	if (!b)
	{
		x = 1;
		y = 0;
		return;
	}
	long long x1, y1;
	ex_gcd(b, a % b, x1, y1);
	x = y1,y = x1 - (a / b) * y1;
}
inline long long getrev(long long a)
{
	if (!a || gcd(a, MOD) > 1)
		return -1;
	long long x, y;
	ex_gcd(a, MOD, x, y);
	return (x + MOD) % MOD;
}
void prepare()
{
	cuopai[0] = cuopai[2] = 1;
	for (int i = 3; i <= 1000000; i++)
	{
		long long ls = i - 1;
		ls = ls * (cuopai[i - 2] + cuopai[i - 1]);
		ls %= MOD;
		cuopai[i] = (int)ls;
	}
	jiecheng[0] = jiecheng[1] = 1;
	for (int i = 2; i <= 1000000; i++)
	{
		long long ls = i;
		ls = ls * jiecheng[i - 1];
		ls %= MOD;
		jiecheng[i] = (int)ls;
	}
}
int main()
{
	freopen("menci_permutation.in", "r", stdin);
	freopen("menci_permutation.out", "w", stdout);
	prepare();
	T = readint();
	for (; T > 0; T--)
	{
		n = readint();
		m = readint();
		long long ls = jiecheng[n];
		ls = (ls * getrev(jiecheng[m])) % MOD;
		ls = (ls * getrev(jiecheng[n - m])) % MOD;
		ls = (ls * cuopai[n - m]) % MOD;
		printf("%d\n", (int)ls);
	}
	return 0;
}