codeforces Gym 100338H High Speed Trains (递推,高精度)
递推就好了,用二项式定理算出所有连边的方案数,减去不合法的方案,
每次选出一个孤立点,那么对应方案数就是上次的答案。
枚举选几个孤立点和选哪些,选到n-1个点的时候相当于都不选,只减1。
要用到高精度,直接开100*100的组合数数组会MLE,用滚动数组优化一下就好了。
不会java,python太伤了
#include<bits/stdc++.h> using namespace std; const int MAXN = 20000; struct bign { int len, s[MAXN]; bign () { memset(s, 0, sizeof(s)); len = 1; } bign (int num) { *this = num; } bign (const char *num) { *this = num; } bign operator = (const int num) { char s[MAXN]; sprintf(s, "%d", num); *this = s; return *this; } bign operator = (const char *num) { for(int i = 0; num[i] == '0'; num++) ; len = strlen(num); for(int i = 0; i < len; i++) s[i] = num[len-i-1] - '0'; return *this; } bign operator + (const bign &b) const { bign 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; } bign operator += (const bign &b) { *this = *this + b; return *this; } void clean() { while(len > 1 && !s[len-1]) len--; } bign operator * (const bign &b) { bign c; c.len = len + b.len; for(int i = 0; i < len; i++) { for(int j = 0; j < b.len; j++) { c.s[i+j] += s[i] * b.s[j]; } } for(int i = 0; i < c.len; i++) { c.s[i+1] += c.s[i]/10; c.s[i] %= 10; } c.clean(); return c; } bign operator *= (const bign &b) { *this = *this * b; return *this; } bign operator - (const bign &b) { bign 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; } c.clean(); return c; } bign operator -= (const bign &b) { *this = *this - b; return *this; } bign operator / (const bign &b) { bign c, f = 0; for(int i = len-1; i >= 0; i--) { f = f*10; f.s[0] = s[i]; while(f >= b) { f -= b; c.s[i]++; } } c.len = len; c.clean(); return c; } bign operator /= (const bign &b) { *this = *this / b; return *this; } bign operator % (const bign &b) { bign r = *this / b; r = *this - r*b; return r; } bign operator %= (const bign &b) { *this = *this % b; return *this; } bool operator < (const bign &b) { 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 bign &b) { 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 bign &b) { return !(*this > b) && !(*this < b); } bool operator != (const bign &b) { return !(*this == b); } bool operator <= (const bign &b) { return *this < b || *this == b; } bool operator >= (const bign &b) { return *this > b || *this == b; } string str() const { string res = ""; for(int i = 0; i < len; i++) res = char(s[i]+'0') + res; return res; } }; istream& operator >> (istream &in, bign &x) { string s; in >> s; x = s.c_str(); return in; } ostream& operator << (ostream &out, const bign &x) { out << x.str(); return out; } bign fpow(bign a,int b) { bign ret = 1; while(b){ if(b&1) ret *= a; a *= a; b >>= 1; } return ret; } const int maxn = 101; bign tab[maxn]; //typedef long long ll; bign C[2][maxn]; int main() { freopen("trains.in","r",stdin); freopen("trains.out","w",stdout); tab[1] = 0; int n; scanf("%d",&n); C[2&1][1] = C[2&1][0] = 1; for(int i = 2; i <= n; i++){ bign Full = fpow(2,i*(i-1)/2); int pre = i&1,cur = pre^1; C[cur][0] = 1; for(int j = 1; j <= i; j++) C[cur][j] = C[pre][j-1] + C[pre][j]; for(int j = 2; j < i; j++){ Full -= tab[j]*C[cur][j]; } tab[i] = Full - 1; } cout<<tab[n]<<endl; return 0; }