很简单的题目啊。。
结果我还真的求了n次最小公倍数。其实只用求除了2和3以外的公因数乘积是不是想等就可以啦。
#include<cstring> #include<algorithm> #include<queue> #include<cstdio> #include<iostream> using namespace std; int n,st,en; int gcd(int a, int b) { int tmp = a; if(a<b) { a = b; b = tmp; } while(b!=0) { tmp = a%b; a = b; b = tmp; } return a; } bool check(int a, int b) { int c = gcd(a,b); int x1 = a/c, x2 = b/c; while(x1>1){ if(x1%2==0) x1/=2; else if(x1%3==0) x1/=3; else return false; } while(x2>1){ if(x2%2==0) x2/=2; else if(x2%3==0) x2/=3; else return false; } return true; } int main() { cin>>n>>st; int g; bool find = false; for(int i = 1; i< n; i++) { cin>>en; if(!check(st,en)){ find = true; break; } } if(find) cout<<"No"; else cout<<"Yes"; return 0; }
然而时间差别并不大。
#include<cstring> #include<algorithm> #include<queue> #include<cstdio> #include<iostream> using namespace std; int n,st,en; int resi(int x) { while(x%2 == 0) x/=2; while(x%3 == 0) x/=3; return x; } int main() { cin>>n>>st; int g; bool find = false; g = resi(st); for(int i = 1; i< n; i++) { cin>>en; if(g!=resi(en)){ find = true; break; } } if(find) cout<<"No"; else cout<<"Yes"; return 0; }