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. Matlab求范数

    对 p = 2,这称为弗罗贝尼乌斯范数(Frobenius norm)或希尔伯特-施密特范数( Hilbert–Schmidt norm),不过后面这个术语通常只用于希尔伯特空间.这个范数可用不同的方 ...

  2. vector内存分配

    vector,map 这些容器还是在堆上分配的内存,在析构时是释放空间 vector在提高性能可以先reserve在push_back() reserve:决定capacity,但没有真正的分配内存, ...

  3. 学习NAnt Build .CS+Solution+MSBuild+SVN+NUnit+NUnitReport

    NAnt help:http://nant.sourceforge.net/release/latest/help/tasks/NAntContrib help:http://nantcontrib. ...

  4. [geeksforgeeks] Convert a given Binary Tree to Doubly Linked List

    http://www.geeksforgeeks.org/in-place-convert-a-given-binary-tree-to-doubly-linked-list/ Given a Bin ...

  5. https+ssl详解

    这是转载别人的写的很好,(转:崔永秀) 把这几天学习到的关于ssl和https协议的内容在这里分享一下,适合一些像我一样的网络协议初学者. ssl协议的起源和历史我就不再多说了,就是那个Netscap ...

  6. iOS常见各种ID

    //CFUUID CFUUIDRef cfuuid = CFUUIDCreate(kCFAllocatorDefault); NSString *cfuuidString = (NSString*)C ...

  7. iOS数组和字符串的转化

    NSMutableArray *components = [messageStr componentsSeparatedByString:@"*"] ; 反过来为 NSStrig ...

  8. POJ 1459

    #include<iostream> #define MAXN 105 #include"queue" #define big_num 100000000 using ...

  9. 屏蔽wordpress升级提示

    根据自己的需要,挑选适合的代码放在主题的functions.php文件中就可以了 /* 去除 WordPress 後台升級提示 */ // 2.8 ~ 2.9: add_filter('pre_tra ...

  10. 安装mysql之后,存入中文出现乱码

    如图显示:安装mysql之后,存入中文出现乱码 解决方案: 找到如图的文件位置 打开进行如图的修改: 结果: