codeforces 977D Divide by three, multiply by two

本文介绍了一个有趣的问题:给定一组打乱的数列,通过合理的排序算法将其重新组织成一个符合特定数学规则的有序序列。具体规则为:序列中的每个元素要么是前一个元素的两倍,要么是其三分之一(前提是能被3整除)。文章提供了详细的解决思路及算法实现,包括如何通过图论的方法来简化问题,并利用二分查找提高效率。

题目

Polycarp likes to play with numbers. He takes some integer number x , writes it down on the board, and then performs with it n1

operations of the two kinds:

  • divide the number x by 3 ( x must be divisible by 3 );
  • multiply the number x by 2.

After each operation, Polycarp writes down the result on the board and replaces x by the result. So there will be n numbers on the board after all.

You are given a sequence of length n — the numbers that Polycarp wrote down. This sequence is given in arbitrary order, i.e. the order of the sequence can mismatch the order of the numbers written on the board.

Your problem is to rearrange (reorder) elements of this sequence in such a way that it can match possible Polycarp’s game in the order of the numbers written on the board. I.e. each next number will be exactly two times of the previous number or exactly one third of previous number.

It is guaranteed that the answer exists.

Input
The first line of the input contatins an integer number n ( 2n100 ) — the number of the elements in the sequence. The second line of the input contains n integer numbers a1,a2,,an ( 1ai31018 ) — rearranged (reordered) sequence that Polycarp can wrote down on the board.

Output
Print n integer numbers — rearranged (reordered) input sequence that can be the sequence that Polycarp could write down on the board.

It is guaranteed that the answer exists.

Examples

Input
6
4 8 6 3 12 9
Output
9 3 6 12 4 8

Input
4
42 28 84 126
Output
126 42 84 28

Input
2
1000000000000000000 3000000000000000000
Output
3000000000000000000 1000000000000000000

Note
In the first example the given sequence can be rearranged in the following way: [9,3,6,12,4,8] . It can match possible Polycarp’s game which started with x=9 .

分析

【题意】
有一个原始串 a[n] ,每一位都满足下列的其中一个条件

  • a[i+1]=a[i]2
  • a[i+1]=a[i]/3 a[i] 必须整除3)

现在用打乱的顺序给出该串,输出原串。

【思路】
每个点有两种转移情况,而且转移不可逆,可以放到有向图上思考:给一个含有n个点,最多2*n条边的有向图,找出一条经过所有点的路径。暴力搜索一遍表面上看似有 2n 种情况,但其实对于每个点的搜索,它最多就把 n 个点访问一遍,也就是 n 次,所以对 n 个点全部搜索一遍总共不会超过 n2 次。

【注意】
搜索结束的控制:只有把所有的点不重复的遍历一次后,才算成功:保证遍历不重复的同时,在搜索中增加一维来记录已经遍历过的个数;
边的存在不是明显的,需要检查下一次要搜索的数是否存在,用二分检索以降低复杂度。

代码

#include<stdio.h>
#include<vector>
using std::vector;

#define N_max 105
typedef unsigned long long ll;
int g[N_max][N_max];
ll ipt[N_max];
int n;
vector<int>vis;
#define debug no_bug_at_all
ll ans[N_max];
int dfs(ll cur,int tot) {
    if (tot > n)return 1;
    //二分检查cur是否存在
    int l = -1, r = n,m;
    while (l+1 < r) {
        m = (l + r) / 2;
        if (ipt[m] <= cur)l = m;
        else r = m;
    }
    //不存在
    if (l == -1)return 0;  
    if(ipt[l] != cur)return 0;
    //已经访问过
    if (vis[l] == 1)return 0;
    //标记访问位置
    vis[l] = 1;
    if (cur % 3 == 0)
        if (1 == dfs(cur / 3, tot+1))
        {
            ans[tot] = cur;
            return 1;
        }
    if (1 == dfs(cur * 2, tot+1)) {
        ans[tot] = cur;
            return 1;
    }
    else { 
        vis[l] = 0;
        return 0; 
    }
}
int main() {

    scanf("%d", &n);
    for (int i = 0; i < n; ++i) 
        scanf("%llu", &ipt[i]);
    //排序以二分查找
    sort(ipt, ipt + n);
    for (int i = 0; i < n; ++i) {
         vis.resize(n);
         if (1 == dfs(ipt[i], 1))
         {
             for (int i = 1; i <= n; ++i) {
                 printf("%llu%c", ans[i], i == n? '\n' : ' ');
             }
             return 0;
         }
    }
}

总结

其实题目中的数字一定是非重复的,使用vis数组不仅没有意义,而且另一方面,假如有重复的,vis数组将会带来错误。考虑对重复数字的改进,可以对输入进行预处理,把重复的数字的个数记录到cnt数组,同时保证剩下的数没有重复;另一方面,搜索时要判断是否还有可用的数可以用(cnt[x]>0),每遍历到这个数字,就让cnt[x]–。

### Codeforces 1487D Problem Solution The problem described involves determining the maximum amount of a product that can be created from given quantities of ingredients under an idealized production process. For this specific case on Codeforces with problem number 1487D, while direct details about this exact question are not provided here, similar problems often involve resource allocation or limiting reagent type calculations. For instance, when faced with such constraints-based questions where multiple resources contribute to producing one unit of output but at different ratios, finding the bottleneck becomes crucial. In another context related to crafting items using various materials, it was determined that the formula `min(a[0],a[1],a[2]/2,a[3]/7,a[4]/4)` could represent how these limits interact[^1]. However, applying this directly without knowing specifics like what each array element represents in relation to the actual requirements for creating "philosophical stones" as mentioned would require adjustments based upon the precise conditions outlined within 1487D itself. To solve or discuss solutions effectively regarding Codeforces' challenge numbered 1487D: - Carefully read through all aspects presented by the contest organizers. - Identify which ingredient or component acts as the primary constraint towards achieving full capacity utilization. - Implement logic reflecting those relationships accurately; typically involving loops, conditionals, and possibly dynamic programming depending on complexity level required beyond simple minimum value determination across adjusted inputs. ```cpp #include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for(int i=0;i<n;++i){ cin>>a[i]; } // Assuming indices correspond appropriately per problem statement's ratio requirement cout << min({a[0], a[1], a[2]/2LL, a[3]/7LL, a[4]/4LL}) << endl; } ``` --related questions-- 1. How does identifying bottlenecks help optimize algorithms solving constrained optimization problems? 2. What strategies should contestants adopt when translating mathematical formulas into code during competitive coding events? 3. Can you explain why understanding input-output relations is critical before implementing any algorithmic approach? 4. In what ways do prefix-suffix-middle frameworks enhance model training efficiency outside of just tokenization improvements? 5. Why might adjusting sample proportions specifically benefit models designed for tasks requiring both strong linguistic comprehension alongside logical reasoning skills?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值