C. Bear and Poker
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.
Output
Print "Yes" (without the quotes) if players can make their bids become equal, or "No" otherwise.
Sample test(s)
Input
475 150 75 50
Output
Yes
Input
3100 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.
题目大意是有N个人每个人有ai元,对于每个ai可以翻2倍或者翻3倍多次,问最后是否可以让这N个数相等。
一开始想直接用BFS搞的,但是发现ai太大的时候根本搞不了。于是换一种想法将乘法换成除法,对于每个数ai,如果ai%2==0那么就ai/=2直到ai%2!=0为止,如果ai%3==0那么ai/=3直到ai%3!=0为止,最后只需要检查一下所有的元素是否相等就可以得到答案了。
#include <cstdio> #include <cstring> #include <iostream> #include <queue> #include <vector> #include <stack> using namespace std; const int M = 10005; const int maxn = 5000000; typedef long long ll; vector<int>G[maxn]; queue<int>Q; stack<int>st; int a[maxn]; int main() { int n; int ans = 0; scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); while(a[i]%2==0) a[i] /= 2; while(a[i]%3==0) a[i] /= 3; } for(int i=2;i<=n;i++){ if(a[i] != a[1]){ ans = 1; break; } } if(ans) printf("No\n"); else printf("Yes\n"); return 0; }