The saying “You can’t see the wood for the trees” is not only a cliche, but is also incorrect. The realproblem is that you can’t see the trees for the wood. If you stand in the middle of a wood, the treestend to obscure each other and the number of distinct trees you can actually see is quite small. This isespecially true if the trees are planted in rows and columns, because they tend to line up. The purposeof this problem is to find how many distinct trees one can see if one were standing on the position of atree in the middle of the wood.For a mathematically more precise description we assume that you are situated at the origin of acoordinate system in the plane.Trees are planted at all positions (x, y) ∈ ZZ × ZZ\{(0, 0)}, with |x| ≤ a and |y| ≤ b.A tree at position (x, y) can be seen from the origin if there are no other trees on the straight linefrom (0, 0) to (x, y). Find the number K of all the trees in the wood that can be seen from the originand the number N of all the trees to compute the fraction KN.Hint: The Euler phi function φ(n) is defined to be the number of integers m in the range 1 ≤ m ≤ nrelatively prime to n:φ(n) = #{m | 1 ≤ m ≤ n and gcd(m, n) = 1} (gcd = greatest common divisor)Instead of counting (an adequate method for small n!) you could as well use the following identity:φ(n) = n∏p∈P,p|n(1 ?1p), P = {p ∈ IN|p prime}Hint: Remember that gcd(m, n) = gcd(m + n, n) = gcd(m + 2n, n) = gcd(m + in, i)You might be surprised that the fraction KNconverges to 6π2 ≈ 0.607927 for an infinitely largewood.
Input
Each scenario consists of a line with the two numbers a and b (separated by a white space), with1 ≤ a ≤ 2000 and 1 ≤ b ≤ 2000000. Input is terminated by a line with two zeros.
Output
For each scenario print a line containing the fraction with a precision of 7 digits after the decimal point.Error less than 2e-7 or 2 ? 10?7 will be tolerated.
Sample Input
3 2
0 0
Sample Output
0.7058824
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
欧拉函数+神奇的思路~
类似于 洛谷 P2158 [SDOI2008]仪仗队,详见http://blog.csdn.net/senyelicone/article/details/52264236~
但是这一道是长方形的,切成正方形后再单独计算剩下的长条即可。
对于一个点(x,y),如果x与y互质,则这个点可以被看到,所以就用欧拉函数来计算。
另外,坐标轴上的点要单另计算,四个区间完全相同直接乘4即可。
原点要减掉,一定要用long long!
#include<cstdio>
#define ll long longll n,m,a[2000001],fi[2000001],tot;
bool b[2000001];ll gcd(ll u,ll v)
{return v ? gcd(v,u%v):u;
}int main()
{fi[1]=1;for(ll i=2;i<=2000000;i++){if(!b[i]) a[++a[0]]=i,fi[i]=i-1;for(ll j=1;a[j]*i<=2000000;j++){b[i*a[j]]=1;if(!(i%a[j])){fi[i*a[j]]=fi[i]*a[j];break;}fi[i*a[j]]=fi[i]*(a[j]-1);}}while(scanf("%lld%lld",&n,&m)==2 && n){tot=0;for(ll i=1;i<=n;i++){tot+=m/i*fi[i];for(ll j=m%i;j;j--) if(gcd(i,j)==1) tot++;}tot*=4;tot+=4;printf("%.7lf\n",(double)tot/((2*n+1)*(2*m+1)-1));}return 0;
}