来源:互联网 发布:哪个软件可以听课 编辑:程序博客网 时间:2024/04/27 17:31

这里写图片描述
前30暴力枚举,特判无解情况。
再20 L==R,变为解同余方程

#include <cstdio>#include <iostream>#define LL long longusing namespace std;long long x,y;long long gcd(long long a,long long b){    if(!b) return a;    return gcd(b,a%b);}LL e_gcd(LL a,LL b,LL &x,LL &y)  {      if(b==0)      {          x=1;          y=0;          return a;      }      LL ans=e_gcd(b,a%b,x,y);      LL temp=x;      x=y;      y=temp-a/b*y;      return ans;  }  LL cal(LL a,LL b,LL c)  {      LL x,y;      LL gcd=e_gcd(a,b,x,y);printf("%d ",x);      if(c%gcd!=0) return -1;      x*=c/gcd;      b/=gcd;      if(b<0) b=-b;      LL ans=x%b;      if(ans<=0) ans+=b;      return ans;  }  int main(){    long long t;    scanf("%lld",&t);     for(int i=1;i<=t;i++)    {        long long l,r,s,m;        scanf("%lld%lld%lld%lld",&m,&s,&l,&r);        /*if(l>r||s%m==0||l>=m)         {            printf("-1\n");            continue;        }*/        if(l==r)        {            printf("%lld\n",cal(s,m,r));            continue;        }        for(long long j=1;;j++)        {            if((j*s)%m>=l&&(j*s)%m<=r)             {                printf("%lld\n",j);                break;             }        }    }} 
原创粉丝点击