题目链接: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(数论/构造)的更多相关文章

  1. Codeforces Round #673 (Div. 2)

    [Codeforces Round #673 (Div. 2) ] 题目链接# A. Copy-paste 思路: 贪心的策略.每次只加上最小的就可以了 #include<bits/stdc++ ...

  2. Codeforces Round #673 (Div. 2) A. Copy-paste(贪心)

    题目链接:https://codeforces.com/contest/1417/problem/A 题意 给出一个大小为 $n$ 的数组 $a$,每次操作可以选择两个数,然后将一个数加到另一个数上, ...

  3. Codeforces Round #673 (Div. 2) B. Two Arrays(数学)

    题目链接:https://codeforces.com/contest/1417/problem/B 题意 定义 $f(a)$ 为数组 $a$ 中满足: $i < j$ $a_i + a_j = ...

  4. Codeforces Round #673 (Div. 2) C. k-Amazing Numbers(思维)

    题目链接:https://codeforces.com/contest/1417/problem/C 题意 给出一个大小为 $n$ 的数组 $a$,计算当 $k$ 从 $1$ 到 $n$ 取值时在所有 ...

  5. Codeforces Round #673 (Div. 2) C. k-Amazing Numbers (DP,思维)

    题意:有一组数,分别用长度从\([1,n]\)的区间去取子数组,要求取到的所有子数组中必须有共同的数,如果满足条件数组共同的数中最小的数,否则输出\(-1\). 题解:我们先从后面确定每两个相同数之间 ...

  6. Codeforces Round #673 (Div. 2) B. Two Arrays (贪心)

    题意:给你一组数\(a\)和一个数\(T\),将这组数分为两组\(c\)和\(d\),定义\(f(x)\)为数组\(x\)中任意两个不同元素的和为\(T\)的个数,问为了使\(min(f(c)+f(d ...

  7. Codeforces Round #479 (Div. 3) C. Less or Equal

    题目地址:http://codeforces.com/contest/977/problem/C 题解:给一串数组,是否找到一个数x,找到k个数字<=x,找到输出x,不能输出-1.例如第二组,要 ...

  8. Codeforces Round #292 (Div. 1)A. Drazil and Factorial 构造

    A. Drazil and Factorial 题目连接: http://codeforces.com/contest/516/problem/A Description Drazil is play ...

  9. 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 ...

随机推荐

  1. 基于 OpenMP 的奇偶排序算法的实现

    代码: #include <omp.h> #include <iostream> #include <cstdlib> #include <ctime> ...

  2. 【ORA】ORA-00371: not enough shared pool memory

    今天rac中有一个节点asm实例起不来包了ora-000371的错误,错误贴在下面: [oracle@rac2 dbs]$ srvctl start asm -n rac2 PRKS-1009 : F ...

  3. 【ORA】 ORA-01031:权限不足的问题

    今天创建一个用户,赋予dba权限,在plsql中选择sysdba登录,但是报错 ORA-01031 在网上找了好久最后的解决办法是 不仅仅要有dba权限 还要有这个权限: grant all priv ...

  4. CTFHub - Web(一)

    请求方法: 1.进入页面,提示:HTTP 请求方法, HTTP/1.1协议中共定义了八种方法(也叫动作)来以不同方式操作指定的资源. 2.当前http的请求方式是get请求,当你使用CTFHUB为请求 ...

  5. buuctf刷题之旅—web—EasySQL

    打开环境,发现依旧是sql注入 GitHub上有源码(https://github.com/team-su/SUCTF-2019/tree/master/Web/easy_sql) index.php ...

  6. 小试牛刀ElasticSearch大数据聚合统计

    ElasticSearch相信有不少朋友都了解,即使没有了解过它那相信对ELK也有所认识E即是ElasticSearch.ElasticSearch最开始更多用于检索,作为一搜索的集群产品简单易用绝对 ...

  7. 阿里云RDS物理备份恢复到本地

    一:业务场景 验证阿里云备份文件可用性 二:恢复到本地过程中遇到的问题 1.修改密码报错 2.自定义函数不可用 三:恢复步骤 1.xtrabackup安装使用 请参考:https://www.cnbl ...

  8. uni-app开发经验分享二: uni-app生命周期记录

    应用生命周期(仅可在App.vue中监听) 页面生命周期(在页面中添加) 当页面中需要用到下拉刷新功能时,打开pages.json,在"globalStyle"里设置"e ...

  9. 浅析Linux进程空间布局

    一.进程空间分布概述 对于一个进程,其空间分布如下图所示: 1.参数说明 程序段(Text):程序代码在内存中的映射,存放函数体的二进制代码. 初始化过的数据(Data):在程序运行初已经对变量进行初 ...

  10. MySQL调优用户监控之show processlist

    简介 show processlist显示这台MySQL正在连接的用户: mysql> show processlist; +----+------+-----------+-------+-- ...