题目链接: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$ 的倍数。

代码

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main() {
  4. ios::sync_with_stdio(false);
  5. cin.tie(nullptr);
  6. int t;
  7. cin >> t;
  8. while (t--) {
  9. int n;
  10. cin >> n;
  11. vector<int> a(n + 1);
  12. int sum = 0;
  13. for (int i = 1; i <= n; i++) {
  14. cin >> a[i];
  15. sum += a[i];
  16. }
  17. if (sum % n != 0) {
  18. cout << -1 << "\n";
  19. continue;
  20. }
  21. cout << 3 * (n - 1) << "\n";
  22. for (int i = 2; i <= n; i++) {
  23. cout << 1 << ' ' << i << ' ' << (i - (a[i] % i)) % i << "\n";
  24. cout << i << ' ' << 1 << ' ' << (a[i] + i - 1) / i << "\n";
  25. }
  26. for (int i = 2; i <= n; i++) {
  27. cout << 1 << ' ' << i << ' ' << sum / n << "\n";
  28. }
  29. }
  30. return 0;
  31. }

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. idea生成UML

    原文链接http://zhhll.icu/2020/12/18/idea/%E7%94%9F%E6%88%90UML/ 使用idea直接生成UML类图 然后点击所要生成的类即可生成 由于本身的博客百度 ...

  2. C# 设置默认关联程序

    以下代码做个Mark /// <summary> /// Create an associaten for a file extension in the windows registry ...

  3. 使用 C# 9 的records作为强类型ID - 初次使用

    强类型ID 实体通常是整数,GUID或者string类型,因为数据库直接支持这些类型,但是,如果实体的ID的类型是一样的,比如都是整数的ID,这有可能会出现ID值传错的问题,看下边的示例. publi ...

  4. git 遇到 fatal: loose object xxxx (stored in .git/objects/cb/xxxx) is corrupt 问题

    我的git版本是2.3.x,用下面这个参考链接的方法也可以解决 参考blog

  5. Spring Boot Scheduled定时任务特性

    SpringBoot中的Scheduled定时任务是Spring Boot中非常常用的特性,用来执行一些比如日切或者日终对账这种定时任务 下面说说使用时要注意的Scheduled的几个特性 Sched ...

  6. centos7虚拟机开启端口后 外部不能访问的问题

    转载 https://blog.csdn.net/u012045045/article/details/104219823 虚拟机新开了5005端口,系统内部是显示开了的(wget 192.168.4 ...

  7. 记一次 RocketMQ broker 因内存不足导致的启动失败

    原创:西狩 编写日期 / 修订日期:2020-01-12 / 2020-01-12 版权声明:本文为博主原创文章,遵循 CC BY-SA-4.0 版权协议,转载请附上原文出处链接和本声明. 背景 该小 ...

  8. Java 8中字符串拼接新姿势:StringJoiner

    介绍 StringJoiner是java.util包中的一个类,用于构造一个由分隔符分隔的字符序列(可选),并且可以从提供的前缀开始并以提供的后缀结尾.虽然这也可以在StringBuilder类的帮助 ...

  9. python中json模块的使用

    Python自带json模块,它有loads.dumps.load和dump这4个功能,用于Json格式字符串和Python数据类型间进行转换. 一.json.loads() 把Json格式字符串解码 ...

  10. 转 11 jmeter之图形监控扩展

    11 jmeter之图形监控扩展   Jmeter默认监听器的缺陷 Jmeter默认的监听器在表格.文字方面比较健全,但是在图形监控方面比较逊色,尤其在监控Windows或Linux的系统资源方面.但 ...