Friday, March 27, 2020

UVa - 12439 - February 29

Link to pdf version on UVa OJ

The problem seems to be straight forward, but you need to get read of the dates and deal with years. What I thought was that I can calculate the number of leap years from year 1 to year n, right? Should not be that hard, after a little thinking I came up with the formula :


According to the explanation, this would be the formula.
For anything, if you can calculate f(1, n), you can calculate f(a, b) -> a, b inclusive

f(a, b) = f(1, b) - f(1, a-1)

So now I should convert input to such thing : calculate the number of leap days in years a...b inclusive.
For start, if it is month of January or February it means I'm still hopeful that this year has leap day in it, so I would keep the year, otherwise I'm sure that current year doesn't have leap day in it, so I skip it in my calculations and calculate from startYear + 1.

 

We should write another function named getEnd() that returns the end year, When do we return year and when do we return year - 1?

After writing these two functions the result can be calculated easily.

Here is my code on github.



No comments:

Post a Comment

USACO - Prime Palindromes

I just skimmed the problem statement and panicked of the high boundary of the input, but something inside told me don't worry everyth...