Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) C. Bear and Poker | 书脊
http://codeforces.com/contest/574/problem/C
题目大意: 给一个n个数的数组, 问这几个数能不能通过double或者triple得到一个相同的数.
分析: 几个数能通过double或者triple得到一个相同的数, 必然这几个数由2 或者(和) 3的倍数组成, 所以这几个数可以分解成2^a*3^b* rest, 所以我们先要把每个数的2的倍数和3的倍数都消去, 然后检查rest部分是不是相同, 即可判断是不是可以通过double和triple组成相同的数.
http://codeforces.com/contest/574/problem/C
Limak is an old brown bear. He often plays poker with his friends. Today they went to a casino. There are n players (including Limak himself) and right now all of them have bids on the table. i-th of them has bid with size ai dollars.
Each player can double his bid any number of times and triple his bid any number of times. The casino has a great jackpot for making all bids equal. Is it possible that Limak and his friends will win a jackpot?
Input
First line of input contains an integer n (2 ≤ n ≤ 105), the number of players.
The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 109) — the bids of players.
4 75 150 75 50
output
Yes
input
3 100 150 250
output
No
Note
In the first sample test first and third players should double their bids twice, second player should double his bid once and fourth player should both double and triple his bid.
It can be shown that in the second sample test there is no way to make all bids equal.
分析: 几个数能通过double或者triple得到一个相同的数, 必然这几个数由2 或者(和) 3的倍数组成, 所以这几个数可以分解成2^a*3^b* rest, 所以我们先要把每个数的2的倍数和3的倍数都消去, 然后检查rest部分是不是相同, 即可判断是不是可以通过double和triple组成相同的数.
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.readInt();
int[] ary = IOUtils.readIntArray(in,n);
for (int i = 0; i < n; i++) {
ary[i] = modify(ary[i]);
}
for (int i = 1; i < n; i++) {
if (ary[i-1] != ary[i]) {
out.print("No");
return;
}
}
out.print("Yes");
}
public int modify (int n) {
int m = n;
while (m % 2 == 0)
m/=2;
while (m % 3 == 0)
m/=3;
return m;
}
Read full article from Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) C. Bear and Poker | 书脊