日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Quadratic equation(二次剩余)2019牛客多校第九场

發布時間:2023/12/15 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Quadratic equation(二次剩余)2019牛客多校第九场 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

鏈接:https://ac.nowcoder.com/acm/contest/889/B
來源:牛客網

題目描述
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
輸入
復制
10
4 4
5 6
10 10
10 25
20000 100000000
0 5
3 6
220 284
0 1
1000000000 1000000000
輸出
復制
2 2
2 3
-1 -1
5 5
10000 10000
474848249 525151758
352077071 647922939
448762649 551237578
-1 -1
366417496 633582504
這個題是隊友過的,用到了二次剩余定理,記錄下來。

#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);//assert(ret.b == 0);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);elseprintf("-1 -1\n");}}return 0; }

努力加油a啊,(o)/~

總結

以上是生活随笔為你收集整理的Quadratic equation(二次剩余)2019牛客多校第九场的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。