Beingamanforever

Grid GCD, memset se TLE

Dec 27th, 2024
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. /**
  2.  *    author:  heWhoCooks
  3.  *    created: 2024-12-27 19:50:38
  4.  **/
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
  8. #define NeedForSpeed                  \
  9.     ios_base::sync_with_stdio(false); \
  10.     cin.tie(NULL);                    \
  11.     cout.tie(NULL);
  12. #define int long long
  13. #define all(x) (x).begin(), (x).end()
  14. typedef vector<int> vi;
  15. typedef vector<bool> vb;
  16. typedef vector<vi> vvi;
  17. typedef vector<pair<int, int>> vpi;
  18. #define f first
  19. #define s second
  20. #define pb push_back
  21. #define pf push_front
  22. #define yes cout << "YES" << endl
  23. #define no cout << "NO" << endl
  24. #define endl "\n"
  25. #define M_PI 3.14159265358979323846L
  26. const int mod = 1e9 + 7;
  27. const int INF = 2e18;
  28. int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
  29. const int N = 105;
  30. int dp[N][N], grid[N][N];
  31. void solve()
  32. {
  33.     int n, m;
  34.     cin >> n >> m;
  35.     for (int i = 1; i <= n; i++)
  36.     {
  37.         for (int j = 1; j <= m; j++)
  38.         {
  39.             cin >> grid[i][j];
  40.         }
  41.     }
  42.     vi divisors;
  43.     int maxi = gcd(grid[1][1], grid[n][m]); // have to be taken anyhow
  44.     for (int i = 1; i * i <= maxi; i++)
  45.     {
  46.         if (maxi % i == 0)
  47.         {
  48.             divisors.pb(i);
  49.             if (i != maxi / i)
  50.             {
  51.                 divisors.pb(maxi / i);
  52.             }
  53.         }
  54.     }
  55.     sort(all(divisors), greater<int>());
  56.     for (auto &div : divisors)
  57.     {
  58.         // reset, memset is giving TLE
  59.         for (int i = 1; i <= n; i++)
  60.         {
  61.             for (int j = 1; j <= m; j++)
  62.             {
  63.                 dp[i][j] = 0;
  64.             }
  65.         }
  66.         // check if we can reach the end with this gcd
  67.         // base case, initial state true
  68.         dp[1][1] = 1;
  69.         for (int i = 1; i <= n; i++)
  70.         {
  71.             for (int j = 1; j <= m; j++)
  72.             {
  73.                 if (!dp[i][j])
  74.                 {
  75.                     // can't reach here
  76.                     continue;
  77.                 }
  78.                 if (i + 1 <= n && ((grid[i + 1][j] % div) == 0))
  79.                 {
  80.                     dp[i + 1][j] = 1;
  81.                 }
  82.                 if (j + 1 <= m && ((grid[i][j + 1] % div) == 0))
  83.                 {
  84.                     dp[i][j + 1] = 1;
  85.                 }
  86.             }
  87.         }
  88.         if (dp[n][m])
  89.         {
  90.             cout << div << endl;
  91.             return;
  92.         }
  93.     }
  94.     cout << -1 << endl;
  95.     return;
  96. }
  97.  
  98. signed main()
  99. {
  100.     NeedForSpeed;
  101.     int t = 1;
  102.     cin >> t;
  103.     while (t--)
  104.     {
  105.         solve();
  106.     }
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment