http://acm.hdu.edu.cn/showproblem.php?pid=5446

题意:题目意思很简单,要你求C(n,m)mod p的值 p=p1*p2*...pn;

题解:对于C(n,m)mod p 由于n,m的值很大 我们用lucas定理把n,m的范围缩小。由于模数是由若干个素数的乘积组成,那么对于最终要求的解x,我们可以用中国剩余定理求解。中国剩余定理如下:

设正整数两两互素,则同余方程组

有整数解。并且在模下的解是唯一的,解为

其中,而的逆元。

最后说一点,由于数据的范围还是比较大,在乘法求解的过程中,如果用普通的乘法,是会溢出的,这里还要用到按位乘法(具体看代#include <cstdio>#include <iostream>

#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
ll m[],a[];
ll mul(ll a,ll b,ll p)// 按位乘
{
ll ret=;
while(b)
{
if(b&) ret=(ret+a)%p;
b=b>>;
a=(a+a)%p;
}
return ret; }
ll exgcd(ll a,ll b,ll &x,ll &y)// 扩展欧几里得
{
if(b==)
{
x=;
y=;
return a;
}
ll temp=exgcd(b,a%b,y,x);
y-=(a/b)*x;
return temp;
}
ll finv(ll a,ll m)// 求逆元
{
ll x,y;
ll g=exgcd(a,m,x,y);
x=(x%m+m)%m;//
return x;
}
ll c(ll n,ll m,ll p)
{
if(m > n) return ;
ll a,b;
a=b=;
while(m)
{
a=(a*n)%p;
b=b*m%p;
n--;
m--;
}
return mul(a,finv(b,p),p);
}
ll lucas(ll n,ll m,int p)
{
if(m==) return ;// c(n,0)=1;
return mul(lucas(n/p,m/p,p),c(n%p,m%p,p),p);// lucas把组合数要求解的范围缩小到了p之内
} ll crt(int len)
{
ll sum=;
ll M=;
for(int i=;i<=len;i++) M*=m[i];
for(int i=;i<=len;i++)
{
ll temp=M/m[i];
sum=(sum+mul(mul(a[i],temp,M),finv(temp,m[i]),M))%M;// 这里有一个数据溢出的问题 对于相乘数据会溢出的问题 用转为二进制的按位乘法
}
return sum;
} void init(ll p)
{
fac[]=;
fac[]=;
for(ll i=;i<=p;i++) fac[i]=fac[i]*i%p;
}
int main()
{
cin.sync_with_stdio(false);
int t;
cin>>t;
while(t--)
{
ll n,mm,k;
cin>>n>>mm>>k;
init(k);
for(int i=;i<=k;i++)
{
cin>>m[i];
a[i]=lucas(n,mm,m[i]);
}
cout<<crt(k)<<endl;
}
return ;
}

hdu 5446 lucas+crt+按位乘的更多相关文章

  1. HDU 5446 lucas CRT

    n中选m个模M,M为多个素数之积 $n, m, k (1 \leq m \leq n \leq 10^{18}, 1 \leq k \leq 10)$,$M = p_1 · p_2 · · · p_k ...

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

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

  3. 中国剩余定理&Lucas定理&按位与——hdu 5446

    链接: hdu 5446 http://acm.hdu.edu.cn/showproblem.php?pid=5446 题意: 给你三个数$n, m, k$ 第二行是$k$个数,$p_1,p_2,p_ ...

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

  5. HDU 5446 Unknown Treasure(Lucas定理+CRT)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5446 [题目大意] 给出一个合数M的每一个质因子,同时给出n,m,求C(n,m)%M. [题解] ...

  6. hdu 5446 Unknown Treasure lucas和CRT

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

  7. hdu 5446(中国剩余+lucas+按位乘)

    题意:c( n, m)%M    M = P1 * P2 * ......* Pk Lucas定理是用来求 c(n,m) mod p,p为素数的值.得出一个存余数数组,在结合中国剩余定理求值 其中有个 ...

  8. HDU 5446——Unknown Treasure——————【CRT+lucas+exgcd+快速乘+递推求逆元】

    Each test case starts with three integers n,m,k(1≤m≤n≤1018,1≤k≤10) on a line where k is the number o ...

  9. HDU 5446 CRT+Lucas+快速乘

    Unknown Treasure Problem Description On the way to the next secret treasure hiding place, the mathem ...

随机推荐

  1. Ubuntu下Nginx的安装和卸载

    环境是Ubuntu 16.04 安装: sudo apt-get update sudo apt-get install nginx 卸载: sudo apt-get --purge remove n ...

  2. Git 中无法忽略 .xcuserstate 的解决方法

    1.查看代码变化git status 2.接着输入 git rm –cached 刚才复制的地址 ,如下.git rm --cached RxSwift/Rx.xcodeproj/xcuserdata ...

  3. arcgis python 标注

    import arcpy mxd = arcpy.mapping.MapDocument("current") lyr = arcpy.mapping.ListLayers(mxd ...

  4. office很抱歉遇到一些临时服务器问题

    office2016登录很抱歉遇到一些临时服务器问题   主要问题:word不能进行发博客了.一直以为是cnblog服务器不稳定,今天才发现,word不能登录也就是不能联网. 查了原因,才知道是代理造 ...

  5. Coarse-to-Fine超分辨率相关

    1.A Coarse-to-Fine Subpixel Registration Method to Recover Local Perspective Deformation in the Appl ...

  6. [hibernate]log4jdbc日志输出完整SQL语句

    1.在maven引入: <dependency> <groupId>log4j</groupId> <artifactId>log4j</arti ...

  7. OSG Qt Widget加载三维模型

    graphicswindowqt.h #ifndef GRAPHICSWINDOWQT_H #define GRAPHICSWINDOWQT_H #include <QGLWidget> ...

  8. MyBatis接口式编程

    MyBatis使用接口连接数据库 之前学习了如何传统的使用MyBatis连接数据库,今天学习如何使用更方便快捷的方式练级数据库.使用接口连接数据库 https://www.cnblogs.com/li ...

  9. 【DSP开发】CCS数据格式 load

    CCS支持的.dat文件详解(转载于hellodsp) CCS支持的.dat文件的格式为: 定数 数据格式 起始地址 页类型 数据块大小 1651 其后是文件内容,每行表示一个数据. 定数固定为&qu ...

  10. Spring 控制器重定向

    1.示例 return "redirect:/allUser"; redirect是跳转的意思后面是跳转的页面