HDU 6154 CaoHaha's staff 找规律

来源:互联网 发布:windows loader v2.2.2 编辑:程序博客网 时间:2024/06/05 01:51

题目链接:HDU6154

CaoHaha's staff

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 549    Accepted Submission(s): 324


Problem Description
"You shall not pass!"
After shouted out that,the Force Staff appered in CaoHaha's hand.
As we all know,the Force Staff is a staff with infinity power.If you can use it skillful,it may help you to do whatever you want.
But now,his new owner,CaoHaha,is a sorcerers apprentice.He can only use that staff to send things to other place.
Today,Dreamwyy come to CaoHaha.Requesting him send a toy to his new girl friend.It was so far that Dreamwyy can only resort to CaoHaha.
The first step to send something is draw a Magic array on a Magic place.The magic place looks like a coordinate system,and each time you can draw a segments either on cell sides or on cell diagonals.In additional,you need 1 minutes to draw a segments.
If you want to send something ,you need to draw a Magic array which is not smaller than the that.You can make it any deformation,so what really matters is the size of the object.
CaoHaha want to help dreamwyy but his time is valuable(to learn to be just like you),so he want to draw least segments.However,because of his bad math,he needs your help.
 

Input
The first line contains one integer T(T<=300).The number of toys.
Then T lines each contains one intetger S.The size of the toy(N<=1e9).
 

Output
Out put T integer in each line ,the least time CaoHaha can send the toy.
 

Sample Input
512345
 

Sample Output
44667
 
题意:问在一个网格中只能连接一个单元格的边或者对角线,然后不小于面积s最少要画多少条线。
题目分析:蜜汁找规律,画一画就能看出来,边数为4的倍数是构成的面积最大是全是对角线构成的那种斜的正方形,面积2n^2,然后比如4条边到8条边每加一条边面积+0.5,1.5,1.5,2.5,8到12之间是1.5,2.5,2.5,3.5,对应着算一遍即可。
////  main.cpp//  CaoHaha's staff////  Created by teddywang on 2017/8/19.//  Copyright © 2017年 teddywang. All rights reserved.//#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int mod=1e9+7;typedef long long ll;ll s[40000];int init(){    s[1]=2;    s[2]=8;    for(int i=3;;i++)    {        s[i]=i*i*2;        if(s[i]>1e9) return i;    }}int main(){    int cnt=init();    int T;    scanf("%d",&T);    while(T--)    {        ll a;        scanf("%lld",&a);        if(a==1)        {            printf("4\n");            continue;        }        ll num=0;        for(int i=1;i<cnt;i++)        {            if(a>=s[i]&&a<s[i+1])            {                num=i;                break;            }        }        if(s[num]==a)        {            printf("%lld\n",num*4);            continue;        }        else        {            double s1=s[num];            double b[4]={num-0.5,num+0.5,num+0.5,num+1.5};            num*=4;            for(int j=0;j<4;j++)            {                s1+=b[j];                num++;                if(s1>=a) break;            }            printf("%lld\n",num);        }            }}



原创粉丝点击