首页 > 其他分享 >hdoj The sum problem 2058 (数学等差公式&技巧转换)

hdoj The sum problem 2058 (数学等差公式&技巧转换)

时间:2023-04-19 15:34:25浏览次数:55  
标签:case 2058 sum long each problem include define

The sum problem Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 21416    Accepted Submission(s): 6287


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、此题是公差为1的等差数列; 3、此题的n,m很大(不可能模拟所有的起点和终点); 技巧转换: 只模拟起点和数列的长度,知道起点i和数列长度j,很容易根据等差数列公式求得这个子序列的和:(i+(i+j-1))*j/2; 化简公式得: j*j+(2*i-1)=2*m;因为i>=1,m已知,所以j的取值为:j<=sqrt(2*m); 所以知道这个规律就可以写这个题了。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<map>
#include<math.h>
#define INF 0x3f3f3f3f
#define ull unsigned long long
#define ll long long
#define IN __int64
#define N 10010
#define M 1000000007
using namespace std;
int main()
{
	int n,m,i,j;
	while(scanf("%d%d",&n,&m),n|m)
	{
		for(j=sqrt(2*m);j>0;j--)
		{
			i=(2*m/j-j+1)/2;
			if(j*(j+2*i-1)/2==m)
				printf("[%d,%d]\n",i,i+j-1);
		}
		printf("\n");
	}
	return 0;
}

 

标签:case,2058,sum,long,each,problem,include,define
From: https://blog.51cto.com/u_16079508/6206444

相关文章

  • CodeForces - 616E Sum of Remainders (数论)大数取余求和 好题
    CodeForces-616ESumofRemaindersTimeLimit: 2000MS MemoryLimit: 262144KB 64bitIOFormat: %I64d&%I64uSubmit StatusDescriptionCalculatethevalueofthesum: nmod1 + nmod2 + nmod3 +...+ nmodm.Astheresultcanbeve......
  • Topcoder 10880 - Rabbit Problemming
    \[兔子,兔子,兔子\]首先,我们考虑一只兔子可以达到的最大值\(mx_i\)和最小值\(mn_i\),这个可以很方便的求出来。并且每只兔子的取值是独立的。然后,如果一个组合能被选中,那么在这个组合内部所有的兔子都取\(mx_i\),其他的兔子都取\(mn_i\)的时候一定也能被选中。我们就钦定所有......
  • Sumsets UVA - 10125
     一个集合中需要找到a,b,c,d,使得a+b+c=d 枚举a,b,计算a+b,存在map里再枚举d,c,计算d-c是否存在d-c==a+b #include<iostream>#include<map>#include<algorithm>#include<vector>usingnamespacestd;constintN=1e3+2;//a+b==d-c#definepiipair<......
  • HDU 5443 The Water Problem RMQ
    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5443题意:给定一个数组,查询区间最大值思路:RMQ模板题#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>usingnamespacestd;constint......
  • POJ 3468 A Simple Problem with Integers(线段树区间更新)
    题目地址:POJ3468打了个篮球回来果然神经有点冲动。。无脑的狂交了8次WA。。居然是更新的时候把r-l写成了l-r。。。这题就是区间更新裸题。区间更新就是加一个lazy标记,延迟标记,只有向下查询的时候才将lazy标记向下更新。其他的均按线段树的来就行。代码如下:#include<iostream>#in......
  • HNU2019 Summer Training 3 E. Blurred Pictures
    E.BlurredPicturestimelimitpertest2secondsmemorylimitpertest256megabytesinputstandardinputoutputstandardoutputDamonlovestotakephotosoftheplaceshevisitsduringhistravels,toputthemintoframes.Allofhisphotosarei......
  • percona-toolkit工具:使用pt-table-checksum检查MySQL主从库的差异
    环境介绍CentOS7.6MySQL5.7PerconaToolkit3.4.0 下载并安装PerconaToolkit从WEB端下载https://www.percona.com/downloads或者通过wget下载[root]#wgethttps://downloads.percona.com/downloads/percona-toolkit/3.5.2/binary/redhat/7/x86_64/percona-toolkit-3.......
  • UVa 112 Tree Summing (scanf()去空格&递归&二叉树遍历)
    112-TreeSummingTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=48BackgroundLISPwasoneoftheearliesthigh-levelprogramminglanguagesand,withFORTRAN,isoneoft......
  • UVa 10783 Odd Sum (water ver.)
    10783-OddSumTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=19&page=show_problem&problem=1724Givenarange [a, b],youaretofindthesummationofalltheoddintegersinthisran......
  • UVa 524 Prime Ring Problem (数论&DFS)
    524-PrimeRingProblemTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=465Aringiscomposedofn(evennumber)circlesasshownindiagram.Putnaturalnumbers  intoea......