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

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6286    Accepted Submission(s): 2411


 

Problem Description

  我们知道,在编程中,我们时常需要考虑到时间复杂度,特别是对于循环的部分。例如,
如果代码中出现
for(i=1;i<=n;i++) OP ;
那么做了n次OP运算,如果代码中出现
fori=1;i<=n; i++)
  for(j=i+1;j<=n; j++) OP;
那么做了n*(n-1)/2 次OP 操作。
现在给你已知有m层for循环操作,且每次for中变量的起始值是上一个变量的起始值+1(第一个变量的起始值是1),终止值都是一个输入的n,问最后OP有总共多少计算量。

 

 

Input

  有T组case,T<=10000。每个case有两个整数m和n,0<m<=2000,0<n<=2000.

 

 

Output

  对于每个case,输出一个值,表示总的计算量,也许这个数字很大,那么你只需要输出除1007留下的余数即可。

Sample input

2
1 3
2 3

Sample output

3
3

分析:杨辉三角的一个应用!看了学长的图片!

hdu 1799 循环多少次? 【打表+找规律】

刚开始程序炸了一次:(贴上炸了的程序)

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxn = 2000+10;
const int mod = 1007;
int dp[maxn][maxn];
void solve()
{
    memset(dp,0,sizeof dp);
    for(int i=0;i<maxn;i++)
        dp[i][1] = i%mod;
    for(int i=2;i<=maxn;i++)
    {
        for(int j=2;j<maxn;j++)
        {
            dp[i][j] = (dp[i-1][j]%mod + dp[i-1][j-1]%mod)%mod;
        }
    }
}
int main()
{
    int t,n,m;
    scanf("%d",&t);
    solve();
    while(t--)
    {
        scanf("%d %d",&n,&m);
        printf("%d\n",dp[m][n]);
    }
    return 0;
}

最后贴上我的AC代码:

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxn = 2000+10;
const int mod = 1007;
int dp[maxn][maxn];
void solve()
{
    memset(dp,0,sizeof dp);
    for(int i=0;i<maxn;i++)
        dp[i][1] = i%mod;
    for(int i=2;i<maxn;i++)  //这是与上述程序不同的地方所在,请认真思考为啥
    {
        for(int j=2;j<maxn;j++)
        {
            dp[i][j] = (dp[i-1][j]%mod + dp[i-1][j-1]%mod)%mod;
        }
    }
}
int main()
{
    int t,n,m;
    scanf("%d",&t);
    solve();
    while(t--)
    {
        scanf("%d %d",&n,&m);
        printf("%d\n",dp[m][n]);
    }
    return 0;
}

 

相关文章:

  • 2020-09-28
  • 2021-06-13
  • 2021-08-06
  • 2019-03-25
  • 2021-07-22
  • 2021-09-20
  • 2019-05-30
猜你喜欢
  • 2021-12-26
  • 2021-07-07
  • 2019-08-26
  • 2021-06-29
  • 2021-11-25
  • 2021-09-30
  • 2021-04-02
相关资源
相似解决方案