time limit per test2 seconds

memory limit per test256 megabytes

inputstandard input

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

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.

【题目链接】:http://codeforces.com/problemset/problem/599/D

【题解】





其中(n+m)∑i = (n+m)(0+n-1)*n/2;

而∑i^2 = n*(n+1)*(2n+1)/6

所以x=n^2*m-(m+n)(0+n-1)*n/2+n(n+1)*(2n+1)/6

整理一下得

m= (6*x+n^3-n)/(3*n^2+n);

这样我们只要枚举一下n就可以快速得到相应的m了;

而因为随着n的增大m肯定是减少的(直觉告诉我);所以枚举结束的条件就是m< n;

然后把合法的m(能整除的)记录下来;

然后再逆序输出一遍就是n < m的情况了;

(如果出现n==m则要避免第二次再输出一次);

(n==m)的情况最多就出现一次的;

而那个x=n^2*m-(m+n)(0+n-1)*n/2+n(n+1)*(2n+1)/6

可以看到出现了n^2*m;不妨就看成n^3;

而x虽然可以很大开一个立方根里面变成10^6级的了;可以结束!



【完整代码】

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cmath>
  4. #include <set>
  5. #include <map>
  6. #include <iostream>
  7. #include <algorithm>
  8. #include <cstring>
  9. #include <queue>
  10. #include <vector>
  11. #include <stack>
  12. #include <string>
  13. #define lson l,m,rt<<1
  14. #define rson m+1,r,rt<<1|1
  15. #define LL long long
  16. using namespace std;
  17. const LL INF = 1e18;
  18. const int dx[5] = {0,1,-1,0,0};
  19. const int dy[5] = {0,0,0,-1,1};
  20. const double pi = acos(-1.0);
  21. LL ans[700000][2];
  22. LL x,n;
  23. void rel(LL &r)
  24. {
  25. r = 0;
  26. char t = getchar();
  27. while (!isdigit(t) && t!='-') t = getchar();
  28. LL sign = 1;
  29. if (t == '-')sign = -1;
  30. while (!isdigit(t)) t = getchar();
  31. while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
  32. r = r*sign;
  33. }
  34. void rei(int &r)
  35. {
  36. r = 0;
  37. char t = getchar();
  38. while (!isdigit(t)&&t!='-') t = getchar();
  39. int sign = 1;
  40. if (t == '-')sign = -1;
  41. while (!isdigit(t)) t = getchar();
  42. while (isdigit(t)) r = r * 10 + t - '0', t = getchar();
  43. r = r*sign;
  44. }
  45. int main()
  46. {
  47. rel(n);
  48. LL flag =-1,num = 0;
  49. for (LL i = 1;;i++)
  50. {
  51. LL fz = 6*n+i*i*i-i,temp = 3*(i*i+i),key = fz/temp;
  52. if (fz % temp ==0 && i <=key)
  53. {
  54. num++;
  55. if (i==key)
  56. flag = num;
  57. ans[num][0] = i;ans[num][1] = key;
  58. }
  59. if (key < i)
  60. break;
  61. }
  62. if (flag == -1)
  63. {
  64. printf("%d\n",num*2);
  65. for (int i = 1;i <= num;i++)
  66. cout << ans[i][0] << " " << ans[i][1] << endl;
  67. for (int i = num;i>=1;i--)
  68. cout << ans[i][1] << " " << ans[i][0] << endl;
  69. }
  70. else
  71. {
  72. printf("%d\n",num*2-1);
  73. for (int i = 1;i <= num;i++)
  74. cout << ans[i][0] << " " << ans[i][1] << endl;
  75. for (int i = num;i >=1;i--)
  76. if (flag==i)
  77. continue;
  78. else
  79. cout << ans[i][1] << " " << ans[i][0] << endl;
  80. }
  81. return 0;
  82. }

【27.40%】【codeforces 599D】Spongebob and Squares的更多相关文章

  1. Codeforces 599D:Spongebob and Squares

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

  2. 【 BowWow and the Timetable CodeForces - 1204A 】【思维】

    题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...

  3. 【51.27%】【codeforces 604A】Uncowed Forces

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  4. 【27.91%】【codeforces 734E】Anton and Tree

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

  5. 【27.85%】【codeforces 743D】Chloe and pleasant prizes

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

  6. 【47.40%】【codeforces 743B】Chloe and the sequence

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. 【40.17%】【codeforces 569B】Inventory

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  8. 【27.66%】【codeforces 592D】Super M

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

  9. 【42.59%】【codeforces 602A】Two Bases

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

随机推荐

  1. C#DateTime与Unix时间戳的转换

    /// <summary> /// Unix时间戳转为C#格式时间 /// </summary> /// <param name="timeStamp" ...

  2. C语言深度剖析-----数组与指针分析

    数组的本质: 指针的运算: 小标VS指针: a和&a的区别: 例: 数组参数: 所以下例返回4 指针和数组的对比小结:

  3. Asp.NETCore让FromServices回来

    起因 这两天,我忽然有点怀念 Asp.NET MVC 5 之前的时代,原因是我看到项目里面有这么一段代码(其实不止一段,几乎每个 Controller 都是) [Route("home&qu ...

  4. 【数学】概念的理解 —— 有序对(ordered pair)

    有序对 (a,b) 是一组对象,不同的顺序意味着不同的对象,(a,b)≠(b,a) 除非 a=b,正是因为对象的相对顺序是有着不同含义的,因此有时也称之为 2 维向量.与之相对的无序对(unorder ...

  5. 读Effective Objective-C [提高OC代码质量总结笔记第一篇:熟悉OC]

    一.OC特性 OC 为 C 语言添加了面向对象特性,是其超集; OC 使用动态绑定的消息结构,也就是,在运行时才会检查对象类型; 接收一条消息后,究竟应执行何种代码,由运行期环境来决定,而非 编译器; ...

  6. 【心情】"支NMLGB配树”

    大视野oj坏了 那就做杭电呗 看看大触都做杭电里的哪些题 看到杭电的分类了 Tarjan算法诶,我好像会嘛,就是你了 诶,怎么不是求强连通分量? 哦,原来是Tarjan算法的另外一个应用叫做支配树 我 ...

  7. css3-6 表格如何设置样式和定位样式是什么

    css3-6 表格如何设置样式和定位样式是什么 一.总结 一句话总结:css可以解决所有属性设置的样式. 1.表格如何设置样式? css样式可以解决一切问题,没必要在表格上面加属性来设置样式. 7 t ...

  8. GoJS超详细入门(插件使用无非:引包、初始化、配参数(json)、引数据(json)四步)

    GoJS超详细入门(插件使用无非:引包.初始化.配参数(json).引数据(json)四步) 一.总结 一句话总结:插件使用无非:引包.初始化.配参数(json).引数据(json)四步. 1.goj ...

  9. php中的var关键字的用法总结(var在类外用报错)(类属性必须带限定词)

    php中的var关键字的用法总结(var在类外用报错)(类属性必须带限定词) 一.总结 1.var在类外用报错:如果不是在类中,用var定义变量是错的. 2.类属性必须带限定词:php中类属性必须定义 ...

  10. Ajax方法

    json的解析: json是js原生的内容,也就意味着js可以直接取出json对象中的数据. 案例一: var persons = [ {"firstname":"张&q ...