BZOJ 2982 combination

Lucas定理模板题目

#include <iostream>
#include <string.h>
#include <cmath>
#define ll long long
using namespace std;
const int maxn=10000007;
ll n,m,p;
ll fac[maxn];
 
void getfac(ll p)//预处理阶层
{
    fac[0]=1;
    for(int i=1;i<=p;i++)
        fac[i]=fac[i-1]*i%p;
}
 
ll power(ll a,ll n,ll p)//快速幂运算
{
    ll ans=1;
    while(n)
    {
        if(n&1)
            ans=ans*a%p;
        a=a*a%p;
        n/=2;
    }
    return ans;
}
 
ll lucas(ll n,ll m,ll p)
{
    ll ans=1;
    while(n&&m)
    {
        ll a=n%p;
        ll b=m%p;
        if(a<b) return 0;
        ans=(ans*fac[a]*power(fac[b]*fac[a-b]%p,p-2,p))%p;//  fac[b]*fac[a-b]后面别忘了%p,否则WA
        n/=p;
        m/=p;
    }
    return ans;
}
 
 
int main()
{
    int t;cin>>t;
    getfac(10007);
    while(t--)
    {
        cin>>n>>m;
        cout<<lucas(n,m,10007)<<endl;
    }
    return 0;
}
时间: 2024-08-05 14:59:57

BZOJ 2982 combination的相关文章

BZOJ 2982: combination( lucas )

lucas裸题. C(m,n) = C(m/p,n/p)*C(m%p,n%p). ----------------------------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int MOD = 10007; int Inv

bzoj——2982: combination

2982: combination Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 611  Solved: 368[Submit][Status][Discuss] Description LMZ有n个不同的基友,他每天晚上要选m个进行[河蟹],而且要求每天晚上的选择都不一样.那么LMZ能够持续多少个这样的夜晚呢?当然,LMZ的一年有10007天,所以他想知道答案mod 10007的值.(1<=m<=n<=200,000,000) Inpu

BZOJ 2982 combination Lucas定理

题目大意:求C(n,m)%p. 思路:Lucas定理:C(n,m)%p = C(n/p,m/p)*C(n%p,m%p)%p 处理出来1~10007所有的阶乘和阶乘的逆元,nm都小于10007的时候就可以直接算了,剩下的情况递归处理. CODE: #define _CRT_SECURE_NO_WARNINGS #include <cstdio> #include <cstring> #include <iostream> #include <algorithm>

【BZOJ 2982】 2982: combination (卢卡斯定理)

2982: combination Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 510  Solved: 316 Description LMZ有n个不同的基友,他每天晚上要选m个进行[河蟹],而且要求每天晚上的选择都不一样.那么LMZ能够持续多少个这样的夜晚呢?当然,LMZ的一年有10007天,所以他想知道答案mod 10007的值.(1<=m<=n<=200,000,000) Input   第一行一个整数t,表示有t组数据.(t&l

【BZOJ】【2982】Combination

排列组合 Lucas定理模板题…… 感觉我做题顺序有点问题啊……应该是BZOJ 2982-->HDOJ 3037-->BZOJ 1272 好吧这个现在来看就有些水了…… 预处理一下fact和inv即可 1 /************************************************************** 2 Problem: 2982 3 User: Tunix 4 Language: C++ 5 Result: Accepted 6 Time:4 ms 7 Mem

组合数学lucas定理 BZOJ2982 combination

2982: combination Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 597  Solved: 357[Submit][Status][Discuss] Description LMZ有n个不同的基友,他每天晚上要选m个进行[河蟹],而且要求每天晚上的选择都不一样.那么LMZ能够持续多少个这样的夜晚呢?当然,LMZ的一年有10007天,所以他想知道答案mod 10007的值.(1<=m<=n<=200,000,000) Inpu

bzoj2982: combination(lucas定理板子)

2982: combination Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 664  Solved: 397[Submit][Status][Discuss] Description LMZ有n个不同的基友,他每天晚上要选m个进行[河蟹],而且要求每天晚上的选择都不一样.那么LMZ能够持续多少个这样的夜晚呢?当然,LMZ的一年有10007天,所以他想知道答案mod 10007的值.(1<=m<=n<=200,000,000) Inpu

10月刷题总结

(写的题真少QAQ 动态规划: [vijos]1286 座位安排(状压dp) [BZOJ]1026: [SCOI2009]windy数(数位dp) [BZOJ]1596: [Usaco2008 Jan]电话网络(树形dp+特殊的技巧) [BZOJ]1827: [Usaco2010 Mar]gather 奶牛大集会(树形dp) [BZOJ]2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛(树形dp) 计数: [vijos]1789 String(组合计数+奇怪的题)

BZOJ 4403 2982 Lucas定理模板

思路: Lucas定理的模板题.. 4403 //By SiriusRen #include <cstdio> using namespace std; const int mod=1000003; #define int long long int cases,N,L,R,fac[mod],inv[mod]; int C(int n,int m){ if(n<m)return 0; if(n<mod&&m<mod)return fac[n]*inv[n-m]