博客
关于我
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/

    你可能感兴趣的文章
    nnU-Net 终极指南
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    NO 157 去掉禅道访问地址中的zentao
    查看>>
    no available service ‘default‘ found, please make sure registry config corre seata
    查看>>
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>
    No resource identifier found for attribute 'srcCompat' in package的解决办法
    查看>>
    no session found for current thread
    查看>>
    No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
    查看>>
    NO.23 ZenTaoPHP目录结构
    查看>>