[欧拉函数]Visible Lattice Points POJ3090

16 篇文章 0 订阅

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

InputOutput
4
2
4
5
231
1 2 5
2 4 13
3 5 21
4 231 32549

 

题意: 输入n表示矩阵大小,求从(0,0)能看到的点个数。

分析: 有一个很显然的事实是若两点连线上没有其他点,则两点构成向量的横纵坐标互质,这条结论在Pick定理总结中求边上格点个数时也用到了。接下来围绕这点可以有多种解法。

O(n^2)解法 

这个就非常暴力了,直接枚举所有的点,然后判断x坐标和y坐标是否互质,互质就答案+1。显然这种方法会TLE。

O(n^1.5)解法

刚才是求了x、y任取时gcd(x, y) == 1的点数,也可以在x <= y的条件下求gcd(x, y) == 1的点数,之后再乘2就行,如果固定y的值,则gcd(x, y) == 1的点数就是y的欧拉函数值,可以用质因数分解求出欧拉函数值,其时间复杂度为O(n^0.5),综合起来就是O(n^1.5)的复杂度。

O(n)解法

有了上面的思路,优化到O(n)是相当容易的了,直接用线性筛筛出1~1000的欧拉函数值。

O(1)解法

观察一下这道题的特点:多组数据、答案个数较少、暴力好写。妥妥的打表呀,可以说是打表中的模板题了,于是用O(n^2)暴力打出答案,并将答案存放在一个长度为1000的数组里,每次直接输出ans[n]。

具体代码如下:

O(n^2)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
using namespace std;

signed main()
{
	int T;
	cin >> T;
	for(int _ = 1; _ <= T; _++)
	{
		int n, ans = 2;
		scanf("%d", &n);
		for(int i = 1; i <= n; i++)
			for(int j = 1; j <= n; j++)
				if(__gcd(i, j) == 1)
					ans++;
		printf("%d %d %d\n", _, n, ans);
	} 
    return 0;
}

O(n^1.5)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
using namespace std;

int phi(int x)
{
	int ans = x;
	for(int i = 2; i*i <= x; i++)
	{
		if(x%i == 0)
		{
			ans = ans/i*(i-1);
			while(x%i == 0)
				x /= i;
		}
	}
	if(x > 1) ans = ans/x*(x-1);
	return ans;
}

signed main()
{
	int T;
	cin >> T;
	for(int _ = 1; _ <= T; _++)
	{
		int n, ans = 0;
		scanf("%d", &n);
		for(int i = 1; i <= n; i++)
			ans += phi(i);
		ans = (ans-1)*2+3;
		printf("%d %d %d\n", _, n, ans);
	} 
    return 0;
}

O(n)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
using namespace std;

int is_prime[1005], prime[1005], phi[1005], cnt;

void get_phi()
{
	memset(is_prime, -1, sizeof(is_prime));
	phi[1] = 1;
	for(int i = 2; i <= 1000; i++)
	{
		if(is_prime[i])
		{
			prime[++cnt] = i;
			phi[i] = i - 1;
		}
		for(int j = 1; j <= cnt && i*prime[j] <= 1000; j++)
		{
			is_prime[i*prime[j]] = 0;
			if(i % prime[j] == 0)
			{
				phi[i*prime[j]] = phi[i] * prime[j];
				break;
			}
			else phi[i*prime[j]] = phi[i] * phi[prime[j]];
		}
	}	
}

signed main()
{
	get_phi();
	int T;
	cin >> T;
	for(int _ = 1; _ <= T; _++)
	{
		int n, ans = 0;
		scanf("%d", &n);
		for(int i = 1; i <= n; i++)
			ans += phi[i];
		ans = (ans-1)*2+3;
		printf("%d %d %d\n", _, n, ans);
	} 
    return 0;
}

O(1)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
using namespace std;

int ans[1005] = {0,3,5,9,13,21,25,37,45,57,65,85,93,117,129,145,161,193,205,241,257,281,301,345,361,401,425,461,485,541,557,617,649,689,721,769,793,865,901,949,981,1061,1085,1169,1209,1257,1301,1393,1425,1509,1549,1613,1661,1765,1801,1881,1929,2001,2057,2173,2205,2325,2385,2457,2521,2617,2657,2789,2853,2941,2989,3129,3177,3321,3393,3473,3545,3665,3713,3869,3933,4041,4121,4285,4333,4461,4545,4657,4737,4913,4961,5105,5193,5313,5405,5549,5613,5805,5889,6009,6089,6289,6353,6557,6653,6749,6853,7065,7137,7353,7433,7577,7673,7897,7969,8145,8257,8401,8517,8709,8773,8993,9113,9273,9393,9593,9665,9917,10045,10213,10309,10569,10649,10865,10997,11141,11269,11541,11629,11905,12001,12185,12325,12565,12661,12885,13029,13197,13341,13637,13717,14017,14161,14353,14473,14713,14809,15121,15277,15485,15613,15877,15985,16309,16469,16629,16793,17125,17221,17533,17661,17877,18045,18389,18501,18741,18901,19133,19309,19665,19761,20121,20265,20505,20681,20969,21089,21409,21593,21809,21953,22333,22461,22845,23037,23229,23397,23789,23909,24305,24465,24729,24929,25265,25393,25713,25917,26181,26373,26733,26829,27249,27457,27737,27949,28285,28429,28789,29005,29293,29453,29837,29981,30425,30617,30857,31081,31533,31677,32133,32309,32549,32773,33237,33381,33749,33981,34293,34485,34961,35089,35569,35789,36113,36353,36689,36849,37281,37521,37849,38049,38549,38693,39133,39385,39641,39897,40409,40577,41009,41201,41537,41797,42321,42481,42897,43113,43465,43729,44265,44409,44949,45205,45493,45765,46165,46341,46893,47169,47529,47721,48281,48465,49029,49309,49597,49837,50317,50509,51053,51277,51661,51949,52533,52701,53165,53453,53813,54109,54637,54797,55301,55601,56001,56289,56769,56961,57573,57813,58221,58461,59081,59273,59897,60209,60497,60809,61441,61649,62209,62465,62889,63153,63729,63945,64425,64749,65181,65501,66053,66213,66873,67201,67633,67965,68493,68685,69357,69669,70117,70373,70973,71189,71777,72113,72465,72809,73501,73725,74421,74661,75093,75413,76117,76349,76909,77261,77645,78001,78717,78909,79593,79953,80393,80681,81257,81497,82229,82581,83061,83349,83973,84213,84957,85277,85677,86045,86717,86933,87689,87977,88481,88861,89625,89881,90361,90745,91249,91633,92409,92601,93305,93641,94161,94553,95177,95417,96209,96605,97037,97357,98157,98421,99141,99541,99973,100309,101029,101285,102101,102421,102965,103373,104069,104333,104989,105373,105925,106285,107121,107313,108153,108573,109125,109541,110181,110461,111181,111605,112085,112421,113281,113569,114433,114793,115241,115673,116465,116753,117629,117949,118453,118837,119721,120009,120713,121157,121749,122133,123029,123269,124069,124517,125117,125569,126145,126433,127345,127801,128377,128729,129649,129889,130813,131261,131741,132205,133137,133425,134217,134585,135209,135673,136513,136825,137545,137929,138553,139029,139985,140241,141105,141585,142113,142553,143321,143645,144617,145097,145745,146081,147061,147381,148277,148709,149189,149669,150509,150837,151833,152233,152897,153397,154401,154689,155489,155929,156553,157057,158073,158329,159193,159705,160353,160865,161681,162017,162937,163369,164057,164441,165481,165817,166861,167381,167861,168385,169345,169665,170677,171093,171789,172221,173181,173533,174381,174909,175621,176157,176997,177285,178365,178905,179625,180137,181001,181289,182381,182925,183645,184045,185053,185405,186341,186893,187469,188021,189133,189493,190501,190885,191525,192085,193209,193577,194473,195037,195685,196245,197381,197669,198809,199289,200049,200529,201409,201793,202945,203489,204257,204705,205689,206073,207113,207689,208265,208849,210021,210357,211437,211901,212685,213261,214445,214805,215573,216165,216957,217485,218681,219001,220201,220705,221497,222097,222977,223377,224589,225165,225837,226317,227421,227805,229029,229641,230281,230761,231993,232401,233637,234117,234909,235529,236585,236969,237969,238593,239313,239937,241089,241377,242637,243261,244101,244733,245741,246157,247165,247725,248565,249077,250357,250781,252065,252593,253265,253841,255133,255565,256725,257205,257925,258573,259877,260309,261349,261989,262853,263405,264721,265041,266361,267021,267789,268445,269309,269741,270973,271637,272525,273053,274253,274637,275981,276653,277373,277997,279349,279797,280949,281461,282365,282965,284329,284761,285849,286437,287349,288021,289269,289621,291001,291689,292409,293101,294205,294653,295933,296629,297557,298037,299437,299869,301165,301805,302541,303245,304445,304909,306325,306885,307821,308525,309845,310229,311189,311901,312853,313569,315005,315389,316613,317297,318257,318977,320097,320537,321989,322565,323537,324113,325457,325937,327401,328133,328805,329509,330829,331309,332785,333361,334225,334849,336333,336813,337997,338741,339725,340365,341637,342037,343537,344273,345273,345945,347145,347577,349089,349845,350725,351301,352821,353325,354621,355381,356149,356913,358305,358817,360353,360833,361857,362625,364169,364673,365873,366641,367505,368281,369721,370105,371505,372209,373217,373889,375137,375657,377229,378013,379061,379685,381029,381509,382949,383741,384573,385365,386957,387389,388861,389501,390557,391357,392797,393325,394381,395101,396173,396973,398589,399021,400641,401313,402393,403113,404409,404921,406433,407249,408113,408753,410393,410937,412581,413397,414197,414893,416545,417073,418729,419385,420489,421257,422601,423153,424481,425201,426281,427117,428793,429177,430801,431641,432761,433601,434849,435401,436721,437553,438681,439321,440905,441465,443169,443889,444753,445601,447313,447793,449509,450181,451141,452001,453725,454301,455677,456541,457629,458349,459909,460357,461941,462805,463957,464749,465949,466525,468277,469153,470321,470961,472721,473225,474989,475757,476685,477569,479341,479917,481429,482133,483213,484101,485757,486349,487773,488541,489597,490493,492173,492653,494317,495117,496125,497021,498461,499061,500873,501777,502977,503553,505373,505949,507589,508501,509461,510373,511933,512509,514345,515049,516273,517193,518873,519353,520793,521717,522941,523837,525693,526173,527685,528613,529853,530785,532065,532641,534513,535305,536553,537289,539169,539793,541553,542481,543345,544185,546077,546701,548429,549149,550413,551181,553085,553709,555229,556181,557301,558257,559889,560401,562261,563125,564397,565357,566893,567421,569353,570233,571385,572153,574093,574741,576397,577369,578329,579289,581241,581889,583649,584321,585617,586597,588561,589201,590769,591665,592769,593633,595481,595961,597941,598901,600221,601061,602645,603301,605293,606289,607585,608385};

signed main()
{
	int T;
	cin >> T;
	for(int _ = 1; _ <= T; _++)
	{
		int n;
		scanf("%d", &n);
		printf("%d %d %d\n", _, n, ans[n]);
	}
    return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值