hdu 4432杯具

题目还是挺简单的,就是硬算,先求出N的所有因数,然后一个个地加起来,最后再处理输出就行了。现场赛的时候,模板准备不足,这题居然打了一个多小时,唉,要是能早交20分钟,银牌就妥妥的了……杯具……

/*

 * hdu4432/win.cpp

 * Created on: 2012-10-26

 * Author    : ben

 */

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <cmath>

#include <ctime>

#include <iostream>

#include <algorithm>

#include <queue>

#include <set>

#include <map>

#include <stack>

#include <string>

#include <vector>

#include <deque>

#include <list>

#include <functional>

#include <numeric>

#include <cctype>

using namespace std;

typedef vector<pair<int, int> > Int_Pair;

typedef long long LL;

/*

 * 得到所有不超过N的素数表,存于容器pt中

 */

void get_prime_table(int N, vector<int> &pt) {

    vector<bool> ip;

    ip.resize(N + 1);

    fill(ip.begin(), ip.end(), true);

    int i, j, s, t = N - 1;

    for (i = 3; i <= N; i++) {

        s = (int) sqrt(i);

        for (j = 2; j <= s; j++) {

            if (i % j == 0)    break;

        }

        if (j <= s) {

            ip[i] = false;

            t--;

        }

    }

    pt.resize(t);

    t = 0;

    for(int i = 2; i <= N; i++) {

        if(ip[i]) {

            pt[t++] = i;

        }

    }

}

/**

 * p为素数表,存下sqrt(N)的素数即可,f保存结果

 * f[i].first表示N的一个素因数,f[i].second为这个素因子的个数

 */

void get_prime_factor(int N, Int_Pair &f, const vector<int> &p) {

    int i, t, n, pl = p.size();

    f.clear();

    for(i = 0; i < pl; i++) {

        t = p[i];

        if(N % t == 0) {

            n = 0;

            while(N % t == 0) {

                n++;    N /= t;

            }

            f.push_back(make_pair(t, n));

        }

        if(N == 1) {    break;    }

    }

    if(N > 1) {

        f.push_back(make_pair(N, 1));

    }

}



void dfs(int k, int now, Int_Pair &f, vector<int> &data, vector<int> &ret) {

    if(k == (int)f.size()) {

        ret.push_back(now);

        return ;

    }

    data[k] = 0;

    while(data[k] <= f[k].second) {

        dfs(k + 1, now, f, data, ret);

        data[k]++;

        now *= f[k].first;

    }

}

inline LL getbitsum(int n, int m) {

    LL ret = 0, t;

    while(n > 0) {

        t = n % m;

        ret += t * t;

        n /= m;

    }

    return ret;

}

void myprint(LL ans, int m) {

    char str[100];

    int I = 0;

    char c;

    while(ans > 0) {

        c = ans % m + '0';

        if(c > '9') {

            c = c - '9' - 1 + 'A';

        }

        str[I++] = c;

        ans /= m;

    }

    str[I] = 0;

    reverse(str, str + I);

    puts(str);

}

int main() {

#ifndef ONLINE_JUDGE

    freopen("data.in", "r", stdin);

#endif

    vector<int> prime_table;

    get_prime_table(100000, prime_table);

    int n, m;

    LL ans;

    while(scanf("%d%d", &n, &m) == 2) {

        Int_Pair factor;

        get_prime_factor(n, factor, prime_table);

        vector<int> data;

        data.resize(factor.size());

        vector<int> result;

        dfs(0, 1, factor, data, result);

        int len = result.size();

        ans = 0;

        for(int i = 0; i < len; i++) {

            ans += getbitsum(result[i], m);

        }

        myprint(ans, m);

    }

    return 0;

}

你可能感兴趣的:(HDU)