直接硬爆,把所有組合都找出來看看有沒有互質,再求機率即可。
C++(0.112)
/*******************************************************/
/* UVa 412 Pi */
/* Author: Maplewing [at] knightzone.studio */
/* Version: 2014/10/05 */
/*******************************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int gcd( int a, int b ){
while( (a %= b) && (b %= a) );
return a+b;
}
int main(){
int N;
while( scanf("%d", &N) != EOF && N != 0 ){
int dataset[55] = {0};
for( int i = 0 ; i < N ; ++i ){
scanf( "%d", &dataset[i] );
}
int sum = 0;
for( int i = 0 ; i < N ; ++i ){
for( int j = i+1 ; j < N ; ++j ){
if( gcd( dataset[i], dataset[j] ) == 1 ){
++sum;
}
}
}
if( sum == 0 ){
printf( "No estimate for this data set.\n" );
}
else{
printf("%.6f\n", sqrt( (float)((N * N - N) / 2) * 6.0f / (double)sum ));
}
}
return 0;
}