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 ...
随机推荐
- 十款不容错过的Swift iOS开源项目及介绍
1.十款不容错过的Swift iOS开源项目. http://www.csdn.net/article/2014-10-16/2822083-swift-ios-open-source-project ...
- linux系统命令学习系列-用户切换命令su,sudo
先复习一下上节内容: 用户组添加groupadd 用户组修改groupmod 用户组删除groupdel 作业创建一个id为501的组group1,然后改成group2, 同时id变为502,最后删除 ...
- Thinkphp框架部署步骤
Thinkphp框架部署步骤 thinkphp框架部署起来简单,但是由于步骤较多也容易遗忘: 这是安装了集成环境后的一个www根目录结构: 然后需要在这个目录下面创建一个文件夹做项目:thinkphp ...
- 释义Oracle 11r2中并行执行相关参数
因最近对现场某些服务器进行诊断和调整,用到了这类参数,因此对这类参数做了详尽的查阅和研究,现将该类参数释义如下,以方便同行和自己参考,禁止转载: 1.PARALLEL_ADAPTIVE_MULTI_U ...
- Python内置函数(13)——bytearray
英文文档: class bytearray([source[, encoding[, errors]]]) Return a new array of bytes. The bytearray cla ...
- C语言学习(一)
C语言易学难精,如果在平时的编程中,加入一些小技巧,可以提供程序运行的效率,何乐而不为呢? 本小白初学C语言准备记录自己的学C之路,经常贴一些自己觉得优化的小程序代码,希望大神们不吝 赐教. 宏定义下 ...
- shuffle和sort分析
MapReduce中的Shuffle和Sort分析 MapReduce 是现今一个非常流行的分布式计算框架,它被设计用于并行计算海量数据.第一个提出该技术框架的是Google 公司,而Google 的 ...
- [机器学习实战]K-近邻算法
1. K-近邻算法概述(k-Nearest Neighbor,KNN) K-近邻算法采用测量不同的特征值之间的距离方法进行分类.该方法的思路是:如果一个样本在特征空间中的k个最相似(即特征空间中最邻近 ...
- 复习上学期的HTML+CSS(1)
自己跟着网上教程复习上学期的HTML+CSS,因为已经忘得差不多了,而且现在学的js也要以HTML+CSS为基础,坚持每天持续更新. n B/S 网络结构 Browser/Server 浏览器/ ...
- python/数据库操作补充—模板—Session
python/数据库操作补充—模板—Session 一.创建一个app目录 在models.py只能类进行进行创建表 class Foo: xx= 字段(数据库数据类型) 字段类型 字符串 Email ...