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 ...
随机推荐
- Linux基础知识之man手册的使用
在Linux中man的使用频率应该是很高的,灵活运用它可以让自己快速的掌握一个不熟悉命令的使用方法.下面来介绍下man 环境CentOS6.8 man - format and display the ...
- asp.net mvc webconfig配置文件操作
读取web.config数据,可以不用编译.如发布后,非常有用web.config文件<configuration> <appSettings> <add key=&qu ...
- 【Java集合的详细研究9】Java堆栈(stack)的使用方法
栈是一种常用的数据结构,栈只允许访问栈顶的元素,栈就像一个杯子,每次都只能取杯子顶上的东西,而对于栈就只能每次访问它的栈顶元素,从而可以达到保护栈顶元素以下的其他元素.”先进后出”或”后进先出”就是栈 ...
- Loadrunner C 编程_1
就目前的了解.Loadrunner的脚本语言其实和C没什么区别.他内部的好多机制都是C实现的. 不过是一种“类C” 所以我从几个方面分析 1:定义常量变量和C一样 2:在LR中,C的变量和LR的参数是 ...
- 【Python】动态获取python类名、函数名&多线程
import time import random import threading import inspect def get_current_function_name(): return in ...
- linux系统中,查看当前系统中,都在监听哪些端口
需求描述: 查看当前系统中都监听着哪些的端口,用netstat命令,在此记录下 操作过程: 1.查看系统中都在监听哪些端口 [root@testvm home]# netstat -ntl Activ ...
- Leetcode: Best Time to Buy and Sell Stock I, II
思路: 1. 算法导论讲 divide and conquer 时, 讲到过这个例子. 书中的做法是先让 price 数组减去一个值, 然后求解最大连续子数组的和. 分治算法的复杂度为 o(nlogn ...
- POJ 1160 Post Office(区间DP)
Description There is a straight highway with villages alongside the highway. The highway is represen ...
- 2011年冬斯坦福大学公开课 iOS应用开发教程学习笔记(第三课)
第二课名称是:Objective-C 回顾上节课的内容: 创建了单个MVC模式的项目 显示项目的各个文件,显示或隐藏导航,Assistant Editor, Console, Object Libra ...
- Python IDLE背景设置与使用
相信刚进入python学习之路的朋友们,都还是挺喜欢python自带的IDLE,但是白的代码背景色以及其它的代码色确实让人看着有点不舒服,所以当时也琢磨着能不能自己给它换换颜色,这个当然可以,废话不多 ...