码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)C. Bear and Poker(gcd模拟)

时间:2015-08-31 13:39:18      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

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.

任何数都可以表示成 x=2^n*3^n*num, 只要这些数中num都相同,就输出yes.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int gcd(int a,int b)    // 最大公约数模板
{
 if(a<b)    return gcd(b,a);
 int r;
 while(b) {r=a%b;a=b;b=r;}
 return a;
}


int lcm(int a,int b) {return a/gcd(a,b)*b;}  //最小公倍数模板

const int maxn=1e5+100;
int a[maxn];
int main()
{
	int n,i,j;
	int num;
	cin>>n;
	cin>>a[1];
	num=a[1];
	for(i=2;i<=n;i++) {
		cin>>a[i];
		num=gcd(num,a[i]);
	}
	while(num%2==0) num/=2;
	while(num%3==0) num/=3;
	for(i=1;i<=n;i++) {
		int tow=1,three=1;
		while(a[i]%(tow*2)==0) tow*=2;
		while(a[i]%(three*3)==0) three*=3;
		if(tow*three*num!=a[i]) {
			printf("No\n");
			return 0;
		}
	}
	printf("Yes\n");
	return 0;
}






版权声明:本文为博主原创文章,未经博主允许不得转载。

Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2)C. Bear and Poker(gcd模拟)

标签:

原文地址:http://blog.csdn.net/h1021456873/article/details/48131935

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!