Factorial problem n! After the rightmost number is not 0

Source: Internet
Author: User

Factorial problem time limit: 1 Sec memory limit: MB
Submitted: $ 13 Resolution:
Submitted State [Discussion Version] Title Description Perhaps you already know the meaning of factorial, n factorial is multiplied by 1 to N, such as:
12! = 1 x 2 x 3 x 4 x 5 x 6 x 7 x 8 x 9 x Ten x x 12 = 479,001,600
The rightmost non-0 bit of the factorial of 12 is 6.
Write a program that calculates the rightmost non-0-bit value of the N (1<=n<=50,000,000) factorial.
NOTE: 10,000,000! There are 2,499,999 zeros.
Enter only one row containing a positive integer n. Output
A single line contains an integer representing the rightmost non-0-bit value.
Sample input
12
Sample output
6

#include <cstdio>#include<algorithm>#definell Long Long#defineMAXN 800010#defineLson l,m,rt<<1#defineRson m+1,r,rt<< 1 | 1using namespacestd;ints[ -];intMain () {intnum,n,add,pai[4]={6,2,4,8};  while(~SCANF ("%d",&N)) {num=N; Add=0;  while(num) {if(num%5==2) {Add+=1; }            Else{                if(num%5==4) Add+=2; } Add+=num/5; Num/=5; }        if(n>1) {printf ("%d\n", pai[add%4]); }        Elseprintf"1\n"); }}

Factorial problem n! After the rightmost number is not 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.