标签:operation value sage possible call 一个 print var tco
Time limit : 2sec / Memory limit : 1000MB
Score: 300 points
As AtCoder Beginner Contest 100 is taking place, the office of AtCoder, Inc. is decorated with a sequence of length N, a={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≤i≤N, 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?
给你一个长度为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; }
标签:operation value sage possible call 一个 print var tco
原文地址:https://www.cnblogs.com/FJ-LinHua/p/9192854.html