[toj4111] Binomial efficient

求(nk)(mod232)

  • (nk)=n!k!(n?k)!
  • 根据上式,只需要枚举各质数的指数即可。即得到如下形式:

2a1×3a2×5a3…2b1×3b2×5b3?×2c1×3c2×5c3…=2a1?b1?c1×3a2?b2?c2×5a3?b3?c3…

  • 先筛出质数,然后枚举各质数,对每个质数,算出各指数即可
/* **********************************************

  File Name: 4111.cpp

  Auther: [email protected]

  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

时间: 2024-10-06 15:06:46

[toj4111] Binomial efficient的相关文章

论文笔记之:Fully Convolutional Attention Localization Networks: Efficient Attention Localization for Fine-Grained Recognition

Fully Convolutional Attention Localization Networks: Efficient Attention Localization for Fine-Grained Recognition   细粒度的识别(Fine-grained recognition)的挑战性主要来自于 类内差异(inter-class differences)在细粒度类别中通常是局部的,细微的:类间差异(intra-class differences)由于姿态的变换而导致很大.为了

Web Pages - Efficient Paging Without The WebGrid

Web Pages - Efficient Paging Without The WebGrid If you want to display your data over a number of pages using WebMatrix Beta1, you have two options. One is to use the built-in paging support that comes with the WebGrid helper. But that means that yo

基本概率分布Basic Concept of Probability Distributions 4: Negative Binomial Distribution

PDF version PMF Suppose there is a sequence of independent Bernoulli trials, each trial having two potential outcomes called "success" and "failure". In each trial the probability of success is $p$ and of failure is $(1-p)$. We are obs

Efficient ticket lock synchronization implementation using early wakeup in the presence of oversubscription

A turn-oriented thread and/or process synchronization facility obtains a ticket value from a monotonically increasing ticket counter and waits until a memory location contains a value equal to the ticket value, yielding the processor between polls of

Thrift---more efficient transport protocol.

Thrift  是什么?  Thrift源于大名鼎鼎的facebook之手,在2007年facebook提交Apache基金会将Thrift作为一个开源项目,对于当时的facebook来说创造thrift是为了解决facebook系统中各系统间大数据量的传 输通信以及系统之间语言环境不同需要跨平台的特性.所以thrift可以支持多种程序语言,例如:  C++, C#, Cocoa, Erlang, Haskell, Java, Ocami, Perl, PHP, Python, Ruby, Sm

poj 2249 Binomial Showdown(组合数 公式优化)

//  组合数学,开始了-- 题目地址 : poj 2249 Binomial Showdown Description In how many ways can you choose k elements out of n elements, not taking order into account? Write a program to compute this number. Input The input will contain one or more test cases. Eac

基本概率分布Basic Concept of Probability Distributions 1: Binomial Distribution

PDF下载链接 PMF If the random variable $X$ follows the binomial distribution with parameters $n$ and $p$, we write $X \sim B(n, p)$. The probability of getting exactly $x$ successes in $n$ trials is given by the probability mass function: $$f(x; n, p) =

UVA 11020 - Efficient Solutions(set)

UVA 11020 - Efficient Solutions 题目链接 题意:每个人有两个属性值(x, y),对于每一个人(x,y)而言,当有另一个人(x', y'),如果他们的属性值满足x' < x, y' <= y或x' <= x, y' < y的话,这个人会失去优势,每次添加一个人,并输出当前优势人个数 思路:由于每个人失去优势后,不可能再得到优势,所以失去优势就可以当成删去这些点,这样的话,就可以用一个multiset来维护点集,每次加入一个点,利用lowerbound,

[2011山东ACM省赛] Binomial Coeffcients(求组合数)

Binomial Coeffcients nid=24#time" style="padding-bottom:0px; margin:0px; padding-left:0px; padding-right:0px; color:rgb(83,113,197); text-decoration:none; padding-top:0px"> Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述 输入