zoukankan      html  css  js  c++  java
  • 2014鞍山现场赛C题HDU5072(素筛+容斥原理)

    Coprime

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
    Total Submission(s): 130    Accepted Submission(s): 59


    Problem Description
    There are n people standing in a line. Each of them has a unique id number.

    Now the Ragnarok is coming. We should choose 3 people to defend the evil. As a group, the 3 people should be able to communicate. They are able to communicate if and only if their id numbers are pairwise coprime or pairwise not coprime. In other words, if their id numbers are a, b, c, then they can communicate if and only if [(a, b) = (b, c) = (a, c) = 1] or [(a, b) ≠ 1 and (a, c) ≠ 1 and (b, c) ≠ 1], where (x, y) denotes the greatest common divisor of x and y.

    We want to know how many 3-people-groups can be chosen from the n people.
     

    Input
    The first line contains an integer T (T ≤ 5), denoting the number of the test cases.

    For each test case, the first line contains an integer n(3 ≤ n ≤ 105), denoting the number of people. The next line contains n distinct integers a1, a2, . . . , an(1 ≤ ai ≤ 105) separated by a single space, where ai stands for the id number of the i-th person.
     

    Output
    For each test case, output the answer in a line.
     

    Sample Input
    1 5 1 3 9 10 2
     

    Sample Output
    4

    题意:求出满足三个数的集合个数,使得每一个集合里的数满足两两互质或两两不互质

    思路:因为n非常大,一定是得寻求一种nlogn的解法

                注意到这题能够通过求解反面来求得答案

                集合里的三个数,两两互质或两两不互质的反面是 至少有一对不互质和至少有一对互质

                设这三个数为a,b,c,如今如果a与b互质,b与c不互质,设满足这种集合数位temp,那么答案为C(n,3)-temp/2,之所以除2是由于b作为中间的数算重了一次

                如今问题转为求解temp

                对于每一个b,设与它互质的数个数为cnt1,与它不互质的个数为cnt2,显然cnt1+cnt2+1=n

                那么对于b的temp值为cnt*cnt2,当中仅仅须要求出cnt2就可以,将b分解质因子,然后这些质因子的个数不会超过6个,显然能够状压以后容斥原理求解

                如今问题进而转化为求出一个数x有多少个数是x的倍数,这个用素数筛选能够求得,问题得以解决

  • 相关阅读:
    client offset screen 的区别
    js中const,var,let区别
    jquery的选择器
    gulp
    JS 实现图片放大效果
    html单个标签实现跑马灯效果
    前端之HTML知识点整理
    各种纯css图标
    防止反复点击的思路
    .NET Memcached Client 扩展获取所有缓存Key
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4356111.html
Copyright © 2011-2022 走看看