zoukankan      html  css  js  c++  java
  • hdu 5912(迭代+gcd)

    Fraction

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 97    Accepted Submission(s): 64


    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 (n8).

    The second line contains n integers: a1,a2,an(1ai10).
    The third line contains n integers: b1,b2,,bn(1bi10).
     
    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
    Hint
    Here are the details for the first sample: 2/(1+3/1) = 1/2
     
    题意:求图上的公式的结果.直接迭代求然后约分
    #include <bits/stdc++.h>
    using namespace std;
    map<int ,int> value;
    int a[10],b[10];
    int gcd(int x,int y){
        return y==0?x:gcd(y,x%y);
    }
    int main()
    {
        int tcase,t=1;
        scanf("%d",&tcase);
        while(tcase--){
            int n,res1,res2;
            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]);
            res1 = a[n],res2 = b[n];
            for(int i=n-1;i>=1;i--){
                int k = res1;
                res1 = a[i]*res1+res2;
                res2 = b[i]*k;
            }
            int d = gcd(res1,res2);
            printf("Case #%d: %d %d
    ",t++,res2/d,res1/d);
        }
        return 0;
    }
  • 相关阅读:
    [BZOJ3694]最短路
    [Usaco2011 Jan]道路和航线
    洛谷P1443马的遍历
    洛谷P1636学画画
    洛谷P1605走迷宫
    队列&广搜
    数论卷积公式and莫比乌斯反演
    数学之判断素数
    纯数学篇之欧拉定理证明
    筛素数
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5931382.html
Copyright © 2011-2022 走看看