Unknown Treasure

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

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

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

HINT

题意

给你n,m,num

然后给你num个数,k1,k2....,knum

然后让你求C(n,m)%(k1*k2*....*knum)

题解:

首先,C(n,m)%k我们是会求的,大概这部分子问题是一个很经典的题目。

假设你会求了,那么我们就可以由此得到num个答案,是%k1,k2,k3....knum后得到的值

然后我们就可以看出就是类似韩信点兵一样的题,三个人一组剩了2个,五个人一组剩了2个这种……

这时候,就用中国剩余定理处理处理就好了

注意ll*ll会爆,所以得手写个快速乘法

代码:

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//************************************************************************************** void extend_gcd(ll a,ll &x,ll b,ll &y)
{
if(b==)
{
x=,y=;
return;
}
ll x1,y1;
extend_gcd(b,x1,a%b,y1);
x=y1;
y=x1-(a/b)*y1;
}
ll inv(ll a,ll m)
{
ll t1,t2;
extend_gcd(a,t1,m,t2);
return (t1%m+m)%m;
}
ll qpow(ll x,ll y,ll m)
{
if(!y)return ;
ll ans = qpow(x,y>>,m);
ans = ans*ans%m;
if(y&)ans = ans*x%m;
return ans;
} ll nump(ll x,ll p)
{
ll ans = ;
while(x)ans+=x/p,x/=p;
return ans;
}
ll fac(ll n,ll p,ll pk)
{
if(n==)return ;
ll ans = ;
for(ll i=;i<=pk;i++)
{
if(i%p==)continue;
ans = ans*i%pk;
}
ans = qpow(ans,n/pk,pk);
ll to = n%pk;
for(ll i =;i<=to;i++)
{
if(i%p==)continue;
ans = ans*i%pk;
}
return fac(n/p,p,pk)*ans%pk;
}
ll cal(ll n,ll m,ll p ,ll pi,ll pk)
{
ll a = fac(n,pi,pk),b=fac(m,pi,pk),c=fac(n-m,pi,pk);
ll d = nump(n,pi)-nump(m,pi)-nump(n-m,pi);
ll ans = a%pk * inv(b,pk)%pk * inv(c,pk)%pk*qpow(pi,d,pk)%pk;
return ans*(p/pk)%p*inv(p/pk,pk)%p;
}
ll mCnmodp(ll n,ll m,ll p)
{
ll ans = ;
ll x = p;
for(ll i =;i*i<=x&&x>;i++)
{
ll k=,pk=;
while(x%i==)
{
x/=i;
k++;
pk*=i;
}
if(k>)
ans=(ans+cal(n,m,p,i,pk))%p;
}
if(x>)ans=(ans+cal(n,m,p,x,x))%p;
return ans;
}
ll qtpow(ll x,ll y,ll M)
{
ll ret=0LL;
for(x%=M;y;y>>=1LL)
{
if(y&1LL)
{
ret+=x;
ret%=M;
if(ret<) ret+=M;
}
x+=x;
x%=M;
if(x<) x+=M;
}
return ret;
}
void solve(ll r[],ll s[],int t)
{
ll M=1LL,ans=0LL;
ll p[],q[],e[];
for(int i=;i<t;i++)
M*=r[i];
for(int i=;i<t;i++)
{
ll tmp=M/r[i],tt;
extend_gcd(tmp,p[i],r[i],q[i]);
p[i]%=M;
if(p[i]<) p[i]+=M;
e[i]=qtpow(tmp,p[i],M);
tt=qtpow(e[i],s[i],M);
ans=(ans+tt)%M;
if(ans<) ans+=M;
}
printf("%I64d\n",ans);
} ll CCC[],DDD[];
int main()
{
int t;
scanf("%d",&t);
int num = ;
ll n,m,p;
while(t--)
{
memset(CCC,,sizeof(CCC));
memset(DDD,,sizeof(DDD));
scanf("%I64d %I64d %d",&n,&m,&num);
for(int i=;i<num;i++)
{
scanf("%I64d",&CCC[i]);
DDD[i]=mCnmodp(n,m,CCC[i]);
}
solve(CCC,DDD,num);
}
return ;
}

hdu 5446 Unknown Treasure lucas和CRT的更多相关文章

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

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

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

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

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

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

  4. HDU 5446 Unknown Treasure(lucas + 中国剩余定理 + 模拟乘法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5446 题目大意:求C(n, m) % M, 其中M为不同素数的乘积,即M=p1*p2*...*pk, ...

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

  6. hdu 5446 Unknown Treasure 卢卡斯+中国剩余定理

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

  7. HDU 5446 Unknown Treasure

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

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

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

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

随机推荐

  1. 1439. Battle with You-Know-Who(splay树)

    1439 路漫漫其修远兮~ 手抄一枚splay树 长长的模版.. 关于spaly树的讲解   网上很多随手贴一篇 貌似这题可以用什么bst啦 堆啦 平衡树啦 等等 这些本质都是有共同点的 查找.删除特 ...

  2. CentOS6.5_Nginx1.40_Php5.57_MySQL5.5.35编译安装全记录

    环境说明:CentOS 6.5 32位  PHP Version 5.5.7  mysql version _5.6.16 一.准备工作 配置防火墙,允许防火墙通过22(sshd).80(WEB).3 ...

  3. android Bitmap getByteCount和getRowBytes

    今天做图像缓存需要计算Bitmap的所占的内存空间,于是研究了下Bitmap关于内存占用的API 1.getRowBytes:Since API Level 1,用于计算位图每一行所占用的内存字节数. ...

  4. C# 中的装箱与拆箱

    转角撞倒猪 原文 C# 中的装箱与拆箱   装箱:将一个数据项(副本)从栈中自动复制到堆中的行为. int i = 8; object o = i; // 装箱 // 首先在堆中开辟出一片区域,再将 ...

  5. Protobuf语言指南

    Protobuf语言指南 l  定义一个消息(message)类型 l  标量值类型 l  Optional 的字段及默认值 l  枚举 l  使用其他消息类型 l  嵌套类型 l  更新一个消息类型 ...

  6. JDBCTemplate.java

    package com.pk.xjgs.util; import java.sql.Connection; import java.sql.SQLException; import java.util ...

  7. 自己安装的几个Eclipse插件

    http://eclipsenotepad.sourceforge.net This plugin has the simple objective to let developers write s ...

  8. 【原创】lua的module的一些点

    lua的module好像是5.1开始有的 在xx.lua的开头写上 module('my_module') 这行等价于如下几行 local name = 'my_module' local M = { ...

  9. HTML-css selector

    Css selector 基本有三种 HTML(TAG)selector , ID selector , Class selector css selector 综合使用 : 重用,子选择器,组选择器 ...

  10. sqlserver 出现 因为文件组 'PRIMARY' 已满 的解决办法 有可能是磁盘剩余空间不足 导致的

    一般虚拟主机提供商是通过限制数据库文件的大小来实现提供定制的数据库空间的.当你把从虚拟数据库空间备份下来的文件恢复到自己的服务器上时,这个限制还是存在的.找到数据库文件 给增加个数据文件就好了 解决办 ...