SPOJ NWERC11A_Binomial coefficients
来源:互联网 发布:mac rar文件 编辑:程序博客网 时间:2024/05/18 22:11
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)#endiftypedef 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_pairLL 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;}
- SPOJ NWERC11A_Binomial coefficients
- SPOJ NWERC11A Binomial coefficients
- Spoj 9887 Binomial coefficients 构造
- Binomial coefficients
- Binomial Coefficients
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- SPOJ
- poj 2699 The Maximum Number of Strong Kings 网络流
- 分布式大作业p2p聊天室
- Android Fragment应用实战,使用碎片向ActivityGroup说再见
- 积累的VC编程--图标、光标及位图(转)
- 优酷客户端或者网页浏览器 播放视频花屏的解决方法
- SPOJ NWERC11A_Binomial coefficients
- 杭电字符串之1106(模拟)
- 逻辑表达式的运用
- Minimum Inversion Number(线段树求逆序数小总结)
- oracle版本历史
- 黑马程序员-面向对象(stati、this关键字)
- 求是否是闰年
- hnuoj LRU Caching (链表,结构体指针 / vector)
- matlab字符串处理