码迷,mamicode.com
首页 > 其他好文 > 详细

[ABC 100] C-*3 or /2

时间:2018-06-17 15:16:01      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:operation   value   sage   possible   call   一个   print   var   tco   

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

标签:operation   value   sage   possible   call   一个   print   var   tco   

原文地址:https://www.cnblogs.com/FJ-LinHua/p/9192854.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!