Educational Codeforces Round 32 D. Almost Identity Permutations

    xiaoxiao2022-07-08  195

    题目链接:http://codeforces.com/contest/888/problem/D 我用的组合数学做的,这里需要学一个公式。 错排的公式: D(1)= 0, D(2)= 1, D(n)= (n- 1)*(D(n-1)+ D(n-2)) 具体解释链接:https://www.cnblogs.com/c1299401227/p/5349727.html K范围为1-4

    k>=1 ans++; k>=2 ans+=d[2]*Cn2 k>=3 ans+=d[3]*Cn3 k>=4 ans+=d[4]*Cn4

    这里我Cnx暴力写的 先留个坑回头写On的算法的

    #include <bits/stdc++.h> using namespace std; #define ll long long #define forn(i,n) for(int i=0;i<n;i++) #define for1(i,n) for(int i=1;i<=n;i++) #define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0) ll C(ll n,ll x){ ll a=1,b=1; for(int i = n;i>n-x;i--) a*=i; for1(i,x) b*=i; return a/b; } int main(){ IO; int n,k;cin>>n>>k; vector<int>b = {0,1,2,9}; ll ans = 0; for(int i=2;i<=k;i++){ ans+=1ll*b[i-1]*C(n,i); //cerr<<C(n,i)<<'\n'; } ans++; cout <<ans<<'\n'; return 0; }
    最新回复(0)