More than 2019 cattle off summer school camp (ninth field) B Quadratic equation (Euler's criterion + solution of quadratic residue)

Topic links: https://ac.nowcoder.com/acm/contest/889/B

Subject to the effect:

  Given b, c, lets you find x, y, x and y satisfy (x + y)% p = c and (x * y)% p = c.

Report problem solving:

  The subject two formulas, can be turned into $ (xy) ^ {2} = (x + y) ^ {2} -4xy $, only needs to calculate $ (xy) ^ {2} = b ^ {2} - 4c (mod p) $ residual quadratic

  The remaining secondary determines whether n mod p, the criteria may be Euler. Euler's criterion of judgment stamp here .

  Then just to satisfy the first point p discover Tonelli_shanks algorithm, so you can directly set the first point, the last is the solution of the equation. The remaining solution-based secondary stamp here .

AC Code:

 1 #include<bits/stdc++.h>
 2 #define numm ch-48
 3 #define pd putchar(' ')
 4 #define pn putchar('\n')
 5 using namespace std;
 6 template <typename T>
 7 void read(T &res) {
 8     bool flag=false;char ch;
 9     while(!isdigit(ch=getchar())) (ch=='-')&&(flag=true);
10     for(res=numm;isdigit(ch=getchar());res=(res<<1)+(res<<3)+numm);
11     flag&&(res=-res);
12 }
13 template <typename T>
14 void write(T x) {
15     if(x<0) putchar('-'),x=-x;
16     if(x>9) write(x/10);
17     putchar(x%10+'0');
18 }
19 typedef long long ll;
20 const int mod=1e9+7;
21 const int inv2=500000004;
22 ll ksm(ll a,ll b) {
23     ll ans=1;
24     while(b) {
25         if(b&1) ans=ans*a%mod;
26         b>>=1;
27         a=a*a%mod;
28     }
29     return ans;
30 }
31 int main()
32 {
33     int _;
34     read(_);
35     while(_-- ) {
 36          LL B, C;
 37 [          Read (B), Read (C);
 38 is          LL DR = ((B * B- . 4 * C) + MOD% MOD) MOD%;   /// the right equation 
39          iF (KSM (dr, (mod- . 1 ) / 2 ) == mod- . 1 ) {   /// Euler's criterion is determined whether a quadratic residue dr 
40              Write (- . 1 ), PD, Write (- . 1 ); PN;
 41 is              Continue ;
 42 is          }
 43 is          LL = R & lt KSM (DR, (+ MOD . 1 ) / . 4 );      /// solution 
44         ll f1=((b-R)%mod+mod)%mod,f2=((b+R)%mod+mod)%mod;
45         f1=f1*inv2%mod,f2=f2*inv2%mod;
46         write(min(f1,f2)),pd,write(max(f1,f2));pn;
47     }
48     return 0;
49 }
Code here!

 

Guess you like

Origin www.cnblogs.com/wuliking/p/11366852.html