solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
istream& operator>>(istream& in, vector<T>& a) {
for (long long i = 0; i < a.size(); i++) in >> a[i];
return in;
}
template <typename T>
ostream& operator<<(ostream& out, vector<T>& a) {
for (long long i = 0; i < a.size(); i++) out << a[i];
return out;
}
template <typename T, typename E>
istream& operator>>(istream& in, pair<T, E>& a) {
in >> a.first >> a.second;
return in;
}
long long gcd(long long a, long long b) {
long long c = 1;
while (b) {
c = a % b;
a = b;
b = c;
}
return a;
}
long long dx[8] = {-2, -1, 1, 2, 2, 1, -1, -2};
long long dy[8] = {1, 2, 2, 1, -1, -2, -2, -1};
vector<long long> nx = {0, 9, 189, 2889, 38889, 488889, 5888889, 68888889};
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
long long q;
cin >> q;
while (q--) {
long long k;
cin >> k;
long long l = 1, r = 1, cc = 1, u = 1, pow = 1, i = 1;
while (l < k) {
r += cc;
l += r;
i++;
if (u * 9 == r) {
pow *= 10;
cc++;
u += pow * cc;
}
}
long long z = k - (l - r);
long long cnt = 0, ww = 1;
pow = 1;
while ((cnt + pow * ww) * 9 < z) {
cnt += pow * ww;
ww++;
pow *= 10;
}
long long sym_cnt = (z - (cnt * 9)) - 1;
long long ok = (pow) + sym_cnt / ww;
string s = to_string(ok);
if (z < 10)
cout << z << endl;
else
cout << s[sym_cnt % ww] << endl;
z = z;
}
}
| 5 |
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
ll gcd(ll x,ll y){
if(y==0) return x;
else return gcd(y,x%y);
}
int main (){
ll n,x;
scanf("%lld%lld",&n,&x);
printf("%lld\n",3ll*(n-gcd(n-x,x)));
return 0;
}
| 0 |
#include<iostream>
#include<vector>
#include<set>
#include<map>
using namespace std;
int n;
string s[1<<17];
set<unsigned long>S[700];
map<int,int>M;
vector<unsigned long>h,make;
vector<int>le;
string t;
long dp[1<<17],mod=1e9+7;
int main()
{
cin>>n;
int sz=0;
for(int i=0;i<n;i++)
{
cin>>s[i];
int l=s[i].size();
if(M.find(l)==M.end())
{
M[l]=sz++;
le.push_back(l);
h.push_back(0);
unsigned long now=1;
for(int j=0;j<l;j++)
{
now=now*mod;
}
make.push_back(now);
}
unsigned long tmp=0;
for(int j=0;j<l;j++)tmp=tmp*mod+s[i][j];
S[M[l]].insert(tmp);
}
cin>>t;
dp[0]=1;
for(int i=0;i<t.size();i++)
{
for(int j=0;j<sz;j++)
{
h[j]=h[j]*mod+t[i];
if(i>=le[j])h[j]-=t[i-le[j]]*make[j];
if(i>=le[j]-1&&S[j].find(h[j])!=S[j].end())dp[i+1]=(dp[i+1]+dp[i+1-le[j]])%mod;
}
}
cout<<dp[t.size()]<<endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int vis[201000], dep[201000], n, a[201000], b[201000], fa[201000], rta, rtb, O;
int cp, cq;
vector<int> G[201000], cir;
void dfs(int u, int f) {
fa[u] = f, dep[u] = dep[f] + 1;
for (auto v : G[u])
if (v != f) dfs(v, u);
}
void no() {
puts("-1");
exit(0);
}
int lca(int u, int v) {
while (u != v) dep[u] > dep[v] ? u = fa[u] : v = fa[v];
return u;
}
void findcir() {
int p = 0, q = 0;
for (int i = 1; i <= n; ++i)
if (a[i] != b[i] && dep[i] > dep[p]) p = i;
if (!p) return;
for (int i = p; i && a[i] != b[i]; i = fa[i]) cir.push_back(i), vis[i] = 1;
for (int i = 1; i <= n; ++i)
if (a[i] != b[i] && !vis[i] && dep[i] > dep[q]) q = i;
if (q) {
reverse(cir.begin(), cir.end());
for (int i = q; i && a[i] != b[i]; i = fa[i]) cir.push_back(i), vis[i] = 1;
} else
q = fa[cir.back()];
for (int i = 1; i <= n; ++i)
if (a[i] != b[i] && !vis[i]) no();
int lc = lca(p, q);
if (a[lc] != b[lc]) no();
if (cir.size() != dep[p] + dep[q] - 2 * dep[lc]) no();
O = lc, cp = p, cq = q;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
for (int i = 1; i <= n; ++i) scanf("%d", &b[i]);
for (int i = 2, u, v; i <= n; ++i) {
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
for (int i = 1; i <= n; ++i)
if (!a[i]) rta = i;
for (int i = 1; i <= n; ++i)
if (!b[i]) rtb = i;
dfs(rtb, 0);
for (int i = rta; i != rtb; i = fa[i]) swap(a[i], a[fa[i]]);
findcir();
if (!cir.size()) {
printf("0 %d\n", dep[rta] - 1);
return 0;
}
int mov = 0;
for (int i = 0; i < cir.size(); ++i)
if (a[cir[i]] == b[cir[0]]) mov = i;
for (int i = 0; i < cir.size(); ++i)
if (a[cir[(i + mov) % cir.size()]] != b[cir[i]]) no();
long long ans = dep[rta] - 1;
int len = cir.size();
memset(vis, 0, sizeof(vis));
for (int i = rta; i; i = fa[i]) vis[i] = 1;
if (vis[cir[0]] || vis[cir.back()]) {
int O = cir.size();
for (int i = 0; i < cir.size(); ++i)
if (!vis[cir[i]]) {
O = i;
break;
}
ans = ans - O +
min((long long)mov * (len + 1) - O,
abs((long long)(len - mov) * (len + 1) + O));
} else {
ans += (long long)min(len - mov, mov) * (len + 1);
}
for (; !vis[O]; O = fa[O]) ans += 2;
if (cp > cq) swap(cp, cq);
printf("%d %d %lld\n", cp, cq, ans);
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1LL << 60;
long long a[200005], b[200005], c[200005];
int main() {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 0; i <= n; ++i) scanf("%lld", a + i);
memcpy(b, a, sizeof a);
int r = n;
for (int i = 0; i < n; ++i) {
if (b[i] % 2 == 0) {
b[i + 1] += b[i] / 2;
} else {
r = i;
break;
}
}
memcpy(c, a, sizeof a);
int l = 0;
for (int i = n; i > 0; --i) {
if (abs(c[i]) < inf) {
c[i - 1] += c[i] * 2;
} else {
l = i;
break;
}
}
int cnt = 0;
for (int i = l; i <= r; ++i) {
if (abs(c[i] + b[i] - 2 * a[i]) <= k) ++cnt;
if (i == n && b[i] + c[i] - 2 * a[i] == 0) --cnt;
}
printf("%d\n", cnt);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int l, i, cnt = 1;
char s[101];
scanf("%s", &s);
l = strlen(s);
sort(s, s + l);
for (i = 0; i < l - 1; i++) {
if (s[i] != s[i + 1]) cnt++;
}
if (cnt % 2 == 1)
printf("IGNORE HIM!\n");
else
printf("CHAT WITH HER!\n");
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5000 + 5;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
int n, m, q;
int dp[2][maxn][3];
pair<int, int> a[maxn];
int main() {
scanf("%d%d", &n, &q);
for (int i = 1; i <= q; i++) scanf("%d%d", &a[i].first, &a[i].second);
sort(a + 1, a + q + 1);
int ans = 0;
memset(dp, -0x3f, sizeof(dp));
dp[0][0][0] = 0;
for (int i = 1; i <= q; i++) {
int id = i % 2;
int lastid = (i + 1) % 2;
int rig = a[i].second;
int lef = a[i].first;
for (int k = 0; k < 3; k++) {
for (int j = 0; j <= n; j++) {
dp[id][max(rig, j)][k] =
max(dp[id][max(rig, j)][k],
dp[lastid][j][k] + min(rig - lef + 1, max(0, rig - j)));
if (k) dp[id][j][k] = max(dp[id][j][k], dp[lastid][j][k - 1]);
ans = max(ans, dp[id][j][2]);
}
}
}
printf("%d\n", ans);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long n, k, ans, a[2333333];
long long sum(int ccd) { return (ccd * (1 + ccd)) / 2; }
int main() {
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) {
if (k > i)
k -= i;
else {
cout << a[k];
return 0;
}
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long MX = 1e5 + 1;
int n, m, ans;
vector<vector<int> > A(MX);
bool vis[MX];
bool solve(int i, int pre = -1) {
if (vis[i]) return false;
vis[i] = true;
int ret = 1;
for (int j = 0; j < (int)A[i].size(); ++j)
if (A[i][j] != pre) ret = ret && solve(A[i][j], i);
return ret;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= m; ++i) {
int a, b;
cin >> a >> b;
A[a].push_back(b);
A[b].push_back(a);
}
for (int i = 1; i <= n; ++i) ans += solve(i);
cout << ans;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
struct str {
string s;
int num;
bool operator<(const str &x) const {
if (x.s == s) return num > x.num;
return s > x.s;
}
} x;
void solve() {
priority_queue<str> q;
string s;
int n;
cin >> s;
cin >> n;
for (int i = 0; i < s.size(); i++) {
x.s = s[i];
x.num = i;
q.push(x);
}
while (!q.empty()) {
x = q.top();
q.pop();
n--;
if (!n) break;
if (x.num < s.size() - 1) {
x.num++;
x.s += s[x.num];
q.push(x);
}
}
if (n)
cout << "No such line.";
else
cout << x.s;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
while (--t >= 0) {
solve();
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string s;
cin >> s;
int n = s.length();
vector<vector<int> > count(26, vector<int>(n + 1, 0));
for (int i = 0; i < 26; i++) {
char c = 'a' + i;
for (int j = 1; j <= n; j++) {
count[i][j] = count[i][j - 1];
if (s[j - 1] == c) count[i][j]++;
}
}
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
if (r - l + 1 < 2)
cout << "Yes\n";
else {
if (s[l - 1] != s[r - 1])
cout << "Yes\n";
else {
int dist = 0;
for (int i = 0; i < 26; i++) {
int quant = count[i][r] - count[i][l - 1];
if (0 < quant) {
dist++;
}
}
if (dist < 3)
cout << "No\n";
else
cout << "Yes\n";
}
}
}
return 0;
}
| 2 |
#include<iostream>
using namespace std;
int main(){
int E,Y;
cin>>E>>Y;
switch(E){
case 0:
if(1868<=Y && Y<=1911){
cout<<"M"<<Y-1867<<endl;
}
else if(1912<=Y && Y<=1925){
cout<<"T"<<Y-1911<<endl;
}
else if(1926<=Y && Y<=1988){
cout<<"S"<<Y-1925<<endl;
}
else if(1989<=Y && Y<=2016){
cout<<"H"<<Y-1988<<endl;
}
break;
case 1:
cout<<Y+1867<<endl;
break;
case 2:
cout<<Y+1911<<endl;
break;
case 3:
cout<<Y+1925<<endl;
break;
case 4:
cout<<Y+1988<<endl;
break;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long MAX_N = 1e5 + 20, md = 998244353;
long long dp[MAX_N][2], f[2 * MAX_N], g[2 * MAX_N], a[MAX_N], b[MAX_N];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long n, k;
cin >> n >> k;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
cin >> a[i / 2];
} else {
cin >> b[i / 2];
}
}
for (int i = n; i < MAX_N; i++) {
a[i] = 0;
b[i] = 0;
}
f[0] = 1;
f[1] = k - 2;
g[0] = 0;
g[1] = k - 1;
for (int i = 2; i < n; i++) {
long long o = f[i - 2], h = g[i - 2], q = f[i - 2];
o *= (k - 2);
o %= md;
o *= (k - 2);
o %= md;
h *= (k - 2);
h %= md;
q *= (k - 1);
q %= md;
f[i] = o + h + q;
f[i] %= md;
o = f[i - 2];
o *= (k - 1);
o %= md;
o *= (k - 2);
o %= md;
h = g[i - 2];
h *= (k - 1);
h %= md;
g[i] = h + o;
g[i] %= md;
}
long long x = -1;
if (a[0] == -1) {
dp[0][0] = k - 1;
if (a[1] <= 0) {
dp[0][0]++;
}
} else {
dp[0][0] = 1;
x = 0;
}
for (int i = 1; i < (n + 1) / 2; i++) {
if (a[i] >= 1) {
x = i;
dp[i][0] = dp[i - 1][0];
if (a[i] == a[i - 1]) {
cout << "0\n";
return 0;
}
} else {
long long z = k - 2;
if (a[i + 1] <= 0 || a[i + 1] == a[i - 1]) {
z++;
}
dp[i][0] = dp[i - 1][0];
dp[i][0] *= z;
dp[i][0] %= md;
if (a[i - 1] != -1) {
continue;
}
if (z == k - 2) {
if (i - 2 >= 0) {
if (a[i - 2] == a[i + 1]) {
continue;
}
}
if (x == -1) {
if (i == 1) {
dp[i][0]++;
dp[i][0] %= md;
} else {
long long hey = f[i - 2];
hey *= (k - 1);
hey %= md;
dp[i][0] += hey + g[i - 2];
dp[i][0] %= md;
}
} else {
if (a[x] == a[i + 1]) {
long long o = g[i - x - 2];
o *= dp[x][0];
o %= md;
dp[i][0] += o;
dp[i][0] %= md;
} else {
long long o = f[i - x - 2];
o *= dp[x][0];
o %= md;
dp[i][0] += o;
dp[i][0] %= md;
}
}
}
}
}
x = -1;
if (b[0] == -1) {
dp[0][1] = k - 1;
if (b[1] <= 0) {
dp[0][1]++;
}
} else {
x = 0;
dp[0][1] = 1;
}
for (int i = 1; i < n / 2; i++) {
if (b[i] >= 1) {
x = i;
dp[i][1] = dp[i - 1][1];
if (b[i] == b[i - 1]) {
cout << "0\n";
return 0;
}
} else {
long long z = k - 2;
if (b[i + 1] <= 0 || b[i + 1] == b[i - 1]) {
z++;
}
dp[i][1] = dp[i - 1][1];
dp[i][1] *= z;
dp[i][1] %= md;
if (b[i - 1] != -1) {
continue;
}
if (z == k - 2) {
if (i - 2 >= 0) {
if (b[i - 2] == b[i + 1]) {
continue;
}
}
if (x == -1) {
if (i == 1) {
dp[i][1]++;
dp[i][1] %= md;
} else {
long long hey = f[i - 2];
hey *= (k - 1);
hey %= md;
dp[i][1] += hey + g[i - 2];
dp[i][1] %= md;
}
} else {
if (b[x] == b[i + 1]) {
long long o = g[i - x - 2];
o *= dp[x][1];
o %= md;
dp[i][1] += o;
dp[i][1] %= md;
} else {
long long o = f[i - x - 2];
o *= dp[x][1];
o %= md;
dp[i][1] += o;
dp[i][1] %= md;
}
}
}
}
}
long long ans = dp[(n - 1) / 2][0];
ans *= dp[(n - 2) / 2][1];
ans %= md;
cout << ans << "\n";
return 0;
}
| 5 |
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;
const int maxx=100010;
vector<int> A[maxx];
int pre[maxx],parent[maxx],low[maxx];
bool flag[maxx];
int v,e,now=1;
void dfs(int u){
flag[u]=true;
low[u]=now;
pre[u]=now;
now++;
for(int i=0;i<A[u].size();i++){
if(flag[A[u][i]]==false) {
parent[A[u][i]]=u;
dfs(A[u][i]);
low[u]=min(low[u],low[A[u][i]]);
}
else if(parent[u]!=A[u][i]){
low[u]=min(low[u],pre[A[u][i]]);
}
}
return ;
}
int main (){
int x,y;
parent[0]=0;
for(int i=0;i<maxx;i++){
flag[i]=false;
}
cin>>v>>e;
while(e--){
cin>>x>>y;
A[x].push_back(y);
A[y].push_back(x);
}
dfs(0);
//for(int i=0;i<v;i++) cout<<i<<" "<<low[i]<<endl;
set<int> ans;
int cnt=0;
for(int i=1;i<v;i++){
int p=parent[i];
if(p==0) cnt++;
else if(pre[p]<=low[i]) ans.insert(p);
}
if(cnt>=2) ans.insert(0);
for(set<int>::iterator it=ans.begin();it!=ans.end();it++) cout<<*it<<endl;
return 0;
}
| 0 |
#include "bits/stdc++.h"
using namespace std;
int gcd(int x, int y)
{
if (x % y)
{
return gcd(y, x % y);
}
else
{
return y;
}
}
int main()
{
int x, y, g;
scanf("%d%d", &x, &y);
g = gcd(x, y);
x /= g;
y /= g;
printf("%d\n", g + 1 + (x + y - 2) * g);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long a[5000007], b[5000007], c[5000007];
long long mod = 1e9 + 7;
long long power(long long x, long long y, long long p) {
long long res = 1;
x = x % p;
if (x == 0) return 0;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
void SieveOfEratosthenes(long long n) {
bool prime[n + 1];
long long j = 0;
memset(prime, true, sizeof(prime));
for (long long p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (long long i = p * p; i <= n; i += p) prime[i] = false;
}
}
for (long long p = 2; p <= n; p++)
if (prime[p]) prime[j++] = p;
}
bool isPrime(long long n) {
if (n <= 1) return false;
for (long long i = 2; i < n; i++)
if (n % i == 0) return false;
return true;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t = 1;
cin >> t;
while (t--) {
long long n, i, c = 0, area = 0, x, flag = 1;
std::map<long long, long long> mp1;
cin >> n;
for (i = 1; i <= 4 * n; i++) cin >> a[i];
sort(a, a + 4 * n + 1);
area = a[1] * a[4 * n];
for (i = 1; i <= n; i++) {
if (a[2 * i] != a[2 * i - 1]) flag = 0;
if (a[4 * n - 2 * i + 1] != a[4 * n - 2 * i + 2]) flag = 0;
if (a[4 * n - 2 * i + 2] * a[2 * i - 1] != area) flag = 0;
if (flag == 0) break;
}
if (flag)
cout << "YES\n";
else
cout << "NO\n";
}
return 0;
}
| 2 |
#include <iostream>
using namespace std;
int n,c,ans = 1000000000;
int d[1001][1001];
int cc[501][501];
int co[3][31];
int main()
{
cin >> n >> c;
for(int i = 1;i <= c;i++){
for(int j = 1;j <= c;j++){
cin >> d[i][j];
}
}
for(int i = 1;i <= n;i++){
for(int j = 1;j <= n;j++){
cin >> cc[i][j];
co[(i+j)%3][cc[i][j]]++;
}
}
for(int i = 1;i <= c;i++){
for(int j = 1;j <= c;j++){
for(int k = 1;k <= c;k++){
if(i == j || j == k || k == i) continue;
int ai = 0,aj = 0,ak = 0;
for(int a = 1;a <= c;a++){
ai += co[0][a] * d[a][i];
aj += co[1][a] * d[a][j];
ak += co[2][a] * d[a][k];
}
if(ans > ai + aj + ak)
ans = ai + aj + ak;
}
}
}
cout << ans << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int a[10010], n, l, r, k;
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> l >> r;
for (int j = l; j <= r; j++) a[j] = n - i;
}
cin >> k;
cout << a[k];
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[] = {1, 0, -1, 0, 1, -1, 1, -1};
int n, m;
char a[300][300];
int u[300][300];
void dfs(int x, int y, int& t) {
t++;
u[x][y] = 1;
a[x][y] = '2';
for (int i = 0; i < 8; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (nx >= 0 && nx < n && ny >= 0 && ny < m && a[nx][ny] == '1' &&
u[nx][ny] == 0)
dfs(nx, ny, t);
}
}
bool solve(int x, int y, int temp, int z, int w) {
for (int i = 0; i <= temp; i++) {
for (int j = z; j <= w; j++) {
int nx = x + dx[j] * i;
int ny = y + dy[j] * i;
if (nx < 0 || nx >= n || ny < 0 || ny >= m || a[nx][ny] != '2' ||
u[nx][ny] != 1)
return false;
}
}
return true;
}
int main() {
int t;
cin >> t;
while (t--) {
memset(u, 0, sizeof(u));
memset(a, 0, sizeof(a));
int sum = 0;
cin >> n >> m;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
if (a[i][j] == '1' && u[i][j] == 0) {
int d = 0;
dfs(i, j, d);
if (d % 4 || d / 4 > 250) continue;
if (solve(i, j, d / 4, 0, 1) &&
solve(i + d / 4, j + d / 4, d / 4, 2, 3))
sum++;
if (solve(i, j, d / 4, 4, 5) && solve(i + d / 2, j, d / 4, 6, 7))
sum++;
}
}
cout << sum << endl;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int M = 2e5 + 1;
int inf = 1e9 + 1;
int n, a[29][29], maxi, j;
int main() {
ios::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
string s;
cin >> n;
cin >> s;
for (int i = 0; i < n - 1; i++) {
a[int(s[i]) - 65][int(s[i + 1]) - 65]++;
if (maxi < a[int(s[i]) - 65][int(s[i + 1]) - 65])
maxi = a[int(s[i]) - 65][int(s[i + 1]) - 65], j = i;
}
cout << s[j] << s[j + 1];
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main(){
int N,K; cin >> N >> K;
int x;
int sum;
for(int i=0; i<N; i++){
cin >> x;
sum+=(2*min(x,abs(K-x)));
}
cout << sum << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
char ans[55][55];
long long a, b, c, d;
void input() { cin >> a >> b >> c >> d; }
void output() {}
void solve() {
for (long long i = 1; i <= 25; i++) {
for (long long j = 1; j <= 25; j++) {
ans[i][j] = 'A';
}
}
for (long long i = 1; i <= 25; i++) {
for (long long j = 26; j <= 50; j++) {
ans[i][j] = 'B';
}
}
for (long long i = 26; i <= 50; i++) {
for (long long j = 1; j <= 25; j++) {
ans[i][j] = 'C';
}
}
for (long long i = 26; i <= 50; i++) {
for (long long j = 26; j <= 50; j++) {
ans[i][j] = 'D';
}
}
long long j = 26, k = 26;
for (long long i = 2; i <= a; i++) {
ans[j][k] = 'A';
if (j < 50)
j += 2;
else {
j = 26;
k += 2;
}
}
j = 1;
k = 1;
for (long long i = 2; i <= d; i++) {
ans[j][k] = 'D';
if (j < 25)
j += 2;
else {
j = 1;
k += 2;
}
}
j = 26;
k = 1;
for (long long i = 2; i <= b; i++) {
ans[j][k] = 'B';
if (j < 50)
j += 2;
else {
j = 26;
k += 2;
}
}
j = 1;
k = 26;
for (long long i = 2; i <= c; i++) {
ans[j][k] = 'C';
if (j < 25)
j += 2;
else {
j = 1;
k += 2;
}
}
cout << "50 50" << endl;
for (long long i = 1; i <= 50; i++) {
for (long long j = 1; j <= 50; j++) cout << ans[i][j];
cout << endl;
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
input();
solve();
output();
}
| 3 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int getrnd(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); }
template <typename T1, typename T2>
bool relax(T1& a, const T2& b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <typename T1, typename T2>
bool strain(T1& a, const T2& b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
const int MOD = 1e9 + 7;
int add(int a, int b) {
a += b;
if (a >= MOD) a -= MOD;
if (a < 0) a += MOD;
return a;
}
int mul(int a, int b) { return 1LL * a * b % MOD; }
const int N = 2e5 + 3;
int n;
ll tree[4 * N], mtree[4 * N];
void build(int v, int tl, int tr, vector<ll>& a, vector<ll>& b) {
if (tl == tr) {
tree[v] = a[tl];
mtree[v] = mul(a[tl], b[tl]);
} else {
int tm = (tl + tr) / 2;
build(2 * v, tl, tm, a, b);
build(2 * v + 1, tm + 1, tr, a, b);
tree[v] = tree[2 * v] + tree[2 * v + 1];
mtree[v] = add(mtree[2 * v], mtree[2 * v + 1]);
}
}
void update(int v, int tl, int tr, int pos, ll x, vector<ll>& b) {
if (tl == tr) {
tree[v] = x;
mtree[v] = mul(b[tl], x);
} else {
int tm = (tl + tr) / 2;
if (pos <= tm)
update(2 * v, tl, tm, pos, x, b);
else
update(2 * v + 1, tm + 1, tr, pos, x, b);
tree[v] = tree[2 * v] + tree[2 * v + 1];
mtree[v] = add(mtree[2 * v], mtree[2 * v + 1]);
}
}
ll get(int v, int tl, int tr, int l, int r) {
if (l > r) return 0;
if (tl == l && tr == r)
return tree[v];
else {
int tm = (tl + tr) / 2;
return get(2 * v, tl, tm, l, min(r, tm)) +
get(2 * v + 1, tm + 1, tr, max(l, tm + 1), r);
}
}
ll getm(int v, int tl, int tr, int l, int r) {
if (l > r) return 0;
if (tl == l && tr == r)
return mtree[v];
else {
int tm = (tl + tr) / 2;
return add(getm(2 * v, tl, tm, l, min(r, tm)),
getm(2 * v + 1, tm + 1, tr, max(tm + 1, l), r));
}
}
int find(int v, int tl, int tr, int l, int r, ll x) {
if (l > r) return -1;
if (tl == tr)
return tl;
else {
int tm = (tl + tr) / 2;
ll s = get(1, 0, n - 1, l, tm);
if (s >= x)
return find(2 * v, tl, tm, l, min(r, tm), x);
else
return find(2 * v + 1, tm + 1, tr, max(tm + 1, l), r, x - s);
}
}
void solve() {
int q;
cin >> n >> q;
vector<ll> a(n), w(n), y(n);
for (int i = 0; i < n; ++i) cin >> a[i], y[i] = add(a[i], -i);
for (int i = 0; i < n; ++i) cin >> w[i];
build(1, 0, n - 1, w, y);
while (q--) {
int l, r;
cin >> l >> r;
if (l < 0) {
update(1, 0, n - 1, -l - 1, r, y);
} else {
--l;
--r;
ll s = get(1, 0, n - 1, l, r);
int pos = find(1, 0, n - 1, l, r, (s + 1) / 2);
assert(pos != -1);
ll x = y[pos];
ll res =
add(-getm(1, 0, n - 1, l, pos - 1), getm(1, 0, n - 1, pos + 1, r));
res = add(res, mul(add(get(1, 0, n - 1, l, pos - 1) % MOD,
-(get(1, 0, n - 1, pos + 1, r) % MOD)),
x));
cout << res << '\n';
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(nullptr);
cout.tie(nullptr);
srand(time(0));
int t = 1;
while (t--) solve();
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x;
scanf("%d%d", &n, &x);
if (n == 3)
printf(">vv\n^<.\n^.<\n1 3\n");
else if (n == 5)
printf(">...v\nv.<..\n..^..\n>....\n..^.<\n1 1\n");
else {
for (int k = 1; k <= 50; k++) {
for (int i = 1; i <= 49; i++) printf(">");
for (int i = 1; i <= 50; i++)
if (i & 1)
printf(".");
else
printf(">");
printf("v\n");
printf("^");
for (int i = 1; i <= 50; i++)
if (i & 1)
printf("<");
else
printf(".");
for (int i = 1; i <= 49; i++) printf("<");
printf("\n");
}
printf("1 100\n");
}
return 0;
}
| 4 |
#include <stdio.h>
#include <algorithm>
using namespace std;
int main(){
int n,a[100000];
long long int ans;
while(1){
scanf("%d",&n);
if(n==0)return 0;
ans=0;
for(int i=0;i<n;i++)scanf("%d",&a[i]);
sort(a,a+n);
for(int i=0;i<n-1;i++){
ans+=a[i]*(n-1-i);
}
printf("%lld\n",ans);
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
bool a[2][100005];
int main() {
int n, q;
cin >> n >> q;
int constraints_violated = 0;
for (int i = 0; i < q; i++) {
int r, c;
cin >> r >> c;
r--;
c--;
a[r][c] = !a[r][c];
if (a[r][c]) {
if (c > 0) {
if (a[1 - r][c - 1]) {
constraints_violated++;
}
}
if (a[1 - r][c]) {
constraints_violated++;
}
if (c < n - 1) {
if (a[1 - r][c + 1]) {
constraints_violated++;
}
}
}
if (!a[r][c]) {
if (c > 0) {
if (a[1 - r][c - 1]) {
constraints_violated--;
}
}
if (a[1 - r][c]) {
constraints_violated--;
}
if (c < n - 1) {
if (a[1 - r][c + 1]) {
constraints_violated--;
}
}
}
if (constraints_violated == 0) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
long long a[maxn], t, n;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> t;
while (t--) {
cin >> n;
long long mi = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (a[mi] > a[i])
mi = i;
}
long long mii = a[mi];
if (n == 1) {
cout << 0 << endl;
continue;
}
if (mi != 0) {
cout << n << endl;
cout << 1 << ' ' << mi + 1 << ' ' << mii << ' ' << mii + mi << endl;
}
else {
cout << n - 1 << endl;
}
for (long long i = 1; i < n; i++) {
cout << 1 << ' ' << i + 1 << ' ' << mii << ' ' << mii + i << endl;
}
}
return 0;
}
| 2 |
#include <cstdio>
#include <queue>
using namespace std;
char s[200001];
int main() {
int n, k, c = 0, f = 0, i, j;
deque <int> q;
scanf("%d %d", &n, &k);
scanf("%s", s);
for (i = 0; i < n; i++) q.push_back(s[i] - 'A');
for (i = 0; i < k; i++) {
if ((q[0] + f) % 2 == 0) {
q[0] = 1 - q[0];
} else {
f++;
q.pop_front();
q.push_back(f % 2);
if (f == n) {
if (n % 2 == 1 && (k - i - 1) % 2 == 1) q[0] = 1 - q[0];
break;
}
}
}
for (i = 0; i < n; i++) putchar('A' + (q[i] + f) % 2);
puts("");
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, a;
cin >> n;
vector<int> vec;
for (int i = 0; i < n; i++) {
cin >> a;
vec.push_back(a);
}
vector<int> ans;
ans.resize(n);
int ch = 0;
if (n % 2 == 1) {
ans[n / 2] = vec[n / 2];
}
for (int j = n / 2 - 1; j >= 0; j--) {
if ((j + 1) % 2 == 1) {
ans[j] = vec[n - j - 1];
ans[n - j - 1] = vec[j];
} else {
ans[j] = vec[j];
ans[n - j - 1] = vec[n - j - 1];
}
}
for (auto t : ans) {
cout << t << " ";
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main()
{
double a,b,c,d,S;cin>>a>>b>>c;
S=(0.5)*a*b*sin(M_PI*c/180);
d=sqrt(a*a+b*b-2*a*b*cos(M_PI*c/180));
printf("%.8lf\n",S);
printf("%.8lf\n",a+b+d);
printf("%.8lf\n",2*S/a);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
vector<long long> primes;
vector<long long> primes1;
vector<long long> primes2;
vector<long long> calc(long long val, vector<long long> prim) {
vector<long long> results;
results.push_back(1);
for (long long p : prim) {
vector<long long> r;
for (long long res : results) {
long long temp = p;
while (temp <= val / res + 1) {
r.push_back(temp * res);
if (temp > val / p + 1) break;
temp *= p;
}
}
for (long long x : r) results.push_back(x);
}
return results;
}
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
primes.push_back(x);
}
sort(primes.begin(), primes.end());
for (unsigned int i = 0; i < primes.size(); i++) {
if (i % 2)
primes1.push_back(primes[i]);
else
primes2.push_back(primes[i]);
}
vector<long long> a = calc(1e18, primes1);
vector<long long> b = calc(1e18, primes2);
sort(a.begin(), a.end());
sort(b.begin(), b.end());
long long k;
cin >> k;
long long beg = 1;
long long end = 1e18;
long long sol;
while (1) {
long long c = (beg + end) / 2;
int first = lower_bound(a.begin(), a.end(), c) - a.begin();
int second = lower_bound(b.begin(), b.end(), c) - b.begin();
if (first == a.size()) first--;
if (second == b.size()) second--;
int i = 0;
int j = second;
long long amount = 0;
long long maxi = 0;
while (i <= first && j >= 0) {
if (a[i] > c / b[j]) {
j--;
} else {
maxi = max(maxi, a[i] * b[j]);
amount += (j + 1);
i++;
}
}
if (amount < k) {
beg = c + 1;
} else {
end = c;
}
if (amount == k) {
sol = maxi;
break;
}
}
cout << sol << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[]) {
int n, i, h, m = 0, t = 0;
scanf("%d", &n);
stack<int> s;
for (i = 1; i <= n; ++i) {
scanf("%d", &h);
m = max(m, h);
if (!s.empty() && s.top() == h)
s.pop();
else if (!s.empty() && h > s.top()) {
s.pop();
s.push(h);
t++;
} else
s.push(h);
}
if (t) printf("NO\n");
if (!t) {
if (s.size() > 1)
printf("NO\n");
else if (s.size() == 1 && s.top() < m)
printf("NO\n");
else
printf("YES\n");
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
class cmp_vec {
long long _i;
bool _incr;
public:
cmp_vec(long long i, bool incr) : _i(i), _incr(incr) { ; }
bool operator()(const vector<long long> &v1, const vector<long long> &v2) {
if (_incr)
return v1[_i] < v2[_i];
else
return v1[_i] > v2[_i];
}
};
template <typename T>
void popHeap(vector<vector<T> > &v, cmp_vec &cmp) {
pop_heap(v.begin(), v.end(), cmp);
v.pop_back();
}
template <typename T>
void pushHeap(vector<vector<T> > &v, vector<T> &val, cmp_vec &cmp) {
v.push_back(val);
push_heap(v.begin(), v.end(), cmp);
}
bool all_empty(vector<vector<long long> > qq[2][2]) {
for (int i = 0; i < 4; i++)
if (!qq[i / 2][i % 2].empty()) return false;
return true;
}
int main() {
int N, M;
cin >> N >> M;
vector<vector<long long> > q(N, vector<long long>(5, 0));
vector<vector<long long> > r;
for (int i = 0; i < N; i++) {
cin >> q[i][0] >> q[i][1] >> q[i][2];
q[i][3] = i;
}
sort(q.begin(), q.end(), cmp_vec(0, true));
vector<vector<long long> > qq[2][2];
cmp_vec fns[2][2] = {{cmp_vec(2, true), cmp_vec(1, true)},
{cmp_vec(2, false), cmp_vec(1, false)}};
for (long long i = 0, c = 1, t = q[i][0]; (i < q.size()) || !all_empty(qq);) {
while (i < q.size() && q[i][0] == t) {
if (q[i][1] == c) {
pushHeap(qq[q[i][2] > c][0], q[i], fns[q[i][2] > c][0]);
} else {
pushHeap(qq[q[i][1] > c][1], q[i], fns[q[i][1] > c][1]);
}
i++;
}
long long p = (long long)(qq[0][0].size() + qq[0][1].size()) -
(long long)(qq[1][0].size() + qq[1][1].size());
long long dir = p <= 0, sign = p <= 0 ? 1 : -1;
while (!qq[dir][0].empty() && qq[dir][0][0][2] == c) {
qq[dir][0][0][4] = t;
r.push_back(qq[dir][0][0]);
popHeap(qq[dir][0], fns[dir][0]);
}
while (!qq[1 - dir][0].empty() && qq[1 - dir][0][0][2] == c) {
qq[1 - dir][0][0][4] = t;
r.push_back(qq[1 - dir][0][0]);
popHeap(qq[1 - dir][0], fns[1 - dir][0]);
}
while (!qq[1 - dir][1].empty() && qq[1 - dir][1][0][1] == c) {
if (sign * qq[1 - dir][1][0][2] >= sign * c) {
pushHeap(qq[dir][0], qq[1 - dir][1][0], fns[dir][0]);
} else {
pushHeap(qq[1 - dir][0], qq[1 - dir][1][0], fns[1 - dir][0]);
}
popHeap(qq[1 - dir][1], fns[1 - dir][1]);
}
while (!qq[dir][1].empty() && c == qq[dir][1][0][1]) {
if (sign * qq[dir][1][0][2] < sign * c) {
pushHeap(qq[1 - dir][0], qq[dir][1][0], fns[1 - dir][0]);
} else {
pushHeap(qq[dir][0], qq[dir][1][0], fns[dir][0]);
}
popHeap(qq[dir][1], fns[dir][1]);
}
p = (long long)(qq[0][0].size() + qq[0][1].size()) -
(long long)(qq[1][0].size() + qq[1][1].size());
dir = p <= 0, sign = (p <= 0) ? 1 : -1;
long long tran =
!qq[dir][1].empty() ? (abs(c - qq[dir][1][0][1]) + t) : LLONG_MAX;
long long drop =
!qq[dir][0].empty() ? (abs(c - qq[dir][0][0][2]) + t) : LLONG_MAX;
long long pick = (i < q.size()) ? (abs(t - q[i][0]) + t) : LLONG_MAX;
long long tt = min(tran, min(drop, pick));
if (!all_empty(qq)) c += sign * abs(t - tt);
t += abs(t - tt);
}
sort(r.begin(), r.end(), cmp_vec(3, true));
for (int i = 0; i < r.size(); i++) {
cout << r[i][4] << endl;
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 10;
const int MOD = 1e9 + 7;
struct star {
int from, next, to, id;
};
int n, m, num[MAXN], fa[MAXN], d, head[MAXN], len;
star g[MAXN * 2];
bool choose[MAXN], vis[MAXN];
int getfather(int x) { return fa[x] == x ? x : fa[x] = getfather(fa[x]); }
void add(int x, int y) {
x = getfather(x);
y = getfather(y);
fa[y] = x;
return;
}
void init() {
memset(head, -1, sizeof(head));
len = 0;
for (int i = 1; i <= n; ++i) {
fa[i] = i;
}
memset(num, 0, sizeof(num));
memset(choose, false, sizeof(choose));
memset(vis, false, sizeof(vis));
}
void adde(int u, int v, int id) {
g[++len].to = v;
g[len].id = id;
g[len].from = u;
g[len].next = head[u];
head[u] = len;
}
int main() {
scanf("%d%d%d", &n, &m, &d);
init();
int need = n - 1 - d;
for (int i = 1; i <= m; ++i) {
int u, v;
scanf("%d%d", &u, &v);
++num[u];
++num[v];
adde(u, v, i);
adde(v, u, i);
if (u != 1 && v != 1) {
add(u, v);
}
}
int p = 0, p2 = 0;
for (int i = 2; i <= n; ++i) {
if (getfather(i) == i) {
++p;
}
}
if (d < p || num[1] < p) {
printf("NO\n");
return 0;
}
for (int i = head[1]; i != -1; i = g[i].next) {
if (getfather(1) != getfather(g[i].to)) {
add(1, g[i].to);
++p2;
--d;
choose[g[i].id] = true;
}
}
if (p != p2) {
printf("NO\n");
return 0;
}
for (int i = head[1]; i != -1 && d > 0; i = g[i].next) {
if (!choose[g[i].id]) {
choose[g[i].id] = true;
--d;
}
}
for (int i = 1; i <= n; ++i) {
fa[i] = i;
}
for (int i = head[1]; i != -1; i = g[i].next) {
if (choose[g[i].id]) {
add(1, g[i].to);
}
}
for (int i = 1; i <= len; i += 2) {
if (g[i].from != 1 && g[i].to != 1 && !choose[g[i].id] &&
getfather(g[i].from) != getfather(g[i].to)) {
choose[g[i].id] = true;
add(g[i].from, g[i].to);
--need;
}
}
if (need == 0) {
printf("YES\n");
for (int i = 1; i <= len; i += 2) {
if (choose[g[i].id]) {
printf("%d %d\n", g[i].from, g[i].to);
}
}
} else {
printf("NO\n");
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
int main() {
int w,h,n;cin>>w>>h>>n;
int s=0;
int t=0;
rep(i,n){
int x,y,a;cin>>x>>y>>a;
if(a==2)w=min(w,x);
if(a==1)s=max(s,x);
if(a==4)h=min(h,y);
if(a==3)t=max(t,y);
}
int ans = max((w-s)*(h-t),0);
if((w-s)<=0 ||(h-t)<=0)ans=0;
cout<<ans<<endl;
return 0;
}
| 0 |
/**** There's three kinds of edges in a di-graph dfs traversal. Like undirected graphs, recursively traversed edges
* are called tree edges, going to back to an ancestor is known as a back link, going back to an descendent is a down link
* and going to visited node which is neither an ancestor or a descendent is known as a cross edge.
* Any back links (which in turn means down links) results in cycles *****/
#include <iostream>
#include <vector>
using namespace std;
vector<int> nodes[100100];
int pre[100100] = {0};
int post[100100] = {0};
int ith = 0;
bool hasCycle(int s)
{
pre[s] = ++ith;
for (auto i : nodes[s])
{
if (!pre[i])
{
if (hasCycle(i))
{
return true;
}
}
else if (post[i] == 0)
{
return true;
}
}
post[s] = ++ith;
return false;
}
int main()
{
int e, n;
cin >> n >> e;
for (int i = 0; i < e; ++i)
{
int x, y;
cin >> x >> y;
nodes[x].push_back(y);
}
for (int i = 0; i < n; ++i)
{
for (unsigned i = 0; i < n; ++i)
{
pre[i] = 0;
post[i] = 0;
}
if (hasCycle(i))
{
cout << 1 << "\n";
break;
}
else if (i == n - 1)
{
cout << 0 << "\n";
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, m, a[2000][2000], ans = -1, mx, my;
char s[200];
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> s;
for (int j = 0; j <= m - 1; j++) {
if (s[j] == 'W')
a[i][j + 1] = 0;
else
a[i][j + 1] = 1;
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
for (int k = 0; k <= max(n, m); k++) {
if (i - k < 0 || j - k < 0) continue;
if (a[i + k][j] + a[i - k][j] + a[i][j - k] + a[i][j + k] +
a[i - k][j - k] + a[i - k][j + k] + a[i + k][j - k] +
a[i + k][j + k] ==
8) {
if (k > ans) {
ans = k;
mx = i, my = j;
}
}
}
}
}
cout << mx << " " << my << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int a[100000];
int b[100000];
char s[10000];
int main() {
int n;
scanf("%d%s", &n, s);
;
int sit = 0;
for (int i = 0; i < n; i++) {
if (s[i] == 'x') sit++;
}
int rez = 0;
for (int i = 0; i < n; i++) {
if (s[i] == 'x' && sit > n / 2) s[i] = 'X', sit--, rez++;
if (s[i] == 'X' && sit < n / 2) s[i] = 'x', sit++, rez++;
}
printf("%d\n%s", rez, s);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, i, t, m, mx = 0, T, ans = 0, r, k = 0, p = 0, q, x, y, l;
string s;
cin >> s;
l = s.size();
map<char, long long int> make_pair;
for (i = 0; i < l; i++) {
make_pair[s[i]]++;
}
char j;
for (j = 'a'; j <= 'z'; j++) {
if (make_pair[j] % 2 != 0) k++;
}
if (k >= 1) k--;
if (k % 2 != 0)
cout << "Second\n";
else
cout << "First\n";
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 9223372036854775807ll;
const int inf = 2147483647, nmax = 500005, M = 1000000007;
long long n, v[nmax], aib1[nmax], aib2[nmax];
pair<int, int> e[nmax];
void update(long long idx, long long val, long long aib[]) {
while (idx <= n) {
aib[idx] = (aib[idx] + val) % M;
idx += (idx & (-idx));
}
}
long long read(long long idx, long long aib[]) {
long long sum = 0;
while (idx) {
sum = (sum + aib[idx]) % M;
idx -= (idx & (-idx));
}
return sum;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> v[i];
e[i] = {v[i], i};
}
sort(e + 1, e + n + 1);
long long sol = 0;
for (int i = 1; i <= n; ++i) {
long long pos = e[i].second;
long long sum = (pos * (n - pos + 1)) % M;
sol = (sol + sum * e[i].first) % M;
sum = (read(pos - 1, aib1) * (n - pos + 1)) % M;
sol = (sol + sum * e[i].first) % M;
sum = read(n, aib2) - read(pos, aib2);
if (sum < 0) sum += M;
sum = (sum * pos) % M;
sol = (sol + sum * e[i].first) % M;
update(pos, pos, aib1);
update(pos, n - pos + 1, aib2);
}
cout << sol;
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
vector<int >times;
map<int,int >id;
priority_queue<int>q;
int main()
{
int t;
cin >> t;
while (t--)
{
times.clear();
id.clear();
int n;
cin>>n;
for(int i=1;i<=n;i++){
int a;
cin>>a;
if(!id.count(a)){
id[a]=id.size();
times.push_back(1);
}
else times[id[a]]++;
}
for(int i=0;i<times.size();i++){
q.push(times[i]);
}
while(q.size()>=2){
int a1=q.top();
q.pop();
int a2=q.top();
q.pop();
a1--;
a2--;
if(a1) q.push(a1);
if(a2) q.push(a2);
}
if(!q.empty()){
cout<<q.top()<<endl;
q.pop();
}
else cout<<0<<endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> ed(n - 1, 0);
for (int i = 0; i < n - 1; i++) cin >> ed[i], ed[i]--;
vector<int> nd(n, 0);
cout << ed[0] + 1 << endl;
int cur = n - 1;
for (int i = 0; i < n - 1; i++) {
nd[ed[i]]++;
while (nd[cur]) cur--;
if (i == n - 2 || nd[ed[i + 1]]) {
nd[cur]++;
cout << ed[i] + 1 << " " << cur + 1 << endl;
} else
cout << ed[i + 1] + 1 << " " << ed[i] + 1 << endl;
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int N = 5e6 + 10;
const unsigned long long p = 233;
int n, f[N], ans;
char s[N];
unsigned long long ha[N], pows[N], fha[N];
unsigned long long ask(int l, int r) {
return ha[r] - ha[l - 1] * pows[r - l + 1];
}
unsigned long long fask(int l, int r) {
return fha[l] - fha[r + 1] * pows[r - l + 1];
}
int main() {
scanf("%s", s + 1);
n = strlen(s + 1);
pows[0] = 1;
for (int i = 1; i <= n; i++) {
pows[i] = pows[i - 1] * p;
ha[i] = ha[i - 1] * p + (s[i] - 'a');
}
for (int i = n; i >= 1; i--) fha[i] = fha[i + 1] * p + (s[i] - 'a');
ans = f[1] = 1;
for (int i = 2; i <= n; i++)
if (ask(1, i / 2) == fask((i + 1) / 2 + 1, i))
f[i] = f[i / 2] + 1, ans += f[i];
printf("%d", ans);
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
struct qe {
int type;
int left, right;
};
class Queries {
int n, q, m;
int a[200001];
qe lq[200001];
int b[101];
public:
void read() {
cin >> n >> q >> m;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= q; ++i) cin >> lq[i].type >> lq[i].left >> lq[i].right;
for (int i = 1; i <= m; ++i) cin >> b[i];
}
void do_querry() {
for (int i = 1; i <= m; ++i) {
for (int j = q; j > 0; --j) {
if (lq[j].left <= b[i] && b[i] <= lq[j].right) {
if (lq[j].type == 1) {
if (b[i] == lq[j].left)
b[i] = lq[j].right;
else
b[i]--;
} else
b[i] = lq[j].right + lq[j].left - b[i];
}
}
}
}
void show() {
for (int i = 1; i <= m; ++i) cout << a[b[i]] << ' ';
}
};
int main() {
Queries qe;
qe.read();
qe.do_querry();
qe.show();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
unordered_map<int, int> d1, d2;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
d1[x + y]++;
d2[x - y]++;
}
long long ans = 0;
for (auto a : d1) {
long long val = a.second;
ans += (val * (val - 1)) / 2;
}
for (auto a : d2) {
long long val = a.second;
ans += (val * (val - 1)) / 2;
}
cout << ans;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long MODULE = 1000000009LL;
int n, m;
long long m2 = 1, ans = 1;
int main(void) {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; ++i) {
m2 <<= 1;
if (m2 > MODULE) m2 %= MODULE;
}
for (int i = 1; i <= n; ++i) {
ans = (ans * ((m2 + MODULE - i) % MODULE)) % MODULE;
}
cout << ans << endl;
return 0;
}
| 3 |
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2")
#include <bits/stdc++.h>
using namespace std;
int l[2009],r[2009];
int inter(int &lo, int &hi, int &ind)
{
return max(min(hi,r[ind])-max(lo,l[ind])+1,0);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,m,k;
cin>>n>>m>>k;
for(int i=0;i<m;i++) cin>>l[i]>>r[i];
int ans=0;
int v=n-k+2;
for(int i=1;i<v;i++)
for(int j=1;j<v;j++)
{
int cnt=0;
int t=i+k-1;
int t2=j+k-1;
for(int ii=0;ii<m;ii++)
cnt += max(inter(i,t,ii),inter(j,t2,ii));
ans=max(ans,cnt);
}
cout<<ans<<'\n';
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
long long n, x, y, a = -1, b = 0, i, j;
int main() {
cin >> x >> y >> n;
for (i = 1; i <= n; i++) {
j = i * x / y;
if (x * i - j * y > (j + 1) * y - x * i) j++;
if (a == -1 || abs(a * y - x * b) * i > abs(j * y - x * i) * b)
a = j, b = i;
}
cout << a << '/' << b << '\n';
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long exp(long long base, long long expz, long long mod) {
long long res = 1;
while (expz > 0) {
if (expz % 2 == 1) res = (res * base) % mod;
base = (base * base) % mod;
expz /= 2;
}
return res % mod;
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
long long n;
cin >> n;
long long a[n], b[n];
map<long long, long long> mp;
for (long long i = 0; i < n; i++) {
cin >> a[i];
mp[a[i]]++;
}
long long c = 0;
for (long long i = 0; i < n; i++) {
cin >> b[i];
mp[b[i]]++;
if (a[i] == b[i]) c++;
}
long long m;
cin >> m;
long long res = 1;
long long fac[2 * n + 1];
long long two[2 * n + 1];
fac[0] = 1;
two[0] = 0;
for (long long i = 1; i < 2 * n + 1; i++) {
long long num = i;
two[i] = two[i - 1];
while (num % 2 == 0) {
num /= 2;
two[i]++;
}
fac[i] = num * fac[i - 1];
fac[i] %= m;
}
for (auto i : mp) {
long long num = i.second;
res *= fac[num];
res %= m;
if (c == 0) {
res *= exp(2, two[num], m);
res %= m;
continue;
}
if (c >= two[num]) {
c -= two[num];
} else {
long long op = two[num] - c;
c = 0;
res *= exp(2, op, m);
}
res %= m;
}
cout << res << "\n";
return 0;
}
| 4 |
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
inline int read() {
int Res = 0, f = 1;
char ch = getchar();
while (ch > '9' || ch < '0') f = (ch == '-' ? -f : f), ch = getchar();
while (ch >= '0' && ch <= '9') Res = Res * 10 + ch - '0', ch = getchar();
return Res * f;
}
int N, G[2505][2505], Ans[2505][2505], low[2505], pre[2505];
bool vis[2505];
int tot, lnk[2505], son[5005], nxt[5005], w[5005];
inline void add(int x, int y, int z) {
son[++tot] = y;
nxt[tot] = lnk[x];
lnk[x] = tot;
w[tot] = z;
}
void Prim() {
vis[1] = 1;
for (int i = 2; i <= N; i++) low[i] = G[1][i], pre[i] = 1;
for (int i = 1; i < N; i++) {
int Min = 1e9, id = 0;
for (int j = 1; j <= N; j++)
if (!vis[j] && low[j] < Min) Min = low[j], id = j;
add(id, pre[id], Min);
add(pre[id], id, Min);
vis[id] = 1;
for (int j = 1; j <= N; j++)
if (!vis[j] && low[j] > G[j][id]) low[j] = G[j][id], pre[j] = id;
}
}
void dfs(int x, int fa, int S, int Max) {
Ans[S][x] = Ans[x][S] = Max;
for (int i = lnk[x]; i; i = nxt[i]) {
if (son[i] == fa) continue;
dfs(son[i], x, S, max(Max, w[i]));
}
}
int main() {
N = read();
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) G[i][j] = read();
}
for (int i = 1; i <= N; i++)
if (G[i][i]) return puts("NOT MAGIC"), 0;
for (int i = 1; i <= N; i++) {
for (int j = i + 1; j <= N; j++)
if (G[i][j] != G[j][i]) return puts("NOT MAGIC"), 0;
}
Prim();
for (int i = 1; i <= N; i++) dfs(i, 0, i, 0);
for (int i = 1; i <= N; i++) {
for (int j = i; j <= N; j++)
if (Ans[i][j] != G[i][j]) return puts("NOT MAGIC"), 0;
}
puts("MAGIC");
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
void IO() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
const int N = 2e5 + 1;
int n;
vector<int> adj[N];
bool vis[N];
void dfs(int node) {
vis[node] = true;
for (int v : adj[node]) {
if (!vis[v]) dfs(v);
}
}
int main() {
IO();
cin >> n;
for (int i = 1; i <= n; i++) {
int parent;
cin >> parent;
adj[i].push_back(parent);
adj[parent].push_back(i);
}
int countTrees = 0;
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
countTrees++;
dfs(i);
}
}
cout << countTrees << '\n';
;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
void debug_out() { cerr << '\n'; }
template <typename T, typename... R>
void debug_out(const T &f, const R &...r) {
cerr << f << " ";
debug_out(r...);
}
inline long long read() {
long long x = 0;
int f = 0;
char ch = getchar();
while (ch < '0' || ch > '9') f |= (ch == '-'), ch = getchar();
while (ch >= '0' && ch <= '9')
x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
return x = f ? -x : x;
}
inline void write(long long x) {
if (x == 0) {
putchar('0');
return;
}
if (x < 0) {
putchar('-');
x = -x;
}
static char s[23];
int l = 0;
while (x != 0) s[l++] = x % 10 + 48, x /= 10;
while (l) putchar(s[--l]);
}
int lowbit(int x) { return x & (-x); }
template <class T>
T big(const T &a1, const T &a2) {
return a1 > a2 ? a1 : a2;
}
template <typename T, typename... R>
T big(const T &f, const R &...r) {
return big(f, big(r...));
}
template <class T>
T sml(const T &a1, const T &a2) {
return a1 < a2 ? a1 : a2;
}
template <typename T, typename... R>
T sml(const T &f, const R &...r) {
return sml(f, sml(r...));
}
template <class T, class U>
inline void checkMin(T &x, U y) {
if (y < x) x = y;
}
template <class T, class U>
inline void checkMax(T &x, U y) {
if (y > x) x = y;
}
template <class T, class U>
inline bool ifMax(T &x, U y) {
if (y > x) return x = y, true;
return false;
}
template <class T, class U>
inline bool ifMin(T &x, U y) {
if (y < x) return x = y, true;
return false;
}
const int M = 212345;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1000000007;
namespace RMQ {
const int MX = M;
const int P = 17;
int MIN[MX][P], MAX[MX][P];
int lg[MX];
int A[MX];
void RMQ_init1() {
for (int i = 1, j = 0; i < MX; ++i) {
if (i <= (1 << (j + 1)))
lg[i] = j;
else
lg[i] = ++j;
}
}
void RMQ_init2(int n) {
for (int i = 0; i < n + 1; ++i) {
MAX[i][0] = MIN[i][0] = A[i];
}
for (int j = 1; (1 << j) <= n + 1; ++j) {
for (int i = 0; i + (1 << j) - 1 < n + 1; ++i) {
MAX[i][j] = max(MAX[i][j - 1], MAX[i + (1 << (j - 1))][j - 1]);
MIN[i][j] = min(MIN[i][j - 1], MIN[i + (1 << (j - 1))][j - 1]);
}
}
}
int RMQ_min(int L, int R) {
int k = lg[R - L + 1];
return min(MIN[L][k], MIN[R - (1 << k) + 1][k]);
}
int RMQ_max(int L, int R) {
int k = lg[R - L + 1];
return max(MAX[L][k], MAX[R - (1 << k) + 1][k]);
}
} // namespace RMQ
int _, n, m, k;
void init() {
scanf("%d", &_);
RMQ::RMQ_init1();
while (_--) {
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &RMQ::A[i]);
}
RMQ::RMQ_init2(n);
if (k >= m) k = m - 1;
int ans = -1;
for (int l = 0; l <= k; l++) {
int s = l + 1, t = l + 1 + m - 1 - k;
int ss = n + l + 3 - m - 2, tt = n - k + l;
int tmp1 = inf;
for (int i = l + 1; i + n - m <= tt; i++) {
int j = i + n - m;
int tmp = big(RMQ::A[i], RMQ::A[j]);
checkMin(tmp1, tmp);
}
checkMax(ans, tmp1);
}
printf("%d\n", ans);
}
}
void solve() {}
int main() {
init();
solve();
return 0;
}
| 3 |
//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <unordered_map>
#include <climits>
#include <set>
#include <unordered_set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <queue>
#include <random>
#include <chrono>
#include <complex>
#include <regex>
#include <locale>
#include <random>
#include <cassert>
#include <type_traits>
using namespace std;
// typedef
//------------------------------------------
typedef long long LL;
typedef vector<int> VI;
typedef vector<bool> VB;
typedef vector<char> VC;
typedef vector<double> VD;
typedef vector<string> VS;
typedef vector<LL> VLL;
typedef vector<VI> VVI;
typedef vector<VB> VVB;
typedef vector<VS> VVS;
typedef vector<VLL> VVLL;
typedef vector<VVI> VVVI;
typedef vector<VVLL> VVVLL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<int, string> PIS;
typedef pair<string, int> PSI;
typedef pair<string, string> PSS;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef vector<VPII> VVPII;
typedef vector<VPLL> VVPLL;
typedef vector<VS> VVS;
typedef map<int, int> MII;
typedef map<LL, LL> MLL;
typedef map<string, int> MSI;
typedef map<int, string> MIS;
// to-method
//------------------------------------------
inline int toInt(string s) {
int v;
istringstream sin(s);
sin >> v;
return v;
}
inline LL toLongLong(string s) {
LL v;
istringstream sin(s);
sin >> v;
return v;
}
template<class T>
inline string toString(T x) {
ostringstream sout;
sout << x;
return sout.str();
}
inline VC toVC(string s) {
VC data(s.begin(), s.end());
return data;
}
// container util
//------------------------------------------
#define ALL(a) (a).begin(),(a).end()
#define SZ(a) int((a).size())
#define EACH(i, arr) for(typeof((arr).begin()) i=(arr).begin(); i!=(arr).end(); ++i)
#define EXIST(str, e) ((str).find(e)!=(str).end())
#define COUNT(arr, v) count((arr).begin(), (arr).end(), v)
#define SEARCH(v, w) search((v).begin(), (v).end(), (w).begin(), (w).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define REVERSE(c) reverse((c).begin(), (c).end())
#define ROTATE_LEFT(arr, c) rotate((arr).begin(), (arr).begin()+(c), (arr).end())
#define ROTATE_RIGHT(arr, c) rotate((arr).rbegin(), (arr).rbegin() + (c), (arr).rend())
#define SUMI(arr) accumulate((arr).begin(), (arr).end(), 0)
#define SUMD(arr) accumulate((arr).begin(), (arr).end(), 0.)
#define SUMLL(arr) accumulate((arr).begin(), (arr).end(), 0LL)
#define SUMS(arr) accumulate((arr).begin(), (arr).end(), string())
#define MULD(arr) accumulate((arr).begin(), (arr).end(), 1., multiplies<double>())
#define MULLL(arr) accumulate((arr).begin(), (arr).end(), 1LL, multiplies<long long>())
#define UB(arr, n) upper_bound((arr).begin(), (arr).end(), n)
#define LB(arr, n) lower_bound((arr).begin(), (arr).end(), n)
#define OF_ALL(arr, func) all_of((arr).begin(), (arr).end(), (func))
#define OF_NONE(arr, func) none_of((arr).begin(), (arr).end(), (func))
#define OF_ANY(arr, func) any_of((arr).begin(), (arr).end(), (func))
#define PB push_back
#define MP make_pair
#define ft first
#define sd second
// input output
//------------------------------------------
#define GL(s) getline(cin, (s))
#define INIT() std::ios::sync_with_stdio(false);std::cin.tie(0)
#define OUT(d) std::cout<<(d)
#define OUT_L(d) std::cout<<(d)<<endl
#define FOUT(n, data) std::cout<<std::fixed<<std::setprecision(n)<<(data)
#define FOUT_L(n, data) std::cout<<std::fixed<<std::setprecision(n)<<(data)<<"\n"
#define EL() printf("\n")
#define SHOW_VECTOR(v) {std::cerr << #v << "\t:";for(const auto& xxx : v){std::cerr << xxx << " ";}std::cerr << "\n";}
#define SHOW_MAP(v) {std::cerr << #v << endl; for(const auto& xxx: v){std::cerr << xxx.first << " " << xxx.second << "\n";}}
#define Yes() printf("Yes\n")
#define No() printf("No\n")
#define YES() printf("YES\n")
#define NO() printf("NO\n")
#define Yay() printf("Yay!\n")
#define Nnn() printf(":(\n")
template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
in >> p.first >> p.second;
return in;
}
template<typename T>
istream &operator>>(istream &in, vector<T> &v) {
for (auto &x: v)
in >> x;
return in;
}
template<typename T1, typename T2>
ostream &operator<<(ostream &out, const std::pair<T1, T2> &p) {
out << "[" << p.first << ", " << p.second << "]" << "\n";
return out;
}
//repetition
//------------------------------------------
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define RFOR(i, a, b) for(int i=(b)-1;i>=(a);--i)
#define REP(i, n) FOR(i,0,n)
#define RREP(i, n) for(int i = n-1;i >= 0;i--)
#define FORLL(i, a, b) for(LL i=LL(a);i<LL(b);++i)
#define RFORLL(i, a, b) for(LL i=LL(b)-1;i>=LL(a);--i)
#define REPLL(i, n) for(LL i=0;i<LL(n);++i)
#define RREPLL(i, n) for(LL i=LL(n)-1;i>=0;--i)
#define FOREACH(x, arr) for(auto &(x) : (arr))
#define FORITER(x, arr) for(auto (x) = (arr).begin(); (x) != (arr).end(); ++(x))
template<class T>
void chmin(T &a, const T b) { a = min(a, b); }
template<class T>
void chmax(T &a, const T b) { a = max(a, b); }
//------------------------------------------
//------------------------------------------
int main() {
int N;
cin >> N;
VLL A(N);
cin >> A;
SORT(A);
LL ans = 0;
for (int i = 0; i < N; i++) {
ans += A[i] - (i + 1);
}
cout << ans << endl;
// LL ans = accumulate(ALL(A), 0LL);
//
// ans -= N * (N + 1) / 2;
// cout << ans << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n;
cin >> n;
if (n < 2) {
cout << -1;
return 0;
}
if (n & 1) n -= 1;
cout << n << " " << 2;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9;
const long long MOD = 1e9 + 7;
const int MX = 100015;
char c[11][11];
int main() {
int n = 8;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) cin >> c[i][j];
}
int mnw = 11, mnb = 11;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
bool b = 0;
if (c[i][j] == '.') continue;
if (c[i][j] == 'B') {
for (int k = i + 1; k < n; k++) {
if (c[k][j] == 'W') b = 1;
}
}
if (c[i][j] == 'W') {
for (int k = i - 1; k >= 0; k--) {
if (c[k][j] == 'B') b = 1;
}
}
if (b == 0) {
if (c[i][j] == 'W')
mnw = min(mnw, i);
else
mnb = min(mnb, 8 - i - 1);
}
}
}
if (mnw > mnb)
cout << 'B';
else
cout << 'A';
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
int main() {
ll n,nw;
cin >> n >> nw;
vector<ll> w(n);
vector<ll> v(n);
for(ll i=0;i<n;i++) {
cin >> w[i] >> v[i];
}
ll bw=w[0];
for(ll i=0;i<n;i++) {
w[i]-=bw;
}
vector<vector<vector<ll>>> dp(n+1,vector<vector<ll>> (301,vector<ll> (n+1)));
for(ll i=0;i<n;i++) {
for(ll j=0;j<=300;j++) {
for(ll k=0;k<=i+1;k++) {
dp[i+1][j][k]=max(dp[i+1][j][k],dp[i][j][k]);
if(j+w[i]<=300&&k+1<=n) {
dp[i+1][j+w[i]][k+1]=max(dp[i+1][j+w[i]][k+1],dp[i][j][k]+v[i]);
}
}
}
}
ll ans=0;
for(ll i=0;i<=min(n,nw/bw);i++) {
ll temp=300;
ans=max(ans,dp[n][min(temp,nw-i*bw)][i]);
}
cout << ans << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long a[100010];
int main() {
long long n, k, x;
cin >> n >> x >> k;
for (int i = 0; i < n; i++) scanf("%I64d", &a[i]);
sort(a, a + n);
long long sum = 0;
for (int i = 0; i < n; i++) {
long long p = (a[i] - 1) / x + 1;
sum += (lower_bound(a, a + n, p * x + k * x) -
lower_bound(a, a + n, max(a[i], p * x + (k - 1) * x)));
}
cout << sum << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
pair<int, int> ans[100005];
vector<pair<int, pair<int, int> > > v[2];
int main() {
int n, w, h;
scanf("%d%d%d", &n, &w, &h);
for (int i = 0; i < n; i++) {
int aa, bb, cc;
scanf("%d%d%d", &aa, &bb, &cc);
aa--;
v[aa].push_back({bb - cc, {bb, i}});
}
sort(v[0].begin(), v[0].end());
sort(v[1].begin(), v[1].end());
int lx = (int)v[0].size(), rx = (int)v[1].size();
int ll = 0, rr = 0;
while (ll < lx && rr < rx) {
if (v[0][ll].first == v[1][rr].first) {
int nll = ll, nrr = rr;
while (nll < lx && v[0][ll].first == v[0][nll].first) nll++;
while (nrr < rx && v[1][rr].first == v[1][nrr].first) nrr++;
int cntt = 0;
for (int i = nrr - 1; i >= rr; i--) {
if (cntt < nll - ll) {
ans[v[1][i].second.second] = {v[0][cntt + ll].second.first, h};
} else {
ans[v[1][i].second.second] = {
w, v[1][nrr - 1 - cntt + (nll - ll)].second.first};
}
cntt++;
}
for (int i = ll; i < nll; i++) {
if (cntt < nll - ll) {
ans[v[0][i].second.second] = {v[0][cntt + ll].second.first, h};
} else {
ans[v[0][i].second.second] = {
w, v[1][nrr - 1 - cntt + (nll - ll)].second.first};
}
cntt++;
}
ll = nll;
rr = nrr;
} else if (v[0][ll].first < v[1][rr].first) {
ans[v[0][ll].second.second] = {v[0][ll].second.first, h};
ll++;
} else {
ans[v[1][rr].second.second] = {w, v[1][rr].second.first};
rr++;
}
}
for (; ll < lx; ll++) {
ans[v[0][ll].second.second] = {v[0][ll].second.first, h};
}
for (; rr < rx; rr++) {
ans[v[1][rr].second.second] = {w, v[1][rr].second.first};
}
for (int i = 0; i < n; i++) printf("%d %d\n", ans[i].first, ans[i].second);
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL<<62
#define inf 1000000007
ll s[5000],e[5000],c[5000],v[2500],v1[2500]; ll n,m,p;
ll ans[2500];
ll ans2[2500];
int main() {
cin>>n>>m>>p;
for(ll i=0;i<m;i++){
cin>>s[i]>>e[i]>>c[i];
s[i]--;e[i]--;
c[i]*=-1;
c[i]+=p;
}
for(ll i=0;i<2500;i++){
v[i]=INF;
ans[i]=INF;
ans2[i]=INF;
}
v[0]=0;
ans[0]=0;
ans2[n-1]=0;
for(ll i=0;i<n+100;i++){
for(ll j=0;j<m;j++){
ll now=s[j];
ll next=e[j];
v[next]=min(v[next],v[now]+c[j]);
ans[next]=min(ans[next],ans[now]);
ans2[now]=min(ans2[now],ans2[next]);
}
}
for(ll i=0;i<n;i++){
v1[i]=v[i];
}
for(ll i=0;i<1;i++){
for(ll j=0;j<m;j++){
ll now=s[j];
ll next=e[j];
v[next]=min(v[next],v[now]+c[j]);
}
}
for(ll i=0;i<n;i++){
if(v[i]<v1[i]&&ans[i]==0&&ans2[i]==0){
cout << -1;
return 0;
}
}
if(v[n-1]>0){
cout << 0;
}
else{
cout << abs(v[n-1]);
}
// your code goes here
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back((a))
#define all(x) (x).begin(),(x).end()
#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string){cout<<endl;}
template<class H,class... T> void _dbg(string s,H h,T... t){int l=s.find(',');cout<<s.substr(0,l)<<" = "<<h<<", ";_dbg(s.substr(l+1),t...);}
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}
#define INF 1120000000
bool f[250][250];
int d[250][250][6];
int main(){
int sx,sy,gx,gy,n;
cin>>sx>>sy>>gx>>gy>>n;
sx += 120; sy += 120; gx += 120; gy += 120;
fill(f[0], f[250], true);
rep(i,n){
int x,y;
cin>>x>>y;
x += 120; y += 120;
f[x][y] = false;
}
int lx,ly;
cin>>lx>>ly;
auto ok = [&](int i, int j){
return i>=120-lx && i<=120+lx && j>=120-ly && j<=120+ly && f[i][j];
};
const int dx[2][7] = {
{0,1,1,0,-1,-1,0},
{0,1,1,0,-1,-1,0}
},
dy[2][7] = {
{1,0,-1,-1,-1,0,0},
{1,1,0,-1,0,1,0}
};
using S = tuple<int,int,int>;
fill(d[0][0], d[250][0], INF);
d[sx][sy][0] = 0;
queue<S> q;
q.push(S{sx,sy,0});
while(!q.empty()){
int x,y,t;
tie(x,y,t) = q.front(); q.pop();
int nt = (t+1)%6;
int k = abs((x-120)*(y-120)*t)%6;
rep(i,7){
int nx = x + dx[x%2][i], ny = y + dy[x%2][i];
int nd = d[x][y][t];
if(i!=k) nd++;
if(ok(nx,ny) && d[nx][ny][nt] > nd){
d[nx][ny][nt] = nd;
q.push(S{nx,ny,nt});
}
}
}
int ans = INF;
rep(i,6) ans = min(ans, d[gx][gy][i]);
if(ans == INF) ans = -1;
cout << ans << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n, m, ans = 0;
cin >> n >> m;
for (int i = 0; i < n; i++) {
int temp = 1000000001;
for (int i = 0; i < m; i++) {
int t;
cin >> t;
temp = min(temp, t);
}
ans = max(ans, temp);
}
cout << ans << endl;
}
int main() {
long long int t = 1;
while (t--) solve();
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
string s;
cin >> s;
int l = s.length();
long long odd = (long long)l, even = 0;
long long evena = 0, odda = 0, evenb = 0, oddb = 0;
for (int i = 0; i < l; i++) {
if (i % 2 == 0) {
if (s[i] == 'a')
evena++;
else
evenb++;
} else {
if (s[i] == 'a')
odda++;
else
oddb++;
}
}
odd += evena * 1ll * (evena - 1) / 2;
odd += evenb * 1ll * (evenb - 1) / 2;
odd += odda * 1ll * (odda - 1) / 2;
odd += oddb * 1ll * (oddb - 1) / 2;
even += evena * 1ll * odda + evenb * 1ll * oddb;
cout << even << " " << odd;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
char arr1[2][2];
char arr2[2][2];
int main() {
string s1, s2, s3, s4;
cin >> s1 >> s2 >> s3 >> s4;
reverse(s2.begin(), s2.end());
reverse(s4.begin(), s4.end());
s1 += s2;
s3 += s4;
for (int i = 0; i < s1.size(); i++) {
if (s1[i] == 'X') s1.erase(i, 1);
}
for (int i = 0; i < s3.size(); i++) {
if (s3[i] == 'X') s3.erase(i, 1);
}
for (int i = 0; i < s3.size(); i++) {
if (s3[i] == s1[0]) {
if (s3[(i + 1) % 3] == s1[1])
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
long long n;
cin >> n;
vector<long long> adj[2 * n];
vector<pair<long long, long long> > a;
for (long long i = (0); i < (n); i++) {
long long x, y;
cin >> x >> y;
x--;
y--;
pair<long long, long long> temp(x, y);
a.push_back(temp);
adj[x].push_back(y);
adj[y].push_back(x);
}
for (long long i = (0); i < (n); i++) {
adj[2 * i].push_back(2 * i + 1);
adj[2 * i + 1].push_back(2 * i);
}
long long nodes = 2 * n;
long long color[nodes];
for (long long i = (0); i < (nodes); i++) color[i] = 0;
bool visited[nodes];
for (long long i = (0); i < (nodes); i++) visited[i] = 0;
for (long long i = (0); i < (nodes); i++) {
if (visited[i]) continue;
queue<long long> q;
q.push(i);
visited[i] = 1;
color[i] = 1;
while (q.size() != 0) {
long long c = q.front();
q.pop();
visited[c] = 1;
for (long long i = (0); i < (adj[c].size()); i++) {
if (!visited[adj[c][i]]) {
visited[adj[c][i]] = 1;
color[adj[c][i]] = -1 * color[c];
q.push(adj[c][i]);
}
}
}
}
for (int i = 0; i < a.size(); i++) {
if (color[a[i].first] == -1) color[a[i].first] = 2;
if (color[a[i].second] == -1) color[a[i].second] = 2;
cout << color[a[i].first] << " " << color[a[i].second] << "\n";
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
double ans = 0.0000;
void bfs();
list<int> arr[100005];
int visited[100005], dep[100005];
int main() {
int n, i, start, end;
cin >> n;
n--;
while (n--) {
cin >> start;
cin >> end;
arr[start].push_back(end);
arr[end].push_back(start);
}
bfs();
cout.precision(7);
cout << fixed << ans;
return 0;
}
void bfs() {
int node;
dep[1] = 1;
queue<int> qu;
list<int>::iterator iter = arr[1].begin();
node = 1;
visited[1] = 1;
qu.push(1);
while (!qu.empty()) {
for (iter = arr[node].begin(); iter != arr[node].end(); ++iter) {
if (!visited[*iter]) {
qu.push(*iter);
visited[*iter] = 1;
dep[*iter] = dep[node] + 1;
}
}
int k = qu.front();
qu.pop();
node = qu.front();
ans += 1 / (double)dep[k];
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
T maxm(T a, T b) {
return (a > b) ? a : b;
}
template <typename T>
T minm(T a, T b) {
return (a < b) ? a : b;
}
template <class T>
inline T gcd(const T& a, const T& b) {
return (b == 0) ? a : gcd(b, a % b);
}
int main() {
long long n;
cin >> n;
long long* a = new long long[n];
bool* c = new bool[n];
for (long long i = (0); i < (n); ++i) scanf("%lld", &a[i]);
long long* b = new long long[101];
bool* d = new bool[101];
for (long long i = (0); i < (101); ++i) {
b[i] = 0;
d[i] = false;
}
for (long long i = (0); i < (n); ++i) c[i] = false;
sort(a, a + n);
long long index = 0;
while (index < 101) {
for (long long i = (0); i < (n); ++i) {
if (!c[i] && a[i] >= b[index]) {
if (d[index]) {
if (a[i] == 0) {
} else {
c[i] = true;
b[index] += 1;
}
} else {
c[i] = true;
b[index] += 1;
d[index] = true;
}
}
}
index++;
}
long long count = 0;
for (long long i = (0); i < (101); ++i) {
if (d[i]) count++;
}
cout << count << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
const int mxN = 5005;
template <class T>
T gcd(T a, T b) {
return ((b == 0) ? a : gcd(b, a % b));
}
int n, m;
int dist(int a, int b) {
if (b >= a) return b - a;
return n - (a - b);
}
int cur = 0;
vector<int> g[mxN];
bool cmp(int a, int b) { return dist(cur, a) < dist(cur, b); }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
vector<int> a(m), b(m);
for (int i = 0; i < (int)(m); i++)
cin >> a[i] >> b[i], g[a[i]].push_back(b[i]);
for (int i = (int)(1); i < (int)(n + 1); i++) {
cur = i;
sort(g[i].begin(), g[i].end(), cmp);
}
for (int i = (int)(1); i < (int)(n + 1); i++) {
int ans = 0;
int ind = i;
int res = 0;
int num = 0;
for (int j = 0; j < (int)(n); j++) {
res = max(res, ans + (g[ind].size() ? ((int)(g[ind].size() - 1) * n) +
dist(ind, g[ind][0])
: 0));
num += g[ind].size();
if (num == m) break;
ind++;
if (ind == (n + 1)) ind = 1;
ans++;
}
cout << res << " ";
}
cout << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define UNIQUE(v) v.erase(unique(all(v)), v.end());
#define ZIP(v) sort(all(v)),UNIQUE(v)
#define repi(i,m,n) for(int i = m;i < n;i++)
#define drep(i,n,m) for(int i = n;i >= m;i--)
#define rep(i,n) repi(i,0,n)
#define rrep(i,n) repi(i,1,n+1)
#define chmin(x,y) x = min(x,y)
#define chmax(x,y) x = max(x,y)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb(x) push_back(x)
#define fi first
#define se second
typedef pair<int,int> P;
typedef pair<int, P> PP;
typedef vector<int> vi;
const int inf = 1e9+7;
const int INF = 1e18+7;
int mod = 1e9+7;
string str[10010], T;
int judge(string s){
if(s == T)return 2;
if(s > T)return 0;
else return 1;
}
int solve(int p){
string s = str[p];
int size = str[p].size(), ret[2] = {};
rep(i,size)if(str[p][i] == '?')s[i] = 'z';
ret[0] = judge(s);
rep(i,size)if(str[p][i] == '?')s[i] = 'a';
ret[1] = judge(s);
if(ret[0] == 2 or ret[1] == 2)return -1;
if(ret[0]+ret[1] == 1)return -1;
if(ret[0]+ret[1] == 0)return 0;
return 1;
}
signed main(){
int n;
scanf("%lld", &n);
rep(i,n)cin >> str[i];
cin >> T;
int cnt = 0, r = 0;
rep(i,n){
int tmp = solve(i);
if(tmp == -1)r++;
else cnt += tmp;
}
//printf("%lld %lld\n", r, cnt);
rrep(i,r+1){
printf("%lld%c", cnt+i, i == r+1 ? '\n' : ' ');
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t, k;
string n;
cin >> t;
vector<int> digits(10);
while (t-- > 0) {
cin >> n >> k;
std::fill(digits.begin(), digits.end(), 0);
int cnt = 0;
int up_idx = 1000;
int i = 0;
while (i < n.length()) {
if (i > up_idx) {
int j = 0;
while (j < 10) {
if (digits[j] > 0 || cnt < k) {
n[i] = (char)(j + '0');
if (digits[j]++ == 0) cnt++;
break;
}
j++;
}
} else {
int d = n[i] - '0';
if (digits[d]++ == 0) cnt++;
if (cnt > k) {
int j = d + 1;
digits[d]--;
while (j < 10) {
if (digits[j] > 0) {
n[i] = (char)(j + '0');
digits[j]++;
cnt--;
up_idx = i;
break;
}
j++;
}
if (cnt > k) {
if (--digits[n[i - 1] - '0'] == 0) cnt--;
cnt--;
n[i - 1]++;
up_idx = i - 1;
i--;
continue;
}
}
}
i++;
}
cout << n << "\n";
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 7;
const int MAXK = 2005;
int poz[MAXN][MAXK];
vector<int> s[MAXK];
int dp[MAXK], n, k;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
cin >> k >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
int temp;
cin >> temp;
--temp;
if (i == 0) {
poz[i][j] = temp;
} else {
poz[i][temp] = j;
}
}
}
for (int i = 0; i < k; i++) {
s[i] = vector<int>(n - 1);
for (int j = 1; j < n; ++j) {
s[i][j - 1] = poz[j][poz[0][i]];
}
}
int res = 0;
for (int i = 0; i < k; i++) {
dp[i] = 1;
for (int j = 0; j < i; j++) {
bool canUse = true;
for (int p = 0; p < (int)s[j].size(); p++) {
if (s[i][p] <= s[j][p]) {
canUse = false;
break;
}
}
if (canUse) {
dp[i] = max(dp[j] + 1, dp[i]);
}
}
res = max(res, dp[i]);
}
cout << res;
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
bool t[501];
int main() {
for (int i = 1; i * (i + 1) / 2 <= 500; i++) t[i * (i + 1) / 2] = true;
int n;
scanf("%d", &n);
printf(t[n] ? "YES\n" : "NO\n");
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;
#define ALL(a) begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define FF first
#define SS second
template<class S, class T>
istream& operator>>(istream& is, pair<S,T>& p){
return is >> p.FF >> p.SS;
}
template<class S, class T>
ostream& operator<<(ostream& os, const pair<S,T>& p){
return os << p.FF << " " << p.SS;
}
template<class T>
void maxi(T& x, T y){
if(x < y) x = y;
}
template<class T>
void mini(T& x, T y){
if(x > y) x = y;
}
const double EPS = 1e-10;
const double PI = acos(-1.0);
int H, h, m, s;
//! x / y
class Frac{
public:
using LL = long long;
LL x, y;
Frac(LL x = 0, LL y = 1) : x(x), y(y) { red(); }
LL gcd(LL x, LL y){
if(y == 0) return x;
return gcd(y, x%y);
}
void red(){
if(x == 0){
y = 1;
return;
}
if(y < 0){
x *= -1;
y *= -1;
}
LL g = gcd(abs(x), y);
x /= g;
y /= g;
}
Frac& operator += (const Frac& p){ x = x * p.y + y * p.x; y *= p.y; red(); return *this;}
Frac operator + (const Frac& p) const{ Frac q = *this;return q += p;}
Frac operator - () const { Frac q = *this; q.x *= -1; return q; }
Frac& operator -= (const Frac& p){ Frac q = -p; *this += q; return *this;}
Frac operator - (const Frac& p) const{ Frac q = *this; return q -= p;}
Frac& operator *= (const Frac& a){ x *= a.x; y *= a.y; red(); return *this;}
Frac operator * (Frac a) const{ Frac q = *this; return q *= a;}
Frac& operator /= (const Frac& a){ x *= a.y; y *= a.x; red(); return *this;}
Frac operator / (Frac a) const{ Frac q = *this; return q /= a;}
bool operator < (const Frac &p) const{
return x * p.y < y * p.x;
}
bool operator <= (const Frac &p) const{
return *this < p || *this == p;
}
bool operator > (const Frac &p) const{
return p < *this;
}
bool operator >= (const Frac &p) const{
return *this > p || *this == p;
}
bool operator == (const Frac &p) const {
return x == p.x && y == p.y;
}
double getR() const { return x * 1. / y; }
friend ostream& operator <<(ostream& os, const Frac& p);
};
Frac abs(const Frac& q){ return Frac(abs(q.x), q.y); }
ostream& operator <<(ostream& os, const Frac& p){
return os << p.x << "/" << p.y;
}
void norm(Frac& t){
if(t.x < 0) t += 3600*H;
while(t >= 60 * 60 * H)
t -= 60 * 60 * H;
}
void normR(Frac& t){
if(t.x < 0) t += 360;
while(t >= 360)
t -= 360;
}
Frac dist(Frac& now, Frac& dst){
if(now <= dst) return dst - now;
return dst + 3600*H - now;
}
void print(Frac& ans){
LL ah = ans.x / ans.y / 60 / 60;
LL am = ans.x / ans.y / 60 % 60;
Frac as = ans - ah*3600 - am*60;
cout << ah << " " << am << " " << as.x << " " << as.y << endl;
}
int main(){
cin.tie(0);
ios_base::sync_with_stdio(false);
while(cin>>H>>h>>m>>s,H){
Frac now(h*3600+m*60+s);
Frac ans(1e9, 1), ans_dt(1e9, 1);
REP(th,H) REP(tm,60){
for(int s1=-1;s1<=1;s1+=2){
for(int s2=-1;s2<=1;s2+=2){
Frac c1(3600*th + 60*tm, 10*H);
Frac a1 = Frac(1, 10*H) - 6;
Frac c2(6*tm);
Frac a2 = Frac(1, 10) - 6;
for(int ang=0;ang<=360;ang+=360){
Frac ss = (c2 * s2 - c1 * s1 + ang) / (a1 * s1 - a2 * s2);
Frac rh = c1 + ss / (10*H);
normR(rh);
Frac rm = c2 + ss / 10;
normR(rm);
Frac rs = ss * 6;
normR(rs);
if(rh == rm || rh == rs || rm == rs) continue;
Frac nxt = ss + 3600*th + 60*tm;
norm(nxt);
Frac dd = dist(now, nxt);
if(dd < ans_dt){
ans_dt = dd;
ans = nxt;
}
}
}
}
}
print(ans);
}
return 0;
}
| 0 |
#include<algorithm>
#include<iostream>
#include<fstream>
#include<map>
using namespace std;
typedef long long ll;
int t,n,m,s;
int a[100005];
ll b[100005];
map<long long,bool>M;
int Find(int x,int y,int z){
int l=x,r=y,mid=(x+y)>>1;
while(l<=r){
mid=(l+r)>>1;
if(a[mid]<=z&&a[mid+1]>z){
return mid;
}else if(a[mid]>z){
r=mid-1;
}else{
l=mid+1;
}
}
}
void Divide_n(int x,int y){
M[b[y]-b[x-1]]=1;
if(a[x]==a[y])return;
int stdn=(a[x]+a[y])/2;
int pos=Find(x,y,stdn);
M[b[pos]-b[x-1]]=M[b[y]-b[pos]]=1;
Divide_n(x,pos);
Divide_n(pos+1,y);
}
int main(){
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
M.clear();
sort(a+1,a+1+n);
for(int i=1;i<=n;i++){
b[i]=b[i-1]+ll(a[i]);
}
Divide_n(1,n);
while(m--){
scanf("%d",&s);
if(M[s]){
printf("Yes\n");
}else{
printf("No\n");
}
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 10;
int n, m, d[maxn][maxn], val[maxn][maxn];
string s[maxn];
vector<int> rows[maxn], cols[maxn];
bool marked[maxn][maxn];
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> s[i];
for (int j = 0; j < m; j++) {
if (s[i][j] == '#') {
rows[i].push_back(j);
cols[j].push_back(i);
}
}
}
queue<pair<int, int>> q;
q.push(make_pair(n - 1, m));
val[n - 1][m] = 1;
while (!q.empty()) {
auto v = q.front();
q.pop();
if (val[v.first][v.second]) {
for (int c : rows[v.first]) {
if (!marked[v.first][c]) {
d[v.first][c] = d[v.first][v.second] + 1;
q.push(make_pair(v.first, c));
marked[v.first][c] = true;
val[v.first][c] = 0;
}
}
} else {
for (int r : cols[v.second]) {
if (!marked[r][v.second]) {
d[r][v.second] = d[v.first][v.second] + 1;
q.push(make_pair(r, v.second));
marked[r][v.second] = true;
val[r][v.second] = 1;
}
}
}
}
int ans = 1e9;
for (int i = 0; i < m; i++) {
if (s[0][i] == '#' && marked[0][i]) ans = min(ans, d[0][i]);
}
if (ans == 1e9)
cout << -1 << endl;
else
cout << ans << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int i, j, k, l, n, m, g[12000], x, ans[12000];
char c[1200000], ch[27];
short f[1 << 26];
int main() {
scanf("%s", c + 1);
n = strlen(c + 1);
scanf("%d", &m);
for (i = 1; i <= m; ++i) {
scanf("%s", ch + 1);
l = strlen(ch + 1);
x = 0;
for (j = 1; j <= l; ++j) x |= 1 << ch[j] - 97;
f[x] = i;
g[i] = x;
}
c[0] = c[n + 1] = 'z' + 1;
for (i = 1; i <= n; ++i) {
x = 0;
for (j = i; j <= n; ++j) {
if (c[j] == c[i - 1]) break;
x |= 1 << c[j] - 97;
if (!(x >> c[j + 1] - 97 & 1)) ans[f[x]]++;
}
}
for (i = 1; i <= m; ++i) printf("%d\n", ans[f[g[i]]]);
}
| 2 |
#include <cstdio>
#include <iostream>
using namespace std;
double dp[305][305][305];
bool vis[305][305][305];
int n,cnt[4];
double dfs (int i,int j,int k) {
if(i==0&&j==0&&k==0) return 0;
if(vis[i][j][k]) return dp[i][j][k];
vis[i][j][k]=true;
dp[i][j][k]=1;
if(i!=0) dp[i][j][k]+=dfs(i-1,j,k)*i/n;
if(j!=0) dp[i][j][k]+=dfs(i+1,j-1,k)*j/n;
if(k!=0) dp[i][j][k]+=dfs(i,j+1,k-1)*k/n;
dp[i][j][k]/=1.0*(i+j+k)/n;
return dp[i][j][k];
}
int main () {
cin>>n;
for(int i=1;i<=n;i++) {
int x;
scanf("%d",&x);
cnt[x]++;
}
printf("%.10lf",dfs(cnt[1],cnt[2],cnt[3]));
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n, m, k, i, j;
long long f = -1;
cin >> n >> m >> k;
if (k > (n + m - 2)) {
cout << f << endl;
return 0;
}
for (i = 1; i * i <= n; i++) {
if (n % i == 0) {
j = k - (i - 1) + 1;
if (j >= 1) {
f = max(f, (n / i) * (m / j));
}
j = k - ((n / i) - 1) + 1;
if (j >= 1) {
f = max(f, (i) * (m / j));
}
}
}
for (i = 1; i * i <= m; i++) {
if (m % i == 0) {
j = k - (i - 1) + 1;
if (j >= 1) {
f = max(f, (m / i) * (n / j));
}
j = k - ((m / i) - 1) + 1;
if (j >= 1) {
f = max(f, (i) * (n / j));
}
}
}
cout << f << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n,k,a[200010];
void work()
{
scanf("%d %d",&n,&k);
for (int i=1,x; i<=n; i++) scanf("%d",&x),a[x]++;
sort(a+1,a+n+1),reverse(a+1,a+n+1);
for (int i=1; i<=k; i++) n-=a[i];
printf("%d",n);
}
int main()
{
work();
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
queue<int> q;
int n, m, minans = 0;
int mark[1000006];
int main() {
memset(mark, 0, sizeof(mark));
int i, j, diff1, mul2;
cin >> n >> m;
q.push(n);
int cnt = 1;
bool flag = 0;
while (!q.empty()) {
int res = cnt;
cnt = 0;
for (i = 0; i < res; i++) {
int temp = q.front();
q.pop();
diff1 = temp - 1;
mul2 = temp * 2;
if (diff1 < 0) diff1 = -1;
if (mul2 > 10000) mul2 = 10004;
if (diff1 == m || mul2 == m) {
flag = 1;
minans++;
break;
} else {
if (diff1 != -1 && !mark[diff1]) {
q.push(diff1);
mark[diff1] = true;
cnt++;
}
if (mul2 <= 10000 && !mark[mul2]) {
q.push(mul2);
mark[mul2] = true;
cnt++;
}
}
}
if (flag) break;
minans++;
}
cout << minans << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
long long co = 0;
string *arr = new string[n];
set<string> se;
for (int i = 0; i < n; i++) {
cin >> arr[i];
se.insert(arr[i]);
}
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
string s = "";
for (int l = 0; l < k; l++) {
if ((arr[i][l] == arr[j][l] && arr[j][l] == 'S') ||
(arr[i][l] != arr[j][l] && arr[i][l] != 'S' && arr[j][l] != 'S')) {
s += "S";
}
if ((arr[i][l] == arr[j][l] && arr[j][l] == 'E') ||
(arr[i][l] != arr[j][l] && arr[i][l] != 'E' && arr[j][l] != 'E')) {
s += "E";
}
if ((arr[i][l] == arr[j][l] && arr[j][l] == 'T') ||
(arr[i][l] != arr[j][l] && arr[i][l] != 'T' && arr[j][l] != 'T')) {
s += "T";
}
}
if (se.find(s) != se.end()) co++;
}
}
cout << co / 3 << endl;
return 0;
}
| 2 |
#include<iostream>
#include<vector>
#include<map>
#include<set>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef vector<int> D;
int R[][6]={{0,3,1,4,2,5},{2,1,5,0,4,3},{4,0,2,3,5,1}};
void rot(D &d,int a){
D r(6); rep(i,6)r[i]=d[R[a][i]]; d=r;
}
int main(){
map<char,int> M; M['R']=0,M['Y']=1,M['B']=2,M['M']=3,M['G']=4,M['C']=5;
int n;
while(cin>>n,n){
set<D> s,alld; char b[9];
rep(ii,n){
D d(6),t(6);
alld.clear();
rep(jj,6)cin>>b,d[jj]=M[b[0]];
rep(i,4)rep(j,4)rep(k,4){
t=d;
rep(p,i)rot(t,0);rep(q,j)rot(t,1);rep(r,k)rot(t,2);
alld.insert(t);
}
s.insert(*alld.begin());
}
cout<<n-s.size()<<endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-6;
const long long mod = 1e9 + 7;
const int MAX = 2e5 + 10;
int a[MAX], b[MAX];
int main() {
int n, i, ans;
while (~scanf("%d", &n)) {
ans = 0;
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (i = 0; i < n; i++) {
scanf("%d", &b[i]);
if (b[i] == a[ans]) ans++;
}
printf("%d\n", n - ans);
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int n;
int m;
int a[500000];
long long ans = 0;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[++m];
while (m > 2 && a[m - 1] <= min(a[m - 2], a[m])) {
ans += min(a[m - 2], a[m]);
a[m - 1] = a[m];
m--;
}
}
for (int i = 1; i < m; i++) {
ans += min(a[i - 1], a[i + 1]);
}
cout << ans;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = (int)1e7 + 42;
const int64_t mod = (int64_t)1e9 + 7;
template <class T>
struct fenwick {
int sz;
T tr[MAXN];
void init(int n) {
sz = n + 1;
memset(tr, 0, sizeof(tr));
}
T query(int idx) {
T ans = 0;
for (; idx >= 1; idx -= (idx & -idx)) ans += tr[idx];
return ans;
}
void update(int idx, T val) {
if (idx <= 0) return;
for (; idx <= sz; idx += (idx & -idx)) tr[idx] += val;
}
T query(int l, int r) { return query(r) - query(l - 1); }
};
int n;
void read() { cin >> n; }
int64_t answer = 0;
int64_t cnt[MAXN], c[MAXN];
int lp[MAXN];
int64_t C(int64_t x) { return x * 1ll * (x - 1) / 2ll; }
fenwick<int64_t> t;
void solve() {
t.init(n);
for (int i = 2; i <= n; i++)
if (lp[i] == 0)
for (int x = i; x <= n; x += i)
if (lp[x] == 0) lp[x] = i;
for (int i = 2; i <= n; i++)
cnt[i] += (n - i) / i + 1, c[i] += (n - i) / i + 1;
for (int i = 2; i <= n; i++) cnt[i] = C(cnt[i]);
for (int i = n; i >= 1; i--)
for (int x = i * 2; x <= n; x += i) cnt[i] -= cnt[x];
for (int i = 2; i <= n; i++) answer -= cnt[i];
for (int i = 2; i <= n; i++) t.update(lp[i], 1);
for (int i = 2; i <= n; i++) {
t.update(lp[i], -1);
int64_t cnt2 = t.query(n / lp[i]);
int64_t cnt3 = t.query(n / 2);
cnt3 -= cnt2;
answer += cnt2 * 2ll;
if (lp[i] * 2 <= n) answer += cnt3 * 3ll;
}
cout << answer << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
read();
solve();
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long n, poz;
int q;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> q;
while (q--) {
cin >> poz;
if (poz % 2 == 1) {
cout << (poz + 1) / 2 << "\n";
} else {
while (poz % 2 == 0) {
poz = poz / 2 + n;
}
cout << poz / 2 + 1 << "\n";
}
}
return 0;
}
| 4 |
#include <iostream>
#include <vector>
using namespace std;
int main()
{
while (true) {
int n, k, m;
cin >> n >> k >> m;
if (n == 0 && k == 0 && m == 0) {
break;
}
vector<int> stone(n);
for (int i = 0; i < n; i++) {
stone[i] = i + 1;
}
int cur = m - 1;
while (stone.size() != 1) {
stone.erase(stone.begin() + cur);
cur = (cur + k - 1) % stone.size();
}
cout << stone[0] << 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 vb = vector<bool>;
using vd = vector<double>;
using vs = vector<string>;
using vpii = vector<pair<int, int>>;
using vpll = vector<pair<ll, ll>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int dy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
void solve() {
vi a(6);
for (ll i = 0; i < 6; i++) cin >> a[i];
;
ll ans = ((a[0] + a[1] + a[2]) * (a[0] + a[1] + a[2]) - a[0] * a[0] -
a[2] * a[2] - a[4] * a[4]);
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll t = 1;
while (t--) {
solve();
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long mul(long long a, long long b) { return (a * b) % (100000007); }
long long add(long long a, long long b) { return (a + b) % (100000007); }
long long sub(long long a, long long b) {
return (a - b + (a - b) / (100000007) * (100000007) + (100000007)) %
(100000007);
}
char s1[(100000 + 10)], s2[(100000 + 10)];
long long a[(100000 + 10)] = {0}, b[(100000 + 10)] = {0};
void pushdown(long long a[], int i) {
a[i - 1] += a[i], a[i - 2] += a[i];
a[i] = 0;
}
int main() {
scanf("%s%s", s1, s2);
int n1 = strlen(s1), n2 = strlen(s2);
for (int i = 0; i < n1 / 2; i++) swap(s1[i], s1[n1 - i - 1]);
for (int i = 0; i < n2 / 2; i++) swap(s2[i], s2[n2 - i - 1]);
for (int i = 1; i <= n1; i++) a[i] = s1[i - 1] - '0';
for (int i = 1; i <= n2; i++) b[i] = s2[i - 1] - '0';
int n = max(n1, n2);
for (int i = n; i; i--) a[i] -= b[i];
for (int i = n; i >= 3; i--) {
if (a[i] >= 2) {
cout << '>' << endl;
return 0;
} else if (a[i] <= -2) {
cout << '<' << endl;
return 0;
}
pushdown(a, i);
}
const double q = (1 + sqrt(5)) / 2;
double p = a[1] + a[2] * q;
if (p > (1e-3))
putchar('>');
else if (p < -(1e-3))
putchar('<');
else
putchar('=');
putchar('\n');
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int oo = 1e9 + 7;
const int maxn = 1e6 + 10;
int wa[maxn], wb[maxn], wv[maxn], wc[maxn];
int r[maxn], sa[maxn], rak[maxn], height[maxn], dp[22][maxn], jump[maxn],
SIGMA = 0;
void da(int *r, int *sa, int n, int m) {
int i, j, p, *x = wa, *y = wb, *t;
for (i = 0; i < m; i++) wc[i] = 0;
for (i = 0; i < n; i++) wc[x[i] = r[i]]++;
for (i = 1; i < m; i++) wc[i] += wc[i - 1];
for (i = n - 1; i >= 0; i--) sa[--wc[x[i]]] = i;
for (j = 1, p = 1; p < n; j *= 2, m = p) {
for (p = 0, i = n - j; i < n; i++) y[p++] = i;
for (i = 0; i < n; i++)
if (sa[i] >= j) y[p++] = sa[i] - j;
for (i = 0; i < n; i++) wv[i] = x[y[i]];
for (i = 0; i < m; i++) wc[i] = 0;
for (i = 0; i < n; i++) wc[wv[i]]++;
for (i = 1; i < m; i++) wc[i] += wc[i - 1];
for (i = n - 1; i >= 0; i--) sa[--wc[wv[i]]] = y[i];
for (t = x, x = y, y = t, p = 1, x[sa[0]] = 0, i = 1; i < n; i++)
x[sa[i]] = (y[sa[i - 1]] == y[sa[i]] && y[sa[i - 1] + j] == y[sa[i] + j])
? p - 1
: p++;
}
}
void calheight(int *r, int *sa, int n) {
int i, j, k = 0;
for (i = 1; i <= n; i++) rak[sa[i]] = i;
for (i = 0; i < n; height[rak[i++]] = k) {
for (k ? k-- : 0, j = sa[rak[i] - 1]; r[i + k] == r[j + k]; k++)
;
}
}
void initRMQ(int n) {
for (int i = 0; i <= n; i++) dp[0][i] = height[i];
for (int j = 1; (1 << j) <= n; j++) {
for (int i = 0; i + (1 << j) - 1 <= n; i++) {
dp[j][i] = min(dp[j - 1][i], dp[j - 1][i + (1 << (j - 1))]);
}
}
for (int i = 1; i <= n; i++) {
int k = 0;
while ((1 << (k + 1)) <= i) k++;
jump[i] = k;
}
}
int askRMQ(int L, int R) {
register int k = jump[R - L + 1];
return min(dp[k][L], dp[k][R - (1 << k) + 1]);
}
int start[N];
int len[N];
int ans[N];
inline int common_len(int i, int j) {
i = rak[i];
j = rak[j];
if (i > j) swap(i, j);
if (i == j)
return maxn;
else
return askRMQ(i + 1, j);
}
inline bool cmp_str(pair<int, int> l, pair<int, int> r) {
if (common_len(start[l.first], start[r.first]) >= min(l.second, r.second)) {
if (l.second == r.second) {
pair<int, int> lx = {start[l.first] + l.second + 1,
max(len[l.first] - l.second - 1, 0)};
pair<int, int> rx = {start[r.first] + r.second + 1,
max(len[r.first] - r.second - 1, 0)};
return ((common_len(lx.first, rx.first) >= min(lx.second, rx.second))
? (lx.second == rx.second ? lx < rx : lx.second < rx.second)
: rak[lx.first] < rak[rx.first]);
} else if (l.second < r.second) {
pair<int, int> lx = {start[l.first] + l.second + 1,
max(len[l.first] - l.second - 1, 0)};
pair<int, int> rx = {start[r.first] + l.second, r.second - l.second};
if ((common_len(lx.first, rx.first) >= min(lx.second, rx.second))) {
if (lx.second == rx.second) {
return max(len[r.first] - r.second - 1, 0) > 0 ? 1 : l < r;
} else if (lx.second < rx.second) {
return 1;
} else {
lx.first += rx.second;
lx.second -= rx.second;
rx = {start[r.first] + r.second + 1,
max(len[r.first] - r.second - 1, 0)};
return (
(common_len(lx.first, rx.first) >= min(lx.second, rx.second))
? (lx.second == rx.second ? lx < rx : lx.second < rx.second)
: rak[lx.first] < rak[rx.first]);
}
} else {
return rak[lx.first] < rak[rx.first];
}
} else {
pair<int, int> lx = {start[l.first] + r.second, l.second - r.second};
pair<int, int> rx = {start[r.first] + r.second + 1,
max(len[r.first] - r.second - 1, 0)};
if ((common_len(lx.first, rx.first) >= min(lx.second, rx.second))) {
if (lx.second == rx.second) {
return max(len[l.first] - l.second - 1, 0) > 0 ? 0 : l < r;
} else if (lx.second > rx.second) {
return 0;
} else {
rx.first += lx.second;
rx.second -= lx.second;
lx = {start[l.first] + l.second + 1,
max(len[l.first] - l.second - 1, 0)};
return (
(common_len(lx.first, rx.first) >= min(lx.second, rx.second))
? (lx.second == rx.second ? lx < rx : lx.second < rx.second)
: rak[lx.first] < rak[rx.first]);
}
} else {
return rak[lx.first] < rak[rx.first];
}
}
} else
return rak[start[l.first]] < rak[start[r.first]];
}
char s[maxn];
pair<int, int> x[maxn + 100000];
int32_t main() {
int n;
scanf("%d", &n);
int xs = 0;
int now = 0;
for (int i = 1; i <= n; i++) {
scanf("%s", s);
start[i] = now;
for (int j = 0; s[j]; j++) {
r[now] = s[j] - 'a' + 1;
SIGMA = max(SIGMA, r[now++]);
}
len[i] = now - start[i];
for (int j = 0; j <= len[i]; j++) {
x[xs].first = i;
x[xs].second = j;
xs++;
}
}
r[now] = 0;
da(r, sa, now + 1, SIGMA + 1);
calheight(r, sa, now);
initRMQ(now);
sort(x, x + xs, cmp_str);
ans[0] = 1;
for (int i = 0, z = x[i].first; i < xs; i++, z = x[i].first)
ans[z] = (ans[z] + ans[z - 1]) % oo;
printf("%d\n", ans[n]);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
char s[5005];
long long mat[5005][5005];
long long temp[5005];
int main() {
long long n, m;
scanf("%lld%lld", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%s", s);
for (int j = 0; j < m; j++) {
mat[i][j] = (s[j] == '1');
}
for (int j = 1; j < m; j++) {
if (s[j] == '0') {
mat[i][j] = 0;
} else {
mat[i][j] += mat[i][j - 1];
}
}
}
memset(temp, 0, sizeof(temp));
long long ans = 0;
for (int j = 0; j < m; j++) {
for (int i = 0; i < n; i++) {
temp[i] = mat[i][j];
}
sort(temp, temp + n);
reverse(temp, temp + n);
long long maxx = 0;
for (int i = 0; i < n; i++) {
maxx = max(maxx, (i + 1) * temp[i]);
}
ans = max(ans, maxx);
}
printf("%lld\n", ans);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int nowx[1010], nowy[1010], tarx[1010], tary[1010], n;
int ansx[20 * 1010], ansy[20 * 1010], cnt = 0;
bool ok[1010][1010], tarok[1010][1010];
vector<int> t1, t2;
void sol(vector<int> &t) {
int d = t.size();
if (d <= 3) return;
int nbest = 0, dx = -1, dy = -1;
for (int i = 0; i < d; i++)
for (int j = i + 1; j < d; j++)
if (j != ((i + 1) % t.size()) && j != ((i - 1 + t.size()) % t.size()))
if (tarok[t[i]][t[j]] || tarok[t[j]][t[i]]) {
int tmp = (i - j + d) % d;
if (tmp > (d >> 1)) tmp = d - tmp;
if (tmp > nbest) {
dx = i;
dy = j;
nbest = tmp;
}
}
if (!ok[t[dx]][t[dy]]) {
int a = dy, b = dy;
while (!ok[t[dx]][t[a]] && ((a + 1) % t.size()) != dx)
a = ((a + 1) % t.size());
while (!ok[t[dx]][t[b]] && ((b - 1 + t.size()) % t.size()) != dx)
b = ((b - 1 + t.size()) % t.size());
int c = ((a - 1 + t.size()) % t.size());
bool dir = false;
if (!ok[t[b]][t[c]])
while ((!ok[t[b]][t[c]] || !ok[t[a]][t[c]]) &&
((c - 1 + t.size()) % t.size()) != b) {
if (c == dy) dir = true;
c = ((c - 1 + t.size()) % t.size());
}
cnt++;
ansx[cnt] = t[a];
ansy[cnt] = t[b];
ok[t[a]][t[b]] = ok[t[b]][t[a]] = false;
ok[t[dx]][t[c]] = ok[t[c]][t[dx]] = true;
while (c != dy && b != dy) {
if (!dir)
a = c;
else
b = c;
c = ((a - 1 + t.size()) % t.size());
dir = false;
if (!ok[t[b]][t[c]])
while ((!ok[t[b]][t[c]] || !ok[t[a]][t[c]]) &&
((c - 1 + t.size()) % t.size()) != b) {
if (c == dy) dir = true;
c = ((c - 1 + t.size()) % t.size());
}
cnt++;
ansx[cnt] = t[a];
ansy[cnt] = t[b];
ok[t[a]][t[b]] = ok[t[b]][t[a]] = false;
ok[t[dx]][t[c]] = ok[t[c]][t[dx]] = true;
}
}
vector<int> t1, t2;
bool ins = false;
for (int i = 0; i < t.size(); i++) {
if (!ins)
t1.push_back(t[i]);
else
t2.push_back(t[i]);
if (i == dx || i == dy) {
ins ^= 1;
if (!ins)
t1.push_back(t[i]);
else
t2.push_back(t[i]);
}
}
sol(t1);
sol(t2);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n - 3; i++) {
scanf("%d%d", &nowx[i], &nowy[i]);
ok[nowx[i]][nowy[i]] = ok[nowy[i]][nowx[i]] = true;
}
for (int i = 1; i <= n - 3; i++) {
scanf("%d%d", &tarx[i], &tary[i]);
tarok[tarx[i]][tary[i]] = tarok[tary[i]][tarx[i]] = true;
if (tarx[i] > tary[i]) swap(tarx[i], tary[i]);
}
vector<int> a;
for (int i = 1; i <= n; i++) a.push_back(i);
sol(a);
printf("%d\n", cnt);
for (int i = 1; i <= cnt; i++) printf("%d %d\n", ansx[i], ansy[i]);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int f[300010][2];
int dp[300010][2];
int Flag[300010];
int main() {
int n, k;
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) scanf("%d", &f[i][0]);
for (int i = 1; i <= n; i++) scanf("%d", &f[i][1]);
if (k == 1) {
for (int ii = 0; ii < 2; ii++) {
int r = ii, s = ii ^ 1;
for (int i = 1; i <= n; i++) {
int nr = f[i][r], ns = f[i][s];
int ss = nr + ns;
if (nr != ((ss + 1) / 2) || ns != (ss / 2)) {
Flag[ii] = 1;
break;
}
if (ss & 1) swap(r, s);
}
}
if (Flag[1] && Flag[0])
puts("NO");
else
puts("YES");
return 0;
}
dp[0][0] = dp[0][1] = 0;
for (int i = 1; i <= n; i++) {
dp[i][0] = dp[i][1] = 0x3f3f3f3f;
for (int v = 0; v < 2; v++) {
int u = v ^ 1;
int r = dp[i - 1][v];
if (r != 0x3f3f3f3f) {
int x = f[i][v], y = f[i][u];
if (x >= y) {
if (x <= (k - r + (long long)k * y))
dp[i][v] = min((long long)dp[i][v],
max(1LL, x - (k - r) - (long long)k * (y - 1)));
if (x <= ((long long)k * (y - 1) + k - r))
dp[i][u] = min(dp[i][u], 1);
} else {
if (y <= ((long long)k * (x + 1)))
dp[i][u] = min((long long)dp[i][u], max(1LL, y - (long long)k * x));
if (y <= ((long long)k * x)) dp[i][v] = min(dp[i][v], 1);
}
}
}
}
if (dp[n][0] == 0x3f3f3f3f && dp[n][1] == 0x3f3f3f3f)
puts("NO");
else
puts("YES");
return 0;
}
| 6 |
#include <bits/stdc++.h>
#include <unistd.h>
using namespace std;
#define DEBUG(x) cerr<<#x<<": "<<x<<endl;
#define DEBUG_VEC(v) cerr<<#v<<":";for(int i=0;i<v.size();i++) cerr<<" "<<v[i]; cerr<<endl
#define DEBUG_MAT(v) cerr<<#v<<endl;for(int i=0;i<v.size();i++){for(int j=0;j<v[i].size();j++) {cerr<<v[i][j]<<" ";}cerr<<endl;}
typedef long long ll;
#define vi vector<int>
#define vl vector<ll>
#define vii vector< vector<int> >
#define vll vector< vector<ll> >
#define vs vector<string>
#define pii pair<int,int>
#define pis pair<int,string>
#define psi pair<string,int>
#define pll pair<ll,ll>
template<class S, class T> pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first + t.first, s.second + t.second); }
template<class S, class T> pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first - t.first, s.second - t.second); }
template<class S, class T> ostream& operator<<(ostream& os, pair<S, T> p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
#define X first
#define Y second
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define rrep1(i,n) for(int i=(n);i>0;i--)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define in(x, a, b) (a <= x && x < b)
#define all(c) c.begin(),c.end()
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a = b; return 1; } return 0; }
#define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end());
const ll inf = 1000000001;
const ll INF = (ll)1e18 + 1;
const ll MOD = 1000000007;
//const ll MOD = 998244353;
const double pi = 3.14159265358979323846;
#define Sp(p) cout<<setprecision(15)<< fixed<<p<<endl;
int dx[4] = { 1,0, -1,0 }, dy[4] = { 0,1,0,-1 };
int dx2[8] = { 1,1,0,-1,-1,-1,0,1 }, dy2[8] = { 0,1,1,1,0,-1,-1,-1 };
#define fio() cin.tie(0); ios::sync_with_stdio(false);
//#define mp make_pair
class UnionFind {
public:
int n;
vi par; //親
vi ran; //木の深さ
vi num; //要素数
UnionFind(int _n) {
n = _n;
par.resize(n); ran.resize(n); num.resize(n);
for (int i = 0; i < n; i++) {
par[i] = i; ran[i] = 0; num[i] = 1;
}
}
//木の根を求める
int find(int x) {
if (par[x] == x) {
return x;
}
else {
return par[x] = find(par[x]);
}
}
//xとyの属する集合を併合
void unite(int x, int y) {
x = find(x); y = find(y);
int numsum = num[x] + num[y];
if (x == y) {
return;
}
if (ran[x]<ran[y]) {
par[x] = y;
}
else {
par[y] = x;
if (ran[x] == ran[y]) {
ran[x]++;
}
}
num[x] = num[y] = numsum;
}
//xとyが同じ集合に属するか否か
bool same(int x, int y) {
return find(x) == find(y);
}
};
vector< pair<ll, pll> > edges;
const int N = 4010;
vector<vector<pll> > G(N), G2(N);
bitset<N> used;
vii max_c(N, vi(N));
ll kruskal(int n) {
sort(edges.begin(), edges.end());
UnionFind uf(n);
ll res = 0;
for (int i = 0; i < edges.size(); i++) {
if (!uf.same(edges[i].second.first, edges[i].second.second)) {
uf.unite(edges[i].second.first, edges[i].second.second);
res += edges[i].first;
int u = edges[i].second.first, v = edges[i].second.second;
ll c = edges[i].first;
G2[u].push_back(pll(c, v));
G2[v].push_back(pll(c, u));
}
}
return res;
}
void dfs(int s, int now, int ma) {
used[now] = true;
max_c[s][now] = ma;
rep (i, G2[now].size()) {
int v = G2[now][i].second;
if (used[v]) continue;
dfs(s, v, max(ma, (int)G2[now][i].first));
}
}
int main() {
int n, m;
cin >> n >> m;
rep (i, m) {
ll u, v, c;
cin >> u >> v >> c;
u--; v--;
G[u].push_back(pll(c, v));
G[v].push_back(pll(c, u));
edges.push_back({c, pll(u, v)});
}
sort(all(edges));
ll sum = kruskal(n);
rep (i, n) {
used.reset();
dfs(i, i, 0);
}
int q;
cin >> q;
while (q--) {
int s, t;
cin >> s >> t;
s--; t--;
cout << sum - max_c[s][t] << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 80;
int dp[maxn][maxn][maxn][3];
int inf = 1e9;
string s;
int n;
int find(int ind, int k, int v, int x) {
int ck = 0, cv = 0, cx = 0;
int cnt = 0;
for (int i = 0; i < ind; i++) {
if (s[i] == 'K') {
if (ck < k)
ck++;
else
cnt++;
}
if (s[i] == 'V') {
if (cv < v)
cv++;
else
cnt++;
}
if (s[i] == 'X') {
if (cx < x)
cx++;
else
cnt++;
}
}
return cnt;
}
void solve(int k, int v, int x) {
int nk = -1, nv = -1, nx = -1, ck = 0, cv = 0, cx = 0;
for (int i = 0; i < n; i++) {
if (s[i] == 'V' && cv <= v) {
cv++;
nv = i;
} else if (s[i] == 'K' && ck <= k) {
ck++;
nk = i;
} else if (s[i] == 'X' && cx <= x) {
cx++;
nx = i;
}
}
int fv = find(nv, k, v, x);
int fk = find(nk, k, v, x);
int fx = find(nx, k, v, x);
dp[k][v][x + 1][0] =
min(dp[k][v][x + 1][0], min(dp[k][v][x][1], dp[k][v][x][0]) + fx);
dp[k][v + 1][x][1] =
min(dp[k][v + 1][x][1], min(dp[k][v][x][1], dp[k][v][x][0]) + fv);
dp[k + 1][v][x][0] = min(dp[k + 1][v][x][0], dp[k][v][x][0] + fk);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
cin >> n;
cin >> s;
int vc = 0, kc = 0, xc = 0;
for (int i = 0; i < n; i++) {
if (s[i] != 'V' && s[i] != 'K') {
xc++;
s[i] = 'X';
}
vc += (s[i] == 'V');
kc += (s[i] == 'K');
}
for (int i = 0; i <= kc; i++) {
for (int j = 0; j <= vc; j++) {
for (int k = 0; k <= xc; k++) {
for (int t = 0; t < 2; t++) {
dp[i][j][k][t] = inf;
}
}
}
}
dp[0][0][0][0] = 0;
for (int i = 0; i <= kc; i++) {
for (int j = 0; j <= vc; j++) {
for (int k = 0; k <= xc; k++) {
solve(i, j, k);
}
}
}
cout << min(dp[kc][vc][xc][0], dp[kc][vc][xc][1]);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const int maxn = 150000;
vector<int> g[maxn + 5];
int sz[maxn + 5], dep[maxn + 5];
int fa[18][maxn + 5];
int tag[maxn + 5], ans[maxn + 5], add[maxn + 5];
long long inv;
int B;
vector<pair<int, int>> q;
int n, m;
template <class T>
inline void rd(T &x) {
x = 0;
char c = getchar();
int f = 1;
while (!isdigit(c)) {
if (c == '-') f = -1;
c = getchar();
}
while (isdigit(c)) x = x * 10 - '0' + c, c = getchar();
x *= f;
}
namespace Tree {
int len[maxn + 5], son[maxn + 5], top[maxn + 5];
int Log[maxn + 5];
vector<int> up[maxn + 5], down[maxn + 5];
void dfs(int k) {
for (auto u : g[k]) {
if (u == fa[0][k]) continue;
dfs(u);
if (len[u] > len[k]) son[k] = u;
len[k] = max(len[u], len[k]);
}
len[k] += 1;
}
void dfs1(int k, int TOP) {
top[k] = TOP;
if (son[k]) dfs1(son[k], TOP);
for (auto u : g[k]) {
if (u == fa[0][k] || u == son[k]) continue;
dfs1(u, u);
}
}
void init() {
Log[1] = 0;
for (int i = 2; i <= n; ++i) Log[i] = Log[i >> 1] + 1;
dfs(1);
dfs1(1, 1);
for (int i = 1; i <= n; ++i)
if (top[i] == i) {
up[i].resize(len[i]);
down[i].resize(len[i]);
for (int j = 0, u = i; j < len[i]; ++j, u = son[u]) down[i][j] = u;
for (int j = 0, u = i; j < len[i]; ++j, u = fa[0][u]) up[i][j] = u;
}
}
int query(int u, int k) {
if (dep[u] <= k) return 0;
if (k == 0) return u;
int tmp = Log[k];
u = fa[tmp][u];
k -= 1 << tmp;
if (dep[u] - dep[top[u]] >= k) return down[top[u]][dep[u] - dep[top[u]] - k];
k -= dep[u] - dep[top[u]];
return up[top[u]][k];
}
} // namespace Tree
long long Pow(long long a, long long b) {
long long ans = 1;
while (b) {
if (b & 1) ans = ans * a % mod;
a = a * a % mod;
b >>= 1;
}
return ans;
}
void dfs(int k, int last) {
fa[0][k] = last;
dep[k] = dep[last] + 1;
sz[k] = 1;
for (auto u : g[k])
if (u != last) dfs(u, k), sz[k] += sz[u];
}
int query(int u, int v) {
if (u == v) return n;
if (dep[u] <= dep[v]) return sz[v];
int d = dep[u] - dep[v] - 1;
u = Tree::query(u, d);
if (fa[0][u] == v)
return n - sz[u];
else
return sz[v];
}
int query(int u) {
int res = ans[u];
for (auto v : q) res = (res + 1LL * query(u, v.first) * v.second % mod) % mod;
if (res < 0) res += mod;
return res;
}
void dfs1(int k, int sum) {
ans[k] = (ans[k] + sum) % mod;
for (auto u : g[k]) {
if (u == fa[0][k]) continue;
dfs1(u, (sum + add[k] + 1LL * tag[k] * sz[u]) % mod);
}
}
void rebuild() {
int TAG = 0;
for (auto tmp : q) {
int v = tmp.first;
TAG = (TAG + 1LL * tmp.second * sz[v]) % mod;
tag[v] = (tag[v] - tmp.second) % mod;
add[v] = (add[v] + 1LL * (n - sz[v]) * tmp.second) % mod;
}
dfs1(1, 0);
for (int i = 1; i <= n; ++i)
ans[i] = (1LL * ans[i] + TAG + add[i]) % mod, tag[i] = add[i] = 0;
q.clear();
}
int main() {
rd(n), rd(m);
inv = Pow(n, mod - 2);
B = 600;
for (int i = 1; i < n; ++i) {
int u, v;
rd(u), rd(v);
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, 0);
for (int j = 1; j < 18; ++j)
for (int i = 1; i <= n; ++i) fa[j][i] = fa[j - 1][fa[j - 1][i]];
Tree::init();
while (m--) {
int op, v, d;
rd(op);
if (op == 1) {
rd(v), rd(d);
q.push_back(make_pair(v, d));
if (q.size() > B) rebuild();
} else {
rd(v);
int ans = 1LL * query(v) * inv % mod;
if (ans < 0) ans += mod;
printf("%d\n", ans);
}
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
#pragma GCC optimize("O2,Ofast,inline,unroll-all-loops,-ffast-math")
using namespace std;
const int mod = 119 << 23 | 1;
long long powmod(long long a, long long b) {
long long res = 1;
a %= mod;
assert(b >= 0);
for (; b; b >>= 1) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
}
return res;
}
const int MOD = 119 << 23 | 1;
int dp[2020][2020];
int P[2020];
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
string s;
cin >> s;
int n = s.length();
for (int i = 0; i < n; i++) {
P[i + 1] = P[i];
P[i + 1] += (s[i] == '?');
}
for (int Len = 2; Len < n + 1; Len++) {
for (int l = 0; l < n - Len + 1; l++) {
int r = l + Len - 1;
if (s[l] != '(') {
(dp[l][r] += dp[l + 1][r]) %= mod;
}
if (s[r] != ')') {
(dp[l][r] += dp[l][r - 1]) %= mod;
}
if (s[l] != '(' && s[r] != ')') {
((dp[l][r] -= dp[l + 1][r - 1]) += mod) %= mod;
}
if (s[l] != ')' && s[r] != '(') {
(dp[l][r] += dp[l + 1][r - 1]) %= mod;
(dp[l][r] += powmod(2, P[r] - P[l + 1])) %= mod;
}
}
}
cout << dp[0][n - 1];
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long ma = 500005;
int main() {
int t;
cin >> t;
while (t--) {
int n, a[ma];
cin >> n;
int i;
for (i = 0; i < n; i++) scanf("%d", &a[i]);
sort(a, a + n);
int ans = 0;
int sun = 0;
for (i = 0; i < n; i++) {
sun++;
if (a[i] == sun) {
ans++;
sun = 0;
}
}
printf("%d\n", ans);
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
bool isPrime(int n) {
for (int i = 2; i <= n / i; i++)
if (n % i == 0) return 0;
return 1;
}
int main() {
int n;
cin >> n;
if (n == 2)
cout << "1\n";
else if (n % 2 == 0)
cout << "2\n";
else if (isPrime(n))
cout << "1\n";
else if (isPrime(n - 2))
cout << "2\n";
else
cout << "3\n";
return 0;
}
| 2 |
#include <iostream>
using namespace std;
int a, b, c, x, y, r;
int main() {
cin >> a >> b >> c >> x >> y;
cout << min(min(a*x+b*y, c*2*max(x,y)), min(x,y)*2*c+abs(x-y)*(x>y?a:b));
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int mabs(int i) { return abs(i) - 1; }
int main() {
int n, m;
cin >> n >> m;
string s;
vector<int> v;
vector<int> guilty(n);
vector<int> not_guilty(n);
vector<bool> is_possible_killer(n);
int not_guilty_sum = 0;
for (int i = 0; i < n; ++i) {
cin >> s;
v.push_back(atoi(s.c_str()));
if (v[i] > 0) {
guilty[mabs(v[i])]++;
} else {
not_guilty[mabs(v[i])]++;
not_guilty_sum++;
}
}
int number_of_possible_killers = 0;
for (int i = 0; i < n; ++i) {
if (guilty[i] + (not_guilty_sum - not_guilty[i]) == m) {
number_of_possible_killers++;
is_possible_killer[i] = true;
}
}
for (int i = 0; i < n; ++i) {
if (!is_possible_killer[mabs(v[i])] && v[i] > 0 ||
is_possible_killer[mabs(v[i])] && v[i] < 0 &&
number_of_possible_killers == 1) {
cout << "Lie" << endl;
} else if (!is_possible_killer[mabs(v[i])] && v[i] < 0 ||
is_possible_killer[mabs(v[i])] && v[i] > 0 &&
number_of_possible_killers == 1) {
cout << "Truth" << endl;
} else {
cout << "Not defined" << endl;
}
}
}
| 2 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.