zoukankan      html  css  js  c++  java
  • HDU_2058——等差数列,子集,集合长度判断

    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]
     1 /*
     2 设j为子串的长度,i为子串的第一个数 
     3 m=[j*(i+i+(j-1)*1)]/2
     4 2*m/j-j=i+i-1----2*i=2*m/j-j+1
     5 因为i+i-1>0
     6 所以2*m>j*j----j<sqrt(2m)
     7 */
     8 #include <cstdio>
     9 #include <cmath>
    10 int main()
    11 {
    12    int n,m,i,j;
    13    while(~scanf("%d%d",&n,&m),(n||m))
    14       {
    15          for(j=(int)sqrt(2*(double)m);j>=1;j--)//枚举长度 
    16             {
    17                i=(2*m/j-j+1)/2;//由长度获得的子串第一个元素值
    18                if((j*(i+i+j-1))/2 == m)//等差数列前n项的和,还有通项公式
    19                   printf("[%d,%d]
    ",i,i+j-1);
    20             }
    21          printf("
    ");
    22       }
    23    return 0;   
    24 }
    ——现在的努力是为了小时候吹过的牛B!!
  • 相关阅读:
    FreeSql 教程引导
    Day3-JS-JavaScript 函数专题
    Day2-JS-JSON
    Day2-JS-let和const
    Day2-JS-this 关键字
    Day2-JS-JavaScript 验证 API
    Day2-JS-表单
    Day2-JS-严格模式
    Day2-JS-JavaScript 错误
    Day2-JS-正则表达式
  • 原文地址:https://www.cnblogs.com/pingge/p/3185143.html
Copyright © 2011-2022 走看看