UVALive

来源:互联网 发布:星光网络摄像机 编辑:程序博客网 时间:2024/04/29 09:41

题意:N道题连线,问至少前k道题目错误的可能有几种

思路:那就是所有连线可能的情况数(即n!) 减去前k道题存在正确答案的情况数

根据容斥原理,奇数个集合为正,偶数个集合为负计算即可

代码如下:

#include<bits/stdc++.h>using namespace std;typedef long long ll;const ll INF = 1e14 + 7;const int N = 1e5 + 10;int n, k;int main(){    int t, cas;    scanf("%d", &t);    while(t--)    {        scanf("%d%d%d", &cas, &n, &k);        ll ans = 1;        for(int i = 1; i <= n; i++) ans *= i;        ll cnt = 0;for(int i = 1; i <= k; i++){ll tmp = 1;for(int j = k; j > k - i; j--){tmp *= j;}for(int j = i; j > 1; j--){tmp /= j;}for(int j = n - i; j >= (n - k + 1); j--) tmp *= j;if(i % 2 == 0) cnt -= tmp;else cnt += tmp;}for(int i = n - k; i > 1; i--) cnt *= i;ans -= cnt;printf("%d %lld\n", cas, ans);     }    return 0;}/*41 4 12 7 33 10 54 17 17*/


0 0