Brackets解题报告
2013-07-10 20:37
127 查看
题目摘要:This year MK is 5 years old. So hedecides to learn somearithmetic
But he wasconfused by how to write the brackets. He has already known that the bracketsshould match when writing them correctly. Such as “()(())” is correct but “())(”is not.
The problem is that, if there are N pairsof brackets, how many ways that MK can write them correctly?
题目大意:给出N对括号,输出正确的组合方式数
输入输出要求:
Input
There are several test cases. Each casecontains a number N (1 <= N <= 1000) indicating the pairs of brackets.
Output
For each case, please output the answer mod1,000,000,007.
输入输出样例:
Sample Input :
5
7
Sample Output :
42
429
解题思路:结果就是一组卡特兰数,用公式输出结果就行,由于测试的数据很大,所以边乘边模。
代码:
#include<iostream>
using namespace std;
const int maxn=1000+5;
const int MOD=1000000007;
int n;
long long f[maxn];
int main()
{
f[0]=1;
for(int i=1;i<=1000;i++)
{
for(int j=0; j<i; j++)
{
f[i]+=f[j]*f[i-j-1]%MOD;
f[i]%=MOD;
}
}
while(cin>>n)
{
cout<<f
<<endl;
}
return 0;
}
解题感想:开始用递归的公式做的,数据一大就递不出结果了,感觉不会再爱递归了……
But he wasconfused by how to write the brackets. He has already known that the bracketsshould match when writing them correctly. Such as “()(())” is correct but “())(”is not.
The problem is that, if there are N pairsof brackets, how many ways that MK can write them correctly?
题目大意:给出N对括号,输出正确的组合方式数
输入输出要求:
Input
There are several test cases. Each casecontains a number N (1 <= N <= 1000) indicating the pairs of brackets.
Output
For each case, please output the answer mod1,000,000,007.
输入输出样例:
Sample Input :
5
7
Sample Output :
42
429
解题思路:结果就是一组卡特兰数,用公式输出结果就行,由于测试的数据很大,所以边乘边模。
代码:
#include<iostream>
using namespace std;
const int maxn=1000+5;
const int MOD=1000000007;
int n;
long long f[maxn];
int main()
{
f[0]=1;
for(int i=1;i<=1000;i++)
{
for(int j=0; j<i; j++)
{
f[i]+=f[j]*f[i-j-1]%MOD;
f[i]%=MOD;
}
}
while(cin>>n)
{
cout<<f
<<endl;
}
return 0;
}
解题感想:开始用递归的公式做的,数据一大就递不出结果了,感觉不会再爱递归了……
相关文章推荐
- Winter-2-STL-B Brackets 解题报告及测试数据
- Sicily 1543. Completing Brackets 解题报告
- ZOJ 2104解题报告
- POJ2456(复赛模拟试题) 好斗的奶牛 解题报告
- 【LeetCode】Pascal's Triangle & II 解题报告
- 线段树 POJ2777 Count Color 解题报告
- 0729模拟赛解题报告
- Moscow Subregional of NEERC, ACM ICPC 2016-2017 解题报告
- [Leetcode] 484. Find Permutation 解题报告
- POJ1001 Exponentiation (解题报告)
- Codeforces Round #185 (Div. 2) A题解题报告
- 2016.8.1测试解题报告(maxtrix,sum,salesman)
- JOJ 1064: Caeser Comes Back 解题报告
- LeetCode: Word Break II 解题报告
- POJ1005解题报告
- PKU1061 解题报告 青蛙的约会 __用扩展欧几里得解模同余方程
- ZOJ 3721 Final Exam Arrangement 解题报告
- USACO Hidden Password 解题报告
- [Leetcode] 495. Teemo Attacking 解题报告
- 【LeetCode】Longest Palindromic Substring 解题报告