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 ...
随机推荐
- python爬虫_从零开始破解js加密(一)
除了一些类似字体反爬之类的奇淫技巧,js加密应该是反爬相当常见的一部分了,这也是一个分水岭,我能解决基本js加密的才能算入阶. 最近正好遇到一个比较简单的js,跟大家分享一下迅雷网盘搜索_838888 ...
- 3.4_springboot2.x整合spring Data Elasticsearch
Spring Data Elasticsearch 是spring data对elasticsearch进行的封装. 这里有两种方式操作elasticsearch: 1.使用Elasticsearch ...
- StringBuilder 和 StringBuffer类
通常在涉及到StringBuilder和StringBuffer时中任何一个时,都应该想到另外一个并且在脑海中问自己是否用另外一个更加合适. 为什么这么说,请继续往下看,当然如果你已经对二者烂熟于胸自 ...
- SolrCloud6.1.0之SQL查询测试
Solr发展飞快,现在最新的版本已经6.1.0了,下面来回顾下Solr6.x之后的一些新的特点: (1)并行SQL特性支持,编译成Streaming 表达式,可以在solrcloud集群中,并行执行 ...
- Erlang学习记录:输入和输出
输入和输出 输入和输出功能都被定义在io模块 输出功能非常常用,由于erlang项目没有可断点调试的IDE(或者说根本不需要),所以所有的调试操作都是由io输出 来调试的 io:get_line/1. ...
- springboot+mybatis 非web项目构建
https://blog.csdn.net/wlittlefive/article/details/86157134 https://blog.csdn.net/ththcc/article/deta ...
- leetcode-229-求众数②
题目描述: 方法一:摩尔投票法 class Solution: def majorityElement(self, nums: List[int]) -> List[int]: candiate ...
- Windows cd
显示当前目录名或改变当前目录. CHDIR [/D] [drive:][path]CHDIR [..]CD [/D] [drive:][path]CD [..] .. 指定要改成父目录. 键入 C ...
- thinkphp 使用函数
我们往往需要对模板输出变量使用函数,可以使用: 大理石平台支架 {$data.name|md5} 编译后的结果是: <?php echo (md5($data['name'])); ?> ...
- System.Web.Mvc.JavaScriptResult.cs
ylbtech-System.Web.Mvc.JavaScriptResult.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, P ...