您的位置:首页 > 其它

cf599D Spongebob and Squares(推公式,枚举)

2015-11-21 09:31 381 查看
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.



就是一个n*m的矩形,里面有多少个不同的正方形。现在给出不同 正方形的个数n,让你求所有满足不同正方形个数为n的矩形长和宽。

推公式,推一会就出来了。首先是当n大于m是,n每加一,正方形数量只会增加m + m - 1 + m - 2 + …… + 1 = m*(m + 1)/2;

就是f(n + 1,m) = f(n,m) + m*(m + 1)/2;

然后矩形又是对称的,就是n和m是可以互换的(如果m大于n,还是等价于上面那个式子,互换就可以了)

接下来就是搞f(n,n)的值,当时列了一下递推式,求和太麻烦,然后我就看看1,2,3,4的规律,发现是平方和,验证了一下是对的。

那么这题就好搞了,首先确定m(f(m,m) <= x),然后判断x - f(m,m)的差值是否可以被m*(m + 1)/2整除,如果可以,这个就是答案,然后就是枚举了,根据公式,应该是1e6的复杂度。

枚举之后个数乘以2,输出长宽,倒着再输一次,因为答案的矩形都是对称的。

还有一点注意就是如果f(m,m)恰好是x的话,这个本身就是一个正方形,那么最后答案是乘以2之后还要-1。

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: