
Note that m%(n+1) is either 0 or 1 (roughly 6 times faster?)
Darko
Moderator: Board moderators
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>
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];