Trees in a Wood. UVA - 10214 欧拉函数表
2017-08-06 10:02
691 查看
紫书p339
枚举a,每个的贡献为b/i*phi[i],因为gcd(kx+y,x)=gcd(x,y),因为gcd(x,y)=gcd(y,x%y)
#include <iostream>
#include <cstdio>
#include <ctime>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>
#include <set>
#include <queue>
using namespace std;
typedef unsigned long long ll;
const int INF=1e9+100;
const int mod=1e9+7;
int phi[2005];
void phi_table(int n){
memset(phi,0,sizeof(phi));
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*(i-1);
}
}
}
}
ll gcd(ll x,ll y){
return y?gcd(y,x%y):x;
}
int main(){
//freopen("out.txt","w",stdout);
int a,b;
phi_table(2001);
while(scanf("%d %d",&a,&b)&&a+b){
ll ans=0;
for(int i=1;i<=a;i++){
int t=b/i;
ans+=1LL*t*phi[i];
for(int j=t*i+1;j<=b;j++){
if(gcd(j,i)==1) ans++;
}
}
ans=1LL*ans*4+4;
double ans1=1.0*ans/((1LL*2*a+1)*(2*b+1)-1);
printf("%.7f\n",ans1 );
}
return 0;
}
枚举a,每个的贡献为b/i*phi[i],因为gcd(kx+y,x)=gcd(x,y),因为gcd(x,y)=gcd(y,x%y)
#include <iostream>
#include <cstdio>
#include <ctime>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
#include <cmath>
#include <set>
#include <queue>
using namespace std;
typedef unsigned long long ll;
const int INF=1e9+100;
const int mod=1e9+7;
int phi[2005];
void phi_table(int n){
memset(phi,0,sizeof(phi));
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*(i-1);
}
}
}
}
ll gcd(ll x,ll y){
return y?gcd(y,x%y):x;
}
int main(){
//freopen("out.txt","w",stdout);
int a,b;
phi_table(2001);
while(scanf("%d %d",&a,&b)&&a+b){
ll ans=0;
for(int i=1;i<=a;i++){
int t=b/i;
ans+=1LL*t*phi[i];
for(int j=t*i+1;j<=b;j++){
if(gcd(j,i)==1) ans++;
}
}
ans=1LL*ans*4+4;
double ans1=1.0*ans/((1LL*2*a+1)*(2*b+1)-1);
printf("%.7f\n",ans1 );
}
return 0;
}
相关文章推荐
- Trees in a Wood. UVA 10214 欧拉函数或者容斥定理 给定a,b求 |x|<=a, |y|<=b这个范围内的所有整点不包括原点都种一棵树。求出你站在原点向四周看到的树的数量/总的树的数量的值。
- UVa 10214 (莫比乌斯反演 or 欧拉函数) Trees in a Wood.
- 20140925 【 初等数论--欧拉函数 】 UVa 10214 - Trees in a Wood
- UVa 10214 Trees in a Wood.
- UVA 10214 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.
- UVa10214 - Trees in a Wood.(数论)
- UVA 10214 - Trees in a Wood.
- UVA 10214(p339)----Trees in a Wood
- UVA10214 Trees in a Wood. 欧拉phi函数
- uva10214 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. (数论-欧拉函数)
- UvaLive 6600 Spanning trees in a secure lock pattern 矩阵行列式
- UVA Tress in a Wood