博客
关于我
agc005D ~K Perm Counting
阅读量:296 次
发布时间:2019-03-01

本文共 1841 字,大约阅读时间需要 6 分钟。

排列问题的解决方案:

要解决满足条件的排列数量问题,可以将其转化为二分图匹配问题。具体步骤如下:

  • 问题转化为二分图匹配

    • 构造二分图,其中左边的点代表排列的位置i,右边的点同样代表位置j。
    • 两个点i和j之间有边当且仅当满足|i - j| = k。即,位置i可以移动到位置j,且差为k。
  • 动态规划计算匹配数量

    • 定义动态规划数组f[i][j][0]和f[i][j][1],分别表示从位置i开始到末尾的匹配状态。
    • 初始化f[1][0][0] = 1,表示从位置1开始的初始状态。
    • 遍历每个位置i和状态,更新动态规划数组,考虑是否匹配当前位置或是跳过当前位置。
    • 当位置i不能匹配时,状态转移可能需要考虑前面的匹配情况。
  • 计算g[i]的值

    • g[i]表示从位置i开始的所有可能的匹配数目,可能表示某种状态转移的总和。
    • 通过动态规划结果累加,得到每个位置i的匹配数量。
  • 计算最终答案

    • 使用g[i]和预先计算的阶乘数组,根据排列的位置进行计算。
    • 应用模运算确保数值在合理范围内。
  • 代码实现:

    #include 
    #define maxn 2000#define mod 924844033int read() { int x = 0, f = 1; char ch = getchar(); while ((ch < '0') || (ch > '9')) { if (ch == '-') f = -f; ch = getchar(); } while ((ch >= '0') && (ch <= '9')) { x = x * 10 + ch - '0'; ch = getchar(); } return x * f;}int main() { n = read(); k = read(); for (int i = 1; i <= n; ++i) { for (int j = 0; j < 2; ++j) { if (!vis[i][j]) { int len = 0; for (int x = i, y = j; x <= n; x += k, y ^= 1) { vis[x][y] = 1; ++len; } t += len; a[t + 1] = 1; } } } f[1][0][0] = 1; for (int i = 2; i <= t; ++i) { f[i][0][0] = (f[i-1][0][0] + f[i-1][0][1]) % mod; for (int j = 1; j <= n; ++j) { f[i][j][0] = (f[i-1][j][0] + f[i-1][j][1]) % mod; if (!a[i]) { f[i][j][1] = f[i-1][j-1][0]; } } } for (int i = 0; i <= n; ++i) { g[i] = (f[t][i][0] + f[t][i][1]) % mod; } fac[0] = 1; for (int i = 1; i <= n; ++i) { fac[i] = (fac[i-1] * i) % mod; } ans = 0; for (int i = 0; i <= n; ++i) { ans = (ans + ((i % 2 == 1) ? (mod - 1) : 1) * g[i] % mod * fac[n - i]) % mod; } printf("%d\n", ans); return 0;}

    通过上述步骤,可以高效地计算出满足条件的排列数量。

    转载地址:http://bcwo.baihongyu.com/

    你可能感兴趣的文章
    npm发布自己的组件UI包(详细步骤,图文并茂)
    查看>>
    npm和package.json那些不为常人所知的小秘密
    查看>>
    npm和yarn清理缓存命令
    查看>>
    npm和yarn的使用对比
    查看>>
    npm如何清空缓存并重新打包?
    查看>>
    npm学习(十一)之package-lock.json
    查看>>
    npm安装 出现 npm ERR! code ETIMEDOUT npm ERR! syscall connect npm ERR! errno ETIMEDOUT npm ERR! 解决方法
    查看>>
    npm安装crypto-js 如何安装crypto-js, python爬虫安装加解密插件 找不到模块crypto-js python报错解决丢失crypto-js模块
    查看>>
    npm安装教程
    查看>>
    npm报错Cannot find module ‘webpack‘ Require stack
    查看>>
    npm报错Failed at the node-sass@4.14.1 postinstall script
    查看>>
    npm报错fatal: Could not read from remote repository
    查看>>
    npm报错File to import not found or unreadable: @/assets/styles/global.scss.
    查看>>
    npm报错TypeError: this.getOptions is not a function
    查看>>
    npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
    查看>>
    npm淘宝镜像过期npm ERR! request to https://registry.npm.taobao.org/vuex failed, reason: certificate has ex
    查看>>
    npm版本过高问题
    查看>>
    npm的“--force“和“--legacy-peer-deps“参数
    查看>>
    npm的安装和更新---npm工作笔记002
    查看>>
    npm的常用操作---npm工作笔记003
    查看>>