zoukankan      html  css  js  c++  java
  • POJ 3090 坐标系上的视线遮蔽问题

    Description

    A lattice point (x, y) 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 (x, y) 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 (x, y) with 0 ≤ x, y ≤ 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 (x, y) with 0 ≤ x, yN.

    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


    这题目求的是视线所及未挡住的点
    因为如(4,2)这样的点,gcd(4,2)=2!=1,有(2,1)将其挡住了。所以这题变向再求下x,y互质的点
    可以从斜对角线对半分开,算一半最后乘2即可

    代码如下:
     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <cstring>
     4 using namespace std;
     5 #define LL long long
     6 #define N 1010
     7 int phi[1020];
     8 int F[1020];
     9 
    10 void get_phi()
    11 {
    12     memset(phi,0,sizeof(phi));
    13     phi[1]=1;
    14     for(int i=2;i<=N;i++)
    15     {
    16         if(!phi[i])
    17             for(int j=i;j<=N;j+=i)
    18             {
    19                 if(!phi[j]) phi[j]=j;
    20                 phi[j]=phi[j]*(i-1)/i;
    21             }
    22     }
    23 }
    24 
    25 int main()
    26 {
    27     F[1]=1;
    28     get_phi();
    29     for(int i=2;i<=N;i++) F[i]=F[i-1]+phi[i];
    30     int n,T;
    31     cin>>T;
    32     for(int i=1;i<=T;i++){
    33         cin>>n;
    34         cout<<i<<' '<<n<<' '<<F[n]*2+1<<endl;
    35     }
    36     return 0;
    37 }
  • 相关阅读:
    Solaris 11 让 ls 的输出 带上颜色
    Solaris11 Vsphere client tool 的鼠标问题
    VNC Viewer连接Cent OS 时的 复制粘帖 功能
    vSphere Client的拷贝 粘帖 功能
    更改CentOS7登录画面的分辨率
    一个故意消耗内存的java程序MemoryEater
    安装CentOS7文字界面版后,无法联网,用yum安装软件提示 cannot find a valid baseurl for repo:base/7/x86_64 的解决方法
    windows 安装 Redis
    centos 安装postgresql 完整版
    centos 7 官网安装 PostgreSQL
  • 原文地址:https://www.cnblogs.com/CSU3901130321/p/3863413.html
Copyright © 2011-2022 走看看