【HDOJ】5446 Unknown Treasure
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的更多相关文章
- 【HDOJ】1448 The Treasure
这就是个简单的bfs.真没什么好说的,三维的状态就可以了.每次预处理一下monster的位置,然后再恢复. /* 1924 */ #include <iostream> #include ...
- 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 (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 Lucas+中国剩余定理+按位乘
HDU 5446 Unknown Treasure 题意:求C(n, m) %(p[1] * p[2] ··· p[k]) 0< n,m < 1018 思路:这题基本上算是模版题了 ...
- hdu 5446 Unknown Treasure lucas和CRT
Unknown Treasure Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...
- HDU 5446 Unknown Treasure(Lucas定理+CRT)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5446 [题目大意] 给出一个合数M的每一个质因子,同时给出n,m,求C(n,m)%M. [题解] ...
- 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
Unknown Treasure Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
随机推荐
- hdu 1392 Surround the Trees
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1392 题意:给出一些点的坐标,求最小的凸多边形把所有点包围时此多边形的周长. 解法:凸包ConvexH ...
- 聊一聊js中的null、undefined与NaN
零.寒暄 翻翻自己的博客,上一篇竟然是六月26号的,说好的更新呢?回顾刚刚过去的这个七月,整天都是公司的入职培训加上自己的小论文,每天奋战到凌晨1点多,这是要挂的节奏啊!但是不论怎么说,自己的时间管理 ...
- CSS3弹性盒模型,Flex布局教程
布局的传统解决方案,基于盒状模型,依赖 display属性 + position属性 + float属性.它对于那些特殊布局非常不方便,比如,垂直居中就不容易实现. CSS3中引入flex的弹性盒模型 ...
- ibatis的iterate使用
Iterate:这属性遍历整个集合,并为 List 集合中的元素重复元素体的内容. Iterate 的属性: prepend - 可被覆盖的 SQL 语句组成部分,添加在语句的前面(可选 ...
- 高德地图根据经纬度转换成地址JS代码demo
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- MySQL: InnoDB 还是 MyISAM? (转载)
MyISAM存储引擎 原文作者:http://www.cnblogs.com/villion/archive/2009/07/09/1893762.html MyISAM是 默认存储引擎.它基于更老的 ...
- Android调用天气预报的WebService简单例子
下面例子改自网上例子:http://express.ruanko.com/ruanko-express_34/technologyexchange5.html 不过网上这个例子有些没有说明,有些情况不 ...
- hdu 4068 SanguoSHA
搜索下就可以了…… 代码如下: #include<iostream> #include<cstring> #include<cstdio> #include< ...
- Tomcat SSL 设置
1. 先用如下命令生成tomcat 证书 cls rem please set the env JAVA_HOME before run this bat file SET JAVA_HOME=C:\ ...
- C#中out的用法
out的用法 out 关键字会导致参数通过引用来传递.这与 ref 关键字类似,不同之处在于 ref 要求变量必须在传递之前进行初始化.若要使用 out 参数,方法定义和调用方法都必须显式使用 out ...