CF #318 C. Bear and Poker

C. Bear and Poker
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 int gcd(int a,int b) 
 7 { 
 8     if(a<b) 
 9         return gcd(b,a); 
10     else if(b==0) 
11         return a; 
12     else
13         return gcd(b,a%b); 
14 } 
15 
16 int main()
17 {
18     int n;
19     int i,j,k;
20     int a[100005];
21     scanf("%d",&n);
22     for(i=1;i<=n;i++)
23     {
24         scanf("%d",&a[i]);
25     }
26     int flg=1,x,y,z;
27     for(i=2;i<=n;i++)
28     {
29         z=gcd(a[i-1],a[i]);
30         x=a[i-1]/z,y=a[i]/z;
31         while(x>1)
32         {
33             if(x%2==0)
34                 x=x/2;
35             else if(x%3==0)
36                 x=x/3;
37             else
38             {
39                 flg=0;
40                 break;
41             }
42         }
43         while(y>1)
44         {
45             if(y%2==0)
46                 y=y/2;
47             else if(y%3==0)
48                 y=y/3;
49             else
50             {
51                 flg=0;
52                 break;
53             }
54         }
55         if(flg==0)
56             break;
57     }
58     if(flg==1)
59         printf("Yes\n");
60     else
61         printf("No\n");
62     return 0;
63 }
View Code

 

posted @ 2015-08-30 14:06  cyd2014  阅读(302)  评论(0编辑  收藏  举报