HDU 5812 Distance 数论

Distance



Problem Description
In number theory, a prime is a positive integer greater than 1 that has no positive divisors other than 1 and itself. The distance between two positive integers x and y, denoted by d(x, y), is defined as the minimum number of multiplications by a prime or divisions (without a remainder) by a prime one can perform to transform x into y. For example, d(15, 50) = 3, because 50 = 15 * 2 * 5 / 3, and you have to perform two multiplications (*2, *5) and one division (/3) to transform 15 into 50.

For a set S of positive integers, which is initially empty, you are asked to implement the following types of operations on S.

1.  I x: Insert x into S. If x is already in S, just ignore this operation.
2.  D x: Delete x from S. If x is not in S, just ignore this operation.
3.  Q x: Find out a minimum z such that there exists a y in S and d(x, y) = z.
 

Input
The input contains multiple test cases. The first line of each case contains an integer Q (1 <= Q <= 50000), indicating the number of operations. The following lines each contain a letter ‘I’, ‘D’ or ‘Q’, and an integer x (1 <= x <= 1000000).
Q = 0 indicates the end of the input.
The total number of operations does not exceed 300000.
 

Output
For each case, output “Case #X:” first, where X is the case number, starting from 1. Then for each ‘Q’ operation, output the result in a line; if S is empty when a ‘Q’ operation is to perform, output -1 instead.
 

Sample Input
  
  
12 I 20 I 15 Q 30 I 30 Q 30 D 10 Q 27 I 15 D 15 D 20 D 30 Q 5 0
 

Sample Output
  
  
Case #1: 1 0 3 -1
 

//
// d(x, y) = f(x / gcd(x, y)) + f(g / gcd(x, y))
// f(x) is the total number of prime(same is different) factors of x
//

#include <iostream>
#include <set>
#include <cstring>

#define endl "\n"

using namespace std;
typedef long long ll;
const int INF = 1e9;
const int MAXN = 1000000 + 7;
set<int> s;
int prime[MAXN], cnt, tot[MAXN];
bool mark[MAXN];
void getPrime() {
    memset(mark, true, sizeof(mark));
    cnt = 0;
    for(int i = 2; i <= MAXN; ++i) {
        if(mark[i]) {
            prime[++cnt] = i;
            tot[i] = 1;            // total number of factors of i
        }
        for(int j = 1; (j <= cnt) && (i * prime[j] <= MAXN); ++j) {
            mark[i*prime[j]] = false;
            tot[i*prime[j]] = tot[i] + 1;
            if(i % prime[j] == 0) break;
        }
    }
}

multiset<int> dis[MAXN];

void add(int x, int y) {
    dis[x].insert(y);
}

void del(int x, int y) {
    multiset<int>::iterator it = dis[x].find(y);
    // erase first occurrence element = *it 
    // erase all elements = *it while dis.erase(*it)
    if(it != dis[x].end()) dis[x].erase(it);
}

// return first element of dis[x]
int get(int x) {
    if(dis[x].empty()) return INF;
    return *dis[x].begin();
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    getPrime();
    int n, cas = 1, num;
    char op;
    while(cin >> n) {
        s.clear();
        if(n == 0) break;
        cout << "Case #" << cas++ << ":" << endl;
        for(int i = 1; i < MAXN; ++i) {
            dis[i].clear();
        }
        for(int k = 1; k <= n; ++k) {
            cin >> op >> num;
            if(op == 'I') {
                if(s.count(num) == 0) {
                    int i;
                    for(i = 1; i*i < num; ++i) {
                        if(num % i == 0) {
                        	// all possible gcd of num in set and question num
                            add(i, tot[num/i]);
                            add(num/i, tot[i]);
                        }
                    }
                    if(i * i == num) {
                        add(i, tot[i]);
                    }
                    s.insert(num);
                }
            } else if(op == 'D') {
                if(s.count(num)) {
                    s.erase(num);
                    int i;
                    for(i = 1; i*i < num; ++i) {
                        if(num % i == 0) {
                            del(i, tot[num/i]);
                            del(num/i, tot[i]);
                        }
                    }
                    if(i * i == num) {
                        del(i, tot[i]);
                    }
                }
            } else if(op == 'Q') {
                if(s.count(num)) {
                    cout << 0 << endl;
                } else {
                    if(!s.size()) {
                        cout << "-1" << endl;
                        continue;
                    }
                    int ans = INF;
                    for(int i = 1; i * i <= num; ++i) {
                        if(num % i == 0) {
                            ans = min(ans, min(get(i) + tot[num/i], get(num/i) + tot[i]));
                        }
                    }
                    cout << ans << endl;
                }
            }
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值