D. Spongebob and Squares--cf599D(数学)
http://codeforces.com/problemset/problem/599/D
题目大意:给你一个数k 让你求一个n*m的矩形里面包含k个正方形 输出有几个这样的矩形 分别是什么
可以推出一个数学公式
我们枚举i*i的正方形 这个正方形里面的包含的正方形是有(i*i)+(i-1) *( i-1)+(i-2)*(i-2)+...+(1*1) 就等于b=i*(i-1)*(2*i-1)/6;
如果k>b 说明这个正方形里面的正方形是不够的 我们需要再添加n个(1*i)的列
如果添加一列能增加的小正方形是(从0加到i)i*(i+1)/2
所以如果说(k-b)%(i*(i+1)/2)==0 说明正好有(k-b)/(i*(i+1)/2)这么多列 然后就保存下来就行了
i最多也就2000000的样子 可以直接暴力
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <math.h>
#include <ctype.h> using namespace std;
#define memset(a,b) memset(a,b,sizeof(a))
#define N 5001000
typedef long long ll;
const double ESP = 1e-;
#define INF 0xfffffff struct node
{
ll x,y;
}a[N]; int main()
{
ll k;
while(scanf("%lld",&k)!=EOF)
{ ll sum=;
ll i;
int flag=;
ll p=(ll)sqrt(k);
for(i=;;i++)
{
if(i> || i>p)
break;
ll b=(i*(i+)*(*i+)/);
ll c=i*(i+)/;
if(k>=b&&(k-b)%c==)
{
a[sum].x=i;
a[sum++].y=(k-b)/c+i;
}
}
if(a[sum-].y==a[sum-].x && a[sum-].x==a[sum-].y)
{
printf("%lld\n",(sum-)*);
for(i=;i<sum;i++)
printf("%lld %lld\n",a[i].x,a[i].y);
for(i=sum-;i>=;i--)
printf("%lld %lld\n",a[i].y,a[i].x);
}
else
{
if(a[sum-].x==a[sum-].y)
{
printf("%d\n",sum*-);
for(i=;i<sum-;i++)
printf("%lld %lld\n",a[i].x,a[i].y);
for(i=sum-;i>=;i--)
printf("%lld %lld\n",a[i].y,a[i].x);
}
else
{
printf("%lld\n",sum*);
for(i=;i<sum;i++)
printf("%lld %lld\n",a[i].x,a[i].y);
for(i=sum-;i>=;i--)
printf("%lld %lld\n",a[i].y,a[i].x);
}
}
}
return ;
}
D. Spongebob and Squares--cf599D(数学)的更多相关文章
- Codeforces 599D Spongebob and Squares(数学)
D. Spongebob and Squares Spongebob is already tired trying to reason his weird actions and calculati ...
- 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 数学
D. Spongebob and Squares Spongebob is already tired trying to reason his weird actions and calcula ...
- 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 599D:Spongebob and Squares
D. Spongebob and Squares time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- 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 ...
- NYOJ 141 Squares (数学)
题目链接 描述 A square is a 4-sided polygon whose sides have equal length and adjacent sides form 90-degre ...
- Codeforces Round #332 (Div. 2) D. Spongebob and Squares(枚举)
http://codeforces.com/problemset/problem/599/D 题意:给出一个数x,问你有多少个n*m的网格中有x个正方形,输出n和m的值. 思路: 易得公式为:$\su ...
- codeforces 599D Spongebob and Squares
很容易得到n × m的方块数是 然后就是个求和的问题了,枚举两者中小的那个n ≤ m. 然后就是转化成a*m + c = x了.a,m≥0,x ≥ c.最坏是n^3 ≤ x,至于中间会不会爆,测下1e ...
随机推荐
- 深入解析Web Services
SOA,面向服务器建构,是一款架构,这几年虽然没前几年那么流行,但是还是有很多企业在用,而Web Services是目前适合做SOA的主要技术之一,通过使用Web Services,应用程序可以对外发 ...
- 洛谷 P2912 [USACO08OCT]牧场散步Pasture Walking
题目描述 The N cows (2 <= N <= 1,000) conveniently numbered 1..N are grazing among the N pastures ...
- 学习Python的一些Tips
0. Python安装 官网提供多种方式,一般Windows下直接安装exe即可:Linux下基本上自带python:另外也提供源码,也可自行编译: 若安装后无法使用,则检查一下环境变量是否设置正确. ...
- mysql数据库比较,各数据库不同之处
和mysql数据库比较,各数据库不同之处: Oracle数据库:字段类型不同 postgresql数据库:show tables不同; SQL语句需要前面加上 模式名 Mongodb数据库:文档存储, ...
- Open Cascade:使用鼠标画线
Open Cascade:使用鼠标画线 在View类文件中创建以下代码: 1.创建鼠标消息: afx_msg void OnLButtonDown(UINT nFlags, CPoint point) ...
- 内置函数filter和map
filter filter()函数接收一个函数 f 和一个list,这个函数 f 的作用是对每个元素进行判断,返回 True或 False,filter()根据判断结果自动过滤掉不符合条件的元素,返回 ...
- FastNet C++/Python 网络通信库之 协议
协议可以使用的基础数据类型: UInt8,UInt16,UInt32,UInt64Int8,Int16,Int32,Int64Float,Double,Bool,String [T] 数组,T代表元 ...
- UVa-1339-古老的密码
这题的话,我们可以把字符串序列里面的字母直接计数,然后比较两个数组里面的数字是否一一相同,然后就可以直接判定YES or NO. 因为它题目中说的就是一种映射的关系,首先我们读入之后,把两个字符串的不 ...
- 19. REFERENTIAL_CONSTRAINTS
19. REFERENTIAL_CONSTRAINTS REFERENTIAL_CONSTRAINTS表提供有关外键的信息. REFERENTIAL_CONSTRAINTS有以下列: CONSTRAI ...
- redis搭建配置
1 .去官方下载 2.解压tar 3.进入解压目录 编译 4.将编译好的目录移动到制定位置.做软连接 .配置环境便利 5.创建数据保存目录.创建配置文件 [root@radis ~]# vim /da ...