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

该学习了。。。
2006-12-09 17:29
zhanghuan_10
Rank: 1
等 级:新手上路
威 望:2
帖 子:751
专家分:0
注 册:2006-10-25
收藏
得分:0 
是啊!我运行之后得到的数很大!

该学习了。。。
2006-12-09 17:33
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
zhanghuan_10
Rank: 1
等 级:新手上路
威 望:2
帖 子:751
专家分:0
注 册:2006-10-25
收藏
得分:0 

还有能帮我回答一下9楼的问题吗?谢谢了!


该学习了。。。
2006-12-09 18:14
zhanghuan_10
Rank: 1
等 级:新手上路
威 望:2
帖 子:751
专家分:0
注 册:2006-10-25
收藏
得分:0 

又写了一个,好像用时更多了!
#include <stdio.h>

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

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

return 0;
}

long a(long n)
{
long sum = 0;

if(n == 1)
sum = 3;
else if(n == 2)
sum = 5;
else if(n == 3)
sum = 9;
else
sum = a(n - 1) + a(n - 2) - 1;
return sum;
}


该学习了。。。
2006-12-09 19:11
zhanghuan_10
Rank: 1
等 级:新手上路
威 望:2
帖 子:751
专家分:0
注 册:2006-10-25
收藏
得分:0 

又写了一个,还是不对啊!
#include <stdio.h>

int main()
{
int s, m, i, j, k;
int sum;

scanf("%d", &s);
for(i = 1;i <= s;i ++)
{
sum = 0;
scanf("%d", &m);
if(m == 1)
printf("%d %d %d\n", i, m, 3);
else if(m == 2)
printf("%d %d %d\n", i, m, 5);
else if(m == 3)
printf("%d %d %d\n", i, m, 9);
else
{
for(k = 4;k <= m;k ++)
for(j = 2;j <= i - 2;j ++)
{
if(i%j == 0)
continue;
else if(i%2 == 0&&j%2 == 0)
continue;
else
sum ++;
}
sum += (3 + 2*(2*m -3));
printf("%d %d %d\n", i, m, sum);
}
}

return 0;

}


该学习了。。。
2006-12-09 19:27
zhanghuan_10
Rank: 1
等 级:新手上路
威 望:2
帖 子:751
专家分:0
注 册:2006-10-25
收藏
得分:0 
我想前三个单独列给出来,2*(2*m - 3) + 3这是固定的!然后其他的只要是没有公约数就行!不知对不对!

该学习了。。。
2006-12-09 19:52
快速回复:[求助]一道acm的题!
数据加载中...
 
   



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

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