Codeforces Beta Round #85 (Div. 1 Only) A. Petya and Inequiations 贪心
2015-11-20 16:43
651 查看
A. Petya and Inequiations
Time Limit: 20 SecMemory Limit: 256 MB
题目连接
http://codeforces.com/contest/111/problem/ADescription
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 n, x 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\n"); printf("%d\n",t); }
相关文章推荐
- cannot access storage file (as uid:107, gid:107)permission denied
- AD 域控查询命令 dsquery
- UIView的生命周期
- iOS经典讲解之UIImage初始化一张图片的方法及优缺点
- GPUImage简单滤镜使用(一)
- UITextView在光标处插入字符串
- iOS- 添加UIWindow蒙版
- 关于UGUI的EventSystem的学习
- 那些年,我们一起追的面试题。。to be continued!!!
- UIToolBar
- UITextView的动态适应高度处理
- 如何做一个网站 (C# + MVC Web+ easyUI )
- response.setContentType与 request.setCharacterEncoding 区别
- Size Classes与Interface Builder与Imag Asset与Autolayout
- 往resin 服务器中发送post 报 POST requires content-length
- UUID 和 GUID 的区别
- UIAccessibilityElement
- Unique Binary Search Trees II
- 1007. Maximum Subsequence Sum
- 详释(常见UITextView 输入之字数限制)之一---固定长度