1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
| #include<bits/stdc++.h> using namespace std; #define ll long long #define re register #define gc getchar #define pc putchar #define puts put_s #define cs const
namespace IO{ namespace READONLY{ cs int Rlen=1<<18|1; char buf[Rlen],*p1,*p2; char obuf[Rlen],*p3=obuf; char ch[23]; } inline char get_char(){ using namespace READONLY; return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,Rlen,stdin),p1==p2)?EOF:*p1++; } inline void put_char(cs char &c){ using namespace READONLY; *p3++=c; if(p3==obuf+Rlen)fwrite(obuf,1,Rlen,stdout),p3=obuf; } inline void put_s(cs char *s){ for(;*s;++s)pc(*s); pc('\n'); } inline void FLUSH(){ using namespace READONLY; fwrite(obuf,1,p3-obuf,stdout); p3=obuf; }
inline ll getint(){ re ll num; re char c; while(!isdigit(c=gc()));num=c^48; while(isdigit(c=gc()))num=(num+(num<<2)<<1)+(c^48); return num; } inline void outint(ll a){ using namespace READONLY; if(a==0)pc('0'); if(a<0)pc('-'),a=-a; while(a)ch[++ch[0]]=a-a/10*10,a/=10; while(ch[0])pc(ch[ch[0]--]^48); } } using namespace IO;
namespace Linear_sieves{ cs int P=300005; int prime[P],pcnt; bool mark[P];
inline void init(int len=P-5){ mark[1]=true; for(int re i=2;i<=len;++i){ if(!mark[i])prime[++pcnt]=i; for(int re j=1;j<=pcnt&&i*prime[j]<=len;++j){ mark[i*prime[j]]=true; if(i%prime[j]==0)break; } } } }
namespace Find_root{ #define complex COMPLEX using namespace Linear_sieves; inline ll mul(cs ll &a,cs ll &b,cs ll &mod){ return (a*b-(ll)((long double)a/mod*b)*mod+mod)%mod; } inline ll quickpow(ll a,ll b,cs ll &mod,ll res=1){ for(;b;b>>=1,a=mul(a,a,mod))if(b&1)res=mul(res,a,mod); return res; }
inline ll ex_gcd(cs ll &a,cs ll &b,ll &x,ll &y){ if(!b){ y=0; x=1; return a; } ll t=ex_gcd(b,a-a/b*b,y,x); y-=(a/b)*x; return t; } inline ll inv(cs ll a,cs ll mod){ ll x,y; ll t=ex_gcd(a,mod,x,y); return (x%mod+mod)%mod; }
ll W,Mod; class complex{ public: ll x,y; complex(cs ll &_x=0,cs ll &_y=0):x(_x),y(_y){} inline friend complex operator*(cs complex &a,cs complex &b){ return complex( (mul(a.x,b.x,Mod)+mul(mul(a.y,b.y,Mod),W,Mod))%Mod, (mul(a.x,b.y,Mod)+mul(a.y,b.x,Mod))%Mod); } };
complex quickpow(complex a,ll b){ complex res(1,0); for(;b;b>>=1,a=a*a)if(b&1)res=res*a; return res; }
inline bool isprime(ll x){ if(x<=P-5)return !mark[x]; if(x%2==0||x%3==0||x%5==0||x%7==0||x%31==0||x%24251==0)return false; re ll t=x-1,s; t>>=(s=__builtin_ctzll(t)); for(int re i=1;i<=5;++i){ re ll p=prime[rand()%pcnt+1]; re ll num=quickpow(p,t,x),pre=num; for(int re j=0;j<s;++j){ num=mul(num,num,x); if(num==1&&pre!=x-1&&pre!=1)return false; pre=num; if(num==1)break; } if(num^1)return false; } return true; }
inline ll Pollard_rho(ll x){ if(x%2==0)return 2; if(x%3==0)return 3; if(x%5==0)return 5; if(x%7==0)return 7; if(x%31==0)return 31; if(x%24251==0)return 24251; re ll n=0,m=0,t=1,q=1,c=rand()%(x-2)+2; for(int re k=2;;k<<=1,m=n,q=1){ for(int re i=1;i<=k;++i){ n=(mul(n,n,x)+c)%x; q=mul(q,abs(n-m),x); } if((t=__gcd(q,x))>1)return t; } }
ll fact[60],cntf; inline void sieves(ll x){ if(x==1)return ; if(isprime(x)){fact[++cntf]=x;return;} re ll p=x; while(p==x)p=Pollard_rho(p); sieves(p); while(x%p==0)x/=p; sieves(x); }
inline ll solve_2k(ll a,ll k){ if(a%(1<<k)==0)return 0; a%=(1<<k); re ll t=0,res=1; a>>=(t=__builtin_ctzll(a)); if((a&7)^1)return -1; if(t&1)return -1; k-=t; for(int re i=4;i<=k;++i){ res=(res+(a%(1<<i)-res*res)/2)%(1<<k); } res%=1<<k; if(res<0)res+=1<<k; return res<<(t>>1); }
inline ll solve_p(ll a,ll p){ a%=p; if(quickpow(a,(p-1)>>1,p)==p-1)return -1; re ll b; Mod=p; while(true){ b=rand()%p; W=(mul(b,b,p)-a+p)%p; if(quickpow(W,(p-1)>>1,p)==p-1)break; } re ll ans=quickpow(complex(b,1),(p+1)>>1).x; return min(ans,p-ans); }
inline ll solve_pk(ll a,ll k,ll p,ll mod){ if(a%mod==0)return 0; re ll t=0,hmod=1; while(a%p==0)a/=p,++t,hmod*=(t&1)?p:1; if(t&1)return -1; k-=t; mod/=hmod*hmod; re ll res=solve_p(a,p); if(res==-1)return -1; complex tmp(res,1); W=a; Mod=mod; tmp=quickpow(tmp,k); res=mul(tmp.x,inv(tmp.y,Mod),Mod); return res*hmod; }
ll remain[20],mod[20],p; inline ll CRT(){ re ll ans=0; for(int re i=1;i<=cntf;++i){ ans=(ans+mul(mul(p/mod[i],inv(p/mod[i],mod[i]),p),remain[i],p))%p; } return ans; }
inline ll solve(ll a,ll pmod){ a%=pmod; cntf=0; p=pmod; sieves(pmod); if(cntf>1)sort(fact+1,fact+cntf+1); if(cntf>1)cntf=unique(fact+1,fact+cntf+1)-fact-1; for(int re i=1;i<=cntf;++i){ re ll now=0,rmod=1; while(pmod%fact[i]==0)pmod/=fact[i],++now,rmod*=fact[i]; mod[i]=rmod; if(fact[i]==2)remain[i]=solve_2k(a,now); else remain[i]=solve_pk(a,now,fact[i],rmod); if(remain[i]==-1)return -1; } return CRT(); }
#undef complex }
int T; signed main(){ srand(time(0)); Linear_sieves::init(); T=getint(); const ll p = 1e9+7; while(T--){ re ll b=getint(),c=getint(),ans; ans=Find_root::solve(b * b - 4 * c,4*p); if(ans == -1){ b += p; ans=Find_root::solve(b * b - 4 * c,4*p); if(ans == -1){ printf("-1 -1\n"); continue; } } ll x = ans + b; x >>= 1; x %= p; ll y = (b - x + p) % p; if(x > y){ swap(x, y); } printf("%lld %lld\n", x, y); } FLUSH(); return 0; }
|