标签:临时 size alt ssi image img for 允许 queue
2 2 2 3 2
1 3
题意是求n对夫妇里面有m对找错,可以看作从n对里面选m对进行错排
错排的递推式a[i]=(i-1)*(a[i-1]+a[i-2]);
所以答案就是c(n,m)*a[m]
1 #include<cstdio> 2 #include<cstring> 3 #include<string> 4 #include<cmath> 5 #include<iostream> 6 #include<algorithm> 7 #include<map> 8 #include<set> 9 #include<queue> 10 #include<vector> 11 using namespace std; 12 long long C(long long n,long long m) 13 { 14 long long p = 1; 15 for(int i = n-m+1; i <= n; i++) p*=i; 16 for(int i = 1; i <= m; i++) p/=i; 17 return p; 18 } 19 long long a[30]; 20 int main() 21 { 22 int c; 23 a[1]=0; 24 a[2]=1; 25 for(int i=3; i<=20; i++) 26 { 27 a[i]=(i-1)*(a[i-1]+a[i-2]); 28 } 29 while(~scanf("%d",&c)) 30 { 31 int n,m; 32 while(c--) 33 { 34 scanf("%d %d",&n,&m); 35 printf("%lld\n",C(n,m)*a[m]);//n对夫妻里面m对找错 36 } 37 38 } 39 return 0; 40 }
标签:临时 size alt ssi image img for 允许 queue
原文地址:https://www.cnblogs.com/fqfzs/p/9763767.html