Codeforces Round #838 (Div. 2)——A. Divide and Conquer

274 阅读3分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第21天,点击查看活动详情

Codeforces Round #838 (Div. 2)——A. Divide and Conquer

Problem - A - Codeforces

An array bb is good if the sum of elements of bb is even.

You are given an array aa consisting of nn positive integers. In one operation, you can select an index ii and change ai:=⌊ai2⌋ai:=⌊ai2⌋. ††

Find the minimum number of operations (possibly 00) needed to make aa good. It can be proven that it is always possible to make aa good.

†† ⌊x⌋⌊x⌋ denotes the floor function — the largest integer less than or equal to xx. For example, ⌊2.7⌋=2⌊2.7⌋=2, ⌊π⌋=3⌊π⌋=3 and ⌊5⌋=5⌊5⌋=5.

Input

Each test contains multiple test cases. The first line contains a single integer tt (1≤t≤10001≤t≤1000) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤501≤n≤50) — the length of the array aa.

The second line of each test case contains nn space-separated integers a1,a2,…,ana1,a2,…,an (1≤ai≤1061≤ai≤106) — representing the array aa.

Do note that the sum of nn over all test cases is not bounded.

Output

For each test case, output the minimum number of operations needed to make aa good.

Example

input

4
4
1 1 1 1
2
7 4
3
1 2 4
1
15

output

0
2
1
4

Note

In the first test case, array aa is already good.

In the second test case, we can perform on index 22 twice. After the first operation, array aa becomes 7,2. After performing on index 22 again, aa becomes 7,1, which is good. It can be proved that it is not possible to make aa good in less number of operations.

In the third test case, aa becomes 0,2,4 if we perform the operation on index 11 once. As 0,2,4 is good, answer is 11.

In the fourth test case, we need to perform the operation on index 11 four times. After all operations, aa becomes 0. It can be proved that it is not possible to make aa good in less number of operations.

问题解析

题意是说,如果数组的所有元素和是偶数,则这个数组是好的。现在你每次可以把ai变成ai/2,问把一个数组变成好的最少要几次操作。

遍历数组,计算遍历过的元素和sum,并且如果当前元素是奇数,则计算把它变成偶数需要多少次操作。如果是偶数,则计算变成奇数需要多少操作。维护操作次数的最小数。

如果sum已经是偶数了就输出0,如果是奇数,则直接输出维护的最小值。

因为把数组和变成偶数,要么把一个奇数元素变成偶数,要么把一个偶数元素变成奇数。

AC代码

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<math.h>
#include<set>
#include <random>
#include<numeric>
#include<string>
#include<string.h>
#include<iterator>
#include<fstream>
#include<map>
#include<unordered_map>
#include<stack>
#include<list>
#include<queue>
#include<iomanip>
#include<bitset>//#pragma GCC optimize(2)
//#pragma GCC optimize(3)#define endl '\n'
//#define int ll
#define PI acos(-1)
#define INF 0x3f3f3f3f
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PII;
const int N = 50 + 50, MOD = 1e9 + 7;
​
int a[N];
void solve()
{
    int n, sum = 0, mn = 1e9;
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
        sum += a[i];
        if (a[i] % 2 == 0)
        {
            int cnt = 0;
            while (a[i] % 2 == 0)
            {
                a[i] /= 2;
                cnt++;
            }
            mn = min(mn, cnt);
        }
        else if (a[i] % 2 == 1)
        {
            int cnt = 0;
            while (a[i] % 2 == 1)
            {
                a[i] /= 2;
                cnt++;
            }
            mn = min(mn, cnt);
        }
    }
    if (sum % 2 == 0)
    {
        cout << 0 << endl;
    }
    else cout << mn << endl;
}
​
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t = 1;
​
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}