大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE稳定放心使用
描述
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 contains multiple test cases. each case contains two integers N, M( 1 <= N, M <= 1000000000).input ends with N = M = 0.
输出
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.
样例输入
20 10
50 30
0 0
样例输出
[1,4]
[10,10]
[4,8]
[6,9]
[9,11]
[30,30]
思路
等差数列求和的变形。
\(S=\frac{(k+k+n-1)*n}{2}\)
\(2k=\frac{2s}{n}-n+1, n≤\sqrt{S}\)
枚举n然后算出k就好
代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
ll n, m;
while(~scanf("%lld %lld", &n, &m))
{
if(n + m == 0) break;
ll k, t;
for(t = sqrt(2 * m); t > 0; t--)
{
if(2 * m % t) continue;
ll r = 2 * m / t - t + 1;
if(r % 2) continue;
k = r >> 1;
printf("[%lld,%lld]\n", k, k + t - 1);
}
printf("\n");
}
return 0;
}
转载于:https://www.cnblogs.com/HackHarry/p/8371005.html
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/186755.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...