[Codeforces710D]Two Arithmetic Progressions(扩展中国剩余定理)
来源:互联网 发布:电路绘图软件 编辑:程序博客网 时间:2024/06/14 14:54
题目描述
传送门
题解
也就是说
将两个方程用扩展中国剩余定理合并一下求出x
然后计算一下在[L,R]内的合法解个数就行了
还有一个需要注意的是
也就是说
所以要将L更新一下
代码
#include<algorithm>#include<iostream>#include<cstring>#include<cstdio>#include<cmath>using namespace std;#define LL long longLL m1,m2,c1,c2,L,R,c,m,t,ans;LL gcd(LL a,LL b){ if (!b) return a; else return gcd(b,a%b);}void exgcd(LL a,LL b,LL &x,LL &y){ if (!b) x=1,y=0; else exgcd(b,a%b,y,x),y-=a/b*x;}LL inv(LL a,LL b){ LL x=0,y=0; exgcd(a,b,x,y); x=(x%b+b)%b; if (!x) x+=b; return x;}int main(){ scanf("%I64d%I64d%I64d%I64d%I64d%I64d",&m1,&c1,&m2,&c2,&L,&R); L=max(L,max(c1,c2)); if (L>R) { puts("0"); return 0; } c1%=m1,c2%=m2; t=gcd(m1,m2); if ((c2-c1)%t) { puts("0"); return 0; } m=m1*m2/t; c=(inv(m1/t,m2/t)*((c2-c1)/t))%(m2/t)*m1+c1; c%=m; if (c>R) { ans=(c-R)/m-(c-L)/m; if ((c-R)%m==0) ++ans; } else if (c<L) { ans=(R-c)/m-(L-c)/m; if ((L-c)%m==0) ++ans; } else ans=(R-c)/m+(c-L)/m+1; printf("%I64d\n",ans);}
0 0
- [Codeforces710D]Two Arithmetic Progressions(扩展中国剩余定理)
- codeforces 710D Two Arithmetic Progressions (扩展中国剩余定理)
- 扩展欧几里得算法,中国剩余定理(Two Arithmetic Progressions,cf 710D)
- Codeforces Problem 710D Two Arithmetic Progressions(中国剩余定理)
- cf/Codeforces ECR16-D - Two Arithmetic Progressions-中国剩余定理
- 中国剩余定理+扩展
- 中国剩余定理的扩展
- 中国剩余定理与扩展
- 扩展中国剩余定理模板
- D. Two Arithmetic Progressions
- [BZOJ2142]礼物(扩展Lucas定理+中国剩余定理)
- 【数论】【扩展欧几里得】Codeforces 710D Two Arithmetic Progressions
- pku1061(扩展欧几里德算法,中国剩余定理,模线性方程)
- 欧几里德算法与扩展(中国剩余定理)
- [HDU1573]X问题(扩展中国剩余定理)
- [Codeforces338D]GCD Table(扩展中国剩余定理)
- codeforces 338D GCD Table (扩展中国剩余定理)
- [Codeforces688D]Remainders Game(扩展中国剩余定理)
- Android仿微信图片上传,可以选择多张图片,缩放预览,拍照上传等
- 网页设计制作必须知道的10个秘诀
- 计算阶乘和
- Python常用标准库 --- time
- Android 日常开发总结的技术经验 60 条
- [Codeforces710D]Two Arithmetic Progressions(扩展中国剩余定理)
- 算法2.2 合并排序
- addAttributeToFilter condition in Magento
- Nginx的安装
- Report_server_and_alert_oracle.sh
- [安全] -- PHP API 等传输 明文加密解密方法
- Hive学习笔记8@运维相关
- 通过哪个日志查看CRS启动过程
- json单对象、多对象拼接方式