标签:数学
/* **********************************************
  File Name: 4111.cpp
  Auther: zhengdongjian@tju.edu.cn
  Created Time: 2015年08月17日 星期一 19时46分13秒
*********************************************** */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll MOD = 1LL << 32;
const int MAX = 1000007;
bool is_prime[MAX];
int prime[MAX], tot;
int times[MAX];
int get_prime() {
    memset(is_prime, true, sizeof(is_prime));
    for (int i = 2; i <= MAX / i; ++i) {
        for (int j = i * i; j < MAX; j += i) {
            is_prime[j] = false;
        }
    }
    tot = 0;
    for (int i = 2; i < MAX; ++i) {
        if (is_prime[i]) {
            prime[tot++] = i;
        }
    }
    return tot;
}
int gao(int p, int n) {
    //printf("gao %d, %d: ", p, n);
    int sum = 0;
    for (ll i = p; i <= n; i *= p) {
        sum += n / i;
    }
    //printf("%d\n", sum);
    return sum;
}
ui fast_pow(ui a, int n) {
    ui res = 1;
    while (n) {
        if (n & 1) res *= a;
        a *= a;
        n >>= 1;
    }
    return res;
}
int main() {
    ios::sync_with_stdio(false);
    get_prime();
    int T;
    cin >> T;
    while (T--) {
        int n, k;
        cin >> n >> k;
        if (k == 0) {
            cout << 1 << endl;
            continue;
        }
        memset(times, 0, sizeof(times));
        for (int i = 0; prime[i] <= n; ++i) {
            times[i] += gao(prime[i], n);
            times[i] -= gao(prime[i], k);
            times[i] -= gao(prime[i], n - k);
        }
        ui res = 1;
        for (int i = 0; prime[i] <= n; ++i) {
            //printf("pair %d^%d\n", prime[i], times[i]);
            res *= fast_pow(prime[i], times[i]);
            res %= MOD;
        }
        cout << res << endl;
    }
    return 0;
}
版权声明:那泥烤去看鸭o o
标签:数学
原文地址:http://blog.csdn.net/bit_line/article/details/47736739