USACO Arithmetic Progressions 解题日志

因为一开始把题目意思理解错了,做了好久好久。


还是先把题目贴上来吧:

Arithmetic Progressions

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 search
Line 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)

5
7

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 4
37 4
2 8
29 8
1 12
5 12
13 12
17 12
5 20
2 24

先说说我对题目的误解吧。

一开始,我以为只需要看 a + n * b是不是平方数就可以了。但是这么看很奇怪啊!仔细看了看题目,发现要是长度为n的序列。

然后问题就来了。在题目中对输出的描述中,我以为是前面一个是b,后面一个是a。然后怎么看SAMPLE OUTPUT的“1 4”都是错的啊,是吧?于是我认为我题目读错了,又看了好多遍题目,看得我心累,决定第二天再来做。

第二天,我决定不管三七二十一,先把n长度的序列找出来输出再看看和样例是不是一样好了(寻找方面在下面介绍)。然后我的结果,和样例的前一个后一个是相反的!好开心啊!原来是前面一个是a,后面一个才是b啊!!把输出顺序修改后,还是和题目有点不一样。我很难过。然后,我突然明白题目的最后一句话的意思了!他的意思是说,输出顺序中,先是a“将就”b,b要从小到大排,在这个基础上,a从小到大排。看看我原来的程序,循环最外层是a。那么把循环次序交换一下就好了嘛!

当然其中有一些小细节,调了好久。我好菜啊.jpg。


接着,我们来说这道题是怎么寻找长度为n的平方数序列的。

我们很容易看到,数据规模并不大,于是第一个想到的便是暴力算法。但是题目有时间要求5秒,我第一次直接暴力用了5.26秒。那么我们来对她做一下优化。

首先,p,q的范围M的最大值才250,意味着这个平方数最大也只有125000。不大是吧?于是我们可以开一个布尔类型的数组exist[125001],其中,exist[i] = 1 代表 i 这个数是平方数,exist[i] = 0 代表 i 不是平方数。这样,在寻找序列前,我们先把小于等于 2 * M * M 的平方数都算出来,修改对应的exist数组的值(初始全为0)。

接下来,我们开始寻找序列。同样,a也不用从0开始每次加一地寻找。注意到,既然我们要找的序列首项为a,那么a肯定是平方数。因此,我们只需要将最开始算出来的所有平方数从小到大排个序,然后a依次从这个序列里取就好了。

害怕还是会超时,放弃了>> , << 操作,选择了scanf和printf。。。


好了,下面贴上代码:

#include<fstream>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
using namespace std;

/*ifstream fin("ariprog.in");
ofstream fout("ariprog.out");*/

int n, m;
bool exist[126000];
int num[126000];

bool check(int a, int b)
{
        for (int i = 0; i < n; ++i){
                if (!exist[a]) return 0;
                a += b;
        }
        return 1;
}

int main()
{
        freopen("ariprog.in","r",stdin);
        freopen("ariprog.out","w",stdout);
        //while (scanf("%d%d", &n, &m) != EOF){
        //cin >> n >> m;
        scanf("%d%d", &n, &m);
        int numCnt = 0;
        for (int i = 0; i <= m; ++i)
        for (int j = 0; j <= m; ++j) {
                if (!exist[i * i + j * j]){
                        num[numCnt++] = i * i + j * j;
                        exist[i * i + j * j] = 1;
                }
        }

        sort(num, num + numCnt);
        int a = num[0], b, pos;
        int cnt = 0;
        int bigB = (num[numCnt - 1] - num[0]) / (n - 1);
        for (b = 1; b <= bigB; ++b){
                for (int j = 0; j < numCnt; ++j){
                        if (check(num[j], b)) ++cnt, printf("%d %d\n", num[j], b);
                        if (num[j] + (n - 1) * b > num[numCnt - 1]) break;
                }
        }
        if (cnt == 0) puts("NONE");
        //}//fout << "NONE" << endl;//fout << "NONE" << endl;
        /*fin.close();
        fout.close();*/

        return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值