1. 程式人生 > 實用技巧 >P4821 [中山市選]生成樹

P4821 [中山市選]生成樹

題目連結

我們可以看一下題目中給的這張圖。

首先,樹是沒有環的,所以我們要把所有的環上的邊都刪去一條。

我們可以現在每個五邊形上刪去一條邊。

但刪完之後我們會發現,裡面還有一圈。

這時候,我們就要在這裡面隨便選一條邊刪去。

也就是,我們需要把n-1個五邊形刪去一條邊,再把剩下的一個刪掉兩條邊

那麼方案數就是\(4 \times n \times 5 ^ {n-1}\)

程式碼

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define LL long long
int n,t;
const int p = 2007;
LL ksm(LL a,LL b)
{
	LL res = 1;
	for(; b; b >>= 1)
	{
		if(b & 1) res = res * a % p;
		a = a * a % p;
	}
	return res;
}
int main()
{
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		printf("%lld\n",n* 4 *ksm(5,n-1) % p);
	}
	return 0;
}

聽說,這題還可以用矩陣樹過去,但蒟蒻我不會QAQ。

不知不覺又水了一道題。