solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using pii = pair<int, int>;
int main() {
int n, k;
cin >> n >> k;
vector<int> p(n), c(n);
rep(i, n) {
cin >> p[i];
p[i]--;
}
rep(i, n) {
cin >> c[i];
}
ll ans = -1e18;
rep(i, n) {
int now = i;
vector<ll> s;
ll tot = 0;
while (1) {
now = p[now];
s.push_back(c[now]);
tot += c[now];
if (now == i) break;
}
int l = s.size();
ll t = 0;
rep(i, l) {
if (i + 1 > k) break;
t += s[i];
ll u = t;
if (tot > 0) {
u += tot * ((k - (i + 1)) / l);
}
ans = max(ans, u);
}
}
cout << ans << endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
void init() { freopen("input.txt", "r", stdin); }
void printDigit(signed long int x) {
if (x < 5) {
printf("O-|");
} else {
printf("-O|");
}
switch (x % 5) {
case 0:
printf("-OOOO");
break;
case 1:
printf("O-OOO");
break;
case 2:
printf("OO-OO");
break;
case 3:
printf("OOO-O");
break;
case 4:
printf("OOOO-");
break;
}
printf("\n");
}
void solve() {
signed long int n;
cin >> n;
do {
printDigit(n % 10);
n /= 10;
} while (n > 0);
}
void output() {}
int main() {
solve();
output();
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
if (fopen("input.in", "r")) {
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
}
int n;
cin >> n;
long long a[maxn];
a[0] = 0;
for (int i = 1; i <= n; i++) cin >> a[i];
long long d[maxn];
long long sum = 0;
for (int i = 0; i < n; i++) {
d[i] = a[i + 1] - a[i];
sum += d[i] > 0 ? d[i] * (n - a[i + 1] + 1) : -d[i] * a[i + 1];
}
cout << sum << "\n";
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int cpu[101];
int ram[101];
int hdd[101];
int cost[101];
cost[0] = 1000000;
for (int i = 1; i <= n; i++) cin >> cpu[i] >> ram[i] >> hdd[i] >> cost[i];
bool mark[101] = {false};
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (cpu[i] < cpu[j] && ram[i] < ram[j] && hdd[i] < hdd[j]) mark[i] = true;
int Min = 0;
for (int i = 1; i <= n; i++)
if (!mark[i])
if (cost[i] < cost[Min]) Min = i;
cout << Min;
cin.get();
cin.get();
return 0;
}
| 2 |
#include <iostream>
using namespace std;
int main()
{
int n, m, t[110], h[110], ha, ta, a1, a2, best;
bool ok;
while (1)
{
ha=0; ta=0; ok=false; best=500;
cin>>n>>m;
if (n==0 && m==0) break;
for (int i=0; i<n; i++) { cin>>t[i]; ta+=t[i]; }
for (int i=0; i<m; i++) { cin>>h[i]; ha+=h[i]; }
for (int i=0; i<n; i++)
{
for (int j=0; j<m; j++)
{
if (ta-t[i]+h[j]==ha-h[j]+t[i]) { ok=true; if (t[i]+h[j]<best) { a1=t[i]; a2=h[j]; best=a1+a2; } }
}
}
if (!ok) cout<<"-1\n";
else cout<<a1<<' '<<a2<<'\n';
}
return 0;
}
| 0 |
#include<iostream>
#include<algorithm>
using namespace std;
const int MAX = 1001;
int sumj[MAX][MAX],sumo[MAX][MAX], sumi[MAX][MAX];
string data[MAX];
int x,y;
void make(){
for(int i = 1; i <= y; i++){
for(int j = 1; j <= x; j++){
sumj[i][j] = sumj[i-1][j]+sumj[i][j-1]-sumj[i-1][j-1];
sumo[i][j] = sumo[i-1][j]+sumo[i][j-1]-sumo[i-1][j-1];
sumi[i][j] = sumi[i-1][j]+sumi[i][j-1]-sumi[i-1][j-1];
if(data[i][j] == 'J') sumj[i][j]++;
if(data[i][j] == 'O') sumo[i][j]++;
if(data[i][j] == 'I') sumi[i][j]++;
}
}
}
int main(){
cin >> y >> x;
string trash;
int num;
cin >> num;
getline(cin,trash);
for(int i = 1; i <= y; i++){
string str;
getline(cin,str);
data[i] = " ";
data[i] += str;
}
make();
for(int i = 0; i < num; i++){
int po[4];
cin >> po[0] >> po[1] >> po[2] >> po[3];
cout << sumj[po[2]][po[3]]-sumj[po[2]][po[1]-1]-sumj[po[0]-1][po[3]]+sumj[po[0]-1][po[1]-1] << " ";
cout << sumo[po[2]][po[3]]-sumo[po[2]][po[1]-1]-sumo[po[0]-1][po[3]]+sumo[po[0]-1][po[1]-1] << " ";
cout << sumi[po[2]][po[3]]-sumi[po[2]][po[1]-1]-sumi[po[0]-1][po[3]]+sumi[po[0]-1][po[1]-1] <<endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<int, int>;
const double eps = 1e-8;
const ll MOD = 1000000007;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;
template <typename T1, typename T2>
bool chmax(T1 &a, const T2 &b) {
if(a < b) {a = b; return true;}
return false;
}
template <typename T1, typename T2>
bool chmin(T1 &a, const T2 &b) {
if(a > b) {a = b; return true;}
return false;
}
template<typename T1, typename T2>
ostream& operator<<(ostream &os, const pair<T1, T2> p) {
os << p.first << ":" << p.second;
return os;
}
template<class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
for(int i=0;i<((int)(v.size()));++i) {
if(i) os << " ";
os << v[i];
}
return os;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
while(1) {
int n;
cin >> n;
if (n == 0) break;
vector<pair<string, string>> v(n);
for(int i=0;i<(n);++i) {
string s;
cin >> s;
int pos = 0;
for(int j=0;j<((int)(s.size()));++j) {
if (s[j] == '-') {
pos = j;
break;
}
}
v[i].first = s.substr(0, pos);
v[i].second = s.substr(pos + 1, (int)(s.size()) - pos - 1);
}
map<string, int> mp;
int id = 0;
for(int i=0;i<(n);++i) {
if (mp.find(v[i].first) == mp.end()) {
mp[v[i].first] = id;
id++;
}
if (mp.find(v[i].second) == mp.end()) {
mp[v[i].second] = id;
id++;
}
}
vector<P> cross(n);
for(int i=0;i<(n);++i) {
cross[i].first = mp[v[i].first];
cross[i].second = mp[v[i].second];
}
vvi g1(id);
for(int i=0;i<(n);++i) {
g1[cross[i].first].push_back(cross[i].second);
}
map<P, bool> sel1, sel2;
for(int i=0;i<(n);++i) {
for(int j=(i + 1);j<(n);++j) {
if (cross[i].first == cross[j].first) {
sel1[{cross[i].second, cross[j].second}] = true;
sel1[{cross[j].second, cross[i].second}] = true;
}
if (cross[i].second == cross[j].second) {
sel1[{cross[i].first, cross[j].first}] = true;
sel1[{cross[j].first, cross[i].first}] = true;
}
if (cross[i].first == cross[j].second && cross[i].second != cross[j].first) {
sel2[{cross[i].second, cross[j].first}] = true;
sel2[{cross[j].first, cross[i].second}] = true;
}
if (cross[i].second == cross[j].first && cross[i].first != cross[j].second) {
sel2[{cross[i].first, cross[j].second}] = true;
sel2[{cross[j].second, cross[i].first}] = true;
}
}
}
for(int i=0;i<(n);++i) {
sel2[{cross[i].first, cross[i].second}] = true;
sel2[{cross[i].second, cross[i].first}] = true;
}
vvi g2(id);
for(int i=0;i<(id);++i) {
for(int j=0;j<(id);++j) {
if (sel1[{i, j}] && !sel2[{i, j}]) {
g2[i].push_back(j);
}
}
}
cout << id << endl;
int m;
cin >> m;
for(int i=0;i<(m);++i) {
string s;
cin >> s;
int pos = 0;
for(int j=0;j<((int)(s.size()));++j) {
if (s[j] == '-') {
pos = j;
break;
}
}
string x = s.substr(0, pos);
string y = s.substr(pos + 1, (int)(s.size()) - pos - 1);
if (mp.find(x) == mp.end() || mp.find(y) == mp.end()) {
cout << "NO" << endl;
continue;
}
int idx = mp[x];
int idy = mp[y];
queue<P> que;
vvi d(2, vi(id, INF));
d[0][idx] = 0;
que.push({0, idx});
while (!que.empty()) {
int col, now;
tie(col, now) = que.front();
que.pop();
for (auto &nxt: g1[now]) {
if (d[!col][nxt] > d[col][now] + 1) {
d[!col][nxt] = d[col][now] + 1;
que.push({!col, nxt});
}
}
for (auto &nxt: g2[now]) {
if (d[col][nxt] > d[col][now] + 1) {
d[col][nxt] = d[col][now] + 1;
que.push({col, nxt});
}
}
}
if (d[1][idy] != INF) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long a1, a2, b1, b2, c1, c2, d1, d2;
int main() {
cin >> a1 >> a2 >> b1 >> b2 >> c1 >> c2;
for (int q = 0; q < 4; q++) {
if (q == 0) {
d1 = b1 - (a1);
d2 = b2 - (a2);
}
if (q == 1) {
d1 = b1 - (-a1);
d2 = b2 - (-a2);
}
if (q == 2) {
d1 = b1 - (-a2);
d2 = b2 - (a1);
}
if (q == 3) {
d1 = b1 - (a2);
d2 = b2 - (-a1);
}
long long k1 = d1 * c1 + d2 * c2;
long long k2 = c1 * c1 + c2 * c2;
long long k3 = c1 * d2 - c2 * d1;
long long k4 = c1 * c1 + c2 * c2;
if (k2 == 0) {
if (d1 == 0 && d2 == 0) {
cout << "YES";
return 0;
}
} else if (k1 % k2 == 0 && k3 % k4 == 0) {
cout << "YES";
return 0;
}
}
cout << "NO";
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const long long MAXI = 1234567890123456LL;
const long long SQRTMAXI = 32622776;
const long long MOD = 998244353LL;
const long long N = 111;
vector<bool> sieve;
vector<int> primes;
vector<int> factor;
unordered_map<long long, long long> mp;
void init(long long n) {
sieve = vector<bool>(SQRTMAXI, false);
for (long long i = 2; i < SQRTMAXI; i++) {
if (sieve[i] == true) {
continue;
}
for (long long j = 2; i * j < SQRTMAXI; j++) {
sieve[i * j] = true;
}
}
primes.push_back(2);
for (int i = 3; i < SQRTMAXI; i += 2) {
if (sieve[i] == false && n % i == 0) {
primes.push_back(i);
}
}
factor.push_back(1);
for (int i = 1; i < N; i++) {
factor.push_back(1LL * i * factor.back() % MOD);
}
}
long long gcd(long long a, long long b) {
if (a % b == 0) {
return b;
}
return gcd(b, a % b);
}
long long mypow(long long a, long long b) {
long long res = 1;
while (b) {
if (b & 1) {
res = res * a % MOD;
}
a = a * a % MOD;
b >>= 1;
}
return res;
}
long long divmod(long long a) { return mypow(a, MOD - 2) % MOD; }
long long calcpath(long long u) {
const long long key = u;
if (mp.count(key)) {
return mp[key];
}
long long tot = 0;
long long div = 1;
for (auto p : primes) {
if (p > u) {
break;
}
long long cnt = 0;
while (u % p == 0) {
tot++;
cnt++;
u /= p;
}
div = div * divmod(factor[cnt]) % MOD;
}
if (u > 1) {
tot++;
}
long long res = factor[tot] * div % MOD;
return mp[key] = res;
}
long long solve(long long D, long long a, long long b) {
if (a > b) {
swap(a, b);
}
long long g = gcd(a, b);
long long pa = calcpath(a / g);
long long pb = calcpath(b / g);
return pa * pb % MOD;
}
void test() {
init(12);
assert(solve(12, 12, 1) == 3);
assert(solve(12, 4, 4) == 1);
assert(calcpath(12) == 3);
mp.clear();
init(288807105787200LL);
assert(solve(288807105787200LL, 46LL, 482955026400LL) == 547558588LL);
mp.clear();
}
template <class T>
inline void scan_d(T &ret) {
char c;
ret = 0;
while ((c = getchar()) < '0' || c > '9')
;
while (c >= '0' && c <= '9') {
ret = ret * 10 + (c - '0'), c = getchar();
}
}
int main() {
long long D;
scan_d(D);
init(D);
int q;
scan_d(q);
while (q--) {
long long a, b;
scan_d(a);
scan_d(b);
puts(to_string(solve(D, a, b)).c_str());
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
const int MOD = 1e9 + 7;
const int SIZE = 1e6 + 10;
using namespace std;
int main(int argc, char const *argv[]) {
std::ios::sync_with_stdio(false);
cin.tie(0);
long long n, p, j, l, d1 = 0, d2 = 0, fd = 0, presses = 0, flag, foo = -1LL;
string str;
cin >> n >> p >> str;
if (p > n / 2)
flag = -1;
else
flag = 1;
assert(flag == 1 || flag == -1);
std::vector<int> v;
assert(v.size() == 0);
j = n - 1;
for (int i = 0; i < (n); ++i) {
if (i > j) break;
if (str[i] != str[j]) {
presses += min(max((str[j] - str[i]), str[i] - str[j]),
26 - (max((str[j] - str[i]), str[i] - str[j])));
if (flag == 1)
v.push_back(i);
else
v.push_back(j);
}
j--;
}
if (presses == 0) {
cout << "0" << endl;
return 0;
}
p--;
d1 = max((p - v[0]) * flag, foo);
d2 = max((v[v.size() - 1] - p) * flag, foo);
if (d1 == -1)
fd += d2;
else if (d2 == -1)
fd += d1;
else
fd += 2 * min(d1, d2) + max(d1, d2);
fd += presses;
cout << fd << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n, p, s;
int a[55];
long long memo[55][55][55][55];
double fact[55];
long long dp(int x, int sum, int cnt, int forb) {
if (x == n) return (cnt == 0) && (a[forb] > sum);
long long &dev = memo[x][sum][cnt][forb];
if (dev == -1) {
dev = 0LL;
if (sum >= a[x] && x != forb && cnt)
dev += dp(x + 1, sum - a[x], cnt - 1, forb);
dev += dp(x + 1, sum, cnt, forb);
}
return dev;
}
int main() {
fact[0] = 1;
for (int i = 1; i < 55; ++i) fact[i] = fact[i - 1] * (1.0 * i);
while (cin >> n) {
for (int i = 0; i < n; ++i) cin >> a[i];
s = accumulate(a, a + n, 0);
cin >> p;
if (s <= p) {
cout << n << "\n";
continue;
}
memset(memo, -1, sizeof(memo));
double ans = 0;
for (int X = 1; X < n; ++X)
for (int j = 0; j < n; ++j)
ans += fact[X] * fact[n - X - 1] * (1.0 * dp(0, p, X, j)) * X;
printf("%.10lf\n", ans / fact[n]);
}
}
| 4 |
#include <bits/stdc++.h>
const double PI =
3.141592653589793238462643383279502884197169399375105820974944;
using namespace std;
char a[2001][2001];
vector<pair<long long, long long> > dp[2001];
int x, y, r, c, n, m;
bool visited[2001][2001];
void bfs(int r, int c) {
deque<pair<long long, long long> > q;
q.push_front({r, c});
while (!q.empty()) {
pair<long long, long long> xx = q[0];
q.pop_front();
r = xx.first;
c = xx.second;
if (c - 1 >= 0 && a[r][c - 1] == '.' && !visited[r][c - 1] &&
dp[r][c].first + 1 <= x) {
dp[r][c - 1].first = dp[r][c].first + 1;
dp[r][c - 1].second = dp[r][c].second;
visited[r][c - 1] = true;
q.push_back({r, c - 1});
}
if (c + 1 < m && a[r][c + 1] == '.' && !visited[r][c + 1] &&
dp[r][c].second + 1 <= y) {
dp[r][c + 1].first = dp[r][c].first;
dp[r][c + 1].second = dp[r][c].second + 1;
visited[r][c + 1] = true;
q.push_back({r, c + 1});
}
if (r - 1 >= 0 && a[r - 1][c] == '.' && !visited[r - 1][c]) {
dp[r - 1][c] = dp[r][c];
visited[r - 1][c] = true;
q.push_front({r - 1, c});
}
if (r + 1 < n && a[r + 1][c] == '.' && !visited[r + 1][c]) {
dp[r + 1][c] = dp[r][c];
visited[r + 1][c] = true;
q.push_front({r + 1, c});
}
}
}
void solve() {
cin >> n >> m;
cin >> r >> c;
cin >> x >> y;
for (long long i = 0; i < n; ++i) {
for (long long j = 0; j < m; ++j) {
dp[i].resize(2001);
cin >> a[i][j];
}
}
visited[r - 1][c - 1] = true;
bfs(r - 1, c - 1);
int ans = 0;
for (long long i = 0; i < n; ++i) {
for (long long j = 0; j < m; ++j) {
ans += (visited[i][j] == true);
}
}
cout << ans << "\n";
}
int main() {
bool testing = false;
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if (testing) {
freopen("test.txt", "rt", stdin);
int start = clock();
solve();
int end = clock();
cout << "time: " << (end - start) / (double)(CLOCKS_PER_SEC)*1000
<< " milliseconds\n";
} else {
solve();
}
}
| 2 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
long long n, m;
cin >> n >> m;
long long a[n][m];
vector<long long> mn(n, INT_MAX);
vector<long long> mx(m, 0);
for (long long i = 0; i < n; i++) {
for (long long j = 0; j < m; j++) {
cin >> a[i][j];
mn[i] = min(mn[i], a[i][j]);
mx[j] = min(mx[j], a[i][j]);
}
}
cout << *max_element(mn.begin(), mn.end()) << '\n';
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
#define C continue;
#define R return
#define int long long
#define D double
#define I insert
#define ull unsigned long long
#define ui unsigned int
#define ld long double
#define pb emplace_back
#define pf push_front
#define F first
#define S second
#define mp make_pair
#define vi vector<int>
#define vc vector<char>
#define vs vector<string>
#define vb vector<bool>
#define vd vector<D>
#define vull vector<ull>
#define vld vector<ld>
#define vpld vector<pld>
#define vpii vector<pii>
#define vpDD vector<pDD>
#define all(v) v.begin(), v.end()
#define allcomp(v) v.begin(), v.end(), comp
#define pii pair<int, int>
#define pld pair<ld, ld>
#define pDD pair<D, D>
#define PQ(type) priority_queue<type>
#define PQD(type) priority_queue<type, vector<type>, greater<type>>
#define mii map<int, int>
#define mci map<char, int>
#define msi map<string, int>
#define mis map<int, string>
#define mib map<int, bool>
#define mcb map<char, bool>
#define umii unordered_map<int, int>
#define umib unordered_map<int, bool>
#define umci unordered_map<char, int>
#define umsi unordered_map<string, int>
#define umis unordered_map<int, string>
#define init(arr, val) memset(arr, val, sizeof(arr))
#define rep(i, a, b) for (int i = a; i < b; i++)
#define distance(a, b, p, q) sqrt((p - a) * (p - a) + (q - b) * (q - b))
#define pp(n) printf("%.10Lf", n);
#define nl "\n"
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
string vow = "aeiou";
int month[] = { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int dxhorse[] = { -2, -2, -1, -1, 1, 1, 2, 2};
const int dyhorse[] = {1, -1, 2, -2, 2, -2, 1, -1};
const int dx4[] = { -1, 0, 1, 0};
const int dy4[] = {0, 1, 0, -1};
const int dx8[] = { -1, -1, -1, 0, 0, 1, 1, 1};
const int dy8[] = { -1, 0, 1, -1, 1, -1, 0, 1};
const ld pie = 3.1415926535897932384626;
const int special_prime = 982451653l; // The 50,000,000th prime number
const int mod = 1e9 + 7;
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
/// Tip : If a and b are positive integers ; we may say - ceil (a/b) = 1 + floor ( (a-1)/b )
///DEBUGGER
void __print(int x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x)
{
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T>
void __print(const T &x)
{
int f = 0;
cerr << '{';
for (auto &i : x)
cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v)
{
__print(t);
if (sizeof...(v))
cerr << ", ";
_print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...) \
cerr << "[" << #x << "] = ["; \
_print(x)
#else
#define debug(x...)
#endif
//VECTOR
template <class T>
istream &operator>>(istream &in, vector<T> &a)
{
for (auto &i : a)
cin >> i;
return in;
}
template <class T>
ostream &operator<<(ostream &out, const vector<T> &a)
{
for (auto &i : a)
cout << i << " ";
return out;
}
//MAP
template <class T, class U>
ostream &operator<<(ostream &out, const map<T, U> &a)
{
for (auto &i : a)
cout << "(" << i.first << ", " << i.second << ")\n";
return out;
}
template <class T, class U>
ostream &operator<<(ostream &out, const unordered_map<T, U> &a)
{
for (auto &i : a)
cout << "(" << i.first << ", " << i.second << ")\n";
return out;
}
bool comp(int a, int b)
{
return a > b;
}
void sieve(vi &prime)
{
prime[0] = false;
prime[1] = false;
for (int p = 2; p * p <= 99999; p++)
{
if (prime[p] == true)
{
for (int i = p * p; i <= 99999; i += p)
prime[i] = false;
}
}
}
/* CODE STARTS FROM HERE */
void solve()
{
int n; cin >> n;
int ans = 0;
int k = 3;
int d = k * k + 1;
while (d <= 2 * n)
{
ans++;
k += 2;
d = k * k + 1;
}
cout << ans << nl;
}
int32_t main()
{
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #
fast
int t = 1;
cin >> t;
// cin>>ws;
//vi prime(99999, true);
//sieve(prime);
for (int i = 1; i <= t; i++)
solve();
} | 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
long long k = 0, kol1 = 0;
cin >> k;
cin >> s;
if (!(s[0] == ')') and !(s.size() >= 3 and (s[s.size() - 2] == '('))) {
vector<int> ar(s.size(), 0);
long long kolx = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '(') {
ar[i] = 1;
kol1++;
}
if (s[i] == ')') {
ar[i] = -1;
kol1--;
}
if (s[i] == '?') {
kolx++;
}
}
if (s[0] == '?') {
ar[0] = 1;
kol1++;
kolx--;
}
for (int i = 0; i < s.size(); i++) {
if (ar[i] == 0) {
if (kolx == kol1) {
ar[i] = -1;
kolx--;
kol1--;
} else {
ar[i] = 1;
kol1++;
kolx--;
}
}
}
bool b1 = true;
string ans = "";
kol1 = 0;
for (int i = 0; i < s.size(); i++) {
if (ar[i] == 1) {
ans += '(';
kol1++;
} else {
ans += ')';
kol1--;
}
if (i != s.size() - 1 and kol1 <= 0) {
b1 = false;
}
}
if (!kol1 and b1) {
cout << ans;
} else {
cout << ":(";
}
} else {
cout << ":(";
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2048;
int v[N];
int ss[N];
int vv(char c) { return c == 'W' ? 0 : c == 'R' ? 1 : c == 'Y' ? 2 : 3; }
bitset<N> a[N];
bool ha[N] = {false};
void gg() {
puts("NO");
exit(0);
}
int n, k;
void ins(bitset<N> &b) {
for (int i = 2 * n; i >= 0; i--) {
if (b[i]) {
if (ha[i]) {
b ^= a[i];
} else {
ha[i] = true;
a[i] = b;
if (i == 0) {
gg();
}
return;
}
}
}
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
ss[2 * i] = 1;
ss[2 * i + 1] = 2;
}
while (k--) {
char op[10];
scanf("%s", op);
int cnt;
scanf("%d", &cnt);
for (int i = 0; i < cnt; i++) {
scanf("%d", &v[i]);
}
if (op[0] == 'm') {
scanf("%s", op);
int vl = vv(op[0]);
bitset<N> b[2];
for (int i = 0; i < cnt; i++) {
int x = v[i];
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
b[j][2 * x - 1 + k] = (ss[2 * x + j] >> k) & 1;
}
}
}
for (int i = 0; i < 2; i++) {
b[i][0] = (vl >> i) & 1;
ins(b[i]);
}
} else if (op[0] == 'Y') {
for (int i = 0; i < cnt; i++) {
int x = v[i];
ss[2 * x] ^= ss[2 * x + 1];
}
} else if (op[1] == 'Y') {
for (int i = 0; i < cnt; i++) {
int x = v[i];
swap(ss[2 * x], ss[2 * x + 1]);
}
} else {
for (int i = 0; i < cnt; i++) {
int x = v[i];
ss[2 * x + 1] ^= ss[2 * x];
}
}
}
puts("YES");
bitset<N> ans;
ans[0] = true;
for (int i = 1; i <= 2 * n; i++) {
if (ha[i]) {
ans[i] = (a[i] & ans).count() & 1;
} else {
ans[i] = 0;
}
}
for (int i = 1; i <= n; i++) {
putchar(".RYB"[ans[2 * i - 1] + ans[2 * i] * 2]);
}
printf("\n");
return 0;
}
| 6 |
#include <cstdio>
int w[] = { 2, 3, 5, 10, 12, 15 };
int v[] = { 380, 550, 850, 1520, 1870, 2244 };
int n = 6;
#define M 50
#define INF 10000000
#define min(x, y) ((x) < (y) ? (x) : (y))
int dp[M+10];
int main()
{
for (int i = 0; i <= M; ++i) dp[i] = INF;
for (int i = 0; i < n; ++i) dp[w[i]] = v[i];
for (int i = 6; i <= M; ++i)
for (int j = 0; j < n; ++j)
for (int k = 1; k * w[j] <= i; ++k)
dp[i] = min(dp[i], dp[i-k*w[j]] + k*v[j]);
int m;
while (scanf("%d", &m), m) printf("%d\n", dp[m/100]);
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long q, a, z;
cin >> q;
while (q--) {
cin >> a;
z = a % 4;
if (z == 0) {
cout << a / 4 << "\n";
} else if (z == 1 && a >= 9) {
cout << a / 4 - 1 << "\n";
} else if (z == 2 && a >= 6) {
cout << a / 4 << "\n";
} else if (z == 3 && a >= 15) {
cout << a / 4 - 1 << "\n";
} else
cout << "-1\n";
}
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
long long t = 1;
while (t--) {
solve();
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 5005;
vector<int> cand[maxn], repl[maxn];
int n, m;
int t[maxn];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int ai, bi;
cin >> ai >> bi;
cand[ai].push_back(bi);
}
for (int i = 1; i <= n; i++) {
if (cand[i].empty()) {
t[i] = 0;
continue;
} else {
int ret = i;
int dist = n + 1;
for (int j : cand[i]) {
int d = j > i ? j - i : n - (i - j);
if (d < dist) {
dist = d;
ret = i;
}
}
t[i] = ((int)cand[i].size() - 1) * n + dist;
}
}
for (int i = 1; i <= n; i++) {
vector<int> times;
int mx = 0;
for (int j = 0; j < n; j++) {
int idx = i + j <= n ? i + j : i + j - n;
if (t[idx] > 0) times.push_back(t[idx] + j);
}
for (int j : times) {
mx = max(mx, j);
}
cout << mx << '\n';
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n;
long long x[100005], y[100005];
bool solve(int pos) {
long long temp = x[pos + 1];
for (int i = sqrt(temp); i >= 1; i--) {
if (temp % i || (i + temp / i) % 2) continue;
long long tx = (i + temp / i) / 2, ty = (temp / i - i) / 2;
if (ty > y[pos - 1]) {
y[pos] = ty;
y[pos + 1] = tx;
return true;
}
}
return false;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n / 2; i++) scanf("%lld", &x[i * 2]);
for (long long i = 1; i <= n; i += 2)
if (!solve(i)) {
printf("No\n");
return 0;
}
for (int i = 1; i <= n; i++) x[i] = y[i] * y[i] - y[i - 1] * y[i - 1];
printf("Yes\n");
for (int i = 1; i <= n; i++) printf("%lld ", x[i]);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main(void) {
ios::sync_with_stdio(false);
cin.tie(NULL);
int n, k, t, pre = 0, buf = INT_MAX, bef = 0;
cin >> n >> k;
bool flag = false, fz = false;
for (int i = 0; i < n; i++) {
cin >> t;
if (fz) {
fz = false;
buf = t;
}
if (k == 1 && !t) fz = true, bef = pre;
if (!t) continue;
if (i && t < pre) flag = true;
pre = t;
}
if (k == 1) cin >> n;
if (flag || k > 1 || (k == 1 && (n > buf || n < bef)))
cout << "Yes\n";
else
cout << "No\n";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
void print(list<int> *gr, int n) {
for (int i = 1; i < n; ++i) {
cout << i << "->";
for (auto nbr : gr[i]) {
cout << nbr << " ";
}
cout << endl;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, t;
cin >> n >> t;
if (t == 10) {
if (n == 1) {
cout << -1 << endl;
return 0;
} else {
cout << 1;
while (--n) {
cout << 0;
}
return 0;
}
}
while (n--) {
cout << t;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
register int x = 0, f = 1;
register char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = 0;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + (ch ^ '0');
ch = getchar();
}
return f ? x : -x;
}
int n, k, f[80][80], w[80][80], c[80][80], ans, vis[80];
struct node {
int a, b, id;
} p[80];
vector<int> C[80][80], ansp;
int main() {
for (int T = (1), _ed = (read()); T <= _ed; ++T) {
memset(vis, 0, sizeof vis);
n = read(), k = read();
ans = 0;
ansp.clear();
for (int i = (1), _ed = (n); i <= _ed; ++i)
p[i] = (node){read(), read(), i};
sort(p + 1, p + n + 1, [&](node x, node y) { return x.b < y.b; });
for (int i = (1), _ed = (n); i <= _ed; ++i)
for (int j = (1), _ed = (k - 1); j <= _ed; ++j)
w[i][j] = -(k - j) * p[i].b + p[i].a;
for (int l = (1), _ed = (n); l <= _ed; ++l) {
memset(f, -0x3f, sizeof f);
f[0][0] = 0, C[0][0].clear();
int t = 0;
for (int i = (1), _ed = (n); i <= _ed; ++i) {
if (i == l) continue;
++t;
for (int j = (0), _ed = (min(t, k - 1)); j <= _ed; ++j) {
f[t][j] = f[t - 1][j];
C[t][j] = C[t - 1][j];
if (j) {
int tmp = f[t - 1][j - 1] + w[i][j];
if (f[t][j] < tmp) {
f[t][j] = tmp;
C[t][j] = C[t - 1][j - 1];
C[t][j].push_back(p[i].id);
}
}
}
}
int res = f[t][k - 1];
for (int i = (1), _ed = (n); i <= _ed; ++i) res += (k - 1) * p[i].b;
res += p[l].a;
C[t][k - 1].push_back(p[l].id);
if (ans < res) {
ans = res;
ansp = C[t][k - 1];
}
}
int m = (int)(ansp).size();
printf("%d\n", n + n - m);
for (int i = (0), _ed = (m - 2); i <= _ed; ++i)
printf("%d ", ansp[i]), vis[ansp[i]] = 1;
vis[ansp.back()] = 1;
for (int i = (1), _ed = (n); i <= _ed; ++i)
if (!vis[i]) printf("%d %d ", i, -i);
printf("%d\n", ansp.back());
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int ara[32];
for (int i = 0; i <= 30; i++) {
ara[i] = pow(2, i) - 1;
}
int x = n;
vector<int> v;
int f = 0;
int cnt = 0;
for (int i = 0;; i++) {
for (int j = 0; j <= 30; j++) {
if (x == ara[j]) {
f = 1;
break;
}
}
if (f == 1) {
break;
}
if (i % 2 == 0) {
int g = log(x) / log(2);
for (int j = g; j >= 0; j--) {
int e = pow(2, j) - 1;
int m = x ^ e;
if (m > x) {
x = m;
v.push_back(j);
break;
}
}
} else {
x++;
}
cnt++;
}
cout << cnt << endl;
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
set<pair<int, int> > s;
int i, n, m, k, t, a, b;
cin >> n >> m >> k >> t;
vector<int> v(t + 1);
for (i = 0; i < k; i++) {
scanf("%d%d", &a, &b);
s.insert(make_pair((a - 1) * m + b, 0));
}
for (i = 0; i < t; i++) {
scanf("%d%d", &a, &b);
if (s.count(make_pair((a - 1) * m + b, 0)))
v[i + 1] = 3;
else
s.insert(make_pair((a - 1) * m + b, i + 1));
}
m = 0;
for (set<pair<int, int> >::iterator it = s.begin(); it != s.end(); it++)
if (it->second != 0)
v[it->second] = (it->first - m) % 3;
else
m++;
for (i = 1; i <= t; i++)
if (v[i] == 0)
printf("Grapes\n");
else if (v[i] == 1)
printf("Carrots\n");
else if (v[i] == 2)
printf("Kiwis\n");
else
printf("Waste\n");
return 0;
}
| 2 |
#include <bits/stdc++.h>
int main() {
int i, n, m, count = 0;
scanf("%d%d", &n, &m);
while (n > 0 && m > 0) {
if (n > m && n >= 2) {
n -= 2;
m--;
count++;
} else if (m > n && m >= 2) {
m -= 2;
n--;
count++;
} else if (n == m && m >= 2) {
m -= 2;
n--;
count++;
} else
break;
}
printf("%d", count);
return 0;
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=998244353;
const int N=2e5+10;
ll f[N];
ll invf[N];
ll invmod(int a,int b)
{
if(b==0||b==1)
return a;
ll k=invmod(a,b/2);
k=(k*k)%mod;
if(b%2)
k=(k*a)%mod;
return k;
}
ll solve(int a, int b)
{
ll k=(f[a]*invf[b])%mod;
k=(k*invf[a-b])%mod;
return k;
}
void init()
{
f[0]=1;
for(ll i=1;i<N;i++)
{
f[i]=(f[i-1]*i)%mod;
}
invf[200000]=invmod(f[200000],mod-2);
for(ll i=199999;i>=0;i--)
{
invf[i]=(invf[i+1]*(i+1))%mod;
}
}
int main()
{
int t;
cin>>t;
init();
while(t--)
{
ll n;
cin>>n;
string s;
cin>>s;
ll zero=0,one=0;
for(int i=0;i<n;i++)
{
if(s[i]=='0') zero++;
if(i>0&&s[i]=='1'&&s[i-1]=='1')
{
s[i]='0';
one++;
}
}
ll ans=solve(zero+one,one)%mod;
cout<<ans<<endl;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n;
vector<char> g[100500];
void say() {
cout << "NO";
exit(0);
}
void update(int x) {
if (g[x].size() < g[x - 1].size())
say();
else if (g[x].size() > g[x - 1].size()) {
for (int i = 0; i < g[x].size(); i++) {
if (g[x][i] == '?') {
if (i == 0)
g[x][i] = '1';
else
g[x][i] = '0';
}
}
} else {
bool more = 0;
int last = -1;
vector<int> ques;
for (int i = 0; i < g[x].size(); i++) {
if (g[x][i] != '?') {
if (g[x - 1][i] > g[x][i]) {
if (!more) {
if (last == -1) say();
g[x][last]++;
for (int j = 0; j < ques.size(); j++) {
if (ques[j] > last) {
g[x][ques[j]] = '0';
}
}
more = 1;
}
} else if (g[x][i] > g[x - 1][i]) {
more = 1;
}
} else {
ques.push_back(i);
if (more) {
g[x][i] = '0';
last = i;
} else {
g[x][i] = g[x - 1][i];
if (g[x][i] != '9') last = i;
}
}
}
if (!more) {
if (last == -1)
say();
else {
g[x][last]++;
for (int j = 0; j < ques.size(); j++) {
if (ques[j] > last) {
g[x][ques[j]] = '0';
}
}
}
}
}
}
int main() {
cin >> n;
string s;
getline(cin, s);
for (int i = 1; i <= n; i++) {
getline(cin, s);
for (int j = 0; j < s.length(); j++) {
g[i].push_back(s[j]);
}
}
for (int i = 0; i < g[1].size(); i++) {
if (g[1][i] == '?') {
if (i == 0)
g[1][i] = '1';
else
g[1][i] = '0';
}
}
for (int j = 2; j <= n; j++) update(j);
cout << "YES\n";
for (int i = 1; i <= n; i++) {
for (int j = 0; j < g[i].size(); j++) {
cout << g[i][j];
}
cout << "\n";
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
double a, d;
int n;
int ar[4][2];
double v[4][2];
memset(ar, 0, sizeof(ar));
while (cin >> a >> d >> n) {
ar[0][1] = 1, v[0][0] = a, v[0][1] = 0;
ar[1][0] = 1, v[1][0] = a, v[1][1] = a;
ar[2][1] = 1, v[2][0] = 0, v[2][1] = a;
ar[3][0] = 1;
v[3][0] = 0, v[3][1] = 0;
double sum = 0;
double a1 = a * 4;
for (int i = 0; i < n; ++i) {
sum += d;
double x = (sum / a);
long long x1 = (long long)floor(x);
x -= (double)x1;
int mx = (int)(sum / a1);
sum -= mx * a1;
x1 %= 4;
for (int i = 0; i < 2; ++i) {
if (i) printf(" ");
if (x1 <= 1) {
if (ar[x1][i])
printf("%lf", v[x1][i]);
else
printf("%lf", x * a);
} else {
if (ar[x1][i])
printf("%lf", v[x1][i]);
else
printf("%lf", a - x * a);
}
}
printf("\n");
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-6;
double sqrt3 = sqrt(3.0);
long long N, ans, pos, inicio, fin, pp;
double dis(double x, double y) { return x * x + y * y; }
bool sePuede(double x, double y) {
if (dis(x, y + 1) - eps > N * N) return 0;
if (dis(x, y - 1) - eps > N * N) return 0;
if (dis(x + sqrt3 / 2, y + 0.5) - eps > N * N) return 0;
if (dis(x + sqrt3 / 2, y - 0.5) - eps > N * N) return 0;
if (dis(x - sqrt3 / 2, y + 0.5) - eps > N * N) return 0;
if (dis(x - sqrt3 / 2, y - 0.5) - eps > N * N) return 0;
return 1;
}
int main() {
cin >> N;
inicio = 1;
fin = 3000000;
while (inicio <= fin) {
long long mitad = (inicio + fin) / 2;
if (sePuede((mitad - 1) * sqrt3, 0)) {
pos = mitad;
inicio = mitad + 1;
} else
fin = mitad - 1;
}
long long cnt = 0;
double x = 0;
double y = -3;
while (1) {
if (!sePuede(x, y)) break;
inicio = 1;
fin = 3000000;
while (inicio <= fin) {
long long mitad = (inicio + fin) / 2;
if (sePuede(x - (mitad - 1) * sqrt3 / 2, y - (mitad - 1) * 1.5)) {
pp = mitad;
inicio = mitad + 1;
} else
fin = mitad - 1;
}
pp = 1 + (pp - 1) * 2;
cnt = cnt + pp;
y = y - 3;
}
ans = 6 * (pos - 1) + 1 + 6 * cnt;
cout << ans << "\n";
return 0;
}
| 4 |
#include"bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define FOR(k,m,n) for(ll (k)=(m);(k)<(n);(k)++)
#define REP(i,n) FOR((i),0,(n))
#define WAITING(str) int str;std::cin>>str;
#define DEBUGING(str) cout<< #str << " " str<<endl
constexpr int INF = (1 << 30);
constexpr ll INFL = (1ll << 62);
constexpr ll MOD = 1000000007;// 10^9+7
string calc(const vector<set<string>>& level, stack<string>& sent) {
for (auto ops : level) {
stack<string> res;
while (!sent.empty()) {
string top = sent.top(); sent.pop();
if (ops.find(top) == ops.end()) {
res.push(top);
}
else {
ll l = stoll(res.top()); res.pop();
ll r = stoll(sent.top()); sent.pop();
if (top == "+")res.push(to_string(l + r));
if (top == "-")res.push(to_string(l - r));
if (top == "*")res.push(to_string(l * r));
}
}
while (!res.empty()) {
sent.push(res.top());
res.pop();
}
}
if (sent.size() != 1)exit(1);
return sent.top();
}
vector<string> separater(string q) {
vector<string> res;
string now;
for (char c : q) {
if (isalnum(c)) {
now += c;
}
else {
if (!now.empty()) {
res.push_back(now);
now = "";
}
res.push_back({ c });
}
}
if (!now.empty())res.push_back(now);
return res;
}
ll solve(vector<string> vs, vector<set<string>>& level) {
stack<string> st;
for (string s : vs) {
if (s == ")") {
stack<string> sent;
while (true) {
string top = st.top(); st.pop();
if (top == "(")break;
sent.push(top);
}
st.push(calc(level, sent));
}
else {
st.push(s);
}
}
stack<string> op;
while (!st.empty()) {
op.push(st.top());
st.pop();
}
return stoll(calc(level, op));
}
int main()
{
string s;
cin >> s;
auto vs = separater(s);
vector<vector<set<string>>> levels = {
// 1
{{"+"},{"-"},{"*"}},
{{"+"},{"*"},{"-"}},
{{"-"},{"+"},{"*"}},
{{"-"},{"*"},{"+"}},
{{"*"},{"+"},{"-"}},
{{"*"},{"-"},{"+"}},
// 2
{{"+"},{"-","*"}},
{{"-"},{"+","*"}},
{{"*"},{"+","-"}},
{{"+","-"},{"*"}},
{{"+","*"},{"-"}},
{{"-","*"},{"+"}},
// 3
{{"+","-","*"}},
};
ll res = solve(vs, levels.front());
FOR(i, 1, levels.size()) {
res = max(res, solve(vs, levels[i]));
}
cout << res << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ll x ;cin>>x;
ll ans = x/11ll * 2ll;
x%=11ll;
if( x )
ans += ((x>6)?2ll:1ll);
cout<< ans <<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long a[100010], on[100010], off[100010], n, M;
int main(int argc, char *argv[]) {
while (cin >> n >> M) {
a[0] = 0;
a[n + 1] = M;
for (long long i = 1; i <= n + 1; i++) {
if (i < n + 1) cin >> a[i];
if (i % 2) {
on[i] = on[i - 1] + a[i] - a[i - 1];
off[i] = off[i - 1];
} else {
on[i] = on[i - 1];
off[i] = off[i - 1] + a[i] - a[i - 1];
}
}
long long ans = on[n + 1], temp = 0;
for (long long i = 1; i <= n; i++) {
if (a[i] - a[i - 1] > 1 || a[i + 1] - a[i] > 1) {
if (i % 2) {
temp = on[i] - 1 + off[n + 1] - off[i];
} else {
temp = on[i] + 1 + off[n + 1] - off[i];
}
}
ans = max(ans, temp);
}
cout << ans << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
bool is_prm(int a) {
for (int i = 2; i < a; i++) {
if (a % i == 0) return 0;
}
return 1;
}
int main() {
long long c;
int n, i, j, k, l, r, len;
int d, M, mx, p, B, test;
char s[111];
vector<long long> a;
vector<int> I;
vector<int> lng(30, 0);
vector<int> tmp(30, 0);
vector<int> pow(30, 1);
vector<int> prm;
vector<int> dyna[17][4];
vector<int> MOD = {300690390, 1808691197, 2044926619, 2073080791};
for (i = 2; i < 100; i++) {
if (is_prm(i)) {
prm.emplace_back(i);
}
}
I.resize(prm.size());
for (i = 0; i < prm.size(); i++) {
for (j = 0; j < MOD.size(); j++) {
if (MOD[j] % prm[i] == 0) {
I[i] = j;
break;
}
}
}
scanf("%d", &n);
a.resize(n);
for (auto& r : a) scanf("%I64d", &r);
for (l = 2; l < 17; l++) {
for (r = mx = 1, B = l + 1; r < n; r *= l, mx *= B, lng[l]++)
;
for (i = 1; i < lng[l]; i++) {
pow[i] = pow[i - 1] * B;
}
for (i = 0; i < 4; i++) {
dyna[l][i].resize(mx + 1);
}
for (i = 0; i < mx; i++) {
for (j = 0, M = i, p = -1; j < lng[l]; j++) {
tmp[j] = M % B;
M /= B;
if (tmp[j] == l) p = j;
}
for (j = 0; j < 4; j++) {
dyna[l][j][i] = 1;
}
if (~p) {
for (j = 0; j < 4; j++) {
for (k = 1; k <= l; k++) {
dyna[l][j][i] =
1ll * dyna[l][j][i] * dyna[l][j][i - k * pow[p]] % MOD[j];
}
}
} else {
for (j = lng[l], M = 0; j--;) {
M = M * l + tmp[j];
}
if (M < n) {
for (j = 0; j < 4; j++) {
dyna[l][j][i] = a[M] % MOD[j];
}
}
}
}
}
scanf("%d", &test);
while (test--) {
scanf("%d%s%I64d", &d, &s, &c);
len = strlen(s);
for (i = max(0, len - lng[d]), mx = 0; i < len; i++) {
if (s[i] == '?')
j = d;
else if (isalpha(s[i]))
j = s[i] - 'A' + 10;
else
j = s[i] - '0';
mx = mx * (d + 1) + j;
}
for (i = 0, k = -1; i < prm.size() && !~k; i++) {
if (!((c + dyna[d][I[i]][mx]) % prm[i])) {
k = prm[i];
}
}
printf("%d\n", k);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
double s[100020], r[100020];
int n;
double k;
bool check(double x) {
double tol = 0;
for (int i = 1; i <= n; i++) {
if (r[i] < x * s[i]) {
tol += x * s[i] - r[i];
}
}
if (tol > x * k)
return false;
else
return true;
}
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> s[i] >> r[i];
}
double ss = 0, rr = 0;
for (int i = 1; i <= n; i++) {
ss += s[i];
rr += r[i];
}
if (ss <= k)
cout << "-1" << endl;
else {
double l = 0, r = 100000000000, mid;
for (int i = 1; i <= 70; i++) {
mid = (l + r) / 2.0;
if (!check(mid)) {
r = mid;
} else
l = mid;
}
printf("%.4lf", l);
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int avl, ara[35004], lst[35004], seg[40 * 35004], lft[40 * 35004],
rght[40 * 35004], version[35004], suf[35004];
int update(int n, int s, int e, int indx, int val) {
if (s == indx && e == indx) {
seg[++avl] = seg[n] + val;
return avl;
}
if (s > indx || e < indx) return n;
int mid = (s + e) >> 1;
int x = ++avl;
lft[x] = update(lft[n], s, mid, indx, val);
rght[x] = update(rght[n], mid + 1, e, indx, val);
seg[x] = seg[lft[x]] + seg[rght[x]];
return x;
}
int query(int n, int s, int e, int l, int r) {
if (s >= l && e <= r) return seg[n];
if (s > r || e < l) return 0;
int mid = (s + e) >> 1;
return query(lft[n], s, mid, l, r) + query(rght[n], mid + 1, e, l, r);
}
int mem[52][35004];
void dp(int n, int rem, int l, int r, int p1, int p2) {
if (l > r) return;
int mid = (l + r) >> 1;
mem[rem][mid] = 0;
int ind = p1, c = 0;
if (p2 < mid) c = query(version[mid], 1, n, p2 + 1, mid);
for (int i = min(mid, p2); i >= p1; i--) {
if (suf[i] > mid) c++;
int x = mem[rem - 1][i - 1] + c;
if (x >= mem[rem][mid]) mem[rem][mid] = x, ind = i;
}
dp(n, rem, l, mid - 1, p1, ind);
dp(n, rem, mid + 1, r, ind, p2);
return;
}
int main() {
int n, k;
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &ara[i]);
mem[1][i] = mem[1][i - 1];
version[i] = update(version[i - 1], 1, n, i, 1);
if (lst[ara[i]])
version[i] = update(version[i], 1, n, lst[ara[i]], -1);
else
mem[1][i]++;
lst[ara[i]] = i;
}
for (int i = 1; i <= n; i++) lst[i] = n + 1;
for (int i = n; i >= 1; i--) suf[i] = lst[ara[i]], lst[ara[i]] = i;
for (int i = 2; i <= k; i++) dp(n, i, 1, n, 1, n);
printf("%d\n", mem[k][n]);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
vector<long long> mar;
long long n, k;
map<long long, long long> br;
long long wy(long long tp) {
long long bst = k + 1;
br.clear();
for (long long i = 0; i < (n); ++i) {
for (long long j = 0; j < (k + 1); ++j) {
if (j * mar[i] > tp) continue;
if (br.find(tp - j * mar[i]) == br.end())
br[tp - j * mar[i]] = j;
else
br[tp - j * mar[i]] = min(br[tp - j * mar[i]], j);
}
}
for (long long i = 0; i < (n); ++i) {
for (long long j = 0; j < (k + 1); ++j) {
if (j * mar[i] > tp) continue;
if (br.find(j * mar[i]) == br.end()) continue;
bst = min(bst, j + br[j * mar[i]]);
}
}
if (bst == k + 1) return -1;
return bst;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
mar.resize(n);
for (long long i = 0; i < (n); ++i) cin >> mar[i];
long long q;
cin >> q;
long long tp;
for (long long z = 0; z < (q); ++z) {
cin >> tp;
cout << wy(tp) << "\n";
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long ans[500010], vis[500010];
struct tp {
long long min, idx;
} tree[500010 * 3];
tp combine(tp a, tp b) { return a.min < b.min ? a : b; }
long long arr[500010];
void build(long long at, long long left, long long right) {
if (left == right) {
tree[at].min = arr[left];
tree[at].idx = left;
return;
}
build(at * 2, left, (left + right) / 2);
build(at * 2 + 1, (left + right) / 2 + 1, right);
tree[at] = combine(tree[at * 2], tree[at * 2 + 1]);
}
tp query(long long at, long long left, long long right, long long start,
long long end) {
if (start > right || end < left) return {1 << 30, -1};
if (left >= start && right <= end) {
return tree[at];
}
long long m = (left + right) / 2;
tp x = combine(query(at * 2, left, m, start, end),
query(at * 2 + 1, m + 1, right, start, end));
return x;
}
long long cal(long long l, long long r, long long n) {
if (l == r) {
ans[l] = arr[l];
return arr[l];
}
long long x = query(1, 1, n, l, r).idx;
ans[x] = arr[x];
if (x == l) {
return arr[x] + cal(l + 1, r, n);
}
if (x == r) {
return arr[x] + cal(l, r - 1, n);
}
long long left_Sum = (x - l + 1) * arr[x];
long long right_Sum = (r - x + 1) * arr[x];
long long left = cal(l, x - 1, n);
long long right = cal(x + 1, r, n);
if (left + right_Sum > right + left_Sum) {
for (long long i = x + 1; i <= r; ++i) {
ans[i] = arr[x];
}
return left + right_Sum;
} else {
for (long long i = l; i < x; ++i) {
ans[i] = arr[x];
}
return right + left_Sum;
}
}
int main() {
long long n;
cin >> n;
for (long long i = 1; i <= n; ++i) cin >> arr[i];
build(1, 1, n);
cal(1, n, n);
for (long long i = 1; i <= n; ++i) {
cout << ans[i] << " ";
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int sc = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') sc = sc * 10 + ch - '0', ch = getchar();
return sc * f;
}
int main() {
int n = read();
if (n % 4 > 1) return puts("NO"), 0;
puts("YES");
for (int i = n % 4; i < n; i += 4) {
for (int j = 0; j < i; j++) printf("%d %d\n", j + 1, i + 1);
printf("%d %d\n", i + 1, i + 2);
for (int j = i - 1; ~j; j--) printf("%d %d\n", j + 1, i + 2);
for (int j = 0; j < i; j++) printf("%d %d\n", j + 1, i + 3);
printf("%d %d\n", i + 3, i + 4);
for (int j = i - 1; ~j; j--) printf("%d %d\n", j + 1, i + 4);
printf("%d %d\n", i + 1, i + 4);
printf("%d %d\n", i + 2, i + 3);
printf("%d %d\n", i + 1, i + 3);
printf("%d %d\n", i + 2, i + 4);
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main(){
long long n;
cin >> n;
for(int i = sqrt(n); i>=0; i--) {
if(!(n % i)) {
cout << i-1+(n/i-1);
break;
}
}
} | 0 |
#include <bits/stdc++.h>
const bool unsyncedio = std::ios::sync_with_stdio(false);
using namespace std;
int main() {
int n, z;
cin >> n >> z;
vector<int> x(n);
vector<int> id(n);
for (long long i = 0; i < (n); i++) {
cin >> x[i];
id[i] = i;
}
sort(x.begin(), x.end());
int lidx = n / 2;
int i = 0;
int ans = 0;
while (lidx < n && i < n / 2) {
while (lidx < n && x[lidx] - x[i] < z) lidx++;
if (lidx >= n) break;
lidx++;
i++;
ans++;
}
cout << ans << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
void f(long long a, long long b, bool bl) {
if (b == 1) {
printf("%I64d%c", a - 1, 65 + bl);
return;
}
printf("%I64d%c", a / b, 65 + bl);
f(b, a % b, !bl);
}
int main() {
long long a, b;
scanf("%I64d%I64d", &a, &b);
if (gcd(a, b) != 1) {
puts("Impossible");
return 0;
}
if (a < b)
f(b, a, 1);
else
f(a, b, 0);
puts("");
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
bool a[210] = {0};
bool testcase = 1;
void SieveOfEratosthenes() {
long long n = 200;
memset(a, true, 210);
for (long long p = 2; p * p <= n; p++) {
if (a[p] == true) {
for (long long i = p * p; i <= n; i += p) a[i] = false;
}
}
}
long long gcd(long long a, long long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
void solve() {
long long n, m;
cin >> n >> m;
map<long long, vector<long long> > mp;
long long k;
for (long long i = 0; i < n; i++) {
cin >> k;
mp[k].push_back(k);
for (long long j = 1; j < m; j++) {
long long l;
cin >> l;
mp[k].push_back(l);
}
}
long long o = 0;
long long p;
for (long long i = 0; i < m * n; i++) {
cin >> p;
if (mp.find(p) != mp.end()) {
for (long long j = 0; j < m; j++) cout << mp[p][j] << " ";
cout << endl;
}
}
}
int32_t main() {
std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
long long k;
if (testcase) {
cin >> k;
} else
k = 1;
while (k--) {
solve();
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
void dfs(vector<int> adj[], vector<int>& in, vector<int>& val, vector<int>& vis,
int i, vector<int>& arr) {
int j;
for (j = 0; j < adj[i].size(); j++) {
if (vis[adj[i][j]] == -1) {
vis[adj[i][j]] = 1;
in.push_back(adj[i][j]);
val.push_back(arr[adj[i][j]]);
dfs(adj, in, val, vis, adj[i][j], arr);
}
}
}
int main() {
int n, i, j;
cin >> n;
vector<int> arr(n + 1, 0);
vector<int> ans(n + 1, 0);
vector<int> adj[n + 1];
for (i = 1; i <= n; i++) {
cin >> arr[i];
}
vector<string> mat;
string temp;
for (i = 0; i < n; i++) {
cin >> temp;
mat.push_back(temp);
}
for (i = 0; i < n; i++) {
for (j = 0; j <= i; j++) {
if (mat[i][j] == '1') {
adj[i + 1].push_back(j + 1);
adj[j + 1].push_back(i + 1);
}
}
}
vector<int> vis(n + 1, -1);
for (i = 1; i <= n; i++) {
if (vis[i] == -1) {
vis[i] = 1;
vector<int> in;
vector<int> val;
in.push_back(i);
val.push_back(arr[i]);
dfs(adj, in, val, vis, i, arr);
sort(in.begin(), in.end());
sort(val.begin(), val.end());
for (j = 0; j < in.size(); j++) {
ans[in[j]] = val[j];
}
}
}
for (i = 1; i <= n; i++) {
cout << ans[i] << " ";
}
cout << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long n, m, ans, c, d, k, tot, x, y, t, r, p, a[1001][1001];
int main() {
cin >> n >> k;
if (n < 2 * k + 1) {
cout << -1;
return 0;
}
cout << n * k << endl;
for (int i = 1; i <= n; i++)
for (int j = i; j < k + i; j++) printf("%d %d\n", i, j % n + 1);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100000;
int n;
tuple<int, int, int> a[N];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; ++i) scanf("%d", &get<0>(a[i]));
for (int i = 0; i < n; ++i) scanf("%d", &get<1>(a[i]));
for (int i = 0; i < n; ++i) get<2>(a[i]) = i + 1;
sort(a, a + n);
printf("%d\n", n / 2 + 1);
for (int i = 0; i < n; i += 2) {
if (i + 1 == n) {
printf("%d\n", get<2>(a[i]));
} else if (i + 2 == n) {
printf("%d %d\n", get<2>(a[i]), get<2>(a[i + 1]));
} else {
printf("%d ",
get<1>(a[i]) < get<1>(a[i + 1]) ? get<2>(a[i + 1]) : get<2>(a[i]));
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string str;
long long int nb, ns, nc, push_back, ps, pc, money;
cin >> str >> nb >> ns >> nc >> push_back >> ps >> pc >> money;
long long int b, s, c;
b = s = c = 0;
for (long long int i = 0; i < str.size(); i++) {
if (str[i] == 'B') b++;
if (str[i] == 'S') s++;
if (str[i] == 'C') c++;
}
long long int l, r, f, m;
l = 0;
r = 1e13;
while (l < r) {
m = (l + r + 1) / 2;
f = max(0ll, b * m - nb) * push_back + max(0ll, s * m - ns) * ps +
max(0ll, c * m - nc) * pc;
if (f > money)
r = m - 1;
else
l = m;
}
cout << l << "\n";
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
class C_ {};
template <typename T>
C_& operator<<(C_& m, const T& s) {
if (!1) cerr << "\E[91m" << s << "\E[0m";
return m;
}
C_ merr;
int B[111111];
int A[111111];
int main(void) {
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> B[i];
}
for (int i = 0; i < m; i++) {
int b = B[i];
for (int j = b; j <= n; j++) {
if (!A[j]) A[j] = b;
}
}
for (int i = 0; i < n; i++) {
cout << A[i + 1] << " ";
}
cout << endl;
merr << "Execution time: " << fixed << setprecision(3)
<< (double)clock() / CLOCKS_PER_SEC << " s.\n";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> arr = vector<int>(n);
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
if (n == 1) {
cout << 0 << endl;
return 0;
}
int maxCount = 0;
int count = 0;
if (arr[0] == 1 && arr[1] == 2) count++;
for (int i = 1; i < n - 1; i++) {
if (arr[i] == arr[i - 1] + 1 && arr[i] == arr[i + 1] - 1)
count++;
else {
maxCount = max(maxCount, count);
count = 0;
}
}
if (arr[n - 1] == 1000 && arr[n - 2] == 999) count++;
maxCount = max(maxCount, count);
cout << maxCount << endl;
}
| 1 |
#include <iostream>
#define llint long long
#define mod 1000000007
using namespace std;
llint fact[2005], fact_i[2005];
llint pow(llint a, llint n)
{
if(n == 0) return 1;
if(n % 2){
return ((a%mod) * (pow(a, n-1)%mod)) % mod;
}
else{
return pow((a*a)%mod, n/2) % mod;
}
}
void make_fact()
{
llint val = 1;
fact[0] = 1;
for(int i = 1; i < 2005; i++){
val *= i;
val %= mod;
fact[i] = val;
}
for(int i = 0; i < 2005; i++){
fact_i[i] = pow(fact[i], mod-2);
}
}
llint comb(llint n, llint k)
{
llint ret = 1;
ret *= fact[n];
ret *= fact_i[k], ret %= mod;
ret *= fact_i[n-k], ret %= mod;
return ret;
}
llint N;
llint A, B, C, D;
llint dp[1005][1005];
int main(void)
{
cin >> N;
cin >> A >> B >> C >> D;
make_fact();
dp[0][0] = 1;
llint val;
for(int i = 0; i < N; i++){
for(int j = 0; j <= N; j++){
if(dp[i][j] == 0) continue;
for(int k = 0; j + (i+1)*k <= N; k++){
if(k != 0 && (i+1 < A || i+1 > B)) break;
if(k != 0 && (k < C || k > D)) continue;
val = 1;
for(int l = 0; l < k; l++){
val *= comb(N-j-(i+1)*l, i+1);
val %= mod;
}
val *= fact_i[k];
val %= mod;
dp[i+1][j+(i+1)*k] += (dp[i][j] * val) % mod;
dp[i+1][j+(i+1)*k] %= mod;
}
}
}
cout << dp[N][N] << endl;
return 0;
} | 0 |
#include<iostream>
#include<algorithm>
#include<math.h>
#include<queue>
#include<vector>
#include<climits>
#include<map>
#include<string>
#include<functional>
#include<iomanip>
#include<deque>
#include<random>
#include<set>
using namespace std;
typedef long long ll;
typedef double lldo;
#define mp make_pair
#define pub push_back
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
ll gcd(ll a, ll b) { if (a % b == 0) { return b; } else return gcd(b, a % b); }
ll lcm(ll a, ll b) { if (a == 0) { return b; }return a / gcd(a, b) * b; }
template<class T>ll LBI(vector<T>& ar, T in) { return lower_bound(ar.begin(), ar.end(), in) - ar.begin(); }
template<class T>ll UBI(vector<T>& ar, T in) { return upper_bound(ar.begin(), ar.end(), in) - ar.begin(); }
ll n, q, com, s, t, x, i;
const int INF = (1LL << 31) - 1;
const int sqrtN = 512;
struct SqrtDecomposition {
int N, K;
vector<int> data;
vector<bool> lazyFlag;
vector<int> lazyUpdate;
SqrtDecomposition(int n) : N(n) {
K = (N + sqrtN - 1) / sqrtN;
data.assign(K * sqrtN, INF);
lazyFlag.assign(K, false);
lazyUpdate.assign(K, 0);
}
void eval(int k) {
if (lazyFlag[k]) {
lazyFlag[k] = false;
for (int i = k * sqrtN; i < (k + 1) * sqrtN; ++i) {
data[i] = lazyUpdate[k];
}
}
}
// [s, t)
void update(int s, int t, int x) {
for (int k = 0; k < K; ++k) {
int l = k * sqrtN, r = (k + 1) * sqrtN;
if (r <= s || t <= l)
continue;
if (s <= l && r <= t) {
lazyFlag[k] = true;
lazyUpdate[k] = x;
}
else {
eval(k);
for (int i = max(s, l); i < min(t, r); ++i) {
data[i] = x;
}
}
}
}
int find(int i) {
int k = i / sqrtN;
eval(k);
return data[i];
}
};
int main() {
cin >> n >> q;
SqrtDecomposition ruq(n);
rep(j, q) {
cin >> com;
if (com == 0) {
cin >> s >> t >> x;
ruq.update(s, t + 1, x);
}
else {
cin >> i;
cout << ruq.find(i) << endl;
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, m;
int route[2010], newRoute[2010];
int a[110][110], b[110][110];
int na[110];
void read() {
cin >> n >> m;
int i;
memset(na, sizeof(na), 0);
for (i = 0; i < m + 1; i++)
if (i == 0)
cin >> route[i];
else {
cin >> route[i];
a[route[i]][route[i - 1]] = 1;
a[route[i - 1]][route[i]] = 1;
}
}
int g = 0;
void dfs(int x, int flag) {
int i;
if (g == 1) return;
if (x == m + 1) {
if (flag == 0) return;
g = 1;
for (i = 0; i <= m; i++) cout << newRoute[i] << ' ';
cout << endl;
return;
}
for (i = 1; i <= n; i++)
if (a[newRoute[x - 1]][i] == 1) {
if (flag == 1) {
newRoute[x] = i;
a[newRoute[x - 1]][i] = 0;
a[i][newRoute[x - 1]] = 0;
dfs(x + 1, 1);
a[newRoute[x - 1]][i] = 1;
a[i][newRoute[x - 1]] = 1;
} else if (i >= route[x]) {
newRoute[x] = i;
a[newRoute[x - 1]][i] = 0;
a[i][newRoute[x - 1]] = 0;
if (i > route[x])
dfs(x + 1, 1);
else
dfs(x + 1, 0);
a[newRoute[x - 1]][i] = 1;
a[i][newRoute[x - 1]] = 1;
}
}
}
int main() {
read();
newRoute[0] = route[0];
dfs(1, 0);
if (g == 0) cout << "No solution" << endl;
}
| 4 |
#include <algorithm>
#include <numeric>
#include <queue>
#include <iostream>
#include <iomanip>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); ++i)
struct Event {
double time;
function<void ()> proc;
Event(double time, function<void ()> proc): time(time), proc(proc) {}
bool operator<(const Event& rhs) const { return rhs.time < time; }
};
struct Elevator {
int capacity, speed, stopTime, target, size, rev, dir;
double pos, time;
Elevator(): size(0), rev(0), dir(1), time(0) {}
};
int floorCount, elevatorCount, floorDistance;
int initFireFloor, burningTime, upSpreadTime, downSpreadTime;
vector<Elevator> elevators;
vector<int> deviceCount;
vector<bool> floorBurned;
priority_queue<Event> Q;
double ansTime;
int ansCount;
void changeTarget(double time, int elevator, int stopTime);
int newTarget(int elevator, int currentTarget) {
Elevator& e = elevators[elevator];
if (e.size == e.capacity)
return 0;
for (int i = currentTarget - 1; i > 0; --i)
if (!floorBurned[i] && deviceCount[i] > 0)
return i;
return 0;
}
void onReach(double time, int floor, int elevator, int rev) {
Elevator& e = elevators[elevator];
if (rev < e.rev) return;
int n = min(deviceCount[floor], e.capacity - e.size);
deviceCount[floor] -= n;
e.size += n;
if (floor == 0) {
if (e.size > 0) {
ansTime = max(ansTime, time + e.stopTime);
ansCount += e.size;
e.size = 0;
e.target = floorCount;
changeTarget(time, elevator, e.stopTime);
}
} else {
if (deviceCount[floor] == 0) {
REP(i, elevatorCount)
if (i != elevator && elevators[i].target == floor)
changeTarget(time, i, 0);
}
changeTarget(time, elevator, e.stopTime);
}
}
void onBurned(double time, int floor) {
floorBurned[floor] = true;
REP(i, elevatorCount)
if (elevators[i].target == floor)
changeTarget(time, i, 0);
}
void changeTarget(double time, int elevator, int stopTime) {
Elevator& e = elevators[elevator];
int nextTarget = newTarget(elevator, e.target);
if (e.target == nextTarget) return;
e.target = nextTarget;
e.pos += e.dir * (time - e.time) * e.speed;
e.time = time + stopTime;
double dist = e.target * floorDistance - e.pos;
e.dir = dist >= 0 ? 1 : -1;
e.rev += 1;
double newTime = time + stopTime + fabs(dist) / e.speed;
Q.push(Event(newTime, bind(onReach, newTime, e.target, elevator, e.rev)));
}
int main() {
while (cin >> floorCount >> elevatorCount, floorCount | elevatorCount) {
deviceCount.resize(floorCount);
elevators.assign(elevatorCount, Elevator());
floorBurned.assign(floorCount, false);
cin >> floorDistance;
REP(i, floorCount) cin >> deviceCount[i];
REP(i, elevatorCount) {
Elevator& e = elevators[i];
cin >> e.capacity >> e.speed >> e.stopTime >> e.pos;
e.pos = (e.pos - 1) * floorDistance;
e.target = floorCount;
}
cin >> initFireFloor >> burningTime >> upSpreadTime >> downSpreadTime; --initFireFloor;
ansTime = 0;
ansCount = deviceCount[0];
deviceCount[0] = 0;
REP(floor, floorCount) {
double t = floor >= initFireFloor ? upSpreadTime * (floor - initFireFloor) + burningTime
: downSpreadTime * (initFireFloor - floor) + burningTime;
Q.push(Event(t, bind(onBurned, t, floor)));
}
REP(i, elevatorCount) changeTarget(0, i, 0);
for (; !Q.empty(); Q.pop()) { Q.top().proc(); }
cout << setprecision(20);
cout << ansCount << " " << ansTime << endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 1000000007;
int dp[500010], color[500010];
int n;
long long N, ans;
int mypow(int x, int n) {
int res = 1;
while (n) {
if (n & 1) res = 1ll * res * x % inf;
x = 1ll * x * x % inf;
n >>= 1;
}
return res;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &color[i]);
N += color[i];
}
dp[0] = 0;
dp[1] = 1ll * (N - 1) * (N - 1) % inf * mypow(N, inf - 2) % inf;
for (int i = 1; i < 110000; i++)
dp[i + 1] = ((((dp[i] + dp[i]) % inf - dp[i - 1]) % inf -
1ll * (N - 1) * mypow(N - i, inf - 2) % inf) %
inf +
inf) %
inf;
for (int i = 1; i <= n; i++) ans = (ans + dp[color[i]]) % inf;
cout << ans << endl;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int N = 200 * 1000 + 17;
const int MOD = 1000 * 1000 * 1000 + 7;
using pt = pair<int, int>;
int n = 3;
pt p[3];
bool read() {
if (!(cin >> p[0].first >> p[0].second >> p[1].first >> p[1].second >>
p[2].first >> p[2].second))
return false;
return true;
}
void solve() {
sort(p, p + n, [](const pt& a, const pt& b) { return a.first < b.first; });
set<pt> ans;
pt cur = p[0];
for (; cur.first < p[1].first; ++cur.first) ans.insert(cur);
for (; cur.second != p[1].second;
cur.second < p[1].second ? ++cur.second : --cur.second)
ans.insert(cur);
ans.insert(cur);
cur = p[2];
for (; cur.first > p[1].first; --cur.first) ans.insert(cur);
for (; cur.second != p[1].second;
cur.second < p[1].second ? ++cur.second : --cur.second)
ans.insert(cur);
ans.insert(cur);
cout << ans.size() << endl;
for (auto i : ans) cout << i.first << ' ' << i.second << endl;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
while (read()) solve();
return 0;
}
| 1 |
#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<iostream>
using namespace std;
struct Node
{
int key;
Node *right, *left, *parent;
};
Node *root,*NIL;
Node * treeMinimum(Node *x)
{
while(x->left != NIL) x=x->left;
return x;
}
Node * find(Node *u,int k)
{
while(u!=NIL&&k!=u->key)
{
if(k<u->key) u=u->left;
else u=u->right;
}
return u;
}
Node * treeSuccessor(Node *x)
{
if(x->right != NIL) return treeMinimum(x->right);
Node *y = x->parent;
while(y!=NIL&&x==y->right)
{
x=y;
y=y->parent;
}
return y;
}
void treeDelete(Node *z)
{
Node *y;
Node *x;
if(z->left==NIL||z->right==NIL)
y=z;
else y=treeSuccessor(z);
if(y->left!=NIL)
{
x=y->left;
}
else
{
x=y->right;
}
if(x!=NIL)
{
x->parent=y->parent;
}
if(y->parent==NIL)
{
root=x;
}
else
{
if(y==y->parent->left)
{
y->parent->left=x;
}
else
{
y->parent->right=x;
}
}
if(y!=z)
{
z->key=y->key;
}
free(y);
}
void insert(int k)
{
Node *y = NIL;
Node *x = root;
Node *z;
z = (Node *)malloc(sizeof(struct Node));
z->key = k;
z->left = NIL;
z->right = NIL;
for(;x!=NIL;)
{
y=x;
if(z->key < x->key)
{
x=x->left;
}
else
{
x=x->right;
}
}
z->parent = y;
if(y==NIL)
{
root=z;
}
else
{
if(z->key < y->key)
{
y->left=z;
}
else
{
y->right = z;
}
}
}
void inorder(Node *u)
{
if(u==NIL) return;
inorder(u->left);
printf(" %d",u->key);
inorder(u->right);
}
void preorder(Node *u)
{
if(u==NIL) return;
printf(" %d",u->key);
preorder(u->left);
preorder(u->right);
}
int main()
{
int n,i,x;
string com;
scanf("%d",&n);
for(i=0;i<n;i++)
{
cin>>com;
if(com[0]=='f')
{
scanf("%d",&x);
Node *t = find(root,x);
if(t!=NIL) printf("yes\n");
else printf("no\n");
}
else if(com=="insert")
{
scanf("%d",&x);
insert(x);
}
else if(com=="print")
{
inorder(root);
printf("\n");
preorder(root);
printf("\n");
}
else if(com=="delete")
{
scanf("%d",&x);
treeDelete(find(root,x));
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
char s1[3000], s2[3000];
int f[2005][2005];
int main() {
int l1, l2, ans, i, j, add;
cin >> s1;
cin >> s2;
l1 = strlen(s1);
l2 = strlen(s2);
ans = l2;
for (i = 0; i < l2; i++) {
if (s2[i] == s1[l1 - 1])
f[l1 - 1][i] = 1;
else
f[l1 - 1][i] = 0;
if (ans > l2 - f[l1 - 1][i]) ans = l2 - f[l1 - 1][i];
}
for (i = 0; i < l1; i++) f[i][l2] = 0;
for (i = l1 - 2; i >= 0; i--)
for (j = 0; j < l2; j++) {
if (s1[i] == s2[j])
add = 1;
else
add = 0;
f[i][j] = f[i + 1][j + 1] + add;
if (ans > l2 - f[i][j]) ans = l2 - f[i][j];
}
cout << ans << endl;
i = 1;
}
| 1 |
#include <bits/stdc++.h>
const long long MOD = 1e9 + 7;
using namespace std;
const long long INF = 1LL << 60;
const double PI = acos(-1);
using ll = long long;
using P = array<ll, 2>;
template <typename T>
istream& operator>>(istream& i, vector<T>& v) {
for (ll j = (0); j < (ll)(v.size()); ++j) i >> v[j];
return i;
}
template <typename T>
string join(vector<T>& v) {
stringstream s;
for (ll i = (0); i < (ll)(v.size()); ++i) s << ' ' << v[i];
return s.str().substr(1);
}
template <typename T>
ostream& operator<<(ostream& o, vector<T>& v) {
if (v.size()) o << join(v);
return o;
}
template <typename T>
string join(vector<vector<T>>& vv) {
string s = "\n";
for (ll i = (0); i < (ll)(vv.size()); ++i) s += join(vv[i]) + "\n";
return s;
}
template <typename T>
ostream& operator<<(ostream& o, vector<vector<T>>& vv) {
if (vv.size()) o << join(vv);
return o;
}
template <typename T1, typename T2>
istream& operator>>(istream& i, pair<T1, T2>& v) {
return i >> v.first >> v.second;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& o, pair<T1, T2>& v) {
return o << "(" << v.first << "," << v.second << ")";
}
template <typename T>
istream& operator>>(istream& i, array<T, 2>& v) {
i >> v[0] >> v[1];
return i;
}
template <typename T>
ostream& operator<<(ostream& o, array<T, 2>& v) {
return o << "(" << v[0] << "," << v[1] << ")";
return o;
}
int dx[4]{0, 1, 0, -1};
int dy[4]{1, 0, -1, 0};
void init_init_init() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
std::cout << fixed << setprecision(10);
}
template <class T>
T up(T a, T b) {
assert(b);
return (a + b - 1) / b;
}
template <typename... A>
bool eq(A const&... a) {
auto t = {a...};
assert(t.size());
auto tar = *t.begin();
for (const auto& e : t)
if (tar != e) return false;
return true;
}
template <class T>
bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T>
bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T>
bool chmax(T& a, initializer_list<T> l) {
return chmax(a, max(l));
}
template <class T>
bool chmin(T& a, initializer_list<T> l) {
return chmin(a, min(l));
}
int main(int argc, char** argv) {
init_init_init();
ll N;
cin >> N;
vector<ll> A(N);
cin >> A;
ll res{INF};
chmin(res, 1000000 - A[0]);
chmin(res, A[N - 1] - 1);
for (ll i = 0; i < N - 1; ++i) {
chmin(res, max(A[i] - 1, 1000000 - A[i + 1]));
}
std::cout << res << std::endl;
}
| 2 |
#include <bits/stdc++.h>
const int MAXN = 1e4 + 10;
using namespace std;
vector<int> vec[MAXN << 2];
int vul, n, q;
void update(int rt, int l, int r, int ql, int qr) {
if (ql <= l && r <= qr) {
vec[rt].push_back(vul);
return;
}
int mid = (l + r) >> 1;
if (ql <= mid) update(rt << 1, l, mid, ql, qr);
if (qr > mid) update(rt << 1 | 1, mid + 1, r, ql, qr);
}
bitset<MAXN> ans;
bitset<MAXN> sum;
void querty(int rt, int l, int r, bitset<MAXN> num) {
for (int i = 0; i < vec[rt].size(); i++) num |= (num << vec[rt][i]);
if (l == r) {
ans |= num;
return;
}
int mid = (l + r) >> 1;
querty(rt << 1, l, mid, num);
querty(rt << 1 | 1, mid + 1, r, num);
}
int main() {
n, q;
scanf("%d%d", &n, &q);
int l, r;
for (int i = 1; i <= q; i++) {
scanf("%d%d%d", &l, &r, &vul);
update(1, 1, n, l, r);
}
sum[0] = 1;
querty(1, 1, n, sum);
int cnt = 0;
for (int i = 1; i <= n; i++)
if (ans[i]) cnt++;
printf("%d\n", cnt);
for (int i = 1; i <= n; i++) {
if (ans[i]) printf("%d ", i);
}
puts("");
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int gcd(int x, int y) {
int r = x % y;
while (r != 0) {
x = y;
y = r;
r = x % y;
}
return y;
}
int main() {
int n, m, x, y, a, b;
cin >> n >> m >> x >> y >> a >> b;
int o, p;
o = a / gcd(a, b);
p = b / gcd(a, b);
int s = (int)n / o, l = (int)m / p;
int x1, x2, y1, y2, k, j;
if (s > l) {
k = o * l;
j = p * l;
if (k % 2 == 0) {
x1 = x - k / 2;
x2 = x + k / 2;
} else {
x1 = x - k / 2 - 1;
x2 = x + k / 2;
}
if (j % 2 == 0) {
y1 = y - j / 2;
y2 = y + j / 2;
} else {
y1 = y - j / 2 - 1;
y2 = y + j / 2;
}
} else {
k = o * s;
j = p * s;
if (k % 2 == 0) {
x1 = x - k / 2;
x2 = x + k / 2;
} else {
x1 = x - k / 2 - 1;
x2 = x + k / 2;
}
if (j % 2 == 0) {
y1 = y - j / 2;
y2 = y + j / 2;
} else {
y1 = y - j / 2 - 1;
y2 = y + j / 2;
}
}
if (x2 > n)
while (x2 > n) {
x2--;
x1--;
}
else if (x1 < 0)
while (x1 < 0) {
x1++;
x2++;
}
if (y2 > m)
while (y2 > m) {
y2--;
y1--;
}
else
while (y1 < 0) {
y1++;
y2++;
}
cout << x1 << " " << y1 << " " << x2 << " " << y2;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x7fffffff;
const int MINF = 0x80000000;
const int mod = 1000000007;
const int cons = 100001;
int main() {
int t;
scanf("%d", &t);
while (t--) {
long long n;
scanf("%I64d", &n);
long long sum = n * (n + 1) / 2;
for (int i = 0; (1LL << i) <= n; i++) {
sum -= 2 * (1LL << i);
}
printf("%I64d\n", sum);
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
void ga(int N, long long* A) {
for (int i(0); i < N; i++) scanf("%lld", A + i);
}
long long W;
struct TK {
multiset<long long> U, L;
long long S;
void nr() {
long long a, b;
while (!L.empty() && (long long)U.size() < W)
S += -*L.begin() / 2, U.insert(-*L.begin()), L.erase(L.begin());
while (!L.empty() && *U.begin() < -*L.begin())
a = *U.begin(), U.erase(U.begin()), b = -*L.begin(), L.erase(L.begin()),
U.insert(b), L.insert(-a), S -= a / 2, S += b / 2;
}
void add(long long a) { L.insert(-a), nr(); }
void del(long long a) {
if (U.count(a))
U.erase(U.find(a)), S -= a / 2;
else if (L.count(-a))
L.erase(L.find(-a));
else
assert(0);
nr();
}
} G;
long long N, K, A[(202202)], T[(202202)], I, X = 0, S[(202202)], B, H;
int main(void) {
scanf("%lld%lld%lld", &N, &W, &K), ga(N, A), ga(N, T), *S = *T;
for (int k(1); k < N; k++) S[k] = S[k - 1] + T[k];
for (int i(0); i < N; i++) {
while (B <= K) {
X = max(X, H);
if (I == N) return printf("%lld\n", X), 0;
G.add(T[I]), B = (i <= I ? (S[I] - (i ? S[i - 1] : 0)) : 0) - G.S,
H += A[I++];
}
G.del(T[i]),
B = (i + 1 <= I - 1 ? (S[I - 1] - (i + 1 ? S[i + 1 - 1] : 0)) : 0) -
G.S,
H -= A[i];
}
printf("%lld\n", X);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s; cin>>s;
(s[2]==s[3] && s[4]==s[5]) ? cout<<"Yes\n":cout<<"No\n";
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int MX = 200111;
double dp[MX][55];
int Q[55][2 * MX], t[MX];
int L[55], R[55];
double g[MX], s[MX], f[MX];
int n, K;
inline double Cross(double ax, double ay, double bx, double by) {
return ax * by - ay * bx;
}
inline double Y(int j, int k) { return dp[j][k] - g[j] + f[j] * s[j]; }
inline double X(int j) { return s[j]; }
int main() {
while (scanf("%d%d", &n, &K) == 2) {
for (int i = 1; i <= n; i++) {
scanf("%d", &t[i]);
s[i] = s[i - 1] + t[i];
g[i] = g[i - 1] + s[i] / t[i];
f[i] = f[i - 1] + 1.0 / t[i];
}
memset(L, 0, sizeof(L));
memset(R, 0, sizeof(R));
Q[0][R[0]++] = 0;
dp[0][0] = 0;
for (int i = 1; i <= n; i++) {
for (int k = min(i, K); k; k--) {
int& l = L[k - 1];
int& r = R[k - 1];
int* q = Q[k - 1];
while (l + 1 < r && f[i] * (X(q[l + 1]) - X(q[l])) >=
Y(q[l + 1], k - 1) - Y(q[l], k - 1))
l++;
dp[i][k] = g[i] + -f[i] * X(q[l]) + Y(q[l], k - 1);
{
int& l = L[k];
int& r = R[k];
q = Q[k];
while (l + 1 < r &&
Cross(X(q[r - 1]) - X(q[r - 2]),
Y(q[r - 1], k) - Y(q[r - 2], k), X(i) - X(q[r - 1]),
Y(i, k) - Y(q[r - 1], k)) <= 1e-8)
r--;
q[r++] = i;
}
}
}
printf("%.9f\n", dp[n][min(n, K)]);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
inline int take() {
int s = 0, t = 1;
char c = getchar();
while (!isdigit(c)) {
if (c == '-') t = -1;
c = getchar();
}
while (isdigit(c)) s = s * 10 + c - 48, c = getchar();
return s * t;
}
struct node {
int v, t;
bool operator<(const node& rhs) const { return v < rhs.v; }
} A[200005];
int tot = 0, n, m, v[200005], q[200005];
inline int fin(int val) {
if (val > A[n].v && val < A[1].v) return 0;
int l = 1, r = n, mid;
while (l < r) {
mid = (l + r) >> 1;
if (A[mid].v >= val)
r = mid;
else
l = mid + 1;
}
return A[l].v == val;
}
int main() {
n = take(), m = take();
for (int i(1); i <= n; i++) v[i] = A[A[i].t = i].v = take();
sort(A + 1, A + n + 1);
int odd = 0, oven = 0;
if (A[1].v & 1)
odd++;
else
oven++;
for (int i(2); i <= n; i++) {
if (A[i].v == A[i - 1].v)
v[q[++tot] = A[i].t] = -1;
else {
if (A[i].v & 1)
odd++;
else
oven++;
}
}
if (odd > n >> 1) {
for (int i(1); i <= n; i++) {
if (v[i] != -1 && (v[i] & 1)) v[q[++tot] = i] = -1, odd--;
if (odd == n >> 1) break;
}
} else if (oven > n >> 1) {
for (int i(1); i <= n; i++) {
if (v[i] != -1 && !(v[i] & 1)) v[q[++tot] = i] = -1, oven--;
if (oven == n >> 1) break;
}
}
int x = 1, ans = tot;
while (tot && x <= m) {
if ((x & 1) && odd < n >> 1) {
if (!fin(x)) v[q[tot--]] = x, odd++;
} else if (!(x & 1) && oven < n >> 1) {
if (!fin(x)) v[q[tot--]] = x, oven++;
}
x++;
}
if (tot)
puts("-1");
else {
cout << ans << endl;
for (int i(1); i <= n - 1; i++) cout << v[i] << " ";
cout << v[n] << endl;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int a[100009 + 1], v[100009 + 1];
struct node {
long long v[5];
int s;
} aint[265009];
int k, poz, val;
inline int caut(int a, int v[], int n) {
int rez = 0;
for (int pas = 1 << 16; pas; pas >>= 1)
if (rez + pas <= n && v[rez + pas] <= a) rez += pas;
return rez;
}
inline void unite(node &n, node a, node b) {
n.s = a.s + b.s;
if (n.s >= 5) n.s -= 5;
for (int i = 0; i < 5; i++) n.v[i] = a.v[i];
for (int i = 0; i + a.s < 5; i++) n.v[i + a.s] += b.v[i];
for (int i = 5 - a.s; i < 5; i++) n.v[i + a.s - 5] += b.v[i];
}
void update(int p, int st, int dr) {
if (st == dr) {
aint[p].v[0] += val;
if (aint[p].v[0] == 0)
aint[p].s = 0;
else
aint[p].s = 1;
return;
}
int m = (st + dr) / 2;
if (poz <= m)
update(2 * p, st, m);
else
update(2 * p + 1, m + 1, dr);
unite(aint[p], aint[2 * p], aint[2 * p + 1]);
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
string s;
cin >> s;
if (s == "sum")
v[i] = 0;
else {
cin >> v[i];
if (v[i] == 0) return 1;
if (s == "add")
a[++k] = v[i];
else
v[i] = -v[i];
}
}
sort(a + 1, a + k + 1);
int u = 1;
for (int i = 2; i <= k; i++)
if (a[u] != a[i]) a[++u] = a[i];
for (int i = 1; i <= n; i++) {
if (v[i] == 0)
cout << aint[1].v[2] << "\n";
else {
val = v[i];
poz = caut(abs(v[i]), a, u);
update(1, 1, u);
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18 + 9;
const long long mod = 1e9 + 7;
const long long maxn = 2e5 + 8;
long long n, m, a[maxn];
void solve() {
long long i, j, u, v;
string s;
cin >> n >> s;
sort(s.begin(), s.end());
cout << s << endl;
}
signed main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
long long t = 1;
while (t--) solve();
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n;
bool a[100], b[100];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int I = 0, i, j; I < n * n; I++) {
cin >> i >> j;
i--;
j--;
if (a[i] == 0 && b[j] == 0) {
b[j] = 1;
a[i] = 1;
cout << I + 1 << " ";
}
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
char s[5555];
inline long long get(int st, int fn) {
long long ans = 0;
for (int i = st; i <= fn;) {
long long cur = s[i] - '0';
while (i < fn && s[i + 1] == '*') {
i += 2;
cur = cur * (s[i] - '0');
}
ans += cur;
i += 2;
}
return ans;
}
int main() {
gets(s + 2);
s[0] = '1';
s[1] = '*';
int n = (int)strlen(s);
s[n] = '*';
s[n + 1] = '1';
n += 2;
long long ans = 0;
for (int i = 2; i < n; ++i) {
if (s[i - 1] == '*') {
for (int j = i; j < n - 1; ++j) {
if (s[j + 1] == '*') {
long long cur = 0;
for (int k = 0; k < n;) {
long long now = s[k] - '0';
while (k < n && s[k + 1] == '*') {
if (k == i - 2) {
now *= get(i, j);
k = j;
} else {
k += 2;
now *= (s[k] - '0');
}
}
cur += now;
k += 2;
}
ans = max(ans, cur);
}
}
}
}
cout << ans << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int max_int = numeric_limits<int>::max();
unsigned long long gcd(unsigned long long x, unsigned long long y) {
return y > 0 ? gcd(y, x % y) : x;
}
unsigned long long lcm(unsigned long long x, unsigned long long y) {
unsigned long long r = x / (x > y ? gcd(x, y) : gcd(y, x));
return r < 5e18 / y ? r * y : (unsigned long long)5e18 + 1;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
unsigned long long t, w, b;
cin >> t >> w >> b;
unsigned long long m = min(w, b);
unsigned long long s = lcm(w, b);
unsigned long long r = 0;
if (s > t) {
r += min(t, m - 1);
} else {
r += t / s * m;
r += min(t - t / s * s, m - 1);
}
unsigned long long q = gcd(t, r);
cout << r / q << '/' << t / q;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin>>N;
vector<tuple<string,int,int>> res;
for(int i=0;i<N;i++){
string S;
int P;
cin>>S>>P;
res.push_back(make_tuple(S,100-P,i+1));
}
sort(res.begin(),res.end());
for(tuple<string,int,int> t:res){
cout<<get<2>(t)<<endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
string arr[150], e[150];
long long n, m;
bool isinc(int i) {
for (int j = 1; j < n; j++) {
if (arr[j][i] < arr[j - 1][i]) return false;
}
return true;
}
bool isinc1(int i) {
for (int j = 1; j < n; j++) {
if (arr[j][i] < arr[j - 1][i]) {
if (e[j] == e[j - 1]) return false;
}
}
return true;
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> arr[i];
long long cnt = 0, cnt1 = 0;
for (int j = 0; j < m; j++) {
if (cnt1 == 0) {
if (isinc(j)) {
cnt1++;
for (int i = 0; i < n; i++) {
e[i] += arr[i][j];
}
} else
cnt++;
} else {
if (isinc1(j)) {
for (int i = 0; i < n; i++) {
e[i] += arr[i][j];
}
} else
cnt++;
}
}
cout << cnt;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int v1, v2, v3, vm;
cin >> v1 >> v2 >> v3 >> vm;
for (int a = v1; a < v1 * 2 + 1; a++) {
for (int b = v2; b < min(a, v2 * 2 + 1); b++) {
for (int c = v3; c < min(b, v3 * 2 + 1); c++) {
if (c >= vm && 2 * vm >= c && 2 * vm < b) {
cout << a << endl << b << endl << c << endl;
return 0;
}
}
}
}
cout << -1 << endl;
return 0;
}
| 1 |
//ABC152-E Flatten より
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define SP << " "
#define LLi long long int
using namespace std;
int gcdi(int a, int b) {
if(a<0) a=b;
if(b<0) b=a;
if (b == 0) return a;
return gcdi(b, a%b);
}
int main(){
int a, b, c, d, x, y;
cin>> a >> b >> c >> d;
x=abs(a-c);
y=abs(b-d);
if(x==0 || y==0){
cout<< "0" <<endl;
return 0;
}
cout<< x+y-gcdi(x, y) <<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
#define va first
#define vb second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int N, mx;
ll ans;
int A[101010], B[101010];
set<int> st;
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cin >> N;
for (int i=0; i<N; i++) cin >> A[i];
for (int i=0; i<N; i++)
B[A[i]] = i;
st.insert(-1); st.insert(N);
for (int i=N; i>0; i--) {
st.insert(B[i]);
set<int>::iterator it = st.find(B[i]);
set<int>::iterator p = prev(it), n = next(it);
if (*p!=-1) {
ans += (ll)i*(*n-*it)*(*p-*prev(p));
}
if (*n!=N) {
ans += (ll)i*(*next(n)-*n)*(*it-*p);
}
}
cout << ans;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void maxtt(T& t1, T t2) {
t1 = max(t1, t2);
}
template <typename T>
void mintt(T& t1, T t2) {
t1 = min(t1, t2);
}
bool debug = 0;
int n, m, k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
string direc = "URDL";
const long long MOD2 = (long long)998244353 * (long long)998244353;
long long ln, lk, lm;
void etp(bool f = 0) {
puts(f ? "YES" : "NO");
exit(0);
}
void addmod(int& x, int y, int mod = 998244353) {
x += y;
if (x >= mod) x -= mod;
if (x < 0) x += mod;
assert(x >= 0 && x < mod);
}
void et(int x = -1) {
printf("%d\n", x);
exit(0);
}
long long fastPow(long long x, long long y, int mod = 998244353) {
long long ans = 1;
while (y > 0) {
if (y & 1) ans = (x * ans) % mod;
x = x * x % mod;
y >>= 1;
}
return ans;
}
long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; }
char s[55];
vector<int> rd(string os) {
cout << "? " << os << endl;
scanf("%d", &k);
if (k == -1) exit(0);
vector<int> vp;
for (int(j) = 0; (j) < (int)(k); (j)++) {
int x;
scanf("%d", &x);
vp.push_back(x);
for (int(z) = 0; (z) < (int)(os.size()); (z)++) s[x + z] = os[z];
}
return vp;
}
void ppt() {
printf("! ");
for (int(i) = 1; (i) <= (int)(n); (i)++) printf("%c", s[i]);
puts("");
fflush(stdout);
int x;
scanf("%d", &x);
if (x == 0) exit(0);
}
void cala() {
vector<int> zz = rd("CCC");
if (!zz.empty()) {
for (int(i) = 1; (i) <= (int)(n); (i)++)
if (s[i] == '?') s[i] = 'O';
ppt();
return;
}
zz = rd("HHH");
if (!zz.empty()) {
for (int(i) = 1; (i) <= (int)(n); (i)++)
if (s[i] == '?') s[i] = 'O';
ppt();
return;
}
zz = rd("OOO");
if (!zz.empty()) {
if (zz.back() == n - 2) {
ppt();
return;
}
string tmp = "";
if (s[n - 1] == '?') s[n - 1] = 'C';
s[n] = 'C';
for (int(i) = 1; (i) <= (int)(n); (i)++) tmp.push_back(s[i]);
zz = rd(tmp);
if (!zz.empty()) {
ppt();
return;
}
if (s[n - 1] == 'C') s[n - 1] = 'H';
s[n] = 'H';
ppt();
return;
}
assert(n == 4);
zz = rd("OOCC");
if (!zz.empty()) {
ppt();
return;
}
s[1] = 'O';
s[2] = 'O';
s[3] = 'H';
s[4] = 'H';
ppt();
}
void fmain(int tid) {
scanf("%d", &n);
for (int(i) = 1; (i) <= (int)(n); (i)++) s[i] = '?';
rd("CH");
rd("CO");
rd("HC");
rd("HO");
int st = 1;
while (st <= n && s[st] == '?') {
st++;
}
if (st == n + 1) {
cala();
return;
}
string S = "";
S.push_back(s[st]);
S.push_back(s[st + 1]);
if (st != 1) {
int j = st - 1;
while (j >= 1) {
vector<int> zz = rd(S[0] + S);
if (!zz.empty() && zz[0] == j) {
S = S[0] + S;
j--;
} else {
for (int(zz) = 1; (zz) <= (int)(j); (zz)++) S = "O" + S;
break;
}
}
}
while (S.size() < n) {
int ed = (int)S.size() + 1;
if (s[ed] != '?') {
S.push_back(s[ed]);
continue;
}
while (ed <= n && s[ed] == '?') {
ed++;
}
if (ed <= n) {
for (int j = (int)S.size() + 1; j < ed; j++) {
vector<int> zz = rd(S + S.back());
if (!zz.empty() && zz[0] == 1) {
S.push_back(S.back());
} else {
while (j < ed) {
S.push_back(s[ed]);
j++;
}
break;
}
}
} else {
for (int j = (int)S.size() + 1; j < ed; j++) {
vector<int> zz = rd(S + S.back());
if (!zz.empty() && zz[0] == 1) {
S.push_back(S.back());
} else {
int sz = (int)S.size();
while (S.size() < n) {
S.push_back('C');
}
zz = rd(S);
if (!zz.empty()) break;
while (S.size() > sz) S.pop_back();
while (S.size() < n) {
S.push_back('H');
}
break;
}
}
}
}
for (int(i) = 1; (i) <= (int)(n); (i)++) s[i] = S[i - 1];
ppt();
}
int main() {
int t = 1;
scanf("%d", &t);
for (int(i) = 1; (i) <= (int)(t); (i)++) {
fmain(i);
}
return 0;
}
| 5 |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
int n,a[100000];
cin>>n;
for(int i=0;i<n;i++)cin>>a[i];
ll ans=a[0]-1,t=2;
for(int i=1;i<n;i++){
if(a[i]==t)t++;
if(a[i]>t){
if(a[i]%t)ans+=a[i]/t;
else ans+=((a[i]-t)/t);
}
}
cout<<ans<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int a, b, c;
cin >> a >> b >> c;
int x, y, z;
cin >> x >> y >> z;
int q = a - x;
int w = b - y;
int e = c - z;
int extra = 0;
if (q > 0) extra += q / 2;
if (w > 0) extra += w / 2;
if (e > 0) extra += e / 2;
if (q < 0) extra += q;
if (w < 0) extra += w;
if (e < 0) extra += e;
if (extra >= 0)
cout << "Yes";
else
cout << "No";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
int a, b, c;
cin >> a >> b >> c;
while (a > 0) {
if ((b / 2 >= a) && (c / 4 >= a)) {
b = a * 2;
c = a * 4;
break;
} else {
a--;
}
}
if (a == 0) {
cout << 0;
} else {
cout << a + b + c;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int dir[4][2] = {{1, 0}, {0, 1}, {0, -1}, {-1, 0}};
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-6;
const long long mod = 1e9 + 7;
inline long long read() {
long long x = 0;
bool f = true;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = false;
c = getchar();
}
while (c >= '0' && c <= '9')
x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
return f ? x : -x;
}
inline long long gcd(long long m, long long n) {
return n == 0 ? m : gcd(n, m % n);
}
void exgcd(long long A, long long B, long long& x, long long& y) {
if (B)
exgcd(B, A % B, y, x), y -= A / B * x;
else
x = 1, y = 0;
}
inline int qpow(int x, long long n) {
int r = 1;
while (n > 0) {
if (n & 1) r = 1ll * r * x % mod;
n >>= 1;
x = 1ll * x * x % mod;
}
return r;
}
inline int inv(int x) { return qpow(x, mod - 2); }
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
const int N = 2e5 + 5;
int main() {
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
vector<vector<int>> dp(n + 1, vector<int>(2, inf));
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) cin >> a[i];
dp[1][1] = a[1];
dp[0][0] = 0;
for (int i = 2; i <= n; i++) {
dp[i][0] = min(dp[i - 2][1], dp[i - 1][1]);
dp[i][1] = min(dp[i - 2][0] + a[i - 1] + a[i], dp[i - 1][0] + a[i]);
}
cout << min(dp[n][1], dp[n][0]) << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
int n, k, a[N];
int main() {
int ans = 0;
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < k; i++) {
int one = 0, two = 0;
for (int j = i; j < n; j += k) {
if (a[j] == 1)
one++;
else
two++;
}
ans += min(one, two);
}
cout << ans << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
stack<char> st;
for (int i = 0; i < s.size(); i++) {
while (!st.empty() && st.top() < s[i]) {
st.pop();
}
st.push(s[i]);
}
stack<char> st1;
while (!st.empty()) {
st1.push(st.top());
st.pop();
}
while (!st1.empty()) {
cout << st1.top();
st1.pop();
}
return 0;
}
| 1 |
#include<iostream>
using namespace std;
int gcd(int a,int b){
if(b==0)return a;
return gcd(b,a%b);
}
int main(){
int w,h,c;
cin>>w>>h>>c;
int x=w*h/gcd(w,h);
cout<<x/w*x/h*c<<endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
const int N = 2e5 + 5;
int a[N];
long long sum[N];
struct Line {
int a;
long long b;
long long get(int x) { return 1ll * a * x + b; }
};
struct Convex_hull {
int sz;
Line *hull;
Convex_hull(int maxn) {
hull = new Line[++maxn];
sz = 0;
}
void clear() { sz = 0; }
bool is_bad(int cur, int pre, int nex) {
Line c = hull[cur], p = hull[pre], n = hull[nex];
return (c.b - n.b) * (c.a - p.a) <= (p.b - c.b) * (n.a - c.a);
}
void add_line(int a, long long b) {
hull[sz++] = (Line){a, b};
while (sz > 2 && is_bad(sz - 2, sz - 3, sz - 1)) {
hull[sz - 2] = hull[sz - 1];
sz--;
}
}
long long query(int x) {
int low = -1, high = sz - 1;
while (low + 1 < high) {
int mid = low + high >> 1;
if (hull[mid].get(x) <= hull[mid + 1].get(x)) {
low = mid;
} else {
high = mid;
}
}
return hull[high].get(x);
}
};
int main() {
int n;
scanf("%d", &n);
long long ans = 0;
sum[0] = 0;
for (int i = 1; i <= n; ++i) {
scanf("%d", a + i);
sum[i] = sum[i - 1] + a[i];
ans += 1ll * a[i] * i;
}
Convex_hull *hull = new Convex_hull(n);
long long add = 0;
for (int r = 2; r <= n; ++r) {
hull->add_line(r - 1, -sum[r - 2]);
add = std::max(add, hull->query(a[r]) + sum[r - 1] - 1ll * a[r] * r);
}
hull->clear();
for (int l = n - 1; l >= 1; --l) {
hull->add_line(-(l + 1), -sum[l + 1]);
add = std::max(add, hull->query(-a[l]) + sum[l] - 1ll * a[l] * l);
}
printf("%I64d\n", ans + add);
return 0;
}
| 5 |
#include <string>
#include <iostream>
#include <math.h>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <map>
#include <set>
typedef long long ll;
const int mod=1e9+7;
#define lol(i,n) for(int i=0;i<n;i++)
using namespace std;
int n,a[100010],b[100010];
//vector<pair<int,int>> a;
ll sum=0,s=0;
int main() {
cin >>n;
lol(i,n){
//int x,y;
cin >>a[i]>>b[i];
// a.push_back(make_pair(x,y));
}
//sort(a.begin(),a.end());
sort(a,a+n);
sort(b,b+n);
int x,y;
x=0;
lol(i,n){
sum++;
while(b[x]<=a[i]){
sum--;
x++;
}
s=max(sum,s);
}
cout << s<< endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
bool two[10005] = {0};
bool three[10005] = {0};
int main() {
string s;
cin >> s;
set<string> ans;
two[s.size()] = three[s.size()] = 1;
for (int i = s.size() - 2; i >= 5; i--) {
string p = string(1, s[i]) + string(1, s[i + 1]);
if (three[i + 2]) {
ans.insert(p);
two[i] = 1;
} else if (two[i + 2] && p != string(1, s[i + 2]) + string(1, s[i + 3])) {
ans.insert(p);
two[i] = 1;
}
p = string(1, s[i]) + string(1, s[i + 1]) + string(1, s[i + 2]);
if (two[i + 3]) {
ans.insert(p);
three[i] = 1;
} else if (three[i + 3] && p != string(1, s[i + 3]) + string(1, s[i + 4]) +
string(1, s[i + 5])) {
three[i] = 1;
ans.insert(p);
}
}
cout << ans.size() << '\n';
for (string s : ans) cout << s << '\n';
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
bool DFS(string q){
if(q=="ABC")return 1;
if(q.size()<5)return 0;
string x="";char z='B';
if(q.substr(0,3)=="ABC")z='A';
if(q.substr(q.size()-3,3)=="ABC")z='C';
int i=0;
while(i<q.size()){
if(q.substr(i,3)=="ABC"){x.push_back(z);i+=3;}
else{if(q[i]==z)return 0;x.push_back(q[i]);i++;}
}
if(x.size()<q.size())return DFS(x);
return 0;
}
int main(){
string s;cin>>s;
cout<<(DFS(s)?"Yes":"No")<<endl;
return 0;
} | 0 |
#include <iostream>
#include <bitset>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#define REP(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
string adj(string a, string b, unsigned long int m) {
if(a.find(b) != string::npos) return a;
int mmch = 0;
REP(i,min(m,min(a.size(),b.size()))){
if (a.substr(a.size() - i) == b.substr(0,i)) mmch = i;
}
a += b.substr(mmch);
return a;
}
int main() {
while(1){
int n;
cin >> n;
if(!n) break;
vector<string> a(n);
REP(i,n) cin >> a[i];
string ms = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz";
vector<vector<string>> bdp(1<<n,vector<string>(n,ms));
std::fill(begin(bdp[0]),end(bdp[0]),"");
REP(i,1<<n){
REP(l,n){
bitset<10> bs(i);
REP(j,n){
if(!bs[j]){
bitset<10> cbs(bs);
cbs[j]=true;
int k = cbs.to_ulong();
string as = adj(bdp[i][l],a[j],a[j].size());
if(as.size() < bdp[k][j].size() || (as.size() == bdp[k][j].size() && as < bdp[k][j]))
bdp[k][j] = as;
}
}
}
}
REP(i,n){
if(ms.size() > bdp.back()[i].size() || (ms.size() == bdp.back()[i].size() && ms > bdp.back()[i]))
ms = bdp.back()[i];
}
cout << ms << endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
char s[100010];
int b[100010], i, j, k = 0, c, c1, l, f = 0, a, j1, f1;
scanf("%s", s);
l = strlen(s);
c = 0;
for (i = 0; i < l; i++) {
if (s[i] == '(')
c++;
else if (s[i] == ')')
c--;
else if (s[i] == '#') {
a = 1;
c1 = 0;
f1 = 0;
for (j = i + 1; j < l; j++) {
if (s[j] == ')')
c--;
else if (s[j] == '#')
a++;
else if (s[j] == '(') {
f1 = 0;
c1 = 0;
for (j1 = j; j1 < l; j1++) {
if (s[j1] == '#') {
f1 = 1;
break;
} else if (s[j1] == '(')
c1++;
else
c1--;
}
if (c1 < 0 && !f1) c += c1;
break;
}
}
if (a > c) {
f = 1;
printf("-1\n");
break;
} else {
if (!f1) {
b[k] = c - a + 1;
c = 0;
if (c1 < 0) c = 0 - c1;
} else {
b[k] = 1;
c -= a;
}
for (j1 = k + 1; j1 < k + a; j1++) b[j1] = 1;
k = k + a;
i = j - 1;
}
}
if (c < 0 && !f) {
f = 1;
printf("-1\n");
break;
}
}
if (!f) {
if (c > 0)
printf("-1\n");
else
for (i = 0; i < k; i++) printf("%d\n", b[i]);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
struct point {
long long x, y;
} C[100010];
long long triArea2(const point &a, const point &b, const point &c) {
return (a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y));
}
bool inConvexPoly(int nc, const point &p) {
int st = 1, en = nc - 1, mid;
while (en - st > 1) {
mid = (st + en) >> 1;
if (triArea2(C[0], C[mid], p) < 0)
en = mid;
else
st = mid;
}
if (st == 1) {
if (triArea2(C[0], C[st], p) <= 0) return false;
}
if (en == nc - 1) {
if (triArea2(C[en], C[0], p) <= 0) return false;
}
if (triArea2(C[0], C[st], p) < 0) return false;
if (triArea2(C[st], C[en], p) <= 0) return false;
if (triArea2(C[en], C[0], p) < 0) return false;
return true;
}
int main() {
int n, T, t = 1, m, i, j, k;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &m);
C[i].x = m;
scanf("%d", &m);
C[i].y = m;
}
reverse(C, C + n);
C[n] = C[0];
cin >> k;
bool f = 1;
point p;
while (k--) {
scanf("%d", &m);
p.x = m;
scanf("%d", &m);
p.y = m;
if (!inConvexPoly(n, p)) {
f = 0;
break;
}
}
if (f)
puts("YES");
else
puts("NO");
return 0;
}
| 2 |
#include<bits/stdc++.h>
#define fastasfuckboi ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define test int t;cin>>t;while(t--)
#define REP(i,n) for(int i=0; i<n;i++)
#define rep(i,a,b) for(int i=a;i<b;i++)
#define mod 1000000007
#define mod2 998244353
#define ll long long
#define ld long double
#define int long long
#define pb push_back
#define mp make_pair
#define ii pair<int,int>
#define llll pair<ll,ll>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>
#define vllll vector<llll>
#define f first
#define s second
#define INF 1000000000
#define HINF 1000000000000000
#define mem(a,b) memset(a,b,sizeof(a))
#define arrin(x,n) int x[n]; for(int o=0;o<n;o++) cin>>x[o]
#define arrout(x,n) for(int o=0;o<n;o++) cout<<x[o]<<" "; cout<<endl
#define vecin(x,n); vi x; for(int o=0;o<n;o++) {cin>>hool; x.pb(hool);}
#define vecout(x,n) for(int o=0;o<n;o++) cout<<x[o]<<" "; cout<<endl
#define all(x) x.begin(),x.end()
#define endl '\n'
int hool;
using namespace std;
template<typename T,typename T1>T amax(T &a,T1 b){if(b>a)a=b;return a;}
template<typename T,typename T1>T amin(T &a,T1 b){if(b<a)a=b;return a;}
namespace number_theory{
int powersimple(int a, int b){//a^b
int res=1;
while(b>0){
if(b&1)
{res=(res*a);
b--;}
a=(a*a);
b>>=1;
}
return res;
}
int ncr(int n,int k)
{
int ans=1;
if(k>n-k)
k=n-k;
for(int i=1;i<=k;i++)
ans*=(n-i+1),ans/=i;
return ans;
}
int power(int x,int y,int p)
{
int res = 1;
x = x % p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
int modInverse(int n, int p)
{
return power(n, p - 2, p);
}
int ncrModPFermat(int n,int r, int p)
{
if (r == 0)
return 1;
int fac[n + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = (fac[i - 1] * i) % p;
return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p;
}
int gcd(int a, int b){
return (b==0)?a:gcd(b,a%b);
}
}
using namespace number_theory;
// https://codeforces.com/blog/entry/62393
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
//use: unordered_map<int,int,custom_hash> mp;
void solve(){
int a,b;cin>>a>>b;
set<int>s;
int c=0;
while(a){
int te=a%b;
s.insert(te);
a/=b;
c++;
}
cout<<(c==s.size()?"yes":"no");
}
signed main()
{
fastasfuckboi;
int t;
t=1;
//cin>>t;
while(t-->0){
solve();
}
return 0;
} | 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, k, i, j;
cin >> n >> k;
vector<long long> a(n);
for (i = 0; i < n; ++i) cin >> a[i];
sort(a.begin(), a.end());
unordered_set<long long> s;
s.insert(a.begin(), a.end());
long long ans = 0;
for (i = 0; i < n; ++i) {
if (s.count(a[i]) > 0) {
int len = 0;
long long st = a[i];
while (s.count(st) > 0) {
len++;
s.erase(st);
st *= k;
}
ans += len / 2 + len % 2;
}
}
cout << ans;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9;
long long MOD = 1e9 + 7;
int a[1111];
int main() {
int n;
cin >> n;
int l = -1;
int res = 0;
int t = 0;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) {
int x;
x = a[n - 1 - i];
if (l == -1) {
l = x;
continue;
}
if (l == x) {
cout << "Infinite";
return 0;
}
if (l == 1) {
if (x == 2) {
res += 3;
t = 0;
} else {
res -= t;
res += 4;
t = 0;
}
l = x;
continue;
}
if (l == 2) {
if (x == 3) {
cout << "Infinite";
return 0;
} else {
t = 1;
res += 3;
}
l = x;
}
if (l == 3) {
if (x == 1) {
t = 0;
res += 4;
} else {
cout << "Infinite";
return 0;
}
l = x;
}
}
cout << "Finite\n";
cout << res << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000006;
string A, a, b;
int h1[MAXN], h2[MAXN], pw[MAXN];
int f1[MAXN], f2[MAXN];
void Hash() {
pw[0] = 1;
for (int i = 1; i < MAXN; i++) {
pw[i] = pw[i - 1] * 257;
}
h1[0] = A[0];
h2[0] = b[0];
for (int i = 1; i < A.size(); i++) {
h1[i] = h1[i - 1] * 257 + A[i];
}
for (int i = 1; i < b.size(); i++) {
h2[i] = h2[i - 1] * 257 + b[i];
}
}
void KMP() {
for (int i = 1, j = 0; i < a.size(); i++) {
while (j && a[i] != a[j]) j = f1[j - 1];
if (a[i] == a[j]) f1[i] = ++j;
}
for (int i = 0, j = 0; i < b.size(); i++) {
while (j && b[i] != a[j]) j = f1[j - 1];
if (b[i] == a[j]) j++;
f2[i] = j;
}
}
bool check(int i, int j) {
if (i > j) return true;
if (i == 0)
return h1[j] == h2[j - i];
else {
return (h1[j] - h1[i - 1] * pw[j - i + 1]) == h2[j - i];
}
}
int main() {
ios::sync_with_stdio(false);
getline(cin, A);
getline(cin, b);
a = A;
if (A.length() != b.length()) {
cout << -1 << ' ' << -1 << "\n";
exit(0);
}
reverse(a.begin(), a.end());
KMP();
Hash();
int ansI = 0, ansJ = 0;
int chk = 0;
for (int i = 0; i < A.size(); i++) {
if (A[i] != b[A.size() - i - 1]) break;
int overlap = f2[b.size() - i - 2];
if (overlap && check(i + 1, A.size() - overlap - 1)) {
ansI = i;
ansJ = A.size() - overlap - 1;
chk = 1;
}
}
if (chk)
cout << ansI << ' ' << ansJ + 1;
else
cout << -1 << ' ' << -1;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t, n, c0, c1, h;
scanf("%d", &t);
while (t--) {
scanf("%d %d %d %d ", &n, &c0, &c1, &h);
int cost = 0;
if (c0 + h < c1) c1 = c0 + h;
if (c1 + h < c0) c0 = c1 + h;
for (int i = 0; i < n; i++) cost += getchar() == '0' ? c0 : c1;
getchar();
printf("%d\n", cost);
}
return 0;
}
| 1 |
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define MAXCOST 50000
double dp[51][MAXCOST+1];
int v[4];
struct INFO{
int type,val;
INFO(){type=0;val=0;};
};
INFO mass[51];
int main(){
int x,y,z;
while(cin >> x >> y >> z , x){
rep(i,51)mass[i] = INFO();
rep(i,51)rep(j,MAXCOST+1)dp[i][j] = 0;
rep(i,x)cin >> v[i];
int mxcost = 0;
rep(i,z){
int N;
cin >> N;
cin >> mass[N].type >> mass[N].val;
if(mass[N].type == 2)mxcost += mass[N].val;
}
double baseProb = 1.0/x;
dp[0][0] = 1;
for(int pos = 0; pos < y ; pos++){
for(int cost = 0 ; cost <= mxcost ; cost++){
rep(i,x){
int nextPos = pos+v[i];
if(nextPos > y)nextPos = y;
if(mass[nextPos].type == 0){
dp[nextPos][cost] += dp[pos][cost] * baseProb;
}
if(mass[nextPos].type == 1){
int newPos = nextPos + mass[nextPos].val;
if(newPos > y)newPos = y;
dp[newPos][cost] += dp[pos][cost] * baseProb;
}
if(mass[nextPos].type == 2){
int newCost = mass[nextPos].val + cost;
dp[nextPos][newCost] += dp[pos][cost] * baseProb;
}
if(mass[nextPos].type == 3){
int newCost = cost - mass[nextPos].val;
if(newCost < 0)newCost = 0;
dp[nextPos][newCost] += dp[pos][cost] * baseProb;
}
}
}
}
double sum = 0;
for(int i = 0; i <= mxcost ;i++){
sum += dp[y][i] * i;
}
cout << (int)sum << endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main()
{string A, B; cin >> A >> B;
cout << (A == B ? "EQUAL" : vector<string>{"GREATER", "LESS"}[A.size() == B.size() ? A < B : A.size() < B.size()]);} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long _INF = 0xc0c0c0c0c0c0c0c0;
const long long mod = (int)1e9 + 7;
const int N = 1e5 + 100;
int a[N], k, n;
bool check(int x) {
if (x < 1 || x > n) return false;
int t = a[x] + 1;
if (x > 1 && a[x - 1] * 2 < t) return false;
if (x < n && t >= a[x + 1]) return false;
return true;
}
int main() {
scanf("%d%d", &k, &n);
for (int i = 1; i <= n; ++i) a[i] = i;
long long t = 1ll * (n + 1) * n / 2;
if (t > k) {
puts("NO");
return 0;
}
t = k - t;
int z = t / n;
t %= n;
for (int i = 1; i <= n; ++i) a[i] += z;
int ban = n;
for (int i = 1; i <= t; ++i) {
while (ban >= 1 && !check(ban)) --ban;
if (ban < 1) {
puts("NO");
return false;
}
++a[ban];
ban = (ban + 1, n);
}
puts("YES");
for (int i = 1; i <= n; ++i) {
printf("%d%c", a[i], " \n"[i == n]);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
vector<int> p;
vector<vector<int>> decomp;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
p.resize(n);
for (int i = 0; i < n; ++i) cin >> p[i], --p[i];
{
vector<bool> vis(n, false);
for (int i = 0; i < n; ++i) {
if (not vis[i]) {
int j = i;
vector<int> vi;
while (not vis[j]) {
vis[j] = true;
vi.push_back(j);
j = p[j];
}
decomp.push_back(vi);
}
}
}
sort(decomp.begin(), decomp.end(),
[](vector<int> a, vector<int> b) { return a.size() < b.size(); });
if (decomp[0].size() == 1) {
int a = decomp[0][0];
cout << "YES\n";
for (int i = 0; i < n; ++i) {
if (i != a) cout << i + 1 << ' ' << a + 1 << '\n';
}
} else if (decomp[0].size() == 2) {
for (int i = 0; i < decomp.size(); ++i)
if (decomp[i].size() & 1) {
cout << "NO\n";
exit(0);
}
cout << "YES\n";
cout << decomp[0][1] + 1 << ' ' << decomp[0][0] + 1 << '\n';
for (int i = 1; i < decomp.size(); ++i) {
for (int j = 0; j < decomp[i].size(); ++j) {
cout << decomp[i][j] + 1 << ' ' << decomp[0][j & 1] + 1 << '\n';
}
}
} else {
cout << "NO\n";
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long int powmod(long long int base, long long int pow) {
long long int res = 1;
while (pow) {
if (pow % 2 != 0) res = (res * base) % 1000000007;
base = (base * base) % 1000000007;
pow /= 2;
}
return res;
}
long long int cumfreq[(long long int)1000001];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int n, k;
cin >> n >> k;
long long int mini = (int)1e9;
for (long long int i = 0; i < n; i++) {
long long int x;
cin >> x;
mini = min(mini, x);
cumfreq[x]++;
}
for (long long int i = 1; i <= (long long int)1e6; i++) {
cumfreq[i] += cumfreq[i - 1];
}
if (mini <= k + 1) {
cout << mini;
return 0;
}
for (long long int ans = mini; ans; ans--) {
long long int sum = 0;
for (long long int i = 1; i * ans - 1 <= (long long int)1e6; i++) {
sum += ((ans * i + k > (long long int)1e6 ? cumfreq[(long long int)1e6]
: cumfreq[ans * i + k]) -
cumfreq[ans * i - 1]);
}
if (sum == n) {
cout << ans;
return 0;
}
}
}
| 5 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.