Unknown Treasure

Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

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 k different 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
题意:求c(n,m)%(p1*p2*...*pk);
思路:求出每个c(n,m)%p1=a1......求出a数组;
   然后根据a求中国剩余即是答案;
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=1e5+,M=1e6+,inf=1e9+;
const ll INF=1e18+,mod=;
ll p[],a[];
ll n,m;
ll mulmod(ll x,ll y,ll m)
{
ll ans=;
while(y)
{
if(y%)
{
ans+=x;
ans%=m;
}
x+=x;
x%=m;
y/=;
}
ans=(ans+m)%m;
return ans;
}
ll ff(ll x,ll p)
{
ll ans=;
for(int i=;i<=x;i++)
ans*=i,ans%=p;
return ans;
}
ll pow_mod(ll a, ll x, ll p) {
ll ret = ;
while (x) {
if (x & ) ret = ret * a % p;
a = a * a % p;
x >>= ;
}
return ret;
} ll Lucas(ll n, ll k, ll p) { //C (n, k) % p
ll ret = ;
while (n && k) {
ll nn = n % p, kk = k % p;
if (nn < kk) return ; //inv (f[kk]) = f[kk] ^ (p - 2) % p
ret = ret * ff(nn,p) * pow_mod (ff(kk,p) * ff(nn-kk,p) % p, p - , p) % p;
n /= p, k /= p;
}
return ret;
}
void exgcd(ll a, ll b, ll &x, ll &y)
{
if(b == )
{
x = ;
y = ;
return;
}
exgcd(b, a % b, x, y);
ll tmp = x;
x = y;
y = tmp - (a / b) * y;
}
ll CRT(ll a[],ll m[],ll n)
{
ll M = ;
ll ans = ;
for(ll i=; i<=n; i++)
M *= m[i];
for(ll i=; i<=n; i++)
{
ll x, y;
ll Mi = M / m[i];
exgcd(Mi, m[i], x, y);
//ans = (ans + Mi * x * a[i]) % M;
ans = (ans +mulmod( mulmod( x , Mi ,M ), a[i] , M ) ) % M;
}
ans=(ans + M )% M;
return ans;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int k;
scanf("%lld%lld%d",&n,&m,&k);
for(int i=;i<=k;i++)
scanf("%lld",&p[i]);
for(int i=;i<=k;i++)
a[i]=Lucas(n,m,p[i]);
printf("%lld\n",CRT(a,p,k));
}
return ;
}

hdu 5446 Unknown Treasure 卢卡斯+中国剩余定理的更多相关文章

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

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

  2. HDU 5446 Unknown Treasure (卢卡斯+CRT

    代码: #include"bits/stdc++.h" #define db double #define ll long long #define vec vector<l ...

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

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

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

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

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

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

  7. hdu 5446 Unknown Treasure lucas和CRT

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

  8. HDU 5446 Unknown Treasure

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

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

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

随机推荐

  1. ubuntu下wine安装软件

    安装wine 1. sudo apt-get install playonlinux playonlinux就是wine.或者在软件中心,搜索wine. 2. 在dash搜索playonlinux 安 ...

  2. Cookie 知识点再整理

    1. Cookie  是存储在客户端 内存 或者 硬盘(例如火狐把 Cookie 存储在 C:\Documents and Settings\用户名\Application Data\Mozilla\ ...

  3. CSS 伪元素&伪类

    单冒号(:)用于CSS3伪类,双冒号(::)用于CSS3伪元素 伪元素 属性 描述 CSS :first-letter 向文本的第一个字母添加特殊样式 1 :first-line 向文本的首行添加特殊 ...

  4. ListView简单使用

    先上效果: 主要实现了Listview的绑定和点击事件.项目资源结构如下: 先创建一个动物类,用来装载数据: Animal类如下: package com.example.simplelistview ...

  5. Cell的一些坑: UITableViewCell宽度,在iphone5的时候是320,在iphone6的时候为啥也是320?

    在自定制cell'的.m文件里重写setframe方法就可以了- (void)setFrame:(CGRect)frame{    frame.size.width = [UIScreen mainS ...

  6. Java集合---HashMap源码剖析

    一.HashMap概述二.HashMap的数据结构三.HashMap源码分析     1.关键属性     2.构造方法     3.存储数据     4.调整大小 5.数据读取           ...

  7. Maven-001-初识及本地环境配置

    前段时间想对自己之前写的一些代码或者小工具,因为写的比较乱,因而想系统的管理一下自己学习 Java 时写的源码,经过多方请教.网上查询,最终决定使用 Maven 来管理自己写的代码. Maven 是一 ...

  8. LeetCode Minimum Height Trees

    原题链接在这里:https://leetcode.com/problems/minimum-height-trees/ 题目: For a undirected graph with tree cha ...

  9. oracle重建控制文件

    根据已有数据库创建新的控制文件#数据库必须是mounted或open状态 sql> alter database backup controlfile to trace; 可以使用以下快捷方式找 ...

  10. node与Elment以及子节点childrenNode与children的区别(2)

    测试代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...