【BZOJ4517】排列计数(排列组合)

题意:1-n的一个序列,其中有m个a[i]=i,求方案数

n,m<=1000000

题意:显然ANS=c(n,m)*d[n-m]

d[i]为错排方案数=d[i-1]*n+(-1)^n

 1 const mo=1000000007;
 2 var fac,exf,d:array[0..1000000]of int64;
 3     cas,i,n,m:longint;
 4 
 5 function c(x,y:longint):int64;
 6 begin
 7  exit(fac[x]*exf[y] mod mo*exf[x-y] mod mo);
 8 end;
 9 
10 begin
11  assign(input,'bzoj4517.in'); reset(input);
12  assign(output,'bzoj4517.out'); rewrite(output);
13  d[1]:=0;
14  for i:=2 to 1000000 do
15  begin
16   d[i]:=d[i-1]*i;
17   if i and 1=0 then inc(d[i])
18    else dec(d[i]);
19   d[i]:=d[i] mod mo;
20  end;
21  fac[0]:=1; exf[0]:=1; exf[1]:=1;
22  for i:=1 to 1000000 do fac[i]:=fac[i-1]*i mod mo;
23  for i:=2 to 1000000 do exf[i]:=exf[mo mod i]*(mo-mo div i) mod mo;
24  for i:=1 to 1000000 do exf[i]:=exf[i-1]*exf[i] mod mo;
25  readln(cas); d[0]:=1;
26  for i:=1 to cas do
27  begin
28   readln(n,m);
29   writeln(c(n,m)*d[n-m] mod mo);
30  end;
31  close(input);
32  close(output);
33 end.

 

null

posted on 2017-02-26 18:47  myx12345  阅读(159)  评论(0编辑  收藏  举报

导航