HDU 6424 Rikka with Time Complexity(log复杂度分析求极限)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37025443/article/details/81938121

Rikka with Time Complexity

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 371    Accepted Submission(s): 129


 

Problem Description

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…logn (there are exactly a log in this function, and log uses base 2). And then, for an integer array A, Rikka defines gA(n) in the following way (Bis the suffix of A with length |A|−1):

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



For example, g[1,2](n)=(logn)loglogn and g[3,1,1](n)=(logloglogn)(logn)logn.

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

 

Input

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

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

The second line contains a integers Ai and the third line contains b integers Bi(1≤Ai,Bi≤109), which describe A and B.

 

扫描二维码关注公众号,回复: 3105965 查看本文章

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 f(+\infty)

-1

 题意:

给你两个序列A,B,A/B的长度最长为3,g(A)=f(A1)^(f(A2)^f(A3))

f(A1)=loglogloglog...logn(有A1个log)

问你ans=\lim_{n \rightarrow +\infty}\frac{g_A(n)}{g_B(n)},ans->\infty,输出1;ans->0,输出-1;ans=1,输出0

解析

log(log(f(A1)^{f(A2)^{f(A3)}}))=f(A1+2)+f(A2+1)f(A3)

同理B也可以变成f(B1+2)+f(B2+1)f(B3)

然后比较上下两项,因为n \rightarrow +\infty,所以对于log函数,f(i)和f(i+1)相差非常大,

所以拿两个式子中的最大项max(f(A1+2),f(A2+1)f(A3)),max(f(B1+2),f(B2+1)f(B3))拿出来比较,只要f(Amax)>f(Bmax),那么f(Amax)>f(Bmax)+f(Bmin)

如果最大项相等,再比较两项之中的最小项min(f(A1+2),f(A2+1)f(A3)),min(f(B1+2),f(B2+1)f(B3))的大小,就可以比较A与B的大小

例如f(Amax)=f(2)*f(7),f(Bmax)=f(2),f(Bmin)=f(3)*f(3)

因为f(Amax)>f(Bmax),n \rightarrow +\infty,所以f(7)这一项仍然可以保持很大,但与f(2)又差很多,所以f(2)*f(7)>>f(2)

而f(2)>>f(3),所以f(2)>>f(3)*f(3),所以f(2)*f(7)>>f(2)+f(3)*f(3)

这样的话,我们只需要比较上面的式子中比较大的那一项,如果较大项相等,那么再比较较小项

对于比较f(a),和f(b)*f(c)哪个大,其实我们只需要比较f(a)f(+ \infty)和f(b)f(c)哪个大就可以了。

因为f()是对数函数,是一个递减的函数,f(i) > f(i+1),所以对于f(+ \infty)是一个无限小的数。

那么我们比较max(f(a),f(+\infty))max(f(b),f(c)),哪个大,
如果最大项相等再比较min(f(a),f(+\infty))min(f(b),f(c)),哪个大

同理比较f(a)f(b)和f(d)f(c),只要把+\infty换成b重复上述比较就可以了

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define  Min(a,b) (a<=b?a:b)
#define IO ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
typedef pair<int,int > PII;
const int INF = 0x3f3f3f3f;

int A[5],B[5];

inline int calmax(int x,int y)
{
    /*if(x>=INF&&y>=INF) return INF;
    else if(x>=INF) return y;
    else if(y>=INF) return x;
    else return x<=y?x:y;*/
    return x<=y?x:y;

}

inline int calmin(int x,int y)
{
    return x>=y?x:y;
}

inline int compare(PII x,PII y)
{
    int xmax=calmax(x.first,x.second);
    int xmin=calmin(x.first,x.second);

    int ymax=calmax(y.first,y.second);
    int ymin=calmin(y.first,y.second);

    if(xmax!=ymax) return xmax<ymax?2:0;
    else if(ymin!=xmin) return xmin<ymin?2:0;
    else return 1;

}

int main()
{
    int t;
    IO
    cin>>t;
    while(t--)
    {
        int a,b;
        cin>>a>>b;

        for(int i=1;i<=a;i++) cin>>A[i];
        for(int i=1;i<=b;i++) cin>>B[i];

        for(int i=a+1;i<=3;i++) A[i]=INF;
        for(int i=b+1;i<=3;i++) B[i]=INF;
        A[1]+=2;
        A[2]+=1;
        B[1]+=2;
        B[2]+=1;

        PII a1,a2;
        a1=make_pair(A[1],INF);
        a2=make_pair(A[2],A[3]);

        if(compare(a2,a1)) swap(a1,a2);

        PII b1,b2;
        b1=make_pair(B[1],INF);
        b2=make_pair(B[2],B[3]);

        if(compare(b2,b1)) swap(b1,b2);

        int fir=compare(a1,b1);
        int sec=compare(a2,b2);
        if(fir!=1)
        {
            if(fir==2) printf("1\n");
            else printf("-1\n");
        }
        else if(sec!=1)
        {
            if(sec==2) printf("1\n");
            else printf("-1\n");
        }
        else printf("0\n");

    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37025443/article/details/81938121