Advertisement
Guest User

Untitled

a guest
Jul 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. /******************************************************************************
  2. * *
  3. * XXIII Olimpiada Informatyczna *
  4. * *
  5. * Zadanie: Poslaniec *
  6. * Autor: Wojtek Nadara *
  7. * Zlozonosc czasowa: O(n^3 * d + n^2*d^2 + q) *
  8. * Opis: Rozwiazanie wzorcowe *
  9. *****************************************************************************/
  10.  
  11. #include <iostream>
  12. #define D 52
  13. #define M mod
  14. #define N 154
  15. #define uint long long int
  16. using namespace std;
  17. uint m[N][N][D+2];
  18. uint dp[D][2];
  19. uint que [500007][3];
  20. uint res[N][N][D];
  21. int main()
  22. {
  23. ios_base::sync_with_stdio(false);
  24.  
  25. int n, kr;//, d;
  26. uint mod;
  27. cin>>n>>kr>>mod;
  28. for(int i=1; i<=kr; i++)
  29. {
  30. int a, b;
  31. cin>>a>>b;
  32. m[a][b][1]=1;
  33. // m[b][a][1]=1;
  34. }
  35. for(int i=1; i<=n; i++)
  36. {
  37. m[i][i][0]=1;
  38. }
  39. int q;
  40. cin>>q;
  41. for(int i=0;i<q;i++)
  42. cin >>que[i][0] >> que[i][1] >> que[i][2];
  43. int mxd = -1;
  44. for(int i=0;i<q;i++)
  45. if(que[i][2] > mxd)
  46. mxd = que[i][2];
  47.  
  48. for(int t=2; t<=mxd; t++)
  49. {
  50. for(int i=1; i<=n; i++)
  51. {
  52. for(int j=1; j<=n; j++)
  53. {
  54. for(int k=1; k<=n; k++)
  55. {
  56. m[i][j][t]+=m[i][k][t-1]*m[k][j][1];
  57. m[i][j][t]%=M;
  58. }
  59. }
  60. }
  61. }
  62. for(int a=1; a<=n; a++)
  63. {
  64. for(int b = 1; b <= n; b++)
  65. {
  66. dp[0][0]=-1;
  67. for(int i=1; i<=mxd; i++)
  68. {
  69. dp[i][0]=0;
  70. dp[i][1]=0;
  71. for(int j=0; j<=i-1; j++)
  72. {
  73. dp[i][0]-=dp[j][0]*m[a][a][i-j];
  74. dp[i][0]%=M;
  75. dp[i][1]-=dp[j][0]*m[a][b][i-j];
  76. dp[i][1]%=M;
  77. dp[i][0]-=dp[j][1]*m[b][a][i-j];
  78. dp[i][0]%=M;
  79. dp[i][1]-=dp[j][1]*m[b][b][i-j];
  80. dp[i][1]%=M;
  81. }
  82. }
  83. for(int i=1; i <= mxd; i++)
  84. res[a][b][i] = (dp[i][1]%M+M)%M;
  85. }
  86. }
  87.  
  88. for(int i = 0; i < q; i++)
  89. cout << res[que[i][0]][que[i][1]][que[i][2]] << endl;
  90.  
  91. return 0;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement