• 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
     

     
    $n,m$这么大,$mod$这么小
    上个裸的$Lucas$
    解决。
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #define re register
     5 using namespace std;
     6 int min(int a,int b){return a<b?a:b;}
     7 const int p=1e4+7;
     8 int t,n,m,fac[p+2];
     9 int Pow(int x,int y){
    10     int res=1;
    11     for(;y;y>>=1,x=1ll*x*x%p)
    12         if(y&1) res=1ll*res*x%p;
    13     return res;
    14 }
    15 int C(int a,int b){
    16     return a<b?0:1ll*fac[a]*Pow(fac[b],p-2)%p*Pow(fac[a-b],p-2)%p;
    17 }
    18 int lucas(int a,int b){
    19     return b?1ll*lucas(a/p,b/p)%p*C(a%p,b%p)%p:1;
    20 }
    21 int main(){
    22     scanf("%d",&t); fac[0]=1;
    23     for(int i=1;i<=p;++i) fac[i]=1ll*fac[i-1]*i%p;
    24     while(t--){
    25         scanf("%d%d",&n,&m);
    26         printf("%d
    ",lucas(n,min(m,n-m)));
    27     }return 0;
    28 }
    View Code
  • 相关阅读:
    9.1 一周学习总结
    EasyUI(添加购物车小demo)
    助你了解react的小demo
    PHP(一)OOP基础
    继承的三种方式
    jQuery基础应用
    刘强1109 JavaScript基础二(分支与循环结构)
    JavaScript基础一(js基础函数与运算符)
    移动开发与响应式
    css基础语法三
  • 原文地址:https://www.cnblogs.com/kafuuchino/p/9906626.html
Copyright © 2020-2023  润新知