The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online J Press the Button —— 暴力

J Press the Button
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 (v+0.5) seconds (no matter what the value of the timer is before the button is pressed), where v 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 goes out (that is to say, the light is 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 a, BaoBao will immediately press the button b times; If the current time in seconds is an integer and is a multiple of another given integer c, DreamGrid will immediately press the button d 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 d times after BaoBao finishes pressing the button b times.
The game starts at 0 second and ends after t seconds (if the button will be pressed at t 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 T (about 100), indicating the number of test cases. For each test case:

The first and only line contains six integers a, b, c, d, v and t (1≤a,b,c,d≤10
​6
​​ , 1≤v,t≤10
​12
​​ ). 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.
题目链接:https://pintia.cn/problem-sets/1036903825309761536/problems/1041156452403703808
题意:
给你a,b,c,d,v,t五个数
有两个人在敲按钮,第一个人在a的倍数及0的时候会敲这个按钮b下,第二个人会在c的倍数的时间及0的时候敲d下,敲完后这个按钮会亮v+0.5秒,总共时间是t,如果这个人在按钮暗的时候敲b下,那么第一次敲会使这个按钮亮起来,在亮的时候敲这个按钮会使一个ans+1,那么就是加了b-1下,若是在亮的时候敲b下,ans就加了b下,问你最后ans是多少
题解:
我们可以知道,就算a,c这两个数互质,t足够大,那么敲的次数就是a+c下,不超过2e6,在乘上100,就是1e8的时间复杂度。那么我们只需要把它塞到一个数组里,一个一个判过去就好了。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pii;
const int mod=(int)1e9+7;
const int maxn=100005;
//priority_queue<pii, vector<pii>,greater<pii>  > aim;
pii aim[4000009];
int n;
ll a,b,c,d,v,t;
int main(){
    int T;
    cin>>T;
    while(T--){
        n=0;
        scanf("%lld%lld%lld%lld%lld%lld",&a,&b,&c,&d,&v,&t);
        ll lcm=a*c/__gcd(a,c);
        ll upa=lcm/a;
        ll upb=lcm/c;

        ll ar1=1,ar2=1;
        while(ar1<=upa&&ar2<=upb){
            if(a*ar1<=c*ar2)aim[++n]={a*ar1,1},ar1++;
            else aim[++n]={c*ar2,2},ar2++;
        }
        while(ar1<=upa)aim[++n]={a*ar1,1},ar1++;
        while(ar2<=upb)aim[++n]={c*ar2,2},ar2++;

        ll nowti=0,ans=b+d-1,tmp=0,tmp2=0,yu=t%lcm,bei=t/lcm;
        //printf("t=%lld lcm=%lld \n",t,lcm);

        for(int ar=1;ar<=n;ar++){
            ll fi=aim[ar].first;
            //printf("when %lld at %lld , ",aim.top().second,fi);
            if(aim[ar].second==1){
                if(fi<=nowti+v){
                    tmp+=b;
                    if(fi<=yu){
                        tmp2+=b;
                    }
                }
                else {
                    tmp+=b-1;
                    if(fi<=yu){
                        tmp2+=b-1;
                    }
                }
            }
            else{
                if(fi<=nowti+v){
                    tmp+=d;
                    if(fi<=yu){
                        tmp2+=d;
                    }
                }
                else {
                    tmp+=d-1;
                    if(fi<=yu){
                        tmp2+=d-1;
                    }
                }
            }
            //printf("tmp = %lld\n",tmp);
            nowti=fi;
        }
        //printf("bei = %lld , tmp = %lld , tmp2 = %lld\n",bei,tmp,tmp2);
        ans+=bei*tmp+tmp2;
        printf("%lld\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/tianyizhicheng/article/details/82728416