P4071 [SDOI2016] Permutation count | Permutation and combination

Title description

Find out how many & 1 & to & n & arrangements & a & satisfy the sequence has exactly & m & positions & i &, so that & a_i = i &.

The answer is modulo & 10 ^ 9 + 7 &.

Input format

There are multiple sets of data in the test points of this question sheet.

The first line of input is an integer & T &, which represents the integer of the test data.

The following & T & lines, each line describes a set of test data.

For each set of test data, enter two integers per line, which in turn represent & n & and & m &.

Output format

A total of & T & lines are output. For each set of test data, a line of integers is output to represent the answer.


错 Exhaust formula f [i] = (i-1) (f [i-1] + f [i-2])

#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int mod=1e9+7,N=1e6+10,M=1e6;
#define int long long
inline int read(){
	int x=0; char c=getchar();
	while(c<'0'||c>'9')c=getchar();
	while('0'<=c&&c<='9'){ x=(x<<1)+(x<<3)+(c^48); c=getchar(); }
	return x;
}
int jc[N],inv[N],D[N];
inline int ksm(int x,int y){
	int res=1;
	while(y){
		if(y&1)res=res*x%mod;
		x=x*x%mod; y>>=1;
	}
	return res;
}
inline void pre(){
	jc[0]=1; for(int i=1;i<=M;i++)jc[i]=jc[i-1]*i%mod;
	inv[M]=ksm(jc[M],mod-2);
	for(int i=M-1;i>=0;i--)inv[i]=inv[i+1]*(i+1)%mod;
	D[0]=1,D[1]=0,D[2]=1;
	for(int i=3;i<=M;i++)D[i]=(i-1)*(D[i-1]+D[i-2])%mod;
}
inline int C(int x,int y){
	if(y>x)return 0;
	return jc[x]*inv[x-y]%mod*inv[y]%mod;
}
int n,m;
signed main(){
	int T=read(); pre();
	while(T--){
		n=read(),m=read();
		printf("%lld\n",C(n,m)*D[n-m]%mod);
	}
}

Guess you like

Origin www.cnblogs.com/naruto-mzx/p/12677782.html