幻想鄉是個什麼東西??(逃
就是設\(dp[i]\)表示至多i個公司修建道路,那麼我們有\(ans=dp[n-1]-dp[n-2]+dp[n-3]......\)balabala(就是那個容斥公式嘛qwqwq)
然後每次都跑一次矩陣樹定理qwqwq
但是這樣的時間複雜度是\(o(n^32^)\)???所以說。。。跑的。。。2s。。。很勉強qwqwq
**如下:
#include#include#include#include#include#include#define maxn 100010
#define mod 1000000007
using namespace std;
int n,maxx,ans;
int cnt[maxn],dp[20][20];
struct node;
vectornode[20];
inline int getnum(int x)
inline int matrix_tree()
} cur_ans=(1ll*cur_ans*dp[i][i])%mod;
} return cur_ans;
}inline int calc(int x)
} }return matrix_tree();
}int main()
); }
} for(int i=0;i<=maxx;i++) cnt[i]=getnum(i);
for(int i=0;i<1<<(n-1);++i)
ans=(ans+(((n-1-cnt[i])&1)?mod-calc(i):calc(i)))%mod;
printf("%d\n",ans);
return 0;
}
SHOI2016 黑暗前的幻想鄉
點此看題 好久沒有做過這麼版的題了 首先你要知道指數級容斥,也就是我們列舉乙個公司集合,表示我們只在這些公司中選邊,那麼我們就用全部公司 少乙個公司 少兩個公司 來計算就行。知道邊算生成樹,不就是矩陣樹定理麼?include include include include using namespa...
SHOI2016 黑暗前的幻想鄉
如果沒有建築公司的限制,那麼就是個 mathrm 板子 其實有了也一樣 發現 n leq 17 考慮容斥 每次欽定一些建築公司,計算它們包含的邊的生成樹的方案數 複雜度 mathrm 2 nn 3 include include include include define rg register ...
SHOI2016 黑暗前的幻想鄉
嘟嘟嘟 這題昨天看覺得沒有思路,今天看了一眼覺得就是個水題。首先如果不考慮每乙個人只能選一條路的話,那就是求一張無向圖 有重邊,有自環 的生成樹個數。這個直接用矩陣樹定理 高斯消元求解行列式即可解決。現在有了限制,怎麼辦?容斥!其實和 zjoi2016 小星星這道題有點像。想一下,如果乙個公司修了兩...