Unknown Treasure

Problem Description
On the way to the next secret treasure hiding place, the mathematician discovered a cave unknown to the map. The mathematician entered the cave because it is there. Somewhere deep in the cave, she found a treasure chest with a combination lock and some numbers on it. After quite a research, the mathematician found out that the correct combination to the lock would be obtained by calculating how many ways are there to pick m different apples among n of them and modulo it with M. M is the product of several different primes.
 
Input
On the first line there is an integer T(T≤20) representing the number of test cases.

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 of primes. Following on the next line are kdifferent primes p1,...,pk. It is guaranteed that M=p1⋅p2⋅⋅⋅pk≤1018 and pi≤105 for every i∈{1,...,k}.

 
Output
For each test case output the correct combination on a line.
 
Sample Input
1
9 5 2
3 5
 
Sample Output
6
 
Source
 
 
题意:M=p1*p2*...pk;求C(n,m)%M,pi小于10^5,n,m,M都是小于10^18。 pi为质数
题解:首先M=x*pi(1<=i<=k)
    M不一定是质数 所以只能用Lucas定理求k次 C(n,m)%Pi得到 B[i];
   最后会得到一个同余方程组
   x≡B[0](mod p[0])
   x≡B[1](mod p[1])
   x≡B[2](mod p[2])
   ......
   解这个同余方程组 用中国剩余定理
  但ll*ll都会爆所以用快速乘
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
const int INF=0x3f3f3f3f;
typedef long long LL;
LL M[],B[];
int t;
LL n,m,p,num;
LL exgcd(LL a, LL b, LL &x, LL &y)
{
if (!b)
{
x = ;
y = ;
return a;
}
LL gcd = exgcd(b, a % b, x, y);
LL t = x;
x = y;
y = t - (a / b) * x;
return gcd;
}
LL mult(LL a,LL b,LL p)
{
LL ans=;
while(b)
{
if(b&)
ans=(ans+a)%p;
b>>=;
a=(a+a)%p;
}
return ans;
}
LL inverse(LL num, LL mod)
{
LL x, y;
exgcd(num, mod, x, y);
return (x % mod + mod) % mod;
} LL C(LL a, LL b, LL mod)
{
if (b > a)
return ;
LL t1 = , t2 = ;
for (int i = ; i <= b; ++i)
{
t2 *= i;
t2 %= mod;
t1 *= (a - i + );
t1 %= mod;
}
return mult(t1,inverse(t2, mod),mod);
}
LL lucas(LL n, LL m, LL p)
{
if (m == )
return ;
return mult(C(n % p, m % p, p),lucas(n / p, m / p, p),p);
}
LL China(LL m[],LL b[],int k)//m:模数 b:余数
{
LL n=,xx,yy;
LL ans=;
for(int i=;i<k;i++)
n*=M[i];
for(int i=;i<k;i++)
{
LL t=n/M[i];
exgcd(t,M[i],xx,yy);
LL x1=mult(xx,t,n);
LL x2=mult(x1,b[i],n);
ans=(ans+x2)%n;
}
return (ans%n+n)%n;
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%lld%lld%lld",&n,&m,&num);
for(int i=;i<num;i++)
{
scanf("%d",&p);
M[i]=p;
B[i]=lucas(n,m,p);
}
LL ans=China(M,B,num);
printf("%lld\n",ans);
}
return ;
}

来自Tisuama

HDU 5446 CRT+Lucas+快速乘的更多相关文章

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

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

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

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

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

  4. CRT, lucas及其扩展形式

    CRT, lucas及其扩展形式 exgcd int exgcd(int a, int b, int &x, int &y) { if (b == 0) return a, x = 1 ...

  5. HDU 1061 Rightmost Digit --- 快速幂取模

    HDU 1061 题目大意:给定数字n(1<=n<=1,000,000,000),求n^n%10的结果 解题思路:首先n可以很大,直接累积n^n再求模肯定是不可取的, 因为会超出数据范围, ...

  6. HDU 1930 CRT

    也是很模板的一道题,给出一些数,分割,模数固定是4个互质的. /** @Date : 2017-09-16 23:54:51 * @FileName: HDU 1930 CRT.cpp * @Plat ...

  7. HDU 1573 CRT

    CRT模板题 /** @Date : 2017-09-15 13:52:21 * @FileName: HDU 1573 CRT EXGCD.cpp * @Platform: Windows * @A ...

  8. HDU.2640 Queuing (矩阵快速幂)

    HDU.2640 Queuing (矩阵快速幂) 题意分析 不妨令f为1,m为0,那么题目的意思为,求长度为n的01序列,求其中不含111或者101这样串的个数对M取模的值. 用F(n)表示串长为n的 ...

  9. HDU 5667 构造矩阵快速幂

    HDU 5667 构造矩阵快速幂 题目描述 解析 我们根据递推公式 设 则可得到Q的指数关系式 求Q构造矩阵 同时有公式 其中φ为欧拉函数,且当p为质数时有 代码 #include <cstdi ...

随机推荐

  1. ORA-01033:ORACLE initialization or shutdown in process

    Oracle遇到问题 :在PL/SQL当输入用户名和密码后 竟然出现标题上错误,我一项目数据库数据库全都没有备份,还有很多很多数据,该不会让我重装数据库吧,想到这个我汗那个流啊. 在网上查了下 看了看 ...

  2. Nuxt.js使用详解

    首先来讲一下服务端渲染 直白的说就是在服务端拿数据进行解析渲染,直接生成html片段返回给前端.具体用法也有很多种比如: 传统的服务端模板引擎渲染整个页面 服务渲染生成htmll代码块, 前端 AJA ...

  3. JAVA程序员面试笔试宝典2

    1.Java集合框架 2.迭代器 使用容器的iterator()方法返回一个iterator,然后通过iterator的next()方法返回第一个元素 使用iterator的hasnext()方法判断 ...

  4. Java基础(七)--Exception异常处理

    发现错误的理想时机是程序运行之前(编译期),然后不太现实,很多异常无法被发现(特别是业务上的数据),需要在运行时解决. 错误恢复机制保证代码健壮性的方式,异常处理在程序中很常见,也是必须的,必须考虑有 ...

  5. Eigen库笔记整理(二)

    Eigen/Geometry 模块提供了各种旋转和平移的表示 旋转矩阵直接使用 Matrix3d 或 Matrix3f Eigen::Matrix3d rotation_matrix = Eigen: ...

  6. 【Hadoop】二、HDFS文件读写流程

    (二)HDFS数据流   作为一个文件系统,文件的读和写是最基本的需求,这一部分我们来了解客户端是如何与HDFS进行交互的,也就是客户端与HDFS,以及构成HDFS的两类节点(namenode和dat ...

  7. ROS lesson 1

    ROS ROS官网 ROS 简介 ROS 是 Robot Operation System 的简写,并且 他诞生在2000年后,至今有10余年了,运行在 Linux(Ubuntu) 上 ROS 不是 ...

  8. 洛谷 3870 [TJOI2009]开关

    [题解] 线段树基础题.对于每个修改操作把相应区间的sum改为区间长度-sum即可. #include<cstdio> #include<algorithm> #include ...

  9. [bzoj1005][HNOI2008][明明的烦恼] (高精度+prufer定理)

    Description 自从明明学了树的结构,就对奇怪的树产生了兴趣......给出标号为1到N的点,以及某些点最终的度数,允许在任意两点间连线,可产生多少棵度数满足要求的树? Input 第一行为N ...

  10. 52. spring boot日志升级篇—log4j多环境不同日志级别的控制【从零开始学Spring Boot】

    在上一章节中我们介绍了,仅通过log4j-spring.properties对日志级别进行控制,对于需要多环境部署的环境不是很方便,可能我们在开发环境大部分模块需要采用DEBUG级别,在测试环境可能需 ...