1. 程式人生 > >Codeforces Round #467 C題

Codeforces Round #467 C題

A題B題就不用說了吧,連我都寫出來了。

說一下C題吧,就是因為C題才寫這篇部落格的,題目如下。

C. Save Energy!time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

Julia is going to cook a chicken in the kitchen of her dormitory. To save energy, the stove in the kitchen automatically turns off after k

minutes after turning on.

During cooking, Julia goes to the kitchen every d minutes and turns on the stove if it is turned off. While the cooker is turned off, it stays warm. The stove switches on and off instantly.

It is known that the chicken needs t minutes to be cooked on the stove, if it is turned on, and 2t

 minutes, if it is turned off. You need to find out, how much time will Julia have to cook the chicken, if it is considered that the chicken is cooked evenly, with constant speed when the stove is turned on and at a constant speed when it is turned off.

Input

The single line contains three integers kd

 and t (1 ≤ k, d, t ≤ 1018).

Output

Print a single number, the total time of cooking in minutes. The relative or absolute error must not exceed 10 - 9.

Namely, let's assume that your answer is x and the answer of the jury is y. The checker program will consider your answer correct if .

ExamplesinputCopy
3 2 6
output
6.5
inputCopy
4 2 20
output
20.0
Note

In the first example, the chicken will be cooked for 3 minutes on the turned on stove, after this it will be cooked for . Then the chicken will be cooked for one minute on a turned off stove, it will be cooked for . Thus, after four minutes the chicken will be cooked for . Before the fifth minute Julia will turn on the stove and after 2.5 minutes the chicken will be ready .

In the second example, when the stove is turned off, Julia will immediately turn it on, so the stove will always be turned on and the chicken will be cooked in 20 minutes.

題目其實並不難,稍微列舉幾種情況你就可以發現規律。規律就是:cook的時間呈週期性,即在這個週期裡,先是在turn on的cook,後是turn off的cook。 比如,第一組資料 3 2 6 ,週期為 4, 在週期4裡面,turn on 的cook佔3,turn off的cook 佔1.

如果資料是2 3 6,同樣也會有周期性,週期為3.  我就提示到這吧,相信知道這一點就能有思路了。

另我WA的是,double還不能用double,還要用long double,這還是第一次見,仔細想想,畢竟前面整數都long long 了,整數和小數又要在一起混合運算,所以必須用long double.

程式碼如下:

#include<iostream>
using namespace std;
#include<cstdio>
#include<cstring>
#include<stack>
#include<map>
#include<cstdlib>
#include<queue>
#include<algorithm>
typedef long long ll;
ll a,b,c;
#define eps 1e-10
int main()
{
	while( scanf("%I64d %I64d %I64d",&a,&b,&c) == 3 )
	{
		if( a%b == 0 )
		{
			printf("%I64d\n",c);
			continue;
		}
		ll period;
		if( a < b )
			period = b;
		else period =  (ll)(a/b)*b + b;
		long double val = a+(period-a)*0.5;
		long double ans = 0;
		long double temp;
		temp = ( (ll)(c/val) )*val;
		ans += ( (ll)(c/val) )*period;
		if( (double)(c-temp) < a  )
			ans += c-temp;
		else ans += a+(c-temp-a)*2;
		printf("%.25lf\n",(double)ans);
	}
	return 0;
}
/*

1 629 384378949109878497

  */