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 ...
随机推荐
- 7 Scatter-loading Features
7.1 About scatter-loading The scatter-loading mechanism enables you to specify the memory map of an ...
- 拾遗:Perl 基础语法
Perl 常用的命令行参数 -i:将处理结果直接写入文件,可以通过 -i.bak 或 -i"/tmp/orig_*" 等形式,在修改之前进行备份 -e:启用 perl 的命令行模式 ...
- Markdown 语法大全
1 强调 星号与下划线都可以,单是斜体,双是粗体,符号可跨行,符号可加空格 **一个人来到田纳西** __毫无疑问__ *我做的馅饼 是全天下* _最好吃的_ 效果: 一个人来到田纳西 毫无疑问 我做 ...
- 城市代码表mysql
只有代码: # ************************************************************ # Sequel Pro SQL dump # Version ...
- 初探.Net Core API 网关Ocelot(一)
一.介绍 Ocelot 是基于.NetCore实现的开源的API网关,支持IdentityServer认证.Ocelot具有路由.请求聚合.服务发现.认证.鉴权.限流熔断等功能,并内置了负载均衡器与S ...
- OpenLiveWriter博客工具
1.OpenLiveWriter安装 官网下载地址:http://openlivewriter.org/ 默认安装到:C:\Users\用户\AppData\Local\OpenLiveWriter目 ...
- .Net串口通讯中的若干问题(C#多串口硬件识别、热插拔、Close方法报错问题、IsOpen的可靠性问题)
一.需求场景 最近有时间静下心来研究SDK,串口通讯的.要求实现识别cp210x和cp2303驱动的两款硬件,并且2303的优先级高,即有2303识别之,没有再识别210x:要求实现热插拔,拔掉自动断 ...
- __typeof与typeof
其实之前在stackoverflow就看过一篇讲的比较详细的, https://stackoverflow.com/questions/14877415/difference-between-type ...
- 【noi.ac-CSP-S全国模拟赛第三场】#705. mmt
给定数组a[],b[] 求$$c_i=\sum_{j=1}^{i} a_{\left \lfloor \frac{n}{j} \right \rfloor}·b_{i \bmod j}$$ 大概就是对 ...
- phonegap 开发指南系列----简介(2)
一.简介 Cordova提供了一组设备相关的API,通过这组API,移动应用能够以JavaScript访问原生的设备功能,如摄像头.麦克风等. Cordova还提供了一组统一的Ja ...