码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 3090 Visible Lattice Points 布雷级数

时间:2015-03-21 08:38:16      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:poj   数论   

Visible Lattice Points
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5779   Accepted: 3409

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

Source

先说一下格点的定义:在坐标x,y都是整数的点就是格点。
然后给出一个结论:两个点P1(x1,y1),P2(x2,y2)的连线上(不含P1,P2)格点的个数是:|x1-x2|和|y1-y2|的最大公约数-1.(见《挑战程序设计竞赛 第2版》)
这个题是都是点到(0,0)的连线,也就是求在N*N(第一象限)内有多少个点(x,y)到原点的连线上没有格点,那么就是gcd(x,y)=1,其实求多少对x,y互素。
那么问题就很明显了,设phi[j]表示1~j中与j互素的数的个数。    在N*N的点阵中,由对称性,我们只看原点和(N,N)的连线的下面的三角区域(由(0,0),(N,0)和(N,N)组成),要求当x=2时,有几个满足题意的格点;当x=3时,有几个满足;x=4时,,x=5,,,,x=N时有几个满足,然后求和phi[2]+phi[3]+phi[4]+……+phi[N]就是要求的答案(x=1时特殊处理一下)。
别忘了我们只处理下三角,还有上三角,由对称性,直接乘以2就好了。

phi[]    素数筛法
phi[1]+...+phip[N]  布雷级数

(当x=1时有三个满足,(1,0),(1,1,),(0,1)满足,特殊处理一下就好了)

#include <stdio.h>
#include <string.h>

const int maxn=1000+10;
int phi[maxn];

void phi_table(){
	int i,j;
	memset(phi,0,sizeof(phi));
	phi[1]=1;
	for(i=2;i<=maxn;i++)
		if(!phi[i])
			for(j=i;j<=maxn;j+=i){
				if(!phi[j]) phi[j]=j;
				phi[j]=phi[j]/i*(i-1);
			}
}

int s[maxn];

//布雷级数
void cal(){
	int i;
	s[1]=0;
	s[2]=phi[2];
	for(i=3;i<=maxn;i++)
		s[i]=s[i-1]+phi[i];
}

int main()
{
	int i,T,kcase=1,n;
	phi_table();
	cal();
	scanf("%d",&T);
	while(T--){
		scanf("%d",&n);
		printf("%d %d %d\n",kcase++,n,s[n]*2+3);
	}
	return 0;
}





POJ 3090 Visible Lattice Points 布雷级数

标签:poj   数论   

原文地址:http://blog.csdn.net/u013068502/article/details/44499461

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!