Codeforces Contest 1081 problem E Missing Numbers——枚举

91 篇文章 0 订阅

Chouti is working on a strange math problem.

There was a sequence of n positive integers x1,x2,…,xn, where n is even. The sequence was very special, namely for every integer t from 1 to n, x1+x2+…+xt is a square of some integer number (that is, a perfect square).

Somehow, the numbers with odd indexes turned to be missing, so he is only aware of numbers on even positions, i.e. x2,x4,x6,…,xn. The task for him is to restore the original sequence. Again, it’s your turn to help him.

The problem setter might make mistakes, so there can be no possible sequence at all. If there are several possible sequences, you can output any.

Input
The first line contains an even number n (2≤n≤105).

The second line contains n2 positive integers x2,x4,…,xn (1≤xi≤2⋅105).

Output
If there are no possible sequence, print “No”.

Otherwise, print “Yes” and then n positive integers x1,x2,…,xn (1≤xi≤1013), where x2,x4,…,xn should be same as in input data. If there are multiple answers, print any.

Note, that the limit for xi is larger than for input data. It can be proved that in case there is an answer, there must be a possible sequence satisfying 1≤xi≤1013.

Examples
inputCopy
6
5 11 44
outputCopy
Yes
4 5 16 11 64 44
inputCopy
2
9900
outputCopy
Yes
100 9900
inputCopy
6
314 1592 6535
outputCopy
No
Note
In the first example

x1=4
x1+x2=9
x1+x2+x3=25
x1+x2+x3+x4=36
x1+x2+x3+x4+x5=100
x1+x2+x3+x4+x5+x6=144
All these numbers are perfect squares.
In the second example, x1=100, x1+x2=10000. They are all perfect squares. There’re other answers possible. For example, x1=22500 is another answer.

In the third example, it is possible to show, that no such sequence exists.

题意:

有n个数,n是偶数,现在给你第2i个数,其他的要你去找,这个数组有一个性质,所有前缀和都是平方数。

题解:

假设某个偶数位置前缀和是 s 1 2 s_1^2 s12,下一个是 s 2 2 s_2^2 s22,下一个是 s 3 2 s_3^2 s32,那么 s 2 2 s_2^2 s22= s 1 2 s_1^2 s12+x, s 3 2 s_3^2 s32= s 2 2 s_2^2 s22+c(x是未知,c是已知)。
那么 s 2 = s 1 2 + x s_2=\sqrt{s_1^2+x} s2=s12+x s 3 2 = s 2 2 + c = s 1 2 + x + c s_3^2=\sqrt{s_2^2+c}=\sqrt{s_1^2+x+c} s32=s22+c =s12+x+c ,那么我们s1是已知的,我们枚举s2,对每个s2算出x,在检验s3的存在性,如果枚举到的s2*s2-(s2-1)*(s2-1)>c的话,就说明没有这个数了,因为c的范围是1e13,所以枚举到的最大是3e6;

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=2e5+5;
ll c[N],ans[N];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=2;i<=n;i+=2)
        scanf("%lld",&c[i]);
    ll s=0;
    int flag=0;
    for(int i=1;i<=n;i+=2)
    {
        for(ll j=s+1;;j++)
        {
            ll x=j*j-s*s,s2=sqrt(j*j+c[i+1]);
            if(s2*s2==j*j+c[i+1])
            {
                c[i]=x;
                s=s2;
                break;
            }
            if(j*j-(j-1)*(j-1)>c[i+1])
            {
                flag=1;
                break;
            }
        }
        if(flag)
            break;
    }
    if(flag)
        return 0*printf("No\n");
    printf("Yes\n");
    for(int i=1;i<=n;i++)
        printf("%d%c",c[i],i==n?'\n':' ');
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值