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

题意是给定一个X,问那些矩形中含有的正方形总数等于X。

这题当时没时间做了,(太弱。。。)后面补的。

官方题解:

第一点:n*m里面的正方形数量就是sum((n-i)*(m-i)),i从1到n-1啊。。。在纸上画几次就明白了。

第二点:从1到n的平方和等于n(n+1)(2n+1)/6。。。

然后就是枚举n,求m。

代码:

#pragma warning(disable:4996)
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
using namespace std;
typedef long long ll; const int maxn = 2000005;
ll x;
ll a[maxn];
ll b[maxn]; int main()
{
//freopen("i.txt", "r", stdin);
//freopen("o.txt", "w", stdout); int flag;
ll i, len, num, n, m, temp;
cin >> x; flag = -1;
num = 0;
len = 2 * pow((double)x, ((double)1 / (double)3));
for (i = 1; i <= len+1; i++)
{
temp = 6 * x + i*i*i - i;
n = i*i + i; if ((temp % (3 * n) == 0) && (i <= temp / (3 * n)))
{
a[num] = i;
b[num] = temp / (3 * n); if (a[num] == b[num])
{
flag = num;
}
num++;
}
}
if (flag == -1)
{
cout << num * 2 << endl;
for (i = 0; i < num; i++)
{
cout << a[i] << " " << b[i] << endl;
}
for (i = num-1; i >= 0; i--)
{
cout << b[i] << " " << a[i] << endl;
}
}
else
{
cout << num * 2 - 1 << endl;
for (i = 0; i < num; i++)
{
cout << a[i] << " " << b[i] << endl;
}
for (i = num - 1; i >= 0; i--)
{
if (flag == i)
continue;
cout << b[i] << " " << a[i] << endl;
}
}
//system("pause");
return 0;
}
												

Codeforces 599D:Spongebob and Squares的更多相关文章

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

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

  2. Codeforces 599D Spongebob and Squares(数学)

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

  3. 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/ ...

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

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

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

  6. codeforces 599D Spongebob and Squares

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

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

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

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

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

  9. [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 ...

随机推荐

  1. 获取目标字符串在字符串中第N次出现的位置

    /** * 获取目标字符串在字符串中第N次出现的位置 * @file name * @author xiehongwei * @date 2017-8-2 下午3:29:09 * @param sou ...

  2. 单例模式的Java泛型实现方式

    import java.util.HashMap; import java.util.Map; /** * Created by zhao.wu on 2016/11/18. */ public cl ...

  3. js克隆一个对象

    我们知道,对象类型在赋值的过程中其实是复制了地址,所以如果改变了一方,其他都会被改变.我们应该如何克隆一个对象,并且避免这种现象的发生呢? 方法一:Object.assign function cop ...

  4. JAVA基础学习(5)之数组

    5数组 5.1数组 5.1.1初识数组 // 输出大于平均数的所有数 Scanner in = new Scanner(System.in); int n; int[] a = new int[100 ...

  5. Spring Boot 文件上传简易教程

    上传文件是我们日常使用最为广泛的功能之一,比如App端上传头像.本章演示如何从客户端上传到 Spring Boot 开发的 Api中. 本项目源码 github 下载 1 新建 Spring Boot ...

  6. [QT] QT5.12 HTTPS请求 TLS initialization failed

    #前言 接触到了Qt的网络编程 然后尝试对一个http页面请求获取源码 是可以的 但是当对https界面发出请求的时候总是错误 TLC什么的初始化失败 百度也是没有结果 然后网上各种方法 比如说编译O ...

  7. OO Byebye

    一.架构设计 1.第一次作业 首先做的就是把所有的Element全部存起来,我把UmlClass和UmlInterface重新用两个新的类来记录了一下,用于更快地找到他们的关联.其实总体思路还是比较简 ...

  8. python练习:编写一个程序,要求用户输入一个整数,然后输出两个整数root和pwr,满足0<pwr<6,并且root**pwr等于用户输入的整数。如果不存在这样一对整数,则输入一条消息进行说明。

    python练习:编写一个程序,要求用户输入一个整数,然后输出两个整数root和pwr,满足0<pwr<6,并且root**pwr等于用户输入的整数.如果不存在这样一对整数,则输入一条消息 ...

  9. ASP.NET Core搭建多层网站架构【3-xUnit单元测试之简单方法测试】

    2020/01/28, ASP.NET Core 3.1, VS2019, xUnit 2.4.0 摘要:基于ASP.NET Core 3.1 WebApi搭建后端多层网站架构[3-xUnit单元测试 ...

  10. L298N模块的使用(文末有惊喜)

    这个模块有两个供电口,标示着“12V输入”的是功率驱动电源输入,供电范围可以是7-46V, 一般12V供电就能满足我们大部分的DIY需求.标示着“5V输出可不接”的是逻辑供电, 当我们将“板载5V输出 ...