HDU-5778 abs

题目大意:

给定一个数x,求正整数y\geq 2y≥2,使得满足以下条件:
1.y-x的绝对值最小
2.y的质因数分解式中每个质因数均恰好出现2次。

解题思路:

由于y质因数分解式中每个质因数均出现2次,那么y是一个完全平方数,设y=z*z,题目可转换成求z,使得每个质因数出现1次. 我们可以暴力枚举z,检查z是否符合要求,显然当z是质数是符合要求,由素数定理可以得,z的枚举量在logn级别

代码:

朴素写法:

#include <cmath>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long LL;

const int MAXN = 1e5 + 10;

LL prime[MAXN];

void init()
{
    memset(prime, 0, sizeof(prime));
    for(int i = 2; i < MAXN; ++i){
        if(!prime[i]) prime[++prime[0]] = i;
        for(int j = 1; j <= prime[0] && prime[j] < MAXN / i; ++j){
            prime[prime[j] * i] = 1;
            if(i % prime[j] == 0) break;
        }
    }
}
bool isPrime(LL n){
    for(int i = 2; i * i <= n; ++i){
        if(n % i == 0) return 0;
    }
    return 1;
}
bool Judge(LL n){
    for(int i = 1; i <= prime[0]; ++i){
        if(n % prime[i] == 0){
            n /= prime[i];
            if(n % prime[i] == 0) return 0;
        }
    }
    if(n == 1 || isPrime(n)) return 1;
    else return 0;
}
LL solve(LL n){
    LL d = sqrt(n + 0.5);
    LL ans = 1e18;
    for(LL i = d; i >= 2; --i){
        if(Judge(i)){
            ans = min(ans, n - (i * i));
            break;
        }
    }
    for(LL i = d + 1; ; ++i){
        if(Judge(i)){
            ans = min(ans, (i * i) - n);
            break;
        }
    }
    return ans;
}
int main(){
    LL n, t;
    cin >> t; init();
    while(t--){
        cin >> n;
        cout << solve(n) << endl;
    }
    return 0;
}

Miller_Rabin + Pollard_rho写法:

#include <cmath>
#include <time.h>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 100 + 5;
const int S = 20;

int tot;
LL factor[maxn];

LL mod_mul(LL a, LL b, LL n){
    LL res = 0;
    while(b){
        if(b & 1) res = (res + a) % n;
        a = (a + a) % n;
        b >>= 1;
    }
    return res;
}
LL mod_exp(LL a, LL b, LL n){
    LL res = 1;
    while(b){
        if(b & 1) res = mod_mul(res, a, n);
        a = mod_mul(a, a, n);
        b >>= 1;
    }
    return res;
}
LL gcd(LL a, LL b){
    if(a == 0) return 1;
    if(a < 0) return gcd(-a, b);
    while(b){
        LL tmp = a % b;
        a = b;
        b = tmp;
    }
    return a;
}

LL pollard_rho(LL x, LL c){
    LL i = 1, k = 2;
    LL x0 = rand() % x;
    LL y = x0;
    while(true){
        ++i;
        x0 = (mod_mul(x0, x0, x) + c) % x;
        LL d = gcd(y - x0, x);
        if(d != 1 && d != x) return d;
        if(y == x0) return x;
        if(i == k){
            y = x0;
            k += k;
        }
    }
}

bool miller_rabin(LL n) {
    if(n == 2 || n == 3 || n == 5 || n == 7 || n == 11) return true;
    if(n == 1 || !(n % 2) || !(n % 3) || !(n % 5) || !(n % 7) || !(n % 11)) return false;

    LL x, pre, u = n - 1, k = 0;

    while(!(u & 1)){
        ++k;
        u >>= 1;
    }

    srand((LL)time(NULL));
    for(int i = 0; i < S; ++i){                     //进行S次测试,S越大,结果越准确
        x = rand() % (n - 2) + 2;                   //在[2, n)中取随机数
        if(x % n == 0) continue;

        x = mod_exp(x, u, n);                       //计算x^u % n
        pre = x;
        for(int j = 0; j < k; ++j){
            x = mod_mul(x, x, n);
            if(x == 1 && pre != 1 && pre != n - 1)
                return false;
            pre = x;
        }
        if(x != 1) return false;
    }
    return true;
}

void findfactor(LL n){
    if(miller_rabin(n)){
        factor[tot++] = n;
        return;
    }

    LL p = n;
    while(p >= n){
        p = pollard_rho(p, rand() % (n - 1) + 1);
    }
    findfactor(p);
    findfactor(n / p);
}

int main(){
    LL t, x, tmp;
    cin >> t;
    while(t--){
        cin >> x;
        if(x == 1 || x == 2 || x == 3 || x == 4){
            cout << (4 - x) << endl;
            continue;
        }

        tmp = (LL)sqrt(x + 0.500);
        LL ans = 1e18;
        for(LL i = tmp; i > 1; --i){
            tot = 0;
            memset(factor, 0, sizeof(factor));
            findfactor(i);
            sort(factor, factor + tot);

            int flag = 1;
            for(int j = 1; j < tot; ++j){
                if(factor[j] == factor[j-1]) {flag = 0; break;}
            }
            if(flag) {ans = min(ans, x - i * i); break;}
        }
        for(LL i = tmp + 1; ; ++i){
            tot = 0;
            memset(factor, 0, sizeof(factor));
            findfactor(i);
            sort(factor, factor + tot);

            int flag = 1;
            for(int j = 1; j < tot; ++j){
                if(factor[j] == factor[j-1]) {flag = 0; break;}
            }
            if(flag) {ans = min(ans, i * i - x); break;}
        }
        cout << ans << endl;
    }
    return 0;
}


posted @ 2016-07-31 14:20  _Wilbert  阅读(108)  评论(0编辑  收藏  举报