Hoj 1016 Joseph's problem I tree Array

Source: Internet
Author: User
Hoj 1016 Joseph's problem I
My tags (Edit)
  Source: Unknown
  Time Limit: 10 sec   Memory limit: 32 m

Submitted1484,Accepted439

The Joseph's problem is notoriously known. For those who are not familiar with the problem, among N people numbered 1, 2... n, standing in circle everyMTHIs going to be executed and only the life of the last remaining person will be saved. joseph was smart enough to choose the position of the last remaining person, thus saving his life to give the message about the incident.

Although good programmers have been saved since Joseph spread out this information, Joseph's cousin introduced a new variant of the malignant game. this insane character is known for its barbarian ideas and wishes to clean up the world from silly programmers. we had to infiltrate some the agents of the ACM in order to know the process in this new mortal game.

In order to save yourself from this edevil practice, you must develop a tool capable of predicting which person will be saved.

The destructive process

The persons are eliminated in a very peculiar order; M is a dynamical variable, which each time takes a different value corresponding to the prime numbers 'succession (2, 3, 5, 7 ...). so in order to kill the ith person, Joseph's cousin counts up toIthPrime.

Input

It consists of separate lines containing N [1 .. 3501], and finishes with a 0.

Output

The output will consist in separate lines containing the position of the person which life will be saved.

Sample Input

 
6

Sample output

 
4

Question:
Joseph's ring problem, but the interval between the K times is the K prime number.

Analysis:
Use the sieve method to filter out all prime numbers, and then use the tree array to calculate the K size. However, the data volume is large
If every input data is retrieved, it will be tle... So we can first store all the 3501
In an array, each query is directly output at O (1) time.

# Include <vector> # include <cstdio> # include <cstring> # include <iostream> using namespace STD; typedef long ll; # define debug puts ("here "); const int x = 3605; const int maxm = 36005; vector <int> prime; bool use [maxm]; int C [X]; void Init () {memset (use, false, sizeof (use); Prime. push_back (2); For (INT I = 3; I <maxm; I ++ = 2) if (! Use [I]) {Prime. push_back (I); For (Int J = I + I; j <maxm; j + = I) use [J] = true ;}} int lowbit (INT X) {return X &-X;} void modify (int x, int num) {While (x <X) {C [x] + = num; X + = lowbit (x) ;}} int query (int x) {int ret = 0; while (x> 0) {RET + = C [x]; x-= lowbit (x);} return ret;} void binary (int n, int K) {int L = 1, R = N; while (L <= r) {int mid = (L + r)> 1; int temp = query (MID); If (temp> = k) r = mid-1; else l = Mid + 1;} modify (L,-1);} int ans [X]; int main () {# ifndef online_judge freopen ("sum. in "," r ", stdin); // freopen (" sum. out "," W ", stdout); # endif int op = 3600; // controls Init (); int N; For (n = 1; n <op; N ++) {memset (C, 0, sizeof (c); For (INT I = 1; I <= N; I ++) Modify (I, 1 ); int pre = 1; int res = N; For (INT I = 0; I <n-1; I ++) {pre = (prime [I] + pre-2) % res + 1; binary (n, pre); res --;} For (INT I = 1; I <= N; I ++) if (C [I]) {ans [N] = I; break;} while (scanf ("% d", & N), n) printf ("% d \ n ", ans [N]); Return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.