CodeForces 1277 B Make Them Odd

Description:

There are n positive integers a 1 , a 2 , … , a n a_{1},a_{2},…,a_{n} a1,a2,,an. For the one move you can choose any even value c and divide by two all elements that equal c.

For example, if a = [ 6 , 8 , 12 , 6 , 3 , 12 ] a=[6,8,12,6,3,12] a=[6,8,12,6,3,12] and you choose c = 6 c=6 c=6, and a is transformed into a = [ 3 , 8 , 12 , 3 , 3 , 12 ] a=[3,8,12,3,3,12] a=[3,8,12,3,3,12] after the move.

You need to find the minimal number of moves for transforming a to an array of only odd integers (each element shouldn’t be divisible by 2 2 2).

Input

The first line of the input contains one integer t ( 1 ≤ t ≤ 1 0 4 ) t (1≤t≤10^4) t(1t104) — the number of test cases in the input. Then t t t test cases follow.

The first line of a test case contains n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 ) n (1≤n≤2⋅10^5) n(1n2105) — the number of integers in the sequence a a a. The second line contains positive integers a 1 , a 2 , … , a n ( 1 ≤ a i ≤ 1 0 9 ) . a_{1},a_{2},…,a_{n} (1≤a_{i}≤10^9). a1,a2,,an(1ai109).

The sum of n n n for all test cases in the input doesn’t exceed 2 ⋅ 1 0 5 . 2⋅10^5. 2105.

Output

For t test cases print the answers in the order of test cases in the input. The answer for the test case is the minimal number of moves needed to make all numbers in the test case odd ( i . e i.e i.e. not divisible by 2 2 2).

Example

input

4
6
40 6 40 3 20 1
1
1024
4
2 4 8 16
3
3 1 7

output

4
10
4
0

Note

In the first test case of the example, the optimal sequence of moves can be as follows:

before making moves a = [ 40 , 6 , 40 , 3 , 20 , 1 ] ; a=[40,6,40,3,20,1]; a=[40,6,40,3,20,1];
choose c = 6 c=6 c=6;
now a = [ 40 , 3 , 40 , 3 , 20 , 1 ] a=[40,3,40,3,20,1] a=[40,3,40,3,20,1];
choose c = 40 c=40 c=40;
now a = [ 20 , 3 , 20 , 3 , 20 , 1 ] ; a=[20,3,20,3,20,1]; a=[20,3,20,3,20,1];
choose c = 20 ; c=20; c=20;
now a = [ 10 , 3 , 10 , 3 , 10 , 1 ] ; a=[10,3,10,3,10,1]; a=[10,3,10,3,10,1];
choose c = 10 ; c=10; c=10;
now a = [ 5 , 3 , 5 , 3 , 5 , 1 ] a=[5,3,5,3,5,1] a=[5,3,5,3,5,1] — all numbers are odd.
Thus, all numbers became odd after 4 4 4 moves. In 3 3 3 or fewer moves, you cannot make them all odd.

题意: 给出 n n n个数字,每次可以选特定的数字除 2 2 2,求最少需要操作多少次才能让整个序列都变成奇数序列,既然要操作最少那么肯定从大的开始删除数字,有一个 s e t set set来排序去重。

AC代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define sd(n) scanf("%d", &n)
#define sdd(n, m) scanf("%d%d", &n, &m)
#define sddd(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define pd(n) printf("%d\n", n)
#define pc(n) printf("%c", n)
#define pdd(n, m) printf("%d %d", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n, m) printf("%lld %lld\n", n, m)
#define sld(n) scanf("%lld", &n)
#define sldd(n, m) scanf("%lld%lld", &n, &m)
#define slddd(n, m, k) scanf("%lld%lld%lld", &n, &m, &k)
#define sf(n) scanf("%lf", &n)
#define sc(n) scanf("%c", &n)
#define sff(n, m) scanf("%lf%lf", &n, &m)
#define sfff(n, m, k) scanf("%lf%lf%lf", &n, &m, &k)
#define ss(str) scanf("%s", str)
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, a, n) for (int i = n; i >= a; i--)
#define mem(a, n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mod(x) ((x) % MOD)
#define gcd(a, b) __gcd(a, b)
#define lowbit(x) (x & -x)
typedef pair<int, int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int MOD = 1e9 + 7;
const double eps = 1e-9;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
inline int read()
{
    int ret = 0, sgn = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            sgn = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        ret = ret * 10 + ch - '0';
        ch = getchar();
    }
    return ret * sgn;
}
inline void Out(int a)
{
    if (a > 9)
        Out(a / 10);
    putchar(a % 10 + '0');
}

ll gcd(ll a, ll b)
{
    return b == 0 ? a : gcd(b, a % b);
}

ll lcm(ll a, ll b)
{
    return a * b / gcd(a, b);
}
///快速幂m^k%mod
ll qpow(int m, int k, int mod)
{
    ll res = 1, t = m;
    while (k)
    {
        if (k & 1)
            res = res * t % mod;
        t = t * t % mod;
        k >>= 1;
    }
    return res;
}

// 快速幂求逆元
int Fermat(int a, int p) //费马求a关于b的逆元
{
    return qpow(a, p - 2, p);
}

///扩展欧几里得
int exgcd(int a, int b, int &x, int &y)
{
    if (b == 0)
    {
        x = 1;
        y = 0;
        return a;
    }
    int g = exgcd(b, a % b, x, y);
    int t = x;
    x = y;
    y = t - a / b * y;
    return g;
}

///使用ecgcd求a的逆元x
int mod_reverse(int a, int p)
{
    int d, x, y;
    d = exgcd(a, p, x, y);
    if (d == 1)
        return (x % p + p) % p;
    else
        return -1;
}

///中国剩余定理模板
ll china(int a[], int b[], int n) //a[]为除数,b[]为余数
{
    int M = 1, y, x = 0;
    for (int i = 0; i < n; ++i) //算出它们累乘的结果
        M *= a[i];
    for (int i = 0; i < n; ++i)
    {
        int w = M / a[i];
        int tx = 0;
        int t = exgcd(w, a[i], tx, y); //计算逆元
        x = (x + w * (b[i] / t) * x) % M;
    }
    return (x + M) % M;
}

int n, t, x;
int ans, res, cnt, temp;
set<int> v;
int main()
{
    sd(t);
    while (t--)
    {
        sd(n);
        ans = 0;
        v.clear();
        rep(i, 1, n)
        {
            sd(x);
            if (x % 2 == 0)
                v.insert(x);
        }
        while (!v.empty())
        {
            auto i = --v.end();
            int k = *i;
            v.erase(i);
            ans++;
            k = k / 2;
            if (k & 1)
                continue;
            else
                v.insert(k);
        }
        pd(ans);
    }
    return 0;
}

你可能感兴趣的:(CodeForces)