题目链接

Problem Description

There are n nonnegative integers a1…n which are less than p. HazelFan wants to know how many pairs i,j(1≤i<j≤n) are there, satisfying 1ai+aj≡1ai+1aj when we calculate module p, which means the inverse element of their sum equals the sum of their inverse elements. Notice that zero element has no inverse element.

Input

The first line contains a positive integer T(1≤T≤5), denoting the number of test cases.

For each test case:

The first line contains two positive integers n,p(1≤n≤105,2≤p≤1018), and it is guaranteed that p is a prime number.

The second line contains n nonnegative integers a1...n(0≤ai<p).

Output

For each test case:

A single line contains a nonnegative integer, denoting the answer.

Sample Input

2

5 7

1 2 3 4 5

6 7

1 2 3 4 5 6

Sample Output

4

6

题意:

给定一个数组a,找出数组a里面所有的满足当(1≤i<j≤n)是,1/(ai+aj)≡1/ai+1/aj的关系有多少对。

分析:

如果暴力遍历整个a数组的话,因为i,j的位置都需要确定,时间复杂度相当于n^2,肯定会超时,所以想办法将上面的式子进行变形,使之变为在O(n)的时间之内可以确定出来结果。

将式子通分后化简可得(ai2+aj2+ai*aj)%p=0 。

然后等式两边同时乘上(ai-aj),化简可得(ai3-aj3)%p=0。现在的问题就转换为求满足这个关系的对数。

但是直接计算满足这个等式的pair的对数就可以了吗?不是。我们还要考虑到a[i]=a[j]的时候。

当a[i]=a[j]时,(ai2+aj2+aiaj)%p=0 可以转换为(a[i]a[i]+a[i]a[i]+a[i]a[i])%p=0%p(因为p是素数)是不满足条件的,但是我们直接计算上面那个式子会把满足这个关系的式子也算进去,所以我们需要把满足a[i]=a[j]即 3a[i]a[j]>0的这些对数减掉。 这样求出来的才是最终的结果。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll; const int N=1e5+7;
int t,n;
ll p,a[N];
map<ll,int>hsh;
map<ll,int>cnt; ll mul(ll a,ll b)///注意这里并不是整数幂,作用时将a连加b次,返回加后的结果
{
ll an=0;
while(b)
{
if(b&1)an=(an+a)%p;
b>>=1,a=(a+a)%p;
}
return an;
} int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d%lld",&n,&p);
hsh.clear();
cnt.clear();
ll ans=0;
for(int i=1;i<=n;i++)
{
scanf("%lld",a+i);
if(!a[i])continue;
if(mul(mul(a[i],a[i]),3))ans-=cnt[a[i]];///判断3*a[i]*a[i]的值是否大于0,大于的话要把之前加上的全部减去
ll tp=mul(mul(a[i],a[i]),a[i]);///求出的是a[i]^3
ans+=hsh[tp]++;///当前求出的这个tp值可以于之前的所有的相匹配,匹配过后个数再加,下次匹配时的方案数就是这次加过之后的
++cnt[a[i]];///a[i]所对应的值也要更新
}
printf("%lld\n",ans);
}
return 0;
}

2017ACM暑期多校联合训练 - Team 7 1009 HDU 6128 Inverse of sum (数学计算)的更多相关文章

  1. 2017ACM暑期多校联合训练 - Team 3 1003 HDU 6058 Kanade's sum (模拟)

    题目链接 Problem Description Give you an array A[1..n]of length n. Let f(l,r,k) be the k-th largest elem ...

  2. 2017ACM暑期多校联合训练 - Team 2 1009 HDU 60563 TrickGCD (容斥公式)

    题目链接 Problem Description You are given an array A , and Zhu wants to know there are how many differe ...

  3. 2017ACM暑期多校联合训练 - Team 2 1011 HDU 6055 Regular polygon (数学规律)

    题目链接 **Problem Description On a two-dimensional plane, give you n integer points. Your task is to fi ...

  4. 2017ACM暑期多校联合训练 - Team 4 1004 HDU 6070 Dirt Ratio (线段树)

    题目链接 Problem Description In ACM/ICPC contest, the ''Dirt Ratio'' of a team is calculated in the foll ...

  5. 2017ACM暑期多校联合训练 - Team 9 1005 HDU 6165 FFF at Valentine (dfs)

    题目链接 Problem Description At Valentine's eve, Shylock and Lucar were enjoying their time as any other ...

  6. 2017ACM暑期多校联合训练 - Team 9 1010 HDU 6170 Two strings (dp)

    题目链接 Problem Description Giving two strings and you should judge if they are matched. The first stri ...

  7. 2017ACM暑期多校联合训练 - Team 8 1006 HDU 6138 Fleet of the Eternal Throne (字符串处理 AC自动机)

    题目链接 Problem Description The Eternal Fleet was built many centuries ago before the time of Valkorion ...

  8. 2017ACM暑期多校联合训练 - Team 8 1002 HDU 6134 Battlestation Operational (数论 莫比乌斯反演)

    题目链接 Problem Description The Death Star, known officially as the DS-1 Orbital Battle Station, also k ...

  9. 2017ACM暑期多校联合训练 - Team 8 1011 HDU 6143 Killer Names (容斥+排列组合,dp+整数快速幂)

    题目链接 Problem Description Galen Marek, codenamed Starkiller, was a male Human apprentice of the Sith ...

随机推荐

  1. 操作系统作业一——仿CMD

    实验一.CMD实验 2014商软2  卓宇靖  4238 一.        实验目的 (1)掌握命令解释程序的原理: (2)掌握简单的DOS调用方法: (3)掌握C语言编程初步. 二.        ...

  2. 201621123037 《Java程序设计》第3周学习总结

    #Week03-面向对象入门 1. 本周学习总结 初学面向对象,会学习到很多碎片化的概念与知识.尝试学会使用思维导图将这些碎片化的概念.知识点组织起来.请使用工具画出本周学习到的知识点及知识点之间的联 ...

  3. rfid工作原理

    RFID的工作原理是:标签进入磁场后,如果接收到阅读器发出的特殊射频信号,就能凭借感应电流所获得的能量发送出存储在芯片中的产品信息(即Passive Tag,无源标签或被动标签),或者主动发送某一频率 ...

  4. 【.Net】C# 将Access中时间段条件查询的数据添加到ListView中

    一.让ListView控件显示表头的方法 在窗体中添加ListView 空间,其属性中设置:View属性设置为:Detail,Columns集合中添加表头中的文字. 二.利用代码给ListView添加 ...

  5. AtCoder Regular Contest 083 C: Sugar Water

    题意 给你一个空杯子,有4种操作: 操作1 加100a克的水 操作2 加100b克的水 操作3 加c克的糖 操作4 加d克的糖 糖的质量不能超过水的质量e/100 糖和水的总质量不能超过f 糖的质量不 ...

  6. 【poj2409】Let it Bead Polya定理

    题目描述 用 $c$ 种颜色去染 $r$ 个点的环,如果两个环在旋转或翻转后是相同的,则称这两个环是同构的.求不同构的环的个数. $r·c\le 32$ . 题解 Polya定理 Burnside引理 ...

  7. 【uoj#310】[UNR #2]黎明前的巧克力 FWT

    题目描述 给出 $n$ 个数,从中选出两个互不相交的集合,使得第一个集合与第二个集合内的数的异或和相等.求总方案数. 输入 第一行一个正整数 $n$ ,表示巧克力的个数.第二行 $n$ 个整数 $a_ ...

  8. selenium学习网址

    1.http://www.testclass.net/selenium_java/#      testclass网址 2.http://www.yiibai.com/selenium/seleniu ...

  9. 洛谷P1345 [USACO5.4]奶牛的电信(最小割)

    题目描述 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流.这些机器用如下的方式发送电邮:如果存在一个由c台电脑组成的序列a1,a2,...,a(c),且a1与a2相 ...

  10. [APIO2014]回文串 manacher 后缀数组

    题面:洛谷 题解: 还是这个性质:对于每个串而言,本质不同的回文串最多只有O(n)个. 所以我们先求出这O(n)个本质不同的回文串,然后对整个串求一次sa. 然后对于每个回文串,求出它的出现次数,更新 ...