等差数列The sum problem

针对Thesumproblem问题,本篇介绍了一个高效算法,利用等差数列特性来找出所有可能的子序列,使得这些子序列的和等于给定的目标值M。输入包含多个测试案例,每个案例由两个整数N和M组成,输出为所有可能的子序列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The sum 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]

解题思路:
        一看到N,M的范围就知道不能直接暴力,于是想到等差数列。(a+1,a+2。。。a+n)
即a*n+n(n+1)/2==m,即n*n<m;所以枚举 1-sqrt(2*m);如果a*n+n(n+1)/2==m成立即打印,注意枚举顺序从sqrt(2*m)到1。。。因为这个wa了一次。。。
ac代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    ll n,m,t;
    while(~scanf("%lld%lld",&n,&m)){
        if(m==0&&n==0) break;
        ll t=sqrt(m*2.0);
        for(ll i=t;i>=1;i--){
            if((m-i*(i+1)/2)%i==0){
                ll a=(m-i*(i+1)/2)/i;
                printf("[%lld,%lld]\n",a+1,a+i);
            }
        }
        printf("\n");
    }
    return 0;
}

You and your team have worked tirelessly until you have a sequence a1,a2,…,a2n+1 of positive integers satisfying these properties. 1≤ai≤1018 for all 1≤i≤2n+1 . a1,a2,…,a2n+1 are pairwise distinct. a1=a2−a3+a4−a5+…+a2n−a2n+1 . However, the people you worked with sabotaged you because they wanted to publish this sequence first. They deleted one number from this sequence and shuffled the rest, leaving you with a sequence b1,b2,…,b2n . You have forgotten the sequence a and want to find a way to recover it. If there are many possible sequences, you can output any of them. It can be proven under the constraints of the problem that at least one sequence a exists.    Input Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104 ). The description of the test cases follows. The first line of each test case contains one integer n (1≤n≤2⋅105 ). The second line of each test case contains 2n distinct integers b1,b2,…,b2n (1≤bi≤109 ), denoting the sequence b . It is guaranteed that the sum of n over all test cases does not exceed 2⋅105 .    Output For each test case, output 2n+1 distinct integers, denoting the sequence a (1≤ai≤1018 ). If there are multiple possible sequences, you can output any of them. The sequence a should satisfy the given conditions, and it should be possible to obtain b after deleting one element from a and shuffling the remaining elements. Example InputCopy 4 1 9 2 2 8 6 1 4 3 99 2 86 33 14 77 2 1 6 3 2 OutputCopy 7 9 2 1 8 4 6 9 86 99 2 77 69 14 33 4 6 1 2 3
最新发布
03-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值