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 ...
随机推荐
- smith waterman算法
http://www.360doc.com/content/14/0106/00/14641369_342933143.shtml
- Markdown 简明语法手册
Markdown 简明语法手册 本文原文http://www.jianshu.com/p/fdb5cbdaf244 根据个人使用情况有所修改. Markdown是一种轻量级标记语言,简称md.创始人为 ...
- time模块
In [1]: import time In [2]: import datetime In [3]: date_time = datetime.datetime.now() In [4]: prin ...
- ecshop Admin后台商品列表处(上架、下架、精品...取消精品)增加操作
相关文件:goods.php,goods_list.htm 思路: a.增添连接“转移仓库” b.在goods.php,读取仓库列表数据,并且实例化 c. 在goods_list.htm循环数据.点击 ...
- Xcode文档安装
找到所需文档的下载地址,搜索.dmg 安装位置
- 要引用这几个才有GetOwinContext与GetAutofacLifetimeScope
using Owin; using Autofac; using Autofac.Integration.Owin; using System.Web; var owin = this.Request ...
- Linux的io机制
Linux的io机制 Buffered-IO 和Direct-IO Linux磁盘I/O分为Buffered IO和Direct IO,这两者有何区别呢? 对于Buffered IO: 当应用程序尝试 ...
- java 的 AccessController.doPrivileged使用
AccessController.doPrivileged意思是这个是特别的,不用做权限检查. 在什么地方会用到呢:加入1.jar中有类可以读取一个文件,现在我们要使用1.jar去做这个事情.但是我们 ...
- ubuntu系统无法访问无法磁盘最佳解决办法
出现如下错误: Error mounting /dev/sda8 at /media/fzh/System: Command-line `mount -t "ntfs" -o &q ...
- PlayMaker 学习笔记
1.v1.7.8.3版本无法使用全局变量,原因是Assets\Plugins\PlayMaker下的Resources文件夹变成中文了,无法创建全局变量文件,手动创建一个Resources文件夹即可