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


     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
  • 相关阅读:
    [WinJS] Promise 用法
    Python 统计代码行
    mac下Apache + MySql + PHP网站开发
    android中,获取网速的方法实现
    如何屏蔽掉两个activity切换时的动画效果
    dp与px的相互转化
    毫秒的格式化
    关于android中事件传递和分发的一些小理解
    汉字转拼音
    关于实现无限循环的做法
  • 原文地址:https://www.cnblogs.com/GreenDuck/p/11708520.html
Copyright © 2020-2023  润新知