UVa 568 Just The Facts: Number theory & Play & Don't hit the table

Source: Internet
Author: User
Tags integer printf time limit

568-just the Facts

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_ problem&problem=509

The expression n!, read as ' N factorial, denotes the product of the ' the ' the ' the ' the ' the ' the ' the ' the ' the ' the ', where n is positive. So, for example,

n N!
0 1
1 1
2 2
3 6
4 24
5 120
10 3628800

For this problem, your are to write a and can compute the last non-zero digit of any factorial for ($ le N le 100 00$). For example, if your's asked to compute the last nonzero digit of 5!, your program should produce ' 2 ' because 5! = 2 is the last nonzero digit of 120.

Input
The Input to the program is a series of nonnegative integers not exceeding 10000, and each on its own line with no other letters, digits or spaces. For each integer N, you should read the value and compute to the last nonzero digit of n!.

Output
For each integer input, the program should print exactly one line of output. Each line of output should contain the value N, right-justified in columns 1 through 5 with leading blanks, not leading ze ROEs. Columns 6-9 must contain '-> ' (space hyphen greater spaces). Column must contain the single last Non-zero digit of n!.

Sample Input

1
2
26
125
3125
9999

Sample Output
1-> 1
2-> 2
Num-> 4
-> 8
3125-> 2
9999-> 8

Method 1: Hit the table. Depending on the range of data, the last 5 digits are counted when the calculation is done.

Method 2: Calculates the count of the power of 5 in the n! factor decomposition, and then the number divisible by 2 is divided by 2 until count is reached

First put on the algorithm to play table:

/*0.016s*/
    
#include <cstdio>  
    
int a[10001] = {1};///0!=1  
    
int main ()  
{  
    int i, x;  
    for (i = 1; I <= 10000; i++)  
    {  
        A[i] = a[i-1] * i;  
        While (a[i]% = = 0) a[i]/=;  
        A[i]%= 100000;///, according to the data range, retains the last 5 bits on the line ~  
    }  
    while (~scanf ("%d", &x))  
        printf ("%5d->%d\n", X, A[x]% 10); return  
    0;  
}

Then there is the algorithm for not playing the table:

/*0.016s*/#include <cstdio> int p[10000];  
    int main () {int n;  
        while (~SCANF ("%d", &n)) {int I, sum = 1, Count = 0, tem = 0;  
            if (n = = 0) {printf ("0-> 1\n");  
        Continue  
        for (i = 0; i < n; i++) P[i] = i + 1;  
                for (i = 0; i < N. i++) while (true) {if (P[i]% 5 = 0)  
                    {P[i]/= 5;  
                count++;  
            else break;  for (i = 0; i < n; i++) {while (true) {if (P[i]%  
                    2 = 0) {p[i]/= 2;  
                tem++;  
            } if (P[i]% 2 | | | tem = count) break; } if (tem = count) break; 
        for (i = 0; i < n; i++) {sum *= p[i];  
        Sum%= 10;  
    printf ("%5d->%d\n", N, sum);  
return 0; }

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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.