UVA10214 Trees in a Wood. 欧拉phi函数
2015-02-03 23:46
316 查看
只看某一个象限 能看到的数 == 一个 象限*4+4
能看到的树既距离原点的距离 gcd(x,y)==1
a 和 b 一大一小 预处理2000以内的phi函数,枚举小的一条边
从1...a 与 a gcd 为 1 的数的个数就是 phi(a)
从 1+a ... 2*a 与 a gcd 为 1 的数的个数 因为 GCD(i,a) = GCD(i+a,a) 所以还是 phi(a)
....
Trees in a Wood.
Submit Status
Description
![](http://uva.onlinejudge.org/components/com_onlinejudge/images/button_pdf.png)
Problem H
Trees in a Wood.
Input: Standard Input
Output: Standard Output
Time Limit: 10 seconds
number of distinct trees you can actually see is quite small. This is especially true if the trees are planted in rows and columns, because they tend to line up. The purpose of this problem is to find how many distinct trees one can see if one were standing
on the position of a tree in the middle of the wood.
Trees are planted at all positions
![](http://uva.onlinejudge.org/external/102/p10214a.gif)
, with |x|<=a and |y|<=b .
![](http://uva.onlinejudge.org/external/102/p10214b.gif)
A tree at position (x,y) can be seen from the origin if there are no other trees on the straight line from (0,0) to (x,y) . Find the number K of all the trees in the wood that can be seen from the origin and the number N of all the trees to
compute the fraction
![](http://uva.onlinejudge.org/external/102/p10214c.gif)
.
Hint: The Euler phi function
![](http://uva.onlinejudge.org/external/102/p10214d.gif)
is defined to be the number of integers m in
the range 1≤m≤n relatively prime to n :
![](http://uva.onlinejudge.org/external/102/p10214e.gif)
Instead of counting (an adequate method for small n !) you could as well use the following identity:
![](http://uva.onlinejudge.org/external/102/p10214f.gif)
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
![](http://uva.onlinejudge.org/external/102/p10214c.gif)
converges to
![](http://uva.onlinejudge.org/external/102/p10214g.gif)
0.607927
for an infinitely large wood.
![](http://uva.onlinejudge.org/external/102/p10214c.gif)
with a precision of 7 digits after the decimal point.
Error less than 2e-7 or 2*10^(-7) will be tolerated.
0 0
Collected from TUD Programming Contest
Source
Root :: AOAPC II: Beginning Algorithm Contests (Second Edition) (Rujia Liu) :: Chapter 10. Maths :: Examples
Submit Status
/* ***********************************************
Author :CKboss
Created Time :2015年02月03日 星期二 22时13分33秒
File Name :UVA10214.cpp
************************************************ */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;
int gcd(int a,int b)
{
return (b==0)?a:gcd(b,a%b);
}
int phi[2200];
int GCD[2020][2020];
void phi_table(int n)
{
phi[1]=1;
for(int i=2;i<=n;i++)
{
if(!phi[i])
{
for(int j=i;j<=n;j+=i)
{
if(!phi[j]) phi[j]=j;
phi[j]=phi[j]*(i-1)/i;
}
}
}
}
void init()
{
phi_table(2010);
for(int i=1;i<=2000;i++)
for(int j=1;j<=2000;j++)
if(GCD[i][j]==0)
GCD[i][j]=GCD[j][i]=gcd(i,j);
}
int a,b;
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
init();
while(scanf("%d%d",&a,&b)!=EOF)
{
if(a==0&&b==0) break;
if(a>b) swap(a,b);
double ret=0;
for(int i=1;i<=a;i++)
{
int duan = b/i;
int yu = b%i;
ret += duan * phi[i];
for(int j=1;j<=yu;j++)
{
if(GCD[j][i]==1)
ret+=1;
}
}
ret = ret*4 + 4;
double all = (2.*a+1)*(2.*b+1)-1.;
double ans = ret/all;
printf("%.7lf\n",ans);
}
return 0;
}
能看到的树既距离原点的距离 gcd(x,y)==1
a 和 b 一大一小 预处理2000以内的phi函数,枚举小的一条边
从1...a 与 a gcd 为 1 的数的个数就是 phi(a)
从 1+a ... 2*a 与 a gcd 为 1 的数的个数 因为 GCD(i,a) = GCD(i+a,a) 所以还是 phi(a)
....
Trees in a Wood.
Time Limit: 3000MS | Memory Limit: Unknown | 64bit IO Format: %lld & %llu |
Description
![](http://uva.onlinejudge.org/components/com_onlinejudge/images/button_pdf.png)
Problem H
Trees in a Wood.
Input: Standard Input
Output: Standard Output
Time Limit: 10 seconds
Background
The saying ``You can't see the wood for the trees'' is not only a cliche, but is also incorrect. The real problem is that you can't see the trees for the wood. If you stand in the middle of a wood, the trees tend to obscure each other and thenumber of distinct trees you can actually see is quite small. This is especially true if the trees are planted in rows and columns, because they tend to line up. The purpose of this problem is to find how many distinct trees one can see if one were standing
on the position of a tree in the middle of the wood.
The Problem
For a mathematically more precise description we assume that you are situated at the origin of a coordinate system in the plane.Trees are planted at all positions
![](http://uva.onlinejudge.org/external/102/p10214a.gif)
, with |x|<=a and |y|<=b .
![](http://uva.onlinejudge.org/external/102/p10214b.gif)
A tree at position (x,y) can be seen from the origin if there are no other trees on the straight line from (0,0) to (x,y) . Find the number K of all the trees in the wood that can be seen from the origin and the number N of all the trees to
compute the fraction
![](http://uva.onlinejudge.org/external/102/p10214c.gif)
.
Hint: The Euler phi function
![](http://uva.onlinejudge.org/external/102/p10214d.gif)
is defined to be the number of integers m in
the range 1≤m≤n relatively prime to n :
![](http://uva.onlinejudge.org/external/102/p10214e.gif)
Instead of counting (an adequate method for small n !) you could as well use the following identity:
![](http://uva.onlinejudge.org/external/102/p10214f.gif)
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
![](http://uva.onlinejudge.org/external/102/p10214c.gif)
converges to
![](http://uva.onlinejudge.org/external/102/p10214g.gif)
0.607927
for an infinitely large wood.
The Input
Each scenario consists of a line with the two numbers a and b (separated by a white space), with 1 <= a <= 2000 and 1 <= b <= 2000000 Input is terminated by a line with two zeros.The Output
For each scenario print a line containing the fraction![](http://uva.onlinejudge.org/external/102/p10214c.gif)
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 20 0
Sample Output
0.7058824Collected from TUD Programming Contest
Source
Root :: AOAPC II: Beginning Algorithm Contests (Second Edition) (Rujia Liu) :: Chapter 10. Maths :: Examples
Submit Status
/* ***********************************************
Author :CKboss
Created Time :2015年02月03日 星期二 22时13分33秒
File Name :UVA10214.cpp
************************************************ */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
using namespace std;
int gcd(int a,int b)
{
return (b==0)?a:gcd(b,a%b);
}
int phi[2200];
int GCD[2020][2020];
void phi_table(int n)
{
phi[1]=1;
for(int i=2;i<=n;i++)
{
if(!phi[i])
{
for(int j=i;j<=n;j+=i)
{
if(!phi[j]) phi[j]=j;
phi[j]=phi[j]*(i-1)/i;
}
}
}
}
void init()
{
phi_table(2010);
for(int i=1;i<=2000;i++)
for(int j=1;j<=2000;j++)
if(GCD[i][j]==0)
GCD[i][j]=GCD[j][i]=gcd(i,j);
}
int a,b;
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
init();
while(scanf("%d%d",&a,&b)!=EOF)
{
if(a==0&&b==0) break;
if(a>b) swap(a,b);
double ret=0;
for(int i=1;i<=a;i++)
{
int duan = b/i;
int yu = b%i;
ret += duan * phi[i];
for(int j=1;j<=yu;j++)
{
if(GCD[j][i]==1)
ret+=1;
}
}
ret = ret*4 + 4;
double all = (2.*a+1)*(2.*b+1)-1.;
double ans = ret/all;
printf("%.7lf\n",ans);
}
return 0;
}
相关文章推荐
- UVA 10214 Trees in a Wood. (欧拉函数)
- UVa10214 - Trees in a Wood.(数论)
- 20140925 【 初等数论--欧拉函数 】 UVa 10214 - Trees in a Wood
- UVA 10214(p339)----Trees in a Wood
- uva10214 Trees in a Wood.
- uva 10214 Trees in a wood 两个区间内的互素数对
- UVA 10214 - Trees in a Wood.
- UVa10214 Trees in a Wood.
- UVA 10214 Trees in a Wood.
- UVA 10214 Trees in a Wood(欧拉函数)
- Trees in a Wood. UVA - 10214 欧拉函数表
- UVa 10214 Trees in a Wood.
- UVA 10214 - Trees in a Wood.
- Trees in a Wood. UVA 10214 欧拉函数或者容斥定理 给定a,b求 |x|<=a, |y|<=b这个范围内的所有整点不包括原点都种一棵树。求出你站在原点向四周看到的树的数量/总的树的数量的值。
- UVa 10214 (莫比乌斯反演 or 欧拉函数) Trees in a Wood.
- Trees in a Wood. UVA - 10214
- UVA 10214 Trees in a Wood
- uva 10562 - Undraw the Trees
- Uva712——S-Trees
- uva_712-S-Trees