zoukankan      html  css  js  c++  java
  • 7.3.7 循环多少次?

    循环多少次?

    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
    ",f[a][b]);
    40         }
    41     }
    42 }
    43 
    44 int main ()
    45 {
    46     init();
    47     close();
    48     return 0;
    49 }
  • 相关阅读:
    网易企业免费邮箱
    168. Excel Sheet Column Title
    167.Two Sum II-Input array is sorted
    166. Fraction to Recurring Decimal
    165 Compare Version Numbers
    164. Maximum Gap
    163.Missing Ranges
    162.Find Peak Element
    161.One Edit Distance
    160. Intersection of Two Linked Lists
  • 原文地址:https://www.cnblogs.com/cssystem/p/3212364.html
Copyright © 2011-2022 走看看