Poj P3090 Visible Lattice Points___欧拉函数求和

题目大意:

在一个平面直角坐标系,[0,0]为左下角、[N,N]为右上角,除了[0,0]以外,其他的坐标上都有一个钉子。
给出C个询问,每个询问给出一个N,问从[0,0]向四周看最多能看到多少个点。

1≤C≤100
1≤N≤1000

分析:

不难发现从[0,0]能看到的
肯定有[0,1],[1,1],[1,0]3个点
其他能看到的点[x,y]必定是满足gcd(x,y) = 1
所以就可以求x的欧拉函数φ(x)
而任意一个询问的N,
answer 显然就等于 3 + 2 i = 2 N φ ( i )

代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#define N 1005

using namespace std;

int phi[N],ans[N];

void init_answer(){
    for (int i = 2; i <= N; i++) phi[i] = i;
    for (int i = 2; i <= N; i++)
         if (phi[i] == i)
             for (int j = i; j <= N; j += i) phi[j] -= phi[j] / i;
    for (int i = 1; i <= N; i++){
         ans[i] = 3;
         for (int j = 2; j <= i; j++) ans[i] += 2 * phi[j];
    }
}

int main(){
    init_answer();
    int T, n, cnt = 0;
    scanf("%d", &T);
    while (T--){
           scanf("%d", &n);
           printf("%d %d %d\n", ++cnt, n, ans[n]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/gx_man_vip/article/details/80273895