HDU1799——循环多少次(组合排列问题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1799

 

 

题目大意是有m层循环操作,有n次op操作,然后问下总共运行了多少次op操作。

 

具体的解题思路是:使用2维数组来储存杨辉三角,然后在每次输入m,n时只需找到相应的数组就行了

代码如下:

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <iostream>
 4 
 5 using namespace std;
 6 
 7 int list[2001][2001];//存放杨辉三角
 8 
 9 void min()
10 {
11     memset(list,0,sizeof(list));
12     for (int i=0;i<=2000;i++)
13     {
14         list[i][0]=1;
15         for (int j=1;j<=i;j++)
16         {
17             list[i][j]=list[i-1][j-1]%1007+list[i-1][j]%1007;//防止数值超过int范围
18         }
19     }
20     return;
21 }
22 
23 
24 int main()
25 {
26     int t;
27     int m,n;
28     min();
29     while (scanf("%d",&t)!=EOF)
30     {
31         for (int i=0;i<t;i++)
32         {
33             scanf("%d%d",&m,&n);
34             printf("%d\n",list[n][m]%1007);//因为已经用memset初始化了,所以对于m>n情况结果会是0
35         }
36         return 0;
37     }
38     return 0;
39 }

 

posted @ 2016-08-02 20:47  shadervio  阅读(166)  评论(0编辑  收藏  举报