- Problem Description
Recently, qbwj bought a powerful cellphone which performs even better than most computers. He spent lots of time on it. However, this cellphone has a very strange feature which confused qbwj. There is a battery with infinite capacity in the cellphone. On each day, qbwj has three choices: use the cellphone, charge the battery or do nothing. He cannot do using and charging on the same day. Charging on the kthkth day adds kk units of power to the battery. If qbwj chooses to use the cellphone on the kthkth day, it would consume kk units of power. Note that qbwj can choose to use the cellphone if and only if there are enough units of power in the battery.
Despite of the strengths of the cellphone, qbwj couldn’t suffer it any more. So he decides to sell it at the end of TthTth day. Today is the Sth day and the battery is empty now. He wants to know how many days at maximum he can use the cellphone before selling it out.
Note that qbwj can still choose to use the cellphone on both the SthSth day and TthTth day.
- Input
The first line of the input will be an integer NN (N≤10000N≤10000) indicating the number of cases.
For each test case, two integers are given on a single line: SS TT. 1≤S≤T≤1081≤S≤T≤108.
Output
Print Case #k: d in a single line for each test case, in which kk represents the case number which starts from 11, and dd is the answer.Sample input and output
Sample Input
3
3 6
3 9
1 100000
Sample Output
Case #1: 1
Case #2: 3
Case #3: 49994
Hint
For the first sample, we charge the cellphone on the 3rd3rd, 4th4th, 5th5th day and use it on the 6th6th day.分析
- 这是一道贪心题
我们要尽可能多的获得使用手机的天数,最好的方法就是在电量足够的时候尽早的使用手机。
所以在第s天充电s
第s+1天充电s+1,总电量2*s+1
第s+2天用电s+2,总电量s-1
也就是说每两天要使用s中的一格电,且在s+3天的时候必须继续充电,每两天成一个循环。
所以过了2*s的时候刚好把s格电用完,再加上原来的s天。所用天为3*s,其中有s天是用电的。剩余部分再除以2相加得用电总天数。
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
long long i,s,t,n,tot;
scanf("%lld",&n);
for (i=0;i<n;i++)
{
scanf("%lld %lld",&s,&t);
tot=0;
while (t>=3*s+1)
{
tot+=s;
s=3*s+1;
}
tot+=(t-s)/2;
printf("Case #%lld: %lld\n",i+1,tot);
}
return 0;
}