solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
struct Edge{
int src,dst,cost;
bool isl;
Edge(int s,int d,int c) : src(s), dst(d), cost(c) {isl = false;}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
int dist[21];
bool visited[21],isleaf[21],ispar[21];
bool dfs(Graph &g,int v,int d) {
visited[v] = true;
dist[v] = d;
bool leaf = true;
for(int i=0; i<g[v].size(); ++i) {
if(visited[g[v][i].dst]) continue;
leaf = false;
bool k = dfs(g,g[v][i].dst, d+g[v][i].cost);
if(k) {
ispar[v] = true;
g[v][i].isl = true;
for(int k=0; k<g[g[v][i].dst].size(); ++k) {
if(g[g[v][i].dst][k].dst == v)
g[g[v][i].dst][k].isl = true;
}
}
}
return leaf;
}
int main() {
int N,a,b,t;
while(cin>>N, N) {
Graph g(N);
memset(visited, false, sizeof(visited));
memset(ispar, false, sizeof(ispar));
memset(isleaf, false, sizeof(isleaf));
for(int i=0; i<N-1; ++i) {
cin>>a>>b>>t;
a--,b--;
g[a].push_back(Edge(a,b,t));
g[b].push_back(Edge(b,a,t));
}
dfs(g,0,0);
int md = 0;
int ans = 0;
for(int i=0; i<N; ++i)
if(ispar[i])
md = max(dist[i], md);
for(int i=0; i<N; ++i)
for(int j=0; j<g[i].size(); ++j)
if(!g[i][j].isl) {
ans += g[i][j].cost;
//cout<<g[i][j].src<<" "<<g[i][j].dst<<endl;
}
//cout<<ans<<" - "<<md<<endl;
ans -= md;
cout<<ans<<endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int n, d, k, START;
int main() {
cin >> n >> d >> k;
if (n == 3 && d == 1 && k == 1) {
cout << "-1";
return 0;
}
if (n == 2 && d == 1) {
cout << "1 2\n";
return 0;
}
if (d == k && d == 1) {
cout << "-1";
return 0;
}
if (d == k)
START = 2;
else
START = 1;
if (d - k > k) {
cout << -1;
return 0;
}
for (int i = 1; i <= k; ++i)
if (i + 1 <= n) cout << i << " " << i + 1 << "\n";
if (k + 2 <= n) cout << START << " " << k + 2 << "\n";
int Nxt = d - k + 1;
int X = k + 2;
if (START == 2) X = k + 3;
for (int i = X; i <= k + Nxt - 1; ++i)
if (i + 1 <= n) cout << i << " " << i + 1 << "\n";
int Start = k + Nxt + 1;
if (START == 2) Start++;
for (int i = Start; i <= n; ++i)
if (i <= n) cout << START << " " << i << "\n";
return 0;
}
| 3 |
#include<bits/stdc++.h>
#define M_PIl 3.141592653589793238462643383279502884L /* pi */
#define lld long long int
#define ld long double
#define mod 1000000007
#define modd 998244353
#define all(v) v.begin(),v.end()
#define rep(i,a,b) for(lld i=a;i<=b;i++)
#define repr(i,a,b) for(lld i=a;i>=b;i--)
#define ar array
#define pb push_back
#define mp make_pair
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
lld n,m;
lld arr[500001];
lld dp[500001];
lld las[500001];
lld lazy[5000000];
ar<lld,2> sum[5000000];
lld ele[5000000];
void update1(lld node,lld e,lld b,lld l,lld r,ar<lld,2> val){
if(r<e|l>b)return ;
if(l<=e&&b<=r){
sum[node]=val;
return ;
}
lld mid=(e+b)/2;
update1(2*node+1,e,mid,l,r,val);
update1(2*node+2,mid+1,b,l,r,val);
sum[node]=max(sum[2*node+1],sum[2*node+2]);
}
void update2(lld node,lld e,lld b,lld l,lld r,lld val){
if(r<e|l>b)return ;
if(l<=e&&b<=r){
ele[node]=val;
return ;
}
lld mid=(e+b)/2;
update2(2*node+1,e,mid,l,r,val);
update2(2*node+2,mid+1,b,l,r,val);
ele[node]=max(ele[2*node+1],ele[2*node+2]);
}
ar<lld,2> query1(lld node,lld e,lld b,lld l,lld r){
if(r<e||l>b){
return (ar<lld,2>){LLONG_MIN,0};
}
if(l<=e&&b<=r){
return sum[node];
}
lld mid=(e+b)/2;
return max(query1(2*node+1,e,mid,l,r),query1(2*node+2,mid+1,b,l,r));
}
lld query2(lld node,lld e,lld b,lld l,lld r){
if(r<e||l>b){
return LLONG_MIN;
}
if(l<=e&&b<=r){
return ele[node];
}
lld mid=(e+b)/2;
return max(query2(2*node+1,e,mid,l,r),query2(2*node+2,mid+1,b,l,r));
}
int main()
{
ios;
lld TESTS,q,a,b,l,r,c,k,p,h,w,x,y,z,xs,ys,t,f;
ld d;
TESTS=1;
cin>>TESTS;
while(TESTS--)
{
cin>>n;
rep(i,1,n)cin>>arr[i];
rep(i,1,n){
update1(0,1,n,i,i,{0,i});
update2(0,1,n,i,i,0);
las[i]=0;
dp[i]=0;
}
rep(i,1,n){
ar<lld,2> cur=query1(0,1,n,1,arr[i]);
lld extra=0;
if(las[cur[1]]+1<=i-1){
lld now=query2(0,1,n,las[cur[1]]+1,i-1);
if(now>arr[i])extra=1;
}
las[arr[i]]=i;
dp[arr[i]]=cur[0]+extra+1;
update1(0,1,n,arr[i],arr[i],{dp[arr[i]],arr[i]});
update2(0,1,n,i,i,arr[i]);
}
lld ans=0;
rep(i,1,n)ans=max(ans,dp[i]);
cout<<ans<<"\n";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
const long long N = 1e5 + 5;
long long powm(long long a, long long b) {
long long res = 1;
while (b) {
if (b & 1) res = (res * a) % 1000000007;
a = (a * a) % 1000000007;
b >>= 1;
}
return res;
}
long long pow(long long a, long long b) {
long long res = 1;
while (b) {
if (b & 1) res = (res * a);
a = (a * a);
b >>= 1;
}
return res;
}
vector<long long> cnt(N, 0);
signed main(void) {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
vector<long long> v(1e5 + 5, 1);
for (long long i = 2; i < 1e5 + 5; i++) {
if (v[i] == 1) {
v[i] = i;
for (long long j = i * i; j < 1e5 + 5; j += i) v[j] = i;
}
}
long long n;
cin >> n;
vector<long long> a(n);
long long mx = 0;
for (long long i = 0; i < n; i++) {
cin >> a[i];
cnt[a[i]]++;
mx = max(mx, a[i]);
}
for (long long i = 1; i < 1e5 + 2; i++) {
cnt[i] += cnt[i - 1];
}
long long ans = 0;
sort((a).begin(), (a).end());
for (long long i = 1; i < mx + 1; i++) {
long long x = sqrt(i);
long long val = 1;
vector<long long> div;
for (long long j = 1; j < x + 1; j++) {
if (i % j == 0) {
div.push_back(j);
if (j * j != i) div.push_back(i / j);
}
}
div.push_back(mx + 1);
sort((div).begin(), (div).end());
for (long long j = 0; j < (long long)div.size() - 1; j++) {
if (j == (long long)div.size() - 2) {
long long c = cnt[div[j + 1] - 1] - cnt[div[j] - 1];
long long b = powm(j + 1, c) - powm(j, c);
b = (b + 1000000007) % 1000000007;
val *= b;
} else {
long long c = cnt[div[j + 1] - 1] - cnt[div[j] - 1];
val *= powm(j + 1, c);
}
val %= 1000000007;
}
ans += val;
ans %= 1000000007;
}
cout << ans << '\n';
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
const int MOD = 1e9+7;
string s;
int d;
int memo[10005][105][3];
int dp(int idx, int tot, int stat){
if(idx>=s.size()){
if(tot == 0) return 1;
else return 0;
}
int &ret = memo[idx][tot][stat];
if(ret!=-1) return ret;
ret = 0;
for(int i=0;i<10;i++){
if(stat == 0 && i>s[idx]-'0') break;
if(s[idx]-'0' == i && stat == 0) ret+= dp(idx+1,(tot+i)%d,0);
else ret+= dp(idx+1,(tot+i)%d,1);
ret%=MOD;
}
return ret;
}
int main(){
memset(memo,-1,sizeof(memo));
cin >> s;
cin >> d;
int ans = dp(0,0,0);
printf("%d\n",(ans+MOD-1)%MOD);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 10000 + 10;
int N;
int a[MAX];
int main() {
while (cin >> a[++N])
;
--N;
int d1 = a[2] - a[1];
int d2 = a[3] - a[2];
int d3 = a[4] - a[3];
if (d1 == d2 && d2 == d3) {
cout << a[4] + d1 << endl;
return 0;
}
if (a[1] == 0 || a[2] == 0 || a[3] == 0 || a[4] == 0) {
cout << 42 << endl;
return 0;
}
double p1 = (double)a[2] / a[1];
double p2 = (double)a[3] / a[2];
double p3 = (double)a[4] / a[3];
if (p1 == p2 && p2 == p3) {
double t = a[4] * p3;
if (abs(round(t) - t) < 1e-10)
cout << round(t) << endl;
else
cout << 42 << endl;
} else
cout << 42 << endl;
return 0;
}
| 1 |
#include <iostream>
#include <algorithm>
using namespace std;
int n;
int main() {
cin >> n;
int ans = 0;
while (n) {
ans += n % 10;
n /= 10;
}
if (ans == 1) ans *= 10;
cout << ans << endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int ttl = 0;
int c = 0;
while (ttl < n) {
for (int i = 1; i <= 3 && ttl < n; i++) {
ttl++;
printf("%d %d\n", c, (i / 3) * 3);
if (i != 2) c++;
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int t, n, m;
int ans;
vector<vector<int> > p;
bool check(int x) {
vector<bool> flag(n);
bool pair = false;
for (int i = 0; i < m; i++) {
int cnt = 0;
for (int j = 0; j < n; j++) {
if (p[i][j] >= x) {
flag[j] = true;
cnt++;
}
}
if (cnt > 1) pair = true;
}
if (!pair) return false;
int ans = true;
for (bool x : flag) {
ans = ans && x;
}
return ans;
}
int main() {
cin >> t;
while (t--) {
cin >> m >> n;
p.assign(m, vector<int>(n));
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) cin >> p[i][j];
}
long long l = 1, r = 1e9;
while (l <= r) {
int mid = (l + r) / 2;
if (check(mid)) {
ans = mid;
l = mid + 1;
} else
r = mid - 1;
}
cout << ans << endl;
}
return 0;
}
| 4 |
#include <iostream>
#include <algorithm>
typedef long long ll;
using namespace std;
ll dp[3010][3010][4]={}, item[3010][3010]={};
int main() {
int R, C, K;
cin >> R >> C >> K;
for(int i=0; i<K; ++i){
int r, s;
ll v;
cin >> r >> s >> v;
item[r][s]=v;
}
for(int i=1; i<=R; ++i){
for(int j=1; j<=C; ++j){
for(int k=0; k<=3; ++k){
dp[i][j][0]=max(dp[i][j][0], dp[i-1][j][k]);
dp[i][j][k]=max(dp[i][j][k], dp[i][j-1][k]);
}
if(item[i][j]){
for(int k=2; k>=0; --k){
dp[i][j][k+1]=max(dp[i][j][k+1], dp[i][j][k]+item[i][j]);
}
}
}
}
ll ans=0;
for(int i=0; i<=3; ++i) ans=max(ans, dp[R][C][i]);
cout << ans << endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
bool isvowel(char c) {
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') return true;
return false;
}
long long int isprime(long long int n) {
for (long long int i = 2; i <= sqrt(n); i++) {
if (n % i == 0) return 0;
}
return 1;
}
long long int power(long long int a, long long int b) {
if (b == 0) return 1;
if (b == 1) return a % 1000000007;
long long int temp = power(a, b / 2);
if (b % 2 == 0)
return ((temp % 1000000007) * (temp % 1000000007)) % 1000000007;
else
return ((((temp % 1000000007) * (temp % 1000000007)) % 1000000007) * a %
1000000007) %
1000000007;
}
long long int c(long long int n, long long int k) {
long long int C[n + 1][k + 1];
long long int i, j;
for (i = 0; i <= n; i++) {
for (j = 0; j <= min(i, k); j++) {
if (j == 0 || j == i)
C[i][j] = 1;
else
C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]);
}
}
return C[n][k];
}
long long int modInverse(long long int a, long long int m) {
long long int m0 = m;
long long int y = 0, x = 1;
if (m == 1) return 0;
while (a > 1) {
long long int q = a / m;
long long int t = m;
m = a % m, a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0) x += m0;
return x;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t;
cin >> t;
while (t--) {
long long int n;
cin >> n;
vector<long long int> v;
long long int s = 1, sum = 1;
v.push_back(s);
while (sum < n) {
s = s * 2;
v.push_back(s);
sum += s;
}
if (sum != n) {
v.pop_back();
sum -= s;
v.push_back(n - sum);
sort(v.begin(), v.end());
}
cout << v.size() - 1 << endl;
for (long long int i = 1; i < v.size(); i++) cout << v[i] - v[i - 1] << " ";
cout << endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
inline void file() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
if (0) {
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
}
}
const clock_t MAXT = (100 * CLOCKS_PER_SEC) / 1000;
const int PX[5] = {0, 0, 1, -1}, PY[5] = {-1, 1, 0, 0}, N = 503, INF = 1e9;
const long long INFL = 1e18, MOD = 1e9 + 7;
const long double EPS = 1e-6;
int n, len, max_err, mod, a[N];
int dp_old[N][N], dp_new[N][N];
inline void add(int& a, int b) {
a += b;
while (a >= mod) a -= mod;
}
int main() {
file();
cin >> n >> len >> max_err >> mod;
for (int i = 1; i <= n; ++i) cin >> a[i];
dp_new[0][0] = 1;
for (int i = 0; i < len; ++i) {
for (int j = 0; j <= max_err; ++j)
for (int k = 0; k <= n; ++k) {
dp_old[j][k] = dp_new[j][k];
dp_new[j][k] = 0;
}
for (int j = 0; j <= max_err; ++j)
for (int k = 1; k <= n; ++k) {
add(dp_old[j][k], dp_old[j][k - 1]);
if (a[k] + j <= max_err) add(dp_new[j + a[k]][k], dp_old[j][k]);
}
}
int ans = 0;
for (int i = 0; i <= max_err; ++i)
for (int j = 0; j <= n; ++j) add(ans, dp_new[i][j]);
cout << ans;
}
| 1 |
#include <iostream>
#include <cstdio>
using namespace std;
class list
{
struct node
{
int val;
node* prev;
node* next;
node(int n) : val(n), prev(NULL), next(NULL) {}
node(int n, node* prev_p, node* next_p) : val(n), prev(prev_p), next(next_p) {}
};
node* nil;
node* cur;
public:
list()
{
nil = new node(0);
nil->prev = nil;
nil->next = nil;
cur = nil;
}
~list() { clear(); }
void insert(int val);
void move(int d);
void erase();
void dump();
void clear();
private:
void moveForward(int d);
void moveBackword(int d);
};
void list::insert(int val)
{
// 新しいオブジェクトを作成する
// 新しいオブジェクトのprevをcurのprevにする
// 新しいオブジェクトのnextをcurにする
node* newobj = new node(val, cur->prev, cur);
// curのprevのnextを新しいオブジェクトにする
cur->prev->next = newobj;
// curのprevを新しいオブジェクトにする
cur->prev = newobj;
// curを新しいオブジェクトにする
cur = newobj;
}
void list::move(int d)
{
if (d == 0) return;
if (d > 0) moveForward(d);
else moveBackword(d * (-1));
}
void list::erase()
{
if (cur == nil) return;
// curのnexeのprevをcurのprevにする
cur->next->prev = cur->prev;
// curのprevのnextをcurのnextにする
cur->prev->next = cur->next;
node* p = cur;
cur = cur->next;
delete p;
}
void list::moveForward(int d)
{
while (d > 0 && cur != nil)
{
cur = cur->next;
--d;
}
}
void list::moveBackword(int d)
{
while (d > 0)
{
cur = cur->prev;
--d;
}
}
void list::dump()
{
node* p = nil->next;
while (p != nil)
{
printf("%d\n", p->val);
p = p->next;
}
}
void list::clear()
{
node* p = nil->next;
node* tmp;
while (p != nil)
{
tmp = p;
p = p->next;
delete tmp;
}
delete nil;
}
int main()
{
int q, i, c, n;
list arylist;
scanf("%d", &q);
for (i = 0; i < q; i++)
{
scanf("%d", &c);
switch (c)
{
case 0:
scanf("%d", &n);
arylist.insert(n);
break;
case 1:
scanf("%d", &n);
arylist.move(n);
break;
case 2:
arylist.erase();
break;
}
}
arylist.dump();
return 0;
}
| 0 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
const int maxn = 100010;
int n, m, a[maxn];
void print(int x) {
if (x >= 10) print(x / 10);
putchar(x % 10 + '0');
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (int i = 1, op, l, r, x; i <= m; i++) {
scanf("%d %d %d %d", &op, &l, &r, &x);
if (op == 1) {
int j = l;
int *b = a + l;
for (; j + 7 <= r; j += 8) {
*b -= *b > x ? x : 0, b++;
*b -= *b > x ? x : 0, b++;
*b -= *b > x ? x : 0, b++;
*b -= *b > x ? x : 0, b++;
*b -= *b > x ? x : 0, b++;
*b -= *b > x ? x : 0, b++;
*b -= *b > x ? x : 0, b++;
*b -= *b > x ? x : 0, b++;
}
while (j <= r) {
a[j] -= a[j] > x ? x : 0, j++;
}
} else {
int ans = 0, j = l;
int *b = a + l;
for (; j + 7 <= r; j += 8) {
ans += *b == x, b++;
ans += *b == x, b++;
ans += *b == x, b++;
ans += *b == x, b++;
ans += *b == x, b++;
ans += *b == x, b++;
ans += *b == x, b++;
ans += *b == x, b++;
}
while (j <= r) {
ans += a[j] == x, j++;
}
print(ans), putchar('\n');
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007ll;
long long n, q, t, ma, a[301], to[301], f[100001];
bool m[301], in[301], vis[301];
void dfs(long long u, long long d, long long s) {
ma = d, vis[u] = true;
if (to[u]) dfs(to[u], d + 1, s + a[u]);
t -= (ma - d) * a[u], a[u] += s;
}
int main() {
cin >> n >> q >> t, f[0] = 1;
for (int i = 1; i <= n; cin >> a[i], ++i)
;
for (int i = 1; i <= q; ++i) {
long long b, c;
cin >> b >> c;
m[b] = m[c] = true, in[c] = true, to[b] = c;
}
for (int i = 1; i <= n; ++i)
if (m[i] && !in[i]) ma = 0, dfs(i, 0, 0);
if (t < 0) {
cout << 0 << endl;
return 0;
}
for (int i = 1; i <= n; ++i)
if (m[i] && !vis[i]) {
cout << 0 << endl;
return 0;
}
for (int i = 1; i <= n; ++i)
for (int j = 0; j <= t - a[i]; ++j) (f[j + a[i]] += f[j]) %= mod;
cout << f[t] << endl;
return 0;
}
| 5 |
#include<bits/stdc++.h>
using namespace std;
double n,d;
int main(){
cin>>n>>d;
cout<<ceil(n/(d*2+1));
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, x[110], y[110], vis[110], c = -1;
void dfs(int p) {
vis[p] = 1;
for (int i = 1; i <= n; i++) {
if ((x[i] == x[p] || y[i] == y[p]) && vis[i] == 0) dfs(i);
}
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) cin >> x[i] >> y[i];
for (int i = 1; i <= n; i++)
if (vis[i] == 0) dfs(i), c++;
cout << c;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int x[100001];
int main() {
int t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> x[i];
}
sort(x, x + n, greater<int>());
int num = 1;
int ans = 0;
for (int i = 0; i < n; i++) {
if (x[i] * num >= m) {
ans++;
num = 1;
} else {
num++;
}
}
cout << ans << endl;
}
return 0;
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int h = 0;
for (int i = n; i > 0; i /= 10) h += i % 10;
cout << (n % h == 0 ? "Yes" : "No") << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 100000 + 10;
vector<int> G[MAX_N], ans;
int l[MAX_N], goal[MAX_N], n, x, y, dep[MAX_N], mark[MAX_N];
void dfs(int u, int a, int b) {
mark[u] = 1;
int aa, bbb;
aa = a;
bbb = b;
if (dep[u] % 2 == 1) {
if (b == 1) {
l[u] = 1 - l[u];
}
} else {
if (a == 1) {
l[u] = 1 - l[u];
}
}
if (l[u] != goal[u]) {
ans.push_back(u);
if (dep[u] % 2 == 0) {
aa = 1 - aa;
} else {
bbb = 1 - bbb;
}
}
for (int i = 0; i < G[u].size(); i++)
if (mark[G[u][i]] == 0) {
dep[G[u][i]] = dep[u] + 1;
dfs(G[u][i], aa, bbb);
}
}
int main() {
cin >> n;
for (int i = 1; i < n; i++) {
cin >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
for (int i = 1; i <= n; i++) {
cin >> l[i];
}
for (int i = 1; i <= n; i++) {
cin >> goal[i];
}
dfs(1, 0, 0);
cout << ans.size() << endl;
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << endl;
}
}
| 3 |
//#pragma GCC target ("avx2")
//#pragma GCC optimize("O3")
//#pragma GCC optimization ("unroll-loops")
//#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
//#include <ext/rope>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_cxx;
//using namespace __gnu_pbds;
#define files(fin, fout) freopen(fin, "r", stdin); freopen(fout, "w", stdout)
#define foru(i, n) for(int i = 0; i < n; ++i)
#define fori(i, j, n) for(int i = j; i < n; ++i)
#define vi vector <int>
#define pii pair <int, int>
#define pb push_back
#define mp make_pair
#define mpm(a, b) make_pair(min(a, b), max(a, b))
#define mpint(a, b) ((ll)(a) * (INT_MAX) + (b))
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define mbort(a) sort(all(a))
#define cinm(a) for(auto &i : a) cin >> i
#define sz(a) (int)(a).size()
#define coutm(a) for(auto &i : a) cout << i << ' '; cout << '\n'
#define debug(x) cout << #x << ": " << x << '\n'
#define debugm(x) cout << #x << ":\n"; coutm(x)
#define ff first
#define ss second
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const signed inf = 1000000100;
const ll inf64 = 1ll * inf * inf;
const ll mod = 1e9 + 7;
mt19937 rng(0);
#define int long long
//typedef tree<string, null_type, less_equal<string>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
void run() {
int n;
cin >> n;
vector <int> ans;
map <int, int> cnt;
for(int i = 2; i * i <= n; ++i){
while(n % i == 0){
cnt[i]++;
n /= i;
}
}
if(n != 1)
cnt[n]++;
int mx = 0;
for(auto i : cnt){
if(i.ss == 0)
continue;
if(i.ss > cnt[mx]){
mx = i.ff;
}
}
ans.resize(cnt[mx]);
for(int i = 0; i < cnt[mx] - 1; ++i){
ans[i] = mx;
}
ans[ans.size() - 1] = mx;
for(auto i : cnt){
if(i.ff == mx)
continue;
if(i.ff == 0)
continue;
for(int j = 0; j < i.ss; ++j){
ans[ans.size() - 1] *= i.ff;
}
}
cout << ans.size() << '\n';
coutm(ans);
}
signed main(){
//files("input.txt", "output.txt");
//files("spiral.in", "spiral.out");
iostream::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while(t--){
run();
cout.flush();
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
map<int, vector<int>> v;
int x;
for (int i = 0; i < n; i++) {
cin >> x;
v[x].push_back(i + 1);
}
int cnt = 0;
vector<pair<int, int>> ans;
for (auto itr : v) {
vector<int> a = itr.second;
sort(a.begin(), a.end());
if (a.size() == 1) {
cnt++;
ans.push_back({itr.first, 0});
} else if (a.size() == 2) {
cnt++;
ans.push_back({itr.first, (a[1] - a[0])});
} else {
bool flag = true;
int d = a[1] - a[0];
for (int i = 2; i < a.size(); i++) {
if (!(a[i] == a[i - 1] + d)) {
flag = false;
break;
}
}
if (flag) {
cnt++;
ans.push_back({itr.first, d});
}
}
}
cout << cnt << endl;
for (int i = 0; i < ans.size(); i++) {
cout << ans[i].first << " " << ans[i].second << endl;
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int sum = 0;
int mini = 101;
for (int i = 0; i < 4; ++i) {
int s;
cin >> s;
sum += s;
mini = min(s, mini);
}
int s1, s2;
cin >> s1 >> s2;
cout << sum - mini + max(s1, s2) << '\n';
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
int par[maxn],sz[maxn],x[maxn],y[maxn],n,m;
long long now,ans[maxn];
int find(int x)
{
if(par[x]==x)return x;
else return par[x]=find(par[x]);
}
int main()
{
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)
{
par[i]=i;
sz[i]=1;
}
for(int i=1;i<=m;i++)
scanf("%d %d",&x[i],&y[i]);
now=(long long)n*(n-1)/2;
for(int i=m;i>=1;i--)
{
ans[i]=now;
int X=x[i],Y=y[i];
X=find(X);Y=find(Y);
if(X==Y)continue;
now-=sz[X]*sz[Y];
par[X]=Y;
sz[Y]+=sz[X];
}
for(int i=1;i<=m;i++)
printf("%lld\n",ans[i]);
return 0;
}
| 0 |
#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
long long mul_inv(long long a, long long b = 1000000007) {
long long b0 = b, t, q;
long long x0 = 0, x1 = 1;
if (b == 1) return 1;
while (a > 1) {
q = a / b;
t = b, b = a % b, a = t;
t = x0, x0 = x1 - q * x0, x1 = t;
}
if (x1 < 0) x1 += b0;
return x1;
}
int n, m, p, s;
long long d[2005][22];
long long e[2005][22];
long long fac[300000], ifac[300000];
vector<pair<int, int> > a;
long long ncr(long long n, long long r) {
return fac[n] * ifac[r] % 1000000007 * ifac[n - r] % 1000000007;
}
int main() {
int i, j, k, l;
fac[0] = 1;
for (i = 1; i < 300000; i++) fac[i] = fac[i - 1] * i, fac[i] %= 1000000007;
for (i = 0; i < 300000; i++) ifac[i] = mul_inv(fac[i]);
cin >> n >> m >> p >> s;
for (i = 0; i < p; i++) {
int x, y;
scanf("%d%d", &x, &y), x--, y--;
if (x == 0 && y == 0)
s = (s + 1) / 2;
else if (x == n - 1 && y == n - 1)
s = (s + 1) / 2;
else
a.emplace_back(x, y);
}
a.emplace_back(0, 0);
a.emplace_back(n - 1, m - 1);
sort((a).begin(), (a).end());
d[0][0] = e[0][0] = 1;
for (i = 1; i < a.size(); i++) {
d[i][0] = ncr(a[i].first + a[i].second, a[i].first);
for (j = 1; j < 22; j++) {
for (k = 0; k < i; k++) {
if (a[k].first <= a[i].first && a[k].second <= a[i].second) {
d[i][j] += e[k][j - (i == a.size() - 1 ? 0 : 1)] *
ncr(a[i].first + a[i].second - a[k].first - a[k].second,
a[i].first - a[k].first);
d[i][j] %= 1000000007;
}
}
}
for (k = 0; k < 21; k++)
e[i][k] = (d[i][k] - d[i][k + 1] + 1000000007) % 1000000007;
e[i][21] = d[i][21];
}
long long ans = 0;
for (i = 0; i < 22; i++) {
ans += e[a.size() - 1][i] * s;
ans %= 1000000007;
s = (s + 1) / 2;
}
ans *= mul_inv(ncr(n - 1 + m - 1, m - 1));
ans %= 1000000007;
cout << ans;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e9;
const long long N = 400007;
const long long mod = 1e9 + 7;
const double eps = 1e-6;
const double pi = acos(-1.0);
inline long long read() {
long long s = 0, w = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
return s * w;
}
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long ksm(long long a, long long b) {
long long res = 1;
while (b) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
long long fa[N];
long long find(long long x) { return x == fa[x] ? x : (fa[x] = find(fa[x])); }
long long merge(long long x, long long y) {
long long xx = find(x), yy = find(y);
if (xx == yy) return 0;
fa[xx] = yy;
return 1;
}
long long prime[5000007], sz;
bitset<10000007> vis;
void get_prime() {
vis[1] = 1;
for (long long i = (2); i <= (10000006); ++i) {
if (!vis[i]) prime[++sz] = i;
for (long long j = (1); j <= (sz); ++j) {
if (i * prime[j] > 10000006) break;
vis[i * prime[j]] = 1;
if (i % prime[j] == 0) break;
}
}
}
long long T, n, m;
char s[N];
long long g[5007][5007];
long long dp[N];
void solve() {
n = read();
scanf("%s", s + 1);
for (long long i = (1); i <= (n + 1); ++i)
for (long long j = (1); j <= (n + 1); ++j) g[i][j] = 0;
for (long long i = (1); i <= (n); ++i) dp[i] = n - i + 1;
for (long long i = (n); i >= (1); --i)
for (long long j = (n); j >= (1); --j) {
if (s[i] == s[j])
g[i][j] = g[i + 1][j + 1] + 1;
else
g[i][j] = 0;
}
for (long long i = (1); i <= (n); ++i)
for (long long j = (1); j <= (i - 1); ++j) {
if (i + g[i][j] <= n and s[i + g[i][j]] > s[j + g[i][j]])
dp[i] = max(dp[i], dp[j] + n - i - g[i][j] + 1);
}
printf("%lld\n", *max_element(dp + 1, dp + 1 + n));
}
signed main() {
T = read();
while (T--) solve();
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 500010;
int lft[maxn << 2], rht[maxn << 2], M1[maxn << 2], M2[maxn << 2];
void build(int n, int l, int r) {
lft[n] = l, rht[n] = r;
if (l == r) return;
int mid = (l + r) / 2;
build(n << 1, l, mid);
build((n << 1) + 1, mid + 1, r);
}
void upd1(int n, int p, int k) {
M1[n] = k;
if (lft[n] == rht[n]) return;
int mid = (lft[n] + rht[n]) / 2;
if (p <= mid)
upd1(n << 1, p, k);
else
upd1((n << 1) + 1, p, k);
}
int get1(int n, int l, int r) {
if (lft[n] == l && rht[n] == r) return M1[n];
int mid = (lft[n] + rht[n]) / 2;
if (r <= mid)
return get1(n << 1, l, r);
else if (l > mid)
return get1((n << 1) + 1, l, r);
return max(get1(n << 1, l, mid), get1((n << 1) + 1, mid + 1, r));
}
void upd2(int n, int l, int r, int k) {
if (lft[n] == l && rht[n] == r) {
M2[n] = k;
return;
}
int mid = (lft[n] + rht[n]) / 2;
if (r <= mid)
upd2(n << 1, l, r, k);
else if (l > mid)
upd2((n << 1) + 1, l, r, k);
else
upd2(n << 1, l, mid, k), upd2((n << 1) + 1, mid + 1, r, k);
}
int get2(int n, int p) {
if (lft[n] == rht[n]) return M2[n];
int mid = (lft[n] + rht[n]) / 2;
int ans;
if (p <= mid)
ans = get2(n << 1, p);
else
ans = get2((n << 1) + 1, p);
return max(ans, M2[n]);
}
int tou[maxn], wei[maxn], chuo, fa[maxn];
int first[maxn], nxt[maxn << 1], vv[maxn << 1];
void dfs(int n) {
tou[n] = ++chuo;
for (int e = first[n]; e; e = nxt[e])
if (vv[e] - fa[n]) fa[vv[e]] = n, dfs(vv[e]);
wei[n] = chuo;
}
int main() {
int n, m, i, j, k;
cin >> n;
int e = 2, u, v;
for (i = 1; i < n; i++) {
scanf("%d%d", &u, &v);
nxt[e] = first[u], vv[e] = v, first[u] = e++;
nxt[e] = first[v], vv[e] = u, first[v] = e++;
}
dfs(1);
cin >> m;
build(1, 1, n);
for (i = 1; i <= m; i++) {
scanf("%d%d", &j, &k);
if (j == 1)
upd2(1, tou[k], wei[k], i);
else if (j == 2)
upd1(1, tou[k], i);
else {
int m1 = get1(1, tou[k], wei[k]);
int m2 = get2(1, tou[k]);
if (m1 >= m2)
puts("0");
else
puts("1");
}
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9;
const long long md = 1e9 + 7;
const long double eps = 1e-9;
const int maxn = 1e3 + 10;
mt19937 rnd(100500);
int n, ans;
string s[600];
int main() {
ios_base::sync_with_stdio(false);
;
cin.tie(NULL);
;
cin >> n;
for (int i = 0; i < n; i++) cin >> s[i];
ans = 0;
for (int i = 1; i < n - 1; i++) {
for (int j = 1; j < n - 1; j++) {
if (s[i][j] == s[i - 1][j - 1] && s[i][j] == s[i - 1][j + 1] &&
s[i][j] == s[i + 1][j - 1] && s[i][j] == s[i + 1][j + 1] &&
s[i][j] == 'X')
ans++;
}
}
cout << ans;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int n, answer;
bool p1[2005], p2[2005], used[2005];
int x1[2005], x2[2005];
vector<vector<int>> g(1005);
void dfs(int v) {
if (p1[v]) {
answer = v;
return;
}
used[v] = 1;
for (int i = 0; i < g[v].size(); i++)
if (!used[g[v][i]]) dfs(g[v][i]);
return;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
cin >> n;
for (int i = 0; i <= n; i++) {
g[i].clear();
p1[i] = 0;
p2[i] = 0;
used[i] = 0;
}
for (int i = 0; i < n - 1; i++) {
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
int k1, k2;
cin >> k1;
for (int i = 0; i < k1; i++) {
cin >> x1[i];
p1[x1[i]] = 1;
}
cin >> k2;
for (int i = 0; i < k2; i++) {
cin >> x2[i];
p2[x2[i]] = 1;
}
cout << "B " << x2[0] << endl;
int f;
cin >> f;
dfs(f);
int ans = answer;
cout << "A " << ans << endl;
int f1;
cin >> f1;
if (p2[f1])
cout << "C " << ans << endl;
else
cout << "C -1" << endl;
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long modexpo(long long x, long long p) {
long long res = 1;
x = x % 1000000007;
while (p) {
if (p % 2) res = res * x;
p >>= 1;
x = x * x % 1000000007;
res %= 1000000007;
}
return res;
}
struct compare {
bool operator()(const pair<long long, long long> a,
const pair<long long, long long> b) const {
return a.first < b.first;
}
};
long long n, m;
vector<long long> vert, store;
vector<long long> hori;
map<long long, vector<pair<long long, long long>>> mp;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (long long i = 0, x; i < n; i++) {
cin >> x;
vert.push_back(x);
}
for (long long i = 0, a, b, c; i < m; i++) {
cin >> a >> b >> c;
mp[c].push_back({a, b});
}
if (m == 0) {
cout << 0 << "\n";
exit(0);
}
vert.push_back(1e9);
sort(vert.begin(), vert.end());
store.resize(vert.size());
long long ans = 0;
for (auto &x : mp) {
long long mn = 1e18, mx = -1;
sort(x.second.begin(), x.second.end());
long long flag = 0;
for (long long i = 0; i < x.second.size(); i++) {
if (i == 0) {
if (x.second[0].first > 1) {
hori.push_back(0);
flag = 1;
break;
}
continue;
}
if (x.second[i].first - x.second[i - 1].second >= 1) {
hori.push_back(x.second[i - 1].second);
flag = 1;
break;
}
}
if (flag == 0) {
hori.push_back(x.second.rbegin()->second);
}
}
m = hori.size();
for (long long i : hori) {
if (i == 1e9) {
ans++;
m--;
continue;
}
auto temp = upper_bound(vert.begin(), vert.end(), i);
auto index = temp - vert.begin();
store[index]++;
}
for (long long i = 1; i < vert.size(); i++) {
store[i] += store[i - 1];
}
long long res = 1e18;
for (long long i = 0; i < vert.size(); i++) {
res = min(res, ans + (m - store[i]) + i);
}
cout << res << "\n";
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long n, m, p, N;
int a[200005], b[200005];
int cnt[200005];
int ori[200005];
vector<int> v;
map<int, int> id;
int main() {
cin >> n >> m >> p;
for (int i = 0; i < n; ++i) scanf("%d", a + i);
for (int i = 0; i < m; ++i) scanf("%d", b + i), v.push_back(b[i]);
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
N = v.size();
for (int i = 0; i < v.size(); ++i) id[v[i]] = i;
for (int i = 0; i < n; ++i)
if (id.count(a[i]))
a[i] = id[a[i]];
else
a[i] = -1;
for (int i = 0; i < m; ++i) b[i] = id[b[i]], ori[b[i]]++;
vector<int> ans;
for (int i = 0; i < p; ++i)
if (i + (m - 1) * p < n) {
int tot = 0;
for (int j = 0; j < N; ++j) cnt[j] = ori[j];
for (int j = 0; j < m; ++j) {
if (a[i + (j)*p] == -1)
continue;
else {
if (cnt[a[i + (j)*p]] == 0) tot--;
cnt[a[i + (j)*p]]--;
if (cnt[a[i + (j)*p]] == 0) tot++;
}
}
if (tot == N) ans.push_back(i);
int t = i + m * p, tt = i;
while (t < n) {
if (a[t] != -1) {
if (cnt[a[t]] == 0) tot--;
cnt[a[t]]--;
if (cnt[a[t]] == 0) tot++;
}
if (a[tt] != -1) {
if (cnt[a[tt]] == 0) tot--;
cnt[a[tt]]++;
if (cnt[a[tt]] == 0) tot++;
}
if (tot == N) ans.push_back(tt + p);
t += p, tt += p;
}
}
printf("%d\n", ans.size());
sort(ans.begin(), ans.end());
for (int i = 0; i < ans.size(); ++i) printf("%d ", ans[i] + 1);
}
| 2 |
#include<iostream>
#include<string>
using namespace std;
string rev(string str){
for(int i=0;i<str.length();i++){
if(str[i]=='['){
int j;
int co=1;
for(j=i+1;j<str.length();j++){
if(str[j]=='[')co++;
if(str[j]==']')co--;
if(co==0)break;
}
string n=rev(str.substr(i+1,j-i-1));
str=str.substr(0,i)+n+str.substr(j+1,str.length()-j-1);
}
}
string tem="";
for(int i=0;i<str.length();i++)
tem+=str[str.length()-1-i];
return tem;
}
string pm(string str){
char c=str[str.length()-1];
for(int i=0;i<str.length()-1;i++){
if(str[i]=='+'){
if(c=='Z')
c='A';
else c++;
}
if(str[i]=='-'){
if(c=='A')
c='Z';
else c--;
}
}
string tem="";
tem+=c;
return tem;
}
int main(){
while(1){
string str;
cin>>str;
if(str==".")break;
for(int i=0;i<str.length();i++){
if(str[i]=='+'||str[i]=='-'){
int j;
for(j=i+1;str[j]=='+'||str[j]=='-';j++);
string n;
if(str[j]=='?')n="A";
else n=pm(str.substr(i,j-i+1));
str=str.substr(0,i)+n+str.substr(j+1,str.length()-j-1);
}
if(str[i]=='?')str[i]='A';
// cout<<str<<endl;
}
for(int i=0;i<str.length();i++){
if(str[i]=='['){
int j;
int co=1;
for(j=i+1;j<str.length();j++){
if(str[j]=='[')co++;
if(str[j]==']')co--;
if(co==0)break;
}
string n=rev(str.substr(i+1,j-i-1));
//cout<<n<<endl;
str=str.substr(0,i)+n+str.substr(j+1,str.length()-j-1);
}
}
cout<<str<<endl;
}
} | 0 |
#include<bits/stdc++.h>
using namespace std;
using LL = long long;
int main() {
LL N;
cin >> N;
vector<LL> A(N);
for(auto& i: A) cin >> i;
auto a = max_element(begin(A), end(A)) - begin(A);
if(A[a] <= 0) {
cout << A[a] << endl;
cout << N-1 << endl;
for(auto i=N; a+1<i; --i) cout << i << endl;
for(auto i=1; i<=a; ++i) cout << 1 << endl;
return 0;
}
LL b = 0, c = 0;
for(auto i=0; i<N; ++i) {
if(i&1) b += max(0ll, A[i]);
else c += max(0ll, A[i]);
}
cout << max(b, c) << endl;
vector<LL> ans;
LL i = 0;
if(b > c) ans.emplace_back(i=1);
for(;A[i] < 0; i += 2) {
ans.emplace_back(1);
ans.emplace_back(1);
}
for(;i+2 < N; i += 2) {
if(A[i+2] < 0) {
if(i+3 < N) ans.emplace_back(3);
else ans.emplace_back(3), ans.emplace_back(2);
} else {
ans.emplace_back(2);
}
}
if(i+2 == N) ans.emplace_back(2);
cout << ans.size() << endl;
for(auto i: ans) cout << i << endl;
}
| 0 |
#include "bits/stdc++.h"
#define ALL(g) (g).begin(),(g).end()
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)
#define RREP(i, x, n) for(int i = x; i >= n; i--)
#define rrep(i, n) RREP(i,n,0)
#define pb push_back
using namespace std;
using ll = long long;
using P = pair<int,int>;
using Pl = pair<ll,int>;
const int mod=1e9+7,INF=1<<30;
const double EPS=1e-12,PI=3.1415926535897932384626;
const ll LINF=1LL<<60, lmod = 1e9+7;
const int MAX_N = 100005;
class Edge{
public:
ll cost;int to;
Edge(ll cost,int to)
:cost(cost),to(to){}
};
vector<Edge> graph[MAX_N];
void dijkstra(int start,vector<Edge> graph[],ll dist[],ll pat[]){
priority_queue<Pl,vector<Pl>,greater<Pl>> que;
fill(dist,dist+MAX_N,LINF);
dist[start] = 0LL;
pat[start] = 1LL;
que.push(Pl(0LL,start));
while(!que.empty()){
Pl p=que.top(); que.pop();
int v=int(p.second); //行き先
if(dist[v]<p.first) continue;
rep(i,graph[v].size()){
Edge e=graph[v][i];
if(dist[v]+e.cost<dist[e.to]){
dist[e.to]=dist[v]+e.cost;
// 最小値を更新したら今までのパターンを消す
pat[e.to] = 0LL;
que.push(Pl(dist[e.to],e.to));
}
if(dist[v]+e.cost==dist[e.to]){
// 最小値を見つけたらパターンを足す
(pat[e.to] += pat[v]) %= lmod;
}
}
}
return ;
}
ll distS[MAX_N],distT[MAX_N],patS[MAX_N],patT[MAX_N];
int main(){
int N,M; scanf("%d%d",&N,&M);
int S,T; scanf("%d%d",&S,&T); S--; T--;
rep(i,M){
int l,r; ll dd; scanf("%d%d%lld",&l,&r,&dd);
l--; r--; graph[l].pb(Edge(dd,r)); graph[r].pb(Edge(dd,l));
}
dijkstra(S,graph,distS,patS);
dijkstra(T,graph,distT,patT);
ll ans = patS[T] * patS[T] % lmod;
ll len = distS[T];
rep(i,N){
if(distS[i]+distT[i]==len && distS[i]*2LL==len){
(ans += (lmod - patS[i]*patS[i]%lmod*patT[i]%lmod*patT[i]%lmod)) %= lmod;
}
}
rep(i,N) for(auto e:graph[i]){
int j = e.to; ll c = e.cost;
if(distS[i]+c+distT[j]==len && distS[i]*2LL<len && distT[j]*2LL<len){
(ans += (lmod - patS[i]*patS[i]%lmod*patT[j]%lmod*patT[j]%lmod)) %= lmod;
}
}
cout << ans << endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
int main() {
std::ios::sync_with_stdio(false);
cin.tie(0);
cin.exceptions(cin.failbit);
int T;
cin >> T;
while (T--) {
long long int i;
string s1, s2;
cin >> s1;
cin >> s2;
int a1[26] = {0}, a2[26] = {0};
int l1 = s1.length(), l2 = s2.length();
for (i = 0; i < (l1); ++i) {
a1[s1[i] - 'a']++;
}
for (i = 0; i < (l2); ++i) {
a2[s2[i] - 'a']++;
}
int c = 0;
char cl;
int ck = 0, in = 0, pos, fin;
for (int in = 0; in < 26 && ck == 0; in++) {
pos = -1;
fin = -1;
for (int i1 = 0; i1 < l1; i1++) {
if (s1[i1] - 'A' > in) {
pos = i1;
break;
}
}
for (int i = 0; i < l1; i++) {
if (s1[i] - 'A' == in) fin = i;
}
if (pos == -1 || fin == -1) continue;
if (pos < fin) {
cl = s1[pos];
s1[pos] = s1[fin];
s1[fin] = cl;
ck = 1;
}
}
if (s1 < s2)
cout << s1 << "\n";
else
cout << "---\n";
}
}
| 2 |
/* In the name of Allah */
#include<bits/stdc++.h>
using namespace std;
const int N = 2e3 + 5;
int n, m, q, a[N][N], dp[N][N][3];
int get(int b, int xl, int xr, int yl, int yr) {
return dp[xr][yr][b] - dp[xl][yr][b] - dp[xr][yl][b] + dp[xl][yl][b];
}
void read_input() {
cin >> n >> m >> q;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
char c;
cin >> c;
a[i][j] = c - '0';
}
}
void solve() {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
dp[i][j][0] = (a[i][j] && a[i][j + 1]) + dp[i - 1][j][0] + dp[i][j - 1][0] - dp[i - 1][j - 1][0];
dp[i][j][1] = (a[i][j] && a[i + 1][j]) + dp[i - 1][j][1] + dp[i][j - 1][1] - dp[i - 1][j - 1][1];
dp[i][j][2] = a[i][j] + dp[i - 1][j][2] + dp[i][j - 1][2] - dp[i - 1][j - 1][2];
}
}
void write_output() {
while (q--) {
int xl, xr, yl, yr;
cin >> xl >> yl >> xr >> yr;
cout << get(2, xl - 1, xr, yl - 1, yr) - get(1, xl - 1, xr - 1, yl - 1, yr) - get(0, xl - 1, xr, yl - 1, yr - 1) << endl;
}
}
int main() {
ios:: sync_with_stdio(0), cin.tie(0), cout.tie(0);
read_input(), solve(), write_output();
return 0;
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
#define maxn 100100
int a[maxn];
int n,c,k;
int main()
{
ios::sync_with_stdio(false);
cin>>n>>c>>k;
for(int i=1;i<=n;i++)
cin>>a[i];
sort(a+1,a+1+n);
int ans=0;
for(int i=1;i<=n;i++)
{
int j=i;
while (j+1<=n && j+1<=i+c-1 && a[j+1]<=a[i]+k) j++;
ans++;
i=j;
}
cout<<ans<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long INF = (long long)1e18;
void print_vector(vector<long long> &a) {
for (auto &i : a) {
cout << i << " ";
}
cout << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
string s;
cin >> s;
long long n, l = 1, r = 1;
n = (long long)s.size();
bool check = true;
for (int i = 1; i <= 5; ++i) {
for (int j = 1; j <= 20; ++j) {
if (j * i >= n) {
l = i;
r = j;
check = false;
break;
}
}
if (!check) {
break;
}
}
long long k = n / l, p;
p = l * r - n;
long long cur = 0, asd = p;
cout << l << " " << r << '\n';
for (int j = 0; j < l; ++j) {
for (int i = 0; i < r; ++i) {
if (i == 0 and p > 0) {
cout << '*';
p--;
} else {
cout << s[cur];
cur++;
}
}
cout << '\n';
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
set<long long> s[201];
void insertion(long long num, vector<long long> v) {
for (long long i = 0; i < v.size(); i++) {
if (v[i] != num) {
s[num].insert(v[i]);
}
}
}
void checkandremove(long long num, vector<long long> v) {
set<long long> ss;
for (long long i = 0; i < v.size(); i++) {
ss.insert(v[i]);
}
vector<long long> vv;
for (auto i : s[num]) {
if (ss.find(i) == ss.end()) {
vv.push_back(i);
}
}
for (auto i : vv) {
s[num].erase(i);
}
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
long long n;
cin >> n;
long long flag[201] = {0};
for (long long i = 0; i < n * (n - 1) / 2; i++) {
long long a;
cin >> a;
vector<long long> v;
for (long long j = 0; j < a; j++) {
long long x;
cin >> x;
v.push_back(x);
}
for (auto k : v) {
if (flag[k] == 0) {
insertion(k, v);
flag[k] = 1;
} else {
checkandremove(k, v);
}
}
}
vector<vector<long long>> v;
for (long long i = 1; i <= 200; i++) {
if (flag[i] == 1) {
vector<long long> vf;
vf.push_back(i);
for (auto j : s[i]) {
vf.push_back(j);
flag[j] = 0;
}
v.push_back(vf);
}
}
long long num = v.size();
for (long long i = 0; i < num; i++) {
while (v[i].size() > 1 && num < n) {
long long a = v[i].back();
v[i].pop_back();
vector<long long> temp;
temp.push_back(a);
v.push_back(temp);
num++;
}
}
for (long long i = 0; i < v.size(); i++) {
cout << v[i].size() << " ";
for (auto j : v[i]) {
cout << j << " ";
}
cout << "\n";
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
long long int sum[s.length()];
for (long long int i = 0; i < s.length(); i++) {
if (s[i] == 'Q') {
if (i == 0)
sum[i] = 1;
else
sum[i] = 1 + sum[i - 1];
} else {
if (i == 0)
sum[i] = 0;
else
sum[i] = sum[i - 1];
}
}
long long int pr = 0;
for (long long int i = 0; i < s.length() - 1; i++) {
if (s[i] == 'A') {
pr += sum[i] * (sum[s.length() - 1] - sum[i]);
}
}
cout << pr << endl;
}
| 1 |
#include<iostream>
#include<vector>
#include<algorithm>
#include<iomanip>
using namespace std;
typedef pair<int,int> p;
typedef long long ll;
int main(){
int n, w;
cin >> n >> w;
vector<p> v(n);
for(int i = 0; i < n; i++) cin >> v[i].first >> v[i].second;
sort(v.rbegin(), v.rend(), [](p a, p b){return (ll)a.first*b.second < (ll)b.first*a.second;});
double ans = 0;
for(int i = 0; i < n; i++){
int take = min(w, v[i].second);
ans += (double)take / v[i].second * v[i].first;
w -= take;
}
cout << fixed << setprecision(12) << ans << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int s = 0;
for (int i = 1;; i++) {
s += i * (i + 1) / 2;
if (s + (i + 1) * (i + 2) / 2 > n) {
cout << i;
return 0;
}
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long Min = -1e4;
int main() {
long long t;
cin >> t;
while (t--) {
long long X, Y;
cin >> X >> Y;
if (X >= 4)
cout << "YES" << endl;
else if (X == 2 || X == 3)
cout << (Y <= 3 ? "YES" : "NO") << endl;
else if (X == 1)
cout << (Y <= 1 ? "YES" : "NO") << endl;
else
assert(false);
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
while (b != 0) {
long long c = a % b;
a = b;
b = c;
}
return a;
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
int main() {
int n, a[105], i;
long long ans = 1, cnt;
bool visit[105];
bool indeg[105];
memset(indeg, false, sizeof(indeg));
cin >> n;
for (i = 1; i <= n; i++) {
cin >> a[i];
indeg[a[i]] = true;
}
for (i = 1; i <= n; i++) {
if (!indeg[i]) {
cout << "-1" << endl;
return 0;
}
}
memset(visit, false, sizeof(visit));
for (i = 1; i <= n; i++) {
if (!visit[i]) {
cnt = 0;
int cur = i;
while (!visit[cur]) {
cnt++;
visit[cur] = true;
cur = a[cur];
}
}
if (cnt % 2 == 0) cnt /= 2;
ans = lcm(ans, (long long)cnt);
}
cout << ans << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(NULL);
ios_base::sync_with_stdio(0);
string s;
cin >> s;
int k;
cin >> k;
int ans = 0;
s += '&';
while (k--) {
string c;
cin >> c;
int t1 = 0, t2 = 0;
for (int i = 0; i < s.length(); i++) {
if (c[0] == s[i]) t1++;
if (c[1] == s[i]) t2++;
if (c[0] != s[i] && c[1] != s[i]) {
ans += min(t1, t2);
t1 = t2 = 0;
}
}
}
cout << ans << "\n";
return 0;
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
int a[200050],n,k;
bool check(int mid)
{
int ans=0;
for(int i=1;i<=n;i++)
{
if(a[i]>mid)
ans+=a[i]/mid;
}
if(ans>k) return 0;
return 1;
}
int main()
{
cin>>n>>k;
int l=1,r=-1;
for(int i=1;i<=n;i++)
{
cin>>a[i];
r=max(a[i],r);
}
int ans=0;
while(l<=r)
{
int mid=(l+r)/2;
if(check(mid))
{
r=mid-1;
ans=mid;
}
else l=mid+1;
}
cout<<ans;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int N;
int P[5050];
int B[5050];
int BX[5050];
int PX[5050];
int TN;
int TP[5050] = {1, 0, 3, 2};
int TB[5050];
int ask(int p, int b) {
int ret;
if (TN) {
cout << p << " " << b << " " << (TP[p] ^ TB[b]) << endl;
return TP[p] ^ TB[b];
}
cout << "? " << p << " " << b << endl;
cin >> ret;
return ret;
}
void answer(int ret) {
int i;
cout << "!" << endl;
cout << ret << endl;
for (i = 0; i < (N); i++) {
cout << P[i];
if (i != N - 1) cout << " ";
}
cout << endl;
exit(0);
}
void solve() {
int i, j, k, l, r, x, y;
string s;
if (TN) {
N = TN;
for (i = 0; i < (N); i++) TB[TP[i]] = i;
} else {
cin >> N;
}
for (i = 0; i < (N); i++) BX[i] = ask(0, i);
for (i = 0; i < (N); i++) PX[i] = ask(i, i);
int ret = 0;
for (i = 0; i < (N); i++) {
int cnt[5050] = {};
int P2[5050];
for (j = 0; j < (N); j++) B[j] = BX[j] ^ i, cnt[B[j]]++;
for (j = 0; j < (N); j++)
if (cnt[j] != 1) break;
if (j < N) continue;
for (j = 0; j < (N); j++) P2[B[j]] = j;
if (P2[0] != i) continue;
for (j = 0; j < (N); j++)
if (PX[j] != (P2[j] ^ B[j])) break;
if (j < N) continue;
ret++;
for (j = 0; j < (N); j++) P[j] = P2[j];
}
answer(ret);
}
int main(int argc, char** argv) {
string s;
int i;
if (argc == 1) ios::sync_with_stdio(false), cin.tie(0);
for (i = 0; i < (argc - 1); i++) s += argv[i + 1], s += '\n';
for (i = 0; i < (s.size()); i++) ungetc(s[s.size() - 1 - i], stdin);
cout.tie(0);
solve();
return 0;
}
| 4 |
#include<bits/stdc++.h>
using namespace std;
int lth,vis[30];
char s[30];
int main()
{
scanf("%s",s+1),lth=strlen(s+1);
for(int i=1;i<=lth;i++)vis[s[i]-'a']=i;
if(lth<26)
{
for(int i=0;i<26;i++)if(!vis[i])
return printf("%s%c",s+1,'a'+i),0;
}
else for(int i=26;i;i--)
for(int j=s[i]-'a'+1;j<26;j++)if(vis[j]>i)
{
s[i]='a'+j,s[i+1]=0;
return printf("%s",s+1),0;
}
return puts("-1"),0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int arr[100005];
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) cin >> arr[i];
int mx = 0;
for (int i = 1; i <= n; i++) {
if (arr[i] > mx) {
cout << i << endl;
return 0;
}
mx = max(mx, arr[i] + 1);
}
cout << "-1" << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int cutLen(vector<pair<int, int> > v) {
pair<int, int> p(0, 0);
int len = 0;
for (unsigned i = 0; i < v.size(); ++i) {
if (v[i].first <= p.second) {
p.second = max(v[i].second, p.second);
} else {
len += p.second - p.first;
p = v[i];
}
}
len += p.second - p.first;
return len;
}
int solve(vector<pair<int, int> > v, int len) {
int prev = 0;
for (unsigned i = 0; i < v.size(); ++i) {
if (v[i].first > prev) {
if (len <= v[i].first - prev) break;
len -= v[i].first - prev;
prev = v[i].second;
} else {
prev = max(prev, v[i].second);
}
}
return prev + len;
}
int main() {
int w, h, n;
cin >> w >> h >> n;
map<int, vector<pair<int, int> > > row, col;
while (--n >= 0) {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
if (y1 == y2) {
if (x1 > x2) swap(x1, x2);
row[y1].push_back(make_pair(x1, x2));
} else {
if (y1 > y2) swap(y1, y2);
col[x1].push_back(make_pair(y1, y2));
}
}
int ret = 0;
if ((h - row.size()) % 2 == 0) ret ^= w;
if ((w - col.size()) % 2 == 0) ret ^= h;
for (map<int, vector<pair<int, int> > >::iterator it = row.begin();
it != row.end(); ++it) {
sort(it->second.begin(), it->second.end());
int len = w - cutLen(it->second);
ret ^= len;
}
for (map<int, vector<pair<int, int> > >::iterator it = col.begin();
it != col.end(); ++it) {
sort(it->second.begin(), it->second.end());
int len = h - cutLen(it->second);
ret ^= len;
}
if (!ret) {
cout << "SECOND" << endl;
return 0;
}
cout << "FIRST" << endl;
for (int i = 1;; ++i) {
if (i < h) {
int y = i;
vector<pair<int, int> > v;
if (row.find(y) != row.end()) v = row[y];
int len = w - cutLen(v);
len = len - (len ^ ret);
if (len > 0) {
int x = solve(v, len);
cout << 0 << ' ' << y << ' ' << x << ' ' << y << endl;
return 0;
}
}
if (i < w) {
int x = i;
vector<pair<int, int> > v;
if (col.find(x) != col.end()) v = col[x];
int len = h - cutLen(v);
len = len - (len ^ ret);
if (len > 0) {
int y = solve(v, len);
cout << x << ' ' << 0 << ' ' << x << ' ' << y << endl;
return 0;
}
}
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
char s[1000005];
int num[1000005];
char f[8][5] = {"1869", "1968", "1689", "6198", "1698", "1986", "1896"};
int main() {
int len, i, ant, flag, sum;
int a[20];
while (~scanf("%s", s)) {
ant = 0;
len = strlen(s);
for (i = 0; i < len; i++) {
num[i] = s[i] - 48;
}
memset(a, 0, sizeof(a));
for (i = 0; i < len; i++) {
if (num[i] == 1 && a[1] == 0) {
num[i] = 0;
a[1] = 1;
}
if (num[i] == 6 && a[6] == 0) {
num[i] = 0;
a[6] = 1;
}
if (num[i] == 8 && a[8] == 0) {
num[i] = 0;
a[8] = 1;
}
if (num[i] == 9 && a[9] == 0) {
num[i] = 0;
a[9] = 1;
}
}
sort(num, num + len);
for (i = len - 1; i >= 0; i--) {
ant = ant * 10 + num[i];
ant %= 7;
}
flag = 0;
sum = 0;
for (i = len - 1; i >= 4; i--) {
if (num[i] == 0 && flag == 0) {
sum++;
} else {
printf("%d", num[i]);
flag = 1;
}
}
printf("%s", f[(7 - ant) % 7]);
for (i = 0; i < sum; i++) printf("0");
printf("\n");
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long n, k;
cin >> n >> k;
long arrt[n], arrm[n];
for (int i = 0; i < n; i++) {
int temp;
cin >> temp;
arrt[i] = temp;
}
for (int i = 0; i < n; i++) {
int temp;
cin >> temp;
arrm[i] = temp;
}
int dp = 0;
for (int i = 0; i < k; i++) {
if (arrm[i] == 0) dp += arrt[i];
}
int start = 0;
int end = k - 1;
int max = dp;
int start1;
while (start <= n - k) {
end++;
if (arrm[start] == 0) {
dp = dp - arrt[start];
}
if (arrm[end] == 0) {
dp = dp + arrt[end];
}
if (dp > max) {
max = dp;
}
start++;
}
int ans = 0;
for (int i = 0; i < n; i++) {
if (arrm[i] == 1) {
ans += arrt[i];
}
}
cout << ans + max << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int a, b, c, n;
cin >> a >> b >> c >> n;
int r = max(max(a, b), c);
r = r * 3 - a - b - c;
if (r <= n && (n - r) % 3 == 0)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
const int maxn = 10000+5;
int N,M,dis[maxn],vis[maxn],cost[maxn];
struct edge
{
int v,d,c;
edge(int A,int B,int C):v(A),d(B),c(C) {}
};
vector<edge> G[maxn];
queue<int> que;
void spfa()
{
memset(dis,inf,sizeof(dis));
memset(vis,0,sizeof(vis));
memset(cost,inf,sizeof(cost));
dis[1]=0;
vis[1]=1;
cost[1]=0;
que.push(1);
while(!que.empty())
{
int u = que.front();
que.pop();
for(int i=0; i<G[u].size(); ++i)
{
edge &e = G[u][i];
if(dis[e.v] > dis[u] + e.d)
{
dis[e.v] = dis[u] + e.d;
cost[e.v] = e.c;
if(!vis[e.v])
{
vis[e.v] = 1;
que.push(e.v);
}
}
else if(dis[e.v] == dis[u] + e.d && cost[e.v] > e.c)
{
cost[e.v] = e.c;
if(!vis[e.v])
{
vis[e.v] = 1;
que.push(e.v);
}
}
}
vis[u]=0;
}
int ans = 0;
for(int i=1; i<=N; ++i)
ans+=cost[i];
cout<<ans<<endl;
}
int main()
{
while(cin>>N>>M)
{
if(N == 0 && M == 0)
break;
int u,v,d,c;
for(int i=1; i<=M; ++i)
{
cin>>u>>v>>d>>c;
G[u].push_back(edge(v,d,c));
G[v].push_back(edge(u,d,c));
}
spfa();
/// clear
for(int i=1; i<=N; ++i)
G[i].clear();
}
} | 0 |
//In His Name
#include<bits/stdc++.h>
using namespace std;
vector<pair<int,int> >e;
vector<int>adj[100100];
int n,m,minlvl[100100],lvl[100100];
bool mark[100100];
void dfs(int x,int par){
mark[x]=true;
minlvl[x]=lvl[x];
for(int i=0;i<adj[x].size();i++){
int v=adj[x][i];
if(!mark[v]){
lvl[v]=lvl[x]+1;
dfs(v,x);
minlvl[x]=min(minlvl[x],minlvl[v]);
}
else if(v!=par) minlvl[x]=min(minlvl[x],minlvl[v]);
}
}
int main(){
ios_base::sync_with_stdio(0);
cin>>n>>m;
for(int i=0;i<m;i++){
int x,y;
cin>>x>>y;
if(x>y) swap(x,y);
adj[x].push_back(y);
adj[y].push_back(x);
e.push_back(make_pair(x,y));
}
for(int i=0;i<n;i++)
if(!mark[i]) dfs(i,i);
sort(e.begin(),e.end());
for(int i=0;i<m;i++){
int x=e[i].first,y=e[i].second;
if(lvl[x]<lvl[y]) swap(x,y);
if(minlvl[x]>lvl[y]) cout<<e[i].first<<" "<<e[i].second<<endl;
}
return -0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, s[110000], r[110000], i, j, k, c, m, w = 0, x = 0, y = 0, z = 0, o,
p;
cin >> a >> b;
for (i = 1; i <= a; i++) {
cin >> s[i];
}
for (j = 1; j <= b; j++) {
cin >> r[j];
}
for (k = 1; k <= a; k++) {
if (s[k] % 2 == 0) {
w++;
}
if (s[k] % 2 == 1) {
y++;
}
}
for (c = 1; c <= b; c++) {
if (r[c] % 2 == 1) {
x++;
}
if (r[c] % 2 == 0) {
z++;
}
}
if (w >= x) {
o = x;
} else
o = w;
if (y >= z) {
p = z;
} else
p = y;
cout << o + p;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[]) {
std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
;
int t;
int n;
cin >> t;
while (t--) {
cin >> n;
set<int> used;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0 && !used.count(i)) {
used.insert(i);
n /= i;
break;
}
}
for (int i = 2; i * i <= n; i++) {
if (n % i == 0 && !used.count(i)) {
used.insert(i);
n /= i;
break;
}
}
if (int(used.size()) < 2 || n == 1 || used.count(n))
cout << "NO" << endl;
else {
cout << "YES" << endl;
set<int>::iterator iter;
iter = used.begin();
used.insert(n);
for (; iter != used.end(); iter++) {
cout << *iter << " ";
}
cout << endl;
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using i64 = long long;
struct node {
int ans;
i64 adv;
node(int ans = 0, i64 adv = 0) : ans(ans), adv(adv) {}
node operator+(const node b) const { return node(ans + b.ans, adv + b.adv); }
node operator+() const { return node(ans + (adv > 0)); }
bool operator<(const node b) const {
if (ans == b.ans) {
return adv < b.adv;
}
return ans < b.ans;
}
};
std::vector<node> merge(std::vector<node> &a, std::vector<node> &b) {
std::vector<node> res(a.size() + b.size(), node(-1, 0));
for (int i = 0; i < int(a.size()); i++) {
for (int j = 0; j < int(b.size()); j++) {
res[i + j] = std::max(res[i + j], a[i] + b[j]);
res[i + j + 1] = std::max(res[i + j + 1], a[i] + +b[j]);
}
}
return res;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
int n, m;
std::cin >> n >> m;
std::vector<int> a(n), b(n);
for (int i = 0; i < n; i++) {
std::cin >> a[i];
}
for (int i = 0; i < n; i++) {
std::cin >> b[i];
}
std::vector<std::vector<int>> e(n);
for (int i = 0; i < n - 1; i++) {
int u, v;
std::cin >> u >> v;
u--;
v--;
e[u].push_back(v);
e[v].push_back(u);
}
std::vector<std::vector<node>> dp(n, std::vector<node>(1));
std::function<void(int, int)> dfs = [&](int u, int p) {
dp[u][0] = node(0, -a[u] + b[u]);
for (auto v : e[u]) {
if (v != p) {
dfs(v, u);
dp[u] = merge(dp[u], dp[v]);
}
}
};
dfs(0, -1);
std::cout << (+dp[0][m - 1]).ans << "\n";
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
struct pnt {
int x, y;
pnt(){};
pnt(int x, int y) : x(x), y(y){};
};
struct vect {
int x, y;
vect(){};
vect(int x, int y) : x(x), y(y){};
vect(pnt a, pnt b) : x(b.x - a.x), y(b.y - a.y){};
};
bool left(vect a, vect b) { return (a.x * b.y - a.y * b.x) > 0; }
int main() {
int n;
scanf("%d", &n);
vector<pnt> ps(n + 1);
for (int i = 0; i < n + 1; i++) {
int x, y;
scanf("%d%d", &x, &y);
ps[i] = pnt(x, y);
}
int cnt = 0;
for (int i = 0; i < n - 1; i++) {
cnt += left(vect(ps[i], ps[i + 1]), vect(ps[i + 1], ps[i + 2]));
}
printf("%d", cnt);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
map<long long, long long> cache[10];
long long go(int d, long long n) {
if (cache[d].find(n) != cache[d].end()) return cache[d][n];
long long p = 1;
while (n / p >= 10) p *= 10;
int x = n / p;
long long y = n % p;
long long res = 0;
for (; x >= 0; x--) {
int td = max(d, x);
long long tmp = go(td, y);
res += tmp / 10;
if (tmp % 10)
y = p - tmp % 10;
else if (x) {
res++;
y = p - td;
} else
y = p;
}
return cache[d][n] = res * 10 + p - y;
}
int main() {
for (int i = 1; i < 10; i++)
for (int j = 0; j < 10; j++) {
cache[j][i] = 10 + max(0, j - i);
cache[j][0] = 0;
}
long long n;
cin >> n;
cout << go(0, n) / 10 << endl;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000009;
const int inf = 50000000;
const int maxn = 300010;
const int sqrtn = 1000;
long long add(long long a, long long b) {
long long ret = (a + b) % mod;
return ret;
}
long long fib[maxn], arr[maxn], pref[maxn], fibarr[maxn];
vector<pair<int, int> > updates;
pair<int, int> intersection(int l1, int r1, int l2, int r2) {
pair<int, int> temp;
temp.first = max(l1, l2);
temp.second = min(r1, r2);
return temp;
}
int main() {
int t, n, m, i, j, l, r, rs, re;
long long f1, f2;
fib[1] = fib[2] = 1;
for (i = 3; i < maxn; i++) fib[i] = add(fib[i - 1], fib[i - 2]);
scanf("%d%d", &n, &m);
for (i = 1; i <= n; i++) {
scanf("%lld", &arr[i]);
pref[i] = add(pref[i - 1], arr[i]);
}
while (m--) {
scanf("%d%d%d", &t, &l, &r);
if (t == 1) {
updates.push_back(make_pair(l, r));
fibarr[l] = add(fibarr[l], 1);
fibarr[r + 1] = (fibarr[r + 1] - fib[r - l + 2] + mod) % mod;
fibarr[r + 2] = (fibarr[r + 2] - fib[r - l + 1] + mod) % mod;
} else {
long long ans = (pref[r] - pref[l - 1] + mod) % mod;
for (auto it : updates) {
pair<int, int> temp = intersection(l, r, it.first, it.second);
if (temp.first > temp.second) continue;
rs = temp.first - it.first + 1;
re = temp.second - it.first + 1;
ans = (ans + (fib[re + 2] - fib[rs + 1] + mod) % mod + mod) % mod;
}
printf("%lld\n", ans);
}
if (updates.size() > sqrtn) {
f1 = f2 = 0;
for (i = 1; i <= n; i++) {
fibarr[i] = (fibarr[i] + (f1 + f2) % mod) % mod;
f1 = fibarr[i - 1];
f2 = fibarr[i];
}
for (i = 1; i <= n; i++) {
arr[i] = add(arr[i], fibarr[i]);
fibarr[i] = 0;
pref[i] = add(pref[i - 1], arr[i]);
}
updates.clear();
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
vector<int> adj(19, 0);
long long int dp[1 << 19][19] = {}, ans = 0;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int i, j, n, m;
cin >> n >> m;
while (m--) {
cin >> i >> j;
i--;
j--;
adj[i] |= (1 << j);
adj[j] |= (1 << i);
}
for (i = 0; i < n; i++) {
dp[1 << i][i] = 1;
}
for (int mask = 1; mask < (1 << n); mask++) {
int fmask = mask & (-mask);
int lmask = mask & (mask - 1);
if (!lmask) continue;
if (!(lmask & (lmask - 1))) {
int last = __builtin_ctz(mask & (mask - 1));
dp[mask][last] = (adj[last] & (mask ^ (1 << last))) ? 1 : 0;
continue;
}
for (int lgmask = lmask; lgmask; lgmask &= (lgmask - 1)) {
int last = __builtin_ctz(lgmask & (-lgmask));
for (int slmask = lmask ^ (1 << last); slmask; slmask &= (slmask - 1))
if (adj[last] & (slmask & (-slmask)))
dp[mask][last] +=
dp[mask ^ (1 << last)][__builtin_ctz(slmask & (-slmask))];
if (dp[mask][last] && (adj[last] & fmask)) ans += dp[mask][last];
}
}
ans /= 2;
cout << ans;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
void gn(int &x) {
int sg = 1;
char c;
while (((c = getchar()) < '0' || c > '9') && c != '-')
;
if (c == '-')
sg = -1, x = 0;
else
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9') x = x * 10 + c - '0';
x *= sg;
}
const int mo = 1000000007;
int n;
struct para {
int x, l;
} a[111];
int cmp(const para &a, const para &b) { return a.x < b.x; }
int ll[111], mm[111], rr[111];
int x[333];
int tot = 0;
int f[111][111][333];
int de(int x, int y) {
if (x == 1000000000 || y == 1000000000) return 1000000000;
return min(x, y);
}
inline int upd(int l, int r, int j, int lx, int rx) {
if (lx <= j && j <= rx) {
if (l == r) return lx;
int mi = 1000000000;
for (int t = r - 1; t >= l; t--)
mi = min(mi, de(lx, f[l][t][mm[t] > lx ? mm[t] : lx]));
return mi;
} else {
if (l == r) return 1000000000;
int mi = 1000000000;
for (int t = r - 1; t >= l; t--) mi = min(mi, de(lx, f[l][t][j]));
return mi;
}
}
int g[111][333];
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d%d", &a[i].x, &a[i].l);
x[++tot] = a[i].x - a[i].l;
x[++tot] = a[i].x;
x[++tot] = a[i].x + a[i].l;
}
sort(a + 1, a + 1 + n, cmp);
sort(x + 1, x + 1 + tot);
tot = unique(x + 1, x + 1 + tot) - x - 1;
for (int i = 1; i <= n; i++) {
ll[i] = lower_bound(x + 1, x + 1 + tot, a[i].x - a[i].l) - x;
mm[i] = lower_bound(x + 1, x + 1 + tot, a[i].x) - x;
rr[i] = lower_bound(x + 1, x + 1 + tot, a[i].x + a[i].l) - x;
}
for (int l = 1; l <= n; l++)
for (int r = l; r <= n; r++)
for (int j = 1; j <= tot; j++) {
f[l][r][j] = 1000000000;
f[l][r][j] = min(f[l][r][j], upd(l, r, j, mm[r], rr[r]));
f[l][r][j] = min(f[l][r][j], upd(l, r, j, ll[r], mm[r]));
}
for (int l = 1; l <= n; l++)
for (int r = l + 1; r <= n; r++) {
if (rr[l] > r && ll[r] < l) {
int le = ll[r], ri = rr[l];
for (int j = le; j <= ri; j++) f[l][r][j] = min(f[l][r][j], le);
}
}
for (int r = 1; r <= n; r++)
for (int j = 1; j <= tot; j++) {
if (f[1][r][j] == 1000000000)
g[r][j] = -1000000000;
else
g[r][j] = x[j] - x[f[1][r][j]];
}
for (int r = 2; r <= n; r++)
for (int j = 1; j <= tot; j++) {
for (int l = 2; l <= r; l++)
if (f[l][r][j] < 1000000000) {
for (int k = 1; k <= f[l][r][j]; k++) {
int nu = x[j] - x[f[l][r][j]] + g[l - 1][k];
g[r][j] = max(g[r][j], nu);
}
}
if (g[r][j] < 0) g[r][j] = -1000000000;
}
int ma = 0;
for (int i = 1; i <= tot; i++) ma = max(ma, g[n][i]);
printf("%d\n", ma);
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int h[10000];
map<int, int> b;
map<int, int>::iterator it;
int main() {
int n, k;
cin >> n >> k;
for (int i = 1; i <= n; ++i) cin >> h[i], ++b[h[i] - i * k];
int maxi = 0, bb = 0;
for (it = b.begin(); it != b.end(); ++it)
if (k + it->first > 0 && maxi < it->second)
maxi = it->second, bb = it->first;
cout << n - maxi << endl;
for (int i = 1; i <= n; ++i)
if (h[i] != k * i + bb) {
if (h[i] > k * i + bb)
cout << "- " << i << ' ' << h[i] - k * i - bb << endl;
else
cout << "+ " << i << ' ' << k * i + bb - h[i] << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<string> s(n);
for (int i = 0; i < n; i++) cin >> s[i];
vector<int> cntn(n), cntm(m);
int sum = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (s[i][j] == '*') {
cntn[i]++;
cntm[j]++;
sum++;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int tmp = cntn[i] + cntm[j];
if (s[i][j] == '*') tmp--;
if (tmp == sum) {
cout << "YES" << endl;
cout << i + 1 << " " << j + 1 << endl;
return 0;
}
}
}
cout << "NO" << endl;
return 0;
}
| 2 |
#include<cstdio>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int N=100005;
int n,K,i,p[N],q[N],invp[N],invq[N],a[N],b[N],c[N],x[10][N],ans[N],m;
void Mult(int a[N],int b[N],int rtn[N])
{
for(int i=1;i<=n;++i)
rtn[i]=a[b[i]];
}
void Inv(int a[N],int rtn[N])
{
for(int i=1;i<=n;++i)
rtn[a[i]]=i;
}
int main()
{
scanf("%d%d",&n,&K);
for(i=1;i<=n;++i)
scanf("%d",p+i);
for(i=1;i<=n;++i)
scanf("%d",q+i);
Inv(p,invp);
Inv(q,invq);
for(i=1;i<=n;++i)
x[1][i]=p[i],x[2][i]=q[i];
Mult(q,invp,x[3]);
Mult(x[3],invq,x[4]);
Mult(x[4],p,a);
Mult(a,invq,x[5]);
Mult(a,p,b);
Mult(b,invq,x[0]);
Inv(a,b);
m=(K-1)/6;
for(i=1;i<=n;++i)
ans[i]=i;
while(m)
{
if(m&1)
{
Mult(ans,a,c);
for(i=1;i<=n;++i)
ans[i]=c[i];
}
Mult(a,a,c);
for(i=1;i<=n;++i)
a[i]=c[i];
m>>=1;
}
Mult(ans,x[K%6],c);
for(i=1;i<=n;++i)
ans[i]=c[i];
m=(K-1)/6;
while(m)
{
if(m&1)
{
Mult(ans,b,c);
for(i=1;i<=n;++i)
ans[i]=c[i];
}
Mult(b,b,c);
for(i=1;i<=n;++i)
b[i]=c[i];
m>>=1;
}
for(i=1;i<=n;++i)
printf("%d ", ans[i]);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> magulang;
vector<vector<int>> anak;
vector<int> preorder;
void getPreorder(int v) {
preorder.push_back(v);
for (int nxt : anak[v]) getPreorder(nxt);
}
int main() {
scanf("%d", &n);
magulang = vector<int>(n, -1);
anak = vector<vector<int>>(n);
for (int i = 1; i < n; ++i) {
scanf("%d", &magulang[i]);
anak[magulang[i]].push_back(i);
}
vector<int> lalim(n);
for (int i = 1; i < n; ++i) lalim[i] = lalim[magulang[i]] + 1;
int protectRoot = 0;
for (int i = 1; i < n; ++i)
if (lalim[i] > lalim[protectRoot]) protectRoot = i;
vector<bool> tanggol(n);
vector<int> nextLabel(n, -1), ans;
for (int cv = protectRoot; cv != -1; cv = magulang[cv]) {
tanggol[cv] = true;
if (magulang[cv] != -1) nextLabel[magulang[cv]] = cv;
}
getPreorder(0);
for (int v : preorder) {
if (tanggol[v]) continue;
int sumusunod = nextLabel[magulang[v]];
nextLabel[magulang[v]] = v;
ans.push_back(sumusunod);
nextLabel[v] = sumusunod;
}
for (int cx = 0; cx != -1; cx = nextLabel[cx]) printf("%d ", cx);
printf("\n");
printf("%lu\n", ans.size());
reverse(begin(ans), end(ans));
for (int x : ans) printf("%d ", x);
printf("\n");
return 0;
}
| 6 |
#include <bits/stdc++.h>
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
using namespace std;
void file() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main() {
file();
int x, y, z;
cin >> x >> y >> z;
if (min(x, y) + z < max(x, y) && max(x, y) == y)
cout << "-" << '\n';
else if (min(x, y) + z < max(x, y) && max(x, y) == x)
cout << "+" << '\n';
else if (min(x, y) + z >= max(x, y) && z)
cout << "?" << '\n';
else
cout << "0" << '\n';
}
| 1 |
#include <bits/stdc++.h>
#pragma warning(disable : 4996)
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("-ffloat-store")
using namespace std;
clock_t time_p = clock();
void aryanc403() {
time_p = clock() - time_p;
cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n";
}
class CMP {
public:
bool operator()(long long int a, long long int b) { return !(a <= b); }
};
void add(map<long long int, long long int> &m, long long int x) {
map<long long int, long long int>::iterator jt;
jt = m.find(x);
if (jt == m.end())
m.insert(make_pair(x, 1));
else
jt->second++;
}
void del(map<long long int, long long int> &m, long long int x) {
map<long long int, long long int>::iterator jt;
jt = m.find(x);
if (jt->second == 1)
m.erase(jt);
else
jt->second--;
}
bool cmp(const pair<long long int, long long int> &a,
const pair<long long int, long long int> &b) {
return a.first < b.first || (a.first == b.first && a.second < b.second);
}
const long long int INF = 0xFFFFFFFFFFFFFFFL;
const long long int mod = 1000000007L;
long long int T, n, i, m, j, k, in, cnt, l, r;
vector<long long int> a;
vector<long long int>::iterator it;
string s;
void solve(long long int n, long long int mul) {
if (n < 4) cout << mul << " ";
if (n == 1) return;
if (n == 2) {
cout << 2 * mul << " ";
return;
}
if (n == 3) {
cout << mul << " " << 3 * mul << " ";
return;
}
long long int i = 0;
for (i = 1; i <= n; i += 2) cout << mul << " ";
solve(n / 2, 2 * mul);
}
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
{
cin >> n;
solve(n, 1);
}
aryanc403();
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int sizz = 1e6 + 4;
const long long int MIN = numeric_limits<long long int>::min();
bool compare(const pair<int, pair<int, int> > &a,
const pair<int, pair<int, int> > &b) {
return a.first < b.first;
}
int main() {
long long int n, m;
cin >> n >> m;
vector<pair<int, pair<int, int> > > v;
for (int i = 0; i < m; i++) {
long long int l, r;
cin >> l >> r;
v.push_back({(r - l + 1), {l, r}});
}
int flag[n + 1];
for (int i = 1; i <= n; i++) {
if (i % 2 == 0) {
flag[i] = 0;
} else {
flag[i] = 1;
}
}
for (int i = 1; i <= n; i++) {
cout << flag[i];
}
cout << "\n";
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
int max = 1e5 + 5;
while (t--) {
int n;
cin >> n;
vector<int> count(max, 0);
vector<int> a(n);
int maxm = 0;
int ans = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
count[a[i]]++;
if (count[a[i]] > maxm) {
ans = 1;
maxm = count[a[i]];
} else if (count[a[i]] == maxm)
ans++;
}
cout << ((n - ans * maxm) / (maxm - 1)) + (ans - 1) << "\n";
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int a[10005];
int n, z;
int t, sum;
int ans;
int main() {
cin >> t;
while (t--) {
z = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a, a + n);
int y = 10000;
for (int i = 1; i < n; i++) {
z = a[i] - a[i - 1];
y = min(z, y);
}
cout << y << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int t;
cin >> t;
while (t--) {
long long int n;
cin >> n;
long long int a, b;
long long int c = 0, d = 0;
bool f = true;
for (long long int i = 0; i < n; i++) {
cin >> a >> b;
if (a < c || b < d || a - c < b - d) f = false;
c = a, d = b;
}
if (f)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
int main(){
string x,y;
cin>>x>>y;
y.pop_back();
if(x==y){
cout<<"Yes"<<endl;
}
else{
cout<<"No"<<endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int a[100010][4], n, m, k;
int main() {
long long res;
scanf("%d %d %d", &n, &m, &k);
for (int i = 1; i <= m; i++) scanf("%d %d %d", &a[i][1], &a[i][2], &a[i][3]);
int cell;
res = 0;
while (k) {
k--;
scanf("%d", &cell);
for (int i = 1; i <= m; i++)
if (a[i][1] <= cell && cell <= a[i][2]) res += cell - a[i][1] + a[i][3];
}
printf("%I64d", res);
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0;
bool f = 0;
char ch = getchar();
for (; !isdigit(ch); ch = getchar()) f = (ch == '-');
for (; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ 48);
return f ? -x : x;
}
const int maxn = 4e5 + 7;
int n, m, c[maxn], fa[maxn];
vector<pair<int, int> > v[maxn];
int head[maxn], to[maxn * 2], nx[maxn * 2], ec = 1;
inline void add(int a, int b) {
nx[++ec] = head[a];
head[a] = ec;
to[ec] = b;
}
long long ans[maxn], cur;
struct node {
int fa, c[2], sz;
int isz, ro;
long long ich2;
} a[maxn];
inline bool get(int x) { return a[a[x].fa].c[1] == x; }
inline bool isrt(int x) { return a[a[x].fa].c[0] ^ x && a[a[x].fa].c[1] ^ x; }
inline void rot(int x) {
swap(a[x].c[0], a[x].c[1]);
a[x].ro ^= 1;
}
inline void pushup(int x) {
a[x].sz = 1 + a[x].isz + a[a[x].c[0]].sz + a[a[x].c[1]].sz;
}
inline void pushdn(int x) {
if (a[x].ro) rot(a[x].c[0]), rot(a[x].c[1]), a[x].ro = 0;
}
inline void rotate(int x) {
int f = a[x].fa, ff = a[f].fa, w = get(x), ch = a[x].c[!w];
if (!isrt(f)) a[ff].c[get(f)] = x;
a[x].c[!w] = f;
a[f].c[w] = ch;
if (ch) a[ch].fa = f;
a[f].fa = x;
a[x].fa = ff;
pushup(f);
}
int sta[maxn], top;
inline void splay(int x) {
for (int y = sta[top = 1] = x; !isrt(y); sta[++top] = y = a[y].fa)
;
while (top) pushdn(sta[top--]);
for (; !isrt(x); rotate(x))
if (!isrt(a[x].fa)) rotate(get(x) == get(a[x].fa) ? a[x].fa : x);
pushup(x);
}
inline void access(int x) {
for (int y = 0; x; y = x, x = a[x].fa) {
splay(x);
a[x].isz += a[a[x].c[1]].sz;
a[x].ich2 += (1ll * (a[a[x].c[1]].sz) * (a[a[x].c[1]].sz));
a[x].c[1] = y;
a[x].isz -= a[a[x].c[1]].sz;
a[x].ich2 -= (1ll * (a[a[x].c[1]].sz) * (a[a[x].c[1]].sz));
pushup(x);
}
}
inline void makeroot(int x) {
access(x);
splay(x);
rot(x);
}
inline int findroot(int x) {
access(x);
splay(x);
pushdn(x);
while (a[x].c[0]) x = a[x].c[0], pushdn(x);
splay(x);
return x;
}
inline void link(int x) {
int y = fa[x];
access(x);
splay(x);
cur -= a[x].ich2 + (1ll * (a[a[x].c[1]].sz) * (a[a[x].c[1]].sz));
int z = findroot(y);
access(y);
splay(z);
cur -= (1ll * (a[a[z].c[1]].sz) * (a[a[z].c[1]].sz));
splay(y);
a[x].fa = y;
a[y].isz += a[x].sz;
a[y].ich2 += (1ll * (a[x].sz) * (a[x].sz));
pushup(y);
access(x);
splay(z);
cur += (1ll * (a[a[z].c[1]].sz) * (a[a[z].c[1]].sz));
}
inline void cut(int x) {
int y = fa[x];
access(x);
cur += a[x].ich2;
int z = findroot(x);
access(x);
splay(z);
cur -= (1ll * (a[a[z].c[1]].sz) * (a[a[z].c[1]].sz));
splay(x);
a[a[x].c[0]].fa = 0;
a[x].c[0] = 0;
pushup(x);
access(y);
splay(z);
cur += (1ll * (a[a[z].c[1]].sz) * (a[a[z].c[1]].sz));
}
void dfs(int x, int ff) {
fa[x] = ff;
for (int i = head[x]; i; i = nx[i]) {
int ver = to[i];
if (ver == ff) continue;
dfs(ver, x);
}
a[x].fa = ff;
a[ff].isz += a[x].sz;
a[ff].ich2 += (1ll * (a[x].sz) * (a[x].sz));
pushup(ff);
}
int main() {
n = read();
m = read();
for (int i = 1; i <= n; ++i) v[c[i] = read()].push_back(make_pair(0, i));
for (int i = 1; i < n; ++i) {
int u = read(), v = read();
add(u, v);
add(v, u);
}
for (int i = 1; i <= n + 1; ++i) a[i].sz = 1;
dfs(1, n + 1);
for (int i = 1; i <= m; ++i) {
int x = read(), y = read();
if (c[x] == y) continue;
v[c[x]].push_back(make_pair(i, -x));
v[c[x] = y].push_back(make_pair(i, x));
}
cur = (1ll * (n) * (n));
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < v[i].size(); ++j) {
int x = v[i][j].second;
if (x > 0)
cut(x);
else
link(-x);
ans[v[i][j].first] += (1ll * (n) * (n)) - cur;
ans[j + 1 == v[i].size() ? m + 1 : v[i][j + 1].first] -=
(1ll * (n) * (n)) - cur;
}
for (int j = (int)v[i].size() - 1; ~j; --j) {
int x = v[i][j].second;
if (x > 0)
link(x);
else
cut(-x);
}
}
for (int i = 1; i <= m; ++i) ans[i] += ans[i - 1];
for (int i = 0; i <= m; ++i) printf("%lld\n", ans[i]);
return 0;
}
| 5 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
using namespace std;
template <class T>
inline T sqr(T x) {
return x * x;
}
template <class T>
inline T parse(const string &s) {
T x;
stringstream ss(s);
ss >> x;
return x;
}
const double EPS = 1e-12;
const int INF = 1000 * 1000 * 1000;
const long long LINF = INF * 1ll * INF;
const double DINF = 1e200;
const double PI = 3.1415926535897932384626433832795l;
int gcd(int a, int b) { return a ? gcd(b % a, a) : b; }
long long gcd(long long a, long long b) { return a ? gcd(b % a, a) : b; }
long long gcdex(long long a, long long b, long long &x, long long &y) {
if (!a) {
x = 0, y = 1;
return b;
}
long long k = b / a;
long long g = gcdex(b - k * a, a, y, x);
x -= k * y;
return g;
}
long long inv(long long a, long long m) {
assert(m > 1);
long long x, y, g;
g = gcdex(a, m, x, y);
return (x % (m / g) + m / g) % m / g;
}
long long crt(long long a1, long long m1, long long a2, long long m2) {
long long a = (a2 - a1 % m2 + m2) % m2;
long long x, y, g;
g = gcdex(m1, m2, x, y);
if (a % g) return -1;
long long m = m1 / g * m2;
assert(x + m2 >= 0);
x = a / g * (x + m2) % m2;
long long r = (a1 + x * m1) % m;
assert(r % m1 == a1 && r % m2 == a2);
return r;
}
long long powmod(long long a, long long p, long long m) {
assert(p >= 0);
long long r = 1;
while (p) {
if (p & 1) r = r * a % m;
p >>= 1;
a = a * a % m;
}
return r;
}
bool isprime(long long a) {
if (a <= 1) return false;
for (long long i = 2; i * i <= a; ++i) {
if (a % i == 0) return false;
}
return true;
}
long long sqrtup(long long a) {
if (!a) return 0;
long long x = max(0ll, (long long)sqrt((double)a));
while (x * x >= a) --x;
while ((x + 1) * (x + 1) < a) ++x;
return x + 1;
}
long long isqrt(long long a) {
if (a <= 0) {
assert(!a);
return 0;
}
long long x = (long long)sqrt((double)a);
while (sqr(x + 1) <= a) ++x;
while (x * x > a) --x;
return x;
}
long long sgn(long long x) { return x < 0 ? -1 : x > 0 ? 1 : 0; }
double randf() {
double m = RAND_MAX + 1.;
return (((rand() + .5) / m + rand()) / m + rand()) / m;
}
template <class T>
ostream &operator<<(ostream &s, const vector<T> &v);
template <class A, class B>
ostream &operator<<(ostream &s, const pair<A, B> &p);
template <class K, class V>
ostream &operator<<(ostream &s, const map<K, V> &m);
template <class T>
ostream &operator<<(ostream &s, const set<T> &m);
template <class T, size_t N>
ostream &operator<<(ostream &s, const array<T, N> &a);
template <class... T>
ostream &operator<<(ostream &s, const tuple<T...> &t);
template <class T>
ostream &operator<<(ostream &s, const vector<T> &v) {
s << '[';
for (int i = 0; i < (((int)(v).size())); ++i) {
if (i) s << ',';
s << v[i];
}
s << ']';
return s;
}
template <class A, class B>
ostream &operator<<(ostream &s, const pair<A, B> &p) {
s << "(" << p.first << "," << p.second << ")";
return s;
}
template <class K, class V>
ostream &operator<<(ostream &s, const map<K, V> &m) {
s << "{";
bool f = false;
for (const auto &it : m) {
if (f) s << ",";
f = true;
s << it.first << ": " << it.second;
}
s << "}";
return s;
}
template <class T>
ostream &operator<<(ostream &s, const set<T> &m) {
s << "{";
bool f = false;
for (const auto &it : m) {
if (f) s << ",";
f = true;
s << it;
}
s << "}";
return s;
}
template <class T>
ostream &operator<<(ostream &s, const multiset<T> &m) {
s << "{";
bool f = false;
for (const auto &it : m) {
if (f) s << ",";
f = true;
s << it;
}
s << "}";
return s;
}
template <class T, class V, class C>
ostream &operator<<(ostream &s, const priority_queue<T, V, C> &q) {
auto a = q;
s << "{";
bool f = false;
while (!a.empty()) {
if (f) s << ",";
f = true;
s << a.top();
a.pop();
}
return s << "}";
}
template <class T, size_t N>
ostream &operator<<(ostream &s, const array<T, N> &a) {
s << '[';
for (int i = 0; i < (((int)(a).size())); ++i) {
if (i) s << ',';
s << a[i];
}
s << ']';
return s;
}
template <class T>
ostream &operator<<(ostream &s, const deque<T> &a) {
s << '[';
for (int i = 0; i < (((int)(a).size())); ++i) {
if (i) s << ',';
s << a[i];
}
s << ']';
return s;
}
template <size_t n, class... T>
struct put1 {
static ostream &put(ostream &s, const tuple<T...> &t) {
s << get<sizeof...(T) - n>(t);
if (n > 1) s << ',';
return put1<n - 1, T...>::put(s, t);
}
};
template <class... T>
struct put1<0, T...> {
static ostream &put(ostream &s, const tuple<T...> &t) { return s; }
};
template <class... T>
ostream &operator<<(ostream &s, const tuple<T...> &t) {
s << "(";
put1<sizeof...(T), T...>::put(s, t);
s << ")";
return s;
}
ostream &put3(ostream &s, const char *, bool) { return s; }
template <class U, class... T>
ostream &put3(ostream &s, const char *f, bool fs, U &&u, T &&...t) {
while (*f == ' ') ++f;
if (!fs) s << ", ";
auto nf = f;
int d = 0;
while (*nf && (*nf != ',' || d)) {
if (*nf == '(')
++d;
else if (*nf == ')')
--d;
++nf;
}
auto nf2 = nf;
while (nf2 > f && *(nf2 - 1) == ' ') --nf;
fs = *f == '"';
if (!fs) {
s.write(f, nf2 - f);
s << "=";
};
s << u;
if (fs) s << ' ';
if (*nf) ++nf;
return put3(s, nf, fs, forward<T>(t)...);
}
struct segtree {
vector<int> T;
segtree(int n) : T(n * 4) {}
void add(int l, int r, int v, int a, int b, int k) {
if (l >= b || r <= a) return;
if (l <= a && r >= b) {
T[k] += v;
return;
}
int c = (a + b) / 2;
add(l, r, v, a, c, k * 2 + 1);
add(l, r, v, c, b, k * 2 + 2);
}
int get(int p, int a, int b, int k) {
if (b == a + 1) return T[k];
int c = (a + b) / 2;
if (p < c)
return T[k] + get(p, a, c, k * 2 + 1);
else
return T[k] + get(p, c, b, k * 2 + 2);
}
};
int main(int argc, char **argv) {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.precision(20);
srand(35964);
int tc;
cin >> tc;
for (int qq = 0; qq < (tc); ++qq) {
string A;
cin >> A;
map<int, int> B;
array<int, 26> C{};
array<set<int>, 26> D;
for (int i = 0; i < (((int)(A).size()) - 1); ++i) {
if (A[i] == A[i + 1]) {
if (((int)(B).size())) {
pair<int, int> t = *B.rbegin();
if (t.second != A[i] - 'a') {
D.at(t.second).insert(t.first);
D.at(A[i] - 'a').insert(t.first);
}
}
B[i + 1] = A[i] - 'a';
++C.at(A[i] - 'a');
}
}
do {
} while (false);
segtree T(((int)(A).size()));
vector<pair<int, int> > R;
auto turn = [&](int a, int b) {
int da = T.get(a, 0, ((int)(A).size()), 0);
int db = T.get(b - 1, 0, ((int)(A).size()), 0);
R.push_back(make_pair(a - da, b - db));
do {
} while (false);
T.add(a, ((int)(A).size()), b - db - a + da, 0, ((int)(A).size()), 0);
};
while (true) {
pair<int, int> mx(-1, -1);
for (int i = 0; i < (26); ++i) mx = max(mx, make_pair(C[i], i));
if (!mx.first || !((int)(D.at(mx.second)).size())) break;
int a = *D[mx.second].begin();
auto it1 = B.find(a);
assert(it1 != B.end());
auto it2 = it1;
++it2;
assert(it2 != B.end());
int b = it2->first;
assert(it1->second != it2->second);
assert(b);
turn(a, b);
D[it1->second].erase(it1->first);
D[it2->second].erase(it1->first);
--C[it1->second];
--C[it2->second];
auto it3 = it2;
++it3;
if (it3 != B.end() && it3->second != it2->second) {
D[it2->second].erase(it2->first);
D[it3->second].erase(it2->first);
}
auto it0 = it1;
if (it1 != B.begin()) {
--it0;
if (it0->second != it1->second) {
D[it0->second].erase(it0->first);
D[it1->second].erase(it0->first);
}
}
if (it1 != B.begin() && it3 != B.end() && it0->second != it3->second) {
D[it0->second].insert(it0->first);
D[it3->second].insert(it0->first);
}
B.erase(it1);
B.erase(it2);
}
do {
} while (false);
int p = 0;
while (((int)(B).size())) {
turn(p, B.begin()->first);
p = B.begin()->first;
B.erase(B.begin());
}
turn(p, ((int)(A).size()));
cout << ((int)(R).size()) << '\n';
for (int i = 0; i < (((int)(R).size())); ++i)
cout << R[i].first + 1 << ' ' << R[i].second << '\n';
}
return 0;
}
| 4 |
#include <iostream>
using namespace std;
int main()
{
int a, b, n;
while (cin >> a >> b >> n){
int sum = 0;
for (int i = 0; i < n; i++){
a *= 10;
sum += (a / b) % 10;
a %= b;
}
cout << sum << endl;
}
return (0);
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int b[100010] = {0}, c[11][100010] = {0};
int main() {
int n, k, w;
string a;
cin >> n >> k >> w;
cin >> a;
for (int i = 0; i < n; i++) {
if (a[i] == '1') b[i + 1] = 1;
}
for (int i = 0; i < k; i++) {
for (int j = i + 1; j <= n; j++) {
c[i][j] = c[i][j - 1];
if ((j - i) % k == 0 && b[j] == 0)
c[i][j]++;
else if ((j - i) % k && b[j] == 1)
c[i][j]++;
}
}
while (w--) {
int a, b, s;
cin >> a >> b;
s = (a - 1) % k;
cout << c[s][b] - c[s][a - 1] << endl;
}
return 0;
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
using LL=long long;
LL get(int level,LL h){
if(level==0)return 1;
LL s=pow(LL(2),level+2)-3;
if(h==1)return 0;
if(h==s)return pow(LL(2),level+1)-1;
if(h<=(s-1)/2)return get(level-1,h-1);
if(h==(s+1)/2)return get(level-1,(s-1)/2-1)+1;
return get(level-1,(s-1)/2-1)+1+get(level-1,h-(s+1)/2);
}
int main(){
int N;LL X;cin>>N>>X;
cout<<get(N,X)<<endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
bool dd[5000005];
long long n, a[5000005], b[5000005], avail[5000005], pos[5000005];
vector<long long> v, ans, pri, vec;
multiset<long long> s;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
dd[1] = true;
for (long long i = 2; i < 5000000; i++) {
if (!dd[i]) {
v.push_back(i);
for (long long j = i * i; j < 5000000; j += i) dd[j] = true;
}
}
for (long long i = 0; i < 200001; i++) a[i + 1] = v[i], pos[v[i]] = i + 1;
cin >> n;
for (long long i = 1; i <= 2 * n; i++) {
cin >> b[i];
if (!dd[b[i]])
pri.push_back(b[i]);
else
vec.push_back(b[i]);
s.insert(b[i]);
avail[b[i]]++;
}
sort(vec.begin(), vec.end());
reverse(vec.begin(), vec.end());
for (long long i = 0; i < (int)vec.size(); i++) {
if (avail[vec[i]] > 0) {
for (long long j = 2; j <= (long long)sqrt(vec[i]); j++) {
if (vec[i] % j == 0 && avail[vec[i] / j] > 0) {
ans.push_back(vec[i]);
avail[vec[i]]--;
avail[vec[i] / j]--;
if (!dd[vec[i] / j]) s.erase(s.find(vec[i] / j));
break;
}
}
}
}
sort(pri.begin(), pri.end());
for (int i = (int)pri.size() - 1; i >= 0; i--) {
if (avail[pri[i]] > 0 && avail[pos[pri[i]]] > 0) {
ans.push_back(pos[pri[i]]);
s.erase(s.find(pos[pri[i]]));
s.erase(s.find(pri[i]));
avail[pos[pri[i]]]--;
avail[pri[i]]--;
}
}
for (long long i = 0; i < ans.size(); i++) cout << ans[i] << " ";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e6 + 100;
int Z[MAX], F[MAX], SZ;
string Str;
void ZArray() {
int L, R, K;
L = 0, R = 0, K = 0;
for (int i = 1; i < SZ; i++) {
if (i > R) {
L = R = i;
while (Str[R] == Str[R - L]) R++;
Z[i] = R - L;
R--;
} else {
K = i - L;
if (Z[K] < R - i + 1)
Z[i] = Z[K];
else {
L = i;
while (Str[R] == Str[R - L]) R++;
Z[i] = R - L;
R--;
}
}
}
}
int main() {
cin >> Str;
SZ = Str.size();
ZArray();
int pos_min = 0;
for (int i = 0; i < SZ; i++) {
if (Z[i] > 0) F[Z[i]]++, pos_min = max(pos_min, Z[i]);
}
int sz = 0;
string tt, ans;
for (int i = SZ - 1; i >= 1; i--) {
if (Z[i] + i == SZ && ((F[Z[i]] > 1) || (pos_min > Z[i]))) {
if (Z[i] > sz) {
sz = Z[i];
}
}
}
if (!sz)
cout << "Just a legend";
else
cout << Str.substr(0, sz);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
template <typename T, size_t N>
int SIZE(const T (&t)[N]) {
return N;
}
template <typename T>
int SIZE(const T &t) {
return t.size();
}
string to_string(const string s, int x1 = 0, int x2 = 1e9) {
return '"' + ((x1 < s.size()) ? s.substr(x1, x2 - x1 + 1) : "") + '"';
}
string to_string(const char *s) { return to_string((string)s); }
string to_string(const bool b) { return (b ? "true" : "false"); }
string to_string(const char c) { return string({c}); }
template <size_t N>
string to_string(const bitset<N> &b, int x1 = 0, int x2 = 1e9) {
string t = "";
for (int __iii__ = min(x1, SIZE(b)), __jjj__ = min(x2, SIZE(b) - 1);
__iii__ <= __jjj__; ++__iii__) {
t += b[__iii__] + '0';
}
return '"' + t + '"';
}
template <typename A, typename... C>
string to_string(const A(&v), int x1 = 0, int x2 = 1e9, C... coords);
int l_v_l_v_l = 0, t_a_b_s = 0;
template <typename A, typename B>
string to_string(const pair<A, B> &p) {
l_v_l_v_l++;
string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
l_v_l_v_l--;
return res;
}
template <typename A, typename... C>
string to_string(const A(&v), int x1, int x2, C... coords) {
int rnk = rank<A>::value;
string tab(t_a_b_s, ' ');
string res = "";
bool first = true;
if (l_v_l_v_l == 0) res += '\n';
res += tab + "[";
x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v));
auto l = begin(v);
advance(l, x1);
auto r = l;
advance(r, (x2 - x1) + (x2 < SIZE(v)));
for (auto e = l; e != r; e = next(e)) {
if (!first) {
res += ", ";
}
first = false;
l_v_l_v_l++;
if (e != l) {
if (rnk > 1) {
res += '\n';
t_a_b_s = l_v_l_v_l;
};
} else {
t_a_b_s = 0;
}
res += to_string(*e, coords...);
l_v_l_v_l--;
}
res += "]";
if (l_v_l_v_l == 0) res += '\n';
return res;
}
void dbgm() { ; }
template <typename Heads, typename... Tails>
void dbgm(Heads H, Tails... T) {
cerr << to_string(H) << " | ";
dbgm(T...);
}
const long long mod = 1e9 + 7;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cerr << "...............Console is yours! :)................."
<< "\n";
long long n;
int m;
cin >> n >> m;
string num = to_string(n);
int l = num.length();
long long dp[1 << l][m];
memset(dp, 0, sizeof(dp));
long long fac = 1;
int count[10];
memset(count, 0, sizeof(count));
for (int i = 0; i < l; i++) fac *= ++count[num[i] -= '0'];
dp[0][0] = 1;
for (int i = 0; i < 1 << l; i++) {
for (int j = 0; j < l; j++) {
if (!(i & (1 << j)) && (i || num[j])) {
for (int k = 0; k < m; k++)
dp[i | (1 << j)][(k * 10 + num[j]) % m] += dp[i][k];
}
}
}
cout << dp[(1 << l) - 1][0] / fac << "\n";
cerr << "......^_^....."
<< "\n";
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
int dy[] = {0, 0, 0, 1, 1, 1, 2, 2, 2};
int dx[] = {0, 1, 2, 0, 1, 2, 0, 1, 2};
void solve() {
int a[9][9], wa[9][9] = {};
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
cin >> a[i][j];
}
}
for (int d = 1; d < 10; d++) {
for (int i = 0; i < 9; i++) {
int cnt = 0;
for (int j = 0; j < 9; j++) {
cnt += a[i][j] == d;
}
if (cnt == 1) continue;
for (int j = 0; j < 9; j++) {
if (a[i][j] == d) wa[i][j] = 1;
}
}
for (int j = 0; j < 9; j++) {
int cnt = 0;
for (int i = 0; i < 9; i++) {
cnt += a[i][j] == d;
}
if (cnt == 1) continue;
for (int i = 0; i < 9; i++) {
if (a[i][j] == d) wa[i][j] = 1;
}
}
for (int i = 0; i < 9; i += 3) {
for (int j = 0; j < 9; j += 3) {
int cnt = 0;
for (int k = 0; k < 9; k++) {
int ny = i + dy[k], nx = j + dx[k];
cnt += a[ny][nx] == d;
}
if (cnt == 1) continue;
for (int k = 0; k < 9; k++) {
int ny = i + dy[k], nx = j + dx[k];
if (a[ny][nx] == d) wa[ny][nx] = 1;
}
}
}
}
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
cout << " *"[wa[i][j]] << a[i][j];
}
cout << endl;
}
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(10);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
if (i) cout << endl;
solve();
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1000000000 + 7;
const long long int N = 10000000 + 6;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int q;
cin >> q;
for (int i = 0; i < q; ++i) {
long long n;
cin >> n;
vector<int> vals;
int pos2 = -1;
while (n > 0) {
vals.push_back(n % 3);
if (vals.back() == 2) pos2 = int(vals.size()) - 1;
n /= 3;
}
vals.push_back(0);
if (pos2 != -1) {
int pos0 = find(vals.begin() + pos2, vals.end(), 0) - vals.begin();
vals[pos0] = 1;
for (int i = pos0 - 1; i >= 0; --i) {
vals[i] = 0;
}
}
long long ans = 0;
long long pw = 1;
for (int i = 0; i < int(vals.size()); ++i) {
ans += pw * vals[i];
pw *= 3;
}
if (vals.back() == 1) ans = pw / 3;
cout << ans << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
inline long long in() {
int32_t x;
scanf("%d", &x);
return x;
}
inline long long lin() {
long long x;
scanf("%lld", &x);
return x;
}
const long long maxn = 1e3 + 10;
const long long inf = 1e18 + 10;
const long long mod = 1e9 + 7;
long long res = 1;
bool changed = false;
long long z[maxn];
long long cnt[maxn];
int32_t main() {
long long n = in(), m = in();
if ((n + m) % 2) return cout << 0 << endl, 0;
if (n < m) swap(n, m), changed = true;
long long k = in();
fill(z, z + maxn, 1);
for (long long i = 0; i < k; i++) {
long long x = in() - 1, y = in() - 1, value = in();
if (changed) swap(x, y);
z[x] *= value;
cnt[x]++;
}
bool vis = 0;
long long p = in();
for (long long i = 0; i < n; i++) {
if (!vis && cnt[i] == 0) {
vis = true;
continue;
}
if (cnt[i] == m && z[i] == 1) return cout << 0 << endl, 0;
for (long long pt = 0; pt < m - cnt[i] - 1; pt++) res = res * 2 % p;
}
assert(vis);
cout << res << endl;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
while (cin >> n) {
int t[100011];
for (int i = 1; i <= n; ++i) cin >> t[i];
int suma = 0, sumb = 0;
int l = 1, r = n;
int alice = 0, bob = 0;
while (l <= r) {
if (suma > sumb) {
sumb += t[r];
r--;
bob++;
} else {
suma += t[l];
l++;
alice++;
}
}
cout << alice << " " << bob << '\n';
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int k, i, n, l, count = 0, count1 = 0, j;
string s, fin = "";
cin >> s;
cin >> k;
char ans[k];
l = s.size();
int f = 0;
for (i = 0; i < l; i++) {
if (s[i] == '?') count++;
if (s[i] == '*') {
count1++;
}
}
int rl = l - count - count1;
if (k < rl - count - count1)
cout << "Impossible";
else if (k > rl && count1 == 0)
cout << "Impossible";
else if (k <= rl) {
int t = rl - k;
int m = 0;
for (j = 0; j < l; j++) {
if ((s[j] == '*' || s[j] == '?') && t > 0) {
m--;
t--;
} else if (s[j] != '*' && s[j] != '?')
ans[m++] = s[j];
}
for (j = 0; j < k; j++) cout << ans[j];
} else {
f = 0;
for (i = 0; i < l; i++) {
if (s[i] == '?')
continue;
else if (s[i] == '*' && f != 1 && i > 0) {
for (j = 0; j < k - rl; j++) fin = fin + s[i - 1];
f = 1;
} else if (s[i] != '*')
fin = fin + s[i];
}
cout << fin;
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') f = -1;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
return x * f;
}
const int MAXN = 1800010;
const int INF = 2147483600;
int N;
int Next[MAXN + 1], Root[MAXN + 1], tim;
vector<int> vec[MAXN + 1];
int a[MAXN + 1];
int col[MAXN + 1];
int id;
bool vis[MAXN + 1];
inline int D(int x) { return ((x - 1) ^ 1) + 1; }
int Node[MAXN << 1], Nexte[MAXN << 1], Roote[MAXN + 1], cnt, Cost[MAXN << 1];
inline void insert(int u, int v, int x) {
Node[++cnt] = v;
Nexte[cnt] = Roote[u];
Roote[u] = cnt;
Cost[cnt] = x;
}
inline bool dfs(int j, int bg) {
col[j] = 1;
if (a[j] == a[bg]) {
Next[D(j)] = bg;
return 1;
}
while (vec[a[j]].size() && col[*vec[a[j]].rbegin()]) vec[a[j]].pop_back();
if (!vec[a[j]].size()) return false;
int x = *vec[a[j]].rbegin();
vec[a[j]].pop_back();
Next[D(j)] = x;
col[x] = 1;
return dfs(D(x), bg);
}
int val[MAXN + 1];
pair<int, int> sta[MAXN + 1];
int In[MAXN + 1];
int main() {
N = read();
for (int i = 1; i <= N; i++) {
val[2 * i - 1] = read();
val[2 * i] = read();
}
for (int i = 20; i >= 0; i--) {
for (int j = 1; j <= 2 * N; j++) {
a[j] = (val[j] & ((1 << i) - 1)) + 1;
vec[a[j]].clear();
Next[j] = 0;
}
for (int j = 1; j <= 2 * N; j++) {
vec[a[j]].push_back(j);
In[a[j]] = 0;
col[j] = 0;
}
bool flag = 1;
tim = 0;
for (int j = 1; j <= 2 * N; j++) {
if (!col[j] && !col[D(j)])
++tim, Root[tim] = j, col[j] = 1, flag &= dfs(D(j), j);
}
if (!flag) continue;
for (int j = 1; j <= tim + (1 << i); j++) Roote[j] = 0, vis[j] = 0;
cnt = 0;
for (int j = 1; j <= tim; j++) {
bool Ad = 0;
int top = 0;
for (int d = Root[j]; d;) {
sta[++top] = make_pair(a[Next[d]], d);
insert(j, a[Next[d]] + tim, d);
insert(a[Next[d]] + tim, j, d);
d = Next[d];
if (d == Root[j]) break;
}
}
queue<int> que;
que.push(1);
vis[1] = 1;
int sm = 0;
while (!que.empty()) {
int k = que.front();
que.pop();
if (k > tim) {
for (int xt = Roote[k]; xt; xt = Nexte[xt]) {
int v = Node[xt];
if (vis[v]) continue;
int d = Cost[xt];
int x = In[k - tim];
int y = Next[x];
Next[x] = Next[d];
Next[d] = y;
que.push(v);
vis[v] = 1;
}
} else {
++sm;
for (int x = Roote[k]; x; x = Nexte[x]) {
int v = Node[x];
if (vis[v]) continue;
In[v - tim] = Cost[x];
que.push(v);
vis[v] = 1;
}
}
}
if (tim == sm) {
printf("%d\n", i);
int x = Root[1];
for (int k = 1; k <= N; k++) printf("%d %d ", x, D(x)), x = Next[x];
break;
}
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
char d[1010];
int main() {
int n;
scanf("%d", &n);
scanf("%s", d);
int pierwszeR = -1, ostatnieR = -1;
int pierwszeW, ostatnieW;
for (int i = 1; i <= n; i++) {
if (i != 1 && d[i - 1] != '.' && d[i - 2] == '.') pierwszeW = i;
if (i != n && d[i - 1] != '.' && d[i] == '.') ostatnieW = i;
if (d[i - 1] == 'R' && pierwszeR == -1) pierwszeR = i;
if (d[i - 1] == 'L' && ostatnieR == -1) ostatnieR = i - 1;
}
if (ostatnieR == -1)
printf("%d %d\n", pierwszeW, ostatnieW + 1);
else if (pierwszeR == -1)
printf("%d %d\n", ostatnieW, pierwszeW - 1);
else
printf("%d %d\n", pierwszeR, ostatnieR);
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T &x) {
char ch = getchar();
int f = 1;
while ((ch != '-') && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-')
f = 0, x = 0;
else
x = ch - 48;
ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - 48, ch = getchar();
if (!f) x = -x;
}
template <class T>
inline void read(T &x, T &y) {
read(x);
read(y);
}
template <class T>
inline void read(T &x, T &y, T &z) {
read(x);
read(y);
read(z);
}
template <class T>
inline void read(T &x, T &y, T &z, T &o) {
read(x);
read(y);
read(z);
read(o);
}
int _t[20];
template <class T>
inline void out(T x, int ln = 1) {
if (!x)
putchar('0');
else {
if (x < 0) putchar('-'), x = -x;
for (_t[0] = 0; x; x /= 10) _t[++_t[0]] = x % 10;
for (int i = (_t[0]); i >= (1); i--) putchar(_t[i] + 48);
}
if (ln) putchar('\n');
}
int n, H, h[100010], l[100010], r[100010], f[100010];
vector<pair<int, int> > v;
set<pair<int, int> > s;
set<int> g[100010];
set<pair<int, int> >::iterator it, pre, suc;
void upd(int x, int y) {
f[x] = max(f[x], min(f[y], min(r[x], r[y]) - max(l[x], l[y])));
}
int main() {
read(n, H);
for (int i = (1); i <= (n); i++)
read(h[i], l[i], r[i]), v.push_back(make_pair(l[i], i)),
v.push_back(make_pair(r[i], -i));
sort(v.begin(), v.end());
h[++n] = 0;
l[n] = -1000000000 - 10;
r[n] = 1000000000 + 10;
h[++n] = H;
l[n] = -1000000000 - 20;
r[n] = 1000000000 + 20;
s.insert(make_pair(h[n - 1], n - 1));
s.insert(make_pair(h[n], n));
for (int o = 0; o < v.size(); o++) {
int i = v[o].second;
if (i > 0) {
pre = suc = it = s.insert(make_pair(h[i], i)).first;
--pre;
++suc;
if (g[pre->second].find(suc->second) != g[pre->second].end())
g[pre->second].erase(suc->second);
g[pre->second].insert(i);
g[i].insert(suc->second);
} else
s.erase(make_pair(h[-i], -i));
}
v.clear();
for (int i = (1); i <= (n); i++) v.push_back(make_pair(h[i], i));
sort(v.begin(), v.end());
f[n - 1] = 2 * 1000000000;
for (int o = 0; o < v.size() - 1; o++) {
int i = v[o].second;
for (set<int>::iterator j = g[i].begin(); j != g[i].end(); j++) upd(*j, i);
}
out(f[n]);
return 0;
}
| 4 |
#include<iostream>
#include<cctype>
#include<cstdio>
#include<cstring>
#include<climits>
using namespace std;
long long len(const char *str){
long long ret = 0;
str++;
while(*str!=')'){
//printf(" %s : ret=%d\n",str,ret);
if(isdigit(*str)||*str=='('){
long long num;
if(*str=='('){
num = 1;
}else{
sscanf(str,"%lld",&num);
while(isdigit(*str)) str++;
}
long long length;
if(*str=='('){
length = len(str);
}else{
length = 1;
}
//printf(" len=%d, num=%d\n",length,num);
if(*str=='('){
long long nest = 0;
do{
if(*str=='(') nest++;
else if(*str==')') nest--;
str++;
}while(nest!=0);
}else{
str++;
}
ret+=num*length;
}else if(isalpha(*str)){
long long cnt = 0;
while(isalpha(*str)){
cnt++;
//printf(" alpha: %d (%c)\n",cnt,*str);
str++;
}
//printf(" alpha: %d (%c)\n",cnt,*str);
ret+=cnt;
}
if(ret > INT_MAX) return INT_MAX;
}
return ret;
}
char parse(const char *str, long long p){
//printf("str: %s p: %lld\n",str,p);
int f = (*str=='('?1:0);
if(p<0 || *str=='\0') return '0';
if(isdigit(*str)||*str=='('){
long long num;
if(*str=='('){
num = 1;
}else{
sscanf(str,"%lld",&num);
while(isdigit(*str)) str++;
}
long long length;
if(*str=='('){
length = len(str);
//printf(" %s: %d\n",str,length);
}else{
length = 1;
}
if(length==INT_MAX){
return parse(str+f, p);
}else if(num*length>p){
return parse(str+f, p%length);
}else{
if(*str=='('){
long long nest = 0;
do{
if(*str=='(') nest++;
else if(*str==')') nest--;
str++;
}while(nest!=0);
}else{
str++;
}
return parse(str, p-num*length);
}
}else if(isalpha(*str)){
if(p==0) return *str;
else return parse(str+1, p-1);
}
}
int main(){
char buff[120];
long long p;
while(scanf("%s%lld",buff,&p),p!=0 || strcmp(buff,"0")!=0)
printf("%c\n",parse(buff,p));
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
const int sigma_size = 26;
const int N = 20;
const int MAXN = 100000 + 50;
const int inf = 0x3fffffff;
const double eps = 1e-8;
const int HASH = 100007;
const int mod = 1000000000 + 7;
template <class T>
inline bool RD(T& res) {
char c;
int sgn;
if (c = getchar(), c == EOF) return 0;
while (c != '-' && (c < '0' || c > '9')) c = getchar();
sgn = (c == '-') ? -1 : 1;
res = (c == '-') ? 0 : c - '0';
while (c = getchar(), c >= '0' && c <= '9') res = res * 10 + (c - '0');
res *= sgn;
return 1;
}
template <class T>
inline void PT(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) PT(x / 10);
putchar(x % 10 + '0');
}
int n, K, edge_cnt;
int head[MAXN];
int anc[MAXN][N], up[MAXN], down[MAXN], dep[MAXN], cost[MAXN * 10];
struct Edge {
int d, v, next;
} edge[MAXN << 1];
void init() {
edge_cnt = 0;
memset(up, 0, sizeof(up));
memset(down, 0, sizeof(down));
memset(head, -1, sizeof(head));
}
void addedge(int u, int v, int d) {
edge[edge_cnt].d = d;
edge[edge_cnt].v = v;
edge[edge_cnt].next = head[u];
head[u] = edge_cnt++;
}
void dfs(int u, int fa, int depth) {
dep[u] = depth;
anc[u][0] = fa;
for (int i = (int)1; i <= (int)N - 1; i++)
anc[u][i] = anc[anc[u][i - 1]][i - 1];
for (int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].v;
if (v == fa) continue;
dfs(v, u, depth + 1);
}
}
int LCA(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
for (int i = (int)0; i <= (int)N - 1; i++)
if (dep[u] - dep[v] & (1 << i)) u = anc[u][i];
if (u == v) return v;
for (int i = (int)N - 1; i >= (int)0; i--)
if (anc[u][i] != anc[v][i]) u = anc[u][i], v = anc[v][i];
return anc[u][0];
}
int ans;
void DFS(int u, int fa) {
for (int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].v;
if (v == fa) continue;
int d1 = edge[i].d;
int d2 = edge[i ^ 1].d;
DFS(v, u);
if (d2 == 0) {
ans += cost[up[v]];
if (ans >= mod) ans %= mod;
}
if (d1 == 0) {
ans += cost[down[v]];
if (ans >= mod) ans %= mod;
}
up[u] += up[v];
down[u] += down[v];
}
}
int main() {
cost[0] = 0;
cost[1] = 1;
for (int i = (int)2; i <= (int)10 * MAXN - 1; i++)
cost[i] = cost[i - 1] * 2 % mod;
for (int i = (int)2; i <= (int)10 * MAXN - 1; i++)
cost[i] = (cost[i - 1] + cost[i]) % mod;
while (~scanf("%d", &n)) {
init();
for (int i = (int)1; i <= (int)n - 1; i++) {
int u, v, d;
RD(u);
RD(v);
RD(d);
if (d == 1) {
addedge(u, v, 1);
addedge(v, u, 0);
} else {
addedge(u, v, 1);
addedge(v, u, 1);
}
}
dfs(1, 1, 0);
RD(K);
int pre = 1;
while (K--) {
int now;
RD(now);
int lca = LCA(pre, now);
up[pre]++;
up[lca]--;
down[lca]--;
down[now]++;
pre = now;
}
ans = 0;
DFS(1, 1);
printf("%d\n", ans);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long MxV = 3000001;
struct dst {
long long r, x, y;
};
int main() {
long long n, k;
long long i, j;
long long a[10][55];
long long cnt = 0;
cin >> n >> k;
for (i = 0; i < 4; ++i) {
for (j = 0; j < n; ++j) {
cin >> a[i][j];
}
}
for (i = 1; i < 3; ++i) {
for (j = 0; j < n; ++j) {
if (a[i][j] != a[i - 1][j] && a[i][j] != a[i + 1][j] && a[i][j] != 0)
++cnt;
}
}
if (cnt == k && k == n * 2) return cout << -1, 0;
vector<dst> ans;
long long t[10][55];
while (k != 0) {
for (i = 0; i < 10; ++i) {
for (j = 0; j < 55; ++j) {
t[i][j] = 0;
}
}
for (i = 1; i < 3; ++i) {
for (j = 0; j < n; ++j) {
if (a[i - 1][j] == a[i][j] && i == 1 && a[i][j] != 0) {
ans.push_back({a[i][j], i, j + 1});
a[i][j] = 0;
--k;
}
if (a[i + 1][j] == a[i][j] && i == 2 && a[i][j] != 0) {
ans.push_back({a[i][j], i + 2, j + 1});
a[i][j] = 0;
--k;
}
}
}
pair<long long, long long> to;
for (j = 0; j < n; ++j) {
if (a[1][j] == 0) {
to = {1, j};
break;
}
}
if (to.first == 0 && to.second == 0) {
for (j = n - 1; j >= 0; --j) {
if (a[2][j] == 0) {
to = {2, j};
break;
}
}
}
if (to.first == 1) {
long long pos = to.second;
for (i = pos; i >= 1; --i) {
if (a[1][i - 1] != 0) {
t[1][i] = a[1][i - 1];
ans.push_back({a[1][i - 1], 2, i + 1});
}
}
if (a[2][0] != 0) {
t[1][0] = a[2][0];
ans.push_back({a[2][0], 2, 1});
}
for (i = 0; i < n - 1; ++i) {
if (a[2][i + 1] != 0) {
t[2][i] = a[2][i + 1];
ans.push_back({a[2][i + 1], 3, i + 1});
}
}
if (a[1][n - 1] != 0) {
t[2][n - 1] = a[1][n - 1];
ans.push_back({a[1][n - 1], 3, n});
}
for (i = n - 1; i >= pos + 2; --i) {
if (a[1][i - 1] != 0) {
t[1][i] = a[1][i - 1];
ans.push_back({a[1][i - 1], 2, i + 1});
}
}
} else {
long long pos = to.second;
for (i = pos; i < n - 1; ++i) {
if (a[2][i + 1] != 0) {
t[2][i] = a[2][i + 1];
ans.push_back({a[2][i + 1], 3, i + 1});
}
}
if (a[1][n - 1] != 0) {
t[2][n - 1] = a[1][n - 1];
ans.push_back({a[1][n - 1], 3, n});
}
for (i = n - 1; i >= 1; --i) {
if (a[1][i - 1] != 0) {
t[1][i] = a[1][i - 1];
ans.push_back({a[1][i - 1], 2, i + 1});
}
}
if (a[2][0] != 0) {
t[1][0] = a[2][0];
ans.push_back({a[2][0], 2, 1});
}
for (i = 0; i <= pos - 1; ++i) {
if (a[2][i + 1] != 0) {
t[2][i] = a[2][i + 1];
ans.push_back({a[2][i + 1], 3, i + 1});
}
}
}
for (i = 1; i < 3; ++i) {
for (j = 0; j < n; ++j) {
a[i][j] = t[i][j];
}
}
}
cout << ans.size() << endl;
for (i = 0; i < ans.size(); ++i) {
cout << ans[i].r << ' ' << ans[i].x << ' ' << ans[i].y << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2003;
int ans = 0;
int xx;
int dp[maxn][maxn];
char a[maxn], b[maxn];
int main() {
a[0] = b[0] = 1;
scanf("%s%s", &a[1], &b[1]);
int lena = strlen(a);
int lenb = strlen(b);
lena--;
lenb--;
for (int i = 1; i <= lena; i++) {
for (int j = 1; j <= lenb; j++) {
ans = max(ans, dp[i][j] = dp[i - 1][j - 1] + (a[i] == b[j]));
}
}
xx = lenb - ans;
cout << xx << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 105;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long m, b;
cin >> m >> b;
long long ans = 0;
for (long long y = b; y >= 0; --y) {
long long x = m * (b - y);
long long cnty = y * (y + 1) / 2;
long long cntx = x * (x + 1) / 2;
ans = max(ans, cntx * (y + 1) + cnty * (x + 1));
}
cout << ans;
}
| 2 |
#include <bits/stdc++.h>
const int maxn = 2000010;
long long val[maxn], n, sum, veryleft[maxn], cnt[maxn];
std::deque<long long> q;
long long query(int k) {
if (sum <= k) return 1;
long long inseq = sum, ans = maxn;
for (int i = 1; i <= n; i++) q.push_back(val[i + 1]);
for (int i = n + 1; i <= (n << 1); i++) {
while (inseq > k) {
inseq -= q.front();
q.pop_front();
}
int s = i - q.size(), t = veryleft[s];
if (t)
veryleft[i] = t;
else
veryleft[i] = s;
cnt[i] = cnt[s] + 1;
if (i - veryleft[i] >= n) ans = std::min(ans, cnt[i]);
if (i != (n << 1)) {
q.push_back(val[i + 1]);
inseq += val[i + 1];
}
}
while (!q.empty()) q.pop_back();
return ans;
}
int main() {
long long m;
scanf("%I64d%I64d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%I64d", &val[i]);
val[n + i] = val[i];
sum += val[i];
}
for (int i = 0; i < m; i++) {
long long k;
scanf("%I64d", &k);
printf("%I64d\n", query(k));
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
#pragma warning(disable : 4996)
#pragma comment(linker, "/STACK:36777216")
using namespace std;
const long double EPS = 1e-9;
const int INF = 1e9 + 7;
long double dp[4][2002][2002];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << fixed << setprecision(7);
int n, h;
long double p;
cin >> n >> h >> p;
vector<int> xs(n);
for (int i = 0; i < n; ++i) {
cin >> xs[i];
}
sort(xs.begin(), xs.end());
for (int l = n - 1; l >= 0; --l) {
for (int r = l; r < n; ++r) {
for (int st = 0; st < 4; ++st) {
dp[st][l][r] = 0.;
int hl = st & 1;
int hr = st & 2;
int liml = (l == 0 ? -INF : xs[l - 1] + (hl ? h : 0));
int limr = (r == n - 1 ? INF : xs[r + 1] - (hr ? h : 0));
int add_ll = max(0, min(limr, xs[l]) - max(liml, xs[l] - h));
int add_lr = max(0, min(limr, xs[l] + h) - max(liml, xs[l]));
int add_rl = max(0, min(limr, xs[r]) - max(liml, xs[r] - h));
int add_rr = max(0, min(limr, xs[r] + h) - max(liml, xs[r]));
if (hl && l > 0 && xs[l] - xs[l - 1] < h) {
dp[st][l][r] = (l == r ? 0 : dp[st][l + 1][r]) + (long double)add_lr;
continue;
}
if (hr && r < n - 1 && xs[r + 1] - xs[r] < h) {
dp[st][l][r] = (l == r ? 0 : dp[st][l][r - 1]) + (long double)add_rl;
continue;
}
if (l == r) {
dp[st][l][r] =
(long double)add_ll * p + (long double)add_lr * (1. - p);
continue;
}
dp[st][l][r] += (dp[st & 2][l + 1][r] + (long double)add_ll) * p;
dp[st][l][r] += (dp[st | 1][l + 1][r] + (long double)add_lr) * (1. - p);
dp[st][l][r] += (dp[st & 1][l][r - 1] + (long double)add_rr) * (1. - p);
dp[st][l][r] += (dp[st | 2][l][r - 1] + (long double)add_rl) * p;
dp[st][l][r] *= 0.5;
}
}
}
cout << dp[0][0][n - 1];
return 0;
};
| 4 |
#include <bits/stdc++.h>
using namespace std;
int n, m, k, x, y, sum, cnt, ans, a[85], f[85][85];
int main() {
scanf("%d %d", &n, &k);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
for (int i = 1; i <= n; i++) f[n][i] = INT_MAX;
for (int i = n - 1; i >= 0; i--) {
for (int j = 1; j <= n; j++) {
if (a[i] == j)
f[i][j] = i;
else
f[i][j] = f[i + 1][j];
}
}
set<int> s;
for (int i = 0; i < n; i++) {
if (s.count(a[i]) == 0) {
if (s.size() == k) {
int maxx = -1, maxi;
for (int j = 1; j <= n; j++) {
if (s.count(j) && f[i][j] > maxx) {
maxx = f[i][j];
maxi = j;
}
}
s.erase(maxi);
s.insert(a[i]);
ans++;
} else {
s.insert(a[i]);
ans++;
}
}
}
printf("%d\n", ans);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const long long md = (long long)1e9 + 7, inf = (long long)1e18;
vector<vector<int>> g;
vector<int> mark, us, wh, bl;
int sz = 1;
void add(long long x, int k, int c) {
int v = 0;
for (int i = 0; i < k; i++) {
int whr = 31 - i;
int to = (x >> whr) & 1;
if (g[v][to] == -1) {
g.push_back({-1, -1});
mark.push_back(0);
wh.push_back(0);
bl.push_back(0);
g[v][to] = sz++;
}
v = g[v][to];
}
if (mark[v] && mark[v] != c) {
cout << "-1\n";
exit(0);
}
mark[v] = c;
}
void dfs(int v) {
wh[v] = mark[v] == 1;
bl[v] = mark[v] == -1;
for (int u : g[v]) {
if (u == -1) continue;
dfs(u);
wh[v] += wh[u], bl[v] += bl[u];
}
if (mark[v] == 1 && bl[v]) {
cout << "-1\n";
exit(0);
}
if (mark[v] == -1 && wh[v]) {
cout << "-1\n";
exit(0);
}
}
vector<pair<int, long long>> ans;
void doit(int v, long long cur, int d) {
if (!wh[v]) {
ans.push_back({d, cur});
return;
}
for (int i = 0; i < 2; i++) {
int u = g[v][i];
if (u != -1) doit(u, cur + i * (1ll << (31 - d)), d + 1);
}
}
pair<int, long long> f(string s) {
long long cur = 0;
long long t = 0;
reverse(s.begin(), s.end());
while (s.size()) {
char c = s.back();
s.pop_back();
if (c == '.') {
cur *= 1 << 8;
cur += t;
t = 0;
continue;
}
if (c == '/') {
cur *= 1 << 8;
cur += t;
t = 0;
break;
}
t = (t * 10) + c - '0';
}
if (!s.size())
return {32, cur * (1 << 8) + t};
else {
while (s.size()) {
char c = s.back();
s.pop_back();
t = (t * 10) + c - '0';
}
return {t, cur};
}
}
string nf(pair<int, long long> p) {
string ret = "";
for (int i = 0; i < 4; i++) {
int c = 0;
for (int j = i * 8; j < (i + 1) * 8; j++)
c += ((p.second >> j) & 1) * (1 << (j - (i * 8)));
string t = to_string(c);
reverse(t.begin(), t.end());
if (i) ret += ".";
ret += t;
}
reverse(ret.begin(), ret.end());
return ret + "/" + to_string(p.first);
}
int main() {
iostream::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
g.resize(1, {-1, -1});
mark.resize(1, 0);
wh.resize(1, 0);
bl.resize(1, 0);
int n;
cin >> n;
while (n--) {
string s;
cin >> s;
bool ch = s[0] == '-';
s = s.substr(1, s.length() - 1);
pair<int, long long> cur = f(s);
add(cur.second, cur.first, (ch ? -1 : 1));
}
dfs(0);
if (!bl[0]) {
cout << "0\n";
return 0;
}
doit(0, 0, 0);
cout << ans.size() << '\n';
for (auto &c : ans) cout << nf(c) << '\n';
return 0;
}
| 2 |