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的更多相关文章

  1. Educational Codeforces Round 16 D. Two Arithmetic Progressions (不互质中国剩余定理)

    Two Arithmetic Progressions 题目链接: http://codeforces.com/contest/710/problem/D Description You are gi ...

  2. [Educational Codeforces Round 16]D. Two Arithmetic Progressions

    [Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...

  3. E - Two Arithmetic Progressions(CodeForces - 710D)(拓展中国剩余定理)

    You are given two arithmetic progressions: a1k + b1 and a2l + b2. Find the number of integers x such ...

  4. 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 ...

  5. 洛谷P1214 [USACO1.4]等差数列 Arithmetic Progressions

    P1214 [USACO1.4]等差数列 Arithmetic Progressions• o 156通过o 463提交• 题目提供者该用户不存在• 标签USACO• 难度普及+/提高 提交 讨论 题 ...

  6. POJ 3006 Dirichlet's Theorem on Arithmetic Progressions (素数)

    Dirichlet's Theorem on Arithmetic Progressions Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  7. poj 3006 Dirichlet's Theorem on Arithmetic Progressions【素数问题】

    题目地址:http://poj.org/problem?id=3006 刷了好多水题,来找回状态...... Dirichlet's Theorem on Arithmetic Progression ...

  8. (素数求解)I - Dirichlet&#39;s Theorem on Arithmetic Progressions(1.5.5)

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit cid=1006#sta ...

  9. USACO 1.4 Arithmetic Progressions

    Arithmetic Progressions An arithmetic progression is a sequence of the form a, a+b, a+2b, ..., a+nb ...

随机推荐

  1. Java数据结构与排序算法——堆和堆排序

    //================================================= // File Name : Heap_demo //--------------------- ...

  2. Linux版MonoDevelop无法连接调试器的解决方案(Could not connet to the debugger)

    安装了Linux版本的MonoDevelop之后,在运行程序的时候会提示Could not connnet to the debugger.的错误. 原因是新版本的Gnome Terminal不再接受 ...

  3. swiper 增加同页面增加2个滚动

    js代码 <script type="text/javascript"> var mySwiper = new Swiper('.slide_a',{ noSwipin ...

  4. CO-类的本质、description方法

    类的本质 1. 类也是个对象 其实类也是一个对象,是Class类型的对象,简称“类对象” Class类型的定义 typedef struct objc_class  *Class; 类名就代表着类对象 ...

  5. cookie, localStorage, sessionStorage区别

    cookie 有过期时间,默认是关闭浏览器后失效,4K,兼容ie6,不可跨域,子域名会继承父域名的cookielocalStorage 永不过期,除非手动删除,5M,兼容IE8,不可跨域,子域名不能继 ...

  6. Redis for .NET 系列之实现分页需求

    代码笔记: var tableName = "Table1"; redisClient.AddItemToSortedSet(tableName, ); redisClient.A ...

  7. 解决子元素margin让父辈元素位置一起改变的问题

    1.在父元素内添加内容,并且要在子元素块前面添加,后面添加内容无效. 内容可以是文字.图片甚至是空格,源代码里直接按空格无效,可以用占位符  2.让子元素或父元素浮动float:left. 缺点:在元 ...

  8. Python 爬虫笔记、多线程、xml解析、基础笔记(不定时更新)

    1  Python学习网址:http://www.runoob.com/python/python-multithreading.html

  9. HighCharts选项和参数详细配置查询表

    概述:作为一款出色的交互图表制作工具,HighCharts有着全面的选项.参数等配置信息.为了帮助大家进一步掌握HighCharts,今天我们为大家整理了HighCharts的所有配置信息和说明,将其 ...

  10. c++模板

    1.从 python 说起 def add(a, b): return a + b; print add(3.1, 5.1); #8.2 print add("abc", &quo ...