HDU 6424 Rikka with Time Complexity sb数学题

本文介绍了一个关于算法时间复杂度比较的问题,通过定义特定的函数gA(n),对比两个数组所对应的函数在n趋向于无穷大时的增长速度。利用高数中的极限理论,提出了一种有效的比较方法,并给出了具体实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Rikka with Time Complexity

Calculating and comparing time complexity for algorithms are the most important necessary skills for CS students.

This semester, Rikka applies for the assistant of course "Algorithm Analysis". Now Rikka needs to set problems for the final examination, and she is going to set some tasks about time complexity.

Let fa(n)=log…lognfa(n)=log…log⁡n (there are exactly aa loglog in this function, and loglog uses base 22 ). And then, for an integer array AA , Rikka defines gA(n)gA(n) in the following way (BB is the suffix of AA with length |A|−1|A|−1 ):

gA(n)={fA1(n)fA1(n)gB(n)|A|=1|A|>1gA(n)={fA1(n)|A|=1fA1(n)gB(n)|A|>1



For example, g[1,2](n)=(logn)loglogng[1,2](n)=(log⁡n)log⁡log⁡n and g[3,1,1](n)=(logloglogn)(logn)logng[3,1,1](n)=(log⁡log⁡log⁡n)(log⁡n)log⁡n .

Now, given integer arrays AA and BB , Rikka wants you to compare gA(n)gA(n) with gB(n)gB(n) . i.e., let kk be limn→+∞gA(n)gB(n)limn→+∞gA(n)gB(n) . If k=0k=0 , output −1−1 ; if k=+∞k=+∞ , output 11 ; otherwise output 00 .

Input

The first line contains a single number t(1≤t≤105)t(1≤t≤105) , the number of testcases.

For each testcase, the first line contains two integers a,b(1≤a,b≤3)a,b(1≤a,b≤3) , the length of AA and BB .

The second line contains aa integers AiAi and the third line contains bb integers Bi(1≤Ai,Bi≤109)Bi(1≤Ai,Bi≤109) , which describe AA and BB .

Output

For each testcase, output a single line with a single integer, the answer.

Sample Input

3
1 1
1
2
2 2
1 2
2 1
1 3
1
1000000000 3 3

Sample Output

1
-1
-1

题意:给你一个函数gA(n),定义如题面,然后让你求当n趋近于无穷大的时候gA(n)/gB(n)的极限。

思路:听了吉老师的讲解,发现就是高数中的求极限的运用。先对gA,gB两次取log最后变成了\frac{(\log)_{A1+2}n+(\log)_{A2+1}n*(\log)_{A3}n }{ (\log)_{B1+2}n+(\log)_{B2+1}n*(\log)_{B3}n}

Ai,Bi表示有几个log,为0的用inf来表示,由于y=logx是一个单调递增的函数,但是每取一次log值就会变小,(log)inf n n->inf等于1

有转化成了\frac{a*inf+b*c}{d*inf+e*f} log的数量越小值越大,又因为取了log所以只用先把最小的找到,来比较,相同的话就比较下一个,也不同的话直接可以判断答案了。比较b*c和e*f时不是很好比较,在取一个log判断就好了,陈述起来有点麻烦,可以去看吉老师的录播:吉老师在线.AV

反思:感觉自己的高数都白学了,学以致用才是现在学习的目的,不在是为了应付考试了。

我的代码:

#include<stdio.h>
#include<string.h>
#include<cmath>
#include<stdlib.h>
#include<time.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<set>
#include<map>
#define ll long long
#define qq printf("QAQ\n");
using namespace std;
const int maxn=2e5+5;
const int inf=0x3f3f3f3f;
const ll linf=8e18+9e17;
const int mod=1e9+7;
const double e=exp(1.0);
const double pi=acos(-1);
const double eps=1e-6;
int A[4]={0},B[4]={0};
void rotate(int *a)
{
	if(min(a[0],a[1])==min(a[2],a[3])){
		if(max(a[0],a[1])>=max(a[2],a[3]))
		swap(a[0],a[2]),swap(a[1],a[3]);
	}
	else if(min(a[0],a[1])>min(a[2],a[3]))swap(a[0],a[2]),swap(a[1],a[3]);
	if(a[0]>a[1])swap(a[0],a[1]);
	if(a[2]>a[3])swap(a[2],a[3]);
}
int main()
{
	int t,a,b;
	scanf("%d",&t);
	while(t--)
	{		
		memset(A,0,sizeof A);
		memset(B,0,sizeof B); 
		scanf("%d%d",&a,&b);
		for(int i=1;i<=a;i++)scanf("%d",&A[i]);
		for(int i=1;i<=b;i++)scanf("%d",&B[i]);
		A[0]=inf,B[0]=inf;
		for(int i=1;i<=3;i++){
			if(A[i])A[i]+=3-i;
			else A[i]=inf;
			if(B[i])B[i]+=3-i;
			else B[i]=inf;
		}
		rotate(A),rotate(B);//把两个之间小的放前面 
		int ans=0;
		for(int i=0;i<=3;i++)
		{
			if(A[i]==B[i])continue;
			if(A[i]<B[i]){
				ans=1;break;
			}
			else if(A[i]>B[i]){
				ans=-1;break;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值