codeforces 710D Two Arithmetic Progressions (扩展中国剩余定理)

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

Input

The only line contains six integers a1, b1, a2, b2, L, R (0 < a1, a2 ≤ 2·109,  - 2·109 ≤ b1, b2, L, R ≤ 2·109, L ≤ R).

Output

Print the desired number of integers x.

Examples
input
2 0 3 3 5 21
output
3
input
2 4 3 0 6 17
output
2
题目就是求区间内同时满足两个等式的x个数。

两个等式:x = a1k' + b1,x= a2l' + b2,因为k',l'不一定是同步的,只要存在就行,其实相当于平面上两条直线,x就是给的一个函数值然后与两直线交的坐标是整数。

看成同余方程:x= b1 (mod a1) , x=b2 (mod a2),不过注意k',l'是非负的,只需要限制x的下线就可以了,因为直线斜率是大于0的,也就是说x最小取值就是max(b1,b2).

这样我们把区间左端点L修改一下,就可以满足非负的限制,剩下的就是用扩展中国剩余定理(模不一定互质)把两个方程合并成一个。

只有一个就很好做了。(比如同余看成等差什么的,然后求区间解)


#pragma comment(linker, "/STACK:10240000,10240000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cctype>
#include<queue>
#include<set>
#include<vector>
#include<map>
#include<stack>
#include<cmath>
#include<algorithm>
using namespace std;
const double R=0.5772156649015328606065120900;
const int N=50+5;
const int mod=1e9+7;
const int INF=0x3f3f3f3f;
const double eps=1e-8;
const double pi=acos(- 1.0);
typedef long long ll;
ll gcd(ll a,ll b)
{
    return b==0?a:gcd(b,a%b);
}
ll ex_gcd(ll a,ll b,ll&x,ll&y)
{
    if(b==0)
    {
        x=1,y=0;
        return a;
    }
    ll d=ex_gcd(b,a%b,y,x);
    y-=a/b*x;
    return d;
}
//a在mod下的逆
ll inverse(ll a,ll mod)
{
    ll x,y;
    if(ex_gcd(a,mod,x,y)!=1) return -1;//无解
    return (x%mod+mod)%mod;
}
//合并两个同余方程
/*
x%m1=a1;//x=a1(mod m1)
x%m2=a2;//x=a2(mod m2)
ansa ansm是合并后的方程
*/
bool Merge(ll a1,ll m1,ll a2,ll m2,ll &ansa,ll &ansm)//模板
{
    ll c=a2-a1,d=gcd(m1,m2);
    if(c%d) return false;
    c=(c%m2+m2)%m2;//care
    ll A=c/d*inverse(m1/d,m2/d);
    A%=m2/d;//care
    ll tmp_a=m1*A+a1;
    ansm=m1/d*m2;
    ansa=(tmp_a%ansm+ansm)%ansm;
    return true;
}
int main()
{
    ll a1,b1,a2,b2,le,ri;scanf("%I64d%I64d%I64d%I64d%I64d%I64d",&a1,&b1,&a2,&b2,&le,&ri);
    ll a,m;
    if(!Merge(b1,a1,b2,a2,a,m))
    {
        puts("0");
        return 0;
    }
    le=max(le,max(b1,b2));//左端点修改
    if(le>ri) puts("0");
    else
    {
        //一个方程的解法很丑,建议还是自己写
        ll c1=double(le-a)/m;//先求最小的、在区间里的、满足同余式的c1
        bool tag=false;
        //上面double算出的粗糙值,这里左右4的区域枚举找解
        for(ll i=c1-4;i<=c1+4;i++)//注意起点,不要妄想是最低是0
        {
            ll tm=i*m+a;
            if(tm>=le) {
                c1=i;
                tag=true;
                break;
            }
        }
        if(!tag){puts("0");return 0;}//care
        ll c2=double(ri-a)/m;//和上面的思想一样
        tag=false;
        for(ll i=c2+4;i>=c2-4;i--)
        {
            ll tm=i*m+a;
            if(tm<=ri) {
                c2=i;
                tag=true;
                break;
            }
        }
        if(!tag){puts("0");return 0;}
        ll ans=c2-c1+1;
        //无时无刻记得要判不存在的;
        if(ans<0) puts("0");//care
        else
        printf("%I64d\n",ans);
    }
    return 0;
}
/*

*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值