zoukankan      html  css  js  c++  java
  • A

    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


    分析:本题读懂题意很关键,题目的实质就是排列组合问题,多推几组数据找规律,c(m)n=(c(m-1)n-1+c(m)n-1)*(n-1),这个问题想了很久还没有完全弄懂,注意超时问题
    心得:考虑问题要全面不要落下一些特别情况,要不等你写完代码后再找就会麻烦很多
    AC代码:

    #include <iostream>;
    #include<cstring>;
    #define N 2000
    using namespace std;
    int a[N+10][N+10];
    void fun()//打表
    {
    int i,j;
    memset(a,0,sizeof(a));
    for(i=2;i<=N;i++)
    a[i][1]=0;
    for(j=1;j<=N;j++)
    a[1][j]=j%1007;
    for(i=2;i<=N;i++)
    for(j=2;j<=N;j++)
    a[i][j]=(a[i][j-1]+a[i-1][j-1])%1007;
    }
    int main()
    {
    int m,n,t;
    cin>>t;
    fun();//函数被我写到循环里面去了,造成超时
    while(t--)
    {
    cin>>m>>n;
    cout<<a[m][n]<<endl;
    }

    return 0;
    }





  • 相关阅读:
    poj 3616 Milking Time
    poj 3176 Cow Bowling
    poj 2229 Sumsets
    poj 2385 Apple Catching
    poj 3280 Cheapest Palindrome
    hdu 1530 Maximum Clique
    hdu 1102 Constructing Roads
    codeforces 592B The Monster and the Squirrel
    CDOJ 1221 Ancient Go
    hdu 1151 Air Raid(二分图最小路径覆盖)
  • 原文地址:https://www.cnblogs.com/lbyj/p/5727273.html
Copyright © 2011-2022 走看看