Codeforces Round #750 (Div. 2) D. Vupsen, Pupsen and 0(Codeforces Round #750 (Div. 2) D. Vupsen, Pupsen and 0)

原题链接

题目大意:给定n个数据a1,a2,a3,a4……an,求出bi,使得∑ai*bi = 0

解题思路:

考虑一种情况:a1对应的b1 = – b2 ,a2 对应的b2 = a1,那么a1b1 + a2b2 = -a1b2 + a1b2 = 0

那么对n为偶数的情况考虑,只需要对每两个a进行如上操作即可

对n为奇数的情况,考虑讲三个a中的两个化为一个,再转化成n为偶数的情况即可

AC代码:

#include<bits/stdc++.h>

using namespace std;

int main(){
    int n,T;
    scanf("%d",&T);

    while(T --){
        scanf("%d",&n);
        if(n % 2 == 0){
            int a,b;

            for(int i = 0;i < n;i += 2){
                scanf("%d%d",&a,&b);
                printf("%d %d ",-b,a);
            }
        }
        else {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);

            if(a + b != 0){
                printf("%d %d %d ",-c,-c,a + b);
            }

            else if(a + c != 0){
                printf("%d %d %d ",-b,a + c,-b);
            }
            else{
                printf("%d %d %d ",b + c,-a,-a);
            }
            
            for(int i = 3;i < n;i += 2){
                scanf("%d%d",&a,&b);
                printf("%d %d ",-b,a);
            }
        }
        printf("\n");
    }
    return 0;
}
————————

Original question link

Main idea of the topic: given n data A1, A2, A3, A4… An, find Bi so that Σ AI * Bi = 0

Problem solving ideas:

Consider a case: B1 corresponding to A1 = – B2  , a2   Corresponding B2 = A1, then A1B1 + a2b2 = – A1B2  +  a1b2  = 0

Then, for the case where n is an even number, you only need to perform the above operation for every two a

For the case where n is an odd number, consider transforming two of the three a into one, and then into the case where n is an even number

AC Code:

#include<bits/stdc++.h>

using namespace std;

int main(){
    int n,T;
    scanf("%d",&T);

    while(T --){
        scanf("%d",&n);
        if(n % 2 == 0){
            int a,b;

            for(int i = 0;i < n;i += 2){
                scanf("%d%d",&a,&b);
                printf("%d %d ",-b,a);
            }
        }
        else {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);

            if(a + b != 0){
                printf("%d %d %d ",-c,-c,a + b);
            }

            else if(a + c != 0){
                printf("%d %d %d ",-b,a + c,-b);
            }
            else{
                printf("%d %d %d ",b + c,-a,-a);
            }
            
            for(int i = 3;i < n;i += 2){
                scanf("%d%d",&a,&b);
                printf("%d %d ",-b,a);
            }
        }
        printf("\n");
    }
    return 0;
}