| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1467 人关注过本帖
标题:[求助]一道acm的题!
只看楼主 加入收藏
zhanghuan_10
Rank: 1
等 级:新手上路
威 望:2
帖 子:751
专家分:0
注 册:2006-10-25
收藏
 问题点数:0 回复次数:25 
[求助]一道acm的题!

这道题总是超时!好不容易 不超时了!当数在小的范围内是对的!但数字一达就错了!
Visible Lattice Points--------------------------------------------------------------------------------

Time limit: 1sec. Submitted: 73
Memory limit: 64M Accepted: 37

Source : Greater NewYork 2006

--------------------------------------------------------------------------------

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, y <= 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

图片附件: 游客没有浏览图片的权限,请 登录注册

搜索更多相关主题的帖子: acm 
2006-12-09 16:15
zhanghuan_10
Rank: 1
等 级:新手上路
威 望:2
帖 子:751
专家分:0
注 册:2006-10-25
收藏
得分:0 

这是我写的!
#include <stdio.h>

int main()
{
int m, n, i;
long a(int n);

scanf("%d", &m);
for(i = 1;i <= m;i ++)
{
scanf("%d", &n);
if(n == 1)
printf("%d %d %d\n", i, n, 3);
else if(n == 2)
printf("%d %d %d\n" ,i, n, 5);
else
printf("%d %d %ld\n", i, n, a(n));
}

return 0;
}

long a(int n)
{
long sum;
int b(int n);
if(n == 3)
sum = 9;
else
sum = b(n - 3) + a(n - 1);

return sum;
}

int b(int n)
{
int f1 = 0, f2 = 4, f;
int i;

for(i = 0;i < n;i ++)
{
f = f1 + f2;
f1 = f2;
f2 = f;
}

return f;
}


该学习了。。。
2006-12-09 16:15
nuciewth
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:我爱龙龙
等 级:贵宾
威 望:104
帖 子:9786
专家分:208
注 册:2006-5-23
收藏
得分:0 
这个是斐波拉基数列吧.
1--->3
2--->5
3--->8
4--->13
5--->21
...

其实就是求大数的加法,估计几百对应的数就会很大.

倚天照海花无数,流水高山心自知。
2006-12-09 17:27
zhanghuan_10
Rank: 1
等 级:新手上路
威 望:2
帖 子:751
专家分:0
注 册:2006-10-25
收藏
得分:0 
这样啊!当数很大时怎样处理啊!

该学习了。。。
2006-12-09 17:29
nuciewth
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:我爱龙龙
等 级:贵宾
威 望:104
帖 子:9786
专家分:208
注 册:2006-5-23
收藏
得分:0 
不对,4 231 32549
怎么才这么点

倚天照海花无数,流水高山心自知。
2006-12-09 17:29
zhanghuan_10
Rank: 1
等 级:新手上路
威 望:2
帖 子:751
专家分:0
注 册:2006-10-25
收藏
得分:0 
是啊!我运行之后得到的数很大!

该学习了。。。
2006-12-09 17:33
卧龙孔明
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:59
帖 子:3872
专家分:684
注 册:2006-10-13
收藏
得分:0 
1.数很大用高精度计算.

2.解决时间效率问题 因为 Memory limit: 64M,所以可以先定义一个数组预先存储1-1000所对应的数据,然后求1000以内的可以直接打印,而超过1000,如1001,只要999+1000即可得到

My Blog: www.aiexp.info
虽然我的路是从这里开始的,但是这里不再是乐土.感谢曾经影响过,引导过,帮助过我的董凯,飞燕,leeco,starwing,Rockcarry,soft_wind等等等等.别了,BCCN.
2006-12-09 17:34
zhanghuan_10
Rank: 1
等 级:新手上路
威 望:2
帖 子:751
专家分:0
注 册:2006-10-25
收藏
得分:0 
1.数很大用高精度计算.
这个不是很理解!能详细说一下吗?

该学习了。。。
2006-12-09 17:49
zhanghuan_10
Rank: 1
等 级:新手上路
威 望:2
帖 子:751
专家分:0
注 册:2006-10-25
收藏
得分:0 
可以这样拆么,如你所说的!也可以先把前两个数存起来!当然这没有什么意义!那3不是可以拆成1+2吗?
另外五楼的的版主,这个好像不是那个数列!因为在3时结果好像是9而不是8。

该学习了。。。
2006-12-09 18:05
卧龙孔明
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:59
帖 子:3872
专家分:684
注 册:2006-10-13
收藏
得分:0 

详见以下帖中我的回帖
http://bbs.bc-cn.net/viewthread.php?tid=107263

http://bbs.bc-cn.net/viewthread.php?tid=107216

[此贴子已经被作者于2006-12-9 18:08:28编辑过]


My Blog: www.aiexp.info
虽然我的路是从这里开始的,但是这里不再是乐土.感谢曾经影响过,引导过,帮助过我的董凯,飞燕,leeco,starwing,Rockcarry,soft_wind等等等等.别了,BCCN.
2006-12-09 18:07
快速回复:[求助]一道acm的题!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027622 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved