Round A APAC Test 2016: Problem B. gCube
2015-09-19 16:06
375 查看
其实题目(见下)很简单,唯一的问题是边长最大可取10^9,计算的时候要小心溢出
两种解决方法:
1, 先开方在做乘法
2. 取对数后计算
我用的第二种,
long double *a, t, sum = 0;
unsigned int l, r;
fin >> N >> M;
a = new long double
;
for (size_t i = 0; i < N; i++)
{
fin >> t;
a[i] = log(t);
}
for (size_t i = 0; i < M; i++)
{
fin >> l >> r;
sum = 0;
for (size_t j = l; j <= r; j++)
{
sum += a[j];
}
fout << setiosflags(ios::fixed) << setprecision(7) << expl(sum / (r - l + 1)) << endl;
}
附题目:
Problem
Googlers are very interested in cubes, but they are bored with normal three-dimensional cubes and also want to think about other kinds of cubes! A "D-dimensional cube" has D dimensions, all of equal length. (D may be any positive integer; for example, a
1-dimensional cube is a line segment, and a 2-dimensional cube is a square, and a 4-dimensional cube is a hypercube.) A "D-dimensional cuboid" has D dimensions, but they might not all have the same lengths.
Suppose we have an N-dimensional cuboid. The N dimensions are numbered in order (0, 1, 2, ..., N - 1), and each dimension has a certain length. We want to solve many subproblems of this type:
1. Take all consecutive dimensions between the Li-th dimension and Ri-th dimension, inclusive.
2. Use those dimensions to form a D-dimensional cuboid, where D = Ri - Li + 1. (For example, if Li = 3 and Ri = 6, we would form a 4-dimensional
cuboid using the 3rd, 4th, 5th, and 6th dimensions of our N-dimensional cuboid.)
3. Reshape it into a D-dimensional cube that has exactly the same volume as that D-dimensional cuboid, and find the edge length of that cube.
Each test case will have M subproblems like this, all of which use the same original N-dimensional cuboid.
Each test case begins with two integers N and M; N is the number of dimensions and M is the number of queries. Then there is one line with N positive integers ai,
which are the lengths of the dimensions, in order. Then, M lines follow. In the ith line, there are two integers Li and Ri, which give the range of dimensions to use for the ith subproblem.
if it is within an absolute error of 10-6 of the correct answer. See the FAQ for
an explanation of what that means, and what formats of real numbers we accept.
1 ≤ ai ≤ 109.
0 ≤ Li ≤ Ri < N.
1 ≤ M ≤ 10.
1 ≤ M ≤ 100.
两种解决方法:
1, 先开方在做乘法
2. 取对数后计算
我用的第二种,
long double *a, t, sum = 0;
unsigned int l, r;
fin >> N >> M;
a = new long double
;
for (size_t i = 0; i < N; i++)
{
fin >> t;
a[i] = log(t);
}
for (size_t i = 0; i < M; i++)
{
fin >> l >> r;
sum = 0;
for (size_t j = l; j <= r; j++)
{
sum += a[j];
}
fout << setiosflags(ios::fixed) << setprecision(7) << expl(sum / (r - l + 1)) << endl;
}
附题目:
Problem B. gCube
ProblemGooglers are very interested in cubes, but they are bored with normal three-dimensional cubes and also want to think about other kinds of cubes! A "D-dimensional cube" has D dimensions, all of equal length. (D may be any positive integer; for example, a
1-dimensional cube is a line segment, and a 2-dimensional cube is a square, and a 4-dimensional cube is a hypercube.) A "D-dimensional cuboid" has D dimensions, but they might not all have the same lengths.
Suppose we have an N-dimensional cuboid. The N dimensions are numbered in order (0, 1, 2, ..., N - 1), and each dimension has a certain length. We want to solve many subproblems of this type:
1. Take all consecutive dimensions between the Li-th dimension and Ri-th dimension, inclusive.
2. Use those dimensions to form a D-dimensional cuboid, where D = Ri - Li + 1. (For example, if Li = 3 and Ri = 6, we would form a 4-dimensional
cuboid using the 3rd, 4th, 5th, and 6th dimensions of our N-dimensional cuboid.)
3. Reshape it into a D-dimensional cube that has exactly the same volume as that D-dimensional cuboid, and find the edge length of that cube.
Each test case will have M subproblems like this, all of which use the same original N-dimensional cuboid.
Input
The first line of the input gives the number of test cases, T. T test cases follow.Each test case begins with two integers N and M; N is the number of dimensions and M is the number of queries. Then there is one line with N positive integers ai,
which are the lengths of the dimensions, in order. Then, M lines follow. In the ith line, there are two integers Li and Ri, which give the range of dimensions to use for the ith subproblem.
Output
For each test case, output one line containing "Case #x:", where x is the test case number (starting from 1). After that, output M lines, where the ith line has the edge length for the ith subproblem. An edge length will be considered correctif it is within an absolute error of 10-6 of the correct answer. See the FAQ for
an explanation of what that means, and what formats of real numbers we accept.
Limits
1 ≤ T ≤ 100.1 ≤ ai ≤ 109.
0 ≤ Li ≤ Ri < N.
Small dataset
1 ≤ N ≤ 10.1 ≤ M ≤ 10.
Large dataset
1 ≤ N ≤ 1000.1 ≤ M ≤ 100.
Sample
Input | Output |
2 2 2 1 4 0 0 0 1 3 2 1 2 3 0 1 1 2 | Case #1: 1.000000000 2.000000000 Case #2: 1.414213562 2.449489743 |
相关文章推荐
- 简单的四则运算
- 数的奇偶性
- ACM网址
- 1272 小希的迷宫
- 1272 小希的迷宫
- hdu 1250 大数相加并用数组储存
- 矩阵的乘法操作
- 蚂蚁爬行问题
- 蚂蚁爬行问题
- 求两个数的最大公约数【ACM基础题】
- 打印出二进制中所有1的位置
- 杭电题目---一只小蜜蜂
- HDOJ 1002 A + B Problem II (Big Numbers Addition)
- POJ ACM 1001
- POJ ACM 1002
- POJ 2635 The Embarrassed Cryptographe
- POJ 3292 Semi-prime H-numbers
- ACM题库以及培养策略
- 排序算法之快速排序
- 网易2018校招题