天天看點

「組合計數」CodeForces - 888D

題意&分析:

對于一個給定數列,求滿足 ai = i 的元素個數不少于 n-k 個的數列的個數。換句話說,就是求最多有 k 個元素不滿足 ai = i 的數列有幾個。

分析一下用排列數得出一下結論:

k = 1 時,ans = 1;

k = 2 時, ans = 1 + C(2,n);

k = 3 時 , ans = 1 + C(2,n) + C(3,n)*2 (其中三個元素排列不在自己位置上的情況隻有兩種,即2 3 1 和 3 1 2);

k = 4 時 , ans = 1 + C(2,n) + C(3,n) * 2 + C(4,n) * 9 (9同上理);

代碼如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <cctype>
#include <fstream>
#define INF 0x3f3f3f3f
#define EPS 1e-6
#define TEST cout<<"stop here"<<endl
using namespace std;
typedef long long ll;
const ll mod =  + ;

ll n,k;
int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie();

    while(cin>>n>>k){
        ll ans = ;
        if(k == ){
            ans = ;
        }
        else if(k == ){
            ans =  + n*(n-)/;
        }
        else if( k == ){
            ans =  + n*(n-)/ + *n*(n-)*(n-)/;
        }
        else if(k == ){
            ans =  + n*(n-)/ + *n*(n-)*(n-)/ + *n*(n-)*(n-)*(n-)/;
        }
        cout<< ans <<endl;
    }

    return ;
}