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!



你可能感兴趣的:(Algorithm,ACM,topcoder)