zzuli 1902 985的因子对难题

Description

985有n个正整数,他想知道存在多少个不同的因子对(a[i], a[j])使得
1 <= i, j <= n && i != j && a[j] % a[i] == 0,其中i和j是元素的下标。
特别地,他认为(a[i],a[j])与(a[j],a[i])是一样的因子对。

Input

第一行输入一个整数t,代表有t组测试数据。
每组数据占两行,第一行输入一个n代表元素个数,下面一行输入n个整数a[]。
注:1 <= t <= 30,1 <= n <= 1e5,1 <= a[] <= 1e6。

Output

一个整数代表最后的答案。

Sample Input

251 2 3 4 552 2 2 2 2

Sample Output

510
题解:
因子对个数 = 该数个数 * 该数倍数的个数 + 该数的个数的组合数;
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

#define LL long long
#define N 100010
#define mod 1000000007
int a[N];
int main()
{
   int T,n,x;
   scanf("%d",&T);
   while(T--)
   {
       scanf("%d",&n);
       int maxx=0,ans=0;
       memset(a,0,sizeof(a));
       for(int i=1;i<=n;i++)
       {
           scanf("%d",&x);
           a[x]++;
           if(x>maxx) maxx=x;
       }
       for(int i=1;i<=maxx;i++)
       {
           if(a[i])
           {
               for(int j=i*2;j<=maxx;j+=i)
                 ans+=a[i]*a[j];
               ans+=a[i]*(a[i]-1)/2;
           }
       }
       printf("%d\n",ans);
   }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值