B - Limit
You are given two polynomials:
- P(x) = a0·xn + a1·xn - 1 + ... + an - 1·x + an and
- Q(x) = b0·xm + b1·xm - 1 + ... + bm - 1·x + bm.
Calculate limit .
Input
The first line contains two space-separated integers n and m (0 ≤ n, m ≤ 100) — degrees of polynomials P(x) and Q(x) correspondingly.
The second line contains n + 1 space-separated integers — the factors of polynomial P(x): a0, a1, ..., an - 1, an ( - 100 ≤ ai ≤ 100, a0 ≠ 0).
The third line contains m + 1 space-separated integers — the factors of polynomial Q(x): b0, b1, ..., bm - 1, bm ( - 100 ≤ bi ≤ 100, b0 ≠ 0).
Output
If the limit equals + ∞, print "Infinity" (without quotes). If the limit equals - ∞, print "-Infinity" (without the quotes).
If the value of the limit equals zero, print "0/1" (without the quotes).
Otherwise, print an irreducible fraction — the value of limit , in the format "p/q" (without the quotes), where p is the — numerator, q (q > 0) is the denominator of the fraction.
Example
2 1
1 1 1
2 5
Infinity
1 0
-1 3
2
-Infinity
0 1
1
1 0
0/1
2 2
2 1 6
4 5 -7
1/2
1 1
9 0
-5 2
-9/5
Note
Let's consider all samples:
You can learn more about the definition and properties of limits if you follow the link: http://en.wikipedia.org/wiki/Limit_of_a_function
这个题目的意思就是,给你两个关于x的多项式,一个作为分子,一个作为分母,x的取值趋于正无穷,求这个分式的值趋于什么(0,inf,-inf或某个确定的值)
其中最高项系数不为0.
那么,我们发现,由于x趋于无穷大,那么,除了两个最高项,其他就不用考虑了.
如果n>m,则趋于无穷大,至于是正还是负,要看两个系数同号还是异号.
如果n<m,则值趋向与0.
如果n=m,则输出某数的分数形式,还要用gcd约分一下,也要注意符号.
1 #include<cstdio>
2 #include<cstring>
3 #include<algorithm>
4 using namespace std;
5 int n,m,a[105],b[105];
6 int read(){
7 int x=0,f=1; char ch=getchar();
8 while (ch<'0'||ch>'9'){if (ch=='-') f=-f; ch=getchar();}
9 while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
10 return x*f;
11 }
12 int gcd(int x,int y){
13 return y==0?x:gcd(y,x%y);
14 }
15 int main(){
16 n=read(),m=read();
17 for (int i=0; i<=n; i++) a[i]=read();
18 for (int j=0; j<=m; j++) b[j]=read();
19 if (n>m){
20 if (a[0]*b[0]>0) printf("Infinity"); else
21 if (a[0]*b[0]<0) printf("-Infinity");
22 }else
23 if (n<m){
24 printf("0/1");
25 }else{
26 int x=a[0],y=b[0];
27 if (x<0) x=-x; if (y<0) y=-y;
28 int K=gcd(x,y);
29 x/=K,y/=K;
30 if (a[0]*b[0]<0) printf("-");
31 printf("%d/%d",x,y);
32 }
33 return 0;
34 }