Advertisement
a53

RxExCxCx

a53
May 4th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. #include <fstream>
  2. #include <cstring>
  3. #include <algorithm>
  4. #define LL long long
  5. #define MAX 1000001
  6. using namespace std;
  7. struct matrice
  8. {
  9. int M[4][4];
  10. };
  11. matrice C;
  12. LL N;
  13. int B,X,K,I,A[4],vs[MAX],vd[MAX];
  14.  
  15. matrice inmultire(matrice A,matrice B) /// C=A*B
  16. {
  17. matrice C;
  18. memset(C.M,0,sizeof(C.M));
  19. for(int i=0;i<4;++i)
  20. for(int j=0;j<4;++j)
  21. for(int k=0;k<4;++k)
  22. C.M[i][j]=(C.M[i][j]+1LL*A.M[i][k]*B.M[k][j])%K;
  23. return C;
  24. }
  25.  
  26. matrice putere(matrice A,LL N) /// P=A^N (logadrtmic)
  27. {
  28. if(N==1)
  29. return A;
  30. if(N%2==1)
  31. return inmultire(A,putere(A,N-1));
  32. matrice P=putere(A,N/2);
  33. return inmultire(P,P);
  34. }
  35.  
  36. LL sol;
  37.  
  38. int F(int val) /// Cautare binara (cauta pe v in vectorul v)
  39. {
  40. for(int st=1,dr=I;st<=dr;)
  41. {
  42. int mij=(st+dr)/2;
  43. if(vs[mij]==val)
  44. return vd[mij];
  45. if(vs[mij]<val)
  46. st=mij+1;
  47. else
  48. dr=mij-1;
  49. }
  50. return 0;
  51. }
  52.  
  53. int main()
  54. {
  55. ifstream f("recc.in");
  56. f>>N>>B>>X>>K;
  57. for(int i=0;i<4;++i)
  58. f>>A[i],C.M[3-i][3]=A[i]%K;
  59. f.close();
  60. for(int i=0;i<3;++i)
  61. C.M[i+1][i]=1;
  62. C=putere(C,N-4);
  63. for(int i=0;i<4;++i)
  64. A[i]=C.M[i][3];
  65. for(int i=1;i<=B;++i)
  66. for(int j=1;j<=B;++j)
  67. vs[++I]=(1LL*A[0]*i%K +1LL*A[1]*j%K)%K;
  68. sort(vs+1,vs+I+1);
  69. int II=1;
  70. vd[1]=1;
  71. for(int i=2;i<=I;++i)
  72. if(vs[II]!=vs[i])
  73. vs[++II]=vs[i],vd[II]=1;
  74. else
  75. ++vd[II];
  76. I=II;
  77. LL sol=0;
  78. for(int i=1;i<=B;++i)
  79. for(int j=1;j<=B;++j)
  80. {
  81. int v=(1LL*A[2]*i%K+1LL*A[3]*j%K)%K;
  82. v=(X-v+K)%K;
  83. sol+=F(v);
  84. }
  85. ofstream g("recc.out");
  86. g<<sol<<'\n';
  87. g.close();
  88. return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement