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 ...
随机推荐
- Graphviz 对网状结构进行可视化
Graphviz 是一款开源的,免费的图结构的可视化软件,只需要描述清楚图中的顶点,边的信息,Graphviz 可以自动化的对图进行布局,生成对应的图片: Graphviz 采用DOT 的这种语言来描 ...
- sip 认证分析
转自:http://blog.csdn.net/wangqi0079/article/details/11569489 SIP类似Http协议.其认证模式也一样.Http协议(RFC 2616 )规定 ...
- JavaScript网页换肤
使网页背景颜色可选黄/粉 <!doctype html> <html> <head><title>网页换肤</title></head ...
- TTCN中PTC的执行流程
一些概念 Component(測试组件或者測试成分),TTCN接触下来最频繁的就是MTC(Main Test Component,主測试组件),在执行測试用例前,须要首先创建一个MTC.在testca ...
- 从源代码来理解ArrayList和LinkedList差别
从源代码理解ArrayList和LinkedList差别 ArrayList ArrayList默认容量为10,实质是一个数组用于存放元素,size表示ArrayList所包括的元素个数. Array ...
- 自定向下分析Binder 之 Binder Model(1)
Java层的Binder对象模型: IBinder IBinder是Binder通信机制中的核心部分(Base interface for a remotable object, the core p ...
- org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 12 in XML document from
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 12 in XML document from ...
- ARM漏洞
Google安全团队Project Zero公布了多个高危漏洞,称这些漏洞几乎影响到了市面上所有的微处理器,AMD.ARM还是英特尔的处理器都难以幸免,围绕这些处理器打造的操作系统和硬件设备也会受到影 ...
- IP地址转、整数互相转换
知识点:一个二进制数,按位左移n位,就是把该数的值乘以2的n次方 二进制除二即右移一位 1.IP地址转换为整数 原理:IP地址每段可以看成是8位无符号整数即0-255 ...
- JS Date parse
因为JS中的Date转换格式没有“-”这种间隔符,Date.parse会生成NAN,所以只能进行转换. <script type="text/javascript"> ...