Lightoj-1045-digits of factorial (using logarithms)

Source: Internet
Author: User

1082-array Queries
PDF (中文版) statisticsforum
Time Limit:3 second (s) Memory limit:64 MB
Given an array with n elements, indexed from 1 to n. Now you'll be a given some queries in the form I J, your task was to find the minimum value from index I to J.

Input
Input starts with an integer T (≤5), denoting the number of test cases.

The first line of a case was a blank line. The next line contains the integers N (1≤n≤105), Q (1≤q≤50000). The next line contains N space separated integers forming the array. there integers range in [0, 105].

The next Q lines would contain a query which is in the form I J (1≤i≤j≤n).

Output
For each test case, print the case number in a single line. Then for each query, the to print a line containing the minimum value between index I and J.

Sample Input
Output for Sample Input
2

5 3
1 31045-digits of factorial
PDF (中文版) statisticsforum
Time Limit:2 second (s) Memory limit:32 MB
Factorial of an integer was defined by the following function

F (0) = 1
F (n) = f (n-1) * N, if (n > 0)

So, factorial of 5 is 120. But in different bases, the factorial is different. For example, factorial of 5 in base 8 is 170.

In this problem, you have to find the number of digit (s) of the of the factorial of an integer in a certain base.

Input
Input starts with an integer T (≤50000), denoting the number of test cases.

Each case is begins with the integers n (0≤n≤106) and base (2≤base≤1000). Both of these integers would be a given in decimal.

Output
For each case of input, you had to print the case number and the digit (s) of factorial n in the given base.

Sample Input
Output for Sample Input
5
5 10
8 10
22 3
1000000 2
0 100
Case 1:3
Case 2:5
Case 3:45
Case 4:18,488,885
Case 5:1
3 5
4 4

1 1
10
1 1
Case 1:
1
3
12
Case 2:
10

Problem-solving ideas: Because the number of times can be used to calculate the logarithm, so f[] directly to the logarithm to save up.

Number of digits = (F[n]/log (base) +1) (PS: +1 is because for example log2 (4) = 2, but his digits are three, so to + 1)

#include <iostream>#include<cstring>#include<cmath>#include<cstdio>using namespacestd;typedef unsignedLong Longull;Const intN =1000010;DoubleF[n];voidinit () {f[0] = log (1);  for(intI=1; i<=n;i++) {F[i]= f[i-1]+log (i*1.0); }    return ;} intMain () {init (); intT,n,Base; scanf ("%d",&T);  for(intt=1; t<=t;t++) {scanf ("%d%d",&n,&Base); printf ("Case %d:%d\n", T, (int) (F[n]/log (Base)+1)); }    return 0;}
View Code

Lightoj-1045-digits of factorial (using logarithms)

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.