zoukankan      html  css  js  c++  java
  • hdu 2058 The sum problem

    The sum problem

    Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 9078    Accepted Submission(s): 2776


    Problem Description
    Given a sequence 1,2,3,......N, your job is to calculate all the possible sub-sequences that the sum of the sub-sequence is M.
     
    Input
    Input contains multiple test cases. each case contains two integers N, M( 1 <= N, M <= 1000000000).input ends with N = M = 0.
     
    Output
    For each test case, print all the possible sub-sequence that its sum is M.The format is show in the sample below.print a blank line after each test case.
     
    Sample Input
    20 10
    50 30
    0 0
     
    Sample Output
    [1,4]
    [10,10]
    [4,8]
    [6,9]
    [9,11]
    [30,30]
     
    Author
    8600
     
    Source
     
    Recommend
    linle
    //枚举序列长度
    //等差数列呀,开始用队列超时了

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <queue>
    #include <vector>
    #include <cmath>
    #define lson l,m,k<<1
    #define rson m+1,r,k<<1|1
    #define N 20000
    using namespace std;
    int main()
    {
        int n,m;
        int len,a;
        while(scanf("%d%d",&n,&m),n||m)
        {
            len=sqrt(double(2*m))+1;
            while(--len)
            {
               a=m/len-(len-1)/2;
               if((((a<<1)+len-1)*len>>1)==m)
                   a<=n?printf("[%d,%d]\n",a,a+len-1):0;
            }
          printf("\n");
        }
        return 0;
    }

    //下面错误的代码也过了、、呵呵

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <queue>
    #include <vector>
    #include <cmath>
    #define lson l,m,k<<1
    #define rson m+1,r,k<<1|1
    #define N 20000
    using namespace std;
    int main()
    {
        int n,m;
        int len,a;
        while(scanf("%d%d",&n,&m),n||m)
        {
            len=sqrt(double(2*m))+1;
            while(--len)
            {
               a=m/len-(len-1)/2;
               if((((a<<1)+len-1)*len>>1)==m)
                   printf("[%d,%d]\n",a,a+len-1);//这里没判断a要不大于n噢
            }
          printf("\n");
        }
        return 0;
    }

  • 相关阅读:
    arcgis for flex 学习笔记(一)
    也说JS脚本加载控制
    数据验证随想(续)
    Oracle 脚本记录
    探讨:如何更快的赋值取值
    正则表达式技巧
    类似web风格的 Winform 分页控件
    数据验证随想
    [leetcode]Excel Sheet Column Title
    [leetcode]Merge k Sorted Lists
  • 原文地址:https://www.cnblogs.com/372465774y/p/2599898.html
Copyright © 2011-2022 走看看