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 ...
随机推荐
- 【Java面试题】55 说说&和&&的区别。
&和&&都可以用作逻辑与的运算符,表示逻辑与(and),当运算符两边的表达式的结果都为true时,整个运算结果才为true,否则,只要有一方为false,则结果为false. ...
- js中页面跳转(href)中文参数传输方式
编码: escape(参数); 解码: unescape(参数);
- Oracle查询优化-多表查询
--合并结果集 --1.union all UNION ALL--单纯合并 ; --2.union UNION --将重复结果集合并 ; --------------使用命令窗口执行,查看union与 ...
- C++与Java混合编程
现在的程序员,不再像以前一样,掌握一种编程语言就可以混得有模有样了,现实的情况是,真实的项目中,通常是涉及多种编程语言,举几个简单的例子,一个软件为了快速开发,可能是使用Delphi或VB作为界面开发 ...
- linux vi编辑器中,如何通过快捷键上下翻页?
需求说明: 之前在vi的时候,如果想看下一页,就直接按住 ↓ 这个箭头一直翻,现在觉得有些麻烦, 就找了下上,下翻页的快捷方式.在此记录下. 记录: 1.向下翻页快捷键(下一页):Ctrl + f 2 ...
- Java精选笔记_网络编程
网络编程 概述 现在的网络编程基本上都是基于请求/响应方式的,也就是一个设备发送请求数据给另外一个,然后接收另一个设备的反馈. 在网络编程中,发起连接程序,也就是发送第一次请求的程序,被称作客户端(C ...
- Webservice简单案例
东西不用,时间长了就会被忘掉.重新拾起来 做一个简单的Demo,便于以后的查询 服务器端--新建Calculator.asmx using System; using System.Collectio ...
- swift -- 如何在swift下,使用类似oc的pch文件功能
以前在做oc下的项目的时候,pch文件是必创建的,因为实在是太方便了,只要在build setting里面把pch的路径换成绝对路径,那么剩下的,想干什么就在里面干什么,但是swift下,可以实现这种 ...
- 使用mt_rand代替rand
mt_rand比rand生成的随机数“更随机”,重复值较少 下面是测试: <?php function t1($num=10000){ $arr = array(); for ($i=0; $i ...
- round()
round() 用于对一个数值进行四舍五入,如果接收两个参数,则第二个参数表示保留多少位小数 In [1]: round(1.5324) Out[1]: 2.0 In [2]: round(1.532 ...