Codeforces Round #479 (Div. 3) D. Divide by three, multiply by two 【dfs】
发布日期:2021-06-29 14:28:59 浏览次数:2 分类:技术文章

本文共 785 字,大约阅读时间需要 2 分钟。

题意

一个数要么除以3,要么乘2,共记录n个数,但是n个数顺序打乱了,要你求新序列保证相互之间是两倍或者三分之一关系

思路

直接搜索完事,找到一条路就return 0,因为先考虑的是3再考虑2,最后打印的时候要反序输出。

code

#include
#define endl '\n'using namespace std;typedef long long ll;const int maxn=110;int n;ll a[maxn];map
vis;vector
v;bool dfs(ll x,int t){
bool flag=false; if(t==n){
v.push_back(x); return true; } if(x%3==0&&vis[x/3]){
if(dfs(x/3,t+1)) v.push_back(x),flag=true; } if(vis[x*2]){
if(dfs(x*2,t+1)) v.push_back(x),flag=true; } return flag;}int main(){
cin>>n; for(int i=0;i
>a[i]; vis[a[i]]=true; } for(int i=0;i
0;i--) cout<
<<" "; cout<
<
学如逆水行舟,不进则退

转载地址:https://chocolate.blog.csdn.net/article/details/104097995 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:VK Cup 2015 - Qualification Round 1, problem: (A) Reposts 【map】
下一篇:2020 愿远方依旧有篮球为伴 致敬24号 科比

发表评论

最新留言

不错!
[***.144.177.141]2024年05月01日 21时45分15秒