D. Spongebob and Squares

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/599/problem/D

Description

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 Input

26

Sample Output

6
1 26
2 9
3 5
5 3
9 2
26 1

HINT

题意

给你x,然后让你找有多少个n*m的矩形,可以由x个相同的多边形组成

题解:

数学题,这道题实际上是问,f(n,m) = sigma(k=1,k=min(n,m))(n-k+1)*(m-k+1)=x的解有多少个

化简之后,我们可以得到f(n,m) = n^2m+n^2+n*m+n-(n+1)*n/2*(n+m+2)+n*(n+1)*(2n+1)/6

这个式子是一个关于m的一次函数,我们枚举n就好了

就可以求m了,注意break条件

#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std; struct node
{
long long x,y;
};
bool cmp(node a,node b)
{
return a.y>b.y;
}
vector<node> ans1;
int main()
{
long long x;cin>>x;
for(long long i = ;i<=10000000LL||i*i*i<=x;i++)
{
long long a = (i*i+i-(i+)*i/2LL);
long long b = (i*i+i-(i+)*i*i/2LL-(i+)*i+(i*(i+)*(*i+)/));
long long y = x;
long long t= (y-b)/a;
if(i>t)continue;
if(a*t+b==y)
{
if(i==t)
{
node k;k.x = i,k.y = t;
ans1.push_back(k);
}
else
{
node k;k.x = i,k.y = t;
ans1.push_back(k);
k.x = t,k.y = i;
ans1.push_back(k);
}
}
}
sort(ans1.begin(),ans1.end(),cmp);
printf("%d\n",ans1.size());
for(int i=;i<ans1.size();i++)
{
printf("%lld %lld\n",ans1[i].x,ans1[i].y);
}
}

代码

Codeforces Round #332 (Div. 2) D. Spongebob and Squares 数学题枚举的更多相关文章

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

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

  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. Codeforces Round #332 (Div. 2) B. Spongebob and Joke 水题

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

  4. Codeforces Round #332 (Div. 二) B. Spongebob and Joke

    Description While Patrick was gone shopping, Spongebob decided to play a little trick on his friend. ...

  5. Codeforces Round #332 (Div. 2)_B. Spongebob and Joke

    B. Spongebob and Joke time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  6. Codeforces Round #332 (Div. 2)B. Spongebob and Joke

    B. Spongebob and Joke time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  7. Codeforces Round #332 (Div. 2) B. Spongebob and Joke 模拟

    B. Spongebob and Joke     While Patrick was gone shopping, Spongebob decided to play a little trick ...

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

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

  9. Codeforces Round #332 (Div. 2)

    水 A - Patrick and Shopping #include <bits/stdc++.h> using namespace std; int main(void) { int ...

随机推荐

  1. hdu 1078(dfs记忆化搜索)

    题意:容易理解... 思路:我开始是用dfs剪枝做的,968ms险过的,后来在网上学习了记忆化搜索=深搜形式+dp思想,时间复杂度大大降低,我个人理解,就是从某一个点出发,前面的点是由后面的点求出的, ...

  2. Android Dialog用法

    摘要: 创建对话框 一个对话框一般是一个出现在当前Activity之上的一个小窗口. 处于下面的Activity失去焦点, 对话框接受所有的用户交互. 对话框一般用于提示信息和与当前应用程序直接相关的 ...

  3. slidingmenu + fragment 左右菜单滑动

                           content_frame.xml <?xml version="1.0" encoding="utf-8" ...

  4. Matlab编程实例(1) 移动平均

    MATLAB数字信号处理作业,把自己写的程序发上来..欢迎交流~ QQ 五幺九七九零六四   首先是任意点移动平均: 主程序:mov_average_main.m (运行) 函数:mov_averag ...

  5. Asp.net TextBox常规输入验证

    Asp.net TextBox只能输入数字<asp:textbox id="TextBox1" onkeyup="if(isNaN(value))execComma ...

  6. 【数据结构和算法】 O(1)时间取得栈中的最大 / 最小元素值

    常数时间取得栈中的元素最大值和最小值,我们可以想到当push的时候比较一下,如果待push元素值小于栈顶元素,则更新min值,最大值亦然. 这样有个问题就是当pop的时候,就没了最大最小值. 于是上网 ...

  7. BufferedReader和BufferedWriter读写文件(转载)

    http://375940084.blog.51cto.com/2581965/751040 1.创建Student类存储每个学生信息,属性(学号,姓名,出生日期,得分)2.从c:/test/stud ...

  8. 【暑假】[实用数据结构]范围最小值问题(RMQ)

    范围最小值问题: 提供操作: Query(L,R):计算min{AL ~ AR } Sparse-Table算法: 定义d[i][j]为从i开始长度为2j的一段元素的最小值.所以可以用递推的方法表示. ...

  9. stl+模拟 CCF2016 4 路径解析

    // stl+模拟 CCF2016 4 路径解析 // 一开始题意理解错了.... #include <iostream> #include <string> #include ...

  10. MFC工程目录

    如果已经以Debug方式编译链接过程序,则会在解决方案文件夹下和工程子文件夹下各有一个名为“Debug”的文件夹,而如果是Release方式编译则会有名为“Release”的文件夹.这两种编译方式将产 ...