Codeforces 599D:Spongebob and Squares
2 seconds
256 megabytes
standard input
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.
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.
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.
26
6
1 26
2 9
3 5
5 3
9 2
26 1
2
2
1 2
2 1
8
4
1 8
2 3
3 2
8 1
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的更多相关文章
- 【27.40%】【codeforces 599D】Spongebob and Squares
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Codeforces 599D Spongebob and Squares(数学)
D. Spongebob and Squares Spongebob is already tired trying to reason his weird actions and calculati ...
- 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/ ...
- codeforces #332 div 2 D. Spongebob and Squares
http://codeforces.com/contest/599/problem/D 题意:给出总的方格数x,问有多少种不同尺寸的矩形满足题意,输出方案数和长宽(3,5和5,3算两种) 思路:比赛的 ...
- 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 ...
- codeforces 599D Spongebob and Squares
很容易得到n × m的方块数是 然后就是个求和的问题了,枚举两者中小的那个n ≤ m. 然后就是转化成a*m + c = x了.a,m≥0,x ≥ c.最坏是n^3 ≤ x,至于中间会不会爆,测下1e ...
- CF 599D Spongebob and Squares(数学)
题目链接:http://codeforces.com/problemset/problem/599/D 题意:定义F(n,m)为n行m列的矩阵中方阵的个数,比如3行5列的矩阵,3x3的方阵有3个.2x ...
- Codeforces Round #332 (Div. 2) D. Spongebob and Squares(枚举)
http://codeforces.com/problemset/problem/599/D 题意:给出一个数x,问你有多少个n*m的网格中有x个正方形,输出n和m的值. 思路: 易得公式为:$\su ...
- [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 ...
随机推荐
- IQueryable、IEnumberable 、IList与List区别
IEnumerable:使用的是LINQ to Object方式,它会将AsEnumerable()时对应的所有记录都先加载到内存,然后在此基础上再执行后来的Query IQeurable(IQuer ...
- pyqt中定时器的使用
1.定义一个定时器函数 # 定时器 from PyQt5.QtCore import QTimer def timer_start(): timer = QTimer() # fun1是监听的函数,如 ...
- 如何创建Maven项目和Spring IOC例子
把如何创建Maven项目和创建Spring IOC的例子分享给大家,希望能对大家有帮助! 我的博客地址:https://www.cnblogs.com/themysteryofhackers/p/12 ...
- python接口自动化测试 - unittest框架基本使用
unittest简单介绍 单元测试框架 还可以适用WEB自动化测试用例的开发与执行 提供丰富的断言方法 官方文档:https://docs.python.org/zh-cn/3/library/uni ...
- Ubuntu18.04配置中文输入法(系统自带的)
Ubuntu18.04配置中文输入法 之前安装Ubuntu18.04的时候选择了english,现在要配置中文的输入法 打开系统设置 点击其中的工具按钮左边第一个 点击管理语言 ==如果系统刚安装可能 ...
- 洛谷 P3956 棋盘(记忆化搜索)
嗯... 题目链接:https://www.luogu.org/problem/P3956 这是一道比较好搜的题,注意一些剪枝.预处理和魔法的处理问题(回溯). AC代码: #include<c ...
- QBImagePickerController 不可选择照片流中照片
因为QBImagePickerController使用的ALAssetsLibrary方式来读取图片,如果采用默认方式去执行,那么根据url读取到的asset会为空,这时候我们就 需要特殊处理 [se ...
- (Java多线程系列九)线程池
线程池 1.什么是线程池 线程池是指在初始化一个多线程应用程序过程中创建一个线程集合,然后在需要执行新的任务时重用这些线程而不是新建一个线程.线程池中线程的数量通常取决于可用内存数量和应用程序的需求. ...
- VBA 学习笔记 - 运算符
学习资料:https://www.yiibai.com/vba/vba_operators.html 算术运算符 加减乘除模指,这个没啥特别的. 比较运算符 和 Lua 相比,判断相等变成了一个等于号 ...
- 在工业派上使用opencv库的记录
1.在工业派linux操作系统环境下,调用OpenCV库,用python写了第一个查看图片的程序 注意:要进入工业派的terminal 参考:https://www.cnblogs.com/magic ...