You are given an array a1,a2,…,an consisting of n distinct integers. Count the number of pairs of indices (i,j) such that i<j and ai⋅aj=i+j.

Input
The first line contains one integer t (1≤t≤10^4) — the number of test cases. Then t cases follow.

The first line of each test case contains one integer n (2≤n≤10^5) — the length of array a.

The second line of each test case contains n space separated integers a1,a2,…,an (1≤ai≤2⋅n) — the array a. It is guaranteed that all elements are distinct.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅10^5.

Output
For each test case, output the number of pairs of indices (i,j) such that i<j and ai⋅aj=i+j.

Example
input
3
2
3 1
3
6 1 5
5
3 1 5 9 2
output
1
1
3

Note
For the first test case, the only pair that satisfies the constraints is (1,2), as a1⋅a2=1+2=3
For the second test case, the only pair that satisfies the constraints is (2,3).

For the third test case, the pairs that satisfy the constraints are (1,2), (1,5), and (2,3).

题意:
找出一个数组a[n]中有几对(a[i],a[j])满足 a[i] * a[j] = i + j (i < j),其中数组a[n]中的元素不重复。

思路:
暴力 + 剪枝

具体的剪枝思路:
因为是 a[i] * a[j] ,然后这里是根据 a[i] 来找 a[j] ,可以知道 i + j 一定是 a[i] 的倍数,i 是不变的,所以 j 的增加量一定是a[i] 的倍数。

ps:我一开始不知道咋剪枝,只剪了一点点还是TLM ……orz……然后后面还是一直wa,是因为数组没开long long ,(因为运算的数据会超过int的范围QAQ)

#include<iostream>
#include<cmath>
#include<algorithm>
#define bug(a) cout << a << "*" << endl;
using namespace std;typedef long long ll;
const int N = 2e5 + 5;
ll a[N];int main()
{ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);int T;cin >> T;while(T--){int n;cin >> n;ll ans = 0;for(int i = 1; i <= n; ++i)cin >> a[i];for(int i = 1; i <= n; ++i){for(int j = a[i] - i; j <= n; j += a[i]){if(j <= i)continue;if(a[i] * a[j] == i + j)ans++;}}cout << ans << endl;}return 0;}

B. Pleasant Pairs相关推荐

  1. Codeforces1541B Pleasant Pairs (思维)

    题目链接:Pleasant Pairs 大致题意 给出一个长度为n的序列a, 里面的数字各不相同且值域为[1, 2n]. 问你有多少满足: i < j 并且 a[i] * a [j] = i + ...

  2. Codeforces Round #728 (Div. 2)

    文章目录 A. Pretty Permutations B. Pleasant Pairs C. Great Graphs A. Pretty Permutations There are nnn c ...

  3. android安全开发_现代android安全开发

    android安全开发 第一次(错误)练习 (1st (Mistake) Practice) Given the "find out how to en-decrypt data in An ...

  4. codeforces 有意思的思维题 1 ~ 15

    codeforces 思维题 1.给定数组,求满足i < j and ai * aj = i + j的数对数量 2.第 i 步向前跳 i 步或后退 1 步 3.给两个点,求正方形的另两个点 4. ...

  5. Pairs Forming LCM LightOJ - 1236

    Pairs Forming LCM LightOJ - 1236 题意 问共有多少组数的最大公约数是n 分析 组合数学 ,唯一分解定理 参考代码 int Prime[670000]; const in ...

  6. 373. Find K Pairs with Smallest Sums (java,优先队列)

    题目: You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Def ...

  7. Codeforces Round #562 (Div. 2) B. Pairs

    链接:https://codeforces.com/contest/1169/problem/B 题意: Toad Ivan has mm pairs of integers, each intege ...

  8. R语言散点图可视化:自定义标题和标签、拟合回归线、lowess为散点图添加平滑拟合线、修改散点图中点颜色和点符号、分组散点图、添加图例、pairs可视化散点图矩阵、ggplt2可视化、lattice

    R语言散点图可视化:自定义标题和标签.拟合回归线.lowess为散点图添加平滑拟合线.修改散点图中点颜色和点符号.分组散点图.添加图例.pairs可视化散点图矩阵.ggplt2可视化.lattice ...

  9. 【leetcode】1013. Pairs of Songs With Total Durations Divisible by 60

    题目如下: In a list of songs, the i-th song has a duration of time[i] seconds. Return the number of pair ...

最新文章

  1. 模拟教务评教(强智教务)—一件评教实现原理
  2. 解决idea 控制台中文乱码
  3. JavaWEB中读取配置信息
  4. c++常用知识点,易错点,面试常问点
  5. 第九篇: 服务链路追踪(Spring Cloud Sleuth)(Finchley版本)
  6. Android SQLite数据库升级的问题
  7. JVM第四节:JVM 执行子程序
  8. Luogu1390 公约数的和
  9. 史上最简单的免费短信验证码案例
  10. 在IPCAM上实现RTSP协议直播-live555
  11. Golang-Beego(go mod)
  12. 基于springboot 支付宝app端支付,可用于uni-app使用
  13. 《浅入浅出》-RocketMQ
  14. 42表盘直径是从哪测量_表盘直径多大合适,怎样测量手表表盘的直径
  15. tabIndex的用途
  16. 字节跳动小程序tt.pay支付流程和遇到的问题
  17. 为什么要格式化namenode以及注意点
  18. 三面阿里,有惊无险成功拿到offer定级P7,只能说是真的难
  19. ArcMap模型构建器使用临时工作空间
  20. python编写一个班级类 点名簿_班级小游戏

热门文章

  1. 使用VS2017将我的c++项目打包成安装程序,并设置开机自启,卸载,图标等
  2. JavaBeans分类
  3. 《计算机网络(计算机)》
  4. Linux报错:Port xxx is in use by another program. Either identify and stop that program...
  5. 回归损失函数:Log-Cosh Loss
  6. Graphite安装
  7. 什么是GIS,GIS能干什么
  8. 浅谈推荐系统之内容推荐
  9. 【linux】一篇全面的linux软件包管理的总结
  10. 走近Harvest Moon:Moonbeam DeFi狂欢会