mickypinata

CUBE-T169: Factor Match

Jul 12th, 2021 (edited)
762
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.09 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long lli;
  5. typedef pair<int, int> pii;
  6.  
  7. const int X = 1e5;
  8. const int sqrtX = sqrt(X);
  9. const int N = 1e6;
  10. const int PB = 1e9 + 7;
  11.  
  12. int arrA[N + 1], arrB[N + 1], minDiv[X + 1], factorCnt[X + 1];
  13. int lenA, lenB;
  14.  
  15. int factor(int x){
  16.     int lastOne = 1;
  17.     int cnt = 0;
  18.     int ans = 1;
  19.     while(true){
  20.         if(minDiv[x] != lastOne){
  21.             ans *= cnt + 1;
  22.             lastOne = minDiv[x];
  23.             cnt = 1;
  24.         } else {
  25.             ++cnt;
  26.         }
  27.         if(x == 1){
  28.             break;
  29.         }
  30.         x /= minDiv[x];
  31.     }
  32.     return ans;
  33. }
  34.  
  35. bool sameSubstr(int len){
  36.     lli hshTr = 0;
  37.     lli base = 1;
  38.     for(int i = 1; i <= len; ++i){
  39.         if(i > 1){
  40.             base *= PB;
  41.         }
  42.         hshTr *= PB;
  43.         hshTr += arrB[i];
  44.     }
  45.     lli hsh = 0;
  46.     for(int i = 1; i <= len; ++i){
  47.         hsh *= PB;
  48.         hsh += arrA[i];
  49.     }
  50.     if(hsh == hshTr){
  51.         return true;
  52.     }
  53.     for(int i = len + 1; i <= lenA - lenB + len; ++i){
  54.         hsh -= base * arrA[i - len];
  55.         hsh *= PB;
  56.         hsh += arrA[i];
  57.         if(hsh == hshTr){
  58.             return true;
  59.         }
  60.     }
  61.     return false;
  62. }
  63.  
  64. int main(){
  65.  
  66.     // Sieve
  67.     minDiv[1] = 0;
  68.     for(int i = 2; i <= X; ++i){
  69.         if(minDiv[i] == 0){
  70.             for(int j = i; j <= X; j += i){
  71.                 minDiv[j] = i;
  72.             }
  73.         }
  74.     }
  75.  
  76.     // Precompute factors
  77.     for(int i = 1; i <= X; ++i){
  78.         factorCnt[i] = factor(i);
  79.     }
  80.  
  81.     scanf("%d%d", &lenA, &lenB);
  82.     for(int i = 1; i <= lenA; ++i){
  83.         int x;
  84.         scanf("%d", &x);
  85.         arrA[i] = factorCnt[x];
  86.     }
  87.     for(int i = 1; i <= lenB; ++i){
  88.         int x;
  89.         scanf("%d", &x);
  90.         arrB[i] = factorCnt[x];
  91.     }
  92.  
  93.     int l = 0;
  94.     int r = lenB;
  95.     int mx = 0;
  96.     while(l <= r){
  97.         int m = (l + r) / 2;
  98.         if(sameSubstr(m)){
  99.             mx = max(mx, m);
  100.             l = m + 1;
  101.         } else {
  102.             r = m - 1;
  103.         }
  104.     }
  105.     cout << mx;
  106.  
  107.     return 0;
  108. }
  109.  
Add Comment
Please, Sign In to add comment