HDU 5072 coprime-tolerant principle

Source: Internet
Author: User
Tags greatest common divisor unique id

coprime Time limit:2000/1000 MS (java/others) Memory limit:262144/262144 K (java/others)
Total submission (s): 1460 Accepted Submission (s): 571


Problem Description There is n people standing in a line. Each of the 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 is able to communicate. They is able to communicate if and only if their ID numbers is pairwise coprime or pairwise not coprime. In other words, if their ID numbers is a, B, C, then they can communicate if and only if [(A, B) = (b, c) = (A, c) = 1] o R [(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 is chosen from the N people.
Input The first line contains an integer T (t≤5), denoting the number of the the test cases.

For each test case, the first line contains an integer n (3≤n≤10 5), denoting the number of people. The next line contains n distinct integers a 1, a 2, ..., a n (1≤a i≤10 5) separated by a single space, where a I st ANDs for the ID number of the i-th person.
Output for each test case, output of the answer in a line.
Sample Input

1 5 1 3 9 10 2
Sample Output
4
Source Asia Anshan Regional Contest

Choose A,b,c Three numbers from n numbers, either 22 coprime, or 22 not coprime, for combinatorial numbers.

So the solution = all combinations-coprime and non-coprime combined number of simultaneous existence

Then the number of numbers that are not coprime with a[i] can be calculated and the number of coprime with A[i]

Method: Count The factors that each number can do as the number

For a number, decompose the factorization, and then use the tolerant principle to figure out the number of coprime with this number.

So for A[i], select a coprime number of cases * Select a non-coprime number of cases = obtained is not satisfied with the condition and contains a[i] case.

As each number is processed, the final result is the number of cases not satisfied




#include <iostream> #include <cstdio> #include <vector> #include <cstring> using namespace std;
#define LL Long long #define MAXN 100007 int CHECK[MAXN];
Vector<int> PRIN[MAXN];
int NUM[MAXN];
    void Init () {memset (check,0,sizeof (check));
                for (int i = 2;i < MAXN, i++) {if (check[i] = = 0) {for (int j = I;j < Maxn; j+=i) {
                CHECK[J] = 1;
            Prin[j].push_back (i);
    }}}} int main () {init ();
    int n,t,u,s;
    scanf ("%d", &t);
        while (t--) {scanf ("%d", &n);
        memset (check,0,sizeof (check));
        memset (num,0,sizeof (num));
            for (int i = 0;i < n; i++) {scanf ("%d", &check[i]);
        num[check[i]]++; } for (int i = 1;i < MAXN; i++) for (int j = i+i; j < Maxn; j+= i) num[i] + = Num[j
        ];
        ll res = 0,ans = 0;
      for (int i = 0;i < n; i++) {u = check[i];      res = 0,s = Prin[u].size ();
                for (int j = 1;j < (1<<s); j + +) {int v = 1,f = 0;
                        for (int k = 0;k < S; k++) {if ((1<<k) &j) {f++;
                    V *= Prin[u][k];
                }} if (f&1) res + = Num[v]-1;
            else Res-= num[v]-1;
        } if (res) ans + = res* (n-res-1);
        } ans = 1ll*n* (n-1) * (n-2)/6-ANS/2;
    printf ("%i64d\n", ans);
} return 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.