ZOJ Monthly, January 2018 - F The Limit

题意:

求分数的极限。


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

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值