CF div2 318 C

來源:互聯網
上載者:User

標籤:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.