Palindrome P2425 Little Red Riding Hood

Palindrome P2425 Little Red Riding Hood

This enumeration Hex violent conversion is human. But only 40pts.

In fact, there is such a law: for \ (the X-\) , the (x + 1 \) \ under the band is definitely a palindrome, then also, because only one. So only need to enumerate to (x + 1 \) \ so far. But no use.

We consider optimizing part notation.

When the band is less than \ (\ sqrt {a_i} \ ) , there will be a lot of bits, and greater than or equal \ (\ sqrt {a_i} \ ) only when the two.

Also two palindrome, that the two numbers are the same. This figure is set \ (J \) , hexadecimal is \ (b \) , then:

\ [b \ times j + j
= a_i \] can be solved for \ (B = \ {a_i FRAC -1} {J} \) , when the \ (a_i \) be (J \) \ there is divisible Solutions .

When it is less than hexadecimal \ (\ sqrt {a_i} \ ) , the complexity
\ (O (\ sqrt {a_i} \ log⁡ \ sqrt {a_i}) \) , is equal to greater than \ (O (\ sqrt a_i} {) \) .

So can too.

Code:

#include<cstdio>
#include<cmath>
#include<vector>
#define ll long long
const int maxn = 100005;
ll vec[maxn], tot;
ll m;
bool check(ll a, ll b) {
    tot = 0;
    while(a) {
        vec[++tot] = a % b; a /= b;
    }
    ll i = 1, j = tot;
    while(i <= j) {
        if(vec[i] != vec[j]) return false;
        i++, j--;
    }
    return true;
}
ll read() {
    ll ans = 0, s = 1;
    char ch = getchar();
    while(ch > '9' || ch < '0') { if(ch == '-') s = -1; ch = getchar(); }
    while(ch >= '0' && ch <= '9') ans = ans * 10 + ch - '0', ch = getchar();
    return s * ans;
}
int main() {
    m = read();
    while(m--) {
        ll a = read();
        ll lim = sqrt(a);
        bool flag = false;
        for(ll i = 2; i <= std::max(2ll, lim); i++) {
            if(check(a, i)) {
                printf("%lld\n", i);
                flag = true; break;
            }
        }
        if(flag) continue;
        for(ll i = a / lim; i; i--) {
            if(a % i == 0) {
                printf("%lld\n", a / i - 1);
                flag = true; break;
            }
        }
        if(flag) continue;
        printf("%lld\n", a + 1);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/Garen-Wang/p/10959622.html