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 ...
随机推荐
- 20145212 《Java程序设计》第6周学习总结
20145212 <Java程序设计>第6周学习总结 学习内容总结 第十章 1.Java将输入/输出抽象化为串流.读取数据对象成为输入流,能向其写入的对象叫输出流. 我从网上找到了一个可以 ...
- Java适配器设计模式
适配器设计模式,一个接口首先被一个抽象类先实现(此抽象类通常称为适配器类),并在此抽象类中实现若干方法(但是这个抽象类中的方法体是空的),则以后的子类直接继承此抽象类,就可以有选择地覆写所需要的方法. ...
- Linux下Memcache 安装和使用
Memcached是一种高性能的分布式内存对象缓存系统(memcached虽然称为“分布式”缓存服务器,但服务器端并没有“分布式”功能,其“分布式”由客户端函数库完成,成熟算法的为一致性Hash),用 ...
- 第1章 JavaScript简介
概述:Javascript是一种依赖于网页浏览器的脚本语言.是一种脚本语言.由Netscape和Sun共同开发.与Java没有什么关系.作为一种语言标准,而被称为ECMACcript.一个JS的实现包 ...
- C++ wMA 算法
CMakeLists.txt project(aaa) add_library(aaa SHARED aaa.cpp) add_executable(bbb bbb.cpp) target_link_ ...
- HTTP2.0介绍
HTTP2.0的所有通信都是在一个TCP连接上完成的. 1.信息传输术语 1.1 流 虚拟信道,可以承载双向消息,每个流都有一个唯一的整数标识符(1.2....N); 1.2 消息 逻辑上的HTTP消 ...
- Visual Studio低版本升级到Visual Studio 2012出现Warning LNK4075
Warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/SAFESEH' specification
- [FMX] Android APP 启动黑屏优化补丁
使用说明 *************************************************** Android APP 启动黑屏优化补丁 作者: Swish, YangYxd 201 ...
- 自定义列表dl的使用原因和场合
为什么要使用自定义列表? dl和ol, ul的区别? 要正确理解dl的意图, 理解 dl的 "语义" ! 才能知道为什么要使用dl, 以及在什么时候/ 什么情况下使用 dl? dl ...
- recording just for inquiry in the future
auditd审计 相关命令有: auditd, auditctl, ausearch, aureport 相关文件: /etc/audit/auditd.conf, /etc/audit/audit. ...