Unknown Treasure

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

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
题意:就是让你求组合数C(n,m)的值模M=p1*p2*...pk的值这写p;
思路:中国剩余定理+lucas定理;
因为组合数比较大,模数乘起来也很大,所以我们先用lucas定理求出对每个模数所求得的模,然后再通过中国剩余定理求对那个大模数的模;
在使用中国剩余定理的时候,最后那个M可能会很大,所以乘法的时候可能会爆LL,要用快速乘去处理
  1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<stdlib.h>
6 #include<queue>
7 #include<map>
8 #include<math.h>
9 using namespace std;
10 typedef long long LL;
11 int mod[20];
12 LL a[100005];
13 LL yu[30];
14 LL quick(LL n,LL m,LL p)
15 {
16 LL ans=1;
17 while(m)
18 {
19 if(m&1)
20 {
21 ans=ans*n%p;
22 }
23 n=n*n%p;
24 m/=2;
25 }
26 return ans;
27 }
28 LL lucas(LL n,LL m,LL p)
29 {
30 if(n==0)
31 {
32 return 1;
33 }
34 else
35 {
36 LL nn=n%p;
37 LL mm=m%p;
38 if(mm<nn)
39 return 0;
40 else
41 {
42 LL ni=a[mm-nn]*a[nn]%p;
43 ni=a[mm]*quick(ni,p-2,p)%p;
44 return ni*lucas(n/p,m/p,p);
45 }
46 }
47 }
48 LL mul(LL n, LL m,LL p)
49 {
50 n%=p;
51 m%=p;
52 LL ret=0;
53 while(m)
54 {
55 if(m&1)
56 {
57 ret=ret+n;
58 ret%=p;
59 }
60 m>>=1;
61 n<<=1;
62 n%=p;
63 }
64 return ret;
65 }
66 int main(void)
67 {
68 LL n,m;
69 int k;
70 int t;
71 scanf("%d",&k);
72 int i,j;
73 while(k--)
74 {
75 scanf("%lld %lld %d",&n,&m,&t);
76 for(i=0; i<t; i++)
77 {
78 scanf("%d",&mod[i]);
79 a[0]=1;
80 a[1]=1;
81 for(j=2; j<mod[i]; j++)
82 {
83 a[j]=a[j-1]*j%mod[i];
84 }
85 yu[i]=lucas(m,n,mod[i]);
86 }
87 LL sum=1;
88 for(i=0; i<t; i++)
89 {
90 sum*=(LL)mod[i];
91 }
92 LL acc=0;
93 for(i=0; i<t; i++)
94 {
95 LL kk=sum/mod[i];
96 LL ni=quick(kk%mod[i],mod[i]-2,mod[i]);
97 acc=(acc+mul(yu[i],mul(kk,ni,sum),sum)%sum)%sum;
98
99 }
100 acc=acc%sum+sum;
101 acc%=sum;
102 printf("%lld\n",acc);
103 }
104 return 0;
105 }

Unknown Treasure(hdu5446)的更多相关文章

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

    Unknown Treasure Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

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

    Unknown Treasure Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

  3. HDU 5446 Unknown Treasure Lucas+中国剩余定理

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5446 Unknown Treasure 问题描述 On the way to the next se ...

  4. hdu 5446 Unknown Treasure lucas和CRT

    Unknown Treasure Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...

  5. Unknown Treasure (卢卡斯 + 孙子定理, 模板题)

    Unknown Treasure 参考链接 : https://www.cnblogs.com/linyujun/p/5199684.html 卢卡斯定理 : C(n, m) % p  =  C(n ...

  6. Hdu 5446 Unknown Treasure (2015 ACM/ICPC Asia Regional Changchun Online Lucas定理 + 中国剩余定理)

    题目链接: Hdu 5446 Unknown Treasure 题目描述: 就是有n个苹果,要选出来m个,问有多少种选法?还有k个素数,p1,p2,p3,...pk,结果对lcm(p1,p2,p3.. ...

  7. HDU 5446 Unknown Treasure

    Unknown Treasure Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

  8. HDU 5446 Unknown Treasure Lucas+中国剩余定理+按位乘

    HDU 5446 Unknown Treasure 题意:求C(n, m) %(p[1] * p[2] ··· p[k])     0< n,m < 1018 思路:这题基本上算是模版题了 ...

  9. HDU5446 Unknown Treasure(组合数膜合数-->Lucas+中国剩余定理)

    >On the way to the next secret treasure hiding place, the mathematician discovered a cave unknown ...

随机推荐

  1. 【玩具】获取B站视频的音频片段

    事情是这样的,我有个和社畜的社会地位不太相符的小爱好--听音乐剧. 基本上是在B站上点开视频听,不是不想在网易云或者QQ音乐听,只是在这些音乐软件上面,我想听的片段要不就收费,要不版本不是我喜欢的,要 ...

  2. RocketMQ这样做,压测后性能提高30%

    从官方这边获悉,RocketMQ在4.9.1版本中对消息发送进行了大量的优化,性能提升十分显著,接下来请跟着我一起来欣赏大神们的杰作. 根据RocketMQ4.9.1的更新日志,我们从中提取到关于消息 ...

  3. 零基础学习java------day16-----文件,递归,IO流(字节流读写数据)

    1.File 1.1 构造方法(只是创建已经存在文件的对象,并不能创建没有的文件) (1)public File(String pathname) (2)public File(String pare ...

  4. 使用WtmPlus低代码平台提高生产力

    低代码平台的概念很火爆,产品也是鱼龙混杂. 对于开发人员来说,在使用绝大部分低代码平台的时候都会遇到一个致命的问题:我在上面做的项目无法得到源码,完全黑盒.一旦我的需求平台满足不了,那就是无解.   ...

  5. KMP算法中的next函数

    原文链接:http://blog.csdn.net/joylnwang/article/details/6778316/ 其实后面大段的代码都可以不看 KMP的关键是next的产生 这里使用了中间变量 ...

  6. jenkins之代码回滚

    #:通过传参数方式 #:保存后就会看到这样 #;:我们在jenkins服务器写一个脚本 root@ubuntu:~# mkdir /root/script/web1 -pv mkdir: create ...

  7. OC简单介绍

    一.OC与C的对比 关键字 OC新增的关键字在使用时,注意部分关键字以"@"开头 方法->函数 定义与实现 数据类型 新增:BOOL/NSObject/id/SEL/bloc ...

  8. easyhadoop 安装

    ldconfig deferred processing now taking place正在处理用于 libapache2-mod-php5 的触发器... * Reloading web serv ...

  9. 用oracle中的Row_Number实现分页

    Row_Number实现分页   1:首先是 select ROW_NUMBER() over(order by id asc) as 'rowNumber', * from table1 生成带序号 ...

  10. CentOs 7 yum 安装Nginx

    打开官网下载文档:http://nginx.org/en/download.html 2进入操作系统 centOs 7,建立文件夹 nginx ,进入nginx ,拷贝 上图1编辑命令:/etc/yu ...