SGU 476 Coach's Trouble(集合独立性、容斥、高精度)
来源:互联网 发布:js通过标签名获取元素 编辑:程序博客网 时间:2024/05/02 01:33
题目链接;
SGU 476 Coach’s Trouble
题意:
给一个
数据范围:
分析:
我们先考虑
化简一下得到递推式:
考虑到数据范围需要用大数。
然后我们来考虑
如果只考虑一个限制条件,那么每个限制条件把限制条件用上后就相当与刚刚多算了
如果考虑两个限制条件,那么这两个限制条件必须是彼此独立的,也就是两个集合没有交集,然后根据容斥原理,这时候就要加上
依次递推。。。
因为
我们可以记录当前已经搜索
另外这题的
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <climits>#include <cmath>#include <ctime>#include <cassert>#define IOS ios_base::sync_with_stdio(0); cin.tie(0);using namespace std;typedef long long ll;const int MAX_N = 1005;const int SIZE = 11000;struct BigInteger { int len, s[SIZE + 5]; BigInteger () { memset(s, 0, sizeof(s)); len = 1; } BigInteger operator = (const char *num) { //字符串赋值 memset(s, 0, sizeof(s)); len = strlen(num); for(int i = 0; i < len; i++) s[i] = num[len - i - 1] - '0'; return *this; } BigInteger operator = (const int num) { //int 赋值 memset(s, 0, sizeof(s)); char ss[SIZE + 5]; sprintf(ss, "%d", num); *this = ss; return *this; } BigInteger (int num) { *this = num; } BigInteger (char* num) { *this = num; } string str() const { //转化成string string res = ""; for(int i = 0; i < len; i++) res = (char)(s[i] + '0') + res; if(res == "") res = "0"; return res; } BigInteger clean() { while(len > 1 && !s[len - 1]) len--; return *this; } BigInteger operator + (const BigInteger& b) const { BigInteger c; c.len = 0; for(int i = 0, g = 0; g || i < max(len, b.len); i++) { int x = g; if(i < len) x += s[i]; if(i < b.len) x += b.s[i]; c.s[c.len++] = x % 10; g = x / 10; } return c.clean(); } BigInteger operator - (const BigInteger& b) { BigInteger c; c.len = 0; for(int i = 0, g = 0; i < len; i++) { int x = s[i] - g; if(i < b.len) x -= b.s[i]; if(x >= 0) g = 0; else { g = 1; x += 10; } c.s[c.len++] = x; } return c.clean(); } BigInteger operator * (const int num) const { int c = 0, t; BigInteger pro; for(int i = 0; i < len; ++i) { t = s[i] * num + c; pro.s[i] = t % 10; c = t / 10; } pro.len = len; while(c != 0) { pro.s[pro.len++] = c % 10; c /= 10; } return pro.clean(); } BigInteger operator * (const BigInteger& b) const { BigInteger c; for(int i = 0; i < len; i++) { for(int j = 0; j < b.len; j++) { c.s[i + j] += s[i] * b.s[j]; c.s[i + j + 1] += c.s[i + j] / 10; c.s[i + j] %= 10; } } c.len = len + b.len + 1; return c.clean(); } BigInteger operator / (const BigInteger &b) const { BigInteger c, f; for(int i = len - 1; i >= 0; --i) { f = f * 10; f.s[0] = s[i]; while(f >= b) { f = f - b; ++c.s[i]; } } c.len = len; return c.clean(); } //高精度取模 BigInteger operator % (const BigInteger &b) const{ BigInteger r; for(int i = len - 1; i >= 0; --i) { r = r * 10; r.s[0] = s[i]; while(r >= b) r = r - b; } r.len = len; return r.clean(); } bool operator < (const BigInteger& b) const { if(len != b.len) return len < b.len; for(int i = len - 1; i >= 0; i--) if(s[i] != b.s[i]) return s[i] < b.s[i]; return false; } bool operator > (const BigInteger& b) const { return b < *this; } bool operator <= (const BigInteger& b) const { return !(b < *this); } bool operator == (const BigInteger& b) const { return !(b < *this) && !(*this < b); } bool operator != (const BigInteger &b) const { return !(*this == b); } bool operator >= (const BigInteger &b) const { return *this > b || *this == b; } friend istream & operator >> (istream &in, BigInteger& x) { string s; in >> s; x = s.c_str(); return in; } friend ostream & operator << (ostream &out, const BigInteger& x) { out << x.str(); return out; }};BigInteger f[MAX_N];void init(){ f[0] = f[1] = 1; for(int i = 2; i < MAX_N; ++i) { f[i] = f[i - 1] * ((3 * i - 1) * (3 * i - 2) / 2); }}int num[25], vis[4010], data[25][5];void dfs(int cur, int total, int limit){ num[total]++; for(int i = cur; i < limit; ++i) { int flag = 0; if(vis[data[i][0]] == 0 && vis[data[i][1]] == 0 && vis[data[i][2]] == 0) { flag = 1; } if(flag == 0) continue; for(int j = 0; j < 3; ++j) { vis[data[i][j]] = 1; } dfs(i + 1, total + 1, limit); for(int j = 0; j < 3; ++j) { vis[data[i][j]] = 0; } }}int main(){ init(); int n, k; while(cin >> n >> k) { for(int i = 0; i < k; ++i) { cin >> data[i][0] >> data[i][1] >> data[i][2]; } BigInteger ans = 0; memset(vis, 0, sizeof(vis)); memset(num, 0, sizeof(num)); dfs(0, 0, k); for(int i = 0; i <= min(k, n); i += 2) { ans = ans + f[n - i] * num[i]; } for(int i = 1; i <= min(k, n); i += 2) { ans = ans - f[n - i] * num[i]; } cout << ans.clean() << endl; } return 0;}
0 0
- SGU 476 Coach's Trouble(集合独立性、容斥、高精度)
- SGU 476 Coach's Trouble(高精度,dfs,容斥)
- SGU 476 Coach's Trouble 大数模拟
- SGU 294 He's Circle(Polya原理+高精度)
- POJ2103 Jackpot(容斥+高精度)
- SGU - 112 - ab-ba (大数高精度)
- SGU 200 Cracking RSA(高斯消元+高精度)
- HDU 3208 Integer's Power(容斥原理、指数和、高精度求开根)
- ACdream 1420 High Speed Trains(容斥原理+高精度)
- [BZOJ2024][SHOI2009] 舞会(dp+容斥原理+高精度)
- UVA 10497 - Sweet Child Makes Trouble(DP+高精度)
- HDU 2832 Snail’s trouble(数学题)
- king's trouble II (单调栈)
- HDU 2832-Snail’s trouble(分治)
- Eddy's爱好(容斥原理)
- [IDE] Zoran coach 11s ...
- uva 10458 - Cricket Ranking(容斥+高精度)
- king's trouble II
- 两个栈实现一个队列,两个队列实现一个栈
- 表达式树(公共表达式消除 uva 12219)
- Python读取文件
- Android获取屏幕宽高
- 【解决】Ubuntu无法识别U盘分区(NTFS移动硬盘无法挂载)
- SGU 476 Coach's Trouble(集合独立性、容斥、高精度)
- 对于父类的私有属性,子类是从哪里访问到的?
- Android Art Hook 技术方案
- nginx php问题集
- 折半查找(二分法)
- 使用游标更新和删除数据
- 解决Servlet中处理GET请求乱码问题
- ffmpeg AVRational结构体介绍
- Linq查询的简单使用