Codeforces Round #479 (Div. 3) D - Divide by three, multiply by two

题目链接:点击打开链接

题目大意:开始有一个数x,你可以操作n-1次:1,除3(x必须被3整除才行);2,乘2。每操作一次,就把结果写在那个数的后面,所以最终你会得到n个数,现在你得到了一个乱序后的结果,让你输出可能的有序的游戏结果。

解题思路:由于能整除3才可能除3,所以看这些数的因子中3的个数,多的放在前面,少的放在后面,如果一样的话(比如12, 6),就把小的的放在前面(6, 12才能行得通)。

用到了一个比较巧妙的sort

还有就是数据范围较大应该开ll

代码:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<vector>
#include<map>
#include<set>
#define FAST ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int mod = (int)1e9 + 9;
const int maxn = (int)1e5 + 5;
using namespace std;

int three(ll n){
	int res = 0;
	while(!(n % 3)) res++, n /= 3;
	return res;
}

bool cmp(ll x, ll y){
	int thrx = three(x), thry = three(y);
	if(thrx == thry) return x < y;
	return thrx > thry;
}

ll a[105];

int main()
{
	int n;
	cin >> n;
	for(int i = 0; i < n; i++){
		cin >> a[i];
	}
	sort(a, a + n, cmp);
	for(int i = 0; i < n; i++){
		cout << a[i] << ' ';
	}
	cout << endl;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值