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 (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

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[],b[];
int gcd(int x,int y){
return y==?x:gcd(y,x%y);
}
int main()
{
int tcase,t=;
scanf("%d",&tcase);
while(tcase--){
int n,res1,res2;
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&a[i]);
for(int i=;i<=n;i++) scanf("%d",&b[i]);
res1 = a[n],res2 = b[n];
for(int i=n-;i>=;i--){
int k = res1;
res1 = a[i]*res1+res2;
res2 = b[i]*k;
}
int d = gcd(res1,res2);
printf("Case #%d: %d %d\n",t++,res2/d,res1/d);
}
return ;
}

hdu 5912(迭代+gcd)的更多相关文章

  1. HDU 5912 Fraction 【模拟】 (2016中国大学生程序设计竞赛(长春))

    Fraction Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  2. HDU 5912 Fraction(模拟——分子式化简求解)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5912 Problem Description Mr. Frog recently studied h ...

  3. HDU 5869 Different GCD Subarray Query (GCD种类预处理+树状数组维护)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5869 问你l~r之间的连续序列的gcd种类. 首先固定右端点,预处理gcd不同尽量靠右的位置(此时gc ...

  4. HDU 5869 Different GCD Subarray Query

    离线操作,树状数组,$RMQ$. 这个题的本质和$HDU$ $3333$是一样的,$HDU$ $3333$要求计算区间内不同的数字有几个. 这题稍微变了一下,相当于原来扫描到$i$的之后是更新$a[i ...

  5. HDU 5512 - Pagodas - [gcd解决博弈]

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5512 Time Limit: 2000/1000 MS (Java/Others) Mem ...

  6. HDU 5869 Different GCD Subarray Query rmq+离线+数状数组

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5869 Different GCD Subarray Query Time Limit: 6000/3 ...

  7. HDU 5869 Different GCD Subarray Query 树状数组 + 一些数学背景

    http://acm.hdu.edu.cn/showproblem.php?pid=5869 题意:给定一个数组,然后给出若干个询问,询问[L, R]中,有多少个子数组的gcd是不同的. 就是[L, ...

  8. hdu 6053 trick gcd 容斥

    http://acm.hdu.edu.cn/showproblem.php?pid=6053 题意:给定一个数组,我们定义一个新的数组b满足bi<ai 求满足gcd(b1,b2....bn)&g ...

  9. HDU 5869 Different GCD Subarray Query 离线+树状数组

    Different GCD Subarray Query Problem Description   This is a simple problem. The teacher gives Bob a ...

随机推荐

  1. 【NOIP考试范围】

    ※号为可能考察的算法[历年有出现过,但概率小,但最好掌握] [本图片仅作参考] 梦想总是要有的,万一实现了呢?

  2. linux swap交换分区配置

    参考 http://commandos.blog.51cto.com/154976/115288/

  3. SQL注入(SQL Injection)案例和防御方案

    sql注入(SQL Injection):就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令. SQL注入攻击的主要危害包括:非法读取.篡 ...

  4. bzoj4569: [Scoi2016]萌萌哒(ST表+并查集)

    好喵喵的题 将一个要求用ST表分割成logn个要求,如果把f[i][j]和f[u][v]在同一个集合,那么f[i][j-1]和f[u][v-1],f[i+2^(j-1)][j-1]和f[u][u+2^ ...

  5. [POI2005]Bank notes

    link 试题分析 我们发现此题是一个十分简单的多重背包.但是按照朴素写法会超时.所以要去考虑优化. 我们发现我们若$W=7$,可以拆成$1+2+4$,不用每次$1+1+1+1+1+1+1$,从$N$ ...

  6. mysql \G

    mysql 命令区分大小写.ego       (\G) Send command to mysql server, display result vertically. go        (\g) ...

  7. codevs 1492 探案第二部

    1492 探案第二部  时间限制: 1 s  空间限制: 128000 KB     题目描述 Description 我们伟大的 Sherlock·Holmes 先生最近遇上了一件相当棘手的案子,随 ...

  8. 图论:DFS序

    DFS序可以把树转到区间上再用高级数据结构维护,比树链剖分好理解很多 一个闭区间就是一棵子树 POJ3321 #include<cstdio> ; ; int n,m,id,cnt; in ...

  9. NYOJ 93 汉诺塔 (数学)

    题目链接 描述 在印度,有这么一个古老的传说:在世界中心贝拿勒斯(在印度北部)的圣庙里,一块黄铜板上插着三根宝石针.印度教的主神梵天在创造世界的时候,在其中一根针上从下到上地穿好了由大到小的64片金片 ...

  10. L - Looking for Taste Gym - 101991L 二进制枚举/思维

    方法一:因为最多是10的六次方,所以可以直接枚举二进制上的每一位来得到最优结果. AC代码: #include<iostream> #include<stack> #inclu ...