Problem Description
度度熊最近学习了多项式和极限的概念。
现在他有两个多项式 f(x) 和 g(x),他想知道当 x 趋近无限大的时候,f(x)/g(x) 收敛于多少。
Input
第一行一个整数 T (1≤T≤100) 表示数据组数。
对于每组数据,第一行一个整数 n(1≤n≤1,000) n−1 表示多项式 f 和 g 可能的最高项的次数(最高项系数不一定非0)。
接下来一行 n 个数表示多项式 f,第 i 个整数 fi (0≤fi≤1,000,000) 表示次数为 i−1 次的项的系数。
接下来一行 n 个数表示多项式 g,第 i 个整数 gi (0≤gi≤1,000,000) 表示次数为 i−1 次的项的系数。
数据保证多项式 f 和 g 的系数中至少有一项非0。
Output
对于每组数据,输出一个最简分数 a/b(a 和 b 的最大公约数为1)表示答案。
如果不收敛,输出 1/0。
Sample Input
3
2
0 2
1 0
2
1 0
0 2
3
2 4 0
1 2 0Sample Output
1/0
0/1
2/1Hint
这些多项式分别为
f(x) = 2x
g(x) = 1
f(x) = 1
g(x) = 2x
f(x) = 4x + 2
g(x) = 2x + 1
思路:首先记录 f(x)、g(x) 两个式子的系数不为 0 的最高次项,然后比较两者最高次项位置的关系,只有相等时求 gcd 即可
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>
const int MOD = 1E9+7;
const int N = 1000+5;
const int dx[] = {0,0,-1,1,-1,-1,1,1};
const int dy[] = {-1,1,0,0,-1,1,-1,1};
using namespace std;
int a[N];
int b[N];
int main(){
int t;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
scanf("%d",&b[i]);
int up,upPos;
for(int i=n;i>=1;i--){
if(a[i]!=0){
upPos=i;
up=a[i];
break;
}
}
int down,downPos;
for(int i=n;i>=1;i--){
if(b[i]!=0){
downPos=i;
down=b[i];
break;
}
}
if(upPos>downPos)
printf("1/0\n");
else if(upPos<downPos)
printf("0/1\n");
else{
int gcd=__gcd(up,down);
printf("%d/%d\n",up/gcd,down/gcd);
}
}
}