zoukankan      html  css  js  c++  java
  • 【HNOI模拟By YMD】move

    Description

    设P(n)为从(0,0)移动到点(n,0)的不同路径数目,移动的方式有以下三种:(x,y)->(x+1,y-1),(x,y)->(x+1,y),(x+y)->(x+1,y+1),并且路径不能和第四象限有交集。求P(n),并对10^9+7取模。

    Input

    第一行一个整数T,表示数据组数。

    对于每组数据,一行一个整数n。

    Output

    对于每组数据,输出答案。

    Data Range

    20%:n≤10;
    50%:n≤10000;
    100%:n≤106,T≤10。

     

    Solution

    20%  

    直接O(3^n*n)的暴力枚举即可。

    50%

    考虑第一次直线y=0的点,假设是(i,0)。

    那么从(1,1)到(i-1,1)之间的路径均在直线y=1的上方,显然有P(i-2)种。同理,从(x,0)到(n,0)之间的路径均在直线y=0的上方,有P(n-i)种。

    所以,全部合起来可以得到P(n)=Σni=1 P(i-2)P(n-i),其中,规定P(-1)=P(0)=1。

    其实可以在20%基础上打个表,考场有超过5人这样写。。。

    100%

    假设移动的路径中一共有i个(x,y)->(x+1,y+1),那么就一定会有i个(x,y)->(x+1,y-1),n-2i个(x,y)->(x+1,y)。

    而如果不考虑所有的(x,y)->(x+1,y),那么路径的种数就是第i个Catalan数,设为C_i。如果加入(x,y)->(x+1,y),也就是在2i+1个空处中插入n-2i相同的球,方案数是C2in

    所以,P(n)=Σni=1 C_i C2in

     1 #include<algorithm>
     2 #include<iostream>
     3 #include<cstdlib>
     4 #include<cstring>
     5 #include<cstdio>
     6 #include<cmath>
     7 using namespace std;
     8 
     9 typedef long long LL;
    10 
    11 #define N 1000010
    12 #define MOD 1000000007
    13 
    14 int t;
    15 int n;
    16 
    17 LL f[N],d[N];
    18 
    19 inline LL qpow(LL a,LL b)
    20 {
    21     LL ans=1;
    22     while (b)
    23     {
    24         if (b&1)
    25             ans=(1LL*ans*a)%MOD;
    26         b>>=1;
    27         a=(1LL*a*a)%MOD;
    28     }
    29     return ans;
    30 }
    31 
    32 inline LL C(LL x,LL y)
    33 {
    34     return f[x]*d[y]%MOD*d[x-y]%MOD;
    35 }
    36 
    37 inline LL Catalan(LL x)
    38 {
    39     return (C(x<<1,x)-C(x<<1,x-1)+MOD)%MOD;
    40 }
    41 
    42 int main()
    43 {
    44     freopen("move.in","r",stdin);freopen("move.out","w",stdout);
    45     scanf("%d",&t);
    46     f[0]=1;
    47     for (int i=1;i<=1000000;i++)
    48         f[i]=f[i-1]*i%MOD;
    49     for (int i=0;i<=1000000;i++)
    50         d[i]=qpow(f[i],MOD-2)%MOD;
    51     while (t--)
    52     {
    53         scanf("%d",&n);
    54         LL ans=0;
    55         for (int i=0;i<=n;i+=2)
    56             ans+=C(n,i)*Catalan(i>>1),ans%=MOD;
    57         printf("%lld
    ",ans);
    58     }
    59     return 0;
    60 }
    View Code
  • 相关阅读:
    saltstack学习
    linux命令学习(2):wc 命令
    linux命令学习(1):grep 命令
    Docker Compose—简化复杂容器应用的利器
    Docker-compose命令详解
    Python json数据写入csv json excel文件
    Centos 不重启 修改ulimit参数
    pip包管理工具 基本使用
    python 使用xpath解析含有命名空间(xmlns)的xml
    pyqt5 + pyinstaller 制作爬虫小程序
  • 原文地址:https://www.cnblogs.com/yangjiyuan/p/5350633.html
Copyright © 2011-2022 走看看