题意略:

题解:二次剩余板子题

//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
//#include <bits/extc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define mt make_tuple
//#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define MOD 1000000009
#define ld long double
//#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define sqr(x) ((x)*(x))
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define ull unsigned long long
#define bpc __builtin_popcount
#define base 1000000000000000000ll
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
#define mr mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
//inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
//inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;} inline ll mul(ll a,ll b,ll c){return (a*b-(ll)((ld)a*b/c)*c+c)%c;}
//inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=mul(ans,a,c);a=mul(a,a,c),b>>=1;}return ans;} using namespace std;
//using namespace __gnu_pbds; const ld pi=acos(-1);
const ull ba=233;
const db eps=1e-5;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=100000+10,maxn=2000000+10,inf=0x3f3f3f3f; ll mod;
struct T
{
ll p, d;
};
ll w;
//O1乘法取模黑科技
ll mul(ll x,ll y)
{
return (x * y-(ll)(x /(long double)mod * y + 1e-3) * mod + mod) % mod;
}
//二次域乘法
T multi_er(T a, T b)
{
T ans;
ans.p = (mul(a.p, b.p) + mul(mul(a.d,b.d), w)) % mod;
ans.d = (mul(a.p, b.d) + mul(a.d, b.p)) % mod;
return ans;
}
ll quick_mod(ll a, ll b)
{
ll ans = 1;
a %= mod;
while(b)
{
if(b & 1)
{
ans = mul(ans , a);
b--;
}
b >>= 1;
a = mul(a , a);
}
return ans;
} //二次域上快速幂
T power(T a, ll b)
{
T ans;
ans.p = 1;
ans.d = 0;
while(b)
{
if(b & 1)
{
ans = multi_er(ans, a);
b--;
}
b >>= 1;
a = multi_er(a, a);
}
return ans;
}
//求勒让德符号
ll Legendre(ll a, ll p)
{
return quick_mod(a, (p-1)>>1);
}
ll QuadraticResidue()
{
ll rem = (-3 % mod + mod) % mod;
if(rem == 0)//特判mod==3
return 0;
if(mod == 2)//特判非奇素数
return 1;
if(Legendre(rem, mod) + 1 == mod)//欧拉判别条件 非剩余
return -1;
ll b;
while(1)//找一个非剩余求二次域上的单位w=sqrt(b^2 - rem)
{
b = rand() % mod;
w = (mul(b, b) - rem + mod) % mod;
if(quick_mod(w, (mod - 1)/2) + 1 == mod)//cipolla
break;
}
T tmp;
tmp.p = b;
tmp.d = 1;
T ans = power(tmp, (mod + 1) / 2);
return ans.p;
} ll a[N];
map<ll,int>ma;
int main()
{
int T;scanf("%d",&T);
while(T--)
{
ll cc = 0;
ma.clear();
int n;ll p;scanf("%d%lld",&n,&p);
mod = p;
ll te=QuadraticResidue();
for(int i=1;i<=n;i++) {
scanf("%lld",&a[i]);
if(a[i]) ma[a[i]]++, cc++;
}
if(p == 2) {
printf("%lld\n", cc * (cc - 1) / 2);
continue;
}
if(te==-1){puts("0");continue;}
ll ans=0;
for(int i=1;i<=n;i++)if(a[i])
{
ll x=mul(a[i],(-te-1+p),p);x=mul(x,qp(2,p-2,p),p);
ll y=mul(a[i],(te-1+p)%p,p);y=mul(y,qp(2,p-2,p),p);
if(x==y)
{
if(a[i]!=x)ans+=ma[x];
else ans+=ma[x]-1;
}
else
{
if(a[i]==x)ans+=ma[x]-1;
else ans+=ma[x];
if(a[i]==y)ans+=ma[y]-1;
else ans+=ma[y];
}
}
printf("%lld\n",ans/2);
}
return 0;
}
/******************** ********************/

HDU - 6128的更多相关文章

  1. HDU 6128 Inverse of sum(同余)

    http://acm.hdu.edu.cn/showproblem.php?pid=6128 题意:有一个a数列,并且每个数都小于p,现在要求有多少对$(i,j)$满足$\frac{1}{a_i+a_ ...

  2. 2017多校第7场 HDU 6128 Inverse of sum 推公式或者二次剩余

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6128 题意:给你n个数,问你有多少对i,j,满足i<j,并且1/(ai+aj)=1/ai+1/a ...

  3. 2017ACM暑期多校联合训练 - Team 7 1009 HDU 6128 Inverse of sum (数学计算)

    题目链接 Problem Description There are n nonnegative integers a1-n which are less than p. HazelFan wants ...

  4. 数学--数论--HDU 6128 Inverse of sum (公式推导论)

    Description 给nn个小于pp的非负整数a1,-,na1,-,n,问有多少对(i,j)(1≤i<j≤n)(i,j)(1≤i<j≤n)模pp在意义下满足1ai+aj≡1ai+1aj ...

  5. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  7. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  8. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  9. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

随机推荐

  1. 基于Netty的RPC架构学习笔记(六):netty5案例学习

    文章目录 netty5服务端入门案例 netty5客户端入门案例 单客户端多连接程序 知识普及 线程池原理图 对象池原理图 对象组原理图 结论 理论结合实际 开干开干 总结 netty5服务端入门案例 ...

  2. 使用Beyond Compare作为Perforce默认的文件比较工具

    使用perforce自带的文件比较工具有时候会遇到乱码的情况,如下: 暂时不知道如何解决上述问题,因此想换个文件比对工具,比如Beyond Compare. 设定位置:Edit->prefere ...

  3. 刚装完Linux就CPU占用率高

    top命令发现如下三个进程占据了前三的CPU使用率 wpa_supplicant NetworkManager rsyslogd google发现前两个进程与无线网络有关,我的电脑是笔记本,插的有线, ...

  4. 使用Pyppeteer进行gmail模拟登录

    import asyncio import time from pyppeteer import launch async def gmailLogin(username, password, url ...

  5. .NET Core 3.0之深入源码理解Startup的注册及运行

    原文:.NET Core 3.0之深入源码理解Startup的注册及运行   写在前面 开发.NET Core应用,直接映入眼帘的就是Startup类和Program类,它们是.NET Core应用程 ...

  6. 无LoadLibrary获取指定模块基址

    实际上,这块可以写成汇编,然后做远程注入用 方法 1.通过fs:[30h]获取当前进程的_PEB结构 2.通过_PEB的Ldr成员获取_PEB_LDR_DATA结构 3.通过_PEB_LDR_DATA ...

  7. java 数组中的数值反转输出

    package com.test; /** *数组元素反转 * */ public class ArraySwap { public static void main(String[] args) { ...

  8. DataList做一个相册,并可以上传图片

    1.前台代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DataLis ...

  9. js实现获取选中checkbox的值

    var bookVersionId = []; var examVirtualSetResultId; $.each($('input[type=checkbox]:checked'),functio ...

  10. 推荐一个Java设计模式写的很好的博客

    博客地址:https://quanke.gitbooks.io/design-pattern-java/%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E8%AE%BE%E8 ...