生活随笔
收集整理的這篇文章主要介紹了
Quadratic equation(二次剩余)2019牛客多校第九场
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
鏈接:https://ac.nowcoder.com/acm/contest/889/B 來(lái)源:牛客網(wǎng)
題目描述 Amy asks Mr. B problem B. Please help Mr. B to solve the following problem.
Let p = 1000000007. Given two integers b and c, please find two integers (0≤x≤y<p), such that 輸入描述: The first line contains an integer t, which is the number of test cases (1 <= t <= 10).
In the following t lines, each line contains two integers b and c (0 <= b, c < p). 輸出描述: For each test case, please output x, y in one line. If there is a solution, because x <= y, the solution is unique.
If there is no solution, please output -1, -1 示例1 輸入 復(fù)制 10 4 4 5 6 10 10 10 25 20000 100000000 0 5 3 6 220 284 0 1 1000000000 1000000000 輸出 復(fù)制 2 2 2 3 -1 -1 5 5 10000 10000 474848249 525151758 352077071 647922939 448762649 551237578 -1 -1 366417496 633582504 這個(gè)題是隊(duì)友過(guò)的,用到了二次剩余定理,記錄下來(lái)。
#include <bits/stdc++.h>
typedef long long ll
;
using namespace std
;
ll
pow_mod ( ll a
, ll n
, ll p
) { ll res
= 1 ; while ( n
) { if ( n
& 1 ) res
= res
* a
% p
; n
>>= 1 ; a
= a
* a
% p
; } return res
;
}
ll
legendre ( ll a
, ll p
) { return pow_mod ( a
, ( p
- 1 ) >> 1 , p
) ;
}
ll
mod ( ll a
, ll p
) { a
% = p
; if ( a
< 0 ) a
+ = p
; return a
;
}
struct node
{ static ll p
, omega
; ll a
, b
; node ( ll a
, ll b
) : a ( a
% p
) , b ( b
% p
) { }
} ;
node
operator * ( const node
& p
, const node
& q
) { int m
= node
:: p
; return node ( p
. a
* q
. a
+ p
. b
* q
. b
% m
* node
:: omega
, p
. a
* q
. b
+ q
. a
* p
. b
) ;
}
node
pow_mod ( node a
, ll n
) { node
result ( 1 , 0 ) ; while ( n
> 0 ) { if ( ( n
& 1 ) == 1 ) { result
= result
* a
; } a
= a
* a
; n
>>= 1 ; } return result
;
}
ll node
:: p
, node
:: omega
;
ll
modsqr ( ll a
, ll p
) { if ( p
== 2 ) return 1 ; if ( legendre ( a
, p
) + 1 == p
) return - 1 ; if ( ( ( ( p
+ 1 ) >> 1 ) & 1 ) == 0 ) return pow_mod ( a
, ( p
+ 1 ) >> 2 , p
) ; ll a_0
= - 1 ; while ( true ) { a_0
= rand ( ) % p
; if ( legendre ( mod ( a_0
* a_0
- a
, p
) , p
) + 1 == p
) break ; } node
:: p
= p
; node
:: omega
= mod ( a_0
* a_0
- a
, p
) ; node ret
= pow_mod ( node ( a_0
, 1 ) , ( p
+ 1 ) >> 1 ) ; return ret
. a
;
}
const long long mood
= 1000000007 ;
int main
( )
{ ll b
, c
; ll s2
= pow_mod ( 2 , mood
- 2 , mood
) ; int T
; scanf ( "%d" , & T
) ; while ( T
-- ) { scanf ( "%lld %lld" , & b
, & c
) ; ll t
= b
* b
- 4 * c
; t
= t
% mood
; ll x_y
= modsqr ( t
, mood
) ; if ( x_y
== - 1 ) { printf ( "-1 -1\n" ) ; } else { if ( x_y
* 2 > mood
) x_y
= mood
- x_y
; ll x
= ( ( ( x_y
+ b
) % mood
) * s2
) % mood
; ll y
= ( ( b
- x
) + mood
) % mood
; if ( x
> y
) swap ( x
, y
) ; if ( ( x
+ y
) % mood
== b
&& ( x
* y
) % mood
== c
) printf ( "%lld %lld\n" , x
, y
) ; else printf ( "-1 -1\n" ) ; } } return 0 ;
}
努力加油a啊,(o )/~
總結(jié)
以上是生活随笔 為你收集整理的Quadratic equation(二次剩余)2019牛客多校第九场 的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。