ACM学习历程—HDU 5446 Unknown Treasure(数论)(2015长春网赛1010题)
Each test case starts with three integers n,m,k(1≤m≤n≤10^18,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≤10^18 and pi≤10^5 for every i∈{1,...,k}.
题目要求一个大组合数模几个素数乘积的结果。
大组合那块能通过Lucas得到对每个素数模的结果。
然后再通过互质型的中国剩余定理可以得到最终结果。
不过我的模板中国剩余定理里面乘法部分会爆long long。
然后用大数优化了乘法部分,通过大数乘大数,模完后返回小数。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <queue>
#include <vector>
#define LL long long
#define UNIT 10 using namespace std; const int maxK = ;
LL n, m, s[maxK], prime[maxK];
int k; struct Bignum
{
int val[];
int len; Bignum()
{
memset(val, , sizeof(val));
len = ;
} Bignum operator=(const LL &a)
{
LL t, p = a;
len = ;
while (p >= UNIT)
{
t = p - (p/UNIT)*UNIT;
p = p / UNIT;
val[len++] = t;
}
val[len++] = p;
return *this;
} Bignum operator*(const Bignum &a) const
{
Bignum x;
int i, j, up;
int x1, x2;
for (i = ; i < len; i++)
{
up = ;
for (j = ; j < a.len; j++)
{
x1 = val[i]*a.val[j] + x.val[i+j] + up;
if (x1 >= UNIT)
{
x2 = x1 - x1/UNIT*UNIT;
up = x1 / UNIT;
x.val[i+j] = x2;
}
else
{
up = ;
x.val[i+j] = x1;
}
}
if (up != )
x.val[i+j] = up;
}
x.len = i + j;
while (x.val[x.len-] == && x.len > )
x.len--;
return x;
} LL operator%(const LL &a) const
{
LL x = ;
for (int i = len-; i >= ; --i)
x = ((x*UNIT)%a+val[i]) % a;
return x;
}
}; LL mulMod(LL x, LL y, LL p)
{
LL ans = ;
Bignum xx, yy;
xx = x;
yy = y;
xx = xx*yy;
ans = xx%p;
return ans;
} LL quickMod(LL a, LL b, LL p)
{
LL ans = ;
a %= p;
while (b)
{
if (b&)
{
ans = ans*a%p;
b--;
}
b >>= ;
a = a*a%p;
}
return ans;
} LL C(LL n, LL m, LL p)
{
if (m > n)
return ;
LL ans = ;
for(int i = ; i <= m; i++)
{
LL a = (n+i-m)%p;
LL b = i%p;
ans = ans*(a*quickMod(b, p-, p)%p)%p;
}
return ans;
} LL Lucas(LL x, LL y, LL p)
{
if (y == )
return ;
return C(x%p, y%p, p)*Lucas(x/p, y/p, p)%p;
} //EXGCD
//求解方程ax+by=d,即ax=d mod(b)
//扩展可求逆元
//O(logn)
void exgcd(LL a, LL b, LL &x, LL &y, LL &d)
{
if (b == )
{
x = ;
y = ;
d = a;
}
else
{
exgcd(b, a%b, y, x, d);
y -= a/b*x;
}
} //中国剩余定理(互质)
//其中a为除数数组,n为模数数组
LL CRT(LL *a, LL *n, int len)
{
LL N = , ans = ;
for (int i = ; i < len; i++)
{
N *= n[i];
}
for (int i = ; i < len; i++)
{
LL m = N / n[i];
LL x, y, d;
exgcd(m, n[i], x, y, d);
x = (x%n[i] + n[i]) % n[i];
//ans = (ans + m*a[i]*x%N) % N;
ans = (ans + mulMod(mulMod(m, a[i], N), x, N)) % N;
}
return ans;
} void input()
{
scanf("%I64d%I64d%d", &n, &m, &k);
for (int i = ; i < k; ++i)
scanf("%I64d", &prime[i]);
} void work()
{
for (int i = ; i < k; ++i)
s[i] = Lucas(n, m, prime[i]);
LL ans = CRT(s, prime, k);
printf("%I64d\n", ans);
} int main()
{
//freopen("test.in", "r", stdin);
int T;
scanf("%d", &T);
for (int times = ; times < T; ++times)
{
input();
work();
}
return ;
}
ACM学习历程—HDU 5446 Unknown Treasure(数论)(2015长春网赛1010题)的更多相关文章
- ACM学习历程—Hihocoder 1233 Boxes(bfs)(2015北京网赛)
hihoCoder挑战赛12 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 There is a strange storehouse in PKU. In this ...
- ACM学习历程—HDU 5317 RGCDQ (数论)
Problem Description Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find more an ...
- ACM学习历程—HDU 5459 Jesus Is Here(递推)(2015沈阳网赛1010题)
Sample Input 9 5 6 7 8 113 1205 199312 199401 201314 Sample Output Case #1: 5 Case #2: 16 Case #3: 8 ...
- ACM学习历程—HDU 5443 The Water Problem(RMQ)(2015长春网赛1007题)
Problem Description In Land waterless, water is a very limited resource. People always fight for the ...
- ACM学习历程——ZOJ 3829 Known Notation (2014牡丹江区域赛K题)(策略,栈)
Description Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathema ...
- hdu 5443 (2015长春网赛G题 求区间最值)
求区间最值,数据范围也很小,因为只会线段树,所以套了线段树模板=.= Sample Input3110011 151 2 3 4 551 21 32 43 43 531 999999 141 11 2 ...
- 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.. ...
- HDU 5446 Unknown Treasure Lucas+中国剩余定理+按位乘
HDU 5446 Unknown Treasure 题意:求C(n, m) %(p[1] * p[2] ··· p[k]) 0< n,m < 1018 思路:这题基本上算是模版题了 ...
- HDU 5446 Unknown Treasure Lucas+中国剩余定理
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5446 Unknown Treasure 问题描述 On the way to the next se ...
随机推荐
- python 基础 9.4 游标
一. 游标是系统为用户开设的一个数据缓冲区,存放SQL语句的执行结果. 用户可以用SQL 语句逐一从游标中获取记录,并赋值给主变量,交由python 进一步处理,一组主变量一次只能存放一条 ...
- 九度OJ 1346:会员积分排序 (排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:967 解决:413 题目描述: 元旦佳节快到了,超市A想要给会员一些奖品.但是奖品有限,所以它需要给这些会员做一个排序,然后将名单输出来.排 ...
- spring配置中的classpath
1 classpath指WEB-INF下面的classes目录 2 配置成classpath*的话,spring会去所有的classpath中去找,包括lib下面的jar包 对于web app而言,c ...
- effect request
from bs4 import BeautifulSoup import os filepath = 'D:\\pymine\\clean\\spider_map\\baidu_map_html_fi ...
- python+NLTK 自然语言学习处理八:分类文本一
从这一章开始将进入到关键部分:模式识别.这一章主要解决下面几个问题 1 怎样才能识别出语言数据中明显用于分类的特性 2 怎样才能构建用于自动执行语言处理任务的语言模型 3 从这些模型中我们可以学到那些 ...
- 又一次发现Oracle太美之awr相关脚本简介
又一次发现Oracle太美之awr相关脚本简介 大家知道在$ORACLE_HOME/rdbms/admin下,有例如以下的相关脚本(我的环境为11.2.0.4.2): [oracle@rh64 ~]$ ...
- java基础入门之数组循环初始化
/* Name:数组循环化 Power by Stuart Date:2015-4-23 */public class ArrayTest02{ public static void main (St ...
- pom.xml配置文件详解(转发)
setting.xml主要用于配置maven的运行环境等一系列通用的属性,是全局级别的配置文件:而pom.xml主要描述了项目的maven坐标,依赖关系,开发者需要遵循的规则,缺陷管理系统,组织和li ...
- 保持linux下保持ssh不断线
用ssh链接服务端,一段时间不操作或屏幕没输出(比如复制文件)的时候,会自动断开,有两种解决办法: 1.在客户端配置 #vi /etc/ssh/ssh_config(注意不是/etc/ssh/ssh ...
- action extension添加图标
最近在做ios的action extension,这里记录一下添加图标的方法. 在Action Extension的target里面的Build Settings,里面的Asset Catalog C ...