题目链接: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) +....)的更多相关文章

  1. Codeforces 599D Spongebob and Squares(数学)

    D. Spongebob and Squares Spongebob is already tired trying to reason his weird actions and calculati ...

  2. 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/ ...

  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 ...

  4. Codeforces 599D:Spongebob and Squares

    D. Spongebob and Squares time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

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

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

  6. 【27.40%】【codeforces 599D】Spongebob and Squares

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. CF 599D Spongebob and Squares(数学)

    题目链接:http://codeforces.com/problemset/problem/599/D 题意:定义F(n,m)为n行m列的矩阵中方阵的个数,比如3行5列的矩阵,3x3的方阵有3个.2x ...

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

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

  9. codeforces 599D Spongebob and Squares

    很容易得到n × m的方块数是 然后就是个求和的问题了,枚举两者中小的那个n ≤ m. 然后就是转化成a*m + c = x了.a,m≥0,x ≥ c.最坏是n^3 ≤ x,至于中间会不会爆,测下1e ...

随机推荐

  1. git call failed: [git clone Could not resolve host: git.openstack.org

    git config --global http.proxy $http_proxy git config --global https.proxy $https_proxy ref: http:// ...

  2. js 使用事件委托

    想象一下,如果你有一个无序列表,里面有一堆<li>元素,每一个<li>元素都会在点击的时候触发一个行为.这个时候,你通常会在每一个元素上添加一个事件监听,但是如果当这个元素或者 ...

  3. 设置同样字体大小,chrome浏览器有时字体偏大的解决办法(转)

    本文是找了在网上搜了好久才找到非常棒的一篇文章,很好的解决了这个问题. 原文地址:https://github.com/amfe/article/issues/10 这个特性被称做「Text Auto ...

  4. Phpcms v9专题分类增加模板设置的方法

    Phpcms v9专题设置里面,默认专题子分类是无模板设置的,本文教你通过官方论坛给出的教程实现专题分类增加模板设置.先来看看默认专题子分类设置界面: 修改后的的专题子分类设置界面多了模板设置: 修改 ...

  5. 【java】java 设计模式(4):建造者模式(Builder)

    工厂类模式提供的是创建单个类的模式,而建造者模式则是将各种产品集中起来进行管理,用来创建复合对象,所谓复合对象就是指某个类具有不同的属性,其实建造者模式就是前面抽象工厂模式和最后的Test结合起来得到 ...

  6. Ubuntu16.04下Mongodb(离线安装方式|非apt-get)安装部署步骤(图文详解)(博主推荐)

    不多说,直接上干货! 说在前面的话  首先,查看下你的操作系统的版本. root@zhouls-virtual-machine:~# cat /etc/issue Ubuntu LTS \n \l r ...

  7. Cocos2d-x3.0终于版Mac以及Win系统相关环境部署

    因个人原因此博客停止更新,其它更新博文将在该博客继续更新. http://blog.csdn.net/xiaohan_aimti/article/details/24653831 就在前几天,2014 ...

  8. zoj 1022 - Parallel Expectations

    题目:有两个指令序列,在执行时,能够执行随意指令序列的下一条指令.每条指令要一直执行到结束. 求两组指令执行结束后,每一个变量里面存储值的期望. 分析:dp,模拟.这道题算不上难题,只是算得上的麻烦题 ...

  9. 通过Nagios监控weblogic服务

    1.前言      前段时间搭建了一套Nagios监控服务,心血来潮想自己写一个脚本,拓展Nagios插件来监控公司的weblogic服务. 环境:weblogic10.3.3.0 . CentOS6 ...

  10. C++成员初始化列表的语法

    如果Classy是一个累,而mem1.mem2.mem3都是这个类的数据称源,则类构造函数可以使用如下的语法来初始化数据成员:Classy::Classy(int n, int m) : mem1(n ...