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 |
|---|---|---|---|---|---|---|---|---|---|---|---|
p02589 | C++ | Runtime Error | #include <iostream>
using namespace std;
typedef long long ll;
int n;
string s[202020];
int l[202020];
int b[202020];
int tr[1222333][26];
int tc;
int th[1222333][26];
void add(int i, int k) {
int u = 0;
for (int j = k; j >= 1; j--) {
for (int h = 0; h < 26; h++) {
if (b[j] & (1 << h))
th[u][h]++;
}
int c = s[i][j] - 'a';
if (tr[u][c] == 0)
tr[u][c] = ++tc;
u = tr[u][c];
}
}
int calc(int i, int k) {
int u = 0;
for (int j = k; j >= 2; j--) {
int c = s[i][j] - 'a';
u = tr[u][c];
}
return th[u][s[i][1] - 'a'];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> s[i];
l[i] = s[i].size();
s[i] = "#" + s[i];
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= l[i]; j++) {
b[j] = b[j - 1] | (1 << (s[i][j] - 'a'));
}
add(i, l[i]);
}
ll c = 0;
for (int i = 1; i <= n; i++) {
// cout << i << " " << s[i] << " " << calc(i,l[i]) << "\n";
c += calc(i, l[i]) - 1;
}
cout << c << "\n";
}
| #include <iostream>
using namespace std;
typedef long long ll;
int n;
string s[202020];
int l[202020];
int b[1222333];
int tr[1222333][26];
int tc;
int th[1222333][26];
void add(int i, int k) {
int u = 0;
for (int j = k; j >= 1; j--) {
for (int h = 0; h < 26; h++) {
if (b[j] & (1 << h))
th[u][h]++;
}
int c = s[i][j] - 'a';
if (tr[u][c] == 0)
tr[u][c] = ++tc;
u = tr[u][c];
}
}
int calc(int i, int k) {
int u = 0;
for (int j = k; j >= 2; j--) {
int c = s[i][j] - 'a';
u = tr[u][c];
}
return th[u][s[i][1] - 'a'];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> s[i];
l[i] = s[i].size();
s[i] = "#" + s[i];
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= l[i]; j++) {
b[j] = b[j - 1] | (1 << (s[i][j] - 'a'));
}
add(i, l[i]);
}
ll c = 0;
for (int i = 1; i <= n; i++) {
// cout << i << " " << s[i] << " " << calc(i,l[i]) << "\n";
c += calc(i, l[i]) - 1;
}
cout << c << "\n";
}
| replace | 9 | 10 | 9 | 10 | -11 | |
p02589 | C++ | Runtime Error | #include <cstdio>
#include <cstring>
int ch[1000010][26], pr[1000010];
int ct[1000010], ex[100010][26];
bool fn[100010];
int N;
void add(int i, char *s) {
// printf("%d %c\n",i,*s);
if (*s == 0) {
fn[i] = true;
} else if (ch[i][(*s) - 'a'] == -1) {
ch[i][(*s) - 'a'] = N;
for (int x = 0; x < 26; x++) {
ch[N][x] = -1;
}
fn[N] = false;
N++;
add(N - 1, s + 1);
} else {
add(ch[i][(*s) - 'a'], s + 1);
}
}
long long ans;
void count(int i) {
ct[i] = (fn[i] ? 1 : 0);
for (int y = 0; y < 26; y++) {
ex[i][y] = 0;
}
for (int x = 0; x < 26; x++) {
int j = ch[i][x];
if (j == -1)
continue;
count(j);
ct[i] += ct[j];
for (int y = 0; y < 26; y++) {
if (y == x) {
ex[i][y] += ct[j];
} else {
ex[i][y] += ex[j][y];
}
}
}
for (int x = 0; x < 26; x++) {
int j = ch[i][x];
if (j != -1 && fn[j]) {
ans += ex[i][x] - 1;
}
}
}
int main() {
int n;
scanf("%d", &n);
for (int x = 0; x < 26; x++) {
ch[0][x] = -1;
}
fn[0] = false;
N = 1;
for (int i = 0; i < n; i++) {
static char t[1000010], s[1000010];
scanf("%s", t);
int l = strlen(t);
for (int j = 0; j < l; j++) {
s[l - j - 1] = t[j];
}
s[l] = 0;
add(0, s);
}
ans = 0ll;
count(0);
printf("%lld\n", ans);
return 0;
}
| #include <cstdio>
#include <cstring>
int ch[1000010][26], pr[1000010];
int ct[1000010], ex[1000010][26];
bool fn[1000010];
int N;
void add(int i, char *s) {
// printf("%d %c\n",i,*s);
if (*s == 0) {
fn[i] = true;
} else if (ch[i][(*s) - 'a'] == -1) {
ch[i][(*s) - 'a'] = N;
for (int x = 0; x < 26; x++) {
ch[N][x] = -1;
}
fn[N] = false;
N++;
add(N - 1, s + 1);
} else {
add(ch[i][(*s) - 'a'], s + 1);
}
}
long long ans;
void count(int i) {
ct[i] = (fn[i] ? 1 : 0);
for (int y = 0; y < 26; y++) {
ex[i][y] = 0;
}
for (int x = 0; x < 26; x++) {
int j = ch[i][x];
if (j == -1)
continue;
count(j);
ct[i] += ct[j];
for (int y = 0; y < 26; y++) {
if (y == x) {
ex[i][y] += ct[j];
} else {
ex[i][y] += ex[j][y];
}
}
}
for (int x = 0; x < 26; x++) {
int j = ch[i][x];
if (j != -1 && fn[j]) {
ans += ex[i][x] - 1;
}
}
}
int main() {
int n;
scanf("%d", &n);
for (int x = 0; x < 26; x++) {
ch[0][x] = -1;
}
fn[0] = false;
N = 1;
for (int i = 0; i < n; i++) {
static char t[1000010], s[1000010];
scanf("%s", t);
int l = strlen(t);
for (int j = 0; j < l; j++) {
s[l - j - 1] = t[j];
}
s[l] = 0;
add(0, s);
}
ans = 0ll;
count(0);
printf("%lld\n", ans);
return 0;
}
| replace | 4 | 6 | 4 | 6 | -11 | |
p02589 | C++ | Runtime Error | // Coded by Abhijay Mitra
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <vector>
// #include <bits/stdc++.h>
#define double long double
#define int long long int
#define ll int
#define ibs ios_base::sync_with_stdio(false)
#define cti cin.tie(0)
#define bp __builtin_popcount
#define pb emplace_back
#define koto_memory(a) cout << (sizeof(a) / 1048576.0) << " MB";
#define res(v) sort(all(v)), v.erase(unique(all(v)), v.end());
#define timer \
cerr << "Time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " sec \n";
#define deb(x) cout << "\n[" << #x << " = " << x << "]\n";
using vi = std::vector<int>;
using vvi = std::vector<vi>;
using pii = std::pair<int, int>;
using vpii = std::vector<pii>;
using vvpii = std::vector<vpii>;
#define mp make_pair
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, b, a) for (int i = b; i >= a; i--)
#define all(x) x.begin(), x.end()
using namespace std;
const int N = 2e5 + 10;
const int inf = /*0x3f3f3f3f*/ 1e18 + 10;
// const ll M = 998244353 ; // Mulo
// const int M = 1e9+7 ; // Mulo
const double Pi = 3.14159265;
#define F first
#define S second
int n, child[N][26], is_end[N * 26], nxt = 1, ans;
string s[N];
vvi a;
void solve() {
cin >> n;
a.resize(n + 1);
rep(i, 1, n) {
cin >> s[i];
reverse(s[i].begin(), s[i].end());
int node = 0;
for (int j = 0; j < s[i].length(); j++) {
a[i].push_back(node);
if (child[node][s[i][j] - 'a'] == 0)
child[node][s[i][j] - 'a'] = nxt++;
node = child[node][s[i][j] - 'a'];
}
is_end[node] = 1;
}
rep(i, 1, n) {
set<int> prefix;
// s[i].length()-1 elements must match in original string
for (int j = s[i].length() - 1; j > -1; j--) {
prefix.insert(s[i][j]);
int node = a[i][j];
for (auto tmp : prefix)
if (is_end[child[node][tmp - 'a']] == 1)
ans++;
}
}
cout << ans - n;
}
int32_t main() {
ibs;
cti;
solve();
return 0;
/*,cout<<"\n"*/;
// cout<<"\n";
int xx = 0;
int t;
cin >> t;
while (t--) { /*xx++;cout<<"Case "<<xx<<":\n"*/
;
solve();
cout << "\n";
}
return 0;
} | // Coded by Abhijay Mitra
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <vector>
// #include <bits/stdc++.h>
#define double long double
#define int long long int
#define ll int
#define ibs ios_base::sync_with_stdio(false)
#define cti cin.tie(0)
#define bp __builtin_popcount
#define pb emplace_back
#define koto_memory(a) cout << (sizeof(a) / 1048576.0) << " MB";
#define res(v) sort(all(v)), v.erase(unique(all(v)), v.end());
#define timer \
cerr << "Time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " sec \n";
#define deb(x) cout << "\n[" << #x << " = " << x << "]\n";
using vi = std::vector<int>;
using vvi = std::vector<vi>;
using pii = std::pair<int, int>;
using vpii = std::vector<pii>;
using vvpii = std::vector<vpii>;
#define mp make_pair
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, b, a) for (int i = b; i >= a; i--)
#define all(x) x.begin(), x.end()
using namespace std;
const int N = 2e6 + 10;
const int inf = /*0x3f3f3f3f*/ 1e18 + 10;
// const ll M = 998244353 ; // Mulo
// const int M = 1e9+7 ; // Mulo
const double Pi = 3.14159265;
#define F first
#define S second
int n, child[N][26], is_end[N * 26], nxt = 1, ans;
string s[N];
vvi a;
void solve() {
cin >> n;
a.resize(n + 1);
rep(i, 1, n) {
cin >> s[i];
reverse(s[i].begin(), s[i].end());
int node = 0;
for (int j = 0; j < s[i].length(); j++) {
a[i].push_back(node);
if (child[node][s[i][j] - 'a'] == 0)
child[node][s[i][j] - 'a'] = nxt++;
node = child[node][s[i][j] - 'a'];
}
is_end[node] = 1;
}
rep(i, 1, n) {
set<int> prefix;
// s[i].length()-1 elements must match in original string
for (int j = s[i].length() - 1; j > -1; j--) {
prefix.insert(s[i][j]);
int node = a[i][j];
for (auto tmp : prefix)
if (is_end[child[node][tmp - 'a']] == 1)
ans++;
}
}
cout << ans - n;
}
int32_t main() {
ibs;
cti;
solve();
return 0;
/*,cout<<"\n"*/;
// cout<<"\n";
int xx = 0;
int t;
cin >> t;
while (t--) { /*xx++;cout<<"Case "<<xx<<":\n"*/
;
solve();
cout << "\n";
}
return 0;
} | replace | 40 | 41 | 40 | 41 | 0 | |
p02589 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
#define idfc \
ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
//:/
#define pb push_back
#define mp make_pair
#define nt _ll128
#define ld double
long double PI = 3.14159265;
using ll = long long;
const ll modo = 1e9 + 7;
const ll ms = 2e5 + 5;
const int inf = 1e9 + 9;
int trie[ms][26] = {0};
int ct[ms][26] = {0};
int tim = 1;
int ar[26] = {0};
string br[ms];
int query(string s) {
int n = s.length();
int i = 0;
int v = 0;
n--;
int p = 1;
while (i < n) {
v = trie[v][s[i] - 97];
if (!v) {
return 0;
}
i++;
}
return ct[v][s[i] - 97];
cout << s << " " << p << "\n";
return p;
}
void add(string s) {
int i = 0;
int v = 0;
int n;
n = s.length();
for (char e : s)
ar[e - 97]++;
i = 0;
v = 0;
int j;
while (i < n) {
for (j = 0; j < 26; j++) {
if (ar[j])
ct[v][j]++;
}
if (!trie[v][s[i] - 97]) {
trie[v][s[i] - 97] = tim;
tim++;
}
v = trie[v][s[i] - 97];
ar[s[i] - 97]--;
i++;
}
}
int main() {
idfc;
int n;
cin >> n;
int i;
ll ans = 0;
string s;
for (i = 1; i <= n; i++) {
cin >> s;
reverse(s.begin(), s.end());
br[i] = s;
add(s);
}
for (i = 1; i <= n; i++) {
ans += (query(br[i]) - 1);
}
cout << ans;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
#define idfc \
ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
//:/
#define pb push_back
#define mp make_pair
#define nt _ll128
#define ld double
long double PI = 3.14159265;
using ll = long long;
const ll modo = 1e9 + 7;
const ll ms = 1e6 + 5;
const int inf = 1e9 + 9;
int trie[ms][26] = {0};
int ct[ms][26] = {0};
int tim = 1;
int ar[26] = {0};
string br[ms];
int query(string s) {
int n = s.length();
int i = 0;
int v = 0;
n--;
int p = 1;
while (i < n) {
v = trie[v][s[i] - 97];
if (!v) {
return 0;
}
i++;
}
return ct[v][s[i] - 97];
cout << s << " " << p << "\n";
return p;
}
void add(string s) {
int i = 0;
int v = 0;
int n;
n = s.length();
for (char e : s)
ar[e - 97]++;
i = 0;
v = 0;
int j;
while (i < n) {
for (j = 0; j < 26; j++) {
if (ar[j])
ct[v][j]++;
}
if (!trie[v][s[i] - 97]) {
trie[v][s[i] - 97] = tim;
tim++;
}
v = trie[v][s[i] - 97];
ar[s[i] - 97]--;
i++;
}
}
int main() {
idfc;
int n;
cin >> n;
int i;
ll ans = 0;
string s;
for (i = 1; i <= n; i++) {
cin >> s;
reverse(s.begin(), s.end());
br[i] = s;
add(s);
}
for (i = 1; i <= n; i++) {
ans += (query(br[i]) - 1);
}
cout << ans;
return 0;
} | replace | 14 | 15 | 14 | 15 | 0 | |
p02589 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
const int N = 200005;
const int M = 1000005;
void getint(int &num) {
char c;
bool flag = 0;
num = 0;
while ((c = getchar()) < '0' || c > '9')
if (c == '-')
flag = 1;
while (c >= '0' && c <= '9') {
num = num * 10 + c - 48;
c = getchar();
}
if (flag)
num = -num;
}
int n, first[30], len[N], id[N], tot[30];
int ch[N][27], sz[M], letter[M][27], cnt = 1;
ll ans;
string str[N];
map<string, int> vis;
inline void insert(string str) {
int m = str.length(), x = 1;
for (int i = 0; i < 26; i++)
first[i] = -1;
for (int i = 0; i < m; i++)
if (first[str[i] - 'a'] == -1) {
first[str[i] - 'a'] = i;
tot[str[i] - 'a']++;
}
if (m == 1) {
ans += tot[str[0] - 'a'] - 1;
return;
}
for (int i = m - 1; i >= 0; i--) {
int y = str[i] - 'a';
if (!ch[x][y])
ch[x][y] = ++cnt;
x = ch[x][y];
sz[x]++;
if (i == 1)
ans += letter[x][str[0] - 'a'];
for (int j = 0; j < 26; j++)
if ((~first[j]) && first[j] < i)
letter[x][j]++;
}
}
bool cmp(int i, int j) { return len[i] > len[j]; }
int main() {
getint(n);
for (int i = 1; i <= n; i++) {
cin >> str[i];
len[i] = str[i].length();
id[i] = i;
}
sort(id + 1, id + n + 1, cmp);
for (int i = 1; i <= n; i++) {
insert(str[id[i]]);
if (!vis.count(str[id[i]]))
vis[str[id[i]]] = 1;
else
ans += vis[str[id[i]]];
}
cout << ans << endl;
}
/*
*/
| #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
const int N = 200005;
const int M = 1000005;
void getint(int &num) {
char c;
bool flag = 0;
num = 0;
while ((c = getchar()) < '0' || c > '9')
if (c == '-')
flag = 1;
while (c >= '0' && c <= '9') {
num = num * 10 + c - 48;
c = getchar();
}
if (flag)
num = -num;
}
int n, first[30], len[N], id[N], tot[30];
int ch[M][27], sz[M], letter[M][27], cnt = 1;
ll ans;
string str[N];
map<string, int> vis;
inline void insert(string str) {
int m = str.length(), x = 1;
for (int i = 0; i < 26; i++)
first[i] = -1;
for (int i = 0; i < m; i++)
if (first[str[i] - 'a'] == -1) {
first[str[i] - 'a'] = i;
tot[str[i] - 'a']++;
}
if (m == 1) {
ans += tot[str[0] - 'a'] - 1;
return;
}
for (int i = m - 1; i >= 0; i--) {
int y = str[i] - 'a';
if (!ch[x][y])
ch[x][y] = ++cnt;
x = ch[x][y];
sz[x]++;
if (i == 1)
ans += letter[x][str[0] - 'a'];
for (int j = 0; j < 26; j++)
if ((~first[j]) && first[j] < i)
letter[x][j]++;
}
}
bool cmp(int i, int j) { return len[i] > len[j]; }
int main() {
getint(n);
for (int i = 1; i <= n; i++) {
cin >> str[i];
len[i] = str[i].length();
id[i] = i;
}
sort(id + 1, id + n + 1, cmp);
for (int i = 1; i <= n; i++) {
insert(str[id[i]]);
if (!vis.count(str[id[i]]))
vis[str[id[i]]] = 1;
else
ans += vis[str[id[i]]];
}
cout << ans << endl;
}
/*
*/
| replace | 29 | 30 | 29 | 30 | -11 | |
p02589 | C++ | Time Limit Exceeded | /* #region header */
#ifdef LOCAL
#include "/Users/takakurashokichi/Desktop/atcoder/cxx-prettyprint-master/prettyprint.hpp"
#define debug(x) cout << x << endl
#else
#define debug(...) 42
#endif
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
// types
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
typedef pair<ll, ll> Pl;
typedef pair<int, int> Pi;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vc;
template <typename T> using mat = vector<vector<T>>;
typedef vector<vector<int>> vvi;
typedef vector<vector<long long>> vvl;
typedef vector<vector<char>> vvc;
template <int mod> struct modint {
int x;
modint() : x(0) {}
modint(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
modint &operator+=(const modint &p) {
if ((x += p.x) >= mod)
x -= mod;
return *this;
}
modint &operator-=(const modint &p) {
if ((x += mod - p.x) >= mod)
x -= mod;
return *this;
}
modint &operator*=(const modint &p) {
x = (int)(1LL * x * p.x % mod);
return *this;
}
modint &operator/=(const modint &p) {
*this *= p.inverse();
return *this;
}
modint operator-() const { return modint(-x); }
modint operator+(const modint &p) const { return modint(*this) += p; }
modint operator-(const modint &p) const { return modint(*this) -= p; }
modint operator*(const modint &p) const { return modint(*this) *= p; }
modint operator/(const modint &p) const { return modint(*this) /= p; }
bool operator==(const modint &p) const { return x == p.x; }
bool operator!=(const modint &p) const { return x != p.x; }
modint inverse() const {
int a = x, b = mod, u = 1, v = 0, t;
while (b > 0) {
t = a / b;
swap(a -= t * b, b);
swap(u -= t * v, v);
}
return modint(u);
}
modint pow(int64_t n) const {
modint ret(1), mul(x);
while (n > 0) {
if (n & 1)
ret *= mul;
mul *= mul;
n >>= 1;
}
return ret;
}
friend ostream &operator<<(ostream &os, const modint &p) { return os << p.x; }
friend istream &operator>>(istream &is, modint &a) {
int64_t t;
is >> t;
a = modint<mod>(t);
return (is);
}
static int get_mod() { return mod; }
};
// abreviations
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep_(i, a_, b_, a, b, ...) for (ll i = (a), max_i = (b); i < max_i; i++)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define rrep_(i, a_, b_, a, b, ...) \
for (ll i = (b - 1), min_i = (a); i >= min_i; i--)
#define rrep(i, ...) rrep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define SZ(x) ((ll)(x).size())
#define pb(x) push_back(x)
#define eb(x) emplace_back(x)
#define mp make_pair
#define print(x) cout << x << endl
#define vprint(x) \
rep(i, x.size()) cout << x[i] << ' '; \
cout << endl
#define vsum(x) accumulate(all(x), 0LL)
#define vmax(a) *max_element(all(a))
#define vmin(a) *min_element(all(a))
#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))
// functions
// gcd(0, x) fails.
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
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;
}
template <typename T> T mypow(T x, ll n) {
T ret = 1;
while (n > 0) {
if (n & 1)
(ret *= x);
(x *= x);
n >>= 1;
}
return ret;
}
ll modpow(ll x, ll n, const ll mod) {
ll ret = 1;
while (n > 0) {
if (n & 1)
(ret *= x);
(x *= x);
n >>= 1;
x %= mod;
ret %= mod;
}
return ret;
}
uint64_t my_rand(void) {
static uint64_t x = 88172645463325252ULL;
x = x ^ (x << 13);
x = x ^ (x >> 7);
return x = x ^ (x << 17);
}
ll popcnt(ull x) { return __builtin_popcountll(x); }
// graph template
template <typename T> struct edge {
int src, to;
T cost;
edge(int to, T cost) : src(-1), to(to), cost(cost) {}
edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
edge &operator=(const int &x) {
to = x;
return *this;
}
bool operator<(const edge<T> &r) const { return cost < r.cost; }
operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
struct Timer {
clock_t start_time;
void start() { start_time = clock(); }
int lap() {
// return x ms.
return (clock() - start_time) * 1000 / CLOCKS_PER_SEC;
}
};
/* #endregion*/
// constant
#define inf 1000000005
#define INF 4000000004000000000LL
#define mod 1000000007LL
#define endl '\n'
typedef modint<mod> mint;
const long double eps = 0.000001;
const long double PI = 3.141592653589793;
// library
template <typename Monoid> struct SegmentTree {
using F = function<Monoid(Monoid, Monoid)>;
int sz;
vector<Monoid> seg;
const F f;
const Monoid M1;
SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
sz = 1;
while (sz < n)
sz <<= 1;
seg.assign(2 * sz, M1);
}
void set(int k, const Monoid &x) { seg[k + sz] = x; }
void build() {
for (int k = sz - 1; k > 0; k--) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
void update(int k, const Monoid &x) {
k += sz;
seg[k] = x;
while (k >>= 1) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
Monoid query(int a, int b) {
Monoid L = M1, R = M1;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1)
L = f(L, seg[a++]);
if (b & 1)
R = f(seg[--b], R);
}
return f(L, R);
}
Monoid operator[](const int &k) const { return seg[k + sz]; }
template <typename C>
int find_subtree(int a, const C &check, Monoid &M, bool type) {
while (a < sz) {
Monoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]);
if (check(nxt))
a = 2 * a + type;
else
M = nxt, a = 2 * a + 1 - type;
}
return a - sz;
}
// check(seg[i])を満たす最小のb<=iを返す.なければ-1
template <typename C> int find_first(int a, const C &check) {
Monoid L = M1;
if (a <= 0) {
if (check(f(L, seg[1])))
return find_subtree(1, check, L, false);
return -1;
}
int b = sz;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1) {
Monoid nxt = f(L, seg[a]);
if (check(nxt))
return find_subtree(a, check, L, false);
L = nxt;
++a;
}
}
return -1;
}
// check(seg[i])を満たす最小のi<bを返す.なければ-1
template <typename C> int find_last(int b, const C &check) {
Monoid R = M1;
if (b >= sz) {
if (check(f(seg[1], R)))
return find_subtree(1, check, R, true);
return -1;
}
int a = sz;
for (b += sz; a < b; a >>= 1, b >>= 1) {
if (b & 1) {
Monoid nxt = f(seg[--b], R);
if (check(nxt))
return find_subtree(b, check, R, true);
R = nxt;
}
}
return -1;
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
cout << setprecision(20);
ll n;
cin >> n;
vector<string> S(n);
rep(i, n) {
cin >> S[i];
reverse(all(S[i]));
}
sort(all(S));
vl id(n);
iota(all(id), 0);
sort(all(id), [&](int i, int j) { return S[i].size() > S[j].size(); });
auto f = [&](ll x, ll y) { return x + y; };
vector<SegmentTree<ll>> seg(26, SegmentTree<ll>(n, f, 0));
unordered_set<ll> que;
ll len = S[id[0]].size();
ll ans = 0;
for (int i : id) {
for (int j : que) {
rep(k, S[i].size() - 1, len) seg[S[j][k] - 'a'].update(j, 1);
}
chmin(len, (ll)S[i].size() - 1);
que.insert(i);
seg[S[i].back() - 'a'].update(i, 1);
string x = "";
rep(j, S[i].size() - 1) x += S[i][j];
ll l = ub(S, x);
ll r = ub(S, x + '{');
ans += seg[S[i].back() - 'a'].query(l, r) - 1;
}
print(ans);
}
| /* #region header */
#ifdef LOCAL
#include "/Users/takakurashokichi/Desktop/atcoder/cxx-prettyprint-master/prettyprint.hpp"
#define debug(x) cout << x << endl
#else
#define debug(...) 42
#endif
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
// types
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
typedef pair<ll, ll> Pl;
typedef pair<int, int> Pi;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vc;
template <typename T> using mat = vector<vector<T>>;
typedef vector<vector<int>> vvi;
typedef vector<vector<long long>> vvl;
typedef vector<vector<char>> vvc;
template <int mod> struct modint {
int x;
modint() : x(0) {}
modint(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
modint &operator+=(const modint &p) {
if ((x += p.x) >= mod)
x -= mod;
return *this;
}
modint &operator-=(const modint &p) {
if ((x += mod - p.x) >= mod)
x -= mod;
return *this;
}
modint &operator*=(const modint &p) {
x = (int)(1LL * x * p.x % mod);
return *this;
}
modint &operator/=(const modint &p) {
*this *= p.inverse();
return *this;
}
modint operator-() const { return modint(-x); }
modint operator+(const modint &p) const { return modint(*this) += p; }
modint operator-(const modint &p) const { return modint(*this) -= p; }
modint operator*(const modint &p) const { return modint(*this) *= p; }
modint operator/(const modint &p) const { return modint(*this) /= p; }
bool operator==(const modint &p) const { return x == p.x; }
bool operator!=(const modint &p) const { return x != p.x; }
modint inverse() const {
int a = x, b = mod, u = 1, v = 0, t;
while (b > 0) {
t = a / b;
swap(a -= t * b, b);
swap(u -= t * v, v);
}
return modint(u);
}
modint pow(int64_t n) const {
modint ret(1), mul(x);
while (n > 0) {
if (n & 1)
ret *= mul;
mul *= mul;
n >>= 1;
}
return ret;
}
friend ostream &operator<<(ostream &os, const modint &p) { return os << p.x; }
friend istream &operator>>(istream &is, modint &a) {
int64_t t;
is >> t;
a = modint<mod>(t);
return (is);
}
static int get_mod() { return mod; }
};
// abreviations
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep_(i, a_, b_, a, b, ...) for (ll i = (a), max_i = (b); i < max_i; i++)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define rrep_(i, a_, b_, a, b, ...) \
for (ll i = (b - 1), min_i = (a); i >= min_i; i--)
#define rrep(i, ...) rrep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define SZ(x) ((ll)(x).size())
#define pb(x) push_back(x)
#define eb(x) emplace_back(x)
#define mp make_pair
#define print(x) cout << x << endl
#define vprint(x) \
rep(i, x.size()) cout << x[i] << ' '; \
cout << endl
#define vsum(x) accumulate(all(x), 0LL)
#define vmax(a) *max_element(all(a))
#define vmin(a) *min_element(all(a))
#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))
// functions
// gcd(0, x) fails.
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
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;
}
template <typename T> T mypow(T x, ll n) {
T ret = 1;
while (n > 0) {
if (n & 1)
(ret *= x);
(x *= x);
n >>= 1;
}
return ret;
}
ll modpow(ll x, ll n, const ll mod) {
ll ret = 1;
while (n > 0) {
if (n & 1)
(ret *= x);
(x *= x);
n >>= 1;
x %= mod;
ret %= mod;
}
return ret;
}
uint64_t my_rand(void) {
static uint64_t x = 88172645463325252ULL;
x = x ^ (x << 13);
x = x ^ (x >> 7);
return x = x ^ (x << 17);
}
ll popcnt(ull x) { return __builtin_popcountll(x); }
// graph template
template <typename T> struct edge {
int src, to;
T cost;
edge(int to, T cost) : src(-1), to(to), cost(cost) {}
edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
edge &operator=(const int &x) {
to = x;
return *this;
}
bool operator<(const edge<T> &r) const { return cost < r.cost; }
operator int() const { return to; }
};
template <typename T> using Edges = vector<edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
struct Timer {
clock_t start_time;
void start() { start_time = clock(); }
int lap() {
// return x ms.
return (clock() - start_time) * 1000 / CLOCKS_PER_SEC;
}
};
/* #endregion*/
// constant
#define inf 1000000005
#define INF 4000000004000000000LL
#define mod 1000000007LL
#define endl '\n'
typedef modint<mod> mint;
const long double eps = 0.000001;
const long double PI = 3.141592653589793;
// library
template <typename Monoid> struct SegmentTree {
using F = function<Monoid(Monoid, Monoid)>;
int sz;
vector<Monoid> seg;
const F f;
const Monoid M1;
SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
sz = 1;
while (sz < n)
sz <<= 1;
seg.assign(2 * sz, M1);
}
void set(int k, const Monoid &x) { seg[k + sz] = x; }
void build() {
for (int k = sz - 1; k > 0; k--) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
void update(int k, const Monoid &x) {
k += sz;
seg[k] = x;
while (k >>= 1) {
seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
}
}
Monoid query(int a, int b) {
Monoid L = M1, R = M1;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1)
L = f(L, seg[a++]);
if (b & 1)
R = f(seg[--b], R);
}
return f(L, R);
}
Monoid operator[](const int &k) const { return seg[k + sz]; }
template <typename C>
int find_subtree(int a, const C &check, Monoid &M, bool type) {
while (a < sz) {
Monoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]);
if (check(nxt))
a = 2 * a + type;
else
M = nxt, a = 2 * a + 1 - type;
}
return a - sz;
}
// check(seg[i])を満たす最小のb<=iを返す.なければ-1
template <typename C> int find_first(int a, const C &check) {
Monoid L = M1;
if (a <= 0) {
if (check(f(L, seg[1])))
return find_subtree(1, check, L, false);
return -1;
}
int b = sz;
for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
if (a & 1) {
Monoid nxt = f(L, seg[a]);
if (check(nxt))
return find_subtree(a, check, L, false);
L = nxt;
++a;
}
}
return -1;
}
// check(seg[i])を満たす最小のi<bを返す.なければ-1
template <typename C> int find_last(int b, const C &check) {
Monoid R = M1;
if (b >= sz) {
if (check(f(seg[1], R)))
return find_subtree(1, check, R, true);
return -1;
}
int a = sz;
for (b += sz; a < b; a >>= 1, b >>= 1) {
if (b & 1) {
Monoid nxt = f(seg[--b], R);
if (check(nxt))
return find_subtree(b, check, R, true);
R = nxt;
}
}
return -1;
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
cout << setprecision(20);
ll n;
cin >> n;
vector<string> S(n);
rep(i, n) {
cin >> S[i];
reverse(all(S[i]));
}
sort(all(S));
vl id(n);
iota(all(id), 0);
sort(all(id), [&](int i, int j) { return S[i].size() > S[j].size(); });
auto f = [&](ll x, ll y) { return x + y; };
vector<SegmentTree<ll>> seg(26, SegmentTree<ll>(n, f, 0));
unordered_set<ll> que;
ll len = S[id[0]].size();
ll ans = 0;
for (int i : id) {
if (len > S[i].size() - 1) {
for (int j : que) {
rep(k, S[i].size() - 1, len) seg[S[j][k] - 'a'].update(j, 1);
}
}
chmin(len, (ll)S[i].size() - 1);
que.insert(i);
seg[S[i].back() - 'a'].update(i, 1);
string x = "";
rep(j, S[i].size() - 1) x += S[i][j];
ll l = ub(S, x);
ll r = ub(S, x + '{');
ans += seg[S[i].back() - 'a'].query(l, r) - 1;
}
print(ans);
}
| replace | 328 | 330 | 328 | 332 | TLE | |
p02589 | C++ | Runtime Error | /*
In the name of ALLAH
Author : Raashid Anwar
*/
#include <bits/stdc++.h>
using namespace std;
#define int int64_t
const int M1 = 998244353;
const int M2 = 1000000007;
int trie[200000][26];
int cnt[2000000][26];
int tot;
void insert(string s) {
reverse(s.begin(), s.end());
int n = s.size();
int a[26] = {};
for (int i = 0; i < n; i++)
a[s[i] - 'a']++;
for (int j = 0; j < 26; j++)
cnt[0][j] += (a[j] > 0);
int u = 0;
for (int i = 0; i < n; i++) {
a[s[i] - 'a']--;
if (!trie[u][s[i] - 'a'])
trie[u][s[i] - 'a'] = tot++;
u = trie[u][s[i] - 'a'];
for (int j = 0; j < 26; j++)
cnt[u][j] += (a[j] > 0);
}
}
int get(string s) {
reverse(s.begin(), s.end());
int n = s.size();
int u = 0;
for (int i = 0; i < n - 1; i++) {
if (!trie[u][s[i] - 'a'])
return 1;
u = trie[u][s[i] - 'a'];
}
return cnt[u][s[n - 1] - 'a'];
}
void solve() {
int n;
cin >> n;
tot = 1;
string s[n];
int ans = 0;
for (int i = 0; i < n; i++) {
cin >> s[i];
insert(s[i]);
}
for (int i = 0; i < n; i++)
ans += get(s[i]) - 1;
cout << ans << "\n";
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
} | /*
In the name of ALLAH
Author : Raashid Anwar
*/
#include <bits/stdc++.h>
using namespace std;
#define int int64_t
const int M1 = 998244353;
const int M2 = 1000000007;
int trie[2000000][26];
int cnt[2000000][26];
int tot;
void insert(string s) {
reverse(s.begin(), s.end());
int n = s.size();
int a[26] = {};
for (int i = 0; i < n; i++)
a[s[i] - 'a']++;
for (int j = 0; j < 26; j++)
cnt[0][j] += (a[j] > 0);
int u = 0;
for (int i = 0; i < n; i++) {
a[s[i] - 'a']--;
if (!trie[u][s[i] - 'a'])
trie[u][s[i] - 'a'] = tot++;
u = trie[u][s[i] - 'a'];
for (int j = 0; j < 26; j++)
cnt[u][j] += (a[j] > 0);
}
}
int get(string s) {
reverse(s.begin(), s.end());
int n = s.size();
int u = 0;
for (int i = 0; i < n - 1; i++) {
if (!trie[u][s[i] - 'a'])
return 1;
u = trie[u][s[i] - 'a'];
}
return cnt[u][s[n - 1] - 'a'];
}
void solve() {
int n;
cin >> n;
tot = 1;
string s[n];
int ans = 0;
for (int i = 0; i < n; i++) {
cin >> s[i];
insert(s[i]);
}
for (int i = 0; i < n; i++)
ans += get(s[i]) - 1;
cout << ans << "\n";
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
} | replace | 12 | 13 | 12 | 13 | -11 | |
p02589 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<ll>;
using vvi = vector<vi>;
#define all(a) a.begin(), a.end()
#define pb push_back
void solve() {
int n;
cin >> n;
vector<pair<string, char>> vec(n);
for (int i = 0; i < n; i++) {
string s;
cin >> s;
reverse(s.begin() + 1, s.end());
vec[i].second = s[0];
vec[i].first = s.substr(1, s.size() - 1);
}
sort(all(vec));
ll M = 999999937;
ll k = 45;
vvi hash(n);
for (int i = 0; i < n; i++) {
hash[i] = vi(vec[i].first.size() + 1);
for (int j = 1; j < hash[i].size(); j++) {
hash[i][j] = hash[i][j - 1] * k + vec[i].first[j - 1];
hash[i][j] %= M;
}
}
vvi cnt(n, vi(26));
for (int i = 0; i < n; i++) {
for (auto c : vec[i].first) {
cnt[i][c - 'a']++;
}
}
ll res = 0;
vi st;
vector<pair<char, int>> query;
for (int i = 0; i < n; i++) {
while (!st.empty()) {
int v = st.back();
if (hash[v].back() == hash[i][vec[v].first.size()]) {
break;
} else {
st.pop_back();
query.pop_back();
}
}
for (auto q : query) {
if (cnt[i][q.first - 'a'] + (q.first == vec[i].second) >= q.second)
res++;
}
st.push_back(i);
query.push_back({vec[i].second, cnt[i][vec[i].second - 'a'] + 1});
}
cout << res << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<ll>;
using vvi = vector<vi>;
#define all(a) a.begin(), a.end()
#define pb push_back
void solve() {
int n;
cin >> n;
vector<pair<string, char>> vec(n);
for (int i = 0; i < n; i++) {
string s;
cin >> s;
reverse(s.begin() + 1, s.end());
vec[i].second = s[0];
vec[i].first = s.substr(1, s.size() - 1);
}
sort(all(vec));
ll M = 999999937;
ll k = 45;
vvi hash(n);
for (int i = 0; i < n; i++) {
hash[i] = vi(vec[i].first.size() + 1);
for (int j = 1; j < hash[i].size(); j++) {
hash[i][j] = hash[i][j - 1] * k + vec[i].first[j - 1];
hash[i][j] %= M;
}
}
vvi cnt(n, vi(26));
for (int i = 0; i < n; i++) {
for (auto c : vec[i].first) {
cnt[i][c - 'a']++;
}
}
ll res = 0;
vi st;
vector<pair<char, int>> query;
for (int i = 0; i < n; i++) {
while (!st.empty()) {
int v = st.back();
if (!(vec[i].first.size() < vec[v].first.size()) &&
hash[v].back() == hash[i][vec[v].first.size()]) {
break;
} else {
st.pop_back();
query.pop_back();
}
}
for (auto q : query) {
if (cnt[i][q.first - 'a'] + (q.first == vec[i].second) >= q.second)
res++;
}
st.push_back(i);
query.push_back({vec[i].second, cnt[i][vec[i].second - 'a'] + 1});
}
cout << res << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
} | replace | 49 | 50 | 49 | 51 | 0 | |
p02589 | C++ | Runtime Error | #include <bits/stdc++.h>
#define fi first
#define se second
const int N = 100100;
const int Q = 1000100;
using namespace std;
int n;
string s[N];
int G;
int t[Q][26];
int f[Q];
bool good[Q][26];
int main() {
ios_base::sync_with_stdio(0);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
cin >> n;
vector<int> ord;
for (int i = 1; i <= n; i++) {
cin >> s[i];
reverse(s[i].begin(), s[i].end());
ord.push_back(i);
}
sort(ord.begin(), ord.end(),
[&](int x, int y) { return s[x].size() < s[y].size(); });
long long res = 0;
for (int i : ord) {
int cur = 0;
for (int h = 0; h < 26; h++) {
good[s[i].size()][h] = false;
}
for (int j = s[i].size() - 1; j >= 0; j--) {
for (int h = 0; h < 26; h++) {
good[j][h] = good[j + 1][h];
}
good[j][s[i][j] - 'a'] = true;
}
for (int j = 0, g; j < s[i].size(); j++) {
for (int h = 0; h < 26; h++) {
if (good[j][h] && t[cur][h]) {
res += f[t[cur][h]];
}
}
g = s[i][j] - 'a';
if (!t[cur][g]) {
t[cur][g] = ++G;
}
cur = t[cur][g];
}
f[cur] += 1;
}
cout << res << "\n";
}
| #include <bits/stdc++.h>
#define fi first
#define se second
const int N = 200100;
const int Q = 1000100;
using namespace std;
int n;
string s[N];
int G;
int t[Q][26];
int f[Q];
bool good[Q][26];
int main() {
ios_base::sync_with_stdio(0);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
cin >> n;
vector<int> ord;
for (int i = 1; i <= n; i++) {
cin >> s[i];
reverse(s[i].begin(), s[i].end());
ord.push_back(i);
}
sort(ord.begin(), ord.end(),
[&](int x, int y) { return s[x].size() < s[y].size(); });
long long res = 0;
for (int i : ord) {
int cur = 0;
for (int h = 0; h < 26; h++) {
good[s[i].size()][h] = false;
}
for (int j = s[i].size() - 1; j >= 0; j--) {
for (int h = 0; h < 26; h++) {
good[j][h] = good[j + 1][h];
}
good[j][s[i][j] - 'a'] = true;
}
for (int j = 0, g; j < s[i].size(); j++) {
for (int h = 0; h < 26; h++) {
if (good[j][h] && t[cur][h]) {
res += f[t[cur][h]];
}
}
g = s[i][j] - 'a';
if (!t[cur][g]) {
t[cur][g] = ++G;
}
cur = t[cur][g];
}
f[cur] += 1;
}
cout << res << "\n";
}
| replace | 5 | 6 | 5 | 6 | -11 | |
p02589 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
int n;
#define Maxn 200010
#define V 1000010
char ch[Maxn];
int len;
char *str[Maxn];
int L[Maxn];
int *sum[26][Maxn];
struct Node {
int son[26], sum;
} tree[V];
int root = 0, cnt = 0;
void Insert(int &k, int at) {
if (!k)
k = ++cnt;
if (at == len + 1) {
tree[k].sum++;
return;
}
Insert(tree[k].son[ch[at] - 'a'], at + 1);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%s", ch + 1);
len = strlen(ch + 1);
L[i] = len;
str[i] = new char[L[i] + 1];
for (int j = 1; j <= L[i]; ++j)
str[i][j] = ch[j];
for (int j = 0; j < 26; ++j) {
sum[j][i] = new int[L[i] + 1];
sum[j][i][0] = 0;
for (int k = 1; k <= L[i]; ++k) {
sum[j][i][k] = sum[j][i][k - 1];
if ((ch[k] - 'a') == j)
sum[j][i][k]++;
}
}
reverse(ch + 1, ch + len + 1);
Insert(root, 1);
}
ll Ans = -n;
for (int i = 1; i <= n; ++i) {
int k = root;
for (int j = L[i]; j >= 1; --j) {
for (int z = 0; z < 26; ++z)
if (sum[z][i][j])
Ans += tree[tree[k].son[z]].sum;
k = tree[k].son[str[i][j] - 'a'];
}
}
printf("%lld\n", Ans);
} | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
int n;
#define Maxn 200010
#define V 1000010
char ch[V];
int len;
char *str[Maxn];
int L[Maxn];
int *sum[26][Maxn];
struct Node {
int son[26], sum;
} tree[V];
int root = 0, cnt = 0;
void Insert(int &k, int at) {
if (!k)
k = ++cnt;
if (at == len + 1) {
tree[k].sum++;
return;
}
Insert(tree[k].son[ch[at] - 'a'], at + 1);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%s", ch + 1);
len = strlen(ch + 1);
L[i] = len;
str[i] = new char[L[i] + 1];
for (int j = 1; j <= L[i]; ++j)
str[i][j] = ch[j];
for (int j = 0; j < 26; ++j) {
sum[j][i] = new int[L[i] + 1];
sum[j][i][0] = 0;
for (int k = 1; k <= L[i]; ++k) {
sum[j][i][k] = sum[j][i][k - 1];
if ((ch[k] - 'a') == j)
sum[j][i][k]++;
}
}
reverse(ch + 1, ch + len + 1);
Insert(root, 1);
}
ll Ans = -n;
for (int i = 1; i <= n; ++i) {
int k = root;
for (int j = L[i]; j >= 1; --j) {
for (int z = 0; z < 26; ++z)
if (sum[z][i][j])
Ans += tree[tree[k].son[z]].sum;
k = tree[k].son[str[i][j] - 'a'];
}
}
printf("%lld\n", Ans);
} | replace | 9 | 10 | 9 | 10 | -11 | |
p02589 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int N = 2e5 + 5;
int nxt[N][26], cnt[N][26], cur = 0;
void insert(string s) {
vector<int> c(26, 0);
int idx = 0;
for (auto i : s)
c[i - 'a']++;
for (int j = 0; j < 26; j++)
cnt[idx][j] += (c[j] > 0);
for (auto i : s) {
c[i - 'a']--;
if (!nxt[idx][i - 'a']) {
nxt[idx][i - 'a'] = ++cur;
}
idx = nxt[idx][i - 'a'];
for (int j = 0; j < 26; j++)
cnt[idx][j] += (c[j] > 0);
}
}
int solve(string s) {
int n = s.length(), idx = 0;
for (int i = 0; i < n - 1; i++) {
idx = nxt[idx][s[i] - 'a'];
}
return cnt[idx][s[n - 1] - 'a'] - 1;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<string> s(n);
for (int i = 0; i < n; i++) {
cin >> s[i];
reverse(s[i].begin(), s[i].end());
insert(s[i]);
}
ll ans = 0;
for (int i = 0; i < n; i++) {
ans += solve(s[i]);
}
cout << ans << '\n';
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int N = 1e6 + 5;
int nxt[N][26], cnt[N][26], cur = 0;
void insert(string s) {
vector<int> c(26, 0);
int idx = 0;
for (auto i : s)
c[i - 'a']++;
for (int j = 0; j < 26; j++)
cnt[idx][j] += (c[j] > 0);
for (auto i : s) {
c[i - 'a']--;
if (!nxt[idx][i - 'a']) {
nxt[idx][i - 'a'] = ++cur;
}
idx = nxt[idx][i - 'a'];
for (int j = 0; j < 26; j++)
cnt[idx][j] += (c[j] > 0);
}
}
int solve(string s) {
int n = s.length(), idx = 0;
for (int i = 0; i < n - 1; i++) {
idx = nxt[idx][s[i] - 'a'];
}
return cnt[idx][s[n - 1] - 'a'] - 1;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<string> s(n);
for (int i = 0; i < n; i++) {
cin >> s[i];
reverse(s[i].begin(), s[i].end());
insert(s[i]);
}
ll ans = 0;
for (int i = 0; i < n; i++) {
ans += solve(s[i]);
}
cout << ans << '\n';
}
| replace | 6 | 7 | 6 | 7 | 0 | |
p02589 | C++ | Time Limit Exceeded | // By TheOneYouWant
#pragma GCC optimize("-O2")
#include <bits/stdc++.h>
using namespace std;
#define fastio \
ios_base::sync_with_stdio(0); \
cin.tie(0)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(), x.end()
// #define int unsigned long long int
#define memreset(a) memset(a, 0, sizeof(a))
#define testcase(t) \
int t; \
cin >> t; \
while (t--)
#define forstl(i, v) for (auto &i : v)
#define forn(i, e) for (int i = 0; i < e; ++i)
#define forsn(i, s, e) for (int i = s; i < e; ++i)
#define rforn(i, s) for (int i = s; i >= 0; --i)
#define rforsn(i, s, e) for (int i = s; i >= e; --i)
#define bitcount(a) __builtin_popcount(a) // set bits (add ll)
#define ln '\n'
#define getcurrtime() \
cerr << "Time = " << ((double)clock() / CLOCKS_PER_SEC) << endl
#define dbgarr(v, s, e) \
cerr << #v << " = "; \
forsn(i, s, e) cerr << v[i] << ", "; \
cerr << endl
#define inputfile freopen("input.txt", "r", stdin)
#define outputfile freopen("output.txt", "w", stdout)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> p64;
typedef pair<int, int> p32;
typedef pair<int, p32> p96;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<v32> vv32;
typedef vector<v64> vv64;
typedef vector<p32> vp32;
typedef vector<p64> vp64;
typedef vector<vp32> vvp32;
typedef map<int, int> m32;
const int mul1 = 1003, mul2 = 5, mul3 = 8;
const int MOD3 = 179424673, MOD2 = 982451653, MOD1 = 1190494759;
const ld EPS = 1e-9;
int read() {
int xx = 0, ff = 1;
char ch = getchar();
while (ch > '9' || ch < '0') {
if (ch == '-')
ff = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
xx = 10 * xx + ch - '0';
ch = getchar();
}
return xx * ff;
}
ull hasher(ull a, ull b) {
return 14831553208299411291ull * (a ^ 16184882973064831860ull) + b;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n;
const int LIM = 2e5 + 5;
string s[LIM];
map<ull, int> m;
int main() {
fastio;
cin >> n;
ll ans = -n;
forn(i, n) {
cin >> s[i];
reverse(s[i].begin(), s[i].end());
ull h1 = 0;
forn(j, s[i].length()) { h1 = hasher(h1, s[i][j]); }
++m[h1];
}
forn(i, n) {
int num[26] = {0};
forn(j, s[i].length()) { ++num[s[i][j] - 'a']; }
ull h1 = 0;
forn(j, s[i].length()) {
forn(k, 26) {
if (num[k]) {
ull n1 = hasher(h1, k + 'a');
ans += m[n1];
}
}
h1 = hasher(h1, s[i][j]);
--num[s[i][j] - 'a'];
}
}
cout << ans << ln;
return 0;
}
| // By TheOneYouWant
#pragma GCC optimize("-O2")
#include <bits/stdc++.h>
using namespace std;
#define fastio \
ios_base::sync_with_stdio(0); \
cin.tie(0)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(), x.end()
// #define int unsigned long long int
#define memreset(a) memset(a, 0, sizeof(a))
#define testcase(t) \
int t; \
cin >> t; \
while (t--)
#define forstl(i, v) for (auto &i : v)
#define forn(i, e) for (int i = 0; i < e; ++i)
#define forsn(i, s, e) for (int i = s; i < e; ++i)
#define rforn(i, s) for (int i = s; i >= 0; --i)
#define rforsn(i, s, e) for (int i = s; i >= e; --i)
#define bitcount(a) __builtin_popcount(a) // set bits (add ll)
#define ln '\n'
#define getcurrtime() \
cerr << "Time = " << ((double)clock() / CLOCKS_PER_SEC) << endl
#define dbgarr(v, s, e) \
cerr << #v << " = "; \
forsn(i, s, e) cerr << v[i] << ", "; \
cerr << endl
#define inputfile freopen("input.txt", "r", stdin)
#define outputfile freopen("output.txt", "w", stdout)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> p64;
typedef pair<int, int> p32;
typedef pair<int, p32> p96;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<v32> vv32;
typedef vector<v64> vv64;
typedef vector<p32> vp32;
typedef vector<p64> vp64;
typedef vector<vp32> vvp32;
typedef map<int, int> m32;
const int mul1 = 1003, mul2 = 5, mul3 = 8;
const int MOD3 = 179424673, MOD2 = 982451653, MOD1 = 1190494759;
const ld EPS = 1e-9;
int read() {
int xx = 0, ff = 1;
char ch = getchar();
while (ch > '9' || ch < '0') {
if (ch == '-')
ff = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
xx = 10 * xx + ch - '0';
ch = getchar();
}
return xx * ff;
}
ull hasher(ull a, ull b) {
return 14831553208299411291ull * (a ^ 16184882973064831860ull) + b;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n;
const int LIM = 2e5 + 5;
string s[LIM];
map<ull, int> m;
int main() {
fastio;
cin >> n;
ll ans = -n;
forn(i, n) {
cin >> s[i];
reverse(s[i].begin(), s[i].end());
ull h1 = 0;
forn(j, s[i].length()) { h1 = hasher(h1, s[i][j]); }
++m[h1];
}
forn(i, n) {
int num[26] = {0};
forn(j, s[i].length()) { ++num[s[i][j] - 'a']; }
ull h1 = 0;
forn(j, s[i].length()) {
forn(k, 26) {
if (num[k]) {
ull n1 = hasher(h1, k + 'a');
auto it = m.find(n1);
if (it != m.end())
ans += (*it).second;
}
}
h1 = hasher(h1, s[i][j]);
--num[s[i][j] - 'a'];
}
}
cout << ans << ln;
return 0;
}
| replace | 103 | 104 | 103 | 107 | TLE | |
p02589 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T> void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x)
cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
__print(t);
if (sizeof...(v))
cerr << ", ";
_print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...) \
cerr << "[" << #x << "] = ["; \
_print(x)
#else
#define debug(x...)
#endif
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a) memset(a, 0, sizeof(a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
void pre() {}
void solve() {}
vector<pair<int, string>> v;
typedef unsigned long long H;
static const H C = 123891739; // arbitrary
// Arithmetic mod 2^64-1. 5x slower than mod 2^64 and more
// code, but works on evil test data (e.g. Thue-Morse).
// "typedef H K;" instead if you think test data is random.
struct K {
typedef __uint128_t H2;
H x;
K(H x = 0) : x(x) {}
K operator+(K o) { return x + o.x + H(((H2)x + o.x) >> 64); }
K operator*(K o) { return K(x * o.x) + H(((H2)x * o.x) >> 64); }
H operator-(K o) {
K a = *this + ~o.x;
return a.x + !~a.x;
}
};
vector<K> ha, pw;
int cnt[26];
vector<K> hsh(string &str) {
fill(cnt);
pw.resize(sz(str) + 1);
ha.resize(sz(pw));
pw[0] = 1;
rep(i, sz(str)) {
cnt[str[i] - 'a']++;
ha[i + 1] = ha[i] * C + str[i], pw[i + 1] = pw[i] * C;
}
return ha;
}
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cin.exceptions(cin.failbit);
pre();
int n;
cin >> n;
rep(i, n) {
string s;
cin >> s;
v.pb(mp(sz(s), s));
}
sort(all(v));
map<pair<H, int>, int> m;
ll ans = 0;
trav(i, v) {
reverse(all(i.snd));
vector<K> h = hsh(i.snd);
int ix = 0;
trav(j, h) {
rep(k, 26) if (cnt[k]) ans += m[mp(j.x, k)];
if (ix < sz(i.snd))
cnt[i.snd[ix] - 'a']--;
ix++;
}
m[mp(h[sz(i.snd) - 1].x, i.snd.back() - 'a')]++;
}
cout << ans;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T> void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x)
cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
__print(t);
if (sizeof...(v))
cerr << ", ";
_print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...) \
cerr << "[" << #x << "] = ["; \
_print(x)
#else
#define debug(x...)
#endif
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a) memset(a, 0, sizeof(a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
void pre() {}
void solve() {}
vector<pair<int, string>> v;
typedef unsigned long long H;
static const H C = 123891739; // arbitrary
// Arithmetic mod 2^64-1. 5x slower than mod 2^64 and more
// code, but works on evil test data (e.g. Thue-Morse).
// "typedef H K;" instead if you think test data is random.
struct K {
typedef __uint128_t H2;
H x;
K(H x = 0) : x(x) {}
K operator+(K o) { return x + o.x + H(((H2)x + o.x) >> 64); }
K operator*(K o) { return K(x * o.x) + H(((H2)x * o.x) >> 64); }
H operator-(K o) {
K a = *this + ~o.x;
return a.x + !~a.x;
}
};
vector<K> ha, pw;
int cnt[26];
vector<K> hsh(string &str) {
fill(cnt);
pw.resize(sz(str) + 1);
ha.resize(sz(pw));
pw[0] = 1;
rep(i, sz(str)) {
cnt[str[i] - 'a']++;
ha[i + 1] = ha[i] * C + str[i], pw[i + 1] = pw[i] * C;
}
return ha;
}
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cin.exceptions(cin.failbit);
pre();
int n;
cin >> n;
rep(i, n) {
string s;
cin >> s;
v.pb(mp(sz(s), s));
}
sort(all(v));
map<pair<H, int>, int> m;
ll ans = 0;
trav(i, v) {
reverse(all(i.snd));
vector<K> h = hsh(i.snd);
int ix = 0;
trav(j, h) {
auto it = m.lower_bound(mp(j.x, -1));
rep(k, 26) if (cnt[k]) {
while (it != m.end() && it->fst.snd < k)
it++;
if ((*it).fst == mp(j.x, k))
ans += it->snd;
}
if (ix < sz(i.snd))
cnt[i.snd[ix] - 'a']--;
ix++;
}
m[mp(h[sz(i.snd) - 1].x, i.snd.back() - 'a')]++;
}
cout << ans;
return 0;
}
| replace | 116 | 117 | 116 | 123 | TLE | |
p02589 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int max_n = 202222, inf = 1000111222;
const int max_v = 1000111;
const int max_c = 27;
char buf[max_n];
string read() {
scanf("%s", buf);
return buf;
}
int n;
int first, nxt[max_v][max_c], cnt[max_v][max_c], tot[max_c];
string s[max_n];
long long ans;
void add(const string &s) {
memset(tot, 0, sizeof(tot));
for (char c : s) {
++tot[c - 'a'];
}
int v = 0;
for (char c : s) {
for (int to = 0; to < max_c; ++to) {
if (tot[to]) {
++cnt[v][to];
}
}
--tot[c - 'a'];
if (!nxt[v][c - 'a']) {
nxt[v][c - 'a'] = ++first;
}
v = nxt[v][c - 'a'];
}
}
int main() {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
s[i] = read();
reverse(s[i].begin(), s[i].end());
add(s[i]);
}
for (int i = 0; i < n; ++i) {
int v = 0;
for (int j = 0; j + 1 < s[i].length(); ++j) {
v = nxt[v][s[i][j] - 'a'];
}
ans += cnt[v][s[i].back() - 'a'] - 1;
}
cout << ans << "\n";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const int max_n = 202222, inf = 1000111222;
const int max_v = 1000111;
const int max_c = 27;
char buf[max_v];
string read() {
scanf("%s", buf);
return buf;
}
int n;
int first, nxt[max_v][max_c], cnt[max_v][max_c], tot[max_c];
string s[max_n];
long long ans;
void add(const string &s) {
memset(tot, 0, sizeof(tot));
for (char c : s) {
++tot[c - 'a'];
}
int v = 0;
for (char c : s) {
for (int to = 0; to < max_c; ++to) {
if (tot[to]) {
++cnt[v][to];
}
}
--tot[c - 'a'];
if (!nxt[v][c - 'a']) {
nxt[v][c - 'a'] = ++first;
}
v = nxt[v][c - 'a'];
}
}
int main() {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
s[i] = read();
reverse(s[i].begin(), s[i].end());
add(s[i]);
}
for (int i = 0; i < n; ++i) {
int v = 0;
for (int j = 0; j + 1 < s[i].length(); ++j) {
v = nxt[v][s[i][j] - 'a'];
}
ans += cnt[v][s[i].back() - 'a'] - 1;
}
cout << ans << "\n";
return 0;
}
| replace | 8 | 9 | 8 | 9 | -11 | |
p02589 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
long long n, cur, pas, b[N], endz[N * 10][30], aft[N * 10][30], k, ans;
string s[N];
bool ss(string s1, string s2) { return s1.size() < s2.size(); }
int get() {
int u = 0, pas = 0;
b[0] = 1ll << (s[k][0] - 'a');
for (int i = 1; i < s[k].size(); i++) {
b[i] = b[i - 1] | (1ll << (s[k][i] - 'a'));
}
// cout<<s[k].size()<<endl;
for (int i = s[k].size() - 1; i >= 0; i--) {
if (i == 0)
break;
for (int j = 0; j <= 25; j++) {
if (b[i] & (1 << j))
pas += endz[u][j];
}
if (!aft[u][s[k][i] - 'a'])
break;
u = aft[u][s[k][i] - 'a'];
}
return pas;
}
void add() {
int u = 0;
for (int i = s[k].size() - 1; i >= 1; i--) {
if (!aft[u][s[k][i] - 'a']) {
cur++;
aft[u][s[k][i] - 'a'] = cur;
}
u = aft[u][s[k][i] - 'a'];
}
endz[u][s[k][0] - 'a']++;
}
int main() {
cin >> n;
for (k = 1; k <= n; k++) {
cin >> s[k];
}
sort(s + 1, s + n + 1, ss);
long long ans = 0;
for (k = 1; k <= n; k++) {
ans += (long long)get();
add();
}
cout << ans;
}
| #include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
long long n, cur, pas, b[5 * N], endz[N * 10][30], aft[N * 10][30], k, ans;
string s[N];
bool ss(string s1, string s2) { return s1.size() < s2.size(); }
int get() {
int u = 0, pas = 0;
b[0] = 1ll << (s[k][0] - 'a');
for (int i = 1; i < s[k].size(); i++) {
b[i] = b[i - 1] | (1ll << (s[k][i] - 'a'));
}
// cout<<s[k].size()<<endl;
for (int i = s[k].size() - 1; i >= 0; i--) {
if (i == 0)
break;
for (int j = 0; j <= 25; j++) {
if (b[i] & (1 << j))
pas += endz[u][j];
}
if (!aft[u][s[k][i] - 'a'])
break;
u = aft[u][s[k][i] - 'a'];
}
return pas;
}
void add() {
int u = 0;
for (int i = s[k].size() - 1; i >= 1; i--) {
if (!aft[u][s[k][i] - 'a']) {
cur++;
aft[u][s[k][i] - 'a'] = cur;
}
u = aft[u][s[k][i] - 'a'];
}
endz[u][s[k][0] - 'a']++;
}
int main() {
cin >> n;
for (k = 1; k <= n; k++) {
cin >> s[k];
}
sort(s + 1, s + n + 1, ss);
long long ans = 0;
for (k = 1; k <= n; k++) {
ans += (long long)get();
add();
}
cout << ans;
}
| replace | 3 | 4 | 3 | 4 | -11 | |
p02589 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
using Pld = pair<ld, ld>;
using Vec = vector<ll>;
using VecP = vector<P>;
using VecB = vector<bool>;
using VecC = vector<char>;
using VecD = vector<ld>;
using VecS = vector<string>;
using Graph = vector<VecP>;
template <typename T> using Vec1 = vector<T>;
template <typename T> using Vec2 = vector<Vec1<T>>;
#define REP(i, m, n) for (ll i = (m); i < (n); ++i)
#define REPN(i, m, n) for (ll i = (m); i <= (n); ++i)
#define REPR(i, m, n) for (ll i = (m)-1; i >= (n); --i)
#define REPNR(i, m, n) for (ll i = (m); i >= (n); --i)
#define rep(i, n) REP(i, 0, n)
#define repn(i, n) REPN(i, 1, n)
#define repr(i, n) REPR(i, n, 0)
#define repnr(i, n) REPNR(i, n, 1)
#define all(s) (s).begin(), (s).end()
#define pb push_back
#define fs first
#define sc second
template <typename T> bool chmax(T &a, const T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T> bool chmin(T &a, const T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T> ll pow2(const T n) { return (1LL << n); }
template <typename T> void cosp(const T n) { cout << n << ' '; }
void co(void) { cout << '\n'; }
template <typename T> void co(const T n) { cout << n << '\n'; }
template <typename T1, typename T2> void co(pair<T1, T2> p) {
cout << p.fs << ' ' << p.sc << '\n';
}
template <typename T> void co(const Vec1<T> &v) {
for (T i : v)
cosp(i);
co();
}
template <typename T> void co(initializer_list<T> v) {
for (T i : v)
cosp(i);
co();
}
template <typename T> void ce(const T n) { cerr << n << endl; }
void sonic() {
ios::sync_with_stdio(false);
cin.tie(0);
}
void setp(const ll n) { cout << fixed << setprecision(n); }
constexpr int INF = 1e9 + 1;
constexpr ll LINF = 1e18 + 1;
constexpr ll MOD = 1e9 + 7;
// constexpr ll MOD = 998244353;
constexpr ld EPS = 1e-11;
const double PI = acos(-1);
struct Str {
string s;
char c;
void input() {
cin >> s;
reverse(all(s));
c = s.back();
s.pop_back();
}
size_t size() { return s.size(); }
};
bool asc(Str &a, Str &b) {
if (a.s == b.s)
return a.c < b.c;
return a.s < b.s;
}
int main(void) {
ll n;
cin >> n;
vector<Str> s(n);
rep(i, n) s[i].input();
sort(all(s), asc);
// rep(i, n) cout << s[i].s << "+" << s[i].c << endl;
ll ans = 0;
rep(i, n) {
REP(j, i + 1, n) {
if (s[i].size() >= s[j].size())
continue;
ll l = s[i].size();
if (s[i].s == s[j].s.substr(0, l)) {
if (s[j].s.find(s[i].c, l) != string::npos || s[i].c == s[j].c)
ans++;
} else
break;
}
}
co(ans);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
using Pld = pair<ld, ld>;
using Vec = vector<ll>;
using VecP = vector<P>;
using VecB = vector<bool>;
using VecC = vector<char>;
using VecD = vector<ld>;
using VecS = vector<string>;
using Graph = vector<VecP>;
template <typename T> using Vec1 = vector<T>;
template <typename T> using Vec2 = vector<Vec1<T>>;
#define REP(i, m, n) for (ll i = (m); i < (n); ++i)
#define REPN(i, m, n) for (ll i = (m); i <= (n); ++i)
#define REPR(i, m, n) for (ll i = (m)-1; i >= (n); --i)
#define REPNR(i, m, n) for (ll i = (m); i >= (n); --i)
#define rep(i, n) REP(i, 0, n)
#define repn(i, n) REPN(i, 1, n)
#define repr(i, n) REPR(i, n, 0)
#define repnr(i, n) REPNR(i, n, 1)
#define all(s) (s).begin(), (s).end()
#define pb push_back
#define fs first
#define sc second
template <typename T> bool chmax(T &a, const T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T> bool chmin(T &a, const T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T> ll pow2(const T n) { return (1LL << n); }
template <typename T> void cosp(const T n) { cout << n << ' '; }
void co(void) { cout << '\n'; }
template <typename T> void co(const T n) { cout << n << '\n'; }
template <typename T1, typename T2> void co(pair<T1, T2> p) {
cout << p.fs << ' ' << p.sc << '\n';
}
template <typename T> void co(const Vec1<T> &v) {
for (T i : v)
cosp(i);
co();
}
template <typename T> void co(initializer_list<T> v) {
for (T i : v)
cosp(i);
co();
}
template <typename T> void ce(const T n) { cerr << n << endl; }
void sonic() {
ios::sync_with_stdio(false);
cin.tie(0);
}
void setp(const ll n) { cout << fixed << setprecision(n); }
constexpr int INF = 1e9 + 1;
constexpr ll LINF = 1e18 + 1;
constexpr ll MOD = 1e9 + 7;
// constexpr ll MOD = 998244353;
constexpr ld EPS = 1e-11;
const double PI = acos(-1);
struct Str {
string s;
char c;
void input() {
cin >> s;
reverse(all(s));
c = s.back();
s.pop_back();
}
size_t size() { return s.size(); }
};
bool asc(Str &a, Str &b) {
if (a.s == b.s)
return a.c < b.c;
return a.s < b.s;
}
int main(void) {
ll n;
cin >> n;
vector<Str> s(n);
rep(i, n) s[i].input();
sort(all(s), asc);
// rep(i, n) cout << s[i].s << "+" << s[i].c << endl;
ll ans = 0;
rep(i, n) {
REP(j, i + 1, n) {
if (s[i].size() > s[j].size())
break;
if (s[i].size() == s[j].size())
continue;
ll l = s[i].size();
if (s[i].s == s[j].s.substr(0, l)) {
if (s[j].s.find(s[i].c, l) != string::npos || s[i].c == s[j].c)
ans++;
} else
break;
}
}
co(ans);
return 0;
}
| replace | 102 | 103 | 102 | 105 | TLE | |
p02589 | C++ | Time Limit Exceeded | // warm heart, wagging tail,and a smile just for you!
// ███████████
// ███╬╬╬╬╬╬╬╬╬╬███
// ███╬╬╬╬╬████╬╬╬╬╬╬███
// ███████████
// ██╬╬╬╬╬████╬╬████╬╬╬╬╬██
// █████████╬╬╬╬╬████████████╬╬╬╬╬██╬╬╬╬╬╬███╬╬╬╬╬██
// ████████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████████╬╬╬╬╬╬██╬╬╬╬╬╬╬██
// ████╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████████╬╬╬╬╬╬╬╬╬╬╬██
// ███╬╬╬█╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬███╬╬╬╬╬╬╬█████
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬████████╬╬╬╬╬██
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬╬╬╬╬╬███
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬██
// ████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████╬╬╬╬╬████
// █████████████╬╬╬╬╬╬╬╬██╬╬╬╬╬████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬██████
// ████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬██████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██████╬╬╬╬╬╬╬███████████╬╬╬╬╬╬╬╬██╬╬╬██╬╬╬██
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████╬╬╬╬╬╬╬╬╬╬╬█╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬██
// ██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬▓▓▓▓▓▓╬╬╬████╬╬████╬╬╬╬╬╬╬▓▓▓▓▓▓▓▓██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬███
// ██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██████▓▓▓▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓▓▓▓██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬█████
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████████
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██
// ██████████████
// ████╬╬╬╬╬╬███████████████████████████╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████
// ███████ █████
// ███████████████████
//
#include "bits/stdc++.h"
#include <unordered_map>
using namespace std;
#define INF (1 << 30)
#define LINF (1LL << 60)
#define fs first
#define sc second
#define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FOR2(i, a, b) for (int i = (a); i <= (b); ++i)
#define RFOR(i, a, b) for (int i = (b - 1); i >= (a); --i)
#define RFOR2(i, a, b) for (int i = (b); i >= (a); --i)
#define REP(i, n) FOR(i, 0, (n))
#define REP2(i, n) FOR2(i, 0, (n))
#define RREP(i, n) RFOR(i, 0, (n))
#define RREP2(i, n) RFOR2(i, 0, (n))
#define ITR(itr, mp) for (auto itr = (mp).begin(); itr != (mp).end(); ++itr)
#define RITR(itr, mp) for (auto itr = (mp).rbegin(); itr != (mp).rend(); ++itr)
#define range(i, a, b) ((a) <= (i) && (i) < (b))
#define debug(x) cout << #x << " = " << (x) << endl
#define SP << " " <<
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
if (a > b) {
a = b;
return true;
} else
return false;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
if (a < b) {
a = b;
return true;
} else
return false;
}
#define MSB(x) (63 - __builtin_clzll(x))
#define pcnt(x) (__builtin_popcountll(x))
#define parity(i, j) (i & (1LL << j))
typedef pair<string, int> P;
typedef tuple<int, int, int> T;
typedef vector<int> vec;
typedef vector<vector<int>> mat;
void solve() {
int N;
cin >> N;
vector<string> S(N);
REP(i, N) cin >> S[i];
auto cmp = [](string s, string t) { return s.size() < t.size(); };
sort(S.begin(), S.end(), cmp);
map<string, int> mp;
vec f(1010101, 0);
int ans = 0;
REP(i, N) {
// debug(S[i]);
vec cnt(26, 0);
REP(j, S[i].size()) cnt[S[i][j] - 'a']++;
string tmp;
RREP(j, S[i].size()) {
if (f[tmp.size() + 1]) {
REP(k, 26) {
if (!cnt[k])
continue;
tmp += (char)(k + 'a');
if (mp.count(tmp))
ans += mp[tmp];
tmp.pop_back();
}
}
cnt[S[i][j] - 'a']--;
tmp += S[i][j];
}
// pre[tmp.size()]++;
mp[tmp]++;
f[tmp.size()]++;
}
cout << ans << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T = 1;
// cin >> T;
while (T--)
solve();
return 0;
} | // warm heart, wagging tail,and a smile just for you!
// ███████████
// ███╬╬╬╬╬╬╬╬╬╬███
// ███╬╬╬╬╬████╬╬╬╬╬╬███
// ███████████
// ██╬╬╬╬╬████╬╬████╬╬╬╬╬██
// █████████╬╬╬╬╬████████████╬╬╬╬╬██╬╬╬╬╬╬███╬╬╬╬╬██
// ████████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████████╬╬╬╬╬╬██╬╬╬╬╬╬╬██
// ████╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████████╬╬╬╬╬╬╬╬╬╬╬██
// ███╬╬╬█╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬███╬╬╬╬╬╬╬█████
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬████████╬╬╬╬╬██
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬╬╬╬╬╬███
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬██
// ████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████╬╬╬╬╬████
// █████████████╬╬╬╬╬╬╬╬██╬╬╬╬╬████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬██████
// ████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬██████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██████╬╬╬╬╬╬╬███████████╬╬╬╬╬╬╬╬██╬╬╬██╬╬╬██
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████╬╬╬╬╬╬╬╬╬╬╬█╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬██
// ██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬▓▓▓▓▓▓╬╬╬████╬╬████╬╬╬╬╬╬╬▓▓▓▓▓▓▓▓██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬███
// ██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██████▓▓▓▓▓▓▓╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬▓▓▓▓▓▓▓██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██╬╬╬╬█████
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████████
// ███╬╬╬╬╬╬╬╬╬╬╬╬╬█████╬╬╬╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬███╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬██
// ██████████████
// ████╬╬╬╬╬╬███████████████████████████╬╬╬╬╬██╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬╬████
// ███████ █████
// ███████████████████
//
#include "bits/stdc++.h"
#include <unordered_map>
using namespace std;
#define INF (1 << 30)
#define LINF (1LL << 60)
#define fs first
#define sc second
#define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FOR2(i, a, b) for (int i = (a); i <= (b); ++i)
#define RFOR(i, a, b) for (int i = (b - 1); i >= (a); --i)
#define RFOR2(i, a, b) for (int i = (b); i >= (a); --i)
#define REP(i, n) FOR(i, 0, (n))
#define REP2(i, n) FOR2(i, 0, (n))
#define RREP(i, n) RFOR(i, 0, (n))
#define RREP2(i, n) RFOR2(i, 0, (n))
#define ITR(itr, mp) for (auto itr = (mp).begin(); itr != (mp).end(); ++itr)
#define RITR(itr, mp) for (auto itr = (mp).rbegin(); itr != (mp).rend(); ++itr)
#define range(i, a, b) ((a) <= (i) && (i) < (b))
#define debug(x) cout << #x << " = " << (x) << endl
#define SP << " " <<
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
if (a > b) {
a = b;
return true;
} else
return false;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
if (a < b) {
a = b;
return true;
} else
return false;
}
#define MSB(x) (63 - __builtin_clzll(x))
#define pcnt(x) (__builtin_popcountll(x))
#define parity(i, j) (i & (1LL << j))
typedef pair<string, int> P;
typedef tuple<int, int, int> T;
typedef vector<int> vec;
typedef vector<vector<int>> mat;
void solve() {
int N;
cin >> N;
vector<string> S(N);
REP(i, N) cin >> S[i];
auto cmp = [](string s, string t) { return s.size() < t.size(); };
sort(S.begin(), S.end(), cmp);
unordered_map<string, int> mp;
vec f(1010101, 0);
int ans = 0;
REP(i, N) {
// debug(S[i]);
vec cnt(26, 0);
REP(j, S[i].size()) cnt[S[i][j] - 'a']++;
string tmp;
RREP(j, S[i].size()) {
if (f[tmp.size() + 1]) {
REP(k, 26) {
if (!cnt[k])
continue;
tmp += (char)(k + 'a');
if (mp.count(tmp))
ans += mp[tmp];
tmp.pop_back();
}
}
cnt[S[i][j] - 'a']--;
tmp += S[i][j];
}
// pre[tmp.size()]++;
mp[tmp]++;
f[tmp.size()]++;
}
cout << ans << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T = 1;
// cin >> T;
while (T--)
solve();
return 0;
} | replace | 79 | 80 | 79 | 80 | TLE | |
p02589 | C++ | Runtime Error | //~ while (clock()<=69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC
//target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") ~ #pragma
//GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define st first
#define nd second
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x) \
sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<( \
c i) {
sim > struct rge {
c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i;
ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define shandom_ruffle random_shuffle
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
const int kAlpha = 26;
const int kMaxSize = 4e5;
struct Node {
int children[kAlpha];
bool is_end;
};
int num_nodes = 1;
Node nodes[kMaxSize];
int cnt_alpha[kAlpha];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(11);
cerr << fixed << setprecision(6);
int n;
cin >> n;
vector<string> ss(n);
for (auto &x : ss) {
cin >> x;
}
sort(ALL(ss),
[&](const string &lhs, const string &rhs) { return SZ(lhs) < SZ(rhs); });
ll ans = 0;
for (string str : ss) {
for (int i = 0; i < kAlpha; ++i) {
cnt_alpha[i] = 0;
}
for (char ch : str) {
++cnt_alpha[ch - 'a'];
}
const int len = SZ(str);
int curv = 0;
for (int i = len - 1; i >= 0; --i) {
const char ch = str[i];
const int chidx = ch - 'a';
for (int other = 0; other < kAlpha; ++other) {
if (!cnt_alpha[other]) {
continue;
}
const int s = nodes[curv].children[other];
if (s && nodes[s].is_end) {
++ans;
}
}
--cnt_alpha[chidx];
if (!nodes[curv].children[chidx]) {
break;
} else {
curv = nodes[curv].children[chidx];
}
}
curv = 0;
for (int i = len - 1; i >= 0; --i) {
const char ch = str[i];
const int chidx = ch - 'a';
int &s = nodes[curv].children[chidx];
if (!s) {
s = num_nodes++;
}
curv = s;
}
nodes[curv].is_end = true;
}
cout << ans << "\n";
}
| //~ while (clock()<=69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC
//target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") ~ #pragma
//GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define st first
#define nd second
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x) \
sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<( \
c i) {
sim > struct rge {
c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i;
ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define shandom_ruffle random_shuffle
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
const int kAlpha = 26;
const int kMaxSize = 2e6;
struct Node {
int children[kAlpha];
bool is_end;
};
int num_nodes = 1;
Node nodes[kMaxSize];
int cnt_alpha[kAlpha];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(11);
cerr << fixed << setprecision(6);
int n;
cin >> n;
vector<string> ss(n);
for (auto &x : ss) {
cin >> x;
}
sort(ALL(ss),
[&](const string &lhs, const string &rhs) { return SZ(lhs) < SZ(rhs); });
ll ans = 0;
for (string str : ss) {
for (int i = 0; i < kAlpha; ++i) {
cnt_alpha[i] = 0;
}
for (char ch : str) {
++cnt_alpha[ch - 'a'];
}
const int len = SZ(str);
int curv = 0;
for (int i = len - 1; i >= 0; --i) {
const char ch = str[i];
const int chidx = ch - 'a';
for (int other = 0; other < kAlpha; ++other) {
if (!cnt_alpha[other]) {
continue;
}
const int s = nodes[curv].children[other];
if (s && nodes[s].is_end) {
++ans;
}
}
--cnt_alpha[chidx];
if (!nodes[curv].children[chidx]) {
break;
} else {
curv = nodes[curv].children[chidx];
}
}
curv = 0;
for (int i = len - 1; i >= 0; --i) {
const char ch = str[i];
const int chidx = ch - 'a';
int &s = nodes[curv].children[chidx];
if (!s) {
s = num_nodes++;
}
curv = s;
}
nodes[curv].is_end = true;
}
cout << ans << "\n";
}
| replace | 68 | 69 | 68 | 69 | 0 | |
p02589 | C++ | Runtime Error |
#pragma comment(linker, "/STACK: 16777216")
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define allp(x) (x)->begin(), (x)->end()
#define pb push_back
using namespace std;
void dout() { cerr << endl; }
// typedef long long ll;
template <typename Head, typename... Tail> void dout(Head H, Tail... T) {
cerr << H << ' ';
dout(T...);
}
#define debug(x) cout << #x " = " << (x) << endl
using ll = long long;
using hll = __int128;
using pii = pair<ll, ll>;
using ld = long double;
using pll = pair<long long, long long>;
template <typename T> void do_uniq(vector<T> vec) {
sort(all(vec));
vec.resize(unique(all(vec)) - vec.begin());
}
#ifdef _getchar_nolock
#else
#define _getchar_nolock getchar_unlocked
#endif
template <typename T> void freadT(T &x) {
x = 0;
char ch = _getchar_nolock();
ll sign = 1;
while (!isdigit(ch)) {
if (ch == '-')
sign *= -1;
ch = _getchar_nolock();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = _getchar_nolock();
}
x *= sign;
}
int fparse(string s, int start_indx) {
int x = 0;
while (isdigit(s[start_indx])) {
x = x * 10 + s[start_indx] - '0';
++start_indx;
}
return x;
}
#define solvsh
// #define multi
#ifdef solvsh
const int K = 26;
const int NMAX = 2e5 + 120;
struct vertex {
int next[K];
int p;
char pch;
int link;
int go[K];
int subtree[K];
};
vertex t[NMAX + 1];
int sz;
void init() {
t[0].p = t[0].link = -1;
memset(t[0].next, 255, sizeof t[0].next);
memset(t[0].go, 255, sizeof t[0].go);
memset(t[0].subtree, 0, sizeof t[0].subtree);
sz = 1;
}
int cnt_crr[26];
void add_string(const string &s, int indx = 0, int v = 0) {
if (indx == s.size())
return;
char c = s[indx] - 'a';
if (t[v].next[c] == -1) {
memset(t[sz].next, 255, sizeof t[sz].next);
memset(t[sz].go, 255, sizeof t[sz].go);
memset(t[sz].subtree, 0, sizeof t[sz].subtree);
t[sz].link = -1;
t[sz].p = v;
t[sz].pch = c;
t[v].next[c] = sz++;
}
for (int i = 0; i < 26; ++i) {
if (cnt_crr[i])
t[v].subtree[i]++;
}
cnt_crr[c]--;
add_string(s, indx + 1, t[v].next[c]);
}
int go(int v, char c);
int get_link(int v) {
if (t[v].link == -1)
if (v == 0 || t[v].p == 0)
t[v].link = 0;
else
t[v].link = go(get_link(t[v].p), t[v].pch);
return t[v].link;
}
int go(int v, char c) {
if (t[v].go[c] == -1)
if (t[v].next[c] != -1)
t[v].go[c] = t[v].next[c];
else
t[v].go[c] = v == 0 ? 0 : go(get_link(v), c);
return t[v].go[c];
}
void solve() {
int n;
cin >> n;
vector<string> v;
for (int i = 0; i < n; ++i) {
string s;
cin >> s;
reverse(all(s));
v.push_back(s);
}
init();
for (auto s : v) {
for (int i = 0; i < 26; ++i)
cnt_crr[i] = 0;
for (auto e : s)
cnt_crr[e - 'a']++;
add_string(s);
}
ll answr = 0;
for (auto s : v) {
int ver = 0;
for (int j = 0; j < s.size() - 1; ++j) {
ver = go(ver, s[j] - 'a');
}
answr += t[ver].subtree[s.back() - 'a'] - 1;
}
cout << answr << "\n";
}
#else
void solve() {}
#endif
clock_t timestamp_start = clock();
void time_calc() {
cerr << (ld)(clock() - timestamp_start) / CLOCKS_PER_SEC << "\n";
}
void multisolve() {
// prec();
int t;
cin >> t;
// prec();
// scanf("%d", &t);
int i = 1;
while (t--) {
// cout << "Case " << (i ++)<<": \n";
// printf("Case %d: \n", i++);
solve();
i++;
// cout << "\n";
}
}
#define int int
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
cout << fixed << setprecision(20);
#ifdef multi
multisolve();
#else
solve();
// gen();
#endif
} |
#pragma comment(linker, "/STACK: 16777216")
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define allp(x) (x)->begin(), (x)->end()
#define pb push_back
using namespace std;
void dout() { cerr << endl; }
// typedef long long ll;
template <typename Head, typename... Tail> void dout(Head H, Tail... T) {
cerr << H << ' ';
dout(T...);
}
#define debug(x) cout << #x " = " << (x) << endl
using ll = long long;
using hll = __int128;
using pii = pair<ll, ll>;
using ld = long double;
using pll = pair<long long, long long>;
template <typename T> void do_uniq(vector<T> vec) {
sort(all(vec));
vec.resize(unique(all(vec)) - vec.begin());
}
#ifdef _getchar_nolock
#else
#define _getchar_nolock getchar_unlocked
#endif
template <typename T> void freadT(T &x) {
x = 0;
char ch = _getchar_nolock();
ll sign = 1;
while (!isdigit(ch)) {
if (ch == '-')
sign *= -1;
ch = _getchar_nolock();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = _getchar_nolock();
}
x *= sign;
}
int fparse(string s, int start_indx) {
int x = 0;
while (isdigit(s[start_indx])) {
x = x * 10 + s[start_indx] - '0';
++start_indx;
}
return x;
}
#define solvsh
// #define multi
#ifdef solvsh
const int K = 26;
const int NMAX = 3e6 + 120;
struct vertex {
int next[K];
int p;
char pch;
int link;
int go[K];
int subtree[K];
};
vertex t[NMAX + 1];
int sz;
void init() {
t[0].p = t[0].link = -1;
memset(t[0].next, 255, sizeof t[0].next);
memset(t[0].go, 255, sizeof t[0].go);
memset(t[0].subtree, 0, sizeof t[0].subtree);
sz = 1;
}
int cnt_crr[26];
void add_string(const string &s, int indx = 0, int v = 0) {
if (indx == s.size())
return;
char c = s[indx] - 'a';
if (t[v].next[c] == -1) {
memset(t[sz].next, 255, sizeof t[sz].next);
memset(t[sz].go, 255, sizeof t[sz].go);
memset(t[sz].subtree, 0, sizeof t[sz].subtree);
t[sz].link = -1;
t[sz].p = v;
t[sz].pch = c;
t[v].next[c] = sz++;
}
for (int i = 0; i < 26; ++i) {
if (cnt_crr[i])
t[v].subtree[i]++;
}
cnt_crr[c]--;
add_string(s, indx + 1, t[v].next[c]);
}
int go(int v, char c);
int get_link(int v) {
if (t[v].link == -1)
if (v == 0 || t[v].p == 0)
t[v].link = 0;
else
t[v].link = go(get_link(t[v].p), t[v].pch);
return t[v].link;
}
int go(int v, char c) {
if (t[v].go[c] == -1)
if (t[v].next[c] != -1)
t[v].go[c] = t[v].next[c];
else
t[v].go[c] = v == 0 ? 0 : go(get_link(v), c);
return t[v].go[c];
}
void solve() {
int n;
cin >> n;
vector<string> v;
for (int i = 0; i < n; ++i) {
string s;
cin >> s;
reverse(all(s));
v.push_back(s);
}
init();
for (auto s : v) {
for (int i = 0; i < 26; ++i)
cnt_crr[i] = 0;
for (auto e : s)
cnt_crr[e - 'a']++;
add_string(s);
}
ll answr = 0;
for (auto s : v) {
int ver = 0;
for (int j = 0; j < s.size() - 1; ++j) {
ver = go(ver, s[j] - 'a');
}
answr += t[ver].subtree[s.back() - 'a'] - 1;
}
cout << answr << "\n";
}
#else
void solve() {}
#endif
clock_t timestamp_start = clock();
void time_calc() {
cerr << (ld)(clock() - timestamp_start) / CLOCKS_PER_SEC << "\n";
}
void multisolve() {
// prec();
int t;
cin >> t;
// prec();
// scanf("%d", &t);
int i = 1;
while (t--) {
// cout << "Case " << (i ++)<<": \n";
// printf("Case %d: \n", i++);
solve();
i++;
// cout << "\n";
}
}
#define int int
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
cout << fixed << setprecision(20);
#ifdef multi
multisolve();
#else
solve();
// gen();
#endif
} | replace | 72 | 73 | 72 | 73 | 0 | |
p02589 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned ll
#define pint pair<int, int>
#define mk(x, y) make_pair(x, y)
#define fir first
#define sec second
#define Rep(x, y, z) for (int x = y; x <= z; ++x)
#define Red(x, y, z) for (int x = y; x >= z; --x)
using namespace std;
const int MAXN = 2e5 + 5, MAXM = 1e6 + 5, base = 13131;
char buf[1 << 12], *p1 = buf, *p2 = buf, nc;
int ny;
// inline char gc() {return
// p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<12,stdin),p1==p2)?EOF:*p1++;}
inline char gc() { return getchar(); }
inline int read() {
int x = 0;
ny = 1;
while (nc = gc(), (nc < 48 || nc > 57) && nc != EOF)
if (nc == 45)
ny = -1;
if (nc < 0)
return nc;
x = nc - 48;
while (nc = gc(), 47 < nc && nc < 58 && nc != EOF)
x = (x << 3) + (x << 1) + (nc ^ 48);
return x * ny;
}
int n, vis[26];
ull Fpow[MAXM];
vector<ull> Has[MAXN];
string S[MAXN];
int len[MAXN];
unordered_map<ull, int> cnt[26];
int main() {
// freopen("std.in","r",stdin);
// freopen("std.out","w",stdout);
n = read();
Fpow[0] = 1;
Rep(i, 1, MAXM - 1) Fpow[i] = Fpow[i - 1] * base;
Rep(i, 1, n) {
cin >> S[i];
len[i] = S[i].size();
Has[i].resize(len[i] + 1), Has[i][len[i]] = 0;
for (int j = len[i] - 1; j >= 0; j--)
Has[i][j] = Has[i][j + 1] + Fpow[len[i] - j - 1] * S[i][j];
cnt[S[i][0] - 'a'][Has[i][1]]++;
}
ll ans = 0;
Rep(i, 1, n) {
Rep(c, 0, 25) vis[c] = 0;
cnt[S[i][0] - 'a'][Has[i][1]]--;
for (int j = 0; j <= len[i]; j++) {
Rep(c, 0, 25) if (vis[c]) ans += cnt[c][Has[i][j]];
vis[S[i][j] - 'a'] = 1;
}
cnt[S[i][0] - 'a'][Has[i][1]]++;
}
cout << ans << "\n";
return 0;
}
| #include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned ll
#define pint pair<int, int>
#define mk(x, y) make_pair(x, y)
#define fir first
#define sec second
#define Rep(x, y, z) for (int x = y; x <= z; ++x)
#define Red(x, y, z) for (int x = y; x >= z; --x)
using namespace std;
const int MAXN = 2e5 + 5, MAXM = 1e6 + 5, base = 13131;
char buf[1 << 12], *p1 = buf, *p2 = buf, nc;
int ny;
// inline char gc() {return
// p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<12,stdin),p1==p2)?EOF:*p1++;}
inline char gc() { return getchar(); }
inline int read() {
int x = 0;
ny = 1;
while (nc = gc(), (nc < 48 || nc > 57) && nc != EOF)
if (nc == 45)
ny = -1;
if (nc < 0)
return nc;
x = nc - 48;
while (nc = gc(), 47 < nc && nc < 58 && nc != EOF)
x = (x << 3) + (x << 1) + (nc ^ 48);
return x * ny;
}
int n, vis[26];
ull Fpow[MAXM];
vector<ull> Has[MAXN];
string S[MAXN];
int len[MAXN];
unordered_map<ull, int> cnt[26];
int main() {
// freopen("std.in","r",stdin);
// freopen("std.out","w",stdout);
n = read();
Fpow[0] = 1;
Rep(i, 1, MAXM - 1) Fpow[i] = Fpow[i - 1] * base;
Rep(i, 1, n) {
cin >> S[i];
len[i] = S[i].size();
Has[i].resize(len[i] + 1), Has[i][len[i]] = 0;
for (int j = len[i] - 1; j >= 0; j--)
Has[i][j] = Has[i][j + 1] + Fpow[len[i] - j - 1] * S[i][j];
cnt[S[i][0] - 'a'][Has[i][1]]++;
}
ll ans = 0;
Rep(i, 1, n) {
Rep(c, 0, 25) vis[c] = 0;
cnt[S[i][0] - 'a'][Has[i][1]]--;
for (int j = 0; j <= len[i]; j++) {
Rep(c, 0, 25) if (vis[c]) {
if (cnt[c].count(Has[i][j]))
ans += cnt[c][Has[i][j]];
}
vis[S[i][j] - 'a'] = 1;
}
cnt[S[i][0] - 'a'][Has[i][1]]++;
}
cout << ans << "\n";
return 0;
}
| replace | 55 | 56 | 55 | 59 | TLE | |
p02589 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAX_N = 2e5 + 5;
int N;
string s[MAX_N];
int ch[MAX_N][26];
int ed[MAX_N], tot = 1;
void ins(string s) {
int o = 1;
for (int i = 0; i < (int)s.length(); i++) {
int c = s[i] - 'a';
if (!ch[o][c])
ch[o][c] = ++tot;
o = ch[o][c];
}
ed[o]++;
}
int main() {
ios::sync_with_stdio(0);
cin >> N;
for (int i = 1; i <= N; i++)
cin >> s[i];
for (int i = 1; i <= N; i++)
reverse(s[i].begin(), s[i].end()), ins(s[i]);
LL ans = 0;
for (int i = 1; i <= N; i++) {
int o = 1, cnt = 0, buc[30];
memset(buc, 0, sizeof(buc));
for (int j = 0; j < (int)s[i].length(); j++)
++buc[s[i][j] - 'a'];
for (int j = 0; j < (int)s[i].length(); j++) {
for (int k = 0; k < 26; k++)
if (buc[k] && k != s[i][j] - 'a')
cnt += ed[ch[o][k]];
o = ch[o][s[i][j] - 'a'], buc[s[i][j] - 'a']--, cnt += ed[o];
}
ans += cnt;
}
cout << ans - N << '\n';
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAX_N = 2e6 + 5;
int N;
string s[MAX_N];
int ch[MAX_N][26];
int ed[MAX_N], tot = 1;
void ins(string s) {
int o = 1;
for (int i = 0; i < (int)s.length(); i++) {
int c = s[i] - 'a';
if (!ch[o][c])
ch[o][c] = ++tot;
o = ch[o][c];
}
ed[o]++;
}
int main() {
ios::sync_with_stdio(0);
cin >> N;
for (int i = 1; i <= N; i++)
cin >> s[i];
for (int i = 1; i <= N; i++)
reverse(s[i].begin(), s[i].end()), ins(s[i]);
LL ans = 0;
for (int i = 1; i <= N; i++) {
int o = 1, cnt = 0, buc[30];
memset(buc, 0, sizeof(buc));
for (int j = 0; j < (int)s[i].length(); j++)
++buc[s[i][j] - 'a'];
for (int j = 0; j < (int)s[i].length(); j++) {
for (int k = 0; k < 26; k++)
if (buc[k] && k != s[i][j] - 'a')
cnt += ed[ch[o][k]];
o = ch[o][s[i][j] - 'a'], buc[s[i][j] - 'a']--, cnt += ed[o];
}
ans += cnt;
}
cout << ans - N << '\n';
return 0;
} | replace | 3 | 4 | 3 | 4 | 0 | |
p02589 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define ff first
#define ss second
#define pii pair<int, int>
#define pb emplace_back
#define pf emplace_front
#define mp make_pair
#define ld long double
#define all(x) x.begin(), x.end()
#define uniq(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
const int maxn = 2e5 + 9;
int g[maxn][26];
int dp[maxn][26];
int cnt[maxn];
string s[maxn];
int n, cnt_v;
bool used[maxn];
void add_ver() {
for (int j = 0; j < 26; j++)
g[cnt_v][j] = -1;
cnt_v++;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
add_ver();
for (int i = 0; i < n; i++) {
cin >> s[i];
reverse(all(s[i]));
int v = 0;
vector<int> cur;
cur.pb(v);
for (int e = 0; e < (int)s[i].size(); e++) {
if (g[v][s[i][e] - 'a'] == -1) {
g[v][s[i][e] - 'a'] = cnt_v;
add_ver();
}
v = g[v][s[i][e] - 'a'];
cur.pb(v);
}
cnt[v]++;
for (int j = 0; j < 26; j++)
used[j] = 0;
for (int e = (int)s[i].size() - 1; e >= 0; e--) {
if (!used[s[i][e] - 'a']) {
dp[cur[e]][s[i][e] - 'a']++;
used[s[i][e] - 'a'] = 1;
}
}
}
for (int v = cnt_v - 1; v >= 0; v--)
for (int j = 0; j < 26; j++)
for (int e = 0; e < 26; e++)
dp[v][e] += (g[v][j] >= 0 ? dp[g[v][j]][e] : 0);
int ans = 0;
for (int v = 0; v < cnt_v; v++)
for (int j = 0; j < 26; j++)
ans += (g[v][j] >= 0 ? cnt[g[v][j]] * (dp[v][j] - cnt[g[v][j]]) : 0);
for (int i = 0; i < cnt_v; i++)
ans += cnt[i] * (cnt[i] - 1) / 2;
cout << ans;
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
#define ff first
#define ss second
#define pii pair<int, int>
#define pb emplace_back
#define pf emplace_front
#define mp make_pair
#define ld long double
#define all(x) x.begin(), x.end()
#define uniq(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
const int maxn = 1e6 + 9;
int g[maxn][26];
int dp[maxn][26];
int cnt[maxn];
string s[maxn];
int n, cnt_v;
bool used[maxn];
void add_ver() {
for (int j = 0; j < 26; j++)
g[cnt_v][j] = -1;
cnt_v++;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
add_ver();
for (int i = 0; i < n; i++) {
cin >> s[i];
reverse(all(s[i]));
int v = 0;
vector<int> cur;
cur.pb(v);
for (int e = 0; e < (int)s[i].size(); e++) {
if (g[v][s[i][e] - 'a'] == -1) {
g[v][s[i][e] - 'a'] = cnt_v;
add_ver();
}
v = g[v][s[i][e] - 'a'];
cur.pb(v);
}
cnt[v]++;
for (int j = 0; j < 26; j++)
used[j] = 0;
for (int e = (int)s[i].size() - 1; e >= 0; e--) {
if (!used[s[i][e] - 'a']) {
dp[cur[e]][s[i][e] - 'a']++;
used[s[i][e] - 'a'] = 1;
}
}
}
for (int v = cnt_v - 1; v >= 0; v--)
for (int j = 0; j < 26; j++)
for (int e = 0; e < 26; e++)
dp[v][e] += (g[v][j] >= 0 ? dp[g[v][j]][e] : 0);
int ans = 0;
for (int v = 0; v < cnt_v; v++)
for (int j = 0; j < 26; j++)
ans += (g[v][j] >= 0 ? cnt[g[v][j]] * (dp[v][j] - cnt[g[v][j]]) : 0);
for (int i = 0; i < cnt_v; i++)
ans += cnt[i] * (cnt[i] - 1) / 2;
cout << ans;
}
| replace | 15 | 16 | 15 | 16 | 0 | |
p02589 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define fir first
#define sec second
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define rrp(i, a, b) for (int i = (a); i >= (b); --i)
#define gc() getchar()
template <typename tp> inline void read(tp &x) {
x = 0;
char tmp;
bool key = 0;
for (tmp = gc(); !isdigit(tmp); tmp = gc())
key = (tmp == '-');
for (; isdigit(tmp); tmp = gc())
x = (x << 3) + (x << 1) + (tmp ^ '0');
if (key)
x = -x;
}
template <typename tp> inline void ckmn(tp &x, tp y) { x = x < y ? x : y; }
template <typename tp> inline void ckmx(tp &x, tp y) { x = x < y ? y : x; }
typedef double db;
const int N = 200010;
int n;
string str[N];
int ch[N * 4][26], cnt = 1, sz[N * 4][26], num[N * 4];
void ins(string s) {
int p = 1;
int hav[26];
rep(i, 0, 25) hav[i] = 0;
rep(i, 0, (int)s.size() - 1) hav[s[i] - 'a']++;
rep(i, 0, (int)s.size() - 1) {
rep(j, 0, 25) sz[p][j] += (hav[j] > 0);
num[p]++;
if (!ch[p][s[i] - 'a'])
ch[p][s[i] - 'a'] = ++cnt;
p = ch[p][s[i] - 'a'];
--hav[s[i] - 'a'];
}
++num[p];
}
ll ans = 0;
void doit(string s) {
int p = 1;
rep(i, 0, (int)s.size()) {
if (i + 1 == (int)s.size()) {
int c = s[i] - 'a';
rep(a, 0, 25) if (a != c) { ans += sz[ch[p][a]][c]; }
}
if (i == (int)s.size()) {
ans += num[p];
break;
}
p = ch[p][s[i] - 'a'];
}
}
int main() {
read(n);
rep(i, 1, n) cin >> str[i];
rep(i, 1, n) reverse(str[i].begin(), str[i].end());
sort(str + 1, str + n + 1,
[=](string a, string b) -> bool { return a.size() > b.size(); });
rep(i, 1, n) {
doit(str[i]);
ins(str[i]);
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define fir first
#define sec second
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define rrp(i, a, b) for (int i = (a); i >= (b); --i)
#define gc() getchar()
template <typename tp> inline void read(tp &x) {
x = 0;
char tmp;
bool key = 0;
for (tmp = gc(); !isdigit(tmp); tmp = gc())
key = (tmp == '-');
for (; isdigit(tmp); tmp = gc())
x = (x << 3) + (x << 1) + (tmp ^ '0');
if (key)
x = -x;
}
template <typename tp> inline void ckmn(tp &x, tp y) { x = x < y ? x : y; }
template <typename tp> inline void ckmx(tp &x, tp y) { x = x < y ? y : x; }
typedef double db;
const int N = 200010;
int n;
string str[N];
int ch[N * 5][26], cnt = 1, sz[N * 5][26], num[N * 5];
void ins(string s) {
int p = 1;
int hav[26];
rep(i, 0, 25) hav[i] = 0;
rep(i, 0, (int)s.size() - 1) hav[s[i] - 'a']++;
rep(i, 0, (int)s.size() - 1) {
rep(j, 0, 25) sz[p][j] += (hav[j] > 0);
num[p]++;
if (!ch[p][s[i] - 'a'])
ch[p][s[i] - 'a'] = ++cnt;
p = ch[p][s[i] - 'a'];
--hav[s[i] - 'a'];
}
++num[p];
}
ll ans = 0;
void doit(string s) {
int p = 1;
rep(i, 0, (int)s.size()) {
if (i + 1 == (int)s.size()) {
int c = s[i] - 'a';
rep(a, 0, 25) if (a != c) { ans += sz[ch[p][a]][c]; }
}
if (i == (int)s.size()) {
ans += num[p];
break;
}
p = ch[p][s[i] - 'a'];
}
}
int main() {
read(n);
rep(i, 1, n) cin >> str[i];
rep(i, 1, n) reverse(str[i].begin(), str[i].end());
sort(str + 1, str + n + 1,
[=](string a, string b) -> bool { return a.size() > b.size(); });
rep(i, 1, n) {
doit(str[i]);
ins(str[i]);
}
cout << ans << endl;
return 0;
} | replace | 27 | 28 | 27 | 28 | -11 | |
p02589 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int N;
string s[200010];
char tmp[200010];
typedef long long LL;
bool vis[1000010][26];
int ch[1000010][26];
bool isend[1000010];
int tot = 1;
long long ans = 0;
void ins(string s) {
int n = s.length();
for (int i = 0; i < 26; i++)
vis[0][i] = (s[0] - 'a' == i);
for (int i = 1; i < n; i++) {
for (int j = 0; j < 26; j++) {
vis[i][j] = (vis[i - 1][j] | (s[i] - 'a' == j));
}
}
int now = 1;
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j < 26; j++) {
if (ch[now][j] && isend[ch[now][j]] && vis[i][j])
ans++;
}
int v = s[i] - 'a';
if (!ch[now][v])
ch[now][v] = ++tot;
now = ch[now][v];
}
isend[now] = true;
}
int main() {
scanf("%d", &N);
for (int i = 1; i <= N; i++) {
scanf("%s", tmp);
s[i] = string(tmp);
}
sort(s + 1, s + N + 1,
[&](string s1, string s2) { return s1.length() < s2.length(); });
for (int i = 1; i <= N; i++) {
ins(s[i]);
}
printf("%lld\n", ans);
} | #include <bits/stdc++.h>
using namespace std;
int N;
string s[200010];
char tmp[1000010];
typedef long long LL;
bool vis[1000010][26];
int ch[1000010][26];
bool isend[1000010];
int tot = 1;
long long ans = 0;
void ins(string s) {
int n = s.length();
for (int i = 0; i < 26; i++)
vis[0][i] = (s[0] - 'a' == i);
for (int i = 1; i < n; i++) {
for (int j = 0; j < 26; j++) {
vis[i][j] = (vis[i - 1][j] | (s[i] - 'a' == j));
}
}
int now = 1;
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j < 26; j++) {
if (ch[now][j] && isend[ch[now][j]] && vis[i][j])
ans++;
}
int v = s[i] - 'a';
if (!ch[now][v])
ch[now][v] = ++tot;
now = ch[now][v];
}
isend[now] = true;
}
int main() {
scanf("%d", &N);
for (int i = 1; i <= N; i++) {
scanf("%s", tmp);
s[i] = string(tmp);
}
sort(s + 1, s + N + 1,
[&](string s1, string s2) { return s1.length() < s2.length(); });
for (int i = 1; i <= N; i++) {
ins(s[i]);
}
printf("%lld\n", ans);
} | replace | 6 | 7 | 6 | 7 | -11 | |
p02589 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef pair<int, int> P;
typedef tuple<int, int, int> tpl;
#define ALL(a) (a).begin(), (a).end()
#define SORT(c) sort((c).begin(), (c).end())
#define REVERSE(c) reverse((c).begin(), (c).end())
#define EXIST(m, v) (m).find((v)) != (m).end()
#define LB(a, x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a, x) upper_bound((a).begin(), (a).end(), x) - (a).begin()
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (int i = (a)-1; i >= (b); --i)
#define RREP(i, n) RFOR(i, n, 0)
#define en "\n"
constexpr double EPS = 1e-9;
constexpr double PI = 3.1415926535897932;
constexpr int INF = 2147483647;
constexpr long long LINF = 1LL << 60;
const long long MOD = (1LL << 61) - 1;
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;
}
struct RollingHash {
const long long MASK30 = (1LL << 30) - 1;
const long long MASK31 = (1LL << 31) - 1;
const long long MASK61 = MOD;
int N;
vector<long long> hs, b;
long long base;
RollingHash(string &s, long long base_ = -1) {
if (base_ == -1) {
random_device rnd; // 非決定的な乱数生成器
mt19937 mt(rnd());
base = mt() + 1;
} else {
base = base_;
}
N = s.size();
hs.resize(N + 1);
b.resize(N + 1);
b[0] = 1;
hs[0] = 0;
for (int i = 0; i < N; i++) {
b[i + 1] = Mul(b[i], base);
hs[i + 1] = Mul(hs[i], base) + s[i];
if (hs[i + 1] >= MOD)
hs[i + 1] -= MOD;
}
}
long long get(int l, int r) {
long long ret = hs[r] + MOD - Mul(hs[l], b[r - l]);
if (ret >= MOD)
ret -= MOD;
return ret;
}
// mod 2^61-1を計算する関数
long long CalcMod(long long x) {
long long xu = x >> 61;
long long xd = x & MASK61;
long long ret = xu + xd;
if (ret >= MOD)
ret -= MOD;
return ret;
}
// a*b mod 2^61-1を返す関数(最後にModを取る)
long long Mul(long long a, long long b) {
long long au = a >> 31;
long long ad = a & MASK31;
long long bu = b >> 31;
long long bd = b & MASK31;
long long mid = ad * bu + au * bd;
long long midu = mid >> 30;
long long midd = mid & MASK30;
return CalcMod(au * bu * 2 + midu + (midd << 31) + ad * bd);
}
};
bool comp(string s, string t) { return s.size() < t.size(); }
void Main() {
int N;
cin >> N;
vector<string> S(N);
REP(i, N) {
cin >> S[i];
REVERSE(S[i]);
}
sort(ALL(S), comp);
random_device rnd; // 非決定的な乱数生成器
mt19937 mt(rnd());
ll base = mt() + 1;
ll ans = 0;
map<ll, ll> m;
REP(i, N) {
int k = S[i].size();
VVI n(26, VI(k + 1, 0));
REP(j, k) {
int x = S[i][j] - 'a';
n[x][j] = 1;
}
REP(l, 26) REP(j, k) n[l][k - 1 - j] += n[l][k - j];
RollingHash rh(S[i], base);
REP(j, k) {
string s = S[i].substr(0, j) + "-";
REP(a, 26) {
if (n[a][j] == 0)
continue;
ll h = rh.Mul(rh.get(0, j), base) + (ll)(a + 'a');
if (EXIST(m, h))
ans += m[h];
}
}
ll h = rh.get(0, k);
if (EXIST(m, h))
m[h]++;
else
m[h] = 1;
}
cout << ans << en;
return;
}
int main(void) {
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
cout << fixed << setprecision(15);
int t = 1; // cin>>t;
REP(_, t) Main();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef pair<int, int> P;
typedef tuple<int, int, int> tpl;
#define ALL(a) (a).begin(), (a).end()
#define SORT(c) sort((c).begin(), (c).end())
#define REVERSE(c) reverse((c).begin(), (c).end())
#define EXIST(m, v) (m).find((v)) != (m).end()
#define LB(a, x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a, x) upper_bound((a).begin(), (a).end(), x) - (a).begin()
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (int i = (a)-1; i >= (b); --i)
#define RREP(i, n) RFOR(i, n, 0)
#define en "\n"
constexpr double EPS = 1e-9;
constexpr double PI = 3.1415926535897932;
constexpr int INF = 2147483647;
constexpr long long LINF = 1LL << 60;
const long long MOD = (1LL << 61) - 1;
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;
}
struct RollingHash {
const long long MASK30 = (1LL << 30) - 1;
const long long MASK31 = (1LL << 31) - 1;
const long long MASK61 = MOD;
int N;
vector<long long> hs, b;
long long base;
RollingHash(string &s, long long base_ = -1) {
if (base_ == -1) {
random_device rnd; // 非決定的な乱数生成器
mt19937 mt(rnd());
base = mt() + 1;
} else {
base = base_;
}
N = s.size();
hs.resize(N + 1);
b.resize(N + 1);
b[0] = 1;
hs[0] = 0;
for (int i = 0; i < N; i++) {
b[i + 1] = Mul(b[i], base);
hs[i + 1] = Mul(hs[i], base) + s[i];
if (hs[i + 1] >= MOD)
hs[i + 1] -= MOD;
}
}
long long get(int l, int r) {
long long ret = hs[r] + MOD - Mul(hs[l], b[r - l]);
if (ret >= MOD)
ret -= MOD;
return ret;
}
// mod 2^61-1を計算する関数
long long CalcMod(long long x) {
long long xu = x >> 61;
long long xd = x & MASK61;
long long ret = xu + xd;
if (ret >= MOD)
ret -= MOD;
return ret;
}
// a*b mod 2^61-1を返す関数(最後にModを取る)
long long Mul(long long a, long long b) {
long long au = a >> 31;
long long ad = a & MASK31;
long long bu = b >> 31;
long long bd = b & MASK31;
long long mid = ad * bu + au * bd;
long long midu = mid >> 30;
long long midd = mid & MASK30;
return CalcMod(au * bu * 2 + midu + (midd << 31) + ad * bd);
}
};
bool comp(string s, string t) { return s.size() < t.size(); }
void Main() {
int N;
cin >> N;
vector<string> S(N);
REP(i, N) {
cin >> S[i];
REVERSE(S[i]);
}
sort(ALL(S), comp);
random_device rnd; // 非決定的な乱数生成器
mt19937 mt(rnd());
ll base = mt() + 1;
ll ans = 0;
map<ll, ll> m;
REP(i, N) {
int k = S[i].size();
VVI n(26, VI(k + 1, 0));
REP(j, k) {
int x = S[i][j] - 'a';
n[x][j] = 1;
}
REP(l, 26) REP(j, k) n[l][k - 1 - j] += n[l][k - j];
RollingHash rh(S[i], base);
REP(j, k) {
REP(a, 26) {
if (n[a][j] == 0)
continue;
ll h = rh.Mul(rh.get(0, j), base) + (ll)(a + 'a');
if (EXIST(m, h))
ans += m[h];
}
}
ll h = rh.get(0, k);
if (EXIST(m, h))
m[h]++;
else
m[h] = 1;
}
cout << ans << en;
return;
}
int main(void) {
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
cout << fixed << setprecision(15);
int t = 1; // cin>>t;
REP(_, t) Main();
return 0;
} | delete | 136 | 137 | 136 | 136 | TLE | |
p02590 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using int64 = long long;
constexpr int DEBUG = 0;
constexpr int64 P = 200003;
using Complex = complex<double>;
// Reference: http://www.prefield.com/algorithm/math/fft.html
vector<Complex> DFT(const vector<Complex> &p, int n, int sign = 1) {
const double theta = 2.0 * M_PI / n * sign;
int unit = 1;
vector<Complex> q = p;
for (int m = n; m >= 2; m >>= 1) {
int mh = m >> 1;
for (int i = 0; i < mh; i++) {
Complex w = Complex(cos(theta * unit * i), sin(theta * unit * i));
for (int j = i; j < n; j += m) {
int k = j + mh;
Complex x = q[j] - q[k];
q[j] += q[k];
q[k] = w * x;
}
}
unit *= 2;
}
int i = 0;
for (int j = 1; j < n - 1; j++) {
for (int k = n >> 1; k > (i ^= k); k >>= 1)
;
if (j < i)
swap(q[i], q[j]);
}
return q;
}
vector<Complex> InvDFT(const vector<Complex> &p, int n) {
auto q = DFT(p, n, -1);
for (int i = 0; i < n; i++) {
q[i] /= n;
}
return q;
}
vector<Complex> MultiplyFFT(const vector<Complex> &p1,
const vector<Complex> &p2) {
int d = p1.size() + p2.size() - 1;
int n = 1;
while (n < d + 1)
n *= 2;
auto q1 = p1;
q1.resize(n);
auto q2 = p2;
q2.resize(n);
auto r1 = DFT(q1, n);
auto r2 = DFT(q2, n);
vector<Complex> s(n);
for (int i = 0; i < n; i++) {
s[i] = r1[i] * r2[i];
}
return InvDFT(s, n);
}
vector<int64> MultiplyFFT(const vector<int64> &p1, const vector<int64> &p2) {
vector<Complex> q1;
for (int i = 0; i < p1.size(); i++) {
q1.emplace_back(p1[i], 0);
}
vector<Complex> q2;
for (int i = 0; i < p2.size(); i++) {
q2.emplace_back(p2[i], 0);
}
vector<Complex> r = MultiplyFFT(q1, q2);
vector<int64> s;
for (int i = 0; i < r.size(); i++) {
s.push_back(llround(r[i].real()));
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
// i = 2^p
vector<int64> i_to_p(P, -1);
vector<int64> p_to_i(P - 1, -1);
{
int64 x = 1;
for (int i = 0; i < P - 1; i++) {
i_to_p[x] = i;
p_to_i[i] = x;
x *= 2;
x %= P;
}
}
int n;
cin >> n;
int64 delta = 0;
vector<int64> xs(P - 1);
for (int i = 0; i < n; i++) {
int64 a;
cin >> a;
if (a == 0)
continue;
xs[i_to_p[a]] += 1;
delta += a * a % P;
}
vector<int64> ys = MultiplyFFT(xs, xs);
int64 ans = 0;
for (int i = 0; i < ys.size(); i++) {
int k = i % (P - 1);
ans += p_to_i[k] * ys[i];
}
ans -= delta;
ans /= 2;
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
using int64 = long long;
constexpr int DEBUG = 0;
constexpr int64 P = 200003;
using Complex = complex<double>;
// Reference: http://www.prefield.com/algorithm/math/fft.html
vector<Complex> DFT(const vector<Complex> &p, int n, int sign = 1) {
const double theta = 2.0 * M_PI / n * sign;
int unit = 1;
vector<Complex> q = p;
for (int m = n; m >= 2; m >>= 1) {
int mh = m >> 1;
for (int i = 0; i < mh; i++) {
Complex w = Complex(cos(theta * unit * i), sin(theta * unit * i));
for (int j = i; j < n; j += m) {
int k = j + mh;
Complex x = q[j] - q[k];
q[j] += q[k];
q[k] = w * x;
}
}
unit *= 2;
}
int i = 0;
for (int j = 1; j < n - 1; j++) {
for (int k = n >> 1; k > (i ^= k); k >>= 1)
;
if (j < i)
swap(q[i], q[j]);
}
return q;
}
vector<Complex> InvDFT(const vector<Complex> &p, int n) {
auto q = DFT(p, n, -1);
for (int i = 0; i < n; i++) {
q[i] /= n;
}
return q;
}
vector<Complex> MultiplyFFT(const vector<Complex> &p1,
const vector<Complex> &p2) {
int d = p1.size() + p2.size() - 1;
int n = 1;
while (n < d + 1)
n *= 2;
auto q1 = p1;
q1.resize(n);
auto q2 = p2;
q2.resize(n);
auto r1 = DFT(q1, n);
auto r2 = DFT(q2, n);
vector<Complex> s(n);
for (int i = 0; i < n; i++) {
s[i] = r1[i] * r2[i];
}
return InvDFT(s, n);
}
vector<int64> MultiplyFFT(const vector<int64> &p1, const vector<int64> &p2) {
vector<Complex> q1;
for (int i = 0; i < p1.size(); i++) {
q1.emplace_back(p1[i], 0);
}
vector<Complex> q2;
for (int i = 0; i < p2.size(); i++) {
q2.emplace_back(p2[i], 0);
}
vector<Complex> r = MultiplyFFT(q1, q2);
vector<int64> s;
for (int i = 0; i < r.size(); i++) {
s.push_back(llround(r[i].real()));
}
return s;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
// i = 2^p
vector<int64> i_to_p(P, -1);
vector<int64> p_to_i(P - 1, -1);
{
int64 x = 1;
for (int i = 0; i < P - 1; i++) {
i_to_p[x] = i;
p_to_i[i] = x;
x *= 2;
x %= P;
}
}
int n;
cin >> n;
int64 delta = 0;
vector<int64> xs(P - 1);
for (int i = 0; i < n; i++) {
int64 a;
cin >> a;
if (a == 0)
continue;
xs[i_to_p[a]] += 1;
delta += a * a % P;
}
vector<int64> ys = MultiplyFFT(xs, xs);
int64 ans = 0;
for (int i = 0; i < ys.size(); i++) {
int k = i % (P - 1);
ans += p_to_i[k] * ys[i];
}
ans -= delta;
ans /= 2;
cout << ans << endl;
} | insert | 82 | 82 | 82 | 83 | TLE | |
p02590 | C++ | Runtime Error | #include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef long double ld;
// typedef tree<int,null_type,less<int
// >,rb_tree_tag,tree_order_statistics_node_update>indexed_set;
template <class T1, class T2> ostream &operator<<(ostream &os, pair<T1, T2> &p);
template <class T> ostream &operator<<(ostream &os, vector<T> &v);
template <class T> ostream &operator<<(ostream &os, set<T> &v);
template <class T1, class T2> ostream &operator<<(ostream &os, map<T1, T2> &v);
#ifdef APURBA
#define debug(a) cout << #a << " --> " << (a) << "\n";
#define debug2(a) cout << #a << " --> " << (a) << " ";
#define HERE cout << "here - " << __LINE__ << "\n";
#else
#define debug(a)
#define debug2(a)
#define HERE
#endif
const ll mod = 200003;
const int N = 1e5 + 5;
typedef pair<int, int> pii;
ll gun(ll a, ll b) {
ll x = (ll)a * b;
x %= mod;
return x;
}
ll bigmod(ll a, ll b) {
ll tt = b;
ll ans = 1;
while (b > 0) {
if (b & 1) {
ans = gun(ans, a);
}
a = gun(a, a);
b >>= 1;
}
return ans;
}
using cd = complex<double>;
const double PI = acos(-1);
void fft(vector<cd> &a, bool invert) {
ll n = a.size();
for (ll i = 1, j = 0; i < n; i++) {
ll bit = n >> 1;
for (; j & bit; bit >>= 1)
j ^= bit;
j ^= bit;
if (i < j)
swap(a[i], a[j]);
}
for (ll len = 2; len <= n; len <<= 1) {
double ang = 2 * PI / len * (invert ? -1 : 1);
cd wlen(cos(ang), sin(ang));
for (ll i = 0; i < n; i += len) {
cd w(1);
for (ll j = 0; j < len / 2; j++) {
cd u = a[i + j], v = a[i + j + len / 2] * w;
a[i + j] = u + v;
a[i + j + len / 2] = u - v;
w *= wlen;
}
}
}
if (invert) {
for (cd &x : a)
x /= n;
}
}
void multiply(vector<ll> const &a, vector<ll> const &b, vector<ll> &ans) {
vector<cd> fa(a.begin(), a.end()), fb(b.begin(), b.end());
ll n = 1;
while (n < a.size() + b.size())
n <<= 1;
fa.resize(n);
fb.resize(n);
fft(fa, false);
fft(fb, false);
for (ll i = 0; i < n; i++)
fa[i] *= fb[i];
fft(fa, true);
vector<ll> result(n);
for (ll i = 0; i < n; i++)
result[i] = round(fa[i].real());
swap(result, ans);
}
vector<ll> v(mod, 0);
map<ll, ll> mp;
void TEST_CASES(int cas) {
for (ll i = 0; i < mod; i++) {
mp[bigmod(2, i)] = i;
}
ll n;
scanf("%lld", &n);
vector<ll> a;
for (ll i = 0; i < n; i++) {
ll x;
scanf("%lld", &x);
if (x == 0)
continue;
v[mp[x]]++;
a.push_back(mp[x]);
}
if (a.size() == 0) {
printf("0\n");
return;
}
vector<ll> res;
multiply(v, v, res);
ll ans = 0;
for (ll i : a) {
if (i + i < res.size())
res[i + i]--;
}
for (ll i = 0; i < res.size(); i++) {
if (res[i]) {
assert(res[i] % 2 == 0);
ll tmp = bigmod(2, i);
tmp *= (ll)(res[i] / 2);
ans += tmp;
}
}
printf("%lld\n", ans);
}
/*
*/
int main() {
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
int t = 1, cas = 0;
// scanf("%d",&t);
while (t--) {
TEST_CASES(++cas);
}
return 0;
}
template <class T1, class T2>
ostream &operator<<(ostream &os, pair<T1, T2> &p) {
os << "{" << p.first << ", " << p.second << "} ";
return os;
}
template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
os << "[ ";
for (int i = 0; i < v.size(); i++) {
os << v[i] << " ";
}
os << " ]";
return os;
}
template <class T> ostream &operator<<(ostream &os, set<T> &v) {
os << "[ ";
for (T i : v) {
os << i << " ";
}
os << " ]";
return os;
}
template <class T1, class T2> ostream &operator<<(ostream &os, map<T1, T2> &v) {
for (auto i : v) {
os << "Key : " << i.first << " , Value : " << i.second << endl;
}
return os;
}
| #include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef long double ld;
// typedef tree<int,null_type,less<int
// >,rb_tree_tag,tree_order_statistics_node_update>indexed_set;
template <class T1, class T2> ostream &operator<<(ostream &os, pair<T1, T2> &p);
template <class T> ostream &operator<<(ostream &os, vector<T> &v);
template <class T> ostream &operator<<(ostream &os, set<T> &v);
template <class T1, class T2> ostream &operator<<(ostream &os, map<T1, T2> &v);
#ifdef APURBA
#define debug(a) cout << #a << " --> " << (a) << "\n";
#define debug2(a) cout << #a << " --> " << (a) << " ";
#define HERE cout << "here - " << __LINE__ << "\n";
#else
#define debug(a)
#define debug2(a)
#define HERE
#endif
const ll mod = 200003;
const int N = 1e5 + 5;
typedef pair<int, int> pii;
ll gun(ll a, ll b) {
ll x = (ll)a * b;
x %= mod;
return x;
}
ll bigmod(ll a, ll b) {
ll tt = b;
ll ans = 1;
while (b > 0) {
if (b & 1) {
ans = gun(ans, a);
}
a = gun(a, a);
b >>= 1;
}
return ans;
}
using cd = complex<double>;
const double PI = acos(-1);
void fft(vector<cd> &a, bool invert) {
ll n = a.size();
for (ll i = 1, j = 0; i < n; i++) {
ll bit = n >> 1;
for (; j & bit; bit >>= 1)
j ^= bit;
j ^= bit;
if (i < j)
swap(a[i], a[j]);
}
for (ll len = 2; len <= n; len <<= 1) {
double ang = 2 * PI / len * (invert ? -1 : 1);
cd wlen(cos(ang), sin(ang));
for (ll i = 0; i < n; i += len) {
cd w(1);
for (ll j = 0; j < len / 2; j++) {
cd u = a[i + j], v = a[i + j + len / 2] * w;
a[i + j] = u + v;
a[i + j + len / 2] = u - v;
w *= wlen;
}
}
}
if (invert) {
for (cd &x : a)
x /= n;
}
}
void multiply(vector<ll> const &a, vector<ll> const &b, vector<ll> &ans) {
vector<cd> fa(a.begin(), a.end()), fb(b.begin(), b.end());
ll n = 1;
while (n < a.size() + b.size())
n <<= 1;
fa.resize(n);
fb.resize(n);
fft(fa, false);
fft(fb, false);
for (ll i = 0; i < n; i++)
fa[i] *= fb[i];
fft(fa, true);
vector<ll> result(n);
for (ll i = 0; i < n; i++)
result[i] = round(fa[i].real());
swap(result, ans);
}
vector<ll> v(mod, 0);
map<ll, ll> mp;
void TEST_CASES(int cas) {
for (ll i = 0; i < mod; i++) {
mp[bigmod(2, i)] = i;
}
ll n;
scanf("%lld", &n);
vector<ll> a;
for (ll i = 0; i < n; i++) {
ll x;
scanf("%lld", &x);
if (x == 0)
continue;
v[mp[x]]++;
a.push_back(mp[x]);
}
if (a.size() == 0) {
printf("0\n");
return;
}
vector<ll> res;
multiply(v, v, res);
ll ans = 0;
for (ll i : a) {
if (i + i < res.size())
res[i + i]--;
}
for (ll i = 0; i < res.size(); i++) {
if (res[i]) {
// assert(res[i]%2==0);
ll tmp = bigmod(2, i);
tmp *= (ll)(res[i] / 2);
ans += tmp;
}
}
printf("%lld\n", ans);
}
/*
*/
int main() {
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
int t = 1, cas = 0;
// scanf("%d",&t);
while (t--) {
TEST_CASES(++cas);
}
return 0;
}
template <class T1, class T2>
ostream &operator<<(ostream &os, pair<T1, T2> &p) {
os << "{" << p.first << ", " << p.second << "} ";
return os;
}
template <class T> ostream &operator<<(ostream &os, vector<T> &v) {
os << "[ ";
for (int i = 0; i < v.size(); i++) {
os << v[i] << " ";
}
os << " ]";
return os;
}
template <class T> ostream &operator<<(ostream &os, set<T> &v) {
os << "[ ";
for (T i : v) {
os << i << " ";
}
os << " ]";
return os;
}
template <class T1, class T2> ostream &operator<<(ostream &os, map<T1, T2> &v) {
for (auto i : v) {
os << "Key : " << i.first << " , Value : " << i.second << endl;
}
return os;
}
| replace | 126 | 127 | 126 | 127 | TLE | |
p02590 | C++ | Runtime Error | /**
* @brief atcoder
* @author yao
*/
#include <algorithm>
#include <cctype>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <utility>
#define ft first
#define sd second
#ifdef DBG
#define dbg_pri(x...) fprintf(stderr, x)
#else
#define dbg_pri(x...) 0
#define NDEBUG
#endif // DBG
#include <cassert>
typedef unsigned int uint;
typedef long long int lli;
typedef unsigned long long int ulli;
#define N 524288
#define P 200003
static_assert(__builtin_popcount(N) == 1);
#define PRIME ((3u << 30) + 1)
#define OMEGA (ntt_pow(5, PRIME / N))
constexpr uint ntt_pow(uint a, uint b) {
return !b ? 1
: (ulli)ntt_pow((ulli)a * a % PRIME, b >> 1) * (b & 1 ? a : 1) %
PRIME;
}
void ntt_rec(uint *a, int n, uint w) {
if (n == 1)
return;
int n2 = n / 2;
uint w2 = ntt_pow(w, 2);
ntt_rec(a, n2, w2);
ntt_rec(a + n2, n2, w2);
uint k = 1;
for (int i = 0, j = n2; i < n2; ++i, ++j, k = (ulli)k * w % PRIME) {
uint e = a[i], o = a[j];
a[i] = (e + (ulli)o * k) % PRIME;
a[j] = (e + (ulli)PRIME * PRIME - (ulli)o * k) % PRIME;
}
}
uint ntt_bitreverse32(uint k) {
k = __builtin_bswap32(k);
k = (((k & 0xf0f0f0f0) >> 4) | ((k & 0x0f0f0f0f) << 4));
k = (((k & 0xcccccccc) >> 2) | ((k & 0x33333333) << 2));
k = (((k & 0xaaaaaaaa) >> 1) | ((k & 0x55555555) << 1));
return k;
}
void ntt(uint *a, int n = N, uint w = OMEGA) {
for (int i = 0; i < n; ++i) {
int ii = ntt_bitreverse32(i) >> __builtin_clz(n) + 1;
if (i < ii)
std::swap(a[i], a[ii]);
}
ntt_rec(a, n, w);
}
void ntt_inv(uint *a) {
ntt(a, N, ntt_pow(OMEGA, PRIME - 2));
for (int i = 0; i < N; ++i)
a[i] = (ulli)a[i] * ntt_pow(N, PRIME - 2) % PRIME;
}
int po[N], lo[N];
uint a[N], b[N];
int main() {
for (int i = 0, k = 1; i < P - 1; ++i, k = k * 2 % P)
po[i] = k, lo[k] = i;
int n;
lli ans = 0;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
int k;
scanf("%d", &k);
if (!k)
continue;
++a[lo[k]];
ans -= (lli)k * k % P;
}
for (int i = 0; i < N; ++i)
b[i] = a[i] % 13;
ntt(a);
for (int i = 0; i < N; ++i)
a[i] = (ulli)a[i] * a[i] % PRIME;
ntt_inv(a);
ntt(b);
for (int i = 0; i < N; ++i)
b[i] = (ulli)b[i] * b[i] % PRIME;
ntt_inv(b);
for (int i = 0; i < N; ++i)
if (a[i]) {
lli k = a[i];
while (k % b[i] != 0)
k += PRIME;
ans += po[i % (P - 1)] * k;
}
assert(ans % 2 == 0);
ans /= 2;
printf("%lld\n", ans);
return 0;
}
| /**
* @brief atcoder
* @author yao
*/
#include <algorithm>
#include <cctype>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <utility>
#define ft first
#define sd second
#ifdef DBG
#define dbg_pri(x...) fprintf(stderr, x)
#else
#define dbg_pri(x...) 0
#define NDEBUG
#endif // DBG
#include <cassert>
typedef unsigned int uint;
typedef long long int lli;
typedef unsigned long long int ulli;
#define N 524288
#define P 200003
static_assert(__builtin_popcount(N) == 1);
#define PRIME ((3u << 30) + 1)
#define OMEGA (ntt_pow(5, PRIME / N))
constexpr uint ntt_pow(uint a, uint b) {
return !b ? 1
: (ulli)ntt_pow((ulli)a * a % PRIME, b >> 1) * (b & 1 ? a : 1) %
PRIME;
}
void ntt_rec(uint *a, int n, uint w) {
if (n == 1)
return;
int n2 = n / 2;
uint w2 = ntt_pow(w, 2);
ntt_rec(a, n2, w2);
ntt_rec(a + n2, n2, w2);
uint k = 1;
for (int i = 0, j = n2; i < n2; ++i, ++j, k = (ulli)k * w % PRIME) {
uint e = a[i], o = a[j];
a[i] = (e + (ulli)o * k) % PRIME;
a[j] = (e + (ulli)PRIME * PRIME - (ulli)o * k) % PRIME;
}
}
uint ntt_bitreverse32(uint k) {
k = __builtin_bswap32(k);
k = (((k & 0xf0f0f0f0) >> 4) | ((k & 0x0f0f0f0f) << 4));
k = (((k & 0xcccccccc) >> 2) | ((k & 0x33333333) << 2));
k = (((k & 0xaaaaaaaa) >> 1) | ((k & 0x55555555) << 1));
return k;
}
void ntt(uint *a, int n = N, uint w = OMEGA) {
for (int i = 0; i < n; ++i) {
int ii = ntt_bitreverse32(i) >> __builtin_clz(n) + 1;
if (i < ii)
std::swap(a[i], a[ii]);
}
ntt_rec(a, n, w);
}
void ntt_inv(uint *a) {
ntt(a, N, ntt_pow(OMEGA, PRIME - 2));
for (int i = 0; i < N; ++i)
a[i] = (ulli)a[i] * ntt_pow(N, PRIME - 2) % PRIME;
}
int po[N], lo[N];
uint a[N], b[N];
int main() {
for (int i = 0, k = 1; i < P - 1; ++i, k = k * 2 % P)
po[i] = k, lo[k] = i;
int n;
lli ans = 0;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
int k;
scanf("%d", &k);
if (!k)
continue;
++a[lo[k]];
ans -= (lli)k * k % P;
}
for (int i = 0; i < N; ++i)
b[i] = a[i] % 13;
ntt(a);
for (int i = 0; i < N; ++i)
a[i] = (ulli)a[i] * a[i] % PRIME;
ntt_inv(a);
ntt(b);
for (int i = 0; i < N; ++i)
b[i] = (ulli)b[i] * b[i] % PRIME;
ntt_inv(b);
for (int i = 0; i < N; ++i) {
ans += po[i % (P - 1)] *
(a[i] + (lli)(b[i] + (PRIME + 2) - a[i]) * 6 % 13 * PRIME);
static_assert(((lli)PRIME * 6) % 13 == 1);
static_assert((PRIME + 2) % 13 == 0);
}
assert(ans % 2 == 0);
ans /= 2;
printf("%lld\n", ans);
return 0;
}
| replace | 99 | 106 | 99 | 105 | 0 | |
p02590 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define MOD @
#define ADD(X, Y) ((X) = ((X) + (Y) % MOD) % MOD)
typedef long long i64;
typedef vector<int> ivec;
typedef vector<string> svec;
const double PI = acos(-1);
struct CP {
double x, y;
CP(double x = 0, double y = 0) : x(x), y(y) {}
CP operator+(const CP &a) { return CP(x + a.x, y + a.y); }
CP operator-(const CP &a) { return CP(x - a.x, y - a.y); }
CP operator*(const CP &a) { return CP(x * a.x - y * a.y, x * a.y + y * a.x); }
};
void FFTMakeG(int n, CP g[]) {
for (int i = 0; i < n; ++i) {
double th = PI * 2.0 * i / n;
g[i] = CP(cos(th), sin(th));
}
}
void FFT(int n, CP g[], CP z[]) {
int i, j, k, l, m;
for (i = 1, j = 0; i < n; ++i) {
for (k = n >> 1; (j ^= k) < k; k >>= 1)
;
if (i < j)
swap(z[i], z[j]);
}
for (l = k = 1, m = __builtin_ctz(n); m--; l |= k <<= 1) {
for (i = 0; i < n; ++i)
if (i < (j = i ^ k)) {
CP t = z[j] * g[(i & l) << m];
z[j] = z[i] - t;
z[i] = z[i] + t;
}
}
}
const int P = 200003;
int N, A[202020];
i64 pows[202020];
map<i64, int> ord;
int cnt[202020];
CP g[1 << 19], xhi[1 << 19], xlo[1 << 19], y[1 << 19];
const int K = 330;
i64 ans[202020];
i64 naive() {
i64 ret = 0;
for (int i = 0; i < N; ++i) {
for (int j = i + 1; j < N; ++j)
ret += (A[i] * (i64)A[j]) % P;
}
return ret;
}
int main() {
scanf("%d", &N);
for (int i = 0; i < N; ++i)
scanf("%d", A + i);
i64 v = 1;
for (int i = 0; i < P - 1; ++i) {
ord[v] = i;
pows[i] = v;
v = v * 2 % P;
}
for (int i = 0; i < N; ++i) {
int ai = A[i];
if (ai == 0)
continue;
++cnt[ord[ai]];
}
FFTMakeG(1 << 19, g);
for (int i = 0; i < P - 1; ++i) {
xhi[i] = cnt[i] / K;
xlo[i] = cnt[i] % K;
}
FFT(1 << 19, g, xhi);
FFT(1 << 19, g, xlo);
for (int i = 0; i < 3; ++i) {
if (i == 0) {
for (int j = 0; j < (1 << 19); ++j)
y[j] = xhi[j] * xhi[j];
} else if (i == 1) {
for (int j = 0; j < (1 << 19); ++j)
y[j] = xhi[j] * xlo[j];
} else {
for (int j = 0; j < (1 << 19); ++j)
y[j] = xlo[j] * xlo[j];
}
reverse(g + 1, g + (1 << 19));
FFT(1 << 19, g, y);
reverse(g + 1, g + (1 << 19));
i64 waffle = 1;
if (i == 0)
waffle = K * K;
else if (i == 1)
waffle = K * 2;
for (int j = 0; j < (1 << 19); ++j) {
i64 v = (i64)(y[j].x / (1 << 19) + 0.5);
ans[j % (P - 1)] += v * waffle;
}
}
i64 ret = 0;
for (int i = 0; i < P - 1; ++i) {
ret += ans[i] * pows[i];
ret -= cnt[i] * pows[(i * 2) % (P - 1)];
}
ret /= 2;
if (ret != naive())
printf("%lld\n", naive());
printf("%lld\n", ret);
return 0;
}
| #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define MOD @
#define ADD(X, Y) ((X) = ((X) + (Y) % MOD) % MOD)
typedef long long i64;
typedef vector<int> ivec;
typedef vector<string> svec;
const double PI = acos(-1);
struct CP {
double x, y;
CP(double x = 0, double y = 0) : x(x), y(y) {}
CP operator+(const CP &a) { return CP(x + a.x, y + a.y); }
CP operator-(const CP &a) { return CP(x - a.x, y - a.y); }
CP operator*(const CP &a) { return CP(x * a.x - y * a.y, x * a.y + y * a.x); }
};
void FFTMakeG(int n, CP g[]) {
for (int i = 0; i < n; ++i) {
double th = PI * 2.0 * i / n;
g[i] = CP(cos(th), sin(th));
}
}
void FFT(int n, CP g[], CP z[]) {
int i, j, k, l, m;
for (i = 1, j = 0; i < n; ++i) {
for (k = n >> 1; (j ^= k) < k; k >>= 1)
;
if (i < j)
swap(z[i], z[j]);
}
for (l = k = 1, m = __builtin_ctz(n); m--; l |= k <<= 1) {
for (i = 0; i < n; ++i)
if (i < (j = i ^ k)) {
CP t = z[j] * g[(i & l) << m];
z[j] = z[i] - t;
z[i] = z[i] + t;
}
}
}
const int P = 200003;
int N, A[202020];
i64 pows[202020];
map<i64, int> ord;
int cnt[202020];
CP g[1 << 19], xhi[1 << 19], xlo[1 << 19], y[1 << 19];
const int K = 330;
i64 ans[202020];
i64 naive() {
i64 ret = 0;
for (int i = 0; i < N; ++i) {
for (int j = i + 1; j < N; ++j)
ret += (A[i] * (i64)A[j]) % P;
}
return ret;
}
int main() {
scanf("%d", &N);
for (int i = 0; i < N; ++i)
scanf("%d", A + i);
i64 v = 1;
for (int i = 0; i < P - 1; ++i) {
ord[v] = i;
pows[i] = v;
v = v * 2 % P;
}
for (int i = 0; i < N; ++i) {
int ai = A[i];
if (ai == 0)
continue;
++cnt[ord[ai]];
}
FFTMakeG(1 << 19, g);
for (int i = 0; i < P - 1; ++i) {
xhi[i] = cnt[i] / K;
xlo[i] = cnt[i] % K;
}
FFT(1 << 19, g, xhi);
FFT(1 << 19, g, xlo);
for (int i = 0; i < 3; ++i) {
if (i == 0) {
for (int j = 0; j < (1 << 19); ++j)
y[j] = xhi[j] * xhi[j];
} else if (i == 1) {
for (int j = 0; j < (1 << 19); ++j)
y[j] = xhi[j] * xlo[j];
} else {
for (int j = 0; j < (1 << 19); ++j)
y[j] = xlo[j] * xlo[j];
}
reverse(g + 1, g + (1 << 19));
FFT(1 << 19, g, y);
reverse(g + 1, g + (1 << 19));
i64 waffle = 1;
if (i == 0)
waffle = K * K;
else if (i == 1)
waffle = K * 2;
for (int j = 0; j < (1 << 19); ++j) {
i64 v = (i64)(y[j].x / (1 << 19) + 0.5);
ans[j % (P - 1)] += v * waffle;
}
}
i64 ret = 0;
for (int i = 0; i < P - 1; ++i) {
ret += ans[i] * pows[i];
ret -= cnt[i] * pows[(i * 2) % (P - 1)];
}
ret /= 2;
printf("%lld\n", ret);
return 0;
}
| delete | 127 | 129 | 127 | 127 | TLE | |
p02591 | C++ | Runtime Error | #include <bits/stdc++.h>
#define For(i, x, y) for (register int i = (x); i <= (y); i++)
#define FOR(i, x, y) for (register int i = (x); i < (y); i++)
#define Dow(i, x, y) for (register int i = (x); i >= (y); i--)
#define Debug(v) \
for (auto i : v) \
printf("%lld ", i); \
puts("")
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define ep emplace_back
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define fil(a, b) memset((a), (b), sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pa;
typedef pair<ll, ll> PA;
typedef vector<int> poly;
inline ll read() {
ll x = 0, f = 1;
char c = getchar();
while ((c < '0' || c > '9') && (c != '-'))
c = getchar();
if (c == '-')
f = -1, c = getchar();
while (c >= '0' && c <= '9')
x = x * 10 + c - '0', c = getchar();
return x * f;
}
const int N = 1 << 18 | 3, mod = 1e9 + 7;
int h, cnt, n, p[N];
inline int power(int a, int b) {
int ret = 1;
for (; b; b >>= 1, a = 1ll * a * a % mod)
if (b & 1)
ret = 1ll * ret * a % mod;
return ret;
}
inline int Mod(int x) { return x >= mod ? x - mod : x; }
int Tim, l[N], r[N], f[N], invf[N], inv[N];
poly v[N];
inline void dfs(int u) {
invf[u] = power(f[u], mod - 2);
if (p[u])
return l[u] = r[u] = u, void(0);
f[u << 1] = 1ll * f[u] * (u << 1) % mod;
f[u << 1 ^ 1] = 1ll * f[u] * (u << 1 ^ 1) % mod;
dfs(u << 1), dfs(u << 1 ^ 1);
l[u] = l[u << 1], r[u] = r[u << 1 ^ 1];
}
int ans, flag[N], ip[N];
inline void dfs2(int u) {
if (p[u])
return v[u].pb(p[u] + cnt - 1), void(0);
dfs2(u << 1), dfs2(u << 1 ^ 1);
int l = 0, L = 0;
while (l < siz(v[u << 1]) || L < siz(v[u << 1 ^ 1]))
if (l == siz(v[u << 1]) || v[u << 1 ^ 1][L] < v[u << 1][l])
v[u].pb(v[u << 1 ^ 1][L]), ++L;
else
v[u].pb(v[u << 1][l]), l++;
// printf(" %d:\n",u);
// for (auto i:v[u]) printf("%d ",i);puts("");
for (auto i : v[u << 1]) {
int tmp = 1ll * f[ip[i]] * invf[u] % mod * u % mod;
for (int now = i; now; now >>= 1) {
tmp = 1ll * tmp * now % mod, flag[now] = Mod(flag[now] + tmp);
}
}
for (auto i : v[u << 1 ^ 1]) {
int tmp = 1ll * f[ip[i]] * invf[u] % mod, tmp2 = i;
for (int las = i, now = i >> 1; now; las = now, now >>= 1) {
tmp2 = 1ll * tmp2 * now % mod;
if (now * 2 == las) {
ans = (ans + 1ll * flag[now * 2 + 1] * tmp % mod * tmp2) % mod;
} else {
ans = (ans + 1ll * flag[now * 2] * tmp % mod * tmp2) % mod;
}
}
}
for (auto i : v[u << 1])
for (int now = i; now; now >>= 1)
flag[now] = 0;
}
int main() {
h = read(), cnt = 1 << (h - 1), n = cnt * 2 - 1;
For(i, 1, cnt) p[cnt + i - 1] = read(),
ip[p[cnt + i - 1] + cnt - 1] = cnt + i - 1;
For(i, 1, n) inv[i] = power(i, mod - 2);
f[1] = invf[1] = 1, dfs(1), dfs2(1), printf("%d\n", ans);
} | #include <bits/stdc++.h>
#define For(i, x, y) for (register int i = (x); i <= (y); i++)
#define FOR(i, x, y) for (register int i = (x); i < (y); i++)
#define Dow(i, x, y) for (register int i = (x); i >= (y); i--)
#define Debug(v) \
for (auto i : v) \
printf("%lld ", i); \
puts("")
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define ep emplace_back
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define fil(a, b) memset((a), (b), sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pa;
typedef pair<ll, ll> PA;
typedef vector<int> poly;
inline ll read() {
ll x = 0, f = 1;
char c = getchar();
while ((c < '0' || c > '9') && (c != '-'))
c = getchar();
if (c == '-')
f = -1, c = getchar();
while (c >= '0' && c <= '9')
x = x * 10 + c - '0', c = getchar();
return x * f;
}
const int N = 1 << 18 | 3, mod = 1e9 + 7;
int h, cnt, n, p[N];
inline int power(int a, int b) {
int ret = 1;
for (; b; b >>= 1, a = 1ll * a * a % mod)
if (b & 1)
ret = 1ll * ret * a % mod;
return ret;
}
inline int Mod(int x) { return x >= mod ? x - mod : x; }
int Tim, l[N], r[N], f[N], invf[N], inv[N];
poly v[N];
inline void dfs(int u) {
invf[u] = power(f[u], mod - 2);
if (p[u])
return l[u] = r[u] = u, void(0);
f[u << 1] = 1ll * f[u] * (u << 1) % mod;
f[u << 1 ^ 1] = 1ll * f[u] * (u << 1 ^ 1) % mod;
dfs(u << 1), dfs(u << 1 ^ 1);
l[u] = l[u << 1], r[u] = r[u << 1 ^ 1];
}
int ans, flag[N], ip[N];
inline void dfs2(int u) {
if (p[u])
return v[u].pb(p[u] + cnt - 1), void(0);
dfs2(u << 1), dfs2(u << 1 ^ 1);
v[u] = v[u << 1];
for (auto i : v[u << 1 ^ 1])
v[u].pb(i);
// printf(" %d:\n",u);
// for (auto i:v[u]) printf("%d ",i);puts("");
for (auto i : v[u << 1]) {
int tmp = 1ll * f[ip[i]] * invf[u] % mod * u % mod;
for (int now = i; now; now >>= 1) {
tmp = 1ll * tmp * now % mod, flag[now] = Mod(flag[now] + tmp);
}
}
for (auto i : v[u << 1 ^ 1]) {
int tmp = 1ll * f[ip[i]] * invf[u] % mod, tmp2 = i;
for (int las = i, now = i >> 1; now; las = now, now >>= 1) {
tmp2 = 1ll * tmp2 * now % mod;
if (now * 2 == las) {
ans = (ans + 1ll * flag[now * 2 + 1] * tmp % mod * tmp2) % mod;
} else {
ans = (ans + 1ll * flag[now * 2] * tmp % mod * tmp2) % mod;
}
}
}
for (auto i : v[u << 1])
for (int now = i; now; now >>= 1)
flag[now] = 0;
}
int main() {
h = read(), cnt = 1 << (h - 1), n = cnt * 2 - 1;
For(i, 1, cnt) p[cnt + i - 1] = read(),
ip[p[cnt + i - 1] + cnt - 1] = cnt + i - 1;
For(i, 1, n) inv[i] = power(i, mod - 2);
f[1] = invf[1] = 1, dfs(1), dfs2(1), printf("%d\n", ans);
} | replace | 62 | 68 | 62 | 65 | 0 | |
p02591 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
const int MX = 1 << 18, md = 1000000007;
unordered_map<int, int> here[18];
bool old[18];
int a[MX];
int main() {
int h = 18;
ignore = scanf("%d", &h);
int n = 1 << (h - 1);
for (int i = 0; i < n; i++)
ignore = scanf("%d", a + i);
// srand(42);
// iota(a, a + n, 1);
// random_shuffle(a, a + n);
int ans = 0;
for (int i = 0; i < n; i++) {
for (int u = n + i, p = 1, d = 0; u != 1; d++, u /= 2) {
p = p * 1LL * u % md;
if (u % 2 == 0) {
if (old[d]) {
here[d].clear();
old[d] = false;
}
for (int v = n + a[i] - 1, q = p; v != 1; v /= 2) {
q = q * 1LL * v % md;
int &val = here[d][v];
val += q;
if (val >= md)
val -= md;
}
} else {
old[d] = true;
for (int v = n + a[i] - 1, q = p; v != 1; v /= 2) {
q = q * 1LL * v % md;
auto it = here[d].find(v ^ 1);
if (it != here[d].end()) {
int there = it->second;
ans += q * 1LL * there % md * (v / 2) % md * (u / 2) % md;
if (ans >= md)
ans -= md;
}
}
}
}
}
printf("%d\n", ans);
return 0;
}
| #pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
const int MX = 1 << 18, md = 1000000007;
unordered_map<int, int> here[18];
bool old[18];
int a[MX];
int main() {
int h = 18;
ignore = scanf("%d", &h);
int n = 1 << (h - 1);
for (int i = 0; i < n; i++)
ignore = scanf("%d", a + i);
// srand(42);
// iota(a, a + n, 1);
// random_shuffle(a, a + n);
int ans = 0;
for (int i = 0; i < n; i++) {
for (int u = n + i, p = 1, d = 0; u != 1; d++, u /= 2) {
p = p * 1LL * u % md;
if (u % 2 == 0) {
if (old[d]) {
here[d].clear();
old[d] = false;
}
for (int v = n + a[i] - 1, q = p; v != 1; v /= 2) {
q = q * 1LL * v % md;
int &val = here[d][v];
val += q;
if (val >= md)
val -= md;
}
} else {
old[d] = true;
for (int v = n + a[i] - 1, q = p; v != 1; v /= 2) {
q = q * 1LL * v % md;
auto it = here[d].find(v ^ 1);
if (it != here[d].end()) {
int there = it->second;
ans += q * 1LL * there % md * (v / 2) % md * (u / 2) % md;
if (ans >= md)
ans -= md;
}
}
}
}
}
printf("%d\n", ans);
return 0;
}
| insert | 0 | 0 | 0 | 4 | TLE | |
p02591 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int const p = 1e9 + 7;
int c[1000005], w[1000005], lg2[100005];
int mod(int x) { return x >= p ? x - p : x; }
int main() {
int h, ans = 0;
scanf("%d", &h);
for (int i = (1 << (h - 1)); i < (1 << h); i++)
scanf("%d", &c[i]), c[i] += (1 << (h - 1)) - 1;
for (int i = 2; i < (1 << (h - 1)); i++)
lg2[i] = lg2[i >> 1] + 1;
for (int i = 1; i < (1 << (h - 1)); i++) {
int t = lg2[i] + 1;
for (int j = 0; j < (1 << (h - t - 1)); j++) {
int x = (i << (h - t)) + j, now = x, prod = i;
while (now != i)
prod = 1ll * prod * now % p, now >>= 1;
now = c[x];
while (now)
prod = 1ll * prod * now % p, w[now] = mod(w[now] + prod), now >>= 1;
}
for (int j = (1 << (h - t - 1)); j < (1 << (h - t)); j++) {
int x = (i << (h - t)) + j, now = x, prod = 1, pre = 0;
while (now != i)
prod = 1ll * prod * now % p, now >>= 1;
now = c[x];
while (now) {
ans = mod(ans + 1ll * (w[now] - 1ll * w[pre] * now % p + p) * prod % p);
prod = 1ll * prod * now % p, pre = now, now >>= 1;
}
}
for (int j = 0; j < (1 << (h - t - 1)); j++) {
int now = c[(i << (h - t)) + j];
while (now)
w[now] = 0, now >>= 1;
}
}
printf("%d", ans);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int const p = 1e9 + 7;
int c[1000005], w[1000005], lg2[1000005];
int mod(int x) { return x >= p ? x - p : x; }
int main() {
int h, ans = 0;
scanf("%d", &h);
for (int i = (1 << (h - 1)); i < (1 << h); i++)
scanf("%d", &c[i]), c[i] += (1 << (h - 1)) - 1;
for (int i = 2; i < (1 << (h - 1)); i++)
lg2[i] = lg2[i >> 1] + 1;
for (int i = 1; i < (1 << (h - 1)); i++) {
int t = lg2[i] + 1;
for (int j = 0; j < (1 << (h - t - 1)); j++) {
int x = (i << (h - t)) + j, now = x, prod = i;
while (now != i)
prod = 1ll * prod * now % p, now >>= 1;
now = c[x];
while (now)
prod = 1ll * prod * now % p, w[now] = mod(w[now] + prod), now >>= 1;
}
for (int j = (1 << (h - t - 1)); j < (1 << (h - t)); j++) {
int x = (i << (h - t)) + j, now = x, prod = 1, pre = 0;
while (now != i)
prod = 1ll * prod * now % p, now >>= 1;
now = c[x];
while (now) {
ans = mod(ans + 1ll * (w[now] - 1ll * w[pre] * now % p + p) * prod % p);
prod = 1ll * prod * now % p, pre = now, now >>= 1;
}
}
for (int j = 0; j < (1 << (h - t - 1)); j++) {
int now = c[(i << (h - t)) + j];
while (now)
w[now] = 0, now >>= 1;
}
}
printf("%d", ans);
return 0;
} | replace | 3 | 4 | 3 | 4 | TLE | |
p02591 | C++ | Time Limit Exceeded | /**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author aajisaka
*/
#include <bits/stdc++.h>
using namespace std;
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
#define SPEED \
ios_base::sync_with_stdio(false); \
cin.tie(nullptr)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
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;
}
using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;
constexpr long double PI = 3.14159265358979323846264338327950288L;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
constexpr ll mod17 = 1e9 + 7;
constexpr ll mod19 = 1e9 + 9;
constexpr ll mod9 = 998244353;
ll mod = mod17;
// Mod int libraries
template <typename T> T mod_pow(T a, ll x) {
T res = 1;
while (x > 0) {
if (x & 1)
res *= a;
a *= a;
x >>= 1;
}
return res;
}
// Mint32
unordered_map<ll, ll> minvmap;
ll minv(ll a, ll m) {
auto k = a;
auto p = minvmap[a];
if (p != 0)
return p;
ll b = m, u = 1, v = 0;
while (b) {
ll t = a / b;
swap(a -= t * b, b);
swap(u -= t * v, v);
}
p = (u % m + m) % m;
minvmap[k] = p;
return p;
}
struct mint {
ll x;
mint() : x(0) {}
mint(ll x) : x((x % mod + mod) % mod) {}
mint &fix() {
x = (x % mod + mod) % mod;
return *this;
}
mint operator-() const { return mint(0) - *this; }
mint &operator+=(const mint &a) {
if ((x += a.x) >= mod)
x -= mod;
return *this;
}
mint &operator-=(const mint &a) {
if ((x += mod - a.x) >= mod)
x -= mod;
return *this;
}
mint &operator*=(const mint &a) {
(x *= a.x) %= mod;
return *this;
}
mint &operator/=(const mint &a) {
(x *= minv(a.x, mod)) %= mod;
return *this;
}
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; }
};
// Mint64
struct mint64 {
ull x;
static ull mod, inv, r2;
mint64() : x(0) {}
mint64(ull x) : x(init(x)) {}
static ull init(ull x) { return reduce(__uint128_t(x) * r2); }
static void set_mod(ull m) {
mod = inv = m;
for (int i = 0; i < 5; i++)
inv *= 2 - inv * m;
r2 = -__uint128_t(m) % m;
}
static ull reduce(__uint128_t x) {
ull y = ull(x >> 64) - ull((__uint128_t(ull(x) * inv) * mod) >> 64);
return ll(y) < 0 ? y + mod : y;
}
mint64 &operator+=(mint64 &a) {
x += a.x - mod;
if (ll(x) < 0)
x += mod;
return *this;
}
mint64 operator+(mint64 &a) const { return mint64(*this) += a; }
mint64 &operator*=(mint64 &a) {
x = reduce(__uint128_t(x) * a.x);
return *this;
}
mint64 operator*(mint64 &a) const { return mint64(*this) *= a; }
};
ull mint64::mod, mint64::inv, mint64::r2;
// Verify: https://yukicoder.me/submissions/472580
struct Miller {
const vector<ull> v = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
bool suspect(ull a, ull s, ull d, ull n) {
if (mint64::mod != n)
mint64::set_mod(n);
mint64 one = 1, minusone = n - 1, ma = a;
auto x = mod_pow(ma, d);
if (x.x == one.x)
return true;
for (int r = 0; r < s; r++) {
if (x.x == minusone.x)
return true;
x = x * x;
}
return false;
}
// check if n is prime
bool check(ull n) {
if (n < 2 || (n > 2 && n % 2 == 0))
return false;
ull d = n - 1;
ull s = 0;
while (!(d & 1)) {
d >>= 1;
s++;
}
for (auto a : v) {
if (a >= n)
break;
if (!suspect(a, s, d, n))
return false;
}
return true;
}
};
class combination {
// factorial
public:
std::vector<mint> fact;
public:
std::vector<mint> inv;
combination(int n) {
fact.resize(n + 1);
inv.resize(n + 1);
fact[0] = 1;
for (int i = 1; i <= n; i++) {
fact[i] = fact[i - 1] * i;
}
inv[n] = mint(1) / fact[n];
for (int i = n - 1; i >= 0; i--) {
inv[i] = inv[i + 1] * (i + 1);
}
}
// nCr
public:
mint get(int n, int r) {
if (n < r || n < 0 || r < 0)
return 0;
return fact[n] * inv[r] * inv[n - r];
}
// nPr
public:
mint p(int n, int r) {
if (n < r || n < 0)
return 0;
return fact[n] * inv[n - r];
}
};
class DTwinBinaryTrees {
public:
mint ret = 0;
vector<mint> dp;
vector<int> perm;
vector<int> clear;
int l, n;
void mark(int p, mint now) {
now *= p;
if (p < n) {
mark(p * 2, now);
mark(p * 2 + 1, now);
} else {
int b = perm[p - n] + n;
mark2(b, now);
clear.push_back(b);
}
}
void mark2(int p, mint now) {
if (p == 1)
return;
now *= p;
dp[p] += now;
mark2(p / 2, now);
}
void calc(int p, mint now) {
now *= p;
if (p < n) {
calc(p * 2, now);
calc(p * 2 + 1, now);
} else {
int b = perm[p - n] + n;
calc2(b, now);
}
}
void calc2(int p, mint now) {
if (p == 1)
return;
now *= p;
ret += now * (p / 2) * dp[p ^ 1];
calc2(p / 2, now);
}
void solve(istream &cin, ostream &cout) {
SPEED;
int h;
cin >> h;
l = 1 << h;
n = l / 2;
dp.resize(l);
perm.resize(n);
rep(i, n) {
cin >> perm[i];
perm[i]--;
}
for (int i = 1; i < n; i++) {
mark(i * 2, i);
calc(i * 2 + 1, 1);
for (auto e : clear) {
for (int x = e; x > 0; x /= 2) {
dp[x] = 0;
}
}
}
cout << ret.x << endl;
}
};
signed main() {
DTwinBinaryTrees solver;
std::istream &in(std::cin);
std::ostream &out(std::cout);
solver.solve(in, out);
return 0;
} | /**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author aajisaka
*/
#include <bits/stdc++.h>
using namespace std;
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
#define SPEED \
ios_base::sync_with_stdio(false); \
cin.tie(nullptr)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
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;
}
using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;
constexpr long double PI = 3.14159265358979323846264338327950288L;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
constexpr ll mod17 = 1e9 + 7;
constexpr ll mod19 = 1e9 + 9;
constexpr ll mod9 = 998244353;
ll mod = mod17;
// Mod int libraries
template <typename T> T mod_pow(T a, ll x) {
T res = 1;
while (x > 0) {
if (x & 1)
res *= a;
a *= a;
x >>= 1;
}
return res;
}
// Mint32
unordered_map<ll, ll> minvmap;
ll minv(ll a, ll m) {
auto k = a;
auto p = minvmap[a];
if (p != 0)
return p;
ll b = m, u = 1, v = 0;
while (b) {
ll t = a / b;
swap(a -= t * b, b);
swap(u -= t * v, v);
}
p = (u % m + m) % m;
minvmap[k] = p;
return p;
}
struct mint {
ll x;
mint() : x(0) {}
mint(ll x) : x((x % mod + mod) % mod) {}
mint &fix() {
x = (x % mod + mod) % mod;
return *this;
}
mint operator-() const { return mint(0) - *this; }
mint &operator+=(const mint &a) {
if ((x += a.x) >= mod)
x -= mod;
return *this;
}
mint &operator-=(const mint &a) {
if ((x += mod - a.x) >= mod)
x -= mod;
return *this;
}
mint &operator*=(const mint &a) {
(x *= a.x) %= mod;
return *this;
}
mint &operator/=(const mint &a) {
(x *= minv(a.x, mod)) %= mod;
return *this;
}
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; }
};
// Mint64
struct mint64 {
ull x;
static ull mod, inv, r2;
mint64() : x(0) {}
mint64(ull x) : x(init(x)) {}
static ull init(ull x) { return reduce(__uint128_t(x) * r2); }
static void set_mod(ull m) {
mod = inv = m;
for (int i = 0; i < 5; i++)
inv *= 2 - inv * m;
r2 = -__uint128_t(m) % m;
}
static ull reduce(__uint128_t x) {
ull y = ull(x >> 64) - ull((__uint128_t(ull(x) * inv) * mod) >> 64);
return ll(y) < 0 ? y + mod : y;
}
mint64 &operator+=(mint64 &a) {
x += a.x - mod;
if (ll(x) < 0)
x += mod;
return *this;
}
mint64 operator+(mint64 &a) const { return mint64(*this) += a; }
mint64 &operator*=(mint64 &a) {
x = reduce(__uint128_t(x) * a.x);
return *this;
}
mint64 operator*(mint64 &a) const { return mint64(*this) *= a; }
};
ull mint64::mod, mint64::inv, mint64::r2;
// Verify: https://yukicoder.me/submissions/472580
struct Miller {
const vector<ull> v = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
bool suspect(ull a, ull s, ull d, ull n) {
if (mint64::mod != n)
mint64::set_mod(n);
mint64 one = 1, minusone = n - 1, ma = a;
auto x = mod_pow(ma, d);
if (x.x == one.x)
return true;
for (int r = 0; r < s; r++) {
if (x.x == minusone.x)
return true;
x = x * x;
}
return false;
}
// check if n is prime
bool check(ull n) {
if (n < 2 || (n > 2 && n % 2 == 0))
return false;
ull d = n - 1;
ull s = 0;
while (!(d & 1)) {
d >>= 1;
s++;
}
for (auto a : v) {
if (a >= n)
break;
if (!suspect(a, s, d, n))
return false;
}
return true;
}
};
class combination {
// factorial
public:
std::vector<mint> fact;
public:
std::vector<mint> inv;
combination(int n) {
fact.resize(n + 1);
inv.resize(n + 1);
fact[0] = 1;
for (int i = 1; i <= n; i++) {
fact[i] = fact[i - 1] * i;
}
inv[n] = mint(1) / fact[n];
for (int i = n - 1; i >= 0; i--) {
inv[i] = inv[i + 1] * (i + 1);
}
}
// nCr
public:
mint get(int n, int r) {
if (n < r || n < 0 || r < 0)
return 0;
return fact[n] * inv[r] * inv[n - r];
}
// nPr
public:
mint p(int n, int r) {
if (n < r || n < 0)
return 0;
return fact[n] * inv[n - r];
}
};
class DTwinBinaryTrees {
public:
mint ret = 0;
vector<mint> dp;
vector<int> perm;
vector<int> clear;
int l, n;
void mark(int p, mint now) {
now *= p;
if (p < n) {
mark(p * 2, now);
mark(p * 2 + 1, now);
} else {
int b = perm[p - n] + n;
mark2(b, now);
clear.push_back(b);
}
}
void mark2(int p, mint now) {
if (p == 1)
return;
now *= p;
dp[p] += now;
mark2(p / 2, now);
}
void calc(int p, mint now) {
now *= p;
if (p < n) {
calc(p * 2, now);
calc(p * 2 + 1, now);
} else {
int b = perm[p - n] + n;
calc2(b, now);
}
}
void calc2(int p, mint now) {
if (p == 1)
return;
now *= p;
ret += now * (p / 2) * dp[p ^ 1];
calc2(p / 2, now);
}
void solve(istream &cin, ostream &cout) {
SPEED;
int h;
cin >> h;
l = 1 << h;
n = l / 2;
dp.resize(l);
perm.resize(n);
rep(i, n) {
cin >> perm[i];
perm[i]--;
}
for (int i = 1; i < n; i++) {
mark(i * 2, i);
calc(i * 2 + 1, 1);
for (auto e : clear) {
for (int x = e; x > 0; x /= 2) {
dp[x] = 0;
}
}
clear.clear();
}
cout << ret.x << endl;
}
};
signed main() {
DTwinBinaryTrees solver;
std::istream &in(std::cin);
std::ostream &out(std::cout);
solver.solve(in, out);
return 0;
} | insert | 292 | 292 | 292 | 293 | TLE | |
p02591 | C++ | Runtime Error | #include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
using namespace std;
const int maxn = 234567;
const int M = 1000000007;
typedef long long ll;
typedef pair<int, ll> pi;
void add(ll &x, ll y) {
x += y;
if (x >= M)
x -= M;
}
int n, h, p[maxn];
vector<pi> v[2];
ll ans, cnt[2][maxn];
void ins(int x, ll val, int o) {
while (x) {
val = val * x % M;
add(cnt[o][x], val);
x >>= 1;
}
}
ll dfs2(int x) {
if (!cnt[0][x] || !cnt[1][x])
return 0;
ll ret =
(cnt[0][x * 2] * cnt[1][x * 2 + 1] + cnt[1][x * 2] * cnt[0][x * 2 + 1]) %
M * x % M;
add(ret, dfs2(x * 2));
add(ret, dfs2(x * 2 + 1));
return ret;
}
void dfs(int x, ll val, int o) {
val = val * x % M;
if (x >= n) {
v[o].pb((pi){p[x - n] + n, val});
return;
}
dfs(x * 2, val, o);
dfs(x * 2 + 1, val, o);
}
ll solve(int x) {
v[0].clear();
v[1].clear();
dfs(x * 2, 1, 0);
dfs(x * 2 + 1, 1, 1);
for (auto x : v[0])
ins(x.F, x.S, 0);
for (auto x : v[1])
ins(x.F, x.S, 1);
ll ret = dfs2(1);
for (auto x : v[0])
ins(x.F, M - x.S, 0);
for (auto x : v[1])
ins(x.F, M - x.S, 1);
return ret;
}
int main() {
cin >> h;
n = 1 << (h - 1);
for (int i = 0; i < n; i++) {
cin >> p[i];
p[i]--;
}
for (int i = 1; i < n; i++)
ans = (ans + solve(i) * i) % M;
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
using namespace std;
const int maxn = 434567;
const int M = 1000000007;
typedef long long ll;
typedef pair<int, ll> pi;
void add(ll &x, ll y) {
x += y;
if (x >= M)
x -= M;
}
int n, h, p[maxn];
vector<pi> v[2];
ll ans, cnt[2][maxn];
void ins(int x, ll val, int o) {
while (x) {
val = val * x % M;
add(cnt[o][x], val);
x >>= 1;
}
}
ll dfs2(int x) {
if (!cnt[0][x] || !cnt[1][x])
return 0;
ll ret =
(cnt[0][x * 2] * cnt[1][x * 2 + 1] + cnt[1][x * 2] * cnt[0][x * 2 + 1]) %
M * x % M;
add(ret, dfs2(x * 2));
add(ret, dfs2(x * 2 + 1));
return ret;
}
void dfs(int x, ll val, int o) {
val = val * x % M;
if (x >= n) {
v[o].pb((pi){p[x - n] + n, val});
return;
}
dfs(x * 2, val, o);
dfs(x * 2 + 1, val, o);
}
ll solve(int x) {
v[0].clear();
v[1].clear();
dfs(x * 2, 1, 0);
dfs(x * 2 + 1, 1, 1);
for (auto x : v[0])
ins(x.F, x.S, 0);
for (auto x : v[1])
ins(x.F, x.S, 1);
ll ret = dfs2(1);
for (auto x : v[0])
ins(x.F, M - x.S, 0);
for (auto x : v[1])
ins(x.F, M - x.S, 1);
return ret;
}
int main() {
cin >> h;
n = 1 << (h - 1);
for (int i = 0; i < n; i++) {
cin >> p[i];
p[i]--;
}
for (int i = 1; i < n; i++)
ans = (ans + solve(i) * i) % M;
cout << ans << endl;
return 0;
}
| replace | 5 | 6 | 5 | 6 | 0 | |
p02591 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define SZ(x) ((int)x.size())
#define ALL(x) x.begin(), x.end()
#define L(i, u) for (register int i = head[u]; i; i = nxt[i])
#define rep(i, a, b) for (register int i = (a); i <= (b); i++)
#define per(i, a, b) for (register int i = (a); i >= (b); i--)
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
typedef vector<int> Vi;
template <class T> inline void read(T &x) {
x = 0;
char c = getchar();
int f = 1;
while (!isdigit(c)) {
if (c == '-')
f = -1;
c = getchar();
}
while (isdigit(c)) {
x = x * 10 + c - '0';
c = getchar();
}
x *= f;
}
template <class T> T gcd(T a, T b) { return !b ? a : gcd(b, a % b); }
template <class T> inline void umin(T &x, T y) { x = x < y ? x : y; }
template <class T> inline void umax(T &x, T y) { x = x > y ? x : y; }
mt19937 R(chrono::system_clock().now().time_since_epoch().count());
const int N = 1 << 20 | 3, mo = 1e9 + 7;
inline void add(int &x, int y) { x = x + y < mo ? x + y : x + y - mo; }
inline void sub(int &x, int y) { x = x - y >= 0 ? x - y : x - y + mo; }
inline int power(int a, int n) {
int res = 1;
while (n) {
if (n & 1)
res = 1LL * res * a % mo;
a = 1LL * a * a % mo;
n >>= 1;
}
return res;
}
int h, p[N], q[N], dy[N], rev[N], val[N], ival[N], inv[N], le[N], ri[N];
inline int lowbit(int x) { return x & -x; }
inline int getlca(int x, int y) { return x >> (h - dy[lowbit(rev[x ^ y])]); }
inline int calc(int x, int y) {
int z = getlca(x, y);
return 1ll * val[x] * val[y] % mo * ival[z] % mo * ival[z >> 1] % mo;
}
int xs[N], zhi[N][8], tot, res;
struct node {
int id, l, r, pos, xs;
};
vector<node> que[N];
int v[N], bas;
inline void ins(int p, int x) {
assert(p <= (1 << h - 1) && p);
while (p <= (1 << h - 1))
add(v[p], x), p += p & -p;
}
int qry(int p) {
assert(p <= (1 << h - 1));
int r = 0;
while (p)
add(r, v[p]), p -= p & -p;
return r;
}
int debug(int l1, int r1, int l2, int r2) {
int res = 0;
rep(i, l1, r1) if (p[i - bas] + bas >= l2 && p[i - bas] + bas <= r2) res =
(res + 1ll * val[i] * val[p[i - bas] + bas]) % mo;
return res;
}
int main() {
freopen("1.in", "r", stdin);
read(h);
rep(i, 1, 1 << h - 1) read(p[i]), q[p[i]] = i;
int B = h >> 1;
bas = (1 << h - 1) - 1;
rep(i, 0, (1 << h) - 1) rep(j, 0, h - 1) if (i >> j & 1) rev[i] |=
1 << h - 1 - j;
rep(i, 0, h - 1) dy[1 << i] = i;
val[0] = 1;
val[1] = 1;
rep(i, 2, (1 << h) - 1) val[i] = 1ll * val[i >> 1] * i % mo;
rep(i, 0, (1 << h) - 1) inv[i] = power(i, mo - 2),
ival[i] = power(val[i], mo - 2);
assert(ival[0] == 1 && ival[1] == 1);
rep(i, 1 << h - 1,
(1 << h) - 1) for (int j = i + 1;
j < (1 << h) && getlca(i, j) >= (1 << B); j++) {
res = (res + 1ll * calc(i, j) * calc(p[i - bas] + bas, p[j - bas] + bas)) %
mo;
// printf("%d %d : %d %d
//%d\n",i,j,calc(i,j),calc(p[i-bas]+bas,p[j-bas]+bas),calc(i,j)*calc(p[i]+bas,p[j]+bas));
}
rep(i, 1 << h - 1,
(1 << h) - 1) for (int j = i + 1;
j < (1 << h) && getlca(i, j) >= (1 << B);
j++) if (getlca(q[i - bas] + bas, q[j - bas] + bas) <
(1 << B)) res =
(res + 1ll * calc(i, j) * calc(q[i - bas] + bas, q[j - bas] + bas)) % mo;
/*rep(i,1<<h-1,(1<<h)-1)for(int
j=i+1;j<(1<<h);j++)if(getlca(i,j)>=(1<<B)||getlca(p[i-bas]+bas,p[j-bas]+bas)>=(1<<B)){
res=(res+1ll*calc(i,j)*calc(p[i-bas]+bas,p[j-bas]+bas))%mo;
}*/
memset(le, 0x3f, sizeof(le));
rep(i, 1 << h - 1, (1 << h) - 1) for (int j = i; j; j >>= 1) {
umax(ri[j], i);
umin(le[j], i);
}
rep(x, 1, (1 << B) - 1)
rep(y, 1, (1 << B) - 1) if ((x << 1) < (1 << h) && (y << 1) < (1 << h)) {
xs[++tot] = (1ll * ival[x] * ival[x >> 1] % mo) *
(1ll * ival[y] * ival[y >> 1] % mo) % mo;
que[le[x * 2] - 1].pb((node){tot, le[y * 2], ri[y * 2], 0, -1});
que[ri[x * 2]].pb((node){tot, le[y * 2], ri[y * 2], 0, 1});
que[le[x * 2 + 1] - 1].pb((node){tot, le[y * 2 + 1], ri[y * 2 + 1], 1, -1});
que[ri[x * 2 + 1]].pb((node){tot, le[y * 2 + 1], ri[y * 2 + 1], 1, 1});
que[le[x * 2] - 1].pb((node){tot, le[y * 2 + 1], ri[y * 2 + 1], 2, -1});
que[ri[x * 2]].pb((node){tot, le[y * 2 + 1], ri[y * 2 + 1], 2, 1});
que[le[x * 2 + 1] - 1].pb((node){tot, le[y * 2], ri[y * 2], 3, -1});
que[ri[x * 2 + 1]].pb((node){tot, le[y * 2], ri[y * 2], 3, 1});
}
rep(i, 1 << h - 1, (1 << h) - 1) {
ins(p[i - bas], 1ll * val[i] * val[p[i - bas] + bas] % mo);
// cerr<<1ll*val[i]*val[p[i-bas]+bas]%mo<<endl;
for (auto q : que[i])
(zhi[q.id][q.pos] +=
1ll * q.xs * (qry(q.r - bas) - qry(q.l - bas - 1))) %= mo;
}
// cerr<<debug(le[2],ri[2],le[2],ri[2])<<endl;
// cerr<<debug(le[3],ri[3],le[3],ri[3])<<endl;
// cerr<<debug(le[2],ri[2],le[3],ri[3])<<endl;
// cerr<<debug(le[3],ri[3],le[2],ri[2])<<endl;
rep(i, 1, tot) {
// rep(j,0,7)
res = (res +
1ll * (1ll * zhi[i][0] * zhi[i][1] + 1ll * zhi[i][2] * zhi[i][3]) %
mo * xs[i]) %
mo;
// printf("%d:%d %d %d %d
//%d\n",i,zhi[i][0],zhi[i][1],zhi[i][2],zhi[i][3],xs[i]);
}
res = (res + mo) % mo;
cout << res << endl;
// cerr<<getlca(4,7)<<endl;
// rep(i,4,7)rep(j,i+1,7)printf("%d %d:%d %d\n",i,j,getlca(i,j),calc(i,j));
// cerr<<getlca(4,5)<<endl;
// cerr<<getlca(8,9)<<endl;
return 0;
}
| #include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define SZ(x) ((int)x.size())
#define ALL(x) x.begin(), x.end()
#define L(i, u) for (register int i = head[u]; i; i = nxt[i])
#define rep(i, a, b) for (register int i = (a); i <= (b); i++)
#define per(i, a, b) for (register int i = (a); i >= (b); i--)
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> Pii;
typedef pair<ll, ll> Pll;
typedef vector<int> Vi;
template <class T> inline void read(T &x) {
x = 0;
char c = getchar();
int f = 1;
while (!isdigit(c)) {
if (c == '-')
f = -1;
c = getchar();
}
while (isdigit(c)) {
x = x * 10 + c - '0';
c = getchar();
}
x *= f;
}
template <class T> T gcd(T a, T b) { return !b ? a : gcd(b, a % b); }
template <class T> inline void umin(T &x, T y) { x = x < y ? x : y; }
template <class T> inline void umax(T &x, T y) { x = x > y ? x : y; }
mt19937 R(chrono::system_clock().now().time_since_epoch().count());
const int N = 1 << 20 | 3, mo = 1e9 + 7;
inline void add(int &x, int y) { x = x + y < mo ? x + y : x + y - mo; }
inline void sub(int &x, int y) { x = x - y >= 0 ? x - y : x - y + mo; }
inline int power(int a, int n) {
int res = 1;
while (n) {
if (n & 1)
res = 1LL * res * a % mo;
a = 1LL * a * a % mo;
n >>= 1;
}
return res;
}
int h, p[N], q[N], dy[N], rev[N], val[N], ival[N], inv[N], le[N], ri[N];
inline int lowbit(int x) { return x & -x; }
inline int getlca(int x, int y) { return x >> (h - dy[lowbit(rev[x ^ y])]); }
inline int calc(int x, int y) {
int z = getlca(x, y);
return 1ll * val[x] * val[y] % mo * ival[z] % mo * ival[z >> 1] % mo;
}
int xs[N], zhi[N][8], tot, res;
struct node {
int id, l, r, pos, xs;
};
vector<node> que[N];
int v[N], bas;
inline void ins(int p, int x) {
assert(p <= (1 << h - 1) && p);
while (p <= (1 << h - 1))
add(v[p], x), p += p & -p;
}
int qry(int p) {
assert(p <= (1 << h - 1));
int r = 0;
while (p)
add(r, v[p]), p -= p & -p;
return r;
}
int debug(int l1, int r1, int l2, int r2) {
int res = 0;
rep(i, l1, r1) if (p[i - bas] + bas >= l2 && p[i - bas] + bas <= r2) res =
(res + 1ll * val[i] * val[p[i - bas] + bas]) % mo;
return res;
}
int main() {
read(h);
rep(i, 1, 1 << h - 1) read(p[i]), q[p[i]] = i;
int B = h >> 1;
bas = (1 << h - 1) - 1;
rep(i, 0, (1 << h) - 1) rep(j, 0, h - 1) if (i >> j & 1) rev[i] |=
1 << h - 1 - j;
rep(i, 0, h - 1) dy[1 << i] = i;
val[0] = 1;
val[1] = 1;
rep(i, 2, (1 << h) - 1) val[i] = 1ll * val[i >> 1] * i % mo;
rep(i, 0, (1 << h) - 1) inv[i] = power(i, mo - 2),
ival[i] = power(val[i], mo - 2);
assert(ival[0] == 1 && ival[1] == 1);
rep(i, 1 << h - 1,
(1 << h) - 1) for (int j = i + 1;
j < (1 << h) && getlca(i, j) >= (1 << B); j++) {
res = (res + 1ll * calc(i, j) * calc(p[i - bas] + bas, p[j - bas] + bas)) %
mo;
// printf("%d %d : %d %d
//%d\n",i,j,calc(i,j),calc(p[i-bas]+bas,p[j-bas]+bas),calc(i,j)*calc(p[i]+bas,p[j]+bas));
}
rep(i, 1 << h - 1,
(1 << h) - 1) for (int j = i + 1;
j < (1 << h) && getlca(i, j) >= (1 << B);
j++) if (getlca(q[i - bas] + bas, q[j - bas] + bas) <
(1 << B)) res =
(res + 1ll * calc(i, j) * calc(q[i - bas] + bas, q[j - bas] + bas)) % mo;
/*rep(i,1<<h-1,(1<<h)-1)for(int
j=i+1;j<(1<<h);j++)if(getlca(i,j)>=(1<<B)||getlca(p[i-bas]+bas,p[j-bas]+bas)>=(1<<B)){
res=(res+1ll*calc(i,j)*calc(p[i-bas]+bas,p[j-bas]+bas))%mo;
}*/
memset(le, 0x3f, sizeof(le));
rep(i, 1 << h - 1, (1 << h) - 1) for (int j = i; j; j >>= 1) {
umax(ri[j], i);
umin(le[j], i);
}
rep(x, 1, (1 << B) - 1)
rep(y, 1, (1 << B) - 1) if ((x << 1) < (1 << h) && (y << 1) < (1 << h)) {
xs[++tot] = (1ll * ival[x] * ival[x >> 1] % mo) *
(1ll * ival[y] * ival[y >> 1] % mo) % mo;
que[le[x * 2] - 1].pb((node){tot, le[y * 2], ri[y * 2], 0, -1});
que[ri[x * 2]].pb((node){tot, le[y * 2], ri[y * 2], 0, 1});
que[le[x * 2 + 1] - 1].pb((node){tot, le[y * 2 + 1], ri[y * 2 + 1], 1, -1});
que[ri[x * 2 + 1]].pb((node){tot, le[y * 2 + 1], ri[y * 2 + 1], 1, 1});
que[le[x * 2] - 1].pb((node){tot, le[y * 2 + 1], ri[y * 2 + 1], 2, -1});
que[ri[x * 2]].pb((node){tot, le[y * 2 + 1], ri[y * 2 + 1], 2, 1});
que[le[x * 2 + 1] - 1].pb((node){tot, le[y * 2], ri[y * 2], 3, -1});
que[ri[x * 2 + 1]].pb((node){tot, le[y * 2], ri[y * 2], 3, 1});
}
rep(i, 1 << h - 1, (1 << h) - 1) {
ins(p[i - bas], 1ll * val[i] * val[p[i - bas] + bas] % mo);
// cerr<<1ll*val[i]*val[p[i-bas]+bas]%mo<<endl;
for (auto q : que[i])
(zhi[q.id][q.pos] +=
1ll * q.xs * (qry(q.r - bas) - qry(q.l - bas - 1))) %= mo;
}
// cerr<<debug(le[2],ri[2],le[2],ri[2])<<endl;
// cerr<<debug(le[3],ri[3],le[3],ri[3])<<endl;
// cerr<<debug(le[2],ri[2],le[3],ri[3])<<endl;
// cerr<<debug(le[3],ri[3],le[2],ri[2])<<endl;
rep(i, 1, tot) {
// rep(j,0,7)
res = (res +
1ll * (1ll * zhi[i][0] * zhi[i][1] + 1ll * zhi[i][2] * zhi[i][3]) %
mo * xs[i]) %
mo;
// printf("%d:%d %d %d %d
//%d\n",i,zhi[i][0],zhi[i][1],zhi[i][2],zhi[i][3],xs[i]);
}
res = (res + mo) % mo;
cout << res << endl;
// cerr<<getlca(4,7)<<endl;
// rep(i,4,7)rep(j,i+1,7)printf("%d %d:%d %d\n",i,j,getlca(i,j),calc(i,j));
// cerr<<getlca(4,5)<<endl;
// cerr<<getlca(8,9)<<endl;
return 0;
}
| delete | 82 | 83 | 82 | 82 | TLE | |
p02593 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long
#define db long double
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define all(a) a.begin(), a.end()
using namespace std;
struct PermutationTree {
static const int N = 200007;
#define ll long long
#define ii pair<ll, ll>
#define iii pair<ii, ll>
#define fi first
#define se second
#define rep(x, start, end) \
for (auto x = (start) - ((start) > (end)); x != (end) - ((start) > (end)); \
((start) < (end) ? x++ : x--))
struct node {
int s, e, m;
ll val = 0, lazy = 0;
node *l, *r;
node(int _s, int _e) {
s = _s, e = _e, m = s + e >> 1;
if (s != e) {
l = new node(s, m);
r = new node(m + 1, e);
}
}
void propo() {
if (lazy) {
val += lazy;
if (s != e) {
l->lazy += lazy;
r->lazy += lazy;
}
lazy = 0;
}
}
void update(int i, int j, ll k) {
if (s == i && e == j)
lazy += k;
else {
if (j <= m)
l->update(i, j, k);
else if (m < i)
r->update(i, j, k);
else
l->update(i, m, k), r->update(m + 1, j, k);
l->propo(), r->propo();
val = min(l->val, r->val);
}
}
ll query(int i, int j) {
propo();
if (s == i && e == j)
return val;
else if (j <= m)
return l->query(i, j);
else if (m < i)
return r->query(i, j);
else
return min(l->query(i, m), r->query(m + 1, j));
}
};
int n, q;
int arr[N];
ii range[N];
ii span[N];
vector<int> children[N];
int parent[N];
int typ[N];
int idx; // new index to assign to nodes
ii get_range(ii i, ii j) { return ii(min(i.fi, j.fi), max(i.se, j.se)); }
void add_edge(int u, int v) { // u is parent of v
parent[v] = u;
children[u].push_back(v);
}
bool adj(int i, int j) { return range[i].se == range[j].fi - 1; }
int length(int i) { return range[i].se - range[i].fi + 1; }
void build(vector<int> v) {
n = v.size();
for (int i = 0; i < n; ++i)
arr[i] = v[i];
idx = n;
memset(parent, -1, sizeof(parent));
node *root = new node(0, N);
vector<int> mx = {-1}, mn = {-1}; // stacks for max and min
vector<int> nodes; // stack of cut and join nodes
rep(x, 0, n) {
// update Q values
while (mx.back() != -1 && arr[mx.back()] < arr[x]) {
int temp = mx.back();
mx.pop_back();
root->update(mx.back() + 1, temp, arr[x] - arr[temp]);
}
mx.push_back(x);
while (mn.back() != -1 && arr[mn.back()] > arr[x]) {
int temp = mn.back();
mn.pop_back();
root->update(mn.back() + 1, temp, arr[temp] - arr[x]);
}
mn.push_back(x);
// handle stack updates
range[x] = ii(arr[x], arr[x]);
span[x] = ii(x, x);
int curr = x;
while (true) {
if (!nodes.empty() &&
(adj(nodes.back(), curr) || adj(curr, nodes.back()))) {
if ((adj(nodes.back(), curr) && typ[nodes.back()] == 1) ||
(adj(curr, nodes.back()) && typ[nodes.back()] == 2)) {
add_edge(nodes.back(), curr);
range[nodes.back()] = get_range(range[nodes.back()], range[curr]);
span[nodes.back()] = get_range(span[nodes.back()], span[curr]);
curr = nodes.back();
nodes.pop_back();
} else { // make a new join node
typ[idx] = (adj(nodes.back(), curr) ? 1 : 2);
add_edge(idx, nodes.back());
add_edge(idx, curr);
range[idx] = get_range(range[nodes.back()], range[curr]);
span[idx] = get_range(span[nodes.back()], span[curr]);
nodes.pop_back();
curr = idx++;
}
} else if (x - (length(curr) - 1) &&
root->query(0, x - length(curr)) == 0) {
int len = length(curr);
ii r = range[curr];
ii s = span[curr];
add_edge(idx, curr);
do {
len += length(nodes.back());
r = get_range(r, range[nodes.back()]);
s = get_range(s, span[nodes.back()]);
add_edge(idx, nodes.back());
nodes.pop_back();
} while (r.se - r.fi + 1 != len);
reverse(all(children[idx]));
range[idx] = r;
span[idx] = s;
curr = idx++;
} else {
break;
}
}
nodes.push_back(curr);
root->update(0, x, -1);
}
}
pair<int, int> get_range(int x) { return range[x]; }
pair<int, int> get_seg(int x) { return span[x]; }
vector<int> get_sons(int x) { return children[x]; }
bool if_cut(int x) {
auto var = get_sons(x);
vector<pair<int, int>> tet;
for (auto x : var)
tet.pb(range[x]);
auto ctet = tet;
sort(all(tet));
if (tet == ctet)
return true;
reverse(all(tet));
if (tet == ctet)
return true;
return false;
}
int get_root() {
int cur = 0;
while (parent[cur] != -1) {
cur = parent[cur];
}
return cur;
}
int get_parent(int x) { return parent[x]; }
};
PermutationTree tree;
vector<pair<int, int>> arr;
map<pair<int, int>, int> tet;
vector<ll> ans;
int get_dist(pair<int, int> a, pair<int, int> b) {
return abs(a.x - b.x) + abs(a.y - b.y);
}
void solve(int root, ll x, ll y) {
auto res = tree.get_seg(root);
// cout << res.x << " " << res.y << " " << x << " " << y << endl;
if (res.x == res.y) {
ans[tet[arr[res.x]]] = min(x, y);
return;
}
auto children = tree.get_sons(root);
if (!tree.if_cut(root)) {
for (auto x : children) {
solve(x, 0, 0);
}
return;
}
int sz = children.size();
vector<int> take_right(sz), take_left(sz);
vector<int> cnt_left(sz), cnt_right(sz);
for (int i = 0; i < sz; ++i) {
auto cur = tree.get_seg(children[i]);
if (cur.x != cur.y)
take_left[i] = 0, cnt_left[i] = 0;
else {
take_left[i] = 1;
cnt_left[i] = cur.y - cur.x + 1;
if (i > 0) {
take_left[i] += take_left[i - 1];
cnt_left[i] += cnt_left[i - 1];
}
}
}
reverse(all(children));
for (int i = 0; i < sz; ++i) {
auto cur = tree.get_seg(children[i]);
if (cur.x != cur.y) {
take_right[i] = 0;
cnt_right[i] = 0;
} else {
take_right[i] = 1;
cnt_right[i] = cur.y - cur.x + 1;
if (i > 0) {
take_right[i] += take_right[i - 1];
cnt_right[i] += cnt_right[i - 1];
}
}
}
reverse(all(children));
reverse(all(take_right));
reverse(all(cnt_right));
// cout << res.x << " " << res.y << " " << x << " " << y << endl;
for (int i = 0; i < children.size(); ++i) {
int L = tree.get_seg(children[i]).x;
int R = tree.get_seg(children[i]).y;
ll a = 1e18, b = 1e18;
int tl = 0, tr = 0, cl = 0, cr = 0;
if (i > 0) {
cl = cnt_left[i - 1];
tl = take_left[i - 1];
}
if (i + 1 < children.size()) {
cr = cnt_right[i + 1];
tr = take_right[i + 1];
}
ll left_sign = 0, right_sign = 0;
if (cr + cl + 1 == children.size())
left_sign = x, right_sign = y;
int e = L - tl, f = R + tr;
if (cl == 0)
e = R;
if (cr == 0)
f = L;
a = min({a,
right_sign + get_dist(arr[L], arr[L - tl]) +
get_dist(arr[L - tl], arr[R + tr]) - tl - tr,
left_sign + get_dist(arr[L], arr[f]) +
get_dist(arr[L - tl], arr[f]) - tl - tr});
b = min({b,
right_sign + get_dist(arr[R], arr[e]) +
get_dist(arr[e], arr[R + tr]) - tl - tr,
left_sign + get_dist(arr[R], arr[R + tr]) +
get_dist(arr[L - tl], arr[R + tr]) - tl - tr});
// cout << a << " " << b << " " << L << " " << R << " " << tl << " " << tr
// << endl;
solve(children[i], a, b);
}
}
int main() {
#ifdef LOCAL
freopen("K_input.txt", "r", stdin);
// freopen("K_output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
ans.assign(n, -1);
vector<int> ys;
for (int i = 0; i < n; ++i) {
int x, y;
cin >> x >> y;
ys.pb(y);
arr.pb(mp(x, y));
tet[mp(x, y)] = i;
}
sort(all(arr));
sort(all(ys));
map<int, int> tet;
for (int i = 0; i < ys.size(); ++i)
tet[ys[i]] = i;
vector<int> v(n);
for (int i = 0; i < n; ++i) {
v[i] = tet[arr[i].y];
// cout << v[i] << " ";
}
tree.build(v);
int root = tree.get_root();
solve(root, 0, 0);
for (auto x : ans) {
cout << x << '\n';
}
}
| #include <bits/stdc++.h>
#define ll long long
#define db long double
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define all(a) a.begin(), a.end()
using namespace std;
struct PermutationTree {
static const int N = 400007;
#define ll long long
#define ii pair<ll, ll>
#define iii pair<ii, ll>
#define fi first
#define se second
#define rep(x, start, end) \
for (auto x = (start) - ((start) > (end)); x != (end) - ((start) > (end)); \
((start) < (end) ? x++ : x--))
struct node {
int s, e, m;
ll val = 0, lazy = 0;
node *l, *r;
node(int _s, int _e) {
s = _s, e = _e, m = s + e >> 1;
if (s != e) {
l = new node(s, m);
r = new node(m + 1, e);
}
}
void propo() {
if (lazy) {
val += lazy;
if (s != e) {
l->lazy += lazy;
r->lazy += lazy;
}
lazy = 0;
}
}
void update(int i, int j, ll k) {
if (s == i && e == j)
lazy += k;
else {
if (j <= m)
l->update(i, j, k);
else if (m < i)
r->update(i, j, k);
else
l->update(i, m, k), r->update(m + 1, j, k);
l->propo(), r->propo();
val = min(l->val, r->val);
}
}
ll query(int i, int j) {
propo();
if (s == i && e == j)
return val;
else if (j <= m)
return l->query(i, j);
else if (m < i)
return r->query(i, j);
else
return min(l->query(i, m), r->query(m + 1, j));
}
};
int n, q;
int arr[N];
ii range[N];
ii span[N];
vector<int> children[N];
int parent[N];
int typ[N];
int idx; // new index to assign to nodes
ii get_range(ii i, ii j) { return ii(min(i.fi, j.fi), max(i.se, j.se)); }
void add_edge(int u, int v) { // u is parent of v
parent[v] = u;
children[u].push_back(v);
}
bool adj(int i, int j) { return range[i].se == range[j].fi - 1; }
int length(int i) { return range[i].se - range[i].fi + 1; }
void build(vector<int> v) {
n = v.size();
for (int i = 0; i < n; ++i)
arr[i] = v[i];
idx = n;
memset(parent, -1, sizeof(parent));
node *root = new node(0, N);
vector<int> mx = {-1}, mn = {-1}; // stacks for max and min
vector<int> nodes; // stack of cut and join nodes
rep(x, 0, n) {
// update Q values
while (mx.back() != -1 && arr[mx.back()] < arr[x]) {
int temp = mx.back();
mx.pop_back();
root->update(mx.back() + 1, temp, arr[x] - arr[temp]);
}
mx.push_back(x);
while (mn.back() != -1 && arr[mn.back()] > arr[x]) {
int temp = mn.back();
mn.pop_back();
root->update(mn.back() + 1, temp, arr[temp] - arr[x]);
}
mn.push_back(x);
// handle stack updates
range[x] = ii(arr[x], arr[x]);
span[x] = ii(x, x);
int curr = x;
while (true) {
if (!nodes.empty() &&
(adj(nodes.back(), curr) || adj(curr, nodes.back()))) {
if ((adj(nodes.back(), curr) && typ[nodes.back()] == 1) ||
(adj(curr, nodes.back()) && typ[nodes.back()] == 2)) {
add_edge(nodes.back(), curr);
range[nodes.back()] = get_range(range[nodes.back()], range[curr]);
span[nodes.back()] = get_range(span[nodes.back()], span[curr]);
curr = nodes.back();
nodes.pop_back();
} else { // make a new join node
typ[idx] = (adj(nodes.back(), curr) ? 1 : 2);
add_edge(idx, nodes.back());
add_edge(idx, curr);
range[idx] = get_range(range[nodes.back()], range[curr]);
span[idx] = get_range(span[nodes.back()], span[curr]);
nodes.pop_back();
curr = idx++;
}
} else if (x - (length(curr) - 1) &&
root->query(0, x - length(curr)) == 0) {
int len = length(curr);
ii r = range[curr];
ii s = span[curr];
add_edge(idx, curr);
do {
len += length(nodes.back());
r = get_range(r, range[nodes.back()]);
s = get_range(s, span[nodes.back()]);
add_edge(idx, nodes.back());
nodes.pop_back();
} while (r.se - r.fi + 1 != len);
reverse(all(children[idx]));
range[idx] = r;
span[idx] = s;
curr = idx++;
} else {
break;
}
}
nodes.push_back(curr);
root->update(0, x, -1);
}
}
pair<int, int> get_range(int x) { return range[x]; }
pair<int, int> get_seg(int x) { return span[x]; }
vector<int> get_sons(int x) { return children[x]; }
bool if_cut(int x) {
auto var = get_sons(x);
vector<pair<int, int>> tet;
for (auto x : var)
tet.pb(range[x]);
auto ctet = tet;
sort(all(tet));
if (tet == ctet)
return true;
reverse(all(tet));
if (tet == ctet)
return true;
return false;
}
int get_root() {
int cur = 0;
while (parent[cur] != -1) {
cur = parent[cur];
}
return cur;
}
int get_parent(int x) { return parent[x]; }
};
PermutationTree tree;
vector<pair<int, int>> arr;
map<pair<int, int>, int> tet;
vector<ll> ans;
int get_dist(pair<int, int> a, pair<int, int> b) {
return abs(a.x - b.x) + abs(a.y - b.y);
}
void solve(int root, ll x, ll y) {
auto res = tree.get_seg(root);
// cout << res.x << " " << res.y << " " << x << " " << y << endl;
if (res.x == res.y) {
ans[tet[arr[res.x]]] = min(x, y);
return;
}
auto children = tree.get_sons(root);
if (!tree.if_cut(root)) {
for (auto x : children) {
solve(x, 0, 0);
}
return;
}
int sz = children.size();
vector<int> take_right(sz), take_left(sz);
vector<int> cnt_left(sz), cnt_right(sz);
for (int i = 0; i < sz; ++i) {
auto cur = tree.get_seg(children[i]);
if (cur.x != cur.y)
take_left[i] = 0, cnt_left[i] = 0;
else {
take_left[i] = 1;
cnt_left[i] = cur.y - cur.x + 1;
if (i > 0) {
take_left[i] += take_left[i - 1];
cnt_left[i] += cnt_left[i - 1];
}
}
}
reverse(all(children));
for (int i = 0; i < sz; ++i) {
auto cur = tree.get_seg(children[i]);
if (cur.x != cur.y) {
take_right[i] = 0;
cnt_right[i] = 0;
} else {
take_right[i] = 1;
cnt_right[i] = cur.y - cur.x + 1;
if (i > 0) {
take_right[i] += take_right[i - 1];
cnt_right[i] += cnt_right[i - 1];
}
}
}
reverse(all(children));
reverse(all(take_right));
reverse(all(cnt_right));
// cout << res.x << " " << res.y << " " << x << " " << y << endl;
for (int i = 0; i < children.size(); ++i) {
int L = tree.get_seg(children[i]).x;
int R = tree.get_seg(children[i]).y;
ll a = 1e18, b = 1e18;
int tl = 0, tr = 0, cl = 0, cr = 0;
if (i > 0) {
cl = cnt_left[i - 1];
tl = take_left[i - 1];
}
if (i + 1 < children.size()) {
cr = cnt_right[i + 1];
tr = take_right[i + 1];
}
ll left_sign = 0, right_sign = 0;
if (cr + cl + 1 == children.size())
left_sign = x, right_sign = y;
int e = L - tl, f = R + tr;
if (cl == 0)
e = R;
if (cr == 0)
f = L;
a = min({a,
right_sign + get_dist(arr[L], arr[L - tl]) +
get_dist(arr[L - tl], arr[R + tr]) - tl - tr,
left_sign + get_dist(arr[L], arr[f]) +
get_dist(arr[L - tl], arr[f]) - tl - tr});
b = min({b,
right_sign + get_dist(arr[R], arr[e]) +
get_dist(arr[e], arr[R + tr]) - tl - tr,
left_sign + get_dist(arr[R], arr[R + tr]) +
get_dist(arr[L - tl], arr[R + tr]) - tl - tr});
// cout << a << " " << b << " " << L << " " << R << " " << tl << " " << tr
// << endl;
solve(children[i], a, b);
}
}
int main() {
#ifdef LOCAL
freopen("K_input.txt", "r", stdin);
// freopen("K_output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
ans.assign(n, -1);
vector<int> ys;
for (int i = 0; i < n; ++i) {
int x, y;
cin >> x >> y;
ys.pb(y);
arr.pb(mp(x, y));
tet[mp(x, y)] = i;
}
sort(all(arr));
sort(all(ys));
map<int, int> tet;
for (int i = 0; i < ys.size(); ++i)
tet[ys[i]] = i;
vector<int> v(n);
for (int i = 0; i < n; ++i) {
v[i] = tet[arr[i].y];
// cout << v[i] << " ";
}
tree.build(v);
int root = tree.get_root();
solve(root, 0, 0);
for (auto x : ans) {
cout << x << '\n';
}
}
| replace | 13 | 14 | 13 | 14 | 0 | |
p02594 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int x;
cin >> x;
string ret;
if (x >= 30) {
ret = "Yes";
} else {
ret = "No";
}
cout << ret << endl;
return true;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int x;
cin >> x;
string ret;
if (x >= 30) {
ret = "Yes";
} else {
ret = "No";
}
cout << ret << endl;
} | delete | 15 | 16 | 15 | 15 | 1 | |
p02594 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
while (1) {
cin >> n;
if (n >= 30) {
cout << "Yes" << endl;
break;
} else
cout << "No" << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
if (n >= 30)
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
| replace | 4 | 12 | 4 | 10 | TLE | |
p02594 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define sl set<ll>
#define vs vector<string>
#define vl vector<ll>
#define vi vector<int>
#define vpl vector<pair<ll, ll>>
#define pb push_back
#define PI 3.14159265358979323846
#define ll long long
#define ld long double
#define endl "\n"
#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define repp(i, a, b) for (ll i = (a); i >= (b); i--)
#define rop(i, a, b) for (ll i = (a); i > (b); i--)
#define min3(a, b, c) min(c, min(a, b))
#define min4(a, b, c, d) min(d, min(c, min(a, b)))
#define tt \
long long t; \
cin >> t; \
while (t--)
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0);
const int maxn = 1e5 + 100;
int main() {
tt {
ll w;
cin >> w;
map<int, int> mp;
for (int i = 0; i < w; i++) {
string s;
cin >> s;
ll p = s.length();
rep(i, 0, p) { mp[s[i] - 'a']++; }
}
ll flag = 0;
rep(i, 0, 26) {
if (mp[i] % w) {
cout << "NO";
flag = 1;
break;
}
}
if (flag == 0) {
cout << "YES";
}
cout << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define sl set<ll>
#define vs vector<string>
#define vl vector<ll>
#define vi vector<int>
#define vpl vector<pair<ll, ll>>
#define pb push_back
#define PI 3.14159265358979323846
#define ll long long
#define ld long double
#define endl "\n"
#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define repp(i, a, b) for (ll i = (a); i >= (b); i--)
#define rop(i, a, b) for (ll i = (a); i > (b); i--)
#define min3(a, b, c) min(c, min(a, b))
#define min4(a, b, c, d) min(d, min(c, min(a, b)))
#define tt \
long long t; \
cin >> t; \
while (t--)
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0);
const int maxn = 1e5 + 100;
int main() {
ll n;
cin >> n;
if (n >= 30)
cout << "Yes";
else
cout << "No";
} | replace | 28 | 53 | 28 | 34 | TLE | |
p02594 | C++ | Memory Limit Exceeded | // #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, n) for (ll i = (s); i < (ll)(n); i++)
#define all(x) (x).begin(), (x).end()
#define in(x, l, r) (ll)(l) <= (x) && (x) < (ll)(r)
int main() {
vector<int> v(300000000, 0);
int X;
cin >> X;
if (X >= 30) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
| // #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, n) for (ll i = (s); i < (ll)(n); i++)
#define all(x) (x).begin(), (x).end()
#define in(x, l, r) (ll)(l) <= (x) && (x) < (ll)(r)
int main() {
vector<int> v(250000000, 0);
int X;
cin >> X;
if (X >= 30) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
| replace | 9 | 10 | 9 | 10 | MLE | |
p02594 | Python | Runtime Error | x = input()
if x >= 30:
print("Yes")
else:
print("No")
| x = int(input())
if x >= 30:
print("Yes")
else:
print("No")
| replace | 0 | 1 | 0 | 1 | TypeError: '>=' not supported between instances of 'str' and 'int' | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02594/Python/s522477833.py", line 3, in <module>
if x >= 30:
TypeError: '>=' not supported between instances of 'str' and 'int'
|
p02594 | Python | Runtime Error | X = int(input().split())
if X >= 30:
print("Yes")
else:
print("No")
| X = int(input())
if X >= 30:
print("Yes")
else:
print("No")
| replace | 0 | 1 | 0 | 1 | TypeError: int() argument must be a string, a bytes-like object or a real number, not 'list' | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02594/Python/s217813658.py", line 1, in <module>
X = int(input().split())
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'list'
|
p02594 | Python | Runtime Error | x = input()
if x >= 30:
print("Yes")
else:
print("No")
| x = int(input())
if x >= 30:
print("Yes")
else:
print("No")
| replace | 0 | 1 | 0 | 1 | TypeError: '>=' not supported between instances of 'str' and 'int' | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02594/Python/s624570905.py", line 2, in <module>
if x >= 30:
TypeError: '>=' not supported between instances of 'str' and 'int'
|
p02594 | Python | Runtime Error | x = input()
if x >= 30:
print("Yes")
else:
print("No")
| x = input()
if int(x) >= 30:
print("Yes")
else:
print("No")
| replace | 1 | 2 | 1 | 2 | TypeError: '>=' not supported between instances of 'str' and 'int' | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02594/Python/s511585247.py", line 2, in <module>
if x >= 30:
TypeError: '>=' not supported between instances of 'str' and 'int'
|
p02594 | C++ | Runtime Error | #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, a, n) for (ll i = a; i < n; i++)
#define per(i, n, a) for (ll i = n - 1; i >= a; i--)
#define nl "\n"
#define LLMAX 1e18
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define pll pair<ll, ll>
#define fastread \
cin.tie(0); \
cout.tie(0); \
ios::sync_with_stdio(false);
#define online_judge
const ll MOD = 1e9 + 7;
ll pow(ll n, ll e) {
if (e == 0)
return 1;
else if (e == 1)
return n;
else if (e % 2 == 0)
return pow(n, e / 2);
else
return pow(n, e / 2) * n;
}
bool prime(ll n) {
ll ct = 0;
rep(i, 2, sqrt(n) + 1) {
if (n % i == 0)
return false;
}
return true;
}
int solve() {
fastread;
ll n, m, temp, mi, q, ma = 0, t;
cin >> n;
(n >= 30) ? cout << "Yes" : cout << "No";
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
clock_t start, end;
start = clock();
solve();
end = clock();
double time_taken = double(end - start) / double(CLOCKS_PER_SEC);
cerr << "Time taken by program is : " << fixed << time_taken
<< setprecision(5);
cerr << " sec " << endl;
return 0;
} | #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, a, n) for (ll i = a; i < n; i++)
#define per(i, n, a) for (ll i = n - 1; i >= a; i--)
#define nl "\n"
#define LLMAX 1e18
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define pll pair<ll, ll>
#define fastread \
cin.tie(0); \
cout.tie(0); \
ios::sync_with_stdio(false);
#define online_judge
const ll MOD = 1e9 + 7;
ll pow(ll n, ll e) {
if (e == 0)
return 1;
else if (e == 1)
return n;
else if (e % 2 == 0)
return pow(n, e / 2);
else
return pow(n, e / 2) * n;
}
bool prime(ll n) {
ll ct = 0;
rep(i, 2, sqrt(n) + 1) {
if (n % i == 0)
return false;
}
return true;
}
void solve() {
fastread;
ll n, m, temp, mi, q, ma = 0, t;
cin >> n;
(n >= 30) ? cout << "Yes" : cout << "No";
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
clock_t start, end;
start = clock();
solve();
end = clock();
double time_taken = double(end - start) / double(CLOCKS_PER_SEC);
cerr << "Time taken by program is : " << fixed << time_taken
<< setprecision(5);
cerr << " sec " << endl;
return 0;
} | replace | 43 | 44 | 43 | 44 | 0 | |
p02595 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for (ll i = 0; i < ll(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= ll(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= ll(b); i--)
#define FORA(i, I) for (const auto &i : I)
#define ALL(x) x.begin(), x.end()
#define SIZE(x) ll(x.size())
int main(void) {
long double N, D;
cin >> N >> D;
long double x[100000] = {0};
long double y[100000] = {0};
REP(i, N) {
cin >> x[i];
cin >> y[i];
}
int ans = 0;
REP(i, N) {
// if(x[i]*x[i] + y[i]*y[i] <= D*D){
if (sqrt(x[i] * x[i] + y[i] * y[i]) <= D) {
ans++;
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for (ll i = 0; i < ll(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = a; i <= ll(b); i++)
#define FORD(i, a, b) for (ll i = a; i >= ll(b); i--)
#define FORA(i, I) for (const auto &i : I)
#define ALL(x) x.begin(), x.end()
#define SIZE(x) ll(x.size())
int main(void) {
long double N, D;
cin >> N >> D;
long double x[1000000] = {0};
long double y[10000000] = {0};
REP(i, N) {
cin >> x[i];
cin >> y[i];
}
int ans = 0;
REP(i, N) {
// if(x[i]*x[i] + y[i]*y[i] <= D*D){
if (sqrt(x[i] * x[i] + y[i] * y[i]) <= D) {
ans++;
}
}
cout << ans << endl;
return 0;
}
| replace | 16 | 18 | 16 | 18 | 0 | |
p02595 | C++ | Runtime Error | #include <iostream>
#include <math.h>
using namespace std;
int main() {
int N, count = 0;
double X[20000], Y[20000], D, x;
cin >> N;
cin >> D;
for (int i = 0; i < N; i++) {
cin >> X[i];
cin >> Y[i];
x = X[i] * X[i] + Y[i] * Y[i];
x = sqrt(x);
if (D >= x)
count++;
}
cout << count;
return 0;
} | #include <iostream>
#include <math.h>
using namespace std;
int main() {
int N, count = 0;
static double X[200000], Y[200000];
double D, x;
cin >> N;
cin >> D;
for (int i = 0; i < N; i++) {
cin >> X[i];
cin >> Y[i];
x = X[i] * X[i] + Y[i] * Y[i];
x = sqrt(x);
if (D >= x)
count++;
}
cout << count;
return 0;
} | replace | 5 | 6 | 5 | 7 | 0 | |
p02595 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long int
int32_t main() {
int n, d;
cin >> n >> d;
int count = 0;
for (int i = 1; i <= n; i++) {
int x, y;
scanf("%d %d");
if (x * x + y * y <= d * d)
count++;
}
cout << count << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long int
int32_t main() {
int n, d;
cin >> n >> d;
int count = 0;
for (int i = 1; i <= n; i++) {
int x, y;
scanf("%lld %lld", &x, &y);
if (x * x + y * y <= d * d)
count++;
}
cout << count << endl;
} | replace | 14 | 15 | 14 | 15 | -11 | |
p02595 | C++ | Runtime Error | #include <cmath>
#include <iostream>
#define L 20005
using namespace std;
double x[L], y[L];
double dist(double x, double y) {
double s = (double)(x * x) + (double)(y * y);
return sqrt(s);
}
int main() {
int N, D;
cin >> N >> D;
int cnt = 0;
for (int i = 0; i < N; i++)
cin >> x[i] >> y[i];
for (int i = 0; i < N; i++) {
if (dist(x[i], y[i]) <= (double)(D))
cnt++;
}
cout << cnt << "\n";
return 0;
} | #include <cmath>
#include <iostream>
#define L 200005
using namespace std;
double x[L], y[L];
double dist(double x, double y) {
double s = (double)(x * x) + (double)(y * y);
return sqrt(s);
}
int main() {
int N, D;
cin >> N >> D;
int cnt = 0;
for (int i = 0; i < N; i++)
cin >> x[i] >> y[i];
for (int i = 0; i < N; i++) {
if (dist(x[i], y[i]) <= (double)(D))
cnt++;
}
cout << cnt << "\n";
return 0;
} | replace | 3 | 4 | 3 | 4 | 0 | |
p02595 | C++ | Runtime Error | #include <string.h>
#include <algorithm>
#include <cmath>
#include <cstdlib> // abs() for integer
#include <iostream>
using namespace std;
int main(void) {
int N, D;
int X[200], Y[200];
cin >> N >> D;
for (int i = 0; i < N; i++)
cin >> X[i] >> Y[i];
int cnt = 0;
for (int i = 0; i < N; i++) {
long long dist_2 = (long long)X[i] * X[i] + (long long)Y[i] * Y[i];
// cout << dist_2 << endl;
if (dist_2 <= (long long)D * D) {
cnt++;
}
}
cout << cnt << endl;
// cout<<template<<endl;
return 0;
} | #include <string.h>
#include <algorithm>
#include <cmath>
#include <cstdlib> // abs() for integer
#include <iostream>
using namespace std;
int main(void) {
int N, D;
int X[200000], Y[200000];
cin >> N >> D;
for (int i = 0; i < N; i++)
cin >> X[i] >> Y[i];
int cnt = 0;
for (int i = 0; i < N; i++) {
long long dist_2 = (long long)X[i] * X[i] + (long long)Y[i] * Y[i];
// cout << dist_2 << endl;
if (dist_2 <= (long long)D * D) {
cnt++;
}
}
cout << cnt << endl;
// cout<<template<<endl;
return 0;
} | replace | 11 | 12 | 11 | 12 | 0 | |
p02595 | C++ | Runtime Error | #include <stdio.h>
typedef long long ll;
int main() {
int n, ans = 0;
ll d, x, y;
scanf("%d%lld", &n, &d);
d = d * d;
while (n--) {
scanf("%lld%lld", &x, y);
if (x * x + y * y <= d)
ans++;
}
printf("%lld\n", ans);
return 0;
} | #include <stdio.h>
typedef long long ll;
int main() {
int n, ans = 0;
ll d, x, y;
scanf("%d%lld", &n, &d);
d = d * d;
while (n--) {
scanf("%lld%lld", &x, &y);
if (x * x + y * y <= d)
ans++;
}
printf("%lld\n", ans);
return 0;
} | replace | 8 | 9 | 8 | 9 | -11 | |
p02595 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, D;
int count = 0;
cin >> N >> D;
vector<vector<int>> position(N, vector<int>(N));
for (int i = 0; i < N; i++) {
cin >> position.at(i).at(0) >> position.at(i).at(1);
if (sqrt(pow(position.at(i).at(0), 2) + pow(position.at(i).at(1), 2)) <= D)
count++;
}
cout << count << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, D;
int count = 0;
cin >> N >> D;
vector<vector<int>> position(N, vector<int>(2));
for (int i = 0; i < N; i++) {
cin >> position.at(i).at(0) >> position.at(i).at(1);
if (sqrt(pow(position.at(i).at(0), 2) + pow(position.at(i).at(1), 2)) <= D)
count++;
}
cout << count << endl;
} | replace | 7 | 8 | 7 | 8 | 0 | |
p02595 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <string>
using namespace std;
long long N, D;
long long X[110000];
long long Y[110000];
int main(void) {
cin >> N >> D;
for (int i = 1; i <= N; i++)
cin >> X[i] >> Y[i];
int ans;
ans = 0;
D = D * D;
for (int i = 1; i <= N; i++) {
long long dist = X[i] * X[i] + Y[i] * Y[i];
if (D >= dist)
ans++;
}
cout << ans << endl;
return 0;
} | #include <algorithm>
#include <iostream>
#include <string>
using namespace std;
long long N, D;
long long X[210000];
long long Y[210000];
int main(void) {
cin >> N >> D;
for (int i = 1; i <= N; i++)
cin >> X[i] >> Y[i];
int ans;
ans = 0;
D = D * D;
for (int i = 1; i <= N; i++) {
long long dist = X[i] * X[i] + Y[i] * Y[i];
if (D >= dist)
ans++;
}
cout << ans << endl;
return 0;
}
| replace | 6 | 8 | 6 | 8 | 0 | |
p02595 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
int main() {
long long N, D;
cin >> N >> D;
long long X[100000], Y[100000];
for (long long i = 0; i < N; i++) {
cin >> X[i];
cin >> Y[i];
}
string s;
// cin >> s;
// int A[5]
// sort(A, A + 5);
long long a = 0;
long double b = 0;
long long count = 0;
for (long long i = 0; i < N; i++) {
a = 0;
a = X[i] * X[i] + Y[i] * Y[i];
b = sqrt(a);
if (b <= D) {
count++;
}
}
if (true) {
cout << count << endl;
} else {
cout << "No" << endl;
}
} | #include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
int main() {
long long N, D;
cin >> N >> D;
long long X[200000], Y[200000];
for (long long i = 0; i < N; i++) {
cin >> X[i];
cin >> Y[i];
}
string s;
// cin >> s;
// int A[5]
// sort(A, A + 5);
long long a = 0;
long double b = 0;
long long count = 0;
for (long long i = 0; i < N; i++) {
a = 0;
a = X[i] * X[i] + Y[i] * Y[i];
b = sqrt(a);
if (b <= D) {
count++;
}
}
if (true) {
cout << count << endl;
} else {
cout << "No" << endl;
}
} | replace | 9 | 10 | 9 | 10 | 0 | |
p02595 | C++ | Runtime Error | /*
___ is a raging sadist.
*/
// Destroy all WA at test 2
// Stay cool
// Pass the 4th wall..
#include <bits/stdc++.h>
#define fastio() \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define pb push_back
#define show(x) cout << (#x) << " : " << x << endl;
#define ll long long
#define ld long double
#define pow power
#define mp make_pair
#define ff first
#define ss second
#define pii pair<ll, ll>
#define sq(x) ((x) * (x))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define siz(a) int((a).size())
#define For(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define endl "\n"
#define pi 3.14159265
const ll mod = 1000 * 1000 * 1000 + 7;
const ll mod1 = 998244353;
const ll INF = 1ll * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 + 7;
using namespace std;
ll power(ll x, ll y) {
ll res = 1;
while (y > 0) {
if (y & 1)
res = (long long)(res * x);
y = y >> 1;
if (x <= 100000000)
x = (long long)(x * x);
// cout<<x<<'\n';
}
return res;
}
ll x[100005];
ll y[100005];
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
fastio();
ll n, d;
cin >> n >> d;
For(i, 0, n) cin >> x[i] >> y[i];
ll cnt = 0;
d = d * d;
For(i, 0, n) {
ll now = x[i] * x[i] + y[i] * y[i];
if (now <= d)
cnt += 1;
}
cout << cnt;
return 0;
} | /*
___ is a raging sadist.
*/
// Destroy all WA at test 2
// Stay cool
// Pass the 4th wall..
#include <bits/stdc++.h>
#define fastio() \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define pb push_back
#define show(x) cout << (#x) << " : " << x << endl;
#define ll long long
#define ld long double
#define pow power
#define mp make_pair
#define ff first
#define ss second
#define pii pair<ll, ll>
#define sq(x) ((x) * (x))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define siz(a) int((a).size())
#define For(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define endl "\n"
#define pi 3.14159265
const ll mod = 1000 * 1000 * 1000 + 7;
const ll mod1 = 998244353;
const ll INF = 1ll * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 + 7;
using namespace std;
ll power(ll x, ll y) {
ll res = 1;
while (y > 0) {
if (y & 1)
res = (long long)(res * x);
y = y >> 1;
if (x <= 100000000)
x = (long long)(x * x);
// cout<<x<<'\n';
}
return res;
}
ll x[200005];
ll y[200005];
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
fastio();
ll n, d;
cin >> n >> d;
For(i, 0, n) cin >> x[i] >> y[i];
ll cnt = 0;
d = d * d;
For(i, 0, n) {
ll now = x[i] * x[i] + y[i] * y[i];
if (now <= d)
cnt += 1;
}
cout << cnt;
return 0;
} | replace | 51 | 53 | 51 | 53 | TLE | |
p02595 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define int long long
using namespace std;
int n, ans;
double d, x[100005], y[100005];
signed main() {
cin >> n >> d;
for (int i = 1; i <= n; i++) {
scanf("%lf %lf", &x[i], &y[i]);
if (sqrt(x[i] * x[i] + y[i] * y[i]) <= d) {
ans++;
}
}
cout << ans;
} | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define int long long
using namespace std;
int n, ans;
double d, x[200005], y[200005];
signed main() {
cin >> n >> d;
for (int i = 1; i <= n; i++) {
scanf("%lf %lf", &x[i], &y[i]);
if (sqrt(x[i] * x[i] + y[i] * y[i]) <= d) {
ans++;
}
}
cout << ans;
} | replace | 11 | 12 | 11 | 12 | 0 | |
p02595 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define optimizar_io \
ios_base::sync_with_stdio(false); \
cin.tie(0);
typedef long long int ll;
int main() {
optimizar_io int n;
ll d, x[100010], y[100010];
cin >> n >> d;
d = d * d;
for (int i = 0; i < n; i++)
cin >> x[i] >> y[i];
int cnt = 0;
for (int i = 0; i < n; i++) {
ll dist = x[i] * x[i] + y[i] * y[i];
if (dist <= d)
cnt++;
}
cout << cnt << "\n";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define optimizar_io \
ios_base::sync_with_stdio(false); \
cin.tie(0);
typedef long long int ll;
int main() {
optimizar_io int n;
ll d, x[200010], y[200010];
cin >> n >> d;
d = d * d;
for (int i = 0; i < n; i++)
cin >> x[i] >> y[i];
int cnt = 0;
for (int i = 0; i < n; i++) {
ll dist = x[i] * x[i] + y[i] * y[i];
if (dist <= d)
cnt++;
}
cout << cnt << "\n";
return 0;
} | replace | 10 | 11 | 10 | 11 | 0 | |
p02595 | C++ | Runtime Error | #include <cmath>
#include <iostream>
using namespace std;
int main() {
long N, D, output = 0;
cin >> N;
cin >> D;
long *X = (long *)malloc(N * sizeof(long));
long *Y = (long *)malloc(N * sizeof(long));
double *distances = (double *)malloc(N * sizeof(float));
for (long i = 0; i < N; i++) {
cin >> X[i];
// prlongf("X[%i] = %i\n", i, X[i]);
cin >> Y[i];
// prlongf("Y[%i] = %i\n", i, Y[i]);
}
for (long i = 0; i < N; i++) {
distances[i] = sqrt(pow(X[i], 2) + pow(Y[i], 2));
// cout << (distances[i]) << endl;
if (distances[i] <= D) {
output++;
}
}
cout << output;
} | #include <cmath>
#include <iostream>
using namespace std;
int main() {
long N, D, output = 0;
cin >> N;
cin >> D;
long *X = (long *)malloc(N * sizeof(long));
long *Y = (long *)malloc(N * sizeof(long));
double *distances = (double *)malloc(N * sizeof(double));
for (long i = 0; i < N; i++) {
cin >> X[i];
// prlongf("X[%i] = %i\n", i, X[i]);
cin >> Y[i];
// prlongf("Y[%i] = %i\n", i, Y[i]);
}
for (long i = 0; i < N; i++) {
distances[i] = sqrt(pow(X[i], 2) + pow(Y[i], 2));
// cout << (distances[i]) << endl;
if (distances[i] <= D) {
output++;
}
}
cout << output;
} | replace | 12 | 13 | 12 | 13 | -6 | malloc(): corrupted top size
|
p02595 | C++ | Runtime Error | #include <iostream>
#include <math.h>
using namespace std;
int main(void) {
int n, d;
int counter = 0;
long long A[100010], B[100010];
long double C[100010];
cin >> n >> d;
for (int i = 0; i < n; i++) {
cin >> A[i] >> B[i];
C[i] = (double)((A[i] * A[i]) + (B[i] * B[i]));
// cout << C[i] << " " << sqrt(C[i]) << endl;
}
for (int i = 0; i < n; i++) {
if (sqrt(C[i]) <= d) {
counter++;
}
}
cout << counter << endl;
}
| #include <iostream>
#include <math.h>
using namespace std;
int main(void) {
int n, d;
int counter = 0;
long long A[200020], B[200020];
long double C[200020];
cin >> n >> d;
for (int i = 0; i < n; i++) {
cin >> A[i] >> B[i];
C[i] = (double)((A[i] * A[i]) + (B[i] * B[i]));
// cout << C[i] << " " << sqrt(C[i]) << endl;
}
for (int i = 0; i < n; i++) {
if (sqrt(C[i]) <= d) {
counter++;
}
}
cout << counter << endl;
}
| replace | 6 | 8 | 6 | 8 | 0 | |
p02595 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
int x[100001], y[100001];
int main() {
int n, d;
cin >> n >> d;
for (int i = 0; i < n; i++) {
cin >> x[i] >> y[i];
}
int ans = 0;
for (int i = 0; i < n; i++) {
if (sqrt(pow(x[i], 2) + pow(y[i], 2)) <= d) {
ans++;
}
}
cout << ans << endl;
return 0;
} | #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
int x[200001], y[200001];
int main() {
int n, d;
cin >> n >> d;
for (int i = 0; i < n; i++) {
cin >> x[i] >> y[i];
}
int ans = 0;
for (int i = 0; i < n; i++) {
if (sqrt(pow(x[i], 2) + pow(y[i], 2)) <= d) {
ans++;
}
}
cout << ans << endl;
return 0;
} | replace | 12 | 13 | 12 | 13 | 0 | |
p02595 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define INF 2000000007
#define LINF 100000000000000007
#define MOD 1000000007
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define repb(i, n) for (int i = n - 1; i >= 0; i--)
// #define MODE 1
#ifdef MODE
#define DEB(X) cout << #X << ": " << X << " ";
#define ARDEB(i, X) cout << #X << "[" << i << "]: " << X[i] << " ";
#define END cout << endl;
#else
#define DEB(X) \
{}
#define ARDEB(i, X) \
{}
#define END \
{}
#endif
// typedef long long int ll;
typedef pair<int, int> P;
struct edge {
int to, cost;
};
int ceil2(int a, int b) {
if (a % b) {
return a / b + 1;
} else {
return a / b;
}
}
int n, k, ans;
int a[111111], b[111111];
signed main() {
cin >> n >> k;
rep(i, n) cin >> a[i] >> b[i];
rep(i, n) {
if (a[i] * a[i] + b[i] * b[i] <= k * k)
ans++;
}
cout << ans << endl;
} | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define INF 2000000007
#define LINF 100000000000000007
#define MOD 1000000007
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define repb(i, n) for (int i = n - 1; i >= 0; i--)
// #define MODE 1
#ifdef MODE
#define DEB(X) cout << #X << ": " << X << " ";
#define ARDEB(i, X) cout << #X << "[" << i << "]: " << X[i] << " ";
#define END cout << endl;
#else
#define DEB(X) \
{}
#define ARDEB(i, X) \
{}
#define END \
{}
#endif
// typedef long long int ll;
typedef pair<int, int> P;
struct edge {
int to, cost;
};
int ceil2(int a, int b) {
if (a % b) {
return a / b + 1;
} else {
return a / b;
}
}
int n, k, ans;
int a[211111], b[211111];
signed main() {
cin >> n >> k;
rep(i, n) cin >> a[i] >> b[i];
rep(i, n) {
if (a[i] * a[i] + b[i] * b[i] <= k * k)
ans++;
}
cout << ans << endl;
} | replace | 48 | 49 | 48 | 49 | 0 | |
p02595 | C++ | Runtime Error | #include <bits/stdc++.h>
typedef long long lint;
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
int main() {
lint n, d;
cin >> n >> d;
lint x[2000], y[2000];
rep(i, n) cin >> x[i] >> y[i];
lint ans = 0;
rep(i, n) {
lint t = x[i] * x[i] + y[i] * y[i];
if (t <= d * d)
ans++;
}
cout << ans << endl;
} | #include <bits/stdc++.h>
typedef long long lint;
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
int main() {
lint n, d;
cin >> n >> d;
lint x[200000], y[200000];
rep(i, n) cin >> x[i] >> y[i];
lint ans = 0;
rep(i, n) {
lint t = x[i] * x[i] + y[i] * y[i];
if (t <= d * d)
ans++;
}
cout << ans << endl;
} | replace | 8 | 9 | 8 | 9 | 0 | |
p02595 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int a[250][3], n, d, ans = 0;
int main() {
cin >> n >> d;
for (int i = 1; i <= n; i++)
cin >> a[i][1] >> a[i][2];
for (int i = 1; i <= n; i++) {
double x, y, z;
x = a[i][1];
y = a[i][2];
z = sqrt(x * x + y * y);
if (z <= d)
ans++;
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int a[200010][3], n, d, ans = 0;
int main() {
cin >> n >> d;
for (int i = 1; i <= n; i++)
cin >> a[i][1] >> a[i][2];
for (int i = 1; i <= n; i++) {
double x, y, z;
x = a[i][1];
y = a[i][2];
z = sqrt(x * x + y * y);
if (z <= d)
ans++;
}
cout << ans << endl;
return 0;
} | replace | 2 | 3 | 2 | 3 | 0 | |
p02595 | C++ | Runtime Error | #include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
long long int D, N, ans = 0;
long long int X[100001], Y[100001];
cin >> N >> D;
for (int i = 0; i < N; i++) {
cin >> X[i] >> Y[i];
}
for (int i = 0; i < N; i++) {
if (X[i] * X[i] + Y[i] * Y[i] <= D * D)
ans++;
}
cout << ans << endl;
return 0;
} | #include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
long long int D, N, ans = 0;
long long int X[1000001], Y[1000001];
cin >> N >> D;
for (int i = 0; i < N; i++) {
cin >> X[i] >> Y[i];
}
for (int i = 0; i < N; i++) {
if (X[i] * X[i] + Y[i] * Y[i] <= D * D)
ans++;
}
cout << ans << endl;
return 0;
} | replace | 8 | 9 | 8 | 9 | 0 | |
p02595 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
P p[100010];
int main() {
ll n, d;
ll dis = 0;
int res = 0;
cin >> n >> d;
for (int i = 0; i < n; i++) {
cin >> p[i].first >> p[i].second;
}
for (int i = 0; i < n; i++) {
ll x = p[i].first;
ll y = p[i].second;
dis = x * x + y * y;
if (dis <= d * d)
res++;
}
cout << res << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
P p[200010];
int main() {
ll n, d;
ll dis = 0;
int res = 0;
cin >> n >> d;
for (int i = 0; i < n; i++) {
cin >> p[i].first >> p[i].second;
}
for (int i = 0; i < n; i++) {
ll x = p[i].first;
ll y = p[i].second;
dis = x * x + y * y;
if (dis <= d * d)
res++;
}
cout << res << endl;
return 0;
} | replace | 5 | 6 | 5 | 6 | 0 | |
p02595 | C++ | Runtime Error | #include <iostream>
using namespace std;
#include <vector>
#define re(i, n) for (int i = 0; i < n; i++)
int main() {
int n;
cin >> n;
long long x[10000];
long long y[10000];
long long d;
cin >> d;
d *= d;
int cnt = 0;
long long z;
re(i, n) {
cin >> x[i] >> y[i];
z = x[i] * x[i] + y[i] * y[i];
if (z <= d)
cnt++;
}
cout << cnt << endl;
return 0;
} | #include <iostream>
using namespace std;
#include <vector>
#define re(i, n) for (int i = 0; i < n; i++)
int main() {
int n;
cin >> n;
long long x[300000];
long long y[300000];
long long d;
cin >> d;
d *= d;
int cnt = 0;
long long z;
re(i, n) {
cin >> x[i] >> y[i];
z = x[i] * x[i] + y[i] * y[i];
if (z <= d)
cnt++;
}
cout << cnt << endl;
return 0;
} | replace | 8 | 10 | 8 | 10 | 0 | |
p02595 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n, d, x, y, ans = 0;
scanf("%d%d", &n, &d);
d *= d;
for (int i = 0; i < n; i++) {
scanf("%lld%lld", &x, &y);
ans += (x * x + y * y <= d);
}
printf("%d", ans);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
long long n, d, x, y, ans = 0;
scanf("%lld%lld", &n, &d);
d *= d;
for (int i = 0; i < n; i++) {
scanf("%lld%lld", &x, &y);
ans += (x * x + y * y <= d);
}
printf("%d", ans);
return 0;
} | replace | 4 | 5 | 4 | 5 | TLE | |
p02595 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main() {
int n, d;
scanf("%lld%lld", &n, &d);
int ans = 0;
d *= d;
for (int i = 0; i < n; i++) {
int x, y;
scanf("%lld%lld", x, y);
if (x * x + y * y == d)
ans++;
}
printf("%lld\n", ans);
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main() {
int n, d;
scanf("%lld%lld", &n, &d);
int ans = 0;
d *= d;
for (int i = 0; i < n; i++) {
int x, y;
scanf("%lld%lld", &x, &y);
if (x * x + y * y <= d)
ans++;
}
printf("%lld\n", ans);
} | replace | 11 | 13 | 11 | 13 | -11 | |
p02596 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
ll n, m, t, a[100005];
vector<ll> adj[100005];
#define ari \
for (int i = 0; i < n; i++) \
cin >> a[i];
#define ginp \
for (int i = 0; i < m; i++) { \
int u, v; \
cin >> u >> v; \
adj[u].push_back(v); \
adj[v].push_back(u); \
}
#define pb push_back
#define ss second
#define ff first
#define fs first.second
#define fff first.first
#define sss second.second
#define sf second.first
#define mp make_pair
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll k;
cin >> k;
if (k % 2 == 0) {
cout << "-1" << endl;
return (0);
}
ll po = (7 % k), prev = 0, ans = 0;
for (int i = 1;; i++) {
if ((po + prev) % k == 0) {
ans = i;
break;
}
prev = (po + prev) % k;
po = (po * 10) % k;
}
cout << ans << endl;
return (0);
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
ll n, m, t, a[100005];
vector<ll> adj[100005];
#define ari \
for (int i = 0; i < n; i++) \
cin >> a[i];
#define ginp \
for (int i = 0; i < m; i++) { \
int u, v; \
cin >> u >> v; \
adj[u].push_back(v); \
adj[v].push_back(u); \
}
#define pb push_back
#define ss second
#define ff first
#define fs first.second
#define fff first.first
#define sss second.second
#define sf second.first
#define mp make_pair
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll k;
cin >> k;
if (k % 2 == 0 or k % 5 == 0) {
cout << "-1" << endl;
return (0);
}
ll po = (7 % k), prev = 0, ans = 0;
for (int i = 1;; i++) {
if ((po + prev) % k == 0) {
ans = i;
break;
}
prev = (po + prev) % k;
po = (po * 10) % k;
}
cout << ans << endl;
return (0);
} | replace | 30 | 31 | 30 | 31 | TLE | |
p02596 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<double> vd;
typedef vector<string> vs;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
void YN(bool flg) {
if (flg)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
void Yn(bool flg) {
if (flg)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
void yn(bool flg) {
if (flg)
cout << "yes" << endl;
else
cout << "no" << endl;
}
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() {
int k;
cin >> k;
if (k % 2 == 0) {
cout << -1 << endl;
return 0;
}
int mod = 7 % k;
int cnt = 1;
set<int> s;
while (!s.count(mod)) {
if (mod == 0) {
cout << cnt << endl;
return 0;
}
s.count(mod);
mod = (mod * 10 + 7) % k;
cnt++;
}
cout << -1 << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<double> vd;
typedef vector<string> vs;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
void YN(bool flg) {
if (flg)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
void Yn(bool flg) {
if (flg)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
void yn(bool flg) {
if (flg)
cout << "yes" << endl;
else
cout << "no" << endl;
}
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() {
int k;
cin >> k;
if (k % 2 == 0) {
cout << -1 << endl;
return 0;
}
int mod = 7 % k;
int cnt = 1;
set<int> s;
while (!s.count(mod)) {
if (mod == 0) {
cout << cnt << endl;
return 0;
}
s.insert(mod);
mod = (mod * 10 + 7) % k;
cnt++;
}
cout << -1 << endl;
return 0;
}
| replace | 67 | 68 | 67 | 68 | TLE | |
p02596 | C++ | Time Limit Exceeded | #include <iostream>
using namespace std;
int main(void) {
long long k;
cin >> k;
if (k % 2 == 0) {
cout << -1 << endl;
return 0;
}
long long next_inc = 70;
long long ex = 7;
int cnt = 1;
while (ex % k != 0) {
ex %= k;
ex += next_inc;
next_inc *= 10;
next_inc %= k;
cnt += 1;
}
cout << cnt << endl;
return 0;
} | #include <iostream>
using namespace std;
int main(void) {
long long k;
cin >> k;
if (k % 2 == 0 || k % 5 == 0) {
cout << -1 << endl;
return 0;
}
long long next_inc = 70;
long long ex = 7;
int cnt = 1;
while (ex % k != 0) {
ex %= k;
ex += next_inc;
next_inc *= 10;
next_inc %= k;
cnt += 1;
}
cout << cnt << endl;
return 0;
} | replace | 8 | 9 | 8 | 9 | TLE | |
p02596 | C++ | Time Limit Exceeded | #pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pll pair<ll, ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define lb(c, x) distance(c.begin(), lower_bound(all(c), x))
#define ub(c, x) distance(c.begin(), upper_bound(all(c), x))
using namespace std;
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;
}
const ll INF = 1e18;
const ll mod = 1e9 + 7;
int main() {
ll k;
cin >> k;
if (k == 2 || k == 5) {
cout << -1 << endl;
return 0;
}
ll v = 0;
ll cnt = 0;
while (true) {
cnt++;
v = v * 10 + 7;
v %= k;
if (v == 0)
break;
}
cout << cnt << endl;
return 0;
} | #pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pll pair<ll, ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define lb(c, x) distance(c.begin(), lower_bound(all(c), x))
#define ub(c, x) distance(c.begin(), upper_bound(all(c), x))
using namespace std;
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;
}
const ll INF = 1e18;
const ll mod = 1e9 + 7;
int main() {
ll k;
cin >> k;
if (k % 2 == 0 || k % 5 == 0) {
cout << -1 << endl;
return 0;
}
ll v = 0;
ll cnt = 0;
while (true) {
cnt++;
v = v * 10 + 7;
v %= k;
if (v == 0)
break;
}
cout << cnt << endl;
return 0;
}
| replace | 37 | 38 | 37 | 38 | TLE | |
p02596 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
#define all(v) v.begin(), v.end()
const ll mod = 1000000007;
ll countBits(ll in) {
int res = 0;
for (; in > 0; in >>= 1) {
if ((in & 0x01) != 0) {
res++;
}
}
return res;
}
template <typename T> void show2dVector(vector<vector<T>> &v) {
for (int i = 0; i < v.size(); i++) {
int m = v[i].size();
cout << i << " : ";
for (int j = 0; i < m; i++) {
cout << v[i][j] << " ";
}
cout << endl;
}
}
void bfs(const vector<vector<int>> &g, int v, vector<bool> &seen) {
seen[v] = true;
cout << v << " ";
for (auto next : g[v]) {
if (seen[next]) {
continue;
}
bfs(g, next, seen);
}
}
bool dfs(vector<vector<int>> &g, int start, int goal, vector<bool> &seen) {
bool res = false;
seen[start] = true;
if (start == goal) {
return true;
}
for (auto next : g[start]) {
if (seen[next]) {
continue;
}
res = dfs(g, next, goal, seen);
if (res) {
break;
}
}
return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
bool isLowerCase(char c) { return (c >= 'a' && c <= 'z'); }
ll powm(ll a, ll n, ll m) {
ll ret = 1;
while (n > 0) {
if (n % 2 == 1) {
ret = (ret * a) % m;
}
n >>= 1;
a = (a * a) % m;
}
return ret;
}
void primeFactor(ll val, map<ll, ll> &primeList) {
if (val < 1) {
return;
}
for (int i = 2; i * i < val; i++) {
while (val % i == 0) {
primeList[i]++;
val /= i;
}
}
if (val != 1) {
primeList[val] = 1;
}
return;
}
const string yesno(bool ans) { return (ans ? "Yes" : "No"); }
int main() {
ll k;
cin >> k;
int ans = 0;
ll a = 0;
if (k == 1 || k == 7) {
cout << 1 << endl;
return 0;
} else if (k % 2 == 0) {
cout << -1 << endl;
return 0;
}
for (int i = 1; i < INT_MAX; i++) {
a = a * 10 + 7;
a %= k;
if (a == 0) {
ans = i;
break;
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
#define all(v) v.begin(), v.end()
const ll mod = 1000000007;
ll countBits(ll in) {
int res = 0;
for (; in > 0; in >>= 1) {
if ((in & 0x01) != 0) {
res++;
}
}
return res;
}
template <typename T> void show2dVector(vector<vector<T>> &v) {
for (int i = 0; i < v.size(); i++) {
int m = v[i].size();
cout << i << " : ";
for (int j = 0; i < m; i++) {
cout << v[i][j] << " ";
}
cout << endl;
}
}
void bfs(const vector<vector<int>> &g, int v, vector<bool> &seen) {
seen[v] = true;
cout << v << " ";
for (auto next : g[v]) {
if (seen[next]) {
continue;
}
bfs(g, next, seen);
}
}
bool dfs(vector<vector<int>> &g, int start, int goal, vector<bool> &seen) {
bool res = false;
seen[start] = true;
if (start == goal) {
return true;
}
for (auto next : g[start]) {
if (seen[next]) {
continue;
}
res = dfs(g, next, goal, seen);
if (res) {
break;
}
}
return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
bool isLowerCase(char c) { return (c >= 'a' && c <= 'z'); }
ll powm(ll a, ll n, ll m) {
ll ret = 1;
while (n > 0) {
if (n % 2 == 1) {
ret = (ret * a) % m;
}
n >>= 1;
a = (a * a) % m;
}
return ret;
}
void primeFactor(ll val, map<ll, ll> &primeList) {
if (val < 1) {
return;
}
for (int i = 2; i * i < val; i++) {
while (val % i == 0) {
primeList[i]++;
val /= i;
}
}
if (val != 1) {
primeList[val] = 1;
}
return;
}
const string yesno(bool ans) { return (ans ? "Yes" : "No"); }
int main() {
ll k;
cin >> k;
int ans = 0;
ll a = 0;
if (k == 1 || k == 7) {
cout << 1 << endl;
return 0;
} else if (k % 2 == 0 || k % 5 == 0) {
cout << -1 << endl;
return 0;
}
for (int i = 1; i < INT_MAX; i++) {
a = a * 10 + 7;
a %= k;
if (a == 0) {
ans = i;
break;
}
}
cout << ans << endl;
return 0;
}
| replace | 101 | 102 | 101 | 102 | TLE | |
p02596 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using P = pair<int, int>;
int main() {
ll k;
cin >> k;
if (k % 2 == 0) {
cout << -1 << endl;
return 0;
}
ll s = k;
ll cnt = 0;
while (1) {
if (s == 7) {
cnt++;
break;
} else if (s % 10 == 7) {
s /= 10;
cnt++;
} else {
s += k;
}
if (cnt > 999982) {
cnt = -1;
break;
}
}
cout << cnt << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using P = pair<int, int>;
int main() {
ll k;
cin >> k;
if (k % 2 == 0) {
cout << -1 << endl;
return 0;
}
if (k % 5 == 0) {
cout << -1 << endl;
return 0;
}
ll s = k;
ll cnt = 0;
while (1) {
if (s == 7) {
cnt++;
break;
} else if (s % 10 == 7) {
s /= 10;
cnt++;
} else {
s += k;
}
if (cnt > 999982) {
cnt = -1;
break;
}
}
cout << cnt << endl;
return 0;
}
| insert | 10 | 10 | 10 | 14 | TLE | |
p02596 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
long long int n;
cin >> n;
if (n % 2 == 0) {
cout << "-1" << endl;
return 0;
}
long long int k = 7;
long long int cnt = 1;
while (1) {
if (k % n == 0) {
cout << cnt << endl;
break;
}
if (cnt > n) {
cout << "-1" << endl;
}
k = (k % n) * 10 + 7;
cnt++;
}
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
long long int n;
cin >> n;
if (n % 2 == 0) {
cout << "-1" << endl;
return 0;
}
long long int k = 7;
long long int cnt = 1;
while (1) {
if (k % n == 0) {
cout << cnt << endl;
break;
}
if (cnt > n) {
cout << "-1" << endl;
break;
}
k = (k % n) * 10 + 7;
cnt++;
}
}
| insert | 18 | 18 | 18 | 19 | TLE | |
p02596 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pb push_back
#define pf push_front
#define pof pop_front
#define pob pop_back
#define mp make_pair
#define pll pair<ll, ll>
#define pii pair<int, int>
#define all(s) s.begin(), s.end()
#define vll vector<ll>
#define vi vector<int>
#define vpii vector<pii>
#define vpll vector<pll>
#define vd vector<double>
#define vull vector<ull>
#define vc vector<char>
#define vs vector<string>
#define fi first
#define se second
#define sz(x) x.size()
#define cinarr(n, a) \
for (int i = 0; i < n; i++) \
cin >> a[i];
#define coutarr(n, a) \
for (int i = 0; i < n; i++) \
cout << a[i] << (i + 1 == n ? '\n' : ' ');
#define fori(i, a, b, x) for (int i = a; i <= b; i += x)
#define ford(i, a, b, x) for (int i = a; i >= b; i -= x)
#define tc \
int tt; \
cin >> tt; \
for (int cs = 1; cs <= tt; cs++)
#define IOS \
ios_base::sync_with_stdio(0); \
cin.tie(NULL);
#define ld long double
#define reset(x, y) memset(x, y, sizeof x);
#define sqr(x) 1LL * (x) * (x)
#define cube(x) 1LL * (x) * (x) * (x)
#define ub(v, a) upper_bound(all(v), a)
#define lb(v, a) lower_bound(all(v), a)
#define EPS 1e-10
#define INF 200000
#define mod 1000000007
#define minof(a) min_element(all(a))
#define maxof(a) max_element(all(a))
#define debug(val) cout << "The value of " << #val << " is = " << val << endl;
#define testing cout << "--------------------------\n";
int main() {
IOS int n;
cin >> n;
if (n % 2 == 0) {
cout << "-1\n";
return 0;
}
int cur = 1, nw = 7 % n;
while (nw) {
nw = (nw * 10 + 7) % n;
cur++;
}
cout << cur << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pb push_back
#define pf push_front
#define pof pop_front
#define pob pop_back
#define mp make_pair
#define pll pair<ll, ll>
#define pii pair<int, int>
#define all(s) s.begin(), s.end()
#define vll vector<ll>
#define vi vector<int>
#define vpii vector<pii>
#define vpll vector<pll>
#define vd vector<double>
#define vull vector<ull>
#define vc vector<char>
#define vs vector<string>
#define fi first
#define se second
#define sz(x) x.size()
#define cinarr(n, a) \
for (int i = 0; i < n; i++) \
cin >> a[i];
#define coutarr(n, a) \
for (int i = 0; i < n; i++) \
cout << a[i] << (i + 1 == n ? '\n' : ' ');
#define fori(i, a, b, x) for (int i = a; i <= b; i += x)
#define ford(i, a, b, x) for (int i = a; i >= b; i -= x)
#define tc \
int tt; \
cin >> tt; \
for (int cs = 1; cs <= tt; cs++)
#define IOS \
ios_base::sync_with_stdio(0); \
cin.tie(NULL);
#define ld long double
#define reset(x, y) memset(x, y, sizeof x);
#define sqr(x) 1LL * (x) * (x)
#define cube(x) 1LL * (x) * (x) * (x)
#define ub(v, a) upper_bound(all(v), a)
#define lb(v, a) lower_bound(all(v), a)
#define EPS 1e-10
#define INF 200000
#define mod 1000000007
#define minof(a) min_element(all(a))
#define maxof(a) max_element(all(a))
#define debug(val) cout << "The value of " << #val << " is = " << val << endl;
#define testing cout << "--------------------------\n";
int main() {
IOS int n;
cin >> n;
if (n % 2 == 0) {
cout << "-1\n";
return 0;
}
int cur = 1, nw = 7 % n;
while (nw) {
nw = (nw * 10 + 7) % n;
cur++;
if (cur > 1e7) {
cout << "-1\n";
return 0;
}
}
cout << cur << endl;
}
| insert | 64 | 64 | 64 | 68 | TLE | |
p02596 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
// One implementation to rule them all?
#define db double
#define ll long long int
#define ld long double
// #define int long long int
#define ull unsigned long long
#define fi first
#define se second
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, n, b) for (int i = n; i >= b; i--)
#define endl '\n'
#define mem(a, b) memset(a, b, sizeof a)
#define mkp make_pair
#define pb push_back
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define w(t) \
int t; \
cin >> t; \
while (t--)
#define all(a) a.begin(), a.end()
#define sz(a) int((a).size())
#define dbg(x) cerr << #x << " = " << x << endl;
#define dbgp(x) \
cerr << "[ " << #x << ".ff : " << x.first << ", " << #x \
<< ".ss : " << x.second << " ]" << endl
#define dbgs(x) \
cerr << "[ "; \
for (auto i : x) \
cerr << i << " "; \
cerr << " ]" << endl;
#define dbgm(x) \
cerr << "----------" << endl; \
for (auto i : x) \
cerr << i.fi << ":" << i.se << endl; \
cerr << "----------";
#define printdb(i) printf("%.12lf\n", i)
#define isOn(S, j) (S & (1 << j))
#define setBit(S, j) (S |= (1 << j))
#define clearBit(S, j) (S &= ~(1 << j))
#define toggleBit(S, j) (S ^= (1 << j))
#define lowBit(S) (S & (-S))
#define setAll(S, n) (S = (1 << n) - 1)
#define turnOffLastBit(S) ((S) & (S - 1))
#define turnOnLastZero(S) ((S) | (S + 1))
// Can use tuples in C++17
using namespace std;
typedef map<int, int> mii;
typedef vector<int> vii;
typedef vector<ll> vll;
typedef vector<vii> vvi;
typedef pair<int, int> pii;
template <class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
template <class T> using max_heap = priority_queue<T>;
const db PI = acos(-1);
const int INF = 0x3f3f3f3f;
const ll LINF = (ll)2e18;
db eps = 1e-5;
ll mod = (1e9 + 7); // 998244353
void print(vector<int> &arr) {
for (int i = 0; i < arr.size(); i++)
cout << arr[i] << " ";
cout << endl;
}
void printll(vector<ll> &arr) {
for (int i = 0; i < arr.size(); i++)
cout << arr[i] << " ";
cout << endl;
}
ll ncr(ll n, ll r) {
r = min(r, n - r);
ll A[r], i, j, B[r];
iota(A, A + r, n - r + 1);
iota(B, B + r, 1);
ll g;
for (i = 0; i < r; i++)
for (j = 0; j < r; j++) {
if (B[i] == 1)
break;
g = __gcd(B[i], A[j]);
A[j] /= g;
B[i] /= g;
}
ll ans = 1;
for (i = 0; i < r; i++)
ans = (ans * A[i] % mod);
return ans;
}
ll binpow(ll a, ll b, ll m) {
a %= m;
ll res = 1;
while (b > 0) {
if (b & 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
const int SIZE = 200001; // constant SIZE here
bool judge(int a[], int n) {
static int used[SIZE + 1];
for (int i = 1; i <= n; i++)
used[i] = 0;
for (int i = 0; i < n; i++)
used[a[i]] = 1;
for (int i = 1; i <= n; i++) {
if (!used[i])
return 0;
}
return 1;
}
int n, m, r, u, v, idx = 1, k = 0;
vvi adj(SIZE);
vii color(SIZE, -1), indeg(SIZE, 0), outdeg(SIZE, 0), level(SIZE, 0);
vector<bool> vis(SIZE, false);
stack<int> st;
bool dfs(int u) {
color[u] = 1;
bool ans = false;
for (auto i : adj[u]) {
if (color[i] == 1)
ans = true;
if (color[i] == 0 && dfs(i))
ans = true;
}
color[u] = 2;
return ans;
}
vector<set<int>> vv(SIZE);
void dfs2(int u, int par) {
vis[u] = 1;
// dbg(u);
// dbg(par);
if (color[u] == -1) {
set<int> s;
for (auto i : adj[u])
s.insert(color[i]);
for (auto i : adj[par])
s.insert(color[i]);
idx = 0;
// dbgs(s);
while (s.find(idx) != s.end()) {
idx = (idx + 1) % k;
}
// dbg(idx);
color[u] = idx;
}
for (auto i : adj[u]) {
if (!vis[i])
dfs2(i, u);
}
}
int main() {
// CONDITION && cout << "YES" || cout << "NO"; cout << '\n';
//////////////////////////////////////////////////////////////////
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cerr.tie(NULL);
// For fast IO
/////////////////////////////////////////////////////////////////////
// Have you thought about it enough?
/////////////////////////////////////////////////////////////////////
// std::cout.unsetf ( std::ios::floatfield ); // floatfield not set
// std::cout.precision(16);
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
ll n;
cin >> n;
ll ans = 1, num = 7;
while (num % n != 0) {
if (ans >= LLONG_MAX) {
cout << -1 << endl;
exit(0);
}
ans++;
num = (1LL * num * 10 + 7LL) % n;
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
// One implementation to rule them all?
#define db double
#define ll long long int
#define ld long double
// #define int long long int
#define ull unsigned long long
#define fi first
#define se second
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repr(i, n, b) for (int i = n; i >= b; i--)
#define endl '\n'
#define mem(a, b) memset(a, b, sizeof a)
#define mkp make_pair
#define pb push_back
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define w(t) \
int t; \
cin >> t; \
while (t--)
#define all(a) a.begin(), a.end()
#define sz(a) int((a).size())
#define dbg(x) cerr << #x << " = " << x << endl;
#define dbgp(x) \
cerr << "[ " << #x << ".ff : " << x.first << ", " << #x \
<< ".ss : " << x.second << " ]" << endl
#define dbgs(x) \
cerr << "[ "; \
for (auto i : x) \
cerr << i << " "; \
cerr << " ]" << endl;
#define dbgm(x) \
cerr << "----------" << endl; \
for (auto i : x) \
cerr << i.fi << ":" << i.se << endl; \
cerr << "----------";
#define printdb(i) printf("%.12lf\n", i)
#define isOn(S, j) (S & (1 << j))
#define setBit(S, j) (S |= (1 << j))
#define clearBit(S, j) (S &= ~(1 << j))
#define toggleBit(S, j) (S ^= (1 << j))
#define lowBit(S) (S & (-S))
#define setAll(S, n) (S = (1 << n) - 1)
#define turnOffLastBit(S) ((S) & (S - 1))
#define turnOnLastZero(S) ((S) | (S + 1))
// Can use tuples in C++17
using namespace std;
typedef map<int, int> mii;
typedef vector<int> vii;
typedef vector<ll> vll;
typedef vector<vii> vvi;
typedef pair<int, int> pii;
template <class T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
template <class T> using max_heap = priority_queue<T>;
const db PI = acos(-1);
const int INF = 0x3f3f3f3f;
const ll LINF = (ll)2e18;
db eps = 1e-5;
ll mod = (1e9 + 7); // 998244353
void print(vector<int> &arr) {
for (int i = 0; i < arr.size(); i++)
cout << arr[i] << " ";
cout << endl;
}
void printll(vector<ll> &arr) {
for (int i = 0; i < arr.size(); i++)
cout << arr[i] << " ";
cout << endl;
}
ll ncr(ll n, ll r) {
r = min(r, n - r);
ll A[r], i, j, B[r];
iota(A, A + r, n - r + 1);
iota(B, B + r, 1);
ll g;
for (i = 0; i < r; i++)
for (j = 0; j < r; j++) {
if (B[i] == 1)
break;
g = __gcd(B[i], A[j]);
A[j] /= g;
B[i] /= g;
}
ll ans = 1;
for (i = 0; i < r; i++)
ans = (ans * A[i] % mod);
return ans;
}
ll binpow(ll a, ll b, ll m) {
a %= m;
ll res = 1;
while (b > 0) {
if (b & 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
const int SIZE = 200001; // constant SIZE here
bool judge(int a[], int n) {
static int used[SIZE + 1];
for (int i = 1; i <= n; i++)
used[i] = 0;
for (int i = 0; i < n; i++)
used[a[i]] = 1;
for (int i = 1; i <= n; i++) {
if (!used[i])
return 0;
}
return 1;
}
int n, m, r, u, v, idx = 1, k = 0;
vvi adj(SIZE);
vii color(SIZE, -1), indeg(SIZE, 0), outdeg(SIZE, 0), level(SIZE, 0);
vector<bool> vis(SIZE, false);
stack<int> st;
bool dfs(int u) {
color[u] = 1;
bool ans = false;
for (auto i : adj[u]) {
if (color[i] == 1)
ans = true;
if (color[i] == 0 && dfs(i))
ans = true;
}
color[u] = 2;
return ans;
}
vector<set<int>> vv(SIZE);
void dfs2(int u, int par) {
vis[u] = 1;
// dbg(u);
// dbg(par);
if (color[u] == -1) {
set<int> s;
for (auto i : adj[u])
s.insert(color[i]);
for (auto i : adj[par])
s.insert(color[i]);
idx = 0;
// dbgs(s);
while (s.find(idx) != s.end()) {
idx = (idx + 1) % k;
}
// dbg(idx);
color[u] = idx;
}
for (auto i : adj[u]) {
if (!vis[i])
dfs2(i, u);
}
}
int main() {
// CONDITION && cout << "YES" || cout << "NO"; cout << '\n';
//////////////////////////////////////////////////////////////////
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cerr.tie(NULL);
// For fast IO
/////////////////////////////////////////////////////////////////////
// Have you thought about it enough?
/////////////////////////////////////////////////////////////////////
// std::cout.unsetf ( std::ios::floatfield ); // floatfield not set
// std::cout.precision(16);
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
ll n;
cin >> n;
ll ans = 1, num = 7;
while (num % n != 0) {
if (ans >= 100000000LL) {
cout << -1 << endl;
exit(0);
}
ans++;
num = (1LL * num * 10 + 7LL) % n;
}
cout << ans << endl;
return 0;
}
| replace | 180 | 181 | 180 | 181 | TLE | |
p02596 | C++ | Time Limit Exceeded | // created by mtnshh
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repb(i, a, b) for (ll i = a; i >= b; i--)
#define err() cout << "--------------------------" << endl;
#define errA(A) \
for (auto i : A) \
cout << i << " "; \
cout << endl;
#define err1(a) cout << #a << " " << a << endl
#define err2(a, b) cout << #a << " " << a << " " << #b << " " << b << endl
#define err3(a, b, c) \
cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c \
<< endl
#define err4(a, b, c, d) \
cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c \
<< " " << #d << " " << d << endl
#define pb push_back
#define all(A) A.begin(), A.end()
#define allr(A) A.rbegin(), A.rend()
#define ft first
#define sd second
#define pll pair<ll, ll>
#define V vector<ll>
#define S set<ll>
#define VV vector<V>
#define Vpll vector<pll>
#define endl "\n"
const ll logN = 20;
const ll M = 1000000007;
const ll INF = 1e18;
#define PI 3.14159265
const ll N = 100005;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll k;
cin >> k;
ll p = 1;
ll rem = 0;
if (k % 2 == 0) {
cout << -1 << endl;
} else {
ll cnt = 0;
while (1) {
cnt++;
rem = (rem + p * 7) % k;
p = (p * 10) % k;
if (rem == 0) {
cout << cnt << endl;
return 0;
}
}
}
return 0;
} | // created by mtnshh
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repb(i, a, b) for (ll i = a; i >= b; i--)
#define err() cout << "--------------------------" << endl;
#define errA(A) \
for (auto i : A) \
cout << i << " "; \
cout << endl;
#define err1(a) cout << #a << " " << a << endl
#define err2(a, b) cout << #a << " " << a << " " << #b << " " << b << endl
#define err3(a, b, c) \
cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c \
<< endl
#define err4(a, b, c, d) \
cout << #a << " " << a << " " << #b << " " << b << " " << #c << " " << c \
<< " " << #d << " " << d << endl
#define pb push_back
#define all(A) A.begin(), A.end()
#define allr(A) A.rbegin(), A.rend()
#define ft first
#define sd second
#define pll pair<ll, ll>
#define V vector<ll>
#define S set<ll>
#define VV vector<V>
#define Vpll vector<pll>
#define endl "\n"
const ll logN = 20;
const ll M = 1000000007;
const ll INF = 1e18;
#define PI 3.14159265
const ll N = 100005;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll k;
cin >> k;
ll p = 1;
ll rem = 0;
if (k % 2 == 0 or k % 5 == 0) {
cout << -1 << endl;
} else {
ll cnt = 0;
while (1) {
cnt++;
rem = (rem + p * 7) % k;
p = (p * 10) % k;
if (rem == 0) {
cout << cnt << endl;
return 0;
}
}
}
return 0;
} | replace | 55 | 56 | 55 | 56 | TLE | |
p02596 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
int K, sum = 0;
cin >> K;
if (K % 2 == 0 || K % 5 == 0) {
sum = -1;
} else {
K *= 9;
if (K % 7 == 0) {
K /= 7;
}
int s = 0, i = 10;
while (s == 0) {
sum++;
if (i % K == 1) {
s = 1;
}
i *= 10;
}
}
cout << sum << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int K, sum = 0;
cin >> K;
if (K % 2 == 0 || K % 5 == 0) {
sum = -1;
} else {
K *= 9;
if (K % 7 == 0) {
K /= 7;
}
int s = 0, i = 10;
while (s == 0) {
sum++;
i %= K;
if (i == 1) {
s = 1;
}
i *= 10;
}
}
cout << sum << endl;
} | replace | 16 | 17 | 16 | 18 | TLE | |
p02596 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
int k, n = 1;
cin >> k;
if (k % 2 == 0) {
cout << -1 << endl;
} else {
int r = 7 % k;
while (r != 0) {
r = (10 * r + 7) % k;
n++;
}
cout << n << endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int k, n = 1;
cin >> k;
if (k % 2 == 0 || k % 5 == 0) {
cout << -1 << endl;
} else {
int r = 7 % k;
while (r != 0) {
r = (10 * r + 7) % k;
n++;
}
cout << n << endl;
}
} | replace | 6 | 7 | 6 | 7 | TLE | |
p02596 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define int long long
using namespace std;
signed main(void) {
int n, i, k, now = 7, ans = 1;
cin >> n;
if (n % 2 == 0 || n % 5 == 0) {
cout << -1 << "\n";
} else {
while (1) {
if (now % n == 0) {
cout << ans << "\n";
break;
} else {
ans++;
n = n % 1000000;
now = now * 10 + 7;
now = now % 1000000;
}
}
}
return 0;
} | #include <bits/stdc++.h>
#define int long long
using namespace std;
signed main(void) {
int n, i, k, now = 7, ans = 1;
cin >> n;
if (n % 2 == 0 || n % 5 == 0) {
cout << -1 << "\n";
} else {
while (1) {
if (now % n == 0) {
cout << ans << "\n";
break;
} else {
ans++;
now = (now * 10 + 7) % n;
}
}
}
return 0;
} | replace | 22 | 27 | 22 | 23 | TLE | |
p02596 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define per(i, a, b) for (int i = a; i >= b; --i)
#define mem0(x) memset(x, 0, sizeof(x))
#define meminf(x) memset(x, 0x3f, sizeof(x))
#define VI vector<int>
#define VL vector<ll>
using namespace std;
int main() {
ios::sync_with_stdio(0);
int k;
cin >> k;
if (k % 2 == 0) {
cout << -1 << '\n';
} else {
ll num = 7;
ll cnt = 1;
while (1) {
if (num % k != 0) {
num = (num * 10 + 7) % k;
++cnt;
} else {
break;
}
}
cout << cnt << '\n';
}
}
| #include <bits/stdc++.h>
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define per(i, a, b) for (int i = a; i >= b; --i)
#define mem0(x) memset(x, 0, sizeof(x))
#define meminf(x) memset(x, 0x3f, sizeof(x))
#define VI vector<int>
#define VL vector<ll>
using namespace std;
int main() {
ios::sync_with_stdio(0);
int k;
cin >> k;
if (k % 2 == 0 || k % 5 == 0) {
cout << -1 << '\n';
} else {
ll num = 7;
ll cnt = 1;
while (1) {
if (num % k != 0) {
num = (num * 10 + 7) % k;
++cnt;
} else {
break;
}
}
cout << cnt << '\n';
}
}
| replace | 16 | 17 | 16 | 17 | TLE | |
p02596 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
void solve() {
int k;
cin >> k;
if (k % 2 == 0) {
cout << -1 << endl;
return;
}
int cnt = 1;
int r = 7;
map<int, int> check;
while (true) {
if (r % k == 0)
break;
if (check[r] > 0) {
cout << -1 << endl;
}
check[r]++;
r = r * 10 + 7;
r %= k;
cnt++;
}
cout << cnt << endl;
}
int main() {
solve();
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
void solve() {
int k;
cin >> k;
if (k % 2 == 0) {
cout << -1 << endl;
return;
}
int cnt = 1;
int r = 7;
map<int, int> check;
while (true) {
if (r % k == 0)
break;
if (check[r] > 0) {
cout << -1 << endl;
return;
}
check[r]++;
r = r * 10 + 7;
r %= k;
cnt++;
}
cout << cnt << endl;
}
int main() {
solve();
return 0;
}
| insert | 22 | 22 | 22 | 23 | TLE | |
p02596 | C++ | Time Limit Exceeded | // Author: AnandRaj doubleux
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<pll> vpll;
#define fastio \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define test() \
int t; \
cin >> t; \
while (t--)
#define all(v) v.begin(), v.end()
#define prinp(p) cout << p.first << " " << p.second << endl
#define prinv(V) \
for (auto v : V) \
cout << v << " "; \
cout << endl
#define take(V, f, n) \
for (int in = f; in < f + n; in++) \
cin >> V[in]
#define what(x) cerr << #x << " = " << x << endl
#define KStest() \
int t, t1; \
cin >> t; \
t1 = t; \
while (t--)
#define KScout cout << "Case #" << t1 - t << ": "
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9 + 7, MAX = 1e6 + 5;
const ll INF = 1e18 + 5;
int main() {
int k;
cin >> k;
if (k % 2 == 0)
cout << -1 << endl;
else {
int curr = 7;
int step = 1;
while (curr % k) {
curr *= 10;
curr += 7;
curr %= k;
step++;
}
cout << step << endl;
}
} | // Author: AnandRaj doubleux
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<pll> vpll;
#define fastio \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define test() \
int t; \
cin >> t; \
while (t--)
#define all(v) v.begin(), v.end()
#define prinp(p) cout << p.first << " " << p.second << endl
#define prinv(V) \
for (auto v : V) \
cout << v << " "; \
cout << endl
#define take(V, f, n) \
for (int in = f; in < f + n; in++) \
cin >> V[in]
#define what(x) cerr << #x << " = " << x << endl
#define KStest() \
int t, t1; \
cin >> t; \
t1 = t; \
while (t--)
#define KScout cout << "Case #" << t1 - t << ": "
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9 + 7, MAX = 1e6 + 5;
const ll INF = 1e18 + 5;
int main() {
int k;
cin >> k;
if (k % 2 == 0 || k % 5 == 0)
cout << -1 << endl;
else {
int curr = 7;
int step = 1;
while (curr % k) {
curr *= 10;
curr += 7;
curr %= k;
step++;
}
cout << step << endl;
}
} | replace | 46 | 47 | 46 | 47 | TLE | |
p02596 | C++ | Time Limit Exceeded | using namespace std;
#include <algorithm>
#include <assert.h>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
typedef long long ll;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long k;
cin >> k;
long long nm = 1;
long long md = 7;
if (k == 1 || k == 7) {
cout << 1 << endl;
return 0;
}
while (true) {
if (md % k == 7 % k && nm > 1) {
cout << -1 << endl;
break;
}
if (md == 0) {
cout << nm << endl;
break;
}
nm++;
md *= 10;
md += 7;
md %= k;
}
} | using namespace std;
#include <algorithm>
#include <assert.h>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
typedef long long ll;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long k;
cin >> k;
long long nm = 1;
long long md = 7;
if (k == 1 || k == 7) {
cout << 1 << endl;
return 0;
}
while (true) {
if ((md % k == 7 % k && nm > 1) || nm > 1000100) {
cout << -1 << endl;
break;
}
if (md == 0) {
cout << nm << endl;
break;
}
nm++;
md *= 10;
md += 7;
md %= k;
}
} | replace | 24 | 25 | 24 | 25 | TLE | |
p02596 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
// #define int long long int
void solve() {
int k, cnt = 0, rem = 0, num = 7;
cin >> k;
if (k % 2 == 0)
cout << "-1"
<< "\n";
else {
while (1) {
rem += (num % k);
num %= k;
num *= 10;
cnt++;
rem %= k;
if (rem % k == 0)
break;
}
cout << cnt << "\n";
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
// cin>>t;
while (t--) {
solve();
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
// #define int long long int
void solve() {
int k, cnt = 0, rem = 0, num = 7;
cin >> k;
if (k % 2 == 0)
cout << "-1"
<< "\n";
else {
while (1) {
rem += (num % k);
num %= k;
num *= 10;
cnt++;
rem %= k;
if (rem % k == 0)
break;
if (cnt > 1000000) {
cnt = -1;
break;
}
}
cout << cnt << "\n";
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
// cin>>t;
while (t--) {
solve();
}
return 0;
}
| insert | 18 | 18 | 18 | 22 | TLE | |
p02596 | C++ | Time Limit Exceeded | #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define FOR_LT(i, beg, end) for (int i = (int)(beg); i < (int)(end); i++)
#define FOR_LE(i, beg, end) for (int i = (int)(beg); i <= (int)(end); i++)
#define FOR_DW(i, beg, end) for (int i = (int)(beg); (int)(end) <= i; i--)
#define REP(n) for (int repeat_index = 0; repeat_index < (int)n; repeat_index++)
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << fixed << setprecision(20);
int64_t k;
cin >> k;
if (!(k & 1)) {
cout << -1 << endl;
return 0;
}
int64_t r = 1;
int64_t n = 0;
int64_t c = 1;
while (true) {
n += r * 7;
n %= k;
if (n == 0) {
cout << c << endl;
return 0;
}
r = (r * 10) % k;
c++;
}
return 0;
} | #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define FOR_LT(i, beg, end) for (int i = (int)(beg); i < (int)(end); i++)
#define FOR_LE(i, beg, end) for (int i = (int)(beg); i <= (int)(end); i++)
#define FOR_DW(i, beg, end) for (int i = (int)(beg); (int)(end) <= i; i--)
#define REP(n) for (int repeat_index = 0; repeat_index < (int)n; repeat_index++)
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << fixed << setprecision(20);
int64_t k;
cin >> k;
if (!(k & 1) || (k % 10) == 5) {
cout << -1 << endl;
return 0;
}
int64_t r = 1;
int64_t n = 0;
int64_t c = 1;
while (true) {
n += r * 7;
n %= k;
if (n == 0) {
cout << c << endl;
return 0;
}
r = (r * 10) % k;
c++;
}
return 0;
} | replace | 41 | 42 | 41 | 42 | TLE | |
p02596 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(i, n) for (i = 0; i < n; i++)
#define Fo(i, k, n) for (i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)
#define ll long long
#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define ss(s) scanf("%s", s)
#define pi(x) printf("%d\n", x)
#define pl(x) printf("%lld\n", x)
#define ps(s) printf("%s\n", s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
mt19937_64
rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) {
uniform_int_distribution<int> uid(0, lim - 1);
return uid(rang);
}
int mpow(int base, int exp);
void ipgraph(int n, int m);
void dfs(int u, int par);
const int mod = 1'000'000'007;
const int N = 3e5, M = N;
//=======================
/*Advanced
vi g[N];
int a[N];
*/
void solve() { int i, j, n, m; }
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
srand(chrono::high_resolution_clock::now().time_since_epoch().count());
int k;
cin >> k;
k = k % mod;
int sequ = 7 % k;
int i = 1;
while (i != mod) {
if (sequ == 0) {
cout << i;
return 0;
}
sequ = (sequ * 10 + 7) % k;
i++;
}
cout << -1;
return 0;
}
/*
int mpow(int base, int exp) {
base %= mod;
int result = 1;
while (exp > 0) {
if (exp & 1) result = ((ll)result * base) % mod;
base = ((ll)base * base) % mod;
exp >>= 1;
}
return result;
}
void ipgraph(int n, int m){
int i, u, v;
while(m--){
cin>>u>>v;
u--, v--;
g[u].pb(v);
g[v].pb(u);
}
}
void dfs(int u, int par){
for(int v:g[u]){
if (v == par) continue;
dfs(v, u);
}
}
*/
| #include <bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(i, n) for (i = 0; i < n; i++)
#define Fo(i, k, n) for (i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)
#define ll long long
#define si(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define ss(s) scanf("%s", s)
#define pi(x) printf("%d\n", x)
#define pl(x) printf("%lld\n", x)
#define ps(s) printf("%s\n", s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
mt19937_64
rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) {
uniform_int_distribution<int> uid(0, lim - 1);
return uid(rang);
}
int mpow(int base, int exp);
void ipgraph(int n, int m);
void dfs(int u, int par);
const int mod = 1'000'000'007;
const int N = 3e5, M = N;
//=======================
/*Advanced
vi g[N];
int a[N];
*/
void solve() { int i, j, n, m; }
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
srand(chrono::high_resolution_clock::now().time_since_epoch().count());
int k;
cin >> k;
k = k % mod;
int sequ = 7 % k;
int i = 1;
while (i != 1e7) {
if (sequ == 0) {
cout << i;
return 0;
}
sequ = (sequ * 10 + 7) % k;
i++;
}
cout << -1;
return 0;
}
/*
int mpow(int base, int exp) {
base %= mod;
int result = 1;
while (exp > 0) {
if (exp & 1) result = ((ll)result * base) % mod;
base = ((ll)base * base) % mod;
exp >>= 1;
}
return result;
}
void ipgraph(int n, int m){
int i, u, v;
while(m--){
cin>>u>>v;
u--, v--;
g[u].pb(v);
g[v].pb(u);
}
}
void dfs(int u, int par){
for(int v:g[u]){
if (v == par) continue;
dfs(v, u);
}
}
*/
| replace | 58 | 59 | 58 | 59 | TLE | |
p02596 | C++ | Time Limit Exceeded | #include "bits/stdc++.h"
using namespace std;
using ll = long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
#define rep(i, begin, n) for (int i = begin; i < n; i++)
#define repe(i, begin, n) for (int i = begin; i <= n; i++)
#define repr(i, begin, n) for (int i = begin; i > begin - n; i--)
#define repre(i, begin, end) for (int i = begin; i >= end; i--)
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;
}
const int inf = 1000000007;
const int MOD = 1000000007;
const long long INF = 1000000000000000007;
// -------------------------------------------------------
ll K;
int main() {
//
cin >> K;
if (K % 2 == 0) {
cout << -1;
return 0;
}
ll cur = 7 % K;
ll ans = 1;
for (;;) {
if (cur == 0) {
cout << ans;
return 0;
}
cur = (10 % K) * (cur % K) + 7 % K;
cur %= K;
++ans;
if (ans > 100000000) {
cout << -1;
return 0;
}
}
}
| #include "bits/stdc++.h"
using namespace std;
using ll = long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
#define rep(i, begin, n) for (int i = begin; i < n; i++)
#define repe(i, begin, n) for (int i = begin; i <= n; i++)
#define repr(i, begin, n) for (int i = begin; i > begin - n; i--)
#define repre(i, begin, end) for (int i = begin; i >= end; i--)
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;
}
const int inf = 1000000007;
const int MOD = 1000000007;
const long long INF = 1000000000000000007;
// -------------------------------------------------------
ll K;
int main() {
//
cin >> K;
if (K % 2 == 0) {
cout << -1;
return 0;
}
ll cur = 7 % K;
ll ans = 1;
for (;;) {
if (cur == 0) {
cout << ans;
return 0;
}
cur = (10 % K) * (cur % K) + 7 % K;
cur %= K;
++ans;
if (ans > 10000000) {
cout << -1;
return 0;
}
}
}
| replace | 55 | 56 | 55 | 56 | TLE | |
p02596 | C++ | Time Limit Exceeded | // #include<i_am_noob_orz>
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define ull unsigned long long
#define pii pair<int, int>
#define X first
#define Y second
#define mod ((ll)1e9 + 7)
#define pb push_back
#define mp make_pair
#define abs(x) ((x) > 0 ? (x) : (-(x)))
#define F(n) Fi(i, n)
#define Fi(i, n) Fl(i, 0, n)
#define Fl(i, l, n) for (int i = l; i < n; i++)
#define memres(a) memset(a, 0, sizeof(a))
#define all(a) a.begin(), a.end()
#define sz(a) ((int)a.size())
#define ceiling(a, b) (((a) + (b)-1) / (b))
#define endl '\n'
#define bit_count(x) __builtin_popcount((x))
#define ykh mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// #define LOCAL
#ifdef LOCAL
#define debug(a) cerr << #a << " " << a << endl;
#else
#define debug(a)
#endif
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
bool ar[1000010];
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, ct = 0, p = 7, s = 0;
cin >> n;
bool tag = true;
while (1) {
s += p;
ct++;
s = s % n;
if (s == 0)
break;
if (ar[s]) {
tag = false;
break;
}
p = (p * 10) % n;
}
if (tag)
cout << ct << endl;
else
cout << "-1" << endl;
return 0;
}
| // #include<i_am_noob_orz>
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define ull unsigned long long
#define pii pair<int, int>
#define X first
#define Y second
#define mod ((ll)1e9 + 7)
#define pb push_back
#define mp make_pair
#define abs(x) ((x) > 0 ? (x) : (-(x)))
#define F(n) Fi(i, n)
#define Fi(i, n) Fl(i, 0, n)
#define Fl(i, l, n) for (int i = l; i < n; i++)
#define memres(a) memset(a, 0, sizeof(a))
#define all(a) a.begin(), a.end()
#define sz(a) ((int)a.size())
#define ceiling(a, b) (((a) + (b)-1) / (b))
#define endl '\n'
#define bit_count(x) __builtin_popcount((x))
#define ykh mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// #define LOCAL
#ifdef LOCAL
#define debug(a) cerr << #a << " " << a << endl;
#else
#define debug(a)
#endif
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
bool ar[1000010];
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, ct = 0, p = 7, s = 0;
cin >> n;
bool tag = true;
while (1) {
s += p;
ct++;
s = s % n;
if (s == 0)
break;
if (ar[s]) {
tag = false;
break;
}
ar[s] = true;
p = (p * 10) % n;
}
if (tag)
cout << ct << endl;
else
cout << "-1" << endl;
return 0;
}
| insert | 51 | 51 | 51 | 52 | TLE | |
p02596 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cassert>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
typedef long long int llint;
typedef pair<int, int> pint;
typedef pair<llint, llint> pllint;
typedef vector<int> vint;
typedef vector<llint> vllint;
typedef vector<pint> vpint;
typedef vector<string> vstring;
typedef vector<pair<llint, llint>> vpllint;
typedef vector<vector<int>> vvint;
typedef vector<vector<llint>> vvllint;
typedef vector<vector<pint>> vvpint;
typedef vector<bool> vbool;
typedef vector<vbool> vvbool;
typedef vector<vpllint> vvpllint;
#define rep(i, n) for (int i = 0; i < n; i++)
#define drep(i, n) for (int i = n - 1; 0 <= i; i--)
#define yes(ans) \
if (ans) \
cout << "yes" << endl; \
else \
cout << "no" << endl;
#define Yes(ans) \
if (ans) \
cout << "Yes" << endl; \
else \
cout << "No" << endl;
#define YES(ans) \
if (ans) \
cout << "YES" << endl; \
else \
cout << "NO" << endl;
#define POSSIBLE(ans) \
if (ans) \
cout << "POSSIBLE" << endl; \
else \
cout << "IMPOSSIBLE" << endl;
#define Pi 3.1415926535897932384626
#define mod llint(1e9 + 7)
#define Inf 2147483647
#define llInf 9223372036854775807
#define all(x) x.begin(), x.end()
#define pb push_back
#define isin(n, i) 0 <= i &&i < n
class UnionFind {
public:
// 親の番号を格納する。親だった場合は-(その集合のサイズ)
vector<int> Parent;
// 作るときはParentの値を全て-1にする
// こうすると全てバラバラになる
UnionFind(int N) { Parent = vector<int>(N, -1); }
// Aがどのグループに属しているか調べる
int root(int A) {
if (Parent[A] < 0)
return A;
return Parent[A] = root(Parent[A]);
}
// 自分のいるグループの頂点数を調べる
int size(int A) {
return -Parent[root(A)]; // 親をとってきたい
}
// AとBをくっ付ける
bool connect(int A, int B) {
// AとBを直接つなぐのではなく、root(A)にroot(B)をくっつける
A = root(A);
B = root(B);
if (A == B) {
// すでにくっついてるからくっ付けない
return false;
}
// 大きい方(A)に小さいほう(B)をくっ付けたい
// 大小が逆だったらひっくり返しちゃう。
if (size(A) < size(B))
swap(A, B);
// Aのサイズを更新する
Parent[A] += Parent[B];
// Bの親をAに変更する
Parent[B] = A;
return true;
}
};
// セグ木・0-indexed・非再帰・(大きさ・単位元・関数)で初期化
template <typename T> struct SegTree {
// 比較関数の型
using F = function<T(T, T)>;
// 二分木を配列で表したもの
vector<T> seg;
// 木の半分の大きさ
int siz;
// 単位元
const T unit;
// 比較する関数
const F f;
// 大きさn、unit(単位元)、f(モノイド)でsegtreeを初期化する
SegTree(int n, const T unit, const F f) : unit(unit), f(f) {
siz = 1;
while (siz < n)
siz <<= 1;
seg.assign(siz * 2 - 1, unit);
siz--;
}
// k番目にtを入力
void set(int k, const T &t) { seg[k + siz] = t; }
// fによって木を構築する
void build() {
for (int i = siz - 1; 0 <= i; i--) {
seg[i] = f(seg[i * 2 + 1], seg[i * 2 + 2]);
}
}
T operator[](const int i) { return seg[i + siz]; }
// k番目をxに更新する
void update(int k, T x) {
k += siz;
seg[k] = x;
while (0 < k) {
k = (k - 1) >> 1;
seg[k] = f(seg[k * 2 + 1], seg[k * 2 + 2]);
}
}
//[a,b)について、fした結果を返す
// 半開区域のためa以上b未満の位置を指す
T query(int a, int b) {
T l = unit, r = unit;
for (a += siz, b += siz; a < b; a >>= 1, b >>= 1) {
if (!(a & 1))
l = f(l, seg[a++]);
if (!(b & 1))
r = f(seg[--b], r);
}
return f(l, r);
}
};
// aとbの最大公約数を求めるよ
long long GCD(long long a, long long b) {
if (b == 0)
return a;
else
return GCD(b, a % b);
}
// 返り値: a と b の最大公約数
// ax + by = gcd(a, b) を満たす (x, y) が格納される
long long extGCD(long long a, long long b, long long &x, long long &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
long long d = extGCD(b, a % b, y, x);
y -= a / b * x;
return d;
}
// mod. m での a の逆元 a^{-1} を計算する
long long modinv(long long a, long long m) {
long long b = m, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
u %= m;
if (u < 0)
u += m;
return u;
}
// nCrをmで割った余りを求める
llint nCr(llint n, llint r, llint m) {
llint ans = 1;
for (llint i = 0; i < r; i++) {
ans *= n - i;
ans %= m;
}
for (llint i = 1; i <= r; i++) {
ans *= modinv(i, m);
ans %= m;
}
return ans;
}
// aのb乗をmで割った余りを求める
llint power(llint a, llint b, llint m) {
if (b == 1)
return a;
if (b == 0)
return 1;
llint tmp = power(a, (llint)b / 2, m);
tmp *= tmp;
tmp %= m;
if (b % 2 == 1) {
tmp *= a;
tmp %= m;
}
return tmp;
}
// bitを表すsub,要素数を表すlength
bool next_combination(llint &sub, int length) {
llint x = sub & -sub, y = sub + x;
sub = (((sub & ~y) / x) >> 1) | y;
return sub < (llint)(1 << (llint)length);
}
void Zalgorithm(string &s, vint &a) {
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() && a[k] + k < j) {
a[i + k] += a[k];
k++;
}
i += k;
j -= k;
}
return;
}
int main() {
int k;
cin >> k;
int now = 7 % k;
int ans = 1;
while (now != 0) {
now *= 10;
now += 7;
now %= k;
ans++;
if (now == 7 % k) {
cout << -1 << endl;
return 0;
}
}
cout << ans << endl;
return 0;
} | #include <algorithm>
#include <bitset>
#include <cassert>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
typedef long long int llint;
typedef pair<int, int> pint;
typedef pair<llint, llint> pllint;
typedef vector<int> vint;
typedef vector<llint> vllint;
typedef vector<pint> vpint;
typedef vector<string> vstring;
typedef vector<pair<llint, llint>> vpllint;
typedef vector<vector<int>> vvint;
typedef vector<vector<llint>> vvllint;
typedef vector<vector<pint>> vvpint;
typedef vector<bool> vbool;
typedef vector<vbool> vvbool;
typedef vector<vpllint> vvpllint;
#define rep(i, n) for (int i = 0; i < n; i++)
#define drep(i, n) for (int i = n - 1; 0 <= i; i--)
#define yes(ans) \
if (ans) \
cout << "yes" << endl; \
else \
cout << "no" << endl;
#define Yes(ans) \
if (ans) \
cout << "Yes" << endl; \
else \
cout << "No" << endl;
#define YES(ans) \
if (ans) \
cout << "YES" << endl; \
else \
cout << "NO" << endl;
#define POSSIBLE(ans) \
if (ans) \
cout << "POSSIBLE" << endl; \
else \
cout << "IMPOSSIBLE" << endl;
#define Pi 3.1415926535897932384626
#define mod llint(1e9 + 7)
#define Inf 2147483647
#define llInf 9223372036854775807
#define all(x) x.begin(), x.end()
#define pb push_back
#define isin(n, i) 0 <= i &&i < n
class UnionFind {
public:
// 親の番号を格納する。親だった場合は-(その集合のサイズ)
vector<int> Parent;
// 作るときはParentの値を全て-1にする
// こうすると全てバラバラになる
UnionFind(int N) { Parent = vector<int>(N, -1); }
// Aがどのグループに属しているか調べる
int root(int A) {
if (Parent[A] < 0)
return A;
return Parent[A] = root(Parent[A]);
}
// 自分のいるグループの頂点数を調べる
int size(int A) {
return -Parent[root(A)]; // 親をとってきたい
}
// AとBをくっ付ける
bool connect(int A, int B) {
// AとBを直接つなぐのではなく、root(A)にroot(B)をくっつける
A = root(A);
B = root(B);
if (A == B) {
// すでにくっついてるからくっ付けない
return false;
}
// 大きい方(A)に小さいほう(B)をくっ付けたい
// 大小が逆だったらひっくり返しちゃう。
if (size(A) < size(B))
swap(A, B);
// Aのサイズを更新する
Parent[A] += Parent[B];
// Bの親をAに変更する
Parent[B] = A;
return true;
}
};
// セグ木・0-indexed・非再帰・(大きさ・単位元・関数)で初期化
template <typename T> struct SegTree {
// 比較関数の型
using F = function<T(T, T)>;
// 二分木を配列で表したもの
vector<T> seg;
// 木の半分の大きさ
int siz;
// 単位元
const T unit;
// 比較する関数
const F f;
// 大きさn、unit(単位元)、f(モノイド)でsegtreeを初期化する
SegTree(int n, const T unit, const F f) : unit(unit), f(f) {
siz = 1;
while (siz < n)
siz <<= 1;
seg.assign(siz * 2 - 1, unit);
siz--;
}
// k番目にtを入力
void set(int k, const T &t) { seg[k + siz] = t; }
// fによって木を構築する
void build() {
for (int i = siz - 1; 0 <= i; i--) {
seg[i] = f(seg[i * 2 + 1], seg[i * 2 + 2]);
}
}
T operator[](const int i) { return seg[i + siz]; }
// k番目をxに更新する
void update(int k, T x) {
k += siz;
seg[k] = x;
while (0 < k) {
k = (k - 1) >> 1;
seg[k] = f(seg[k * 2 + 1], seg[k * 2 + 2]);
}
}
//[a,b)について、fした結果を返す
// 半開区域のためa以上b未満の位置を指す
T query(int a, int b) {
T l = unit, r = unit;
for (a += siz, b += siz; a < b; a >>= 1, b >>= 1) {
if (!(a & 1))
l = f(l, seg[a++]);
if (!(b & 1))
r = f(seg[--b], r);
}
return f(l, r);
}
};
// aとbの最大公約数を求めるよ
long long GCD(long long a, long long b) {
if (b == 0)
return a;
else
return GCD(b, a % b);
}
// 返り値: a と b の最大公約数
// ax + by = gcd(a, b) を満たす (x, y) が格納される
long long extGCD(long long a, long long b, long long &x, long long &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
long long d = extGCD(b, a % b, y, x);
y -= a / b * x;
return d;
}
// mod. m での a の逆元 a^{-1} を計算する
long long modinv(long long a, long long m) {
long long b = m, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
u %= m;
if (u < 0)
u += m;
return u;
}
// nCrをmで割った余りを求める
llint nCr(llint n, llint r, llint m) {
llint ans = 1;
for (llint i = 0; i < r; i++) {
ans *= n - i;
ans %= m;
}
for (llint i = 1; i <= r; i++) {
ans *= modinv(i, m);
ans %= m;
}
return ans;
}
// aのb乗をmで割った余りを求める
llint power(llint a, llint b, llint m) {
if (b == 1)
return a;
if (b == 0)
return 1;
llint tmp = power(a, (llint)b / 2, m);
tmp *= tmp;
tmp %= m;
if (b % 2 == 1) {
tmp *= a;
tmp %= m;
}
return tmp;
}
// bitを表すsub,要素数を表すlength
bool next_combination(llint &sub, int length) {
llint x = sub & -sub, y = sub + x;
sub = (((sub & ~y) / x) >> 1) | y;
return sub < (llint)(1 << (llint)length);
}
void Zalgorithm(string &s, vint &a) {
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() && a[k] + k < j) {
a[i + k] += a[k];
k++;
}
i += k;
j -= k;
}
return;
}
int main() {
int k;
cin >> k;
int now = 7 % k;
int ans = 1;
while (now != 0) {
now *= 10;
now += 7;
now %= k;
ans++;
if (ans > k) {
cout << -1 << endl;
return 0;
}
}
cout << ans << endl;
return 0;
} | replace | 271 | 272 | 271 | 272 | TLE | |
p02596 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define LI list<int>
#define D(a) (double)(a)
#define vct vector
#define vi vct<int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vvi vct<vi>
#define vll vct<ll>
#define vvll vct<vll>
#define vpii vct<pii>
#define vpll vct<pll>
#define vb vct<bool>
#define vs vector<string>
#define all(a) a.begin(), a.end()
#define allr(a) a.rbegin(), a.rend()
#define mp(a, b) make_pair(a, b)
#define pb push_back
#define ff first
#define ss second
#define bg begin()
#define UNIQUE(X) (X).erase(unique(all(X)), (X).end())
#define ft cout << "for test" << endl;
#define read(v, a, n) \
for (int i = a; i < n; i++) \
cin >> v[i];
#define For(a, n, in) for (int i = a; i < n; i += in)
#define print(v) \
for (auto it : v) \
cout << it << ' '; \
cout << endl;
#define PI acos(-1)
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define FIO \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define t_c \
int test, cs = 1; \
cin >> test; \
while (test--)
///................function.....................///
#define siz(s) (int)(s.size())
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define mod 999999937
int main() {
/// freopen("in.txt","r",stdin);
/// freopen("output.txt","w",stdout);
ll k;
cin >> k;
int res = 0, cnt = 0;
while (1) {
res = res * 10 + 7;
cnt++;
if (res % k == 0)
break;
res %= k;
}
cout << cnt << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define LI list<int>
#define D(a) (double)(a)
#define vct vector
#define vi vct<int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vvi vct<vi>
#define vll vct<ll>
#define vvll vct<vll>
#define vpii vct<pii>
#define vpll vct<pll>
#define vb vct<bool>
#define vs vector<string>
#define all(a) a.begin(), a.end()
#define allr(a) a.rbegin(), a.rend()
#define mp(a, b) make_pair(a, b)
#define pb push_back
#define ff first
#define ss second
#define bg begin()
#define UNIQUE(X) (X).erase(unique(all(X)), (X).end())
#define ft cout << "for test" << endl;
#define read(v, a, n) \
for (int i = a; i < n; i++) \
cin >> v[i];
#define For(a, n, in) for (int i = a; i < n; i += in)
#define print(v) \
for (auto it : v) \
cout << it << ' '; \
cout << endl;
#define PI acos(-1)
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define FIO \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define t_c \
int test, cs = 1; \
cin >> test; \
while (test--)
///................function.....................///
#define siz(s) (int)(s.size())
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define mod 999999937
int main() {
/// freopen("in.txt","r",stdin);
/// freopen("output.txt","w",stdout);
ll k;
cin >> k;
int res = 0, cnt = 0;
if (k % 2 == 0 || k % 5 == 0) {
cout << -1 << endl;
return 0;
}
while (1) {
res = res * 10 + 7;
cnt++;
if (res % k == 0)
break;
res %= k;
}
cout << cnt << endl;
}
| insert | 55 | 55 | 55 | 59 | TLE | |
p02596 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#include <iostream>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
#define ll long long int
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n;
cin >> n;
if (n % 2 == 0) {
cout << "-1" << endl;
return 0;
}
ll count = 1, num = 7;
while (num % n) {
num %= n;
num *= 10;
num += 7;
count++;
// cout<<num<<endl;
}
cout << count << endl;
return 0;
}
| #include <bits/stdc++.h>
#include <iostream>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
#define ll long long int
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n;
cin >> n;
if (n % 2 == 0) {
cout << "-1" << endl;
return 0;
}
ll count = 1, num = 7;
while (num % n) {
num %= n;
num *= 10;
num += 7;
count++;
if (count > n) {
cout << "-1" << endl;
return 0;
}
// cout<<num<<endl;
}
cout << count << endl;
return 0;
}
| insert | 25 | 25 | 25 | 29 | TLE | |
p02596 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define fio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
#define int long long
#define endl '\n'
#define pb push_back
#define debug(x) cout << #x << " " << x << endl;
const int mod = 1e18 + 7;
const int MAX = 1e5 + 9;
const double pi = 3.141592653589793238460;
using namespace std;
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
#else
#define trace(...)
#endif
bool is_pal(string s) {
return equal(s.begin(), s.begin() + s.size() / 2, s.rbegin()) ? 1 : 0;
}
int power(int x, int y, int res = 1) {
x %= mod;
for (; y; y >>= 1LL) {
if (y & 1LL) {
res = res * x % mod;
}
x = x * x % mod;
}
return res;
}
int modInv(int x) { return (power(x, mod - 2) + mod) % mod; }
int fact[MAX], inv[MAX];
void compFact(void) {
fact[0] = fact[1] = inv[1] = inv[0] = 1;
for (int i = 1; i < MAX; i++) {
fact[i] = (fact[i - 1] * i % mod + mod) % mod;
}
}
void compInv(void) {
for (int i = 2; i < MAX; ++i) {
inv[i] = (mod - (mod / i) * inv[mod % i] % mod + mod) % mod;
}
}
int nCr(int n, int r) {
if (n < r || n < 0) {
return 0;
}
return ((((fact[n] % mod) * (inv[fact[n - r]] % mod)) % mod) *
(inv[fact[r]] % mod) +
mod) %
mod;
}
int factMod(int n, int res = 1) {
while (n > 1) {
res = (res * ((n / mod) % 2 ? mod - 1 : 1)) % mod;
for (int i = 2; i <= n % mod; ++i)
res = (res * i) % mod;
n /= mod;
}
return res % mod;
}
int max(int x, int y) { return x > y ? x : y; }
int min(int x, int y) { return x < y ? x : y; }
int lcm(int x, int y) { return (x * y) / __gcd(x, y); }
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int N;
bool under_bound(int x, int y) { return x >= 0 && y <= 0 && x < N && y < N; }
bool prime[MAX];
void SieveOfEratosthenes(int n) {
memset(prime, true, sizeof(prime));
for (int p = 2; p * p < n; p++) {
if (prime[p] == true) {
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
}
multiset<int> A;
void primeFact(int n) {
while (n % 2 == 0) {
A.insert(2);
n = n / 2;
}
for (int i = 3; i <= sqrt(n); i = i + 2) {
while (n % i == 0) {
A.insert(i);
n = n / i;
}
}
if (n > 2)
A.insert(n);
}
int spf[MAX];
void SieveOfEuler() {
spf[1] = 1;
for (int i = 2; i < MAX; i++)
spf[i] = i;
for (int i = 4; i < MAX; i += 2)
spf[i] = 2;
for (int i = 3; i * i < MAX; i++) {
if (spf[i] == i) {
for (int j = i * i; j < MAX; j += i)
if (spf[j] == j)
spf[j] = i;
}
}
}
map<pair<int, int>, int> factFreq;
void getFactorization(int x) {
while (x > 1) {
int pt = spf[x], f = 0;
while (x % pt == 0) {
x /= pt;
f++;
factFreq[{pt, f}]++;
}
}
}
int n, k, z;
int a[MAX];
map<vector<int>, int> save;
int go(int pos, int left, int now) {
if (now == -1) {
return 0;
}
vector<int> key = {pos, left, now};
if (save[key] != NULL)
return save[key];
int ans = 0;
if (pos == 1) {
ans += (a[pos] + go(pos + 1, left, now - 1));
} else if (pos == n) {
if (left > 0)
ans += max(ans, a[pos] + go(pos - 1, left - 1, now - 1));
else
return a[pos];
} else {
if (left > 0)
ans = max(ans, a[pos] + go(pos - 1, left - 1, now - 1));
ans = max(ans, a[pos] + go(pos + 1, left, now - 1));
}
return save[key] = ans;
}
int ans = 1;
void solve(void) {
int n;
cin >> n;
if (!(n & 1)) {
cout << -1;
return;
}
int now = 7;
while (now % n != 0) {
now = now * 10 + 7;
now %= n;
ans++;
}
cout << ans;
}
signed main() {
fio;
int t = 1;
// cin >> t;
while (t--)
solve();
return 0;
} | #include <bits/stdc++.h>
#define fio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
#define int long long
#define endl '\n'
#define pb push_back
#define debug(x) cout << #x << " " << x << endl;
const int mod = 1e18 + 7;
const int MAX = 1e5 + 9;
const double pi = 3.141592653589793238460;
using namespace std;
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
#else
#define trace(...)
#endif
bool is_pal(string s) {
return equal(s.begin(), s.begin() + s.size() / 2, s.rbegin()) ? 1 : 0;
}
int power(int x, int y, int res = 1) {
x %= mod;
for (; y; y >>= 1LL) {
if (y & 1LL) {
res = res * x % mod;
}
x = x * x % mod;
}
return res;
}
int modInv(int x) { return (power(x, mod - 2) + mod) % mod; }
int fact[MAX], inv[MAX];
void compFact(void) {
fact[0] = fact[1] = inv[1] = inv[0] = 1;
for (int i = 1; i < MAX; i++) {
fact[i] = (fact[i - 1] * i % mod + mod) % mod;
}
}
void compInv(void) {
for (int i = 2; i < MAX; ++i) {
inv[i] = (mod - (mod / i) * inv[mod % i] % mod + mod) % mod;
}
}
int nCr(int n, int r) {
if (n < r || n < 0) {
return 0;
}
return ((((fact[n] % mod) * (inv[fact[n - r]] % mod)) % mod) *
(inv[fact[r]] % mod) +
mod) %
mod;
}
int factMod(int n, int res = 1) {
while (n > 1) {
res = (res * ((n / mod) % 2 ? mod - 1 : 1)) % mod;
for (int i = 2; i <= n % mod; ++i)
res = (res * i) % mod;
n /= mod;
}
return res % mod;
}
int max(int x, int y) { return x > y ? x : y; }
int min(int x, int y) { return x < y ? x : y; }
int lcm(int x, int y) { return (x * y) / __gcd(x, y); }
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int N;
bool under_bound(int x, int y) { return x >= 0 && y <= 0 && x < N && y < N; }
bool prime[MAX];
void SieveOfEratosthenes(int n) {
memset(prime, true, sizeof(prime));
for (int p = 2; p * p < n; p++) {
if (prime[p] == true) {
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
}
multiset<int> A;
void primeFact(int n) {
while (n % 2 == 0) {
A.insert(2);
n = n / 2;
}
for (int i = 3; i <= sqrt(n); i = i + 2) {
while (n % i == 0) {
A.insert(i);
n = n / i;
}
}
if (n > 2)
A.insert(n);
}
int spf[MAX];
void SieveOfEuler() {
spf[1] = 1;
for (int i = 2; i < MAX; i++)
spf[i] = i;
for (int i = 4; i < MAX; i += 2)
spf[i] = 2;
for (int i = 3; i * i < MAX; i++) {
if (spf[i] == i) {
for (int j = i * i; j < MAX; j += i)
if (spf[j] == j)
spf[j] = i;
}
}
}
map<pair<int, int>, int> factFreq;
void getFactorization(int x) {
while (x > 1) {
int pt = spf[x], f = 0;
while (x % pt == 0) {
x /= pt;
f++;
factFreq[{pt, f}]++;
}
}
}
int n, k, z;
int a[MAX];
map<vector<int>, int> save;
int go(int pos, int left, int now) {
if (now == -1) {
return 0;
}
vector<int> key = {pos, left, now};
if (save[key] != NULL)
return save[key];
int ans = 0;
if (pos == 1) {
ans += (a[pos] + go(pos + 1, left, now - 1));
} else if (pos == n) {
if (left > 0)
ans += max(ans, a[pos] + go(pos - 1, left - 1, now - 1));
else
return a[pos];
} else {
if (left > 0)
ans = max(ans, a[pos] + go(pos - 1, left - 1, now - 1));
ans = max(ans, a[pos] + go(pos + 1, left, now - 1));
}
return save[key] = ans;
}
int ans = 1;
void solve(void) {
int n;
cin >> n;
if (!(n & 1) || n % 5 == 0) {
cout << -1;
return;
}
int now = 7;
while (now % n != 0) {
now = now * 10 + 7;
now %= n;
ans++;
}
cout << ans;
}
signed main() {
fio;
int t = 1;
// cin >> t;
while (t--)
solve();
return 0;
} | replace | 194 | 195 | 194 | 195 | TLE | |
p02596 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
bool checkValue(long long v) {
while (v > 0) {
if ((v % 7 != 0) || (v % 10 == 0))
return false;
v /= 10;
}
return true;
}
int main() {
long long x;
cin >> x;
unsigned long long value = 7;
vector<long long> result;
long long cnt = 0;
while (value > 0) {
cnt++;
// cout << value%x << endl;
value = value % x;
if (value == 0) {
cout << cnt << endl;
return 0;
}
if (result.size() != 0) {
for (long long i = 0; i < result.size(); i++) {
if (result[i] == value) {
cout << -1 << endl;
return 0;
}
}
}
result.push_back(value);
value = value * 10 + 7;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
bool checkValue(long long v) {
while (v > 0) {
if ((v % 7 != 0) || (v % 10 == 0))
return false;
v /= 10;
}
return true;
}
int main() {
long long x;
cin >> x;
unsigned long long value = 7;
vector<long long> result;
long long cnt = 0;
while (value > 0) {
cnt++;
// cout << value%x << endl;
value = value % x;
if (value == 0) {
cout << cnt << endl;
return 0;
}
// if(result.size()%10000!=0){
// for(long long i = 0; i < result.size(); i++){
// if(result[i]==value){
// cout << -1 << endl;
// return 0;
//}
//}
//}
if (result.size() >= 10000000) {
cout << -1 << endl;
return 0;
}
result.push_back(value);
value = value * 10 + 7;
}
return 0;
} | replace | 24 | 31 | 24 | 35 | TLE | |
p02596 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// freopen("input.txt","r",stdin);
long long n, i = 1, k = 7;
cin >> n;
while (1) {
if (n % 2 == 0) {
cout << -1 << endl;
break;
} else {
if (k % n) {
k = 10 * (k % n) + 7;
} else {
cout << i << endl;
break;
}
i++;
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// freopen("input.txt","r",stdin);
long long n, i = 1, k = 7;
cin >> n;
while (1) {
if (n % 2 == 0 || n % 5 == 0) {
cout << -1 << endl;
break;
} else {
if (k % n) {
k = 10 * (k % n) + 7;
} else {
cout << i << endl;
break;
}
i++;
}
}
return 0;
}
| replace | 13 | 14 | 13 | 14 | TLE | |
p02596 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e18;
const ll MOD = 1000000007;
const ll MX = 0; // 最大値
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define ALL(x) (x).begin(), (x).end()
#define MAX(x) *max_element(ALL(x))
#define PB push_back
#define F first
#define S second
int main() {
ll n;
cin >> n;
ll res = 7 % n, cnt = 1;
while (res != 0) {
cnt++;
res = res * 10 + 7;
res %= n;
}
cout << cnt << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e18;
const ll MOD = 1000000007;
const ll MX = 0; // 最大値
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define ALL(x) (x).begin(), (x).end()
#define MAX(x) *max_element(ALL(x))
#define PB push_back
#define F first
#define S second
int main() {
ll n;
cin >> n;
ll res = 7 % n, cnt = 1;
if (n % 5 == 0 || n % 2 == 0) {
cout << -1 << endl;
return 0;
}
while (res != 0) {
cnt++;
res = res * 10 + 7;
res %= n;
}
cout << cnt << endl;
return 0;
} | insert | 19 | 19 | 19 | 25 | TLE | |
p02596 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
int64_t n;
cin >> n;
int64_t L, s = 1;
if (n % 7 == 0) {
L = 9 * n / 7;
s = 10 % L;
for (int64_t i = 1;; i++) {
if (s % L == 1) {
cout << i << endl;
break;
}
s = (s % L) * 10;
}
} else {
L = 9 * n;
s = 10 % L;
for (int64_t i = 1;; i++) {
if (s % L == 1) {
cout << i << endl;
break;
}
s = (s % L) * 10;
}
}
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int64_t n;
cin >> n;
int64_t L, s = 1;
if (n % 2 == 0 || n % 5 == 0) {
cout << -1 << endl;
} else if (n % 7 == 0) {
L = 9 * n / 7;
s = 10 % L;
for (int64_t i = 1;; i++) {
if (s % L == 1) {
cout << i << endl;
break;
}
s = (s % L) * 10;
}
} else {
L = 9 * n;
s = 10 % L;
for (int64_t i = 1;; i++) {
if (s % L == 1) {
cout << i << endl;
break;
}
s = (s % L) * 10;
}
}
}
| replace | 6 | 7 | 6 | 9 | TLE | |
p02596 | C++ | Time Limit Exceeded | #include <iostream>
using namespace std;
int main() {
int k;
cin >> k;
if (k % 2 == 0) {
cout << -1 << endl;
return 0;
}
int n = 0;
int k1 = k;
int num = 0;
int des = 1;
while (k1) {
n++;
num = 7 * des + num;
des *= 10;
k1 /= 10;
}
// cout<<des<<endl;
int count = 0;
while (1) {
num = num % k;
if (num == 0) {
// cout<<num<<endl;
cout << n << endl;
return 0;
} else {
n++;
num = num * 10 + 7;
}
}
} | #include <iostream>
using namespace std;
int main() {
int k;
cin >> k;
if (k % 2 == 0 || k % 5 == 0) {
cout << -1 << endl;
return 0;
}
int n = 0;
int k1 = k;
int num = 0;
int des = 1;
while (k1) {
n++;
num = 7 * des + num;
des *= 10;
k1 /= 10;
}
// cout<<des<<endl;
int count = 0;
while (1) {
num = num % k;
if (num == 0) {
// cout<<num<<endl;
cout << n << endl;
return 0;
} else {
n++;
num = num * 10 + 7;
}
}
} | replace | 6 | 7 | 6 | 7 | TLE | |
p02596 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
int main() {
int k;
cin >> k;
if (k % 7 == 0) {
k = k / 7;
}
if (gcd(10, k) != 1) {
cout << -1 << endl;
}
long long int l = 1, v = 1 % k;
while (v != 0) {
l++;
v = (v * 10 + 1) % k;
}
cout << l << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
int main() {
int k;
cin >> k;
if (k % 7 == 0) {
k = k / 7;
}
if (gcd(10, k) != 1) {
cout << -1 << endl;
return 0;
}
long long int l = 1, v = 1 % k;
while (v != 0) {
l++;
v = (v * 10 + 1) % k;
}
cout << l << endl;
return 0;
} | insert | 19 | 19 | 19 | 20 | TLE |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.