循環多少次? |
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) |
Total Submission(s): 37 Accepted Submission(s): 24 |
|
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 |
Author
wangye
|
求組合數啊。編程
1 #include <cmath> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 #include <string> 6 #include <cstdlib> 7 using namespace std; 8 9 const int maxn=2010; 10 int f[maxn][maxn]; 11 int maxnum=2000; 12 13 void close() 14 { 15 exit(0); 16 } 17 18 void work() 19 { 20 for (int i=1;i<=maxnum;i++) 21 { 22 f[i][1]=i % 1007; 23 f[i][0]=1; 24 } 25 for (int i=2;i<=maxnum;i++) 26 for (int j=1;j<=maxnum;j++) 27 f[i][j]=(f[i-1][j-1]+f[i-1][j]) % 1007; 28 } 29 30 void init() 31 { 32 work(); 33 int T,a,b; 34 while(scanf("%d",&T)!=EOF) 35 { 36 while (T--) 37 { 38 scanf("%d %d",&b,&a); 39 printf("%d\n",f[a][b]); 40 } 41 } 42 } 43 44 int main () 45 { 46 init(); 47 close(); 48 return 0; 49 }