Sicily 1038. EKG Sequence
2015-03-21 08:41
253 查看
1038. EKG Sequence
Constraints
Time Limit: 5 secs, Memory Limit: 32 MB
Description
The EKG sequence is a sequence of positive integers generated as follows: The first two numbers of the sequence are 1 and 2. Each successive entry is the smallest positive integer not already used that shares a factor with the preceding term. So, the thirdentry in the sequence is 4 (being the smallest even number not yet used). The next number is 6 and the next is 3. The first few numbers of this sequence are given below.
1, 2, 4, 6, 3, 9, 12, 8, 10, 5, 15, 18, 14, 7, 21, 24, 16, 20, 22, 11, 33, 27
The sequence gets its name from its rather erratic fluctuations. The sequence has a couple of interesting, but non-trivial, properties. One is that all positive integers will eventually appear in the sequence. Another is that all primes appear in increasing
order. Your job here is to _nd the position in the sequence of a given integer.
Input
Input consists of a number of test cases. Each case will be a line containing a single integer n, 1 <= n <= 300000. An input of 0 follows the last test case. Note that the portion of the EKG sequence that contains all integers <= 300,000 will not contain aninteger >1,000,000.
Output
Each test case should produce one line of output of the form:The number n appears in location p.
where n is the number given and p is the position of n in the EKG sequence. You are guaranteed that p will be no larger than 1,000,000.
Sample Input
12 21 2 33 100000 299977 0
Sample Output
The number 12 appears in location 7. The number 21 appears in location 15. The number 2 appears in location 2. The number 33 appears in location 21. The number 100000 appears in location 97110. The number 299977 appears in location 584871.// Problem#: 1038 // Submission#: 3238022 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/ // All Copyright reserved by Informatic Lab of Sun Yat-sen University #include <algorithm> #include <iostream> #include <string> #include <stdio.h> #include <queue> #include <string.h> #include <vector> #include <iomanip> #include <map> #include <stack> #include <functional> #include <list> #include <cmath> using namespace std; const int MAX = 1000001; int ans[MAX]; int Bn[MAX]; inline int gap(int m) { } int small[MAX]; inline int quot(int m) { } void init() { int i, j, n, k, B, p; ans[1] = 1; ans[2] = 2; for (i = 2; i < MAX; i++) Bn[i] = i; Bn[2] = 4; for (i = 2; i < MAX; i++) { if (small[i] == 0) { for (j = i; j < MAX; j += i) { if (small[j] == 0) small[j] = i; } } } k, B = 2; for (n = 2; n < MAX; n++) { k = B; B = MAX; while (k != 1) { p = small[k]; while (ans[Bn[p]] != 0 && Bn[p] + p < MAX) Bn[p] += p; if (Bn[p] < B) B = Bn[p]; while (k % p == 0) k /= p; } ans[B] = n + 1; } } int main() { std::ios::sync_with_stdio(false); init(); int pos; while (1) { cin >> pos; if (pos == 0) break; cout << "The number " << pos << " appears in location " << ans[pos] << "." << endl; } //getchar(); //getchar(); return 0; }
相关文章推荐
- Sicily 1028. Hanoi Tower Sequence
- Sicily1209. Sequence Sum Possibi题解
- Sicily 1209 Sequence Sum Possibi
- Sicily 1803. Arithmetic Sequence
- <OJ_Sicily>Hanoi_Tower_Sequence
- sicily 1028.Hanoi Tower Sequence
- sicily 1002(anti-prime sequence)
- Sicily 1028 Hanoi Tower Sequence
- sicily 1028. Hanoi Tower Sequence
- sicily 1800 Sequence【线段树-RMQ】
- sicily 1209. Sequence Sum Possibi
- Sicily 1028. Hanoi Tower Sequence
- Sicily 解题: 1028 Hanoi Tower Sequence
- sicily 1089. Farey Sequence
- [sicily]1209. Sequence Sum Possibi
- Sicily 1028. Hanoi Tower Sequence
- Sicily/1089. Farey Sequence
- Sicily 1089. Farey Sequence
- Sicily 1028. Hanoi Tower Sequence【高精度取余和除法】
- Sicily 1089.Farey Sequence[线性筛法求素数]