关闭

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

标签: codeforces数学
353人阅读 评论(0) 收藏 举报
分类:

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

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 is15 + 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.

Examples
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.




把③化简得到 :6*x = 6*n*n*m - 3*(n+m)*n*(n-1) + n*(n-1)*(2*n-1)

进而有 (3*n*n+3*n)*m = 6*x + n*n*n - n

枚举n求m即可

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
typedef long long ll;
using namespace std;
struct Node{
    ll nn,mm;
};
vector<Node>ans;
ll high=5000000;
int main()
{
    ll x;
    scanf("%I64d",&x);
    for(ll n=1;n<=high;n++){  //枚举n
        ll a=6*x+n*n*n-n;
        ll b=3*n*n+3*n;
        if(a%b!=0)continue;
        ll m=a/b;
        if(n>m)break;         //枚举到n<=m即可,因为在n>m时的结果等于n<m所有结果(n,m)的交换,即(m,n)
        Node now;
        now.nn=n;
        now.mm=m;
        ans.push_back(now);
    }
    int top=ans.size()-1;
    //如果最末答案为(n,n),则总数为2*(n<=m的对数)-1,否则总数为2*(n<=m的对数)
    if(ans[top].nn==ans[top].mm) top=ans.size()*2-1;
    else top=ans.size()*2;
    printf("%d\n",top);
    int i;
    top=ans.size()-1;
    for(i=0;i<=top;i++){
        printf("%I64d %I64d\n",ans[i].nn,ans[i].mm);
    }
    if(ans[top].nn==ans[top].mm)i=top-1;
    else  i=top;
    for(;i>=0;i--){
        printf("%I64d %I64d\n",ans[i].mm,ans[i].nn);
    }
    return 0;
}






1
0
查看评论

Codeforces #332 D. Spongebob and Squares (公式)

题意: n∗m矩形网格,给定x≤1018种正方形可能,问有多少种n∗m方案,按照字典序输出n*m矩形网格, 给定x\leq 10^{18}种正方形可能, 问有多少种n*m方案, 按照字典序输出 分析: 我们发现n∗m的矩形网格有正方形可能是:我们发现n*m的矩形网格有正方形可能是: 假...
  • lwt36
  • lwt36
  • 2015-11-21 03:30
  • 228

Codeforces 599D:Spongebob and Squares

D. Spongebob and Squares time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Spon...
  • u010885899
  • u010885899
  • 2015-11-22 21:08
  • 772

【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 megabytes input standard input output standar...
  • snowy_smile
  • snowy_smile
  • 2015-12-02 12:14
  • 500

Codeforces 599 D Spongebob and Squares

传送门: http://codeforces.com/problemset/problem/599/D题意: 求出所有的n和m,使得在n*m的矩形里恰有k个不同的正方形和明显的数学公式题嘛!! n*m的矩形中的不同正方形的数量必定可以用公式去表示出来,必定是枚举n,然后可以在O(1)的时间内把...
  • yp_2013
  • yp_2013
  • 2016-01-16 23:58
  • 689

Codeforces 599D Spongebob and Squares(枚举 + 数学)

@(K ACMer)题意:给你一个数k,(k<1e18),表示矩形不同的正方形的个数,求满足k的所有矩形.分析:求出公式,枚举实现即可. 这个题,比赛的时候错误的认知了复杂度,以为1e18必须要logn的算法才行,其实开次3根号也好的.#include <iostream> #i...
  • jibancanyang
  • jibancanyang
  • 2015-11-21 18:56
  • 437

Codeforces 599D Spongebob and Squares 【公式推导 暴力】

D. Spongebob and Squares time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Spon...
  • chenzhenyu123456
  • chenzhenyu123456
  • 2015-12-05 21:38
  • 345

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

D. Spongebob and Squares time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Spon...
  • GKHack
  • GKHack
  • 2015-11-23 16:17
  • 958

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

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 a...
  • Techay
  • Techay
  • 2015-11-21 09:31
  • 520

Codeforces 898E - Squares and not squares 【优先队列贪心】

Codeforces 898E - Squares and not squares 【优先队列贪心】 Ann and Boryahave n piles with candies and n is even number. There are ai candies in pile with nu...
  • my_sunshine26
  • my_sunshine26
  • 2017-12-17 00:32
  • 191

CodeForces 599D Spongebob and Squares(DP+math)

Spongebob and Squares Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u SubmitStatusPracticeCodeForces 599D Descrip...
  • zyd8888102
  • zyd8888102
  • 2016-01-23 11:51
  • 244
    个人资料
    • 访问:46144次
    • 积分:1700
    • 等级:
    • 排名:千里之外
    • 原创:128篇
    • 转载:3篇
    • 译文:0篇
    • 评论:5条
    最新评论