POJ3090 Visible Lattice Points(欧拉函数)

1人阅读 评论(0) 收藏 举报
分类:

欧拉函数

欧拉函数的定义:1~N中与N互质的数的个数被称为欧拉函数,我们用phi[i]来记录与i互质的数的个数。

根据算数基本定理(唯一分解定理),即N可以分解成N=p1^c1*p2^c2*…pm^cm,其中pi为质数可以推出phi[N]=N * (p1 - 1) / p1 * (p2 - 1) / p2*…(pm - 1) / pm。

欧拉函数的部分性质:phi[n] = phi[n / p] * (n % (p ^ 2) ? p - 1 : p)(其中n % p == 0)。其他性质请自行百度……

下面来看一个例题,题目链接:http://poj.org/problem?id=3090

Description

A lattice point (x, y) 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 (x, y) 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 (x, y) with 0 ≤ x, y ≤ 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 (x, y) with 0 ≤ x, yN.

Input

The first line of input contains a single integer C (1 ≤ C ≤ 1000) which is the number of datasets that follow.

Each dataset consists of a single line of input containing a single integer N (1 ≤ N ≤ 1000), which is the size.

Output

For each dataset, there is to be one line of output consisting of: the dataset number starting at 1, a single space, the size, a single space and the number of visible points for that size.

Sample Input

4
2
4
5
231

Sample Output

1 2 52 4 133 5 214 231 32549

思路:易知在每条直线上只能看到一个点,由过原点的直线的斜率公式y/x知,能看到的点的坐标为横纵坐标互质的点。从而将问题转换为求欧拉函数,通过作图(经验)知,能看到的点关于y = x 对称,因而本题要求的就是结果为3 + 2 * phi[i](i:1~n),其中的3为(1,0),(0,1),(0,0)。在进行线性筛的同时利用上面说的欧拉函数的性质将每个数的欧拉函数求出来。
代码实现如下:

#include <cstdio>
#include <cstring>

int c, n, m;
int v[1007], p[1007];
long long phi[1007], ans;

void euler(int n) {
    memset(v, 0, sizeof(v));
    m = 0;
    for(int i = 2; i <= n; i++) {
        if(v[i] == 0) {
            v[i] = i;
            p[m++] = i;
            phi[i] = i - 1;
        }
        for(int j = 0; j < m; j++) {
            if(p[j] > v[i] || p[j] > n / i) break;
            v[i * p[j]] = p[j];
            phi[i * p[j]] = phi[i] * (i % p[j] ? p[j] - 1 : p[j]);
        }
    }
}

int main() {
    scanf("%d", &c);
    for(int icase = 1; icase <= c; icase++) {
        scanf("%d", &n);
        euler(n);
        ans = 0;
        for(int i =2; i <= n; i++) {
            ans += phi[i];
        }
        printf("%d %d %lld\n", icase, n, ans * 2 + 3);
    }
}

查看评论

poj3090-Visible Lattice Points

http://poj.org/problem?id=3090 法雷级数,,欧拉函数 #include #include #include using namespace std; #de...
  • liujie619406439
  • liujie619406439
  • 2013-06-07 18:43:11
  • 793

Visible Lattice Points(Poj3090)(欧拉函数运用)

Visible Lattice Points Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: ...
  • hdd871532887
  • hdd871532887
  • 2015-12-18 12:36:20
  • 276

POJ 3090 Visible Lattice Points

Description A lattice point (x, y) in the first quadrant (x and y are integers greater than or ...
  • u012313382
  • u012313382
  • 2014-08-16 19:59:04
  • 812

Visible Lattice Points(POJ3090)

【题目描述】 A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to ...
  • qq_31640513
  • qq_31640513
  • 2017-02-25 17:19:31
  • 217

POJ3090 Visible Lattice Points

题目链接:http://poj.org/problem?id=3090   和一道叫兔八哥什么的题是差不多的   涉及到网格图上的一个问题:判断(x1,y1)和(x2,y2)的连线上有没有其它格点  ...
  • wry0112
  • wry0112
  • 2017-07-08 15:14:15
  • 60

Visible Lattice Points(poj3090)

题目: Description A lattice point (x, y) in the first quadrant (x and y are integers greater tha...
  • csu__zhouyi
  • csu__zhouyi
  • 2014-07-25 18:06:16
  • 175

poj3090 Visible Lattice Points

poj题解
  • mk703
  • mk703
  • 2017-02-20 18:20:52
  • 139

SPOJ 7001 Visible Lattice Points (数论关于gcd,超经典极力推荐-莫比乌斯反演)

传送门:http://www.spoj.com/problems/VLATTICE/SPOJ Problem Set (classical)7001. Visible Lattice PointsPr...
  • ilovexiaohao
  • ilovexiaohao
  • 2013-10-06 15:51:58
  • 2759

[POJ3090]Visible Lattice Points

原题链接线性筛欧拉函数#include #include #include #include #include #include #include #include #include #include...
  • qq_36920028
  • qq_36920028
  • 2017-05-28 08:13:50
  • 81

poj3090 Visible Lattice Points 欧拉函数的应用

从点(0,0)到点(x,y)画线段不经过其它点,意思就是x,y互素,由此把问题转化为求1到n内互素的数的对数,当然不要漏了(1,1)(1,0)(0,1)这三个点,接下来就是求1到n的欧拉函数值来解决这...
  • u010682557
  • u010682557
  • 2013-12-16 16:16:19
  • 706
    个人资料
    等级:
    访问量: 1145
    积分: 213
    排名: 33万+
    文章存档