现在的位置: 首页 > 综合 > 正文

hdu 4486 Pen Counts 数学

2013年11月04日 ⁄ 综合 ⁄ 共 389字 ⁄ 字号 评论关闭

枚举最大边,其他的找规律。

注:O(n)算法才不会超时

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
    int cas;
    scanf("%d",&cas);
    while(cas--)
    {
        int id,n,tot=0;
        scanf("%d %d",&id,&n);
        if(n%3==0) tot++;
        int top=(int)ceil(n*1.0/3);
        for(int i=n/2;i>=top;i--)
        {
            if(n-i==i) continue;
            if((n-i)&1) {tot+=(i-(n-i+1)/2+1)*2;tot--;}
            else {tot+=(i-(n-i)/2+1)*2;tot-=2;}
        }
        printf("%d %d\n",id,tot);
    }
    return 0;
}

抱歉!评论已关闭.