hdu 5446(中国剩余+lucas+按位乘)
题意: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+按位乘)的更多相关文章
- HDU 5446 Unknown Treasure Lucas+中国剩余定理+按位乘
HDU 5446 Unknown Treasure 题意:求C(n, m) %(p[1] * p[2] ··· p[k]) 0< n,m < 1018 思路:这题基本上算是模版题了 ...
- HDU 5446 Unknown Treasure Lucas+中国剩余定理
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5446 Unknown Treasure 问题描述 On the way to the next se ...
- hdu 5446 Unknown Treasure Lucas定理+中国剩余定理
Unknown Treasure Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- HDU 5446 Unknown Treasure(lucas + 中国剩余定理 + 模拟乘法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5446 题目大意:求C(n, m) % M, 其中M为不同素数的乘积,即M=p1*p2*...*pk, ...
- hdu 5446 Unknown Treasure lucas和CRT
Unknown Treasure Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...
- HDU 5446 中国剩余定理+lucas
Unknown Treasure Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- hdu 2891 中国剩余定理
从6点看到10点,硬是没算出来,早知道玩游戏去了,艹,明天继续看 不爽,起来再看,终于算是弄懂了,以后超过一个小时的题不会再看了,不是题目看不懂,是水平不够 #include<cstdio> ...
- HDU 5768 中国剩余定理
题目链接:Lucky7 题意:求在l和r范围内,满足能被7整除,而且不满足任意一组,x mod p[i] = a[i]的数的个数. 思路:容斥定理+中国剩余定理+快速乘法. (奇+ 偶-) #incl ...
- hdu 5446 Unknown Treasure 卢卡斯+中国剩余定理
Unknown Treasure Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
随机推荐
- Vue.js学习
<!DOCTYPE html> <html> <head> <title>xxx</title> </head> <bod ...
- 【Swift】Runtime动态性分析
Swift是苹果2014年发布的编程开发语言,可与Objective-C共同运行于Mac OS和iOS平台,用于搭建基于苹果平台的应用程序.Swift已经开源,目前最新版本为2.2.我们知道Objec ...
- Tornado 协程
同步异步I/O客户端 from tornado.httpclient import HTTPClient,AsyncHTTPClient def ssync_visit(): http_client ...
- "未找到应用程序的“aps-environment”的权利字符串"
1.先生成App ID,在去Provisioning里面生成新的Profile 2.删除Xcode里面原来的push profile(如果没有就不用删除)再次双击新下载的profile(mobilep ...
- nyoj 公约数和公倍数
公约数和公倍数 时间限制:1000 ms | 内存限制:65535 KB 难度:1 描述 小明被一个问题给难住了,现在需要你帮帮忙.问题是:给出两个正整数,求出它们的最大公约数和最小公倍数. ...
- Visual Studio 开发工具常用的插件
转载自落日故乡 http://www.spersky.com/post/vsPlugins.html 该博客中收集整理归纳了若干个常用的vs插件,比如高亮显示当前选择,垂直辅助线,折叠代码等等,具体 ...
- OpenGL中怎么把世界坐标系变成屏幕坐标系
对这个3D坐标手动进行OpenGL的四个变换,得到的结果就是屏幕上的像素坐标.前三个变换(Model, View, Projection)都是4x4矩阵,操作对象是四维向量,所以需要把(100, 10 ...
- phalcon框架命名空间
命名空间第一影像就是实际上就相当宏定义,就是需要把一个很长的带有路径的类文件指定一个空间,然后就可直接用简单简写模式 当然如果是外部文件需要首先引入外部文件,如果不引入外部文件还是会报错.一般最会出错 ...
- Python内置函数(16)——ord
英文文档: ord(c) Given a string representing one Unicode character, return an integer representing the U ...
- JS的if和switch
var aa=parseInt(prompt("请输入你的年龄")); //定义输入 if(aa<18){ //输出小于18,返回值少年 alert("少年&quo ...