code_file1
stringlengths 80
4k
| code_file2
stringlengths 91
4k
| similar_or_different
int64 0
1
|
---|---|---|
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
template<typename T, typename ...Ts>
vector<T> fusion(vector<T> bs,Ts... ts){
auto append=[&](auto vs){for(auto v:vs) bs.emplace_back(v);};
initializer_list<int>{(void(append(ts)),0)...};
return bs;
}
// longest common prefix of s and s[i:n]
vector<int> zalgorithm(vector<int> s){
int n=s.size();
vector<int> as(n,0);
as[0]=n;
int i=1,j=0;
while(i<n){
while(i+j<n&&s[j]==s[i+j]) j++;
as[i]=j;
if(j==0){
i++;
continue;
}
int k=1;
while(i+k<n&&k+as[k]<j) as[i+k]=as[k],k++;
i+=k;
j-=k;
}
return as;
}
//INSERT ABOVE HERE
signed main(){
int n;
cin>>n;
vector<int> as(n),bs(n);
for(int i=0;i<n;i++) cin>>as[i];
for(int i=0;i<n;i++) cin>>bs[i];
vector<int> cs(n),ds(n);
for(int i=0;i<n;i++){
cs[i]=as[i]^as[(i+1)%n];
ds[i]=bs[i]^bs[(i+1)%n];
}
vector<int> ss=fusion(ds,cs,cs);
auto zs=zalgorithm(ss);
for(int i=0;i<n;i++)
if(zs[n+i]>=n) cout<<i<<" "<<(as[i]^bs[0])<<endl;
return 0;
}
| //fold
#ifndef KHALIL
#include <bits/stdc++.h>
#else
#include "header.h"
#endif
#define endl '\n'
#define mp make_pair
#define tostr(x) static_cast<ostringstream&>((ostringstream()<<dec<<x)).str()
#define rep(i,begin,end) for(auto i = begin;i < end;i++)
#define repr(i,begin,end) for(auto i = begin-1;i >= end;i--)
#define pb push_back
#define sz(a) ((int)(a).size())
#define fi first
#define se second
#define abs(a) ((a) < (0) ? (-1)*(a) : (a))
#define SQ(a) ((a)*(a))
#define eqd(a,b) (abs(a-b)<1e-9)
#define X real()
#define Y imag()
using namespace std;
typedef long long ll;
typedef long double ld;
template <typename t> t in(t q){cin >> q;return q;}
template <typename T> ostream& operator<<(ostream& os, const vector<T>& v){os << "[";for (int i = 0; i < sz(v); ++i) { os << v[i]; if (i != sz(v) - 1) os << ",";}os << "]";return os;}
template <typename T, typename S>ostream& operator<<(ostream& os, const map<T, S>& v){for (auto it : v)os << "(" << it.first << ":" << it.second << ")";return os;}
template <typename T, typename S>ostream& operator<<(ostream& os, const pair<T, S>& v){os << "(" << v.first << "," << v.second << ")";return os;}
const long double PI = acosl(-1);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
inline int rand(int l,int r){return uniform_int_distribution<int>(l, r)(rng);}
inline ll rand(ll l,ll r){return uniform_int_distribution<ll>(l, r)(rng64);}
//endfold
#define N (200'005)
#define MOD (1000000000ll + 7ll)
#define OO (1050000000)
#define OOL (1100000000000000000)
//global
int a[N];
int main(){
//fold
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cout << setprecision(10);
//endfold
int n,m;
cin >> n >> m;
int eew = 0;
rep(i,0,m){
cin >> a[i];
if(a[i]&1){
eew++;
}
}
if(eew == 0){
vector<int> v;
vector<int> w;
v.push_back(a[0]-2);
v.push_back(1);
rep(i,1,m){
v.push_back(a[i]);
}
v.push_back(1);
rep(i,0,sz(v)){
if(v[i] != 0) w.push_back(v[i]);
}
rep(i,0,m){
cout << a[i] << " ";
}cout << endl;
cout << sz(w) << endl;
rep(i,0,sz(w)){
cout << w[i] << " ";
}
return 0;
}
if(eew == 1){
sort(a,a+m,[](int w,int e){
return (w&1) > (e&1);
});
vector<int> v;
vector<int> w;
v.push_back(a[0]-1);
rep(i,1,m){
v.push_back(a[i]);
}
v.push_back(1);
rep(i,0,sz(v)){
if(v[i] != 0) w.push_back(v[i]);
}
rep(i,0,m){
cout << a[i] << " ";
}cout << endl;
cout << sz(w) << endl;
rep(i,0,sz(w)){
cout << w[i] << " ";
}
return 0;
}
if(eew == 2){
vector<int> q;
rep(i,0,m){
if(a[i]&1) q.push_back(i);
}
vector<int> res = {a[q[0]]};
vector<int> v;
vector<int> w;
rep(i,0,m){
if(i == q[0] || i == q[1]) continue;
res.push_back(a[i]);
}
res.push_back(a[q[1]]);
v.push_back(a[q[0]]-1);
rep(i,1,m-1){
v.push_back(res[i]);
}
v.push_back(a[q[1]]+1);
rep(i,0,sz(v)){
if(v[i] != 0) w.push_back(v[i]);
}
rep(i,0,m){
cout << res[i] << " ";
}cout << endl;
cout << sz(w) << endl;
rep(i,0,sz(w)){
cout << w[i] << " ";
}
return 0;
}
cout << "Impossible";
return 0;
} | 0 |
#include<iostream>
using namespace std;
int main(){
int n, a ;
cin >> n;
int day[n];
for(int i = 0; i < n; i++){
cin >> a;
day[i] = a;
}
for(int i = 0; i < n; i++){
int b = n - i - 1;
printf("%d", day[b]);
if(b != 0){
printf(" ");
}
}
printf("\n");
return 0;
}
| #include <iostream>
#include <vector>
#include <string>
using namespace std;
int main() {
int n;
string result;
vector<int> a;
vector<int>::reverse_iterator begin,end;
cin >> n;
for (int c = 0; c < n; c++) {
int t;
cin >> t;
a.push_back(t);
}
begin = a.rbegin();
end = a.rend();
for (;begin != end; begin++) {
result += to_string(*begin) + " ";
}
result.erase(result.size() - 1);
cout << result << endl;
} | 1 |
#include<bits/stdc++.h>
#define rep(i,n) for(int i = 0;i<n;i++)
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
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;}
int main() {
int n;
cin >> n;
vector<int>a(n);
rep(i,n) cin >> a[i];
sort(a.rbegin(),a.rend());
vector<int>ans;
rep(i,n-1) {
if(a[i] == a[i+1]) {
ans.push_back(a[i]);
i++;
}
}
if(ans.size()<2) {
cout << 0 << endl;
return 0;
}
ll square = (ll)(ans[0]) * (ll)(ans[1]);
cout << square << endl;
} | #include <iostream>
#include <map>
#include <cmath>
using namespace std;
int main() {
int n; cin >> n;
map<pair<int, int>, int> mp;
long long ans = 0;
for(int i = 0; i < n; i++) {
double num; cin >> num;
long long x = llround(num*1e9);
int two = 0, five = 0;
while(x % 2 == 0) two++, x /= 2;
while(x % 5 == 0) five++, x /= 5;
for(auto p: mp) {
if(p.first.first+two >= 18 && p.first.second+five >= 18) ans += p.second;
}
mp[make_pair(two, five)]++;
}
cout << ans << endl;
return 0;
}
| 0 |
#include <iostream>
using namespace std;
int main() {
int A, B;
cin >> A >> B;
bool can;
if (A > 8 || B > 8) can = false;
else can = true;
if (can) cout << "Yay!" << endl;
else cout << ":(" << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> ii;
#define DEBUG freopen("in.txt", "r", stdin);
struct fastio {
fastio() {
ios::sync_with_stdio(false);
cout << setprecision(10) << fixed;
cin.tie(0);
}
};
fastio _fast_io;
string a, b, c;
int main() {
cin >> a >> b >> c;
bool ok = true;
if (a.back() != b.front())
ok = false;
if (b.back() != c.front())
ok = false;
if (ok)
cout << "YES" << endl;
else
cout << "NO" << endl;
return 0;
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
const int inf=0x3f3f3f3f;
long long a[maxn];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
long long d;
cin>>d;
a[0]=1;
for(int i=1;i<=20;i++)
a[i]=a[i-1]*10;
long long ans=0;
for(int i=2;i<=20;i++)
{
int k=i/2;
for(long long state=0;state<1<<k;state++)
{
long long cha,chan=d;
if(i%2==1)
cha=10;
else
cha=1;
for(int j=0;j<k;j++)
{
long long x=abs(chan)%10;
if(chan<0)
{
x=10-x;
x%=10;
}
if(state>>j&1)
{
long long sym=10-x;
if(j==0)
sym--;
cha*=sym;
chan+=x*a[i-2*j-1];
chan-=x;
}
else
{
long long sym=x;
if(j==0)
sym--;
if(sym<0)
{
cha=0;
break;
}
cha*=sym;
chan-=(10-x)*a[i-2*j-1];
chan+=(10-x);
}
chan/=10;
}
if(chan==0)
ans+=cha;
}
}
cout<<ans<<endl;
return 0;
}
| #include<bits/stdc++.h>
#include<vector>
#define LL long long
#define PI pair<LL,LL>
using namespace std;
LL D,St1[25],St2[25],P[25],Last[25],Ans;
vector<LL>V;
void DFS(LL L,LL R,LL Val,LL Cnt){
if(L==R){
Ans+=(Val==D)*Cnt;
return;
}
if(abs(Val-D)>Last[L]*10) return;
for(LL I=-9;I<=9;I++){
LL Ways=((L==0)?St2[I+10]:St1[I+10]);
if(V[L]==0) Ways=(I<=0);
if(Ways==0) continue;
DFS(L+1,R,Val+I*V[L],Cnt*Ways);
}
}
void Solve(LL Len){
V.clear();
for(LL I=0;Len-I-1>=I;++I) V.push_back(P[Len-I-1]-P[I]);
vector<PI>An1,An2;
for(LL I=V.size()-1;I>-1;--I) Last[I]=Last[I+1]+V[I];
DFS(0,V.size(),0,1);
}
int main(){
scanf("%lld",&D);
P[0]=1;
for(LL I=1;I<=18;I++) P[I]=P[I-1]*10;
for(LL I=0;I<=9;I++)
for(LL J=0;J<=9;J++){
St1[I-J+10]++;
if(I&&J) St2[I-J+10]++;
}
for(LL I=1;I<=18;I++) Solve(I);
printf("%lld",Ans);
return 0;
}
| 1 |
#include <stdio.h>
int main(){
long long int s,max,min,k;
int n;
scanf("%d",&n);
max = -1000000;
min = 1000000;
s = 0;
for (int i=0; i<n ; ++i){
scanf("%lld",&k);
if(k>max) max = k;
if(k<min) min = k;
s =s+k;
};
printf("%lld %lld %lld\n",min ,max, s);
return 0;
} | #include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <numeric>
using namespace std;
int main() {
vector<int> result;
string n;
string input;
getline(cin, n);
getline(cin, input);
istringstream iss(input);
string num;
while (getline(iss, num, ' ')) {
result.push_back(stoi(num));
}
long long min = *min_element(result.begin(), result.end());
long long max = *max_element(result.begin(), result.end());
long long sum = accumulate(result.begin(), result.end(), 0LL);
cout << min << " " << max << " " << sum << endl;
}
| 1 |
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/rope>
using namespace std;
using namespace chrono;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
mt19937 rng(high_resolution_clock::now().time_since_epoch().count());
mt19937_64 rngll(high_resolution_clock::now().time_since_epoch().count());
template<typename T> T ctmax(T &x, const T &y){ return x = max(x, y); }
template<typename T> T ctmin(T &x, const T &y){ return x = min(x, y); }
template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
int main(){
cin.tie(0)->sync_with_stdio(0);
int n;
cin >> n;
vector<vector<int>> a(n, vector<int>(n));
for(auto i = 0; i < n; ++ i){
for(auto j = 0; j < n; ++ j){
cin >> a[i][j];
}
}
const long long minf = numeric_limits<long long>::min() / 8;
const int mx = 1 << n;
vector<long long> dp(mx, minf), sum(mx);
dp[0] = 0;
for(auto mask = 1; mask < mx; ++ mask){
for(auto i = 0; i < n; ++ i){
if(mask & 1 << i){
for(auto j = i + 1; j < n; ++ j){
if(mask & 1 << j){
sum[mask] += a[i][j];
}
}
}
}
for(auto submask = mask; submask; submask = (submask - 1) & mask){
ctmax(dp[mask], dp[mask ^ submask] + sum[submask]);
}
}
cout << dp.back();
return 0;
}
/*
*/
////////////////////////////////////////////////////////////////////////////////////////
// //
// Coded by Aeren //
// //
//////////////////////////////////////////////////////////////////////////////////////// | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(long long i=0;i<(long long)(n);i++)
#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)
#define all(a) a.begin(),a.end()
#define pb emplace_back
#define eb emplace_back
#define lb(v,k) (lower_bound(all(v),k)-v.begin())
#define ub(v,k) (upper_bound(all(v),k)-v.begin())
#define fi first
#define se second
#define pi M_PI
#define PQ(T) priority_queue<T>
#define SPQ(T) priority_queue<T,vector<T>,greater<T>>
#define dame(a) {out(a);return 0;}
#define decimal cout<<fixed<<setprecision(15);
#define dupli(a) a.erase(unique(all(a)),a.end())
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> PP;
typedef tuple<ll,ll,ll,ll> PPP;
typedef multiset<ll> S;
using vi=vector<ll>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
using vvvvi=vector<vvvi>;
using vp=vector<P>;
using vvp=vector<vp>;
using vb=vector<bool>;
using vvb=vector<vb>;
const ll inf=1001001001001001001;
const ll INF=1001001001;
const ll mod=1000000007;
const double eps=1e-10;
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';}
template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';}
template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);}
template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);}
template<class T> void yesno(T b){if(b)out("yes");else out("no");}
template<class T> void YesNo(T b){if(b)out("Yes");else out("No");}
template<class T> void YESNO(T b){if(b)out("YES");else out("NO");}
template<class T> void noyes(T b){if(b)out("no");else out("yes");}
template<class T> void NoYes(T b){if(b)out("No");else out("Yes");}
template<class T> void NOYES(T b){if(b)out("NO");else out("YES");}
void outs(ll a,ll b){if(a>=inf-100)out(b);else out(a);}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
ll modpow(ll a,ll b){ll res=1;a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;}
int main(){
ll n;cin>>n;
vvi v(n,vi(n));
rep(i,n)rep(j,n)cin>>v[i][j];
vi dp(1<<n);
vi val(1<<n);
rep(i,1<<n)rep(j,n)REP(k,j+1,n)if((i>>j&1)&&(i>>k&1))val[i]+=v[j][k];
dp[0]=0;
REP(i,1,1<<n){
for(int j=i;j>=0;j=(j-1)&i){
chmax(dp[i],dp[j]+val[i&(~j)]);
if(j==0)break;
}
}
out(dp[(1<<n)-1]);
} | 1 |
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define sz(s) ((int) ((s).size()))
#define eprintf(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#ifdef _WIN32
#define LLD "%I64d"
#else
#define LLD "%lld"
#endif
typedef long long ll;
typedef long double ld;
const int inf = (int) 1e9 + 100;
const ld eps = 1e-11;
const ld pi = acos(-1.0L);
mt19937 mrand(random_device{} ());
int rnd(int x) {
return mrand() % x;
}
void precalc() {
}
const int maxn = 1e6 + 100;
int n;
char s[maxn], t[maxn];
bool read() {
if (scanf("%d", &n) < 1) {
return false;
}
scanf("%s", s);
scanf("%s", t);
return true;
}
vector<int> pos[300];
void solve() {
{
bool ok = true;
for (int i = 0; i < n; i++) {
if (s[i] != t[i]) {
ok = false;
}
}
if (ok) {
printf("0\n");
return;
}
}
for (int i = 0; i < 300; i++) {
pos[i].clear();
}
for (int i = 0; i < n; i++) {
pos[s[i]].pb(i);
}
vector<int> xs;
vector<int> want;
for (int i = n - 1; i >= 0; i--) {
if (i == 0 || t[i] != t[i - 1]) {
int mx = sz(xs) ? xs.back() : inf;
mx = min(mx, i);
int j = (int) (upper_bound(pos[t[i]].begin(), pos[t[i]].end(), mx) - pos[t[i]].begin());
j--;
if (j == -1) {
printf("-1\n");
return;
}
xs.pb(pos[t[i]][j]);
want.pb(i);
}
}
reverse(xs.begin(), xs.end());
reverse(want.begin(), want.end());
vector<int> magic;
for (int i = 0; i < sz(xs); i++) {
magic.pb(xs[i] - i);
}
magic.pb(want.back() - sz(xs) + 1);
int ans = 1;
for (int i = 0; i < sz(xs); i++) {
int j = (int) (lower_bound(magic.begin(), magic.end(), want[i] - i) - magic.begin());
ans = max(ans, 1 + j - i);
}
printf("%d\n", ans);
}
int main() {
precalc();
#ifdef DEBUG
assert(freopen("text.in", "r", stdin));
assert(freopen("text.out", "w", stdout));
#endif
while (true) {
if (!read()) {
break;
}
solve();
#ifdef DEBUG
eprintf("Time: %.18f\n", (double) clock() / CLOCKS_PER_SEC);
#endif
}
return 0;
} | #include <iostream>
#include <vector>
using namespace std;
const long long INF = 1LL << 60;
template<typename T>
class LazySegTree {
public:
explicit LazySegTree(int n, T init, T def) : N(calcN_(n)), def(def) {
mVal.assign(2*N+1, init);
mLazy.assign(2*N+1, 0);
mDirty.assign(2*N+1, 0);
}
void update(int l, int r, T value){
updateImpl_(l, r, value, 0, 0, N);
}
void setVal(int idx, T value){
T cur= get(idx, idx+1);
update(idx, idx+1, value - cur);
}
T get(int l, int r){
return getImpl_(l, r, 0, 0, N);
}
private:
int calcN_(int n){
int res = 1;
while(res < n) res *= 2;
return res;
}
void updateImpl_(int l, int r, T value, int idx, int rangeL, int rangeR){
eval_(idx);
if(r <= rangeL || rangeR <= l) return;
if(l <= rangeL && rangeR <= r){
setLazy_(idx, value);
eval_(idx);
} else {
int rangeM = (rangeL+rangeR)/2;
updateImpl_(l, r, value, 2*idx+1, rangeL, rangeM);
updateImpl_(l, r, value, 2*idx+2, rangeM, rangeR);
mVal[idx] = operate_(mVal[2*idx+1], mVal[2*idx+2]);
}
}
void setLazy_(int idx, T value){
mLazy[idx] += value;
mDirty[idx] = 1;
}
T getImpl_(int l, int r, int idx, int rangeL, int rangeR){
if(r <= rangeL || rangeR <= l) return def;
eval_(idx);
if(l <= rangeL && rangeR <= r) return mVal[idx];
int rangeM = (rangeL+rangeR)/2;
T a = getImpl_(l, r, 2*idx+1, rangeL, rangeM);
T b = getImpl_(l, r, 2*idx+2, rangeM, rangeR);
return operate_(a, b);
}
void eval_(int idx){
if(!mDirty[idx]) return;
mVal[idx] += mLazy[idx];
if(idx < N-1){
setLazy_(2*idx+1, mLazy[idx]);
setLazy_(2*idx+2, mLazy[idx]);
}
mLazy[idx] = 0;
mDirty[idx] = 0;
}
T operate_(T a, T b) const { return min(a, b); }
const int N;
const T def;
vector<T> mVal;
vector<T> mLazy;
vector<int> mDirty;
};
int main(){
int N, Q, A, B; cin >> N >> Q >> A >> B;
vector<int> x(Q);
for(auto& t : x) cin >> t;
LazySegTree<long long> minus(N+1, 1LL << 40, INF);
LazySegTree<long long> plus(N+1, 1LL << 40, INF);
minus.setVal(A, abs(B-x[0]) - A);
minus.setVal(B, abs(A-x[0]) - B);
plus.setVal(A, abs(B-x[0]) + A);
plus.setVal(B, abs(A-x[0]) + B);
for(int i=1;i<Q;i++){
auto v = min(minus.get(0, x[i]) + x[i], plus.get(x[i], N+1) - x[i]);
minus.update(1, N+1, abs(x[i]-x[i-1]));
plus.update(1, N+1, abs(x[i]-x[i-1]));
minus.setVal(x[i-1], v - x[i-1]);
plus.setVal(x[i-1], v + x[i-1]);
}
long long res = INF;
for(int i=1;i<=N;i++) res = min(res, plus.get(i, i+1) - i);
cout << res << endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout<<setprecision(32);
int n; cin>>n;
set<pair<int, int> > a, b;
for(int i = 0; i < n; i++){
int x, y;
cin>>x>>y;
a.insert({x, y});
}
for(int i = 0; i < n; i++){
int x, y;
cin>>x>>y;
b.insert({x, y});
}
int ans = 0;
for(auto pp : b){
int x = -1, y = -1;
for(auto qq : a){
if(qq.first < pp.first && qq.second < pp.second){
if(y < qq.second){
x = qq.first;
y = qq.second;
}
}
}
if(x != -1){
a.erase({x, y});
ans++;
}
}
cout<<ans<<endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
template<class T> void ckmin(T &a, T b) { a = min(a, b); }
template<class T> void ckmax(T &a, T b) { a = max(a, b); }
#define pb push_back
#define mp make_pair
#define cotu cout
#define itn int
#define Red ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define F first
#define S second
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for(int i = 0; i < n; ++i)
#define repr(i,n) for(int i = n - 1; i >= 0; --i)
#define Rep(i, a, n) for(int i = (a); i <=(n); ++i)
#define repst(i, n) for(auto it = n.begin(); it != n.end(); ++it)
#define Repr(i, a, n) for(int i = (n); i >= (a); --i)
#define setp(x) fixed << setprecision(x)
#define ordered_set tree<int , null_type,less<int >, rb_tree_tag,tree_order_statistics_node_update>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
const int inf = int(1e9);
const int mod = inf + 7;
const int N = 3e5 + 555; // TL MB
const double PI = acos(-1.0);
void mul(ll &x, ll y){
x *= y;
if(x >= mod) x %= mod;
}
void add(ll &x, ll y){
x += y;
if(x >= mod) x -= mod;
if(x < 0) x += mod;
}
ll bp(ll a, ll n){
ll r = 1;
while(n){
if(n & 1) mul(r, a);
mul(a, a);
n >>= 1;
}
return r;
}
vector<int> g[N];
int n, k;
vector<int> mt;
vector<char> used;
bool try_kuhn (int v) {
if (used[v]) return false;
used[v] = true;
for (size_t i=0; i<g[v].size(); ++i) {
int to = g[v][i];
if (mt[to] == -1 || try_kuhn (mt[to])) {
mt[to] = v;
return true;
}
}
return false;
}
void solve()
{
cin >> n;
vector<pii> p(n), q(n);
rep(i, n) cin >> p[i].F >> p[i].S;
rep(i, n) cin >> q[i].F >> q[i].S;
sort(all(p));
sort(all(q));
rep(i, sz(p)){
rep(j, sz(q)){
if(p[i].F < q[j].F && p[i].S < q[j].S) g[i].pb(j + n), g[j + n].pb(i);
}
}
k = 2 * n;
mt.assign (k, -1);
for (int v=0; v<n; ++v) {
used.assign (n, false);
try_kuhn (v);
}
int res = 0;
for (int i=n; i<2*n; ++i)
if (mt[i] != -1) ++res;
cout << res;
}
int main()
{
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// Red;
int T = 1;
// cin >> T;
for(int i = 1; i <= T; ++i){
solve();
}
return 0;
} | 1 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
string S;
int main() {
cin >> S;
reverse(S.begin(), S.end());
string new_S = S.substr(8, S.length()-8);
reverse(new_S.begin(), new_S.end());
cout << new_S << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
string s; cin >> s;
for (int i = 0; i < s.length() - 8; i++) cout << s[i];
return 0;
}
| 1 |
#include <fstream>
#include <vector>
#include <cstring>
#include <iostream>
// BE CAREFUL WITH HASH TABLE & UNORDERED MAP
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define MOD 998244353
#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define F0R(i,b) FOR(i,0,b)
#define RFO(i,a,b) for (int i = (b-1); i >= (a); i--)
#define RF0(i,b) RFO(i,0,b)
#define lli long long int
#define pii pair<int,int>
#define add(a,b) ((int) (((lli) (a) + (b))%MOD))
#define mul(a,b) ((int) (((lli) (a) * (b))%MOD))
#define PB emplace_back
#define F first
#define S second
// ifstream cin ("cinput.in");
// ofstream cout ("coutput.out");
int N, u, v;
vector<int> graph [100000];
int visited [100000][2];
// Helper Functions
void dfs(int node, int dist, int ind) {
visited[node][ind] = dist;
for (int neigh: graph[node]) {
if (visited[neigh][ind] != -1) continue;
visited[neigh][ind] = dist+1;
dfs(neigh, dist+1, ind);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> u >> v;
u--; v--;
memset(visited, -1, sizeof(int)*N*2);
F0R(i,N-1) {
int a,b;
cin >> a >> b;
a--; b--;
graph[a].PB(b);
graph[b].PB(a);
}
dfs(u,0,0);
dfs(v,0,1);
int ans = 0;
F0R(i,N) {
// cout << i+1 << ' ' << visited[i][0] << ' ' << visited[i][1] << '\n';
if (visited[i][0] < visited[i][1]) {
ans = max(ans, visited[i][1]-1);
}
}
cout << ans << '\n';
}
// even diff = -1. odd diff rts | #include<bits/stdc++.h>
using namespace std;
vector<int>adj[100003],leaf;
int par[100003],depth[100005],sparse[100005][20],root=0,lz=0;
int lca(int p,int q)
{
int a,b,c,d,lp,i,j;
if(depth[p]<depth[q])
{
c=p; p=q; q=c;
}
for(j=19;j>=0;j--)
{
if((depth[p]-(1 << j))>=depth[q])
{
p=sparse[p][j]; if(depth[p]==depth[q])break;
}
}
if(p==q){ return p; }
else
{
for(j=19;j>=0;j--)
{
if(sparse[p][j]!=0 && sparse[p][j]!=sparse[q][j])
{
p=sparse[p][j]; q=sparse[q][j];
if(par[p]==par[q])break;
}
}
return par[p];
}
}
void dfs(int src)
{
int sz=adj[src].size();
if(sz==1 && src!=root){ leaf.push_back(src); lz++; }
for(int lp=0;lp<sz;lp++)
{
int u=adj[src][lp];
if(u!=root && par[u]==0)
{
par[u]=src; sparse[u][0]=src; depth[u]=depth[src]+1;
dfs(u);
}
}
}
int main()
{
int n,m,a,b,i,j,u,v,ans,z,x,y,fr,sc,tr,w;
scanf("%d %d %d",&n,&u,&v);
for(i=1;i<=n-1;i++)
{
scanf("%d %d",&a,&b);
adj[a].push_back(b); if(root==0){ if(adj[a].size()==2)root=a; }
adj[b].push_back(a); if(root==0){ if(adj[b].size()==2)root=b; }
}
if(n==2)printf("0\n");
else
{
dfs(root);
for(j=1;j<=19;j++)
{
for(i=1;i<=n;i++)
{
if(sparse[i][j-1]!=0)
{
sparse[i][j]=sparse[sparse[i][j-1]][j-1];
}
}
}
m=lca(u,v);
ans=(depth[v]-depth[m])+(depth[u]-depth[m]); ans=ans-1;
for(i=0;i<lz;i++)
{
y=leaf[i]; m=lca(u,y); w=lca(v,y);
fr=(depth[u]-depth[m])+(depth[y]-depth[m]);
sc=(depth[v]-depth[w])+(depth[y]-depth[w]);
if(fr<sc)
{
if(sc-1>ans)ans=sc-1;
}
}
printf("%d\n",ans);
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define inf 1001001001001001001
#define mod 1000000007
#define mod2 998244353
#define pi acos(-1)
#define all(v) v.begin(),v.end()
int main(){
int n;
cin>>n;
int A,B,C,D;
int sum=0;
A=n/1000,n%=1000;
B=n/100,n%=100;
C=n/10,n%=10;
D=n;
string s="=7";
vector<int>num={A,B,C,D};
vector<char>op(3);
/*考え方:
全探索を考える(オーダー的にもOK)
・opの入れ方は+か-の2通り
・op1,op2,op3がある
->2^3通り ->ビット全探索orループ*/
for(int bit=0;bit<(1<<3);bit++){
bitset<3>s(bit);
/*operantの決定*/
for(int l=0;l<3;l++){
if(s.test(l)){//bitがいならtrue
op[l]='+';
}else{
op[l]='-';
}
}
sum+=A;
rep(l,3){
if(op[l]=='+'){
sum+=num.at(l+1);
}else{
sum-=num.at(l+1);
}
}
if(sum==7){
cout<<A<<op.at(0)<<B<<op.at(1)<<C<<op.at(2)<<D;
cout<<"=7"<<endl;
return 0;
}else{
sum=0;
}
}
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll R[400009];
string s;
cin >> s;
ll n = s.size();
map<char, int>mp;
for (int i = 0; i < n; i++) {
mp[s[i]]++;
}
ll ans = n*(n-1)/2;
for (auto& p : mp) {
ll v = p.second;
ans -= v*(v-1)/2;
}
cout << ans + 1 << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
//#define cerr if (false) cerr
#define db(x) cerr << #x << "=" << x << endl
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z) cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << endl
#define dbv(v) cerr << #v << "="; for (auto _x : v) cerr << _x << ", "; cerr << endl
#define dba(a, n) cerr << #a << "="; for (int _i = 0; _i < (n); ++_i) cerr << a[_i] << ", "; cerr << endl
template <typename A, typename B>
ostream& operator<<(ostream& os, const pair<A, B>& x) {
return os << "(" << x.first << "," << x.second << ")";
}
typedef long long ll;
typedef long double ld;
int main() {
ll k;
scanf("%lld", &k);
ll n = 50;
printf("%lld\n", n);
ll r = k % n;
for (int i = 0; i < n; ++i) {
printf("%lld ", i + k / n + (i >= n - r) );
}
}
| #include <bits/stdc++.h>
using namespace std;
#define ll int64_t
const ll MX=1E16+1000;
int main() {
ios_base::sync_with_stdio(0);cin.tie(0);
ll k;
cin>>k;
ll n=50;
cout<<n<<'\n';
vector<ll> res(n);
for(int i=0;i<n;++i) res[i]=i+(k/n);
for(int i=0;i<k%n;++i)
for(int j=0;j<n;++j)
if(j==i) res[j]+=n;
else res[j]--;
for(ll x:res) cout<<x<<' ';
cout<<endl;
}
| 1 |
#include <iostream>
#include <cstring>
#include <map>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <set>
#include <vector>
#include <queue>
#include <list>
#include <numeric>
#include <stdio.h>
#include <string>
#include <cstdlib>
#include <math.h>
#include <stack>
#include <climits>
#include <bitset>
#include <utility>
using namespace std;
typedef long long ll;
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; }
//最大公約数
ll gcd(ll x, ll y) {
ll tmp = 0;
if (x < y){
tmp=x;
x=y;
y=tmp;
}
while (y > 0) {
ll r = x % y;
x = y;
y = r;
}
return x;
}
//最大公倍数
ll lcm(ll x,ll y){
return x/gcd(x,y)*y;
}
const int MAX = 1e6+1;
const ll MOD = 1e9+7;
long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++){
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
long long COM(int n, int k){
if (n < k) return 0;
if (n < 0 || k < 0) return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
//階乗
ll kaijo(ll k){
ll sum = 1;
for (ll i = 1; i <= k; ++i)
{
sum *= i;
sum%=1000000000+7;
}
return sum;
}
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
//sieve(MAX);でベクトルIsPrimeに「MAXまでの数値の素数の真偽」を格納する。
vector<bool> IsPrime;
void sieve(size_t max){
if(max+1 > IsPrime.size()){ // resizeで要素数が減らないように
IsPrime.resize(max+1,true); // IsPrimeに必要な要素数を確保
}
IsPrime[0] = false; // 0は素数ではない
IsPrime[1] = false; // 1は素数ではない
for(size_t i=2; i*i<=max; ++i) // 0からsqrt(max)まで調べる
if(IsPrime[i]) // iが素数ならば
for(size_t j=2; i*j<=max; ++j) // (max以下の)iの倍数は
IsPrime[i*j] = false; // 素数ではない
}
struct UnionFind {
vector<int> par;
UnionFind(int n) : par(n, -1) { }
int root(int x) {
if (par[x] < 0) return x;
else return par[x] = root(par[x]);
}
bool issame(int x, int y) {
return root(x) == root(y);
}
bool merge(int x, int y) {
x = root(x); y = root(y);
if (x == y) return false;
if (par[x] > par[y]) swap(x, y); // merge technique
par[x] += par[y];
par[y] = x;
return true;
}
int size(int x) {
return -par[root(x)];
}
};
ll count(int n, int a){
ll bunshi = 1;
for(int i = 0; i < a; i++){
bunshi*=(n-i);
bunshi%=MOD;
}
ll bunbo = 1;
for(int i = 1; i < a+1; i++){
bunbo*=i;
bunbo%=MOD;
}
bunbo = modpow(bunbo,MOD-2,MOD);
// cout<<(bunshi*bunbo)%MOD<<endl;
return (bunshi*bunbo)%MOD;
}
// 約数列挙。約数をvector<ll>で返す。計算量はsqrt(n)。
vector<ll> divisor(ll n) {
vector<ll> ret;
for(ll i = 1; i * i <= n; i++) {
if(n % i == 0) {
ret.push_back(i);
if(i * i != n) ret.push_back(n / i);
}
}
sort(begin(ret), end(ret));
return (ret);
}
// ここから開始
int main(){
int x,y;
cin>>x>>y;
int a[3] = {300000,200000,100000};
ll ans = 0;
if(0<x&&x<4){
ans+=a[x-1];
}
if(0<y&&y<4){
ans+=a[y-1];
}
if(x==1&&y==1){
ans+=400000;
}
cout<<ans<<endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
/*
*/
ll gcd(ll a,ll b){
if(b == 0) return a;
return gcd(b, a % b);
}
using Graph = vector<vector<int>>;
ll calc(ll a, ll b, ll p){//aのb乗をpで割った余り
if(b == 0) return 1;
else if(b%2 == 0){
ll d = calc(a,b/2,p);
return (d*d)%p;
}
else if(b%2 == 1){
return (a * calc(a,b-1,p))%p;
}
}
typedef pair<int,int> P;
bool is_prime(int n){//nが素数であるかどうか判定 iで割り切れると素数でない
if(n == 1) return false;//1は例外
for(int i = 2;i*i <= n;i++){
if(n%i == 0) return false;
}
return true;
}
int factorial(int a){
int res = 1;
while(a){
res *= a;
a--;
}
return res;
}
//r本再利用されない時、N本から再利用して売れる本数
int rec(int m,int n,int N,int r){
if(N == 0) return 0;
int k = N/m * n;//新たに作り出す本数
r = N%m;//再利用されない本数
int res = N + k;//売った本数
while(1){
k = k/m * n;//k本からk/m本再利用
res += k;
r += k%m;
if(k < m){//k本で再利用できなくなったら
k += r;
res += rec(m,n,k,r);
}
}
}
int main(){
int X,Y;
cin >> X >> Y;
if(X == 1 && Y == 1) cout << 1000000 << endl;
else if((X == 1 && Y == 2) || X == 2 && Y == 1) cout << 500000 << endl;
else if((X == 1 && Y == 3) || X == 3 && Y == 1) cout << 400000 << endl;
else if(X == 2 && Y == 2) cout << 400000 << endl;
else if((X == 2 && Y == 3) || (X == 3 && Y == 2)) cout << 300000 << endl;
else if(X == 3 && Y == 3) cout << 200000 << endl;
else if(X == 1 || Y == 1) cout << 300000 << endl;
else if(X == 2 || Y == 2) cout << 200000 << endl;
else if(X == 3 || Y == 3) cout << 100000 << endl;
else cout << 0 << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
// boost
#include <boost/multiprecision/cpp_int.hpp>
using boost::multiprecision::cpp_int;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vs = vector<string>;
using vld = vector<ld>;
using vvld = vector<vld>;
typedef pair<ll, ll> P;
#define bit(n) (1LL << (n))
//#define int long long
#define all(v) v.begin(), v.end()
#define rep(i, n) for (ll i = 0; i < n; i++)
#define REP(i, n) for (ll i = 1; i < n; i++)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORm(i, m) for (auto i = m.begin(); i != m.end(); i++)
template <class T>
inline void chmax(T& a, T b) {
a = std::max(a, b);
}
template <class T>
inline void chmin(T& a, T b) {
a = std::min(a, b);
}
#define mod (ll)(1e9 + 7)
// #define mod (998244353ll)
const long long INF = 1LL << 60;
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
ll n;
cin >> n;
multiset<ll> a;
rep(i, n) {
ll tmp;
cin >> tmp;
a.insert(tmp);
}
ll ans = 0;
while (a.size() >= 2) {
ll mx = *a.rbegin();
ll p2 = 2;
while (p2 <= mx)
p2 *= 2;
if (p2 == mx - p2) {
a.erase(a.lower_bound(mx));
auto itr = a.lower_bound(mx);
if (itr != a.end()) {
a.erase(itr);
ans++;
}
} else {
a.erase(a.lower_bound(mx));
auto itr = a.lower_bound(p2 - mx);
if (*itr == p2 - mx) {
ans++;
a.erase(itr);
}
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep0(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define ll long long
#define coYes cout << "Yes" << endl;
#define coYES cout << "YES" << endl;
#define coyes cout << "yes" << endl;
#define coNo cout << "No" << endl;
#define coNO cout << "NO" << endl;
#define cono cout << "no" << endl;
#define BIT_FLAG_0 (1<<0) // 0000 0000 0000 0001
int gcd(int a, int b){
if(a%b == 0){
return b;
}else{
return gcd(b, a%b);
}
}
int main(){
int a,b,n=1,ans=0;
cin >> a >> b;
while(n<b){
n+=a-1;
ans++;
}
cout << ans << endl;
return 0;
} | 0 |
#include<bits/stdc++.h>
using namespace std;
#define f1(a,b,c) for(int c=a;c<=b;c++)
#define f2(a,b,c) for(int c=a;c>=b;c--)
#define f3(a,b,c) for(int c=a;c;c=b)
#define so1(a,n) sort(a+1,a+n+1,mycmp);
#define so2(a,n) sort(a+1,a+n+1);
#define ll long long
#define itn int
#define ubt int
#define pii pair<int,int>
#define mp make_pair
const int twx=1e6+100;
const int inf=0x7fffffff;
ll read()
{
ll sum=0;
ll flag=1;
char c=getchar();
while(c<'0'||c>'9')
{
if(c=='-')
{
flag=-1;
}
c=getchar();
}
while(c>='0'&&c<='9')
{
sum=((sum*10)+c-'0');
c=getchar();
}
return sum*flag;
}
int n,m;
struct LV
{
int Next;
int y;
int v;
}a[twx<<1];
int Link[twx];
int len;
map<pii,int> p;
int cnt=0;
bool v[twx];
int d[twx];
void Insert(int x,int y,int z)
{
a[++len].y=y;
a[len].Next=Link[x];
a[len].v=z;
Link[x]=len;
}
int get(int x,int y)
{
if(p.find(mp(x,y))!=p.end())
{
return p[mp(x,y)];
}
else
{
return p[mp(x,y)]=++cnt;
}
}
void spfa()
{
memset(d,0x3f,sizeof d);
queue<int> q;
q.push(1);
v[1]=1;
d[1]=0;
while(!q.empty())
{
int x=q.front();
q.pop();
f3(Link[x],a[i].Next,i)
{
int y=a[i].y;
int vs=a[i].v;
if(d[y]>d[x]+vs)
{
d[y]=d[x]+vs;
if(!v[y])
{
v[y]=1;
q.push(y);
}
}
}
v[x]=0;
}
}
void init()
{
n=read();
m=read();
cnt=n;
f1(1,m,i)
{
int x=read();
int y=read();
int z=read();
int xz=get(x,z);
int yz=get(y,z);
Insert(xz,yz,0);
Insert(yz,xz,0);
Insert(x,xz,1);
Insert(xz,x,1);
Insert(y,yz,1);
Insert(yz,y,1);
}
spfa();
printf("%d\n",d[n]==0x3f3f3f3f?-1:d[n]/2);
}
void work()
{
}
void print()
{
}
int main()
{
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
init();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> pipii;
typedef pair<pii, pii> piipii;
#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
#define eb emplace_back
vector<pii> g[100005];
vector<int> cur[100005];
int dp[100005];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
for(int i=1;i<=m;i++){
int a, b, c;
cin >> a >> b >> c;
g[a].eb(mp(b, c));
g[b].eb(mp(a, c));
}
memset(dp, -1, sizeof(dp));
priority_queue<pii> dq;
// deque<pii> dq;
dp[1] = 1;
dq.push(mp(-dp[1], 1));
for(int i=1;i<=1000000;i++) cur[1].eb(i);
while(!dq.empty()){
int u = dq.top().se;
int val = -dq.top().fi;
dq.pop();
if(val != dp[u]) continue;
sort(all(cur[u]));
for(int i=0;i<sz(g[u]);i++){
int v = g[u][i].fi, c = g[u][i].se;
if(binary_search(all(cur[u]), c)){
if(dp[v] == -1 || dp[v] > dp[u]){
cur[v].clear();
cur[v].eb(c);
dp[v] = dp[u];
dq.push(mp(-dp[v], v));
}
else if(dp[v] == dp[u]){
cur[v].eb(c);
}
}
else{
if(dp[v] == -1 || dp[v] > dp[u] + 1){
cur[v].clear();
cur[v].eb(c);
dp[v] = dp[u] + 1;
dq.push(mp(-dp[v], v));
}
else if(dp[v] == dp[u] + 1){
cur[v].eb(c);
}
}
}
}
printf("%d\n", dp[n]);
} | 1 |
#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#include<complex>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define CC puts("-------ok--------");
#define all(in) in.begin(), in.end()
#define bv vector<bool>
using namespace std;
typedef pair<int, int>PA;
using namespace std;
#define MAX 999999
int main(){
while(true){
int ans = 0;
vvi v(100, vi (100 ,INF));
vc c(100,0);vi d(100,0);
vi x(100,0); vi y(100,0);
int m ;cin >>m; if(!m) break;
int tempx = 10,tempy = 10;rep(i,m){cin >> x[i] >> y[i];v[y[i]][x[i]] = 0;}
int n; cin >> n; rep(i,n) {cin >> c[i] >> d[i];
if(c[i] == 'N'){
for(int k =1;k<=d[i];k++)if(v[tempy+k][tempx] == 0){ans++;v[tempy+k][tempx] =INF;}tempy+=d[i];
}
if(c[i] == 'E'){
for(int k =1;k<=d[i];k++)if(v[tempy][tempx+k] == 0) {ans++;v[tempy][tempx+k] = INF;} tempx+=d[i];
}
if(c[i] == 'S'){
for(int k =1;k<=d[i];k++)if(v[tempy-(k)][tempx] == 0){ans++;v[tempy-k][tempx] = INF;}tempy-=d[i];
}
if(c[i] == 'W'){
for(int k =1;k<=d[i];k++)if(v[tempy][tempx-k] == 0){ans++;v[tempy][tempx-k] = INF;}tempx-=d[i];
}
}
if(ans == m)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
} | #include <iostream>
#include<stdio.h>
using namespace std;
void ini(int* p,int n);
void print(int *p);
void check(int *p);
bool judge(int *p);
int main(){
int n;
while(1){
bool ans;
cin >> n;
if(n == 0) break;
int state[21];
for(int i = 0; i<21 ; i++){
state[i] = 0;
}
ini(state,n);
//print(state);
check(state);
ans = judge(state);
if(ans == 0) printf("Yes\n");
else printf("No\n");
//print(state);
}
return 0;
}
void ini(int* state,int n){
int x, y;
int mask;
for(int i = 0; i<n; i++){
cin >>x >> y;
mask = (04000000 >> x);
state[y] =( state[y] | mask);
}
}
void check(int* state){
int n;
char dir;
int step;
int nowx = 10, nowy = 10;
int mask;
cin >> n;
for(int i = 0; i< n; i++){
//printf("x = %d y = %d",nowx, nowy);
cin >>dir >> step;
// printf("%c %d\n",dir,step);
switch(dir){
case 'N':
mask = ~(04000000 >>nowx);
for(int j = 0; j<step; j++){
nowy ++;
//printf("%d\n",nowy);
state[nowy] = (state[nowy] & mask);
}
break;
case 'S':
mask = ~(04000000 >>nowx);
for(int j = 0; j<step; j++){
nowy --;
state[nowy] = (state[nowy] & mask);
}
break;
case 'E':
for(int j = 0; j<step; j++){
nowx ++;
mask = ~(04000000 >>nowx);
state[nowy] = (state[nowy] & mask);
}
break;
case 'W':
for(int j = 0; j<step; j++){
nowx --;
mask = ~(04000000 >>nowx);
state[nowy] = (state[nowy] & mask);
}
break;
}
}
}
bool judge(int *state){
for(int i = 0; i< 21; i++){
if(state[i] != 0) return 1;
}
return 0;
}
void print(int *p){
for(int i = 0; i<21; i++){
printf("[ %d] %o\n",i,p[i]);
}
} | 1 |
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
char S[3];
int ans=0;
for(int i=0;i<3;i++){
cin >>S[i];
}
bool p = S[0] =='R';
bool q = S[1] =='R';
bool r = S[2] =='R';
if(p && q && r){
ans = 3;
}else if((p&&q) || (q&&r)){
ans = 2;
}else if(p || q || r){
ans =1;
}else{
ans=0;
}
cout << ans;
return 0;
}
| #include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <ctype.h>
#include <math.h>
#include <stack>
#include <string>
#include <string.h>
using namespace std;
double PI = 3.1415926535897932;
long mod = 1000000007;
const long INF = 1e9 + 1;
int main() {
int n,m,X,Y,x[200],y[200];
cin >> n >> m >> X >> Y;
for(int i = 0; i < n; i++)
cin >> x[i];
for(int i = 0; i < m; i++)
cin >> y[i];
sort(x,x+n); sort(y,y+m);
if(x[n-1] < y[0] & X < y[0] & x[n-1] < Y)
cout << "No War" << endl;
else
cout << "War" << endl;
} | 0 |
#include <bits/stdc++.h>
int modnum = 1000000007;
int main(){
int N;
std::cin >> N;
std::vector< int > cnt(N+1, 0);
cnt[0] = 3;
long long ans = 1;
for(int i=0; i<N; i++){
int a;
std::cin >> a;
int tmp = cnt[a] - cnt[a + 1];
ans *= (long long)tmp;
ans = ans % modnum;
cnt[a + 1]++;
}
std::cout << ans << std::endl;
return 0;
}
| #include <iostream>
#include <fstream>
#include <cstdlib>
#include <math.h>
#include <utility>
#include <algorithm>
#include <functional>
#include <vector>
#include <numeric>
#include <bits/stdc++.h>
// #include <atcoder/all>
#define int long long
struct edge {
int to;
int cost;
};
using namespace std;
// using namespace atcoder;
using vi = vector<int>;
using vii = vector<vector<int>>;
using vb = vector<bool>;
using qi = queue<int>;
using P = pair<int, int>;
using graph = vector<vector<edge>>;
// using graph = vector<vector<int>>;
// using mint = modint1000000007;
#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define REP(i,n) for(int i=0;i<n;i++)
#define delim(i, n) cout << (i == n-1 ? "\n" : " ")
#define print1(v) cout << fixed << setprecision(10) << v << "\n"
#define print2(v1, v2) cout << fixed << setprecision(10) << v1 << " " << v2 << "\n"
#define print3(v1, v2, v3) cout << fixed << setprecision(10) << v1 << " " << v2 << " " << v3 << "\n"
#define printvi(v, n) REP(iiii, n) { cout << v[iiii]; delim(iiii, n); }
#define printvii(v, h, w) REP(iiiii, h) { printvi(v[iiiii], w); }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
const int inf = 1LL << 60;
// const int inf = 100000000;
const int mod = 1000000007;
const int maxn = 1e5+5;
int n, m;
vi color(3);
vi dx = {-1, 0, 0, 1};
vi dy = {0, -1, 1, 0};
signed main () {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n;
int ret = 1;
REP(i, n) {
int a; cin >> a;
int cnt = 0;
REP(j, 3) {
if (color[j] == a) {
if (cnt == 0) color[j]++;
cnt++;
}
}
ret *= cnt;
ret %= mod;
}
print1(ret);
} | 1 |
#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
using namespace std;
int main() {
int n;
cin >> n;
cout << n/3 << endl;
} | #include<bits/stdc++.h>
using namespace std;
// Returns floor of square root of x
int floorSqrt(int x)
{
// Base cases
if (x == 0 || x == 1)
return x;
// Staring from 1, try all numbers until
// i*i is greater than or equal to x.
int i = 1, result = 1;
while (result <= x)
{
i++;
result = i * i;
}
return (i - 1)*(i-1);
}
// Driver program
int main()
{
int x ;
cin>>x;
cout << floorSqrt(x) << endl;
return 0;
}
| 0 |
#include <iostream>
#include <math.h>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
ll n;
cin>>n;
ll ans=0;
for(ll i=1; i*i<=n; i++){
ans=i*i;
}
cout << ans;
} | #include <bits/stdc++.h>
using namespace std;
int main(){
int n; cin >> n;
vector<bool> p(60000,true);
for(int i = 2; i < 55560; i++){
if(!p[i]) continue;
for(int j = 2; j*i < 55560; j++){
p[i*j] = false;
}
}
vector<int> fp;
for(int i = 2; i < 55560; i++){
if(p[i] && i % 5 == 1) fp.push_back(i);
}
for(int i = 0; i < n; i++){
cout << fp[i] << " ";
}
} | 0 |
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <cassert>
typedef long long ll;
using namespace std;
#define mod 1000000007 //1e9+7
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 30
int mm[SIZE][SIZE];
int w,h;
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
int dfs(int y,int x,int depth = 0){
int ret = INF;
if(depth >= 10) return INF;
//
for(int i=0;i<4;i++){
int tx = x + dx[i];
int ty = y + dy[i];
if(ty<0 || h<=ty || tx<0 || w<=tx || mm[ty][tx]==1) continue;
if(mm[ty][tx]==3) return depth+1;
while(1){
tx += dx[i];
ty += dy[i];
if(ty<0 || h<=ty || tx<0 || w<=tx) break;
if(mm[ty][tx]==3) return depth+1;
if(mm[ty][tx]==1){
mm[ty][tx] = 0;
ret = min(ret,dfs(ty-dy[i],tx-dx[i],depth+1));
mm[ty][tx] = 1;
break;
}
}
}
return ret;
}
int calc(){
int sy,sx;
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
scanf("%d",mm[i]+j);
if(mm[i][j]==2){
sy = i;
sx = j;
}
}
}
int ans = dfs(sy,sx);
if(ans==INF) ans = -1;
return ans;
}
int main(){
while(1){
scanf("%d%d",&w,&h);
if(w==0) break;
printf("%d\n",calc());
}
return 0;
} | #include<iostream>
#include<queue>
#include<cstring>
using namespace std;
queue<int>Q;
int z[100][100], x[100][100], sum; char a;
void BFS(int col,int x1,int y1) {
Q.push(x1 * 100 + y1);
x[x1][y1] = col;
int a, b;
while (!Q.empty()) {
a = Q.front() / 100;
b = Q.front() % 100;
if (z[a + 1][b] == 1 && x[a + 1][b] == 0) {
Q.push(Q.front() + 100);
x[a + 1][b] = col;
}
if (z[a - 1][b] == 1 && x[a - 1][b] == 0) {
Q.push(Q.front() - 100);
x[a - 1][b] = col;
}
if (z[a][b + 1] == 1 && x[a][b + 1] == 0) {
Q.push(Q.front() + 1);
x[a][b + 1] = col;
}
if (z[a][b - 1] == 1 && x[a][b - 1] == 0) {
Q.push(Q.front() - 1);
x[a][b - 1] = col;
}
Q.pop();
}
}
int main() {
while (cin >> a) {
if (a == '1') { z[1][1] = 1; }
sum = 0;
for (int i = 1; i <= 12; i++) {
for (int j = 1; j <= 12; j++) {
if (i + j != 2) {
cin >> a; if (a == '1') { z[i][j] = 1; }
}
}
}
for (int i = 1; i <= 12; i++) {
for (int j = 1; j <= 12; j++) {
if (x[i][j] == 0 && z[i][j] == 1) {
sum++;
BFS(sum, i, j);
}
}
}
cout << sum << endl;
memset(x, 0, sizeof(x));
memset(z, 0, sizeof(z));
}
} | 0 |
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define FOR(i, a, b) for(int i=(a);i<(b);i++)
#define RFOR(i, a, b) for(int i=(b-1);i>=(a);i--)
#define REP(i, n) for(int i=0; i<(n); i++)
#define RREP(i, n) for(int i=(n-1); i>=0; i--)
#define REP1(i, n) for(int i=1; i<=(n); i++)
#define RREP1(i, n) for(int i=(n); i>=1; i--)
#define ALL(a) (a).begin(),(a).end()
#define UNIQUE_SORT(l) sort(ALL(l)); l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define out(...) printf(__VA_ARGS__)
int dxy[] = {0, 1, 0, -1, 0};
void solve();
signed main()
{
#if DEBUG
std::ifstream in("input.txt");
std::cin.rdbuf(in.rdbuf());
#endif
cin.tie(0);
ios::sync_with_stdio(false);
solve();
return 0;
}
/*================================*/
#if DEBUG
#define SIZE 100
#else
#define SIZE 123450
#endif
int N,M,K;
const int MAX = 5e5;
const int MOD = 1e9+7;
long long fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++){
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
long long COM(int n, int k){
if (n < k) return 0;
if (n < 0 || k < 0) return 0;
if (!fac[n]) COMinit();
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
void solve() {
cin>>N>>M>>K;
int ans = 0;
REP1(d, N-1) {
int add = d*(N-d) % MOD;
(add *= M*M)%=MOD;
(ans += add)%=MOD;
}
REP1(d, M-1) {
int add = d*(M-d) % MOD;
(add *= N*N)%=MOD;
(ans += add)%=MOD;
}
(ans *= COM(N*M-2,K-2))%=MOD;
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
#ifdef zxc
#include <sys/resource.h>
#include "debug.h"
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
using ll = int64_t;
using ld = long double;
const ld EPS = 1e-9;
//const ll MOD = 924844033;
const ld PI = 3.141592653589793;
const int maxn = 300001;
const ll MOD = 1e9 + 7;
void fast_io() {
ios_base::sync_with_stdio(false);
cin.tie(0);
}
ll pw(ll a, ll b) {
ll res = 1;
for (; b; b >>= 1) {
if (b & 1) {
res = res * a % MOD;
}
a = a * a % MOD;
}
return res;
}
template<typename Cont>
vector<vector<int>> accum2d(Cont a, int n, int m) {
vector<vector<int>> res(n, vector<int>(m));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
res[i][j] = a[i][j];
if (i > 0) {
res[i][j] += res[i - 1][j];
}
if (j > 0) {
res[i][j] += res[i][j - 1];
}
if (i > 0 && j > 0) {
res[i][j] -= res[i - 1][j - 1];
}
}
}
return res;
}
vector<vector<int>> resize2d(vector<vector<int>> a, int n, int m) {
a.resize(n);
for (auto &x:a) {
x.resize(m);
}
return a;
}
int up[1030][1030];
int down[1030][1030];
int lft[1030][1030];
int rght[1030][1030];
int ans[1030][1030];
int pref[1030][1030];
int arifm[1030][1030];
int dp1[1030][1030];
int dp2[1030][1030];
signed main() {
#ifdef zxc
struct rlimit rl;
const rlim_t kStackSize = 512L * 1024L * 1024L;
assert(!getrlimit(RLIMIT_STACK, &rl));
rl.rlim_cur = kStackSize;
assert(!setrlimit(RLIMIT_STACK, &rl));
freopen("../kolya_input.txt", "r", stdin);
// freopen("../kolya_output.txt", "w", stdout);
#else
// mt19937 mt(std::chrono::high_resolution_clock::now().time_since_epoch().count());
// freopen("", "r", stdin);
// freopen("hack.out", "w", stdout);
#endif
auto solve = [](int _) {
int n;
cin >> n;
string s;
cin >> s;
vector<int> v;
for (char c:s) {
v.push_back(c == 'B' ? 1 : 0);
}
bool xx = true;
for (int i = 2 * n - 1; i >= 0; --i) {
if (xx) {
v[i] ^= 1;
}
xx ^= 1;
}
ll cock = 1;
ll cnt_zeros = 0;
for (int i = 2 * n - 1; i >= 0; --i) {
if (v[i]) {
cock *= cnt_zeros;
cock %= MOD;
--cnt_zeros;
} else {
++cnt_zeros;
}
}
if (cnt_zeros) {
cock = 0;
}
for (int i = 1; i <= n; ++i) {
cock *= i;
cock %= MOD;
}
cout << cock;
};
fast_io();
cout.precision(9);
cout << fixed;
auto start = chrono::steady_clock::now();
for (int i = 1; i <= 1; ++i)solve(i);
auto end = chrono::steady_clock::now();
debug("time", chrono::duration_cast<chrono::milliseconds>(end - start).count() / 1000.0);
}
| 0 |
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
#define loop(i,a,b) for(int i=(a); i<(int)(b); i++)
#define rep(i,n) loop(i,0,n)
int main(void){
string ro;
int m[256]={};
m['I']=1,
m['V']=5,
m['X']=10,
m['L']=50,
m['C']=100,
m['D']=500,
m['M']=1000;
while(cin>>ro){
int n=0;
for(auto c:ro)n+=m[c];
rep(i,ro.size()) if(m[ro[i]]<m[ro[i+1]])n-=2*m[ro[i]];
cout<<n<<endl;
}
} | //ヘッダー
#include<bits/stdc++.h>
using namespace std;
//型定義
typedef long long ll;
//定数
const int INF=1e+9;
const int MOD=1e+9+7;
//REPマクロ
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define REP2(i,a,b) for(ll i=a;i<(ll)(b);i++)
#define REPD2(i,a,b) for(ll i=a;i>(ll)(b);i--)
// 多次元 vector 生成
template<class T>
vector<T> make_vec(size_t a){
return vector<T>(a);
}
template<class T, class... Ts>
auto make_vec(size_t a, Ts... ts){
return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
//vectorの扱い
#define ALL(x) (x).begin(),(x).end() //sortなどの引数省略
#define SIZE(x) ((ll)(x).size()) //size
#define MAX(x) *max_element(ALL(x)) //最大値
#define MIN(x) *min_element(ALL(x)) //最小値
int main(){
string A;
cin>>A;
int N=A.size();
vector<ll> cnt(26,0);
vector<ll> dp(N+1,0);
dp[0]=1;
REP(i,N){
dp[i+1]=dp[i];
dp[i+1]+=(i-cnt[A[i]-'a']);
cnt[A[i]-'a']++;
}
cout<<dp[N]<<endl;
} | 0 |
#include<iostream>
#include<cstring>
#include<algorithm>
#define Dist(x,y) ((x)*(x)+(y)*(y))
using namespace std;
typedef struct{
int height;
int weight;
} Rectangle;
bool operator<(Rectangle r1,Rectangle r2){
int di1=Dist(r1.height,r1.weight);
int di2=Dist(r2.height,r2.weight);
if(di1 < di2) return true;
else if(di1 > di2) return false;
else return (r1.height < r2.height);
}
int main(){
int h,w;
while(cin>>h>>w,w+h){
Rectangle r;
r.height=h;
r.weight=w;
Rectangle min;
min.height=150;
min.weight=150;
for(int i=1;i<150;i++){
for(int j=1;j<i;j++){
Rectangle tmp;
tmp.height=j;
tmp.weight=i;
if(r < tmp && tmp < min) min = tmp;
}
}
cout<<min.height<<" "<<min.weight<<endl;
}
return 0;
} | #include<iostream>
#include<iomanip>
#include<string>
#include<algorithm>
#include<vector>
#include<utility>
#include<tuple>
#include<map>
#include<queue>
#include<stack>
#include<deque>
#include<bitset>
#include<math.h>
using namespace std;
using ll = int64_t;
using Graph = vector<vector<int> >;
const ll M = 1000000007;
int main(){
ll l,r;
cin >> l >> r;
if(l+1==r){
cout << ((l%2019)*(r%2019))%2019 << endl;
return 0;
}
if(l==0){
cout << 0 << endl;
return 0;
}
if(r-l+1>=673){
cout << 0 << endl;
}else{
int m=2018;
for(int i=l;i<r;i++){
for(int j=i+1;j<=r;j++){
m = min(m,((i%2019)*(j%2019))%2019);
}
}
cout << m << endl;
}
} | 0 |
#include <stdio.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <vector>
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main(){
string x;
cin >> x;
int n = x.length();
if(n == 2){
if(x[0] == x[1]){
printf("1 2");
}
else {
printf("-1 -1");
}
return 0;
}
for(int i = 0; i < n-2; i++){
if(x[i] == x[i + 1] || x[i + 1] == x[i + 2] || x[i + 2] == x[i]){
printf("%d %d", i+1, i+3);
return 0;
}
}
printf("-1 -1");
return 0;
}
| #include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define OP ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define F first
#define S second
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for(int i = 0; i < n; ++i)
#define repr(i,n) for(int i = n - 1; i >= 0; --i)
#define Rep(i, a, n) for(int i = (a); i <=(n); ++i)
#define repst(i, n) for(auto it = n.begin(); it != n.end(); ++it)
#define Repr(i, a, n) for(int i = (n); i >= (a); --i)
typedef long long ll;
typedef std::vector<int> vi;
typedef std::pair<int, int> pii;
const int inf = int(1e9);
const int mod = inf + 7;
const double PI = acos(-1.0);
using namespace std;
const int N = 1e5 + 155;
template<typename T>
inline void read(T&x){
x=0;int f=1;char ch = getchar();
while(ch<'0' ||ch>'9'){ if(ch=='-')f=-1;ch=getchar(); }
while(ch>='0' && ch<='9'){ x=x*10+ch-'0'; ch=getchar(); }
x*=f;
}
ll bp(ll a, ll n){
ll r = 1;
while(n){
if(n & 1) r = (r * a) % mod;
a = (a * a) % mod;
n >>= 1;
}
return r;
}
void solve(){
string s;
cin >> s;
unordered_map<char, vector<int>> m;
rep(i, sz(s)) {
m[s[i]].pb(i + 1);
}
for(auto it: m) {
vector<int> v = it.second;
for(int i = 0; i < v.size() - 1; i++) {
if (v[i + 1] - v[i] <= 2) {
cout << v[i] << ' ' << v[i + 1];
return;
}
}
}
cout << "-1 -1";
}
int main(){
OP
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int t = 1;
//cin >> t;
for(int i = 1; i <= t; i++) {
solve();
}
return 0;
} | 1 |
#pragma GCC optimze("Ofast")
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define mod 1000000007
#define md 998244353
#define trace(...)
#define ll long long int
#define ld long double
#define fi first
#define se second
#define fr(i,a,b) for(ll i=a;i<=b;i++)
#define frr(i,b,a) for(ll i=b;i>=a;i--)
#define mem(a) memset(a, 0, sizeof(a))
#define vpi std::vector<pair<int,int>>
#define pii pair<int,int>
#define pb push_back
#define sall(v) sort(v.begin(),v.end())
#define all(v) (v.begin(),v.end())
#define minf -10000000
#define vi vector<int>
const ll infl=0x3f3f3f3f3f3f3f3fLL;
const int infi=0x3f3f3f3f;
typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> oset;
auto clk=clock();
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) {
uniform_int_distribution<int> uid(0,lim-1);
return uid(rang);
}
ll power(ll x, ll y,ll p) {
ll res = 1;
x = x % p;
while (y > 0) {
if (y & 1){
res = (res*x) % p;
}
y = y>>1;
x = (x*x) % p;
}
return res;
}
ll power(ll x, ll y) {
ll res = 1;
while (y > 0) {
if (y & 1)
res = (res*x) ;
y = y>>1;
x = (x*x);
}
return res;
}
ll max3(ll a,ll b,ll c){
return max(a,max(b,c));
}
ll min3(ll a,ll b,ll c){
return min(a,min(b,c));
}
bool isPrime(ll n) { // Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (ll i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
const int nax=2e5+5;
void solve(){
int n;cin>>n;
vector<ll> v(n);fr(i,0,n-1)cin>>v[i];
ll cnt=0,mx=v[0];
fr(i,1,n-1){cnt+=max(0ll,mx-v[i]);mx=max(mx,v[i]);}
cout<<cnt<<endl;
return;
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
srand(chrono::high_resolution_clock::now().time_since_epoch().count());
cout<<fixed<<setprecision(8);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int t;
// cin>>t;
t=1;
while(t--){
solve();
}
#ifndef ONLINE_JUDGE
cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
#endif
return 0;
} | #include <iostream>
#define ll long long
using namespace std;
int main () {
int q,p = 0,n; cin >> q; ll ans = 0;
while (q--){
cin >> n;
if (n<p) ans += p-n;
else p=n;
} cout << ans << "\n";
return 0;
} | 1 |
/*
Author:zeke
pass System Test!
GET AC!!
*/
#include <iostream>
#include <queue>
#include <vector>
#include <iostream>
#include <vector>
#include <string>
#include <cassert>
#include <algorithm>
#include <functional>
#include <cmath>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <map>
#include <iomanip>
#include <math.h>
#include <utility>
#include <stack>
#include <bitset>
using ll = long long;
using ld = long double;
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define rep3(var, min, max) for (ll(var) = (min); (var) < (max); ++(var))
#define repi3(var, min, max) for (ll(var) = (max)-1; (var) + 1 > (min); --(var))
#define Mp(a, b) make_pair((a), (b))
#define F first
#define S second
#define Icin(s) \
ll(s); \
cin >> (s);
#define Scin(s) \
ll(s); \
cin >> (s);
template <class T>
bool chmax(T &a, const T &b)
{
if (a < b)
{
a = b;
return 1;
}
return 0;
}
template <class T>
bool chmin(T &a, const T &b)
{
if (b < a)
{
a = b;
return 1;
}
return 0;
}
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef vector<V> VV;
typedef vector<P> VP;
ll mod = 1e9 + 7;
unsigned long long MOD = 1e9 + 7;
ll INF = 1e18;
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
int n; // 頂点数
cin >> n;
ll m;
cin >> m;
m = n - 1 + m; // 辺数
vector<pair<int, int>> edge(n); // first -> second
// cout << "hello" << endl;
vector<vector<int>> eList(n); // 隣接リスト
vector<int> inDeg(n, 0); // 入次数
rep(i,m)
{
ll A,B;
cin >> A >> B;
A--;
B--;
eList[A].push_back(B);
inDeg[B]++;
}
queue<int> zeroQue; // 入次数 0 の頂点でまだ取られていないものを入れる
for (int i = 0; i < n; i++)
{
if (inDeg[i] == 0){
zeroQue.push(i);
}
}
// cout << "hello" << endl;
V Result(n);
vector<int> result; // トポロジカルソートの結果を入れる
while (!zeroQue.empty())
{
int u = zeroQue.front(); // 入次数 0 の頂点 u を 1 つ取る
zeroQue.pop();
result.push_back(u);
for (int v : eList[u])
{ // u を始点とする辺すべてについて、
inDeg[v]--; // 終点の入次数を減らす
if (inDeg[v] == 0){
zeroQue.push(v); // 入次数が 0 になったら取ることができる
Result[v] = u+1;
// cout<<u<<endl;
}
}
}
// cout << "hello" << endl;
rep(i, n)
{
cout << Result[i] << endl;
}
}
| //#include <bits/stdc++.h>
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
//#include "boost/multiprecision/cpp_int.hpp"
//typedef boost::multiprecision::cpp_int ll;
typedef long double dd;
#define i_7 (ll)(1E9+7)
//#define i_7 998244353
#define i_5 i_7-2
ll mod(ll a){
ll c=a%i_7;
if(c>=0)return c;
return c+i_7;
}
typedef pair<ll,ll> l_l;
ll inf=(ll)1E16;
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define pb push_back
ll max(ll a,ll b){if(a<b)return b;else return a;}
ll min(ll a,ll b){if(a>b)return b;else return a;}
void Max(ll &pos,ll val){pos=max(pos,val);}//Max(dp[n],dp[n-1]);
void Min(ll &pos,ll val){pos=min(pos,val);}
void Add(ll &pos,ll val){pos=mod(pos+val);}
dd EPS=1E-9;
#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define fi first
#define se second
////////////////////////////
ll n,m;
#define N 1005
vector<ll>v[N];
ll used[N];
vector<ll>row;
vector<ll>cyc;
bool dfs(ll k){
if(cyc.size()!=0)return 0;
for(auto x:v[k]){
if(used[x]==1){
if(cyc.size()!=0)return 0;
bool fl=false;
rep(i,0,row.size()-1){
if(fl)cyc.pb(row[i]);
else if(row[i]==x){
cyc.pb(row[i]);fl=true;
}
}
return 0;
}else if(used[x]==0){
row.pb(x);
used[x]=1;
dfs(x);
row.pop_back();
}
}
return 0;
}
int main(){fastio
cin>>n>>m;
rep(i,1,m){
ll a,b;cin>>a>>b;
v[a].pb(b);
}
rep(i,1,n){
if(used[i]==0){
row.pb(i);
dfs(i);
rep(j,1,n){
if(used[j]==1)used[j]=2;
}
}
}
if(cyc.size()==0){
cout<<-1;return 0;
}
rep(i,1,n)sort(v[i].begin(),v[i].end());
bool ok[n+1];memset(ok,false,sizeof(ok));
while(1){
bool flag=true;
vector<ll>newcyc;
ll cs=cyc.size();
rep(i,0,cs-1){
if(!flag)break;
ll top=cyc[i];
if(ok[top])continue;
for(ll qj=(i+cs-1);qj>=i+2;qj--){
ll j=qj%cs;
if(*lower_bound(v[top].begin(),v[top].end(),cyc[j])==cyc[j]){
if(i<j){
rep(k,0,i)newcyc.pb(cyc[k]);
rep(k,j,cs-1)newcyc.pb(cyc[k]);
}else{
rep(k,j,i)newcyc.pb(cyc[k]);
}
ok[top]=true;
flag=false;
break;
}
}
}
if(flag){
cout<<cyc.size()<<endl;
for(auto x:cyc)cout<<x<<endl;
return 0;
}else{
cyc.clear();
for(auto x:newcyc)cyc.pb(x);
}
}
return 0;
}
| 0 |
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
template <typename TYPE>
void print_vec(const vector<TYPE>& v){
for(int i=0; i<v.size(); i++){
cout << v[i] << " ";
}
cout << endl;
}
template <typename TYPE>
void print_vec2(const vector<vector<TYPE>>& v){
cout << endl; cout << " ";
for(int i=0; i<v[0].size(); i++) cout << i << " ";
cout << endl;
for(int i=0; i<v.size(); i++){
cout << "i=" << i << ": ";
for(int j=0; j<v[i].size(); j++){
if(v[i][j] == 0) cout << "\x1B[0m" << v[i][j] << " ";
else cout << "\x1B[31m" << v[i][j] << " ";//https://stackoverrun.com/ja/q/12618775
}
cout << "\x1B[0m" << endl;
}
}
using P = pair<int ,int>;
using LP = pair<ll, int>;
int main(){
int N;
ll M;
cin >> N >> M;
vector<ll> divs;
for(ll i=1; i*i<=M; i++){
if(M%i == 0){
divs.push_back(i);
if(i*i != M)divs.push_back(M/i);
}
}
sort(divs.begin(), divs.end());
ll ans = 0;
for(int i=0; i<(int)divs.size(); i++){
if(M/divs[i] >= N) ans = max(ans, divs[i]);
}
// cout << " divs: "; print_vec(divs);
cout << ans << endl;
// vector<LP> prime_divisors;
// ll m = M;
// //エラトステネスの篩
// for(ll p=2; p*p<m; p++){
// if(m % p != 0) continue;
// int cnt = 0;
// while(m%p == 0){
// m /= p;
// cnt++;
// }
// prime_divisors.emplace_back(p, cnt);
// }
// if(m != 1) prime_divisors.emplace_back(m, 1);
// vector<ll> divs;// Mの因数となるモノを格納
// ll ans = 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e16;
const ll mod = 1000000007;
#define rep(i, n) for (int i = 0; i < (ll)(n); i++)
int main() {
ll n, m; cin >> n >> m;
ll res = 1;
for (ll i = 1; i*i <= m; i++) {
if (m%i == 0) {
if (i*n <= m) res = max(res, i);
if (m/i != i) {
if (m/i*n <= m) res = max(res, m/i);
}
}
}
cout << res << endl;
} | 1 |
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, j, k) for(int i = j; i < k; ++i)
#define rep(i, j) FOR(i, 0, j)
#define repr(i, j) for(int i = j; i >= 0; --i)
#define INF (1 << 30)
#define MOD 1e9 + 7
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
int dp[1001][1001];
int main() {
string s1, s2;
cin >> s1 >> s2;
int A = s1.size(), B = s2.size();
rep(i, A + 1) dp[i][0] = i; rep(i, B + 1) dp[0][i] = i;
FOR(i, 1, A + 1) FOR(j, 1, B + 1) {
int cost = (s1[i - 1] == s2[j - 1]) ? 0 : 1;
int tmp = min(dp[i - 1][j] + 1, dp[i][j - 1] + 1);
dp[i][j] = min(tmp, dp[i - 1][j - 1] + cost);
}
cout << dp[A][B] << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
ll GetDigit(ll num){ return log10(num)+1; } //numの桁数を求める
int main()
{
int n,l;
cin >> n >> l;
vector<int> taste(n);
rep(i,n){
taste[i] = l + i;
}
int total = 0;
rep(i,n) total += taste[i];
if(l >= 0) total -= taste[0];
else if(n+l <= 0) total -= taste[n-1];
cout << total << endl;
return 0;
} | 0 |
#include<bits/stdc++.h>
using namespace std;
const int maxn=200000;
int sq[555555],d1,d2,xx,yy,p[555555],cnt,n,col[555555],msk[555555],pos,sum[5],id;
int cnt1,cnt2,cpt,pa[555555],val[555555];
pair<int,int> v1[1111],v2[1111];
int root(int x)
{
if (pa[x]==x) return x;
int rt=root(pa[x]);
val[x]^=val[pa[x]];
return pa[x]=rt;
}
void Union(int x,int y)
{
int rx=root(x),ry=root(y);
if (rx==ry) return;
pa[rx]=ry;val[rx]=(1^val[x]^val[y]);
}
void Init()
{
memset(sq,-1,sizeof(sq));
for (int i=0;i<=500;i++) sq[i*i]=i;
}
int get(int x,int y)
{
return (x-1)*n+y;
}
bool In(int x,int y)
{
return (x>=1 && x<=n && y>=1 && y<=n);
}
void addedge(int x,int y)
{
Union(x,y);
}
int main()
{
scanf("%d%d%d",&n,&d1,&d2);
Init();n<<=1;
for (int k=0;k<=n;k++)
{
if (d1-k*k>=0 && ~sq[d1-k*k])
{
v1[++cnt1]=make_pair(k,sq[d1-k*k]);
}
if (d2-k*k>=0 && ~sq[d2-k*k])
{
v2[++cnt2]=make_pair(k,sq[d2-k*k]);
}
}
for (int i=1;i<=n*n;i++)
{
pa[i]=i;
val[i]=0;
}
for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
{
id=get(i,j);
for (int k=1;k<=cnt1;k++)
{
xx=i+v1[k].first;yy=j+v1[k].second;
if (In(xx,yy)) addedge(id,get(xx,yy));
xx=i-v1[k].first;yy=j+v1[k].second;
if (In(xx,yy)) addedge(id,get(xx,yy));
}
}
}
for (int i=1;i<=n*n;i++)
{
root(i);
col[i]=val[i];
}
for (int i=1;i<=n*n;i++) msk[i]=col[i];
for (int i=1;i<=n*n;i++)
{
pa[i]=i;
val[i]=0;
}
for (int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
{
id=get(i,j);
for (int k=1;k<=cnt2;k++)
{
xx=i+v2[k].first;yy=j+v2[k].second;
if (In(xx,yy)) addedge(id,get(xx,yy));
xx=i-v2[k].first;yy=j+v2[k].second;
if (In(xx,yy)) addedge(id,get(xx,yy));
}
}
}
for (int i=1;i<=n*n;i++)
{
root(i);
col[i]=val[i];
}
for (int i=1;i<=n*n;i++) msk[i]|=(col[i]<<1);
for (int i=1;i<=n*n;i++) sum[msk[i]]++;
for (int i=0;i<4;i++)
{
if (sum[i]>=n*n/4)
{
pos=i;
break;
}
}
for (int i=1;i<=n*n;i++)
{
if (cnt==n*n/4) break;
if (msk[i]==pos)
{
printf("%d %d\n",(i-1)/n,(i-1)%n);
cnt++;
}
}
return 0;
} | #include <cstdio>
typedef long long ll;
#define ri register int
namespace io {
const int SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr;
// getchar
#define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
// print the remaining part
inline void flush () {
fwrite (obuf, 1, oS - obuf, stdout);
oS = obuf;
}
// putchar
inline void putc (char x) {
*oS ++ = x;
if (oS == oT) flush ();
}
// input a signed integer
template <class I>
inline void gi (I &x) {
for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;
for (x = 0; c <= '9' && c >= '0'; c = gc()) x = (x << 1) + (x << 3) + (c & 15); x *= f;
}
// print a signed integer
template <class I>
inline void print (I x) {
if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;
while (x) qu[++ qr] = x % 10 + '0', x /= 10;
while (qr) putc (qu[qr --]);
}
//no need to call flush at the end manually!
struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
}
using io :: gi;
using io :: putc;
using io :: print;
const int N=605;
int n,m,col1[N][N],col2[N][N],tag[2][2];
inline void solve(ri d,int col[N][N]){
ri i,j,k;
k=0;
while(d%4==0) d>>=2,++k;
if(d&1){
for(i=0;i<m;++i)
for(j=0;j<m;++j)
if((i>>k)+(j>>k)&1)
col[i][j]=1;
}
else{
for(i=0;i<m;++i)
for(j=0;j<m;++j)
if((i>>k)&1)
col[i][j]=1;
}
}
int main(){
ri d1,d2,i,j,x,y,cnt;
gi(n);gi(d1);gi(d2);
m=n<<1;
solve(d1,col1);
solve(d2,col2);
for(i=0;i<m;++i)
for(j=0;j<m;++j)
++tag[col1[i][j]][col2[i][j]];
for(i=0;i<=1;++i)
for(j=0;j<=1;++j)
if(tag[i][j]>=n*n){
cnt=0;
for(x=0;x<m;++x)
for(y=0;y<m;++y)
if(col1[x][y]==i&&col2[x][y]==j){
print(x);putc(' ');print(y);putc('\n');
if(++cnt==n*n) return 0;
}
}
} | 1 |
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main()
{
string s;
cin >> s;
map<char, int> dic;
rep(i, 26) dic[i + 'a'] = 0;
int n = s.size();
rep(i, n) dic[s[i]]++;
rep(i, 26)
{
if (dic['a' + i] < 1)
{
cout << char('a' + i) << endl;
return 0;
}
}
cout << "None" << endl;
return 0;
} | #include<iostream>
#include<set>
#include<map>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long li;
#define repa(i,a,n) for(int i=(a);i<(n);i++)
#define yn(i) print((i)?"Yes":"No")
#define rep(i,n) for(int i=0;i<(n);i++)
#define df 0
template<class T> void print(const T& t){ cout << t << "\n"; }
template<class T, class... Ts> void print(const T& t, const Ts&... ts) { cout << t; if (sizeof...(ts)) cout << " "; print(ts...); }
// pair 型
template<class S,class T>
std::ostream& operator<<(std::ostream& os, const pair<T,S>& p){
cout << "(" << p.first << "," << p.second << ")";
return os;
}
// Container コンテナ型, map以外
template< template<class ...> class Ctn,class T>
std::ostream& operator<<(std::ostream& os,const Ctn<T>& v){
auto itr=v.begin();
while(itr!=v.end()){
if(itr!=v.begin())cout << " ";
cout << *(itr++);
}
return os;
}
// map 型
template<class S,class T>
std::ostream& operator<<(std::ostream& os, const map<S,T>& mp){
for(const pair<S,T>& x: mp){
cout << "[" << x.first << "]=" << x.second << " ";
}
return os;
}
int main(){
set<char> st;
rep(i,26) st.insert('a'+i);
string s; cin >>s;
int n=s.size();
rep(i,n) st.erase(s[i]);
if(st.empty()){
print("None");
}else{
print(*st.begin());
}
}
| 1 |
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream>
#include <random>
#include<map>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cassert>
#include<fstream>
#include <unordered_map>
#include <cstdlib>
#include <complex>
using namespace std;
#define Ma_PI 3.141592653589793
#define eps 0.00000001
#define LONG_INF 30000000000000LL
#define GOLD 1.61803398874989484820458
const long long MAX_MOD = 1e6 + 3;
#define REP(i,n) for(long long i = 0;i < n;++i)
#define seg_size 524288
long long powering(long long now,long long now_go){
long long ans = 1;
while (now_go != 0) {
if (now_go % 2 == 1) {
ans *= now;
ans %= MAX_MOD;
}
now *= now;
now %= MAX_MOD;
now_go /= 2;
}
return ans;
}
long long inv(long long now) {
return powering(now, MAX_MOD - 2LL);
}
long long d;
long long geko[32] = {};
long long bobo[30] = {};
long long solve(long long now, long long now_digit, long long L) {
if (L / 2 == now_digit) {
if (L % 2 == 0) {
if (d == now) {
return 1;
}
else {
return 0;
}
}
else {
if (d == now) {
return 10LL;
}
else {
return 0LL;
}
}
}
long long ans = 0;
for (long long i = -9; i <= 9; ++i) {
long long tea = geko[L - now_digit - 1] - geko[now_digit];
tea *= i;
tea += now;
if (tea >= 0) {
if (tea % geko[now_digit + 1] == d % geko[now_digit + 1]) {
//ok
long long geko = solve(tea, now_digit + 1, L);
if (geko == 0) continue;
if (now_digit == 0) {
bobo[i + 9]--;
}
ans += geko * bobo[i+9];
if (now_digit == 0) {
bobo[i + 9]++;
}
}
}
else {
tea += geko[18];
if (tea % geko[now_digit + 1] == d % geko[now_digit + 1]) {
long long geko = solve(tea - bobo[18], now_digit + 1, L);
if (geko == 0)continue;
if (now_digit == 0) {
bobo[i + 9]--;
}
ans += geko * bobo[i + 9];
if (now_digit == 0) {
bobo[i + 9]++;
}
}
tea -= geko[18];
}
}
return ans;
}
int main() {
for (int i = 0; i <= 9; ++i) {
for (int q = 0; q <= 9; ++q) {
bobo[i - q + 9]++;
}
}
geko[0] = 1;
for (int i = 1; i <= 18; ++i) {
geko[i] = 10LL * geko[i-1];
}
cin >> d;
if (d % 9 != 0) {
cout << 0 << endl;
return 0;
}
int len = to_string(d).length();
long long ans = 0;
for (long long digit = len; digit <= len * 2; ++digit) {
ans += solve(0, 0, digit);
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)
#define ALL(v) (v).begin(),(v).end()
#define CLR(t,v) memset(t,(v),sizeof(t))
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";}
template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<" ";cout<<endl;}
template<class T>void chmin(T&a,const T&b){if(a>b)a=b;}
template<class T>void chmax(T&a,const T&b){if(a<b)a=b;}
ll nextLong() { ll x; scanf("%lld", &x); return x;}
ll ten[20];
ll val = 0;
void f(int L, int R, ll D, ll cur) {
if (L > R) {
if (D == 0) val += cur;
return;
}
if (abs(D) > ten[R+1]) return;
// cout << L << " " << R << " " << D << endl;
if (L == R) {
f(L+1, R-1, D, cur * 10);
} else {
for (int d = -9; d <= 9; d++) {
ll D2 = D - (ten[R] - ten[L]) * d;
ll ways = 10 - abs(d);
if (L == 0) ways = 9 - abs(d);
f(L+1, R-1, D2, cur * ways);
}
}
}
int main2() {
ll D = nextLong();
ten[0] = 1;
REP(i, 19) ten[i+1] = ten[i] * 10;
ll ans = 0;
for (int k = 1; k <= 18; k++) {
val = 0;
f(0, k-1, D, 1);
ans += val;
}
cout << ans << endl;
return 0;
}
int main() {
#ifdef LOCAL
for (;!cin.eof();cin>>ws)
#endif
main2();
return 0;
} | 1 |
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define pb push_back
#define COUT(x) cout << (x) << endl
#define COUTF(x) cout << setprecision(15) << (x) << endl
#define ENDL cout << endl
#define DF(x) x.erase(x.begin()) // 先頭文字削除
#define ALL(x) x.begin(), x.end()
#define SORT(x) sort(ALL(x))
#define REVERSE(x) reverse(ALL(x))
#define init() \
cin.tie(0); \
ios::sync_with_stdio(false)
#define debug(x) cerr << "[debug] " << #x << ": " << x << endl;
#define debugV(v) \
cerr << "[debugV] " << #v << ":"; \
rep(i, v.size()) cerr << " " << v[i]; \
cerr << endl;
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using P = pair<ll, ll>;
constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
constexpr double PI = 3.141592653589793238462643383279;
ll getDigit(ll x) {
return x == 0 ? 1 : log10(x) + 1;
}
ll gcd(ll x, ll y) {
return y ? gcd(y, x % y) : x;
}
ll lcm(ll a, ll b) {
return a / gcd(a, b) * b;
}
vector<P> factorize(ll n) {
vector<P> result;
for (ll i = 2; i * i <= n; ++i) {
if (n % i == 0) {
result.pb({i, 0});
while (n % i == 0) {
n /= i;
result.back().second++;
}
}
}
if (n != 1) {
result.pb({n, 1});
}
return result;
}
vll divisor(ll n) {
vll ret;
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n) ret.push_back(n / i);
}
}
SORT(ret);
return (ret);
}
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
signed main() {
init();
ll N;
cin >> N;
string S;
cin >> S;
ll count_W = 0;
ll count_E = 0;
rep(i, S.size()) {
if (S[i] == 'W') {
count_W++;
} else {
count_E++;
}
}
ll t_W = 0;
ll t_E = 0;
ll mini = INF;
rep(i, S.size()) {
if (S[i] == 'W') {
ll count = t_W + count_E - (t_E + 0);
if (count < mini) {
mini = count;
}
} else {
ll count = t_W + count_E - (t_E + 1);
if (count < mini) {
mini = count;
}
}
if (S[i] == 'W') {
t_W++;
} else {
t_E++;
}
}
COUT(mini);
return 0;
} | // C Attention
#include <bits/stdc++.h>
using namespace std;
vector<int> input(int N){
vector<int> vec;
int temp = 0;
for (int i = 0; i < N; i++ ) {
cin >> temp;
vec.push_back(temp);
}
return vec;
}
int main(){
int N; // 100
cin >> N;
string str;
cin >> str;
vector<int> east_num(N);
vector<int> west_num(N);
if (str.at(0)=='E'){
east_num.at(0) = 1;
west_num.at(0) = 0;
}else{
east_num.at(0) = 0;
west_num.at(0) = 1;
}
for (int i =0; i <N-1; i++) {
if(str.at(i+1) == 'E'){
east_num.at(i+1) = east_num.at(i)+1;
west_num.at(i+1) = west_num.at(i);
}
else{
east_num.at(i+1) = east_num.at(i);
west_num.at(i+1) = west_num.at(i)+1;
}
}
int min_val = east_num.at(N-1) - east_num.at(0);
for (int i =0; i <N-1; i++) {
int value = east_num.at(N-1) - east_num.at(i+1) + west_num.at(i);
if (value < min_val) min_val = value;
}
/*
for (int i =0; i <N; i++) {
cout << east_num.at(i) << " ";
}
cout << endl;
for (int i =0; i <N; i++) {
cout << west_num.at(i) << " ";
}
cout << endl;
*/
cout << min_val << endl;
} | 1 |
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
#define ALL(x) (x).begin(), (x).end()
#define REP(i ,n) for(int i = 0; i < (int)(n); i++)
#define pb push_back
typedef vector<int>vint;
typedef vector<ll>vll;
template<typename T> istream &operator>>(istream &is, vector<T> &vec){ for (auto &v : vec) is >> v; return is; }
template<typename A,typename B>inline bool chmin(A &a,const B &b){if(a>b){a=b;return true;}else{return false;}}
template<typename A,typename B>inline bool chmax(A &a,const B &b){if(a<b){a=b;return true;}else{return false;}}
int main()
{
int N,Y;
cin >> N >> Y;
int yen[] = {10000, 5000, 1000};
int total,sub;
REP(i,2001){
REP(j,2001){
total = yen[0]*i + yen[1]*j;
sub = Y-total;
if(0 != sub%yen[2] || sub < 0)break;
sub /= yen[2];
if(N == (i+j+sub)){
cout << i << " " << j << " " << sub << endl;
return 0;
}
}
}
cout << -1 << " " << -1 << " " << -1 << endl;
} | #include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
#define point pair<int,int>
int N, K;
int mem[200010];
signed main()
{
cin >> N >> K;
rep(i, N)cin >> mem[i];
rep(j, min(K, 100LL))
{
int tmp[400010] = {0};
rep(i, N)
{
tmp[max(i - mem[i], 0LL)]++;
tmp[i + mem[i] + 1]--;
}
rep(i, N)if(i)
tmp[i] += tmp[i - 1];
rep(i, N)mem[i] = tmp[i];
}
rep(i, N)
{
if(i)cout << " ";
cout << mem[i];
}cout << endl;
return 0;
}
| 0 |
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#include <math.h>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <iterator>
#include <sstream>
#include <string>
#include <bitset>
using namespace std;
#define FOR(I,F,N) for(int I = F; I < (int)(N); I++)
#define rep(i, n) FOR(i, 0, n)
#define FIN(V) cout<<V<<endl
#define pb push_back
#define INF (1 << 30)
typedef pair<int, int> P;
typedef long long ll;
typedef priority_queue<int> pq;
int StrToInt(string);
string IntToStr(int);
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int main(void){
map<string, bool> memo;
string s;
int n, m;
cin >> n;
rep(i, n){
cin >> s;
memo[s] = 1;
}
cin >> m;
int cnt = 0;
rep(i, m){
cin >> s;
if(memo[s])cnt++;
}
FIN(cnt);
return 0;
}
int StrToInt(string s){
stringstream ss;
ss << s;
int val;
ss >> val;
return val;
}
string IntToStr(int i){
stringstream ss;
ss << i;
return ss.str();
} | #include<bits/stdc++.h>
using namespace std;
int main(){
int N,Q;string S;cin>>N>>Q>>S;
vector<int>V(N);
int U=0;
for(int X=0;X<N;X++){
if(X!=0&&S[X]=='C'&&S[X-1]=='A'){
U++;
}
V[X]=U;
}
for(int X=0;X<Q;X++){
int A,B;cin>>A>>B;
if(A!=1&&V[A-2]!=V[A-1]){
cout<<V[B-1]-V[A-1]<<endl;
}
else{
cout<<V[B-1]-V[A-1]<<endl;
}
}
}
| 0 |
#include <iostream>
#include <fstream>
#include <list>
using namespace std;
class game{
int length;
int cur;
int *r,*l;
int r_score,l_score;
public:
game(int _l){
length = _l;
r = new int [length];
l = new int [length];
r_score = 0;
l_score = 0;
cur = 0;
}
~game(){
delete [] r;
delete [] l;
}
void run();
void insert(int,int);
};
void game::run(){
for(int i = 0;i < length;i++){
if(r[i] < l[i]){
l_score += r[i] + l[i];
}else if(r[i] > l[i]){
r_score += r[i] + l[i];
}else{
l_score += l[i];
r_score += r[i];
}
}
cout<<l_score<<" "<<r_score<<endl;
}
void game::insert(int l_card,int r_card){
r[cur] = r_card;
l[cur] = l_card;
cur++;
}
int main(){
list<game*> lst;
while(1){
int read;
cin>>read;
if(read == 0){
break;
}
game *g = new game(read);
for(int i = 0; i < read;i++){
int r_card,l_card;
cin>>l_card>>r_card;
//cout<<l_card<<","<<r_card<<endl;
g->insert(l_card,r_card);
}
lst.push_back(g);
}
for(auto it = lst.begin();it != lst.end();it++){
(*it)->run();
delete (*it);
}
return 0;
} | #include <iostream>
using namespace std;
int main() {
int n;
cin>>n;
while(n!=0){
int ap=0,bp=0;
int a,b;
for(int k=0;k<n;k++){
cin>>a>>b;
if(a>b){
ap=ap+a+b;
}
else if(a<b){
bp=bp+a+b;
}
else{
ap=ap+a;
bp=bp+a;
}
}
cout<<ap<<" "<<bp<<endl;
cin>>n;
}
return 0;
} | 1 |
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<cctype>
#include<complex>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<functional>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>
using namespace std;
const int dx[] = {1,0,-1,0},dy[] = {0,1,0,-1};
#define INF 999999
#define rep(i,j) for(int i=0;i<(j);++i)
#define reps(i,j,k) for(int i=j;i<k;++i)
typedef long long ll;
typedef unsigned long long ull;
bool us[100100]={false};
int p[100100]={0};
int main(){
/*Eratosthenes*/
p[2] = 2;
for(int i = 0; i < 100101 ; i+=2)us[i] = true;
for(int i = 3; i < 100101; i+=2){
if(us[i] == true)continue;
else if(us[i] == false){
p[i] = i;
for(int j = i+i; j < 100101; j += i)us[j] = true;
}
}
int n;
while(scanf("%d",&n),n){
for(int i = n; i >= 0; i--){
if(!us[i] && !us[i-2]){
printf("%d %d\n",p[i-2],p[i]);
break;
}
}
}
return 0;
} | #include <iostream>
using namespace std;
const int MAX = 10000;
int plist[MAX + 1] = {};
void primeNumber(){
//0 -> prime, 1 -> composite
plist[0] = 1;
plist[1] = 1;
for(int i = 2; i <= MAX; ++i){
if(plist[i] == 0){
int c = i * 2;
while(c <= MAX){
plist[c] = 1;
c += i;
}
}
}
}
int main(){
int n;
primeNumber();
while(cin >> n, n){
int k;
if(n % 2 == 0){
k = n - 1;
}else{
k = n;
}
while(k >= 3){
if(plist[k] == 0 && plist[k - 2] == 0){
cout << k - 2 << " " << k << endl;
break;
}
k -= 2;
}
}
return 0;
} | 1 |
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using Graph=vector<vector<pair<int,int>>>;
#define INF 1000000000000000000
#define MOD 1000000007
int N;
vector<vector<ll>> a;
vector<bool> flag;
vector<ll> dp;
ll solve(int S){
if(flag.at(S)){
return dp.at(S);
}
flag.at(S)=true;
ll ans=0;
for(int i=0;i<N;i++){
for(int j=i+1;j<N;j++){
if((((S>>i)&1)&((S>>j)&1))==1){
ans+=a.at(i).at(j);
}
}
}
for(int T=(S-1)&S;T>0;T=(T-1)&S){
ans=max(ans,solve(T)+solve(S^T));
}
return dp.at(S)=ans;
}
int main(){
cin>>N;
a.resize(N);
for(int i=0;i<N;i++){
a.at(i).resize(N);
for(int j=0;j<N;j++){
cin>>a.at(i).at(j);
}
}
flag.resize(1<<N,false);
dp.resize(1<<N,0);
cout<<solve((1<<N)-1)<<endl;
}
| #include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1e9 + 7;
int main(){
int n;
cin >> n;
vector<long long int> a(n);
for(int i=0; i<n; i++){
cin >> a[i];
}
long long int r = 0, g = 0, b = 0, ans = 1;
for(int i=0; i<n; i++){
if(a[i] == r && a[i] == g && a[i] == b){ ans *= 3; r += 1; }
else if(a[i] == r && a[i] == g){ ans *= 2; r += 1; }
else if(a[i] == g && a[i] == b){ ans *= 2; g += 1; }
else if(a[i] == r) r += 1;
else if(a[i] == g) g += 1;
else if(a[i] == b) b += 1;
else ans = 0;
ans %= MOD;
}
cout << ans << endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
#define cn count
#define pb push_back
#define in insert
#define fs first
#define sc second
#define sz size
#define len length
#define forn(i, n) for (ll i = 0; i < (ll) (n); ++i)
#define forni(i, n) for (ll i = 1; i < (ll) (n); ++i)
#define IO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
using ll = long long;
const ll inf = 1e9+7;
const double PI = 3.141592653589793;
int main(){
IO;
ll n,x,t;
cin>>n>>x>>t;
cout<<((n+x-1)/x)*t<<'\n';
return 0;
}
// _ .-') .-. .-') .-')
//( '.( OO )_ \ ( OO ) ( OO ).
// ,--. ,--.).-'),-----. ,--. ,--. .-'),-----. (_)---\_) ,--. ,--. .-----. ,-.-') .-'),-----.
// | `.' |( OO' .-. '| .' / ( OO' .-. '/ _ | | | | | ' .--./ | |OO)( OO' .-. '
// | |/ | | | || /, / | | | |\ :` `. | | | .-') | |('-. | | \/ | | | |
// | |'.'| |\_) | |\| || ' _)\_) | |\| | '..`''.) | |_|( OO )/_) |OO ) | |(_/\_) | |\| |
// | | | | \ | | | || . \ \ | | | |.-._) \ | | | `-' /|| |`-'| ,| |_.' \ | | | |
// | | | | `' '-' '| |\ \ `' '-' '\ /(' '-'(_.-'(_' '--'\(_| | `' '-' '
// """ """ """""""" """ """" """"""" """"""" """""""" """""" """ """""""
| //Mahir Ratanpara (DA-IICT)
#include<bits/stdc++.h>
using namespace std;
#define Ff(i,a,n) for(i=a;i<n;i++)
#define Fr(i,a,n) for(i=a;i>n;i--)
#define ll long long
#define FAST ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define pb push_back
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
typedef pair<ll, ll> pa;
typedef vector<ll> vc;
typedef vector<pa> vp;
typedef vector<vc> vvc;
int mpow(int base, int exp);
void ipgraph(ll n,ll m);
void dfs(int u, int par);
const int mod = 1000000007;
const int N = 3e5, M = N;
//=======================
vc g[N];
int main() {
FAST;
ll t, i, j, k, p, q, r, x, y, u, v, n, m;
cin>>n>>x>>t;
y=n/x;
if(n%x>0)
y++;
cout<<y*t;
return 0;
}
int mpow(int base, int exp) {
base %= mod;
int result = 1;
while (exp > 0) {
if (exp & 1) result = ((ll)result * base) % mod;
base = ((ll)base * base) % mod;
exp >>= 1;
}
return result;
}
void ipgraph(ll n, ll m){
int i, u, v;
while(m--){
cin>>u>>v;
g[u-1].pb(v-1);
g[v-1].pb(u-1);
}
}
void dfs(int u, int par){
for(int v:g[u]){
if (v == par) continue;
dfs(v, u);
}
} | 1 |
// TeamNichoen
// g++ -O2 -Wall -Wextra -Wshadow problemA.cpp
#include "bits/stdc++.h"
using namespace std;
typedef long long int LL;
typedef long long int ll;
#define FOR(i,a,b) for(LL i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
int solve(vector<vector<int> > &mp,int sx,int sy,int gx,int gy,int dir,int dep){
if(dep>=11){
return 9999;
}
if(mp[sx+dx[dir]][sy+dy[dir]]==0)
{
while(mp[sx+dx[dir]][sy+dy[dir]]==0){
sx+=dx[dir];
sy+=dy[dir];
if(sx==gx&&sy==gy){
return 0;//goal
}
}
if(mp[sx+dx[dir]][sy+dy[dir]]==1){
mp[sx+dx[dir]][sy+dy[dir]]=0;
int mini=99999;
REP(i,4){
int val=solve(mp,sx,sy,gx,gy,i,dep+1)+1;
if(val<mini){
mini=val;
}
}
mp[sx+dx[dir]][sy+dy[dir]]=1;
return mini;
}else{
return 9999;
}
}
else
{
return 9999;
}
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
while(true){
int w,h;
cin>>w>>h;
if(w==0){
break;
}
vector<vector<int> > mp(w+2,vector<int>(h+2,-1));
int startx;
int starty;
int goalx;
int goaly;
REP(j,h){
REP(i,w){
int hoge;
cin>>hoge;
if(hoge==0||hoge==1)
{
mp[i+1][j+1]=hoge;
}else
if(hoge==2){
mp[i+1][j+1]=0;
startx=i+1;
starty=j+1;
}else{
mp[i+1][j+1]=0;
goalx=i+1;
goaly=j+1;
}
}
}
int mini=99999;
REP(i,4){
int val=solve(mp,startx,starty,goalx,goaly,i,0)+1;
if(val<mini){
mini=val;
}
}
if(mini>10){
cout<<-1<<endl;
}else{
cout<<mini<<endl;
}
}
}
| #include <iostream>
#include <vector>
using namespace std;
typedef vector <int> VI;
typedef vector <VI> VVI;
const int dx[5] = { 0, 0, 0, 1, -1 };
const int dy[5] = { 0, 1, -1, 0, 0 };
int w, h;
int sx, sy, gx, gy;
VVI M;
int answer;
//==================================================
// Tõ
//==================================================
void dfs( int x, int y, int a, int d )
{
if ( d > 10 ) return;
if ( M[y][x] == 3 )
{
if ( answer == -1 )
answer = d;
else
answer = min( answer, d );
}
if ( a > 0 )
{
int nx = x + dx[a];
int ny = y + dy[a];
if ( nx < 0 || nx >= w || ny < 0 || ny >= h )
return;
if ( M[ny][nx] == 1 )
{
M[ny][nx] = 0;
dfs( x, y, 0, d );
M[ny][nx] = 1;
}
else
dfs( nx, ny, a, d );
}
else
{
for ( int i = 1; i < 5; i++ )
{
int nx = x + dx[i];
int ny = y + dy[i];
if ( nx < 0 || nx >= w || ny < 0 || ny >= h )
continue;
if ( M[ny][nx] == 0 || M[ny][nx] == 3 )
{
dfs( nx, ny, i, d+1 );
}
}
}
}
int main( void )
{
while ( cin >> w >> h && w && h )
{
//==================================================
// Ï
//==================================================
M = VVI( h, VI( w, 0 ) );
answer = -1;
//==================================================
// }bvÌüÍ
//==================================================
for ( int i = 0; i < h; i++ )
{
for ( int j = 0; j < w; j++ )
{
int& s = M[i][j];
cin >> s;
// X^[gES[
if ( s == 2 )
{
sx = j;
sy = i;
s = 0;
}
else if ( s == 3 )
{
gx = j;
gy = i;
}
}
}
//==================================================
//
//==================================================
dfs( sx, sy, 0, 0 );
//==================================================
// oÍ
//==================================================
cout << answer << endl;
}
return 0;
} | 1 |
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <functional>
#include <ctime>
#include <fstream>
#include <cmath>
#include <limits>
#include <chrono>
#include <numeric>
#include <type_traits>
#include <iomanip>
#include <float.h>
#include <math.h>
#include <cassert>
#include <atcoder/all>
using namespace atcoder;
#pragma warning (disable: 4996)
using namespace std;
using ll = long long;
unsigned euclidean_gcd(unsigned a, unsigned b) {
if (a < b) return euclidean_gcd(b, a);
unsigned r;
while ((r = a % b)) {
a = b;
b = r;
}
return b;
}
ll ll_gcd(ll a, ll b) {
if (a < b) return ll_gcd(b, a);
ll r;
while ((r = a % b)) {
a = b;
b = r;
}
return b;
}
struct UnionFind {
vector <ll> par;
vector <ll> siz;
UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
for (ll i = 0; i < sz_; ++i) par[i] = i;
}
void init(ll sz_) {
par.resize(sz_);
siz.assign(sz_, 1LL);
for (ll i = 0; i < sz_; ++i) par[i] = i;
}
ll root(ll x) {
while (par[x] != x) {
x = par[x] = par[par[x]];
}
return x;
}
bool merge(ll x, ll y) {
x = root(x);
y = root(y);
if (x == y) return false;
if (siz[x] < siz[y]) swap(x, y);
siz[x] += siz[y];
par[y] = x;
return true;
}
bool issame(ll x, ll y) {
return root(x) == root(y);
}
ll size(ll x) {
return siz[root(x)];
}
};
long long modpow(long long a, long long n, long long mod) {
if (n < 0)return 0;
long long res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
long long modinv(long long a, long long mod) {
return modpow(a, mod - 2, mod);
}
ll merge_cnt(vector<ll>& a) {
int n = a.size();
if (n <= 1) { return 0; }
ll cnt = 0;
vector<ll> b(a.begin(), a.begin() + n / 2);
vector<ll> c(a.begin() + n / 2, a.end());
cnt += merge_cnt(b);
cnt += merge_cnt(c);
int ai = 0, bi = 0, ci = 0;
while (ai < n) {
if (bi < b.size() && (ci == c.size() || b[bi] <= c[ci])) {
a[ai++] = b[bi++];
}
else {
cnt += n / 2 - bi;
a[ai++] = c[ci++];
}
}
return cnt;
}
int main() {
ll n, q;
cin >> n >> q;
fenwick_tree<ll> z(n);
for (int i = 0; i < n; i++) {
ll a;
cin >> a;
z.add(i, a);
}
for (int i = 0; i < q; i++) {
ll a, b, c;
cin >> a >> b >> c;
if (a == 0) {
z.add(b, c);
}
else {
cout << z.sum(b, c) << endl;
}
}
} | #include <bits/stdc++.h>
using namespace std;
// #define int long long
#define rep(i, n) for (long long i = (long long)(0); i < (long long)(n); ++i)
#define reps(i, n) for (long long i = (long long)(1); i <= (long long)(n); ++i)
#define rrep(i, n) for (long long i = ((long long)(n)-1); i >= 0; i--)
#define rreps(i, n) for (long long i = ((long long)(n)); i > 0; i--)
#define irep(i, m, n) for (long long i = (long long)(m); i < (long long)(n); ++i)
#define ireps(i, m, n) for (long long i = (long long)(m); i <= (long long)(n); ++i)
#define SORT(v, n) sort(v, v + n);
#define REVERSE(v, n) reverse(v, v+n);
#define vsort(v) sort(v.begin(), v.end());
#define all(v) v.begin(), v.end()
#define mp(n, m) make_pair(n, m);
#define cout(d) cout<<d<<endl;
#define coutd(d) cout<<std::setprecision(10)<<d<<endl;
#define cinline(n) getline(cin,n);
#define replace_all(s, b, a) replace(s.begin(),s.end(), b, a);
#define PI (acos(-1))
#define FILL(v, n, x) fill(v, v + n, x);
#define sz(x) long long(x.size())
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vs = vector<string>;
using vpll = vector<pair<ll, ll>>;
using vtp = vector<tuple<ll,ll,ll>>;
using vb = vector<bool>;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const ll INF = 1e9+10;
const ll MOD = 1e9+7;
const ll LINF = 1e18;
const ll dy[] = {0, 1, 0, -1, -1, 1, 1, -1};
const ll dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
inline bool inside(ll y, ll x, ll H, ll W) {
return (y >= 0 && x >= 0 && y < H && x < W);
}
char G[405][405];
signed main()
{
cin.tie( 0 ); ios::sync_with_stdio( false );
ll h,w; cin>>h>>w;
rep(i,h) rep(j,w) cin>>G[i][j];
map<char, int> ctoi;
ctoi['.']=0, ctoi['#']=1;
ll ans=0;
vvi visited(h+1, vi(w+1));
rep(i,h){
rep(j,w){
if(visited[i][j]) continue;
visited[i][j]=1;
char st=ctoi[G[i][j]];
ll cnt0=0, cnt1=0;
if(st) cnt1++;
else cnt0++;
queue<pair<pii,int>> q;
q.emplace(pii(i,j), st);
ll tmp=0;
while(!q.empty()){
auto p=q.front(); q.pop();
int y=p.first.first, x=p.first.second, from=p.second;
rep(ki,4){
int ny=y+dy[ki], nx=x+dx[ki];
if(inside(ny,nx,h,w)){
if(visited[ny][nx]) continue;
ll now=ctoi[G[ny][nx]];
if(from==now) continue;
visited[ny][nx]=1;
q.emplace(pii(ny,nx), now);
if(now) cnt1++;
else cnt0++;
}
}
}
ans+=cnt1*cnt0;
}
}
cout<<ans<<endl;
} | 0 |
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
typedef vector< vector<int> > VVI;
struct edge {
int to, cost;
edge(int t, int c) :to(t), cost(c) {}
};
typedef pair<int, int> P; // firstは最短距離, secondは頂点の番号
const int INF = (1e9);
void wFloyd ( VVI &v )
{
int len = v.size();
for (int k = 0; k < len; ++k)
{
for (int i = 0; i < len; ++i)
{
for (int j = 0; j < len; ++j)
{
v[i][j] = min(v[i][j], v[i][k] + v[k][j]);
}
}
}
}
int dijkstra ( int s, int e, int size, vector< vector<edge> > &graph )
{
priority_queue< P, vector<P>, greater<P> > que;
vector<int> cost(size, INF);
cost[s] = 0;
que.push(P(0, s));
while (!que.empty())
{
P p = que.top(); que.pop();
int v = p.second;
if (cost[v] < p.first) continue;
for (int i = 0, ilen = graph[v].size(); i < ilen; ++i)
{
edge e = graph[v][i];
if (cost[e.to] > (cost[v] + e.cost))
{
cost[e.to] = cost[v] + e.cost;
que.push(P(cost[e.to], e.to));
}
}
}
return cost[e];
}
int main ( void )
{
int n, k;
while (cin >> n >> k, (n | k))
{
//VVI v(n, vector<int>(n, INF));
vector< vector<edge> > v(n);
for (int i = 0; i < k; ++i)
{
int x;
cin >> x;
if (x)
{
int c, d, e;
cin >> c >> d >> e;
--c; --d;
bool hit = false;
for (int j = 0, jlen = v[c].size(); j < jlen && !hit; ++j)
{
if ((v[c][j].to == d) && (v[c][j].cost > e))
{
v[c][j].cost = e;
hit = true;
}
}
if (!hit) v[c].push_back(edge(d, e));
hit = false;
for (int j = 0, jlen = v[d].size(); j < jlen && !hit; ++j)
{
if ((v[d][j].to == c) && (v[d][j].cost > e))
{
v[d][j].cost = e;
hit = true;
}
}
if (!hit) v[d].push_back(edge(c, e));
//v[c].push_back(edge(d, e));
//v[d].push_back(edge(c, e));
//if ((v[c][d] > e) || (v[d][c] > e))
//{
// v[c][d] = v[d][c] = e;
// wFloyd(v);
//}
}
else
{
int a, b;
cin >> a >> b;
--a; --b;
int cost = dijkstra(a, b, n, v);
if (cost == INF)
{
cout << -1 << endl;
}
else
{
cout << cost << endl;
}
//if (v[a][b] == INF)
//{
// cout << -1 << endl;
//}
//else
//{
// cout << v[a][b] << endl;
//}
}
}
}
return 0;
} | #include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
using P = pair<int,int>;
const vector<int> di{-1,0,1,0};
const vector<int> dj{0,-1,0,1};
int main(){
int H, W;
cin >> H >> W;
vector<vector<char>> A(H,vector<char>(W));
vector<vector<int>> D(H,vector<int>(W,-1));
queue<P> q;
for(int i=0; i<H; i++){
for(int j=0; j<W; j++){
cin >> A.at(i).at(j);
if(A.at(i).at(j)=='#'){
q.push(make_pair(i,j));
D.at(i).at(j) = 0;
}
}
}
while(!q.empty()){
int i, j;
tie(i,j) = q.front();
q.pop();
for(int v=0; v<4; v++){
int ni = i+di.at(v);
int nj = j+dj.at(v);
if(ni<0||ni>=H) continue;
if(nj<0||nj>=W) continue;
if(D.at(ni).at(nj)<0){
D.at(ni).at(nj) = D.at(i).at(j)+1;
q.push(make_pair(ni,nj));
}
}
}
for(int i=0; i<H; i++){
sort(D.at(i).begin(),D.at(i).end());
}
int ans = 0;
for(int i=0; i<H; i++){
ans = max(ans,D.at(i).at(W-1));
}
cout << ans;
return 0;
} | 0 |
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
int main()
{
int n, m;
cin >> n >> m;
vector<int> ac(n, 0);
vector<int> wa(n, 0);
rep(i, m)
{
int p;
string s;
cin >> p >> s;
p--;
if (ac[p] == 1)
continue;
if (s == "AC")
ac[p] = 1;
else
wa[p]++;
}
rep(i, n)
wa[i] *= ac[i];
cout << accumulate(ac.begin(), ac.end(), 0) << ' ' << accumulate(wa.begin(), wa.end(), 0) << endl;
return 0;
} | #include <bits/stdc++.h>
#define PI 3.141592653589793
#define rep(i,n) for (int i=0;i<(n);i++)
using namespace std;
using ll = long long;
int main(void){
cin.tie(0);
ios::sync_with_stdio(false);
int N,M;
int p;
string S;
int i,k;
int AC=0;
int WA=0;
cin >> N >> M;
int wrong[N+5]={0};
for(i=0;i<M;i++){
cin >> p >> S;
if(S=="AC" && wrong[p]>=0){
AC++;
WA+=wrong[p];
wrong[p]=-1000000;
}else{
wrong[p]++;
}
}
cout << AC << " " << WA << endl;
return 0;
}
| 1 |
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define cinf(n,x) for(int i=0;i<(n);i++)cin>>x[i];
#define ft first
#define sc second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(),(v).end()
#define mod 1000000007
#define FS fixed<<setprecision(15)
using namespace std;
typedef long long ll;
template<class T> using V=vector<T>;
using Graph = vector<vector<int>>;
using P=pair<ll,ll>;
typedef unsigned long long ull;
typedef long double ldouble;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
const ll INF=1e18;
int main(){
cin.tie(0);ios::sync_with_stdio(false);
int n,m;
cin>>n>>m;
set<int> st;
for(int i=1;i<n;i++){
st.insert(i);
}
int k=1;
int pr=-1;
set<int> check;
while(k<=m){
int v=*rbegin(st);
st.erase(v);
while(pr-v==1||check.count(n-v)||2*v==n){
v=*rbegin(st);
st.erase(v);
}
pr=v;
check.insert(v);
printf("%d %d\n",k,k+v);
k++;
}
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define zero_pad(num) setfill('0') << std::right << setw(num)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
int main() {
ll x, y;
cin >> x >> y;
if(x == y)cout << 0 << endl;
else if(x == -y)cout << 1 << endl;
else if(abs(x) < abs(y)){
ll ans = abs(y) - abs(x);
if(x < 0 && y < 0)ans += 2;
else if(x < 0 || y < 0)ans++;
cout << ans << endl;
}else{
ll ans = abs(x) - abs(y);
if(x > 0 && y > 0)ans += 2;
else if(x > 0 || y > 0)ans++;
cout << ans << endl;
}
} | 0 |
#include<bits/stdc++.h>
using namespace std;
int main(void){
while(1){
int month, day;
int days_from_new_year = 0;
cin >> month >> day;
if(month == 0 && day == 0) break;
for(int i = 1; i < month; i++){
if(i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12) days_from_new_year += 31;
else if(i == 4 || i == 6 || i == 9 || i == 11) days_from_new_year += 30;
else if(i == 2) days_from_new_year += 29;
}
days_from_new_year += day;
int mod = days_from_new_year % 7;
if(mod == 0) cout << "Wednesday";
else if(mod == 1) cout << "Thursday";
else if(mod == 2) cout << "Friday";
else if(mod == 3) cout << "Saturday";
else if(mod == 4) cout << "Sunday";
else if(mod == 5) cout << "Monday";
else if(mod == 6) cout << "Tuesday";
cout << endl;
}
return 0;
} | #include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main(){
int N, K;
while(cin >> N >> K){
int res = 0;
vector<int> deg(N, 0);
vector<int> next(N, -1);
vector<int> dist(N, 0);
for(int i=0;i<N;i++){
cin >> next[i];
--next[i];
if(i == 0 && next[i]){
++res;
next[i] = 0;
}
++deg[next[i]];
}
queue<int> qu;
for(int i=1;i<N;i++) if(!deg[i]) qu.push(i);
while(!qu.empty()){
int p = qu.front(); qu.pop();
if(!p) break;
if((next[p] && dist[p] == K-1) || (!p && !next[p])){
++res;
} else {
dist[next[p]] = max(dist[next[p]], dist[p]+1);
}
--deg[next[p]];
if(!deg[next[p]]) qu.push(next[p]);
}
cout << res << endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> pipii;
typedef pair<pii, pii> piipii;
#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
#define eb emplace_back
const int mod = 1e9+7;
int dp[45][1<<17];
int main(){
int n, x, y, z;
scanf("%d%d%d%d", &n, &x, &y, &z);
int b = (1<<(x+y+z-1))|(1<<(y+z-1))|(1<<(z-1));
dp[0][0] = 1;
for(int i=1;i<=n;i++){
for(int j=0;j<(1<<17);j++){
if(dp[i-1][j] == 0) continue;
for(int k=1;k<=10;k++){
int nt = (j<<k) + (1<<(k-1));
nt &= (1<<17)-1;
if((nt&b) == b) continue;
dp[i][nt] = (dp[i][nt] + dp[i-1][j])%mod;
}
}
}
ll ans = 1;
for(int i=1;i<=n;i++) ans = ans*10%mod;
for(int j=0;j<(1<<17);j++) ans = (ans - dp[n][j] + mod)%mod;
printf("%lld\n", ans);
} | #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#define rep(i,l,r) for(int i=(l);i<=(r);++i)
#define per(i,r,l) for(int i=(r);i>=(l);--i)
using namespace std;
const int N=3e5+10,P=1e9+7;
const int tr[][2]={{1,2},{3,4},{5,6},{1,1},{1,2},{7,2},{6,6},{5,5}};
char s[N];int dp[N][8];
inline void upd(int &x,int y){((x+=y)>=P)&&(x-=P);}
int main(){
scanf("%s",s);int n=strlen(s);
dp[0][0]=1;
rep(i,0,n-1){
rep(j,0,7){
if(s[i]^49) upd(dp[i+1][tr[j][0]],dp[i][j]);
if(s[i]^48) upd(dp[i+1][tr[j][1]],dp[i][j]);
}
}
printf("%d\n",dp[n][2]+dp[n][6]>=P?dp[n][2]+dp[n][6]-P:dp[n][2]+dp[n][6]);
return 0;
} | 0 |
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<cmath>
#include<cstdio>
#include<queue>
#include<deque>
#include<map>
#include<stack>
#include<set>
#include<utility>
using namespace std;
typedef pair<int,int> ii;
typedef long long ll;
typedef pair<ll,ll> P;
typedef unsigned long long int ull;
int dy[]={1,0,-1,0};
int dx[]={0,1,0,-1};
const int MAXN=100000;
const int MAXE=100000;
const int MAXV=10000;
const ll INF=2e18;
const ll MOD=1e9+7;
int main(){
int N,M;cin>>N>>M;
vector<ll> A(N),S(N+1,0);
map<int,ll> mp;
mp[0]++;
for(int i=0;i<N;++i) cin>>A[i];
for(int i=0;i<N;++i){
S[i+1]=S[i]+A[i];
S[i+1]%=M;
mp[S[i+1]]++;
}
ll ans=0;
for(auto it:mp){
ans+=(it.second)*(it.second-1)/2;
}
cout<<ans<<endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
int main(){
int64_t N,M;cin>>N>>M;
vector<int64_t>A(N);
for(int i=0;i<N;i++)
cin>>A.at(i);
vector<int64_t>B(N+1);B.at(0)=0;
for(int i=1;i<=N;i++)
B.at(i)=(B.at(i-1)+A.at(i-1))%M;
map<int64_t,int64_t>m;
for(int i=0;i<=N;i++)
if(m.count(B.at(i)))
m.at(B.at(i))++;
else
m[B.at(i)]=1;
int64_t ans=0;
for(auto p:m){
auto v=p.second;
ans+=(v*(v-1))/2;
}cout<<ans<<endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main(){
char a,b,c,d;
cin>>a>>b>>c>>d;
if((a-'0')+(b-'0')+(c-'0')+(d-'0')==7){
cout<<a<<"+"<<b<<"+"<<c<<"+"<<d<<"=7"<<endl;
}
else if((a-'0')+(b-'0')+(c-'0')-(d-'0')==7){
cout<<a<<"+"<<b<<"+"<<c<<"-"<<d<<"=7"<<endl;
}
else if((a-'0')+(b-'0')-(c-'0')+(d-'0')==7){
cout<<a<<"+"<<b<<"-"<<c<<"+"<<d<<"=7"<<endl;
}
else if((a-'0')+(b-'0')-(c-'0')-(d-'0')==7){
cout<<a<<"+"<<b<<"-"<<c<<"-"<<d<<"=7"<<endl;
}
else if((a-'0')-(b-'0')+(c-'0')+(d-'0')==7){
cout<<a<<"-"<<b<<"+"<<c<<"+"<<d<<"=7"<<endl;
}
else if((a-'0')-(b-'0')+(c-'0')-(d-'0')==7){
cout<<a<<"-"<<b<<"+"<<c<<"-"<<d<<"=7"<<endl;
}
else if((a-'0')-(b-'0')-(c-'0')+(d-'0')==7){
cout<<a<<"-"<<b<<"-"<<c<<"+"<<d<<"=7"<<endl;
}
else if((a-'0')-(b-'0')-(c-'0')-(d-'0')==7){
cout<<a<<"-"<<b<<"-"<<c<<"-"<<d<<"=7"<<endl;
}
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main(){
int N;
cin >> N;
vector<int> num(4);
for(int i=0;i<4;i++){
num[3-i]=N%10;
N/=10;
}
for(int i=0;i<(1<<3);i++){
vector<char> op(3);
int ans=num[0];
for(int j=0;j<3;j++){
if((i & (1<<j))==(1<<j)){
ans+=num[j+1];
op[j]='+';
}
else{
ans-=num[j+1];
op[j]='-';
}
}
if(ans==7){
for(int i=0;i<4;i++){
cout << num[i];
if(i!=3){
cout << op[i];
}
}
cout << "=7" << endl;
return 0;
}
}
} | 1 |
#pragma region header
#include <algorithm>
#include <bitset>
#include <tuple>
#include <cstdint>
#include <cctype>
#include <assert.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <math.h>
#include <cstring>
#include <array>
// ===============================================================
//using系
#pragma region header
using namespace std;
using lint = long long;
using ld = long double;
using ulint = unsigned long long;
const int dx[] = { 1,0,-1,0 };
const int dy[] = { 0,1,0,-1 };
constexpr lint mod = 1000000007;
constexpr long double pi = 3.141592653589793238462643383279;
#pragma endregion
// ========================================================================
//define
#define INF (lint)10000000000000000;
#define mod (int)1000000007
#pragma region header
template <class T, class U>
inline bool chmin(T& lhs, const U& rhs) {
if (lhs > rhs) {
lhs = rhs;
return 1;
}
return 0;
}
template <class T, class U>
inline bool chmax(T& lhs, const U& rhs) {
if (lhs < rhs) {
lhs = rhs;
return 1;
}
return 0;
}
#pragma endregion
#pragma endregion
lint dp[1000][2020];
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
int n; cin >> n;
vector<int> d(n);
vector<bool> ans(n, false);
for (int i = 0; i < n; i++) cin >> d[i];
for (int i = n - 1; i >= 0; i--) {
int cnt = 0;
for (int j = i; j < n; j += i + 1) {
if (ans[j]) cnt++;
}
if (cnt % 2 != d[i]) ans[i] = true;
}
int cnt = 0;
lint a = 0;
for (int i = 0; i < n; i++) {
if (ans[i]) a++;
}
cout << a << endl;
for (int i = 0; i < n; i++) {
if (ans[i]) {
cout << i + 1;
cnt++;
if (cnt == a) cout << endl;
else cout << ' ';
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 5LL << 60;
const ll mod = 1e9 + 7;
int main() {
int H, W;
cin >> H >> W;
int a[H][W];
for(int i = 0; i < H; i++)
for(int j = 0; j < W; j++) cin >> a[i][j];
vector<tuple<int, int, int, int>> v;
for(int i = 0; i < H; i++) {
for(int j = 0; j < W; j++) {
if(a[i][j] % 2 == 0) continue;
if(i == H - 1 && j == W - 1) continue;
if(j == W - 1) {
v.push_back(make_tuple(i + 1, j + 1, i + 2, j + 1));
a[i + 1][j]++;
} else {
v.push_back(make_tuple(i + 1, j + 1, i + 1, j + 2));
a[i][j + 1]++;
}
}
}
cout << v.size() << "\n";
for(int i = 0; i < v.size(); i++) cout << get<0>(v[i]) << " " << get<1>(v[i]) << " " << get<2>(v[i]) << " " << get<3>(v[i]) << "\n";
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for(int i = n-1; i >= 0; i--)
#define all(x) (x).begin(),(x).end() // 昇順ソート
#define rall(v) (v).rbegin(), (v).rend() // 降順ソート
#define FastIO ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
typedef long long ll;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<vector<int>>;
using VL = vector<ll>;
using VVL = vector<vector<ll>>;
using VP = vector<P>;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return true; } return false; }
const int MAX = 55555;
int main(){
int n;
cin >> n;
VI d(MAX + 1);
for(int i = 1; i <= MAX; i++){
for(int j = i; j <= MAX; j += i){
d[j]++;
}
}
int cnt = 0, i = 3;
VI ans;
while(cnt < n){
if(d[i] == 2 && i % 5 == 3){
ans.push_back(i);
cnt++;
}
i++;
}
rep(i,n) printf("%d%c", ans[i], i == n-1? '\n' : ' ');
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll,ll>;
using vl = vector<ll>;
using Map = map<ll,ll>;
using T = tuple<ll,ll,ll>;
using vvl = vector<vector<ll>>;
#define all(v) v.begin(), v.end()
#define prt(v) cout<<v<<"\n";
#define fi(v) get<0>(v)
#define se(v) get<1>(v)
#define th(v) get<2>(v)
#define endl "\n"
template <typename T> bool chmax(T &a, const T &b){if (a<b){a=b;return 1;}return 0;}
template <typename T> bool chmin(T &a, const T &b){if (a>b){a=b;return 1;}return 0;}
const ll INF=1LL<<60;
const ll MOD=1000000007;
vector<ll> Eratosthenes( const ll N )
{
std::vector<bool> is_prime( N + 1 );
for( ll i = 0; i <= N; i++ )
{
is_prime[ i ] = true;
}
std::vector<ll> P;
for( ll i = 2; i <= N; i++ )
{
if( is_prime[ i ] )
{
for( ll j = 2 * i; j <= N; j += i )
{
is_prime[ j ] = false;
}
P.emplace_back( i );
}
}
return P;
}
signed main(void){
cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);
ll N;
cin >> N;
vl A=Eratosthenes(60000);
ll count=0;ll now=0;
while(count<N){
now++;
while(A[now]%5!=3)now++;
cout<<A[now]<<" ";
count++;
}cout<<endl;
return 0;
}
| 1 |
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <functional>
#include <ctime>
#include <cmath>
#include <limits>
#include <numeric>
#include <type_traits>
#include <iomanip>
#include <float.h>
#include <math.h>
using namespace std;
using ll = long long;
unsigned euclidean_gcd(unsigned a, unsigned b) {
if (a < b) return euclidean_gcd(b, a);
unsigned r;
while ((r = a % b)) {
a = b;
b = r;
}
return b;
}
ll ll_gcd(ll a, ll b) {
if (a < b) return ll_gcd(b, a);
ll r;
while ((r = a % b)) {
a = b;
b = r;
}
return b;
}
struct UnionFind {
vector <ll> par;
vector <ll> siz;
UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
for (ll i = 0; i < sz_; ++i) par[i] = i;
}
void init(ll sz_) {
par.resize(sz_);
siz.assign(sz_, 1LL);
for (ll i = 0; i < sz_; ++i) par[i] = i;
}
ll root(ll x) {
while (par[x] != x) {
x = par[x] = par[par[x]];
}
return x;
}
bool merge(ll x, ll y) {
x = root(x);
y = root(y);
if (x == y) return false;
if (siz[x] < siz[y]) swap(x, y);
siz[x] += siz[y];
par[y] = x;
return true;
}
bool issame(ll x, ll y) {
return root(x) == root(y);
}
ll size(ll x) {
return siz[root(x)];
}
};
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
long long modinv(long long a, long long mod) {
return modpow(a, mod - 2, mod);
}
vector<int> tpsort(vector<vector<int>>& G) {
int V = G.size();
vector<int> sorted_vertices;
queue<int> que;
vector<int> indegree(V);
for (int i = 0; i < V; i++) {
for (int j = 0; j < G[i].size(); j++) {
indegree[G[i][j]]++;
}
}
for (int i = 0; i < V; i++) {
if (indegree[i] == 0) {
que.push(i);
}
}
while (que.empty() == false) {
int v = que.front();
que.pop();
for (int i = 0; i < G[v].size(); i++) {
int u = G[v][i];
indegree[u] -= 1;
if (indegree[u] == 0) que.push(u);
}
sorted_vertices.push_back(v);
}
return sorted_vertices;
}
struct Point
{
double x;
double y;
};
struct LineSegment
{
Point start;
Point end;
};
double tenkyori(const LineSegment& line, const Point& point)
{
double x0 = point.x, y0 = point.y;
double x1 = line.start.x, y1 = line.start.y;
double x2 = line.end.x, y2 = line.end.y;
double a = x2 - x1;
double b = y2 - y1;
double a2 = a * a;
double b2 = b * b;
double r2 = a2 + b2;
double tt = -(a * (x1 - x0) + b * (y1 - y0));
if (tt < 0)
return sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0));
else if (tt > r2)
return sqrt((x2 - x0) * (x2 - x0) + (y2 - y0) * (y2 - y0));
double f1 = a * (y1 - y0) - b * (x1 - x0);
return sqrt((f1 * f1) / r2);
}
int main() {
ll n;
cin >> n;
string s;
cin >> s;
ll m = 0;
ll ans = 1;
for (int i = 0; i < 2 * n; i++) {
if (i % 2 == 0 && s[i] == 'W' || i % 2 == 1 && s[i] == 'B') {
ans *= m;
m--;
ans %= 1000000007;
}
else {
m++;
}
}
if (m != 0)cout << 0 << endl;
else {
for (ll i = 0; i < n; i++) {
ans *= (i+1);
ans %= 1000000007;
}
cout << ans << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i,m,n) for(int (i)=(int)(m);i<(int)(n);i++)
#define rep2(i,m,n) for(int (i)=(int)(n)-1;i>=(int)(m);i--)
#define REP(i,n) rep(i,0,n)
#define REP2(i,n) rep2(i,0,n)
#define FOR(i,c) for(decltype((c).begin())i=(c).begin();i!=(c).end();++i)
#define ll long long
#define ull unsigned long long
#define all(hoge) (hoge).begin(),(hoge).end()
#define en '\n'
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
typedef pair<ll, ll> P;
constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;
constexpr long long MOD = (ll) 1e9 + 7;
typedef vector<ll> Array;
typedef vector<Array> Matrix;
constexpr int loose = 0;
constexpr int tight = 1;
template<class T> inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template<class T> inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
struct Edge {
ll to, cap, rev;
Edge(ll _to, ll _cap, ll _rev) {
to = _to; cap = _cap; rev = _rev;
}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
void add_edge(Graph& G, ll from, ll to, ll cap, bool revFlag, ll revCap) {
G[from].push_back(Edge(to, cap, (ll)G[to].size()));
if (revFlag)G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));
}
void solve(){
ll n;
cin>>n;
Array a(n);
REP(i,n) cin>>a[i];
map<ll,ll> mp;
REP(i,n) mp[a[i]]++;
sort(all(a));
ll ans=0;
rep2(i,1,n){
ll con1=mp[a[i]];
if(con1==0) continue;
ll num=1;
while(num<=a[i]) num*=2;
ll con2=mp[num-a[i]];
if(num==a[i]*2){
ans+=con1/2;
mp[a[i]]-=con1;
continue;
}
ans += min(con1,con2);
mp[a[i]] = 0;
mp[num-a[i]] -= min(con1,con2);
}
cout<<ans<<en;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
solve();
return 0;
} | 0 |
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define watch(x) cout << (#x) << " is " << (x) << endl
#define debug cout << "hi" << endl
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const ll mod = 1e9 + 7;
const int INF32 = 1<<30;
const ll INF64 = 1LL<<60;
const ld pi = 3.141592653589793;
void solve(){
int n;cin >> n;
vector<ll>a(n);
for(int i = 0;i<n;i++) cin >> a[i];
int cnt = 0;ll sum = 0;ll mini = INT_MAX;
for(int i = 0;i<n;i++){
if(a[i]<0) cnt++;
sum += abs(a[i]);
mini = min(abs(a[i]),mini);
// cout << sum <<endl;
}
if(cnt%2) cout << sum - 2*mini;
else cout << sum;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
} | #include <iostream>
using namespace std;
int N;
int A[210]; // 最大 200 個なので余裕を持って 210 に --- 200 以上ならなんでもよいです
int main() {
cin >> N;
for (int i = 0; i < N; ++i) cin >> A[i];
int res = 0;
// 操作が行える限り操作を繰り返す
while (true) {
bool exist_odd = false; // A[i] がすべて偶数かどうかを判定するフラグ
for (int i = 0; i < N; ++i) {
if (A[i] % 2 != 0) exist_odd = true; // 奇数があったらフラグを立てる
}
if (exist_odd) break; // 奇数があったら break
// 操作を行えるなら操作を実際に行う
for (int i = 0; i < N; ++i) {
A[i] /= 2;
}
++res; // 操作回数をインクリメント
}
cout << res << endl;
} | 0 |
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <bitset>
#include <string>
#include <cmath>
#include <complex>
#include <numeric>
#include <cassert>
#include <vector>
#include <array>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <utility>
#define PI 3.14159265358979323846
#define int64 long long
#define uint64 unsigned long long
using namespace std;
int main()
{
string ss;
cin >> ss;
if(ss[0] != 'A')
{
cout << "WA" << endl;
return 0;
}
int64 cc = 0;
for(int64 ii = 2;ii < ss.length() - 1;ii++)
{
if(ss[ii] == 'C')
{
cc++;
}
}
if(cc != 1)
{
cout << "WA" << endl;
return 0;
}
cc = 0;
for(int64 ii = 0;ii < ss.length();ii++)
{
if(ss[ii] >= 'A' && ss[ii] <= 'Z')
{
cc++;
}
}
if(cc != 2)
{
cout << "WA" << endl;
return 0;
}
cout << "AC" << endl;
return 0;
}
| #include<stdio.h>
int main(){
int N;
scanf("%d", &N);
if( N%10==7 || (N%100)/10==7 || N/100==7)
printf("Yes\n");
else
printf("No\n");
return 0;
} | 0 |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define mp make_pair
#define pb push_back
#define lp(i,s,f) for(ll i = s; i < ll(f); i++)
#define inF freopen("input.in", "r", stdin);
#define outF freopen("output.in", "w", stdout);
#define endl '\n'
#define MOD 1000000007
#define mm(arr) memset(arr, 0, sizeof(arr))
int main(){
FAST
int arr[3];
for(int i = 0; i < 3; i++){
cin >> arr[i];
}
sort(arr, arr + 3);
cout << arr[0] + arr[1];
return 0;
}
| // I make this just for fun because i'm done
// Aimi Haraguni >> Konomi Suzuki >> Yui >> Ikimono Gakari >> Garnidelia >> Kalafina >> Eir Aoi. .. dude?
// problems that involves any kind of persistent data structures are the best of the best, are not them?
// #pragma GCC optimize ("Ofast,unroll-loops")
// #pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#define pb push_back
#define ff first
#define ss second
#define tm1 first
#define tm2 second.first
#define tm3 second.second
#define sz(x) ll(x.size())
#define all(v) (v).begin(), (v).end()
#define FER(i,a,b) for(ll i=ll(a); i< ll(b); ++i)
#define IFR(i,a,b) for(ll i=ll(a); i>=ll(b); --i )
#define fastio ios_base::sync_with_stdio(0); cin.tie(0)
#define N 31000000
// #define M 19
// #define mod 11092019
#define mod1 1000000007
#define mod2 1000000009
#define bas 987625403
#define sqr(x) (x)*(x)
#define INF 2000000000000000
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<ll, ll> ii;
typedef pair<ll, ii > tri;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag, tree_order_statistics_node_update> S_t;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define trace(...) fff(#__VA_ARGS__, __VA_ARGS__)
template<typename t> void fff(const char* x, t&& val1) { cout<<x<< " : "<<val1<<"\n";}
template<typename t1, typename... t2> void fff(const char* x, t1&& val1, t2&&... val2){
const char* xd=strchr(x+1, ',');
cout.write(x, xd-x)<<" : "<<val1<<" | ";
fff(xd+1, val2...);
}
inline ll mul(ll a, ll b, ll mod) { return (long long) a*b%mod;}
inline ll add(ll a, ll b, ll mod) { return a+b < mod? a+b: a+b-mod;}
inline void Mul(ll &a, ll b, ll mod) { a = (long long) a*b%mod;}
inline ll bp(ll a, ll p, ll mod){
ll ret;
for(ret = 1; p; p>>=1, Mul(a, a, mod)) (p & 1) && (Mul(ret, a, mod), 1);
return ret;
}
static inline void read(ll &x) { cin>>x;}
int main(){
ll a, b, c; read(a), read(b), read(c);
ll z = min({a+b, b+c, c+a});
cout<<z<<"\n";
return 0;
} | 1 |
/* You can't hack me! */
#include"bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
#define fo(i,n) for(ll i=0; i<(n); i++)
#define FO(i,a,b) for(auto i=a; i!=(b); i+=(b)>(a)?1:-1)
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(c) c.begin(), c.end()
#define F first
#define S second
#define os(x) cout<<x<<' '
#define on(x) cout<<x<<"\n"
#define nl cout<<"\n"
#define maxe(x) max_element(all(x))-(x).begin()
#define mine(x) min_element(all(x))-(x).begin()
const ll P=998244353;
const ll MaxN=2000010;
const long double pi=3.14159265358979323846264338;
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("C:\\Users\\ASISH\\Desktop\\coding\\input.txt", "r", stdin);
freopen("C:\\Users\\ASISH\\Desktop\\coding\\output.txt", "w", stdout);
#endif
ll T=1;
// cin>>T;
fo(q, T)
{
ll N, ans=0;
cin>>N;
vll cnt(3);
string s;
cin>>s;
for(auto x : s){
if(x=='R') cnt[0]++;
else if(x=='G') cnt[1]++;
else cnt[2]++;
}
ans=cnt[0]*cnt[1]*cnt[2];
// on(ans);
fo(i, N-2){
FO(k, i+2, N){
// os(i); on(k);
if((i+k)%2) continue;
ll j=(i+k)/2;
if(s[k]!=s[j] && s[j]!=s[i] && s[i]!=s[k]) ans--;
}
}
on(ans);
}
cerr << "Time : " << ((double)clock()) / (double)CLOCKS_PER_SEC << "s\n";
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 999999999
#define rep(i,n) for(int i=0;i<n;i++)
const ll MOD = 1000000007;
ll cnt =0,ans=0;
const int MAX = 510000;
int main(){
ll n;cin >> n;
string s; cin >> s;
ll r=0,g=0,b=0;
for(int i=0;i<n;i++){
if(s.at(i) == 'R')r++;
if(s.at(i) == 'G')g++;
if(s.at(i) == 'B')b++;
}
for(int i=0;i<n-2;i++){
for(int j=i+1;j<n-1;j++){
int k = j+(j-i);
if(k>=n)break;
if(s.at(i) != s.at(j) && s.at(j) != s.at(k) && s.at(k) != s.at(i))cnt++;
}
}
cout << r*g*b - cnt << endl;
}
| 1 |
#include <iostream>
#include <map>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <sstream>
#include <cmath>
#include <math.h>
#include <string>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int h , w;
string s[50] = {};
cin >> h >> w;
for( int i = 0 ; i < h ; i++ ) cin >> s[i];
for( int i = 0 ; i < h ; i++ ) {
for ( int j = 0 ; j < w ; j++ ) {
if ( s[i][j] == '#' ) {
bool y = 0;
if ( i && s[i - 1][j] == '#' ) y = 1;
if ( i + 1 < h && s[i + 1][j] == '#' ) y = 1;
if ( j && s[i][j - 1] == '#' ) y = 1;
if ( j + 1 < w && s[i][j + 1] == '#' ) y = 1;
if ( !y ) {
cout << "No";
return 0;
}
}
}
}
cout << "Yes";
}
| /********************************************************************************
Code by a weak man who named CYJian, and he hope the code can get more points.
Algorithm:
********************************************************************************/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int __SIZE = 1 << 18;
char ibuf[__SIZE], *iS, *iT;
#define ge (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)
#define ri read_int()
#define rl read_ll()
#define FILE(s) freopen(s"in", "r", stdin), freopen(s"out", "w", stdout)
template<typename T>
inline void read(T &x) {
char ch, t = 0; x = 0;
while(!isdigit(ch = ge)) t |= ch == '-';
while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = ge;
x = t ? -x : x;
}
inline int read_int() { int x; return read(x), x; }
inline ll read_ll() { ll x; return read(x), x; }
template<typename T>
inline void chkmin(T&a, T b) { a = a < b ? a : b; }
const string S4[4] = {"abcc", "abdd", "ccab", "ddab"};
const string S5[5] = {"abbcc", "ad..a", "bd..a", "b.eez", "oorrz"};
const string S6[6] = {"oorrzz", ".a.b.c", ".a.b.c", "d.e.f.", "d.e.f.", "oorrzz"};
const string S7[7] = {".oorrzz", "oabb...", "oa.c...", "rddc...", "r...abb", "z...a.c", "z...ddc"};
char s[1010][1010];
inline void solve(int&n, int x) {
if(x == 4)
for(int i = 0; i < x; i++)
for(int j = 0; j < x; j++)
s[n + i][n + j] = S4[i][j];
if(x == 5)
for(int i = 0; i < x; i++)
for(int j = 0; j < x; j++)
s[n + i][n + j] = S5[i][j];
if(x == 6)
for(int i = 0; i < x; i++)
for(int j = 0; j < x; j++)
s[n + i][n + j] = S6[i][j];
if(x == 7)
for(int i = 0; i < x; i++)
for(int j = 0; j < x; j++)
s[n + i][n + j] = S7[i][j];
n += x;
}
int main() {
int n = ri;
if(n <= 2) return puts("-1"), 0;
if(n == 3) {
puts("a..");
puts("a..");
puts(".bb");
return 0;
}
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
s[i][j] = '.';
int x = n % 4, N = 1;
if(x == 0) solve(N, 4);
if(x == 1) solve(N, 5);
if(x == 2) solve(N, 6);
if(x == 3) solve(N, 7);
while(N <= n) solve(N, 4);
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++)
putchar(s[i][j]);
puts("");
}
return 0;
} | 0 |
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
int main()
{
string a,b;
cin>>a>>b;
cout<<b<<""<<a<<""<<endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define pb push_back
#define COUT(x) cout << (x) << "\n"
#define COUTF(x) cout << setprecision(15) << (x) << "\n"
#define ENDL cout << "\n"
#define DF(x) x.erase(x.begin())
#define ALL(x) x.begin(), x.end()
#define SORT(x) sort(ALL(x))
#define RSORT(x) sort(x.rbegin(), x.rend())
#define REVERSE(x) reverse(ALL(x))
#define MAX(x) *max_element(ALL(x))
#define MAXI(x) max_element(ALL(x)) - x.begin()
#define SUM(x) accumulate(ALL(x), 0)
#define COUNT(x, y) count(ALL(x), y);
#define ANS cout << ans << "\n"
#define YES cout << "YES\n";
#define NO cout << "NO\n";
#define Yes cout << "Yes\n";
#define No cout << "No\n";
#define YN YES else NO
#define yn Yes else No
#define init() \
cin.tie(0); \
ios::sync_with_stdio(false)
#define LINE cerr << "[debug] line: " << __LINE__ << "\n";
#define debug(x) cerr << "[debug] " << #x << ": " << x << "\n";
#define debugV(v) \
cerr << "[debugV] " << #v << ":"; \
rep(z, v.size()) cerr << " " << v[z]; \
cerr << "\n";
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using mll = map<ll, ll>;
using qll = queue<ll>;
using P = pair<ll, ll>;
using vp = vector<P>;
using vs = vector<string>;
template <typename T>
inline istream& operator>>(istream& i, vector<T>& v) {
rep(j, v.size()) i >> v[j];
return i;
}
template <typename T1, typename T2>
inline istream& operator>>(istream& i, pair<T1, T2>& v) {
return i >> v.first >> v.second;
}
constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
constexpr ld PI = 3.141592653589793238462643383279;
ll get_digit(ll x) {
return to_string(x).size();
}
ll gcd(ll x, ll y) {
return y ? gcd(y, x % y) : x;
}
ll lcm(ll a, ll b) {
return a / gcd(a, b) * b;
}
template <class T>
bool chmax(T& a, const T& b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
bool chmin(T& a, const T& b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
signed main() {
init();
string S;
ll w;
cin >> S >> w;
string ans = "";
rep(i, S.size()) if (i % w == 0) ans += S[i];
ANS;
return 0;
} | 0 |
#include<bits/stdc++.h>
#define rep(i,n) for (long long i = 0; i < (n); ++i)
//g++ -std=c++11
using namespace std;
long gcd(long a,long b){
if (a%b==0){
return(b);
}
else{
return(gcd(b,a%b));
}
}
long long prime_factor(long long num){
long long bak = num;
set<long long> nums;
for(long long i = 2; i*i <= num; i++){
while(bak % i == 0){
nums.insert(i);
bak /= i;
}
}
if(bak > 1){
nums.insert(bak);
}
nums.insert(1);
return (long long) nums.size();
}
long long A, B;
int main(){
cin >> A >> B;
long long num = gcd(A, B);
cout << prime_factor(num) << endl;
} | #include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int INF = 1e9;
long long gcd(long long x, long long y){
if(y == 0) return x;
else return gcd(y, x%y);
}
bool IsPrime(long long n){
if(n == 1) return false;
for(long long i = 2; i * i < n; i++){
if(n%i == 0) return false;
}
return true;
}
vector<long long> EnumDivisors(long long n){
vector<long long> res;
for(long long i = 1; i * i <= n; i++){
if(n % i == 0){
res.push_back(i);
if(n/i != i) res.push_back(n/i);
}
}
sort(res.begin(), res.end());
return res;
}
vector<pair<long long, long long>> PrimeFactorize(long long n){
vector<pair<long long, long long>> res;
long long a = 0;
long long b = 0;
for(long long i = 2; i * i <= n; i++){
a = 0;
if(n%i == 0){
if(IsPrime(i)){
while(n % i == 0){
n /= i;
a++;
}
res.push_back(make_pair(i, a));
}
}
}
if(n!= 1) res.push_back(make_pair(n, 1));
return res;
}
int main(){
long long a, b;
cin >> a >> b;
vector<pair<long long, long long>> a_fac = PrimeFactorize(a);
vector<pair<long long, long long>> b_fac = PrimeFactorize(b);
// for(int i = 0; i < a_fac.size(); i++) cout << a_fac[i].first << ", " << a_fac[i].second << endl;
// for(int i = 0; i < b_fac.size(); i++) cout << b_fac[i].first << ", " << b_fac[i].second << endl;
long long ans = 0;
for(int i = 0; i < a_fac.size(); i++){
for(int j = 0; j < b_fac.size(); j++){
if(a_fac[i].first == b_fac[j].first) {
// cout << "i = " << i << ", j = " << j << endl;
ans ++;
}
}
}
cout << ans + 1 << endl;
return 0;
} | 1 |
#include <bits/stdc++.h>
using namespace std;
const int nax = 105;
const int mod = 1e9 + 7;
string s;
int d;
void add_self(int &a, int b)
{
a += b;
if(a >= mod)
a -= mod;
}
int main()
{
cin >> s;
cin >> d;
vector<vector<int>>dp(nax, vector<int>(2));
int n = s.length();
dp[0][0] = 1;
for(int where = 0; where < n; ++where)
{
vector<vector<int>>new_dp(nax, vector<int>(2));
for(int sum = 0; sum < d; ++sum)
for(int digit = 0; digit < 10; ++digit)
for(bool is_smaller : {false, true})
{
if(!is_smaller && digit > s[where] - '0')
continue;
add_self(new_dp[(sum + digit) % d][is_smaller || digit < s[where] - '0'], dp[sum][is_smaller]);
}
dp = new_dp;
}
int answer = (dp[0][false] + dp[0][true]) % mod;
--answer;
if(answer < 0)
answer += mod;
cout << answer;
return 0;
}
| //product of pairs
#include<bits/stdc++.h>
using namespace std;
long long int mod = 1e9 + 7;
int main()
{
long long int tt;
tt = 1;
while (tt--)
{
long long int n, i, count = 0;
cin >> n;
long long int a[n + 1], p[n + 1];
for (i = 1; i <= n; i++)
{
cin >> a[i];
}
p[n] = a[n];
for (i = n - 1; i >= 1; i--)
{
p[i] = (p[i + 1] + a[i]) % mod;
}
for (i = 1; i <= n - 1; i++)
{
count = (count + (a[i] * p[i + 1]) % mod) % mod;
}
cout << count << " " << endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
#define N 100020
#define ll long long
using namespace std;
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch>'9'||ch<'0')ch=='-'&&(f=0)||(ch=getchar());
while(ch<='9'&&ch>='0')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return f?x:-x;
}
int a[3][N], f[N];
bool rev[2];
int main(int argc, char const *argv[]) {
int n = read();
for (int i = 1; i <= n; ++ i) {
a[0][i] = read();
}
for (int i = 1; i <= n; ++ i) {
a[1][i] = read();
}
for (int i = 1; i <= n; ++ i) {
a[2][i] = read();
}
for (int i = 1; i <= n; ++ i) {
if (a[1][i] % 3 != 2 || abs(a[0][i] - a[1][i]) > 1
|| abs(a[2][i] - a[1][i]) > 1) {
return puts("No"), 0;
}
f[i] = a[1][i] / 3 + 1;
if ((f[i] & 1) != (i & 1)) {
return puts("No"), 0;
}
if (a[0][i] > a[1][i]) {
rev[f[i] & 1] ^= 1;
}
}
for (int i = 1; i <= n; ++ i) {
while (f[i] != i) {
rev[i&1^1] ^= 1;
swap(f[i], f[f[i]]);
}
}
if (rev[0] || rev[1]) {
puts("No");
} else {
puts("Yes");
}
return 0;
}
/*
将每一列变成 x/3+1,如果是倒序则加个负号。
一次操作可以看做:
1 2 3 4 5 -> -3 -2 -1 4 5
let's do some buffs
[ 1 2 3] 4 5
-3 -2[-1 4 5]
[-3 -2 -5]-4 1
5 2[ 3 -4 1]
[ 5 2 -1] 4 -3
1 -2[-5 4 -3]
1 -2 3 -4 5
*/ | #include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int mat[4][N], val[N], n, num[2], v[N];
void add(int x, int val) {
for (; x <= n; x += ((x) & (-(x))))
v[x] += val;
}
int query(int x) {
int ret = 0;
for (; x; x -= ((x) & (-(x))))
ret += v[x];
return ret;
}
void fail() {
puts("No");
exit(0);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= 3; ++i)
for (int j = 1; j <= n; ++j)
scanf("%d", &mat[i][j]);
for (int i = 1; i <= n; ++i) {
if (mat[1][i] % 3 == 0) {
if (mat[2][i] == mat[1][i] - 1 && mat[3][i] == mat[1][i] - 2)
val[i] = -mat[1][i] / 3;
else fail();
}
else if (mat[3][i] % 3 == 0) {
if (mat[2][i] == mat[3][i] - 1 && mat[1][i] == mat[3][i] - 2)
val[i] = mat[3][i] / 3;
else fail();
}
else fail();
}
for (int i = 1; i <= n; i += 2) {
if (val[i] < 0) num[1] ^= 1, val[i] = -val[i];
if (val[i] % 2 == 0) fail();
}
for (int i = 2; i <= n; i += 2) {
if (val[i] < 0) num[0] ^= 1, val[i] = -val[i];
if (val[i] & 1) fail();
}
for (int i = 1; i <= n; i += 2) {
if ((query(n) - query(val[i])) & 1) num[0] ^= 1;
add(val[i], 1);
}
memset(v, 0, sizeof v);
for (int i = 2; i <= n; i += 2) {
if ((query(n) - query(val[i])) & 1) num[1] ^= 1;
add(val[i], 1);
}
if ((!num[0]) && (!num[1]))
puts("Yes");
else puts("No");
return 0;
} | 1 |
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
const int MAX = 30;
int dp[MAX + 1];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
dp[0] = 1;
for (int i = 0; i <= MAX - 1; i++) {
for (int j = 1; j <= 3; j++) {
if (i + j <= MAX)
dp[i + j] += dp[i];
}
}
int n;
while (cin >> n , n) {
cout << ((dp[n] + 9) / 10 + 364) / 365 << endl;
}
} | #include<iostream>
using namespace std;
int main()
{
int n;
long long int e[4000], p[4000], q[4000], sum[4000];
bool ej[4000];
while( cin >> n )
{
if( n == 0 )
break;
for( int i = 0; i < 4000; i++ )
{
sum[i] = 0;
ej[i] = false;
}
for( int i = 0; i < n; i++ )
{
cin >> e[i] >> p[i] >> q[i];
sum[e[i]] += p[i] * q[i];
}
bool judge = false;
for( int i = 0; i < n; i++ )
{
if( sum[e[i]] >= 1000000 && ej[e[i]] == false )
{
cout << e[i] << endl;
judge = true;
ej[e[i]] = true;
}
}
if( judge == false )
cout << "NA" << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template <class T>
using v = vector<T>;
template <class T>
using vv = v<v<T>>;
#define ALL(c) (c).begin(), (c).end()
int digit(ll i)
{
ll r = i;
int ret = 0;
while (r != 0)
{
r /= 10;
ret++;
}
return ret;
}
int main()
{
ll N;
cin >> N;
int ans = INT32_MAX;
for (ll i = 1; i <= sqrt(N); i++)
{
int f = INT32_MAX;
if (N == N / i * i)
{
f = max(digit(i), digit(N / i));
}
ans = min(ans, f);
}
cout << ans << endl;
return 0;
} | #include<bits/stdc++.h>
#define ll long long
#define ld long double
#define F first
#define S second
#define Tsetso ios_base::sync_with_stdio(0) ; cin.tie(0) ;
using namespace std;
const ll N = 1e5+10, inf = 1e3+5, mod = 1e9+7, NN = 1e5+10;
int a ;
int main()
{
Tsetso ;
int n , mn = 1e9 ;
cin >> n ;
for ( int i = 0 ; i <n ; i++)
{
int cnt = 0 ;
cin >> a ;
while (a%2 == 0)
a /= 2 , cnt++ ;
mn = min(cnt,mn) ;
}
cout << mn ;
}
| 0 |
#include<bits/stdc++.h>
#define L long long
#define vi vector<int>
#define pb push_back
#define pi pair<int,int>
#define pii pair<pi,int>
#define aa first
#define bb second
#define xx aa.aa
#define yy aa.bb
#define zz bb
#define mp make_pair
#define mpp(a,b,c) mp(mp(a,b),c)
using namespace std;
int n,m,p;
L f[600000],g[600000],q,ans;
inline void modify(L f[],int i,int j,int k,int x,L p)
{
f[i]=min(f[i],p);
if(j<k)
if(x<=(j+k>>1))
modify(f,i<<1,j,j+k>>1,x,p);
else
modify(f,i<<1|1,(j+k>>1)+1,k,x,p);
}
inline L minn(L f[],int i,int j,int k,int l,int r)
{
if(l<=j && k<=r)
return f[i];
else
{
L p=1e18;;
if(l<=(j+k>>1))
p=min(p,minn(f,i<<1,j,j+k>>1,l,r));
if(r>(j+k>>1))
p=min(p,minn(f,i<<1|1,(j+k>>1)+1,k,l,r));
return p;
}
}
int main()
{
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
int i,j;
L k;
scanf("%d%d",&n,&m);
for(p=1;p<n;p<<=1);
for(i=1;i<2*p;i++)
f[i]=g[i]=1e18;
scanf("%d%d",&i,&j);
modify(f,1,1,p,i,-i);
modify(g,1,1,p,i,i);
while(m--)
{
scanf("%d",&i);
q+=abs(i-j);
ans+=abs(i-j);
k=min(minn(f,1,1,p,1,i)+i,minn(g,1,1,p,i,n)-i)-abs(i-j);
modify(f,1,1,p,j,k-j);
modify(g,1,1,p,j,k+j);
ans=min(ans,q+k);
j=i;
}
printf("%lld\n",ans);
return 0;
}
| // Crt. 2019-12-11 21:17:53
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#define N 200005
using namespace std;
typedef long long ll;
const ll INF = 1e15;
struct Segt {
#define ls (p << 1)
#define rs (p << 1 | 1)
ll sum[N << 2], tag[N << 2];
Segt() {
memset(sum, 0, sizeof sum);
memset(tag, 0, sizeof tag);
}
inline void add(int p, ll w) {
sum[p] += w, tag[p] += w;
}
inline void pushdown(int p) {
if (!tag[p]) return ;
add(ls, tag[p]);
add(rs, tag[p]);
tag[p] = 0;
}
void Modify(int l, int r, int L, int R, ll w, int p) {
if (L <= l && r <= R) return add(p, w);
int mid = (l + r) >> 1;
pushdown(p);
if (L <= mid) Modify(l, mid, L, R, w, ls);
if (mid < R) Modify(mid + 1, r, L, R, w, rs);
sum[p] = min(sum[ls], sum[rs]);
}
ll Query(int l, int r, int L, int R, int p) {
if (L <= l && r <= R) return sum[p];
int mid = (l + r) >> 1; ll res = 0x3f3f3f3f3f3f3f3fll;
pushdown(p);
if (L <= mid) res = Query(l, mid, L, R, ls);
if (mid < R) res = min(res, Query(mid + 1, r, L, R, rs));
return res;
}
#undef ls
#undef rs
} x, y, z; // y + z -
inline int Abs(int t) {
return t < 0 ? -t : t;
}
int n, q, a, b, pos[N];
int main() {
#ifdef LOCAL_JUDGE
freopen("work.in", "r", stdin);
freopen("work.out", "w", stdout);
#endif
cin >> n >> q >> a >> b;
for (int i = 1; i <= q; ++i)
scanf("%d", pos + i);
x.Modify(1, n, 1, n, INF, 1);
y.Modify(1, n, 1, n, INF, 1);
z.Modify(1, n, 1, n, INF, 1);
x.Modify(1, n, a, a, -INF, 1);
y.Modify(1, n, a, a, -INF + a, 1);
z.Modify(1, n, a, a, -INF - a, 1);
pos[0] = b;
for (int i = 1; i <= q; ++i) {
ll tmp = min(y.Query(1, n, pos[i], n, 1) - pos[i], z.Query(1, n, 1, pos[i], 1) + pos[i]);
x.Modify(1, n, 1, n, Abs(pos[i] - pos[i - 1]), 1);
y.Modify(1, n, 1, n, Abs(pos[i] - pos[i - 1]), 1);
z.Modify(1, n, 1, n, Abs(pos[i] - pos[i - 1]), 1);
ll tmp2 = x.Query(1, n, pos[i - 1], pos[i - 1], 1);
//cout << tmp << " " << tmp2 << " #\n";
if (tmp < tmp2) x.Modify(1, n, pos[i - 1], pos[i - 1], tmp - tmp2, 1), tmp2 = tmp;
//cout << x.Query(1, n, pos[i - 1], pos[i - 1], 1) << endl;
y.Modify(1, n, pos[i - 1], pos[i - 1], tmp2 + pos[i - 1] - y.Query(1, n, pos[i - 1], pos[i - 1], 1), 1);
z.Modify(1, n, pos[i - 1], pos[i - 1], tmp2 - pos[i - 1] - z.Query(1, n, pos[i - 1], pos[i - 1], 1), 1);
}
cout << x.Query(1, n, 1, n, 1) << endl;
return 0;
}
| 1 |
//Mahir Ratanpara (DA-IICT)
#include<bits/stdc++.h>
using namespace std;
#define Ff(i,a,n) for(i=a;i<n;i++)
#define Fr(i,a,n) for(i=a;i>n;i--)
#define ll long long
#define FAST ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define pb push_back
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
typedef pair<ll, ll> pa;
typedef vector<ll> vc;
typedef vector<pa> vp;
typedef vector<vc> vvc;
int mpow(int base, int exp);
void ipgraph(ll n,ll m);
void dfs(int u, int par);
const int mod = 1000000007;
const int N = 3e5, M = N;
//=======================
vc g[N];
int main() {
FAST;
ll t, i, j, k, p, q, r, x, y, u, v, n, m;
cin>>n;
vc a(n);
Ff(i,0,n)
cin>>a[i];
ll ans=0;
sortall(a);
Ff(i,0,n)
{
Ff(j,i+1,n)
{
Ff(k,j+1,n)
{
if (a[i] != a[j] && a[j] != a[k] && a[k] < a[i] + a[j])
ans++;
}
}
}
cout<<ans;
return 0;
}
int mpow(int base, int exp) {
base %= mod;
int result = 1;
while (exp > 0) {
if (exp & 1) result = ((ll)result * base) % mod;
base = ((ll)base * base) % mod;
exp >>= 1;
}
return result;
}
void ipgraph(ll n, ll m){
int i, u, v;
while(m--){
cin>>u>>v;
g[u-1].pb(v-1);
g[v-1].pb(u-1);
}
}
void dfs(int u, int par){
for(int v:g[u]){
if (v == par) continue;
dfs(v, u);
}
} | #include <iostream>
using namespace std;
int func(int x, int y, int z){
// cout << x << " " << y << " " << z << endl;
if (x==y||y==z||z==x) return false;
if (x+y>z&&y+z>x&&z+x>y){
// cout << "ok" << endl;
return true;
}
return false;
}
int main(void){
int n,ans;
int len[200];
cin >> n;
for (int i=0; i<n; i++) cin >> len[i];
ans = 0;
for (int i=0; i<n; i++){
for (int j=i+1; j<n; j++){
for (int k=j+1; k<n; k++){
ans += func(len[i], len[j], len[k]);
}
}
}
cout << ans << endl;
return 0;
} | 1 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, ss, ee) for (int i = ss; i < ee; ++i)
string a26(ll n) {
string res = "";
while (n) {
--n;
res += (char)('a' + (n % 26));
n /= 26;
}
reverse(res.begin(), res.end());
return res;
}
ll N;
void input() { cin >> N; }
void solve() { cout << a26(N) << endl; }
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
input();
solve();
} | #include <bits/stdc++.h>
using namespace std;
queue<char> que[3];
int rec(int i){
if(que[i].empty()) return i;
char q = que[i].front();
que[i].pop();
return rec(q - 'a');
}
int main(){
for(int i = 0; i < 3; i++){
string s;
cin >> s;
for(int j = 0; j < s.size(); j++){
que[i].push(s[j]);
}
}
cout << (char)(rec(0) + 'A') << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
// #undef DEBUG // Uncomment this line to forcefully disable debug print.
#if DEBUG
template <typename T>
void debug(const T& value) {
std::cerr << value;
}
template <typename T, typename... Ts>
void debug(const T& value, const Ts&... args) {
std::cerr << value << ", ";
debug(args...);
}
#define dbg(...) \
do { \
cerr << #__VA_ARGS__ << ": "; \
debug(__VA_ARGS__); \
cerr << " (L" << __LINE__ << ")\n"; \
} while (0)
#else
#define dbg(...)
#endif
void read_from_cin() {}
template <typename T, typename... Ts>
void read_from_cin(T& value, Ts&... args) {
std::cin >> value;
read_from_cin(args...);
}
#define in(type, ...) \
type __VA_ARGS__; \
read_from_cin(__VA_ARGS__);
template <typename T>
void write_to_cout(const T& value) {
std::cout << value << '\n';
}
template <typename T, typename... Ts>
void write_to_cout(const T& value, const Ts&... args) {
std::cout << value << ' ';
write_to_cout(args...);
}
#define out(...) write_to_cout(__VA_ARGS__);
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
using ll = long long;
template <typename T>
using V = std::vector<T>;
template <typename T>
using VV = V<V<T>>;
using namespace std;
template <typename T>
class BIT {
public:
BIT(int n) : v_(n) {}
T Sum(int i) const {
T ret = 0;
while (i >= 0) {
ret += v_[i];
i = (i & (i + 1)) - 1;
}
return ret;
}
T Get(int i) const { return Sum(i) - Sum(i - 1); }
void Add(int i, T v) {
while (i < v_.size()) {
v_[i] += v;
i |= i + 1;
}
}
private:
std::vector<T> v_;
};
using P = pair<int, int>;
struct Query {
int i, l, r;
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
in(int, N, M, Q);
V<P> trains;
rep(i, M) {
in(int, L, R);
trains.push_back({L - 1, R - 1});
}
sort(all(trains), [](const P& a, const P& b) { return a.second < b.second; });
V<Query> queries;
rep(i, Q) {
in(int, p, q);
queries.push_back({i, p - 1, q - 1});
}
sort(all(queries), [](const Query& a, const Query& b) { return a.r < b.r; });
V<int> ans(Q);
BIT<int> bit(N);
auto ti = trains.begin();
auto qi = queries.begin();
rep(r, N) {
for (; ti != trains.end() && ti->second <= r; ++ti) {
bit.Add(ti->first, 1);
}
for (; qi != queries.end() && qi->r <= r; ++qi) {
ans[qi->i] = bit.Sum(qi->r) - bit.Sum(qi->l - 1);
}
}
rep(i, Q) out(ans[i]);
}
| #include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl "\n"
using namespace std;
using ll = long long;
const ll MOD = 1000000007LL; // = 10^9 + 7
const double PI = 3.14159265358979;
int dp[505][505];
void solve()
{
int n, m, q, l, r, ql, qr;
cin >> n >> m >> q;
for(int i = 0; i < m; ++i)
{
cin >> l >> r;
dp[l][r]++;
}
for(int y = 1; y <= n; ++y)
{
for(int x = 1; x <= n; ++x)
{
dp[y][x] += dp[y][x - 1];
}
}
for(int x = 1; x <= n; ++x)
{
for(int y = 1; y <= n; ++y)
{
dp[y][x] += dp[y - 1][x];
}
}
for(int i = 0; i < q; ++i)
{
cin >> ql >> qr;
ql--;
cout << dp[qr][qr] - dp[ql][qr] - dp[qr][ql] + dp[ql][ql] << endl;
}
}
int main()
{
fastio;
solve();
return 0;
} | 1 |
#include <bits/stdc++.h>
using namespace std;
template<typename T>
struct Segtree {
int n;
T e;
vector<T> dat;
typedef function<T(T a, T b)> Func;
Func f;
Segtree(){}
Segtree(int n_input, Func f_input, T e_input){
initialize(n_input, f_input, e_input);
}
void initialize(int n_input, Func f_input, T e_input){
f = f_input;
e = e_input;
n = 1;
while(n < n_input) n <<= 1;
dat.resize(2*n-1, e);
}
void update(int k, T a){
k += n - 1;
dat[k] = a;
while(k > 0){
k = (k - 1)/2;
dat[k] = f(dat[2*k+1], dat[2*k+2]);
}
}
T get(int k){
return dat[k+n-1];
}
T between(int a, int b){
return query(a, b+1, 0, 0, n);
}
T query(int a, int b, int k, int l, int r){
if(r<=a || b<=l) return e;
if(a<=l && r<=b) return dat[k];
T vl = query(a, b, 2*k+1, l, (l+r)/2);
T vr = query(a, b, 2*k+2, (l+r)/2, r);
return f(vl, vr);
}
};
#include <bits/stdc++.h>
using namespace std;
struct UnionFind {
vector<int> par;
vector<int> sz;
UnionFind(int n=0){
if(n>0) initialize(n);
}
void initialize(int n){
par.resize(n);
sz.resize(n);
for(int i=0; i<n; i++){
par[i] = i;
sz[i] = 1;
}
}
int find(int x){
if(par[x] == x){
return x;
}else{
return par[x] = find(par[x]);
}
}
void unite(int x, int y){
x = find(x);
y = find(y);
if(x == y) return;
if(sz[x] < sz[y]){
par[x] = y;
sz[y] += sz[x];
}else{
par[y] = x;
sz[x] += sz[y];
}
}
bool same(int x, int y){
return find(x) == find(y);
}
};
int main(){
int N;
int64_t D, A[200000];
cin >> N >> D;
for(int i=0; i<N; i++) cin >> A[i];
typedef pair<int64_t, int> P;
const int64_t INF = 1e18;
const P INFP = {INF, 0};
Segtree<P> stL(N, [](P a, P b){ return min(a, b);}, INFP);
Segtree<P> stR(N, [](P a, P b){ return min(a, b);}, INFP);
vector<int> order;
for(int i=0; i<N; i++) order.push_back(i);
sort(order.begin(), order.end(), [&](int i, int j){ return A[i] < A[j];});
vector<vector<int64_t>> edges;
for(int i : order){
auto p = stL.between(0, i);
if(p != INFP){
int64_t cost = p.first + A[i] - (N-i)*D;
edges.push_back({cost, i, p.second});
}
p = stR.between(i, N-1);
if(p != INFP){
int64_t cost = p.first + A[i] - i*D;
edges.push_back({cost, i, p.second});
}
stL.update(i, {A[i] + (N-i)*D, i});
stR.update(i, {A[i] + i*D, i});
}
sort(edges.begin(), edges.end());
UnionFind uf(N);
int64_t ans = 0;
for(auto& e : edges){
if(!uf.same(e[1], e[2])){
uf.unite(e[1], e[2]);
ans += e[0];
}
}
cout << ans << endl;
return 0;
} | #include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
int main(){
ll N,D;
ll A[200005];
cin>>N>>D;
ll ret = D*(N-1);
for(int i = 0;i < N;i++){
cin>>A[i];
ret += A[i];
}
for(int i = 1;i < N;i++){
A[i]=min(A[i-1]+D,A[i]);
}
for(int i = N-2;i >=0;i--){
A[i]=min(A[i+1]+D,A[i]);
}
for(int i = 1;i < N-1;i++){
ret += A[i];
}
cout<<ret<<endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string S;
cin >> S;
S = regex_replace(S, regex("hi"), "");
cout << ((S.size() == 0) ? "Yes" : "No") << "\n";
} | #include <iostream>
using namespace std;
string s;
int main() {
cin >> s;
if (s.length() % 2 != 0) {
cout << "No\n";
return 0;
}
bool works = true;
for (int i=0; i<s.length(); i+=2) {
if (s[i] != 'h' || s[i + 1] != 'i')
works = false;
}
if (works)
cout << "Yes\n";
else
cout << "No\n";
} | 1 |
#include <iostream>
#include <string>
using namespace std;
int main(){
string s;
cin >> s;
string ans = "";
for(int i = 0; i+8 < s.length(); i++) ans += s[i];
cout << ans << endl;
return 0;
} | #include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <vector>
#include <stack>
#include <queue>
#include <tuple>
#include <cassert>
#include <functional>
#include <set>
#include <deque>
#include <list>
using namespace std;
typedef long long ll;
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
#define LNF __LONG_LONG_MAX__
const ll MOD = 1000000007;
const int MAX = 200000;
long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++){
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
long long COM(int n, int k){
if (n < k) return 0;
if (n < 0 || k < 0) return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
// 法mでのaの逆元を計算
long long modinv(long long a, long long m) {
long long b = m, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b; swap(a, b);
u -= t * v; swap(u, v);
}
u %= m;
if (u < 0) u += m;
return u;
}
// a^n mod を計算する
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
//最大公約数
long long gcd(long long x, long long y){
if(y==0) return x;
else return gcd(y,x%y);
}
//最小公倍数
long long lcm(long long x, long long y){
return x / gcd(x, y) * y;
}
// nが大きく,kが10^7程度の時の二項係数
long long com[200005];
void combination(long long n,long long k){
com[0] = 1;
for(int i=1;i<=k;i++){
com[i] = (com[i-1]*(n-i+1)%MOD)*modinv(i,MOD)%MOD;
}
}
int main(){
int A,B,C,D; cin >> A >> B >> C >> D;
cout << min(A,B)+min(C,D) << endl;
return 0;
}
| 0 |
//include
//------------------------------------------
#include <string>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <complex>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <ctime>
// namespace
using namespace std;
// type alias
using ll = long long;
using ull = unsigned long long;
using comp = complex<double>;
// constant
static const ll MOD = 1000000007LL;
static const double PI = 3.14159265358979323846;
// conversion
inline ll toint(string s) { ll v; istringstream sin(s); sin >> v; return v; }
template<class t> inline string tostring(t x) { ostringstream sout; sout << x; return sout.str(); }
// print
#define RET(x) return cout << x << endl, 0;
// for loop
#define REP(i, a, b) for ((i) = (ll)(a);(i) < (ll)(b);(i)++)
#define REPD(i, a, b) for (ll i = (ll)(a);(i) < (ll)(b);(i)++)
#define REPI(v, vs) for (auto& v : vs)
//debug
#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x) cerr << #x << " = " << (x) << " (l" << __LINE__ << ")" << " " << __FILE__ << endl
#define MAX_VALUE 9223372036854775807
template<class Integer>
class BinaryIndexedTree
{
private:
ull maxSize_;
vector<Integer> values_;
public:
BinaryIndexedTree(ull maxSize) : maxSize_(maxSize), values_(maxSize_, 0) {}
void update(ll index, Integer increment)
{
if (maxSize_ <= index)
{
throw invalid_argument("Index should be less than maxSize.");
}
++index;
while (index <= maxSize_)
{
values_[index - 1] += increment;
index += index & -index;
}
}
Integer query(ll index)
{
if (maxSize_ <= index)
{
throw invalid_argument("Index should be less than maxSize.");
}
++index;
Integer res = 0;
while (index > 0)
{
res += values_[index - 1];
index -= index & -index;
}
return res;
}
};
using bit = BinaryIndexedTree<ll>;
int solve() {
ll n, q;
cin >> n >> q;
vector<ll> as(n);
vector<tuple<ll, ll, ll>> rlis;
ll l, r;
REPD(i, 0, n) cin >> as[i];
REPD(i, 0, q){
cin >> l >> r;
--l, --r;
rlis.emplace_back(r, l, i);
}
sort(rlis.begin(), rlis.end());
bit counters(n);
map<ll, ll> color_to_max_idx;
vector<ll> results(q);
ll idx, curr_r = 0;
REPD(i, 0, q) {
tie(r, l, idx) = rlis[i];
while(curr_r <= r) {
counters.update(curr_r, 1LL);
if (color_to_max_idx.count(as[curr_r]) != 0LL) {
counters.update(color_to_max_idx[as[curr_r]], -1LL);
}
color_to_max_idx[as[curr_r]] = curr_r;
++curr_r;
}
results[idx] = counters.query(r) - (l > 0LL ? counters.query(l - 1LL) : 0LL);
}
REPD(i, 0, q) cout << results[i] << endl;
return 0;
}
//main function
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
solve();
// ll t;
// cin >> t;
// REPD(i, 0, t) solve();
return 0;
}
| #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define rep(i,N) for(int i=0;i<(int)N;i++)
#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
static inline void PUT(char c)
{
static char buf[1<<15],*ptr=buf;
if(ptr==buf+strlen(buf)||c==0){fwrite(buf,1,ptr-buf,stdout),ptr=buf;}*ptr++=c;
}
static inline int IN(void)
{
int x=0,f=0,c=getchar();while(c<48||c>57){f^=c==45,c=getchar();}
while(c>47&&c<58){x=x*10+c-48,c=getchar();}return f?-x:x;
}
static inline void OUT(int a)
{
if(a<0)PUT('-'),a=-a;
int d[40],i=0;do{d[i++]=a%10;}while(a/=10);
while(i--){PUT(d[i]+48);}PUT('\n');
}
int N=0;
static inline void Add(int *fwt,int i,int v){while(i<N){fwt[i]+=v;i|=i+1;}}
static inline int Sum(int *fwt,int i){int v=0;while(i>=0){v+=fwt[i];i=(i&(i+1))-1;}return v;}
int main()
{
N=IN();int Q=IN(),after[500001],dat[500000],fenwick[500000],id[500001],left[500001],pos[500000],result[500000];
rep(i,N){dat[i]=IN()-1;}
rep(i,Q){left[i+1]=IN()-1;int r=IN()-1;after[i+1]=id[r];id[r]=i+1;}
for(int r=0;r<N;r++)
{
if(pos[dat[r]]){Add(fenwick,pos[dat[r]]-1,-1);}
Add(fenwick,r,1);
for(int i=id[r];i;i=after[i]){result[i-1]=Sum(fenwick,r)-Sum(fenwick,left[i]-1);}
pos[dat[r]]=r+1;
}
rep(i,Q){OUT(result[i]);}
} | 1 |
#include <bits/stdc++.h>
#define loop2(i, s, n, a) for (int i = int(s); i < int(n); i += a)
#define loop(i, s, n) loop2(i, s, n, 1)
#define rep(i, n) loop(i, 0, n)
#define pb push_back
#define all(in) in.begin(),in.end()
using ll = long long;
using ull = unsigned long long;
using namespace std;
const ll MOD = 1e9+7;
ll dp[101][8];
int main(){
ll h, w, k; cin >> h >> w >> k;
if(w == 1){ cout << 1 << endl; return 0; }
dp[0][0] = 1;
rep(i,h){
rep(j,1<<(w-1)){
vector<bool> count(w-1,false);
rep(k,w-1) if(j & 1<<k) count[k] = true;
bool check = false;
rep(k,w-2) if(count[k] && count[k+1]) check = true;
if(check) continue;
rep(k,w-1){
if(count[k]){
dp[i+1][k+1] += dp[i][k];
dp[i+1][k+1] %= MOD;
dp[i+1][k] += dp[i][k+1];
dp[i+1][k] %= MOD;
}
else if(k == 0){
dp[i+1][0] += dp[i][0];
dp[i+1][0] %= MOD;
}
else if(!count[k-1]){
dp[i+1][k] += dp[i][k];
dp[i+1][k] %= MOD;
}
if(!count[k] && k == w-2){
dp[i+1][w-1] += dp[i][w-1];
dp[i+1][w-1] %= MOD;
}
}
}
}
cout << dp[h][k-1] << endl;
}
| #include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1005,M=1e9+7;
int n,A,B,C,D,fac[N],inv[N],dp[N][N];
int ksm(int x,int y){
int ans=1;
for (;y;y>>=1,x=x*x%M)
if (y&1)(ans*=x)%=M;
return ans;
}
signed main(){
scanf("%lld%lld%lld%lld%lld",&n,&A,&B,&C,&D);
dp[0][A-1]=1;
fac[0]=inv[0]=1;
for (int i=1;i<=n;i++)fac[i]=fac[i-1]*i%M,inv[i]=ksm(fac[i],M-2);
for (int i=0;i<=n;i++)
for (int j=A;j<=B;j++){
dp[i][j]=dp[i][j-1];
for (int k=C,p=ksm(inv[j],C);k<=D;k++,(p*=inv[j])%=M){
if (i-j*k<0)break;
(dp[i][j]+=dp[i-j*k][j-1]*p%M*inv[k])%=M;
}
}
printf("%lld\n",dp[n][B]*fac[n]%M);
}
| 0 |
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef double db;
#define fr first
#define sc second
#define pb push_back
#define rep(i,x) for(ll i=0;i<x;i++)
#define rep1(i,x) for(ll i=1;i<=x;i++)
#define rrep(i,x) for(ll i=x-1;i>=0;i--)
#define rrep1(i,x) for(ll i=x;i>0;i--)
ll ans=0;
ll n;
string s;
ll cnt=0;
P p[1000001];
ll sz;
int main()
{
cin>>s;
sz=0;
n=s.length();
rep(i,n){
if(i==0){
if(s[0]=='J') p[sz]=P(1,0);
else if(s[0]=='O') p[sz]=P(1,1);
else p[sz]=P(1,2);
sz++;
}
else{
if(s[i]!=s[i-1]){
if(s[i]=='J') p[sz]=P(1,0);
else if(s[i]=='O') p[sz]=P(1,1);
else p[sz]=P(1,2);
sz++;
}
else{
p[sz-1].fr++;
}
}
}
rep(i,sz-2){
if(p[i].sc==0&&p[i+1].sc==1&&p[i+2].sc==2&&p[i].fr>=p[i+1].fr&&p[i+1].fr<=p[i+2].fr){
ans = max(ans , p[i+1].fr );
}
}
cout<<ans<<endl;
} | #pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#include<cmath>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int,int> pii;
const int inf = 0x3f3f3f3f,oo = inf;
#define Rep(i,a,b) for(RG int i=(a);i<=int(b);++i)
#define Dep(i,a,b) for(RG int i=(a);i>=int(b);--i)
#define rep(i,a,b) for(RG int i=(a);i<int(b);++i)
#define pi 3.14159265358979323846
#define IL inline
#define pc putchar
#define RG register
#define gc getchar
#define fi first
#define se second
#define debug(x) printf(#x" = %d\n",x);
//char __wzp[1<<15|1],*__S=__wzp+32768;
//#define gc() (__S>=__wzp+32768?(__wzp[fread(__wzp,sizeof(char),1<<15,stdin)]=EOF),*((__S=__wzp)++):*(__S++))
#define rd read
#define mem(x,v) memset(x,v,sizeof(x))
#define sqr(x) ((x)*(x))
#define lowbit(x) ((x)&(-(x)))
inline ll read(){
RG ll x=0,f=1;RG char c=gc();
for(;!isdigit(c);c=gc())if(c=='-')f=-1;
for(;isdigit(c);c=gc())x=(x<<1)+(x<<3)+(c^48);
return x*f;
}
IL double readdb(){
RG double x=0,p=0.1;RG char f=0,c=gc();
for(;!isdigit(c);c=gc())f|=(c=='-');
for(;isdigit(c);c=gc())x=x*10+(c^48);
if(c=='.')for(c=gc();isdigit(c);c=gc(),p/=10)x=x+(c^48)*p;
return f?-x:x;
}
#define rdb() readdb()
void write(ll x){if(x<0)x=-x,pc('-');if(x>=10)write(x/10);putchar(x%10+'0');}
void writeln(ll x){write(x);puts("");}
int color[2][1005][1005];
int db[2<<17],n;
const int dx[] = {1,1,-1,-1};
const int dy[] = {1,-1,1,-1};
void dfs(int x,int y,int d,int col,int c){
color[c][x][y] = col;
for(int i=0;i*i<=d;i++){
if(db[d-i*i]<0)continue;
int j = db[d-i*i];
rep(r,0,4){
int tx = x+dx[r]*i;
int ty = y+dy[r]*j;
if(tx<0||ty<0||tx>=2*n||ty>=2*n||color[c][tx][ty]) continue;
dfs(tx,ty,d,col==1?2:1,c);
}
}
}
int main(){
n = rd();
rep(i,0,2<<17) db[i] = -1;
for(int i=0;i*i<(2<<17);++i) db[i*i] = i;
rep(i,0,2){
int d = rd();
rep(x,0,2*n)
rep(y,0,2*n)
if(!color[i][x][y])
dfs(x,y,d,1,i);
}
vector<pair<int,int> > a;
rep(x,0,2*n)
rep(y,0,2*n)
if(color[0][x][y]==1&&color[1][x][y]==1)
a.push_back(make_pair(x,y));
rep(i,0,n*n)
printf("%d %d\n",a[i].fi,a[i].se);
}
| 0 |
# include <bits/stdc++.h>
using namespace std ;
bool is_war ( const vector <int >& x , const vector <int >& y ) {
for ( int Z = -100; Z <= 100; ++ Z ) {
bool is_ok = true ;
for ( int i = 0; i < x . size (); ++ i ) {
if ( x [ i ] >= Z ) is_ok = false ;
}
for ( int i = 0; i < y . size (); ++ i ) {
if ( y [ i ] < Z ) is_ok = false ;
}
if ( is_ok ) return false ;
}
return true ;
}
int main () {
int N , M ; cin >> N >> M ;
vector <int > x ( N +1) , y ( M +1);
cin >> x [0] >> y [0];
for ( int i = 0; i < N ; ++ i ) {
cin >> x [ i +1];
}
for ( int i = 0; i < M ; ++ i ) {
cin >> y [ i +1];
}
if ( is_war (x , y )) {
cout << " War " << endl ;
}
else {
cout << "No War " << endl ;
}
}
| #include <bits/stdc++.h>
#define ll long long
#define sz(x) (int)x.size()
using namespace std;
int main(){
int n,m,x,y;
cin>>n>>m>>x>>y;
vector<int> a(n),b(m);
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=0;i<m;i++){
cin>>b[i];
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
for(int i=0;i<101;i++){
if(a[n-1]<i && i<=b[0] && i>x && i<y){
cout<<"No War";
return 0;
}
}
cout<<"War";
} | 1 |
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
using namespace std;
typedef pair<int, int> P;
int k, a[100000], dist[100000];
vector<int> v[100000];
vector<P> w;
bool b[100000];
void cal(int x, int y){
dist[x] = y;
b[x] = false;
w.push_back(P(y, x));
for(int i=0;i<v[x].size();i++){
cal(v[x][i], y+1);
}
}
int hin(int x, int y){
if(b[x])return 0;
b[x] = true;
if(x == 0 || (a[x] == 0 && y == k-1))return 0;
if(y == k-1)return 1;
return hin(a[x], y+1);
}
int main() {
int n, ans = 0;
cin >> n >> k >> a[0];
if(--a[0] != 0)ans++;
for(int i=1;i<n;i++){
cin >> a[i];
v[--a[i]].push_back(i);
}
cal(0, 0);
sort(w.begin(), w.end(), greater<P>());
for(int i=0;i<w.size();i++){
int p = w[i].second;
if(!b[p]){
ans += hin(p, 0);
}
}
cout << ans << endl;
return 0;
}
| #include <iostream>
#include <array>
#include <algorithm>
#include <vector>
#include <bitset>
#include <set>
#include <unordered_set>
#include <cmath>
#include <complex>
#include <deque>
#include <iterator>
#include <numeric>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <limits>
#include <iomanip>
#include <functional>
#include <cassert>
using namespace std;
using ll=long long;
template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define ALL(v) v.begin(),v.end()
template < class T > inline bool chmax(T& a, T b) {if (a < b) { a=b; return true; } return false; }
template < class T > inline bool chmin(T& a, T b) {if (a > b) { a=b; return true; } return false; }
#define DEBUG_VLL(vec) for(int sz=0;sz<int(vec.size());sz++) std::cerr<<vec[sz]<<(sz==vec.size()-1?'\n':' ');
const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, k; cin >> n >> k;
V<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
a[i]--;
}
V< V<int> > edges(n);
for (int i = 1; i < n; i++) edges[a[i]].emplace_back(i);
int ans = 0;
auto dfs = [&](auto && self, int i) -> int {
int d = 0;
for (int e: edges[i]) {
int chd = self(self, e);
if (chd == k && i != 0) ans++;
else chmax(d, chd);
}
return d + 1;
};
dfs(dfs, 0);
if (a[0] != 0) ans++;
cout << ans << '\n';
return 0;
}
| 1 |
//Name: Space Coconut Crab
//Level: 2
//Category: 数学,探索
//Note:
/**
* zの範囲は0≦z≦E^(1/3)、yの範囲は0≦y≦E^(1/2)で表される。
* zとyが決まればxも一意に決まるので、zとyを全探索し、x,y,z≧0を満たすような組み合わせについてx+y+zの最小値を求めればよい。
*
* オーダーは O(E^(5/6))。
*/
#include <iostream>
#include <algorithm>
using namespace std;
bool solve(bool first) {
int E;
if(!(cin >> E)) return false;
if(!E) return false;
int ans = E;
for(int z = 0; z*z*z <= E; ++z) {
for(int y = 0; y*y <= E; ++y) {
const int x = E - z*z*z - y*y;
if(x >= 0) {
ans = min(ans, x+y+z);
}
}
}
cout << ans << endl;
return true;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(0);
cout.setf(ios::fixed);
cout.precision(10);
bool first = true;
while(solve(first)) {
first = false;
}
return 0;
} | #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
using namespace std;
#define FOR(i,k,n) for(int i=(k); i<(int)n; ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cout<<*i<<" "; cout<<endl; }
typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int main(){
int n,m;
while(cin>>n>>m,n){
vector<int> c(n+1);
REP(i,n)cin>>c[i+1];
vector<int> a;
REP(i,n+1)REP(j,n+1)a.push_back(c[i]+c[j]);
sort(a.begin(), a.end());
a.erase(unique(a.begin(),a.end()), a.end());
int ans = 0;
REP(i,a.size()){
if(a[i] <= m){
int t = m - a[i];
vector<int>::iterator it = upper_bound(a.begin(), a.end(), t);
it--;
ans = max(ans, a[i] + *it);
}
}
cout<<ans<<endl;
}
return 0;
} | 0 |
#include <iostream>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <bitset>
#include <utility>
#include <numeric>
#include <queue>
#include <stack>
#include <iomanip>
using ll = long long;
using namespace std;
constexpr int MOD = 1e9 + 7;
constexpr ll MOD_LL = ll(1e9 + 7);
int main(void) {
string a;
cin >> a;
int n = (int)a.size();
map<char, int> mp;
for(int i = 0; i < n; ++i) {
mp[a[i]]++;
}
ll ans = 1LL;
for(int i = 0; i < n; ++i) {
ans += n - i - 1 - (mp[a[i]] - 1);
mp[a[i]]--;
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = b - 1; i >= a; --i)
#define SORT(v) sort(v.begin(), v.end())
#define SORTR(v) sort(v.rbegin(), v.rend())
#define REV(v) reverse(v.begin(), v.end())
#define ITER(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define SZ(v) (int)v.size()
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
string A;
cin >> A;
vector<ll> num_char(26);
for (auto &c : A) {
num_char[c - 'a']++;
}
ll res = 1;
REP(i, 25) {
FOR(j, i + 1, 26) {
res += num_char[i] * num_char[j];
}
}
cout << res << endl;
return 0;
}
| 1 |
/*{{{*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PII> VPII;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(LL &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const LL &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W(x.F); putchar(' '); _W(x.S);}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
#ifdef HOME
#define DEBUG(...) {printf("# ");printf(__VA_ARGS__);puts("");}
#else
#define DEBUG(...)
#endif
int MOD = 1e9+7;
void ADD(LL& x,LL v){x=(x+v)%MOD;if(x<0)x+=MOD;}
/*}}}*/
const int SIZE = 1e6+10;
int N,X;
LL b[SIZE],l[SIZE],u[SIZE];
bool used[SIZE];
int main(){
R(N,X);
LL need=0;
priority_queue<PLL>h0,h1,h2;
REP(i,N){
R(b[i],l[i],u[i]);
need+=b[i]*l[i];
h0.push({l[i]*b[i]+u[i]*(X-b[i]),i});
}
LL an=0;
while(need>=h0.top().F){
an+=X;
used[h0.top().S]=1;
need-=h0.top().F;
h0.pop();
}
if(need<=0){
W(an);
return 0;
}
int add_id=h0.top().S;
LL add_v=l[add_id]*b[add_id]+u[add_id]*(X-b[add_id]);
LL mi=1e18;
REP(i,N){
if(used[i]){
LL need2=need-add_v+l[i]*b[i]+u[i]*(X-b[i]);
if(l[i]*b[i]>=need2){
mi=min(mi,(need2+l[i]-1)/l[i]);
}
else{
mi=min(mi,(need2-l[i]*b[i]+u[i]-1)/u[i]+b[i]);
}
}
else{
if(l[i]*b[i]+u[i]*(X-b[i])<need)continue;
if(l[i]*b[i]>=need){
mi=min(mi,(need+l[i]-1)/l[i]);
}
else{
mi=min(mi,(need-l[i]*b[i]+u[i]-1)/u[i]+b[i]);
}
}
}
an+=mi;
W(an);
return 0;
}
| #include<bits/stdc++.h>
#define ll long long
#define fornum(A,B,C) for(A=B;A<C;A++)
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>
using namespace std;
/////////////////////////////////////////////////////
#define INF 1e15
ll N, X, b[101010];
ll l[101010], u[101010];
vector<pll> mxv;
ll mk[101010];
ll i, j, k,aa,bb,dd,bs,ans;
int main(){
scanf("%lld%lld", &N,&X);
bs = 0;
fornum(i,0,N){
scanf("%lld%lld%lld", &b[i], &l[i], &u[i]);
bs += l[i] * b[i];
mxv.push_back({u[i] * (X - b[i]) + l[i] * b[i], i});
}
sort(mxv.begin(), mxv.end(), greater<pll>());
//printf("%lld\n", bs);
if(bs==0){
printf("0");
return 0;
}
while(bb<N&&aa<bs){
aa += mxv[bb].first;
ll a = mxv[bb].second;
mk[a] = 1;
dd += X;
//printf("%lld %lld\n", mxv[i].second, bs);
bb++;
}
bb--;
ans = INF;
fornum(i,0,bb){
ll cc = bs-(aa - mxv[i].first);
ll ii = mxv[i].second;
ll a = dd-X;
if(cc<=b[ii]*l[ii]){
a += (cc - 1) / l[ii] + 1;
}else{
a += (cc - b[ii] * l[ii] - 1) / u[ii] + 1 + b[ii];
}
if(a<ans){
ans = a;
}
//printf("%llda\n", ans);
}
fornum(i,bb,N){
ll cc = bs-(aa - mxv[bb].first);
ll ii = mxv[i].second;
ll a = dd - X;
if(cc<=b[ii]*l[ii]){
a += (cc - 1) / l[ii] + 1;
}else{
a += (cc - b[ii] * l[ii] - 1) / u[ii] + 1 + b[ii];
}
if(a<ans){
ans = a;
}
//printf("%lld\n", ans);
}
printf("%lld", ans);
return 0;
} | 1 |
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define li long long
#define itn int
using namespace std;
inline int nxt(){
int n;
scanf("%d", &n);
return n;
}
const int N = 111 * 111 / 3;
int dp[N][N];
int main(){
int n = nxt(), m = nxt();
vector<string> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
int posX = 0, posY = 0;
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
if (a[i][j] == 'E'){
posX = i;
posY = j;
}
}
}
vector<int> i1, i2, j1, j2;
for (int i = 0; i <= posX; i++){
for (int j = posX; j < n; j++){
i1.push_back(i);
i2.push_back(j);
}
}
for (int i = 0; i <= posY; i++){
for (int j = posY; j < m; j++){
j1.push_back(i);
j2.push_back(j);
}
}
vector<vector<int>> prefX(n, vector<int>(m + 1, 0)), prefY(n + 1, vector<int>(m, 0));
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
prefX[i][j + 1] = prefX[i][j] + (a[i][j] == 'o' ? 1 : 0);
}
}
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
prefY[i + 1][j] = prefY[i][j] + (a[i][j] == 'o' ? 1 : 0);
}
}
int q = i1.size(), w = j1.size();
vector<vector<int>> pos1(posX + 1, vector<int>(n - posX));
for (int i = 0; i < q; i++)
pos1[i1[i]][i2[i] - posX] = i;
vector<vector<int>> pos2(posY + 1, vector<int>(m - posY));
for (int i = 0; i < w; i++)
pos2[j1[i]][j2[i] - posY] = i;
dp[pos1[posX][0]][pos2[posY][0]] = 0;
for (int i = posX; i >= 0; i--){
for (int ii = 0; ii < n - posX; ii++){
for (int j = posY; j >= 0; j--){
for (int jj = 0; jj < m - posY; jj++){
int l = j, r = jj + posY, u = i, d = ii + posX;
int actl = jj, actr = m - 1 - (posY - j), actu = ii, actd = n - 1 - (posX - i);
// if (l == posY && r == posY && u == posX && d == posX)
// continue;
int& res = dp[pos1[i][ii]][pos2[j][jj]];
if (r > posY)
res = max(res, dp[pos1[i][ii]][pos2[j][jj - 1]] + (r > actr ? 0 : prefY[min(d, actd) + 1][r] - prefY[max(u, actu)][r]));
if (l < posY)
res = max(res, dp[pos1[i][ii]][pos2[j + 1][jj]] + (l < actl ? 0 : prefY[min(d, actd) + 1][l] - prefY[max(u, actu)][l]));
if (d > posX)
res = max(res, dp[pos1[i][ii - 1]][pos2[j][jj]] + (d > actd ? 0 : prefX[d][min(r, actr) + 1] - prefX[d][max(l, actl)]));
if (u < posX)
res = max(res, dp[pos1[i + 1][ii]][pos2[j][jj]] + (u < actu ? 0 : prefX[u][min(r, actr) + 1] - prefX[u][max(l, actl)]));
}
}
}
}
int ans = 0;
for (int i = posX; i >= 0; i--){
for (int ii = 0; ii < n - posX; ii++){
for (int j = posY; j >= 0; j--){
for (int jj = 0; jj < m - posY; jj++){
// cerr << i << " " << ii + posX << " " << j << " " << jj + posY << " " << dp[pos1[i][ii]][pos2[j][jj]] << "\n";
ans = max(ans, dp[pos1[i][ii]][pos2[j][jj]]);
}
}
}
}
cout << ans << "\n";
return 0;
}
| #include <algorithm>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
#define fst first
#define snd second
/* clang-format off */
template <class T, size_t D> struct _vec { using type = vector<typename _vec<T, D - 1>::type>; };
template <class T> struct _vec<T, 0> { using type = T; };
template <class T, size_t D> using vec = typename _vec<T, D>::type;
template <class T> vector<T> make_v(size_t size, const T& init) { return vector<T>(size, init); }
template <class... Ts> auto make_v(size_t size, Ts... rest) { return vector<decltype(make_v(rest...))>(size, make_v(rest...)); }
/* clang-format on */
template <class I>
class SegmentTree {
public:
using Op = function<I(const I&, const I&)>;
private:
int n;
vector<I> dat;
Op op;
I e;
public:
SegmentTree(int n_, const Op& op, const I& e) : op(op), e(e) {
n = 1;
while (n < n_) n *= 2;
dat.resize(2 * n - 1, e);
}
SegmentTree(const vector<I>& init, const Op& op, const I& e)
: SegmentTree(init.size(), op, e) {
for (int i = 0; i < init.size(); i++) {
dat[i + n - 1] = init[i];
}
for (int i = n - 2; i >= 0; i--) {
dat[i] = op(dat[i * 2 + 1], dat[i * 2 + 2]);
}
}
const I& operator[](int k) const { return dat[k + n - 1]; }
void update(int k, const I& a) {
k += n - 1;
dat[k] = a;
while (k > 0) {
k = (k - 1) / 2;
dat[k] = op(dat[k * 2 + 1], dat[k * 2 + 2]);
}
}
// [a, b)
I query(int a, int b) const {
a += n - 1;
b += n - 1;
I vl = e;
I vr = e;
while (a < b) {
if ((a & 1) == 0) vl = op(vl, dat[a]);
if ((b & 1) == 0) vr = op(dat[b - 1], vr);
a = a / 2;
b = (b - 1) / 2;
}
return op(vl, vr);
}
// query(0, i) >= x となる最小のi
int kth(const I& x) const {
int k = 0;
I v = e;
while (k < n - 1) {
I t = op(v, dat[k * 2 + 1]);
if (x <= t) {
k = k * 2 + 1;
} else {
v = t;
k = k * 2 + 2;
}
}
return k - (n - 1) + 1;
}
};
int main() {
#ifdef LOCAL
ifstream cin("in.txt");
#endif
int N, Q;
while (cin >> N >> Q) {
int M = Q + 2;
vector<ll> x(M);
for (int i = 0; i < M; i++) cin >> x[i], --x[i];
vector<ll> dsum(M);
dsum[0] = 0;
for (int i = 1; i < M; i++) {
dsum[i] = dsum[i - 1] + abs(x[i] - x[i - 1]);
}
auto op = [](ll a, ll b) { return min(a, b); };
ll inf = numeric_limits<ll>::max();
vector<ll> dp(M);
dp[M - 1] = 0;
SegmentTree<ll> addSeg(N, op, inf), subSeg(N, op, inf);
addSeg.update(x[M - 1], dp[M - 1] + dsum[M - 2] + x[M - 1]);
subSeg.update(x[M - 1], dp[M - 1] + dsum[M - 2] - x[M - 1]);
for (int i = M - 2; i >= 1; i--) {
dp[i] = min({dsum[M - 1] - dsum[i], // 先頭を最後まで持っていく
addSeg.query(x[i - 1], N) - dsum[i] - x[i - 1],
subSeg.query(0, x[i - 1] + 1) - dsum[i] + x[i - 1]});
addSeg.update(x[i], min(addSeg[x[i]], dp[i] + dsum[i - 1] + x[i]));
subSeg.update(x[i], min(subSeg[x[i]], dp[i] + dsum[i - 1] - x[i]));
}
cout << dp[1] << endl;
}
return 0;
}
| 0 |
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <math.h>
#include <climits>
#include <assert.h>
#include <cstring>
#include <set>
using namespace std;
typedef long long ll;
int arr[200001];
int main()
{
int N;
cin >> N;
int temp;
int i;
int m = 0;
ll sum = 0;
for(i = 0; i < N; i++)
{
cin >> temp;
arr[i] = temp;
m = max(m,arr[i]);
sum += m-arr[i];
}
for(i = N-1; i >= 0; i--)
{
}
cout << sum;
} | #include <iostream>
#include <algorithm>
using namespace std;
int main (int argc, char const* argv[]){
int n, max1, a[5001];
while(cin >> n && n!=0){
for(int i=0;i<n;i++) cin >> a[i];
max1 = a[0];
for (int i = 0; i < n; i += 1){
int sum = 0;
for(int j=i;j<n;j++){
sum += a[j];
max1 = max(max1,sum);
}
}
cout << max1 << endl;
}
return 0;
} | 0 |
//52行目から編集
#include <iostream>
#include <limits.h>
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <cassert>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define all(x) (x).begin(),(x).end()
#define mp make_pair
#define pb push_back
#define eb emplace_back
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vector<int> > vvi;
typedef vector<vector<ll> > vvl;
typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
template<class T> bool chmin(T& a,T b){if(a > b){a = b;return true;}else return false;}
template<class T> bool chmax(T& a,T b){if(a < b){a = b;return true;}else return false;}
///////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
/*
* 強連結成分分解
*/
struct StronglyConnectedComponents{
int V;//頂点数
vector<vector<int> > G; // グラフの隣接リスト表現
vector<vector<int> > rG; //辺の向きを逆にしたグラフ
vector<int> vs; // 帰りがけ順の並び
vector<bool> used; // すでに調べたか
vector<int> cmp; // 属する今日連結成分のトポロジカル順序
StronglyConnectedComponents(int V):V(V){
G = vector<vector<int> >(V,vector<int>(0)); rG = vector<vector<int> >(V,vector<int>(0));
vs = vector<int>(V,0); used = vector<bool>(V,0); cmp = vector<int>(V,0);
}
void add_edge(int from,int to){ G[from].push_back(to); rG[to].push_back(from); }
void dfs(int v){
used[v] = 1;
for(int i = 0;i < G[v].size();i++){ if(!used[G[v][i]])dfs(G[v][i]); }
vs.push_back(v);
}
void rdfs(int v,int k){
used[v] = 1; cmp[v] = k;
for(int i = 0;i < rG[v].size();i++){ if(!used[rG[v][i]]) rdfs(rG[v][i],k); }
}
int calc(){
for(int i = 0;i < V;i++)used[i] = 0;
vs.clear();
for(int v = 0;v < V;v++){ if(!used[v])dfs(v);}
for(int i = 0;i < V;i++)used[i] = 0;
int k = 0;
for(int i = vs.size() - 1;i >= 0;i--){ if(!used[vs[i]]) rdfs(vs[i],k++); }
return k;
}
};
////////////////////////////////////////////////////////////////////////////////////////////
int main(void){
cin.tie(0);
ios::sync_with_stdio(false);
//cout << fixed << setprecision(20);
int n,m;cin >> n >> m;
StronglyConnectedComponents scc(n);
rep(i,m){
int a,b;cin >> a >> b;
scc.add_edge(a,b);
}
int q;cin >> q;
scc.calc();
//rep(i,n)cout << i << " : " << scc.cmp[i] << '\n';
rep(i,q){
int a,b;cin >> a >> b;
cout << (scc.cmp[a] == scc.cmp[b]) << '\n';
}
return 0;
}
| #include "bits/stdc++.h"
#define REP(i,num) for(int i=0;i<(num);++i)
#define ALL(c) c.begin(),c.end()
#define LOOP(i) while(i--)
#define PRINTALL(c) for(auto& x:c){cout<<x<<' ';}cout<<endl;
#define PAIRCOMP(c,comp) [](const pair<ll,ll>& lhs,const pair<ll,ll>& rhs){return lhs.c comp rhs.c;}
using namespace std;
using ll = long long;
constexpr ll atcoder_mod = 1e9+7;
template<typename T=int>
T in(){T x; cin >> x; return (x);}
template<typename T=int,typename C=vector<T>>
C vecin(int N){C x(N);REP(i,N){x[i]=in<T>();}return move(x);}
void vout(){cout << endl;}
template<typename Head,typename... Tail>
void vout(Head&& h,Tail&&... t){cout << ' ' << h;vout(forward<Tail>(t)...);}
void out(){cout << endl;}
template<typename Head,typename... Tail>
void out(Head&& h,Tail&&... t){cout << h;vout(forward<Tail>(t)...);}
class ConnectNodeInfo{
vector<vector<pair<ll,ll>>> graph;
public:
ConnectNodeInfo(int node_num){
graph.resize(node_num);
}
void AddNonDirectionalConnection(ll u,ll v,ll w){
graph[u].emplace_back(v,w);
graph[v].emplace_back(u,w);
}
void AddDirectionalConnection(ll u,ll v,ll w){
graph[u].emplace_back(v,w);
}
vector<pair<ll,ll>>& operator[](ll index){
return graph[index];
}
size_t size(){return graph.size();}
};
class UnionFind{
vector<ll> rank;
vector<ll> diff_weight;
vector<ll> num;
public:
vector<ll> par;
UnionFind(ll N):par(N),num(N),rank(N),diff_weight(N){
for(ll i=0;i<N;i++){
par[i]=i;
num[i]=1;
rank[i]=0;
diff_weight[i]=0;
}
}
ll root(ll x){
if(par[x]==x){
return x;
}
ll r = root(par[x]);
diff_weight[x]+=diff_weight[par[x]];
return par[x]=r;
}
void unite(ll x,ll y){
ll rx = root(x);
ll ry = root(y);
if (rx == ry) return;
if(rank[rx]<rank[ry]) swap(rx,ry);
if(rank[rx]==rank[ry]) ++rank[rx];
par[ry] = rx;
num[rx] += num[ry];
}
void relate(ll x,ll y,ll w){
w+=weight(x),w-=weight(y);
ll rx = root(x);
ll ry = root(y);
if (rx == ry) return;
if(rank[rx]<rank[ry]){
swap(rx,ry);
w = -w;
}
if(rank[rx]==rank[ry]) ++rank[rx];
par[ry] = rx;
diff_weight[ry] = w;
num[rx] += num[ry];
}
bool same(ll x,ll y){
ll rx = root(x);
ll ry = root(y);
return rx == ry;
}
ll getsize(ll x){
return num[root(x)];
}
ll weight(ll x){
root(x);
return diff_weight[x];
}
ll diff(ll x,ll y){
return weight(y)-weight(x);
}
};
void FirstDFS(int child,int& post_count,vector<int>& post,ConnectNodeInfo& connect,vector<int>& memo){
memo[child] = 1;
for(auto& x:connect[child]){
if(!memo[x.first]){
FirstDFS(x.first,post_count,post,connect,memo);
}
}
post[child] = post_count++;
}
void SecondDFS(int start,int child,ConnectNodeInfo& connect,UnionFind& uf,vector<int>& memo){
uf.unite(start,child);
memo[child] = 1;
for(auto& x:connect[child]){
if(!memo[x.first]){
SecondDFS(start,x.first,connect,uf,memo);
}
}
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
cout<<fixed<<setprecision(10);
int N=in(),M=in();
ConnectNodeInfo connect(N),inverse_connect(N);
REP(i,M){
int u=in(),v=in();
connect.AddDirectionalConnection(u,v,1);
inverse_connect.AddDirectionalConnection(v,u,1);
}
int post_count=0;
vector<int> post(N,-1);
vector<int> memo(N,0);
REP(i,N){
if(!memo[i]) FirstDFS(i,post_count,post,connect,memo);
}
vector<pair<int,int>> new_order(N);
REP(i,N){
new_order[i].first=i;
new_order[i].second=post[i];
}
sort(ALL(new_order),PAIRCOMP(second,>));
UnionFind uf(N);
fill(ALL(memo),0);
REP(i,N){
if(!memo[new_order[i].first]) SecondDFS(new_order[i].first,new_order[i].first,inverse_connect,uf,memo);
}
int Q=in();
LOOP(Q){
int u=in(),v=in();
out(uf.same(u,v)?1:0);
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N, K;
cin >> N >> K;
int x = 1;
for (int i = 0;N > i;i++){
cin >> x;
if (x < K){
x *= 2;
}
else{
x += K;
}
}
cout << x << endl;
} | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define fi first
#define se second
const ll mod = 1e9+7;
const int N = 2e5+6;
int k,n;
void solve(){
cin>>n>>k;
int st = 1;
for(int i=1;i<=n;i++){
if(st*2<st+k)st*=2;
else st+=k;
}
cout<<st<<endl;
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t=1;
// cin>>t;
while(t--){
solve();
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define all(x) (x).begin(),(x).end()
using ll = long long;
string char_to_string(char val) {
return string(1, val);
}
int char_to_int(char val) {
return val - '0';
}
template<class T> inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template<class T> inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int vector_finder(std::vector<ll> vec, int number) {
auto itr = std::find(vec.begin(), vec.end(), number);
size_t index = std::distance( vec.begin(), itr );
if (index != vec.size()) { // 発見できたとき
return 1;
}
else { // 発見できなかったとき
return 0;
}
}
int main() {
string S; cin >> S;
string ans = "AC";
if(S[0] != 'A') ans = "WA";
if(S[1] != tolower(S[1])) ans = "WA";
if(S[S.size() - 1] != tolower(S[S.size() - 1])) ans = "WA";
int cnt = 0;
for(int i = 2; i < S.size() - 1; ++i) {
if(S[i] == 'C') cnt++;
if(cnt > 1) ans = "WA";
if(i == S.size() - 2 && cnt == 0) ans = "WA";
}
cout << ans << endl;
} | #include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
using namespace std;
void b_accepted(void) {
string s;
cin >> s;
bool judge_a = false, judge_b = false, judge_c = false;
int cnt_c = 0;
for (int i = 0; i < s.size(); i++) {
if (i == 0) {
if (s[i] == 'A') judge_a = true;
else break;
}
else if (s[i] == 'C'){
if (i >= 2 && i < s.size() - 1) {
cnt_c++;
}
}
else {
if (s[i] < 'a') {
judge_c = false;
break;
}
else judge_c = true;
}
}
if (cnt_c == 1) judge_b = true;
if (judge_a & judge_b & judge_c) {
cout << "AC" << endl;
}
else {
cout << "WA" << endl;
}
}
int main()
{
b_accepted();
return 0;
} | 1 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//cout << fixed << setprecision(15);
vector<int> forintinput(int n) {
vector<int> x;
for(int i = 0;i < n;i++){
int ia;
cin >> ia;
x.push_back(ia);
}
return x;
}
int main(){
int a,b;
cin >> a >> b;
cout << max(a+b,max(a*2-1,b*2-1)) << endl;
} | #include <iostream>
#include <iomanip>
//#include <string>
#include <vector>
#include <algorithm>
//#include <utility>
//#include <set>
//#include <map>
//#include <queue>
//#include <deque>
//#include <bitset>
//#include <math.h>
using namespace std ;
using ll = long long ;
//using ld = long double ;
using vll = vector<ll> ;
//using vvll = vector<vll> ;
//using vc = vector<char> ;
//using vvc = vector<vc> ;
//using vb = vector<bool> ;
//using vvb = vector<vb> ;
//using pll = pair<ll,ll> ;
//#define all(v) v.begin(),v.end()
//ll mod = 1e9+7 ;
//long double pie = acos(-1) ;
//ll INF = 1000000000000 ;
//void yorn(bool a){if(a) cout << "Yes" << endl ; else cout << "No" << endl ;}
//void YorN(bool a){if(a) cout << "YES" << endl ; else cout << "NO" << endl ;}
//ll gcd(long long a,long long b){if(b==0) return a ; return gcd(b,a%b) ;}
//ll lcm(long long a,long long b){return a/gcd(a,b)*b ;}
//ll sa(long long a,long long b){if(a>b) return a-b ; return b-a ;}
void fix_cout(){cout << fixed << setprecision(20) ;}
int main(){
ll n,k ;
cin >> n >> k ;
vll a(n) ;
for(auto &i:a) cin >> i ;
ll sum = 0 ;
ll now = 0 ;
for(int i=0;i<k;i++) now += a.at(i) ;
sum = now ;
for(int i=k;i<n;i++){
now = now-a.at(i-k)+a.at(i) ;
sum = max(sum,now) ;
}
fix_cout() ;
cout << (sum+k)/2.0 << endl ;
}
| 0 |
#include <iostream>
using namespace std;
int main() {
long long n,i,mod=1000000007LL;
cin >> n;
long long a[n+1],p[n+1],ans=0LL;
p[0]=0;
for(i=1;i<=n;i++)
{
cin >> a[i];
p[i]=a[i]+p[i-1];
p[i]%=mod;
}
for(i=1;i<=n;i++)
{
ans+=((p[n]-p[i])*a[i]);
ans+=mod;
ans%=mod;
}
cout << ans%mod;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
const int N=55;
void maxi(int &a, int b) {
a = max(a,b);
}
using ll = long long;
ll MOD = 1e9 +7;
ll add(ll x, ll y) {
return ((x + y) % MOD);
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
vector<ll> X;
vector<ll> Y;
int n, m; cin >> n >> m;
for (int i=0; i < n; i++) {
ll x; cin >> x;
X.push_back(x);
}
for (int i=0; i < m; i++) {
ll y; cin >> y;
Y.push_back(y);
}
ll x_total = 0;
for (int k=0; k <n; k++) {
x_total += add(X[k]*k, -(n-k-1)*X[k]);
x_total %= MOD;
}
//ll x_check =0;
//for (int i=0; i < n; i++) {
// for (int j=i+1; j < n; j++) {
// x_check += (X[j] - X[i]);
// }
//}
//assert(x_check == x_total);
ll y_total = 0;
for (int k=0; k < m; k++) {
y_total += add(Y[k]*k, -(m-k-1)*Y[k]);
y_total %= MOD;
}
//ll y_check = 0;
//for (int i=0; i < m; i++) {
// for (int j=i+1; j < m; j++) {
// y_check += (Y[j] - Y[i]);
// }
//}
//assert(y_check == y_total);
ll total = (x_total*y_total) % MOD;
if (total < 0) {
total += MOD;
}
cout << total << endl;
}
| 0 |
/*
ID: anonymo14
TASK: wormhole
LANG: C++
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<pair<int,int>> vpii;
#define F first
#define S second
#define PU push
#define PUF push_front
#define PUB push_back
#define PO pop
#define POF pop_front
#define POB pop_back
#define REP(i,a,b) for(int i=a; i<=b; i++)
#define MOD 1000000007
void solve(int test_case) {//227
int i;
cin>>i;
for(;i<=999;i++) {
if(i%10==(i%100)/10&&i%10==i/100){
cout<<i;
return;
}
}
}
int main() {
////// FILE BASED IO////
//freopen("wormhole.in", "r", stdin);
//freopen("wormhole.out", "w", stdout);
///////////////
ios::sync_with_stdio(0);
cin.tie(0);
int t=1;
//cin>>t;
REP(i,1,t) {
solve(i);
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i,s,n) for (int i = (int)s; i < (int)n; i++)
#define ll long long
#define ld long double
#define pb push_back
#define eb emplace_back
#define All(x) x.begin(), x.end()
#define Range(x, i, j) x.begin() + i, x.begin() + j
#define lbidx(x, y) lower_bound(x.begin(), x.end(), y) - x.begin()
#define ubidx(x, y) upper_bound(x.begin(), x.end(), y) - x.begin()
#define llbidx(x, y, z) lower_bound(x.begin(), x.end(), z) - lower_bound(x.begin(), x.end(), y) // 二要素間の距離
#define deg2rad(deg) ((((double)deg)/((double)360)*2*M_PI))
#define rad2deg(rad) ((((double)rad)/(double)2/M_PI)*(double)360)
#define Find(set, element) set.find(element) != set.end()
#define Decimal(x) printf("%.10f\n", x) // 小数点を10桁まで表示
// debug用
#define PrintVec(x) for (auto elementPrintVec: x) { cout << elementPrintVec << " "; } cout << "\n";
#define debug(x) cerr << #x << ": " << (x) << "\n";
#define endl "\n"
// gcj print用
#define Case(x) printf("Case #%d: ", x);
typedef pair<int, int> PI;
typedef pair<ll, ll> PLL;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<vector<vector<int>>> vvvl;
typedef vector<PI> vpi;
typedef vector<vector<PI>> vvpi;
typedef vector<vector<vector<PI>>> vvvpi;
typedef vector<PLL> vpl;
typedef vector<vector<PLL>> vvpl;
typedef vector<vector<vector<PLL>>> vvvpl;
int POWINT(int x, int n) {
int ret = 1;
while (n > 0) {
if (n & 1) ret *= x;
x *= x;
n >>= 1;
}
return ret;
};
ll POWLL(ll x, int n) {
ll ret = 1;
while (n > 0) {
if (n & 1) ret *= x;
x *= x;
n >>= 1;
}
return ret;
};
template<class T>
inline bool chmax(T &a, T b) {
if(a < b) {
a = b;
return true;
}
return false;
};
template<class T>
inline bool chmin(T &a, T b) {
if(a > b) {
a = b;
return true;
}
return false;
};
void print() {
cout << endl;
}
template <class T>
void print(vector<T> &vec) {
for (auto& a : vec) {
cout << a;
if (&a != &vec.back()) cout << " ";
}
cout << endl;
}
template <class T>
void print(vector<T> &vec, ll k){
ll n = vec.size();
k = min(k, n);
rep(i, 0, k-1) cout << vec[i] << " ";
cout << vec[k-1] << endl;
}
template <class T>
void print(vector<vector<T>> &df) {
for (auto& vec : df) {
print(vec);
}
}
template<class T, class U>
void print(pair<T,U> &p){
cout << p.first << " " << p.second << "\n";
}
const ll MAX = 1e6+5;
// エラトステネスの篩
struct Sieve {
ll n;
vl f, primes;
Sieve(ll n = 1): n(n), f(n+1) {
f[0] = f[1] = -1ll;
for (ll i = 2; i <= n; i++) {
if (f[i]) continue;
primes.pb(i);
f[i] = i;
for (ll j = i*i; j <= n; j += i) {
if (!f[j]) f[j] = i; // jの最小の素因数
}
}
}
bool isPrime(int x) { return f[x] == x; }
// 素因数分解
vl factorList(ll x) {
vl res;
while (x != 1ll) {
res.pb(f[x]);
x /= f[x];
}
return res;
}
vpl factor(ll x) {
vl fl = factorList(x);
if (fl.size() == 0) return {};
vpl res(1, PLL(fl[0], 0));
for (int p: fl) {
if (res.back().first == p) {
res.back().second++;
} else {
res.emplace_back(p, 1);
}
}
return res;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
Sieve sieve(MAX);
vector<int> primes;
rep(i, 2, 55556) {
if (sieve.isPrime(i)) {
primes.pb(i);
}
}
int N; cin >> N;
vi ans;
int cnt = 0;
for (int p: primes) {
if (p % 10 == 3) {
ans.pb(p);
cnt++;
}
if (cnt >= N) {
break;
}
}
print(ans);
return 0;
};
| 0 |
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main(){
ll n;
cin >> n;
ll ans = 1;
while(pow(ans, 2) <= n){
ans++;
}
cout << (ll)pow(ans-1, 2) << endl;
} | #include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
int N; cin >> N;
int f = 1.0;
while(true) {
double M = sqrt(N), x = 0.0, y = 0.0;
x = modf(M, &y);
if (x == 0) {
printf("%d\n", N);
return 0;
}
else N--;
}
}
| 1 |
#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<time.h>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-10;
const int inf=1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
vector<vector<char> >in(14,vector<char>(14));
int x[4]={0,0,1,-1};
int y[4]={1,-1,0,0};
void func(int a,int b){
in[a][b]=0;
rep(k,4)if(in[a+x[k]][b+y[k]]!=0)func(a+x[k],b+y[k]);
}
int main(){
char d;
while(cin>>d){
rep(i,12)rep(j,12){
if(!i&&!j)in[i+1][j+1]=d;
else cin>>in[i+1][j+1];
if(in[i+1][j+1]=='0')in[i+1][j+1]=0;else in[i+1][j+1]=1;
}
int out=0;
rep(i,12)rep(j,12)if(in[i+1][j+1]!=0){
out++;
func(i+1,j+1);
}
cout<<out<<endl;
}
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
int N;
cin >> N;
string S;
cin >> S;
string T = S.substr(0, N);
string U = S.substr(N, N);
reverse(U.begin(), U.end());
//cout << T << endl << U << endl;
unordered_map<string, ll> um1(0), um2(0);
for (int i = 0; i < (1 << N); i++) {
string a="", b="";
for (int j = 0; j < N; j++) {
if (((i >> j) & 1) == 0) a += T[j];
else b += T[j];
}
um1[a + "_" + b]++;
}
ll ans = 0;
for (int i = 0; i < (1 << N); i++) {
string a="", b="";
for (int j = 0; j < N; j++) {
if (((i >> j) & 1) == 0) a += U[j];
else b += U[j];
}
ans += um1[a + "_" + b];
}
cout << ans << endl;
return 0;
} | 0 |
#include "bits/stdc++.h"
using ll = long long;
using namespace std;
// #include "debug.h"
#define debug(...) 42
const int mod = 998244353;
void solve() {
int n;
cin >> n;
vector <int> d(n), cnt(n);
for(int i = 0; i < n; i++) {
cin >> d[i];
cnt[d[i]]++;
}
bool ok = true;
ok &= d[0] == 0;
ok &= cnt[0] == 1;
if(!ok) {
cout << 0 << endl;
return;
}
int m = *max_element(d.begin(), d.end());
ll ans = 1;
for(int i = 1; i <= m; i++) {
if(cnt[i] == 0) {
cout << 0 << endl;
return;
}
for(int j = 0; j < cnt[i]; j++) {
ans = (ans * 1LL * cnt[i - 1]) % mod;
}
}
cout << ans << endl;
}
signed main() {
cin.tie(0);
cin.sync_with_stdio(0);
int testcase = 1;
// cin >> testcase;
for(int i = 0; i < testcase; i++) {
solve();
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 998244353;
ll Ep(int x, int y){
if(y == 1) return x;
ll r = Ep(x,y/2);
if(y%2 == 1) return r *r%MOD *x%MOD;
else return r *r%MOD;
}
int main(){
int n, x;
cin >> n;
cin >> x;
if(x != 0){
cout << 0 << endl;
return 0;
}
map<int, int> mp;
for(int i=1; i<n; ++i){
cin >> x;
if(x == 0){
cout << 0 << endl;
return 0;
}
++mp[x];
}
ll ans = 1;
for(auto p: mp){
if(p.first == 1) continue;
ans *= Ep(mp[p.first-1], p.second);
ans %= MOD;
}
cout << ans << endl;
} | 1 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ll N;
cin >> N;
if (N == 0) {
cout << 0 << endl;
return 0;
}
vector<int> ans(0);
while (N != 0) {
int tmp = N % 2;
if (tmp < 0) tmp += 2;
N = (N-tmp) / (-2);
ans.push_back(tmp);
}
for (int i = ans.size()-1; i >= 0; i--) cout << ans[i];
cout << endl;
}
| #include <bits/stdc++.h>
using namespace std;
long long mypow(long long x, long long y)
{
long long result = 1;
while (y != 0) {
if (y%2 != 0) {
result *= x;
y -= 1;
}
x *= x;
y /= 2;
}
return result;
}
int main(void)
{
long long n;
cin >> n;
if (n == 0) {
cout << 0 << endl;
return 0;
}
long long base = 0;
for (int i = 0; i <= 31; i++) {
if (i % 2 == 1)
base -= mypow(2, i);
}
long long m = n-base;
string s;
for (int i = 0; i <= 31; i++) {
if (m % mypow(2, i+1) != 0) {
if (i % 2 == 0) {
s.push_back('1');
} else {
s.push_back('0');
}
m -= mypow(2, i);
} else {
if (i % 2 == 0)
s.push_back('0');
else
s.push_back('1');
}
}
reverse(s.begin(), s.end());
int i;
for (i = 0; i < s.size(); i++) {
if (s[i] == '1')
break;
}
cout << s.substr(i) << endl;
return 0;
} | 1 |
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
using namespace std;
#define NIL -1
#define N 500001
//struct Node{
// int val;
// Node *p, *l, *r;
//};
struct Node {
int val;
Node *p, *l, *r;
Node(int v) :val(v), p(NULL), l(NULL), r(NULL){}
Node() :val(0), p(NULL), l(NULL), r(NULL){}
};
Node T[N];
int D[N], H[N];
Node *root;
void insert(int k){
Node *y = NULL;
Node *x = root;
Node *z = new Node(k);
while (x != NULL && x->val != k){
y = x;
if (k > x->val) x = x->r;
else x = x->l;
}
z->p = y;
if (y == NULL) root = z;
else{
if (k < y->val) y->l = z;
else y->r = z;
}
}
Node *find(int val){
Node *p = root;
while (p != NULL && p->val != val) {
if (val < p->val) p = p->l;
else p = p->r;
}
return p;
}
void ino(Node *p){
if (p == NULL) return;
ino(p->l);
printf(" %d", p->val);
ino(p->r);
}
void preo(Node *p){
if (p == NULL) return;
printf(" %d", p->val);
preo(p->l);
preo(p->r);
}
Node *mi(Node *z){
while (z->l != NULL) z = z->l;
return z;
}
Node *suc(Node *z){
if (z->r != NULL) return mi(z->r);
Node *y = z->p;
while (y != NULL && z == y->r) {
z = y;
y = y->p;
}
return y;
}
void del(Node *z){
Node *y = NULL;
Node *x = NULL;
if (NULL == z->l || NULL == z->r) y = z;
else y = suc(z);
if (NULL != y->l) x = y->l;
else x = y->r;
if (NULL != x) x->p = y->p;
if (NULL == y->p) root = x;
else {
if (y == y->p->l) y->p->l = x;
else y->p->r = x;
}
if (y != z) z->val = y->val;
delete y;
}
int main(){
int n; cin >> n;
int val;
string com;
for (int i = 0; i < n; ++i) {
cin >> com;
if (com[0] == 'f'){
cin >> val;
Node *p = find(val);
cout << ((p == NULL) ? "no" : "yes") << endl;
}
else if (com[0] == 'i'){
cin >> val;
insert(val);
}
else if (com[0] == 'p'){
ino(root); cout << "\n";
preo(root); cout << "\n";
}
else if (com[0] == 'd'){
cin >> val;
del(find(val));
}
}
return 0;
}
//yes
//yes
//yes
//no
//no
//no
//yes
//yes
//1 2 3 7 8 22
//8 2 1 3 7 22
//1 2 8 22
//8 2 1 22 | #include <bits/stdc++.h>
using namespace std;
int main(){
int m;
cin >> m;
vector<int> nodes;
vector<pair<int, int>> child;
int n = 0;
for (int i = 0; i < m; i++){
string q;
cin >> q;
if (q == "insert"){
int k;
cin >> k;
if (n != 0){
int v = 0;
while (1){
if (k < nodes[v]){
if (child[v].first != -1){
v = child[v].first;
} else {
child[v].first = nodes.size();
break;
}
} else {
if (child[v].second != -1){
v = child[v].second;
} else {
child[v].second = nodes.size();
break;
}
}
}
}
nodes.push_back(k);
child.push_back(make_pair(-1, -1));
n++;
}
if (q == "find"){
int k;
cin >> k;
if (n == 0){
cout << "no" << endl;
} else {
int v = 0;
while (1){
if (k < nodes[v]){
if (child[v].first == -1){
cout << "no" << endl;
break;
} else {
v = child[v].first;
}
} else if (k > nodes[v]){
if (child[v].second == -1){
cout << "no" << endl;
break;
} else {
v = child[v].second;
}
} else {
cout << "yes" << endl;
break;
}
}
}
}
if (q == "delete"){
int k;
cin >> k;
int v = 0;
int u;
while (1){
if (k < nodes[v]){
u = v;
v = child[v].first;
} else if (k > nodes[v]){
u = v;
v = child[v].second;
} else {
break;
}
}
if (child[v].first != -1 && child[v].second != -1){
u = v;
int w = child[v].second;
while (child[w].first != -1 || child[w].second != -1){
u = w;
if (child[w].first != -1){
w = child[w].first;
} else {
w = child[w].second;
}
}
swap(nodes[v], nodes[w]);
v = w;
}
if (child[v].first != -1 && child[v].second == -1){
int w = child[v].first;
nodes[v] = nodes[w];
child[v].first = child[w].first;
child[v].second = child[w].second;
} else if (child[v].second != -1 && child[v].first == -1){
int w = child[v].second;
nodes[v] = nodes[w];
child[v].first = child[w].first;
child[v].second = child[w].second;
} else {
if (child[u].first == v){
child[u].first = -1;
} else {
child[u].second = -1;
}
}
n--;
}
if (q == "print"){
//Inorder
if (n != 0){
stack<int> S;
vector<int> t(nodes.size(), 0);
S.push(0);
while (!S.empty()){
int v = S.top();
S.pop();
if (t[v] == 1){
cout << ' ' << nodes[v];
}
if (t[v] == 0){
S.push(v);
if (child[v].second != -1){
S.push(child[v].second);
}
S.push(v);
if (child[v].first != -1){
S.push(child[v].first);
}
}
t[v]++;
}
}
cout << endl;
//Preorder
if (n != 0){
stack<int> S;
vector<int> t(nodes.size(), 0);
S.push(0);
while (!S.empty()){
int v = S.top();
S.pop();
if (t[v] == 0){
cout << ' ' << nodes[v];
}
if (t[v] == 0){
S.push(v);
if (child[v].second != -1){
S.push(child[v].second);
}
S.push(v);
if (child[v].first != -1){
S.push(child[v].first);
}
}
t[v]++;
}
}
cout << endl;
}
}
}
| 1 |
#include <cstdio>
typedef long long LL;
const int MX = 10005, MP = 2005;
const int MN = 505;
bool ip[MX];
int p[MP], pc;
inline void Sieve(int N) {
for (int i = 2; i <= N; ++i) {
if (!ip[i]) p[++pc] = i;
for (int j = 1, k; j <= pc; ++j) {
if ((k = p[j] * i) > N) break;
ip[k] = 1;
if (i % p[j] == 0) break;
}
}
}
int N, A[MN], B[MN];
int main() {
Sieve(10000);
scanf("%d", &N);
for (int i = 1; i <= N; ++i) A[i] = p[i & 1 ? i / 2 + 1 : N + N - i / 2 + 1];
for (int i = 1; i <= N; ++i) B[i] = p[(i & 1 ? N - i / 2 : N + i / 2) + (N & 1)];
A[0] = A[N + 1] = B[0] = B[N + 1] = 1;
for (int i = 1; i <= N; ++i, puts(""))
for (int j = 1; j <= N; ++j)
if ((i + j) % 2) printf("%lld ", (LL)A[(i + j) / 2] * A[(i + j) / 2 + 1] * B[(N + i - j + (N & 1)) / 2] * B[(N + i - j + (N & 1)) / 2 + 1] + 1);
else printf("%d ", A[(i + j) / 2] * B[(N + i - j + (N & 1)) / 2]);
return 0;
}
// Luogu : PinkRabbit | #include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<ctime>
#include<map>
#include<bitset>
#include<set>
#include<assert.h>
#include<chrono>
#include<random>
#define LL long long
#define mp(x,y) make_pair(x,y)
#define pll pair<long long,long long>
#define pii pair<int,int>
#define SZ(x) ((int)x.size())
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
inline LL read()
{
LL f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int stack[20];
template<typename T>inline void write(T x)
{
if(x<0){putchar('-');x=-x;}
if(!x){putchar('0');return;}
int top=0;
while(x)stack[++top]=x%10,x/=10;
while(top)putchar(stack[top--]+'0');
}
template<typename T>inline void pr1(T x){write(x);putchar(' ');}
template<typename T>inline void pr2(T x){write(x);putchar('\n');}
template<typename T>inline void chkmin(T &x,T y){x=x<y?x:y;}
template<typename T>inline void chkmax(T &x,T y){x=x>y?x:y;}
const int MAXN=10005;
const int MAXM=505;
int pr[MAXN],is[MAXN],plen;
void getpr()
{
for(int i=2;i<MAXN;i++)
{
if(!is[i])pr[++plen]=i;
for(int j=1;j<=plen&&i*pr[j]<MAXN;j++)
{
is[i*pr[j]]=1;
if(!(i%pr[j]))break;
}
}
}
int n,vis[MAXM][MAXM],now;LL a[MAXM][MAXM],b[MAXM][MAXM];
LL gcd(LL a,LL b){return a==0?b:gcd(b%a,a);}
void get1(int x,int y)
{
if(x>n||y>n)return ;
vis[x][y]=1;a[x][y]*=pr[now];
get1(x+1,y+1);
}
void get2(int x,int y)
{
if(x<1||x>n||y<1||y>n)return ;
vis[x][y]=1;a[x][y]*=pr[now];
get2(x+1,y-1);
}
bool in(int x,int y){return x>=1&&x<=n&&y>=1&&y<=n;}
LL lcm(LL a,LL b){return a/gcd(a,b)*b;}
int main()
{
getpr();
n=read();now=0;
if(n==2)
{
printf("4 7\n23 10\n");return 0;
}
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)a[i][j]=1;
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)if(((i+j-1)&1)&&!vis[i][j])
++now,get1(i,j);
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)if(((i+j-1)&1)&&!vis[i][j])
++now,get2(i,j);
for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)if(!((i+j-1)&1))
{
if(in(i-1,j))a[i][j]=lcm(a[i][j],a[i-1][j]);
if(in(i+1,j))a[i][j]=lcm(a[i][j],a[i+1][j]);
if(in(i,j-1))a[i][j]=lcm(a[i][j],a[i][j-1]);
if(in(i,j+1))a[i][j]=lcm(a[i][j],a[i][j+1]);
++a[i][j];
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)pr1(a[i][j]);
puts("");
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
int N, M, Q;
scanf("%d %d %d\n", &N, &M, &Q);
vector<string> grid(N+1, "0");
grid[0].resize(M);
for (int i = 1; i <= N; i++) {
char s[M];
scanf("%s\n", s);
grid[i] += s;
}
vector<vector<int>> psum(N+1, vector<int>(M+1, 0));
vector<vector<int>> horsum(N+1, vector<int>(M+1, 0));
vector<vector<int>> versum(N+1, vector<int>(M+1, 0));
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
if (grid[i][j] == '1') {
psum[i][j]++;
if (grid[i][j-1] == '1') horsum[i][j]++;
if (grid[i-1][j] == '1') versum[i][j]++;
}
psum[i][j] += psum[i][j-1] + psum[i-1][j] - psum[i-1][j-1];
horsum[i][j] += horsum[i][j-1] + horsum[i-1][j] - horsum[i-1][j-1];
versum[i][j] += versum[i][j-1] + versum[i-1][j] - versum[i-1][j-1];
}
}
for (int z = 0; z < Q; z++) {
int x1, x2, y1, y2;
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
int ps = psum[x2][y2] - psum[x1-1][y2] - psum[x2][y1-1] + psum[x1-1][y1-1];
int hors = horsum[x2][y2] - horsum[x1-1][y2] - horsum[x2][y1] + horsum[x1-1][y1];
int vers = versum[x2][y2] - versum[x1][y2] - versum[x2][y1-1] + versum[x1][y1-1];
printf("%d\n", ps - hors - vers);
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
inline int read(){
int s=0,w=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
int n,m,q,a[2010][2010];
int po[2010][2010],ba[2010][2010];
int ob[2010][2010],zb[2010][2010];
int main(){
// freopen("duty.in","r",stdin);
// freopen("duty.out","w",stdout);
n=read(); m=read(); q=read();
for(int i=0;++i<=n;){
char ch;
for(int j=0;++j<=m;)
ch=getchar(),a[i][j]=(ch=='1');
ch=getchar();
}
for(int i=0;++i<=n;)
for(int j=0;++j<=m;){
po[i][j]=po[i-1][j]+po[i][j-1]-po[i-1][j-1]+a[i][j];
ba[i][j]=ba[i-1][j]+ba[i][j-1]-ba[i-1][j-1]+(a[i][j]&a[i-1][j])+(a[i][j]&a[i][j-1]);
ob[i][j]=ob[i][j-1]+(a[i][j]&a[i-1][j]);
}
for(int j=0;++j<=m;)
for(int i=0;++i<=n;)
zb[i][j]=zb[i-1][j]+(a[i][j]&a[i][j-1]);
for(int i=0;++i<=q;){
int sx=read(),sy=read(),ex=read(),ey=read();
int Sp=po[ex][ey]-po[sx-1][ey]-po[ex][sy-1]+po[sx-1][sy-1];
int Sb=ba[ex][ey]-ba[sx-1][ey]-ba[ex][sy-1]+ba[sx-1][sy-1];
printf("%d\n",Sp-Sb+ob[sx][ey]-ob[sx][sy-1]+zb[ex][sy]-zb[sx-1][sy]);
}
return 0;
} | 1 |
#include<bits/stdc++.h>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
ll M,N,P[1001];
int main(){
while(scanf("%lld%lld",&N,&M),N){
for(int i=0;i<N;i++) scanf("%lld",&P[i]);
vector<ll> d;
for(int i=0;i<=N;i++) for(int j=0;j<=N;j++)
d.push_back(P[i]+P[j]);
sort(d.begin(),d.end());
d.erase(unique(d.begin(),d.end()),d.end());
vector<ll>::iterator it;
ll res=0;
for(int i=0;i<d.size();i++){
it=upper_bound(d.begin(),d.end(),M-d[i]);
if(it!=d.begin()){
it--;
res=max(res,*it+d[i]);
}
}
printf("%lld\n",res);
}
} | #include <iostream>
#include <string.h>
using namespace std;
const int dx[] = {1,0,-1,0};
const int dy[] = {0,1,0,-1};
int field[101][101];
int xs,ys,xg,yg;
void SetBlock(int c,int d,int x,int y)
{
if(d==0){//横なが
for(int ay=0;ay<2;++ay){
for(int ax=0;ax<4;++ax){
field[y+ay][x+ax] = c;
}
}
}
if(d==1){//縦なが
for(int ay=0;ay<4;++ay){
for(int ax=0;ax<2;++ax){
field[y+ay][x+ax] = c;
}
}
}
}
bool dfs(int x,int y)
{
int c = field[y][x];
field[y][x] = 0;
if(x == xg && y == yg) return true;
for(int i=0;i<4;++i){
if(c==field[y+dy[i]][x+dx[i]])if(dfs(x+dx[i],y+dy[i]))return true;
}
return false;
}
int main(void)
{
while(1){
int w,h;
int n;
memset(field,0,sizeof(field));
cin >> w >> h;
if(w==0&&h==0)
return 0;
cin >> xs >> ys;
cin >> xg >> yg;
cin >> n;
for(int i=0;i<n;++i){
int c,d,x,y;
cin >> c >> d >> x >> y;
SetBlock(c,d,x,y);
}
if(field[ys][xs] == 0)
cout << "NG" << endl;
else
{
if(dfs(xs,ys))
cout << "OK" << endl;
else
cout << "NG" << endl;
}
}
return 0;
} | 0 |
#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define f first
#define s second
using namespace std;
int n, a[100005];
vector<pair<int,int>> vec, vec2;
unordered_map<int,int> mp, mp2;
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifdef debug
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
cin >> n;
for(int i = 1; i <= n; i++)cin >> a[i];
for(int i = 1; i <= n; i+= 2)mp[a[i]]++;
for(int i = 2; i <= n; i += 2)mp2[a[i]]++;
for(auto i : mp)vec.pb({i.s, i.f});
for(auto i : mp2)vec2.pb({i.s, i.f});
sort(vec.begin(), vec.end()); sort(vec2.begin(), vec2.end());
int cnt = 1e9;
int delta = n/2;
if(vec.size() == 1 && vec2.size() == 1){
if(vec[0].s == vec2[0].s){
cnt = delta;
}
else{
cnt = 0;
}
}
else{
if(vec[vec.size()-1].s != vec2[vec2.size()-1].s){
cnt = delta - vec[vec.size()-1].f + delta - vec2[vec2.size()-1].f;
}
else{
if(vec.size() > 1)cnt = min(cnt, delta - vec2[vec2.size()-1].f + delta - vec[vec.size()-2].f);
else cnt = min(cnt, delta + delta - vec2[vec2.size()-1].f);
if(vec2.size() > 1)cnt = min(cnt, delta - vec2[vec2.size()-2].f + delta - vec[vec.size()-1].f);
else cnt = min(cnt, delta + delta - vec[vec.size()-1].f);
}
}
cout << cnt << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int,int>;
#define INF 1000000001
int main() {
int n;cin >> n;
n /= 2;
vector<int> e(100001),o(100001);
for(int i = 0; i < n*2; i++) {
int v;cin >> v;
if(i%2==0){
o[v]++;
}
else e[v]++;
}
int a=0,b=0,c=0,d=0;
for(int i = 0; i < 100001; i++) {
if(e[i]>a){
c=i;
a=e[i];
}
}
for(int i = 0; i < 100001; i++) {
if(o[i]>b){
d=i;
b=o[i];
}
}
int x=a+b;
if(c==d){
sort(e.begin(),e.end());
sort(o.begin(),o.end());
x=max(a+o[99999],b+e[99999]);
}
cout << n*2-x << endl;
return 0;
} | 1 |
#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <cstdlib>
#include <list>
#include <numeric>
#include <map>
#include <cmath>
#include <string>
#define rep(i,n) for(int i=0;i<n;i++)
#define all(x) (x).begin(),(x).end()
using namespace std;
//================================================
int a = 0, b = 0, c, d, n, k = 0;
string s, t;
int main()
{
int n, h, w;
cin >> n >> h >> w;
cout << (n - h + 1) * (n - w + 1) << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std ;
int main(){
int a , b , c , d ;
cin >> a >> b >> c >> d ;
int M = 0 ;
if(a > b ){
M += b ;
}
else{
M =+ a ;
}
if(c>d) M += d ;
else M += c ;
cout << M << endl ;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1001001001;
const ll mod = 1000000007;
int main(){
int n, u, v; cin >> n >> u >> v;
--u; --v;
if(u == v){
cout << 0 << endl;
return 0;
}
vector<int> dist_u(n, -1), dist_v(n, -1);
vector<vector<int>> G(n);
for(int i = 0; i < n - 1; i++){
int a, b; cin >> a >> b;
a--; b--;
G[a].push_back(b);
G[b].push_back(a);
}
queue<int> que;
que.push(u);
dist_u[u] = 0;
dist_v[v] = 0;
while(!que.empty()){
int v = que.front(); que.pop();
for(int nv : G[v]){
if(dist_u[nv] != -1) continue;
dist_u[nv] = dist_u[v] + 1;
que.push(nv);
}
}
que.push(v);
while(!que.empty()){
int v = que.front(); que.pop();
for(int nv : G[v]){
if(dist_v[nv] != -1) continue;
dist_v[nv] = dist_v[v] + 1;
que.push(nv);
}
}
set<int> st;
for(int i = 0; i < n; i++){
if(dist_u[i] < dist_v[i]) st.insert(dist_v[i]);
}
sort(dist_v.begin(), dist_v.end(), greater<int>());
for(int i = 0; i < n; i++){
if(st.count(dist_v[i])){
cout << dist_v[i] - 1 << endl;
return 0;
}
}
} | #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define vtpl(x,y,z) vector<tuple<x,y,z>>
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[9]={1,0,-1,0,1,1,-1,-1,0};
const ll dx[9]={0,-1,0,1,1,-1,1,-1,0};
template<class T> inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template<class T> inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
void dfs(ll v,ll par,set<ll> &st,vl &ans, vvl &g){
for(auto nv:g[v]){
if(par==nv)continue;
if(ans[nv]==-1){
ans[nv]=v;
st.ins(v);dfs(nv,v,st,ans,g);st.erase(v);
}
else{
if(st.count(ans[nv]))ans[nv]=v;
}
}
}
int main(){
ll n,m;cin >>n >>m;
vvl g(n);
vl ans(n,-1);
set<ll> st;rep(i,n)st.ins(i);
rep(i,n-1+m){
ll a,b;cin >> a >> b;a--;b--;
g[a].pb(b);
st.erase(b);
}
ll s=*st.begin();st.clear();
dfs(s,-1,st,ans,g);
for(auto p:ans)cout << p+1 <<endl;
} | 0 |
#include <iostream>
#include <math.h>
using namespace std;
int main() {
long N;
cin >> N;
// N = A * B
// B = N / A
// A , B 桁数大きい方の、最小値
long ans = 100000000;
double d = sqrt(N) + 1;
//cout << "d: " << d << endl;
for (long i = 1; i < d ; i++) {
long a = i;
//整数チェック
if (N % a != 0){
continue;
}
long b = N / a;
//cout << "a: " << a << " b: " << b << endl;
string s_a = to_string(a);
int k_a = s_a.size();
string s_b = to_string(b);
int k_b = s_b.size();
int m = max(k_a, k_b);
if (ans > m){
ans = m;
}
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
void chmax(int& a, int b) {
if (a < b) a = b;
}
void chmin(int& a, int b) {
if (a > b) a = b;
}
int main () {
int N, M, X, Y;
cin >> N >> M >> X >> Y;
for (int i = 0; i < N; i++) {
int x;
cin >> x;
chmax(X, x);
}
for (int i = 0; i < M; i++) {
int y;
cin >> y;
chmin(Y, y);
}
if (X < Y) {
cout << "No War" << endl;
} else {
cout << "War" << endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
long long MOD = 1000000007;
long long modpow(long long a, long long b){
long long ans = 1;
while (b > 0){
if (b % 2 == 1){
ans *= a;
ans %= MOD;
}
a *= a;
a %= MOD;
b /= 2;
}
return ans;
}
long long modinv(long long a){
return modpow(a, MOD - 2);
}
vector<long long> mf = {1};
long long modfact(int n){
if (mf.size() > n){
return mf[n];
} else {
for (int i = mf.size(); i <= n; i++){
long long next = mf.back() * i % MOD;
mf.push_back(next);
}
return mf[n];
}
}
long long modbinom(int n, int k){
return modfact(n) * modinv(modfact(k)) % MOD * modinv(modfact(n - k)) % MOD;
}
int main(){
long long N, M, K;
cin >> N >> M >> K;
long long ans1 = 0;
for (int i = 1; i <= N; i++){
ans1 += (N - i) * i;
}
ans1 %= MOD;
ans1 *= M;
ans1 %= MOD;
ans1 *= M;
ans1 %= MOD;
long long ans2 = 0;
for (int i = 1; i <= M; i++){
ans2 += (M - i) * i;
}
ans2 %= MOD;
ans2 *= N;
ans2 %= MOD;
ans2 *= N;
ans2 %= MOD;
long long ans = ans1 + ans2;
ans %= MOD;
ans *= modbinom(N * M - 2, K - 2);
ans %= MOD;
cout << ans << endl;
} | #include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <string>
#include <queue>
#include <fstream>
#include <string>
#include <math.h>
#include <set>
#include <string>
#include <bitset>
#include <fstream>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
//typedef pair<ll, ll> P;
//typedef pair<ll, P> T;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define revrep(i, n) for (ll i = (n) - 1; i >= 0; i--)
#define f first
#define s second
#define pb push_back
ll mod = 1000000007;
const ll INF = 100000000000010000;
//const ull B = 998244353;
//ll bit[200010];
//ll kai[3010];
//ll Cnt[10010];
//void pres(double A, ll x = 20) { printf("%.20f\n", x); }
vector<ll> dx = { 0, 1, 0, -1, 1, -1, 1, -1, 0};
vector<ll> dy = { 1, 0, -1, 0, 1, 1, -1, -1, 0};
void chmin(ll& x, ll y) {
x = min(x, y);
}
void chmax(ll& x, ll y) {
x = max(x, y);
}
/*ll sum(ll i)
{
i += 1;
ll t = 0;
while (i > 0)
{
t += bit[i];
i -= i & -i;
}
return t;
}
void add(ll i, ll x)
{
i += 1;
while (i <= 200005)
{
bit[i] += x;
i += i & -i;
}
}*/
/*`ll SAIKI(ll S) {
for (ll T = S; ; T = (T - 1) & S) {
}
}*/
void test() {
cout << "888888888888888888888888" << endl;
}
ll gcd(ll x, ll y) {
if (y == 0) return x;
return gcd(y, x % y);
}
ll pow_mod(ll x, ll k) {
x %= mod;
ll res = 1;
while (k > 0) {
if (k % 2) {
res *= x; res %= mod;
}
x *= x; x %= mod;
k /= 2;
}
return res;
}
ll div_mod(ll x) {
return pow_mod(x, mod - 2);
}
vector <ll> par; // 各元の親を表す配列
vector <ll> siz; // 素集合のサイズを表す配列(1 で初期化)
void init_u(ll sz_) {
par.resize(sz_);
siz.assign(sz_, 1LL); // resize だとなぜか初期化されなかった
for (ll i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身
}
// Member Function
// Find
ll root(ll x) { // 根の検索
while (par[x] != x) {
x = par[x] = par[par[x]]; // x の親の親を x の親とする
}
return x;
}
// Union(Unite, Merge)
bool merge(ll x, ll y) {
x = root(x);
y = root(y);
if (x == y) return false;
// merge technique(データ構造をマージするテク.小を大にくっつける)
if (siz[x] < siz[y]) swap(x, y);
siz[x] += siz[y];
par[y] = x;
return true;
}
bool issame(ll x, ll y) { // 連結判定
return root(x) == root(y);
}
ll size(ll x) { // 素集合のサイズ
return siz[root(x)];
}
//コンビネーション
const int MAXcomb = 1000010;
ll fac[MAXcomb], finv[MAXcomb], inv[MAXcomb];
//facはn!,finvは1/n!
//invは逆元
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAXcomb; i++) {
fac[i] = fac[i - 1] * i % mod;
inv[i] = mod - inv[mod%i] * (mod / i) % mod;
finv[i] = finv[i - 1] * inv[i] % mod;
}
}
ll comb(int n, int k) {
if (n < k) return 0;
if (n < 0 || k < 0) return 0;
return fac[n] * finv[k] % mod * finv[n - k] % mod;
}
/*map<ll, ll> pres;
ll ct;
void makePrimeList(ll x) {
ll X = x;
for (ll j = 2; j * j <= x; j++) {
ll cnt = 0;
while (X % j == 0) {
cnt++;
X /= j;
}
if (cnt) {
if (pres[j] == 0) {
ct++;
pres[j] = ct;
}
Cnt[pres[j]] += cnt;
}
}
if (X != 1) {
if (pres[X] == 0) {
ct++;
pres[X] = ct;
}
Cnt[pres[X]] += 1;
}
}*/
ll N, M, K;
ll ans, num;
void solve() {
cin >> N >> M >> K;
COMinit();
rep(i, N) {
num += i * (i + 1) / 2 % mod;
num %= mod;
}
ans += comb(N * M - 2, K - 2) * M % mod * M % mod * num % mod;
ans %= mod;
num = 0;
rep(i, M) {
num += i * (i + 1) / 2 % mod;
num %= mod;
}
ans += comb(N * M - 2, K - 2) * N % mod * N % mod * num % mod;
ans %= mod;
cout << ans << endl;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll T = 1;
//cin >> T;
rep(i, T) solve();
ll N;
cin >> N;
} | 1 |
#include <bits/stdc++.h>
using namespace std;
template<class T>
struct SegmentTree {
using F = function<T(T, T)>;
const T init;
const F f;
int sz;
vector<T> seg;
SegmentTree(int n, const T init, const F f) :
init(init),
f(f)
{
sz = 1;
while(sz < n) sz <<= 1;
seg.resize(sz << 1, init);
}
void update(int id) {
while(id >>= 1) {
seg[id] = f(seg[2 * id], seg[2 * id + 1]);
}
}
void replace(int id, T x){
id += sz;
seg[id] = x;
update(id);
}
// [l,r)
T get(int l, int r){
T L = init, R = init;
for(l += sz, r += sz; l < r; l >>= 1, r>>= 1) {
if(l & 1) L = f(L, seg[l++]);
if(r & 1) R = f(seg[--r], R);
}
return f(L, R);
}
void debug(){
int i = 1;
for(int r = 2; r <= 2 * sz; r <<= 1) {
while(i < r) {
cout << seg[i++] << " ";
}
cout << "\n";
}
}
};
int main() {
int n; cin >> n;
pair<int, int> p[n];
for(auto &e : p) {
cin >> e.first >> e.second;
e.second++;
}
sort(p, p + n);
SegmentTree<int> segL(n, 0, [](int lhs, int rhs) {
return max(lhs, rhs);
});
SegmentTree<int> segR(n, 1e9 + 10, [](int lhs, int rhs) {
return min(lhs, rhs);
});
for(int i = 0; i < n; ++i) {
segL.replace(i, p[i].first);
segR.replace(i, p[i].second);
}
int ans = 0;
for(int i = 0; i < n - 1; ++i) {
{
int l1 = p[i].first;
int r1 = p[i].second;
int l2 = max(segL.get(0, i), segL.get(i + 1, n));
int r2 = min(segR.get(0, i), segR.get(i + 1, n));
ans = max(ans, max(0, (r1 - l1)) + max(0, (r2 - l2)));
}
{
int l1 = segL.get(0, i + 1);
int r1 = segR.get(0, i + 1);
int l2 = segL.get(i + 1, n);
int r2 = segR.get(i + 1, n);
ans = max(ans, max(0, (r1 - l1)) + max(0, (r2 - l2)));
}
}
cout << ans << '\n';
return 0;
} | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<vector<vector<ll>>> vvvll;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<vector<vector<bool>>> vvvb;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef vector<double> vd;
typedef vector<vd> vdd;
typedef pair<double, double> pd;
typedef vector<pd> vpd;
#define FOR(i,x,y) for(ll i=(ll)x; i<(ll)y; ++i)
#define REP(i,y) FOR(i, 0, y)
#define RFOR(i,x,y) for(ll i=(ll)x; i>=(ll)y; --i)
#define RREP(i,x) RFOR(i, x, 0)
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define debug_print(x...) cerr << "line " << __LINE__ << " : "; debug_print_in(x);
template <typename First>
void debug_print_in(First first){
cerr << first << endl;
return;
}
template <typename First, typename... Rest>
void debug_print_in(First first, Rest... rest){
cerr << first << " ";
debug_print_in(rest...);
return;
}
void IN(void){
return;
}
template <typename First, typename... Rest>
void IN(First& first, Rest&... rest){
cin >> first;
IN(rest...);
return;
}
template <typename First>
void OUT(First first){
cout << first << endl;
return;
}
template <typename First, typename... Rest>
void OUT(First first, Rest... rest){
cout << first << " ";
OUT(rest...);
return;
}
template<class t, class u> t chmax(t&a,u b){if(a<b)a=b; return a;};
template<class t, class u> t chmin(t&a,u b){if(a>b)a=b; return a;};
int popcount(int t){return __builtin_popcount(t);} //GCC
int popcount(ll t){return __builtin_popcountll(t);} //GCC
template <typename T>
void vec_print(vector<T> VEC){
REP(i, VEC.size()){
cerr << VEC[i] << " ";
}
cerr << endl;
};
template <typename T>
void mat_print(vector<vector<T> > MAT){
REP(i,MAT.size()){
REP(j,MAT[i].size()){
cerr << MAT[i][j] << " ";
}
cerr << endl;
}
};
constexpr int INF = (1<<30);
constexpr ll INFLL = 1LL<<62;
constexpr long double EPS = 1e-12;
constexpr ll MOD = (ll)((1E+9)+7);
int main(){
cin.tie(0); // cut the cin and cout (default, std::flush is performed after std::cin)
ios::sync_with_stdio(false); // cut the iostream and stdio (DON'T endl; BUT "\n";)
ll N;
IN(N);
vpll RL(N);
REP(i,N){
IN(RL[i].second, RL[i].first);
}
sort(ALL(RL));
vll Lmax(N, RL[0].second);
REP(i,N-1){
Lmax[i+1] = max(Lmax[i], RL[i+1].second);
}
/*
REP(i,N){
debug_print(RL[i].second, RL[i].first);
}
*/
ll ans = max(RL[0].first-Lmax[N-2]+1, (ll)0)+(RL[N-1].first-RL[N-1].second)+1;
ll Lmax_after = RL[N-1].second;
for(ll i=N-2; i>0; --i){
ll Lmax1 = Lmax[i-1];
ll Rmin1 = RL[0].first;
ll Lmax2 = RL[i].second;
ll Rmin2 = RL[i].first;
ll LR1 = max(Rmin1-Lmax1+1, (ll)0);
ll LR2 = max(Rmin2-Lmax2+1, (ll)0);
ll LR1_1 = max(Rmin1-max(Lmax_after, Lmax1)+1, (ll)0);
ll LR2_1 = max(Rmin2-max(Lmax_after, Lmax2)+1, (ll)0);
ll ans_tmp = max(LR1+LR2_1, LR1_1+LR2);
chmax(ans, ans_tmp);
chmax(Lmax_after, Lmax2);
}
OUT(ans);
return 0;
}
| 1 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.