[POJ] [1423] Big Number

Problem Description

Loading...

Problem Analysis

A formula problem
using Stirling’s formula:
when n is sufficiently large,

$$n!\approx\sqrt{2πn}{(\frac{n}{e})^n}$$
cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include<stdio.h>
#include<math.h>
#define PI 3.141592653589793239
#define e 2.7182818284590452354
int main() {
	int T;
	scanf("%d",&T);
	while(T--) {
		int n;
		scanf("%d",&n);
		if(n==1)
			printf("1\n");
		else
			printf("%d\n",(int)(n*log10(n/e)+log10(2*PI*n)/2+1));
	}
	return 0;
}

Problem link: 【POJ】[1423]Big Number

This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...