Codeforces 599D Spongebob and Squares(数学)
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(数学)的更多相关文章
- codeforces 599D Spongebob and Squares
很容易得到n × m的方块数是 然后就是个求和的问题了,枚举两者中小的那个n ≤ m. 然后就是转化成a*m + c = x了.a,m≥0,x ≥ c.最坏是n^3 ≤ x,至于中间会不会爆,测下1e ...
- 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 ...
- CF 599D Spongebob and Squares(数学)
题目链接:http://codeforces.com/problemset/problem/599/D 题意:定义F(n,m)为n行m列的矩阵中方阵的个数,比如3行5列的矩阵,3x3的方阵有3个.2x ...
- [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 ...
- Codeforces 599D:Spongebob and Squares
D. Spongebob and Squares time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- 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算两种) 思路:比赛的 ...
- 【27.40%】【codeforces 599D】Spongebob and Squares
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Codeforces Round #332 (Div. 2) D. Spongebob and Squares(枚举)
http://codeforces.com/problemset/problem/599/D 题意:给出一个数x,问你有多少个n*m的网格中有x个正方形,输出n和m的值. 思路: 易得公式为:$\su ...
随机推荐
- init_MUTEX
2.6.25及以后的linux内核版本废除了init_MUTEX函数 新版本使用sema_init函数 平台:X86 32位内核:2.6.24定义: 引用 static inline void ini ...
- @interface java注解
@Documented,@Retention,@Target,@Inherited 1. 编写自定义@Todo注解经常我们在写程序时,有时候有些功能在当前的版本中并不提供,或由于某些其它原因,有些方法 ...
- LA 3641 (置换 循环的分解) Leonardo's Notebook
给出一个26个大写字母的置换B,是否存在A2 = B 每个置换可以看做若干个循环的乘积.我们可以把这些循环看成中UVa 10294的项链, 循环中的数就相当于项链中的珠子. A2就相当于将项链旋转了两 ...
- UVa 12096 The SetStack Computer【STL】
题意:给出一个空的栈,支持集合的操作,求每次操作后,栈顶集合的元素个数 从紫书给的例子 A={{},{{}}} B={{},{{{}}}} A是栈顶元素,A是一个集合,同时作为一个集合的A,它自身里面 ...
- Java Socket(2): 异常处理
1 超时 套接字底层是基于TCP的,所以socket的超时和TCP超时是相同的.下面先讨论套接字读写缓冲区,接着讨论连接建立超时.读写超时以及JAVA套接字编程的嵌套异常捕获和一个超时例子程序的抓包示 ...
- poj 3694 Network
题意: 添加每条新连接后网络中桥的数目// 超时 先放着了 ,下次改//早上这代码超时了 下午改了,代码在下面#include <iostream> #include <algori ...
- 快速搭建建SSH服务
一般来说如果用Ubuntu作为服务器,我们经常需要通过其他客户端远程连接它. 远程连接需要使用SSH,这里列出了一个快速完成这一任务的方法. 键入命令 # sudo apt-get install o ...
- 【转】apue《UNIX环境高级编程第三版》第一章答案详解
原文网址:http://blog.csdn.net/hubbybob1/article/details/40859835 大家好,从这周开始学习apue<UNIX环境高级编程第三版>,在此 ...
- TCP/IP详解学习笔记(5)-IP选路,动态选路,和一些细节
1.静态IP选路 1.1.一个简单的路由表 选路是IP层最重要的一个功能之一.前面的部分已经简单的讲过路由器是通过何种规则来根据IP数据包的IP地址来选择路由.这里就不重复了.首先来看看一个简单的系统 ...
- Linux iostat监测IO状态
Linux iostat监测IO状态 http://www.orczhou.com/index.php/2010/03/iostat-detail/