Problem
Given the value of an n you will have to find the modulo 100000000 value of the following expression:
floor(sqrt(1)) + floor(sqrt(3)) + floor(sqrt(5)) + ... + floor(sqrt(m))
, where m is the largest odd number not greater than n. Or in other words you will have to find the value of S where,
S = floor(sqrt(1)) + floor(sqrt(3)) + floor(sqrt(5)) + ... + floor(sqrt(m)) MOD 100000000
Input
The input file contains at most 30000 lines of inputs. Each line contains a single 64-nit signed integer which denotes the value of n. Input is terminated by a line containing a single zero which should not b processed.
Output
For each line of input produce one line of output. This line contains the value of S.
Sample Input
|
|
Output for Sample Input
|
|
Solution
先把前幾項列出來,會發現一個規律
|
|
每一次會增加兩個。
假設第 1 組為 11 22
,第 2 組為 3333 4444
…
需要使用二分搜尋找到我們總共要幾組,然後直接利用公式算出來 sigma(2i * (2i - 1 + 2i))。
但是公式算出來後,會有一個 /3
需要出處理,這邊使用乘法反元素,幸好 3 和 100000000 有互質,不然還真是沒有辦法避免大數運算,計算的時候請各種小心 overflow 的問題。
|
|