[CF] D. Almost Identity Permutations - Educational Codeforces Round 32

https://codeforces.com/contest/888/problem/D

题目大意

问至多有 $k \ (1 \le k \le 4)$ 个位置不对的 $n \ (4 \le n \le 1000)$ 长的排列有多少。

简要题解

因为 $k$ 很小,所以我们直接把 $1 \sim 4$ 的错位排列全写出来,然后直接选位置就可以了。

复杂度

$T$:$O(n)$

$S$:$O(1)$

代码实现

#include <bits/stdc++.h>
using namespace std;
 
int io_=[](){ ios::sync_with_stdio(false); cin.tie(nullptr); return 0; }();
 
using LL = long long;
using ULL = unsigned long long;
using LD = long double;
using PII = pair<int, int>;
using VI = vector<int>;
using MII = map<int, int>;
 
template<typename T> void cmin(T &x,const T &y) { if(y<x) x=y; }
template<typename T> void cmax(T &x,const T &y) { if(x<y) x=y; }
template<typename T> bool ckmin(T &x,const T &y) { 
    return y<x ? (x=y, true) : false; }
template<typename T> bool ckmax(T &x,const T &y) { 
    return x<y ? (x=y, true) : false; }
template<typename T> void cmin(T &x,T &y,const T &z) {// x<=y<=z 
    if(z<x) { y=x; x=z; } else if(z<y) y=z; }
template<typename T> void cmax(T &x,T &y,const T &z) {// x>=y>=z
    if(x<z) { y=x; x=z; } else if(y<z) y=z; }
 
// mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());
// mt19937_64 rnd_64(chrono::system_clock::now().time_since_epoch().count());
 
/*
---------1---------2---------3---------4---------5---------6---------7---------
1234567890123456789012345678901234567890123456789012345678901234567890123456789
*/

// 2 1
// 3 2
// 4 9

LL C(int n, int m) {
  LL ans = 1;
  for (int i = 1, j = n; i <= m; i++, j--) {
    ans *= j;
    ans /= i;
  }
  return ans;
}

void solve() {
  LL n, k;
  cin >> n >> k;
  
  LL ans = 1;
  if (k >= 2) ans += C(n, 2);
  if (k >= 3) ans += C(n, 3) * 2;
  if (k >= 4) ans += C(n, 4) * 9;
  
  cout << ans << '\n';
}
 
int main() {
  int t = 1; 
 // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
Prev: [CF] C. K-Dominant Character - Educational Codeforces Round 32
Next: [CF] B. Find the Permutation - Codeforces Round 997 (Div. 2)