Posted: Fri Feb 17, 2006 9:45 am
Nice 
Note that m%(n+1) is either 0 or 1 (roughly 6 times faster?)
Darko

Note that m%(n+1) is either 0 or 1 (roughly 6 times faster?)
Darko
My solution is very similar to this, although slightly more optimized (since it eliminates many m values in advance). I compiled and ran both your version and my version on my machine, and mine was about .1 second faster (0.5 seconds vs. 0.4 seconds, roughly), to compute all cases (1 through 13).mf wrote:Actually there is a way to solve this problem without precalculation!
Here's how I've solved it, but I'm too lazy to re-derive my solution now.
The code solves the case k=13 in less than a second.
<snip>
Aha, I hadn't thought of that. I'll try this.mf wrote:Input may contain repeated values of k, and if your code recomputes output every time then it'll easily time-out. (Just imagine a file with, say, a thousand of 13's).
Code: Select all
#include <iostream>
using namespace std;
int main (void)
{
int k;
while ((cin>>k)&&k)
{
int p = k*2;
int m;
for (m=k; ; m++)
{
int x=p;
int y=(m-1)%x;
while (y>=k && x>k)
{
x--;
y = (y-1+m)%x;
}
if (x==k)
break;
}
cout << m << endl;
}
}
Code: Select all
#include<iostream>
}
Code: Select all
Code: Select all
long int p[13];