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组成相同的数.
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; }
Leave A Comment