Wannafly Challenge 22C Polynomial (Large Number, Polynomial Limit)

Keywords: PHP less iOS

Links: https://ac.nowcoder.com/acm/contest/160/C
Source: Niuke.com

polynomial
Time limit: C/C++ 1 second, 2 seconds for other languages
Spatial constraints: C/C++ 262144K, other languages 524288K
64bit IO Format: %lld
Topic Description
Seek Lim limits {x to + infty} frac {f (x)} {g (x)}
x→+∞
lim

g(x)
f(x)

Where f and g are polynomials about x.
Input Description:
Two lines, the first act f, the second act g.
Both f and g are expressed by an expression consisting of parentheses'('and'), plus sign'+', multiplier sign','x'. The grammar of the expression is the same as the usual practice.
Ensure that the length of the expression does not exceed 1000.
Output description:
If the answer is integer x, output x/1, the answer is + infty and output 1/0, otherwise the output is the simplest fraction a/b of the answer.
Example 1
input
copy
x+x
x+(x+x)
output
copy
2/3
Example 2
input
copy
x(x+x+xx+xx)
x+(x+x)(x+x+x)
output
copy
1/0
Example 3
input
copy
x
x(x(x)+x)(((x+x)))*x
output
copy
0/1

Topic:

Ideas:

In the knowledge of higher mathematics, we know that when x is infinite, the value of fraction is related to the highest power term of molecule and denominator, and the other is the equivalent infinitesimal of the highest power term.

If the highest power of the denominator is equal, then the limit value of the fraction is the coefficient ratio of the highest power of the denominator and the molecule. Remember to take gcd.

Otherwise, if the highest power of the molecule is greater than that of the denominator, the value of the limit of the fraction is infinite, and the highest power less than that of the denominator is 0.

Will explode ll, use a large number.

See the code for details:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int ten[4] = {1,10,100,1000};
const int MAXN=1200;
typedef struct BigNumber
{
    int d[MAXN];
    BigNumber(string s)
    {
        int i, j, k, len;
        len = s.size();
        d[0] = (len-1)/4+1;
        for(i=1;i<MAXN;i++)
            d[i] = 0;
        for(i=len-1;i>=0;i--)
        {
            j = (len-i-1)/4+1;
            k = (len-i-1)%4;
            d[j] += ten[k]*(s[i]-'0');
        }
        while(d[0]>1 && d[d[0]]==0)
            --d[0];
    }
    BigNumber()
    {
        *this = BigNumber(string("0"));
    }
    string toString()
    {
        int i, j, temp;
        string s("");
        for(i=3;i>=1;i--)
        {
            if(d[d[0]]>=ten[i])
                break;
        }
        temp = d[d[0]];
        for(j=i;j>=0;j--)
        {
            s = s+(char)(temp/ten[j]+'0');
            temp %= ten[j];
        }
        for(i=d[0]-1;i>0;i--)
        {
            temp = d[i];
            for(j=3;j>=0;j--)
            {
                s = s+(char)(temp/ten[j]+'0');
                temp %= ten[j];
            }
        }
        return s;
    }
}BigNumber;
BigNumber zero("0"), d, temp, mid[15];
bool operator < (const BigNumber &a, const BigNumber &b)
{
    int i;
    if(a.d[0]!=b.d[0])
        return a.d[0]<b.d[0];
    for(i=a.d[0];i>0;i--)
    {
        if(a.d[i]!=b.d[i])
            return a.d[i]<b.d[i];
    }
    return 0;
}
bool operator > (const BigNumber &a, const BigNumber &b)
{
    int i;
    if(a.d[0]!=b.d[0])
        return a.d[0]>b.d[0];
    for(i=a.d[0];i>0;i--)
    {
        if(a.d[i]!=b.d[i])
            return a.d[i]>b.d[i];
    }
    return 0;
}
bool operator == (const BigNumber &a, const BigNumber &b)
{
    int i;
    if(a.d[0]!=b.d[0])
        return 0;
    for(i=a.d[0];i>0;i--)
    {
        if(a.d[i]!=b.d[i])
            return 0;
    }
    return 1;
}
BigNumber operator + (const BigNumber &a, const BigNumber &b)
{
    int i, x;
    BigNumber c;
    c.d[0] = max(a.d[0], b.d[0]);
    x = 0;
    for(i=1;i<=c.d[0];i++)
    {
        x = a.d[i]+b.d[i]+x;
        c.d[i] = x%10000;
        x /= 10000;
    }
    while(x!=0)
    {
        c.d[++c.d[0]] = x%10000;
        x /= 10000;
    }
    return c;
}
BigNumber operator - (const BigNumber &a, const BigNumber &b)
{
    int i, x;
    BigNumber c;
    c.d[0] = a.d[0];
    x = 0;
    for(i=1;i<=c.d[0];i++)
    {
        x = 10000+a.d[i]-b.d[i]+x;
        c.d[i] = x%10000;
        x = x/10000-1;
    }
    while((c.d[0]>1) && (c.d[c.d[0]]==0))
        --c.d[0];
    return c;
}
BigNumber operator * (const BigNumber &a, const BigNumber &b)
{
    int i, j, x;
    BigNumber c;
    c.d[0] = a.d[0]+b.d[0];
    for(i=1;i<=a.d[0];i++)
    {
        x = 0;
        for(j=1;j<=b.d[0];j++)
        {
            x = a.d[i]*b.d[j]+x+c.d[i+j-1];
            c.d[i+j-1] = x%10000;
            x /= 10000;
        }
        c.d[i+b.d[0]] = x;
    }
    while((c.d[0]>1) && (c.d[c.d[0]]==0))
        --c.d[0];
    return c;
}
bool smaller(const BigNumber &a, const BigNumber &b, int delta)
{
    int i;
    if(a.d[0]+delta!=b.d[0])
        return a.d[0]+delta<b.d[0];
    for(i=a.d[0];i>0;i--)
    {
        if(a.d[i]!=b.d[i+delta])
            return a.d[i]<b.d[i+delta];
    }
    return 1;
}
void Minus(BigNumber &a, const BigNumber &b, int delta)
{
    int i, x;
    x = 0;
    for(i=1;i<=a.d[0]-delta;i++)
    {
        x = 10000+a.d[i+delta]-b.d[i]+x;
        a.d[i+delta] = x%10000;
        x = x/10000-1;
    }
    while((a.d[0]>1) && (a.d[a.d[0]]==0))
        --a.d[0];
}
BigNumber operator * (const BigNumber &a, int k)
{
    BigNumber c;
    c.d[0] = a.d[0];
    int i, x;
    x = 0;
    for(i=1;i<=a.d[0];i++)
    {
        x = a.d[i]*k+x;
        c.d[i] = x%10000;
        x /= 10000;
    }
    while(x>0)
    {
        c.d[++c.d[0]] = x%10000;
        x /= 10000;
    }
    while((c.d[0]>1) && (c.d[c.d[0]]==0))
        --c.d[0];
    return c;
}
BigNumber operator / (const BigNumber &a, const BigNumber &b)
{
    int i, j, temp;
    BigNumber c;
    d = a;
    mid[0] = b;
    for(i=1;i<=13;i++)
        mid[i] = mid[i-1]*2;
    for(i=a.d[0]-b.d[0];i>=0;i--)
    {
        temp = 8192;
        for(j=13;j>=0;j--)
        {
            if(smaller(mid[j], d, i))
            {
                Minus(d, mid[j], i);
                c.d[i+1] += temp;
            }
            temp /= 2;
        }
    }
    c.d[0] = max(1, a.d[0]-b.d[0]+1);
    while((c.d[0]>1) && (c.d[c.d[0]]==0))
        --c.d[0];
    return c;
}
BigNumber Gcd(const BigNumber &a, const BigNumber &b)
{
    BigNumber c("0");
    if(b==c)
        return a;
    c = a-a/b*b;
    return Gcd(b, c);
}


string str1;
string str2;

struct res
{
    BigNumber A,C;

    res operator + (const res & b) const
    {
        res temp=b;
        if(temp.C==C)
        {
            temp.A=temp.A+A;
        }else if(temp.C<C)
        {
            temp.C=C;
            temp.A=A;
        }
        return temp;
    }
    res operator * (const res & b)const
    {
        res temp;
        temp.C=C+b.C;
        temp.A=A*b.A;
        return temp;
    }
};
res Gao(int l,int r)
{
    if(l==r)
    {
        res t;
        t.A=BigNumber("1");
        t.C=BigNumber("1");
        return t;
    }else
    {
        int now=0;
        for(int i=l;i<=r;++i)
        {
            if(str1[i]=='(')
            {
                now++;
            }
            if(str1[i]==')')
            {
                now--;
            }
            if(now==0&&str1[i]=='+')
            {
                return Gao(l,i-1)+Gao(i+1,r);
            }
        }
        for(int i=l;i<=r;++i)
        {
            if(str1[i]=='(')
            {
                now++;
            }
            if(str1[i]==')')
            {
                now--;
            }
            if(now==0&&str1[i]=='*')
            {
                return Gao(l,i-1)*Gao(i+1,r);
            }
        }

        return Gao(l+1,r-1);
    }
}
res solve()
{
    int len=str1.length();
    return Gao(0,len-1);
}
int main()
{
    //freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
    //freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);

    gbtb;
    cin>>str1;
    res r1=solve();
    cin>>str1;
    res r2=solve();
    if(r1.C>r2.C)
    {
        cout<<"1/0"<<endl;
    }else if(r1.C<r2.C)
    {
        cout<<"0/1"<<endl;
    }else
    {
        BigNumber g=Gcd(r1.A,r2.A);
        cout<<(r1.A/g).toString()<<"/"<<(r2.A/g).toString()<<endl;
    }



    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}



Posted by karldesign on Thu, 01 Aug 2019 21:50:17 -0700