solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <bits/stdc++.h>
using namespace std;
const long long inf = 0x3f3f3f3f;
const int N = 2e5 + 10;
const long long mod = 998244353;
int n, m, t, sx, sy, k, sum, cnt, ans;
int r, c;
long long p[N];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
string s;
string st;
cout << 0 << " " << 1 << endl;
cin >> st;
int l = 2, r = 1e9;
for (int i = 1; i < n; i++) {
int mid = (l + r) >> 1;
cout << mid << " " << 1 << endl;
cin >> s;
if (s == st)
l = mid - 1;
else
r = mid + 1;
}
cout << l << " " << 0 << " " << r << " " << 2 << endl;
}
| 5 |
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main()
{
int n;
cin >> n;
string s; cin >> s;
cout << (count(s.begin(), s.end(), 'R') > count(s.begin(), s.end(), 'B') ? "Yes" : "No") << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long s, n, i;
cin >> n >> s;
if (s < 2 * n) {
cout << "NO";
} else {
cout << "YES" << endl;
for (i = 0; i < n - 1; i++) {
cout << 1 << " ";
}
cout << s - n + 1 << endl << n;
}
return 0;
}
| 4 |
#include <iostream>
#include <string>
using namespace std;
string S; long long K;
int main() {
cin >> S >> K;
for(int i = 0; i < S.size(); i++) {
if(S[i] == '1') continue;
if(i < K) {cout << S[i]; return 0;}
}
cout << '1';
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
long long t1;
cin >> t1;
long long t2;
cin >> t2;
long long x1;
cin >> x1;
long long x2;
cin >> x2;
long long t0;
cin >> t0;
auto opt = make_pair((double)t2, 0L);
if (t2 == t0) {
if (t1 == t0)
cout << x1 << ' ' << x2;
else
cout << 0 << ' ' << x2;
return 0;
}
for (int y1 = 0; y1 < x1 + 1; ++y1) {
long long y2 = y1 == 0 ? x2 : (t0 * y1 - t1 * y1 + t2 - t0 - 1) / (t2 - t0);
if (y2 > x2) continue;
double t = t1 * y1 + t2 * y2;
t /= (y1 + y2);
{ opt = min(opt, make_pair(t, -1L * y1)); };
}
{
long long y1 = -opt.second;
long long y2 = y1 == 0 ? x2 : (t0 * y1 - t1 * y1 + t2 - t0 - 1) / (t2 - t0);
if (y1 != 0 && y2 != 0) {
long long d = min(x1 / y1, x2 / y2);
y1 *= d;
y2 *= d;
}
cout << y1 << ' ' << y2;
}
}
| 3 |
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int dp[100][400][400];
int main(){
int tc; cin >> tc;
while(tc--){
int L; cin >> L;
vector<int>vec(L);
for(int i = 0 ; i < L ; i++)cin >> vec[i];
for(int i = 0 ; i < L ; i++)
for(int j = 0 ; j < 400 ; j++)
for(int k = 0 ; k < 400 ; k++)
dp[i][j][k] = -1;
dp[0][vec[0]][vec[1]] = vec[2];
vec.push_back(0);
int res = vec[L-1];
for(int i = 0 ; i < L-2 ; i++){
for(int j = 0 ; j <= 320 ; j++){
for(int k = 0 ; k <= 320 ; k++){
if(dp[i][j][k] == -1)continue;
dp[i+1][k][dp[i][j][k]] = vec[i+3];
if(k-1 >= 0){
dp[i+1][k-1][dp[i][j][k]+j] = vec[i+3];
if(i+3 == L){
res = max(res,dp[i][j][k]+j);
}
}
}
}
}
cout << res << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
int num[500009];
int vis[1000009];
int n, m;
int maxx, left, right, tmp;
void solve() {
int differ = 0;
int cur = 0;
for (int i = 1; i <= n; i++) {
int val = differ;
if (val < m) {
while (1) {
if (++cur > n || val == m && !vis[num[cur]]) break;
if (vis[num[cur]]) {
vis[num[cur]]++;
continue;
} else if (!vis[num[cur]]) {
val++;
vis[num[cur]] = 1;
continue;
}
}
cur = cur - 1;
differ = val;
}
tmp = cur - i + 1;
if (vis[num[i]] == 1) {
vis[num[i]] = 0;
differ--;
} else if (vis[num[i]] > 1)
vis[num[i]]--;
if (tmp > maxx) {
maxx = tmp;
left = i;
right = cur;
}
}
}
int main(void) {
scanf("%d%d", &n, &m);
memset(vis, 0, sizeof(vis));
left = 0;
right = 0;
maxx = 0;
for (int i = 1; i <= n; i++) scanf("%d", &num[i]);
solve();
printf("%d %d\n", left, right);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
bool is_prime(long long i) {
for (long long j = 2; j <= sqrt(i); j++) {
if (i % j == 0) return false;
}
return true;
}
set<long long> find_factors(long long x, long long y) {
set<long long> A;
while (x % 2 == 0) {
x = x / 2;
A.insert(2);
}
for (long long i = 3; i <= sqrt(x); i += 2) {
while (x % i == 0) {
x = x / i;
A.insert(i);
}
}
if (x > 2) A.insert(x);
while (y % 2 == 0) {
y = y / 2;
A.insert(2);
}
for (long long i = 3; i <= sqrt(y); i += 2) {
while (y % i == 0) {
y = y / i;
A.insert(i);
}
}
if (y > 2) A.insert(y);
return A;
}
int main() {
long long n;
cin >> n;
vector<pair<long long, long long>> A;
for (long long i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
A.push_back(make_pair(x, y));
}
long long x = A[0].first;
long long y = A[0].second;
set<long long> factors = find_factors(x, y);
bool ans = 0;
for (auto it : factors) {
bool flag = true;
for (long long i = 0; i < n; i++) {
if (A[i].first % it != 0 && A[i].second % it != 0) {
flag = false;
break;
}
}
if (flag) {
cout << it << endl;
ans = 1;
break;
}
}
if (!ans) cout << -1 << endl;
return 0;
}
| 2 |
#include <cstdio>
#include <vector>
#include <set>
#include <string>
#include <queue>
int main() {
size_t n;
char buf[2048];
scanf("%zu %s", &n, buf);
std::string s = buf;
std::queue<std::string> q;
q.push(s);
std::set<std::string> st, vis;
while (!q.empty()) {
std::string t = q.front();
q.pop();
std::string t0 = t.substr(0, t.length()/2);
std::string t1 = t.substr(t.length()/2);
if (t0 != t1) st.insert(t);
if (t0.length() > 1 && !vis.count(t0)) {
vis.insert(t0);
q.push(t0);
}
if (t1.length() > 1 && !vis.count(t1)) {
vis.insert(t1);
q.push(t1);
}
}
// for (auto const& s0: st) fprintf(stderr, "%s\n", s0.c_str());
printf("%zu\n", st.size());
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n, c1, c2, c3, t1, t2, t3;
int dp[10005];
int main() {
while (~scanf("%d%d%d%d%d%d%d", &n, &c1, &c2, &c3, &t1, &t2, &t3)) {
memset(dp, 0, sizeof(dp));
for (int i = 1; i <= n; i++) {
if (i >= c1) dp[i] = max(dp[i], dp[i - c1] + t1);
if (i >= c2) dp[i] = max(dp[i], dp[i - c2] + t2);
if (i >= c3) dp[i] = max(dp[i], dp[i - c3] + t3);
}
printf("%d\n", dp[n - 1] + t1 + t2 + t3);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
struct crop {
int x, y;
};
string a[] = {"Carrots", "Kiwis", "Grapes"};
map<int, int> mp;
vector<crop> v;
vector<int> vv;
int n, m, k, t, x, y, sum, temp;
crop cr;
bool found = false;
int main() {
cin >> n >> m >> k >> t;
while (k--) {
cin >> x >> y;
cr.x = x;
cr.y = y;
v.push_back(cr);
}
while (t--) {
cin >> x >> y;
cr.x = x;
cr.y = y;
for (int j = 0; j < v.size(); ++j) {
if (v[j].x == cr.x && v[j].y == cr.y) {
found = true;
break;
}
}
if (found) {
cout << "Waste" << endl;
found = false;
} else {
cr.x = x;
cr.y = 1;
temp = (((x - 1) * m) + (y - 1));
for (int i = 0; i < v.size(); i++) {
if (v[i].x == x && v[i].y == y)
break;
else if (v[i].x < x)
sum++;
else if (v[i].x == x && v[i].y < y)
sum++;
}
temp -= sum;
cout << a[temp % 3] << endl;
}
sum = 0;
temp = 0;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int cases(long long n, long long k, long long d1, long long d2) {
if (n % 3 != 0) {
return 0;
}
for (int sign1 = -1; sign1 <= 1; sign1++) {
for (int sign2 = -1; sign2 <= 1; sign2++) {
if (sign1 == 0 || sign2 == 0) {
continue;
}
long long D1 = d1 * sign1;
long long D2 = d2 * sign2;
long long x2 = (k - D1 + D2) / 3;
if ((k - D1 + D2) % 3 != 0) {
continue;
}
if (x2 >= 0 && x2 <= k) {
long long x1 = D1 + x2;
long long x3 = x2 - D2;
if (x1 >= 0 && x1 <= k && x3 >= 0 && x3 <= k) {
if (x1 <= n / 3 && x2 <= n / 3 && x3 <= n / 3) {
return 1;
}
}
}
}
}
return 0;
}
int main() {
int t;
cin >> t;
while (t--) {
long long n, k, d1, d2;
cin >> n >> k >> d1 >> d2;
int ans = cases(n, k, d1, d2);
if (ans == 1)
cout << "yes" << endl;
else
cout << "no" << endl;
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = (1 << 20);
long long n;
long long a[MAXN];
long long p[MAXN];
void read() {
cin >> n;
for (long long i = 1; i <= n; i++) cin >> a[i];
for (long long j = 1; j <= n; j++) cin >> p[j];
}
long long par[MAXN], sz[MAXN], ans, sum[MAXN];
long long used[MAXN];
void init_dsu(long long k) {
for (long long i = 0; i <= k; i++)
used[i] = 0, sz[i] = 1, sum[i] = 0, par[i] = i;
}
long long root(long long x) { return x == par[x] ? x : par[x] = root(par[x]); }
void unite(long long u, long long v) {
u = root(u), v = root(v);
if (u == v) return;
if (sz[u] < sz[v]) swap(u, v);
par[v] = u;
sz[u] += sz[v];
sum[u] += sum[v];
ans = max(ans, sum[u]);
}
long long answer[MAXN];
void solve() {
init_dsu(n);
for (long long i = n; i >= 1; i--) {
answer[i] = ans;
if (used[p[i] - 1]) unite(p[i], p[i] - 1);
if (used[p[i] + 1]) unite(p[i], p[i] + 1);
sum[root(p[i])] += a[p[i]];
ans = max(ans, sum[root(p[i])]);
used[p[i]] = 1;
}
for (long long i = 1; i <= n; i++) cout << answer[i] << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
read();
solve();
return 0;
}
| 3 |
#include <iostream>
#include <cstdio>
#define N 400
using namespace std;
int n, a, b, c, m1, m2, mn = 1e9, d[805][805];
int main()
{
int i, j;
cin >> n >> m1 >> m2;
for (i = 0; i <= N; i++) {
for (j = 0; j <= N; j++) {
d[i][j] = 1e9;
}
}
d[0][0] = 1;
while (n--) {
scanf("%d %d %d", &a, &b, &c);
for (i = N; i >= 0; i--) {
for (j = N; j >= 0; j--) {
if (d[i][j] < 1e9) {
d[i + a][j + b] = min(d[i + a][j + b], d[i][j] + c);
}
}
}
}
for (i = 1; i <= N; i++) {
for (j = 1; j <= N; j++) {
if (i % m1 == 0 && j % m2 == 0 && i / m1 == j / m2) mn = min(mn, d[i][j]);
}
}
if (mn == 1e9) mn = 0;
cout << mn - 1 << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10;
char tmp[maxn];
char st[6][maxn] = {"jolteon", "flareon", "umbreon",
"leafeon", "glaceon", "sylveon"};
bool vis[6];
int main() {
int len;
scanf("%d", &len);
scanf("%s", tmp);
if (len == 6) {
printf("espeon");
} else if (len == 8) {
printf("vaporeon");
} else {
for (int i = 0; i < len; i++) {
if (tmp[i] >= 'a' && tmp[i] <= 'z') {
for (int j = 0; j < 6; j++) {
if (tmp[i] != st[j][i]) vis[j] = true;
}
}
}
int id;
for (int i = 0; i < 6; i++) {
if (!vis[i]) id = i;
}
printf("%s", st[id]);
}
return 0;
}
| 1 |
#include <cstdio>
int main()
{
int a, b;
scanf("%d %d", &a, &b);
printf("%d\n", 2 * b - a);
return 0;
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,m;
cin >> n >> m;
vector<int> a(m),b(m);
vector<pair<int,int>> c(m);
for(int i=0;i<m;i++){
cin >> a[i] >> b[i];
c[i]=make_pair(b[i],a[i]);
}
sort(c.begin(),c.end());
int cnt=1;
int x=c[0].first;
for(int i=1;i<m;i++){
if(c[i].second>=x){
cnt++;
x=c[i].first;
}
}
cout << cnt << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int a[105];
int main() {
long long int a, b, c, d, e, f;
double t1, t2, t3;
double s1, s2, s3, s4;
double a1, a2, a3, a4;
scanf("%lld %lld %lld %lld %lld %lld", &a, &b, &c, &d, &e, &f);
double ta;
t1 = sqrt(1.0 * a * a + b * b * 1.0 + 1.0 * a * b);
t2 = sqrt(1.0 * c * c + d * d * 1.0 + 1.0 * c * d);
t3 = sqrt(1.0 * e * e + f * f * 1.0 + 1.0 * e * f);
s1 = (t1 + 1.0 * a + b) / 2;
a1 = sqrt(s1 * (s1 - t1) * (s1 - a) * (s1 - b));
s2 = (t2 + 1.0 * c + d) / 2;
a2 = sqrt(s2 * (s2 - t2) * (s2 - c) * (s2 - d));
s3 = (t3 + 1.0 * e + f) / 2;
a3 = sqrt(s3 * (s3 - t3) * (s3 - e) * (s3 - f));
s4 = (t1 + 1.0 * t2 + t3) / 2;
a4 = sqrt(s4 * (s4 - t1) * (s4 - t2) * (s4 - t3));
double fans;
long long int ans;
ta = (a1 + a2 + a3 + a4) * 4.0;
fans = ta / (sqrt(3));
ans = (long long int)fans;
cout << ans << "\n";
return 0;
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int a,b,x;
cin>>a>>b>>x;
cout<<(a<=x&&a+b>=x?"YES":"NO")<<endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const double EPS = (1e-7);
int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; }
long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); }
long long lcm(long long a, long long b) { return (a * (b / gcd(a, b))); }
bool in_range(int i, int j, int n, int m) {
return i >= 0 && i < n && j >= 0 && j < m;
}
int arr[111][111];
void solution() {
int n, a, b;
cin >> n >> a >> b;
if (n > a * b) {
cout << -1;
return;
}
int i = 0, j = 0, r = 1, x = 1;
while (x <= n) {
if (j == b)
r ^= 1, j--, i++;
else if (j == -1)
r ^= 1, j++, i++;
arr[i][j] = x++;
r ? j++ : j--;
}
for (int i = 0; i < int(a); i++) {
for (int j = 0; j < int(b - 1); j++) cout << arr[i][j] << " ";
cout << arr[i][b - 1] << endl;
};
}
int main() {
ios::sync_with_stdio(false);
solution();
return 0;
}
| 1 |
#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
using namespace std;
struct po{int x,y,dis;};
int h,w;
string mp[20],str;
int D[21][21];
void bfs(po s){
for(int i=0;i<h;i++)
for(int j=0;j<w;j++) D[i][j] = 10000000;
D[s.y][s.x] = 0;
queue<po> Q;
Q.push((po){s.x,s.y,0});
while(!Q.empty()){
po t=Q.front();Q.pop();
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};
for(int i=0;i<4;i++){
int nx = t.x+dx[i];
int ny = t.y+dy[i];
if(nx<0||ny<0||nx>=w||ny>=h||mp[ny][nx]!='.'||D[ny][nx]<=t.dis+1)continue;
Q.push((po){nx,ny,t.dis+1});
D[ny][nx]=t.dis+1;
}
}
}
void move(po s){
po ima,ans;
ima.x=s.x,ima.y=s.y;
if(D[ima.y][ima.x]==0)ans.x=ima.x,ans.y=ima.y,ans.dis=0;
else ans.dis = 10000000;
int visited[21][21]={};
int dx[]={0,-1,1,0};
int dy[]={1,0,0,-1};
int cnt=0;
while(1){
visited[ima.y][ima.x]=1;
for(int i=0;i<str.size();i++){
cnt++;
int j = (str[i]-'0')/2-1;
int nx=ima.x+dx[j];
int ny=ima.y+dy[j];
if(nx<0||ny<0||nx>=w||ny>=h||str[i]=='5')nx=ima.x,ny=ima.y;
ima.x=nx,ima.y=ny;
if(ans.dis>cnt && cnt>=D[ny][nx]) ans.x=ima.x,ans.y=ima.y,ans.dis=cnt;
}
if(cnt>5000)break;
}
if(ans.dis==10000000 || ans.dis>1000) cout <<"impossible"<<endl;
else cout<< ans.dis<<" " << ans.y <<" "<< ans.x<<endl;
}
int main() {
while(1){
cin >> h >> w;
if(h==0 && w==0)break;
for(int i=0;i<h;i++) cin >> mp[i];
cin>> str;
po A,B;
for(int i=0;i<h;i++)
for(int j=0;j<w;j++){
if(mp[i][j] == 'A') A.y=i ,A.x=j;
if(mp[i][j]=='B') B.y=i,B.x=j;
}
for(int i=0;i<h;i++)
for(int j=0;j<w;j++)
if(mp[i][j] == 'A'||mp[i][j]=='B') mp[i][j] = '.';
bfs(A);
move(B);
}
return 0;
}
| 0 |
#include <iostream>
#include <vector>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define PRIME_MAX 1500000
vector<bool> prime(PRIME_MAX+1,true);
void init_prime(){
prime[0] = prime[1] = false;
for(int i=2;i*i<=PRIME_MAX;i++)
if(prime[i]) for(int j=i*i;j<=PRIME_MAX;j+=i)prime[j] = false;
}
int main(){
init_prime();
int n;
while(cin >> n ,n ){
int h = 0,l = 0;
for(int i=n;i<prime.size();i++)
if(prime[i]){h = i;break;}
for(int i=n;i>=0;i--)
if(prime[i]){l = i;break;}
cout << h - l << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define FOR(i,j,k) for(int i=j;i<=k;i++)
#define read freopen("in.txt", "r", stdin)
#define write freopen("out.txt", "w", stdout)
#define pf printf
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define PB push_back
#define F first
#define S second
#define MP make_pair
typedef long long ll;
typedef pair<int,int> pii;
const int M = 998244353 ;
const int MAX = (5e5) + 10;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, m, k;
unordered_map<int, int> cou;
vector<int> mat[MAX];
string ss;
int getCharId(char c) {
if(c == '_') {
return 0;
}
return (c - 'a') + 1;
}
int getHash(string str) {
int h = 0, b = 1;
for(char c: str) {
h += (b * getCharId(c));
b *= 27;
}
return h;
}
vector<int> lists(string ss) {
vector<int> cao;
FOR(bit,0,(1<<k)-1) {
string cp = ss;
FOR(p,0,k-1) {
if(bit & (1<<p)) {
cp[p] = '_';
}
}
int h = getHash(cp);
if(cou.find(h) != cou.end()) {
cao.PB(cou[h]);
}
}
return cao;
}
bool hasCir, in[MAX], out[MAX];
queue<int> stk;
void dfs(int p) {
if(in[p]) {
hasCir = !out[p] ? true : hasCir;
return;
}
in[p] = 1;
for(int x: mat[p]) {
dfs(x);
}
out[p] = 1;
stk.push(p);
}
void solve() {
int v;
cin >> n >> m >> k;
FOR(i,0,n-1) {
cin >> ss;
cou[getHash(ss)] = i + 1;
}
FOR(i,0,m-1) {
cin >> ss >> v;
vector<int> mye = lists(ss);
int ok = 0;
for(int x: mye) {
if(x == v) {
ok = 1;
} else {
mat[x].PB(v);
}
}
if(!ok) {
cout << "NO" << endl;
return;
}
}
FOR(i,1,n) {
if(!in[i]) {
dfs(i);
}
}
if(hasCir) {
cout << "NO" << endl;
return;
}
cout << "YES" << endl;
while(!stk.empty()) {
cout << stk.front() << " ";
stk.pop();
}
}
int main() {
ios::sync_with_stdio(false);
//read;
//write;
solve();
return 0;
}
| 5 |
#include <cstdio>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
#define maxn 105
#define maxx 1000000007
long long dp[maxn][maxn][256];
int a[maxn];
int main()
{
memset(dp, 0, sizeof(dp));
int n, kk;
cin >> n >> kk;
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = 0; i <= n; i++)
dp[i][0][0] = 1;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= i; j++)
{
dp[i][1][a[j]] = 1;
}
}
for (int i = 2; i <= n; i++)
{
for (int j = 2; j <= i; j++)
{
for (int k = 0; k <= 255; k++)
{
dp[i][j][k] += dp[i - 1][j - 1][a[i]^k];
dp[i][j][k] %= maxx;
dp[i][j][k] += dp[i - 1][j][k];
dp[i][j][k] %= maxx;
}
}
}
long long anss = 1;
long long ans = 0;
for (int i = 0; i <= n; i++)
{
anss = 1;
if (dp[n][i][kk]>0)
{
for (int j = 1; j <= i; j++)
{
anss *= j;
anss %= maxx;
}
anss *= dp[n][i][kk];
anss %= maxx;
ans += anss;
ans %= maxx;
}
}
cout << ans << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
int main() {
int32_t n;
std::cin >> n;
std::string* target = new std::string[n];
for (int32_t i = 0; i < n; i++) {
std::cin >> target[i];
}
bool *inverted_row = new bool[n], *inverted_col = new bool[n];
bool* diagonal = new bool[n];
for (int32_t i = 0; i < n; i++) {
inverted_row[i] = false;
inverted_col[i] = false;
diagonal[i] = false;
}
int32_t answer = 0;
for (int32_t i = n - 1; i > 0; i--) {
for (int32_t j = 0; j < i; j++) {
bool cur_state = inverted_row[i] ^ inverted_col[j];
if ((cur_state && target[i][j] == '0') ||
(!cur_state && target[i][j] == '1')) {
inverted_row[i] = !inverted_row[i];
inverted_col[j] = !inverted_col[j];
diagonal[i] = !diagonal[i];
diagonal[j] = !diagonal[j];
answer++;
}
}
}
for (int32_t i = 0; i < n; i++) {
inverted_row[i] = false;
inverted_col[i] = false;
}
for (int32_t i = 0; i < n - 1; i++) {
for (int32_t j = n - 1; j > i; j--) {
bool cur_state = inverted_row[i] ^ inverted_col[j];
if ((cur_state && target[i][j] == '0') ||
(!cur_state && target[i][j] == '1')) {
inverted_row[i] = !inverted_row[i];
inverted_col[j] = !inverted_col[j];
diagonal[i] = !diagonal[i];
diagonal[j] = !diagonal[j];
answer++;
}
}
}
for (int32_t i = 0; i < n; i++)
if ((diagonal[i] && target[i][i] == '0') ||
(!diagonal[i] && target[i][i] == '1'))
answer++;
std::cout << answer;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int i, j, maxcash, t, hour[100010], minute[100010], cash = 1;
cin >> t;
for (i = 0; i < t; i++) {
cin >> hour[i] >> minute[i];
}
i = 0;
maxcash = 1;
while (i < t) {
cash = 1;
j = i + 1;
while (j < t && hour[j] == hour[i] && minute[j] == minute[i]) {
cash++;
j++;
}
i = j;
if (cash > maxcash) maxcash = cash;
}
cout << maxcash << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long x,k,d;
int main() {
cin >> x >> k >> d;
x = abs(x);
if(x/d > k) cout << x-k*d;
else {
k -= x/d;
x -= (x/d)*d;
if(k%2 == 0) cout << x;
else cout << abs(x-d);
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
const double EPS = 1e-9;
const double PI = acos(-1.0);
const long long INF = 1070000000LL;
const long long MOD = 1000000007LL;
using namespace std;
int n, m;
char c[55][55];
int used[55][55];
int dy[] = {0, 1, 0, -1};
int dx[] = {1, 0, -1, 0};
int dfs(int y, int x, int py, int px) {
used[y][x] = 1;
for (long long d = (long long)(0); d < (long long)(4); d++) {
int ny = y + dy[d];
int nx = x + dx[d];
if (c[ny][nx] && c[ny][nx] == c[y][x]) {
if (used[ny][nx]) {
if (ny != py || nx != px) {
return 1;
}
} else {
if (dfs(ny, nx, y, x)) return 1;
}
}
}
return 0;
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
cin >> n >> m;
for (long long i = (long long)(1); i < (long long)(n + 1); i++)
cin >> c[i] + 1;
for (long long i = (long long)(1); i < (long long)(n + 1); i++)
for (long long j = (long long)(1); j < (long long)(m + 1); j++)
if (used[i][j] == 0) {
if (dfs(i, j, -1, -1)) return cout << "Yes", 0;
}
cout << "No";
}
| 2 |
#include <iostream>
#include <math.h>
using namespace std;
int x,y,s;
int tax(int r,int p);
int tax(int r,int p){ //r????¨????(%)???p????¨??????????
return floor(p*(100+r)/100);
}
int main(){
int maximum;
while(cin>>x>>y>>s && x>0){
maximum = 0;
for(int i=1; i<s; i++){
for(int j=1; j<s; j++){
if((tax(x,i)+tax(x,j))==s){
maximum = max(maximum, tax(y,i)+tax(y,j));
}
}
}
cout << maximum << endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;
cin >> n;
string s;
cin >> s;
vector<long long int> col;
int zero = 0, one = 0;
col.push_back(0);
for (int i = 1; i < s.size(); i++) {
if (s[i] >= s[zero]) {
col.push_back(0);
zero = i;
} else {
if (s[i] < s[one] && one != 0) {
cout << "NO";
return 0;
} else {
col.push_back(1);
one = i;
}
}
}
cout << "YES" << '\n';
for (auto i : col) cout << i;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void chmin(T &x, const T &y) {
if (x > y) x = y;
}
template <typename T>
void chmax(T &x, const T &y) {
if (x < y) x = y;
}
int read() {
char c;
while ((c = getchar()) < '-')
;
if (c == '-') {
int x = (c = getchar()) - '0';
while ((c = getchar()) >= '0') x = x * 10 + c - '0';
return -x;
}
int x = c - '0';
while ((c = getchar()) >= '0') x = x * 10 + c - '0';
return x;
}
const int N = 2000 + 5, D = 1e9 + 7;
void split(map<int, int> &Map, int x) {
auto it = --Map.lower_bound(x + 1);
Map[x] = it->second;
}
namespace A {
map<int, int> Map;
int64_t sum;
void upd(const pair<int, int> &pr) {
int nx = pr.first + 1, ny = pr.second;
if (nx <= Map.begin()->first) return;
split(Map, nx);
while (1) {
auto it = --Map.lower_bound(nx);
if (it->second >= ny) {
if (nx <= pr.first) {
(sum += 1LL * (pr.first - nx + 1) * ny) %= D;
Map[nx] = ny;
}
break;
}
(sum -= 1LL * (nx - it->first) * it->second) %= D;
nx = it->first;
if (it == Map.begin()) {
(sum += 1LL * (pr.first - nx + 1) * ny) %= D;
it->second = ny;
break;
}
Map.erase(it);
}
}
}; // namespace A
struct Candy {
int x, y, c;
};
Candy candy[N];
int main() {
int n, k, L;
cin >> n >> k >> L;
for (int i = 1; i <= n; ++i) {
candy[i].x = read();
candy[i].y = read();
candy[i].c = read() - 1;
}
sort(candy + 1, candy + n + 1,
[&](const Candy &a, const Candy &b) { return a.x < b.x; });
candy[0].x = -1;
int64_t ans = 0;
for (int i = n; i >= 1; --i)
if (candy[i].x > candy[i - 1].x) {
int now_x = candy[i].x;
sort(candy + i, candy + n + 1,
[&](const Candy &a, const Candy &b) { return a.y < b.y; });
vector<vector<pair<int, int>>> q(k);
for (int i = 0; i <= k - 1; ++i)
q[i].push_back(pair<int, int>(now_x, L + 1));
vector<vector<pair<int, int>>> events(n + 1);
for (int j = n; j >= i; --j) {
int nx = candy[j].x + 1, ny = candy[j].y + 1;
auto &nq = q[candy[j].c];
int last_x = L + 1;
while (nq.back().first > nx) {
events[j].push_back(pair<int, int>(last_x - 1, nq.back().second));
last_x = nq.back().first;
nq.pop_back();
}
events[j].push_back(pair<int, int>(last_x - 1, nq.back().second));
if (nq.back().first == nx)
nq.back().second = ny;
else
nq.push_back(pair<int, int>(nx, ny));
}
A::Map.clear();
A::Map[now_x] = 0;
A::sum = 0;
for (int i = 0; i <= k - 1; ++i) {
int last_x = L + 1;
for (auto it = q[i].end(); --it >= q[i].begin();) {
A::upd(pair<int, int>(last_x - 1, it->second));
last_x = it->first;
}
}
int last_y = -1;
for (int j = i; j <= n; ++j) {
(ans += (1LL * (L - now_x + 1) * (L + 1) - A::sum) % D *
(now_x - candy[i - 1].x) % D * (candy[j].y - last_y)) %= D;
last_y = candy[j].y;
for (auto pr : events[j]) A::upd(pr);
}
}
cout << (ans % D + D) % D << endl;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1010;
const long long MOD = 1000000007;
int n, Tx, Ty;
pair<int, int> XY[MAXN];
pair<int, int> AB[MAXN];
int main(void) {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
int x, y;
for (int i = 0; i < n; i++) {
cin >> x >> y;
XY[i] = make_pair(x, y);
}
int a, b;
for (int i = 0; i < n; i++) {
cin >> a >> b;
AB[i] = make_pair(a, b);
}
sort(AB, AB + n);
sort(XY, XY + n);
Tx += AB[0].first + XY[n - 1].first;
Ty += AB[0].second + XY[n - 1].second;
cout << Tx << " " << Ty << "\n";
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int n, MAX;
int a[10];
string command[10];
string ex(int id) {
string _ = "eax";
_[1] += id;
return _;
}
bool dfs(int pos) {
if (pos == MAX) {
if (a[pos] != n) return 0;
cout << pos << '\n';
for (int i = 0; i < pos; i++) cout << command[i] << '\n';
return 1;
}
for (int i = 0; i <= pos; i++)
for (int j = 1; j <= 8; j <<= 1) {
a[pos + 1] = a[i] * j;
if (a[pos + 1] > n) continue;
if (pos + 1 == MAX && a[pos + 1] != n) continue;
command[pos] =
"lea " + ex(pos + 1) + ", [" + (char)('0' + j) + "*" + ex(i) + "]";
if (dfs(pos + 1)) return 1;
}
for (int i = 0; i <= pos; i++)
for (int j = 0; j <= pos; j++)
for (int k = 1; k <= 8; k <<= 1) {
a[pos + 1] = a[i] + a[j] * k;
if (a[pos + 1] > n) continue;
if (pos + 1 == MAX && a[pos + 1] != n) continue;
command[pos] = "lea " + ex(pos + 1) + ", [" + ex(i) + " + " +
(char)('0' + k) + "*" + ex(j) + "]";
if (dfs(pos + 1)) return 1;
}
return 0;
}
int main() {
ios_base::sync_with_stdio(0);
cin >> n;
a[0] = 1;
while (!dfs(0)) MAX++;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long
string s;
int cnt[26];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> s;
for(auto &i : s) cnt[i - 'a'] = 1;
for(int i = 0 ; i < 26 ; i++)
if(cnt[i] == 0) finish(s << char(i + 'a'));
string t = s;
if(next_permutation(t.begin(), t.end())){
bool b = 0;
for(int i = 0 ; i < 26 ; i++){
if(b == 0) cout << t[i];
if(t[i] > s[i]) b = 1;
}
cout << endl;
}
else cout << -1 << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
map<int, string> mp;
mp[0] = "zero";
mp[1] = "one";
mp[2] = "two";
mp[3] = "three";
mp[4] = "four";
mp[5] = "five";
mp[6] = "six";
mp[7] = "seven";
mp[8] = "eight";
mp[9] = "nine";
mp[10] = "ten";
mp[11] = "eleven";
mp[12] = "twelve";
mp[13] = "thirteen";
mp[14] = "fourteen";
mp[15] = "fifteen";
mp[16] = "sixteen";
mp[17] = "seventeen";
mp[18] = "eighteen";
mp[19] = "nineteen";
mp[20] = "twenty";
mp[21] = "twenty-one";
mp[22] = "twenty-two";
mp[23] = "twenty-three";
mp[24] = "twenty-four";
mp[25] = "twenty-five";
mp[26] = "twenty-six";
mp[27] = "twenty-seven";
mp[28] = "twenty-eight";
mp[29] = "twenty-nine";
mp[30] = "thirty";
mp[31] = "thirty-one";
mp[32] = "thirty-two";
mp[33] = "thirty-three";
mp[34] = "thirty-four";
mp[35] = "thirty-five";
mp[36] = "thirty-six";
mp[37] = "thirty-seven";
mp[38] = "thirty-eight";
mp[39] = "thirty-nine";
mp[40] = "forty";
mp[41] = "forty-one";
mp[42] = "forty-two";
mp[43] = "forty-three";
mp[44] = "forty-four";
mp[45] = "forty-five";
mp[46] = "forty-six";
mp[47] = "forty-seven";
mp[48] = "forty-eight";
mp[49] = "forty-nine";
mp[50] = "fifty";
mp[51] = "fifty-one";
mp[52] = "fifty-two";
mp[53] = "fifty-three";
mp[54] = "fifty-four";
mp[55] = "fifty-five";
mp[56] = "fifty-six";
mp[57] = "fifty-seven";
mp[58] = "fifty-eight";
mp[59] = "fifty-nine";
mp[60] = "sixty";
mp[61] = "sixty-one";
mp[62] = "sixty-two";
mp[63] = "sixty-three";
mp[64] = "sixty-four";
mp[65] = "sixty-five";
mp[66] = "sixty-six";
mp[67] = "sixty-seven";
mp[68] = "sixty-eight";
mp[69] = "sixty-nine";
mp[70] = "seventy";
mp[71] = "seventy-one";
mp[72] = "seventy-two";
mp[73] = "seventy-three";
mp[74] = "seventy-four";
mp[75] = "seventy-five";
mp[76] = "seventy-six";
mp[77] = "seventy-seven";
mp[78] = "seventy-eight";
mp[79] = "seventy-nine";
mp[80] = "eighty";
mp[81] = "eighty-one";
mp[82] = "eighty-two";
mp[83] = "eighty-three";
mp[84] = "eighty-four";
mp[85] = "eighty-five";
mp[86] = "eighty-six";
mp[87] = "eighty-seven";
mp[88] = "eighty-eight";
mp[89] = "eighty-nine";
mp[90] = "ninety";
mp[91] = "ninety-one";
mp[92] = "ninety-two";
mp[93] = "ninety-three";
mp[94] = "ninety-four";
mp[95] = "ninety-five";
mp[96] = "ninety-six";
mp[97] = "ninety-seven";
mp[98] = "ninety-eight";
mp[99] = "ninety-nine";
cout << mp[n];
return 0;
}
| 1 |
#include <bits/stdc++.h>
#define _overload(_1,_2,_3,name,...) name
#define _rep(i,n) _range(i,0,n)
#define _range(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)
#define _rrep(i,n) _rrange(i,n,0)
#define _rrange(i,a,b) for(int i=int(a)-1;i>=int(b);--i)
#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)
#define _all(arg) begin(arg),end(arg)
#define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg))
#define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary)
#define clr(a,b) memset((a),(b),sizeof(a))
#define bit(n) (1LL<<(n))
#define popcount(n) (__builtin_popcountll(n))
using namespace std;
template<class T>bool chmax(T &a, const T &b) { return (a < b) ? (a = b, 1) : 0;}
template<class T>bool chmin(T &a, const T &b) { return (b < a) ? (a = b, 1) : 0;}
using ll = long long;
using R = long double;
const R EPS = 1e-9L; // [-1000,1000]->EPS=1e-8 [-10000,10000]->EPS=1e-7
inline int sgn(const R& r) {return (r > EPS) - (r < -EPS);}
inline R sq(R x) {return sqrt(max(x, 0.0L));}
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
// Problem Specific Parameter:
const int limit = 100010;
int a[limit], b[limit];
int num[limit];
int main(void) {
int n;
cin >> n;
using pii = pair<int, int>;
vector<pii> ary;
rep(i, n) cin >> a[i];
rep(i, n) cin >> b[i];
rep(i, n) num[b[i]]++;
rep(i, n) {
if (num[a[i]] == 0) {
puts("No");
rep(j, n) cout << (j ? " " : "") << 1 + 1;
cout << endl;
rep(j, n) cout << (j ? " " : "") << 1 + (j != i);
cout << endl;
return 0;
}
}
puts("Yes");
return 0;
}
| 0 |
#include <iostream>
#include <unordered_set>
using namespace std;
bool simple(int x) {
int i = 2;
while (i * i <= x) {
if (x % i == 0) {
return false;
}
++i;
}
return true;
}
void solution() {
int n, x, result = 0;
cin >> n >> x;
unordered_set<int> st;
for (; n > 0; --n) {
cin >> x;
int i = 2, ans = 0;
if (simple(x)) {
if (x == 1) {
ans = 0;
} else {
ans = x;
}
} else {
while (i <= x) {
int cnt = 0;
while (x > 1 && x % i == 0) {
x /= i;
cnt += 1;
}
ans += i * (cnt % 2);
++i;
}
}
if (st.find(ans) != st.end()) {
++result;
st.clear();
}
st.insert(ans);
}
cout << result + 1 << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tt;
cin >> tt;
while (tt--) {
solution();
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int i, j, m, n, p, k, root[200001], tot, vis[200001], st[200001], ed[200001],
top, fox[200001], k1;
int fa[200001][21], deep[100001], x, y, ans;
struct Node {
int l, r, sum;
} Tree[5000001];
struct Bian {
int ed, before;
} s[1000001];
void add(int x, int y) {
s[++k1].ed = y;
s[k1].before = fox[x];
fox[x] = k1;
}
void update(int ll, int l, int r, int t) {
if (l == r)
Tree[t].sum++;
else {
if (ll <= ((l + r) >> 1)) {
if (!Tree[t].l) Tree[t].l = ++tot;
update(ll, l, ((l + r) >> 1), Tree[t].l);
} else {
if (!Tree[t].r) Tree[t].r = ++tot;
update(ll, ((l + r) >> 1) + 1, r, Tree[t].r);
}
Tree[t].sum = Tree[Tree[t].l].sum + Tree[Tree[t].r].sum;
}
}
void ask(int Q, int ll, int rr, int l, int r, int t) {
if (ll <= l && r <= rr)
ans += Q * Tree[t].sum;
else {
if (ll <= ((l + r) >> 1)) ask(Q, ll, rr, l, ((l + r) >> 1), Tree[t].l);
if (rr > ((l + r) >> 1)) ask(Q, ll, rr, ((l + r) >> 1) + 1, r, Tree[t].r);
}
}
void Get(int x) {
int i;
for (i = 1; i <= 20; i++) fa[x][i] = fa[fa[x][i - 1]][i - 1];
}
int get(int x, int y) {
for (i = 20; i >= 0; i--)
if (y >= (1 << i)) x = fa[x][i], y -= (1 << i);
return x;
}
void dfs(int x) {
vis[x] = 1;
st[x] = ++top;
Get(x);
int i;
for (i = fox[x]; i; i = s[i].before)
if (!vis[s[i].ed]) {
deep[s[i].ed] = deep[x] + 1;
fa[s[i].ed][0] = x;
dfs(s[i].ed);
}
ed[x] = top;
}
int main() {
scanf("%d", &n);
for (i = 0; i <= n; i++) root[i] = ++tot;
for (i = 1; i <= n; i++) {
scanf("%d", &x);
if (x > 0) add(x, i), fa[i][0] = x;
}
for (i = 1; i <= n; i++)
if (!fa[i][0]) dfs(i);
for (i = 1; i <= n; i++) update(st[i], 1, n, root[deep[i]]);
scanf("%d", &m);
for (; m--;) {
scanf("%d%d", &x, &y);
int lca = get(x, y);
if (lca == 0) {
printf("0 ");
continue;
}
ans = 0;
ask(1, st[lca], ed[lca], 1, n, root[deep[x]]);
printf("%d ", ans - 1);
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n, m, i, j, k, em, e[100005 * 2], nx[100005 * 2], ls[100005];
int tot, dfn[100005], sz[100005], g[100005], top[100005], fa[100005],
dep[100005];
struct path {
double k, b, t1, t2;
int i;
path(int _x = 0, int _y = 0, double _t1 = 0, double _t2 = 0, double _v = 0) {
k = _v, t1 = _t1, t2 = _t2, b = _x - t1 * k;
}
};
vector<path> p[100005];
void insert(int x, int y) {
em++;
e[em] = y;
nx[em] = ls[x];
ls[x] = em;
em++;
e[em] = x;
nx[em] = ls[y];
ls[y] = em;
}
void dfs1(int x, int p) {
sz[x] = 1, fa[x] = p;
for (int i = ls[x]; i; i = nx[i])
if (e[i] != p) {
dfs1(e[i], x), sz[x] += sz[e[i]];
g[x] = (!g[x] || sz[g[x]] < sz[e[i]]) ? e[i] : g[x];
}
}
void dfs2(int x, int p) {
dfn[x] = ++tot, dep[x] = dep[p] + 1;
if (g[x]) top[g[x]] = top[x], dfs2(g[x], x);
for (int i = ls[x]; i; i = nx[i])
if (e[i] != p && e[i] != g[x]) top[e[i]] = e[i], dfs2(e[i], x);
}
int getlca(int x, int y) {
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
x = fa[top[x]];
}
return (dep[x] < dep[y]) ? x : y;
}
void addit() {
int x, y, z;
double v, t1, t2;
scanf("%lf%lf%d%d", &t1, &v, &x, &y);
z = getlca(x, y), t2 = t1 + (dep[x] + dep[y] - dep[z] * 2) / v;
while (top[x] != top[y]) {
if (dep[top[x]] > dep[top[y]]) {
p[top[x]].push_back(path(dep[x], dep[top[x]] - 1, t1,
t1 + (dep[x] - dep[top[x]] + 1) / v, -v));
t1 += (dep[x] - dep[top[x]] + 1) / v;
x = fa[top[x]];
} else {
p[top[y]].push_back(path(dep[top[y]] - 1, dep[y],
t2 - (dep[y] - dep[top[y]] + 1) / v, t2, v));
t2 -= (dep[y] - dep[top[y]] + 1) / v;
y = fa[top[y]];
}
}
p[top[x]].push_back(path(dep[x], dep[y], t1, t2, (dep[x] < dep[y]) ? v : -v));
}
struct arr {
int i, t;
double tim;
} q[100005 * 2];
int cmp(arr a, arr b) {
if (abs(a.tim - b.tim) < 0.00000001) return a.t > b.t;
return a.tim < b.tim;
}
double tim;
set<path> s;
set<path>::iterator pre, nex, it;
int operator<(path a, path b) { return a.k * tim + a.b < b.k * tim + b.b; }
int pd(double a, double b) { return a < b || abs(a - b) <= 0.00000001; }
double check(path a, path b) {
if (abs(a.k - b.k) <= 0.00000001) {
if (abs(a.b - b.b) <= 0.00000001)
return pd(max(a.t1, b.t1), min(a.t2, b.t2)) ? max(a.t1, b.t1) : 2e9;
return 2e9;
}
double t = (a.b - b.b) / (b.k - a.k);
if (pd(a.t1, t) && pd(b.t1, t) && pd(t, a.t2) && pd(t, b.t2)) return t;
return 2e9;
}
double ans;
void doit(int t) {
int cnt = 0;
for (i = 0; i < p[t].size(); i++) {
p[t][i].i = i;
cnt++, q[cnt].i = i, q[cnt].t = 1, q[cnt].tim = p[t][i].t1;
cnt++, q[cnt].i = i, q[cnt].t = -1, q[cnt].tim = p[t][i].t2;
}
sort(q + 1, q + 1 + cnt, cmp), s.clear();
tim = 0;
double mi = 2e9;
for (i = 1; i <= cnt; i++) {
tim = q[i].tim, k = q[i].i;
if (pd(mi, tim)) break;
if (q[i].t > 0) {
nex = s.lower_bound(p[t][k]);
if (nex != s.begin())
pre = nex, pre--;
else
pre = s.end();
if (pre != s.end()) mi = min(mi, check(*pre, p[t][k]));
if (nex != s.end()) mi = min(mi, check(*nex, p[t][k]));
s.insert(p[t][k]);
} else {
it = s.find(p[t][k]);
if (it != s.begin())
pre = it, pre--;
else
pre = s.end();
nex = it, nex++;
s.erase(it);
if (pre != s.end() && nex != s.end()) mi = min(mi, check(*pre, *nex));
}
}
ans = min(ans, mi);
}
int main() {
scanf("%d%d", &n, &m);
for (i = 1; i < n; i++) scanf("%d%d", &j, &k), insert(j, k);
dfs1(1, 0);
top[1] = 1, dfs2(1, 0);
for (i = 1; i <= m; i++) addit();
ans = 2e9;
for (int now = 1; now <= n; now++)
if (now == top[now]) doit(now);
if (ans == 2e9)
printf("-1");
else
printf("%.10lf", ans);
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2000005;
int k, d, i, j, x, n, Q, t, go[N][22 + 2];
char s[N / 2], e[N / 2];
int main() {
scanf("%s", &s);
n = strlen(s);
scanf("%d", &Q);
while (Q--) {
scanf("%d%d", &k, &d);
t = n - k + 1;
x = 0;
for (i = 0; i < d; i++)
for (j = i; j < k; j += d) go[1000000 + j][0] = x - 1, x++;
for (i = k; i < n; i++) go[1000000 + i][0] = i - 1;
for (i = -t; i < 0; i++) go[1000000 + i][0] = i - 1;
for (j = 1; j < 22; j++) {
for (i = -t; i < n; i++)
go[1000000 + i][j] = go[1000000 + go[i + 1000000][j - 1]][j - 1];
}
for (i = 0; i < n; i++) {
x = i;
for (j = 0; j < 22; j++)
if ((1 << j) & t) x = go[x + 1000000][j];
e[x + t] = s[i];
}
for (i = 0; i < n; i++) s[i] = e[i];
for (i = 0; i < n; i++) putchar(s[i]);
putchar('\n');
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar();
}
return x * f;
}
const int MN = 1e6 + 6;
int N, fa[MN], f[MN], ans = 1;
int cnt[MN];
void solve(int x) {
f[x] = 1;
for (; fa[x] != 1; x = fa[x]) {
int fl = f[fa[x]];
if (f[fa[x]] < f[x]) {
f[fa[x]] = f[x];
cnt[fa[x]] = 1;
} else if (f[fa[x]] == f[x]) {
if (cnt[fa[x]] == 1)
ans = max(ans, ++f[fa[x]]), ++cnt[fa[x]];
else
cnt[fa[x]] = 1;
}
ans = max(ans, f[x]);
if (fl == f[fa[x]]) break;
}
ans = max(ans, f[x]);
}
int main() {
N = read() + 1;
register int i;
for (i = 2; i <= N; ++i) {
fa[i] = read();
if (fa[i] != 1) solve(i);
printf("%d ", ans);
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long q;
cin >> q;
while (q--) {
long long n, k;
bool ch = 1;
cin >> n >> k;
if (k % 3 == 0) {
long long temp = n % (k + 1);
if (temp % 3 == 0 && temp != k) ch = 0;
} else if (n % 3 == 0) {
ch = 0;
}
if (ch)
cout << "Alice\n";
else
cout << "Bob\n";
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1e9 + 7;
const long double PI = 3.14159265;
long long int powerWithMod(long long int base, long long int exponent,
long long int modulus = LLONG_MAX) {
long long int result = 1;
base %= modulus;
while (exponent > 0) {
if (exponent % 2 == 1) result = (result * base) % modulus;
exponent >>= 1;
base = (base * base) % modulus;
}
return result;
}
long long int modInverse(long long int a, long long int m = MOD) {
return powerWithMod(a, m - 2, m);
}
long long int n, b[212345], c[212345], a[212345], bb[212345][32],
bc[212345][32], cnt[32];
void t() {
cout << "-1\n";
exit(0);
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 0; i < n; i++) cin >> b[i];
for (int i = 0; i < n; i++) {
cin >> c[i];
if (c[i] < b[i]) t();
}
auto s = accumulate(b, b + n, 0LL) + accumulate(c, c + n, 0LL);
if (s % (2 * n)) t();
s /= 2 * n;
for (int i = 0; i < n; i++) {
if ((b[i] + c[i] - s) % n) t();
a[i] = (b[i] + c[i] - s) / n;
}
for (int i = 0; i < n; i++)
for (int j = 0; j < 32; j++)
if (a[i] & (1LL << j)) cnt[j]++;
for (int i = 0; i < n; i++) {
long long int currb = 0, currc = 0;
for (int j = 0; j < 32; j++) {
if (a[i] & (1LL << j))
bb[i][j] = cnt[j], bc[i][j] = n;
else
bb[i][j] = 0, bc[i][j] = cnt[j];
currb += bb[i][j] * (1LL << j);
currc += bc[i][j] * (1LL << j);
}
if (currb != b[i] or currc != c[i]) t();
}
for (int i = 0; i < n; i++) cout << a[i] << " ";
cout << "\n";
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int sqn = 316;
int n, a[100069], pc[100069];
vector<int> al[100069];
bitset<100069> spc;
int main() {
int t, rr, i, j, k, l, w, sz, sm, z = 0;
vector<int> v;
char ch;
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%d %c%d", &k, &ch, &l);
a[i] = 100 - (k * 100 + l);
z += a[i] * 100;
}
for (i = 0; i < n - 1; i++) {
scanf("%d%d", &k, &l);
k++;
l++;
z -= a[k] * a[l];
al[k].push_back(l);
al[l].push_back(k);
}
for (i = 1; i <= n; i++) {
sz = al[i].size();
spc[i] = sz > sqn;
}
for (i = 1; i <= n; i++) {
v.clear();
sz = al[i].size();
for (j = 0; j < sz; j++) {
l = al[i][j];
if (spc[l]) {
if (!spc[i]) {
pc[l] += a[i];
} else {
v.push_back(l);
}
}
}
if (spc[i]) {
al[i] = v;
}
}
scanf("%d", &t);
for (rr = 0; rr < t; rr++) {
scanf("%d%d %c%d", &k, &l, &ch, &w);
k++;
w = 100 - (l * 100 + w);
sm = pc[k];
sz = al[k].size();
for (i = 0; i < sz; i++) {
l = al[k][i];
sm += a[l];
}
z += (w - a[k]) * (100 - sm);
if (!spc[i]) {
for (i = 0; i < sz; i++) {
l = al[k][i];
if (spc[l]) {
pc[l] += w - a[k];
}
}
}
a[k] = w;
printf("%d.", z / 10000);
for (k = z % 10000, i = 0; k < 1000 && i < 3; k *= 10, i++) {
printf("0");
}
printf("%d\n", z % 10000);
}
}
| 4 |
#include <bits/stdc++.h>
int b[100], a[1000006], u[100], ans[1000006];
int bit(int x) {
int c = 1;
while (1) {
if (x % 2 == 1) return b[c];
c++;
x = x / 2;
}
}
int main() {
int i, g, y, c = 0, h, x, r, f = 1;
b[1] = 1;
for (i = 2; i < 20; i++) b[i] = b[i - 1] * 2;
scanf("%d %d", &x, &y);
while (x != 0) {
if (y == 0) {
f = 0;
break;
}
r = bit(y);
if (r > x) {
y--;
} else {
a[c++] = y;
x = x - r;
y--;
}
}
if (f == 0)
printf("-1");
else {
printf("%d\n", c);
for (i = 0; i < c; i++) printf("%d ", a[i]);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
inline void _RD(int &x) { scanf("%d", &x); }
inline void _RD(long long &x) { scanf("%lld", &x); }
inline void _RD(double &x) { scanf("%lf", &x); }
inline void _RD(long double &x) { scanf("%Lf", &x); }
inline void _RD(char &x) { scanf(" %c", &x); }
inline void RD() {}
template <class T, class... U>
inline void RD(T &head, U &...tail) {
_RD(head);
RD(tail...);
}
using PII = pair<int, int>;
using LL = long long;
using VI = vector<int>;
using VLL = vector<LL>;
using VVI = vector<VI>;
const int M = 1e9 + 7;
LL pw(int p) {
LL res = 1, x = 2;
while (p) {
if (p & 1) res = res * x % M;
p >>= 1;
x = x * x % M;
}
return res;
}
void add(LL &x, LL v) {
x = (x + v) % M;
if (x < 0) x += M;
}
int main() {
LL x, y;
RD(x, y);
if (y % x != 0)
printf("0\n");
else {
y /= x;
VLL d;
LL i;
for (i = 1; i * i < y; ++i)
if (y % i == 0) d.push_back(i), d.push_back(y / i);
if (i * i == y) d.push_back(i);
sort((d).begin(), (d).end());
VLL f = VLL((int)((d).size()), 0);
f[0] = 1;
for (int i = 1; i < (int)((d).size()); ++i) {
f[i] = pw(d[i] - 1);
for (int j = 0; j < (i); ++j)
if (d[i] % d[j] == 0) add(f[i], -f[j]);
}
printf("%lld\n", f.back());
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
long long n, k;
long long pr(long long x) {
long long otv = 0;
while (x > 0) {
otv = (long long)otv + x;
x = x / k;
}
return otv;
}
long long bin(long long st, long long fin) {
if (st == fin) return st;
long long m = (long long)(st + fin) / 2;
if (pr(m) < n) {
return bin(m + 1, fin);
} else
return bin(st, m);
}
int main() {
cin >> n >> k;
long long z = (long long)1000000000 * 100000000;
cout << bin(1, z);
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, f, ans = 0;
cin >> n;
for (f = 1; (f * (f + 1)) / 2 <= (n + f) / 3; f++) {
if ((n + f) % 3 == 0) ans++;
}
cout << ans;
return 0;
}
| 3 |
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
#include <stdio.h>
#include <cmath>
#define llint long long
#define inf 1e18
using namespace std;
typedef pair<double, pair<int, int> > edge;
struct UnionFind{
int size;
vector<int> parent;
UnionFind(){}
UnionFind(int size){
this->size = size;
parent.resize(size+1);
init();
}
void init(){
for(int i = 0; i <= size; i++) parent[i] = i;
}
int root(int i){
if(parent[i] == i) return i;
return parent[i] = root(parent[i]);
}
bool same(int i, int j){
return root(i) == root(j);
}
void unite(int i, int j){
int root_i = root(i), root_j = root(j);
if(root_i == root_j) return;
parent[root_i] = root_j;
}
};
llint N;
double x[15], y[15];
llint a[15];
double G[15][15];
vector<edge> E;
UnionFind uf(16);
double mst[1<<15];
double sum[1<<15];
double dp[1<<15];
int main(void)
{
cin >> N;
for(int i = 0; i < N; i++) cin >> x[i] >> y[i] >> a[i];
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
if(i == j) G[i][j] = 0.0;
else{
G[i][j] = sqrt((x[i]-x[j])*(x[i]-x[j]) + (y[i]-y[j])*(y[i]-y[j]));
}
}
}
int S = 1 << N;
for(int s = 0; s < S; s++){
E.clear();
for(int i = 0; i < N; i++){
if((s & (1<<i)) == 0) continue;
for(int j = 0; j < N; j++){
if(i >= j) continue;
if((s & (1<<j)) == 0) continue;
E.push_back(make_pair(G[i][j], make_pair(i, j)));
}
}
sort(E.begin(), E.end());
uf.init();
double sum = 0;
for(int i = 0; i < E.size(); i++){
edge e = E[i];
if(uf.same(e.second.first, e.second.second)) continue;
uf.unite(e.second.first, e.second.second);
sum += e.first;
}
mst[s] = sum;
}
for(int s = 0; s < S; s++){
for(int i = 0; i < N; i++){
if(s & (1<<i)) sum[s] += a[i];
}
}
vector<int> vec;
dp[0] = -inf;
for(int s = 1; s < S; s++){
vec.clear();
for(int i = 0; i < N; i++) if(s & (1<<i)) vec.push_back(i);
int n = vec.size();
dp[s] = (sum[s] - mst[s]) / n;
for(int i = 0; i < (1<<n); i++){
int bit = 0, cbit = 0;
for(int j = 0; j < n; j++){
if(i & (1<<j)) bit |= (1<<vec[j]);
else cbit |= (1<<vec[j]);
}
dp[s] = max(dp[s], min(dp[bit], dp[cbit]));
}
}
//for(int i = 1; i < S; i++) cout << dp[i] << " "; cout << endl;
printf("%.11f\n", dp[S-1]);
return 0;
}
| 0 |
#include <iostream>
#include <cstring>
using namespace std;
int t[9],d;
//âèÉêvµ½@wÅ é©Ì»è
bool isModuicSquare(void){
int rem = (t[0] + t[1] + t[2]) % t[9];
return ((t[3] + t[4] + t[5]) % t[9] == rem &&
(t[6] + t[7] + t[8]) % t[9] == rem &&
(t[0] + t[3] + t[6]) % t[9] == rem &&
(t[1] + t[4] + t[7]) % t[9] == rem &&
(t[2] + t[5] + t[8]) % t[9] == rem &&
(t[0] + t[4] + t[8]) % t[9] == rem &&
(t[2] + t[4] + t[6]) % t[9] == rem);
}
int solve(int idx,bool *used){
if(idx == 10) return isModuicSquare() ? 1 : 0;
if(t[idx] != 0) return solve(idx+1,used);
int res = 0;
for(int i=1;i<=10;i++){
if(!used[i]){
t[idx] = i;
used[i] = true;
res += solve(idx+1,used);
used[i] = false;
t[idx] = 0;
}
}
return res;
}
int main(void){
while(1){
bool used[11];
memset(used,0,sizeof(used));
for(int i=0;i<10;i++){
cin>>t[i];
if(t[i] > 0) used[t[i]] = true;
}
if(t[0] == -1) break;
cout<<solve(0,used)<<endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long int n;
long long int ppow(long long int i, long long int j) {
if (i >= 998244353) i %= 998244353;
long long int res = 1LL;
while (j) {
if (j & 1LL) {
res *= i;
if (res >= 998244353) res %= 998244353;
}
i *= i;
if (i >= 998244353) i %= 998244353;
j >>= 1LL;
}
return res;
}
long long int k[1000002];
long long int r[1000002];
long long int C(int a, int b) {
long long int up = k[a];
long long int dw = r[a - b] * r[b];
if (dw >= 998244353) dw %= 998244353;
up *= dw;
if (up >= 998244353) up %= 998244353;
return up;
}
long long int p3[1000002];
int main() {
scanf("%lld", &n);
p3[0] = 1;
for (int i = 1; i < 1000002; i++) {
p3[i] = p3[i - 1];
p3[i] *= 3LL;
if (p3[i] >= 998244353) p3[i] %= 998244353;
}
k[0] = 1LL;
for (int i = 1; i < 1000002; i++) {
k[i] = k[i - 1];
k[i] *= (long long int)(i);
if (k[i] >= 998244353) k[i] %= 998244353;
}
r[1000002 - 1] = ppow(k[1000002 - 1], 998244353 - 2);
for (int i = 1000002 - 2; i >= 0; i--) {
r[i] = r[i + 1] * (long long int)(i + 1);
if (r[i] >= 998244353) r[i] %= 998244353;
}
long long int ans = 0;
for (int i = 1; i <= n; i++) {
long long int way = C(n, i) * ppow(3, i);
if (way >= 998244353) way %= 998244353;
way *= ppow(p3[n], n - i);
if (way >= 998244353) way %= 998244353;
if (i & 1) {
ans += way;
} else {
ans += 998244353 - way;
}
if (ans >= 998244353) ans %= 998244353;
}
for (int i = 1; i <= n; i++) {
{
long long int way = 3LL * C(n, i);
if (way >= 998244353) way %= 998244353;
long long int dif_col = ppow(p3[n - i] + 998244353 - 1LL, n);
way *= dif_col;
if (way >= 998244353) way %= 998244353;
if (i & 1) {
ans += way;
} else {
ans += 998244353 - way;
}
if (ans >= 998244353) ans %= 998244353;
}
{
long long int way = ((p3[i] + 998244353 - 3LL) % 998244353) * C(n, i);
if (way >= 998244353) way %= 998244353;
way *= ppow(p3[n - i], n);
if (way >= 998244353) way %= 998244353;
if (i & 1) {
ans += way;
} else {
ans += 998244353 - way;
}
if (ans >= 998244353) ans %= 998244353;
}
}
printf("%lld\n", ans);
return 0;
}
| 3 |
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;
typedef long long ll;
const int N=100010;
bool w[N];
long long sz[N];
int fa[N],a[N],b[N],Mx[N],id[N];
vector <int> q[N];
inline int gi() {
int x=0,o=1;
char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') o=-1,ch=getchar();
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
return x*o;
}
inline bool cmp(const int &x,const int &y) {return a[x]<a[y];}
inline int find(int x) {return fa[x]==x?x:fa[x]=find(fa[x]);}
int main() {
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++) {
a[i]=gi(),b[i]=gi(),id[i]=i,fa[i]=i;
Mx[i]=a[i]=max(0,a[i]-b[i]),sz[i]=b[i];
}
for(int i=1;i<=m;i++) {
int x=gi(),y=gi();
q[x].push_back(y),q[y].push_back(x);
}
sort(id+1,id+1+n,cmp);
for(int i=1;i<=n;i++) {
int x=id[i];w[x]=1;
for(int j=0;j<q[x].size();j++)
if(w[q[x][j]]) {
int y=find(q[x][j]);
if(x!=y) {
sz[x]+=sz[y],fa[y]=x;
Mx[x]=min(1LL*Mx[x],Mx[y]+max(0LL,a[x]-sz[y]-Mx[y]));
}
}
if(i==n) cout<<Mx[x]+sz[x];
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 100000;
int arr[N];
int main(int argc, char **argv) {
int t, n, k, z;
int ans, mx, sum, to;
ios::sync_with_stdio(false);
cin.tie(0);
cin >> t;
for (int i = 0; i < t; i++) {
cin >> n >> k >> z;
for (int j = 0; j < n; j++) cin >> arr[j];
ans = -1;
for (int j = 0; j <= z; j++) {
mx = -1;
sum = 0;
to = k - 2 * j;
if (to < 0) continue;
for (int l = 0; l <= to; l++) {
if (l < n - 1) mx = max(mx, arr[l] + arr[l + 1]);
sum += arr[l];
}
ans = max(ans, sum + mx * j);
}
cout << ans << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
pair<int, int> edge[100001];
int n, m, k, x, y;
bool centre[1001];
int r[1001], parent[1001], set_size[1001];
void init(int n) {
for (int i = 0; i < (int)(n); i++) {
r[i] = 0;
parent[i] = i;
set_size[i] = 1;
centre[i] = false;
}
}
int findset(int i) {
return parent[i] == i ? i : parent[i] = findset(parent[i]);
}
void unionset(int a, int b) {
x = findset(a), y = findset(b);
if (x == y) return;
if (r[x] != r[y])
if (r[x] > r[y])
set_size[x] += set_size[y], parent[y] = x;
else
set_size[y] += set_size[x], parent[x] = y;
else
r[x]++, set_size[x] += set_size[y], parent[y] = x;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> k;
init(n);
for (int i = 0; i < (int)(k); i++)
cin >> x, --x, centre[x] = true, r[x] = 5000;
for (int i = 0; i < (int)(m); i++)
cin >> x >> y, edge[i] = make_pair(x - 1, y - 1);
for (int i = 0; i < (int)(m); i++) unionset(edge[i].first, edge[i].second);
long long int ans = 0;
int m_size = -1, m_comp = -1;
for (int i = 0; i < (int)(n); i++)
if (findset(i) == i and centre[i] and set_size[i] > m_size)
m_size = set_size[i], m_comp = i;
long long int temp = 0, num = 0;
for (int i = 0; i < (int)(n); i++) {
if (i == m_comp or (!centre[i] and findset(i) == i)) {
num += set_size[i];
} else if (centre[i]) {
ans += (set_size[i] * (set_size[i] - 1)) / 2LL;
}
}
ans += (num * (num - 1) / 2LL);
ans -= m;
cout << ans, cout << "\n";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
int p[n];
for (int i = 0; i < n; i++) {
cin >> p[i];
}
vector<int> v[n];
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
v[x - 1].push_back(y - 1);
v[y - 1].push_back(x - 1);
}
int vis[n];
for (int i = 0; i < n; i++) {
vis[i] = 0;
}
for (int i = 0; i < n; i++) {
if (vis[i] == 0) {
queue<int> q;
q.push(i);
vis[i] = 1;
priority_queue<int> q1;
priority_queue<int, vector<int>, greater<int> > q2;
while (!q.empty()) {
int x = q.front();
q.pop();
q1.push(p[x]);
q2.push(x);
for (int j = 0; j < v[x].size(); j++) {
if (vis[v[x][j]] == 0) {
q.push(v[x][j]);
vis[v[x][j]] = 1;
}
}
}
while (!q1.empty()) {
p[q2.top()] = q1.top();
q1.pop();
q2.pop();
}
}
}
for (int i = 0; i < n; i++) {
cout << p[i];
if (i != n - 1) {
cout << " ";
}
}
cout << endl;
return 0;
}
| 4 |
#include <bits/stdc++.h>
const long long INF = 1e9;
const long long lINF = 1e18;
using namespace std;
int n;
long long xx1, yy1, xx2, yy2;
int main() {
cin >> xx1 >> yy1 >> xx2 >> yy2;
cin >> n;
int ans = 0;
for (int i = 0; i < n; i++) {
long long a, b, c;
cin >> a >> b >> c;
if (a * xx1 + b * yy1 + c < 0 && a * xx2 + b * yy2 + c > 0) ans++;
if (a * xx1 + b * yy1 + c > 0 && a * xx2 + b * yy2 + c < 0) ans++;
}
cout << ans;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long m, h1, h2, a1, a2, x1, x2, y1, y2;
cin >> m >> h1 >> a1 >> x1 >> y1 >> h2 >> a2 >> x2 >> y2;
bool ok = 0;
long long t1 = 0;
long long p1 = 0;
long long t2 = 0;
long long p2 = 0;
for (int i = 0; i < 1000000; i++) {
h1 *= x1;
h1 += y1;
h1 %= m;
if (h1 == a1) {
t1 = i + 1;
ok = 1;
break;
}
}
if (!ok) {
cout << -1 << endl;
return 0;
}
ok = 0;
for (int i = 0; i < 1000000; i++) {
h1 *= x1;
h1 += y1;
h1 %= m;
if (h1 == a1) {
p1 = i + 1;
ok = 1;
break;
}
}
ok = 0;
for (int i = 0; i < 1000000; i++) {
h2 *= x2;
h2 += y2;
h2 %= m;
if (h2 == a2) {
t2 = i + 1;
ok = 1;
break;
}
}
if (!ok) {
cout << -1 << endl;
return 0;
}
ok = 0;
for (int i = 0; i < 1000000; i++) {
h2 *= x2;
h2 += y2;
h2 %= m;
if (h2 == a2) {
p2 = i + 1;
ok = 1;
break;
}
}
ok = 0;
for (int i = 0; i < 2000000; i++) {
if (t1 == t2) {
ok = 1;
break;
}
if (t1 < t2) {
t1 += p1;
continue;
}
t2 += p2;
}
if (!ok) {
cout << -1 << endl;
return 0;
}
cout << t1 << endl;
cin >> t1;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long n, q, timer, ai, bi, ci;
vector<vector<long long> > data;
vector<vector<long long> > money;
vector<long long> tin, tout, first, second, line, rmq, values, pushs, now;
vector<pair<long long, long long> > edges;
void dfs(long long vertex, long long dist) {
tin[vertex] = timer;
timer++;
line.push_back(dist + values[vertex]);
first[vertex] = line.size() - 1;
for (long long i = 0; i < data[vertex].size(); i++) {
long long to = data[vertex][i];
dfs(to, dist + money[vertex][i]);
}
tout[vertex] = timer;
timer++;
line.push_back(dist + values[vertex]);
second[vertex] = line.size() - 1;
}
void create(long long i, long long l, long long r) {
if (r - l == 1) {
rmq[i] = line[l];
return;
}
long long mid = (l + r) / 2;
create(2 * i + 1, l, mid);
create(2 * i + 2, mid, r);
rmq[i] = min(rmq[2 * i + 1], rmq[2 * i + 2]);
}
void push(long long i, long long l, long long r) {
if (l + 1 >= r) return;
long long p = pushs[i];
rmq[2 * i + 1] += p;
rmq[2 * i + 2] += p;
pushs[2 * i + 1] += p;
pushs[2 * i + 2] += p;
pushs[i] = 0;
}
long long get(long long i, long long l, long long r, long long ind) {
push(i, l, r);
if (r - l == 1) {
return rmq[i];
}
long long mid = (l + r) / 2;
if (ind < mid)
return get(2 * i + 1, l, mid, ind);
else
return get(2 * i + 2, mid, r, ind);
}
long long get_big(long long i, long long l, long long r, long long l1,
long long r1) {
push(i, l, r);
if (l1 >= r1) return 1e12;
if (l1 == l && r1 == r) {
return rmq[i];
}
long long mid = (l + r) / 2;
return min(get_big(2 * i + 1, l, mid, l1, min(r1, mid)),
get_big(2 * i + 2, mid, r, max(l1, mid), r1));
}
void change(long long i, long long l, long long r, long long l1, long long r1,
long long vv) {
push(i, l, r);
if (l1 >= r1) return;
if (l == l1 && r == r1) {
rmq[i] += vv;
pushs[i] += vv;
return;
}
long long mid = (l + r) / 2;
change(2 * i + 1, l, mid, l1, min(r1, mid), vv);
change(2 * i + 2, mid, r, max(l1, mid), r1, vv);
rmq[i] = min(rmq[2 * i + 1], rmq[2 * i + 2]);
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> q;
for (long long i = 0; i < n; i++) {
vector<long long> h1, h2;
data.push_back(h1);
money.push_back(h2);
tin.push_back(0);
tout.push_back(0);
first.push_back(0);
second.push_back(0);
values.push_back(0);
}
for (long long i = 0; i < n - 1; i++) {
cin >> ai >> bi >> ci;
edges.push_back(make_pair(ai, bi));
now.push_back(ci);
data[ai - 1].push_back(bi - 1);
money[ai - 1].push_back(ci);
}
for (long long i = 0; i < n - 1; i++) {
cin >> ai >> bi >> ci;
edges.push_back(make_pair(ai, bi));
now.push_back(ci);
values[ai - 1] = ci;
}
timer = 0;
dfs(0, 0);
long long sz = 2 * n;
for (long long i = 0; i < 4 * sz; i++) {
rmq.push_back(0);
pushs.push_back(0);
}
create(0, 0, sz);
for (long long i = 0; i < q; i++) {
long long t;
cin >> t;
if (t == 2) {
cin >> ai >> bi;
ai--;
bi--;
if (tin[ai] <= tin[bi] && tout[ai] >= tout[bi]) {
cout << get(0, 0, sz, first[bi]) - get(0, 0, sz, first[ai]) -
values[bi] + values[ai]
<< endl;
} else {
long long r1 = get_big(0, 0, sz, first[ai], second[ai] + 1);
r1 -= get(0, 0, sz, first[ai]);
r1 += values[ai];
r1 += get(0, 0, sz, first[bi]);
r1 -= values[bi];
cout << r1 << endl;
}
} else {
cin >> ai >> ci;
if (ai < n) {
long long v = edges[ai - 1].second - 1;
long long diff = ci - now[ai - 1];
change(0, 0, sz, first[v], second[v] + 1, diff);
now[ai - 1] = ci;
} else {
long long v = edges[ai - 1].first - 1;
long long diff = ci - values[v];
values[v] = ci;
change(0, 0, sz, first[v], first[v] + 1, diff);
change(0, 0, sz, second[v], second[v] + 1, diff);
}
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int t;
cin >> t;
while (t--) {
string s;
cin >> s;
long long int cnt1 = 0;
vector<long long int> ans;
long long int cnt = 0;
if (s[0] == '1') cnt++;
for (long long int i = 1; i < s.length(); i++) {
if (s[i] == '1') cnt++;
if (s[i] == '0' && s[i - 1] == '1') {
ans.push_back(cnt);
cnt = 0;
}
}
if (s[s.length() - 1] == '1') {
ans.push_back(cnt);
}
sort(ans.begin(), ans.end(), greater<int>());
for (long long int i = 0; i < ans.size(); i++) {
if (i % 2 == 0) cnt1 += ans[i];
}
cout << cnt1 << '\n';
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main()
{
string a, b, c;
cin>>a>>b>>c;
if(a[a.size()-1] == b[0] && b[b.size()-1] == c[0])
puts("YES");
else
puts("NO");
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void read1D(int A[], int n) {
for (int i = 0; i < n; i++) cin >> A[i];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
int n;
cin >> n;
map<string, int> m;
while (n--) {
string s;
cin >> s;
if (m.find(s) != m.end())
cout << "YES\n";
else {
cout << "NO\n";
m[s] = 1;
}
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define PI 3.1415926535897932384626
#define all(x) x.begin(),x.end()
#define endl '\n'
#define MOD 1000000007
typedef vector< ll > vl;
typedef pair< ll , ll > pl;
typedef unordered_map< ll, ll > uml;
typedef vector< pl > vpl;
typedef map< ll, ll > ml;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll t;
cin>>t;
while(t--){
ll a,b,d;
cin>>a>>b>>d;
if(a>b){
swap(a,b);
}
if(a*(d+1)>=b){
cout<<"YES"<<endl;
}else{
cout<<"NO"<<endl;
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
int n, Mn, ts, Fr[N], To[N], P[N], M[N];
vector<int> Lf;
set<pair<int, int> > S, Adj[N][2];
map<pair<int, int>, bool> MP;
int Find(int v) {
if (P[v] == v) return (v);
return (P[v] = Find(P[v]));
}
void Merge(int v, int u) {
v = Find(v);
u = Find(u);
if (v == u) return;
if (Adj[v][0].size() + Adj[v][1].size() < Adj[u][0].size() + Adj[u][1].size())
swap(v, u);
P[u] = v;
for (auto X : Adj[u][0]) Adj[v][0].insert(X);
for (auto X : Adj[u][1]) Adj[v][1].insert(X);
Adj[u][0].clear();
Adj[u][1].clear();
}
pair<int, int> Get(int v, int w) {
if (!Adj[v][w].size()) return (make_pair(-1, -1));
pair<int, int> ret = *Adj[v][w].begin();
Adj[v][w].erase(Adj[v][w].begin());
return (ret);
}
void Del(int v, int u, int w) {
int pv = Find(v), pu = Find(u);
Adj[pv][w].erase({v, u});
Adj[pu][w].erase({u, v});
}
int main() {
scanf("%d", &n);
Mn = n - 1;
for (int i = 1, v, u; i < n; i++)
scanf("%d%d", &v, &u), Adj[v][0].insert({v, u}), Adj[u][0].insert({u, v}),
MP[{v, u}] = MP[{u, v}] = 1;
for (int i = 1; i < n; i++)
scanf("%d%d", &Fr[i], &To[i]), Adj[Fr[i]][1].insert({Fr[i], To[i]}),
Adj[To[i]][1].insert({To[i], Fr[i]});
for (int i = 1; i <= n; i++) P[i] = i;
for (int i = 1; i < n; i++)
if (MP[{Fr[i], To[i]}]) {
Del(To[i], Fr[i], 0);
Del(To[i], Fr[i], 1);
Merge(Fr[i], To[i]);
Mn--;
}
for (int i = 1; i <= n; i++)
if (Find(i) == i) S.insert({(int)Adj[i][0].size(), i});
printf("%d\n", Mn);
while (S.size()) {
int lv = (*S.begin()).second;
S.erase(S.begin());
pair<int, int> to = Get(lv, 1), pr = Get(lv, 0);
if (to.first == -1 || pr.first == -1) continue;
printf("%d %d %d %d\n", pr.first, pr.second, to.first, to.second);
S.erase({(int)Adj[Find(pr.second)][0].size(), Find(pr.second)});
S.erase({(int)Adj[Find(to.second)][0].size(), Find(to.second)});
Adj[Find(to.second)][1].erase({to.second, to.first});
Adj[Find(pr.second)][0].erase({pr.second, pr.first});
Merge(to.first, to.second);
S.insert({(int)Adj[Find(pr.second)][0].size(), Find(pr.second)});
if (Find(pr.second) != Find(to.second))
S.insert({(int)Adj[Find(to.second)][0].size(), Find(to.second)});
}
return (0);
}
| 5 |
#include<cstdio>
typedef long long ll;
const int mod=998244353;
int N,A,B;
ll K,fac[300010],ifac[300010];
ll inv(int a,int p=mod){return a==1?1:(1+p*(a-inv(p%a,a)))/a%p;}
ll C(int n,int m){return fac[n]*ifac[m]%mod*ifac[n-m]%mod;}
int main(){
scanf("%d%d%d%lld",&N,&A,&B,&K);
for(int i=*fac=1;i<=N;i++)fac[i]=fac[i-1]*i%mod;
ifac[N]=inv(fac[N]);
for(int i=N;i;i--)ifac[i-1]=ifac[i]*i%mod;
int s=0;
for(int i=0;i<=N;i++){
ll j=K-1ll*A*i;
if(j%B==0){
j/=B;
if(j>=0&&j<=N){
s=(s+C(N,i)*C(N,j))%mod;
}
}
}
printf("%d\n",s);
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
long long n, m;
char x[1000005], y[1000005];
int xlen, ylen;
long long cnt[26];
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
int main() {
cin >> n >> m;
scanf("%s%s", x, y);
int xlen = strlen(x), ylen = strlen(y);
int g = gcd(xlen, ylen);
long long total = 1LL * n * xlen;
long long mul = 1LL * n / (ylen / g);
for (int k = 0; k < g; k++) {
for (int i = 0; i < 26; i++) cnt[i] = 0;
for (int i = k; i < ylen; i += g) cnt[y[i] - 'a']++;
for (int i = k; i < xlen; i += g) total -= mul * cnt[x[i] - 'a'];
}
cout << total << endl;
}
| 2 |
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(){
int i = 0;
string w,t;
cin >> w;
transform(w.begin(), w.end(), w.begin(), ::tolower);
while (cin >> t, t != "END_OF_TEXT"){
transform(t.begin(), t.end(), t.begin(), ::tolower);
if (w == t) i++;
}
cout << i << endl;
return(0);
}
| 0 |
#include <bits/stdc++.h>
const int N = (1 << 16) + 1;
int leaf[N], degree[N], s[N];
int main(int argc, char *argv[]) {
int n, i, cnt, index, adj, sum = 0;
scanf("%d", &n);
cnt = 0;
for (i = 0; i < n; ++i) {
scanf("%d%d", °ree[i], &s[i]);
sum += degree[i];
if (1 == degree[i]) {
leaf[cnt++] = i;
}
}
printf("%d\n", sum / 2);
while (cnt > 1) {
index = leaf[--cnt];
if (degree[index] != 1) {
continue;
}
adj = s[index];
printf("%d %d\n", index, adj);
--degree[adj];
s[adj] ^= index;
if (1 == degree[adj]) {
leaf[cnt++] = adj;
}
}
return EXIT_SUCCESS;
}
| 3 |
#include<cstdio>
#include<algorithm>
#define M 0
using namespace std;
int n,q;
int c[300100],x;
int main(){
scanf("%d%d",&n,&q);
int lim = 0;
for(int i=0;i<n;i++){ scanf("%d",&c[i]); lim = max(lim,c[i]); }
sort(c,c+n);
int m[M+1] = {0};
for(int i=1;i<=M;i++){
for(int j=0;j<n;j++)m[i] = max(m[i],c[j]%i);
}
for(int i=0;i<q;i++){
scanf("%d",&x);
if(x<=M)printf("%d\n",m[x]);
else{
int res = 0;
for(int j=1;(j-1)*x<=lim;j++){
int tmp = lower_bound(c,c+n,j*x) - c;
if(tmp)res = max(res,c[tmp-1]%x);
}
printf("%d\n",res);
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e6 + 7;
const long long mod = 1e9 + 7;
long long n, k;
long long f[maxn];
void init() {
f[0] = 1;
for (int i = 1; i < maxn; ++i) f[i] = (f[i - 1] * i) % mod;
}
long long pow_(long long a, long long n) {
long long res = 1LL;
while (n) {
if (n & 1) res = (res * a) % mod;
a = (a * a) % mod;
n >>= 1;
}
return res;
}
long long C(long long n, long long m) {
return ((f[n] * pow_(f[m], mod - 2)) % mod * pow_(f[n - m], mod - 2) % mod);
}
int main() {
init();
int T;
scanf("%d", &T);
while (T--) {
scanf("%lld%lld", &n, &k);
long long ans = pow_(2LL, k - 1);
for (long long i = 2; i * i <= n; ++i) {
if (n % i == 0) {
long long cnt = 0;
while (n % i == 0) {
cnt++;
n /= i;
}
ans = (ans * C(cnt + k - 1, cnt)) % mod;
}
}
if (n > 1) ans = (ans * k) % mod;
printf("%lld\n", ans);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n;
int a[110], b[110], c[110], d[110];
vector<int> stdprimes;
vector<int> sepra(int x) {
vector<int> res = vector<int>();
for (int i = 2; i * i <= x; i++) {
if (x % i == 0) {
res.push_back(i);
while (x % i == 0) x /= i;
}
}
if (x != 1) res.push_back(x);
return res;
}
struct record {
vector<int> stdpows;
vector<int> extrapr;
vector<int> extrapo;
record() {
stdpows.clear();
extrapr.clear();
extrapo.clear();
}
record(int x) {
for (int i = 0; i < stdprimes.size(); i++) {
stdpows.push_back(0);
while (x % stdprimes[i] == 0) {
x /= stdprimes[i];
stdpows.back()++;
}
}
for (int i = 2; i * i <= x; i++) {
if (x % i == 0) {
extrapr.push_back(i);
extrapo.push_back(0);
while (x % i == 0) {
x /= i;
extrapo.back()++;
}
}
}
if (x != 1) {
extrapr.push_back(x);
extrapo.push_back(1);
}
}
};
inline long long quickpow(long long base, long long ind) {
long long ans = 1;
while (ind > 0) {
if (ind & 1) ans = ans * base % 1000000007;
ind >>= 1;
base = base * base % 1000000007;
}
return ans;
}
inline void mul(long long &a, long long b) { a = a * b % 1000000007; }
inline long long extgcd(long long a, long long b, long long &x, long long &y) {
if (!b) {
x = 1, y = 0;
return a;
}
long long g = extgcd(b, a % b, y, x);
y = y - a / b * x;
return g;
}
void combine(long long &a1, long long &m1, int a2, int m2) {
long long c = a2 - a1;
long long a = m1, b = m2;
long long x, y, g;
g = extgcd(a, b, x, y);
if (c % g != 0) {
cout << -1;
exit(0);
}
long long b1 = b / g;
long long x1 = ((x + b1) * c / g);
x1 = (x1 % b1 + b1) % b1;
long long y1 = ((c - a * x1) / b);
a1 = a1 + m1 * x1;
m1 = m1 * m2 / g;
}
void trysingle(int id, int pos) {
stdprimes.clear();
record fst(a[id]);
stdprimes = fst.extrapr;
record ratio(b[id]);
for (int i = 0; i < ratio.extrapr.size(); i++)
stdprimes.push_back(ratio.extrapr[i]);
fst = record(a[id]);
ratio = record(b[id]);
vector<int> cnt;
for (int i = 0; i < fst.stdpows.size(); i++) {
cnt.push_back(fst.stdpows[i]);
cnt.back() += ratio.stdpows[i] * pos;
}
for (int i = 0; i < n; i++) {
record curfst(a[i]);
record currat(b[i]);
if (!curfst.extrapr.empty()) {
cout << -1;
exit(0);
}
int curneed = -1;
for (int j = 0; j < stdprimes.size(); j++) {
if (curfst.stdpows[j] > cnt[j]) {
cout << -1;
exit(0);
}
int need = cnt[j] - curfst.stdpows[j];
if (!currat.stdpows[j]) {
if (need) {
cout << -1;
exit(0);
}
continue;
} else if (need % currat.stdpows[j] != 0) {
cout << -1;
exit(0);
}
need /= currat.stdpows[j];
if (curneed == -1)
curneed = need;
else if (curneed != need) {
cout << -1;
exit(0);
}
}
if (curneed > 0 && !currat.extrapr.empty()) {
cout << -1;
exit(0);
}
}
long long ans = a[id];
mul(ans, quickpow(b[id], pos));
cout << ans;
exit(0);
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i] >> b[i];
for (int i = 0; i < n; i++)
if (b[i] == 1) trysingle(i, 0);
if (n == 1) {
cout << a[0];
return 0;
}
stdprimes = sepra(b[0]);
record fst(a[0]);
record ratio(b[0]);
c[0] = fst.stdpows[0];
d[0] = ratio.stdpows[0];
for (int i = 1; i < n; i++) {
record curfst(a[i]);
record currat(b[i]);
if (!currat.extrapr.empty()) {
int needPow = 0, p = currat.extrapr[0], pw = currat.extrapo[0];
for (int j = 0; j < fst.extrapr.size(); j++) {
if (fst.extrapr[j] == p) {
needPow += fst.extrapo[j];
break;
}
}
for (int j = 0; j < curfst.extrapr.size(); j++) {
if (curfst.extrapr[j] == p) {
needPow -= curfst.extrapo[j];
break;
}
}
if (needPow < 0 || needPow % pw != 0) {
cout << -1;
return 0;
}
trysingle(i, needPow / pw);
}
c[i] = curfst.stdpows[0];
d[i] = currat.stdpows[0];
int fstpos = 0, errpos = -1;
while (fstpos < stdprimes.size() && currat.stdpows[fstpos] == 0) fstpos++;
int pow0 = ratio.stdpows[fstpos];
int powi = currat.stdpows[fstpos];
for (int j = 0; j < stdprimes.size(); j++) {
if (ratio.stdpows[j] * powi != currat.stdpows[j] * pow0) {
errpos = j;
break;
}
}
if (~errpos) {
int s1 = fst.stdpows[fstpos], s2 = fst.stdpows[errpos];
int t1 = curfst.stdpows[fstpos], t2 = curfst.stdpows[errpos];
int u1 = ratio.stdpows[fstpos], u2 = ratio.stdpows[errpos];
int v1 = currat.stdpows[fstpos], v2 = currat.stdpows[errpos];
int mulx = u1 * v2 - u2 * v1;
int eqa = t1 * v2 - t2 * v1 - s1 * v2 + s2 * v1;
if (mulx == 0 || (abs(eqa) % abs(mulx) != 0) || eqa / mulx < 0) {
cout << -1;
return 0;
}
trysingle(0, eqa / mulx);
}
if (fst.extrapr.size() != curfst.extrapr.size()) {
cout << -1;
return 0;
}
for (int j = 0; j < fst.extrapr.size(); j++)
if (fst.extrapr[j] != curfst.extrapr[j] ||
fst.extrapo[j] != curfst.extrapo[j]) {
cout << -1;
return 0;
}
int dif = curfst.stdpows[0] - fst.stdpows[0];
for (int j = 1; j < stdprimes.size(); j++) {
int curdif = curfst.stdpows[j] - fst.stdpows[j];
if (dif * ratio.stdpows[j] != curdif * ratio.stdpows[0]) {
cout << -1;
return 0;
}
}
}
long long ansC = c[0], ansD = d[0];
for (int i = 1; i < n; i++) {
combine(ansC, ansD, c[i], d[i]);
}
for (int i = 0; i < n; i++) {
if (ansC < c[i]) {
(ansC += ((c[i] - ansC + ansD - 1) / ansD) * ansD);
}
}
long long ans = a[0];
ansC /= ratio.stdpows[0];
mul(ans, quickpow(b[0], ansC));
cout << ans;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, d;
cin >> n >> m >> d;
cout.precision(18);
cout << (d ? 2. : 1.)*(m - 1)*(n - d) / n / n << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string st, st1;
int n;
scanf("%d", &n);
if (n % 2 == 0) {
for (int i = 0; i < n; i += 2) {
if (i % 4 == 0) {
st += "aa";
} else {
st += "bb";
}
}
cout << st << endl;
st1 = "c";
for (int i = 1; i < n - 1; i += 2) {
if (i % 4 == 1) {
st1 += "dd";
} else {
st1 += "ee";
}
}
st1 += 'f';
cout << st1 << endl;
for (int i = 1; i < n - 1; i += 2) {
if (i % 4 == 1) {
st1[i] = 'g';
st1[i + 1] = 'g';
} else {
st1[i] = 'h';
st1[i + 1] = 'h';
}
}
cout << st1 << endl;
cout << st << endl;
} else {
st = 'a';
for (int i = 1; i < n; i += 2) {
if (i % 4 == 1) {
st += "bb";
} else {
st += "cc";
}
}
cout << st << endl;
st = "a";
for (int i = 1; i < n; i += 2) {
if (i % 4 == 1) {
st += "dd";
} else {
st += "ee";
}
}
cout << st << endl;
st = "";
for (int i = 0; i < n - 1; i += 2) {
if (i % 4 == 0) {
st += "ff";
} else {
st += "gg";
}
}
st += 'h';
cout << st << endl;
st = "";
for (int i = 0; i < n - 1; i += 2) {
if (i % 4 == 0) {
st += "aa";
} else {
st += "bb";
}
}
st += 'h';
cout << st << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
#define REP(i,n) for(int i=0; i<(int)(n); ++i)
using namespace std;
typedef long long LL;
typedef complex<double> P;
const double EPS = 1e-8;
// 誤差を加味した符号判定
int sign(double x){ return x > EPS ? 1 : x < -EPS ? -1 : 0; }
// 内積・外積
double dot(P a, P b){return real(conj(a) * b);}
double cross(P a, P b){return imag(conj(a) * b);}
// OAとOBのなす符号付き角度 [-pi, pi]
// example : (1, 0), (0, 1) -> pi/2
double angle(P a, P b){return arg(conj(a) * b);}
// aをc中心にb[rad]回転
P rotate(P a, double b, P c = P()){return (a - c) * polar(1.0, b) + c;}
int ccw(P a, P b, P c) {
b -= a; c -= a;
if (cross(b, c) > +EPS) return +1; // 反時計回り
if (cross(b, c) < -EPS) return -1; // 時計回り
if (dot(b, c) < -EPS) return +2; // c--a--b の順番で一直線上
if (norm(b) + EPS < norm(c)) return -2; // a--b--c の順番で一直線上
return 0; // 点が線分ab上にある
}
enum{ OUT, ON, IN };
// Pointの比較をしたいときだけ定義する.
namespace std{
bool operator < (const P& a, const P& b) {
return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag();
}
};
typedef vector<P> L;
P vec(L l){return l[1] - l[0];}
// 線分と点の交差判定(端点の処理に注意)(端点は含むけれども誤差に注意)
// verify : AOJ1279, AOJ2506
bool iSP(L s, P p) {return ccw(s[0], s[1], p) == 0;}
// 直線と直線の交点
// Verified: AOJ2579
// size()によって場合分け
// *4 : 直線が重なる *1 : 交点が1つ存在 *0 : 直線が交差しない
vector<P> pLL(L l, L m){
double A = cross(vec(l), vec(m));
double B = cross(vec(l), l[1] - m[0]);
if(sign(A) == 0 && sign(B) == 0) return {l[0], l[1], m[0], m[1]}; // 二直線が重なっている
if(sign(A) == 0) return{}; // 直線が交わらない
return {m[0] + vec(m) * B / A};
}
vector<P> pSS(L l, L m){
vector<P> res;
auto find = [&](P p){
for(P r : res) if(sign(abs(r - p)) == 0)
return true;
return false;
};
for(P p : pLL(l, m))
if(iSP(l,p) && iSP(m,p) && !find(p)) // 片方が直線の場合は適宜変えること
res.push_back(p);
return res;
}
// 入力
// ss : 線分のリスト
//
// 出力
// ps : グラフの頂点番号に対応する点が入る
// 返り値 : 上の説明のグラフ
//
// Verified
// AOJ 2113
typedef vector<int> Node;
typedef vector<Node> Graph;
Graph segment_arrangement(const vector<L> &ss, vector<P> &ps) {
for (int i = 0; i < ss.size(); i++) {
ps.push_back( ss[i][0] );
ps.push_back( ss[i][1] );
for (int j = i+1; j < ss.size(); j++){
auto cp = pSS(ss[i], ss[j]);
if (!cp.empty()) {
assert(cp.size() == 1);
ps.push_back( cp.back() );
}
}
}
sort(ps.begin(), ps.end());
ps.erase(unique(ps.begin(), ps.end()), ps.end());
Graph g(ps.size());
for (int i = 0; i < ss.size(); i++) {
vector<int> on;
for (int j = 0; j < ps.size(); j++){
if (iSP(ss[i], ps[j])){
on.push_back(j);
}
}
for (int j = 0; j + 1 < on.size(); j++) {
for(int k = j + 1; k < on.size(); k++) {
int a = on[j], b = on[k];
g[a].push_back( b );
g[b].push_back( a );
}
}
}
return g;
}
// 線分併合
//
// 線分のリストからオーバーラップするものたちをまとめ,新しい線分のリストを作る.
// 元々の線分のリストにおける順番は破壊される.
//
// not verified
void merge_segments(vector<L>& segs) {
auto merge_if_able = [](L& s, L t){
if (abs(cross(s[1]-s[0], t[1]-t[0])) > EPS) return false;
//if (sign(cross(vec(s), vec(t))) != 0) return false;
if (ccw(s[0], t[0], s[1]) == +1 ||
ccw(s[0], t[0], s[1]) == -1) return false; // not on the same line
if (ccw(s[0], s[1], t[0]) == -2 ||
ccw(t[0], t[1], s[0]) == -2) return false; // separated
s = { min(s[0], t[0]), max(s[1], t[1]) };
return true;
};
for (int i = 0; i < segs.size(); ++i)
if (segs[i][1] < segs[i][0])
swap(segs[i][1], segs[i][0]);
for (int i = 0; i < segs.size(); ++i)
for (int j = i+1; j < segs.size(); ++j)
if (merge_if_able(segs[i], segs[j]))
segs[j--] = segs.back(), segs.pop_back();
}
P input() {
double x, y;
cin >> x >> y;
return {x, y};
}
typedef tuple<double, int, int> T;
const int MAX = 500;
const double INF = 1e64;
int main(){
int n;
while(cin >> n && n > 0){
vector<L> lines(n);
REP(i, n) {
P a = input();
P b = input();
lines[i] = {a, b};
}
merge_segments(lines);
//assert(size == lines.size());
vector<P> ps;
Graph G = segment_arrangement(lines, ps);
/*
REP(i, G.size()){
cout << i << ":";
for(int v : G[i]) cout << v << " ";
cout << endl;
}
*/
assert(ps.size() < MAX);
REP(i, ps.size()) REP(j, i) {
assert(abs(ps[i] - ps[j]) > EPS);
}
auto find = [](const vector<P>& pv, P p) -> int {
for(int i = 0; i < pv.size(); i++) {
auto q = pv[i];
if(abs(p - q) < EPS) {
return i;
}
}
return -1;
};
P sp = input();
P gp = input();
int s = find(ps, sp);
int g = find(ps, gp);
/*
if(s == g) {
cout << 0 << endl;
continue;
}
*/
assert(s != -1 && g != -1);
priority_queue<T, vector<T>, greater<T>> que;
double dist[MAX][MAX] = {};
REP(i, MAX) REP(j, MAX) dist[i][j] = INF;
for(int u : G[s]) {
dist[s][u] = 0.0;
que.push(T(0.0, s, u));
}
double ans = INF;
while(!que.empty()) {
T t = que.top(); que.pop();
double d;
int u, v;
tie(d, u, v) = t;
if(v == g) {
ans = d;
break;
}
for(int w : G[v]) if(u != w) {
P p = ps[v] - ps[u];
P q = ps[w] - ps[v];
double th = abs(angle(p, q)) / M_PI * 180.0;
double nd = d + th;
if(dist[v][w] > nd) {
dist[v][w] = nd;
que.push(T(nd, v, w));
}
}
}
if(ans == INF) {
cout << -1 << endl;
} else {
printf("%.12f\n", ans);
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops")
using namespace std;
long long n, x, y, z;
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> x;
y = max(y, x);
}
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> x;
z = max(z, x);
}
cout << y << ' ' << z;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
struct range {
long long l, r;
range() {}
};
union rect {
struct {
long long x, y, xx, yy;
};
long long a[4];
rect() {}
rect(long long x, long long y, long long xx, long long yy)
: x(x), y(y), xx(xx), yy(yy) {}
};
long long query(const rect &r) {
static rect first = {3, 4, 3, 5}, second = {2, 2, 2, 2};
if (r.x > r.xx || r.y > r.yy) {
return 0;
}
static int cnt = 0;
cnt++;
if (cnt == 200) throw;
cout << "? " << r.x << " " << r.y << " " << r.xx << " " << r.yy << "\n"
<< flush;
long long ans;
cin >> ans;
return ans;
}
ostream &operator<<(ostream &cout, const rect &r) {
cout << r.x << " " << r.y << " " << r.xx << " " << r.yy;
return cout;
}
void answer(rect a, rect b) {
cout << "! " << a << " " << b << "\n" << flush;
exit(0);
}
long long fnc_lower(rect re, long long pos, long long cnt) {
long long l = re.a[pos], r = re.a[pos + 2];
while (l != r) {
long long m = (l + r + 1) / 2;
re.a[pos] = m;
if ((query(re) == cnt))
l = m;
else
r = m - 1;
}
return l;
}
long long fnc_upper(rect re, long long pos, long long cnt) {
long long l = re.a[pos - 2], r = re.a[pos];
while (l != r) {
long long m = (l + r) / 2;
re.a[pos] = m;
if (!(query(re) == cnt))
l = m + 1;
else
r = m;
}
return l;
}
void minimize(rect &r, long long cnt) {
for (long long i = 0; i < 2; ++i) {
r.a[i] = fnc_lower(r, i, cnt);
}
for (long long i = 2; i < 4; ++i) {
r.a[i] = fnc_upper(r, i, cnt);
}
}
int main() {
ios::sync_with_stdio(0);
long long n;
cin >> n;
rect all_rect = rect(1, 1, n, n);
minimize(all_rect, 2);
rect one_rect = all_rect;
minimize(one_rect, 1);
rect another_rect;
another_rect = all_rect;
another_rect.x = one_rect.xx + 1;
if (query(another_rect) == 1) {
minimize(another_rect, 1);
answer(one_rect, another_rect);
}
another_rect = all_rect;
another_rect.xx = one_rect.x - 1;
if (query(another_rect) == 1) {
minimize(another_rect, 1);
answer(one_rect, another_rect);
}
another_rect = all_rect;
another_rect.y = one_rect.yy + 1;
if (query(another_rect) == 1) {
minimize(another_rect, 1);
answer(one_rect, another_rect);
}
another_rect = all_rect;
another_rect.yy = one_rect.y - 1;
if (query(another_rect) == 1) {
minimize(another_rect, 1);
answer(one_rect, another_rect);
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int n, d;
cin >> n >> d;
long long int a[n];
for (long long int i = 0; i < n; i++) {
cin >> a[i];
}
long long int r = 0, ans = 0;
for (long long int i = 0; i < n; i++) {
while (a[r] - a[i] <= d && r < n) r++;
ans += ((r - 1 - i) * (r - 2 - i)) / 2;
}
cout << ans << endl;
cerr << "Time : " << (double)clock() / (double)CLOCKS_PER_SEC << "s\n";
return 0;
}
| 1 |
#include<iostream>
#include<string>
#include<string.h>
#include<map>
using namespace std;
#define REP(i,a,b) for(i=a; i<b; ++i)
#define rep(i,n) REP(i,0,n)
long long hp[100];
long long dp1[100050];
long long dp2[100050];
const long long inf = 1<<29;
pair<long long, long long> smagic[100];
pair<long long, long long> amagic[100];
int main() {
int N,M,i,j,k,mp,dm,am,sm;
long long maxhp;
string name,range;
while(cin>>N, N) {
memset(hp, 0, sizeof(hp));
rep(i,100005) dp1[i] = dp2[i] = inf;
am = sm = maxhp = 0;
rep(i,N) cin>>hp[i];
rep(i,N) maxhp = max(maxhp, hp[i]);
cin>>M;
rep(i,M) {
cin>>name>>mp>>range>>dm;
if(range == "All") {
amagic[am] = make_pair(mp,dm);
am++;
}else{
smagic[sm] = make_pair(mp,dm);
sm++;
}
}
dp1[0] = dp2[0] = 0;
rep(i,maxhp+1) {
rep(j,am) {
if(dp1[i] == inf) continue;
long long h = min(maxhp, i+amagic[j].second);
dp1[h] = min(dp1[h],
dp1[i]+amagic[j].first);
}
rep(j,sm) {
if(dp2[i] == inf) continue;
int h = min(maxhp, i+smagic[j].second);
dp2[h] = min(dp2[h],
dp2[i]+smagic[j].first);
}
}
int smax = inf,amax = inf;
for(i = 100004; i>=0; --i) {
if(smax > dp2[i]) smax = dp2[i];
if(amax > dp1[i]) amax = dp1[i];
if(dp2[i] > smax) dp2[i] = smax;
if(dp1[i] > amax) dp1[i] = amax;
}
long long ans = inf;
for(int i=maxhp; i>=0; --i) {
//cout<<i<<" "<<ans<<endl;
int j;
long long tmp = dp1[i];
rep(j,N) {
if(hp[j]-i > 0) {
//if(i == 98495) cout<<hp[j]-i<<" "<<j<<" "<<dp2[hp[j]-i]<<endl;
tmp += dp2[hp[j]-i];
}
}
//cout<<ans<<" "<<tmp<<endl;
// if(ans > tmp) {
// cout<<i<<" "<<ans<<" "<<tmp<<endl;
// }
ans = min(tmp, ans);
}
cout<<ans<<endl;
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long int t;
cin >> t;
while (t--) {
string s;
cin >> s;
long long int one = INT_MAX, two = INT_MAX, three = INT_MAX, ans = INT_MAX;
long long int i = 0;
while (i < s.size()) {
if (one != INT_MAX && two != INT_MAX && three != INT_MAX) {
long long int val1 = min(min(one, two), three);
long long int val2 = max(max(one, two), three);
ans = min(ans, val2 - val1 + 1);
if (one == val1) {
one = INT_MAX;
}
if (two == val1) {
two = INT_MAX;
}
if (three == val1) {
three = INT_MAX;
}
i++;
} else {
if (s[i] == '1') {
one = i + 1;
}
if (s[i] == '2') {
two = i + 1;
}
if (s[i] == '3') {
three = i + 1;
}
if (one == INT_MAX || two == INT_MAX || three == INT_MAX) {
i++;
}
}
}
if (one != INT_MAX && two != INT_MAX && three != INT_MAX) {
long long int val1 = min(min(one, two), three);
long long int val2 = max(max(one, two), three);
ans = min(ans, val2 - val1 + 1);
}
if (ans == INT_MAX) {
cout << "0"
<< "\n";
} else {
cout << ans << "\n";
}
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for(int i = 0; i < n; i++)
#define all(a) a.begin(), a.end()
#define mod 1000000007
#define MOD 998244353
#define INF 40000000000000000
int dx8[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
int dy8[8] = {1, 1, 1, 0, -1, -1, -1, 0};
int dx4[4] = {-1, 0, 1, 0};
int dy4[4] = {0, -1, 0, 1};
template <class T>
inline bool chmax(T &a, T b) {
if(a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool chmin(T &a, T b) {
if(a > b) {
a = b;
return 1;
}
return 0;
}
signed main() {
int N;cin>>N;
priority_queue<int,vector<int>,greater<int>>Q;
rep(i,N){
int M;cin>>M;
int x;cin>>x;
Q.push(x);
rep(j,M-1){
int p;cin>>p;
int now=Q.top();Q.pop();
if(now<p)Q.push(p);
else Q.push(now);
}
}
int ans=0;
while(!Q.empty()){
int x=Q.top();Q.pop();
ans+=x;
}
cout<<ans<<endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int T, N, a[1000001], b[1000001];
bool chk[1000001];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> T;
while (T--) {
cin >> N;
for (int i = 0; i < N; i++) {
cin >> a[i];
chk[i] = 0;
}
for (int i = 0; i < N; i++) cin >> b[i];
int s, prev = -1, cnt = 0;
while (1) {
int f = 0;
for (s = 0; s < N; s++) {
if (a[s] > b[s] && f == 0)
f = 1;
else if (f == 1 && b[s] >= a[s])
break;
}
if (f == 0) {
cout << "YES\n";
break;
} else if (s == N && a[0] > b[0]) {
cout << "NO\n";
break;
}
if (f == 1 && s == N) {
s = 0;
}
chk[s] = 1;
for (int i = 0; i < N; i++) {
int now = (s + i) % N;
int nxt = (now + 1) % N;
if (b[now] < a[now]) continue;
chk[nxt] = 0;
int x = min(a[nxt], b[now] - a[now]);
a[nxt] -= x;
b[now] -= x;
}
cnt++;
if (cnt == 31) {
cout << "NO\n";
break;
}
}
}
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const int maxn = 1e5 + 15;
long long rev[maxn], multy[maxn];
long long ksm(long long a, long long p) {
long long ans = 1, multy = a;
while (p) {
if (p & 1) {
ans *= multy;
ans %= mod;
}
p >>= 1;
multy *= multy;
multy %= mod;
}
return ans;
}
long long C(int n, int m) { return multy[n] * rev[n - m] % mod * rev[m] % mod; }
int main() {
ios::sync_with_stdio(false);
char now[maxn][2];
int n, i, j, k;
int rB = 0, rWait = 0, lW = 0, lWait = 0, waitWait = 0, waitB = 0, wWait = 0,
wB = 0;
bool hasBB = false;
cin >> n;
multy[0] = multy[1] = 1;
rev[0] = rev[1] = 1;
for (i = 2; i <= n; i++) {
multy[i] = multy[i - 1] * i % mod;
rev[i] = rev[i - 1] * ksm(i, mod - 2) % mod;
}
long long ans = 0;
for (i = 1; i <= n; i++) {
cin >> now[i][0] >> now[i][1];
if (now[i][1] == 'B') {
rB++;
if (now[i][0] == 'B') {
hasBB = true;
} else if (now[i][0] == '?') {
waitB++;
} else {
wB++;
}
} else if (now[i][1] == '?') {
rWait++;
if (now[i][0] == '?') {
waitWait++;
} else if (now[i][0] == 'W') {
wWait++;
}
}
if (now[i][0] == 'W') {
lW++;
} else if (now[i][0] == '?') {
lWait++;
}
}
for (int add = 0; add <= rWait; add++) {
if (hasBB) {
int b = rB + add;
if (rB + add >= lW && rB + add <= lW + lWait) {
ans += C(rWait, add) * C(lWait, b - lW) % mod;
ans %= mod;
}
} else {
int bNum = rB + add;
if (rB + add >= lW && rB + add <= lW + lWait) {
ans += C(rWait, add) * C(lWait, bNum - lW) % mod;
ans %= mod;
if (bNum != n && bNum != 0) {
int hasWaitL = lWait - waitB;
int nowW = lW + waitB;
if (nowW <= bNum) {
ans -=
C(waitWait, bNum - nowW) * C(wWait, add - (bNum - nowW)) % mod;
ans %= mod;
}
}
}
}
}
if (ans < 0) {
ans += mod;
}
printf("%lld", ans);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
pair<int, int> p(int f, int c, vector<vector<pair<int, int> > >& padre) {
if (padre[f][c].first == -1) return pair<int, int>(f, c);
return padre[f][c] = p(padre[f][c].first, padre[f][c].second, padre);
}
pair<int, int> bp;
int dp = 0, cp = -1, m, m2;
int cornerx[50][50][4], cornery[50][50][4];
int incf[] = {0, 1, 0, -1};
int incc[] = {1, 0, -1, 0};
void fill(int f, int c, vector<vector<pair<int, int> > >& padre) {
cornerx[f][c][0] = f;
cornery[f][c][0] = c;
int first = f, second = c;
while (second < m2 and p(first, second, padre) == pair<int, int>(f, c))
++second;
--second;
cornerx[f][c][1] = first;
cornery[f][c][1] = second;
while (first < m and p(first, second, padre) == pair<int, int>(f, c)) ++first;
--first;
cornerx[f][c][2] = first;
cornery[f][c][2] = second;
cornerx[f][c][3] = first;
cornery[f][c][3] = c;
}
inline int getcorner() {
if (dp == 0) {
if (cp == -1) return 1;
return 2;
}
if (dp == 1) {
if (cp == -1) return 2;
return 3;
}
if (dp == 2) {
if (cp == -1) return 3;
return 0;
}
if (cp == -1) return 0;
return 1;
}
int main() {
int n;
cin >> m >> n;
vector<string> M(m);
for (int i = 0; i < m; ++i) cin >> M[i];
m2 = M[0].size();
vector<vector<pair<int, int> > > padre(
m, vector<pair<int, int> >(m2, pair<int, int>(-1, -1)));
for (int i = 0; i < m; ++i) {
for (int j = 0; j < m2; ++j) {
if (i > 0 and M[i][j] == M[i - 1][j])
padre[i][j] = p(i - 1, j, padre);
else if (j > 0 and M[i][j] == M[i][j - 1])
padre[i][j] = p(i, j - 1, padre);
}
}
bp = p(0, 0, padre);
memset(cornerx, -1, sizeof(cornerx));
memset(cornery, -1, sizeof(cornery));
for (int i = 0; i < m; ++i) {
for (int j = 0; j < m2; ++j) {
if (p(i, j, padre) != pair<int, int>(i, j)) continue;
fill(i, j, padre);
}
}
for (int i = 0; i < n; ++i) {
int c = getcorner();
int nx = cornerx[bp.first][bp.second][c] + incf[dp],
ny = cornery[bp.first][bp.second][c] + incc[dp];
if (nx < 0 or nx >= m or ny < 0 or ny >= m2 or M[nx][ny] == '0') {
if (cp == -1)
cp = 1;
else {
cp = -1;
++dp;
if (dp == 4) dp = 0;
}
} else
bp = p(nx, ny, padre);
}
cout << M[bp.first][bp.second] << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
int add(int a, int b) { return a + b - (a + b >= MOD) * MOD; }
int mult(int a, int b) { return ((long long)a * b) % MOD; }
int n, h, w;
char s[500013];
int dX[200], dY[200];
int diffx, diffy;
int maxx, minx;
int maxy, miny;
int hasx_[1000013], hasy_[1000013];
int& hasx(int i) { return hasx_[i + 500000]; }
int& hasy(int i) { return hasy_[i + 500000]; }
void calc() {
int x = 0, y = 0;
for (int i = 0; i < n; i++) {
x += dX[s[i]];
y += dY[s[i]];
maxx = max(maxx, x);
minx = min(minx, x);
maxy = max(maxy, y);
miny = min(miny, y);
if (!hasx(x)) hasx(x) = i + 1;
if (!hasy(y)) hasy(y) = i + 1;
}
diffx = x;
diffy = y;
}
int main() {
dX['U'] = -1;
dX['D'] = 1;
dY['L'] = -1;
dY['R'] = 1;
scanf("%d%d%d %s", &n, &h, &w, &s);
calc();
if (maxx - minx + 1 <= h && maxy - miny + 1 <= w && diffx == 0 && diffy == 0)
return printf("-1\n"), 0;
int bot = -1, top = h;
int lef = -1, rig = w;
int x = 0, y = 0;
int step = 0;
int ans = 0;
while (top > bot + 1 && rig > lef + 1) {
int tryt = 1e9;
if (x + top - 1 + maxx >= h) tryt = hasx(h - (top + x - 1));
int tryb = 1e9;
if (x + bot + 1 + minx <= -1) tryb = hasx(-1 - (bot + x + 1));
int tryr = 1e9;
if (y + rig - 1 + maxy >= w) tryr = hasy(w - (rig + y - 1));
int tryl = 1e9;
if (y + lef + 1 + miny <= -1) tryl = hasy(-1 - (lef + y + 1));
if (min(min(tryt, tryb), min(tryr, tryl)) < 1e7) {
if (tryt < min(tryb, min(tryr, tryl))) {
int cur = mult(rig - lef - 1, mult(step, n) + tryt);
ans = add(ans, cur);
top -= 1;
} else if (tryb < min(tryt, min(tryr, tryl))) {
int cur = mult(rig - lef - 1, mult(step, n) + tryb);
ans = add(ans, cur);
bot += 1;
} else if (tryr < min(tryt, min(tryb, tryl))) {
int cur = mult(top - bot - 1, mult(step, n) + tryr);
ans = add(ans, cur);
rig -= 1;
} else if (tryl < min(tryt, min(tryb, tryr))) {
int cur = mult(top - bot - 1, mult(step, n) + tryl);
ans = add(ans, cur);
lef += 1;
}
} else {
x += diffx;
y += diffy;
step += 1;
}
}
printf("%d\n", ans);
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
int s1 = pow(2, n), s2 = 0;
for (int i = 1; i < n; i++) {
if (i < n / 2)
s1 += pow(2, i);
else
s2 += pow(2, i);
}
cout << abs(s1 - s2) << '\n';
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
void solve();
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}
long long visited[1001];
vector<long long> graph[1001];
vector<long long> capitals;
vector<long long> temp_nodes;
void update_nodes_dfs(long long node) {
temp_nodes.push_back(node);
visited[node] = 1;
for (long long i = 0; i < graph[node].size(); i++) {
if (!visited[graph[node][i]]) {
update_nodes_dfs(graph[node][i]);
}
}
}
void solve() {
long long n, m, k, max_edge_count = 0;
cin >> n >> m >> k;
for (long long i = 0; i < k; i++) {
long long x;
cin >> x;
capitals.push_back(x - 1);
}
for (long long i = 0; i < m; i++) {
long long u, v;
cin >> u >> v;
graph[u - 1].push_back(v - 1);
graph[v - 1].push_back(u - 1);
}
long long max_node_count = 0;
for (long long i = 0; i < k; i++) {
temp_nodes.clear();
update_nodes_dfs(capitals[i]);
long long sz = temp_nodes.size();
max_node_count = max(max_node_count, sz);
max_edge_count += (sz * (sz - 1)) / 2;
}
for (long long i = 0; i < n; i++) {
if (!visited[i]) {
temp_nodes.clear();
update_nodes_dfs(i);
long long sz = temp_nodes.size();
max_edge_count -= (max_node_count * (max_node_count - 1)) / 2;
max_node_count += sz;
max_edge_count += (max_node_count * (max_node_count - 1)) / 2;
}
}
cout << max_edge_count - m;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const unsigned long long P = 29;
int n1, n2;
vector<vector<int> > chi2;
vector<int> par2;
vector<unsigned long long> vhs1, vps1, vhs2;
const int HSZ = 1 << 15;
const int HMSK = HSZ - 1;
vector<pair<unsigned long long, int> > htbl[2][HSZ];
int hver[2][HSZ];
int lhver[2];
void addTbl(int id, unsigned long long h, int k) {
int pos = h & HMSK;
if (hver[id][pos] < lhver[id]) {
htbl[id][pos].clear();
hver[id][pos] = lhver[id];
}
for (vector<pair<unsigned long long, int> >::iterator it =
htbl[id][pos].begin();
it != htbl[id][pos].end(); it++)
if (it->first == h) {
it->second += k;
return;
}
htbl[id][pos].push_back(make_pair(h, k));
}
int getTbl(int id, unsigned long long h) {
int pos = h & HMSK;
if (hver[id][pos] < lhver[id]) {
htbl[id][pos].clear();
hver[id][pos] = lhver[id];
}
for (vector<pair<unsigned long long, int> >::iterator it =
htbl[id][pos].begin();
it != htbl[id][pos].end(); it++)
if (it->first == h) return it->second;
return 0;
}
unsigned long long toFind, findK;
int dfs(int v) {
unsigned long long curFind = toFind + vhs2[v] * findK;
addTbl(1, curFind, 1);
int res = 0;
res += getTbl(1, vhs2[v]);
for (int i = 0; i < ((int)(chi2[v]).size()); i++) res += dfs(chi2[v][i]);
addTbl(1, curFind, -1);
return res;
}
int solve(unsigned long long h, unsigned long long k) {
toFind = h;
findK = k;
lhver[1]++;
return dfs(0);
}
int main() {
int ops;
while (scanf("%d", &ops) >= 1) {
vhs1 = vector<unsigned long long>(1, 0);
vps1 = vector<unsigned long long>(1, 1);
vhs2 = vector<unsigned long long>(1, 0);
par2 = vector<int>(1, -1);
n1 = n2 = 1;
memset(hver, 0, sizeof hver);
memset(lhver, 0, sizeof lhver);
for (int i = 0; i < HSZ; i++) {
htbl[0][i].clear();
htbl[0][i].reserve(10);
}
for (int i = 0; i < HSZ; i++) {
htbl[1][i].clear();
htbl[1][i].reserve(10);
}
addTbl(0, 0, 1);
long long cans = 1;
chi2 = vector<vector<int> >(1);
while (ops-- > 0) {
int t, v;
char c;
scanf(" %d%d %c", &t, &v, &c), v--;
if (t == 1) {
assert(v <= n1);
vhs1.push_back(vhs1[v] + vps1[v] * c);
vps1.push_back(vps1[v] * P);
addTbl(0, vhs1[n1], 1);
cans += solve(vhs1[n1], vps1[n1]);
n1++;
} else {
assert(v <= n2);
vhs2.push_back(vhs2[v] * P + c);
chi2[v].push_back(n2);
chi2.push_back(vector<int>());
par2.push_back(v);
unsigned long long ck = 1;
for (int x = n2; x >= 0; x = par2[x], ck *= P) {
unsigned long long ch = vhs2[n2] - vhs2[x] * ck;
cans += getTbl(0, ch);
}
n2++;
}
printf("%I64d\n", cans);
}
}
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
bool umax(T &x, T y) {
return (x < y) ? x = y, true : false;
}
template <class T>
bool umin(T &x, T y) {
return (x > y) ? x = y, true : false;
}
struct SAM {
struct state {
int fa, len;
vector<int> nxt;
};
vector<state> m;
vector<int> R, idx;
void init() {
m.assign(2, {0, 0, vector<int>(26)});
R.assign(2, 0);
}
int add() {
m.resize(m.size() + 1);
R.push_back(0);
return m.size() - 1;
}
int touch(int x, int c) {
int y = m[x].nxt[c], len = m[x].len + 1;
if (y) {
if (m[y].len == len) return y;
int z = add();
m[z] = {m[y].fa, len, m[y].nxt};
for (m[y].fa = z; m[x].nxt[c] == y; x = m[x].fa) m[x].nxt[c] = z;
return z;
} else {
y = add();
for (; x && !m[x].nxt[c]; x = m[x].fa) m[x].nxt[c] = y;
int fa = (!x) ? 1 : touch(x, c);
m[y] = {fa, len, m[0].nxt};
return y;
}
}
int extend(int x, int c) {
int res = touch(x, c);
return R[res]++, res;
}
void prepare() {
idx.assign(m.size(), 0);
vector<int> cnt(m.size());
for (int i = 0; i < (m.size()); i++) cnt[m[i].len]++;
for (int i = (1); i < (m.size()); i++) cnt[i] += cnt[i - 1];
for (int i = (m.size()) - 1; i >= 0; i--) idx[--cnt[m[i].len]] = i;
for (int i = (m.size()) - 1; i >= (2); i--) R[m[idx[i]].fa] += R[idx[i]];
}
long long solve(string &s) {
int x = 1, len = 0;
int pos = 0;
for (auto c : s) {
c -= 'a';
while (x && !m[x].nxt[c]) x = m[x].fa, len = m[x].len;
if (!x)
x = 1, len = 0;
else
x = m[x].nxt[c], ++len;
}
int sx = -1;
long long res = 0;
for (auto c : s) {
c -= 'a';
while (x && !m[x].nxt[c]) x = m[x].fa, len = m[x].len;
if (!x)
x = 1, len = 0;
else
x = m[x].nxt[c], ++len;
for (;;) {
int y = m[x].fa;
if (m[y].len < ((int)(s).length())) break;
x = y;
len = m[y].len;
}
if (len >= ((int)(s).length())) {
if (x == sx) break;
if (sx == -1) sx = x;
res += R[x];
}
}
return res;
}
} sam;
void prepare() {}
void input() {
string s;
cin >> s;
int last = 1;
sam.init();
for (auto c : s) last = sam.extend(last, c - 'a');
sam.prepare();
}
void solve() {
int q;
for (cin >> q; q--;) {
string t;
cin >> t;
cout << sam.solve(t) << endl;
}
}
void output() {}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
prepare();
input();
solve();
output();
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
struct point {
int p;
int color;
int num;
};
bool compare1(point a, point b) { return a.p < b.p; }
bool compare2(point a, point b) { return a.num < b.num; }
int main() {
int n, m;
int b[105][2];
point p[105];
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> p[i].p;
p[i].num = i;
}
for (int j = 0; j < m; j++) {
cin >> b[j][0];
cin >> b[j][1];
}
sort(p, p + n, compare1);
for (int i = 0; i < n; i++) {
if (i % 2) {
p[i].color = 0;
} else {
p[i].color = 1;
}
}
sort(p, p + n, compare2);
for (int i = 0; i < n; i++) {
cout << p[i].color;
if (i != n - 1) {
cout << " ";
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
const int MAXN = 1e6 + 5;
int res[MAXN];
int main() {
int n, k;
scanf("%d%d", &n, &k);
if (k % 2 != 0) {
int take = n - 3 * (k - 1);
if (take < 0) {
printf("-1\n");
return 0;
}
for (int i = 1; i < take; i++) res[i] = 1;
res[n] = 1;
int p = 2;
for (int i = take; i < n; i += 6) {
res[i] = p;
res[i + 1] = p;
res[i + 3] = p;
res[i + 2] = p + 1;
res[i + 4] = p + 1;
res[i + 5] = p + 1;
p += 2;
}
for (int i = 1; i <= n; i++) printf("%d ", res[i]);
printf("\n");
return 0;
}
int take = n - 3 * (k - 2);
if (take < 6) {
printf("-1\n");
return 0;
}
res[1] = res[2] = res[4] = 1;
res[3] = 2;
take -= 4;
for (int i = n; i > n - take; i--) res[i] = 2;
int p = 3;
for (int i = 5; i <= n - take; i += 6) {
res[i] = p;
res[i + 1] = p;
res[i + 3] = p;
res[i + 2] = p + 1;
res[i + 4] = p + 1;
res[i + 5] = p + 1;
p += 2;
}
for (int i = 1; i <= n; i++) printf("%d ", res[i]);
printf("\n");
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
for (int i = 0; i < t; i++) {
int a, b;
cin >> a >> b;
if (a > b) {
if (a >= 2 * b) {
cout << a * a << endl;
} else {
cout << (2 * b) * (2 * b) << endl;
}
} else if (a == b) {
cout << 4 * a * b << endl;
} else {
swap(a, b);
if (a >= 2 * b) {
cout << a * a << endl;
} else {
cout << (2 * b) * (2 * b) << endl;
}
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int nmax = 110, tmax = 110;
struct PAIR {
long long first, last;
};
int n, t;
PAIR f[nmax];
long long p[tmax];
long long ans;
bool cmp(PAIR x, PAIR y) { return x.last < y.last; }
int main() {
int i, lv = 1;
long long num = 0, tmp;
scanf("%d", &n);
for (i = 1; i <= n; i++) scanf("%I64d%I64d", &f[i].first, &f[i].last);
scanf("%d", &t);
for (i = 1; i <= t; i++) scanf("%I64d", p + i);
sort(f + 1, f + n + 1, cmp);
p[t + 1] = 10000000000000ll;
for (i = 1; i <= n; i++) {
if (num + f[i].first <= p[lv]) {
ans += f[i].first * f[i].last * lv;
num += f[i].first;
} else {
tmp = p[lv] - num;
ans += tmp * f[i].last * lv;
num += tmp;
f[i].first -= tmp;
lv++;
i--;
}
}
printf("%I64d\n", ans);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int m = 0, a = 0;
for (int i = 0; i < 100; i++) {
string s;
getline(cin, s);
if (s[0] == '+') {
m++;
continue;
}
if (s[0] == '-') {
m--;
continue;
}
int c = 1;
int t = 0;
for (int j = 0; j < s.size(); j++) {
if (s[j] == ':' && c == 1) {
c = 0;
continue;
}
if (c == 0) {
a += (s.size() - j) * m;
break;
}
}
}
cout << a;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1100;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
int n, k;
char s[maxn];
long long dp[maxn][maxn << 1][5];
int main() {
scanf("%d%d", &n, &k);
memset(dp, 0, sizeof dp);
dp[1][1][0] = dp[1][1][3] = 1;
dp[1][2][1] = dp[1][2][2] = 1;
for (int i = 2; i <= n; i++) {
for (int j = 1; j <= n << 1; j++) {
dp[i][j][0] = (dp[i - 1][j][0] + dp[i - 1][j][1] + dp[i - 1][j][2] +
dp[i - 1][j - 1][3]) %
998244353;
dp[i][j][1] = (dp[i - 1][j][1] + dp[i - 1][j - 2][2] +
dp[i - 1][j - 1][0] + dp[i - 1][j - 1][3]) %
998244353;
dp[i][j][2] = (dp[i - 1][j][2] + dp[i - 1][j - 2][1] +
dp[i - 1][j - 1][0] + dp[i - 1][j - 1][3]) %
998244353;
dp[i][j][3] = (dp[i - 1][j][3] + dp[i - 1][j][1] + dp[i - 1][j][2] +
dp[i - 1][j - 1][0]) %
998244353;
}
}
long long ans = dp[n][k][0] + dp[n][k][1] + dp[n][k][2] + dp[n][k][3];
ans = ans % 998244353;
printf("%lld\n", ans);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e6 + 3;
long long qpow(long long base, long long p) {
long long res = 1;
while (p > 0) {
if (p & 1) res = (res * base) % mod;
base = (base * base) % mod;
p >>= 1;
}
return res;
}
long long C(long long n, long long m) {
m = min(m, n - m);
long long a = 1, b = 1, i;
for (i = 1; i <= m; i++) {
a = a * i % mod;
b = b * (n + 1 - i) % mod;
}
a = qpow(a, mod - 2);
return (a * b) % mod;
}
int main() {
long long n, m;
scanf("%lld %lld", &n, &m);
printf("%lld", (C(n + m, m) + mod - 1) % mod);
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int s, v1, v2, t1, t2;
cin >> s >> v1 >> v2 >> t1 >> t2;
if (s * v1 + 2 * t1 < s * v2 + 2 * t2)
cout << "First";
else if (s * v1 + 2 * t1 > s * v2 + 2 * t2)
cout << "Second";
else
cout << "Friendship";
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
string tostr(const T& t) {
ostringstream os;
os << t;
return os.str();
}
string go(string s) {
string res = "";
for (int i = 0; i < int((s).size()); ++i) {
if (s[i] == '0') continue;
res += s[i];
}
return res;
}
int main(int argc, char* argv[]) {
string a, b, c2, a2, b2;
int c;
cin >> a >> b;
a2 = go(a);
b2 = go(b);
c = atoi(a.c_str()) + atoi(b.c_str());
c2 = go(tostr(c));
if (atoi(a2.c_str()) + atoi(b2.c_str()) == atoi(c2.c_str()))
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long N;
long long M;
struct point {
long long x, y;
point() {}
point(long long x, long long y) : x(x), y(y) {}
long long ilg() { return x * x + y * y; }
};
point operator-(point a, point b) { return point(a.x - b.x, a.y - b.y); }
long long cross(point a, point b) { return a.x * b.y - a.y * b.x; }
vector<point> convex(vector<point> x) {
sort(x.begin(), x.end(), [](point a, point b) {
return make_pair(a.y, a.x) < make_pair(b.y, b.x);
});
vector<point> ret;
ret.push_back(x[0]);
x.erase(x.begin());
sort(x.begin(), x.end(), [&](point a, point b) {
if (cross(a - ret[0], b - ret[0]) == 0)
return (a - ret[0]).ilg() > (b - ret[0]).ilg();
return (cross(a - ret[0], b - ret[0]) > 0);
});
for (point a : x) {
if (ret.size() >= 2 && cross(ret[0] - a, ret.back() - ret[0]) == 0)
continue;
while (ret.size() >= 2) {
long long k = ret.size();
if (cross(a - ret[k - 1], ret[k - 1] - ret[k - 2]) >= 0)
ret.pop_back();
else
break;
}
ret.push_back(a);
}
return ret;
}
const long long modulo = 998244353;
long long mod(long long x, long long m = modulo) {
x %= m;
x += m;
x %= m;
return x;
}
long long power(long long x, long long a) {
x = mod(x);
a = mod(a, modulo - 1);
long long ret = 1;
long long k = 1;
while (a) {
if ((k & a) > 0) {
a ^= k;
ret = mod(ret * x);
}
k <<= 1;
x = mod(x * x);
}
return ret;
}
bool same(vector<long long> x, vector<long long> y) {
if (x.size() + y.size() <= 3) return true;
if (x.size() != y.size()) return false;
srand(clock());
long long Hy = 0;
const long long base = 1073 + rand() % 1516;
long long n = x.size();
long long w = 1;
for (long long i = 0; i < n; i++) {
y[i] = mod(y[i]);
x[i] = mod(x[i]);
Hy += (w * y[i]);
w *= base;
Hy %= modulo;
w %= modulo;
}
long long Hx = 0;
w = 1;
for (long long i = 0; i < n; i++) {
Hx += (w * x[i]);
w *= base;
Hx %= modulo;
w %= modulo;
}
long long W = mod(1 - power(base, n));
for (long long i = (n - 1); i >= 0; i--) {
Hx *= base;
Hx %= modulo;
Hx += x[i] * W;
Hx %= modulo;
if (Hx == Hy) {
return true;
}
}
return false;
}
bool same(vector<point> a, vector<point> b) {
if (a.size() != b.size()) {
return false;
}
vector<long long> x;
vector<long long> y;
long long n = a.size();
for (long long i = 0; i < n; i++) {
x.push_back((a[i] - a[(i + 1) % n]).ilg());
x.push_back(cross(a[i] - a[(i + 1) % n], a[(i + 1) % n] - a[(i + 2) % n]));
}
for (long long i = 0; i < n; i++) {
y.push_back((b[i] - b[(i + 1) % n]).ilg());
y.push_back(cross(b[i] - b[(i + 1) % n], b[(i + 1) % n] - b[(i + 2) % n]));
}
return same(x, y);
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
long long n, m;
N = n;
M = m;
while (cin >> n >> m) {
vector<point> x(n);
vector<point> y(m);
for (point &i : x) cin >> i.x >> i.y;
for (point &i : y) cin >> i.x >> i.y;
if (same(convex(x), convex(y)))
cout << "YES\n";
else
cout << "NO\n";
}
}
| 5 |
#include <stdio.h>
int main(){
long long a, x = 1, y = 1, z = 1;
scanf("%lld", &a);
for(int i = 0; i<a; i++){
x = (x*10)%1000000007;
y = (y*9)%1000000007;
z = (z*8)%1000000007;
}
long long ans = x%1000000007;
ans = (ans+1000000007-((2*y)%1000000007))%1000000007;
ans = (ans+z)%1000000007;
printf("%lld", ans);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int di[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dj[] = {1, -1, 0, 0, -1, 1, 1, -1};
const double PI = 3.14159265359, EPS = 1e-9;
const int N = 100005, M = 202, Mod = 1000000007, OO = 1000000000;
int arr[N];
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0), cout.precision(10),
cout << fixed;
int n, a, b;
cin >> n >> a >> b;
int f;
cin >> f;
int sum = f;
for (int i = 0; i < n - 1; ++i) {
cin >> arr[i];
sum += arr[i];
}
if (f * a / sum >= b) {
cout << 0 << '\n';
return 0;
}
sort(arr, arr + n - 1);
reverse(arr, arr + n - 1);
for (int i = 0; i < n - 1; ++i) {
sum -= arr[i];
if (f * a / sum >= b) {
cout << i + 1 << '\n';
return 0;
}
}
return 0;
}
| 2 |
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,a[1000001];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+n+1,greater<int>());
for(int i=1;i<=n;i++)
if(i+1>a[i+1]&&a[i]>=i)
{
int j=0;
while(a[j+i+1]==i) j++;
if((a[i]-i)%2==0&&j%2==0) printf("Second");
else printf("First");
return 0;
}
return 0;
}
| 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.