Grids

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 717    Accepted Submission(s): 296

Problem Description
  度度熊最近很喜欢玩游戏。这一天他在纸上画了一个2行N列的长方形格子。他想把1到2N这些数依次放进去,但是为了使格子看起来优美,他想找到使每行每列都递增的方案。不过画了很久,他发现方案数实在是太多了。度度熊想知道,有多少种放数字的方法能满足上面的条件?
 
Input
  第一行为数据组数T(1<=T<=100000)。
  然后T行,每行为一个数N(1<=N<=1000000)表示长方形的大小。
 
Output
  对于每组数据,输出符合题意的方案数。由于数字可能非常大,你只需要把最后的结果对1000000007取模即可。
 
Sample Input
2
1
3
 
Sample Output
Case #1:
1
Case #2:
5

Hint

对于第二组样例,共5种方案,具体方案为:

 
Source
 思路:卡特兰数+费马小定理求逆元;
为啥是卡特兰数,我们把第一行看成是入栈,第二行看成是出栈,那么观察下1只能放在第一,接下来我们需要放2,2可以放在2个位置,1.放在1的下边,那么此时3就只能放在第一行的第二个,这就表示1出栈了,那么3需要进栈,2.放在第一行的第二个的话,那么3可以放在1的下端,也可以放在第一行的第3个.....
那么我们从中可以知道只要队列不空那么当前的数就可以放在下边,并且是下边没放的第一个,这样后来的数大,就保证了升序合法,同样放上面也是这个原则,所以这个就和火车入栈出栈一样,是卡特兰数的应用。然后递推下卡特兰数,取模时费马小定理求下逆元即可。
 1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<string.h>
5 #include<queue>
6 #include<stack>
7 #include<set>
8 #include<math.h>
9 using namespace std;
10 typedef long long LL;
11 const int N=1e9+7;
12 LL dp[1000005];
13 LL quick(LL n,LL m);
14 int main(void)
15 {
16 int i,j,k;
17 dp[1]=1;
18 dp[2]=2;
19 dp[3]=5;
20 for(i=4; i<=1000000; i++)
21 {
22 dp[i]=dp[i-1]*(4*i-2)%N;
23 dp[i]=dp[i]*quick((LL)(i+1),N-2)%N;
24 }
25 scanf("%d",&k);
26 int s;
27 int t;
28 for(s=1; s<=k; s++)
29 {
30 scanf("%d",&t);
31 printf("Case #%d:\n",s);
32 printf("%lld\n",dp[t]);
33 }
34 return 0;
35 }
36 LL quick(LL n,LL m)
37 {
38 LL ak=1;
39 while(m)
40 {
41 if(m&1)
42 {
43 ak=ak*n%N;
44 }
45 n=(n*n)%N;
46 m/=2;
47 }
48 return ak;
49 }

Grids的更多相关文章

  1. ExtJS笔记 Grids

    参考:http://blog.csdn.net/zhangxin09/article/details/6885175 The Grid Panel is one of the centerpieces ...

  2. hdu 4828 Grids 卡特兰数+逆元

    Grids Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Problem D ...

  3. hdu 4828 Grids(拓展欧几里得+卡特兰数)

    题目链接:hdu 4828 Grids 题目大意:略. 解题思路:将上一行看成是入栈,下一行看成是出栈,那么执着的方案就是卡特兰数,用递推的方式求解. #include <cstdio> ...

  4. A Multipart Series on Grids in ASP.NET MVC

    A Multipart Series on Grids in ASP.NET MVC Displaying a grid of data is one of the most common tasks ...

  5. [HDU 4828] Grids

    Grids Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Subm ...

  6. Cohort Analysis and LifeCycle Grids mixed segmentation with R(转)

    This is the third post about LifeCycle Grids. You can find the first post about the sense of LifeCyc ...

  7. Customer segmentation – LifeCycle Grids, CLV and CAC with R(转)

    We studied a very powerful approach for customer segmentation in the previous post, which is based o ...

  8. Customer segmentation – LifeCycle Grids with R(转)

    I want to share a very powerful approach for customer segmentation in this post. It is based on cust ...

  9. 正六边形网格化(Hexagonal Grids)原理与实现

    在路径规划.游戏设计栅格法应用中,正六边形网格不如矩形网格直接和常见,但是正六边形具有自身的应用特点,更适用于一些特殊场景中,比如旷阔的海洋.区域或者太空.本文主要讲述如何对正六边形进行几何学分析.网 ...

  10. ACM程序设计选修课——1065: Operations on Grids(暴力字符串)

    1065: Operations on Grids Time Limit: 3 Sec  Memory Limit: 128 MB Submit: 17  Solved: 4 [Submit][Sta ...

随机推荐

  1. A Child's History of England.2

    They made boats of basket-work, covered with the skins of animals, but seldom, if ever, ventured far ...

  2. day11 函数

    day11 函数 一.函数基础 """ 1 什么是函数 函数是盛放代码的容器:把实现某一功能的代码放到一个函数内就制造一个工具 2 为何要用函数 没有用函数之前程序的问题 ...

  3. LVM磁盘创建与扩容

    以虚拟机为例 1.在虚拟机上添加新磁盘,点击虚拟机→设置->添加,最后如下图. 2.进入系统fdisk -l,查看当前磁盘信息 [root@master shell]# fdisk -l Dis ...

  4. SpringBoot之HandlerInterceptorAdapter

    SpringBoot之HandlerInterceptorAdapter   在SpringBoot中我们可以使用HandlerInterceptorAdapter这个适配器来实现自己的拦截器.这样就 ...

  5. [Windows编程]模块遍历

    模块遍历 整体思路 1.创建进程快照 2.遍历首次模块 3.继续下次遍历 4.模块信息结构体 相关API的调用 创建进程快照API HANDLE WINAPI CreateToolhelp32Snap ...

  6. Ubuntu下STL源码文件路径+VS2010下查看STL源码

    Ubuntu版本信息 然后STL源码位置就在 /usr/include/c++/7/bits /usr/include/c++/7.4.9/bits 这两个文件下都有 然后我日常写程序用的Window ...

  7. mysql外键策略

    1.外键 建表时添加外键:constraint 外键名 foreign key 从表字段 references 主表字段 级联操作 create table dage( create table xi ...

  8. maven内存溢出解决方法

    maven内存溢出(InvocationTargetException: PermGen space) 解决方案:maven脚本:mvn.bat文件@REM set MAVEN_OPTS=-Xdebu ...

  9. Java poi导出设置 Excel某些单元格不可编辑

    小白的总结,大神勿喷:需要转载请说明出处,如果有什么问题,欢迎留言 一.需求: 1.某一列 .某一行或某些单元格不可编辑,其他列可以编辑 二.期间遇到的问题 1.无法设置成不可编辑 2.设置为不可编辑 ...

  10. java异常处理中throws和throw的使用

    异常介绍: 运行时异常.非运行时异常 在编写可能会抛出异常的方法时,它们都必须声明为有异常. 一.throws关键字 1.声明方法可能抛出的异常: 2.写在方法名后面: 3.可声明抛出多个异常,异常名 ...