首页 > 代码库 > HDU 4937 Lucky Number 规律题_(:зゝ∠)_

HDU 4937 Lucky Number 规律题_(:зゝ∠)_

把所有合法的进制打出来会发现合法的进制都是在 

n/3 n/4 n/5的边上

然后暴力边上的进制数。。


#include <cstdio>
#include <set>
typedef long long ll;

bool ok(ll x, ll y) {
    ll v;
    while (x > 0) {
        v = x % y;
        if (v != 3 && v != 4 && v != 5 && v != 6) 
            return false;
        x /= y;
    }    
    return true;
}

std::set<ll> vis;
int T = 0;
ll n;

void work() {
    scanf("%I64d", &n);
    printf("Case #%d: ", ++T);
    if (n == 3 || n == 4 || n == 5 || n == 6)
        puts("-1");
    else {
        vis.clear();
        int ans = 0;
        ll base;
        for (int i = 1; i <= 50 && i < n; ++i) {
            for (int j = 0; j <= 50; ++j) {
                base = n / i + j;
                if (base <= 1 || base >= n || vis.count(base) > 0)
                    continue;
                vis.insert(base);
                if (ok(n, base))
                    ++ ans;
            }
            for (int j = 1; j <= 50; ++j) {
                base = n / i - j;
                if (base <= 1 || base >= n || vis.count(base) > 0)
                    continue;
                vis.insert(base);
                if (ok(n, base))
                    ++ ans;
            }
        }
        for (int i = 2; i < n && i <= 1000; ++i) {
            if (vis.count(i))
                continue;
            vis.insert(i);
            if (ok(n, i))
                ++ ans;
        }
        printf("%d\n", ans);
    }
}

int main() {
    int cas;
    scanf("%d", &cas);
    while (cas -- > 0) 
        work();
    return 0;
}