1. 程式人生 > >E. Missing Numbers (平方和)

E. Missing Numbers (平方和)

題目連結:https://codeforces.com/contest/1081/problem/E

題意:給個n,然後有n/2個數字,它們分別是位置2*i的值,讓你再找出n/2個數字,使得任意前n項和的值是平方數。

題解:我們直接從1開始列舉,列舉到值為平方數。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>

using namespace std;

typedef long long LL;

const int maxn=100010;

LL num[maxn];
LL res[maxn];

bool nosquare(LL x) ///判斷值x是否為平方數
{
    LL item=sqrt(x);
    return item*item!=x;
}

LL sqr(LL x) ///sqrt(x)
{
    double item=sqrt(x);
    item+=0.2;
    return item;
}

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n/2;i++)
            scanf("%lld",&num[i]);

            LL cur=0;
            bool flag=1;
        for(int i=0;i<n/2;i++)
        {
            cur++;

            ///列舉,直到為平方數
            while(nosquare(cur*cur+num[i])&&cur<(LL)1000000)
                cur++;


            if(cur==1000000){ 
                printf("No\n");
                flag=0;
                break;
            }

            res[2*i]=cur*cur; ///這兩個都是平方數
            res[2*i+1]=cur*cur+num[i];

            cur=sqr(cur*cur+num[i]);///開方,給下一輪

        }

        if(flag){
            printf("Yes\n");
                printf("%lld ",res[0]);
        for(int i=0;i<n-1;i++)
            printf("%lld ",res[i+1]-res[i]);
        puts("");
        }


    }

}