#include <iostream>
#include <cmath>
#include <vector>
using namespace std; struct node{
int left;
int right;
};
int main()
{
long N, M;
long long sum = ;
vector<node> nodes;
while (cin >> N >> M)
{
if (N == && M == )
break; for (long n = N; n >=; n--)
{
if ((*M-n*n+n)%(*n)!=)
continue;
long long a1 = ( * M - n*n + n) / ( * n); long long an = a1 + n - ;
if (a1 >= && a1 <= N && an >= && an <= N && a1 <= an)
{
//node node;
//node.left = a1;
//node.right = an;
//nodes.push_back(node);
cout << "[" << a1 << "," << an << "]" << endl;
}
}
//for (node node : nodes)
//{
// cout << "[" << node.left << "," << node.right << "]" << endl;
//}
cout << endl;
}
return ;
}
超时
a1 = k
an = k + n - 1
M = n*(2k+n-1)/2
解的
k = M/n - (n-1)/2,这个不能这样写,要写在一起
(2M-n*n-n)/(2*n)。否则对于30/4 - 3/2,会忽略这样的结果,如果先通分当然也就可以。
超时
利用a1>=1这个条件可以减少循环次数
2M - n*n >n
2M > n*n - n>n*n
sqrt(2M)>n
#include <iostream>
#include <cmath>
#include <vector>
using namespace std; struct node{
int left;
int right;
};
int main()
{
long N, M;
long long sum = ;
vector<node> nodes;
while (cin >> N >> M)
{
if (N == && M == )
break; for (long n = sqrt(*M); n >=; n--)
{
if ((*M-n*n+n)%(*n)!=)
continue;
long a1 = ( * M - n*n + n) / ( * n); long an = a1 + n - ;
if (a1 >= && a1 <= N && an >= && an <= N && a1 <= an)
{
//node node;
//node.left = a1;
//node.right = an;
//nodes.push_back(node);
cout << "[" << a1 << "," << an << "]" << endl;
}
}
//for (node node : nodes)
//{
// cout << "[" << node.left << "," << node.right << "]" << endl;
//}
cout << endl;
}
return ;
}