Codeforces Round #666 (Div. 2) ------ Multiples of Length

题目

You are given an array a of n integers.

You want to make all elements of a equal to zero by doing the following operation exactly three times:

Select a segment, for each number in this segment we can add a multiple of len to it, where len is the length of this segment (added integers can be different).

It can be proven that it is always possible to make all elements of a equal to zero.

题解:

这题的大致题意即给定你一个长度为n的数组,然后进行3次操作(不可以多于3次,也不可以少于三次)。操作要求如下:选定一个区间,区间长度为r - l + 1,这个区间内每个数都可以加上m * (r - l + 1),m为任意整数,且对于每个数m的值都可以不同。
求解方法:这里采用一种特殊的通解
第一次操作:选定区间1-(n - 1),是其里面的数a[i]都变成n * a[i],即加上(n - 1)* a[i],符合操作要求
第二次操作:对最后一个数a[n]进行操作,使其变为0
第三次操作:选定区间1 - n,减去n * a[i], 使其整个数组变为0;

AC代码

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<map>
#include<string>
using namespace std;
#define ll long long
const int N = 1e5 + 15;
ll a[N];
int main()
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);
	int n; cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	if (n == 1) {
		cout << 1 << " " << 1 << endl;
		cout << -a[0] << endl;
		cout << 1 << " " << 1 << endl;
		cout << 0 << endl;
		cout << 1 << " " << 1 << endl;
		cout << 0 << endl;
	}
	else {
		cout << "1 " << n - 1 << endl;
		for (int i = 0; i < n - 1; i++) {
			cout << a[i] * (n - 1) << " ";
			a[i] = n * a[i];
		}
		cout << endl;
		cout << n << " " << n << endl;
		cout << -a[n - 1] << endl;
		a[n - 1] = 0;
		cout << 1 << " " << n << endl;
		for (int i = 0; i < n; i++)
			cout << -a[i] << " ";
	}
	return 0;
}

Python代码

n = int(input())
a = []
a = list(map(int, input().split()))
if n == 1:
    print("1 1")
    print(-a[0])
    print("1 1")
    print(0)
    print("1 1")
    print(0)
else:
    print("1 {}".format(n - 1))
    for i in range(n - 1):
        print("{}".format((n - 1) * a[i]), end = " ")
        a[i] = n * a[i]
    print()
    print("{} {}".format(n, n))
    print(-a[n - 1])
    a[n - 1] = 0
    print("{} {}".format(1, n))
    for i in range(n):
        print("{}".format(-a[i]), end = " ")
    print()

全部评论

相关推荐

点赞 收藏 评论
分享
正在热议
# 牛客帮帮团来啦!有问必答 #
1150893次浏览 17147人参与
# 通信和硬件还有转码的必要吗 #
11181次浏览 101人参与
# OPPO开奖 #
19186次浏览 267人参与
# 和牛牛一起刷题打卡 #
18840次浏览 1635人参与
# 实习与准备秋招该如何平衡 #
203333次浏览 3625人参与
# 大厂无回复,继续等待还是奔赴小厂 #
4959次浏览 30人参与
# 不去互联网可以去金融科技 #
20303次浏览 255人参与
# 通信硬件薪资爆料 #
265864次浏览 2484人参与
# 国企是理工四大天坑的最好选择吗 #
2207次浏览 34人参与
# 互联网公司评价 #
97661次浏览 1280人参与
# 简历无回复,你会继续海投还是优化再投? #
25033次浏览 354人参与
# 0offer是寒冬太冷还是我太菜 #
454786次浏览 5124人参与
# 国企和大厂硬件兄弟怎么选? #
53892次浏览 1012人参与
# 参加过提前批的机械人,你们还参加秋招么 #
14631次浏览 349人参与
# 硬件人的简历怎么写 #
82283次浏览 852人参与
# 面试被问第一学历差时该怎么回答 #
19386次浏览 213人参与
# 你见过最离谱的招聘要求是什么? #
28012次浏览 248人参与
# 学历对求职的影响 #
161207次浏览 1804人参与
# 你收到了团子的OC了吗 #
538651次浏览 6386人参与
# 你已经投递多少份简历了 #
344144次浏览 4963人参与
# 实习生应该准时下班吗 #
96956次浏览 722人参与
# 听劝,我这个简历该怎么改? #
63514次浏览 622人参与
牛客网
牛客企业服务