B. Lord of the Values 思维数学建构
作者:互联网
原题链接 Problem - 1523B - Codeforces
题目及部分翻译
While trading on(贸易,利用) his favorite exchange trader William realized that he found a vulnerability( [ˌvʌlnərəˈbɪləti] n.易损性;弱点). Using this vulnerability he could change the values of certain internal variables(内部变量) to his advantage. To play around he decided to change the values of all internal variables from a1,a2,…,an to −a1,−a2,…,−an. For some unknown reason, the number of service variables is always an even number.
当和他最喜欢的交易员交易时, 威廉意识到他发现了一个弱点。使用这个弱点,他可以改变确定的内部变量成他的优势。他决定改变所有内部变量的值,从正改为负。因不明原因,服务变量的数目总是偶数。
William understands that with his every action he attracts more and more attention from the exchange's security team, so the number of his actions must not exceed 5000 and after every operation no variable can have an absolute value greater than 1018. William can perform actions of two types for two chosen variables with indices i and j, where i<j:
威廉知道,他的行为会吸引交易所安全团队越来越多的注意,所以他的操作数不能超过5000,并且每次操作完,没有变量的绝对值会超过10的18次方。威廉展示2种操作,其中的变量是i,j,满足i<j;
- Perform assignment ai=ai+aj
- Perform assignment aj=aj−ai
类型一:ai=ai+aj
类型二:aj=aj−ai
William wants you to develop a strategy that will get all the internal variables to the desired values. 威廉想要你去研究一种策略使得所有内部变量是他想要的值。 InputEach test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤20). Description of the test cases follows.
The first line of each test case contains a single even integer nn (2≤n≤103), which is the number of internal variables.
The second line of each test case contains nn integers a1,a2,…,an (1≤ai≤109), which are initial values of internal variables.
OutputFor each test case print the answer in the following format:
The first line of output must contain the total number of actions kk, which the strategy will perform. Note that you do not have to minimize kk. The inequality k≤5000k≤5000 must be satisfied.
Each of the next kk lines must contain actions formatted as "type i j", where "type" is equal to "1" if the strategy needs to perform an assignment of the first type and "2" if the strategy needs to perform an assignment of the second type. Note that i<j should hold.
We can show that an answer always exists.
解析: 对于任意两个元素,只需6步,可以使二者都变为负值
ai aj
ai+aj aj
ai+2aj aj
ai+2aj -ai-aj
aj -ai-aj
-ai -ai-aj
-ai -aj
代码
#include <iostream> using namespace std; const int N = 1e3+10; int a[N]; int main() { int t; cin >> t; while(t --) { int n; cin >> n; for(int i = 0; i < n; i ++) cin >> a[i]; cout << n * 3 << endl; for(int i = 1; i < n; i += 2) { int j = i; cout << "1 " << j <<' ' << j+1 << endl; cout << "1 " << j <<' ' << j+1 << endl; cout << "2 " << j <<' ' << j+1 << endl; cout << "1 " << j <<' ' << j+1 << endl; cout << "1 " << j <<' ' << j+1 << endl; cout << "2 " << j <<' ' << j+1 << endl; } } return 0; }
标签:建构,variables,aj,number,ai,int,Values,test,Lord 来源: https://www.cnblogs.com/la-la-wanf/p/14882661.html