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 }
  • 相关阅读:
    【蜕变之路】第20天 UUID和时间戳的生成 (2019年3月10日)
    3.EntityFramework的多种记录日志方式,记录错误并分析执行时间过长原因(系列4)
    reactnative资源
    代码
    模板匹配模型、原型模型和区别性特征模型各自如何解释汉字的知觉
    mysqldatadir 转移
    mysql主从设置windows
    心灵鸡汤
    测试的发现遗漏BUG的做法
    汉字模式匹配的过程
  • 原文地址:https://www.cnblogs.com/Run-dream/p/3863247.html
Copyright © 2011-2022 走看看