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 |
|---|---|---|---|---|---|---|---|---|---|---|---|
p02624 | C++ | Runtime Error | #include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
#define INF ((1 << 30) - 1 + (1 << 30))
#define INFLL (1LL << 60)
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < (n); ++i)
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
int main() {
const int sz = 1e6;
vector<int> bkt(sz + 1, 2);
bkt[0] = 0;
bkt[1] = 1;
for (int i = 2; i <= sz / 2; ++i) {
for (int j = i * 2; j < sz + 1; j += i) {
++bkt[j];
}
}
int N;
cin >> N;
ll ans = 0;
rep(i, N + 1) { ans += (ll)i * bkt[i]; }
cout << ans << endl;
return 0;
}
| #include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
#define INF ((1 << 30) - 1 + (1 << 30))
#define INFLL (1LL << 60)
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < (n); ++i)
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
int main() {
const int sz = 1e7;
vector<int> bkt(sz + 1, 2);
bkt[0] = 0;
bkt[1] = 1;
for (int i = 2; i <= sz / 2; ++i) {
for (int j = i * 2; j < sz + 1; j += i) {
++bkt[j];
}
}
int N;
cin >> N;
ll ans = 0;
rep(i, N + 1) { ans += (ll)i * bkt[i]; }
cout << ans << endl;
return 0;
}
| replace | 32 | 33 | 32 | 33 | 0 | |
p02624 | C++ | Time Limit Exceeded | #include <iostream>
#define ll long long
using namespace std;
int main() {
int N;
cin >> N;
ll ans = 0;
for (int i = 1; i <= N; i++) {
for (int j = i; i <= N; j += i) {
ans += j;
}
}
cout << ans << endl;
return 0;
} | #include <iostream>
#define ll long long
using namespace std;
int main() {
int N;
cin >> N;
ll ans = 0;
for (int i = 1; i <= N; i++) {
for (int j = i; j <= N; j += i) {
ans += j;
}
}
cout << ans << endl;
return 0;
}
| replace | 10 | 11 | 10 | 11 | TLE | |
p02624 | C++ | Time Limit Exceeded |
///***### بسم الله الرحمن الرحيم ###***///
/*
Author : Borhan Uddin Rafi
University : Begum Rokeya University,Rangpur.
Date : 27 June,2020;
*/
///************************Start Of Template*************************///
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#define Read_Fast \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define PI acos(-1)
#define all(a) a.begin(), a.end()
#define f first
#define s second
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define mp make_pair
#define f0(i, n) for (i = 0; i < n; i++)
#define f1(i, n) for (i = 1; i < n; i++)
#define f1n(i, n) for (i = 1; i <= n; i++)
#define szf sizeof()
#define sz size()
#define ln length()
#define mem(x, r) memset(x, r, sizeof(x))
using namespace std;
using ll = long long;
const int N = 2e5 + 5;
string str, ans, rafi, chk;
map<string, string> maap;
queue<int> q;
/*** this solution will not work in terminal because of freopen................
If you close that freopen then its work here.
But before submission you need to open freopen line ***/
//////return sum of all digits///////
map<ll, ll> arr;
int main() {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
Read_Fast;
int t;
// cin>>t;
// while(t--)
// {
ll n, m, i, j, cnt1 = 0, cnt2 = 0, cnt = 0, even = 0, odd = 0, len, k, r, l,
z = 0, x = 0, y = 0, flag = 0, sum = 0;
ll a = 0, b = 0, c = 0, d, ans = 0, zero = 0, fst = 0, snd = 0, lst = 0,
one = 0, mn = INT_MAX, mx = INT_MIN;
char ch;
int h1, h2, m1, m2, h, time;
string str, store = "";
cin >> n;
// map<ll,ll>arr;
for (ll i = 1; i <= n; i++) {
for (ll j = i; j <= n; j = j + i) {
arr[j]++;
}
}
for (ll i = 1; i <= n; i++) {
sum = sum + (i * arr[i]);
}
cout << sum << endl;
// }
return 0;
///***### ٱلْحَمْدُ لِلَّٰهِ ###***///
}
|
///***### بسم الله الرحمن الرحيم ###***///
/*
Author : Borhan Uddin Rafi
University : Begum Rokeya University,Rangpur.
Date : 27 June,2020;
*/
///************************Start Of Template*************************///
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
#define Read_Fast \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define PI acos(-1)
#define all(a) a.begin(), a.end()
#define f first
#define s second
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define mp make_pair
#define f0(i, n) for (i = 0; i < n; i++)
#define f1(i, n) for (i = 1; i < n; i++)
#define f1n(i, n) for (i = 1; i <= n; i++)
#define szf sizeof()
#define sz size()
#define ln length()
#define mem(x, r) memset(x, r, sizeof(x))
using namespace std;
using ll = long long;
const int N = 2e5 + 5;
string str, ans, rafi, chk;
map<string, string> maap;
queue<int> q;
/*** this solution will not work in terminal because of freopen................
If you close that freopen then its work here.
But before submission you need to open freopen line ***/
//////return sum of all digits///////
int arr[10000004];
int main() {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
Read_Fast;
int t;
// cin>>t;
// while(t--)
// {
ll n, m, i, j, cnt1 = 0, cnt2 = 0, cnt = 0, even = 0, odd = 0, len, k, r, l,
z = 0, x = 0, y = 0, flag = 0, sum = 0;
ll a = 0, b = 0, c = 0, d, ans = 0, zero = 0, fst = 0, snd = 0, lst = 0,
one = 0, mn = INT_MAX, mx = INT_MIN;
char ch;
int h1, h2, m1, m2, h, time;
string str, store = "";
cin >> n;
// map<ll,ll>arr;
for (ll i = 1; i <= n; i++) {
for (ll j = i; j <= n; j = j + i) {
arr[j]++;
}
}
for (ll i = 1; i <= n; i++) {
sum = sum + (i * arr[i]);
}
cout << sum << endl;
// }
return 0;
///***### ٱلْحَمْدُ لِلَّٰهِ ###***///
}
| replace | 50 | 51 | 50 | 51 | TLE | |
p02624 | C++ | Time Limit Exceeded | /*BISMILLAH
THE WHITE WOLF
NO DREAM IS TOO BIG AND NO DREAMER IS TOO SMALL*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<long long> vll;
typedef vector<int> vi;
#define io ios_base::sync_with_stdio(false)
#define pb push_back
#define eb emplace_back
#define mod 1000000007
#define PI 2 * acos(0.0)
#define maxn 10000005
int32_t main() {
io;
int n;
cin >> n;
ll ans = 0;
for (int i = 1; i <= n; i++) {
int di = 0;
for (int j = 1; j * j <= i; j++) {
if (i % j == 0) {
di++;
if (j != i / j)
di++;
}
}
ans += 1LL * i * di;
}
cout << ans;
return 0;
} | /*BISMILLAH
THE WHITE WOLF
NO DREAM IS TOO BIG AND NO DREAMER IS TOO SMALL*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<long long> vll;
typedef vector<int> vi;
#define io ios_base::sync_with_stdio(false)
#define pb push_back
#define eb emplace_back
#define mod 1000000007
#define PI 2 * acos(0.0)
#define maxn 10000005
int32_t main() {
io;
int n;
cin >> n;
ll ans = 0;
vi din(n + 1, 1);
for (int i = 2; i <= n; i++)
for (int j = i; j <= n; j += i)
din[j]++;
for (int i = 1; i <= n; i++)
ans += 1LL * i * din[i];
cout << ans;
return 0;
} | replace | 24 | 35 | 24 | 30 | TLE | |
p02624 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long ll;
ll pass() {
ll k = 0, s = 0;
int n;
cin >> n;
vector<int> a(n + 1, 0);
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= n; j += i) {
a[j]++;
}
}
for (int i = 1; i <= n; i++) {
s += (i * (ll)a[i]);
;
}
cout << s << endl;
}
int main() {
ll n, m, k = 0, s = 0, l = 0, t = 0;
pass();
}
| #include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long ll;
void pass() {
ll k = 0, s = 0;
int n;
cin >> n;
vector<int> a(n + 1, 0);
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= n; j += i) {
a[j]++;
}
}
for (int i = 1; i <= n; i++) {
s += (i * (ll)a[i]);
;
}
cout << s << endl;
}
int main() {
ll n, m, k = 0, s = 0, l = 0, t = 0;
pass();
}
| replace | 4 | 5 | 4 | 5 | 0 | |
p02624 | Python | Time Limit Exceeded | import numpy as np
from numba import njit
n = int(input())
@njit
def f(n):
cnt = np.zeros(n + 1, np.int32)
for i in range(1, n + 1):
cnt[i::i] += 1
return (cnt * np.arange(n + 1)).sum()
ans = f(n)
print(ans)
| import numpy as np
from numba import njit
n = int(input())
@njit
def f(n):
cnt = np.zeros(n + 1, np.int16)
for i in range(1, n + 1):
cnt[i::i] += 1
return (cnt * np.arange(n + 1)).sum()
ans = f(n)
print(ans)
| replace | 8 | 9 | 8 | 9 | TLE | |
p02624 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
int N;
cin >> N;
ll ans = 0;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
if (i % j == 0) {
ans += i;
}
}
}
cout << ans;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
int N;
cin >> N;
ll ans = 0;
for (int i = 1; i <= N; i++) {
ll c = N / i;
ans += c * (c + 1) / 2 * i;
}
cout << ans;
return 0;
} | replace | 11 | 16 | 11 | 13 | TLE | |
p02624 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ll N;
cin >> N;
ll x = 0;
vector<ll> div(10000000000, 0);
for (ll i = 1; i <= N; i++) {
for (ll j = 1; i * j <= N; j++) {
div[i * j]++;
}
}
for (ll k = 1; k <= N; k++) {
x += k * div[k];
}
cout << x << endl;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ll N;
cin >> N;
ll x = 0;
vector<ll> div(10000000, 0);
for (ll i = 1; i <= N; i++) {
for (ll j = 1; i * j <= N; j++) {
div[i * j]++;
}
}
for (ll k = 1; k <= N; k++) {
x += k * div[k];
}
cout << x << endl;
} | replace | 8 | 9 | 8 | 9 | -6 | terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
|
p02624 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define endl "\n"
int main() {
i64 N;
cin >> N;
i64 ans = 1;
vector<map<i64, i64>> cnt(N + 1);
vector<i64> memo(N + 1);
memo[1] = 1;
for (i64 i = 2; i <= N; i++) {
i64 t = i;
for (i64 j = 2; j * j <= i; j++) {
if (i % j != 0)
continue;
cnt[i] = cnt[i / j];
t = j;
break;
}
cnt[i][t]++;
memo[i] = memo[i / t] / (cnt[i][t] == 0 ? 1 : cnt[i][t]) * (cnt[i][t] + 1);
ans += memo[i] * i;
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define endl "\n"
int main() {
i64 N;
cin >> N;
vector<i64> cnt(N + 1);
for (i64 i = 1; i <= N; i++)
for (i64 j = 1; i * j <= N; j++)
cnt[i * j]++;
i64 ans = 0;
for (i64 i = 1; i <= N; i++)
ans += cnt[i] * i;
cout << ans << endl;
return 0;
}
| replace | 8 | 25 | 8 | 15 | TLE | |
p02624 | C++ | Runtime Error | // Problem : D - Sum of Divisors
// Contest : AtCoder - AtCoder Beginner Contest 172
// URL : https://atcoder.jp/contests/abc172/tasks/abc172_d
// Memory Limit : 1024 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define rep() for (LL i = 0; i < n; i++)
#define f(i, s, n) for (LL i = s; i < n; i++)
#define fe(i, s, n) for (LL i = s; i <= n; i++)
#define rfe(i, s, n) for (LL i = s; i >= n; i--)
#define auto(m) for (auto &it : m)
#define f2(it, v) for (it = v.begin(); it != v.end(); ++it)
#define fori(it, x) \
for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)
#define ip(n, a) \
LL n; \
cin >> n; \
LL a[n]; \
rep() { cin >> a[i]; }
#define op(n, a) \
f(i, 0, n) { cout << a[i] << " "; } \
cout << endl;
#define i2p(n, k, a) \
LL n, k; \
cin >> n >> k; \
LL a[n]; \
rep { cin >> a[i]; }
#define TCs(x) \
LL x; \
cin >> x; \
while (x--)
#define TC(x) \
LL x = 1; \
while (x--)
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define mem(a, x) memset(a, x, sizeof(a))
#define Max(x, y, z) max(x, max(y, z))
#define Min(x, y, z) min(x, min(y, z))
#define fastIO \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define ps(x) \
std::cout << std::fixed; \
std::cout << std::setprecision(x);
#define clz(a) \
__builtin_clz(a) // count leading zeroes
// (geeksforgeeks.org/builtin-functions-gcc-compiler/)
#define ctz(a) __builtin_ctz(a) // count trailing zeroes
#define popc(a) \
__builtin_popcount(a) // count set bits (for ints only diff for ll)
#define GCD(A, B) __gcd(A, B)
#define trace1(x) cout << #x << ": " << x << endl
#define trace2(x, y) cout << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z) \
cout << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": " \
<< z << endl
#define trace4(a, b, c, d) \
cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << endl
#define trace5(a, b, c, d, e) \
cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define trace6(a, b, c, d, e, f) \
cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " \
<< #f << ": " << f << endl
// Typedefs:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
typedef string S;
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef vector<long long> VL;
typedef vector<VL> VVL;
typedef vector<string> VS;
typedef vector<char> VC;
typedef pair<LL, LL> PLL;
typedef pair<string, LL> PSL;
typedef pair<LL, string> PLS;
typedef pair<char, LL> PCL;
typedef pair<LL, char> PLC;
typedef pair<string, string> PSS;
typedef pair<char, char> PCC;
typedef vector<PLL> VPLL;
typedef vector<PSL> VPSL;
typedef vector<PLS> VPLS;
typedef vector<PCL> VPCL;
typedef vector<PLC> VPLC;
typedef vector<PSS> VPSS;
typedef vector<PCC> VPCC;
typedef map<LL, LL> MLL;
typedef map<string, LL> MSL;
typedef map<char, LL> MCL;
typedef set<LL> SL;
typedef set<string> SS;
typedef set<char> SC;
int power(LL x, LL y) {
if (y == 0)
return 1;
LL p = power(x, y / 2);
p = (p * p);
return (y % 2 == 0) ? p : (x * p);
}
const long long N = (long long)(1e5 + 1);
const long long N2 = (long long)(1e6 + 1);
const long long MOD = (long long)(1e9 + 7);
const long long INF = (long long)(2e18);
LL a[5000001];
void solve() {
LL n;
cin >> n;
mem(a, 0);
LL x = 1;
LL ans = 0;
while (x <= n / 2) {
for (LL i = x; i <= n; i += x) {
a[i]++;
// trace2(i,a[i]);
}
x++;
}
fe(i, x, n) { a[i]++; }
fe(i, 1, n) {
// trace2(i,a[i]);
ans += i * a[i];
}
cout << ans << endl;
}
int main() {
fastIO TC(t) { solve(); }
return 0;
}
| // Problem : D - Sum of Divisors
// Contest : AtCoder - AtCoder Beginner Contest 172
// URL : https://atcoder.jp/contests/abc172/tasks/abc172_d
// Memory Limit : 1024 MB
// Time Limit : 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define rep() for (LL i = 0; i < n; i++)
#define f(i, s, n) for (LL i = s; i < n; i++)
#define fe(i, s, n) for (LL i = s; i <= n; i++)
#define rfe(i, s, n) for (LL i = s; i >= n; i--)
#define auto(m) for (auto &it : m)
#define f2(it, v) for (it = v.begin(); it != v.end(); ++it)
#define fori(it, x) \
for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)
#define ip(n, a) \
LL n; \
cin >> n; \
LL a[n]; \
rep() { cin >> a[i]; }
#define op(n, a) \
f(i, 0, n) { cout << a[i] << " "; } \
cout << endl;
#define i2p(n, k, a) \
LL n, k; \
cin >> n >> k; \
LL a[n]; \
rep { cin >> a[i]; }
#define TCs(x) \
LL x; \
cin >> x; \
while (x--)
#define TC(x) \
LL x = 1; \
while (x--)
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define mem(a, x) memset(a, x, sizeof(a))
#define Max(x, y, z) max(x, max(y, z))
#define Min(x, y, z) min(x, min(y, z))
#define fastIO \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define ps(x) \
std::cout << std::fixed; \
std::cout << std::setprecision(x);
#define clz(a) \
__builtin_clz(a) // count leading zeroes
// (geeksforgeeks.org/builtin-functions-gcc-compiler/)
#define ctz(a) __builtin_ctz(a) // count trailing zeroes
#define popc(a) \
__builtin_popcount(a) // count set bits (for ints only diff for ll)
#define GCD(A, B) __gcd(A, B)
#define trace1(x) cout << #x << ": " << x << endl
#define trace2(x, y) cout << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z) \
cout << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": " \
<< z << endl
#define trace4(a, b, c, d) \
cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << endl
#define trace5(a, b, c, d, e) \
cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define trace6(a, b, c, d, e, f) \
cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " \
<< c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " \
<< #f << ": " << f << endl
// Typedefs:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
typedef string S;
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef vector<long long> VL;
typedef vector<VL> VVL;
typedef vector<string> VS;
typedef vector<char> VC;
typedef pair<LL, LL> PLL;
typedef pair<string, LL> PSL;
typedef pair<LL, string> PLS;
typedef pair<char, LL> PCL;
typedef pair<LL, char> PLC;
typedef pair<string, string> PSS;
typedef pair<char, char> PCC;
typedef vector<PLL> VPLL;
typedef vector<PSL> VPSL;
typedef vector<PLS> VPLS;
typedef vector<PCL> VPCL;
typedef vector<PLC> VPLC;
typedef vector<PSS> VPSS;
typedef vector<PCC> VPCC;
typedef map<LL, LL> MLL;
typedef map<string, LL> MSL;
typedef map<char, LL> MCL;
typedef set<LL> SL;
typedef set<string> SS;
typedef set<char> SC;
int power(LL x, LL y) {
if (y == 0)
return 1;
LL p = power(x, y / 2);
p = (p * p);
return (y % 2 == 0) ? p : (x * p);
}
const long long N = (long long)(1e5 + 1);
const long long N2 = (long long)(1e6 + 1);
const long long MOD = (long long)(1e9 + 7);
const long long INF = (long long)(2e18);
const long long maxN = (long long)(1e7 + 1);
LL a[maxN];
void solve() {
LL n;
cin >> n;
mem(a, 0);
LL x = 1;
LL ans = 0;
while (x <= n / 2) {
for (LL i = x; i <= n; i += x) {
a[i]++;
// trace2(i,a[i]);
}
x++;
}
fe(i, x, n) { a[i]++; }
fe(i, 1, n) {
// trace2(i,a[i]);
ans += i * a[i];
}
cout << ans << endl;
}
int main() {
fastIO TC(t) { solve(); }
return 0;
}
| replace | 118 | 119 | 118 | 120 | 0 | |
p02624 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
// TEMPLATE
#define pb push_back
#define mp make_pair
#define ll long long
#define ld long double
#define pii pair<ll, ll>
#define piii pair<ll, pii>
#define F first
#define S second
#define newline printf("\n")
#define minusone printf("-1\n")
#define zeroo printf("0\n")
#define scl1(a) scanf("%lld", &a)
#define scl2(a, b) scanf("%lld %lld", &a, &b)
#define scl3(a, b, c) scanf("%lld %lld %lld", &a, &b, &c)
#define prl1(a) printf("%lld\n", a)
#define prl2(a, b) printf("%lld %lld\n", a, b)
#define ssl1(s) scanf(" %[^\n]", s)
#define scd1(a) scanf("%lf", &a)
#define scd2(a, b) scanf("%lf %lf", &a, &b)
#define prd(a) printf("%lf\n", a)
#define prld(a) printf("%Lf\n", a)
#define prcase(cs) printf("Case %lld: ", cs)
#define cin1(a) cin >> a
#define cin2(a, b) cin >> a >> b
#define cin3(a, b, c) cin >> a >> b >> c
#define cin4(a, b, c, d) cin >> a >> b >> c >> d
#define cot1(a) cout << a << "\n"
#define cot2(a, b) cout << a << " " << b << "\n"
#define cot3(a, b, c) cout << a << " " << b << " " << c << "\n"
#define cot4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << "\n"
#define cotline cout << "\n"
#define cotminus cout << "-1\n"
#define cot0 cout << "0\n"
#define cotyes cout << "YES\n"
#define cotno cout << "NO\n"
#define cotcase(cs) cout << "Case " << cs << ": "
#define reversed(s) reverse(s.begin(), s.end())
#define asort(s) sort(s.begin(), s.end())
#define dsort(s) sort(s.rbegin(), s.rend())
#define all(s) s.begin(), s.end()
#define uniq(s) s.resize(distance(s.begin(), unique(s.begin(), s.end())))
#define found(s, x) s.find(x) != s.end()
#define for0(i, n) for (i = 0; i < n; i++)
#define for1(i, n) for (i = 1; i <= n; i++)
#define fora(i, a, b) for (i = a; i <= b; i++)
#define forb(i, b, a) for (i = b; i >= a; i--)
#define fori(it, s) for (auto it = s.begin(); it != s.end(); it++)
#define FR \
ios_base::sync_with_stdio(false); \
cin.tie(NULL)
#define ms(a, x) memset(a, x, sizeof a)
#define bitcount(n) __builtin_popcountll(n)
// #define pi 3.1415926536
#define pi acos(-1)
const ll INF = LLONG_MAX;
const ll SZ = 4e5 + 5;
const ll mod = 1e9 + 7;
ll n;
unsigned ll a[SZ];
// ll b[SZ];
int main() {
FR;
ll cs = 0, tc = 1, x, y, z, i, j, k, g, p, q, ans = 0, sum = 0, t = 0;
// ll a, b, d;
string s, s1, s2;
cin1(n);
unsigned ll c = 0;
for1(i, n) {
for (j = i; j <= n; j += i) {
a[j]++;
}
}
for1(i, n) { c += (a[i] * i); }
cot1(c);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
// TEMPLATE
#define pb push_back
#define mp make_pair
#define ll long long
#define ld long double
#define pii pair<ll, ll>
#define piii pair<ll, pii>
#define F first
#define S second
#define newline printf("\n")
#define minusone printf("-1\n")
#define zeroo printf("0\n")
#define scl1(a) scanf("%lld", &a)
#define scl2(a, b) scanf("%lld %lld", &a, &b)
#define scl3(a, b, c) scanf("%lld %lld %lld", &a, &b, &c)
#define prl1(a) printf("%lld\n", a)
#define prl2(a, b) printf("%lld %lld\n", a, b)
#define ssl1(s) scanf(" %[^\n]", s)
#define scd1(a) scanf("%lf", &a)
#define scd2(a, b) scanf("%lf %lf", &a, &b)
#define prd(a) printf("%lf\n", a)
#define prld(a) printf("%Lf\n", a)
#define prcase(cs) printf("Case %lld: ", cs)
#define cin1(a) cin >> a
#define cin2(a, b) cin >> a >> b
#define cin3(a, b, c) cin >> a >> b >> c
#define cin4(a, b, c, d) cin >> a >> b >> c >> d
#define cot1(a) cout << a << "\n"
#define cot2(a, b) cout << a << " " << b << "\n"
#define cot3(a, b, c) cout << a << " " << b << " " << c << "\n"
#define cot4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << "\n"
#define cotline cout << "\n"
#define cotminus cout << "-1\n"
#define cot0 cout << "0\n"
#define cotyes cout << "YES\n"
#define cotno cout << "NO\n"
#define cotcase(cs) cout << "Case " << cs << ": "
#define reversed(s) reverse(s.begin(), s.end())
#define asort(s) sort(s.begin(), s.end())
#define dsort(s) sort(s.rbegin(), s.rend())
#define all(s) s.begin(), s.end()
#define uniq(s) s.resize(distance(s.begin(), unique(s.begin(), s.end())))
#define found(s, x) s.find(x) != s.end()
#define for0(i, n) for (i = 0; i < n; i++)
#define for1(i, n) for (i = 1; i <= n; i++)
#define fora(i, a, b) for (i = a; i <= b; i++)
#define forb(i, b, a) for (i = b; i >= a; i--)
#define fori(it, s) for (auto it = s.begin(); it != s.end(); it++)
#define FR \
ios_base::sync_with_stdio(false); \
cin.tie(NULL)
#define ms(a, x) memset(a, x, sizeof a)
#define bitcount(n) __builtin_popcountll(n)
// #define pi 3.1415926536
#define pi acos(-1)
const ll INF = LLONG_MAX;
const ll SZ = 1e7 + 5;
const ll mod = 1e9 + 7;
ll n;
unsigned ll a[SZ];
// ll b[SZ];
int main() {
FR;
ll cs = 0, tc = 1, x, y, z, i, j, k, g, p, q, ans = 0, sum = 0, t = 0;
// ll a, b, d;
string s, s1, s2;
cin1(n);
unsigned ll c = 0;
for1(i, n) {
for (j = i; j <= n; j += i) {
a[j]++;
}
}
for1(i, n) { c += (a[i] * i); }
cot1(c);
return 0;
}
| replace | 65 | 66 | 65 | 66 | 0 | |
p02624 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#define int long long
#define rr register
#define inf 1e9
#define MAXN 10000010
using namespace std;
inline int read() {
int s = 0, f = 0;
char ch = getchar();
while (!isdigit(ch))
f |= ch == '-', ch = getchar();
while (isdigit(ch))
s = s * 10 + (ch ^ 48), ch = getchar();
return f ? -s : s;
}
void print(int x) {
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
print(x / 10);
putchar(x % 10 + 48);
}
int n, ans, m, k, cnt;
int a[MAXN];
inline int pri(int x) {
for (rr int i = 1; i <= x; i++)
for (rr int j = i; j <= x; j += i)
a[j]++;
}
signed main() {
n = read();
pri(10000000);
for (rr int i = 1; i <= n; i++)
ans += i * a[i];
cout << ans;
}
/*
5 4 731
80 150 80 150 1
60 90 120 149
*/ | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#define int long long
#define rr register
#define inf 1e9
#define MAXN 10000010
using namespace std;
inline int read() {
int s = 0, f = 0;
char ch = getchar();
while (!isdigit(ch))
f |= ch == '-', ch = getchar();
while (isdigit(ch))
s = s * 10 + (ch ^ 48), ch = getchar();
return f ? -s : s;
}
void print(int x) {
if (x < 0)
putchar('-'), x = -x;
if (x > 9)
print(x / 10);
putchar(x % 10 + 48);
}
int n, ans, m, k, cnt;
int a[MAXN];
inline void pri(int x) {
for (rr int i = 1; i <= x; i++)
for (rr int j = i; j <= x; j += i)
a[j]++;
}
signed main() {
n = read();
pri(10000000);
for (rr int i = 1; i <= n; i++)
ans += i * a[i];
cout << ans;
}
/*
5 4 731
80 150 80 150 1
60 90 120 149
*/ | replace | 38 | 39 | 38 | 39 | TLE | |
p02624 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 1; i <= (int)(n); i++)
using namespace std;
typedef long long ll;
int main() {
ll n;
cin >> n;
ll s = 0;
vector<ll> a(n);
rep(i, n / 2) {
rep(j, n) {
if (i * j <= n) {
ll x = i * j;
a[x]++;
}
}
}
rep(i, n) { s += a[i] * i; }
cout << s << endl;
} | #include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 1; i <= (int)(n); i++)
using namespace std;
typedef long long ll;
int main() {
ll n;
cin >> n;
ll s = 0;
vector<ll> a(n);
rep(i, n) {
for (int j = i; j <= n; j += i) {
a[j]++;
}
}
rep(i, n) { s += a[i] * i; }
cout << s << endl;
} | replace | 16 | 22 | 16 | 19 | 0 | |
p02624 | C++ | Time Limit Exceeded |
/*****/
#define TRUE true
#define FALSE false
#include <bits/stdc++.h>
/**/
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
/**/
using namespace std;
using ll = long long;
using ld = long double;
using pint = pair<int, int>;
using pll = pair<long long, long long>;
template <class T> using vec = vector<T>;
template <class T> using vec2 = vector<vector<T>>;
template <class T> using vec3 = vector<vector<vector<T>>>;
constexpr int INF = numeric_limits<int>::max();
constexpr ll INFL = numeric_limits<ll>::max();
constexpr ll MOD = 1000000007; // 10^9+7
#ifdef TRUE
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define rep1(i, n) for (ll i = 1, i##_len = (n); i <= i##_len; ++i)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; --i)
#define rrep1(i, n) for (ll i = (n); i > 0; --i)
#define step(i, a, n) for (ll i = (a), i##_len = (a) + (n); i < i##_len; ++i)
#define rstep(i, a, n) \
for (ll i = (a) + (n)-1, i##_len = (a); i >= i##_len; --i)
#define range(i, a, b) for (ll i = (a), i##_len = (b); i < i##_len; ++i)
#define rrange(i, a, b) for (ll i = (b)-1, i##_len = (a); i >= i##_len; --i)
std::string strMulti(const int n, const std::string &t) {
std::string out = "";
for (int i = 0; i < n; i++) {
out += t;
}
return out;
}
std::string tab(const int n) { return string(n, '\t'); }
std::string join(const vector<string> &v, const char *delim = 0) {
std::string s;
if (!v.empty()) {
s += v[0];
for (decltype(v.size()) i = 1, c = v.size(); i < c; ++i) {
if (delim)
s += delim;
s += v[i];
}
}
return s;
}
string to_string(string &s) { return '"' + s + '"'; }
string to_string(char &c) { return string({'\'', c, '\''}); }
template <class T1, class T2> string to_string(pair<T1, T2> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <class Tuple, size_t... I>
string _to_string_tuple(Tuple &&t, std::index_sequence<I...>) {
return "(" + join({to_string(std::get<I>(t))...}, ", ") + ")";
}
template <class... Args> string to_string(tuple<Args...> t) {
return _to_string_tuple(
std::forward<tuple<Args...>>(t),
std::make_index_sequence<
std::tuple_size<std::decay_t<tuple<Args...>>>::value>{});
}
template <class T> string to_string(vector<T> ar) {
vector<string> texts(ar.size());
for (size_t i = 0; i < ar.size(); ++i) {
texts[i] = to_string(ar[i]);
}
return "{" + join(texts, ", ") + "}";
}
template <class T> string to_string(initializer_list<T> il) {
vector<string> texts(il.size());
size_t i = 0;
for (T v : il) {
texts[i] = to_string(v);
i++;
}
return "{" + join(texts, ", ") + "}";
}
void debug(string name);
void debugn(string name);
template <class T> void debug(string name, T v);
template <class T> void debugn(string name, T v);
template <class T> void debug(string name, initializer_list<T> il);
template <class T> void debugn(string name, initializer_list<T> il);
template <class... Args> void debug(string name, Args... args);
template <class... Args> void debugn(string name, Args... args);
void debugdo(function<void()> func);
#ifdef DEBUG_BUILD
#define debugvar(x) debugn(#x, (x))
#define debugvartab(x, t) debugn(tab(t) + #x, (x))
void debug(string name) { std::cerr << name; }
void debugn(string name) { std::cerr << name << '\n'; }
template <class T> void debug(string name, T v) {
std::cerr << name << " = " << to_string(v);
}
template <class T> void debugn(string name, T v) {
std::cerr << name << " = " << to_string(v) << '\n';
}
template <class T> void debug(string name, initializer_list<T> il) {
std::cerr << name << " = " << to_string(il);
}
template <class T> void debugn(string name, initializer_list<T> il) {
std::cerr << name << " = " << to_string(il) << '\n';
}
template <class... Args> void debug(string name, Args... args) {
std::cerr << name << " = " << to_string(tuple<Args...>(args...));
}
template <class... Args> void debugn(string name, Args... args) {
std::cerr << name << " = " << to_string(tuple<Args...>(args...)) << '\n';
}
void debugdo(function<void()> func) { func(); }
#else
#define debugvar(x)
#define debugvartab(x, t)
void debug(string name) {}
void debugln(string name) {}
template <class T> void debug(string name, T v) {}
template <class T> void debugn(string name, T v) {}
template <class T> void debug(string name, initializer_list<T> il) {}
template <class T> void debugn(string name, initializer_list<T> il) {}
template <class... Args> void debug(string name, Args... args) {}
template <class... Args> void debugn(string name, Args... args) {}
void debugdo(function<void()> func) {}
#endif
#define all(x) (x).begin(), (x).end()
#define pair(a, b) make_pair(a, b)
template <class T> bool chmax(T &a, const T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> T divup(const T a, const T b) {
if (a % b == 0) {
return a / b;
}
return a / b + 1;
}
template <class T> bool cmp_2nd(pair<T, T> a, pair<T, T> b) {
if (a.second != b.second) {
return a.second < b.second;
}
return a.first < b.first;
}
ll modpow(ll x, ll n, const ll &p) {
ll ret = 1;
while (n > 0) {
if (n & 1) {
(ret *= x) %= p;
}
(x *= x) %= p;
n >>= 1;
}
return ret;
}
template <class T> T modinv(T a, const T &p) {
T b = p, u = 1, v = 0;
while (b) {
T t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
u %= p;
if (u < 0) {
u += p;
}
return u;
}
template <class T> T math_P(T m, T n) {
T ret = 1;
for (T i = m; i > m - n; i--) {
ret *= i;
}
return ret;
}
template <class T> T math_C(T m, T n) {
T ret = math_P(m, n);
for (T i = 2; i <= n; i++) {
ret /= i;
}
return ret;
}
ll extended_euclidean(ll u, ll v) {
ll r0 = u;
ll r1 = v;
ll s0 = 1;
ll s1 = 0;
ll t0 = 0;
ll t1 = 1;
while (r1 != 0) {
ll q = r0 / r1;
ll r = r0 - q * r1;
ll s = s0 - q * s1;
ll t = t0 - q * t1;
r0 = r1;
s0 = s1;
t0 = t1;
r1 = r;
s1 = s;
t1 = t;
}
if (t0 < 0) {
return t0 + u;
} else {
return t0;
}
}
ll math_C_mod(ll n, ll c, const ll &p) {
ll upe = 1;
ll dow = 1;
for (ll i = 1; i < c + 1; ++i) {
upe = upe * n % p;
dow = dow * i % p;
n -= 1;
}
return (upe * extended_euclidean(p, dow)) % p;
}
template <class T> T math_gcd(T a, T b) {
if (b == 0) {
return a;
} else {
return math_gcd(b, a % b);
}
}
template <class T> T math_lcm(T a, T b) { return a / math_gcd(a, b) * b; }
template <class T> T SumStep(T a, T n, T d) {
return n * (2 * a + (n - 1) * d) / 2;
}
template <class T> T SumRange(T a, T b, T d) {
return SumStep(a, (b - a) / d, d);
}
#endif
/*****/
// 素因数分解する O(sprt(n))
vector<pair<long long, long long>> prime_factorize(long long n) {
vector<pair<long long, long long>> res;
for (long long i = 2; i * i <= n; ++i) {
if (n % i != 0)
continue;
long long ex = 0; // 指数
while (n % i == 0) {
++ex;
n /= i;
}
res.push_back({i, ex});
}
if (n != 1)
res.push_back({n, 1});
return res;
}
void Main() {
ll N;
cin >> N;
ll ret = 0;
rep1(i, N) {
auto E = prime_factorize(i);
ll es = 1;
for (auto e : E) {
es *= e.second + 1;
}
ret += es * i;
}
cout << ret << endl;
}
/*****/
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout << std::fixed << std::setprecision(10);
Main();
std::cerr << flush;
std::cout << flush;
return 0;
}
|
/*****/
#define TRUE true
#define FALSE false
#include <bits/stdc++.h>
/**/
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
/**/
using namespace std;
using ll = long long;
using ld = long double;
using pint = pair<int, int>;
using pll = pair<long long, long long>;
template <class T> using vec = vector<T>;
template <class T> using vec2 = vector<vector<T>>;
template <class T> using vec3 = vector<vector<vector<T>>>;
constexpr int INF = numeric_limits<int>::max();
constexpr ll INFL = numeric_limits<ll>::max();
constexpr ll MOD = 1000000007; // 10^9+7
#ifdef TRUE
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define rep1(i, n) for (ll i = 1, i##_len = (n); i <= i##_len; ++i)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; --i)
#define rrep1(i, n) for (ll i = (n); i > 0; --i)
#define step(i, a, n) for (ll i = (a), i##_len = (a) + (n); i < i##_len; ++i)
#define rstep(i, a, n) \
for (ll i = (a) + (n)-1, i##_len = (a); i >= i##_len; --i)
#define range(i, a, b) for (ll i = (a), i##_len = (b); i < i##_len; ++i)
#define rrange(i, a, b) for (ll i = (b)-1, i##_len = (a); i >= i##_len; --i)
std::string strMulti(const int n, const std::string &t) {
std::string out = "";
for (int i = 0; i < n; i++) {
out += t;
}
return out;
}
std::string tab(const int n) { return string(n, '\t'); }
std::string join(const vector<string> &v, const char *delim = 0) {
std::string s;
if (!v.empty()) {
s += v[0];
for (decltype(v.size()) i = 1, c = v.size(); i < c; ++i) {
if (delim)
s += delim;
s += v[i];
}
}
return s;
}
string to_string(string &s) { return '"' + s + '"'; }
string to_string(char &c) { return string({'\'', c, '\''}); }
template <class T1, class T2> string to_string(pair<T1, T2> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <class Tuple, size_t... I>
string _to_string_tuple(Tuple &&t, std::index_sequence<I...>) {
return "(" + join({to_string(std::get<I>(t))...}, ", ") + ")";
}
template <class... Args> string to_string(tuple<Args...> t) {
return _to_string_tuple(
std::forward<tuple<Args...>>(t),
std::make_index_sequence<
std::tuple_size<std::decay_t<tuple<Args...>>>::value>{});
}
template <class T> string to_string(vector<T> ar) {
vector<string> texts(ar.size());
for (size_t i = 0; i < ar.size(); ++i) {
texts[i] = to_string(ar[i]);
}
return "{" + join(texts, ", ") + "}";
}
template <class T> string to_string(initializer_list<T> il) {
vector<string> texts(il.size());
size_t i = 0;
for (T v : il) {
texts[i] = to_string(v);
i++;
}
return "{" + join(texts, ", ") + "}";
}
void debug(string name);
void debugn(string name);
template <class T> void debug(string name, T v);
template <class T> void debugn(string name, T v);
template <class T> void debug(string name, initializer_list<T> il);
template <class T> void debugn(string name, initializer_list<T> il);
template <class... Args> void debug(string name, Args... args);
template <class... Args> void debugn(string name, Args... args);
void debugdo(function<void()> func);
#ifdef DEBUG_BUILD
#define debugvar(x) debugn(#x, (x))
#define debugvartab(x, t) debugn(tab(t) + #x, (x))
void debug(string name) { std::cerr << name; }
void debugn(string name) { std::cerr << name << '\n'; }
template <class T> void debug(string name, T v) {
std::cerr << name << " = " << to_string(v);
}
template <class T> void debugn(string name, T v) {
std::cerr << name << " = " << to_string(v) << '\n';
}
template <class T> void debug(string name, initializer_list<T> il) {
std::cerr << name << " = " << to_string(il);
}
template <class T> void debugn(string name, initializer_list<T> il) {
std::cerr << name << " = " << to_string(il) << '\n';
}
template <class... Args> void debug(string name, Args... args) {
std::cerr << name << " = " << to_string(tuple<Args...>(args...));
}
template <class... Args> void debugn(string name, Args... args) {
std::cerr << name << " = " << to_string(tuple<Args...>(args...)) << '\n';
}
void debugdo(function<void()> func) { func(); }
#else
#define debugvar(x)
#define debugvartab(x, t)
void debug(string name) {}
void debugln(string name) {}
template <class T> void debug(string name, T v) {}
template <class T> void debugn(string name, T v) {}
template <class T> void debug(string name, initializer_list<T> il) {}
template <class T> void debugn(string name, initializer_list<T> il) {}
template <class... Args> void debug(string name, Args... args) {}
template <class... Args> void debugn(string name, Args... args) {}
void debugdo(function<void()> func) {}
#endif
#define all(x) (x).begin(), (x).end()
#define pair(a, b) make_pair(a, b)
template <class T> bool chmax(T &a, const T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> T divup(const T a, const T b) {
if (a % b == 0) {
return a / b;
}
return a / b + 1;
}
template <class T> bool cmp_2nd(pair<T, T> a, pair<T, T> b) {
if (a.second != b.second) {
return a.second < b.second;
}
return a.first < b.first;
}
ll modpow(ll x, ll n, const ll &p) {
ll ret = 1;
while (n > 0) {
if (n & 1) {
(ret *= x) %= p;
}
(x *= x) %= p;
n >>= 1;
}
return ret;
}
template <class T> T modinv(T a, const T &p) {
T b = p, u = 1, v = 0;
while (b) {
T t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
u %= p;
if (u < 0) {
u += p;
}
return u;
}
template <class T> T math_P(T m, T n) {
T ret = 1;
for (T i = m; i > m - n; i--) {
ret *= i;
}
return ret;
}
template <class T> T math_C(T m, T n) {
T ret = math_P(m, n);
for (T i = 2; i <= n; i++) {
ret /= i;
}
return ret;
}
ll extended_euclidean(ll u, ll v) {
ll r0 = u;
ll r1 = v;
ll s0 = 1;
ll s1 = 0;
ll t0 = 0;
ll t1 = 1;
while (r1 != 0) {
ll q = r0 / r1;
ll r = r0 - q * r1;
ll s = s0 - q * s1;
ll t = t0 - q * t1;
r0 = r1;
s0 = s1;
t0 = t1;
r1 = r;
s1 = s;
t1 = t;
}
if (t0 < 0) {
return t0 + u;
} else {
return t0;
}
}
ll math_C_mod(ll n, ll c, const ll &p) {
ll upe = 1;
ll dow = 1;
for (ll i = 1; i < c + 1; ++i) {
upe = upe * n % p;
dow = dow * i % p;
n -= 1;
}
return (upe * extended_euclidean(p, dow)) % p;
}
template <class T> T math_gcd(T a, T b) {
if (b == 0) {
return a;
} else {
return math_gcd(b, a % b);
}
}
template <class T> T math_lcm(T a, T b) { return a / math_gcd(a, b) * b; }
template <class T> T SumStep(T a, T n, T d) {
return n * (2 * a + (n - 1) * d) / 2;
}
template <class T> T SumRange(T a, T b, T d) {
return SumStep(a, (b - a) / d, d);
}
#endif
/*****/
// 素因数分解する O(sprt(n))
vector<pair<long long, long long>> prime_factorize(long long n) {
vector<pair<long long, long long>> res;
for (long long i = 2; i * i <= n; ++i) {
if (n % i != 0)
continue;
long long ex = 0; // 指数
while (n % i == 0) {
++ex;
n /= i;
}
res.push_back({i, ex});
}
if (n != 1)
res.push_back({n, 1});
return res;
}
void Main() {
ll N;
cin >> N;
ll ret = 0;
rep1(i, N) { ret += i / 2.0 * floor(N / i) * floor(1 + N / i); }
cout << ret << endl;
}
/*****/
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout << std::fixed << std::setprecision(10);
Main();
std::cerr << flush;
std::cout << flush;
return 0;
}
| replace | 285 | 293 | 285 | 286 | TLE | |
p02624 | C++ | Time Limit Exceeded | // time out
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
int main() {
int N;
cin >> N;
ll ans = 0;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= i; j++) {
if (i % j == 0)
ans += i;
}
}
cout << ans << endl;
} | // time out
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
int main() {
int N;
cin >> N;
ll ans = 0;
for (int i = 1; i <= N; i++) {
for (int j = i; j <= N; j += i) {
ans += j;
}
}
cout << ans << endl;
} | replace | 15 | 18 | 15 | 17 | TLE | |
p02624 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long int
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ss string
#define ll long long int
#define Endl endl
#define u_b upper_bound
#define rep(i, n) for (int i = 0; i < n; i++)
#define ld long double
#define tc \
long long t; \
cin >> t; \
while (t--)
#define fr(i, a, b) for (long long int i = a; i <= b; i++)
#define E endl
#define FIO \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define V deque<ll> v;
#define aaste \
long long int n; \
cin >> n; \
long long int a[n]; \
for (long long int i = 0; i < n; i++) \
cin >> a[i];
const ll mod = 1000000007;
const ll MOD1 = (int)1e9 + 9;
const ll MOD2 = 998244353;
const ld pi = atan2(0, -1);
const ld eps = 1e-9;
const int N = 2e5 + 5;
const int MAXR = 1000005;
ll tests = 1;
using namespace std;
ll binarySearch(ll l, ll r, ll k) {
if (r >= l) {
ll mid = l + (r - l) / 2;
if (mid * (mid + 1) == k)
return 1;
if (mid * (mid + 1) > k)
return binarySearch(l, mid - 1, k);
return binarySearch(mid + 1, r, k);
}
return 0;
}
bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) {
return (a.first > b.first);
}
void swap(ll *p1, ll *p2) {
ll temp = *p1;
*p1 = *p2;
*p2 = temp;
}
ll cbratt(ll x) {
ll l = 0, r = MAXR;
while (l != r) {
ll m = (l + r + 1) / 2;
if (m * m * m > x)
r = m - 1;
else
l = m;
}
return l;
}
int MaxSubArraySumReturn(int a[], int n) // negative thakle o kaaj korbe in O(n)
{
int sum = 0, best = 0;
for (ll i = 0; i < n; i++) {
sum = max(a[i], a[i] + sum);
best = max(best, sum);
}
return best;
}
ll totalDigits(ll n) {
ll number_of_digits = 0;
for (ll i = 1; i <= n; i *= 10)
number_of_digits += (n - i + 1);
return number_of_digits;
}
// Functions
ll a[1000000];
void findDivisors(ll n) {
// Array to store the count
// of divisors
ll divi[n + 1];
memset(divi, 0, sizeof divi);
// For every number from 1 to n
for (ll i = 1; i <= n; i++) {
// Increase divisors count for
// every number divisible by i
for (ll j = 1; j * i <= n; j++)
divi[i * j]++;
}
for (ll i = 1; i <= n; i++)
a[i] = divi[i];
}
int main() {
#ifndef ONLINE_JUDGE
{
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
}
#endif
ll n;
cin >> n;
findDivisors(n);
ll sum = 0;
for (ll i = 1; i <= n; i++) {
sum += (i * a[i]);
}
cout << sum << Endl;
}
| #include <bits/stdc++.h>
#define ll long long int
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ss string
#define ll long long int
#define Endl endl
#define u_b upper_bound
#define rep(i, n) for (int i = 0; i < n; i++)
#define ld long double
#define tc \
long long t; \
cin >> t; \
while (t--)
#define fr(i, a, b) for (long long int i = a; i <= b; i++)
#define E endl
#define FIO \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define V deque<ll> v;
#define aaste \
long long int n; \
cin >> n; \
long long int a[n]; \
for (long long int i = 0; i < n; i++) \
cin >> a[i];
const ll mod = 1000000007;
const ll MOD1 = (int)1e9 + 9;
const ll MOD2 = 998244353;
const ld pi = atan2(0, -1);
const ld eps = 1e-9;
const int N = 2e5 + 5;
const int MAXR = 1000005;
ll tests = 1;
using namespace std;
ll binarySearch(ll l, ll r, ll k) {
if (r >= l) {
ll mid = l + (r - l) / 2;
if (mid * (mid + 1) == k)
return 1;
if (mid * (mid + 1) > k)
return binarySearch(l, mid - 1, k);
return binarySearch(mid + 1, r, k);
}
return 0;
}
bool sortinrev(const pair<int, int> &a, const pair<int, int> &b) {
return (a.first > b.first);
}
void swap(ll *p1, ll *p2) {
ll temp = *p1;
*p1 = *p2;
*p2 = temp;
}
ll cbratt(ll x) {
ll l = 0, r = MAXR;
while (l != r) {
ll m = (l + r + 1) / 2;
if (m * m * m > x)
r = m - 1;
else
l = m;
}
return l;
}
int MaxSubArraySumReturn(int a[], int n) // negative thakle o kaaj korbe in O(n)
{
int sum = 0, best = 0;
for (ll i = 0; i < n; i++) {
sum = max(a[i], a[i] + sum);
best = max(best, sum);
}
return best;
}
ll totalDigits(ll n) {
ll number_of_digits = 0;
for (ll i = 1; i <= n; i *= 10)
number_of_digits += (n - i + 1);
return number_of_digits;
}
// Functions
ll a[10000001];
void findDivisors(ll n) {
// Array to store the count
// of divisors
ll divi[n + 1];
memset(divi, 0, sizeof divi);
// For every number from 1 to n
for (ll i = 1; i <= n; i++) {
// Increase divisors count for
// every number divisible by i
for (ll j = 1; j * i <= n; j++)
divi[i * j]++;
}
for (ll i = 1; i <= n; i++)
a[i] = divi[i];
}
int main() {
#ifndef ONLINE_JUDGE
{
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
}
#endif
ll n;
cin >> n;
findDivisors(n);
ll sum = 0;
for (ll i = 1; i <= n; i++) {
sum += (i * a[i]);
}
cout << sum << Endl;
}
| replace | 88 | 89 | 88 | 89 | -11 | |
p02624 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> pint;
template <class T> inline void chmax(T &a, const T b) { a = max(a, b); }
template <class T> inline void chmin(T &a, const T b) { a = min(a, b); }
template <class T> void pp(T v, int n) {
REP(i, n) cout << v[i] << ' ';
cout << endl;
}
void inline TorF(bool tf, const string &trueMessage,
const string &falseMessage) {
cout << (tf ? trueMessage : falseMessage) << endl;
}
void inline YESNO(bool tf) { TorF(tf, "YES", "NO"); }
void inline YesNo(bool tf) { TorF(tf, "Yes", "No"); }
vector<int> primes;
vector<bool> isprime;
vector<ll> cnt;
void init(const int N) {
isprime = vector<bool>(N + 1, true);
cnt = vector<ll>(N + 1, 1);
cnt[1] = 1;
// for(int i = 2; i <= N; i+= 2) cnt[i] = 1;
isprime[0] = isprime[1] = false;
for (int i = 2; i <= N; i++) {
if (isprime[i]) {
primes.push_back(i);
for (int j = i + i; j <= N; j += i)
isprime[j] = false;
}
}
// pp(primes, primes.size());
#ifdef DEBUG
for (int i = 1; i <= N; i++) {
for (int j = 0; j <= primes.size(); j++) {
int k = i * primes[j];
if (k > N)
break;
cnt[k] += cnt[i];
}
}
#endif
}
ll g(int n) {
ll ret = 0;
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
if (i * i == n)
ret++;
else
ret += 2;
}
}
return ret;
}
ll f(int n) {
ll ret = 1;
if (n == 1)
return 1LL;
if (isprime[n])
return 2LL;
REP(i, primes.size()) {
if (n == 1)
break;
int cnt = 0;
while (n % primes[i] == 0) {
cnt++;
n /= primes[i];
}
ret *= cnt + 1;
}
return ret;
}
int main() {
int N;
cin >> N;
ll ans = 0;
init(N);
for (int i = 1; i <= N; i++) {
ll tmp = f(i);
// cout << i << " " << tmp << endl;
ans += tmp * (ll)i;
}
cout << ans << endl;
return 0;
}
| #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> pint;
template <class T> inline void chmax(T &a, const T b) { a = max(a, b); }
template <class T> inline void chmin(T &a, const T b) { a = min(a, b); }
template <class T> void pp(T v, int n) {
REP(i, n) cout << v[i] << ' ';
cout << endl;
}
void inline TorF(bool tf, const string &trueMessage,
const string &falseMessage) {
cout << (tf ? trueMessage : falseMessage) << endl;
}
void inline YESNO(bool tf) { TorF(tf, "YES", "NO"); }
void inline YesNo(bool tf) { TorF(tf, "Yes", "No"); }
vector<int> primes;
vector<bool> isprime;
vector<ll> cnt;
void init(const int N) {
isprime = vector<bool>(N + 1, true);
cnt = vector<ll>(N + 1, 1);
cnt[1] = 1;
// for(int i = 2; i <= N; i+= 2) cnt[i] = 1;
isprime[0] = isprime[1] = false;
for (int i = 2; i <= N; i++) {
if (isprime[i]) {
primes.push_back(i);
for (int j = i + i; j <= N; j += i)
isprime[j] = false;
}
}
// pp(primes, primes.size());
#ifdef DEBUG
for (int i = 1; i <= N; i++) {
for (int j = 0; j <= primes.size(); j++) {
int k = i * primes[j];
if (k > N)
break;
cnt[k] += cnt[i];
}
}
#endif
}
ll g(int n) {
ll ret = 0;
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
if (i * i == n)
ret++;
else
ret += 2;
}
}
return ret;
}
ll f(int n) {
ll ret = 1;
if (n == 1)
return 1LL;
if (isprime[n])
return 2LL;
REP(i, primes.size()) {
if (n == 1)
break;
if (isprime[n]) {
ret *= 2LL;
break;
}
int cnt = 0;
while (n % primes[i] == 0) {
cnt++;
n /= primes[i];
}
ret *= cnt + 1;
}
return ret;
}
int main() {
int N;
cin >> N;
ll ans = 0;
init(N);
for (int i = 1; i <= N; i++) {
ll tmp = f(i);
// cout << i << " " << tmp << endl;
ans += tmp * (ll)i;
}
cout << ans << endl;
return 0;
}
| insert | 81 | 81 | 81 | 85 | TLE | |
p02624 | C++ | Time Limit Exceeded | /*==============================================*\
Codeforces ID: mdshihab
Atcoder ID : mdshihab |
Name: Md. Shihabul Minhaz |
Study: CSE, JKKNIU |
Address: Trishal, Mymensingh, Bangladesh |
|
mail: mdshihabul20171@gmail.com |
FB : fb.com/mdshihabul.minhaz.7 |
github: Shihabulminhaz |
stopstalk : mdshihab |
|
@uthor Md. Shihabul Minhaz (shihab) |
\*===============================================*/
#include <bits/stdc++.h>
using namespace std;
#define fi(a) scanf("%d", &a);
#define fli(a) scanf("%ld", &a);
#define fll(a) scanf("%lld", &a);
#define pi(a) printf("%d\n", a);
#define ppi(i, a) printf("Case %d: %d\n", i, a);
#define ll long long
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
void FI() {
#ifndef ONLINE_JUDGE
freopen("C:\\Users\\SHIHAB\\Desktop\\input.in", "r", stdin);
#endif // ONLINE_JUDGE
}
// ll arr[10000000];
int main() {
fast
// FI(); /// skip this line for compile
/// brute force solution
/*
ll n,l,j=0,ans=1;
ll sum=0;
cin >> n;
for(ll i=1;i<=n;i++){
for(ll j=i;j<=n;j+=i){
arr[j]++;
}
}
for(ll i=1;i<=n;i++){
sum+=i*arr[i];
}
cout << sum << endl;*/
/// optimize solution
/* ll cnt,ans=0,sum,n;
cin >> n;
for(ll i=1;i<=n;i++){
cnt = n/i;
sum = (cnt+1) * cnt/2;
ans+=sum*i;
}
cout << ans << endl;*/
/// more optimize solution
ll n,
ans = 0;
cin >> n;
for (ll i = 1; i <= n; i++) {
for (ll j = 1; j <= n; j++) {
if (!(i % j))
ans += i;
}
}
cout << ans << endl;
return 0;
}
| /*==============================================*\
Codeforces ID: mdshihab
Atcoder ID : mdshihab |
Name: Md. Shihabul Minhaz |
Study: CSE, JKKNIU |
Address: Trishal, Mymensingh, Bangladesh |
|
mail: mdshihabul20171@gmail.com |
FB : fb.com/mdshihabul.minhaz.7 |
github: Shihabulminhaz |
stopstalk : mdshihab |
|
@uthor Md. Shihabul Minhaz (shihab) |
\*===============================================*/
#include <bits/stdc++.h>
using namespace std;
#define fi(a) scanf("%d", &a);
#define fli(a) scanf("%ld", &a);
#define fll(a) scanf("%lld", &a);
#define pi(a) printf("%d\n", a);
#define ppi(i, a) printf("Case %d: %d\n", i, a);
#define ll long long
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
void FI() {
#ifndef ONLINE_JUDGE
freopen("C:\\Users\\SHIHAB\\Desktop\\input.in", "r", stdin);
#endif // ONLINE_JUDGE
}
// ll arr[10000000];
int main() {
fast
// FI(); /// skip this line for compile
/// brute force solution
/*
ll n,l,j=0,ans=1;
ll sum=0;
cin >> n;
for(ll i=1;i<=n;i++){
for(ll j=i;j<=n;j+=i){
arr[j]++;
}
}
for(ll i=1;i<=n;i++){
sum+=i*arr[i];
}
cout << sum << endl;*/
/// optimize solution
/* ll cnt,ans=0,sum,n;
cin >> n;
for(ll i=1;i<=n;i++){
cnt = n/i;
sum = (cnt+1) * cnt/2;
ans+=sum*i;
}
cout << ans << endl;*/
/// more optimize solution
ll n,
ans = 0;
cin >> n;
for (ll i = 1; i <= n; i++) {
for (ll j = i; j <= n; j += i) {
ans += j;
}
}
cout << ans << endl;
return 0;
}
| replace | 68 | 71 | 68 | 70 | TLE | |
p02624 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define MOD 998244353
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 long long INF = 1LL << 60;
bool pairCompare(const pair<double, ll> &firstElof,
const pair<double, ll> &secondElof) {
return firstElof.first < secondElof.first;
}
bool pairCompareSecond(const pair<double, ll> &firstElof,
const pair<double, ll> &secondElof) {
return firstElof.second < secondElof.second;
}
// 四方向への移動ベクトル
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
struct edge { // グラフに使うヤツ
ll from, to, cost;
};
typedef vector<vector<edge>> G;
ll gcd(ll a, ll b) {
if (a % b == 0)
return (b);
else
return (gcd(b, a % b));
}
vector<pair<long long, long long>> prime_factorize(long long N) {
vector<pair<long long, long long>> res;
for (long long a = 2; a * a <= N; ++a) {
if (N % a != 0)
continue;
long long ex = 0;
while (N % a == 0) {
++ex;
N /= a;
}
res.push_back(make_pair(a, ex));
}
if (N != 1)
res.push_back(make_pair(N, 1));
return res;
}
int main() {
long long N;
cin >> N;
const auto &pf = prime_factorize(N);
long long res = 1;
ll ans = 0;
for (ll i = 1; i <= N; i++) {
const auto &pf = prime_factorize(i);
long long res = 1;
for (auto p : pf)
res *= p.second + 1;
ans += i * res;
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define MOD 998244353
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 long long INF = 1LL << 60;
bool pairCompare(const pair<double, ll> &firstElof,
const pair<double, ll> &secondElof) {
return firstElof.first < secondElof.first;
}
bool pairCompareSecond(const pair<double, ll> &firstElof,
const pair<double, ll> &secondElof) {
return firstElof.second < secondElof.second;
}
// 四方向への移動ベクトル
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
struct edge { // グラフに使うヤツ
ll from, to, cost;
};
typedef vector<vector<edge>> G;
ll gcd(ll a, ll b) {
if (a % b == 0)
return (b);
else
return (gcd(b, a % b));
}
vector<pair<long long, long long>> prime_factorize(long long N) {
vector<pair<long long, long long>> res;
for (long long a = 2; a * a <= N; ++a) {
if (N % a != 0)
continue;
long long ex = 0;
while (N % a == 0) {
++ex;
N /= a;
}
res.push_back(make_pair(a, ex));
}
if (N != 1)
res.push_back(make_pair(N, 1));
return res;
}
int main() {
ll n, ans = 0;
cin >> n;
for (ll i = 1; i <= n; i++) {
for (ll j = i; j <= n; j += i) {
ans += j;
}
}
cout << ans << endl;
} | replace | 62 | 73 | 62 | 68 | TLE | |
p02624 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
using namespace std;
using ll = long long;
int main() {
ll n;
cin >> n;
ll ans = 0;
rep(i, n) {
ll k = n / i;
ans += i * k * (k + 1) / 2;
}
cout << ans << endl;
} | #include <bits/stdc++.h>
#define rep(i, n) for (ll i = 1; i <= (ll)(n); i++)
using namespace std;
using ll = long long;
int main() {
ll n;
cin >> n;
ll ans = 0;
rep(i, n) {
ll k = n / i;
ans += i * k * (k + 1) / 2;
}
cout << ans << endl;
} | replace | 1 | 2 | 1 | 2 | -8 | |
p02624 | C++ | Runtime Error | /*
──────────────────────────────░░░────────
───────────────────────────░▓█████▒──────
──────────────────────────░████████▒─────
─────────────────────────░██████████░────
─────────────────────────███████████▓────
────────────────────────░████████████────
────────────────────────█████████████▒───
───────────────────────░█████████████▓───
───────────────────────▓██████████████───
───────────────────────███████████████───
───────────────────────█████▓█████████░──
──────────────────────░████▓▓█████████░──
───────────────────────███▓▓▓▓▓███▓▓██▒──
───────────────────────████▓▓▓▓▓▓▓▓▓██▒──
───────────────────────████▓▓▓▓▓▓▓▓▓██▒──
───────────────────────████▓▓▓▓▓▓▓▓▓██▒──
──────────░░░───░░░▒▓▓▓███▓▓▓▓▓▓▓█████░──
─────────░████████████████▓▓██████████░──
──────────████████████████▓▓██▓▓▓█████░──
──────────██▓▓████████████▓████▓▓█████░──
────────▓▓█▓▓▓▓▓██████████▓███████████░──
────────██▓▓▓▓█▓█████▓▓▓██▓████▓▓█████░──
────────░█▓▓█▓█████────░██▓████▓▓█████░──
────────▓█▓▓▓▓▓▒█▒▓─────██▓█▓██▓▓█████░──
───────░██▓▓▓▓█░█▒░░────███▓▓▓▓▓▓█████░──
───────██▓▓▓███▓▓█▓░────▓██▓▓▓█▓▓▓███▓───
──────███▓▓▓▓▓████░─────▒██▓▓▓█▓▓▓███▒───
─────▓███▓▓▓▓▓██────────░███▓▓███████▒───
────░██▓▓▓██▓▓██─────────░███████████▒───
───░██▓▓▓███▓▓██─────────░███████████▒───
───████▓████▓██▓──────────███████████░───
──█████▓██▓▓▓██▓──────────███████▓███────
─███▓██████████▓──────────▓███▓▓████▓────
─██▓▓████▓█████▓──────────▒█████████░────
░██▓▓███──██████──────────▓█████████─────
▒██▓▓▓██──░███▒░─────────░██████████─────
▓██▓▓██▓─────────────────███████████─────
███████▒────────────────▓█▓▓████████▒────
███████░─────────░░░──░▓██▓▓▓██████▓█────
███████─────▓█████████████▓▓▓█████▓▓█▓───
██████▓────░██████████████▓▓▓▓████▓▓██▒──
██████▒────▒███████████████▓▓▓▓██▓▓▓████░
██████░────▓███████████████▓▓▓▓██▓▓▓█████
██████─────████████████████▓▓▓████▓▓▓████
██████────░███████████████▓▓▓▓████▓▓▓████
█████▓───░████████████████▓▓▓██████▓▓████
█████▒───█████████████████▓▓▓██████▓▓████
█████──▒██████████████████▓▓█▓▓██▓██▓████
████▓─░███████████████████▓▓▓▓▓██▓▓██████
████▒─████████████████████▓▓▓▓▓██▓▓▓█████
████▓▓████████████████████▓▓▓▓▓██▓▓▓█████
██████████████████████████▓▓▓▓▓██▓▓▓█████
██████████████████████████▓▓▓▓▓██▓▓▓█████
██████████████████████████▓▓▓▓███▓▓▓█████
██████████████████████████▓▓▓▓███▓▓▓█████
██████████████████████████▓▓▓▓████▓▓█████
██████████████████████████▓▓▓▓████▓▓█████
██████████████████████████▓▓▓▓████▓▓█████
██████████████████████████▓▓▓▓████▓▓█████
██████████████████████████▓▓▓▓████▓▓█████
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define se second
#define fi first
// char a[100][100], b[200007], c[10000];
ll c[200007];
ll b[200007], a[200007];
ll C[20007 + 1][64 + 1];
// string a, b;
// vector<pair<ll,ll>> a(200007);
int main() {
#ifndef ONLINE_JUDGE
freopen("input1.txt", "r", stdin);
freopen("output1.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll i, k, l, t, n, x, r, y, j, posi, m, posj, sumi, sumj;
cin >> n;
memset(a, 0, sizeof a);
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
if (i * j > n)
break;
a[i * j]++;
}
}
ll sum = 0;
for (i = 1; i <= n; i++) {
sum += i * a[i];
}
cout << sum;
}
// MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
// MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
// MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
// MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMHBYYYWMMMM#BYYTTTYWMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
// MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMB9O1==?????zzCC111>>;;;;;;;;;;;<?TMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
// MMMMMMMMMMMMMMMMMMMMMMMMMMMMMM9Olll=l======??????????>>>>>>;;;;;;;;;:;?TMMMMMMMMMMMMMMMMMMMMMMMMMMMM
// MMMMMMMMMMMMMMMMMMMMMMMMMMM9ttlllllllll=l=======?????????>>>>>>;;;;;;;;;;?TMMMMMMMMMMMMMMMMMMMMMMMMM
// MMMMMMMMMMMMMMMMMMMMMMMMBOttOtttttltlllllllll=======??????????>>>>>>>;;;;;;?TMMMMMMMMMMMMMMMMMMMMMMM
// MMMMMMMMMMMMMMMMMMMMMMBttwOtttttttttttttlllllllll========?????????>>>>>>;;;;;<TMMMMMMMMMMMMMMMMMMMMM
// MMMMMMMMMMMMMMMMMMMMBrwZrttttttttttttttttttlllllllllll======???????????>>>>>>;;?HMMMMMMMMMMMMMMMMMMM
// MMMMMMMMMMMMMMMMMMMSw0trtrrtrtrttrtttttttttttttlllOllllll========????<<zz??>>>>>>ZMMMMMMMMMMMMMMMMMM
// MMMMMMMMMMMMMMMMMMX0ttrtrOOttrttrttOOttttttttttttltwllllllllll========??wy?????>>>vZMMMMMMMMMMMMMMMM
// MMMMMMMMMMMMMMMMM0ttrI+wV1rtttttttwZtttttttltttOwylOXOllllllllllll=l====1dkz???????vZdMMMMMMMMMMMMMM
// MMMMMMMMMMMMMMMSrrwrtwZCjttttttttwSttOlllltllllwtXOlZkOlllllllllllllll==z+dk===?????X2JMMMMMMMMMMMMM
// MMMMMMMMMMMMMBrtwwtrw0<jttttttttOXllOttltllttllOZwHOtXkyltlllllllllllllll<+XZ======?dk?JMMMMMMMMMMMM
// MMMMMMMMMMMMStwdZtwXC<+ttttttOOOd6ltZlltllllttllStWWOOHWytttttltltOllltllz:zHllll===zX=?dMMMMMMMMMMM
// MMMMMMMMMMBrwdKOtwW3;;zrttttwZwXRlldOllltltlltttwlXvktdXWytltttttttOttlttl<<dklllllllZ===dMMMMMMMMMM
// MMMMMMMMM8tQM#ttwW3;;;1trttwSwfUOlORltllltlllllldtX>dktDOWOtlttltttOOttltO<;+WOllllllXlllzMMMMMMMMMM
// MMMMMMMM9OdM#Ottd$;;;;;<?1z0OKjRltd0llltOllllllldZX>?WOw_WWOtttttttttOttI<;;<dkttttllllllldMMMMMMMMM
// MMMMMMM9wMMMSttwS<;;;;>;;;J<j>(I<?U111zltllllllld0X>~dkwl(WkttlttlOOwWk<;;:;;zHttttttwOllllMMMMMMMMM
// MMMMMMBdMMM8tttdI+zttttttdSXt~dlzXwlllzzOzzzzzlldkW<~?kOk~?sx++++jdHmH6+++<;;jdZtttttdkOtlldMMMMMMMM
// MMMMM#dMMM#tttw0+tttttttdKdf((RsdfRllllldZllltlldWK~~_W0w_~OWOlOdgg9ZtOWOtttz+wktttttdpktttwMMMMMMMM
// MMMMMWMMMMSlttdIttttttOdWHH>~(IzWDRll=llzRlllzlldk$>++dkd<~_XkdgH9tttttdktttt+OkrttrtdfpkttOMMMMMMMM
// MMMMNWMMM#Zttlk=tttttOXWWd$_.(IdW1R=llll=SlllzOldK<_._(kd6+-(MM9lltttttrWktttztWZttrwXppWttrMMMMMMMM
// MMMMNMMMM#lltwDzttllOXyW0X:..(OXk<Rl=ll=lwOlllOld3<``.(Rd>~?CdklllltttttXdXttzOWktrwwWpfpkrtMMMMMMMM
// MMMMMMMMM@ltldIzlttOXyW$w$```(OyD(Rl==llldZ=llIlw;~```-Xd:~~~~UkylltttAyHdgSrzObRtOXwpfppWrrMMMMMMMM
// MMMMMMMMM@llldtlllldVyW+d>```.wZ$ wl==lI=wRl=ll=w<````
// dd_..~~(RZXOQdggHH9ZwrzwHWrdXXppfppkrMMMMMMMM
// MMMMMMMMM#llldlltlwyyyD(S-...,Wk] zI=l=llzWzl=l=P~````
// jZ``...~zQkH@MBUtrZtrtldHRdSdpfpffpkwMMMMMMMM
// MMMMNMMMMNZllXtllldVyW3(Mf=<ONMHP~(k===l=zdk=lld3```...(C````..(M96lttttrrtrrtdHWHXpfpppfpRwMMMMMMMM
// MMMMMMMMMMNzzWZllzyyyy(W@` =~MWK6 jz=lzlzvRz=lZ`.I+JgkWm&-.
// `..(ZltlttttrtttrWWHppfppfpppSdMMMMMMMM MMMMMMMMMMMkAWRllzVyyS_(b
// .MNHU-(Uz=lI={XkzO>.dVT7<TMHMNHHx-.(kwZyltttrtrrdHHpfppfpffpp0dMMMMMMMM
// MMMMMMMMMMMMKyWzOzyyyk.`<! hdWMH]``(S==t=l(kX2 ?_`
// WMMHH#HH+.XwkwOttttrwrZ~(HfpfpppfpWwXMMMMMMMM MMMMMMMMMMMMMHWROOyyyW;` `.
// (WVM#b```(0=zzz wX!``` a.dM#NM@N(4WhXkXkWtttttwd3O_(HpffpfpfWwSMMMMMMMM
// MMMMMMMMMMMMNZvWzXXyyXP```-_ ?o+?!````.4=Z=`(:```` HpbNNMHHH
// (C=XXyWZWttrdwf:(>.dfppffppSXSMMMMMMMM
// MMMMMMMMMMMMMRzuHzXyZXH.``````````````` ?zz ``````` ZKvTHHbWt ``
// XZyyHZWOdSZ<::~`(fpfpfffXp0WMMMMMMM
// MMMMMMMMMMMMMKzyyHvyZZW____.`````````````.I_````````
// ?nJzX7^````.WXWyyHyWKZ<:::_`Jffpfpfpfp0WMMMMMMM
// MMMMMMMMMMMMM@zZyZWwWyXo-_~(~ ```````````` ``````````````
// _`````.WXkyyVWmWc::<~ .HffppfffffkXMMMMMMM
// MMMMMMMMMMMMMKzZZZZXkUXr~....```````....```````````` ...
// ````..JXWWyyW83vXx~..WfffVfWVffffkdMMMMMMM
// MMMMMMMMMMMMMRzZZZZZZZZb````````````<````````````./<~._<_.____`(WfdyW3<:~~(XWkVVVVVVVHVVVVV0dMMMMMMM
// MMMMMMMMMMMMMSzZZZZZZZWX-```````````````````````````.......~~._j9jX=_~~_(XZyVHHkyyyyWHyyykykOMMMMMMM
// MMMMMMMMMMMMMSzuuuuuZXWZW,```````````````````````````````.``..(3<!
// ...JWyyXWyyyWHkyyWHyyyHyklMMMMMMM MMMMMMMMMMMMNXzuuuXXuXSuuXh,```````` ....
// ```````````````````_~ .JWyyyyZyy0HyyZyyZyZXWZZZWHZIdMMMMMM
// MMMMMMMMMMMMNwtuuuXkuXXuuuuXh,``````(:::~<?71(,``````````````.(UMNUkZZZZZZZHZZ0ZZZZZWWZZZWNXIdMMMMMM
// MMMMMMMMMMMMNKOzuzXkuXuuuuuXuXW, ````
// _~~~:~~(}```````````.(YC;::<kCfZZZZXO#ZZVZZZZZMZZuuMMNXzMMMMMM
// MMMMMMMMMMMMHKOzzuXkzdzzzzuXXzuuU&.```````````````````
// .JY>::::;;J=:dZuZuZd#uuruuuud#uuuXMMMNXdMMMMM
// MMMMMMMMMMMMMNOvzzXkvMRzzzzXKzuzzzXh,
// `````````````..JC<;:;;:;;+7<:~(HHuXIdNuuzuuuXMSuuuWMMMMROMMMMM
// MMMMMMMMMMMMMNvvvrdkvM#vzzzzHzzzzzuzzUG. ```
// ...JdY<:;:;:::;;+v<~~~:(HpHmzMNXZzuzzd#zzzdMMMMMMRdMMMM
// MMMMMMMMMMMMMNwrrvdRrMNwvvvvdRvvvzwkzzzzXWWHY=~~O+::::::::<+<~~~~~~~dppppWMMkZzzzwM#zzdMMMMMMMMNMMMM
// MMMMMMMMMMMMMM#rrrwRrMMbOrrOZNkrvvvXwvvvvvwX;.~._W_:::::(?!~~~~~~~~(HpppppppHWdvwM#XwdMMMMMMMMMMNMMM
// MMMMMMMMMMMMMMNyrrrRrWMMmzOrzdNyrrrZNvrwQWWfb....(r~~_J>_.....~.~~-dpfpffpfpppWHHMNdMMMMMMMMMMMMMMMM
// MMMMMMMMMMMMMMMNrtrStdMMMNxzrzMNmrrrdNWfVfffP._-.(~_J!............JpfpffpfpfffpppWNppppHMMMMMMMMMMMM
// MMMMMMMMMMMMMMMMNOtXtdMMMMMNxzZMMNmgHyVVVVVW%..?/(.,(x-..........(HfffpffpffpffWHHffpffppfVyyWMMMMMM | /*
──────────────────────────────░░░────────
───────────────────────────░▓█████▒──────
──────────────────────────░████████▒─────
─────────────────────────░██████████░────
─────────────────────────███████████▓────
────────────────────────░████████████────
────────────────────────█████████████▒───
───────────────────────░█████████████▓───
───────────────────────▓██████████████───
───────────────────────███████████████───
───────────────────────█████▓█████████░──
──────────────────────░████▓▓█████████░──
───────────────────────███▓▓▓▓▓███▓▓██▒──
───────────────────────████▓▓▓▓▓▓▓▓▓██▒──
───────────────────────████▓▓▓▓▓▓▓▓▓██▒──
───────────────────────████▓▓▓▓▓▓▓▓▓██▒──
──────────░░░───░░░▒▓▓▓███▓▓▓▓▓▓▓█████░──
─────────░████████████████▓▓██████████░──
──────────████████████████▓▓██▓▓▓█████░──
──────────██▓▓████████████▓████▓▓█████░──
────────▓▓█▓▓▓▓▓██████████▓███████████░──
────────██▓▓▓▓█▓█████▓▓▓██▓████▓▓█████░──
────────░█▓▓█▓█████────░██▓████▓▓█████░──
────────▓█▓▓▓▓▓▒█▒▓─────██▓█▓██▓▓█████░──
───────░██▓▓▓▓█░█▒░░────███▓▓▓▓▓▓█████░──
───────██▓▓▓███▓▓█▓░────▓██▓▓▓█▓▓▓███▓───
──────███▓▓▓▓▓████░─────▒██▓▓▓█▓▓▓███▒───
─────▓███▓▓▓▓▓██────────░███▓▓███████▒───
────░██▓▓▓██▓▓██─────────░███████████▒───
───░██▓▓▓███▓▓██─────────░███████████▒───
───████▓████▓██▓──────────███████████░───
──█████▓██▓▓▓██▓──────────███████▓███────
─███▓██████████▓──────────▓███▓▓████▓────
─██▓▓████▓█████▓──────────▒█████████░────
░██▓▓███──██████──────────▓█████████─────
▒██▓▓▓██──░███▒░─────────░██████████─────
▓██▓▓██▓─────────────────███████████─────
███████▒────────────────▓█▓▓████████▒────
███████░─────────░░░──░▓██▓▓▓██████▓█────
███████─────▓█████████████▓▓▓█████▓▓█▓───
██████▓────░██████████████▓▓▓▓████▓▓██▒──
██████▒────▒███████████████▓▓▓▓██▓▓▓████░
██████░────▓███████████████▓▓▓▓██▓▓▓█████
██████─────████████████████▓▓▓████▓▓▓████
██████────░███████████████▓▓▓▓████▓▓▓████
█████▓───░████████████████▓▓▓██████▓▓████
█████▒───█████████████████▓▓▓██████▓▓████
█████──▒██████████████████▓▓█▓▓██▓██▓████
████▓─░███████████████████▓▓▓▓▓██▓▓██████
████▒─████████████████████▓▓▓▓▓██▓▓▓█████
████▓▓████████████████████▓▓▓▓▓██▓▓▓█████
██████████████████████████▓▓▓▓▓██▓▓▓█████
██████████████████████████▓▓▓▓▓██▓▓▓█████
██████████████████████████▓▓▓▓███▓▓▓█████
██████████████████████████▓▓▓▓███▓▓▓█████
██████████████████████████▓▓▓▓████▓▓█████
██████████████████████████▓▓▓▓████▓▓█████
██████████████████████████▓▓▓▓████▓▓█████
██████████████████████████▓▓▓▓████▓▓█████
██████████████████████████▓▓▓▓████▓▓█████
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define se second
#define fi first
// char a[100][100], b[200007], c[10000];
// ll c[20000007];
ll b[200007], a[10000007];
ll C[20007 + 1][64 + 1];
// string a, b;
// vector<pair<ll,ll>> a(200007);
int main() {
#ifndef ONLINE_JUDGE
freopen("input1.txt", "r", stdin);
freopen("output1.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll i, k, l, t, n, x, r, y, j, posi, m, posj, sumi, sumj;
cin >> n;
memset(a, 0, sizeof a);
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
if (i * j > n)
break;
a[i * j]++;
}
}
ll sum = 0;
for (i = 1; i <= n; i++) {
sum += i * a[i];
}
cout << sum;
}
// MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
// MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
// MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
// MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMHBYYYWMMMM#BYYTTTYWMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
// MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMB9O1==?????zzCC111>>;;;;;;;;;;;<?TMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
// MMMMMMMMMMMMMMMMMMMMMMMMMMMMMM9Olll=l======??????????>>>>>>;;;;;;;;;:;?TMMMMMMMMMMMMMMMMMMMMMMMMMMMM
// MMMMMMMMMMMMMMMMMMMMMMMMMMM9ttlllllllll=l=======?????????>>>>>>;;;;;;;;;;?TMMMMMMMMMMMMMMMMMMMMMMMMM
// MMMMMMMMMMMMMMMMMMMMMMMMBOttOtttttltlllllllll=======??????????>>>>>>>;;;;;;?TMMMMMMMMMMMMMMMMMMMMMMM
// MMMMMMMMMMMMMMMMMMMMMMBttwOtttttttttttttlllllllll========?????????>>>>>>;;;;;<TMMMMMMMMMMMMMMMMMMMMM
// MMMMMMMMMMMMMMMMMMMMBrwZrttttttttttttttttttlllllllllll======???????????>>>>>>;;?HMMMMMMMMMMMMMMMMMMM
// MMMMMMMMMMMMMMMMMMMSw0trtrrtrtrttrtttttttttttttlllOllllll========????<<zz??>>>>>>ZMMMMMMMMMMMMMMMMMM
// MMMMMMMMMMMMMMMMMMX0ttrtrOOttrttrttOOttttttttttttltwllllllllll========??wy?????>>>vZMMMMMMMMMMMMMMMM
// MMMMMMMMMMMMMMMMM0ttrI+wV1rtttttttwZtttttttltttOwylOXOllllllllllll=l====1dkz???????vZdMMMMMMMMMMMMMM
// MMMMMMMMMMMMMMMSrrwrtwZCjttttttttwSttOlllltllllwtXOlZkOlllllllllllllll==z+dk===?????X2JMMMMMMMMMMMMM
// MMMMMMMMMMMMMBrtwwtrw0<jttttttttOXllOttltllttllOZwHOtXkyltlllllllllllllll<+XZ======?dk?JMMMMMMMMMMMM
// MMMMMMMMMMMMStwdZtwXC<+ttttttOOOd6ltZlltllllttllStWWOOHWytttttltltOllltllz:zHllll===zX=?dMMMMMMMMMMM
// MMMMMMMMMMBrwdKOtwW3;;zrttttwZwXRlldOllltltlltttwlXvktdXWytltttttttOttlttl<<dklllllllZ===dMMMMMMMMMM
// MMMMMMMMM8tQM#ttwW3;;;1trttwSwfUOlORltllltlllllldtX>dktDOWOtlttltttOOttltO<;+WOllllllXlllzMMMMMMMMMM
// MMMMMMMM9OdM#Ottd$;;;;;<?1z0OKjRltd0llltOllllllldZX>?WOw_WWOtttttttttOttI<;;<dkttttllllllldMMMMMMMMM
// MMMMMMM9wMMMSttwS<;;;;>;;;J<j>(I<?U111zltllllllld0X>~dkwl(WkttlttlOOwWk<;;:;;zHttttttwOllllMMMMMMMMM
// MMMMMMBdMMM8tttdI+zttttttdSXt~dlzXwlllzzOzzzzzlldkW<~?kOk~?sx++++jdHmH6+++<;;jdZtttttdkOtlldMMMMMMMM
// MMMMM#dMMM#tttw0+tttttttdKdf((RsdfRllllldZllltlldWK~~_W0w_~OWOlOdgg9ZtOWOtttz+wktttttdpktttwMMMMMMMM
// MMMMMWMMMMSlttdIttttttOdWHH>~(IzWDRll=llzRlllzlldk$>++dkd<~_XkdgH9tttttdktttt+OkrttrtdfpkttOMMMMMMMM
// MMMMNWMMM#Zttlk=tttttOXWWd$_.(IdW1R=llll=SlllzOldK<_._(kd6+-(MM9lltttttrWktttztWZttrwXppWttrMMMMMMMM
// MMMMNMMMM#lltwDzttllOXyW0X:..(OXk<Rl=ll=lwOlllOld3<``.(Rd>~?CdklllltttttXdXttzOWktrwwWpfpkrtMMMMMMMM
// MMMMMMMMM@ltldIzlttOXyW$w$```(OyD(Rl==llldZ=llIlw;~```-Xd:~~~~UkylltttAyHdgSrzObRtOXwpfppWrrMMMMMMMM
// MMMMMMMMM@llldtlllldVyW+d>```.wZ$ wl==lI=wRl=ll=w<````
// dd_..~~(RZXOQdggHH9ZwrzwHWrdXXppfppkrMMMMMMMM
// MMMMMMMMM#llldlltlwyyyD(S-...,Wk] zI=l=llzWzl=l=P~````
// jZ``...~zQkH@MBUtrZtrtldHRdSdpfpffpkwMMMMMMMM
// MMMMNMMMMNZllXtllldVyW3(Mf=<ONMHP~(k===l=zdk=lld3```...(C````..(M96lttttrrtrrtdHWHXpfpppfpRwMMMMMMMM
// MMMMMMMMMMNzzWZllzyyyy(W@` =~MWK6 jz=lzlzvRz=lZ`.I+JgkWm&-.
// `..(ZltlttttrtttrWWHppfppfpppSdMMMMMMMM MMMMMMMMMMMkAWRllzVyyS_(b
// .MNHU-(Uz=lI={XkzO>.dVT7<TMHMNHHx-.(kwZyltttrtrrdHHpfppfpffpp0dMMMMMMMM
// MMMMMMMMMMMMKyWzOzyyyk.`<! hdWMH]``(S==t=l(kX2 ?_`
// WMMHH#HH+.XwkwOttttrwrZ~(HfpfpppfpWwXMMMMMMMM MMMMMMMMMMMMMHWROOyyyW;` `.
// (WVM#b```(0=zzz wX!``` a.dM#NM@N(4WhXkXkWtttttwd3O_(HpffpfpfWwSMMMMMMMM
// MMMMMMMMMMMMNZvWzXXyyXP```-_ ?o+?!````.4=Z=`(:```` HpbNNMHHH
// (C=XXyWZWttrdwf:(>.dfppffppSXSMMMMMMMM
// MMMMMMMMMMMMMRzuHzXyZXH.``````````````` ?zz ``````` ZKvTHHbWt ``
// XZyyHZWOdSZ<::~`(fpfpfffXp0WMMMMMMM
// MMMMMMMMMMMMMKzyyHvyZZW____.`````````````.I_````````
// ?nJzX7^````.WXWyyHyWKZ<:::_`Jffpfpfpfp0WMMMMMMM
// MMMMMMMMMMMMM@zZyZWwWyXo-_~(~ ```````````` ``````````````
// _`````.WXkyyVWmWc::<~ .HffppfffffkXMMMMMMM
// MMMMMMMMMMMMMKzZZZZXkUXr~....```````....```````````` ...
// ````..JXWWyyW83vXx~..WfffVfWVffffkdMMMMMMM
// MMMMMMMMMMMMMRzZZZZZZZZb````````````<````````````./<~._<_.____`(WfdyW3<:~~(XWkVVVVVVVHVVVVV0dMMMMMMM
// MMMMMMMMMMMMMSzZZZZZZZWX-```````````````````````````.......~~._j9jX=_~~_(XZyVHHkyyyyWHyyykykOMMMMMMM
// MMMMMMMMMMMMMSzuuuuuZXWZW,```````````````````````````````.``..(3<!
// ...JWyyXWyyyWHkyyWHyyyHyklMMMMMMM MMMMMMMMMMMMNXzuuuXXuXSuuXh,```````` ....
// ```````````````````_~ .JWyyyyZyy0HyyZyyZyZXWZZZWHZIdMMMMMM
// MMMMMMMMMMMMNwtuuuXkuXXuuuuXh,``````(:::~<?71(,``````````````.(UMNUkZZZZZZZHZZ0ZZZZZWWZZZWNXIdMMMMMM
// MMMMMMMMMMMMNKOzuzXkuXuuuuuXuXW, ````
// _~~~:~~(}```````````.(YC;::<kCfZZZZXO#ZZVZZZZZMZZuuMMNXzMMMMMM
// MMMMMMMMMMMMHKOzzuXkzdzzzzuXXzuuU&.```````````````````
// .JY>::::;;J=:dZuZuZd#uuruuuud#uuuXMMMNXdMMMMM
// MMMMMMMMMMMMMNOvzzXkvMRzzzzXKzuzzzXh,
// `````````````..JC<;:;;:;;+7<:~(HHuXIdNuuzuuuXMSuuuWMMMMROMMMMM
// MMMMMMMMMMMMMNvvvrdkvM#vzzzzHzzzzzuzzUG. ```
// ...JdY<:;:;:::;;+v<~~~:(HpHmzMNXZzuzzd#zzzdMMMMMMRdMMMM
// MMMMMMMMMMMMMNwrrvdRrMNwvvvvdRvvvzwkzzzzXWWHY=~~O+::::::::<+<~~~~~~~dppppWMMkZzzzwM#zzdMMMMMMMMNMMMM
// MMMMMMMMMMMMMM#rrrwRrMMbOrrOZNkrvvvXwvvvvvwX;.~._W_:::::(?!~~~~~~~~(HpppppppHWdvwM#XwdMMMMMMMMMMNMMM
// MMMMMMMMMMMMMMNyrrrRrWMMmzOrzdNyrrrZNvrwQWWfb....(r~~_J>_.....~.~~-dpfpffpfpppWHHMNdMMMMMMMMMMMMMMMM
// MMMMMMMMMMMMMMMNrtrStdMMMNxzrzMNmrrrdNWfVfffP._-.(~_J!............JpfpffpfpfffpppWNppppHMMMMMMMMMMMM
// MMMMMMMMMMMMMMMMNOtXtdMMMMMNxzZMMNmgHyVVVVVW%..?/(.,(x-..........(HfffpffpffpffWHHffpffppfVyyWMMMMMM | replace | 70 | 72 | 70 | 72 | -11 | |
p02624 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define int long long int
#define pb push_back
#define vi vector<int>
#define pob push_back
#define pf pop_front()
#define mp make_pair
#define loop(i, a, b) for (int i = a; i < b; ++i)
#define eloop(i, a, b) for (int i = a; i <= b; ++i)
#define rloop(i, a, b) for (int i = b - 1; i >= b; --i)
#define MAP map<int, int>
#define DQ deque<int>
#define vf(i) v[i].first
#define vs(i) v[i].second
#define mod 1000000007
#define M 998244353
#define faster \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
int binarySearch(int arr[], int l, int r, int x) {
if (r >= l) {
int mid = l + (r - l) / 2;
// If the element is present at the middle
// itself
if (arr[mid] == x)
return mid;
// If element is smaller than mid, then
// it can only be present in left subarray
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
// Else the element can only be present
// in right subarray
return binarySearch(arr, mid + 1, r, x);
}
// We reach here when element is not
// present in array
return -1;
}
bool isPrime(int n) {
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// This is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
bool isPowerOfTwo(int x) {
/* First x in the below expression is for the case when x is 0 */
return x && (!(x & (x - 1)));
}
int countBits(int number) {
// log function in base 2
// take only integer part
return (int)log2(number) + 1;
}
int countSetBits(int n) {
int count = 0;
while (n) {
count += n & 1;
n >>= 1;
}
return count;
}
void SieveOfEratosthenes(int n, bool prime[], bool primesquare[], int a[]) {
// Create a boolean array "prime[0..n]" and
// initialize all entries it as true. A value
// in prime[i] will finally be false if i is
// Not a prime, else true.
for (int i = 2; i <= n; i++)
prime[i] = true;
// Create a boolean array "primesquare[0..n*n+1]"
// and initialize all entries it as false. A value
// in squareprime[i] will finally be true if i is
// square of prime, else false.
for (int i = 0; i <= (n * n + 1); i++)
primesquare[i] = false;
// 1 is not a prime number
prime[1] = false;
for (int p = 2; p * p <= n; p++) {
// If prime[p] is not changed, then
// it is a prime
if (prime[p] == true) {
// Update all multiples of p
for (int i = p * 2; i <= n; i += p)
prime[i] = false;
}
}
int j = 0;
for (int p = 2; p <= n; p++) {
if (prime[p]) {
// Storing primes in an array
a[j] = p;
// Update value in primesquare[p*p],
// if p is prime.
primesquare[p * p] = true;
j++;
}
}
}
// Function to count divisors
int countDivisors(int n) {
// If number is 1, then it will have only 1
// as a factor. So, total factors will be 1.
if (n == 1)
return 1;
bool prime[n + 1], primesquare[n * n + 1];
int a[n]; // for storing primes upto n
// Calling SieveOfEratosthenes to store prime
// factors of n and to store square of prime
// factors of n
SieveOfEratosthenes(n, prime, primesquare, a);
// ans will contain total number of distinct
// divisors
int ans = 1;
// Loop for counting factors of n
for (int i = 0;; i++) {
// a[i] is not less than cube root n
if (a[i] * a[i] * a[i] > n)
break;
// Calculating power of a[i] in n.
int cnt = 1; // cnt is power of prime a[i] in n.
while (n % a[i] == 0) // if a[i] is a factor of n
{
n = n / a[i];
cnt = cnt + 1; // incrementing power
}
// Calculating number of divisors
// If n = a^p * b^q then total divisors of n
// are (p+1)*(q+1)
ans = ans * cnt;
}
// if a[i] is greater than cube root of n
// First case
if (prime[n])
ans = ans * 2;
// Second case
else if (primesquare[n])
ans = ans * 3;
// Third casse
else if (n != 1)
ans = ans * 4;
return ans; // Total divisors
}
int32_t main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n, s = 0;
cin >> n;
for (int i = 1; i <= n; i++) {
s = s + (i * countDivisors(i));
}
cout << s << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long int
#define pb push_back
#define vi vector<int>
#define pob push_back
#define pf pop_front()
#define mp make_pair
#define loop(i, a, b) for (int i = a; i < b; ++i)
#define eloop(i, a, b) for (int i = a; i <= b; ++i)
#define rloop(i, a, b) for (int i = b - 1; i >= b; --i)
#define MAP map<int, int>
#define DQ deque<int>
#define vf(i) v[i].first
#define vs(i) v[i].second
#define mod 1000000007
#define M 998244353
#define faster \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
int binarySearch(int arr[], int l, int r, int x) {
if (r >= l) {
int mid = l + (r - l) / 2;
// If the element is present at the middle
// itself
if (arr[mid] == x)
return mid;
// If element is smaller than mid, then
// it can only be present in left subarray
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x);
// Else the element can only be present
// in right subarray
return binarySearch(arr, mid + 1, r, x);
}
// We reach here when element is not
// present in array
return -1;
}
bool isPrime(int n) {
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// This is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
bool isPowerOfTwo(int x) {
/* First x in the below expression is for the case when x is 0 */
return x && (!(x & (x - 1)));
}
int countBits(int number) {
// log function in base 2
// take only integer part
return (int)log2(number) + 1;
}
int countSetBits(int n) {
int count = 0;
while (n) {
count += n & 1;
n >>= 1;
}
return count;
}
void SieveOfEratosthenes(int n, bool prime[], bool primesquare[], int a[]) {
// Create a boolean array "prime[0..n]" and
// initialize all entries it as true. A value
// in prime[i] will finally be false if i is
// Not a prime, else true.
for (int i = 2; i <= n; i++)
prime[i] = true;
// Create a boolean array "primesquare[0..n*n+1]"
// and initialize all entries it as false. A value
// in squareprime[i] will finally be true if i is
// square of prime, else false.
for (int i = 0; i <= (n * n + 1); i++)
primesquare[i] = false;
// 1 is not a prime number
prime[1] = false;
for (int p = 2; p * p <= n; p++) {
// If prime[p] is not changed, then
// it is a prime
if (prime[p] == true) {
// Update all multiples of p
for (int i = p * 2; i <= n; i += p)
prime[i] = false;
}
}
int j = 0;
for (int p = 2; p <= n; p++) {
if (prime[p]) {
// Storing primes in an array
a[j] = p;
// Update value in primesquare[p*p],
// if p is prime.
primesquare[p * p] = true;
j++;
}
}
}
// Function to count divisors
int countDivisors(int n) {
// If number is 1, then it will have only 1
// as a factor. So, total factors will be 1.
if (n == 1)
return 1;
bool prime[n + 1], primesquare[n * n + 1];
int a[n]; // for storing primes upto n
// Calling SieveOfEratosthenes to store prime
// factors of n and to store square of prime
// factors of n
SieveOfEratosthenes(n, prime, primesquare, a);
// ans will contain total number of distinct
// divisors
int ans = 1;
// Loop for counting factors of n
for (int i = 0;; i++) {
// a[i] is not less than cube root n
if (a[i] * a[i] * a[i] > n)
break;
// Calculating power of a[i] in n.
int cnt = 1; // cnt is power of prime a[i] in n.
while (n % a[i] == 0) // if a[i] is a factor of n
{
n = n / a[i];
cnt = cnt + 1; // incrementing power
}
// Calculating number of divisors
// If n = a^p * b^q then total divisors of n
// are (p+1)*(q+1)
ans = ans * cnt;
}
// if a[i] is greater than cube root of n
// First case
if (prime[n])
ans = ans * 2;
// Second case
else if (primesquare[n])
ans = ans * 3;
// Third casse
else if (n != 1)
ans = ans * 4;
return ans; // Total divisors
}
int32_t main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n, s = 0;
cin >> n;
for (int i = 1; i <= n; i++) {
int y = n / i;
int ans = (y * (y + 1) * i) / 2;
s += ans;
}
cout << s << endl;
return 0;
}
| replace | 191 | 192 | 191 | 194 | TLE | |
p02624 | C++ | Time Limit Exceeded | // #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define INF 0x6fffffff
#define INFL 0x6fffffffffffffffLL
int main() {
ll a, b, c, h, i, j, k, l, m, n, x, y;
ll ans = 0;
string s;
cin >> n;
for (a = 1; a <= n; a++) {
ll fk = 0;
for (i = 1; i * i <= a; i++) {
if ((a % i) == 0) {
fk++;
if (a / i != i)
fk++;
}
}
// cout << "fk:" << fk << endl;
ans += a * fk;
}
cout << ans << endl;
return 0;
}
| // #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define INF 0x6fffffff
#define INFL 0x6fffffffffffffffLL
int main() {
ll a, b, c, h, i, j, k, l, m, n, x, y;
ll ans = 0;
string s;
cin >> n;
for (i = 1; i <= n; i++) {
ans += (n - (n % i) + i) * (n / i) / 2;
}
cout << ans << endl;
return 0;
}
| replace | 13 | 24 | 13 | 16 | TLE | |
p02624 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define debug printf("\n<<CameHere<<\n")
#define mem(x, y) memset(x, y, sizeof(x))
#define all(qz) qz.begin(), qz.end()
#define rall(qz) qz.rbegin(), qz.rend()
#define inf LLONG_MIN
#define INF LLONG_MAX
#define mod 1000000007
#define Sqr(x) ((x) * (x))
#define ll long long
#define mk make_pair
#define pb push_back
#define pi acos(-1.0)
#define cos(a) cos(a *pi / 180)
#define sin(a) sin(a *pi / 180)
#define tan(a) tan(a *pi / 180)
ll pow(ll c, ll d) { return d == 0 ?: c * pow(c, d - 1); }
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return ((a * b) / gcd(a, b)); }
/*---------AL ASAD NUR RIYAD------------*/
#define mx 20000009
ll ar[mx + 100];
void fn() {
for (ll i = 2; i <= mx / 2; i++) {
for (ll j = i * 2; j <= mx; j += i) {
ar[j]++;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
fn();
ll n;
cin >> n;
ll cnt = 1;
for (ll i = 2; i <= n; i++) {
cnt += (i * (ar[i] + 2));
}
cout << cnt << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define debug printf("\n<<CameHere<<\n")
#define mem(x, y) memset(x, y, sizeof(x))
#define all(qz) qz.begin(), qz.end()
#define rall(qz) qz.rbegin(), qz.rend()
#define inf LLONG_MIN
#define INF LLONG_MAX
#define mod 1000000007
#define Sqr(x) ((x) * (x))
#define ll long long
#define mk make_pair
#define pb push_back
#define pi acos(-1.0)
#define cos(a) cos(a *pi / 180)
#define sin(a) sin(a *pi / 180)
#define tan(a) tan(a *pi / 180)
ll pow(ll c, ll d) { return d == 0 ?: c * pow(c, d - 1); }
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return ((a * b) / gcd(a, b)); }
/*---------AL ASAD NUR RIYAD------------*/
#define mx 10000009
ll ar[mx + 100];
void fn() {
for (ll i = 2; i <= mx / 2; i++) {
for (ll j = i * 2; j <= mx; j += i) {
ar[j]++;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
fn();
ll n;
cin >> n;
ll cnt = 1;
for (ll i = 2; i <= n; i++) {
cnt += (i * (ar[i] + 2));
}
cout << cnt << endl;
return 0;
}
| replace | 22 | 23 | 22 | 23 | TLE | |
p02624 | C++ | Memory Limit Exceeded | #include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
typedef pair<int, int> P;
const int INF = 1001001001;
const long double PI = (acos(-1));
const int mod = 1e9 + 7;
const int vx[6] = {0, 1, 0, -1, 1, -1};
const int vy[6] = {1, 0, -1, 0, 1, 1};
ll f(ll a) {
ll rep = 0;
for (ll i = 1; i <= a / 2; ++i) {
if (a % i == 0) {
rep++;
}
}
return rep + 1;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
ll n;
cin >> n;
ll ans = 0;
vector<ll> dp(200000000, 2);
dp[0] = 0;
dp[1] = 1;
rep(i, n / 2 + 1) {
if (i == 0 || i == 1)
continue;
for (ll j = i + i; j <= n; j += i) {
dp[j]++;
}
}
rep(i, n + 1) { ans += i * dp[i]; }
cout << ans << endl;
cout << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
typedef pair<int, int> P;
const int INF = 1001001001;
const long double PI = (acos(-1));
const int mod = 1e9 + 7;
const int vx[6] = {0, 1, 0, -1, 1, -1};
const int vy[6] = {1, 0, -1, 0, 1, 1};
ll f(ll a) {
ll rep = 0;
for (ll i = 1; i <= a / 2; ++i) {
if (a % i == 0) {
rep++;
}
}
return rep + 1;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
ll n;
cin >> n;
ll ans = 0;
vector<ll> dp(20000000, 2);
dp[0] = 0;
dp[1] = 1;
rep(i, n / 2 + 1) {
if (i == 0 || i == 1)
continue;
for (ll j = i + i; j <= n; j += i) {
dp[j]++;
}
}
rep(i, n + 1) { ans += i * dp[i]; }
cout << ans << endl;
cout << endl;
return 0;
}
| replace | 31 | 32 | 31 | 32 | MLE | |
p02624 | C++ | Time Limit Exceeded | /*input
*/
// assic value of ('0'-'9') is(48 - 57) and (a-z) is (97-122) and (A-Z)
// is(65-90) and 32 for space #pragma GCC target ("avx2") #pragma GCC
// optimization ("O3") #pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// order_of_key (k) : Number of items strictly smaller than k .
// find_by_order(k) : K-th element in a set (counting from zero).
using namespace __gnu_pbds;
template <class T>
using oset =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long int
#define pb push_back
#define pii pair<ll, ll>
#define vpii vector<pii>
#define vi vector<ll>
#define vs vector<string>
#define vvi vector<vector<ll>>
#define inf (ll)1e18
#define all(it, a) for (auto it = (a).begin(); it != (a).end(); it++)
#define F first
#define S second
#define sz(x) (ll) x.size()
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, a, b) for (ll i = a; i > b; i--)
#define lbnd lower_bound
#define ubnd upper_bound
#define mp make_pair
#define whatis(x) cout << #x << " is " << x << "\n";
#define graph(n) adj(n, vector<ll>())
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define debug(x) cout << #x << " " << x << endl;
#define debug_p(x) cout << #x << " " << x.F << " " << x.S << endl;
#define debug_v(x) \
{ \
cout << #x << " "; \
for (auto ioi : x) \
cout << ioi << " "; \
cout << endl; \
}
#define debug_vp(x) \
{ \
cout << #x << " "; \
for (auto ioi : x) \
cout << '[' << ioi.F << " " << ioi.S << ']'; \
cout << endl; \
}
#define debug_v_v(x) \
{ \
cout << #x << "/*\n"; \
for (auto ioi : x) { \
for (auto ioi2 : ioi) \
cout << ioi2 << " "; \
cout << '\n'; \
} \
cout << "*/" << #x << endl; \
}
const ll N = 1e7 + 5;
vi spd(N, -1);
void init() {
rep(i, 2, N) {
if (spd[i] == -1) {
spd[i] = i;
for (ll j = i * i; j < N; j += i)
if (spd[j] == -1)
spd[j] = i;
}
}
return;
}
int solve() {
init();
ll n;
cin >> n;
ll ans = 1;
rep(i, 2, n + 1) {
unordered_map<ll, ll> cnt;
ll temp = 1;
ll dup = i;
while (dup > 1)
cnt[spd[dup]]++, dup /= spd[dup];
all(it, cnt) temp *= (it->S + 1);
ans = ans + i * temp;
}
cout << ans << "\n";
return 0;
}
int main() {
auto start = chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll test_cases = 1;
// cin>>test_cases;
while (test_cases--)
solve();
auto stop = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::milliseconds>(stop - start);
// cout<<"\nduration: "<<(double)duration.count()<<" milliseconds";
} | /*input
*/
// assic value of ('0'-'9') is(48 - 57) and (a-z) is (97-122) and (A-Z)
// is(65-90) and 32 for space #pragma GCC target ("avx2") #pragma GCC
// optimization ("O3") #pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// order_of_key (k) : Number of items strictly smaller than k .
// find_by_order(k) : K-th element in a set (counting from zero).
using namespace __gnu_pbds;
template <class T>
using oset =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long int
#define pb push_back
#define pii pair<ll, ll>
#define vpii vector<pii>
#define vi vector<ll>
#define vs vector<string>
#define vvi vector<vector<ll>>
#define inf (ll)1e18
#define all(it, a) for (auto it = (a).begin(); it != (a).end(); it++)
#define F first
#define S second
#define sz(x) (ll) x.size()
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, a, b) for (ll i = a; i > b; i--)
#define lbnd lower_bound
#define ubnd upper_bound
#define mp make_pair
#define whatis(x) cout << #x << " is " << x << "\n";
#define graph(n) adj(n, vector<ll>())
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define debug(x) cout << #x << " " << x << endl;
#define debug_p(x) cout << #x << " " << x.F << " " << x.S << endl;
#define debug_v(x) \
{ \
cout << #x << " "; \
for (auto ioi : x) \
cout << ioi << " "; \
cout << endl; \
}
#define debug_vp(x) \
{ \
cout << #x << " "; \
for (auto ioi : x) \
cout << '[' << ioi.F << " " << ioi.S << ']'; \
cout << endl; \
}
#define debug_v_v(x) \
{ \
cout << #x << "/*\n"; \
for (auto ioi : x) { \
for (auto ioi2 : ioi) \
cout << ioi2 << " "; \
cout << '\n'; \
} \
cout << "*/" << #x << endl; \
}
const ll N = 1e7 + 5;
vi spd(N, -1);
void init() {
rep(i, 2, N) {
if (spd[i] == -1) {
spd[i] = i;
for (ll j = i * i; j < N; j += i)
if (spd[j] == -1)
spd[j] = i;
}
}
return;
}
int solve() {
init();
ll n;
cin >> n;
ll ans = 0;
vi cnt(n + 1, 0);
rep(i, 1, n + 1) for (ll j = i; j <= n; j += i) cnt[j]++;
rep(i, 1, n + 1) ans += i * cnt[i];
cout << ans << "\n";
return 0;
}
int main() {
auto start = chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll test_cases = 1;
// cin>>test_cases;
while (test_cases--)
solve();
auto stop = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::milliseconds>(stop - start);
// cout<<"\nduration: "<<(double)duration.count()<<" milliseconds";
} | replace | 84 | 95 | 84 | 88 | TLE | |
p02624 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define arep(a, i, n) for (int i = (a); i < (n); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define cinf(x, n) \
for (int i = 0; i < (n); i++) \
cin >> x[i];
#define coutf(x, n) \
for (int i = 0; i < (n); i++) \
cout << x[i] << endl;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using namespace std;
ll a[10000001];
int main() {
ll n;
cin >> n;
ull sum = 0;
for (ll i = 1; i < n + 1; i++) {
for (ll j = i; j < n + 1; j *= i) {
a[j]++;
}
}
arep(1, i, n + 1) { sum += i * a[i]; }
cout << sum << endl;
} | #include <bits/stdc++.h>
#define arep(a, i, n) for (int i = (a); i < (n); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define cinf(x, n) \
for (int i = 0; i < (n); i++) \
cin >> x[i];
#define coutf(x, n) \
for (int i = 0; i < (n); i++) \
cout << x[i] << endl;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using namespace std;
ll a[10000001];
int main() {
ll n;
cin >> n;
ull sum = 0;
for (ll i = 1; i < n + 1; i++) {
for (ll j = i; j < n + 1; j += i) {
a[j]++;
}
}
arep(1, i, n + 1) { sum += i * a[i]; }
cout << sum << endl;
} | replace | 22 | 23 | 22 | 23 | TLE | |
p02624 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <unordered_map>
#include <vector>
#define MOD 1000000007
typedef long long int lli;
using namespace std;
int f(int n) {
int cnt = 0;
for (int i = 1; i <= sqrt(n); i++) {
if (n % i == 0) {
if (n / i == i)
cnt++;
else
cnt += 2;
}
}
return cnt;
}
int main() {
int n;
cin >> n;
lli ans = 0;
for (int k = 1; k <= n; k++) {
lli g = (lli)f(k);
ans += (k * f(k));
}
cout << ans << "\n";
return 0;
}
| #include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <unordered_map>
#include <vector>
#define MOD 1000000007
typedef long long int lli;
using namespace std;
int f(int n) {
int cnt = 0;
for (int i = 1; i <= sqrt(n); i++) {
if (n % i == 0) {
if (n / i == i)
cnt++;
else
cnt += 2;
}
}
return cnt;
}
int main() {
int n;
cin >> n;
lli ans = 0;
int dp[n + 1];
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j += i) {
dp[j]++;
}
ans += ((lli)dp[i] * i);
}
cout << ans << "\n";
return 0;
}
| replace | 33 | 36 | 33 | 40 | TLE | |
p02624 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
// 型定義
typedef long long ll;
typedef pair<ll, ll> P;
// forループ
#define REP(i, n) for (ll i = 0; i < (ll)(n); ++i)
// 定数宣言
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;
// グラフ表現
using Graph = vector<vector<int>>;
// グラフの辺表現
using Edge = map<pair<int, int>, int>;
// n次元配列の初期化。第2引数の型のサイズごとに初期化していく。
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
std::fill((T *)array, (T *)(array + N), val);
}
// 最大公約数
ll gcd(ll a, ll b) {
if (a % b == 0)
return (b);
else
return (gcd(b, a % b));
}
// 最小公倍数
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
// 約数の全列挙
vector<ll> divisor(ll n) {
vector<ll> ret;
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n)
ret.push_back(n / i);
}
}
sort(begin(ret), end(ret));
return (ret);
}
int main() {
cout << fixed << setprecision(15);
ll N;
cin >> N;
ll ans = 0;
for (ll i = 1; i <= N; i++) {
vector<ll> P = divisor(i);
ans += i * P.size();
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
// 型定義
typedef long long ll;
typedef pair<ll, ll> P;
// forループ
#define REP(i, n) for (ll i = 0; i < (ll)(n); ++i)
// 定数宣言
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;
// グラフ表現
using Graph = vector<vector<int>>;
// グラフの辺表現
using Edge = map<pair<int, int>, int>;
// n次元配列の初期化。第2引数の型のサイズごとに初期化していく。
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
std::fill((T *)array, (T *)(array + N), val);
}
// 最大公約数
ll gcd(ll a, ll b) {
if (a % b == 0)
return (b);
else
return (gcd(b, a % b));
}
// 最小公倍数
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
// 約数の全列挙
vector<ll> divisor(ll n) {
vector<ll> ret;
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n)
ret.push_back(n / i);
}
}
sort(begin(ret), end(ret));
return (ret);
}
int main() {
cout << fixed << setprecision(15);
ll N;
cin >> N;
ll ans = 0;
for (ll i = 1; i <= N; i++) {
ll k = N / i;
ans += i * k * (k + 1) / 2;
}
cout << ans << endl;
return 0;
} | replace | 59 | 62 | 59 | 61 | TLE | |
p02624 | C++ | Runtime Error | // Created by Kshitij Anand NSIT
#include <bits/stdc++.h>
// #include <ext/numeric>
// using namespace __gnu_cxx;
using namespace std;
#define int long long
#define pb push_back
#define P pair<int, int>
#define F first
#define S second
#define vi vector<int>
#define vc vector<char>
#define vb vector<bool>
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define mp(a, b) make_pair(a, b)
#define min3(a, b, c) min(min(a, b), c)
#define min4(a, b, c, d) min(min(a, b), min(c, d))
#define max3(a, b, c) max(max(a, b), c)
#define max4(a, b, c, d) max(max(a, b), max(c, d))
#define fill(arr, val) memset(arr, val, sizeof(arr))
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define db(x) cout << #x << " : " << x << endl
// #define M 10000000
const int N = 1000000007;
const int MAX = 1000001;
// array to store prime factors
int factor[MAX] = {0};
// function to generate all prime factors
// of numbers from 1 to 10^6
void generatePrimeFactors() {
factor[1] = 1;
// Initializes all the positions with their value.
for (int i = 2; i < MAX; i++)
factor[i] = i;
// Initializes all multiples of 2 with 2
for (int i = 4; i < MAX; i += 2)
factor[i] = 2;
// A modified version of Sieve of Eratosthenes to
// store the smallest prime factor that divides
// every number.
for (int i = 3; i * i < MAX; i++) {
// check if it has no prime factor.
if (factor[i] == i) {
// Initializes of j starting from i*i
for (int j = i * i; j < MAX; j += i) {
// if it has no prime factor before, then
// stores the smallest prime divisor
if (factor[j] == j)
factor[j] = i;
}
}
}
}
// function to calculate number of factors
int calculateNoOFactors(int n) {
if (n == 1)
return 1;
int ans = 1;
// stores the smallest prime number
// that divides n
int dup = factor[n];
// stores the count of number of times
// a prime number divides n.
int c = 1;
// reduces to the next number after prime
// factorization of n
int j = n / factor[n];
// false when prime factorization is done
while (j != 1) {
// if the same prime number is dividing n,
// then we increase the count
if (factor[j] == dup)
c += 1;
/* if its a new prime factor that is factorizing n,
then we again set c=1 and change dup to the new
prime factor, and apply the formula explained
above. */
else {
dup = factor[j];
ans = ans * (c + 1);
c = 1;
}
// prime factorizes a number
j = j / factor[j];
}
// for the last prime factor
ans = ans * (c + 1);
return ans;
}
void solve() {
int n, a, b, c, ans = 0, count = 0, sum = 1;
cin >> n;
// seive();
generatePrimeFactors();
for (int i = 1; i <= n; i++) {
ans += i * calculateNoOFactors(i);
}
cout << ans << endl;
// cout<<primes[n-1]<<endl;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// int t; cin>>t; while(t--)
{ solve(); }
return 0;
}
| // Created by Kshitij Anand NSIT
#include <bits/stdc++.h>
// #include <ext/numeric>
// using namespace __gnu_cxx;
using namespace std;
#define int long long
#define pb push_back
#define P pair<int, int>
#define F first
#define S second
#define vi vector<int>
#define vc vector<char>
#define vb vector<bool>
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define mp(a, b) make_pair(a, b)
#define min3(a, b, c) min(min(a, b), c)
#define min4(a, b, c, d) min(min(a, b), min(c, d))
#define max3(a, b, c) max(max(a, b), c)
#define max4(a, b, c, d) max(max(a, b), max(c, d))
#define fill(arr, val) memset(arr, val, sizeof(arr))
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define db(x) cout << #x << " : " << x << endl
// #define M 10000000
const int N = 1000000007;
const int MAX = 10000001;
// array to store prime factors
int factor[MAX] = {0};
// function to generate all prime factors
// of numbers from 1 to 10^6
void generatePrimeFactors() {
factor[1] = 1;
// Initializes all the positions with their value.
for (int i = 2; i < MAX; i++)
factor[i] = i;
// Initializes all multiples of 2 with 2
for (int i = 4; i < MAX; i += 2)
factor[i] = 2;
// A modified version of Sieve of Eratosthenes to
// store the smallest prime factor that divides
// every number.
for (int i = 3; i * i < MAX; i++) {
// check if it has no prime factor.
if (factor[i] == i) {
// Initializes of j starting from i*i
for (int j = i * i; j < MAX; j += i) {
// if it has no prime factor before, then
// stores the smallest prime divisor
if (factor[j] == j)
factor[j] = i;
}
}
}
}
// function to calculate number of factors
int calculateNoOFactors(int n) {
if (n == 1)
return 1;
int ans = 1;
// stores the smallest prime number
// that divides n
int dup = factor[n];
// stores the count of number of times
// a prime number divides n.
int c = 1;
// reduces to the next number after prime
// factorization of n
int j = n / factor[n];
// false when prime factorization is done
while (j != 1) {
// if the same prime number is dividing n,
// then we increase the count
if (factor[j] == dup)
c += 1;
/* if its a new prime factor that is factorizing n,
then we again set c=1 and change dup to the new
prime factor, and apply the formula explained
above. */
else {
dup = factor[j];
ans = ans * (c + 1);
c = 1;
}
// prime factorizes a number
j = j / factor[j];
}
// for the last prime factor
ans = ans * (c + 1);
return ans;
}
void solve() {
int n, a, b, c, ans = 0, count = 0, sum = 1;
cin >> n;
// seive();
generatePrimeFactors();
for (int i = 1; i <= n; i++) {
ans += i * calculateNoOFactors(i);
}
cout << ans << endl;
// cout<<primes[n-1]<<endl;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// int t; cin>>t; while(t--)
{ solve(); }
return 0;
}
| replace | 30 | 31 | 30 | 31 | 0 | |
p02624 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define pb push_back
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define IN freopen("input.txt", "r", stdin);
#define OUT freopen("output.txt", "w", stdout);
const int N = 2e5 + 5;
const double pi = 3.141592653589793238;
int32_t main() {
IOS;
int n;
cin >> n;
int cnt = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i % j == 0)
cnt += i;
}
}
cout << cnt << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define pb push_back
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define IN freopen("input.txt", "r", stdin);
#define OUT freopen("output.txt", "w", stdout);
const int N = 2e5 + 5;
const double pi = 3.141592653589793238;
int32_t main() {
IOS;
int n;
cin >> n;
int cnt = 0;
for (int i = 1; i <= n; i++) {
int num = n / i;
cnt += i * (num + 1) * num / 2;
}
cout << cnt << endl;
}
| replace | 22 | 26 | 22 | 24 | TLE | |
p02624 | C++ | Time Limit Exceeded | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
/* template */
using ll = long long;
void debug_out() { std::cout << std::endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cout << H << " ";
debug_out(T...);
}
#ifdef LOCAL
#define debug(...) \
cout << "debug: "; \
debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
#define rep(i, a, n) for (int i = (int)(a); i < (int)(n); i++)
#define rrep(i, a, n) for (int i = ((int)(n - 1)); i >= (int)(a); i--)
#define Rep(i, a, n) for (long long i = (long long)(a); i < (long long)(n); i++)
#define RRep(i, a, n) \
for (long long i = ((long long)(n - 1ll)); i >= (long long)(a); i--)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
template <typename T>
std::ostream &operator<<(std::ostream &os, std::vector<T> vec) {
for (std::size_t i = 0; i < vec.size(); i++)
os << vec[i] << (i + 1 == vec.size() ? "" : " ");
return os;
}
struct Edge {
int to;
ll weight;
Edge(int t, ll w) : to(t), weight(w) {}
};
struct edge {
int from;
int to;
ll weight;
edge(int f, int t, ll w) : from(f), to(t), weight(w) {}
};
using Graph = vector<vector<Edge>>;
using graph = vector<vector<int>>;
using edges = vector<edge>;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> T lcm(T a, T b) { return (a * b) / gcd(a, b); }
ll ctoi(char c) {
switch (c) {
case '0':
return 0;
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
default:
return 0;
}
}
constexpr ll LNF = 1LL << 50;
constexpr int INF = 1e9 + 7;
const long double PI = 3.14159265358979323846;
vector<int> dx = {1, 0, -1, 1, -1, 0};
vector<int> dy = {1, 1, 1, 0, 0, -1};
/* template */
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
ll n;
cin >> n;
vector<ll> a(n + 1);
vector<map<ll, ll>> b(n + 1);
vector<ll> p(n + 1, -1);
rep(i, 2, n + 1) {
if (p[i] == -1) {
for (int j = 2 * i; j <= n; j += i) {
p[j] = i;
}
}
}
a[1] = 1;
ll ans = 1ll;
rep(i, 2, n + 1) {
ll x = 0;
if (p[i] == -1) {
x = 2;
b[i][i]++;
} else {
ll y = i / p[i];
ll t = b[y][p[i]];
x = a[y] / (t + 1ll);
x *= t + 2ll;
b[i] = b[y];
b[i][p[i]]++;
}
a[i] = x;
ans += x * i;
}
cout << ans << endl;
} | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
/* template */
using ll = long long;
void debug_out() { std::cout << std::endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cout << H << " ";
debug_out(T...);
}
#ifdef LOCAL
#define debug(...) \
cout << "debug: "; \
debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
#define rep(i, a, n) for (int i = (int)(a); i < (int)(n); i++)
#define rrep(i, a, n) for (int i = ((int)(n - 1)); i >= (int)(a); i--)
#define Rep(i, a, n) for (long long i = (long long)(a); i < (long long)(n); i++)
#define RRep(i, a, n) \
for (long long i = ((long long)(n - 1ll)); i >= (long long)(a); i--)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
template <typename T>
std::ostream &operator<<(std::ostream &os, std::vector<T> vec) {
for (std::size_t i = 0; i < vec.size(); i++)
os << vec[i] << (i + 1 == vec.size() ? "" : " ");
return os;
}
struct Edge {
int to;
ll weight;
Edge(int t, ll w) : to(t), weight(w) {}
};
struct edge {
int from;
int to;
ll weight;
edge(int f, int t, ll w) : from(f), to(t), weight(w) {}
};
using Graph = vector<vector<Edge>>;
using graph = vector<vector<int>>;
using edges = vector<edge>;
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> T lcm(T a, T b) { return (a * b) / gcd(a, b); }
ll ctoi(char c) {
switch (c) {
case '0':
return 0;
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
default:
return 0;
}
}
constexpr ll LNF = 1LL << 50;
constexpr int INF = 1e9 + 7;
const long double PI = 3.14159265358979323846;
vector<int> dx = {1, 0, -1, 1, -1, 0};
vector<int> dy = {1, 1, 1, 0, 0, -1};
/* template */
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
ll n;
cin >> n;
ll ans = 0;
Rep(i, 1, n + 1) {
ll res = n / i;
ans += i * res * (res + 1) / 2;
}
cout << ans << endl;
} | replace | 135 | 162 | 135 | 139 | TLE | |
p02624 | C++ | Runtime Error | #include <iostream>
#include <numeric>
#include <vector>
using namespace std;
struct _ {
ios_base::Init i;
_() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); }
} _;
vector<long long> spf(1e7 + 1, 0);
int main() {
long long n;
cin >> n;
iota(spf.begin(), spf.begin() + n, 0);
for (long long i = 2; i * i <= n; i++) {
for (long long j = i * i; j <= n; j += i)
spf[j] = min(spf[j], i);
}
long long ans = 0;
for (long long i = 1; i <= n; i++) {
long long x = i, cnt = 1;
while (x != 1) {
long long pfac = spf[x], temp = 0;
while (x % pfac == 0)
x /= pfac, temp++;
cnt *= (temp + 1);
}
ans += (i * cnt);
}
cout << ans;
return 0;
} | #include <iostream>
#include <numeric>
#include <vector>
using namespace std;
struct _ {
ios_base::Init i;
_() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); }
} _;
vector<long long> spf(1e7 + 1, 0);
int main() {
long long n;
cin >> n;
iota(spf.begin(), spf.begin() + n + 1, 0);
for (long long i = 2; i * i <= n; i++) {
for (long long j = i * i; j <= n; j += i)
spf[j] = min(spf[j], i);
}
long long ans = 0;
for (long long i = 1; i <= n; i++) {
long long x = i, cnt = 1;
while (x != 1) {
long long pfac = spf[x], temp = 0;
while (x % pfac == 0)
x /= pfac, temp++;
cnt *= (temp + 1);
}
ans += (i * cnt);
}
cout << ans;
return 0;
} | replace | 17 | 18 | 17 | 18 | -8 | |
p02624 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<vector<int>> vvi;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vector<string>> vvs;
typedef vector<ll> vll;
typedef pair<int, int> P;
#define mp(x, y) make_pair(x, y)
typedef queue<int> qi;
typedef queue<string> qs;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repp(i, a, b) for (int i = (a); i <= (b); i++)
#define repm(i, n) for (int i = n; i >= 0; i--)
#define all(v) v.begin(), v.end()
// sort( all(v) ) などと使える
ll f(ll x) {
ll cnt = 0;
for (ll i = 1; i * i <= x; i++) {
if (x % i == 0)
cnt++;
if (x % i == 0 && i * i != x)
cnt++;
}
return cnt;
}
int main(void) {
ll n;
cin >> n;
ll ans = 0;
// cout << f(n) << endl;
for (ll k = 1; k <= n; k++) {
ans += k * f(k);
}
cout << ans << endl;
}
| #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<vector<int>> vvi;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vector<string>> vvs;
typedef vector<ll> vll;
typedef pair<int, int> P;
#define mp(x, y) make_pair(x, y)
typedef queue<int> qi;
typedef queue<string> qs;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repp(i, a, b) for (int i = (a); i <= (b); i++)
#define repm(i, n) for (int i = n; i >= 0; i--)
#define all(v) v.begin(), v.end()
// sort( all(v) ) などと使える
ll f(ll x) {
ll cnt = 0;
for (ll i = 1; i * i <= x; i++) {
if (x % i == 0)
cnt++;
if (x % i == 0 && i * i != x)
cnt++;
}
return cnt;
}
int main(void) {
ll n;
cin >> n;
ll ans = 0;
// cout << f(n) << endl;
for (ll j = 1; j <= n; j++) {
// jの倍数はqコあるよ
ll q = n / j;
// jの倍数 * jはj + 2 * j + ・・・ + q * j
// = q * (q + 1) / 2 * j
ans += j * q * (q + 1) / 2;
}
cout << ans << endl;
}
| replace | 56 | 58 | 56 | 62 | TLE | |
p02624 | C++ | Time Limit Exceeded | #include <bits/stdc++.h> // This will work only for g++ compiler.
#include <ext/pb_ds/assoc_container.hpp> // Used to include policy based data structure
using namespace __gnu_pbds; // Only for g++ compiler
using namespace std;
#define for0(i, n) for (int i = 0; i < (int)(n); ++i) // 0 based indexing
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i) // 1 based indexing
#define forc(i, l, r) \
for (int i = (int)(l); i <= (int)(r); \
++i) // closed interver from l to r r inclusive
#define forr0(i, n) for (int i = (int)(n)-1; i >= 0; --i) // reverse 0 based.
#define forr1(i, n) for (int i = (int)(n); i >= 1; --i) // reverse 1 based
#define max(a, b) (a > b) ? a : b // max of 2 elements
#define min(a, b) (a < b) ? a : b // min of 2 elements
// short hand for usual tokens
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define mii map<int, int>
#define pqb priority_queue<int> // Max Heap
#define pqs priority_queue<int, vi, greater<int>> // Min Heap
#define setbits(x) \
__builtin_popcountll( \
x) // returns the number of set bits in the binary representation of x
#define zrobits(x) \
__builtin_ctzll(x) // returns the number of zeroes before the first set bit in
// the binary representation of x
#define mod 1000000007 // 1e9 + 7
#define inf 1e18 // pow(10, 18)
#define mk(arr, n, type) type *arr = new type[n]; // generates a dynamic array
#define w(x) \
int x; \
cin >> x; \
while (x--) // for taking test cases
mt19937 rng(chrono::steady_clock::now()
.time_since_epoch()
.count()); // used with shuffle
// to be used with algorithms that processes a container Eg: find(all(c),42)
#define all(x) (x).begin(), (x).end() // Forward traversal
#define rall(x) (x).rbegin, (x).rend() // reverse traversal
// traversal function to avoid long template definition. Now with C++11 auto
// alleviates the pain.
#define tr(c, i) \
for (__typeof__((c)).begin() i = (c).begin(); i != (c).end(); i++)
// find if a given value is present in a container. Container version. Runs in
// log(n) for set and map
#define present(c, x) ((c).find(x) != (c).end())
// find version works for all containers. This is present in std namespace.
#define cpresent(c, x) (find(all(c), x) != (c).end())
// Avoiding wrap around of size()-1 where size is a unsigned int.
#define sz(a) int((a).size())
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
pbds; // Used to implement policy-based data structure
// Shorthand for commonly used types
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef double ld;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.precision(10);
cout << fixed;
ll n;
cin >> n;
ll ans = 0LL;
for (ll i = 1; i <= n; i++) {
for (ll j = 1; j <= n; j++) {
if (i % j == 0)
ans += i;
}
}
cout << ans;
return 0;
}
| #include <bits/stdc++.h> // This will work only for g++ compiler.
#include <ext/pb_ds/assoc_container.hpp> // Used to include policy based data structure
using namespace __gnu_pbds; // Only for g++ compiler
using namespace std;
#define for0(i, n) for (int i = 0; i < (int)(n); ++i) // 0 based indexing
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i) // 1 based indexing
#define forc(i, l, r) \
for (int i = (int)(l); i <= (int)(r); \
++i) // closed interver from l to r r inclusive
#define forr0(i, n) for (int i = (int)(n)-1; i >= 0; --i) // reverse 0 based.
#define forr1(i, n) for (int i = (int)(n); i >= 1; --i) // reverse 1 based
#define max(a, b) (a > b) ? a : b // max of 2 elements
#define min(a, b) (a < b) ? a : b // min of 2 elements
// short hand for usual tokens
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define mii map<int, int>
#define pqb priority_queue<int> // Max Heap
#define pqs priority_queue<int, vi, greater<int>> // Min Heap
#define setbits(x) \
__builtin_popcountll( \
x) // returns the number of set bits in the binary representation of x
#define zrobits(x) \
__builtin_ctzll(x) // returns the number of zeroes before the first set bit in
// the binary representation of x
#define mod 1000000007 // 1e9 + 7
#define inf 1e18 // pow(10, 18)
#define mk(arr, n, type) type *arr = new type[n]; // generates a dynamic array
#define w(x) \
int x; \
cin >> x; \
while (x--) // for taking test cases
mt19937 rng(chrono::steady_clock::now()
.time_since_epoch()
.count()); // used with shuffle
// to be used with algorithms that processes a container Eg: find(all(c),42)
#define all(x) (x).begin(), (x).end() // Forward traversal
#define rall(x) (x).rbegin, (x).rend() // reverse traversal
// traversal function to avoid long template definition. Now with C++11 auto
// alleviates the pain.
#define tr(c, i) \
for (__typeof__((c)).begin() i = (c).begin(); i != (c).end(); i++)
// find if a given value is present in a container. Container version. Runs in
// log(n) for set and map
#define present(c, x) ((c).find(x) != (c).end())
// find version works for all containers. This is present in std namespace.
#define cpresent(c, x) (find(all(c), x) != (c).end())
// Avoiding wrap around of size()-1 where size is a unsigned int.
#define sz(a) int((a).size())
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
pbds; // Used to implement policy-based data structure
// Shorthand for commonly used types
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef double ld;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.precision(10);
cout << fixed;
ll n;
cin >> n;
ll ans = 0LL;
for (ll i = 1; i <= n; i++) {
ll temp = n / i;
ans += (temp * (temp + 1) * i) / 2;
}
cout << ans;
return 0;
} | replace | 83 | 87 | 83 | 85 | TLE | |
p02624 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
#define endl '\n'
#define pi 2 * acos(0.0)
#define pb(a) push_back(a)
#define db double
#define ld long double
#define ll long long
#define ull unsigned long long
#define mod 1000000007
#define inf 1000000000000000001
#define sqr(x) (x) * (x)
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) ((a / gcd(a, b)) * b)
#define debug(x) cerr << #x << " = " << (x) << endl
#define all(x) (x).begin(), (x).end()
#define unsyncIO \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
using namespace std;
int main() {
// freopen("/home/taalpatar_shepai/Documents/my_codes/out.txt", "w", stdout);
unsyncIO;
ll n, ans = 0;
cin >> n;
for (ll j = 1; j <= n; j++) {
for (ll i = 1; i <= n; i++) {
if (i % j == 0)
ans += i;
}
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#define endl '\n'
#define pi 2 * acos(0.0)
#define pb(a) push_back(a)
#define db double
#define ld long double
#define ll long long
#define ull unsigned long long
#define mod 1000000007
#define inf 1000000000000000001
#define sqr(x) (x) * (x)
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) ((a / gcd(a, b)) * b)
#define debug(x) cerr << #x << " = " << (x) << endl
#define all(x) (x).begin(), (x).end()
#define unsyncIO \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
using namespace std;
int main() {
// freopen("/home/taalpatar_shepai/Documents/my_codes/out.txt", "w", stdout);
unsyncIO;
ll n, ans = 0;
cin >> n;
for (ll j = 1; j <= n; j++) {
ll t = n / j;
ans += t * (t + 1) / 2 * j;
}
cout << ans << endl;
return 0;
} | replace | 28 | 32 | 28 | 30 | TLE | |
p02624 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ll n;
cin >> n;
ll a = 0;
for (ll i = 1; i <= n; i++) {
for (ll j = 1; j <= n; j++) {
if (i % j == 0)
a += i;
}
}
cout << a;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ll n;
cin >> n;
ll a = 0;
for (ll i = 1; i <= n; i++) {
for (ll j = i; j <= n; j += i) {
a += j;
}
}
cout << a;
}
| replace | 8 | 11 | 8 | 10 | TLE | |
p02624 | C++ | Time Limit Exceeded | #include <iostream>
using namespace std;
int main() {
long long int n, i, j, s = 0, sum = 0;
cin >> n;
for (i = 1; i <= n; i++) {
for (j = 1; j <= i; j++) {
if (i % j == 0)
s++;
}
sum += (s * i);
s = 0;
}
cout << sum;
}
| #include <iostream>
using namespace std;
int main() {
long long int n, i, j, s = 0, sum = 0;
cin >> n;
for (i = 1; i <= n; i++) {
for (j = i; j <= n; j = j + i)
sum += j;
}
cout << sum;
}
| replace | 6 | 12 | 6 | 8 | TLE | |
p02624 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ll n;
cin >> n;
map<ll, int> mp;
ll ans = 0;
for (int i = 1; i <= n; ++i) {
for (int j = i; j <= n; j = j + i) {
mp[j]++;
}
ans += i * mp[i];
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ll n;
cin >> n;
// map<ll,int> mp;
vector<ll> mp(n + 1, 0);
ll ans = 0;
for (int i = 1; i <= n; ++i) {
for (int j = i; j <= n; j = j + i) {
mp[j]++;
}
ans += i * mp[i];
}
cout << ans << endl;
return 0;
}
| replace | 7 | 8 | 7 | 9 | TLE | |
p02624 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define pb push_back
using namespace std;
typedef long long ll;
int main() {
int n;
cin >> n;
vector<ll> p(n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n / i; j++) {
p[i * j - 14]++;
}
}
ll ans = 0;
rep(i, n) { ans += (i + 1) * p[i]; }
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define pb push_back
using namespace std;
typedef long long ll;
int main() {
int n;
cin >> n;
vector<ll> p(n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n / i; j++) {
p[i * j - 1]++;
}
}
ll ans = 0;
rep(i, n) { ans += (i + 1) * p[i]; }
cout << ans << endl;
return 0;
}
| replace | 12 | 13 | 12 | 13 | 0 | |
p02624 | C++ | Time Limit Exceeded | #include <iostream>
#include <string>
#include <vector>
using namespace std;
#define ll long long
int main() {
// INPUT
ll N = 0;
cin >> N;
// SOURCE
ll ans = 0;
for (ll i = 1; i < N + 1; ++i) {
for (ll j = 1; j <= i; ++j) {
if (i % j == 0)
ans += i;
}
}
// OUTPUT
cout << ans << endl;
return 0;
}
| #include <iostream>
#include <string>
#include <vector>
using namespace std;
#define ll long long
int main() {
// INPUT
ll N = 0;
cin >> N;
// SOURCE
ll ans = 0;
for (ll i = 1; i < N + 1; ++i) {
ll Y = N / i;
ans += (Y * (Y + 1) * i) / 2;
}
// OUTPUT
cout << ans << endl;
return 0;
}
| replace | 13 | 17 | 13 | 15 | TLE | |
p02624 | C++ | Time Limit Exceeded | #include <cstdio>
using namespace std;
#define ll long long
int n;
int main() {
scanf("%d", &n);
ll ans = 0;
for (int i = 1; i <= n; i++) {
ll sum = 0;
for (int j = 1; j <= i; j++)
if (i % j == 0)
sum++;
ans += 1ll * i * sum;
}
printf("%lld", ans);
return 0;
} | #include <cstdio>
using namespace std;
#define ll long long
int n;
int main() {
scanf("%d", &n);
ll ans = 0;
for (int i = 1; i <= n; i++) {
ll x = n / i;
ans += x * (x + 1) / 2 * i;
}
printf("%lld", ans);
return 0;
} | replace | 11 | 16 | 11 | 13 | TLE | |
p02624 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
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;
}
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const int MAX = 510000;
const double pi = acos(-1);
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};
// int d[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll n;
cin >> n;
ll res = 0;
for (int i = 1; i <= n; i++) {
ll x = n / x;
res += (x + 1) * i / 2;
}
cout << res << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
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;
}
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const int MAX = 510000;
const double pi = acos(-1);
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};
// int d[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll n;
cin >> n;
ll res = 0;
for (ll i = 1; i <= n; i++) {
ll x = n / i;
res += x * (x + 1) * i / 2;
}
cout << res << endl;
return 0;
}
| replace | 37 | 40 | 37 | 40 | -8 | |
p02624 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define ff first
#define ss second
#define file_se_input \
freopen("input.txt", "r", stdin); \
freopen("output.txt", "w", stdout);
#define pp long long int
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define pb push_back
#define mp make_pair
#define pi 3.141592653589793238
#define eb emplace_back
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define zip(i, a, b) for (int i = a; i < b; i++)
#define rzip(i, a, b) for (int i = a; i >= b; i--)
#define ll unsigned long long int
#define test \
int t; \
cin >> t; \
while (t--)
#define um unordered_map
#define en '\n'
#define us unordered_set
typedef pair<int, int> pii;
typedef pair<char, int> pci;
typedef pair<char, char> pcc;
typedef vector<pii> vii;
typedef long double ld;
#define all(v) v.begin(), v.end()
#define INF (1e18 + 5)
#define inf (1e9 + 5)
#define mod 1000000007
bool check_prime(long long n) {
int flag = 0;
for (long long i = 2; i * i <= n; i++) {
if (n % i == 0) {
flag = 1;
break;
}
}
if (n == 1)
return false;
else if (flag == 0 || n == 2 || n == 3) {
return true;
} else {
return false;
}
}
int BE(int x, int n, int m) // function to calculate x raise to power n modulo m
{
int result = 1;
while (n > 0) {
if (n % 2 == 1)
result = result * x % m;
x = x * x % m;
n = n / 2;
}
return result;
}
void SieveOfEratosthenes(int n, bool prime[], bool primesquare[], int a[]) {
// Create a boolean array "prime[0..n]" and
// initialize all entries it as true. A value
// in prime[i] will finally be false if i is
// Not a prime, else true.
for (int i = 2; i <= n; i++)
prime[i] = true;
// Create a boolean array "primesquare[0..n*n+1]"
// and initialize all entries it as false. A value
// in squareprime[i] will finally be true if i is
// square of prime, else false.
for (int i = 0; i <= (n * n + 1); i++)
primesquare[i] = false;
// 1 is not a prime number
prime[1] = false;
for (int p = 2; p * p <= n; p++) {
// If prime[p] is not changed, then
// it is a prime
if (prime[p] == true) {
// Update all multiples of p
for (int i = p * 2; i <= n; i += p)
prime[i] = false;
}
}
int j = 0;
for (int p = 2; p <= n; p++) {
if (prime[p]) {
// Storing primes in an array
a[j] = p;
// Update value in primesquare[p*p],
// if p is prime.
primesquare[p * p] = true;
j++;
}
}
}
// Function to count divisors
int countDivisors(int n) {
// If number is 1, then it will have only 1
// as a factor. So, total factors will be 1.
if (n == 1)
return 1;
bool prime[n + 1], primesquare[n * n + 1];
int a[n]; // for storing primes upto n
// Calling SieveOfEratosthenes to store prime
// factors of n and to store square of prime
// factors of n
SieveOfEratosthenes(n, prime, primesquare, a);
// ans will contain total number of distinct
// divisors
int ans = 1;
// Loop for counting factors of n
for (int i = 0;; i++) {
// a[i] is not less than cube root n
if (a[i] * a[i] * a[i] > n)
break;
// Calculating power of a[i] in n.
int cnt = 1; // cnt is power of prime a[i] in n.
while (n % a[i] == 0) // if a[i] is a factor of n
{
n = n / a[i];
cnt = cnt + 1; // incrementing power
}
// Calculating number of divisors
// If n = a^p * b^q then total divisors of n
// are (p+1)*(q+1)
ans = ans * cnt;
}
// if a[i] is greater than cube root of n
// First case
if (prime[n])
ans = ans * 2;
// Second case
else if (primesquare[n])
ans = ans * 3;
// Third casse
else if (n != 1)
ans = ans * 4;
return ans; // Total divisors
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);
#ifndef ONLINE_JUDGE
if (fopen("input.txt", "r")) {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
#endif
//_______________________________-code
//starts-_______________________________________________
int n;
cin >> n;
int ans = 0;
rep(i, 1, n) { ans += i * (countDivisors(i)); }
cout << ans << en;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define ff first
#define ss second
#define file_se_input \
freopen("input.txt", "r", stdin); \
freopen("output.txt", "w", stdout);
#define pp long long int
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define pb push_back
#define mp make_pair
#define pi 3.141592653589793238
#define eb emplace_back
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define zip(i, a, b) for (int i = a; i < b; i++)
#define rzip(i, a, b) for (int i = a; i >= b; i--)
#define ll unsigned long long int
#define test \
int t; \
cin >> t; \
while (t--)
#define um unordered_map
#define en '\n'
#define us unordered_set
typedef pair<int, int> pii;
typedef pair<char, int> pci;
typedef pair<char, char> pcc;
typedef vector<pii> vii;
typedef long double ld;
#define all(v) v.begin(), v.end()
#define INF (1e18 + 5)
#define inf (1e9 + 5)
#define mod 1000000007
bool check_prime(long long n) {
int flag = 0;
for (long long i = 2; i * i <= n; i++) {
if (n % i == 0) {
flag = 1;
break;
}
}
if (n == 1)
return false;
else if (flag == 0 || n == 2 || n == 3) {
return true;
} else {
return false;
}
}
int BE(int x, int n, int m) // function to calculate x raise to power n modulo m
{
int result = 1;
while (n > 0) {
if (n % 2 == 1)
result = result * x % m;
x = x * x % m;
n = n / 2;
}
return result;
}
void SieveOfEratosthenes(int n, bool prime[], bool primesquare[], int a[]) {
// Create a boolean array "prime[0..n]" and
// initialize all entries it as true. A value
// in prime[i] will finally be false if i is
// Not a prime, else true.
for (int i = 2; i <= n; i++)
prime[i] = true;
// Create a boolean array "primesquare[0..n*n+1]"
// and initialize all entries it as false. A value
// in squareprime[i] will finally be true if i is
// square of prime, else false.
for (int i = 0; i <= (n * n + 1); i++)
primesquare[i] = false;
// 1 is not a prime number
prime[1] = false;
for (int p = 2; p * p <= n; p++) {
// If prime[p] is not changed, then
// it is a prime
if (prime[p] == true) {
// Update all multiples of p
for (int i = p * 2; i <= n; i += p)
prime[i] = false;
}
}
int j = 0;
for (int p = 2; p <= n; p++) {
if (prime[p]) {
// Storing primes in an array
a[j] = p;
// Update value in primesquare[p*p],
// if p is prime.
primesquare[p * p] = true;
j++;
}
}
}
// Function to count divisors
int countDivisors(int n) {
// If number is 1, then it will have only 1
// as a factor. So, total factors will be 1.
if (n == 1)
return 1;
bool prime[n + 1], primesquare[n * n + 1];
int a[n]; // for storing primes upto n
// Calling SieveOfEratosthenes to store prime
// factors of n and to store square of prime
// factors of n
SieveOfEratosthenes(n, prime, primesquare, a);
// ans will contain total number of distinct
// divisors
int ans = 1;
// Loop for counting factors of n
for (int i = 0;; i++) {
// a[i] is not less than cube root n
if (a[i] * a[i] * a[i] > n)
break;
// Calculating power of a[i] in n.
int cnt = 1; // cnt is power of prime a[i] in n.
while (n % a[i] == 0) // if a[i] is a factor of n
{
n = n / a[i];
cnt = cnt + 1; // incrementing power
}
// Calculating number of divisors
// If n = a^p * b^q then total divisors of n
// are (p+1)*(q+1)
ans = ans * cnt;
}
// if a[i] is greater than cube root of n
// First case
if (prime[n])
ans = ans * 2;
// Second case
else if (primesquare[n])
ans = ans * 3;
// Third casse
else if (n != 1)
ans = ans * 4;
return ans; // Total divisors
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);
#ifndef ONLINE_JUDGE
if (fopen("input.txt", "r")) {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
#endif
//_______________________________-code
//starts-_______________________________________________
int n;
cin >> n;
int ans = 0;
int j = 1;
rep(i, 1, n) {
ans += (i * ((n / j + 1) * (n / j)) / 2);
j++;
}
cout << ans << en;
return 0;
}
| replace | 180 | 181 | 180 | 185 | TLE | |
p02624 | C++ | Runtime Error | #include <algorithm>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<ll, P> Q;
typedef complex<double> C;
#define cx real()
#define cy imag()
#define FOR(i, a, b) for (int i = (a); i < (b); (i)++)
const ll INF = 1LL << 60;
const double DINF = 1e30;
const ll mod = 1000000007;
const ll dx[4] = {1, 0, -1, 0};
const ll dy[4] = {0, -1, 0, 1};
const C I = C(0, 1);
const double EPS = 1e-10;
const ll NCK_MAX = 4100000;
ll pw(ll i, ll j) {
if (j == 0)
return 1;
if (j % 2 == 1)
return i * pw(i, j - 1) % mod;
ll tmp = pw(i, j / 2);
return tmp * tmp % mod;
}
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
ll extgcd(ll a, ll b, ll &x, ll &y) {
if (b == 0) {
x = 1, y = 0;
return a;
}
ll q = a / b, g = extgcd(b, a - q * b, x, y);
ll z = x - q * y;
x = y;
y = z;
return g;
}
ll invmod(ll a, ll m) { // a^-1 mod m
ll x, y;
extgcd(a, m, x, y);
x %= m;
if (x < 0)
x += m;
return x;
}
ll *fac, *finv, *inv;
void nCk_init(ll mod) {
fac = new ll[NCK_MAX];
finv = new ll[NCK_MAX];
inv = new ll[NCK_MAX];
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (ll i = 2; i < NCK_MAX; i++) {
fac[i] = fac[i - 1] * i % mod;
inv[i] = mod - inv[mod % i] * (mod / i) % mod;
finv[i] = finv[i - 1] * inv[i] % mod;
}
}
ll nCk(ll n, ll k, ll mod) {
if (fac == NULL)
nCk_init(mod);
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
template <typename T> class Zip {
vector<T> d;
bool flag;
void init() {
sort(d.begin(), d.end());
d.erase(unique(d.begin(), d.end()), d.end());
flag = false;
}
public:
Zip() { flag = false; }
void add(T x) {
d.push_back(x);
flag = true;
}
ll getNum(T x) {
if (flag)
init();
return lower_bound(d.begin(), d.end(), x) - d.begin();
}
ll size() {
if (flag)
init();
return (ll)d.size();
}
};
class UnionFind {
vector<ll> par, rank; // par > 0: number, par < 0: -par
public:
UnionFind(ll n) : par(n, 1), rank(n, 0) {}
ll getSize(ll x) { return par[find(x)]; }
ll find(ll x) {
if (par[x] > 0)
return x;
return -(par[x] = -find(-par[x]));
}
void merge(ll x, ll y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (rank[x] < rank[y]) {
par[y] += par[x];
par[x] = -y;
} else {
par[x] += par[y];
par[y] = -x;
if (rank[x] == rank[y])
rank[x]++;
}
}
bool isSame(ll x, ll y) { return find(x) == find(y); }
};
template <typename T> class SegmentTree {
ll n;
vector<T> node;
function<T(T, T)> fun, fun2;
bool customChange;
T outValue, initValue;
public:
void init(ll num, function<T(T, T)> resultFunction, T init, T out,
function<T(T, T)> changeFunction = NULL) {
// changeFunction: (input, beforevalue) => newvalue
fun = resultFunction;
fun2 = changeFunction;
customChange = changeFunction != NULL;
n = 1;
while (n < num)
n *= 2;
node.resize(2 * n - 1);
fill(node.begin(), node.end(), init);
outValue = out;
initValue = init;
}
void valueChange(ll num, T value) {
num += n - 1;
if (customChange)
node[num] = fun2(value, node[num]);
else
node[num] = value;
while (num > 0)
num = (num - 1) / 2,
node[num] = fun(node[num * 2 + 1], node[num * 2 + 2]);
}
T rangeQuery(ll a, ll b, ll l = 0, ll r = -1, ll k = 0) { // [a, b)
if (r == -1)
r = n;
if (a <= l && r <= b)
return node[k];
if (b <= l || r <= a)
return outValue;
ll mid = (l + r) / 2;
return fun(rangeQuery(a, b, l, mid, 2 * k + 1),
rangeQuery(a, b, mid, r, 2 * k + 2));
}
};
template <typename T> class Graph {
struct edge {
ll to;
T cost;
};
struct edge_data {
ll from, to;
T cost;
};
ll v;
vector<vector<edge>> e, re;
vector<edge_data> ed;
vector<bool> used;
vector<ll> vs, cmp;
bool isDirected, isMinasEdge;
public:
Graph(ll _v, bool _isDirected = true, ll range_add = 0) {
// range_add 0:no / 1:in / 2:out / 3:in+out
//_v++;
v = _v, isDirected = _isDirected;
isMinasEdge = false;
e.resize(v), re.resize(v);
}
void add_edge(ll s, ll t, T cost = 1) {
e[s].push_back((edge){t, cost});
if (!isDirected)
e[t].push_back((edge){s, cost});
else
re[t].push_back((edge){s, cost});
ed.push_back((edge_data){s, t, cost});
if (cost < 0)
isMinasEdge = true;
}
vector<T> dijkstra(ll s) {
vector<T> d(v, INF);
d[s] = 0;
auto edge_cmp = [](const edge &a, const edge &b) {
return a.cost > b.cost;
};
priority_queue<edge, vector<edge>, decltype(edge_cmp)> pq(edge_cmp);
pq.push((edge){s, 0});
while (!pq.empty()) {
edge temp = pq.top();
pq.pop();
if (d[temp.to] < temp.cost)
continue;
for (const edge &next : e[temp.to]) {
T cost = temp.cost + next.cost;
if (d[next.to] > cost) {
d[next.to] = cost;
pq.push((edge){next.to, cost});
}
}
}
return d;
}
vector<T> bellmanford(ll s) {
vector<T> d(v, INF);
d[s] = 0;
for (ll i = 0; i < v; i++) {
for (const edge_data &temp : ed) {
if (d[temp.from] != INF && d[temp.to] > d[temp.from] + temp.cost)
d[temp.to] = d[temp.from] + temp.cost;
if (!isDirected && d[temp.to] != INF &&
d[temp.from] > d[temp.to] + temp.cost)
d[temp.from] = d[temp.to] + temp.cost;
}
}
for (ll i = 0; i < v; i++) {
for (const edge_data &temp : ed) {
if (d[temp.from] != INF && d[temp.to] > d[temp.from] + temp.cost)
d[temp.to] = -INF;
if (!isDirected && d[temp.to] != INF &&
d[temp.from] > d[temp.to] + temp.cost)
d[temp.from] = -INF;
}
}
return d;
}
vector<T> shortest_path(ll s) {
if (isMinasEdge)
return bellmanford(s);
else
return dijkstra(s);
}
T kruskal() {
// if (isDirected)
UnionFind uf(v);
auto edge_data_cmp = [](const edge_data &a, const edge_data &b) {
return a.cost < b.cost;
};
sort(ed.begin(), ed.end(), edge_data_cmp);
T ans = 0;
for (const edge_data &temp : ed) {
if (uf.isSame(temp.from, temp.to))
continue;
uf.merge(temp.from, temp.to);
ans += temp.cost;
}
return ans;
}
void scc_dfs(ll s) {
used[s] = true;
for (const edge &i : e[s])
if (!used[i.to])
scc_dfs(i.to);
vs.push_back(s);
}
void scc_rdfs(ll s, ll k) {
used[s] = true;
cmp[s] = k;
for (const edge &i : re[s])
if (!used[i.to])
scc_rdfs(i.to, k);
}
vector<ll> scc() {
used.resize(v);
fill(used.begin(), used.end(), false);
cmp.resize(v);
vs.clear();
for (ll i = 0; i < v; i++)
if (!used[i])
scc_dfs(i);
used.resize(v);
fill(used.begin(), used.end(), false);
ll k = 0;
for (ll i = vs.size() - 1; i >= 0; i--)
if (!used[vs[i]])
scc_rdfs(vs[i], k++);
return cmp;
}
};
vector<P> prime_factorize(ll N) {
vector<P> res;
for (ll a = 2; a * a <= N; ++a) {
if (N % a != 0)
continue;
ll ex = 0;
while (N % a == 0) {
++ex;
N /= a;
}
res.push_back({a, ex});
}
if (N != 1)
res.push_back({N, 1});
return res;
}
ll N;
ll g(ll n, ll x) {
ll y = n / x;
return y * (y + 1) * x / 2;
}
int main() {
cin >> N;
ll ans = 0;
FOR(i, 0, N) ans += g(N, i);
cout << ans << endl;
return 0;
}
| #include <algorithm>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<ll, P> Q;
typedef complex<double> C;
#define cx real()
#define cy imag()
#define FOR(i, a, b) for (int i = (a); i < (b); (i)++)
const ll INF = 1LL << 60;
const double DINF = 1e30;
const ll mod = 1000000007;
const ll dx[4] = {1, 0, -1, 0};
const ll dy[4] = {0, -1, 0, 1};
const C I = C(0, 1);
const double EPS = 1e-10;
const ll NCK_MAX = 4100000;
ll pw(ll i, ll j) {
if (j == 0)
return 1;
if (j % 2 == 1)
return i * pw(i, j - 1) % mod;
ll tmp = pw(i, j / 2);
return tmp * tmp % mod;
}
ll gcd(ll a, ll b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
ll extgcd(ll a, ll b, ll &x, ll &y) {
if (b == 0) {
x = 1, y = 0;
return a;
}
ll q = a / b, g = extgcd(b, a - q * b, x, y);
ll z = x - q * y;
x = y;
y = z;
return g;
}
ll invmod(ll a, ll m) { // a^-1 mod m
ll x, y;
extgcd(a, m, x, y);
x %= m;
if (x < 0)
x += m;
return x;
}
ll *fac, *finv, *inv;
void nCk_init(ll mod) {
fac = new ll[NCK_MAX];
finv = new ll[NCK_MAX];
inv = new ll[NCK_MAX];
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (ll i = 2; i < NCK_MAX; i++) {
fac[i] = fac[i - 1] * i % mod;
inv[i] = mod - inv[mod % i] * (mod / i) % mod;
finv[i] = finv[i - 1] * inv[i] % mod;
}
}
ll nCk(ll n, ll k, ll mod) {
if (fac == NULL)
nCk_init(mod);
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
template <typename T> class Zip {
vector<T> d;
bool flag;
void init() {
sort(d.begin(), d.end());
d.erase(unique(d.begin(), d.end()), d.end());
flag = false;
}
public:
Zip() { flag = false; }
void add(T x) {
d.push_back(x);
flag = true;
}
ll getNum(T x) {
if (flag)
init();
return lower_bound(d.begin(), d.end(), x) - d.begin();
}
ll size() {
if (flag)
init();
return (ll)d.size();
}
};
class UnionFind {
vector<ll> par, rank; // par > 0: number, par < 0: -par
public:
UnionFind(ll n) : par(n, 1), rank(n, 0) {}
ll getSize(ll x) { return par[find(x)]; }
ll find(ll x) {
if (par[x] > 0)
return x;
return -(par[x] = -find(-par[x]));
}
void merge(ll x, ll y) {
x = find(x);
y = find(y);
if (x == y)
return;
if (rank[x] < rank[y]) {
par[y] += par[x];
par[x] = -y;
} else {
par[x] += par[y];
par[y] = -x;
if (rank[x] == rank[y])
rank[x]++;
}
}
bool isSame(ll x, ll y) { return find(x) == find(y); }
};
template <typename T> class SegmentTree {
ll n;
vector<T> node;
function<T(T, T)> fun, fun2;
bool customChange;
T outValue, initValue;
public:
void init(ll num, function<T(T, T)> resultFunction, T init, T out,
function<T(T, T)> changeFunction = NULL) {
// changeFunction: (input, beforevalue) => newvalue
fun = resultFunction;
fun2 = changeFunction;
customChange = changeFunction != NULL;
n = 1;
while (n < num)
n *= 2;
node.resize(2 * n - 1);
fill(node.begin(), node.end(), init);
outValue = out;
initValue = init;
}
void valueChange(ll num, T value) {
num += n - 1;
if (customChange)
node[num] = fun2(value, node[num]);
else
node[num] = value;
while (num > 0)
num = (num - 1) / 2,
node[num] = fun(node[num * 2 + 1], node[num * 2 + 2]);
}
T rangeQuery(ll a, ll b, ll l = 0, ll r = -1, ll k = 0) { // [a, b)
if (r == -1)
r = n;
if (a <= l && r <= b)
return node[k];
if (b <= l || r <= a)
return outValue;
ll mid = (l + r) / 2;
return fun(rangeQuery(a, b, l, mid, 2 * k + 1),
rangeQuery(a, b, mid, r, 2 * k + 2));
}
};
template <typename T> class Graph {
struct edge {
ll to;
T cost;
};
struct edge_data {
ll from, to;
T cost;
};
ll v;
vector<vector<edge>> e, re;
vector<edge_data> ed;
vector<bool> used;
vector<ll> vs, cmp;
bool isDirected, isMinasEdge;
public:
Graph(ll _v, bool _isDirected = true, ll range_add = 0) {
// range_add 0:no / 1:in / 2:out / 3:in+out
//_v++;
v = _v, isDirected = _isDirected;
isMinasEdge = false;
e.resize(v), re.resize(v);
}
void add_edge(ll s, ll t, T cost = 1) {
e[s].push_back((edge){t, cost});
if (!isDirected)
e[t].push_back((edge){s, cost});
else
re[t].push_back((edge){s, cost});
ed.push_back((edge_data){s, t, cost});
if (cost < 0)
isMinasEdge = true;
}
vector<T> dijkstra(ll s) {
vector<T> d(v, INF);
d[s] = 0;
auto edge_cmp = [](const edge &a, const edge &b) {
return a.cost > b.cost;
};
priority_queue<edge, vector<edge>, decltype(edge_cmp)> pq(edge_cmp);
pq.push((edge){s, 0});
while (!pq.empty()) {
edge temp = pq.top();
pq.pop();
if (d[temp.to] < temp.cost)
continue;
for (const edge &next : e[temp.to]) {
T cost = temp.cost + next.cost;
if (d[next.to] > cost) {
d[next.to] = cost;
pq.push((edge){next.to, cost});
}
}
}
return d;
}
vector<T> bellmanford(ll s) {
vector<T> d(v, INF);
d[s] = 0;
for (ll i = 0; i < v; i++) {
for (const edge_data &temp : ed) {
if (d[temp.from] != INF && d[temp.to] > d[temp.from] + temp.cost)
d[temp.to] = d[temp.from] + temp.cost;
if (!isDirected && d[temp.to] != INF &&
d[temp.from] > d[temp.to] + temp.cost)
d[temp.from] = d[temp.to] + temp.cost;
}
}
for (ll i = 0; i < v; i++) {
for (const edge_data &temp : ed) {
if (d[temp.from] != INF && d[temp.to] > d[temp.from] + temp.cost)
d[temp.to] = -INF;
if (!isDirected && d[temp.to] != INF &&
d[temp.from] > d[temp.to] + temp.cost)
d[temp.from] = -INF;
}
}
return d;
}
vector<T> shortest_path(ll s) {
if (isMinasEdge)
return bellmanford(s);
else
return dijkstra(s);
}
T kruskal() {
// if (isDirected)
UnionFind uf(v);
auto edge_data_cmp = [](const edge_data &a, const edge_data &b) {
return a.cost < b.cost;
};
sort(ed.begin(), ed.end(), edge_data_cmp);
T ans = 0;
for (const edge_data &temp : ed) {
if (uf.isSame(temp.from, temp.to))
continue;
uf.merge(temp.from, temp.to);
ans += temp.cost;
}
return ans;
}
void scc_dfs(ll s) {
used[s] = true;
for (const edge &i : e[s])
if (!used[i.to])
scc_dfs(i.to);
vs.push_back(s);
}
void scc_rdfs(ll s, ll k) {
used[s] = true;
cmp[s] = k;
for (const edge &i : re[s])
if (!used[i.to])
scc_rdfs(i.to, k);
}
vector<ll> scc() {
used.resize(v);
fill(used.begin(), used.end(), false);
cmp.resize(v);
vs.clear();
for (ll i = 0; i < v; i++)
if (!used[i])
scc_dfs(i);
used.resize(v);
fill(used.begin(), used.end(), false);
ll k = 0;
for (ll i = vs.size() - 1; i >= 0; i--)
if (!used[vs[i]])
scc_rdfs(vs[i], k++);
return cmp;
}
};
vector<P> prime_factorize(ll N) {
vector<P> res;
for (ll a = 2; a * a <= N; ++a) {
if (N % a != 0)
continue;
ll ex = 0;
while (N % a == 0) {
++ex;
N /= a;
}
res.push_back({a, ex});
}
if (N != 1)
res.push_back({N, 1});
return res;
}
ll N;
ll g(ll n, ll x) {
ll y = n / x;
return y * (y + 1) * x / 2;
}
int main() {
cin >> N;
ll ans = 0;
FOR(i, 0, N) ans += g(N, i + 1);
cout << ans << endl;
return 0;
}
| replace | 358 | 359 | 358 | 359 | -8 | |
p02624 | C++ | Time Limit Exceeded | // prepared by dumb
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define dl double
#define llu unsigned long long int
#define scll(x) scanf("%lld", &x)
#define scdl(x) scanf("%lf", &x)
#define scull(x) scanf("%llu", &x)
#define scch(x) scanf(" %c", &x)
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define ppb() pop_back()
#define ppf() pop_front()
#define Pi acos(-1.0)
#define INF 1000000000000000000
#define mod 1000000007
#define ff first
#define ss second
#define mpr make_pair
#define debug(x) cout << x << '\n';
// sin((x*pi)/180.0)[x in degree]
// sin(x)[x in radian]
// asin(x)[radian]
//(asin(x)*180)/pi[degree]
// or(|)//and(&)//xor(^)//not(~)
#define fast \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll a, b, c, d, e, f = 0, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x,
y, z;
ll cnt = 0, ans = 0, l1, l2, l3, c1 = 0, c2 = 0, c3 = 0, mx = 0, mn = 0;
ll prevsum = 0, ans1 = 0, ans2 = 0, odd = 0, even = 0, hh, mm;
string str, str1, str2;
scll(n);
unordered_map<ll, ll> coun;
for (i = 1; i <= n; i++) {
for (j = i; j <= n; j += i)
coun[j]++;
}
for (i = 1; i <= n; i++)
ans += i * coun[i];
cout << ans;
return 0;
} | // prepared by dumb
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define dl double
#define llu unsigned long long int
#define scll(x) scanf("%lld", &x)
#define scdl(x) scanf("%lf", &x)
#define scull(x) scanf("%llu", &x)
#define scch(x) scanf(" %c", &x)
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define ppb() pop_back()
#define ppf() pop_front()
#define Pi acos(-1.0)
#define INF 1000000000000000000
#define mod 1000000007
#define ff first
#define ss second
#define mpr make_pair
#define debug(x) cout << x << '\n';
// sin((x*pi)/180.0)[x in degree]
// sin(x)[x in radian]
// asin(x)[radian]
//(asin(x)*180)/pi[degree]
// or(|)//and(&)//xor(^)//not(~)
#define fast \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll a, b, c, d, e, f = 0, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x,
y, z;
ll cnt = 0, ans = 0, l1, l2, l3, c1 = 0, c2 = 0, c3 = 0, mx = 0, mn = 0;
ll prevsum = 0, ans1 = 0, ans2 = 0, odd = 0, even = 0, hh, mm;
string str, str1, str2;
scll(n);
vector<ll> coun(n + 1, 0);
for (i = 1; i <= n; i++) {
for (j = i; j <= n; j += i)
coun[j]++;
}
for (i = 1; i <= n; i++)
ans += i * coun[i];
cout << ans;
return 0;
} | replace | 42 | 43 | 42 | 43 | TLE | |
p02624 | C++ | Runtime Error | #include <iostream>
using namespace std;
int main(void) {
long long N;
cin >> N;
long long ans = 0;
for (int X = 0; X < N; X++) {
long long Y = N / X;
long long G = Y * (Y + 1) * X / 2;
ans += G;
}
cout << ans << endl;
return 0;
}
| #include <iostream>
using namespace std;
int main(void) {
long long N;
cin >> N;
long long ans = 0;
for (int X = 1; X <= N; X++) {
long long Y = N / X;
long long G = Y * (Y + 1) * X / 2;
ans += G;
}
cout << ans << endl;
return 0;
}
| replace | 8 | 9 | 8 | 9 | -8 | |
p02625 | C++ | Runtime Error |
/* You are being watched. */
//~ while (clock() <= 69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")
#include <bits/stdc++.h>
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC
//target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") ~ #pragma
//GCC optimize("unroll-loops")
using namespace std;
//~ #include <ext/pb_ds/assoc_container.hpp>
//~ #include <ext/pb_ds/tree_policy.hpp>
//~ using namespace __gnu_pbds;
//~ template <typename T>
//~ using ordered_set =
//~ tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define all(x) (x).begin(), (x).end()
#define show(x) cerr << __LINE__ << " says: " << #x << " = " << (x) << "\n"
#define FastIO \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
// using li = __int128;
// using uli = unsigned __int128;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
double getTime() { return clock() / (double)CLOCKS_PER_SEC; }
void read(){};
template <typename T, typename... Args> void read(T &a, Args &...args) {
cin >> a;
read(args...);
}
void print(){};
template <typename T, typename... Args> void print(T a, Args... args) {
cout << a << " \n"[sizeof...(args) == 0];
print(args...);
}
const int N = 2e5 + 5;
const ll MOD = 1e9 + 7;
ll f[N], inv[N];
ll get(ll x, ll y) {
if (y == -1)
y = MOD - 2;
ll res = 1LL;
while (y) {
if (y & 1)
res = (res * x) % MOD;
y >>= 1LL;
x = (x * x) % MOD;
}
return res;
}
ll ncr(ll n, ll m) {
if (n < 0 or m < 0 or n < m)
return 0ll;
return f[n] * inv[m] % MOD * inv[n - m] % MOD;
}
void solve() {
ll n, m;
cin >> n >> m;
ll tobe = 0ll;
ll mul = 1ll;
for (ll i = 0; i <= n; i++) {
ll cur = mul * ncr(n, i) * ncr(m - i, n - i) % MOD * f[n - i] % MOD;
tobe = (tobe + cur) % MOD;
if (tobe < 0)
tobe += MOD;
mul = -mul;
}
cout << (tobe * ncr(m, n) % MOD * f[n]) % MOD << '\n';
}
int main() {
f[0] = inv[0] = 1LL;
for (int i = 1; i < N; i++) {
f[i] = (i * f[i - 1]) % MOD;
inv[i] = get(f[i], -1);
}
int t;
t = 1;
while (t--)
solve();
}
|
/* You are being watched. */
//~ while (clock() <= 69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")
#include <bits/stdc++.h>
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC
//target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") ~ #pragma
//GCC optimize("unroll-loops")
using namespace std;
//~ #include <ext/pb_ds/assoc_container.hpp>
//~ #include <ext/pb_ds/tree_policy.hpp>
//~ using namespace __gnu_pbds;
//~ template <typename T>
//~ using ordered_set =
//~ tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define all(x) (x).begin(), (x).end()
#define show(x) cerr << __LINE__ << " says: " << #x << " = " << (x) << "\n"
#define FastIO \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
// using li = __int128;
// using uli = unsigned __int128;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
double getTime() { return clock() / (double)CLOCKS_PER_SEC; }
void read(){};
template <typename T, typename... Args> void read(T &a, Args &...args) {
cin >> a;
read(args...);
}
void print(){};
template <typename T, typename... Args> void print(T a, Args... args) {
cout << a << " \n"[sizeof...(args) == 0];
print(args...);
}
const int N = 5e5 + 5;
const ll MOD = 1e9 + 7;
ll f[N], inv[N];
ll get(ll x, ll y) {
if (y == -1)
y = MOD - 2;
ll res = 1LL;
while (y) {
if (y & 1)
res = (res * x) % MOD;
y >>= 1LL;
x = (x * x) % MOD;
}
return res;
}
ll ncr(ll n, ll m) {
if (n < 0 or m < 0 or n < m)
return 0ll;
return f[n] * inv[m] % MOD * inv[n - m] % MOD;
}
void solve() {
ll n, m;
cin >> n >> m;
ll tobe = 0ll;
ll mul = 1ll;
for (ll i = 0; i <= n; i++) {
ll cur = mul * ncr(n, i) * ncr(m - i, n - i) % MOD * f[n - i] % MOD;
tobe = (tobe + cur) % MOD;
if (tobe < 0)
tobe += MOD;
mul = -mul;
}
cout << (tobe * ncr(m, n) % MOD * f[n]) % MOD << '\n';
}
int main() {
f[0] = inv[0] = 1LL;
for (int i = 1; i < N; i++) {
f[i] = (i * f[i - 1]) % MOD;
inv[i] = get(f[i], -1);
}
int t;
t = 1;
while (t--)
solve();
}
| replace | 56 | 57 | 56 | 57 | 0 | |
p02625 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> v;
typedef vector<vector<ll>> vv;
#define MOD 1000000007
#define INF 1001001001
#define MIN -1001001001
#define rep(i, k, N) for (int i = k; i < N; i++)
#define MP make_pair
#define MT make_tuple // tie,make_tuple は別物
#define PB push_back
#define PF push_front
#define all(x) (x).begin(), (x).end()
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
ll inv(ll x) {
// MOD=10e9+7
ll n = MOD - 2, a = 1;
while (n > 0) {
if (n % 2)
a = a * x % MOD;
x = x * x % MOD;
n = n / 2;
}
return a;
}
vector<ll> facts(1, 1);
ll factorial(ll n) {
if (facts.size() - 1 >= n)
return facts[n];
else {
rep(i, facts.size(), n + 1) { facts.push_back((facts[i - 1] * i) % MOD); }
return facts[n];
}
}
vector<ll> inv_facts(1, 1);
ll inv_factorial(ll n) {
if (inv_facts.size() - 1 >= n)
return inv_facts[n];
else {
rep(i, inv_facts.size(), n + 1) {
inv_facts.push_back((inv_facts[i - 1] * inv(i)) % MOD);
}
return inv_facts[n];
}
}
ll Comb(ll n, ll m) {
return (((factorial(n) * inv_factorial(m)) % MOD) * inv_factorial(n - m)) %
MOD;
}
ll PowMod(ll N, ll P, ll M) {
ll ans = 1;
while (P >= 1) {
if (P % 2)
ans = ans * N % M;
N = N * N % M;
P /= 2;
}
ans %= M;
return ans;
}
int main() {
ll N, M;
cin >> M >> N;
ll ans = 0;
rep(k, 0, N + 1) {
if (k % 2 == 0)
ans += (((factorial(M - k) * inv_factorial(M - N)) % MOD) * Comb(N, k)) %
MOD;
else {
ans -=
((factorial(M - k) * inv_factorial(M - N)) % MOD * Comb(N, k)) % MOD;
ans += MOD;
ans %= MOD;
}
}
ans *= factorial(M);
ans %= MOD;
ans *= inv_factorial(M - N);
ans %= MOD;
cout << ans;
return 0;
} | #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> v;
typedef vector<vector<ll>> vv;
#define MOD 1000000007
#define INF 1001001001
#define MIN -1001001001
#define rep(i, k, N) for (int i = k; i < N; i++)
#define MP make_pair
#define MT make_tuple // tie,make_tuple は別物
#define PB push_back
#define PF push_front
#define all(x) (x).begin(), (x).end()
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
ll inv(ll x) {
// MOD=10e9+7
ll n = MOD - 2, a = 1;
while (n > 0) {
if (n % 2)
a = a * x % MOD;
x = x * x % MOD;
n = n / 2;
}
return a;
}
vector<ll> facts(1, 1);
ll factorial(ll n) {
if (facts.size() - 1 >= n)
return facts[n];
else {
rep(i, facts.size(), n + 1) { facts.push_back((facts[i - 1] * i) % MOD); }
return facts[n];
}
}
vector<ll> inv_facts(1, 1);
ll inv_factorial(ll n) {
if (inv_facts.size() - 1 >= n)
return inv_facts[n];
else {
rep(i, inv_facts.size(), n + 1) {
inv_facts.push_back((inv_facts[i - 1] * inv(i)) % MOD);
}
return inv_facts[n];
}
}
ll Comb(ll n, ll m) {
return (((factorial(n) * inv_factorial(m)) % MOD) * inv_factorial(n - m)) %
MOD;
}
ll PowMod(ll N, ll P, ll M) {
ll ans = 1;
while (P >= 1) {
if (P % 2)
ans = ans * N % M;
N = N * N % M;
P /= 2;
}
ans %= M;
return ans;
}
int main() {
ll N, M;
cin >> N >> M;
ll ans = 0;
rep(k, 0, N + 1) {
if (k % 2 == 0)
ans += (((factorial(M - k) * inv_factorial(M - N)) % MOD) * Comb(N, k)) %
MOD;
else {
ans -=
((factorial(M - k) * inv_factorial(M - N)) % MOD * Comb(N, k)) % MOD;
ans += MOD;
ans %= MOD;
}
}
ans *= factorial(M);
ans %= MOD;
ans *= inv_factorial(M - N);
ans %= MOD;
cout << ans;
return 0;
} | replace | 83 | 84 | 83 | 84 | 0 | |
p02625 | C++ | Runtime Error | #include <bits/stdc++.h>
#define rep(i, n) for (int i = (0); i < (n); i++)
using namespace std;
typedef long long ll;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
ll MOD = 1e9 + 7;
const ll max_n = 505050;
ll fact[max_n];
bool fact_inited = false;
ll powmod(ll a, ll b) {
if (b == 0)
return 1;
if (b % 2 == 1)
return a * powmod(a, b - 1) % MOD;
ll d = powmod(a, b / 2) % MOD;
return d * d % MOD;
}
ll init_fact() {
fact[0] = 1;
fact[1] = 1;
for (ll i = 2; i < max_n; i++) {
fact[i] = fact[i - 1] * i % MOD;
}
fact_inited = true;
}
ll comb(ll n, ll k) {
if (!fact_inited)
init_fact();
ll ret = fact[n];
ret *= powmod(fact[k], MOD - 2);
ret %= MOD;
ret *= powmod(fact[n - k], MOD - 2);
ret %= MOD;
return ret;
// X^(-1) = X^(p-2) (mod p) (Fermat's little theorem)
}
ll perm(ll n, ll k) {
if (!fact_inited)
init_fact();
ll ret = fact[n];
ret *= powmod(fact[n - k], MOD - 2);
ret %= MOD;
return ret;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll n, m;
cin >> n >> m;
ll ans = perm(m, n) * perm(m, n);
ans %= MOD;
ll s = -1;
for (int x = 1; x <= n; x++) {
ll t = perm(m, n) * comb(n, x) % MOD * perm(m - x, n - x) % MOD;
t %= MOD;
t *= s;
ans += MOD + t;
ans %= MOD;
s *= -1;
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = (0); i < (n); i++)
using namespace std;
typedef long long ll;
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
ll MOD = 1e9 + 7;
const ll max_n = 505050;
ll fact[max_n];
bool fact_inited = false;
ll powmod(ll a, ll b) {
if (b == 0)
return 1;
if (b % 2 == 1)
return a * powmod(a, b - 1) % MOD;
ll d = powmod(a, b / 2) % MOD;
return d * d % MOD;
}
void init_fact() {
fact[0] = 1;
fact[1] = 1;
for (ll i = 2; i < max_n; i++) {
fact[i] = fact[i - 1] * i % MOD;
}
fact_inited = true;
}
ll comb(ll n, ll k) {
if (!fact_inited)
init_fact();
ll ret = fact[n];
ret *= powmod(fact[k], MOD - 2);
ret %= MOD;
ret *= powmod(fact[n - k], MOD - 2);
ret %= MOD;
return ret;
// X^(-1) = X^(p-2) (mod p) (Fermat's little theorem)
}
ll perm(ll n, ll k) {
if (!fact_inited)
init_fact();
ll ret = fact[n];
ret *= powmod(fact[n - k], MOD - 2);
ret %= MOD;
return ret;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll n, m;
cin >> n >> m;
ll ans = perm(m, n) * perm(m, n);
ans %= MOD;
ll s = -1;
for (int x = 1; x <= n; x++) {
ll t = perm(m, n) * comb(n, x) % MOD * perm(m - x, n - x) % MOD;
t %= MOD;
t *= s;
ans += MOD + t;
ans %= MOD;
s *= -1;
}
cout << ans << endl;
}
| replace | 38 | 39 | 38 | 39 | 0 | |
p02625 | C++ | Runtime Error | #include <bits/stdc++.h>
#define ll long long
#define ar array
#define AC \
ios_base::sync_with_stdio(0); \
cin.tie(NULL);
using namespace std;
const int MOD = 1e9 + 7;
const int N = 5 * 1e5;
struct mi {
ll v;
explicit operator ll() const { return v; }
mi() { v = 0; }
mi(ll _v) {
v = (-MOD < _v && _v < MOD) ? _v : _v % MOD;
if (v < 0)
v += MOD;
}
friend bool operator==(const mi &a, const mi &b) { return a.v == b.v; }
friend bool operator!=(const mi &a, const mi &b) { return !(a == b); }
friend bool operator<(const mi &a, const mi &b) { return a.v < b.v; }
mi &operator+=(const mi &m) {
if ((v += m.v) >= MOD)
v -= MOD;
return *this;
}
mi &operator-=(const mi &m) {
if ((v -= m.v) < 0)
v += MOD;
return *this;
}
mi &operator*=(const mi &m) {
v = v * m.v % MOD;
return *this;
}
mi &operator/=(const mi &m) { return (*this) *= inv(m); }
friend mi pow(mi a, ll p) {
mi ans = 1;
assert(p >= 0);
for (; p; p /= 2, a *= a)
if (p & 1)
ans *= a;
return ans;
}
friend mi inv(const mi &a) {
assert(a.v != 0);
return pow(a, MOD - 2);
}
mi operator-() const { return mi(-v); }
mi &operator++() { return *this += 1; }
mi &operator--() { return *this -= 1; }
mi operator++(int) {
mi temp;
temp.v = v++;
return temp;
}
mi operator--(int) {
mi temp;
temp.v = v--;
return temp;
}
friend mi operator+(mi a, const mi &b) { return a += b; }
friend mi operator-(mi a, const mi &b) { return a -= b; }
friend mi operator*(mi a, const mi &b) { return a *= b; }
friend mi operator/(mi a, const mi &b) { return a /= b; }
friend ostream &operator<<(ostream &os, const mi &m) {
os << m.v;
return os;
}
friend istream &operator>>(istream &is, mi &m) {
ll x;
is >> x;
m.v = x;
return is;
}
};
int n, m;
mi fac[N];
mi facInv[N];
void initFac() {
fac[0] = 1;
facInv[0] = 1;
for (int i = 1; i <= m; i++) {
fac[i] = fac[i - 1] * i;
facInv[i] = inv(fac[i]);
}
}
mi choose(int n, int k) {
if (k < 0)
return 0;
if (n < 0)
return 0;
if (k > n)
return 0;
mi res = fac[n] * facInv[n - k] * facInv[k];
return res;
}
int main() {
AC cin >> n >> m;
initFac();
mi res = 0;
mi cur = 1;
for (int k = 0; k <= n; k++) {
res += cur * choose(n, k) * choose(m - k, n - k) * fac[n - k];
cur = -cur;
}
cout << res * choose(m, n) * fac[n] << endl;
} | #include <bits/stdc++.h>
#define ll long long
#define ar array
#define AC \
ios_base::sync_with_stdio(0); \
cin.tie(NULL);
using namespace std;
const int MOD = 1e9 + 7;
const int N = 5 * 1e5;
struct mi {
ll v;
explicit operator ll() const { return v; }
mi() { v = 0; }
mi(ll _v) {
v = (-MOD < _v && _v < MOD) ? _v : _v % MOD;
if (v < 0)
v += MOD;
}
friend bool operator==(const mi &a, const mi &b) { return a.v == b.v; }
friend bool operator!=(const mi &a, const mi &b) { return !(a == b); }
friend bool operator<(const mi &a, const mi &b) { return a.v < b.v; }
mi &operator+=(const mi &m) {
if ((v += m.v) >= MOD)
v -= MOD;
return *this;
}
mi &operator-=(const mi &m) {
if ((v -= m.v) < 0)
v += MOD;
return *this;
}
mi &operator*=(const mi &m) {
v = v * m.v % MOD;
return *this;
}
mi &operator/=(const mi &m) { return (*this) *= inv(m); }
friend mi pow(mi a, ll p) {
mi ans = 1;
assert(p >= 0);
for (; p; p /= 2, a *= a)
if (p & 1)
ans *= a;
return ans;
}
friend mi inv(const mi &a) {
assert(a.v != 0);
return pow(a, MOD - 2);
}
mi operator-() const { return mi(-v); }
mi &operator++() { return *this += 1; }
mi &operator--() { return *this -= 1; }
mi operator++(int) {
mi temp;
temp.v = v++;
return temp;
}
mi operator--(int) {
mi temp;
temp.v = v--;
return temp;
}
friend mi operator+(mi a, const mi &b) { return a += b; }
friend mi operator-(mi a, const mi &b) { return a -= b; }
friend mi operator*(mi a, const mi &b) { return a *= b; }
friend mi operator/(mi a, const mi &b) { return a /= b; }
friend ostream &operator<<(ostream &os, const mi &m) {
os << m.v;
return os;
}
friend istream &operator>>(istream &is, mi &m) {
ll x;
is >> x;
m.v = x;
return is;
}
};
int n, m;
mi fac[N + 5];
mi facInv[N + 5];
void initFac() {
fac[0] = 1;
facInv[0] = 1;
for (int i = 1; i <= m; i++) {
fac[i] = fac[i - 1] * i;
facInv[i] = inv(fac[i]);
}
}
mi choose(int n, int k) {
if (k < 0)
return 0;
if (n < 0)
return 0;
if (k > n)
return 0;
mi res = fac[n] * facInv[n - k] * facInv[k];
return res;
}
int main() {
AC cin >> n >> m;
initFac();
mi res = 0;
mi cur = 1;
for (int k = 0; k <= n; k++) {
res += cur * choose(n, k) * choose(m - k, n - k) * fac[n - k];
cur = -cur;
}
cout << res * choose(m, n) * fac[n] << endl;
} | replace | 81 | 83 | 81 | 83 | 0 | |
p02625 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
/* run this program using the console pauser or add your own getch,
* system("pause") or input loop */
#define pb push_back
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define ll long long
#define sc(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
const ll mod = 1e9 + 7;
ll ad(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
ll jc[720005];
ll ap(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
ll ksm(ll a, ll b) {
ll res = 1;
while (b) {
if (b % 2 == 1)
res = ap(res, a);
a = ap(a, a);
b /= 2;
}
return res;
}
ll C(ll a, ll b) {
return ap(jc[a], ap(ksm(jc[b], mod - 2), ksm(jc[a - b], mod - 2)));
}
ll f[720005];
ll g[720005];
int main(int argc, char **argv) {
ll n, m;
sl(n);
sl(m);
jc[0] = 1;
rep(i, 1, m + m) { jc[i] = ap(jc[i - 1], i); }
ll ans = 0;
rep(i, 0, n) // 最少有i个相等
{
g[i] = ap(ap(ap(C(n, i), C(m, i)), ap(C(m - i, (n - i)), C(m - i, n - i))),
ap(jc[i], ap(jc[n - i], jc[n - i])));
// cout<<g[i]<<" "<<ap(C(m,2*(n-i)),(jc[2*(n-i)]))<<endl;
ans = ad(ans, ((i) % 2 == 0) ? g[i] : mod - g[i]);
}
cout << ans;
return 0;
} | #include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
/* run this program using the console pauser or add your own getch,
* system("pause") or input loop */
#define pb push_back
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define ll long long
#define sc(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
const ll mod = 1e9 + 7;
ll ad(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
ll jc[720005];
ll ap(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
ll ksm(ll a, ll b) {
ll res = 1;
while (b) {
if (b % 2 == 1)
res = ap(res, a);
a = ap(a, a);
b /= 2;
}
return res;
}
ll C(ll a, ll b) {
return ap(jc[a], ap(ksm(jc[b], mod - 2), ksm(jc[a - b], mod - 2)));
}
ll f[720005];
ll g[720005];
int main(int argc, char **argv) {
ll n, m;
sl(n);
sl(m);
jc[0] = 1;
rep(i, 1, m) { jc[i] = ap(jc[i - 1], i); }
ll ans = 0;
rep(i, 0, n) // 最少有i个相等
{
g[i] = ap(ap(ap(C(n, i), C(m, i)), ap(C(m - i, (n - i)), C(m - i, n - i))),
ap(jc[i], ap(jc[n - i], jc[n - i])));
// cout<<g[i]<<" "<<ap(C(m,2*(n-i)),(jc[2*(n-i)]))<<endl;
ans = ad(ans, ((i) % 2 == 0) ? g[i] : mod - g[i]);
}
cout << ans;
return 0;
} | replace | 38 | 39 | 38 | 39 | 0 | |
p02625 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define mod 1000000007
#define big 1e18 + 10
#define small -big
#define pb push_back
#define endl "\n"
template <typename T>
void prarr(T a) {
cerr << "[ ";
for (auto i : a) {
cerr << i << " ";
}
cerr << "]" << endl;
}
#define trace(...) _er(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void _er(const char *name, Arg1 &&arg1) {
cerr << "[" << name << " : " << arg1 << "]" << endl;
}
template <typename Arg1, typename... Args>
void _er(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr << "[";
cerr.write(names, comma - names) << " : " << arg1 << "] ";
_er(comma + 2, args...);
}
ll pow(ll a, ll n, ll m) {
ll res = 1;
while (n) {
if (n % 2)
res = res * a % m;
a = a * a % m;
n /= 2;
}
return res;
}
ll Fact[1000005];
ll invFact[1000005];
void cal() {
Fact[0] = 1;
Fact[1] = 1;
for (ll i = 2; i <= 1000000; i++) {
Fact[i] = i * Fact[i - 1] % mod;
}
invFact[0] = 1;
for (ll i = 1; i <= 1000000; i++) {
invFact[i] = pow(Fact[i], mod - 2, mod);
}
}
ll comb(ll n, ll r) {
ll res = 1;
res = Fact[n];
res = res * invFact[n - r] % mod;
res = res * invFact[r] % mod;
return res;
}
void addse(ll &a, ll b) {
a %= mod;
b %= mod;
a += b;
a %= mod;
}
void subse(ll &a, ll b) {
a %= mod;
b %= mod;
a -= b;
if (a < 0) {
a += mod;
}
a %= mod;
}
void mulse(ll &a, ll b) {
a %= mod;
b %= mod;
a *= b;
a %= mod;
}
void solve() {
ll N, M;
cin >> N >> M;
ll tot = 0;
for (ll i = 0; i <= N; i++) {
ll now = comb(N, i);
mulse(now, comb(M, i));
mulse(now, Fact[i]);
mulse(now, comb(M - i, N - i));
mulse(now, Fact[N - i]);
mulse(now, comb(M - i, N - i));
mulse(now, Fact[N - i]);
trace(i, now);
if (i % 2) {
subse(tot, now);
} else {
addse(tot, now);
}
}
cout << tot << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cal();
int t = 1;
// cin >> t;
for (int i = 1; i <= t; i++) {
solve();
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define mod 1000000007
#define big 1e18 + 10
#define small -big
#define pb push_back
#define endl "\n"
template <typename T>
void prarr(T a) {
cerr << "[ ";
for (auto i : a) {
cerr << i << " ";
}
cerr << "]" << endl;
}
#define trace(...) _er(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void _er(const char *name, Arg1 &&arg1) {
cerr << "[" << name << " : " << arg1 << "]" << endl;
}
template <typename Arg1, typename... Args>
void _er(const char *names, Arg1 &&arg1, Args &&...args) {
const char *comma = strchr(names + 1, ',');
cerr << "[";
cerr.write(names, comma - names) << " : " << arg1 << "] ";
_er(comma + 2, args...);
}
ll pow(ll a, ll n, ll m) {
ll res = 1;
while (n) {
if (n % 2)
res = res * a % m;
a = a * a % m;
n /= 2;
}
return res;
}
ll Fact[1000005];
ll invFact[1000005];
void cal() {
Fact[0] = 1;
Fact[1] = 1;
for (ll i = 2; i <= 1000000; i++) {
Fact[i] = i * Fact[i - 1] % mod;
}
invFact[0] = 1;
for (ll i = 1; i <= 1000000; i++) {
invFact[i] = pow(Fact[i], mod - 2, mod);
}
}
ll comb(ll n, ll r) {
ll res = 1;
res = Fact[n];
res = res * invFact[n - r] % mod;
res = res * invFact[r] % mod;
return res;
}
void addse(ll &a, ll b) {
a %= mod;
b %= mod;
a += b;
a %= mod;
}
void subse(ll &a, ll b) {
a %= mod;
b %= mod;
a -= b;
if (a < 0) {
a += mod;
}
a %= mod;
}
void mulse(ll &a, ll b) {
a %= mod;
b %= mod;
a *= b;
a %= mod;
}
void solve() {
ll N, M;
cin >> N >> M;
ll tot = 0;
for (ll i = 0; i <= N; i++) {
ll now = comb(N, i);
mulse(now, comb(M, i));
mulse(now, Fact[i]);
mulse(now, comb(M - i, N - i));
mulse(now, Fact[N - i]);
mulse(now, comb(M - i, N - i));
mulse(now, Fact[N - i]);
if (i % 2) {
subse(tot, now);
} else {
addse(tot, now);
}
}
cout << tot << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cal();
int t = 1;
// cin >> t;
for (int i = 1; i <= t; i++) {
solve();
}
return 0;
}
| replace | 128 | 129 | 128 | 129 | TLE | |
p02625 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;
#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x, to) for (x = 0; x < (to); x++)
#define FORR(x, arr) for (auto &x : arr)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
//-------------------------------------------------------
ll dp[505050];
int N, M;
const ll mo = 1000000007;
const int NUM_ = 400001;
static ll fact[NUM_ + 1], factr[NUM_ + 1], inv[NUM_ + 1];
ll comb(ll N_, ll C_) {
if (C_ < 0 || C_ > N_)
return 0;
return factr[C_] * fact[N_] % mo * factr[N_ - C_] % mo;
}
void solve() {
int i, j, k, l, r, x, y;
string s;
inv[1] = fact[0] = factr[0] = 1;
for (int i = 2; i <= NUM_; ++i)
inv[i] = inv[mo % i] * (mo - mo / i) % mo;
for (int i = 1; i <= NUM_; ++i)
fact[i] = fact[i - 1] * i % mo, factr[i] = factr[i - 1] * inv[i] % mo;
cin >> N >> M;
ll ret = 0;
FOR(i, N + 1) {
dp[i] = comb(M, i) * comb(N, i) % mo * fact[i] % mo * fact[M - i] % mo *
fact[M - i] % mo * factr[M - N] % mo * factr[M - N] % mo;
if (i % 2) {
ret -= dp[i];
} else {
ret += dp[i];
}
}
cout << (ret % mo + mo) % mo << endl;
}
int main(int argc, char **argv) {
string s;
int i;
if (argc == 1)
ios::sync_with_stdio(false), cin.tie(0);
FOR(i, argc - 1) s += argv[i + 1], s += '\n';
FOR(i, s.size()) ungetc(s[s.size() - 1 - i], stdin);
cout.tie(0);
solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;
#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x, to) for (x = 0; x < (to); x++)
#define FORR(x, arr) for (auto &x : arr)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
//-------------------------------------------------------
ll dp[505050];
int N, M;
const ll mo = 1000000007;
const int NUM_ = 600001;
static ll fact[NUM_ + 1], factr[NUM_ + 1], inv[NUM_ + 1];
ll comb(ll N_, ll C_) {
if (C_ < 0 || C_ > N_)
return 0;
return factr[C_] * fact[N_] % mo * factr[N_ - C_] % mo;
}
void solve() {
int i, j, k, l, r, x, y;
string s;
inv[1] = fact[0] = factr[0] = 1;
for (int i = 2; i <= NUM_; ++i)
inv[i] = inv[mo % i] * (mo - mo / i) % mo;
for (int i = 1; i <= NUM_; ++i)
fact[i] = fact[i - 1] * i % mo, factr[i] = factr[i - 1] * inv[i] % mo;
cin >> N >> M;
ll ret = 0;
FOR(i, N + 1) {
dp[i] = comb(M, i) * comb(N, i) % mo * fact[i] % mo * fact[M - i] % mo *
fact[M - i] % mo * factr[M - N] % mo * factr[M - N] % mo;
if (i % 2) {
ret -= dp[i];
} else {
ret += dp[i];
}
}
cout << (ret % mo + mo) % mo << endl;
}
int main(int argc, char **argv) {
string s;
int i;
if (argc == 1)
ios::sync_with_stdio(false), cin.tie(0);
FOR(i, argc - 1) s += argv[i + 1], s += '\n';
FOR(i, s.size()) ungetc(s[s.size() - 1 - i], stdin);
cout.tie(0);
solve();
return 0;
}
| replace | 17 | 18 | 17 | 18 | 0 | |
p02625 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e5 + 5, MOD = 1e9 + 7;
struct mi {
ll v;
explicit operator ll() const { return v; }
mi() { v = 0; }
mi(ll _v) {
v = (-MOD < _v && _v < MOD) ? _v : _v % MOD;
if (v < 0)
v += MOD;
}
friend bool operator==(const mi &a, const mi &b) { return a.v == b.v; }
friend bool operator!=(const mi &a, const mi &b) { return !(a == b); }
friend bool operator<(const mi &a, const mi &b) { return a.v < b.v; }
mi &operator+=(const mi &m) {
if ((v += m.v) >= MOD)
v -= MOD;
return *this;
}
mi &operator-=(const mi &m) {
if ((v -= m.v) < 0)
v += MOD;
return *this;
}
mi &operator*=(const mi &m) {
v = v * m.v % MOD;
return *this;
}
mi &operator/=(const mi &m) { return (*this) *= inv(m); }
friend mi pow(mi a, ll p) {
mi ans = 1;
assert(p >= 0);
for (; p; p /= 2, a *= a)
if (p & 1)
ans *= a;
return ans;
}
friend mi inv(const mi &a) {
assert(a.v != 0);
return pow(a, MOD - 2);
}
mi operator-() const { return mi(-v); }
mi &operator++() { return *this += 1; }
mi &operator--() { return *this -= 1; }
mi operator++(int) {
mi temp;
temp.v = v++;
return temp;
}
mi operator--(int) {
mi temp;
temp.v = v--;
return temp;
}
friend mi operator+(mi a, const mi &b) { return a += b; }
friend mi operator-(mi a, const mi &b) { return a -= b; }
friend mi operator*(mi a, const mi &b) { return a *= b; }
friend mi operator/(mi a, const mi &b) { return a /= b; }
friend ostream &operator<<(ostream &os, const mi &m) {
os << m.v;
return os;
}
friend istream &operator>>(istream &is, mi &m) {
ll x;
is >> x;
m.v = x;
return is;
}
};
mi facs[N], facInvs[N];
mi nCr(mi a, mi b) {
if ((ll)b > (ll)a)
return 0;
if ((ll)a < 0)
return 0;
if ((ll)b < 0)
return 0;
mi cur = facs[(ll)a];
cur = cur * facInvs[(ll)b];
cur = cur * facInvs[(ll)a - (ll)b];
return cur;
}
void initFacs() {
facs[0] = 1;
facInvs[0] = 1;
for (int i = 1; i < N; i++) {
facs[i] = (facs[i - 1] * i);
facInvs[i] = inv(facs[i]);
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
initFacs();
int n, m;
cin >> n >> m;
mi ans = 0, cur = 1;
for (int i = 0; i <= n; i++) {
ans += cur * facs[n - i] * nCr(m - i, n - i) * nCr(n, i);
cur = -cur;
}
cout << ans * facs[n] * nCr(m, n) << "\n";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 5e5 + 5, MOD = 1e9 + 7;
struct mi {
ll v;
explicit operator ll() const { return v; }
mi() { v = 0; }
mi(ll _v) {
v = (-MOD < _v && _v < MOD) ? _v : _v % MOD;
if (v < 0)
v += MOD;
}
friend bool operator==(const mi &a, const mi &b) { return a.v == b.v; }
friend bool operator!=(const mi &a, const mi &b) { return !(a == b); }
friend bool operator<(const mi &a, const mi &b) { return a.v < b.v; }
mi &operator+=(const mi &m) {
if ((v += m.v) >= MOD)
v -= MOD;
return *this;
}
mi &operator-=(const mi &m) {
if ((v -= m.v) < 0)
v += MOD;
return *this;
}
mi &operator*=(const mi &m) {
v = v * m.v % MOD;
return *this;
}
mi &operator/=(const mi &m) { return (*this) *= inv(m); }
friend mi pow(mi a, ll p) {
mi ans = 1;
assert(p >= 0);
for (; p; p /= 2, a *= a)
if (p & 1)
ans *= a;
return ans;
}
friend mi inv(const mi &a) {
assert(a.v != 0);
return pow(a, MOD - 2);
}
mi operator-() const { return mi(-v); }
mi &operator++() { return *this += 1; }
mi &operator--() { return *this -= 1; }
mi operator++(int) {
mi temp;
temp.v = v++;
return temp;
}
mi operator--(int) {
mi temp;
temp.v = v--;
return temp;
}
friend mi operator+(mi a, const mi &b) { return a += b; }
friend mi operator-(mi a, const mi &b) { return a -= b; }
friend mi operator*(mi a, const mi &b) { return a *= b; }
friend mi operator/(mi a, const mi &b) { return a /= b; }
friend ostream &operator<<(ostream &os, const mi &m) {
os << m.v;
return os;
}
friend istream &operator>>(istream &is, mi &m) {
ll x;
is >> x;
m.v = x;
return is;
}
};
mi facs[N], facInvs[N];
mi nCr(mi a, mi b) {
if ((ll)b > (ll)a)
return 0;
if ((ll)a < 0)
return 0;
if ((ll)b < 0)
return 0;
mi cur = facs[(ll)a];
cur = cur * facInvs[(ll)b];
cur = cur * facInvs[(ll)a - (ll)b];
return cur;
}
void initFacs() {
facs[0] = 1;
facInvs[0] = 1;
for (int i = 1; i < N; i++) {
facs[i] = (facs[i - 1] * i);
facInvs[i] = inv(facs[i]);
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
initFacs();
int n, m;
cin >> n >> m;
mi ans = 0, cur = 1;
for (int i = 0; i <= n; i++) {
ans += cur * facs[n - i] * nCr(m - i, n - i) * nCr(n, i);
cur = -cur;
}
cout << ans * facs[n] * nCr(m, n) << "\n";
return 0;
} | replace | 5 | 6 | 5 | 6 | 0 | |
p02626 | C++ | Runtime Error | //{{{
#include <algorithm>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <sys/time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using ll = long long;
enum : int { M = (int)1e9 + 7 };
enum : ll { MLL = (ll)1e18L + 9 };
using namespace std;
#ifdef LOCAL
#include "rprint2.hpp"
#else
#define FUNC(name) \
template <ostream &out = cout, class... T> void name(T &&...) {}
FUNC(printde)
FUNC(printdbe) FUNC(printdwe) FUNC(printdu) FUNC(printe) FUNC(printe0);
#endif
template <template <class T, class = std::allocator<T>> class V, class E>
istream &operator>>(istream &in, V<E> &v) {
for (auto &e : v) {
in >> e;
}
return in;
}
//}}}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<ll> as(n);
cin >> as;
ll a = as[0], b = as[1], c = 0;
for (int i = 2; i < n; i++) {
c ^= as[i];
}
printde(as);
printde(a, b, c);
ll ans = 0;
for (int i = 60; i >= 0; i--) {
bool ab = a >> i & 1, bb = b >> i & 1, cb = c >> i & 1;
ll mask = (1ll << (i + 1)) - 1;
if ((ab ^ bb) == cb) {
continue;
}
if (cb) {
if (!ab & !bb) {
ll num = (1ll << i) - (b & mask);
if (a < num) {
cout << -1 << '\n';
return 0;
}
a -= num;
b += num;
ans += num;
} else {
ll num = (a & mask) - (1ll << i) + 1;
a -= num;
b += num;
ans += num;
if ((b >> i & 1) == 0) {
cout << -1 << '\n';
return 0;
}
}
} else {
if (ab) {
ll num = min((1ll << i) - (b & mask), (a & mask) - (1ll << i) + 1);
if (a < num) {
cout << -1 << '\n';
return 0;
}
a -= num;
b += num;
ans += num;
} else if (bb) {
cout << -1 << '\n';
return 1;
}
}
}
if (a == 0 || (a ^ b) != c) {
cout << -1 << '\n';
return 0;
}
cout << ans << '\n';
}
| //{{{
#include <algorithm>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <sys/time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using ll = long long;
enum : int { M = (int)1e9 + 7 };
enum : ll { MLL = (ll)1e18L + 9 };
using namespace std;
#ifdef LOCAL
#include "rprint2.hpp"
#else
#define FUNC(name) \
template <ostream &out = cout, class... T> void name(T &&...) {}
FUNC(printde)
FUNC(printdbe) FUNC(printdwe) FUNC(printdu) FUNC(printe) FUNC(printe0);
#endif
template <template <class T, class = std::allocator<T>> class V, class E>
istream &operator>>(istream &in, V<E> &v) {
for (auto &e : v) {
in >> e;
}
return in;
}
//}}}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<ll> as(n);
cin >> as;
ll a = as[0], b = as[1], c = 0;
for (int i = 2; i < n; i++) {
c ^= as[i];
}
printde(as);
printde(a, b, c);
ll ans = 0;
for (int i = 60; i >= 0; i--) {
bool ab = a >> i & 1, bb = b >> i & 1, cb = c >> i & 1;
ll mask = (1ll << (i + 1)) - 1;
if ((ab ^ bb) == cb) {
continue;
}
if (cb) {
if (!ab & !bb) {
ll num = (1ll << i) - (b & mask);
if (a < num) {
cout << -1 << '\n';
return 0;
}
a -= num;
b += num;
ans += num;
} else {
ll num = (a & mask) - (1ll << i) + 1;
a -= num;
b += num;
ans += num;
if ((b >> i & 1) == 0) {
cout << -1 << '\n';
return 0;
}
}
} else {
if (ab) {
ll num = min((1ll << i) - (b & mask), (a & mask) - (1ll << i) + 1);
if (a < num) {
cout << -1 << '\n';
return 0;
}
a -= num;
b += num;
ans += num;
} else if (bb) {
ll num = (1ll << i) - (b & (1ll << i) - 1);
if (a < num) {
cout << -1 << '\n';
return 0;
}
a -= num;
b += num;
ans += num;
i++;
// cout << -1 << '\n';
// return 0;
}
}
}
if (a == 0 || (a ^ b) != c) {
cout << -1 << '\n';
return 0;
}
cout << ans << '\n';
}
| replace | 89 | 91 | 89 | 100 | 0 | |
p02626 | C++ | Runtime Error | // Utkarsh.25dec
#include <bits/stdc++.h>
#include <chrono>
#include <random>
#define ll long long int
#define ull unsigned long long int
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define rep(i, n) for (ll i = 0; i < n; i++)
#define loop(i, a, b) for (ll i = a; i <= b; i++)
#define vi vector<int>
#define vs vector<string>
#define vc vector<char>
#define vl vector<ll>
#define all(c) (c).begin(), (c).end()
#define max3(a, b, c) max(max(a, b), c)
#define min3(a, b, c) min(min(a, b), c)
#define deb(x) cerr << #x << ' ' << '=' << ' ' << x << '\n'
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set \
tree<int, null_type, less<int>, rb_tree_tag, \
tree_order_statistics_node_update>
// ordered_set s ; s.order_of_key(val) no. of elements strictly less than val
// s.find_by_order(i) itertor to ith element (0 indexed)
typedef vector<vector<ll>> matrix;
ll power(ll a, ll b) {
ll res = 1;
a %= mod;
assert(b >= 0);
for (; b; b >>= 1) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
}
return res;
}
ll modInverse(ll a) { return power(a, mod - 2); }
const int N = 500023;
bool vis[N];
vector<int> adj[N];
void solve() {
ll n;
cin >> n;
vl v;
rep(i, n) {
ll c;
cin >> c;
v.pb(c);
}
ll req = 0;
for (int i = 2; i < n; i++)
req ^= v[i];
if ((req ^ v[0] ^ v[1]) == 0) {
cout << 0 << '\n';
return;
}
ll sum = v[0] + v[1];
ll my = 0;
vl store;
ll possible = 0;
vl fun;
for (int i = 0; i < 50; i++) {
if (((1LL << i) & (req)) != 0) {
my += (1LL << i);
fun.pb(i);
} else {
store.pb(i);
possible += (1LL << i);
}
}
ll diff = sum - my;
if (diff < 0) {
cout << -1;
return;
}
if ((diff % 2) != 0) {
cout << -1;
return;
}
ll x = diff / 2;
if ((x & possible) != x) {
cout << -1 << '\n';
return;
}
sort(all(fun));
reverse(all(fun));
for (auto it : fun) {
if ((x | (1LL << it)) <= v[0]) {
x |= (1LL << it);
}
}
if (x == 0) {
cout << -1 << '\n';
return;
}
assert(x <= v[0]);
assert(x >= 1);
if ((x ^ (sum - x)) != req) {
cout << -1;
return;
}
cout << (v[0] - x);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int T = 1;
// cin>>T;
int t = 0;
while (t++ < T) {
// cout<<"Case #"<<t<<":"<<' ';
solve();
// cout<<'\n';
}
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
<< "ms\n";
} | // Utkarsh.25dec
#include <bits/stdc++.h>
#include <chrono>
#include <random>
#define ll long long int
#define ull unsigned long long int
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define rep(i, n) for (ll i = 0; i < n; i++)
#define loop(i, a, b) for (ll i = a; i <= b; i++)
#define vi vector<int>
#define vs vector<string>
#define vc vector<char>
#define vl vector<ll>
#define all(c) (c).begin(), (c).end()
#define max3(a, b, c) max(max(a, b), c)
#define min3(a, b, c) min(min(a, b), c)
#define deb(x) cerr << #x << ' ' << '=' << ' ' << x << '\n'
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set \
tree<int, null_type, less<int>, rb_tree_tag, \
tree_order_statistics_node_update>
// ordered_set s ; s.order_of_key(val) no. of elements strictly less than val
// s.find_by_order(i) itertor to ith element (0 indexed)
typedef vector<vector<ll>> matrix;
ll power(ll a, ll b) {
ll res = 1;
a %= mod;
assert(b >= 0);
for (; b; b >>= 1) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
}
return res;
}
ll modInverse(ll a) { return power(a, mod - 2); }
const int N = 500023;
bool vis[N];
vector<int> adj[N];
void solve() {
ll n;
cin >> n;
vl v;
rep(i, n) {
ll c;
cin >> c;
v.pb(c);
}
ll req = 0;
for (int i = 2; i < n; i++)
req ^= v[i];
if ((req ^ v[0] ^ v[1]) == 0) {
cout << 0 << '\n';
return;
}
ll sum = v[0] + v[1];
ll my = 0;
vl store;
ll possible = 0;
vl fun;
for (int i = 0; i < 50; i++) {
if (((1LL << i) & (req)) != 0) {
my += (1LL << i);
fun.pb(i);
} else {
store.pb(i);
possible += (1LL << i);
}
}
ll diff = sum - my;
if (diff < 0) {
cout << -1;
return;
}
if ((diff % 2) != 0) {
cout << -1;
return;
}
ll x = diff / 2;
if ((x & possible) != x) {
cout << -1 << '\n';
return;
}
sort(all(fun));
reverse(all(fun));
for (auto it : fun) {
if ((x | (1LL << it)) <= v[0]) {
x |= (1LL << it);
}
}
if (x == 0) {
cout << -1 << '\n';
return;
}
if (x > v[0]) {
cout << -1 << '\n';
return;
}
assert(x >= 1);
if ((x ^ (sum - x)) != req) {
cout << -1;
return;
}
cout << (v[0] - x);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int T = 1;
// cin>>T;
int t = 0;
while (t++ < T) {
// cout<<"Case #"<<t<<":"<<' ';
solve();
// cout<<'\n';
}
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
<< "ms\n";
} | replace | 99 | 100 | 99 | 103 | 0 | Time : 46.366ms
|
p02626 | 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);
#ifndef ONLINE_JUDGE
#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 = 1e9 + 7;
const int N = 2e5 + 5;
typedef pair<int, int> pii;
ll n;
vector<ll> a;
ll sum = 0, andd = 0, x = 0;
bool check() {
ll bal = 0;
for (ll &i : a)
bal ^= a[i];
if (bal == 0)
return true;
return false;
}
void TEST_CASES(int cas) {
cin >> n;
a.resize(n);
for (ll i = 0; i < n; i++) {
cin >> a[i];
}
if (check()) {
printf("0\n");
return;
}
for (ll i = 2; i < n; i++)
x ^= a[i];
sum = a[0] + a[1];
if ((sum < x) || (sum - x) % 2) {
printf("-1\n");
return;
}
andd = (sum - x) / 2;
ll ans1 = 0, ans2 = 0;
for (ll i = 40; i >= 0; i--) {
ll b1 = 0, b2 = 0;
if (x & (1ll << i))
b1 = 1;
if (andd & (1ll << i))
b2 = 1;
if (b1 && b2) {
printf("-1\n");
return;
}
if (b1 == 0 && b2 == 1) {
ans1 |= (1ll << i);
ans2 |= (1ll << i);
}
}
for (ll i = 40; i >= 0; i--) {
ll b1 = 0, b2 = 0;
if (x & (1ll << i))
b1 = 1;
if (andd & (1ll << i))
b2 = 1;
if (b1 == 1 && b2 == 0) {
if ((ans1 | (1ll << i)) >= a[0]) {
ans2 |= (1ll << i);
} else {
ans1 |= (1ll << i);
}
}
}
if (ans1 == 0) {
printf("-1\n");
return;
}
if (ans1 < a[0] && ans2 > a[1]) {
printf("%lld\n", a[0] - ans1);
} else {
printf("-1\n");
}
}
/*
*/
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;
}
// os.order_of_key(v): returns how many elements strictly less than v
// os.find_by_order()
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);
#ifndef ONLINE_JUDGE
#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 = 1e9 + 7;
const int N = 2e5 + 5;
typedef pair<int, int> pii;
ll n;
vector<ll> a;
ll sum = 0, andd = 0, x = 0;
bool check() {
ll bal = 0;
for (ll &i : a)
bal ^= i;
if (bal == 0)
return true;
return false;
}
void TEST_CASES(int cas) {
cin >> n;
a.resize(n);
for (ll i = 0; i < n; i++) {
cin >> a[i];
}
if (check()) {
printf("0\n");
return;
}
for (ll i = 2; i < n; i++)
x ^= a[i];
sum = a[0] + a[1];
if ((sum < x) || (sum - x) % 2) {
printf("-1\n");
return;
}
andd = (sum - x) / 2;
ll ans1 = 0, ans2 = 0;
for (ll i = 40; i >= 0; i--) {
ll b1 = 0, b2 = 0;
if (x & (1ll << i))
b1 = 1;
if (andd & (1ll << i))
b2 = 1;
if (b1 && b2) {
printf("-1\n");
return;
}
if (b1 == 0 && b2 == 1) {
ans1 |= (1ll << i);
ans2 |= (1ll << i);
}
}
for (ll i = 40; i >= 0; i--) {
ll b1 = 0, b2 = 0;
if (x & (1ll << i))
b1 = 1;
if (andd & (1ll << i))
b2 = 1;
if (b1 == 1 && b2 == 0) {
if ((ans1 | (1ll << i)) >= a[0]) {
ans2 |= (1ll << i);
} else {
ans1 |= (1ll << i);
}
}
}
if (ans1 == 0) {
printf("-1\n");
return;
}
if (ans1 < a[0] && ans2 > a[1]) {
printf("%lld\n", a[0] - ans1);
} else {
printf("-1\n");
}
}
/*
*/
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;
}
// os.order_of_key(v): returns how many elements strictly less than v
// os.find_by_order()
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 | 30 | 31 | 30 | 31 | 0 | |
p02626 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int main() {
int N;
cin >> N;
ull x, A1, A2;
cin >> A1;
cin >> A2;
x = 0;
for (int i = 3; i <= N; i++) {
ull a;
cin >> a;
x ^= a;
}
set<pair<ull, ull>> S; // (z+b,b)
S.insert(pair<ull, ull>(1, 1));
while (S.size() > 0) {
pair<ull, ull> p = *S.begin();
S.erase(S.begin());
ull z = p.first - p.second;
ull b = p.second;
// cout << "check (" << z << "," << b << ")" << endl;
if (((A1 - z) ^ (A2 + z)) == x) {
cout << z << endl;
return (0);
}
if ((((A1 - z) & (b * 2 - 1)) ^ ((A2 + z) & (b * 2 - 1))) ==
(x & (b * 2 - 1))) {
if (p.first + p.second < A1) {
S.insert(pair<ull, ull>(z + b * 2, b * 2));
// cout << "insert (" << z + b * 2 << "," << b *
//2 << ")" << endl;
S.insert(pair<ull, ull>(z + b + b, b));
// cout << "insert (" << z + b + b * 2 << "," <<
//b * 2 << ")" << endl;
}
}
}
cout << -1 << endl;
return (0);
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int main() {
int N;
cin >> N;
ull x, A1, A2;
cin >> A1;
cin >> A2;
x = 0;
for (int i = 3; i <= N; i++) {
ull a;
cin >> a;
x ^= a;
}
set<pair<ull, ull>> S; // (z+b,b)
S.insert(pair<ull, ull>(1, 1));
while (S.size() > 0) {
pair<ull, ull> p = *S.begin();
S.erase(S.begin());
ull z = p.first - p.second;
ull b = p.second;
// cout << "check (" << z << "," << b << ")" << endl;
if (((A1 - z) ^ (A2 + z)) == x) {
cout << z << endl;
return (0);
}
if ((((A1 - z) & (b * 2 - 1)) ^ ((A2 + z) & (b * 2 - 1))) ==
(x & (b * 2 - 1))) {
if (p.first + p.second < A1) {
S.insert(pair<ull, ull>(z + b * 2, b * 2));
// cout << "insert (" << z + b * 2 << "," << b *
//2 << ")" << endl;
if (((A1 - z) & b) == 0 || ((A2 + z) & b) == b) {
S.insert(pair<ull, ull>(z + b + b * 2, b * 2));
// cout << "insert (" << z + b + b * 2 << "," << b * 2
//<< ")" << endl;
}
}
}
}
cout << -1 << endl;
return (0);
}
| replace | 35 | 38 | 35 | 40 | TLE | |
p02626 | C++ | Time Limit Exceeded | /*temp*/
//
//
//
//
// #undef _DEBUG
// #pragma GCC optimize("Ofast")
// 不動小数点の計算高速化
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #include <boost/multiprecision/cpp_int.hpp>
#ifdef _DEBUG
#include "template.h"
#else
#if __cplusplus >= 201703L
/*Atcoderでしか使えない(c++17 && このテンプレートが使えるならAtcoder)*/
#include <boost/sort/pdqsort/pdqsort.hpp>
#define fast_sort boost::sort::pdqsort
#endif
#endif
#ifndef _DEBUG
#ifndef UNTITLED15_TEMPLATE_H
#define UNTITLED15_TEMPLATE_H
#ifdef _DEBUG
#include "bits_stdc++.h"
#else
#include <bits/stdc++.h>
#endif
#ifndef fast_sort
#define fast_sort sort
#endif
// #define use_pq
#define use_for
#define use_for_each
#define use_sort
#define use_fill
#define use_rand
#define use_mgr
#define use_rui
#define use_compress
//
//
//
//
//
//
#define use_pbds
#ifdef use_pbds
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace std::chrono;
template <class T, class U>
void operator+=(__gnu_pbds::priority_queue<T, greater<T>,
__gnu_pbds::rc_binomial_heap_tag> &a,
const U &v) {
a.push(v);
}
template <class T, class U>
void operator+=(
__gnu_pbds::priority_queue<T, less<T>, __gnu_pbds::rc_binomial_heap_tag> &a,
const U &v) {
a.push(v);
}
template <class T, class U, class W, class X>
auto count(__gnu_pbds::gp_hash_table<T, U, W> &a, X k) {
return a.find(k) != a.end();
}
#endif
/*@formatter:off*/
#define ll long long
using sig_dou = double;
// マクロ省略形 関数等
#define arsz(a) (sizeof(a) / sizeof(a[0]))
#define sz(a) ((ll)(a).size())
#define mp make_pair
#define mt make_tuple
#define pb pop_back
#define pf push_front
#define eb emplace_back
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
template <class T, class U> auto max(T a, U b) { return a > b ? a : b; }
template <class T, class U> auto min(T a, U b) { return a < b ? a : b; }
// optional<T>について下でオーバーロード(nullopt_tを左辺右辺について単位元として扱う)
template <class T, class U> bool chma(T &a, const U &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T, class U> bool chmi(T &a, const U &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
// メタ系 meta
template <typename T, typename U = typename T::value_type>
std::true_type value_type_tester(signed);
template <typename T> std::false_type value_type_tester(long);
template <typename T>
struct has_value_type : decltype(value_type_tester<T>(0)) {};
// A<T>でTを返す
template <class T> using decl_t = typename T::value_type;
// A<B<.....T>>でTを返す
template <class T, bool end = !has_value_type<T>::value> struct decl2_ {
typedef T type;
};
template <class T> struct decl2_<T, false> {
typedef typename decl2_<decl_t<T>>::type type;
};
template <class T> using decl2_t = typename decl2_<T>::type;
// #define decl_max(a, b) decltype(max(MAX<decltype(a)>(), MAX<decltype(b)>()))
#define is_same2(T, U) is_same<T, U>::value
template <class T> struct is_vector : std::false_type {};
template <class T> struct is_vector<std::vector<T>> : std::true_type {};
// 大きい型を返す max_type<int, char>::type
// todo mintがlong long より小さいと判定されるためバグる
template <class T1, class T2, bool t1_bigger = (sizeof(T1) > sizeof(T2))>
struct max_type {
typedef T1 type;
};
template <class T1, class T2> struct max_type<T1, T2, false> {
typedef T2 type;
};
template <class T> struct vec_rank : integral_constant<int, 0> {};
template <class T>
struct vec_rank<vector<T>> : integral_constant<int, vec_rank<T>{} + 1> {};
// N個のTを並べたtupleを返す
// tuple_n<3, int>::type tuple<int, int, int>
template <size_t N, class T, class... Arg> struct tuple_n {
typedef typename tuple_n<N - 1, T, T, Arg...>::type type;
};
template <class T, class... Arg> struct tuple_n<0, T, Arg...> {
typedef tuple<Arg...> type;
};
struct dummy_t1 {};
struct dummy_t2 {};
struct dummy_t3 {};
struct dummy_t4 {};
struct dummy_t5 {};
struct dummy_t6 {};
// template<class T, require(is_integral<T>::value)>など
#define require_t(bo) enable_if_t<bo> * = nullptr
// 複数でオーバーロードする場合、引数が同じだとうまくいかないため
// require_arg(bool, dummy_t1)
// require_arg(bool, dummy_t2)等とする
#define require_arg1(bo) enable_if_t<bo> * = nullptr
#define require_arg2(bo, dummy_type) enable_if_t<bo, dummy_type> * = nullptr
#define require_arg(...) \
over2(__VA_ARGS__, require_arg2, require_arg1)(__VA_ARGS__)
//->//enable_if_tのtを書き忘れそうだから
#define require_ret(bo, ret_type) enable_if_t<bo, ret_type>
#define int long long // todo 消したら動かない intの代わりにsignedを使う
auto start_time = system_clock::now();
auto past_time = system_clock::now();
#define debugName(VariableName) #VariableName
// 最大引数がN
#define over2(o1, o2, name, ...) name
#define over3(o1, o2, o3, name, ...) name
#define over4(o1, o2, o3, o4, name, ...) name
#define over5(o1, o2, o3, o4, o5, name, ...) name
#define over6(o1, o2, o3, o4, o5, o6, name, ...) name
#define over7(o1, o2, o3, o4, o5, o6, o7, name, ...) name
#define over8(o1, o2, o3, o4, o5, o6, o7, o8, name, ...) name
#define over9(o1, o2, o3, o4, o5, o6, o7, o8, o9, name, ...) name
#define over10(o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, name, ...) name
#ifdef _DEBUG
string message;
string res_mes;
bool was_deb = false;
int cou_deb = 0;
[[noreturn]] void my_exit(signed e = 0) {
if (was_deb && sz(res_mes)) {
cerr << "result = " << endl << res_mes << endl;
}
if (sz(message)) {
cerr << "****************************" << endl;
cerr << "Note." << endl;
cerr << message << endl;
cerr << "****************************" << endl;
}
exit(e);
}
#define exit my_exit
#endif
void assert2(bool b, const string &s = "") {
if (!b) {
cerr << s << endl;
exit(1); /*assert(0);*/
}
}
// my_nulloptをあらゆる操作の単位元的な物として扱う
// vectorの参照外時に返したり、右辺値として渡されたときに何もしないなど
struct my_nullopt_t {
} my_nullopt;
#define nullopt_t my_nullopt_t
#define nullopt my_nullopt
/*@formatter:off*/
// 値が無いときは、setを使わない限り代入できない
//=を使っても無視される
template <class T> struct my_optional {
private:
bool is_null;
T v;
public:
typedef T value_type;
my_optional() : is_null(true) {}
my_optional(const nullopt_t &) : is_null(true) {}
my_optional(const T &v) : v(v), is_null(false) {}
bool has_value() const { return !is_null; }
T &value() {
static string mes = "optional has no value";
assert2(!is_null, mes);
return v;
}
const T &value() const {
static string mes = "optional has no value";
assert2(!is_null, mes);
return v;
}
void set(const T &nv) {
is_null = false;
v = nv;
}
template <class U> void operator=(const U &v) {
set(v); // null状態でも代入出来るようにした
// if (has_value())value() = v; else return;
}
template <class U> void operator=(const my_optional<U> &v) {
if (/*has_value() && */ v.has_value())
(*this) = v;
else
return;
}
/*@formatter:off*/
void reset() { is_null = true; }
void operator=(const nullopt_t &) { reset(); }
template <require_t(!is_same2(T, bool))> explicit operator bool() {
return !is_null;
}
// nullの時はエラー
operator T &() { return value(); }
operator const T &() const { return value(); }
my_optional<T> operator++() {
if (this->has_value()) {
this->value()++;
return *this;
} else {
return *this;
}
}
my_optional<T> operator++(signed) {
if (this->has_value()) {
auto tem = *this;
this->value()++;
return tem;
} else {
return *this;
}
}
my_optional<T> operator--() {
if (this->has_value()) {
this->value()--;
return *this;
} else {
return *this;
}
}
my_optional<T> operator--(signed) {
if (this->has_value()) {
auto tem = *this;
this->value()--;
return tem;
} else {
return *this;
}
}
};
template <class T> istream &operator>>(istream &iss, my_optional<T> &v) {
T val;
iss >> val;
v.set(val);
return iss;
}
#define optional my_optional
template <class T> using opt = my_optional<T>;
// template<class T, class A = std::allocator<T>> struct debtor : std::vector<T,
// A> {
template <class Key, class T, class Compare = less<Key>,
class Allocator = allocator<pair<const Key, T>>>
struct o_map : std::map<Key, optional<T>, Compare, Allocator> {
optional<T> emp;
o_map() : std::map<Key, optional<T>, Compare, Allocator>() {}
auto operator()(const nullopt_t &) { return nullopt; }
optional<T> &operator()(const optional<Key> &k) {
if (k.has_value()) {
return std::map<Key, optional<T>, Compare, Allocator>::operator[](
k.value());
} else {
emp.reset();
return emp;
}
}
optional<T> &operator()(const Key &k) {
auto &v = std::map<Key, optional<T>, Compare, Allocator>::operator[](k);
if (v.has_value())
return v;
else {
v.set(0);
return v;
}
}
template <class U> void operator[](U) {
static string mes = "s_map cant []";
assert2(0, mes);
}
};
// 以下、空のoptionalをnulloptと書く
// ov[-1(参照外)] でnulloptを返す
// ov[nullopt] で nulloptをかえす
template <class T> struct ov {
optional<T> emp;
vector<optional<T>> v;
ov(int i = 0, T val = 0) : v(i, val) {}
template <class U> ov(const U &rhs) {
v.resize(sz(rhs));
for (int i = 0; i < sz(rhs); i++)
v[i].set(rhs[i]);
}
optional<T> &operator()(int i) {
if (i < 0 || sz(v) <= i) {
emp.reset();
return emp;
} else {
return v[i];
}
}
optional<T> &operator()(const nullopt_t &) { return operator()(-1); }
optional<T> &operator()(const optional<T> &i) {
if (i.has_value())
return operator()(i.value());
else {
return operator()(-1);
}
}
/*@formatter:off*/
};
template <class T> string deb_tos(const ov<T> &v) { return deb_tos(v.v); }
// vectorに対しての処理は.vを呼ぶ
template <class T> class ovv {
optional<T> emp;
public:
vector<vector<optional<T>>> v;
ovv(int i = 0, int j = 0, T val = 0) : v(i, vector<optional<T>>(j, val)) {}
ovv(const vector<vector<T>> &S) {
v.resize(S.size());
for (int h = 0; h < sz(S); h++) {
v[h].resize(sz(S[h]));
for (int w = 0; w < sz(S[h]); w++) {
v[h][w].set(S[h][w]);
}
}
}
optional<T> &operator()(int i, int j) {
if (i < 0 || j < 0 || sz(v) <= i || sz(v[i]) <= j) {
emp.reset();
return emp;
} else {
return v[i][j];
}
}
// 再帰ver 遅いと思う
// optional<T>& gets(optional<T>& v){return v;}
// template<class V, class H, class... U> optional<T>& gets(V& v, H i, U...
// tail){ if constexpr(is_same2(H, nullopt_t))return
// operator()(-1,-1); else if constexpr(is_same2(H, optional<int>)){
// if(i.has_value())return gets(v[(int)i], tail...); else return
// operator()(-1,-1); }else if constexpr(is_integral<H>::value){
// return gets(v[(int)i], tail...); }else{ assert(0);
// return emp; } }
#if __cplusplus >= 201703L
// if constexprバージョン 上が遅かったらこれで
template <class U, class V>
optional<T> &operator()(const U &i, const V &j) { /*駄目な場合を除外*/
if constexpr (is_same2(U, nullopt_t) || is_same2(U, nullopt_t)) {
return operator()(-1, -1); /* o, o*/
} else if constexpr (is_same2(U, optional<int>) &&
is_same2(V, optional<int>)) {
return operator()(i.has_value() ? (int)i : -1,
j.has_value() ? (int)j : -1); /* o, x*/
} else if constexpr (is_same2(U, optional<int>)) {
return operator()(i.has_value() ? (int)i : -1, (int)j); /* x, o*/
} else if constexpr (is_same2(V, optional<int>)) {
return operator()((int)i, j.has_value() ? (int)j : -1); /* x, x*/
} else {
return operator()((int)i, (int)j);
}
}
#endif
operator const vector<vector<optional<T>>> &() { return v; }
};
template <class T> istream &operator>>(istream &iss, ovv<T> &a) {
for (int h = 0; h < sz(a); h++) {
for (int w = 0; w < sz(a[h]); w++) {
iss >> a.v[h][w];
}
}
return iss;
}
template <class T> ostream &operator<<(ostream &os, ovv<T> &a) {
os << a.v;
return os;
}
template <class T> string deb_tos(const ovv<T> &v) { return deb_tos(v.v); }
template <class T> struct ov3 {
optional<T> emp;
vector<vector<vector<optional<T>>>> v;
ov3(int i, int j, int k, T val = 0)
: v(i, vector<vector<optional<T>>>(j, vector<optional<T>>(k, val))) {}
optional<T> &operator()(int i, int j, int k) {
if (i < 0 || j < 0 || sz(v) <= i || sz(v[i]) <= j) {
if (k < 0 || sz(v[i][j]) <= k) {
emp.reset();
return emp;
}
}
return v[i][j][k];
}
private:
#if __cplusplus >= 201703L
// 再帰ver 遅いと思う
template <class V, class H> optional<T> &gets(V &nowv, H i) {
if constexpr (is_same2(H, nullopt_t)) {
emp.reset();
return emp;
} else if constexpr (is_same2(H, optional<int>)) {
if (i.has_value()) {
return nowv[(int)i];
} else {
emp.reset();
return emp;
}
} else if constexpr (is_integral<H>::value) {
return nowv[(int)i];
} else {
static string mes = "ov3 error not index";
assert2(0, mes);
emp.reset();
return emp;
}
}
// todo const &消した
template <class V, class H, class... U>
optional<T> &gets(V &nowv, H i, U... tail) {
if constexpr (is_same2(H, nullopt_t)) {
emp.reset();
return emp;
} else if constexpr (is_same2(H, optional<int>)) {
if (i.has_value()) {
return gets(nowv[(int)i], tail...);
} else {
emp.reset();
return emp;
}
} else if constexpr (is_integral<H>::value) {
return gets(nowv[(int)i], tail...);
} else {
static string mes = "ov3 error not index";
assert2(0, mes);
emp.reset();
return emp;
}
}
#endif
public:
template <class U, class V, class W> optional<T> &operator()(U i, V j, W k) {
return gets(v, i, j, k);
}
/*@formatter:off*/
};
template <class T> string deb_tos(const ov3<T> &v) { return deb_tos(v.v); }
// nullopt_t
// 優先順位
// null, [opt, tem]
// + と += は違う意味を持つ
// val+=null : val
// val+null : null
//
//+は途中計算
//+=は最終的に格納したい値にだけ持たせる
//+=がvoidを返すのは、途中計算で使うのを抑制するため
// nulloptを考慮する際、計算途中では+を使ってnulloptを作り
// 格納する際は+=で無効にする必要がある
// 演算子==
// optional<int>(10) == 10
// 全ての型に対応させ、value_typeが等しいかを見るようにするのもありかも
// null同士を比較する状況はおかしいのではないか
bool operator==(const nullopt_t &, const nullopt_t &) {
assert2(0, "nul == null cant hikaku");
return false;
}
template <class T> bool operator==(const nullopt_t &, const T &) {
return false;
}
template <class T> bool operator!=(const nullopt_t &, const T &) {
return true;
}
template <class T> bool operator==(const T &, const nullopt_t &) {
return false;
}
template <class T> bool operator!=(const T &, const nullopt_t &) {
return true;
}
// nullを
nullopt_t &operator+(const nullopt_t &, const nullopt_t &) { return nullopt; }
nullopt_t &operator-(const nullopt_t &, const nullopt_t &) { return nullopt; }
nullopt_t &operator*(const nullopt_t &, const nullopt_t &) { return nullopt; }
nullopt_t &operator/(const nullopt_t &, const nullopt_t &) { return nullopt; }
nullopt_t &operator+=(const nullopt_t &, const nullopt_t &) { return nullopt; }
nullopt_t &operator-=(const nullopt_t &, const nullopt_t &) { return nullopt; }
nullopt_t &operator*=(const nullopt_t &, const nullopt_t &) { return nullopt; }
nullopt_t &operator/=(const nullopt_t &, const nullopt_t &) { return nullopt; }
template <class ANY> nullopt_t operator+(const nullopt_t &, const ANY &) {
return nullopt;
}
template <class ANY> nullopt_t operator-(const nullopt_t &, const ANY &) {
return nullopt;
}
template <class ANY> nullopt_t operator*(const nullopt_t &, const ANY &) {
return nullopt;
}
template <class ANY> nullopt_t operator/(const nullopt_t &, const ANY &) {
return nullopt;
}
template <class ANY> nullopt_t operator+(const ANY &, const nullopt_t &) {
return nullopt;
}
template <class ANY> nullopt_t operator-(const ANY &, const nullopt_t &) {
return nullopt;
}
template <class ANY> nullopt_t operator*(const ANY &, const nullopt_t &) {
return nullopt;
}
template <class ANY> nullopt_t operator/(const ANY &, const nullopt_t &) {
return nullopt;
}
template <class ANY> void operator+=(nullopt_t &, const ANY &) {}
template <class ANY> void operator-=(nullopt_t &, const ANY &) {}
template <class ANY> void operator*=(nullopt_t &, const ANY &) {}
template <class ANY> void operator/=(nullopt_t &, const ANY &) {}
template <class ANY> void operator+=(ANY &, const nullopt_t &) {}
template <class ANY> void operator-=(ANY &, const nullopt_t &) {}
template <class ANY> void operator*=(ANY &, const nullopt_t &) {}
template <class ANY> void operator/=(ANY &, const nullopt_t &) {}
template <class T> struct is_optional : false_type {};
template <class T> struct is_optional<optional<T>> : true_type {};
template <class T, class U>
true_type both_optional(optional<T> t, optional<U> u);
false_type both_optional(...);
template <class T, class U>
class opt_check : public decltype(both_optional(declval<T>(), declval<U>())) {};
// optionalは同じ型同士しか足せない
//(o, t), (t, o), (o, o)
#define opt_tem(op) \
template <class O, class O_ret = decltype(declval<O>() op declval<O>())> \
optional<O_ret> operator op(const optional<O> &opt1, \
const optional<O> &opt2) { \
if (!opt1.has_value() || !opt2.has_value()) { \
return optional<O_ret>(); \
} else { \
return optional<O_ret>(opt1.value() op opt2.value()); \
} \
} \
template <class O, class T, \
class O_ret = decltype(declval<O>() op declval<O>())> \
auto operator op(const optional<O> &opt, const T &tem) \
->require_ret(!(opt_check<optional<O>, T>::value), optional<O_ret>) { \
if (!opt.has_value()) { \
return optional<O_ret>(); \
} else { \
return optional<O_ret>(opt.value() op tem); \
} \
} \
template <class O, class T, \
class O_ret = decltype(declval<O>() op declval<O>())> \
auto operator op(const T &tem, const optional<O> &opt) \
->require_ret(!(opt_check<optional<O>, T>::value), optional<O_ret>) { \
if (!opt.has_value()) { \
return optional<O_ret>(); \
} else { \
return optional<O_ret>(opt.value() op tem); \
} \
}
/*@formatter:off*/
opt_tem(+) opt_tem(-) opt_tem(*) opt_tem(/)
// 比較はoptional<bool>を返す
opt_tem(<) opt_tem(>) opt_tem(<=) opt_tem(>=)
/*@formatter:on*/ /*@formatter:off*/
template <class O, class T>
bool operator==(const optional<O> &opt, const T &tem) {
if (opt.has_value()) {
return opt.value() == tem;
} else
return nullopt == tem;
}
template <class O, class T>
bool operator!=(const optional<O> &opt, const T &tem) {
if (opt.has_value()) {
return opt.value() != tem;
} else
return nullopt != tem;
}
template <class O, class T>
bool operator==(const T &tem, const optional<O> &opt) {
if (opt.has_value()) {
return opt.value() == tem;
} else
return nullopt == tem;
}
template <class O, class T>
bool operator!=(const T &tem, const optional<O> &opt) {
if (opt.has_value()) {
return opt.value() != tem;
} else
return nullopt != tem;
}
template <class O>
bool operator==(const optional<O> &opt1, const optional<O> &opt2) {
if (opt1.has_value() != opt2.has_value()) {
return false;
} else if (opt1.has_value()) {
return opt1.value() == opt2.value();
} else {
return nullopt == nullopt;
}
}
template <class O>
bool operator!=(const optional<O> &opt1, const optional<O> &opt2) {
return !(opt1 == opt2);
}
//(a+=null) != (a=a+null)
// a null
template <class T, class O> void operator+=(T &tem, const optional<O> &opt) {
if (opt.has_value()) {
tem += opt.value();
}
}
template <class T, class O> void operator-=(T &tem, const optional<O> &opt) {
if (opt.has_value()) {
tem -= opt.value();
}
}
template <class T, class O> void operator*=(T &tem, const optional<O> &opt) {
if (opt.has_value()) {
tem *= opt.value();
}
}
template <class T, class O> void operator/=(T &tem, const optional<O> &opt) {
if (opt.has_value()) {
tem /= opt.value();
}
}
template <class T, class O> void operator+=(optional<O> &opt, const T &tem) {
if (opt.has_value()) {
opt.value() += tem;
}
}
template <class T, class O> void operator-=(optional<O> &opt, const T &tem) {
if (opt.has_value()) {
opt.value() -= tem;
}
}
template <class T, class O> void operator*=(optional<O> &opt, const T &tem) {
if (opt.has_value()) {
opt.value() *= tem;
}
}
template <class T, class O> void operator/=(optional<O> &opt, const T &tem) {
if (opt.has_value()) {
opt.value() /= tem;
}
}
//
template <class Ol, class Or>
void operator+=(optional<Ol> &opl, const optional<Or> &opr) {
if (opr.has_value()) {
return opl += opr.value();
}
}
template <class Ol, class Or>
void operator-=(optional<Ol> &opl, const optional<Or> &opr) {
if (opr.has_value()) {
return opl -= opr.value();
}
}
template <class Ol, class Or>
void operator*=(optional<Ol> &opl, const optional<Or> &opr) {
if (opr.has_value()) {
return opl *= opr.value();
}
}
template <class Ol, class Or>
void operator/=(optional<Ol> &opl, const optional<Or> &opr) {
if (opr.has_value()) {
return opl /= opr.value();
}
}
/*@formatter:off*/
template <class U> auto max(const nullopt_t &, const U &val) { return val; }
template <class U> auto max(const U &val, const nullopt_t &) { return val; }
template <class U> auto min(const nullopt_t &, const U &val) { return val; }
template <class U> auto min(const U &val, const nullopt_t &) { return val; }
template <class T, class U> auto max(const optional<T> &opt, const U &val) {
if (opt.has_value())
return max(opt.value(), val);
else
return val;
}
template <class T, class U> auto max(const U &val, const optional<T> &opt) {
if (opt.has_value())
return max(opt.value(), val);
else
return val;
}
template <class T, class U> auto min(const optional<T> &opt, const U &val) {
if (opt.has_value())
return min(opt.value(), val);
else
return val;
}
template <class T, class U> auto min(const U &val, const optional<T> &opt) {
if (opt.has_value())
return min(opt.value(), val);
else
return val;
}
// null , optional, T
bool chma(nullopt_t &, const nullopt_t &) { return false; }
template <class T> bool chma(T &opt, const nullopt_t &) { return false; }
template <class T> bool chma(nullopt_t &, const T &opt) { return false; }
template <class T> bool chma(optional<T> &olv, const optional<T> &orv) {
if (orv.has_value()) {
return chma(olv, orv.value());
} else
return false;
}
template <class T, class U> bool chma(optional<T> &opt, const U &rhs) {
if (opt.has_value()) {
return chma(opt.value(), rhs);
} else
return false;
}
template <class T, class U> bool chma(T &lhs, const optional<U> &opt) {
if (opt.has_value()) {
return chma(lhs, opt.value());
} else
return false;
}
bool chmi(nullopt_t &, const nullopt_t &) { return false; }
template <class T> bool chmi(T &opt, const nullopt_t &) { return false; }
template <class T> bool chmi(nullopt_t &, const T &opt) { return false; }
template <class T> bool chmi(optional<T> &olv, const optional<T> &orv) {
if (orv.has_value()) {
return chmi(olv, orv.value());
} else
return false;
}
template <class T, class U> bool chmi(optional<T> &opt, const U &rhs) {
if (opt.has_value()) {
return chmi(opt.value(), rhs);
} else
return false;
}
template <class T, class U> bool chmi(T &lhs, const optional<U> &opt) {
if (opt.has_value()) {
return chmi(lhs, opt.value());
} else
return false;
}
template <class T> ostream &operator<<(ostream &os, optional<T> p) {
if (p.has_value())
os << p.value();
else
os << "e";
return os;
}
template <class T> using opt = my_optional<T>;
struct xorshift {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(const uint64_t &x) const {
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
size_t operator()(const std::pair<ll, ll> &x) const {
ll v = ((x.first) << 32) | x.second;
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(v + FIXED_RANDOM);
}
size_t operator()(const std::tuple<ll, ll, ll> &x) const {
vector<ll> x2 = {get<0>(x), get<1>(x), get<2>(x)};
return operator()(x2);
}
size_t operator()(const std::tuple<ll, ll, ll, ll> &x) const {
vector<ll> x2 = {get<0>(x), get<1>(x), get<2>(x), get<3>(x)};
return operator()(x2);
}
size_t operator()(const std::tuple<ll, ll, ll, ll, ll> &x) const {
vector<ll> x2 = {get<0>(x), get<1>(x), get<2>(x), get<3>(x), get<4>(x)};
return operator()(x2);
}
template <class T, class U>
size_t operator()(const std::pair<T, U> &x) const {
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
uint64_t hasx = splitmix64(x.first);
uint64_t hasy = splitmix64(x.second + FIXED_RANDOM);
return hasx ^ hasy;
}
template <class T> size_t operator()(const vector<T> &x) const {
uint64_t has = 0;
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
uint64_t rv = splitmix64(FIXED_RANDOM);
for (int i = 0; i < sz(x); i++) {
uint64_t v = splitmix64(x[i] + rv);
has ^= v;
rv = splitmix64(rv);
}
return has;
}
};
#ifdef _DEBUG
// #define use_debtor
// template<class T, class U, class X> auto count(unordered_map<T, U> &a, X k) {
// return a.find(k) != a.end(); }
#ifdef use_debtor
// https://marycore.jp/prog/cpp/class-extension-methods/ 違うかも
template <class T, class A = std::allocator<T>>
struct debtor : std::vector<T, A> {
using std::vector<T, A>::vector;
template <class U> int deb_v(U a, int v) { return v; }
template <class U> int deb_v(debtor<U> &a, int v = 0) {
cerr << a.size() << " ";
return deb_v(a.at(0), v + 1);
}
template <class U> void deb_o(U a) { cerr << a << " "; }
template <class U> void deb_o(debtor<U> &a) {
for (int i = 0; i < min((int)a.size(), 15ll); i++) {
deb_o(a[i]);
}
if ((int)a.size() > 15) {
cerr << "...";
}
cerr << endl;
}
typename std::vector<T>::reference my_at(typename std::vector<T>::size_type n,
vector<int> &ind) {
if (n < 0 || n >= (int)this->size()) {
int siz = (int)this->size();
cerr << "vector size = ";
int dim = deb_v((*this));
cerr << endl;
ind.push_back(n);
cerr << "out index at ";
for (auto &&i : ind) {
cerr << i << " ";
}
cerr << endl;
cerr << endl;
if (dim <= 2) {
deb_o((*this));
}
exit(0);
}
return this->at(n);
}
typename std::vector<T>::reference
operator[](typename std::vector<T>::size_type n) {
if (n < 0 || n >= (int)this->size()) {
int siz = (int)this->size();
cerr << "vector size = ";
int dim = deb_v((*this));
cerr << endl;
cerr << "out index at " << n << endl;
cerr << endl;
if (dim <= 2) {
deb_o((*this));
}
exit(0);
}
return this->at(n);
}
};
#define vector debtor
#endif
#ifdef use_pbds
template <class T> struct my_pbds_tree {
set<T> s;
auto begin() { return s.begin(); }
auto end() { return s.end(); }
auto rbegin() { return s.rbegin(); }
auto rend() { return s.rend(); }
auto empty() { return s.empty(); }
auto size() { return s.size(); }
void clear() { s.clear(); }
template <class U> void insert(U v) { s.insert(v); }
template <class U> void operator+=(U v) { insert(v); }
template <class F> auto erase(F v) { return s.erase(v); }
template <class U> auto find(U v) { return s.find(v); }
template <class U> auto lower_bound(U v) { return s.lower_bound(v); }
template <class U> auto upper_bound(U v) { return s.upper_bound(v); }
auto find_by_order(ll k) {
auto it = s.begin();
for (ll i = 0; i < k; i++)
it++;
return it;
}
auto order_of_key(ll v) {
auto it = s.begin();
ll i = 0;
for (; it != s.end() && *it < v; i++)
it++;
return i;
}
};
#define pbds(T) my_pbds_tree<T>
#endif
// 区間削除は出来ない
// gp_hash_tableでcountを使えないようにするため
template <class K, class V> using umap_f = unordered_map<K, V>;
template <typename K, typename T> using map_f = map<K, T>;
#else
#define endl '\n'
// umapはunorderd_mapになる
// umapiはgp_hash_table
// find_by_order(k) k番目のイテレーター
// order_of_key(k) k以上が前から何番目か
#define pbds(U) \
__gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, \
__gnu_pbds::tree_order_statistics_node_update>
template <class K, class V>
struct umap_f : public __gnu_pbds::gp_hash_table<K, V, xorshift> {
int count(const K &k) { return this->find(k) != this->end(); }
};
template <typename K, typename T>
using map_f = __gnu_pbds::tree<K, T, less<K>, __gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update>;
#endif
#define umapi unordered_map<ll, ll>
#define umapp unordered_map<P, ll>
#define umappp unordered_map<P, P>
#define umapu unordered_map<uint64_t, ll>
#define umapip unordered_map<ll, P>
template <class T, class U, class X> auto count(unordered_map<T, U> &a, X k) {
return a.find(k) != a.end();
}
/*@formatter:off*/
#ifdef use_pbds
template <class U, class L>
void operator+=(
__gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update> &s,
L v) {
s.insert(v);
}
#endif
// 衝突対策
#define ws ws_
typedef tuple<ll, ll, ll> T;
typedef tuple<ll, ll, ll, ll> F;
// T mt(ll a, ll b, ll c) { return T(a, b, c); }
// F mf(ll a, ll b, ll c, ll d) { return F(a, b, c, d); }
// 関数内をまとめる
// 初期値l=-1, r=-1
void set_lr12(int &l, int &r, int n) { /*r==-1*/
if (r == -1) {
if (l == -1) {
l = 0;
r = n;
} else {
r = l;
l = 0;
}
}
}
//@マクロ省略系 型,構造
// using で元のdoubleを同時に使えるはず
#define double_big
#ifdef double_big
#define double long double
// #define pow powl
#endif
using dou = double;
/*@formatter:off*/
template <class T> T MAX() { return numeric_limits<T>::max(); }
template <class T> T MIN() { return numeric_limits<T>::min(); }
constexpr ll inf = (ll)1e9 + 100;
constexpr ll linf = (ll)1e18 + 100;
constexpr dou dinf = (dou)linf * linf;
constexpr char infc = '{';
const string infs = "{";
template <class T> T INF() { return MAX<T>() / 2; }
template <> signed INF() { return inf; }
template <> ll INF() { return linf; }
template <> double INF() { return dinf; }
template <> char INF() { return infc; }
template <> string INF() { return infs; }
const double eps = 1e-9;
// #define use_epsdou
#ifdef use_epsdou
// 基本コメントアウト
struct epsdou {
double v;
epsdou(double v = 0) : v(v) {}
template <class T> epsdou &operator+=(T b) {
v += (double)b;
return (*this);
}
template <class T> epsdou &operator-=(T b) {
v -= (double)b;
return (*this);
}
template <class T> epsdou &operator*=(T b) {
v *= (double)b;
return (*this);
}
template <class T> epsdou &operator/=(T b) {
v /= (double)b;
return (*this);
}
epsdou operator+(epsdou b) { return v + (double)b; }
epsdou operator-(epsdou b) { return v - (double)b; }
epsdou operator*(epsdou b) { return v * (double)b; }
epsdou operator/(epsdou b) { return v / (double)b; }
epsdou operator-() const { return epsdou(-v); }
template <class T> bool operator<(T b) { return v < (double)b; }
template <class T> bool operator>(T b) {
auto r = (double)b;
return v > (double)b;
}
template <class T> bool operator==(T b) { return fabs(v - (double)b) <= eps; }
template <class T> bool operator<=(T b) {
return v < (double)b || fabs(v - b) <= eps;
}
template <class T> bool operator>=(T b) {
return v > (double)b || fabs(v - b) <= eps;
}
operator double() { return v; }
};
template <> epsdou MAX() { return MAX<double>(); }
template <> epsdou MIN() { return MIN<double>(); }
// priqrity_queue等で使うのに必要
bool operator<(const epsdou &a, const epsdou &b) { return a.v < b.v; }
bool operator>(const epsdou &a, const epsdou &b) { return a.v > b.v; }
istream &operator>>(istream &iss, epsdou &a) {
iss >> a.v;
return iss;
}
ostream &operator<<(ostream &os, epsdou &a) {
os << a.v;
return os;
}
#define eps_conr_t(o) \
template <class T> epsdou operator o(T a, epsdou b) { return (dou)a o b.v; }
#define eps_conl_t(o) \
template <class T> epsdou operator o(epsdou a, T b) { return a.v o(dou) b; }
eps_conl_t(+) eps_conl_t(-) eps_conl_t(*) eps_conl_t(/) eps_conr_t(+)
eps_conr_t(-) eps_conr_t(*) eps_conr_t(/)
// template<class U> epsdou max(epsdou a, U b){return a.v>b ? a.v: b;}
// template<class U> epsdou max(U a, epsdou b){return a>b.v ? a: b.v;}
// template<class U> epsdou min(epsdou a, U b){return a.v<b ? a.v: b;}
// template<class U> epsdou min(U a, epsdou b){return a<b.v ? a: b.v;}
#undef double
#define double epsdou
#undef dou
#define dou epsdou
#endif
template <class T = int, class A, class B = int>
T my_pow(A a, B b = 2) {
if (b < 0)
return (T)1 / my_pow<T>(a, -b);
#if __cplusplus >= 201703L
if constexpr (is_floating_point<T>::value) {
return pow((T)a, (T)b);
} else if constexpr (is_floating_point<A>::value) {
assert2(
0,
"pow <not dou>(dou, )"); /*return
0;しない方がコンパイル前に(voidを受け取るので)エラーが出ていいかも*/
} else if constexpr (is_floating_point<B>::value) {
assert2(
0,
"pow <not dou>(, dou)"); /*return
0;しない方がコンパイル前に(voidを受け取るので)エラーが出ていいかも*/
} else {
#endif
T ret = 1;
T bek = a;
while (b) {
if (b & 1)
ret *= bek;
bek *= bek;
b >>= 1;
}
return ret;
#if __cplusplus >= 201703L
}
#endif
}
#define pow my_pow
#define ull unsigned long long
using itn = int;
using str = string;
using bo = bool;
#define au auto
using P = pair<ll, ll>;
#define fi first
#define se second
#define beg begin
#define rbeg rbegin
#define con continue
#define bre break
#define brk break
#define is ==
#define el else
#define elf else if
#define upd update
#define sstream stringstream
#define maxq 1
#define minq -1
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
#define MALLOC(type, len) (type *)malloc((len) * sizeof(type))
#define lam1(ret) [&](auto &&v) { return ret; }
#define lam2(v, ret) [&](auto &&v) { return ret; }
#define lam(...) over2(__VA_ARGS__, lam2, lam1)(__VA_ARGS__)
#define lamr(right) [&](auto &&p) { return p right; }
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());
// マクロ省略系 コンテナ
using vi = vector<ll>;
using vb = vector<bool>;
using vs = vector<string>;
using vd = vector<double>;
using vc = vector<char>;
using vp = vector<P>;
using vt = vector<T>;
// #define V vector
#define vvt0(t) vector<vector<t>>
#define vvt1(t, a) vector<vector<t>> a
#define vvt2(t, a, b) vector<vector<t>> a(b)
#define vvt3(t, a, b, c) vector<vector<t>> a(b, vector<t>(c))
#define vvt4(t, a, b, c, d) vector<vector<t>> a(b, vector<t>(c, d))
#define vv(type, ...) \
over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(type, __VA_ARGS__)
#define vvi(...) vv(ll, __VA_ARGS__)
#define vvb(...) vv(bool, __VA_ARGS__)
#define vvs(...) vv(string, __VA_ARGS__)
#define vvd(...) vv(double, __VA_ARGS__)
#define vvc(...) vv(char, __VA_ARGS__)
#define vvp(...) vv(P, __VA_ARGS__)
#define vvt(...) vv(T, __VA_ARGS__)
// optional
#define vvoi(...) vv(optional<ll>, __VA_ARGS__)
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
#define vni(name, ...) auto name = make_v<ll>(__VA_ARGS__)
#define vnb(name, ...) auto name = make_v<bool>(__VA_ARGS__)
#define vns(name, ...) auto name = make_v<string>(__VA_ARGS__)
#define vnd(name, ...) auto name = make_v<double>(__VA_ARGS__)
#define vnc(name, ...) auto name = make_v<char>(__VA_ARGS__)
#define vnp(name, ...) auto name = make_v<P>(__VA_ARGS__)
#define vn(type, name, ...) auto name = make_v<type>(__VA_ARGS__)
#define PQ priority_queue<ll, vector<ll>, greater<ll>>
#define tos to_string
using mapi = map<ll, ll>;
using mapp = map<P, ll>;
using mapd = map<dou, ll>;
using mapc = map<char, ll>;
using maps = map<str, ll>;
using seti = set<ll>;
using setp = set<P>;
using setd = set<dou>;
using setc = set<char>;
using sets = set<str>;
using qui = queue<ll>;
#define uset unordered_set
#define useti unordered_set<ll, xorshift>
#define mset multiset
#define mseti multiset<ll>
#define umap unordered_map
#define mmap multimap
// 初期化子を与える
template <template <class...> class MAP, class K, class V, class... A>
struct init_map : public MAP<K, V, A...> {
V init_value;
init_map(V init_value = 0) : MAP<K, V, A...>(), init_value(init_value) {}
V &operator[](const K &k) {
if (MAP<K, V, A...>::count(k) == 0) {
return MAP<K, V, A...>::operator[](k) = init_value;
}
return MAP<K, V, A...>::operator[](k);
}
};
template <class... A> using map2 = init_map<map, A...>;
template <class... A> using umap2 = init_map<umap, A...>;
template <class... A> using umap_f2 = init_map<umap_f, A...>;
// 任意のマクロサポート用 使う度に初期化する //todo これを消す
int index_, v1_, v2_, v3_;
/*@formatter:off*/
string to_string(char c) {
string ret = "";
ret += c;
return ret;
}
template <class T> class pq_min_max {
vector<T> d;
void make_heap() {
for (int i = d.size(); i--;) {
if (i & 1 && d[i - 1] < d[i])
swap(d[i - 1], d[i]);
int k = down(i);
up(k, i);
}
}
inline int parent(int k) const { return ((k >> 1) - 1) & ~1; }
int down(int k) {
int n = d.size();
if (k & 1) { /* min heap*/
while (2 * k + 1 < n) {
int c = 2 * k + 3;
if (n <= c || d[c - 2] < d[c])
c -= 2;
if (c < n && d[c] < d[k]) {
swap(d[k], d[c]);
k = c;
} else
break;
}
} else { /* max heap*/
while (2 * k + 2 < n) {
int c = 2 * k + 4;
if (n <= c || d[c] < d[c - 2])
c -= 2;
if (c < n && d[k] < d[c]) {
swap(d[k], d[c]);
k = c;
} else
break;
}
}
return k;
}
int up(int k, int root = 1) {
if ((k | 1) < (int)d.size() && d[k & ~1] < d[k | 1]) {
swap(d[k & ~1], d[k | 1]);
k ^= 1;
}
int p;
while (root < k && d[p = parent(k)] < d[k]) { /*max heap*/
swap(d[p], d[k]);
k = p;
}
while (root < k && d[k] < d[p = parent(k) | 1]) { /* min heap*/
swap(d[p], d[k]);
k = p;
}
return k;
}
public:
pq_min_max() {}
pq_min_max(const vector<T> &d_) : d(d_) { make_heap(); }
template <class Iter> pq_min_max(Iter first, Iter last) : d(first, last) {
make_heap();
}
void operator+=(const T &x) {
int k = d.size();
d.push_back(x);
up(k);
}
void pop_min() {
if (d.size() < 3u) {
d.pop_back();
} else {
swap(d[1], d.back());
d.pop_back();
int k = down(1);
up(k);
}
}
void pop_max() {
if (d.size() < 2u) {
d.pop_back();
} else {
swap(d[0], d.back());
d.pop_back();
int k = down(0);
up(k);
}
}
const T &get_min() const { return d.size() < 2u ? d[0] : d[1]; }
const T &get_max() const { return d[0]; }
int size() const { return d.size(); }
bool empty() const { return d.empty(); }
};
// 小さいほうからM個取得するpq
template <class T> struct helper_pq_size {
pq_min_max<T> q;
T su = 0;
int max_size = 0;
helper_pq_size() {}
helper_pq_size(int max_size) : max_size(max_size) {}
void clear() {
q = pq_min_max<T>();
su = 0;
}
void operator+=(T v) {
su += v;
q += (v);
if (sz(q) > max_size) {
su -= q.get_max();
q.pop_max();
}
}
T sum() { return su; }
T top() { return q.get_min(); }
void pop() {
su -= q.get_min();
q.pop_min();
}
T poll() {
T ret = q.get_min();
su -= ret;
q.pop_min();
return ret;
}
ll size() { return q.size(); }
};
// 大きいほうからM個取得するpq
template <class T> struct helper_pqg_size {
pq_min_max<T> q;
T su = 0;
int max_size = 0;
helper_pqg_size() {}
helper_pqg_size(int max_size) : max_size(max_size) {}
void clear() {
q = pq_min_max<T>();
su = 0;
}
void operator+=(T v) {
su += v;
q += (v);
if (sz(q) > max_size) {
su -= q.get_min();
q.pop_min();
}
}
T sum() { return su; }
T top() { return q.get_max(); }
void pop() {
su -= q.get_max();
q.pop_max();
}
T poll() {
T ret = q.get_max();
su -= ret;
q.pop_min();
return ret;
}
ll size() { return q.size(); }
};
;
template <class T, class Container = vector<T>,
class Compare = std::less<typename Container::value_type>>
struct helper_pqg {
priority_queue<T, Container, Compare> q; /*小さい順*/
T su = 0;
helper_pqg() {}
void clear() {
q = priority_queue<T, vector<T>, greater<T>>();
su = 0;
}
void operator+=(T v) {
su += v;
q.push(v);
}
T sum() { return su; }
T top() { return q.top(); }
void pop() {
su -= q.top();
q.pop();
}
T poll() {
T ret = q.top();
su -= ret;
q.pop();
return ret;
}
ll size() { return q.size(); }
};
template <class T> using helper_pq = helper_pqg<T, vector<T>, greater<T>>;
#if __cplusplus >= 201703L
// 小さいほうからsize個残る
// Tがoptionalなら空の時nullを返す
template <class T> struct pq {
helper_pq<T> a_q; /*大きい順*/
helper_pq_size<T> b_q; /*大きい順*/
bool aquery;
T su = 0;
pq(int size = inf) {
aquery = size == inf;
if (!aquery) {
b_q = helper_pq_size<T>(size);
}
}
void clear() {
if (aquery)
a_q.clear();
else
b_q.clear();
}
void operator+=(T v) {
if (aquery)
a_q += v;
else
b_q += v;
}
// optionalなら空の時nullを返す
T top() {
if constexpr (is_optional<T>::value) {
if (aquery) {
if (sz(a_q) == 0)
return T();
return a_q.top();
} else {
if (sz(b_q) == 0)
return T();
return b_q.top();
}
} else {
if (aquery)
return a_q.top();
else
return b_q.top();
}
}
T sum() {
if (aquery)
return a_q.sum();
else
return b_q.sum();
}
// optionalなら空の時何もしない
void pop() {
if constexpr (is_optional<T>::value) {
if (aquery) {
if (sz(a_q))
a_q.pop();
} else {
if (sz(b_q))
b_q.pop();
}
} else {
if (aquery)
a_q.pop();
else
b_q.pop();
}
} /*T*/
T poll() {
if constexpr (is_optional<T>::value) {
if (aquery) {
if (sz(a_q) == 0)
return T();
return a_q.poll();
} else {
if (sz(b_q) == 0)
return T();
return b_q.poll();
}
} else {
if (aquery)
return a_q.poll();
else
return b_q.poll();
}
}
ll size() {
if (aquery)
return a_q.size();
else
return b_q.size();
}
/*@formatter:off*/
};
template <class T> struct pqg {
helper_pqg<T> a_q; /*大きい順*/
helper_pqg_size<T> b_q; /*大きい順*/
bool aquery;
T su = 0;
pqg(int size = inf) {
aquery = size == inf;
if (!aquery) {
b_q = helper_pqg_size<T>(size);
}
}
void clear() {
if (aquery)
a_q.clear();
else
b_q.clear();
}
void operator+=(T v) {
if (aquery)
a_q += v;
else
b_q += v;
}
T sum() {
if (aquery)
return a_q.sum();
else
return b_q.sum();
}
T top() {
if (aquery)
return a_q.top();
else
return b_q.top();
}
void pop() {
if (aquery)
a_q.pop();
else
b_q.pop();
}
T poll() {
if (aquery)
return a_q.poll();
else
return b_q.poll();
}
ll size() {
if (aquery)
return a_q.size();
else
return b_q.size();
}
};
#else
// 小さいほうからsize個残る
template <class T> struct pq {
helper_pq<T> a_q; /*大きい順*/
helper_pq_size<T> b_q; /*大きい順*/
bool aquery;
T su = 0;
pq(int size = inf) {
aquery = size == inf;
if (!aquery) {
b_q = helper_pq_size<T>(size);
}
}
void clear() {
if (aquery)
a_q.clear();
else
b_q.clear();
}
void operator+=(T v) {
if (aquery)
a_q += v;
else
b_q += v;
}
T sum() {
if (aquery)
return a_q.sum();
else
return b_q.sum();
}
T top() {
if (aquery)
return a_q.top();
else
return b_q.top();
}
void pop() {
if (aquery)
a_q.pop();
else
b_q.pop();
}
T poll() {
if (aquery)
return a_q.poll();
else
return b_q.poll();
}
ll size() {
if (aquery)
return a_q.size();
else
return b_q.size();
}
};
// 大きいほうからsize個残る
template <class T> struct pqg {
helper_pqg<T> a_q; /*大きい順*/
helper_pqg_size<T> b_q; /*大きい順*/
bool aquery;
T su = 0;
pqg(int size = inf) {
aquery = size == inf;
if (!aquery) {
b_q = helper_pqg_size<T>(size);
}
}
void clear() {
if (aquery)
a_q.clear();
else
b_q.clear();
}
void operator+=(T v) {
if (aquery)
a_q += v;
else
b_q += v;
}
T sum() {
if (aquery)
return a_q.sum();
else
return b_q.sum();
}
T top() {
if (aquery)
return a_q.top();
else
return b_q.top();
}
void pop() {
if (aquery)
a_q.pop();
else
b_q.pop();
}
T poll() {
if (aquery)
return a_q.poll();
else
return b_q.poll();
}
ll size() {
if (aquery)
return a_q.size();
else
return b_q.size();
}
};
#endif
#define pqi pq<ll>
#define pqgi pqg<ll>
template <class T> string deb_tos(pq<T> &q) {
vector<T> res;
auto temq = q;
while (sz(temq))
res.push_back(temq.top()), temq.pop();
stringstream ss;
ss << res;
return ss.str();
}
template <class T> string deb_tos(pqg<T> &q) {
vector<T> res;
auto temq = q;
while (sz(temq))
res.push_back(temq.top()), temq.pop();
stringstream ss;
ss << res;
return ss.str();
}
/*@formatter:off*/
// マクロ 繰り返し
// ↓@オーバーロード隔離
// todo 使わないもの非表示
#define rep1(n) for (ll rep1i = 0, rep1lim = n; rep1i < rep1lim; ++rep1i)
#define rep2(i, n) for (ll i = 0, rep2lim = n; i < rep2lim; ++i)
#define rep3(i, m, n) for (ll i = m, rep3lim = n; i < rep3lim; ++i)
#define rep4(i, m, n, ad) for (ll i = m, rep4lim = n; i < rep4lim; i += ad)
// 逆順 閉区間
#define rer2(i, n) for (ll i = n; i >= 0; i--)
#define rer3(i, m, n) for (ll i = m, rer3lim = n; i >= rer3lim; i--)
#define rer4(i, m, n, dec) for (ll i = m, rer4lim = n; i >= rer4lim; i -= dec)
#ifdef use_for
// ループを一つにまとめないとフォーマットで汚くなるため
#define nex_ind1(i) i++
#define nex_ind2(i, j, J) \
i = (j + 1 == J) ? i + 1 : i, j = (j + 1 == J ? 0 : j + 1)
#define nex_ind3(i, j, k, J, K) \
i = (j + 1 == J && k + 1 == K) ? i + 1 : i, \
j = (k + 1 == K) ? (j + 1 == J ? 0 : j + 1) : j, \
k = (k + 1 == K ? 0 : k + 1)
#define nex_ind4(i, j, k, l, J, K, L) \
i = (j + 1 == J && k + 1 == K && l + 1 == L) ? i + 1 : i, \
j = (k + 1 == K && l + 1 == L) ? (j + 1 == J ? 0 : j + 1) : j, \
k = (l + 1 == L ? (k + 1 == K ? 0 : k + 1) : k), l = l + 1 == L ? 0 : l + 1
#define nex_ind5(i, j, k, l, m, J, K, L, M) \
i = (j + 1 == J && k + 1 == K && l + 1 == L && m + 1 == M) ? i + 1 : i, \
j = (k + 1 == K && l + 1 == L && m + 1 == M) ? (j + 1 == J ? 0 : j + 1) : j, \
k = (l + 1 == L && m + 1 == M ? (k + 1 == K ? 0 : k + 1) : k), \
l = m + 1 == M ? l + 1 == L ? 0 : l + 1 : l, m = m + 1 == M ? 0 : m + 1
#define repss2(i, I) for (int i = 0; i < I; i++)
#define repss4(i, j, I, J) \
for (int i = (J ? 0 : I), j = 0; i < I; nex_ind2(i, j, J))
#define repss6(i, j, k, I, J, K) \
for (int i = (J && K ? 0 : I), j = 0, k = 0; i < I; nex_ind3(i, j, k, J, K))
#define repss8(i, j, k, l, I, J, K, L) \
for (int i = (J && K && L ? 0 : I), j = 0, k = 0, l = 0; i < I; \
nex_ind4(i, j, k, l, J, K, L))
#define repss10(i, j, k, l, m, I, J, K, L, M) \
for (int i = (J && K && L && M ? 0 : I), j = 0, k = 0, l = 0, m = 0; i < I; \
nex_ind5(i, j, k, l, m, J, K, L, M))
// i,j,k...をnまで見る
#define reps2(i, n) repss2(i, n)
#define reps3(i, j, n) repss4(i, j, n, n)
#define reps4(i, j, k, n) repss6(i, j, k, n, n, n)
#define reps5(i, j, k, l, n) repss8(i, j, k, l, n, n, n, n)
template <class T>
void nex_repv2(int &i, int &j, int &I, int &J, vector<vector<T>> &s) {
while (1) {
j++;
if (j >= J) {
j = 0;
i++;
if (i < I) {
J = (int)s[i].size();
}
}
if (i >= I || J)
return;
}
}
template <class T>
void nex_repv3(int &i, int &j, int &k, int &I, int &J, int &K,
vector<vector<vector<T>>> &s) {
while (1) {
k++;
if (k >= K) {
k = 0;
j++;
if (j >= J) {
j = 0;
i++;
if (i >= I)
return;
}
}
J = (int)s[i].size();
K = (int)s[i][j].size();
if (J && K)
return;
}
}
#define repv_2(i, a) repss2(i, sz(a))
// 正方形である必要はない
// 直前を持つのとどっちが早いか
#define repv_3(i, j, a) \
for (int repvI = (int)a.size(), repvJ = (int)a[0].size(), i = 0, j = 0; \
i < repvI; nex_repv2(i, j, repvI, repvJ, a))
// 箱状になっている事が要求される つまり[i] 次元目の要素数は一定
#define repv_4(i, j, k, a) \
for (int repvI = (int)a.size(), repvJ = (int)a[0].size(), \
repvK = (int)a[0][0].size(), i = 0, j = 0, k = 0; \
i < repvI; nex_repv3(i, j, k, repvI, repvJ, repvK, a))
#define repv_5(i, j, k, l, a) \
repss8(i, j, k, l, sz(a), sz(a[0]), sz(a[0][0]), sz(a[0][0][0]))
#define repv_6(i, j, k, l, m, a) \
repss10(i, j, k, l, m, sz(a), sz(a[0]), sz(a[0][0]), sz(a[0][0][0]), \
sz(a[0][0][0][0]))
#endif
template <typename T> struct has_rbegin_rend {
private:
template <typename U>
static auto check(U &&obj)
-> decltype(std::rbegin(obj), std::rend(obj), std::true_type{});
static std::false_type check(...);
public:
static constexpr bool value = decltype(check(std::declval<T>()))::value;
};
template <typename T>
constexpr bool has_rbegin_rend_v = has_rbegin_rend<T>::value;
template <typename Iterator> class Range {
public:
Range(Iterator &&begin, Iterator &&end) noexcept
: m_begin(std::forward<Iterator>(begin)),
m_end(std::forward<Iterator>(end)) {}
Iterator begin() const noexcept { return m_begin; }
Iterator end() const noexcept { return m_end; }
private:
const Iterator m_begin;
const Iterator m_end;
};
template <typename Iterator>
static inline Range<Iterator> makeRange(Iterator &&begin,
Iterator &&end) noexcept {
return Range<Iterator>{std::forward<Iterator>(begin),
std::forward<Iterator>(end)};
}
template <typename T>
static inline decltype(auto)
makeReversedRange(const std::initializer_list<T> &iniList) noexcept {
return makeRange(std::rbegin(iniList), std::rend(iniList));
}
template <typename T, typename std::enable_if_t<has_rbegin_rend_v<T>,
std::nullptr_t> = nullptr>
static inline decltype(auto) makeReversedRange(T &&c) noexcept {
return makeRange(std::rbegin(c), std::rend(c));
} /* rbegin(),
rend()を持たないものはこっちに分岐させて,エラーメッセージを少なくする*/
template <typename T, typename std::enable_if<!has_rbegin_rend<T>::value,
std::nullptr_t>::type = nullptr>
static inline void makeReversedRange(T &&) noexcept {
static_assert(has_rbegin_rend<T>::value,
"Specified argument doesn't have reverse iterator.");
}
// #define use_for
#define form3(k, v, st) for (auto &&[k, v] : st)
#define form4(k, v, st, r) \
for (auto &&[k, v] : range(st.begin(), st.lower_bound(r)))
#define form5(k, v, st, l, r) \
for (auto &&[k, v] : range(st.lower_bound(l), st.lower_bound(r)))
#define form(...) over5(__VA_ARGS__, form5, form4, form3)(__VA_ARGS__)
#define forrm1(st) \
for (auto &&forrm_it = st.rbegin(); forrm_it != st.rend(); ++forrm_it)
#define forrm3(k, v, st) \
for (auto &&forrm_it = st.rbegin(); forrm_it != st.rend(); ++forrm_it)
// 向こう側で
// ++itか it = st.erase(it)とする
#define fors1(st) for (auto &&it = st.begin(); it != st.end();)
#define fors2(v, st) for (auto &&it = st.begin(); it != st.end();)
#define fors3(v, st, r) \
for (auto &&it = st.begin(); it != st.end() && (*it) < r;)
#define fors4(v, st, l, r) \
for (auto &&it = st.lower_bound(l); it != st.end() && (*it) < r;)
#ifdef use_for
#define forslr3(st, a, b) \
for (auto &&forslr_it = st.begin(); forslr_it != st.end(); ++forslr_it)
#define forslr4(v, st, a, b) \
for (auto &&forslr_it = st.begin(); forslr_it != st.end(); ++forslr_it)
#define forslr5(v, st, r, a, b) \
for (auto &&forslr_it = st.begin(); \
forslr_it != st.end() && (*forslr_it) < r; ++forslr_it)
#define forslr6(v, st, l, r, a, b) \
for (auto &&forslr_it = st.lower_bound(l); \
forslr_it != st.end() && (*forslr_it) < r; ++forslr_it)
#define fora_f_init_2(a, A) ;
#define fora_f_init_3(fora_f_i, a, A) auto &&a = A[fora_f_i];
#define fora_f_init_4(a, b, A, B) \
auto &&a = A[fora_f_i]; \
auto &&b = B[fora_f_i];
#define fora_f_init_5(fora_f_i, a, b, A, B) \
auto &&a = A[fora_f_i]; \
auto &&b = B[fora_f_i];
#define fora_f_init_6(a, b, c, A, B, C) \
auto &&a = A[fora_f_i]; \
auto &&b = B[fora_f_i]; \
auto &&c = C[fora_f_i];
#define fora_f_init_7(fora_f_i, a, b, c, A, B, C) \
auto &&a = A[fora_f_i]; \
auto &&b = B[fora_f_i]; \
auto &&c = C[fora_f_i];
#define fora_f_init_8(a, b, c, d, A, B, C, D) \
auto &&a = A[fora_f_i]; \
auto &&b = B[fora_f_i]; \
auto &&c = C[fora_f_i]; \
auto &&d = D[fora_f_i];
#define fora_f_init_9(fora_f_i, a, b, c, d, A, B, C, D) \
auto &&a = A[fora_f_i]; \
auto &&b = B[fora_f_i]; \
auto &&c = C[fora_f_i]; \
auto &&d = D[fora_f_i];
#define fora_f_init(...) \
over9(__VA_ARGS__, fora_f_init_9, fora_f_init_8, fora_f_init_7, \
fora_f_init_6, fora_f_init_5, fora_f_init_4, fora_f_init_3, \
fora_f_init_2)(__VA_ARGS__)
#define forr_init_2(a, A) auto &&a = A[forr_i];
#define forr_init_3(forr_i, a, A) auto &&a = A[forr_i];
#define forr_init_4(a, b, A, B) \
auto &&a = A[forr_i]; \
auto &&b = B[forr_i];
#define forr_init_5(forr_i, a, b, A, B) \
auto &&a = A[forr_i]; \
auto &&b = B[forr_i];
#define forr_init_6(a, b, c, A, B, C) \
auto &&a = A[forr_i]; \
auto &&b = B[forr_i]; \
auto &&c = C[forr_i];
#define forr_init_7(forr_i, a, b, c, A, B, C) \
auto &&a = A[forr_i]; \
auto &&b = B[forr_i]; \
auto &&c = C[forr_i];
#define forr_init_8(a, b, c, d, A, B, C, D) \
auto &&a = A[forr_i]; \
auto &&b = B[forr_i]; \
auto &&c = C[forr_i]; \
auto &&d = D[forr_i];
#define forr_init_9(forr_i, a, b, c, d, A, B, C, D) \
auto &&a = A[forr_i]; \
auto &&b = B[forr_i]; \
auto &&c = C[forr_i]; \
auto &&d = D[forr_i];
#define forr_init(...) \
over9(__VA_ARGS__, forr_init_9, forr_init_8, forr_init_7, forr_init_6, \
forr_init_5, forr_init_4, forr_init_3, forr_init_2)(__VA_ARGS__)
#define forp_init3(k, v, S) \
auto &&k = S[forp_i].first; \
auto &&v = S[forp_i].second;
#define forp_init4(forp_i, k, v, S) \
auto &&k = S[forp_i].first; \
auto &&v = S[forp_i].second;
#define forp_init(...) \
over4(__VA_ARGS__, forp_init4, forp_init3, forp_init2, \
forp_init1)(__VA_ARGS__)
#define forrm_init(k, v, ...) \
auto &&k = (*forrm_it).fi; \
auto &&v = (*forrm_it).se;
#define fors_init(v, ...) auto &&v = (*it);
#define forlr_init(a, A, ngl, ngr) \
auto a = A[forlr_i]; \
auto prev = forlr_i ? A[forlr_i - 1] : ngl; \
auto next = forlr_i + 1 < rep2lim ? A[forlr_i + 1] : ngr;
#define forslr_init4(a, A, ngl, ngr) \
auto a = (*forslr_it); \
auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \
auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr;
#define forslr_init5(a, A, r, ngl, ngr) \
auto a = (*forslr_it); \
auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \
auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr;
#define forslr_init6(a, A, l, r, ngl, ngr) \
auto a = (*forslr_it); \
auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \
auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr;
#define forslr_init(...) \
over6(__VA_ARGS__, forslr_init6, forslr_init5, forslr_init4)(__VA_ARGS__);
// こうしないとmapがおかしくなる
#define fora_f_2(a, A) for (auto &&a : A)
#define fora_f_3(fora_f_i, a, A) rep(fora_f_i, sz(A))
#define fora_f_4(a, b, A, B) rep(fora_f_i, sz(A))
#define fora_f_5(fora_f_i, a, b, A, B) rep(fora_f_i, sz(A))
#define fora_f_6(a, b, c, A, B, C) rep(fora_f_i, sz(A))
#define fora_f_7(fora_f_i, a, b, c, A, B, C) rep(fora_f_i, sz(A))
#define fora_f_8(a, b, c, d, A, B, C, D) rep(fora_f_i, sz(A))
#define fora_f_9(fora_f_i, a, b, c, d, A, B, C, D) rep(fora_f_i, sz(A))
#define forr_2(a, A) rer(forr_i, sz(A) - 1)
#define forr_3(forr_i, a, A) rer(forr_i, sz(A) - 1)
#define forr_4(a, b, A, B) rer(forr_i, sz(A) - 1)
#define forr_5(forr_i, a, b, A, B) rer(forr_i, sz(A) - 1)
#define forr_6(a, b, c, A, B, C) rer(forr_i, sz(A) - 1)
#define forr_7(forr_i, a, b, c, A, B, C) rer(forr_i, sz(A) - 1)
#define forr_8(a, b, c, d, A, B, C, D) rer(forr_i, sz(A) - 1)
#define forr_9(forr_i, a, b, c, d, A, B, C, D) rer(forr_i, sz(A) - 1)
#endif
// ↑@オーバーロード隔離
// rep系はインデックス、for系は中身
#define rep(...) over4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rer(...) over4(__VA_ARGS__, rer4, rer3, rer2, )(__VA_ARGS__)
// 自分込みで残りがREM以上の間ループを回す
#define rem(i, N, REM) for (int i = 0; i < N - REM + 1; i++)
// char用のrep
#define repc(i, m, n) for (char i = m, repc3lim = n; i < repc3lim; ++i)
// i,j,k...をnまで見る
#define reps(...) over5(__VA_ARGS__, reps5, reps4, reps3, reps2, )(__VA_ARGS__)
#define repss(...) \
over10(__VA_ARGS__, repss10, a, repss8, a, repss6, a, repss4, a, \
repss2)(__VA_ARGS__)
// vectorのindexを走査する
// repv(i,j,vvi)
#define repv(...) \
over6(__VA_ARGS__, repv_6, repv_5, repv_4, repv_3, repv_2, )(__VA_ARGS__)
#define rerv(i, A) for (int i = sz(A) - 1; i >= 0; i--)
// repvn(dp) nは次元
#define repv1(a) repv(i, a)
#define repv2(a) repv(i, j, a)
#define repv3(a) repv(i, j, k, a)
#define repv4(a) repv(i, j, k, l, a)
#ifdef use_for
#define fora_f(...) \
over9(__VA_ARGS__, fora_f_9, fora_f_8, fora_f_7, fora_f_6, fora_f_5, \
fora_f_4, fora_f_3, fora_f_2)(__VA_ARGS__)
#endif
#define forr(...) \
over9(__VA_ARGS__, forr_9, forr_8, forr_7, forr_6, forr_5, forr_4, forr_3, \
forr_2)(__VA_ARGS__)
// 0~N-2まで見る
#define forar_init(v, rv, A) \
auto &&v = A[forar_i]; \
auto &&rv = A[forar_i + 1];
#define forar(v, rv, A) rep(forar_i, sz(A) - 1)
#if __cplusplus >= 201703L
template <size_t M_SZ, bool indexed, class Iterator, class T, class U = T,
class V = T, class W = T>
class ite_vec_merge : public Iterator {
std::size_t i = 0;
vector<T> &A;
vector<U> &B;
vector<V> &C;
vector<W> &D;
public:
ite_vec_merge(Iterator ita, vector<T> &A)
: Iterator(ita), A(A), B(A), C(A), D(A) {}
ite_vec_merge(Iterator ita, vector<T> &A, vector<U> &B)
: Iterator(ita), A(A), B(B), C(A), D(A) {}
ite_vec_merge(Iterator ita, vector<T> &A, vector<U> &B, vector<V> &C)
: Iterator(ita), A(A), B(B), C(C), D(A) {}
ite_vec_merge(Iterator ita, vector<T> &A, vector<U> &B, vector<V> &C,
vector<W> &D)
: Iterator(ita), A(A), B(B), C(C), D(D) {}
auto &operator++() {
++i;
this->Iterator::operator++();
return *this;
}
auto operator*() const noexcept {
if constexpr (!indexed && M_SZ == 1) {
return tuple<T &>(A[i]);
} else if constexpr (!indexed && M_SZ == 2) {
return tuple<T &, U &>(A[i], B[i]);
} else if constexpr (!indexed && M_SZ == 3) {
return tuple<T &, U &, V &>(A[i], B[i], C[i]);
} else if constexpr (!indexed && M_SZ == 4) {
return tuple<T &, U &, V &, W &>(A[i], B[i], C[i], D[i]);
} else if constexpr (indexed && M_SZ == 1) {
return tuple<int, T &>(i, A[i]);
} else if constexpr (indexed && M_SZ == 2) {
return tuple<int, T &, U &>(i, A[i], B[i]);
} else if constexpr (indexed && M_SZ == 3) {
return tuple<int, T &, U &, V &>(i, A[i], B[i], C[i]);
} else if constexpr (indexed && M_SZ == 4) {
return tuple<int, T &, U &, V &, W &>(i, A[i], B[i], C[i], D[i]);
} else {
assert(0);
return tuple<int>(i);
}
}
};
template <size_t M_SZ, bool indexed, class T, class U = T, class V = T,
class W = T>
class vec_merge {
vector<T> &a;
vector<U> &b;
vector<V> &c;
vector<W> &d;
public:
vec_merge(vector<T> &a) : a(a), b(a), c(a), d(a) {}
vec_merge(vector<T> &a, vector<U> &b) : a(a), b(b), c(a), d(a) {}
vec_merge(vector<T> &a, vector<U> &b, vector<V> &c)
: a(a), b(b), c(c), d(a) {}
vec_merge(vector<T> &a, vector<U> &b, vector<V> &c, vector<W> &d)
: a(a), b(b), c(c), d(d) {}
auto begin() const {
if constexpr (M_SZ == 1) {
return ite_vec_merge<M_SZ, indexed, decltype(std::begin(a)), T, U, V, W>{
std::begin(a), a};
} else if constexpr (M_SZ == 2) {
return ite_vec_merge<M_SZ, indexed, decltype(std::begin(a)), T, U, V, W>{
std::begin(a), a, b};
} else if constexpr (M_SZ == 3) {
return ite_vec_merge<M_SZ, indexed, decltype(std::begin(a)), T, U, V, W>{
std::begin(a), a, b, c};
} else if constexpr (M_SZ == 4) {
return ite_vec_merge<M_SZ, indexed, decltype(std::begin(a)), T, U, V, W>{
std::begin(a), a, b, c, d};
} else {
assert(0);
return ite_vec_merge<M_SZ, indexed, decltype(std::begin(a)), T, U, V, W>{
std::begin(a), a};
}
}
auto end() const {
if constexpr (M_SZ == 1) {
return ite_vec_merge<M_SZ, indexed, decltype(std::end(a)), T, U, V, W>{
std::end(a), a};
} else if constexpr (M_SZ == 2) {
return ite_vec_merge<M_SZ, indexed, decltype(std::end(a)), T, U, V, W>{
std::end(a), a, b};
} else if constexpr (M_SZ == 3) {
return ite_vec_merge<M_SZ, indexed, decltype(std::end(a)), T, U, V, W>{
std::end(a), a, b, c};
} else if constexpr (M_SZ == 4) {
return ite_vec_merge<M_SZ, indexed, decltype(std::end(a)), T, U, V, W>{
std::end(a), a, b, c, d};
} else {
assert(0);
return ite_vec_merge<M_SZ, indexed, decltype(std::end(a)), T, U, V, W>{
std::end(a), a};
}
}
};
#endif
#define fora_2(a, A) for (auto &&a : A)
#if __cplusplus >= 201703L
#define fora_3(i, a, A) \
for (auto [i, a] : vec_merge<1, true, decl_t<decltype(A)>>(A))
#define fora_4(a, b, A, B) \
for (auto [a, b] : \
vec_merge<2, false, decl_t<decltype(A)>, decl_t<decltype(B)>>(A, B))
#define fora_5(i, a, b, A, B) \
for (auto [i, a, b] : \
vec_merge<2, true, decl_t<decltype(A)>, decl_t<decltype(B)>>(A, B))
#define fora_6(a, b, c, A, B, C) \
for (auto [a, b, c] : \
vec_merge<3, false, decl_t<decltype(A)>, decl_t<decltype(B)>, \
decl_t<decltype(C)>>(A, B, C))
#define fora_7(i, a, b, c, A, B, C) \
for (auto [i, a, b, c] : \
vec_merge<3, true, decl_t<decltype(A)>, decl_t<decltype(B)>, \
decl_t<decltype(C)>>(A, B, C))
#define fora_8(a, b, c, d, A, B, C, D) \
for (auto [a, b, c, d] : \
vec_merge<4, false, decl_t<decltype(A)>, decl_t<decltype(B)>, \
decl_t<decltype(C)>, decl_t<decltype(D)>>(A, B, C, D))
#define fora_9(i, a, b, c, d, A, B, C, D) \
for (auto [i, a, b, c, d] : \
vec_merge<4, true, decl_t<decltype(A)>, decl_t<decltype(B)>, \
decl_t<decltype(C)>, decl_t<decltype(D)>>(A, B, C, D))
#endif
// 構造化束縛ver
// 1e5要素で40ms程度
// 遅いときはfora_fを使う
#define fora(...) \
over9(__VA_ARGS__, fora_9, fora_8, fora_7, fora_6, fora_5, fora_4, fora_3, \
fora_2)(__VA_ARGS__)
// #define forr(v, a) for(auto&& v : makeReversedRange(a))
// 参照を取らない
/*@formatter:off*/
#ifdef use_for
template <class U> vector<U> to1d(vector<U> &a) { return a; }
template <class U> auto to1d(vector<vector<U>> &a) {
vector<U> res;
for (auto &&a1 : a)
for (auto &&a2 : a1)
res.push_back(a2);
return res;
}
template <class U> vector<U> to1d(vector<vector<vector<U>>> &a) {
vector<U> res;
for (auto &&a1 : a)
for (auto &&a2 : a1)
for (auto &&a3 : a2)
res.push_back(a3);
return res;
}
template <class U> vector<U> to1d(vector<vector<vector<vector<U>>>> &a) {
vector<U> res;
for (auto &&a1 : a)
for (auto &&a2 : a1)
for (auto &&a3 : a2)
for (auto &&a4 : a3)
res.push_back(a4);
return res;
}
template <class U>
vector<U> to1d(vector<vector<vector<vector<vector<U>>>>> &a) {
vector<U> res;
for (auto &&a1 : a)
for (auto &&a2 : a1)
for (auto &&a3 : a2)
for (auto &&a4 : a3)
for (auto &&a5 : a4)
res.push_back(a5);
return res;
}
template <class U>
vector<U> to1d(vector<vector<vector<vector<vector<vector<U>>>>>> &a) {
vector<U> res;
for (auto &&a1 : a)
for (auto &&a2 : a1)
for (auto &&a3 : a2)
for (auto &&a4 : a3)
for (auto &&a5 : a4)
for (auto &&a6 : a5)
res.push_back(a6);
return res;
}
#define forv(a, b) for (auto a : to1d(b))
// インデックスを前後含めて走査
#define ring(i, s, len) \
for (int i = s, prev = (s == 0) ? len - 1 : s - 1, \
next = (s == len - 1) ? 0 : s + 1, cou = 0; \
cou < len; \
cou++, prev = i, i = next, next = (next == len - 1) ? 0 : next + 1)
// 値と前後を見る
#define ringv(v, d) \
index_ = 0; \
for (auto prev = d[sz(d) - 1], next = (int)d.size() > 1 ? d[1] : d[0], \
v = d[0]; \
index_ < sz(d); index_++, prev = v, v = next, \
next = (index_ >= sz(d) - 1 ? d[0] : d[index_ + 1]))
// 左右をnext prevで見る 0の左と nの右
#define forlr(v, d, banpei_l, banpei_r) rep(forlr_i, sz(d))
#endif
#define forrm(...) \
over5(__VA_ARGS__, forrm5, forrm4, forrm3, forrm2, forrm1)(__VA_ARGS__)
#define fors(...) over4(__VA_ARGS__, fors4, fors3, fors2, fors1)(__VA_ARGS__)
#define forslr(...) \
over6(__VA_ARGS__, forslr6, forslr5, forslr4, forslr3)(__VA_ARGS__)
#define forp3(k, v, st) rep(forp_i, sz(st))
#define forp4(forp_i, k, v, st) rep(forp_i, sz(st))
#define forp(...) over4(__VA_ARGS__, forp4, forp3)(__VA_ARGS__)
// to_vec(rep(i, N))のように使い
// iが走査した値を持つvectorを返す
#define to_vec2(type, my_for) \
[&]() { \
vector<type> ret; \
my_for { ret.push_back(i); } \
return ret; \
}()
#define to_vec1(my_for) to_vec2(int, my_for)
#define to_vec(...) over2(__VA_ARGS__, to_vec2, to_vec1)(__VA_ARGS__)
// マクロ 定数
#define k3 1010
#define k4 10101
#define k5 101010
#define k6 1010101
#define k7 10101010
const double PI = 3.1415926535897932384626433832795029L;
constexpr bool ev(ll a) { return !(a & 1); }
constexpr bool od(ll a) { return (a & 1); }
//@拡張系 こう出来るべきというもの
// 埋め込み 存在を意識せずに機能を増やされているもの
namespace std {
template <> class hash<std::pair<signed, signed>> {
public:
size_t operator()(const std::pair<signed, signed> &x) const {
return hash<ll>()(((ll)x.first << 32) | x.second);
}
};
template <> class hash<std::pair<ll, ll>> {
public
: /*大きいllが渡されると、<<32でオーバーフローするがとりあえず問題ないと判断*/
size_t operator()(const std::pair<ll, ll> &x) const {
return hash<ll>()(((ll)x.first << 32) | x.second);
}
};
template <> class hash<std::tuple<ll, ll, ll>> {
public:
size_t operator()(const std::tuple<ll, ll, ll> &x_) const {
static xorshift xor_s;
return xor_s(x_);
}
};
template <> class hash<std::tuple<ll, ll, ll, ll>> {
public:
size_t operator()(const std::tuple<ll, ll, ll, ll> &x_) const {
static xorshift xor_s;
return xor_s(x_);
}
};
template <class T> class hash<std::vector<T>> {
public:
size_t operator()(const std::vector<T> &x_) const {
static xorshift xor_s;
return xor_s(x_);
}
};
} // namespace std
// stream まとめ
/*@formatter:off*/
istream &operator>>(istream &iss, P &a) {
iss >> a.first >> a.second;
return iss;
}
template <typename T> istream &operator>>(istream &iss, vector<T> &vec_) {
for (T &x : vec_)
iss >> x;
return iss;
}
template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) {
os << p.fi << " " << p.se;
return os;
}
ostream &operator<<(ostream &os, T p) {
os << get<0>(p) << " " << get<1>(p) << " " << get<2>(p);
return os;
}
ostream &operator<<(ostream &os, F p) {
os << get<0>(p) << " " << get<1>(p) << " " << get<2>(p) << " " << get<3>(p);
return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec_) {
for (ll i = 0; i < vec_.size(); ++i)
os << vec_[i] << (i + 1 == vec_.size() ? "" : " ");
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<vector<T>> &vec_) {
for (ll i = 0; i < vec_.size(); ++i) {
for (ll j = 0; j < vec_[i].size(); ++j) {
os << vec_[i][j] << " ";
}
os << endl;
}
return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &m) {
os << endl;
for (auto &&v : m)
os << v << endl;
return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const unordered_map<T, U> &m) {
os << endl;
for (auto &&v : m)
os << v << endl;
return os;
}
template <typename T, typename U> string deb_tos(const unordered_map<T, U> &m) {
map<T, U> a;
for (auto &&v : m) {
a[v.first] = v.second;
}
stringstream ss;
ss << a;
return ss.str();
}
template <class T> ostream &operator<<(ostream &os, const set<T> &s) {
fora(v, s) { os << v << " "; }
return os;
}
template <class T> ostream &operator<<(ostream &os, const mset<T> &s) {
fora(v, s) { os << v << " "; }
return os;
}
template <class T> ostream &operator<<(ostream &os, const deque<T> &a) {
fora(v, a) { os << v << " "; }
return os;
}
ostream &operator<<(ostream &os, const vector<vector<char>> &vec_) {
rep(h, sz(vec_)) {
rep(w, sz(vec_[0])) { os << vec_[h][w]; }
os << endl;
}
return os;
}
ostream &operator<<(ostream &os,
const vector<vector<my_optional<char>>> &vec_) {
rep(h, sz(vec_)) {
rep(w, sz(vec_[0])) { os << vec_[h][w]; }
os << endl;
}
return os;
}
// tuple "," 区切り
#if __cplusplus >= 201703L
template <class Tuple, size_t... Indicies>
void helper_os_tuple(ostream &os, const Tuple &tup,
index_sequence<Indicies...>) {
stringstream ss;
auto f = [&](auto a) { ss << ", " << a; };
(..., f(get<Indicies>(tup)));
os << ss.str().substr(2);
}
template <class... T> ostream &operator<<(ostream &os, const tuple<T...> &tup) {
helper_os_tuple(os, tup, index_sequence_for<T...>{});
return os;
}
#endif
template <class T> struct range_now {
int l;
vector<T> A;
range_now(vector<T> &&A, int l) : A(A), l(l) {}
};
/*@formatter:off*/
// template<class T,class U>ostream &operator<<(ostream &os, vector<pair<T,U>>&
// a) {fora_f(v,a)os<<v<<endl;return os;}
template <typename W, typename H> void resize(W &vec_, const H head) {
vec_.resize(head);
}
template <typename W, typename H, typename... T>
void resize(W &vec_, const H &head, const T... tail) {
vec_.resize(head);
for (auto &v : vec_)
resize(v, tail...);
}
// #define use_for_each //_each _all_of _any_of _none_of _find_if _rfind_if
// _contains _count_if _erase_if _entry_if
#ifdef use_for_each
// todo Atcoderの過去問がc++17に対応したら
// for_each以外はconst & (呼び出し側のラムダも)
template <typename T, typename F> bool all_of2(const T &v, F f) {
if constexpr (has_value_type<T>::value) {
for (auto &&v_ : v) {
if (!all_of2(v_, f))
return false;
}
return true;
} else {
return f(v);
}
}
template <typename T, typename F> bool any_of2(const T &v, F f) {
if constexpr (has_value_type<T>::value) {
for (auto &&v_ : v) {
if (any_of2(v_, f))
return true;
}
return false;
} else {
return f(v);
}
}
template <typename T, typename F> bool none_of2(const T &v, F f) {
return all_of2(v, [&](auto a) { return !f(a); });
}
// 存在しない場合
// 1次元 Nを返す
// 多次元(-1,-1,..)を返す
template <typename T, typename F, require_t(has_value_type<T>::value)>
ll find_if2(const T &v, F f) {
rep(i, sz(v)) {
if (f(v[i]))
return i;
}
return sz(v);
}
template <typename T, typename F>
tuple<int, int> find_if2(const vector<vector<T>> &v, F f) {
rep(i, sz(v)) {
rep(j, sz(v[i])) {
if (f(v[i][j])) {
return tuple<int, int>(i, j);
}
}
}
return tuple<int, int>(-1, -1);
}
template <typename T, typename F>
auto find_if2(const vector<vector<vector<T>>> &v, F f) {
rep(i, sz(v)) {
if (auto ret = find_if2(v[i], f); get<0>(ret) != -1) {
return tuple_cat(tuple<int>(i), ret);
}
}
auto bad = tuple_cat(tuple<int>(-1), find_if2(v[0], f));
return bad;
}
template <class T, class F> auto find_if2(const range_now<T> &v, F f) {
return find_if2(v.A, f) + v.l;
}
// 存在しない場合
// 1次元 -1を返す
// 多次元-1を返す
template <typename T, typename F, require_t(has_value_type<T>::value)>
ll rfind_if2(const T &v, F f) {
rer(i, sz(v) - 1) {
if (f(v[i]))
return i;
}
return -1;
}
template <typename T, typename F>
tuple<int, int> rfind_if2(const vector<vector<T>> &v, F f) {
rer(i, sz(v) - 1) {
rer(j, sz(v[i]) - 1) {
if (f(v[i][j])) {
return tuple<int, int>(i, j);
}
}
}
return tuple<int, int>(-1, -1);
}
template <typename T, typename F>
auto rfind_if2(const vector<vector<vector<T>>> &v, F f) {
rer(i, sz(v) - 1) {
if (auto ret = rfind_if2(v[i], f); get<0>(ret) != -1) {
return tuple_cat(tuple<int>(i), ret);
}
}
auto bad = tuple_cat(tuple<int>(-1), rfind_if2(v[0], f));
return bad;
}
// todo まとめられそう string,vector全般
template <class T> bool contains(const string &s, const T &v) {
return s.find(v) != string::npos;
}
template <typename T> bool contains(const vector<T> &v, const T &val) {
return std::find(v.begin(), v.end(), val) != v.end();
}
template <typename T, typename F> bool contains_if2(const vector<T> &v, F f) {
return find_if(v.begin(), v.end(), f) != v.end();
}
template <typename T, typename F> ll count_if2(const T &v, F f) {
if constexpr (has_value_type<T>::value) {
ll ret = 0;
for (auto &&v_ : v) {
ret += count_if2(v_, f);
}
return ret;
} else {
return f(v);
}
}
template <typename T, typename F> void for_each2(T &a, F f) {
if constexpr (has_value_type<T>::value) {
for (auto &&v_ : a)
for_each2(v_, f);
} else {
f(a);
}
}
template <typename W> ll count_od(const vector<W> &a) {
return count_if2(a, [](ll v) { return v & 1; });
}
template <typename W> ll count_ev(const vector<W> &a) {
return count_if2(a, [](ll v) { return !(v & 1); });
}
// 削除した後のvectorを返す
template <typename T, typename F> vector<T> erase_if2(const vector<T> &v, F f) {
vector<T> nv;
rep(i, sz(v)) {
if (!f(v[i])) {
nv.push_back(v[i]);
}
}
return nv;
}
template <typename T, typename F>
vector<vector<T>> erase_if2(const vector<vector<T>> &v, F f) {
vector<vector<T>> res;
rep(i, sz(v)) { res[i] = erase_if2(v[i], f); }
return res;
}
template <typename T, typename F> vector<T> entry_if2(const vector<T> &v, F f) {
vector<T> nv;
rep(i, sz(v)) {
if (f(v[i])) {
nv.push_back(v[i]);
}
}
return nv;
}
template <typename T, typename F>
vector<vector<T>> entry_if2(const vector<vector<T>> &v, F f) {
vector<vector<T>> res;
rep(i, sz(v)) { res[i] = entry_if2(v[i], f); }
return res;
}
template <typename T, typename F> ll l_rfind_if(const vector<T> &v, F f) {
rer(i, sz(v) - 1) {
if (f(v[i]))
return i;
}
return -1;
}
template <typename T, typename F> bool l_contains_if(const vector<T> &v, F f) {
rer(i, sz(v) - 1) {
if (f(v[i]))
return true;
}
return false;
}
template <class A, class B, class C> auto t_all_of(A a, B b, C c) {
return std::all_of(a, b, c);
}
template <class A, class B, class C> auto t_any_of(A a, B b, C c) {
return std::any_of(a, b, c);
}
template <class A, class B, class C> auto t_none_of(A a, B b, C c) {
return std::none_of(a, b, c);
}
template <class A, class B, class C> auto t_find_if(A a, B b, C c) {
return std::find_if(a, b, c);
}
template <class A, class B, class C> auto t_count_if(A a, B b, C c) {
return std::count_if(a, b, c);
}
#define all_of_s__2(a, right) (t_all_of(ALL(a), lamr(right)))
#define all_of_s__3(a, v, siki) (t_all_of(ALL(a), [&](auto v) { return siki; }))
#define all_of_s(...) over3(__VA_ARGS__, all_of_s__3, all_of_s__2)(__VA_ARGS__)
// all_of(A, %2);
// all_of(A, a, a%2);
#define all_of__2(a, right) all_of2(a, lamr(right))
#define all_of__3(a, v, siki) all_of2(a, [&](auto v) { return siki; })
#define all_of(...) over3(__VA_ARGS__, all_of__3, all_of__2)(__VA_ARGS__)
#define all_of_f(a, f) all_of2(a, f)
#define any_of_s__2(a, right) (t_any_of(ALL(a), lamr(right)))
#define any_of_s__3(a, v, siki) (t_any_of(ALL(a), [&](auto v) { return siki; }))
#define any_of_s(...) over3(__VA_ARGS__, any_of_s__3, any_of_s__2)(__VA_ARGS__)
#define any_of__2(a, right) any_of2(a, lamr(right))
#define any_of__3(a, v, siki) any_of2(a, [&](auto v) { return siki; })
#define any_of(...) over3(__VA_ARGS__, any_of__3, any_of__2)(__VA_ARGS__)
#define any_of_f(a, f) any_of2(a, f)
#define none_of_s__2(a, right) (t_none_of(ALL(a), lamr(right)))
#define none_of_s__3(a, v, siki) \
(t_none_of(ALL(a), [&](auto v) { return siki; }))
#define none_of_s(...) \
over3(__VA_ARGS__, none_of_s__3, none_of_s__2)(__VA_ARGS__)
#define none_of__2(a, right) none_of2(a, lamr(right))
#define none_of__3(a, v, siki) none_of2(a, [&](auto v) { return siki; })
#define none_of(...) over3(__VA_ARGS__, none_of__3, none_of__2)(__VA_ARGS__)
#define none_of_f(a, f) none_of2(a, f)
#define find_if_s__2(a, right) (t_find_if(ALL(a), lamr(right)) - a.begin())
#define find_if_s__3(a, v, siki) \
(t_find_if(ALL(a), [&](auto v) { return siki; }) - a.begin())
#define find_if_s(...) \
over3(__VA_ARGS__, find_if_s__3, find_if_s__2)(__VA_ARGS__)
#define find_if__2(a, right) find_if2(a, lamr(right))
#define find_if__3(a, v, siki) find_if2(a, [&](auto v) { return siki; })
#define find_if__4(a, l, v, siki) \
(find_if2(decltype(a)(a.begin() + l, a.end()), \
[&](auto v) { return siki; }) + \
l)
#define find_if(...) \
over4(__VA_ARGS__, find_if__4, find_if__3, find_if__2)(__VA_ARGS__)
#define find_if_f(a, f) find_if2(a, f)
#define rfind_if_s__2(a, right) l_rfind_if(a, lamr(right))
#define rfind_if_s__3(a, v, siki) l_rfind_if(a, [&](auto v) { return siki; })
#define rfind_if_s(...) \
over3(__VA_ARGS__, rfind_if_s__3, rfind_if_s__2)(__VA_ARGS__)
#define rfind_if__2(a, right) rfind_if2(a, lamr(right))
#define rfind_if__3(a, v, siki) rfind_if2(a, [&](auto v) { return siki; })
#define rfind_if(...) over3(__VA_ARGS__, rfind_if__3, rfind_if__2)(__VA_ARGS__)
#define rfind_if_f(a, f) rfind_if2(a, f)
#define contains_if_s__2(a, right) l_contains_if(a, lamr(right))
#define contains_if_s__3(a, v, siki) \
l_contains_if(a, [&](auto v) { return siki; })
#define contains_if_s(...) \
over3(__VA_ARGS__, contains_if_s__3, contains_if_s__2)(__VA_ARGS__)
#define contains_if__2(a, right) contains_if2(a, lamr(right))
#define contains_if__3(a, v, siki) contains_if2(a, [&](auto v) { return siki; })
#define contains_if(...) \
over3(__VA_ARGS__, contains_if__3, contains_if__2)(__VA_ARGS__)
#define contains_if_f(a, f) contains_if2(a, f)
#define count_if_s__2(a, right) (t_count_if(ALL(a), lamr(right)))
#define count_if_s__3(a, v, siki) \
(t_count_if(ALL(a), [&](auto v) { return siki; }))
#define count_if_s(...) \
over3(__VA_ARGS__, count_if_s__3, count_if_s__2)(__VA_ARGS__)
#define count_if__2(a, right) count_if2(a, lamr(right))
#define count_if__3(a, v, siki) count_if2(a, [&](auto v) { return siki; })
#define count_if(...) over3(__VA_ARGS__, count_if__3, count_if__2)(__VA_ARGS__)
#define count_if_f(a, f) count_if2(a, f)
// vector<vi>で、viに対して操作
#define for_each_s__2(a, right) \
do { \
fora(v, a) { v right; } \
} while (0)
#define for_each_s__3(a, v, shori) \
do { \
fora(v, a) { shori; } \
} while (0)
#define for_each_s(...) \
over3(__VA_ARGS__, for_each_s__3, for_each_s__2)(__VA_ARGS__)
// vector<vi>で、intに対して操作
#define for_each__2(a, right) for_each2(a, lamr(right))
#define for_each__3(a, v, shori) for_each2(a, [&](auto &v) { shori; })
#define for_each(...) over3(__VA_ARGS__, for_each__3, for_each__2)(__VA_ARGS__)
#define for_each_f(a, f) for_each2(a, f);
template <class T, class F> vector<T> help_for_eached(const vector<T> &A, F f) {
vector<T> ret = A;
for_each(ret, v, f(v));
return ret;
}
#define for_eached__2(a, right) help_for_eached(a, lamr(right))
#define for_eached__3(a, v, shori) help_for_eached(a, lam(v, shori))
#define for_eached(...) \
over3(__VA_ARGS__, for_eached__3, for_eached__2)(__VA_ARGS__)
#define for_eached_f(a, f) for_eached2(a, f);
#define each for_each
#define eached for_eached
// #define erase_if_s__2(a, right) l_erase_if2(a,lamr(right))
// #define erase_if_s__3(a, v, siki) l_erase_if2(a,[&](auto v){return siki;})
// #define erase_if_s(...)
// over3(__VA_ARGS__,erase_if_s__3,erase_if_s__2)(__VA_ARGS__)
#define erase_if__2(a, right) erase_if2(a, lamr(right))
#define erase_if__3(a, v, siki) erase_if2(a, [&](auto v) { return siki; })
#define erase_if(...) over3(__VA_ARGS__, erase_if__3, erase_if__2)(__VA_ARGS__)
#define erase_if_f(a, f) erase_if2(a, f)
// #define entry_if_s__2(a, right) l_entry_if2(a,lamr(right))
// #define entry_if_s__3(a, v, siki) l_entry_if2(a,[&](auto v){return siki;})
// #define entry_if_s(...)
// over3(__VA_ARGS__,entry_if_s__3,entry_if_s__2)(__VA_ARGS__)
#define entry_if__2(a, right) entry_if2(a, lamr(right))
#define entry_if__3(a, v, siki) entry_if2(a, [&](auto v) { return siki; })
#define entry_if(...) over3(__VA_ARGS__, entry_if__3, entry_if__2)(__VA_ARGS__)
#define entry_if_f(a, f) entry_if2(a, f)
#endif
// 機能削除 todo
// string.replace()は長さを指定するため、間違えやすい
/*@formatter:off*/
void replace(str &a, char key, char v) { replace(ALL(a), key, v); }
template <class T, class U> void my_replace(T &s, int l, int r, const U &t) {
s.replace(l, r - l, t);
}
#define replace my_replace
template <class T, class U, class W> void replace(vector<W> &a, T key, U v) {
rep(i, sz(a)) if (a[i] == key) a[i] = v;
}
template <class T, class U, class W>
void replace(vector<vector<W>> &A, T key, U v) {
rep(i, sz(A)) replace(A[i], key, v);
}
void replace(str &a, char key, str v) {
if (v == "")
a.erase(remove(ALL(a), key), a.end());
}
// keyと同じかどうか01で置き換える
template <class T, class U> void replace(vector<T> &a, U k) {
rep(i, sz(a)) a[i] = a[i] == k;
}
template <class T, class U> void replace(vector<vector<T>> &a, U k) {
rep(i, sz(a)) rep(j, sz(a[0])) a[i][j] = a[i][j] == k;
}
void replace(str &a) {
int dec = 0;
if ('a' <= a[0] && a[0] <= 'z')
dec = 'a';
if ('A' <= a[0] && a[0] <= 'Z')
dec = 'A';
fora(v, a) { v -= dec; }
}
void replace(string &a, char key) {
string res;
for (auto c : a) {
if (c != key)
res.push_back(c);
}
swap(a, res);
}
void replace(str &a, str key, str v) {
stringstream t;
ll kn = sz(key);
std::string::size_type Pos(a.find(key));
ll l = 0;
while (Pos != std::string::npos) {
t << a.substr(l, Pos - l);
t << v;
l = Pos + kn;
Pos = a.find(key, Pos + kn);
}
t << a.substr(l, sz(a) - l);
a = t.str();
}
template <class T> bool is_permutation(vector<T> &a, vector<T> &b) {
return is_permutation(ALL(a), ALL(b));
}
template <class T> bool next_permutation(vector<T> &a) {
return next_permutation(ALL(a));
}
vi iota(ll s, ll len) {
vi ve(len);
iota(ALL(ve), s);
return ve;
}
//[iterator, iterator)等と渡す
// vectorに変換もできる
#if __cplusplus >= 201703L
template <class I, class J> struct body_range {
I itl;
J itr;
body_range(I it, J end) : itl(it), itr(end) {}
I begin() { return itl; }
I end() { return itr; }
// 毎回コピーする
operator vector<typename I::value_type>() {
return vector<typename I::value_type>(itl, itr);
}
};
template <class I, class J, require_t(is_integral_v<I>)>
vector<int> range(I l, J r) {
return iota(l, r - l);
}
template <class I, require_t(is_integral_v<I>)> vector<int> range(I r) {
return iota(0, r);
}
template <class I, require_t(!is_integral_v<I>)> auto range(I itl, I itr) {
return body_range(itl, itr);
}
#endif
/*@formatter:off*/
// tuple<vecs...> = take_vt(vt)
#if __cplusplus >= 201703L
template <class Tup_l, class Tup_r, size_t... Indicies>
auto helper_take_vector_tuple(Tup_l &res, const Tup_r &rhs,
index_sequence<Indicies...>) {
(..., get<Indicies>(res).emplace_back(get<Indicies>(rhs)));
}
template <class... T> auto take_vector_tuple(const vector<tuple<T...>> &elems) {
tuple<vector<T>...> res;
for (auto &&elem : elems) {
helper_take_vector_tuple(res, elem, index_sequence_for<T...>{});
}
return res;
}
// tie_vt(A,B,C) = vt;
template <class... T> struct tie_vector_tuple {
tuple<vector<T> &...> v;
tie_vector_tuple(vector<T> &...args) : v(args...) {}
template <class Tup, size_t... Indicies>
void helper_emplace(int i, Tup &rhs, index_sequence<Indicies...>) {
(..., (get<Indicies>(v).emplace_back(get<Indicies>(rhs))));
}
template <size_t... Indicies> void clear(index_sequence<Indicies...>) {
(..., (get<Indicies>(v).clear()));
}
void operator=(vector<tuple<T...>> &rhs) {
clear(index_sequence_for<T...>{});
rep(i, sz(rhs)) { helper_emplace(i, rhs[i], index_sequence_for<T...>{}); }
}
};
/*@formatter:off*/
// vt = make_vt(A, B, C)
template <class H, class... T>
auto make_vector_tuple(const vector<H> &head, const vector<T> &...tails) {
vector<tuple<H, T...>> res;
rep(i, sz(head)) { res.emplace_back(head[i], tails[i]...); }
return res;
}
#endif
/*@formatter:off*/
#define mvt make_vector_tuple
#define tie_vt tie_vector_tuple
#define make_vt make_vector_tuple
#define take_vt take_vector_tuple
/*@formatter:off*/
// 基本ソート@0
template <class T> void sort(vector<T> &a, int l = -1, int r = -1) {
set_lr12(l, r, sz(a));
fast_sort(a.begin() + l, a.begin() + r);
}
template <class T> void rsort(vector<T> &a, int l = -1, int r = -1) {
set_lr12(l, r, sz(a));
fast_sort(a.begin() + l, a.begin() + r, greater<T>());
};
// f(tup)の大きさでソート
template <class U, class F> void sort(vector<U> &a, F f) {
fast_sort(ALL(a), [&](U l, U r) { return f(l) < f(r); });
};
template <class U, class F> void rsort(vector<U> &a, F f) {
fast_sort(ALL(a), [&](U l, U r) { return f(l) > f(r); });
};
enum tuple_comparator { /*tcom_less = 1728, tcom_greater = 1729, */ fisi = 0,
fisd,
fdsi,
fdsd,
sifi,
sifd,
sdfi,
sdfd,
fisiti,
fisitd,
fisdti,
fisdtd,
fdsiti,
fdsitd,
fdsdti,
fdsdtd,
fitisi,
fitisd,
fitdsi,
fitdsd,
fdtisi,
fdtisd,
fdtdsi,
fdtdsd,
sifiti,
sifitd,
sifdti,
sifdtd,
sdfiti,
sdfitd,
sdfdti,
sdfdtd,
sitifi,
sitifd,
sitdfi,
sitdfd,
sdtifi,
sdtifd,
sdtdfi,
sdfdfd,
tifisi,
tifisd,
tifdsi,
tifdsd,
tdfisi,
tdfisd,
tdfdsi,
tdfdsd,
tisifi,
tisifd,
tisdfi,
tisdfd,
tdsifi,
tdsifd,
tdsdfi,
tdsdfd };
// todo 短くする
#define set_4(i0, i1, o0, o1) \
[&](U l, U r) { \
return get<i0>(l) != get<i0>(r) ? get<i0>(l) o0 get<i0>(r) \
: get<i1>(l) o1 get<i1>(r); \
}
#define set_6(i0, i1, i2, o0, o1, o2) \
[&](U l, U r) { \
return get<i0>(l) != get<i0>(r) ? get<i0>(l) o0 get<i0>(r) \
: get<i1>(l) != get<i1>(r) ? get<i1>(l) o1 get<i1>(r) \
: get<i2>(l) o2 get<i2>(r); \
}
// functionを返すため少し遅い todo
#if __cplusplus >= 201703L
template <class... T, class U = tuple<T...>>
function<bool(U, U)> get_function2(const vector<tuple<T...>> &a,
tuple_comparator Compare) {
if constexpr (sizeof...(T) >= 2) {
int type = Compare;
if (type == 0)
return set_4(0, 1, <, <);
if (type == 1)
return set_4(0, 1, <, >);
if (type == 2)
return set_4(0, 1, >, <);
if (type == 3)
return set_4(0, 1, >, >);
if (type == 4)
return set_4(1, 0, <, <);
if (type == 5)
return set_4(1, 0, <, >);
if (type == 6)
return set_4(1, 0, >, <);
if (type == 7)
return set_4(1, 0, >, >);
}
if constexpr (sizeof...(T) >= 3) {
int type = Compare - 8;
if (type == 0)
return set_6(0, 1, 2, <, <, <);
else if (type == 1)
return set_6(0, 1, 2, <, <, >);
else if (type == 2)
return set_6(0, 1, 2, <, >, <);
else if (type == 3)
return set_6(0, 1, 2, <, >, >);
else if (type == 4)
return set_6(0, 1, 2, >, <, <);
else if (type == 5)
return set_6(0, 1, 2, >, <, >);
else if (type == 6)
return set_6(0, 1, 2, >, >, <);
else if (type == 7)
return set_6(0, 1, 2, >, >, >);
else if (type == 8)
return set_6(0, 2, 1, <, <, <);
else if (type == 9)
return set_6(0, 2, 1, <, <, >);
else if (type == 10)
return set_6(0, 2, 1, <, >, <);
else if (type == 11)
return set_6(0, 2, 1, <, >, >);
else if (type == 12)
return set_6(0, 2, 1, >, <, <);
else if (type == 13)
return set_6(0, 2, 1, >, <, >);
else if (type == 14)
return set_6(0, 2, 1, >, >, <);
else if (type == 15)
return set_6(0, 2, 1, >, >, >);
else if (type == 16)
return set_6(1, 0, 2, <, <, <);
else if (type == 17)
return set_6(1, 0, 2, <, <, >);
else if (type == 18)
return set_6(1, 0, 2, <, >, <);
else if (type == 19)
return set_6(1, 0, 2, <, >, >);
else if (type == 20)
return set_6(1, 0, 2, >, <, <);
else if (type == 21)
return set_6(1, 0, 2, >, <, >);
else if (type == 22)
return set_6(1, 0, 2, >, >, <);
else if (type == 23)
return set_6(1, 0, 2, >, >, >);
else if (type == 24)
return set_6(1, 2, 0, <, <, <);
else if (type == 25)
return set_6(1, 2, 0, <, <, >);
else if (type == 26)
return set_6(1, 2, 0, <, >, <);
else if (type == 27)
return set_6(1, 2, 0, <, >, >);
else if (type == 28)
return set_6(1, 2, 0, >, <, <);
else if (type == 29)
return set_6(1, 2, 0, >, <, >);
else if (type == 30)
return set_6(1, 2, 0, >, >, <);
else if (type == 31)
return set_6(1, 2, 0, >, >, >);
else if (type == 32)
return set_6(2, 0, 1, <, <, <);
else if (type == 33)
return set_6(2, 0, 1, <, <, >);
else if (type == 34)
return set_6(2, 0, 1, <, >, <);
else if (type == 35)
return set_6(2, 0, 1, <, >, >);
else if (type == 36)
return set_6(2, 0, 1, >, <, <);
else if (type == 37)
return set_6(2, 0, 1, >, <, >);
else if (type == 38)
return set_6(2, 0, 1, >, >, <);
else if (type == 39)
return set_6(2, 0, 1, >, >, >);
else if (type == 40)
return set_6(2, 1, 0, <, <, <);
else if (type == 41)
return set_6(2, 1, 0, <, <, >);
else if (type == 42)
return set_6(2, 1, 0, <, >, <);
else if (type == 43)
return set_6(2, 1, 0, <, >, >);
else if (type == 44)
return set_6(2, 1, 0, >, <, <);
else if (type == 45)
return set_6(2, 1, 0, >, <, >);
else if (type == 46)
return set_6(2, 1, 0, >, >, <);
else if (type == 47)
return set_6(2, 1, 0, >, >, >);
}
return [&](U l, U r) { return true; };
}
template <class... T>
void sort(vector<tuple<T...>> &a, tuple_comparator Compare) {
auto f = get_function2(a, Compare);
fast_sort(ALL(a), f);
}
#endif
/*@formatter:off*/
#undef set_4
#undef set_6
#if __cplusplus >= 201703L
// sort_tuple
template <class H1, class H2, class... T>
void sortt(vector<H1> &head1, vector<H2> &head2, vector<T> &...tails) {
auto vec_t = make_vt(head1, head2, tails...);
sort(vec_t);
tie_vt(head1, head2, tails...) = vec_t;
}
template <class H1, class H2, class... T>
void rsortt(vector<H1> &head1, vector<H2> &head2, vector<T> &...tails) {
auto vec_t = make_vt(head1, head2, tails...);
rsort(vec_t);
tie_vt(head1, head2, tails...) = vec_t;
}
template <class F, class... T> void helper_sortt_f(F f, vector<T> &...a) {
auto z = make_vt(a...);
sort(z, f);
tie_vt(a...) = z;
}
template <class F, class... T> void helper_rsortt_f(F f, vector<T> &...a) {
auto z = make_vt(a...);
rsort(z, f);
tie_vt(a...) = z;
}
// sortt(a,b,c,d, f)まで対応->
template <class A, class B, class F, require_t(!is_vector<F>::value)>
void sortt(vector<A> &a, vector<B> &b, F f) {
helper_sortt_f(f, a, b);
}
template <class A, class B, class F, require_t(!is_vector<F>::value)>
void rsortt(vector<A> &a, vector<B> &b, F f) {
helper_rsortt_f(f, a, b);
}
template <class A, class B, class C, class F, require_t(!is_vector<F>::value)>
void sortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) {
helper_sortt_f(f, a, b, c);
}
template <class A, class B, class C, class F, require_t(!is_vector<F>::value)>
void rsortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) {
helper_rsortt_f(f, a, b, c);
}
template <class A, class B, class C, class D, class F,
require_t(!is_vector<F>::value)>
void sortt(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d, F f) {
helper_sortt_f(f, a, b, c, d);
}
template <class A, class B, class C, class D, class F,
require_t(!is_vector<F>::value)>
void rsortt(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d, F f) {
helper_rsortt_f(f, a, b, c, d);
} // sort_tuple f }
#endif
/*@formatter:off*/
// 任意引数は取らない
template <class T> vi sorti(const vector<T> &a) {
auto b = a;
vi ind = iota(0, sz(b));
sortt(b, ind);
return ind;
}
template <class T> vi rsorti(const vector<T> &a) {
auto b = a;
vi ind = iota(0, sz(b));
rsortt(b, ind);
return ind;
}
#if __cplusplus >= 201703L
template <class T, class F> vi sorti(const vector<T> &a, F f) {
vi ind = iota(0, sz(a));
if constexpr (is_same_v<F, tuple_comparator>) {
auto f2 = get_function2(a, f);
auto g = [&](int i, int j) { return f2(a[i], a[j]); };
fast_sort(ALL(ind), g);
} else {
auto g = [&](int i) { return f(a[i]); };
sort(ind, g);
}
return ind;
}
template <class T, class F, require_t(is_function_v<F>)>
vi rsorti(const vector<T> &a, F f) {
vi ind = iota(0, sz(a));
auto g = [&](int i) { return f(a[i]); };
rsort(ind, g);
return ind;
}
// 任意引数にfを渡したい場合は (make_vt(a, b, c), f)
template <class H, class... Ts> vi sortti(vector<H> head, vector<Ts>... tails) {
vi ind = iota(0, sz(head));
auto vec = make_vt(head, tails..., ind);
sort(vec);
auto rets = take_vt(vec);
return get<sizeof...(tails) + 1>(rets);
}
template <class H, class... Ts>
vi rsortti(vector<H> head, vector<Ts>... tails) {
vi ind = iota(0, sz(head));
auto vec = make_vt(head, tails..., ind);
rsort(vec);
auto rets = take_vt(vec);
return get<sizeof...(tails) + 1>(rets);
}
#endif
/*@formatter:off*/
//@5
void sort(string &a) { sort(ALL(a)); }
void rsort(string &a) { sort(RALL(a)); }
void sort(int &a, int &b) {
if (a > b)
swap(a, b);
}
void sort(int &a, int &b, int &c) {
sort(a, b);
sort(a, c);
sort(b, c);
}
void rsort(int &a, int &b) {
if (a < b)
swap(a, b);
}
void rsort(int &a, int &b, int &c) {
rsort(a, b);
rsort(a, c);
rsort(b, c);
}
template <class... T, class U> auto sorted(U head, T... a) {
sort(head, a...);
return head;
}
template <class... T, class U> auto rsorted(U head, T... a) {
rsort(head, a...);
return head;
}
template <class T> vector<T> merge(const vector<T> &a, const vector<T> &b) {
vector<T> res;
std::merge(ALL(a), ALL(b), back_inserter(res));
return res;
}
// firstの値が同じ場合secondが最小の物を残す
template <class T, class U> void unique_min(vector<pair<T, U>> &a) {
vector<pair<T, U>> res;
if (sz(a))
res.push_back(a[0]);
rep(i, 1, sz(a)) {
if (res.back().fi == a[i].fi) {
chmi(res.back().se, a[i].se);
} else {
res.push_back(a[i]);
}
}
swap(a, res);
}
// firstの値が同じ場合secondが最大の物を残す
template <class T, class U> void unique_max(vector<pair<T, U>> &a) {
vector<pair<T, U>> res;
if (sz(a))
res.push_back(a[0]);
rep(i, 1, sz(a)) {
if (res.back().fi == a[i].fi) {
chma(res.back().se, a[i].se);
} else {
res.push_back(a[i]);
}
}
swap(a, res);
}
template <class T> bool includes(vector<T> &a, vector<T> &b) {
vi c = a;
vi d = b;
sort(c);
sort(d);
return includes(ALL(c), ALL(d));
}
template <class T> bool distinct(const vector<T> &A) {
if ((int)(A).size() == 1)
return true;
if ((int)(A).size() == 2)
return A[0] != A[1];
if ((int)(A).size() == 3)
return (A[0] != A[1] && A[1] != A[2] && A[0] != A[2]);
auto B = A;
sort(B);
int N = (B.size());
unique(B);
return N == (int)(B.size());
}
template <class H, class... T> bool distinct(const H &a, const T &...b) {
return distinct(vector<H>{a, b...});
}
/*@formatter:off*/
template <class T, class U> void inc(pair<T, U> &a, U v = 1) {
a.first += v, a.second += v;
}
template <class T, class U> void inc(T &a, U v = 1) { a += v; }
template <class T, class U = int> void inc(vector<T> &a, U v = 1) {
for (auto &u : a)
inc(u, v);
}
template <class T, class U> void dec(T &a, U v = 1) { a -= v; }
template <class T, class U = int> void dec(vector<T> &a, U v = 1) {
for (auto &u : a)
dec(u, v);
}
template <class U> void dec(string &a, U v = 1) {
for (auto &u : a)
dec(u, v);
}
template <class T, class U, class W>
void dec(vector<T> &a, vector<U> &b, W v = 1) {
for (auto &u : a)
dec(u, v);
for (auto &u : b)
dec(u, v);
}
template <class T, class U, class W>
void dec(vector<T> &a, vector<U> &b, vector<W> &c) {
for (auto &u : a)
dec(u, 1);
for (auto &u : b)
dec(u, 1);
for (auto &u : c)
dec(u, 1);
}
bool ins(ll h, ll w, ll H, ll W) { return h >= 0 && w >= 0 && h < H && w < W; }
bool san(ll l, ll v, ll r) { return l <= v && v < r; }
template <class T> bool ins(vector<T> &a, ll i, ll j = 0) {
return san(0, i, sz(a)) && san(0, j, sz(a));
}
#define inside ins
ll u0(ll a) { return a < 0 ? 0 : a; }
template <class T> vector<T> u0(vector<T> &a) {
vector<T> ret = a;
fora(v, ret) { v = u(v); }
return ret;
}
// todo 名前
bool d_(int a, int b) {
if (b == 0)
return false;
return (a % b) == 0;
}
// エラー
void ole() {
#ifdef _DEBUG
cerr << "ole" << endl;
exit(0);
#endif
string a = "a";
rep(i, 30) a += a;
rep(i, 1 << 17) cout << a << endl;
cout << "OLE 出力長制限超過" << endl;
exit(0);
}
void re(string s = "") {
cerr << s << endl;
assert(0 == 1);
exit(0);
}
void tle() {
while (inf)
cout << inf << endl;
}
//@汎用便利関数 入力
ll in() {
ll ret;
cin >> ret;
return ret;
}
template <class T, require_t(!has_value_type<T>::value)> T in() {
T ret;
cin >> ret;
return ret;
}
template <class VT, class T = typename VT::value_type> VT in(int len) {
VT ret;
T dat;
for (int i = 0; i < len; i++) {
cin >> dat;
ret += dat;
}
return ret;
}
template <class VT, class T = typename VT::value_type> VT ind(int len) {
VT ret;
T dat;
for (int i = 0; i < len; i++) {
cin >> dat;
ret += (dat - 1);
}
return ret;
}
string sin() {
string ret;
cin >> ret;
return ret;
}
template <class T> void in(T &head) { cin >> head; }
template <class T, class... U> void in(T &head, U &...tail) {
cin >> head;
in(tail...);
}
#define din_t2(type, a) \
type a; \
cin >> a
#define din_t3(type, a, b) \
type a, b; \
cin >> a >> b
#define din_t4(type, a, b, c) \
type a, b, c; \
cin >> a >> b >> c
#define din_t5(type, a, b, c, d) \
type a, b, c, d; \
cin >> a >> b >> c >> d
#define din_t6(type, a, b, c, d, e) \
type a, b, c, d, e; \
cin >> a >> b >> c >> d >> e
#define din_t7(type, a, b, c, d, e, f) \
type a, b, c, d, e, f; \
cin >> a >> b >> c >> d >> e >> f
#define din_t(...) \
over7(__VA_ARGS__, din_t7, din_t6, din_t5, din_t4, din_t3, \
din_t2)(__VA_ARGS__)
#define din(...) din_t(int, __VA_ARGS__)
#define d_in
#define dsig(...) din_t(signed, __VA_ARGS__)
#define dst(...) din_t(string, __VA_ARGS__)
#define dstr dst
#define d_str dst
#define dcha(...) din_t(char, __VA_ARGS__)
#define dchar dcha
#define ddou(...) din_t(double, __VA_ARGS__)
#define din1d(a) \
din_t2(int, a); \
a--
#define din2d(a, b) \
din_t3(int, a, b); \
a--, b--
#define din3d(a, b, c) \
din_t4(int, a, b, c); \
a--, b--, c--
#define din4d(a, b, c, d) \
din_t5(int, a, b, c, d); \
a--, b--, c--, d--
#define dind(...) over4(__VA_ARGS__, din4d, din3d, din2d, din1d)(__VA_ARGS__)
/*@formatter:off*/
#ifdef _DEBUG
template <class T> void err2(T &&head) { cerr << head; }
template <class T, class... U> void err2(T &&head, U &&...tail) {
cerr << head << " ";
err2(tail...);
}
template <class T, class... U> void err(T &&head, U &&...tail) {
cerr << head << " ";
err2(tail...);
cerr << "" << endl;
}
template <class T> void err(T &&head) { cerr << head << endl; }
void err() { cerr << "" << endl; }
// debで出力する最大長
constexpr int DEB_LEN = 20;
constexpr int DEB_LEN_H = 12;
string deb_tos(const int &v) {
if (abs(v) == inf || abs(v) == linf)
return "e";
else
return to_string(v);
}
template <class T> string deb_tos(const T &a) {
stringstream ss;
ss << a;
return ss.str();
}
string deb_tos(const P &p) {
stringstream ss;
ss << "{";
ss << p.fi;
ss << ", ";
ss << p.se;
ss << "}";
return ss.str();
}
#ifdef use_epsdou
string deb_tos(const epsdou &a) { return deb_tos(a.v); }
#endif
template <class T> string deb_tos(const optional<T> &a) {
if (a.has_value()) {
return deb_tos(a.value());
} else
return "e";
}
template <class T> string deb_tos(const vector<T> &a, ll W = inf) {
stringstream ss;
if (W == inf)
W = min(sz(a), DEB_LEN);
if (sz(a) == 0)
return ss.str();
rep(i, W) {
ss << deb_tos(a[i]);
if (typeid(a[i]) == typeid(P)) {
ss << endl;
} else {
ss << " ";
}
}
return ss.str();
}
template <class T>
string deb_tos(const vector<vector<T>> &a, vi H, vi W, int key = -1) {
stringstream ss;
ss << endl;
vi lens(sz(W));
fora(h, H) {
rep(wi, sz(W)) {
if (sz(a[h]) <= W[wi])
break;
lens[wi] = max(lens[wi], sz(deb_tos(a[h][W[wi]])) + 1);
lens[wi] = max(lens[wi], sz(deb_tos(W[wi])) + 1);
}
}
if (key == -1)
ss << " *|";
else
ss << " " << key << "|";
int wi = 0;
fora(w, W) {
ss << std::right << std::setw(lens[wi]) << w;
wi++;
}
ss << "" << endl;
rep(i, sz(W)) rep(lens[i]) ss << "_";
rep(i, 3) ss << "_";
ss << "" << endl;
fora(h, H) {
ss << std::right << std::setw(2) << h << "|";
int wi = 0;
fora(w, W) {
if (sz(a[h]) <= w)
break;
ss << std::right << std::setw(lens[wi]) << deb_tos(a[h][w]);
wi++;
}
ss << "" << endl;
}
return ss.str();
}
template <class T>
string deb_tos(const vector<vector<T>> &a, ll H = inf, ll W = inf,
int key = -1) {
H = (H != inf) ? H : min({H, sz(a), DEB_LEN_H});
W = 0;
rep(h, H) { chma(W, sz(a[h])); }
chmi(W, DEB_LEN_H);
vi hs, ws;
rep(h, H) { hs.push_back(h); }
rep(w, W) { ws.push_back(w); }
return deb_tos(a, hs, ws, key);
}
/*@formatter:off*/
template <class T>
string deb_tos(const vector<vector<vector<T>>> &a, ll H = inf) {
stringstream ss;
if (H == inf)
H = DEB_LEN_H;
H = min(H, sz(a));
rep(i, H) {
ss << endl;
ss << deb_tos(a[i], inf, inf, i);
}
return ss.str();
}
template <class T>
string deb_tos(vector<set<T>> &a, ll H = inf, ll W = inf, int key = -1) {
vector<vector<T>> b(sz(a));
rep(i, sz(a)) {
fora(v, a[i]) { b[i].push_back(v); }
}
return deb_tos(b, H, W, key);
}
template <class T>
string deb_tos(vector<multiset<T>> &a, ll H = inf, ll W = inf, int key = -1) {
vector<vector<T>> b(sz(a));
rep(i, sz(a)) {
fora(v, a[i]) { b[i].push_back(v); }
}
return deb_tos(b, H, W, key);
}
template <class T> string deb_tos(const set<T> &S) {
vector<T> b;
fora(s, S) b += s;
return deb_tos(b);
}
template <class T, size_t A> string deb_tos(T (&a)[A]) {
return deb_tos(vector<T>(begin(a), end(a)));
}
template <class T, size_t A, size_t B> string deb_tos(T (&a)[A][B]) {
return deb_tos(vector<vector<T>>(begin(a), end(a)));
}
template <class T, size_t A, size_t B, size_t C>
string deb_tos(T (&a)[A][B][C]) {
return deb_tos(vector<vector<vector<T>>>(begin(a), end(a)));
}
/*@formatter:off*/
template <class T> void out2(T head) {
cout << head;
res_mes += deb_tos(head);
}
template <class T, class... U> void out2(T head, U... tail) {
cout << head << " ";
res_mes += deb_tos(head) + " ";
out2(tail...);
}
template <class T, class... U> void out(T head, U... tail) {
cout << head << " ";
res_mes += deb_tos(head) + " ";
out2(tail...);
cout << "" << endl;
res_mes += "\n";
}
template <class T> void out(T head) {
cout << head << endl;
res_mes += deb_tos(head) + "\n";
}
void out() { cout << "" << endl; }
#else
#define err(...) ;
template <class T> void out2(T &&head) { cout << head; }
template <class T, class... U> void out2(T &&head, U &&...tail) {
cout << head << " ";
out2(tail...);
}
template <class T, class... U> void out(T &&head, U &&...tail) {
cout << head << " ";
out2(tail...);
cout << "" << endl;
}
template <class T> void out(T &&head) { cout << head << endl; }
void out() { cout << "" << endl; }
#endif
template <class H, class... T>
void outl(const vector<H> &h, const vector<T> &...t) {
rep(i, sz(h)) out(h[i], t[i]...);
}
// テーブルをスペースなしで出力
template <class T> void outt(vector<vector<T>> &a) {
rep(i, sz(a)) {
rep(j, sz(a[i])) { cout << a[i][j]; }
cout << endl;
}
}
// int型をbit表記で出力
void outb(int a) { cout << bitset<20>(a) << endl; }
/*@formatter:off*/
template <class T> void na(vector<T> &a, ll n) {
a.resize(n);
rep(i, n) cin >> a[i];
}
template <class T> void na(set<T> &a, ll n) { rep(i, n) a.insert(in()); }
#define dna(a, n) \
vi a; \
na(a, n); /*nを複数使うと n==in()の時バグる事に注意*/
#define dnad(a, n) \
vi a; \
nad(a, n);
template <class T> void nao(vector<T> &a, ll n) {
a.resize(n + 1);
a[0] = 0;
rep(i, n) cin >> a[i + 1];
}
template <class T> void naod(vector<T> &a, ll n) {
a.resize(n + 1);
a[0] = 0;
rep(i, n) cin >> a[i + 1], a[i + 1]--;
}
template <class T> void nad(vector<T> &a, ll n) {
a.resize(n);
rep(i, n) cin >> a[i], a[i]--;
}
template <class T> void nad(set<T> &a, ll n) { rep(i, n) a.insert(in() - 1); }
template <class T, class U> void na2(vector<T> &a, vector<U> &b, ll n) {
a.resize(n);
b.resize(n);
rep(i, n) cin >> a[i] >> b[i];
}
template <class T, class U> void na2(set<T> &a, set<U> &b, ll n) {
rep(i, n) {
a.insert(in());
b.insert(in());
}
}
#define dna2(a, b, n) \
vi a, b; \
na2(a, b, n);
template <class T, class U> void nao2(vector<T> &a, vector<U> &b, ll n) {
a.resize(n + 1);
b.resize(n + 1);
a[0] = b[0] = 0;
rep(i, n) cin >> a[i + 1] >> b[i + 1];
}
#define dnao2(a, b, n) \
vi a, b; \
nao2(a, b, n);
template <class T, class U> void na2d(vector<T> &a, vector<U> &b, ll n) {
a.resize(n);
b.resize(n);
rep(i, n) cin >> a[i] >> b[i], a[i]--, b[i]--;
}
#define dna2d(a, b, n) \
vi a, b; \
na2d(a, b, n);
template <class T, class U, class W>
void na3(vector<T> &a, vector<U> &b, vector<W> &c, ll n) {
a.resize(n);
b.resize(n);
c.resize(n);
rep(i, n) cin >> a[i] >> b[i] >> c[i];
}
#define dna3(a, b, c, n) \
vi a, b, c; \
na3(a, b, c, n);
template <class T, class U, class W>
void na3d(vector<T> &a, vector<U> &b, vector<W> &c, ll n) {
a.resize(n);
b.resize(n);
c.resize(n);
rep(i, n) cin >> a[i] >> b[i] >> c[i], a[i]--, b[i]--, c[i]--;
}
#define dna3d(a, b, c, n) \
vi a, b, c; \
na3d(a, b, c, n);
template <class T, class U, class W, class X>
void na4(vector<T> &a, vector<U> &b, vector<W> &c, vector<X> &d, ll n) {
a.resize(n);
b.resize(n);
c.resize(n);
d.resize(n);
rep(i, n) cin >> a[i] >> b[i] >> c[i] >> d[i];
}
#define dna4(a, b, c, d, n) \
vi a, b, c, d; \
na4(a, b, c, d, n);
#define dna4d(a, b, c, d, n) \
vi a, b, c, d; \
na4d(a, b, c, d, n);
#define nt(a, h, w) \
resize(a, h, w); \
rep(nthi, h) rep(ntwi, w) cin >> a[nthi][ntwi];
#define ntd(a, h, w) \
resize(a, h, w); \
rep(ntdhi, h) rep(ntdwi, w) cin >> a[ntdhi][ntdwi], a[ntdhi][ntdwi]--;
#define ntp(a, h, w) \
resize(a, h + 2, w + 2); \
fill(a, '#'); \
rep(ntphi, 1, h + 1) rep(ntpwi, 1, w + 1) cin >> a[ntphi][ntpwi];
#define dnt(S, h, w) \
vvi(S, h, w); \
nt(S, h, w);
#define dntc(S, h, w) \
vvc(S, h, w); \
nt(S, h, w);
#define dnts(S, h, w) \
vvs(S, h, w); \
nt(S, h, w);
// デバッグ
#define sp << " " <<
/*@formatter:off*/
#define deb1(x) debugName(x) << " = " << deb_tos(x)
#define deb_2(x, ...) deb1(x) << ", " << deb1(__VA_ARGS__)
#define deb_3(x, ...) deb1(x) << ", " << deb_2(__VA_ARGS__)
#define deb_4(x, ...) deb1(x) << ", " << deb_3(__VA_ARGS__)
#define deb5(x, ...) deb1(x) << ", " << deb_4(__VA_ARGS__)
#define deb6(x, ...) deb1(x) << ", " << deb5(__VA_ARGS__)
// #define deb7(x, ...) deb1(x) <<", "<< deb6(__VA_ARGS__)
// #define deb8(x, ...) deb1(x) <<", "<< deb7(__VA_ARGS__)
// #define deb9(x, ...) deb1(x) <<", "<< deb8(__VA_ARGS__)
// #define deb10(x, ...) deb1(x) <<", "<< deb9(__VA_ARGS__)
/*@formatter:off*/
#ifdef _DEBUG
#define deb(...) \
do { \
if (cou_deb++ >= 1000) \
break; \
was_deb = true; \
cerr << over10(__VA_ARGS__, deb10, deb9, deb8, deb7, deb6, deb5, deb_4, \
deb_3, deb_2, deb1)(__VA_ARGS__) \
<< endl; \
} while (0)
void print_n_base(int x, int base) {
string S;
while (x) {
S += (char)('0' + x % base);
x /= base;
}
cerr << S << endl;
}
template <class T> void print_n_base(vector<T> X, int base) {
cerr << endl;
for (auto &&x : X) {
print_n_base(x, base);
}
cerr << endl;
}
// n進数
#define deb2(x) \
was_deb = true; \
cerr << debugName(x) << " = "; \
print_n_base(x, 2);
#define deb3(x) \
was_deb = true; \
cerr << debugName(x) << " = "; \
print_n_base(x, 3);
#define deb4(x) \
was_deb = true; \
cerr << debugName(x) << " = "; \
print_n_base(x, 4);
#define deb_ex_deb(x, len) debugName(x) << " = " << deb_tos(x, len)
#define call_deb_ex_deb(x, len) deb_ex_deb(x, len)
// 要素が存在する行だけ出力(vvt)
#define deb_ex(v) \
do { \
int N = sz(v); \
int s = N; \
int t = 0; \
rep(i, N) { \
if (sz(v[i])) { \
chmi(s, i); \
chma(t, i); \
} \
} \
auto ex_v = sub(v, s, N); \
str S = deb_tos(ex_v, sz(ex_v)); \
debugName(v); \
cerr << " = " << endl; \
cerr << S << endl; \
} while (0);
#define debi(A) \
{ \
int len = min(sz(A), 20); \
was_deb = true; \
cerr << debugName(A) << " = " << endl; \
rep(i, len) cerr << std::right \
<< std::setw((int)(sz(tos(A[i])) + (i ? 1 : 0))) \
<< (i % 10); \
cerr << endl; \
rep(i, len) cerr << std::right \
<< std::setw((int)(sz(tos(A[i])) + (i ? 1 : 0))) << A[i]; \
cerr << endl; \
}
template <class T, class F> string deb_tos_f(vector<T> &A, F f) {
vi I;
rep(i, sz(A)) {
if (f(A[i]))
I.push_back(i);
}
was_deb = true;
stringstream ss;
fora(ii, i, I) ss << std::right
<< std::setw(
(int)(max(sz(tos(A[i])), sz(tos(i))) + (ii ? 1 : 0)))
<< i;
ss << endl;
fora(ii, i, I) ss << std::right
<< std::setw(
(int)(max(sz(tos(A[i])), sz(tos(i))) + (ii ? 1 : 0)))
<< A[i];
return ss.str();
}
template <class T, class F>
string deb_tos_f(vector<vector<T>> &a, F f, int key = -1) {
vi hs, ws_;
int H = sz(a), W = sz(a[0]);
vi exh(H), exw(W);
rep(h, H) {
rep(w, W) {
if (f(a[h][w])) {
exh[h] = true;
exw[w] = true;
}
}
}
rep(h, H) if (exh[h]) hs.push_back(h);
rep(w, W) if (exw[w]) ws_.push_back(w);
return deb_tos(a, hs, ws_, key);
}
template <class T, class F>
string deb_tos_f(vector<vector<vector<T>>> &a, F f) {
stringstream ss;
int H = sz(a);
if (sz(a) == 0)
return ss.str();
int cou = 0;
rep(i, H) {
if (any_of(a[i], v, f(v))) {
ss << deb_tos_f(a[i], f, i);
cou++;
}
if (cou > 50)
break;
}
ss << "" << endl;
return ss.str();
}
#define debf_normal(tab, f) \
do { \
cerr << debugName(tab) << " = " << endl; \
cerr << deb_tos_f(tab, f) << endl; \
} while (0);
#define debf2(tab, siki_r) debf_normal(tab, lamr(siki_r))
#define debf3(tab, v, siki) debf_normal(tab, lam(siki))
// S, sikir
// S, v, siki
#define debf(...) over3(__VA_ARGS__, debf3, debf2, debf1)(__VA_ARGS__)
#else
#define deb(...) ;
#define deb2(...) ;
#define deb3(...) ;
#define deb4(...) ;
#define deb_ex(...) ;
#define debf(...) ;
#define debi(...) ;
#endif
#define debugline(x) \
cerr << x << " " \
<< "(L:" << __LINE__ << ")" << '\n'
/*@formatter:off*/
using u32 = unsigned;
using u64 = unsigned long long;
using u128 = __uint128_t;
using bint = __int128;
std::ostream &operator<<(std::ostream &dest, __int128_t value) {
std::ostream::sentry s(dest);
if (s) {
__uint128_t tmp = value < 0 ? -value : value;
char buffer[128];
char *d = std::end(buffer);
do {
--d;
*d = "0123456789"[tmp % 10];
tmp /= 10;
} while (tmp != 0);
if (value < 0) {
--d;
*d = '-';
}
ll len = std::end(buffer) - d;
if (dest.rdbuf()->sputn(d, len) != len) {
dest.setstate(std::ios_base::badbit);
}
}
return dest;
}
__int128 to_bint(string &s) {
__int128 ret = 0;
for (ll i = 0; i < (ll)s.length(); ++i)
if ('0' <= s[i] && s[i] <= '9')
ret = 10 * ret + s[i] - '0';
return ret;
}
void operator>>(istream &iss, bint &v) {
string S;
iss >> S;
v = 0;
rep(i, sz(S)) {
v *= 10;
v += S[i] - '0';
}
}
// 便利関数
/*@formatter:off*/
// テスト用
#define rand xor128_
unsigned long xor128_(void) {
static unsigned long x = 123456789, y = 362436069, z = 521288629,
w = 88675123;
unsigned long t;
t = (x ^ (x << 11));
x = y;
y = z;
z = w;
return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}
char ranc() { return (char)('a' + rand() % 26); }
ll rand(ll min, ll max) {
assert(min <= max);
if (min >= 0 && max >= 0) {
return rand() % (max + 1 - min) + min;
} else if (max < 0) {
return -rand(-max, -min);
} else {
if (rand() % 2) {
return rand(0, max);
} else {
return -rand(0, -min);
}
}
}
ll rand(ll max) { return rand(0, max); }
template <class T> T rand(vector<T> &A) { return A[rand(sz(A) - 1)]; }
// 重複することがある
template <class T> vector<T> ranv(vector<T> &A, int N) {
vector<T> ret(N);
rep(i, N) { ret[i] = rand(A); }
return ret;
}
template <class T> vector<T> ranv_unique(vector<T> &A, int N) {
vector<T> ret(N);
umapi was;
rep(j, N) {
int i;
while (1) {
i = rand(sz(A) - 1);
if (was.find(i) == was.end())
break;
}
ret[j] = A[i];
was[i] = 1;
}
return ret;
}
vi ranv(ll n, ll min, ll max) {
vi v(n);
rep(i, n) v[i] = rand(min, max);
return v;
}
/*@formatter:off*/
#ifdef _DEBUG
bool timeup(int time) {
static bool never = true;
if (never)
message += "may timeup, because slow";
never = false;
auto end_time = system_clock::now();
auto part = duration_cast<milliseconds>(end_time - start_time);
auto lim = milliseconds(time);
return part >= lim;
}
#else
bool timeup(int time) {
auto end_time = system_clock::now();
auto part = duration_cast<milliseconds>(end_time - start_time);
auto lim = milliseconds(time);
return part >= lim;
}
#endif
void set_time() { past_time = system_clock::now(); }
// MS型(millisecqnds)で返る
// set_timeをしてからの時間
auto calc_time_milli() {
auto now = system_clock::now();
auto part = duration_cast<milliseconds>(now - past_time);
return part;
}
auto calc_time_micro() {
auto now = system_clock::now();
auto part = duration_cast<microseconds>(now - past_time);
return part;
}
auto calc_time_nano() {
auto now = system_clock::now();
auto part = duration_cast<nanoseconds>(now - past_time);
return part;
}
bool calc_time(int zikan) { return calc_time_micro() >= microseconds(zikan); }
using MS = std::chrono::microseconds;
int div(microseconds a, microseconds b) { return a / b; }
int div(nanoseconds a, nanoseconds b) {
if (b < nanoseconds(1)) {
return a / nanoseconds(1);
}
int v = a / b;
return v;
}
// set_time();
// rep(i,lim)shori
// lim*=time_nanbai();
// rep(i,lim)shoriと使う
// 全体でmilliかかっていいときにlimを何倍してもう一回できるかを返す
int time_nanbai(int milli) {
auto dec = duration_cast<nanoseconds>(past_time - start_time);
auto part = calc_time_nano();
auto can_time = nanoseconds(milli * 1000 * 1000);
can_time -= part;
can_time -= dec;
return div(can_time, part);
}
/*@formatter:off*/
// #define use_rand
#ifdef use_rand
str ransu(ll n) {
str s;
rep(i, n) s += (char)rand('A', 'Z');
return s;
}
str ransl(ll n) {
str s;
rep(i, n) s += (char)rand('a', 'z');
return s;
}
// 単調増加
vi ranvinc(ll n, ll min, ll max) {
vi v(n);
bool bad = 1;
while (bad) {
bad = 0;
v.resize(n);
rep(i, n) {
if (i && min > max - v[i - 1]) {
bad = 1;
break;
}
if (i)
v[i] = v[i - 1] + rand(min, max - v[i - 1]);
else
v[i] = rand(min, max);
}
}
return v;
}
// 便利 汎用
#endif
void ranvlr(ll n, ll min, ll max, vi &l, vi &r) {
l.resize(n);
r.resize(n);
rep(i, n) {
l[i] = rand(min, max);
r[i] = l[i] + rand(0, max - l[i]);
}
}
template <class Iterable, class T = typename Iterable::value_type>
vector<pair<T, int>> run_length(const Iterable &a) {
vector<pair<T, int>> ret;
ret.eb(a[0], 1);
rep(i, 1, sz(a)) {
if (ret.back().fi == a[i]) {
ret.back().se++;
} else {
ret.eb(a[i], 1);
}
}
return ret;
}
/*@formatter:off*/
// #define use_mgr //_goldd _goldt
#ifdef use_mgr
//->[i, f(i)]
template <class T, class U, class F>
auto mgr(T ok, U ng, const F &f,
require_arg(is_integral<T>::value &&is_integral<U>::value)) {
auto mid = (ok + ng);
if (ok < ng)
while (ng - ok > 1) {
mid = (ok + ng) >> 1;
if (f(mid))
ok = mid;
else
ng = mid;
}
else
while (ok - ng > 1) {
mid = (ok + ng) >> 1;
if (f(mid))
ok = mid;
else
ng = mid;
}
return ok;
}
//[l, r)の中で,f(i)がtrueとなる範囲を返す okはそこに含まれる
template <class F> P mgr_range(int l, int r, F f, int ok) {
if (f(ok) == 0) {
out("f(ok) must true");
re();
}
return mp(mgr(ok, l - 1, f), mgr(ok, r, f) + 1);
}
template <class F> auto mgrd(dou ok, dou ng, F f, int kai = 100) {
if (ok < ng)
rep(i, kai) {
dou mid = (ok + ng) / 2;
if (f(mid))
ok = mid;
else
ng = mid;
}
else
rep(i, kai) {
dou mid = (ok + ng) / 2;
if (f(mid))
ok = mid;
else
ng = mid;
}
return ok;
}
template <class F> dou mgrd_time(dou ok, dou ng, F f, int time = 1980) {
bool han = true;
if (ok < ng)
while (1) {
dou mid = (ok + ng) / 2;
if (f(mid))
ok = mid, han = true;
else
ng = mid, han = false;
deb(mid, han);
if (timeup(time)) {
break;
}
}
else
while (1) {
dou mid = (ok + ng) / 2;
if (f(mid))
ok = mid, han = true;
else
ng = mid, han = false;
deb(mid, han);
if (timeup(time)) {
break;
}
}
return ok;
}
// todo 減らす
template <class F> auto goldd_l(ll left, ll right, F calc) {
double GRATIO = 1.6180339887498948482045868343656;
ll lm = left + (ll)((right - left) / (GRATIO + 1.0));
ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0));
ll fl = calc(lm);
ll fr = calc(rm);
while (right - left > 10) {
if (fl < fr) {
right = rm;
rm = lm;
fr = fl;
lm = left + (ll)((right - left) / (GRATIO + 1.0));
fl = calc(lm);
} else {
left = lm;
lm = rm;
fl = fr;
rm = lm + (ll)((right - lm) / (GRATIO + 1.0));
fr = calc(rm);
}
}
ll minScore = MAX<ll>();
ll resIndex = left;
for (ll i = left; i < right + 1; ++i) {
ll score = calc(i);
if (minScore > score) {
minScore = score;
resIndex = i;
}
}
return make_tuple(resIndex, calc(resIndex));
}
template <class F> auto goldt_l(ll left, ll right, F calc) {
double GRATIO = 1.6180339887498948482045868343656;
ll lm = left + (ll)((right - left) / (GRATIO + 1.0));
ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0));
ll fl = calc(lm);
ll fr = calc(rm);
while (right - left > 10) {
if (fl > fr) {
right = rm;
rm = lm;
fr = fl;
lm = left + (ll)((right - left) / (GRATIO + 1.0));
fl = calc(lm);
} else {
left = lm;
lm = rm;
fl = fr;
rm = lm + (ll)((right - lm) / (GRATIO + 1.0));
fr = calc(rm);
}
}
if (left > right) {
ll l = left;
left = right;
right = l;
}
ll maxScore = MIN<ll>();
ll resIndex = left;
for (ll i = left; i < right + 1; ++i) {
ll score = calc(i);
if (maxScore < score) {
maxScore = score;
resIndex = i;
}
}
return make_tuple(resIndex, calc(resIndex));
}
/*loopは200にすればおそらく大丈夫 余裕なら300に*/
template <class F> auto goldd_d(dou left, dou right, F calc, ll loop = 200) {
dou GRATIO = 1.6180339887498948482045868343656;
dou lm = left + ((right - left) / (GRATIO + 1.0));
dou rm = lm + ((right - lm) / (GRATIO + 1.0));
dou fl = calc(lm);
dou fr = calc(rm); /*200にすればおそらく大丈夫*/ /*余裕なら300に*/
ll k = 141;
loop++;
while (--loop) {
if (fl < fr) {
right = rm;
rm = lm;
fr = fl;
lm = left + ((right - left) / (GRATIO + 1.0));
fl = calc(lm);
} else {
left = lm;
lm = rm;
fl = fr;
rm = lm + ((right - lm) / (GRATIO + 1.0));
fr = calc(rm);
}
}
return make_tuple(left, calc(left));
}
template <class F> auto goldt_d(dou left, dou right, F calc, ll loop = 200) {
double GRATIO = 1.6180339887498948482045868343656;
dou lm = left + ((right - left) / (GRATIO + 1.0));
dou rm = lm + ((right - lm) / (GRATIO + 1.0));
dou fl = calc(lm);
dou fr = calc(rm);
loop++;
while (--loop) {
if (fl > fr) {
right = rm;
rm = lm;
fr = fl;
lm = left + ((right - left) / (GRATIO + 1.0));
fl = calc(lm);
} else {
left = lm;
lm = rm;
fl = fr;
rm = lm + ((right - lm) / (GRATIO + 1.0));
fr = calc(rm);
}
}
return make_tuple(left, calc(left));
}
// l ~ rを複数の区間に分割し、極致を与えるiを返す time-20 msまで探索
template <class F> auto goldd_ls(ll l, ll r, F calc, ll time = 2000) {
auto lim = milliseconds(time - 20);
ll mini = 0, minv = MAX<ll>(); /*区間をk分割する*/
rep(k, 1, inf) {
auto s = system_clock::now();
ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/
ll nl = l;
ll nr = l + haba;
rep(i, k) {
ll ni = goldd_l(nl, nr, calc);
if (chmi(minv, calc(ni)))
mini = ni;
nl = nr;
nr = nl + haba;
}
auto end = system_clock::now();
auto part = duration_cast<milliseconds>(end - s);
auto elapsed = duration_cast<milliseconds>(end - start_time);
if (elapsed + part * 2 >= lim) {
break;
}
}
return make_tuple(mini, calc(mini));
}
template <class F> auto goldt_ls(ll l, ll r, F calc, ll time = 2000) {
auto lim = milliseconds(time - 20);
ll maxi = 0, maxv = MIN<ll>(); /*区間をk分割する*/
rep(k, 1, inf) {
auto s = system_clock::now();
ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/
ll nl = l;
ll nr = l + haba;
rep(i, k) {
ll ni = goldt_l(nl, nr, calc);
if (chma(maxv, calc(ni)))
maxi = ni;
nl = nr;
nr = nl + haba;
}
auto end = system_clock::now();
auto part = duration_cast<milliseconds>(end - s);
auto elapsed = duration_cast<milliseconds>(end - start_time);
if (elapsed + part * 2 >= lim) {
break;
}
}
return make_tuple(maxi, calc(maxi));
}
template <class F>
auto goldd_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を持つ*/
auto lim = milliseconds(time - 20);
dou mini = 0, minv = MAX<dou>(); /*区間をk分割する*/
rep(k, 1, inf) {
auto s = system_clock::now();
dou haba = (r - l) / k;
dou nl = l;
dou nr = l + haba;
rep(i, k) {
dou ni = goldd_d(nl, nr, calc);
if (chmi(minv, calc(ni)))
mini = ni;
nl = nr;
nr = nl + haba;
}
auto end = system_clock::now();
auto part = duration_cast<milliseconds>(end - s);
auto elapsed = duration_cast<milliseconds>(end - start_time);
if (elapsed + part * 2 >= lim) {
break;
}
}
return make_tuple(mini, calc(mini));
}
template <class F>
auto goldt_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を残している*/
auto lim = milliseconds(time - 20);
dou maxi = 0, maxv = MIN<dou>(); /*区間をk分割する*/
rep(k, 1, inf) {
auto s = system_clock::now();
dou haba = (r - l) / k;
dou nl = l;
dou nr = l + haba;
rep(i, k) {
dou ni = goldt_d(nl, nr, calc);
if (chma(maxv, calc(ni)))
maxi = ni;
nl = nr;
nr = nl + haba;
}
auto end = system_clock::now();
auto part = duration_cast<milliseconds>(end - s);
auto elapsed = duration_cast<milliseconds>(end - start_time);
if (elapsed + part * 2 >= lim) {
break;
}
}
return make_tuple(maxi, calc(maxi));
}
#endif
// strを整数として比較
string smax(str &a, str b) {
if (sz(a) < sz(b)) {
return b;
} else if (sz(a) > sz(b)) {
return a;
} else if (a < b)
return b;
else
return a;
}
// strを整数として比較
string smin(str &a, str b) {
if (sz(a) > sz(b)) {
return b;
} else if (sz(a) < sz(b)) {
return a;
} else if (a > b)
return b;
else
return a;
}
// エラーN
// 多次元なら(-1,-1,-1)
template <typename W, typename T> ll find(vector<W> &a, int l, const T key) {
rep(i, l, sz(a)) if (a[i] == key) return i;
return sz(a);
}
template <typename W, typename T> ll find(vector<W> &a, const T key) {
rep(i, sz(a)) if (a[i] == key) return i;
return sz(a);
}
template <typename W, typename T> P find(vector<vector<W>> &a, const T key) {
rep(i, sz(a)) rep(j, sz(a[0])) if (a[i][j] == key) return mp(i, j);
return mp(-1, -1);
}
// getid(find())を返す 1次元にする
template <typename W, typename T> int findi(vector<vector<W>> &a, const T key) {
rep(i, sz(a)) rep(j, sz(a[0])) if (a[i][j] == key) return i * sz(a[0]) + j;
return -1;
}
template <typename W, typename U>
tuple<int, int, int> find(vector<vector<vector<W>>> &a, const U key) {
rep(i, sz(a)) rep(j, sz(a[0]))
rep(k, sz(a[0][0])) if (a[i][j][k] ==
key) return tuple<int, int, int>(i, j, k);
return tuple<int, int, int>(-1, -1, -1);
}
// 無ければN
int find(string &s, const string key) {
int klen = sz(key);
rep(i, sz(s) - klen + 1) {
if (s[i] != key[0])
continue;
if (s.substr(i, klen) == key) {
return i;
}
}
return sz(s);
}
int find(string &s, int l, const string key) {
int klen = sz(key);
rep(i, l, sz(s) - klen + 1) {
if (s[i] != key[0])
continue;
if (s.substr(i, klen) == key) {
return i;
}
}
return sz(s);
}
int find(string &s, const char key) {
rep(i, sz(s)) {
if (s[i] == key)
return i;
}
return sz(s);
}
int find(string &s, int l, const char key) {
rep(i, l, sz(s)) {
if (s[i] == key)
return i;
}
return sz(s);
}
// N箇所について(自分含む)右のkeyの場所を返す
template <typename W, typename T> vi finds(const W &a, const T &key) {
int n = sz(a);
vi rpos(n, n);
rer(i, n - 1) {
if (i < n - 1) {
rpos[i] = rpos[i + 1];
}
if (a[i] == key)
rpos[i] = i;
}
return rpos;
}
template <typename W, typename T> vi rfinds(const W &a, const T &key) {
int n = sz(a);
vi lpos(n, -1);
rep(i, n) {
if (i > 0) {
lpos[i] = lpos[i - 1];
}
if (a[i] == key)
lpos[i] = i;
}
return lpos;
}
// todoz
#if __cplusplus >= 201703L
template <typename W, typename T, class Iterable = typename W::value_type>
ll count(const W &a, const T &k) {
return count_if(a, == k);
}
template <typename W, class Iterable = typename W::value_type>
vi count(const W &a) {
vi res;
for_each(a, v, if (sz(res) <= (int)v) res.resize((int)v + 1); res[v]++;);
return res;
}
#else
ll count(const string &S, const char &c) {
int res = 0;
fora(s, S) { res += s == c; }
return res;
}
#endif
ll count(const str &a, const str &k) {
ll ret = 0, len = k.length();
auto pos = a.find(k);
while (pos != string::npos)
pos = a.find(k, pos + len), ++ret;
return ret;
}
/*@formatter:off*/
//'a' = 'A' = 0 として集計 既に-'a'されていても動く
vi count(str &a, int l, int r) {
vi cou(26);
char c = 'a';
if ('A' <= a[l] && a[l] <= 'Z')
c = 'A';
if ('a' <= a[l] && a[l] <= 'z')
c = 'a';
else
c = 0;
rep(i, l, r)++ cou[a[i] - c];
return cou;
}
#define couif count_if
// algorythm
ll rev(ll a) {
ll res = 0;
while (a) {
res *= 10;
res += a % 10;
a /= 10;
}
return res;
}
template <class T> auto rev(const vector<T> &a) {
auto b = a;
reverse(ALL(b));
return b;
}
/* \反転 */ template <class U> auto rev(vector<vector<U>> &a) {
vector<vector<U>> b(sz(a[0]), vector<U>(sz(a)));
rep(h, sz(a)) rep(w, sz(a[0])) b[w][h] = a[h][w];
return b;
}
/* |反転 */ template <class U> auto revw(vector<vector<U>> &a) {
vector<vector<U>> b(sz(a), vector<U>(sz(a[0])));
int W = sz(a[0]);
rep(h, sz(a)) rep(w, sz(a[0])) { b[h][W - 1 - w] = a[h][w]; }
return b;
}
/* ー反転 */ template <class U> auto revh(vector<vector<U>> &a) {
vector<vector<U>> b(sz(a), vector<U>(sz(a[0])));
int H = sz(a);
rep(h, sz(a)) rep(w, sz(a[0])) { b[H - 1 - h][w] = a[h][w]; }
return b;
}
/* /反転 */ template <class U> auto revr(vector<vector<U>> &a) {
vector<vector<U>> b(sz(a[0]), vector<U>(sz(a)));
int H = sz(a);
int W = sz(a[0]);
rep(h, sz(a)) rep(w, sz(a[0])) b[w][h] = a[H - 1 - h][W - 1 - w];
return b;
}
auto rev(const string &a) {
string b = a;
reverse(ALL(b));
return b;
}
template <class T> auto rev(const T &v, int i) { return v[sz(v) - 1 - i]; }
int rev(int N, int i) { return N - 1 - i; }
constexpr ll p10[] = {1,
10,
100,
1000,
10000,
100000,
1000000,
10000000,
100000000,
1000000000,
10000000000ll,
100000000000ll,
1000000000000ll,
10000000000000ll,
100000000000000ll,
1000000000000000ll,
10000000000000000ll,
100000000000000000ll,
1000000000000000000ll};
// 0は0桁
ll keta(ll v, int if_zero_res) {
if (!v)
return if_zero_res;
if (v < p10[9]) {
if (v < p10[4]) {
if (v < p10[2]) {
if (v < p10[1]) {
if (v < p10[0])
return 0;
else
return 1;
} else
return 2;
} else {
if (v < p10[3])
return 3;
else
return 4;
}
} else {
if (v < p10[7]) {
if (v < p10[5])
return 5;
else if (v < p10[6])
return 6;
else
return 7;
} else {
if (v < p10[8])
return 8;
else
return 9;
}
}
} else {
if (v < p10[13]) {
if (v < p10[11]) {
if (v < p10[10])
return 10;
else
return 11;
} else {
if (v < p10[12])
return 12;
else
return 13;
}
} else {
if (v < p10[15]) {
if (v < p10[14])
return 14;
else
return 15;
} else {
if (v < p10[17]) {
if (v < p10[16])
return 16;
else
return 17;
} else {
if (v < p10[18])
return 18;
else
return 19;
}
}
}
}
}
#if __cplusplus >= 201703L
ll getr(ll a, ll keta) { return (a / pow<ll>(10, keta)) % 10; }
#else
ll getr(ll a, ll keta) { return (a / (int)pow(10, keta)) % 10; }
#endif
// 上から何桁目か
ll getl(ll a, ll ket) {
int sketa = keta(a, 1);
return getr(a, sketa - 1 - ket);
}
ll dsum(ll v, ll sin = 10) {
ll ret = 0;
for (; v; v /= sin)
ret += v % sin;
return ret;
}
ll mask10(ll v) { return p10[v] - 1; }
// 変換系
template <class T, class U>
auto to_v1(vector<reference_wrapper<U>> &ret, vector<T> &A) {
rep(i, sz(A)) ret.push_back(A[i]);
return ret;
}
template <class T, class U>
auto to_v1(vector<reference_wrapper<U>> &ret, vector<vector<T>> &A) {
rep(i, sz(A)) to_v1(ret, A[i]);
return ret;
}
// 参照付きで1次元に起こす
template <class T> auto to_v1(vector<vector<T>> &A) {
vector<reference_wrapper<decl2_t<decltype(A)>>> ret;
rep(i, sz(A)) to_v1(ret, A[i]);
return ret;
}
//[v] := iとなるようなvectorを返す
// 存在しない物は-1
// 空でも動く(なぜか)
template <class T> auto keys(const T &a) {
vector<decltype((a.begin())->fi)> res;
for (auto &&k : a)
res.push_back(k.fi);
return res;
}
template <class T> auto values(const T &a) {
vector<decltype((a.begin())->se)> res;
for (auto &&k : a)
res.push_back(k.se);
return res;
}
// todo 可変長で
template <class T> constexpr T min(T a, T b, T c) {
return a >= b ? b >= c ? c : b : a >= c ? c : a;
}
template <class T> constexpr T max(T a, T b, T c) {
return a <= b ? b <= c ? c : b : a <= c ? c : a;
}
// 1次元のコンテナ
template <class T> int mini(const vector<T> &a) {
return min_element(ALL(a)) - a.begin();
}
template <class T> int maxi(const vector<T> &a) {
return max_element(ALL(a)) - a.begin();
}
template <class T> T sum(const T &A) { return A; }
template <class T> T sum(const vector<T> &A, int l = -1, int r = -1) {
T s = 0;
set_lr12(l, r, sz(A));
rep(i, l, r) s += sum(A[i]);
return s;
}
// 1次元のIterableに使える
template <class T, require_t(!has_value_type<T>::value)> T min(T &a) {
return a;
}
template <class V, require_t(has_value_type<V>::value)>
auto min(V &a, ll s = -1, ll n = -1) {
using Ret_t = decl2_t<V>;
auto ret = MAX<Ret_t>();
set_lr12(s, n, sz(a));
if (s == n) {
return INF<Ret_t>();
} else {
rep(i, s, n) { ret = min(ret, min(a[i])); }
return ret;
}
}
template <class T, require_t(!has_value_type<T>::value)> T max(T &a) {
return a;
}
template <class V, require_t(has_value_type<V>::value)>
auto max(V &a, ll s = -1, ll n = -1) {
using Ret_t = decl2_t<V>;
auto ret = MIN<Ret_t>();
set_lr12(s, n, sz(a));
if (s == n) {
return -INF<Ret_t>();
} else {
rep(i, s, n) { ret = max(ret, max(a[i])); }
return ret;
}
}
template <class T> T mul(vector<T> &v, ll t = inf) {
T ret = v[0];
rep(i, 1, min(t, sz(v))) ret *= v[i];
return ret;
}
// template<class T, class U, class... W> auto sumn(vector<T> &v, U head, W...
// tail) { auto ret = sum(v[0], tail...); rep(i, 1, min(sz(v), head))ret
// += sum(v[i], tail...); return ret;} indexを持つvectorを返す
/*@formatter:off*/
template <typename W, typename T> void fill(W &xx, const T vall) { xx = vall; }
template <typename W, typename T> void fill(vector<W> &vecc, const T vall) {
for (auto &&vx : vecc)
fill(vx, vall);
}
template <typename W, typename T> void fill(vector<W> &xx, const T v, ll len) {
rep(i, len) xx[i] = v;
}
template <typename W, typename T>
void fill(vector<W> &xx, const T v, int s, ll t) {
rep(i, s, t) xx[i] = v;
}
template <typename W, typename T>
void fill(vector<vector<W>> &xx, T v, int sh, int th, int sw, int tw) {
rep(h, sh, th) rep(w, sw, tw) xx[h][w] = v;
}
// #define use_fill //_sum _array _max _min
#ifdef use_fill
template <typename A, size_t N, typename T> void fill(A (&a)[N], const T &v) {
rep(i, N) { fill(a[i], v); }
}
template <class T, class U> void fill(vector<T> &a, const vi &ind, U val) {
fora(v, ind) { a[v] = val; }
}
template <typename A, size_t N> auto sum(A (&a)[N], int l = -1, int r = -1) {
set_lr12(l, r, N);
auto res = sum(a[l]);
if (l == r) {
return res - res;
}
rep(i, l + 1, r) res += sum(a[i]);
return res;
}
template <typename A, size_t N> auto max(A (&a)[N], int l = -1, int r = -1) {
set_lr12(l, r, N);
auto res = max(a[l]);
if (l == r) {
return -INF<decltype(res)>();
}
rep(i, l + 1, r) res = max(res, max(a[i]));
return res;
}
template <typename A, size_t N> auto min(A (&a)[N], int l = -1, int r = -1) {
set_lr12(l, r, N);
auto res = min(a[l]);
if (l == r) {
return INF<decltype(res)>();
}
rep(i, l + 1, r) res = min(res, min(a[i]));
return res;
}
#endif
vi inds_(vi &a) {
int n = max(a) + 1;
vi ret(n, -1);
rep(i, sz(a)) {
assert(ret[a[i]] == -1);
ret[a[i]] = i;
}
return ret;
}
void clear(PQ &q) { q = PQ(); }
void clear(priority_queue<int> &q) { q = priority_queue<int>(); }
template <class T> void clear(queue<T> &q) {
while (q.size())
q.pop();
}
// template<class T> T *negarr(ll size) { T *body = (T *) malloc((size * 2 +
// 1) * sizeof(T)); return body + size;} template<class T> T *negarr2(ll h,
// ll w) { double **dummy1 = new double *[2 * h + 1]; double *dummy2 = new
// double[(2 * h + 1) * (2 * w + 1)]; dummy1[0] = dummy2 + w; for (ll i =
// 1; i <= 2 * h + 1; ++i) { dummy1[i] = dummy1[i - 1] + 2 * w + 1; } double
// **a = dummy1 + h; return a;}
template <class T> struct ruiC {
vector<T> rui;
ruiC(vector<T> &ru) : rui(ru) {}
/*先頭0*/
ruiC() : rui(1, 0) {}
T operator()(ll l, ll r) {
if (l > r) {
cerr << "ruic ";
deb(l, r);
assert(0);
}
return rui[r] - rui[l];
}
T operator()(int r = inf) { return operator()(0, min(r, sz(rui) - 1)); }
/*ruiv[]をruic[]に変えた際意味が変わるのがまずいため()と統一*/
/*単体iを返す 累積でないことに注意(seg木との統一でこうしている)*/
// T operator[](ll i) { return rui[i + 1] - rui[i]; }
T operator[](ll i) { return rui[i]; }
/*0から順に追加される必要がある*/
void operator+=(T v) { rui.push_back(rui.back() + v); }
void add(int i, T v) {
if (sz(rui) - 1 != i)
ole();
operator+=(v);
}
T back() { return rui.back(); }
ll size() { return rui.size(); }
auto begin() { return rui.begin(); }
auto end() { return rui.end(); }
};
template <class T> string deb_tos(const ruiC<T> &a) { return deb_tos(a.rui); }
template <class T> ostream &operator<<(ostream &os, ruiC<T> a) {
fora(v, a.rui) { os << v << " "; }
return os;
}
template <class T> vector<T> ruiv(const vector<T> &a) {
vector<T> ret(a.size() + 1);
rep(i, a.size()) ret[i + 1] = ret[i] + a[i];
return ret;
}
template <class T> ruiC<T> ruic(const vector<T> &a) {
vector<T> ret = ruiv(a);
return ruiC<T>(ret);
}
template <class T> ruiC<T> ruic() { return ruiC<T>(); }
// imoは0-indexed
// ruiは1-indexed
template <class T> vector<T> imo(const vector<T> &v) {
vector<T> ret = v;
rep(i, sz(ret) - 1) ret[i + 1] += ret[i];
return ret;
}
// #define use_rui //_imo _ruic _ruiv
#ifdef use_rui
// kと同じものの数
template <class T, class U> vi imo(const vector<T> &a, U k) {
vi equ(sz(a));
rep(i, sz(a)) { equ[i] = a[i] == k; }
return imo(equ);
}
template <class T> vector<T> imox(const vector<T> &v) {
vector<T> ret = v;
rep(i, sz(ret) - 1) ret[i + 1] ^= ret[i];
return ret;
}
// 漸化的に最小を持つ
template <class T> vector<T> imi(const vector<T> &v) {
vector<T> ret = v;
rep(i, sz(ret) - 1) chmi(ret[i + 1], ret[i]);
return ret;
}
template <class T> vector<T> ima(const vector<T> &v) {
vector<T> ret = v;
rep(i, sz(ret) - 1) chma(ret[i + 1], ret[i]);
return ret;
}
template <class T> vector<T> rimi(const vector<T> &v) {
vector<T> ret = v;
rer(i, sz(ret) - 1, 1) chmi(ret[i - 1], ret[i]);
return ret;
}
template <class T> vector<T> rima(const vector<T> &v) {
vector<T> ret = v;
rer(i, sz(ret) - 1, 1) chma(ret[i - 1], ret[i]);
return ret;
}
template <class T> struct ruimax {
template <typename Monoid> struct SegmentTree { /*pairで処理*/
int sz;
vector<Monoid> seg;
const Monoid M1 = mp(MIN<T>(), -1);
Monoid f(Monoid a, Monoid b) { return max(a, b); }
void build(vector<T> &a) {
int n = sz(a);
sz = 1;
while (sz < n)
sz <<= 1;
seg.assign(2 * sz, M1);
rep(i, n) { seg[i + sz] = mp(a[i], i); }
for (int k = sz - 1; k > 0; k--) {
seg[k] = f(seg[k << 1], seg[(k << 1) | 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]; }
};
private:
vector<T> ve;
SegmentTree<pair<T, int>> seg;
vector<T> rv;
vector<int> ri;
bool build = false;
public:
int n;
ruimax(vector<T> &a) : ve(a), n(sz(a)) {
int index = -1;
T ma = MIN<T>();
rv.resize(n + 1);
ri.resize(n + 1);
rv[0] = -INF<T>();
ri[0] = -1;
rep(i, n) {
if (chma(ma, a[i])) {
index = i;
}
rv[i + 1] = ma;
ri[i + 1] = index;
}
}
T operator()(int l, int r) {
if (!(l <= r && 0 <= l && r <= n)) {
deb(l, r, n);
assert(0);
}
if (l == 0) {
return rv[r];
} else {
if (!build)
seg.build(ve), build = true;
return seg.query(l, r).first;
}
}
T operator()(int r = inf) { return operator()(0, min(r, n)); }
T operator[](int r) { return operator()(0, r); }
T getv(int l, int r) { return operator()(l, r); }
T getv(int r = inf) { return getv(0, min(r, n)); };
int geti(int l, int r) {
assert(l <= r && 0 <= l && r <= n);
if (l == 0) {
return ri[r];
} else {
if (!build)
seg.build(ve), build = true;
return seg.query(l, r).second;
}
}
int geti(int r = inf) { return geti(0, min(r, n)); };
auto begin() { return rv.begin(); }
auto end() { return rv.end(); }
};
template <class T> struct ruimin {
template <typename Monoid> struct SegmentTree { /*pairで処理*/
int sz;
vector<Monoid> seg;
const Monoid M1 = mp(MAX<T>(), -1);
Monoid f(Monoid a, Monoid b) { return min(a, b); }
void build(vector<T> &a) {
int n = sz(a);
sz = 1;
while (sz < n)
sz <<= 1;
seg.assign(2 * sz, M1);
rep(i, n) { seg[i + sz] = mp(a[i], i); }
for (int k = sz - 1; k > 0; k--) {
seg[k] = f(seg[k << 1], seg[(k << 1) | 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]; }
};
private:
vector<T> ve;
SegmentTree<pair<T, int>> seg;
vector<T> rv;
vector<int> ri;
bool build = false;
int n;
public:
ruimin(vector<T> &a) : ve(a), n(sz(a)) {
int index = -1;
T mi = MAX<T>();
rv.resize(n + 1);
ri.resize(n + 1);
rv[0] = INF<T>();
ri[0] = -1;
rep(i, n) {
if (chmi(mi, a[i])) {
index = i;
}
rv[i + 1] = mi;
ri[i + 1] = index;
}
}
T operator()(int l, int r) {
assert(l <= r && 0 <= l && r <= n);
if (l == 0) {
return rv[r];
} else {
if (!build)
seg.build(ve), build = true;
return seg.query(l, r).first;
}
}
T operator()(int r = inf) { return operator()(0, min(r, n)); }
T operator[](int r) { return operator()(0, r); }
T getv(int l, int r) { return operator()(l, r); }
T getv(int r = inf) { return getv(0, min(r, n)); };
int geti(int l, int r) {
{
assert(l <= r && 0 <= l && r <= n);
if (l == 0) {
return ri[r];
} else {
if (!build)
seg.build(ve), build = true;
return seg.query(l, r).second;
}
}
assert(l <= r && 0 <= l && r <= n);
if (l == 0) {
return ri[r];
} else {
if (!build)
seg.build(ve), build = true;
return seg.query(l, r).second;
}
}
int geti(int r = inf) { return geti(0, min(r, n)); };
auto begin() { return rv.begin(); }
auto end() { return rv.end(); }
}; /*@formatter:off*/
vvi() ruib(vi &a) {
vvi(res, 61, sz(a) + 1);
rep(k, 61) {
rep(i, sz(a)) { res[k][i + 1] = res[k][i] + ((a[i] >> k) & 1); }
}
return res;
}
vector<ruiC<int>> ruibc(vi &a) {
vector<ruiC<int>> ret(61);
vvi(res, 61, sz(a));
rep(k, 61) {
rep(i, sz(a)) { res[k][i] = (a[i] >> k) & 1; }
ret[k] = ruic(res[k]);
}
return ret;
}
// kと同じものの数
template <class T, class U> vi ruiv(T &a, U k) {
vi ret(a.size() + 1);
rep(i, a.size()) ret[i + 1] = ret[i] + (a[i] == k);
return ret;
}
template <class T, class U> ruiC<ll> ruic(T &a, U k) {
vi ret = ruiv(a, k);
return ruiC<ll>(ret);
}
template <class T> struct ruiC2 {
int H;
vector<ruiC<T>> rui;
ruiC<T> dummy; // 変なのをよばれたときはこれを返す//todo
ruiC2(const vector<vector<T>> &ru) : rui(sz(ru)), H(sz(ru)) {
for (int h = 0; h < H; h++) {
if (sz(ru[h]) == 0)
continue;
if (sz(dummy) == 1)
dummy = ruic(vector<T>(sz(ru[h])));
rui[h] = ruic(ru[h]);
}
}
// WについてHを返す
vector<T> operator()(ll l, ll r) {
if (l > r) {
cerr << "ruic ";
deb(l, r);
assert(0);
}
vector<T> res(H);
for (int h = 0; h < H; h++)
res[h] = rui[h](l, r);
return res;
}
// HについてWを返す
ruiC<T> &operator[](ll h) {
#ifdef _DEBUG
if (h >= H) {
message += "warning ruiC h >= H";
}
#endif
if (h >= H || sz(rui[h]) == 1)
return dummy;
else
return rui[h];
}
/*@formatter:off*/
// vector<T> operator()(int r) { return operator()(0, r); }
/*ruiv[]をruic[]に変えた際意味が変わるのがまずいため()と統一*/
/*単体iを返す 累積でないことに注意(seg木との統一でこうしている)*/
// T operator[](ll i) { return rui[i + 1] - rui[i]; }
/*0から順に追加される必要がある*/
// T back() { return rui.back(); }
// ll size() { return rui.size(); }
// auto begin(){return rui.begin();}
// auto end(){return rui.end();}
};
template <class T, class U> ruiC<ll> ruicou(vector<T> &a, U b) {
vi cou(sz(a));
rep(i, sz(a)) { cou[i] = a[i] == b; }
return ruic(cou);
}
// メモリは形式によらず(26*N)
// rui(l,r)でvector(26文字について, l~rのcの個数)
// rui[h] ruic()を返す
// 添え字は'a', 'A'のまま扱う
// (予め-='a','A'されているものが渡されたらそれに従う)
template <typename Iterable, class is_Iterable = typename Iterable::value_type>
ruiC2<ll> ruicou(const Iterable &a) {
int H = max(a) + 1;
vvi(cou, H);
rep(i, sz(a)) {
if (sz(cou[a[i]]) == 0)
cou[a[i]].resize(sz(a));
cou[a[i]][i] = 1;
}
return ruiC2<ll>(cou);
}
/*@formatter:off*/
// h query
template <class T> vector<T> imoh(vector<vector<T>> &v, int w) {
vector<T> ret(sz(v));
rep(h, sz(ret)) { ret[h] = v[h][w]; }
rep(i, sz(ret) - 1) { ret[i + 1] += ret[i]; }
return ret;
}
template <class T> vector<T> ruih(vector<vector<T>> &v, int w) {
vector<T> ret(sz(v) + 1);
rep(h, sz(v)) { ret[h + 1] = v[h][w]; }
rep(i, sz(v)) { ret[i + 1] += ret[i]; }
return ret;
}
template <class T> ruiC<T> ruihc(vector<vector<T>> &a, int w) {
vector<T> ret = ruih(a, w);
return ruiC<T>(ret);
}
// xor
template <class T> struct ruixC {
vector<T> rui;
ruixC(vector<T> &ru) : rui(ru) {}
T operator()(ll l, ll r) {
if (l > r) {
cerr << "ruiXc ";
deb(l, r);
assert(0);
}
return rui[r] ^ rui[l];
}
T operator[](ll i) { return rui[i]; }
T back() { return rui.back(); }
ll size() { return rui.size(); }
};
template <class T> vector<T> ruix(const vector<T> &a) {
vector<T> ret(a.size() + 1);
rep(i, a.size()) ret[i + 1] = ret[i] ^ a[i];
return ret;
}
template <class T> ruixC<ll> ruixc(vector<T> &a) {
vi ret = ruix(a);
return ruixC<ll>(ret);
}
// 差分を返す(累積を取ると元に戻る)
// 101なら
// 1111を返す
// 元の配列で[l, r)へのxorは
//[l]と[r]へのxorになる https://atcoder.jp/contests/abc155/tasks/abc155_f
vi ruix_diff(vi &A) {
int N = sz(A);
assert(N);
vi res(N + 1);
res[0] = A[0];
rep(i, 1, N) { res[i] = A[i - 1] ^ A[i]; }
res[N] = A[N - 1];
return res;
}
template <class T> vector<T> ruim(vector<T> &a) {
vector<T> res(a.size() + 1, 1);
rep(i, a.size()) res[i + 1] = res[i] * a[i];
return res;
}
// 漸化的に最小を1indexで持つ
template <class T> vector<T> ruimi(vector<T> &a) {
ll n = sz(a);
vector<T> ret(n + 1);
rep(i, 1, n) {
ret[i] = a[i - 1];
chmi(ret[i + 1], ret[i]);
}
return ret;
}
// template<class T> T *rrui(vector<T> &a) {
// 右から左にかけての半開区間 (-1 n-1]
template <class T> struct rruiC {
vector<T> rui;
int n;
rruiC(vector<T> &a) : n(sz(a)) {
rui.resize(n + 1);
rer(i, n - 1) { rui[i] = rui[i + 1] + a[i]; }
}
/*[r l)*/
T operator()(int r, int l) {
r++;
l++;
assert(l <= r && l >= 0 && r <= n);
return rui[l] - rui[r];
}
T operator()(int l) { return operator()(n - 1, l); }
T operator[](int i) { return operator()(i); }
};
template <class T> ostream &operator<<(ostream &os, rruiC<T> a) {
fora(v, a.rui) { os << v << " "; }
return os;
}
template <class T> string deb_tos(rruiC<T> &a) { return deb_tos(a.rui); }
#define rrui rruic
template <class T> rruiC<T> rruic(vector<T> &a) { return rruiC<T>(a); }
// 掛け算
template <class T> struct ruimulC {
vector<T> rv;
int n;
ruimulC(vector<T> &a) : rv(a), n(sz(a)) {
rv.resize(n + 1);
rv[0] = 1;
rep(i, n) { rv[i + 1] = a[i] * rv[i]; }
}
ruimulC() : n(0) {
rv.resize(n + 1);
rv[0] = 1;
}
void operator+=(T v) {
rv.push_back(rv.back() * v);
n++;
}
T operator()(int l, int r) {
assert(l <= r && 0 <= l && r <= n);
return rv[r] / rv[l];
}
T operator()(int r = inf) { return operator()(0, min(r, n)); }
T operator[](int r) { return operator()(0, r); }
auto begin() { return rv.begin(); }
auto end() { return rv.end(); }
};
template <class T> ruimulC<T> ruimul(vector<T> &a) { return ruimulC<T>(a); }
template <class T> ruimulC<T> ruimul() {
vector<T> a;
return ruimulC<T>(a);
}
template <class T> T *rruim(vector<T> &a) {
ll len = a.size();
T *body = (T *)malloc((len + 1) * sizeof(T));
T *res = body + 1;
res[len - 1] = 1;
rer(i, len - 1) res[i - 1] = res[i] * a[i];
return res;
}
template <class T, class U, class W> T lowerBound(ruiC<T> &a, U v, W banpei) {
return lowerBound(a.rui, v, banpei);
}
template <class T, class U, class W> T upperBound(ruiC<T> &a, U v, W banpei) {
return upperBound(a.rui, v, banpei);
}
template <class T, class U, class W> T rlowerBound(ruiC<T> &a, U v, W banpei) {
return rlowerBound(a.rui, v, banpei);
}
template <class T, class U, class W> T rupperBound(ruiC<T> &a, U v, W banpei) {
return rupperBound(a.rui, v, banpei);
}
#endif
constexpr bool bget(ll m, ll keta) {
#ifdef _DEBUG
assert(keta <= 62); // オーバーフロー 1^62までしか扱えない
#endif
return (m >> keta) & 1;
}
// bget(n)次元
// NならN-1まで
vector<vi> bget2(vi &a, int keta_size) {
vvi(res, keta_size, sz(a));
rep(k, keta_size) {
rep(i, sz(a)) { res[k][i] = bget(a[i], k); }
}
return res;
}
vi bget1(vi &a, int keta) {
vi res(sz(a));
rep(i, sz(a)) { res[i] = bget(a[i], keta); }
return res;
}
#if __cplusplus >= 201703L
ll bget(ll m, ll keta, ll sinsuu) {
m /= pow<ll>(sinsuu, keta);
return m % sinsuu;
}
#else
ll bget(ll m, ll keta, ll sinsuu) {
m /= (ll)pow(sinsuu, keta);
return m % sinsuu;
}
#endif
constexpr ll bit(ll n) {
#ifdef _DEBUG
assert(n <= 62); // オーバーフロー 1^62までしか扱えない
#endif
return (1LL << (n));
}
#if __cplusplus >= 201703L
ll bit(ll n, ll sinsuu) { return pow<ll>(sinsuu, n); }
#else
ll bit(ll n, ll sinsuu) { return (ll)pow(sinsuu, n); }
#endif
ll mask(ll n) { return (1ll << n) - 1; }
// aをbitに置きなおす
//{0, 2} -> 101
ll bit(const vi &a) {
int m = 0;
for (auto &&v : a)
m |= bit(v);
return m;
}
//{1, 1, 0} -> 011
// bitsetに置き換える感覚 i が立っていたら i bit目を立てる
ll bit_bool(vi &a) {
int m = 0;
rep(i, sz(a)) if (a[i]) m |= bit(i);
return m;
}
#define bcou __builtin_popcountll
// 最下位ビット
ll lbit(ll n) {
assert(n);
return n & -n;
}
ll lbiti(ll n) {
assert(n);
return log2(n & -n);
}
// 最上位ビット
ll hbit(ll n) {
assert(n);
n |= (n >> 1);
n |= (n >> 2);
n |= (n >> 4);
n |= (n >> 8);
n |= (n >> 16);
n |= (n >> 32);
return n - (n >> 1);
}
ll hbiti(ll n) {
assert(n);
return log2(hbit(n));
}
// ll hbitk(ll n) { ll k = 0; rer(i, 5) { ll a = k + (1ll << i); ll
// b = 1ll << a; if (b <= n)k += 1ll << i; } return k;}
// 初期化は0を渡す
ll nextComb(ll &mask, ll n, ll r) {
if (!mask)
return mask = (1LL << r) - 1;
ll x = mask & -mask; /*最下位の1*/
ll y = mask + x; /*連続した下の1を繰り上がらせる*/
ll res = ((mask & ~y) / x >> 1) | y;
if (bget(res, n))
return mask = 0;
else
return mask = res;
}
// n桁以下でビットがr個立っているもののvectorを返す
vi bitCombList(ll n, ll r) {
vi res;
ll m = 0;
while (nextComb(m, n, r)) {
res.push_back(m);
}
return res;
}
/*over*/ #define forbit1_2(i, mas) \
for (int forbitj = !mas ? 0 : lbit(mas), forbitm = mas, \
i = !mas ? 0 : log2(forbitj); \
forbitm; forbitm = forbitm ^ forbitj, \
forbitj = !forbitm ? 1 : lbit(forbitm), i = log2(forbitj))
/*over*/ #define forbit1_3(i, N, mas) \
for (int forbitj = !mas ? 0 : lbit(mas), forbitm = mas, \
i = !mas ? 0 : log2(forbitj); \
forbitm && i < N; forbitm = forbitm ^ forbitj, \
forbitj = !forbitm ? 1 : lbit(forbitm), i = log2(forbitj))
// masの立ってるindexを見る
// i, [N], mas
#define forbit1(...) over3(__VA_ARGS__, forbit1_3, forbit1_2)(__VA_ARGS__)
// masが立っていないindexを見る
// i, N, mas
#define forbit0(i, N, mas) forbit1(i, (mask(N) & (~(mas))))
// forsubをスニペットして使う
// Mの部分集合(0,M含む)を見る 3^sz(S)個ある
#define forsub_all(m, M) for (int m = M; m != -1; m = m == 0 ? -1 : (m - 1) & M)
// BASE進数
template <size_t BASE> class base_num {
int v;
public:
base_num(int v = 0) : v(v){};
int operator[](int i) { return bget(v, i, BASE); }
void operator++() { v++; }
void operator++(signed) { v++; }
operator int() { return v; }
};
#define base3(mas, lim, BASE) for (base_num<BASE> mas; mas < lim; mas++)
#define base2(mas, lim) base3(mas, lim, 2)
#define for_base(...) over3(__VA_ARGS__, base3, base2, base1)(__VA_ARGS__)
// aにある物をtrueとする
vb bool_(vi a, int n) {
vb ret(max(max(a) + 1, n));
rep(i, sz(a)) ret[a[i]] = true;
return ret;
}
char itoal(ll i) { return 'a' + i; }
char itoaL(ll i) { return 'A' + i; }
ll altoi(char c) {
if ('A' <= c && c <= 'Z')
return c - 'A';
return c - 'a';
}
ll ctoi(char c) { return c - '0'; }
char itoc(ll i) { return i + '0'; }
ll vtoi(vi &v) {
ll res = 0;
if (sz(v) > 18) {
debugline("vtoi");
deb(sz(v));
ole();
}
rep(i, sz(v)) {
res *= 10;
res += v[i];
}
return res;
}
vi itov(ll i) {
vi res;
while (i) {
res.push_back(i % 10);
i /= 10;
}
res = rev(res);
return res;
}
vi stov(string &a) {
ll n = sz(a);
vi ret(n);
rep(i, n) { ret[i] = a[i] - '0'; }
return ret;
}
// 基準を満たさないものは0になる
vi stov(string &a, char one) {
ll n = sz(a);
vi ret(n);
rep(i, n) ret[i] = a[i] == one;
return ret;
}
vector<vector<ll>> ctoi(vector<vector<char>> s, char c) {
ll n = sz(s), m = sz(s[0]);
vector<vector<ll>> res(n, vector<ll>(m));
rep(i, n) rep(j, m) res[i][j] = s[i][j] == c;
return res;
}
// #define use_compress
//[i] := vを返す
// aは0~n-1で置き換えられる
vi compress(vi &a) {
vi b;
ll len = a.size();
for (ll i = 0; i < len; ++i) {
b.push_back(a[i]);
}
sort(b);
unique(b);
for (ll i = 0; i < len; ++i) {
a[i] = lower_bound(ALL(b), a[i]) - b.begin();
}
ll blen = sz(b);
vi ret(blen);
rep(i, blen) { ret[i] = b[i]; }
return ret;
}
#ifdef use_compress
// ind[i] := i番目に小さい数
// map[v] := vは何番目に小さいか
vi compress(vi &a, umapi &map) {
vi b;
ll len = a.size();
for (ll i = 0; i < len; ++i) {
b.push_back(a[i]);
}
sort(b);
unique(b);
for (ll i = 0; i < len; ++i) {
ll v = a[i];
a[i] = lower_bound(ALL(b), a[i]) - b.begin();
map[v] = a[i];
}
ll blen = sz(b);
vi ret(blen);
rep(i, blen) { ret[i] = b[i]; }
return ret;
}
vi compress(vi &a, vi &r) {
vi b;
ll len = a.size();
fora(v, a) { b.push_back(v); }
fora(v, r) { b.push_back(v); }
sort(b);
unique(b);
for (ll i = 0; i < len; ++i)
a[i] = lower_bound(ALL(b), a[i]) - b.begin();
for (ll i = 0; i < sz(r); ++i)
r[i] = lower_bound(ALL(b), r[i]) - b.begin();
ll blen = sz(b);
vi ret(blen);
rep(i, blen) { ret[i] = b[i]; }
return ret;
}
vi compress(vi &a, vi &r, vi &s) {
vi b;
ll len = a.size();
fora(v, a) { b.push_back(v); }
fora(v, r) { b.push_back(v); }
fora(v, s) { b.push_back(v); }
sort(b);
unique(b);
for (ll i = 0; i < len; ++i)
a[i] = lower_bound(ALL(b), a[i]) - b.begin();
for (ll i = 0; i < sz(r); ++i)
r[i] = lower_bound(ALL(b), r[i]) - b.begin();
for (ll i = 0; i < sz(s); ++i)
r[i] = lower_bound(ALL(b), s[i]) - b.begin();
ll blen = sz(b);
vi ret(blen);
rep(i, blen) { ret[i] = b[i]; }
return ret;
}
vi compress(vector<vi> &a) {
vi b;
fora(vv, a) {
fora(v, vv) { b.push_back(v); }
}
sort(b);
unique(b);
fora(vv, a) {
fora(v, vv) { v = lower_bound(ALL(b), v) - b.begin(); }
}
ll blen = sz(b);
vi ret(blen);
rep(i, blen) { ret[i] = b[i]; }
return ret;
}
vi compress(vector<vector<vi>> &a) {
vi b;
fora(vvv, a) {
fora(vv, vvv) {
fora(v, vv) { b.push_back(v); }
}
}
sort(b);
unique(b);
fora(vvv, a) {
fora(vv, vvv) {
fora(v, vv) { v = lower_bound(ALL(b), v) - b.begin(); }
}
}
ll blen = sz(b);
vi ret(blen);
rep(i, blen) { ret[i] = b[i]; }
return ret;
}
void compress(ll a[], ll len) {
vi b;
for (ll i = 0; i < len; ++i) {
b.push_back(a[i]);
}
sort(b);
unique(b);
for (ll i = 0; i < len; ++i) {
a[i] = lower_bound(ALL(b), a[i]) - b.begin();
}
}
#endif
// 要素が見つからなかったときに困る
#define binarySearch(a, v) (binary_search(ALL(a), v))
#define lowerIndex(a, v) (lower_bound(ALL(a), v) - a.begin())
#define upperIndex(a, v) (upper_bound(ALL(a), v) - a.begin())
#define rlowerIndex(a, v) (upper_bound(ALL(a), v) - a.begin() - 1)
#define rupperIndex(a, v) (lower_bound(ALL(a), v) - a.begin() - 1)
template <class T, class U, class W> T lowerBound(vector<T> &a, U v, W banpei) {
auto it = lower_bound(a.begin(), a.end(), v);
if (it == a.end())
return banpei;
else
return *it;
}
template <class T, class U, class W> T upperBound(vector<T> &a, U v, W banpei) {
auto it = upper_bound(a.begin(), a.end(), v);
if (it == a.end())
return banpei;
else
return *it;
}
template <class T, class U, class W>
T rlowerBound(vector<T> &a, U v, W banpei) {
auto it = upper_bound(a.begin(), a.end(), v);
if (it == a.begin())
return banpei;
else {
return *(--it);
}
}
template <class T, class U, class W>
T rupperBound(vector<T> &a, U v, W banpei) {
auto it = lower_bound(a.begin(), a.end(), v);
if (it == a.begin())
return banpei;
else {
return *(--it);
}
}
// todo 消せないか
template <class T, class U, class W> T lowerBound(set<T> &a, U v, W banpei) {
auto it = a.lower_bound(v);
if (it == a.end())
return banpei;
else
return *it;
}
template <class T, class U, class W> T upperBound(set<T> &a, U v, W banpei) {
auto it = a.upper_bound(v);
if (it == a.end())
return banpei;
else
return *it;
}
template <class T, class U, class W> T rlowerBound(set<T> &a, U v, W banpei) {
auto it = a.upper_bound(v);
if (it == a.begin())
return banpei;
else {
return *(--it);
}
}
template <class T, class U, class W> T rupperBound(set<T> &a, U v, W banpei) {
auto it = a.lower_bound(v);
if (it == a.begin())
return banpei;
else {
return *(--it);
}
}
template <class T, class U, class W> T lowerBound(mset<T> &a, U v, W banpei) {
auto it = a.lower_bound(v);
if (it == a.end())
return banpei;
else
return *it;
}
template <class T, class U, class W> T upperBound(mset<T> &a, U v, W banpei) {
auto it = a.upper_bound(v);
if (it == a.end())
return banpei;
else
return *it;
}
template <class T, class U, class W> T rlowerBound(mset<T> &a, U v, W banpei) {
auto it = a.upper_bound(v);
if (it == a.begin())
return banpei;
else {
return *(--it);
}
}
template <class T, class U, class W> T rupperBound(mset<T> &a, U v, W banpei) {
auto it = a.lower_bound(v);
if (it == a.begin())
return banpei;
else {
return *(--it);
}
}
#define next2(a) next(next(a))
#define prev2(a) prev(prev(a))
// 狭義の単調増加列 長さを返す
template <class T> int lis(vector<T> &a) {
int n = sz(a);
vi tail(n + 1, MAX<T>());
rep(i, n) {
int id = lowerIndex(tail, a[i]); /**/
tail[id] = a[i];
}
return lowerIndex(tail, MAX<T>());
}
template <class T> int lis_eq(vector<T> &a) {
int n = sz(a);
vi tail(n + 1, MAX<T>());
rep(i, n) {
int id = upperIndex(tail, a[i]); /**/
tail[id] = a[i];
}
return lowerIndex(tail, MAX<T>());
}
// iteratorを返す
// valueが1以上の物を返す 0は見つけ次第削除
// vを減らす場合 (*it).se--でいい
template <class T, class U, class V> auto lower_map(map<T, U> &m, V k) {
auto ret = m.lower_bound(k);
while (ret != m.end() && (*ret).second == 0) {
ret = m.erase(ret);
}
return ret;
}
template <class T, class U, class V> auto upper_map(map<T, U> &m, V k) {
auto ret = m.upper_bound(k);
while (ret != m.end() && (*ret).second == 0) {
ret = m.erase(ret);
}
return ret;
}
// 存在しなければエラー
template <class T, class U, class V> auto rlower_map(map<T, U> &m, V k) {
auto ret = upper_map(m, k);
assert(ret != m.begin());
ret--;
while (1) {
if ((*ret).second != 0)
break;
assert(ret != m.begin());
auto next = ret;
--next;
m.erase(ret);
ret = next;
}
return ret;
}
template <class T, class U, class V> auto rupper_map(map<T, U> &m, V k) {
auto ret = lower_map(m, k);
assert(ret != m.begin());
ret--;
while (1) {
if ((*ret).second != 0)
break;
assert(ret != m.begin());
auto next = ret;
--next;
m.erase(ret);
ret = next;
}
return ret;
}
template <class... T> void fin(T... s) {
out(s...);
exit(0);
}
// 便利 数学 math
// sub ⊂ top
bool subsup(int sub, int top) { return (sub & top) == sub; }
//-180 ~ 180 degree
double atand(double h, double w) { return atan2(h, w) / PI * 180; }
double degree(double theta) { return theta / PI * 180; }
//% -mの場合、最小の正の数を返す
ll mod(ll a, ll m) {
if (m < 0)
m *= -1;
return (a % m + m) % m;
}
// ll pow(ll a) { return a * a; };
template <class T> T fact(int v) {
static vector<T> fact(2, 1);
if (sz(fact) <= v) {
rep(i, sz(fact), v + 1) { fact.emplace_back(fact.back() * i); }
}
return fact[v];
}
ll comi(ll n, ll r) {
assert(n < 100);
static vvi(pas, 100, 100);
if (pas[0][0])
return pas[n][r];
pas[0][0] = 1;
rep(i, 1, 100) {
pas[i][0] = 1;
rep(j, 1, i + 1) pas[i][j] = pas[i - 1][j - 1] + pas[i - 1][j];
}
return pas[n][r];
}
// 二項係数の偶奇を返す
int com_mod2(int n, int r) { return n == (r | (n - r)); }
double comd2(ll n, ll r) {
static vvd(comb, 2020, 2020);
if (comb[0][0] == 0) {
comb[0][0] = 1;
rep(i, 2000) {
comb[i + 1][0] = 1;
rep(j, 1, i + 2) { comb[i + 1][j] = comb[i][j] + comb[i][j - 1]; }
}
}
return comb[n][r];
}
double comd(int n, int r) {
if (r < 0 || r > n)
return 0;
if (n < 2020)
return comd2(n, r);
static vd fact(2, 1);
if (sz(fact) <= n) {
rep(i, sz(fact), n + 1) { fact.push_back(fact.back() * i); }
}
return fact[n] / fact[n - r] / fact[r];
}
#define gcd my_gcd
template <class T, class U, class V = typename max_type<T, U>::type>
V gcd(T a_, U b_) {
V a = a_;
V b = b_;
while (b)
a %= b, swap(a, b);
return abs(a);
}
template <class T> T gcd(const vector<T> &b) {
T res = b[0];
rep(i, 1, sz(b)) res = gcd(b[i], res);
return res;
}
#define lcm my_lcm
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll lcm(vi a) {
ll res = a[0];
rep(i, 1, sz(a)) res = lcm(a[i], res);
return res;
}
ll ceil(ll a, ll b) {
if (b == 0) {
debugline("ceil");
deb(a, b);
ole();
return -1;
} else if (a < 0) {
return 0;
} else {
return (a + b - 1) / b;
}
}
#define hypot my_hypot
double hypot(double dx, double dy) { return std::sqrt(dx * dx + dy * dy); }
ll sig0(int t) { return t <= 0 ? 0 : ((1 + t) * t) >> 1; }
bint sig0(bint t) { return t <= 0 ? 0 : ((1 + t) * t) >> 1; }
// ll sig(ll s, ll t) { return ((s + t) * (t - s + 1)) >> 1; }
ll sig0(ll s, ll t) {
if (s > t)
swap(s, t);
return sig0(t - s) + s * (t - s + 1);
}
#define tousa_i tosa_i
#define lower_tousa_i lower_tosa_i
#define upper_tousa upper_tosa
#define upper_tousa_i upper_tosa_i
ll tosa_i(ll st, ll ad, ll v) {
assert(((v - st) % ad) == 0);
return (v - st) / ad;
}
ll tosa_s(ll st, ll ad, ll len) { return st * len + sig0(len - 1) * ad; }
// ax + r (x は非負整数) で表せる整数のうち、v 以上となる最小の整数
ll lower_tosa(ll st, ll ad, ll v) {
if (st >= v)
return st;
return (v - st + ad - 1) / ad * ad + st;
}
// 第何項か
ll lower_tosa_i(ll st, ll ad, ll v) {
if (st >= v)
return 0;
return (v - st + ad - 1) / ad;
}
ll upper_tosa(ll st, ll ad, ll v) { return lower_tosa(st, ad, v + 1); }
ll upper_tosa_i(ll st, ll ad, ll v) { return lower_tosa_i(st, ad, v + 1); }
// b * res <= aを満たす [l, r)を返す div
P drange_ika(int a, int b) {
P null_p = mp(linf, linf);
if (b == 0) {
if (a >= 0) {
return mp(-linf, linf + 1) /*全て*/;
} else {
return null_p /*無い*/;
}
} else {
if (a >= 0) {
if (b > 0) {
return mp(-linf, a / b + 1);
} else {
return mp(-(a / -b), linf + 1);
}
} else {
if (b > 0) {
return mp(-linf, -ceil(-a, b) + 1);
} else {
return mp(ceil(-a, -b), linf + 1);
}
}
}
}
// v * v >= aとなる最小のvを返す
ll sqrt(ll a) {
if (a < 0) {
debugline("sqrt");
deb(a);
ole();
}
ll res = (ll)std::sqrt(a);
while (res * res < a)
++res;
return res;
}
double log(double e, double x) { return log(x) / log(e); }
/*@formatter:off*/
// 機能拡張
#define dtie(a, b) \
int a, b; \
tie(a, b)
template <class T, class U> string to_string(T a, U b) {
string res = "";
res += a;
res += b;
return res;
}
template <class T, class U, class V> string to_string(T a, U b, V c) {
string res = "";
res += a;
res += b;
res += c;
return res;
}
template <class T, class U, class V, class W>
string to_string(T a, U b, V c, W d) {
string res = "";
res += a;
res += b;
res += c;
res += d;
return res;
}
template <class T, class U, class V, class W, class X>
string to_string(T a, U b, V c, W d, X e) {
string res = "";
res += a;
res += b;
res += c;
res += d;
res += e;
return res;
}
// todo stringもセットで
// find_ifはsubrを使う
string sub(string &A, int l = -1, int r = -1) {
set_lr12(l, r, sz(A));
assert(0 <= l && l <= r && r <= sz(A));
return A.substr(l, r - l);
}
template <class T> vector<T> sub(const vector<T> &A, int l, int r) {
assert(0 <= l && l <= r && r <= sz(A));
vector<T> ret(r - l);
std::copy(A.begin() + l, A.begin() + r, ret.begin());
return ret;
}
template <class T> vector<T> sub(const vector<T> &A, int r) {
return sub(A, 0, r);
}
#if __cplusplus >= 201703L
template <class I, require_t(has_value_type<I>::value)>
I subn(const I &A, int l, int len) {
return sub(A, l, l + len);
}
#endif
template <class T, class F>
// sub2で呼ぶ
vector<T> sub(const vector<vector<T>> &A, int h, int w, int ah, int aw, F f) {
vector<T> res;
while (0 <= h && h < sz(A) && 0 <= w && w < sz(A[h]) && f(A[h][w])) {
res.emplace_back(A[h][w]);
h += ah;
w += aw;
}
return res;
}
template <class T>
vector<T> sub(const vector<vector<T>> &A, int h, int w, int ah, int aw) {
return sub(A, h, w, ah, aw, [&](T v) { return true; });
}
// range_nowを返す(find_ifでしか使われない)
#if __cplusplus >= 201703L
template <class T> auto subr(const vector<T> &A, int l) {
return range_now(vector<T>(A.begin() + l, A.end()), l);
}
#endif
#define sub25(A, h, w, ah, aw) sub(A, h, w, ah, aw)
#define sub26(A, h, w, ah, aw, siki_r) \
sub(A, h, w, ah, aw, [&](auto v) { return v siki_r; })
#define sub27(A, h, w, ah, aw, v, siki) \
sub(A, h, w, ah, aw, [&](auto v) { return siki; })
#define sub2(...) over7(__VA_ARGS__, sub27, sub26, sub25)(__VA_ARGS__)
constexpr int bsetlen = 50001;
// constexpr int bsetlen = 5050;
#define bset bitset<bsetlen>
bool operator<(bitset<bsetlen> &a, bitset<bsetlen> &b) {
rer(i, bsetlen - 1) {
if (a[i] < b[i])
return true;
if (a[i] > b[i])
return false;
}
return false;
}
bool operator>(bitset<bsetlen> &a, bitset<bsetlen> &b) {
rer(i, bsetlen - 1) {
if (a[i] > b[i])
return true;
if (a[i] < b[i])
return false;
}
return false;
}
bool operator<=(bitset<bsetlen> &a, bitset<bsetlen> &b) {
rer(i, bsetlen - 1) {
if (a[i] < b[i])
return true;
if (a[i] > b[i])
return false;
}
return true;
}
bool operator>=(bitset<bsetlen> &a, bitset<bsetlen> &b) {
rer(i, bsetlen - 1) {
if (a[i] > b[i])
return true;
if (a[i] < b[i])
return false;
}
return true;
}
string operator~(string &a) {
string res = a;
for (auto &&c : res) {
if (c == '0')
c = '1';
else if (c == '1')
c = '0';
else {
cerr << "cant ~" << a << "must bit" << endl;
exit(0);
}
}
return res;
}
ostream &operator<<(ostream &os, bset &a) {
bitset<10> b;
vi list;
rep(i, bsetlen) {
if (a[i])
list.push_back(i), b[i] = 1;
}
os << b << ", " << list;
return os;
}
int hbiti(bset &a) {
rer(i, bsetlen) {
if (a[i])
return i;
}
return -1;
}
#define hk(a, b, c) (a <= b && b < c)
// O(N/64)
bset nap(bset &a, int v) {
bset r = a | a << v;
return r;
}
bset nap(bset &a, bset &v) {
bset r = a;
rep(i, bsetlen) {
if (v[i])
r |= a << i;
}
return r;
}
template <class T> int count(set<T> &S, T l, T r) {
assert(l < r);
auto it = S.lower_bound(l);
return it != S.end() && (*it) < r;
}
// template<class T> void seth(vector<vector<T>> &S, int w, vector<T> &v)
// {assert(sz(S) == sz(v));assert(w < sz(S[0]));rep(h, sz(S)) { S[h][w] = v[h];
// }}
template <class T> vector<T> geth(vector<vector<T>> &S, int w) {
assert(w < sz(S[0]));
vector<T> ret(sz(S));
rep(h, sz(S)) { ret[h] = S[h][w]; }
return ret;
}
// vector<bool>[i]は参照を返さないため、こうしないとvb[i] |=
// trueがコンパイルエラー
vb::reference operator|=(vb::reference a, bool b) { return a = a | b; }
vb::reference operator&=(vb::reference a, bool b) { return a = a & b; }
template <class T, class U>
void operator+=(pair<T, U> &a, const pair<T, U> &b) {
a.fi += b.fi;
a.se += b.se;
}
template <class T, class U>
pair<T, U> operator+(const pair<T, U> &a, const pair<T, U> &b) {
return pair<T, U>(a.fi + b.fi, a.se + b.se);
}
template <class T, class U> pair<T, U> operator*(const pair<T, U> &a, int b) {
return pair<T, U>(a.fi * b, a.se * b);
}
template <class T, class U>
pair<T, U> operator-(const pair<T, U> &a, const pair<T, U> &b) {
return pair<T, U>(a.fi - b.fi, a.se - b.se);
}
template <class T, class U> pair<T, U> operator-(const pair<T, U> &a) {
return pair<T, U>(-a.first, -a.second);
}
template <typename CharT, typename Traits, typename Alloc>
basic_string<CharT, Traits, Alloc>
operator+(const basic_string<CharT, Traits, Alloc> &lhs, const int rhs) {
#ifdef _DEBUG
static bool was = false;
if (!was)
message += "str += 65 is 'A' not \"65\" ";
was = true;
#endif
return lhs + (char)rhs;
}
template <typename CharT, typename Traits, typename Alloc>
basic_string<CharT, Traits, Alloc>
operator+(const int lhs, const basic_string<CharT, Traits, Alloc> &rhs) {
#ifdef _DEBUG
static bool was = false;
if (!was)
message += "str += 65 is 'A' not \"65\" ";
was = true;
#endif
return (char)lhs + rhs;
}
template <typename CharT, typename Traits, typename Alloc>
void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const int rv) {
lhs = lhs + rv;
}
template <typename CharT, typename Traits, typename Alloc>
basic_string<CharT, Traits, Alloc>
operator+(const basic_string<CharT, Traits, Alloc> &lhs, const signed rv) {
const int rv2 = rv;
return lhs + rv2;
}
template <typename CharT, typename Traits, typename Alloc>
void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const signed rv) {
const int v = rv;
lhs += v;
}
template <typename CharT, typename Traits, typename Alloc>
void operator*=(basic_string<CharT, Traits, Alloc> &s, int num) {
auto bek = s;
s = "";
for (; num; num >>= 1) {
if (num & 1) {
s += bek;
}
bek += bek;
}
}
template <class T, class U> void operator+=(queue<T> &a, const U &v) {
a.push(v);
}
template <class T, class U> void operator+=(deque<T> &a, const U &v) {
a.push_back(v);
}
template <class T>
priority_queue<T, vector<T>, greater<T>> &
operator+=(priority_queue<T, vector<T>, greater<T>> &a, vector<T> &v) {
fora(d, v) { a.push(d); }
return a;
}
template <class T, class U>
priority_queue<T, vector<T>, greater<T>> &
operator+=(priority_queue<T, vector<T>, greater<T>> &a, const U &v) {
a.push(v);
return a;
}
template <class T, class U>
priority_queue<T> &operator+=(priority_queue<T> &a, const U &v) {
a.push(v);
return a;
}
template <class T> set<T> &operator+=(set<T> &a, const vector<T> &v) {
fora(d, v) { a.insert(d); }
return a;
}
template <class T, class U> auto operator+=(set<T> &a, const U &v) {
return a.insert(v);
}
template <class T, class U> auto operator-=(set<T> &a, const U &v) {
return a.erase(v);
}
template <class T, class U> auto operator+=(mset<T> &a, const U &v) {
return a.insert(v);
}
template <class T, class U>
set<T, greater<T>> &operator+=(set<T, greater<T>> &a, const U &v) {
a.insert(v);
return a;
}
template <class T, class U> vector<T> &operator+=(vector<T> &a, const U &v) {
a.push_back(v);
return a;
}
template <class T, class U> vector<T> operator+(U v, const vector<T> &a) {
vector<T> ret = a;
ret.insert(ret.begin(), v);
return ret;
}
template <class T> vector<T> operator+(const vector<T> &a, const vector<T> &b) {
vector<T> ret;
ret = a;
fora(v, b) { ret += v; }
return ret;
}
template <class T> vector<T> &operator+=(vector<T> &a, const vector<T> &b) {
rep(i, sz(b)) { /*こうしないとa+=aで両辺が増え続けてバグる*/
a.push_back(b[i]);
}
return a;
}
template <class T, class U> map<T, U> &operator+=(map<T, U> &a, map<T, U> &b) {
for (auto &&bv : b) {
a[bv.first] += bv.second;
}
return a;
}
template <class T, class U>
vector<T> operator+(const vector<T> &a, const U &v) {
vector<T> ret = a;
ret += v;
return ret;
}
template <class T, class U> auto operator+=(uset<T> &a, U v) {
return a.insert(v);
}
template <class T> vector<T> operator%(vector<T> &a, int v) {
vi ret(sz(a));
rep(i, sz(a)) { ret[i] = a[i] % v; }
return ret;
}
template <class T> vector<T> operator%=(vector<T> &a, int v) {
rep(i, sz(a)) { a[i] %= v; }
return a;
}
vi operator&(vi &a, vi &b) {
assert(sz(a) == sz(b));
vi ret(sz(a));
rep(i, sz(a)) { ret[i] = min(a[i], b[i]); }
return ret;
}
template <class T> void operator+=(mset<T> &a, vector<T> &v) {
for (auto &&u : v)
a.insert(u);
}
template <class T> void operator+=(set<T> &a, vector<T> &v) {
for (auto &&u : v)
a.insert(u);
}
template <class T> void operator+=(vector<T> &a, set<T> &v) {
for (auto &&u : v)
a.emplace_back(u);
}
template <class T> void operator+=(vector<T> &a, mset<T> &v) {
for (auto &&u : v)
a.emplace_back(u);
}
template <class T> vector<T> &operator-=(vector<T> &a, vector<T> &b) {
if (sz(a) != sz(b)) {
debugline("vector<T> operator-=");
deb(a);
deb(b);
exit(0);
}
rep(i, sz(a)) a[i] -= b[i];
return a;
}
template <class T> vector<T> operator-(vector<T> &a, vector<T> &b) {
if (sz(a) != sz(b)) {
debugline("vector<T> operator-");
deb(a);
deb(b);
ole();
}
vector<T> res(sz(a));
rep(i, sz(a)) res[i] = a[i] - b[i];
return res;
}
// template<class T, class U> void operator*=(vector<T> &a, U b) { vector<T>
// ta = a; rep(b-1){ a+=ta; }}
template <typename T> void erase(T &v, unsigned ll i) {
v.erase(v.begin() + i);
}
template <typename T> void erase(T &v, unsigned ll s, unsigned ll e) {
v.erase(v.begin() + s, v.begin() + e);
}
template <class T, class... I> auto erased(T &v, I... arg) {
auto ret = v;
erase(ret, arg...);
return ret;
}
template <typename T> void pop_front(vector<T> &v) { erase(v, 0); }
template <typename T> void entry(vector<T> &v, unsigned ll s, unsigned ll e) {
erase(v, e, sz(v));
erase(v, 0, s);
}
template <class T, class U> void erase(map<T, U> &m, ll okl, ll ngr) {
m.erase(m.lower_bound(okl), m.lower_bound(ngr));
}
template <class T> void erase(set<T> &m, ll okl, ll ngr) {
m.erase(m.lower_bound(okl), m.lower_bound(ngr));
}
template <typename T> void erasen(vector<T> &v, unsigned ll s, unsigned ll n) {
v.erase(v.begin() + s, v.begin() + s + n);
}
template <typename T, typename U>
void insert(vector<T> &v, unsigned ll i, U t) {
v.insert(v.begin() + i, t);
}
template <typename T, typename U> void push_front(vector<T> &v, U t) {
v.insert(v.begin(), t);
}
template <typename T, typename U>
void insert(vector<T> &v, unsigned ll i, vector<T> list) {
for (auto &&va : list)
v.insert(v.begin() + i++, va);
}
vector<string> split(const string &a, const char deli) {
string b = a + deli;
ll l = 0, r = 0, n = b.size();
vector<string> res;
rep(i, n) {
if (b[i] == deli) {
r = i;
if (l < r)
res.push_back(b.substr(l, r - l));
l = i + 1;
}
}
return res;
}
vector<string> split(const string &a, const string deli) {
vector<string> res;
ll kn = sz(deli);
std::string::size_type Pos(a.find(deli));
ll l = 0;
while (Pos != std::string::npos) {
if (Pos - l)
res.push_back(a.substr(l, Pos - l));
l = Pos + kn;
Pos = a.find(deli, Pos + kn);
}
if (sz(a) - l)
res.push_back(a.substr(l, sz(a) - l));
return res;
}
ll stoi(string &s) { return stol(s); }
#define assert_yn(yn_v, v) \
; \
assert(yn_v == 0 || yn_v == v); \
yn_v = v;
// 不完全な対策、現状はautohotkeyで対応
int yn_v = 0;
void yn(bool a) {
assert_yn(yn_v, 1);
if (a)
cout << "yes" << endl;
else
cout << "no" << endl;
}
void fyn(bool a) {
assert_yn(yn_v, 1);
yn(a);
exit(0);
}
void Yn(bool a) {
assert_yn(yn_v, 2);
if (a)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
void fYn(bool a) {
assert_yn(yn_v, 2);
Yn(a);
exit(0);
}
void YN(bool a) {
assert_yn(yn_v, 3);
if (a)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
void fYN(bool a) {
assert_yn(yn_v, 3);
YN(a);
exit(0);
}
int ab_v = 0;
void fAb(bool a) {
assert_yn(ab_v, 1);
if (a)
cout << "Alice" << endl;
else
cout << "Bob";
}
void fAB(bool a) {
assert_yn(yn_v, 2);
if (a)
cout << "ALICE" << endl;
else
cout << "BOB";
}
int pos_v = 0;
void Possible(bool a) {
assert_yn(pos_v, 1);
if (a)
cout << "Possible" << endl;
else
cout << "Impossible" << endl;
exit(0);
}
void POSSIBLE(bool a) {
assert_yn(pos_v, 2);
if (a)
cout << "POSSIBLE" << endl;
else
cout << "IMPOSSIBLE" << endl;
exit(0);
}
void fPossible(bool a) {
assert_yn(pos_v, 1) Possible(a);
exit(0);
}
void fPOSSIBLE(bool a) {
assert_yn(pos_v, 2) POSSIBLE(a);
exit(0);
}
template <typename T> class fixed_point : T {
public:
explicit constexpr fixed_point(T &&t) noexcept : T(std::forward<T>(t)) {}
template <typename... Args>
constexpr decltype(auto) operator()(Args &&...args) const {
return T::operator()(*this, std::forward<Args>(args)...);
}
};
template <typename T>
static inline constexpr decltype(auto) fix(T &&t) noexcept {
return fixed_point<T>{std::forward<T>(t)};
}
// 未分類
// 0,2,1 1番目と2番目の次元を入れ替える
template <class T>
auto irekae(vector<vector<vector<T>>> &A, int x, int y, int z) {
#define irekae_resize_loop(a, b, c) \
resize(res, a, b, c); \
rep(i, a) rep(j, b) rep(k, c)
vector<vector<vector<T>>> res;
if (x == 0 && y == 1 && z == 2) {
res = A;
} else if (x == 0 && y == 2 && z == 1) {
irekae_resize_loop(sz(A), sz(A[0][0]), sz(A[0])) {
res[i][j][k] = A[i][k][j];
}
} else if (x == 1 && y == 0 && z == 2) {
irekae_resize_loop(sz(A[0]), sz(A), sz(A[0][0])) {
res[i][j][k] = A[j][i][k];
}
} else if (x == 1 && y == 2 && z == 0) {
irekae_resize_loop(sz(A[0]), sz(A[0][0]), sz(A)) {
res[i][j][k] = A[k][i][j];
}
} else if (x == 2 && y == 0 && z == 1) {
irekae_resize_loop(sz(A[0][0]), sz(A), sz(A[0])) {
res[i][j][k] = A[j][k][i];
}
} else if (x == 2 && y == 1 && z == 0) {
irekae_resize_loop(sz(A[0][0]), sz(A[0]), sz(A)) {
res[i][j][k] = A[k][j][i];
}
}
return res;
#undef irekae_resize_loop
}
template <class T> auto irekae(vector<vector<T>> &A, int i = 1, int j = 0) {
vvt(res, sz(A[0]), sz(A));
rep(i, sz(A)) {
rep(j, sz(A[0])) { res[j][i] = A[i][j]; }
}
return res;
}
// tou分割する
template <typename Iterable, require_t(has_value_type<Iterable>::value)>
vector<Iterable> cut(const Iterable &a, int tou = 2) {
int N = sz(a);
vector<Iterable> res(tou);
int hab = N / tou;
vi lens(tou, hab);
rep(i, N % tou) { lens[tou - 1 - i]++; }
int l = 0;
rep(i, tou) {
int len = lens[i];
int r = l + len;
res[i].resize(len);
std::copy(a.begin() + l, a.begin() + r, res[i].begin());
l = r;
}
return res;
}
// 長さn毎に分割する
template <typename Iterable>
vector<Iterable> cut_n(const Iterable &a, int len) {
int N = sz(a);
vector<Iterable> res(ceil(N, len));
vi lens(N / len, len);
if (N % len)
lens.push_back(N % len);
int l = 0;
rep(i, sz(lens)) {
int len = lens[i];
int r = l + len;
res[i].resize(len);
std::copy(a.begin() + l, a.begin() + r, res[i].begin());
l = r;
}
return res;
}
vi cut(int N, int K) {
vi res(K);
int lim = K;
rep(i, lim) {
res[i] = N / K;
N -= res[i];
K--;
}
return res;
}
// 縦を返す
vi &geth(vvi() & a, int w) {
static vi ret;
ret.resize(sz(a));
rep(i, sz(a)) { ret[i] = a[i][w]; }
return ret;
}
//@起動時
struct initon {
initon() {
cin.tie(0);
ios::sync_with_stdio(false);
cout.setf(ios::fixed);
cout.precision(16);
srand((unsigned)clock() + (unsigned)time(NULL));
};
} initonv;
// #define pre prev
// #define nex next
// gra mll pr
// 上下左右
const string udlr = "udlr";
string UDLR = "UDLR"; // x4と連動 UDLR.find('U') := x4[0]
vc atoz = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
vc AtoZ = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
// 右、上が正
constexpr ll h4[] = {1, -1, 0, 0};
constexpr ll w4[] = {0, 0, -1, 1};
constexpr ll h8[] = {0, 1, 0, -1, -1, 1, 1, -1};
constexpr ll w8[] = {1, 0, -1, 0, 1, -1, 1, -1};
int mei_inc(int h, int w, int H, int W, int i) {
while (++i < 4) {
if (inside(h + h4[i], w + w4[i], H, W))
return i;
}
return i;
}
#define mei(nh, nw, h, w) \
for (int i = mei_inc(h, w, H, W, -1), nh = i < 4 ? h + h4[i] : 0, \
nw = i < 4 ? w + w4[i] : 0; \
i < 4; i = mei_inc(h, w, H, W, i), nh = h + h4[i], nw = w + w4[i])
int mei_inc8(int h, int w, int H, int W, int i) {
while (++i < 8) {
if (inside(h + h8[i], w + w8[i], H, W))
return i;
}
return i;
}
#define mei8(nh, nw, h, w) \
for (int i = mei_inc8(h, w, H, W, -1), nh = i < 8 ? h + h8[i] : 0, \
nw = i < 8 ? w + w8[i] : 0; \
i < 8; i = mei_inc8(h, w, H, W, i), nh = h + h8[i], nw = w + w8[i])
int mei_incv(int h, int w, int H, int W, int i, vp &p) {
while (++i < sz(p)) {
if (inside(h + p[i].fi, w + p[i].se, H, W))
return i;
}
return i;
}
#define meiv(nh, nw, h, w, p) \
for (int i = mei_incv(h, w, H, W, -1, p), nh = i < sz(p) ? h + p[i].fi : 0, \
nw = i < sz(p) ? w + p[i].se : 0; \
i < sz(p); \
i = mei_incv(h, w, H, W, i, p), nh = h + p[i].fi, nw = w + p[i].se)
// H*Wのグリッドを斜めに分割する
// 右上
vector<vp> naname_list_ne(int H, int W) {
vector<vp> res(H + W - 1);
rep(sh, H) {
int sw = 0;
res[sh] += mp(sh, sw);
int nh = sh;
int nw = sw;
while (1) {
nh--;
nw++;
if (0 <= nh && nw < W) {
res[sh] += mp(nh, nw);
} else {
break;
}
}
}
rep(sw, 1, W) {
int sh = H - 1;
res[H + sw - 1] += mp(sh, sw);
int nh = sh;
int nw = sw;
while (1) {
nh--;
nw++;
if (0 <= nh && nw < W) {
res[H + sw - 1] += mp(nh, nw);
} else {
break;
}
}
}
return res;
}
// 右下
vector<vp> naname_list_se(int H, int W) {
vector<vp> res(H + W - 1);
rep(sh, H) {
int sw = 0;
res[sh] += mp(sh, sw);
int nh = sh;
int nw = sw;
while (1) {
nh++;
nw++;
if (0 <= nh && nh < H && nw < W) {
res[sh] += mp(nh, nw);
} else {
break;
}
}
}
rep(sw, 1, W) {
int sh = 0;
res[H + sw - 1] += mp(sh, sw);
int nh = sh;
int nw = sw;
while (1) {
nh++;
nw++;
if (0 <= nh && nh < H && nw < W) {
res[H + sw - 1] += mp(nh, nw);
} else {
break;
}
}
}
return res;
}
// グラフ内で #undef getid
// #define getidとしているため、ここを書き直したらgraphも書き直す
#define getid_2(h, w) ((h) * (W) + (w))
#define getid_1(p) ((p).first * W + (p).second)
#define getid(...) over2(__VA_ARGS__, getid_2, getid_1)(__VA_ARGS__)
#define getp(id) mp((id) / W, (id) % W)
#define set_shuffle() \
std::random_device seed_gen; \
std::mt19937 engine(seed_gen())
#define shuffle(a) std::shuffle((a).begin(), (a).end(), engine);
// 1980 開始からtime ms経っていたらtrue
vb bit_bool(int v, int len) {
assert(bit(len) > v);
vb ret(len);
rep(i, len) { ret[i] = bget(v, i); }
return ret;
}
vi tov(vb &a) {
vi ret;
rep(i, sz(a)) {
if (a[i])
ret.push_back(i);
}
return ret;
}
bool kaibun(const str &S) { return S == rev(S); }
template <class T> vector<T> repeat(const vector<T> &A, int kaisu) {
vector<T> ret;
while (kaisu--) {
ret += A;
}
return ret;
}
#define rge range
#define upd update
// S[{s, t, d}]
#define strs slice_str
struct slice_str {
string S;
slice_str() {}
slice_str(const string &S) : S(S) {}
slice_str(int len, char c) : S(len, c) {}
auto size() { return S.size(); }
char &operator[](int p) { return S[p]; }
string operator[](initializer_list<int> p) {
if (sz(p) == 1) {
return S.substr(0, *(p.begin()));
} else if (sz(p) == 2) {
int l = *(p.begin());
int r = *(next(p.begin()));
return S.substr(l, r - l);
} else {
auto it = p.begin();
int s = *(it++);
int t = *(it++);
int d = *(it);
if (d == -1) {
int s_ = sz(S) - s - 1;
int t_ = sz(S) - t - 1;
return rev(S).substr(s_, t_ - s_);
} else if (d < 0) {
t = max(-1ll, t);
string ret;
while (s > t) {
ret += S[s];
s += d;
}
return ret;
} else {
t = min(sz(S), t);
string ret;
while (s < t) {
ret += S[s];
s += d;
}
return ret;
}
}
}
operator string &() { return S; }
template <class T> void operator+=(const T &a) { S += a; }
bool operator==(const slice_str &rhs) { return S == rhs.S; }
auto pop_back() { return S.pop_back(); }
};
ostream &operator<<(ostream &os, const slice_str &a) {
os << a.S;
return os;
}
istream &operator>>(istream &iss, slice_str &a) {
iss >> a.S;
return iss;
}
template <class T> bool can(const T &v, int i) { return 0 <= i && i < sz(v); }
#if __cplusplus >= 201703L
// template<class T> auto sum(int a, T v...) {return (v + ... + 0);}
#endif
#define VEC vector
// string.substr()は長さを指定するため、間違えやすい
#define substr my_substr
template <class T> struct my_set : public set<T> {
template <class... Arg> my_set(Arg... arg) : set<T>(arg...) {}
T back() { return *(set<T>::rbegin()); }
T front() { return *(set<T>::begin()); }
};
#define set my_set
#endif /*UNTITLED15_TEMPLATE_H*/
#endif
// † ←template終了
/*@formatter:on*/
// vectorで取れる要素数
// bool=> 1e9 * 8.32
// int => 1e8 * 2.6
// ll => 1e8 * 1.3
// 3次元以上取るとメモリがヤバい
// static配列を使う
vvc(ba);
ll N, M, H, W;
vi A, B, C;
void solve() {
in(N);
na(A, N);
vi cand;
int want = ruix(sub(A, 2, N)).back();
int a = A[0];
int b = A[1];
rep(x, min(A[0], bit(20))) {
int xo = (a - x) ^ (b + x) ^ want;
if ((xo & mask(20)) == 0) {
deb(x);
deb2(x);
cand += x;
}
}
int res = linf;
fora(can, cand) {
rep(x, can, a, bit(20)) {
int xo = (a - x) ^ (b + x) ^ want;
if ((xo) == 0) {
chmi(res, x);
}
}
}
if (res != linf)
fin(res);
fin(-1);
}
auto my(ll n, vi &a) { return 0; }
auto sister(ll n, vi &a) {
ll ret = 0;
return ret;
}
signed main() {
solve();
#define arg n, a
#ifdef _DEBUG
bool bad = 0;
for (ll i = 0, ok = 1; i < k5 && ok; ++i) {
ll n = rand(1, 8);
vi a = ranv(n, 1, 10);
auto myres = my(arg);
auto res = sister(arg);
ok = myres == res;
if (!ok) {
out(arg);
cerr << "AC : " << res << endl;
cerr << "MY : " << myres << endl;
bad = 1;
break;
}
}
if (!bad) {
// solveを書き直す
// solveを呼び出す
}
exit(0);
#endif
return 0;
};
| /*temp*/
//
//
//
//
// #undef _DEBUG
// #pragma GCC optimize("Ofast")
// 不動小数点の計算高速化
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #include <boost/multiprecision/cpp_int.hpp>
#ifdef _DEBUG
#include "template.h"
#else
#if __cplusplus >= 201703L
/*Atcoderでしか使えない(c++17 && このテンプレートが使えるならAtcoder)*/
#include <boost/sort/pdqsort/pdqsort.hpp>
#define fast_sort boost::sort::pdqsort
#endif
#endif
#ifndef _DEBUG
#ifndef UNTITLED15_TEMPLATE_H
#define UNTITLED15_TEMPLATE_H
#ifdef _DEBUG
#include "bits_stdc++.h"
#else
#include <bits/stdc++.h>
#endif
#ifndef fast_sort
#define fast_sort sort
#endif
// #define use_pq
#define use_for
#define use_for_each
#define use_sort
#define use_fill
#define use_rand
#define use_mgr
#define use_rui
#define use_compress
//
//
//
//
//
//
#define use_pbds
#ifdef use_pbds
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace std::chrono;
template <class T, class U>
void operator+=(__gnu_pbds::priority_queue<T, greater<T>,
__gnu_pbds::rc_binomial_heap_tag> &a,
const U &v) {
a.push(v);
}
template <class T, class U>
void operator+=(
__gnu_pbds::priority_queue<T, less<T>, __gnu_pbds::rc_binomial_heap_tag> &a,
const U &v) {
a.push(v);
}
template <class T, class U, class W, class X>
auto count(__gnu_pbds::gp_hash_table<T, U, W> &a, X k) {
return a.find(k) != a.end();
}
#endif
/*@formatter:off*/
#define ll long long
using sig_dou = double;
// マクロ省略形 関数等
#define arsz(a) (sizeof(a) / sizeof(a[0]))
#define sz(a) ((ll)(a).size())
#define mp make_pair
#define mt make_tuple
#define pb pop_back
#define pf push_front
#define eb emplace_back
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
template <class T, class U> auto max(T a, U b) { return a > b ? a : b; }
template <class T, class U> auto min(T a, U b) { return a < b ? a : b; }
// optional<T>について下でオーバーロード(nullopt_tを左辺右辺について単位元として扱う)
template <class T, class U> bool chma(T &a, const U &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T, class U> bool chmi(T &a, const U &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
// メタ系 meta
template <typename T, typename U = typename T::value_type>
std::true_type value_type_tester(signed);
template <typename T> std::false_type value_type_tester(long);
template <typename T>
struct has_value_type : decltype(value_type_tester<T>(0)) {};
// A<T>でTを返す
template <class T> using decl_t = typename T::value_type;
// A<B<.....T>>でTを返す
template <class T, bool end = !has_value_type<T>::value> struct decl2_ {
typedef T type;
};
template <class T> struct decl2_<T, false> {
typedef typename decl2_<decl_t<T>>::type type;
};
template <class T> using decl2_t = typename decl2_<T>::type;
// #define decl_max(a, b) decltype(max(MAX<decltype(a)>(), MAX<decltype(b)>()))
#define is_same2(T, U) is_same<T, U>::value
template <class T> struct is_vector : std::false_type {};
template <class T> struct is_vector<std::vector<T>> : std::true_type {};
// 大きい型を返す max_type<int, char>::type
// todo mintがlong long より小さいと判定されるためバグる
template <class T1, class T2, bool t1_bigger = (sizeof(T1) > sizeof(T2))>
struct max_type {
typedef T1 type;
};
template <class T1, class T2> struct max_type<T1, T2, false> {
typedef T2 type;
};
template <class T> struct vec_rank : integral_constant<int, 0> {};
template <class T>
struct vec_rank<vector<T>> : integral_constant<int, vec_rank<T>{} + 1> {};
// N個のTを並べたtupleを返す
// tuple_n<3, int>::type tuple<int, int, int>
template <size_t N, class T, class... Arg> struct tuple_n {
typedef typename tuple_n<N - 1, T, T, Arg...>::type type;
};
template <class T, class... Arg> struct tuple_n<0, T, Arg...> {
typedef tuple<Arg...> type;
};
struct dummy_t1 {};
struct dummy_t2 {};
struct dummy_t3 {};
struct dummy_t4 {};
struct dummy_t5 {};
struct dummy_t6 {};
// template<class T, require(is_integral<T>::value)>など
#define require_t(bo) enable_if_t<bo> * = nullptr
// 複数でオーバーロードする場合、引数が同じだとうまくいかないため
// require_arg(bool, dummy_t1)
// require_arg(bool, dummy_t2)等とする
#define require_arg1(bo) enable_if_t<bo> * = nullptr
#define require_arg2(bo, dummy_type) enable_if_t<bo, dummy_type> * = nullptr
#define require_arg(...) \
over2(__VA_ARGS__, require_arg2, require_arg1)(__VA_ARGS__)
//->//enable_if_tのtを書き忘れそうだから
#define require_ret(bo, ret_type) enable_if_t<bo, ret_type>
#define int long long // todo 消したら動かない intの代わりにsignedを使う
auto start_time = system_clock::now();
auto past_time = system_clock::now();
#define debugName(VariableName) #VariableName
// 最大引数がN
#define over2(o1, o2, name, ...) name
#define over3(o1, o2, o3, name, ...) name
#define over4(o1, o2, o3, o4, name, ...) name
#define over5(o1, o2, o3, o4, o5, name, ...) name
#define over6(o1, o2, o3, o4, o5, o6, name, ...) name
#define over7(o1, o2, o3, o4, o5, o6, o7, name, ...) name
#define over8(o1, o2, o3, o4, o5, o6, o7, o8, name, ...) name
#define over9(o1, o2, o3, o4, o5, o6, o7, o8, o9, name, ...) name
#define over10(o1, o2, o3, o4, o5, o6, o7, o8, o9, o10, name, ...) name
#ifdef _DEBUG
string message;
string res_mes;
bool was_deb = false;
int cou_deb = 0;
[[noreturn]] void my_exit(signed e = 0) {
if (was_deb && sz(res_mes)) {
cerr << "result = " << endl << res_mes << endl;
}
if (sz(message)) {
cerr << "****************************" << endl;
cerr << "Note." << endl;
cerr << message << endl;
cerr << "****************************" << endl;
}
exit(e);
}
#define exit my_exit
#endif
void assert2(bool b, const string &s = "") {
if (!b) {
cerr << s << endl;
exit(1); /*assert(0);*/
}
}
// my_nulloptをあらゆる操作の単位元的な物として扱う
// vectorの参照外時に返したり、右辺値として渡されたときに何もしないなど
struct my_nullopt_t {
} my_nullopt;
#define nullopt_t my_nullopt_t
#define nullopt my_nullopt
/*@formatter:off*/
// 値が無いときは、setを使わない限り代入できない
//=を使っても無視される
template <class T> struct my_optional {
private:
bool is_null;
T v;
public:
typedef T value_type;
my_optional() : is_null(true) {}
my_optional(const nullopt_t &) : is_null(true) {}
my_optional(const T &v) : v(v), is_null(false) {}
bool has_value() const { return !is_null; }
T &value() {
static string mes = "optional has no value";
assert2(!is_null, mes);
return v;
}
const T &value() const {
static string mes = "optional has no value";
assert2(!is_null, mes);
return v;
}
void set(const T &nv) {
is_null = false;
v = nv;
}
template <class U> void operator=(const U &v) {
set(v); // null状態でも代入出来るようにした
// if (has_value())value() = v; else return;
}
template <class U> void operator=(const my_optional<U> &v) {
if (/*has_value() && */ v.has_value())
(*this) = v;
else
return;
}
/*@formatter:off*/
void reset() { is_null = true; }
void operator=(const nullopt_t &) { reset(); }
template <require_t(!is_same2(T, bool))> explicit operator bool() {
return !is_null;
}
// nullの時はエラー
operator T &() { return value(); }
operator const T &() const { return value(); }
my_optional<T> operator++() {
if (this->has_value()) {
this->value()++;
return *this;
} else {
return *this;
}
}
my_optional<T> operator++(signed) {
if (this->has_value()) {
auto tem = *this;
this->value()++;
return tem;
} else {
return *this;
}
}
my_optional<T> operator--() {
if (this->has_value()) {
this->value()--;
return *this;
} else {
return *this;
}
}
my_optional<T> operator--(signed) {
if (this->has_value()) {
auto tem = *this;
this->value()--;
return tem;
} else {
return *this;
}
}
};
template <class T> istream &operator>>(istream &iss, my_optional<T> &v) {
T val;
iss >> val;
v.set(val);
return iss;
}
#define optional my_optional
template <class T> using opt = my_optional<T>;
// template<class T, class A = std::allocator<T>> struct debtor : std::vector<T,
// A> {
template <class Key, class T, class Compare = less<Key>,
class Allocator = allocator<pair<const Key, T>>>
struct o_map : std::map<Key, optional<T>, Compare, Allocator> {
optional<T> emp;
o_map() : std::map<Key, optional<T>, Compare, Allocator>() {}
auto operator()(const nullopt_t &) { return nullopt; }
optional<T> &operator()(const optional<Key> &k) {
if (k.has_value()) {
return std::map<Key, optional<T>, Compare, Allocator>::operator[](
k.value());
} else {
emp.reset();
return emp;
}
}
optional<T> &operator()(const Key &k) {
auto &v = std::map<Key, optional<T>, Compare, Allocator>::operator[](k);
if (v.has_value())
return v;
else {
v.set(0);
return v;
}
}
template <class U> void operator[](U) {
static string mes = "s_map cant []";
assert2(0, mes);
}
};
// 以下、空のoptionalをnulloptと書く
// ov[-1(参照外)] でnulloptを返す
// ov[nullopt] で nulloptをかえす
template <class T> struct ov {
optional<T> emp;
vector<optional<T>> v;
ov(int i = 0, T val = 0) : v(i, val) {}
template <class U> ov(const U &rhs) {
v.resize(sz(rhs));
for (int i = 0; i < sz(rhs); i++)
v[i].set(rhs[i]);
}
optional<T> &operator()(int i) {
if (i < 0 || sz(v) <= i) {
emp.reset();
return emp;
} else {
return v[i];
}
}
optional<T> &operator()(const nullopt_t &) { return operator()(-1); }
optional<T> &operator()(const optional<T> &i) {
if (i.has_value())
return operator()(i.value());
else {
return operator()(-1);
}
}
/*@formatter:off*/
};
template <class T> string deb_tos(const ov<T> &v) { return deb_tos(v.v); }
// vectorに対しての処理は.vを呼ぶ
template <class T> class ovv {
optional<T> emp;
public:
vector<vector<optional<T>>> v;
ovv(int i = 0, int j = 0, T val = 0) : v(i, vector<optional<T>>(j, val)) {}
ovv(const vector<vector<T>> &S) {
v.resize(S.size());
for (int h = 0; h < sz(S); h++) {
v[h].resize(sz(S[h]));
for (int w = 0; w < sz(S[h]); w++) {
v[h][w].set(S[h][w]);
}
}
}
optional<T> &operator()(int i, int j) {
if (i < 0 || j < 0 || sz(v) <= i || sz(v[i]) <= j) {
emp.reset();
return emp;
} else {
return v[i][j];
}
}
// 再帰ver 遅いと思う
// optional<T>& gets(optional<T>& v){return v;}
// template<class V, class H, class... U> optional<T>& gets(V& v, H i, U...
// tail){ if constexpr(is_same2(H, nullopt_t))return
// operator()(-1,-1); else if constexpr(is_same2(H, optional<int>)){
// if(i.has_value())return gets(v[(int)i], tail...); else return
// operator()(-1,-1); }else if constexpr(is_integral<H>::value){
// return gets(v[(int)i], tail...); }else{ assert(0);
// return emp; } }
#if __cplusplus >= 201703L
// if constexprバージョン 上が遅かったらこれで
template <class U, class V>
optional<T> &operator()(const U &i, const V &j) { /*駄目な場合を除外*/
if constexpr (is_same2(U, nullopt_t) || is_same2(U, nullopt_t)) {
return operator()(-1, -1); /* o, o*/
} else if constexpr (is_same2(U, optional<int>) &&
is_same2(V, optional<int>)) {
return operator()(i.has_value() ? (int)i : -1,
j.has_value() ? (int)j : -1); /* o, x*/
} else if constexpr (is_same2(U, optional<int>)) {
return operator()(i.has_value() ? (int)i : -1, (int)j); /* x, o*/
} else if constexpr (is_same2(V, optional<int>)) {
return operator()((int)i, j.has_value() ? (int)j : -1); /* x, x*/
} else {
return operator()((int)i, (int)j);
}
}
#endif
operator const vector<vector<optional<T>>> &() { return v; }
};
template <class T> istream &operator>>(istream &iss, ovv<T> &a) {
for (int h = 0; h < sz(a); h++) {
for (int w = 0; w < sz(a[h]); w++) {
iss >> a.v[h][w];
}
}
return iss;
}
template <class T> ostream &operator<<(ostream &os, ovv<T> &a) {
os << a.v;
return os;
}
template <class T> string deb_tos(const ovv<T> &v) { return deb_tos(v.v); }
template <class T> struct ov3 {
optional<T> emp;
vector<vector<vector<optional<T>>>> v;
ov3(int i, int j, int k, T val = 0)
: v(i, vector<vector<optional<T>>>(j, vector<optional<T>>(k, val))) {}
optional<T> &operator()(int i, int j, int k) {
if (i < 0 || j < 0 || sz(v) <= i || sz(v[i]) <= j) {
if (k < 0 || sz(v[i][j]) <= k) {
emp.reset();
return emp;
}
}
return v[i][j][k];
}
private:
#if __cplusplus >= 201703L
// 再帰ver 遅いと思う
template <class V, class H> optional<T> &gets(V &nowv, H i) {
if constexpr (is_same2(H, nullopt_t)) {
emp.reset();
return emp;
} else if constexpr (is_same2(H, optional<int>)) {
if (i.has_value()) {
return nowv[(int)i];
} else {
emp.reset();
return emp;
}
} else if constexpr (is_integral<H>::value) {
return nowv[(int)i];
} else {
static string mes = "ov3 error not index";
assert2(0, mes);
emp.reset();
return emp;
}
}
// todo const &消した
template <class V, class H, class... U>
optional<T> &gets(V &nowv, H i, U... tail) {
if constexpr (is_same2(H, nullopt_t)) {
emp.reset();
return emp;
} else if constexpr (is_same2(H, optional<int>)) {
if (i.has_value()) {
return gets(nowv[(int)i], tail...);
} else {
emp.reset();
return emp;
}
} else if constexpr (is_integral<H>::value) {
return gets(nowv[(int)i], tail...);
} else {
static string mes = "ov3 error not index";
assert2(0, mes);
emp.reset();
return emp;
}
}
#endif
public:
template <class U, class V, class W> optional<T> &operator()(U i, V j, W k) {
return gets(v, i, j, k);
}
/*@formatter:off*/
};
template <class T> string deb_tos(const ov3<T> &v) { return deb_tos(v.v); }
// nullopt_t
// 優先順位
// null, [opt, tem]
// + と += は違う意味を持つ
// val+=null : val
// val+null : null
//
//+は途中計算
//+=は最終的に格納したい値にだけ持たせる
//+=がvoidを返すのは、途中計算で使うのを抑制するため
// nulloptを考慮する際、計算途中では+を使ってnulloptを作り
// 格納する際は+=で無効にする必要がある
// 演算子==
// optional<int>(10) == 10
// 全ての型に対応させ、value_typeが等しいかを見るようにするのもありかも
// null同士を比較する状況はおかしいのではないか
bool operator==(const nullopt_t &, const nullopt_t &) {
assert2(0, "nul == null cant hikaku");
return false;
}
template <class T> bool operator==(const nullopt_t &, const T &) {
return false;
}
template <class T> bool operator!=(const nullopt_t &, const T &) {
return true;
}
template <class T> bool operator==(const T &, const nullopt_t &) {
return false;
}
template <class T> bool operator!=(const T &, const nullopt_t &) {
return true;
}
// nullを
nullopt_t &operator+(const nullopt_t &, const nullopt_t &) { return nullopt; }
nullopt_t &operator-(const nullopt_t &, const nullopt_t &) { return nullopt; }
nullopt_t &operator*(const nullopt_t &, const nullopt_t &) { return nullopt; }
nullopt_t &operator/(const nullopt_t &, const nullopt_t &) { return nullopt; }
nullopt_t &operator+=(const nullopt_t &, const nullopt_t &) { return nullopt; }
nullopt_t &operator-=(const nullopt_t &, const nullopt_t &) { return nullopt; }
nullopt_t &operator*=(const nullopt_t &, const nullopt_t &) { return nullopt; }
nullopt_t &operator/=(const nullopt_t &, const nullopt_t &) { return nullopt; }
template <class ANY> nullopt_t operator+(const nullopt_t &, const ANY &) {
return nullopt;
}
template <class ANY> nullopt_t operator-(const nullopt_t &, const ANY &) {
return nullopt;
}
template <class ANY> nullopt_t operator*(const nullopt_t &, const ANY &) {
return nullopt;
}
template <class ANY> nullopt_t operator/(const nullopt_t &, const ANY &) {
return nullopt;
}
template <class ANY> nullopt_t operator+(const ANY &, const nullopt_t &) {
return nullopt;
}
template <class ANY> nullopt_t operator-(const ANY &, const nullopt_t &) {
return nullopt;
}
template <class ANY> nullopt_t operator*(const ANY &, const nullopt_t &) {
return nullopt;
}
template <class ANY> nullopt_t operator/(const ANY &, const nullopt_t &) {
return nullopt;
}
template <class ANY> void operator+=(nullopt_t &, const ANY &) {}
template <class ANY> void operator-=(nullopt_t &, const ANY &) {}
template <class ANY> void operator*=(nullopt_t &, const ANY &) {}
template <class ANY> void operator/=(nullopt_t &, const ANY &) {}
template <class ANY> void operator+=(ANY &, const nullopt_t &) {}
template <class ANY> void operator-=(ANY &, const nullopt_t &) {}
template <class ANY> void operator*=(ANY &, const nullopt_t &) {}
template <class ANY> void operator/=(ANY &, const nullopt_t &) {}
template <class T> struct is_optional : false_type {};
template <class T> struct is_optional<optional<T>> : true_type {};
template <class T, class U>
true_type both_optional(optional<T> t, optional<U> u);
false_type both_optional(...);
template <class T, class U>
class opt_check : public decltype(both_optional(declval<T>(), declval<U>())) {};
// optionalは同じ型同士しか足せない
//(o, t), (t, o), (o, o)
#define opt_tem(op) \
template <class O, class O_ret = decltype(declval<O>() op declval<O>())> \
optional<O_ret> operator op(const optional<O> &opt1, \
const optional<O> &opt2) { \
if (!opt1.has_value() || !opt2.has_value()) { \
return optional<O_ret>(); \
} else { \
return optional<O_ret>(opt1.value() op opt2.value()); \
} \
} \
template <class O, class T, \
class O_ret = decltype(declval<O>() op declval<O>())> \
auto operator op(const optional<O> &opt, const T &tem) \
->require_ret(!(opt_check<optional<O>, T>::value), optional<O_ret>) { \
if (!opt.has_value()) { \
return optional<O_ret>(); \
} else { \
return optional<O_ret>(opt.value() op tem); \
} \
} \
template <class O, class T, \
class O_ret = decltype(declval<O>() op declval<O>())> \
auto operator op(const T &tem, const optional<O> &opt) \
->require_ret(!(opt_check<optional<O>, T>::value), optional<O_ret>) { \
if (!opt.has_value()) { \
return optional<O_ret>(); \
} else { \
return optional<O_ret>(opt.value() op tem); \
} \
}
/*@formatter:off*/
opt_tem(+) opt_tem(-) opt_tem(*) opt_tem(/)
// 比較はoptional<bool>を返す
opt_tem(<) opt_tem(>) opt_tem(<=) opt_tem(>=)
/*@formatter:on*/ /*@formatter:off*/
template <class O, class T>
bool operator==(const optional<O> &opt, const T &tem) {
if (opt.has_value()) {
return opt.value() == tem;
} else
return nullopt == tem;
}
template <class O, class T>
bool operator!=(const optional<O> &opt, const T &tem) {
if (opt.has_value()) {
return opt.value() != tem;
} else
return nullopt != tem;
}
template <class O, class T>
bool operator==(const T &tem, const optional<O> &opt) {
if (opt.has_value()) {
return opt.value() == tem;
} else
return nullopt == tem;
}
template <class O, class T>
bool operator!=(const T &tem, const optional<O> &opt) {
if (opt.has_value()) {
return opt.value() != tem;
} else
return nullopt != tem;
}
template <class O>
bool operator==(const optional<O> &opt1, const optional<O> &opt2) {
if (opt1.has_value() != opt2.has_value()) {
return false;
} else if (opt1.has_value()) {
return opt1.value() == opt2.value();
} else {
return nullopt == nullopt;
}
}
template <class O>
bool operator!=(const optional<O> &opt1, const optional<O> &opt2) {
return !(opt1 == opt2);
}
//(a+=null) != (a=a+null)
// a null
template <class T, class O> void operator+=(T &tem, const optional<O> &opt) {
if (opt.has_value()) {
tem += opt.value();
}
}
template <class T, class O> void operator-=(T &tem, const optional<O> &opt) {
if (opt.has_value()) {
tem -= opt.value();
}
}
template <class T, class O> void operator*=(T &tem, const optional<O> &opt) {
if (opt.has_value()) {
tem *= opt.value();
}
}
template <class T, class O> void operator/=(T &tem, const optional<O> &opt) {
if (opt.has_value()) {
tem /= opt.value();
}
}
template <class T, class O> void operator+=(optional<O> &opt, const T &tem) {
if (opt.has_value()) {
opt.value() += tem;
}
}
template <class T, class O> void operator-=(optional<O> &opt, const T &tem) {
if (opt.has_value()) {
opt.value() -= tem;
}
}
template <class T, class O> void operator*=(optional<O> &opt, const T &tem) {
if (opt.has_value()) {
opt.value() *= tem;
}
}
template <class T, class O> void operator/=(optional<O> &opt, const T &tem) {
if (opt.has_value()) {
opt.value() /= tem;
}
}
//
template <class Ol, class Or>
void operator+=(optional<Ol> &opl, const optional<Or> &opr) {
if (opr.has_value()) {
return opl += opr.value();
}
}
template <class Ol, class Or>
void operator-=(optional<Ol> &opl, const optional<Or> &opr) {
if (opr.has_value()) {
return opl -= opr.value();
}
}
template <class Ol, class Or>
void operator*=(optional<Ol> &opl, const optional<Or> &opr) {
if (opr.has_value()) {
return opl *= opr.value();
}
}
template <class Ol, class Or>
void operator/=(optional<Ol> &opl, const optional<Or> &opr) {
if (opr.has_value()) {
return opl /= opr.value();
}
}
/*@formatter:off*/
template <class U> auto max(const nullopt_t &, const U &val) { return val; }
template <class U> auto max(const U &val, const nullopt_t &) { return val; }
template <class U> auto min(const nullopt_t &, const U &val) { return val; }
template <class U> auto min(const U &val, const nullopt_t &) { return val; }
template <class T, class U> auto max(const optional<T> &opt, const U &val) {
if (opt.has_value())
return max(opt.value(), val);
else
return val;
}
template <class T, class U> auto max(const U &val, const optional<T> &opt) {
if (opt.has_value())
return max(opt.value(), val);
else
return val;
}
template <class T, class U> auto min(const optional<T> &opt, const U &val) {
if (opt.has_value())
return min(opt.value(), val);
else
return val;
}
template <class T, class U> auto min(const U &val, const optional<T> &opt) {
if (opt.has_value())
return min(opt.value(), val);
else
return val;
}
// null , optional, T
bool chma(nullopt_t &, const nullopt_t &) { return false; }
template <class T> bool chma(T &opt, const nullopt_t &) { return false; }
template <class T> bool chma(nullopt_t &, const T &opt) { return false; }
template <class T> bool chma(optional<T> &olv, const optional<T> &orv) {
if (orv.has_value()) {
return chma(olv, orv.value());
} else
return false;
}
template <class T, class U> bool chma(optional<T> &opt, const U &rhs) {
if (opt.has_value()) {
return chma(opt.value(), rhs);
} else
return false;
}
template <class T, class U> bool chma(T &lhs, const optional<U> &opt) {
if (opt.has_value()) {
return chma(lhs, opt.value());
} else
return false;
}
bool chmi(nullopt_t &, const nullopt_t &) { return false; }
template <class T> bool chmi(T &opt, const nullopt_t &) { return false; }
template <class T> bool chmi(nullopt_t &, const T &opt) { return false; }
template <class T> bool chmi(optional<T> &olv, const optional<T> &orv) {
if (orv.has_value()) {
return chmi(olv, orv.value());
} else
return false;
}
template <class T, class U> bool chmi(optional<T> &opt, const U &rhs) {
if (opt.has_value()) {
return chmi(opt.value(), rhs);
} else
return false;
}
template <class T, class U> bool chmi(T &lhs, const optional<U> &opt) {
if (opt.has_value()) {
return chmi(lhs, opt.value());
} else
return false;
}
template <class T> ostream &operator<<(ostream &os, optional<T> p) {
if (p.has_value())
os << p.value();
else
os << "e";
return os;
}
template <class T> using opt = my_optional<T>;
struct xorshift {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(const uint64_t &x) const {
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
size_t operator()(const std::pair<ll, ll> &x) const {
ll v = ((x.first) << 32) | x.second;
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(v + FIXED_RANDOM);
}
size_t operator()(const std::tuple<ll, ll, ll> &x) const {
vector<ll> x2 = {get<0>(x), get<1>(x), get<2>(x)};
return operator()(x2);
}
size_t operator()(const std::tuple<ll, ll, ll, ll> &x) const {
vector<ll> x2 = {get<0>(x), get<1>(x), get<2>(x), get<3>(x)};
return operator()(x2);
}
size_t operator()(const std::tuple<ll, ll, ll, ll, ll> &x) const {
vector<ll> x2 = {get<0>(x), get<1>(x), get<2>(x), get<3>(x), get<4>(x)};
return operator()(x2);
}
template <class T, class U>
size_t operator()(const std::pair<T, U> &x) const {
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
uint64_t hasx = splitmix64(x.first);
uint64_t hasy = splitmix64(x.second + FIXED_RANDOM);
return hasx ^ hasy;
}
template <class T> size_t operator()(const vector<T> &x) const {
uint64_t has = 0;
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
uint64_t rv = splitmix64(FIXED_RANDOM);
for (int i = 0; i < sz(x); i++) {
uint64_t v = splitmix64(x[i] + rv);
has ^= v;
rv = splitmix64(rv);
}
return has;
}
};
#ifdef _DEBUG
// #define use_debtor
// template<class T, class U, class X> auto count(unordered_map<T, U> &a, X k) {
// return a.find(k) != a.end(); }
#ifdef use_debtor
// https://marycore.jp/prog/cpp/class-extension-methods/ 違うかも
template <class T, class A = std::allocator<T>>
struct debtor : std::vector<T, A> {
using std::vector<T, A>::vector;
template <class U> int deb_v(U a, int v) { return v; }
template <class U> int deb_v(debtor<U> &a, int v = 0) {
cerr << a.size() << " ";
return deb_v(a.at(0), v + 1);
}
template <class U> void deb_o(U a) { cerr << a << " "; }
template <class U> void deb_o(debtor<U> &a) {
for (int i = 0; i < min((int)a.size(), 15ll); i++) {
deb_o(a[i]);
}
if ((int)a.size() > 15) {
cerr << "...";
}
cerr << endl;
}
typename std::vector<T>::reference my_at(typename std::vector<T>::size_type n,
vector<int> &ind) {
if (n < 0 || n >= (int)this->size()) {
int siz = (int)this->size();
cerr << "vector size = ";
int dim = deb_v((*this));
cerr << endl;
ind.push_back(n);
cerr << "out index at ";
for (auto &&i : ind) {
cerr << i << " ";
}
cerr << endl;
cerr << endl;
if (dim <= 2) {
deb_o((*this));
}
exit(0);
}
return this->at(n);
}
typename std::vector<T>::reference
operator[](typename std::vector<T>::size_type n) {
if (n < 0 || n >= (int)this->size()) {
int siz = (int)this->size();
cerr << "vector size = ";
int dim = deb_v((*this));
cerr << endl;
cerr << "out index at " << n << endl;
cerr << endl;
if (dim <= 2) {
deb_o((*this));
}
exit(0);
}
return this->at(n);
}
};
#define vector debtor
#endif
#ifdef use_pbds
template <class T> struct my_pbds_tree {
set<T> s;
auto begin() { return s.begin(); }
auto end() { return s.end(); }
auto rbegin() { return s.rbegin(); }
auto rend() { return s.rend(); }
auto empty() { return s.empty(); }
auto size() { return s.size(); }
void clear() { s.clear(); }
template <class U> void insert(U v) { s.insert(v); }
template <class U> void operator+=(U v) { insert(v); }
template <class F> auto erase(F v) { return s.erase(v); }
template <class U> auto find(U v) { return s.find(v); }
template <class U> auto lower_bound(U v) { return s.lower_bound(v); }
template <class U> auto upper_bound(U v) { return s.upper_bound(v); }
auto find_by_order(ll k) {
auto it = s.begin();
for (ll i = 0; i < k; i++)
it++;
return it;
}
auto order_of_key(ll v) {
auto it = s.begin();
ll i = 0;
for (; it != s.end() && *it < v; i++)
it++;
return i;
}
};
#define pbds(T) my_pbds_tree<T>
#endif
// 区間削除は出来ない
// gp_hash_tableでcountを使えないようにするため
template <class K, class V> using umap_f = unordered_map<K, V>;
template <typename K, typename T> using map_f = map<K, T>;
#else
#define endl '\n'
// umapはunorderd_mapになる
// umapiはgp_hash_table
// find_by_order(k) k番目のイテレーター
// order_of_key(k) k以上が前から何番目か
#define pbds(U) \
__gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag, \
__gnu_pbds::tree_order_statistics_node_update>
template <class K, class V>
struct umap_f : public __gnu_pbds::gp_hash_table<K, V, xorshift> {
int count(const K &k) { return this->find(k) != this->end(); }
};
template <typename K, typename T>
using map_f = __gnu_pbds::tree<K, T, less<K>, __gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update>;
#endif
#define umapi unordered_map<ll, ll>
#define umapp unordered_map<P, ll>
#define umappp unordered_map<P, P>
#define umapu unordered_map<uint64_t, ll>
#define umapip unordered_map<ll, P>
template <class T, class U, class X> auto count(unordered_map<T, U> &a, X k) {
return a.find(k) != a.end();
}
/*@formatter:off*/
#ifdef use_pbds
template <class U, class L>
void operator+=(
__gnu_pbds::tree<U, __gnu_pbds::null_type, less<U>, __gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update> &s,
L v) {
s.insert(v);
}
#endif
// 衝突対策
#define ws ws_
typedef tuple<ll, ll, ll> T;
typedef tuple<ll, ll, ll, ll> F;
// T mt(ll a, ll b, ll c) { return T(a, b, c); }
// F mf(ll a, ll b, ll c, ll d) { return F(a, b, c, d); }
// 関数内をまとめる
// 初期値l=-1, r=-1
void set_lr12(int &l, int &r, int n) { /*r==-1*/
if (r == -1) {
if (l == -1) {
l = 0;
r = n;
} else {
r = l;
l = 0;
}
}
}
//@マクロ省略系 型,構造
// using で元のdoubleを同時に使えるはず
#define double_big
#ifdef double_big
#define double long double
// #define pow powl
#endif
using dou = double;
/*@formatter:off*/
template <class T> T MAX() { return numeric_limits<T>::max(); }
template <class T> T MIN() { return numeric_limits<T>::min(); }
constexpr ll inf = (ll)1e9 + 100;
constexpr ll linf = (ll)1e18 + 100;
constexpr dou dinf = (dou)linf * linf;
constexpr char infc = '{';
const string infs = "{";
template <class T> T INF() { return MAX<T>() / 2; }
template <> signed INF() { return inf; }
template <> ll INF() { return linf; }
template <> double INF() { return dinf; }
template <> char INF() { return infc; }
template <> string INF() { return infs; }
const double eps = 1e-9;
// #define use_epsdou
#ifdef use_epsdou
// 基本コメントアウト
struct epsdou {
double v;
epsdou(double v = 0) : v(v) {}
template <class T> epsdou &operator+=(T b) {
v += (double)b;
return (*this);
}
template <class T> epsdou &operator-=(T b) {
v -= (double)b;
return (*this);
}
template <class T> epsdou &operator*=(T b) {
v *= (double)b;
return (*this);
}
template <class T> epsdou &operator/=(T b) {
v /= (double)b;
return (*this);
}
epsdou operator+(epsdou b) { return v + (double)b; }
epsdou operator-(epsdou b) { return v - (double)b; }
epsdou operator*(epsdou b) { return v * (double)b; }
epsdou operator/(epsdou b) { return v / (double)b; }
epsdou operator-() const { return epsdou(-v); }
template <class T> bool operator<(T b) { return v < (double)b; }
template <class T> bool operator>(T b) {
auto r = (double)b;
return v > (double)b;
}
template <class T> bool operator==(T b) { return fabs(v - (double)b) <= eps; }
template <class T> bool operator<=(T b) {
return v < (double)b || fabs(v - b) <= eps;
}
template <class T> bool operator>=(T b) {
return v > (double)b || fabs(v - b) <= eps;
}
operator double() { return v; }
};
template <> epsdou MAX() { return MAX<double>(); }
template <> epsdou MIN() { return MIN<double>(); }
// priqrity_queue等で使うのに必要
bool operator<(const epsdou &a, const epsdou &b) { return a.v < b.v; }
bool operator>(const epsdou &a, const epsdou &b) { return a.v > b.v; }
istream &operator>>(istream &iss, epsdou &a) {
iss >> a.v;
return iss;
}
ostream &operator<<(ostream &os, epsdou &a) {
os << a.v;
return os;
}
#define eps_conr_t(o) \
template <class T> epsdou operator o(T a, epsdou b) { return (dou)a o b.v; }
#define eps_conl_t(o) \
template <class T> epsdou operator o(epsdou a, T b) { return a.v o(dou) b; }
eps_conl_t(+) eps_conl_t(-) eps_conl_t(*) eps_conl_t(/) eps_conr_t(+)
eps_conr_t(-) eps_conr_t(*) eps_conr_t(/)
// template<class U> epsdou max(epsdou a, U b){return a.v>b ? a.v: b;}
// template<class U> epsdou max(U a, epsdou b){return a>b.v ? a: b.v;}
// template<class U> epsdou min(epsdou a, U b){return a.v<b ? a.v: b;}
// template<class U> epsdou min(U a, epsdou b){return a<b.v ? a: b.v;}
#undef double
#define double epsdou
#undef dou
#define dou epsdou
#endif
template <class T = int, class A, class B = int>
T my_pow(A a, B b = 2) {
if (b < 0)
return (T)1 / my_pow<T>(a, -b);
#if __cplusplus >= 201703L
if constexpr (is_floating_point<T>::value) {
return pow((T)a, (T)b);
} else if constexpr (is_floating_point<A>::value) {
assert2(
0,
"pow <not dou>(dou, )"); /*return
0;しない方がコンパイル前に(voidを受け取るので)エラーが出ていいかも*/
} else if constexpr (is_floating_point<B>::value) {
assert2(
0,
"pow <not dou>(, dou)"); /*return
0;しない方がコンパイル前に(voidを受け取るので)エラーが出ていいかも*/
} else {
#endif
T ret = 1;
T bek = a;
while (b) {
if (b & 1)
ret *= bek;
bek *= bek;
b >>= 1;
}
return ret;
#if __cplusplus >= 201703L
}
#endif
}
#define pow my_pow
#define ull unsigned long long
using itn = int;
using str = string;
using bo = bool;
#define au auto
using P = pair<ll, ll>;
#define fi first
#define se second
#define beg begin
#define rbeg rbegin
#define con continue
#define bre break
#define brk break
#define is ==
#define el else
#define elf else if
#define upd update
#define sstream stringstream
#define maxq 1
#define minq -1
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
#define MALLOC(type, len) (type *)malloc((len) * sizeof(type))
#define lam1(ret) [&](auto &&v) { return ret; }
#define lam2(v, ret) [&](auto &&v) { return ret; }
#define lam(...) over2(__VA_ARGS__, lam2, lam1)(__VA_ARGS__)
#define lamr(right) [&](auto &&p) { return p right; }
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());
// マクロ省略系 コンテナ
using vi = vector<ll>;
using vb = vector<bool>;
using vs = vector<string>;
using vd = vector<double>;
using vc = vector<char>;
using vp = vector<P>;
using vt = vector<T>;
// #define V vector
#define vvt0(t) vector<vector<t>>
#define vvt1(t, a) vector<vector<t>> a
#define vvt2(t, a, b) vector<vector<t>> a(b)
#define vvt3(t, a, b, c) vector<vector<t>> a(b, vector<t>(c))
#define vvt4(t, a, b, c, d) vector<vector<t>> a(b, vector<t>(c, d))
#define vv(type, ...) \
over4(__VA_ARGS__, vvt4, vvt3, vvt2, vvt1, vvt0)(type, __VA_ARGS__)
#define vvi(...) vv(ll, __VA_ARGS__)
#define vvb(...) vv(bool, __VA_ARGS__)
#define vvs(...) vv(string, __VA_ARGS__)
#define vvd(...) vv(double, __VA_ARGS__)
#define vvc(...) vv(char, __VA_ARGS__)
#define vvp(...) vv(P, __VA_ARGS__)
#define vvt(...) vv(T, __VA_ARGS__)
// optional
#define vvoi(...) vv(optional<ll>, __VA_ARGS__)
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
#define vni(name, ...) auto name = make_v<ll>(__VA_ARGS__)
#define vnb(name, ...) auto name = make_v<bool>(__VA_ARGS__)
#define vns(name, ...) auto name = make_v<string>(__VA_ARGS__)
#define vnd(name, ...) auto name = make_v<double>(__VA_ARGS__)
#define vnc(name, ...) auto name = make_v<char>(__VA_ARGS__)
#define vnp(name, ...) auto name = make_v<P>(__VA_ARGS__)
#define vn(type, name, ...) auto name = make_v<type>(__VA_ARGS__)
#define PQ priority_queue<ll, vector<ll>, greater<ll>>
#define tos to_string
using mapi = map<ll, ll>;
using mapp = map<P, ll>;
using mapd = map<dou, ll>;
using mapc = map<char, ll>;
using maps = map<str, ll>;
using seti = set<ll>;
using setp = set<P>;
using setd = set<dou>;
using setc = set<char>;
using sets = set<str>;
using qui = queue<ll>;
#define uset unordered_set
#define useti unordered_set<ll, xorshift>
#define mset multiset
#define mseti multiset<ll>
#define umap unordered_map
#define mmap multimap
// 初期化子を与える
template <template <class...> class MAP, class K, class V, class... A>
struct init_map : public MAP<K, V, A...> {
V init_value;
init_map(V init_value = 0) : MAP<K, V, A...>(), init_value(init_value) {}
V &operator[](const K &k) {
if (MAP<K, V, A...>::count(k) == 0) {
return MAP<K, V, A...>::operator[](k) = init_value;
}
return MAP<K, V, A...>::operator[](k);
}
};
template <class... A> using map2 = init_map<map, A...>;
template <class... A> using umap2 = init_map<umap, A...>;
template <class... A> using umap_f2 = init_map<umap_f, A...>;
// 任意のマクロサポート用 使う度に初期化する //todo これを消す
int index_, v1_, v2_, v3_;
/*@formatter:off*/
string to_string(char c) {
string ret = "";
ret += c;
return ret;
}
template <class T> class pq_min_max {
vector<T> d;
void make_heap() {
for (int i = d.size(); i--;) {
if (i & 1 && d[i - 1] < d[i])
swap(d[i - 1], d[i]);
int k = down(i);
up(k, i);
}
}
inline int parent(int k) const { return ((k >> 1) - 1) & ~1; }
int down(int k) {
int n = d.size();
if (k & 1) { /* min heap*/
while (2 * k + 1 < n) {
int c = 2 * k + 3;
if (n <= c || d[c - 2] < d[c])
c -= 2;
if (c < n && d[c] < d[k]) {
swap(d[k], d[c]);
k = c;
} else
break;
}
} else { /* max heap*/
while (2 * k + 2 < n) {
int c = 2 * k + 4;
if (n <= c || d[c] < d[c - 2])
c -= 2;
if (c < n && d[k] < d[c]) {
swap(d[k], d[c]);
k = c;
} else
break;
}
}
return k;
}
int up(int k, int root = 1) {
if ((k | 1) < (int)d.size() && d[k & ~1] < d[k | 1]) {
swap(d[k & ~1], d[k | 1]);
k ^= 1;
}
int p;
while (root < k && d[p = parent(k)] < d[k]) { /*max heap*/
swap(d[p], d[k]);
k = p;
}
while (root < k && d[k] < d[p = parent(k) | 1]) { /* min heap*/
swap(d[p], d[k]);
k = p;
}
return k;
}
public:
pq_min_max() {}
pq_min_max(const vector<T> &d_) : d(d_) { make_heap(); }
template <class Iter> pq_min_max(Iter first, Iter last) : d(first, last) {
make_heap();
}
void operator+=(const T &x) {
int k = d.size();
d.push_back(x);
up(k);
}
void pop_min() {
if (d.size() < 3u) {
d.pop_back();
} else {
swap(d[1], d.back());
d.pop_back();
int k = down(1);
up(k);
}
}
void pop_max() {
if (d.size() < 2u) {
d.pop_back();
} else {
swap(d[0], d.back());
d.pop_back();
int k = down(0);
up(k);
}
}
const T &get_min() const { return d.size() < 2u ? d[0] : d[1]; }
const T &get_max() const { return d[0]; }
int size() const { return d.size(); }
bool empty() const { return d.empty(); }
};
// 小さいほうからM個取得するpq
template <class T> struct helper_pq_size {
pq_min_max<T> q;
T su = 0;
int max_size = 0;
helper_pq_size() {}
helper_pq_size(int max_size) : max_size(max_size) {}
void clear() {
q = pq_min_max<T>();
su = 0;
}
void operator+=(T v) {
su += v;
q += (v);
if (sz(q) > max_size) {
su -= q.get_max();
q.pop_max();
}
}
T sum() { return su; }
T top() { return q.get_min(); }
void pop() {
su -= q.get_min();
q.pop_min();
}
T poll() {
T ret = q.get_min();
su -= ret;
q.pop_min();
return ret;
}
ll size() { return q.size(); }
};
// 大きいほうからM個取得するpq
template <class T> struct helper_pqg_size {
pq_min_max<T> q;
T su = 0;
int max_size = 0;
helper_pqg_size() {}
helper_pqg_size(int max_size) : max_size(max_size) {}
void clear() {
q = pq_min_max<T>();
su = 0;
}
void operator+=(T v) {
su += v;
q += (v);
if (sz(q) > max_size) {
su -= q.get_min();
q.pop_min();
}
}
T sum() { return su; }
T top() { return q.get_max(); }
void pop() {
su -= q.get_max();
q.pop_max();
}
T poll() {
T ret = q.get_max();
su -= ret;
q.pop_min();
return ret;
}
ll size() { return q.size(); }
};
;
template <class T, class Container = vector<T>,
class Compare = std::less<typename Container::value_type>>
struct helper_pqg {
priority_queue<T, Container, Compare> q; /*小さい順*/
T su = 0;
helper_pqg() {}
void clear() {
q = priority_queue<T, vector<T>, greater<T>>();
su = 0;
}
void operator+=(T v) {
su += v;
q.push(v);
}
T sum() { return su; }
T top() { return q.top(); }
void pop() {
su -= q.top();
q.pop();
}
T poll() {
T ret = q.top();
su -= ret;
q.pop();
return ret;
}
ll size() { return q.size(); }
};
template <class T> using helper_pq = helper_pqg<T, vector<T>, greater<T>>;
#if __cplusplus >= 201703L
// 小さいほうからsize個残る
// Tがoptionalなら空の時nullを返す
template <class T> struct pq {
helper_pq<T> a_q; /*大きい順*/
helper_pq_size<T> b_q; /*大きい順*/
bool aquery;
T su = 0;
pq(int size = inf) {
aquery = size == inf;
if (!aquery) {
b_q = helper_pq_size<T>(size);
}
}
void clear() {
if (aquery)
a_q.clear();
else
b_q.clear();
}
void operator+=(T v) {
if (aquery)
a_q += v;
else
b_q += v;
}
// optionalなら空の時nullを返す
T top() {
if constexpr (is_optional<T>::value) {
if (aquery) {
if (sz(a_q) == 0)
return T();
return a_q.top();
} else {
if (sz(b_q) == 0)
return T();
return b_q.top();
}
} else {
if (aquery)
return a_q.top();
else
return b_q.top();
}
}
T sum() {
if (aquery)
return a_q.sum();
else
return b_q.sum();
}
// optionalなら空の時何もしない
void pop() {
if constexpr (is_optional<T>::value) {
if (aquery) {
if (sz(a_q))
a_q.pop();
} else {
if (sz(b_q))
b_q.pop();
}
} else {
if (aquery)
a_q.pop();
else
b_q.pop();
}
} /*T*/
T poll() {
if constexpr (is_optional<T>::value) {
if (aquery) {
if (sz(a_q) == 0)
return T();
return a_q.poll();
} else {
if (sz(b_q) == 0)
return T();
return b_q.poll();
}
} else {
if (aquery)
return a_q.poll();
else
return b_q.poll();
}
}
ll size() {
if (aquery)
return a_q.size();
else
return b_q.size();
}
/*@formatter:off*/
};
template <class T> struct pqg {
helper_pqg<T> a_q; /*大きい順*/
helper_pqg_size<T> b_q; /*大きい順*/
bool aquery;
T su = 0;
pqg(int size = inf) {
aquery = size == inf;
if (!aquery) {
b_q = helper_pqg_size<T>(size);
}
}
void clear() {
if (aquery)
a_q.clear();
else
b_q.clear();
}
void operator+=(T v) {
if (aquery)
a_q += v;
else
b_q += v;
}
T sum() {
if (aquery)
return a_q.sum();
else
return b_q.sum();
}
T top() {
if (aquery)
return a_q.top();
else
return b_q.top();
}
void pop() {
if (aquery)
a_q.pop();
else
b_q.pop();
}
T poll() {
if (aquery)
return a_q.poll();
else
return b_q.poll();
}
ll size() {
if (aquery)
return a_q.size();
else
return b_q.size();
}
};
#else
// 小さいほうからsize個残る
template <class T> struct pq {
helper_pq<T> a_q; /*大きい順*/
helper_pq_size<T> b_q; /*大きい順*/
bool aquery;
T su = 0;
pq(int size = inf) {
aquery = size == inf;
if (!aquery) {
b_q = helper_pq_size<T>(size);
}
}
void clear() {
if (aquery)
a_q.clear();
else
b_q.clear();
}
void operator+=(T v) {
if (aquery)
a_q += v;
else
b_q += v;
}
T sum() {
if (aquery)
return a_q.sum();
else
return b_q.sum();
}
T top() {
if (aquery)
return a_q.top();
else
return b_q.top();
}
void pop() {
if (aquery)
a_q.pop();
else
b_q.pop();
}
T poll() {
if (aquery)
return a_q.poll();
else
return b_q.poll();
}
ll size() {
if (aquery)
return a_q.size();
else
return b_q.size();
}
};
// 大きいほうからsize個残る
template <class T> struct pqg {
helper_pqg<T> a_q; /*大きい順*/
helper_pqg_size<T> b_q; /*大きい順*/
bool aquery;
T su = 0;
pqg(int size = inf) {
aquery = size == inf;
if (!aquery) {
b_q = helper_pqg_size<T>(size);
}
}
void clear() {
if (aquery)
a_q.clear();
else
b_q.clear();
}
void operator+=(T v) {
if (aquery)
a_q += v;
else
b_q += v;
}
T sum() {
if (aquery)
return a_q.sum();
else
return b_q.sum();
}
T top() {
if (aquery)
return a_q.top();
else
return b_q.top();
}
void pop() {
if (aquery)
a_q.pop();
else
b_q.pop();
}
T poll() {
if (aquery)
return a_q.poll();
else
return b_q.poll();
}
ll size() {
if (aquery)
return a_q.size();
else
return b_q.size();
}
};
#endif
#define pqi pq<ll>
#define pqgi pqg<ll>
template <class T> string deb_tos(pq<T> &q) {
vector<T> res;
auto temq = q;
while (sz(temq))
res.push_back(temq.top()), temq.pop();
stringstream ss;
ss << res;
return ss.str();
}
template <class T> string deb_tos(pqg<T> &q) {
vector<T> res;
auto temq = q;
while (sz(temq))
res.push_back(temq.top()), temq.pop();
stringstream ss;
ss << res;
return ss.str();
}
/*@formatter:off*/
// マクロ 繰り返し
// ↓@オーバーロード隔離
// todo 使わないもの非表示
#define rep1(n) for (ll rep1i = 0, rep1lim = n; rep1i < rep1lim; ++rep1i)
#define rep2(i, n) for (ll i = 0, rep2lim = n; i < rep2lim; ++i)
#define rep3(i, m, n) for (ll i = m, rep3lim = n; i < rep3lim; ++i)
#define rep4(i, m, n, ad) for (ll i = m, rep4lim = n; i < rep4lim; i += ad)
// 逆順 閉区間
#define rer2(i, n) for (ll i = n; i >= 0; i--)
#define rer3(i, m, n) for (ll i = m, rer3lim = n; i >= rer3lim; i--)
#define rer4(i, m, n, dec) for (ll i = m, rer4lim = n; i >= rer4lim; i -= dec)
#ifdef use_for
// ループを一つにまとめないとフォーマットで汚くなるため
#define nex_ind1(i) i++
#define nex_ind2(i, j, J) \
i = (j + 1 == J) ? i + 1 : i, j = (j + 1 == J ? 0 : j + 1)
#define nex_ind3(i, j, k, J, K) \
i = (j + 1 == J && k + 1 == K) ? i + 1 : i, \
j = (k + 1 == K) ? (j + 1 == J ? 0 : j + 1) : j, \
k = (k + 1 == K ? 0 : k + 1)
#define nex_ind4(i, j, k, l, J, K, L) \
i = (j + 1 == J && k + 1 == K && l + 1 == L) ? i + 1 : i, \
j = (k + 1 == K && l + 1 == L) ? (j + 1 == J ? 0 : j + 1) : j, \
k = (l + 1 == L ? (k + 1 == K ? 0 : k + 1) : k), l = l + 1 == L ? 0 : l + 1
#define nex_ind5(i, j, k, l, m, J, K, L, M) \
i = (j + 1 == J && k + 1 == K && l + 1 == L && m + 1 == M) ? i + 1 : i, \
j = (k + 1 == K && l + 1 == L && m + 1 == M) ? (j + 1 == J ? 0 : j + 1) : j, \
k = (l + 1 == L && m + 1 == M ? (k + 1 == K ? 0 : k + 1) : k), \
l = m + 1 == M ? l + 1 == L ? 0 : l + 1 : l, m = m + 1 == M ? 0 : m + 1
#define repss2(i, I) for (int i = 0; i < I; i++)
#define repss4(i, j, I, J) \
for (int i = (J ? 0 : I), j = 0; i < I; nex_ind2(i, j, J))
#define repss6(i, j, k, I, J, K) \
for (int i = (J && K ? 0 : I), j = 0, k = 0; i < I; nex_ind3(i, j, k, J, K))
#define repss8(i, j, k, l, I, J, K, L) \
for (int i = (J && K && L ? 0 : I), j = 0, k = 0, l = 0; i < I; \
nex_ind4(i, j, k, l, J, K, L))
#define repss10(i, j, k, l, m, I, J, K, L, M) \
for (int i = (J && K && L && M ? 0 : I), j = 0, k = 0, l = 0, m = 0; i < I; \
nex_ind5(i, j, k, l, m, J, K, L, M))
// i,j,k...をnまで見る
#define reps2(i, n) repss2(i, n)
#define reps3(i, j, n) repss4(i, j, n, n)
#define reps4(i, j, k, n) repss6(i, j, k, n, n, n)
#define reps5(i, j, k, l, n) repss8(i, j, k, l, n, n, n, n)
template <class T>
void nex_repv2(int &i, int &j, int &I, int &J, vector<vector<T>> &s) {
while (1) {
j++;
if (j >= J) {
j = 0;
i++;
if (i < I) {
J = (int)s[i].size();
}
}
if (i >= I || J)
return;
}
}
template <class T>
void nex_repv3(int &i, int &j, int &k, int &I, int &J, int &K,
vector<vector<vector<T>>> &s) {
while (1) {
k++;
if (k >= K) {
k = 0;
j++;
if (j >= J) {
j = 0;
i++;
if (i >= I)
return;
}
}
J = (int)s[i].size();
K = (int)s[i][j].size();
if (J && K)
return;
}
}
#define repv_2(i, a) repss2(i, sz(a))
// 正方形である必要はない
// 直前を持つのとどっちが早いか
#define repv_3(i, j, a) \
for (int repvI = (int)a.size(), repvJ = (int)a[0].size(), i = 0, j = 0; \
i < repvI; nex_repv2(i, j, repvI, repvJ, a))
// 箱状になっている事が要求される つまり[i] 次元目の要素数は一定
#define repv_4(i, j, k, a) \
for (int repvI = (int)a.size(), repvJ = (int)a[0].size(), \
repvK = (int)a[0][0].size(), i = 0, j = 0, k = 0; \
i < repvI; nex_repv3(i, j, k, repvI, repvJ, repvK, a))
#define repv_5(i, j, k, l, a) \
repss8(i, j, k, l, sz(a), sz(a[0]), sz(a[0][0]), sz(a[0][0][0]))
#define repv_6(i, j, k, l, m, a) \
repss10(i, j, k, l, m, sz(a), sz(a[0]), sz(a[0][0]), sz(a[0][0][0]), \
sz(a[0][0][0][0]))
#endif
template <typename T> struct has_rbegin_rend {
private:
template <typename U>
static auto check(U &&obj)
-> decltype(std::rbegin(obj), std::rend(obj), std::true_type{});
static std::false_type check(...);
public:
static constexpr bool value = decltype(check(std::declval<T>()))::value;
};
template <typename T>
constexpr bool has_rbegin_rend_v = has_rbegin_rend<T>::value;
template <typename Iterator> class Range {
public:
Range(Iterator &&begin, Iterator &&end) noexcept
: m_begin(std::forward<Iterator>(begin)),
m_end(std::forward<Iterator>(end)) {}
Iterator begin() const noexcept { return m_begin; }
Iterator end() const noexcept { return m_end; }
private:
const Iterator m_begin;
const Iterator m_end;
};
template <typename Iterator>
static inline Range<Iterator> makeRange(Iterator &&begin,
Iterator &&end) noexcept {
return Range<Iterator>{std::forward<Iterator>(begin),
std::forward<Iterator>(end)};
}
template <typename T>
static inline decltype(auto)
makeReversedRange(const std::initializer_list<T> &iniList) noexcept {
return makeRange(std::rbegin(iniList), std::rend(iniList));
}
template <typename T, typename std::enable_if_t<has_rbegin_rend_v<T>,
std::nullptr_t> = nullptr>
static inline decltype(auto) makeReversedRange(T &&c) noexcept {
return makeRange(std::rbegin(c), std::rend(c));
} /* rbegin(),
rend()を持たないものはこっちに分岐させて,エラーメッセージを少なくする*/
template <typename T, typename std::enable_if<!has_rbegin_rend<T>::value,
std::nullptr_t>::type = nullptr>
static inline void makeReversedRange(T &&) noexcept {
static_assert(has_rbegin_rend<T>::value,
"Specified argument doesn't have reverse iterator.");
}
// #define use_for
#define form3(k, v, st) for (auto &&[k, v] : st)
#define form4(k, v, st, r) \
for (auto &&[k, v] : range(st.begin(), st.lower_bound(r)))
#define form5(k, v, st, l, r) \
for (auto &&[k, v] : range(st.lower_bound(l), st.lower_bound(r)))
#define form(...) over5(__VA_ARGS__, form5, form4, form3)(__VA_ARGS__)
#define forrm1(st) \
for (auto &&forrm_it = st.rbegin(); forrm_it != st.rend(); ++forrm_it)
#define forrm3(k, v, st) \
for (auto &&forrm_it = st.rbegin(); forrm_it != st.rend(); ++forrm_it)
// 向こう側で
// ++itか it = st.erase(it)とする
#define fors1(st) for (auto &&it = st.begin(); it != st.end();)
#define fors2(v, st) for (auto &&it = st.begin(); it != st.end();)
#define fors3(v, st, r) \
for (auto &&it = st.begin(); it != st.end() && (*it) < r;)
#define fors4(v, st, l, r) \
for (auto &&it = st.lower_bound(l); it != st.end() && (*it) < r;)
#ifdef use_for
#define forslr3(st, a, b) \
for (auto &&forslr_it = st.begin(); forslr_it != st.end(); ++forslr_it)
#define forslr4(v, st, a, b) \
for (auto &&forslr_it = st.begin(); forslr_it != st.end(); ++forslr_it)
#define forslr5(v, st, r, a, b) \
for (auto &&forslr_it = st.begin(); \
forslr_it != st.end() && (*forslr_it) < r; ++forslr_it)
#define forslr6(v, st, l, r, a, b) \
for (auto &&forslr_it = st.lower_bound(l); \
forslr_it != st.end() && (*forslr_it) < r; ++forslr_it)
#define fora_f_init_2(a, A) ;
#define fora_f_init_3(fora_f_i, a, A) auto &&a = A[fora_f_i];
#define fora_f_init_4(a, b, A, B) \
auto &&a = A[fora_f_i]; \
auto &&b = B[fora_f_i];
#define fora_f_init_5(fora_f_i, a, b, A, B) \
auto &&a = A[fora_f_i]; \
auto &&b = B[fora_f_i];
#define fora_f_init_6(a, b, c, A, B, C) \
auto &&a = A[fora_f_i]; \
auto &&b = B[fora_f_i]; \
auto &&c = C[fora_f_i];
#define fora_f_init_7(fora_f_i, a, b, c, A, B, C) \
auto &&a = A[fora_f_i]; \
auto &&b = B[fora_f_i]; \
auto &&c = C[fora_f_i];
#define fora_f_init_8(a, b, c, d, A, B, C, D) \
auto &&a = A[fora_f_i]; \
auto &&b = B[fora_f_i]; \
auto &&c = C[fora_f_i]; \
auto &&d = D[fora_f_i];
#define fora_f_init_9(fora_f_i, a, b, c, d, A, B, C, D) \
auto &&a = A[fora_f_i]; \
auto &&b = B[fora_f_i]; \
auto &&c = C[fora_f_i]; \
auto &&d = D[fora_f_i];
#define fora_f_init(...) \
over9(__VA_ARGS__, fora_f_init_9, fora_f_init_8, fora_f_init_7, \
fora_f_init_6, fora_f_init_5, fora_f_init_4, fora_f_init_3, \
fora_f_init_2)(__VA_ARGS__)
#define forr_init_2(a, A) auto &&a = A[forr_i];
#define forr_init_3(forr_i, a, A) auto &&a = A[forr_i];
#define forr_init_4(a, b, A, B) \
auto &&a = A[forr_i]; \
auto &&b = B[forr_i];
#define forr_init_5(forr_i, a, b, A, B) \
auto &&a = A[forr_i]; \
auto &&b = B[forr_i];
#define forr_init_6(a, b, c, A, B, C) \
auto &&a = A[forr_i]; \
auto &&b = B[forr_i]; \
auto &&c = C[forr_i];
#define forr_init_7(forr_i, a, b, c, A, B, C) \
auto &&a = A[forr_i]; \
auto &&b = B[forr_i]; \
auto &&c = C[forr_i];
#define forr_init_8(a, b, c, d, A, B, C, D) \
auto &&a = A[forr_i]; \
auto &&b = B[forr_i]; \
auto &&c = C[forr_i]; \
auto &&d = D[forr_i];
#define forr_init_9(forr_i, a, b, c, d, A, B, C, D) \
auto &&a = A[forr_i]; \
auto &&b = B[forr_i]; \
auto &&c = C[forr_i]; \
auto &&d = D[forr_i];
#define forr_init(...) \
over9(__VA_ARGS__, forr_init_9, forr_init_8, forr_init_7, forr_init_6, \
forr_init_5, forr_init_4, forr_init_3, forr_init_2)(__VA_ARGS__)
#define forp_init3(k, v, S) \
auto &&k = S[forp_i].first; \
auto &&v = S[forp_i].second;
#define forp_init4(forp_i, k, v, S) \
auto &&k = S[forp_i].first; \
auto &&v = S[forp_i].second;
#define forp_init(...) \
over4(__VA_ARGS__, forp_init4, forp_init3, forp_init2, \
forp_init1)(__VA_ARGS__)
#define forrm_init(k, v, ...) \
auto &&k = (*forrm_it).fi; \
auto &&v = (*forrm_it).se;
#define fors_init(v, ...) auto &&v = (*it);
#define forlr_init(a, A, ngl, ngr) \
auto a = A[forlr_i]; \
auto prev = forlr_i ? A[forlr_i - 1] : ngl; \
auto next = forlr_i + 1 < rep2lim ? A[forlr_i + 1] : ngr;
#define forslr_init4(a, A, ngl, ngr) \
auto a = (*forslr_it); \
auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \
auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr;
#define forslr_init5(a, A, r, ngl, ngr) \
auto a = (*forslr_it); \
auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \
auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr;
#define forslr_init6(a, A, l, r, ngl, ngr) \
auto a = (*forslr_it); \
auto prev = (forslr_it != A.begin()) ? (*std::prev(forslr_it)) : ngl; \
auto next = (forslr_it != std::prev(A.end())) ? (*std::next(forslr_it)) : ngr;
#define forslr_init(...) \
over6(__VA_ARGS__, forslr_init6, forslr_init5, forslr_init4)(__VA_ARGS__);
// こうしないとmapがおかしくなる
#define fora_f_2(a, A) for (auto &&a : A)
#define fora_f_3(fora_f_i, a, A) rep(fora_f_i, sz(A))
#define fora_f_4(a, b, A, B) rep(fora_f_i, sz(A))
#define fora_f_5(fora_f_i, a, b, A, B) rep(fora_f_i, sz(A))
#define fora_f_6(a, b, c, A, B, C) rep(fora_f_i, sz(A))
#define fora_f_7(fora_f_i, a, b, c, A, B, C) rep(fora_f_i, sz(A))
#define fora_f_8(a, b, c, d, A, B, C, D) rep(fora_f_i, sz(A))
#define fora_f_9(fora_f_i, a, b, c, d, A, B, C, D) rep(fora_f_i, sz(A))
#define forr_2(a, A) rer(forr_i, sz(A) - 1)
#define forr_3(forr_i, a, A) rer(forr_i, sz(A) - 1)
#define forr_4(a, b, A, B) rer(forr_i, sz(A) - 1)
#define forr_5(forr_i, a, b, A, B) rer(forr_i, sz(A) - 1)
#define forr_6(a, b, c, A, B, C) rer(forr_i, sz(A) - 1)
#define forr_7(forr_i, a, b, c, A, B, C) rer(forr_i, sz(A) - 1)
#define forr_8(a, b, c, d, A, B, C, D) rer(forr_i, sz(A) - 1)
#define forr_9(forr_i, a, b, c, d, A, B, C, D) rer(forr_i, sz(A) - 1)
#endif
// ↑@オーバーロード隔離
// rep系はインデックス、for系は中身
#define rep(...) over4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rer(...) over4(__VA_ARGS__, rer4, rer3, rer2, )(__VA_ARGS__)
// 自分込みで残りがREM以上の間ループを回す
#define rem(i, N, REM) for (int i = 0; i < N - REM + 1; i++)
// char用のrep
#define repc(i, m, n) for (char i = m, repc3lim = n; i < repc3lim; ++i)
// i,j,k...をnまで見る
#define reps(...) over5(__VA_ARGS__, reps5, reps4, reps3, reps2, )(__VA_ARGS__)
#define repss(...) \
over10(__VA_ARGS__, repss10, a, repss8, a, repss6, a, repss4, a, \
repss2)(__VA_ARGS__)
// vectorのindexを走査する
// repv(i,j,vvi)
#define repv(...) \
over6(__VA_ARGS__, repv_6, repv_5, repv_4, repv_3, repv_2, )(__VA_ARGS__)
#define rerv(i, A) for (int i = sz(A) - 1; i >= 0; i--)
// repvn(dp) nは次元
#define repv1(a) repv(i, a)
#define repv2(a) repv(i, j, a)
#define repv3(a) repv(i, j, k, a)
#define repv4(a) repv(i, j, k, l, a)
#ifdef use_for
#define fora_f(...) \
over9(__VA_ARGS__, fora_f_9, fora_f_8, fora_f_7, fora_f_6, fora_f_5, \
fora_f_4, fora_f_3, fora_f_2)(__VA_ARGS__)
#endif
#define forr(...) \
over9(__VA_ARGS__, forr_9, forr_8, forr_7, forr_6, forr_5, forr_4, forr_3, \
forr_2)(__VA_ARGS__)
// 0~N-2まで見る
#define forar_init(v, rv, A) \
auto &&v = A[forar_i]; \
auto &&rv = A[forar_i + 1];
#define forar(v, rv, A) rep(forar_i, sz(A) - 1)
#if __cplusplus >= 201703L
template <size_t M_SZ, bool indexed, class Iterator, class T, class U = T,
class V = T, class W = T>
class ite_vec_merge : public Iterator {
std::size_t i = 0;
vector<T> &A;
vector<U> &B;
vector<V> &C;
vector<W> &D;
public:
ite_vec_merge(Iterator ita, vector<T> &A)
: Iterator(ita), A(A), B(A), C(A), D(A) {}
ite_vec_merge(Iterator ita, vector<T> &A, vector<U> &B)
: Iterator(ita), A(A), B(B), C(A), D(A) {}
ite_vec_merge(Iterator ita, vector<T> &A, vector<U> &B, vector<V> &C)
: Iterator(ita), A(A), B(B), C(C), D(A) {}
ite_vec_merge(Iterator ita, vector<T> &A, vector<U> &B, vector<V> &C,
vector<W> &D)
: Iterator(ita), A(A), B(B), C(C), D(D) {}
auto &operator++() {
++i;
this->Iterator::operator++();
return *this;
}
auto operator*() const noexcept {
if constexpr (!indexed && M_SZ == 1) {
return tuple<T &>(A[i]);
} else if constexpr (!indexed && M_SZ == 2) {
return tuple<T &, U &>(A[i], B[i]);
} else if constexpr (!indexed && M_SZ == 3) {
return tuple<T &, U &, V &>(A[i], B[i], C[i]);
} else if constexpr (!indexed && M_SZ == 4) {
return tuple<T &, U &, V &, W &>(A[i], B[i], C[i], D[i]);
} else if constexpr (indexed && M_SZ == 1) {
return tuple<int, T &>(i, A[i]);
} else if constexpr (indexed && M_SZ == 2) {
return tuple<int, T &, U &>(i, A[i], B[i]);
} else if constexpr (indexed && M_SZ == 3) {
return tuple<int, T &, U &, V &>(i, A[i], B[i], C[i]);
} else if constexpr (indexed && M_SZ == 4) {
return tuple<int, T &, U &, V &, W &>(i, A[i], B[i], C[i], D[i]);
} else {
assert(0);
return tuple<int>(i);
}
}
};
template <size_t M_SZ, bool indexed, class T, class U = T, class V = T,
class W = T>
class vec_merge {
vector<T> &a;
vector<U> &b;
vector<V> &c;
vector<W> &d;
public:
vec_merge(vector<T> &a) : a(a), b(a), c(a), d(a) {}
vec_merge(vector<T> &a, vector<U> &b) : a(a), b(b), c(a), d(a) {}
vec_merge(vector<T> &a, vector<U> &b, vector<V> &c)
: a(a), b(b), c(c), d(a) {}
vec_merge(vector<T> &a, vector<U> &b, vector<V> &c, vector<W> &d)
: a(a), b(b), c(c), d(d) {}
auto begin() const {
if constexpr (M_SZ == 1) {
return ite_vec_merge<M_SZ, indexed, decltype(std::begin(a)), T, U, V, W>{
std::begin(a), a};
} else if constexpr (M_SZ == 2) {
return ite_vec_merge<M_SZ, indexed, decltype(std::begin(a)), T, U, V, W>{
std::begin(a), a, b};
} else if constexpr (M_SZ == 3) {
return ite_vec_merge<M_SZ, indexed, decltype(std::begin(a)), T, U, V, W>{
std::begin(a), a, b, c};
} else if constexpr (M_SZ == 4) {
return ite_vec_merge<M_SZ, indexed, decltype(std::begin(a)), T, U, V, W>{
std::begin(a), a, b, c, d};
} else {
assert(0);
return ite_vec_merge<M_SZ, indexed, decltype(std::begin(a)), T, U, V, W>{
std::begin(a), a};
}
}
auto end() const {
if constexpr (M_SZ == 1) {
return ite_vec_merge<M_SZ, indexed, decltype(std::end(a)), T, U, V, W>{
std::end(a), a};
} else if constexpr (M_SZ == 2) {
return ite_vec_merge<M_SZ, indexed, decltype(std::end(a)), T, U, V, W>{
std::end(a), a, b};
} else if constexpr (M_SZ == 3) {
return ite_vec_merge<M_SZ, indexed, decltype(std::end(a)), T, U, V, W>{
std::end(a), a, b, c};
} else if constexpr (M_SZ == 4) {
return ite_vec_merge<M_SZ, indexed, decltype(std::end(a)), T, U, V, W>{
std::end(a), a, b, c, d};
} else {
assert(0);
return ite_vec_merge<M_SZ, indexed, decltype(std::end(a)), T, U, V, W>{
std::end(a), a};
}
}
};
#endif
#define fora_2(a, A) for (auto &&a : A)
#if __cplusplus >= 201703L
#define fora_3(i, a, A) \
for (auto [i, a] : vec_merge<1, true, decl_t<decltype(A)>>(A))
#define fora_4(a, b, A, B) \
for (auto [a, b] : \
vec_merge<2, false, decl_t<decltype(A)>, decl_t<decltype(B)>>(A, B))
#define fora_5(i, a, b, A, B) \
for (auto [i, a, b] : \
vec_merge<2, true, decl_t<decltype(A)>, decl_t<decltype(B)>>(A, B))
#define fora_6(a, b, c, A, B, C) \
for (auto [a, b, c] : \
vec_merge<3, false, decl_t<decltype(A)>, decl_t<decltype(B)>, \
decl_t<decltype(C)>>(A, B, C))
#define fora_7(i, a, b, c, A, B, C) \
for (auto [i, a, b, c] : \
vec_merge<3, true, decl_t<decltype(A)>, decl_t<decltype(B)>, \
decl_t<decltype(C)>>(A, B, C))
#define fora_8(a, b, c, d, A, B, C, D) \
for (auto [a, b, c, d] : \
vec_merge<4, false, decl_t<decltype(A)>, decl_t<decltype(B)>, \
decl_t<decltype(C)>, decl_t<decltype(D)>>(A, B, C, D))
#define fora_9(i, a, b, c, d, A, B, C, D) \
for (auto [i, a, b, c, d] : \
vec_merge<4, true, decl_t<decltype(A)>, decl_t<decltype(B)>, \
decl_t<decltype(C)>, decl_t<decltype(D)>>(A, B, C, D))
#endif
// 構造化束縛ver
// 1e5要素で40ms程度
// 遅いときはfora_fを使う
#define fora(...) \
over9(__VA_ARGS__, fora_9, fora_8, fora_7, fora_6, fora_5, fora_4, fora_3, \
fora_2)(__VA_ARGS__)
// #define forr(v, a) for(auto&& v : makeReversedRange(a))
// 参照を取らない
/*@formatter:off*/
#ifdef use_for
template <class U> vector<U> to1d(vector<U> &a) { return a; }
template <class U> auto to1d(vector<vector<U>> &a) {
vector<U> res;
for (auto &&a1 : a)
for (auto &&a2 : a1)
res.push_back(a2);
return res;
}
template <class U> vector<U> to1d(vector<vector<vector<U>>> &a) {
vector<U> res;
for (auto &&a1 : a)
for (auto &&a2 : a1)
for (auto &&a3 : a2)
res.push_back(a3);
return res;
}
template <class U> vector<U> to1d(vector<vector<vector<vector<U>>>> &a) {
vector<U> res;
for (auto &&a1 : a)
for (auto &&a2 : a1)
for (auto &&a3 : a2)
for (auto &&a4 : a3)
res.push_back(a4);
return res;
}
template <class U>
vector<U> to1d(vector<vector<vector<vector<vector<U>>>>> &a) {
vector<U> res;
for (auto &&a1 : a)
for (auto &&a2 : a1)
for (auto &&a3 : a2)
for (auto &&a4 : a3)
for (auto &&a5 : a4)
res.push_back(a5);
return res;
}
template <class U>
vector<U> to1d(vector<vector<vector<vector<vector<vector<U>>>>>> &a) {
vector<U> res;
for (auto &&a1 : a)
for (auto &&a2 : a1)
for (auto &&a3 : a2)
for (auto &&a4 : a3)
for (auto &&a5 : a4)
for (auto &&a6 : a5)
res.push_back(a6);
return res;
}
#define forv(a, b) for (auto a : to1d(b))
// インデックスを前後含めて走査
#define ring(i, s, len) \
for (int i = s, prev = (s == 0) ? len - 1 : s - 1, \
next = (s == len - 1) ? 0 : s + 1, cou = 0; \
cou < len; \
cou++, prev = i, i = next, next = (next == len - 1) ? 0 : next + 1)
// 値と前後を見る
#define ringv(v, d) \
index_ = 0; \
for (auto prev = d[sz(d) - 1], next = (int)d.size() > 1 ? d[1] : d[0], \
v = d[0]; \
index_ < sz(d); index_++, prev = v, v = next, \
next = (index_ >= sz(d) - 1 ? d[0] : d[index_ + 1]))
// 左右をnext prevで見る 0の左と nの右
#define forlr(v, d, banpei_l, banpei_r) rep(forlr_i, sz(d))
#endif
#define forrm(...) \
over5(__VA_ARGS__, forrm5, forrm4, forrm3, forrm2, forrm1)(__VA_ARGS__)
#define fors(...) over4(__VA_ARGS__, fors4, fors3, fors2, fors1)(__VA_ARGS__)
#define forslr(...) \
over6(__VA_ARGS__, forslr6, forslr5, forslr4, forslr3)(__VA_ARGS__)
#define forp3(k, v, st) rep(forp_i, sz(st))
#define forp4(forp_i, k, v, st) rep(forp_i, sz(st))
#define forp(...) over4(__VA_ARGS__, forp4, forp3)(__VA_ARGS__)
// to_vec(rep(i, N))のように使い
// iが走査した値を持つvectorを返す
#define to_vec2(type, my_for) \
[&]() { \
vector<type> ret; \
my_for { ret.push_back(i); } \
return ret; \
}()
#define to_vec1(my_for) to_vec2(int, my_for)
#define to_vec(...) over2(__VA_ARGS__, to_vec2, to_vec1)(__VA_ARGS__)
// マクロ 定数
#define k3 1010
#define k4 10101
#define k5 101010
#define k6 1010101
#define k7 10101010
const double PI = 3.1415926535897932384626433832795029L;
constexpr bool ev(ll a) { return !(a & 1); }
constexpr bool od(ll a) { return (a & 1); }
//@拡張系 こう出来るべきというもの
// 埋め込み 存在を意識せずに機能を増やされているもの
namespace std {
template <> class hash<std::pair<signed, signed>> {
public:
size_t operator()(const std::pair<signed, signed> &x) const {
return hash<ll>()(((ll)x.first << 32) | x.second);
}
};
template <> class hash<std::pair<ll, ll>> {
public
: /*大きいllが渡されると、<<32でオーバーフローするがとりあえず問題ないと判断*/
size_t operator()(const std::pair<ll, ll> &x) const {
return hash<ll>()(((ll)x.first << 32) | x.second);
}
};
template <> class hash<std::tuple<ll, ll, ll>> {
public:
size_t operator()(const std::tuple<ll, ll, ll> &x_) const {
static xorshift xor_s;
return xor_s(x_);
}
};
template <> class hash<std::tuple<ll, ll, ll, ll>> {
public:
size_t operator()(const std::tuple<ll, ll, ll, ll> &x_) const {
static xorshift xor_s;
return xor_s(x_);
}
};
template <class T> class hash<std::vector<T>> {
public:
size_t operator()(const std::vector<T> &x_) const {
static xorshift xor_s;
return xor_s(x_);
}
};
} // namespace std
// stream まとめ
/*@formatter:off*/
istream &operator>>(istream &iss, P &a) {
iss >> a.first >> a.second;
return iss;
}
template <typename T> istream &operator>>(istream &iss, vector<T> &vec_) {
for (T &x : vec_)
iss >> x;
return iss;
}
template <class T, class U> ostream &operator<<(ostream &os, pair<T, U> p) {
os << p.fi << " " << p.se;
return os;
}
ostream &operator<<(ostream &os, T p) {
os << get<0>(p) << " " << get<1>(p) << " " << get<2>(p);
return os;
}
ostream &operator<<(ostream &os, F p) {
os << get<0>(p) << " " << get<1>(p) << " " << get<2>(p) << " " << get<3>(p);
return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec_) {
for (ll i = 0; i < vec_.size(); ++i)
os << vec_[i] << (i + 1 == vec_.size() ? "" : " ");
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<vector<T>> &vec_) {
for (ll i = 0; i < vec_.size(); ++i) {
for (ll j = 0; j < vec_[i].size(); ++j) {
os << vec_[i][j] << " ";
}
os << endl;
}
return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &m) {
os << endl;
for (auto &&v : m)
os << v << endl;
return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const unordered_map<T, U> &m) {
os << endl;
for (auto &&v : m)
os << v << endl;
return os;
}
template <typename T, typename U> string deb_tos(const unordered_map<T, U> &m) {
map<T, U> a;
for (auto &&v : m) {
a[v.first] = v.second;
}
stringstream ss;
ss << a;
return ss.str();
}
template <class T> ostream &operator<<(ostream &os, const set<T> &s) {
fora(v, s) { os << v << " "; }
return os;
}
template <class T> ostream &operator<<(ostream &os, const mset<T> &s) {
fora(v, s) { os << v << " "; }
return os;
}
template <class T> ostream &operator<<(ostream &os, const deque<T> &a) {
fora(v, a) { os << v << " "; }
return os;
}
ostream &operator<<(ostream &os, const vector<vector<char>> &vec_) {
rep(h, sz(vec_)) {
rep(w, sz(vec_[0])) { os << vec_[h][w]; }
os << endl;
}
return os;
}
ostream &operator<<(ostream &os,
const vector<vector<my_optional<char>>> &vec_) {
rep(h, sz(vec_)) {
rep(w, sz(vec_[0])) { os << vec_[h][w]; }
os << endl;
}
return os;
}
// tuple "," 区切り
#if __cplusplus >= 201703L
template <class Tuple, size_t... Indicies>
void helper_os_tuple(ostream &os, const Tuple &tup,
index_sequence<Indicies...>) {
stringstream ss;
auto f = [&](auto a) { ss << ", " << a; };
(..., f(get<Indicies>(tup)));
os << ss.str().substr(2);
}
template <class... T> ostream &operator<<(ostream &os, const tuple<T...> &tup) {
helper_os_tuple(os, tup, index_sequence_for<T...>{});
return os;
}
#endif
template <class T> struct range_now {
int l;
vector<T> A;
range_now(vector<T> &&A, int l) : A(A), l(l) {}
};
/*@formatter:off*/
// template<class T,class U>ostream &operator<<(ostream &os, vector<pair<T,U>>&
// a) {fora_f(v,a)os<<v<<endl;return os;}
template <typename W, typename H> void resize(W &vec_, const H head) {
vec_.resize(head);
}
template <typename W, typename H, typename... T>
void resize(W &vec_, const H &head, const T... tail) {
vec_.resize(head);
for (auto &v : vec_)
resize(v, tail...);
}
// #define use_for_each //_each _all_of _any_of _none_of _find_if _rfind_if
// _contains _count_if _erase_if _entry_if
#ifdef use_for_each
// todo Atcoderの過去問がc++17に対応したら
// for_each以外はconst & (呼び出し側のラムダも)
template <typename T, typename F> bool all_of2(const T &v, F f) {
if constexpr (has_value_type<T>::value) {
for (auto &&v_ : v) {
if (!all_of2(v_, f))
return false;
}
return true;
} else {
return f(v);
}
}
template <typename T, typename F> bool any_of2(const T &v, F f) {
if constexpr (has_value_type<T>::value) {
for (auto &&v_ : v) {
if (any_of2(v_, f))
return true;
}
return false;
} else {
return f(v);
}
}
template <typename T, typename F> bool none_of2(const T &v, F f) {
return all_of2(v, [&](auto a) { return !f(a); });
}
// 存在しない場合
// 1次元 Nを返す
// 多次元(-1,-1,..)を返す
template <typename T, typename F, require_t(has_value_type<T>::value)>
ll find_if2(const T &v, F f) {
rep(i, sz(v)) {
if (f(v[i]))
return i;
}
return sz(v);
}
template <typename T, typename F>
tuple<int, int> find_if2(const vector<vector<T>> &v, F f) {
rep(i, sz(v)) {
rep(j, sz(v[i])) {
if (f(v[i][j])) {
return tuple<int, int>(i, j);
}
}
}
return tuple<int, int>(-1, -1);
}
template <typename T, typename F>
auto find_if2(const vector<vector<vector<T>>> &v, F f) {
rep(i, sz(v)) {
if (auto ret = find_if2(v[i], f); get<0>(ret) != -1) {
return tuple_cat(tuple<int>(i), ret);
}
}
auto bad = tuple_cat(tuple<int>(-1), find_if2(v[0], f));
return bad;
}
template <class T, class F> auto find_if2(const range_now<T> &v, F f) {
return find_if2(v.A, f) + v.l;
}
// 存在しない場合
// 1次元 -1を返す
// 多次元-1を返す
template <typename T, typename F, require_t(has_value_type<T>::value)>
ll rfind_if2(const T &v, F f) {
rer(i, sz(v) - 1) {
if (f(v[i]))
return i;
}
return -1;
}
template <typename T, typename F>
tuple<int, int> rfind_if2(const vector<vector<T>> &v, F f) {
rer(i, sz(v) - 1) {
rer(j, sz(v[i]) - 1) {
if (f(v[i][j])) {
return tuple<int, int>(i, j);
}
}
}
return tuple<int, int>(-1, -1);
}
template <typename T, typename F>
auto rfind_if2(const vector<vector<vector<T>>> &v, F f) {
rer(i, sz(v) - 1) {
if (auto ret = rfind_if2(v[i], f); get<0>(ret) != -1) {
return tuple_cat(tuple<int>(i), ret);
}
}
auto bad = tuple_cat(tuple<int>(-1), rfind_if2(v[0], f));
return bad;
}
// todo まとめられそう string,vector全般
template <class T> bool contains(const string &s, const T &v) {
return s.find(v) != string::npos;
}
template <typename T> bool contains(const vector<T> &v, const T &val) {
return std::find(v.begin(), v.end(), val) != v.end();
}
template <typename T, typename F> bool contains_if2(const vector<T> &v, F f) {
return find_if(v.begin(), v.end(), f) != v.end();
}
template <typename T, typename F> ll count_if2(const T &v, F f) {
if constexpr (has_value_type<T>::value) {
ll ret = 0;
for (auto &&v_ : v) {
ret += count_if2(v_, f);
}
return ret;
} else {
return f(v);
}
}
template <typename T, typename F> void for_each2(T &a, F f) {
if constexpr (has_value_type<T>::value) {
for (auto &&v_ : a)
for_each2(v_, f);
} else {
f(a);
}
}
template <typename W> ll count_od(const vector<W> &a) {
return count_if2(a, [](ll v) { return v & 1; });
}
template <typename W> ll count_ev(const vector<W> &a) {
return count_if2(a, [](ll v) { return !(v & 1); });
}
// 削除した後のvectorを返す
template <typename T, typename F> vector<T> erase_if2(const vector<T> &v, F f) {
vector<T> nv;
rep(i, sz(v)) {
if (!f(v[i])) {
nv.push_back(v[i]);
}
}
return nv;
}
template <typename T, typename F>
vector<vector<T>> erase_if2(const vector<vector<T>> &v, F f) {
vector<vector<T>> res;
rep(i, sz(v)) { res[i] = erase_if2(v[i], f); }
return res;
}
template <typename T, typename F> vector<T> entry_if2(const vector<T> &v, F f) {
vector<T> nv;
rep(i, sz(v)) {
if (f(v[i])) {
nv.push_back(v[i]);
}
}
return nv;
}
template <typename T, typename F>
vector<vector<T>> entry_if2(const vector<vector<T>> &v, F f) {
vector<vector<T>> res;
rep(i, sz(v)) { res[i] = entry_if2(v[i], f); }
return res;
}
template <typename T, typename F> ll l_rfind_if(const vector<T> &v, F f) {
rer(i, sz(v) - 1) {
if (f(v[i]))
return i;
}
return -1;
}
template <typename T, typename F> bool l_contains_if(const vector<T> &v, F f) {
rer(i, sz(v) - 1) {
if (f(v[i]))
return true;
}
return false;
}
template <class A, class B, class C> auto t_all_of(A a, B b, C c) {
return std::all_of(a, b, c);
}
template <class A, class B, class C> auto t_any_of(A a, B b, C c) {
return std::any_of(a, b, c);
}
template <class A, class B, class C> auto t_none_of(A a, B b, C c) {
return std::none_of(a, b, c);
}
template <class A, class B, class C> auto t_find_if(A a, B b, C c) {
return std::find_if(a, b, c);
}
template <class A, class B, class C> auto t_count_if(A a, B b, C c) {
return std::count_if(a, b, c);
}
#define all_of_s__2(a, right) (t_all_of(ALL(a), lamr(right)))
#define all_of_s__3(a, v, siki) (t_all_of(ALL(a), [&](auto v) { return siki; }))
#define all_of_s(...) over3(__VA_ARGS__, all_of_s__3, all_of_s__2)(__VA_ARGS__)
// all_of(A, %2);
// all_of(A, a, a%2);
#define all_of__2(a, right) all_of2(a, lamr(right))
#define all_of__3(a, v, siki) all_of2(a, [&](auto v) { return siki; })
#define all_of(...) over3(__VA_ARGS__, all_of__3, all_of__2)(__VA_ARGS__)
#define all_of_f(a, f) all_of2(a, f)
#define any_of_s__2(a, right) (t_any_of(ALL(a), lamr(right)))
#define any_of_s__3(a, v, siki) (t_any_of(ALL(a), [&](auto v) { return siki; }))
#define any_of_s(...) over3(__VA_ARGS__, any_of_s__3, any_of_s__2)(__VA_ARGS__)
#define any_of__2(a, right) any_of2(a, lamr(right))
#define any_of__3(a, v, siki) any_of2(a, [&](auto v) { return siki; })
#define any_of(...) over3(__VA_ARGS__, any_of__3, any_of__2)(__VA_ARGS__)
#define any_of_f(a, f) any_of2(a, f)
#define none_of_s__2(a, right) (t_none_of(ALL(a), lamr(right)))
#define none_of_s__3(a, v, siki) \
(t_none_of(ALL(a), [&](auto v) { return siki; }))
#define none_of_s(...) \
over3(__VA_ARGS__, none_of_s__3, none_of_s__2)(__VA_ARGS__)
#define none_of__2(a, right) none_of2(a, lamr(right))
#define none_of__3(a, v, siki) none_of2(a, [&](auto v) { return siki; })
#define none_of(...) over3(__VA_ARGS__, none_of__3, none_of__2)(__VA_ARGS__)
#define none_of_f(a, f) none_of2(a, f)
#define find_if_s__2(a, right) (t_find_if(ALL(a), lamr(right)) - a.begin())
#define find_if_s__3(a, v, siki) \
(t_find_if(ALL(a), [&](auto v) { return siki; }) - a.begin())
#define find_if_s(...) \
over3(__VA_ARGS__, find_if_s__3, find_if_s__2)(__VA_ARGS__)
#define find_if__2(a, right) find_if2(a, lamr(right))
#define find_if__3(a, v, siki) find_if2(a, [&](auto v) { return siki; })
#define find_if__4(a, l, v, siki) \
(find_if2(decltype(a)(a.begin() + l, a.end()), \
[&](auto v) { return siki; }) + \
l)
#define find_if(...) \
over4(__VA_ARGS__, find_if__4, find_if__3, find_if__2)(__VA_ARGS__)
#define find_if_f(a, f) find_if2(a, f)
#define rfind_if_s__2(a, right) l_rfind_if(a, lamr(right))
#define rfind_if_s__3(a, v, siki) l_rfind_if(a, [&](auto v) { return siki; })
#define rfind_if_s(...) \
over3(__VA_ARGS__, rfind_if_s__3, rfind_if_s__2)(__VA_ARGS__)
#define rfind_if__2(a, right) rfind_if2(a, lamr(right))
#define rfind_if__3(a, v, siki) rfind_if2(a, [&](auto v) { return siki; })
#define rfind_if(...) over3(__VA_ARGS__, rfind_if__3, rfind_if__2)(__VA_ARGS__)
#define rfind_if_f(a, f) rfind_if2(a, f)
#define contains_if_s__2(a, right) l_contains_if(a, lamr(right))
#define contains_if_s__3(a, v, siki) \
l_contains_if(a, [&](auto v) { return siki; })
#define contains_if_s(...) \
over3(__VA_ARGS__, contains_if_s__3, contains_if_s__2)(__VA_ARGS__)
#define contains_if__2(a, right) contains_if2(a, lamr(right))
#define contains_if__3(a, v, siki) contains_if2(a, [&](auto v) { return siki; })
#define contains_if(...) \
over3(__VA_ARGS__, contains_if__3, contains_if__2)(__VA_ARGS__)
#define contains_if_f(a, f) contains_if2(a, f)
#define count_if_s__2(a, right) (t_count_if(ALL(a), lamr(right)))
#define count_if_s__3(a, v, siki) \
(t_count_if(ALL(a), [&](auto v) { return siki; }))
#define count_if_s(...) \
over3(__VA_ARGS__, count_if_s__3, count_if_s__2)(__VA_ARGS__)
#define count_if__2(a, right) count_if2(a, lamr(right))
#define count_if__3(a, v, siki) count_if2(a, [&](auto v) { return siki; })
#define count_if(...) over3(__VA_ARGS__, count_if__3, count_if__2)(__VA_ARGS__)
#define count_if_f(a, f) count_if2(a, f)
// vector<vi>で、viに対して操作
#define for_each_s__2(a, right) \
do { \
fora(v, a) { v right; } \
} while (0)
#define for_each_s__3(a, v, shori) \
do { \
fora(v, a) { shori; } \
} while (0)
#define for_each_s(...) \
over3(__VA_ARGS__, for_each_s__3, for_each_s__2)(__VA_ARGS__)
// vector<vi>で、intに対して操作
#define for_each__2(a, right) for_each2(a, lamr(right))
#define for_each__3(a, v, shori) for_each2(a, [&](auto &v) { shori; })
#define for_each(...) over3(__VA_ARGS__, for_each__3, for_each__2)(__VA_ARGS__)
#define for_each_f(a, f) for_each2(a, f);
template <class T, class F> vector<T> help_for_eached(const vector<T> &A, F f) {
vector<T> ret = A;
for_each(ret, v, f(v));
return ret;
}
#define for_eached__2(a, right) help_for_eached(a, lamr(right))
#define for_eached__3(a, v, shori) help_for_eached(a, lam(v, shori))
#define for_eached(...) \
over3(__VA_ARGS__, for_eached__3, for_eached__2)(__VA_ARGS__)
#define for_eached_f(a, f) for_eached2(a, f);
#define each for_each
#define eached for_eached
// #define erase_if_s__2(a, right) l_erase_if2(a,lamr(right))
// #define erase_if_s__3(a, v, siki) l_erase_if2(a,[&](auto v){return siki;})
// #define erase_if_s(...)
// over3(__VA_ARGS__,erase_if_s__3,erase_if_s__2)(__VA_ARGS__)
#define erase_if__2(a, right) erase_if2(a, lamr(right))
#define erase_if__3(a, v, siki) erase_if2(a, [&](auto v) { return siki; })
#define erase_if(...) over3(__VA_ARGS__, erase_if__3, erase_if__2)(__VA_ARGS__)
#define erase_if_f(a, f) erase_if2(a, f)
// #define entry_if_s__2(a, right) l_entry_if2(a,lamr(right))
// #define entry_if_s__3(a, v, siki) l_entry_if2(a,[&](auto v){return siki;})
// #define entry_if_s(...)
// over3(__VA_ARGS__,entry_if_s__3,entry_if_s__2)(__VA_ARGS__)
#define entry_if__2(a, right) entry_if2(a, lamr(right))
#define entry_if__3(a, v, siki) entry_if2(a, [&](auto v) { return siki; })
#define entry_if(...) over3(__VA_ARGS__, entry_if__3, entry_if__2)(__VA_ARGS__)
#define entry_if_f(a, f) entry_if2(a, f)
#endif
// 機能削除 todo
// string.replace()は長さを指定するため、間違えやすい
/*@formatter:off*/
void replace(str &a, char key, char v) { replace(ALL(a), key, v); }
template <class T, class U> void my_replace(T &s, int l, int r, const U &t) {
s.replace(l, r - l, t);
}
#define replace my_replace
template <class T, class U, class W> void replace(vector<W> &a, T key, U v) {
rep(i, sz(a)) if (a[i] == key) a[i] = v;
}
template <class T, class U, class W>
void replace(vector<vector<W>> &A, T key, U v) {
rep(i, sz(A)) replace(A[i], key, v);
}
void replace(str &a, char key, str v) {
if (v == "")
a.erase(remove(ALL(a), key), a.end());
}
// keyと同じかどうか01で置き換える
template <class T, class U> void replace(vector<T> &a, U k) {
rep(i, sz(a)) a[i] = a[i] == k;
}
template <class T, class U> void replace(vector<vector<T>> &a, U k) {
rep(i, sz(a)) rep(j, sz(a[0])) a[i][j] = a[i][j] == k;
}
void replace(str &a) {
int dec = 0;
if ('a' <= a[0] && a[0] <= 'z')
dec = 'a';
if ('A' <= a[0] && a[0] <= 'Z')
dec = 'A';
fora(v, a) { v -= dec; }
}
void replace(string &a, char key) {
string res;
for (auto c : a) {
if (c != key)
res.push_back(c);
}
swap(a, res);
}
void replace(str &a, str key, str v) {
stringstream t;
ll kn = sz(key);
std::string::size_type Pos(a.find(key));
ll l = 0;
while (Pos != std::string::npos) {
t << a.substr(l, Pos - l);
t << v;
l = Pos + kn;
Pos = a.find(key, Pos + kn);
}
t << a.substr(l, sz(a) - l);
a = t.str();
}
template <class T> bool is_permutation(vector<T> &a, vector<T> &b) {
return is_permutation(ALL(a), ALL(b));
}
template <class T> bool next_permutation(vector<T> &a) {
return next_permutation(ALL(a));
}
vi iota(ll s, ll len) {
vi ve(len);
iota(ALL(ve), s);
return ve;
}
//[iterator, iterator)等と渡す
// vectorに変換もできる
#if __cplusplus >= 201703L
template <class I, class J> struct body_range {
I itl;
J itr;
body_range(I it, J end) : itl(it), itr(end) {}
I begin() { return itl; }
I end() { return itr; }
// 毎回コピーする
operator vector<typename I::value_type>() {
return vector<typename I::value_type>(itl, itr);
}
};
template <class I, class J, require_t(is_integral_v<I>)>
vector<int> range(I l, J r) {
return iota(l, r - l);
}
template <class I, require_t(is_integral_v<I>)> vector<int> range(I r) {
return iota(0, r);
}
template <class I, require_t(!is_integral_v<I>)> auto range(I itl, I itr) {
return body_range(itl, itr);
}
#endif
/*@formatter:off*/
// tuple<vecs...> = take_vt(vt)
#if __cplusplus >= 201703L
template <class Tup_l, class Tup_r, size_t... Indicies>
auto helper_take_vector_tuple(Tup_l &res, const Tup_r &rhs,
index_sequence<Indicies...>) {
(..., get<Indicies>(res).emplace_back(get<Indicies>(rhs)));
}
template <class... T> auto take_vector_tuple(const vector<tuple<T...>> &elems) {
tuple<vector<T>...> res;
for (auto &&elem : elems) {
helper_take_vector_tuple(res, elem, index_sequence_for<T...>{});
}
return res;
}
// tie_vt(A,B,C) = vt;
template <class... T> struct tie_vector_tuple {
tuple<vector<T> &...> v;
tie_vector_tuple(vector<T> &...args) : v(args...) {}
template <class Tup, size_t... Indicies>
void helper_emplace(int i, Tup &rhs, index_sequence<Indicies...>) {
(..., (get<Indicies>(v).emplace_back(get<Indicies>(rhs))));
}
template <size_t... Indicies> void clear(index_sequence<Indicies...>) {
(..., (get<Indicies>(v).clear()));
}
void operator=(vector<tuple<T...>> &rhs) {
clear(index_sequence_for<T...>{});
rep(i, sz(rhs)) { helper_emplace(i, rhs[i], index_sequence_for<T...>{}); }
}
};
/*@formatter:off*/
// vt = make_vt(A, B, C)
template <class H, class... T>
auto make_vector_tuple(const vector<H> &head, const vector<T> &...tails) {
vector<tuple<H, T...>> res;
rep(i, sz(head)) { res.emplace_back(head[i], tails[i]...); }
return res;
}
#endif
/*@formatter:off*/
#define mvt make_vector_tuple
#define tie_vt tie_vector_tuple
#define make_vt make_vector_tuple
#define take_vt take_vector_tuple
/*@formatter:off*/
// 基本ソート@0
template <class T> void sort(vector<T> &a, int l = -1, int r = -1) {
set_lr12(l, r, sz(a));
fast_sort(a.begin() + l, a.begin() + r);
}
template <class T> void rsort(vector<T> &a, int l = -1, int r = -1) {
set_lr12(l, r, sz(a));
fast_sort(a.begin() + l, a.begin() + r, greater<T>());
};
// f(tup)の大きさでソート
template <class U, class F> void sort(vector<U> &a, F f) {
fast_sort(ALL(a), [&](U l, U r) { return f(l) < f(r); });
};
template <class U, class F> void rsort(vector<U> &a, F f) {
fast_sort(ALL(a), [&](U l, U r) { return f(l) > f(r); });
};
enum tuple_comparator { /*tcom_less = 1728, tcom_greater = 1729, */ fisi = 0,
fisd,
fdsi,
fdsd,
sifi,
sifd,
sdfi,
sdfd,
fisiti,
fisitd,
fisdti,
fisdtd,
fdsiti,
fdsitd,
fdsdti,
fdsdtd,
fitisi,
fitisd,
fitdsi,
fitdsd,
fdtisi,
fdtisd,
fdtdsi,
fdtdsd,
sifiti,
sifitd,
sifdti,
sifdtd,
sdfiti,
sdfitd,
sdfdti,
sdfdtd,
sitifi,
sitifd,
sitdfi,
sitdfd,
sdtifi,
sdtifd,
sdtdfi,
sdfdfd,
tifisi,
tifisd,
tifdsi,
tifdsd,
tdfisi,
tdfisd,
tdfdsi,
tdfdsd,
tisifi,
tisifd,
tisdfi,
tisdfd,
tdsifi,
tdsifd,
tdsdfi,
tdsdfd };
// todo 短くする
#define set_4(i0, i1, o0, o1) \
[&](U l, U r) { \
return get<i0>(l) != get<i0>(r) ? get<i0>(l) o0 get<i0>(r) \
: get<i1>(l) o1 get<i1>(r); \
}
#define set_6(i0, i1, i2, o0, o1, o2) \
[&](U l, U r) { \
return get<i0>(l) != get<i0>(r) ? get<i0>(l) o0 get<i0>(r) \
: get<i1>(l) != get<i1>(r) ? get<i1>(l) o1 get<i1>(r) \
: get<i2>(l) o2 get<i2>(r); \
}
// functionを返すため少し遅い todo
#if __cplusplus >= 201703L
template <class... T, class U = tuple<T...>>
function<bool(U, U)> get_function2(const vector<tuple<T...>> &a,
tuple_comparator Compare) {
if constexpr (sizeof...(T) >= 2) {
int type = Compare;
if (type == 0)
return set_4(0, 1, <, <);
if (type == 1)
return set_4(0, 1, <, >);
if (type == 2)
return set_4(0, 1, >, <);
if (type == 3)
return set_4(0, 1, >, >);
if (type == 4)
return set_4(1, 0, <, <);
if (type == 5)
return set_4(1, 0, <, >);
if (type == 6)
return set_4(1, 0, >, <);
if (type == 7)
return set_4(1, 0, >, >);
}
if constexpr (sizeof...(T) >= 3) {
int type = Compare - 8;
if (type == 0)
return set_6(0, 1, 2, <, <, <);
else if (type == 1)
return set_6(0, 1, 2, <, <, >);
else if (type == 2)
return set_6(0, 1, 2, <, >, <);
else if (type == 3)
return set_6(0, 1, 2, <, >, >);
else if (type == 4)
return set_6(0, 1, 2, >, <, <);
else if (type == 5)
return set_6(0, 1, 2, >, <, >);
else if (type == 6)
return set_6(0, 1, 2, >, >, <);
else if (type == 7)
return set_6(0, 1, 2, >, >, >);
else if (type == 8)
return set_6(0, 2, 1, <, <, <);
else if (type == 9)
return set_6(0, 2, 1, <, <, >);
else if (type == 10)
return set_6(0, 2, 1, <, >, <);
else if (type == 11)
return set_6(0, 2, 1, <, >, >);
else if (type == 12)
return set_6(0, 2, 1, >, <, <);
else if (type == 13)
return set_6(0, 2, 1, >, <, >);
else if (type == 14)
return set_6(0, 2, 1, >, >, <);
else if (type == 15)
return set_6(0, 2, 1, >, >, >);
else if (type == 16)
return set_6(1, 0, 2, <, <, <);
else if (type == 17)
return set_6(1, 0, 2, <, <, >);
else if (type == 18)
return set_6(1, 0, 2, <, >, <);
else if (type == 19)
return set_6(1, 0, 2, <, >, >);
else if (type == 20)
return set_6(1, 0, 2, >, <, <);
else if (type == 21)
return set_6(1, 0, 2, >, <, >);
else if (type == 22)
return set_6(1, 0, 2, >, >, <);
else if (type == 23)
return set_6(1, 0, 2, >, >, >);
else if (type == 24)
return set_6(1, 2, 0, <, <, <);
else if (type == 25)
return set_6(1, 2, 0, <, <, >);
else if (type == 26)
return set_6(1, 2, 0, <, >, <);
else if (type == 27)
return set_6(1, 2, 0, <, >, >);
else if (type == 28)
return set_6(1, 2, 0, >, <, <);
else if (type == 29)
return set_6(1, 2, 0, >, <, >);
else if (type == 30)
return set_6(1, 2, 0, >, >, <);
else if (type == 31)
return set_6(1, 2, 0, >, >, >);
else if (type == 32)
return set_6(2, 0, 1, <, <, <);
else if (type == 33)
return set_6(2, 0, 1, <, <, >);
else if (type == 34)
return set_6(2, 0, 1, <, >, <);
else if (type == 35)
return set_6(2, 0, 1, <, >, >);
else if (type == 36)
return set_6(2, 0, 1, >, <, <);
else if (type == 37)
return set_6(2, 0, 1, >, <, >);
else if (type == 38)
return set_6(2, 0, 1, >, >, <);
else if (type == 39)
return set_6(2, 0, 1, >, >, >);
else if (type == 40)
return set_6(2, 1, 0, <, <, <);
else if (type == 41)
return set_6(2, 1, 0, <, <, >);
else if (type == 42)
return set_6(2, 1, 0, <, >, <);
else if (type == 43)
return set_6(2, 1, 0, <, >, >);
else if (type == 44)
return set_6(2, 1, 0, >, <, <);
else if (type == 45)
return set_6(2, 1, 0, >, <, >);
else if (type == 46)
return set_6(2, 1, 0, >, >, <);
else if (type == 47)
return set_6(2, 1, 0, >, >, >);
}
return [&](U l, U r) { return true; };
}
template <class... T>
void sort(vector<tuple<T...>> &a, tuple_comparator Compare) {
auto f = get_function2(a, Compare);
fast_sort(ALL(a), f);
}
#endif
/*@formatter:off*/
#undef set_4
#undef set_6
#if __cplusplus >= 201703L
// sort_tuple
template <class H1, class H2, class... T>
void sortt(vector<H1> &head1, vector<H2> &head2, vector<T> &...tails) {
auto vec_t = make_vt(head1, head2, tails...);
sort(vec_t);
tie_vt(head1, head2, tails...) = vec_t;
}
template <class H1, class H2, class... T>
void rsortt(vector<H1> &head1, vector<H2> &head2, vector<T> &...tails) {
auto vec_t = make_vt(head1, head2, tails...);
rsort(vec_t);
tie_vt(head1, head2, tails...) = vec_t;
}
template <class F, class... T> void helper_sortt_f(F f, vector<T> &...a) {
auto z = make_vt(a...);
sort(z, f);
tie_vt(a...) = z;
}
template <class F, class... T> void helper_rsortt_f(F f, vector<T> &...a) {
auto z = make_vt(a...);
rsort(z, f);
tie_vt(a...) = z;
}
// sortt(a,b,c,d, f)まで対応->
template <class A, class B, class F, require_t(!is_vector<F>::value)>
void sortt(vector<A> &a, vector<B> &b, F f) {
helper_sortt_f(f, a, b);
}
template <class A, class B, class F, require_t(!is_vector<F>::value)>
void rsortt(vector<A> &a, vector<B> &b, F f) {
helper_rsortt_f(f, a, b);
}
template <class A, class B, class C, class F, require_t(!is_vector<F>::value)>
void sortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) {
helper_sortt_f(f, a, b, c);
}
template <class A, class B, class C, class F, require_t(!is_vector<F>::value)>
void rsortt(vector<A> &a, vector<B> &b, vector<C> &c, F f) {
helper_rsortt_f(f, a, b, c);
}
template <class A, class B, class C, class D, class F,
require_t(!is_vector<F>::value)>
void sortt(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d, F f) {
helper_sortt_f(f, a, b, c, d);
}
template <class A, class B, class C, class D, class F,
require_t(!is_vector<F>::value)>
void rsortt(vector<A> &a, vector<B> &b, vector<C> &c, vector<D> &d, F f) {
helper_rsortt_f(f, a, b, c, d);
} // sort_tuple f }
#endif
/*@formatter:off*/
// 任意引数は取らない
template <class T> vi sorti(const vector<T> &a) {
auto b = a;
vi ind = iota(0, sz(b));
sortt(b, ind);
return ind;
}
template <class T> vi rsorti(const vector<T> &a) {
auto b = a;
vi ind = iota(0, sz(b));
rsortt(b, ind);
return ind;
}
#if __cplusplus >= 201703L
template <class T, class F> vi sorti(const vector<T> &a, F f) {
vi ind = iota(0, sz(a));
if constexpr (is_same_v<F, tuple_comparator>) {
auto f2 = get_function2(a, f);
auto g = [&](int i, int j) { return f2(a[i], a[j]); };
fast_sort(ALL(ind), g);
} else {
auto g = [&](int i) { return f(a[i]); };
sort(ind, g);
}
return ind;
}
template <class T, class F, require_t(is_function_v<F>)>
vi rsorti(const vector<T> &a, F f) {
vi ind = iota(0, sz(a));
auto g = [&](int i) { return f(a[i]); };
rsort(ind, g);
return ind;
}
// 任意引数にfを渡したい場合は (make_vt(a, b, c), f)
template <class H, class... Ts> vi sortti(vector<H> head, vector<Ts>... tails) {
vi ind = iota(0, sz(head));
auto vec = make_vt(head, tails..., ind);
sort(vec);
auto rets = take_vt(vec);
return get<sizeof...(tails) + 1>(rets);
}
template <class H, class... Ts>
vi rsortti(vector<H> head, vector<Ts>... tails) {
vi ind = iota(0, sz(head));
auto vec = make_vt(head, tails..., ind);
rsort(vec);
auto rets = take_vt(vec);
return get<sizeof...(tails) + 1>(rets);
}
#endif
/*@formatter:off*/
//@5
void sort(string &a) { sort(ALL(a)); }
void rsort(string &a) { sort(RALL(a)); }
void sort(int &a, int &b) {
if (a > b)
swap(a, b);
}
void sort(int &a, int &b, int &c) {
sort(a, b);
sort(a, c);
sort(b, c);
}
void rsort(int &a, int &b) {
if (a < b)
swap(a, b);
}
void rsort(int &a, int &b, int &c) {
rsort(a, b);
rsort(a, c);
rsort(b, c);
}
template <class... T, class U> auto sorted(U head, T... a) {
sort(head, a...);
return head;
}
template <class... T, class U> auto rsorted(U head, T... a) {
rsort(head, a...);
return head;
}
template <class T> vector<T> merge(const vector<T> &a, const vector<T> &b) {
vector<T> res;
std::merge(ALL(a), ALL(b), back_inserter(res));
return res;
}
// firstの値が同じ場合secondが最小の物を残す
template <class T, class U> void unique_min(vector<pair<T, U>> &a) {
vector<pair<T, U>> res;
if (sz(a))
res.push_back(a[0]);
rep(i, 1, sz(a)) {
if (res.back().fi == a[i].fi) {
chmi(res.back().se, a[i].se);
} else {
res.push_back(a[i]);
}
}
swap(a, res);
}
// firstの値が同じ場合secondが最大の物を残す
template <class T, class U> void unique_max(vector<pair<T, U>> &a) {
vector<pair<T, U>> res;
if (sz(a))
res.push_back(a[0]);
rep(i, 1, sz(a)) {
if (res.back().fi == a[i].fi) {
chma(res.back().se, a[i].se);
} else {
res.push_back(a[i]);
}
}
swap(a, res);
}
template <class T> bool includes(vector<T> &a, vector<T> &b) {
vi c = a;
vi d = b;
sort(c);
sort(d);
return includes(ALL(c), ALL(d));
}
template <class T> bool distinct(const vector<T> &A) {
if ((int)(A).size() == 1)
return true;
if ((int)(A).size() == 2)
return A[0] != A[1];
if ((int)(A).size() == 3)
return (A[0] != A[1] && A[1] != A[2] && A[0] != A[2]);
auto B = A;
sort(B);
int N = (B.size());
unique(B);
return N == (int)(B.size());
}
template <class H, class... T> bool distinct(const H &a, const T &...b) {
return distinct(vector<H>{a, b...});
}
/*@formatter:off*/
template <class T, class U> void inc(pair<T, U> &a, U v = 1) {
a.first += v, a.second += v;
}
template <class T, class U> void inc(T &a, U v = 1) { a += v; }
template <class T, class U = int> void inc(vector<T> &a, U v = 1) {
for (auto &u : a)
inc(u, v);
}
template <class T, class U> void dec(T &a, U v = 1) { a -= v; }
template <class T, class U = int> void dec(vector<T> &a, U v = 1) {
for (auto &u : a)
dec(u, v);
}
template <class U> void dec(string &a, U v = 1) {
for (auto &u : a)
dec(u, v);
}
template <class T, class U, class W>
void dec(vector<T> &a, vector<U> &b, W v = 1) {
for (auto &u : a)
dec(u, v);
for (auto &u : b)
dec(u, v);
}
template <class T, class U, class W>
void dec(vector<T> &a, vector<U> &b, vector<W> &c) {
for (auto &u : a)
dec(u, 1);
for (auto &u : b)
dec(u, 1);
for (auto &u : c)
dec(u, 1);
}
bool ins(ll h, ll w, ll H, ll W) { return h >= 0 && w >= 0 && h < H && w < W; }
bool san(ll l, ll v, ll r) { return l <= v && v < r; }
template <class T> bool ins(vector<T> &a, ll i, ll j = 0) {
return san(0, i, sz(a)) && san(0, j, sz(a));
}
#define inside ins
ll u0(ll a) { return a < 0 ? 0 : a; }
template <class T> vector<T> u0(vector<T> &a) {
vector<T> ret = a;
fora(v, ret) { v = u(v); }
return ret;
}
// todo 名前
bool d_(int a, int b) {
if (b == 0)
return false;
return (a % b) == 0;
}
// エラー
void ole() {
#ifdef _DEBUG
cerr << "ole" << endl;
exit(0);
#endif
string a = "a";
rep(i, 30) a += a;
rep(i, 1 << 17) cout << a << endl;
cout << "OLE 出力長制限超過" << endl;
exit(0);
}
void re(string s = "") {
cerr << s << endl;
assert(0 == 1);
exit(0);
}
void tle() {
while (inf)
cout << inf << endl;
}
//@汎用便利関数 入力
ll in() {
ll ret;
cin >> ret;
return ret;
}
template <class T, require_t(!has_value_type<T>::value)> T in() {
T ret;
cin >> ret;
return ret;
}
template <class VT, class T = typename VT::value_type> VT in(int len) {
VT ret;
T dat;
for (int i = 0; i < len; i++) {
cin >> dat;
ret += dat;
}
return ret;
}
template <class VT, class T = typename VT::value_type> VT ind(int len) {
VT ret;
T dat;
for (int i = 0; i < len; i++) {
cin >> dat;
ret += (dat - 1);
}
return ret;
}
string sin() {
string ret;
cin >> ret;
return ret;
}
template <class T> void in(T &head) { cin >> head; }
template <class T, class... U> void in(T &head, U &...tail) {
cin >> head;
in(tail...);
}
#define din_t2(type, a) \
type a; \
cin >> a
#define din_t3(type, a, b) \
type a, b; \
cin >> a >> b
#define din_t4(type, a, b, c) \
type a, b, c; \
cin >> a >> b >> c
#define din_t5(type, a, b, c, d) \
type a, b, c, d; \
cin >> a >> b >> c >> d
#define din_t6(type, a, b, c, d, e) \
type a, b, c, d, e; \
cin >> a >> b >> c >> d >> e
#define din_t7(type, a, b, c, d, e, f) \
type a, b, c, d, e, f; \
cin >> a >> b >> c >> d >> e >> f
#define din_t(...) \
over7(__VA_ARGS__, din_t7, din_t6, din_t5, din_t4, din_t3, \
din_t2)(__VA_ARGS__)
#define din(...) din_t(int, __VA_ARGS__)
#define d_in
#define dsig(...) din_t(signed, __VA_ARGS__)
#define dst(...) din_t(string, __VA_ARGS__)
#define dstr dst
#define d_str dst
#define dcha(...) din_t(char, __VA_ARGS__)
#define dchar dcha
#define ddou(...) din_t(double, __VA_ARGS__)
#define din1d(a) \
din_t2(int, a); \
a--
#define din2d(a, b) \
din_t3(int, a, b); \
a--, b--
#define din3d(a, b, c) \
din_t4(int, a, b, c); \
a--, b--, c--
#define din4d(a, b, c, d) \
din_t5(int, a, b, c, d); \
a--, b--, c--, d--
#define dind(...) over4(__VA_ARGS__, din4d, din3d, din2d, din1d)(__VA_ARGS__)
/*@formatter:off*/
#ifdef _DEBUG
template <class T> void err2(T &&head) { cerr << head; }
template <class T, class... U> void err2(T &&head, U &&...tail) {
cerr << head << " ";
err2(tail...);
}
template <class T, class... U> void err(T &&head, U &&...tail) {
cerr << head << " ";
err2(tail...);
cerr << "" << endl;
}
template <class T> void err(T &&head) { cerr << head << endl; }
void err() { cerr << "" << endl; }
// debで出力する最大長
constexpr int DEB_LEN = 20;
constexpr int DEB_LEN_H = 12;
string deb_tos(const int &v) {
if (abs(v) == inf || abs(v) == linf)
return "e";
else
return to_string(v);
}
template <class T> string deb_tos(const T &a) {
stringstream ss;
ss << a;
return ss.str();
}
string deb_tos(const P &p) {
stringstream ss;
ss << "{";
ss << p.fi;
ss << ", ";
ss << p.se;
ss << "}";
return ss.str();
}
#ifdef use_epsdou
string deb_tos(const epsdou &a) { return deb_tos(a.v); }
#endif
template <class T> string deb_tos(const optional<T> &a) {
if (a.has_value()) {
return deb_tos(a.value());
} else
return "e";
}
template <class T> string deb_tos(const vector<T> &a, ll W = inf) {
stringstream ss;
if (W == inf)
W = min(sz(a), DEB_LEN);
if (sz(a) == 0)
return ss.str();
rep(i, W) {
ss << deb_tos(a[i]);
if (typeid(a[i]) == typeid(P)) {
ss << endl;
} else {
ss << " ";
}
}
return ss.str();
}
template <class T>
string deb_tos(const vector<vector<T>> &a, vi H, vi W, int key = -1) {
stringstream ss;
ss << endl;
vi lens(sz(W));
fora(h, H) {
rep(wi, sz(W)) {
if (sz(a[h]) <= W[wi])
break;
lens[wi] = max(lens[wi], sz(deb_tos(a[h][W[wi]])) + 1);
lens[wi] = max(lens[wi], sz(deb_tos(W[wi])) + 1);
}
}
if (key == -1)
ss << " *|";
else
ss << " " << key << "|";
int wi = 0;
fora(w, W) {
ss << std::right << std::setw(lens[wi]) << w;
wi++;
}
ss << "" << endl;
rep(i, sz(W)) rep(lens[i]) ss << "_";
rep(i, 3) ss << "_";
ss << "" << endl;
fora(h, H) {
ss << std::right << std::setw(2) << h << "|";
int wi = 0;
fora(w, W) {
if (sz(a[h]) <= w)
break;
ss << std::right << std::setw(lens[wi]) << deb_tos(a[h][w]);
wi++;
}
ss << "" << endl;
}
return ss.str();
}
template <class T>
string deb_tos(const vector<vector<T>> &a, ll H = inf, ll W = inf,
int key = -1) {
H = (H != inf) ? H : min({H, sz(a), DEB_LEN_H});
W = 0;
rep(h, H) { chma(W, sz(a[h])); }
chmi(W, DEB_LEN_H);
vi hs, ws;
rep(h, H) { hs.push_back(h); }
rep(w, W) { ws.push_back(w); }
return deb_tos(a, hs, ws, key);
}
/*@formatter:off*/
template <class T>
string deb_tos(const vector<vector<vector<T>>> &a, ll H = inf) {
stringstream ss;
if (H == inf)
H = DEB_LEN_H;
H = min(H, sz(a));
rep(i, H) {
ss << endl;
ss << deb_tos(a[i], inf, inf, i);
}
return ss.str();
}
template <class T>
string deb_tos(vector<set<T>> &a, ll H = inf, ll W = inf, int key = -1) {
vector<vector<T>> b(sz(a));
rep(i, sz(a)) {
fora(v, a[i]) { b[i].push_back(v); }
}
return deb_tos(b, H, W, key);
}
template <class T>
string deb_tos(vector<multiset<T>> &a, ll H = inf, ll W = inf, int key = -1) {
vector<vector<T>> b(sz(a));
rep(i, sz(a)) {
fora(v, a[i]) { b[i].push_back(v); }
}
return deb_tos(b, H, W, key);
}
template <class T> string deb_tos(const set<T> &S) {
vector<T> b;
fora(s, S) b += s;
return deb_tos(b);
}
template <class T, size_t A> string deb_tos(T (&a)[A]) {
return deb_tos(vector<T>(begin(a), end(a)));
}
template <class T, size_t A, size_t B> string deb_tos(T (&a)[A][B]) {
return deb_tos(vector<vector<T>>(begin(a), end(a)));
}
template <class T, size_t A, size_t B, size_t C>
string deb_tos(T (&a)[A][B][C]) {
return deb_tos(vector<vector<vector<T>>>(begin(a), end(a)));
}
/*@formatter:off*/
template <class T> void out2(T head) {
cout << head;
res_mes += deb_tos(head);
}
template <class T, class... U> void out2(T head, U... tail) {
cout << head << " ";
res_mes += deb_tos(head) + " ";
out2(tail...);
}
template <class T, class... U> void out(T head, U... tail) {
cout << head << " ";
res_mes += deb_tos(head) + " ";
out2(tail...);
cout << "" << endl;
res_mes += "\n";
}
template <class T> void out(T head) {
cout << head << endl;
res_mes += deb_tos(head) + "\n";
}
void out() { cout << "" << endl; }
#else
#define err(...) ;
template <class T> void out2(T &&head) { cout << head; }
template <class T, class... U> void out2(T &&head, U &&...tail) {
cout << head << " ";
out2(tail...);
}
template <class T, class... U> void out(T &&head, U &&...tail) {
cout << head << " ";
out2(tail...);
cout << "" << endl;
}
template <class T> void out(T &&head) { cout << head << endl; }
void out() { cout << "" << endl; }
#endif
template <class H, class... T>
void outl(const vector<H> &h, const vector<T> &...t) {
rep(i, sz(h)) out(h[i], t[i]...);
}
// テーブルをスペースなしで出力
template <class T> void outt(vector<vector<T>> &a) {
rep(i, sz(a)) {
rep(j, sz(a[i])) { cout << a[i][j]; }
cout << endl;
}
}
// int型をbit表記で出力
void outb(int a) { cout << bitset<20>(a) << endl; }
/*@formatter:off*/
template <class T> void na(vector<T> &a, ll n) {
a.resize(n);
rep(i, n) cin >> a[i];
}
template <class T> void na(set<T> &a, ll n) { rep(i, n) a.insert(in()); }
#define dna(a, n) \
vi a; \
na(a, n); /*nを複数使うと n==in()の時バグる事に注意*/
#define dnad(a, n) \
vi a; \
nad(a, n);
template <class T> void nao(vector<T> &a, ll n) {
a.resize(n + 1);
a[0] = 0;
rep(i, n) cin >> a[i + 1];
}
template <class T> void naod(vector<T> &a, ll n) {
a.resize(n + 1);
a[0] = 0;
rep(i, n) cin >> a[i + 1], a[i + 1]--;
}
template <class T> void nad(vector<T> &a, ll n) {
a.resize(n);
rep(i, n) cin >> a[i], a[i]--;
}
template <class T> void nad(set<T> &a, ll n) { rep(i, n) a.insert(in() - 1); }
template <class T, class U> void na2(vector<T> &a, vector<U> &b, ll n) {
a.resize(n);
b.resize(n);
rep(i, n) cin >> a[i] >> b[i];
}
template <class T, class U> void na2(set<T> &a, set<U> &b, ll n) {
rep(i, n) {
a.insert(in());
b.insert(in());
}
}
#define dna2(a, b, n) \
vi a, b; \
na2(a, b, n);
template <class T, class U> void nao2(vector<T> &a, vector<U> &b, ll n) {
a.resize(n + 1);
b.resize(n + 1);
a[0] = b[0] = 0;
rep(i, n) cin >> a[i + 1] >> b[i + 1];
}
#define dnao2(a, b, n) \
vi a, b; \
nao2(a, b, n);
template <class T, class U> void na2d(vector<T> &a, vector<U> &b, ll n) {
a.resize(n);
b.resize(n);
rep(i, n) cin >> a[i] >> b[i], a[i]--, b[i]--;
}
#define dna2d(a, b, n) \
vi a, b; \
na2d(a, b, n);
template <class T, class U, class W>
void na3(vector<T> &a, vector<U> &b, vector<W> &c, ll n) {
a.resize(n);
b.resize(n);
c.resize(n);
rep(i, n) cin >> a[i] >> b[i] >> c[i];
}
#define dna3(a, b, c, n) \
vi a, b, c; \
na3(a, b, c, n);
template <class T, class U, class W>
void na3d(vector<T> &a, vector<U> &b, vector<W> &c, ll n) {
a.resize(n);
b.resize(n);
c.resize(n);
rep(i, n) cin >> a[i] >> b[i] >> c[i], a[i]--, b[i]--, c[i]--;
}
#define dna3d(a, b, c, n) \
vi a, b, c; \
na3d(a, b, c, n);
template <class T, class U, class W, class X>
void na4(vector<T> &a, vector<U> &b, vector<W> &c, vector<X> &d, ll n) {
a.resize(n);
b.resize(n);
c.resize(n);
d.resize(n);
rep(i, n) cin >> a[i] >> b[i] >> c[i] >> d[i];
}
#define dna4(a, b, c, d, n) \
vi a, b, c, d; \
na4(a, b, c, d, n);
#define dna4d(a, b, c, d, n) \
vi a, b, c, d; \
na4d(a, b, c, d, n);
#define nt(a, h, w) \
resize(a, h, w); \
rep(nthi, h) rep(ntwi, w) cin >> a[nthi][ntwi];
#define ntd(a, h, w) \
resize(a, h, w); \
rep(ntdhi, h) rep(ntdwi, w) cin >> a[ntdhi][ntdwi], a[ntdhi][ntdwi]--;
#define ntp(a, h, w) \
resize(a, h + 2, w + 2); \
fill(a, '#'); \
rep(ntphi, 1, h + 1) rep(ntpwi, 1, w + 1) cin >> a[ntphi][ntpwi];
#define dnt(S, h, w) \
vvi(S, h, w); \
nt(S, h, w);
#define dntc(S, h, w) \
vvc(S, h, w); \
nt(S, h, w);
#define dnts(S, h, w) \
vvs(S, h, w); \
nt(S, h, w);
// デバッグ
#define sp << " " <<
/*@formatter:off*/
#define deb1(x) debugName(x) << " = " << deb_tos(x)
#define deb_2(x, ...) deb1(x) << ", " << deb1(__VA_ARGS__)
#define deb_3(x, ...) deb1(x) << ", " << deb_2(__VA_ARGS__)
#define deb_4(x, ...) deb1(x) << ", " << deb_3(__VA_ARGS__)
#define deb5(x, ...) deb1(x) << ", " << deb_4(__VA_ARGS__)
#define deb6(x, ...) deb1(x) << ", " << deb5(__VA_ARGS__)
// #define deb7(x, ...) deb1(x) <<", "<< deb6(__VA_ARGS__)
// #define deb8(x, ...) deb1(x) <<", "<< deb7(__VA_ARGS__)
// #define deb9(x, ...) deb1(x) <<", "<< deb8(__VA_ARGS__)
// #define deb10(x, ...) deb1(x) <<", "<< deb9(__VA_ARGS__)
/*@formatter:off*/
#ifdef _DEBUG
#define deb(...) \
do { \
if (cou_deb++ >= 1000) \
break; \
was_deb = true; \
cerr << over10(__VA_ARGS__, deb10, deb9, deb8, deb7, deb6, deb5, deb_4, \
deb_3, deb_2, deb1)(__VA_ARGS__) \
<< endl; \
} while (0)
void print_n_base(int x, int base) {
string S;
while (x) {
S += (char)('0' + x % base);
x /= base;
}
cerr << S << endl;
}
template <class T> void print_n_base(vector<T> X, int base) {
cerr << endl;
for (auto &&x : X) {
print_n_base(x, base);
}
cerr << endl;
}
// n進数
#define deb2(x) \
was_deb = true; \
cerr << debugName(x) << " = "; \
print_n_base(x, 2);
#define deb3(x) \
was_deb = true; \
cerr << debugName(x) << " = "; \
print_n_base(x, 3);
#define deb4(x) \
was_deb = true; \
cerr << debugName(x) << " = "; \
print_n_base(x, 4);
#define deb_ex_deb(x, len) debugName(x) << " = " << deb_tos(x, len)
#define call_deb_ex_deb(x, len) deb_ex_deb(x, len)
// 要素が存在する行だけ出力(vvt)
#define deb_ex(v) \
do { \
int N = sz(v); \
int s = N; \
int t = 0; \
rep(i, N) { \
if (sz(v[i])) { \
chmi(s, i); \
chma(t, i); \
} \
} \
auto ex_v = sub(v, s, N); \
str S = deb_tos(ex_v, sz(ex_v)); \
debugName(v); \
cerr << " = " << endl; \
cerr << S << endl; \
} while (0);
#define debi(A) \
{ \
int len = min(sz(A), 20); \
was_deb = true; \
cerr << debugName(A) << " = " << endl; \
rep(i, len) cerr << std::right \
<< std::setw((int)(sz(tos(A[i])) + (i ? 1 : 0))) \
<< (i % 10); \
cerr << endl; \
rep(i, len) cerr << std::right \
<< std::setw((int)(sz(tos(A[i])) + (i ? 1 : 0))) << A[i]; \
cerr << endl; \
}
template <class T, class F> string deb_tos_f(vector<T> &A, F f) {
vi I;
rep(i, sz(A)) {
if (f(A[i]))
I.push_back(i);
}
was_deb = true;
stringstream ss;
fora(ii, i, I) ss << std::right
<< std::setw(
(int)(max(sz(tos(A[i])), sz(tos(i))) + (ii ? 1 : 0)))
<< i;
ss << endl;
fora(ii, i, I) ss << std::right
<< std::setw(
(int)(max(sz(tos(A[i])), sz(tos(i))) + (ii ? 1 : 0)))
<< A[i];
return ss.str();
}
template <class T, class F>
string deb_tos_f(vector<vector<T>> &a, F f, int key = -1) {
vi hs, ws_;
int H = sz(a), W = sz(a[0]);
vi exh(H), exw(W);
rep(h, H) {
rep(w, W) {
if (f(a[h][w])) {
exh[h] = true;
exw[w] = true;
}
}
}
rep(h, H) if (exh[h]) hs.push_back(h);
rep(w, W) if (exw[w]) ws_.push_back(w);
return deb_tos(a, hs, ws_, key);
}
template <class T, class F>
string deb_tos_f(vector<vector<vector<T>>> &a, F f) {
stringstream ss;
int H = sz(a);
if (sz(a) == 0)
return ss.str();
int cou = 0;
rep(i, H) {
if (any_of(a[i], v, f(v))) {
ss << deb_tos_f(a[i], f, i);
cou++;
}
if (cou > 50)
break;
}
ss << "" << endl;
return ss.str();
}
#define debf_normal(tab, f) \
do { \
cerr << debugName(tab) << " = " << endl; \
cerr << deb_tos_f(tab, f) << endl; \
} while (0);
#define debf2(tab, siki_r) debf_normal(tab, lamr(siki_r))
#define debf3(tab, v, siki) debf_normal(tab, lam(siki))
// S, sikir
// S, v, siki
#define debf(...) over3(__VA_ARGS__, debf3, debf2, debf1)(__VA_ARGS__)
#else
#define deb(...) ;
#define deb2(...) ;
#define deb3(...) ;
#define deb4(...) ;
#define deb_ex(...) ;
#define debf(...) ;
#define debi(...) ;
#endif
#define debugline(x) \
cerr << x << " " \
<< "(L:" << __LINE__ << ")" << '\n'
/*@formatter:off*/
using u32 = unsigned;
using u64 = unsigned long long;
using u128 = __uint128_t;
using bint = __int128;
std::ostream &operator<<(std::ostream &dest, __int128_t value) {
std::ostream::sentry s(dest);
if (s) {
__uint128_t tmp = value < 0 ? -value : value;
char buffer[128];
char *d = std::end(buffer);
do {
--d;
*d = "0123456789"[tmp % 10];
tmp /= 10;
} while (tmp != 0);
if (value < 0) {
--d;
*d = '-';
}
ll len = std::end(buffer) - d;
if (dest.rdbuf()->sputn(d, len) != len) {
dest.setstate(std::ios_base::badbit);
}
}
return dest;
}
__int128 to_bint(string &s) {
__int128 ret = 0;
for (ll i = 0; i < (ll)s.length(); ++i)
if ('0' <= s[i] && s[i] <= '9')
ret = 10 * ret + s[i] - '0';
return ret;
}
void operator>>(istream &iss, bint &v) {
string S;
iss >> S;
v = 0;
rep(i, sz(S)) {
v *= 10;
v += S[i] - '0';
}
}
// 便利関数
/*@formatter:off*/
// テスト用
#define rand xor128_
unsigned long xor128_(void) {
static unsigned long x = 123456789, y = 362436069, z = 521288629,
w = 88675123;
unsigned long t;
t = (x ^ (x << 11));
x = y;
y = z;
z = w;
return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}
char ranc() { return (char)('a' + rand() % 26); }
ll rand(ll min, ll max) {
assert(min <= max);
if (min >= 0 && max >= 0) {
return rand() % (max + 1 - min) + min;
} else if (max < 0) {
return -rand(-max, -min);
} else {
if (rand() % 2) {
return rand(0, max);
} else {
return -rand(0, -min);
}
}
}
ll rand(ll max) { return rand(0, max); }
template <class T> T rand(vector<T> &A) { return A[rand(sz(A) - 1)]; }
// 重複することがある
template <class T> vector<T> ranv(vector<T> &A, int N) {
vector<T> ret(N);
rep(i, N) { ret[i] = rand(A); }
return ret;
}
template <class T> vector<T> ranv_unique(vector<T> &A, int N) {
vector<T> ret(N);
umapi was;
rep(j, N) {
int i;
while (1) {
i = rand(sz(A) - 1);
if (was.find(i) == was.end())
break;
}
ret[j] = A[i];
was[i] = 1;
}
return ret;
}
vi ranv(ll n, ll min, ll max) {
vi v(n);
rep(i, n) v[i] = rand(min, max);
return v;
}
/*@formatter:off*/
#ifdef _DEBUG
bool timeup(int time) {
static bool never = true;
if (never)
message += "may timeup, because slow";
never = false;
auto end_time = system_clock::now();
auto part = duration_cast<milliseconds>(end_time - start_time);
auto lim = milliseconds(time);
return part >= lim;
}
#else
bool timeup(int time) {
auto end_time = system_clock::now();
auto part = duration_cast<milliseconds>(end_time - start_time);
auto lim = milliseconds(time);
return part >= lim;
}
#endif
void set_time() { past_time = system_clock::now(); }
// MS型(millisecqnds)で返る
// set_timeをしてからの時間
auto calc_time_milli() {
auto now = system_clock::now();
auto part = duration_cast<milliseconds>(now - past_time);
return part;
}
auto calc_time_micro() {
auto now = system_clock::now();
auto part = duration_cast<microseconds>(now - past_time);
return part;
}
auto calc_time_nano() {
auto now = system_clock::now();
auto part = duration_cast<nanoseconds>(now - past_time);
return part;
}
bool calc_time(int zikan) { return calc_time_micro() >= microseconds(zikan); }
using MS = std::chrono::microseconds;
int div(microseconds a, microseconds b) { return a / b; }
int div(nanoseconds a, nanoseconds b) {
if (b < nanoseconds(1)) {
return a / nanoseconds(1);
}
int v = a / b;
return v;
}
// set_time();
// rep(i,lim)shori
// lim*=time_nanbai();
// rep(i,lim)shoriと使う
// 全体でmilliかかっていいときにlimを何倍してもう一回できるかを返す
int time_nanbai(int milli) {
auto dec = duration_cast<nanoseconds>(past_time - start_time);
auto part = calc_time_nano();
auto can_time = nanoseconds(milli * 1000 * 1000);
can_time -= part;
can_time -= dec;
return div(can_time, part);
}
/*@formatter:off*/
// #define use_rand
#ifdef use_rand
str ransu(ll n) {
str s;
rep(i, n) s += (char)rand('A', 'Z');
return s;
}
str ransl(ll n) {
str s;
rep(i, n) s += (char)rand('a', 'z');
return s;
}
// 単調増加
vi ranvinc(ll n, ll min, ll max) {
vi v(n);
bool bad = 1;
while (bad) {
bad = 0;
v.resize(n);
rep(i, n) {
if (i && min > max - v[i - 1]) {
bad = 1;
break;
}
if (i)
v[i] = v[i - 1] + rand(min, max - v[i - 1]);
else
v[i] = rand(min, max);
}
}
return v;
}
// 便利 汎用
#endif
void ranvlr(ll n, ll min, ll max, vi &l, vi &r) {
l.resize(n);
r.resize(n);
rep(i, n) {
l[i] = rand(min, max);
r[i] = l[i] + rand(0, max - l[i]);
}
}
template <class Iterable, class T = typename Iterable::value_type>
vector<pair<T, int>> run_length(const Iterable &a) {
vector<pair<T, int>> ret;
ret.eb(a[0], 1);
rep(i, 1, sz(a)) {
if (ret.back().fi == a[i]) {
ret.back().se++;
} else {
ret.eb(a[i], 1);
}
}
return ret;
}
/*@formatter:off*/
// #define use_mgr //_goldd _goldt
#ifdef use_mgr
//->[i, f(i)]
template <class T, class U, class F>
auto mgr(T ok, U ng, const F &f,
require_arg(is_integral<T>::value &&is_integral<U>::value)) {
auto mid = (ok + ng);
if (ok < ng)
while (ng - ok > 1) {
mid = (ok + ng) >> 1;
if (f(mid))
ok = mid;
else
ng = mid;
}
else
while (ok - ng > 1) {
mid = (ok + ng) >> 1;
if (f(mid))
ok = mid;
else
ng = mid;
}
return ok;
}
//[l, r)の中で,f(i)がtrueとなる範囲を返す okはそこに含まれる
template <class F> P mgr_range(int l, int r, F f, int ok) {
if (f(ok) == 0) {
out("f(ok) must true");
re();
}
return mp(mgr(ok, l - 1, f), mgr(ok, r, f) + 1);
}
template <class F> auto mgrd(dou ok, dou ng, F f, int kai = 100) {
if (ok < ng)
rep(i, kai) {
dou mid = (ok + ng) / 2;
if (f(mid))
ok = mid;
else
ng = mid;
}
else
rep(i, kai) {
dou mid = (ok + ng) / 2;
if (f(mid))
ok = mid;
else
ng = mid;
}
return ok;
}
template <class F> dou mgrd_time(dou ok, dou ng, F f, int time = 1980) {
bool han = true;
if (ok < ng)
while (1) {
dou mid = (ok + ng) / 2;
if (f(mid))
ok = mid, han = true;
else
ng = mid, han = false;
deb(mid, han);
if (timeup(time)) {
break;
}
}
else
while (1) {
dou mid = (ok + ng) / 2;
if (f(mid))
ok = mid, han = true;
else
ng = mid, han = false;
deb(mid, han);
if (timeup(time)) {
break;
}
}
return ok;
}
// todo 減らす
template <class F> auto goldd_l(ll left, ll right, F calc) {
double GRATIO = 1.6180339887498948482045868343656;
ll lm = left + (ll)((right - left) / (GRATIO + 1.0));
ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0));
ll fl = calc(lm);
ll fr = calc(rm);
while (right - left > 10) {
if (fl < fr) {
right = rm;
rm = lm;
fr = fl;
lm = left + (ll)((right - left) / (GRATIO + 1.0));
fl = calc(lm);
} else {
left = lm;
lm = rm;
fl = fr;
rm = lm + (ll)((right - lm) / (GRATIO + 1.0));
fr = calc(rm);
}
}
ll minScore = MAX<ll>();
ll resIndex = left;
for (ll i = left; i < right + 1; ++i) {
ll score = calc(i);
if (minScore > score) {
minScore = score;
resIndex = i;
}
}
return make_tuple(resIndex, calc(resIndex));
}
template <class F> auto goldt_l(ll left, ll right, F calc) {
double GRATIO = 1.6180339887498948482045868343656;
ll lm = left + (ll)((right - left) / (GRATIO + 1.0));
ll rm = lm + (ll)((right - lm) / (GRATIO + 1.0));
ll fl = calc(lm);
ll fr = calc(rm);
while (right - left > 10) {
if (fl > fr) {
right = rm;
rm = lm;
fr = fl;
lm = left + (ll)((right - left) / (GRATIO + 1.0));
fl = calc(lm);
} else {
left = lm;
lm = rm;
fl = fr;
rm = lm + (ll)((right - lm) / (GRATIO + 1.0));
fr = calc(rm);
}
}
if (left > right) {
ll l = left;
left = right;
right = l;
}
ll maxScore = MIN<ll>();
ll resIndex = left;
for (ll i = left; i < right + 1; ++i) {
ll score = calc(i);
if (maxScore < score) {
maxScore = score;
resIndex = i;
}
}
return make_tuple(resIndex, calc(resIndex));
}
/*loopは200にすればおそらく大丈夫 余裕なら300に*/
template <class F> auto goldd_d(dou left, dou right, F calc, ll loop = 200) {
dou GRATIO = 1.6180339887498948482045868343656;
dou lm = left + ((right - left) / (GRATIO + 1.0));
dou rm = lm + ((right - lm) / (GRATIO + 1.0));
dou fl = calc(lm);
dou fr = calc(rm); /*200にすればおそらく大丈夫*/ /*余裕なら300に*/
ll k = 141;
loop++;
while (--loop) {
if (fl < fr) {
right = rm;
rm = lm;
fr = fl;
lm = left + ((right - left) / (GRATIO + 1.0));
fl = calc(lm);
} else {
left = lm;
lm = rm;
fl = fr;
rm = lm + ((right - lm) / (GRATIO + 1.0));
fr = calc(rm);
}
}
return make_tuple(left, calc(left));
}
template <class F> auto goldt_d(dou left, dou right, F calc, ll loop = 200) {
double GRATIO = 1.6180339887498948482045868343656;
dou lm = left + ((right - left) / (GRATIO + 1.0));
dou rm = lm + ((right - lm) / (GRATIO + 1.0));
dou fl = calc(lm);
dou fr = calc(rm);
loop++;
while (--loop) {
if (fl > fr) {
right = rm;
rm = lm;
fr = fl;
lm = left + ((right - left) / (GRATIO + 1.0));
fl = calc(lm);
} else {
left = lm;
lm = rm;
fl = fr;
rm = lm + ((right - lm) / (GRATIO + 1.0));
fr = calc(rm);
}
}
return make_tuple(left, calc(left));
}
// l ~ rを複数の区間に分割し、極致を与えるiを返す time-20 msまで探索
template <class F> auto goldd_ls(ll l, ll r, F calc, ll time = 2000) {
auto lim = milliseconds(time - 20);
ll mini = 0, minv = MAX<ll>(); /*区間をk分割する*/
rep(k, 1, inf) {
auto s = system_clock::now();
ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/
ll nl = l;
ll nr = l + haba;
rep(i, k) {
ll ni = goldd_l(nl, nr, calc);
if (chmi(minv, calc(ni)))
mini = ni;
nl = nr;
nr = nl + haba;
}
auto end = system_clock::now();
auto part = duration_cast<milliseconds>(end - s);
auto elapsed = duration_cast<milliseconds>(end - start_time);
if (elapsed + part * 2 >= lim) {
break;
}
}
return make_tuple(mini, calc(mini));
}
template <class F> auto goldt_ls(ll l, ll r, F calc, ll time = 2000) {
auto lim = milliseconds(time - 20);
ll maxi = 0, maxv = MIN<ll>(); /*区間をk分割する*/
rep(k, 1, inf) {
auto s = system_clock::now();
ll haba = (r - l + k) / k; /*((r-l+1) + k-1) /k*/
ll nl = l;
ll nr = l + haba;
rep(i, k) {
ll ni = goldt_l(nl, nr, calc);
if (chma(maxv, calc(ni)))
maxi = ni;
nl = nr;
nr = nl + haba;
}
auto end = system_clock::now();
auto part = duration_cast<milliseconds>(end - s);
auto elapsed = duration_cast<milliseconds>(end - start_time);
if (elapsed + part * 2 >= lim) {
break;
}
}
return make_tuple(maxi, calc(maxi));
}
template <class F>
auto goldd_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を持つ*/
auto lim = milliseconds(time - 20);
dou mini = 0, minv = MAX<dou>(); /*区間をk分割する*/
rep(k, 1, inf) {
auto s = system_clock::now();
dou haba = (r - l) / k;
dou nl = l;
dou nr = l + haba;
rep(i, k) {
dou ni = goldd_d(nl, nr, calc);
if (chmi(minv, calc(ni)))
mini = ni;
nl = nr;
nr = nl + haba;
}
auto end = system_clock::now();
auto part = duration_cast<milliseconds>(end - s);
auto elapsed = duration_cast<milliseconds>(end - start_time);
if (elapsed + part * 2 >= lim) {
break;
}
}
return make_tuple(mini, calc(mini));
}
template <class F>
auto goldt_d_s(dou l, dou r, F calc, ll time = 2000) { /*20ms余裕を残している*/
auto lim = milliseconds(time - 20);
dou maxi = 0, maxv = MIN<dou>(); /*区間をk分割する*/
rep(k, 1, inf) {
auto s = system_clock::now();
dou haba = (r - l) / k;
dou nl = l;
dou nr = l + haba;
rep(i, k) {
dou ni = goldt_d(nl, nr, calc);
if (chma(maxv, calc(ni)))
maxi = ni;
nl = nr;
nr = nl + haba;
}
auto end = system_clock::now();
auto part = duration_cast<milliseconds>(end - s);
auto elapsed = duration_cast<milliseconds>(end - start_time);
if (elapsed + part * 2 >= lim) {
break;
}
}
return make_tuple(maxi, calc(maxi));
}
#endif
// strを整数として比較
string smax(str &a, str b) {
if (sz(a) < sz(b)) {
return b;
} else if (sz(a) > sz(b)) {
return a;
} else if (a < b)
return b;
else
return a;
}
// strを整数として比較
string smin(str &a, str b) {
if (sz(a) > sz(b)) {
return b;
} else if (sz(a) < sz(b)) {
return a;
} else if (a > b)
return b;
else
return a;
}
// エラーN
// 多次元なら(-1,-1,-1)
template <typename W, typename T> ll find(vector<W> &a, int l, const T key) {
rep(i, l, sz(a)) if (a[i] == key) return i;
return sz(a);
}
template <typename W, typename T> ll find(vector<W> &a, const T key) {
rep(i, sz(a)) if (a[i] == key) return i;
return sz(a);
}
template <typename W, typename T> P find(vector<vector<W>> &a, const T key) {
rep(i, sz(a)) rep(j, sz(a[0])) if (a[i][j] == key) return mp(i, j);
return mp(-1, -1);
}
// getid(find())を返す 1次元にする
template <typename W, typename T> int findi(vector<vector<W>> &a, const T key) {
rep(i, sz(a)) rep(j, sz(a[0])) if (a[i][j] == key) return i * sz(a[0]) + j;
return -1;
}
template <typename W, typename U>
tuple<int, int, int> find(vector<vector<vector<W>>> &a, const U key) {
rep(i, sz(a)) rep(j, sz(a[0]))
rep(k, sz(a[0][0])) if (a[i][j][k] ==
key) return tuple<int, int, int>(i, j, k);
return tuple<int, int, int>(-1, -1, -1);
}
// 無ければN
int find(string &s, const string key) {
int klen = sz(key);
rep(i, sz(s) - klen + 1) {
if (s[i] != key[0])
continue;
if (s.substr(i, klen) == key) {
return i;
}
}
return sz(s);
}
int find(string &s, int l, const string key) {
int klen = sz(key);
rep(i, l, sz(s) - klen + 1) {
if (s[i] != key[0])
continue;
if (s.substr(i, klen) == key) {
return i;
}
}
return sz(s);
}
int find(string &s, const char key) {
rep(i, sz(s)) {
if (s[i] == key)
return i;
}
return sz(s);
}
int find(string &s, int l, const char key) {
rep(i, l, sz(s)) {
if (s[i] == key)
return i;
}
return sz(s);
}
// N箇所について(自分含む)右のkeyの場所を返す
template <typename W, typename T> vi finds(const W &a, const T &key) {
int n = sz(a);
vi rpos(n, n);
rer(i, n - 1) {
if (i < n - 1) {
rpos[i] = rpos[i + 1];
}
if (a[i] == key)
rpos[i] = i;
}
return rpos;
}
template <typename W, typename T> vi rfinds(const W &a, const T &key) {
int n = sz(a);
vi lpos(n, -1);
rep(i, n) {
if (i > 0) {
lpos[i] = lpos[i - 1];
}
if (a[i] == key)
lpos[i] = i;
}
return lpos;
}
// todoz
#if __cplusplus >= 201703L
template <typename W, typename T, class Iterable = typename W::value_type>
ll count(const W &a, const T &k) {
return count_if(a, == k);
}
template <typename W, class Iterable = typename W::value_type>
vi count(const W &a) {
vi res;
for_each(a, v, if (sz(res) <= (int)v) res.resize((int)v + 1); res[v]++;);
return res;
}
#else
ll count(const string &S, const char &c) {
int res = 0;
fora(s, S) { res += s == c; }
return res;
}
#endif
ll count(const str &a, const str &k) {
ll ret = 0, len = k.length();
auto pos = a.find(k);
while (pos != string::npos)
pos = a.find(k, pos + len), ++ret;
return ret;
}
/*@formatter:off*/
//'a' = 'A' = 0 として集計 既に-'a'されていても動く
vi count(str &a, int l, int r) {
vi cou(26);
char c = 'a';
if ('A' <= a[l] && a[l] <= 'Z')
c = 'A';
if ('a' <= a[l] && a[l] <= 'z')
c = 'a';
else
c = 0;
rep(i, l, r)++ cou[a[i] - c];
return cou;
}
#define couif count_if
// algorythm
ll rev(ll a) {
ll res = 0;
while (a) {
res *= 10;
res += a % 10;
a /= 10;
}
return res;
}
template <class T> auto rev(const vector<T> &a) {
auto b = a;
reverse(ALL(b));
return b;
}
/* \反転 */ template <class U> auto rev(vector<vector<U>> &a) {
vector<vector<U>> b(sz(a[0]), vector<U>(sz(a)));
rep(h, sz(a)) rep(w, sz(a[0])) b[w][h] = a[h][w];
return b;
}
/* |反転 */ template <class U> auto revw(vector<vector<U>> &a) {
vector<vector<U>> b(sz(a), vector<U>(sz(a[0])));
int W = sz(a[0]);
rep(h, sz(a)) rep(w, sz(a[0])) { b[h][W - 1 - w] = a[h][w]; }
return b;
}
/* ー反転 */ template <class U> auto revh(vector<vector<U>> &a) {
vector<vector<U>> b(sz(a), vector<U>(sz(a[0])));
int H = sz(a);
rep(h, sz(a)) rep(w, sz(a[0])) { b[H - 1 - h][w] = a[h][w]; }
return b;
}
/* /反転 */ template <class U> auto revr(vector<vector<U>> &a) {
vector<vector<U>> b(sz(a[0]), vector<U>(sz(a)));
int H = sz(a);
int W = sz(a[0]);
rep(h, sz(a)) rep(w, sz(a[0])) b[w][h] = a[H - 1 - h][W - 1 - w];
return b;
}
auto rev(const string &a) {
string b = a;
reverse(ALL(b));
return b;
}
template <class T> auto rev(const T &v, int i) { return v[sz(v) - 1 - i]; }
int rev(int N, int i) { return N - 1 - i; }
constexpr ll p10[] = {1,
10,
100,
1000,
10000,
100000,
1000000,
10000000,
100000000,
1000000000,
10000000000ll,
100000000000ll,
1000000000000ll,
10000000000000ll,
100000000000000ll,
1000000000000000ll,
10000000000000000ll,
100000000000000000ll,
1000000000000000000ll};
// 0は0桁
ll keta(ll v, int if_zero_res) {
if (!v)
return if_zero_res;
if (v < p10[9]) {
if (v < p10[4]) {
if (v < p10[2]) {
if (v < p10[1]) {
if (v < p10[0])
return 0;
else
return 1;
} else
return 2;
} else {
if (v < p10[3])
return 3;
else
return 4;
}
} else {
if (v < p10[7]) {
if (v < p10[5])
return 5;
else if (v < p10[6])
return 6;
else
return 7;
} else {
if (v < p10[8])
return 8;
else
return 9;
}
}
} else {
if (v < p10[13]) {
if (v < p10[11]) {
if (v < p10[10])
return 10;
else
return 11;
} else {
if (v < p10[12])
return 12;
else
return 13;
}
} else {
if (v < p10[15]) {
if (v < p10[14])
return 14;
else
return 15;
} else {
if (v < p10[17]) {
if (v < p10[16])
return 16;
else
return 17;
} else {
if (v < p10[18])
return 18;
else
return 19;
}
}
}
}
}
#if __cplusplus >= 201703L
ll getr(ll a, ll keta) { return (a / pow<ll>(10, keta)) % 10; }
#else
ll getr(ll a, ll keta) { return (a / (int)pow(10, keta)) % 10; }
#endif
// 上から何桁目か
ll getl(ll a, ll ket) {
int sketa = keta(a, 1);
return getr(a, sketa - 1 - ket);
}
ll dsum(ll v, ll sin = 10) {
ll ret = 0;
for (; v; v /= sin)
ret += v % sin;
return ret;
}
ll mask10(ll v) { return p10[v] - 1; }
// 変換系
template <class T, class U>
auto to_v1(vector<reference_wrapper<U>> &ret, vector<T> &A) {
rep(i, sz(A)) ret.push_back(A[i]);
return ret;
}
template <class T, class U>
auto to_v1(vector<reference_wrapper<U>> &ret, vector<vector<T>> &A) {
rep(i, sz(A)) to_v1(ret, A[i]);
return ret;
}
// 参照付きで1次元に起こす
template <class T> auto to_v1(vector<vector<T>> &A) {
vector<reference_wrapper<decl2_t<decltype(A)>>> ret;
rep(i, sz(A)) to_v1(ret, A[i]);
return ret;
}
//[v] := iとなるようなvectorを返す
// 存在しない物は-1
// 空でも動く(なぜか)
template <class T> auto keys(const T &a) {
vector<decltype((a.begin())->fi)> res;
for (auto &&k : a)
res.push_back(k.fi);
return res;
}
template <class T> auto values(const T &a) {
vector<decltype((a.begin())->se)> res;
for (auto &&k : a)
res.push_back(k.se);
return res;
}
// todo 可変長で
template <class T> constexpr T min(T a, T b, T c) {
return a >= b ? b >= c ? c : b : a >= c ? c : a;
}
template <class T> constexpr T max(T a, T b, T c) {
return a <= b ? b <= c ? c : b : a <= c ? c : a;
}
// 1次元のコンテナ
template <class T> int mini(const vector<T> &a) {
return min_element(ALL(a)) - a.begin();
}
template <class T> int maxi(const vector<T> &a) {
return max_element(ALL(a)) - a.begin();
}
template <class T> T sum(const T &A) { return A; }
template <class T> T sum(const vector<T> &A, int l = -1, int r = -1) {
T s = 0;
set_lr12(l, r, sz(A));
rep(i, l, r) s += sum(A[i]);
return s;
}
// 1次元のIterableに使える
template <class T, require_t(!has_value_type<T>::value)> T min(T &a) {
return a;
}
template <class V, require_t(has_value_type<V>::value)>
auto min(V &a, ll s = -1, ll n = -1) {
using Ret_t = decl2_t<V>;
auto ret = MAX<Ret_t>();
set_lr12(s, n, sz(a));
if (s == n) {
return INF<Ret_t>();
} else {
rep(i, s, n) { ret = min(ret, min(a[i])); }
return ret;
}
}
template <class T, require_t(!has_value_type<T>::value)> T max(T &a) {
return a;
}
template <class V, require_t(has_value_type<V>::value)>
auto max(V &a, ll s = -1, ll n = -1) {
using Ret_t = decl2_t<V>;
auto ret = MIN<Ret_t>();
set_lr12(s, n, sz(a));
if (s == n) {
return -INF<Ret_t>();
} else {
rep(i, s, n) { ret = max(ret, max(a[i])); }
return ret;
}
}
template <class T> T mul(vector<T> &v, ll t = inf) {
T ret = v[0];
rep(i, 1, min(t, sz(v))) ret *= v[i];
return ret;
}
// template<class T, class U, class... W> auto sumn(vector<T> &v, U head, W...
// tail) { auto ret = sum(v[0], tail...); rep(i, 1, min(sz(v), head))ret
// += sum(v[i], tail...); return ret;} indexを持つvectorを返す
/*@formatter:off*/
template <typename W, typename T> void fill(W &xx, const T vall) { xx = vall; }
template <typename W, typename T> void fill(vector<W> &vecc, const T vall) {
for (auto &&vx : vecc)
fill(vx, vall);
}
template <typename W, typename T> void fill(vector<W> &xx, const T v, ll len) {
rep(i, len) xx[i] = v;
}
template <typename W, typename T>
void fill(vector<W> &xx, const T v, int s, ll t) {
rep(i, s, t) xx[i] = v;
}
template <typename W, typename T>
void fill(vector<vector<W>> &xx, T v, int sh, int th, int sw, int tw) {
rep(h, sh, th) rep(w, sw, tw) xx[h][w] = v;
}
// #define use_fill //_sum _array _max _min
#ifdef use_fill
template <typename A, size_t N, typename T> void fill(A (&a)[N], const T &v) {
rep(i, N) { fill(a[i], v); }
}
template <class T, class U> void fill(vector<T> &a, const vi &ind, U val) {
fora(v, ind) { a[v] = val; }
}
template <typename A, size_t N> auto sum(A (&a)[N], int l = -1, int r = -1) {
set_lr12(l, r, N);
auto res = sum(a[l]);
if (l == r) {
return res - res;
}
rep(i, l + 1, r) res += sum(a[i]);
return res;
}
template <typename A, size_t N> auto max(A (&a)[N], int l = -1, int r = -1) {
set_lr12(l, r, N);
auto res = max(a[l]);
if (l == r) {
return -INF<decltype(res)>();
}
rep(i, l + 1, r) res = max(res, max(a[i]));
return res;
}
template <typename A, size_t N> auto min(A (&a)[N], int l = -1, int r = -1) {
set_lr12(l, r, N);
auto res = min(a[l]);
if (l == r) {
return INF<decltype(res)>();
}
rep(i, l + 1, r) res = min(res, min(a[i]));
return res;
}
#endif
vi inds_(vi &a) {
int n = max(a) + 1;
vi ret(n, -1);
rep(i, sz(a)) {
assert(ret[a[i]] == -1);
ret[a[i]] = i;
}
return ret;
}
void clear(PQ &q) { q = PQ(); }
void clear(priority_queue<int> &q) { q = priority_queue<int>(); }
template <class T> void clear(queue<T> &q) {
while (q.size())
q.pop();
}
// template<class T> T *negarr(ll size) { T *body = (T *) malloc((size * 2 +
// 1) * sizeof(T)); return body + size;} template<class T> T *negarr2(ll h,
// ll w) { double **dummy1 = new double *[2 * h + 1]; double *dummy2 = new
// double[(2 * h + 1) * (2 * w + 1)]; dummy1[0] = dummy2 + w; for (ll i =
// 1; i <= 2 * h + 1; ++i) { dummy1[i] = dummy1[i - 1] + 2 * w + 1; } double
// **a = dummy1 + h; return a;}
template <class T> struct ruiC {
vector<T> rui;
ruiC(vector<T> &ru) : rui(ru) {}
/*先頭0*/
ruiC() : rui(1, 0) {}
T operator()(ll l, ll r) {
if (l > r) {
cerr << "ruic ";
deb(l, r);
assert(0);
}
return rui[r] - rui[l];
}
T operator()(int r = inf) { return operator()(0, min(r, sz(rui) - 1)); }
/*ruiv[]をruic[]に変えた際意味が変わるのがまずいため()と統一*/
/*単体iを返す 累積でないことに注意(seg木との統一でこうしている)*/
// T operator[](ll i) { return rui[i + 1] - rui[i]; }
T operator[](ll i) { return rui[i]; }
/*0から順に追加される必要がある*/
void operator+=(T v) { rui.push_back(rui.back() + v); }
void add(int i, T v) {
if (sz(rui) - 1 != i)
ole();
operator+=(v);
}
T back() { return rui.back(); }
ll size() { return rui.size(); }
auto begin() { return rui.begin(); }
auto end() { return rui.end(); }
};
template <class T> string deb_tos(const ruiC<T> &a) { return deb_tos(a.rui); }
template <class T> ostream &operator<<(ostream &os, ruiC<T> a) {
fora(v, a.rui) { os << v << " "; }
return os;
}
template <class T> vector<T> ruiv(const vector<T> &a) {
vector<T> ret(a.size() + 1);
rep(i, a.size()) ret[i + 1] = ret[i] + a[i];
return ret;
}
template <class T> ruiC<T> ruic(const vector<T> &a) {
vector<T> ret = ruiv(a);
return ruiC<T>(ret);
}
template <class T> ruiC<T> ruic() { return ruiC<T>(); }
// imoは0-indexed
// ruiは1-indexed
template <class T> vector<T> imo(const vector<T> &v) {
vector<T> ret = v;
rep(i, sz(ret) - 1) ret[i + 1] += ret[i];
return ret;
}
// #define use_rui //_imo _ruic _ruiv
#ifdef use_rui
// kと同じものの数
template <class T, class U> vi imo(const vector<T> &a, U k) {
vi equ(sz(a));
rep(i, sz(a)) { equ[i] = a[i] == k; }
return imo(equ);
}
template <class T> vector<T> imox(const vector<T> &v) {
vector<T> ret = v;
rep(i, sz(ret) - 1) ret[i + 1] ^= ret[i];
return ret;
}
// 漸化的に最小を持つ
template <class T> vector<T> imi(const vector<T> &v) {
vector<T> ret = v;
rep(i, sz(ret) - 1) chmi(ret[i + 1], ret[i]);
return ret;
}
template <class T> vector<T> ima(const vector<T> &v) {
vector<T> ret = v;
rep(i, sz(ret) - 1) chma(ret[i + 1], ret[i]);
return ret;
}
template <class T> vector<T> rimi(const vector<T> &v) {
vector<T> ret = v;
rer(i, sz(ret) - 1, 1) chmi(ret[i - 1], ret[i]);
return ret;
}
template <class T> vector<T> rima(const vector<T> &v) {
vector<T> ret = v;
rer(i, sz(ret) - 1, 1) chma(ret[i - 1], ret[i]);
return ret;
}
template <class T> struct ruimax {
template <typename Monoid> struct SegmentTree { /*pairで処理*/
int sz;
vector<Monoid> seg;
const Monoid M1 = mp(MIN<T>(), -1);
Monoid f(Monoid a, Monoid b) { return max(a, b); }
void build(vector<T> &a) {
int n = sz(a);
sz = 1;
while (sz < n)
sz <<= 1;
seg.assign(2 * sz, M1);
rep(i, n) { seg[i + sz] = mp(a[i], i); }
for (int k = sz - 1; k > 0; k--) {
seg[k] = f(seg[k << 1], seg[(k << 1) | 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]; }
};
private:
vector<T> ve;
SegmentTree<pair<T, int>> seg;
vector<T> rv;
vector<int> ri;
bool build = false;
public:
int n;
ruimax(vector<T> &a) : ve(a), n(sz(a)) {
int index = -1;
T ma = MIN<T>();
rv.resize(n + 1);
ri.resize(n + 1);
rv[0] = -INF<T>();
ri[0] = -1;
rep(i, n) {
if (chma(ma, a[i])) {
index = i;
}
rv[i + 1] = ma;
ri[i + 1] = index;
}
}
T operator()(int l, int r) {
if (!(l <= r && 0 <= l && r <= n)) {
deb(l, r, n);
assert(0);
}
if (l == 0) {
return rv[r];
} else {
if (!build)
seg.build(ve), build = true;
return seg.query(l, r).first;
}
}
T operator()(int r = inf) { return operator()(0, min(r, n)); }
T operator[](int r) { return operator()(0, r); }
T getv(int l, int r) { return operator()(l, r); }
T getv(int r = inf) { return getv(0, min(r, n)); };
int geti(int l, int r) {
assert(l <= r && 0 <= l && r <= n);
if (l == 0) {
return ri[r];
} else {
if (!build)
seg.build(ve), build = true;
return seg.query(l, r).second;
}
}
int geti(int r = inf) { return geti(0, min(r, n)); };
auto begin() { return rv.begin(); }
auto end() { return rv.end(); }
};
template <class T> struct ruimin {
template <typename Monoid> struct SegmentTree { /*pairで処理*/
int sz;
vector<Monoid> seg;
const Monoid M1 = mp(MAX<T>(), -1);
Monoid f(Monoid a, Monoid b) { return min(a, b); }
void build(vector<T> &a) {
int n = sz(a);
sz = 1;
while (sz < n)
sz <<= 1;
seg.assign(2 * sz, M1);
rep(i, n) { seg[i + sz] = mp(a[i], i); }
for (int k = sz - 1; k > 0; k--) {
seg[k] = f(seg[k << 1], seg[(k << 1) | 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]; }
};
private:
vector<T> ve;
SegmentTree<pair<T, int>> seg;
vector<T> rv;
vector<int> ri;
bool build = false;
int n;
public:
ruimin(vector<T> &a) : ve(a), n(sz(a)) {
int index = -1;
T mi = MAX<T>();
rv.resize(n + 1);
ri.resize(n + 1);
rv[0] = INF<T>();
ri[0] = -1;
rep(i, n) {
if (chmi(mi, a[i])) {
index = i;
}
rv[i + 1] = mi;
ri[i + 1] = index;
}
}
T operator()(int l, int r) {
assert(l <= r && 0 <= l && r <= n);
if (l == 0) {
return rv[r];
} else {
if (!build)
seg.build(ve), build = true;
return seg.query(l, r).first;
}
}
T operator()(int r = inf) { return operator()(0, min(r, n)); }
T operator[](int r) { return operator()(0, r); }
T getv(int l, int r) { return operator()(l, r); }
T getv(int r = inf) { return getv(0, min(r, n)); };
int geti(int l, int r) {
{
assert(l <= r && 0 <= l && r <= n);
if (l == 0) {
return ri[r];
} else {
if (!build)
seg.build(ve), build = true;
return seg.query(l, r).second;
}
}
assert(l <= r && 0 <= l && r <= n);
if (l == 0) {
return ri[r];
} else {
if (!build)
seg.build(ve), build = true;
return seg.query(l, r).second;
}
}
int geti(int r = inf) { return geti(0, min(r, n)); };
auto begin() { return rv.begin(); }
auto end() { return rv.end(); }
}; /*@formatter:off*/
vvi() ruib(vi &a) {
vvi(res, 61, sz(a) + 1);
rep(k, 61) {
rep(i, sz(a)) { res[k][i + 1] = res[k][i] + ((a[i] >> k) & 1); }
}
return res;
}
vector<ruiC<int>> ruibc(vi &a) {
vector<ruiC<int>> ret(61);
vvi(res, 61, sz(a));
rep(k, 61) {
rep(i, sz(a)) { res[k][i] = (a[i] >> k) & 1; }
ret[k] = ruic(res[k]);
}
return ret;
}
// kと同じものの数
template <class T, class U> vi ruiv(T &a, U k) {
vi ret(a.size() + 1);
rep(i, a.size()) ret[i + 1] = ret[i] + (a[i] == k);
return ret;
}
template <class T, class U> ruiC<ll> ruic(T &a, U k) {
vi ret = ruiv(a, k);
return ruiC<ll>(ret);
}
template <class T> struct ruiC2 {
int H;
vector<ruiC<T>> rui;
ruiC<T> dummy; // 変なのをよばれたときはこれを返す//todo
ruiC2(const vector<vector<T>> &ru) : rui(sz(ru)), H(sz(ru)) {
for (int h = 0; h < H; h++) {
if (sz(ru[h]) == 0)
continue;
if (sz(dummy) == 1)
dummy = ruic(vector<T>(sz(ru[h])));
rui[h] = ruic(ru[h]);
}
}
// WについてHを返す
vector<T> operator()(ll l, ll r) {
if (l > r) {
cerr << "ruic ";
deb(l, r);
assert(0);
}
vector<T> res(H);
for (int h = 0; h < H; h++)
res[h] = rui[h](l, r);
return res;
}
// HについてWを返す
ruiC<T> &operator[](ll h) {
#ifdef _DEBUG
if (h >= H) {
message += "warning ruiC h >= H";
}
#endif
if (h >= H || sz(rui[h]) == 1)
return dummy;
else
return rui[h];
}
/*@formatter:off*/
// vector<T> operator()(int r) { return operator()(0, r); }
/*ruiv[]をruic[]に変えた際意味が変わるのがまずいため()と統一*/
/*単体iを返す 累積でないことに注意(seg木との統一でこうしている)*/
// T operator[](ll i) { return rui[i + 1] - rui[i]; }
/*0から順に追加される必要がある*/
// T back() { return rui.back(); }
// ll size() { return rui.size(); }
// auto begin(){return rui.begin();}
// auto end(){return rui.end();}
};
template <class T, class U> ruiC<ll> ruicou(vector<T> &a, U b) {
vi cou(sz(a));
rep(i, sz(a)) { cou[i] = a[i] == b; }
return ruic(cou);
}
// メモリは形式によらず(26*N)
// rui(l,r)でvector(26文字について, l~rのcの個数)
// rui[h] ruic()を返す
// 添え字は'a', 'A'のまま扱う
// (予め-='a','A'されているものが渡されたらそれに従う)
template <typename Iterable, class is_Iterable = typename Iterable::value_type>
ruiC2<ll> ruicou(const Iterable &a) {
int H = max(a) + 1;
vvi(cou, H);
rep(i, sz(a)) {
if (sz(cou[a[i]]) == 0)
cou[a[i]].resize(sz(a));
cou[a[i]][i] = 1;
}
return ruiC2<ll>(cou);
}
/*@formatter:off*/
// h query
template <class T> vector<T> imoh(vector<vector<T>> &v, int w) {
vector<T> ret(sz(v));
rep(h, sz(ret)) { ret[h] = v[h][w]; }
rep(i, sz(ret) - 1) { ret[i + 1] += ret[i]; }
return ret;
}
template <class T> vector<T> ruih(vector<vector<T>> &v, int w) {
vector<T> ret(sz(v) + 1);
rep(h, sz(v)) { ret[h + 1] = v[h][w]; }
rep(i, sz(v)) { ret[i + 1] += ret[i]; }
return ret;
}
template <class T> ruiC<T> ruihc(vector<vector<T>> &a, int w) {
vector<T> ret = ruih(a, w);
return ruiC<T>(ret);
}
// xor
template <class T> struct ruixC {
vector<T> rui;
ruixC(vector<T> &ru) : rui(ru) {}
T operator()(ll l, ll r) {
if (l > r) {
cerr << "ruiXc ";
deb(l, r);
assert(0);
}
return rui[r] ^ rui[l];
}
T operator[](ll i) { return rui[i]; }
T back() { return rui.back(); }
ll size() { return rui.size(); }
};
template <class T> vector<T> ruix(const vector<T> &a) {
vector<T> ret(a.size() + 1);
rep(i, a.size()) ret[i + 1] = ret[i] ^ a[i];
return ret;
}
template <class T> ruixC<ll> ruixc(vector<T> &a) {
vi ret = ruix(a);
return ruixC<ll>(ret);
}
// 差分を返す(累積を取ると元に戻る)
// 101なら
// 1111を返す
// 元の配列で[l, r)へのxorは
//[l]と[r]へのxorになる https://atcoder.jp/contests/abc155/tasks/abc155_f
vi ruix_diff(vi &A) {
int N = sz(A);
assert(N);
vi res(N + 1);
res[0] = A[0];
rep(i, 1, N) { res[i] = A[i - 1] ^ A[i]; }
res[N] = A[N - 1];
return res;
}
template <class T> vector<T> ruim(vector<T> &a) {
vector<T> res(a.size() + 1, 1);
rep(i, a.size()) res[i + 1] = res[i] * a[i];
return res;
}
// 漸化的に最小を1indexで持つ
template <class T> vector<T> ruimi(vector<T> &a) {
ll n = sz(a);
vector<T> ret(n + 1);
rep(i, 1, n) {
ret[i] = a[i - 1];
chmi(ret[i + 1], ret[i]);
}
return ret;
}
// template<class T> T *rrui(vector<T> &a) {
// 右から左にかけての半開区間 (-1 n-1]
template <class T> struct rruiC {
vector<T> rui;
int n;
rruiC(vector<T> &a) : n(sz(a)) {
rui.resize(n + 1);
rer(i, n - 1) { rui[i] = rui[i + 1] + a[i]; }
}
/*[r l)*/
T operator()(int r, int l) {
r++;
l++;
assert(l <= r && l >= 0 && r <= n);
return rui[l] - rui[r];
}
T operator()(int l) { return operator()(n - 1, l); }
T operator[](int i) { return operator()(i); }
};
template <class T> ostream &operator<<(ostream &os, rruiC<T> a) {
fora(v, a.rui) { os << v << " "; }
return os;
}
template <class T> string deb_tos(rruiC<T> &a) { return deb_tos(a.rui); }
#define rrui rruic
template <class T> rruiC<T> rruic(vector<T> &a) { return rruiC<T>(a); }
// 掛け算
template <class T> struct ruimulC {
vector<T> rv;
int n;
ruimulC(vector<T> &a) : rv(a), n(sz(a)) {
rv.resize(n + 1);
rv[0] = 1;
rep(i, n) { rv[i + 1] = a[i] * rv[i]; }
}
ruimulC() : n(0) {
rv.resize(n + 1);
rv[0] = 1;
}
void operator+=(T v) {
rv.push_back(rv.back() * v);
n++;
}
T operator()(int l, int r) {
assert(l <= r && 0 <= l && r <= n);
return rv[r] / rv[l];
}
T operator()(int r = inf) { return operator()(0, min(r, n)); }
T operator[](int r) { return operator()(0, r); }
auto begin() { return rv.begin(); }
auto end() { return rv.end(); }
};
template <class T> ruimulC<T> ruimul(vector<T> &a) { return ruimulC<T>(a); }
template <class T> ruimulC<T> ruimul() {
vector<T> a;
return ruimulC<T>(a);
}
template <class T> T *rruim(vector<T> &a) {
ll len = a.size();
T *body = (T *)malloc((len + 1) * sizeof(T));
T *res = body + 1;
res[len - 1] = 1;
rer(i, len - 1) res[i - 1] = res[i] * a[i];
return res;
}
template <class T, class U, class W> T lowerBound(ruiC<T> &a, U v, W banpei) {
return lowerBound(a.rui, v, banpei);
}
template <class T, class U, class W> T upperBound(ruiC<T> &a, U v, W banpei) {
return upperBound(a.rui, v, banpei);
}
template <class T, class U, class W> T rlowerBound(ruiC<T> &a, U v, W banpei) {
return rlowerBound(a.rui, v, banpei);
}
template <class T, class U, class W> T rupperBound(ruiC<T> &a, U v, W banpei) {
return rupperBound(a.rui, v, banpei);
}
#endif
constexpr bool bget(ll m, ll keta) {
#ifdef _DEBUG
assert(keta <= 62); // オーバーフロー 1^62までしか扱えない
#endif
return (m >> keta) & 1;
}
// bget(n)次元
// NならN-1まで
vector<vi> bget2(vi &a, int keta_size) {
vvi(res, keta_size, sz(a));
rep(k, keta_size) {
rep(i, sz(a)) { res[k][i] = bget(a[i], k); }
}
return res;
}
vi bget1(vi &a, int keta) {
vi res(sz(a));
rep(i, sz(a)) { res[i] = bget(a[i], keta); }
return res;
}
#if __cplusplus >= 201703L
ll bget(ll m, ll keta, ll sinsuu) {
m /= pow<ll>(sinsuu, keta);
return m % sinsuu;
}
#else
ll bget(ll m, ll keta, ll sinsuu) {
m /= (ll)pow(sinsuu, keta);
return m % sinsuu;
}
#endif
constexpr ll bit(ll n) {
#ifdef _DEBUG
assert(n <= 62); // オーバーフロー 1^62までしか扱えない
#endif
return (1LL << (n));
}
#if __cplusplus >= 201703L
ll bit(ll n, ll sinsuu) { return pow<ll>(sinsuu, n); }
#else
ll bit(ll n, ll sinsuu) { return (ll)pow(sinsuu, n); }
#endif
ll mask(ll n) { return (1ll << n) - 1; }
// aをbitに置きなおす
//{0, 2} -> 101
ll bit(const vi &a) {
int m = 0;
for (auto &&v : a)
m |= bit(v);
return m;
}
//{1, 1, 0} -> 011
// bitsetに置き換える感覚 i が立っていたら i bit目を立てる
ll bit_bool(vi &a) {
int m = 0;
rep(i, sz(a)) if (a[i]) m |= bit(i);
return m;
}
#define bcou __builtin_popcountll
// 最下位ビット
ll lbit(ll n) {
assert(n);
return n & -n;
}
ll lbiti(ll n) {
assert(n);
return log2(n & -n);
}
// 最上位ビット
ll hbit(ll n) {
assert(n);
n |= (n >> 1);
n |= (n >> 2);
n |= (n >> 4);
n |= (n >> 8);
n |= (n >> 16);
n |= (n >> 32);
return n - (n >> 1);
}
ll hbiti(ll n) {
assert(n);
return log2(hbit(n));
}
// ll hbitk(ll n) { ll k = 0; rer(i, 5) { ll a = k + (1ll << i); ll
// b = 1ll << a; if (b <= n)k += 1ll << i; } return k;}
// 初期化は0を渡す
ll nextComb(ll &mask, ll n, ll r) {
if (!mask)
return mask = (1LL << r) - 1;
ll x = mask & -mask; /*最下位の1*/
ll y = mask + x; /*連続した下の1を繰り上がらせる*/
ll res = ((mask & ~y) / x >> 1) | y;
if (bget(res, n))
return mask = 0;
else
return mask = res;
}
// n桁以下でビットがr個立っているもののvectorを返す
vi bitCombList(ll n, ll r) {
vi res;
ll m = 0;
while (nextComb(m, n, r)) {
res.push_back(m);
}
return res;
}
/*over*/ #define forbit1_2(i, mas) \
for (int forbitj = !mas ? 0 : lbit(mas), forbitm = mas, \
i = !mas ? 0 : log2(forbitj); \
forbitm; forbitm = forbitm ^ forbitj, \
forbitj = !forbitm ? 1 : lbit(forbitm), i = log2(forbitj))
/*over*/ #define forbit1_3(i, N, mas) \
for (int forbitj = !mas ? 0 : lbit(mas), forbitm = mas, \
i = !mas ? 0 : log2(forbitj); \
forbitm && i < N; forbitm = forbitm ^ forbitj, \
forbitj = !forbitm ? 1 : lbit(forbitm), i = log2(forbitj))
// masの立ってるindexを見る
// i, [N], mas
#define forbit1(...) over3(__VA_ARGS__, forbit1_3, forbit1_2)(__VA_ARGS__)
// masが立っていないindexを見る
// i, N, mas
#define forbit0(i, N, mas) forbit1(i, (mask(N) & (~(mas))))
// forsubをスニペットして使う
// Mの部分集合(0,M含む)を見る 3^sz(S)個ある
#define forsub_all(m, M) for (int m = M; m != -1; m = m == 0 ? -1 : (m - 1) & M)
// BASE進数
template <size_t BASE> class base_num {
int v;
public:
base_num(int v = 0) : v(v){};
int operator[](int i) { return bget(v, i, BASE); }
void operator++() { v++; }
void operator++(signed) { v++; }
operator int() { return v; }
};
#define base3(mas, lim, BASE) for (base_num<BASE> mas; mas < lim; mas++)
#define base2(mas, lim) base3(mas, lim, 2)
#define for_base(...) over3(__VA_ARGS__, base3, base2, base1)(__VA_ARGS__)
// aにある物をtrueとする
vb bool_(vi a, int n) {
vb ret(max(max(a) + 1, n));
rep(i, sz(a)) ret[a[i]] = true;
return ret;
}
char itoal(ll i) { return 'a' + i; }
char itoaL(ll i) { return 'A' + i; }
ll altoi(char c) {
if ('A' <= c && c <= 'Z')
return c - 'A';
return c - 'a';
}
ll ctoi(char c) { return c - '0'; }
char itoc(ll i) { return i + '0'; }
ll vtoi(vi &v) {
ll res = 0;
if (sz(v) > 18) {
debugline("vtoi");
deb(sz(v));
ole();
}
rep(i, sz(v)) {
res *= 10;
res += v[i];
}
return res;
}
vi itov(ll i) {
vi res;
while (i) {
res.push_back(i % 10);
i /= 10;
}
res = rev(res);
return res;
}
vi stov(string &a) {
ll n = sz(a);
vi ret(n);
rep(i, n) { ret[i] = a[i] - '0'; }
return ret;
}
// 基準を満たさないものは0になる
vi stov(string &a, char one) {
ll n = sz(a);
vi ret(n);
rep(i, n) ret[i] = a[i] == one;
return ret;
}
vector<vector<ll>> ctoi(vector<vector<char>> s, char c) {
ll n = sz(s), m = sz(s[0]);
vector<vector<ll>> res(n, vector<ll>(m));
rep(i, n) rep(j, m) res[i][j] = s[i][j] == c;
return res;
}
// #define use_compress
//[i] := vを返す
// aは0~n-1で置き換えられる
vi compress(vi &a) {
vi b;
ll len = a.size();
for (ll i = 0; i < len; ++i) {
b.push_back(a[i]);
}
sort(b);
unique(b);
for (ll i = 0; i < len; ++i) {
a[i] = lower_bound(ALL(b), a[i]) - b.begin();
}
ll blen = sz(b);
vi ret(blen);
rep(i, blen) { ret[i] = b[i]; }
return ret;
}
#ifdef use_compress
// ind[i] := i番目に小さい数
// map[v] := vは何番目に小さいか
vi compress(vi &a, umapi &map) {
vi b;
ll len = a.size();
for (ll i = 0; i < len; ++i) {
b.push_back(a[i]);
}
sort(b);
unique(b);
for (ll i = 0; i < len; ++i) {
ll v = a[i];
a[i] = lower_bound(ALL(b), a[i]) - b.begin();
map[v] = a[i];
}
ll blen = sz(b);
vi ret(blen);
rep(i, blen) { ret[i] = b[i]; }
return ret;
}
vi compress(vi &a, vi &r) {
vi b;
ll len = a.size();
fora(v, a) { b.push_back(v); }
fora(v, r) { b.push_back(v); }
sort(b);
unique(b);
for (ll i = 0; i < len; ++i)
a[i] = lower_bound(ALL(b), a[i]) - b.begin();
for (ll i = 0; i < sz(r); ++i)
r[i] = lower_bound(ALL(b), r[i]) - b.begin();
ll blen = sz(b);
vi ret(blen);
rep(i, blen) { ret[i] = b[i]; }
return ret;
}
vi compress(vi &a, vi &r, vi &s) {
vi b;
ll len = a.size();
fora(v, a) { b.push_back(v); }
fora(v, r) { b.push_back(v); }
fora(v, s) { b.push_back(v); }
sort(b);
unique(b);
for (ll i = 0; i < len; ++i)
a[i] = lower_bound(ALL(b), a[i]) - b.begin();
for (ll i = 0; i < sz(r); ++i)
r[i] = lower_bound(ALL(b), r[i]) - b.begin();
for (ll i = 0; i < sz(s); ++i)
r[i] = lower_bound(ALL(b), s[i]) - b.begin();
ll blen = sz(b);
vi ret(blen);
rep(i, blen) { ret[i] = b[i]; }
return ret;
}
vi compress(vector<vi> &a) {
vi b;
fora(vv, a) {
fora(v, vv) { b.push_back(v); }
}
sort(b);
unique(b);
fora(vv, a) {
fora(v, vv) { v = lower_bound(ALL(b), v) - b.begin(); }
}
ll blen = sz(b);
vi ret(blen);
rep(i, blen) { ret[i] = b[i]; }
return ret;
}
vi compress(vector<vector<vi>> &a) {
vi b;
fora(vvv, a) {
fora(vv, vvv) {
fora(v, vv) { b.push_back(v); }
}
}
sort(b);
unique(b);
fora(vvv, a) {
fora(vv, vvv) {
fora(v, vv) { v = lower_bound(ALL(b), v) - b.begin(); }
}
}
ll blen = sz(b);
vi ret(blen);
rep(i, blen) { ret[i] = b[i]; }
return ret;
}
void compress(ll a[], ll len) {
vi b;
for (ll i = 0; i < len; ++i) {
b.push_back(a[i]);
}
sort(b);
unique(b);
for (ll i = 0; i < len; ++i) {
a[i] = lower_bound(ALL(b), a[i]) - b.begin();
}
}
#endif
// 要素が見つからなかったときに困る
#define binarySearch(a, v) (binary_search(ALL(a), v))
#define lowerIndex(a, v) (lower_bound(ALL(a), v) - a.begin())
#define upperIndex(a, v) (upper_bound(ALL(a), v) - a.begin())
#define rlowerIndex(a, v) (upper_bound(ALL(a), v) - a.begin() - 1)
#define rupperIndex(a, v) (lower_bound(ALL(a), v) - a.begin() - 1)
template <class T, class U, class W> T lowerBound(vector<T> &a, U v, W banpei) {
auto it = lower_bound(a.begin(), a.end(), v);
if (it == a.end())
return banpei;
else
return *it;
}
template <class T, class U, class W> T upperBound(vector<T> &a, U v, W banpei) {
auto it = upper_bound(a.begin(), a.end(), v);
if (it == a.end())
return banpei;
else
return *it;
}
template <class T, class U, class W>
T rlowerBound(vector<T> &a, U v, W banpei) {
auto it = upper_bound(a.begin(), a.end(), v);
if (it == a.begin())
return banpei;
else {
return *(--it);
}
}
template <class T, class U, class W>
T rupperBound(vector<T> &a, U v, W banpei) {
auto it = lower_bound(a.begin(), a.end(), v);
if (it == a.begin())
return banpei;
else {
return *(--it);
}
}
// todo 消せないか
template <class T, class U, class W> T lowerBound(set<T> &a, U v, W banpei) {
auto it = a.lower_bound(v);
if (it == a.end())
return banpei;
else
return *it;
}
template <class T, class U, class W> T upperBound(set<T> &a, U v, W banpei) {
auto it = a.upper_bound(v);
if (it == a.end())
return banpei;
else
return *it;
}
template <class T, class U, class W> T rlowerBound(set<T> &a, U v, W banpei) {
auto it = a.upper_bound(v);
if (it == a.begin())
return banpei;
else {
return *(--it);
}
}
template <class T, class U, class W> T rupperBound(set<T> &a, U v, W banpei) {
auto it = a.lower_bound(v);
if (it == a.begin())
return banpei;
else {
return *(--it);
}
}
template <class T, class U, class W> T lowerBound(mset<T> &a, U v, W banpei) {
auto it = a.lower_bound(v);
if (it == a.end())
return banpei;
else
return *it;
}
template <class T, class U, class W> T upperBound(mset<T> &a, U v, W banpei) {
auto it = a.upper_bound(v);
if (it == a.end())
return banpei;
else
return *it;
}
template <class T, class U, class W> T rlowerBound(mset<T> &a, U v, W banpei) {
auto it = a.upper_bound(v);
if (it == a.begin())
return banpei;
else {
return *(--it);
}
}
template <class T, class U, class W> T rupperBound(mset<T> &a, U v, W banpei) {
auto it = a.lower_bound(v);
if (it == a.begin())
return banpei;
else {
return *(--it);
}
}
#define next2(a) next(next(a))
#define prev2(a) prev(prev(a))
// 狭義の単調増加列 長さを返す
template <class T> int lis(vector<T> &a) {
int n = sz(a);
vi tail(n + 1, MAX<T>());
rep(i, n) {
int id = lowerIndex(tail, a[i]); /**/
tail[id] = a[i];
}
return lowerIndex(tail, MAX<T>());
}
template <class T> int lis_eq(vector<T> &a) {
int n = sz(a);
vi tail(n + 1, MAX<T>());
rep(i, n) {
int id = upperIndex(tail, a[i]); /**/
tail[id] = a[i];
}
return lowerIndex(tail, MAX<T>());
}
// iteratorを返す
// valueが1以上の物を返す 0は見つけ次第削除
// vを減らす場合 (*it).se--でいい
template <class T, class U, class V> auto lower_map(map<T, U> &m, V k) {
auto ret = m.lower_bound(k);
while (ret != m.end() && (*ret).second == 0) {
ret = m.erase(ret);
}
return ret;
}
template <class T, class U, class V> auto upper_map(map<T, U> &m, V k) {
auto ret = m.upper_bound(k);
while (ret != m.end() && (*ret).second == 0) {
ret = m.erase(ret);
}
return ret;
}
// 存在しなければエラー
template <class T, class U, class V> auto rlower_map(map<T, U> &m, V k) {
auto ret = upper_map(m, k);
assert(ret != m.begin());
ret--;
while (1) {
if ((*ret).second != 0)
break;
assert(ret != m.begin());
auto next = ret;
--next;
m.erase(ret);
ret = next;
}
return ret;
}
template <class T, class U, class V> auto rupper_map(map<T, U> &m, V k) {
auto ret = lower_map(m, k);
assert(ret != m.begin());
ret--;
while (1) {
if ((*ret).second != 0)
break;
assert(ret != m.begin());
auto next = ret;
--next;
m.erase(ret);
ret = next;
}
return ret;
}
template <class... T> void fin(T... s) {
out(s...);
exit(0);
}
// 便利 数学 math
// sub ⊂ top
bool subsup(int sub, int top) { return (sub & top) == sub; }
//-180 ~ 180 degree
double atand(double h, double w) { return atan2(h, w) / PI * 180; }
double degree(double theta) { return theta / PI * 180; }
//% -mの場合、最小の正の数を返す
ll mod(ll a, ll m) {
if (m < 0)
m *= -1;
return (a % m + m) % m;
}
// ll pow(ll a) { return a * a; };
template <class T> T fact(int v) {
static vector<T> fact(2, 1);
if (sz(fact) <= v) {
rep(i, sz(fact), v + 1) { fact.emplace_back(fact.back() * i); }
}
return fact[v];
}
ll comi(ll n, ll r) {
assert(n < 100);
static vvi(pas, 100, 100);
if (pas[0][0])
return pas[n][r];
pas[0][0] = 1;
rep(i, 1, 100) {
pas[i][0] = 1;
rep(j, 1, i + 1) pas[i][j] = pas[i - 1][j - 1] + pas[i - 1][j];
}
return pas[n][r];
}
// 二項係数の偶奇を返す
int com_mod2(int n, int r) { return n == (r | (n - r)); }
double comd2(ll n, ll r) {
static vvd(comb, 2020, 2020);
if (comb[0][0] == 0) {
comb[0][0] = 1;
rep(i, 2000) {
comb[i + 1][0] = 1;
rep(j, 1, i + 2) { comb[i + 1][j] = comb[i][j] + comb[i][j - 1]; }
}
}
return comb[n][r];
}
double comd(int n, int r) {
if (r < 0 || r > n)
return 0;
if (n < 2020)
return comd2(n, r);
static vd fact(2, 1);
if (sz(fact) <= n) {
rep(i, sz(fact), n + 1) { fact.push_back(fact.back() * i); }
}
return fact[n] / fact[n - r] / fact[r];
}
#define gcd my_gcd
template <class T, class U, class V = typename max_type<T, U>::type>
V gcd(T a_, U b_) {
V a = a_;
V b = b_;
while (b)
a %= b, swap(a, b);
return abs(a);
}
template <class T> T gcd(const vector<T> &b) {
T res = b[0];
rep(i, 1, sz(b)) res = gcd(b[i], res);
return res;
}
#define lcm my_lcm
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll lcm(vi a) {
ll res = a[0];
rep(i, 1, sz(a)) res = lcm(a[i], res);
return res;
}
ll ceil(ll a, ll b) {
if (b == 0) {
debugline("ceil");
deb(a, b);
ole();
return -1;
} else if (a < 0) {
return 0;
} else {
return (a + b - 1) / b;
}
}
#define hypot my_hypot
double hypot(double dx, double dy) { return std::sqrt(dx * dx + dy * dy); }
ll sig0(int t) { return t <= 0 ? 0 : ((1 + t) * t) >> 1; }
bint sig0(bint t) { return t <= 0 ? 0 : ((1 + t) * t) >> 1; }
// ll sig(ll s, ll t) { return ((s + t) * (t - s + 1)) >> 1; }
ll sig0(ll s, ll t) {
if (s > t)
swap(s, t);
return sig0(t - s) + s * (t - s + 1);
}
#define tousa_i tosa_i
#define lower_tousa_i lower_tosa_i
#define upper_tousa upper_tosa
#define upper_tousa_i upper_tosa_i
ll tosa_i(ll st, ll ad, ll v) {
assert(((v - st) % ad) == 0);
return (v - st) / ad;
}
ll tosa_s(ll st, ll ad, ll len) { return st * len + sig0(len - 1) * ad; }
// ax + r (x は非負整数) で表せる整数のうち、v 以上となる最小の整数
ll lower_tosa(ll st, ll ad, ll v) {
if (st >= v)
return st;
return (v - st + ad - 1) / ad * ad + st;
}
// 第何項か
ll lower_tosa_i(ll st, ll ad, ll v) {
if (st >= v)
return 0;
return (v - st + ad - 1) / ad;
}
ll upper_tosa(ll st, ll ad, ll v) { return lower_tosa(st, ad, v + 1); }
ll upper_tosa_i(ll st, ll ad, ll v) { return lower_tosa_i(st, ad, v + 1); }
// b * res <= aを満たす [l, r)を返す div
P drange_ika(int a, int b) {
P null_p = mp(linf, linf);
if (b == 0) {
if (a >= 0) {
return mp(-linf, linf + 1) /*全て*/;
} else {
return null_p /*無い*/;
}
} else {
if (a >= 0) {
if (b > 0) {
return mp(-linf, a / b + 1);
} else {
return mp(-(a / -b), linf + 1);
}
} else {
if (b > 0) {
return mp(-linf, -ceil(-a, b) + 1);
} else {
return mp(ceil(-a, -b), linf + 1);
}
}
}
}
// v * v >= aとなる最小のvを返す
ll sqrt(ll a) {
if (a < 0) {
debugline("sqrt");
deb(a);
ole();
}
ll res = (ll)std::sqrt(a);
while (res * res < a)
++res;
return res;
}
double log(double e, double x) { return log(x) / log(e); }
/*@formatter:off*/
// 機能拡張
#define dtie(a, b) \
int a, b; \
tie(a, b)
template <class T, class U> string to_string(T a, U b) {
string res = "";
res += a;
res += b;
return res;
}
template <class T, class U, class V> string to_string(T a, U b, V c) {
string res = "";
res += a;
res += b;
res += c;
return res;
}
template <class T, class U, class V, class W>
string to_string(T a, U b, V c, W d) {
string res = "";
res += a;
res += b;
res += c;
res += d;
return res;
}
template <class T, class U, class V, class W, class X>
string to_string(T a, U b, V c, W d, X e) {
string res = "";
res += a;
res += b;
res += c;
res += d;
res += e;
return res;
}
// todo stringもセットで
// find_ifはsubrを使う
string sub(string &A, int l = -1, int r = -1) {
set_lr12(l, r, sz(A));
assert(0 <= l && l <= r && r <= sz(A));
return A.substr(l, r - l);
}
template <class T> vector<T> sub(const vector<T> &A, int l, int r) {
assert(0 <= l && l <= r && r <= sz(A));
vector<T> ret(r - l);
std::copy(A.begin() + l, A.begin() + r, ret.begin());
return ret;
}
template <class T> vector<T> sub(const vector<T> &A, int r) {
return sub(A, 0, r);
}
#if __cplusplus >= 201703L
template <class I, require_t(has_value_type<I>::value)>
I subn(const I &A, int l, int len) {
return sub(A, l, l + len);
}
#endif
template <class T, class F>
// sub2で呼ぶ
vector<T> sub(const vector<vector<T>> &A, int h, int w, int ah, int aw, F f) {
vector<T> res;
while (0 <= h && h < sz(A) && 0 <= w && w < sz(A[h]) && f(A[h][w])) {
res.emplace_back(A[h][w]);
h += ah;
w += aw;
}
return res;
}
template <class T>
vector<T> sub(const vector<vector<T>> &A, int h, int w, int ah, int aw) {
return sub(A, h, w, ah, aw, [&](T v) { return true; });
}
// range_nowを返す(find_ifでしか使われない)
#if __cplusplus >= 201703L
template <class T> auto subr(const vector<T> &A, int l) {
return range_now(vector<T>(A.begin() + l, A.end()), l);
}
#endif
#define sub25(A, h, w, ah, aw) sub(A, h, w, ah, aw)
#define sub26(A, h, w, ah, aw, siki_r) \
sub(A, h, w, ah, aw, [&](auto v) { return v siki_r; })
#define sub27(A, h, w, ah, aw, v, siki) \
sub(A, h, w, ah, aw, [&](auto v) { return siki; })
#define sub2(...) over7(__VA_ARGS__, sub27, sub26, sub25)(__VA_ARGS__)
constexpr int bsetlen = 50001;
// constexpr int bsetlen = 5050;
#define bset bitset<bsetlen>
bool operator<(bitset<bsetlen> &a, bitset<bsetlen> &b) {
rer(i, bsetlen - 1) {
if (a[i] < b[i])
return true;
if (a[i] > b[i])
return false;
}
return false;
}
bool operator>(bitset<bsetlen> &a, bitset<bsetlen> &b) {
rer(i, bsetlen - 1) {
if (a[i] > b[i])
return true;
if (a[i] < b[i])
return false;
}
return false;
}
bool operator<=(bitset<bsetlen> &a, bitset<bsetlen> &b) {
rer(i, bsetlen - 1) {
if (a[i] < b[i])
return true;
if (a[i] > b[i])
return false;
}
return true;
}
bool operator>=(bitset<bsetlen> &a, bitset<bsetlen> &b) {
rer(i, bsetlen - 1) {
if (a[i] > b[i])
return true;
if (a[i] < b[i])
return false;
}
return true;
}
string operator~(string &a) {
string res = a;
for (auto &&c : res) {
if (c == '0')
c = '1';
else if (c == '1')
c = '0';
else {
cerr << "cant ~" << a << "must bit" << endl;
exit(0);
}
}
return res;
}
ostream &operator<<(ostream &os, bset &a) {
bitset<10> b;
vi list;
rep(i, bsetlen) {
if (a[i])
list.push_back(i), b[i] = 1;
}
os << b << ", " << list;
return os;
}
int hbiti(bset &a) {
rer(i, bsetlen) {
if (a[i])
return i;
}
return -1;
}
#define hk(a, b, c) (a <= b && b < c)
// O(N/64)
bset nap(bset &a, int v) {
bset r = a | a << v;
return r;
}
bset nap(bset &a, bset &v) {
bset r = a;
rep(i, bsetlen) {
if (v[i])
r |= a << i;
}
return r;
}
template <class T> int count(set<T> &S, T l, T r) {
assert(l < r);
auto it = S.lower_bound(l);
return it != S.end() && (*it) < r;
}
// template<class T> void seth(vector<vector<T>> &S, int w, vector<T> &v)
// {assert(sz(S) == sz(v));assert(w < sz(S[0]));rep(h, sz(S)) { S[h][w] = v[h];
// }}
template <class T> vector<T> geth(vector<vector<T>> &S, int w) {
assert(w < sz(S[0]));
vector<T> ret(sz(S));
rep(h, sz(S)) { ret[h] = S[h][w]; }
return ret;
}
// vector<bool>[i]は参照を返さないため、こうしないとvb[i] |=
// trueがコンパイルエラー
vb::reference operator|=(vb::reference a, bool b) { return a = a | b; }
vb::reference operator&=(vb::reference a, bool b) { return a = a & b; }
template <class T, class U>
void operator+=(pair<T, U> &a, const pair<T, U> &b) {
a.fi += b.fi;
a.se += b.se;
}
template <class T, class U>
pair<T, U> operator+(const pair<T, U> &a, const pair<T, U> &b) {
return pair<T, U>(a.fi + b.fi, a.se + b.se);
}
template <class T, class U> pair<T, U> operator*(const pair<T, U> &a, int b) {
return pair<T, U>(a.fi * b, a.se * b);
}
template <class T, class U>
pair<T, U> operator-(const pair<T, U> &a, const pair<T, U> &b) {
return pair<T, U>(a.fi - b.fi, a.se - b.se);
}
template <class T, class U> pair<T, U> operator-(const pair<T, U> &a) {
return pair<T, U>(-a.first, -a.second);
}
template <typename CharT, typename Traits, typename Alloc>
basic_string<CharT, Traits, Alloc>
operator+(const basic_string<CharT, Traits, Alloc> &lhs, const int rhs) {
#ifdef _DEBUG
static bool was = false;
if (!was)
message += "str += 65 is 'A' not \"65\" ";
was = true;
#endif
return lhs + (char)rhs;
}
template <typename CharT, typename Traits, typename Alloc>
basic_string<CharT, Traits, Alloc>
operator+(const int lhs, const basic_string<CharT, Traits, Alloc> &rhs) {
#ifdef _DEBUG
static bool was = false;
if (!was)
message += "str += 65 is 'A' not \"65\" ";
was = true;
#endif
return (char)lhs + rhs;
}
template <typename CharT, typename Traits, typename Alloc>
void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const int rv) {
lhs = lhs + rv;
}
template <typename CharT, typename Traits, typename Alloc>
basic_string<CharT, Traits, Alloc>
operator+(const basic_string<CharT, Traits, Alloc> &lhs, const signed rv) {
const int rv2 = rv;
return lhs + rv2;
}
template <typename CharT, typename Traits, typename Alloc>
void operator+=(basic_string<CharT, Traits, Alloc> &lhs, const signed rv) {
const int v = rv;
lhs += v;
}
template <typename CharT, typename Traits, typename Alloc>
void operator*=(basic_string<CharT, Traits, Alloc> &s, int num) {
auto bek = s;
s = "";
for (; num; num >>= 1) {
if (num & 1) {
s += bek;
}
bek += bek;
}
}
template <class T, class U> void operator+=(queue<T> &a, const U &v) {
a.push(v);
}
template <class T, class U> void operator+=(deque<T> &a, const U &v) {
a.push_back(v);
}
template <class T>
priority_queue<T, vector<T>, greater<T>> &
operator+=(priority_queue<T, vector<T>, greater<T>> &a, vector<T> &v) {
fora(d, v) { a.push(d); }
return a;
}
template <class T, class U>
priority_queue<T, vector<T>, greater<T>> &
operator+=(priority_queue<T, vector<T>, greater<T>> &a, const U &v) {
a.push(v);
return a;
}
template <class T, class U>
priority_queue<T> &operator+=(priority_queue<T> &a, const U &v) {
a.push(v);
return a;
}
template <class T> set<T> &operator+=(set<T> &a, const vector<T> &v) {
fora(d, v) { a.insert(d); }
return a;
}
template <class T, class U> auto operator+=(set<T> &a, const U &v) {
return a.insert(v);
}
template <class T, class U> auto operator-=(set<T> &a, const U &v) {
return a.erase(v);
}
template <class T, class U> auto operator+=(mset<T> &a, const U &v) {
return a.insert(v);
}
template <class T, class U>
set<T, greater<T>> &operator+=(set<T, greater<T>> &a, const U &v) {
a.insert(v);
return a;
}
template <class T, class U> vector<T> &operator+=(vector<T> &a, const U &v) {
a.push_back(v);
return a;
}
template <class T, class U> vector<T> operator+(U v, const vector<T> &a) {
vector<T> ret = a;
ret.insert(ret.begin(), v);
return ret;
}
template <class T> vector<T> operator+(const vector<T> &a, const vector<T> &b) {
vector<T> ret;
ret = a;
fora(v, b) { ret += v; }
return ret;
}
template <class T> vector<T> &operator+=(vector<T> &a, const vector<T> &b) {
rep(i, sz(b)) { /*こうしないとa+=aで両辺が増え続けてバグる*/
a.push_back(b[i]);
}
return a;
}
template <class T, class U> map<T, U> &operator+=(map<T, U> &a, map<T, U> &b) {
for (auto &&bv : b) {
a[bv.first] += bv.second;
}
return a;
}
template <class T, class U>
vector<T> operator+(const vector<T> &a, const U &v) {
vector<T> ret = a;
ret += v;
return ret;
}
template <class T, class U> auto operator+=(uset<T> &a, U v) {
return a.insert(v);
}
template <class T> vector<T> operator%(vector<T> &a, int v) {
vi ret(sz(a));
rep(i, sz(a)) { ret[i] = a[i] % v; }
return ret;
}
template <class T> vector<T> operator%=(vector<T> &a, int v) {
rep(i, sz(a)) { a[i] %= v; }
return a;
}
vi operator&(vi &a, vi &b) {
assert(sz(a) == sz(b));
vi ret(sz(a));
rep(i, sz(a)) { ret[i] = min(a[i], b[i]); }
return ret;
}
template <class T> void operator+=(mset<T> &a, vector<T> &v) {
for (auto &&u : v)
a.insert(u);
}
template <class T> void operator+=(set<T> &a, vector<T> &v) {
for (auto &&u : v)
a.insert(u);
}
template <class T> void operator+=(vector<T> &a, set<T> &v) {
for (auto &&u : v)
a.emplace_back(u);
}
template <class T> void operator+=(vector<T> &a, mset<T> &v) {
for (auto &&u : v)
a.emplace_back(u);
}
template <class T> vector<T> &operator-=(vector<T> &a, vector<T> &b) {
if (sz(a) != sz(b)) {
debugline("vector<T> operator-=");
deb(a);
deb(b);
exit(0);
}
rep(i, sz(a)) a[i] -= b[i];
return a;
}
template <class T> vector<T> operator-(vector<T> &a, vector<T> &b) {
if (sz(a) != sz(b)) {
debugline("vector<T> operator-");
deb(a);
deb(b);
ole();
}
vector<T> res(sz(a));
rep(i, sz(a)) res[i] = a[i] - b[i];
return res;
}
// template<class T, class U> void operator*=(vector<T> &a, U b) { vector<T>
// ta = a; rep(b-1){ a+=ta; }}
template <typename T> void erase(T &v, unsigned ll i) {
v.erase(v.begin() + i);
}
template <typename T> void erase(T &v, unsigned ll s, unsigned ll e) {
v.erase(v.begin() + s, v.begin() + e);
}
template <class T, class... I> auto erased(T &v, I... arg) {
auto ret = v;
erase(ret, arg...);
return ret;
}
template <typename T> void pop_front(vector<T> &v) { erase(v, 0); }
template <typename T> void entry(vector<T> &v, unsigned ll s, unsigned ll e) {
erase(v, e, sz(v));
erase(v, 0, s);
}
template <class T, class U> void erase(map<T, U> &m, ll okl, ll ngr) {
m.erase(m.lower_bound(okl), m.lower_bound(ngr));
}
template <class T> void erase(set<T> &m, ll okl, ll ngr) {
m.erase(m.lower_bound(okl), m.lower_bound(ngr));
}
template <typename T> void erasen(vector<T> &v, unsigned ll s, unsigned ll n) {
v.erase(v.begin() + s, v.begin() + s + n);
}
template <typename T, typename U>
void insert(vector<T> &v, unsigned ll i, U t) {
v.insert(v.begin() + i, t);
}
template <typename T, typename U> void push_front(vector<T> &v, U t) {
v.insert(v.begin(), t);
}
template <typename T, typename U>
void insert(vector<T> &v, unsigned ll i, vector<T> list) {
for (auto &&va : list)
v.insert(v.begin() + i++, va);
}
vector<string> split(const string &a, const char deli) {
string b = a + deli;
ll l = 0, r = 0, n = b.size();
vector<string> res;
rep(i, n) {
if (b[i] == deli) {
r = i;
if (l < r)
res.push_back(b.substr(l, r - l));
l = i + 1;
}
}
return res;
}
vector<string> split(const string &a, const string deli) {
vector<string> res;
ll kn = sz(deli);
std::string::size_type Pos(a.find(deli));
ll l = 0;
while (Pos != std::string::npos) {
if (Pos - l)
res.push_back(a.substr(l, Pos - l));
l = Pos + kn;
Pos = a.find(deli, Pos + kn);
}
if (sz(a) - l)
res.push_back(a.substr(l, sz(a) - l));
return res;
}
ll stoi(string &s) { return stol(s); }
#define assert_yn(yn_v, v) \
; \
assert(yn_v == 0 || yn_v == v); \
yn_v = v;
// 不完全な対策、現状はautohotkeyで対応
int yn_v = 0;
void yn(bool a) {
assert_yn(yn_v, 1);
if (a)
cout << "yes" << endl;
else
cout << "no" << endl;
}
void fyn(bool a) {
assert_yn(yn_v, 1);
yn(a);
exit(0);
}
void Yn(bool a) {
assert_yn(yn_v, 2);
if (a)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
void fYn(bool a) {
assert_yn(yn_v, 2);
Yn(a);
exit(0);
}
void YN(bool a) {
assert_yn(yn_v, 3);
if (a)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
void fYN(bool a) {
assert_yn(yn_v, 3);
YN(a);
exit(0);
}
int ab_v = 0;
void fAb(bool a) {
assert_yn(ab_v, 1);
if (a)
cout << "Alice" << endl;
else
cout << "Bob";
}
void fAB(bool a) {
assert_yn(yn_v, 2);
if (a)
cout << "ALICE" << endl;
else
cout << "BOB";
}
int pos_v = 0;
void Possible(bool a) {
assert_yn(pos_v, 1);
if (a)
cout << "Possible" << endl;
else
cout << "Impossible" << endl;
exit(0);
}
void POSSIBLE(bool a) {
assert_yn(pos_v, 2);
if (a)
cout << "POSSIBLE" << endl;
else
cout << "IMPOSSIBLE" << endl;
exit(0);
}
void fPossible(bool a) {
assert_yn(pos_v, 1) Possible(a);
exit(0);
}
void fPOSSIBLE(bool a) {
assert_yn(pos_v, 2) POSSIBLE(a);
exit(0);
}
template <typename T> class fixed_point : T {
public:
explicit constexpr fixed_point(T &&t) noexcept : T(std::forward<T>(t)) {}
template <typename... Args>
constexpr decltype(auto) operator()(Args &&...args) const {
return T::operator()(*this, std::forward<Args>(args)...);
}
};
template <typename T>
static inline constexpr decltype(auto) fix(T &&t) noexcept {
return fixed_point<T>{std::forward<T>(t)};
}
// 未分類
// 0,2,1 1番目と2番目の次元を入れ替える
template <class T>
auto irekae(vector<vector<vector<T>>> &A, int x, int y, int z) {
#define irekae_resize_loop(a, b, c) \
resize(res, a, b, c); \
rep(i, a) rep(j, b) rep(k, c)
vector<vector<vector<T>>> res;
if (x == 0 && y == 1 && z == 2) {
res = A;
} else if (x == 0 && y == 2 && z == 1) {
irekae_resize_loop(sz(A), sz(A[0][0]), sz(A[0])) {
res[i][j][k] = A[i][k][j];
}
} else if (x == 1 && y == 0 && z == 2) {
irekae_resize_loop(sz(A[0]), sz(A), sz(A[0][0])) {
res[i][j][k] = A[j][i][k];
}
} else if (x == 1 && y == 2 && z == 0) {
irekae_resize_loop(sz(A[0]), sz(A[0][0]), sz(A)) {
res[i][j][k] = A[k][i][j];
}
} else if (x == 2 && y == 0 && z == 1) {
irekae_resize_loop(sz(A[0][0]), sz(A), sz(A[0])) {
res[i][j][k] = A[j][k][i];
}
} else if (x == 2 && y == 1 && z == 0) {
irekae_resize_loop(sz(A[0][0]), sz(A[0]), sz(A)) {
res[i][j][k] = A[k][j][i];
}
}
return res;
#undef irekae_resize_loop
}
template <class T> auto irekae(vector<vector<T>> &A, int i = 1, int j = 0) {
vvt(res, sz(A[0]), sz(A));
rep(i, sz(A)) {
rep(j, sz(A[0])) { res[j][i] = A[i][j]; }
}
return res;
}
// tou分割する
template <typename Iterable, require_t(has_value_type<Iterable>::value)>
vector<Iterable> cut(const Iterable &a, int tou = 2) {
int N = sz(a);
vector<Iterable> res(tou);
int hab = N / tou;
vi lens(tou, hab);
rep(i, N % tou) { lens[tou - 1 - i]++; }
int l = 0;
rep(i, tou) {
int len = lens[i];
int r = l + len;
res[i].resize(len);
std::copy(a.begin() + l, a.begin() + r, res[i].begin());
l = r;
}
return res;
}
// 長さn毎に分割する
template <typename Iterable>
vector<Iterable> cut_n(const Iterable &a, int len) {
int N = sz(a);
vector<Iterable> res(ceil(N, len));
vi lens(N / len, len);
if (N % len)
lens.push_back(N % len);
int l = 0;
rep(i, sz(lens)) {
int len = lens[i];
int r = l + len;
res[i].resize(len);
std::copy(a.begin() + l, a.begin() + r, res[i].begin());
l = r;
}
return res;
}
vi cut(int N, int K) {
vi res(K);
int lim = K;
rep(i, lim) {
res[i] = N / K;
N -= res[i];
K--;
}
return res;
}
// 縦を返す
vi &geth(vvi() & a, int w) {
static vi ret;
ret.resize(sz(a));
rep(i, sz(a)) { ret[i] = a[i][w]; }
return ret;
}
//@起動時
struct initon {
initon() {
cin.tie(0);
ios::sync_with_stdio(false);
cout.setf(ios::fixed);
cout.precision(16);
srand((unsigned)clock() + (unsigned)time(NULL));
};
} initonv;
// #define pre prev
// #define nex next
// gra mll pr
// 上下左右
const string udlr = "udlr";
string UDLR = "UDLR"; // x4と連動 UDLR.find('U') := x4[0]
vc atoz = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
vc AtoZ = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
// 右、上が正
constexpr ll h4[] = {1, -1, 0, 0};
constexpr ll w4[] = {0, 0, -1, 1};
constexpr ll h8[] = {0, 1, 0, -1, -1, 1, 1, -1};
constexpr ll w8[] = {1, 0, -1, 0, 1, -1, 1, -1};
int mei_inc(int h, int w, int H, int W, int i) {
while (++i < 4) {
if (inside(h + h4[i], w + w4[i], H, W))
return i;
}
return i;
}
#define mei(nh, nw, h, w) \
for (int i = mei_inc(h, w, H, W, -1), nh = i < 4 ? h + h4[i] : 0, \
nw = i < 4 ? w + w4[i] : 0; \
i < 4; i = mei_inc(h, w, H, W, i), nh = h + h4[i], nw = w + w4[i])
int mei_inc8(int h, int w, int H, int W, int i) {
while (++i < 8) {
if (inside(h + h8[i], w + w8[i], H, W))
return i;
}
return i;
}
#define mei8(nh, nw, h, w) \
for (int i = mei_inc8(h, w, H, W, -1), nh = i < 8 ? h + h8[i] : 0, \
nw = i < 8 ? w + w8[i] : 0; \
i < 8; i = mei_inc8(h, w, H, W, i), nh = h + h8[i], nw = w + w8[i])
int mei_incv(int h, int w, int H, int W, int i, vp &p) {
while (++i < sz(p)) {
if (inside(h + p[i].fi, w + p[i].se, H, W))
return i;
}
return i;
}
#define meiv(nh, nw, h, w, p) \
for (int i = mei_incv(h, w, H, W, -1, p), nh = i < sz(p) ? h + p[i].fi : 0, \
nw = i < sz(p) ? w + p[i].se : 0; \
i < sz(p); \
i = mei_incv(h, w, H, W, i, p), nh = h + p[i].fi, nw = w + p[i].se)
// H*Wのグリッドを斜めに分割する
// 右上
vector<vp> naname_list_ne(int H, int W) {
vector<vp> res(H + W - 1);
rep(sh, H) {
int sw = 0;
res[sh] += mp(sh, sw);
int nh = sh;
int nw = sw;
while (1) {
nh--;
nw++;
if (0 <= nh && nw < W) {
res[sh] += mp(nh, nw);
} else {
break;
}
}
}
rep(sw, 1, W) {
int sh = H - 1;
res[H + sw - 1] += mp(sh, sw);
int nh = sh;
int nw = sw;
while (1) {
nh--;
nw++;
if (0 <= nh && nw < W) {
res[H + sw - 1] += mp(nh, nw);
} else {
break;
}
}
}
return res;
}
// 右下
vector<vp> naname_list_se(int H, int W) {
vector<vp> res(H + W - 1);
rep(sh, H) {
int sw = 0;
res[sh] += mp(sh, sw);
int nh = sh;
int nw = sw;
while (1) {
nh++;
nw++;
if (0 <= nh && nh < H && nw < W) {
res[sh] += mp(nh, nw);
} else {
break;
}
}
}
rep(sw, 1, W) {
int sh = 0;
res[H + sw - 1] += mp(sh, sw);
int nh = sh;
int nw = sw;
while (1) {
nh++;
nw++;
if (0 <= nh && nh < H && nw < W) {
res[H + sw - 1] += mp(nh, nw);
} else {
break;
}
}
}
return res;
}
// グラフ内で #undef getid
// #define getidとしているため、ここを書き直したらgraphも書き直す
#define getid_2(h, w) ((h) * (W) + (w))
#define getid_1(p) ((p).first * W + (p).second)
#define getid(...) over2(__VA_ARGS__, getid_2, getid_1)(__VA_ARGS__)
#define getp(id) mp((id) / W, (id) % W)
#define set_shuffle() \
std::random_device seed_gen; \
std::mt19937 engine(seed_gen())
#define shuffle(a) std::shuffle((a).begin(), (a).end(), engine);
// 1980 開始からtime ms経っていたらtrue
vb bit_bool(int v, int len) {
assert(bit(len) > v);
vb ret(len);
rep(i, len) { ret[i] = bget(v, i); }
return ret;
}
vi tov(vb &a) {
vi ret;
rep(i, sz(a)) {
if (a[i])
ret.push_back(i);
}
return ret;
}
bool kaibun(const str &S) { return S == rev(S); }
template <class T> vector<T> repeat(const vector<T> &A, int kaisu) {
vector<T> ret;
while (kaisu--) {
ret += A;
}
return ret;
}
#define rge range
#define upd update
// S[{s, t, d}]
#define strs slice_str
struct slice_str {
string S;
slice_str() {}
slice_str(const string &S) : S(S) {}
slice_str(int len, char c) : S(len, c) {}
auto size() { return S.size(); }
char &operator[](int p) { return S[p]; }
string operator[](initializer_list<int> p) {
if (sz(p) == 1) {
return S.substr(0, *(p.begin()));
} else if (sz(p) == 2) {
int l = *(p.begin());
int r = *(next(p.begin()));
return S.substr(l, r - l);
} else {
auto it = p.begin();
int s = *(it++);
int t = *(it++);
int d = *(it);
if (d == -1) {
int s_ = sz(S) - s - 1;
int t_ = sz(S) - t - 1;
return rev(S).substr(s_, t_ - s_);
} else if (d < 0) {
t = max(-1ll, t);
string ret;
while (s > t) {
ret += S[s];
s += d;
}
return ret;
} else {
t = min(sz(S), t);
string ret;
while (s < t) {
ret += S[s];
s += d;
}
return ret;
}
}
}
operator string &() { return S; }
template <class T> void operator+=(const T &a) { S += a; }
bool operator==(const slice_str &rhs) { return S == rhs.S; }
auto pop_back() { return S.pop_back(); }
};
ostream &operator<<(ostream &os, const slice_str &a) {
os << a.S;
return os;
}
istream &operator>>(istream &iss, slice_str &a) {
iss >> a.S;
return iss;
}
template <class T> bool can(const T &v, int i) { return 0 <= i && i < sz(v); }
#if __cplusplus >= 201703L
// template<class T> auto sum(int a, T v...) {return (v + ... + 0);}
#endif
#define VEC vector
// string.substr()は長さを指定するため、間違えやすい
#define substr my_substr
template <class T> struct my_set : public set<T> {
template <class... Arg> my_set(Arg... arg) : set<T>(arg...) {}
T back() { return *(set<T>::rbegin()); }
T front() { return *(set<T>::begin()); }
};
#define set my_set
#endif /*UNTITLED15_TEMPLATE_H*/
#endif
// † ←template終了
/*@formatter:on*/
// vectorで取れる要素数
// bool=> 1e9 * 8.32
// int => 1e8 * 2.6
// ll => 1e8 * 1.3
// 3次元以上取るとメモリがヤバい
// static配列を使う
vvc(ba);
ll N, M, H, W;
vi A, B, C;
void solve() {
in(N);
na(A, N);
vi cand;
int want = ruix(sub(A, 2, N)).back();
int a = A[0];
int b = A[1];
rep(x, min(A[0], bit(20))) {
int xo = (a - x) ^ (b + x) ^ want;
if ((xo & mask(20)) == 0) {
deb(x);
deb2(x);
cand += x;
}
}
int res = linf;
fora(can, cand) {
rep(x, can, a, bit(20)) {
if (x >= res)
break;
int xo = (a - x) ^ (b + x) ^ want;
if ((xo) == 0) {
chmi(res, x);
}
}
}
if (res != linf)
fin(res);
fin(-1);
}
auto my(ll n, vi &a) { return 0; }
auto sister(ll n, vi &a) {
ll ret = 0;
return ret;
}
signed main() {
solve();
#define arg n, a
#ifdef _DEBUG
bool bad = 0;
for (ll i = 0, ok = 1; i < k5 && ok; ++i) {
ll n = rand(1, 8);
vi a = ranv(n, 1, 10);
auto myres = my(arg);
auto res = sister(arg);
ok = myres == res;
if (!ok) {
out(arg);
cerr << "AC : " << res << endl;
cerr << "MY : " << myres << endl;
bad = 1;
break;
}
}
if (!bad) {
// solveを書き直す
// solveを呼び出す
}
exit(0);
#endif
return 0;
};
| insert | 7,194 | 7,194 | 7,194 | 7,196 | TLE | |
p02626 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
template <class T> ostream &operator<<(ostream &os, vector<T> V) {
os << "[ ";
for (auto v : V)
os << v << " ";
return os << "]";
}
template <class T> ostream &operator<<(ostream &os, set<T> S) {
os << "{ ";
for (auto s : S)
os << s << " ";
return os << "}";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
return os << "(" << P.first << "," << P.second << ")";
}
template <class L, class R> ostream &operator<<(ostream &os, map<L, R> M) {
os << "{ ";
for (auto m : M)
os << "(" << m.F << ":" << m.S << ")";
return os << "}";
}
#define cerr cout
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << std::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(...) 1
#endif
#define ll long long
#define ld long double
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define I insert
#define pb push_back
#define F first
#define S second
#define endl "\n"
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define For(i, n) for (int i = 0; i < (int)n; ++i)
#define Rev(i, n) for (int i = (int)n - 1; i >= 0; --i)
#define Rep(i, n) for (int i = 1; i <= (int)n; ++i)
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
typedef vector<pii> vpii;
#define fio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
const int N = 500;
ll A[N];
ll B[N];
int main() {
fio;
cout << fixed << setprecision(25);
ll xr = 0;
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> A[i];
if (i > 2)
xr ^= A[i];
}
if ((xr ^ A[1] ^ A[2]) == 0) {
cout << 0 << endl;
return 0;
}
if (A[1] == 1) {
cout << -1 << endl;
return 0;
}
ll sum = A[1] + A[2];
// trace(sum , xr);
if (sum < xr || (((sum - xr) % 2) != 0)) {
cout << -1 << endl;
} else {
ll ed = (sum - xr) / 2;
ll fir = ed;
// trace(ed , xr);
// B[0] += (ed&1);
// for(int i = 1;i < 50 ; ++i){
// B[i] += B[i-1];
// if((1ll<<i)&ed)
// B[i] += (1ll<<i);
// }
// trace(ed);
// trace(B[49]);
if (ed > A[1]) {
cout << -1 << endl;
return 0;
}
for (int i = 49; i >= 0; --i) {
if ((ed & (1ll << i))) {
assert((xr & (1ll << i)) == 0);
// trace(i);
// fir += (1ll<<i);
} else if (xr & (1ll << i)) {
ll fin = fir + (1ll << i);
// trace(B[i]);
if (fin <= A[1]) {
// trace(i);
fir += (1ll << i);
}
}
}
// assert(fir <= A[1]);
// trace(fir);
if (fir <= A[1] && (fir != 0)) {
// assert(fir != A[1]);
cout << A[1] - fir << endl;
} else {
cout << -1 << endl;
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
template <class T> ostream &operator<<(ostream &os, vector<T> V) {
os << "[ ";
for (auto v : V)
os << v << " ";
return os << "]";
}
template <class T> ostream &operator<<(ostream &os, set<T> S) {
os << "{ ";
for (auto s : S)
os << s << " ";
return os << "}";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
return os << "(" << P.first << "," << P.second << ")";
}
template <class L, class R> ostream &operator<<(ostream &os, map<L, R> M) {
os << "{ ";
for (auto m : M)
os << "(" << m.F << ":" << m.S << ")";
return os << "}";
}
#define cerr cout
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << std::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(...) 1
#endif
#define ll long long
#define ld long double
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define I insert
#define pb push_back
#define F first
#define S second
#define endl "\n"
#define mp make_pair
#define all(v) (v).begin(), (v).end()
#define For(i, n) for (int i = 0; i < (int)n; ++i)
#define Rev(i, n) for (int i = (int)n - 1; i >= 0; --i)
#define Rep(i, n) for (int i = 1; i <= (int)n; ++i)
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
typedef vector<pii> vpii;
#define fio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
const int N = 500;
ll A[N];
ll B[N];
int main() {
fio;
cout << fixed << setprecision(25);
ll xr = 0;
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> A[i];
if (i > 2)
xr ^= A[i];
}
if ((xr ^ A[1] ^ A[2]) == 0) {
cout << 0 << endl;
return 0;
}
if (A[1] == 1) {
cout << -1 << endl;
return 0;
}
ll sum = A[1] + A[2];
// trace(sum , xr);
if (sum < xr || (((sum - xr) % 2) != 0)) {
cout << -1 << endl;
} else {
ll ed = (sum - xr) / 2;
ll fir = ed;
// trace(ed , xr);
// B[0] += (ed&1);
// for(int i = 1;i < 50 ; ++i){
// B[i] += B[i-1];
// if((1ll<<i)&ed)
// B[i] += (1ll<<i);
// }
// trace(ed);
// trace(B[49]);
if (ed > A[1]) {
cout << -1 << endl;
return 0;
}
for (int i = 49; i >= 0; --i) {
if ((ed & (1ll << i))) {
if ((xr & (1ll << i)) != 0) {
cout << -1 << endl;
return 0;
}
// trace(i);
// fir += (1ll<<i);
} else if (xr & (1ll << i)) {
ll fin = fir + (1ll << i);
// trace(B[i]);
if (fin <= A[1]) {
// trace(i);
fir += (1ll << i);
}
}
}
// assert(fir <= A[1]);
// trace(fir);
if (fir <= A[1] && (fir != 0)) {
// assert(fir != A[1]);
cout << A[1] - fir << endl;
} else {
cout << -1 << endl;
}
}
return 0;
}
| replace | 111 | 112 | 111 | 115 | 0 | |
p02626 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;
#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x, to) for (x = 0; x < (to); x++)
#define FORR(x, arr) for (auto &x : arr)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
//-------------------------------------------------------
int N;
ll A[303];
void solve() {
int i, j, k, l, r, x, y;
string s;
cin >> N;
FOR(i, N) cin >> A[i];
ll xo = 0;
for (i = 2; i < N; i++)
xo ^= A[i];
vector<int> cand;
FOR(i, 1 << 20) if (i < A[0]) {
ll v = (A[0] - i) ^ (A[1] + i) ^ xo;
if ((v & ((1 << 20) - 1)) == 0)
cand.push_back(i);
}
ll mi = 1LL << 50;
FORR(c, cand) {
for (ll a = c; a < A[0]; a += 1 << 20) {
ll v = (A[0] - a) ^ (A[1] + a) ^ xo;
if (v == 0)
mi = min(mi, a);
}
}
if (mi == 1LL << 50)
mi = -1;
cout << mi << endl;
}
int main(int argc, char **argv) {
string s;
int i;
if (argc == 1)
ios::sync_with_stdio(false), cin.tie(0);
FOR(i, argc - 1) s += argv[i + 1], s += '\n';
FOR(i, s.size()) ungetc(s[s.size() - 1 - i], stdin);
cout.tie(0);
solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;
#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x, to) for (x = 0; x < (to); x++)
#define FORR(x, arr) for (auto &x : arr)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
//-------------------------------------------------------
int N;
ll A[303];
void solve() {
int i, j, k, l, r, x, y;
string s;
cin >> N;
FOR(i, N) cin >> A[i];
ll xo = 0;
for (i = 2; i < N; i++)
xo ^= A[i];
vector<int> cand;
FOR(i, 1 << 20) if (i < A[0]) {
ll v = (A[0] - i) ^ (A[1] + i) ^ xo;
if ((v & ((1 << 20) - 1)) == 0)
cand.push_back(i);
}
ll mi = 1LL << 50;
FORR(c, cand) {
for (ll a = c; a < min(mi, A[0]); a += 1 << 20) {
ll v = (A[0] - a) ^ (A[1] + a) ^ xo;
if (v == 0)
mi = min(mi, a);
}
}
if (mi == 1LL << 50)
mi = -1;
cout << mi << endl;
}
int main(int argc, char **argv) {
string s;
int i;
if (argc == 1)
ios::sync_with_stdio(false), cin.tie(0);
FOR(i, argc - 1) s += argv[i + 1], s += '\n';
FOR(i, s.size()) ungetc(s[s.size() - 1 - i], stdin);
cout.tie(0);
solve();
return 0;
}
| replace | 36 | 37 | 36 | 37 | TLE | |
p02627 | Python | Runtime Error | s = input()
if s.isuper():
print("A")
else:
print("a")
| s = input()
if s.isupper():
print("A")
else:
print("a")
| replace | 1 | 2 | 1 | 2 | AttributeError: 'str' object has no attribute 'isuper'. Did you mean: 'isupper'? | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02627/Python/s327847448.py", line 2, in <module>
if s.isuper():
AttributeError: 'str' object has no attribute 'isuper'. Did you mean: 'isupper'?
|
p02627 | Python | Runtime Error | a = input()
if ord(a) >= "a" and ord(a) <= "z":
print("a")
else:
print("A")
| a = input()
if ord(a) >= ord("a") and ord(a) <= ord("z"):
print("a")
else:
print("A")
| replace | 1 | 2 | 1 | 2 | TypeError: '>=' not supported between instances of 'int' and 'str' | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02627/Python/s399590534.py", line 2, in <module>
if ord(a) >= 'a' and ord(a) <= 'z':
TypeError: '>=' not supported between instances of 'int' and 'str'
|
p02627 | Python | Runtime Error | s = int(input())
if s == s.lower():
print("a")
else:
print("A")
| s = input()
if s == s.lower():
print("a")
else:
print("A")
| replace | 0 | 1 | 0 | 1 | ValueError: invalid literal for int() with base 10: 'B' | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02627/Python/s654626857.py", line 1, in <module>
s = int(input())
ValueError: invalid literal for int() with base 10: 'B'
|
p02627 | C++ | Runtime Error | #include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define SCANF(i) \
int i; \
scanf("%d", &i)
typedef long long ll;
using namespace std;
using P = pair<int, int>;
const long long INF = 1LL << 60;
const int IINF = 100000000;
const int MOD = (int)1e9 + 7;
// 約数列挙
vector<long long> divisor(long long n) {
vector<long long> ret;
for (long long i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n)
ret.push_back(n / i);
}
}
// sort(ret.begin(), ret.end()); // 昇順に並べる
return ret;
}
vector<int> dx = {1, 0, -1, 0};
vector<int> dy = {0, -1, 0, 1};
signed main() {
char t;
cin >> t;
for (char c = 'a'; c <= 'z'; c++) {
if (c == t) {
cout << 'a' << endl;
return 1;
}
}
cout << 'A' << endl;
} // https://atcoder.jp/contests/abc171/tasks/abc171_a | #include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define SCANF(i) \
int i; \
scanf("%d", &i)
typedef long long ll;
using namespace std;
using P = pair<int, int>;
const long long INF = 1LL << 60;
const int IINF = 100000000;
const int MOD = (int)1e9 + 7;
// 約数列挙
vector<long long> divisor(long long n) {
vector<long long> ret;
for (long long i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n)
ret.push_back(n / i);
}
}
// sort(ret.begin(), ret.end()); // 昇順に並べる
return ret;
}
vector<int> dx = {1, 0, -1, 0};
vector<int> dy = {0, -1, 0, 1};
signed main() {
char t;
cin >> t;
for (char c = 'a'; c <= 'z'; c++) {
if (c == t) {
cout << 'a' << endl;
return 0;
}
}
cout << 'A' << endl;
} // https://atcoder.jp/contests/abc171/tasks/abc171_a | replace | 35 | 36 | 35 | 36 | 0 | |
p02627 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
#define ALL(A) A.begin(), A.end()
#define MEM(a, b) memset(a, b, sizeof(a))
#define FOR(n) for (int i = 0; i < n; ++i)
#define FOR_(n) for (int i = 1; i <= n; ++i)
#define FOR_J(n) for (int j = 0; j < n; ++j)
#define FOR_J_(n) for (int j = 1; j <= n; ++j)
#define IT_FOR(A) for (auto i = A.begin(); i != A.end(); ++i)
#define INF 0x3f3f3f3f
#define N ((int)1e6)
// #define max(_a,_b) (((_a) > (_b)) ? (_a) : (_b))
// #define min(_a,_b) (((_a) < (_b)) ? (_a) : (_b))
const int mod = 1000000007;
typedef long long ll;
inline int read() {
int x = 0, w = 1;
char ch = 0;
while (ch < '0' || ch > '9') {
if (ch == '-')
w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + (ch - '0');
ch = getchar();
}
return x * w;
}
int main() {
// freopen("in.txt", "r", stdin);
// freopen("wa4.txt","w",stdout);
int _ = 1;
_ = read();
while (_--) {
char c;
cin >> c;
char t = tolower(c);
if (c == t)
cout << "a\n";
else
cout << "A\n";
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ALL(A) A.begin(), A.end()
#define MEM(a, b) memset(a, b, sizeof(a))
#define FOR(n) for (int i = 0; i < n; ++i)
#define FOR_(n) for (int i = 1; i <= n; ++i)
#define FOR_J(n) for (int j = 0; j < n; ++j)
#define FOR_J_(n) for (int j = 1; j <= n; ++j)
#define IT_FOR(A) for (auto i = A.begin(); i != A.end(); ++i)
#define INF 0x3f3f3f3f
#define N ((int)1e6)
// #define max(_a,_b) (((_a) > (_b)) ? (_a) : (_b))
// #define min(_a,_b) (((_a) < (_b)) ? (_a) : (_b))
const int mod = 1000000007;
typedef long long ll;
inline int read() {
int x = 0, w = 1;
char ch = 0;
while (ch < '0' || ch > '9') {
if (ch == '-')
w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + (ch - '0');
ch = getchar();
}
return x * w;
}
int main() {
// freopen("in.txt", "r", stdin);
// freopen("wa4.txt","w",stdout);
int _ = 1;
//_ = read();
while (_--) {
char c;
cin >> c;
char t = tolower(c);
if (c == t)
cout << "a\n";
else
cout << "A\n";
}
return 0;
} | replace | 35 | 36 | 35 | 36 | TLE | |
p02627 | C++ | Runtime Error | #include <bits/stdc++.h>
#define int long long int
#define pb push_back
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define pii pair<int, int>
#define vvi vector<vector<int>>
#define vi vector<int>
#define f first
#define s second
using namespace std;
const int mod = 1e9 + 7;
int logi(int n) {
int p = 0;
while (n / 2) {
n = n / 2;
p++;
}
return p;
}
int poww(int x, int y) {
int pro = 1;
while (y) {
if (y & 1)
pro = ((pro % mod) * (x % mod) % mod);
y = y >> 1;
x = ((x % mod) * (x % mod) % mod);
}
return pro;
}
int poww2(int x, int y) {
int pro = 1;
while (y) {
if (y & 1)
pro = pro * x;
y = y >> 1;
x = x * x;
}
return pro;
}
bool isprime(int n) {
if (n == 1)
return false;
for (int i = 2; i <= sqrt(n); i++)
if (n % i == 0)
return false;
return true;
}
/*
*/
int solve() {
char c;
cin >> c;
if (c >= 'A' && c <= 'Z')
cout << 'A';
else
cout << 'a';
}
int32_t main() {
fast;
int t = 1;
// cin>>t;
while (t--) {
solve();
}
}
| #include <bits/stdc++.h>
#define int long long int
#define pb push_back
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define pii pair<int, int>
#define vvi vector<vector<int>>
#define vi vector<int>
#define f first
#define s second
using namespace std;
const int mod = 1e9 + 7;
int logi(int n) {
int p = 0;
while (n / 2) {
n = n / 2;
p++;
}
return p;
}
int poww(int x, int y) {
int pro = 1;
while (y) {
if (y & 1)
pro = ((pro % mod) * (x % mod) % mod);
y = y >> 1;
x = ((x % mod) * (x % mod) % mod);
}
return pro;
}
int poww2(int x, int y) {
int pro = 1;
while (y) {
if (y & 1)
pro = pro * x;
y = y >> 1;
x = x * x;
}
return pro;
}
bool isprime(int n) {
if (n == 1)
return false;
for (int i = 2; i <= sqrt(n); i++)
if (n % i == 0)
return false;
return true;
}
/*
*/
int solve() {
char c;
cin >> c;
if (c >= 'A' && c <= 'Z')
cout << 'A';
else
cout << 'a';
return 0;
}
int32_t main() {
fast;
int t = 1;
// cin>>t;
while (t--) {
solve();
}
}
| insert | 61 | 61 | 61 | 62 | 0 | |
p02628 | Python | Runtime Error | N, K = map(int, input().split())
P = list(map(int, input().split())).sort()
print(sum(P[:K]))
| N, K = map(int, input().split())
P = sorted(list(map(int, input().split())))
print(sum(P[:K]))
| replace | 1 | 2 | 1 | 2 | TypeError: 'NoneType' object is not subscriptable | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02628/Python/s595641778.py", line 3, in <module>
print(sum(P[:K]))
TypeError: 'NoneType' object is not subscriptable
|
p02628 | Python | Runtime Error | N, K = (int(i) for i in input().split())
p = [input().split()]
sp = sorted(p)
print(sum(sp[:K]))
| N, K = (int(i) for i in input().split())
p = [int(i) for i in input().split()]
sp = sorted(p)
print(sum(sp[:K]))
| replace | 1 | 2 | 1 | 2 | TypeError: unsupported operand type(s) for +: 'int' and 'list' | Traceback (most recent call last):
File "/home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02628/Python/s927789922.py", line 5, in <module>
print(sum(sp[:K]))
TypeError: unsupported operand type(s) for +: 'int' and 'list'
|
p02628 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define vi vector<int>
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define sz(x) (x).size()
#define all(x) (x).begin(), (x).end() // Forward traversal
#define rall(x) (x).rbegin, (x).rend() // reverse traversal
#define ll long long
#define ld long double
const ld EPS = 10e-9;
const int INF = 1e9 + 5;
const long long INFLL = (ll)INF * (ll)INF;
const ll MOD = 1e9 + 7;
const ld PI = 3.14159265358979323846;
struct HASH {
size_t operator()(const pair<int, int> &x) const {
return hash<long long>()(((long long)x.first) ^
(((long long)x.second) << 32));
}
};
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << "[";
for (int i = 0; i < (int)v.size(); ++i) {
os << v[i];
if (i != (int)v.size() - 1)
os << ", ";
}
os << "]\n";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
os << "{";
for (const auto &it : v) {
os << it;
if (it != *v.rbegin())
os << ", ";
}
os << "}\n";
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const unordered_set<T> &v) {
os << "{";
for (const auto &it : v) {
os << it;
if (it != *v.rbegin())
os << ", ";
}
os << "}\n";
return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const map<T, S> &v) {
os << "{";
for (const auto &it : v) {
os << it.first << " : " << it.second;
if (it != *v.rbegin())
os << ", ";
}
os << "}\n";
return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const unordered_map<T, S> &v) {
os << "{";
for (const auto &it : v) {
os << it.first << " : " << it.second;
if (it != *v.rbegin())
os << ", ";
}
os << "}\n";
return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
os << "(";
os << v.first << ", " << v.second << ")";
os << "\n";
return os;
}
#define DEBUG
#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1;
}
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 debug(...)
#endif
// __gcd(x, y)
// idea behind binary search: there is monotonically increasing predicate and
// range [l, r]. Find x in range such that x is smallest value for which
// predicate holds true.
// int lo = 1, hi = 1e9;
// while(lo < hi)
// {
// ll mid = (lo+hi) / 2;
// if(predicate(mid))
// {
// hi = mid;
// }
// else
// {
// lo = mid+1;
// }
// }
// assert(lo == hi);
// OR: given monotonically decreasing predicate, find x in range such that x is
// largest value for which predicate holds true.
// int x = 0;
// for(int s = max_step; s >= 1; s /= 2)
//{
// while(predicate(x+s)) x += s;
// }
// after this, x is the largest value for which predicate is true
// How to calculate mid element:
// floor((l+r) / 2) might overflow, use l + floor((r-l)/2)
// ll l = 0, r = s.size()-1;
// ll ans = -1;
// while(l < r)
//{
// ll mid = l + (r-l) / 2;
// if(P(mid)) {
// r = mid;
// ans = mid;
// }
// else
// {
// l = mid + 1;
// }
// }
//
// AND
//
// ll l = 0, r = s.size()-1;
// ll ans = -1;
// while(l <= r)
//{
// ll mid = l + (r-l) / 2;
// if(P(mid)) {
// r = mid-1;
// ans = mid;
// }
// else
// {
// l = mid + 1;
// }
// }
//
// ARE EQUIVALENT
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
vector<int> p;
for (int i = 0; i < n; i++)
cin >> p[i];
sort(all(p));
int cost = 0;
for (int i = 0; i < k; i++)
cost += p[i];
cout << cost << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define vi vector<int>
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define sz(x) (x).size()
#define all(x) (x).begin(), (x).end() // Forward traversal
#define rall(x) (x).rbegin, (x).rend() // reverse traversal
#define ll long long
#define ld long double
const ld EPS = 10e-9;
const int INF = 1e9 + 5;
const long long INFLL = (ll)INF * (ll)INF;
const ll MOD = 1e9 + 7;
const ld PI = 3.14159265358979323846;
struct HASH {
size_t operator()(const pair<int, int> &x) const {
return hash<long long>()(((long long)x.first) ^
(((long long)x.second) << 32));
}
};
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << "[";
for (int i = 0; i < (int)v.size(); ++i) {
os << v[i];
if (i != (int)v.size() - 1)
os << ", ";
}
os << "]\n";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &v) {
os << "{";
for (const auto &it : v) {
os << it;
if (it != *v.rbegin())
os << ", ";
}
os << "}\n";
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const unordered_set<T> &v) {
os << "{";
for (const auto &it : v) {
os << it;
if (it != *v.rbegin())
os << ", ";
}
os << "}\n";
return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const map<T, S> &v) {
os << "{";
for (const auto &it : v) {
os << it.first << " : " << it.second;
if (it != *v.rbegin())
os << ", ";
}
os << "}\n";
return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const unordered_map<T, S> &v) {
os << "{";
for (const auto &it : v) {
os << it.first << " : " << it.second;
if (it != *v.rbegin())
os << ", ";
}
os << "}\n";
return os;
}
template <typename T, typename S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
os << "(";
os << v.first << ", " << v.second << ")";
os << "\n";
return os;
}
#define DEBUG
#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1;
}
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 debug(...)
#endif
// __gcd(x, y)
// idea behind binary search: there is monotonically increasing predicate and
// range [l, r]. Find x in range such that x is smallest value for which
// predicate holds true.
// int lo = 1, hi = 1e9;
// while(lo < hi)
// {
// ll mid = (lo+hi) / 2;
// if(predicate(mid))
// {
// hi = mid;
// }
// else
// {
// lo = mid+1;
// }
// }
// assert(lo == hi);
// OR: given monotonically decreasing predicate, find x in range such that x is
// largest value for which predicate holds true.
// int x = 0;
// for(int s = max_step; s >= 1; s /= 2)
//{
// while(predicate(x+s)) x += s;
// }
// after this, x is the largest value for which predicate is true
// How to calculate mid element:
// floor((l+r) / 2) might overflow, use l + floor((r-l)/2)
// ll l = 0, r = s.size()-1;
// ll ans = -1;
// while(l < r)
//{
// ll mid = l + (r-l) / 2;
// if(P(mid)) {
// r = mid;
// ans = mid;
// }
// else
// {
// l = mid + 1;
// }
// }
//
// AND
//
// ll l = 0, r = s.size()-1;
// ll ans = -1;
// while(l <= r)
//{
// ll mid = l + (r-l) / 2;
// if(P(mid)) {
// r = mid-1;
// ans = mid;
// }
// else
// {
// l = mid + 1;
// }
// }
//
// ARE EQUIVALENT
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
vector<int> p(n);
for (int i = 0; i < n; i++)
cin >> p[i];
sort(all(p));
int cost = 0;
for (int i = 0; i < k; i++)
cost += p[i];
cout << cost << endl;
return 0;
}
| replace | 192 | 193 | 192 | 193 | -11 | |
p02628 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, K, sum = 0;
cin >> N >> K;
vector<int> A(N);
for (int i = 0; i < N; i++) {
cin >> A.at(i);
}
sort(A.begin(), A.end());
for (int i = 0; i, K; i++) {
sum += A.at(i);
}
cout << sum << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, K, sum = 0;
cin >> N >> K;
vector<int> A(N);
for (int i = 0; i < N; i++) {
cin >> A.at(i);
}
sort(A.begin(), A.end());
for (int i = 0; i < K; i++) {
sum += A.at(i);
}
cout << sum << endl;
} | replace | 11 | 12 | 11 | 12 | -6 | terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 5) >= this->size() (which is 5)
|
p02628 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using PLL = pair<ll, ll>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repn(i, n) for (int i = 0; i <= (int)(n); i++)
#define srep(i, l, n) for (int i = l; i < (int)(n); i++)
#define srepn(i, l, n) for (int i = l; i <= (int)(n); i++)
#define drep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define drepn(i, n) for (int i = (int)(n); i >= 0; i--)
#define size(s) (int)s.size()
#define debug(var) \
do { \
std::cout << #var << " : "; \
view(var); \
} while (0)
template <typename T> void view(T e) { std::cout << e << std::endl; }
template <typename T> void view(const std::vector<T> &v) {
for (const auto &e : v) {
std::cout << e << " ";
}
std::cout << std::endl;
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
for (const auto &v : vv) {
view(v);
}
}
template <typename T> inline istream &operator>>(istream &i, vector<T> v) {
rep(i, size(v)) cin >> v[i];
}
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 <class T> T gcd(T a, T b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template <class T = int> using V = vector<T>;
template <class T = int> using VV = vector<V<T>>;
bool isIn(int i, int j, int h, int w) {
return i >= 0 && i < h && j >= 0 && j < w;
}
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }
void err() { cout << -1 << endl; }
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define ep emplace_back
const int MOD = 1000000007;
const int INF = 1e9;
#define PI acos(-1);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int ddx[8] = {1, 1, 1, -1, -1, -1, 0, 0};
int ddy[8] = {0, 1, -1, 0, 1, -1, 1, -1};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
V<> a(n);
cin >> a;
sort(all(a));
ll ans = accumulate(a.begin(), a.begin() + k, 0);
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using PLL = pair<ll, ll>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repn(i, n) for (int i = 0; i <= (int)(n); i++)
#define srep(i, l, n) for (int i = l; i < (int)(n); i++)
#define srepn(i, l, n) for (int i = l; i <= (int)(n); i++)
#define drep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define drepn(i, n) for (int i = (int)(n); i >= 0; i--)
#define size(s) (int)s.size()
#define debug(var) \
do { \
std::cout << #var << " : "; \
view(var); \
} while (0)
template <typename T> void view(T e) { std::cout << e << std::endl; }
template <typename T> void view(const std::vector<T> &v) {
for (const auto &e : v) {
std::cout << e << " ";
}
std::cout << std::endl;
}
template <typename T> void view(const std::vector<std::vector<T>> &vv) {
for (const auto &v : vv) {
view(v);
}
}
template <typename T> inline istream &operator>>(istream &i, vector<T> &v) {
rep(j, size(v)) i >> v[j];
return i;
}
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 <class T> T gcd(T a, T b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
template <class T = int> using V = vector<T>;
template <class T = int> using VV = vector<V<T>>;
bool isIn(int i, int j, int h, int w) {
return i >= 0 && i < h && j >= 0 && j < w;
}
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }
void err() { cout << -1 << endl; }
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define ep emplace_back
const int MOD = 1000000007;
const int INF = 1e9;
#define PI acos(-1);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int ddx[8] = {1, 1, 1, -1, -1, -1, 0, 0};
int ddy[8] = {0, 1, -1, 0, 1, -1, 1, -1};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
V<> a(n);
cin >> a;
sort(all(a));
ll ans = accumulate(a.begin(), a.begin() + k, 0);
cout << ans << endl;
} | replace | 30 | 32 | 30 | 33 | TLE | |
p02628 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
#include <vector>
#define int long long
using namespace std;
const int N = 2e5 + 10;
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, k;
cin >> n >> k;
vector<int> v;
for (int i = 0; i < n; ++i)
cin >> v[i];
sort(v.begin(), v.end());
int ans = 0;
for (int i = 0; i < k; ++i)
ans += v[i];
cout << ans;
return 0;
} | #include <algorithm>
#include <iostream>
#include <vector>
#define int long long
using namespace std;
const int N = 2e5 + 10;
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, k;
cin >> n >> k;
vector<int> v(n);
for (int i = 0; i < n; ++i)
cin >> v[i];
sort(v.begin(), v.end());
int ans = 0;
for (int i = 0; i < k; ++i)
ans += v[i];
cout << ans;
return 0;
} | replace | 14 | 15 | 14 | 15 | -11 | |
p02628 | C++ | Runtime Error | #include <stdio.h>
int main(void) {
int n, k;
scanf("%d", &n);
scanf("%d", &k);
int p[n];
for (int i = 0; i < n; i++) {
scanf("%d", &p[i]);
}
int min, tmp;
for (int i = 0; i < n - 1; i++) {
min = p[i];
for (int j = i + 1; j < n; j++) {
if (min > p[j]) {
min = p[j];
tmp = j;
}
}
p[tmp] = p[i];
p[i] = min;
}
int sum = 0;
for (int i = 0; i < k; i++) {
sum += p[i];
}
printf("%d", sum);
return 0;
} | #include <stdio.h>
int main(void) {
int n, k;
scanf("%d", &n);
scanf("%d", &k);
int p[n];
for (int i = 0; i < n; i++) {
scanf("%d", &p[i]);
}
int min, tmp;
for (int i = 0; i < n - 1; i++) {
min = p[i];
tmp = i;
for (int j = i + 1; j < n; j++) {
if (min > p[j]) {
min = p[j];
tmp = j;
}
}
p[tmp] = p[i];
p[i] = min;
}
int sum = 0;
for (int i = 0; i < k; i++) {
sum += p[i];
}
printf("%d", sum);
return 0;
} | insert | 18 | 18 | 18 | 19 | 0 | |
p02628 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define input1 \
freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout);
const ll nax = 1e9 + 7;
void solve() {
int n, k;
cin >> n >> k;
vector<int> a;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
a.push_back(x);
}
sort(a.begin(), a.end());
int ans = 0;
for (int i = 0; i < k; i++) {
ans += a[i];
}
cout << ans;
}
int main() {
input1;
solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define input1 \
freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout);
const ll nax = 1e9 + 7;
void solve() {
int n, k;
cin >> n >> k;
vector<int> a;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
a.push_back(x);
}
sort(a.begin(), a.end());
int ans = 0;
for (int i = 0; i < k; i++) {
ans += a[i];
}
cout << ans;
}
int main() {
// input1;
solve();
return 0;
} | replace | 25 | 26 | 25 | 26 | -6 | terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
|
p02628 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
using namespace std;
int N, K, p[100];
int main() {
cin >> N >> K;
for (int i = 0; i < N; ++i)
cin >> p[i];
sort(p, p + N);
int ans = 0;
for (int i = 0; i < K; ++i)
ans += p[i];
cout << ans << endl;
} | #include <algorithm>
#include <iostream>
using namespace std;
int N, K, p[1010];
int main() {
cin >> N >> K;
for (int i = 0; i < N; ++i)
cin >> p[i];
sort(p, p + N);
int ans = 0;
for (int i = 0; i < K; ++i)
ans += p[i];
cout << ans << endl;
} | replace | 5 | 6 | 5 | 6 | 0 | |
p02628 | C++ | Runtime Error | // AUTHOR:BlackBox Inc.//
#include <bits/stdc++.h>
#include <tr1/unordered_map>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define vlli vector<long long int>
#define vvi vector<vector<int>>
#define endl "\n"
#define vpii vector<pair<int, int>>
#define vs vector<string>
#define lin1(x) \
ll int x; \
x = scan();
#define lin2(x, y) \
ll int x, y; \
x = scan(), y = scan();
#define lin3(x, y, z) \
ll int x, y, z; \
x = scan(), y = scan(), z = scan();
#define pb(x) push_back(x)
#define makep(x, y) make_pair(x, y)
#define mx(a, b) (a) > (b) ? (a) : (b)
#define mn(a, b) (a) < (b) ? (a) : (b)
#define fori(s, e) for (i = s; i <= e; i++)
#define forj(s, e) for (j = s; j <= e; j++)
#define fork(s, e) for (k = s; k <= e; k++)
#define rep(i, s, e) for (int i = s; i <= e; i++)
#define brep(i, s, e) for (int i = s; i >= e; i--)
#define all(x) x.begin(), x.end()
#define mem(x, y) memset(x, y, sizeof(x));
#define bits1(x) __builtin_popcount(x)
#define pi 3.14159265358979323846264338327950
#define MOD7 1000000007
#define MOD9 1000000009
#define cn cin >>
#define co cout <<
#define fast \
std::ios::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
using namespace std;
using namespace std::tr1;
int main() {
#ifndef RAHUL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int a, b;
cin >> a >> b;
std::vector<int> v(a);
for (int i = 0; i < a; ++i) {
/* code */
cin >> v[i];
}
sort(v.begin(), v.end());
int count = 0;
for (int i = 0; i < b; ++i) {
/* code */
count += v[i];
}
cout << count << endl;
return 0;
} | // AUTHOR:BlackBox Inc.//
#include <bits/stdc++.h>
#include <tr1/unordered_map>
#include <unordered_map>
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define vlli vector<long long int>
#define vvi vector<vector<int>>
#define endl "\n"
#define vpii vector<pair<int, int>>
#define vs vector<string>
#define lin1(x) \
ll int x; \
x = scan();
#define lin2(x, y) \
ll int x, y; \
x = scan(), y = scan();
#define lin3(x, y, z) \
ll int x, y, z; \
x = scan(), y = scan(), z = scan();
#define pb(x) push_back(x)
#define makep(x, y) make_pair(x, y)
#define mx(a, b) (a) > (b) ? (a) : (b)
#define mn(a, b) (a) < (b) ? (a) : (b)
#define fori(s, e) for (i = s; i <= e; i++)
#define forj(s, e) for (j = s; j <= e; j++)
#define fork(s, e) for (k = s; k <= e; k++)
#define rep(i, s, e) for (int i = s; i <= e; i++)
#define brep(i, s, e) for (int i = s; i >= e; i--)
#define all(x) x.begin(), x.end()
#define mem(x, y) memset(x, y, sizeof(x));
#define bits1(x) __builtin_popcount(x)
#define pi 3.14159265358979323846264338327950
#define MOD7 1000000007
#define MOD9 1000000009
#define cn cin >>
#define co cout <<
#define fast \
std::ios::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
using namespace std;
using namespace std::tr1;
int main() {
// #ifndef RAHUL
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// #endif
int a, b;
cin >> a >> b;
std::vector<int> v(a);
for (int i = 0; i < a; ++i) {
/* code */
cin >> v[i];
}
sort(v.begin(), v.end());
int count = 0;
for (int i = 0; i < b; ++i) {
/* code */
count += v[i];
}
cout << count << endl;
return 0;
} | replace | 46 | 50 | 46 | 50 | 0 | |
p02628 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <iostream>
using namespace std;
int main() {
int N, K, Only1;
cin >> N >> K;
if (N == 1) {
cin >> Only1;
cout << Only1 << endl;
} else {
vector<int> nedan(N);
for (int i = 0; i < N; i++) {
cin >> nedan.at(i);
}
sort(nedan.begin(), nedan.end());
int goukei;
goukei = nedan.at(0) + nedan.at(1) + nedan.at(2);
cout << goukei << endl;
}
}
| #include <bits/stdc++.h>
#include <iostream>
using namespace std;
int main() {
int N, K, Only1;
cin >> N >> K;
if (N == 1) {
cin >> Only1;
cout << Only1 << endl;
} else {
vector<int> nedan(N);
for (int i = 0; i < N; i++) {
cin >> nedan.at(i);
}
sort(nedan.begin(), nedan.end());
int goukei = 0;
for (int i = 0; i < K; i++) {
goukei = goukei + nedan.at(i);
}
cout << goukei << endl;
}
}
| replace | 20 | 22 | 20 | 25 | 0 | |
p02628 | C++ | Time Limit Exceeded | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
int arr[n];
for (int i = 0; i < n; i++)
cin >> arr[i];
int sum = 0, min = 1001;
while (k != 0) {
for (int i = 0; i < n; i++) {
if (min > arr[i]) {
min = arr[i];
}
}
sum += min;
for (int i = 0; i < n; i++) {
if (min == arr[i]) {
arr[i] = 1001;
break;
}
}
}
cout << sum;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
int arr[n];
for (int i = 0; i < n; i++)
cin >> arr[i];
int sum = 0, min = 1001;
while (k != 0) {
for (int i = 0; i < n; i++) {
if (min > arr[i]) {
min = arr[i];
}
}
sum += min;
for (int i = 0; i < n; i++) {
if (min == arr[i]) {
arr[i] = 1001;
break;
}
}
k--;
min = 1002;
}
cout << sum;
} | insert | 22 | 22 | 22 | 24 | TLE | |
p02628 | C++ | Runtime Error | #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>;
int main() {
int n, k, sum = 0;
cin >> n >> k;
vector<int> p;
rep(i, n) cin >> p.at(i);
sort(p.begin(), p.end());
rep(i, k) { sum += p.at(i); }
cout << sum << endl;
} | #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>;
int main() {
int n, k, sum = 0;
cin >> n >> k;
vector<int> p(n);
rep(i, n) cin >> p.at(i);
sort(p.begin(), p.end());
rep(i, k) { sum += p.at(i); }
cout << sum << endl;
} | replace | 9 | 10 | 9 | 10 | -6 | terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)
|
p02628 | C++ | Runtime Error | #include <bits/stdc++.h>
// #define ll long long
using namespace std;
#define SPEED \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0)
#define fileio \
freopen("http://in.in", "r", stdin), freopen("out.out", "w", stdout);
#define ll long long
#define pb push_back
#define pll pair<ll, ll>
#define ff first
#define ss second
#define N 2000001
#define M 998244353
#define INF 1000000000000000
std::vector<ll> v;
int main() {
SPEED;
ll t;
cin >> t;
while (t-- > 0) {
ll n, k;
cin >> n >> k;
ll a[n];
ll i;
for (i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
ll sum = 0;
for (i = 0; i < k; i++) {
sum += a[i];
}
cout << sum;
}
} | #include <bits/stdc++.h>
// #define ll long long
using namespace std;
#define SPEED \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0)
#define fileio \
freopen("http://in.in", "r", stdin), freopen("out.out", "w", stdout);
#define ll long long
#define pb push_back
#define pll pair<ll, ll>
#define ff first
#define ss second
#define N 2000001
#define M 998244353
#define INF 1000000000000000
std::vector<ll> v;
int main() {
SPEED;
ll t;
t = 1;
while (t-- > 0) {
ll n, k;
cin >> n >> k;
ll a[n];
ll i;
for (i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
ll sum = 0;
for (i = 0; i < k; i++) {
sum += a[i];
}
cout << sum;
}
} | replace | 23 | 24 | 23 | 24 | 0 | |
p02628 | C++ | Runtime Error | #include <algorithm>
#include <cmath>
#include <iostream>
#include <limits.h>
#include <map>
#include <set>
#include <utility>
#include <vector>
using namespace std;
using P = pair<int, int>;
using ll = long long;
constexpr int MOD = 998244353;
constexpr int MAX = 510000;
int main() {
int n, k;
cin >> n >> k;
vector<int> a;
for (int i = 0; i < n; i++)
cin >> a[i];
sort(a.begin(), a.end());
ll ret = 0;
for (int i = 0; i < k; i++)
ret += a[i];
cout << ret << endl;
} | #include <algorithm>
#include <cmath>
#include <iostream>
#include <limits.h>
#include <map>
#include <set>
#include <utility>
#include <vector>
using namespace std;
using P = pair<int, int>;
using ll = long long;
constexpr int MOD = 998244353;
constexpr int MAX = 510000;
int main() {
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
sort(a.begin(), a.end());
ll ret = 0;
for (int i = 0; i < k; i++)
ret += a[i];
cout << ret << endl;
} | replace | 16 | 17 | 16 | 17 | -11 | |
p02628 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int a[50], k, t, i, s = 0, j;
cin >> t >> k;
for (i = 0; i < t; i++) {
cin >> a[i];
}
sort(a, a + t);
for (j = 0; j < k; j++) {
s = s + a[j];
}
cout << s << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int a[1001], k, t, i, s = 0, j;
cin >> t >> k;
for (i = 0; i < t; i++) {
cin >> a[i];
}
sort(a, a + t);
for (j = 0; j < k; j++) {
s = s + a[j];
}
cout << s << endl;
return 0;
}
| replace | 3 | 4 | 3 | 4 | 0 | |
p02628 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int main(void) {
int i, n, k, p[1000], sum = 0;
scanf("%d %d", n, k);
for (i = 0; i < n; i++)
scanf("%d", &p[i]);
sort(p, p + n);
for (i = 0; i < k; i++)
sum += p[i];
printf("%d\n", sum);
return 0;
}
| #include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int main(void) {
int i, n, k, p[1000], sum = 0;
scanf("%d %d", &n, &k);
for (i = 0; i < n; i++)
scanf("%d", &p[i]);
sort(p, p + n);
for (i = 0; i < k; i++)
sum += p[i];
printf("%d\n", sum);
return 0;
}
| replace | 6 | 7 | 6 | 7 | -11 | |
p02628 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int K, N;
cin >> K >> N;
vector<int> vec(N);
int money = 0;
for (int i = 0; i < N; i++) {
cin >> vec.at(i);
}
sort(vec.begin(), vec.end());
for (int i = 0; i < K; i++) {
money += vec.at(i);
}
cout << money << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, K;
cin >> N >> K;
vector<int> vec(N);
int money = 0;
for (int i = 0; i < N; i++) {
cin >> vec.at(i);
}
sort(vec.begin(), vec.end());
for (int i = 0; i < K; i++) {
money += vec.at(i);
}
cout << money << endl;
} | replace | 4 | 6 | 4 | 6 | -6 | terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 3) >= this->size() (which is 3)
|
p02628 | C++ | Time Limit Exceeded | #include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
typedef long long int llint;
unsigned int min(unsigned int A, unsigned int B) {
if (A < B) {
return A;
} else {
return B;
}
}
int main() {
int N, K;
cin >> N >> K;
vector<int> P;
P.push_back(0);
for (int i = 0; i < N; i++) {
int p;
cin >> p;
P.push_back(p);
}
int dp[N + 1][K + 1]; // dp[i][j]=i番目までj個選んだ時の最小の金額
dp[0][0] = 0;
for (int i = 1; i <= K; i++) {
dp[0][i] = 0x0FFFFFFF;
}
for (int i = 1; i <= N; i++) {
dp[i][0] = 0;
for (int k = 1; k <= K; k++) {
dp[i][k] = min(dp[i - 1][k - 1] + P[i], dp[i - 1][k]);
}
}
for (int i = 0; i <= N; i++) {
for (int k = 0; k <= K; k++) {
clog << dp[i][k] << " ";
}
clog << endl;
}
cout << dp[N][K] << endl;
return 0;
}
| #include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
typedef long long int llint;
unsigned int min(unsigned int A, unsigned int B) {
if (A < B) {
return A;
} else {
return B;
}
}
int main() {
int N, K;
cin >> N >> K;
vector<int> P;
P.push_back(0);
for (int i = 0; i < N; i++) {
int p;
cin >> p;
P.push_back(p);
}
int dp[N + 1][K + 1]; // dp[i][j]=i番目までj個選んだ時の最小の金額
dp[0][0] = 0;
for (int i = 1; i <= K; i++) {
dp[0][i] = 0x0FFFFFFF;
}
for (int i = 1; i <= N; i++) {
dp[i][0] = 0;
for (int k = 1; k <= K; k++) {
dp[i][k] = min(dp[i - 1][k - 1] + P[i], dp[i - 1][k]);
}
}
/*
for(int i=0;i<=N;i++){
for(int k=0;k<=K;k++){
clog << dp[i][k] << " ";
}
clog << endl;
}
*/
cout << dp[N][K] << endl;
return 0;
}
| replace | 42 | 48 | 42 | 50 | TLE | |
p02628 | C++ | Runtime Error | #include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <deque> // deque
#include <iostream> // cout, endl, cin
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <string> // string, to_string, stoi
#include <tuple> // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility> // pair, make_pair
#include <vector> // vector
using namespace std;
int main() {
int N, K;
cin >> N >> K;
vector<int> vec(N);
// 100個の入力を受け取る
for (int i = 0; i < N; i++) {
cin >> vec.at(i);
}
sort(vec.begin(), vec.end());
int X = 0;
for (int i = 0; i <= K + 2; i++) {
X += vec.at(i);
}
cout << X << endl;
} | #include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <deque> // deque
#include <iostream> // cout, endl, cin
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <string> // string, to_string, stoi
#include <tuple> // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility> // pair, make_pair
#include <vector> // vector
using namespace std;
int main() {
int N, K;
cin >> N >> K;
vector<int> vec(N);
// 100個の入力を受け取る
for (int i = 0; i < N; i++) {
cin >> vec.at(i);
}
sort(vec.begin(), vec.end());
int X = 0;
for (int i = 0; i < K; i++) {
X += vec.at(i);
}
cout << X << endl;
} | replace | 30 | 31 | 30 | 31 | -6 | terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 5) >= this->size() (which is 5)
|
p02628 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
using namespace std;
#define endl '\n'
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int K, N;
cin >> N >> K;
int a[K];
for (int i = 0; i < N; i++) {
int n;
cin >> n;
a[i] = n;
}
sort(a, a + N);
int num = 0;
for (int j = 0; j < K; j++) {
num = num + a[j];
}
cout << num << endl;
} | #include <algorithm>
#include <iostream>
using namespace std;
#define endl '\n'
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int K, N;
cin >> N >> K;
int a[N];
for (int i = 0; i < N; i++) {
int n;
cin >> n;
a[i] = n;
}
sort(a, a + N);
int num = 0;
for (int j = 0; j < K; j++) {
num = num + a[j];
}
cout << num << endl;
} | replace | 10 | 11 | 10 | 11 | 0 | |
p02628 | C++ | Runtime Error | /**********************************************************************************************
******************************AUTHOR:**********************************************************
****************************ASHISH
*RANJAN******************************************************
***********************************************************************************************/
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using namespace std::chrono;
/*
auto start = high_resolution_clock::now();
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cout << "Time taken by function: "
<< duration.count() << " microseconds" << endl;
*/
#define fast() \
ios_base::sync_with_stdio(false); \
cin.tie(NULL)
#define ll long long
#define mod 1000000007
#define fr(i, n) for (int i = 0; i < n; i++)
#define frs(i, a, b) for (int i = a; i < b; i++)
#define meme(a, b) memset(a, b, sizeof(a)); // works only for 0 and -1
int main() {
fast();
int n, k;
cin >> n >> k;
int arr[k];
fr(i, n) cin >> arr[i];
sort(arr, arr + n);
ll sum = 0;
fr(i, k) sum += arr[i];
cout << sum;
return 0;
}
| /**********************************************************************************************
******************************AUTHOR:**********************************************************
****************************ASHISH
*RANJAN******************************************************
***********************************************************************************************/
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using namespace std::chrono;
/*
auto start = high_resolution_clock::now();
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cout << "Time taken by function: "
<< duration.count() << " microseconds" << endl;
*/
#define fast() \
ios_base::sync_with_stdio(false); \
cin.tie(NULL)
#define ll long long
#define mod 1000000007
#define fr(i, n) for (int i = 0; i < n; i++)
#define frs(i, a, b) for (int i = a; i < b; i++)
#define meme(a, b) memset(a, b, sizeof(a)); // works only for 0 and -1
int main() {
fast();
int n, k;
cin >> n >> k;
int arr[n];
fr(i, n) cin >> arr[i];
sort(arr, arr + n);
ll sum = 0;
fr(i, k) sum += arr[i];
cout << sum;
return 0;
}
| replace | 34 | 35 | 34 | 35 | -6 | *** stack smashing detected ***: terminated
|
p02628 | Python | Time Limit Exceeded | import itertools
N, K = map(int, input().split())
p = list(map(int, input().split()))
ans = sum(p)
for v in itertools.permutations(p, K):
ans = min(ans, sum(v))
print(ans)
| import itertools
N, K = map(int, input().split())
p = list(map(int, input().split()))
ans = 0
for _ in range(K):
min_index = p.index(min(p))
minv = p.pop(min_index)
ans += minv
print(ans)
| replace | 5 | 8 | 5 | 10 | TLE | |
p02628 | Python | Time Limit Exceeded | N, K = map(int, input().split())
P = list(map(int, input().split()))
ans = float("inf")
for i in range(1 << len(P)):
sum = 0
cnt = 0
for j in range(len(P)):
if (i >> j) & 1 == 1:
sum += P[j]
cnt += 1
if cnt == K:
ans = min(ans, sum)
print(ans)
| N, K = map(int, input().split())
P = list(map(int, input().split()))
P = sorted(P)
print(sum(P[:K]))
| replace | 3 | 14 | 3 | 6 | TLE | |
p02628 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define PB push_back
#define in insert
#define MP make_pair
#define F first
#define S second
void faster() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
void inout() {
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
}
void solve() {
ll n, k;
cin >> n >> k;
ll ara[n + 10];
for (int i = 1; i <= n; i++) {
cin >> ara[i];
}
sort(ara + 1, ara + n + 1);
ll sum = 0;
for (int i = 1; i <= k; i++) {
sum += ara[i];
}
cout << sum;
}
int main() {
faster();
inout();
solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define PB push_back
#define in insert
#define MP make_pair
#define F first
#define S second
void faster() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
void inout() {
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
}
void solve() {
ll n, k;
cin >> n >> k;
ll ara[n + 10];
for (int i = 1; i <= n; i++) {
cin >> ara[i];
}
sort(ara + 1, ara + n + 1);
ll sum = 0;
for (int i = 1; i <= k; i++) {
sum += ara[i];
}
cout << sum;
}
int main() {
faster();
// inout();
solve();
return 0;
} | replace | 33 | 34 | 33 | 34 | -11 | |
p02628 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, K;
cin >> N >> K;
vector<int> wage;
for (int i = 0; i < N; i++) {
cin >> wage.at(i);
}
sort(wage.begin(), wage.end());
int answer = 0;
for (int i = 0; i < K; i++) {
answer = answer + wage.at(i);
}
cout << answer << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, K;
cin >> N >> K;
vector<int> wage(N);
for (int i = 0; i < N; i++) {
cin >> wage.at(i);
}
sort(wage.begin(), wage.end());
int answer = 0;
for (int i = 0; i < K; i++) {
answer = answer + wage.at(i);
}
cout << answer << endl;
} | replace | 5 | 6 | 5 | 6 | -6 | terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 0) >= this->size() (which is 0)
|
p02628 | C++ | Runtime Error | #include <algorithm>
#include <cstdio>
using namespace std;
int main() {
int n, k;
scanf("%d%d", &n, &k);
int p[100];
int i;
for (i = 0; i < n; i++) {
scanf("%d", &p[i]);
}
sort(p, p + n);
int sum = 0;
for (i = 0; i < k; i++) {
sum += p[i];
}
printf("%d\n", sum);
return 0;
}
| #include <algorithm>
#include <cstdio>
using namespace std;
int main() {
int n, k;
scanf("%d%d", &n, &k);
int p[1001];
int i;
for (i = 0; i < n; i++) {
scanf("%d", &p[i]);
}
sort(p, p + n);
int sum = 0;
for (i = 0; i < k; i++) {
sum += p[i];
}
printf("%d\n", sum);
return 0;
}
| replace | 8 | 9 | 8 | 9 | 0 | |
p02628 | C++ | Runtime Error | #include <algorithm>
#include <iostream>
int arr[1030];
int N, K;
int answer;
int main() {
scanf("%d %d", &N, &K);
for (int i = 0; i < N; i++) {
scanf("%d", arr[i]);
}
std::sort(arr, arr + N);
for (int i = 0; i < K; i++) {
answer += arr[i];
}
printf("%d", answer);
} | #include <algorithm>
#include <iostream>
int arr[1030];
int N, K;
int answer;
int main() {
scanf("%d %d", &N, &K);
for (int i = 0; i < N; i++) {
scanf("%d", &arr[i]);
}
std::sort(arr, arr + N);
for (int i = 0; i < K; i++) {
answer += arr[i];
}
printf("%d", answer);
} | replace | 10 | 11 | 10 | 11 | -11 | |
p02628 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, K;
cin >> N >> K;
vector<int> p(N);
for (int &i : p)
cin >> i;
sort(p.begin(), p.end());
int ans = 0;
for (int i = 0; i < 3; i++)
ans += p.at(i);
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, K;
cin >> N >> K;
vector<int> p(N);
for (int &i : p)
cin >> i;
sort(p.begin(), p.end());
int ans = 0;
for (int i = 0; i < K; i++)
ans += p.at(i);
cout << ans << endl;
} | replace | 11 | 12 | 11 | 12 | 0 | |
p02628 | C++ | Runtime Error | #include <bits/stdc++.h>
using namespace std;
int p[100];
int main() {
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
int n, k, sum = 0;
cin >> n >> k;
for (int i = 0; i < n; ++i) {
cin >> p[i];
}
sort(p, p + n);
for (int i = 0; i < k; ++i) {
sum += p[i];
}
cout << sum << endl;
} | #include <bits/stdc++.h>
using namespace std;
int p[10000];
int main() {
cin.tie(0);
cout.tie(0);
ios_base::sync_with_stdio(0);
int n, k, sum = 0;
cin >> n >> k;
for (int i = 0; i < n; ++i) {
cin >> p[i];
}
sort(p, p + n);
for (int i = 0; i < k; ++i) {
sum += p[i];
}
cout << sum << endl;
} | replace | 3 | 4 | 3 | 4 | 0 | |
p02628 | C++ | Runtime Error | #include <bits/stdc++.h>
#include <iostream>
using namespace std;
int main() {
int n, k, price[n + 1], total = 0;
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> price[i];
}
sort(price, price + n);
for (int i = 0; i < k; i++)
total = total + price[i];
cout << total;
} | #include <bits/stdc++.h>
#include <iostream>
using namespace std;
int main() {
int n, k, price[1001], total = 0;
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> price[i];
}
sort(price, price + n);
for (int i = 0; i < k; i++)
total = total + price[i];
cout << total;
} | replace | 6 | 7 | 6 | 7 | 0 | |
p02628 | C++ | Runtime Error | #pragma region header
#include <assert.h>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rev(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define rev1(i, n) for (int i = (int)(n); i > 0; i--)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define resort(v) sort((v).rbegin(), (v).rend())
#define vi vector<int>
#define vvi vector<vector<int>>
#define vs vector<string>
#define vvs vector<vector<string>>
#define vc vector<char>
#define vvc vector<vector<char>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
using ll = long long;
using P = pair<int, int>;
/* ----------------よく使う数字や配列----------------- */
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
constexpr ll mod = 1e9 + 7;
constexpr ll inf = INT32_MAX / 2;
constexpr ll INF = LLONG_MAX / 2;
constexpr long double eps = DBL_EPSILON;
constexpr long double pi = 3.141592653589793238462643383279;
/* ----------------------end----------------------- */
/* --------------------テンプレート------------------ */
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
/* ----------------------end----------------------- */
/* --------------------ライブラリ-------------------- */
ll fact(int i) { // 階乗
if (i == 0)
return 1;
return (fact(i - 1)) * i % mod;
}
ll gcd(ll a, ll b) { // 最大公約数
if (b == 0)
return a;
return gcd(b, a % b);
}
ll lcm(ll a, ll b) { // 最小公倍数
return a * b / gcd(a, b);
}
int keta(ll n) { // 桁数を求める
if (n == 0)
return 1;
int count = 0;
while (n != 0) {
n /= 10;
count++;
}
return count;
}
bool isprime(ll a) // 素数判定、primeならtrue,違うならfalse
{
if (a < 2)
return false;
else if (a == 2)
return true;
else if (a % 2 == 0)
return false;
double m = sqrt(a);
for (int i = 3; i <= m; i += 2) {
if (a % i == 0) {
return false;
}
}
// 素数である
return true;
}
ll ketasum(ll n) { // 各桁の和
ll sum = 0;
while (n != 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
#define double long double
/* ----------------------end----------------------- */
#pragma endregion
signed main() {
int N, K;
cin >> N >> K;
vi fruit(N);
rep(i, N) { cin >> fruit.at(i); }
all(fruit);
cout << fruit.at(0) + fruit.at(1) + fruit.at(2) << endl;
}
| #pragma region header
#include <assert.h>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i <= (int)(n); i++)
#define rev(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define rev1(i, n) for (int i = (int)(n); i > 0; i--)
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define resort(v) sort((v).rbegin(), (v).rend())
#define vi vector<int>
#define vvi vector<vector<int>>
#define vs vector<string>
#define vvs vector<vector<string>>
#define vc vector<char>
#define vvc vector<vector<char>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
using ll = long long;
using P = pair<int, int>;
/* ----------------よく使う数字や配列----------------- */
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
constexpr ll mod = 1e9 + 7;
constexpr ll inf = INT32_MAX / 2;
constexpr ll INF = LLONG_MAX / 2;
constexpr long double eps = DBL_EPSILON;
constexpr long double pi = 3.141592653589793238462643383279;
/* ----------------------end----------------------- */
/* --------------------テンプレート------------------ */
template <class T> inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T> inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
/* ----------------------end----------------------- */
/* --------------------ライブラリ-------------------- */
ll fact(int i) { // 階乗
if (i == 0)
return 1;
return (fact(i - 1)) * i % mod;
}
ll gcd(ll a, ll b) { // 最大公約数
if (b == 0)
return a;
return gcd(b, a % b);
}
ll lcm(ll a, ll b) { // 最小公倍数
return a * b / gcd(a, b);
}
int keta(ll n) { // 桁数を求める
if (n == 0)
return 1;
int count = 0;
while (n != 0) {
n /= 10;
count++;
}
return count;
}
bool isprime(ll a) // 素数判定、primeならtrue,違うならfalse
{
if (a < 2)
return false;
else if (a == 2)
return true;
else if (a % 2 == 0)
return false;
double m = sqrt(a);
for (int i = 3; i <= m; i += 2) {
if (a % i == 0) {
return false;
}
}
// 素数である
return true;
}
ll ketasum(ll n) { // 各桁の和
ll sum = 0;
while (n != 0) {
sum += n % 10;
n /= 10;
}
return sum;
}
#define double long double
/* ----------------------end----------------------- */
#pragma endregion
signed main() {
int N, K;
cin >> N >> K;
vi fruit(N);
rep(i, N) { cin >> fruit.at(i); }
sort(fruit.begin(), fruit.end());
int sum = 0;
rep(i, K) { sum += fruit.at(i); }
cout << sum << endl;
}
| replace | 139 | 141 | 139 | 143 | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.