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 }
  • 相关阅读:
    Solr4:加入中文分词IKAnalyzer2012 FF
    Solr4:Hello World级别示范
    Solr4:Tomcat7下面配置Solr
    Java:XML篇,使用DOM读取并解析XML
    Solr4:加入中文分词mmseg4j
    Java:XML篇,使用SAX读取并解析XML数据
    SSIS包如何动态指定文件路径
    如何定时执行SSIS包
    用户控件使用事件
    用户控件使用事件与调用页面交互
  • 原文地址:https://www.cnblogs.com/CSU3901130321/p/3863413.html
Copyright © 2011-2022 走看看