USACO sec1.4 Arithmetic Progressions

预处理+暴力,4.8s险过;

注意长度为N的等差数列是指总共有N个数,所以公差上界为2*M*M/(N-1);

/*

PROG : ariprog

LANG : C

*/

# include <stdio.h>

# include <stdlib.h>



# define MAXN (62505 * 2)



int N, M, ans;

char f[MAXN];

short p[MAXN], q[MAXN];



void pre(void)

{

    int i, j, x;

    for (i = 0; i <= 250; ++i)

    for (j = i; j <= 250; ++j)

    {

        x = i*i + j*j;

        f[x] = 1;

        if ((!q[x] && !p[x]) || (j-i <= q[x]-p[x]))

            q[x] = j, p[x] = i;

    }

}



void search(int a, int inc)

{

    int i, t;

    for (i = 0; i < N; ++i)

    {

        t = a + inc*i;

        if (!f[t] || p[t]>M || q[t]>M) return ;

    }

    ++ans;

    printf("%d %d\n", a, inc);      

}



int main()

{

    freopen("ariprog.in", "r", stdin);

    freopen("ariprog.out", "w", stdout);



    int i, inc, lim, up;

    

    ans = 0;

    pre();    

    scanf("%d%d", &N, &M); up = M*M, lim = up*2/(N-1);

    for (inc = 1; inc <= lim; ++inc)

    for (i = 0; i <= up; ++i)

    {

        if (i+inc*(N-1) <= 2*up) search(i, inc);

    }

    if (!ans) puts("NONE");

    

    fclose(stdin);

    fclose(stdout);

    

    return 0;

}

 

你可能感兴趣的:(progress)