D. 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 are exactly x distinct squares in the table consisting of n rows and m columns. For example, in a 3 × 5 table there are 15squares 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.

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

来自 <http://codeforces.com/problemset/problem/599/D>

Codeforces Round #332 (Div. 2)

【题意】:

对于给定的X,找出所有的 M*N 矩阵,使得其中恰含 X 个正方形

【解题思路】:

主要是推公式。

对于给定的M N:

S = M*N + (M-1)*(N-1) + …… 直至其中一项变0;

以上公式展开并求和可得:

而右边这几项都可以直接求出,

X = m*n*b - (m+n)*(m-1)*m/2 + (2*m-1)*m*(m-1)/6;(m<n)

可知上述 N 可用X和M来线性表示:

故枚举其中一项,利用公式求另一项即可;

注意还要判重和排序,这里直接用set就可以了。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#include<set>
#define inf 0x3f3f3f3f
#define LL long long
#define maxn 2100000
#define IN freopen("in.txt","r",stdin);
using namespace std; LL sum;
LL a, b;
struct node{
LL a,b;
node(LL _a,LL _b) {a=_a;b=_b;}
bool operator <(const node& c)const{
return a<c.a||a==c.a&&b<c.b;
}
};
set<node> ans; LL check(LL a, LL b)
{
return a*a*b - (a+b)*(a-)*a/ + (*a-)*a*(a-)/;
}
LL cal(LL a)
{
return (*sum-a+a*a*a)/(*a*a+*a);
} int main(int argc, char const *argv[])
{
//IN; while(scanf("%I64d",&sum)!=EOF)
{
ans.clear();
for(a=; a<=maxn; a++) {
b = cal(a); if(check(min(a,b),max(a,b)) != sum) continue; ans.insert(node(a,b));
if(a!=b) ans.insert(node(b,a));
} int cnt = ans.size();
//sort(ans.begin(),ans.end());
printf("%d\n",cnt);
set<node>::iterator it;
for(it=ans.begin();it!=ans.end();it++){
printf("%I64d %I64d\n",(*it).a,(*it).b);
}
} return ;
}

Codeforces 599D Spongebob and Squares(数学)的更多相关文章

  1. codeforces 599D Spongebob and Squares

    很容易得到n × m的方块数是 然后就是个求和的问题了,枚举两者中小的那个n ≤ m. 然后就是转化成a*m + c = x了.a,m≥0,x ≥ c.最坏是n^3 ≤ x,至于中间会不会爆,测下1e ...

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

    D. Spongebob and Squares   Spongebob is already tired trying to reason his weird actions and calcula ...

  3. CF 599D Spongebob and Squares(数学)

    题目链接:http://codeforces.com/problemset/problem/599/D 题意:定义F(n,m)为n行m列的矩阵中方阵的个数,比如3行5列的矩阵,3x3的方阵有3个.2x ...

  4. [cf 599D] Spongebob and Squares

    据题意: $K=\sum\limits_{i=0}^{n-1}(n-i)*(m-i)$ $K=n^2m-(n+m)\sum{i}+\sum{i^2}$ 展开化简 $m=(6k-n+n^3)/(3n^2 ...

  5. Codeforces 599D:Spongebob and Squares

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

  6. Codeforces Round #332 (Div. 2) D. Spongebob and Squares 数学题枚举

    D. Spongebob and Squares Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/ ...

  7. codeforces #332 div 2 D. Spongebob and Squares

    http://codeforces.com/contest/599/problem/D 题意:给出总的方格数x,问有多少种不同尺寸的矩形满足题意,输出方案数和长宽(3,5和5,3算两种) 思路:比赛的 ...

  8. 【27.40%】【codeforces 599D】Spongebob and Squares

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

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

    http://codeforces.com/problemset/problem/599/D 题意:给出一个数x,问你有多少个n*m的网格中有x个正方形,输出n和m的值. 思路: 易得公式为:$\su ...

随机推荐

  1. 在Ubuntu上为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序(老罗学习笔记3)

    简单来说,硬件驱动程序一方面分布在Linux内核中,另一方面分布在用户空间的硬件抽象层中.接着,在Ubuntu上为Android系统编写Linux内核驱动程序(老罗学习笔记1)一文中举例子说明了如何在 ...

  2. IOS键盘收起

    1.点击Return按扭时收起键盘 - (BOOL)textFieldShouldReturn:(UITextField *)textField { return [textField resignF ...

  3. Android Material Design-TabLayout的使用

    TabLayout 位于 android.support.design.widget.TabLayout. 一般与 ViewPager 结合在一起使用.以前有开源库 viewpagerindicato ...

  4. 自动化:Appium运行成功,取得一个小的胜利

    看过乙醇大神的博文,然后又看了一些大神的博文,自己陆陆续续的折腾了一个月,今天上午的时候,appium终于跑起来了.纪念下,在自动化路上取得的一个小胜利 Appium版本:1.2 Python版本:2 ...

  5. 面试题_1_to_16_多线程、并发及线程的基础问题

    多线程.并发及线程的基础问题 1)Java 中能创建 volatile 数组吗?能,Java 中可以创建 volatile 类型数组,不过只是一个指向数组的引用,而不是整个数组.我的意思是,如果改变引 ...

  6. Android 开机动画启动过程详解

    Android 开机会出现3个画面: 1. Linux 系统启动,出现Linux小企鹅画面(reboot)(Android 1.5及以上版本已经取消加载图片): 2. Android平台启动初始化,出 ...

  7. 函数rec_init_offsets

    http://database.51cto.com/art/201303/383042.htm /*************************************************** ...

  8. BZOJ_1624_ [Usaco2008_Open]_Clear_And_Present_Danger_寻宝之路_(最短路_Floyd)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1025 给出\(n\)个点以及之间的边的长度,给出必须访问的点的顺序,求最短路线长度. 分析 用 ...

  9. 解决魅族USB调试无法被电脑识别的问题(含Mac OS X、Win7)

      每次打开豌豆荚或者360手机助手之类手机助手后Eclipse才会检测到mx4(实际上是豌豆荚关闭eclipse的adb使用自己的驱动连接的).解决方法就是在"adb_usb.ini&qu ...

  10. zoj 1967 Fiber Network/poj 2570

    题意就是 给你 n个点 m条边 每条边有些公司支持 问 a点到b点的路径有哪些公司可以支持 这里是一条路径中要每段路上都要有该公司支持 才算合格的一个公司// floyd 加 位运算// 将每个字符当 ...