zoukankan      html  css  js  c++  java
  • BZOJ2982:combination(Lucas)

    Description

    LMZn个不同的基友,他每天晚上要选m个进行[河蟹],而且要求每天晚上的选择都不一样。那么LMZ能够持续多少个这样的夜晚呢?当然,LMZ的一年有10007天,所以他想知道答案mod 10007的值。(1<=m<=n<=200,000,000)

    Input

      第一行一个整数t,表示有t组数据。(t<=200)
      接下来t行每行两个整数n, m,如题意。

    Output

    T行,每行一个数,为C(n, m) mod 10007的答案。

    Sample Input

    4
    5 1
    5 2
    7 3
    4 2

    Sample Output

    5
    10
    35
    6

    Solution

    Lucas模板

    Code

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #define N (50000+1000)
     5 #define MOD (10007)
     6 using namespace std;
     7 
     8 int fac[N],facInv[N],inv[N],T,n,m;
     9 
    10 void Init(int p)
    11 {
    12     fac[0]=1; inv[1]=1; facInv[0]=1;
    13     for (int i=1; i<=p; ++i)
    14     {
    15         if (i!=1) inv[i]=(p-p/i)*inv[p%i]%p;
    16         fac[i]=fac[i-1]*i%p; facInv[i]=facInv[i-1]*inv[i]%p;
    17     }
    18 }
    19 
    20 int C(int n,int m)
    21 {
    22     if (m>n) return 0;
    23     return fac[n]*facInv[m]%MOD*facInv[n-m]%MOD;
    24 }
    25 
    26 int Lucas(int n,int m)
    27 {
    28     if (m>n) return 0;
    29     long long ans=1;
    30     for (; m; n/=MOD,m/=MOD)
    31         ans=(ans*C(n%MOD,m%MOD))%MOD;
    32     return ans;
    33 }
    34 
    35 int main()
    36 {
    37     Init(MOD);
    38     scanf("%d",&T);
    39     while (T--)
    40     {
    41         scanf("%d%d",&n,&m);
    42         printf("%d
    ",Lucas(n,m));
    43     }
    44 }
  • 相关阅读:
    test14
    test13
    test12
    test11
    Xcode常用快捷键
    OC弱语法
    对象的结构体属性的成员修改
    IOS 获取手机各种信息
    IOS app启动过程
    iOS退出键盘的两种方式
  • 原文地址:https://www.cnblogs.com/refun/p/9237003.html
Copyright © 2011-2022 走看看