zoukankan      html  css  js  c++  java
  • BZOJ1485 有趣的数列

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1485

    首先有一个显而易见的结论:

    对于任意的 $a(i)$ ($i$为偶数) 有 $a(i) > a(j) (0<j<i)$

    然后有 $a(i) > 2 cdot i$ (只考虑选出n个偶数项)

    $f[i][j]$ 表示前$i$个,最大的数为 $j$ 的方案数,然后 $O(n^2)$ TLE

    打表发现是catalan数,然后就没有然后了。

    将$n!$的质因数分解然后计算$C(n,2 cdot n)/(n+1)$相当于计算出所有质数,然后计算他们各自在答案中的指数,最后乘起来。

    然后$O(nlogn)$AC。

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    
    #define N 2000010
    #define LL long long
    
    using namespace std;
    
    int n,P,fact[N],f[N];
    bool pri[N];
    
    // h[n] = C(2n,n)/(n+1) (mod P)
    
    inline LL qpow(LL x,LL n){
        LL ans=1;
        for(;n;n>>=1,x=x*x%P)
            if(n&1) ans=ans*x%P;
        return ans;
    }
    
    inline void Fac(int n,int v){
        for(int i=2;i<=n;i++) f[i]=i;
        for(int i=2,j;i<=n;i++){
            if(pri[i]) continue;
            for(j=i;j<=n;j+=i){
                if(j>i) pri[j]=1;
                while(f[j]%i==0){
                    f[j]/=i;
                    fact[i]+=v;
                }
            }
        }
    }
    
    int main(){
        scanf("%d%d",&n,&P);
        Fac(2*n,1);
        Fac(n,-2);
        int x=n+1;
        for(int i=2;i<=n+1&&x>1;i++){
            if(pri[i]) continue;
            while(x%i==0) x/=i,fact[i]--;
        }
        LL ans=1;
        for(int i=2;i<=2*n;i++){
            if(pri[i]) continue;
            ans=ans*qpow(i,fact[i])%P;
        }
        printf("%d
    ",ans);
        return 0;
    }
    View Code
  • 相关阅读:
    惊讶
    BLOG休假
    因考试得福
    Shape of My HeartSting !
    一个月的第一天了
    BLOG开张喽~~~
    该走了
    脏话
    EditText的属性
    游戏引擎
  • 原文地址:https://www.cnblogs.com/lawyer/p/4550788.html
Copyright © 2011-2022 走看看