After a hard struggle, DreamGrid was finally admitted to a university. Now he is having trouble calculating the limit of the ratio of two polynomials. Can you help him?
DreamGrid will give you two polynomials of a single variable (eg. x^2-4x+7) or constant integers, and then he will tell you an integer . Your job is to find out the limit of a ratio consisting of these two polynomials (or constant integers) when tends to . The first polynomial is the numerator and the second one is the denominator.
Input
The first line of input contains an integer (), which indicates the number of test cases. For each test case:
The first two lines describe two polynomials or constant integers, consisting of integers, 'x', '+', '-', and '^' without any space. The coefficients range from -9 to 9, and the exponents range from 1 to 9 (If the exponent is 1, it will be omitted and won't be displayed as '^1'). The operaters will be seperated by integers or 'x' (You won't see '-+x' in the input).
The third line is the integer , ranging from -9 to 9.
It's guaranteed that there won't be two same exponents in the same polynomial, and the numerator and denominator won't be both constant 0.
Output
Output 1 line for each case.
If the limit exists, you should output it as the simplest fration (eg. -1, -1/6, 0, 3/2, 2, 3). Otherwise, output "INF" (not including the quotation marks).
Sample Input
2 x^2-2x+1 x^2-1 1 9x^8 -9x^9 9
Sample Output
0 -1/9
#include<iostream>
#include<stdio.h>
using namespace std;
typedef long long ll;
struct node{
int exp,cof;
}arr[3][15];
int cnt1,cnt2;
int x;
ll gcd(ll u,ll d)
{
ll temp;
ll a = u;
ll b = d;
if(a < b)
{
temp = a;
a = b;
b = temp;
}
while(b != 0)
{
temp = a % b;
a = b;
b = temp;
}
return a;
}
ll pow(ll a,ll b)
{
if(b==0)
return 1;
ll ans=a;
b--;
while(b--)
ans*=a;
return ans;
}
ll cal(int i)
{
int cnt=(i==0)?cnt1:cnt2;
ll ans=0;
for(int j=0;j<cnt;j++)
{
if(arr[i][j].exp==-1)
continue;
ans+=arr[i][j].cof*pow(x,arr[i][j].exp);
}
return ans;
}
void lbd()
{
for(int i=0;i<cnt1;i++)
{
if(arr[0][i].exp==-1)continue;
arr[0][i].cof*=arr[0][i].exp;
arr[0][i].exp--;
}
for(int i=0;i<cnt2;i++)
{
if(arr[1][i].exp==-1)continue;
//if(arr[1][i].cof==-1)cout<<"THIS!??"<<arr[1][i].exp<<endl;
arr[1][i].cof*=arr[1][i].exp;
arr[1][i].exp--;
}
}
void printit()
{
for(int i=0;i<cnt1;i++)
cout<<arr[0][i].cof<<"x^"<<arr[0][i].exp<<" ";
cout<<endl;
for(int i=0;i<cnt2;i++)
cout<<arr[1][i].cof<<"x^"<<arr[1][i].exp<<" ";
cout<<endl;
}
void solve()
{
while(1)
{
//printit();
ll fz=cal(0);
ll fm=cal(1);
//cout<<fz<<" "<<fm<<endl;
if(fz!=0&&fm!=0)
{
int f1=1,f2=1;
if(fz<0){
f1=-1;
fz*=-1;
}
if(fm<0){
f2=-1;
fm*=-1;
}
ll t=gcd(fz,fm);
fz/=t;
fm/=t;
if(f1*f2==-1)cout<<"-";
if(fm!=1)
cout<<fz<<"/"<<fm<<endl;
else
cout<<fz<<endl;
break;
}
if(fz==0&&fm!=0)
{
cout<<"0"<<endl;
break;
}
if(fm==0&&fz!=0)
{
cout<<"INF"<<endl;
break;
}
lbd();
}
}
void readS()
{
cnt1=0;cnt2=0;
string s;
cin>>s;
int l=s.size();
int t_cof=1,t_eps=0;
int f_x=0,f_p=0;
for(int i=0;i<l;i++)
{
if(i==0&&s[i]=='-')
{
t_cof=-1;
continue;
}
if(s[i]=='x')
{
t_eps=1;
f_x=1;
}
if(s[i]=='^')
{
f_p=1;
}
if(s[i]>='0'&&s[i]<='9')
{
if(f_x==0)
t_cof*=(s[i]-'0');
if(f_x==1&&f_p==1)
{
t_eps=(s[i]-'0');
}
if(f_x==1&&f_p==0)
t_eps=1;
}
if(s[i]=='-'||s[i]=='+')
{
f_x=0;f_p=0;
//cout<<"!"<<i<<" "<<t_cof<<" "<<t_eps<<endl;
arr[0][cnt1].cof=t_cof;
arr[0][cnt1++].exp=t_eps;
t_eps=0;
t_cof=1;
if(s[i]=='-')
t_cof=-1;
}
}
arr[0][cnt1].cof=t_cof;
arr[0][cnt1++].exp=t_eps;
t_cof=1,t_eps=0;
cin>>s;
l=s.size();
f_x=0;f_p=0;
for(int i=0;i<l;i++)
{
if(i==0&&s[i]=='-')
{
t_cof=-1;
continue;
}
if(s[i]=='x')
{
t_eps=1;
f_x=1;
}
if(s[i]=='^')
{
f_p=1;
}
if(s[i]>='0'&&s[i]<='9')
{
if(f_x==0)
t_cof*=(s[i]-'0');
if(f_x==1&&f_p==1)
{
t_eps=(s[i]-'0');
}
if(f_x==1&&f_p==0)
t_eps=1;
}
if(s[i]=='-'||s[i]=='+')
{
f_x=0;f_p=0;
//cout<<"!"<<i<<" "<<t_cof<<" "<<t_eps<<endl;
arr[1][cnt2].cof=t_cof;
arr[1][cnt2++].exp=t_eps;
t_eps=0;
t_cof=1;
if(s[i]=='-')
t_cof=-1;
}
}
arr[1][cnt2].cof=t_cof;
arr[1][cnt2++].exp=t_eps;
}
int main()
{
int cas;
scanf("%d",&cas);
while(cas--)
{
readS();
scanf("%d",&x);
solve();
}
}
写的比较丑