UCF Local Programming Contest 2016 H. Count the Dividing Pairs(整除对数)

Number Theory provides many fascinating properties. You have most likely written programs dealing with different groups of numbers such as Prime, Perfect, Amicable, Happy, Powerful, and Untouchable numbers, just to name a few. In this problem, you’ll attack yet another fascinating property of numbers, one dealing with pairs of numbers. 

An integer D is said to be a proper divisor of an integer N if D ≠ N and there exist an integer Q such that N = Q * D. For example, 4 is a proper divisor of 8 and 5 is a proper divisor of 15, but 9 is not a proper divisor of 9 and 6 is not a proper divisor of 8. Note that zero is not a proper divisor of any number but all numbers (except zero) are proper divisors of zero. 

We will call (D, N) as defined above “proper dividing pairs”. 

The Problem: 

Given a list of integers A = {A1, A2, …, Ap}, you are to determine (count) the number of proper dividing pairs (Ai, Aj), where 1 ≤ i, j ≤ p. 

The Input: 

The first input line contains a positive integer, n, indicating the number of test cases to process. Each test case starts with an integer, p (2 ≤ p ≤ 1000,000), indicating the number of integers in the list. The following input line will provide p integers, Ai (0 ≤ Ai ≤ 10,000,000). 

The Output: 

For each test case, print "Test case #t: m", where t indicates the case number starting with 1 and m indicates the number of proper dividing pairs. Leave a blank line after the output for each test case.

Note that, as illustrated in Sample Input/Output, duplicate values in the input list are considered as different elements in the list and they each contribute to the total count (proper dividing pairs).

样例输入复制

5 
3 
1 2 3 
4 
1 2 3 1 
2 
7 5 
3 
29 0 17 
10 
32 16 8 4 2 2 4 8 16 32

样例输出复制

Test case #1: 2 

Test case #2: 4 

Test case #3: 0 

Test case #4: 2 

Test case #5: 40

题意:

求数列中能够整除的对数,不包括自己整除自己。

思路:

用数组num[i]标记 i 出现过多少次,暴力枚举整除对

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const int N = 1e7 + 10;

int num[N];

int main()
{
    int t, n, kcase = 0, a;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        memset(num, 0, sizeof(num));
        int maxx = -1;
        for(int i = 1; i <= n; ++i)
        {
            scanf("%d", &a);
            num[a]++;
            if(maxx < a)
                maxx = a;
        }
///        ll ans = (ll)(num[0] * (n - num[0]) + num[1] * (n - num[0] - num[1]));   ///WA 爆int了????
        ll ans = (ll)num[0] * (n - num[0]) + (ll)num[1] * (n - num[0] - num[1]);    ///AC
        for(int i = 2; i <= maxx / 2; ++i)
        {
            if(num[i])
            {
                for(int j = i + i; j <= maxx; j += i)
                {
                    if(num[j])
                    {
                        ans += (ll)num[i] * num[j];
                    }
                }
            }
        }
        printf("Test case #%d: %lld\n\n", ++kcase, ans);
    }
    return 0;
}

 

全部评论

相关推荐

03-19 10:36
云南大学 C++
点赞 评论 收藏
分享
01-30 09:45
燕山大学 Java
喵_coding:这种直接跑就完事了 哪有毕业了才签合同 任何offer和三方都没有的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
6539次浏览 63人参与
# 你的实习产出是真实的还是包装的? #
1304次浏览 32人参与
# 米连集团26产品管培生项目 #
4732次浏览 206人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
7077次浏览 37人参与
# 简历第一个项目做什么 #
31329次浏览 315人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
186521次浏览 1115人参与
# MiniMax求职进展汇总 #
23225次浏览 302人参与
# 研究所笔面经互助 #
118787次浏览 577人参与
# 面试紧张时你会有什么表现? #
30416次浏览 188人参与
# 简历中的项目经历要怎么写? #
309572次浏览 4163人参与
# 职能管理面试记录 #
10722次浏览 59人参与
# AI时代,哪些岗位最容易被淘汰 #
62757次浏览 749人参与
# 网易游戏笔试 #
6374次浏览 83人参与
# 把自己当AI,现在最消耗你token的问题是什么? #
6993次浏览 154人参与
# 腾讯音乐求职进展汇总 #
160437次浏览 1107人参与
# 从哪些方向判断这个offer值不值得去? #
56712次浏览 357人参与
# 正在春招的你,也参与了去年秋招吗? #
362741次浏览 2632人参与
# 你怎么看待AI面试 #
179417次浏览 1182人参与
# 小红书求职进展汇总 #
226905次浏览 1357人参与
# 你的房租占工资的比例是多少? #
92146次浏览 896人参与
# 校招笔试 #
467654次浏览 2954人参与
# 经纬恒润求职进展汇总 #
155713次浏览 1085人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务