题目:
Problem
Maria has been hired by the Ghastly Chemicals Junkies (GCJ) company to help them manufacture bullseyes. A bullseye consists of a number of concentric rings (rings that are centered at the same point), and it usually represents an archery target. GCJ is interested in manufacturing black-and-white bullseyes.
Maria starts with t millilitres of black paint, which she will use to draw rings of thickness 1cm (one centimetre). A ring of thickness 1cm is the space between two concentric circles whose radii differ by 1cm.
Maria draws the first black ring around a white circle of radius r cm. Then she repeats the following process for as long as she has enough paint to do so:
- Maria imagines a white ring of thickness 1cm around the last black ring.
- Then she draws a new black ring of thickness 1cm around that white ring.
Note that each "white ring" is simply the space between two black rings.
The area of a disk with radius 1cm is π cm2. One millilitre of paint is required to cover area π cm2. What is the maximum number of black rings that Maria can draw? Please note that:
- Maria only draws complete rings. If the remaining paint is not enough to draw a complete black ring, she stops painting immediately.
- There will always be enough paint to draw at least one black ring.
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each test case consists of a line containing two space separated integers: r and t.
Output
For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is the maximum number of black rings that Maria can draw.
Limits
Small dataset
1 ≤ T ≤ 1000.
1 ≤ r, t ≤ 1000.
Large dataset
1 ≤ T ≤ 6000.
1 ≤ r ≤ 1018.
1 ≤ t ≤ 2 × 1018.
Sample
Input | |
5 | |
| |
Output | |
Case #1: 1 |
分类:数论 难度:1.5
题意:其实是一道简单的数学题,大意即为给出中心空白圆的半径r,墨水t毫升,每毫升墨水能画π的面积(就是为了把式子里的π约掉),问给定r,t,能画几个圈。每个圈宽度1,间隔也为1.
第一个圈面积:(r+1)^2 - r^2 = 2r+1
第二个圈面积:(r+3)^2 - (r+2)^2 = 2r+5
第三个圈面积:(r+5)^2 - (r+4)^2 = 2r+9
。。。
第n个圈面积:(r+2n-1)^2 - (r+2n-2)^2 = 2r+1+4(n-1) = 2r-3+4n
设a(n) = 2r-3+4n
s(n) = a(1)+a(2)+...+a(n) = n(2r-3)+4(1+2+...+n) = 2n^2+(2r-1)n
则令t = s(n),即为求一元二次方程:2n^2+(2r-1)n-t=0 的正数解 x = [sqrt( (2r-1)^2 + 8t ) - (2r-1) ] / 4
答案ans = floor(x) (x下取整)
几点注意:
1、由于long long 能表示的值最大为 9 * 10^18 ,所以读入和输出用long long 表示,不能用double读入,因为double读入10^18的数时,后几位就不准了,所以必须用long long 读入和输出
2、中间计算sqrt时,需要将r,t转换成double再计算,一开始也以为只要运算式中有double型变量或常量,就不用单写r,t的强制转换,后来发现必须写,因为2*r和8*t可能超过long long 范围。
3、最后还要对结果进行调整,保证正确,毕竟double又加sqrt,精度不好保证,经常会与正确答案偏移1个
最后贴上过了大数据的代码:
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
long long r,t;
int main()
{
freopen("A-large-practice.in","r",stdin);
freopen("A-large-practice.out","w",stdout);
int T;
scanf("%d",&T);
for(int cnt=1;cnt<=T;cnt++)
{
scanf("%lld%lld",&r,&t);
long long a = 2*r-1;
double b,x;
b=(double)a;
x = (sqrt(b*b+8*(double)t)-b)/4.0;
long long n = (long long)floor(x);
//printf("%lld\n",n);
while(t >= (2*(n+1)+a)*(n+1)) n++;
while(t < (2*n+a)*n) n--;
printf("Case #%d: %lld\n",cnt,n);
}
}