题目链接:

  Hdu 5446 Unknown Treasure

题目描述:

  就是有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定理 + 中国剩余定理)的更多相关文章

  1. Hdu 5442 Favorite Donut (2015 ACM/ICPC Asia Regional Changchun Online 最大最小表示法 + KMP)

    题目链接: Hdu 5442 Favorite Donut 题目描述: 给出一个文本串,找出顺时针或者逆时针循环旋转后,字典序最大的那个字符串,字典序最大的字符串如果有多个,就输出下标最小的那个,如果 ...

  2. (并查集)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 ...

  3. (二叉树)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)  ...

  4. 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 ...

  5. 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 ...

  6. 2015 ACM/ICPC Asia Regional Changchun Online

    1001 Alisha’s Party 比赛的时候学长stl吃T.手写堆过. 赛后我贴了那两份代码都过.相差.2s. 于是用stl写水果. # include <iostream> # i ...

  7. 【二分】【最长上升子序列】HDU 5489 Removed Interval (2015 ACM/ICPC Asia Regional Hefei Online)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5489 题目大意: 一个N(N<=100000)个数的序列,要从中去掉相邻的L个数(去掉整个区间 ...

  8. 【贪心】【模拟】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个个数在 ...

  9. (线段树 区间查询)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/ ...

随机推荐

  1. Linux安装配置Redis CentOS 7 下安装Redis

    Redis是一个高性能的,开源key-value型数据库.是构建高性能,可扩展的Web应用的完美解决方案,可以内存存储亦可持久化存储.因为要使用跨进程,跨服务级别的数据缓存,在对比多个方案后,决定使用 ...

  2. 0 lrwxrwxrwx. 1 root root 13 Nov 20 12:44 scala -> scala-2.12.4

    符号链接的文件属性相同,真正的权限属性由符号链接所指向的实际文件决定.

  3. __sizeof__()

    https://bugs.python.org/issue2898 https://bugs.python.org/file10353/footprint.patch Index: Python/sy ...

  4. 使用TASM编译COFF格式和连接

    看到网络上流传的一份Drocon的mercury的代码程序源码使用TASM32编译使用MASM32来连接...关键的地方就在这里为什么要使用TASM编译...正常情况下TASM连接出来的程序代码体积远 ...

  5. TGraphiControl响应WM_MOUSEMOVE的过程(以TPaintBox为例)good

    起因:非Windows句柄控件也可以处理鼠标消息,我想知道是怎么处理的:并且想知道处理消息的顺序(比如TPaintBox和TForm都响应WM_Mouse消息该怎么办)界面:把TPaintBox放到T ...

  6. (C)inline关键字

      背景(C&C++中) 一.inline关键字用来定义一个类的内联函数,引入它的主要原因是用它替代C中表达式形式的宏定义. 表达式形式的宏定义一例:#define ExpressionNam ...

  7. vue2-editor富文本基础使用方法

    vue2-editor的入门使用准备工作: 使用 npm install vue2-editor --save 安装到项目中去: 使用 在需要的单文件内引入 import { VueEditor } ...

  8. css3的渐变效果

    1.css3 渐变的属性 例子: #grad { background: -webkit-linear-gradient(red, blue); /* Safari 5.1 - 6.0 */ back ...

  9. php基础篇:echo 与 print 的区别

    在实际使用中, print 和 echo 两者的功能几乎是完全一样.可以这么说,凡是有一个可以使用的地方,另一个也可以使用.但是,两者之间也还是一个非常重要的区别:在 echo 函数中,可以同时输出多 ...

  10. HDU4763 Theme Section —— KMP next数组

    题目链接:https://vjudge.net/problem/HDU-4763 Theme Section Time Limit: 2000/1000 MS (Java/Others)    Mem ...