等差数列Arithmetic Progressions题解(USACO1.4)

Arithmetic Progressions USACO1.4

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).

INPUT:
N (3 <= N <= 25), the length of progressions for which to search
M (1 <= M <= 250), an upper bound to limit the search to the bisquares with 0 <= p,q <= M.
OUTPUT:
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.

此题目需要一些小的剪枝,详见注释。

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

#define rint register int
const int MAXN=100000+5;
int a[MAXN];
int aa[10005],bb[10005];
bool tab[MAXN];
int n,m,cnt,tot,mx;

int main()
{
    freopen("ariprog.in","r",stdin);
    freopen("ariprog.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(rint i=0;i<=m;++i)
        for(rint j=0;j<=m;++j)
            a[++cnt]=i*i+j*j,tab[a[cnt]]=1;//预处理双平方数表,快速查表
    sort(a+1,a+cnt);
    cnt=unique(a+1,a+cnt+1)-a-1;
    mx=m*m<<1;
    int r=mx/(n-1);//公差上界,最大的数除以要求的长度
    for(rint i=1;i<=r;++i)
    {
        for(rint j=1;j<=cnt;++j)
        {
            rint c=0;
            for(rint k=n-1;k>0&&a[j]+i*k<=mx;--k)//若超过max退出循环
                //从大到小枚举,不符合情况易退出
                if(!tab[a[j]+i*k]) //若有一个不符合条件即break
                    break;
                else ++c;
            if(c==n-1)
            {
                aa[++tot]=a[j];
                bb[tot]=i;
            }
        }
    }
    if(tot==0)
        puts("NONE");
    else
        for(int i=1;i<=tot;++i)
            printf("%d %d\n",aa[i],bb[i]);
    return 0;
}

转载于:https://www.cnblogs.com/chwhc/p/7695701.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值