数学--数论--HDU 6128 Inverse of sum (公式推导论)
2024-08-28 09:24:56
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;
}
最新文章
- angularjs 中的setTimeout(),setInterval() / $interval 和 $timeout
- Linux日志不记录问题
- NSArray
- XdbxAnalysis
- Java中区别.toString() ,(String),valueOf()方法
- data and dream
- 诚聘:全栈开发人员,三线城市10-16K
- 【转载】wireshark:no interface can be used for capturing in this system with the current configuration
- JVM内存配置
- java按值传递相关理解
- 设置只为View加一条边框,子视图大小超出父视图大小,边框在子视图下边显示
- 消除QQ表情小游戏
- 应用引擎BAE3.0(转)
- MyGeneration模板生成NHibernate映射文件和关系(one-to-one,one-to-many,many-to-many)
- 【HDOJ】1979 Fill the blanks
- Miller-Rabin质数测试
- css动画,css过度,js动画
- Java--高效的定时任务设计
- django rest-framework 4.REST的认证和权限
- 【Contest Hunter 5302】金字塔
热门文章
- 详细解析 HBASE 配置的各种要点
- android学习相关intent和fragment的先关知识点
- linux之进程管理(二)
- Linux 压缩备份篇(一 压缩与解压缩)
- Mysqldump参数大全 这 些参数 不同于 mysql 的那些参数(下边文章开头有链接) :2 种类型的参数含义是不一样的
- java 中类为啥要序列化
- Weblogic-SSRF 漏洞复现
- 讲讲HashMap的理解,以及HashMap在1.7和1.8版本的变化(2020/4/16)
- 如何使用Three.js加载obj和mtl文件
- delphi使用ADO在sql数据库存取图片的方法