CodeForces - 1055C Lucky Days

本文探讨了两个竞争性程序员,Alice和Bob,在寻找连续幸运日子的最大数量过程中遇到的问题。通过分析各自的幸运和不幸日子周期,提出了一个算法解决方案,使用C++实现,以找出两人共同的最长连续幸运日。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

Input

0 2 5
1 3 5

Output

2

Input

0 1 3
2 3 6

Output

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.

题解:让 l 或 r 对其一个若无法对其 那就 两边取个最优解

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll cul(ll l1, ll r1, ll l2, ll r2, ll t) {
	l1 += t;
	r1 += t;
	return max(0ll, min(r1, r2) - max(l1, l2) + 1);
}

int main()
{
	ll l1, r1, v1, l2, r2, v2;
	while(~scanf("%lld %lld %lld %lld %lld %lld", &l1, &r1, &v1, &l2, &r2, &v2)) {
		l1++, r1++, l2++, r2++;
		ll d = __gcd(v1, v2);
		ll l = abs(r1 - r2);
		ll ans = 0;
		ll t1 = l / d * d;
		ll t2 = t1 + d;
		ans = max(ans, cul(l1, r1, l2, r2, t1));
		ans = max(ans, cul(l1, r1, l2, r2, t2));
		ans = max(ans, cul(l2, r2, l1, r1, t1));
		ans = max(ans, cul(l2, r2, l1, r1, t2));
		cout << ans << endl;
	}
	return 0;
} 

 

引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值