[HDU3240]Counting Binary Trees(不互质同余除法)
Counting Binary Trees
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 564 Accepted Submission(s): 184
Problem DescriptionThere are 5 distinct binary trees of 3 nodes:
Let T(n) be the number of distinct non-empty binary trees of no more than n nodes, your task is to calculate T(n) mod m.InputThe input contains at most 10 test cases. Each case contains two integers n and m (1 <= n <= 100,000, 1 <= m <= 109) on a single line. The input ends with n = m = 0.OutputFor each test case, print T(n) mod m.Sample Input3 100
4 10
0 0Sample Output8
2Source
题意:求Catalan数的前n项和。
直接递推公式就好了,但是有一个问题,递推式里有除法,而由于除数与模数不互质,不能预处理逆元,这里有一个求不互质同余除法的方法(前提是结果必须是整数,所以只能用来求Catalan,Stirling和组合数这样的数)
$\frac{a}{b}\equiv c (mod \ d)$,我们先将d质因数分解,然后对于$a$和$b$将d的质因子部分单独统计,剩余部分直接exgcd求逆元即可。
因为剩余部分满足互质所以可以直接做逆元,而我们有$p\equiv p(mod \ ap)$,所以最后质因子部分直接乘就可以了。
这样就解决了HNOI2017的70分做法。
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#define rep(i,l,r) for (int i=l; i<=r; i++)
using namespace std; const int maxn=100010;
typedef long long ll; ll ans,cnt[maxn];
vector<int> prime;
int n,m; void exgcd(ll a,ll b,ll &x,ll &y){
if (!b) x=1,y=0;
else exgcd(b,a%b,y,x),y-=x*(a/b);
} ll inv(ll a,ll p){ ll x,y; exgcd(a,p,x,y); return (x+p)%p; } void getPrime(){
ll t=m;
for (int i=2; i*i<=t; i++)
if (t%i==0){
prime.push_back(i);
while (t%i==0) t/=i;
}
if (t>1) prime.push_back(t);
} void solve(){
getPrime(); ans=1; ll res=1;
rep(i,2,n){
ll fz=4*i-2,fm=i+1;
for (int k=0; k<(int)prime.size(); k++)
if (fz%prime[k]==0)
while (fz%prime[k]==0) fz/=prime[k],cnt[k]++;
res=(res*fz)%m;
for (int k=0; k<(int)prime.size(); k++){
if (fm%prime[k]==0)
while (fm%prime[k]==0) fm/=prime[k],cnt[k]--;
}
if (fm>1) res=(res*inv(fm,m))%m;
ll t=res;
for (int k=0; k<(int)prime.size(); k++)
rep(s,1,cnt[k]) t=(t*prime[k])%m;
ans=(ans+t)%m;
}
printf("%lld\n",ans);
} int main(){
while(~scanf("%d%d",&n,&m) && n+m)
prime.clear(),memset(cnt,0,sizeof(cnt)),solve();
return 0;
}
[HDU3240]Counting Binary Trees(不互质同余除法)的更多相关文章
- hdu3240 Counting Binary Trees
Counting Binary Trees Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- POJ 2891- Strange Way to Express Integers CRT 除数非互质
题意:给你余数和除数求x 注意除数不一定互质 思路:不互质的CRT需要的是将两个余数方程合并,需要用到扩展GCD的性质 合并互质求余方程 m1x -+ m2y = r2 - r1 先用exgcd求出特 ...
- Hello Kiki(中国剩余定理——不互质的情况)
Hello Kiki Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- hdu 3579 Hello Kiki 不互质的中国剩余定理
Hello Kiki Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Probl ...
- HDU3579Hello Kiki(中国剩余定理)(不互质的情况)
One day I was shopping in the supermarket. There was a cashier counting coins seriously when a littl ...
- openjudge7834:分成互质组 解析报告
7834:分成互质组 总时间限制: 1000ms 内存限制: 65536kB 描述 给定n个正整数,将它们分组,使得每组中任意两个数互质.至少要分成多少个组? 输入 第一行是一个正整数n.1 &l ...
- poj3696 快速幂的优化+欧拉函数+gcd的优化+互质
这题满满的黑科技orz 题意:给出L,要求求出最小的全部由8组成的数(eg: 8,88,888,8888,88888,.......),且这个数是L的倍数 sol:全部由8组成的数可以这样表示:((1 ...
- codeforces 687B - Remainders Game 数学相关(互质中国剩余定理)
题意:给你x%ci=bi(x未知),是否能确定x%k的值(k已知) ——数学相关知识: 首先:我们知道一些事情,对于k,假设有ci%k==0,那么一定能确定x%k的值,比如k=5和ci=20,知道x% ...
- HDU5668 Circle 非互质中国剩余定理
分析:考虑对给定的出圈序列进行一次模拟,对于出圈的人我们显然可以由位置,编号等关系得到一个同余方程 一圈做下来我们就得到了n个同余方程 对每个方程用扩展欧几里得求解,最后找到最小可行解就是答案. 当然 ...
随机推荐
- Mysql储存过程8:repeat循环
语法: repeat SQL until 条件 end repeat; 就是相当于其他语言中的: do{ # }while(); mysql> create procedure p1p() -& ...
- 16 - 文件操作-StringIO-BytesIO
目录 1 文件操作 1.1 open函数介绍 1.2 打开操作 1.2.1 mode模式 1.2.2 文件指针 1.2.3 缓冲区 1.2.4 encoding编码 1.2.5 其他参数 1.3 读写 ...
- flask插件系列之SQLAlchemy基础使用
sqlalchemy是一个操作关系型数据库的ORM工具.下面研究一下单独使用和其在flask框架中的使用方法. 直接使用sqlalchemy操作数据库 安装sqlalchemy pip install ...
- [Leetcode] Search in Rotated Sorted Array 系列
Search in Rotated Sorted Array 系列题解 题目来源: Search in Rotated Sorted Array Search in Rotated Sorted Ar ...
- Oracle安装出现报错
报错信息如下: >>> Couldnot execute auto check for display colors using command /usr/bin/xdpyinfo. ...
- curl基于URL的文件传输工具
简介 cURL是一款开源的基于URL的文件传输工具,支持HTTP.HTTPS.FTP等协议,支持POST.cookie.认证.扩展头部.限速等特性. curl命令用途广泛,比如下载.发送http请求. ...
- cgi与html相互调用
html中调用cgi.<form action="/cgi-bin/mult.cgi" method="get" target="_blank& ...
- Perl 连接Oracle 出现OCI missing的问题及解决
问题描述 新申请了一个虚拟机操作系统: Win Server 2008, 64位 , 8核, 16G Memory 上 http://www.activestate.com/activeperl 下载 ...
- hdu 5692(dfs序+线段树,好题)
Snacks Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- CVE-2012-0158个人分析
CVE-2012-0158是一个比较有名的老漏洞了,这次从论坛上找到一个poc文件,利用这个poc来分析CVE-2012-0158漏洞的形成. http://bbs.pediy.com/showthr ...
