Input
Output
Sample Input
3
5 3 4 15 3 1
10 2 2 7 3 3
100 1 1 100 1 2
Sample Output
4
3
50
超时代码,因为K很大
/*****************
f1+(k1-1)*d1=f2+(k2-1)*d2
=> (k1-1)*d1+(k2-1)*(-d2)=f2-f1;
=> d1*x+y*(-d2)=f2-f1 d1=>[0,n1-1] d2=>[0,n2-1]
求在给定范围内解的个数
*********************/
#include"iostream"
#include"cstdio"
#include"cstring"
#include"cmath"
#include"algorithm"
using namespace std;
typedef long long ll;
void exgcd(ll n,ll m,ll &x,ll &y,ll &d)
{
if(!m){
x=;y=;d=n;
}
else{
exgcd(m,n%m,y,x,d);
y-=(n/m)*x;
}
}
int main()
{
ll f1,n1,d1,f2,n2,d2,i,j,t;
cin>>t;
while(t--)
{
ll x,y,tmp,d;
//cin>>n1>>f1>>d1>>n2>>f2>>d2;
scanf("%lld%lld%lld%lld%lld%lld",&n1,&f1,&d1,&n2,&f2,&d2);
exgcd(d1,-d2,x,y,d);
f2-=f1;
if(f2%d){
//cout<<"0"<<endl;
puts("");
continue;
}
x=x*(f2/d);
y=y*(f2/d);
//接下来 逼近[0,n1-1],[0,n2-1]
ll t1=d2/(abs(d));
ll t2=d1/(abs(d));
if(x<||y<)
{
int i=;
while()
{
if(x+t1*i>=&&y+t2*i>=)
break;
i++;
}
x=x+t1*i;
y=y+t2*i;
}
else
{
int i=;
while()
{
if(x-t1*i<||y-t2*i<)
break;
i++;
}
x=x-t1*(i-);
y=y-t2*(i-);
}
if(x>(n1-)||y>(n2-))
{
//cout<<"0"<<endl;
puts("");
continue;
}
int ans=;
while()
{
if(x+t1*ans>(n1-)||y+t2*ans>(n2-))
break;
ans++;
}
cout<<ans<<endl;
//printf("%lld\n",ans);
}
return ;
}
AC代码
/*****************
f1+(k1-1)*d1=f2+(k2-1)*d2
=> (k1-1)*d1+(k2-1)*(-d2)=f2-f1;
=> d1*x+y*(-d2)=f2-f1 d1=>[0,n1-1] d2=>[0,n2-1]
求在给定范围内解的个数
*********************/
#include"iostream"
#include"cstdio"
#include"cstring"
#include"cmath"
#include"algorithm"
using namespace std;
typedef long long ll;
void exgcd(ll n,ll m,ll &x,ll &y,ll &d)
{
if(!m){
x=;y=;d=n;
}
else{
exgcd(m,n%m,y,x,d);
y-=(n/m)*x;
}
}
int main()
{
ll f1,n1,d1,f2,n2,d2,i,j,t;
cin>>t;
while(t--)
{
ll x,y,tmp,d;
//cin>>n1>>f1>>d1>>n2>>f2>>d2;
scanf("%lld%lld%lld%lld%lld%lld",&n1,&f1,&d1,&n2,&f2,&d2);
exgcd(d1,-d2,x,y,d);
f2-=f1;
if(f2%d){
//cout<<"0"<<endl;
puts("");
continue;
}
x=x*(f2/d);
y=y*(f2/d);
//接下来 逼近[0,n1-1],[0,n2-1]
ll t1=d2/(abs(d));
ll t2=d1/(abs(d));
if(x<||y<)
{
int i=;
while()
{
if(x+t1*i>=&&y+t2*i>=)
break;
i++;
}
x=x+t1*i;
y=y+t2*i;
}
else
{
int i=;
while()
{
if(x-t1*i<||y-t2*i<)
break;
i++;
}
x=x-t1*(i-);
y=y-t2*(i-);
}
if(x>(n1-)||y>(n2-))
{
//cout<<"0"<<endl;
puts("");
continue;
}
int ans=;
/* while(1)
{
if(x+t1*ans>(n1-1)||y+t2*ans>(n2-1))
break;
ans++;
}
*/
// 这样避免超时
ll a,b;
a=(n1--x)/t1;
b=(n2--y)/t2;
cout<<min(a,b)+<<endl;
}
return ;
}