码迷,mamicode.com
首页 > 其他好文 > 详细

洛谷——P1375 小猫

时间:2017-11-25 22:33:19      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:include   stream   分享   连线   color   ret   opened   closed   质数   

P1375 小猫

题目描述

有2n只小猫站成一圈,主人小明想把它们两两之间用绳子绑住尾巴连在一起。同时小明是个完美主义者,不容许看到有两根绳子交叉。请问小明有几种连线方案,可以把让所有小猫两两配对?

方案数很大,仅需输出方案数模1000000007(一个质数)的值。

数据范围:

60% N<=100

100% N<=100000

输入输出格式

输入格式:

 

输入一个整数n

 

输出格式:

 

输出方案数对1000000007取模的值

 

输入输出样例

输入样例#1: 复制
3
输出样例#1: 复制
5
卡特兰数
这个题可以很快的将她转化成一个有2*n个点的圆,要在上面接n条边,求方案数
卡特兰数递推式
技术分享图片
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 100010
#define mod 1000000007
using namespace std;
int n,a,b,c,h[N],gcd;
int read()
{
    int x=0,f=1; char ch=getchar();
    while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}
    while(ch>=0&&ch<=9) x=x*10+ch-0,ch=getchar();
    return x*f;
}
int main()
{
    n=read();
    h[0]=h[1]=1;
    for(int i=2;i<=n;i++)
     for(int j=1;j<=i;j++)
      h[i]=(1ll*h[j-1]*h[i-j]%mod+h[i]%mod)%mod; 
    printf("%d",h[n]);
    return 0;
}
80分

AC代码

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 100010
#define LL long long
#define mod 1000000007
using namespace std;
LL n,ans,f[N*2];
LL read()
{
    LL x=0,f=1; char ch=getchar();
    while(ch<0||ch>9){if(ch==-)f=-1;ch=getchar();}
    while(ch>=0&&ch<=9) x=x*10+ch-0,ch=getchar();
    return x*f;
}
LL Mi(LL a,LL b,int p)
{
    LL res=1;
    while(b)
    {
        if(b&1) res=res*a%p;
        b>>=1;a=a*a%p;
    }return res;
}
LL C(LL n,LL m,int p)
{
    if(m>n)return 0;
    return  f[n]*Mi(f[m]*f[n-m]%p,p-2,p)%p;
}
LL Lus(LL n,LL m,int p)
{
    if(m==0) return 1;
    return (C(n%p,m%p,p)*Lus(n/p,m/p,p))%p;
}
int main()
{
    n=read();f[0]=1;
    for(int i=1;i<=2*n;i++) f[i]=1ll*f[i-1]*i%mod;
    ans=(Lus(2*n,n,mod)-Lus(2*n,n+1,mod)+mod)%mod;
    printf("%lld",ans);
    return 0;
}

 

 

洛谷——P1375 小猫

标签:include   stream   分享   连线   color   ret   opened   closed   质数   

原文地址:http://www.cnblogs.com/z360/p/7896597.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!