CF 599D 思维

Codeforces 559D
题目链接:
http://codeforces.com/problemset/problem/599/D
题意:
问有几种n*m的矩形方案,使得在里面放置1*1,2*2…矩形的方案由x种。输出方案。
思路:
现场的时候太困先睡,早上测单杠等的时候顺便想了下……好简单啊……
很容易知道对于合法的(n,m),(m,n)也是合法的。故假设n <= m。那么对于极端情况n=m,这时面积应该是n^2 + (n-1)^2+…1^2。怎么想的话画一画就知道对于2*2的矩形在n*n的矩形中有几种放法,和1*1的矩形在(n-1)*(n-1)的矩形中有几种方法是一样的(用边界来考虑)
那么一个面积x,它至少要大于等于n^2 + (n-1)^2+…1^2。也就是rest = 面积x减去n^2 + (n-1)^2+…1^2这个基础值。这时,若面积不够(rest > 0),则每次至少增加一列,一列中有n个格子。根据之前的换算可以类似的得出,每增加一列,面积增加d=(n+1)*n/2。判断一下rest是不是d的整数倍就可以。
源码:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <queue>
#include <vector>
using namespace std;
#define LL long long
const int MAXN = 1e6 + 5;
struct D
{
    LL u, v;
}ans[MAXN];
LL cal3(LL a)
{
    return a * (a + 1) * (2 * a + 1) / 6;
}
bool cmp(D a, D b){return a.u < b.u;}
int main()
{
    LL a;
    while(scanf("%I64d", &a) != EOF){
        int cnt = 0;
        for(LL i = 1 ; cal3(i) <= a ; i++){
            LL rest = a - cal3(i);
            LL d = (i + 1) * i / 2;
            if(rest % d == 0){
//                 printf("cal3(i) = %I64d\n", cal3(i));
//                printf("i = %I64d, rest = %I64d, d = %I64d\n", i, rest, d);
                ans[cnt].u = i, ans[cnt++].v = i + rest / d;
            if(rest != 0)   ans[cnt].u = i + rest / d, ans[cnt++].v = i;
            }
        }
        sort(ans, ans + cnt, cmp);
        printf("%d\n", cnt);
        for(int i = 0 ; i < cnt ; i++)
            printf("%I64d %I64d\n", ans[i].u, ans[i].v);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值