zoukankan      html  css  js  c++  java
  • POJ3090(欧拉函数)

    Visible Lattice Points
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 6681   Accepted: 4000

    Description

    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

    思路:欧拉函数打表。
    #include <cstdio>
    using namespace std;
    const int MAXN=1005;
    int co_prime[MAXN];
    void sieve()
    {
        co_prime[1]=3;
        for(int i=2;i<MAXN;i++)    co_prime[i]=i;
        for(int i=2;i<MAXN;i+=2)    co_prime[i]/=2;
        for(int i=3;i<MAXN;i+=2)
        {
            if(co_prime[i]==i)
            {
                for(int j=i;j<MAXN;j+=i)
                {
                    co_prime[j]=co_prime[j]*(i-1)/i;
                }
            }
        }
        for(int i=2;i<MAXN;i++)
        {
            co_prime[i]*=2;
            co_prime[i]+=co_prime[i-1];
        }
    }
    int main()
    {
        sieve();
        int T;
        scanf("%d",&T);
        for(int cas=1;cas<=T;cas++)
        {
            int n;
            scanf("%d",&n);
            printf("%d %d %d
    ",cas,n,co_prime[n]);
        }
        
        return 0;
    }

     Java:

    import java.util.Scanner;
    
    public class Main{
        Scanner in = new Scanner(System.in);
        final int MAXN = 1005;
        int[] co_prime = new int[MAXN];
        int euler(int n)
        {
            int ret = n;
            for(int i = 2; i * i <= n; i++)
            {
                if(n % i == 0)
                {
                    ret = ret - ret / i;
                    while(n % i == 0)    n /= i;
                }
            }
            if(n > 1)    ret = ret - ret / n;
            return ret;
        }
        void sieve()
        {
            co_prime[1] = 3;
            for(int i = 2; i < MAXN; i++)
            {
                int ret = euler(i);
                co_prime[i] = (co_prime[i-1] + ret * 2);
            }
        }
        Main()
        {
            sieve();
            int T = in.nextInt();
            for(int cas = 1; cas <= T; cas++)
            {
                int n = in.nextInt();
                System.out.println(cas +" " + n +" " + co_prime[n]);
            }
        }
        public static void main(String[] args){
            
            new Main();
        }
    }
  • 相关阅读:
    将一张图片的人物融入另一张图片中
    填充---内容识别图片
    使用蒙版--渐变--制作瓶子倒影
    form表单基础知识
    表格排版及其表格嵌套
    HTML表格,table,thead,tbody,tfoot,th,tr,td,的属性以及跨行,跨列
    垃圾收集,内存问题
    JS预解析机制
    python ==》 内置函数
    python ==》 递归
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5849694.html
Copyright © 2011-2022 走看看