UVA 10214 Trees in a Wood. (欧拉函数)

题意:

在满足|x| <= a |y| <= b 的网格中,整点x,y各种着一棵树(扣掉原点),问你从原点能看到多少棵树。

思路:

显然 就是找有几对(x,y) 满足 gcd(x,y) = 1;

因为不等于1的话,肯定在等于1位置的后面,肯定是看不到的。

当 1<= y <= x 有phi(x) 个。 这是欧拉函数的定义。

当x+1 <= y <= 2x 也有phi(x)个 。 这个需要用gcd的 一个性质, 即 gcd(a+mb,b) = gcd(a,b);

依次类推,,

那么我们枚举列显然会更快了。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

typedef long long LL;

const double eps = 1e-10;

int phi[2000000 + 7];

LL gcd(LL a,LL b){
    return !b ? a : gcd(b,a%b);
}

void init(){
    phi[1] = 1;
    for (int i = 2; i < 2000000+7; ++i) if (!phi[i]){
        for (int j = i; j < 2000000 + 7 ; j += i){
            if (!phi[j]) phi[j] = j;
            phi[j] = phi[j] / i * (i-1);
        }
    }
}

int n, m;

int main(){
    init();
    while(~scanf("%d %d",&n, &m) && (n || m)){
        LL ans = 0;
        for (int x = 1; x <= n; ++x){
            int y;
            for (y = x; y <= m; y += x){
                ans += phi[x];
            }
            y-=x;
            if (y < m){
                for (int j = y+1; j <= m; ++j){
                    if (gcd(j,x) == 1)++ans;
                }
            }
        }

        ans*=4;
        ans+=4;
        LL N = (LL)n*(LL)m*4 + 2*n+2*m;

        printf("%.10f\n",(ans*1.0)/N + eps);
    }


    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值