[ AGC005 D ] ~K Perm Counting

题目

Atcoder

思路

001.png 002.png 003.png

代码

#include <iostream>
#include <cstring>
#include <algorithm>
#define int long long
using namespace std;
const int N = 2010, mod = 924844033;
int n, m, st[N << 1], f[N << 1][N][2], fact[N]{1};
signed main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        fact[i] = fact[i - 1] * i % mod;
    int tot = 0;
    for (int i = 1; i <= m; i++)
        for (int t = 0; t < 2; t++) // 循环两次, 因为有数值和位置两种开始方式
            for (int j = i; j <= n; j += m) 
                if (++tot && i != j) st[tot] = 1;
    f[0][0][0] = 1;
    for (int i = 1; i <= n << 1; i++) 
        for (int j = 0; j <= n; j++) {
            f[i][j][0] = (f[i - 1][j][0] + f[i - 1][j][1]) % mod;
            if (st[i] && j) f[i][j][1] = f[i - 1][j - 1][0];
        }
    int res = 0;
    for (int i = 0; i <= n; i++) 
        res = (res + (fact[n - i] * ((i & 1) ? -1 : 1) * 
                     (f[n << 1][i][0] + f[n << 1][i][1]) % mod) + mod) % mod;
    cout << res << endl;
    return 0;
}
posted @ 2021-05-20 18:00  Protein_lzl  阅读(44)  评论(0编辑  收藏  举报