循環多少次?(1799)

我們知道,在編程中,我們時常需要考慮到時間複雜度,特別是對於循環的部分。例如, 
如果代碼中出現 
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

 

注:題意就是組合數的概念,然後組合數利用楊輝三角求。 

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <stack>
#include <set>
#include <map>
#include <string>
using namespace std;
#define pi acos(-1,0)
#define INF 2147483647
int max(int a,int b)
{
	return a>=b?a:b;
}
int s[2001][2001]; 
int main()
{
	int t,n,m,i,j;  
    memset(s,0,sizeof(s));  
    for(i=1;i<2001;i++)  
    s[i][0]=1;  
    
    s[1][1]=1;  
    for(i=2;i<2001;i++)  
    {
    	for(j=1;j<=i;j++)  
    	s[i][j]=(s[i-1][j-1]+s[i-1][j])%1007; 
    }    	
    while(scanf("%d",&t)!=EOF)
	{
		 while (t--)   
	    {  
	        scanf("%d %d",&m,&n);  
	        printf("%d\n",s[n][m]);  
	    } 
	} 
	
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章