Codeforces Contest 1055 problem C Lucky Days —— 区间相交最大长度

91 篇文章 0 订阅

Bob and Alice are often participating in various programming competitions. Like many competitive programmers, Alice and Bob have good and bad days. They noticed, that their lucky and unlucky days are repeating with some period. For example, for Alice days [la;ra] are lucky, then there are some unlucky days: [ra+1;la+ta−1], and then there are lucky days again: [la+ta;ra+ta] and so on. In other words, the day is lucky for Alice if it lies in the segment [la+kta;ra+kta] for some non-negative integer k.

The Bob’s lucky day have similar structure, however the parameters of his sequence are different: lb, rb, tb. So a day is a lucky for Bob if it lies in a segment [lb+ktb;rb+ktb], for some non-negative integer k.

Alice and Bob want to participate in team competitions together and so they want to find out what is the largest possible number of consecutive days, which are lucky for both Alice and Bob.

Input
The first line contains three integers la, ra, ta (0≤la≤ra≤ta−1,2≤ta≤109) and describes Alice’s lucky days.

The second line contains three integers lb, rb, tb (0≤lb≤rb≤tb−1,2≤tb≤109) and describes Bob’s lucky days.

It is guaranteed that both Alice and Bob have some unlucky days.

Output
Print one integer: the maximum number of days in the row that are lucky for both Alice and Bob.

Examples
inputCopy
0 2 5
1 3 5
outputCopy
2
inputCopy
0 1 3
2 3 6
outputCopy
1
Note
The graphs below correspond to the two sample tests and show the lucky and unlucky days of Alice and Bob as well as the possible solutions for these tests.
在这里插入图片描述
在这里插入图片描述

题意:

给你一个区间l1,r1,t1是他的循环节长度,再给你l2,r2,t2是它的循环节长度,问你这两个区间相交最大是多少

题解:

这道题很像上次青蛙跳那道题,我们只需要对t1,t2取个gcd,那么所有gcd的倍数就是这两个区间相对位置的改变量。那我们只需要找到l2在l1前面的最后一个位置和在后面的第一个位置,取个最大值就好了

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int l1,l2,r1,r2,t1,t2;
    scanf("%d%d%d%d%d%d",&l1,&r1,&t1,&l2,&r2,&t2);
    int g=__gcd(t1,t2);
    if(l1<l2)
        swap(l1,l2),swap(r1,r2);
    int ans=0;
    int t=(l1-l2)/g;
    l2+=t*g,r2+=t*g;
    ans=min(r1-l1+1,r2-l1+1);
    l2+=g,r2+=g;

    ans=max(ans,min(r2-l2+1,r1-l2+1));
    printf("%d\n",max(0,ans));
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值