# cf599D Spongebob and Squares（推公式，枚举）

473人阅读 评论(0)

Spongebob is already tired trying to reason his weird actions and calculations, so he simply asked you to find all pairs of n and m, such that there are exactly x distinct squares in the table consisting of n rows and m columns. For example, in a 3 × 5 table there are 15 squares with side one, 8 squares with side two and 3 squares with side three. The total number of distinct squares in a 3 × 5 table is 15 + 8 + 3 = 26.

Input
The first line of the input contains a single integer x (1 ≤ x ≤ 1018) — the number of squares inside the tables Spongebob is interested in.

Output
First print a single integer k — the number of tables with exactly x distinct squares inside.

Then print k pairs of integers describing the tables. Print the pairs in the order of increasing n, and in case of equality — in the order of increasing m.

Sample test(s)
input
26
output
6
1 26
2 9
3 5
5 3
9 2
26 1
input
2
output
2
1 2
2 1
input
8
output
4
1 8
2 3
3 2
8 1
Note
In a 1 × 2 table there are 2 1 × 1 squares. So, 2 distinct squares in total.

In a 2 × 3 table there are 6 1 × 1 squares and 2 2 × 2 squares. That is equal to 8 squares in total.

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<vector>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1005;
vector<ll>vt;
ll work(ll n)
{
return n*(n + 1)*(2*n + 1)/6;
}
int main()
{
#ifdef LOCAL
freopen("C:\\Users\\ΡΡ\\Desktop\\in.txt","r",stdin);
//freopen("C:\\Users\\ΡΡ\\Desktop\\out.txt","w",stdout);
#endif // LOCAL
ll n;
scanf("%I64d",&n);
ll i = 1;
ll ans = 0;
bool flag = false;
while(work(i) <= n&&i*i <= n)
{
ll temp = work(i);
ll mod = i*(i + 1)/2;
if((n - temp)%mod == 0)
{
vt.push_back(i);
vt.push_back(i + (n - temp)/mod);
ans++;
if(n == temp)flag = true;
}
i++;
}
ans = ans*2;
if(flag)ans--;
printf("%I64d\n",ans);
for(ll i = 0;i < vt.size();i = i + 2)
{
printf("%I64d %I64d\n",vt[i],vt[i + 1]);
}
for(ll i = vt.size() - 1;i >= 0;i = i - 2)
{
if(vt[i] != vt[i - 1])
printf("%I64d %I64d\n",vt[i],vt[i - 1]);
}
return 0;
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：28178次
• 积分：1878
• 等级：
• 排名：千里之外
• 原创：174篇
• 转载：1篇
• 译文：0篇
• 评论：1条
生活的意义在于美好，在于向往目标的力量。
文章分类
最新评论