日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

J - The sum problem

發(fā)布時(shí)間:2024/9/5 编程问答 26 如意码农
生活随笔 收集整理的這篇文章主要介紹了 J - The sum problem 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
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.

InputInput contains multiple test cases. each case contains two integers N, M( 1 <= N, M <= 1000000000).input ends with N = M = 0. 
OutputFor 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] 數(shù)學(xué)思維題目,,看了一下大佬的博文。。感覺(jué) 還復(fù)雜?。?!
#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
#include<math.h>
using namespace std;
/*
思路:
區(qū)間項(xiàng)數(shù)為i
起始位置 a,終點(diǎn)為b則b=a+i-1;
這一段區(qū)間的和為sum=(a+b)*i/2=(a+a+i-1)*i/2=m;
a最小為1,則(1+1+i-1)*i/2<=m即(i+1)*i<=2m , i有一定小于sqrt(2m)
即 0<i<sqrt(2m);判斷條件就是 (a+a+i-1)*i==2*m;
*/
int main()
{
int n,m,a,b;
while(cin>>n>>m)
{
if(n== && m==)
break;
for(int i=sqrt(*m);i>=;i--)//最小為1項(xiàng),就是[m,m]
{
a=m/i-(i-)/;//等差公式 即 m=a*i+i*(i-1)/2-----m/i=a+(i-1)/2---a=m/i-(i-1)/2;
if((a+a+i-)*i==*m)
{
printf("[%d,%d]\n",a,a+i-);
}
}
printf("\n");
}
return ;
}

總結(jié)

以上是生活随笔為你收集整理的J - The sum problem的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。