138 - Street Numbers
Moderator: Board moderators
My code is as follows:
// @BEGIN_OF_SOURCE_CODE
/* @JUDGE_ID: 17243NT 138 C++ "m^2 = n (n + 1) / 2" */
// Send to judge@uva.es
#include <stdio.h>
#include <math.h>
int main() {
int i = 0, m, n, tri;
for(n = 2; i < 10; n++) {
tri = (n * (n + 1)) >> 1;
m = sqrt(tri);
if(m * m == tri) {
printf("%10d%10dn", m, n);
i++;
}
}
return 0;
}
// @END_OF_SOURCE_CODE
I get:
6 8
35 49
204 288
1189 1681
6930 9800
256 131072
7742 131528
11707 132113
19813 134033
25162 135816
Is this wrong?
// @BEGIN_OF_SOURCE_CODE
/* @JUDGE_ID: 17243NT 138 C++ "m^2 = n (n + 1) / 2" */
// Send to judge@uva.es
#include <stdio.h>
#include <math.h>
int main() {
int i = 0, m, n, tri;
for(n = 2; i < 10; n++) {
tri = (n * (n + 1)) >> 1;
m = sqrt(tri);
if(m * m == tri) {
printf("%10d%10dn", m, n);
i++;
}
}
return 0;
}
// @END_OF_SOURCE_CODE
I get:
6 8
35 49
204 288
1189 1681
6930 9800
256 131072
7742 131528
11707 132113
19813 134033
25162 135816
Is this wrong?
138 Number Street - How to reduce the running time?
I know the problem in fact is x^2+x=2y^2, but my program runs too slow. (I tried every x as 1,2,.....)
And I use "int64" in Free Pascal. Does it work on the judge system?
And I use "int64" in Free Pascal. Does it work on the judge system?
Re: 138 Number Street - How to reduce the running time?
Try to find an explicit solution to your diophantine equation.
It can be done in this case.
Kristoffer Hansen
It can be done in this case.
Kristoffer Hansen
I cannot find one.Can u help me?
I cannot find one.Can u help me?
If this were a math class, Diophantine equations would be the way to go. However, you are trying to solve a computing problem. When you brute force the Diophantine equation, you turn an elegant solution into a VERY VERY ugly one. Instead, try brute forcing the problem.
The problem asks a very simple thing. Add the left and the right house numbers. So, just do that:o) Why make it more complicated?
If you use the arithmetic progression formula:
and then use a Dynamic Programming-esque lookup table to precalc the values, you should be fine.
The problem asks a very simple thing. Add the left and the right house numbers. So, just do that:o) Why make it more complicated?
If you use the arithmetic progression formula:
Code: Select all
S=(A+B)*N/2;
where
S=sum of progression
A=first term
B=last term
N=number of terms
-
- A great helper
- Posts: 284
- Joined: Thu Feb 28, 2002 2:00 am
- Location: Germany
- Contact:
-
- A great helper
- Posts: 284
- Joined: Thu Feb 28, 2002 2:00 am
- Location: Germany
- Contact:
-
- A great helper
- Posts: 284
- Joined: Thu Feb 28, 2002 2:00 am
- Location: Germany
- Contact:
-
- A great helper
- Posts: 284
- Joined: Thu Feb 28, 2002 2:00 am
- Location: Germany
- Contact: