Codeforces 61A Ultra-Fast Mathematician Solution & Explanation

Difficulty : 800

Problem Description

Shapur was an extremely gifted student. He was great at everything including Combinatorics, Algebra, Number Theory, Geometry, Calculus, etc. He was not only smart but extraordinarily fast! He could manage to sum 1018 numbers in a single second.

One day in 230 AD Shapur was trying to find out if any one can possibly do calculations faster than him. As a result he made a very great contest and asked every one to come and take part.

In his contest he gave the contestants many different pairs of numbers. Each number is made from digits 0 or 1. The contestants should write a new number corresponding to the given pair of numbers. The rule is simple: The i-th digit of the answer is 1 if and only if the i-th digit of the two given numbers differ. In the other case the i-th digit of the answer is 0.

Shapur made many numbers and first tried his own speed. He saw that he can perform these operations on numbers of length ∞ (length of a number is number of digits in it) in a glance! He always gives correct answers so he expects the contestants to give correct answers, too. He is a good fellow so he won’t give anyone very big numbers and he always gives one person numbers of same length.

Now you are going to take part in Shapur’s contest. See if you are faster and more accurate.

Input

There are two lines in each input. Each of them contains a single number. It is guaranteed that the numbers are made from 0 and 1 only and that their length is same. The numbers may start with 0. The length of each number doesn’t exceed 100.

Output

Write one line — the corresponding answer. Do not omit the leading 0s.

Examples

Input1010100
0100101
Output1110001
Input000
111
Output111
Input1110
1010
Output0100
Input01110
01100
Output00010

Solution

This problem is essentially a disguised test of Binary XOR operations. To optimize performance, we directly utilize the char type for XOR operations and employ StringBuilder to further minimize performance overhead.

C# Solution

Solution1

using System.Text;

char[] arr1 = Console.ReadLine().ToCharArray();
char[] arr2 = Console.ReadLine().ToCharArray();
var result = new StringBuilder();

for(int i=0; i<arr1.Length; i++){
    result.Append(arr1[i]==arr2[i] ? '0' : '1');
}

Console.WriteLine(result.ToString());

Java Solution

Solution1

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        char[] arr1 = scanner.nextLine().toCharArray();
        char[] arr2 = scanner.nextLine().toCharArray();
        StringBuilder result = new StringBuilder();
        
        for(int i=0; i<arr1.length; i++){
            result.append(arr1[i]==arr2[i]? '0' : '1');
        }
        
        System.out.println(result.toString());
    }
}

Conclusion

Further Reading – How to Control Permissions with Binary Operations : How to Effectively Manage Personnel Permissions using Binary-Base Logic Control

🧡If my solution helps, that is my honor!

🧡You can support me by sharing my posts, thanks you~~

✅If you got any problem about the explanation or you need other programming language solution, please feel free to let me know !!

The problem link : Problem – 61A – Codeforces

Random Codeforces Posts

Leave a Reply

Your email address will not be published. Required fields are marked *