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

原创 2015年11月21日 09:31:11

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;
}
版权声明:希望各位巨巨多多赐教

相关文章推荐

codeforces 599D. Spongebob and Squares【推公式+暴力枚举】

D. Spongebob and Squares time limit per test 2 seconds memory limit per test 256 megabyt...

Codeforces 599D Spongebob and Squares【思维枚举+数学方程】

D. Spongebob and Squares time limit per test 2 seconds memory limit per test 256 megabytes ...

Codeforces 599 D Spongebob and Squares

传送门: http://codeforces.com/problemset/problem/599/D题意: 求出所有的n和m,使得在n*m的矩形里恰有k个不同的正方形和明显的数学公式题嘛!! ...

CodeForces 599D Spongebob and Squares(DP+math)

Spongebob and Squares Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u...

【Codeforces Round 332 (Div 2)D】【数学 公式推导】Spongebob and Squares 正方形数量恰好为x个的所有大矩形

D. Spongebob and Squares time limit per test 2 seconds memory limit per test 256 megabyt...

Codeforces Round #332 (Div. 2) D. Spongebob and Squares

Spongebob is already tired trying to reason his weird actions and calculations, so he simply asked y...

Codeforces Round #332 (Div. 2) D. Spongebob and Squares详解

Codeforces Round #332 (Div. 2) D. Spongebob and Squares

Coderforce #332 D. Spongebob and Squares

题意 给你x,然后让你找有多少个n*m的矩形,可以由x个相同的多边形组成 题解: 数学题,这道题实际上是问,f(n,m) = sigma(k=1,k=min(n,m))(n-k+1)...

codeforces 599B Spongebob and Joke

B. Spongebob and Joketime limit per test:2 secondsmemory limit per test:256 megabytesinput:standard ...

Codeforces 599 B Spongebob and Joke

传送门: http://codeforces.com/problemset/problem/599/B 题意: 给出序列f[i]和f[a[i]],要求还原序列a[i]不就是记录一下ans[f[i...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)