【BZOJ1875】【矩陣乘法】[SDOI2009]HH去散步

來源:互聯網
上載者:User

標籤:

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<n,0 ≤="" ai,bi="" <n。<="" p="">

【分析】

這個,能算數學吧...(其實是DP)

簡單的矩陣乘法,練下手。

  1 /*  2 宋代朱敦儒  3 《西江月·世事短如春夢》  4 世事短如春夢,人情薄似秋雲。不須計較苦勞心。萬事原來有命。  5 幸遇三杯酒好,況逢一朵花新。片時歡笑且相親。明日陰晴未定。   6 */  7 #include <cstdio>  8 #include <cstring>  9 #include <algorithm> 10 #include <cmath> 11 #include <queue> 12 #include <vector> 13 #include <iostream> 14 #include <string> 15 #include <ctime> 16 #define LOCAL 17 const int MAXN = 60 * 2 + 10; 18 const int MOD = 45989; 19 const double Pi = acos(-1.0); 20 long long G = 15;//原根  21 const int MAXM = 60 * 2 + 10;  22 using namespace std; 23 //讀入最佳化  24 void read(int &x){ 25     char ch;x = 0; 26     int flag = 1; 27     ch = getchar(); 28     while (ch < ‘0‘ || ch > ‘9‘) {if (ch == ‘0‘) flag = -1; ch = getchar();} 29     while (ch >= ‘0‘ && ch <= ‘9‘) {x = x * 10 + (ch - ‘0‘); ch = getchar();} 30     x *= flag; 31 } 32  33 struct Edge{ 34        int u, v; 35 }edge[MAXM]; 36 int M; 37 struct Matrix{ 38        int num[MAXN][MAXN]; 39        //Matrix(){memset(num, 0, sizeof(num));} 40        Matrix operator * (const Matrix &b){ 41               Matrix c; 42               memset(c.num, 0, sizeof(c.num)); 43               for (int i = 1; i < M; i++) 44               for (int j = 1; j < M; j++) 45               for (int k = 1; k < M; k++){ 46                   //if (i == 5 && j == 9) 47                   //printf(""); 48                   c.num[i][j] = (c.num[i][j] + num[i][k] * b.num[k][j]) % MOD; 49               } 50               return c; 51        } 52 }x1, x2, x3; 53 Matrix pow(Matrix a, int b){ 54        if (b == 1) return a; 55        Matrix tmp = pow(a, b / 2); 56        if (b % 2 == 0) return tmp * tmp; 57        else return (tmp * tmp) * a;  58 } 59 int n, m, t, A, B, head[MAXN], next[MAXM]; 60  61 //無向邊  62 void addEdge(int u, int v){ 63      edge[M].u = u; edge[M].v = v; 64      next[M] = head[u]; 65      head[u] = M++; 66       67      edge[M].u = v; edge[M].v = u; 68      next[M] = head[v]; 69      head[v] = M++; 70 } 71 void init(){ 72      memset(x1.num, 0, sizeof(x1.num)); 73      memset(x2.num, 0, sizeof(x2.num)); 74      memset(x3.num, 0, sizeof(x3.num)); 75      memset(head, -1, sizeof(head)); 76      read(n);read(m); 77      read(t);read(A);read(B); 78      M = 2;//注意這裡要人為規定一個源  79      for (int i = 1; i <= m; i++){ 80          int u, v; 81          read(u);read(v); 82          addEdge(u, v); 83      } 84 }  85 void prepare(){ 86      for (int i = head[A]; i != -1; i = next[i]) x1.num[1][i]++; 87       88      for (int i = 2; i < M; i++) 89      for (int j = 2; j < M; j++) 90      if (edge[i].v == edge[j].u && (i ^ 1) != j) x2.num[i][j]++;//注意這裡是以邊來相連  91 /* 92      for (int i = 1; i < M; i++){ 93          for (int j = 1; j < M ; j++) printf("%d ", x2.num[i][j]); 94          printf("\n"); 95      }*/ 96 } 97 void work(){ 98      int Ans = 0; 99      x1 = x1 * pow(x2, t - 1); 100      /*for (int i = 1; i < M; i++){101          for (int j = 1; j < M ; j++) printf("%d ", x1.num[i][j]);102          printf("\n");103      }*/104      for (int i = head[B]; i != -1; i = next[i]) Ans = (Ans + x1.num[1][i ^ 1]) % MOD;105      printf("%d\n", Ans); 106 }107 108 int main(){109     init();110     prepare();111     work();112     return 0;113 }
View Code

 

【BZOJ1875】【矩陣乘法】[SDOI2009]HH去散步

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.