欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

CodeForces - 1055C Lucky Days(数论)

发布时间:2024/4/11 编程问答 48 豆豆
生活随笔 收集整理的这篇文章主要介绍了 CodeForces - 1055C Lucky Days(数论) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

题目链接:点击查看

题目大意:给出两个人的幸运日期,问交集最大可以是多少,两个人幸运日期的形式如下:

  • [l1,r1,t1][l_1,r_1,t_1][l1,r1,t1][l1+kt1,r1+kt1][l_1+kt_1,r_1+kt_1][l1+kt1,r1+kt1] 是幸运日期,其余日期为不幸运日期
  • [l2,r2,t2][l_2,r_2,t_2][l2,r2,t2][l2+kt2,r2+kt2][l_2+kt_2,r_2+kt_2][l2+kt2,r2+kt2] 是幸运日期,其余日期为不幸运日期
  • 题目分析:需要看出的是,通过适当的移动,每次都可以使得两个幸运日期的相对位置减少 gcd(t1,t2)gcd(t_1,t_2)gcd(t1,t2),所以现在我们只需要将其尽量对齐然后计算交集即可,分四种情况:


    代码:

    // #pragma GCC optimize(2) // #pragma GCC optimize("Ofast","inline","-ffast-math") // #pragma GCC target("avx,sse2,sse3,sse4,mmx") #include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<cassert> #include<bitset> #include<unordered_map> using namespace std; typedef long long LL; typedef unsigned long long ull; template<typename T> inline void read(T &x) {T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f; } template<typename T> inline void write(T x) {if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0'); } const int inf=0x3f3f3f3f; const int N=1e6+100; int cal(int l1,int r1,int l2,int r2,int delta) {l1+=delta,r1+=delta;return max(0,min(r1,r2)-max(l1,l2)+1); } int main() { #ifndef ONLINE_JUDGE // freopen("data.in.txt","r",stdin); // freopen("data.out.txt","w",stdout); #endif // ios::sync_with_stdio(false);int l1,r1,t1,l2,r2,t2;read(l1),read(r1),read(t1),read(l2),read(r2),read(t2);int gcd=__gcd(t1,t2);int d1=abs(l1-l2)/gcd*gcd;int d2=d1+gcd;int ans=0;ans=max(ans,cal(l1,r1,l2,r2,d1));ans=max(ans,cal(l1,r1,l2,r2,d2));ans=max(ans,cal(l2,r2,l1,r1,d1));ans=max(ans,cal(l2,r2,l1,r1,d2));write(ans);return 0; }

    总结

    以上是生活随笔为你收集整理的CodeForces - 1055C Lucky Days(数论)的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。