hdu 5446 Unknown Treasure Lucas定理+中国剩余定理

Unknown Treasure

Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2209    Accepted Submission(s): 821

Problem Description

On the way to the next secret treasure hiding place, the mathematician discovered a cave unknown to the map. The mathematician entered the cave because it is there. Somewhere deep in the cave, she found a treasure chest with a combination lock and some numbers on it. After quite a research, the mathematician found out that the correct combination to the lock would be obtained by calculating how many ways are there to pick m different apples among n of them and modulo it with M. M is the product of several different primes.

Input

On the first line there is an integer T(T≤20) representing the number of test cases.

Each test case starts with three integers n,m,k(1≤m≤n≤1018,1≤k≤10) on a line where k is the number of primes. Following on the next line are k different primes p1,...,pk. It is guaranteed that M=p1⋅p2⋅⋅⋅pk≤1018 and pi≤105 for every i∈{1,...,k}.

Output

For each test case output the correct combination on a line.

Sample Input

1
9 5 2
3 5

Sample Output

6

Source

2015 ACM/ICPC Asia Regional Changchun Online

Recommend

hujie   |   We have carefully selected several similar problems for you:  5842 5841 5840 5839 5838

分析:

根据Lucas求解中 每一个i:C(n,m)%pi,然后根据中国剩余定理把这些结果整合起来。就能得到答案。

注意中国剩余定理在计算当前余数与其他余数乘积的最小公倍数的gcd为1的值时候,由于数据量太大,要取模。

#include<iostream>
#include<stdio.h>
using namespace std;
long long pri[15];
long long a[15];
long long ext_gcd(long long a,long long b,long long *x,long long *y)
{
    if(b==0)
    {
        *x=1,*y=0;
        return a;
    }
    long long r = ext_gcd(b,a%b,x,y);
    long long t = *x;
    *x= *y;
    *y = t - a/b * *y;
    return r;
}
long long quick_mod(long long n,long long m,long long mod)
{
    long long ans=1;
    while(m)
    {
        if(m&1)
            ans=(ans*n)%mod;
        m>>=1;
        n=(n*n)%mod;
    }
    return ans%mod;
}
long long get_c(long long n,long long m,long long mod)
{
    long long a=1,b=1;
    for(int i=1; i<=m; i++)
    {
        b=b*i%mod;
        a=a*(n-i+1)%mod;
    }
    return (a*(quick_mod(b,mod-2,mod)))%mod;
}
long long Lucas(long long n,long long m,long long mod)
{
    if(m==0) return 1;
    return (Lucas(n/mod,m/mod,mod)*get_c(n%mod,m%mod,mod))%mod;
}
long long mul(long long a,long long n,long long mod)
{
    a = (a%mod+mod)%mod;
    n = (n%mod+mod)%mod;
    long long ret =0;
    while(n)
    {
        if(n&1)
            ret=(ret+a)%mod;
        a=(a+a)%mod;
        n>>=1;
    }
    return ret%mod;
}
long long chinese_reminder(long long a[],long long pri[],int len)
{
    long long mul_pri=1;
    long long res=0;
    for(int i=0;i<len;i++)
    {
        mul_pri*=pri[i];
    }
    for(int i=0;i<len;i++)
    {
        long long m = mul_pri/pri[i];
        long long x,y;
        ext_gcd(pri[i],m,&x,&y);
        //res=(res+y*m*a[i])%mul_pri;
        res=(res+mul(mul(y,m,mul_pri),a[i],mul_pri))%mul_pri;
    }
    return ((res%mul_pri+mul_pri)%mul_pri);
}
int main()
{
    int t;
    long long n,m;
    int k;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%I64d%I64d%d",&n,&m,&k);
        for(int i=0; i<k; i++)
            scanf("%I64d",pri+i);
        for(int i=0; i<k; i++)
        {
            a[i]=Lucas(n,m,pri[i]);
        }
        long long ans = chinese_reminder(a,pri,k);
        printf("%I64d\n",ans);
    }
    return 0;
}

时间: 2024-10-15 07:02:16

hdu 5446 Unknown Treasure Lucas定理+中国剩余定理的相关文章

hdu 5446 Unknown Treasure lucas和CRT

Unknown Treasure Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5446 Description On the way to the next secret treasure hiding place, the mathematician discovered a cave unknown to the map. The mathematician ent

hdu 5446 Unknown Treasure (Lucas定理+中国剩余定理+快速乘)

题意:c( n, m)%M    M = P1 * P2 * ......* Pk (其中Pk是素数) 思路:Lucas定理中C(n,m)%M,M必须是素数,当M不是素数时,我们可以把它拆成素数的乘积 如果x=C(n,m)%M ,M=p1*p2*..*pk;  a[i]=Lucas(n,m)%pi: xΞa[1](mod p1) xΞa[2](mod p2) ... xΞa[k](mod pk) 用中国剩余定理就可以把x求出来 注意到这道题ll*ll 由于计算机底层设计的原因,做加法往往比乘法快

HDU 5446 Unknown Treasure(lucas + 中国剩余定理 + 模拟乘法)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5446 题目大意:求C(n, m) % M, 其中M为不同素数的乘积,即M=p1*p2*...*pk, 1≤k≤10.1≤m≤n≤10^18. 分析: 如果M是素数,则可以直接用lucas定理来做,但是M不是素数,而是素数的连乘积.令C(n, m)为 X ,则可以利用lucas定理分别计算出 X%p1,X%p2, ... , X % pk的值,然后用中国剩余定理来组合得到所求结果. 比较坑的地方是,

Hdu 5446 Unknown Treasure(Lucas+中国剩余定理)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=5446 思路:Lucas求出所有a[i]=C(n,m)%m[i],中国剩余定理求出最终结果x (LL*LL会爆掉,手写乘法). 中国剩余定理: 设m1,m2,....mn是两两互质的正整数,对任意给定的整数a1,a2,....an必存在整数,满足 x≡a1 (mod m1),x≡a2 (mod m2),x≡a3 (mod m3)...... 并且满足上列方程组的解x(mod m1m2m3.....mn

HDU 6446 Unknown Treasure Lucas+中国剩余定理+按位乘

HDU 6446 Unknown Treasure 题意:求C(n, m) %(p[1] * p[2] ··· p[k])     0< n,m < 1018 思路:这题基本上算是模版题了,Lucas定理求C(n,m),再用中国剩余定理合并模方程,因为LL相乘会越界,所以用到按位乘. 1 #include <iostream> 2 #include <cstdio> 3 #include <fstream> 4 #include <algorithm&

【bzoj3782】上学路线 dp+容斥原理+Lucas定理+中国剩余定理

题目描述 小C所在的城市的道路构成了一个方形网格,它的西南角为(0,0),东北角为(N,M).小C家住在西南角,学校在东北角.现在有T个路口进行施工,小C不能通过这些路口.小C喜欢走最短的路径到达目的地,因此他每天上学时都只会向东或北行走:而小C又喜欢走不同的路径,因此他问你按照他走最短路径的规则,他可以选择的不同的上学路线有多少条.由于答案可能很大,所以小C只需要让你求出路径数mod P的值. 输入 第一行,四个整数N.M.T.P. 接下来的T行,每行两个整数,表示施工的路口的坐标. 输出 一

hdu 5446 Unknown Treasure 卢卡斯+中国剩余定理

Unknown Treasure Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description On the way to the next secret treasure hiding place, the mathematician discovered a cave unknown to the map. The mathematician

HDU 5446 Unknown Treasure

Unknown Treasure Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 721    Accepted Submission(s): 251 Problem Description On the way to the next secret treasure hiding place, the mathematician d

HDU 1788 Chinese remainder theorem again 中国剩余定理

题意: 给定n,AA 下面n个数m1,m2···mn 则有n条方程 res % m1 = m1-AA res % m2 = m2-AA 问res的最小值 直接上剩余定理,嘿嘿 #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include<math.h> #include<set> #include<queue> #i