【codeforces 727 A】【dfs或者逆向思维】【给你两个操作,问能不能把数字a变成b】
来源:互联网 发布:函数编程题的心得体会 编辑:程序博客网 时间:2024/05/22 01:37
传送门:A. Transformation: from A to B
描述:
Vasily has a number a, which he wants to turn into a number b. For this purpose, he can do two types of operations:
- multiply the current number by 2 (that is, replace the number x by 2·x);
- append the digit 1 to the right of current number (that is, replace the number x by 10·x + 1).
You need to help Vasily to transform the number a into the number b using only the operations described above, or find that it is impossible.
Note that in this task you are not required to minimize the number of operations. It suffices to find any way to transform a into b.
The first line contains two positive integers a and b (1 ≤ a < b ≤ 109) — the number which Vasily has and the number he wants to have.
If there is no way to get b from a, print "NO" (without quotes).
Otherwise print three lines. On the first line print "YES" (without quotes). The second line should contain single integer k — the length of the transformation sequence. On the third line print the sequence of transformations x1, x2, ..., xk, where:
- x1 should be equal to a,
- xk should be equal to b,
- xi should be obtained from xi - 1 using any of two described operations (1 < i ≤ k).
If there are multiple answers, print any of them.
2 162
YES52 4 8 81 162
4 42
NO
100 40021
YES5100 200 2001 4002 40021
题意:
给你两个操作,问能不能把数字a变成b
思路一:
这题大多数一眼看就是dfs
#include <bits/stdc++.h>#define ll __int64using namespace std;typedef vector<ll> vi;template<class T> T sqr(T x){ return x * x; }template<class T> T gcd(T a, T b){ return b ? gcd(b, a%b) : a; }template<class T> void read(T&num) { char CH; bool F=false; for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar()); for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar()); F && (num=-num);}int stk[70], tp;template<class T> inline void print(T p) { if(!p) { puts("0"); return; } while(p) stk[++ tp] = p%10, p/=10; while(tp) putchar(stk[tp--] + '0'); putchar('\n');}ll a,b;vi v;void dfs(ll x){ if(x>b)return ; v.push_back(x); if(x==b){ puts("YES"); print(v.size()); for(auto x : v){ printf("%I64d ", x); } puts(""); exit(0); } dfs(2*x); dfs(10*x + 1); v.pop_back();} int main(){ read(a),read(b); dfs(a); puts("NO"); return 0;}
思路二:
逆向思维,尝试把b变成a#include <bits/stdc++.h>using namespace std;const int N = 105;int a, b, ans[N], sz;int main() { cin >> a >> b; while (a < b) { ans[sz++] = b; if (b % 10 == 1) { b /= 10; } else if (b % 2 == 0) { b /= 2; } else { cout << "NO" << endl; exit(0); } } if (a != b) { cout << "NO" << endl; exit(0); } cout << "YES" << endl; ans[sz++] = a; cout << sz << endl; for (int i = sz - 1; i >= 0; --i) cout << ans[i] << " "; cout << endl; return 0;}
- 【codeforces 727 A】【dfs或者逆向思维】【给你两个操作,问能不能把数字a变成b】
- 【codeforces 733 C】【模拟】C. Epidemic in Monstropolis【给你n个数,再给你k个数,问你a序列能否通过一定规则合并变成b序列】
- 【codeforces gym 100187J】 【dfs判连通】Deck Shuffling 【给你一堆牌和一些洗牌机,后者可以改变牌的顺序,问你能不能把数字为x的牌洗到第一个位置。】
- 给你两个数 a 和 b , 请你计算 a b 之间所有数的数字之和
- 京东 给你两个集合,要求 {A} + {B}。
- Codeforces 377A Maze【Bfs+逆向思维】
- 给定两个正整数A和B,把A变成B需要几位?也就是说A和B之间的位数有多少个是不同的?
- 比较两个数字a,b的大小
- 搜狗笔试:有两个数,A和B,六种操作分别是+12,-12,+7,-7,+5,-5。A经过若干次操作,变成B
- 搜狗笔试:有两个数,A和B,六种操作分别是+12,-12,+7,-7,+5,-5。A经过若干次操作,变成B
- 搜狗笔试:有两个数,A和B,六种操作分别是+12,-12,+7,-7,+5,-5。A经过若干次操作,变成B
- 算法题19 给你三个n*n的矩阵A、B、C,问A*B是不是等于C?
- codeforces-754【A思维】
- Codeforces 519D - A and B and Interesting Substrings (思维)
- Codeforces-448【A数学函数ceil】【B思维】【D二分】
- Codeforces 598B Queries on a String 【思维】
- codeforces-445【A思维】【B并查集】
- Codeforces Round #384 (Div. 2) (A+B)(思维)
- Android 5.1 Settings添加通知震动,通知音乐的开关功能实现
- 深度学习之路—写在开题前
- 读深入理解Linux内核 (第8章 内存管理, 第二部分 --- 任意长度内存分配)
- StringBuffer学习笔记
- SSM框架——使用MyBatis Generator自动创建代码
- 【codeforces 727 A】【dfs或者逆向思维】【给你两个操作,问能不能把数字a变成b】
- 运维开发融合
- 动动乐公司笔试
- 单调队列优化的多重背包
- Matlab 小程序 yuv图片融合
- 剔除单向链表重复值节点时的烦人bug
- POJ3169Layout(线性约束)
- java中冒泡排序与选择排序的区别
- 2013_hangzhou_online