Spoj 9887 Binomial coefficients 构造

题目链接:点击打开链接

#include <cstdio>
#include <cstring>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAX_N = 507;
const long long INF = (long long)1e15;
typedef long long ll;
typedef pair<ll,ll> pii;
ll C[MAX_N][MAX_N];
vector<pii> ord;
ll get(ll nn, int m, ll A) {
    ll ret = 1;
    for (int i = 0; i < m; ++i) {
        if (ret > (A + nn + i - 1) / (nn + i)) return A + 1;
        ret *= (nn + i);
    }
    return ret;
}

void pre(ll n) {
    ll nn = n;
    for (int i = 1; i < 7; ++i) {
            nn *= i;
            ll l = 1, r = INF;
            while (l <= r) {
                ll mid = (l + r) >> 1;
                ll ans = get(mid, i, nn);
                if (ans > nn) r = mid - 1;
                else if (ans < nn) l = mid + 1;
                else {
                    ord.push_back(make_pair(mid + i - 1, i));
                    if (mid - 1 != i)
                        ord.push_back(make_pair(mid + i - 1, mid - 1));
                    break;
                }
            }
    }
}
int main() {
    int T;
    scanf("%d", &T);
    while (T-- > 0) {
        ll n;
        cin >> n;
        ord.clear();
        pre(n);
        C[0][0] = 1;
        for (int i = 1; i < MAX_N; ++i) {
            C[i][0] = C[i][i] = 1;
            for (int j = 1; j < i; ++j) {
                C[i][j] = C[i - 1][j] + C[i - 1][j - 1];
                if (C[i][j] > n) break;
                if (C[i][j] == n) {
                    ord.push_back(make_pair(i, j));
                    if (i - j != j) {
                        ord.push_back(make_pair(i, i - j));
                    }
                }
            }
        }
        sort(ord.begin(), ord.end());
        ord.erase(unique(ord.begin(), ord.end()), ord.end());
        int len = (int)ord.size();
        printf("%d\n", len);
        for (int i = 0; i < len; ++i) {
            if (i == 0) printf("(%lld,%lld)", ord[i].first, ord[i].second);
            else printf(" (%lld,%lld)", ord[i].first, ord[i].second);
        }
        puts("");
    }
    return 0;
}

Spoj 9887 Binomial coefficients 构造,布布扣,bubuko.com

时间: 2024-08-07 00:18:55

Spoj 9887 Binomial coefficients 构造的相关文章

compute Binomial Coefficient or combinations with dynamic programming

The ProblemWrite a function that takes two parameters n and k and returns the value of Binomial Coefficient C(n, k). For example, your function should return 6 for n = 4 and k = 2, and it should return 10 for n = 5 and k = 2. 1 def ComputeBinomialCoe

NWERC 2011 ABCDEH 题解

A:SPOJ NWERC11A A - Binomial coefficients 题解:点击打开链接 B: 点击打开链接 Bird tree 从下到上发现是个gcd的过程(辗转相除 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int main() { int T; scanf("%d", &T); while (T--) { i

The Inclusion-Exclusion Principle

The Inclusion-Exclusion Principle The inclusion-exclusion principle is an important combinatorial way to compute the size of a set or the probability of complex events. It relates the sizes of individual sets with their union. Statement The verbal fo

组合数取模(转载)

本文转自:http://blog.csdn.net/skywalkert/article/details/52553048 0. 写在前面 在程序设计中,可能会碰到多种类型的计数问题,其中不少涉及到组合数的计算,所以笔者写下这么一篇文章,期望能解决一些常规的组合数求模问题.以下部分内容改编自AekdyCoin的<组合数求模>,而且为了感谢他对(懵懂的)笔者的启发,这篇文章的标题与其文章相同.另外,感谢Picks将多项式运算的技巧在中国进行推广,感谢51nod提供了许多有趣的数论题目,感谢fot

Codeforces 520E. Pluses everywhere 数学

520E - Pluses everywhere/521C - Pluses everywhere Idea: Endagorion Preparation: gchebanov, DPR-pavlin Consider some way of placing all the pluses, and a single digit di (digits in the string are numbered starting from 0 from left to right). This digi

Xiao Ming&#39;s Hope 卢卡斯定理的推广。

Xiao Ming's Hope 题目抽象:求C(n,0),C(n,1),……,C(n,n)中奇数的个数. 思路:考察C(n,m)%2. 由卢卡斯定理    C(A,B)=C(a[n-1],b[n-1])*C(a[n-2],b[n-2])* ……*C(a[0],b[0])  ,其中a[i]为A的p进制位 要使C(n,m)%2==1,  那么对于n上的1的位,m上对应的位可以为0,1,(c(1,0)=c(1,1)=1). 对于n上为0的位,m上对应的位必须为0.(c(0,0)=1,c(0,1)=0

UVA10375 Choose and divide 质因数分解

质因数分解: Choose and divide Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Problem D: Choose and divide The binomial coefficient C(m,n) is defined as m! C(m,n) = -------- n!(m-n)! Given four natural n

[Lucas定理推广] hdu 4349 and poj 3219

hdu 4349 Xiao Ming's Hope 题意: 给n,求c(n,0),c(n,1)....c(n,n)中奇数的个数 思路: 因为只有奇偶区别,想到二进制 运用Lucas定理,将n,m(0~n) 化为二进制数 a,b为n,m的二进制数 根据定理 C(n,m)=C(a[n],b[n])*C(a[n-1],b[n-1])*...*C(a[0],b[0]) 然后因为C(0,1)=0.C(0,0)=1.C(1,0)=1.C(1,1)=1 为了保证C(n,m)=1 那么当n的二进制位上是0的时候

计算机程序设计艺术(第一卷) 基本算法 第3版pdf

下载地址:网盘下载 内容简介  · · · · · · <计算机程序设计艺术>系列著作对计算机领域产生了深远的影响.这一系列堪称一项浩大的工程,自1962年开始编写,计划出版7卷,目前已经出版了4卷.<美国科学家>杂志曾将这套书与爱因斯坦的<相对论>等书并列称为20世纪最重要的12本物理学著作.目前Knuth正将毕生精力投入到这部史诗性著作的撰写中.想了解本书最新信息,请访http://www-cs-faculty.stanford.edu/~knuth/taocp.h