[ABC 100] C-*3 or /2

Posted fj-linhua

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[ABC 100] C-*3 or /2相关的知识,希望对你有一定的参考价值。

C - *3 or /2


Time limit : 2sec / Memory limit : 1000MB

Score: 300 points

Problem Statement

As AtCoder Beginner Contest 100 is taking place, the office of AtCoder, Inc. is decorated with a sequence of length Na={a1,a2,a3,…,aN}.
Snuke, an employee, would like to play with this sequence.

Specifically, he would like to repeat the following operation as many times as possible:

For every i satisfying 1≤iN, perform one of the following: "divide ai by 2" and "multiply ai by 3".  
Here, choosing "multiply ai by 3" for every i is not allowed, and the value of ai after the operation must be an integer.

At most how many operations can be performed?

Constraints

  • N is an integer between 1 and 10 000 (inclusive).
  • ai is an integer between 1 and 1 000 000 000 (inclusive).

[题目解释]

  给你一个长度为N的序列,你可以对这个序列进行*3或/2的操作,操作后的数必须是个整数,但是不能只乘以3,问最多可以操作几次.

[题目解析]

  因为任何数*3后均是一个整数,但是/2后是整数的数一定是一个偶数,又因为每一个数都可以唯一表示成一些质数的乘积,而每个数/2的次数取决于这个数含有2这个质因子个数的多少,所以我们考虑每一次只将任意一个含有质因子2的数/2剩下的数均乘3以保证操作次数最大,因为乘3后并不改变所有数含有2质因子数,我们只要知道所有数的质因子2的个数总和就是做多可以操作的质数.

[代码]

/*
    Name: *3 or /2 
    Author: FZSZ-LinHua
    Date: 2018 06 16
    Exec time: 2ms
    Memory usage: 256KB
    Score: 300
    Algorithm: Bit operation 
*/
# include "cstdio"
# include "iostream"

using namespace std;

inline int n_lowbit(int x){    //取出含有质因子2的个数 
    int count=0;
    while(x){
        if(x&1){
            return count; 
        }
        count++; 
        x>>=1; 
    }
    return count; 
} 

int
    ans, //记录答案 
    N, 
    a; 

int main(){
    scanf("%d",&N);
    while(N--){
        scanf("%d",&a);
        ans+=n_lowbit(a); //更新答案 
    }
    printf("%d",ans); 
    return 0; 
} 

 


以上是关于[ABC 100] C-*3 or /2的主要内容,如果未能解决你的问题,请参考以下文章

[linux][c/c++]代码片段01

Android从后台堆栈中删除事务

我的C语言学习进阶之旅解决 Visual Studio 2019 报错:错误 C4996 ‘fscanf‘: This function or variable may be unsafe.(代码片段

我的C语言学习进阶之旅解决 Visual Studio 2019 报错:错误 C4996 ‘fscanf‘: This function or variable may be unsafe.(代码片段

C语言100个经典算法源码片段

使用 OR 连接的性能