zoukankan      html  css  js  c++  java
  • Contest2037

    http://acm.csu.edu.cn/OnlineJudge/problem.php?cid=2037&pid=1

    【题解】:卡特兰数取模

    h(n) = h(n-1)*(4*n-2)/(n+1)

    这题我们公式选择错了,不过还是能AC的

    因为要取模,应该选 h(n)=h(0)*h(n-1)+h(1)*h(n-2)+...+h(n-1)*h(0) (n>=2)

    【code-java】:

     1 import java.math.BigInteger;
     2 import java.util.Scanner;
     3 
     4 public class Main {
     5 
     6         public static void main(String[] args) {
     7             Scanner cin = new Scanner(System.in);
     8             BigInteger [] arr = new BigInteger[10100];
     9             BigInteger temp;
    10             arr[1] = new BigInteger("1");
    11             for(int i=2;i<=10000;i++){
    12                 temp = new BigInteger(""+(4*i-2)+"");
    13                 arr[i] = arr[i-1].multiply(temp).divide(new BigInteger(""+(i+1)+""));
    14                 //System.out.println(arr[i]);
    15             }
    16             
    17             while(cin.hasNextInt())
    18             {
    19                 BigInteger a;
    20                 int n,i;
    21                 n=cin.nextInt();
    22                 System.out.println(arr[n].mod(new BigInteger("1000000007")));
    23             }
    24         }
    25 }

    【code-C++】:

     1 #include <iostream>
     2 #include <vector>
     3 #include <list>
     4 #include <deque>
     5 #include <queue>
     6 #include <iterator>
     7 #include <stack>
     8 #include <map>
     9 #include <set>
    10 #include <algorithm>
    11 #include <cctype>
    12 #include <cstdio>
    13 #include <cstdlib>
    14 #include <cstring>
    15 #include <string>
    16 #include <cmath>
    17 using namespace std;
    18 
    19 const int m=1000000007;
    20 typedef long long LL;
    21 
    22 int n;
    23 
    24 void mul(LL &res,int k)
    25 {
    26     res=(res*k)%m;
    27 }
    28 
    29 int ext_gcd(int a,int b,int &x,int &y)
    30 {
    31     int ret;
    32     if(!b)
    33     {
    34         x=1;y=0;return a;
    35     }
    36     ret=ext_gcd(b,a%b,y,x);
    37     y-=x*(a/b);
    38     return ret;
    39 }
    40 
    41 void chu(LL &res,int k)
    42 {
    43     if(k!=1)
    44     {
    45         int x,y,temp;
    46         temp=ext_gcd(k,m,x,y);
    47         x=(x%m+m)%m;
    48         res=(res*x)%m;
    49     }
    50 }
    51 
    52 LL arr[11111];
    53 
    54 int main()
    55 {
    56      LL res=1,l=1;
    57      arr[1]=1;
    58     for(int i=2;i<=10000;i++)
    59     {
    60         mul(res,4*i-2);
    61         chu(res,i+1);
    62         l=res;
    63         l=l%m;
    64         arr[i]=l;
    65     }
    66     while(scanf("%d",&n)!=EOF)
    67     {
    68 
    69         printf("%lld
    ",arr[n]);
    70     }
    71     return 0;
    72 }
  • 相关阅读:
    hdu4990矩阵快速幂
    预处理+状态压缩+剪枝——codefoece 1209E
    AC自动机处理多串匹配——cf1202E
    二维差分前缀和——cf1202D(好题)
    序列递推——cf1204E(好题)
    建模+线性dp——cf1201D
    暴力——cf1202C
    经典排序背包——cf1203F
    思维+贪心——cf1042D
    分块——cf1207F
  • 原文地址:https://www.cnblogs.com/crazyapple/p/3349478.html
Copyright © 2011-2022 走看看