FACVSPOW - Factorial vs Power
Consider two integer sequences f(n) = n! and g(n) = an, where n is a positive integer. For any integer a > 1 the second sequence is greater than the first for a finite number of values. But starting from some integer k, f(n) is greater than g(n) for all n >= k. You are to find the least positive value of n for which f(n) > g(n), for a given positive integer a > 1.
Input
The first line of the input contains number t – the amount of tests. Then t test descriptions follow. Each test consist of a single number a.
Constraints
1 <= t <= 100000
2 <= a <= 106
Output
For each test print the least positive value of n for which f(n) > g(n).
Example
Input: 3 2 3 4 Output: 4 7 9
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<queue>
#define bug printf("***\n");
//#define mem0 memset(a, 0, sizeof(a));
using namespace std;
typedef pair<long long, int> par;
const int mod = 1e9+7;
const int INF = 1e7;
const int N = 1000010;
const double pi = 3.1415926;
using namespace std;
int T, a;
double sum[3000010];
int main()
{
scanf("%d", &T);
sum[0] = 0;
for(int i = 1; i < 3000010; i ++) {
sum[i] = sum[i-1]+log(i);
}
while(T --) {
scanf("%d", &a);
int l = 0, r = 3000010;
double x = log(a);
while(l+1 < r) {
int mid = (l+r)/2;
if(sum[mid] > mid*x) r = mid;
else l = mid;
}
if(sum[l] > l*x) printf("d\n", l);
else printf("%d\n", l+1);
}
return 0;
}
AC代码(斯特林公式):
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<queue>
#define bug printf("***\n");
//#define mem0 memset(a, 0, sizeof(a));
using namespace std;
typedef pair<long long, int> par;
const int mod = 1e9+7;
const int INF = 1e7;
const int N = 1000010;
const double pi = 3.1415926;
using namespace std;
int T, a;
bool check(int x)
{
if(0.5*log(2*pi*x)+x*(log(x)-1) > x*log(a))
return true;
return false;
}
int main()
{
scanf("%d", &T);
while(T --) {
scanf("%d", &a);
int l = 0, r = INF;
while(l+1 < r) {
int mid = (l+r)/2;
if(check(mid)) r = mid;
else l = mid;
}
if(check(l)) printf("d\n", l);
else printf("%d\n", l+1);
}
return 0;
}