fork(6) download
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4. using namespace __gnu_pbds;
  5. using namespace std;
  6. #define Foreach(i, c) for(__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
  7. #define For(i,a,b) for(int (i)=(a);(i) < (b); ++(i))
  8. #define rof(i,a,b) for(int (i)=(a);(i) > (b); --(i))
  9. #define rep(i, c) for(auto &(i) : (c))
  10. #define x first
  11. #define y second
  12. #define pb push_back
  13. #define PB pop_back()
  14. #define iOS ios_base::sync_with_stdio(false)
  15. #define sqr(a) (((a) * (a)))
  16. #define all(a) a.begin() , a.end()
  17. #define error(x) cerr << #x << " = " << (x) <<endl
  18. #define Error(a,b) cerr<<"( "<<#a<<" , "<<#b<<" ) = ( "<<(a)<<" , "<<(b)<<" )\n";
  19. #define errop(a) cerr<<#a<<" = ( "<<((a).x)<<" , "<<((a).y)<<" )\n";
  20. #define coud(a,b) cout<<fixed << setprecision((b)) << (a)
  21. #define L(x) ((x)<<1)
  22. #define R(x) (((x)<<1)+1)
  23. #define umap unordered_map
  24. #define double long double
  25. typedef long long ll;
  26. typedef pair<int,int>pii;
  27. typedef vector<int> vi;
  28. typedef complex<double> point;
  29. template <typename T> using os = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  30. template <class T> inline void smax(T &x,T y){ x = max((x), (y));}
  31. template <class T> inline void smin(T &x,T y){ x = min((x), (y));}
  32. int a[2], b[2], x[3], y[3], p;
  33. inline int nex(int c, int w){
  34. return (1LL * c * x[w] + y[w])%p;
  35. }
  36. int main(){
  37. iOS;
  38. cin >> p;
  39. For(i,0,2){
  40. if(!i)
  41. cin >> a[0] >> a[1];
  42. else
  43. cin >> b[0] >> b[1];
  44. cin >> x[i] >> y[i];
  45. }
  46. ll ans = 0LL;
  47. while(a[0] != a[1] && ans < p + 20){
  48. ++ ans;
  49. a[0] = nex(a[0], 0);
  50. b[0] = nex(b[0], 1);
  51. }
  52. if(a[0] != a[1]){
  53. cout << -1 << endl;
  54. return 0;
  55. }
  56. if(a[0] == a[1] && b[0] == b[1]){
  57. cout << ans << endl;
  58. return 0;
  59. }
  60. int o = 0;
  61. int cur = a[0];
  62. x[2] = x[1], y[2] = y[1];
  63. x[1] = 1, y[1] = 0;
  64. do{
  65. cur = nex(cur, 0);
  66. ++ o;
  67. x[1] = (1LL * x[1] * x[2]) % p;
  68. y[1] = (1LL * y[1] * x[2]) % p;
  69. y[1] = (1LL * y[1] + y[2]) % p;
  70. }while(o < p + 10 && cur != a[1]);
  71. if(cur != a[1]){
  72. cout << -1 << endl;
  73. return 0;
  74. }
  75. int O = 0;
  76. cur = b[0];
  77. do{
  78. cur = nex(cur, 1);
  79. ++ O;
  80. }while(O < p + 10 && cur != b[1]);
  81. if(cur != b[1]){
  82. cout << -1 << endl;
  83. return 0;
  84. }
  85. ans += 1LL * o * O;
  86. cout << ans << endl;
  87. }
  88.  
  89.  
Success #stdin #stdout 0s 3276KB
stdin
Standard input is empty
stdout
0