Problem Description
Mr. Frog recently studied how to add two fractions up, and he came up with an evil idea to trouble you by asking you to calculate the result of the formula below:
As a talent, can you figure out the answer correctly?
As a talent, can you figure out the answer correctly?
Input
The first line contains only one integer T, which indicates the number of test cases.
For each test case, the first line contains only one integer n ( n≤8 ).
The second line contains n integers: a1,a2,⋯an(1≤ai≤10 ).
The third line contains n integers: b1,b2,⋯,bn(1≤bi≤10) .
For each test case, the first line contains only one integer n ( n≤8 ).
The second line contains n integers: a1,a2,⋯an(1≤ai≤10 ).
The third line contains n integers: b1,b2,⋯,bn(1≤bi≤10) .
Output
For each case, print a line “Case #x: p q”, where x is the case number (starting from 1) and p/q indicates the answer.
You should promise that p/q is irreducible.
You should promise that p/q is irreducible.
Sample Input
1 2 1 1 2 3
Sample Output
Case #1: 1 2HintHere are the details for the first sample: 2/(1+3/1) = 1/2
观察数据范围,直接从下往上计算即可
#include<cstdio>
using namespace std;
int a[101],b[101];
inline int gcd(int x,int y)
{
int m=x%y;
while(m!=0)
{
x=y;
y=m;
m=x%y;
}
return y;
}
int main()
{
int T,k=0;
scanf("%d",&T);
while(T>0)
{
T--;
k++;
int n;
scanf("%d",&n);
int i;
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
for(i=1;i<=n;i++)
scanf("%d",&b[i]);
int b1=b[n],a1=a[n];
for(i=n-1;i>=1;i--)
{
b1+=a1*a[i];
a1*=b[i];
int t=a1;
a1=b1;
b1=t;
}
int xt=gcd(b1,a1);
printf("Case #%d: %d %d\n",k,b1/xt,a1/xt);
}
return 0;
}