程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> POJ 3090 Visible Lattice Points

POJ 3090 Visible Lattice Points

編輯:C++入門知識

POJ 3090 Visible Lattice Points


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<喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vc3Ryb25nPjwvZW0+LCA8ZW0+PHN0cm9uZz55PC9zdHJvbmc+PC9lbT4pIHdpdGggMCCh3CA8ZW0+PHN0cm9uZz54PC9zdHJvbmc+PC9lbT4sIDxlbT48c3Ryb25nPnk8L3N0cm9uZz48L2VtPiCh3CA8ZW0+PHN0cm9uZz5OPC9zdHJvbmc+PC9lbT4uPC9wPgoKPHAgY2xhc3M9"pst">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

思路:實際上題目在說的時候已經為你提供了思路,那就是比較斜率了。只要是斜率沒有出現的點,那麼我們就可以加上這些點,我們發現,每多一個數,就意味著要多一個外層,我們只需要加上這些外層上的點就可以了,而且一個外層上的點是對稱的!
所以AC代碼:
#include
#include
#include
using namespace std;
int a[1005][1005];  //用數組的標號代表x,y坐標,用數組的值表示斜率是否出現過
int b[1005];        //記錄有多少個點
int gcd(int a,int b)
{
    if(a

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved