Unknown Treasure(hdu5446)
Unknown Treasure
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2112 Accepted Submission(s): 771
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.
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}.
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)的更多相关文章
- hdu 5446 Unknown Treasure Lucas定理+中国剩余定理
Unknown Treasure Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- hdu 5446 Unknown Treasure 卢卡斯+中国剩余定理
Unknown Treasure Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- HDU 5446 Unknown Treasure Lucas+中国剩余定理
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5446 Unknown Treasure 问题描述 On the way to the next se ...
- hdu 5446 Unknown Treasure lucas和CRT
Unknown Treasure Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...
- Unknown Treasure (卢卡斯 + 孙子定理, 模板题)
Unknown Treasure 参考链接 : https://www.cnblogs.com/linyujun/p/5199684.html 卢卡斯定理 : C(n, m) % p = C(n ...
- 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.. ...
- HDU 5446 Unknown Treasure
Unknown Treasure Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- HDU 5446 Unknown Treasure Lucas+中国剩余定理+按位乘
HDU 5446 Unknown Treasure 题意:求C(n, m) %(p[1] * p[2] ··· p[k]) 0< n,m < 1018 思路:这题基本上算是模版题了 ...
- HDU5446 Unknown Treasure(组合数膜合数-->Lucas+中国剩余定理)
>On the way to the next secret treasure hiding place, the mathematician discovered a cave unknown ...
随机推荐
- Python队列queue模块
Python中queue模块常用来处理队列相关问题 队列常用于生产者消费者模型,主要功能为提高效率和程序解耦 1. queue模块的基本使用和相关说明 # -*- coding:utf-8 -*- # ...
- C/C++运行时确定字节顺序
字节顺序(英文:Endianness),多字节数据在内存中的存储顺序: 1.对于特定数据,内存空间有起始地址.结束地址: 2.对于数据本身,存在高位字节.地位字节:例如 int data = 0x01 ...
- 求解线性递推方程第n项的一般方法
概述 系数为常数,递推项系数均为一次的,形如下面形式的递推式,称为线性递推方程. \[f[n]=\begin{cases} C &n\in Value\\ a_1 f[n-1]+a_2 f[n ...
- springcloud - alibaba - 2 - 集成Feign - 更新完成
1.依赖 依赖管理 <parent> <artifactId>spring-boot-parent</artifactId> <groupId>org. ...
- keeper及er表示被动
一些像employ这样的动词有employer和employee两个名词,而keep的名词只有keeper,keepee不是词.美剧FRIENDS和TBBT里出现了He/she is a keeper ...
- Flink(九)【Flink的重启策略】
目录 1.Flink的重启策略 2.重启策略 2.1未开启checkpoint 2.2开启checkpoint 1)不设置重启策略 2)不重启 3)固定延迟重启(默认) 4)失败率重启 3.重启效果演 ...
- 双向链表——Java实现
双向链表 链表是是一种重要的数据结构,有单链表和双向链表之分:本文我将重点阐述不带头结点的双向链表: 不带头结点的带链表 我将对双链表的增加和删除元素操作进行如下解析 1.增加元素(采用尾插法) (1 ...
- ORACLE 按逗号拆分字符串为多行
with t as (select '1,2,3,10,11,12' a from dual) select substr(a, decode(level - 1, 0, 0, instr(a, ', ...
- awk的基本用法
最近遇到导入的csv文件首行为日期,但需要将日期作为列导入到数据库中,直接使用ctl文件好像无法实现,了解到awk这个强大的命令. 导入的CSV文件除了首行为日期,其他的都是格式相同的.需要将首行单独 ...
- vue 第三方图标库
"font-awesome": "^4.7.0", "dependencies": { "axios": "^ ...