Gym 100947I-What a Mess

I - What a Mess
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit

Status

Practice

Gym 100947I
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 <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <map>
#include <cstdlib>
int num[100000];
using namespace std;
int main (void)
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d",&num[i]);
        }
        sort(num,num+n);
        long long ans=0;
        for(int i=0;i<n;i++)
        {
            if(num[i]==1)
            {
                ans+=(n-i+1);//1能被任何数整除
            }
            else
            {
                for(int j=1;j*num[i]<=num[n-1];j++)
                {
                    int r=upper_bound(num+i+1,num+n,num[i]*j)-(num+i+1);
                    int l=lower_bound(num+i+1,num+n,num[i]*j)-(num+i+1);
                    //printf("r=%d  l=%d  \n",r,l);
                    if(l==r)
                        continue;
                    else
                        ans+=r-l;
                }
            }
        }
        printf("%lld\n",ans);

    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值