zoukankan      html  css  js  c++  java
  • POJ_3090 Visible Lattice Points 【欧拉函数 + 递推】

    一、题目

    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.

    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 5
    2 4 13
    3 5 21
    4 231 32549

    二、题意分析

    这题题意比较好懂,给定一个数N,代表这个二维平面里在第一象限的一个正方形的边长,就可以得到(N+1)*(N+1)个整点。然后问在除原点的外的(N+1)^2-1个点中,有多少个点与原点相连后,两点连成的线段之间是木有整点的。

    补充个营养:看过《挑战程序设计竞赛》的同学肯定知道,给定一个直角三角形的两条边的边长a,b,那么gcd(a,b)+1就代表这条边上的整点数目。除去两个端点那么gcd(a,b)-1=0不就是这题所要满足的吗,也就是gcd(a,b)=1。

    转换:这题通过上面的知识就可以转换成求一个数N的欧拉函数值。然后我们分析一下,N=1的时候一共4个点中,除去原点有3个点满足,结果为F[1] = 3。N=2的时候一共9个点,N=1时满足的点在此时也必然满足。这里需要注意的是,因为是在二维平面,那么最外面一条边上有φ(2)个点满足的话,那么另外一条边上也有φ(2)个点满足,那么就是 F[1] + 2*φ(2)。后面的原理相同,就得到了递推式

    F[N] = F[N-1] + 2*φ(N),其中F[1] = 3

    然后先线性筛法打表求欧拉函数的值,再用另外一个数组递推即可。

    三、代码

    #include <iostream>
    #include <cstring>
    using namespace std;
    const int MAXN = 1e3+5;
    int Prime[MAXN], Phi[MAXN], nPrime;
    long long Ans[MAXN];
    
    void Euler()
    {
        memset(Phi, 0, sizeof(Phi));
        Phi[1] = 1;
        nPrime = 0;
        for(int i = 2; i < MAXN; i++)
        {
            if(!Phi[i])
            {
                Phi[i] = i-1;
                Prime[nPrime++] = i;
            }
            for(int j = 0; j < nPrime && i*Prime[j] < MAXN; j++)
            {
                if(i%Prime[j])
                {
                    Phi[i*Prime[j]] = Phi[i]*(Prime[j] - 1);
                }
                else
                {
                    Phi[i*Prime[j]] = Phi[i]*Prime[j];
                    break;
                }
            }
        }
    }
    
    void solve()
    {
        Euler();
        Ans[1] = 3;
        for(int i = 2; i < MAXN; i++)
        {
            Ans[i] = Ans[i-1] + Phi[i]*2;
        }
    }
    
    int main()
    {
        int T, N;
        cin >> T;
        solve();
        for(int i = 1; i <= T; i++)
        {
            cin >> N;
            cout << i << ' ' << N << ' ' << Ans[N] << endl;
        }
        return 0;
    }
    

      

  • 相关阅读:
    gerrit权限控制
    kvm虚拟机根目录磁盘扩容
    vim新手指南
    精通 vim 你应该理解的几个名词
    精通 VIM ,此文就够了
    linux下库的使用--动态库
    linux下库的使用--静态库
    linux下程序编译的各个阶段记录
    ASCII码表
    重构的过程记录--之利用系统数据库:
  • 原文地址:https://www.cnblogs.com/dybala21/p/9747807.html
Copyright © 2011-2022 走看看