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();
        }
    }
  • 相关阅读:
    [网络] 第五章 运输层
    pubwin
    R语言
    我的微博开通啦
    迅雷使用积分制的真正作用和目的 [揭密迅雷]
    求数组里面第二大的数
    xp环境下安装vmware workstation 7出错
    一道大学c语言作业题
    写在断网的那些日子里
    mysql里奇怪的日期201627 14:28:15
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5849694.html
Copyright © 2011-2022 走看看