What a Mess

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

standard input/output  Announcement

 
  • Statements

    Alex is a very clever boy, after all he is the son of the greatest watchmaker in Odin.

    One day, Alex was playing with some old watches and he found n gears, each gear has ai teeth in it. Alex likes to make beautiful pairs of gears, he thinks a pair of gears is beautiful if we attach the two gears together and spin the first gear exactly one rotation, then the other gear spins an integer number of rotations. For example a pair of 8 and 4 is beautiful, whereas a pair of 8 and 5 isn't, neither is pair of 4 and 8.

    Now Alex is curious, he wants to know how many beautiful pairs are there. Counting is not Alex's thing, so he asked you to help him.

Input

The first line of input contains one integer T: The number of test cases you need to process.

Each test case consists of two lines. The first line is a single integer n: the number of gears Alex has. The second line contains n space separated integers ai: the number if teeth in the ith gear.

1 ≤ n ≤ 104

2 ≤ ai ≤ 106

Output

For each testcase print a single integer: the number of distinct pairs that satisfy the problem statement.

Sample Input

Input
2 5 4 6 7 8 12 6 2 2 2 3 3 4
Output
3 7

Hint

note that we consider two pair distinct when they differ by at least one gear.

In the first sample the pairs are: (4,8) , (4,12) , (6,12

题解:问可以整除的对数是多少;枚举倍数,二分;

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int MAXN = 1e4 + ;
int a[MAXN];
typedef long long LL;
int main(){
int T, n;
scanf("%d", &T);
while(T--){
scanf("%d", &n);
for(int i = ; i < n; i++){
scanf("%d", a + i);
}
sort(a, a + n);
LL ans = ;
for(int i = ; i < n; i++){
if(a[i] == ){
ans += (n - i - );
}
else{
for(int j = ; j * a[i] <= a[n - ]; j++){
int r = upper_bound(a + i + , a + n, a[i]*j) - (a + i + );
int l = lower_bound(a + i + , a + n, a[i]*j) - (a + i + );
if(l == r)continue;
else{
ans += r - l;
}
}
}
}
printf("%lld\n", ans);
}
return ;
}

What a Mess(二分)的更多相关文章

  1. CROC 2016 - Elimination Round (Rated Unofficial Edition) C. Enduring Exodus 二分

    C. Enduring Exodus 题目连接: http://www.codeforces.com/contest/655/problem/C Description In an attempt t ...

  2. codeforces 655C C. Enduring Exodus(二分)

    题目链接: C. Enduring Exodus time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  3. BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]

    1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 8748  Solved: 3835[Submi ...

  4. BZOJ 2756: [SCOI2012]奇怪的游戏 [最大流 二分]

    2756: [SCOI2012]奇怪的游戏 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 3352  Solved: 919[Submit][Stat ...

  5. 整体二分QAQ

    POJ 2104 K-th Number 时空隧道 题意: 给出一个序列,每次查询区间第k小 分析: 整体二分入门题? 代码: #include<algorithm> #include&l ...

  6. [bzoj2653][middle] (二分 + 主席树)

    Description 一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2],其中a,b从0开始标号,除法取下整. 给你一个长度为n的序列s. 回答Q个这样的询问:s的左端点在[a,b ...

  7. [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二

    Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...

  8. [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值

    Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...

  9. jvascript 顺序查找和二分查找法

    第一种:顺序查找法 中心思想:和数组中的值逐个比对! /* * 参数说明: * array:传入数组 * findVal:传入需要查找的数 */ function Orderseach(array,f ...

随机推荐

  1. android面试题之七

    三十六.请解释下在单线程模型中Message.Handler.Message Queue.Looper之间的关系. 简单的说,Handler获取当前线程中的looper对象,looper用来从存放Me ...

  2. swift2.0 UIImagePickerController 拍照 相册 录像

    系统 ios9.1 语言swift2.0 在app 里最常用的功能就是多媒体选择,首先我们storyboard 创建一个button 用于触发选择事件 @IBAction func selectIma ...

  3. JavaScript 函数基础

    1. JavaScript 函数基础 1. 定义方法 2. 函数的调用方法 3. 函数方法 apply : 将函数作为数组的方法来调用 将参数以数组形式传递给该方法 call   : 将函数作为对象的 ...

  4. DEV GridControl 导出到Excel

    SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Title = "导出Excel"; sa ...

  5. CentOS管理

    1.使用yum安装和卸载软件 主要功能是更方便的添加/删除/更新RPM包. 它能自动解决包的倚赖性问题. 它能便于管理大量系统的更新问题 一.yum list|more               列 ...

  6. 数据库分库分表(sharding)系列(四) 多数据源的事务处理

    系统经sharding改造之后,原来单一的数据库会演变成多个数据库,如何确保多数据源同时操作的原子性和一致性是不得不考虑的一个问题.总体上看,目前对于一个分布式系统的事务处理有三种方式:分布式事务.基 ...

  7. 关于Oracle dmp文件导入随笔

    进入博客园已经两年多了,每次想写点什么,都是给自己个各种借口,不了了之~今天就从Oracle数据库最长用的导入开始吧! 1.低版本的exp/imp可以连接到高版本(或同版本)的数据库服务器,比如:10 ...

  8. sqlserver2005重新安装(安装汇编错误,安装程序无法连接到数据库服务进行服务配置)

    2014-01-09 16:41 1687人阅读 评论(1) 收藏 举报 分类: 数据库(1) 版权声明:本文为博主原创文章,未经博主允许不得转载. sqlserver2005重新安装(安装汇编错误, ...

  9. poj1094 topsort

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32275   Accepted: 11 ...

  10. 费马小定理&欧拉定理

    在p是素数的情况下,对任意整数x都有xp≡x(mod p).这个定理被称作费马小定理其中如果x无法被p整除,我们有xp-1≡1(mod p).利用这条性质,在p是素数的情况下,就很容易求出一个数的逆元 ...