ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

B. Lord of the Values 思维数学建构

2021-06-14 16:34:15  阅读:319  来源: 互联网

标签:建构 variables aj number ai int Values test Lord


原题链接 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;

  1. Perform assignment ai=ai+aj
  2. 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. 威廉想要你去研究一种策略使得所有内部变量是他想要的值。   Input

Each 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.

Output

For 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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有