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();
        }
    }
  • 相关阅读:
    h5-news_index
    h5-爆料view
    h5-列表
    h5-注册
    h5-登录
    h5-弹出层layer,提示,顶部横条,
    jquery 弹窗插件 layer
    jQuery幻灯片插件Owl Carousel
    display:block jquery.sort()
    Android 开源框架ViewPageIndicator 和 ViewPager 仿网易新闻客户端Tab标签
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5849694.html
Copyright © 2011-2022 走看看