SRM 597 DIV2 250

Problem Statement

 

Little Elephant from the Zoo of Lviv likes integers.

You are given an vector <int> A. On a single turn, Little Elephant can double (i.e., multiply by 2) any element of A. He may double the same element more than once, if he wants to. He wants to obtain an array in which all elements are equal. Return "YES" (quotes for clarity) if it is possible to do that and "NO" otherwise.

Definition

 
Class:LittleElephantAndDouble
Method:getAnswer
Parameters:vector <int>
Returns:string
Method signature:string getAnswer(vector <int> A)
(be sure your method is public)
 
 

Notes

-The return value is case-sensitive. Make sure that you return the exact strings "YES" and "NO".

Constraints

-A will contain between 1 and 50 elements, inclusive.
-Each element of A will be between 1 and 1,000,000,000, inclusive.

Examples

0) 
 
{1, 2}
Returns: "YES"
One possible way of making all elements equal is to double the element at index 0.
1) 
 
{1, 2, 3}
Returns: "NO"
It's impossible to make all three elements equal in this case.
2) 
 
{4, 8, 2, 1, 16}
Returns: "YES"
3) 
 
{94, 752, 94, 376, 1504}
Returns: "YES"
4) 
 
{148, 298, 1184}
Returns: "NO"
5) 
 
{7, 7, 7, 7}
Returns: "YES"

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.     



#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>

using namespace std;

class LittleElephantAndDouble {
public:
	string getAnswer(vector <int>);
};

bool ck(int x)
{
    int lb=x&(-x);
    return x==lb;
}

string LittleElephantAndDouble::getAnswer(vector <int> A) {
    int Max=-999999,t=A.size();
    bool flag=true;
    string Yes("YES"),No("NO");
    for(int i=0;i<t;i++) Max=max(Max,A[i]);
    for(int i=0;i<t;i++)
    {
        if(A[i]==Max) continue;
        if(Max%A[i]!=0) {flag=false;break;}
        if(ck(Max/A[i])==false) {flag=false;break;}
    }
    if(flag) return Yes;
    else return No;
}

//<%:testing-code%>
//Powered by [KawigiEdit] 2.0!



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值