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?
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).
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.
Sample Input
1
2
1 1
2 3
Sample Output
Case #1: 1 2
题解:
虽然是签到题,但是我做了好久。刚开始想递归求,但是发现会出现不整除的情况,后来化简公式,wa了一次,原因是没考虑到一个元素的情况。还是得加油啊。
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
int a[110],b[110];
int n;
int main()
{
int T;
scanf("%d",&T);
int ks=1;
while(T--)
{
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 fz=b[n],fm=a[n];
for(int i=n-1;i>0;i--)
{
int tmp=fz;
fz=b[i]*fm;
fm=fm*a[i]+tmp;
}
int gcd=__gcd(fz,fm);
//cout<<fz/gcd<<fm/gcd<<endl;
printf("Case #%d: %d %d\n",ks++,fz/gcd,fm/gcd);
}
return 0;
}