Ultraviolet A 12683 odd and even zeroes

Source: Internet
Author: User
Time Limit: 1000 MS In mathematics, the factorial of a positive integer number N is written as n! And is de ned as follows:
N! = 1 2 3 4: (N 1) n =
Limit n
I = 1
I
The value of 0! Is considered as 1. n! Grows very rapidly with the increase of N. Some values of N!
Are:
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
10! = 3628800
14! = 87178291200
18! = 6402373705728000
22! = 1124000727777607680000
You can see that for some values of N, N! Has odd number of Trailing zeroes (eg 5 !, 18 !) And for some
Values of N, N! Has even number of Trailing zeroes (eg 0 !, 10 !, 22 !). Given the value of N, your job is
Nd how values of the values 0 !; 1 !; 2 !; 3 !; :; (N 1 )!; N! Has even number of Trailing zeroes.
Input
Input le contains at most 1000 lines of input. Each line contains an integer N (0 N 10
18
). Input
Is terminated by a line containing a '-1 '.
Output
For each line of input produce one line of output. This line contains an integer which denotes how
Percent of the numbers 0 !; 1 !; 2 !; 3 !; :; N !, Contains even number of Trailing zeroes.
Sample Input
2
3
10
100
1000
2000
3000
10000
100000
200000
-1
Sample output
3
4
6
61
525
1050
1551
5050
50250

100126



Question: given a number of N, 0! , 1! , 2 !, ..., N! The number of the (n + 1) factorial ending 0 is an even number. (0 <= n <= 10 ^ 18)

Idea: I! The number of 0 at the end is determined by the number of 5 in the factorial. We take 5 numbers as a whole.

1 (5) 1 (10) 1 (15) 1 (20) 2 (25) 1 (30) 1 (35) 1 (40) 1 (45) 2 (50) 1 (55) 1 (60) 1 (65) 1 (70) 2 (75) 1 (80) 1 (85) 1 (90) 1 (95) 2 (100) 1 (105) 1 (110)

1 (115) 1 (120) 3 (125) the former number represents the power of 5 in the number, and the latter represents the number.

We will group the number before 625 as follows:

1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 3

1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 3

1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 3

1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 3

1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 4

So which segments meet the conditions where the number of 0 at the end is an even number? We represent the segment of the first line, (y) is yes, (n) is no

(Y) 1 (n) 1 (y) 1 (n) 1 (y) 2 (y) 1 (n) 1 (y) 1 (n) 1 (y) 2 (y) 1 (n) 1 (y) 1 (n) 1 (y) 2 (y) 1 (n) 1 (y) 1 (n) 1 (y) 2 (y) 1 (n) 1 (y) 1 (n) 1 (y) 3

We found that:

(1) It is known that until the first k power of 5 appears, then until the first 5 (k + 1) appears) the power-wise section must be a K-power section of 5 that repeats 5 segments, and the last segment of the last segment

An element changes K to (k + 1), that is, a large segment that appears until the first 5 (k + 1) power is reached.

(2) It is known that the Y/N values of each small segment in the first k POWER segment of the first 5 can be inferred from the (k + 1) of the first 5) y/N of each small segment of the power.

1. If K is an even number, the next four segments are exactly the same as the previous segments.

2. If K is an odd number, section 2nd and section 4th in the next 4 are the same as those in the previous section. Paragraphs 1st and 3rd are the opposite of the previous ones.

If DP [I] [0] is set, it indicates the number of segments whose last 0 is an even number until the first 5 I power is displayed.

DP [I] [1] is the number of even numbers opposite to the above case

So

DP [I] [0] = 5 * DP [I-1] [0] I is an odd number;

DP [I] [0] = 3 * DP [I] [0] + 2 * DP [I] [1] I is an even number;

DP [I] [1] = A [I-1]-DP [I] [0];

After the DP array is pre-processed, for N, we can find the maximum power range not greater than N for every binary query. We may set it to K, x = N/A [K], then N-= A [k] * X.

Occasionally, update ans. At the same time, we set a variable now to record the current state.



# Include <iostream> # include <algorithm> # include <cstdio> # define ll long longusing namespace STD; const int maxn = 27; ll n, a [maxn], DP [maxn] [2]; void initial () {ll T, sum = 1; A [0] = 1; for (INT I = 1; I <maxn; I ++) A [I] = 5 * A [I-1]; DP [0] [0] = 1, DP [0] [1] = 0; // This assignment is intended for later calculation, not 5 times. The values below are 5 times. DP [1] [0] = 1, DP [1] [1] = 0; For (INT I = 2; I <maxn; I ++) {if (I % 2 = 0) DP [I] [0] = 3 * DP [I-1] [0] + 2 * DP [I-1] [1]; else DP [I] [0] = 5 * DP [I-1] [0]; DP [I] [1] = A [I-1]-DP [I] [0];} For (INT I = 1; I <maxn; I ++) {DP [I] [0] * = 5; DP [I] [1] * = 5 ;}} void solve () {ll ans = 0; if (n <= 4) ans = n + 1; else {bool now = 0; n ++; while (n) {int T = upper_bound (A, A + maxn, n)-A-1; ll num = N/A [T]; n = n % A [T]; If (t = 0) ans + = num * DP [T] [now]; else if (T % 2 = 1) ans = ans + (Num + 1) /2 * DP [T] [now] + num/2 * DP [T] [now ^ 1]; else ans = ans + num * DP [T] [now]; N = n % A [T]; If (T % 2 = 1 & num % 2 = 1) Now ^ = 1 ;}} cout <ans <Endl;} int main () {initial (); While (CIN> N) {If (n =-1) break; solve ();} return 0 ;}



Ultraviolet A 12683 odd and even zeroes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.