HDU 5446 Unknown Treasure

题意:求C(n, m) %(p[1] * p[2] ··· p[k])     0< n,m < 1018

思路:这题基本上算是模版题了,Lucas定理求C(n,m),再用中国剩余定理合并模方程,因为LL相乘会越界,所以用到按位乘。

 #include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define LL long long
#define eps 1e-8
#define INF 0x3f3f3f3f
#define MAXN 10005
#define MAXK 15
using namespace std;
LL p[MAXK], mod[MAXK];
LL quick_mod(LL x, LL y, LL mod){
if (y == ) return ;
LL res = quick_mod(x, y >> , mod);
res = res * res % mod;
if (y & ){
res = res * x % mod;
}
return res;
}
LL comb(LL n, LL m, LL p){
LL res = ;
for (int i = ; i <= m; i++){
LL x = (n + i - m) % p;
LL y = i % p;
res = res * (x * quick_mod(y, p - , p) % p) % p;
}
return res;
}
LL lucas(LL n, LL m, LL p){
if (m == ) return ;
return lucas(n / p, m / p, p) * comb(n % p, m % p, p) % p;
} LL exgcd(LL a, LL b, LL & x, LL & y)
{
if (b == ){
x = ;
y = ;
return a;
}
else{
LL temp = exgcd(b, a % b, x, y);
LL t = y;
y = x - y * (a / b);
x = t;
return temp;
}
}
LL multi(LL a, LL b, LL m){
LL res = ;
while (b){
if (b & ){
res = (res + a) % m;
}
a = (a + a) % m;
b >>= ;
}
return (res + m) % m;
}
LL china(LL p[], LL mod[], LL m){
LL M = ;
for (int i = ; i <= m; i++){
M *= p[i];
}
LL x, y, res = ;
LL d;
for (int i = ; i <= m; i++){
LL s = M / p[i];
d = exgcd(p[i], s, d, y);
res = (res + multi(multi(y, s, M), mod[i], M)) % M;
}
return res;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // OPEN_FILE
int T;
scanf("%d", &T);
LL n, m, k;
while (T--){
scanf("%I64d%I64d%I64d", &n, &m, &k);
for (int i = ; i <= k; i++){
scanf("%I64d", &p[i]);
mod[i] = lucas(n, m, p[i]);
}
LL ans = china(p, mod, k);
printf("%I64d\n", ans);
}
}

HDU 5446 Unknown Treasure Lucas+中国剩余定理+按位乘的更多相关文章

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

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

  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和CRT

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

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

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

  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 中国剩余定理+lucas

    题目链接 求C(n, m)%p的值, n, m<=1e18, p = p1*p2*...pk. pi是质数. 先求出C(n, m)%pi的值, 然后这就是一个同余的式子. 用中国剩余定理求解. ...

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

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

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

  9. ACM学习历程—HDU 5446 Unknown Treasure(数论)(2015长春网赛1010题)

    Problem Description On the way to the next secret treasure hiding place, the mathematician discovere ...

随机推荐

  1. Linux 磁盘管理及分区

    硬盘结构和基础知识  扇区(Sector)为最小的物理储存单位,每个扇区为512 bytes,将扇区组成一个圆就是磁道(track),不同磁盘的相同磁道组成磁柱(Cylinder),磁柱是分区(par ...

  2. docker安装MySQL8,目录挂载、配置用户名密码、忽略表名大小写、连接数、特殊字符、时区

    原文:docker安装MySQL8,目录挂载.配置用户名密码.忽略表名大小写.连接数.特殊字符.时区 一.环境配置 1.系统:centos7.3 2.docker版本:Docker version 1 ...

  3. java 实现顺序结构线性列表

    package com.ncu.list; /** * * 顺序结构线性列表 * * @author liuhao * */ public class SquenceList<T> { p ...

  4. 面向对象设计(OOD)七大原则

    这篇文章我会不停的维护它,它将会越来越长,但它是关于我在面向对象中的一些学习的思考心得.希望对自己对各位都能实用处.     开篇前,说明一下写这篇文章的原因.原因是由于设计模式.由于设计模式里的各种 ...

  5. Android Bitmap太大导致ImageView不显示的问题

    今天做我们的智能相冊的项目时,遇到了非常奇妙的问题,当照片太大时,导致ImageView.setImageBitmap不显示,上网上搜了非常多办法.感觉都不是那么靠谱.最后使用了简单粗暴的手段: // ...

  6. mysql-汇总(聚集)函数

    我们需要汇总数据而不用把他们实际检索出来,他们主要用来进行分析和报表数据的生成. 1.AVG:通过对表中行数计数并计算特定列值之和,求得该列的平均值.可用来返回所有列的平均值,也可以用来返回特定列或行 ...

  7. 【POJ 1845】 Sumdiv (整数唯分+约数和公式+二分等比数列前n项和+同余)

    [POJ 1845] Sumdiv 用的东西挺全 最主要通过这个题学了约数和公式跟二分求等比数列前n项和 另一种小优化的整数拆分  整数的唯一分解定理: 随意正整数都有且仅仅有一种方式写出其素因子的乘 ...

  8. bzoj1934: [Shoi2007]Vote 善意的投票(显然最小割)

    1934: [Shoi2007]Vote 善意的投票 题目:传送门 题解: 明显的不能再明显的最小割... st连同意的,不同意的连ed 朋友之间两两连边(即双向边) 流量都为1... 为啥: 一个人 ...

  9. java操作文件的创建、删除、遍历

    java操作文件的创建.删除.遍历: package test; import java.io.File; import java.io.IOException; import java.util.A ...

  10. 使用tinyxml2库解析xml

    tinyxml2简介 tinyxml2是c++编写的轻量级的xml解析器,而且是开放源代码的,在一些开源的游戏引擎中用的比较多.源码托管在github上. 源码地址:https://github.co ...