Two Arithmetic Progressions

题目链接:

http://codeforces.com/contest/710/problem/D

Description


You are given two arithmetic progressions: a1k + b1 and a2l + b2. Find the number of integers x such that L ≤ x ≤ R and x = a1k' + b1 = a2l' + b2, for some integers k', l' ≥ 0.

Input


The only line contains six integers a1, b1, a2, b2, L, R (0 

Output


Print the desired number of integers x.

Sample Input


```
2 0 3 3 5 21
2 4 3 0 6 17
```

Sample Output


```
3
2
```


##题意:

求[L,R]区间内有多少个整数y满足 y = k1*x1+b1 且 y = k2*x2+b2. (x1 x2 >= 0)


##题解:

首先把两条直线画到平面上,题目限制了直线斜率都大于零. 又由于 x1 x2 >= 0.
所以y的区间可以进一步限制为 [max(L, max(b1,b2)), R];
问题就变为在这个新区间里找使得两个式子相等的"整点"个数了.
这里可以把两个式子通过拓展中国剩余定理(因为不互质)合并成一个式子, 然后计算区间内的解的个数即可.
注意:可能两式子不能合并,直接输出0; 正确计算区间内的解的个数(见注释).

比赛做的时候只想到了拓展中国剩余定理这里,然后想的是b不一定小于k所以不算是模方程,以为不能做.
实际上(拓展)中国剩余定理在处理同余模方程组的时候不要求余数小于模数.


##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 10000100
#define mod 100000007
#define inf 0x3f3f3f3f3f3f3f3f
#define mid(a,b) ((a+b)>>1)
#define IN freopen("in.txt","r",stdin);
using namespace std;

LL x,y,gcd;

void ex_gcd(LL a,LL b)

{

if(!b) {x=1;y=0;gcd=a;}

else {ex_gcd(b,a%b);LL temp=x;x=y;y=temp-a/b*y;}

}

LL n,m[2],a[2]; //x%m=a

LL cur, T;

/模线性方程组--不互质中国剩余定理/

int ex_China() {

LL m1,m2,n1,n2,x0;

m1=m[0];n1=a[0];

for(int i=1; i<n; i++)
{
m2=m[i];
n2=a[i];
ex_gcd(m1,m2);
if((n2-n1)%gcd) return -1;
LL tmp=m2/gcd;
x0=(x*((n2-n1)/gcd)%tmp+tmp)%tmp; n1=n1+x0*m1;
m1=m1/gcd*m2;
}
n1=(n1+m1)%m1; cur = n1; T = m1;
return T;

}

int main(int argc, char const *argv[])

{

//IN;

n = 2;
cin >> m[0] >> a[0] >> m[1] >> a[1];
LL L,R; cin >> L >> R;
L = max(max(a[0], a[1]), L); int ret = ex_China(); LL ans = 0;
if(ret == -1) { /*特判不能合并的方程组*/
printf("0\n");
return 0;
} if(cur >= L) { /*找一个合适的起点,分别计算L-1和R到这个点之间有多少个解*/
cur -= ((cur-L)/T + 1) * T;
} if(L <= R) {
ans = (R - cur) / T - (L - 1 - cur) / T;
} printf("%I64d\n", ans); return 0;

}

Educational Codeforces Round 16 D. Two Arithmetic Progressions (不互质中国剩余定理)的更多相关文章

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

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

  2. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

  3. [Educational Codeforces Round 16]C. Magic Odd Square

    [Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...

  4. [Educational Codeforces Round 16]B. Optimal Point on a Line

    [Educational Codeforces Round 16]B. Optimal Point on a Line 试题描述 You are given n points on a line wi ...

  5. [Educational Codeforces Round 16]A. King Moves

    [Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...

  6. Educational Codeforces Round 16 E. Generate a String dp

    题目链接: http://codeforces.com/problemset/problem/710/E E. Generate a String time limit per test 2 seco ...

  7. Educational Codeforces Round 16 E. Generate a String (DP)

    Generate a String 题目链接: http://codeforces.com/contest/710/problem/E Description zscoder wants to gen ...

  8. Educational Codeforces Round 16

    A. King Moves water.= =. #include <cstdio> ,,,,,-,-,-}; ,-,,,-,,,-,}; #define judge(x,y) x > ...

  9. Educational Codeforces Round 16 A B C E

    做题太久也有点累了..难题不愿做 水题不愿敲..床上一躺一下午..离下一场div2还有点时间 正好有edu的不计分场 就做了一下玩玩了 D是个数学题 F是个AC自动机 都没看明白 留待以后补 A 给出 ...

随机推荐

  1. Java——HashMap底层源码分析

    1.简介 HashMap 根据键的 hashCode 值存储数据,大多数情况下可以直接定位到它的值,因而具有很快的访问速度,但遍历顺序却是不确定的. HashMap 最多只允许一条记录的key为 nu ...

  2. 剑指Offer编程题(Java实现)——从尾到头打印链表

    题目描述 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList. 解题思路 思路一:使用头插法 使用头插法可以得到一个逆序的链表.遍历链表,每次将所遍历节点插入到链表的头部. 头结点和第一个 ...

  3. [Luogu 5465] [LOJ 6435] [PKUSC2018]星际穿越(倍增)

    [Luogu 5465] [LOJ 6435] [PKUSC2018]星际穿越(倍增) 题面 n个点的图,点i和[l[i],i)的所有点连双向边.每次询问(l,r,x)表示x到[l,r]的所有点的最短 ...

  4. kernel编译

    Linux内核编译与安装 Linux内核介绍 Linux内核是一个用C语言写成的,符合POSIX标准的类Unix操作系统.内核是操作系统中最基本的一部分,提供了众多应用程序访问计算机硬件的机制.Lin ...

  5. Django 调试models 输出的SQL语句 定位查看结果

    django 调试models变得更为简单了,不用像之前的版本, 手工去调用django query, 才能打印出之前的代码是执行的什么SQL语句. 1.3开始只需在settings.py里,配置如下 ...

  6. 1. Docker快速入门(仓库,镜像,容器)

    参考阿里云文档:https://help.aliyun.com/document_detail/51853.html?spm=a2c4g.11186623.6.820.RaToNY 参考菜鸟教程文档: ...

  7. 剑指offer-二叉搜索树的后序遍历序列-python

    题目描述 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 递归法: 先判断左子树是否存在 再判断右子树是否存 ...

  8. Animator通过按键切换动画不及时,动画延时切换问题

    再unity3D版本为Unity 5.2.1f1 (64-bit),再设置动画切换时有一个Has Exit Time属性,由于勾上了这个的原因

  9. oracle数据库中的存储函数

    oracle中的存储函数,和系统内的函数类似,可以像调用系统函数一样调用存储函数.它与存储过程的唯一区别就是存储过程没有return返回值,存储函数可以与存储过程互换,存储函数可以在存储过程中调用. ...

  10. SSM框架的常用注解整理

    一.mybatis 1 配置一对多查询和多对多查询的注解方式映射关系: @Results:声明映射关系的配置  Value属性接收 @Result的数组 @Result:配置映射关系   id属性(b ...