2018 icpc 青岛网络赛 J.Press the Button

Press the Button

Time Limit: 1 Second      Memory Limit: 131072 KB

BaoBao and DreamGrid are playing a game using a strange button. This button is attached to an LED light (the light is initially off), a counter and a timer and functions as follows:

  • When the button is pressed, the timer is set to seconds (no matter what the value of the timer is before the button is pressed), where is a given integer, and starts counting down;

  • When the button is pressed with the LED light off, the LED light will be lit up;

  • When the button is pressed with the LED light on, the value of the counter will be increased by 1;

  • When the timer counts down to 0, the LED light turns off.

During the game, BaoBao and DreamGrid will press the button periodically. If the current real time (that is to say, the time elapsed after the game starts, NOT the value of the timer) in seconds is an integer and is a multiple of a given integer , BaoBao will immediately press the button times; If the current time in seconds is an integer and is a multiple of another given integer , DreamGrid will immediately press the button times.

Note that

  • 0 is a multiple of every integer;

  • Both BaoBao and DreamGrid are good at pressing the button, so it takes no time for them to finish pressing;

  • If BaoBao and DreamGrid are scheduled to press the button at the same second, DreamGrid will begin pressing the button times after BaoBao finishes pressing the button times.

The game starts at 0 second and ends after seconds (if the button will be pressed at seconds, the game will end after the button is pressed). What's the value of the counter when the game ends?

Input

There are multiple test cases. The first line of the input contains an integer (about 100), indicating the number of test cases. For each test case:

The first and only line contains six integers , , , , and (, ). Their meanings are described above.

Output

For each test case output one line containing one integer, indicating the value of the counter when the game ends.

Sample Input

2
8 2 5 1 2 18
10 2 5 1 2 10

Sample Output

6
4

Hint

We now explain the first sample test case.

  • At 0 second, the LED light is initially off. After BaoBao presses the button 2 times, the LED light turns on and the value of the counter changes to 1. The value of the timer is also set to 2.5 seconds. After DreamGrid presses the button 1 time, the value of the counter changes to 2.

  • At 2.5 seconds, the timer counts down to 0 and the LED light is off.

  • At 5 seconds, after DreamGrid presses the button 1 time, the LED light is on, and the value of the timer is set to 2.5 seconds.

  • At 7.5 seconds, the timer counts down to 0 and the LED light is off.

  • At 8 seconds, after BaoBao presses the button 2 times, the LED light is on, the value of the counter changes to 3, and the value of the timer is set to 2.5 seconds.

  • At 10 seconds, after DreamGrid presses the button 1 time, the value of the counter changes to 4, and the value of the timer is changed from 0.5 seconds to 2.5 seconds.

  • At 12.5 seconds, the timer counts down to 0 and the LED light is off.

  • At 15 seconds, after DreamGrid presses the button 1 time, the LED light is on, and the value of the timer is set to 2.5 seconds.

  • At 16 seconds, after BaoBao presses the button 2 times, the value of the counter changes to 6, and the value of the timer is changed from 1.5 seconds to 2.5 seconds.

  • At 18 seconds, the game ends.

题意: 

每a秒(c)就会按下按钮b次(d),每次计时器就会重置成v+0.5, 在这段时间内灯亮着,0时刻按下(b+d)次。

当灯亮着的时候,计数器加上你按下的次数的值

当灯灭的时候,会先消耗一次让灯亮起,然后将后续按压的次数记下

 

求出a和c最小的公倍数之内的计数器次数,然后把剩下的单独求一次就可以了

嗯  当时在现场的时候就有想到这样的做法,但是当时脑子秀逗了,感觉求出他们的最小公倍数那也是1e12,肯定tle,我简直就是睿智,因为a和c最大1e6,也就是说最坏情况只有1e6而已,orz

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 ll T,a,b,c,d,v,t;
 5 
 6 
 7 ll gcd(ll a,ll b)
 8 {
 9     return !b?a:gcd(b,a%b);
10 }
11 int main()
12 {
13     scanf("%lld",&T);
14     while(T--)
15     {
16         ll ans = 0;
17         scanf("%lld%lld%lld%lld%lld%lld",&a,&b,&c,&d,&v,&t);
18         ll lcm = a*c/gcd(a,c);
19         if(v >= a || v  >= c)
20         {
21             ans += t/a*b+t/c*d +(b+d-1);
22         }
23         else
24         {
25             ll num = t / lcm;
26             ll cnt = v;
27             ll tmpa = a;
28             ll tmpb = c;
29             while(tmpa <= lcm || tmpb <= lcm)
30             {
31                 if(tmpa < tmpb)
32                 {
33                     ans += b;
34                     if(tmpa > cnt)ans --;
35                     cnt = tmpa + v;
36                     tmpa += a;
37 
38                 }
39                 else
40                 {
41                     ans += d;
42                     if(tmpb > cnt)ans--;
43                     cnt = tmpb + v;
44                     tmpb += c;
45                 }
46             }
47             ans *= num;
48             t %= lcm;
49             tmpa = a;
50             tmpb = c;
51             cnt = v;
52             while(tmpa <= t || tmpb <= t)
53             {
54                 if(tmpa < tmpb)
55                 {
56                     ans += b;
57                     if(tmpa > cnt)ans--;
58                     cnt = tmpa + v;
59                     tmpa += a;
60                 }
61                 else
62                 {
63                     ans += d;
64                     if(tmpb > cnt)ans --;
65                     cnt = tmpb + v;
66                     tmpb += c;
67                 }
68             }
69             ans += b+d-1;
70         }
71         printf("%lld\n",ans);
72     }
73     return 0;
74 }
View Code

 

posted @ 2018-09-17 20:16  进击的黑仔  阅读(577)  评论(0编辑  收藏  举报