HDU 3908 Triple (Reverse Thinking)

Source: Internet
Author: User

 

Given an array, determine the number of such three elements in the array: either the three elements are mutually exclusive, or the three elements are mutually exclusive.

 

Ans = C (n, 3)-three numbers have a pair of mutual elements and a pair of non-Mutual elements sum

How is sum calculated?

 

For num [I], to calculate the number of elements of the mutual element pnum, then the number of elements of the non-Mutual element is the n-1-pnum, for num [I, the number of tuples that do not meet the condition is pnum * (n-1-pnum ).

Note that in addition to 2, for example (2, 3, 4), it should be calculated once when processing 2, and once when processing 4, but when processing 3, certainly not, because for 3, 2 and 3 are also the same, so in general, every triple is counted once.

 

Complete code:

/* 1015 ms, 260KB */# include

 
  
Const int maxn = 805; int a [maxn]; int gcd (int a, int B) {return B? Gcd (B, a % B): a ;}int main () {int t; scanf (% d, & t); while (t --) {int n; scanf (% d, & n); for (int I = 0; I <n; I ++) scanf (% d, & a [I]); int ans = 0; for (int I = 0; I <n; I ++) {int num = 0; for (int j = 0; j <n; j ++) if (I! = J & gcd (a [I], a [j]) = 1) + + num; ans + = num * (n-num-1 );} printf (% d, n * (n-1) * (n-2)/6-ans/2);} return 0 ;}

 

 

Related Article

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.