1. 程式人生 > >HDU 6424 Rikka with Time Complexity sb數學題

HDU 6424 Rikka with Time Complexity sb數學題

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;
}