11053 - Flavius Josephus Reloaded
Moderator: Board moderators
-
- Guru
- Posts: 724
- Joined: Wed Dec 19, 2001 2:00 am
- Location: Germany
The second approach is not workable.Adrian Kuegel wrote:The description of the solutions for the problems are now online.
You can read them at http://www.informatik.uni-ulm.de/acm/Lo ... judge.html .
I use the STL set and I have highly optimized my code, but it is still TLE...Another possibility is to store the numbers of the soldiers in a set and when we calculate a number which is already in the set, we have found the first soldier to commit suicide.
![:-?](./images/smilies/icon_confused.gif)
If the first soldier dies after about n = 10^6 steps, then n log n = 3*10^7.
It is not workable for too many such cases.
-
- New poster
- Posts: 28
- Joined: Fri Mar 30, 2012 12:57 am
Re: 11053 - Flavius Josephus Reloaded
Hi!
Why TLE for this solution?
Please help!
1000000 iteration of the main loop is not time-consuming!
Here is the code:Thanks in advance!
Why TLE for this solution?
Please help!
1000000 iteration of the main loop is not time-consuming!
Here is the code:
Code: Select all
#include <iostream>
#include <cstring>
#include <map>
using namespace std;
long long int n, a, b;
char seen [ 1000000000 ];
int ind [ 1000000 ];
int rem;
void process();
int main()
{
cin >> n;
while ( n != 0 )
{
cin >> a >> b;
process();
cin >> n;
}
return 0;
}
void process()
{
memset( seen, 0, sizeof(seen) );
long long int next = 0;
int cnt = 0 ;
while ( true )
{
if ( seen[ next ] == 0 )
{
seen[ next ] = 1;
ind[ cnt++ ] = next;
}
else
{
for ( int i = 0; i < cnt; i++ )
{
if ( ind[ i ] == next )
{
rem = n - (cnt - i);
break;
}
}
break;
}
next = (((a * ((next * next) % n)) % n) + b) % n;
}
cout << rem << endl;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 11053 - Flavius Josephus Reloaded
memset is going to be slow.
See http://www.informatik.uni-ulm.de/acm/Lo ... judge.html
See http://www.informatik.uni-ulm.de/acm/Lo ... judge.html
Check input and AC output for thousands of problems on uDebug!