CF111A Petya and Inequiations 题解
Content
请找出一个由 \(n\) 个正整数组成的数列 \(\{a_1,a_2,\dots,a_n\}\),满足以下两种条件:
- \(\sum\limits_{i=1}^na_i^2\geqslant x\)。
- \(\sum\limits_{i=1}^na_i\leqslant y\)。
数据范围:\(1\leqslant n\leqslant 10^5\),\(1\leqslant x\leqslant 10^{12}\),\(1\leqslant y\leqslant 10^6\)。
Solution
我们不妨设 \(f(x)=x^2(x>0)\),然后画出这个函数的图像之后不难看出 \(f(x)\) 是增函数,且随着 \(x\) 的增长,其值越来越大。因此我们考虑这样的一个贪心:将前 \(n-1\) 个数全部赋值为 \(1\),最后一个数赋值为 \(y-(n-1)=y-n+1\)。然后再计算 \(1^2\cdot(n-1)+(y-n+1)^2\) 是否 \(\geqslant x\),是的话直接输出方案,否则输出 \(-1\)。
另外这题还要注意一个特判:因为题目中并没有保证 \(y\geqslant n\),而要求的数列又要求全是正整数。所以如果 \(y<n\),那么也无法得到合法的方案,输出 \(-1\)。
Code
ll ans[100007], xx;
int main() {
int n = Rint; ll x = Rll; int y = Rint;
if(y < n) return puts("-1"), 0;
F(i, 2, n) ans[i] = 1;
ans[1] = y - n + 1;
F(i, 1, n) xx += 1ll * ans[i] * ans[i];
if(xx < x) return puts("-1"), 0;
F(i, 1, n) write(ans[i]), puts("");
return 0;
}
CF111A Petya and Inequiations 题解的更多相关文章
- 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 ...
- codeforces 111A/112C Petya and Inequiations
题目:Petya and Inequiations传送门: http://codeforces.com/problemset/problem/111/A http://codeforces.com/p ...
- CF1080A Petya and Origami 题解
Content 小 P 想给 \(n\) 位朋友各发一张邀请函,每张邀请函需要耗费 \(2\) 张红色纸,\(5\) 张绿色纸和 \(8\) 张蓝色纸.商店里面的纸是一堆一堆卖的,每堆里面有 \(k\ ...
- CF1214E Petya and Construction Set题解
原来这就叫构造题,了 这道题的做法,我自己诌了一个形象的名字--"挂葡萄"法( 首先,"搭葡萄架":考虑到每个距离 \(d_i\) 只与 \(2i-1,2i\) ...
- codeforce--Vasya and Petya's Game
网址:http://codeforces.com/contest/576/problem/A A. Vasya and Petya's Game time limit per test 1 secon ...
- Codeforces Beta Round #85 (Div. 1 Only) B. Petya and Divisors 暴力
B. Petya and Divisors Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/111 ...
- Codeforces Codeforces Round #319 (Div. 2) C. Vasya and Petya's Game 数学
C. Vasya and Petya's Game Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/ ...
- CodeForces832-B. Petya and Exam
补的若干年以前的题目,水题,太菜啦_(:з」∠)_ B. Petya and Exam time limit per test 2 seconds memory limit per test 2 ...
- Petya and Array CodeForces - 1042D (树状数组)
D. Petya and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
随机推荐
- JVM的Xms Xmx PermSize MaxPermSize区别
Eclipse崩溃,错误提示:MyEclipse has detected that less than 5% of the 64MB of Perm Gen (Non-heap memory) sp ...
- [USACO07MAR]Face The Right Way G
发现选定一个长度后,怎么翻转是固定的. 那我们直接选定一个长度去操作就行. 优化操作过程 类似于堆里打持久化标记一样的感觉. [USACO07MAR]Face The Right Way G // P ...
- 洛谷 P3643 - [APIO2016]划艇(dp)
题面传送门 一道难度中等的 \(dp\)(虽然我没有想出来/kk). 首先一眼 \(dp_{i,j}\) 表示考虑到第 \(i\) 个学校,第 \(i\) 个学校派出了 \(j\) 个划艇的方案数,转 ...
- 洛谷 P3266 - [JLOI2015]骗我呢(容斥原理+组合数学)
题面传送门 神仙题. 首先乍一看此题非常棘手,不过注意到有一个条件 \(0\le x_{i,j}\le m\),而整个矩阵恰好有 \(m\) 列,这就启发我们考虑将每个元素的上下界求出来,如果我们第一 ...
- 爬虫动态渲染页面爬取之Splash的介绍和使用
Splash是一个JavaScript渲染服务,是一个带有HTTP API的轻量级浏览器,同时它对接了Python中的Twisted和QT库.利用它,我们同样可以实现动态渲染页面的抓取. 1. 功能介 ...
- VMware和Centos的安装及配置
目录 1. 安装VMware 2. 安装CentOS6及配置 2.1 Centos安装 2.1.1 配置网络连接的三种形式 2.1.1.1 桥连接 2.1.1.2 NAT模式 2.1.1.3 主机模式 ...
- html5的canvas鼠标点击画圆
<!doctype html><html lang="en"> <head> <meta charset="UTF-8" ...
- Spark3学习【基于Java】3. Spark-Sql常用API
学习一门开源技术一般有两种入门方法,一种是去看官网文档,比如Getting Started - Spark 3.2.0 Documentation (apache.org),另一种是去看官网的例子,也 ...
- Apache RocketMQ分布式消息传递和流数据平台及大厂面试宝典v4.9.2
概述 **本人博客网站 **IT小神 www.itxiaoshen.com 定义 Apache RocketMQ官网地址 https://rocketmq.apache.org/ Latest rel ...
- 论文解读(SDNE)《Structural Deep Network Embedding》
论文题目:<Structural Deep Network Embedding>发表时间: KDD 2016 论文作者: Aditya Grover;Aditya Grover; Ju ...