ZOJ ACM 2022 (JAVA)

Source: Internet
Author: User

Please refer to the topic Description: ZOJ ACM 2022

1) Difficulty analysis

Is the extension of the large number factorial. If you want to calculate the number of digits in the end 0, the time efficiency is far from satisfying the requirement of 2 seconds.

2) Workaround

The result is calculated by the law. Sets the number of digits at the end of the result of calculating the factorial of N to 0. where n=n* (N-1) *....*i*....*1, Mantissa is 0 of the mantissa is zeronumber.

First of all, only when I is a multiple of 5, the i*2 must be a single digit of 0;

Second, when I is 5 K-square, i* (2 of the K-th square) = (5*2) of the K-square, so inevitably at the end of the K 0. Because it is a factorial operation, 5 of the K-square, it is bound to encounter 2 K-square.

With the above-mentioned two points, we can conclude as follows:

N is 5 l times = = "has l a 5 and 2 multiplied by = =" has l 10 = = "Zeronumber = l;

N is 5 k times the M = = "Zeronumber increase (k-1) *m. (Note: Because 5 of the two is increased by 2 0, but because the previous calculation of 5 times times, has been increased once 1, so only need to add 1 more times, and so on.) )

3) AC Source code

public class Main {public static void main (string[] args) {//TODO auto-generated method Stubjava.util.Scanner Scanner = n EW Java.util.Scanner (system.in); int linenum = Integer.parseint (Scanner.nextline ()); for (int lineindex = 0; lineindex < LineNum; lineindex++) {Long n = Long.parselong (Scanner.nextline ()); System.out.println (CALC_V2 (n));}} public static long Calc_v2 (long N) {long zeronumber = 0;for (int i=1;i<20;i++) {                //The reason for setting 20 here is that POW (5,20) > The maximum value of n in the title is 1000000000, and the actual setting 20 has a surplus. Long BigNumber = (long) Math.pow (5, I); zeronumber+= N/bignumber;} return zeronumber;}}


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.