What is the output of this C program?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Details and Assumptions
Source : Internet Problem Solving Contest
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Calculation
This code calculates n = 1 ∑ M n ! mod 1 3 where M is the highest integer for which M ! < 1 3 1 0 0 0 .
Because n ! mod 1 3 = 0 for n ≥ 1 3 and 1 3 ! < 1 3 1 0 0 0 , this is equal to n = 1 ∑ 1 2 n ! mod 1 3 = 1 + 2 + 6 + 1 1 + 3 + 5 + 9 + 7 + 1 1 + 6 + 1 + 1 2 = 7 4 .
Brief explanation
Let N be the number of times the main loop has been run. Then A [ ] is the representation of N in base 13, and B [ ] is the factorial representation of N . That is, N = k = 0 ∑ 3 9 9 9 A [ k ] 1 3 k with 0 ≤ A [ k ] < 1 3 ; N = k = 0 ∑ 3 9 9 9 B [ k ] ( k + 1 ) ! with 0 ≤ B [ k ] ≤ k + 1 .
The condition ℓ > − 1 will be true precisely if in representation B [ ] a digit becomes non-zero that has not been non-zero before. This happens precisely with N = 1 ! , 2 ! , 3 ! , … . (The variable k keeps track of the last digit that turned non-zero.)
Whenever ℓ > − 1 , the least significant digit in the base-13 representation A [ ] is added to the running total m .
The conclusion is, therefore, that we add N mod 13 for N = 1 ! , 2 ! , 3 ! , … The conclusion above follows easily.