1. 题目描述
题目很简单,就是求$C(n,m) % M$。

2. 基本思路
这是一道应用了众多初等数论定理的题目,因为数据范围较大因此使用Lucas求$C(n,m) % P$。
而M较大,因此通过$a[i] = C(n,m)%P_i$再综合中国剩余定理可解。由于数据可能为$10^{18}$,再进行乘法可能超long long。
因此,还需要模拟乘法。

3. 代码

 /* 5446 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <bitset>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 typedef long long LL;
const int maxn = 1e5+;
LL fact[maxn];
LL P[], a[];
LL n, m;
int k; void init_fact(LL mod) {
fact[] = ;
rep(i, , maxn) fact[i] = fact[i-] * i % mod;
} LL Pow(LL base, LL n, LL mod) {
LL ret = ; while (n) {
if (n & )
ret = ret * base % mod;
base = base * base % mod;
n >>= ;
} return ret;
} inline LL Inv(LL a, LL mod) {
return Pow(a, mod-, mod);
} LL C(LL n, LL m, LL mod) {
if (n < m) return ;
#ifndef ONLINE_JUDGE
assert(n >= m);
#endif
return fact[n] * Inv(fact[n-m]*fact[m]%mod, mod) % mod;
} LL Lucas(LL n, LL m, LL mod) {
if (m == ) return ;
return C(n%mod, m%mod, mod) * Lucas(n/mod, m/mod, mod) % mod;
} void egcd(LL a, LL b, LL& d, LL& x, LL& y) {
if (!b) {
d = a;
x = ;
y = ;
} else {
egcd(b, a%b, d, y, x);
y -= a/b * x;
}
} LL Mul(LL base, LL n, LL mod) {
LL ret = ; while (n) {
if (n & )
ret = (ret + base) % mod;
base = (base + base) % mod;
n >>= ;
} return ret;
} LL china(int n, LL *a, LL *m) {
LL M = , w, d, x = , y;
LL tmp;
bool sign; rep(i, , n) M *= m[i];
rep(i, , n) {
w = M/m[i];
egcd(m[i], w, d, d, y);
sign = y < ;
tmp = Mul(w, abs(y), M);
tmp = Mul(tmp, a[i], M);
if (sign) tmp = -tmp;
x = (x + tmp) % M;
} return (x + M) % M;
} void solve() {
rep(i, , k) {
init_fact(P[i]);
a[i] = Lucas(n, m, P[i]);
} LL ans = china(k, a, P);
printf("%I64d\n", ans);
} int main() {
cin.tie();
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int t; scanf("%d", &t);
while (t--) {
scanf("%I64d%I64d%d", &n,&m,&k);
rep(i, , k)
scanf("%I64d", &P[i]);
solve();
} #ifndef ONLINE_JUDGE
printf("time = %ldms.\n", clock());
#endif return ;
}

【HDOJ】5446 Unknown Treasure的更多相关文章

  1. 【HDOJ】1448 The Treasure

    这就是个简单的bfs.真没什么好说的,三维的状态就可以了.每次预处理一下monster的位置,然后再恢复. /* 1924 */ #include <iostream> #include ...

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

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

  3. 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.. ...

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

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

  5. hdu 5446 Unknown Treasure lucas和CRT

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

  6. HDU 5446 Unknown Treasure(Lucas定理+CRT)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5446 [题目大意] 给出一个合数M的每一个质因子,同时给出n,m,求C(n,m)%M. [题解] ...

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

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

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

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

  9. HDU 5446 Unknown Treasure

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

随机推荐

  1. EditPlus配置[C++] [Python] [Java] 编译运行环境

    以前一直用Codeblocks写C++,eclipse写Java,再在eclipse里面集成PyDev写Python,首先无法忍受代码自动补全功能(这个功能也许你万分喜欢),也无法忍受如此重量级的ID ...

  2. 聊一聊js中的null、undefined与NaN

    零.寒暄 翻翻自己的博客,上一篇竟然是六月26号的,说好的更新呢?回顾刚刚过去的这个七月,整天都是公司的入职培训加上自己的小论文,每天奋战到凌晨1点多,这是要挂的节奏啊!但是不论怎么说,自己的时间管理 ...

  3. linux 配置ssh免密码登录

    1.确保主机名唯一 主机名修改方法: a.修改/etc/sysconfig/network,HOSTNAME=想要设置的主机名称 b.修改/etc/hosts,127.0.0.1   localhos ...

  4. 使用命令行进行 VS单元测试 MSTest

    测试 指定的方法 "D:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\MSTest.exe" /test ...

  5. 《深入浅出JavaScript》

    第一章JS入门 第二章数据和判定常用的转义序列\b 回退 \f换页 \n换行 \r回车 \t制表符 \'单引 \"双引 \\反斜乘除求余的优先级相同,从左向右执行string对象indexO ...

  6. Ubuntu下配置Docbook环境

    1.准备环境 $sudo apt-get install xsltproc $sudo apt-get install docbook-xsl $sudo apt-get install docboo ...

  7. DB2 DATE类型在显示的时候,带有00:00:00,去掉的方法,使用VARCHAR()函数

    DROP VIEW DMS.V_AQ_INSURANCECLAIMS; CREATE VIEW DMS.V_AQ_INSURANCECLAIMS AS SELECT * FROM (SELECT T1 ...

  8. volatile 关键字的复习

    今天早上看何登成的微博(http://hedengcheng.com/?p=725) 对volatile 关键字语意进行了深入分析. 看完之后,用自己的话总结如下: 1.c/c++ volatile中 ...

  9. http://jingyan.baidu.com/article/d169e186b38c37436611d8fa.html

    http://jingyan.baidu.com/article/d169e186b38c37436611d8fa.html

  10. weka平台

    weka平台界面简介 纵向排列的四个主要功能 1.探索(写自己的代码) 2.实验(比较算法) 3.可视化 4.命令行 1.探索 先将weka-src.jar文件解压到一个文件夹 将文件夹导入到Elip ...