E. Missing Numbers (平方和)

版权声明:转载注明下出处就行了。 https://blog.csdn.net/LJD201724114126/article/details/85106908

题目链接: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("");
        }


    }

}

猜你喜欢

转载自blog.csdn.net/LJD201724114126/article/details/85106908
今日推荐