CF 599D 思维

来源:互联网 发布:4万亿救市的后果知乎 编辑:程序博客网 时间:2024/04/30 05:36

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 longconst 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
原创粉丝点击