HDU-3240(卡特兰数+分解质因数后求逆元)

卡特兰数相关公式 :

  1. \(H_n = {C_{2n}^n \over n+1)}\)
  2. \(H_n = {(4n-2)\over n+1}\times H_{n-1}\)
  3. \(H_n = C_{2n}^n - C_{2n}^{n-1}\)
  4. $ H_n = \begin{cases} \sum_{i=1}^{n} H_{i-1} H_{n-i} & n \geq 2, n \in \mathbf{N_{+}}\ 1 & n = 0, 1 \end{cases} $

因为 \(n\le 100000\) ,所以不考虑第四种,第一种和第三种种求组合数也不可以用递推来求,而用公式的话因为要预先算出阶乘和阶乘的逆元,在此题中m不保证为质数,所以也不好计算。

对于第三种,假设以及算出了\(H_{n-1}\) 那么只需要求出\({4n-2\over n+1}\) 即可

先把 m 分解质因数,然后对于分子 \(4n-2\) ,求出m分解后的质因子的所有指数,然后对于分母\(n+1\) ,也求出m分解的质因子的所有指数,前后两个指数数组相减。剩下分子和分母与m互质的部分直接求即可,保留到递推答案pre中。

综上,当前递推的答案由一个变量pre 和一个指数数组共同记录,所以在地推出当前答案之后进行累加时,还要用pre乘所有质因子

#include <bits/stdc++.h>
using namespace std;
const int N = 1000010;
typedef long long ll;
int n,m;
int cnt[N];
vector<ll> v;
void exgcd(ll a,ll b,ll& d,ll& x,ll& y) {
    if(!b) {
        d = a; x = 1; y = 0;
    }else{
        exgcd(b,a%b,d,y,x); y -= x*(a/b);
    }
}
ll inv(ll a,ll n){
    ll d,x,y;
    exgcd(a,n,d,x,y);
    return d== 1?(x+n)%n:-1;
}
void getPrime(int x){
    for(int i=2;i*i<=x;i++){
        if(x % i)continue;
        while(x%i==0)x/=i;
        v.push_back(i);
    }
    if(x > 1)v.push_back(x);
    return;
}
int main(){
    while(cin >> n >> m){
        if(n == 0 && m == 0)break;
        v.clear();
        memset(cnt,0,sizeof cnt);
        ll res = 1 % m;
        ll pre = 1;
        getPrime(m);//对m进行分解质因数
        for(int i=2;i<=n;i++){
            ll fz = 4 * i - 2, fm = i + 1;
            for(int j=0;j<v.size();j++){
                if(fz % v[j] == 0)
                while(fz % v[j] == 0){
                    fz /= v[j];
                    cnt[j] ++;//指数++
                }
            }
            pre = pre * fz % m;//剩余互质部分直接乘
            for(int j=0;j<v.size();j++){
                if(fm%v[j] == 0)
                while(fm % v[j] == 0){
                    fm /= v[j];
                    cnt[j] --;//指数--
                }
            }
            if(fm > 1) pre = pre * inv(fm,m) % m;//更新pre
            ll tmp = pre;
            for(int j=0;j<v.size();j++){
                for(int k=1;k<=cnt[j];k++){
                    tmp = (tmp * v[j]) % m;//计算当前答案
                }
            }
            res = (res + tmp) % m;
        }
        cout << res << endl;
    }
    return 0;
}

为什么不是每次直接把质因子直接计算到pre中呢,因为之后的计算中,对分母(n+1)进行分解质因数时,有可能出现不够减的情况,所以我们要一直用一个数组记录这部分和m有公因数的部分

原文地址:https://www.cnblogs.com/1625--H/p/11462589.html

时间: 2024-08-02 10:07:47

HDU-3240(卡特兰数+分解质因数后求逆元)的相关文章

HDU 4828 (卡特兰数+逆元)

HDU 4828 Grids 思路:可以转化为卡特兰数,先把前n个人标为0,后n个人标为1,然后去全排列,全排列的数列,如果每个1的前面对应的0大于等于1,那么就是满足的序列,如果把0看成入栈,1看成出栈,那么就等价于n个元素入栈出栈,求符合条件的出栈序列,这个就是卡特兰数了.然后去递推一下解,过程中需要求逆元去计算 代码: #include <stdio.h> #include <string.h> const int N = 1000005; const long long M

hdu 5184 卡特兰数

hdu 5184 卡特兰数 题意: 我们给出下列递归的合法括号序列的定义: 1. 空序列是合法括号序列 2. 如果s是一个合法括号序列,那么(s)也是合法括号序列 3. 如果a和b是合法括号序列,那么ab也是合法括号序列 4. 没有其它情况是合法括号序列 比如下列括号序列是合法括号序列 (), (()), ()(), ()(()) 下列括号序列则不是 (, ), )(, ((), ((() 现在,我们要构造长度为n的合法括号序列,前面的一些括号已经给出,问可以构造出多少合法序列. 限制: 1 <

2014年百度之星程序设计大赛 - 初赛(第一轮) hdu Grids (卡特兰数 大数除法取余 扩展gcd)

题目链接 分析:打表以后就能发现时卡特兰数, 但是有除法取余. f[i] = f[i-1]*(4*i - 2)/(i+1); 看了一下网上的题解,照着题解写了下面的代码,不过还是不明白,为什么用扩展gcd, 不是用逆元吗.. 网上还有别人的解释,没看懂,贴一下: (a / b) % m = ( a % (m*b)) / b 笔者注:鉴于ACM题目特别喜欢M=1000000007,为质数: 当gcd(b,m) = 1, 有性质: (a/b)%m = (a*b^-1)%m, 其中b^-1是b模m的逆

HDU 4828 (卡特兰数+逆)

HDU 4828 Grids 思路:能够转化为卡特兰数,先把前n个人标为0.后n个人标为1.然后去全排列,全排列的数列.假设每一个1的前面相应的0大于等于1,那么就是满足的序列,假设把0看成入栈,1看成出栈.那么就等价于n个元素入栈出栈,求符合条件的出栈序列,这个就是卡特兰数了. 然后去递推一下解,过程中须要求逆元去计算 代码: #include <stdio.h> #include <string.h> const int N = 1000005; const long long

Train Problem II HDU 1023 卡特兰数

Problem Description As we all know the Train Problem I, the boss of the Ignatius Train Station want to know if all the trains come in strict-increasing order, how many orders that all the trains can get out of the railway. Input The input contains se

HDU 5428 [The Factor] 分解质因数

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5428 题目大意:给你若干个整数,让你输出这些数乘积的一个最小因子,并且这个因子至少有3个因子. 关键思想:分解质因数,我们要找的其实就是两个最小质因数的乘积.我的算法关键就是维护最小的两个质因数. 代码如下: #include <iostream> #include <cmath> #include <cstdio> #include <algorithm> u

hdu 1023 卡特兰数+高精度

Train Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Description As we all know the Train Problem I, the boss of the Ignatius Train Station want to know if all the trains come in strict-increasi

hdu 1130(卡特兰数,大数)

How Many Trees? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3382    Accepted Submission(s): 1960 Problem Description A binary search tree is a binary tree with root k such that any node v re

hdu 1023 卡特兰数

import java.math.BigInteger; import java.util.Scanner; public class Main{ public static void main(String args[]){ Scanner in = new Scanner(System.in); BigInteger[] a = new BigInteger[105]; for(int i = 0;i < 105;i++){ a[i] = new BigInteger("1"