Triple acm hdu 3908)

Source: Internet
Author: User
Triple

Time Limit: 5000/3000 MS (Java/others) memory limit: 125536/65536 K (Java/Others)
Total submission (s): 387 accepted submission (s): 153

Problem descriptiongiven contains different integers, find out the number of triples (a, B, c) which satisfy a, B, C are co-primed each other or are not co-primed each other. in a triple, (A, B, C) and (B, A, C) are considered as same triple.

Inputthe first line contains a single integer T (t <= 15), indicating the number of test cases.
In each case, the first line contains one integer N (3 <= n <= 800), second line contains n different integers D (2 <= d <105) separated with space.

Outputfor each test case, output an integer in one line, indicating the number of triples.

Sample input1
6
2 3 5 7 11 13

Sample output20

Source2011 multi-university training contest 7-host by ECNU

Recommendxubiao
 /*  
Description:
Give you n numbers and ask how many combinations of A, B, and C meet condition 1 or condition 2.
Condition 1: Any two qualities
Condition 2: Any two do not have mutual quality.
Solution report:
You only need to count the numbers a [I]: And the number of I numbers.
And B [I]: the number of unique I numbers.
Then a [I] * B [I] is a subset of an invalid combination containing I.
It is not difficult to find that such an operation on each I can overwrite all the ABC statements that do not meet the conditions, and calculate twice.
So C (n, 3)-sum/2.

*/
# Include < Stdio. h >
# Define Maxn 10010
Int A [maxn], B [maxn], num [maxn];
Int Gcd ( Int Da, Int Xiao) // The cycle form of finding the maximum common approx.
{
Int Temp;
While (Xiao ! = 0 )
{
Temp = Da % Xiao;
Da = Xiao;
Xiao = Temp;
}
Return Da;
}
Int Main ()
{
// Freopen ("test. In", "r", stdin );
// Freopen ("test. Out", "W", stdout );
Int T, n, I, j;
Int SUM; // Total number of nonconformities
Scanf ( " % D " , & T );
While (T -- )
{
Scanf ( " % D " , & N );
For (I = 1 ; I <= N; I ++ )
{
Scanf ( " % D " , & Num [I]);
A [I] = 0 ; // A [I] indicates the number of interchange qualities with num [I]
B [I] = 0 ; // A [I] indicates the number of numbers that do not communicate with num [I ].
}
Sum = 0 ;
For (I = 1 ; I <= N; I ++ )
{
For (J = 1 ; J < I; j ++ )
{
If (Gcd (Num [I], num [J]) = 1 ) A [I] ++ ;
Else B [I] ++ ;
}
For (J = I + 1 ; J <= N; j ++ )
{
If (Gcd (Num [I], num [J]) = 1 ) A [I] ++ ;
Else B [I] ++ ;
}
Sum + = A [I] * B [I];
}
Int CNT = N * (N - 1 ) * (N - 2 ) / 6 - Sum / 2 ;
Printf ( " % D \ n " , CNT );
}
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.