Codeforces Round #673 (Div. 2) D. Make Them Equal(数论/构造)
题目链接:https://codeforces.com/contest/1417/problem/D
题意
给出一个大小为 $n$ 的正整数数组 $a$ ,每次操作如下:
- 选择 $i,j$ 和 $x$,$(1 \le i, j \le n,\ 0 \le x \le 10^9)$
- 令 $a_i - x \cdot i,\ a_j + x \cdot i$
要求过程中不能有负数产生,问能否在 $3n$ 次操作内使数组中的每个元素相等,如果可以给出操作次数和过程。
题解
把每个 $a_i$ 变为 $i$ 的倍数后全部加给 $a_1$ ,最后从 $a_1$ 处均分即可。
证明
因为 $a_i \ge 1$,所以前 $i-1$ 个数一定可以补余使得第 $i$ 个数为 $i$ 的倍数。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n + 1);
int sum = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
sum += a[i];
}
if (sum % n != 0) {
cout << -1 << "\n";
continue;
}
cout << 3 * (n - 1) << "\n";
for (int i = 2; i <= n; i++) {
cout << 1 << ' ' << i << ' ' << (i - (a[i] % i)) % i << "\n";
cout << i << ' ' << 1 << ' ' << (a[i] + i - 1) / i << "\n";
}
for (int i = 2; i <= n; i++) {
cout << 1 << ' ' << i << ' ' << sum / n << "\n";
}
}
return 0;
}
Codeforces Round #673 (Div. 2) D. Make Them Equal(数论/构造)的更多相关文章
- Codeforces Round #673 (Div. 2)
[Codeforces Round #673 (Div. 2) ] 题目链接# A. Copy-paste 思路: 贪心的策略.每次只加上最小的就可以了 #include<bits/stdc++ ...
- Codeforces Round #673 (Div. 2) A. Copy-paste(贪心)
题目链接:https://codeforces.com/contest/1417/problem/A 题意 给出一个大小为 $n$ 的数组 $a$,每次操作可以选择两个数,然后将一个数加到另一个数上, ...
- Codeforces Round #673 (Div. 2) B. Two Arrays(数学)
题目链接:https://codeforces.com/contest/1417/problem/B 题意 定义 $f(a)$ 为数组 $a$ 中满足: $i < j$ $a_i + a_j = ...
- Codeforces Round #673 (Div. 2) C. k-Amazing Numbers(思维)
题目链接:https://codeforces.com/contest/1417/problem/C 题意 给出一个大小为 $n$ 的数组 $a$,计算当 $k$ 从 $1$ 到 $n$ 取值时在所有 ...
- Codeforces Round #673 (Div. 2) C. k-Amazing Numbers (DP,思维)
题意:有一组数,分别用长度从\([1,n]\)的区间去取子数组,要求取到的所有子数组中必须有共同的数,如果满足条件数组共同的数中最小的数,否则输出\(-1\). 题解:我们先从后面确定每两个相同数之间 ...
- Codeforces Round #673 (Div. 2) B. Two Arrays (贪心)
题意:给你一组数\(a\)和一个数\(T\),将这组数分为两组\(c\)和\(d\),定义\(f(x)\)为数组\(x\)中任意两个不同元素的和为\(T\)的个数,问为了使\(min(f(c)+f(d ...
- Codeforces Round #479 (Div. 3) C. Less or Equal
题目地址:http://codeforces.com/contest/977/problem/C 题解:给一串数组,是否找到一个数x,找到k个数字<=x,找到输出x,不能输出-1.例如第二组,要 ...
- Codeforces Round #292 (Div. 1)A. Drazil and Factorial 构造
A. Drazil and Factorial 题目连接: http://codeforces.com/contest/516/problem/A Description Drazil is play ...
- Codeforces Round #228 (Div. 1) B. Fox and Minimal path 构造
B. Fox and Minimal path 题目连接: http://codeforces.com/contest/388/problem/B Description Fox Ciel wants ...
随机推荐
- idea thymeleaf页面变量报错解决
IDEA在thymeleaf页面编写变量,如${user.id}会出现红色波浪下划线错误,提示Validates unresolved references and invalid expressio ...
- new ArrayList(0) 和 new ArrayList() 和一样吗?
第一感觉是一样的,盲猜后者调用了前者,并传入参数 0.然而,无论是 JDK 7 还是 JDK 8,这两个方法构造的结果都是不一样的.JDK 开发人员在这方面作了优化. JDK 7 在 Java 7 中 ...
- .NET 云原生架构师训练营(模块二 基础巩固 RabbitMQ Masstransit 详解)--学习笔记
2.6.7 RabbitMQ -- Masstransit 详解 Consumer 消费者 Producer 生产者 Request-Response 请求-响应 Consumer 消费者 在 Mas ...
- .NET 云原生架构师训练营(模块二 基础巩固 敏捷开发)--学习笔记
2.7.1 敏捷开发 敏捷介绍 敏捷的起源 敏捷软件开发宣言 敏捷开发十二原则 生命周期对比 敏捷开发的特点 敏捷的发展 敏捷的核心 敏捷的起源 2001年,17个老头子在一起一边滑雪,一边讨论工作, ...
- spring boot gateway 过滤器的执行顺序
前言 学习官方文档,发现对于过滤器有分为三类 默认过滤器 自定义过滤 全局过滤器 于是就有一个疑问,关于这些过滤器的访问顺序是怎样的,今天就以一个demo来进行测试 准备阶段 过滤器工厂类 以此为模板 ...
- 【Linux】用find删除大于30天的文件
1.删除文件命令: find 对应目录 -mtime +天数 -name "文件名" -exec rm -rf {} \; 实例命令:find /opt/soft/log/ -mt ...
- Test typora
目录 0. test 0.5 easy test 1. problem 1 2. problem 2 3. problem 3 import numpy as np import matplotlib ...
- 浏览器performance工具介绍及内存问题表现与监控内存的几种方式
一.GC的目的 为了实现内存空间的良性循环,performance提供多种监控方式监控内存 分析内存相关信息 当代码出现问题的时候及时定位到出现问题的代码块, 提高执行效率. preforcemanc ...
- SparkStreaming和Kafka基于Direct Approach如何管理offset实现exactly once
在之前的文章<解析SparkStreaming和Kafka集成的两种方式>中已详细介绍SparkStreaming和Kafka集成主要有Receiver based Approach和Di ...
- SpringBoot JPA简单使用
引自B站楠哥:https://www.bilibili.com/video/BV137411B7vB 一.新建Springboot项目 pom.xml文件 <?xml version=&qu ...