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 ;
}

最新文章

  1. iOS--关于同步下载
  2. sdutoj 2607 Mountain Subsequences
  3. 定义一个“点”(Point)类用来表示三维空间中的点(有三个坐标)。要求如下: (1)可以生成具有特定坐标的点对象。 (2)提供可以设置三个坐标的方法。 (3)提供可以计算该“点”距原点距离平方的方法。 (4)编写主类程序验证。
  4. .NET使用ICSharpCode.SharpZipLib压缩/解压文件
  5. POJ 3259 Wormholes (最短路)
  6. 【BZOJ】【3759】Hungergame饥饿游戏
  7. HDOJ/HDU 1256 画8(绞下思维~水题)
  8. Android系统智能指针的设计思路(轻量级指针、强指针、弱指针)
  9. flask-信号
  10. bzoj3435 [Wc2014]紫荆花之恋
  11. centos django Failed to load resource: net::ERR_INCOMPLETE_CHUNKED_ENCODING
  12. C# Timer 定时任务
  13. 在Apex中使用sObject
  14. MSA微服务
  15. 使用Jena执行SPARQL的Select和Ask查询
  16. 使用Fiddler发送POST请求
  17. 102. Binary Tree Level Order Traversal + 103. Binary Tree Zigzag Level Order Traversal + 107. Binary Tree Level Order Traversal II + 637. Average of Levels in Binary Tree
  18. static{}块的作用
  19. 计算像素亮度(Pixel Light)
  20. Servlet The Filter

热门文章

  1. 节点操作-创建并添加&amp;删除节点&amp;替换&amp;克隆节点
  2. 总结-swing、JFrame、JScrollPane、JTabbedPane、JEditorPane
  3. thinkphp3.2 cli模式的正确使用方法
  4. faster with MyISAM tables than with InnoDB or NDB tables
  5. WPF TabControl 隐藏标头
  6. Linux内核设计第一周 ——从汇编语言出发理解计算机工作原理
  7. SQLServer性能调优3之索引(Index)的维护
  8. soap和wsdl的定义
  9. 去除行内(inline/inline-block)元素之间的间距
  10. Chrome浏览器M53更新后超链接的dispatchEvent(evt)方法无法触发文件下载