fork download
  1. // ~~ icebear love attttt ~~
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #define int long long
  5. typedef long long ll;
  6. typedef pair<int, int> ii;
  7. typedef pair<int, ii> iii;
  8.  
  9. template<class T>
  10. bool minimize(T &a, const T &b) {
  11. if (a > b) return a = b, true;
  12. return false;
  13. }
  14.  
  15. template<class T>
  16. bool maximize(T &a, const T &b) {
  17. if (a < b) return a = b, true;
  18. return false;
  19. }
  20.  
  21. #define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
  22. #define FORR(i,a,b) for(int i=(a); i>=(b); --i)
  23. #define REP(i, n) for(int i=0; i<(n); ++i)
  24. #define RED(i, n) for(int i=(n)-1; i>=0; --i)
  25. #define MASK(i) (1LL << (i))
  26. #define BIT(S, i) (((S) >> (i)) & 1)
  27. #define mp make_pair
  28. #define pb push_back
  29. #define fi first
  30. #define se second
  31. #define all(x) x.begin(), x.end()
  32. #define task "icebearat"
  33.  
  34. const int MOD[] = {(int)998244353, (int)1e9 + 5277, (int)1e9 + 8277, (int)1e9 + 9277};
  35. const int inf = 1e9 + 27092008;
  36. const ll INF = 1e18 + 27092008;
  37. const int N = 1e6 + 5;
  38. const int base = 313;
  39. int n, m, a[N], b[N];
  40. int hsA[4][N], hsB[4], power[4][N], pref[4][N];
  41.  
  42. void init(void) {
  43. cin >> n >> m;
  44. FOR(i, 1, n) cin >> a[i];
  45. FOR(i, 1, m) cin >> b[i];
  46. REP(j, 4) {
  47. power[j][0] = 1;
  48. FOR(i, 1, n) power[j][i] = 1LL * power[j][i - 1] * base % MOD[j];
  49. FOR(i, 1, n) hsA[j][i] = (hsA[j][i - 1] + 1LL * a[i] * power[j][i] % MOD[j]) % MOD[j];
  50. FOR(i, 1, m) hsB[j] = (hsB[j] + 1LL * b[i] * power[j][i] % MOD[j]) % MOD[j];
  51. FOR(i, 1, n) pref[j][i] = (pref[j][i - 1] + power[j][i]) % MOD[j];
  52. }
  53. }
  54.  
  55. void process(void) {
  56. int ans = 0;
  57. FOR(i, 1, n - m + 1) {
  58. int x[4];
  59. REP(j, 4) x[j] = a[i] - b[1];
  60. int l = i, r = i + m - 1;
  61. int rangeHash[4], rangeB[4];
  62. REP(j, 4) {
  63. rangeHash[j] = (hsA[j][r] - hsA[j][l - 1] + MOD[j]) % MOD[j];
  64. x[j] = (-x[j] + MOD[j]) % MOD[j];
  65. rangeHash[j] = (rangeHash[j] + 1LL * x[j] * (pref[j][r] - pref[j][l - 1] + MOD[j]) % MOD[j]) % MOD[j];
  66. rangeB[j] = 1LL * hsB[j] * power[j][i - 1] % MOD[j];
  67. }
  68. bool ok = true;
  69. REP(j, 4) if (rangeB[j] != rangeHash[j]) {
  70. ok = false;
  71. break;
  72. }
  73.  
  74. if (ok) ans++;
  75. }
  76. cout << ans;
  77. }
  78.  
  79. signed main() {
  80. ios_base::sync_with_stdio(0);
  81. cin.tie(0); cout.tie(0);
  82. if (fopen(task".inp", "r")) {
  83. freopen(task".inp", "r", stdin);
  84. freopen(task".out", "w", stdout);
  85. }
  86. int tc = 1;
  87. // cin >> tc;
  88. while(tc--) {
  89. init();
  90. process();
  91. }
  92. return 0;
  93. }
  94.  
  95.  
Success #stdin #stdout 0.01s 11868KB
stdin
Standard input is empty
stdout
1