HDU 4196 Remoteland

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <map>
#include <ctime>
#include <queue>
#include <cmath>
#include <bitset>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef pair<int, int> pii;
const int maxn = 5111111;
const int SIZE = 1111111;
const int mod = 1000000007;
const double eps = 1e-10;
bitset<maxn + 111> vis;
int p[SIZE], size;
void init() {
    int i, j;
    for (i = 3; i * i <= maxn; i += 2)
        if (!vis[i])
            for (j = i * i; j <= maxn; vis[j] = 1, j += i)
                ;

    p[size++] = 2;
    for (i = 3; i <= maxn; i += 2)
        if (!vis[i])
            p[size++] = i;
}
LL POW(LL a, LL b) {
    LL res = 1;
    while (b) {
        if (b & 1)
            res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}
int getnum(int n, int p) {
    int cnt = 0;
    while (n) {
        cnt += n / p;
        n /= p;
    }
    return cnt;
}
LL go(int n) {
    LL ans = 1, tmp = 1;
    int t, j = 0;
    for (int i = 0; p[i] <= n; ++i) {
        t = getnum(n, p[i]);
        //cout << t << " " << j << endl;
        if (t == 1 && j) {
            if (j & 1)
                ans = ans * POW(tmp, j - 1) % mod;
            else
                ans = ans * POW(tmp, j) % mod;
            break;
        }
        if (j == t)
            tmp = tmp * p[i] % mod;
        else {
            if (j & 1)
                ans = ans * POW(tmp, j - 1) % mod;
            else
                ans = ans * POW(tmp, j) % mod;
            j = t;
            tmp = p[i];
        }
    }
    return ans;
}
int main() {
    init();
    int n;
    while (~scanf("%d", &n) && n) {
        printf("%d\n", (int) go(n));
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章