Advertisement
bibaboba12345

Untitled

Sep 6th, 2021
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. using namespace std;
  6. const long long MOD = 1e9 + 7, N = 2e3 + 7;
  7. long long n, n1, m1, n2, m2, kx1, kx2, kY1, ky2, x1, Y1, x2, y2,lx1,lx2,lY1,ly2,rx1,rx2,rY1,ry2,per_y, per_x,answ;
  8.  
  9. int gcd(int a, int b) {
  10. if (b == 0)
  11. return a;
  12. else
  13. return gcd(b, a % b);
  14. }
  15.  
  16. int main()
  17. {
  18. cin >> n1 >> m1;
  19. vector<vector<char>> a1;
  20. a1.resize(n1);
  21. for (int i = 0; i < n1; i++) {
  22. a1[i].resize(m1);
  23. }
  24. for (int y = 0; y < m1; y++) {
  25. for (int x = 0; x < n1; x++) {
  26. cin >> a1[x][y];
  27. }
  28. }
  29. cin >> n2 >> m2;
  30. vector<vector<char>> a2;
  31. a2.resize(n2);
  32. for (int i = 0; i < n2; i++) {
  33. a2[i].resize(m2);
  34. }
  35. for (int y = 0; y < m2; y++) {
  36. for (int x = 0; x < n2; x++) {
  37. cin >> a2[x][y];
  38. }
  39. }
  40. kx1 = n2 / gcd(n1, n2);
  41. kx2 = n1 / gcd(n1, n2);
  42. kY1 = m2 / gcd(m1, m2);
  43. ky2 = m1 / gcd(m1, m2);
  44. Y1 = 0;
  45. y2 = 0;
  46. answ = 0;
  47. while (Y1 <= m1 - 1 && y2 <= m2 - 1) {
  48. lY1 = Y1 * kY1;
  49. rY1 = (Y1 + 1) * kY1 - 1;
  50. ly2 = y2 * ky2;
  51. ry2 = (y2 + 1) * ky2 - 1;
  52. per_y = min(rY1, ry2) - max(lY1, ly2) + 1;
  53. x1 = 0;
  54. x2 = 0;
  55. while (x1 <= n1 - 1 && x2 <= n2 - 1) {
  56. lx1 = x1 * kx1;
  57. rx1 = (x1 + 1) * kx1 - 1;
  58. lx2 = x2 * kx2;
  59. rx2 = (x2 + 1) * kx2 - 1;
  60. per_x = min(rx1, rx2) - max(lx1, lx2) + 1;
  61. if (a1[x1][Y1] == a2[x2][y2]) {
  62. answ += per_y * per_x;
  63. }
  64. if (rx1 <= rx2) {
  65. x1++;
  66. }
  67. else {
  68. x2++;
  69. }
  70. }
  71. if (rY1 <= ry2) {
  72. Y1++;
  73. }
  74. else {
  75. y2++;
  76. }
  77. }
  78. cout << answ;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement