【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 ...
随机推荐
- 2016ACM-ICPC Qingdao Online青岛网络赛题解
TonyFang+Sps+我=5/12 滚了个大粗 2016年9月21日16:42:36 10题完工辣 01 题意:求形同的数中大于n的最小值 题解:预处理所有的(5194个),在这里面二分 #inc ...
- 也可以使用如下命令更改您的默认 Shell
也可以使用如下命令更改您的默认 Shell chsh -s /bin/zsh (需要输入您的密码)
- CSS3:线上编辑工具及实用资料整理
an I Use 个人最常用的,资料比较全,桌面和移动浏览器支持HTML5,CSS3,SVG和兼容性表. 官网地址:http://caniuse.com/ CSS3 Click Chart CSS3 ...
- 如何创建和发布.asmx Web Service
创建和发布Web ServiceWeb服务方法中可以返回一个DataSet对象 WEB服务可以说是下一代WEB应用程序的基础,无论客户端是WINDOWS应用.ASP.NET Web Form程序.甚至 ...
- 【高可用HA】Apache (1) —— Mac下安装Apache Httpd到自定义路径(非/etc/apache2)
Mac下安装Apache Httpd httpd版本: httpd-2.4.17 参考来源: Tomcat Clustering - A Step By Step Guide Apache HTTP ...
- JavaScript文件应该放在网页的什么位置
JavaScript文件应该放在网页的什么位置 http://www.lihuai.net/qianduan/js/352.html 在<良好的JavaScript编程习惯>系列教程的第三 ...
- POJ 2017
#include<iostream> #include<stdio.h> using namespace std; int main() { //freopen("t ...
- js中的call与apply
看js权威指南里面关于call与apply方法的说明:我们可以将call()与apply()看作是某个对象的方法,通过调用方法的形式来间接调用函数.这样的解释未免使人糊涂啊.下面说一下自己的见解:其实 ...
- JavaScript基于对象编程
js面向对象特征介绍 javascript是一种面向(基于)对象的动态脚本语言,是一种基于对象(Object)和事件驱动(EventDirven)并具有安全性能的脚本语言.它具有面向对象语言所特有的各 ...
- C++ std命名空间
1.命名空间是一种特殊的作用域,它包含了处于该作用域中所有标示符.命名空间使用namespace 来声明,并使用{}来界定命名空间的作用域,例如: namespace func{ int val=0; ...