码迷,mamicode.com
首页 > 其他好文 > 详细

[模板]二次剩余(无讲解)

时间:2019-10-20 19:36:52      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:with   std   event   nbsp   color   ola   show   syn   图片   

技术图片
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long int ll;
 4 typedef unsigned long long int ull;
 5 ull SEED=1;
 6 ll n,mod;
 7 ll w;
 8 struct com
 9 {
10     ll x,y;
11     com operator*(const com&A)const
12     {
13         com B;
14         B.x=(x*A.x+y*A.y%mod*w)%mod;
15         B.y=(x*A.y+y*A.x)%mod;
16         return B;
17     }
18 };
19 inline ull R()
20 {
21     SEED^=SEED<<13;
22     SEED^=SEED>>5;
23     SEED^=SEED<<3;
24     return SEED;
25 }
26 inline ll qpow(ll x,ll y)
27 {
28     ll ans=1,base=x;
29     while(y)
30     {
31         if(y&1)
32             ans=ans*base%mod;
33         base=base*base%mod;
34         y>>=1;
35     }
36     return ans;
37 }
38 inline com qpow(com x,ll y)
39 {
40     com ans=x,base=x;
41     --y;
42     while(y)
43     {
44         if(y&1)
45             ans=ans*base;
46         base=base*base;
47         y>>=1;
48     }
49     return ans;
50 }
51 inline void solve()
52 {
53     cin>>n>>mod;
54     if(qpow(n,(mod-1)/2)==mod-1)
55     {
56         cout<<"Hola!"<<endl;
57         return;
58     }
59     ll a;
60     while(true)
61     {
62         a=R()%mod;
63         w=(a*a%mod-n+mod)%mod;
64         if(qpow(w,(mod-1)/2)==mod-1)
65             break;
66     }
67     com A;
68     A.x=a,A.y=1;
69     A=qpow(A,(mod+1)/2);
70     ll ans1=A.x;
71     ll ans2=(mod-ans1+mod)%mod;
72     if(ans1>ans2)
73         swap(ans1,ans2);
74     if(ans1==ans2)
75         cout<<ans1<<endl;
76     else
77         cout<<ans1<<" "<<ans2<<endl;
78 }
79 int main()
80 {
81     srand(time(0));
82     ios::sync_with_stdio(false);
83     int T;
84     cin>>T;
85     while(T--)
86         solve();
87     return 0;
88 }
View Code

 

[模板]二次剩余(无讲解)

标签:with   std   event   nbsp   color   ola   show   syn   图片   

原文地址:https://www.cnblogs.com/GreenDuck/p/11708520.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!