Spongebob and Squares---cf599D(数学公式1 + (1+2) + (1+2+3) +....)
题目链接:http://codeforces.com/contest/599/problem/D
一个3×5(m×n)的长方形,里面包含15个边长为1的正方形,有8个边长为2的正方形,有3个边长为3的正方形,所以一共有 15+8+3=26 (num)个正方形,现在告诉你26(num),让你求有几个满足这样条件的长方形,并把对应的长和宽输出来;
一个m×n的长方形,里面包含正方形个数是:∑((m-a+1)*(n-a+1))(1<a<min(m, n));
我们可以枚举所有的 m 然后求出对应的整数解 n 即可,注意由于数的取值范围比较大,所以不能打表,要一次算出 n ;
当num = 26 时;
m = 1 : 1*n = 26; n = 26;
m = 2 : 2*n + 1*(n-1) = 26; n = 9;
m = 3 : 3*n + 2*(n-1) + 1*(n-2) = 26; n = 5;
m = 4 : 4*n + 3*(n-1) + 2*(n-2) + 1*(n-3) = 26; n无整数解;
m = 5 : 5*n + 4*(n-1) + 3*(n-2) + 2*(n-3) + 1*(n-4) = 26; n = 3;
...........
由上面可知,当任意m对应的式子都是
(1+2+3+...+m)n -[ (1) + (1+2) + (1+2+3) + ... + (1+2+3+...+m-2) + (1+2+3+...+m-1)] = num;
p n - q = num;
所以n = (num + q)/p;
当然在计算p和q时,用到一下公式:
1 + (1+2) + (1+2+3) +(1+2+3+4)+ ... +(1+2+3+...+n) = n*(n+1)*(n+2)/6
拓展:
1² + 2² + 3² + ... + n² = n*(n+1)*(2n+1)/6;
#include <iostream>
#include <vector>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
using namespace std;
#define N 10000050
#define PI 4*atan(1)
#define met(a, b) memset(a, b, sizeof(a)) typedef long long LL; LL num; struct node
{
LL m, n;
}a[N]; int cmp(node p, node q)
{
if(p.n != q.n)
return p.n < q.n;
return p.m < q.m;
} int main()
{
while(scanf("%I64d", &num)!=EOF)
{
int ans = ;
LL f = ; for(LL i=; i<=num && f<=num; i++)///结束条件就是不能让常数项大于num;
{
LL p = ((i+)*i)/;///X的系数;
LL q = ((i-)*(i-)*i)/ + (i*(i-))/;///常数项; f = q; if((num+q)%p == )
{
if( (num+q)/p < i ) break;///只算一半即可; a[ans].m = i;
a[ans++].n = (q+num)/p; if( (num+q)/p != i )///当两个数不相等时,反向保存;
{
a[ans].n = i;
a[ans++].m = (num+q)/p;
}
}
}
sort(a, a+ans, cmp); printf("%d\n", ans); for(int i=; i<ans; i++)
printf("%I64d %I64d\n", a[i].n, a[i].m);
}
return ;
}
Spongebob and Squares---cf599D(数学公式1 + (1+2) + (1+2+3) +....)的更多相关文章
- 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 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
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 ...
- 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 ...
- codeforces 599D Spongebob and Squares
很容易得到n × m的方块数是 然后就是个求和的问题了,枚举两者中小的那个n ≤ m. 然后就是转化成a*m + c = x了.a,m≥0,x ≥ c.最坏是n^3 ≤ x,至于中间会不会爆,测下1e ...
随机推荐
- sql字符串的拼接 (字符串和二进制,erlang的mysql驱动)
1> list_to_binary(["select * from aa limit","1",",","97"] ...
- 【Java面试题】50 垃圾回收器的基本原理是什么?垃圾回收器可以马上回收内存吗?有什么办法主动通知虚拟机进行垃圾回收?
1.对于GC来说,当程序员创建对象时,GC就开始监控这个对象的地址.大小以及使用情况. 通常,GC采用有向图的方式记录和管理堆(heap)中的所有对象.通过这种方式确定哪些对象是"可达的&q ...
- android获取手机屏幕分辨率
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceSt ...
- MathType使用中的四个小技巧
MathType是一种比较常见的数学公式编辑器,常常与office搭配着使用,我们在使用的时候有一些要注意的小技巧,下面我们就来给大家介绍介绍MathType使用中的四个小技巧? 技巧一:调整工具栏显 ...
- COOKIE和SESSION关系和区别等
一.cookie介绍 cookie 常用于识别用户.cookie 是服务器留在用户计算机中的小文件.每当相同的计算机通过浏览器请求页面时,它同时会发送 cookie.通过 PHP,您能够创建并取回 c ...
- 使用命令wsimport构建WebService客户端
原文:http://www.cnblogs.com/ningvsban/p/3760085.html wsimport命令介绍 在JDK的bin文件夹中,有一个wsimport.exe,这个工具依据w ...
- dz数据结构
pre_common_admincp_cmenu 后台 首页 | 常用操作管理数据表 字段名 数据类型 默认值 允许非空 自动递增 备注 id smallint(6) unsigned NO 是 ...
- LeetCode - Department Highest Salary
题目大概的意思是选出每个Department里工资最高的人的信息并组成相应的表信息 有几个值得注意的地方:1)使用group by语句时,前面的select语句后面的内容只能有两种情况一种是group ...
- Ubuntu 如何更改用户密码
你需要为第一个帐户创建一个密码.这可以用 passwd 命令来完成. 系统会提示输入你的旧密码一次,输入你的新密码两次.用 root 用户更改用户 paul 的密码[root@bigboy root] ...
- CRUX下实现进程隐藏(1)
想必能找到这里的都是被吴一民的操作系统大作业坑过的学弟学妹了,当初我也是千辛万苦才把这个作业完成了,本着服务后辈的宗旨,尽量让学弟学妹少走弯路,我会把实现的大概思路记录下来.本系列一共三篇文章,分别实 ...