[USACO1.4]等差数列 Arithmetic Progressions

来源:互联网 发布:农村淘宝服务站挣钱不 编辑:程序博客网 时间:2024/05/20 22:27

An arithmetic progression is a sequence of the form a, a+b, a+2b, ..., a+nb where n=0,1,2,3,... . For this problem, a is a non-negative integer and b is a positive integer.

Write a program that finds all arithmetic progressions of length n in the set S of bisquares. The set of bisquares is defined as the set of all integers of the form p2 + q2 (where p and q are non-negative integers).

TIME LIMIT: 5 secs

PROGRAM NAME: ariprog

INPUT FORMAT

Line 1:N (3 <= N <= 25), the length of progressions for which to searchLine 2:M (1 <= M <= 250), an upper bound to limit the search to the bisquares with 0 <= p,q <= M.

SAMPLE INPUT (file ariprog.in)

57

OUTPUT FORMAT

If no sequence is found, a single line reading `NONE'. Otherwise, output one or more lines, each with two integers: the first element in a found sequence and the difference between consecutive elements in the same sequence. The lines should be ordered with smallest-difference sequences first and smallest starting number within those sequences first.

There will be no more than 10,000 sequences.

SAMPLE OUTPUT (file ariprog.out)

1 437 42 829 81 125 1213 1217 125 202 24

Arithmetic Progressions 等差数列

目录

 [隐藏] 
  • 1 描述
  • 2 格式
  • 3 SAMPLE INPUT
  • 4 SAMPLE OUTPUT

[编辑]描述

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

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

[编辑]格式

时间限制: 5 秒

题目名称: ariprog

输入格式:

(file ariprog.in)

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

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


输出格式:

(file ariprog.out)

如果没有找到数列,输出“NONE”。

如果找到了,输出一行或多行, 每行由二个整数组成:a,b。 a为等差数列的第一个值,b为等差数列的公差。

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

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

[编辑]SAMPLE INPUT

57

[编辑]SAMPLE OUTPUT

1 437 42 829 81 125 1213 1217 125 202 24




【题解】
剪剪剪
/*ID:luojiny1LANG:C++TASK:ariprog*/#include<cstdio>#include<algorithm>using namespace std;const int maxn=62510;int a[maxn*2]={0},len,m,n=0,cnt=0,MAXB;bool vis[maxn*2]={0};int main(){freopen("ariprog.in","r",stdin);freopen("ariprog.out","w",stdout);scanf("%d%d",&len,&m);for(int i=0;i<=m;i++)for(int j=i;j<=m;j++)//优化1 vis[i*i+j*j]=1;for(int i=0;i<(maxn<<1);i++){n+=vis[i];if(vis[i])a[n-1]=i;}MAXB=(a[n-1]-a[0])/(len-1);//优化2 for(int b=1;b<=MAXB;b++)for(int i=0;i<n;i++){if(a[i]+b*(len-1)>a[n-1])break;//优化3 最重要 bool flag=1;for(int j=1;j<=len-1;j++)if(vis[a[i]+j*b]==0){flag=0;break; //优化4 重要 }if(flag){printf("%d %d\n",a[i],b);cnt=1;}}if(cnt==0)printf("NONE\n");return 0;}



阅读全文
0 0
原创粉丝点击