TJU 1100 Pi double精度 gcd

1100.    Pi
Time Limit: 1.0 Seconds    Memory Limit: 65536K
Total Runs: 6001    Accepted Runs: 2420



Professor Robert A. J. Matthews of the Applied Mathematics and Computer Science Department at the University of Aston in Birmingham, England has recently described how the positions of stars across the night sky may be used to deduce a surprisingly accurate value of π. This result followed from the application of certain theorems in number theory.

Here, we don't have the night sky, but can use the same theoretical basis to form an estimate for π:

Given any pair of whole numbers chosen from a large, random collection of numbers, the probability that the two numbers have no common factor other than one (1) is

For example, using the small collection of numbers: 2, 3, 4, 5, 6; there are 10 pairs that can be formed: (2,3), (2,4), etc. Six of the 10 pairs: (2,3), (2,5), (3,4), (3,5), (4,5) and (5,6) have no common factor other than one. Using the ratio of the counts as the probability we have:

In this problem, you'll receive a series of data sets. Each data set contains a set of pseudo-random positive integers. For each data set, find the portion of the pairs which may be formed that have no common factor other than one (1), and use the method illustrated above to obtain an estimate for π. Report this estimate for each data set.


Input

The input consists of a series of data sets.

The first line of each data set contains a positive integer value, N, greater than one (1) and less than 50.

There is one positive integer per line for the next N lines that constitute the set for which the pairs are to be examined. These integers are each greater than 0 and less than 32768.

Each integer of the input stream has its first digit as the first character on the input line.

The set size designator, N, will be zero to indicate the end of data.


Output

A line with a single real value is to be emitted for each input data set encountered. This value is the estimate for π for the data set. An output format like the sample below should be used. Answers must be rounded to six digits after the decimal point.

For some data sets, it may be impossible to estimate a value for π. This occurs when there are no pairs without common factors. In these cases, emit the single-line message:

No estimate for this data set.

exactly, starting with the first character, "N", as the first character on the line.


Sample Input

5
2
3
4
5
6
2
13
39
0


Sample Output

3.162278
No estimate for this data set.

题目大意就是 先求出总对数,第一组数据中(2,3),(2,4),(2,5),(2,6),(3,4),(3,5)...一共有10组,然后求出最大公约数为1的对数,第一组数据中(2,3),(2,5),(3,4)...一共6组然后 用公式6/(pi^2)=最大公约数为1的对数/总对数,把pi解出来,如果互质的对数为0的话输出那串英文。

#include"stdio.h"
#include"string.h"
#include"sstream"
#include"iostream"
#include"cmath"
#include"vector"
#include"algorithm"
using namespace std;
int prime[100000];
int a[1000];
void list()
{
    prime[0]=prime[1]=1;
    for(int i=2;i*i<=100000;i++)
        if(!prime[i])
        for(int j=i*i;j<=100000;j+=i)
        if(!prime[j]) prime[j]=1;
}
int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}
int main()
{

    //freopen("a.txt","r",stdin);
    int n;
    while(cin>>n,n)
    {
        int i,j;
        double all=(double)n/2.0*(double)(n-1);
        //cout<<"all="<<all<<endl;
        for(i=0;i<n;i++)
            cin>>a[i];
        double cnd=0;
        for(i=0;i<n-1;i++)
        {
            for(j=i+1;j<n;j++)
            {
                if(gcd(a[i],a[j])==1)
                {
                    //cout<<"a[i]="<<a[i]<<" "<<"a[j]="<<a[j]<<endl;
                    cnd++;
                }

            }
        }
        if(cnd==0) {puts("No estimate for this data set.");continue;}
        double ans=sqrt(6.0/(cnd/all));
        printf("%.6f\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值