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/ ...
随机推荐
- 查看windows下port占用
有时假tomcat启动时提示port被占用.能够用netstat -aon|findstr "8080" 命令查找到,然后 用 taskkill /f /pid pid号 强制ki ...
- LeetCode(28)题解:Implement strStr()
https://leetcode.com/problems/implement-strstr/ 题目: Implement strStr(). Returns the index of the fir ...
- Finally语句块的运行
一.finally语句块是否一定运行? Java中异常捕获机制try...catch...finally块中的finally语句是不是一定会被运行?非常多人都说不是.当然他们的回答是正确的,经过试验. ...
- mysql 免安装配置问题
摘要: MySQL是一个小型关系型数据库管理系统,MySQL被广泛地应用在Internet上的中小型网站中.由于其体积小.速度快.总体拥有成本低,尤其是开放源码这一特点,许多中小型网站为了降低网站总体 ...
- elasticsearch索引查询,日志搜素
索引查询 http://10.199.137.115:9200/_cat/indices?format=json 返回json字符串的索引状态 增加索引名称过滤 http://10.199.137.1 ...
- POJ1797 Heavy Transportation —— 最短路变形
题目链接:http://poj.org/problem?id=1797 Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K T ...
- hdu 1514 Free Candies 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1514 题目意思:有4堆糖果,每堆糖果有n个,从上到下排好,取糖果只能从上往下取,取完的糖果放在篮子里, ...
- skynet源码阅读<2>--网络部分
先来看下socket_server的数据结构,这里简称为ss: struct socket_server { int recvctrl_fd; int sendctrl_fd; int checkct ...
- Linux查看当前在线用户信息
Linux是多用户系统,支持同时登陆多个用户,在终端中用"w"命令可以查看当前的在线用户,以及每个用户正在执行的进程: 第一行显示的字段信息分别是: 12:16:49:系统当前时间 ...
- Spring配置错误 No adapter for IAdvice of type
参考:http://www.2cto.com/kf/201305/211728.html 错误十三 在配置拦截器后,运行的时候报错=> Error creating context 'sprin ...