• hdu 5894(组合数取模)


    hannnnah_j’s Biological Test

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
    Total Submission(s): 681    Accepted Submission(s): 235


    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
     
    Source
     
    题意:n个位子,m个人,每两个人之间至少隔k个位置,问排的方式?
    题解:设第一个人和第二个人之间的距离为 a1,第二个和第三个之间距离为 a2 ... 第 n个人和第1 个人之间距离为 an;
    a1+...+an = n-m....1
    ai>=k                ....2
    解方程 1,2得解为 C(n-m*k-1,m-1)
    第一个人有n种放法,所以答案要乘n,m个人没有区别,答案要除 m.
    这场比赛真心不爽,1010坑了3个小时,然后这个题目最后把组合数写出来了,刚好59分快5点了,然后没交上去....交上去...事后在hdu AC....又是2个题..
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    using namespace std;
    const long long  p =  1e9+7;
    typedef long long LL;
    LL pow_mod(LL a,LL n)
    {
        LL ans=1;
        while(n)
        {
            if(n&1) ans=ans*a%p;
            a=a*a%p;
            n>>=1;
        }
        return ans;
    }
    LL cm(LL n,LL m,LL mod)
    {
        if(m>n) return 0;
        LL i,ans=1,a,b;
        for(i=0; i<m; i++)
        {
            a=(n-i)%mod;
            b=(m-i)%mod;
            ans=ans*( a*pow_mod(b,mod-2)%mod )%mod;
        }
        return ans;
    }
    LL lucas(LL n,LL m,LL p)
    {
        if(m==0) return 1;
        return ( cm(n%p,m%p,p)*lucas(n/p,m/p,p) )%p;
    }
    
    int main()
    {
        int T;
        long long n,m,k;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%lld %lld %lld",&n,&m,&k);
            LL a = lucas(n-m*k-1,m-1,p);
            LL c = pow_mod(m,p-2)%p;
            printf("%lld
    ",((a*n)%p*c)%p);
    
        }
        return 0;
    }
  • 相关阅读:
    HDU 5119 Happy Matt Friends(DP || 高斯消元)
    URAL 1698. Square Country 5(记忆化搜索)
    POJ 2546 Circular Area(两个圆相交的面积)
    URAL 1430. Crime and Punishment(数论)
    HDU 1111 Secret Code (DFS)
    HDU 1104 Remainder (BFS求最小步数 打印路径)
    URAL 1091. Tmutarakan Exams(容斥原理)
    PDO连接mysql8.0报PDO::__construct(): Server sent charset (255) unknown to the client. Please, report to the developers错误
    swoole简易实时聊天
    centos安装netcat
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5890985.html
Copyright © 2020-2023  润新知