Topcoder SRM 699 Div2 review

Single Round Match 699 Sponsored by Cisco

Div II Level One: UpDownHiking

Idea is same with editorial.

class UpDownHiking {
    public:
    int maxHeight(int N, int A, int B) {
        int ans = 0;
        for (int i = 0; i <= N; ++i) {
            int tmp = min(A * i, B * (N - i));
            ans = max(ans, tmp);
        }
        return ans;
    }
};

Div II Level Two: LastDigit

Category: Arithmetric

The approach is different from editorial which uses binary search.

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <typeinfo>
#include <fstream>

using namespace std;
typedef long long ll;

class LastDigit {
    public:
    long long findX(long long S) {
        ll base = 1111111111111111111;
        ll ans = 0;
        for (int j = 18; j >= 0; --j) {
            if (S / base > 9) {
                //cout << "fail: " << S << " " << base;
                return -1;
            }
            ans = ans * 10 + S / base;
            S = S % base;
            base /= 10;
        }
        return ans;
    }
};

Div II Level Three: FromToDivisibleDiv2

Category: Arithmetric, graph

I couldn’t solve it in contest, so I just followed editorial for my practice.

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <typeinfo>
#include <fstream>
#include <queue>

using namespace std;

int dist[1005];

int gcd(int a, int b) { return b == 0 ? a : gcd(b, a%b); }
long lcm(int a, int b) { return (long) a * b / gcd(a, b); }

class FromToDivisibleDiv2 {
    public:
    int shortest(int N, int S, int T, vector<int> a, vector<int> b) {

        unsigned long m = a.size();
        queue<int> q;
        for (int i = 0; i < m; ++i) {
            if (S % a[i] == 0) {
                dist[i]= 1;
                q.push(i);
            } else {
                dist[i] = 0; // Init dist
            }
        }

        while (!q.empty()) {
            int i = q.front();
            q.pop();
            if (T % b[i] == 0) return dist[i];
            for (int j = 0; j < m; ++j) {
                if (dist[j] == 0 && lcm(b[i], a[j]) <= N) {
                    q.push(j);
                    dist[j] = dist[i] + 1;
                }
            }
        }
        return -1;
    }
};

Leave a Comment

Your email address will not be published. Required fields are marked *