CodeForces1345A. Alarm Clock

Polycarp需要睡 a a 分钟, b b 分钟后他的闹钟第一次响,闹钟每一次响如果他还没睡够会让闹钟 c c 分钟后再响,被闹钟吵醒之后他需要 d d 分钟才能睡着,问多久他才能睡够。

注意,即使睡够了,也只有闹钟响的时候Polycarp才会起床。

显然, a b a\leq b 时直接输出 b b 即可;

a > b a>b 并且 c d c\leq d 时,Polycarp永远睡不够;

剩余的情况就是Polycarp在 b b 分钟之后还需要睡 ( a b ) (a-b) 分钟,每 c c 分钟可以睡 ( c d ) (c-d) 分钟,计算一下,向上取整

```
#include <bits/stdc++.h>
#define pb push_back 
#define fir first
#define sec second
#define ms(a,b) memset(a,b,sizeof(a)) 
#define INF 0x3f3f3f3f
#define sp system("pause")
using namespace std;
typedef long long ll;
typedef double db;
const int N=1e5+5;
const int mod=10007;
const db pi=acos(-1.0);
int main()
{
    int t;
    ll a,b,c,d;
    cin>>t;
    while(t--)
    {
        cin>>a>>b>>c>>d;
        if(a<=b) 
        {
            cout<<b<<endl;
            continue;
        }
        if(c<=d)
        {
            cout<<-1<<endl;
            continue;
        }
        ll ans=b+(a-b)/(c-d)*c;
        if((a-b)%(c-d)!=0) ans+=c;//手动向上取整,取余不为0就+1
        cout<<ans<<endl;
    }
    #ifndef ONLINE_JUDGE
    sp;
    #endif
}

猜你喜欢

转载自blog.csdn.net/Luowaterbi/article/details/106215766