Can anyone solve this problem?
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 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 singe 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
Help me!
Moderator: Board moderators
-
- New poster
- Posts: 3
- Joined: Tue Feb 03, 2004 12:14 pm
This is a problem from USACO. I've just passed it.
My English is poor. So I can't tell you the way clearly.
You can try every two integers, assume they are the first two in a sequence ,then find the rest ones. If you find correct or more than n numbers , then it's one of the solution.At last , sort all the solutions and print all.
My English is poor. So I can't tell you the way clearly.
You can try every two integers, assume they are the first two in a sequence ,then find the rest ones. If you find correct or more than n numbers , then it's one of the solution.At last , sort all the solutions and print all.
-
- New poster
- Posts: 3
- Joined: Tue Feb 03, 2004 12:14 pm
This is my code. I can pass the data from USACO,but I can't pass the biggest data : 3 250. I wonder is there a way to pass this data.
[pascal]
program ariprog ;
var
a : array[0..1250000]of boolean;
c : array[-1..1250000]of integer;
sh,go : array[1..2,1..100000]of longint;
b : array[1..21047]of longint;
tot,d,m,l,n,nn : longint;
i,j,k : longint;
begin
read(n,m);
fillchar(a,sizeof(a),0);
for i:=0 to m do
for j:=i to m do
begin
tot:=sqr(i)+sqr(j);
if a[tot] then else a[tot]:=true;
end;
k:=0; m:=sqr(m) shl 1;
for i:=0 to m do
if a then begin c:=k; inc(k); b[k]:=i;end else c:=k;
nn:=0;
for i:=1 to k-1 do
begin
tot:=(b[k]-b) div (n-1);
tot:=c[tot+b];
for j:=i+1 to tot do
begin
d:=b[j]-b; m:=b[j];
for l:=3 to n do
begin
inc(m,d);
if a[m] then
else begin dec(l);break;end;
end;
if l=n then begin inc(nn);sh[1][nn]:=b;go[1][nn]:=d; end;
end;
end;
fillchar(c,sizeof(c),0); m:=0;
for i:=1 to nn do
begin inc(c[sh[1]]); if sh[1]>m then m:=sh[1];end;
for i:=1 to m do inc(c[i],c[i-1]);
for i:=1 to nn do
begin
j:=sh[1][i]-1; inc(c[j]); j:=c[j];
sh[2][j]:=sh[1][i]; go[2][j]:=go[1][i];
end;
fillchar(c,sizeof(c),0); m:=0;
for i:=1 to nn do
begin inc(c[go[2][i]]); if go[2][i]>m then m:=go[2][i];end;
for i:=1 to m do inc(c[i],c[i-1]);
for i:=1 to nn do
begin
j:=go[2][i]-1; inc(c[j]); j:=c[j];
sh[1][j]:=sh[2][i]; go[1][j]:=go[2][i];
end;
for i:=1 to nn do writeln(sh[1][i],' ',go[1][i]);
end.[/pascal]
[pascal]
program ariprog ;
var
a : array[0..1250000]of boolean;
c : array[-1..1250000]of integer;
sh,go : array[1..2,1..100000]of longint;
b : array[1..21047]of longint;
tot,d,m,l,n,nn : longint;
i,j,k : longint;
begin
read(n,m);
fillchar(a,sizeof(a),0);
for i:=0 to m do
for j:=i to m do
begin
tot:=sqr(i)+sqr(j);
if a[tot] then else a[tot]:=true;
end;
k:=0; m:=sqr(m) shl 1;
for i:=0 to m do
if a then begin c:=k; inc(k); b[k]:=i;end else c:=k;
nn:=0;
for i:=1 to k-1 do
begin
tot:=(b[k]-b) div (n-1);
tot:=c[tot+b];
for j:=i+1 to tot do
begin
d:=b[j]-b; m:=b[j];
for l:=3 to n do
begin
inc(m,d);
if a[m] then
else begin dec(l);break;end;
end;
if l=n then begin inc(nn);sh[1][nn]:=b;go[1][nn]:=d; end;
end;
end;
fillchar(c,sizeof(c),0); m:=0;
for i:=1 to nn do
begin inc(c[sh[1]]); if sh[1]>m then m:=sh[1];end;
for i:=1 to m do inc(c[i],c[i-1]);
for i:=1 to nn do
begin
j:=sh[1][i]-1; inc(c[j]); j:=c[j];
sh[2][j]:=sh[1][i]; go[2][j]:=go[1][i];
end;
fillchar(c,sizeof(c),0); m:=0;
for i:=1 to nn do
begin inc(c[go[2][i]]); if go[2][i]>m then m:=go[2][i];end;
for i:=1 to m do inc(c[i],c[i-1]);
for i:=1 to nn do
begin
j:=go[2][i]-1; inc(c[j]); j:=c[j];
sh[1][j]:=sh[2][i]; go[1][j]:=go[2][i];
end;
for i:=1 to nn do writeln(sh[1][i],' ',go[1][i]);
end.[/pascal]
-
- New poster
- Posts: 9
- Joined: Tue Jan 06, 2004 7:33 am
many people told me to use brute force, but i can't make it efficient enough to run under 5 seconds.
this is my algorithm:
1) generate all the possible combinations of p^2 + q^2 and stored them in a sorted array
2) searching the sequence with all the possible value of "d", starting from 1 - 1200 (i'm guess 1200 is the upperbound)
Please advice on how I can modify my algorithm, and how i can determine the upper bound of the "difference" in each sequence?
this is my algorithm:
1) generate all the possible combinations of p^2 + q^2 and stored them in a sorted array
2) searching the sequence with all the possible value of "d", starting from 1 - 1200 (i'm guess 1200 is the upperbound)
Please advice on how I can modify my algorithm, and how i can determine the upper bound of the "difference" in each sequence?