题意:
求分数的极限。
POINT:
简单的多项式处理掉。
然后0/0的时候洛必达上下求导继续。1/0 输出inf。注意分母为1的时候。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <math.h>
using namespace std;
#define LL long long
const LL maxn = 1e6+55;
const LL mod = 1e9+7;
LL x;
LL f(string s,LL num[])
{
LL l=s.length();
LL pre=0;
LL flag=0;
LL zhi=0;
LL k=1;
for(int i=0;i<=20;i++) num[i]=0;
for(LL i=0;i<=l;i++){
if(s[i]=='+'||s[i]=='-'||i==l){
if(k==1){
num[zhi]+=pre;
}else{
num[zhi]-=pre;
}
if(s[i]=='+')
k=1;
else
k=-1;
pre=0;
flag=0;
zhi=0;
continue;
}
if(!flag){
if(s[i]=='x'){
zhi=1;
flag=1;
if(pre==0) pre=1;
}else{
pre=pre*10+s[i]-'0';
}
}else{
if(s[i]=='^'){
zhi=s[++i]-'0';
}
}
}
// for(LL i=0;i<=9;i++){
// printf("%d:%3d ",i,num[i]);
// }
// printf("\n");
LL ans=num[0];
LL base=x;
for(LL i=1;i<=9;i++){
ans+=base*num[i];
base=base*x;
}
return ans;
}
void qiudao(LL *num)
{
LL num_temp[33];
for(int i=9;i>=0;i--){
num_temp[i]=num[i+1]*(i+1);
}
for(int i=9;i>=0;i--){
num[i]=num_temp[i];
}
}
LL to_ans(LL *num)
{
LL ans=num[0];
LL base=x;
for(LL i=1;i<=9;i++){
ans+=base*num[i];
base=base*x;
}
return ans;
}
LL gcd(LL a, LL b)
{
return b==0?a:gcd(b,a%b);
}
int main()
{
LL T;
scanf("%lld",&T);
while(T--){
string s1,s2;
cin>>s1;
cin>>s2;
scanf("%lld",&x);
LL num1[33],num2[33];
LL ans1=f(s1,num1);
LL ans2=f(s2,num2);
LL now2 = ans2;
LL now1 = ans1;
while(now2==0&&now1==0){
qiudao(num1);
qiudao(num2);
now2=to_ans(num2);
now1=to_ans(num1);
}
if(now2==0){
printf("INF\n");
}else{
if(now1==0){
printf("0\n");
}else{
LL d = gcd(abs(now1), abs(now2));
now1/=d;
now2/=d;
if(now2<0) now1*=-1,now2*=-1;
if(now2==1) printf("%lld\n",now1);
else printf("%lld/%lld\n",now1,now2);
}
}
}
}