zoukankan      html  css  js  c++  java
  • POJ 3090

    Visible Lattice Points
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 5356   Accepted: 3136

    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

     

    先画个图发现那些可见得点关于y=x对称

    下三角中

    并且除去x=0

    每一行的可见点x,y互质

    个数为euler_phi(x);

     1 #include <iostream>
     2 #include <cmath>
     3 using namespace std;
     4 
     5 int euler_phi(int n)
     6 {
     7     int m=(int)sqrt(n+0.5);
     8     int ans=n;
     9     for(int i=2;i<=m;i++)
    10         if(n%i==0)
    11         {
    12             ans=ans/i*(i-1);
    13             while(n%i==0)n/=i;
    14         }
    15     if(n>1)ans=ans/n*(n-1);
    16     return ans;
    17 }
    18 
    19 int main()
    20 {
    21     int C;
    22     cin>>C;
    23     for(int c =1;c<=C;c++)
    24     {
    25         int N;
    26         cin>>N;
    27         int ans = 0;
    28        for(int i =2;i<=N;i++)
    29        {
    30            ans+=euler_phi(i);
    31        }
    32         ans = ans*2 +3;
    33         cout<<c<<" "<<N<<" "<<ans<<endl;
    34 
    35     }
    36 }
  • 相关阅读:
    235. Lowest Common Ancestor of a Binary Search Tree
    112. Path Sum
    111. Minimum Depth of Binary Tree
    110. Balanced Binary Tree
    108. Convert Sorted Array to Binary Search Tree
    win7+iis7.5+asp.net 安装IIS 遇到的一些问题
    创建一个简单的WCF程序
    ROW_NUMBER() OVER函数的基本用法
    input type="file"
    Nightmare
  • 原文地址:https://www.cnblogs.com/Run-dream/p/3863247.html
Copyright © 2011-2022 走看看