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()

全部评论

相关推荐

2025-12-19 15:17
门头沟学院 Java
27届中九本,目前陆陆续续也面了很多家厂了,大厂面了字节、腾讯、虾皮还有几家中小厂,全是一面挂,只有字节进二面,二面也是秒挂了。知道自己能力很差,基本上大厂面试题只要问题不是八股文,出一些场景题或者真实情况下的一些问题就不太答得上来,我感觉大多数面试官看我项目都是学习性质的项目没有部署上线,也没有面对真实场景好像就对我的项目没啥兴趣了,项目也不太拷打,就问几个简单的八股或者直接另出一个系统设计题(比如字节、虾皮),有一家中厂问的八股啥的还让我介绍项目重点,我就介绍然后正常回答八股,他也不追问,但是莫名就是一面挂了,也想问问大家有什么星球上的项目推荐嘛。目前项目就是一个点评魔改加一个图库烂大街,昨天面的腾讯的面试官人很好,也给我指出了一些建议,希望我深耕一些技术的实际场景不要堆砌中间件还要加深计算机基础知识的学习。因为楼主不是科班的,数据结构因为学过,Hot100也刷烂了,算法只要不是很难问题应该不大,但是计算机网络操作系统完全没学过,面腾讯和虾皮的时候完全不会被拷打了,感觉这些知识也不好速成,加上最近要期末考试了,学习技术的时间也要压缩分担给课上突击一下期末。想先沉淀半个多月,度过期末再做个项目之后继续投,想问一下各位大佬有什么意见?真的有点迷茫,感觉还要学好多才能达到找实习的水平,如果一月还找不到就打算考研了,那些真实场景确实没有接触过考虑不到,但是我都没有工作经验感觉很难锻炼这方面,也想问问大家该怎么提高这种真实场景思维,谢谢各位佬。
纳斯卡可:哥们大厂不要乱面啊,这些都是有面评的。你下次再想去面试都不会给你约了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务