Codeforces977D ---Divide by three, multiply by two 深搜+map存出现的数

Posted Esquecer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces977D ---Divide by three, multiply by two 深搜+map存出现的数相关的知识,希望对你有一定的参考价值。

传送门:点我

题意:给定n长度的序列,重排成后一个数是前一个数除以三,或者后一个数是前一个数乘二,要求输出这个序列。

思路:大力深搜,对每个数搜除3的和乘2的是否出现过,然后继续搜下去。如果有一个数搜能搜完整个序列,那就输出这个数开始的一整个序列。

代码:

#include<bits/stdc++.h> 
using namespace std;
typedef unsigned long long LL;
int ans = 0,flag = 0;
int n,k;
map<LL,int>ma;
LL b[101];
void dfs(LL x){
    if(ans == n - 1){
        flag = 1;
        for(int i = 0 ; i <= ans; i++){
            i == ans ?printf("%I64u\n",b[i]):printf("%I64u ",b[i]);
        }
        return;
    }
    if(x%3==0&&ma[x/3] == 1){
        ans++;
        b[ans] = x/3;
        dfs(x/3);
    }
    if(ma[x*2] == 1){
        ans++;
        b[ans] = x*2;
        dfs(x*2); 
    }
}
int main(){
    scanf("%d",&n);
    LL a[101];
    for(int i = 0 ; i < n ; i ++){
        cin>>a[i];
        ma[a[i]] = 1;
    }
    vector<LL>v;
    for(int i = 0 ; i < n ; i++){
        ans = 0;
        b[0] = a[i];
        dfs(a[i]);
        if(flag){
            return 0;
        }
    }
}

 

以上是关于Codeforces977D ---Divide by three, multiply by two 深搜+map存出现的数的主要内容,如果未能解决你的问题,请参考以下文章

CodeForces - 1609A Divide and Multiply

Divide by Three CodeForces - 792C

codeforces 792C. Divide by Three

Educational Codeforces Round 18 C. Divide by Three DP

CodeForces 792C - Divide by Three [ 分类讨论 ]

Journey CodeForces - 1336F[data structures divide and conquer graphs trees]