A. Finding Sasuke(构造思维)Codeforces Round #679 (Div. 2, based on Technocup 2021 Elimination Round 1)

原题链接: https://codeforces.com/contest/1435/problem/A

在这里插入图片描述
测试样例

input
2
2
1 100
4
1 2 3 6
output
-100 1
1 1 1 -1

Note

For the first door Naruto can use energies [ − 100 , 1 ] [−100,1] [100,1]. The required equality does indeed hold: 1 ⋅ ( − 100 ) + 100 ⋅ 1 = 0 1⋅(−100)+100⋅1=0 1(100)+1001=0.

For the second door Naruto can use, for example, energies [ 1 , 1 , 1 , − 1 ] [1,1,1,−1] [1,1,1,1]. The required equality also holds: 1 ⋅ 1 + 2 ⋅ 1 + 3 ⋅ 1 + 6 ⋅ ( − 1 ) = 0 1⋅1+2⋅1+3⋅1+6⋅(−1)=0 11+21+31+6(1)=0.

题意: 给定一个长度为 n n n(其中 n n n为偶数)的整数序列,你需要寻找一个长度为 n n n整数序列 b b b,使得: a 1 ⋅ b 1 + a 2 ⋅ b 2 + . . . + a n ⋅ b n = 0 a_{1} \cdot b_{1} + a_{2} \cdot b_{2} + ... + a_{n} \cdot b_{n} = 0 a1b1+a2b2+...+anbn=0

解题思路: 对于这类题型,整数序列存在不确定性,我们当然不能对其进行情况讨论,所以我们需要去想办法构造一个 b b b数组。我们往大的看,如果我们将数组 a a a的前一半变为它的相反数,再反转这个数组,这就是我们要构建的 b b b数组,即: a n , a n − 1 , . . . − a 2 , − a 1 a_{n},a_{n-1},...-a_{2},-a_{1} an,an1,...a2,a1 这样这个数组与 a a a数组进行运算后即可得 0 0 0,故此题易解。

AC代码

/*
*邮箱:[email protected]
*blog:https://me.csdn.net/hzf0701
*注:文章若有任何问题请私信我或评论区留言,谢谢支持。
*
*/
#include<bits/stdc++.h>	//POJ不支持

#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll>  pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//

int t,n;
int a[maxn];
int main(){
    
    
	//freopen("in.txt", "r", stdin);//提交的时候要注释掉
	IOS;
	while(cin>>t){
    
    
		while(t--){
    
    
			cin>>n;
			rep(i,1,n){
    
    
				cin>>a[i];
			}
			per(i,n,1){
    
    
				if(i>n/2){
    
    
					cout<<(-1)*a[i]<<" ";
				}
				else{
    
    
					cout<<a[i]<<" ";
				}
			}
			cout<<endl;
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/hzf0701/article/details/109310123