Hdu 5446 Unknown Treasure (2015 ACM/ICPC Asia Regional Changchun Online Lucas定理 + 中国剩余定理)
题目链接:
题目描述:
就是有n个苹果,要选出来m个,问有多少种选法?还有k个素数,p1,p2,p3,...pk,结果对lcm(p1,p2,p3.....,pk)取余。
解题思路:
Lucas + 中国剩余定理,注意的是中国剩余定理的时候有可能会爆long long。然后用一个快速加法就好辣。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef __int64 LL;
const int maxn = ; LL quick_mul (LL a, LL b, LL mod)
{
LL res = ;
while (b)
{
if (b % )
res = (res * a) % mod;
a = (a * a) % mod;
b /= ;
}
return res;
} LL quick_add (LL a, LL b, LL mod)
{
LL res = ;
while (b)
{
if (b % )
res =(res + a) % mod;
a = (a + a) % mod;
b /= ;
}
return res;
} LL C (LL n, LL m, LL mod)
{
if (n < m)
return ;
LL ans = ;
for (int i=; i<=m; i++)
{
LL a = (n - m + i) % mod;
LL b = i % mod;
ans = ans * (a * quick_mul(b, mod - , mod) % mod) % mod;
}
return ans;
} LL Extended_Euclid (LL a, LL b, LL &x, LL &y)
{//a, b只能是正数
if (b == )
{
x = ;
y = ;
return a;
} LL r = Extended_Euclid (b, a%b, x, y), t;
t = x;
x = y;
y = t - a / b * y;
return r;
} LL CRT (LL a[], LL b[], LL n)
{
LL M = , ans = ;
LL Mi, x, y; for (int i=; i<n; i++)
M *= a[i]; for (int i=; i<n; i++)
{
Mi = M / a[i];
LL d = Extended_Euclid (Mi, a[i], x, y);
x = (x % a[i] + a[i]) % a[i];
//注意,这里的x有可能是负数要注意取mod,变成正的 //或者quick_add 的第二个参数传Mi
LL res = quick_add (x, Mi, M);
res = quick_add (res, b[i], M);
ans = (ans + res) % M;
}
return (ans + M) % M;
}
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);
} int main ()
{
LL t, n, m, k, a[maxn], b[maxn];
scanf ("%I64d", &t);
while (t --)
{
scanf ("%I64d %I64d %I64d", &n, &m, &k);
for (int i=; i<k; i++)
{
scanf ("%I64d", &a[i]);
b[i] = Lucas (n, m, a[i]);
}
printf ("%I64d\n", CRT (a, b, k));
}
return ;
}
Hdu 5446 Unknown Treasure (2015 ACM/ICPC Asia Regional Changchun Online Lucas定理 + 中国剩余定理)的更多相关文章
- Hdu 5442 Favorite Donut (2015 ACM/ICPC Asia Regional Changchun Online 最大最小表示法 + KMP)
题目链接: Hdu 5442 Favorite Donut 题目描述: 给出一个文本串,找出顺时针或者逆时针循环旋转后,字典序最大的那个字符串,字典序最大的字符串如果有多个,就输出下标最小的那个,如果 ...
- (并查集)Travel -- hdu -- 5441(2015 ACM/ICPC Asia Regional Changchun Online )
http://acm.hdu.edu.cn/showproblem.php?pid=5441 Travel Time Limit: 1500/1000 MS (Java/Others) Memo ...
- (二叉树)Elven Postman -- HDU -- 54444(2015 ACM/ICPC Asia Regional Changchun Online)
http://acm.hdu.edu.cn/showproblem.php?pid=5444 Elven Postman Time Limit: 1500/1000 MS (Java/Others) ...
- 2015 ACM/ICPC Asia Regional Changchun Online HDU 5444 Elven Postman【二叉排序树的建树和遍历查找】
Elven Postman Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- hdu 5444 Elven Postman(二叉树)——2015 ACM/ICPC Asia Regional Changchun Online
Problem Description Elves are very peculiar creatures. As we all know, they can live for a very long ...
- 2015 ACM/ICPC Asia Regional Changchun Online
1001 Alisha’s Party 比赛的时候学长stl吃T.手写堆过. 赛后我贴了那两份代码都过.相差.2s. 于是用stl写水果. # include <iostream> # i ...
- 【二分】【最长上升子序列】HDU 5489 Removed Interval (2015 ACM/ICPC Asia Regional Hefei Online)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5489 题目大意: 一个N(N<=100000)个数的序列,要从中去掉相邻的L个数(去掉整个区间 ...
- 【贪心】【模拟】HDU 5491 The Next (2015 ACM/ICPC Asia Regional Hefei Online)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5491 题目大意: 一个数D(0<=D<231),求比D大的第一个满足:二进制下1个个数在 ...
- (线段树 区间查询)The Water Problem -- hdu -- 5443 (2015 ACM/ICPC Asia Regional Changchun Online)
链接: http://acm.hdu.edu.cn/showproblem.php?pid=5443 The Water Problem Time Limit: 1500/1000 MS (Java/ ...
随机推荐
- centos下部署项目问题
最近写了商品管理的后台完成了一部分,用node+koa+mongodb搭建,现在想部署到自己的服务器上,部署的过程中遇到了一些坑.首先就是各种环境的搭建,搭建好了之后要把后台的代码传到服务器上运行,运 ...
- LeetCode(26)题解:Remove Duplicates from Sorted Array
https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the ...
- JAVA学习第十九课(java程序的异常处理 (二))
异常处理的捕捉形式: 这是能够对异常进行针对性处理的方式 六.try.catch的理解 详细格式: try { //须要被检測异常的代码 } catch(异常类 变量)//改变量用于接受发生异常的对象 ...
- iOS RAC常用方法
一直想写篇关于RAC的文章,一是分享二是做为笔记,由于项目忙先简单的贴一个自己当初学习的时候代码吧 一.RACCommand // RACCommand 的使用: 使用场景,监听按钮点击,网络请求 - ...
- iframe 框架中 父子界面的JS调用
子界面调用父界面 window.parent.hello(); 父界面调用子界面 window.frmaes[i].hello();
- mini_magick
https://github.com/minimagick/minimagick class https://www.rubydoc.info/github/minimagick/minimagic ...
- ios实现倒计时的两种方法
方法1:使用NSTimer来实现 主要使用的是NSTimer的scheduledTimerWithTimeInterval方法来每1秒执行一次timeFireMethod函数,timeFireMeth ...
- 解决 git branch -a 无法全部显示远程的分支,只显示master分支
新建分支 若遇到 git branch -a 无法全部显示远程的分支,只显示master分支 可以通过 git fetch 将本地远程跟踪分支进行更新,与远程分支保持一致
- CentOS/Ubuntu安装GLIBCXX3.4.21
经过测试“GLIBCXX3.4.21 not find”这篇博文解决了我的问题. 以下是安装步骤: 一.首先查看当前gcc版本 strings /usr/lib/x86_64_linux-gun/ ...
- [Java] static, final
1.静态成员 静态成员独立于类的对象,先于对象的存在而存在.无论创建了类的多少个对象,静态成员都只有一个实例空间.一个静态变量被同一个类的所有对象共享.当改变了其中一个对象的静态变量时,其余对象的静态 ...