Sicily 1038. EKG Sequence

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 third entry 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 an integer >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;
}                                 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值