G++会TLE。。。C++才能AC。。。效率有点低。。。后来去学了更高效的算法。。。。dp代表答案。。sum代表合法的森林。。。
第一次程序:
#include <iostream> #include <queue> #include <stack> #include <map> #include <set> #include <bitset> #include <cstdio> #include <algorithm> #include <cstring> #include <climits> #include <cstdlib> #include <cmath> #include <time.h> #define maxn 200005 #define maxm 1000005 #define eps 1e-10 #define mod 1000000007 #define INF 999999999 #define PI (acos(-1.0)) #define lowbit(x) (x&(-x)) #define mp make_pair #define ls o<<1 #define rs o<<1 | 1 #define lson o<<1, L, mid #define rson o<<1 | 1, mid+1, R #pragma comment(linker, "/STACK:16777216") typedef long long LL; typedef unsigned long long ULL; //typedef int LL; using namespace std; LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;} LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;} void scanf(int &__x){__x=0;char __ch=getchar();while(__ch==' '||__ch=='\n')__ch=getchar();while(__ch>='0'&&__ch<='9')__x=__x*10+__ch-'0',__ch = getchar();} LL gcd(LL _a, LL _b){if(!_b) return _a;else return gcd(_b, _a%_b);} // head int f[1 << 15][15], sum[1 << 15]; int l[15], r[15]; int n; void read(void) { scanf("%d", &n); for(int i = 1; i <= n; i++) scanf("%d%d", &l[i], &r[i]); } void work(void) { int tt = (1 << n) - 1; for(int i = 0; i <= tt; i++) { sum[i] = 0; for(int j = 1; j <= n; j++) f[i][j] = 0; } for(int i = 0; i <= tt; i++) { int s = 0, first, last; for(int j = 1; j <= n; j++) if(i & (1 << j - 1)) { s++; last = j; if(s == 1) first = j; } if(s == 1) { if(l[last] == 1) f[i][last] = sum[i] = 1; continue; } for(int j = 1; j <= n; j++) { if(i & (1 << j - 1)) { f[i][j] = (f[i][j] + sum[i - (1 << j - 1)]) % mod; int x; if(j == last) x = first; else x = last; int t = i - (1 << j - 1) - (1 << x - 1); for(int k = t; k; k = (k - 1) & t) f[i][j] = (f[i][j] + (LL)f[k + (1 << j - 1)][j] * sum[i - k - (1 << j - 1)] % mod) % mod; if(s >= l[j] && s <= r[j]) sum[i] = (sum[i] + f[i][j]) % mod; } } } printf("%d\n", sum[tt]); } int main(void) { int _; while(scanf("%d", &_)!=EOF) { while(_--) { read(); work(); } } return 0; }
第二次程序:
#include <iostream> #include <queue> #include <stack> #include <map> #include <set> #include <bitset> #include <cstdio> #include <algorithm> #include <cstring> #include <climits> #include <cstdlib> #include <cmath> #include <time.h> #define maxn 1005 #define maxm 100005 #define eps 1e-10 #define mod 1000000007 #define INF 0x3f3f3f3f #define PI (acos(-1.0)) #define lowbit(x) (x&(-x)) #define mp make_pair #define ls o<<1 #define rs o<<1 | 1 #define lson o<<1, L, mid #define rson o<<1 | 1, mid+1, R #pragma comment(linker, "/STACK:16777216") typedef long long LL; typedef unsigned long long ULL; //typedef int LL; using namespace std; LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;} LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;} void scanf(int &__x){__x=0;char __ch=getchar();while(__ch==' '||__ch=='\n')__ch=getchar();while(__ch>='0'&&__ch<='9')__x=__x*10+__ch-'0',__ch = getchar();} LL gcd(LL _a, LL _b){if(!_b) return _a;else return gcd(_b, _a%_b);} // head LL dp[1 << 15], sum[1 << 15]; int l[15], r[15]; int n; void init(void) { memset(dp, 0, sizeof dp); memset(sum, 0, sizeof sum); } void read(void) { scanf("%d", &n); for(int i = 1; i <= n; i++) scanf("%d%d", &l[i], &r[i]); } void work(void) { sum[0] = 1; for(int i = 1; i < (1 << n); i++) { int s = 0, last, first; for(int j = 1; j <= n; j++) if(1 & (i >> j - 1)) { s++; last = j; if(s == 1) first = j; } if(s == 1) { if(l[last] == 1) dp[i] = sum[i] = 1; continue; } int x = first; for(int j = 1; j <= n; j++) if(1 & (i >> j - 1)) { if(s >= l[j] && s <= r[j]) dp[i] = (dp[i] + sum[i - (1 << j - 1)]) % mod; } sum[i] = (sum[i] + dp[(1 << x - 1)] * sum[i - (1 << x - 1)] % mod) % mod; int t = i - (1 << x - 1); for(int j = t; j; j = (j - 1) & t) { sum[i] = (sum[i] + dp[(1 << x - 1) + j] * sum[i - j - (1 << x - 1)] % mod) % mod; } } printf("%I64d\n", dp[(1 << n) -1]); } int main(void) { int _; while(scanf("%d", &_)!=EOF) { while(_--) { init(); read(); work(); } } return 0; }