B. Ternary Logic(三进制)

本文介绍了三进制逻辑运算中的tor操作,即两数按位相加对3取余,无进位。给定两个十进制数a和c,寻找一个b,使得a tor b等于c。若存在多个b,输出最小的那个。解决方案包括将a和c转换为三进制,使用c减去a得到b的三进制形式,最后将结果转回十进制。
摘要由CSDN通过智能技术生成

time limit per test 2 seconds
memory limit per test 256 megabytes
input standard input
output standard output
Little Petya very much likes computers. Recently he has received a new “Ternatron IV” as a gift from his mother. Unlike other modern computers, “Ternatron IV” operates with ternary and not binary logic. Petya immediately wondered how the xoroperation is performed on this computer (and whether there is anything like it).

It turned out that the operation does exist (however, it is called tor) and it works like this. Suppose that we need to calculate the value of the expression a tor b. Both numbers a and b are written in the ternary notation one under the other one (bunder a). If they have a different number of digits, then leading zeroes are added to the shorter number until the lengths are the same. Then the numbers are summed together digit by digit. The result of summing each two digits is calculated modulo 3. Note that there is no carry between digits (i. e. during this operation the digits aren’t transferred). For example: 1410 tor5010 = 01123 tor 12123 = 10213 = 3410.

Petya wrote numbers a and c on a piece of paper. Help him find such number b, that a tor b = c. If there are several such numbers, print the smallest one.

Input
The first line contains two integers a and c (0 ≤ a, c ≤ 109). Both numbers are written in decimal notation.

Output
Print the single integer b, such that a tor b = c. If there are several possible numbers b, print the smallest one. You should print the number in decimal notation.

Sample test(s)
input
14 34
output
50
input
50 34
output
14
input
387420489 225159023
output
1000000001
input
5 5
output
0

题意 :就是说tor是一个三进制的运算,代表的是两个三进制数的运算,两个三进制数按位逐一相加后对三取余,没有进位,但是如果两个没有相同多的位数的话,短的那个0。而那个样例就是10进制的14和10进制的59进行tor运算,就相当于分别转化成三进制在进行tor运算,a tor b = c,给你a和c,让你求b 。

思路 : 其实也没什么好说的,题懂了,基本上思路也就出来了,我也就是把a c都转化成三进制的,然后用c的三进制减a 的三进制,最后再转化成10进制即可 。

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std ;
int aa[110],bb[
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值