题意:c( n, m)%M    M = P1 * P2 * ......* Pk

Lucas定理是用来求 c(n,m) mod p,p为素数的值。得出一个存余数数组,在结合中国剩余定理求值

其中有个地方乘积可能超范围,所以按位乘(数论方面薄弱啊,学习学习)。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll; ll p[15],an[15];
ll fac[100005],inv[100005]; ll pow_mod(ll a, int n, int mod)
{
ll ret = 1;
while (n)
{
if (n&1) ret = ret * a % mod;
a = a * a % mod;
n >>= 1;
}
return ret;
} void ini(int x)
{
fac[0] = 1;
for(int i = 1; i < x; i++) fac[i] = fac[i-1]*i%x;
inv[x - 1] = pow_mod(fac[x-1],x-2,x);
for(int i = x - 2; i >= 0; i--) inv[i] = inv[i+1] * (i+1) % x;
} ll c(ll a,ll b,ll p)
{
if(a < b || a < 0 || b < 0)
return 0;
return fac[a]*inv[b]%p*inv[a-b]%p;
} ll lucas(ll a,ll b, int p)
{
if( b == 0)
return 1;
return lucas(a/p,b/p,p)*c(a%p,b%p,p)%p;
} ll ex_gcd(ll a, ll b, ll& x, ll& y)
{
if (b == 0)
{
x = 1;
y = 0;
return a;
}
ll d = ex_gcd(b, a % b, y, x);
y -= x * (a / b);
return d;
} ll mul(ll a, ll b, ll mod)
{
a = (a % mod + mod) % mod;
b = (b % mod + mod) % mod; ll ret = 0;
while(b)
{
if(b&1)
{
ret += a;
if(ret >= mod) ret -= mod;
}
b >>= 1;
a <<= 1;
if(a >= mod) a -= mod;
}
return ret;
} ll china(ll n,ll* a,ll* b)
{
ll M = 1,d,y,x= 0;
for(int i = 0; i < n; i++)
{
M *= b[i];
}
for(int i = 0; i < n; i++)
{
ll w = M/b[i];
ex_gcd(b[i],w,d,y);
x = (x + mul(mul(y, w, M), a[i], M));//可能超范围
}
return (x+M) % M;
} int main()
{
int T,k;
ll n,m;
scanf("%d",&T);
while(T--)
{
scanf("%I64d%I64d",&n,&m);
scanf("%d",&k);
for(int i = 0; i < k; i++)
{
scanf("%I64d",&p[i]);
ini(p[i]);
an[i] = lucas(n,m,p[i]);
}
printf("%I64d\n",china(k,an,p));
}
return 0;
}

  

hdu 5446(中国剩余+lucas+按位乘)的更多相关文章

  1. HDU 5446 Unknown Treasure Lucas+中国剩余定理+按位乘

    HDU 5446 Unknown Treasure 题意:求C(n, m) %(p[1] * p[2] ··· p[k])     0< n,m < 1018 思路:这题基本上算是模版题了 ...

  2. HDU 5446 Unknown Treasure Lucas+中国剩余定理

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5446 Unknown Treasure 问题描述 On the way to the next se ...

  3. hdu 5446 Unknown Treasure Lucas定理+中国剩余定理

    Unknown Treasure Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

  4. HDU 5446 Unknown Treasure(lucas + 中国剩余定理 + 模拟乘法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5446 题目大意:求C(n, m) % M, 其中M为不同素数的乘积,即M=p1*p2*...*pk, ...

  5. hdu 5446 Unknown Treasure lucas和CRT

    Unknown Treasure Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...

  6. HDU 5446 中国剩余定理+lucas

    Unknown Treasure Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

  7. hdu 2891 中国剩余定理

    从6点看到10点,硬是没算出来,早知道玩游戏去了,艹,明天继续看 不爽,起来再看,终于算是弄懂了,以后超过一个小时的题不会再看了,不是题目看不懂,是水平不够 #include<cstdio> ...

  8. HDU 5768 中国剩余定理

    题目链接:Lucky7 题意:求在l和r范围内,满足能被7整除,而且不满足任意一组,x mod p[i] = a[i]的数的个数. 思路:容斥定理+中国剩余定理+快速乘法. (奇+ 偶-) #incl ...

  9. hdu 5446 Unknown Treasure 卢卡斯+中国剩余定理

    Unknown Treasure Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

随机推荐

  1. selenium webdriver API

    元素定位 #coding=utf-8 from selenium import webdriver from selenium.webdriver.firefox.firefox_binary imp ...

  2. tableView//collectionView加载时的动画

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:( ...

  3. Packet for query is too large (84 > -1).

    windows下的resin配置连接mysql,常用的安全的做法是将数据库信息配置到conf目录下的resin.xml文件中. 因为resin连接mysql不是必须的,所以resin本身没有提供mys ...

  4. 22.C++- 继承与组合,protected访问级别

    在C++里,通过继承和组合实现了代码复用,使得开发效率提高,并且能够通过代码看到事物的关系 组合比继承简单,所以在写代码时先考虑能否组合,再来考虑继承. 组合的特点 将其它类的对象作为当前类的成员使用 ...

  5. css中的position

    一.position语法与结构 position语法: position : static absolute relative position参数:static : 无特殊定位,对象遵循HTML定位 ...

  6. Spring源码阅读-spring启动

    web.xml web.xml中的spring容器配置 <listener> <listener-class>org.springframework.web.context.C ...

  7. 【52ABP实战教程】0.3-- 从github推送代码回vsts实现双向同步

    需求 在之前的文章中"[DevOps]如何用VSTS持续集成到Github仓库" 我们有讲述如何将vsts中的代码编译推送到github中,这一篇我们来完善,如果有人给你开源项目推 ...

  8. Oracle12c:支持通过创建identity columen来实现创建自增列

    oracle12c之前如果需要创建自增列必须要通过sequence+trigger来实现.但是oracle12c已经可以像mysql,sqlserver一样通过identity column来设置自增 ...

  9. innerHTML与innerText的区别

    innerHTML获取元素的HTML内容,和设计元素的HTML内容(HTML标签会被解析)例如:ele.innerHTML="<strong>我会被解释加粗</strong ...

  10. SQL语句 (一)

    1 SQL语句分类: 数据查询语句(DQL): SELECT 数据操纵语言 (DML): INSERT.UPDATE.DELETE 数据定义语言 (DDL): 数据控制语言 (DCL): GRANT. ...