BZOJ3000 Big Number

由Stirling公式:

$$n! \approx \sqrt{2 \pi n} (\frac{n}{e})^n$$

故:$$\begin{align} ans &= log_k n! + 1 \\ &\approx log_k [\sqrt{2 \pi n} (\frac{n}{e})^n] + 1 \\ &= \frac{1}{2} log_k 2 \pi n + n * (log_k n - log_k e) + 1\\ \end {align}$$

又$log_a b = \frac{log a}{log b}$

而且要注意n比较小的时候近似值差别比较大。。。可以直接暴力。。。

 1 /**************************************************************
 2     Problem: 3000
 3     User: rausen
 4     Language: C++
 5     Result: Accepted
 6     Time:28 ms
 7     Memory:816 kb
 8 ****************************************************************/
 9
10 #include <cstdio>
11 #include <cmath>
12
13 using namespace std;
14 typedef long double Lf;
15 typedef long long ll;
16 const Lf pi = acos(-1.0);
17 const Lf e = exp(1);
18 const Lf eps = 1e-10;
19
20 int n, k;
21 Lf ans;
22
23 Lf log(Lf x, Lf y) {
24     return log(x) / log(y);
25 }
26
27 int main() {
28     int i;
29     while (scanf("%d%d", &n, &k) != EOF) {
30         if (n <= 10000) {
31             for (ans = 0.0, i = 1; i <= n; ++i) ans += log(i);
32             ans /= log(k);
33             printf("%.0Lf\n", ceil(ans + eps));
34         } else
35         printf("%lld\n", (ll) (0.5 * log(2 * pi * n, k) + n * log(n, k) - n * log(e, k)) + 1);
36     }
37 }

时间: 2024-11-06 09:33:22

BZOJ3000 Big Number的相关文章

bzoj3000 Big Number 数论,斯特林公式

Description 给你两个整数N和K,要求你输出N!的K进制的位数. Input 有多组输入数据,每组输入数据各一行,每行两个数——N,K Output 每行一个数为输出结果 Sample Input 2 52 1010 10100 200 Sample Output 11769对于100%的数据,有2≤N≤2^31, 2≤K≤200,数据组数T≤200. 题解 用Stirling公式求近似值 位数=logk(n!)+1 ≍ logk(sqrt(2πn)*(n/e)^n)+1 = logk

Codeforces 124A - The number of positions

题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing b

17. Letter Combinations of a Phone Number

Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae", &q

实现一个函数clone,使JavaScript中的5种主要的数据类型(包括Number、String、Object、Array、Boolean)进行值复制

实现一个函数clone,可以对JavaScript中的5种主要的数据类型(包括Number.String.Object.Array.Boolean)进行值复制. 1 /** 对象克隆 2 * 支持基本数据类型及对象 3 * 递归方法 */ 4 function clone(obj) { 5 var o; 6 switch (typeof obj) { 7 case "undefined": 8 break; 9 case "string": o = obj + &q

解决sqoop报错Invalid number; item = ITEM_UNICODE

报错栈: java.sql.SQLException: Invalid number; item = ITEM_UNICODE at com.intersys.jdbc.SysList.getInt(SysList.java:1735) at com.intersys.jdbc.CacheResultSet.getInt(CacheResultSet.java:247) at org.apache.sqoop.lib.JdbcWritableBridge.readInteger(JdbcWrit

1005 Number Sequence

Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Each test case co

Minimum Inversion Number 【线段数】

Problem DescriptionThe inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj. For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of

171. Excel Sheet Column Number

Excel Sheet Column Number Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 static publi

hdu 5898 odd-even number 数位DP

odd-even number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 716    Accepted Submission(s): 385 Problem Description For a number,if the length of continuous odd digits is even and the length