Description

给nn个小于pp的非负整数a1,…,na1,…,n,问有多少对(i,j)(1≤i<j≤n)(i,j)(1≤i<j≤n)模pp在意义下满足1ai+aj≡1ai+1aj1ai+aj≡1ai+1aj,即这两个数的和的逆元等于这两个数的逆元的和,注意0没有逆元

Input

第一行一整数TT表示用例组数,每组用例首先输入一整数nn表示序列长度和一素数pp表示模数,之后输入nn个非负整数a1,…,n(1≤T≤5,1≤n≤2×105,2≤p≤1018,0≤a1,…,n<p)a1,…,n(1≤T≤5,1≤n≤2×105,2≤p≤1018,0≤a1,…,n<p)

Output

输出满足条件的(i,j)(1≤i<j≤n)(i,j)(1≤i<j≤n)对数

Sample Input

2

5 7

1 2 3 4 5

6 7

1 2 3 4 5 6

Sample Output

4

6





最后我明白了个道理,当底数过大时,不能用普通乘法,更不不能用快速幂,因为乘一遍就爆了。于是酿成惨剧!

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define _m(a, p) make_pair(a, p)
map<ll, ll> mp;
map<ll, ll> mm;
ll mod;
long long ksc(long long a, long long b, long long mod)
{
long long ans = 0;
for (; b; b >>= 1){
if (b & 1)
ans = (ans + a) % mod;
a = (a + a) % mod; //(计算机加法比乘法快,a+a比a*2快)
}
return ans;
}
int main()
{
int t, n;
scanf("%d", &t);
while (t--)
{
ll cnt = 0;
mp.clear();
mm.clear();
scanf("%d %lld", &n, &mod);
for (int i = 0; i < n; i++)
{
ll a;
scanf("%lld", &a);
if (!a)
continue;
mm[a]++;
ll ans = ksc(ksc(a, a, mod), a, mod);
mp[ans]++;
}
for (auto p : mp)
{
ll cc = p.second;
cnt += cc * (cc - 1) / 2;
}
if (mod != 3)
for (auto m : mm)
{
ll n = m.second;
cnt -= n * (n - 1) / 2;
}
printf("%lld\n", cnt);
}
return 0;
}

数学--数论--HDU 6128 Inverse of sum (公式推导论)的更多相关文章

  1. 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 ...

  2. HDU 6128 Inverse of sum(同余)

    http://acm.hdu.edu.cn/showproblem.php?pid=6128 题意:有一个a数列,并且每个数都小于p,现在要求有多少对$(i,j)$满足$\frac{1}{a_i+a_ ...

  3. 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 ...

  4. 数学--数论--HDU 2802 F(N) 公式推导或矩阵快速幂

    Giving the N, can you tell me the answer of F(N)? Input Each test case contains a single integer N(1 ...

  5. 数学--数论--hdu 6216 A Cubic number and A Cubic Number (公式推导)

    A cubic number is the result of using a whole number in a multiplication three times. For example, 3 ...

  6. 数学--数论--HDU 1299 +POJ 2917 Diophantus of Alexandria (因子个数函数+公式推导)

    Diophantus of Alexandria was an egypt mathematician living in Alexandria. He was one of the first ma ...

  7. 数学--数论--HDU - 6395 Let us define a sequence as below 分段矩阵快速幂

    Your job is simple, for each task, you should output Fn module 109+7. Input The first line has only ...

  8. 数学--数论--HDU 5223 - GCD

    Describtion In mathematics, the greatest common divisor (gcd) of two or more integers, when at least ...

  9. 数学--数论--HDU 2582 F(N) 暴力打表找规律

    This time I need you to calculate the f(n) . (3<=n<=1000000) f(n)= Gcd(3)+Gcd(4)+-+Gcd(i)+-+Gc ...

随机推荐

  1. 在写微信小程序如何 首次编译的是当前写的页面

    首先点击顶部的编译如下图 染后点击添加模式哈 选择页面加载是启动的是哪一个页面

  2. Linux kernel min/max宏

    #define min(x,y) ({ \ typeof(x) _x = (x); \ typeof(y) _y = (y); \ (void) (&_x == &_y); \ _x ...

  3. alg-链表中有环

    typedef struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(nullptr) {} }ListN ...

  4. 怎么入门python?不懂你别瞎尝试,看看大佬怎么说

    学习任何一门语言都是从入门,通过不间断练习达到熟练水准.虽然万事开头难,但好的开始是成功的一半,今天这篇文章就来谈谈怎么入门python? 在开始学习python之前,你需要确定好学习计划和方式 比如 ...

  5. 微信小程序 —搜索框

    wxSearch优雅的微信小程序搜索框 一.功能 支持自定义热门key 支持搜索历史 支持搜索建议 支持搜索历史(记录)缓存 二.使用 1.将wxSearch文件夹整个拷贝到根目录下 2.引入 // ...

  6. js拼接onclick方法字符串参数解决方法

    onclick = contentmap("'+useridarr[i]+'")

  7. NCTF2018_easy_audit->coding_breaks

    easy_audit 题目源码 <?php highlight_file(__FILE__); error_reporting(0); if($_REQUEST){ foreach ($_REQ ...

  8. 新手想掌握Python技能需要众多Python项目练习,适合项目有哪些?

    适合新手练习的Python项目有哪些?简单易上手的Python项目汇总:Web 项目设计:内容聚合器.正则表达式查询工具.网址缩短.便利贴.功能.测验.GUI 项目设计:MP3 播放器.闹铃提醒工具. ...

  9. RESTFul 设计规范

    REST这个词,是Roy Thomas Fielding博士在他2000年提出的,有兴趣的可以阅读一下他的论文, 论文地址为:http://www.ics.uci.edu/~fielding/pubs ...

  10. Liunx常用操作(二)-vim中删除命令

    VIM简介 Vim是一个类似于Vi的著名的功能强大.高度可定制的文本编辑器,在Vi的基础上改进和增加了很多特性.VIM是自由软件.Vim普遍被推崇为类Vi编辑器中最好的一个,事实上真正的劲敌来自Ema ...