Spoj-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
有很多组询问,给个常数1<=a<=100w,求使得n! > a^n 的最小整数n
构造f(n)=n!,g(n)=a^n,a是常数,由高中知识就很容易知道f(n)趋近极限的速度最后会更快
不妨令h(n)=f(n)-g(n),则h(n)应当是递增的(吧?)
只要求h(n)=(n!-a^n) > 0的最小n
因此可知当a增加的时候,h(n)的零点应当也是增加的
所以可以枚举个a的值,不断增加n的值,只要n!>a^n,即log(n!)>nloga
即log1+log2+...+logn>nloga
左边的部分可以在枚举a的时候顺便求得
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define mkp(a,b) make_pair(a,b)
#define pi 3.1415926535897932384626433832795028841971
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n;
int ans[];
int main()
{
double s=;int t=;
for (int i=;i<=;i++)
{
while (s<=t*log(i)){t++;s+=log(t);}
ans[i]=t;
}
int T=read();
while (T--){printf("%d\n",ans[read()]);}
}
Spoj FACVSPOW
Spoj-FACVSPOW Factorial vs Power的更多相关文章
- FACVSPOW - Factorial vs Power 数学方法 + 二分
http://www.spoj.com/problems/FACVSPOW/ 求解n! > a^n最小的整数n 对于有n!和a^n的东西,一般是取ln 然后就是求解 (ln(1) + ln(2) ...
- SPOJ #11 Factorial
Counting trailing 0s of n! It is not very hard to figure out how to count it - simply count how many ...
- 【SPOJ】Power Modulo Inverted(拓展BSGS)
[SPOJ]Power Modulo Inverted(拓展BSGS) 题面 洛谷 求最小的\(y\) 满足 \[k\equiv x^y(mod\ z)\] 题解 拓展\(BSGS\)模板题 #inc ...
- 「SPOJ 3105」Power Modulo Inverted
「SPOJ 3105」Power Modulo Inverted 传送门 题目大意: 求关于 \(x\) 的方程 \[a^x \equiv b \;(\mathrm{mod}\; p) \] 的最小自 ...
- SPOJ:Divisors of factorial (hard) (唯一分解&分块优化)
Factorial numbers are getting big very soon, you'll have to compute the number of divisors of such h ...
- SPOJ #752. Power it!
By property of mod operations , we can simply use Divide and Conquer + Recursion to solve it. Refere ...
- MOOCULUS微积分-2: 数列与级数学习笔记 6. Power series
此课程(MOOCULUS-2 "Sequences and Series")由Ohio State University于2014年在Coursera平台讲授. PDF格式教材下载 ...
- [BZOJ2616]SPOJ PERIODNI 树形dp+组合数+逆元
2616: SPOJ PERIODNI Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 128 Solved: 48[Submit][Status][ ...
- 2016.5.16——leetcode:Rotate Array,Factorial Trailing Zeroe
Rotate Array 本题目收获: 题目: Rotate an array of n elements to the right by k steps. For example, with n = ...
随机推荐
- UVA 10003 cuting sticks 切木棍 (区间dp)
区间dp,切割dp[i][j]的花费和切法无关(无后效性) dp[i][j]表示区间i,j的花费,于是只要枚举切割方法就行了,区间就划分成更小的区间了.O(n^3) 四边形不等式尚待学习 #inclu ...
- k8s 如何 Failover?
上一节我们有 3 个 nginx 副本分别运行在 k8s-node1 和 k8s-node2 上.现在模拟 k8s-node2 故障,关闭该节点. 等待一段时间,Kubernetes 会检查到 k8s ...
- hash 散列表
一个字符串的hash值: •现在我们希望找到一个hash函数,使得每一个字符串都能够映射到一个整数上 •比如hash[i]=(hash[i-1]*p+idx(s[i]))%mod •字符串:abc,b ...
- python基础面试题整理---从零开始 每天十题(04)
一.Q:如何用Python来进行查询和替换一个文本字符串? A:可以使用sub()方法来进行查询和替换,sub方法的格式为:sub(replacement, string[, count=0]) re ...
- shell脚本,判断给出的字符串是否相等。
第一种方法[root@localhost wyb]# .sh #!/bin/bash #判断给出的字符串是否相等 read -p "Please Input a number:" ...
- shell脚本,计算1+3+5....100等于多少?
[root@localhost wyb]# cat unevenjia.sh #!/bin/bash #从1+++...100的结果 i= count=$1 $count` do sum=$(($su ...
- httpClient类
@SuppressWarnings("finally") public JSONObject doPost(String url, String parms){ if (" ...
- iBeacon技术
声明:部分资料来源自互联网 前言 iBeacon 最早推出是在今年的苹果 WWDC 大会上.作为 iOS 7 的一部分,它吸引人的一点是,iBeacon 是一种开发标准——绝大多数智能手机支持蓝牙 4 ...
- 详解Mac睡眠模式设置
详解Mac睡眠模式设置 原文链接:http://www.insanelymac.com/forum/index.php?showtopic=281945 需要说明的是,首先这篇文章是针对已经能够成功睡 ...
- 【离线做法 树状数组】luoguP1972 [SDOI2009]HH的项链
与bzoj3585: mex的线段树做法有着异曲同工之妙 题目描述 HH 有一串由各种漂亮的贝壳组成的项链.HH 相信不同的贝壳会带来好运,所以每次散步完后,他都会随意取出一段贝壳,思考它们所表达的含 ...