zoukankan      html  css  js  c++  java
  • UVa 11481 (计数) Arrange the Numbers

    居然没有往错排公式那去想,真是太弱了。

    先在前m个数中挑出k个位置不变的数,有C(m, k)种方案,然后枚举后面n-m个位置不变的数的个数i,剩下的n-k-i个数就是错排了。

    所以这里要递推一个组合数和错排数。

    顺便再复习一下错排递推公式,Dn = (n-1)(Dn-1 + Dn-2),D0 = 1,D1 = 0.

    这里强调一下D0的值,我之前就是因为直接从D1和D2开始递推的结果WA

     1 #include <cstdio>
     2 typedef long long LL;
     3 
     4 const int maxn = 1000 + 10;
     5 const LL M = 1000000007;
     6 LL c[maxn][maxn], d[maxn];
     7 
     8 inline LL mul(LL a, LL b) { return (a * b) % M; }
     9 
    10 void init()
    11 {
    12     for(int i = 0; i < maxn; i++) c[i][0] = c[i][i] = 1;
    13     for(int i = 2; i < maxn; i++)
    14         for(int j = 1; j < i; j++)
    15             c[i][j] = (c[i-1][j] + c[i-1][j-1]) % M;
    16     d[0] = 1; d[1] = 0; d[2] = 1;
    17     for(int i = 3; i < maxn; i++) d[i] = mul(i-1, (d[i-1] + d[i-2]) % M);
    18 }
    19 
    20 int main()
    21 {
    22     //freopen("in.txt", "r", stdin);
    23 
    24     init();
    25     int T; scanf("%d", &T);
    26     for(int kase = 1; kase <= T; kase++)
    27     {
    28         int n, m, k;
    29         scanf("%d%d%d", &n, &m, &k);
    30         LL ans = 0;
    31         for(int i = 0; i <= n - m; i++)
    32             ans = (ans + mul(c[n-m][i], d[n-k-i])) % M;
    33         ans = (ans * c[m][k]) % M;
    34         printf("Case %d: %lld
    ", kase, ans);
    35     }
    36 
    37     return 0;
    38 }
    代码君
  • 相关阅读:
    往鼠标位置写入 诗词
    shell条件判断
    shell 案例一
    Shell中的变量
    echo e 命令详解
    Docker 安装RedisJSON 与使用
    Python 插入数据库的各种方式
    Python压缩文件/文件夹
    shell运算符
    关于CAN总线简单总结
  • 原文地址:https://www.cnblogs.com/AOQNRMGYXLMV/p/4464768.html
Copyright © 2011-2022 走看看