codeforces A. Transformation: from A to B(水题)

探讨了通过乘以2或末位加1的操作将数字A转换为B的方法。介绍了如何逆向操作解决该问题,并给出AC代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A. Transformation: from A to B

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 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.

Input

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.

Output

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.

Examples
input
2 162
output
YES
5
2 4 8 81 162 
input
4 42
output
NO
input
100 40021
output
YES
5

100 200 2001 4002 40021


小看了一个点~水题wa到想哭。。一直同一个样例傻傻挣扎。。
题意很简单:只有两种操作,一种是对当前数字a * 2,一种是a * 10 + 1,问能不能得到最后的数字b
想法也容易想到,反过来对b进行操作,如果b是偶数,那必定他是 * 2变化而来,本来我理所当然的想了反之,然而各位不是1的奇数就是NO..[抹泪..嘤嘤嘤]...想法就是这样了。。AC 代码如下:

#include<stdio.h>
#include<string.h>
int c[10000];
int main()
{
int a, b;
while (scanf("%d%d", &a, &b) == 2)
{
int i = 0, tmp = b;
if (a == b) i = 1;
while (a != b&&b)
{
if (b % 2)
{
if (b % 10 == 1) b /= 10;
else break;
}
else b /= 2;
c[i++] = b;
}
printf("%s\n", a == b ? "YES" : "NO");
if (a == b) {
printf("%d\n%d ", i + 1, a);
for (int j = i - 2; j >= 0; j--)
printf("%d ", c[j]);
printf("%d\n", tmp);
}
}
return 0;
}



You are given a positive integer N and two strings S and T, each of length N and consisting of lowercase English letters. Determine whether it is possible to make S identical to T by repeating the operation below any number of times (possibly zero). If it is possible, also find the minimum number of operations required. Choose two lowercase English letters x,y and replace every occurrence of x in S with y. Constraints 1≤N≤2×10 5 N is an integer. Each of S and T is a string of length N, consisting of lowercase English letters. Input The input is given from Standard Input in the following format: N S T Output If it is possible to make S identical to T, print the minimum number of operations required. Otherwise, print −1. Sample Input 1 Copy 6 afbfda bkckbb Sample Output 1 Copy 4 By performing the operation four times in the following way, you can make S identical to T: Choose x= b and y= c. S becomes afcfda. Choose x= a and y= b. S becomes bfcfdb. Choose x= f and y= k. S becomes bkckdb. Choose x= d and y= b. S becomes bkckbb, which is identical to T. It cannot be done with fewer than four operations, so the minimum number of operations required is 4. Sample Input 2 Copy 4 abac abac Sample Output 2 Copy 0 S and T are already identical, so no operations are required. Sample Input 3 Copy 4 abac abrc Sample Output 3 Copy -1 No matter how you repeat the operation, it is impossible to make S identical to T. Sample Input 4 Copy 4 abac bcba Sample Output 4 Copy 4 C++,中文!!!
最新发布
03-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值