Codeforces Beta Round #85 (Div. 1 Only) A. Petya and Inequiations 贪心

A. Petya and Inequiations

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/111/problem/A

Description

Little Petya loves inequations. Help him find n positive integers a1, a2, ..., an, such that the following two conditions are satisfied:

  • a12 + a22 + ... + an2 ≥ x
  • a1 + a2 + ... + an ≤ y

Input

The first line contains three space-separated integers nx and y (1 ≤ n ≤ 105, 1 ≤ x ≤ 1012, 1 ≤ y ≤ 106).

Please do not use the %lld specificator to read or write 64-bit integers in С++. It is recommended to use cin, cout streams or the %I64d specificator.

Output

Print n positive integers that satisfy the conditions, one integer per line. If such numbers do not exist, print a single number "-1". If there are several solutions, print any of them.

Sample Input

5 15 15

Sample Output

4
4
1
1
2

HINT

题意

 让你找n个数,使其满足

a1^2+a2^2...+an^2>=x

a1+a2+...+an<=y

找不到输出-1

题解:

贪心一下,我们让a1-an-1都令为1,然后剩下的an我们就直接暴力枚举就好了~

代码

#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;

int main()
{
    int n;long long x,y;
    cin>>n>>x>>y;
    x-=(n-1);
    y-=(n-1);
    if(y<=0){return puts("-1");}
    int flag = 0;
    long long t;
    for(int i=1;i<=y;i++)
    {
        t = i;
        if(t>y)
            return puts("-1");
        if(t*t>=x)
        {
            flag = 1;
            break;
        }
    }
    if(flag==0)
        return puts("-1");
    for(int i=1;i<=n-1;i++)
        printf("1
");
    printf("%d
",t);
}
原文地址:https://www.cnblogs.com/qscqesze/p/4981274.html