P2158,jzoj1709-仪仗队【欧拉函数,数论】

正题

评测记录:https://www.luogu.org/recordnew/lists?uid=52918&pid=P2158


大意

n n 个点,求从 ( 1 , 1 ) 可以看到多少个点。
这里写图片描述


解题思路

我们将 ( 1 , 1 ) 当做 ( 0 , 0 ) ,然后所有点往下和后移一步
我们可以发现点距离点 ( 0 , 0 ) 只要有中点那么就不会被看到,而两个点之间有中点仅当 x 1 x 2 y 1 y 2 都是偶数,而那么我们可以发现 ( x , y ) ( 0 , 0 ) 之间有中点仅当 x y 互质(因为如果有因数那么一定会被 ( x / g c d ( x , y ) , y / g c d ( x , y ) ) 挡住)。那么我们就可以用欧拉函数来算互质。当然我们还要算上离 ( 0 , 0 ) 最近的三个,所以答案就是:

3 + i = 2 n φ ( i )


代码

#include<cstdio>
using namespace std;
int phi[40001],sum,n;
int main()
{
    scanf("%d",&n);
    if (n==1)
    {
        printf("0");
        return 0;
    }
    n--;
    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*(i-1);//筛去一个质因子
      sum+=phi[i];//统计答案
    }
    printf("%d",3+2*sum);
}

猜你喜欢

转载自blog.csdn.net/mr_wuyongcong/article/details/81071889