problem_id stringlengths 6 6 | language stringclasses 2 values | original_status stringclasses 3 values | original_src stringlengths 19 243k | changed_src stringlengths 19 243k | change stringclasses 3 values | i1 int64 0 8.44k | i2 int64 0 8.44k | j1 int64 0 8.44k | j2 int64 0 8.44k | error stringclasses 270 values | stderr stringlengths 0 226k |
|---|---|---|---|---|---|---|---|---|---|---|---|
p02962 | C++ | Runtime Error | /*
author: Apoorv Singh
*/
#include <bits/stdc++.h>
using namespace std;
typedef vector<long long> vi;
typedef pair<long long, long long> pii;
#define pb push_back
#define all(c) c.begin(), c.end()
#define FOR(i, a, b) for (long long i = a; i < b; ++i)
#define FORR(i, a, b) for (long long i = a; i > b; --i)
#define um unordered_map
#define F first
#define S second
#define int long long
#define inf LLONG_MAX
#define endl "\n"
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#define TRACE
#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cout.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
#else
#define trace(...)
#endif
template <class X> void printarr(X arr[], int n) {
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
cout << endl;
}
template <typename X> ostream &operator<<(ostream &x, const vector<X> &v) {
for (X a : v)
x << a << ' ';
return x;
}
template <typename X, typename Y>
ostream &operator<<(ostream &x, const vector<pair<X, Y>> &v) {
for (pair<X, Y> it : v)
x << it.first << ' ' << it.second << endl;
return x;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const map<T, S> &v) {
for (pair<T, S> it : v)
os << it.first << " => " << it.second << endl;
return os;
}
double pi = 3.14159265358979323846264338327950288419716939937510582097494459230;
int modulo = 1e9 + 7;
int fpow(int a, int n) {
int ans = 1;
while (n) {
if (n & 1)
ans = (ans * a) % modulo;
a = (a * a) % modulo;
n = n >> 1;
}
return ans;
}
/*
const int M = 1e6 + 10;
int composite[M] = {0};
void sieve()
{
FOR(i,2,M)
{
if (!composite[i])
{
for (int j = 2*i; j < M; j += i)
composite[j] = max(composite[j], i);
}
}
}
*/
const int M = 5e5 + 10;
vi graph[M], sorted;
int valid[M] = {0}, n, m, visited[M] = {0};
void stringMatching(string s, string t) {
string a = t + '#' + s;
int n = a.size();
int z[n];
z[0] = 0;
int l = 0, r = 0;
FOR(i, 1, n) {
if (i > r) {
l = r = i;
while (r < n and a[r] == a[r - l])
++r;
z[i] = r - l;
--r;
}
else {
if (z[i - l] < r - i + 1)
z[i] = z[i - l];
else {
l = i;
while (r < n and a[r - l] == a[r])
++r;
z[i] = r - l;
--r;
}
}
}
FOR(i, 0, n) {
if (z[i] == t.size())
valid[i - t.size() - 1] = 1;
}
}
bool isCycle(int curr, int code) {
visited[curr] = code;
int next = (curr + m) % n;
if (not valid[next]) {
sorted.pb(curr);
return false;
}
if (visited[next] == code)
return true;
if (visited[next] == 0 and isCycle(next, code))
return true;
sorted.pb(curr);
return false;
}
int dfs(int curr, int len) {
visited[curr] = 1;
int next = (curr + m) % n;
if (not valid[next])
return len;
return dfs(next, len + 1);
}
int solve(string x, string y) {
n = x.size();
m = y.size();
int req = n + m;
string t = y, s = x;
while (s.size() < req)
s += x;
stringMatching(s, t);
int code = 1;
FOR(i, 0, n) {
if (valid[i] and visited[i] == 0) {
if (isCycle(i, code))
return -1;
++code;
}
}
if (sorted.size() == 0)
return 0;
int ans = 0;
FOR(i, 0, n) visited[i] = 0;
FORR(i, sorted.size() - 1, -1) {
int curr = sorted[i];
if (valid[curr] and not visited[curr])
ans = max(ans, dfs(curr, 1));
}
return ans;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string s, t;
cin >> s >> t;
cout << solve(s, t);
return 0;
} | /*
author: Apoorv Singh
*/
#include <bits/stdc++.h>
using namespace std;
typedef vector<long long> vi;
typedef pair<long long, long long> pii;
#define pb push_back
#define all(c) c.begin(), c.end()
#define FOR(i, a, b) for (long long i = a; i < b; ++i)
#define FORR(i, a, b) for (long long i = a; i > b; --i)
#define um unordered_map
#define F first
#define S second
#define int long long
#define inf LLONG_MAX
#define endl "\n"
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#define TRACE
#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cout.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
#else
#define trace(...)
#endif
template <class X> void printarr(X arr[], int n) {
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
cout << endl;
}
template <typename X> ostream &operator<<(ostream &x, const vector<X> &v) {
for (X a : v)
x << a << ' ';
return x;
}
template <typename X, typename Y>
ostream &operator<<(ostream &x, const vector<pair<X, Y>> &v) {
for (pair<X, Y> it : v)
x << it.first << ' ' << it.second << endl;
return x;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const map<T, S> &v) {
for (pair<T, S> it : v)
os << it.first << " => " << it.second << endl;
return os;
}
double pi = 3.14159265358979323846264338327950288419716939937510582097494459230;
int modulo = 1e9 + 7;
int fpow(int a, int n) {
int ans = 1;
while (n) {
if (n & 1)
ans = (ans * a) % modulo;
a = (a * a) % modulo;
n = n >> 1;
}
return ans;
}
/*
const int M = 1e6 + 10;
int composite[M] = {0};
void sieve()
{
FOR(i,2,M)
{
if (!composite[i])
{
for (int j = 2*i; j < M; j += i)
composite[j] = max(composite[j], i);
}
}
}
*/
const int M = 2e6;
vi graph[M], sorted;
int valid[M] = {0}, n, m, visited[M] = {0};
void stringMatching(string s, string t) {
string a = t + '#' + s;
int n = a.size();
int z[n];
z[0] = 0;
int l = 0, r = 0;
FOR(i, 1, n) {
if (i > r) {
l = r = i;
while (r < n and a[r] == a[r - l])
++r;
z[i] = r - l;
--r;
}
else {
if (z[i - l] < r - i + 1)
z[i] = z[i - l];
else {
l = i;
while (r < n and a[r - l] == a[r])
++r;
z[i] = r - l;
--r;
}
}
}
FOR(i, 0, n) {
if (z[i] == t.size())
valid[i - t.size() - 1] = 1;
}
}
bool isCycle(int curr, int code) {
visited[curr] = code;
int next = (curr + m) % n;
if (not valid[next]) {
sorted.pb(curr);
return false;
}
if (visited[next] == code)
return true;
if (visited[next] == 0 and isCycle(next, code))
return true;
sorted.pb(curr);
return false;
}
int dfs(int curr, int len) {
visited[curr] = 1;
int next = (curr + m) % n;
if (not valid[next])
return len;
return dfs(next, len + 1);
}
int solve(string x, string y) {
n = x.size();
m = y.size();
int req = n + m;
string t = y, s = x;
while (s.size() < req)
s += x;
stringMatching(s, t);
int code = 1;
FOR(i, 0, n) {
if (valid[i] and visited[i] == 0) {
if (isCycle(i, code))
return -1;
++code;
}
}
if (sorted.size() == 0)
return 0;
int ans = 0;
FOR(i, 0, n) visited[i] = 0;
FORR(i, sorted.size() - 1, -1) {
int curr = sorted[i];
if (valid[curr] and not visited[curr])
ans = max(ans, dfs(curr, 1));
}
return ans;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string s, t;
cin >> s >> t;
cout << solve(s, t);
return 0;
}
| replace | 95 | 96 | 95 | 96 | 0 | |
p02962 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long
// #define double long double
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (a); i > (b); --i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
#define VECCIN(x) \
for (auto &youso_ : (x)) \
cin >> youso_
#define bitcnt(x) __builtin_popcount(x)
#define lbit(x) __builtin_ffsll(x)
#define rbit(x) __builtin_clzll(x)
#define SZ(x) ((long long)(x).size())
#define fi first
#define se second
#define All(a) (a).begin(), (a).end()
#define rAll(a) (a).rbegin(), (a).rend()
#define cinfast() cin.tie(0), ios::sync_with_stdio(false)
#define PERM(c) \
sort(All(c)); \
for (bool cp = true; cp; cp = next_permutation(All(c)))
#define MKORDER(n) \
vector<int> od(n); \
iota(All(od), 0LL);
template <typename T = long long> inline T IN() {
T x;
cin >> x;
return (x);
}
inline void CIN() {}
template <class Head, class... Tail>
inline void CIN(Head &&head, Tail &&...tail) {
cin >> head;
CIN(move(tail)...);
}
#define CCIN(...) \
char __VA_ARGS__; \
CIN(__VA_ARGS__)
#define DCIN(...) \
double __VA_ARGS__; \
CIN(__VA_ARGS__)
#define LCIN(...) \
long long __VA_ARGS__; \
CIN(__VA_ARGS__)
#define SCIN(...) \
string __VA_ARGS__; \
CIN(__VA_ARGS__)
#define Yes(a) cout << (a ? "Yes" : "No") << "\n"
#define YES(a) cout << (a ? "YES" : "NO") << "\n"
#define Printv(v) \
{ \
FOREACH(x, v) { cout << x << " "; } \
cout << "\n"; \
}
template <typename T = string> inline void eputs(T s) {
cout << s << "\n";
exit(0);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
std::fill((T *)array, (T *)(array + N), val);
}
template <typename T> using PQG = priority_queue<T, vector<T>, greater<T>>;
template <typename T> using PQ = priority_queue<T>;
typedef long long ll;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<ll, ll> PL;
typedef vector<PL> VPL;
typedef vector<bool> VB;
typedef vector<double> VD;
typedef vector<string> VS;
const int INF = 1e9;
const int MOD = 1e9 + 7;
// const int MOD = 998244353;
const ll LINF = 1e18;
const double PI = atan(1.0) * 4.0;
const ll dw[] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll dh[] = {0, 1, 1, 1, 0, -1, -1, -1};
#define PI 3.141592653589793238
VL Zalgo(const string &S) {
int N = (int)S.size();
VL res(N);
res[0] = N;
int i = 1, j = 0;
while (i < N) {
while (i + j < N && S[j] == S[i + j])
++j;
res[i] = j;
if (j == 0) {
++i;
continue;
}
int k = 1;
while (i + k < N && k + res[k] < j)
res[i + k] = res[k], ++k;
i += k, j -= k;
}
return res;
}
VVL G;
VB used, nused;
set<ll> st;
VL hist;
bool finished[1000];
ll start = -1;
void findroop(int v) {
used[v] = true;
hist.emplace_back(v);
FOREACH(to, G[v]) {
if (finished[to])
continue;
if (used[to]) {
start = to;
return;
}
findroop(to);
if (start != -1)
return;
}
hist.pop_back();
finished[v] = true;
}
bool dfs(ll now) {
st.emplace(now);
used[now] = true;
bool res = true;
FOREACH(to, G[now]) {
if (res == false)
break;
if (st.find(to) != st.end())
return false;
res &= dfs(to);
}
return res;
}
ll dfs2(ll now, ll par) {
used[now] = true;
ll res = 0;
FOREACH(to, G[now]) {
if (to == par || used[to])
continue;
res = max(res, dfs2(to, now) + 1);
}
return res;
}
signed main() {
SCIN(S, T);
string tmp = S;
while (tmp.length() < T.length()) {
tmp += S;
}
VL z = Zalgo(T + "$" + tmp + tmp);
ll N = tmp.length();
G.resize(N);
REP(i, N) {
if (z[T.length() + 1 + i] == T.length()) {
G[i].emplace_back((i + T.length()) % N);
}
}
used.assign(N, false);
ll ans = 0;
REP(i, N) {
if (used[i])
continue;
findroop(i);
if (start != -1)
eputs(-1);
}
used.assign(N, false);
REP(i, N) {
if (used[i])
continue;
st.clear();
dfs(i);
ans = max(ans, (ll)st.size() - 1);
}
cout << ans << "\n";
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
// #define double long double
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (a); i > (b); --i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
#define VECCIN(x) \
for (auto &youso_ : (x)) \
cin >> youso_
#define bitcnt(x) __builtin_popcount(x)
#define lbit(x) __builtin_ffsll(x)
#define rbit(x) __builtin_clzll(x)
#define SZ(x) ((long long)(x).size())
#define fi first
#define se second
#define All(a) (a).begin(), (a).end()
#define rAll(a) (a).rbegin(), (a).rend()
#define cinfast() cin.tie(0), ios::sync_with_stdio(false)
#define PERM(c) \
sort(All(c)); \
for (bool cp = true; cp; cp = next_permutation(All(c)))
#define MKORDER(n) \
vector<int> od(n); \
iota(All(od), 0LL);
template <typename T = long long> inline T IN() {
T x;
cin >> x;
return (x);
}
inline void CIN() {}
template <class Head, class... Tail>
inline void CIN(Head &&head, Tail &&...tail) {
cin >> head;
CIN(move(tail)...);
}
#define CCIN(...) \
char __VA_ARGS__; \
CIN(__VA_ARGS__)
#define DCIN(...) \
double __VA_ARGS__; \
CIN(__VA_ARGS__)
#define LCIN(...) \
long long __VA_ARGS__; \
CIN(__VA_ARGS__)
#define SCIN(...) \
string __VA_ARGS__; \
CIN(__VA_ARGS__)
#define Yes(a) cout << (a ? "Yes" : "No") << "\n"
#define YES(a) cout << (a ? "YES" : "NO") << "\n"
#define Printv(v) \
{ \
FOREACH(x, v) { cout << x << " "; } \
cout << "\n"; \
}
template <typename T = string> inline void eputs(T s) {
cout << s << "\n";
exit(0);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
std::fill((T *)array, (T *)(array + N), val);
}
template <typename T> using PQG = priority_queue<T, vector<T>, greater<T>>;
template <typename T> using PQ = priority_queue<T>;
typedef long long ll;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<ll, ll> PL;
typedef vector<PL> VPL;
typedef vector<bool> VB;
typedef vector<double> VD;
typedef vector<string> VS;
const int INF = 1e9;
const int MOD = 1e9 + 7;
// const int MOD = 998244353;
const ll LINF = 1e18;
const double PI = atan(1.0) * 4.0;
const ll dw[] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll dh[] = {0, 1, 1, 1, 0, -1, -1, -1};
#define PI 3.141592653589793238
VL Zalgo(const string &S) {
int N = (int)S.size();
VL res(N);
res[0] = N;
int i = 1, j = 0;
while (i < N) {
while (i + j < N && S[j] == S[i + j])
++j;
res[i] = j;
if (j == 0) {
++i;
continue;
}
int k = 1;
while (i + k < N && k + res[k] < j)
res[i + k] = res[k], ++k;
i += k, j -= k;
}
return res;
}
VVL G;
VB used, nused;
set<ll> st;
VL hist;
bool finished[1000100];
ll start = -1;
void findroop(int v) {
used[v] = true;
hist.emplace_back(v);
FOREACH(to, G[v]) {
if (finished[to])
continue;
if (used[to]) {
start = to;
return;
}
findroop(to);
if (start != -1)
return;
}
hist.pop_back();
finished[v] = true;
}
bool dfs(ll now) {
st.emplace(now);
used[now] = true;
bool res = true;
FOREACH(to, G[now]) {
if (res == false)
break;
if (st.find(to) != st.end())
return false;
res &= dfs(to);
}
return res;
}
ll dfs2(ll now, ll par) {
used[now] = true;
ll res = 0;
FOREACH(to, G[now]) {
if (to == par || used[to])
continue;
res = max(res, dfs2(to, now) + 1);
}
return res;
}
signed main() {
SCIN(S, T);
string tmp = S;
while (tmp.length() < T.length()) {
tmp += S;
}
VL z = Zalgo(T + "$" + tmp + tmp);
ll N = tmp.length();
G.resize(N);
REP(i, N) {
if (z[T.length() + 1 + i] == T.length()) {
G[i].emplace_back((i + T.length()) % N);
}
}
used.assign(N, false);
ll ans = 0;
REP(i, N) {
if (used[i])
continue;
findroop(i);
if (start != -1)
eputs(-1);
}
used.assign(N, false);
REP(i, N) {
if (used[i])
continue;
st.clear();
dfs(i);
ans = max(ans, (ll)st.size() - 1);
}
cout << ans << "\n";
}
| replace | 115 | 116 | 115 | 116 | 0 | |
p02962 | C++ | Time Limit Exceeded | // https://atcoder.jp/contests/abc135/tasks/abc135_f
#include <bits/stdc++.h>
#include <sys/types.h>
#include <unistd.h>
#define CIN_ONLY
#define DECIMAL_DIGITS 10
#define STATIC_MOD 1e9 + 7
#ifdef BTK
/*<head>*/
#include "Template.hpp"
#include "string/RollingHash.hpp"
/*</head>*/
#else
/*<body>*/
/* #region auto includes */
/* #region stl */
/*<stl>*/
#include <bits/stdc++.h>
#include <sys/types.h>
#include <unistd.h>
using namespace std;
/*</stl>*/
/* #endregion */
/* #region template/IncludeSTL.hpp*/
/**
* @file IncludeSTL.hpp
* @author btk
* @brief 標準ライブラリをincludeするだけ
* @version 0.1
* @date 2019-07-21
*
* @copyright Copyright (c) 2019
*
*/
/*<head>*/
#pragma once
/*</head>*/
/*<stl>*/
#include <bits/stdc++.h>
#include <sys/types.h>
#include <unistd.h>
using namespace std;
/*</stl>*/
/* #endregion */
/* #region template/Macro.hpp*/
/**
* @file Macro.hpp
* @author btk
* @brief マクロとか,LLとか
* @version 0.1
* @date 2019-07-13
*
* @copyright Copyright (c) 2019
*
*/
//! LL
using LL = long long;
/**
* @def DEBUG
* @brief デバッグ用のif文 提出時はif(0)で実行されない
*/
/*</head>*/
#ifdef BTK
#define DEBUG if (1)
#else
#ifdef CIN_ONLY
#define FAST_IO
#endif
#define DEBUG if (0)
#endif
/**
* @def ALL(v)
* @brief
* ALLマクロ
*/
#define ALL(v) (v).begin(), (v).end()
/**
* @def REC(ret, ...)
* @brief
* 再帰ラムダをするためのマクロ
*/
#define REC(ret, ...) std::function<ret(__VA_ARGS__)>
/**
* @def VAR_NAME(var)
* @brief 変数名を取得する
*/
#define VAR_NAME(var) #var
/**
* @brief
* rangeで生まれる使わない変数を消す用(警告消し)
*/
template <typename T> inline T &unused_var(T &v) { return v; }
/* #endregion */
/* #region template/IO.hpp*/
/**
* @file IO.hpp
* @author btk
* @brief cin高速化とか,出力の小数桁固定とか
* @version 0.1
* @date 2019-07-13
*
* @copyright Copyright (c) 2019
*/
/**
* @brief 入出力の設定を行うための構造体
*/
struct cww {
/**
* @brief Construct a new cww::cww object
* @details
* CIN_ONLYを定義すると,submit時にcinとscanfの同期を切る設定が走る
* DECIMAL_DIGITSを定義すると,doubleの出力時指定した桁数分小数部を吐くようになる
*/
cww() {
#ifdef FAST_IO
ios::sync_with_stdio(false);
cin.tie(0);
#endif
#ifdef DECIMAL_DIGITS
cout << fixed;
cout << setprecision(DECIMAL_DIGITS);
#endif
}
};
//! 入出力設定構造体を実体化
cww star;
/**
* @brief
* vectorに直接cin流すためのやつ
* @tparam T
* @param is
* @param v
* @return istream&
*/
template <typename T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
for (auto &it : v)
is >> it;
return is;
}
/* #endregion */
/* #region template/Loop.hpp*/
/**
* @file Loop.hpp
* @author btk
* @brief rangeとかループ系のクラス
* @version 0.1
* @date 2019-07-13
*
* @copyright Copyright (c) 2019
*
*/
/**
* @brief
* rangeを逆向きに操作したいとき用
* @details
* ループの範囲は[bg,ed)なので注意
* @see range
*/
class reverse_range {
private:
struct I {
int x;
int operator*() { return x - 1; }
bool operator!=(I &lhs) { return x > lhs.x; }
void operator++() { --x; }
};
I i, n;
public:
/**
* @brief Construct a new reverse range object
*
* @param n
*/
reverse_range(int n) : i({0}), n({n}) {}
/**
* @brief Construct a new reverse range object
*
* @param i
* @param n
*/
reverse_range(int i, int n) : i({i}), n({n}) {}
/**
* @brief
* begin iterator
* @return I&
*/
I &begin() { return n; }
/**
* @brief
* end iterator
* @return I&
*/
I &end() { return i; }
};
/**
* @brief
* python みたいな range-based for を実現
* @details
* ループの範囲は[bg,ed)なので注意
* !つけると逆向きにループが回る (reverse_range)
* 空間計算量はO(1)
* 使わない変数ができて警告が出がちなので,unused_varとかを使って警告消しするとよい
*/
class range {
private:
struct I {
int x;
int operator*() { return x; }
bool operator!=(I &lhs) { return x < lhs.x; }
void operator++() { ++x; }
};
I i, n;
public:
/**
* @brief Construct a new range object
*
* @param n
*/
range(int n) : i({0}), n({n}) {}
/**
* @brief Construct a new range object
*
* @param i
* @param n
*/
range(int i, int n) : i({i}), n({n}) {}
/**
* @brief
* begin iterator
* @return I&
*/
I &begin() { return i; }
/**
* @brief
* end iterator
* @return I&
*/
I &end() { return n; }
/**
* @brief
* 逆向きに参照するrange(reverse_rangeを取得できるs)
* @return reverse_range
*/
reverse_range operator!() { return reverse_range(*i, *n); }
};
/* #endregion */
/* #region template/MinMaxOperation.hpp*/
/**
* @file MinMaxOperation.hpp
* @author btk
* @brief 最大値とか最小値を求める
* @version 0.1
* @date 2019-07-04
*
* @copyright Copyright (c) 2019
*
*/
/**
* @brief 2項の最小値求める
*
* @tparam T
*/
template <typename T> struct min_op {
/**
* @brief 本体
*
* @param l
* @param r
* @return T
*/
static T exec(const T l, const T r) { return l < r ? l : r; }
};
/**
* @brief 2項の最大値求める
*
* @tparam T
*/
template <typename T> struct max_op {
/**
* @brief 本体
*
* @param l
* @param r
* @return T
*/
static T exec(const T l, const T r) { return l > r ? l : r; }
};
/**
* @brief テンプレート再帰の末尾
*
* @tparam F 二項演算
* @tparam T
* @param v
* @return T
*/
template <typename F, typename T> inline T multi_op(T &&v) { return v; }
/**
* @brief 複数項における演算の結果を返す
*
* @tparam F
* @tparam T
* @tparam Ts
* @param head
* @param tail
* @return T
*/
template <typename F, typename T, typename... Ts>
inline T multi_op(const T head, Ts &&...tail) {
return F::exec(head, multi_op<F>(tail...));
}
/**
* @brief 複数項の最小値
* @see multi_op
* @tparam T
* @tparam Ts
* @param head
* @param tail
* @return T
*/
template <typename T, typename... Ts>
inline T multi_min(T &&head, Ts &&...tail) {
return multi_op<min_op<T>>(head, tail...);
}
/**
* @brief 複数項の最大値
* @see multi_op
* @tparam T
* @tparam Ts
* @param head
* @param tail
* @return T
*/
template <typename T, typename... Ts>
inline T multi_max(T &&head, Ts &&...tail) {
return multi_op<max_op<T>>(head, tail...);
}
/**
* @brief
* 先頭の値をFで参照する関数に基づいて変更できたらする
* @tparam F
* @tparam T
* @tparam Ts
* @param target
* @param candidates
* @return true
* @return false
*/
template <typename F, typename T, typename... Ts>
inline bool ch_op(T &target, Ts &&...candidates) {
const T old = target;
target = multi_op<F>(target, candidates...);
return old != target;
}
/**
* @brief change min
* @tparam T 型
* @param target 変更する値
* @param candidates
* @return 更新があればtrue
*/
template <typename T, typename... Ts>
inline bool chmin(T &target, Ts &&...candidates) {
return ch_op<min_op<T>>(target, candidates...);
}
/**
* @brief chminのmax版
* @see chmin
* @tparam T 型
* @param target 変更する値
* @param candidates
* @return 更新があればtrue
*/
template <typename T, typename... Ts>
inline bool chmax(T &target, Ts &&...candidates) {
return ch_op<max_op<T>>(target, candidates...);
}
/* #endregion */
/* #region template/Random.hpp*/
/**
* @file Random.hpp
* @author btk
* @brief 乱数生成系
* @version 0.1
* @date 2019-07-13
* @copyright Copyright (c) 2019
*/
//! 乱数のシード値をプロセスIDとして取得
const pid_t pid = getpid();
/**
* @brief XorShift32の実装
*/
class XorShift32 {
private:
//! XorShiftの現在の値
unsigned value;
/**
* @brief XorShift32のアルゴリズムに基づいて value を更新
*/
inline void update() {
value = value ^ (value << 13);
value = value ^ (value >> 17);
value = value ^ (value << 5);
}
/**
* @brief 値を更新し,更新前の値を返却
* @return unsigned 呼び出し時の value を用いる
*/
inline unsigned get() {
unsigned v = value;
update();
return v;
}
public:
/**
* @brief [0, 2^bit) の範囲の乱数値を取り出す
* @tparam デフォルトは31
* @return int
*/
template <int bit = 31> inline int next_int() {
return (int)(get() >> (32 - bit));
}
/**
* @brief [-2^bit,2^bit)の範囲の乱数値を取り出す
* @tparam デフォルトは31
* @return int
*/
template <int bit = 31> inline int next_signed() {
unsigned v = get();
return (int)((v >> (31 - bit)) - (1 << (bit)));
}
/**
* @brief next_int呼び出し時の最大値を取得
* @tparam 31
* @return int
*/
template <int bit = 31> inline int range_max() {
return (int)((1u << bit) - 1);
};
/**
* @brief Construct a new XorShift32 object
* @param seed
* @details 初期シードをvalueとするXorShift32のインスタンスを生成
*/
XorShift32(const unsigned seed) {
value = seed;
update();
}
/**
* @brief Construct a new XorShift 32 object
* @details 初期シードをプロセスIDとするXorShift32のインスタンスを生成
*/
XorShift32() : XorShift32(pid) {}
};
/* #endregion */
/* #region Template.hpp*/
/**
* @file Template.hpp
* @brief 競技プログラミング用テンプレート
* @author btk15049
* @date 2019/05/02
*/
/* #endregion */
/* #region string/RollingHash.hpp*/
/**
* @file RollingHash.hpp
* @brief ローリングハッシュ
* @author btk15049
* @date 2019/03/08
* @details
* verify: CSA12E,RUPC day3 E
*/
#include <string>
#include <vector>
/**
* @brief ローリングハッシュ
* @details
* ハッシュされている文字列のハッシュ値とポテンシャル(長さ)を管理.
*/
template <int base = 90001, int mod = 999999937> class RollingHash {
public:
//! 文字列のハッシュ値
long long hash;
//! ハッシュ化されている文字列長をnとすると,base^n %mod
long long potential;
/**
* @brief コンストラクタ
* @details 引数無しで呼び出しは,空文字列のハッシュ化に相当する。
*/
RollingHash(long long hash = 0, long long potential = 1)
: hash(hash % mod), potential(potential) {}
/**
* @brief concatenation
* @details
* ハッシュ値の合成を行う.ハッシュ化前の文字列を接続する操作にあたる.
* hash(s)+hash(t) == hash(s+t)
*/
inline RollingHash operator+(const RollingHash o) const {
return RollingHash((hash * o.potential + o.hash) % mod,
potential * o.potential % mod);
}
/**
* @brief ハッシュ値の等価判定
*/
inline bool operator==(const RollingHash o) const {
return hash == o.hash && potential == o.potential;
}
/**
* @brief 文字列をハッシュ化した配列を管理するクラス.
*/
class Table {
private:
//! str[i] := hash(s[0,i))
std::vector<RollingHash> str;
public:
/**
* @brief デフォルトコンストラクタ
*/
Table() {}
/**
* @brief コンストラクタ
* @details O(n)でhash(s[0,i))を全部求める.
*/
Table(const std::string s) {
const int n = s.size();
str.resize(n + 1);
for (int i = 0; i < n; i++) {
str[i + 1] = str[i] + RollingHash(s[i], base);
}
}
/**
* @brief 部分文字列のハッシュ値を取り出す
* @details
* ハッシュ値の合成を行う.ハッシュ化前の文字列を接続する操作にあたる.
* hashtable(s).substr(lb,ub) == hash(s[lb,ub))
*/
RollingHash substr(int lb, int ub) {
return RollingHash(str[ub].hash -
str[lb].hash * str[ub - lb].potential % mod + mod,
str[ub - lb].potential);
}
};
};
/* #endregion */
/* #endregion */
/*</body>*/
#endif
constexpr LL INF = 1e7;
using RH = RollingHash<>;
string s, t;
RH::Table S, T;
using I = __int128;
LL ret = 0;
RH rep(RH rh, I rep_num) {
RH ret;
while (rep_num > 0) {
if (rep_num & 1) {
ret = ret + rh;
}
rh = rh + rh;
rep_num >>= 1;
}
return ret;
}
bool check(int t_bg, LL s_rep) {
I s_size = s.size();
RH target = rep(S.substr(0, s.size()), s_rep);
if (t_bg + s_size * s_rep <= t.size()) {
return T.substr(t_bg, (int)(t_bg + s_size * s_rep)) == target;
} else {
I t_rep = (t_bg + s_size * s_rep) / t.size() - 1;
int t_mod = (int)((t_bg + (I)s.size() * s_rep) % t.size());
return T.substr(t_bg, t.size()) + rep(T.substr(0, t.size()), t_rep) +
T.substr(0, t_mod) ==
target;
}
}
LL solve(int t_bg) {
LL ok = ret;
LL ng = INF + 1;
while (abs(ok - ng) > 1) {
const LL md = (ok + ng) / 2;
(check(t_bg, md) ? ok : ng) = md;
}
return ok;
}
int main() {
cin >> t >> s;
// s = string(512345, 'a');
// t = string(512345, 'a');
// XorShift32 gen;
// for (int i : range(512345)) {
// s[i] = gen.next_int() % 26 + 'a';
// t[i] = gen.next_int() % 26 + 'a';
//}
S = RH::Table(s);
T = RH::Table(t);
for (int i : range(t.size())) {
if (t[i] != s[0])
continue;
chmax(ret, solve(i));
}
if (ret == INF)
ret = -1;
cout << ret << endl;
return 0;
} | // https://atcoder.jp/contests/abc135/tasks/abc135_f
#include <bits/stdc++.h>
#include <sys/types.h>
#include <unistd.h>
#define CIN_ONLY
#define DECIMAL_DIGITS 10
#define STATIC_MOD 1e9 + 7
#ifdef BTK
/*<head>*/
#include "Template.hpp"
#include "string/RollingHash.hpp"
/*</head>*/
#else
/*<body>*/
/* #region auto includes */
/* #region stl */
/*<stl>*/
#include <bits/stdc++.h>
#include <sys/types.h>
#include <unistd.h>
using namespace std;
/*</stl>*/
/* #endregion */
/* #region template/IncludeSTL.hpp*/
/**
* @file IncludeSTL.hpp
* @author btk
* @brief 標準ライブラリをincludeするだけ
* @version 0.1
* @date 2019-07-21
*
* @copyright Copyright (c) 2019
*
*/
/*<head>*/
#pragma once
/*</head>*/
/*<stl>*/
#include <bits/stdc++.h>
#include <sys/types.h>
#include <unistd.h>
using namespace std;
/*</stl>*/
/* #endregion */
/* #region template/Macro.hpp*/
/**
* @file Macro.hpp
* @author btk
* @brief マクロとか,LLとか
* @version 0.1
* @date 2019-07-13
*
* @copyright Copyright (c) 2019
*
*/
//! LL
using LL = long long;
/**
* @def DEBUG
* @brief デバッグ用のif文 提出時はif(0)で実行されない
*/
/*</head>*/
#ifdef BTK
#define DEBUG if (1)
#else
#ifdef CIN_ONLY
#define FAST_IO
#endif
#define DEBUG if (0)
#endif
/**
* @def ALL(v)
* @brief
* ALLマクロ
*/
#define ALL(v) (v).begin(), (v).end()
/**
* @def REC(ret, ...)
* @brief
* 再帰ラムダをするためのマクロ
*/
#define REC(ret, ...) std::function<ret(__VA_ARGS__)>
/**
* @def VAR_NAME(var)
* @brief 変数名を取得する
*/
#define VAR_NAME(var) #var
/**
* @brief
* rangeで生まれる使わない変数を消す用(警告消し)
*/
template <typename T> inline T &unused_var(T &v) { return v; }
/* #endregion */
/* #region template/IO.hpp*/
/**
* @file IO.hpp
* @author btk
* @brief cin高速化とか,出力の小数桁固定とか
* @version 0.1
* @date 2019-07-13
*
* @copyright Copyright (c) 2019
*/
/**
* @brief 入出力の設定を行うための構造体
*/
struct cww {
/**
* @brief Construct a new cww::cww object
* @details
* CIN_ONLYを定義すると,submit時にcinとscanfの同期を切る設定が走る
* DECIMAL_DIGITSを定義すると,doubleの出力時指定した桁数分小数部を吐くようになる
*/
cww() {
#ifdef FAST_IO
ios::sync_with_stdio(false);
cin.tie(0);
#endif
#ifdef DECIMAL_DIGITS
cout << fixed;
cout << setprecision(DECIMAL_DIGITS);
#endif
}
};
//! 入出力設定構造体を実体化
cww star;
/**
* @brief
* vectorに直接cin流すためのやつ
* @tparam T
* @param is
* @param v
* @return istream&
*/
template <typename T>
std::istream &operator>>(std::istream &is, std::vector<T> &v) {
for (auto &it : v)
is >> it;
return is;
}
/* #endregion */
/* #region template/Loop.hpp*/
/**
* @file Loop.hpp
* @author btk
* @brief rangeとかループ系のクラス
* @version 0.1
* @date 2019-07-13
*
* @copyright Copyright (c) 2019
*
*/
/**
* @brief
* rangeを逆向きに操作したいとき用
* @details
* ループの範囲は[bg,ed)なので注意
* @see range
*/
class reverse_range {
private:
struct I {
int x;
int operator*() { return x - 1; }
bool operator!=(I &lhs) { return x > lhs.x; }
void operator++() { --x; }
};
I i, n;
public:
/**
* @brief Construct a new reverse range object
*
* @param n
*/
reverse_range(int n) : i({0}), n({n}) {}
/**
* @brief Construct a new reverse range object
*
* @param i
* @param n
*/
reverse_range(int i, int n) : i({i}), n({n}) {}
/**
* @brief
* begin iterator
* @return I&
*/
I &begin() { return n; }
/**
* @brief
* end iterator
* @return I&
*/
I &end() { return i; }
};
/**
* @brief
* python みたいな range-based for を実現
* @details
* ループの範囲は[bg,ed)なので注意
* !つけると逆向きにループが回る (reverse_range)
* 空間計算量はO(1)
* 使わない変数ができて警告が出がちなので,unused_varとかを使って警告消しするとよい
*/
class range {
private:
struct I {
int x;
int operator*() { return x; }
bool operator!=(I &lhs) { return x < lhs.x; }
void operator++() { ++x; }
};
I i, n;
public:
/**
* @brief Construct a new range object
*
* @param n
*/
range(int n) : i({0}), n({n}) {}
/**
* @brief Construct a new range object
*
* @param i
* @param n
*/
range(int i, int n) : i({i}), n({n}) {}
/**
* @brief
* begin iterator
* @return I&
*/
I &begin() { return i; }
/**
* @brief
* end iterator
* @return I&
*/
I &end() { return n; }
/**
* @brief
* 逆向きに参照するrange(reverse_rangeを取得できるs)
* @return reverse_range
*/
reverse_range operator!() { return reverse_range(*i, *n); }
};
/* #endregion */
/* #region template/MinMaxOperation.hpp*/
/**
* @file MinMaxOperation.hpp
* @author btk
* @brief 最大値とか最小値を求める
* @version 0.1
* @date 2019-07-04
*
* @copyright Copyright (c) 2019
*
*/
/**
* @brief 2項の最小値求める
*
* @tparam T
*/
template <typename T> struct min_op {
/**
* @brief 本体
*
* @param l
* @param r
* @return T
*/
static T exec(const T l, const T r) { return l < r ? l : r; }
};
/**
* @brief 2項の最大値求める
*
* @tparam T
*/
template <typename T> struct max_op {
/**
* @brief 本体
*
* @param l
* @param r
* @return T
*/
static T exec(const T l, const T r) { return l > r ? l : r; }
};
/**
* @brief テンプレート再帰の末尾
*
* @tparam F 二項演算
* @tparam T
* @param v
* @return T
*/
template <typename F, typename T> inline T multi_op(T &&v) { return v; }
/**
* @brief 複数項における演算の結果を返す
*
* @tparam F
* @tparam T
* @tparam Ts
* @param head
* @param tail
* @return T
*/
template <typename F, typename T, typename... Ts>
inline T multi_op(const T head, Ts &&...tail) {
return F::exec(head, multi_op<F>(tail...));
}
/**
* @brief 複数項の最小値
* @see multi_op
* @tparam T
* @tparam Ts
* @param head
* @param tail
* @return T
*/
template <typename T, typename... Ts>
inline T multi_min(T &&head, Ts &&...tail) {
return multi_op<min_op<T>>(head, tail...);
}
/**
* @brief 複数項の最大値
* @see multi_op
* @tparam T
* @tparam Ts
* @param head
* @param tail
* @return T
*/
template <typename T, typename... Ts>
inline T multi_max(T &&head, Ts &&...tail) {
return multi_op<max_op<T>>(head, tail...);
}
/**
* @brief
* 先頭の値をFで参照する関数に基づいて変更できたらする
* @tparam F
* @tparam T
* @tparam Ts
* @param target
* @param candidates
* @return true
* @return false
*/
template <typename F, typename T, typename... Ts>
inline bool ch_op(T &target, Ts &&...candidates) {
const T old = target;
target = multi_op<F>(target, candidates...);
return old != target;
}
/**
* @brief change min
* @tparam T 型
* @param target 変更する値
* @param candidates
* @return 更新があればtrue
*/
template <typename T, typename... Ts>
inline bool chmin(T &target, Ts &&...candidates) {
return ch_op<min_op<T>>(target, candidates...);
}
/**
* @brief chminのmax版
* @see chmin
* @tparam T 型
* @param target 変更する値
* @param candidates
* @return 更新があればtrue
*/
template <typename T, typename... Ts>
inline bool chmax(T &target, Ts &&...candidates) {
return ch_op<max_op<T>>(target, candidates...);
}
/* #endregion */
/* #region template/Random.hpp*/
/**
* @file Random.hpp
* @author btk
* @brief 乱数生成系
* @version 0.1
* @date 2019-07-13
* @copyright Copyright (c) 2019
*/
//! 乱数のシード値をプロセスIDとして取得
const pid_t pid = getpid();
/**
* @brief XorShift32の実装
*/
class XorShift32 {
private:
//! XorShiftの現在の値
unsigned value;
/**
* @brief XorShift32のアルゴリズムに基づいて value を更新
*/
inline void update() {
value = value ^ (value << 13);
value = value ^ (value >> 17);
value = value ^ (value << 5);
}
/**
* @brief 値を更新し,更新前の値を返却
* @return unsigned 呼び出し時の value を用いる
*/
inline unsigned get() {
unsigned v = value;
update();
return v;
}
public:
/**
* @brief [0, 2^bit) の範囲の乱数値を取り出す
* @tparam デフォルトは31
* @return int
*/
template <int bit = 31> inline int next_int() {
return (int)(get() >> (32 - bit));
}
/**
* @brief [-2^bit,2^bit)の範囲の乱数値を取り出す
* @tparam デフォルトは31
* @return int
*/
template <int bit = 31> inline int next_signed() {
unsigned v = get();
return (int)((v >> (31 - bit)) - (1 << (bit)));
}
/**
* @brief next_int呼び出し時の最大値を取得
* @tparam 31
* @return int
*/
template <int bit = 31> inline int range_max() {
return (int)((1u << bit) - 1);
};
/**
* @brief Construct a new XorShift32 object
* @param seed
* @details 初期シードをvalueとするXorShift32のインスタンスを生成
*/
XorShift32(const unsigned seed) {
value = seed;
update();
}
/**
* @brief Construct a new XorShift 32 object
* @details 初期シードをプロセスIDとするXorShift32のインスタンスを生成
*/
XorShift32() : XorShift32(pid) {}
};
/* #endregion */
/* #region Template.hpp*/
/**
* @file Template.hpp
* @brief 競技プログラミング用テンプレート
* @author btk15049
* @date 2019/05/02
*/
/* #endregion */
/* #region string/RollingHash.hpp*/
/**
* @file RollingHash.hpp
* @brief ローリングハッシュ
* @author btk15049
* @date 2019/03/08
* @details
* verify: CSA12E,RUPC day3 E
*/
#include <string>
#include <vector>
/**
* @brief ローリングハッシュ
* @details
* ハッシュされている文字列のハッシュ値とポテンシャル(長さ)を管理.
*/
template <int base = 90001, int mod = 999999937> class RollingHash {
public:
//! 文字列のハッシュ値
long long hash;
//! ハッシュ化されている文字列長をnとすると,base^n %mod
long long potential;
/**
* @brief コンストラクタ
* @details 引数無しで呼び出しは,空文字列のハッシュ化に相当する。
*/
RollingHash(long long hash = 0, long long potential = 1)
: hash(hash % mod), potential(potential) {}
/**
* @brief concatenation
* @details
* ハッシュ値の合成を行う.ハッシュ化前の文字列を接続する操作にあたる.
* hash(s)+hash(t) == hash(s+t)
*/
inline RollingHash operator+(const RollingHash o) const {
return RollingHash((hash * o.potential + o.hash) % mod,
potential * o.potential % mod);
}
/**
* @brief ハッシュ値の等価判定
*/
inline bool operator==(const RollingHash o) const {
return hash == o.hash && potential == o.potential;
}
/**
* @brief 文字列をハッシュ化した配列を管理するクラス.
*/
class Table {
private:
//! str[i] := hash(s[0,i))
std::vector<RollingHash> str;
public:
/**
* @brief デフォルトコンストラクタ
*/
Table() {}
/**
* @brief コンストラクタ
* @details O(n)でhash(s[0,i))を全部求める.
*/
Table(const std::string s) {
const int n = s.size();
str.resize(n + 1);
for (int i = 0; i < n; i++) {
str[i + 1] = str[i] + RollingHash(s[i], base);
}
}
/**
* @brief 部分文字列のハッシュ値を取り出す
* @details
* ハッシュ値の合成を行う.ハッシュ化前の文字列を接続する操作にあたる.
* hashtable(s).substr(lb,ub) == hash(s[lb,ub))
*/
RollingHash substr(int lb, int ub) {
return RollingHash(str[ub].hash -
str[lb].hash * str[ub - lb].potential % mod + mod,
str[ub - lb].potential);
}
};
};
/* #endregion */
/* #endregion */
/*</body>*/
#endif
constexpr LL INF = 1e6;
using RH = RollingHash<>;
string s, t;
RH::Table S, T;
using I = __int128;
LL ret = 0;
RH rep(RH rh, I rep_num) {
RH ret;
while (rep_num > 0) {
if (rep_num & 1) {
ret = ret + rh;
}
rh = rh + rh;
rep_num >>= 1;
}
return ret;
}
bool check(int t_bg, LL s_rep) {
I s_size = s.size();
RH target = rep(S.substr(0, s.size()), s_rep);
if (t_bg + s_size * s_rep <= t.size()) {
return T.substr(t_bg, (int)(t_bg + s_size * s_rep)) == target;
} else {
I t_rep = (t_bg + s_size * s_rep) / t.size() - 1;
int t_mod = (int)((t_bg + (I)s.size() * s_rep) % t.size());
return T.substr(t_bg, t.size()) + rep(T.substr(0, t.size()), t_rep) +
T.substr(0, t_mod) ==
target;
}
}
LL solve(int t_bg) {
LL ok = ret;
LL ng = INF + 1;
while (abs(ok - ng) > 1) {
const LL md = (ok + ng) / 2;
(check(t_bg, md) ? ok : ng) = md;
}
return ok;
}
int main() {
cin >> t >> s;
// s = string(512345, 'a');
// t = string(512345, 'a');
// XorShift32 gen;
// for (int i : range(512345)) {
// s[i] = gen.next_int() % 26 + 'a';
// t[i] = gen.next_int() % 26 + 'a';
//}
S = RH::Table(s);
T = RH::Table(t);
for (int i : range(t.size())) {
if (t[i] != s[0])
continue;
chmax(ret, solve(i));
}
if (ret == INF)
ret = -1;
cout << ret << endl;
return 0;
} | replace | 590 | 591 | 590 | 591 | TLE | |
p02962 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif
#define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = 1'000'000'007;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template <typename T> vector<T> gen_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto gen_v(size_t a, Ts... ts) {
return vector<decltype(gen_v<T>(ts...))>(a, gen_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
for (auto &e : t)
fill_v(e, v);
}
template <long long MOD> struct RollingHash {
int n;
long long base;
vector<long long> pow, hash;
RollingHash() {}
RollingHash(const string &s, long long base_ = 10007) { init(s, base_); }
void init(const string &s, long long base_ = 10007) {
n = s.size();
base = base_;
calc(s.c_str());
}
long long get(int x) const { return hash[x]; }
long long get(int l, int r) const {
return (get(r) - get(l) * pow[r - l] % MOD + MOD) % MOD;
}
void calc(const char *s) {
pow.resize(n + 1);
pow[0] = 1;
hash.resize(n + 1);
hash[0] = 0;
for (int i = 0; i < n; i++) {
pow[i + 1] = pow[i] * base % MOD;
hash[i + 1] = (s[i] + hash[i] * base) % MOD;
}
}
};
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
string s, t;
cin >> s >> t;
while (s.size() <= t.size()) {
s += s;
}
s += s; // s.size() * 2 > t.size()
int n = t.size();
while (s.size() >= t.size()) {
t += t;
}
// s.size() < t.size()
const int MOD2 = 1000000009;
RollingHash<MOD> S(s), T(t);
RollingHash<MOD2> S2(s, 10009), T2(t, 10009);
auto f = [&](int x) {
int hash = T.get(0, n * x);
int hash2 = T2.get(0, n * x);
rep(i, 0, s.size()) {
if (i + n * x >= s.size())
break;
if (S.get(i, i + n * x) == hash && S2.get(i, i + n * x) == hash2)
return false;
}
return true;
};
auto binarySearch = [&](int ng, int ok) {
if (f(ng))
return ng;
while (ng + 1 < ok) {
int m = (ng + ok) / 2;
if (f(m))
ok = m;
else
ng = m;
}
return ok;
};
dump(s, t);
int ans = binarySearch(1, t.size() / n + 1) - 1;
s += s;
t += t;
S = RollingHash<MOD>(s);
T = RollingHash<MOD>(t);
S2 = RollingHash<MOD2>(s, 10009);
T2 = RollingHash<MOD2>(t, 10009);
int ans2 = binarySearch(1, t.size() / n + 1) - 1;
int cnt = 0;
while (ans != ans2 && cnt < 2) {
ans = ans2;
s += s;
t += t;
S = RollingHash<MOD>(s);
T = RollingHash<MOD>(t);
S2 = RollingHash<MOD2>(s, 10009);
T2 = RollingHash<MOD2>(t, 10009);
ans2 = binarySearch(1, t.size() / n + 1) - 1;
cnt++;
}
if (ans != ans2) {
cout << -1 << endl;
} else {
cout << ans << endl;
}
return 0;
} | #include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif
#define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = 1'000'000'007;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template <typename T> vector<T> gen_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto gen_v(size_t a, Ts... ts) {
return vector<decltype(gen_v<T>(ts...))>(a, gen_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
for (auto &e : t)
fill_v(e, v);
}
template <long long MOD> struct RollingHash {
int n;
long long base;
vector<long long> pow, hash;
RollingHash() {}
RollingHash(const string &s, long long base_ = 10007) { init(s, base_); }
void init(const string &s, long long base_ = 10007) {
n = s.size();
base = base_;
calc(s.c_str());
}
long long get(int x) const { return hash[x]; }
long long get(int l, int r) const {
return (get(r) - get(l) * pow[r - l] % MOD + MOD) % MOD;
}
void calc(const char *s) {
pow.resize(n + 1);
pow[0] = 1;
hash.resize(n + 1);
hash[0] = 0;
for (int i = 0; i < n; i++) {
pow[i + 1] = pow[i] * base % MOD;
hash[i + 1] = (s[i] + hash[i] * base) % MOD;
}
}
};
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
string s, t;
cin >> s >> t;
while (s.size() <= t.size()) {
s += s;
}
s += s; // s.size() * 2 > t.size()
int n = t.size();
while (s.size() >= t.size()) {
t += t;
}
// s.size() < t.size()
const int MOD2 = 1000000009;
RollingHash<MOD> S(s), T(t);
RollingHash<MOD2> S2(s, 10009), T2(t, 10009);
auto f = [&](int x) {
int hash = T.get(0, n * x);
int hash2 = T2.get(0, n * x);
rep(i, 0, s.size()) {
if (i + n * x >= s.size())
break;
if (S.get(i, i + n * x) == hash && S2.get(i, i + n * x) == hash2)
return false;
}
return true;
};
auto binarySearch = [&](int ng, int ok) {
if (f(ng))
return ng;
while (ng + 1 < ok) {
int m = (ng + ok) / 2;
if (f(m))
ok = m;
else
ng = m;
}
return ok;
};
dump(s, t);
int ans = binarySearch(1, t.size() / n + 1) - 1;
s += s;
t += t;
S = RollingHash<MOD>(s);
T = RollingHash<MOD>(t);
S2 = RollingHash<MOD2>(s, 10009);
T2 = RollingHash<MOD2>(t, 10009);
int ans2 = binarySearch(1, t.size() / n + 1) - 1;
int cnt = 0;
while (ans != ans2 && cnt < 1) {
ans = ans2;
s += s;
t += t;
S = RollingHash<MOD>(s);
T = RollingHash<MOD>(t);
S2 = RollingHash<MOD2>(s, 10009);
T2 = RollingHash<MOD2>(t, 10009);
ans2 = binarySearch(1, t.size() / n + 1) - 1;
cnt++;
}
if (ans != ans2) {
cout << -1 << endl;
} else {
cout << ans << endl;
}
return 0;
} | replace | 123 | 124 | 123 | 124 | TLE | |
p02962 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <complex>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vec;
typedef vector<vec> mat;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define revrep(i, n) for (ll i = (n - 1); i >= 0; i--)
#define pb push_back
#define f first
#define s second
ll max(ll a, ll b) { return (a > b) ? a : b; }
ll min(ll a, ll b) { return (a < b) ? a : b; }
ll max3(ll a, ll b, ll c) { return max(a, max(b, c)); };
ll min3(ll a, ll b, ll c) { return min(a, min(b, c)); };
ll max4(ll a, ll b, ll c, ll d) { return max(max(a, b), min(c, d)); };
ll min4(ll a, ll b, ll c, ll d) { return min(min(a, b), min(c, d)); };
ll max5(ll a, ll b, ll c, ll d, ll e) { return max(max(a, b), max3(c, d, e)); };
ll min5(ll a, ll b, ll c, ll d, ll e) { return min(min(a, b), min3(c, d, e)); };
const ll INFL = 1LL << 60; // 10^18 = 2^60
const int INF = 1 << 30; // 10^9
ll MOD = 1000000007;
// ll MOD = 998244353;
vector<ll> dy = {0, 0, 1, -1, 1, 1, -1, -1, 0};
vector<ll> dx = {1, -1, 0, 0, 1, -1, 1, -1, 0};
ll pow_long(ll x, ll k) {
ll res = 1;
while (k > 0) {
if (k % 2)
res *= x;
x *= x;
k /= 2;
}
return res;
}
ll pow_mod(ll x, ll k) {
x %= MOD;
x += MOD;
x %= MOD;
ll res = 1;
while (k > 0) {
if (k % 2) {
res *= x;
res %= MOD;
}
x *= x;
x %= MOD;
k /= 2;
}
return res;
}
ll inverse(ll x) { return pow_mod(x, MOD - 2); };
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; };
ll kai_mod(ll x) {
if (x == 0)
return 1;
return x * kai_mod(x - 1) % MOD;
}
/*
//コンビネーション
const int MAXcomb = 200010;
ll fac[MAXcomb], finv[MAXcomb], inv[MAXcomb];
//facはn!,finvは1/n!
//invは逆元
void COMinit(){
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for(int i = 2; i < MAXcomb; i++){
fac[i] = fac[i-1] * i % MOD;
inv[i] = MOD - inv[MOD%i] * (MOD/i) % MOD;
finv[i] = finv[i-1] * inv[i] % MOD;
}
}
ll comb(int n, int k){
if(n < k) return 0;
if(n < 0 || k < 0) return 0;
return fac[n] * finv[k] % MOD * finv[n-k] % MOD;
}
*/
string S, T;
vector<bool> canStart;
const ll MAX_V = 200010;
ll par[MAX_V]; //
ll Rank[MAX_V];
ll child[MAX_V];
void init_UF() {
for (ll i = 0; i < MAX_V; i++) {
par[i] = i;
Rank[i] = 1;
if (canStart[i])
child[i] = 1;
}
}
ll find(ll x) {
if (par[x] == x)
return x;
else {
return par[x] = find(par[x]);
}
}
bool same(ll x, ll y) { return find(x) == find(y); }
void unite(ll x, ll y) {
ll rx = find(x), ry = find(y);
if (rx != ry) {
if (Rank[rx] >= Rank[ry]) {
par[ry] = rx;
if (Rank[rx] == Rank[ry])
Rank[rx]++;
child[rx] += child[ry];
} else {
par[rx] = ry;
child[ry] += child[rx];
}
}
}
int main() {
cin >> S >> T;
canStart.resize(S.size(), 0);
ll a = 998244353;
ll hashS = 0, hashT = 0;
rep(i, T.size()) { hashS = (hashS * a + S[i % S.size()]) % MOD; }
rep(i, T.size()) { hashT = (hashT * a + T[i]) % MOD; }
ll po = pow_mod(a, T.size());
rep(i, S.size()) {
if (hashS == hashT)
canStart[i] = 1;
hashS =
((hashS * a + S[(i + T.size()) % S.size()] - S[i] * po) % MOD + MOD) %
MOD;
}
init_UF();
bool inf = 0;
rep(i, S.size()) {
ll to = (i + T.size()) % S.size();
if (canStart[i] == 0 || canStart[to] == 0)
continue;
if (same(i, to))
inf = 1;
unite(i, to);
}
ll ans = 0;
if (inf)
cout << -1 << endl;
else {
rep(i, S.size()) { ans = max(ans, child[i]); }
cout << ans << endl;
}
}
| #include <algorithm>
#include <bitset>
#include <complex>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vec;
typedef vector<vec> mat;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define revrep(i, n) for (ll i = (n - 1); i >= 0; i--)
#define pb push_back
#define f first
#define s second
ll max(ll a, ll b) { return (a > b) ? a : b; }
ll min(ll a, ll b) { return (a < b) ? a : b; }
ll max3(ll a, ll b, ll c) { return max(a, max(b, c)); };
ll min3(ll a, ll b, ll c) { return min(a, min(b, c)); };
ll max4(ll a, ll b, ll c, ll d) { return max(max(a, b), min(c, d)); };
ll min4(ll a, ll b, ll c, ll d) { return min(min(a, b), min(c, d)); };
ll max5(ll a, ll b, ll c, ll d, ll e) { return max(max(a, b), max3(c, d, e)); };
ll min5(ll a, ll b, ll c, ll d, ll e) { return min(min(a, b), min3(c, d, e)); };
const ll INFL = 1LL << 60; // 10^18 = 2^60
const int INF = 1 << 30; // 10^9
ll MOD = 1000000007;
// ll MOD = 998244353;
vector<ll> dy = {0, 0, 1, -1, 1, 1, -1, -1, 0};
vector<ll> dx = {1, -1, 0, 0, 1, -1, 1, -1, 0};
ll pow_long(ll x, ll k) {
ll res = 1;
while (k > 0) {
if (k % 2)
res *= x;
x *= x;
k /= 2;
}
return res;
}
ll pow_mod(ll x, ll k) {
x %= MOD;
x += MOD;
x %= MOD;
ll res = 1;
while (k > 0) {
if (k % 2) {
res *= x;
res %= MOD;
}
x *= x;
x %= MOD;
k /= 2;
}
return res;
}
ll inverse(ll x) { return pow_mod(x, MOD - 2); };
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; };
ll kai_mod(ll x) {
if (x == 0)
return 1;
return x * kai_mod(x - 1) % MOD;
}
/*
//コンビネーション
const int MAXcomb = 200010;
ll fac[MAXcomb], finv[MAXcomb], inv[MAXcomb];
//facはn!,finvは1/n!
//invは逆元
void COMinit(){
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for(int i = 2; i < MAXcomb; i++){
fac[i] = fac[i-1] * i % MOD;
inv[i] = MOD - inv[MOD%i] * (MOD/i) % MOD;
finv[i] = finv[i-1] * inv[i] % MOD;
}
}
ll comb(int n, int k){
if(n < k) return 0;
if(n < 0 || k < 0) return 0;
return fac[n] * finv[k] % MOD * finv[n-k] % MOD;
}
*/
string S, T;
vector<bool> canStart;
const ll MAX_V = 1000010;
ll par[MAX_V]; //
ll Rank[MAX_V];
ll child[MAX_V];
void init_UF() {
for (ll i = 0; i < MAX_V; i++) {
par[i] = i;
Rank[i] = 1;
if (canStart[i])
child[i] = 1;
}
}
ll find(ll x) {
if (par[x] == x)
return x;
else {
return par[x] = find(par[x]);
}
}
bool same(ll x, ll y) { return find(x) == find(y); }
void unite(ll x, ll y) {
ll rx = find(x), ry = find(y);
if (rx != ry) {
if (Rank[rx] >= Rank[ry]) {
par[ry] = rx;
if (Rank[rx] == Rank[ry])
Rank[rx]++;
child[rx] += child[ry];
} else {
par[rx] = ry;
child[ry] += child[rx];
}
}
}
int main() {
cin >> S >> T;
canStart.resize(S.size(), 0);
ll a = 998244353;
ll hashS = 0, hashT = 0;
rep(i, T.size()) { hashS = (hashS * a + S[i % S.size()]) % MOD; }
rep(i, T.size()) { hashT = (hashT * a + T[i]) % MOD; }
ll po = pow_mod(a, T.size());
rep(i, S.size()) {
if (hashS == hashT)
canStart[i] = 1;
hashS =
((hashS * a + S[(i + T.size()) % S.size()] - S[i] * po) % MOD + MOD) %
MOD;
}
init_UF();
bool inf = 0;
rep(i, S.size()) {
ll to = (i + T.size()) % S.size();
if (canStart[i] == 0 || canStart[to] == 0)
continue;
if (same(i, to))
inf = 1;
unite(i, to);
}
ll ans = 0;
if (inf)
cout << -1 << endl;
else {
rep(i, S.size()) { ans = max(ans, child[i]); }
cout << ans << endl;
}
}
| replace | 111 | 112 | 111 | 112 | 0 | |
p02962 | C++ | Runtime Error | #include <bits/stdc++.h>
#define For(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define rFor(i, a, b) for (int(i) = (a)-1; (i) >= (b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef pair<int, int> pii;
const lint b1 = 1009;
const lint b2 = 1007;
const lint b3 = 997;
const lint h1 = 1e9 + 7;
const lint h2 = 1e9 + 9;
const lint h3 = 998244353;
lint powmod(lint x, lint n, lint mod) {
lint ret = 1;
while (n > 0) {
if (n & 1)
ret *= x, ret %= mod, n -= 1;
else
x *= x, x %= mod, n >>= 1;
}
return ret;
}
int d[500010], n, m;
vector<int> G[500010];
void scc_dfs(int v, vector<bool> &used, vector<int> &vs) {
used[v] = true;
for (int nv : G[v])
if (!used[nv])
scc_dfs(nv, used, vs);
vs.push_back(v);
}
void scc_rdfs(int v, int k, vector<int> &cmp, vector<bool> &used,
vector<vector<int>> &rG) {
used[v] = true;
cmp[v] = k;
for (int nv : rG[v])
if (!used[nv])
scc_rdfs(nv, k, cmp, used, rG);
}
pair<int, vector<int>> scc() {
vector<vector<int>> rG(n);
rep(i, n) for (int nv : G[i]) rG[nv].push_back(i);
vector<bool> used(n, false);
vector<int> vs;
vector<int> cmp(n);
rep(i, n) if (!used[i]) scc_dfs(i, used, vs);
fill(used.begin(), used.end(), false);
int k = 0;
rrep(i, n) if (!used[vs[i]]) scc_rdfs(vs[i], k++, cmp, used, rG);
return make_pair(k, cmp);
}
int longest_path(int v) {
if (d[v] >= 0)
return d[v];
int ret = 0;
for (int nv : G[v])
ret = max(ret, longest_path(nv) + 1);
return d[v] = ret;
}
int main() {
string s, t;
cin >> s >> t;
n = s.size();
m = t.size();
lint th1 = 0;
lint th2 = 0;
lint th3 = 0;
rep(i, m) {
th1 = (th1 * b1 + (t[i] - 'a')) % h1;
th2 = (th2 * b2 + (t[i] - 'a')) % h2;
th3 = (th3 * b3 + (t[i] - 'a')) % h3;
}
lint sh1 = 0;
lint sh2 = 0;
lint sh3 = 0;
rep(i, m) {
sh1 = (sh1 * b1 + (s[i % n] - 'a')) % h1;
sh2 = (sh2 * b2 + (s[i % n] - 'a')) % h2;
sh3 = (sh3 * b3 + (s[i % n] - 'a')) % h3;
}
int num[n];
rep(i, n) num[i] = 0;
if (sh1 == th1 && sh2 == th2 && sh3 == th3)
++num[0];
lint p1 = powmod(b1, m, h1);
lint p2 = powmod(b2, m, h2);
lint p3 = powmod(b3, m, h3);
rep(i, n - 1) {
sh1 =
(sh1 * b1 % h1 - p1 * (s[i] - 'a') % h1 + (s[(i + m) % n] - 'a') + h1) %
h1;
sh2 =
(sh2 * b2 % h2 - p2 * (s[i] - 'a') % h2 + (s[(i + m) % n] - 'a') + h2) %
h2;
sh3 =
(sh3 * b3 % h3 - p3 * (s[i] - 'a') % h3 + (s[(i + m) % n] - 'a') + h3) %
h3;
if (sh1 == th1 && sh2 == th2 && sh3 == th3)
++num[i + 1];
}
rep(i, n) {
if (num[i])
G[i].push_back((i + m) % n);
d[i] = -1;
}
auto scc_result = scc();
if (scc_result.fi < n) {
printf("-1");
return 0;
}
int ans = 0;
rep(i, n) ans = max(ans, longest_path(i));
printf("%d\n", ans);
} | #include <bits/stdc++.h>
#define For(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define rFor(i, a, b) for (int(i) = (a)-1; (i) >= (b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef pair<int, int> pii;
const lint b1 = 1009;
const lint b2 = 1007;
const lint b3 = 997;
const lint h1 = 1e9 + 7;
const lint h2 = 1e9 + 9;
const lint h3 = 998244353;
lint powmod(lint x, lint n, lint mod) {
lint ret = 1;
while (n > 0) {
if (n & 1)
ret *= x, ret %= mod, n -= 1;
else
x *= x, x %= mod, n >>= 1;
}
return ret;
}
int d[500010], n, m;
vector<int> G[500010];
void scc_dfs(int v, vector<bool> &used, vector<int> &vs) {
used[v] = true;
for (int nv : G[v])
if (!used[nv])
scc_dfs(nv, used, vs);
vs.push_back(v);
}
void scc_rdfs(int v, int k, vector<int> &cmp, vector<bool> &used,
vector<vector<int>> &rG) {
used[v] = true;
cmp[v] = k;
for (int nv : rG[v])
if (!used[nv])
scc_rdfs(nv, k, cmp, used, rG);
}
pair<int, vector<int>> scc() {
vector<vector<int>> rG(n);
rep(i, n) for (int nv : G[i]) rG[nv].push_back(i);
vector<bool> used(n, false);
vector<int> vs;
vector<int> cmp(n);
rep(i, n) if (!used[i]) scc_dfs(i, used, vs);
fill(used.begin(), used.end(), false);
int k = 0;
rrep(i, n) if (!used[vs[i]]) scc_rdfs(vs[i], k++, cmp, used, rG);
return make_pair(k, cmp);
}
int longest_path(int v) {
if (d[v] >= 0)
return d[v];
int ret = 0;
for (int nv : G[v])
ret = max(ret, longest_path(nv) + 1);
return d[v] = ret;
}
int main() {
string s, t;
cin >> s >> t;
n = s.size();
m = t.size();
lint th1 = 0;
lint th2 = 0;
lint th3 = 0;
rep(i, m) {
th1 = (th1 * b1 + (t[i] - 'a')) % h1;
th2 = (th2 * b2 + (t[i] - 'a')) % h2;
th3 = (th3 * b3 + (t[i] - 'a')) % h3;
}
lint sh1 = 0;
lint sh2 = 0;
lint sh3 = 0;
rep(i, m) {
sh1 = (sh1 * b1 + (s[i % n] - 'a')) % h1;
sh2 = (sh2 * b2 + (s[i % n] - 'a')) % h2;
sh3 = (sh3 * b3 + (s[i % n] - 'a')) % h3;
}
int num[n];
rep(i, n) num[i] = 0;
if (sh1 == th1 && sh2 == th2 && sh3 == th3)
++num[0];
lint p1 = powmod(b1, m, h1);
lint p2 = powmod(b2, m, h2);
lint p3 = powmod(b3, m, h3);
rep(i, n - 1) {
sh1 =
(sh1 * b1 % h1 - p1 * (s[i] - 'a') % h1 + (s[(i + m) % n] - 'a') + h1) %
h1;
sh2 =
(sh2 * b2 % h2 - p2 * (s[i] - 'a') % h2 + (s[(i + m) % n] - 'a') + h2) %
h2;
sh3 =
(sh3 * b3 % h3 - p3 * (s[i] - 'a') % h3 + (s[(i + m) % n] - 'a') + h3) %
h3;
if (sh1 == th1 && sh2 == th2 && sh3 == th3)
++num[i + 1];
}
rep(i, n) {
if (num[i])
G[i].push_back((i + m) % n);
if (num[i] && i == (i + m) % n) {
printf("-1\n");
return 0;
}
d[i] = -1;
}
auto scc_result = scc();
if (scc_result.fi < n) {
printf("-1");
return 0;
}
int ans = 0;
rep(i, n) ans = max(ans, longest_path(i));
printf("%d\n", ans);
} | insert | 115 | 115 | 115 | 119 | 0 | |
p02962 | C++ | Runtime Error | // Head File
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;
#define il inline
// Variable
#define ll long long
#define ull unsigned long long
#define db double
#define lb long double
#define ui unsigned int
// Debug
#define B cerr << "Break Point" << endl;
#define P(x) cerr << #x << ' ' << "=" << ' ' << (x) << endl;
#define p(x) cerr << #x << ' ' << "=" << ' ' << (x) << ' ';
#define ml(x) \
cerr << "Size of array is " << x * 4 / 1024 / 1024 << " MB" << endl;
// Vector
#define vc vector
#define puf push_front
#define pof pop_front
#define pub push_back
#define pob pop_back
#define vbe(x) x.begin(), x.end()
// Memset
#define ms(x) memset(x, 0, sizeof(x))
#define MS(x) memset(x, 0x3f3f3f3f, sizeof(x))
// Pair
#define fi first
#define se second
// File
#define fin(x) freopen(x, "r", stdin)
#define fou(x) freopen(x, "w", stdout)
void fio() {
#ifndef ONLINE_JUDGE
freopen("sample.in", "r", stdin);
freopen("sample.out", "w", stdout);
#endif
}
void pti() {
double timeuse = clock() * 1000.0 / CLOCKS_PER_SEC;
cerr << "Timeuse " << timeuse << "ms" << endl;
}
void end() {
#ifndef ONLINE_JUDGE
pti();
#endif
exit(0);
}
// Inf
#define INF 0x3f3f3f3f
#define LINF ((long long)(0x3f3f3f3f3f3f3f3f))
// IO
#define pc(s) putchar(s)
#define say(s) cout << s << endl
namespace io {
const int SIZ = 55;
int que[SIZ], op, qr;
char ch;
template <class I> il void gi(I &w) {
ch = getchar(), op = 1, w = 0;
while (!isdigit(ch)) {
if (ch == '-')
op = -1;
ch = getchar();
}
while (isdigit(ch)) {
w = w * 10 + ch - '0';
ch = getchar();
}
w *= op;
}
template <typename T, typename... Args> il void gi(T &t, Args &...args) {
gi(t);
gi(args...);
}
template <class I> il void print(I w) {
qr = 0;
if (!w)
putchar('0');
if (w < 0)
putchar('-'), w = -w;
while (w)
que[++qr] = w % 10 + '0', w /= 10;
while (qr)
putchar(que[qr--]);
}
} // namespace io
using io::gi;
using io::print;
const int N = 2e7 + 5;
const int base = 131;
const ll mod = 192608179;
string a, b;
int tag[N];
ll ha[N], hb[N], fac[N];
ll gethash(int x) {
if (x + b.size() > a.size())
return -1;
return (ha[x + b.size() - 1] - ha[x - 1] * fac[b.size()] % mod + mod) % mod;
}
int main() {
fio();
cin >> a >> b;
while (a.size() < b.size() * 2)
a = a + a;
a = a + a, a = a + a, a = a + a;
fac[0] = 1, ha[0] = a[0] - 'a', hb[0] = b[0] - 'a';
for (int i = 1; i < a.size(); ++i)
fac[i] = fac[i - 1] * base % mod;
for (int i = 1; i < a.size(); ++i)
ha[i] = (ha[i - 1] * base % mod + a[i] - 'a') % mod;
for (int i = 1; i < b.size(); ++i)
hb[i] = (hb[i - 1] * base % mod + b[i] - 'a') % mod;
for (int i = 0; i < a.size(); ++i)
tag[i] = hb[b.size() - 1] == gethash(i);
int ans = 0;
for (int i = a.size() - b.size(); i >= 0; --i)
if (tag[i])
tag[i] += tag[i + b.size()], ans = max(ans, tag[i]);
if (ans + 1 >= a.size() / b.size())
print(-1);
else
print(ans);
end();
}
| // Head File
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <set>
#include <vector>
using namespace std;
#define il inline
// Variable
#define ll long long
#define ull unsigned long long
#define db double
#define lb long double
#define ui unsigned int
// Debug
#define B cerr << "Break Point" << endl;
#define P(x) cerr << #x << ' ' << "=" << ' ' << (x) << endl;
#define p(x) cerr << #x << ' ' << "=" << ' ' << (x) << ' ';
#define ml(x) \
cerr << "Size of array is " << x * 4 / 1024 / 1024 << " MB" << endl;
// Vector
#define vc vector
#define puf push_front
#define pof pop_front
#define pub push_back
#define pob pop_back
#define vbe(x) x.begin(), x.end()
// Memset
#define ms(x) memset(x, 0, sizeof(x))
#define MS(x) memset(x, 0x3f3f3f3f, sizeof(x))
// Pair
#define fi first
#define se second
// File
#define fin(x) freopen(x, "r", stdin)
#define fou(x) freopen(x, "w", stdout)
void fio() {
#ifndef ONLINE_JUDGE
freopen("sample.in", "r", stdin);
freopen("sample.out", "w", stdout);
#endif
}
void pti() {
double timeuse = clock() * 1000.0 / CLOCKS_PER_SEC;
cerr << "Timeuse " << timeuse << "ms" << endl;
}
void end() {
#ifndef ONLINE_JUDGE
pti();
#endif
exit(0);
}
// Inf
#define INF 0x3f3f3f3f
#define LINF ((long long)(0x3f3f3f3f3f3f3f3f))
// IO
#define pc(s) putchar(s)
#define say(s) cout << s << endl
namespace io {
const int SIZ = 55;
int que[SIZ], op, qr;
char ch;
template <class I> il void gi(I &w) {
ch = getchar(), op = 1, w = 0;
while (!isdigit(ch)) {
if (ch == '-')
op = -1;
ch = getchar();
}
while (isdigit(ch)) {
w = w * 10 + ch - '0';
ch = getchar();
}
w *= op;
}
template <typename T, typename... Args> il void gi(T &t, Args &...args) {
gi(t);
gi(args...);
}
template <class I> il void print(I w) {
qr = 0;
if (!w)
putchar('0');
if (w < 0)
putchar('-'), w = -w;
while (w)
que[++qr] = w % 10 + '0', w /= 10;
while (qr)
putchar(que[qr--]);
}
} // namespace io
using io::gi;
using io::print;
const int N = 2e7 + 5;
const int base = 131;
const ll mod = 192608179;
string a, b;
int tag[N];
ll ha[N], hb[N], fac[N];
ll gethash(int x) {
if (x + b.size() > a.size())
return -1;
return (ha[x + b.size() - 1] - ha[x - 1] * fac[b.size()] % mod + mod) % mod;
}
int main() {
cin >> a >> b;
while (a.size() < b.size() * 2)
a = a + a;
a = a + a, a = a + a, a = a + a;
fac[0] = 1, ha[0] = a[0] - 'a', hb[0] = b[0] - 'a';
for (int i = 1; i < a.size(); ++i)
fac[i] = fac[i - 1] * base % mod;
for (int i = 1; i < a.size(); ++i)
ha[i] = (ha[i - 1] * base % mod + a[i] - 'a') % mod;
for (int i = 1; i < b.size(); ++i)
hb[i] = (hb[i - 1] * base % mod + b[i] - 'a') % mod;
for (int i = 0; i < a.size(); ++i)
tag[i] = hb[b.size() - 1] == gethash(i);
int ans = 0;
for (int i = a.size() - b.size(); i >= 0; --i)
if (tag[i])
tag[i] += tag[i + b.size()], ans = max(ans, tag[i]);
if (ans + 1 >= a.size() / b.size())
print(-1);
else
print(ans);
end();
}
| delete | 124 | 125 | 124 | 124 | -11 | |
p02962 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define RFOR(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++)
#define setp(n) fixed << setprecision(n)
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>
#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert
using namespace std;
struct SuffixArray {
vector<int> SA;
const string s;
SuffixArray(const string &str) : s(str) {
SA.resize(s.size());
iota(begin(SA), end(SA), 0);
sort(begin(SA), end(SA),
[&](int a, int b) { return s[a] == s[b] ? a > b : s[a] < s[b]; });
vector<int> classes(s.size()), c(s.begin(), s.end()), cnt(s.size());
for (int len = 1; len < s.size(); len <<= 1) {
for (int i = 0; i < s.size(); i++) {
if (i > 0 && c[SA[i - 1]] == c[SA[i]] && SA[i - 1] + len < s.size() &&
c[SA[i - 1] + len / 2] == c[SA[i] + len / 2]) {
classes[SA[i]] = classes[SA[i - 1]];
} else {
classes[SA[i]] = i;
}
}
iota(begin(cnt), end(cnt), 0);
copy(begin(SA), end(SA), begin(c));
for (int i = 0; i < s.size(); i++) {
int s1 = c[i] - len;
if (s1 >= 0)
SA[cnt[classes[s1]]++] = s1;
}
classes.swap(c);
}
}
int operator[](int k) const { return SA[k]; }
size_t size() const { return s.size(); }
bool lt_substr(const string &t, int si = 0, int ti = 0) {
int sn = (int)s.size(), tn = (int)t.size();
while (si < sn && ti < tn) {
if (s[si] < t[ti])
return true;
if (s[si] > t[ti])
return false;
++si, ++ti;
}
return si >= sn && ti < tn;
}
int lower_bound(const string &t) {
int low = -1, high = (int)SA.size();
while (high - low > 1) {
int mid = (low + high) / 2;
if (lt_substr(t, SA[mid]))
low = mid;
else
high = mid;
}
return high;
}
pair<int, int> lower_upper_bound(string &t) {
int idx = lower_bound(t);
int low = idx - 1, high = (int)SA.size();
t.back()++;
while (high - low > 1) {
int mid = (low + high) / 2;
if (lt_substr(t, SA[mid]))
low = mid;
else
high = mid;
}
t.back()--;
return {idx, high};
}
void output() {
for (int i = 0; i < size(); i++) {
cout << i << ": " << s.substr(SA[i]) << endl;
}
}
};
vector<vi> adj;
vector<bool> visited;
vi dist;
bool loop = false;
int dfs(int v) {
if (loop)
return -1;
visited[v] = true;
if (adj[v].empty()) {
return dist[v] = 0;
;
}
int u = adj[v][0];
if (visited[u] && dist[u] == -1) {
loop = true;
return -1;
}
return dist[v] = dfs(u) + 1;
}
//-------------------------------------------------
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
string s, t;
cin >> s >> t;
int n = s.size();
int m = t.size();
int l = 0;
string u;
rep(i, n + m - 1) { u.pb(s[i % n]); }
SuffixArray sa(u);
auto r = sa.lower_upper_bound(t);
adj.resize(n);
visited.resize(n);
dist.resize(n, -1);
FOR(i, r.fi, r.se - 1) {
int from = sa[i];
int to = (sa[i] + m) % n;
adj[from].pb(to);
}
int ans = 0;
rep(i, n) {
chmax(ans, dfs(i));
if (loop)
break;
}
if (loop) {
cout << "-1\n";
} else {
cout << ans << "\n";
}
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define RFOR(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++)
#define setp(n) fixed << setprecision(n)
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>
#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define ins insert
using namespace std;
struct SuffixArray {
vector<int> SA;
const string s;
SuffixArray(const string &str) : s(str) {
SA.resize(s.size());
iota(begin(SA), end(SA), 0);
sort(begin(SA), end(SA),
[&](int a, int b) { return s[a] == s[b] ? a > b : s[a] < s[b]; });
vector<int> classes(s.size()), c(s.begin(), s.end()), cnt(s.size());
for (int len = 1; len < s.size(); len <<= 1) {
for (int i = 0; i < s.size(); i++) {
if (i > 0 && c[SA[i - 1]] == c[SA[i]] && SA[i - 1] + len < s.size() &&
c[SA[i - 1] + len / 2] == c[SA[i] + len / 2]) {
classes[SA[i]] = classes[SA[i - 1]];
} else {
classes[SA[i]] = i;
}
}
iota(begin(cnt), end(cnt), 0);
copy(begin(SA), end(SA), begin(c));
for (int i = 0; i < s.size(); i++) {
int s1 = c[i] - len;
if (s1 >= 0)
SA[cnt[classes[s1]]++] = s1;
}
classes.swap(c);
}
}
int operator[](int k) const { return SA[k]; }
size_t size() const { return s.size(); }
bool lt_substr(const string &t, int si = 0, int ti = 0) {
int sn = (int)s.size(), tn = (int)t.size();
while (si < sn && ti < tn) {
if (s[si] < t[ti])
return true;
if (s[si] > t[ti])
return false;
++si, ++ti;
}
return si >= sn && ti < tn;
}
int lower_bound(const string &t) {
int low = -1, high = (int)SA.size();
while (high - low > 1) {
int mid = (low + high) / 2;
if (lt_substr(t, SA[mid]))
low = mid;
else
high = mid;
}
return high;
}
pair<int, int> lower_upper_bound(string &t) {
int idx = lower_bound(t);
int low = idx - 1, high = (int)SA.size();
t.back()++;
while (high - low > 1) {
int mid = (low + high) / 2;
if (lt_substr(t, SA[mid]))
low = mid;
else
high = mid;
}
t.back()--;
return {idx, high};
}
void output() {
for (int i = 0; i < size(); i++) {
cout << i << ": " << s.substr(SA[i]) << endl;
}
}
};
vector<vi> adj;
vector<bool> visited;
vi dist;
bool loop = false;
int dfs(int v) {
if (loop)
return -1;
visited[v] = true;
if (adj[v].empty()) {
return dist[v] = 0;
;
}
int u = adj[v][0];
if (visited[u] && dist[u] == -1) {
loop = true;
return -1;
}
return dist[v] = dfs(u) + 1;
}
//-------------------------------------------------
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
string s, t;
cin >> s >> t;
int n = s.size();
int m = t.size();
int l = 0;
string u;
rep(i, n + m - 1) { u.pb(s[i % n]); }
SuffixArray sa(u);
auto r = sa.lower_upper_bound(t);
adj.resize(n);
visited.resize(n);
dist.resize(n, -1);
FOR(i, r.fi, r.se - 1) {
int from = sa[i];
int to = (sa[i] + m) % n;
adj[from].pb(to);
}
int ans = 0;
rep(i, n) {
if (dist[i] != -1)
continue;
chmax(ans, dfs(i));
if (loop)
break;
}
if (loop) {
cout << "-1\n";
} else {
cout << ans << "\n";
}
return 0;
}
| insert | 165 | 165 | 165 | 167 | TLE | |
p02962 | C++ | Runtime Error | #include <bits/stdc++.h>
#define pi acos(-1.0)
using namespace std;
const int maxn = 1e5 + 10;
int f[maxn], deg[maxn], dist[maxn];
vector<int> adj[maxn];
void getfail(string p) {
int n = p.size();
for (int i = 1; i < n; ++i) {
int j = f[i];
while (j && p[j] != p[i])
j = f[j];
if (p[j] == p[i])
f[i + 1] = j + 1;
}
}
int main() {
#ifdef LOCAL
freopen("in", "r", stdin);
freopen("out", "w", stdout);
#endif
ios::sync_with_stdio(false);
string s, t, s1;
cin >> s >> t;
getfail(t);
s1 = s;
int m = t.size(), n = s.size();
while (s.size() < s1.size() + t.size())
s += s1;
int j = 0;
for (int i = 0; i < n + m; ++i) {
while (j && t[j] != s[i])
j = f[j];
if (t[j] == s[i])
++j;
if (j == m)
adj[(i - m + 1) % n].push_back((i + 1) % n), ++deg[(i + 1) % n];
}
queue<int> q;
int ans = 0;
for (int i = 0; i < n; ++i)
if (deg[i] == 0)
q.push(i);
while (!q.empty()) {
int u = q.front();
q.pop();
for (auto v : adj[u])
if (--deg[v] == 0)
q.push(v), dist[v] = max(dist[v], dist[u] + 1), ans = max(ans, dist[v]);
;
}
int ok = 1;
for (int i = 0; i < n; ++i)
if (deg[i]) {
ok = 0;
break;
}
if (!ok)
cout << -1;
else
cout << ans;
return 0;
} | #include <bits/stdc++.h>
#define pi acos(-1.0)
using namespace std;
const int maxn = 1e6 + 10;
int f[maxn], deg[maxn], dist[maxn];
vector<int> adj[maxn];
void getfail(string p) {
int n = p.size();
for (int i = 1; i < n; ++i) {
int j = f[i];
while (j && p[j] != p[i])
j = f[j];
if (p[j] == p[i])
f[i + 1] = j + 1;
}
}
int main() {
#ifdef LOCAL
freopen("in", "r", stdin);
freopen("out", "w", stdout);
#endif
ios::sync_with_stdio(false);
string s, t, s1;
cin >> s >> t;
getfail(t);
s1 = s;
int m = t.size(), n = s.size();
while (s.size() < s1.size() + t.size())
s += s1;
int j = 0;
for (int i = 0; i < n + m; ++i) {
while (j && t[j] != s[i])
j = f[j];
if (t[j] == s[i])
++j;
if (j == m)
adj[(i - m + 1) % n].push_back((i + 1) % n), ++deg[(i + 1) % n];
}
queue<int> q;
int ans = 0;
for (int i = 0; i < n; ++i)
if (deg[i] == 0)
q.push(i);
while (!q.empty()) {
int u = q.front();
q.pop();
for (auto v : adj[u])
if (--deg[v] == 0)
q.push(v), dist[v] = max(dist[v], dist[u] + 1), ans = max(ans, dist[v]);
;
}
int ok = 1;
for (int i = 0; i < n; ++i)
if (deg[i]) {
ok = 0;
break;
}
if (!ok)
cout << -1;
else
cout << ans;
return 0;
} | replace | 4 | 5 | 4 | 5 | 0 | |
p02962 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop \
char nyaa; \
cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
struct rolling_hash {
private:
ll b = 10007;
vector<vector<ll>> hash;
int MAX_L = 100000;
vector<ll> b_pow;
public:
void init() {
hash = {};
b_pow = {1};
rep(i, MAX_L) {
b_pow.push_back(b_pow.back() * b % mod);
// cout << b_pow.back() << endl;
}
}
void make_hash(string s) {
int l = s.length();
vector<ll> h;
h.push_back(0);
rep(i, l) {
h.push_back((h.back() * b % mod + s[i]) % mod);
// cout << i << " " << h.back() << endl;
}
hash.push_back(h);
}
bool same(int a1, P d1, int a2, P d2) {
int l1 = d1.first, r1 = d1.second, l2 = d2.first, r2 = d2.second;
if (r1 - l1 != r2 - l2)
return false;
ll h1 = (hash[a1][r1] - b_pow[r1 - l1] * hash[a1][l1] % mod) % mod,
h2 = (hash[a2][r2] - b_pow[r2 - l2] * hash[a2][l2] % mod) % mod;
if (h1 < 0)
h1 += mod;
if (h2 < 0)
h2 += mod;
// cout << h1 << " " << h2 << endl;
if (h1 == h2)
return true;
return false;
}
};
string s_, s = "", t;
vector<bool> S = {}, used = {};
vector<ll> d;
int ls, lt;
ll c(int m, int sta, bool first) {
// cout << m << " " << sta << " " << first << endl;
if (!S[m])
return 0;
if (!first && m == sta) {
cout << -1 << endl;
exit(0);
}
if (used[m])
return d[m];
used[m] = true;
ll k = c((m + lt) % ls, sta, false) + 1;
d[m] = k;
return k;
}
void solve() {
cin >> s_;
cin >> t;
while (s.length() < t.length()) {
s += s_;
}
// cout << s << " " << t << endl;
ls = s.length(), lt = t.length();
// cout << ls << " " << lt << endl;
while (s.length() - ls < lt) {
s += s_;
}
// cout << s << " " << t << endl;
rolling_hash h;
h.init();
h.make_hash(s);
h.make_hash(t);
rep(i, ls) {
// cout << s.substr(i,lt) << " " << t << endl;
S.push_back(h.same(0, P(i, i + lt), 1, P(0, lt)));
used.push_back(false);
d.push_back(0);
}
ll ans = 0;
rep(i, ls) {
if (!used[i])
c(i, i, true);
// cout << d[i] << " " << S[i] << endl;
ans = max(ans, d[i]);
}
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
solve();
} | #include <algorithm>
#include <bitset>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop \
char nyaa; \
cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
struct rolling_hash {
private:
ll b = 10007;
vector<vector<ll>> hash;
int MAX_L = 5000000;
vector<ll> b_pow;
public:
void init() {
hash = {};
b_pow = {1};
rep(i, MAX_L) {
b_pow.push_back(b_pow.back() * b % mod);
// cout << b_pow.back() << endl;
}
}
void make_hash(string s) {
int l = s.length();
vector<ll> h;
h.push_back(0);
rep(i, l) {
h.push_back((h.back() * b % mod + s[i]) % mod);
// cout << i << " " << h.back() << endl;
}
hash.push_back(h);
}
bool same(int a1, P d1, int a2, P d2) {
int l1 = d1.first, r1 = d1.second, l2 = d2.first, r2 = d2.second;
if (r1 - l1 != r2 - l2)
return false;
ll h1 = (hash[a1][r1] - b_pow[r1 - l1] * hash[a1][l1] % mod) % mod,
h2 = (hash[a2][r2] - b_pow[r2 - l2] * hash[a2][l2] % mod) % mod;
if (h1 < 0)
h1 += mod;
if (h2 < 0)
h2 += mod;
// cout << h1 << " " << h2 << endl;
if (h1 == h2)
return true;
return false;
}
};
string s_, s = "", t;
vector<bool> S = {}, used = {};
vector<ll> d;
int ls, lt;
ll c(int m, int sta, bool first) {
// cout << m << " " << sta << " " << first << endl;
if (!S[m])
return 0;
if (!first && m == sta) {
cout << -1 << endl;
exit(0);
}
if (used[m])
return d[m];
used[m] = true;
ll k = c((m + lt) % ls, sta, false) + 1;
d[m] = k;
return k;
}
void solve() {
cin >> s_;
cin >> t;
while (s.length() < t.length()) {
s += s_;
}
// cout << s << " " << t << endl;
ls = s.length(), lt = t.length();
// cout << ls << " " << lt << endl;
while (s.length() - ls < lt) {
s += s_;
}
// cout << s << " " << t << endl;
rolling_hash h;
h.init();
h.make_hash(s);
h.make_hash(t);
rep(i, ls) {
// cout << s.substr(i,lt) << " " << t << endl;
S.push_back(h.same(0, P(i, i + lt), 1, P(0, lt)));
used.push_back(false);
d.push_back(0);
}
ll ans = 0;
rep(i, ls) {
if (!used[i])
c(i, i, true);
// cout << d[i] << " " << S[i] << endl;
ans = max(ans, d[i]);
}
cout << ans << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
solve();
} | replace | 43 | 44 | 43 | 44 | 0 | |
p02962 | C++ | Runtime Error | #include <bits/stdc++.h>
// #include<cstdio>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define Would
#define you
#define please
const ll mask30 = (1ll << 30) - 1;
const ll mask31 = (1ll << 31) - 1;
const ll RHmod = (1ll << 61) - 1;
const ll len = 1000100;
ll POW[len + 1], RH[len + 1];
ll RHmul(ll A, ll B) {
ll A1 = A >> 31;
ll A2 = A & mask31;
ll B1 = B >> 31;
ll B2 = B & mask31;
ll ret = 2 * A1 * B1 + A2 * B2 + ((A1 * B2 + A2 * B1 & mask30) << 31) +
(A1 * B2 + A2 * B1 >> 30);
return ret % RHmod;
}
ll RHcalc(int i, int j) {
ll kari = (RH[j] - RH[i] + RHmod) % RHmod;
return RHmul(kari, POW[len - i]);
}
int E[500001] = {};
int N;
int memo[500001];
int mugen;
int sagasu(int A, int B) {
if (memo[A] >= 0)
return memo[A];
if (mugen)
return 0;
if (A == B) {
mugen = 1;
return 0;
}
if (B == -1)
B = A;
if (E[A] == -1)
return memo[A] = 0;
return memo[A] = sagasu(E[A], B) + 1;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
string S, T;
cin >> S >> T;
N = S.size();
while (S.size() < max(N * 2, (int)T.size() * 2))
S += S;
int N2 = S.size();
int M = T.size();
POW[0] = 1;
rep(i, len) POW[i + 1] = RHmul(POW[i], 30);
RH[0] = 0;
rep(i, N2) RH[i + 1] = (RH[i] + RHmul(POW[i], S[i] - 'a' + 1)) % RHmod;
ll RHT = 0;
rep(i, M) RHT = (RHT + RHmul(POW[i], T[i] - 'a' + 1)) % RHmod;
RHT = RHmul(RHT, POW[len]);
rep(i, N) {
if (RHT == RHcalc(i, i + M))
E[i] = (i + M) % N;
else
E[i] = -1;
}
rep(i, N) memo[i] = -1;
int kotae = 0;
rep(i, N) {
kotae = max(kotae, sagasu(i, -1));
if (mugen)
break;
}
if (mugen)
co("-1");
else
co(kotae);
Would you please return 0;
} | #include <bits/stdc++.h>
// #include<cstdio>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define Would
#define you
#define please
const ll mask30 = (1ll << 30) - 1;
const ll mask31 = (1ll << 31) - 1;
const ll RHmod = (1ll << 61) - 1;
const ll len = 1000100;
ll POW[len + 1], RH[len + 1];
ll RHmul(ll A, ll B) {
ll A1 = A >> 31;
ll A2 = A & mask31;
ll B1 = B >> 31;
ll B2 = B & mask31;
ll ret = 2 * A1 * B1 + A2 * B2 + ((A1 * B2 + A2 * B1 & mask30) << 31) +
(A1 * B2 + A2 * B1 >> 30);
return ret % RHmod;
}
ll RHcalc(int i, int j) {
ll kari = (RH[j] - RH[i] + RHmod) % RHmod;
return RHmul(kari, POW[len - i]);
}
int E[500001] = {};
int N;
int memo[500001];
int mugen;
int sagasu(int A, int B) {
if (memo[A] >= 0)
return memo[A];
if (mugen)
return 0;
if (A == B) {
mugen = 1;
return 0;
}
if (B == -1)
B = A;
if (E[A] == -1)
return memo[A] = 0;
return memo[A] = sagasu(E[A], B) + 1;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
string S, T;
cin >> S >> T;
N = S.size();
while (S.size() < max(N * 2, (int)T.size() * 2))
S += S;
int N2 = min(1000010, (int)S.size());
int M = T.size();
POW[0] = 1;
rep(i, len) POW[i + 1] = RHmul(POW[i], 30);
RH[0] = 0;
rep(i, N2) RH[i + 1] = (RH[i] + RHmul(POW[i], S[i] - 'a' + 1)) % RHmod;
ll RHT = 0;
rep(i, M) RHT = (RHT + RHmul(POW[i], T[i] - 'a' + 1)) % RHmod;
RHT = RHmul(RHT, POW[len]);
rep(i, N) {
if (RHT == RHcalc(i, i + M))
E[i] = (i + M) % N;
else
E[i] = -1;
}
rep(i, N) memo[i] = -1;
int kotae = 0;
rep(i, N) {
kotae = max(kotae, sagasu(i, -1));
if (mugen)
break;
}
if (mugen)
co("-1");
else
co(kotae);
Would you please return 0;
} | replace | 65 | 66 | 65 | 66 | 0 | |
p02962 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define IFOR(i, m, n) for (ll i = n - 1; i >= m; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define FOREACH(x, a) for (auto &(x) : (a))
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) ll(x.size())
vector<ll> Zalgo(const string &S) {
ll N = SZ(S);
vector<ll> res(N);
res[0] = N;
ll i = 1, j = 0;
while (i < N) {
while (i + j < N && S[j] == S[i + j])
++j;
res[i] = j;
if (j == 0) {
++i;
continue;
}
ll k = 1;
while (i + k < N && k + res[k] < j)
res[i + k] = res[k], ++k;
i += k, j -= k;
}
return res;
}
int main() {
string s, t;
cin >> s >> t;
ll m = SZ(s), n = SZ(t);
string u = s;
while (SZ(u) <= m + n - 1) {
u = u + s;
}
u = t + u;
auto lcp = Zalgo(u);
vector<vector<ll>> to(m);
vector<ll> deg(m, 0);
REP(i, m) {
if (lcp[i + n] >= n) {
ll j = (i + n) % m;
to[i].push_back(j);
deg[j]++;
}
}
stack<ll> st;
REP(i, m) if (deg[i] == 0) st.push(i);
// dp[i] = 頂点iを始点とするパス長の最大値
vector<ll> topo, dp(m, 0);
while (!st.empty()) {
ll cv = st.top();
st.pop();
topo.push_back(cv);
FOREACH(nv, to[cv]) {
deg[nv]--;
dp[nv] = max(dp[nv], dp[cv] + 1);
if (deg[nv] == 0)
st.push(nv);
}
}
// トポロジカルソート列に全頂点が格納されていない場合,閉路がある
if (SZ(topo) != m) {
cout << -1 << endl;
return 0;
}
cout << *max_element(ALL(dp)) << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define IFOR(i, m, n) for (ll i = n - 1; i >= m; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define FOREACH(x, a) for (auto &(x) : (a))
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) ll(x.size())
vector<ll> Zalgo(const string &S) {
ll N = SZ(S);
vector<ll> res(N);
res[0] = N;
ll i = 1, j = 0;
while (i < N) {
while (i + j < N && S[j] == S[i + j])
++j;
res[i] = j;
if (j == 0) {
++i;
continue;
}
ll k = 1;
while (i + k < N && k + res[k] < j)
res[i + k] = res[k], ++k;
i += k, j -= k;
}
return res;
}
int main() {
string s, t;
cin >> s >> t;
ll m = SZ(s), n = SZ(t);
string u = s;
while (SZ(u) <= m + n - 1) {
u = u + u;
}
u = t + u;
auto lcp = Zalgo(u);
vector<vector<ll>> to(m);
vector<ll> deg(m, 0);
REP(i, m) {
if (lcp[i + n] >= n) {
ll j = (i + n) % m;
to[i].push_back(j);
deg[j]++;
}
}
stack<ll> st;
REP(i, m) if (deg[i] == 0) st.push(i);
// dp[i] = 頂点iを始点とするパス長の最大値
vector<ll> topo, dp(m, 0);
while (!st.empty()) {
ll cv = st.top();
st.pop();
topo.push_back(cv);
FOREACH(nv, to[cv]) {
deg[nv]--;
dp[nv] = max(dp[nv], dp[cv] + 1);
if (deg[nv] == 0)
st.push(nv);
}
}
// トポロジカルソート列に全頂点が格納されていない場合,閉路がある
if (SZ(topo) != m) {
cout << -1 << endl;
return 0;
}
cout << *max_element(ALL(dp)) << endl;
return 0;
} | replace | 40 | 41 | 40 | 41 | TLE | |
p02962 | C++ | Runtime Error | // #include "bits/stdc++.h"
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
#define int ll
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<double, double> pdd;
typedef vector<vector<int>> mat;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
const int INF =
sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
class KMP {
public:
string pattern;
int plen;
vector<int> table;
KMP(const string &s)
: pattern(s), plen((int)pattern.size()), table(plen + 1) {
table[0] = -1;
int j = -1;
for (int i = 0; i < plen; i++) {
while (j >= 0 && pattern[i] != pattern[j])
j = table[j];
if (pattern[i + 1] == pattern[++j])
table[i + 1] = table[j];
else
table[i + 1] = j;
}
}
void search(const string &text, vector<int> &res) {
int head = 0, j = 0, tlen = (int)text.size();
while (head + j < tlen) {
if (pattern[j] == text[head + j]) {
if (++j != plen)
continue;
res.push_back(head);
}
head += j - table[j], j = max(table[j], 0LL);
}
}
};
bool ok[500010], checked[500010];
int score[500010];
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
string s, t;
cin >> s >> t;
string ts;
while (SZ(ts) < SZ(t) + SZ(s) - 1) {
ts += s;
}
vector<int> mps;
KMP kmp(t);
kmp.search(ts, mps);
for (auto &p : mps) {
if (SZ(s) <= p)
break;
ok[p] = true;
// cout << p << endl;
}
int ans = 0;
rep(i, 0, SZ(ts)) {
if (checked[i])
continue;
checked[i] = true;
int p = i;
int res = 0;
while (ok[p]) {
res++;
p = (p + SZ(t)) % SZ(s);
if (p == i) {
res = INF;
break;
}
if (score[p] != 0) {
res += score[p];
break;
}
checked[p] = true;
}
chmax(ans, res);
score[i] = res;
}
if (ans == INF) {
cout << -1 << endl;
} else {
cout << ans << endl;
}
return 0;
} | // #include "bits/stdc++.h"
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
#define int ll
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<double, double> pdd;
typedef vector<vector<int>> mat;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
const int INF =
sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
class KMP {
public:
string pattern;
int plen;
vector<int> table;
KMP(const string &s)
: pattern(s), plen((int)pattern.size()), table(plen + 1) {
table[0] = -1;
int j = -1;
for (int i = 0; i < plen; i++) {
while (j >= 0 && pattern[i] != pattern[j])
j = table[j];
if (pattern[i + 1] == pattern[++j])
table[i + 1] = table[j];
else
table[i + 1] = j;
}
}
void search(const string &text, vector<int> &res) {
int head = 0, j = 0, tlen = (int)text.size();
while (head + j < tlen) {
if (pattern[j] == text[head + j]) {
if (++j != plen)
continue;
res.push_back(head);
}
head += j - table[j], j = max(table[j], 0LL);
}
}
};
bool ok[500010], checked[500010];
int score[500010];
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
string s, t;
cin >> s >> t;
string ts;
while (SZ(ts) < SZ(t) + SZ(s) - 1) {
ts += s;
}
vector<int> mps;
KMP kmp(t);
kmp.search(ts, mps);
for (auto &p : mps) {
if (SZ(s) <= p)
break;
ok[p] = true;
// cout << p << endl;
}
int ans = 0;
rep(i, 0, SZ(s)) {
if (checked[i])
continue;
checked[i] = true;
int p = i;
int res = 0;
while (ok[p]) {
res++;
p = (p + SZ(t)) % SZ(s);
if (p == i) {
res = INF;
break;
}
if (score[p] != 0) {
res += score[p];
break;
}
checked[p] = true;
}
chmax(ans, res);
score[i] = res;
}
if (ans == INF) {
cout << -1 << endl;
} else {
cout << ans << endl;
}
return 0;
} | replace | 121 | 122 | 121 | 122 | 0 | |
p02962 | C++ | Runtime Error | /*
Though leaves are many , the root is one.
Through all the lying days of my youth
I swayed my leaves and flowers in the sun.
Now I may wither into the truth.
*/
// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
// #pragma GCC target("avx,tune=native")
#include <bits/stdc++.h>
// #include<ext/pb_ds/assoc_container.hpp>
using namespace std;
// using namespace __gnu_pbds;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
const int mod = 1000000007;
typedef long long ll;
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
inline string getstr(string &s, int l, int r) {
string ret = "";
for (int i = l; i <= r; i++)
ret.push_back(s[i]);
return ret;
}
int modpow(int x, int y, int md = mod) {
if (y == 0)
return 1;
int ret = modpow(x, y >> 1, md);
ret = (ll)ret * ret % md;
if (y & 1)
ret = (ll)ret * x % md;
return ret;
}
#define SZ 5000005
int len[SZ], fail[SZ];
bool can[SZ];
string s, t;
int ans[SZ];
void build(string &p) {
int len = p.size(), j = fail[0] = -1;
for (int i = 1; i <= len; i++) {
while (j >= 0 && p[j] != p[i - 1])
j = fail[j];
fail[i] = ++j; // if(p[fail[i]-1]==p[i-1])fail[i]=fail[fail[i]];
}
}
bool kmp(string &_t, string &_s) {
int n = _t.size(), m = _s.size();
for (int i = 0, k = 0; i < n; i++) {
while (k >= 0 && _s[k] != _t[i])
k = fail[k];
len[i] = ++k;
if (len[i] == m)
can[i] = 1;
else
can[i] = 0;
}
return (*max_element(len, len + n)) == m;
}
int main() {
cin >> s >> t;
const int size = 2 * ((int)s.size() + t.size());
while (s.size() < size) {
s = s + s;
}
s = s + s;
build(t);
kmp(s, t);
// cerr<<s<<" "<<t<<endl;
// for(int i=0;i<=s.size();i++){
// cerr<<i<<" "<<fail[i]<<endl;
// }
for (int i = (int)s.size() - 1; i >= 0; i--) {
if (can[i]) {
ans[i - (int)t.size() + 1] =
max(ans[i - (int)t.size() + 1], ans[i + 1] + 1);
}
}
int final_ans = *max_element(ans, ans + (int)s.size() + 1);
if (final_ans >= (int)s.size() / t.size() - 2) {
puts("-1");
return 0;
}
cout << final_ans << endl;
return 0;
} | /*
Though leaves are many , the root is one.
Through all the lying days of my youth
I swayed my leaves and flowers in the sun.
Now I may wither into the truth.
*/
// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
// #pragma GCC target("avx,tune=native")
#include <bits/stdc++.h>
// #include<ext/pb_ds/assoc_container.hpp>
using namespace std;
// using namespace __gnu_pbds;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
const int mod = 1000000007;
typedef long long ll;
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
inline string getstr(string &s, int l, int r) {
string ret = "";
for (int i = l; i <= r; i++)
ret.push_back(s[i]);
return ret;
}
int modpow(int x, int y, int md = mod) {
if (y == 0)
return 1;
int ret = modpow(x, y >> 1, md);
ret = (ll)ret * ret % md;
if (y & 1)
ret = (ll)ret * x % md;
return ret;
}
#define SZ 10000005
int len[SZ], fail[SZ];
bool can[SZ];
string s, t;
int ans[SZ];
void build(string &p) {
int len = p.size(), j = fail[0] = -1;
for (int i = 1; i <= len; i++) {
while (j >= 0 && p[j] != p[i - 1])
j = fail[j];
fail[i] = ++j; // if(p[fail[i]-1]==p[i-1])fail[i]=fail[fail[i]];
}
}
bool kmp(string &_t, string &_s) {
int n = _t.size(), m = _s.size();
for (int i = 0, k = 0; i < n; i++) {
while (k >= 0 && _s[k] != _t[i])
k = fail[k];
len[i] = ++k;
if (len[i] == m)
can[i] = 1;
else
can[i] = 0;
}
return (*max_element(len, len + n)) == m;
}
int main() {
cin >> s >> t;
const int size = 2 * ((int)s.size() + t.size());
while (s.size() < size) {
s = s + s;
}
s = s + s;
build(t);
kmp(s, t);
// cerr<<s<<" "<<t<<endl;
// for(int i=0;i<=s.size();i++){
// cerr<<i<<" "<<fail[i]<<endl;
// }
for (int i = (int)s.size() - 1; i >= 0; i--) {
if (can[i]) {
ans[i - (int)t.size() + 1] =
max(ans[i - (int)t.size() + 1], ans[i + 1] + 1);
}
}
int final_ans = *max_element(ans, ans + (int)s.size() + 1);
if (final_ans >= (int)s.size() / t.size() - 2) {
puts("-1");
return 0;
}
cout << final_ans << endl;
return 0;
}
| replace | 33 | 34 | 33 | 34 | 0 | |
p02962 | C++ | Runtime Error | /*input
*/
#include <bits/stdc++.h>
#define up(i, a, b) for (int(i) = (a); (i) <= (b); ++(i))
#define down(i, b, a) for (int(i) = (b); i >= (a); --i)
#define debug(x) cerr << (x) << '\n';
#define bits(x, i) ((x >> i) & 1)
#define mid ((l + r) / 2)
#define pr pair<int, int>
using namespace std;
const int N = 1500005;
int p[N];
int nxt[500005];
int ans[500005];
int ok[500005];
string s, t, tmp;
void KMP() {
int n = tmp.size();
int t_size = t.size();
int len = max(tmp.size(), t.size()) * 2;
while (s.size() < len)
s += tmp;
s = t + "#" + s;
len = s.size();
// cout << s << '\n';
for (int i = 1; i < len; ++i) {
int j = p[i - 1];
while (j && s[i] != s[j])
j = p[j - 1];
if (s[i] == s[j])
j++;
p[i] = j;
}
memset(nxt, -1, sizeof(nxt));
for (int i = len - n; i < len; ++i) {
if (p[i] == t_size)
nxt[(i - t_size + 1) % n] = (i + 1) % n;
}
// for(int i=0;i<n;++i) cout << nxt[i] << '\n';
int res = 0, cnt = 0;
bool cycle = 0;
for (int i = 0; i < n; ++i) {
if (!ok[i] && nxt[i] != -1) {
int j = i;
ok[j] = ++cnt;
ans[cnt] = 0;
while (nxt[j] != -1) {
j = nxt[j];
ans[cnt]++;
if (ok[j]) {
if (ok[j] == cnt || ans[ok[j]] == -1) {
ans[cnt] = -1;
cycle = 1;
} else
ans[cnt] += ans[ok[j]];
break;
}
ok[j] = cnt;
}
res = max(res, ans[cnt]);
}
}
// for(int i=0;i<n;++i) cout << ok[i] << ' ';
cout << (!cycle ? res : -1);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> tmp >> t;
KMP();
}
| /*input
*/
#include <bits/stdc++.h>
#define up(i, a, b) for (int(i) = (a); (i) <= (b); ++(i))
#define down(i, b, a) for (int(i) = (b); i >= (a); --i)
#define debug(x) cerr << (x) << '\n';
#define bits(x, i) ((x >> i) & 1)
#define mid ((l + r) / 2)
#define pr pair<int, int>
using namespace std;
const int N = 2000005;
int p[N];
int nxt[500005];
int ans[500005];
int ok[500005];
string s, t, tmp;
void KMP() {
int n = tmp.size();
int t_size = t.size();
int len = max(tmp.size(), t.size()) * 2;
while (s.size() < len)
s += tmp;
s = t + "#" + s;
len = s.size();
// cout << s << '\n';
for (int i = 1; i < len; ++i) {
int j = p[i - 1];
while (j && s[i] != s[j])
j = p[j - 1];
if (s[i] == s[j])
j++;
p[i] = j;
}
memset(nxt, -1, sizeof(nxt));
for (int i = len - n; i < len; ++i) {
if (p[i] == t_size)
nxt[(i - t_size + 1) % n] = (i + 1) % n;
}
// for(int i=0;i<n;++i) cout << nxt[i] << '\n';
int res = 0, cnt = 0;
bool cycle = 0;
for (int i = 0; i < n; ++i) {
if (!ok[i] && nxt[i] != -1) {
int j = i;
ok[j] = ++cnt;
ans[cnt] = 0;
while (nxt[j] != -1) {
j = nxt[j];
ans[cnt]++;
if (ok[j]) {
if (ok[j] == cnt || ans[ok[j]] == -1) {
ans[cnt] = -1;
cycle = 1;
} else
ans[cnt] += ans[ok[j]];
break;
}
ok[j] = cnt;
}
res = max(res, ans[cnt]);
}
}
// for(int i=0;i<n;++i) cout << ok[i] << ' ';
cout << (!cycle ? res : -1);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> tmp >> t;
KMP();
}
| replace | 11 | 12 | 11 | 12 | 0 | |
p02962 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define r(i, n) for (int i = 0; i < n; i++)
#define int long long
typedef pair<int, int> P;
typedef pair<int, P> P2;
#define F first
#define S second
typedef unsigned long long ull;
struct RollingHash {
vector<ull> hash, p;
ull MOD = 1e9 + 7;
ull B = 1777771;
RollingHash() {}
RollingHash(const string &s) {
int n = s.size();
hash.assign(n + 1, 0);
p.assign(n + 1, 1);
for (int i = 0; i < n; i++) {
hash[i + 1] = (hash[i] * B + s[i]) % MOD;
p[i + 1] = p[i] * B % MOD;
}
}
// S[l, r)
ull find(int l, int r) {
ull res = hash[r] + MOD - hash[l] * p[r - l] % MOD;
return res >= MOD ? res - MOD : res;
}
};
signed main() {
string s, t;
cin >> s >> t;
int B = t.size();
while ((int)s.size() <= 1000009)
s = s + s;
while ((int)t.size() <= 1000009)
t = t + t;
RollingHash R(s);
RollingHash T(t);
int l = 0, r = 1000009 / B + 2;
// cout<<t.substr(0,6)<<endl;
while ((r - l) > 1) {
int mid = (r + l) / 2;
ull A = T.find(0, mid * B);
int flag = 0;
r(i, s.size() - mid * B - 1) {
if (R.find(i, i + mid * B) == A) {
flag = 1;
break;
}
}
if (!flag)
r = mid;
else
l = mid;
// cout<<l<<' '<<B<<endl;
}
if (r == 1000009 / B + 2)
cout << -1 << endl;
else
cout << l << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define r(i, n) for (int i = 0; i < n; i++)
#define int long long
typedef pair<int, int> P;
typedef pair<int, P> P2;
#define F first
#define S second
typedef unsigned long long ull;
struct RollingHash {
vector<ull> hash, p;
ull MOD = 1e9 + 7;
ull B = 1777771;
RollingHash() {}
RollingHash(const string &s) {
int n = s.size();
hash.assign(n + 1, 0);
p.assign(n + 1, 1);
for (int i = 0; i < n; i++) {
hash[i + 1] = (hash[i] * B + s[i]) % MOD;
p[i + 1] = p[i] * B % MOD;
}
}
// S[l, r)
ull find(int l, int r) {
ull res = hash[r] + MOD - hash[l] * p[r - l] % MOD;
return res >= MOD ? res - MOD : res;
}
};
signed main() {
string s, t;
cin >> s >> t;
int B = t.size();
while ((int)s.size() <= 2000009)
s = s + s;
while ((int)t.size() <= 1000009)
t = t + t;
RollingHash R(s);
RollingHash T(t);
int l = 0, r = 1000009 / B + 2;
// cout<<t.substr(0,6)<<endl;
while ((r - l) > 1) {
int mid = (r + l) / 2;
ull A = T.find(0, mid * B);
int flag = 0;
r(i, s.size() - mid * B - 1) {
if (R.find(i, i + mid * B) == A) {
flag = 1;
break;
}
}
if (!flag)
r = mid;
else
l = mid;
// cout<<l<<' '<<B<<endl;
}
if (r == 1000009 / B + 2)
cout << -1 << endl;
else
cout << l << endl;
} | replace | 37 | 38 | 37 | 38 | 0 | |
p02962 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define EPS (1e-10)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
template <class T, uint mod> struct RollingHash_ {
vector<uint> hashed, power;
inline uint mul(uint a, uint b) { return a * (unsigned long long)b % mod; }
RollingHash_() {}
RollingHash_(T s, uint base = 10007) {
int sz = (int)s.size();
hashed.assign(sz + 1, 0);
power.assign(sz + 1, 0);
power[0] = 1;
for (int i = 0; i < sz; i++) {
power[i + 1] = mul(power[i], base);
hashed[i + 1] = mul(hashed[i], base) + s[i];
if (hashed[i + 1] >= mod)
hashed[i + 1] -= mod;
}
}
uint get(int l, int r) {
uint ret = hashed[r] + mod - mul(hashed[l], power[r - l]);
if (ret >= mod)
ret -= mod;
return ret;
}
};
typedef pair<uint, uint> Hash;
template <class T> struct RollingHash {
RollingHash_<T, 998244353> RH1;
RollingHash_<T, 1000000007> RH2;
RollingHash(T s) {
RH1 = RollingHash_<T, 998244353>(s);
RH2 = RollingHash_<T, 1000000007>(s);
}
Hash get(int l, int r) { return make_pair(RH1.get(l, r), RH2.get(l, r)); }
};
char S[600000], T[600000];
int nx[600000], prv[600000];
bool used[600000];
int main() {
scanf("%s%s", S, T);
string s = S, t = T;
while (s.size() < t.size())
s += S;
string ss = s + s;
RollingHash<string> rh1(ss), rh2(t);
memset(nx, -1, sizeof(nx));
memset(prv, -1, sizeof(prv));
rep(i, s.size()) {
Hash h1 = rh1.get(i, i + t.size()), h2 = rh2.get(0, t.size());
if (h1 == h2) {
nx[i] = (i + t.size()) % s.size();
prv[nx[i]] = i;
}
}
int Max = 0;
rep(i, s.size()) {
if (used[i])
continue;
if (prv[i] != -1)
continue;
int x = i;
int cnt = 0;
while (1) {
used[x] = true;
if (nx[x] == -1)
break;
x = nx[x];
cnt++;
}
Max = max(Max, cnt);
}
rep(i, s.size()) {
if (!used[i]) {
puts("-1");
return 0;
}
}
cout << Max << endl;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define EPS (1e-10)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
template <class T, uint mod> struct RollingHash_ {
vector<uint> hashed, power;
inline uint mul(uint a, uint b) { return a * (unsigned long long)b % mod; }
RollingHash_() {}
RollingHash_(T s, uint base = 10007) {
int sz = (int)s.size();
hashed.assign(sz + 1, 0);
power.assign(sz + 1, 0);
power[0] = 1;
for (int i = 0; i < sz; i++) {
power[i + 1] = mul(power[i], base);
hashed[i + 1] = mul(hashed[i], base) + s[i];
if (hashed[i + 1] >= mod)
hashed[i + 1] -= mod;
}
}
uint get(int l, int r) {
uint ret = hashed[r] + mod - mul(hashed[l], power[r - l]);
if (ret >= mod)
ret -= mod;
return ret;
}
};
typedef pair<uint, uint> Hash;
template <class T> struct RollingHash {
RollingHash_<T, 998244353> RH1;
RollingHash_<T, 1000000007> RH2;
RollingHash(T s) {
RH1 = RollingHash_<T, 998244353>(s);
RH2 = RollingHash_<T, 1000000007>(s);
}
Hash get(int l, int r) { return make_pair(RH1.get(l, r), RH2.get(l, r)); }
};
char S[600000], T[600000];
int nx[1200000], prv[1200000];
bool used[1200000];
int main() {
scanf("%s%s", S, T);
string s = S, t = T;
while (s.size() < t.size())
s += S;
string ss = s + s;
RollingHash<string> rh1(ss), rh2(t);
memset(nx, -1, sizeof(nx));
memset(prv, -1, sizeof(prv));
rep(i, s.size()) {
Hash h1 = rh1.get(i, i + t.size()), h2 = rh2.get(0, t.size());
if (h1 == h2) {
nx[i] = (i + t.size()) % s.size();
prv[nx[i]] = i;
}
}
int Max = 0;
rep(i, s.size()) {
if (used[i])
continue;
if (prv[i] != -1)
continue;
int x = i;
int cnt = 0;
while (1) {
used[x] = true;
if (nx[x] == -1)
break;
x = nx[x];
cnt++;
}
Max = max(Max, cnt);
}
rep(i, s.size()) {
if (!used[i]) {
puts("-1");
return 0;
}
}
cout << Max << endl;
} | replace | 51 | 53 | 51 | 53 | 0 | |
p02962 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
vector<int> Z(string s) {
vector<int> z(s.size());
z[0] = s.size();
int i = 1, j = 0;
while (i < s.size()) {
while (i + j < s.size() && s[j] == s[i + j])
j++;
if (j == 0) {
z[i] = 0;
i++;
continue;
}
z[i] = j;
int k = 1;
while (i + k < s.size() && k + z[k] < z[i]) {
z[i + k] = z[k];
k++;
}
i += k;
j -= k;
}
return z;
}
vector<int> E[2000000];
int deg[200000];
int d[200000];
int main() {
string s, t;
cin >> s >> t;
while (s.size() < t.size())
s += s;
auto v = Z(t + s + s);
for (int i = t.size(); i < t.size() + s.size(); i++) {
if (v[i] >= t.size()) {
int a = i - t.size(), b = (a + t.size()) % s.size();
E[b].push_back(a);
deg[a] = 1;
}
}
memset(d, -1, sizeof(d));
queue<int> que;
rep(i, s.size()) {
if (!deg[i]) {
d[i] = 0;
que.push(i);
}
}
while (!que.empty()) {
int p = que.front();
que.pop();
for (int u : E[p]) {
if ((--deg[u]) == 0) {
d[u] = d[p] + 1;
que.push(u);
}
}
}
if (*min_element(d, d + s.size()) == -1)
puts("-1");
else
cout << *max_element(d, d + s.size()) << endl;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
vector<int> Z(string s) {
vector<int> z(s.size());
z[0] = s.size();
int i = 1, j = 0;
while (i < s.size()) {
while (i + j < s.size() && s[j] == s[i + j])
j++;
if (j == 0) {
z[i] = 0;
i++;
continue;
}
z[i] = j;
int k = 1;
while (i + k < s.size() && k + z[k] < z[i]) {
z[i + k] = z[k];
k++;
}
i += k;
j -= k;
}
return z;
}
vector<int> E[2000000];
int deg[2000000];
int d[2000000];
int main() {
string s, t;
cin >> s >> t;
while (s.size() < t.size())
s += s;
auto v = Z(t + s + s);
for (int i = t.size(); i < t.size() + s.size(); i++) {
if (v[i] >= t.size()) {
int a = i - t.size(), b = (a + t.size()) % s.size();
E[b].push_back(a);
deg[a] = 1;
}
}
memset(d, -1, sizeof(d));
queue<int> que;
rep(i, s.size()) {
if (!deg[i]) {
d[i] = 0;
que.push(i);
}
}
while (!que.empty()) {
int p = que.front();
que.pop();
for (int u : E[p]) {
if ((--deg[u]) == 0) {
d[u] = d[p] + 1;
que.push(u);
}
}
}
if (*min_element(d, d + s.size()) == -1)
puts("-1");
else
cout << *max_element(d, d + s.size()) << endl;
} | replace | 29 | 31 | 29 | 31 | 0 | |
p02962 | C++ | Runtime Error | // Words are flowing out like endless rain into a paper cup
// They slither while they pass they slip away across the universe
// Pools of sorrow, waves of joy are drifting through my open mind
// Possessing and caressing me
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
namespace _buff {
const size_t BUFF = 1 << 19;
char ibuf[BUFF], *ib = ibuf, *ie = ibuf;
char getc() {
if (ib == ie) {
ib = ibuf;
ie = ibuf + fread(ibuf, 1, BUFF, stdin);
}
return ib == ie ? -1 : *ib++;
}
} // namespace _buff
LL read() {
using namespace _buff;
LL ret = 0;
bool pos = true;
char c = getc();
for (; (c < '0' || c > '9') && c != '-'; c = getc()) {
assert(~c);
}
if (c == '-') {
pos = false;
c = getc();
}
for (; c >= '0' && c <= '9'; c = getc()) {
ret = (ret << 3) + (ret << 1) + (c ^ 48);
}
return pos ? ret : -ret;
}
template <typename T> void chkmax(T &dp, const T &val) {
if (val > dp)
dp = val;
}
const size_t N = 3e5 + 5;
char s[N], ss[N], t[N];
int ls, lt, z[N], indeg[N], dis[N];
bool ok[N];
vector<int> g[N];
void z_func(char s[], int z[]) {
int len = strlen(s);
for (int i = 1, l = 0, r = 0; i < len; ++i) {
if (i < r) {
z[i] = min(z[i - l], r - i);
}
for (; i + z[i] < len && s[i + z[i]] == s[z[i]]; ++z[i]) {
}
if (r <= i + z[i]) {
l = i;
r = i + z[i];
}
}
}
int main() {
scanf("%s%s", s, t);
ls = strlen(s);
lt = strlen(t);
copy(t, t + lt, ss);
for (int i = 0, j = lt; j < N - 1; (++i) %= ls, ++j) {
ss[j] = s[i];
}
z_func(ss, z);
int cnt = 0;
for (int i = 0; i < ls; ++i) {
if (z[i + lt] >= lt) {
cnt += ok[i] = true;
}
}
if (!cnt) {
return puts("0"), 0;
}
for (int i = 0; i < ls; ++i) {
if (ok[i] && ok[(i + lt) % ls]) {
g[i].emplace_back((i + lt) % ls);
++indeg[(i + lt) % ls];
}
}
static int que[N];
int ql = 0, qr = 0;
for (int i = 0; i < ls; ++i) {
if (ok[i] && !indeg[i]) {
dis[i] = 1;
que[qr++] = i;
}
}
int ans = 0;
while (ql < qr) {
int x = que[ql++];
chkmax(ans, dis[x]);
for (int y : g[x]) {
chkmax(dis[y], dis[x] + 1);
if (!(--indeg[y])) {
que[qr++] = y;
}
}
}
if (qr < cnt)
ans = -1;
cout << ans << '\n';
return 0;
} | // Words are flowing out like endless rain into a paper cup
// They slither while they pass they slip away across the universe
// Pools of sorrow, waves of joy are drifting through my open mind
// Possessing and caressing me
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
namespace _buff {
const size_t BUFF = 1 << 19;
char ibuf[BUFF], *ib = ibuf, *ie = ibuf;
char getc() {
if (ib == ie) {
ib = ibuf;
ie = ibuf + fread(ibuf, 1, BUFF, stdin);
}
return ib == ie ? -1 : *ib++;
}
} // namespace _buff
LL read() {
using namespace _buff;
LL ret = 0;
bool pos = true;
char c = getc();
for (; (c < '0' || c > '9') && c != '-'; c = getc()) {
assert(~c);
}
if (c == '-') {
pos = false;
c = getc();
}
for (; c >= '0' && c <= '9'; c = getc()) {
ret = (ret << 3) + (ret << 1) + (c ^ 48);
}
return pos ? ret : -ret;
}
template <typename T> void chkmax(T &dp, const T &val) {
if (val > dp)
dp = val;
}
const size_t N = 1.5e6 + 5;
char s[N], ss[N], t[N];
int ls, lt, z[N], indeg[N], dis[N];
bool ok[N];
vector<int> g[N];
void z_func(char s[], int z[]) {
int len = strlen(s);
for (int i = 1, l = 0, r = 0; i < len; ++i) {
if (i < r) {
z[i] = min(z[i - l], r - i);
}
for (; i + z[i] < len && s[i + z[i]] == s[z[i]]; ++z[i]) {
}
if (r <= i + z[i]) {
l = i;
r = i + z[i];
}
}
}
int main() {
scanf("%s%s", s, t);
ls = strlen(s);
lt = strlen(t);
copy(t, t + lt, ss);
for (int i = 0, j = lt; j < N - 1; (++i) %= ls, ++j) {
ss[j] = s[i];
}
z_func(ss, z);
int cnt = 0;
for (int i = 0; i < ls; ++i) {
if (z[i + lt] >= lt) {
cnt += ok[i] = true;
}
}
if (!cnt) {
return puts("0"), 0;
}
for (int i = 0; i < ls; ++i) {
if (ok[i] && ok[(i + lt) % ls]) {
g[i].emplace_back((i + lt) % ls);
++indeg[(i + lt) % ls];
}
}
static int que[N];
int ql = 0, qr = 0;
for (int i = 0; i < ls; ++i) {
if (ok[i] && !indeg[i]) {
dis[i] = 1;
que[qr++] = i;
}
}
int ans = 0;
while (ql < qr) {
int x = que[ql++];
chkmax(ans, dis[x]);
for (int y : g[x]) {
chkmax(dis[y], dis[x] + 1);
if (!(--indeg[y])) {
que[qr++] = y;
}
}
}
if (qr < cnt)
ans = -1;
cout << ans << '\n';
return 0;
} | replace | 47 | 48 | 47 | 48 | 0 | |
p02962 | C++ | Time Limit Exceeded | #include <string>
#include <vector>
struct KMP {
const std::string &pattern;
std::vector<int> table;
explicit KMP(const std::string &p) : pattern(p), table(p.size() + 1) {
table[0] = -1;
table[1] = 0;
int i = 2;
int j = 0;
while (i < pattern.size()) {
if (pattern[i - 1] == pattern[j]) {
table[i] = j + 1;
i += 1;
j += 1;
} else if (j > 0) {
j = table[j];
} else {
table[i] = 0;
i += 1;
}
}
if (pattern.size() > 1) {
table[pattern.size()] = j + 1;
}
}
std::vector<int> search(const std::string &text) {
std::vector<int> matches;
int m = 0;
int i = 0;
while (m + i < text.size()) {
if (i < pattern.size() && pattern[i] == text[m + i]) {
i += 1;
} else {
if (i == pattern.size()) {
matches.push_back(m);
}
m = m + i - table[i];
if (i > 0) {
i = table[i];
}
}
}
return matches;
}
};
// verify: https://atcoder.jp/contests/abc135/tasks/abc135_f
#include <iostream>
#include <map>
using namespace std;
const int INF = 1e9;
map<int, int> nexts;
vector<bool> vis;
pair<int, int> trace(int pos, int init) {
if (pos == init) {
return make_pair(pos, INF);
} else if (nexts.count(pos) == 0) {
return make_pair(pos, 1);
} else {
auto recur = trace(nexts[pos], init);
return make_pair(recur.first, recur.second + 1);
}
}
int main() {
string s, t;
cin >> s >> t;
string u;
while (u.size() < s.size() + t.size()) {
u.insert(u.end(), s.begin(), s.end());
}
KMP kmp(t);
auto matches = kmp.search(u);
for (const auto &fr : matches) {
nexts[fr] = (fr + t.size()) % s.size();
}
vis = vector<bool>(s.size(), false);
int ans = 0;
for (int fr = 0; fr < s.size(); ++fr) {
if (nexts.count(fr) == 0) {
continue;
}
if (vis[fr]) {
continue;
}
ans = max(ans, trace(nexts[fr], fr).second);
}
cout << ((ans >= INF) ? -1 : ans) << endl;
return 0;
}
| #include <string>
#include <vector>
struct KMP {
const std::string &pattern;
std::vector<int> table;
explicit KMP(const std::string &p) : pattern(p), table(p.size() + 1) {
table[0] = -1;
table[1] = 0;
int i = 2;
int j = 0;
while (i < pattern.size()) {
if (pattern[i - 1] == pattern[j]) {
table[i] = j + 1;
i += 1;
j += 1;
} else if (j > 0) {
j = table[j];
} else {
table[i] = 0;
i += 1;
}
}
if (pattern.size() > 1) {
table[pattern.size()] = j + 1;
}
}
std::vector<int> search(const std::string &text) {
std::vector<int> matches;
int m = 0;
int i = 0;
while (m + i < text.size()) {
if (i < pattern.size() && pattern[i] == text[m + i]) {
i += 1;
} else {
if (i == pattern.size()) {
matches.push_back(m);
}
m = m + i - table[i];
if (i > 0) {
i = table[i];
}
}
}
return matches;
}
};
// verify: https://atcoder.jp/contests/abc135/tasks/abc135_f
#include <iostream>
#include <map>
using namespace std;
const int INF = 1e9;
map<int, int> nexts;
vector<bool> vis;
pair<int, int> trace(int pos, int init) {
vis[pos] = true;
if (pos == init) {
return make_pair(pos, INF);
} else if (nexts.count(pos) == 0) {
return make_pair(pos, 1);
} else {
auto recur = trace(nexts[pos], init);
return make_pair(recur.first, recur.second + 1);
}
}
int main() {
string s, t;
cin >> s >> t;
string u;
while (u.size() < s.size() + t.size()) {
u.insert(u.end(), s.begin(), s.end());
}
KMP kmp(t);
auto matches = kmp.search(u);
for (const auto &fr : matches) {
nexts[fr] = (fr + t.size()) % s.size();
}
vis = vector<bool>(s.size(), false);
int ans = 0;
for (int fr = 0; fr < s.size(); ++fr) {
if (nexts.count(fr) == 0) {
continue;
}
if (vis[fr]) {
continue;
}
ans = max(ans, trace(nexts[fr], fr).second);
}
cout << ((ans >= INF) ? -1 : ans) << endl;
return 0;
}
| insert | 63 | 63 | 63 | 64 | TLE | |
p02962 | C++ | Runtime Error | #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
template <typename T> vector<int> z_algorithm(const int len, const T &in) {
vector<int> res(len, 0);
int c = 0;
for (int i = 1; i < len; ++i) {
if (i + res[i - c] < c + res[c]) {
res[i] = res[i - c];
} else {
int j = max(0, c + res[c] - i);
while (i + j < len && in[j] == in[i + j])
j++;
res[i] = j;
c = i;
}
}
res[0] = len;
return res;
}
template <typename T> vector<int> z_algorithm(const T &in) {
return z_algorithm((int)in.size(), in);
}
template <typename T>
vector<int> z_algorithm(const T &in_first, const T &in_second) {
T combined = T();
combined.append(in_first);
combined.append(in_second);
int n = (int)in_first.size(), m = (int)in_second.size();
vector<int> v = z_algorithm(n + m, combined);
vector<int> res(m);
for (int i = 0; i < m; i++) {
res[i] = min(n, v[i + n]);
}
return res;
}
class UnionFind {
private:
int siz;
vector<int> a;
public:
UnionFind(int x) : siz(x), a(x, -1) {}
int root(int x) { return a[x] < 0 ? x : a[x] = root(a[x]); }
bool unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return false;
siz--;
if (a[x] > a[y])
swap(x, y);
a[x] += a[y];
a[y] = x;
return true;
}
bool same(int x, int y) { return root(x) == root(y); }
int size(int x) { return -a[root(x)]; }
int conneted_component() { return siz; }
};
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
string s, t;
cin >> s >> t;
int n = (int)s.size(), m = (int)t.size();
// s.append(s);
// s.append(s);
// s.append(s);
string s2;
while (2 * (int)t.size() >= (int)s2.size())
s2.append(s);
assert((int)t.size() * 2 <= (int)s.size());
vector<int> v = z_algorithm(t, s);
UnionFind uf(n);
for (int i = 0; i < n; i++) {
if (v[i] == m && v[i + m] == m) {
if (!uf.unite(i, (i + m) % n)) {
cout << -1 << endl;
return 0;
}
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
if (v[i] == m)
ans = max(ans, uf.size(i));
}
cout << ans << endl;
return 0;
}
| #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
template <typename T> vector<int> z_algorithm(const int len, const T &in) {
vector<int> res(len, 0);
int c = 0;
for (int i = 1; i < len; ++i) {
if (i + res[i - c] < c + res[c]) {
res[i] = res[i - c];
} else {
int j = max(0, c + res[c] - i);
while (i + j < len && in[j] == in[i + j])
j++;
res[i] = j;
c = i;
}
}
res[0] = len;
return res;
}
template <typename T> vector<int> z_algorithm(const T &in) {
return z_algorithm((int)in.size(), in);
}
template <typename T>
vector<int> z_algorithm(const T &in_first, const T &in_second) {
T combined = T();
combined.append(in_first);
combined.append(in_second);
int n = (int)in_first.size(), m = (int)in_second.size();
vector<int> v = z_algorithm(n + m, combined);
vector<int> res(m);
for (int i = 0; i < m; i++) {
res[i] = min(n, v[i + n]);
}
return res;
}
class UnionFind {
private:
int siz;
vector<int> a;
public:
UnionFind(int x) : siz(x), a(x, -1) {}
int root(int x) { return a[x] < 0 ? x : a[x] = root(a[x]); }
bool unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return false;
siz--;
if (a[x] > a[y])
swap(x, y);
a[x] += a[y];
a[y] = x;
return true;
}
bool same(int x, int y) { return root(x) == root(y); }
int size(int x) { return -a[root(x)]; }
int conneted_component() { return siz; }
};
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
string s, t;
cin >> s >> t;
int n = (int)s.size(), m = (int)t.size();
s += s;
s += s;
while ((int)t.size() * 2 > (int)s.size())
s += s;
vector<int> v = z_algorithm(t, s);
UnionFind uf(n);
for (int i = 0; i < n; i++) {
if (v[i] == m && v[i + m] == m) {
if (!uf.unite(i, (i + m) % n)) {
cout << -1 << endl;
return 0;
}
}
}
int ans = 0;
for (int i = 0; i < n; i++) {
if (v[i] == m)
ans = max(ans, uf.size(i));
}
cout << ans << endl;
return 0;
} | replace | 76 | 83 | 76 | 80 | 0 | |
p02962 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class Solution {
public:
int solve(string &s, string &t) {
string S = s;
while (S.length() < t.length()) {
S += s;
}
int n = S.length();
S += S;
int m = t.length();
vector<int> next(m + 1, -1);
for (int i = 0, j = -1; i < n; ++i) {
while (j != -1 && t[j] != t[i]) {
j = next[j];
}
next[i + 1] = ++j;
}
vector<int> pos;
for (int i = 0, j = 0; i < S.length(); ++i) {
if (S[i] == t[j]) {
if (++j == m) {
int beg = i - m + 1;
pos.push_back(beg % n);
j = next[j];
}
} else {
while (j != -1 && S[i] != t[j]) {
j = next[j];
}
if (j == -1) {
j = 0;
} else {
--i;
}
}
}
vector<vector<int>> graph(n);
vector<int> degrees(n, 0);
vector<bool> ok(n, false);
for (auto x : pos) {
ok[x] = true;
}
for (int i = 0; i < n; ++i) {
if (ok[i]) {
int j = (i + m) % n;
if (ok[j]) {
graph[i].push_back(j);
degrees[j] += 1;
}
}
}
vector<int> ord;
for (int i = 0; i < n; ++i) {
if (degrees[i] == 0) {
ord.push_back(i);
}
}
for (int i = 0; i < ord.size(); ++i) {
int u = ord[i];
for (auto v : graph[u]) {
if (--degrees[v] == 0) {
ord.push_back(v);
}
}
}
for (int i = 0; i < n; ++i) {
if (degrees[i] != 0) {
return -1;
}
}
reverse(ord.begin(), ord.end());
vector<int> dp(n, 0);
for (auto u : ord) {
if (!ok[u]) {
continue;
}
int ans = 0;
for (auto v : graph[u]) {
ans = max(ans, dp[v]);
}
ans += 1;
dp[u] = ans;
}
int res = *max_element(dp.begin(), dp.end());
return res;
}
};
int main(int argc, char **argv) {
ios::sync_with_stdio(false);
cin.tie(0);
string s;
string t;
cin >> s >> t;
Solution sol;
cout << sol.solve(s, t) << "\n";
return 0;
} | #include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class Solution {
public:
int solve(string &s, string &t) {
string S = s;
while (S.length() < t.length()) {
S += s;
}
int n = S.length();
S += S;
int m = t.length();
vector<int> next(m + 1, -1);
for (int i = 0, j = -1; i < m; ++i) {
while (j != -1 && t[j] != t[i]) {
j = next[j];
}
next[i + 1] = ++j;
}
vector<int> pos;
for (int i = 0, j = 0; i < S.length(); ++i) {
if (S[i] == t[j]) {
if (++j == m) {
int beg = i - m + 1;
pos.push_back(beg % n);
j = next[j];
}
} else {
while (j != -1 && S[i] != t[j]) {
j = next[j];
}
if (j == -1) {
j = 0;
} else {
--i;
}
}
}
vector<vector<int>> graph(n);
vector<int> degrees(n, 0);
vector<bool> ok(n, false);
for (auto x : pos) {
ok[x] = true;
}
for (int i = 0; i < n; ++i) {
if (ok[i]) {
int j = (i + m) % n;
if (ok[j]) {
graph[i].push_back(j);
degrees[j] += 1;
}
}
}
vector<int> ord;
for (int i = 0; i < n; ++i) {
if (degrees[i] == 0) {
ord.push_back(i);
}
}
for (int i = 0; i < ord.size(); ++i) {
int u = ord[i];
for (auto v : graph[u]) {
if (--degrees[v] == 0) {
ord.push_back(v);
}
}
}
for (int i = 0; i < n; ++i) {
if (degrees[i] != 0) {
return -1;
}
}
reverse(ord.begin(), ord.end());
vector<int> dp(n, 0);
for (auto u : ord) {
if (!ok[u]) {
continue;
}
int ans = 0;
for (auto v : graph[u]) {
ans = max(ans, dp[v]);
}
ans += 1;
dp[u] = ans;
}
int res = *max_element(dp.begin(), dp.end());
return res;
}
};
int main(int argc, char **argv) {
ios::sync_with_stdio(false);
cin.tie(0);
string s;
string t;
cin >> s >> t;
Solution sol;
cout << sol.solve(s, t) << "\n";
return 0;
} | replace | 39 | 40 | 39 | 40 | -6 | Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)
|
p02962 | C++ | Runtime Error | #include <bits/stdc++.h>
#define BIT(n) (1LL << (n))
#define BITF(n, i) (((n) >> i) & 1)
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define REPI(i, x) for (int i = 1; i <= x; i++)
#define FORI(i, m, n) for (int i = m; i <= n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define REPZ(i, x) for (int i = 0; i <= x; i++)
#define FORA(i, n) for (auto &&i : n)
using namespace std;
#define DUMPOUT cerr
// vector
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
for (T &x : vec)
is >> x;
return is;
}
// pair
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &pair_var) {
os << "(" << pair_var.first << ", " << pair_var.second << ")";
return os;
}
// vector
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
os << "{";
REP(i, (int)vec.size())
os << vec[i] << (i + 1 == (int)vec.size() ? "" : ", ");
os << "}";
return os;
}
// map
template <typename T, typename U>
ostream &operator<<(ostream &os, map<T, U> &map_var) {
os << "{";
FORA(itr, map_var) {
os << *itr;
itr++;
if (itr != map_var.end())
os << ", ";
itr--;
}
os << "}";
return os;
}
// set
template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) {
os << "{";
FORA(itr, set_var) {
os << *itr;
itr++;
if (itr != set_var.end())
os << ", ";
itr--;
}
os << "}";
return os;
}
void dump_func() { DUMPOUT << endl; }
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&...tail) {
DUMPOUT << head;
if (sizeof...(Tail) > 0)
DUMPOUT << ", ";
dump_func(std::move(tail)...);
}
#ifdef DEBUG_
#define DEB
#define DUMP(...) \
DUMPOUT << " " << string(#__VA_ARGS__) << ": " \
<< "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << endl \
<< " ", \
dump_func(__VA_ARGS__)
#define PRINTARR(x, y) \
cerr << #x << "=\n"; \
for (auto itr = x; itr != y; itr++) \
cerr << *itr << " "; \
cerr << endl;
#define PRINTARR2(x, i0, i1) \
cerr << #x << "=\n"; \
for (int ii0 = 0; ii0 < i0; ii0++) { \
for (int ii1 = 0; ii1 < i1; ii1++) \
cerr << x[ii0][ii1] << " "; \
cerr << endl; \
}
#else
#define DEB if (false)
#define DUMP(...)
#define PRINTARR(x, y)
#define PRINTARR2(x, i0, i1)
#endif
#define ALL(v) v.begin(), v.end()
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
#define epb emplace_back
#define int long long
#define pint pair<int, int>
#define ld long double
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T> using vec = std::vector<T>;
template <class T> void print(const T &x) { cout << x << "\n"; }
const int MOD = 1000000007, INF0 = 1061109567, INF = INF0 * INF0;
const double EPS = 1e-10, PI = acos(-1.0);
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
/*
下のスラッシュを2つ追加しただけ.上のスラッシュを2つにすれば外れる.
//*/
#define MAXN 200100
void Zalgorithm(string S, int *A) {
int N = S.size();
A[0] = N;
int i = 1, j = 0;
while (i < N) {
while (i + j < N && S[j] == S[i + j])
++j;
A[i] = j;
if (j == 0) {
++i;
continue;
}
int k = 1;
while (i + k < N && k + A[k] < j)
A[i + k] = A[k], ++k;
i += k;
j -= k;
}
}
int a[MAXN];
bool isMatch[MAXN];
signed main() {
cin.tie(0), ios::sync_with_stdio(false);
cout << fixed << setprecision(12);
string S, T;
cin >> S >> T;
int Ssize = S.size();
int Tsize = T.size();
string Snew = "";
REP(i, (Ssize + Tsize) / Ssize + 1) { Snew += S; }
string TS = T + Snew;
Zalgorithm(TS, a);
REP(i, Ssize) { isMatch[i] = a[Tsize + i] >= Tsize; }
DUMP(S, Snew, T);
PRINTARR(isMatch, isMatch + Ssize);
int gcdV = __gcd(Ssize, Tsize);
int rep = Ssize / gcdV;
int ans = 0;
REP(i, gcdV) {
int index = i;
int tmpans = 0;
int cnt = 0;
int initcnt = 0;
bool init = true;
REP(j, rep) {
if (isMatch[index]) {
cnt++;
} else {
if (init) {
initcnt = cnt;
init = false;
}
cnt = 0;
}
chmax(tmpans, cnt);
index += Tsize;
index %= Ssize;
}
chmax(tmpans, cnt + initcnt);
chmax(ans, tmpans);
}
DUMP(gcdV, rep);
if (ans == rep) {
print(-1);
} else {
print(ans);
}
} | #include <bits/stdc++.h>
#define BIT(n) (1LL << (n))
#define BITF(n, i) (((n) >> i) & 1)
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define REPI(i, x) for (int i = 1; i <= x; i++)
#define FORI(i, m, n) for (int i = m; i <= n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define REPZ(i, x) for (int i = 0; i <= x; i++)
#define FORA(i, n) for (auto &&i : n)
using namespace std;
#define DUMPOUT cerr
// vector
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
for (T &x : vec)
is >> x;
return is;
}
// pair
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &pair_var) {
os << "(" << pair_var.first << ", " << pair_var.second << ")";
return os;
}
// vector
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
os << "{";
REP(i, (int)vec.size())
os << vec[i] << (i + 1 == (int)vec.size() ? "" : ", ");
os << "}";
return os;
}
// map
template <typename T, typename U>
ostream &operator<<(ostream &os, map<T, U> &map_var) {
os << "{";
FORA(itr, map_var) {
os << *itr;
itr++;
if (itr != map_var.end())
os << ", ";
itr--;
}
os << "}";
return os;
}
// set
template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) {
os << "{";
FORA(itr, set_var) {
os << *itr;
itr++;
if (itr != set_var.end())
os << ", ";
itr--;
}
os << "}";
return os;
}
void dump_func() { DUMPOUT << endl; }
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&...tail) {
DUMPOUT << head;
if (sizeof...(Tail) > 0)
DUMPOUT << ", ";
dump_func(std::move(tail)...);
}
#ifdef DEBUG_
#define DEB
#define DUMP(...) \
DUMPOUT << " " << string(#__VA_ARGS__) << ": " \
<< "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << endl \
<< " ", \
dump_func(__VA_ARGS__)
#define PRINTARR(x, y) \
cerr << #x << "=\n"; \
for (auto itr = x; itr != y; itr++) \
cerr << *itr << " "; \
cerr << endl;
#define PRINTARR2(x, i0, i1) \
cerr << #x << "=\n"; \
for (int ii0 = 0; ii0 < i0; ii0++) { \
for (int ii1 = 0; ii1 < i1; ii1++) \
cerr << x[ii0][ii1] << " "; \
cerr << endl; \
}
#else
#define DEB if (false)
#define DUMP(...)
#define PRINTARR(x, y)
#define PRINTARR2(x, i0, i1)
#endif
#define ALL(v) v.begin(), v.end()
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
#define epb emplace_back
#define int long long
#define pint pair<int, int>
#define ld long double
using namespace std;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T> using vec = std::vector<T>;
template <class T> void print(const T &x) { cout << x << "\n"; }
const int MOD = 1000000007, INF0 = 1061109567, INF = INF0 * INF0;
const double EPS = 1e-10, PI = acos(-1.0);
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
/*
下のスラッシュを2つ追加しただけ.上のスラッシュを2つにすれば外れる.
//*/
#define MAXN 2002000
void Zalgorithm(string S, int *A) {
int N = S.size();
A[0] = N;
int i = 1, j = 0;
while (i < N) {
while (i + j < N && S[j] == S[i + j])
++j;
A[i] = j;
if (j == 0) {
++i;
continue;
}
int k = 1;
while (i + k < N && k + A[k] < j)
A[i + k] = A[k], ++k;
i += k;
j -= k;
}
}
int a[MAXN];
bool isMatch[MAXN];
signed main() {
cin.tie(0), ios::sync_with_stdio(false);
cout << fixed << setprecision(12);
string S, T;
cin >> S >> T;
int Ssize = S.size();
int Tsize = T.size();
string Snew = "";
REP(i, (Ssize + Tsize) / Ssize + 1) { Snew += S; }
string TS = T + Snew;
Zalgorithm(TS, a);
REP(i, Ssize) { isMatch[i] = a[Tsize + i] >= Tsize; }
DUMP(S, Snew, T);
PRINTARR(isMatch, isMatch + Ssize);
int gcdV = __gcd(Ssize, Tsize);
int rep = Ssize / gcdV;
int ans = 0;
REP(i, gcdV) {
int index = i;
int tmpans = 0;
int cnt = 0;
int initcnt = 0;
bool init = true;
REP(j, rep) {
if (isMatch[index]) {
cnt++;
} else {
if (init) {
initcnt = cnt;
init = false;
}
cnt = 0;
}
chmax(tmpans, cnt);
index += Tsize;
index %= Ssize;
}
chmax(tmpans, cnt + initcnt);
chmax(ans, tmpans);
}
DUMP(gcdV, rep);
if (ans == rep) {
print(-1);
} else {
print(ans);
}
} | replace | 124 | 125 | 124 | 125 | 0 | |
p02962 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define dbg(...) \
do { \
cerr << __LINE__ << ": "; \
dbgprint(#__VA_ARGS__, __VA_ARGS__); \
} while (0);
using namespace std;
namespace std {
template <class S, class T> struct hash<pair<S, T>> {
size_t operator()(const pair<S, T> &p) const {
return ((size_t)1e9 + 7) * hash<S>()(p.first) + hash<T>()(p.second);
}
};
template <class T> struct hash<vector<T>> {
size_t operator()(const vector<T> &v) const {
size_t h = 0;
for (auto i : v)
h = h * ((size_t)1e9 + 7) + hash<T>()(i) + 1;
return h;
}
};
} // namespace std
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << "[ ";
rep(i, v.size()) os << v[i] << (i == v.size() - 1 ? " ]" : ", ");
return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &v) {
os << "{ ";
for (const auto &i : v)
os << i << ", ";
return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const map<T, U> &v) {
os << "{";
for (const auto &i : v)
os << " " << i.first << ": " << i.second << ",";
return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
return os << "(" << p.first << ", " << p.second << ")";
}
void dbgprint(const string &fmt) { cerr << endl; }
template <class H, class... T>
void dbgprint(const string &fmt, const H &h, const T &...r) {
cerr << fmt.substr(0, fmt.find(",")) << "= " << h << " ";
dbgprint(fmt.substr(fmt.find(",") + 1), r...);
}
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;
int *buildFail(const string &p) {
int m = p.size();
int *fail = new int[m + 1];
int j = fail[0] = -1;
for (int i = 1; i <= m; ++i) {
while (j >= 0 && p[j] != p[i - 1])
j = fail[j];
fail[i] = ++j;
}
return fail;
}
vector<set<int>> e;
int num[200010];
int rec(int c) {
if (num[c] < 0) {
cout << -1 << endl;
exit(0);
}
if (num[c])
return num[c];
if (!e[c].size())
return num[c] = 0;
num[c] = -1;
return num[c] = rec(*e[c].begin()) + 1;
}
int main() {
cin.tie(0);
cin.sync_with_stdio(0);
string s, t;
cin >> s >> t;
int n = s.size(), m = t.size();
int *fail = buildFail(t);
e.resize(n);
for (int i = 0, k = 0; i < n + m; ++i) {
while (k >= 0 && t[k] != s[i % n])
k = fail[k];
if (++k >= m) {
e[(i - m % n + n + 1) % n].insert((i + 1) % n);
k = fail[k];
}
}
// dbg(e);
int ans = 0;
rep(i, n) if (!num[i]) ans = max(ans, rec(i));
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define pb push_back
#define dbg(...) \
do { \
cerr << __LINE__ << ": "; \
dbgprint(#__VA_ARGS__, __VA_ARGS__); \
} while (0);
using namespace std;
namespace std {
template <class S, class T> struct hash<pair<S, T>> {
size_t operator()(const pair<S, T> &p) const {
return ((size_t)1e9 + 7) * hash<S>()(p.first) + hash<T>()(p.second);
}
};
template <class T> struct hash<vector<T>> {
size_t operator()(const vector<T> &v) const {
size_t h = 0;
for (auto i : v)
h = h * ((size_t)1e9 + 7) + hash<T>()(i) + 1;
return h;
}
};
} // namespace std
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << "[ ";
rep(i, v.size()) os << v[i] << (i == v.size() - 1 ? " ]" : ", ");
return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &v) {
os << "{ ";
for (const auto &i : v)
os << i << ", ";
return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const map<T, U> &v) {
os << "{";
for (const auto &i : v)
os << " " << i.first << ": " << i.second << ",";
return os << "}";
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
return os << "(" << p.first << ", " << p.second << ")";
}
void dbgprint(const string &fmt) { cerr << endl; }
template <class H, class... T>
void dbgprint(const string &fmt, const H &h, const T &...r) {
cerr << fmt.substr(0, fmt.find(",")) << "= " << h << " ";
dbgprint(fmt.substr(fmt.find(",") + 1), r...);
}
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;
int *buildFail(const string &p) {
int m = p.size();
int *fail = new int[m + 1];
int j = fail[0] = -1;
for (int i = 1; i <= m; ++i) {
while (j >= 0 && p[j] != p[i - 1])
j = fail[j];
fail[i] = ++j;
}
return fail;
}
vector<set<int>> e;
int num[500010];
int rec(int c) {
if (num[c] < 0) {
cout << -1 << endl;
exit(0);
}
if (num[c])
return num[c];
if (!e[c].size())
return num[c] = 0;
num[c] = -1;
return num[c] = rec(*e[c].begin()) + 1;
}
int main() {
cin.tie(0);
cin.sync_with_stdio(0);
string s, t;
cin >> s >> t;
int n = s.size(), m = t.size();
int *fail = buildFail(t);
e.resize(n);
for (int i = 0, k = 0; i < n + m; ++i) {
while (k >= 0 && t[k] != s[i % n])
k = fail[k];
if (++k >= m) {
e[(i - m % n + n + 1) % n].insert((i + 1) % n);
k = fail[k];
}
}
// dbg(e);
int ans = 0;
rep(i, n) if (!num[i]) ans = max(ans, rec(i));
cout << ans << endl;
return 0;
} | replace | 73 | 74 | 73 | 74 | 0 | |
p02962 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define all(A) begin(A), end(A)
#define rall(A) rbegin(A), rend(A)
#define sz(A) int(A.size())
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int m;
int get(const string &s) {
int n = (int)s.size();
vector<int> z(n);
for (int i = 1, l = 0, r = 0; i < n; i++) {
if (i <= r)
z[i] = min(r - i + 1, z[i - l]);
while (i + z[i] < n and s[z[i]] == s[i + z[i]])
z[i]++;
if (i + z[i] - 1 > r)
l = i, r = i + z[i] - 1;
}
vector<bool> vis(n, false);
int ans = 0;
for (int i = m; i < n; i++) {
if (vis[i])
continue;
vis[i] = true;
if (z[i] >= m) {
int cnt = 1;
int j = i;
while (j + m < n and z[j + m] >= m) {
j += m;
vis[j] = true;
cnt++;
}
ans = max(ans, cnt);
}
}
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
string s, t;
cin >> s >> t;
m = sz(t);
string S = s;
while (sz(S) < sz(t))
S = S + s;
string X = S + S;
string Y = X + S;
string Z = Y + S;
// int ans1 = get(t + X);
double t1 = clock();
int ans2 = get(t + Y);
double t2 = clock();
if ((t2 - t1) / CLOCKS_PER_SEC > 1.2) {
cout << -1 << endl;
return (0);
}
int ans3 = get(t + Z);
if (ans2 != ans3) {
cout << -1 << endl;
return (0);
} else {
cout << ans2 << endl;
}
return (0);
}
| #include <bits/stdc++.h>
#define all(A) begin(A), end(A)
#define rall(A) rbegin(A), rend(A)
#define sz(A) int(A.size())
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int m;
int get(const string &s) {
int n = (int)s.size();
vector<int> z(n);
for (int i = 1, l = 0, r = 0; i < n; i++) {
if (i <= r)
z[i] = min(r - i + 1, z[i - l]);
while (i + z[i] < n and s[z[i]] == s[i + z[i]])
z[i]++;
if (i + z[i] - 1 > r)
l = i, r = i + z[i] - 1;
}
vector<bool> vis(n, false);
int ans = 0;
for (int i = m; i < n; i++) {
if (vis[i])
continue;
vis[i] = true;
if (z[i] >= m) {
int cnt = 1;
int j = i;
while (j + m < n and z[j + m] >= m) {
j += m;
vis[j] = true;
cnt++;
}
ans = max(ans, cnt);
}
}
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
string s, t;
cin >> s >> t;
m = sz(t);
string S = s;
while (sz(S) < sz(t))
S = S + S;
string X = S + S;
string Y = X + S;
string Z = Y + S;
// int ans1 = get(t + X);
double t1 = clock();
int ans2 = get(t + Y);
double t2 = clock();
if ((t2 - t1) / CLOCKS_PER_SEC > 1.2) {
cout << -1 << endl;
return (0);
}
int ans3 = get(t + Z);
if (ans2 != ans3) {
cout << -1 << endl;
return (0);
} else {
cout << ans2 << endl;
}
return (0);
}
| replace | 52 | 53 | 52 | 53 | TLE | |
p02962 | C++ | Time Limit Exceeded | #pragma GCC optimize("O3")
#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
using QWORD = uint64_t;
using SQWORD = int64_t;
using DWORD = uint32_t;
using SDWORD = int32_t;
using WORD = uint16_t;
using SWORD = int16_t;
using BYTE = uint8_t;
using SBYTE = int8_t;
using DOUBLE = double;
using FLOAT = float;
#define MIN_SDWORD (-2147483648)
#define MAX_SDWORD (2147483647)
#define MIN_SBYTE (-128)
#define MAX_SBYTE (127)
#define MIN_SQWORD (0x8000000000000000)
#define MAX_SQWORD (0x7FFFFFFFFFFFFFFF)
#define MAX_QWORD (0xFFFFFFFFFFFFFFFF)
#define MAX_DWORD (0xFFFFFFFF)
#define MAX_WORD (0xFFFF)
#define MAX_BYTE (0xFF)
#define MAX_DOUBLE (1.0e+308)
#define DOUBLE_EPS (1.0e-12)
#define MIN_DOUBLE_N (-1.0e+308)
#define ArrayLength(a) (sizeof(a) / sizeof(a[0]))
static inline DOUBLE MAX(DOUBLE a, DOUBLE b) { return a > b ? a : b; }
static inline QWORD MAX(QWORD a, QWORD b) { return a > b ? a : b; }
static inline DWORD MAX(DWORD a, DWORD b) { return a > b ? a : b; }
static inline SDWORD MAX(SDWORD a, SDWORD b) { return a > b ? a : b; }
static inline DOUBLE MIN(DOUBLE a, DOUBLE b) { return a < b ? a : b; }
static inline QWORD MIN(QWORD a, QWORD b) { return a < b ? a : b; }
static inline DWORD MIN(DWORD a, DWORD b) { return a < b ? a : b; }
static inline SDWORD MIN(SDWORD a, SDWORD b) { return a < b ? a : b; }
#define BYTE_BITS (8)
#define WORD_BITS (16)
#define DWORD_BITS (32)
#define QWORD_BITS (64)
static inline void inputStringSpSeparated(char *pcStr) {
char *pcCur = pcStr;
for (;;) {
char c = getchar();
if (('\n' == c) || (EOF == c) || (' ' == c)) {
break;
}
*pcCur = c;
pcCur++;
}
*pcCur = '\0';
}
static inline void inputString(char *pcStr) {
char *pcCur = pcStr;
for (;;) {
char c = getchar();
if (('\n' == c) || (EOF == c)) {
break;
}
*pcCur = c;
pcCur++;
}
*pcCur = '\0';
}
static inline SQWORD inputSQWORD(void) {
SQWORD sqNumber = 0;
SQWORD sqMultiplier = 1;
bool bRead = false;
for (;;) {
char c = getchar();
if (!bRead) {
if ('-' == c) {
sqMultiplier = -1;
}
}
if (('0' <= c) && (c <= '9')) {
sqNumber *= 10LL;
sqNumber += (SQWORD)(c - '0');
bRead = true;
} else {
if (bRead) {
return sqNumber * sqMultiplier;
}
}
}
}
static inline SDWORD inputSDWORD(void) {
SDWORD lNumber = 0;
SDWORD lMultiplier = 1;
bool bRead = false;
for (;;) {
char c = getchar();
if (!bRead) {
if ('-' == c) {
lMultiplier = -1;
}
}
if (('0' <= c) && (c <= '9')) {
lNumber *= 10;
lNumber += (c - '0');
bRead = true;
} else {
if (bRead) {
return lNumber * lMultiplier;
}
}
}
}
static inline DOUBLE inputFP(void) {
DOUBLE dInt = 0.0;
DOUBLE dFrac = 0.0;
DOUBLE dMultiplier = 1.0;
DWORD dwFpCnt = 0;
DOUBLE *pdCur = &dInt;
bool bRead = false;
for (;;) {
char c = getchar();
if (!bRead) {
if ('-' == c) {
dMultiplier = -1;
}
}
if ('.' == c) {
pdCur = &dFrac;
} else if (('0' <= c) && (c <= '9')) {
(*pdCur) *= 10;
(*pdCur) += (DOUBLE)(c - '0');
bRead = true;
if (pdCur == &dFrac) {
dwFpCnt++;
}
} else {
if (bRead) {
return dMultiplier *
(dInt + dFrac / (pow((DOUBLE)10.0, (DOUBLE)dwFpCnt)));
}
}
}
}
/**
* mod による操作ライブラリ
*/
#define ANS_MOD (1000000007LL)
static SQWORD addMod(SQWORD x, SQWORD y) { return (x + y) % ANS_MOD; }
static SQWORD subMod(SQWORD x, SQWORD y) { return (x - y + ANS_MOD) % ANS_MOD; }
static SQWORD mulMod(SQWORD x, SQWORD y) { return (x * y) % ANS_MOD; }
static SQWORD powMod(SQWORD x, SQWORD e) {
SQWORD v = 1;
for (; e; x = mulMod(x, x), e >>= 1) {
if (e & 1) {
v = mulMod(v, x);
}
}
return v;
}
static SQWORD divMod(SQWORD x, SQWORD y) {
return mulMod(x, powMod(y, ANS_MOD - 2));
}
static SQWORD combMod(SQWORD n, SQWORD k) {
SQWORD v = 1;
for (SQWORD i = 1; i <= k; i++) {
v = divMod(mulMod(v, n - i + 1), i);
}
return v;
}
/**
* KMP法
*/
class KMP {
public:
vector<SDWORD> makeTable(const string &s) {
SDWORD n = s.size();
vector<SDWORD> ret(n + 1);
ret[0] = -1;
SDWORD j = -1;
for (SDWORD i = 0; i < n; i++) {
while ((0 <= j) && (s[i] != s[j])) {
j = ret[j];
}
j++;
if (s[i + 1] == s[j]) {
ret[i + 1] = ret[j];
} else {
ret[i + 1] = j;
}
}
return ret;
}
/**
* str の中に word とマッチする場所のリストを返す
* ret のそれぞれの要素 el は, 「str[el] からの文字列が word
* と一致する」ことを示す
*/
vector<SDWORD> wordSearch(const string &str, const string &word) {
vector<SDWORD> table = makeTable(word), ret;
SDWORD m = 0;
SDWORD i = 0;
SDWORD n = str.size();
while (m + i < n) {
if (word[i] == str[m + i]) {
i++;
if (i == (SDWORD)(word.size())) {
ret.push_back(m);
m = m + i - table[i];
i = table[i];
}
} else {
m = m + i - table[i];
if (i > 0) {
i = table[i];
}
}
}
return ret;
}
};
/*----------------------------------------------*/
typedef struct {
vector<DWORD> vdwPar;
vector<DWORD> vdwRank;
vector<DWORD> vdwCnt;
DWORD dwSize;
void initUnionFind(DWORD dwSize) {
dwSize = dwSize;
vdwPar.resize(dwSize);
vdwRank.resize(dwSize);
vdwCnt.resize(dwSize);
for (DWORD dwIdx = 0; dwIdx < dwSize; dwIdx++) {
vdwPar[dwIdx] = dwIdx;
vdwRank[dwIdx] = 0;
vdwCnt[dwIdx] = 1;
}
}
DWORD ufGetCnt(DWORD sqIdx) { return vdwCnt[ufGetParent(sqIdx)]; }
DWORD ufGetParent(DWORD dwIdx) const { return vdwPar[dwIdx]; }
DWORD ufGetRank(DWORD dwIdx) const { return vdwRank[dwIdx]; }
void ufSetParent(DWORD dwIdx, DWORD dwParent) {
vdwPar[dwIdx] = dwParent;
if (ufGetRank(dwIdx) == ufGetRank(dwParent)) {
(vdwRank[dwParent])++;
}
}
DWORD ufGetRoot(DWORD dwIdx) const {
if (ufGetParent(dwIdx) == dwIdx) {
return dwIdx;
} else {
DWORD dwParent = ufGetParent(dwIdx);
DWORD dwRoot = ufGetRoot(dwParent);
return dwRoot;
}
}
bool ufUnite(DWORD dwX, DWORD dwY) {
DWORD dwRootX = ufGetRoot(dwX);
DWORD dwRootY = ufGetRoot(dwY);
if (dwRootX == dwRootY) {
return false;
}
if (ufGetRank(dwRootX) < ufGetRank(dwRootY)) {
ufSetParent(dwRootX, dwRootY);
(vdwCnt[dwRootY]) += (vdwCnt[dwRootX]);
} else {
ufSetParent(dwRootY, dwRootX);
(vdwCnt[dwRootX]) += (vdwCnt[dwRootY]);
}
return true;
}
bool ufIsSame(DWORD dwX, DWORD dwY) const {
return (ufGetRoot(dwX) == ufGetRoot(dwY));
}
} ST_UNION_FIND;
/*----------------------------------------------*/
int main(void) {
KMP stringSearch;
string str_s, str_t;
cin >> str_s;
cin >> str_t;
SQWORD sqSLen = str_s.size();
SQWORD sqTLen = str_t.size();
string str_s_plus = str_s + str_s + str_s; /* 最低3個は繰り返す */
while (str_s_plus.size() < str_t.size() * 2) {
str_s_plus += str_s;
}
vector<SDWORD> vecMatchPos = stringSearch.wordSearch(str_s_plus, str_t);
ST_UNION_FIND Uf;
Uf.initUnionFind(sqSLen);
bool bIsForever = false;
for (auto pos : vecMatchPos) {
if (pos < str_s.size()) {
bool bFound = std::binary_search(vecMatchPos.begin(), vecMatchPos.end(),
(pos + sqTLen) % sqSLen);
if (bFound) {
if (!Uf.ufUnite(pos, (pos + sqTLen) % sqSLen)) {
bIsForever = true;
break;
}
}
}
}
if (bIsForever) {
printf("-1\n");
} else {
SQWORD sqSizeMax = 0;
for (SDWORD lIdx = 0; lIdx < sqSLen; lIdx++) {
if (std::binary_search(vecMatchPos.begin(), vecMatchPos.end(), lIdx)) {
sqSizeMax = max(sqSizeMax, (SQWORD)Uf.ufGetCnt(lIdx));
}
}
printf("%lld\n", sqSizeMax);
}
return 0;
} | #pragma GCC optimize("O3")
#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
using QWORD = uint64_t;
using SQWORD = int64_t;
using DWORD = uint32_t;
using SDWORD = int32_t;
using WORD = uint16_t;
using SWORD = int16_t;
using BYTE = uint8_t;
using SBYTE = int8_t;
using DOUBLE = double;
using FLOAT = float;
#define MIN_SDWORD (-2147483648)
#define MAX_SDWORD (2147483647)
#define MIN_SBYTE (-128)
#define MAX_SBYTE (127)
#define MIN_SQWORD (0x8000000000000000)
#define MAX_SQWORD (0x7FFFFFFFFFFFFFFF)
#define MAX_QWORD (0xFFFFFFFFFFFFFFFF)
#define MAX_DWORD (0xFFFFFFFF)
#define MAX_WORD (0xFFFF)
#define MAX_BYTE (0xFF)
#define MAX_DOUBLE (1.0e+308)
#define DOUBLE_EPS (1.0e-12)
#define MIN_DOUBLE_N (-1.0e+308)
#define ArrayLength(a) (sizeof(a) / sizeof(a[0]))
static inline DOUBLE MAX(DOUBLE a, DOUBLE b) { return a > b ? a : b; }
static inline QWORD MAX(QWORD a, QWORD b) { return a > b ? a : b; }
static inline DWORD MAX(DWORD a, DWORD b) { return a > b ? a : b; }
static inline SDWORD MAX(SDWORD a, SDWORD b) { return a > b ? a : b; }
static inline DOUBLE MIN(DOUBLE a, DOUBLE b) { return a < b ? a : b; }
static inline QWORD MIN(QWORD a, QWORD b) { return a < b ? a : b; }
static inline DWORD MIN(DWORD a, DWORD b) { return a < b ? a : b; }
static inline SDWORD MIN(SDWORD a, SDWORD b) { return a < b ? a : b; }
#define BYTE_BITS (8)
#define WORD_BITS (16)
#define DWORD_BITS (32)
#define QWORD_BITS (64)
static inline void inputStringSpSeparated(char *pcStr) {
char *pcCur = pcStr;
for (;;) {
char c = getchar();
if (('\n' == c) || (EOF == c) || (' ' == c)) {
break;
}
*pcCur = c;
pcCur++;
}
*pcCur = '\0';
}
static inline void inputString(char *pcStr) {
char *pcCur = pcStr;
for (;;) {
char c = getchar();
if (('\n' == c) || (EOF == c)) {
break;
}
*pcCur = c;
pcCur++;
}
*pcCur = '\0';
}
static inline SQWORD inputSQWORD(void) {
SQWORD sqNumber = 0;
SQWORD sqMultiplier = 1;
bool bRead = false;
for (;;) {
char c = getchar();
if (!bRead) {
if ('-' == c) {
sqMultiplier = -1;
}
}
if (('0' <= c) && (c <= '9')) {
sqNumber *= 10LL;
sqNumber += (SQWORD)(c - '0');
bRead = true;
} else {
if (bRead) {
return sqNumber * sqMultiplier;
}
}
}
}
static inline SDWORD inputSDWORD(void) {
SDWORD lNumber = 0;
SDWORD lMultiplier = 1;
bool bRead = false;
for (;;) {
char c = getchar();
if (!bRead) {
if ('-' == c) {
lMultiplier = -1;
}
}
if (('0' <= c) && (c <= '9')) {
lNumber *= 10;
lNumber += (c - '0');
bRead = true;
} else {
if (bRead) {
return lNumber * lMultiplier;
}
}
}
}
static inline DOUBLE inputFP(void) {
DOUBLE dInt = 0.0;
DOUBLE dFrac = 0.0;
DOUBLE dMultiplier = 1.0;
DWORD dwFpCnt = 0;
DOUBLE *pdCur = &dInt;
bool bRead = false;
for (;;) {
char c = getchar();
if (!bRead) {
if ('-' == c) {
dMultiplier = -1;
}
}
if ('.' == c) {
pdCur = &dFrac;
} else if (('0' <= c) && (c <= '9')) {
(*pdCur) *= 10;
(*pdCur) += (DOUBLE)(c - '0');
bRead = true;
if (pdCur == &dFrac) {
dwFpCnt++;
}
} else {
if (bRead) {
return dMultiplier *
(dInt + dFrac / (pow((DOUBLE)10.0, (DOUBLE)dwFpCnt)));
}
}
}
}
/**
* mod による操作ライブラリ
*/
#define ANS_MOD (1000000007LL)
static SQWORD addMod(SQWORD x, SQWORD y) { return (x + y) % ANS_MOD; }
static SQWORD subMod(SQWORD x, SQWORD y) { return (x - y + ANS_MOD) % ANS_MOD; }
static SQWORD mulMod(SQWORD x, SQWORD y) { return (x * y) % ANS_MOD; }
static SQWORD powMod(SQWORD x, SQWORD e) {
SQWORD v = 1;
for (; e; x = mulMod(x, x), e >>= 1) {
if (e & 1) {
v = mulMod(v, x);
}
}
return v;
}
static SQWORD divMod(SQWORD x, SQWORD y) {
return mulMod(x, powMod(y, ANS_MOD - 2));
}
static SQWORD combMod(SQWORD n, SQWORD k) {
SQWORD v = 1;
for (SQWORD i = 1; i <= k; i++) {
v = divMod(mulMod(v, n - i + 1), i);
}
return v;
}
/**
* KMP法
*/
class KMP {
public:
vector<SDWORD> makeTable(const string &s) {
SDWORD n = s.size();
vector<SDWORD> ret(n + 1);
ret[0] = -1;
SDWORD j = -1;
for (SDWORD i = 0; i < n; i++) {
while ((0 <= j) && (s[i] != s[j])) {
j = ret[j];
}
j++;
if (s[i + 1] == s[j]) {
// ret[i+1] = ret[j];
ret[i + 1] = j;
} else {
ret[i + 1] = j;
}
}
return ret;
}
/**
* str の中に word とマッチする場所のリストを返す
* ret のそれぞれの要素 el は, 「str[el] からの文字列が word
* と一致する」ことを示す
*/
vector<SDWORD> wordSearch(const string &str, const string &word) {
vector<SDWORD> table = makeTable(word), ret;
SDWORD m = 0;
SDWORD i = 0;
SDWORD n = str.size();
while (m + i < n) {
if (word[i] == str[m + i]) {
i++;
if (i == (SDWORD)(word.size())) {
ret.push_back(m);
m = m + i - table[i];
i = table[i];
}
} else {
m = m + i - table[i];
if (i > 0) {
i = table[i];
}
}
}
return ret;
}
};
/*----------------------------------------------*/
typedef struct {
vector<DWORD> vdwPar;
vector<DWORD> vdwRank;
vector<DWORD> vdwCnt;
DWORD dwSize;
void initUnionFind(DWORD dwSize) {
dwSize = dwSize;
vdwPar.resize(dwSize);
vdwRank.resize(dwSize);
vdwCnt.resize(dwSize);
for (DWORD dwIdx = 0; dwIdx < dwSize; dwIdx++) {
vdwPar[dwIdx] = dwIdx;
vdwRank[dwIdx] = 0;
vdwCnt[dwIdx] = 1;
}
}
DWORD ufGetCnt(DWORD sqIdx) { return vdwCnt[ufGetParent(sqIdx)]; }
DWORD ufGetParent(DWORD dwIdx) const { return vdwPar[dwIdx]; }
DWORD ufGetRank(DWORD dwIdx) const { return vdwRank[dwIdx]; }
void ufSetParent(DWORD dwIdx, DWORD dwParent) {
vdwPar[dwIdx] = dwParent;
if (ufGetRank(dwIdx) == ufGetRank(dwParent)) {
(vdwRank[dwParent])++;
}
}
DWORD ufGetRoot(DWORD dwIdx) const {
if (ufGetParent(dwIdx) == dwIdx) {
return dwIdx;
} else {
DWORD dwParent = ufGetParent(dwIdx);
DWORD dwRoot = ufGetRoot(dwParent);
return dwRoot;
}
}
bool ufUnite(DWORD dwX, DWORD dwY) {
DWORD dwRootX = ufGetRoot(dwX);
DWORD dwRootY = ufGetRoot(dwY);
if (dwRootX == dwRootY) {
return false;
}
if (ufGetRank(dwRootX) < ufGetRank(dwRootY)) {
ufSetParent(dwRootX, dwRootY);
(vdwCnt[dwRootY]) += (vdwCnt[dwRootX]);
} else {
ufSetParent(dwRootY, dwRootX);
(vdwCnt[dwRootX]) += (vdwCnt[dwRootY]);
}
return true;
}
bool ufIsSame(DWORD dwX, DWORD dwY) const {
return (ufGetRoot(dwX) == ufGetRoot(dwY));
}
} ST_UNION_FIND;
/*----------------------------------------------*/
int main(void) {
KMP stringSearch;
string str_s, str_t;
cin >> str_s;
cin >> str_t;
SQWORD sqSLen = str_s.size();
SQWORD sqTLen = str_t.size();
string str_s_plus = str_s + str_s + str_s; /* 最低3個は繰り返す */
while (str_s_plus.size() < str_t.size() * 2) {
str_s_plus += str_s;
}
vector<SDWORD> vecMatchPos = stringSearch.wordSearch(str_s_plus, str_t);
ST_UNION_FIND Uf;
Uf.initUnionFind(sqSLen);
bool bIsForever = false;
for (auto pos : vecMatchPos) {
if (pos < str_s.size()) {
bool bFound = std::binary_search(vecMatchPos.begin(), vecMatchPos.end(),
(pos + sqTLen) % sqSLen);
if (bFound) {
if (!Uf.ufUnite(pos, (pos + sqTLen) % sqSLen)) {
bIsForever = true;
break;
}
}
}
}
if (bIsForever) {
printf("-1\n");
} else {
SQWORD sqSizeMax = 0;
for (SDWORD lIdx = 0; lIdx < sqSLen; lIdx++) {
if (std::binary_search(vecMatchPos.begin(), vecMatchPos.end(), lIdx)) {
sqSizeMax = max(sqSizeMax, (SQWORD)Uf.ufGetCnt(lIdx));
}
}
printf("%lld\n", sqSizeMax);
}
return 0;
} | replace | 215 | 216 | 215 | 217 | TLE | |
p02962 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long int;
const int MAX = (int)(1e5 + 5);
const ll INF = (ll)(1e10 + 5);
const int MAX_N = (int)(5e5 + 5);
/////////////////////////////////////////////////////////////
class StringMatching {
public:
static const int MAX_N = (int)(8e5 + 100);
string s;
string t;
string phi;
string sct;
vector<int> z;
StringMatching(string s, string t, char phi) {
this->s = s;
this->t = t;
this->phi = phi;
this->z = vector<int>(MAX_N, 0);
init();
}
void init() {
sct = t + phi + s;
make_z();
}
void make_z() {
int n = sct.size();
int l = 0, r = 0;
z[0] = sct.size();
for (int i = 1; i < n; ++i) {
if (i > r) {
l = r = i;
while (r < n && sct[r - l] == sct[r])
r += 1;
z[i] = r - l;
r -= 1;
} else {
int k = i - l; // focus at prefix-substring (begin with sct[k]) with
// which already dealed
// no prefix-substring (begin with sct[i]) longer than (r - i + 1) could
// be expected since, "sct[0..(r - l)] == sct[l..r]" entails "sct[k..(r
// - l)] == sct[i..r]", but z[k] is smaller than length of sct[i..r]
if (z[k] < r - i + 1) {
z[i] = z[k];
} else { // there could be longer prefix-substring (begin with sct[i])
l = i;
while (r < n && sct[r - l] == sct[r])
r += 1;
z[i] = r - l;
r -= 1;
}
}
}
}
bool match_from(int i) { return z[t.size() + 1 + i] == t.size(); }
};
/////////////////////////////////////////////////////////////
string s;
string t;
vector<int> graph[MAX_N];
vector<int> tsorted;
int flg[MAX_N]; // 0: not-visited, 1: done, -1, temporary
int path[MAX_N];
int ans;
bool visit(int i) {
if (flg[i] == -1) {
return false;
}
if (flg[i] == 0) {
flg[i] = -1;
for (auto &e : graph[i]) {
if (!visit(e)) {
return false;
}
}
flg[i] = 1;
tsorted.push_back(i);
}
return true;
}
bool topological_sort() {
for (int i = 0; i < s.size(); ++i) {
flg[i] = 0;
}
for (int i = 0; i < s.size(); ++i) {
if (flg[i] == 0) {
if (!visit(i)) {
return false;
}
}
}
reverse(tsorted.begin(), tsorted.end());
return true;
}
int main(void) {
// Here your code !
cin >> s;
cin >> t;
int target_size = s.size() + t.size();
string s_replicate = s;
while (s_replicate.size() < target_size) {
s_replicate += s;
}
StringMatching sm(s_replicate, t, '_');
for (int i = 0; i < s.size(); ++i) {
if (sm.match_from(i)) {
int target = (i + t.size()) % s.size();
graph[i].push_back(target);
}
}
if (!topological_sort()) {
printf("%d\n", -1);
return 0;
}
int length = 0;
for (auto &e : tsorted) {
for (auto &child : graph[e]) {
path[child] = max(path[child], path[e] + (int)(t.size()));
length = max(length, path[child]);
}
}
ans = length / t.size();
printf("%d\n", ans);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long int;
const int MAX = (int)(1e5 + 5);
const ll INF = (ll)(1e10 + 5);
const int MAX_N = (int)(5e5 + 5);
/////////////////////////////////////////////////////////////
class StringMatching {
public:
static const int MAX_N = (int)(8e6 + 100);
string s;
string t;
string phi;
string sct;
vector<int> z;
StringMatching(string s, string t, char phi) {
this->s = s;
this->t = t;
this->phi = phi;
this->z = vector<int>(MAX_N, 0);
init();
}
void init() {
sct = t + phi + s;
make_z();
}
void make_z() {
int n = sct.size();
int l = 0, r = 0;
z[0] = sct.size();
for (int i = 1; i < n; ++i) {
if (i > r) {
l = r = i;
while (r < n && sct[r - l] == sct[r])
r += 1;
z[i] = r - l;
r -= 1;
} else {
int k = i - l; // focus at prefix-substring (begin with sct[k]) with
// which already dealed
// no prefix-substring (begin with sct[i]) longer than (r - i + 1) could
// be expected since, "sct[0..(r - l)] == sct[l..r]" entails "sct[k..(r
// - l)] == sct[i..r]", but z[k] is smaller than length of sct[i..r]
if (z[k] < r - i + 1) {
z[i] = z[k];
} else { // there could be longer prefix-substring (begin with sct[i])
l = i;
while (r < n && sct[r - l] == sct[r])
r += 1;
z[i] = r - l;
r -= 1;
}
}
}
}
bool match_from(int i) { return z[t.size() + 1 + i] == t.size(); }
};
/////////////////////////////////////////////////////////////
string s;
string t;
vector<int> graph[MAX_N];
vector<int> tsorted;
int flg[MAX_N]; // 0: not-visited, 1: done, -1, temporary
int path[MAX_N];
int ans;
bool visit(int i) {
if (flg[i] == -1) {
return false;
}
if (flg[i] == 0) {
flg[i] = -1;
for (auto &e : graph[i]) {
if (!visit(e)) {
return false;
}
}
flg[i] = 1;
tsorted.push_back(i);
}
return true;
}
bool topological_sort() {
for (int i = 0; i < s.size(); ++i) {
flg[i] = 0;
}
for (int i = 0; i < s.size(); ++i) {
if (flg[i] == 0) {
if (!visit(i)) {
return false;
}
}
}
reverse(tsorted.begin(), tsorted.end());
return true;
}
int main(void) {
// Here your code !
cin >> s;
cin >> t;
int target_size = s.size() + t.size();
string s_replicate = s;
while (s_replicate.size() < target_size) {
s_replicate += s;
}
StringMatching sm(s_replicate, t, '_');
for (int i = 0; i < s.size(); ++i) {
if (sm.match_from(i)) {
int target = (i + t.size()) % s.size();
graph[i].push_back(target);
}
}
if (!topological_sort()) {
printf("%d\n", -1);
return 0;
}
int length = 0;
for (auto &e : tsorted) {
for (auto &child : graph[e]) {
path[child] = max(path[child], path[e] + (int)(t.size()));
length = max(length, path[child]);
}
}
ans = length / t.size();
printf("%d\n", ans);
return 0;
}
| replace | 13 | 14 | 13 | 14 | 0 | |
p02962 | C++ | Runtime Error | #include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define FOR(i, k, n) for (int(i) = (k); (i) < (n); ++(i))
#define rep(i, n) FOR(i, 0, n)
#define all(v) begin(v), end(v)
#define debug(x) // std::cerr<<#x<<": "<<x<<"\n"
#define debug2(x, y) // std::cerr<<#x<<": "<<x<<", "<<#y<<": "<<y<<"\n"
#define debug3( \
x, y, \
z) // std::cerr<<#x<<": "<<x<<", "<<#y<<": "<<y<<", "<<#z<<": "<<z<<"\n"
using ll = long long;
using vi = std::vector<int>;
using vvi = std::vector<vi>;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
template <typename T> using vvec = std::vector<std::vector<T>>;
template <typename T> auto make_v(size_t sz) { return std::vector<T>(sz); }
template <typename T, typename... Ts> auto make_v(size_t sz, Ts... ts) {
return std::vector<decltype(make_v<T>(ts...))>(sz, make_v<T>(ts...));
}
template <typename T> void fill_v(T &var, const T &x) { var = x; }
template <typename V, typename T> void fill_v(V &v, const T &x) {
for (auto &&w : v) {
fill_v(w, x);
}
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::vector<T> &v) {
int sz = v.size();
s << "\n";
rep(i, sz) {
s << v[i];
if (i < sz - 1) {
s << "\t";
}
}
s << "\n";
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s,
const std::vector<std::vector<T>> &v) {
for (auto &&w : v) {
s << w;
}
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::deque<T> &v) {
int sz = v.size();
s << "\n";
rep(i, sz) {
s << v[i];
if (i < sz - 1) {
s << "\t";
}
}
s << "\n";
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::deque<std::deque<T>> &v) {
for (auto &&w : v) {
s << w;
}
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::set<T> &v) {
s << "\n";
for (auto &&elm : v) {
s << elm << "\t";
}
s << "\n";
return s;
}
inline void scan(int &a) { scanf("%d", &a); }
inline void scan(ll &a) { scanf("%lld", &a); }
inline void scan(char &a) { scanf(" %c", &a); }
inline void scan(double &a) { scanf("%lf", &a); }
inline void scan(std::string &s) {
char BUF[3000000];
scanf(" %s", BUF);
s = std::string(BUF);
}
template <typename T> inline void scan(std::vector<T> &v) {
for (auto &&sv : v) {
scan(sv);
}
}
template <typename First, typename... Args>
inline void scan(First &f, Args &...args) {
scan(f);
scan(args...);
}
inline void print(int a) { printf("%d\n", a); }
inline void print(ll a) { printf("%lld\n", a); }
inline void print(double a) { printf("%.12f\n", a); }
inline void print(std::string s) { std::cout << s << "\n"; }
using namespace std;
class RollingHash {
using ll = long long;
private:
std::vector<ll> data;
ll base;
std::vector<ll> mod;
std::vector<std::vector<ll>> power_base;
std::vector<std::vector<ll>> hash;
void assign_base() {
std::random_device rnd;
std::mt19937 mt(rnd());
base = mt() % (mod[0] - 3) + 2;
}
void assign_hash() {
power_base.assign(data.size(), std::vector<ll>(mod.size(), 0));
for (size_t i = 0; i < mod.size(); ++i) {
power_base[0][i] = 1;
}
for (size_t i = 0; i < data.size() - 1; ++i) {
for (size_t j = 0; j < mod.size(); ++j) {
power_base[i + 1][j] = power_base[i][j] * base % mod[j];
}
}
hash.assign(data.size() + 1, std::vector<ll>(mod.size(), 0));
for (size_t i = 0; i < data.size(); ++i) {
for (size_t j = 0; j < mod.size(); ++j) {
hash[i + 1][j] = (hash[i][j] + data[i] * power_base[i][j]) % mod[j];
}
}
}
public:
RollingHash() {}
RollingHash(std::vector<ll> &v) : data(v) {
mod = {1000000007, 1000000009, 1000000021, 1000000087};
assign_base();
assign_hash();
}
RollingHash(std::vector<ll> &v, std::vector<ll> &m) : data(v), mod(m) {
std::sort(std::begin(mod), std::end(mod));
assign_base();
assign_hash();
}
std::vector<int> include(std::vector<ll> data2) {
std::vector<int> ret;
if (data.size() < data2.size()) {
return ret;
}
std::vector<ll> hash2(mod.size(), 0);
for (size_t i = 0; i < data2.size(); ++i) {
for (size_t j = 0; j < mod.size(); ++j) {
hash2[j] = (hash2[j] + data2[i] * power_base[i][j]) % mod[j];
}
}
for (size_t i = 0; i < data.size() - data2.size() + 1; ++i) {
bool flag = true;
for (size_t j = 0; j < mod.size(); ++j) {
if (hash[i + data2.size()][j] !=
(hash[i][j] + hash2[j] * power_base[i][j]) % mod[j]) {
flag = false;
break;
}
}
if (flag) {
ret.push_back(i);
}
}
return ret;
}
};
int main() {
string s, t;
scan(s);
scan(t);
int sl = s.length();
int tl = t.length();
string x;
int scnt = (sl + tl - 2 + sl - 1) / sl;
rep(i, scnt) { x += s; }
int xl = x.length();
vll vx(xl, 0);
rep(i, xl) { vx[i] = x[i] - '/'; }
vll vt(tl, 0);
rep(i, tl) { vt[i] = t[i] - '/'; }
RollingHash rh(vx);
auto match = rh.include(vt);
debug(x);
debug(t);
debug(match);
vi g(sl, -1);
vi dig_in(sl, 0);
for (int ind : match) {
if (ind >= sl) {
break;
}
g[ind] = (ind + tl) % sl;
dig_in[g[ind]] += 1;
}
int ans = 0;
vector<bool> arrived(sl, false);
rep(i, sl) {
if (dig_in[i] > 0) {
continue;
}
set<int> si;
int current_index = i;
si.insert(current_index);
arrived[current_index] = true;
while (true) {
int next_index = g[current_index];
if (next_index == -1) {
break;
}
current_index = next_index;
si.insert(current_index);
arrived[current_index] = true;
}
ans = max(ans, (int)si.size() - 1);
}
rep(i, sl) {
if (!arrived[i]) {
ans = -1;
break;
}
}
print(ans);
return 0;
}
| #include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define FOR(i, k, n) for (int(i) = (k); (i) < (n); ++(i))
#define rep(i, n) FOR(i, 0, n)
#define all(v) begin(v), end(v)
#define debug(x) // std::cerr<<#x<<": "<<x<<"\n"
#define debug2(x, y) // std::cerr<<#x<<": "<<x<<", "<<#y<<": "<<y<<"\n"
#define debug3( \
x, y, \
z) // std::cerr<<#x<<": "<<x<<", "<<#y<<": "<<y<<", "<<#z<<": "<<z<<"\n"
using ll = long long;
using vi = std::vector<int>;
using vvi = std::vector<vi>;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
template <typename T> using vvec = std::vector<std::vector<T>>;
template <typename T> auto make_v(size_t sz) { return std::vector<T>(sz); }
template <typename T, typename... Ts> auto make_v(size_t sz, Ts... ts) {
return std::vector<decltype(make_v<T>(ts...))>(sz, make_v<T>(ts...));
}
template <typename T> void fill_v(T &var, const T &x) { var = x; }
template <typename V, typename T> void fill_v(V &v, const T &x) {
for (auto &&w : v) {
fill_v(w, x);
}
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::vector<T> &v) {
int sz = v.size();
s << "\n";
rep(i, sz) {
s << v[i];
if (i < sz - 1) {
s << "\t";
}
}
s << "\n";
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s,
const std::vector<std::vector<T>> &v) {
for (auto &&w : v) {
s << w;
}
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::deque<T> &v) {
int sz = v.size();
s << "\n";
rep(i, sz) {
s << v[i];
if (i < sz - 1) {
s << "\t";
}
}
s << "\n";
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::deque<std::deque<T>> &v) {
for (auto &&w : v) {
s << w;
}
return s;
}
template <typename T>
std::ostream &operator<<(std::ostream &s, const std::set<T> &v) {
s << "\n";
for (auto &&elm : v) {
s << elm << "\t";
}
s << "\n";
return s;
}
inline void scan(int &a) { scanf("%d", &a); }
inline void scan(ll &a) { scanf("%lld", &a); }
inline void scan(char &a) { scanf(" %c", &a); }
inline void scan(double &a) { scanf("%lf", &a); }
inline void scan(std::string &s) {
char BUF[3000000];
scanf(" %s", BUF);
s = std::string(BUF);
}
template <typename T> inline void scan(std::vector<T> &v) {
for (auto &&sv : v) {
scan(sv);
}
}
template <typename First, typename... Args>
inline void scan(First &f, Args &...args) {
scan(f);
scan(args...);
}
inline void print(int a) { printf("%d\n", a); }
inline void print(ll a) { printf("%lld\n", a); }
inline void print(double a) { printf("%.12f\n", a); }
inline void print(std::string s) { std::cout << s << "\n"; }
using namespace std;
class RollingHash {
using ll = long long;
private:
std::vector<ll> data;
ll base;
std::vector<ll> mod;
std::vector<std::vector<ll>> power_base;
std::vector<std::vector<ll>> hash;
void assign_base() {
std::random_device rnd;
std::mt19937 mt(rnd());
base = mt() % (mod[0] - 3) + 2;
}
void assign_hash() {
power_base.assign(data.size(), std::vector<ll>(mod.size(), 0));
for (size_t i = 0; i < mod.size(); ++i) {
power_base[0][i] = 1;
}
for (size_t i = 0; i < data.size() - 1; ++i) {
for (size_t j = 0; j < mod.size(); ++j) {
power_base[i + 1][j] = power_base[i][j] * base % mod[j];
}
}
hash.assign(data.size() + 1, std::vector<ll>(mod.size(), 0));
for (size_t i = 0; i < data.size(); ++i) {
for (size_t j = 0; j < mod.size(); ++j) {
hash[i + 1][j] = (hash[i][j] + data[i] * power_base[i][j]) % mod[j];
}
}
}
public:
RollingHash() {}
RollingHash(std::vector<ll> &v) : data(v) {
mod = {1000000007, 1000000009, 1000000021, 1000000087};
assign_base();
assign_hash();
}
RollingHash(std::vector<ll> &v, std::vector<ll> &m) : data(v), mod(m) {
std::sort(std::begin(mod), std::end(mod));
assign_base();
assign_hash();
}
std::vector<int> include(std::vector<ll> data2) {
std::vector<int> ret;
if (data.size() < data2.size()) {
return ret;
}
std::vector<ll> hash2(mod.size(), 0);
for (size_t i = 0; i < data2.size(); ++i) {
for (size_t j = 0; j < mod.size(); ++j) {
hash2[j] = (hash2[j] + data2[i] * power_base[i][j]) % mod[j];
}
}
for (size_t i = 0; i < data.size() - data2.size() + 1; ++i) {
bool flag = true;
for (size_t j = 0; j < mod.size(); ++j) {
if (hash[i + data2.size()][j] !=
(hash[i][j] + hash2[j] * power_base[i][j]) % mod[j]) {
flag = false;
break;
}
}
if (flag) {
ret.push_back(i);
}
}
return ret;
}
};
int main() {
string s, t;
scan(s);
scan(t);
int sl = s.length();
int tl = t.length();
string x;
int scnt = (sl + tl - 1 + sl - 1) / sl;
rep(i, scnt) { x += s; }
int xl = x.length();
vll vx(xl, 0);
rep(i, xl) { vx[i] = x[i] - '/'; }
vll vt(tl, 0);
rep(i, tl) { vt[i] = t[i] - '/'; }
RollingHash rh(vx);
auto match = rh.include(vt);
debug(x);
debug(t);
debug(match);
vi g(sl, -1);
vi dig_in(sl, 0);
for (int ind : match) {
if (ind >= sl) {
break;
}
g[ind] = (ind + tl) % sl;
dig_in[g[ind]] += 1;
}
int ans = 0;
vector<bool> arrived(sl, false);
rep(i, sl) {
if (dig_in[i] > 0) {
continue;
}
set<int> si;
int current_index = i;
si.insert(current_index);
arrived[current_index] = true;
while (true) {
int next_index = g[current_index];
if (next_index == -1) {
break;
}
current_index = next_index;
si.insert(current_index);
arrived[current_index] = true;
}
ans = max(ans, (int)si.size() - 1);
}
rep(i, sl) {
if (!arrived[i]) {
ans = -1;
break;
}
}
print(ans);
return 0;
}
| replace | 208 | 209 | 208 | 209 | 0 | |
p02962 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define all(x) (x).begin(), (x).end()
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
typedef long long ll;
using namespace std;
vector<int> ok(200000, 0), memo(200000, -1), fl(200000, 0);
int inf = 1e9 + 7, res = 0;
int dfs(int node, int sl, int tl) {
// cout<<node<<" "<<fl[node]<<memo[node]<<endl;
if (fl[node] == 1 && memo[node] == -1)
return inf;
if (memo[node] > -1)
return memo[node];
fl[node] = 1;
int r = 0;
if (ok[node])
r = dfs((node + tl) % sl, sl, tl) + 1;
else
r = 0;
return memo[node] = r;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
string t, s;
cin >> s;
cin >> t;
int l = t.length(), sl = s.length();
vector<int> A(l + 1);
A[0] = -1;
int j = -1;
for (int i = 0; i < t.size(); i++) {
while (j >= 0 && t[i] != t[j])
j = A[j];
j++;
if (j > 0 && t[i + 1] == t[j])
A[i + 1] = A[j];
else
A[i + 1] = j;
}
rep(i, l + 1) A[i] = i - A[i];
int m = 0, i = 0;
while (m < sl) {
if (t[i] == s[(m + i) % sl]) {
i++;
if (i == l)
ok[m] = 1;
} else {
m += A[i];
if (i > 0)
i = i - A[i];
}
}
// rep(i,sl)cout<<ok[i]<<endl;
rep(i, sl) dfs(i, sl, l);
rep(i, sl) chmax(res, memo[i]);
// rep(i,sl)cout<<memo[i]<<endl
if (res >= inf)
res = -1;
cout << res << endl;
return 0;
}
| #include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define all(x) (x).begin(), (x).end()
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
typedef long long ll;
using namespace std;
vector<int> ok(600000, 0), memo(600000, -1), fl(600000, 0);
int inf = 1e9 + 7, res = 0;
int dfs(int node, int sl, int tl) {
// cout<<node<<" "<<fl[node]<<memo[node]<<endl;
if (fl[node] == 1 && memo[node] == -1)
return inf;
if (memo[node] > -1)
return memo[node];
fl[node] = 1;
int r = 0;
if (ok[node])
r = dfs((node + tl) % sl, sl, tl) + 1;
else
r = 0;
return memo[node] = r;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
string t, s;
cin >> s;
cin >> t;
int l = t.length(), sl = s.length();
vector<int> A(l + 1);
A[0] = -1;
int j = -1;
for (int i = 0; i < t.size(); i++) {
while (j >= 0 && t[i] != t[j])
j = A[j];
j++;
if (j > 0 && t[i + 1] == t[j])
A[i + 1] = A[j];
else
A[i + 1] = j;
}
rep(i, l + 1) A[i] = i - A[i];
int m = 0, i = 0;
while (m < sl) {
if (t[i] == s[(m + i) % sl]) {
i++;
if (i == l)
ok[m] = 1;
} else {
m += A[i];
if (i > 0)
i = i - A[i];
}
}
// rep(i,sl)cout<<ok[i]<<endl;
rep(i, sl) dfs(i, sl, l);
rep(i, sl) chmax(res, memo[i]);
// rep(i,sl)cout<<memo[i]<<endl
if (res >= inf)
res = -1;
cout << res << endl;
return 0;
}
| replace | 38 | 39 | 38 | 39 | 0 | |
p02962 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pair<int, int>>
#define mp make_pair
#define FOR(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR1(i, n) for (int i = 1; i <= (int)(n); i++)
#define FORD0(i, n) for (int i = (int)n; i >= 0; i--)
#define FORD1(i, n) for (int i = (int)n; i >= 1; i--)
#define debug(X) \
{ cout << #X << " = " << (X) << endl; }
#define endl '\n'
#define int long long int
using namespace std;
vector<int> lps;
void lpsComp(string pat) {
int len = 0;
lps.resize(pat.size());
lps[0] = 0;
int i = 1;
while (i < pat.size()) {
if (pat[i] == pat[len]) {
lps[i] = ++len;
i++;
} else // (pat[i] != pat[len])
{
if (len != 0) {
len = lps[len - 1];
} else // if (len == 0)
{
lps[i] = 0;
i++;
}
}
}
}
const int lim = (int)6e5;
bool match[lim] = {0};
int len[lim] = {0};
int vis[lim] = {0};
string s, t;
int best = 0;
bool cycle = false;
// 0 is unvisited, 1 is currently visited, 2 is retired
int dfs(int i) {
if (vis[i] == 2)
return len[i];
if (vis[i] == 1) {
cycle = true;
return len[i];
}
vis[i] = 1;
int nex = (i + t.length()) % s.length();
int ans = 1;
if (match[nex]) {
ans += dfs(nex);
}
vis[i] = 2;
len[i] = ans;
best = max(ans, best);
return ans;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("in", "r", stdin);
cin >> s >> t;
lpsComp(t);
// matching loop
int pre = 0; // matching at pre th position
int cnt = 0;
for (int i = 0; cnt < 1e9; cnt++, i++, i %= s.length()) {
// cout << pre <<endl;
// if(i == s.length())break;
if (t[pre] == s[i]) {
pre++;
} else if (pre != 0) {
pre = lps[pre - 1];
i--;
}
if (pre == t.length()) {
// i th position is the ending
int start = (i - (int)t.length() + 1);
start %= (int)s.length();
while (start < 0)
start += s.length();
if (match[start])
break;
match[start] = true;
// debug(start + t.length());
pre = lps[pre - 1];
}
}
FOR(i, s.length()) {
if (match[i] && !vis[i]) {
dfs(i);
}
}
if (cycle)
cout << -1;
else
cout << best;
}
| #include <bits/stdc++.h>
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pair<int, int>>
#define mp make_pair
#define FOR(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR1(i, n) for (int i = 1; i <= (int)(n); i++)
#define FORD0(i, n) for (int i = (int)n; i >= 0; i--)
#define FORD1(i, n) for (int i = (int)n; i >= 1; i--)
#define debug(X) \
{ cout << #X << " = " << (X) << endl; }
#define endl '\n'
#define int long long int
using namespace std;
vector<int> lps;
void lpsComp(string pat) {
int len = 0;
lps.resize(pat.size());
lps[0] = 0;
int i = 1;
while (i < pat.size()) {
if (pat[i] == pat[len]) {
lps[i] = ++len;
i++;
} else // (pat[i] != pat[len])
{
if (len != 0) {
len = lps[len - 1];
} else // if (len == 0)
{
lps[i] = 0;
i++;
}
}
}
}
const int lim = (int)6e5;
bool match[lim] = {0};
int len[lim] = {0};
int vis[lim] = {0};
string s, t;
int best = 0;
bool cycle = false;
// 0 is unvisited, 1 is currently visited, 2 is retired
int dfs(int i) {
if (vis[i] == 2)
return len[i];
if (vis[i] == 1) {
cycle = true;
return len[i];
}
vis[i] = 1;
int nex = (i + t.length()) % s.length();
int ans = 1;
if (match[nex]) {
ans += dfs(nex);
}
vis[i] = 2;
len[i] = ans;
best = max(ans, best);
return ans;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("in", "r", stdin);
cin >> s >> t;
lpsComp(t);
// matching loop
int pre = 0; // matching at pre th position
int cnt = 0;
for (int i = 0; cnt < (s.length() + t.length()) * 2;
cnt++, i++, i %= s.length()) {
// cout << pre <<endl;
// if(i == s.length())break;
if (t[pre] == s[i]) {
pre++;
} else if (pre != 0) {
pre = lps[pre - 1];
i--;
}
if (pre == t.length()) {
// i th position is the ending
int start = (i - (int)t.length() + 1);
start %= (int)s.length();
while (start < 0)
start += s.length();
if (match[start])
break;
match[start] = true;
// debug(start + t.length());
pre = lps[pre - 1];
}
}
FOR(i, s.length()) {
if (match[i] && !vis[i]) {
dfs(i);
}
}
if (cycle)
cout << -1;
else
cout << best;
}
| replace | 74 | 75 | 74 | 76 | TLE | |
p02962 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
struct RollingHash {
vector<long long> hash, power;
const int seed = 10007, mod = 1e9 + 7;
RollingHash(const string &s) {
int n = (int)s.size();
hash.assign(n + 1, 0);
power.assign(n + 1, 1);
for (int i = 0; i < n; ++i)
power[i + 1] = power[i] * seed % mod;
for (int i = 0; i < n; ++i) {
hash[i + 1] = (hash[i] * seed + s[i]) % mod;
power[i + 1] = power[i] * seed % mod;
}
}
// [l, r)
long long get_hash(int l, int r) {
long long ret = (hash[r] - hash[l] * power[r - l] % mod + mod) % mod;
return ret;
}
};
struct UnionFind {
vector<int> par, weight;
UnionFind(int n) : par(n, -1), weight(n) {}
bool unite(int x, int y, int w = 0) {
x = root(x);
y = root(y);
if (x == y)
return false;
if (par[x] > par[y])
swap(x, y), w = -w;
par[x] += par[y];
par[y] = x;
return true;
}
bool same(int x, int y) { return root(x) == root(y); }
int root(int x) {
if (par[x] < 0)
return x;
int r = root(par[x]);
return par[x] = r;
}
int sizeOf(int x) { return -par[root(x)]; }
};
int main() {
string s, t;
cin >> s >> t;
int n = s.size();
s = s + s + s;
while (t.size() * 2 > s.size())
s += s;
int N = s.size();
int m = t.size();
vector<bool> match(N);
RollingHash rh1(s), rh2(t);
unsigned long long thash = rh2.get_hash(0, m);
for (int i = 0; i < N - m; ++i) {
if (rh1.get_hash(i, i + m) == thash)
match[i] = true;
}
// loop check & count
UnionFind uf(n);
for (int i = 0; i < n; ++i) {
if (match[i] && match[(i + m) % n]) {
if (!uf.unite(i, (i + m) % n)) {
cout << -1 << endl;
return 0;
}
}
}
int ans = 0;
for (int i = 0; i < N; ++i)
if (match[i])
ans = max(ans, uf.sizeOf(i));
cout << ans << endl;
return 0;
}
| #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
struct RollingHash {
vector<long long> hash, power;
const int seed = 10007, mod = 1e9 + 7;
RollingHash(const string &s) {
int n = (int)s.size();
hash.assign(n + 1, 0);
power.assign(n + 1, 1);
for (int i = 0; i < n; ++i)
power[i + 1] = power[i] * seed % mod;
for (int i = 0; i < n; ++i) {
hash[i + 1] = (hash[i] * seed + s[i]) % mod;
power[i + 1] = power[i] * seed % mod;
}
}
// [l, r)
long long get_hash(int l, int r) {
long long ret = (hash[r] - hash[l] * power[r - l] % mod + mod) % mod;
return ret;
}
};
struct UnionFind {
vector<int> par, weight;
UnionFind(int n) : par(n, -1), weight(n) {}
bool unite(int x, int y, int w = 0) {
x = root(x);
y = root(y);
if (x == y)
return false;
if (par[x] > par[y])
swap(x, y), w = -w;
par[x] += par[y];
par[y] = x;
return true;
}
bool same(int x, int y) { return root(x) == root(y); }
int root(int x) {
if (par[x] < 0)
return x;
int r = root(par[x]);
return par[x] = r;
}
int sizeOf(int x) { return -par[root(x)]; }
};
int main() {
string s, t;
cin >> s >> t;
int n = s.size();
s = s + s + s;
while (t.size() * 2 > s.size())
s += s;
int N = s.size();
int m = t.size();
vector<bool> match(N);
RollingHash rh1(s), rh2(t);
unsigned long long thash = rh2.get_hash(0, m);
for (int i = 0; i < N - m; ++i) {
if (rh1.get_hash(i, i + m) == thash)
match[i] = true;
}
// loop check & count
UnionFind uf(N);
for (int i = 0; i < n; ++i) {
if (match[i] && match[(i + m) % n]) {
if (!uf.unite(i, (i + m) % n)) {
cout << -1 << endl;
return 0;
}
}
}
int ans = 0;
for (int i = 0; i < N; ++i)
if (match[i])
ans = max(ans, uf.sizeOf(i));
cout << ans << endl;
return 0;
} | replace | 74 | 75 | 74 | 75 | -6 | munmap_chunk(): invalid pointer
|
p02962 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
vector<int> Z(string s) {
vector<int> z(s.size(), 0);
int l = 0, r = 0;
for (int i = 1; i < s.size(); i++) {
if (i <= r)
z[i] = min(z[i - l], r - i + 1);
while (i + z[i] < s.size() && s[i + z[i]] == s[z[i]])
z[i]++;
if (i + z[i] - 1 > r)
l = i, r = i + z[i] - 1;
}
return z;
}
const int N = 5e5 + 5;
vector<int> adj[N];
int vis[N];
bool cycle(int node) {
if (vis[node])
return vis[node] == 1;
vis[node] = 1;
for (int c : adj[node])
if (cycle(c))
return 1;
vis[node] = 2;
return 0;
}
int memo[N];
int solve(int node) {
int &ret = memo[node];
if (~ret)
return ret;
ret = 0;
for (int c : adj[node])
ret = max(ret, solve(c) + 1);
return ret;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
string s, t;
cin >> s >> t;
while (s.size() < t.size())
s += s;
int n = s.size();
int m = t.size();
vector<int> z = Z(t + "$" + s + s);
int st = m + 1;
int en = st + n - 1;
for (int i = st; i <= en; i++)
if (z[i] >= m)
adj[i - st].push_back((i - st + m) % n);
for (int i = 0; i < s.size(); i++) {
if (cycle(i)) {
cout << -1;
return 0;
}
}
memset(memo, -1, sizeof memo);
int mx = 0;
for (int i = 0; i < s.size(); i++)
mx = max(mx, solve(i));
cout << mx;
} | #include <bits/stdc++.h>
using namespace std;
vector<int> Z(string s) {
vector<int> z(s.size(), 0);
int l = 0, r = 0;
for (int i = 1; i < s.size(); i++) {
if (i <= r)
z[i] = min(z[i - l], r - i + 1);
while (i + z[i] < s.size() && s[i + z[i]] == s[z[i]])
z[i]++;
if (i + z[i] - 1 > r)
l = i, r = i + z[i] - 1;
}
return z;
}
const int N = 1e6 + 5;
vector<int> adj[N];
int vis[N];
bool cycle(int node) {
if (vis[node])
return vis[node] == 1;
vis[node] = 1;
for (int c : adj[node])
if (cycle(c))
return 1;
vis[node] = 2;
return 0;
}
int memo[N];
int solve(int node) {
int &ret = memo[node];
if (~ret)
return ret;
ret = 0;
for (int c : adj[node])
ret = max(ret, solve(c) + 1);
return ret;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
string s, t;
cin >> s >> t;
while (s.size() < t.size())
s += s;
int n = s.size();
int m = t.size();
vector<int> z = Z(t + "$" + s + s);
int st = m + 1;
int en = st + n - 1;
for (int i = st; i <= en; i++)
if (z[i] >= m)
adj[i - st].push_back((i - st + m) % n);
for (int i = 0; i < s.size(); i++) {
if (cycle(i)) {
cout << -1;
return 0;
}
}
memset(memo, -1, sizeof memo);
int mx = 0;
for (int i = 0; i < s.size(); i++)
mx = max(mx, solve(i));
cout << mx;
} | replace | 21 | 22 | 21 | 22 | 0 | |
p02962 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define eb emplace_back
typedef unsigned long long ll;
typedef long double ld;
const int IN_PROCESS = -2;
const int NOT_DONE = -1;
const int INF = 1e9;
template <class T> struct segtree_t {
vector<T> t;
int n;
segtree_t(const vector<T> &elems) {
n = elems.size();
t = vector<T>(2 * n);
for (int i = 0; i < n; i++) {
t[n + i] = elems[i];
}
build();
}
void build() { // build the tree
for (int i = n - 1; i > 0; --i)
t[i] = min(t[i << 1], t[i << 1 | 1]);
}
void modify(int p, T value) { // set value at position p
for (t[p += n] = value; p > 1; p >>= 1)
t[p >> 1] = min(t[p], t[p ^ 1]);
}
T query(int l, int r) { // sum on interval [l, r)
int res = INF;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1)
res = min(res, t[l++]);
if (r & 1)
res = min(res, t[--r]);
}
return res;
}
};
int calc(vector<int> &cyclic, int idx, const vector<char> &T, const int step,
const char expected) {
// cout << "IDX " << idx << endl;
if (T[idx] != expected) {
return 0;
}
if (cyclic[idx] == IN_PROCESS) {
cyclic[idx] = INF;
return INF;
}
cyclic[idx] = IN_PROCESS;
int val = calc(cyclic, (idx + step) % T.size(), T, step, expected);
if (val != INF) {
val++;
}
cyclic[idx] = val;
return val;
}
vector<int> prefix_function(vector<char> &s) {
int n = (int)s.size();
vector<int> pi(n);
for (int i = 1; i < n; i++) {
int j = pi[i - 1];
while (j > 0 && s[i] != s[j])
j = pi[j - 1];
if (s[i] == s[j])
j++;
pi[i] = j;
}
return pi;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
vector<char> S, T;
string a, b;
cin >> a >> b;
S.reserve(b.length());
for (char e : b) {
S.pb(e);
}
unsigned int sz = 3 * max(a.length(), S.size());
T.reserve(sz + a.length());
while (T.size() < sz) {
for (char e : a) {
T.pb(e);
}
}
vector<int> cyclic(T.size(), NOT_DONE);
int n = S.size();
for (int i = 0; i < (int)cyclic.size(); i++) {
if (cyclic[i] == NOT_DONE) {
calc(cyclic, i, T, n, T[i]);
}
// cout << cyclic[i] << "\t";
}
segtree_t<int> seg(cyclic);
vector<int> pref_size = prefix_function(S);
int res = 0;
int le = 0;
for (int i = 0; i < (int)T.size(); i++) {
if (S[le] == T[i]) {
le++;
if (le == n) {
// cout << "MATCH" << endl;
res = max(res, seg.query(i - (n - 1), i + 1));
le = pref_size[n - 1];
}
} else {
while (le > 0 && S[le] != T[i]) {
le = pref_size[le - 1];
}
if (S[le] == T[i]) {
le++;
}
}
}
if (res == INF) {
res = -1;
}
cout << res << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define eb emplace_back
typedef unsigned long long ll;
typedef long double ld;
const int IN_PROCESS = -2;
const int NOT_DONE = -1;
const int INF = 1e9;
template <class T> struct segtree_t {
vector<T> t;
int n;
segtree_t(const vector<T> &elems) {
n = elems.size();
t = vector<T>(2 * n);
for (int i = 0; i < n; i++) {
t[n + i] = elems[i];
}
build();
}
void build() { // build the tree
for (int i = n - 1; i > 0; --i)
t[i] = min(t[i << 1], t[i << 1 | 1]);
}
void modify(int p, T value) { // set value at position p
for (t[p += n] = value; p > 1; p >>= 1)
t[p >> 1] = min(t[p], t[p ^ 1]);
}
T query(int l, int r) { // sum on interval [l, r)
int res = INF;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1)
res = min(res, t[l++]);
if (r & 1)
res = min(res, t[--r]);
}
return res;
}
};
int calc(vector<int> &cyclic, int idx, const vector<char> &T, const int step,
const char expected) {
// cout << "IDX " << idx << endl;
if (T[idx] != expected) {
return 0;
}
if (cyclic[idx] > 0) {
return cyclic[idx];
}
if (cyclic[idx] == IN_PROCESS) {
cyclic[idx] = INF;
return INF;
}
cyclic[idx] = IN_PROCESS;
int val = calc(cyclic, (idx + step) % T.size(), T, step, expected);
if (val != INF) {
val++;
}
cyclic[idx] = val;
return val;
}
vector<int> prefix_function(vector<char> &s) {
int n = (int)s.size();
vector<int> pi(n);
for (int i = 1; i < n; i++) {
int j = pi[i - 1];
while (j > 0 && s[i] != s[j])
j = pi[j - 1];
if (s[i] == s[j])
j++;
pi[i] = j;
}
return pi;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
vector<char> S, T;
string a, b;
cin >> a >> b;
S.reserve(b.length());
for (char e : b) {
S.pb(e);
}
unsigned int sz = 3 * max(a.length(), S.size());
T.reserve(sz + a.length());
while (T.size() < sz) {
for (char e : a) {
T.pb(e);
}
}
vector<int> cyclic(T.size(), NOT_DONE);
int n = S.size();
for (int i = 0; i < (int)cyclic.size(); i++) {
if (cyclic[i] == NOT_DONE) {
calc(cyclic, i, T, n, T[i]);
}
// cout << cyclic[i] << "\t";
}
segtree_t<int> seg(cyclic);
vector<int> pref_size = prefix_function(S);
int res = 0;
int le = 0;
for (int i = 0; i < (int)T.size(); i++) {
if (S[le] == T[i]) {
le++;
if (le == n) {
// cout << "MATCH" << endl;
res = max(res, seg.query(i - (n - 1), i + 1));
le = pref_size[n - 1];
}
} else {
while (le > 0 && S[le] != T[i]) {
le = pref_size[le - 1];
}
if (S[le] == T[i]) {
le++;
}
}
}
if (res == INF) {
res = -1;
}
cout << res << endl;
}
| insert | 56 | 56 | 56 | 60 | TLE | |
p02962 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define FOR(i, k, n) for (ll i = (k); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)
using namespace std;
using ll = int64_t;
using vecint = vector<ll>;
constexpr ll MOD = 1000000007;
ll pm(ll x, ll i) {
if (i == 0)
return 1;
if ((i % 2) == 1)
return (pm(x, i - 1) * x) % MOD;
ll h = pm(x, i / 2);
return (h * h) % MOD;
}
int main() {
string s, t;
cin >> s >> t;
int n = s.size();
int m = t.size();
stringstream ss;
REP(i, (m + n - 1) / n + 1) { ss << s; }
string ks = ss.str();
ll h = 0, th = 0;
REP(i, m) {
h *= 31415;
h += ks[i];
h %= MOD;
th *= 31415;
th += t[i];
th %= MOD;
}
ll coeff = pm(31415, m);
vector<bool> v(n, false);
REP(i, n) {
if (h == th) {
v[i] = true;
}
h *= 31415;
h %= MOD;
h += (MOD - ks[i]) * coeff;
h %= MOD;
h += ks[i + m];
h %= MOD;
}
vecint len(n, -1);
bool ok = false;
REP(i, n) {
if (len[i] < 0) {
if (!v[i]) {
len[i] = 0;
continue;
}
FOR(j, 1, n) {
ll idx = (i + j * m) % n;
if (len[idx] >= 0) {
REP(k, j) {
ll idx2 = (i + k * m) % n;
len[idx2] = len[idx] + (j - k);
}
break;
} else if (!v[idx]) {
REP(k, j + 1) {
ll idx2 = (i + k * m) % n;
len[idx2] = j - k;
}
break;
} else if (idx == i) {
ok = true;
break;
}
}
if (ok)
break;
}
}
if (ok) {
cout << -1 << endl;
} else {
cout << *max_element(ALL(len)) << endl;
}
return 0;
}
| #include <bits/stdc++.h>
#define FOR(i, k, n) for (ll i = (k); i < (n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)
using namespace std;
using ll = int64_t;
using vecint = vector<ll>;
constexpr ll MOD = 1000000007;
ll pm(ll x, ll i) {
if (i == 0)
return 1;
if ((i % 2) == 1)
return (pm(x, i - 1) * x) % MOD;
ll h = pm(x, i / 2);
return (h * h) % MOD;
}
int main() {
string s, t;
cin >> s >> t;
int n = s.size();
int m = t.size();
stringstream ss;
REP(i, (m + n - 1) / n + 1) { ss << s; }
string ks = ss.str();
ll h = 0, th = 0;
REP(i, m) {
h *= 31415;
h += ks[i];
h %= MOD;
th *= 31415;
th += t[i];
th %= MOD;
}
ll coeff = pm(31415, m);
vector<bool> v(n, false);
REP(i, n) {
if (h == th) {
v[i] = true;
}
h *= 31415;
h %= MOD;
h += (MOD - ks[i]) * coeff;
h %= MOD;
h += ks[i + m];
h %= MOD;
}
vecint len(n, -1);
bool ok = false;
REP(i, n) {
if (len[i] < 0) {
if (!v[i]) {
len[i] = 0;
continue;
}
FOR(j, 1, n + 1) {
ll idx = (i + j * m) % n;
if (len[idx] >= 0) {
REP(k, j) {
ll idx2 = (i + k * m) % n;
len[idx2] = len[idx] + (j - k);
}
break;
} else if (!v[idx]) {
REP(k, j + 1) {
ll idx2 = (i + k * m) % n;
len[idx2] = j - k;
}
break;
} else if (idx == i) {
ok = true;
break;
}
}
if (ok)
break;
}
}
if (ok) {
cout << -1 << endl;
} else {
cout << *max_element(ALL(len)) << endl;
}
return 0;
}
| replace | 57 | 58 | 57 | 58 | TLE | |
p02962 | C++ | Time Limit Exceeded | // #pragma GCC optimize(2)
// #pragma G++ optimize(2)
// #pragma comment(linker,"/STACK:102400000,102400000")
// #include <bits/stdc++.h>
#include <algorithm>
#include <array>
#include <atomic>
#include <bitset>
#include <cassert>
#include <ccomplex>
#include <cctype>
#include <cerrno>
#include <cfenv>
#include <cfloat>
#include <chrono>
#include <cinttypes>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <condition_variable>
#include <csetjmp>
#include <csignal>
#include <cstdalign>
#include <cstdarg>
#include <cstdbool>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctgmath>
#include <ctime>
#include <cwchar>
#include <cwctype>
#include <deque>
#include <forward_list>
#include <fstream>
#include <functional>
#include <future>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <mutex>
#include <numeric>
#include <queue>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <valarray>
#include <vector>
// #include <conio.h>
// #include <windows.h>
using namespace std;
typedef long long LL;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef float fl;
typedef double ld;
typedef long double LD;
typedef pair<int, int> pii;
#if (WIN32) || (WIN64) || (__WIN32) || (__WIN64) || (_WIN32) || (_WIN64) || \
(WINDOWS)
#define lld "%I64d"
#define llu "%I64u"
#else
#define lld "%lld"
#define llu "%llu"
#endif
#define ui(n) ((unsigned int)(n))
#define LL(n) ((long long)(n))
#define ull(n) ((unsigned long long)(n))
#define fl(n) ((float)(n))
#define ld(n) ((double)(n))
#define LD(n) ((long double)(n))
#define char(n) ((char)(n))
#define Bool(n) ((bool)(n))
#define fixpoint(n) fixed << setprecision(n)
const int INF = 1061109567;
const int NINF = -1044266559;
const LL LINF = 4557430888798830399;
const ld eps = 1e-15;
#define MOD (1000000007)
#define PI (3.1415926535897932384626433832795028841971)
/*
#define MB_LEN_MAX 5
#define SHRT_MIN (-32768)
#define SHRT_MAX 32767
#define USHRT_MAX 0xffffU
#define INT_MIN (-2147483647 - 1)
#define INT_MAX 2147483647
#define UINT_MAX 0xffffffffU
#define LONG_MIN (-2147483647L - 1)
#define LONG_MAX 2147483647L
#define ULONG_MAX 0xffffffffUL
#define LLONG_MAX 9223372036854775807ll
#define LLONG_MIN (-9223372036854775807ll - 1)
#define ULLONG_MAX 0xffffffffffffffffull
*/
#define MP make_pair
#define MT make_tuple
#define All(a) (a).begin(), (a).end()
#define pall(a) (a).rbegin(), (a).rend()
#define log2(x) log(x) / log(2)
#define Log(x, y) log(x) / log(y)
#define SZ(a) ((int)(a).size())
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define rep1(i, n) for (int i = 1; i <= ((int)(n)); i++)
#define repa(i, a, n) for (int i = ((int)(a)); i < ((int)(n)); i++)
#define repa1(i, a, n) for (int i = ((int)(a)); i <= ((int)(n)); i++)
#define repd(i, n) for (int i = ((int)(n)) - 1; i >= 0; i--)
#define repd1(i, n) for (int i = ((int)(n)); i >= 1; i--)
#define repda(i, n, a) for (int i = ((int)(n)); i > ((int)(a)); i--)
#define repda1(i, n, a) for (int i = ((int)(n)); i >= ((int)(a)); i--)
#define FOR(i, a, n, step) \
for (int i = ((int)(a)); i < ((int)(n)); i += ((int)(step)))
#define repv(itr, v) \
for (__typeof((v).begin()) itr = (v).begin(); itr != (v).end(); itr++)
#define repV(i, v) for (auto i : v)
#define repE(i, v) for (auto &i : v)
#define MS(x, y) memset(x, y, sizeof(x))
#define MC(x) MS(x, 0)
#define MINF(x) MS(x, 63)
#define MCP(x, y) memcpy(x, y, sizeof(y))
#define sqr(x) ((x) * (x))
#define UN(v) sort(All(v)), v.erase(unique(All(v)), v.end())
#define filein(x) freopen(x, "r", stdin)
#define fileout(x) freopen(x, "w", stdout)
#define fileio(x) \
freopen(x ".in", "r", stdin); \
freopen(x ".out", "w", stdout)
#define filein2(filename, name) ifstream name(filename, ios::in)
#define fileout2(filename, name) ofstream name(filename, ios::out)
#define file(filename, name) fstream name(filename, ios::in | ios::out)
#define Pause system("pause")
#define Cls system("cls")
#define fs first
#define sc second
#define PC(x) putchar(x)
#define GC(x) x = getchar()
#define Endl PC('\n')
#define SF scanf
#define PF printf
inline int Read() {
int X = 0, w = 0;
char ch = 0;
while (!isdigit(ch)) {
w |= ch == '-';
ch = getchar();
}
while (isdigit(ch))
X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
return w ? -X : X;
}
inline void Write(int x) {
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
Write(x / 10);
putchar(x % 10 + '0');
}
inline LL powmod(LL a, LL b) {
LL RES = 1;
a %= MOD;
assert(b >= 0);
for (; b; b >>= 1) {
if (b & 1)
RES = RES * a % MOD;
a = a * a % MOD;
}
return RES % MOD;
}
inline LL gcdll(LL a, LL b) { return b ? gcdll(b, a % b) : a; }
const int dx[] = {0, 1, 0, -1, 1, -1, -1, 1};
const int dy[] = {1, 0, -1, 0, -1, -1, 1, 1};
/************************************************************Begin************************************************************/
const int maxn = 5000010;
string s, t;
int dp[maxn], ans;
LL base[maxn], h1[maxn], h2[maxn];
inline void gethash() {
base[0] = 1;
rep1(i, s.size() - 1) base[i] = base[i - 1] * 29 % MOD;
h1[0] = s[0] - 'a' + 1;
rep1(i, s.size() - 1) h1[i] = (h1[i - 1] * 29 + s[i] - 'a' + 1) % MOD;
h2[0] = t[0] - 'a' + 1;
rep1(i, t.size() - 1) h2[i] = (h2[i - 1] * 29 + t[i] - 'a' + 1) % MOD;
}
inline LL hashid(int i) {
if (i + t.size() - 1 >= s.size())
return -1;
LL hsh = h1[i + t.size() - 1] - h1[i - 1] * base[t.size()] % MOD;
hsh = (hsh + MOD) % MOD;
return hsh;
}
int main() {
cin >> s >> t;
int sz = 2 * (s.size() + t.size());
while (s.size() < sz)
s += s;
gethash();
rep(i, s.size()) dp[i] = hashid(i) == h2[t.size() - 1];
rep(i, s.size()) cerr << dp[i] << ' ';
cerr << endl;
repd(i, s.size()) if (dp[i]) dp[i] += dp[i + t.size()];
repd(i, s.size()) ans = max(ans, dp[i]);
cout << (ans + 1 >= SZ(s) / SZ(t) ? -1 : ans);
return 0;
}
/*************************************************************End**************************************************************/ | // #pragma GCC optimize(2)
// #pragma G++ optimize(2)
// #pragma comment(linker,"/STACK:102400000,102400000")
// #include <bits/stdc++.h>
#include <algorithm>
#include <array>
#include <atomic>
#include <bitset>
#include <cassert>
#include <ccomplex>
#include <cctype>
#include <cerrno>
#include <cfenv>
#include <cfloat>
#include <chrono>
#include <cinttypes>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <condition_variable>
#include <csetjmp>
#include <csignal>
#include <cstdalign>
#include <cstdarg>
#include <cstdbool>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctgmath>
#include <ctime>
#include <cwchar>
#include <cwctype>
#include <deque>
#include <forward_list>
#include <fstream>
#include <functional>
#include <future>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <mutex>
#include <numeric>
#include <queue>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <string>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <valarray>
#include <vector>
// #include <conio.h>
// #include <windows.h>
using namespace std;
typedef long long LL;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef float fl;
typedef double ld;
typedef long double LD;
typedef pair<int, int> pii;
#if (WIN32) || (WIN64) || (__WIN32) || (__WIN64) || (_WIN32) || (_WIN64) || \
(WINDOWS)
#define lld "%I64d"
#define llu "%I64u"
#else
#define lld "%lld"
#define llu "%llu"
#endif
#define ui(n) ((unsigned int)(n))
#define LL(n) ((long long)(n))
#define ull(n) ((unsigned long long)(n))
#define fl(n) ((float)(n))
#define ld(n) ((double)(n))
#define LD(n) ((long double)(n))
#define char(n) ((char)(n))
#define Bool(n) ((bool)(n))
#define fixpoint(n) fixed << setprecision(n)
const int INF = 1061109567;
const int NINF = -1044266559;
const LL LINF = 4557430888798830399;
const ld eps = 1e-15;
#define MOD (1000000007)
#define PI (3.1415926535897932384626433832795028841971)
/*
#define MB_LEN_MAX 5
#define SHRT_MIN (-32768)
#define SHRT_MAX 32767
#define USHRT_MAX 0xffffU
#define INT_MIN (-2147483647 - 1)
#define INT_MAX 2147483647
#define UINT_MAX 0xffffffffU
#define LONG_MIN (-2147483647L - 1)
#define LONG_MAX 2147483647L
#define ULONG_MAX 0xffffffffUL
#define LLONG_MAX 9223372036854775807ll
#define LLONG_MIN (-9223372036854775807ll - 1)
#define ULLONG_MAX 0xffffffffffffffffull
*/
#define MP make_pair
#define MT make_tuple
#define All(a) (a).begin(), (a).end()
#define pall(a) (a).rbegin(), (a).rend()
#define log2(x) log(x) / log(2)
#define Log(x, y) log(x) / log(y)
#define SZ(a) ((int)(a).size())
#define rep(i, n) for (int i = 0; i < ((int)(n)); i++)
#define rep1(i, n) for (int i = 1; i <= ((int)(n)); i++)
#define repa(i, a, n) for (int i = ((int)(a)); i < ((int)(n)); i++)
#define repa1(i, a, n) for (int i = ((int)(a)); i <= ((int)(n)); i++)
#define repd(i, n) for (int i = ((int)(n)) - 1; i >= 0; i--)
#define repd1(i, n) for (int i = ((int)(n)); i >= 1; i--)
#define repda(i, n, a) for (int i = ((int)(n)); i > ((int)(a)); i--)
#define repda1(i, n, a) for (int i = ((int)(n)); i >= ((int)(a)); i--)
#define FOR(i, a, n, step) \
for (int i = ((int)(a)); i < ((int)(n)); i += ((int)(step)))
#define repv(itr, v) \
for (__typeof((v).begin()) itr = (v).begin(); itr != (v).end(); itr++)
#define repV(i, v) for (auto i : v)
#define repE(i, v) for (auto &i : v)
#define MS(x, y) memset(x, y, sizeof(x))
#define MC(x) MS(x, 0)
#define MINF(x) MS(x, 63)
#define MCP(x, y) memcpy(x, y, sizeof(y))
#define sqr(x) ((x) * (x))
#define UN(v) sort(All(v)), v.erase(unique(All(v)), v.end())
#define filein(x) freopen(x, "r", stdin)
#define fileout(x) freopen(x, "w", stdout)
#define fileio(x) \
freopen(x ".in", "r", stdin); \
freopen(x ".out", "w", stdout)
#define filein2(filename, name) ifstream name(filename, ios::in)
#define fileout2(filename, name) ofstream name(filename, ios::out)
#define file(filename, name) fstream name(filename, ios::in | ios::out)
#define Pause system("pause")
#define Cls system("cls")
#define fs first
#define sc second
#define PC(x) putchar(x)
#define GC(x) x = getchar()
#define Endl PC('\n')
#define SF scanf
#define PF printf
inline int Read() {
int X = 0, w = 0;
char ch = 0;
while (!isdigit(ch)) {
w |= ch == '-';
ch = getchar();
}
while (isdigit(ch))
X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
return w ? -X : X;
}
inline void Write(int x) {
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
Write(x / 10);
putchar(x % 10 + '0');
}
inline LL powmod(LL a, LL b) {
LL RES = 1;
a %= MOD;
assert(b >= 0);
for (; b; b >>= 1) {
if (b & 1)
RES = RES * a % MOD;
a = a * a % MOD;
}
return RES % MOD;
}
inline LL gcdll(LL a, LL b) { return b ? gcdll(b, a % b) : a; }
const int dx[] = {0, 1, 0, -1, 1, -1, -1, 1};
const int dy[] = {1, 0, -1, 0, -1, -1, 1, 1};
/************************************************************Begin************************************************************/
const int maxn = 5000010;
string s, t;
int dp[maxn], ans;
LL base[maxn], h1[maxn], h2[maxn];
inline void gethash() {
base[0] = 1;
rep1(i, s.size() - 1) base[i] = base[i - 1] * 29 % MOD;
h1[0] = s[0] - 'a' + 1;
rep1(i, s.size() - 1) h1[i] = (h1[i - 1] * 29 + s[i] - 'a' + 1) % MOD;
h2[0] = t[0] - 'a' + 1;
rep1(i, t.size() - 1) h2[i] = (h2[i - 1] * 29 + t[i] - 'a' + 1) % MOD;
}
inline LL hashid(int i) {
if (i + t.size() - 1 >= s.size())
return -1;
LL hsh = h1[i + t.size() - 1] - h1[i - 1] * base[t.size()] % MOD;
hsh = (hsh + MOD) % MOD;
return hsh;
}
int main() {
cin >> s >> t;
int sz = 2 * (s.size() + t.size());
while (s.size() < sz)
s += s;
gethash();
rep(i, s.size()) dp[i] = hashid(i) == h2[t.size() - 1];
repd(i, s.size()) if (dp[i]) dp[i] += dp[i + t.size()];
repd(i, s.size()) ans = max(ans, dp[i]);
cout << (ans + 1 >= SZ(s) / SZ(t) ? -1 : ans);
return 0;
}
/*************************************************************End**************************************************************/ | delete | 237 | 239 | 237 | 237 | TLE | |
p02962 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop \
char nyaa; \
cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define Per(i, sta, n) for (int i = n - 1; i >= sta; i--)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int gcd(int a, int b) {
if (a < b)
swap(a, b);
if (b == 0)
return a;
return gcd(b, a % b);
}
// kmp をやるための前計算
vector<int> makeTable(const string &s) {
int n = s.size();
vector<int> ret(n + 1);
ret[0] = -1;
int j = -1;
for (int i = 0; i < n; i++) {
while (j >= 0 && s[i] != s[j])
j = ret[j];
ret[i + 1] = ++j;
}
return ret;
}
// str の中に word とマッチする場所のリストを返す
// ret のそれぞれの要素 el は, 「str[el] からの文字列が word
// と一致する」ことを示す
vector<int> kmp(const string &str, const string &word) {
vector<int> table = makeTable(word), ret;
int m = 0, i = 0, n = str.size();
while (m + i < n) {
if (word[i] == str[m + i]) {
if (++i == (int)(word.size())) {
ret.push_back(m);
m = m + i - table[i];
i = table[i];
}
} else {
m = m + i - table[i];
if (i > 0)
i = table[i];
}
}
return ret;
}
string s, s_, t;
int d[1500010];
void solve() {
cin >> s_ >> t;
while (s.length() < t.length()) {
s += s_;
}
s += s + s;
// cout << s << " " << t << endl;
vector<int> v_ = kmp(s, t), v;
if (v_.empty()) {
cout << 0 << endl;
return;
}
int n = 2 * s.length() / 3, sta = v_[0];
for (int p : v_) {
if (p - sta >= n)
break;
v.push_back(p - sta);
}
int g = gcd(n, t.length());
bool eta = true;
int ans = 0;
for (int p : v) {
// cout << p << endl;
if (p < t.length())
d[p] = 1;
else
d[p] = d[p - t.length()] + 1;
ans = max(ans, d[p]);
}
rep(i, n / g) {
if (d[i * g] == 0)
eta = false;
}
if (eta) {
cout << -1 << endl;
return;
} else {
cout << ans << endl;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(50);
solve();
} | #include <algorithm>
#include <bitset>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop \
char nyaa; \
cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define Per(i, sta, n) for (int i = n - 1; i >= sta; i--)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int gcd(int a, int b) {
if (a < b)
swap(a, b);
if (b == 0)
return a;
return gcd(b, a % b);
}
// kmp をやるための前計算
vector<int> makeTable(const string &s) {
int n = s.size();
vector<int> ret(n + 1);
ret[0] = -1;
int j = -1;
for (int i = 0; i < n; i++) {
while (j >= 0 && s[i] != s[j])
j = ret[j];
ret[i + 1] = ++j;
}
return ret;
}
// str の中に word とマッチする場所のリストを返す
// ret のそれぞれの要素 el は, 「str[el] からの文字列が word
// と一致する」ことを示す
vector<int> kmp(const string &str, const string &word) {
vector<int> table = makeTable(word), ret;
int m = 0, i = 0, n = str.size();
while (m + i < n) {
if (word[i] == str[m + i]) {
if (++i == (int)(word.size())) {
ret.push_back(m);
m = m + i - table[i];
i = table[i];
}
} else {
m = m + i - table[i];
if (i > 0)
i = table[i];
}
}
return ret;
}
string s, s_, t;
int d[3000010];
void solve() {
cin >> s_ >> t;
while (s.length() < t.length()) {
s += s_;
}
s += s + s;
// cout << s << " " << t << endl;
vector<int> v_ = kmp(s, t), v;
if (v_.empty()) {
cout << 0 << endl;
return;
}
int n = 2 * s.length() / 3, sta = v_[0];
for (int p : v_) {
if (p - sta >= n)
break;
v.push_back(p - sta);
}
int g = gcd(n, t.length());
bool eta = true;
int ans = 0;
for (int p : v) {
// cout << p << endl;
if (p < t.length())
d[p] = 1;
else
d[p] = d[p - t.length()] + 1;
ans = max(ans, d[p]);
}
rep(i, n / g) {
if (d[i * g] == 0)
eta = false;
}
if (eta) {
cout << -1 << endl;
return;
} else {
cout << ans << endl;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(50);
solve();
} | replace | 86 | 87 | 86 | 87 | 0 | |
p02962 | C++ | Runtime Error | /**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author tatsumack
*/
#include <fstream>
#include <iostream>
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define int long long
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REPS(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i)
#define REV(i, a, b) for (int i = (a); i >= (b); --i)
#define CLR(a, b) memset((a), (b), sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF 1e18
#define fcout cout << fixed << setprecision(12)
using namespace std;
vector<int> makeTable(const string &s) {
int n = s.size();
vector<int> ret(n + 1);
ret[0] = -1;
int j = -1;
for (int i = 0; i < n; i++) {
while (j >= 0 && s[i] != s[j])
j = ret[j];
j++;
if (i + 1 < s.size() && s[i + 1] == s[j]) {
ret[i + 1] = ret[j];
} else {
ret[i + 1] = j;
}
}
return ret;
}
vector<int> kmp(const string &str, const string &word) {
vector<int> table = makeTable(word), ret;
int m = 0, i = 0, n = str.size();
while (m + i < n) {
if (word[i] == str[m + i]) {
if (++i == (int)(word.size())) {
ret.push_back(m);
m = m + i - table[i];
i = table[i];
}
} else {
m = m + i - table[i];
if (i > 0)
i = table[i];
}
}
return ret;
}
class FStringsOfEternity {
public:
static constexpr int kStressIterations = 0;
static void generateTest(std::ostream &test) {}
set<int> pos;
vector<bool> used;
vector<int> memo;
string u;
string s, t;
int dfs(int v) {
if (memo[v] != -1)
return memo[v];
if (used[v]) {
return memo[v] = INF;
}
used[v] = true;
if (pos.count(v)) {
int to = (v + t.size()) % s.size();
return memo[v] = dfs(to) + 1;
} else {
return memo[v] = 0;
}
}
void solve(std::istream &cin, std::ostream &cout) {
cin >> s >> t;
u = s;
while (u.size() < s.size() + t.size()) {
u += s;
}
auto v = kmp(u, t);
REP(i, v.size()) pos.insert(v[i] % s.size());
used = vector<bool>(s.size());
memo = vector<int>(s.size(), -1);
for (auto i : pos) {
dfs(i);
}
int ans = 0;
REP(i, s.size()) { ans = max(ans, memo[i]); }
if (ans >= INF)
ans = -1;
cout << ans << endl;
}
};
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
std::istream &in(std::cin);
std::ostream &out(std::cout);
FStringsOfEternity solver;
solver.solve(in, out);
return 0;
}
| /**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author tatsumack
*/
#include <fstream>
#include <iostream>
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define int long long
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REPS(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i)
#define REV(i, a, b) for (int i = (a); i >= (b); --i)
#define CLR(a, b) memset((a), (b), sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF 1e18
#define fcout cout << fixed << setprecision(12)
using namespace std;
vector<int> makeTable(const string &s) {
int n = s.size();
vector<int> ret(n + 1);
ret[0] = -1;
int j = -1;
for (int i = 0; i < n; i++) {
while (j >= 0 && s[i] != s[j])
j = ret[j];
j++;
ret[i + 1] = j;
}
return ret;
}
vector<int> kmp(const string &str, const string &word) {
vector<int> table = makeTable(word), ret;
int m = 0, i = 0, n = str.size();
while (m + i < n) {
if (word[i] == str[m + i]) {
if (++i == (int)(word.size())) {
ret.push_back(m);
m = m + i - table[i];
i = table[i];
}
} else {
m = m + i - table[i];
if (i > 0)
i = table[i];
}
}
return ret;
}
class FStringsOfEternity {
public:
static constexpr int kStressIterations = 0;
static void generateTest(std::ostream &test) {}
set<int> pos;
vector<bool> used;
vector<int> memo;
string u;
string s, t;
int dfs(int v) {
if (memo[v] != -1)
return memo[v];
if (used[v]) {
return memo[v] = INF;
}
used[v] = true;
if (pos.count(v)) {
int to = (v + t.size()) % s.size();
return memo[v] = dfs(to) + 1;
} else {
return memo[v] = 0;
}
}
void solve(std::istream &cin, std::ostream &cout) {
cin >> s >> t;
u = s;
while (u.size() < s.size() + t.size()) {
u += s;
}
auto v = kmp(u, t);
REP(i, v.size()) pos.insert(v[i] % s.size());
used = vector<bool>(s.size());
memo = vector<int>(s.size(), -1);
for (auto i : pos) {
dfs(i);
}
int ans = 0;
REP(i, s.size()) { ans = max(ans, memo[i]); }
if (ans >= INF)
ans = -1;
cout << ans << endl;
}
};
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
std::istream &in(std::cin);
std::ostream &out(std::cout);
FStringsOfEternity solver;
solver.solve(in, out);
return 0;
}
| replace | 57 | 62 | 57 | 58 | 0 | |
p02962 | C++ | Runtime Error | #include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
typedef long double ld;
typedef long long ll;
const int max_n = 100011, inf = 1000111222;
const ll inff = 1000111222333444;
string s, t;
char ss[max_n];
bool is_cycle(string a, const string &b) {
// cout << "is_cycle: " << a << ", " << b << endl;
if (b.length() % a.length() != 0)
return false;
int i = 0;
for (int q = 0; q < b.length(); ++q) {
if (b[q] != a[i])
return false;
++i;
if (i == a.length())
i = 0;
}
return true;
}
void get_p(const string &b, vector<int> &p) {
assert(p.size() == b.length());
if (b.empty())
return;
p[0] = 0;
for (int i = 1; i < p.size(); ++i) {
int k = p[i - 1];
while (k > 0 && b[k] != b[i]) {
k = p[k - 1];
}
if (b[i] == b[k])
++k;
p[i] = k;
}
}
bool is_eq_cycle(string a, string b) {
// cout << "is_eq_cycle: " << a << ", " << b << endl;
if (a.length() != b.length())
return false;
b = a + "#" + b + b;
vector<int> p(b.length());
get_p(b, p);
for (int i = 1; i < b.length(); ++i) {
if (p[i] == a.length()) {
return true;
}
}
return false;
}
int get_ans(const string &a, const string &b) {
string s = a + "#" + b + b;
vector<int> dp(s.length()), p(s.length());
get_p(s, p);
int ans = 0;
// cout << s << endl;
// for (int i = 0; i < s.length(); ++i) cout << p[i] << " "; cout << endl;
for (int i = a.length(); i < s.length(); ++i) {
if (p[i] == a.length()) {
dp[i] = dp[i - a.length()] + 1;
ans = max(ans, dp[i]);
}
}
return ans;
}
int main() {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
scanf("%s", ss);
s = ss;
scanf("%s", ss);
t = ss;
int g = __gcd(s.length(), t.length());
if (is_cycle(s.substr(0, g), s) && is_cycle(t.substr(0, g), t) &&
is_eq_cycle(s.substr(0, g), t.substr(0, g))) {
cout << -1 << endl;
return 0;
}
while (s.length() < t.length())
s = s + s;
s = s + s;
cout << get_ans(t, s) << endl;
return 0;
}
| #include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
typedef long double ld;
typedef long long ll;
const int max_n = 500011, inf = 1000111222;
const ll inff = 1000111222333444;
string s, t;
char ss[max_n];
bool is_cycle(string a, const string &b) {
// cout << "is_cycle: " << a << ", " << b << endl;
if (b.length() % a.length() != 0)
return false;
int i = 0;
for (int q = 0; q < b.length(); ++q) {
if (b[q] != a[i])
return false;
++i;
if (i == a.length())
i = 0;
}
return true;
}
void get_p(const string &b, vector<int> &p) {
assert(p.size() == b.length());
if (b.empty())
return;
p[0] = 0;
for (int i = 1; i < p.size(); ++i) {
int k = p[i - 1];
while (k > 0 && b[k] != b[i]) {
k = p[k - 1];
}
if (b[i] == b[k])
++k;
p[i] = k;
}
}
bool is_eq_cycle(string a, string b) {
// cout << "is_eq_cycle: " << a << ", " << b << endl;
if (a.length() != b.length())
return false;
b = a + "#" + b + b;
vector<int> p(b.length());
get_p(b, p);
for (int i = 1; i < b.length(); ++i) {
if (p[i] == a.length()) {
return true;
}
}
return false;
}
int get_ans(const string &a, const string &b) {
string s = a + "#" + b + b;
vector<int> dp(s.length()), p(s.length());
get_p(s, p);
int ans = 0;
// cout << s << endl;
// for (int i = 0; i < s.length(); ++i) cout << p[i] << " "; cout << endl;
for (int i = a.length(); i < s.length(); ++i) {
if (p[i] == a.length()) {
dp[i] = dp[i - a.length()] + 1;
ans = max(ans, dp[i]);
}
}
return ans;
}
int main() {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
scanf("%s", ss);
s = ss;
scanf("%s", ss);
t = ss;
int g = __gcd(s.length(), t.length());
if (is_cycle(s.substr(0, g), s) && is_cycle(t.substr(0, g), t) &&
is_eq_cycle(s.substr(0, g), t.substr(0, g))) {
cout << -1 << endl;
return 0;
}
while (s.length() < t.length())
s = s + s;
s = s + s;
cout << get_ans(t, s) << endl;
return 0;
}
| replace | 9 | 10 | 9 | 10 | 0 | |
p02962 | C++ | Runtime Error | #include <cstdio>
#include <cstring>
#include <random>
#include <stack>
#include <vector>
using namespace std;
#define int long long
#define uint unsigned int
#define dotimes(i, n) for (int i = 0, i##max__ = (n); i < i##max__; i++)
#define whole(x, f, ...) \
([&](decltype((x)) c) { return (f)(begin(c), end(c), ##__VA_ARGS__); })(x)
int rint() {
int x;
scanf("%lld", &x);
return x;
}
void wint(int x) { printf("%lld\n", x); }
template <typename T> int size(T const &c) {
return static_cast<int>(c.size());
}
template <typename T> bool maxs(T &a, T const &b) {
return a < b ? a = b, true : false;
}
template <typename T> bool mins(T &a, T const &b) {
return a > b ? a = b, true : false;
}
inline int lg(int x) {
return 63 - __builtin_clzll(static_cast<unsigned int>(x));
}
constexpr uint mask30 = (1ull << 30) - 1, mask31 = (1ull << 31) - 1,
mask61 = (1ull << 61) - 1;
uint add61(uint x, uint y) {
uint z = x + y, w = (z >> 61) + (z & mask61);
return w < mask61 ? w : w - mask61;
}
uint sub61(uint x, uint y) { return x >= y ? x - y : mask61 - (y - x); }
uint mul61(uint x, uint y) {
uint a = x >> 31, b = x & mask31, c = y >> 31, d = y & mask31,
z = a * d + b * c, e = z >> 30, f = z & mask30;
return (2 * a * c + e + (f << 31) + b * d) % mask61;
}
uint pow61(uint x, int n) {
uint r = 1;
for (; n; n >>= 1, x = mul61(x, x))
if (n & 1)
r = mul61(r, x);
return r;
}
signed main() {
constexpr int s_max = 5e5;
char s[s_max + 1], t[s_max + 1];
scanf("%s\n%s\n", s, t);
const int m = strlen(s), n = strlen(t);
const uint b1 = []() {
std::random_device rd;
std::mt19937_64 prng((static_cast<uint>(rd()) << 32) | rd());
std::uniform_int_distribution<uint> uid(256, mask61 - 2);
return uid(prng);
}();
vector<bool> fs(n);
{
uint hs = 0, ht = 0, b = 1;
for (int i = 0;; i++, b = mul61(b1, b)) {
ht = add61(ht, mul61(b, t[n - i - 1]));
hs = add61(hs, mul61(b, s[(n - i - 1) % m]));
if (i == n - 1)
break;
}
fs[0] = ht == hs;
dotimes(i, m - 1) {
hs = add61(mul61(b1, sub61(hs, mul61(b, s[i]))), s[(i + n) % m]);
fs[i + 1] = ht == hs;
}
}
vector<int> rs(m);
int r = 0;
dotimes(i, m) {
stack<int> path;
for (int j = i;; j = (j + n) % m) {
if (rs[j] || !fs[j]) {
if (rs[j] == -1)
r = -1;
else {
for (int c = rs[j] + 1; !path.empty(); c++, path.pop())
rs[path.top()] = c;
maxs(r, rs[i]);
}
break;
}
rs[j] = -1;
path.emplace(j);
}
if (r < 0)
break;
}
wint(r);
return 0;
}
| #include <cstdio>
#include <cstring>
#include <random>
#include <stack>
#include <vector>
using namespace std;
#define int long long
#define uint unsigned int
#define dotimes(i, n) for (int i = 0, i##max__ = (n); i < i##max__; i++)
#define whole(x, f, ...) \
([&](decltype((x)) c) { return (f)(begin(c), end(c), ##__VA_ARGS__); })(x)
int rint() {
int x;
scanf("%lld", &x);
return x;
}
void wint(int x) { printf("%lld\n", x); }
template <typename T> int size(T const &c) {
return static_cast<int>(c.size());
}
template <typename T> bool maxs(T &a, T const &b) {
return a < b ? a = b, true : false;
}
template <typename T> bool mins(T &a, T const &b) {
return a > b ? a = b, true : false;
}
inline int lg(int x) {
return 63 - __builtin_clzll(static_cast<unsigned int>(x));
}
constexpr uint mask30 = (1ull << 30) - 1, mask31 = (1ull << 31) - 1,
mask61 = (1ull << 61) - 1;
uint add61(uint x, uint y) {
uint z = x + y, w = (z >> 61) + (z & mask61);
return w < mask61 ? w : w - mask61;
}
uint sub61(uint x, uint y) { return x >= y ? x - y : mask61 - (y - x); }
uint mul61(uint x, uint y) {
uint a = x >> 31, b = x & mask31, c = y >> 31, d = y & mask31,
z = a * d + b * c, e = z >> 30, f = z & mask30;
return (2 * a * c + e + (f << 31) + b * d) % mask61;
}
uint pow61(uint x, int n) {
uint r = 1;
for (; n; n >>= 1, x = mul61(x, x))
if (n & 1)
r = mul61(r, x);
return r;
}
signed main() {
constexpr int s_max = 5e5;
char s[s_max + 1], t[s_max + 1];
scanf("%s\n%s\n", s, t);
const int m = strlen(s), n = strlen(t);
const uint b1 = []() {
std::random_device rd;
std::mt19937_64 prng((static_cast<uint>(rd()) << 32) | rd());
std::uniform_int_distribution<uint> uid(256, mask61 - 2);
return uid(prng);
}();
vector<bool> fs(m);
{
uint hs = 0, ht = 0, b = 1;
for (int i = 0;; i++, b = mul61(b1, b)) {
ht = add61(ht, mul61(b, t[n - i - 1]));
hs = add61(hs, mul61(b, s[(n - i - 1) % m]));
if (i == n - 1)
break;
}
fs[0] = ht == hs;
dotimes(i, m - 1) {
hs = add61(mul61(b1, sub61(hs, mul61(b, s[i]))), s[(i + n) % m]);
fs[i + 1] = ht == hs;
}
}
vector<int> rs(m);
int r = 0;
dotimes(i, m) {
stack<int> path;
for (int j = i;; j = (j + n) % m) {
if (rs[j] || !fs[j]) {
if (rs[j] == -1)
r = -1;
else {
for (int c = rs[j] + 1; !path.empty(); c++, path.pop())
rs[path.top()] = c;
maxs(r, rs[i]);
}
break;
}
rs[j] = -1;
path.emplace(j);
}
if (r < 0)
break;
}
wint(r);
return 0;
}
| replace | 70 | 71 | 70 | 71 | 0 | |
p02962 | C++ | Runtime Error | #include <algorithm>
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
#include <vector>
template <typename T> void fin(T const &t) {
std::cout << t << std::endl;
exit(0);
}
template <typename S>
void sa_is(S s, int B, std::vector<int> &sa, std::vector<int> &cnt) {
int n = s.size();
sa.resize(n + 1);
if (n == 0)
return;
for (auto &c : s)
++c;
s.push_back(0);
++B;
std::vector<bool> iss(n + 1);
std::vector<int> bin(B + 1), lms, is_lms(n + 1, -1);
iss[n] = true;
++bin[1];
for (int i = n - 1; i >= 0; --i) {
iss[i] = (s[i] == s[i + 1]) ? iss[i + 1] : (s[i] < s[i + 1]);
if (!iss[i] && iss[i + 1]) {
is_lms[i + 1] = lms.size();
lms.push_back(i + 1);
}
++bin[s[i] + 1];
}
for (int i = 0; i < B; ++i)
bin[i + 1] += bin[i];
auto induce = [&](std::vector<int> const &lms) {
sa.assign(n + 1, -1);
cnt.assign(B, 0);
for (auto x : lms)
sa[bin[s[x] + 1] - ++cnt[s[x]]] = x;
std::fill(cnt.begin(), cnt.end(), 0);
for (auto x : sa)
if (--x >= 0 && !iss[x]) {
sa[bin[s[x]] + cnt[s[x]]++] = x;
}
std::fill(cnt.begin(), cnt.end(), 0);
for (int i = n; i >= 0; --i) {
int x = sa[i] - 1;
if (x >= 0 && iss[x])
sa[bin[s[x] + 1] - ++cnt[s[x]]] = x;
}
};
induce(lms);
int m = lms.size();
if (m <= 1)
return;
std::vector<int> rec_lms;
rec_lms.reserve(m);
for (auto x : sa)
if (is_lms[x] >= 0)
rec_lms.push_back(x);
int rec_n = 1;
std::vector<int> rec_s(m);
rec_s[m - 1 - is_lms[rec_lms[1]]] = rec_n;
for (int i = 2; i < m; ++i) {
int xl = rec_lms[i];
int yl = rec_lms[i - 1];
int xr = lms[is_lms[xl] - 1];
int yr = lms[is_lms[yl] - 1];
if (xr - xl != yr - yl)
++rec_n;
else
while (xl <= xr) {
if (s[xl] != s[yl]) {
++rec_n;
break;
}
++xl;
++yl;
}
rec_s[m - 1 - is_lms[rec_lms[i]]] = rec_n;
}
sa_is(std::move(rec_s), rec_n + 1, sa, cnt);
int li = m;
for (int i = 1; i <= m; ++i) {
rec_lms[--li] = lms[m - 1 - sa[i]];
}
induce(rec_lms);
}
template <typename S> struct suffix_array {
int n_;
std::vector<int> sa, rank, lcp, cnt;
suffix_array(S s, int B) : n_(s.size()), rank(n_ + 1), lcp(n_) {
sa_is(s, B, sa, cnt);
for (int i = 0; i <= n_; ++i)
rank[sa[i]] = i;
int h = 0;
lcp[0] = 0;
for (int i = 0; i < n_; ++i) {
int j = sa[rank[i] - 1];
if (h > 0)
--h;
for (; i + h < n_ && j + h < n_; ++h)
if (s[j + h] != s[i + h])
break;
lcp[rank[i] - 1] = h;
}
}
};
template <int MAXV> struct scc {
std::vector<int> G[MAXV], rG[MAXV], vs;
bool used[MAXV];
// int cmp[MAXV];
scc() {}
void dfs(int v) {
used[v] = true;
for (auto c : G[v])
if (!used[c])
dfs(c);
vs.push_back(v);
}
void rdfs(int v, int k) {
used[v] = true;
// cmp[v] = k;
for (auto c : rG[v])
if (!used[c])
rdfs(c, k);
}
int build(int V) {
std::fill_n(used, V, false);
vs.clear();
for (int v = 0; v < V; ++v)
if (!used[v])
dfs(v);
std::fill_n(used, V, false);
int k = 0;
for (int i = vs.size() - 1; i >= 0; --i) {
if (!used[vs[i]])
rdfs(vs[i], k++);
}
return k;
}
void add_edge(int fr, int to) {
G[fr].push_back(to);
rG[to].push_back(fr);
}
};
int const MAXN = 5e5;
scc<MAXN> sc;
int depth[MAXN];
int calc_depth(int v) {
if (depth[v] >= 0)
return depth[v];
int res = -1;
for (auto c : sc.G[v])
res = std::max(res, calc_depth(c));
++res;
return depth[v] = res;
}
int main() {
std::string S, T;
std::cin >> S >> T;
int N = S.size(), M = T.size(), K = N + M - 1;
std::vector<std::uint8_t> s2(K), t(M);
for (int i = 0; i < N; ++i)
s2[i] = S[i] - 'a';
for (int i = N; i < K; ++i)
s2[i] = s2[i - N];
for (int i = 0; i < M; ++i)
t[i] = T[i] - 'a';
suffix_array<std::vector<std::uint8_t>> sa(s2, 26);
int lb = std::partition_point(sa.sa.begin(), sa.sa.end(),
[&](auto &i) {
for (int j = 0; j < M && j + i < K; ++j) {
if (s2[i + j] != t[j])
return s2[i + j] < t[j];
}
return K - i < M;
}) -
sa.sa.begin();
int ub = std::partition_point(sa.sa.begin(), sa.sa.end(),
[&](auto &i) {
for (int j = 0; j < M && j + i < K; ++j) {
if (s2[i + j] != t[j])
return s2[i + j] < t[j];
}
return true;
}) -
sa.sa.begin();
std::vector<int> v;
for (int i = lb; i < ub; ++i)
if (sa.sa[i] < N)
v.push_back(sa.sa[i]);
if (v.size() < 1)
fin(0);
for (int x : v)
sc.add_edge(x, (x + M) % N);
if (sc.build(N) < N)
fin(-1);
std::fill_n(depth, N, -1);
int ans = 0;
for (int i = 0; i < N; ++i) {
ans = std::max(ans, calc_depth(i));
}
fin(ans);
return 0;
}
| #include <algorithm>
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
#include <vector>
template <typename T> void fin(T const &t) {
std::cout << t << std::endl;
exit(0);
}
template <typename S>
void sa_is(S s, int B, std::vector<int> &sa, std::vector<int> &cnt) {
int n = s.size();
sa.resize(n + 1);
if (n == 0)
return;
for (auto &c : s)
++c;
s.push_back(0);
++B;
std::vector<bool> iss(n + 1);
std::vector<int> bin(B + 1), lms, is_lms(n + 1, -1);
iss[n] = true;
++bin[1];
for (int i = n - 1; i >= 0; --i) {
iss[i] = (s[i] == s[i + 1]) ? iss[i + 1] : (s[i] < s[i + 1]);
if (!iss[i] && iss[i + 1]) {
is_lms[i + 1] = lms.size();
lms.push_back(i + 1);
}
++bin[s[i] + 1];
}
for (int i = 0; i < B; ++i)
bin[i + 1] += bin[i];
auto induce = [&](std::vector<int> const &lms) {
sa.assign(n + 1, -1);
cnt.assign(B, 0);
for (auto x : lms)
sa[bin[s[x] + 1] - ++cnt[s[x]]] = x;
std::fill(cnt.begin(), cnt.end(), 0);
for (auto x : sa)
if (--x >= 0 && !iss[x]) {
sa[bin[s[x]] + cnt[s[x]]++] = x;
}
std::fill(cnt.begin(), cnt.end(), 0);
for (int i = n; i >= 0; --i) {
int x = sa[i] - 1;
if (x >= 0 && iss[x])
sa[bin[s[x] + 1] - ++cnt[s[x]]] = x;
}
};
induce(lms);
int m = lms.size();
if (m <= 1)
return;
std::vector<int> rec_lms;
rec_lms.reserve(m);
for (auto x : sa)
if (is_lms[x] >= 0)
rec_lms.push_back(x);
int rec_n = 1;
std::vector<int> rec_s(m);
rec_s[m - 1 - is_lms[rec_lms[1]]] = rec_n;
for (int i = 2; i < m; ++i) {
int xl = rec_lms[i];
int yl = rec_lms[i - 1];
int xr = lms[is_lms[xl] - 1];
int yr = lms[is_lms[yl] - 1];
if (xr - xl != yr - yl)
++rec_n;
else
while (xl <= xr) {
if (s[xl] != s[yl]) {
++rec_n;
break;
}
++xl;
++yl;
}
rec_s[m - 1 - is_lms[rec_lms[i]]] = rec_n;
}
sa_is(std::move(rec_s), rec_n + 1, sa, cnt);
int li = m;
for (int i = 1; i <= m; ++i) {
rec_lms[--li] = lms[m - 1 - sa[i]];
}
induce(rec_lms);
}
template <typename S> struct suffix_array {
int n_;
std::vector<int> sa, rank, lcp, cnt;
suffix_array(S s, int B) : n_(s.size()), rank(n_ + 1), lcp(n_) {
sa_is(s, B, sa, cnt);
for (int i = 0; i <= n_; ++i)
rank[sa[i]] = i;
int h = 0;
lcp[0] = 0;
for (int i = 0; i < n_; ++i) {
int j = sa[rank[i] - 1];
if (h > 0)
--h;
for (; i + h < n_ && j + h < n_; ++h)
if (s[j + h] != s[i + h])
break;
lcp[rank[i] - 1] = h;
}
}
};
template <int MAXV> struct scc {
std::vector<int> G[MAXV], rG[MAXV], vs;
bool used[MAXV];
// int cmp[MAXV];
scc() {}
void dfs(int v) {
used[v] = true;
for (auto c : G[v])
if (!used[c])
dfs(c);
vs.push_back(v);
}
void rdfs(int v, int k) {
used[v] = true;
// cmp[v] = k;
for (auto c : rG[v])
if (!used[c])
rdfs(c, k);
}
int build(int V) {
std::fill_n(used, V, false);
vs.clear();
for (int v = 0; v < V; ++v)
if (!used[v])
dfs(v);
std::fill_n(used, V, false);
int k = 0;
for (int i = vs.size() - 1; i >= 0; --i) {
if (!used[vs[i]])
rdfs(vs[i], k++);
}
return k;
}
void add_edge(int fr, int to) {
G[fr].push_back(to);
rG[to].push_back(fr);
}
};
int const MAXN = 5e5;
scc<MAXN> sc;
int depth[MAXN];
int calc_depth(int v) {
if (depth[v] >= 0)
return depth[v];
int res = -1;
for (auto c : sc.G[v])
res = std::max(res, calc_depth(c));
++res;
return depth[v] = res;
}
int main() {
std::string S, T;
std::cin >> S >> T;
int N = S.size(), M = T.size(), K = N + M - 1;
std::vector<std::uint8_t> s2(K), t(M);
for (int i = 0; i < N; ++i)
s2[i] = S[i] - 'a';
for (int i = N; i < K; ++i)
s2[i] = s2[i - N];
for (int i = 0; i < M; ++i)
t[i] = T[i] - 'a';
suffix_array<std::vector<std::uint8_t>> sa(s2, 26);
int lb = std::partition_point(sa.sa.begin(), sa.sa.end(),
[&](auto &i) {
for (int j = 0; j < M && j + i < K; ++j) {
if (s2[i + j] != t[j])
return s2[i + j] < t[j];
}
return K - i < M;
}) -
sa.sa.begin();
int ub = std::partition_point(sa.sa.begin(), sa.sa.end(),
[&](auto &i) {
for (int j = 0; j < M && j + i < K; ++j) {
if (s2[i + j] != t[j])
return s2[i + j] < t[j];
}
return true;
}) -
sa.sa.begin();
std::vector<int> v;
for (int i = lb; i < ub; ++i)
if (sa.sa[i] < N)
v.push_back(sa.sa[i]);
if (v.size() < 1)
fin(0);
for (int fr : v) {
int to = (fr + M) % N;
if (fr == to)
fin(-1);
sc.add_edge(fr, to);
}
if (sc.build(N) < N)
fin(-1);
std::fill_n(depth, N, -1);
int ans = 0;
for (int i = 0; i < N; ++i) {
ans = std::max(ans, calc_depth(i));
}
fin(ans);
return 0;
}
| replace | 205 | 207 | 205 | 211 | 0 | |
p02962 | C++ | Runtime Error | #include <algorithm>
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
#include <vector>
template <typename T> void fin(T const &t) {
std::cout << t << std::endl;
exit(0);
}
template <int MAXV> struct scc {
std::vector<int> G[MAXV], rG[MAXV], vs;
bool used[MAXV];
// int cmp[MAXV];
scc() {}
void dfs(int v) {
used[v] = true;
for (auto c : G[v])
if (!used[c])
dfs(c);
vs.push_back(v);
}
void rdfs(int v, int k) {
used[v] = true;
// cmp[v] = k;
for (auto c : rG[v])
if (!used[c])
rdfs(c, k);
}
int build(int V) {
std::fill_n(used, V, false);
vs.clear();
for (int v = 0; v < V; ++v)
if (!used[v])
dfs(v);
std::fill_n(used, V, false);
int k = 0;
for (int i = vs.size() - 1; i >= 0; --i) {
if (!used[vs[i]])
rdfs(vs[i], k++);
}
return k;
}
void add_edge(int fr, int to) {
G[fr].push_back(to);
rG[to].push_back(fr);
}
};
int const MAXN = 5e5;
scc<MAXN> sc;
int depth[MAXN];
int calc_depth(int v) {
if (depth[v] >= 0)
return depth[v];
int res = -1;
for (auto c : sc.G[v])
res = std::max(res, calc_depth(c));
++res;
return depth[v] = res;
}
struct rolling_hash {
__uint128_t m_, b_, bl_;
int l_;
rolling_hash(int64_t mod, int64_t base, int length)
: m_(mod), b_(base), l_(length) {
bl_ = 1;
__uint128_t v = b_;
while (length > 0) {
if (length & 1)
(bl_ *= v) %= m_;
length /= 2;
(v *= v) %= m_;
}
}
int64_t hash(char const *s) {
__uint128_t res = 0;
for (int i = 0; i < l_; ++i)
(res = res * b_ % m_ + s[i]) %= m_;
return res;
}
int64_t next(int64_t hash, char in_c, char out_c) {
__uint128_t h = hash;
(h = h * b_ % m_ + in_c + (m_ - out_c) * bl_ % m_) %= m_;
return h;
}
};
int main() {
std::string S, T;
std::cin >> S >> T;
int N = S.size(), M = T.size(), K = N + M - 1;
std::string U = S + S.substr(0, M - 1);
// rolling_hash rh1((int64_t)1e18+3, (int64_t)1e9+7, M);
rolling_hash rh1((int64_t)999999937, (int64_t)17, M);
int64_t t1 = rh1.hash(T.c_str());
int64_t u1 = rh1.hash(U.c_str());
std::vector<int> v;
for (int i = 0; i < N; ++i) {
if (t1 == u1)
v.push_back(i);
if (M + i >= K)
break;
u1 = rh1.next(u1, U[M + i], U[i]);
}
if (v.size() < 1)
fin(0);
for (int fr : v) {
int to = (fr + M) % N;
if (fr == to)
fin(-1);
sc.add_edge(fr, to);
}
if (sc.build(N) < N)
fin(-1);
std::fill_n(depth, N, -1);
int ans = 0;
for (int i = 0; i < N; ++i) {
ans = std::max(ans, calc_depth(i));
}
fin(ans);
return 0;
}
| #include <algorithm>
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
#include <vector>
template <typename T> void fin(T const &t) {
std::cout << t << std::endl;
exit(0);
}
template <int MAXV> struct scc {
std::vector<int> G[MAXV], rG[MAXV], vs;
bool used[MAXV];
// int cmp[MAXV];
scc() {}
void dfs(int v) {
used[v] = true;
for (auto c : G[v])
if (!used[c])
dfs(c);
vs.push_back(v);
}
void rdfs(int v, int k) {
used[v] = true;
// cmp[v] = k;
for (auto c : rG[v])
if (!used[c])
rdfs(c, k);
}
int build(int V) {
std::fill_n(used, V, false);
vs.clear();
for (int v = 0; v < V; ++v)
if (!used[v])
dfs(v);
std::fill_n(used, V, false);
int k = 0;
for (int i = vs.size() - 1; i >= 0; --i) {
if (!used[vs[i]])
rdfs(vs[i], k++);
}
return k;
}
void add_edge(int fr, int to) {
G[fr].push_back(to);
rG[to].push_back(fr);
}
};
int const MAXN = 5e5;
scc<MAXN> sc;
int depth[MAXN];
int calc_depth(int v) {
if (depth[v] >= 0)
return depth[v];
int res = -1;
for (auto c : sc.G[v])
res = std::max(res, calc_depth(c));
++res;
return depth[v] = res;
}
struct rolling_hash {
__uint128_t m_, b_, bl_;
int l_;
rolling_hash(int64_t mod, int64_t base, int length)
: m_(mod), b_(base), l_(length) {
bl_ = 1;
__uint128_t v = b_;
while (length > 0) {
if (length & 1)
(bl_ *= v) %= m_;
length /= 2;
(v *= v) %= m_;
}
}
int64_t hash(char const *s) {
__uint128_t res = 0;
for (int i = 0; i < l_; ++i)
(res = res * b_ % m_ + s[i]) %= m_;
return res;
}
int64_t next(int64_t hash, char in_c, char out_c) {
__uint128_t h = hash;
(h = h * b_ % m_ + in_c + (m_ - out_c) * bl_ % m_) %= m_;
return h;
}
};
int main() {
std::string S, T;
std::cin >> S >> T;
int N = S.size(), M = T.size(), K = N + M - 1;
std::string U = S;
for (int i = 0; i < (M - 1) / N; ++i)
U += S;
U += S.substr(0, K - U.size());
// rolling_hash rh1((int64_t)1e18+3, (int64_t)1e9+7, M);
rolling_hash rh1((int64_t)999999937, (int64_t)17, M);
int64_t t1 = rh1.hash(T.c_str());
int64_t u1 = rh1.hash(U.c_str());
std::vector<int> v;
for (int i = 0; i < N; ++i) {
if (t1 == u1)
v.push_back(i);
if (M + i >= K)
break;
u1 = rh1.next(u1, U[M + i], U[i]);
}
if (v.size() < 1)
fin(0);
for (int fr : v) {
int to = (fr + M) % N;
if (fr == to)
fin(-1);
sc.add_edge(fr, to);
}
if (sc.build(N) < N)
fin(-1);
std::fill_n(depth, N, -1);
int ans = 0;
for (int i = 0; i < N; ++i) {
ans = std::max(ans, calc_depth(i));
}
fin(ans);
return 0;
}
| replace | 96 | 97 | 96 | 100 | 0 | |
p02962 | C++ | Runtime Error | #include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
#define REP(i, n) for (ll i = 0; i < n; i++)
class RollingHash {
private:
using ull = unsigned long long;
const ull base1;
const ull base2;
const ull mod1;
const ull mod2;
vector<ull> hash1;
vector<ull> hash2;
vector<ull> pow1;
vector<ull> pow2;
string s;
public:
RollingHash(string &s)
: s(s), base1(1009), base2(2009), mod1(1000000007), mod2(1000000009),
hash1(s.size() + 1), hash2(s.size() + 1), pow1(s.size() + 1, 1),
pow2(s.size() + 1, 1) {
for (int i = 0; i < s.size(); i++) {
hash1[i + 1] = (hash1[i] * base1 + s[i]) % mod1;
hash2[i + 1] = (hash2[i] * base2 + s[i]) % mod2;
pow1[i + 1] = (pow1[i] * base1) % mod1;
pow2[i + 1] = (pow2[i] * base2) % mod2;
}
}
// s の [l, r) のハッシュ値を返す
ull get(int l, int r) {
return (hash1[r] - ((hash1[l] * pow1[r - l]) % mod1) + mod1) % mod1;
}
ull get() { return get(0, s.size()); }
ull operator()() { return get(0, s.size()); }
ull operator()(int l, int r) { return get(l, r); }
};
int main() {
string s, t;
cin >> s >> t;
ll n = s.size(), m = t.size();
string ss;
ll tmp = ((m % n == 0) ? m / n : m / n + 1);
REP(i, max(tmp, 2LL)) { ss += s; }
RollingHash roli1(ss), roli2(t);
vector<bool> match(n);
REP(i, n) {
if (roli1(i, i + m) == roli2()) {
match[i] = true;
}
}
vector<ll> d(n);
set<ll> st;
auto f = [dfs = [&](auto &&dfs, ll now) -> ll {
if (st.find(now) != st.end()) {
cout << -1 << endl;
exit(0);
}
if (d[now] != 0) {
return d[now];
}
st.insert(now);
if (match[now]) {
d[now] = dfs(dfs, (now + m) % n) + 1;
}
st.erase(now);
return d[now];
}](ll start) { dfs(dfs, start); };
REP(i, n) { f(i); }
cout << *max_element(d.begin(), d.end()) << endl;
} | #include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
#define REP(i, n) for (ll i = 0; i < n; i++)
class RollingHash {
private:
using ull = unsigned long long;
const ull base1;
const ull base2;
const ull mod1;
const ull mod2;
vector<ull> hash1;
vector<ull> hash2;
vector<ull> pow1;
vector<ull> pow2;
string s;
public:
RollingHash(string &s)
: s(s), base1(1009), base2(2009), mod1(1000000007), mod2(1000000009),
hash1(s.size() + 1), hash2(s.size() + 1), pow1(s.size() + 1, 1),
pow2(s.size() + 1, 1) {
for (int i = 0; i < s.size(); i++) {
hash1[i + 1] = (hash1[i] * base1 + s[i]) % mod1;
hash2[i + 1] = (hash2[i] * base2 + s[i]) % mod2;
pow1[i + 1] = (pow1[i] * base1) % mod1;
pow2[i + 1] = (pow2[i] * base2) % mod2;
}
}
// s の [l, r) のハッシュ値を返す
ull get(int l, int r) {
return (hash1[r] - ((hash1[l] * pow1[r - l]) % mod1) + mod1) % mod1;
}
ull get() { return get(0, s.size()); }
ull operator()() { return get(0, s.size()); }
ull operator()(int l, int r) { return get(l, r); }
};
int main() {
string s, t;
cin >> s >> t;
ll n = s.size(), m = t.size();
string ss;
ll tmp = ((m % n == 0) ? m / n : m / n + 1);
REP(i, max(tmp, 50LL)) { ss += s; }
RollingHash roli1(ss), roli2(t);
vector<bool> match(n);
REP(i, n) {
if (roli1(i, i + m) == roli2()) {
match[i] = true;
}
}
vector<ll> d(n);
set<ll> st;
auto f = [dfs = [&](auto &&dfs, ll now) -> ll {
if (st.find(now) != st.end()) {
cout << -1 << endl;
exit(0);
}
if (d[now] != 0) {
return d[now];
}
st.insert(now);
if (match[now]) {
d[now] = dfs(dfs, (now + m) % n) + 1;
}
st.erase(now);
return d[now];
}](ll start) { dfs(dfs, start); };
REP(i, n) { f(i); }
cout << *max_element(d.begin(), d.end()) << endl;
} | replace | 75 | 76 | 75 | 76 | 0 | |
p02962 | Python | Time Limit Exceeded | import sys
sys.setrecursionlimit(10**6)
def rolling_hash(s, w, MOD):
ret = []
tmp = 0
p = pow(26, w, MOD)
ords = [ord(c) - 97 for c in s]
for i, o in enumerate(ords):
tmp = tmp * 26 + o
if i >= w:
tmp = tmp - ords[i - w] * p
tmp %= MOD
ret.append(tmp)
return ret
def solve(s, t):
MOD = 10**9 + 7
ls, lt = len(s), len(t)
k = (lt - 1) // ls + 1
s *= k * 2
ls *= k
rs, rt = rolling_hash(s, lt, MOD), rolling_hash(t, lt, MOD)
rs = rs[ls:]
ht = rt[-1]
checked = [-1] * ls
def series(i, st):
if i == st:
return float("-inf")
if checked[i] == -1:
checked[i] = series((i + lt) % ls, st) + 1 if rs[i] == ht else 0
return checked[i]
for i, hs in enumerate(rs):
if hs != ht:
continue
ret = series((i + lt) % ls, i)
if ret == float("-inf"):
return -1
checked[i] = ret + 1
return max(0, max(checked))
s = input()
t = input()
print(solve(s, t))
| import sys
sys.setrecursionlimit(10**6)
def rolling_hash(s, w, MOD):
ret = []
tmp = 0
p = pow(26, w, MOD)
ords = [ord(c) - 97 for c in s]
for i, o in enumerate(ords):
tmp = tmp * 26 + o
if i >= w:
tmp = tmp - ords[i - w] * p
tmp %= MOD
ret.append(tmp)
return ret
def solve(s, t):
MOD = 10**9 + 7
ls, lt = len(s), len(t)
k = (lt - 1) // ls + 1
s *= k * 2
ls *= k
rs, rt = rolling_hash(s, lt, MOD), rolling_hash(t, lt, MOD)
rs = rs[ls:]
ht = rt[-1]
checked = [-1] * ls
def series(i, st):
if st <= i < st + lt:
return float("-inf")
if checked[i] == -1:
checked[i] = series((i + lt) % ls, st) + 1 if rs[i] == ht else 0
return checked[i]
for i, hs in enumerate(rs):
if hs != ht:
continue
ret = series((i + lt) % ls, i)
if ret == float("-inf"):
return -1
checked[i] = ret + 1
return max(0, max(checked))
s = input()
t = input()
print(solve(s, t))
| replace | 32 | 33 | 32 | 33 | TLE | |
p02962 | Python | Time Limit Exceeded | s = input()
t = input()
u = ""
while len(u) < len(s) + len(t):
u = s * (len(t) // len(s) + 1)
t_add = t + "."
def partial_match_table(word):
table = [0] * (len(word) + 1)
table[0] = -1
i, j = 0, 1
while j < len(word):
matched = word[i] == word[j]
if not matched and i > 0:
i = table[i]
else:
if matched:
i += 1
j += 1
table[j] = i
return table
table_tx = partial_match_table(t_add)
match = [0] * len(s)
i = j = 0
while i < len(u) and j < len(t_add):
if u[i] == t_add[j]:
i += 1
j += 1
elif j == 0:
i += 1
else:
j = table_tx[j]
if j == len(t_add) - 1:
match[(i - j) % len(s)] = 1
# search
searched = [0] * len(s)
longest = 0
infinity = False
for i in range(len(s)):
if searched[i]:
continue
searched[i] = 1
p = i
right = 0
while match[p]:
right += 1
p = (p + len(t)) % len(s)
searched[p] = 1
if p == i:
infinity = True
break
left = 0
p = i
while match[(p - len(t)) % len(s)]:
left += 1
p = (p - len(t)) % len(s)
searched[p] = 1
if p == i:
infinity = True
break
if left + right > longest:
longest = left + right
if infinity:
print(-1)
else:
print(longest)
| s = input()
t = input()
u = s * (len(t) // len(s) + 2)
t_add = t + "."
def partial_match_table(word):
table = [0] * (len(word) + 1)
table[0] = -1
i, j = 0, 1
while j < len(word):
matched = word[i] == word[j]
if not matched and i > 0:
i = table[i]
else:
if matched:
i += 1
j += 1
table[j] = i
return table
table_tx = partial_match_table(t_add)
match = [0] * len(s)
i = j = 0
while i < len(u) and j < len(t_add):
if u[i] == t_add[j]:
i += 1
j += 1
elif j == 0:
i += 1
else:
j = table_tx[j]
if j == len(t_add) - 1:
match[(i - j) % len(s)] = 1
# search
searched = [0] * len(s)
longest = 0
infinity = False
for i in range(len(s)):
if searched[i]:
continue
searched[i] = 1
p = i
right = 0
while match[p]:
right += 1
p = (p + len(t)) % len(s)
searched[p] = 1
if p == i:
infinity = True
break
left = 0
p = i
while match[(p - len(t)) % len(s)]:
left += 1
p = (p - len(t)) % len(s)
searched[p] = 1
if p == i:
infinity = True
break
if left + right > longest:
longest = left + right
if infinity:
print(-1)
else:
print(longest)
| replace | 3 | 6 | 3 | 4 | TLE | |
p02962 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
#define all(x) (x).begin(), (x).end()
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
typedef long long ll;
const long long INF = 1LL << 60;
typedef pair<int, int> P;
typedef pair<ll, ll> p;
typedef vector<int> vec;
using Graph = vector<vector<int>>;
using graph = vector<vector<ll>>;
const int inf = 1e9 + 7;
const long long MOD = 1000000007;
// ローリングハッシュ
// 二分探索で LCP を求める機能つき
struct RollingHash {
static const int base1 = 1007, base2 = 2009;
static const int mod1 = 1000000007, mod2 = 1000000009;
vector<long long> hash1, hash2, power1, power2;
// construct
RollingHash(const string &S) {
int n = (int)S.size();
hash1.assign(n + 1, 0);
hash2.assign(n + 1, 0);
power1.assign(n + 1, 1);
power2.assign(n + 1, 1);
for (int i = 0; i < n; ++i) {
hash1[i + 1] = (hash1[i] * base1 + S[i]) % mod1;
hash2[i + 1] = (hash2[i] * base2 + S[i]) % mod2;
power1[i + 1] = (power1[i] * base1) % mod1;
power2[i + 1] = (power2[i] * base2) % mod2;
}
}
// get hash of S[left:right]
inline pair<long long, long long> get(int l, int r) const {
long long res1 = hash1[r] - hash1[l] * power1[r - l] % mod1;
if (res1 < 0)
res1 += mod1;
long long res2 = hash2[r] - hash2[l] * power2[r - l] % mod2;
if (res2 < 0)
res2 += mod2;
return {res1, res2};
}
};
int main() {
string s, t;
vec v;
cin >> s >> t;
while (s.size() < t.size()) {
s += s;
}
int lent = t.size();
int lens = s.size();
s += s + s;
RollingHash rhs(s), rht(t);
p Thash = rht.get(0, lent);
rep(i, lens) {
if (rhs.get(i, i + lent) == Thash) {
v.push_back(i);
}
}
if (v.empty())
cout << 0 << endl;
else {
map<int, int> ma;
int i = 0;
rep(i, v.size()) { ma[v[i]] = 1; }
int CNT = 0;
int a = v[0];
while (CNT <= 500000) {
a += lent;
a %= lens;
if (ma.count(a))
CNT++;
else
break;
}
if (CNT == 500001)
cout << -1 << endl;
else {
map<int, int> MA;
int ans = 1;
int cnt = 1;
rep(i, v.size()) {
int q = v[i];
MA[q] = 1;
while (ma.count(q)) {
cnt++;
q += lent;
q %= lens;
MA[q] = 1;
}
ans = max(ans, cnt - 1);
cnt = 1;
}
cout << ans << endl;
}
}
return 0;
} | #include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
#define all(x) (x).begin(), (x).end()
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
typedef long long ll;
const long long INF = 1LL << 60;
typedef pair<int, int> P;
typedef pair<ll, ll> p;
typedef vector<int> vec;
using Graph = vector<vector<int>>;
using graph = vector<vector<ll>>;
const int inf = 1e9 + 7;
const long long MOD = 1000000007;
// ローリングハッシュ
// 二分探索で LCP を求める機能つき
struct RollingHash {
static const int base1 = 1007, base2 = 2009;
static const int mod1 = 1000000007, mod2 = 1000000009;
vector<long long> hash1, hash2, power1, power2;
// construct
RollingHash(const string &S) {
int n = (int)S.size();
hash1.assign(n + 1, 0);
hash2.assign(n + 1, 0);
power1.assign(n + 1, 1);
power2.assign(n + 1, 1);
for (int i = 0; i < n; ++i) {
hash1[i + 1] = (hash1[i] * base1 + S[i]) % mod1;
hash2[i + 1] = (hash2[i] * base2 + S[i]) % mod2;
power1[i + 1] = (power1[i] * base1) % mod1;
power2[i + 1] = (power2[i] * base2) % mod2;
}
}
// get hash of S[left:right]
inline pair<long long, long long> get(int l, int r) const {
long long res1 = hash1[r] - hash1[l] * power1[r - l] % mod1;
if (res1 < 0)
res1 += mod1;
long long res2 = hash2[r] - hash2[l] * power2[r - l] % mod2;
if (res2 < 0)
res2 += mod2;
return {res1, res2};
}
};
int main() {
string s, t;
vec v;
cin >> s >> t;
while (s.size() < t.size()) {
s += s;
}
int lent = t.size();
int lens = s.size();
s += s + s;
RollingHash rhs(s), rht(t);
p Thash = rht.get(0, lent);
rep(i, lens) {
if (rhs.get(i, i + lent) == Thash) {
v.push_back(i);
}
}
if (v.empty())
cout << 0 << endl;
else {
map<int, int> ma;
int i = 0;
rep(i, v.size()) { ma[v[i]] = 1; }
int CNT = 0;
int a = v[0];
while (CNT <= 500000) {
a += lent;
a %= lens;
if (ma.count(a))
CNT++;
else
break;
}
if (CNT == 500001)
cout << -1 << endl;
else {
map<int, int> MA;
int ans = 1;
int cnt = 1;
rep(i, v.size()) {
int q = v[i];
if (MA.count(q))
continue;
MA[q] = 1;
while (ma.count(q)) {
cnt++;
q += lent;
q %= lens;
MA[q] = 1;
}
ans = max(ans, cnt - 1);
cnt = 1;
}
cout << ans << endl;
}
}
return 0;
} | replace | 118 | 119 | 118 | 120 | TLE | |
p02962 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
// const ll mod = 1000000007;
template <class Z> Z rng(Z a, Z b) {
static mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
return uniform_int_distribution<Z>(a, b - 1)(mt);
}
struct RollingHash {
static constexpr uint64_t P0 = 4e9 + 7;
static constexpr uint64_t P1 = 4e9 + 9;
static uint64_t B0;
static uint64_t B1;
static vector<uint64_t> powB0;
static vector<uint64_t> powB1;
const int n;
vector<uint64_t> h0;
vector<uint64_t> h1;
template <class Itr>
RollingHash(Itr first, Itr last)
: n(distance(first, last)), h0(n + 1), h1(n + 1) {
for (int i = 0; i < n; ++i, ++first) {
h0[i + 1] = (h0[i] * B0 + *first) % P0;
h1[i + 1] = (h1[i] * B1 + *first) % P1;
}
while (powB0.size() <= n) {
powB0.push_back(powB0.back() * B0 % P0);
powB1.push_back(powB1.back() * B1 % P1);
}
}
uint64_t get0(int l, int r) {
return (h0[r] + (P0 - h0[l]) * powB0[r - l]) % P0;
}
uint64_t get1(int l, int r) {
return (h1[r] + (P1 - h1[l]) * powB1[r - l]) % P1;
}
pair<uint64_t, uint64_t> get(int l, int r) {
return {get0(l, r), get1(l, r)};
}
bool eq(int l0, int r0, int l1, int r1) {
return get0(l0, r0) == get0(l1, r1) and get1(l0, r0) == get1(l1, r1);
}
template <class Itr> static uint64_t get0(Itr first, Itr last) {
uint64_t res = 0;
while (first != last) {
res = (res * B0 + *first++) % P0;
}
return res;
}
template <class Itr> static uint64_t get1(Itr first, Itr last) {
uint64_t res = 0;
while (first != last) {
res = (res * B1 + *first++) % P1;
}
return res;
}
};
uint64_t RollingHash::B0 = rng<unsigned>(1, RollingHash::P0);
uint64_t RollingHash::B1 = rng<unsigned>(1, RollingHash::P1);
vector<uint64_t> RollingHash::powB0{1};
vector<uint64_t> RollingHash::powB1{1};
string t, s;
ll dp[2][505000];
int main() {
// cout.precision(10);
cin >> s >> t;
RollingHash shash(s.begin(), s.end()), thash(t.begin(), t.end());
for (int i = 0; i <= 5e5; i++) {
dp[0][i] = dp[1][i] = INF;
}
queue<i_i> que;
for (int i = 0; i < s.size(); i++) {
ll len = s.size() - i;
chmin(len, (ll)t.size());
if (shash.get(i, i + len) != thash.get(0, len)) {
dp[0][i] = 0;
que.push({0, i});
}
}
for (int i = 0; i < t.size(); i++) {
ll len = t.size() - i;
chmin(len, (ll)s.size());
if (thash.get(i, i + len) != shash.get(0, len)) {
dp[1][i] = -INF;
que.push({1, i});
}
}
while (!que.empty()) {
i_i now = que.front();
cerr << now.first << " " << now.second << " " << dp[now.first][now.second]
<< endl;
que.pop();
if (now.first == 0) {
if (now.second >= t.size()) {
if (chmin(dp[0][now.second - t.size()], dp[0][now.second] + 1)) {
que.push({0, now.second - t.size()});
}
continue;
} else {
if (chmin(dp[1][t.size() - now.second], dp[0][now.second])) {
que.push({1, t.size() - now.second});
}
}
}
if (now.first == 1) {
if (now.second > s.size()) {
if (chmin(dp[1][now.second - s.size()], dp[1][now.second])) {
que.push({1, now.second - s.size()});
}
continue;
} else {
if (chmin(dp[0][s.size() - now.second], dp[1][now.second] + 1)) {
chmax(dp[0][s.size() - now.second], (ll)0);
que.push({0, s.size() - now.second});
}
}
}
}
ll ans = 0;
for (int i = 0; i < s.size(); i++) {
chmax(ans, dp[0][i]);
}
if (ans == INF)
ans = -1;
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
// const ll mod = 1000000007;
template <class Z> Z rng(Z a, Z b) {
static mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
return uniform_int_distribution<Z>(a, b - 1)(mt);
}
struct RollingHash {
static constexpr uint64_t P0 = 4e9 + 7;
static constexpr uint64_t P1 = 4e9 + 9;
static uint64_t B0;
static uint64_t B1;
static vector<uint64_t> powB0;
static vector<uint64_t> powB1;
const int n;
vector<uint64_t> h0;
vector<uint64_t> h1;
template <class Itr>
RollingHash(Itr first, Itr last)
: n(distance(first, last)), h0(n + 1), h1(n + 1) {
for (int i = 0; i < n; ++i, ++first) {
h0[i + 1] = (h0[i] * B0 + *first) % P0;
h1[i + 1] = (h1[i] * B1 + *first) % P1;
}
while (powB0.size() <= n) {
powB0.push_back(powB0.back() * B0 % P0);
powB1.push_back(powB1.back() * B1 % P1);
}
}
uint64_t get0(int l, int r) {
return (h0[r] + (P0 - h0[l]) * powB0[r - l]) % P0;
}
uint64_t get1(int l, int r) {
return (h1[r] + (P1 - h1[l]) * powB1[r - l]) % P1;
}
pair<uint64_t, uint64_t> get(int l, int r) {
return {get0(l, r), get1(l, r)};
}
bool eq(int l0, int r0, int l1, int r1) {
return get0(l0, r0) == get0(l1, r1) and get1(l0, r0) == get1(l1, r1);
}
template <class Itr> static uint64_t get0(Itr first, Itr last) {
uint64_t res = 0;
while (first != last) {
res = (res * B0 + *first++) % P0;
}
return res;
}
template <class Itr> static uint64_t get1(Itr first, Itr last) {
uint64_t res = 0;
while (first != last) {
res = (res * B1 + *first++) % P1;
}
return res;
}
};
uint64_t RollingHash::B0 = rng<unsigned>(1, RollingHash::P0);
uint64_t RollingHash::B1 = rng<unsigned>(1, RollingHash::P1);
vector<uint64_t> RollingHash::powB0{1};
vector<uint64_t> RollingHash::powB1{1};
string t, s;
ll dp[2][505000];
int main() {
// cout.precision(10);
cin >> s >> t;
RollingHash shash(s.begin(), s.end()), thash(t.begin(), t.end());
for (int i = 0; i <= 5e5; i++) {
dp[0][i] = dp[1][i] = INF;
}
queue<i_i> que;
for (int i = 0; i < s.size(); i++) {
ll len = s.size() - i;
chmin(len, (ll)t.size());
if (shash.get(i, i + len) != thash.get(0, len)) {
dp[0][i] = 0;
que.push({0, i});
}
}
for (int i = 0; i < t.size(); i++) {
ll len = t.size() - i;
chmin(len, (ll)s.size());
if (thash.get(i, i + len) != shash.get(0, len)) {
dp[1][i] = -INF;
que.push({1, i});
}
}
while (!que.empty()) {
i_i now = que.front();
// cerr << now.first << " " << now.second << " " <<
// dp[now.first][now.second] << endl;
que.pop();
if (now.first == 0) {
if (now.second >= t.size()) {
if (chmin(dp[0][now.second - t.size()], dp[0][now.second] + 1)) {
que.push({0, now.second - t.size()});
}
continue;
} else {
if (chmin(dp[1][t.size() - now.second], dp[0][now.second])) {
que.push({1, t.size() - now.second});
}
}
}
if (now.first == 1) {
if (now.second > s.size()) {
if (chmin(dp[1][now.second - s.size()], dp[1][now.second])) {
que.push({1, now.second - s.size()});
}
continue;
} else {
if (chmin(dp[0][s.size() - now.second], dp[1][now.second] + 1)) {
chmax(dp[0][s.size() - now.second], (ll)0);
que.push({0, s.size() - now.second});
}
}
}
}
ll ans = 0;
for (int i = 0; i < s.size(); i++) {
chmax(ans, dp[0][i]);
}
if (ans == INF)
ans = -1;
cout << ans << endl;
return 0;
}
| replace | 113 | 115 | 113 | 115 | TLE | |
p02962 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD = 1e9 + 7;
int n;
bool used[500001];
int cmp[500001];
vector<int> g[500001], gr[500001];
vector<int> vs;
void dfs(int x) {
used[x] = 1;
for (auto y : g[x]) {
if (!used[y])
dfs(y);
}
vs.push_back(x);
}
void rdfs(int v, int k) {
used[v] = 1;
cmp[v] = k;
for (auto y : gr[v]) {
if (!used[y])
rdfs(y, k);
}
}
int scc() {
fill(used, used + n, 0);
vs.clear();
for (int i = 0; i < n; i++) {
if (!used[i])
dfs(i);
}
fill(used, used + n, 0);
int k = 0;
for (int i = vs.size() - 1; i >= 0; i--) {
if (!used[vs[i]])
rdfs(vs[i], k++);
}
return k;
}
int dp[500010];
int solve(int x) {
if (dp[x] >= 0)
return dp[x];
int ret = 0;
for (auto y : g[x]) {
ret = max(ret, solve(y) + 1);
}
return dp[x] = ret;
}
int main() {
string s, t;
cin >> s >> t;
n = s.size();
int m = t.size();
string s0 = s;
while (s0.size() < t.size() + s.size())
s0 += s;
ll bs[2000020];
bs[0] = 0;
ll b = 67;
ll bp = 1;
for (int i = 0; i < s0.size(); i++) {
bs[i + 1] = (bs[i] + bp * s0[i]) % MOD;
bp = bp * b % MOD;
}
bp = 1;
ll bt = 0;
for (int i = 0; i < m; i++) {
(bt += bp * t[i]) %= MOD;
bp = bp * b % MOD;
}
bp = 1;
int nx[500010];
for (int i = 0; i < n; i++) {
if ((bs[i + m] - bs[i] + MOD) % MOD == bt * bp % MOD) {
g[i].push_back((i + m) % n);
gr[(i + m) % n].push_back(i);
// cout<<i<<" "<<(i+m)%n<<endl;
}
bp = bp * b % MOD;
}
int k = scc();
if (k < n) {
cout << -1 << endl;
return 0;
}
fill(dp, dp + n, -1);
int ans = 0;
for (int i = 0; i < n; i++) {
ans = max(ans, solve(i));
}
cout << ans << endl;
return 0;
} | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD = 1e9 + 7;
int n;
bool used[500001];
int cmp[500001];
vector<int> g[500001], gr[500001];
vector<int> vs;
void dfs(int x) {
used[x] = 1;
for (auto y : g[x]) {
if (!used[y])
dfs(y);
}
vs.push_back(x);
}
void rdfs(int v, int k) {
used[v] = 1;
cmp[v] = k;
for (auto y : gr[v]) {
if (!used[y])
rdfs(y, k);
}
}
int scc() {
fill(used, used + n, 0);
vs.clear();
for (int i = 0; i < n; i++) {
if (!used[i])
dfs(i);
}
fill(used, used + n, 0);
int k = 0;
for (int i = vs.size() - 1; i >= 0; i--) {
if (!used[vs[i]])
rdfs(vs[i], k++);
}
return k;
}
int dp[500010];
int solve(int x) {
if (dp[x] >= 0)
return dp[x];
int ret = 0;
for (auto y : g[x]) {
ret = max(ret, solve(y) + 1);
}
return dp[x] = ret;
}
int main() {
string s, t;
cin >> s >> t;
n = s.size();
int m = t.size();
string s0 = s;
while (s0.size() < t.size() + s.size())
s0 += s;
ll bs[2000020];
bs[0] = 0;
ll b = 67;
ll bp = 1;
for (int i = 0; i < s0.size(); i++) {
bs[i + 1] = (bs[i] + bp * s0[i]) % MOD;
bp = bp * b % MOD;
}
bp = 1;
ll bt = 0;
for (int i = 0; i < m; i++) {
(bt += bp * t[i]) %= MOD;
bp = bp * b % MOD;
}
bp = 1;
int nx[500010];
for (int i = 0; i < n; i++) {
if ((bs[i + m] - bs[i] + MOD) % MOD == bt * bp % MOD) {
if (i == (i + m) % n) {
cout << -1 << endl;
return 0;
}
g[i].push_back((i + m) % n);
gr[(i + m) % n].push_back(i);
// cout<<i<<" "<<(i+m)%n<<endl;
}
bp = bp * b % MOD;
}
int k = scc();
if (k < n) {
cout << -1 << endl;
return 0;
}
fill(dp, dp + n, -1);
int ans = 0;
for (int i = 0; i < n; i++) {
ans = max(ans, solve(i));
}
cout << ans << endl;
return 0;
} | insert | 97 | 97 | 97 | 101 | -11 | |
p02962 | C++ | Time Limit Exceeded | #define DEBUG 0
/**
* File : F.cpp
* Author : Kazune Takahashi
* Created : 7/28/2019, 1:18:49 AM
* Powered by Visual Studio Code
*/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
using ll = long long;
class mint {
public:
static ll MOD;
ll x;
mint() : x(0) {}
mint(ll x) : x(x % MOD) {}
mint operator-() const { return x ? MOD - x : 0; }
mint &operator+=(const mint &a) {
if ((x += a.x) >= MOD) {
x -= MOD;
}
return *this;
}
mint &operator-=(const mint &a) { return *this += -a; }
mint &operator*=(const mint &a) {
(x *= a.x) %= MOD;
return *this;
}
mint &operator/=(const mint &a) {
mint b{a};
return *this *= b.power(MOD - 2);
}
mint operator+(const mint &a) const { return mint(*this) += a; }
mint operator-(const mint &a) const { return mint(*this) -= a; }
mint operator*(const mint &a) const { return mint(*this) *= a; }
mint operator/(const mint &a) const { return mint(*this) /= a; }
bool operator<(const mint &a) const { return x < a.x; }
bool operator==(const mint &a) const { return x == a.x; }
const mint power(ll N) {
if (N == 0) {
return 1;
} else if (N % 2 == 1) {
return *this * power(N - 1);
} else {
mint half = power(N / 2);
return half * half;
}
}
};
ll mint::MOD = 1e9 + 7;
istream &operator>>(istream &stream, mint &a) { return stream >> a.x; }
ostream &operator<<(ostream &stream, const mint &a) { return stream << a.x; }
class combination {
public:
vector<mint> inv, fact, factinv;
static int MAX_SIZE;
combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE) {
inv[1] = 1;
for (auto i = 2; i < MAX_SIZE; i++) {
inv[i] = (-inv[mint::MOD % i]) * (mint::MOD / i);
}
fact[0] = factinv[0] = 1;
for (auto i = 1; i < MAX_SIZE; i++) {
fact[i] = mint(i) * fact[i - 1];
factinv[i] = inv[i] * factinv[i - 1];
}
}
mint operator()(int n, int k) {
if (n >= 0 && k >= 0 && n - k >= 0) {
return fact[n] * factinv[k] * factinv[n - k];
}
return 0;
}
};
int combination::MAX_SIZE = 3000010;
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
// constexpr double epsilon = 1e-10;
// constexpr ll infty = 1000000000000000LL;
// constexpr int dx[4] = {1, 0, -1, 0};
// constexpr int dy[4] = {0, 1, 0, -1};
void Yes() {
cout << "Yes" << endl;
exit(0);
}
void No() {
cout << "-1" << endl;
exit(0);
}
class UnionFind {
vector<long long> par;
public:
UnionFind() {}
UnionFind(int n) : par(n, -1) {}
bool is_same(int x, int y) { return root(x) == root(y); }
bool merge(int x, int y) {
x = root(x);
y = root(y);
if (x == y) {
return false;
}
if (par[x] > par[y]) {
swap(x, y);
}
par[x] += par[y];
par[y] = x;
return true;
}
long long size(int x) { return -par[root(x)]; }
private:
int root(int x) {
if (par[x] < 0) {
return x;
}
return par[x] = root(par[x]);
}
};
class RollingHash {
string S;
static mint B;
vector<mint> H;
public:
RollingHash(string s, int t = -1) : S{s} {
if (t == -1) {
t = s.size();
}
assert(1 <= t && t <= static_cast<int>(s.size()));
H = make_init_hash(t);
}
RollingHash(string s, size_t t) : RollingHash{s, static_cast<int>(t)} {}
size_t size() { return S.size() + 1u - H.size(); }
const mint operator[](size_t t) const { return H[t]; }
private:
mint Sm(size_t k) { return static_cast<mint>(S[k]); }
vector<mint> make_init_hash(int);
};
mint RollingHash::B = 1234567;
vector<mint> RollingHash::make_init_hash(int t) {
vector<mint> res(S.size() + 1 - t);
mint now = 0;
const mint pb = B.power(t);
for (auto i = 0; i < t; i++) {
now = now * B + Sm(i);
}
res[0] = now;
for (auto i = 0u; i < res.size() - 1; i++) {
res[i + 1] = res[i] * B - Sm(i) * pb + Sm(i + t);
}
return res;
}
int main() {
string S, T;
cin >> S >> T;
int N = S.size();
stringstream U{};
U << S << S << S;
while (U.str().size() < T.size() * 2) {
U << S;
}
S = U.str();
#if DEBUG == 1
cerr << "S = " << S << endl;
#endif
vector<bool> ok(N, false);
RollingHash rh_S{S, T.size()};
RollingHash rh_T{T};
const mint base = rh_T[0];
#if DEBUG == 1
cerr << "rh_S.size() = " << rh_S.size() << endl;
cerr << "rh_T.size() = " << rh_T.size() << endl;
cerr << "base = " << base << endl;
#endif
for (auto i = 0; i < N; i++) {
ok[i] = (rh_S[i] == base);
#if DEBUG == 1
cerr << "rh_S[" << i << "] = " << rh_S[i] << endl;
if (ok[i]) {
cerr << "ok[" << i << "]" << endl;
}
#endif
}
UnionFind uf{N};
for (auto i = 0; i < N; i++) {
int j = (i + T.size()) % N;
if (ok[i] && ok[j]) {
if (!uf.merge(i, j)) {
No();
}
}
}
ll ans = 0;
for (auto i = 0; i < N; i++) {
if (ok[i]) {
maxs(ans, uf.size(i));
}
}
cout << ans << endl;
} | #define DEBUG 0
/**
* File : F.cpp
* Author : Kazune Takahashi
* Created : 7/28/2019, 1:18:49 AM
* Powered by Visual Studio Code
*/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
using ll = long long;
class mint {
public:
static ll MOD;
ll x;
mint() : x(0) {}
mint(ll x) : x(x % MOD) {}
mint operator-() const { return x ? MOD - x : 0; }
mint &operator+=(const mint &a) {
if ((x += a.x) >= MOD) {
x -= MOD;
}
return *this;
}
mint &operator-=(const mint &a) { return *this += -a; }
mint &operator*=(const mint &a) {
(x *= a.x) %= MOD;
return *this;
}
mint &operator/=(const mint &a) {
mint b{a};
return *this *= b.power(MOD - 2);
}
mint operator+(const mint &a) const { return mint(*this) += a; }
mint operator-(const mint &a) const { return mint(*this) -= a; }
mint operator*(const mint &a) const { return mint(*this) *= a; }
mint operator/(const mint &a) const { return mint(*this) /= a; }
bool operator<(const mint &a) const { return x < a.x; }
bool operator==(const mint &a) const { return x == a.x; }
const mint power(ll N) {
if (N == 0) {
return 1;
} else if (N % 2 == 1) {
return *this * power(N - 1);
} else {
mint half = power(N / 2);
return half * half;
}
}
};
ll mint::MOD = 1e9 + 7;
istream &operator>>(istream &stream, mint &a) { return stream >> a.x; }
ostream &operator<<(ostream &stream, const mint &a) { return stream << a.x; }
class combination {
public:
vector<mint> inv, fact, factinv;
static int MAX_SIZE;
combination() : inv(MAX_SIZE), fact(MAX_SIZE), factinv(MAX_SIZE) {
inv[1] = 1;
for (auto i = 2; i < MAX_SIZE; i++) {
inv[i] = (-inv[mint::MOD % i]) * (mint::MOD / i);
}
fact[0] = factinv[0] = 1;
for (auto i = 1; i < MAX_SIZE; i++) {
fact[i] = mint(i) * fact[i - 1];
factinv[i] = inv[i] * factinv[i - 1];
}
}
mint operator()(int n, int k) {
if (n >= 0 && k >= 0 && n - k >= 0) {
return fact[n] * factinv[k] * factinv[n - k];
}
return 0;
}
};
int combination::MAX_SIZE = 3000010;
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
// constexpr double epsilon = 1e-10;
// constexpr ll infty = 1000000000000000LL;
// constexpr int dx[4] = {1, 0, -1, 0};
// constexpr int dy[4] = {0, 1, 0, -1};
void Yes() {
cout << "Yes" << endl;
exit(0);
}
void No() {
cout << "-1" << endl;
exit(0);
}
class UnionFind {
vector<long long> par;
public:
UnionFind() {}
UnionFind(int n) : par(n, -1) {}
bool is_same(int x, int y) { return root(x) == root(y); }
bool merge(int x, int y) {
x = root(x);
y = root(y);
if (x == y) {
return false;
}
if (par[x] > par[y]) {
swap(x, y);
}
par[x] += par[y];
par[y] = x;
return true;
}
long long size(int x) { return -par[root(x)]; }
private:
int root(int x) {
if (par[x] < 0) {
return x;
}
return par[x] = root(par[x]);
}
};
class RollingHash {
string S;
static mint B;
vector<mint> H;
public:
RollingHash(string s, int t = -1) : S{s} {
if (t == -1) {
t = s.size();
}
assert(1 <= t && t <= static_cast<int>(s.size()));
H = make_init_hash(t);
}
RollingHash(string s, size_t t) : RollingHash{s, static_cast<int>(t)} {}
size_t size() { return S.size() + 1u - H.size(); }
const mint operator[](size_t t) const { return H[t]; }
private:
mint Sm(size_t k) { return static_cast<mint>(S[k]); }
vector<mint> make_init_hash(int);
};
mint RollingHash::B = 1234567;
vector<mint> RollingHash::make_init_hash(int t) {
vector<mint> res(S.size() + 1 - t);
mint now = 0;
const mint pb = B.power(t);
for (auto i = 0; i < t; i++) {
now = now * B + Sm(i);
}
res[0] = now;
for (auto i = 0u; i < res.size() - 1; i++) {
res[i + 1] = res[i] * B - Sm(i) * pb + Sm(i + t);
}
return res;
}
int main() {
string S, T;
cin >> S >> T;
int N = S.size();
stringstream U{};
U << S << S << S;
for (auto i = 0; i < 2 * T.size() / N; i++) {
U << S;
}
S = U.str();
#if DEBUG == 1
cerr << "S = " << S << endl;
#endif
vector<bool> ok(N, false);
RollingHash rh_S{S, T.size()};
RollingHash rh_T{T};
const mint base = rh_T[0];
#if DEBUG == 1
cerr << "rh_S.size() = " << rh_S.size() << endl;
cerr << "rh_T.size() = " << rh_T.size() << endl;
cerr << "base = " << base << endl;
#endif
for (auto i = 0; i < N; i++) {
ok[i] = (rh_S[i] == base);
#if DEBUG == 1
cerr << "rh_S[" << i << "] = " << rh_S[i] << endl;
if (ok[i]) {
cerr << "ok[" << i << "]" << endl;
}
#endif
}
UnionFind uf{N};
for (auto i = 0; i < N; i++) {
int j = (i + T.size()) % N;
if (ok[i] && ok[j]) {
if (!uf.merge(i, j)) {
No();
}
}
}
ll ans = 0;
for (auto i = 0; i < N; i++) {
if (ok[i]) {
maxs(ans, uf.size(i));
}
}
cout << ans << endl;
} | replace | 193 | 194 | 193 | 194 | TLE | |
p02962 | C++ | Runtime Error | // 2019.7.27 by ljz
// email 573902690@qq.com
// if you find any bug in my code
// please tell me
#include <bits/stdc++.h>
using namespace std;
#define res register int
#define LL long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f
#define eps 1e-10
#define RG register
#define db double
#define lb long db
#define pc(x) __builtin_popcount(x)
typedef pair<int, int> Pair;
#define mp make_pair
#define fi first
#define se second
#define pi acos(-1.0)
#define pb push_back
#define gc getchar
#define Pi 2.0 * acos(-1.0)
// inline char gc() {
// static char buf[100000],*p1,*p2;
// return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
// }
// inline int read() {
// res s=0,ch=gc();
// while(ch<'0'||ch>'9')ch=gc();
// while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=gc();
// return s;
// }
inline int read() {
res s = 0, ch = gc(), w = 1;
while (ch < '0' || ch > '9') {
if (ch == '-')
w = -1;
ch = gc();
}
while (ch >= '0' && ch <= '9')
s = s * 10 + ch - '0', ch = gc();
return s * w;
}
// inline LL Read() {
// RG LL s=0;
// res ch=gc();
// while(ch<'0'||ch>'9')ch=gc();
// while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=gc();
// return s;
// }
// inline LL Read() {
// RG LL s=0;
// res ch=gc(),w=1;
// while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=gc();}
// while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=gc();
// return s*w;
// }
// inline void write(RG __int128 x){
// if(x>10)write(x/10);
// putchar(x%10+'0');
// }
inline void swap(res &x, res &y) { x ^= y ^= x ^= y; }
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 1e6 + 10;
namespace MAIN {
char S[N], T[N];
int n, m;
int F[N], G[N];
inline void MAIN() {
scanf("%s", S + 1), scanf("%s", T + 1), n = int(strlen(S + 1)),
m = int(strlen(T + 1));
while (n < (m << 1)) {
for (res i = 1; i <= n; i++)
S[i + n] = S[i];
n <<= 1;
}
for (res i = 1; i <= n; i++)
S[i + n] = S[i];
n <<= 1, F[0] = -1;
for (res i = 1; i <= m; i++) {
res j = i - 1;
while (j && T[F[j] + 1] != T[i])
j = F[j];
F[i] = F[j] + 1;
}
res j = 0, ans = 0;
for (res i = 1; i <= n; i++) {
while (j >= 0 && S[i] != T[j + 1])
j = F[j];
if (++j == m)
G[i] = G[i - m] + 1, ans = max(ans, G[i]);
}
printf("%d\n", ans >= n / m - 1 ? -1 : ans);
}
} // namespace MAIN
int main() {
// freopen("compound1.in","r",stdin);
// freopen("compound.out","w",stdout);
MAIN::MAIN();
return 0;
} | // 2019.7.27 by ljz
// email 573902690@qq.com
// if you find any bug in my code
// please tell me
#include <bits/stdc++.h>
using namespace std;
#define res register int
#define LL long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f
#define eps 1e-10
#define RG register
#define db double
#define lb long db
#define pc(x) __builtin_popcount(x)
typedef pair<int, int> Pair;
#define mp make_pair
#define fi first
#define se second
#define pi acos(-1.0)
#define pb push_back
#define gc getchar
#define Pi 2.0 * acos(-1.0)
// inline char gc() {
// static char buf[100000],*p1,*p2;
// return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
// }
// inline int read() {
// res s=0,ch=gc();
// while(ch<'0'||ch>'9')ch=gc();
// while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=gc();
// return s;
// }
inline int read() {
res s = 0, ch = gc(), w = 1;
while (ch < '0' || ch > '9') {
if (ch == '-')
w = -1;
ch = gc();
}
while (ch >= '0' && ch <= '9')
s = s * 10 + ch - '0', ch = gc();
return s * w;
}
// inline LL Read() {
// RG LL s=0;
// res ch=gc();
// while(ch<'0'||ch>'9')ch=gc();
// while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=gc();
// return s;
// }
// inline LL Read() {
// RG LL s=0;
// res ch=gc(),w=1;
// while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=gc();}
// while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=gc();
// return s*w;
// }
// inline void write(RG __int128 x){
// if(x>10)write(x/10);
// putchar(x%10+'0');
// }
inline void swap(res &x, res &y) { x ^= y ^= x ^= y; }
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 1e7 + 10;
namespace MAIN {
char S[N], T[N];
int n, m;
int F[N], G[N];
inline void MAIN() {
scanf("%s", S + 1), scanf("%s", T + 1), n = int(strlen(S + 1)),
m = int(strlen(T + 1));
while (n < (m << 1)) {
for (res i = 1; i <= n; i++)
S[i + n] = S[i];
n <<= 1;
}
for (res i = 1; i <= n; i++)
S[i + n] = S[i];
n <<= 1, F[0] = -1;
for (res i = 1; i <= m; i++) {
res j = i - 1;
while (j && T[F[j] + 1] != T[i])
j = F[j];
F[i] = F[j] + 1;
}
res j = 0, ans = 0;
for (res i = 1; i <= n; i++) {
while (j >= 0 && S[i] != T[j + 1])
j = F[j];
if (++j == m)
G[i] = G[i - m] + 1, ans = max(ans, G[i]);
}
printf("%d\n", ans >= n / m - 1 ? -1 : ans);
}
} // namespace MAIN
int main() {
// freopen("compound1.in","r",stdin);
// freopen("compound.out","w",stdout);
MAIN::MAIN();
return 0;
} | replace | 64 | 65 | 64 | 65 | 0 | |
p02962 | C++ | Runtime Error | #pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx2,tune=native")
// #pragma comment(linker, "/stack:200000000")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
#define X first
#define Y second
// #include <boost/unordered_map.hpp>
// using namespace boost;
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update> rbtree; rbtree T;
*/
using i32 = int;
using i64 = long long;
using u8 = unsigned char;
using u32 = unsigned;
using u64 = unsigned long long;
using f64 = double;
using f80 = long double;
// using i128 = __int128_t;
// using u128 = __uint128_t;
using i128 = i64;
using u128 = u64;
ll power(ll a, ll b, ll p) {
if (!b)
return 1;
ll t = power(a, b / 2, p);
t = t * t % p;
if (b & 1)
t = t * a % p;
return t;
}
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
ll px, py;
ll d = exgcd(b, a % b, px, py);
x = py;
y = px - a / b * py;
return d;
}
template <class T> inline void freshmin(T &a, const T &b) {
if (a > b)
a = b;
}
template <class T> inline void freshmax(T &a, const T &b) {
if (a < b)
a = b;
}
// #define getchar getchar_unlocked
// #define putchar putchar_unlocked
int inp() {
int x = 0, f = 0;
char ch;
for (ch = getchar(); !isdigit(ch); ch = getchar())
if (ch == '-')
f = 1;
for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
;
return f ? -x : x;
}
ll inp_ll() {
ll x = 0;
int f = 0;
char ch;
for (ch = getchar(); !isdigit(ch); ch = getchar())
if (ch == '-')
f = 1;
for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
;
return f ? -x : x;
}
template <class T> bool read(T &x) {
x = 0;
char ch = getchar();
if (ch == EOF)
return 0;
for (; !isdigit(ch);) {
ch = getchar();
if (ch == EOF)
return 0;
}
for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
;
return 1;
}
template <class T> void write(T x) {
static char s[22];
static char *it = s + 20;
static char *end = s + 20;
if (!x)
*--it = '0';
while (x) {
*--it = x % 10 + '0';
x /= 10;
}
for (; it < end; ++it)
putchar(*it);
}
template <class T> void writeln(T x) {
write(x);
putchar('\n');
}
const int MAXN = 5000010;
const int INF = 1000000000;
const int MOD = 1000000007;
int pre[MAXN];
void kmp(char *s, int n) {
for (int i = 2; i <= n; ++i) {
int j = pre[i - 1];
while (j && s[j + 1] != s[i])
j = pre[j];
pre[i] = s[j + 1] == s[i] ? j + 1 : 0;
}
}
int n, m;
char s[MAXN], t[MAXN];
bool u[MAXN], in[MAXN];
int to[MAXN], F[MAXN];
int head, tail, Q[MAXN];
int main() {
scanf("%s", s + 1);
scanf("%s", t + 1);
n = strlen(s + 1);
m = strlen(t + 1);
int multi = m / n + 1;
if ((multi + 1) * n > 4000000) {
puts("caonima");
return 0;
}
for (int k = 1; k <= multi; ++k)
for (int i = 1; i <= n; ++i) {
if (k * n + i > 4000000) {
puts("caonima");
return 0;
}
s[k * n + i] = s[i];
}
int N = (multi + 1) * n;
kmp(t, m);
int cur = 0;
int flag = 0;
for (int i = 1; i <= N; ++i) {
while (cur && s[i] != t[cur + 1])
cur = pre[cur];
if (s[i] == t[cur + 1])
cur++;
if (cur == m) {
flag = 1;
int x = (i - m + 1) % n, y = (i + 1) % n;
if (x == 0)
x = n;
if (y == 0)
y = n;
to[x] = y;
cur = pre[cur];
}
}
if (!flag) {
puts("0");
return 0;
}
for (int i = 1; i <= n; ++i)
if (!u[i]) {
head = tail = 1;
Q[1] = i;
in[i] = 1;
u[i] = 1;
while (1) {
int x = Q[tail];
int y = to[x];
if (!y)
break;
if (u[y]) {
if (in[y]) {
puts("-1");
return 0;
}
break;
}
Q[++tail] = y;
in[y] = 1;
u[y] = 1;
}
for (int j = tail; j >= 1; --j) {
F[Q[j]] = F[to[Q[j]]] + 1;
in[Q[j]] = 0;
}
}
int ans = 0;
for (int i = 1; i <= n; ++i)
freshmax(ans, F[i]);
cout << ans - 1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
#define X first
#define Y second
// #include <boost/unordered_map.hpp>
// using namespace boost;
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update> rbtree; rbtree T;
*/
using i32 = int;
using i64 = long long;
using u8 = unsigned char;
using u32 = unsigned;
using u64 = unsigned long long;
using f64 = double;
using f80 = long double;
// using i128 = __int128_t;
// using u128 = __uint128_t;
using i128 = i64;
using u128 = u64;
ll power(ll a, ll b, ll p) {
if (!b)
return 1;
ll t = power(a, b / 2, p);
t = t * t % p;
if (b & 1)
t = t * a % p;
return t;
}
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
ll px, py;
ll d = exgcd(b, a % b, px, py);
x = py;
y = px - a / b * py;
return d;
}
template <class T> inline void freshmin(T &a, const T &b) {
if (a > b)
a = b;
}
template <class T> inline void freshmax(T &a, const T &b) {
if (a < b)
a = b;
}
// #define getchar getchar_unlocked
// #define putchar putchar_unlocked
int inp() {
int x = 0, f = 0;
char ch;
for (ch = getchar(); !isdigit(ch); ch = getchar())
if (ch == '-')
f = 1;
for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
;
return f ? -x : x;
}
ll inp_ll() {
ll x = 0;
int f = 0;
char ch;
for (ch = getchar(); !isdigit(ch); ch = getchar())
if (ch == '-')
f = 1;
for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
;
return f ? -x : x;
}
template <class T> bool read(T &x) {
x = 0;
char ch = getchar();
if (ch == EOF)
return 0;
for (; !isdigit(ch);) {
ch = getchar();
if (ch == EOF)
return 0;
}
for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar())
;
return 1;
}
template <class T> void write(T x) {
static char s[22];
static char *it = s + 20;
static char *end = s + 20;
if (!x)
*--it = '0';
while (x) {
*--it = x % 10 + '0';
x /= 10;
}
for (; it < end; ++it)
putchar(*it);
}
template <class T> void writeln(T x) {
write(x);
putchar('\n');
}
const int MAXN = 5000010;
const int INF = 1000000000;
const int MOD = 1000000007;
int pre[MAXN];
void kmp(char *s, int n) {
for (int i = 2; i <= n; ++i) {
int j = pre[i - 1];
while (j && s[j + 1] != s[i])
j = pre[j];
pre[i] = s[j + 1] == s[i] ? j + 1 : 0;
}
}
int n, m;
char s[MAXN], t[MAXN];
bool u[MAXN], in[MAXN];
int to[MAXN], F[MAXN];
int head, tail, Q[MAXN];
int main() {
scanf("%s", s + 1);
scanf("%s", t + 1);
n = strlen(s + 1);
m = strlen(t + 1);
int multi = m / n + 1;
if ((multi + 1) * n > 4000000) {
puts("caonima");
return 0;
}
for (int k = 1; k <= multi; ++k)
for (int i = 1; i <= n; ++i) {
if (k * n + i > 4000000) {
puts("caonima");
return 0;
}
s[k * n + i] = s[i];
}
int N = (multi + 1) * n;
kmp(t, m);
int cur = 0;
int flag = 0;
for (int i = 1; i <= N; ++i) {
while (cur && s[i] != t[cur + 1])
cur = pre[cur];
if (s[i] == t[cur + 1])
cur++;
if (cur == m) {
flag = 1;
int x = (i - m + 1) % n, y = (i + 1) % n;
if (x == 0)
x = n;
if (y == 0)
y = n;
to[x] = y;
cur = pre[cur];
}
}
if (!flag) {
puts("0");
return 0;
}
for (int i = 1; i <= n; ++i)
if (!u[i]) {
head = tail = 1;
Q[1] = i;
in[i] = 1;
u[i] = 1;
while (1) {
int x = Q[tail];
int y = to[x];
if (!y)
break;
if (u[y]) {
if (in[y]) {
puts("-1");
return 0;
}
break;
}
Q[++tail] = y;
in[y] = 1;
u[y] = 1;
}
for (int j = tail; j >= 1; --j) {
F[Q[j]] = F[to[Q[j]]] + 1;
in[Q[j]] = 0;
}
}
int ans = 0;
for (int i = 1; i <= n; ++i)
freshmax(ans, F[i]);
cout << ans - 1 << endl;
return 0;
}
| delete | 0 | 3 | 0 | 0 | 127 | /tmp/f032b621-4be6-46de-91bc-bf3b01049ec6.out: error while loading shared libraries: libc.so.6: failed to map segment from shared object
|
p02962 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using lint = long long int;
#define FOR(i, begin, end) \
for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end) \
for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
constexpr int MOD = 1000000007;
int main() {
string s, t;
cin >> s >> t;
string sloop = "";
while (sloop.size() <= s.size() + t.size()) {
sloop += s;
}
vector<int> table(t.size(), 0);
int i = 2;
int j = 0;
table[0] = -1;
if (t.size() >= 2)
table[1] = 0;
while (i < t.size()) {
if (t[i - 1] == t[j]) {
table[i] = j + 1;
i++;
j++;
} else if (j > 0) {
j = table[j];
} else {
table[i] = 0;
i++;
}
}
int m = 0;
i = 0;
vector<bool> pat(s.size(), false);
while (m + i < sloop.size()) {
if (t[i] == sloop[m + i]) {
i++;
if (i == t.size()) {
pat[m] = true;
i--;
m = m + i - table[i];
if (i > 0) {
i = table[i];
}
}
} else {
m = m + i - table[i];
if (i > 0) {
i = table[i];
}
}
}
int ans = 0;
vector<bool> used(s.size(), false);
int smul = 0;
while (smul < t.size()) {
smul += s.size();
}
REP(i, s.size()) {
if (pat[i] == true && used[i] == false) {
int j = i;
while (true) {
if (pat[(j - t.size() + smul) % s.size()] == true) {
j = (j - t.size() + smul) % s.size();
if (i == j) {
cout << -1 << endl;
return 0;
}
} else {
break;
}
}
int k = 0;
while (true) {
used[j] = true;
k++;
if (pat[(j + t.size()) % s.size()] == true) {
j = (j + t.size()) % s.size();
} else {
break;
}
}
if (k > ans)
ans = k;
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
using lint = long long int;
#define FOR(i, begin, end) \
for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end) \
for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
constexpr int MOD = 1000000007;
int main() {
string s, t;
cin >> s >> t;
string sloop = "";
while (sloop.size() <= s.size() + t.size()) {
sloop += s;
}
vector<int> table(t.size(), 0);
int i = 2;
int j = 0;
table[0] = -1;
if (t.size() >= 2)
table[1] = 0;
while (i < t.size()) {
if (t[i - 1] == t[j]) {
table[i] = j + 1;
i++;
j++;
} else if (j > 0) {
j = table[j];
} else {
table[i] = 0;
i++;
}
}
int m = 0;
i = 0;
vector<bool> pat(s.size(), false);
while (m < s.size()) {
if (t[i] == sloop[m + i]) {
i++;
if (i == t.size()) {
pat[m] = true;
i--;
m = m + i - table[i];
if (i > 0) {
i = table[i];
}
}
} else {
m = m + i - table[i];
if (i > 0) {
i = table[i];
}
}
}
int ans = 0;
vector<bool> used(s.size(), false);
int smul = 0;
while (smul < t.size()) {
smul += s.size();
}
REP(i, s.size()) {
if (pat[i] == true && used[i] == false) {
int j = i;
while (true) {
if (pat[(j - t.size() + smul) % s.size()] == true) {
j = (j - t.size() + smul) % s.size();
if (i == j) {
cout << -1 << endl;
return 0;
}
} else {
break;
}
}
int k = 0;
while (true) {
used[j] = true;
k++;
if (pat[(j + t.size()) % s.size()] == true) {
j = (j + t.size()) % s.size();
} else {
break;
}
}
if (k > ans)
ans = k;
}
}
cout << ans << endl;
} | replace | 38 | 39 | 38 | 39 | 0 | |
p02962 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using i64 = long long;
auto calc(const std::string &s) {
std::vector<int> a(s.size() + 1);
a[0] = -1;
for (int i = 0, j = -1; i < s.size(); i++) {
while (j >= 0 && s[i] != s[j])
j = a[j];
j++;
a[i + 1] = s[i + 1] == s[j] ? a[j] : j;
}
return a;
}
int main() {
std::string s, t;
std::cin >> s >> t;
int n = s.size(), m = t.size();
std::vector<int> e(s.size());
auto ss = s;
while (ss.size() < s.size() + t.size())
ss += s;
auto a = calc(t);
for (int i = 0, j = 0; i < ss.size(); i++) {
while (j >= 0 && ss[i] != t[j])
j = a[j];
j++;
if (j == m) {
e[(i - m + 1) % n] = 1;
j = a[j];
}
}
int ret = 0;
std::vector<int> p(n);
for (int i = 0; i < n; i++) {
if (e[i] < 0 || p[i])
continue;
int j = i, tmp = 0;
while (!p[j] && e[j]) {
j = (j + m) % n;
if (j == i) {
std::cout << -1 << std::endl;
return 0;
}
tmp++;
}
p[i] = tmp + p[j];
ret = std::max(ret, p[i]);
}
std::cout << ret << std::endl;
return 0;
}
| #include <bits/stdc++.h>
using i64 = long long;
auto calc(const std::string &s) {
std::vector<int> a(s.size() + 1);
a[0] = -1;
for (int i = 0, j = -1; i < s.size(); i++) {
while (j >= 0 && s[i] != s[j])
j = a[j];
j++;
a[i + 1] = s[i + 1] == s[j] ? a[j] : j;
}
return a;
}
int main() {
std::string s, t;
std::cin >> s >> t;
int n = s.size(), m = t.size();
std::vector<int> e(s.size());
auto ss = s;
while (ss.size() < s.size() + t.size())
ss += s;
auto a = calc(t);
for (int i = 0, j = 0; i < ss.size(); i++) {
while (j >= 0 && ss[i] != t[j])
j = a[j];
j++;
if (j == m) {
e[(i - m + 1) % n] = 1;
j = a[j];
}
}
int ret = 0;
std::vector<int> p(n);
for (int i = 0; i < n; i++) {
if (e[i] < 0 || p[i])
continue;
int j = i, tmp = 0;
while (!p[j] && e[j]) {
p[j] = 1;
j = (j + m) % n;
if (j == i) {
std::cout << -1 << std::endl;
return 0;
}
tmp++;
}
p[i] = tmp + p[j];
ret = std::max(ret, p[i]);
}
std::cout << ret << std::endl;
return 0;
}
| insert | 42 | 42 | 42 | 43 | TLE | |
p02962 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vc = vector<char>;
using vvc = vector<vc>;
using pll = pair<ll, ll>;
using stkll = vector<pll>;
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
#define rep(i, n) for (ll i = 0; i < (n); i++)
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
if (a > b)
a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
if (a < b)
a = b;
}
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x << ": " << x << endl;
#else
#define debug(x)
#endif
//
// ローリングハッシュ
//
// verified:
// ABC 141 E - Who Says a Pun?
// https://atcoder.jp/contests/abc141/tasks/abc141_e
//
struct RollingHash {
static const int base1 = 1007, base2 = 2009;
static const int mod1 = 1000000007, mod2 = 1000000009;
vector<long long> hash1, hash2, power1, power2;
// construct
RollingHash(const string &S) {
int n = (int)S.size();
hash1.assign(n + 1, 0);
hash2.assign(n + 1, 0);
power1.assign(n + 1, 1);
power2.assign(n + 1, 1);
for (int i = 0; i < n; ++i) {
hash1[i + 1] = (hash1[i] * base1 + S[i]) % mod1;
hash2[i + 1] = (hash2[i] * base2 + S[i]) % mod2;
power1[i + 1] = (power1[i] * base1) % mod1;
power2[i + 1] = (power2[i] * base2) % mod2;
}
}
// get hash of S[left:right]
inline pair<long long, long long> get(int l, int r) const {
long long res1 = hash1[r] - hash1[l] * power1[r - l] % mod1;
if (res1 < 0)
res1 += mod1;
long long res2 = hash2[r] - hash2[l] * power2[r - l] % mod2;
if (res2 < 0)
res2 += mod2;
return {res1, res2};
}
// get lcp of S[a:] and S[b:]
inline int getLCP(int a, int b) const {
int len = min((int)hash1.size() - a, (int)hash1.size() - b);
int low = 0, high = len;
while (high - low > 1) {
int mid = (low + high) >> 1;
if (get(a, a + mid) != get(b, b + mid))
high = mid;
else
low = mid;
}
return low;
}
// get lcp of S[a:] and T[b:]
inline int getLCP(const RollingHash &T, int a, int b) const {
int len = min((int)hash1.size() - a, (int)hash1.size() - b);
int low = 0, high = len;
while (high - low > 1) {
int mid = (low + high) >> 1;
if (get(a, a + mid) != T.get(b, b + mid))
high = mid;
else
low = mid;
}
return low;
}
};
string s, t, s1;
ll n, m;
bool ok[550000];
ll longest[550000];
ll calc_longest(ll i, ll dep = 0) {
if (dep > n)
return -1;
if (longest[i] != -1)
return longest[i];
if (!ok[i])
return longest[i] = 0;
longest[i] = calc_longest((i + m) % n, dep + 1);
if (longest[i] == -1)
return -1;
return longest[i] += 1;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
cin >> s >> t;
n = s.size(), m = t.size();
while (s1.size() < 2 * t.size())
s1 += s;
fill(longest, longest + 550000, -1);
RollingHash rh1(s1), rh2(t);
rep(i, n) if (rh1.get(i, i + m) == rh2.get(0, m)) ok[i] = true;
ll ans = 0;
rep(i, n) {
longest[i] = calc_longest(i);
if (longest[i] == -1) {
cout << -1 << endl;
return 0;
}
chmax(ans, longest[i]);
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vc = vector<char>;
using vvc = vector<vc>;
using pll = pair<ll, ll>;
using stkll = vector<pll>;
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
#define rep(i, n) for (ll i = 0; i < (n); i++)
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
if (a > b)
a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
if (a < b)
a = b;
}
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x << ": " << x << endl;
#else
#define debug(x)
#endif
//
// ローリングハッシュ
//
// verified:
// ABC 141 E - Who Says a Pun?
// https://atcoder.jp/contests/abc141/tasks/abc141_e
//
struct RollingHash {
static const int base1 = 1007, base2 = 2009;
static const int mod1 = 1000000007, mod2 = 1000000009;
vector<long long> hash1, hash2, power1, power2;
// construct
RollingHash(const string &S) {
int n = (int)S.size();
hash1.assign(n + 1, 0);
hash2.assign(n + 1, 0);
power1.assign(n + 1, 1);
power2.assign(n + 1, 1);
for (int i = 0; i < n; ++i) {
hash1[i + 1] = (hash1[i] * base1 + S[i]) % mod1;
hash2[i + 1] = (hash2[i] * base2 + S[i]) % mod2;
power1[i + 1] = (power1[i] * base1) % mod1;
power2[i + 1] = (power2[i] * base2) % mod2;
}
}
// get hash of S[left:right]
inline pair<long long, long long> get(int l, int r) const {
long long res1 = hash1[r] - hash1[l] * power1[r - l] % mod1;
if (res1 < 0)
res1 += mod1;
long long res2 = hash2[r] - hash2[l] * power2[r - l] % mod2;
if (res2 < 0)
res2 += mod2;
return {res1, res2};
}
// get lcp of S[a:] and S[b:]
inline int getLCP(int a, int b) const {
int len = min((int)hash1.size() - a, (int)hash1.size() - b);
int low = 0, high = len;
while (high - low > 1) {
int mid = (low + high) >> 1;
if (get(a, a + mid) != get(b, b + mid))
high = mid;
else
low = mid;
}
return low;
}
// get lcp of S[a:] and T[b:]
inline int getLCP(const RollingHash &T, int a, int b) const {
int len = min((int)hash1.size() - a, (int)hash1.size() - b);
int low = 0, high = len;
while (high - low > 1) {
int mid = (low + high) >> 1;
if (get(a, a + mid) != T.get(b, b + mid))
high = mid;
else
low = mid;
}
return low;
}
};
string s, t, s1;
ll n, m;
bool ok[550000];
ll longest[550000];
ll calc_longest(ll i, ll dep = 0) {
if (dep > n)
return -1;
if (longest[i] != -1)
return longest[i];
if (!ok[i])
return longest[i] = 0;
longest[i] = calc_longest((i + m) % n, dep + 1);
if (longest[i] == -1)
return -1;
return longest[i] += 1;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
cin >> s >> t;
n = s.size(), m = t.size();
while (s1.size() < 2 * max(n, m))
s1 += s;
fill(longest, longest + 550000, -1);
RollingHash rh1(s1), rh2(t);
rep(i, n) if (rh1.get(i, i + m) == rh2.get(0, m)) ok[i] = true;
ll ans = 0;
rep(i, n) {
longest[i] = calc_longest(i);
if (longest[i] == -1) {
cout << -1 << endl;
return 0;
}
chmax(ans, longest[i]);
}
cout << ans << endl;
} | replace | 120 | 121 | 120 | 121 | 0 | |
p02962 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, l, r) for (ll i = (l); i < (r); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 1, n + 1)
#define RFOR(i, l, r) for (ll i = (l); i >= (r); --i)
#define RREP(i, n) RFOR(i, n - 1, 0)
#define RREPS(i, n) RFOR(i, n, 1)
#define pb push_back
#define eb emplace_back
#define SZ(x) ((ll)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template <class T = ll> using V = vector<T>;
template <class T = ll> using VV = V<V<T>>;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
inline void Yes(bool b = true) { cout << (b ? "Yes" : "No") << '\n'; }
inline void YES(bool b = true) { cout << (b ? "YES" : "NO") << '\n'; }
inline void err(bool b = true) {
if (b) {
cout << -1 << '\n';
exit(0);
}
}
template <class T> inline void fin(bool b = true, T e = 0) {
if (b) {
cout << e << '\n';
exit(0);
}
}
template <class T> T Roundup_div(T x, T y) { return (x + (y - 1)) / y; }
const ll INF = 1e18;
template <typename T> T pow(T a, long long n, T e = 1) {
T ret = e;
while (n) {
if (n & 1)
ret *= a;
a *= a;
n >>= 1;
}
return ret;
}
class RollingHash {
const ll mod = 1e9 + 7;
const ll base = 1007;
V<> hash, power;
public:
RollingHash(const string &s) {
ll n = SZ(s);
hash.assign(n + 1, 0);
power.assign(n + 1, 1);
REP(i, n) {
hash[i + 1] = (hash[i] * base + s[i]) % mod;
power[i + 1] = (power[i] * base) % mod;
}
}
inline ll get(ll l, ll r) const {
ll res = hash[r] - hash[l] * power[r - l] % mod;
if (res < 0)
res += mod;
return res;
}
inline ll s_hash() { return get(0, SZ(hash) - 1); }
inline ll getLCP(int a, int b) const {
ll len = min(SZ(hash) - a, SZ(hash) - b);
ll ok = 0, ng = len;
while (abs(ok - ng) > 1) {
ll mid = (ok + ng) / 2;
if (get(a, a + mid) != get(b, b + mid))
ng = mid;
else
ok = mid;
}
return ok;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string s, t;
cin >> s >> t;
string _ = s;
while (SZ(s) < SZ(t))
s += _;
s += s + s;
RollingHash RH(s);
auto check = [&](ll mid) {
string u = "";
REP(i, mid) u += t;
RollingHash mew(u);
ll c = mew.s_hash();
REP(i, SZ(s) - SZ(t)) {
if (RH.get(i, i + SZ(u)) == c)
return true;
}
return false;
};
ll k = Roundup_div(SZ(s), SZ(t));
ll ok = 0, ng = k;
while (abs(ok - ng) > 1) {
ll m = (ok + ng) / 2;
if (check(m))
ok = m;
else
ng = m;
}
cout << (ok == k - 1 ? -1 : ok) << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, l, r) for (ll i = (l); i < (r); ++i)
#define REP(i, n) FOR(i, 0, n)
#define REPS(i, n) FOR(i, 1, n + 1)
#define RFOR(i, l, r) for (ll i = (l); i >= (r); --i)
#define RREP(i, n) RFOR(i, n - 1, 0)
#define RREPS(i, n) RFOR(i, n, 1)
#define pb push_back
#define eb emplace_back
#define SZ(x) ((ll)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template <class T = ll> using V = vector<T>;
template <class T = ll> using VV = V<V<T>>;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
inline void Yes(bool b = true) { cout << (b ? "Yes" : "No") << '\n'; }
inline void YES(bool b = true) { cout << (b ? "YES" : "NO") << '\n'; }
inline void err(bool b = true) {
if (b) {
cout << -1 << '\n';
exit(0);
}
}
template <class T> inline void fin(bool b = true, T e = 0) {
if (b) {
cout << e << '\n';
exit(0);
}
}
template <class T> T Roundup_div(T x, T y) { return (x + (y - 1)) / y; }
const ll INF = 1e18;
template <typename T> T pow(T a, long long n, T e = 1) {
T ret = e;
while (n) {
if (n & 1)
ret *= a;
a *= a;
n >>= 1;
}
return ret;
}
class RollingHash {
const ll mod = 1e9 + 7;
const ll base = 1007;
V<> hash, power;
public:
RollingHash(const string &s) {
ll n = SZ(s);
hash.assign(n + 1, 0);
power.assign(n + 1, 1);
REP(i, n) {
hash[i + 1] = (hash[i] * base + s[i]) % mod;
power[i + 1] = (power[i] * base) % mod;
}
}
inline ll get(ll l, ll r) const {
ll res = hash[r] - hash[l] * power[r - l] % mod;
if (res < 0)
res += mod;
return res;
}
inline ll s_hash() { return get(0, SZ(hash) - 1); }
inline ll getLCP(int a, int b) const {
ll len = min(SZ(hash) - a, SZ(hash) - b);
ll ok = 0, ng = len;
while (abs(ok - ng) > 1) {
ll mid = (ok + ng) / 2;
if (get(a, a + mid) != get(b, b + mid))
ng = mid;
else
ok = mid;
}
return ok;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string s, t;
cin >> s >> t;
string _ = s;
while (SZ(s) < SZ(t))
s += _;
s += s + s;
RollingHash RH(s);
auto check = [&](ll mid) {
string u = "";
REP(i, mid) u += t;
RollingHash mew(u);
ll c = mew.s_hash();
REP(i, SZ(s) - SZ(u)) {
if (RH.get(i, i + SZ(u)) == c)
return true;
}
return false;
};
ll k = Roundup_div(SZ(s), SZ(t));
ll ok = 0, ng = k;
while (abs(ok - ng) > 1) {
ll m = (ok + ng) / 2;
if (check(m))
ok = m;
else
ng = m;
}
cout << (ok == k - 1 ? -1 : ok) << endl;
} | replace | 109 | 110 | 109 | 110 | 0 | |
p02962 | C++ | Time Limit Exceeded | #include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
vector<int> calc(string &S) {
vector<int> A(S.size());
A[0] = S.size();
int i = 1, j = 0;
while (i < S.size()) {
while (i + j < S.size() && S[j] == S[i + j])
++j;
A[i] = j;
if (j == 0) {
++i;
continue;
}
int k = 1;
while (i + k < S.size() && k + A[k] < j)
A[i + k] = A[k], ++k;
i += k;
j -= k;
}
return A;
}
int main() {
string S, t;
cin >> S >> t;
string s = "";
while (s.size() < t.size())
s += S;
s += s;
s += s;
int ok = 0, ng = s.size() / t.size() + 1;
while (ng - ok > 1) {
int mid = (ok + ng) / 2;
string u = "";
rep(i, mid) u += t;
u += s;
auto ret = calc(u);
int ma = 0;
REP(i, mid * t.size(), ret.size()) { ma = max(ma, ret[i]); }
if (ma >= mid * t.size())
ok = mid;
else
ng = mid;
}
s += s;
int res = ok;
ok = 0, ng = s.size() / t.size() + 1;
while (ng - ok > 1) {
int mid = (ok + ng) / 2;
string u = "";
rep(i, mid) u += t;
u += s;
auto ret = calc(u);
int ma = 0;
REP(i, mid * t.size(), ret.size()) { ma = max(ma, ret[i]); }
if (ma >= mid * t.size())
ok = mid;
else
ng = mid;
}
if (ok > res) {
cout << -1 << endl;
} else
cout << ok << endl;
return 0;
} | #include <algorithm>
#include <assert.h>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 1e9 + 7;
vector<int> calc(string &S) {
vector<int> A(S.size());
A[0] = S.size();
int i = 1, j = 0;
while (i < S.size()) {
while (i + j < S.size() && S[j] == S[i + j])
++j;
A[i] = j;
if (j == 0) {
++i;
continue;
}
int k = 1;
while (i + k < S.size() && k + A[k] < j)
A[i + k] = A[k], ++k;
i += k;
j -= k;
}
return A;
}
int main() {
string S, t;
cin >> S >> t;
string w = "";
while (w.size() < t.size())
w += S;
string s = w + w + w;
int ok = 0, ng = s.size() / t.size() + 1;
while (ng - ok > 1) {
int mid = (ok + ng) / 2;
string u = "";
rep(i, mid) u += t;
u += s;
auto ret = calc(u);
int ma = 0;
REP(i, mid * t.size(), ret.size()) { ma = max(ma, ret[i]); }
if (ma >= mid * t.size())
ok = mid;
else
ng = mid;
}
s += s;
int res = ok;
ok = 0, ng = s.size() / t.size() + 1;
while (ng - ok > 1) {
int mid = (ok + ng) / 2;
string u = "";
rep(i, mid) u += t;
u += s;
auto ret = calc(u);
int ma = 0;
REP(i, mid * t.size(), ret.size()) { ma = max(ma, ret[i]); }
if (ma >= mid * t.size())
ok = mid;
else
ng = mid;
}
if (ok > res) {
cout << -1 << endl;
} else
cout << ok << endl;
return 0;
} | replace | 47 | 52 | 47 | 51 | TLE | |
p02962 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <stdio.h>
#include <vector>
int MP(std::string &S, std::vector<long long int> &A);
int KMP(std::string &S, std::string &word,
std::vector<long long int> &match_table);
long long int euclid_gcd(long long int x, long long int y);
int main() {
// read problem
std::string s, t;
std::cin >> s;
std::cin >> t;
long long int slen = s.length(), tlen = t.length();
// std::cerr << "s: " << s << std::endl << "t: " << t << std::endl;
// solve problem
std::string s_extend = s;
for (int i = 0; i <= tlen / slen; i++) {
s_extend = s_extend + s_extend;
}
s_extend = s_extend.substr(0, slen + tlen);
// KMP
std::vector<long long int> match_table;
KMP(s_extend, t, match_table);
// change format of result
std::vector<long long int> match_index(slen);
for (long long int i = 0; i < match_table.size(); i++) {
if (match_table[i] < slen) {
match_index[match_table[i]] = 1;
} else {
break;
}
}
// calculate answer
long long int gcd_st = euclid_gcd(slen, tlen);
long long int tmp, ntmp, ntmp_max = 0, start, nstart, eternity_flg;
for (long long int i = 0; i < gcd_st; i++) {
tmp = i;
eternity_flg = 1;
ntmp = 0;
nstart = 0;
for (int j = 0; j < slen / gcd_st; j++) {
tmp = (tmp + tlen) % slen;
if (match_index[tmp] == 0) {
eternity_flg = 0;
ntmp_max = std::max(ntmp, ntmp_max);
ntmp = 0;
} else {
if (eternity_flg == 1) {
nstart++;
}
ntmp++;
}
}
if (eternity_flg == 1) {
break;
}
ntmp_max = std::max(ntmp_max, nstart + ntmp);
}
std::cerr << "Answer:" << std::endl;
if (eternity_flg == 1) {
std::cout << -1 << std::endl;
} else {
std::cout << ntmp_max << std::endl;
}
return 0;
}
int MP(std::string &S, std::vector<long long int> &A) {
A[0] = -1;
long long int L = S.length();
long long int j = -1;
for (long long int i = 0; i < L; i++) {
while (j >= 0 && S[i] != S[j])
j = A[j];
j++;
A[i + 1] = j;
}
return 0;
}
int KMP(std::string &S, std::string &word,
std::vector<long long int> &match_table) {
// preparation: MP calculation
std::vector<long long int> MP_table(word.length() + 1);
MP(word, MP_table);
match_table.clear();
// make match_table
long long int m = 0, i = 0, ls = S.length(), lword = word.length();
while (m + i < ls) {
if (word[i] == S[m + i]) {
i++;
if (i == lword) {
match_table.push_back(m);
m = m + i - MP_table[i];
i = MP_table[i];
}
} else {
m = m + i - MP_table[i];
if (i > 0) {
i = MP_table[i];
}
}
}
return 0;
}
long long int euclid_gcd(long long int x, long long int y) {
if (y == 0) {
return x;
}
/*else if(x == 0){
return y;
}*/
else {
return euclid_gcd(y, x % y);
}
}
| #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <stdio.h>
#include <vector>
int MP(std::string &S, std::vector<long long int> &A);
int KMP(std::string &S, std::string &word,
std::vector<long long int> &match_table);
long long int euclid_gcd(long long int x, long long int y);
int main() {
// read problem
std::string s, t;
std::cin >> s;
std::cin >> t;
long long int slen = s.length(), tlen = t.length();
// std::cerr << "s: " << s << std::endl << "t: " << t << std::endl;
// solve problem
std::string s_extend = s;
for (int i = 0; s_extend.length() < slen + tlen; i++) {
s_extend = s_extend + s_extend;
}
s_extend = s_extend.substr(0, slen + tlen);
// KMP
std::vector<long long int> match_table;
KMP(s_extend, t, match_table);
// change format of result
std::vector<long long int> match_index(slen);
for (long long int i = 0; i < match_table.size(); i++) {
if (match_table[i] < slen) {
match_index[match_table[i]] = 1;
} else {
break;
}
}
// calculate answer
long long int gcd_st = euclid_gcd(slen, tlen);
long long int tmp, ntmp, ntmp_max = 0, start, nstart, eternity_flg;
for (long long int i = 0; i < gcd_st; i++) {
tmp = i;
eternity_flg = 1;
ntmp = 0;
nstart = 0;
for (int j = 0; j < slen / gcd_st; j++) {
tmp = (tmp + tlen) % slen;
if (match_index[tmp] == 0) {
eternity_flg = 0;
ntmp_max = std::max(ntmp, ntmp_max);
ntmp = 0;
} else {
if (eternity_flg == 1) {
nstart++;
}
ntmp++;
}
}
if (eternity_flg == 1) {
break;
}
ntmp_max = std::max(ntmp_max, nstart + ntmp);
}
std::cerr << "Answer:" << std::endl;
if (eternity_flg == 1) {
std::cout << -1 << std::endl;
} else {
std::cout << ntmp_max << std::endl;
}
return 0;
}
int MP(std::string &S, std::vector<long long int> &A) {
A[0] = -1;
long long int L = S.length();
long long int j = -1;
for (long long int i = 0; i < L; i++) {
while (j >= 0 && S[i] != S[j])
j = A[j];
j++;
A[i + 1] = j;
}
return 0;
}
int KMP(std::string &S, std::string &word,
std::vector<long long int> &match_table) {
// preparation: MP calculation
std::vector<long long int> MP_table(word.length() + 1);
MP(word, MP_table);
match_table.clear();
// make match_table
long long int m = 0, i = 0, ls = S.length(), lword = word.length();
while (m + i < ls) {
if (word[i] == S[m + i]) {
i++;
if (i == lword) {
match_table.push_back(m);
m = m + i - MP_table[i];
i = MP_table[i];
}
} else {
m = m + i - MP_table[i];
if (i > 0) {
i = MP_table[i];
}
}
}
return 0;
}
long long int euclid_gcd(long long int x, long long int y) {
if (y == 0) {
return x;
}
/*else if(x == 0){
return y;
}*/
else {
return euclid_gcd(y, x % y);
}
}
| replace | 26 | 27 | 26 | 27 | 0 | Answer:
|
p02962 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define int long long
template <typename T, T MOD, T B> struct RollingHash {
vector<T> hash, p;
RollingHash() {}
RollingHash(const string &s) {
int n = s.size();
hash.assign(n + 1, 0);
p.assign(n + 1, 1);
for (int i = 0; i < n; i++) {
hash[i + 1] = (hash[i] * B + s[i]) % MOD;
p[i + 1] = p[i] * B % MOD;
}
}
// S[l, r)
T find(int l, int r) {
T res = hash[r] + MOD - hash[l] * p[r - l] % MOD;
return res >= MOD ? res - MOD : res;
}
};
using ll = long long;
const ll MOD = 1e9 + 7;
const ll B = 1777771;
string s, t;
int n, m;
const int MOD2 = 1e9 + 9;
bool check(int k) {
int x = 2000000 / n;
string S = "", T = "";
for (int i = 0; i < x; i++)
S += s;
for (int i = 0; i < k; i++)
T += t;
using RH = RollingHash<ll, MOD, B>;
RH rt(S), rp(T);
for (int i = 0; i < (int)S.size() - (int)T.size() + 1; i++) {
if (rt.find(i, i + T.size()) == rp.find(0, T.size()))
return true;
}
return false;
}
bool check2(int k) {
int x = 2000000 / n;
string S = "", T = "";
for (int i = 0; i < x; i++)
S += s;
for (int i = 0; i < k; i++)
T += t;
using RH = RollingHash<ll, MOD2, B>;
RH rt(S), rp(T);
for (int i = 0; i < (int)S.size() - (int)T.size() + 1; i++) {
if (rt.find(i, i + T.size()) == rp.find(0, T.size()))
return true;
}
return false;
}
signed main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cout << fixed << setprecision(12);
cin >> s >> t;
n = s.size();
m = t.size();
int l = 0, r = 2000000 / m;
while (r - l > 1) {
int mid = (l + r) / 2;
if (check(mid) && check2(mid))
l = mid;
else
r = mid;
}
if (l >= 2000000 / m - 1)
cout << -1 << endl;
else
cout << l << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
template <typename T, T MOD, T B> struct RollingHash {
vector<T> hash, p;
RollingHash() {}
RollingHash(const string &s) {
int n = s.size();
hash.assign(n + 1, 0);
p.assign(n + 1, 1);
for (int i = 0; i < n; i++) {
hash[i + 1] = (hash[i] * B + s[i]) % MOD;
p[i + 1] = p[i] * B % MOD;
}
}
// S[l, r)
T find(int l, int r) {
T res = hash[r] + MOD - hash[l] * p[r - l] % MOD;
return res >= MOD ? res - MOD : res;
}
};
using ll = long long;
const ll MOD = 1e9 + 7;
const ll B = 1777771;
string s, t;
int n, m;
const int MOD2 = 1e9 + 9;
bool check(int k) {
int x = 2000000 / n;
string S = "", T = "";
for (int i = 0; i < x; i++)
S += s;
for (int i = 0; i < k; i++)
T += t;
using RH = RollingHash<ll, MOD, B>;
RH rt(S), rp(T);
for (int i = 0; i < (int)S.size() - (int)T.size() + 1; i++) {
if (rt.find(i, i + T.size()) == rp.find(0, T.size()))
return true;
}
return false;
}
bool check2(int k) {
int x = 2000000 / n;
string S = "", T = "";
for (int i = 0; i < x; i++)
S += s;
for (int i = 0; i < k; i++)
T += t;
using RH = RollingHash<ll, MOD2, B>;
RH rt(S), rp(T);
for (int i = 0; i < (int)S.size() - (int)T.size() + 1; i++) {
if (rt.find(i, i + T.size()) == rp.find(0, T.size()))
return true;
}
return false;
}
signed main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cout << fixed << setprecision(12);
cin >> s >> t;
n = s.size();
m = t.size();
int l = 0, r = 2000000 / m;
while (r - l > 1) {
int mid = (l + r) / 2;
if (check(mid))
l = mid;
else
r = mid;
}
if (l >= 2000000 / m - 1)
cout << -1 << endl;
else
cout << l << endl;
return 0;
}
| replace | 79 | 80 | 79 | 80 | TLE | |
p02962 | C++ | Time Limit Exceeded | /*
* Welcome to my code!
*---------------------------------------------*
* author : lynmisakura(twitter : @andoreiji11)
*/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define repn for (int i = 0; i < n; i++)
#define ain(a) \
for (auto &i : a) \
cin >> i;
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x.size())
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define cont continue
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using pi = pair<int, int>;
using vpi = vector<pi>;
using pl = pair<ll, ll>;
using vpl = vector<pl>;
template <class T> bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
} else
return false;
}
template <class T> bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
} else
return false;
}
template <class T> void print(std::vector<T> a) {
int sz = a.size();
for (int i = 0; i < a.size(); i++)
cout << a[i] << (i < sz - 1 ? ' ' : '\n');
}
void ioboost() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
}
///////////////////////////////////////////////////////////
void getZarr(string str, int Z[]) {
int n = str.length();
int L, R, k;
L = R = 0;
for (int i = 1; i < n; ++i) {
if (i > R) {
L = R = i;
while (R < n && str[R - L] == str[R])
R++;
Z[i] = R - L;
R--;
} else {
k = i - L;
if (Z[k] < R - i + 1)
Z[i] = Z[k];
else {
L = i;
while (R < n && str[R - L] == str[R])
R++;
Z[i] = R - L;
R--;
}
}
}
}
vector<int> search(string text, string pattern) {
string concat = pattern + "$" + text;
int l = concat.length();
int Z[l];
getZarr(concat, Z);
vector<int> res;
for (int i = 0; i < l; ++i)
if (Z[i] == pattern.length())
res.push_back(i - pattern.length() - 1);
return res;
}
int main(int argc, char const *argv[]) {
ioboost();
string s, t;
cin >> s >> t;
string x = s;
int n = s.size(), m = t.size();
rep(i, max(2, (m + n - 1) / n)) s += x;
auto v = search(s, t);
vector<int> ok(n, 0);
for (auto i : v) {
ok[i % n] = 1;
}
vector<int> vis(n, 0);
int ans = 0;
rep(i, n) {
if (vis[i] || !ok[i])
continue;
int cnt = 0;
int now = i;
while (cnt <= n + 1) {
if (ok[now]) {
now = (now + m) % n;
cnt++;
} else {
break;
}
}
if (cnt > n) {
cout << -1 << '\n';
return 0;
} else
chmax(ans, cnt);
}
cout << ans << '\n';
return 0;
} | /*
* Welcome to my code!
*---------------------------------------------*
* author : lynmisakura(twitter : @andoreiji11)
*/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define repn for (int i = 0; i < n; i++)
#define ain(a) \
for (auto &i : a) \
cin >> i;
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x.size())
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define cont continue
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using pi = pair<int, int>;
using vpi = vector<pi>;
using pl = pair<ll, ll>;
using vpl = vector<pl>;
template <class T> bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
} else
return false;
}
template <class T> bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
} else
return false;
}
template <class T> void print(std::vector<T> a) {
int sz = a.size();
for (int i = 0; i < a.size(); i++)
cout << a[i] << (i < sz - 1 ? ' ' : '\n');
}
void ioboost() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
}
///////////////////////////////////////////////////////////
void getZarr(string str, int Z[]) {
int n = str.length();
int L, R, k;
L = R = 0;
for (int i = 1; i < n; ++i) {
if (i > R) {
L = R = i;
while (R < n && str[R - L] == str[R])
R++;
Z[i] = R - L;
R--;
} else {
k = i - L;
if (Z[k] < R - i + 1)
Z[i] = Z[k];
else {
L = i;
while (R < n && str[R - L] == str[R])
R++;
Z[i] = R - L;
R--;
}
}
}
}
vector<int> search(string text, string pattern) {
string concat = pattern + "$" + text;
int l = concat.length();
int Z[l];
getZarr(concat, Z);
vector<int> res;
for (int i = 0; i < l; ++i)
if (Z[i] == pattern.length())
res.push_back(i - pattern.length() - 1);
return res;
}
int main(int argc, char const *argv[]) {
ioboost();
string s, t;
cin >> s >> t;
string x = s;
int n = s.size(), m = t.size();
rep(i, max(2, (m + n - 1) / n)) s += x;
auto v = search(s, t);
vector<int> ok(n, 0);
for (auto i : v) {
ok[i % n] = 1;
}
vector<int> vis(n, 0);
int ans = 0;
rep(i, n) {
if (vis[i] || !ok[i])
continue;
int cnt = 0;
int now = i;
while (cnt <= n + 1) {
vis[now]++;
if (ok[now]) {
now = (now + m) % n;
cnt++;
} else {
break;
}
}
if (cnt > n) {
cout << -1 << '\n';
return 0;
} else
chmax(ans, cnt);
}
cout << ans << '\n';
return 0;
} | insert | 142 | 142 | 142 | 143 | TLE | |
p02962 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
vector<int> z_algorithm(const vector<int> &S) {
int N = S.size(), c = 0;
vector<int> ret(N, 0);
for (int i = 1; i < N; i++) {
if (i + ret[i - c] < c + ret[c]) {
ret[i] = ret[i - c];
} else {
int j = max(0, c + ret[c] - i);
while (i + j < N && S[j] == S[i + j])
j++;
ret[i] = j;
c = i;
}
}
ret[0] = N;
return ret;
}
vector<int> z_algorithm(const string &S) {
int N = S.size();
vector<int> s(N);
for (int i = 0; i < N; i++)
s[i] = S[i];
return z_algorithm(s);
}
vector<int> z_algorithm(string &A, string &B) {
string S = A + '!' + B;
auto z = z_algorithm(S);
vector<int> ret(B.size());
for (int i = 0; i < (int)B.size(); i++) {
ret[i] = z[i + A.size() + 1];
}
return ret;
}
int main() {
string s, T;
cin >> s >> T;
string S;
while (S.size() < T.size())
for (char c : s)
S.push_back(c);
int N = S.size(), M = T.size();
S += S;
auto z = z_algorithm(T, S);
vector<int> len(N, -1);
for (int s = 0; s < N; s++) {
if (len[s] >= 0)
continue;
int pt = s, l = 0;
while (z[pt] == M) {
l++;
pt = (pt + M) % N;
if (pt == s) {
cout << -1 << endl;
return 0;
}
if (len[pt] >= 0) {
l += len[pt];
break;
}
}
len[s] = l;
}
cout << *max_element(len.begin(), len.end()) << endl;
}
| #include <bits/stdc++.h>
using namespace std;
vector<int> z_algorithm(const vector<int> &S) {
int N = S.size(), c = 0;
vector<int> ret(N, 0);
for (int i = 1; i < N; i++) {
if (i + ret[i - c] < c + ret[c]) {
ret[i] = ret[i - c];
} else {
int j = max(0, c + ret[c] - i);
while (i + j < N && S[j] == S[i + j])
j++;
ret[i] = j;
c = i;
}
}
ret[0] = N;
return ret;
}
vector<int> z_algorithm(const string &S) {
int N = S.size();
vector<int> s(N);
for (int i = 0; i < N; i++)
s[i] = S[i];
return z_algorithm(s);
}
vector<int> z_algorithm(string &A, string &B) {
string S = A + '!' + B;
auto z = z_algorithm(S);
vector<int> ret(B.size());
for (int i = 0; i < (int)B.size(); i++) {
ret[i] = z[i + A.size() + 1];
}
return ret;
}
int main() {
string s, T;
cin >> s >> T;
string S;
while (S.size() < T.size())
for (char c : s)
S.push_back(c);
int N = S.size(), M = T.size();
S += S;
auto z = z_algorithm(T, S);
vector<int> len(N, -1);
for (int s = 0; s < N; s++) {
if (len[s] >= 0)
continue;
int pt = s, l = 0;
while (z[pt] == M) {
l++;
pt = (pt + M) % N;
if (pt == s) {
cout << -1 << endl;
return 0;
}
if (len[pt] >= 0) {
l += len[pt];
break;
}
len[pt] = 0;
}
len[s] = l;
}
cout << *max_element(len.begin(), len.end()) << endl;
}
| insert | 65 | 65 | 65 | 66 | TLE | |
p02962 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int main() {
ios::sync_with_stdio(false);
string s, t;
cin >> s;
cin >> t;
int slen = s.length(), tlen = t.length();
s = t + '#' + s;
for (int i = 0; i < t.length(); i++) {
s += s[tlen + 1 + i % s.length()];
}
int z[s.length()];
z[0] = 0;
int j0 = 0, j1 = 0;
for (int i = 1; i < s.length(); i++) {
if (i > j0) {
j0 = j1 = i;
}
if (i + z[i - j0] < j1) {
z[i] = z[i - j0];
} else {
j0 = i;
while (j1 < s.length() && s[j1] == s[j1 - j0]) {
j1++;
}
z[i] = j1 - j0;
}
}
bool v[slen];
memset(v, false, sizeof(v));
int ans = 0;
for (int i = 0; i < slen; i++) {
if (!v[i] && z[tlen + 1 + i] == tlen) {
int x = i;
int cnt = 0;
while (!v[x] && z[tlen + 1 + x] == tlen) {
v[x] = true;
cnt++;
x = ((x - tlen) % slen + slen) % slen;
}
if (v[x]) {
cout << -1 << endl;
return 0;
}
x = (i + tlen) % slen;
while (!v[x] && z[tlen + 1 + x] == tlen) {
v[x] = true;
cnt++;
x = (x + tlen) % slen;
}
ans = max(ans, cnt);
}
}
cout << ans << endl;
return 0;
}
| #include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int main() {
ios::sync_with_stdio(false);
string s, t;
cin >> s;
cin >> t;
int slen = s.length(), tlen = t.length();
s = t + '#' + s;
for (int i = 0; i < t.length(); i++) {
s += s[tlen + 1 + i % s.length()];
}
int z[s.length()];
z[0] = 0;
int j0 = 0, j1 = 0;
for (int i = 1; i < s.length(); i++) {
if (i >= j1) {
j0 = j1 = i;
}
if (i + z[i - j0] < j1) {
z[i] = z[i - j0];
} else {
j0 = i;
while (j1 < s.length() && s[j1] == s[j1 - j0]) {
j1++;
}
z[i] = j1 - j0;
}
}
bool v[slen];
memset(v, false, sizeof(v));
int ans = 0;
for (int i = 0; i < slen; i++) {
if (!v[i] && z[tlen + 1 + i] == tlen) {
int x = i;
int cnt = 0;
while (!v[x] && z[tlen + 1 + x] == tlen) {
v[x] = true;
cnt++;
x = ((x - tlen) % slen + slen) % slen;
}
if (v[x]) {
cout << -1 << endl;
return 0;
}
x = (i + tlen) % slen;
while (!v[x] && z[tlen + 1 + x] == tlen) {
v[x] = true;
cnt++;
x = (x + tlen) % slen;
}
ans = max(ans, cnt);
}
}
cout << ans << endl;
return 0;
}
| replace | 24 | 25 | 24 | 25 | TLE | |
p02962 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define int long long
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef pair<int, int> pii;
const int INF = 1l << 60;
#define u_b upper_bound
#define l_b lower_bound
const int mod = 1000000007;
template <int M = 1000000007> struct ModInt {
int data;
ModInt(int data) : data(data) {}
ModInt() : data(0) {}
inline ModInt<M> operator+(ModInt<M> const &right) const {
int temp = data + right.data;
return ModInt(temp > M ? temp - M : temp);
}
inline ModInt<M> operator-(ModInt<M> const &right) const {
int temp = data - right.data;
return ModInt(temp < 0 ? temp + M : temp);
}
inline ModInt<M> operator*(ModInt<M> const &right) const {
int temp = data * right.data % M;
return ModInt(temp < 0 ? temp + M : temp);
}
inline bool operator==(ModInt<M> const &right) const {
return data == right.data;
}
inline bool operator!=(ModInt<M> const &right) const {
return data != right.data;
}
};
vector<ModInt<>> createHash(string s);
int solve(int begin); // sのbegin文字目から始めた時の答え
void preCalc();
string S, T;
vector<ModInt<>> shash, thash;
bool inf; // 無限?
bool calling[500500]; // 再帰関数solveでループになっているか確認
signed main() {
preCalc();
cin >> S >> T;
shash = createHash(S);
thash = createHash(T);
int ans = 0;
rep(i, S.length()) {
ans = max(ans, solve(i));
if (inf) {
cout << -1 << endl;
return 0;
}
}
cout << ans << endl;
return 0;
}
ModInt<> powMemo[500500];
int memo[500500];
void preCalc() {
memo[0] = -1;
powMemo[0] = 1;
for (int i = 1; i < 500500; ++i) {
memo[i] = -1;
powMemo[i] = powMemo[i - 1] * 27;
}
}
ModInt<> Slice(vector<ModInt<>> hash, int begin, int length) {
return hash[begin + length] - hash[begin] * powMemo[length];
}
int solve(int begin) {
if (calling[begin]) {
inf = true;
return memo[begin] = 0;
}
calling[begin] = true;
int tPlace = 0;
int sPlace = begin;
int remainLength = T.length();
while (remainLength) {
int nextLength =
min(remainLength, (int)min(T.length() - tPlace, S.length() - sPlace));
if (Slice(shash, sPlace, nextLength) != Slice(thash, tPlace, nextLength)) {
calling[begin] = false;
return memo[begin] = 0;
}
remainLength -= nextLength;
sPlace += nextLength;
tPlace += nextLength;
if (sPlace == S.length())
sPlace = 0;
if (tPlace == T.length())
tPlace = 0;
}
int res = (memo[sPlace] != -1 ? memo[sPlace] : solve(sPlace)) + 1;
calling[begin] = false;
return memo[begin] = res;
}
vector<ModInt<>> createHash(string s) {
vector<ModInt<>> hash(s.length() + 1);
rep(i, s.length()) { hash[i + 1] = hash[i] * 27 + (s[i] - 96); }
return hash;
}
| #include <bits/stdc++.h>
#define int long long
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef pair<int, int> pii;
const int INF = 1l << 60;
#define u_b upper_bound
#define l_b lower_bound
const int mod = 1000000007;
template <int M = 1000000007> struct ModInt {
int data;
ModInt(int data) : data(data) {}
ModInt() : data(0) {}
inline ModInt<M> operator+(ModInt<M> const &right) const {
int temp = data + right.data;
return ModInt(temp > M ? temp - M : temp);
}
inline ModInt<M> operator-(ModInt<M> const &right) const {
int temp = data - right.data;
return ModInt(temp < 0 ? temp + M : temp);
}
inline ModInt<M> operator*(ModInt<M> const &right) const {
int temp = data * right.data % M;
return ModInt(temp < 0 ? temp + M : temp);
}
inline bool operator==(ModInt<M> const &right) const {
return data == right.data;
}
inline bool operator!=(ModInt<M> const &right) const {
return data != right.data;
}
};
vector<ModInt<>> createHash(string s);
int solve(int begin); // sのbegin文字目から始めた時の答え
void preCalc();
string S, T;
vector<ModInt<>> shash, thash;
bool inf; // 無限?
bool calling[500500]; // 再帰関数solveでループになっているか確認
signed main() {
preCalc();
cin >> S >> T;
shash = createHash(S);
thash = createHash(T);
int ans = 0;
rep(i, S.length()) {
ans = max(ans, solve(i));
if (inf) {
cout << -1 << endl;
return 0;
}
}
cout << ans << endl;
return 0;
}
ModInt<> powMemo[500500];
int memo[500500];
void preCalc() {
memo[0] = -1;
powMemo[0] = 1;
for (int i = 1; i < 500500; ++i) {
memo[i] = -1;
powMemo[i] = powMemo[i - 1] * 27;
}
}
ModInt<> Slice(vector<ModInt<>> const &hash, int begin, int length) {
return hash[begin + length] - hash[begin] * powMemo[length];
}
int solve(int begin) {
if (calling[begin]) {
inf = true;
return memo[begin] = 0;
}
calling[begin] = true;
int tPlace = 0;
int sPlace = begin;
int remainLength = T.length();
while (remainLength) {
int nextLength =
min(remainLength, (int)min(T.length() - tPlace, S.length() - sPlace));
if (Slice(shash, sPlace, nextLength) != Slice(thash, tPlace, nextLength)) {
calling[begin] = false;
return memo[begin] = 0;
}
remainLength -= nextLength;
sPlace += nextLength;
tPlace += nextLength;
if (sPlace == S.length())
sPlace = 0;
if (tPlace == T.length())
tPlace = 0;
}
int res = (memo[sPlace] != -1 ? memo[sPlace] : solve(sPlace)) + 1;
calling[begin] = false;
return memo[begin] = res;
}
vector<ModInt<>> createHash(string s) {
vector<ModInt<>> hash(s.length() + 1);
rep(i, s.length()) { hash[i + 1] = hash[i] * 27 + (s[i] - 96); }
return hash;
}
| replace | 83 | 84 | 83 | 84 | TLE | |
p02962 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define all(V) V.begin(), V.end()
using ll = long long;
const ll MOD = 1000000007;
bool out[500010], in[500010], arr[500010];
void MP(vector<int> &V, string S) {
V[0] = -1;
int j = -1, ss = S.size();
for (int i = 0; i < ss; i++) {
while (j >= 0 && S[i] != S[j])
j = V[j];
j++;
V[i + 1] = j;
}
}
int main() {
string S, SS, T;
cin >> S >> T;
SS = S;
while (S.size() < T.size())
S += SS;
T += 'X';
int sz = S.size(), tz = T.size();
vector<int> mpt(tz + 1);
MP(mpt, T);
for (int i = 0, st = 0; st < sz;) {
if (T[i] == S[(st + i) % sz]) {
i++;
} else {
if (T[i] == 'X')
out[st] = 1, in[(st + i) % sz] = 1;
st = st + i - mpt[i];
if (i > 0)
i = mpt[i];
}
}
int ans = -1;
bool roop = 0;
tz--;
for (int i = 0; i < sz; i++) {
if (arr[i])
continue;
if (out[i] && in[i])
roop = 1;
if (out[i] && !in[i]) {
int f = i, c = 0;
while (out[f]) {
arr[f] = 1;
f = (f + tz) % sz;
c++;
}
ans = max(c, ans);
}
}
if (roop) {
cout << ans << endl;
} else {
cout << max(ans, 0) << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
#define all(V) V.begin(), V.end()
using ll = long long;
const ll MOD = 1000000007;
bool out[1000010], in[1000010], arr[1000010];
void MP(vector<int> &V, string S) {
V[0] = -1;
int j = -1, ss = S.size();
for (int i = 0; i < ss; i++) {
while (j >= 0 && S[i] != S[j])
j = V[j];
j++;
V[i + 1] = j;
}
}
int main() {
string S, SS, T;
cin >> S >> T;
SS = S;
while (S.size() < T.size())
S += SS;
T += 'X';
int sz = S.size(), tz = T.size();
vector<int> mpt(tz + 1);
MP(mpt, T);
for (int i = 0, st = 0; st < sz;) {
if (T[i] == S[(st + i) % sz]) {
i++;
} else {
if (T[i] == 'X')
out[st] = 1, in[(st + i) % sz] = 1;
st = st + i - mpt[i];
if (i > 0)
i = mpt[i];
}
}
int ans = -1;
bool roop = 0;
tz--;
for (int i = 0; i < sz; i++) {
if (arr[i])
continue;
if (out[i] && in[i])
roop = 1;
if (out[i] && !in[i]) {
int f = i, c = 0;
while (out[f]) {
arr[f] = 1;
f = (f + tz) % sz;
c++;
}
ans = max(c, ans);
}
}
if (roop) {
cout << ans << endl;
} else {
cout << max(ans, 0) << endl;
}
} | replace | 6 | 7 | 6 | 7 | 0 | |
p02963 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef long double ld;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
pbds;
typedef set<int>::iterator sit;
typedef map<int, int>::iterator mit;
typedef vector<int>::iterator vit;
long long INF = numeric_limits<long long>::max();
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
// freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
ll s;
cin >> s;
ll a, b, x, y;
if (s <= 1e9) {
a = 0;
b = 0;
x = 1;
y = s;
} else {
bool ok = 0;
for (ll i = sqrt(s); i >= 2; i--) {
if (s / i > 1000000000 || i > 1000000000)
break;
if (s % i == 0) {
a = 0;
b = 0;
x = i;
y = s / i;
ok = 1;
break;
}
}
ll add;
if (!ok) {
add = 1;
ll curr = s + add;
while (!ok) {
bool t1 = 0;
bool t2 = 0;
for (ll i = sqrt(add); sqrt(add) - i < 100 && i >= 1; i--) {
if (add % i == 0) {
t1 = 1;
a = i;
b = add / i;
break;
}
}
if (t1) {
for (ll i = sqrt(curr); sqrt(curr) - i < 100 && i >= 2; i--) {
if (curr / i > 1000000000 || i > 1000000000)
break;
if (curr % i == 0) {
t2 = 1;
x = i;
y = curr / i;
break;
}
}
}
if (t1 & t2)
ok = 1;
add++;
}
}
// 1000000000
}
cout << a << ' ' << b << ' ' << x << " 0 0 " << y << endl;
// cout << (x-a)*(y-b)-a*b << endl;
}
| #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef long double ld;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
pbds;
typedef set<int>::iterator sit;
typedef map<int, int>::iterator mit;
typedef vector<int>::iterator vit;
long long INF = numeric_limits<long long>::max();
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
// freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
ll s;
cin >> s;
ll x, y;
ll v = 1000000000;
x = (v - s % v) % v;
y = (s + x) / v;
cout << "0 0 1000000000 1 " << x << ' ' << y << endl;
}
| replace | 36 | 92 | 36 | 41 | TLE | |
p02963 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
long long S;
cin >> S;
long long N = pow(10, 9);
for (long long i = 1; i <= N; i++) {
if (S % i == 0 && S / i <= N) {
cout << 0 << " " << 0 << " " << i << " " << 0 << " " << 0 << " " << S / i
<< endl;
break;
}
}
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
long long S;
cin >> S;
long long N = pow(10, 9);
if (S == pow(10, 18)) {
cout << 0 << " " << 0 << " " << N << " " << 0 << " " << 0 << " " << N
<< endl;
} else {
cout << 0 << " " << 0 << " " << N << " " << 1 << " " << N - S % N << " "
<< S / N + 1 << endl;
}
}
| replace | 6 | 12 | 6 | 12 | TLE | |
p02963 | C++ | Runtime Error | // #define DEB1 //output check function
// #define DEB2 //same output, input from input.txt
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <mutex>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <thread>
#include <vector>
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
#define INF(a) memset(a, 0x3f, sizeof(a))
#define ALL(t) (t).begin(), (t).end()
#define FILL(a, c) fill(a, a + sizeof(a) / sizeof(*a), c)
#define FILL2D(A, c) fill(A[0], A[0] + sizeof(A) / sizeof(**A), c)
#define FILL3D(A, c) fill(A[0][0], A[0][0] + sizeof(A) / sizeof(***A), c)
#define REP(i, m) for (int i = 0; i < (m); ++i)
#define REPN(i, m, in) for (int i = (in); i < (m); ++i)
#define IN(a, x, b) (a <= x && x < b)
#define PB push_back
using namespace std;
using LL = long long;
using PAIR = pair<int, int>;
using PAIRLL = pair<LL, LL>;
using VI = vector<int>;
using VVI = vector<VI>;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
#ifdef DEB1
#define DPRINT(x) cout << #x << "=" << (x) << endl; // print at debug mode
#else
#define DPRINT(x) ;
#endif
#ifdef DEB1
#define DCHECK(x) cout << "debug check" << #x << endl;
#else
#define DCHECK(x) ;
#endif
/*
constant values
*/
// const int dx[] = {1,0,-1,0};
// const int dy[] = {0,1,0,-1};
// const int MX = 31001;
// const LL INF= 1LL<<60 ;//INF = 1001001001001; //1e9
// Main program
void solve() {
string S;
cin >> S;
int size = S.size();
DPRINT(size)
if (size == 19) {
cout << "0 0 1000000000 0 0 1000000000 " << endl;
return;
}
if (size < 9) {
cout << "0 0 1 0 0 " + S << endl;
return;
} else {
string left, right;
right = S.substr(size - 9, 9);
left = S.substr(0, size - 9);
DPRINT(left)
LL iright = stoi(right);
LL ileft = stoi(left);
LL b1 = 1000000000 - iright;
LL b2 = ileft + 1;
cout << "0 0 1000000000 1 " << b1 << " " << b2 << endl;
// LL check;
// check = 1000000000 - b1;
// DPRINT(check)
}
}
// ----------------------------------------------
int main() {
#ifdef DEB2
cout << "DEBUG MODE" << endl;
ifstream in("input.txt"); // for debug
cin.rdbuf(in.rdbuf()); // for debug
#endif
int T = 1;
for (int i = 0; i < T; ++i) {
solve();
}
} | // #define DEB1 //output check function
// #define DEB2 //same output, input from input.txt
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <mutex>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <thread>
#include <vector>
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
#define INF(a) memset(a, 0x3f, sizeof(a))
#define ALL(t) (t).begin(), (t).end()
#define FILL(a, c) fill(a, a + sizeof(a) / sizeof(*a), c)
#define FILL2D(A, c) fill(A[0], A[0] + sizeof(A) / sizeof(**A), c)
#define FILL3D(A, c) fill(A[0][0], A[0][0] + sizeof(A) / sizeof(***A), c)
#define REP(i, m) for (int i = 0; i < (m); ++i)
#define REPN(i, m, in) for (int i = (in); i < (m); ++i)
#define IN(a, x, b) (a <= x && x < b)
#define PB push_back
using namespace std;
using LL = long long;
using PAIR = pair<int, int>;
using PAIRLL = pair<LL, LL>;
using VI = vector<int>;
using VVI = vector<VI>;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
#ifdef DEB1
#define DPRINT(x) cout << #x << "=" << (x) << endl; // print at debug mode
#else
#define DPRINT(x) ;
#endif
#ifdef DEB1
#define DCHECK(x) cout << "debug check" << #x << endl;
#else
#define DCHECK(x) ;
#endif
/*
constant values
*/
// const int dx[] = {1,0,-1,0};
// const int dy[] = {0,1,0,-1};
// const int MX = 31001;
// const LL INF= 1LL<<60 ;//INF = 1001001001001; //1e9
// Main program
void solve() {
string S;
cin >> S;
int size = S.size();
DPRINT(size)
if (size == 19) {
cout << "0 0 1000000000 0 0 1000000000 " << endl;
return;
}
if (size < 10) {
cout << "0 0 1 0 0 " + S << endl;
return;
} else {
string left, right;
right = S.substr(size - 9, 9);
left = S.substr(0, size - 9);
DPRINT(left)
LL iright = stoi(right);
LL ileft = stoi(left);
LL b1 = 1000000000 - iright;
LL b2 = ileft + 1;
cout << "0 0 1000000000 1 " << b1 << " " << b2 << endl;
// LL check;
// check = 1000000000 - b1;
// DPRINT(check)
}
}
// ----------------------------------------------
int main() {
#ifdef DEB2
cout << "DEBUG MODE" << endl;
ifstream in("input.txt"); // for debug
cin.rdbuf(in.rdbuf()); // for debug
#endif
int T = 1;
for (int i = 0; i < T; ++i) {
solve();
}
} | replace | 86 | 87 | 86 | 87 | 0 | |
p02963 | C++ | Runtime Error | // -----------------------------------
// Author : MatsuTaku
// Affiliation: Tokushima University
// Country : Japan
// Date : 04/07/2020
// -----------------------------------
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
ll s;
cin >> s;
if (s >= (ll)1e18 / 2) {
ll d = (ll)1e18 - s;
ll a;
for (ll i = sqrt(d); i >= 1; i++) {
if (d % i == 0)
a = i;
}
cout << 0 << " " << 0 << " " << (ll)1e9 << " " << s / a << " " << a << " "
<< (ll)1e9 << endl;
} else {
ll a;
for (ll i = sqrt(s); i >= 1; i++) {
if (s % i == 0)
a = i;
}
cout << 0 << " " << 0 << " " << a << " " << 0 << " " << 0 << " " << s / a
<< endl;
}
return 0;
}
| // -----------------------------------
// Author : MatsuTaku
// Affiliation: Tokushima University
// Country : Japan
// Date : 04/07/2020
// -----------------------------------
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
ll s;
cin >> s;
ll a = (s - 1) / (ll)1e9 + 1;
cout << 0 << " " << 0 << " " << (ll)1e9 << " " << 1 << " "
<< ((ll)1e9 * a - s) << " " << a << endl;
return 0;
}
| replace | 18 | 36 | 18 | 21 | TLE | |
p02963 | C++ | Runtime Error | /* Dsingh_24 */
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define endl '\n'
#define pii pair<ll int, ll int>
#define vi vector<ll int>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (ll int)x.size()
#define hell 1000000007
#define rep(i, a, b) for (ll int i = a; i < b; i++)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
#define ios \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
using namespace std;
#define N 100005
int main() {
ios ll s, i, j;
cin >> s;
ll hi = min(s, (long long)1e9), lo = 1, mid;
while (hi - lo > 1) {
mid = (hi + lo) / 2;
ll y = (s + mid - 1) / mid;
if (y > 1e9)
lo = mid;
else
hi = mid;
}
ll x2, y2;
if ((s + mid - 1) / mid < 1e9)
x2 = mid, y2 = (s + mid - 1) / mid;
else
x2 = hi, y2 = (s + hi - 1) / hi;
// ll x2=mid,y2=(s+mid-1)/mid;
ll req = x2 * y2 - s;
if (req == 0) {
cout << "0 0 0 " << y2 << " " << x2 << " 0";
return 0;
}
for (i = 1; i * i <= req; i++) {
if (req % i == 0 && (req / i) < 1e9) {
cout << "0 0 " << x2 << " " << i << " " << (req / i) << " " << y2;
return 0;
}
}
return 0;
} | /* Dsingh_24 */
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define endl '\n'
#define pii pair<ll int, ll int>
#define vi vector<ll int>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (ll int)x.size()
#define hell 1000000007
#define rep(i, a, b) for (ll int i = a; i < b; i++)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search
#define mp make_pair
#define ios \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
using namespace std;
#define N 100005
int main() {
ios ll s, i, j;
cin >> s;
if (s == 1) {
cout << "0 0 0 1 1 0";
return 0;
}
ll hi = min(s, (long long)1e9), lo = 1, mid;
while (hi - lo > 1) {
mid = (hi + lo) / 2;
ll y = (s + mid - 1) / mid;
if (y > 1e9)
lo = mid;
else
hi = mid;
}
ll x2, y2;
if ((s + mid - 1) / mid < 1e9)
x2 = mid, y2 = (s + mid - 1) / mid;
else
x2 = hi, y2 = (s + hi - 1) / hi;
// ll x2=mid,y2=(s+mid-1)/mid;
ll req = x2 * y2 - s;
if (req == 0) {
cout << "0 0 0 " << y2 << " " << x2 << " 0";
return 0;
}
for (i = 1; i * i <= req; i++) {
if (req % i == 0 && (req / i) < 1e9) {
cout << "0 0 " << x2 << " " << i << " " << (req / i) << " " << y2;
return 0;
}
}
return 0;
} | insert | 29 | 29 | 29 | 33 | 0 | |
p02963 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <vector>
// #include<bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
ll S;
cin >> S;
int yaku;
for (ll i = 1; i * i <= S; i++) {
if (S % i == 0) {
yaku = i;
}
}
cout << "0 0 0 " << yaku << " " << S / yaku << " 0" << endl;
return 0;
}
| #include <algorithm>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <vector>
// #include<bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
long long s;
cin >> s;
const int v = 1000000000;
int x = (v - (s % v)) % v;
int y = (s + x) / v;
cout << "0 0 1000000000 1 " << x << " " << y << endl;
}
| replace | 15 | 25 | 15 | 24 | TLE | |
p02963 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
constexpr ll infl = 10000000000000007LL;
constexpr int inf = 1000000007;
int main() {
ll s;
cin >> s;
ll k = 0;
while (true) {
ll t = s + k;
for (ll i = 1; i * i <= t; ++i) {
if (t % i == 0) {
ll u = t / i;
if (u <= 1000000000LL) {
cout << 0 << " ";
cout << 0 << " ";
cout << i << " ";
cout << u << " ";
cout << 1 << " ";
cout << k << " ";
return 0;
}
}
}
++k;
}
return 0;
} | #include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
constexpr ll infl = 10000000000000007LL;
constexpr int inf = 1000000007;
int main() {
ll s;
cin >> s;
if (s == 1000000000000000000LL) {
cout << 0 << " ";
cout << 0 << " ";
cout << 0 << " ";
cout << 1000000000 << " ";
cout << 1000000000 << " ";
cout << 0 << " ";
} else {
ll a = (ll)(sqrt(s)) + 1;
ll b = a * a - s;
cout << 0 << " ";
cout << 0 << " ";
cout << a << " ";
cout << b << " ";
cout << 1 << " ";
cout << a << " ";
}
return 0;
} | replace | 22 | 40 | 22 | 38 | TLE | |
p02963 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vi = vector<int>;
using vb = vector<bool>;
using pll = pair<ll, ll>;
using pii = pair<int, int>;
using vpii = vector<pii>;
using vpll = vector<pll>;
const ll LINF = 1ll << 55;
const ll INF = 0x3f3f3f3f;
const ll MOD = 1e9 + 7;
const int MAXN = 1e5 + 10;
const ll dx[] = {1, 0, -1, 0};
const ll dy[] = {0, 1, 0, -1};
/// cin/cout overloading
template <typename T> ostream &operator<<(ostream &out, vector<T> &vec) {
for (auto it = vec.begin(); it != vec.end(); ++it) {
out << *it << " ";
}
return out;
}
template <typename T> ostream &operator<<(ostream &out, pair<T, T> &P) {
out << P.first << " " << P.second;
return out;
}
template <typename T> istream &operator>>(istream &in, vector<T> &vec) {
for (auto it = vec.begin(); it != vec.end(); ++it) {
in >> *it;
}
return in;
}
template <typename T> istream &operator>>(istream &in, pair<T, T> &P) {
in >> P.first >> P.second;
return in;
}
/// 图相关
ll N, M;
//
// struct Edge {
// ll to;
// ll weight;
// ll next;
// bool operator<(const Edge& rhs) {
// return weight < rhs.weight;
// }
// };
struct Edge2 {
ll u, v, w;
Edge2(ll u_, ll v_, ll w_) : u(u_), v(v_), w(w_) {}
Edge2() : u(0ll), v(0ll), w(0ll) {}
bool operator<(const Edge2 &rhs) { return w < rhs.w; }
};
// vector<Edge> edges(MAXN);
// vector<ll> head(MAXN, -1);
// vector<int> matchingx(MAXN, -1);
// vector<int> matchingy(MAXN, -1);
// vector<bool> check(MAXN);
// vector<ll> dis(MAXN);
// vector<bool> vis(MAXN, false);
//
// ll cnt = 1;
//
// void addEdge(ll from, ll to, ll weight) {
// edges[cnt].to = to;
// edges[cnt].weight = weight;
// edges[cnt].next = head[from];
// head[from] = cnt++;
//}
//
// bool dfs(int u) {
// for (int i = head[u]; i != -1; i = edges[i].next) {
// int v = edges[i].to;
// if (!check[v]) {
// check[v] = true;
// if (matchingy[v] == -1 || dfs(matchingy[v])) {
// matchingy[v] = u;
// matchingx[u] = v;
// return true;
// }
// }
// }
// return false;
//}
//
// int hungarian() {
// int ans = 0;
// fill(matchingx.begin(), matchingx.end(), -1);
// fill(matchingy.begin(), matchingy.end(), -1);
// for (int u = 1; u <= N; ++u) {
//// if (matchingx[u] == -1) {
// {
// fill(check.begin(), check.end(), false);
// if (dfs(u)) {
// ++ans;
// }
// }
// }
// return ans;
//}
//
// void dijkstra(const ll s) {
// priority_queue<P, vector<P>, greater<P>> que;
// fill(dis.begin(), dis.end(), INF);
// dis[s] = 0;
// que.push(P(0, s));
//////multiple sources
//// for (auto& x : shops) {
//// dis[x] = 0;
//// que.push(P(0, x));
//// }
// while (!que.empty()) {
// P p = que.top();
// que.pop();
//// cout << "pop " << p.second << endl;
// int u = p.second;
// if (dis[u] < p.first) continue;
// for (int i = head[u]; i != -1; i = edges[i].next) {
// int v = edges[i].to;
// if (dis[v] > dis[u] + edges[i].weight) {
// dis[v] = dis[u] + edges[i].weight;
//// cout << "push " << v << endl;
// que.push(P(dis[v], v));
// }
// }
// }
//}
// void zeroOneBFS(const int s) {
// deque<P> que;
// fill(dis.begin(), dis.end(), INF);
// dis[s] = 0;
// que.push_front(P(0, s));
// while (!que.empty()) {
// P p = que.front();
// que.pop_front();
// int u = p.second;
// if (dis[u] < p.first) continue;
// for (int i = head[u]; i != -1; i = edges[i].next) {
// int v = edges[i].to;
// if (dis[v] > dis[u] + edges[i].weight) {
// dis[v] = dis[u] + edges[i].weight;
// if (edges[i].weight) {
// que.push_back(P(dis[v], v));
// } else {
// que.push_front(P(dis[v], v));
// }
// }
// }
// }
// }
// Union-Find 并查集
class UnionFind {
vector<ll> par;
public:
explicit UnionFind(ll n) : par(n, -1) {}
ll root(ll a) {
if (par[a] < 0) {
return a;
}
return par[a] = root(par[a]);
}
ll size(ll a) { return -par[root(a)]; }
void unite(ll a, ll b) {
a = root(a);
b = root(b);
if (a != b) {
if (size(a) < size(b)) {
swap(a, b);
}
par[a] += par[b];
par[b] = a;
}
}
};
ll kruskal(vector<Edge2> &edges2, const ll V) {
sort(edges2.begin(), edges2.end());
UnionFind uf(V + 10);
ll res = 0;
Edge2 e;
for (int i = 0; i < edges2.size(); ++i) {
e = edges2[i];
if (uf.root(e.u) != uf.root(e.v)) {
uf.unite(e.u, e.v);
res += e.w;
}
}
return res;
}
/// 线段树
// struct SegmentTreeNode {
// ll maxVal;
// ll minVal;
// ll sum;
// ll len;
// ll lazy;
// ll left, right;
// SegmentTreeNode() {}
// };
//
// class SegmentTree {
// public:
// explicit SegmentTree(size_t size, const vll& nums) : tree(size << 2),
// nums(nums) {
//
// }
// void build(ll root, ll left, ll right) {
// tree[root].lazy = 0;
// tree[root].left = left;
// tree[root].right = right;
// tree[root].len = right - left + 1;
// if (left == right) {
// tree[root].maxVal = nums[left];
// tree[root].minVal = nums[left];
// tree[root].sum = nums[left];
// } else {
// ll mid = (right - left) / 2 + left;
// build(root * 2, left, mid);
// build(root * 2 + 1, mid + 1, right);
// tree[root].minVal = min(tree[root * 2].minVal, tree[root * 2 +
// 1].minVal); tree[root].maxVal = max(tree[root * 2].maxVal,
// tree[root * 2 + 1].maxVal); tree[root].sum = tree[root * 2].sum +
// tree[root * 2 + 1].sum;
// }
// }
//
// void pushup(ll root) {
// tree[root].minVal = min(tree[root * 2].minVal, tree[root * 2 +
// 1].minVal); tree[root].maxVal = max(tree[root * 2].maxVal, tree[root
// * 2 + 1].maxVal); tree[root].sum = tree[root * 2].sum + tree[root * 2
// + 1].sum;
// }
////// add single node val
// void add(ll root, ll id, ll addVal) {
// if (tree[root].left == tree[root].right) {
// tree[root].maxVal += addVal;
// tree[root].minVal += addVal;
// tree[root].sum += addVal;
// return;
// }
// ll mid = (tree[root].right - tree[root].left) / 2 + tree[root].left;
// if (id <= mid) {
// add(root * 2, id, addVal);
// } else {
// add(root * 2 + 1, id, addVal);
// }
// pushup(root);
// }
//
// void pushdown(ll root) {
// if (tree[root].lazy) {
// tree[root * 2].lazy += tree[root].lazy;
// tree[root * 2 + 1].lazy += tree[root].lazy;
// tree[root * 2].sum += tree[root * 2].len * tree[root].lazy;
// tree[root * 2 + 1].sum += tree[root * 2 + 1].len *
// tree[root].lazy; tree[root * 2].maxVal += tree[root].lazy;
// tree[root * 2 + 1].maxVal += tree[root].lazy;
// tree[root * 2].minVal += tree[root].lazy;
// tree[root * 2 + 1].minVal += tree[root].lazy;
// tree[root].lazy = 0;
// }
// }
// //// query range sum
// ll querySum(ll root, ll left, ll right) {
// if (tree[root].left >= left && tree[root].right <= right) {
// return tree[root].sum;
// }
// if (tree[root].left > right || tree[root].right < left) {
// return 0;
// }
// if (tree[root].lazy) {
// pushdown(root);
// }
// return querySum(root * 2, left, right) + querySum(root * 2 + 1, left,
// right);
// }
// //// query range max/min
// ll queryMax(ll root, ll left, ll right) {
// if (tree[root].left >= left && tree[root].right <= right) {
// return tree[root].maxVal;
// }
// if (tree[root].left > right || tree[root].right < left) {
// return -INF;
// }
// if (tree[root].lazy) {
// pushdown(root);
// }
// return max(queryMax(root * 2, left, right), queryMax(root * 2 + 1,
// left, right));
// }
//
// ll queryMin(ll root, ll left, ll right) {
// if (tree[root].left >= left && tree[root].right <= right) {
// return tree[root].minVal;
// }
// if (tree[root].left > right || tree[root].right < left) {
// return INF;
// }
// if (tree[root].lazy) {
// pushdown(root);
// }
// return min(queryMin(root * 2, left, right), queryMin(root * 2 + 1,
// left, right));
// }
// //// add range val
// void update(ll root, ll left, ll right, ll addVal) {
// if (tree[root].left >= left && tree[root].right <= right) {
// tree[root].lazy += addVal;
// tree[root].sum += tree[root].len * addVal;
// tree[root].maxVal += addVal;
// tree[root].minVal += addVal;
// return;
// }
// if (tree[root].left > right || tree[root].right < left) {
// return;
// }
// if (tree[root].lazy) {
// pushdown(root);
// }
// update(root * 2, left, right, addVal);
// update(root * 2 + 1, left, right, addVal);
// pushup(root);
// }
//
//
// private:
// vector<SegmentTreeNode> tree;
// const vll &nums;
//};
/// 组合数
////約数求める //約数
// void divisor(ll n, vector<ll> &ret) {
// for (ll i = 1; i * i <= n; i++) {
// if (n % i == 0) {
// ret.push_back(i);
// if (i * i != n) ret.push_back(n / i);
// }
// }
// sort(ret.begin(), ret.end());
// }
////階乗
// ll factorial(ll n, ll mod) {
// ll ret = 1;
// for (ll i = 1; i <= n; i++) {
// ret = (ret * i) % mod;
// }
// return ret;
// }
////モジュラ逆数
// ll inv_mod(ll n, ll mod) {
// ll a = n % mod, b = mod - 2, ret = 1;
// while (b > 0) {
// if (b & 1) ret = (ret * a) % mod;
// a = (a * a) % mod;
// b >>= 1;
// }
// return ret;
// }
//
// ll nPr(ll n, ll r, ll mod) {
// ll ret = 1;
// for (ll i = n; i >= n - r + 1; i--) {
// ret = ret * (i % mod) % mod;
// }
// return ret;
// }
//
// ll nCr(ll n, ll r, ll mod) {
// return nPr(n, r, mod) * inv_mod(factorial(r, mod), mod) % mod;
// }
//
// vll F(MAXN), Finv(MAXN), inv(MAXN);
//
// ll pow_mod(ll a, ll b, ll p) {
// ll ret = 1;
// while (b) {
// if (b & 1) ret = (ret * a) % p;
// a = (a * a) % p;
// b >>= 1;
// }
// return ret;
// }
//
// void comb_init() {
// inv[1] = 1;
// for (int i = 2; i < MAXN; ++i) {
// inv[i] = (MOD - MOD / i) * 1ll * inv[MOD % i] % MOD;
// }
// F[0] = Finv[0] = 1;
// for (int i = 1; i < MAXN; ++i) {
// F[i] = F[i-1] * 1ll * i % MOD;
// Finv[i] = Finv[i-1] * 1ll * inv[i] % MOD;
// }
// }
//
// inline ll comb(ll n, ll m) {
// if (m < 0 || m > n) return 0;
// return F[n] * 1ll * Finv[n - m] % MOD * Finv[m] % MOD;
// }
//
inline ll add_mod(ll a, ll b) { return (a + b) % MOD; }
inline ll mul_mod(ll a, ll b) { return a * b % MOD; }
inline ll sub_mod(ll a, ll b) { return (a + MOD - b) % MOD; }
int getDistance(const vi &x, const vi &y) {
int res = 0;
for (int i = 0; i < x.size(); ++i) {
res += (x[i] - y[i]) * (x[i] - y[i]);
}
return res;
}
/// main函数
int main() {
ll S;
cin >> S;
ll x, y;
ll x1 = 0;
ll y1 = 0;
ll &x2 = x;
ll y2 = 0;
ll &x3 = x;
ll &y3 = y;
for (ll i = 1; i <= sqrt(S); ++i) {
if (S % i == 0) {
x = i;
y = S / i;
}
}
cout << x1 << " " << y1 << " " << x2 << " " << y2 << " " << x3 << " " << y3
<< endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vi = vector<int>;
using vb = vector<bool>;
using pll = pair<ll, ll>;
using pii = pair<int, int>;
using vpii = vector<pii>;
using vpll = vector<pll>;
const ll LINF = 1ll << 55;
const ll INF = 0x3f3f3f3f;
const ll MOD = 1e9 + 7;
const int MAXN = 1e5 + 10;
const ll dx[] = {1, 0, -1, 0};
const ll dy[] = {0, 1, 0, -1};
/// cin/cout overloading
template <typename T> ostream &operator<<(ostream &out, vector<T> &vec) {
for (auto it = vec.begin(); it != vec.end(); ++it) {
out << *it << " ";
}
return out;
}
template <typename T> ostream &operator<<(ostream &out, pair<T, T> &P) {
out << P.first << " " << P.second;
return out;
}
template <typename T> istream &operator>>(istream &in, vector<T> &vec) {
for (auto it = vec.begin(); it != vec.end(); ++it) {
in >> *it;
}
return in;
}
template <typename T> istream &operator>>(istream &in, pair<T, T> &P) {
in >> P.first >> P.second;
return in;
}
/// 图相关
ll N, M;
//
// struct Edge {
// ll to;
// ll weight;
// ll next;
// bool operator<(const Edge& rhs) {
// return weight < rhs.weight;
// }
// };
struct Edge2 {
ll u, v, w;
Edge2(ll u_, ll v_, ll w_) : u(u_), v(v_), w(w_) {}
Edge2() : u(0ll), v(0ll), w(0ll) {}
bool operator<(const Edge2 &rhs) { return w < rhs.w; }
};
// vector<Edge> edges(MAXN);
// vector<ll> head(MAXN, -1);
// vector<int> matchingx(MAXN, -1);
// vector<int> matchingy(MAXN, -1);
// vector<bool> check(MAXN);
// vector<ll> dis(MAXN);
// vector<bool> vis(MAXN, false);
//
// ll cnt = 1;
//
// void addEdge(ll from, ll to, ll weight) {
// edges[cnt].to = to;
// edges[cnt].weight = weight;
// edges[cnt].next = head[from];
// head[from] = cnt++;
//}
//
// bool dfs(int u) {
// for (int i = head[u]; i != -1; i = edges[i].next) {
// int v = edges[i].to;
// if (!check[v]) {
// check[v] = true;
// if (matchingy[v] == -1 || dfs(matchingy[v])) {
// matchingy[v] = u;
// matchingx[u] = v;
// return true;
// }
// }
// }
// return false;
//}
//
// int hungarian() {
// int ans = 0;
// fill(matchingx.begin(), matchingx.end(), -1);
// fill(matchingy.begin(), matchingy.end(), -1);
// for (int u = 1; u <= N; ++u) {
//// if (matchingx[u] == -1) {
// {
// fill(check.begin(), check.end(), false);
// if (dfs(u)) {
// ++ans;
// }
// }
// }
// return ans;
//}
//
// void dijkstra(const ll s) {
// priority_queue<P, vector<P>, greater<P>> que;
// fill(dis.begin(), dis.end(), INF);
// dis[s] = 0;
// que.push(P(0, s));
//////multiple sources
//// for (auto& x : shops) {
//// dis[x] = 0;
//// que.push(P(0, x));
//// }
// while (!que.empty()) {
// P p = que.top();
// que.pop();
//// cout << "pop " << p.second << endl;
// int u = p.second;
// if (dis[u] < p.first) continue;
// for (int i = head[u]; i != -1; i = edges[i].next) {
// int v = edges[i].to;
// if (dis[v] > dis[u] + edges[i].weight) {
// dis[v] = dis[u] + edges[i].weight;
//// cout << "push " << v << endl;
// que.push(P(dis[v], v));
// }
// }
// }
//}
// void zeroOneBFS(const int s) {
// deque<P> que;
// fill(dis.begin(), dis.end(), INF);
// dis[s] = 0;
// que.push_front(P(0, s));
// while (!que.empty()) {
// P p = que.front();
// que.pop_front();
// int u = p.second;
// if (dis[u] < p.first) continue;
// for (int i = head[u]; i != -1; i = edges[i].next) {
// int v = edges[i].to;
// if (dis[v] > dis[u] + edges[i].weight) {
// dis[v] = dis[u] + edges[i].weight;
// if (edges[i].weight) {
// que.push_back(P(dis[v], v));
// } else {
// que.push_front(P(dis[v], v));
// }
// }
// }
// }
// }
// Union-Find 并查集
class UnionFind {
vector<ll> par;
public:
explicit UnionFind(ll n) : par(n, -1) {}
ll root(ll a) {
if (par[a] < 0) {
return a;
}
return par[a] = root(par[a]);
}
ll size(ll a) { return -par[root(a)]; }
void unite(ll a, ll b) {
a = root(a);
b = root(b);
if (a != b) {
if (size(a) < size(b)) {
swap(a, b);
}
par[a] += par[b];
par[b] = a;
}
}
};
ll kruskal(vector<Edge2> &edges2, const ll V) {
sort(edges2.begin(), edges2.end());
UnionFind uf(V + 10);
ll res = 0;
Edge2 e;
for (int i = 0; i < edges2.size(); ++i) {
e = edges2[i];
if (uf.root(e.u) != uf.root(e.v)) {
uf.unite(e.u, e.v);
res += e.w;
}
}
return res;
}
/// 线段树
// struct SegmentTreeNode {
// ll maxVal;
// ll minVal;
// ll sum;
// ll len;
// ll lazy;
// ll left, right;
// SegmentTreeNode() {}
// };
//
// class SegmentTree {
// public:
// explicit SegmentTree(size_t size, const vll& nums) : tree(size << 2),
// nums(nums) {
//
// }
// void build(ll root, ll left, ll right) {
// tree[root].lazy = 0;
// tree[root].left = left;
// tree[root].right = right;
// tree[root].len = right - left + 1;
// if (left == right) {
// tree[root].maxVal = nums[left];
// tree[root].minVal = nums[left];
// tree[root].sum = nums[left];
// } else {
// ll mid = (right - left) / 2 + left;
// build(root * 2, left, mid);
// build(root * 2 + 1, mid + 1, right);
// tree[root].minVal = min(tree[root * 2].minVal, tree[root * 2 +
// 1].minVal); tree[root].maxVal = max(tree[root * 2].maxVal,
// tree[root * 2 + 1].maxVal); tree[root].sum = tree[root * 2].sum +
// tree[root * 2 + 1].sum;
// }
// }
//
// void pushup(ll root) {
// tree[root].minVal = min(tree[root * 2].minVal, tree[root * 2 +
// 1].minVal); tree[root].maxVal = max(tree[root * 2].maxVal, tree[root
// * 2 + 1].maxVal); tree[root].sum = tree[root * 2].sum + tree[root * 2
// + 1].sum;
// }
////// add single node val
// void add(ll root, ll id, ll addVal) {
// if (tree[root].left == tree[root].right) {
// tree[root].maxVal += addVal;
// tree[root].minVal += addVal;
// tree[root].sum += addVal;
// return;
// }
// ll mid = (tree[root].right - tree[root].left) / 2 + tree[root].left;
// if (id <= mid) {
// add(root * 2, id, addVal);
// } else {
// add(root * 2 + 1, id, addVal);
// }
// pushup(root);
// }
//
// void pushdown(ll root) {
// if (tree[root].lazy) {
// tree[root * 2].lazy += tree[root].lazy;
// tree[root * 2 + 1].lazy += tree[root].lazy;
// tree[root * 2].sum += tree[root * 2].len * tree[root].lazy;
// tree[root * 2 + 1].sum += tree[root * 2 + 1].len *
// tree[root].lazy; tree[root * 2].maxVal += tree[root].lazy;
// tree[root * 2 + 1].maxVal += tree[root].lazy;
// tree[root * 2].minVal += tree[root].lazy;
// tree[root * 2 + 1].minVal += tree[root].lazy;
// tree[root].lazy = 0;
// }
// }
// //// query range sum
// ll querySum(ll root, ll left, ll right) {
// if (tree[root].left >= left && tree[root].right <= right) {
// return tree[root].sum;
// }
// if (tree[root].left > right || tree[root].right < left) {
// return 0;
// }
// if (tree[root].lazy) {
// pushdown(root);
// }
// return querySum(root * 2, left, right) + querySum(root * 2 + 1, left,
// right);
// }
// //// query range max/min
// ll queryMax(ll root, ll left, ll right) {
// if (tree[root].left >= left && tree[root].right <= right) {
// return tree[root].maxVal;
// }
// if (tree[root].left > right || tree[root].right < left) {
// return -INF;
// }
// if (tree[root].lazy) {
// pushdown(root);
// }
// return max(queryMax(root * 2, left, right), queryMax(root * 2 + 1,
// left, right));
// }
//
// ll queryMin(ll root, ll left, ll right) {
// if (tree[root].left >= left && tree[root].right <= right) {
// return tree[root].minVal;
// }
// if (tree[root].left > right || tree[root].right < left) {
// return INF;
// }
// if (tree[root].lazy) {
// pushdown(root);
// }
// return min(queryMin(root * 2, left, right), queryMin(root * 2 + 1,
// left, right));
// }
// //// add range val
// void update(ll root, ll left, ll right, ll addVal) {
// if (tree[root].left >= left && tree[root].right <= right) {
// tree[root].lazy += addVal;
// tree[root].sum += tree[root].len * addVal;
// tree[root].maxVal += addVal;
// tree[root].minVal += addVal;
// return;
// }
// if (tree[root].left > right || tree[root].right < left) {
// return;
// }
// if (tree[root].lazy) {
// pushdown(root);
// }
// update(root * 2, left, right, addVal);
// update(root * 2 + 1, left, right, addVal);
// pushup(root);
// }
//
//
// private:
// vector<SegmentTreeNode> tree;
// const vll &nums;
//};
/// 组合数
////約数求める //約数
// void divisor(ll n, vector<ll> &ret) {
// for (ll i = 1; i * i <= n; i++) {
// if (n % i == 0) {
// ret.push_back(i);
// if (i * i != n) ret.push_back(n / i);
// }
// }
// sort(ret.begin(), ret.end());
// }
////階乗
// ll factorial(ll n, ll mod) {
// ll ret = 1;
// for (ll i = 1; i <= n; i++) {
// ret = (ret * i) % mod;
// }
// return ret;
// }
////モジュラ逆数
// ll inv_mod(ll n, ll mod) {
// ll a = n % mod, b = mod - 2, ret = 1;
// while (b > 0) {
// if (b & 1) ret = (ret * a) % mod;
// a = (a * a) % mod;
// b >>= 1;
// }
// return ret;
// }
//
// ll nPr(ll n, ll r, ll mod) {
// ll ret = 1;
// for (ll i = n; i >= n - r + 1; i--) {
// ret = ret * (i % mod) % mod;
// }
// return ret;
// }
//
// ll nCr(ll n, ll r, ll mod) {
// return nPr(n, r, mod) * inv_mod(factorial(r, mod), mod) % mod;
// }
//
// vll F(MAXN), Finv(MAXN), inv(MAXN);
//
// ll pow_mod(ll a, ll b, ll p) {
// ll ret = 1;
// while (b) {
// if (b & 1) ret = (ret * a) % p;
// a = (a * a) % p;
// b >>= 1;
// }
// return ret;
// }
//
// void comb_init() {
// inv[1] = 1;
// for (int i = 2; i < MAXN; ++i) {
// inv[i] = (MOD - MOD / i) * 1ll * inv[MOD % i] % MOD;
// }
// F[0] = Finv[0] = 1;
// for (int i = 1; i < MAXN; ++i) {
// F[i] = F[i-1] * 1ll * i % MOD;
// Finv[i] = Finv[i-1] * 1ll * inv[i] % MOD;
// }
// }
//
// inline ll comb(ll n, ll m) {
// if (m < 0 || m > n) return 0;
// return F[n] * 1ll * Finv[n - m] % MOD * Finv[m] % MOD;
// }
//
inline ll add_mod(ll a, ll b) { return (a + b) % MOD; }
inline ll mul_mod(ll a, ll b) { return a * b % MOD; }
inline ll sub_mod(ll a, ll b) { return (a + MOD - b) % MOD; }
int getDistance(const vi &x, const vi &y) {
int res = 0;
for (int i = 0; i < x.size(); ++i) {
res += (x[i] - y[i]) * (x[i] - y[i]);
}
return res;
}
/// main函数
int main() {
ll S;
cin >> S;
int v = 1e9;
int y = (v - S % v) % v;
int x = (S + y) / v;
cout << "0 0 1 1000000000 " << x << " " << y << endl;
return 0;
} | replace | 437 | 452 | 437 | 441 | TLE | |
p02963 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
using ll = long long;
static const ll mod = 1e9 + 7;
static ll INF = 1e9;
using namespace std;
int main() {
ll s;
cin >> s;
ll a, b;
ll l = 0, r = s;
ll mid = (l + r) / 2;
ll xa, ya, xb, yb, xc, yc;
bool judge = false;
if (s < 1e9)
for (ll i = 0; i <= INF; ++i) {
if (judge)
break;
a = s + i;
if (a <= INF * INF) {
for (ll j = 1; j <= INF; ++j) {
if ((a % j == 0) && (a / j <= 1e9)) {
xb = j;
yc = a / j;
judge = true;
break;
}
}
}
}
else
for (ll i = INF; i >= 0; --i) {
if (judge)
break;
a = s + i;
if (a <= INF * INF) {
for (ll j = INF; j >= 1; --j) {
if ((a % j == 0) && (a / j <= 1e9)) {
xb = j;
yc = a / j;
judge = true;
break;
}
}
}
if (a > s / 2)
l = mid + 1;
else
r = mid;
i = (l + r) / 2;
}
xa = 0;
ya = 0;
b = a - s;
l = 0;
r = b;
mid = (r + l) / 2;
for (ll j = 1e9; j >= 1; --j) {
if (b % j == 0 && b / j <= 1e9) {
xc = j;
yb = b / j;
break;
}
if (b > s / 2)
l = mid + 1;
else
r = mid;
j = (l + r) / 2;
}
cout << xa << " " << ya << " " << xb << " " << yb << " " << xc << " " << yc
<< endl;
return 0;
}
| #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
using ll = long long;
static const ll mod = 1e9 + 7;
static ll INF = 1e9;
using namespace std;
int main() {
ll s;
cin >> s;
ll xa = 0, ya = 0, xb = 1e9, yb, xc = 1, yc;
// xb*yc-xc*yb=s
yc = s / INF; // INFで割るため
yb = INF * yc - s;
if (yb < 0) {
yc++;
yb = INF * yc - s;
}
cout << xa << " " << ya << " " << xb << " " << yb << " " << xc << " " << yc
<< endl;
return 0;
}
| replace | 27 | 88 | 27 | 34 | TLE | |
p02963 | C++ | Time Limit Exceeded | #include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
typedef long long LL;
typedef vector<int> VI;
#define UB 1000000000
int main() {
int64_t s;
cin >> s;
int i = 1;
while (true) {
if (s <= i * i) {
break;
}
i++;
}
int64_t x2 = i;
int64_t y3 = i;
while (x2 * y3 - s > UB) {
x2--;
}
int64_t x3 = x2 * y3 - s;
int64_t y2 = 1;
cout << 0 << " " << 0 << " " << x2 << " " << y2 << " " << x3 << " " << y3
<< endl;
return 0;
}
| #include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
typedef long long LL;
typedef vector<int> VI;
#define UB 1000000000
int main() {
int64_t s;
cin >> s;
int64_t i = (int64_t)ceil(sqrt(s));
int64_t x2 = i;
int64_t y3 = i;
while (x2 * y3 - s > UB) {
x2--;
}
int64_t x3 = x2 * y3 - s;
int64_t y2 = 1;
cout << 0 << " " << 0 << " " << x2 << " " << y2 << " " << x3 << " " << y3
<< endl;
return 0;
}
| replace | 22 | 29 | 22 | 23 | TLE | |
p02963 | C++ | Time Limit Exceeded | #include <iostream>
using namespace std;
int main() {
long long int S;
cin >> S;
int xy[6];
xy[0] = 0;
xy[1] = 0;
bool flag = 0;
for (int i = S / 2; i <= 1000000000; i++) {
for (int j = 0; j <= 1000000000; j++) {
for (int k = 0; k <= 1000000000; k++) {
for (int h = S / 2; h <= 1000000000; h++) {
if (i * h - k * j == S) {
xy[2] = i;
xy[3] = j;
xy[4] = k;
xy[5] = h;
flag = 1;
break;
}
if (flag)
break;
}
if (flag)
break;
}
if (flag)
break;
}
if (flag)
break;
}
for (int i = 0; i < 5; i++) {
cout << xy[i] << " ";
}
cout << xy[5] << endl;
}
| #include <iostream>
using namespace std;
int main() {
long long int S;
cin >> S;
int xy[6];
xy[0] = 0;
xy[1] = 0;
xy[2] = 1000000000;
xy[3] = 1;
xy[5] = S / 1000000000 + 1;
xy[4] = 1000000000 - S % 1000000000;
if (S == 1000000000000000000) {
xy[3] = 0;
xy[5] = 1000000000;
}
for (int i = 0; i < 5; i++) {
cout << xy[i] << " ";
}
cout << xy[5] << endl;
}
| replace | 9 | 33 | 9 | 16 | TLE | |
p02963 | C++ | Runtime Error | #include <bits/stdc++.h>
#define cs const
#define pb push_back
#define y1 y_1
using namespace std;
typedef long long ll;
int main() {
#ifdef FSYolanda
freopen("1.in", "r", stdin);
#endif
ll S;
scanf("%lld", &S);
ll t = sqrt(S);
if (1ll * t * t == S) {
cout << 0 << " " << 0 << " " << t << " " << 0 << " " << 0 << " " << t
<< '\n';
return 0;
}
++t;
ll x2 = t, y1 = t;
ll r = t * t - S;
if (r & 1)
++x2, --y1, --r;
ll x1 = 2, y2 = r >> 1;
assert(x1 <= 1e9);
assert(x2 <= 1e9);
assert(y1 <= 1e9);
assert(y2 <= 1e9);
cout << 0 << " " << 0 << " " << x1 << " " << y1 << " " << x2 << " " << y2;
return 0;
} | #include <bits/stdc++.h>
#define cs const
#define pb push_back
#define y1 y_1
using namespace std;
typedef long long ll;
int main() {
#ifdef FSYolanda
freopen("1.in", "r", stdin);
#endif
ll S;
scanf("%lld", &S);
ll t = sqrt(S);
if (1ll * t * t == S) {
cout << 0 << " " << 0 << " " << t << " " << 0 << " " << 0 << " " << t
<< '\n';
return 0;
}
++t;
ll x2 = t, y1 = t;
ll r = t * t - S;
ll x1 = 1, y2 = r;
if (t < 1e9) {
if (r & 1)
++x2, --y1, --r;
x1 = 2, y2 = r >> 1;
cout << 0 << " " << 0 << " " << x1 << " " << y1 << " " << x2 << " " << y2;
return 0;
} else {
if (r <= 1e9) {
cout << 0 << " " << 0 << " " << x1 << " " << y1 << " " << x2 << " " << y2;
return 0;
}
y1 = t - 1;
r = t * (t - 1) - S;
y2 = r;
cout << 0 << " " << 0 << " " << x1 << " " << y1 << " " << x2 << " " << y2;
}
return 0;
} | replace | 21 | 29 | 21 | 38 | 0 | |
p02963 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a) \
{ std::cout << #a << " = " << a << "\n"; }
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
return a < b && (a = b, 1);
}
template <class T, class V> istream &operator>>(istream &ist, pair<T, V> &p) {
return ist >> p.first >> p.second;
}
template <class T> ostream &operator<<(ostream &ost, pair<T, T> &p) {
return ost << p.first << ", " << p.second;
}
template <class T> istream &operator>>(istream &ist, vector<T> &vs) {
for (auto &e : vs)
ist >> e;
return ist;
}
typedef long long ll;
int const inf = INT_MAX / 2;
void output(int x1, int y1, int x2, int y2, int x3, int y3) {
printf("%d %d %d %d %d %d\n", x1, y1, x2, y2, x3, y3);
}
int const ten_9 = 1e9;
int main() {
ll S;
cin >> S;
ll X1, X2, X3, Y1, Y2, Y3;
X1 = ten_9, Y1 = 1;
X3 = 0, Y3 = 0;
if (0 <= S && S <= ten_9) {
X2 = S, Y2 = 0;
} else {
// (S + X2) / 10^9 = Y2 (ただし (S + X2) % 10^9 == 0)
X2 = ten_9 - S % ten_9, Y2 = (S + X2) / ten_9;
}
assert(abs(X1 * Y2 - X2 * Y1) == S);
assert(Y2 <= ten_9);
output(X1, Y1, X2, Y2, X3, Y3);
}
| #include <bits/stdc++.h>
using namespace std;
#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a) \
{ std::cout << #a << " = " << a << "\n"; }
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
return a < b && (a = b, 1);
}
template <class T, class V> istream &operator>>(istream &ist, pair<T, V> &p) {
return ist >> p.first >> p.second;
}
template <class T> ostream &operator<<(ostream &ost, pair<T, T> &p) {
return ost << p.first << ", " << p.second;
}
template <class T> istream &operator>>(istream &ist, vector<T> &vs) {
for (auto &e : vs)
ist >> e;
return ist;
}
typedef long long ll;
int const inf = INT_MAX / 2;
void output(int x1, int y1, int x2, int y2, int x3, int y3) {
printf("%d %d %d %d %d %d\n", x1, y1, x2, y2, x3, y3);
}
int const ten_9 = 1e9;
int main() {
ll S;
cin >> S;
ll X1, X2, X3, Y1, Y2, Y3;
X1 = ten_9, Y1 = 1;
X3 = 0, Y3 = 0;
if (0 <= S && S <= ten_9) {
X2 = S, Y2 = 0;
} else {
// (S + X2) / 10^9 = Y2 (ただし (S + X2) % 10^9 == 0)
X2 = ten_9 - S % ten_9, Y2 = (S + X2) / ten_9;
}
assert(abs(X1 * Y2 - X2 * Y1) == S);
if (Y2 > ten_9) {
output(0, 0, ten_9, 0, 0, ten_9);
} else {
output(X1, Y1, X2, Y2, X3, Y3);
}
}
| replace | 51 | 53 | 51 | 56 | 0 | |
p02963 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxx = 1e5 + 7;
const int Inf = 1 << 30;
const ll INF = 1ll << 60;
ll s;
int a, b, c, d;
int main() {
scanf("%lld", &s);
// 定一顶点在(0, 0), 构造向量(a, b)和(c, d)
// 叉积求面积: s = ad - bc
a = d = (int)sqrt(s);
while (s > a * d * 1ll) {
if (a < d)
a++;
else
d++;
}
ll chk = 1ll * a * d - s;
for (int i = 1; i <= sqrt(chk); i++) {
if (!(chk % i)) {
if (i <= 1e9 && chk / i <= 1e9) {
b = i;
c = chk / i;
break;
}
}
}
printf("0 0 %d %d %d %d\n", a, b, c, d);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxx = 1e5 + 7;
const int Inf = 1 << 30;
const ll INF = 1ll << 60;
ll s;
int a, b, c, d;
int main() {
scanf("%lld", &s);
// 定一顶点在(0, 0), 构造向量(a, b)和(c, d)
// 叉积求面积: s = ad - bc
a = d = (int)sqrt(s);
while (s > 1ll * a * d) {
if (a < d)
a++;
else
d++;
}
ll chk = 1ll * a * d - s;
for (int i = 1; i <= sqrt(chk); i++) {
if (!(chk % i)) {
if (i <= 1e9 && chk / i <= 1e9) {
b = i;
c = chk / i;
break;
}
}
}
printf("0 0 %d %d %d %d\n", a, b, c, d);
return 0;
} | replace | 14 | 15 | 14 | 15 | TLE | |
p02963 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define pf printf
#define sc scanf
#define pb push_back
#define eb emplace_back
#define present(c, x) ((c).find(x) != (c).end())
#define mp make_pair
#define xx first
#define yy second
#define endl '\n'
#define SZ(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
typedef long long ll;
typedef long double LD;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const LD EPS = 1e-9;
const LD PI = acos(-1.0);
const int MX = 6e5 + 5;
ll mod = 1e9 + 7;
int main() {
ll s;
cin >> s;
if (s <= 1000000000LL) {
cout << "0 0 " << s << " 0 0 1\n";
} else {
ll x3 = ceil(sqrt(s));
ll y2 = x3 ? (s / x3 + (s % x3 != 0)) : 0;
ll y3 = x3 * y2 - s, x2;
if (y3 == 0) {
x2 = y3 = 0;
} else {
x2 = 1;
}
cout << "0 0 " << x2 << " " << y2 << " " << x3 << " " << y3 / x2 << endl;
}
return 0;
}
/**
Md. Tariqul Islam
IIT,JU
fb/tariqiitju
tarik.amtoly@gmail.com
*/ | #include <bits/stdc++.h>
using namespace std;
#define pf printf
#define sc scanf
#define pb push_back
#define eb emplace_back
#define present(c, x) ((c).find(x) != (c).end())
#define mp make_pair
#define xx first
#define yy second
#define endl '\n'
#define SZ(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
typedef long long ll;
typedef long double LD;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const LD EPS = 1e-9;
const LD PI = acos(-1.0);
const int MX = 6e5 + 5;
ll mod = 1e9 + 7;
int main() {
ll s;
cin >> s;
if (s <= 1000000000LL) {
cout << "0 0 " << s << " 0 0 1\n";
} else {
ll x3 = ceil(sqrt(s));
ll y2 = x3 ? (s / x3 + (s % x3 != 0)) : 0;
ll y3 = x3 * y2 - s, x2;
if (y3 == 0) {
x2 = y3 = 0;
} else {
x2 = 1;
}
cout << "0 0 " << x2 << " " << y2 << " " << x3 << " " << y3 << endl;
}
return 0;
}
/**
Md. Tariqul Islam
IIT,JU
fb/tariqiitju
tarik.amtoly@gmail.com
*/ | replace | 39 | 40 | 39 | 40 | 0 | |
p02963 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
const int64_t lot = 1000000000;
signed main() {
int64_t s;
cin >> s;
for (int64_t a = 1; a * a <= s; ++a) {
if (s % a == 0) {
int64_t b = s / a;
if (a <= lot && b <= lot) {
cout << 0 << ' ' << 0 << ' ' << a << ' ' << 0 << ' ' << 0 << ' ' << b
<< '\n';
return 0;
}
}
}
}
| #include <bits/stdc++.h>
using namespace std;
const int64_t lot = 1000000000;
signed main() {
int64_t s;
cin >> s;
int64_t x = (lot - (s % lot)) % lot, y = (s + x) / lot;
cout << 0 << ' ' << 0 << ' ' << lot << ' ' << 1 << ' ' << x << ' ' << y
<< '\n';
}
| replace | 8 | 18 | 8 | 11 | TLE | |
p02963 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll s;
int main() {
cin >> s;
ll memo;
for (ll i = 1; i * i <= s; i++) {
if (s % i == 0 && s / i <= 1e9) {
memo = i;
break;
}
}
cout << 0 << ' ' << 0 << ' ' << 0 << ' ' << memo << ' ' << s / memo << ' '
<< 0 << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll s;
int main() {
cin >> s;
ll memo = 1e9;
ll y = (s + memo - 1) / memo;
ll x = y * memo - s;
cout << 0 << ' ' << 0 << ' ' << memo << ' ' << 1 << ' ' << x << ' ' << y
<< endl;
return 0;
}
| replace | 8 | 17 | 8 | 13 | TLE | |
p02963 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
long long s;
cin >> s;
long long a, b;
for (a = s - 1;; a--) {
if (s % a == 0)
break;
}
b = s / a;
cout << 0 << " " << 0 << " " << 0 << " " << a << " " << b << " " << 0 << endl;
getchar();
getchar();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
long long s;
cin >> s;
long long x1, y1, x2, y2, x3, y3;
x1 = 0, y1 = 0;
x2 = 1000000000, y2 = 1;
x3 = (1000000000 - s % 1000000000) % 1000000000, y3 = (s + x3) / 1000000000;
cout << x1 << " " << y1 << " " << x2 << " " << y2 << " " << x3 << " " << y3;
getchar();
getchar();
return 0;
} | replace | 7 | 14 | 7 | 12 | 0 | |
p02963 | Python | Time Limit Exceeded | import sys
import math # noqa
import bisect # noqa
import queue # noqa
def input():
return sys.stdin.readline().rstrip()
if __name__ == "__main__":
S = int(input())
if len(str(S)) == 19:
print(0, 0, 1000000000, 0, 0, 1000000000)
exit(0)
search = min(1000000000, math.ceil(math.sqrt(S)))
for i in range(search, 0, -1):
if S % i == 0:
print(0, 0, i, 0, 0, S // i)
exit(0)
| import sys
import math # noqa
import bisect # noqa
import queue # noqa
def input():
return sys.stdin.readline().rstrip()
if __name__ == "__main__":
S = int(input())
x1, y1 = 0, 0
x2, y2 = 1000000000, 1
x3 = (x2 - S % x2) % x2
y3 = (S + x3) // x2
print("{} {} {} {} {} {}".format(x1, y1, x2, y2, x3, y3))
| replace | 13 | 22 | 13 | 18 | TLE | |
p02963 | Python | Time Limit Exceeded | def solve(s):
k = 10**9
if s <= k:
return 0, 0, 1, 0, 0, s
X = k
while True:
Y = (s - 1) // X + 1
while Y <= k:
xy = X * Y - s
lim = min(Y, int(xy**0.5))
for y in range(1, lim + 1):
x, m = divmod(xy, y)
if x <= X and m == 0:
return 0, 0, X, y, x, Y
Y += 1
X -= 1
s = int(input())
print(*solve(s))
| def solve(s):
k = 10**9
if s <= k:
return 0, 0, 1, 0, 0, s
X = k
while True:
Y = (s - 1) // X + 1
while Y <= k:
xy = X * Y - s
if xy == 0:
return 0, 0, X, 0, 0, Y
lim = min(Y, int(xy**0.5))
for y in range(1, lim + 1):
x, m = divmod(xy, y)
if x <= X and m == 0:
return 0, 0, X, y, x, Y
Y += 1
X -= 1
s = int(input())
print(*solve(s))
| insert | 9 | 9 | 9 | 11 | TLE | |
p02963 | C++ | Time Limit Exceeded | #include <bits/stdc++.h> // Tomasz Nowak
using namespace std; // XIII LO Szczecin
using LL = long long; // Poland
#define FOR(i, l, r) for (int i = (l); i <= (r); ++i)
#define REP(i, n) FOR(i, 0, (n)-1)
template <class T> int size(T &&x) { return int(x.size()); }
template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &p) {
return out << '(' << p.first << ", " << p.second << ')';
}
template <class T>
auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) {
out << '{';
for (auto it = x.begin(); it != x.end(); ++it)
out << *it << (it == prev(x.end()) ? "" : ", ");
return out << '}';
}
void dump() {}
template <class T, class... Args> void dump(T &&x, Args... args) {
cerr << x << "; ";
dump(args...);
}
#ifdef DEBUG
struct Nl {
~Nl() { cerr << '\n'; }
};
#define debug(x...) \
cerr << (strcmp(#x, "") ? #x ": " : ""), dump(x), Nl(), cerr << ""
#else
#define debug(...) 0 && cerr
#endif
mt19937_64 rng(0);
int rd(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); }
// end of templates
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
LL s;
cin >> s;
LL added = 0;
while (LL(sqrt(s + added)) * LL(sqrt(s + added)) != s + added)
++added;
cout << "0 0 " << LL(sqrt(s + added)) << ' ' << (added ? 1 : 0) << ' '
<< added << ' ' << LL(sqrt(s + added));
}
| #include <bits/stdc++.h> // Tomasz Nowak
using namespace std; // XIII LO Szczecin
using LL = long long; // Poland
#define FOR(i, l, r) for (int i = (l); i <= (r); ++i)
#define REP(i, n) FOR(i, 0, (n)-1)
template <class T> int size(T &&x) { return int(x.size()); }
template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &p) {
return out << '(' << p.first << ", " << p.second << ')';
}
template <class T>
auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) {
out << '{';
for (auto it = x.begin(); it != x.end(); ++it)
out << *it << (it == prev(x.end()) ? "" : ", ");
return out << '}';
}
void dump() {}
template <class T, class... Args> void dump(T &&x, Args... args) {
cerr << x << "; ";
dump(args...);
}
#ifdef DEBUG
struct Nl {
~Nl() { cerr << '\n'; }
};
#define debug(x...) \
cerr << (strcmp(#x, "") ? #x ": " : ""), dump(x), Nl(), cerr << ""
#else
#define debug(...) 0 && cerr
#endif
mt19937_64 rng(0);
int rd(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); }
// end of templates
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
LL s;
cin >> s;
LL sq = LL(sqrt(s));
if (sq * sq != s)
++sq;
LL added = sq * sq - s;
while (LL(sqrt(s + added)) * LL(sqrt(s + added)) != s + added)
++added;
cout << "0 0 " << LL(sqrt(s + added)) << ' ' << (added ? 1 : 0) << ' '
<< added << ' ' << LL(sqrt(s + added));
}
| replace | 41 | 42 | 41 | 45 | TLE | |
p02963 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define mod 1000000007
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
int main() {
ll s;
cin >> s;
ll a = 1;
for (int i = 0; i < 10; i++) {
if (a >= s) {
break;
} else {
a *= 10;
}
}
a /= 10;
ll b = (s + a - 1) / a;
ll p = a * b - s;
ll q = 1;
cout << 0 << " " << 0 << " " << a << " " << p << " " << q << " " << b << endl;
// cerr << a*b-p*q << endl;
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define mod 1000000007
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
int main() {
ll s;
cin >> s;
ll a = 1;
if (s == 1) {
cout << 0 << " " << 0 << " " << 1 << " " << 0 << " " << 0 << " " << 1
<< endl;
return 0;
}
for (int i = 0; i < 10; i++) {
if (a >= s) {
break;
} else {
a *= 10;
}
}
a /= 10;
ll b = (s + a - 1) / a;
ll p = a * b - s;
ll q = 1;
cout << 0 << " " << 0 << " " << a << " " << p << " " << q << " " << b << endl;
// cerr << a*b-p*q << endl;
return 0;
} | insert | 30 | 30 | 30 | 35 | 0 | |
p02963 | C++ | Runtime Error | #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REPS(i, n) for (int i = 1, i##_len = (n); i < i##_len; ++i)
#define ALL(x) (x).begin(), (x).end()
#define PRINT(A) std::cout << (#A) << ":" << (A) << std::endl;
using namespace std;
#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
// vector
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
for (T &x : vec)
is >> x;
return is;
}
// pair
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &pair_var) {
os << "(" << pair_var.first << ", " << pair_var.second << ")";
return os;
}
// vector
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
os << "{";
for (int i = 0; i < vec.size(); i++) {
os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
}
os << "}\n ";
return os;
}
// map
template <typename T, typename U>
ostream &operator<<(ostream &os, map<T, U> &map_var) {
os << "{";
repi(itr, map_var) {
os << *itr;
itr++;
if (itr != map_var.end())
os << ", ";
itr--;
}
os << "}";
return os;
}
// set
template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) {
os << "{";
repi(itr, set_var) {
os << *itr;
itr++;
if (itr != set_var.end())
os << ", ";
itr--;
}
os << "}";
return os;
}
#define DUMPOUT cerr
void dump_func() { DUMPOUT << endl; }
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&...tail) {
DUMPOUT << head;
if (sizeof...(Tail) > 0) {
DUMPOUT << ", ";
}
dump_func(std::move(tail)...);
}
#ifdef DEBUG_
#define DEB
#define dump(...) \
DUMPOUT << " " << string(#__VA_ARGS__) << ": " \
<< "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << endl \
<< " ", \
dump_func(__VA_ARGS__)
#else
#define DEB if (false)
#define dump(...)
#endif
typedef long long int ll;
const int MOD = 1e9 + 7;
const int INF = 1e9;
int main() {
ll S;
cin >> S;
ll y2 = floor(sqrt(S));
ll x1;
if (S % y2 == 0) {
x1 = S / y2;
} else {
x1 = S / y2 + 1;
}
ll rest = y2 * x1 - S;
ll x2 = rest;
ll y1 = 1;
dump(x1, y2, x2, y1);
dump(x1 * y2 - x2 * y1);
assert(S == abs(x1 * y2 - x2 * y1));
assert(x1 <= INF && x2 <= INF && y1 <= INF && y2 <= INF);
cout << 0 << " " << 0 << " " << x1 << " " << y1 << " " << x2 << " " << y2
<< endl;
}
| #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REPS(i, n) for (int i = 1, i##_len = (n); i < i##_len; ++i)
#define ALL(x) (x).begin(), (x).end()
#define PRINT(A) std::cout << (#A) << ":" << (A) << std::endl;
using namespace std;
#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
// vector
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
for (T &x : vec)
is >> x;
return is;
}
// pair
template <typename T, typename U>
ostream &operator<<(ostream &os, pair<T, U> &pair_var) {
os << "(" << pair_var.first << ", " << pair_var.second << ")";
return os;
}
// vector
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
os << "{";
for (int i = 0; i < vec.size(); i++) {
os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
}
os << "}\n ";
return os;
}
// map
template <typename T, typename U>
ostream &operator<<(ostream &os, map<T, U> &map_var) {
os << "{";
repi(itr, map_var) {
os << *itr;
itr++;
if (itr != map_var.end())
os << ", ";
itr--;
}
os << "}";
return os;
}
// set
template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) {
os << "{";
repi(itr, set_var) {
os << *itr;
itr++;
if (itr != set_var.end())
os << ", ";
itr--;
}
os << "}";
return os;
}
#define DUMPOUT cerr
void dump_func() { DUMPOUT << endl; }
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&...tail) {
DUMPOUT << head;
if (sizeof...(Tail) > 0) {
DUMPOUT << ", ";
}
dump_func(std::move(tail)...);
}
#ifdef DEBUG_
#define DEB
#define dump(...) \
DUMPOUT << " " << string(#__VA_ARGS__) << ": " \
<< "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << endl \
<< " ", \
dump_func(__VA_ARGS__)
#else
#define DEB if (false)
#define dump(...)
#endif
typedef long long int ll;
const int MOD = 1e9 + 7;
const int INF = 1e9;
int main() {
ll S;
cin >> S;
ll y2 = INF;
ll x1;
if (S % y2 == 0) {
x1 = S / y2;
} else {
x1 = S / y2 + 1;
}
ll rest = y2 * x1 - S;
ll x2 = rest;
ll y1 = 1;
dump(x1, y2, x2, y1);
dump(x1 * y2 - x2 * y1);
assert(S == abs(x1 * y2 - x2 * y1));
assert(x1 <= INF && x2 <= INF && y1 <= INF && y2 <= INF);
cout << 0 << " " << 0 << " " << x1 << " " << y1 << " " << x2 << " " << y2
<< endl;
}
| replace | 87 | 88 | 87 | 88 | 0 | |
p02963 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
long long S;
cin >> S; // s=ad-bc(0,0,a,b,c,d)
long long a = 1000000000;
long long b = 1;
long long c = (S + 999999999) / 1000000000;
long long d = (S + b * c) / a;
cout << "0 0 " + a + ' ' + b + ' ' + c + ' ' + d << endl;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
long long S;
cin >> S; // s=ad-bc(0,0,a,b,c,d)
int a = 1000000000;
int b = 1;
int d = (S + 999999999) / 1000000000;
int c = (-S + a * d) / b;
char e = ' ';
cout << "0 0 " << a << e << b << e << c << e << d << endl;
}
| replace | 6 | 11 | 6 | 12 | -11 | |
p02963 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
const ll MOD = 1000000007;
const ll LINF = 0x1fffffffffffffff;
const int INF = 0x3fffffff;
const vector<pll> dir = {{0, 1}, {1, 0}, {-1, 0}, {0, -1},
{1, 1}, {-1, 1}, {1, -1}, {-1, -1}};
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define rrep(i, a, b) for (ll i = b - 1; i >= a; i--)
#define range(i) (i).begin(), (i).end()
#define rrange(i) (i).rbegin(), (i).rend()
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
inline constexpr int intpow(ll a, ll b) {
if (b == 0)
return 1;
ll ans = intpow(a, b / 2);
return ans * ans % MOD * (b & 1 ? a : 1) % MOD;
}
inline constexpr int modf(ll n) {
ll ans = 1;
rep(i, 0, n) { ans = ans * (n - i) % MOD; }
return ans;
}
vector<ll> func, inv;
void init(ll n) {
func.resize(n + 1);
inv.resize(n + 1);
func[0] = 1;
rep(i, 0, n) { func[i + 1] = func[i] * (i + 1) % MOD; }
inv[n] = intpow(func[n], MOD - 2);
rrep(i, 1, n + 1) { inv[i - 1] = inv[i] * i % MOD; }
}
ll comb(ll a, ll b) { return func[a] * inv[b] % MOD * inv[a - b] % MOD; }
inline long double median(vector<ll> v) {
ll size = v.size();
sort(v.begin(), v.end());
if (size % 2 == 1)
return v[(size - 1) / 2];
else
return v[(size / 2) - 1];
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
int main() {
ll s;
cin >> s;
ll a;
rep(i, round(sqrt(s)) - 1, s) {
if (i * i - s > -1) {
a = i;
break;
}
}
ll b = round(sqrt(a * a - s)) + 1, ans = 0;
while (ans == 0) {
if ((a * a - s) % b == 0)
ans = b;
b--;
}
cout << 0 << ' ' << 0 << ' ' << a << ' ' << ans << ' ' << (a * a - s) / ans
<< ' ' << a;
} | #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
const ll MOD = 1000000007;
const ll LINF = 0x1fffffffffffffff;
const int INF = 0x3fffffff;
const vector<pll> dir = {{0, 1}, {1, 0}, {-1, 0}, {0, -1},
{1, 1}, {-1, 1}, {1, -1}, {-1, -1}};
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define rrep(i, a, b) for (ll i = b - 1; i >= a; i--)
#define range(i) (i).begin(), (i).end()
#define rrange(i) (i).rbegin(), (i).rend()
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
inline constexpr int intpow(ll a, ll b) {
if (b == 0)
return 1;
ll ans = intpow(a, b / 2);
return ans * ans % MOD * (b & 1 ? a : 1) % MOD;
}
inline constexpr int modf(ll n) {
ll ans = 1;
rep(i, 0, n) { ans = ans * (n - i) % MOD; }
return ans;
}
vector<ll> func, inv;
void init(ll n) {
func.resize(n + 1);
inv.resize(n + 1);
func[0] = 1;
rep(i, 0, n) { func[i + 1] = func[i] * (i + 1) % MOD; }
inv[n] = intpow(func[n], MOD - 2);
rrep(i, 1, n + 1) { inv[i - 1] = inv[i] * i % MOD; }
}
ll comb(ll a, ll b) { return func[a] * inv[b] % MOD * inv[a - b] % MOD; }
inline long double median(vector<ll> v) {
ll size = v.size();
sort(v.begin(), v.end());
if (size % 2 == 1)
return v[(size - 1) / 2];
else
return v[(size / 2) - 1];
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
int main() {
ll s;
cin >> s;
ll a = round(sqrt(s) + 0.5);
if (a > 1000000000)
a = 1000000000;
ll b = round(sqrt(a * a - s) + 0.5);
ll ans = 0;
while (ans == 0) {
if ((a * a - s) % b == 0)
ans = b;
b--;
}
cout << 0 << ' ' << 0 << ' ' << a << ' ' << ans << ' ' << (a * a - s) / ans
<< ' ' << a;
} | replace | 73 | 81 | 73 | 78 | TLE | |
p02963 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bits/stdc++.h>
#include <cmath> // abs() for float, and fabs()
#include <cstdlib> // abs() for integer 絶対値求めやつ
#include <iomanip> //浮動小数点数を出力する制度の指定
#include <string>
#include <vector>
// 使い方 数値をnumとして cout<<setprecision(n)<<num ;nは桁数
#define rep(i, n) for (int i = 0; i < (n); i++)
#define SORT(a) sort((a).begin(), (a).end())
#define REV(a) reverse((a).begin(), (a).end())
using namespace std;
using ll = long long;
#define INF 2000000000
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
#define che(a, string) cout << "//" << string << "==" << (a) << "//" << endl;
bool IsInt(double a) {
int b = a / 1;
if (a == b) {
return true;
} else {
return false;
}
}
/*覚えてないことメaモ
数値型から文字列に変換 to_string(number);これは簡単
文字列から数値型に変換 stoi(number)
文字列からllに変換 stoll(number)*/
// ここから書き始める
int main() {
ll S;
cin >> S;
// まず、面積がSとなる四角形の辺の長さを考えればよい
ll Y = 1;
ll i = 2;
ll meow = pow(10, 9);
while (S > meow) {
while (1) {
if (S % i == 0) {
break;
} else {
if (i != 2) {
i += 2;
}
}
}
S /= i;
Y *= i;
}
cout << 0 << " " << 0 << " " << 0 << " " << Y << " " << S << " " << 0 << endl;
}
| #include <algorithm>
#include <bits/stdc++.h>
#include <cmath> // abs() for float, and fabs()
#include <cstdlib> // abs() for integer 絶対値求めやつ
#include <iomanip> //浮動小数点数を出力する制度の指定
#include <string>
#include <vector>
// 使い方 数値をnumとして cout<<setprecision(n)<<num ;nは桁数
#define rep(i, n) for (int i = 0; i < (n); i++)
#define SORT(a) sort((a).begin(), (a).end())
#define REV(a) reverse((a).begin(), (a).end())
using namespace std;
using ll = long long;
#define INF 2000000000
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
#define che(a, string) cout << "//" << string << "==" << (a) << "//" << endl;
bool IsInt(double a) {
int b = a / 1;
if (a == b) {
return true;
} else {
return false;
}
}
/*覚えてないことメaモ
数値型から文字列に変換 to_string(number);これは簡単
文字列から数値型に変換 stoi(number)
文字列からllに変換 stoll(number)*/
// ここから書き始める
int main() {
ll S;
cin >> S;
ll x = ceil(sqrt(S));
ll a = x;
ll d = x;
ll b = 1;
ll c = x * x - S;
cout << 0 << " " << 0 << " " << a << " " << b << " " << c << " " << d << endl;
}
| replace | 47 | 65 | 47 | 53 | TLE | |
p02963 | C++ | Time Limit Exceeded | #include <cstdio>
int k, x1, x2, x3, y1, y2, y3;
long long s;
int main() {
scanf("%lld", &s);
for (x2 = 1, y3 = 1, k = 1; 1LL * x2 * y3 < s; k = k ^ 1)
if (k)
x2++;
else
y3++;
y2 = 1, x3 = 1LL * x2 * y3 - s;
printf("%d %d %d %d %d %d\n", x1, y1, x2, y2, x3, y3);
return 0;
} | #include <cstdio>
int k, x1, x2, x3, y1, y2, y3;
long long s;
int main() {
scanf("%lld", &s);
for (x2 = 1, y3 = 1; 1LL * (x2 + 10000) * (y3 + 10000) < s;
x2 = x2 + 10000, y3 = y3 + 10000)
;
for (k = 1; 1LL * x2 * y3 < s; k = k ^ 1)
if (k)
x2++;
else
y3++;
y2 = 1, x3 = 1LL * x2 * y3 - s;
printf("%d %d %d %d %d %d\n", x1, y1, x2, y2, x3, y3);
return 0;
} | replace | 7 | 8 | 7 | 11 | TLE | |
p02963 | C++ | Time Limit Exceeded | /**
* @brief : c++ code for AtCoder
* @author : rk222
* @created: 2019.07.21 20:55:19
*/
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<int, P> P1;
#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
#define mp1(a, b, c) P1(a, P(b, c))
#define disp(x) cout << #x << ": " << x << endl
#define disp_vec(v) \
cout << #v << ":"; \
rep(i, v.size()) cout << " " << v[i]; \
cout << endl
const int INF = 100000000;
const int M = 100000000;
const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dir_8[8][2] = {{1, 0}, {1, 1}, {0, 1}, {-1, 1},
{-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
/* ------------------------------------- */
int main() {
ll s;
scanf("%lld", &s);
ll l = ((ll)sqrt(s - 1LL)) + 1LL;
// if (s >= 90000000000000000) {
// l--;
// }
ll n = l * l - s;
// printf("%lld %lld %lld\n", n, l, s);
if (s == 1LL) {
printf("0 0 1 0 0 1\n");
return 0;
}
if (s == 1000000000000000000LL) {
printf("0 0 1000000000 0 0 1000000000\n");
return 0;
}
baaaa:
ll a = 0LL;
ll b;
ll sqn = sqrt(n);
while (1) {
if (a > l || a > sqn) {
break;
}
if (a == 0) {
if (n == 0) {
b = 0;
break;
} else {
a++;
continue;
}
}
if (n % a == 0 && n / a <= l) {
b = n / a;
break;
} else {
a++;
}
// printf("%lld\n", a);
}
if (l - a < 0 || l - b < 0) {
l--;
n = l * l - s;
goto baaaa;
}
printf("%lld %lld %lld %lld %lld %lld\n", l - a, 0LL, 0LL, l - b, l, l);
// printf("%lld\n", 2LL * l * l - l * (a + b) - (l - a) * (l - b));
/* --------------------------------- */
return 0;
} | /**
* @brief : c++ code for AtCoder
* @author : rk222
* @created: 2019.07.21 20:55:19
*/
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<int, P> P1;
#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
#define mp1(a, b, c) P1(a, P(b, c))
#define disp(x) cout << #x << ": " << x << endl
#define disp_vec(v) \
cout << #v << ":"; \
rep(i, v.size()) cout << " " << v[i]; \
cout << endl
const int INF = 100000000;
const int M = 100000000;
const int dir_4[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
const int dir_8[8][2] = {{1, 0}, {1, 1}, {0, 1}, {-1, 1},
{-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
/* ------------------------------------- */
int main() {
ll s;
scanf("%lld", &s);
ll l = ((ll)sqrt(s - 1LL)) + 1LL;
// if (s >= 90000000000000000) {
// l--;
// }
ll n = l * l - s;
// printf("%lld %lld %lld\n", n, l, s);
if (s == 1LL) {
printf("0 0 1 0 0 1\n");
return 0;
}
if (s == 1000000000000000000LL) {
printf("0 0 1000000000 0 0 1000000000\n");
return 0;
}
baaaa:
ll a = 0LL;
ll b;
ll sqn = sqrt(n);
while (1) {
if (a > l || a > sqn) {
break;
}
if (a == 0) {
if (n == 0) {
b = 0;
break;
} else {
a++;
continue;
}
}
if (n % a == 0 && n / a <= l) {
b = n / a;
break;
} else {
a++;
}
// printf("%lld\n", a);
}
if (l - a < 0 || l - b < 0) {
l++;
n = l * l - s;
goto baaaa;
}
printf("%lld %lld %lld %lld %lld %lld\n", l - a, 0LL, 0LL, l - b, l, l);
// printf("%lld\n", 2LL * l * l - l * (a + b) - (l - a) * (l - b));
/* --------------------------------- */
return 0;
} | replace | 96 | 97 | 96 | 97 | TLE | |
p02963 | C++ | Runtime Error | #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i))
#define REP_R(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i))
#define REP3R(i, m, n) for (int i = (int)(n)-1; (i) >= (int)(m); --(i))
#define ALL(x) begin(x), end(x)
#define dump(x) cerr << #x " = " << x << endl
using ll = long long;
using namespace std;
template <class T>
using reversed_priority_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U> inline void chmax(T &a, U const &b) {
a = max<T>(a, b);
}
template <class T, class U> inline void chmin(T &a, U const &b) {
a = min<T>(a, b);
}
template <typename X, typename T> auto make_vectors(X x, T a) {
return vector<T>(x, a);
}
template <typename X, typename Y, typename Z, typename... Zs>
auto make_vectors(X x, Y y, Z z, Zs... zs) {
auto cont = make_vectors(y, z, zs...);
return vector<decltype(cont)>(x, cont);
}
template <typename T> ostream &operator<<(ostream &out, vector<T> const &xs) {
REP(i, (int)xs.size() - 1) out << xs[i] << ' ';
if (not xs.empty())
out << xs.back();
return out;
}
/**
* @note O(\sqrt{n})
* @note about 1.0 sec for 10^5 queries with n < 10^10
*/
struct prepared_primes {
int size;
vector<int> sieve;
vector<int> primes;
prepared_primes(int size_) : size(size_) {
sieve.resize(size);
REP3(p, 2, size) if (sieve[p] == 0) {
primes.push_back(p);
for (int k = p; k < size; k += p) {
if (sieve[k] == 0) {
sieve[k] = p;
}
}
}
}
vector<ll> prime_factorize(ll n) {
// assert (1 <= n and n < (ll)size * size);
vector<ll> result;
// trial division for large part
for (int p : primes) {
if (n < size or n < (ll)p * p) {
break;
}
while (n % p == 0) {
n /= p;
result.push_back(p);
}
}
// small part
if (n == 1) {
// nop
} else if (n < size) {
while (n != 1) {
result.push_back(sieve[n]);
n /= sieve[n];
}
} else {
result.push_back(n);
}
assert(is_sorted(ALL(result)));
return result;
}
bool is_prime(ll n) { return prime_factorize(n).size() == 1; }
vector<ll> list_all_factors(ll n) {
auto p = prime_factorize(n);
vector<ll> d;
d.push_back(1);
for (int l = 0; l < p.size();) {
int r = l + 1;
while (r < p.size() and p[r] == p[l])
++r;
int n = d.size();
REP(k1, r - l) {
REP(k2, n) { d.push_back(d[d.size() - n] * p[l]); }
}
l = r;
}
return d;
}
};
vector<ll> solve(ll s) {
constexpr ll LIMIT = 1000000000;
prepared_primes primes(1000000);
REP3(h1, max<ll>(1, sqrt(s) - 1000), LIMIT + 1) {
REP3(w2, max<ll>(0, s / h1 - 100), LIMIT + 1) {
ll t = s - (ll)h1 * w2;
if (t < 0)
break;
// cerr << h1 << ' ' << w2 << ' ' << t << endl;
for (ll h2 : primes.list_all_factors(t)) {
ll w1 = t / h2;
if (h2 <= LIMIT and w1 + w2 <= LIMIT) {
ll x1 = 0;
ll y1 = h1;
ll x2 = w1;
ll y2 = 0;
ll x3 = w1 + w2;
ll y3 = h2;
assert(0 <= x1 and x1 <= LIMIT);
assert(0 <= y1 and y1 <= LIMIT);
assert(0 <= x2 and x2 <= LIMIT);
assert(0 <= y2 and y2 <= LIMIT);
assert(0 <= x3 and x3 <= LIMIT);
assert(0 <= y3 and y3 <= LIMIT);
assert(s == h1 * w2 + h2 * w1);
return vector<ll>({x1, y1, x2, y2, x3, y3});
}
}
}
}
assert(false);
}
int main() {
ll s;
cin >> s;
cout << solve(s) << endl;
return 0;
}
| #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i))
#define REP_R(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i))
#define REP3R(i, m, n) for (int i = (int)(n)-1; (i) >= (int)(m); --(i))
#define ALL(x) begin(x), end(x)
#define dump(x) cerr << #x " = " << x << endl
using ll = long long;
using namespace std;
template <class T>
using reversed_priority_queue = priority_queue<T, vector<T>, greater<T>>;
template <class T, class U> inline void chmax(T &a, U const &b) {
a = max<T>(a, b);
}
template <class T, class U> inline void chmin(T &a, U const &b) {
a = min<T>(a, b);
}
template <typename X, typename T> auto make_vectors(X x, T a) {
return vector<T>(x, a);
}
template <typename X, typename Y, typename Z, typename... Zs>
auto make_vectors(X x, Y y, Z z, Zs... zs) {
auto cont = make_vectors(y, z, zs...);
return vector<decltype(cont)>(x, cont);
}
template <typename T> ostream &operator<<(ostream &out, vector<T> const &xs) {
REP(i, (int)xs.size() - 1) out << xs[i] << ' ';
if (not xs.empty())
out << xs.back();
return out;
}
/**
* @note O(\sqrt{n})
* @note about 1.0 sec for 10^5 queries with n < 10^10
*/
struct prepared_primes {
int size;
vector<int> sieve;
vector<int> primes;
prepared_primes(int size_) : size(size_) {
sieve.resize(size);
REP3(p, 2, size) if (sieve[p] == 0) {
primes.push_back(p);
for (int k = p; k < size; k += p) {
if (sieve[k] == 0) {
sieve[k] = p;
}
}
}
}
vector<ll> prime_factorize(ll n) {
// assert (1 <= n and n < (ll)size * size);
vector<ll> result;
// trial division for large part
for (int p : primes) {
if (n < size or n < (ll)p * p) {
break;
}
while (n % p == 0) {
n /= p;
result.push_back(p);
}
}
// small part
if (n == 1) {
// nop
} else if (n < size) {
while (n != 1) {
result.push_back(sieve[n]);
n /= sieve[n];
}
} else {
result.push_back(n);
}
assert(is_sorted(ALL(result)));
return result;
}
bool is_prime(ll n) { return prime_factorize(n).size() == 1; }
vector<ll> list_all_factors(ll n) {
auto p = prime_factorize(n);
vector<ll> d;
d.push_back(1);
for (int l = 0; l < p.size();) {
int r = l + 1;
while (r < p.size() and p[r] == p[l])
++r;
int n = d.size();
REP(k1, r - l) {
REP(k2, n) { d.push_back(d[d.size() - n] * p[l]); }
}
l = r;
}
return d;
}
};
vector<ll> solve(ll s) {
constexpr ll LIMIT = 1000000000;
prepared_primes primes(1000000);
REP3(h1, max<ll>(1, sqrt(s) - 1000), LIMIT + 1) {
REP3(w2, max<ll>(0, s / h1 - 100), LIMIT + 1) {
ll t = s - (ll)h1 * w2;
if (t < 0)
break;
// cerr << h1 << ' ' << w2 << ' ' << t << endl;
for (ll h2 : primes.list_all_factors(t)) {
ll w1 = t / h2;
if (h2 <= LIMIT and w1 + w2 <= LIMIT) {
ll x1 = 0;
ll y1 = h1;
ll x2 = w1;
ll y2 = 0;
ll x3 = w1 + w2;
ll y3 = h2;
assert(0 <= x1 and x1 <= LIMIT);
assert(0 <= y1 and y1 <= LIMIT);
assert(0 <= x2 and x2 <= LIMIT);
assert(0 <= y2 and y2 <= LIMIT);
assert(0 <= x3 and x3 <= LIMIT);
assert(0 <= y3 and y3 <= LIMIT);
assert(s == (ll)h1 * w2 + (ll)h2 * w1);
return vector<ll>({x1, y1, x2, y2, x3, y3});
}
}
}
}
assert(false);
}
int main() {
ll s;
cin >> s;
cout << solve(s) << endl;
return 0;
}
| replace | 130 | 131 | 130 | 131 | 0 | |
p02963 | C++ | Runtime Error | #include <stdio.h>
int main() {
long s;
scanf("%d", s);
printf("0 ");
printf("0 ");
printf("2 ");
printf("0 ");
printf("0 ");
printf("%ld", s);
} | #include <stdio.h>
int main() {
long long s, t, u;
scanf("%lld", &s);
t = s / 1000000000 + 1;
u = 1000000000 - s % 1000000000;
if (s % 1000000000 == 0) {
t--;
u = 0;
}
printf("0 0 %lld 1000000000 %lld 1", u, t);
}
| replace | 2 | 10 | 2 | 11 | 0 | |
p02963 | C++ | Runtime Error | #include <bits/stdc++.h>
#define int long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define x first
#define y second
#define sz(x) (int)x.size()
#define endl '\n'
#define hell 1000000007
#define rep(i, a, b) for (int i = a; i < b; i++)
using namespace std;
void solve() {
int s;
cin >> s;
int a = sqrt(s);
int d = a;
while ((a + 1) * d <= s)
a++;
int b = (s - a * d);
int c = -1;
assert(a + 1 <= hell - 7);
assert(c + 1 <= hell - 7);
assert(b <= hell - 7);
assert(d <= hell - 7);
assert(abs(d * a - b * c) == s);
cout << 1 << " " << 0 << " " << a + 1 << " " << b << " " << c + 1 << " " << d
<< endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin>>t;
while (t--) {
solve();
}
return 0;
} | #include <bits/stdc++.h>
#define int long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define x first
#define y second
#define sz(x) (int)x.size()
#define endl '\n'
#define hell 1000000007
#define rep(i, a, b) for (int i = a; i < b; i++)
using namespace std;
void solve() {
int s;
cin >> s;
int a = sqrt(s);
int d = a;
while (a + 1 > hell - 7)
a--;
while (d + 1 <= hell - 7 and (d + 1) * a <= s)
d++;
while (d > hell - 7)
d--;
int b = (s - a * d);
int c = -1;
assert(a + 1 <= hell - 7);
assert(c + 1 <= hell - 7);
assert(b <= hell - 7);
assert(d <= hell - 7);
assert(abs(d * a - b * c) == s);
cout << 1 << " " << 0 << " " << a + 1 << " " << b << " " << c + 1 << " " << d
<< endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin>>t;
while (t--) {
solve();
}
return 0;
} | replace | 23 | 25 | 23 | 29 | 0 | |
p02963 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define mp make_pair
#define pb push_back
#define fst first
#define snd second
typedef long long ll;
typedef pair<int, int> pii;
int main() {
ll s, x, z, r;
cin >> s;
for (ll y = ll(sqrt(s)); y >= 1; y--) {
if (s / y >= s % y) {
x = 1;
z = s % y;
r = s / y - z;
if (x > ll(1e9) || z + r > ll(1e9) || x + y > ll(1e9) || r > ll(1e9))
continue;
cout << 0 << " " << 0 << " " << x << " " << z + r << " " << x + y << " "
<< r << endl;
return 0;
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define mp make_pair
#define pb push_back
#define fst first
#define snd second
typedef long long ll;
typedef pair<int, int> pii;
int main() {
ll s, x, z, r;
cin >> s;
if (s == ll(1e18)) {
cout << 0 << " " << 0 << " " << ll(1e9) << " " << 0 << " " << 0 << " "
<< ll(1e9) << endl;
return 0;
}
if (s == ll(1e18) - 1) {
ll y = ll(1e9) - 1, z = ll(1e9) - 1, x = 1, r = ll(1e9) - z;
cout << 0 << " " << 0 << " " << x << " " << z + r << " " << x + y << " "
<< r << endl;
return 0;
}
for (ll y = ll(sqrt(s)); y >= 1; y--) {
if (s / y >= s % y) {
x = 1;
z = s % y;
r = s / y - z;
if (x > ll(1e9) || z + r > ll(1e9) || x + y > ll(1e9) || r > ll(1e9))
continue;
cout << 0 << " " << 0 << " " << x << " " << z + r << " " << x + y << " "
<< r << endl;
return 0;
}
}
return 0;
} | insert | 15 | 15 | 15 | 26 | TLE | |
p02963 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define N 100000
typedef long long ll;
int main() {
int n;
cin >> n;
vector<int> f(3, 0);
cin >> f.at(0) >> f.at(1) >> f.at(2);
vector<int> ln(n, 0);
rep(i, n) { cin >> ln.at(i); }
// sort(ln.begin(),ln.end());
vector<int> s(n, 0);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define N 100000
typedef long long ll;
int main() {
ll s;
cin >> s;
ll x1, y1, x2, y2;
x1 = 1e9;
x2 = 1;
y2 = s / x1;
y1 = (ll)(x1 * y2 - s);
if (y1 < 0) {
y2++;
y1 = (ll)(x1 * y2 - s);
}
printf("0 0 %d %d %d %d", x1, y1, x2, y2);
return 0;
} | replace | 6 | 15 | 6 | 18 | 0 | |
p02963 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vs = vector<string>;
using pll = pair<ll, ll>;
using vp = vector<pll>;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, a, b) for (ll i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((ll)(x).size())
const ll MOD = 1000000007;
const ll INF = 100000000000000000LL;
const double EPS = 1e-9;
inline ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
inline ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }
inline ll powint(ll x, ll y) {
ll r = 1;
while (y) {
if (y & 1)
r *= x;
x *= x;
y >>= 1;
}
return r;
}
inline ll powmod(ll x, ll y, ll m = MOD) {
ll r = 1;
while (y) {
if (y & 1)
r *= x;
x *= x;
r %= m;
x %= m;
y >>= 1;
}
return r;
}
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
#ifdef OJ_LOCAL
#include "dump.hpp"
#else
#define dump(...) ((void)0)
#endif
// http://orolog.hatenablog.jp/entry/20100804/1280943541
// a * b % mを返す.
ll multMod(ll a, ll b, ll m) {
ll res = 0;
a %= m;
while (b) {
if (b & 1) {
res = (res + a) % m;
}
a = (a * 2) % m;
b = b / 2;
}
return res;
}
// cははじめ1を入れる.
// numが素数の場合無限ループしてしまう.判定もするなら適度なcで打ち切る.*/
ll PollardRho(ll num, ll c) {
if (c > 1000)
return -1;
ll x, y, p;
x = y = p = 1LL;
while (p == 1) {
x = (multMod(x, x, num) + c) % num;
ll ty = (multMod(y, y, num) + c) % num;
y = (multMod(ty, ty, num) + c) % num;
if (x == y)
return PollardRho(num, c + 1);
p = GCD(num, llabs(x - y));
}
return p;
}
// https://ei1333.github.io/luzhiled/snippets/math/divisor.html
vector<ll> divisor(ll n) {
vector<ll> ret;
for (ll i = sqrt(n) + 1; i >= 1; i--) {
if (n % i == 0) {
ret.push_back(i);
return (ret);
}
}
return (ret);
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
ll s;
cin >> s;
/*
ll div = PollardRho(s,1);
dump(div);
if(div == -1){
cout << "0 0 0 " << s << " 1 0" << endl;
return 0;
}else{
vll divs;
divs = divisor(s);
cout << "0 0 0 " << divs[0] << " " << s/divs[0] << " 0" << endl;
return 0;
}
*/
if (s <= (ll)1e9) {
cout << "0 0 0 " << s << " 1 0" << endl;
return 0;
}
ll cl = 1;
for (ll i = sqrt(s) + EPS; i <= (ll)1e9; i++)
for (ll j = sqrt(s) + EPS, c = 0; j <= (ll)1e9 && c <= cl; j++, c++) {
if (i * j == s) {
cout << "0 0 ";
cout << i;
cout << " ";
cout << 0;
cout << " ";
cout << 0;
cout << " ";
cout << j;
cout << endl;
return 0;
}
ll x = i * j - s;
ll div = PollardRho(x, 1);
if (div != -1) {
vll divs;
divs = divisor(x);
if (divs.empty())
continue;
cout << "0 0 ";
cout << i;
cout << " ";
cout << divs[0];
cout << " ";
cout << x / divs[0];
cout << " ";
cout << j;
cout << endl;
return 0;
}
cl++;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vs = vector<string>;
using pll = pair<ll, ll>;
using vp = vector<pll>;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, a, b) for (ll i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((ll)(x).size())
const ll MOD = 1000000007;
const ll INF = 100000000000000000LL;
const double EPS = 1e-9;
inline ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
inline ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }
inline ll powint(ll x, ll y) {
ll r = 1;
while (y) {
if (y & 1)
r *= x;
x *= x;
y >>= 1;
}
return r;
}
inline ll powmod(ll x, ll y, ll m = MOD) {
ll r = 1;
while (y) {
if (y & 1)
r *= x;
x *= x;
r %= m;
x %= m;
y >>= 1;
}
return r;
}
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
#ifdef OJ_LOCAL
#include "dump.hpp"
#else
#define dump(...) ((void)0)
#endif
// http://orolog.hatenablog.jp/entry/20100804/1280943541
// a * b % mを返す.
ll multMod(ll a, ll b, ll m) {
ll res = 0;
a %= m;
while (b) {
if (b & 1) {
res = (res + a) % m;
}
a = (a * 2) % m;
b = b / 2;
}
return res;
}
// cははじめ1を入れる.
// numが素数の場合無限ループしてしまう.判定もするなら適度なcで打ち切る.*/
ll PollardRho(ll num, ll c) {
if (c > 1000)
return -1;
ll x, y, p;
x = y = p = 1LL;
while (p == 1) {
x = (multMod(x, x, num) + c) % num;
ll ty = (multMod(y, y, num) + c) % num;
y = (multMod(ty, ty, num) + c) % num;
if (x == y)
return PollardRho(num, c + 1);
p = GCD(num, llabs(x - y));
}
return p;
}
// https://ei1333.github.io/luzhiled/snippets/math/divisor.html
vector<ll> divisor(ll n) {
vector<ll> ret;
for (ll i = sqrt(n) + 1; i >= 1; i--) {
if (n % i == 0) {
ret.push_back(i);
return (ret);
}
}
return (ret);
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
ll s;
cin >> s;
/*
ll div = PollardRho(s,1);
dump(div);
if(div == -1){
cout << "0 0 0 " << s << " 1 0" << endl;
return 0;
}else{
vll divs;
divs = divisor(s);
cout << "0 0 0 " << divs[0] << " " << s/divs[0] << " 0" << endl;
return 0;
}
*/
if (s <= (ll)1e9) {
cout << "0 0 0 " << s << " 1 0" << endl;
return 0;
}
ll cl = 1;
for (ll i = sqrt(s) + EPS; i <= (ll)1e9; i++)
for (ll j = sqrt(s) + EPS, c = 0; j <= (ll)1e9 && c <= cl; j++, c++) {
if (i * j == s) {
cout << "0 0 ";
cout << i;
cout << " ";
cout << 0;
cout << " ";
cout << 0;
cout << " ";
cout << j;
cout << endl;
return 0;
}
ll x = i * j - s;
if (x < 0)
continue;
dump(i, j, x);
if (x <= (ll)1e9) {
cout << "0 0 ";
cout << i;
cout << " ";
cout << x;
cout << " ";
cout << 1;
cout << " ";
cout << j;
cout << endl;
return 0;
}
ll div = PollardRho(x, 1);
if (div != -1) {
vll divs;
divs = divisor(x);
if (divs.empty())
continue;
cout << "0 0 ";
cout << i;
cout << " ";
cout << divs[0];
cout << " ";
cout << x / divs[0];
cout << " ";
cout << j;
cout << endl;
return 0;
}
cl++;
}
return 0;
} | insert | 147 | 147 | 147 | 162 | TLE | |
p02963 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 1e6 + 1;
const int MOD = 998244353; // 1e9+7;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define debug(x) \
do { \
cerr << #x << ": " << x << "\n"; \
} while (0)
#define debugv(x) \
do { \
cerr << #x << ": "; \
for (auto &e : (x)) \
cerr << e << " "; \
cerr << "\n"; \
} while (0)
#define FAIL0 \
{ \
cout << "0\n"; \
return 0; \
}
#define FAIL1 \
{ \
cout << "-1\n"; \
return 0; \
}
#define FAILNO \
{ \
cout << "No\n"; \
return 0; \
}
#define YES \
{ \
cout << "Yes\n"; \
return 0; \
}
int main() {
#ifdef OJ
freopen("input.txt", "rt", stdin);
// freopen("output.txt", "wt", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
LL S;
cin >> S;
LL ax = 0, ay = 0, bx, by, cx, cy;
LL a = sqrt(S);
LL r = S - a * a;
if (r <= 1e9) {
ay = 1;
bx = a;
by = 0;
cx = r;
cy = a + 1;
} else {
a++;
r = a * a - S;
assert(r <= 1e9);
bx = a;
by = 1;
cx = r;
cy = a;
}
assert(max({ax, ay, bx, by, cx, cy}) <= 1e9);
cout << ax << ' ' << ay << ' ';
cout << bx << ' ' << by << ' ';
cout << cx << ' ' << cy << '\n';
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MAXN = 1e6 + 1;
const int MOD = 998244353; // 1e9+7;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define debug(x) \
do { \
cerr << #x << ": " << x << "\n"; \
} while (0)
#define debugv(x) \
do { \
cerr << #x << ": "; \
for (auto &e : (x)) \
cerr << e << " "; \
cerr << "\n"; \
} while (0)
#define FAIL0 \
{ \
cout << "0\n"; \
return 0; \
}
#define FAIL1 \
{ \
cout << "-1\n"; \
return 0; \
}
#define FAILNO \
{ \
cout << "No\n"; \
return 0; \
}
#define YES \
{ \
cout << "Yes\n"; \
return 0; \
}
int main() {
#ifdef OJ
freopen("input.txt", "rt", stdin);
// freopen("output.txt", "wt", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
LL S;
cin >> S;
LL ax = 0, ay = 0, bx, by, cx, cy;
LL a = sqrt(S);
LL r = S - a * a;
if (r == 0) {
bx = a;
by = 0;
cx = 0;
cy = a;
} else if (r <= 1e9) {
ay = 1;
bx = a;
by = 0;
cx = r;
cy = a + 1;
} else {
a++;
r = a * a - S;
assert(r <= 1e9);
bx = a;
by = 1;
cx = r;
cy = a;
}
assert(max({ax, ay, bx, by, cx, cy}) <= 1e9);
cout << ax << ' ' << ay << ' ';
cout << bx << ' ' << by << ' ';
cout << cx << ' ' << cy << '\n';
return 0;
}
| replace | 59 | 60 | 59 | 65 | 0 | |
p02963 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
ll const INF = 1LL << 60;
int main() {
ll S;
cin >> S;
ll x1, y1 = 10e9, x2 = 1, y2;
x1 = (y1 - S % y1) % y1;
y2 = (S + x1) / x1;
cout << 0 << " " << 0 << " " << y1 << " " << x2 << " " << x1 << " " << y2
<< endl;
return 0;
} | #include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
ll const INF = 1LL << 60;
int main() {
ll S;
cin >> S;
ll x1, y1, x2 = 1000000000, y2 = 1;
x1 = (x2 - S % x2) % x2;
y1 = (S + x1) / x2;
printf("0 0 1000000000 1 %lld %lld\n", x1, y1);
return 0;
} | replace | 18 | 23 | 18 | 22 | 0 | |
p02964 | C++ | Runtime Error | #include <bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using vi = vector<ll>;
using vvi = vector<vi>;
using pi = pair<ll, ll>;
using vpi = vector<pi>;
const ll mod = 7 * 17 * (1 << 23) + 1;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, jumpid[200200], st[200200];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
set<int> pos[100];
vi a;
ll k = 0;
cin >> n >> k;
a.resize(n);
for (int i = 0; i < n; i++)
cin >> a[i], pos[a[i]].insert(i);
int i = 0, j = 1;
jumpid[0] = 1;
st[1] = 0;
int fcyc = -1, cl = 0, nw;
while (true) {
nw = 0;
auto it = pos[a[i]].upper_bound(i);
if (it != pos[a[i]].end())
i = (1 + *it);
else {
i = (1 + *pos[a[i]].begin());
st[++j] = i;
nw = 1;
}
if (i == n)
i = 0, st[++j] = 0, nw = 1;
if (jumpid[i]) {
fcyc = jumpid[i];
cl = j - jumpid[i];
break;
}
if (nw)
jumpid[i] = j;
}
if (k < fcyc) {
k = st[k];
} else {
k = st[fcyc + (k - fcyc) % cl];
}
set<int> z;
vi ans;
for (int i = k; i < n; i++) {
if (z.count(a[i])) {
while (z.count(a[i])) {
z.erase(ans.back());
ans.pop_back();
}
} else
ans.pb(a[i]), z.insert(a[i]);
}
for (auto i : ans)
cout << i << " ";
cout << "\n";
return 0;
}
| #include <bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using vi = vector<ll>;
using vvi = vector<vi>;
using pi = pair<ll, ll>;
using vpi = vector<pi>;
const ll mod = 7 * 17 * (1 << 23) + 1;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, jumpid[200200], st[200200];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
set<int> pos[200200];
vi a;
ll k = 0;
cin >> n >> k;
a.resize(n);
for (int i = 0; i < n; i++)
cin >> a[i], pos[a[i]].insert(i);
int i = 0, j = 1;
jumpid[0] = 1;
st[1] = 0;
int fcyc = -1, cl = 0, nw;
while (true) {
nw = 0;
auto it = pos[a[i]].upper_bound(i);
if (it != pos[a[i]].end())
i = (1 + *it);
else {
i = (1 + *pos[a[i]].begin());
st[++j] = i;
nw = 1;
}
if (i == n)
i = 0, st[++j] = 0, nw = 1;
if (jumpid[i]) {
fcyc = jumpid[i];
cl = j - jumpid[i];
break;
}
if (nw)
jumpid[i] = j;
}
if (k < fcyc) {
k = st[k];
} else {
k = st[fcyc + (k - fcyc) % cl];
}
set<int> z;
vi ans;
for (int i = k; i < n; i++) {
if (z.count(a[i])) {
while (z.count(a[i])) {
z.erase(ans.back());
ans.pop_back();
}
} else
ans.pb(a[i]), z.insert(a[i]);
}
for (auto i : ans)
cout << i << " ";
cout << "\n";
return 0;
}
| replace | 16 | 17 | 16 | 17 | 0 | |
p02964 | C++ | Time Limit Exceeded | #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#include <bits/stdc++.h>
using namespace std;
template <class t> inline t read(t &x) {
char c = getchar();
bool f = 0;
x = 0;
while (!isdigit(c))
f |= c == '-', c = getchar();
while (isdigit(c))
x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
if (f)
x = -x;
return x;
}
template <class t, class... A> inline void read(t &x, A &...a) {
read(x);
read(a...);
}
template <class t> inline void write(t x) {
if (x < 0)
putchar('-'), write(-x);
else {
if (x > 9)
write(x / 10);
putchar('0' + x % 10);
}
}
#define int long long
const int N = 2e5 + 5;
int n, k, a[N], st[N], last[N], nxt[N], rd = 1, ed[N];
vector<int> roll;
signed main() {
freopen("01-02.txt", "r", stdin);
read(n, k);
for (int i = 1; i <= n; i++)
read(a[i]), st[a[i]] ? 1 : st[a[i]] = i;
for (int i = n; i; i--)
nxt[i] = last[a[i]] ? last[a[i]] : st[a[i]], last[a[i]] = i;
for (int x = 1; x <= n; x = nxt[x] + 1)
ed[rd] = roll.size(), roll.push_back(x), nxt[x] <= x ? ++rd : 1;
ed[rd] = roll.size();
roll.push_back(n + 1);
k %= rd;
if (!k)
k = rd;
ed[0] = -1;
// for(int i=1;i<=rd;i++){
// putchar('#');
// for(int j=ed[i-1]+1;j<=ed[i];j++) write(roll[j]),putchar(' ');
// puts("");
// }
// printf("@%lld %lld\n",roll[ed[k]],nxt[roll[ed[k]]]);
for (int i = 1; i <= n; i++)
last[a[i]] = nxt[a[i]] = 0;
for (int i = n; i >= roll[ed[k]]; i--)
nxt[i] = last[a[i]] ? last[a[i]] + 1 : i + 1, last[a[i]] = i;
for (int i = roll[ed[k]], j; i <= n; i = j + 1) {
j = i;
while (nxt[j] != j + 1 && j <= n)
j = nxt[j];
if (j > n)
break;
write(a[j]);
putchar(' ');
}
} | #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#include <bits/stdc++.h>
using namespace std;
template <class t> inline t read(t &x) {
char c = getchar();
bool f = 0;
x = 0;
while (!isdigit(c))
f |= c == '-', c = getchar();
while (isdigit(c))
x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
if (f)
x = -x;
return x;
}
template <class t, class... A> inline void read(t &x, A &...a) {
read(x);
read(a...);
}
template <class t> inline void write(t x) {
if (x < 0)
putchar('-'), write(-x);
else {
if (x > 9)
write(x / 10);
putchar('0' + x % 10);
}
}
#define int long long
const int N = 2e5 + 5;
int n, k, a[N], st[N], last[N], nxt[N], rd = 1, ed[N];
vector<int> roll;
signed main() {
read(n, k);
for (int i = 1; i <= n; i++)
read(a[i]), st[a[i]] ? 1 : st[a[i]] = i;
for (int i = n; i; i--)
nxt[i] = last[a[i]] ? last[a[i]] : st[a[i]], last[a[i]] = i;
for (int x = 1; x <= n; x = nxt[x] + 1)
ed[rd] = roll.size(), roll.push_back(x), nxt[x] <= x ? ++rd : 1;
ed[rd] = roll.size();
roll.push_back(n + 1);
k %= rd;
if (!k)
k = rd;
ed[0] = -1;
// for(int i=1;i<=rd;i++){
// putchar('#');
// for(int j=ed[i-1]+1;j<=ed[i];j++) write(roll[j]),putchar(' ');
// puts("");
// }
// printf("@%lld %lld\n",roll[ed[k]],nxt[roll[ed[k]]]);
for (int i = 1; i <= n; i++)
last[a[i]] = nxt[a[i]] = 0;
for (int i = n; i >= roll[ed[k]]; i--)
nxt[i] = last[a[i]] ? last[a[i]] + 1 : i + 1, last[a[i]] = i;
for (int i = roll[ed[k]], j; i <= n; i = j + 1) {
j = i;
while (nxt[j] != j + 1 && j <= n)
j = nxt[j];
if (j > n)
break;
write(a[j]);
putchar(' ');
}
} | delete | 36 | 37 | 36 | 36 | TLE |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.