cf_ducational Codeforces Round 16_D(gcd)

时间:2023-03-08 16:22:18

题意:求R-L区间满足x=a1*k+b1=a2*l+b2的x的个数;

思路:求出最小的满足条件的x0,则ans=(L-x)/(a1/gcd(a1, a2)*a2)+1;

注意剪枝,不然会超时;

代码:

 #include <bits/stdc++.h>
#define ll long long
#define MAXN 100000+10
using namespace std; ll get_gcd(ll a, ll b)
{
return b ? get_gcd(b, a%b) : a;
} int main(void)
{
ll a1, b1, a2, b2, R, L;
cin >> a1 >> b1 >> a2 >> b2 >> R >> L;
ll k=ceil((R-b1)*1.0/a1), l=ceil((R-b2)*1.0/a2);
k=max(0ll, k), l=max(0ll, l);
ll x1=a1*k+b1, x2=a2*l+b2;
// cout << x1 << " " << x2 << endl;//*******
if((b2-b1)%get_gcd(a1, a2)!=)
{
cout << "" << endl;
return ;
}
if(a1==a2&&abs(x1-x2)<a1&&x1!=x2)
{
cout << "" << endl;
return ;
}
// cout << x1 << " " << L << endl;//****
while(x1<=L && x2<=L && x1!=x2)
{
if(x1<x2)
x1+=max(1ll, (x2-x1)/a1)*a1;
else if(x1>x2)
x2+=max(1ll, (x1-x2)/a2)*a2;
}
// cout << x1 << "**" << x2 << endl;//****
if(x1==x2&&x1>=R&&x1<=L)
cout << (L-x1)/(a1/get_gcd(a1, a2)*a2)+ << endl;
else cout << "" << endl;
return ;
}

据说这题应该用拓展欧里几德解。。可惜我看了好久也没弄懂。。诶。。继续看吧。。。。