B. Ternary Logic

Source: Internet
Author: User
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 assigned ed 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 howXOROperation
Is already med on this computer (and whether there is anything like it ).

It turned out that the Operation does exist (however, it is calledTor) And it works like this. Suppose that we need to calculate the value
Of the expressionA tor B. Both numbersAAndBAre
Written in the ternary Notation One under the other one (BUnderA).
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: 1410Tor5010 bytes = average 01123Tor12123 bytes = average 10213 rows = 3410.

Petya wrote numbersAAndCOn
A piece of paper. Help him find such numberB, ThatA tor BSignature = SignatureC.
If there are several such numbers, print the smallest one.

Input

The first line contains two integersAAndC(0 bytes ≤ bytesA, Bytes,CLimit ≤ limit 109 ).
Both numbers are written in decimal notation.

Output

Print the single integerB, Such thatA tor BSignature = SignatureC.
If there are several possible numbersB, Print the smallest one. You shoshould 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

Solution Description: this is the calculation of the triplicate system. According to the meaning of the question, the two operators first convert the two numbers to the triplicate system, then each bit is separately accumulated (not carry ). The last result and number are given in the question. If you want to calculate another number, you can subtract another number from the result.

#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <string>#include <algorithm>using namespace std;int main() {int a,b,c,p;scanf("%d %d",&a,&c);b=0;p=1;while(a||c){b+=((c%3-a%3+3)%3)*p;p*=3;a/=3;c/=3;}printf("%d\n",b);return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.