计蒜客 Half-consecutive Numbers

来源:互联网 发布:怎样使用手机淘宝购物 编辑:程序博客网 时间:2024/05/22 03:24

题意:

求解大于n的最小的r,使得1/2*r(r-1)是一个完全平方数。 先打表出几组找规律,

    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

     可以发现1+1=后一项的2,3=sqrt(2^2*2+1),5=2+3,7=sqrt(5^2*2-1)....发现规律是后一项的第一个等于前两个数的和,后一项的第二个等于后一项的第一个的平方*2,偶数项的话再加上1,奇数项的话减去1.

The numbers 113366101015152121282836364545 and ti=12i(i+1)ti=21i(i+1), are called half-consecutive.

For given NN, find the smallest rr which is no smaller than NN such that trtr is square.

Input Format

The input contains multiple test cases.

The first line of a multiple input is an integer TT followed by TT input lines.

Each line contains an integer N (1≤N≤1016)N (1N1016).

Output Format

For each test case, output the case number first.

Then for given NN, output the smallest rr.

If this half-consecutive number does not exist, output −11.

样例输入

412950

样例输出

Case #1: 1Case #2: 8Case #3: 49Case #4: 288

题目来源

2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛

#include<iostream>#include<cstring>#include<cstdio>#include<cmath>#include<algorithm>using namespace std;long long num[55];int main(int argc, const char * argv[]) {    long long a=1,b=1,c;    num[1]=1;    for(int i=2;i<=27;i++)    {        if(i&1)        {            c=(a+b)*(a+b)*2;            num[i]=c-1;            a=a+b;            b=sqrt(c-1);        }        else        {            c=(a+b)*(a+b)*2;            num[i]=c;            a=a+b;            b=sqrt(c+1);        }    }    int t;    long long n;    scanf("%d",&t);    for(int cas=1;cas<=t;cas++)    {        scanf("%lld",&n);        for(int i=1;i<=27;i++)        {            if(num[i]>=n)            {                printf("Case #%d: %lld\n",cas,num[i]);                break;            }        }    }    return 0;}


阅读全文
0 0