CCPC 2016 长春赛区 hdu5912

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值