BZOJ 1875: [SDOI2009]HH去散步( dp + 矩陣快速冪 )

來源:互聯網
上載者:User

標籤:

把雙向邊拆成2條單向邊, 用邊來轉移...然後矩陣乘法+快速冪最佳化 

---------------------------------------------------------------------------------------------

#include<cstdio>#include<cstring>#include<algorithm> using namespace std; const int MOD = 45989;const int maxn = 29;const int maxm = 129; int N, M, T, A, B;int U[maxm], V[maxm], Id[maxm], f[maxm]; struct matrix {int a[maxm][maxm];matrix() {memset(a, 0, sizeof a);}matrix operator * (const matrix &o) const {matrix ret;for(int i = 0; i < M; i++)for(int k = 0; k < M; k++)for(int j = 0; j < M; j++)(ret.a[i][j] += a[i][k] * o.a[k][j]) %= MOD;return ret;}matrix operator = (const matrix &o) {memcpy(a, o.a, sizeof a);return *this;}void Unit() {for(int i = 0; i < M; i++)a[i][i] = 1;}} mat, base; void init() {scanf("%d%d%d%d%d", &N, &M, &T, &A, &B);for(int i = 0; i < M; i++) {scanf("%d%d", U + i, V + i);U[i + M] = V[i];V[i + M] = U[i];Id[i] = Id[i + M] = i;}} int main() {init();M <<= 1;for(int i = 0; i < M; i++)for(int j = 0; j < M; j++)if(Id[i] != Id[j] && V[i] == U[j]) base.a[j][i] = 1;mat.Unit();for(T--; T; T >>= 1, base = base * base)if(T & 1) mat = mat * base;memset(f, 0, sizeof f);for(int i = 0; i < M; i++)if(U[i] == A) f[i] = 1;int ans = 0;for(int i = 0; i < M; i++) if(V[i] == B)for(int j = 0; j < M; j++)if((ans += mat.a[i][j] * f[j]) >= MOD) ans -= MOD;printf("%d\n", ans);return 0;} 

---------------------------------------------------------------------------------------------

1875: [SDOI2009]HH去散步Time Limit: 20 Sec  Memory Limit: 64 MB
Submit: 1027  Solved: 469
[Submit][Status][Discuss]DescriptionHH有個一成不變的習慣,喜歡飯後百步走。所謂百步走,就是散步,就是在一定的時間 內,走過一定的距離。 但是同時HH又是個喜歡變化的人,所以他不會立刻沿著剛剛走來的路走回。 又因為HH是個喜歡變化的人,所以他每天走過的路徑都不完全一樣,他想知道他究竟有多 少種散步的方法。 現在給你學校的地圖(假設每條路的長度都是一樣的都是1),問長度為t,從給定地 點A走到給定地點B共有多少條合格路徑Input第一行:五個整數N,M,t,A,B。其中N表示學校裡的路口的個數,M表示學校裡的 路的條數,t表示HH想要散步的距離,A表示散步的出發點,而B則表示散步的終點。 接下來M行,每行一組Ai,Bi,表示從路口Ai到路口Bi有一條路。資料保證Ai = Bi,但 不保證任意兩個路口之間至多隻有一條路相串連。 路口編號從0到N − 1。 同一行內所有資料均由一個空格隔開,行首行尾沒有多餘空格。沒有多餘空行。 答案模45989。Output一行,表示答案。Sample Input4 5 3 0 0
0 1
0 2
0 3
2 1
3 2Sample Output4HINT

對於30%的資料,N ≤ 4,M ≤ 10,t ≤ 10。
對於100%的資料,N ≤ 20,M ≤ 60,t ≤ 230,0 ≤ A,B

Source

Day1

 

BZOJ 1875: [SDOI2009]HH去散步( dp + 矩陣快速冪 )

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.