標籤:
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
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.
題目大意是有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;}View Code
CF div2 318 C