Codeforces Round #318 Bear and Poker(数学题)

题目链接:Bear and Poker


题目大意

给出n个数,问能否通过其中任意数乘以2或者3的倍数 , 来实现使这n个数变得相同。

Sample test(s)
input
4
75 150 75 50
output
Yes
input
3
100 150 250
output
No

第一组样例 75 * 2, 150 ,75 * 2 ,50 * 3;都变成了150;

第二组就不行。

开始用了求最小公倍数的做法,每两个数求出最小公倍数再算他们与最小公倍数的商是否为2 或者 3 的倍数或者是 1,如果不是就一定不能变成同样的数,然而这样做

每两个数的最小公倍数都要储存,然后用到下一个运算。这样最小公倍数随着数据的增大会变得非常大,,轻易超过long long ,然后我就在21组数据处错了。。


其实通过 如果乘2乘3能变成一个数 ,那么除2除3也能变成一个数。。

博大精深的数学啊。。

【源代码】

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
using namespace std;
int main(){
	int n;
	int tmp;
	set<int>st;
	while(scanf("%d",&n)!=EOF){
		st.clear();
		for(int i=0;i<n;i++){
			scanf("%d",&tmp);
			while(tmp % 3 == 0) tmp/=3;
			while(tmp % 2 == 0) tmp/=2;
			st.insert(tmp);
		}
		if(st.size() == 1)
			printf("Yes\n");
		else 
			printf("No\n");
	}
	return 0;
}


posted @ 2015-09-11 15:12  编程菌  阅读(178)  评论(0编辑  收藏  举报