• 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);
    }
  • 相关阅读:
    畅通工程(hdu1232)并查集
    qsort函数的用法
    二叉搜索树(hdu3791)
    Binary Tree Traversals(HDU1710)二叉树的简单应用
    Safe Or Unsafe(hdu2527)哈弗曼VS优先队列
    山东省第四届acm解题报告(部分)
    Points on Cycle (hdu1700,几何)
    A计划 hdu2102(bfs一般题)
    杀人游戏(hdu2211)插入法
    hdu1518 Square(dfs)
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4981274.html
Copyright © 2020-2023  润新知