【数论】【扩展欧几里得】Codeforces 710D Two Arithmetic Progressions

题目链接:

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

题目大意:

  两个等差数列a1x+b1和a2x+b2,求L到R区间内重叠的点有几个。

  0 < a1, a2 ≤ 2·109,  - 2·109 ≤ b1, b2, L, R ≤ 2·109, L ≤ R).

题目思路:

  【数论】【扩展欧几里得】

  据题意可得同余方程组  x=b1(mod a1)  即  x=k1*a1+b1

              x=b2(mod a2)     x=k2*a2+b2

  化简,k1*a1=k2*a2+(b2-b1) 即 a1= (b2-b1)(mod a2)

  于是只要求一个同余方程即可。令a=a1,b=a2,c=b2-b1。

  扩展欧几里得求解x,再把x改为在L~R区间内的第一个通解,计算数量即可(每次增加lcm(a1,a2)答案+1)。




//
//by coolxxx
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<map>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-8)
#define J 10
#define mod 1000000007
#define MAX 0x7f7f7f7f
#define PI 3.14159265358979323
#define N 20000004
using namespace std;
typedef long long LL;
int cas,cass;
int n,m,lll,ans;
LL a1,a2,b1,b2,l,r;
LL exgcd(LL a,LL b,LL &x,LL &y)
{
	if(!b){x=1,y=0;return a;}
	LL d=exgcd(b,a%b,y,x);
	y-=a/b*x;
	return d;
}
int main()
{
	#ifndef ONLINE_JUDGE
//	freopen("1.txt","r",stdin);
//	freopen("2.txt","w",stdout);
	#endif
	int i,j,k;
	LL a,b,c,d,x,y,ny,lcm;
//	for(scanf("%d",&cas);cas;cas--)
//	for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
//	while(~scanf("%s",s+1))
	while(~scanf("%I64d",&a1))
	{
		cin>>b1>>a2>>b2>>l>>r;
		l=max(l,b1);l=max(l,b2);
		d=exgcd(a1,a2,x,y);
		lcm=a1/d*a2;
		if((b2-b1)%d!=0)
		{
			puts("0");
			continue;
		}
		a=a1/d;b=a2/d;c=(b2-b1)/d;
		d=exgcd(a,b,x,y);
		x=a1*(x*c)+b1;
		if(x>l)x=x-(x-l)/lcm*lcm;
		else x=x+(l-1-x+lcm)/lcm*lcm;
		if(x>r)puts("0");
		else printf("%I64d\n",(r-x+lcm)/lcm-(l-1-x+lcm)/lcm);
	}
	return 0;
}
/*
//

//
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值