Harmonic Lemma : H(n) - UVA 11526

For a given $n$ , calculate $\sum_{i=1}^{n}{\lfloor \frac{n}{i} \rfloor}$ The main idea of this problem is there are maximum $2\sqrt{n}$ different values of $\lfloor \frac{n}{i} \rfloor$. [This is also known as Harmonic lemma] [But why?] click to hide Case 1: $x\le \sqrt{n}$ Hence, if we divide $n$ with $1\le i\le x$, number of values of $\lfloor \frac{n}{i} \rfloor$ will be at most $x$ (because, if all values were different, even then there will be $x$ values).
Read more →

Modulo - Toph

You will be given an array, $A$ of length $n$ and $q$ queries. In each query you will be given a value $x$. You have to perform $A_{i} = A_{i} \text{ (mod $x$)} , 1 \le i \le n$ for each query , and after all the query print the array. Constraints click to hide $A_{i} \le 2^{60}$ $n\le 10^{5}$ $q\le 10^{5}$ $x \le 2^{60}$ Solution: When $A_{i} = A_{i} \text{ (mod $x$)}$ operation change $A_{i}$ ?
Read more →

Gift Dilemma - UVA 12775

$Ax+By+Cz=P$ , $0\le x,y,z$ and $200 \le \frac{C}{gcd(A,B,C)}$. For given $A,B,C,P$ find number of triplet $(x,y,z)$. Solution: \begin{align} 200 &\le \frac{C}{gcd(A,B,C)} \newline C &\ge 200\times gcd(A,B,C) \newline \therefore C &\ge 200 \end{align} Hence, we will rewrite the eqation as $Ax+By+Cz=P \Longrightarrow Ax+By=P-Cz=P'$ We will iterate over all possible values of $P'$(there will be maximum $\frac{10^{8}}{200}$), and for each value of $P'$ we will find number of pair $(x,y)$ that satisfy $Ax+By=P'$ , and sum them up.
Read more →

Long Sandwich - Codechef SANDWICH

It has two parts to this problem. You are given $n$ and $k$. You have to tell the minimum number of pieces, $a$, you can cut a sandwich of length $n$ such that the length of no piece is greater than $k$. The answer is quite simple, isn’t it ? Answer of the first part click to hide $a = \lceil \frac{n}{k} \rceil$ The second part is, how many ways we can cut the sandwich into $a$ pieces such that the length of no piece is greater than $k$.
Read more →

Leading and Trailing - UVA 11029

For given $n$ and $m$ print the value LLL...TTT where LLL is the leading $3$ digits of $n^{m}$ and TTT is trailing $3$ digits of $n^{m}$. Inside Math: For TTT we just use modular arithmetic and find $n^{m} \text{ (mod 1000)}$ Now, assume that , number of digit of $n^{m}$ is $x$. Hence LLL must be equals to $\lfloor \frac{n^{m}}{10^{x-3}}\rfloor$ How to find x ? click to hide \begin{align} 10^{x-1} \le n^{m} &< 10^{x} \text{ [Number of digit in }10^{x-1}\text{ is equals to }x\text{ ]}\newline log_{10}{10^{x-1}} \le log_{10}{n^{m}} &< log_{10}{10^{x}} \newline x-1 \le m \times log_{10}{n} &< x \newline x &\le m \times log_{10}{n} + 1 \newline \therefore\text{maximum possible integer value of }&x\text{ is }\lfloor m \times log_{10}{n} + 1 \rfloor\text{ or }\lfloor m \times log_{10}{n} \rfloor +1 \end{align} But how to calculate $\lfloor \frac{n^{m}}{10^{x-3}}\rfloor$ ?
Read more →