`
445822357
  • 浏览: 740046 次
文章分类
社区版块
存档分类
最新评论

SPOJ NWERC11A_Binomial coefficients

 
阅读更多

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=34912#problem/A

解法:
枚举k,二分n

关键点:
1.枚举时候只考虑n - k >= k的情况,另一半由对称性得到
2.大数求组合数时,注意处理超范围的情况

细节处理:
1.对于C(2k' - 1, k'), n - k = k' - 1 < k' 所以枚举的时候对于一个k,n >= 2k
2.给定一个k值,那么C(2k, k)是最小值,如果最小值大于给定的值,那么结束枚举.可以发现,C(2k, k)增长的速度很快,所以枚举很快就能结束
3.二分n求组合数时,先一直乘,如果大于long long的最大值,那么除


#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <string>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
#include <bitset>
#include <fstream>
using namespace std;


//LOOP
#define FF(i, a, b) for(int i = (a); i < (b); ++i)
#define FE(i, a, b) for(int i = (a); i <= (b); ++i)
#define FED(i, b, a) for(int i = (b); i>= (a); --i)
#define REP(i, N) for(int i = 0; i < (N); ++i)
#define CLR(A,value) memset(A,value,sizeof(A))
#define FC(it, c) for(__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)


//OTHER
#define SZ(V) (int)V.size()
#define PB push_back
#define MP make_pair
#define all(x) (x).begin(),(x).end()


//INPUT
#define RI(n) scanf("%d", &n)
#define RII(n, m) scanf("%d%d", &n, &m)
#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)
#define RV(n, m, k, p, q) scanf("%d%d%d%d%d", &n, &m, &k, &p, &q)
#define RS(s) scanf("%s", s)


//OUTPUT
#define WI(n) printf("%d\n", n)
#define WS(n) printf("%s\n", n)


//debug
//#define online_judge
#ifndef online_judge
#define debugt(a) cout << (#a) << "=" << a << " ";
#define debugI(a) debugt(a) cout << endl
#define debugII(a, b) debugt(a) debugt(b) cout << endl
#define debugIII(a, b, c) debugt(a) debugt(b) debugt(c) cout << endl
#define debugIV(a, b, c, d) debugt(a) debugt(b) debugt(c) debugt(d) cout << endl
#else
#define debugI(v)
#define debugII(a, b)
#define debugIII(a, b, c)
#define debugIV(a, b, c, d)
#endif

typedef long long LL;
typedef unsigned long long ULL;
typedef vector <int> VI;
const int INF = 0x3f3f3f3f;
const double eps = 1e-10;
const int MOD = 100000007;
const int MAXN = 1000010;
const double PI = acos(-1.0);

const LL LL_MAX = (~(unsigned long long)0) >> 1;
const LL MAX = (LL)1e15 + 100;
const int MAX_INDEX = 1001;
LL f[MAX_INDEX][MAX_INDEX];

LL ipt;
typedef pair<LL, LL> PLL;
vector<PLL> v;
#define MP make_pair

LL C(LL a, LL b)
{
    LL ret = 1, ta = a - b + 1, tb = 1;
    while (ta <= a && tb <= b)
    {
        if (ret > LL_MAX / a)
            ret /= tb++;
        else
            ret *= a--;
    }
    if (ta > a)
        while (tb <= b) ret /= tb++;
    else
        return ipt + 1;
    return ret;
}

LL bse(LL l, LL r, LL k)
{
    LL m;
    while (l <= r)
    {
        m = (l + r) >> 1;
        if (C(m, k) <= ipt) l = m + 1;
        else r = m - 1;
    }
    return r;
}

void fun(LL n)
{
    int k = 2;
    while (C(2 * k, k) <= ipt)
    {
        LL t = bse(2 * k, 1000000000, k);
        if (C(t, k) == ipt)
        {
            v.push_back(MP(t, k));
            v.push_back(MP(t, t - k));
        }
        k++;
    }
}


int main()
{
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
    int kase;
    RI(kase);
    while (kase--)
    {
        v.clear();

        cin >> ipt;
        v.push_back(MP(ipt, 1));
        v.push_back(MP(ipt, ipt - 1));
        fun(ipt);

        sort(v.begin(), v.end());
        int size = unique(v.begin(), v.end()) - v.begin();
        cout << size << endl;
        REP(i, size)
        {
            if (i != 0) putchar(' ');
            cout << '(' << v[i].first << ',' << v[i].second << ')';
        }
        cout << endl;
    }
    return 0;
}



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics