TopCoder problem "DoubleXor" used in SRM 454 (Division I Level One)



Problem Statement

    Let us introduce a new operation called double xor, and use the operator ^^ to denote it. For two integers A and B, A ^^ B is calculated as follows. Take the decimal representations of A and B. If they have different lengths, prepend the shorter one with leading zeros until they both have the same length. Then, label the digits of A as a1, a2, ?, an (from left to right) and the digits of B as b1, b2, ... , bn (from left to right). C = A ^^ B will consist of the digits c1, c2, ... , cn (from left to right), where ci = (ai ^ bi) % 10, where ^ is the usual bitwise XOR operator (see notes for exact definition) and x % y is the remainder of x divided by y. If C happens to have any extra leading zeroes, they are ignored.

For example, 8765 ^^ 2309 = 462 (c1 = (8 ^ 2) % 10 = 10 % 10 = 0, c2 = (7 ^ 3) % 10 = 4 % 10 = 4, c3 = (6 ^ 0) % 10 = 6 % 10 = 6, c4 = (5 ^ 9) % 10 = 12 % 10 = 2), and 5 ^^ 123 = 126 ("5" is prepended with two leading zeros to become "005").

When multiple ^^ operations occur in an expression, they must be evaluated from left to right. For example, A ^^ B ^^ C means (A ^^ B) ^^ C.

You are given an int N. Return the value of N ^^ (N - 1) ^^ (N - 2) ^^ ? ^^ 1.
 

Definition

    
Class:DoubleXor
Method:calculate
Parameters:int
Returns:int
Method signature:int calculate(int N)
(be sure your method is public)
    
 

Notes

-If a and b are single bits then a ^ b is defined as (a + b) % 2. For two integers, A and B, in order to calculate A ^ B, they need to be represented in binary: A = (an...a1)2, B = (bn...b1)2 (if the lengths of their representations are different, the shorter one is prepended with the necessary number of leading zeroes). Then A ^ B = C = (cn...c1)2, where ci = ai ^ bi. For example, 10 ^ 3 = (1010)2 ^ (0011)2 = (1001)2 = 9.
 

Constraints

-N will be between 1 and 1,000,000, inclusive.
 

Examples

0)
    
1
Returns: 1
This is simply 1.
1)
    
2
Returns: 3
2^^1=3
2)
    
7
Returns: 0
7^^6^^5^^4^^3^^2^^1=0
3)
    
10
Returns: 11
4)
    
102
Returns: 103

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=10706

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=13908&pm=10706

Writer:

Gluk

Testers:

PabloGilberto , kalinov , ivan_metelsky

Problem categories:

Simulation