Epic Coloring#11836

Epic Coloring (Editoryal)

Problem Author:

Hasan Saleh (MathModel)

Prepared by:

Hasan Saleh (MathModel)

Edited by:

Hasan Saleh (MathModel)

Solution 1

Notice there are only two cases of parity of sum of the array , let's consider when is odd , let's consider all possible choices for pair sum :

  • and thus complementary is .

  • and thus complementary is .

  • and thus complementary is

Notice in all configurations none of them matches the parity , thus the answer is when (\sum a \bmod 2=1).

Now let's consider when is even , we've all possible configurations :

  • and thus complementary is .

  • and thus complementary is .

  • and thus complementary is

Notice that all configurations match, thus we consider all pairings which is

\sum_{i=1}^{n-1} i = 1+2+3+...+(n-1)=\frac{n(n-1)}{2}

Complexity .

#include<bits/stdc++.h>
using namespace std;
#define ll long long
void solve(){
    int n;cin>>n;
    vector<int>a(n);
    ll sum=0;
    for(auto &i:a){
        cin>>i;
        sum+=i;
    }
    if(sum&1){
        cout<<"0\n";
    }else{
        cout<<(ll)(n*(n-1))/2<<"\n";
    }
}
int main(){
    int t;cin>>t;
    while(t--){
        solve(); 
    }
    return 0;
}

Solution 2

We can sequentially pair every parity with it's corresponding one and accumulate the count each time , each time we'll pair even with odds and we accumulate the count simply. Only this works when is even , otherwise it's 0.

#include<bits/stdc++.h>
using namespace std;
#define ll long long
void solve(){
    int n;cin>>n;
    vector<int>a(n);
    int o=0;int e=0;
    ll sum=0;
    ll ans=0;
    for(auto i:a){
        cin>>i;
        sum+=i;
    }
    if(sum&1){
        cout<<"0\n";
        return;
    }else{
        for(auto i:a){
            ans+=e+o;
            if(i&1){
                e++;
            }else{
                o++;
            }
        }
    }
    cout<<ans<<"\n";
}
int main(){
    int t;cin>>t;
    while(t--){
        solve(); 
    }
    return 0;
}

Complexity .