【♂Not The Triumph♂O(∩_∩)O哈哈~But The Struggle♂】

竞赛决不是捷径,它只是另一种艰辛的生活方式。得到与失去,只有时间会去评判;成功与失败,只有历史能去仲裁。我不会永远成功,正如我不会永远失败一样

  C++博客 :: 首页 :: 联系 ::  :: 管理
  6 Posts :: 239 Stories :: 25 Comments :: 0 Trackbacks

常用链接

留言簿(7)

我参与的团队

搜索

  •  

积分与排名

  • 积分 - 107646
  • 排名 - 231

最新评论

阅读排行榜

评论排行榜

一个等差数列是一个能表示成a, a+b, a+2b,..., a+nb (n=0,1,2,3,...)的数列。

在这个问题中a是一个非负的整数,b是正整数。写一个程序来找出在双平方数集合(双平方数集合是所有能表示成p^2+q^2的数的集合)S中长度为n的等差数列。


INPUT FORMAT:

(file ariprog.in)

第一行: N(3<= N<=25),要找的等差数列的长度。

第二行: M(1<= M<=250),搜索双平方数的上界0 <= p,q <= M。

OUTPUT FORMAT:

(file ariprog.out)

如果没有找到数列,输出`NONE'。

如果找到了,输出一行或多行, 每行由二个整数组成:a,b。

这些行应该先按b排序再按a排序。

所求的等差数列将不会多于10,000个。


input:
5
7

output:
1 4
37 4
2 8
29 8
1 12
5 12
13 12
17 12
5 20
2 24

【参考程序】:
/*
ID: XIONGNA1
PROG: ariprog
LANG: C++
*/
#include
<iostream>
#include
<cstring>
#include
<cstdlib>
using namespace std;
struct node
{
    
int a1,d;
}ans[
10001];
bool bo[125001];
int x[125001];
int n,m,an,d1,sum,k;
int cmp(const void *s,const void *t)
{
    node i
=*(node *)s,j=*(node *)t;
    
if (i.d!=j.d) return i.d-j.d;
    
else return i.a1-j.a1;
}
void check(int p)
{
    
int a=x[p];
    
for (int i=2;i<=n;i++)
    {
        a
+=d1;
        
if (!bo[a]) return ;
    }
    sum
++;
    ans[sum].a1
=x[p];ans[sum].d=d1;
}
int main()
{
    freopen(
"ariprog.in","r",stdin);
    freopen(
"ariprog.out","w",stdout);
    scanf(
"%d%d",&n,&m);
    memset(bo,
false,sizeof(bo));
    
for (int i=0;i<=m;i++)
        
for (int j=0;j<=m;j++)
            bo[i
*i+j*j]=true;
    k
=0;
    
for (int i=0;i<=2*m*m;i++)
        
if (bo[i])
        {
            k
++;x[k]=i;
        }
    sum
=0;
    
for (int i=1;i<=k-1;i++)
        
for (int j=i+1;j<=k;j++)
        {
            d1
=x[j]-x[i];
            an
=x[i]+(n-1)*d1;
            
if (an>2*m*m) break;
            
if (!bo[an]) continue;
            check(i);
        }
    
if (sum==0) printf("NONE\n");
    
else 
    {
        qsort(ans
+1,sum,sizeof(node),cmp);
        
for (int i=1;i<=sum;i++)
            printf(
"%d %d\n",ans[i].a1,ans[i].d);
    }
    
return 0;
}
posted on 2009-07-16 10:41 开拓者 阅读(273) 评论(0)  编辑 收藏 引用 所属分类: USACO 题解

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理