题目链接
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.
Input
The first line contains one integer n (1≤n≤100000): the number of elements of the array.
The second line contains n elements of an array a separated by spaces: a1,a2,…,an (−109≤ai≤109).
Output
The output should contain six lines representing three operations.
For each operation, print two lines:
The first line contains two integers l, r (1≤l≤r≤n): the bounds of the selected segment.
The second line contains r−l+1 integers bl,bl+1,…,br (−1018≤bi≤1018): the numbers to add to al,al+1,…,ar, respectively; bi should be divisible by r−l+1.
Example
inputCopy
4
1 3 2 4
outputCopy
1 1
-1
3 4
4 2
2 4
-3 -6 -6
题意
给定一个长度为n的数组,要求操作3次,使数组中的每一个元素都变为0(下标从1开始)
操作要求:每次可以选取一个区间,使得这个区间的所有元素都加上一个值k,要求k是区间差的倍数,即k mod (r-l+1) =0
思路
1.第一次操作,我们可以先选取n-1个区间,给区间中的每一个数都乘以n-1
2.第二次操作,我们将数组中所有的元素都去反,这样n-1个数将全部变为0
3.第三次操作,我们将剩余的一个数变为0即可
注意数值较大,要使用BigInteger
AC代码
import java.io.*;
import java.math.BigInteger;
public class Main {
public static void main(String[] args) throws IOException {
//题中数据较大,使用普通的输入输出流会超时
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
int n=Integer.parseInt(br.readLine());
String[] a=br.readLine().split(" ");
BigInteger[] arr=new BigInteger[n+1];
for (int i = 1; i <=n ; i++) {
arr[i]=new BigInteger(a[i-1]);
}
//特判n为1时
if (n==1){
bw.write("1 1"+"\n");
bw.write(""+arr[1].negate()+"\n");
bw.write("1 1"+"\n");
bw.write("0"+"\n");
bw.write("1 1"+"\n");
bw.write("0"+"\n");
bw.flush();
return;
}
BigInteger chu=arr[1];
//第一次操作
bw.write("2 "+n+"\n");
bw.flush();
for (int i = 2; i <=n ; i++) {
bw.write(""+arr[i].multiply(BigInteger.valueOf(n-1))+" ");
}
bw.write("\n");
bw.flush();
//第二次操作
bw.write("1 "+n+"\n");
for (int i = 1; i <=n ; i++) {
arr[i]=arr[i].multiply(BigInteger.valueOf(n));
bw.write(arr[i].negate()+" ");
}
bw.flush();
bw.write("\n");
bw.flush();
//第三次操作
bw.write("1 1"+"\n");
bw.write(chu.multiply(BigInteger.valueOf(n-1))+"");
bw.flush();
}
}