Codeforces 710 D. Two Arithmetic Progressions
Description
\(x=a_1k+b_1=a_2l+b_2,L\leqslant x \leqslant R\) 求满足这样条件的 \(x\) 的个数.
Sol
扩展欧几里得+中国剩余定理.
发现这个相当于一个线性方程组.
\(x \equiv b_1(mod a_1)\)
\(x \equiv b_2(mod a_2)\)
将原来两式相减得到 \(a_1k-a_2l=b_2-b_1\)
这个用扩展欧几里得求一下,如果 \((a_1,a_2)\nmid (b_2-b_1)\) 显然无解.
用扩展欧几里得求的方程是 \(a_1k-a_2l=(a_1,a_2)\) ,将这个等式再乘上 \(\frac{b_2-b_1}{(a_1,a_2)}\)
现在我们得到了一组合法解,通解就是 \(k=k_0+\frac {a_2}{(a_1,a_2)},l=l_0-\frac {a_1}{(a_1,a_2)}\)
求得最小正数解可以对 \(\frac {a_2}{(a_1,a_2)}\) 取模.
然后原方程的解个数就是 \(k+n[a_1,a_2]\) ,不要忘记计算端点的这个值.
Code
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std; typedef long long LL;
#define debug(a) cout<<#a<<"="<<a<<" " LL a1,b1,a2,b2,k,l,x,lcm,gcd,L,R,ans; void Exgcd(LL a,LL b,LL &x,LL &y){
if(!b){ x=1,y=0;return; }
Exgcd(b,a%b,x,y);
LL t=x;x=y,y=t-(a/b)*y;
}
int main(){
ios::sync_with_stdio(false);
cin>>a1>>b1>>a2>>b2>>L>>R;
Exgcd(a1,a2,k,l);
gcd=__gcd(a1,a2),lcm=a1/gcd*a2;
L=max(L,max(b1,b2));
if((b2-b1)%gcd || L>R) return puts("0"),0;
k*=(b2-b1)/gcd,k=(k%(a2/gcd)+a2/gcd)%(a2/gcd);
x=a1*k+b1;
// debug(x),debug(lcm),debug(L),debug(R);
if(R>=x) ans+=(R-x)/lcm+1;
if(L-1>=x) ans-=(L-1-x)/lcm+1;
cout<<ans<<endl;
return 0;
}
Codeforces 710 D. Two Arithmetic Progressions的更多相关文章
- Educational Codeforces Round 16 D. Two Arithmetic Progressions (不互质中国剩余定理)
Two Arithmetic Progressions 题目链接: http://codeforces.com/contest/710/problem/D Description You are gi ...
- [Educational Codeforces Round 16]D. Two Arithmetic Progressions
[Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...
- E - Two Arithmetic Progressions(CodeForces - 710D)(拓展中国剩余定理)
You are given two arithmetic progressions: a1k + b1 and a2l + b2. Find the number of integers x such ...
- Dirichlet's Theorem on Arithmetic Progressions 分类: POJ 2015-06-12 21:07 7人阅读 评论(0) 收藏
Dirichlet's Theorem on Arithmetic Progressions Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- 洛谷P1214 [USACO1.4]等差数列 Arithmetic Progressions
P1214 [USACO1.4]等差数列 Arithmetic Progressions• o 156通过o 463提交• 题目提供者该用户不存在• 标签USACO• 难度普及+/提高 提交 讨论 题 ...
- POJ 3006 Dirichlet's Theorem on Arithmetic Progressions (素数)
Dirichlet's Theorem on Arithmetic Progressions Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- poj 3006 Dirichlet's Theorem on Arithmetic Progressions【素数问题】
题目地址:http://poj.org/problem?id=3006 刷了好多水题,来找回状态...... Dirichlet's Theorem on Arithmetic Progression ...
- (素数求解)I - Dirichlet's Theorem on Arithmetic Progressions(1.5.5)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit cid=1006#sta ...
- USACO 1.4 Arithmetic Progressions
Arithmetic Progressions An arithmetic progression is a sequence of the form a, a+b, a+2b, ..., a+nb ...
随机推荐
- Hermite Curve
http://paulbourke.net/miscellaneous/interpolation/ http://fivedots.coe.psu.ac.th/Software.coe/Java%2 ...
- $().index() 两种用法
第一种:获得第一个 p 元素的名称和值: $(this).index() <script type="text/javascript"> $(document).rea ...
- WebGrid Helper with Check All Checkboxes
WebGrid Helper with Check All Checkboxes myEvernote Link Tuesday, September 13, 2011ASP.NET ASP.NET ...
- tomcat启动异常(严重: Dispatcher initialization failed Unable to load configuration. - [unknown location] )
严重: Dispatcher initialization failed Unable to load configuration. - [unknown location] at com.opens ...
- linux socket
linux 的 C 库路径为 /usr/include,可以直接查看源码,也可以通过 "man 头文件名" 来学习,需要查看某个函数如 bind() ,则只需要 man 2 bi ...
- jquery点击label触发2次的问题
今天写问卷的时候遇到个label点击的时候,监听的click事件被执行两次:产生这个的原因么...事件冒泡 <div class="questionBox checkBox" ...
- 前端性能利器——dynatrace ajax edition
因为最近的工作跟性能分析有关系,所以写个小总结. 顺带推荐两个我常用的小工具: 1.文件对比工具beyond compare,非常好用,对比.修改很简单.当然我只是用的试用版本.google一下官网下 ...
- U盘操作系统,Kali Linux操作系统安装
为什么要用U盘装操作系统,那好处多了去了.1.随身携带,想用就用.2.平常娱乐还是用Windows比较方便,不用做双系统那么麻烦. 准备: U盘,从天猫上买了个三星闪存盘,32G,USB3.0: 从官 ...
- Web渗透测试使用Kali Linux(一)渗透测试概要及环境部署
渗透测试是利用已经发现的漏洞,采用恶意黑客的惯用手段来尝试对漏洞进行攻击. Kali Linux是BackTrack的进化版,是Linux的衍生版本,专门开发用作渗透测试,其中提供了很多的渗透测试工具 ...
- Tomcat 6 JNDI数据源详解
数据库连接池这个概念应该都不陌生,在Java中连接池也就是数据库的连接池,它是一种采用连接复用的思想避免多次连接造成资源的浪费机制. 最常见的连接池就是DBCP和C30P了,在tomcat中默认使用的 ...