zoukankan      html  css  js  c++  java
  • icpc2016沈阳网络赛1003hannnnah_j’s Biological Test组合数模板题

    hannnnah_j’s Biological Test

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)



    Problem Description
    hannnnah_j is a teacher in WL High school who teaches biology.

    One day, she wants to test m students, thus she arranges n different seats around a round table.

    In order to prevent cheating, she thinks that there should be at least k empty seats between every two students.

    hannnnah_j is poor at math, and she wants to know the sum of the solutions.So she turns to you for help.Can you help her? The answer maybe large, and you need to mod 1e9+7.
     
    Input
    First line is an integer T(T≤1000).
    The next T lines were given n, m, k, respectively.
    0 < m < n < 1e6, 0 < k < 1000
     
    Output
    For each test case the output is only one integer number ans in a line.
     
    Sample Input
    2 4 2 6 5 2 1
     
    Sample Output
    0 5
    思路:

      

     1 #include <iostream>
     2 #include <cstdio>
     3 
     4 using namespace std;
     5 typedef long long LL;
     6 const int p = 1e9 + 7;
     7 LL quick_mod(LL a, LL b) {
     8     LL ans = 1;
     9     a %= p;
    10     while(b) {
    11         if(b & 1) {
    12             ans = ans * a % p;
    13             b--;
    14         }
    15         b >>= 1;
    16         a = a * a % p;
    17     }
    18     return ans;
    19 }
    20 
    21 LL C(LL n, LL m) {
    22     if(m > n) return 0;
    23     LL ans = 1;
    24     for(int i=1; i<=m; i++) {
    25         LL a = (n + i - m) % p;
    26         LL b = i % p;
    27         ans = ans * (a * quick_mod(b, p-2) % p) % p;
    28     }
    29     return ans;
    30 }
    31 LL Lucas(LL n, LL m) {
    32     if(m == 0) return 1;
    33     return C(n % p, m % p) * Lucas(n / p, m / p) % p;
    34 }
    35 int main() {
    36     int k, T, n, m;
    37     scanf("%d", &T);
    38     while(T--){
    39         scanf("%d%d%d", &n, &m, &k);
    40         if(m == 1) printf("%d
    ", n);
    41         else {
    42             printf("%d
    ", n*Lucas(n-k*m-1, m-1)%p*quick_mod(m, p-2)%p);
    43         }
    44     }
    45     return 0;
    46 }

    看了看别人交的代码,发现自己的好慢

  • 相关阅读:
    web.xml报错
    mysql字符集问题汇总
    查询所有表中的某个数存储过程脚本
    SQL Server生成数据库的数据字典存储过程
    浏览器无法访问虚拟机的服务器
    搭建lnmp环境,nginx的配置文件/etc/nginx/nginx.conf
    在centos6.5下搭建lnmp
    Linux服务器关联Git,通过执行更新脚本实现代码同步
    CentOS yum 安装时错误 Errno 14 Couldn't resolve host 解决办法
    LinqToSQL3
  • 原文地址:https://www.cnblogs.com/cshg/p/5887279.html
Copyright © 2011-2022 走看看