当前位置:网站首页>【Pei Shu Theorem】CF1055C Lucky Days
【Pei Shu Theorem】CF1055C Lucky Days
2022-08-10 05:04:00 【line chess】
Lucky Days
better look
给定 l a , r a , t a , l b , r b , t b l_a,r_a,t_a,l_b,r_b,t_b la,ra,ta,lb,rb,tb,对于所有的非负整数 k k k,将区间 [ l a + k t a , r a + k t a ] [l_a+kt_a,r_a+kt_a] [la+kta,ra+kta] 打上标记 1 1 1,将区间 [ l b + k t b , r b + k t b ] [l_b+kt_b,r_b+kt_b] [lb+ktb,rb+ktb] 打上标记 2 2 2.Find the longest continuous interval such that all positions in the interval are marked at the same time 1 , 2 1,2 1,2 标记.
样例一
0 2 5
1 3 5
2
样例二
0 1 3
2 3 6
1
思路
The question requires the length of the maximum coincidence between the two intervals.
First, let's think about the first point:To maximize the coincidence of the two intervals,The two intervals need to be as close as possible.The optimal case is two intervalsThe left endpoints are as equal as possible,This is the maximum degree of overlap.
即使 l a + x ∗ t a = l b + y ∗ t b la + x * ta = lb + y * tb la+x∗ta=lb+y∗tb
Shift the equation to get x ∗ t a − y ∗ t b = l b − l a x * ta - y * tb = lb - la x∗ta−y∗tb=lb−la (此时我们假设 l a < l b la < lb la<lb,The relevant operations have been done in the code,This is just for convenience)
We need to see if the equation has a solution,formula substructure and 裴蜀定理比较像,拿出来进行对比.
裴蜀定理 : 存在整数 x , y x, y x,y,满足 a ∗ x + b ∗ y = g c d ( a , b ) a * x + b * y = gcd(a,b) a∗x+b∗y=gcd(a,b)
This formula is carried out y y yThe sign becomes positive,表示 y y y是整数,则 x ∗ t a + y ∗ t b = l b − l a x * ta + y * tb = lb - la x∗ta+y∗tb=lb−la
我们令 d = g c d ( t a , t b ) d = gcd(ta, tb) d=gcd(ta,tb)
Then you can find out if d ∣ ( l b − l a ) d | (lb - la) d∣(lb−la),Then the formula has a solution,Left endpoints can be coincident.
But what if the left endpoints cannot coincide,Get as close as possible.
Don't forget the right side of the formula at this point l b − l a lb-la lb−la代表的是什么,是 区间aThe distance the left endpoint is moved,那么因为 x ∗ t a + y ∗ t b = d x * ta + y * tb = d x∗ta+y∗tb=dsolution exists,则区间aThe distance moved can be further changed d d d .
注意:I'm talking about the intervala可以移动,It means that a certain state must exist,The distance between the upper and lower two people has changed in two intervals,It's called interval movement and it's easier to understand.如
t a = 3 , [ 1 , 4 ] − > [ 4 , 7 ] − > [ 7 , 10 ] − > [ 10 , 13 ] ta = 3,[1,4]->[4,7]->[7,10]->[10,13] ta=3,[1,4]−>[4,7]−>[7,10]−>[10,13]
t b = 4 , [ 3 , 5 ] − > [ 7 , 9 ] − > [ 11 , 13 ] tb = 4,[3,5]->[7,9]->[11,13] tb=4,[3,5]−>[7,9]−>[11,13]
[ 1 , 4 ] [ 3 , 5 ] [1,4][3,5] [1,4][3,5]The left endpoint differs 2 2 2,This is an interval state,通过移动,Another interval state occurs [ 10 , 13 ] [ 11 , 13 ] [10,13][11,13] [10,13][11,13]The left endpoint differs 1 1 1.Moved a step( d = g c d ( 3 , 4 ) = 1 d = gcd(3,4) = 1 d=gcd(3,4)=1)
The left endpoints are approached as close as possible without overlapping.
There are two states that may be appropriate.
d i s dis dis代表aThe left endpoint of the interval and bThe minimum distance between the left endpoints of the interval(aI think the left endpoint of the interval is less than or equal tob区间左端点),即 d i s = ( l b − l a ) % d dis = (lb - la) \% d dis=(lb−la)%d
区间aMove the left endpoint to and intervalbThe left endpoint differs ( l b − l a ) % d (lb-la) \% d (lb−la)%d,Possibly the closest
- m i n ( r b − l b + 1 , r a − l a + 1 − d i s ) min(rb - lb + 1, ra - la + 1 - dis) min(rb−lb+1,ra−la+1−dis)
Then the above situation moves a step back,即区间aThe left endpoint exceeds the intervalb左端点 d − ( l b − l a ) d-(lb-la)%d d−(lb−la)
- d i s = d − d i s dis = d - dis dis=d−dis
- m i n ( r a − l a + 1 , r b − l b + 1 − d i s ) min(ra - la + 1, rb - lb + 1 - dis) min(ra−la+1,rb−lb+1−dis)
Please draw the two calculations to understand the calculation method.
计算:The case where the left endpoints are coincident can be combined in the code where the left endpoints can be combined,That is, left endpoint merge is a special case of left endpoint not merged.
代码
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int N = 1e5 + 5, mod = 1e9 + 7;
// [l[a] + k * ta, r[a] + k * ta]
// 3 [1, 4] [4, 7] [7, 10] [10, 13] [13, 16] ---
// 4 [3, 5] [7, 9] [11, 13] [15, 17] ---
// The starting point is as the same as possible
// 裴蜀定理:存在x,y 使 ax + by = gcd(a, b)
// la + x * ta = lb + y * tb
// x * ta - y * tb = lb - la
// gcd(ta, tb) | (lb - la)有解
// d = gcd(ta, tb) Seen as the relative moving distance
// la -> la + d -> la + k * d lb
// 差 = lb - la
// dis = (lb - la) % d
void solve()
{
int la, ra, ta, lb, rb, tb;
cin >> la >> ra >> ta >> lb >> rb >> tb;
if(la > lb)
{
swap(la, lb);
swap(ra, rb);
swap(ta, tb);
}
int d = __gcd(ta, tb);
int dis = (lb - la) % d; // Difference of left endpoint
int ans = 0;
ans = max(ans, min(rb - lb + 1, ra - la + 1 - dis));
dis = d - dis; // 向右移动一步
ans = max(ans, min(ra - la + 1, rb - lb + 1 - dis));
cout << ans << "\n";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;
// cin >> t;
t = 1;
while(t--)
solve();
return 0;
}
边栏推荐
猜你喜欢
随机推荐
剑指Offer 033.变位数组
60行从零开始自己动手写FutureTask是什么体验?
JS获取当前时间的年、月、日、时间等
Shield Alt hotkey in vscode
深度梳理:防止模型过拟合的方法汇总
电流探头如何设置示波器参数
如何取得某月的最后一天
各位大佬,idea中测试使用FlinkCDC SQL 读取Mysql 数据写入Kafka中,代码中创
awk of the Three Musketeers of Shell Programming
告诉你如何从keil工程知道使用了多少RAM和ROM空间
请教一下各位大佬。CDC社区中FlinkCDC2.2.0版本有说明支持的sqlserver版本 ,请
【心理学·人物】第二期(学术X综艺)
cmake
解决“File has been changed outside the editor, reload?”提示
FPGA工程师面试试题集锦31~40
华为交换机配置日志推送
2022年T电梯修理考试题及模拟考试
ECMAScript6 Proxy和Reflect 对象操作拦截以及自定义
PHPCMS仿站从入门到精通,小白看这一套课程就够了
2022年R2移动式压力容器充装考试题库模拟考试平台操作