[欧拉函数】 POJ 3090 Visible Lattice Points 详解

题目链接:https://vjudge.net/problem/POJ-3090

题目大意:

A lattice point (xy) in the first quadrant (x and y are integers greater than or equal to 0), other than the origin, is visible from the origin if the line from (0, 0) to (xy) does not pass through any other lattice point. For example, the point (4, 2) is not visible since the line from the origin passes through (2, 1). The figure below shows the points (xy) with 0 ≤ xy ≤ 5 with lines from the origin to the visible points.

Write a program which, given a value for the size, N, computes the number of visible points (xy) with 0 ≤ xy ≤ N.

分析:

首先发现假如(a,b)为一个lattice point,则(ka,kb)必然不是lattice point

因为二者在一条直线上

故可得:

坐标中任意一点(a,b),若a,b有共同因子则其不是lattice point,若互质则其为lattice point

特殊点:(0,1)和(1,0)为lattice point

现在让从1开始考虑:

坐标含1的3个点:(0,1)、(1,1)、(1,0)都是,所以ans[1]=3

坐标含2的5个点:(x,2)或(2,x)中只要有一个为lattice point另一个必然也是,发现只有x=1满足条件,所以ans[2]=ans[1]+2=5

坐标含3的7个点:推出x为1或2都可以,所以ans[3]=ans[2]+4=9

综上:

ans[1]=3;

ans[x]=ans[x-1]+φ(x)

该式可用于递推求解,此题得解

其中φ(x)为1~x中与x互质的数,即欧拉函数值;

欧拉函数打表方法可见:欧拉函数打表

代码

#include <cstdio>
#include <string.h>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;

int phi[1005];
int ans[1005];

void Euler_excel(){
  phi[1]=1;
  for(int i=2;i<=1000;i++) phi[i]=0;
  for(int i=2;i<=1000;i++){
     if(!phi[i]){
          for(int j=i;j<=1000;j+=i){
            if(!phi[j])  phi[j]=j;
            phi[j]=phi[j]/i*(i-1);
          }
     }
  }
}

void pre_sum(){
   ans[1]=3;
   for(int i=2;i<=1000;i++)
     ans[i]=2*phi[i]+ans[i-1];
}

int main()
{
  Euler_excel();
  pre_sum();
  int C;
  scanf("%d",&C);
  for(int i=1;i<=C;i++){
     int N;
     scanf("%d",&N);
     printf("%d %d %d\n",i,N,ans[N]);
  }
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值