HDU - 6128
题意略:
题解:二次剩余板子题
//#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的更多相关文章
- HDU 6128 Inverse of sum(同余)
http://acm.hdu.edu.cn/showproblem.php?pid=6128 题意:有一个a数列,并且每个数都小于p,现在要求有多少对$(i,j)$满足$\frac{1}{a_i+a_ ...
- 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 ...
- 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 ...
- 数学--数论--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 ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
随机推荐
- sizeof,真正终结版GCC与VC
在VC6.0中sizeof结果是16.我电脑上装了个linux虚拟机,在虚拟机上GCC中结果是12, 恩不同编译器默认对齐数值不一样. VC 默认为 8 gcc 默认为 4 有个编译参数控制对齐. # ...
- class8_Canvas 画布
最终的部分运行效果图(程序见序号4): #!/usr/bin/env python# -*- coding:utf-8 -*-# ----------------------------------- ...
- 简介Python正则表达式
一.概念 简单来说正则表达式是由一些普通字符(例如,a 到 z 之间的字母)和一些元字符组成,用来匹配和过滤一些字符串的一种逻辑公式. 二.正则表达式的一些基本规则 1.一些常用的元字符 ^ : ...
- 【集合框架】JDK1.8源码分析之HashMap
一.前言 在分析jdk1.8后的HashMap源码时,发现网上好多分析都是基于之前的jdk,而Java8的HashMap对之前做了较大的优化,其中最重要的一个优化就是桶中的元素不再唯一按照链表组合,也 ...
- ashx后门[转]
https://www.cnblogs.com/Fluorescence-tjy/p/9855828.html 一.标准ASPX一句话木马 .NET平台下的一句话木马则百年不变,最常见的当属下面这句 ...
- 利用DOM节点找对象和直接在标签属性中调函数传值this的书写区别
同样的功能,不同的书写格式. 1.个人觉得比较繁琐的写法,但是比较常见,特别是在大项目的时候常用的就是这种方法: <div id="mouse" onmouseover=&q ...
- Python flask构建微信小程序订餐系统✍✍✍
Python flask构建微信小程序订餐系统 整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的感受,单论单个知识点课程本身没问题, ...
- 2018ICPC焦作 F. Honeycomb /// BFS
题目大意: 给定n m表示一共n行每行m个蜂巢 求从S到T的最短路径 input 1 3 4 +---+ +---+ / \ / \ + +---+ +---+ \ \ / \ + + S +---+ ...
- Red and Black 模板题 /// BFS oj22063
题目大意: Description There is a rectangular room, covered with square tiles. Each tile is colored eithe ...
- JDBC_Template(简化代码)
/** * @Description: TODO(这里用一句话描述这个类的作用) * @Author aikang * @Date 2019/8/27 11:03 */ /* Spring JDBC: ...