• Avito Cool Challenge 2018 E. Missing Numbers 【枚举】


    传送门:http://codeforces.com/contest/1081/problem/E

    E. Missing Numbers

    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Chouti is working on a strange math problem.

    There was a sequence of nn positive integers x1,x2,,xnx1,x2,…,xn, where nn is even. The sequence was very special, namely for every integer ttfrom 11 to nn, x1+x2+...+xtx1+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,,xnx2,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 nn (2n1052≤n≤105).

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

    Output

    If there are no possible sequence, print "No".

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

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

    Examples
    input
    Copy
    6
    5 11 44
    output
    Copy
    Yes
    4 5 16 11 64 44
    input
    Copy
    2
    9900
    output
    Copy
    Yes
    100 9900
    input
    Copy
    6
    314 1592 6535
    output
    Copy
    No
    Note

    In the first example

    • x1=4x1=4
    • x1+x2=9x1+x2=9
    • x1+x2+x3=25x1+x2+x3=25
    • x1+x2+x3+x4=36x1+x2+x3+x4=36
    • x1+x2+x3+x4+x5=100x1+x2+x3+x4+x5=100
    • x1+x2+x3+x4+x5+x6=144x1+x2+x3+x4+x5+x6=144
    All these numbers are perfect squares.

    In the second example, x1=100x1=100, x1+x2=10000x1+x2=10000. They are all perfect squares. There're other answers possible. For example, x1=22500x1=22500 is another answer.

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

    题意概括:

    给出 N/2 个 偶数位的数,需要补充剩余 N/2 个奇数位的数,使得 每一位的前缀和都是完全平方数。

    如果存在解,按顺序输出 N 个完整数字。

    解题思路:

    从第一位开始构造前缀和,如果按照实际的数的范围去枚举 x(1e13)肯定会爆炸,那么就折半枚举√x,判断当前前缀和 Si 是否为完全平方数,

    如果是则记录下来,并且从 √Si + 1开始枚举求解下一个前缀和。

    如果超过的 √Smax ,说明无解。

    AC code:

     1 #include <cstdio>
     2 #include <iostream>
     3 #include <algorithm>
     4 #include <cstring>
     5 #include <vector>
     6 #include <cmath>
     7 #define INF 0x3f3f3f3f
     8 #define LL long long
     9 using namespace std;
    10 
    11 const int MAXN = 1e5+10;
    12 const LL MAXW = 1100000;
    13 
    14 LL b[MAXN], a[MAXN];
    15 
    16 bool check(LL x)
    17 {
    18     LL y = sqrt(x);
    19     if(y*y == x) return true;
    20     return false;
    21 }
    22 
    23 int main()
    24 {
    25     int N;
    26     LL sum = 0;
    27     scanf("%d", &N);
    28     for(int i = 1; i <= N/2; i++){
    29         scanf("%I64d", &a[i]);
    30     }
    31 
    32     LL tmp = 1LL;
    33     while(!check(tmp*tmp+a[1])){
    34         tmp++;
    35         if(tmp >= MAXW){
    36             puts("No");
    37             return 0;
    38         }
    39     }
    40     b[1] = tmp*tmp;
    41     b[2] = a[1];
    42     int cnt = 2;
    43     tmp = sqrt(tmp*tmp+a[1]);
    44 
    45     for(int i = 2; i <= N/2; i++){
    46         if(tmp >= MAXW){
    47                 puts("No");
    48                 return 0;
    49         }
    50         sum = tmp;
    51         tmp++;
    52         while(!check(tmp*tmp+a[i])){
    53             tmp++;
    54             if(tmp >= MAXW){
    55                 puts("No");
    56                 return 0;
    57             }
    58         }
    59         b[++cnt] = tmp*tmp - sum*sum;
    60         b[++cnt] = a[i];
    61         tmp = sqrt(tmp*tmp + a[i]);
    62         printf("a:%I64d tmp:%I64d
    ",a[i], tmp);
    63     }
    64     puts("Yes");
    65     for(int i = 1; i <= cnt; i++)
    66         printf("%I64d ", b[i]);
    67 
    68     return 0;
    69 }
    View Code
  • 相关阅读:
    java基础--13.Regex正则表达式
    java基础面试题12--Integer--自动拆装箱
    java基础面试题11--String--最大公共子串
    笔记本wif模块接线备忘
    windows环境下gcc/g++ 编译器 乱码问题解决
    转轮加密机 加解密 (C语言实现)
    windbg
    windows 内核调试
    python源码编译(嵌入式)
    Android studio
  • 原文地址:https://www.cnblogs.com/ymzjj/p/10139054.html
Copyright © 2020-2023  润新知