题目链接

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. PSP 进度条 柱状图 饼状图

    9号 类别 开始时间 结束时间 间隔 净时间 燃尽图 8::00 8:20 0 20分钟 站立会议 8:20 8:50 0 30分钟 读构建之法 9:20 13:20 120分钟 120分钟 四人小组 ...

  2. Node.js系列——(4)优势及场景

    背景 之前几篇系列文章简单介绍了node.js的安装配置及基本操作: Node.js系列--(1)安装配置与基本使用 Node.js系列--(2)发起get/post请求 Node.js系列--(3) ...

  3. MySQL专题 2 数据库优化 Slow Query log

    MySQL Server 有四种类型的日志——Error Log.General Query Log.Binary Log 和 Slow Query Log. 第一个是错误日志,记录 mysqld 的 ...

  4. Vue2.0 - 全局操作 Vue.set

    引:http://www.cnblogs.com/zccblog/p/7192420.html Vue.set 的作用就是在构造器外部操作构造器内部的数据.属性或者方法.比如在vue构造器内部定义了一 ...

  5. 【Python】Python的time和datetime模块

    time 常用的有time.time()和time.sleep()函数. import time print(time.time()) 1499305554.3239055 上面的浮点数称为UNIX纪 ...

  6. 【bzoj4244】邮戳拉力赛 背包dp

    题目描述 IOI铁路是由N+2个站点构成的直线线路.这条线路的车站从某一端的车站开始顺次标号为0...N+1. 这条路线上行驶的电车分为上行电车和下行电车两种,上行电车沿编号增大方向行驶,下行电车沿编 ...

  7. BZOJ3717 PA2014Pakowanie(状压dp)

    显然贪心地有尽量先往容量大的背包里放.设f[i]为i子集物品最小占用背包数,g[i]为该情况下最后一个背包的剩余容量,转移显然. #include<iostream> #include&l ...

  8. Django安装及简介

    一. Django简介 Python下有许多款不同的 Web 框架.Django是重量级选手中最有代表性的一位.许多成功的网站和APP都基于Django. Django是一个开放源代码的Web应用框架 ...

  9. Spring Boot系列教程一:Eclipse安装spring-tool-suite插件

    一.前言        一直使用eclipse,个人习惯选用Eclipse+spring-tool-suite进行开发,特别注意Eclipse要选用对应的spring-tool-suite进行安装,这 ...

  10. CF 566A Matching Names

    CF 566A Matching Names 题目描述 给出n个名字和n个昵称,求一个名字和昵称的劈配方案,使得被劈配的名字和昵称的最长公共前缀长度的和最大. 1<=n<=100000 字 ...