http://acm.hdu.edu.cn/showproblem.php?pid=6288
缺失的数据范围
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others)
Total Submission(s): 991 Accepted Submission(s): 176
Problem Description
著名出题人小Q出过非常多的题目,在这个漫长的过程中他发现,确定题目的数据范围是非常痛苦的一件事。
每当思考完一道题目的时间效率,小Q就需要结合时限以及评测机配置来设置合理的数据范围。
因为确定数据范围是一件痛苦的事,小Q出了非常多的题目之后,都没有它们设置数据范围。对于一道题目,小Q会告诉你他的算法的时间复杂度为O(nalogbn),且蕴含在这个大O记号下的常数为1。同时,小Q还会告诉你评测机在规定时限内可以执行k条指令。小Q认为只要na(⌈log2n⌉)b不超过k,那么就是合理的数据范围。其中,⌈x⌉表示最小的不小于x的正整数,即x上取整。
自然,小Q希望题目的数据范围n越大越好,他希望你写一个程序帮助他设置最大的数据范围。
Input
第一行包含一个正整数T(1≤T≤1000),表示测试数据的组数。
每组数据包含一行三个正整数a,b,k(1≤a,b≤10,106≤k≤1018),分别描述时间复杂度以及允许的指令数。
Output
对于每组数据,输出一行一个正整数n,即最大可能的n。
Sample Input
3
1 1 100000000
2 1 100000000
1 3 200000000
Sample Output
4347826
2886
48828
Source
"字节跳动杯"2018中国大学生程序设计竞赛-女生专场
由于精度问题所以求log2(n)不可以直接用double和求log函数,只能自定义函数。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#include<cmath>
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f3f3f
#define maxn 17000
#define root 1,n,1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define mod 10007
#define eps 1e-11
using namespace std;
ll a,b,k;
int judge(ll x){
ll y=0,y1=x,flag=0;
while(y1){
if(y1%2!=0&&y1!=1)flag=1;
if(y1>=2)
y++;
y1/=2;
}
if(flag==1)y++;
if(y==0)return 1;
ll a1=a,x1=1,x2=1,b1=b;
while(a1--){ if(x1>(k+5)/x) return 0;x1*=x;}
while(b1--){ if(x2>(k+5)/y) return 0;x2*=y;}
if(x1<=k/x2)
return 1;
else
return 0;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%lld%lld%lld",&a,&b,&k);
ll l=1,r=k,mid,ans=0;
while(l<=r){
mid=(l+r)/2;
if(judge(mid)==1){
ans=mid; l=mid+1;
}
else
r=mid-1;
}
printf("%lld\n",ans);
}
}
import java.util.Scanner;
import java.math.*;
public class Main{
public static void main(String[] args) {
int a,b;BigInteger k;
Scanner read=new Scanner(System.in);
int t=read.nextInt();
int test=0;
while(test!=t) {
test++;
a=read.nextInt();
b=read.nextInt();
k=read.nextBigInteger();
BigInteger l=BigInteger.valueOf(1),r=k,ans=BigInteger.valueOf(0),mid,m=BigInteger.valueOf(2);
BigInteger zero=BigInteger.valueOf(0);
BigInteger one=BigInteger.valueOf(1);
while(l.compareTo(r)<=0) {
mid=(l.add(r)).divide(m);
BigInteger x=mid;
BigInteger y=zero;
int flag=0;
while(x.compareTo(zero)>0) {
//System.out.println(""+x);
if(x.compareTo(one)!=0&&(x.remainder(m)).compareTo(one)==0 )flag=1;
if(x.compareTo(m)>=0)y=y.add(one);
x=x.divide(m);
}
if(flag==1)y=y.add(one);
if( (mid.pow(a) ) .multiply (y.pow(b) ).compareTo(k)<=0 ) {
ans=mid;
l=mid.add(one);
}
else
r=mid.subtract(one);
}
System.out.println(""+ans);
}
}
}