2017 烏魯木齊賽區網絡賽 Half-consecutive Numbers(【規律題】)

題目鏈接:https://nanti.jisuanke.com/t/16954
【中文題意】
找到這樣的i,使得i*(i+1)=2*k^2。其中k爲任意整數,i爲整數。
然後輸入n,輸出不小於n的這個i。
【思路分析】遇到這樣的題目,我們先打表出前幾組數據然後找規律。
1,8,49,288,1681,9800
1*1,4*9,25*49,144*289,841*1681,4900*9801
(1*1)^2,(2*3)^2,(5*7)^2,(12*17)^2,(29*41)^2,(70*99)^2
然後我們可以發現除了第一項,每一項的第一個數是上一項的兩個數之和,第二個數是:這一項的第一個數的平方*2,如果是奇數項就-1,偶數項就+1,然後開根號的結果。這裏我是使用Pair實現的。
【AC代碼】

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define LL long long


int main()
{
    pair<LL,LL >p[105];
    LL re[105];
    p[1]=make_pair(1,1);
    re[1]=1;
    LL cnt=1;
    for(LL i=2;i<=105;i++)
    {
        p[i].first=p[i-1].first+p[i-1].second;
        LL ans=p[i].first*p[i].first*2;
        if(ans%2==0)
        p[i].second=(LL)sqrt(ans+1);
        else
        {
            p[i].second=(LL)sqrt(ans-1);
        }
        //printf("%lld %lld\n",p[i].first,p[i].second);
        re[i]=p[i].second*p[i].second;
        if(i%2==0)
            re[i]-=1;
        //printf("%lld\n",re[i]);
        cnt++;
        if(re[i]>1e16)
        {
            break;
        }
    }
    LL n,t,iCase=0;
    scanf("%lld",&t);
    while(t--)
    {
        scanf("%lld",&n);
        for(int i=1;i<=cnt;i++)
        {
            if(n<=re[i])
            {
                printf("Case #%lld: %lld\n",++iCase,re[i]);
                break;
            }
        }
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章