TOJ1011 factorial end non-zero number sum

Source: Internet
Author: User

/*

Sum of non-zero numbers at the end of a factorial

Http://acm.tongji.edu.cn/people/ps/showproblem.php? Problem_id = 1011

Time Limit: 1 s Memory Limit: 1000 k
Total Submit: 4671 Accepted: 1317

Problem

For a natural number n Less than 25000, calculate the factorial n !, (N-1 )!, (N-2 )!... 3 !, 2 !, 1! The sum of non-zero numbers on the right.

For example:

When n = 5,
5! = 120. The number of non-zero values on the right is 2;
4! = 24. The number of non-zero values on the right is 4;
3! = 6. The number of non-zero values on the right is 6;
2! = 2. The number of non-zero values on the right is 2;
1! = 1. The number of non-zero values on the right is 1.
The sum of non-zero numbers on the right is 15.

Input

This topic contains multiple groups of data, each containing a positive integer N (N not greater than 25000) occupies one row.

Output

Returns an integer for each input group. Each result occupies one row. Do not output extra empty rows.

Sample Input
5101
Sample Output
15391

T_T super depressing question ......

Run ID user problem result memory time Language Date

263600 kingwei 1011 accepted 28 K 659 ms c 16:59:22
263554 kingwei 1011 time limit exceeded C 16:03:42
263477 kingwei 1011 wrong answer 36 K 10 ms c 14:37:16
263466 kingwei 1011 wrong answer 36 K 6 ms c 14:28:36
263463 kingwei 1011 Wrong Answer 44 k 6 ms c 14:27:20

*/

# Include <stdio. h>

# Define MAX_NUM 25000
# Deprecision MAX_LEN 1600

Int start, end;
Int workarr [MAX_LEN] = {1, 0 };
Int res [MAX_NUM] = {0, 1 };

Int main ()
{
Int n, I, j, carry, temp;
 
Start = 0;
End = 0;
For (I = 2; I <MAX_NUM; I ++)
{
Carry = 0;
For (j = start; j <= end; j ++)
{
Workarr [j] = workarr [j] * I + carry;
Carry = workarr [j]/10000;
Workarr [j] %= 10000;
}
While (carry> 0 & end <MAX_LEN)
{
End ++;
Workarr [end] = carry % 10000;
Carry/= 10000;
}
While (workarr [start] = 0)
Start ++;
Temp = workarr [start];
While (temp % 10 = 0)
Temp/= 10;
Res [I] = res [I-1] + temp % 10;
}

While (scanf ("% d", & n )! = EOF)
{
Printf ("% d/n", res [n]);
}

Return 0;
}

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.