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;
}



  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
洛谷的SPOJ需要注册一个SPOJ账号并进行绑定才能进行交题。您可以按照以下步骤进行注册: 1. 打开洛谷网站(https://www.luogu.com.cn/)并登录您的洛谷账号。 2. 在网站顶部导航栏中找到“题库”选项,将鼠标悬停在上面,然后选择“SPOJ”。 3. 在SPOJ页面上,您会看到一个提示,要求您注册SPOJ账号并进行绑定。点击提示中的链接,将会跳转到SPOJ注册页面。 4. 在SPOJ注册页面上,按照要求填写您的用户名、密码和邮箱等信息,并完成注册。 5. 注册完成后,返回洛谷网站,再次进入SPOJ页面。您会看到一个输入框,要求您输入刚刚注册的SPOJ用户名。输入用户名后,点击“绑定”按钮即可完成绑定。 现在您已经成功注册并绑定了SPOJ账号,可以开始在洛谷的SPOJ题库上刷题了。祝您顺利完成编程练习!\[1\]\[2\] #### 引用[.reference_title] - *1* *3* [(洛谷入门系列,适合洛谷新用户)洛谷功能全解](https://blog.csdn.net/rrc12345/article/details/122500057)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [luogu p7492 序列](https://blog.csdn.net/zhu_yin233/article/details/122051384)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值