HDU 1799

循环多少次?

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


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
 
 1 #include<iostream>

 2 using namespace std;

 3 

 4 int ch[2001][2001];

 5 

 6 int main()

 7 {

 8      int i,j;

 9      int T,m,n;

10      memset(ch,0,sizeof(ch));

11      for(i=1;i<=2000;i++)

12      {

13           ch[i][0]=1;

14           ch[i][1]=i%1007;

15      }

16      for(i=2;i<=2000;i++)

17           for(j=2;j<=i;j++)

18           //利用公式C(n)m=C(n-1)m+C(n-1)(m-1)

19                ch[i][j]=(ch[i-1][j]%1007+ch[i-1][j-1]%1007)%1007;

20      cin>>T;

21      while(T--)

22      {

23           cin>>m>>n;

24           if(m>n)

25           {

26                cout<<0<<endl;

27                continue;

28           }

29           else

30                cout<<ch[n][m]<<endl;

31      }

32      return 0;

33 }

 

你可能感兴趣的:(HDU)