code_file1
stringlengths 80
4k
| code_file2
stringlengths 91
4k
| similar_or_different
int64 0
1
|
---|---|---|
// https://atcoder.jp/contests/arc069/tasks/arc069_b
#include "algorithm"
#include "bitset"
#include "cmath"
#include "functional"
#include "iomanip"
#include "iostream"
#include "map"
#include "numeric"
#include "queue"
#include "set"
#include "string"
#include "vector"
#define rep(i, to) for (ll i = 0; i < (to); ++i)
#define rep1(i, to) for (ll i = 1; i <= (to); ++i)
#define repf(i, from, to) for (ll i = from; i < (to); ++i)
#define repr(i, from) for (ll i = from - 1; i >= 0; --i)
#define all(vec) vec.begin(), vec.end()
#define unless(cond) if (!(cond))
#define fi first
#define se second
using namespace std;
typedef long long ll;
template <typename T>
using V = vector<T>;
using VL = V<ll>;
using VVL = V<VL>;
template <typename T, typename U>
using P = pair<T, U>;
using PL = P<ll, ll>;
using VPL = V<PL>;
template <typename T>
inline bool chmax(T& a, T b);
template <typename T>
inline bool chmin(T& a, T b);
void print_ints(vector<ll> v);
template <typename T>
void drop(T a);
const ll INF = 1e18;
void solve() {
ll N;
string S;
cin >> N >> S;
auto get_next = [](char prev, char cur, char ch) -> char {
if (cur == 'S' && ch == 'o') return prev == 'S' ? 'S' : 'W';
if (cur == 'S' && ch == 'x') return prev == 'S' ? 'W' : 'S';
if (cur == 'W' && ch == 'o') return prev == 'S' ? 'W' : 'S';
if (cur == 'W' && ch == 'x') return prev == 'S' ? 'S' : 'W';
};
auto to_s = [](ll v) -> char { return v == 0 ? 'S' : 'W'; };
rep(f, 2) rep(s, 2) {
string res;
res.push_back(to_s(f));
res.push_back(to_s(s));
repf(cur_i, 1, N - 1) {
char n = get_next(res[cur_i - 1], res[cur_i], S[cur_i]);
res.push_back(n);
}
if (get_next(res[N - 2], res[N - 1], S[N - 1]) == res[0] &&
get_next(res[N - 1], res[0], S[0]) == res[1])
drop(res);
}
drop(-1);
}
struct exit_exception : public std::exception {
const char* what() const throw() { return "Exited"; }
};
#ifndef TEST
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
try {
solve();
} catch (exit_exception& e) {
}
return 0;
}
#endif
template <typename T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
void print_ints(vector<ll> v) {
rep(i, v.size()) {
if (i > 0) {
cout << " ";
}
cout << v[i];
}
cout << endl;
}
template <typename T>
void drop(T res) {
cout << res << endl;
throw exit_exception();
}
| //#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using ld=long double;
using st=string;
using ch=char;
typedef pair<ll,ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<P> vP;
typedef vector<ch> vc;
typedef vector<vc> vvc;
#define FOR(i,a,b) for(ll i=a;i<b;i++)
#define rep(i,n) FOR(i,0,n)
#define ROF(i,a,b) for(ll i=a;i>=b;i--)
#define per(i,a) ROF(i,a,0)
#define pb push_back
#define mp make_pair
const ll MOD=1000000007;
const ll MOD2=998244353;
const ld PI=acos(-1);
const ll INF=1e18;
st abc="abcdefghijklmnopqrstuvwxyz";
st ABC="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
struct edge{ll to,cost;};
ll n;
st s;
vc a;
bool b;
void srch(ll z,ll f){
if(z==0){
a[0]='S';
}
else{
a[0]='W';
}
if(f==0){
a[1]='S';
}
else{
a[1]='W';
}
FOR(i,1,n-1){
if((a[i]=='S'&&s[i]=='o')||(a[i]=='W'&&s[i]=='x')){
a[i+1]=a[i-1];
}
else{
if(a[i-1]=='W'){
a[i+1]='S';
}
else{
a[i+1]='W';
}
}
}
b=true;
rep(i,2){
ll cnt=0;
if(a[(n-1+i)%n]=='W'){
cnt++;
}
if(s[(n-1+i)%n]=='x'){
cnt++;
}
if(a[(n-2+i)%n]!=a[i%n]){
cnt++;
}
if(cnt%2==1){
b=false;
}
}
}
int main(){
cin >> n >> s;
a.resize(n);
b=false;
srch(0,0);
if(b){
rep(i,n){
cout << a[i];
}
return 0;
}
srch(0,1);
if(b){
rep(i,n){
cout << a[i];
}
return 0;
}
srch(1,0);
if(b){
rep(i,n){
cout << a[i];
}
return 0;
}
srch(1,1);
if(b){
rep(i,n){
cout << a[i];
}
return 0;
}
cout << -1 << endl;
} | 1 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 1000000000
#define LLINF 100000000000000000ll
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define all(x) (x).begin(),(x).end()
#define sq(x) ((x)*(x))
#define rep(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define repn(i,a,n) for(int (i)=(a);(i)<(int)(n);(i)++)
#define EQ(a,b) (abs((a)-(b))<eps)
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;}
int H,W;
string s[2010];
int dp[2010][2010];
int L[2010],R[2010];
int max_rect(vector<int> v){
stack<int> st;
for(int i=0;i<v.size();i++){
while(!st.empty()&&v[st.top()]>=v[i])st.pop();
if(st.empty())L[i]=-1;
else L[i]=st.top();
st.push(i);
}
while(!st.empty())st.pop();
for(int i=v.size()-1;i>=0;i--){
while(!st.empty()&&v[st.top()]>=v[i])st.pop();
if(st.empty())R[i]=v.size();
else R[i]=st.top();
st.push(i);
}
int res = 0;
for(int i=0;i<v.size();i++){
if(L[i]==-1)res = max(res,(R[i]-L[i]-1)*v[i]);
else res = max(res,(R[i]-L[i])*v[i]);
}
return res;
}
int main(){
cin >> H >> W;
for(int i=0;i<H;i++)cin >> s[i];
for(int i=0;i<W;i++)dp[0][i]=W-i;
for(int i=1;i<H;i++){
dp[i][W-1]=1;
for(int j=W-2;j>=0;j--){
if(s[i][j]==s[i-1][j]&&s[i][j+1]==s[i-1][j+1])dp[i][j]=dp[i][j+1]+1;
else if(s[i][j]!=s[i-1][j]&&s[i][j+1]!=s[i-1][j+1])dp[i][j]=dp[i][j+1]+1;
else dp[i][j]=1;
}
}
/*for(int i=0;i<H;i++){
for(int j=0;j<W;j++){
cout << dp[i][j] << ' ';
}
cout << endl;
}*/
int ans = 0;
for(int j=0;j<W;j++){
vector<int> v;
for(int i=0;i<H;i++){
v.pb(dp[i][j]);
}
ans = max(ans,max_rect(v));
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define FOR(i, a, b) for(ll i = (a); i < (b); ++i)
#define FORR(i, a, b) for(ll i = (a); i > (b); --i)
#define REP(i, n) for(ll i = 0; i < (n); ++i)
#define REPR(i, n) for(ll i = n; i >= 0; i--)
#define FOREACH(x, a) for(auto &(x) : (a))
#define VECCIN(x) \
for(auto &youso_ : (x)) cin >> youso_
#define bitcnt __builtin_popcount
#define SZ(x) ((ll)(x).size())
#define fi first
#define se second
#define All(a) (a).begin(), (a).end()
template <typename T = long long> inline T IN() {
T x;
cin >> x;
return (x);
}
inline void CIN() {}
template <class Head, class... Tail>
inline void CIN(Head &&head, Tail &&... tail) {
cin >> head;
CIN(move(tail)...);
}
#define CCIN(...) \
char __VA_ARGS__; \
CIN(__VA_ARGS__)
#define DCIN(...) \
double __VA_ARGS__; \
CIN(__VA_ARGS__)
#define LCIN(...) \
ll __VA_ARGS__; \
CIN(__VA_ARGS__)
#define SCIN(...) \
string __VA_ARGS__; \
CIN(__VA_ARGS__)
#define Yes(a) cout << (a ? "Yes" : "No") << "\n"
#define YES(a) cout << (a ? "YES" : "NO") << "\n"
#define Printv(v) \
{ \
FOREACH(x, v) { cout << x << " "; } \
cout << "\n"; \
}
template <typename T = string> inline void eputs(T s) {
cout << s << "\n";
exit(0);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
std::fill((T *)array, (T *)(array + N), val);
}
template <typename T> using PQG = priority_queue<T, vector<T>, greater<T>>;
template <typename T> using PQ = priority_queue<T>;
typedef long long ll;
typedef pair<ll, ll> PL;
typedef vector<PL> VPL;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef vector<double> VD;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const double PI = atan(1.0) * 4.0;
// const int MOD = 998244353;
const ll LINF = 9e18;
const ll dx[] = {1, 0, -1, 0};
const ll dy[] = {0, 1, 0, -1};
void cinfast() {
cin.tie(0);
ios::sync_with_stdio(false);
}
ll S[2000][2000], sum[2020][2020], csum[2020][2020], rsum[2020][2020];
signed main() {
cinfast();
LCIN(N, M, Q);
REP(i, N) REP(j, M) {
CCIN(s);
S[i][j] = (s == '1');
sum[i + 1][j + 1] +=
sum[i + 1][j] + sum[i][j + 1] - sum[i][j] + S[i][j];
if(j)
rsum[i + 1][j + 1] += rsum[i + 1][j] + rsum[i][j + 1] - rsum[i][j] +
(S[i][j] & S[i][j - 1]);
if(i)
csum[i + 1][j + 1] += csum[i + 1][j] + csum[i][j + 1] - csum[i][j] +
(S[i][j] & S[i - 1][j]);
}
REP(i, Q) {
LCIN(x1, y1, x2, y2);
x1--, y1--, x2--, y2--;
ll ans = 0;
ans += sum[x2 + 1][y2 + 1] - sum[x1][y2 + 1] - sum[x2 + 1][y1] +
sum[x1][y1];
ans -= rsum[x2 + 1][y2 + 1] - rsum[x1][y2 + 1] - rsum[x2 + 1][y1 + 1] +
rsum[x1][y1 + 1];
ans -= csum[x2 + 1][y2 + 1] - csum[x1 + 1][y2 + 1] - csum[x2 + 1][y1] +
csum[x1 + 1][y1];
cout << ans << "\n";
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define Opy ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
typedef long long ll;
int main()
{
//Opy;
/*-------------------------------------------------
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
-------------------------------------------------*/
ll a, h = 0;
cin>> a;
h += (a - (a % 500)) * 2;
a %= 500;
h += a - (a % 5);
cout<< h<< endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ll X;
cin >> X;
ll c1=0;
ll c2=0;
while(X >= 500){
X -= 500;
c1++;
}
while(X >= 5){
X -= 5;
c2++;
}
cout << c1*1000+c2*5 << endl;
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll N= 998244353;
ll m_pow(ll n, ll k){
if(k==0) return 1;
else{
ll a = (m_pow(n,k/2)*m_pow(n,k/2))%N;
if(k%2==0) return a;
else return (a*n)%N;
}
}
int main(){
ll n,m,k;
cin >> n >> m >> k;
vector<ll> mod_pow(n,1),combi(k+1,1);
for(int i=1; i<n; i++){
mod_pow.at(i) = mod_pow.at(i-1)*(m-1);
mod_pow.at(i) %= N;
if(i>k) continue;
combi.at(i) = ((n-i)*combi.at(i-1))%N;
combi.at(i) *= m_pow(i,N-2);
combi.at(i) %= N;
}
ll ans = 0;
for(int i=0; i<=k; i++){
ll plus= (m*mod_pow.at(n-1-i))%N;
plus = (plus*combi.at(i))%N;
ans += plus;
ans %= N;
}
cout << ans << endl;
/*
for(int i=0; i<k+1; i++){
cout << combi.at(i) << endl;
}*/
} | #include <bits/stdc++.h>
using namespace std;
#define pb push_back
using ll = long long;
const int N = 2e5 + 5;
const int MOD = 1e9 + 7;
int n;
ll ar[N], sum[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> ar[i];
sum[i] = ar[i];
}
for (int i = n - 1; i > 0; i--) {
sum[i] += sum[i + 1];
sum[i] %= MOD;
}
ll ans = 0;
for (int i = 1; i <= n; i++) {
ans += ar[i] * sum[i + 1] % MOD;
ans %= MOD;
}
cout << ans << '\n';
return 0;
} | 0 |
#include <stdio.h>
int main(){
char stringa[120];
char stringb[120];
scanf ("%s %s", &stringa, &stringb);
printf ("%s%s", stringb, stringa);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {
return '"' + s + '"';
}
string to_string(const char* s) {
return to_string((string) s);
}
string to_string(bool b) {
return (b ? "true" : "false");
}
string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template <size_t N>
string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++) {
res += static_cast<char>('0' + v[i]);
}
return res;
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
void solve() {
string s, t;
cin >> s >> t;
t += s;
cout << t << "\n";
debug(s);
debug(t);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
}
| 1 |
#include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define rep(i,n) for( int i = 0; i < n; i++ )
#define rrep(i,n) for( int i = n; i >= 0; i-- )
#define REP(i,s,t) for( int i = s; i <= t; i++ )
#define RREP(i,s,t) for( int i = s; i >= t; i-- )
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000
#define int long long
int x[100010], y[100010];
int n, m;
int sumx[100010], sumy[100010];
signed main(void)
{
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n >> m;
rep(i, n) {
cin >> x[i];
sumx[i + 1] = (sumx[i + 1] + x[i] + sumx[i]) % mod;
}
rep(i, m) {
cin >> y[i];
sumy[i + 1] = (sumy[i + 1] + y[i] + sumy[i]) % mod;
}
int ansX = 0, ansY = 0;
rep(i, n - 1) {
ansX = (ansX + (-x[i] * (n - i - 1)) % mod + (sumx[n] - sumx[i + 1]) % mod + mod) % mod ;
//cout << -x[i] * (n - i - 1) <<" " << (sumx[n] - sumx[i + 1]) << endl;
}
rep(i, m - 1) {
ansY = (ansY + (-y[i] * (m - i - 1)) % mod + (sumy[m] - sumy[i + 1]) % mod + mod) % mod;
}
cout << (ansX * ansY) % mod << endl;
return 0;
}
| #include <iostream>
#include <cmath>
#include <vector>
#include <bitset>
#include <string>
#include <utility>
#include <queue>
#include <algorithm>
using namespace std;
long long int mpown(long long int m, int n);
int bitCount(int bits);
int main()
{
int n,k;
cin >> n >>k;
vector<long long int> v(n);
for(int i=0;i<n;i++)cin >> v.at(i);
// vector<vector<long long int>> dp(n+1,vector<long long int>(n+1,0));
// vector<vector<long long int>> dpm(n+1,vector<long long int>(n+1,0));
// vector<vector<long long int>> dpmn(n+1,vector<long long int>(n+1,0));
// dp.at(i).at(j) : 左からi個,右からj個とったときの価値の合計
// dpmn.at(i).at(j) : 左からi個,右からj個とったときの負の宝石の数
// dpm.at(i).at(j) : 左からi個,右からj個とったときの負の価値の合計
int r = min(n,k);
priority_queue<long long int> mns;
long long int sv=0;
long long int temp;
long long int ans=(-1000000000000);
for(int a=0;a<=n;a++){
for(int b=0;b<=r-a;b++){
sv=0;
priority_queue<long long int> mns;
for(int i=0;i<a;i++){
sv+=v.at(i);
if(v.at(i)<0)
mns.push(-v.at(i));
}
for(int j=0;j<b;j++){
sv+=v.at(n-j-1);
if(v.at(n-j-1)<0)
mns.push(-v.at(n-j-1));
}
for(int i=0;i<k-(a+b);i++){
if(mns.empty()){break;}
sv+=mns.top();
mns.pop();
}
if(ans<sv){ans=sv;
// cout << a << " " << b << endl;
}
}
}
cout << ans << endl;
}
int bitCount(int bits)
{
bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555); // 2bitごとに計算
bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333); // 4bitごとに計算
bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f); // 8bitごとに計算
bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff); // 16ビットごとに計算
return (bits & 0x0000ffff) + (bits >> 16); // 32ビット分を計算
}
long long int mpown(long long int m, int n)
{
if (n == 0)
return 1;
else if (n == 1)
return m;
else
{
long long int temp = mpown(m, n / 2);
return temp * temp * (n & 1 ? m : 1);
}
} | 0 |
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <cstdlib>
#include <vector>
#define R register
#define IN inline
#define gc getchar()
#define ll long long
#define MX 105
template <class T>
IN void in(T &x)
{
x = 0; R char c = gc;
for (; !isdigit(c); c = gc);
for (; isdigit(c); c = gc)
x = (x << 1) + (x << 3) + c - 48;
}
int n, sum;
std::vector <int> eve, odd;
int main(void)
{
in(sum), in(n);
int foo;
for (R int i = 1; i <= n; ++i)
{
in(foo);
if (foo & 1) odd.push_back(foo);
else eve.push_back(foo);
}
if (n == 1)
{
if (sum == 1) printf("1\n1\n1");
else printf("%d\n2\n%d %d", sum, 1, sum - 1);
return 0;
}
if (odd.size() > 2) return puts("Impossible"), 0;
else if (odd.size() == 2)
{
if (odd[0] > odd[1]) std::swap(odd[0], odd[1]);
printf("%d ", odd[0]);
for (auto i : eve) printf("%d ", i);
printf("%d\n", odd[1]);
odd[0]--, odd[1]++;
if (!odd[0]) printf("%d\n", n - 1);
else printf("%d\n%d ", n, odd[0]);
for (auto i : eve) printf("%d ", i);
printf("%d ", odd[1]);
}
else if (odd.size() == 1)
{
printf("%d ", odd[0]);
for (auto i : eve) printf("%d ", i);
puts("");
odd[0]++, eve[eve.size() - 1]--;
printf("%d\n", n);
printf("%d ", odd[0]);
for (auto i : eve) printf("%d ", i);
}
else
{
for (auto i : eve) printf("%d ", i);
eve[0]++, eve[eve.size() - 1]--;
printf("\n%d\n", n);
for (auto i : eve) printf("%d ", i);
}
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const int INF = 1001001001;
const int MOD = 1000000007;
template <typename T>
void print(const T &v);
const int MAX = 510'000;
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;
}
vector<pair<ll, ll>>
factorization(ll num)
{
map<ll, ll> mp;
ll tmp = num;
for (ll i = 2; i * i <= num; i++)
{
while (tmp % i == 0)
{
mp[i]++;
tmp /= i;
}
}
if (tmp != 1)
{
mp[num]++;
}
vector<pair<ll, ll>> ret;
for (auto m : mp)
{
ret.push_back(make_pair(m.first, m.second));
}
return ret;
}
int main()
{
COMinit();
int n, m;
cin >> n >> m;
auto fac = factorization(m);
ll ans = 1;
for (int i = 0; i < fac.size(); i++)
{
int tmp = fac[i].second;
ans = (ans * COM(n + tmp - 1, tmp)) % MOD;
}
cout << ans << endl;
return 0;
}
// Use For Debug
template <typename T>
void print(T const &v)
{
for (int i = 0; i < v.size(); i++)
{
if (i)
cout << " ";
cout << v[i];
}
cout << endl;
}
| 0 |
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>
#include<cstdio>
using namespace std;
#define ll long long
#define rep(i, n) for(int i = 0; i < n; i++)
#define P pair<int, int>
typedef vector<int> vec;
typedef vector<vec> mat;
const ll mod = 1000000007;
deque<int> col;
int main(){
int n;
cin >> n;
int a[n];
rep(i, n) cin >> a[i];
col.push_front(a[0]);
for (int i = 1; i < n; i++){
int idx = lower_bound(col.begin(), col.end(), a[i]) - col.begin();
if(idx == 0){
col.push_front(a[i]);
}else{
col[idx-1] = a[i];
}
}
int ans = col.size();
cout << ans << endl;
} | #include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
int main(void){
int n, m, t, i, j;
scanf("%d %d", &n, &m);
vector<int> sports;
vector<int> votes(m, 0);
for(i = 0;i < n;++i){
scanf("%d", &t);
sports.push_back(t);
}
for(i = 0;i < m;++i){
scanf("%d", &t);
for(j = 0;j < n;++j){
if(sports[j] <= t){
votes[j]++;
break;
}
}
}
vector<int>::iterator itr = max_element(votes.begin(), votes.end());
size_t d = distance(votes.begin(), itr);
printf("%zu\n", d+1);
return 0;
}
| 0 |
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
string S;
cin >> S;
bool hflag = false, iflag = false;
string ans = "Yes";
for (int i = 0; i < S.length(); i++) {
if (S[i] == 'h') {
if ((i < S.length() - 1) && S[i + 1] == 'i') {
i++;
continue;
} else {
ans = "No";
break;
}
} else {
ans = "No";
break;
}
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>//復習必須
#include<iostream>
#include<vector>
#include <cmath>
#include <map>
#include <algorithm>
#include <string>
#define rep(i, n) for (int i = 0; i < n; ++i)
using ll = long long;
using namespace std;
#define P pair<int, int>
int main () {
string s ;
cin >> s ;
size_t n = s.length() ;
bool flag = true ;
if(n % 2 != 0 ){
cout << "No" << endl;
return 0 ;
}else{
rep(i , n /2){
if(s[2*i] != 'h' || s[2*i + 1] != 'i'){
flag = false ;
}
}
}
if(flag == true){
cout << "Yes" << endl;
}else{
cout << "No" << endl;
}
return 0 ;
}
| 1 |
#include<bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
#define pb push_back
using namespace std;
typedef long long ll;
int main(){
int n,m,q;
cin>>n>>m>>q;
vector<vector<int>> d(n,vector<int>(n));
rep(i,m){
int l,r;
cin>>l>>r;
l--; r--;
d[n-l-1][r]++;
}
rep(i,n)for(int j=1;j<n;j++){
d[i][j]+=d[i][j-1];
if(i){
if(d[i-1][j-1]!=d[i-1][j]){
d[i][j]+=d[i-1][j]-d[i-1][j-1];
}
}
}
rep(i,q){
int p,q;
cin>>p>>q;
p--; q--;
cout<<d[n-p-1][q]<<endl;
}
} | #include<bits/stdc++.h>
using namespace std;
int n, l, q;
vector<int>v;
vector<vector<int> >moves;
void preprocess(){
int cur = 1;
for(int i = 1; i <= n; i++){
while(cur <= n && v[cur] - v[i] <= l){
cur++;
}
moves[i][0] = cur - 1;
}
for(int j = 1; j <= 17; j++){
for(int i = 1; i <= n; i++){
int mid = moves[i][j - 1];
if(mid != -1){
moves[i][j] = moves[mid][j - 1];
}
}
}
}
int query(int l, int r){
int ans = 0;
if(l > r){
swap(l, r);
}
for(int i = 17; i >= 0; i--){
if(moves[l][i] == -1){
continue;
}
if(moves[l][i] < r){
l = moves[l][i];
ans += 1 << i;
}
}
ans++;
return ans;
}
int main(){
cin >> n;
v.assign(n + 2, 0);
moves.assign(n + 2, vector<int>(19, -1));
for(int i = 1; i <= n; i++){
cin >> v[i];
}
cin >> l >> q;
preprocess();
for(int i = 1; i <= q; i++){
int l, r;
cin >> l >> r;
cout << query(l, r) << '\n';
}
return 0;
} | 0 |
// #define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <locale>
#include <cctype>
#include <sstream>
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef map<int, int> MAPII;
typedef vector<pair<int, int> > VPII;
typedef multimap<int, string, greater<int> > MuMIS;
#define MP make_pair
#define fastIO cin.tie(0); ios::sync_with_stdio(false);
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
//for gcc (未test)
// #define FOREACH_IT(it,c) for(typeof(c)::iterator it=(c).begin(); it!=(c).end(); ++it)
//for Visual Studio
#define foreach_it(type,it,c) for(type::iterator it=c.begin(), c_end=c.end(); it!=c_end; ++it)
// ------------------- include, typedef, define END. -------------------
int Hit(int *rui){
rui[0]++;
// 塁を進める
FOR(i, 1, 4){
if (rui[i - 1]==2){
rui[i]++;
rui[i - 1] = 1;
}
}
if (rui[3]!=0){
rui[3] = 0;
return 1;
}
return 0;
}
int Homerun(int *rui){
int ret = 1;
FOR(i, 0, 4)
if (rui[i])
ret++;
return ret;
}
int main(){
fastIO;
int n, outCount = 0, tokuten = 0;
string in;
// rui[0]〜rui[2] = 1塁〜3塁, rui[3] = 本塁
// 各値はその塁にいる人数(0〜2)
int rui[4] = {};
cin >> n;
while (cin >> in){
if (in == "OUT")
outCount++;
if (outCount < 3){
if (in == "HIT"){
tokuten += Hit(rui);
}
else if (in == "HOMERUN"){
tokuten += Homerun(rui);
FOR(i, 0, 4) rui[i] = 0;
}
}
else{
cout << tokuten << endl;
FOR(i, 0, 4) rui[i] = 0;
tokuten = 0;
outCount = 0;
}
}
return 0;
} | #include <iostream>
#include <string>
class Baseball{
private:
int point;
int out_count;
int runner;
public:
Baseball();
void singleHit();
void homerun();
void out();
void new_inning();
int get_out_count();
int get_point();
~Baseball() = default;
};
Baseball::Baseball(){
new_inning();
}
void Baseball::singleHit(){
if(runner < 3){
++runner;
}
else if(runner == 3){
++point;
}
}
void Baseball::homerun(){
point += runner + 1;
runner = 0;
}
void Baseball::out(){
++out_count;
}
void Baseball::new_inning(){
point = 0;
out_count = 0;
runner = 0;
}
int Baseball::get_out_count(){
return out_count;
}
int Baseball::get_point(){
return point;
}
int main(){
int num;
std::cin >> num; //data set number
Baseball baseball;
std::string event;
for(int i=0; i<num; ++i){
baseball.new_inning();
while(baseball.get_out_count() < 3){
std::cin >> event;
if(event == "HIT"){
baseball.singleHit();
}
else if(event == "HOMERUN"){
baseball.homerun();
}
else if(event == "OUT"){
baseball.out();
}
}
//output
std::cout << baseball.get_point() << std::endl;
}
} | 1 |
#include <iostream>
#include <string>
using namespace std;
int main(){
string str;
int i,j;
cin >> str;
string st="";
for( i=str.size()-1; i>=0 ; i-- )
st+=str[i] ;
cout << st << endl;
} | #include<iostream>
#include<string>
#include<algorithm>
#include<iterator>
int main(){
std::string str;
std::cin >> str;
std::reverse_copy(str.begin(), str.end(),
std::ostream_iterator<char>(std::cout,""));
std::cout << std::endl;
return 0;
} | 1 |
#include <bits/stdc++.h>
using namespace std;
int main(int argc, const char *argv[]) {
vector<string> vs(3);
for (int i = 0; i < 3; ++i) {
cin >> vs[i];
}
vector<int> cur(3, 0);
int t = 0;
char ans;
while (true) {
if (cur[t] >= vs[t].size()) {
ans = 'A' + t;
break;
}
cur[t]++;
t = vs[t][cur[t] - 1] - 'a';
}
cout << ans << '\n';
} | #include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
if(s.size()==3){
for(int i=2;i>=0;i--){
cout << s.at(i);
}
cout << endl;
}
else{
cout << s << endl;
}
}
| 0 |
#include <iostream>
void ArrayDump(int *A, int N)
{
for (int i = 0; i < N; i++) {
std::cout << *(A + i);
if (i != N - 1) {
std::cout << " ";
}
}
std::cout << std::endl;
}
int BubbleSort(int *A, int N)
{
bool flag = true;
int exchange_num = 0;
while (flag) {
flag = false;
for (int j = N-1; j > 0; j--) {
if (A[j] < A[j-1]) {
int tmp = A[j];
A[j] = A[j-1];
A[j-1] = tmp;
flag = true;
exchange_num++;
}
}
}
return exchange_num;
}
int main(int argc, char* argv[])
{
int array_size = 0;
int array[100];
std::cin >> array_size;
for (int i = 0; i < array_size; i++) {
std::cin >> array[i];
}
int exchange_num = BubbleSort(array, array_size);
ArrayDump(array, array_size);
std::cout << exchange_num << std::endl;
}
| #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
//#define int long long
#define DBG 1
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
signed main() {
int n; cin >> n; vector<int>v(n);
rep(i, 0, n)cin >> v[i];
rep(i, 1, *min_element(all(v))) {
bool f(true);
rep(j, 0, n)if (v[j] % i != 0) {
f = false;
break;
}
if (f)cout << i << endl;
}
return 0;
} | 0 |
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#define MOD 998244353
using namespace std;
typedef long long ll;
template <typename Tp> inline void getint(Tp &num){
register int ch, neg = 0;
while(!isdigit(ch = getchar())) if(ch == '-') neg = 1;
num = ch & 15;
while(isdigit(ch = getchar())) num = num * 10 + (ch & 15);
if(neg) num = -num;
}
inline int fastpow(int bas, int ex){
register int res = 1;
for(; ex; ex >>= 1, bas = bas * (ll)bas % MOD) if(ex & 1) res = res * (ll)bas % MOD;
return res;
}
inline int kazu(int a, const int &b) {return (a += b) >= MOD ? a - MOD : a;}
inline int hiku(int a, const int &b) {return (a -= b) < 0 ? a + MOD : a;}
int N, sum = 0, f[90005], g[90005];
int main(){
getint(N), f[0] = g[0] = 1;
for(register int i = 1, a; i <= N; i++){
getint(a), sum += a;
for(register int j = sum; j >= 0; j--){
f[j] = f[j] * 2LL % MOD;
if(j >= a) f[j] = kazu(f[j], f[j - a]), g[j] = kazu(g[j], g[j - a]);
}
}
int ans = fastpow(3, N);
for(register int i = sum + 1 >> 1; i <= sum; i++) ans = hiku(ans, f[i] * 3LL % MOD);
if(!(sum & 1)) ans = kazu(ans, g[sum >> 1] * 3LL % MOD);
return printf("%d\n", ans), 0;
} | #include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
/*
//#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
*/
// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
// #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}
const ll mod = 998244353;
const int N = 303;
const int M = 90003;
ll dp[N][M][2][2];
ll mod_pow(ll x, ll n){
ll ret = 1;
while(n){
if(n&1) (ret *= x) %= mod;
(x *= x) %= mod;
n >>= 1;
}
return ret;
}
ll mod_inv(ll x){
return mod_pow(x,mod-2);
}
const ll DIV = mod_inv(2);
int main(){
int n;
cin >>n;
vector<int> a(n);
int sa = 0;
rep(i,n){
cin >>a[i];
sa += a[i];
}
dp[0][0][0][0] = 1;
rep(i,n)rep(j,M)rep(x,2)rep(y,2)if(dp[i][j][x][y]){
int nx = j+a[i];
if(nx < M) (dp[i+1][nx][x][y] += dp[i][j][x][y]) %= mod;
(dp[i+1][j][x|1][y] += dp[i][j][x][y]) %= mod;
(dp[i+1][j][x][y|1] += dp[i][j][x][y]) %= mod;
}
ll sub = 0;
rep(i,M)rep(x,2)rep(y,2){
if(sa-i <= i){
if(sa-i==i && (x==0 || y==0)) sub += dp[n][i][x][y]*DIV;
else sub += dp[n][i][x][y];
sub %= mod;
}
}
sub *= 3;
sub %= mod;
ll ans = mod_pow(3,n) - sub + mod;
ans %= mod;
cout << ans << endl;
return 0;
}
| 1 |
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 1000005
using namespace std;
typedef long long LL;
char s[N];
int dp[N][2];
int main()
{
scanf("%s",s+1);
dp[0][1]=1;
for(int i=1;s[i];i++)
{
int d=s[i]-'0';
dp[i][0]=min(dp[i-1][0]+d,dp[i-1][1]+10-d);
dp[i][1]=min(dp[i-1][0]+d+1,dp[i-1][1]+9-d);
if(d==9)dp[i][1]=dp[i-1][1];
}
printf("%d\n",dp[strlen(s+1)][0]);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
//#define int long long
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<n;++i)
#define exrep(i, a, b) for(ll i = a; i < b; i++)
#define out(x) cout << x << endl
#define EPS (1e-7)
#define gearup ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<pair<int,int> > vpii;
typedef vector<vector<int> > vvi;
typedef vector<vector<char> > vvc;
typedef vector<vector<bool> > vvb;
typedef vector<vector<double> > vvd;
typedef vector<vector<string> > vvs;
typedef vector<ll> vl;
typedef vector<vector<ll> > vvl;
typedef vector<vector<vector<ll> > > vvvl;
ll MOD = 1000000007;
const long long L_INF = 1LL << 60;
const int INF = 2147483647; // 2^31-1
const double PI = acos(-1);
//cout<<fixed<<setprecision(10);
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;}
template<class T> void debug(T v){rep(i,v.size()) cout<<v[i]<<" ";cout<<endl;}
const ll dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
bool is_Prime(int x){
if(x < 2)return false;
else if(x == 2) return true;
for(ll i = 2; i*i <= x; i++) if(x%i == 0) return false;
return true;
}
vl res;
vl prime;
ll n;
signed main()
{
gearup;
cin >> n;
//素数格納
for(int i=2;i<=55555;i++){
if(is_Prime(i) && (i % 5) == 1)prime.push_back(i);
}
rep(i,n){
if(i)cout << " ";
cout << prime[i];
}
cout << endl;
} | 0 |
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main(){
string s;
int n;
int c_pos;
cin>>n;
cin.ignore();
while(n--){
getline(cin,s);
while((c_pos=s.find("Hoshino"))!=string::npos){
s.replace(c_pos,7,"Hoshina");
}
cout<<s<<endl;
}
} | #include <iostream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
string s;
int main(){
getline(cin,s);
while(getline(cin,s)){
int a=s.find("Hoshino");
while(a!=string::npos){
s=s.substr(0,a)+"Hoshina"+s.substr(a+7);
a=s.find("Hoshino");
}
cout<<s<<endl;
}
} | 1 |
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
ll const INF = 1LL << 60;
int main() {
ll N;
cin >> N;
vector<ll> A(N);
for (int i = 0; i < N; i++) {
cin >> A[i];
}
vector<vector<ll>> memo(N + 1, vector<ll>(2));
memo[0][0] = 0;
memo[0][1] = -INF;
for (int i = 0; i < N; i++) {
memo[i + 1][0] = max(memo[i][0] + A[i], memo[i][1] - A[i]);
memo[i + 1][1] = max(memo[i][0] - A[i], memo[i][1] + A[i]);
}
cout << memo[N][0] << endl;
return 0;
} | #include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define repr(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define invrepr(i,a,b) for(int i=b-1;i>=a;i--)
#define invrep(i,n) invrepr(i,0,n)
#define repitr(itr,a) for(auto itr=a.begin();itr!=a.end();++itr)
#define P pair<int,int>
const int MOD=998244353;
const ll INF=2e15;
const double PI=acos(-1);
vector<P> vectorCnt(vector<int> a) {
sort(a.begin(),a.end());
int n=a.size();
vector<pair<int,int>> b;
int cnt=1;
rep(i,n-1) {
if (a[i+1]!=a[i]) {
b.push_back({cnt,a[i]});
cnt=1;
} else {
++cnt;
}
}
b.push_back({cnt,a[n-1]});
return b;
}
int main() {
ios_base::sync_with_stdio(false);
int n;
cin >> n;
vector<ll> a(n);
int cnt=0;
rep(i,n) {
cin >> a[i];
if (a[i]<0) {
++cnt;
a[i]=-a[i];
}
}
sort(a.begin(),a.end());
ll ans=0;
rep(i,n) ans+=a[i];
if (cnt%2==1) ans-=2*a[0];
cout << ans << endl;
return 0;
}
| 1 |
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>
#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<30
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
int main() {
int n;
while(cin >> n && n) {
int a = 0,b = 0;
rep(i,n) {
int x,y;
cin >> x >> y;
if(x == y) {
a += x;
b += y;
} else if(x > y) {
a += (x+y);
} else {
b += (x+y);
}
}
cout << a << " " << b << endl;
}
return 0;
} |
#include <iostream>
using namespace std;
int main()
{
int n, a, b, count = 0, pointX = 0, pointY = 0;
cin >> n;
do {
cin >> a >> b;
if (a > b) {
pointX += (a + b);
}
else if (a < b) {
pointY += (a + b);
}
else {
pointX += a;
pointY += b;
}
count++;
if (count == n) {
cout << pointX << " " << pointY << endl;
pointX = pointY = count = 0;
cin >> n;
}
} while (n != 0);
return 0;
} | 1 |
//Author:xht37
#include <bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define ul unsigned ll
#define ld long double
#define pi pair <int, int>
#define fi first
#define se second
#define mp make_pair
#define ls (p << 1)
#define rs (ls | 1)
#define md ((t[p].l + t[p].r) >> 1)
#define vi vector <int>
#define pb push_back
#define pq priority_queue
#define dbg(x) cerr << #x" = " << x << endl
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define fl(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
using namespace std;
namespace io {
const int SI = 1 << 21 | 1;
char IB[SI], *IS, *IT, OB[SI], *OS = OB, *OT = OS + SI - 1, c, ch[100];
int f, t;
#define gc() (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin), IS == IT ? EOF : *IS++) : *IS++)
inline void flush() {
fwrite(OB, 1, OS - OB, stdout), OS = OB;
}
inline void pc(char x) {
*OS++ = x;
if (OS == OT) flush();
}
template <class I>
inline void rd(I &x) {
for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;
for (x = 0; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + (c & 15), c = gc());
x *= f;
}
template <class I>
inline void rd(I &x, I &y) {
rd(x), rd(y);
}
template <class I>
inline void rd(I &x, I &y, I &z) {
rd(x), rd(y), rd(z);
}
template <class I>
inline void rda(I *a, int n) {
for (int i = 1; i <= n; i++) rd(a[i]);
}
inline void rdc(char &c) {
for (c = gc(); c < 33 || c > 126; c = gc());
}
inline void rds(char *s, int &n) {
for (c = gc(); c < 33 || c > 126; c = gc());
for (n = 0; c >= 33 && c <= 126; s[++n] = c, c = gc());
s[n+1] = '\0';
}
inline void rds(string &s) {
for (c = gc(); c < 33 || c > 126; c = gc());
for (s.clear(); c >= 33 && c <= 126; s.pb(c), c = gc());
}
template <class I>
inline void print(I x, char k = '\n') {
if (!x) pc('0');
if (x < 0) pc('-'), x = -x;
while (x) ch[++t] = x % 10 + '0', x /= 10;
while (t) pc(ch[t--]);
pc(k);
}
template <class I>
inline void print(I x, I y) {
print(x, ' '), print(y);
}
template <class I>
inline void print(I x, I y, I z) {
print(x, ' '), print(y, ' '), print(z);
}
template <class I>
inline void printa(I *a, int n) {
for (int i = 1; i <= n; i++) print(a[i], " \n"[i==n]);
}
inline void printc(char c) {
pc(c);
}
inline void prints(char *s, int n) {
for (int i = 1; i <= n; i++) pc(s[i]);
pc('\n');
}
inline void prints(string s) {
int n = s.length();
while (t < n) pc(s[t++]);
pc('\n'), t = 0;
}
struct Flush {
~Flush() {
flush();
}
} flusher;
}
using io::rd;
using io::rda;
using io::rdc;
using io::rds;
using io::print;
using io::printa;
using io::printc;
using io::prints;
const int N = 1e5 + 7;
int n, x, p[N];
ll b[N], l[N], r[N], s[N], sum;
inline ll calc(int x, ll t) {
return t <= b[x] ? t * l[x] : b[x] * l[x] + (t - b[x]) * r[x];
}
inline ll pd(ll t) {
int w = t % x ? t % x : x, c = (t - w) / x;
ll sum = 0, ret = 0;
for (int i = 1; i <= c; i++) sum += s[p[i]];
for (int i = c + 1; i <= n; i++)
ret = max(ret, sum + calc(p[i], w));
sum += s[p[c+1]];
for (int i = 1; i <= c; i++)
ret = max(ret, sum - s[p[i]] + calc(p[i], w));
return ret;
}
int main() {
rd(n, x);
for (int i = 1; i <= n; i++)
rd(b[i], l[i], r[i]), p[i] = i,
s[i] = b[i] * l[i] + (x - b[i]) * r[i], sum += b[i] * l[i];
sort(p + 1, p + n + 1, [&](int i, int j) { return s[i] > s[j]; });
if (!sum) return print(0), 0;
ll l = 1, r = 1ll * n * x;
while (l < r) {
ll mid = (l + r) >> 1;
if (pd(mid) >= sum) r = mid;
else l = mid + 1;
}
print(l);
return 0;
} | #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long int ll;
pair<ll,int> p[100005];
int main(){
ll n,x; cin >> n >> x;
vector<ll> b(n),l(n),u(n);
for(int i=0;i<n;i++){
cin >> b[i] >> l[i] >> u[i];
}
ll sum=0;
for(int i=0;i<n;i++){
p[i]=pair<ll,int>((x-b[i])*u[i]+b[i]*l[i],i);
sum-=b[i]*l[i];
}
sort(p,p+n);
ll ret=0;//満点を取る教科を数える
int last=0;
for(int i=n-1;i>=0;i--){
if(sum+p[i].first>=0){
ret+=(ll)(n-i-1)*(ll)x;//満点を取る教科はn-i-1個である
last=i+1;
break;
}
sum+=p[i].first;
}
ll Min=x;//中途半端に解く科目の得点の最小値
sum=-sum;
for(int i=0;i<last;i++){
int v=p[i].second;
Min=min(Min,(sum+l[v]-1)/l[v]);//中途半端に解く科目に最小の重みをつけた場合
ll nsum=sum+b[v]*(u[v]-l[v]);//最大の重みをつける場合には更にディスアドバンテージが広がる
Min=min(Min,(nsum+u[v]-1)/u[v]);
}
//貪欲に見ると満点を取る科目になる科目のうち中途半端に解くものを探索する場合、
//満点を取る科目としてi=last-1も追加される
for(int i=last;i<n;i++){
int v=p[i].second;
ll nsum=sum+p[i].first-p[last-1].first;
Min=min(Min,(nsum+l[v]-1)/l[v]);
ll nsum2=nsum+b[v]*(u[v]-l[v]);
Min=min(Min,(nsum2+u[v]-1)/u[v]);
}
cout << ret+Min << endl;
} | 1 |
#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for (int i=0;i<(int)(n);i++)
#define codefor int test;scanf("%d",&test);while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define yn(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YN(ans) if(ans)printf("YES\n");else printf("NO\n")
#define vector1d(type,name,...) vector<type>name(__VA_ARGS__)
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define umap unordered_map
#define uset unordered_set
using namespace std;
using ll = long long;
const int MOD=1000000007;
//入力系
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
//出力系
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
//デバッグ系
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<endl;}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
ll updivide(ll a,ll b){if(a%b==0) return a/b;else return (a/b)+1;}
int main(){
INT(h,w);
vector2d(int,table,h,w);
vector<tuple<int,int,int,int>> ope;
in(table);
rep(y,h){
rep(x,w){
table[y][x]%=2;
if(y<h-1&&table[y][x]==1){
table[y+1][x]++;
ope.push_back(make_tuple(y+1,x+1,y+2,x+1));
}
if(y==h-1&&x+1<w&&table[y][x]==1){
table[y][x+1]++;
ope.push_back(make_tuple(y+1,x+1,y+1,x+2));
}
}
}
out(ope.size());
rep(i,ope.size())out(get<0>(ope[i]),get<1>(ope[i]),get<2>(ope[i]),get<3>(ope[i]));
}
| #include<stdio.h>
#include<string.h>
#define N 8
int mp[N][N],row[N],col[N],dneg[2*N-1],dops[2*N-1];
void init()
{
for(int i=0; i<N; i++)
row[i]=col[i]=0;
for(int i=0; i<2*N-1; i++)
dneg[i]=dops[i]=0;
for(int i=0; i<N; i++)
for(int j=0; j<N; j++)
mp[i][j]=0;
}
void check()
{
for(int i=0; i<N; i++)
for(int j=0; j<N; j++)
if(mp[i][j])
{
if(row[i]!=j)
return ;
}
for(int i=0; i<N; i++)
{
for(int j=0; j<N; j++)
{
printf("%c",(row[i]==j)?'Q':'.');
}
printf("\n");
}
return ;
}
void dfs(int i)
{
if(i==N)
{
check();
return ;
}
for(int j=0; j<N; j++)
{
if(col[j] || dops[i+j] || dneg[i-j+N-1]) continue;
row[i]=j;
col[j]=dops[i+j]=dneg[i-j+N-1]=1;
dfs(i+1);
row[i]=col[j]=dops[i+j]=dneg[i-j+N-1]=0;
}
}
int main()
{
int k,r,c;
while(~scanf("%d",&k))
{
init();
for(int i=0; i<k; i++)
{
scanf("%d%d",&r,&c);
mp[r][c]=1;
}
dfs(0);
}
return 0;
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
void solve3()
{
puts("aa.");
puts("..b");
puts("..b");
}
string mp[10][10];
void init()
{
mp[4][0] = "aabc";
mp[4][1] = "ddbc";
mp[4][2] = "efgg";
mp[4][3] = "efhh";
mp[5][0] = "aabbc";
mp[5][1] = "dee.c";
mp[5][2] = "d.ffd";
mp[5][3] = "eg..d";
mp[5][4] = "eg.cc";
mp[6][0] = "aab.cc";
mp[6][1] = "ccbdd.";
mp[6][2] = "d.aa.b";
mp[6][3] = "d.c..b";
mp[6][4] = ".ac..d";
mp[6][5] = ".a.eed";
mp[7][0] = "aab.c..";
mp[7][1] = "..bdc..";
mp[7][2] = "ee.d.ff";
mp[7][3] = "c.bb..a";
mp[7][4] = "c...eea";
mp[7][5] = ".bcc..d";
mp[7][6] = ".b..aad";
}
string ans[1001];
void fillit(int x, int m)
{
for (int i = x, cnt = 0; cnt < m; i++, cnt++)
{
for (int j = 0; j < m; j++) ans[i][x + j] = mp[m][cnt][j];
}
}
int main()
{
init();
int n; scanf("%d", &n);
if (n == 2) puts("-1");
else if (n == 3) solve3();
else
{
for (int i = 0; i < n; i++)
{
ans[i].resize(n);
fill(ans[i].begin(), ans[i].end(), '.');
}
int a = n / 4, b = n % 4;
a--; b += 4;
int now = 0;
fillit(now, b); now += b;
while (a--) fillit(now, 4), now += 4;
for (int i = 0; i < n; i++) cout << ans[i] << '\n';
}
return 0;
} | #define _USE_MATH_DEFINES
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cfloat>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <string>
#include <set>
using namespace std;
int main(){
int n;
int a,b,c,d;
cin>>n;
vector<int> sum(n+1,0);
vector<int> rank(n+1);
sum[0]=-999999;
for(int i=0;i<n*(n-1)/2;i++){
cin>>a>>b>>c>>d;
if(c>d){
sum[a]+=3;
}
else if(c<d)
sum[b]+=3;
else{
sum[a]++;
sum[b]++;
}
}
int k=0;
int o=0;
for(int i=0;i<n;i++){
vector<int>::iterator it=find(sum.begin(),sum.end(),*max_element(sum.begin(),sum.end()));
int j=it-sum.begin();
if(k==0){
k=1;
o=sum[j];
rank[j]=1;
}
else if(o==sum[j]){
rank[j]=k;
}
else{
k=i+1;
o=sum[j];
rank[j]=k;
}
sum[j]=-999999;
}
for(int i=1;i<=n;i++)
cout<<rank[i]<<endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
const int INF=1e9+7;
const ll LINF=9223372036854775807;
const ll MOD=1e9+7;
const ld PI=acos(-1);
const ld EPS = 1e-10; //微調整用(EPSより小さいと0と判定など)
int ii() { int x; if (scanf("%d", &x)==1) return x; else return 0; }
long long il() { long long x; if (scanf("%lld", &x)==1) return x; else return 0; }
string is() { string x; cin >> x; return x; }
char ic() { char x; cin >> x; return x; }
void oi(int x) { printf("%d ", x); }
void ol(long long x) { printf("%lld ", x); }
void od_nosp(double x) { printf("%.15f", x); } // 古い問題用
void od(double x) { printf("%.15f ", x); }
// long doubleで受け取り、fをLfなどに変えて出力すると、変な数値が出る
// それをなんとかするには独自の出力を作らなければならなそう
void os(const string &s) { printf("%s ", s.c_str()); }
void oc(const char &c) { printf("%c ", c); }
#define o_map(v){cerr << #v << endl; for(const auto& xxx: v){cout << xxx.first << " " << xxx.second << "\n";}} //動作未確認
void br() { putchar('\n'); }
// #define gcd __gcd //llは受け取らない C++17~のgcdと違うので注意
// int lcm(int a, int b){return a / gcd(a, b) * b;}
#define begin_end(a) a.begin(),a.end() //sort(begin_end(vec));
#define REP(i,m,n) for(ll i=(ll)(m) ; i < (ll)(n) ; i++ )
#define DREP(i,m,n) for(ll i=(ll)(m) ; i > (ll)(n) ; i-- )
#define rep(i,n) REP(i,0,n)
#define m_p(a,b) make_pair(a,b)
#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 p_b push_back
#define SZ(x) ((int)(x).size) //size()がunsignedなのでエラー避けに
// coutによるpairの出力(空白区切り)
template<typename T1, typename T2> ostream& operator<<(ostream& s, const pair<T1, T2>& p) {return s << "(" << p.first << " " << p.second << ")";}
// coutによるvectorの出力(空白区切り)
template<typename T> ostream& operator<<(ostream& s, const vector<T>& v) {
int len = v.size();
for (int i = 0; i < len; ++i) {
s << v[i]; if (i < len - 1) s << " "; //"\t"に変えるとTabで見やすく区切る
}
return s;
}
// coutによる多次元vectorの出力(空白区切り)
template<typename T> ostream& operator<<(ostream& s, const vector< vector<T> >& vv) {
int len = vv.size();
for (int i = 0; i < len; ++i) {
s << vv[i] << endl;
}
return s;
}
//最大値、最小値の更新。更新したor等しければtrueを返す
template<typename T>
bool chmax(T& a, T b){return (a = max(a, b)) == b;}
template<typename T>
bool chmin(T& a, T b){return (a = min(a, b)) == b;}
//4近傍(上下左右) rep(i, 2) にすると右・下だけに進む
vector<int> dx_4 = {1, 0, -1, 0};
vector<int> dy_4 = {0, 1, 0, -1};
// -------- template end - //
// - library ------------- //
// --------- library end - //
int main(){
ll n, m, d;
cin >> n >> m >> d;
ld ans;
if (d==0){
ans = ld(m-1) / n;
}
else if (n - d - d <= 0){
ans = ld(m-1) / n; // d==0 のときと結果は同じ
}
else{
// n-2d >= 1
ans = ld((n-d-d)*2 + (d+d)) / n * (m-1) / n;
}
od_nosp(ans);
} | #include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string& s)
{
return '"' + s + '"';
}
string to_string(const char* s)
{
return to_string((string) s);
}
string to_string(bool b)
{
return (b ? "true" : "false");
}
string to_string(vector<bool> v)
{
bool first = true;
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++)
{
if (!first)
{
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template <size_t N>
string to_string(bitset<N> v)
{
string res = "";
for (size_t i = 0; i < N; i++)
{
res += static_cast<char>('0' + v[i]);
}
return res;
}
template <typename A>
string to_string(A v)
{
bool first = true;
string res = "{";
for (const auto &x : v)
{
if (!first)
{
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p)
{
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p)
{
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p)
{
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out()
{
cerr << endl;
}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T)
{
cerr << " " << to_string(H);
debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
const long long INF = 1e18;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<long long> a(n + 1);
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
vector<long long> dp1(n + 1, -INF), dp0(n + 1, -INF);
dp0[1] = a[1];
dp1[1] = -a[1];
for (int i = 2; i < n; i++)
{
dp0[i] = max(dp0[i - 1] + a[i], dp1[i - 1] - a[i]);
dp1[i] = max(dp0[i - 1] - a[i], dp1[i - 1] + a[i]);
}
long long ans = max(dp0[n - 1] + a[n], dp1[n - 1] - a[n]);
cout << ans;
return 0;
}
| 0 |
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#define MOD 1000000007
#define MOD2 998244353
#define int long long
#define double long double
#define EPS 1e-9
//#define PI 3.14159265358979
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
template < typename T >
ostream &operator<<(ostream &os, const vector< T > &A) {
for (int i = 0; i < A.size(); i++)
os << A[i] << " ";
os << endl;
return os;
}
template <>
ostream &operator<<(ostream &os, const vector< vector< int > > &A) {
int N = A.size();
for (int i = 0; i < N; i++) {
for (int j = 0; j < A[i].size(); j++)
os << A[i][j] << " ";
os << endl;
}
return os;
}
template < typename T, typename U >
ostream &operator<<(ostream &os, const pair< T, U > &p) {
os << "(" << p.first << "," << p.second << ")";
return os;
}
typedef pair< int, int > pii;
typedef long long ll;
struct edge {
int from, to, d, c, i;
edge(int _from = 0, int _to = 0, int _d = 0, int _c = 0) {
from = _from;
to = _to;
d = _d;
c = _c;
}
bool operator<(const edge &rhs) const {
return (d == rhs.d) ? (c < rhs.c) : (d < rhs.d);
}
};
struct aabb {
int x1, y1, x2, y2;
aabb(int x1, int y1, int x2, int y2) : x1(x1), y1(y1), x2(x2), y2(y2) {}
};
typedef vector< edge > edges;
typedef vector< edges > graph;
struct flow {
int to, cap, rev, cost;
flow(int to = 0, int cap = 0, int rev = 0, int cost = 0) : to(to), cap(cap), rev(rev), cost(cost) {}
};
typedef vector< vector< flow > > flows;
const int di[4] = {0, -1, 0, 1};
const int dj[4] = {-1, 0, 1, 0};
const int ci[5] = {0, 0, -1, 0, 1};
const int cj[5] = {0, -1, 0, 1, 0};
const ll LINF = LLONG_MAX / 2;
const int INF = INT_MAX / 2;
const double PI = acos(-1);
int pow2(int n) { return 1LL << n; }
template < typename T, typename U >
bool chmin(T &x, const U &y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template < typename T, typename U >
bool chmax(T &x, const U &y) {
if (x < y) {
x = y;
return true;
}
return false;
}
template < typename A, size_t N, typename T >
void Fill(A (&array)[N], const T &val) {
fill((T *)array, (T *)(array + N), val);
}
struct initializer {
initializer() {
cout << fixed << setprecision(20);
}
};
initializer _____;
bool BMdfs(vector< int > &M, vector< vector< int > > &G, vector< bool > &used, int v) {
used[v] = true;
rep(i, G[v].size()) {
int u = G[v][i], w = M[u];
if (w < 0 || !used[w] && BMdfs(M, G, used, w)) {
M[v] = u;
M[u] = v;
return true;
}
}
return false;
}
int BMatching(vector< vector< int > > &G, vector< int > &M) {
vector< bool > used(G.size());
M.assign(G.size(), -1);
int ret = 0;
rep(v, G.size()) {
if (M[v] < 0) {
used.assign(G.size(), false);
if (BMdfs(M, G, used, v)) {
ret++;
}
}
}
return ret;
}
int BMatching(vector< vector< int > > &G) {
vector< int > M;
return BMatching(G, M);
}
int N, M, K, T, Q, H, W;
signed main() {
cin >> N;
vector< int > A(N);
rep(i, N) cin >> A[i];
vector< int > B(N);
rep(i, N) cin >> B[i];
int j = 0;
rep(i, N) {
if (A[i] == B[i]) {
while (j < N && (A[i] == A[j] || A[i] == B[j]))
j++;
if (j == N) {
cout << "No" << endl;
return 0;
}
swap(B[i], B[j]);
}
if (i < N - 1 && A[i] != A[i + 1])
j = 0;
}
cout << "Yes" << endl;
cout << B;
return 0;
} | //============================================================================
// Name : JOI.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include<algorithm>
#include<cstdlib>
using namespace std;
int A,B,C,D,E,F;
int X[10001],Y[10001];
int main() {
cin>>A>>B>>C>>D>>E>>F;
int S,T;
S=A;
T=E;
if(S>B)S=B;
if(S>C)S=C;
if(S>D)S=D;
if(T>F)T=F;
int R=A+B+C+D+E+F;
cout<<R-S-T<<endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
signed main() {
int n;
string s;
cin >> n >> s;
vector<int> R, G, B;
rep(i,n) {
if(s[i] == 'R') R.push_back(i);
if(s[i] == 'G') G.push_back(i);
if(s[i] == 'B') B.push_back(i);
}
long long ans = 0;
for (auto r : R) {
for (auto g : G) {
int x = -1, y = -1, z = -1;
int mi = min(r,g);
int ma = max(r,g);
if((r + g)%2 == 0) x = (r + g)/2;
y = ma + abs(r - g);
z = mi - abs(r - g);
int add = B.size();
if(binary_search(B.begin(),B.end(),x)) add--;
if(binary_search(B.begin(),B.end(),y)) add--;
if(binary_search(B.begin(),B.end(),z)) add--;
ans += add;
}
}
cout << ans << '\n';
} | #include<cstdio>
using namespace std;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
char s[400][401];
int par[160000];
int c1[160000];
int c2[160000];
int find(int x) {
if (par[x] == x) return x;
return par[x] = find(par[x]);
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y) return;
par[x] = y;
}
int main() {
int h, w, i, j, k;
long long ans = 0;
scanf("%d %d", &h, &w);
for (i = 0; i < h; i++) scanf("%s", s[i]);
for (i = 0; i < h * w; i++) par[i] = i;
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
for (k = 0; k < 4; k++) {
int x = i + dx[k];
int y = j + dy[k];
if (x < 0 || x >= h || y < 0 || y >= w) continue;
if (s[i][j] != s[x][y]) unite(i * w + j, x * w + y);
}
}
}
for (i = 0; i < h; i++) {
for (j = 0; j < w; j++) {
if (s[i][j] == '.') {
c1[find(i * w + j)]++;
} else {
c2[find(i * w + j)]++;
}
}
}
for (i = 0; i < h * w; i++) ans += (long long)c1[i] * c2[i];
printf("%lld\n", ans);
return 0;
}
| 0 |
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int n;
int area[11][11];
while(cin >> n){
if(n == 0) break;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cin >> area[i][j];
}
}
for(int i = 0; i < n; i++){
int count = 0;
for(int j = 0; j < n; j++){
count += area[i][j];
}
area[i][n] = count;
}
for(int j = 0; j <= n; j++){
int count = 0;
for(int i = 0; i < n; i++){
count += area[i][j];
}
area[n][j] = count;
}
for(int i = 0; i <= n; i++){
for(int j = 0; j <= n; j++){
cout << setw(5) << area[i][j];
}
cout << endl;
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,int> plli;
typedef pair<int,pii> pipii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;
#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n);i>0;i--)
#define rrep2(i,a,b) for (int i=(a);i>b;i--)
#define pb push_back
#define fi first
#define se second
#define all(a) (a).begin(),(a).end()
const ll mod = 1e9 + 7;
const ll INF = 1<<30;
const int dx4[4] = {1, 0, -1, 0};
const int dy4[4] = {0, 1, 0, -1};
const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1};
const double pi = 3.141592653589793;
signed main(){
cin.tie(0);
ios::sync_with_stdio(false);
int a, b, c;
cin >> a >> b >> c;
while (a != 0) {
int n;
cin >> n;
int ans[a + b + c + 1];
fill(ans, ans + a + b + c, 2);
int ii[n] = {}; int jj[n] = {}; int kk[n] = {}; int rr[n] = {};
rep(m, n) {
int i, j, k, r;
cin >> i >> j >> k >> r;
ii[m] = i; jj[m] = j; kk[m] = k; rr[m] = r;
if (r == 1) {
ans[i] = 1;
ans[j] = 1;
ans[k] = 1;
}
else {
if (ans[i] == 1 && ans[j] == 1) ans[k] = 0;
else if (ans[i] == 1 && ans[k] == 1) ans[j] = 0;
else if (ans[j] == 1 && ans[k] == 1) ans[i] = 0;
}
}
rep(m, n) {
int i = ii[m]; int j = jj[m]; int k = kk[m]; int r = rr[m];
if (r == 1) {
ans[i] = 1;
ans[j] = 1;
ans[k] = 1;
}
else {
if (ans[i] == 1 && ans[j] == 1) ans[k] = 0;
else if (ans[i] == 1 && ans[k] == 1) ans[j] = 0;
else if (ans[j] == 1 && ans[k] == 1) ans[i] = 0;
}
}
rep2(i, 1, a + b + c + 1) cout << ans[i] << endl;
cin >> a >> b >> c;
}
} | 0 |
#include<bits/stdc++.h>
#include<math.h>
using namespace std;
/*------------------------------------------------------------------*/
#define ll long long int
#define mp make_pair
#define pb push_back
#define MOD 1000000007
bool isvowel(char c)
{
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u')
return true;
return false;
}
ll isprime(ll n)
{
ll ans=1;
for(ll i=2;i<=sqrt(n);i++)
{
if(n%i==0)
ans = i;
}
return ans;
}
ll power(ll a,ll b,ll m)
{
if(b==0)
return 1;
if(b==1)
return a%m;
ll temp=power(a,b/2,m);
if(b%2==0)
return ((temp%m)*(temp%m))%m;
else
return ((((temp%m)*(temp%m))%m)*a%m)%m;
}
/*-----------------------------------------------------------------------------*/
ll c(ll n, ll k)
{
ll C[n + 1][k + 1];
ll i, j;
for (i = 0; i <= n; i++)
{
for (j = 0; j <= min(i, k); j++)
{
if (j == 0 || j == i)
C[i][j] = 1;
else
C[i][j] = (C[i - 1][j - 1]%MOD +
C[i - 1][j])%MOD;
}
}
return C[n][k]%MOD;
}
ll modInverse(ll a, ll m)
{
ll m0 = m;
ll y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1)
{
ll q = a / m;
ll t = m;
m = a % m, a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0)
x += m0;
return x;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//freopen("1.txt","rt",stdin);
//freopen("output.txt","wwt",stdout);
ll t;
t=1;
//cin>>t;
//ll t1=0;
while(t--)
{
ll n,m;
cin>>n>>m;
ll p[m];
string s[m];
map<ll,ll>m1;
ll ans1=0,ans2=0;
for(ll i=0;i<m;i++)
{
cin>>p[i]>>s[i];
if(s[i]=="AC")
{
if(m1[p[i]]!=-1)
{
ans2+=m1[p[i]];
m1[p[i]]=-1;
ans1++;
}
}
else
{
if(m1[p[i]]!=-1)
m1[p[i]]++;
}
}
cout<<ans1<<" "<<ans2<<endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 10;
int n; ll k, ans, a[maxn], lef[maxn], rig[maxn];
int main() {
scanf("%d %lld", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%lld", a + i), ans += a[i];
}
ans += (n - 1) * k;
lef[1] = a[1];
for (int i = 2; i <= n; i++) {
lef[i] = min(a[i], lef[i - 1] + k);
}
rig[n] = a[n];
for (int i = n - 1; i; i--) {
rig[i] = min(a[i], rig[i + 1] + k);
}
for (int i = 2; i < n; i++) {
ans += min(lef[i], rig[i]);
}
printf("%lld", ans);
return 0;
} | 0 |
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <typeinfo>
#include <map>
using namespace std;
typedef long long LL;
#define REP(i,N) for(int i = 0; i < (int)N; i++)
int dy[4] = {1,0,-1,0};
int dx[4] = {0,1,0,-1};
bool gems[25][25];
vector<bool> hantei;
int main(){
int n;
cin >> n;
while(n!=0){
REP(i,25)memset(gems[i],0,sizeof(gems[i]));
REP(i,n){
int x,y;
cin >> x >> y;
gems[x][y] = true;
}
int ret=0;
int m;
cin >> m;
int nowx,nowy;
nowx = nowy = 10;
REP(i,m){
char c;
int nor;
cin >> c >> nor;
int dir;
switch(c){
case 'N':
dir = 0;
break;
case 'E':
dir = 1;
break;
case 'S':
dir = 2;
break;
case 'W':
dir = 3;
break;
}
REP(j,nor){
nowx += dx[dir];
nowy += dy[dir];
if(gems[nowx][nowy]){
ret++;
gems[nowx][nowy] = false;
}
}
}
if(ret == n){
hantei.push_back(1);
}else{
hantei.push_back(0);
}
cin >> n;
}
REP(i,hantei.size()){
string dis = hantei[i] ? "Yes":"No";
cout << dis << endl;
}
return 0;
} |
#include<iostream>
#include<string>
#include<algorithm>
#include<sstream>
using namespace std;
static const int MAX_N = 12;
int field[MAX_N][MAX_N];
static const int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 };
int stoi(std::string str) {
int ret;
std::stringstream ss;
ss << str;
ss >> ret;
return ret;
}
void dfs(int y, int x) {
field[y][x] = 0;
for (int i = 0; i < 4; i++) {
if (0 <= x + dx[i] && x + dx[i] < 12 && 0 <= y + dy[i] && y + dy[i] < 12 && field[y + dy[i]][x + dx[i]] == 1) {
dfs(y + dy[i], x + dx[i]);
}
}
}
void printTable() {
cout << "------------------" << endl;
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 12; j++) {
cout << field[i][j];
}
cout << endl;
}
}
int main()
{
string s = "";
while (cin >> s) {
int cnt = 0;
if (s == "")break;
//input
for (int i = 0; i < 12; i++)field[0][i] = s[i] - '0';// stoi(s[i]);
for (int i = 1; i < 12; i++) {
cin >> s;
for (int j = 0; j < 12; j++) {
field[i][j] = s[j] - '0';// stoi(s[j]);
}
}
//printTable();
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 12; j++) {
if (field[i][j] == 1) {
cnt++;
dfs(i, j);
// printTable();
}
}
}
cout << cnt << endl;
}
return 0;
} | 0 |
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
/* a と b の最大公約数を返す関数 */
long long GCD(long long a, long long b) {
if (b == 0) return a;
else return GCD(b, a % b);
}
int main()
{
int N;
string S;
cin >> N >> S;
vector<vector<int>> su(2, vector<int>((unsigned int)(N + 100), 0));
su.at(0).at(0) = 0;
su.at(1).at(0) = 0;
for (int i = 0; i < N; ++i)
{
if (S.at(i) == 'W')
{
su.at(0).at(i + 1) = su.at(0).at(i) + 1;
su.at(1).at(i + 1) = su.at(1).at(i);
}
else
{
su.at(0).at(i + 1) = su.at(0).at(i);
su.at(1).at(i + 1) = su.at(1).at(i) + 1;
}
}
int ret = (int)(3 * 1e5);
int l_min;
rep(i, N)
{
l_min = (su.at(0).at(i)) + (su.at(1).at(N) - su.at(1).at(i+1));
ret = min(l_min, ret);
}
cout << ret << endl;
return 0;
} | #include<iostream>
using namespace std;
int N,M,A[1001],B,V[1001];
int main(int i,int j){
cin>>N>>M;
for(;i<=N+M;i++){
cin>>((i<=N)?A[i]:B);
for(j=1;j<=N;j++){
if(i>N&&B>=A[j]){
V[j]++;break;
}
}
}
int max=0;
for(i=1;i<=N;i++){
if(max<V[i]){
max=V[i];j=i;
}
}
cout<<j<<endl;
return 0;
} | 0 |
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_INPUT_SIZE 1024
void trace(int num, int* array);
int bubbleSort(int num, int* array);
int
main(int argc, char* argv[])
{
char input[MAX_INPUT_SIZE];
int num = atoi(fgets(input, sizeof(input), stdin));
int* array = new int[num];
atoi(fgets(input, sizeof(input), stdin));
for (int i = 0; i < num; i++) {
array[i] = i == 0 ?
atoi(strtok(input, " ")) : atoi(strtok(NULL, " "));
//printf("%d\n", array[i]);
}
printf("%d\n", bubbleSort(num, array));
if (array != NULL) {
delete [] array;
}
return 0;
}
void
trace(int num, int* array)
{
for (int i = 0; i < num; i++) {
if (i != 0) {
printf(" ");
}
printf("%d", array[i]);
}
printf("\n");
}
int
bubbleSort(int num, int* array)
{
int ret = 0, loopCount = 0;
for (int j = 0; j < num; j++) {
for (int i = num-1; i > 0; i--) {
if (array[i] < array[i-1]) {
int tmp = array[i];
array[i] = array[i-1];
array[i-1] = tmp;
ret++;
}
}
}
/*
for (int i = 0; i < num; i++) {
printf("%d\n", array[i]);
}
*/
trace(num, array);
return ret;
} | #include<iostream>
using namespace std;
int main()
{
int flag = 1, tmp, n, a[100], count = 0;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
while (flag)
{
flag = 0;
for (int i = n - 1; i > 0; i--)
if (a[i] < a[i - 1])
{
tmp = a[i];
a[i] = a[i - 1];
a[i - 1] = tmp;
count++;
flag = 1;
}
}
for (int i = 0; i < n; i++)
{
if (i)
cout << " ";
cout << a[i];
}
cout << endl << count << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> P;
typedef tuple<long double,ll,ll> T;
const ll mod=1e9+7,INF=mod*mod*3;//M_PI
#define rep(i,N) for(ll i=0; i<(N); i++)
#define rep1(i,N) for(ll i=1; i<(N); i++)
#define f first
#define s second
#define pb push_back
#define mp make_pair
ll N,M,Q;
vector<ll> V[505];
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
cin>>N>>M>>Q;
rep(i,M){
ll L,R;
cin>>L>>R;
V[L].pb(R);
}
rep1(i,N+1){
if(V[i].size()) sort(V[i].begin(),V[i].end());
}
rep(i,Q){
ll ans=0;
ll p,q;
cin>>p>>q;
for(ll j=p;j<=q;j++){
ll sz=V[j].size();
ll k=upper_bound(V[j].begin(),V[j].end(),q)-V[j].begin();
ans+=k;
}
cout<<ans<<endl;
}
}
| #include "bits/stdc++.h"
#define ll long long
#define lld long double
#define MOD 1000000007
#define inf 1000000007
#define pii pair<int,int>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
// ll power(ll x,ll y, ll md=inf){ll res = 1;x%=md;while(y){if(y&1)res = (res*x)%md;x *= x; if(x>=md) x %= md; y >>= 1;}return res;}
using namespace std;
#define endl '\n'
// #define int ll
signed main() {
fast;
int h,w;
cin>>h>>w;
pii v[200002];
for(int i=0;i<h;i++) cin>>v[i].f>>v[i].s;
map<int,int> mp;
for(int i=1;i<=w;i++) mp[i] = 0;
map<int,int> mini = {{0,w}};
for(int i=0;i<h;i++) {
int r = v[i].s;
auto it = mp.lower_bound(v[i].f);
if(r==w) {
while(it!=mp.end() && it->f <= r) {
auto it1 = it++;
auto it2 = mini.find(it1->s);
if(it2->s==1) mini.erase(it2);
else it2->s --;
mp.erase(it1);
}
if(mini.size()==0) cout<<-1<<endl;
else cout<<mini.begin()->f+i+1<<endl;
}
else {
int mn = inf;
while(it!=mp.end() && it->f <= r) {
mn = min(mn, r+1-(it->f)+(it->s));
auto it1 = it++;
auto it2 = mini.find(it1->s);
if(it2->s==1) mini.erase(it2);
else it2->s --;
mp.erase(it1);
}
if(mn<inf) {
if(it!=mp.end() && it->f == r + 1) {
if(it->s > mn) {
auto it1 = mini.find(it->s);
if(it1->s ==1) mini.erase(it1);
else it1->s --;
mini[mn]++;
it->s = mn;
}
}
else {
mp.emplace_hint(it,r+1,mn);
mini[mn]++;
}
cout<<mini.begin()->f+i+1<<endl;
}
else {
if(mini.size()==0) cout<<-1<<endl;
else cout<<mini.begin()->f+i+1<<endl;
}
}
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i=0; i<(int)(n); i++)
int main(){
int H, W, ans=1; cin >> H >> W; string A[H];
REP(i, H){ cin >> A[i]; }
int B[H][W]={}; vector< pair<int, int> > C;
REP(i, H){ REP(j, W){
if(A[i][j]=='#'){ B[i][j]=1; C.push_back(make_pair(i, j)); }
} }
REP(i, H*W){
if((int)C.size()==H*W){ cout << ans-1 << "\n"; return 0; }
int a=C[i].first, b=C[i].second, c=B[a][b]+1;
if(a>0&&!B[a-1][b]){ B[a-1][b]=c; C.push_back(make_pair(a-1, b)); }
if(b>0&&!B[a][b-1]){ B[a][b-1]=c; C.push_back(make_pair(a, b-1)); }
if(a<H-1&&!B[a+1][b]){ B[a+1][b]=c; C.push_back(make_pair(a+1, b)); }
if(b<W-1&&!B[a][b+1]){ B[a][b+1]=c; C.push_back(make_pair(a, b+1)); }
ans=c;
}
} | #include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
const int N = 1010;
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
int _w;
int n, m;
char str[N][N];
int dis[N][N];
queue<pii> q;
bool inside( int x, int y ) {
return x >= 1 && x <= n && y >= 1 && y <= m;
}
int main() {
_w = scanf( "%d%d", &n, &m );
for( int i = 1; i <= n; ++i )
_w = scanf( "%s", str[i]+1 );
memset(dis, -1, sizeof dis);
for( int i = 1; i <= n; ++i )
for( int j = 1; j <= m; ++j )
if( str[i][j] == '#' )
dis[i][j] = 0, q.push( pii(i, j) );
int ans = 0;
while( !q.empty() ) {
pii now = q.front(); q.pop();
ans = max(ans, dis[now.first][now.second]);
for( int i = 0; i < 4; ++i ) {
int nx = now.first + dx[i];
int ny = now.second + dy[i];
if( inside(nx, ny) && dis[nx][ny] == -1 ) {
dis[nx][ny] = dis[now.first][now.second] + 1;
q.push( pii(nx, ny) );
}
}
}
cout << ans << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
ll modpow(ll a,ll n,ll mod){
if(n == 0) return 1LL;
if(n % 2 == 0){
ll d = modpow(a,n/2,mod) % mod;
return (d*d) % mod;
}
return (a % mod) * (modpow(a,n-1,mod) % mod) % mod;
}
int main(){
ll n; cin >> n;
vector<ll> a(n);
for(int i = 0; i < n; i++) cin >> a[i];
ll sum = 0;
ll times = 0;
while(times < 65){
ll cnt1 = 0,cnt0 = 0;
ll b_sum = 0;
for(int i = 0; i < n; i++){
if(a[i] % 2 == 1) cnt1++;
a[i]/=2;
}
cnt0 = n-cnt1;
b_sum = cnt0*cnt1%MOD;
ll res = modpow(2,times,MOD)*b_sum;
sum += res%MOD;
sum %= MOD;
times++;
}
cout << sum << endl;
return 0;
}
| #include <stdio.h>
#include <iostream>
#include <vector>
#include <array>
constexpr int64_t MOD = 1'000'000'007;
constexpr int64_t help = 1;
int main() {
int64_t n;
std::cin >> n;
std::array<uint64_t, 62> bits{};
for (int64_t i = 0; i < n; i++) {
int64_t d;
std::cin >> d;
for (int64_t b = 0; b < 62; b++) {
bits[b] += !!(d & (help << b));
}
}
int64_t ans = 0;
for (int64_t b = 0; b < 62; b++) {
int64_t p = (help << b) % MOD;
ans += (bits[b]*(n-bits[b]) % MOD) * p;
ans %= MOD;
}
std::cout << 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 1001001001
#define mod 1000000007
#define pi 3.141592653589793
typedef vector<int> vi;
typedef vector<ll> vl;
ll gcd(ll a,ll b){
if(b>a) swap(a,b);
if(b==0) return a;
return gcd(b,a%b);
}
ll lcm(ll a,ll b){
ll g=gcd(a,b);
return a/g*b;
}
ll rep_jijo(ll n,ll x){
if(x==0) return 1;
if(x%2==0){
ll t=rep_jijo(n,x/2);
return t*t%mod;
}
return n*rep_jijo(n,x-1)%mod;
}
ll com(ll n,ll r){
ll x=1,y=1;
ll tmp=n;
while(tmp>=n-r+1){
x=x*tmp%mod;
tmp--;
}
while(r>0){
y=y*r%mod;
r--;
}
return x*rep_jijo(y,mod-2)%mod;
}
int main (){
int n;
cin >> n;
double mn=inf;
double sum=0;
int ans;
vector<double>a(n);
rep(i,n){
double x;
cin >> x;
a[i]=x;
sum+=x;
}
sum/=n;
rep(i,n){
double sub=abs(a[i]-sum);
if(sub<mn) {
mn=sub;
ans=i;
}
}
cout << ans << endl;
return 0;
}
| #include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
#include <stdio.h>
#include <iomanip>
#define _USE_MATH_DEFINES
#include <math.h>
#include <numeric>
//#include <map>
using namespace std;
int main()
{
int n;
cin >> n;
vector<int> vData;
for (int i = 0; i < n; i++)
{
int t;
cin >> t;
vData.push_back(t);
}
reverse(vData.begin(), vData.end());
for (int i = 0; i < vData.size(); i++)
{
cout << vData.at(i);
if (i == vData.size() - 1)break;
cout << " ";
}
cout << endl;
return 0;
} | 0 |
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
int N, T;
cin >> N >> T;
vector<int> A(N), B(N);
for(int i = 0; i < N; i++) cin >> A[i] >> B[i];
vector<int> ind(N);
for(int i = 0; i < N; i++) ind[i] = i;
sort(ind.begin(), ind.end(), [&](int i, int j){
return A[i] < A[j];
});
vector<vector<int>> dp(N + 1, vector<int> (T + 1, 0));
for(int k = 0; k < N; k++){
int i = ind[k];
for(int j = 0; j <= T; j++){
dp[k + 1][j] = max(dp[k + 1][j], dp[k][j]);
if(j == T) continue;
if(j + A[i] > T) dp[k + 1][T] = max(dp[k + 1][T], dp[k][j] + B[i]);
else dp[k + 1][j + A[i]] = max(dp[k + 1][j + A[i]], dp[k][j] + B[i]);
}
}
cout << dp[N][T] << endl;
} | #include<bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < n; i++)
#define L first
#define R second
using namespace std;
int qmin(int a, int b){
return (a < b) ? a : b;
}
int qmax(int a, int b){
return (a > b) ? a : b;
}
int n;
pair<int, int> seg[100100];
int len(int l, int r){
return qmax(r-l+1, 0);
}
int main(){
scanf("%d", &n);
rep(i, n) scanf("%d%d", &seg[i].L, &seg[i].R);
int lmax = seg[0].L, lmax_pos = 0, rmin_2nd = seg[0].R;
int rmin = seg[0].R, rmin_pos = 0;
rep(i, n){
if(seg[i].L > lmax){
lmax = seg[i].L;
lmax_pos = i;
rmin_2nd = seg[i].R;
}
if(seg[i].L == lmax) lmax_pos = -1;
if(seg[i].R < rmin){
rmin = seg[i].R;
rmin_pos = i;
}
if(seg[i].R == rmin) rmin_pos = -1;
}
int ans = len(lmax, rmin) + len(seg[0].L, seg[0].R);
rep(i, n){
ans = qmax(ans, len(lmax, rmin) + len(seg[i].L, seg[i].R));
}
if(lmax_pos < 0 || lmax_pos != rmin_pos){
sort(seg, seg+n);
for(int i = n-1; i > 0; i--){
rmin_2nd = qmin(rmin_2nd, seg[i].R);
ans = qmax(ans, len(lmax, rmin_2nd) + len(seg[i-1].L, rmin));
}
}
cout << ans << endl;
return 0;
} | 0 |
#include <cstdio>
#include <algorithm>
#include <vector>
#define file(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
inline int read()
{
int data = 0, w = 1; char ch = getchar();
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') w = -1, ch = getchar();
while (ch >= '0' && ch <= '9') data = data * 10 + (ch ^ 48), ch = getchar();
return data * w;
}
const int N(2e5 + 10), Mod(1e9 + 7);
int fac[N], inv[N], f[N], n, m, A, B;
int C(int n, int m) { return 1ll * fac[n + m] * inv[m] % Mod * inv[n] % Mod; }
int fastpow(int x, int y)
{
int ans = 1;
for (; y; y >>= 1, x = 1ll * x * x % Mod)
if (y & 1) ans = 1ll * ans * x % Mod;
return ans;
}
int main()
{
n = read() - 1, m = read() - 1, A = read() - 1, B = read() - 1, fac[0] = inv[0] = 1;
if (A == n || B == m) return puts("0"), 0;
for (int i = 1; i <= n + m; i++) fac[i] = 1ll * fac[i - 1] * i % Mod;
inv[n + m] = fastpow(fac[n + m], Mod - 2);
for (int i = n + m - 1; i; i--) inv[i] = 1ll * inv[i + 1] * (i + 1) % Mod;
int ans = C(n, m);
for (int i = 0; i <= A; i++)
ans = (ans - 1ll * C(i, m - B - 1) * C(n - i, B) % Mod + Mod) % Mod;
printf("%d\n", ans);
return 0;
}
| #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long ll;
const ll MAXN = 2e5+10;
const ll MOD = 1e9+7;
ll H, W, A, B, ans = 0, fac[MAXN], inv[MAXN];
int main() {
scanf("%lld%lld%lld%lld", &H, &W, &A, &B);
inv[1] = inv[0] = 1;
fac[1] = fac[0] = 1;
for (ll i = 2; i <= MAXN - 10; i++) {
inv[i] = inv[MOD % i] * (MOD - MOD / i) % MOD;
fac[i] = fac[i-1] * i % MOD;
}
for (ll i = 2; i <= MAXN - 10; i++) {
inv[i] = inv[i-1] * inv[i] % MOD;
}
for (ll i = 1; i <= H-A; i++) {
ll tem = (B - 1) + (i - 1);
ll tql = (W - B - 1) + (H - i);
ll len = W - B - 1;
ll now = ((((fac[tem] * inv[i-1] % MOD) * inv[tem-(i-1)] % MOD) % MOD) * (((fac[tql] * inv[len] % MOD) * inv[tql - len] % MOD) % MOD)) % MOD;
ans = (ans + now) % MOD;
}
printf("%lld\n", ans % MOD);
return 0;
} | 1 |
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define all(x) (x).begin(),(x).end()
#define cyes cout<<"YES"<<endl
#define cno cout<<"NO"<<endl
#define sp <<" "<<
#define cst(x) cout<<fixed<<setprecision(x)
#define pi 3.14159265359
#define mod 1000000007
struct edge {
int to;
int weight;
edge(int t, int w) : to(t),weight(w) {}
};
using namespace std;
using ll = long long;
using ld = long double;
using Graph = vector<vector<int>>;
using que_a = priority_queue<int, vector<int>, greater<int> >;
using que_d = priority_queue<int>;
using pint = pair<int,int>;
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; }
int main(){
int n; cin >> n;
ll ans = -1;
int pre = -1;
bool ok = true;
rep(i,n){
int a; cin >> a;
if(a > pre+1) ok = false;
else if(a == pre+1) ans++;
else ans += a;
pre = a;
}
ok? cout << ans << endl : cout << -1 << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[214514];
int main() {
ll n;
cin>>n;
for(int i=0;i<n;i++)cin>>a[i];
ll ans=0;
if(a[0]!=0)cout<<-1<<endl;
else{
ll cnt=1;
for(int i=1;i<=n;i++){
if(a[i]==a[i-1])cnt++;
else if(a[i]>a[i-1]){
if(a[i]-a[i-1]>1){
cout<<-1<<endl;
return 0;
}
ans+=(cnt-1)*a[i-1];
cnt=1;
}else{
ans+=cnt*a[i-1];
cnt=1;
}
}
cout<<ans<<endl;
}
return 0;
}
| 1 |
#include <iostream>
using namespace std;
int main(){
int A,B,C;
cin >> A >> B >> C;
if (A != B && A != C && B != C) {
cout << "No" << endl;
}
else if (A == B && B == C) {
cout << "No" << endl;
}
else if (A == B && A != C) {
cout << "Yes" << endl;
}
else if (A == C && A != B) {
cout << "Yes" << endl;
}
else if (C == B && A != C) {
cout << "Yes" << endl;
}
}
| // GRL_1_A
#include <iostream>
#include <vector>
#include <queue>
#include <math.h>
#define N 1000000000
using namespace std;
struct edge{
int adj;
int dis;
};
int main(){
int V, E, r, s, t, d, x;
vector<vector<edge> > A;
vector<int> distance;
queue<int> ver;
edge e;
cin>>V>>E>>r;
A.resize(V);
for(int i=0;i<V;i++){
distance.push_back(N);
}
distance[r] = 0;
for(int j=0;j<E;j++){
cin>>s>>t>>d;
e.adj = t;
e.dis = d;
A[s].push_back(e);
}
ver.push(r);
while(!ver.empty()){
x = ver.front();
for(int i=0;i<A[x].size();i++){
if(distance[A[x][i].adj] > distance[x] + A[x][i].dis){
distance[A[x][i].adj] = distance[x] + A[x][i].dis;
ver.push(A[x][i].adj);
}
}
ver.pop();
}
for(int i=0;i<V;i++){
if(distance[i] >= N)
cout<<"INF"<<endl;
else
cout<<distance[i]<<endl;
}
return 0;
} | 0 |
// C++ 14
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define loop(__x, __start, __end) for(ll __x = __start; __x < __end; __x++)
typedef pair<int, int> Edge;
int n;
vector<Edge> G[100000];
vector<ll> D(100000, 0);
int main() {
cin >> n;
loop(i,0,n-1) {
int a, b, c; cin >> a >> b >> c;
a--;b--;
G[a].emplace_back(b, c);
G[b].emplace_back(a, c);
}
int q, k; cin >> q >> k; k--;
queue<pair<int, int> > Q;
Q.push({ k, -1 });
D[k] = 0;
while (Q.size()) {
auto _v = Q.front(); Q.pop();
int v = _v.first, p = _v.second;
for (auto &&edge: G[v]) {
int u = edge.first, w = edge.second;
if (u == p) continue;
D[u] = D[v] + w;
Q.push({ u, v });
}
}
while (q--) {
int x, y; cin >> x >> y; x--;y--;
cout << D[x] + D[y] << endl;
}
return 0;
}
| #include<cstdio>
#include<cstring>
#include<algorithm>
#include<functional>
#include<iostream>
#include<queue>
#define rep(i,a,b) for(i=a;i<b;i++)
#define F1 first.first
#define F2 first.second
#define F3 second
#define INF 2100000000
#define long long int int
using namespace std;
typedef pair<pair<int,int>,int> P1;
typedef pair<int,int> P2;
int main(void)
{
priority_queue<P2,vector<P2>,greater<P2> > que;
int i,j,k,n,m,x,s,e;
int a,b,cos,tim,st,en,way;
int leng[1001],kami[1002];
P1 glid1[6000],glid2[6000];
P2 tori;
while(1) {
cin>>n>>m;
if(n==0&&m==0) break;
rep(i,0,n) {
cin>>a>>b>>cos>>tim;
glid1[i*2].F1=a,glid1[i*2].F2=b,glid1[i*2].F3=cos;
glid2[i*2].F1=a,glid2[i*2].F2=b,glid2[i*2].F3=tim;
glid1[i*2+1].F1=b,glid1[i*2+1].F2=a,glid1[i*2+1].F3=cos;
glid2[i*2+1].F1=b,glid2[i*2+1].F2=a,glid2[i*2+1].F3=tim;
}
sort(glid1,glid1+n*2);sort(glid2,glid2+n*2);
kami[1]=0;
rep(i,1,n*2) {
if(glid1[i].F1!=glid1[i-1].F1) {
x=glid1[i].F1;
kami[x]=i;
}
}
kami[m+1]=n*2;
cin>>k;
rep(i,0,k) {
cin>>st>>en>>way;
for(j=1;j<=m;j++) leng[j]=INF;
while(!que.empty()) que.pop();
que.push(P2(0,st));
while(!que.empty()) {
tori=que.top(),que.pop();
a=tori.first,b=tori.second;
if(a<leng[b]) {
leng[b]=a;
if(b==en) break;
s=kami[b],e=kami[b+1];
if(way==0)rep(j,s,e) que.push(P2(a+glid1[j].F3,glid1[j].F2));
else rep(j,s,e) que.push(P2(a+glid2[j].F3,glid2[j].F2));
}
}
cout<<leng[en]<<endl;
}
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> ii;
struct fastio {
fastio() {
ios::sync_with_stdio(false);
cout << setprecision(10) << fixed;
cin.tie(0);
}
};
fastio _fast_io;
const int H = 105;
const int W = 10;
const int MOD = 1e9 + 7;
int h, w, k;
ll dp[H][W];
ll C[W];
void init() {
C[1] = 2;
int a = 1, b = 1;
for (int i = 2; i < w; ++i) {
int na = b;
int nb = a + b;
C[i] = na + nb;
a = na;
b = nb;
}
}
ll cnt(int x) {
if (x <= 0) return 1;
return C[x];
}
int main() {
cin >> h >> w >> k;
init();
for (int i = 0; i < w; ++i) {
if (i == k - 1)
dp[h][i] = 1;
else
dp[h][i] = 0;
}
// cout << dp[15][4] << endl;
// dp[0][0] => dp[h - 1][k - 1]
for (int i = h - 1; i >= 0; --i) {
for (int j = 0; j < w; ++j) {
dp[i][j] = dp[i + 1][j] * cnt(j - 1) % MOD * cnt(w - j - 2) % MOD; // 0 j 0
if (j > 0)
dp[i][j] = (dp[i][j] + dp[i + 1][j - 1] * cnt(j - 2) % MOD * cnt(w - j - 2) % MOD) % MOD; // 1 j 0
if (j < w - 1)
dp[i][j] = (dp[i][j] + dp[i + 1][j + 1] * cnt(j - 1) % MOD * cnt(w - j - 3) % MOD) % MOD; // 0 j 1
// cout << i << ' ' << j << ' ' << dp[i][j] << endl;
}
}
cout << dp[0][0] << endl;
return 0;
}
| #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long ll;
const int MAXN = 3e5 + 10;
const int Mod = 1e9 + 7;
int N;
char S[MAXN];
ll f[MAXN][2][2][3], s[MAXN];
ll ans;
int main() {
register int i, j, k, l;
scanf("%s", S + 1); N = strlen(S + 1);
for(s[N + 1] = 1, i = N; i; --i) s[i] = s[i + 1] * (S[i] == '?' ? 2 : 1) % Mod;
f[1][0][0][0] = 1;
for(i = 1; i <= N; ++i) {
if(S[i] != '1')
for(j = 0; j < 2; ++j)
for(k = 0; k < 2; ++k)
for(l = 0; l < 3; ++l)
(f[i + 1][j][k][(l & 1) + 1] += f[i][j][k][l]) %= Mod;
if(S[i] != '0')
for(j = 0; j < 2; ++j)
for(k = 0; k < 2; ++k) {
if(!j && !k) (f[i + 1][1][1][0] += f[i][j][k][0]) %= Mod;
else if(j && k) (ans += 1ll * f[i][j][k][0] * s[i + 1]) %= Mod;
else (f[i + 1][!j][k][0] += f[i][j][k][0]) %= Mod;
(f[i + 1][j][k][0] += f[i][j][k][1]) %= Mod;
(f[i + 1][!j][k][0] += f[i][j][k][2]) %= Mod;
}
}
printf("%lld\n", (ans + f[N + 1][1][1][0]) % Mod);
return 0;
} | 0 |
#include<iostream>
#include<algorithm>
#include<utility>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define P pair<int, int>
using namespace std;
char field[12][12];
int cnt;
int dx[4] = { 1, 0, -1, 0 };
int dy[4] = { 0, 1, 0, -1 };
char depth;
void dfs(int x,int y){
/*
rep(i, 12){
rep(j, 12){
if (field[i][j] == '1'){
cout << field[i][j];
}
else{
cout << field[i][j];
}
if (j == 11){
cout << endl;
}
}
}
cout << endl;
*/
field[x][y] = '0';
rep(i, 4){
int ddx = x + dx[i], ddy = y + dy[i];
if (0<=ddx && ddx<12 && 0<=ddy && ddy<12 && field[ddx][ddy] == '1'){
dfs(ddx, ddy);
}
}
}
int main(){
while (1){
cnt = 0;
rep(i, 12){
rep(j, 12){
char c;
cin >> c;
if(cin.eof()){
return 0;
}
field[i][j] = c;
/*if (j == 11){
char EnTeR;
cin >> EnTeR;
}*/
}
}
/*
rep(i, 12){
rep(j, 12){
if (field[i][j] == '1'){
cout << "テ「ツ鳴?;
}
else{
cout << "テ「ツ鳴。";
}
if (j == 11){
cout << endl;
}
}
}
*/
rep(i, 12){
rep(j, 12){
if (field[i][j] == '1'){
cnt++;
dfs(i, j);
}
}
}
cout << cnt << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
int main(){
int n,t;
while(cin>>n,n){
int c[10];fill(c,c+10,0);
for(int i=0;i<n;i++)cin>>t,c[t]++;
for(int i=0;i<10;i++){if(!c[i])cout<<'-';else for(int j=0;j<c[i];j++)cout<<'*';cout<<endl;}
}
} | 0 |
#include <bits/stdc++.h>
const double PI = 3.14159265358979323846;
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
#define rep(i, n) for (int i = 0; i < (n); ++i)
//#define rep(i, n) for (ll i = 0; i < (n); ++i)
typedef pair<ll, ll> P;
const ll INF = 10e17;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)
#define ret() return 0;
std::istream &operator>>(std::istream &in, set<ll> &o) {
ll a;
in >> a;
o.insert(a);
return in;
}
std::istream &operator>>(std::istream &in, queue<int> &o) {
ll a;
in >> a;
o.push(a);
return in;
}
bool contain(set<int> &s, int a) { return s.find(a) != s.end(); }
//ifstream myfile("C:\\Users\\riku\\Downloads\\0_00.txt");
//ofstream outfile("log.txt");
//outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0') << rank << endl;
// std::cout << std::bitset<8>(9);
//const ll mod = 1e10;
//typedef priority_queue<P, vector<P>, greater<P> > PQ_ASK;
vector<ll> bfs(int n, vector<vector<int>> &g) {
vector<ll> costs(n, INF);
costs[0] = 0;
queue<int> q;
q.push(0);
while (!q.empty()) {
int from = q.front();
q.pop();
for (int to : g[from]) {
if (costs[to] != INF) continue;
costs[to] = costs[from] + 1;
q.push(to);
}
}
return costs;
}
bool bfs2(int n, vector<vector<int>> &g) {
vector<ll> costs(n, INF);
costs[0] = 0;
queue<int> q;
q.push(0);
while (!q.empty()) {
int from = q.front();
q.pop();
for (int to : g[from]) {
ll current_cost = costs[to];
ll next_cost = costs[from] + 1;
if (current_cost != INF) {
if (current_cost % 2 != next_cost % 2) return false;
continue;
}
costs[to] = next_cost;
q.push(to);
}
}
return true;
}
int main() {
int n, m;
cin >> n >> m;
vector<P> v(m);
for (P &p : v) cin >> p.first >> p.second, p.first--, p.second--;
vector<vector<int>> g(n);
for (P p: v) g[p.first].push_back(p.second);
for (P p: v) g[p.second].push_back(p.first);
vector<ll> costs = bfs(n, g);
bool is_2 = bfs2(n, g);
if (is_2) {
ll a = 0;
for (ll c : costs) if (c % 2 == 0) a++;
ll b = n - a;
ll all = a * b;
ll ans = all - m;
cout << ans << endl;
ret();
}
ll all = ll(n) * (n - 1) / 2;
ll ans = all - m;
cout << ans << endl;
} | %:pragma GCC optimize "-O3"
%:pragma GCC optimize("Ofast")
%:pragma GCC optimize("inline")
%:pragma GCC optimize("fast-math")
%:pragma GCC optimize("unroll-loops")
%:pragma GCC optimize("inline-functions")
%:pragma GCC optimize("no-stack-protector")
%:pragma GCC optimize("inline-small-functions")
%:pragma GCC optimize("inline-functions-called-once")
%:pragma GCC target("sse3","sse2","sse")
#include<bits/stdc++.h>
#define ll long long
#define heap priority_queue
using namespace std;
const ll maxn=15+1;
const ll inf=9e17;
ll ans,sum,dp[maxn][1<<maxn],dis[1<<maxn],g[maxn][maxn],tmp,lim,n,m;
inline ll lowbit(ll x){
return x&-x;
}
int main()
{
cin>>n>>m;
ll u,v,w;
for(int i=1;i<=m;i++)
{
cin>>u>>v>>w;
--u;
--v;
g[u][v]=g[v][u]=w;
sum+=w;
}
lim=(1<<n)-1;
for(int i=1;i<=lim;i++)
{
tmp=log2(lowbit(i));
dis[i]=dis[i^(1<<tmp)];
for(int j=0;j<n;j++)
if(g[tmp][j]&&(i&(1<<j)))
dis[i]+=g[tmp][j];
}
memset(dp,0xc0,sizeof(dp));
dp[0][1]=0;
ans=-inf;
for(int i=1;i<=lim;i++)
{
if(!(i&1))
continue;
for(int j=0;j<n;j++)
if(i&(1<<j))
{
for(int k=i;k;k=(k-1)&i)
if(k&(1<<j))
dp[j][i]=max(dp[j][i],dp[j][i^k^(1<<j)]+dis[k]);
for(int k=lim^i;k;k-=lowbit(k))
{
tmp=log2(lowbit(k));
if(g[tmp][j])
dp[tmp][i^(1<<tmp)]=max(dp[tmp][i^(1<<tmp)],dp[j][i]+g[tmp][j]);
}
}
if(i&(1<<(n-1)))
ans=max(ans,dp[n-1][i]);
}
cout<<sum-ans<<"\n";
return 0;
} | 0 |
#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
// increment
#define rep_n(_1, _2, _3, NAME, ...) NAME
#define rep_2(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define rep_3(i, initial, n) \
for (int(i) = (int)(initial); (i) < (int)(n); ++(i))
#define rep(...) rep_n(__VA_ARGS__, rep_3, rep_2, rep_1)(__VA_ARGS__)
// decrement
#define rep_r_n(_1, _2, _3, NAME, ...) NAME
#define rep_r_2(i, n) for (int(i) = (int)(n); (i) > 0; (i)--)
#define rep_r_3(i, initial, n) \
for (int(i) = (int)(initial); (i) > (int)(n); (i)--)
#define rep_r(...) rep_r_n(__VA_ARGS__, rep_r_3, rep_r_2)(__VA_ARGS__)
// define type definition
using namespace std;
typedef long long ll;
using mpint = boost::multiprecision::cpp_int; // 多倍長整数(任意精度)
typedef pair<int, int> P;
// define construct variable
const double EPS = numeric_limits<double>::epsilon();
// 10^9 + 7
const ll MOD = ll(1e9) + 7;
const ll INF = ll(1e10);
const string ALPHABET = "abcdefghijklmnopqrstuvwxyz";
const double PI = acos(-1);
// define methods
// 最大公約数
ll gcd(const ll a, const ll b) { return b ? gcd(b, a % b) : a; }
// 最小公倍数
ll lcm(const ll a, const ll b) { return a / gcd(a, b) * b; }
// char -> int
int ctoi(char c) { return (int)(c - '0'); }
// 素因数分解
vector<pair<ll, ll>> prime_factorize(ll n) {
vector<pair<ll, ll>> p;
for (ll i = 2; i * i <= n; i++) {
if ((n % i) != 0) continue;
pair<ll, ll> a = make_pair(i, 0);
while ((n % i) == 0) {
n /= i;
a.second += 1;
}
p.push_back(a);
}
if (n != 1) p.push_back(make_pair(n, 1));
return p;
}
// 組み合わせの整合性を保つ(後ほど解析する)
ll modinv(ll a) {
ll b = MOD, u = 1, v = 0;
while (b) {
ll t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
u %= MOD;
if (u < 0) {
u += MOD;
}
return u;
}
// 順列
ll permutation(const ll n, const ll m) {
ll result = 1;
for (ll i = n; i > n - m; i--) {
result = (result * i) % MOD;
}
return result;
}
// 繰り返し n 乗法
ll repeat_square(const ll n, const ll x) {
if (x == 0) return 1;
ll res = repeat_square(n, x / 2);
res = res * res % MOD;
if ((x % 2) == 1) res = res * n % MOD;
return res;
}
// 組み合わせ nCm
ll combination(ll n, ll m) {
ll p = permutation(n, m);
ll div = 1;
for (ll i = 2; i <= m; i++) {
div = (div * i) % MOD;
}
return (p * modinv(div)) % MOD;
}
// アルファベットの取得
string get_alphabet(ll n) {
n--;
return string(1, 'a' + (n % 26));
}
int main() {
int N, Q;
string S;
cin >> N >> Q >> S;
vector<int> L(Q+1), R(Q+1);
rep(i, Q) {
cin >> L[i] >> R[i];
}
vector<int> p(N, 0);
int count = 0;
string prev = S.substr(0, 1);
rep(i, 1, N) {
string s = S.substr(i, 1);
if ((prev + s) == "AC") count++;
p[i] = count;
prev = s;
}
rep(i, Q) {
int l = L[i], r = R[i];
int ans = p[r-1] - p[l-1];
cout << ans << endl;
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
int N, Q;
cin>>N>>Q;
string S;
vector<int> ac_cnt(N, 0);
int cnt = 0;
cin>>S;
for(int i=0; i<N-1; i++){
if(S[i]=='A' && S[i+1]=='C'){
S[i]='a';
cnt++;
}
ac_cnt[i+1] = cnt;
}
vector<int> ans;
for(int i=0; i<Q; i++){
int l, r;
cin>>l>>r;
if(l == 1){
ans.push_back(ac_cnt[r-1]);
}else{
ans.push_back(ac_cnt[r-1] - ac_cnt[l-1]);
}
}
// for(int a : ac_cnt){
// cout<<a<<endl;
// }
// cout<<"-------"<<endl;
for(int a : ans){
cout<<a<<endl;
}
return 0;
} | 1 |
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
#define ll long long
int main (){
int a,b;
cin >> a >> b;
int i=0;
int sum = 1;
while(sum<b){
sum--;
sum+=a;
i++;
}
cout << i << endl;
return 0;
} | #include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<iomanip>
using namespace std;
int main(){
int l,r;
cin>>l>>r;
long long ans=3000;
if(r-l>=2200)cout<<0<<endl;
else{
for(long long i=l;i<=r-1;i++){
for(long long j=i+1;j<=r;j++){
long long tmp=(i*j)%2019;
ans=min(ans,(i*j)%2019);
}
}
cout<<ans<<endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
struct hito{
long long sum;
long long H;
long long P;
};
bool cmp(const hito &a, const hito &b){
return a.sum < b.sum;
}
int main(){
int N; cin >> N;
long long INF = 1e18;
hito A[5010] = {};
for(int i = 1; i <= N; i++){
cin >> A[i].H >> A[i].P;
A[i].sum = A[i].H + A[i].P;
}
sort(A+1, A+N+1, cmp);
long long dp[5010][5010] = {}; // dp[i人目まで見て][j人のときの最小の高さ]
for(int i = 0; i <= N; i++){
for(int j = 1; j <= N+1; j++){
dp[i][j] = INF;
}
}
for(int i = 1; i <= N; i++){
for(int j = 1; j <= N; j++){
if(A[i].H >= dp[i-1][j-1]) dp[i][j] = min(dp[i-1][j], dp[i-1][j-1] + A[i].P);
else dp[i][j] = dp[i-1][j];
}
}
for(int i = 1; i <= N+1; i++){
if(dp[N][i] == INF){
cout << i-1 << endl; return 0;
}
}
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct node
{
ll x;
ll y;
};
node a[5007];
int n;
bool cmp(node p,node q)
{
if (p.x+p.y==q.x+q.y) return p.y<q.y;
return p.x+p.y<q.x+q.y;
}
int main(){
ios::sync_with_stdio(false);
scanf("%d",&n);
for (int i=1;i<=n;i++)
scanf("%lld%lld",&a[i].x,&a[i].y);
priority_queue<int>q;
// n=n+1;
// a[n].x=1e9+10;
// a[n].y=1e9+10;
sort(a+1,a+n+1,cmp);
ll tmp=0;
ll t=0;
int ans=0;
for (int i=1;i<=n;i++)
{
// cout<<ans<<" "<<a[i].x<<a[]
if (tmp<=a[i].x)
{
tmp+=a[i].y;
q.push(a[i].y);
ans++;
}
else
{
if (a[i].y<q.top())
{
tmp-=q.top();
q.pop();
tmp+=a[i].y;
q.push(a[i].y);
}
}
}
printf("%d\n",ans);
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define debug(x) cout << #x << " = " << x << endl;
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define FOR(it, b, e) for (typeof(b) it = (b); it != (e); ++it)
#define MSET(c, v) memset(c, v, sizeof(c))
const int INF = 0x3F3F3F3F;
const int NEGINF = 0xC0C0C0C0;
const int NULO = -1;
const double EPS = 1e-10;
const ll mod = 1e9 + 7;
inline int cmp(double x, double y = 0)
{
if(fabs(x - y) < EPS)
return 0;
return x > y ? 1 : -1;
}
ll exp(ll a, ll b)
{
ll ans = 1;
while(b)
{
if(b & 1)
ans = (ans * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return ans;
}
const int maxn = 85;
ll fat[maxn], inv[maxn];
ll C(int n, int k)
{
if(n < k)
return 0;
if(n < 0 || k < 0)
return 0;
return (((fat[n] * inv[k]) % mod) * inv[n - k]) % mod;
}
void initFactorials()
{
fat[0] = 1;
inv[0] = 1;
for(int i = 1; i < maxn; i++)
{
fat[i] = (i * fat[i-1]) % mod;
inv[i] = exp(fat[i], mod - 2);
}
}
int dp[maxn][maxn][2 * maxn * maxn];
ll a[maxn][maxn];
ll b[maxn][maxn];
int n,m;
int main()
{
ios::sync_with_stdio(false);
cin >> n >> m;
for(int i = 0 ; i < n; i++)
for(int j = 0; j < m; j++)
cin >> a[i][j];
for(int i = 0 ; i < n; i++)
for(int j = 0; j < m; j++)
cin >> b[i][j];
dp[0][0][abs(a[0][0] - b[0][0])] = true;
for(int i = 0; i < n; i++)
{
for(int j = 0 ; j < m; j++)
{
for(int k = 0; k < (n + m) * maxn; k++)
{
if(i > 0)
{
dp[i][j][k] = dp[i - 1][j][k + abs(a[i][j] - b[i][j])] |
dp[i - 1][j][abs(k - abs(a[i][j] - b[i][j]))];
}
if(j > 0)
{
dp[i][j][k] = dp[i][j][k] |
dp[i][j - 1][k + abs(a[i][j] - b[i][j])] |
dp[i][j - 1][abs(k - abs(a[i][j] - b[i][j]))];
}
}
}
}
for(int i = 0; i < (n + m) * maxn; i++)
{
if(dp[n - 1][m - 1][i] == 1)
{
cout << i << endl;
return 0;
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(i = 0;i < n;++i)
#define all(v) v.begin(), v.end()
using ll = int;
int main()
{
ll i,j,k;
ll h,w;
cin >> h >> w;
ll inf = 160*80;
vector<vector<ll>> a(h,vector<ll>(w,0));
vector<vector<ll>> b(h,vector<ll>(w,0));
for(i = 0;i < h;++i){
rep(j,w) cin >> a.at(i).at(j);
}
for(i = 0;i < h;++i){
rep(j,w) cin >> b.at(i).at(j);
}
vector<vector<vector<ll>>> dp(h, vector<vector<ll>>(w, vector<ll>(2*inf+1,0)));
ll tmp = a.at(0).at(0) - b.at(0).at(0);
dp.at(0).at(0).at(inf + tmp) = 1;
dp.at(0).at(0).at(inf - tmp) = 1;
for(i = 0;i < h;++i){
rep(j,w){
ll ni = i + 1;
ll nj = j + 1;
ll t1, t2;
if(ni < h) t1 = a.at(ni).at(j) - b.at(ni).at(j);
if(nj < w) t2 = a.at(i).at(nj) - b.at(i).at(nj);
rep(k, 2*inf+1){
if(dp.at(i).at(j).at(k)){
if(ni < h){
dp.at(ni).at(j).at(k + t1) = 1;
dp.at(ni).at(j).at(k - t1) = 1;
}
if(nj < w){
dp.at(i).at(nj).at(k + t2) = 1;
dp.at(i).at(nj).at(k - t2) = 1;
}
}
}
}
}
ll ans = 1e9;
for(i = 0;i < 2*inf + 1;++i){
if(dp.at(h-1).at(w-1).at(i)){
ans = min(ans, abs(i - inf));
}
}
cout << ans << endl;
return 0;
} | 1 |
#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;
using i64 = int64_t;
const i64 MOD = 1e9+7;
const i64 INF = 1e18+7;
struct UnionFind{
int n;
vector<int> par;
UnionFind(int n) : n(n), par(n, -1){}
int Find(int x){return par[x] < 0 ? x : Find(par[x]);}
bool Unite(int x, int y){
x = Find(x), y = Find(y);
if(x == y)
return false;
if(par[x] > par[y])
swap(x, y);
par[x] += par[y];
par[y] = x;
return true;
}
};
template<typename T>
struct Segtree{
const int n;
const T op;
vector<T> elm;
function<T(T, T)> f;
Segtree(int n, T init, function<T(T, T)> f, T op = T()) :
n(n),
op(op),
f(f),
elm(2 * n, init)
{
}
void set(int x, T val){
x += n;
elm[x] = val;
while(x >>= 1)
elm[x] = f(elm[2 * x], elm[2 * x + 1]);
}
void update(int x, T val){
x += n;
elm[x] = f(val, elm[x]);
while(x >>= 1)
elm[x] = f(elm[2 * x], elm[2 * x + 1]);
}
T get(int x, int y) const{
T val = op;
for(x += n, y += n - 1; x <= y; x >>= 1, y >>= 1){
if(x & 1)
val = f(val, elm[x++]);
if(!(y & 1))
val = f(val, elm[y--]);
}
return val;
}
};
signed main(){
int n, d;
cin >> n >> d;
vector<int> v(n);
for(auto& x : v)
cin >> x;
auto f = [](pair<i64,int> x, pair<i64,int> y){
return min(x, y);
};
Segtree<pair<i64,int>> seg_a(n, pair<i64,int>(), f, pair<i64,int>(INF, 0));
Segtree<pair<i64,int>> seg_b(n, pair<i64,int>(), f, pair<i64,int>(INF, 0));
auto s = [&](int i){
seg_a.set(i, make_pair(v[i] + i64(1) * d * i, i));
seg_b.set(i, make_pair(v[i] + i64(1) * d * (n - i - 1), i));
};
auto t = [&](int i){
seg_a.set(i, make_pair(INF, i));
seg_b.set(i, make_pair(INF, i));
};
for(int i = 0; i < n; ++i)
s(i);
UnionFind u(n);
i64 ans = 0;
while(u.par[u.Find(0)] != -n){
// 子ノードの一覧
vector<vector<int>> ch(n);
// 接合するノード
vector<pair<i64, int>> nex(n, make_pair(INF, 0));
for(int i = 0; i < n; ++i)
ch[u.Find(i)].push_back(i);
for(int i = 0; i < n; ++i){
if(ch[i].empty())
continue;
for(auto& x : ch[i])
t(x);
pair<i64,int> val(INF, 0);
for(auto& x : ch[i]){
auto p_a = seg_a.get(x + 1, n);
p_a.first -= i64(1) * d * x;
p_a.first += v[x];
val = f(val, p_a);
auto p_b = seg_b.get(0, x);
p_b.first -= i64(1) * d * (n - 1 - x);
p_b.first += v[x];
val = f(val, p_b);
}
for(auto& x : ch[i])
s(x);
nex[i] = val;
}
for(int i = 0; i < n; ++i){
if(nex[i].first != INF && u.Unite(i, nex[i].second))
ans += nex[i].first;
}
}
cout << ans << endl;
}
| #include<bits/stdc++.h>
#define ll long long int
#define endl '\n'
#define M 1000000007
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define f first
#define s second
#define b begin
#define e end
#define pb push_back
#define mp make_pair
#define FOR(i,a,b) for(i=a;i<b;i++)
#define RFOR(i,a,b) for(i=a;i>=b;i--)
#define all(x) x.begin(),x.end()
#define itfr(it,x) for(it=x.begin();it!=x.end();it++)
#define flash ios_base::sync_with_stdio(false); cin.tie(NULL)
using namespace std;
int main()
{
flash;
//sieve();
int T=1,t,n,m,k,i;
// cin>>T;
while(T--)
{
cin>>n>>k;
int a[10]={0};
FOR(i,0,k){
cin>>t;
a[t]=1;
}
vector<int> vs;
FOR(i,0,10)
if(a[i]==0)
vs.pb(i);
// for(auto it:vs)cout<<it<<" ";cout<<endl;
int dg=0;
for(i=n;i>0;i/=10)dg++;
ll ans=0;
k = pow(10,dg-1);
int fl=0;
int num=n;
// cout<<dg<<" "<<k<<endl;
while(ans<num)
{
if(k==0){
ans = ans*10 + vs[0];
continue;
}
m = n/k;
n = n%k;
k/=10;
if(a[m]==0&fl==0){
ans = ans*10 + m;
continue;
}
dg=0;
if(fl==0)
{
for(auto it:vs)
if(it>=m)
{
dg=it;
break;
}
if(dg==0)
{
if(vs[0]!=0)
dg=vs[0];
else
{
if(ans==0)
dg=vs[1];
else
dg=vs[0];
}
}
ans = ans*10+dg;
fl=1;
}
else
{
ans = ans*10 + vs[0];
}
}
cout<<ans<<endl;
}
} | 0 |
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <iomanip>
#define ll long long
using namespace std;
long long MOD = 1000000007;
template<typename T>
void cout_vec(vector<T> &vec){
for(int i=0; i<vec.size(); i++){
if (i!=0){
cout << ' ';
}
cout << vec[i];
}
cout << endl;
}
template<typename T>
void cout_mat(vector<vector<T> > &mat){
for(int i=0; i<mat.size(); i++){
vector<T> vec = mat[i];
cout_vec(mat[i]);
}
}
int main(){
int H, W;
cin >> H >> W;
vector<vector<int>> grid(H,vector<int>(W,0));
for(auto &line:grid){
for(auto &a:line){
cin >> a;
}
}
for(auto &line:grid){
for(auto &a:line){
int x;
cin >> x;
a-=x;
}
}
int N=160*160;
vector<vector<vector<int>>> dp(H+1,vector<vector<int>>(W+1,vector<int>(N+1,0)));
dp[0][0][N/2]=1;
for(int i=0; i<H; i++){
for(int j=0; j<W; j++){
int diff=grid[i][j];
for(int k=0; k<=N; k++){
if(0<=k+diff && k+diff<=N){
dp[i+1][j][k+diff]|=dp[i][j][k];
dp[i][j+1][k+diff]|=dp[i][j][k];
}
if(0<=k-diff && k-diff<=N){
dp[i][j+1][k-diff]|=dp[i][j][k];
dp[i+1][j][k-diff]|=dp[i][j][k];
}
}
}
}
for(int i=0; i<N; i++){
if(dp[H][W-1][N/2-i] || dp[H][W-1][N/2+i]){
cout << i << endl;
return 0;
}
}
} | #include <bits/stdc++.h>
#define fi first
#define se second
#define ii pair<int, int>
#define int long long
using namespace std;
const int INF = 1e9;
int n, m;
int ans = INF;
int a[85][85], b[85][85];
bool f[85][85][52000];
void attempt(int i, int j, int diff)
{
if (i == n && j == m)
{
ans = min(ans, abs(diff + a[i][j] - b[i][j]));
ans = min(ans, abs(diff - a[i][j] + b[i][j]));
return;
}
if (f[i][j][diff])
return;
f[i][j][diff] = 1;
if (i < n){
attempt(i+1, j, abs(diff + a[i][j] - b[i][j]));
attempt(i+1, j, abs(diff - a[i][j] + b[i][j]));
}
if (j < m){
attempt(i, j+1, abs(diff + a[i][j] - b[i][j]));
attempt(i, j+1, abs(diff - a[i][j] + b[i][j]));
}
}
int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("in.inp", "r", stdin);
// freopen("out.out", "w", stdout);
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> a[i][j];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> b[i][j];
attempt(1, 1, 0);
cout << ans;
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m,i,x,y;
cin>>n>>m>>x>>y;
int ara[n],bra[m];
for(i=0;i<n;i++)
cin>>ara[i];
for(i=0;i<m;i++)
cin>>bra[i];
sort(ara,ara+n);
sort(bra,bra+m);
for(i=ara[n-1]+1;i<=bra[0];i++)
{
if(i>x && i<=y)
{
cout<<"No War"<<endl;
return 0;
}
}
cout<<"War"<<endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i,x,n) for(int i=x; i<(int)(n); i++)
using namespace std;
bool hantei( vector<int>& x, vector<int>& y ) {
rep( Z, -100, 100+1 ) {
bool flag = true;
rep( i, 0, x.size() ) {
if ( x.at(i) >= Z ) flag = false;
}
rep( i, 0, y.size() ) {
if ( y.at(i) < Z ) flag = false;
}
if (flag) return false;
}
return true;
}
int main() {
int N, M;
cin >> N >> M;
vector<int> x(N+1), y(M+1);
cin >> x.at(0) >> y.at(0);
rep(i, 1, N+1) cin >> x.at(i);
rep(i, 1, M+1) cin >> y.at(i);
cout << ( hantei(x, y) ? "War" : "No War" ) << endl;
}
| 1 |
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <iomanip>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cassert>
#include <complex>
#include <stdio.h>
#include <time.h>
#include <numeric>
#include <random>
#include <unordered_map>
#include <unordered_set>
#define all(a) a.begin(),a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
#define debug(x) cerr << #x << ':' << (x) << '\n'
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef complex<ld> com;
constexpr int inf = 1000000010;
constexpr ll INF = 1000000000000000010;
constexpr ld eps = 1e-12;
constexpr ld pi = 3.141592653589793238;
template<class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; }
template<class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; }
constexpr ll mod = 1000000007;
const int sz = 1 << 20;
ll modpow(ll a, ll b, ll m = mod) {
ll ans = 1;
a %= m;
while (b) {
if (b & 1) ans = ans * a % m;
a = a * a % m;
b >>= 1;
}
return ans;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
int n;
cin >> n;
int now = 0;
vector<int> a(n), x(n), zero(n);
vector<vector<int>> pl(sz, vector<int>());
rep(i, n) {
cin >> a[i];
now ^= a[i];
x[i] = now;
int pre = 0;
if (i) pre = zero[i - 1];
if (now == 0) pre++;
zero[i] = pre;
pl[now].pb(i);
}
if (now == 0) {
ll ans = modpow(2, pl[0].size() - 1);
for (int num = 1; num < sz; num++) {
vector<int> vec;
int s = pl[num].size();
if (s == 0) continue;
rep(i, s - 1) {
vec.pb(1);
vec.pb(zero[pl[num][i + 1]] - zero[pl[num][i]]);
}
vec.pb(1);
vector<vector<ll>> dp(2 * s, vector<ll>(2));
dp[0][0] = 1;
rep(i, 2 * s - 1) {
dp[i + 1][0] += dp[i][0];
if (i % 2 == 1) {
dp[i + 1][0] += dp[i][1] * vec[i];
}
dp[i + 1][1] += dp[i][1];
if (i % 2 == 0) {
dp[i + 1][1] += dp[i][0] * vec[i];
}
dp[i + 1][0] %= mod;
dp[i + 1][1] %= mod;
}
ans += dp[2 * s - 1][1];
}
ans %= mod;
cout << ans << '\n';
}
else {
int num = now;
vector<int> vec;
int s = pl[num].size();
rep(i, s - 1) {
vec.pb(1);
vec.pb(zero[pl[num][i + 1]] - zero[pl[num][i]]);
}
vector<vector<ll>> dp(2 * s - 1, vector<ll>(2));
dp[0][0] = 1;
rep(i, 2 * s - 2) {
dp[i + 1][0] += dp[i][0];
if (i % 2 == 1) {
dp[i + 1][0] += dp[i][1] * vec[i];
}
dp[i + 1][1] += dp[i][1];
if (i % 2 == 0) {
dp[i + 1][1] += dp[i][0] * vec[i];
}
dp[i + 1][0] %= mod;
dp[i + 1][1] %= mod;
}
cout << dp[2 * s - 2][0] << '\n';
}
} | #include <bits/stdc++.h>
using namespace std;
#define task "test"
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define openfile if (fopen(task ".inp","r")){freopen(task ".inp","r",stdin);freopen(task ".out","w",stdout);}
#define ll long long
#define pii pair <int, int>
#define pll pair <ll, ll>
#define all(c) c.begin(), c.end()
#define pb push_back
#define mp make_pair
#define X first
#define Y second
const int INF = 1E9;
const int MOD = 1E9 + 7;
const int N = 1000 + 10;
int t = 1;
ll n, a, b, c, d;
ll Fact[N], Inv[N], F[N][N];
ll POW(ll n, ll k)
{
if (k == 1) return n;
ll tmp = POW(n, k / 2);
tmp = tmp * tmp % MOD;
if (k & 1) return tmp * n % MOD;
return tmp;
}
void Input()
{
cin >> n >> a >> b >> c >> d;
}
void Prepare()
{
Fact[0] = Inv[0] = Fact[1] = Inv[1] = 1;
for (int i = 2; i <= n; i++) Fact[i] = Fact[i - 1] * (ll) (i) % MOD;
for (int i = 2; i <= n; i++) Inv[i] = POW(Fact[i], MOD - 2);
}
ll Comb(ll n, ll k)
{
if (n < k) return 0;
return Fact[n] * Inv[k] % MOD * Inv[n - k] % MOD;
}
void Solve()
{
F[a - 1][0] = 1;
for (int i = a; i <= b; i++)
for (int j = 0; j <= n; j++) {
F[i][j] = F[i - 1][j];
//cout << "Pre : " << F[i][j] <<" - > ";
for (int k = c; k <= d && i * k <= j; k++) {
F[i][j] += F[i - 1][j - i * k] * Comb(j, j - i * k) % MOD * Fact[i * k] % MOD * POW(Inv[i], k) % MOD * Inv[k] % MOD;
F[i][j] %= MOD;
// if (i == 1 && j == 3) cout << F[i][j] << " " << k << endl;
}
// cout << i << " " << j << " " << F[i][j] << endl;
}
cout << F[b][n];
}
int main()
{
openfile;
// cin >> t;
while (t--) {
Input();
Prepare();
Solve();
}
}
| 0 |
#include <bits/stdc++.h>
#define sz(arr) (int)arr.size()
#define all(arr) arr.begin(), arr.end()
using namespace std;
void _cin() {}
template <class Head, class... Tail>
void _cin(Head&& head, Tail&&... tail) {
cin >> head;
_cin(forward<Tail>(tail)...);
}
#define Cin(T, ...) \
T __VA_ARGS__; \
_cin(__VA_ARGS__)
int main() {
Cin(int, n);
cout << 180 * (n - 2) << endl;
return 0;
} | #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <math.h>
#include <map>
using namespace std;
int main(){
double n;
cin >> n;
cout << n*(180-(360/n)) << endl;
return 0;
}
| 1 |
//Be Name Khoda, Besmellah Rahmane Rahim, In The Name Of God;
//#include<bits/stdc++.h>
#include<iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <queue>
#include <deque>
#include <map>
#include <stack>
#include<bitset>
#include<list>
#include<cassert>
#include<numeric>
#include <stdio.h>
#include <string.h>
#include<iomanip>
#include<unordered_map>
#include<unordered_set>
#include <fstream>
using namespace std;
using namespace std;
const int N = 1e6 + 5;
const int mod = 998244353;
long long fact[N];
int n, m;
void pre()
{
fact[0] = 1;
for (int i = 1; i < N; i++)
{
fact[i] = fact[i - 1] * i;
fact[i] = fact[i] % mod;
}
}
int power(int a, int b)
{
if (!b)
{
return 1;
}
long long ans = power(a, b / 2);
ans = ans * ans;
ans = ans % mod;
if (b % 2)
{
ans = ans * a;
ans = ans % mod;
}
return ans;
}
long long choose(int a, int b)
{
if (b > a)
{
return 0;
}
long long ans = fact[a];
ans = ans * power(fact[b], mod - 2);
ans = ans % mod;
ans = ans * power(fact[a - b], mod - 2);
ans = ans % mod;
return ans;
}
int main()
{
pre();
//cout << choose(5, 2) << endl;
cin >> n >> m;
long long ans = 0;
for (int i = 1; i <= n && i <= m; i++)
{
long long tmp = choose(i * 2, i) * choose(n + m - 2 * i, n - i);
tmp = tmp % mod;
ans += tmp;
ans = ans % mod;
}
long long tmp = choose(n + m, n) * 2;
tmp = power(tmp, mod - 2);
ans = ans * tmp;
ans = ans % mod;
ans += max(n, m);
ans = ans % mod;
cout << ans << endl;
}
| #include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define MAXN 1000005
#define eps 1e-12
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
const int MOD = 998244353;
int N,M;
int fac[MAXN],invfac[MAXN],f[MAXN],v[MAXN],h[MAXN],inv[MAXN];
int inc(int a,int b) {
return a + b >= MOD ? a + b - MOD : a + b;
}
int mul(int a,int b) {
return 1LL * a * b % MOD;
}
int C(int n,int m) {
if(n < m) return 0;
return mul(fac[n],mul(invfac[m],invfac[n - m]));
}
int way(int x1,int y1,int x2,int y2) {
return C(x2 - x1 + y2 - y1,y2 - y1);
}
void update(int &x,int y) {
x = inc(x,y);
}
int fpow(int x,int c) {
int res = 1,t = x;
while(c) {
if(c & 1) res = mul(res,t);
t = mul(t,t);
c >>= 1;
}
return res;
}
void Solve() {
read(N);read(M);
fac[0] = 1;
inv[1] = 1;
for(int i = 1 ; i <= N + M ; ++i) {
fac[i] = mul(fac[i - 1],i);
if(i > 1) inv[i] = mul(inv[MOD % i],MOD - MOD / i);
}
invfac[N + M] = fpow(fac[N + M],MOD - 2);
for(int i = N + M - 1 ; i >= 0 ; --i) {
invfac[i] = mul(invfac[i + 1],i + 1);
}
if(M > N) h[0] = 1;
if(N > M) v[0] = 1;
for(int i = 1 ; i <= N + M ; ++i) {
h[i] = h[i - 1];
int r = min(i - 1,N) - h[i - 1] + 1,c = i - 1 - r + 1;
if(i <= N && M - (N - r) >= c + 1) ++h[i];
if(i > N && M - (N - r) <= c) --h[i];
v[i] = v[i - 1];
c = min(i - 1,M) - v[i - 1] + 1,r = i - 1 - c + 1;
if(i <= M && N - (M - c) >= r + 1) ++v[i];
if(i > M && N - (M - c) <= r) --v[i];
}
f[0] = mul(C(N + M, N),max(N,M));
int valh = 0,valv = 0;
if(h[0]) valh = C(N + M - 1, M - 1);
if(v[0]) valv = C(N + M - 1, N - 1);
for(int i = 1 ; i < N + M ; ++i) {
f[i] = f[i - 1];
if(h[i - 1]) f[i] = inc(f[i],MOD - valh);
if(v[i - 1]) f[i] = inc(f[i],MOD - valv);
if(!h[i - 1] && h[i]) valh = way(i,0,N,M - 1);
if(!v[i - 1] && v[i]) valv = way(0,i,N - 1,M);
int r1 = min(i - 1,N) - h[i - 1] + 1,c1 = i - 1 - r1;
int r2 = min(i,N) - h[i] + 1,c2 = i - r2;
if(h[i - 1]) {
if(r2 > r1) {
valh = inc(valh, MOD - mul(way(0,0,r1,c1),way(r1,c1 + 1,N,M - 1)));
}
else {
if(r2 >= 1) valh = inc(valh,mul(way(0,0,r2 - 1,c2),way(r2,c2,N,M - 1)));
}
}
c1 = min(i - 1,M) - v[i - 1] + 1,r1 = i - 1 - c1;
c2 = min(i,M) - v[i] + 1,r2 = i - c2;
if(v[i - 1]) {
if(c2 > c1) {
valv = inc(valv, MOD - mul(way(0,0,r1,c1),way(r1 + 1,c1,N - 1,M)));
}
else {
if(c2 >= 1) valv = inc(valv,mul(way(0,0,r2,c2 - 1),way(r2,c2,N - 1,M)));
}
}
}
int ans = 0;
for(int i = 0 ; i < N + M ; ++i) {
ans = inc(ans,mul(f[i],inv[N + M - i]));
}
ans = mul(ans,fpow(C(N + M,N),MOD - 2));
out(ans);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}
| 1 |
#include <stdio.h>
using namespace std;
int final_board[8][8] = {{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0}};
void deletePossibility(int tmp_board[8][8],int row,int column){
tmp_board[row][column] = 1;
for(int i = 0; i < 8; i++){
if(i != column){
tmp_board[row][i] = -1;
}
}
for(int i = 0; i < 8; i++){
if(i != row){
tmp_board[i][column] = -1;
}
}
for(int r = row-1,c = column+1;r >= 0 && c <= 7; r--,c++){
tmp_board[r][c] = -1;
}
for(int r = row+1,c = column+1;r <= 7 && c <= 7; r++,c++){
tmp_board[r][c] = -1;
}
for(int r = row-1,c = column-1;r >= 0 && c >= 0; r--,c--){
tmp_board[r][c] = -1;
}
for(int r = row+1,c = column-1;r <= 7 && c >= 0; r++,c--){
tmp_board[r][c] = -1;
}
}
void func(int tmp_board[8][8],int num_of_putQ){
if(num_of_putQ == 8){
for(int i = 0; i < 8; i++){
for(int k = 0; k < 8; k++){
final_board[i][k] = tmp_board[i][k];
}
}
}else{
int row,column,count = 0;
for(row = 0; row < 8; row++){
for(column = 0; column < 8; column++){
if(tmp_board[row][column] == 0){
count++;
}
}
if(count > 0) break;
}
if(row == 8 && column == 8){
//Do nothing
}else{
int tri_board[count][8][8];
int index = 0;
for(int a = 0; a < count; a++){
for(int b = 0; b < 8; b++){
for(int c = 0; c < 8; c++){
tri_board[a][b][c] = tmp_board[b][c];
}
}
}
for(int i = 0; i < 8; i++){
if(tmp_board[row][i] == 0){
deletePossibility(tri_board[index],row,i);
func(tri_board[index++],num_of_putQ+1);
}
}
}
}
}
int main(){
int k,row,colum;
int tmp_board[8][8];
for(int i = 0; i < 8; i++){
for(int k = 0; k < 8; k++){
tmp_board[i][k] = 0;
}
}
scanf("%d",&k);
for(int i = 0; i < k; i++){
scanf("%d %d",&row,&colum);
deletePossibility(tmp_board,row,colum);
}
func(tmp_board,k);
for(int i = 0; i < 8; i++){
for(int p = 0; p < 8; p++){
if(final_board[i][p] == 1){
printf("Q");
}else{
printf(".");
}
}
printf("\n");
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i,ini,n) for(int i=ini;i<n;i++)
#define _rep(i,ini,n) for(int i=ini;i>=n;i--)
int main(){
int n;
cin>>n;
vector<pair<int,int>> a,b;
rep(i,0,n){
int x,y;
cin>>x>>y;
a.push_back(make_pair(y,x));
}
rep(i,0,n){
int x,y;
cin>>x>>y;
b.push_back(make_pair(x,y));
}
sort(a.begin(),a.end());
sort(b.begin(),b.end());
rep(i,0,n){
int x,y;
x=a.at(i).second;
y=a.at(i).first;
a.at(i)=make_pair(x,y);
}
int numPair=0;
rep(i,0,n){ //blue
_rep(j,n-1,0){ //red
if(a.at(j).first<b.at(i).first && a.at(j).second<b.at(i).second){
numPair++;
a.at(j)=make_pair(2*n,2*n);
break;
}
}
}
cout<<numPair<<endl;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n); i++)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
using P = pair<int, int>;
const int INF = 1e9;
const long long LINF = 1e18;
const long long MOD = 1e9 + 7;
signed main(){
string s;
cin >> s;
cout << s.substr(0, s.size() - 8) << endl;
return 0;
} | #include <iostream>
using namespace std;
int main()
{
string name;
int count = 0;
getline(cin,name);
/*if (name == "FESTIVAL")
{
cout << "Yes" << endl;
}*/
if(name[name.length()-1] == 'L')
{
for(int i = 0; i < name.length()-8; i++)
{
cout << name[i];
}
}
cout << endl;
}
| 1 |
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int w,h;
vector<vector<int> > b;
int sx,sy,gx,gy;
int ans;
void test(int x,int y,int n){
int bx,by;
int xx=x,yy=y;
if(++n>=ans) return;
while(b[y][x+1]==0){
x++;
if(x==gx&&y==gy){
ans=n;
return;
}
}
if((x!=xx)&&(x+1!=w-1)){
bx=x+1; by=y;
b[by][bx]=0;
test(x,y,n);
b[by][bx]=1;
}
x=xx;y=yy;
while(b[y][x-1]==0){
x--;
if(x==gx&&y==gy){
ans=n;
return;
}
}
if(x!=xx&&x-1!=0){
bx=x-1; by=y;
b[by][bx]=0;
test(x,y,n);
b[by][bx]=1;
}
x=xx;y=yy;
while(b[y+1][x]==0){
y++;
if(x==gx&&y==gy){
ans=n;
return;
}
}
if(y!=yy&&y+1!=h-1){
bx=x; by=y+1;
b[by][bx]=0;
test(x,y,n);
b[by][bx]=1;
}
x=xx;y=yy;
while(b[y-1][x]==0){
y--;
if(x==gx&&y==gy){
ans=n;
return;
}
}
if(y!=yy&&y-1!=0){
bx=x; by=y-1;
b[by][bx]=0;
test(x,y,n);
b[by][bx]=1;
}
x=xx;y=yy;
}
int main(){
while(cin>>w>>h,w+h){
w+=2;h+=2;
ans=11;
b=vector<vector<int> >(h+2,vector<int>(w+2,1));
for(int i=1;i<h-1;i++){
for(int j=1;j<w-1;j++){
cin >> b[i][j];
if(b[i][j]==2){
b[i][j]=0;
sx=j;sy=i;
}else if(b[i][j]==3){
b[i][j]=0;
gx=j;gy=i;
}
}
}
test(sx,sy,0);
if(ans==11) cout << -1 << endl;
else cout << ans << endl;
}
return 0;
} | #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
#define HMAX 20
#define WMAX 20
using namespace std;
int w,h;
int dd[5]={0,1,0,-1,0};
void dfs_aux(int sx, int sy, int map[WMAX][HMAX],int *min,int count){
rep(r,4){
int nx=sx,ny=sy;
if(map[nx+dd[r]][ny+dd[r+1]]==1)
continue;
while(0<=nx+dd[r] && nx+dd[r]<w && 0<=ny+dd[r+1] && ny+dd[r+1]<h){
nx+=dd[r];
ny+=dd[r+1];
if(map[nx][ny]==3)
if(*min > count){
*min = count;
break;
}
if(map[nx][ny]==1){
break;
}
}
if(map[nx][ny]==1 && count <10){
map[nx][ny]=0;
nx-=dd[r];ny-=dd[r+1];
dfs_aux(nx,ny,map,min,count+1);
nx+=dd[r];ny+=dd[r+1];
map[nx][ny]=1;
}
}
}
int dfs(int sx, int sy, int map[WMAX][HMAX]){
int min=11;
dfs_aux(sx,sy,map,&min,1);
if(min>10)
return -1;
return min;
}
int main(){
for(;;){
scanf("%d %d",&w,&h);
if(!(w+h)) break;
int map[WMAX][HMAX],sx,sy;
rep(i,h){
rep(j,w){
scanf("%d",&map[j][i]);
if(map[j][i]==2){
sx=j;
sy=i;
}
}
}
printf("%d\n",dfs(sx,sy,map));
}
} | 1 |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define mp make_pair
#define PI pair<ll,ll>
#define poly vector<ll>
#define mem(a) memset((a),0,sizeof(a))
#define For(i,l,r) for(int i=(int)(l);i<=(int)(r);i++)
#define Rep(i,r,l) for(int i=(int)(r);i>=(int)(l);i--)
#define pb push_back
#define fi first
#define se second
#define SZ(x) ((int)(x.size()))
inline char gc(){
static char buf[100000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
#define gc getchar
inline ll read(){
ll x = 0; char ch = gc(); bool positive = 1;
for (; !isdigit(ch); ch = gc()) if (ch == '-') positive = 0;
for (; isdigit(ch); ch = gc()) x = x * 10 + ch - '0';
return positive ? x : -x;
}
inline void write(ll a){
if(a<0){
a=-a; putchar('-');
}
if(a>=10)write(a/10);
putchar('0'+a%10);
}
inline void writeln(ll a){write(a); puts("");}
inline void wri(ll a){write(a); putchar(' ');}
ll rnd(){
ull ans=0;
For(i,0,4)ans=ans<<15^rand();
return ans%((ull)1<<63);
}
const int N=205;
ll b[N],a[63];
char ch[N];
int main(){
#ifdef Brollan
freopen("1.in","r",stdin);
#endif
int T=read();
while(T--){
int n=read();
For(i,1,n)b[i]=read();
scanf("%s",ch+1);
mem(a);
int f=0;
Rep(i,n,1){
ll x=b[i];
Rep(j,60,0)if(x>>j&1){
if(a[j])x^=a[j];
else{
if(ch[i]=='0')a[j]=x;
else f=1; break;
}
}
}
writeln(f);
}
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> v1;
typedef vector<v1> v2;
typedef vector<v2> v3;
typedef unordered_map<ll, unordered_map<ll, ll>> graph;
const ll INF = 1ll << 50;
const ll mod = 1000000007;
ll n;
ll query(ll x){
cout << x << endl;
cout << flush;
string s;
cin >> s;
if(s[0] == 'V') exit(0);
if(s[0] == 'M') return 0;
if(s[0] == 'F') return 1;
return -1;
}
int main(){
cin >> n;
ll left_state = query(0);
// ll right_state = query(n-1);
ll l = 0;
ll r = n;
for(ll i = 0;i < 19;i++){
ll x = (l+r)/2;
cerr << i << " " << l << " " << r << " " << x << endl;
ll ret = query(x);
if((x % 2) == ((left_state+ret)%2)){
l = x;
}else{
r = x;
}
}
}
| 0 |
#include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define rrep(i,n) for(int i=1;i<=n;i++)
#define drep(i,n) for(int i=n;i>=0;i--)
#define INF 100000005
#define MAX 100000001
#define dmp make_pair
#define dpb push_back
#define fi first
#define se second
using namespace std;
//__gcd(a,b), __builtin_popcount(a);
int n, m, a[10], ans;
map<int,int> mp;
int solve(int tmp[4], int k, int sum, int p){
int t, u[4];
rep(i,4)u[i] = tmp[i];
u[k++] = p;
if(a[p] >= 10)sum = sum*100+a[p];
else sum = sum*10+a[p];
if(m == k){
if(mp[sum]==0)ans++;
mp[sum] = 1;
return 0;
}
for(int i = 0;i < n;i++){
t = 1;
rep(j,k)if(u[j] == i)t = 0;
if(t)solve(u, k, sum, i);
}
return 0;
}
int main(){
int u[4] = {0,0,0,0};
while(1){
ans = 0;
mp.clear();
scanf("%d%d", &n, &m);
if(!n)break;
rep(i,n)scanf("%d", &a[i]);
for(int i = 0;i < n;i++){
solve(u, 0, 0, i);
}
printf("%d\n", ans);
}
return 0;
} | #include<stdio.h>
#include<string.h>
#include<math.h>
int value( char c )
{
switch( c ){
case 'I':
return 1;
break;
case 'V':
return 5;
break;
case 'X':
return 10;
break;
case 'L':
return 50;
break;
case 'C':
return 100;
break;
case 'D':
return 500;
break;
case 'M':
return 1000;
break;
}
}
int main()
{
char str[101];
while( scanf( "%s", str ) != EOF ){
int ans = 0;
for( int i = 1; i <= strlen( str ); i++ ){
if( value( str[i-1] ) >= value( str[i] ) ){
ans += value( str[i-1] );
}
else{
ans += value( str[i] ) - value( str[i-1] );
i++;
}
}
printf( "%d\n", ans );
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
long long n;
string s,sw = "SW";
string solve();
string check(string now);
int main() {
cin >> n >> s;
cout << solve() << endl;
return 0;
}
string solve(){
string ans;
for(int i = 0;i < 4;++i){
string now;
now += sw[i % 2];
now += sw[i / 2];
ans = check(now);
if(ans.size() == n)return ans;
}
return "-1";
}
string check(string now){
for(int i = 2;i < n;++i){
if(now[i-1] == 'S'){
if(s[i-1] == 'o')now += now[i-2];
else now += now[i-2] == 'S'? 'W':'S';
}
else {
if(s[i-1] != 'o')now += now[i-2];
else now += now[i-2] == 'S'? 'W':'S';
}
}
bool ch = 1;
if(now[0] == 'S'){
if(now[1] == now[n-1] && s[0] != 'o')ch = 0;
if(now[1] != now[n-1] && s[0] == 'o')ch = 0;
}
else {
if(now[1] == now[n-1] && s[0] == 'o')ch = 0;
if(now[1] != now[n-1] && s[0] != 'o')ch = 0;
}
if(now[n-1] == 'S'){
if(now[n-2] == now[0] && s[n-1] != 'o')ch = 0;
if(now[n-2] != now[0] && s[n-1] == 'o')ch = 0;
}
else {
if(now[n-2] == now[0] && s[n-1] == 'o')ch = 0;
if(now[n-2] != now[0] && s[n-1] != 'o')ch = 0;
}
if(ch)return now;
return "";
} | #include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
int main() {
int n,p;
while (cin >> n >> p && n != 0 && p != 0) {
vector<int> a(n, 0);
int flag = n;
int winner;
int i = 0;
while (1) {
if (p != 0) {
if (a[i % n] == 0) flag--;
a[i % n] += 1;
p--;
} else if (a[i % n] != 0) {
flag++;
p += a[i % n];
a[i % n] = 0;
}
if (p == 0 && flag == n - 1) {
winner = i % n;
break;
}
i++;
}
cout << winner << endl;
}
return 0;
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
const int maxn=100010;
pair<int,int> p[maxn];
int n;
int L1,R1,L2,R2;
int L[maxn],R[maxn];
void solve(){
scanf("%d",&n);
for(int i=1;i<=n;++i){
scanf("%d%d",&p[i].first,&p[i].second);
}
sort(p+1,p+n+1);
L1=p[1].first;R1=p[1].second;
L[n]=p[n].first;R[n]=p[n].second;
for(int i=n-1;i>1;--i){
R[i]=min(R[i+1],p[i].second);
L[i]=max(L[i+1],p[i].first);
}
int ans=max(R1-L1+1,0)+max(R[2]-L[2]+1,0),res=0;
for(int i=3;i<=n;++i){
L1=max(L1,p[i-1].first);
R1=min(R1,p[i-1].second);
ans=max(max(R1-L1+1,0)+max(R[i]-L[i]+1,0),ans);
}
for(int i=1;i<=n;++i)res=max(res,p[i].second-p[i].first+1);
int maxl=INT_MIN,minr=INT_MAX;
for(int i=1;i<=n;++i){
maxl=max(maxl,p[i].first);
minr=min(minr,p[i].second);
}
res+=max(0,minr-maxl+1);
printf("%d\n",max(ans,res));
}
int main(){
solve();
return 0;
} | /*
* @Author: RBQRBQ
* @Date: 2020-04-05 15:08:15
* @LastEditors: RBQRBQ
* @LastEditTime: 2020-04-05 19:16:50
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<ll,ll> pii;
namespace IO{
template<typename T>inline void read(T &x){
x=0;ll f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
while(isdigit(ch)){x=x*10+ch-48;ch=getchar();}
x=x*f;
}
}
using namespace IO;
vector<pii> pp;
vector<pii> T;
ll mxL,minR,mxR,minL;
int main()
{
int N;
cin>>N;
mxL=mxR=-1;
minL=minR=10000000000ll;
int P,Q;
for(int i=1;i<=N;i++)
{
ll x,y;
read(x),read(y);
y++;
pp.push_back(make_pair(x,y));
if(x>mxL)
{
mxL=x;
P=i;
}
if(minR>y)
{
minR=y;
Q=i;
}
}
ll ans1=0,ans2=0;
minL=0;
for(int i=1;i<=N;i++)
{
minL=max(minL,pp[i-1].second-pp[i-1].first);
}
ans1=max((minL),0ll)+max((minR-mxL),0ll);
int ca=1;
for(auto p:pp)
{
if(ca!=P&&ca!=Q)
T.push_back(make_pair(max(p.second-mxL,0ll),max(minR-p.first,0ll)));
ca++;
}
sort(T.begin(),T.end());
ll P1=pp[P-1].second-pp[P-1].first;
ll Q1=pp[Q-1].second-pp[Q-1].first;
ll x1=0,x2=Q1;
// for(auto p:T)
// {
// cout<<p.first<<" "<<p.second<<endl;
// }
for(int i=0;i<T.size();i++)
{
x1=min(T[i].first,P1);
ans2=max(ans2,x1+x2);
x2=min(T[i].second,x2);
}
ans2=max(ans2,P1+x2);
cout<<max(ans1,ans2);
} | 1 |
//hate penalty
#include <bits/stdc++.h>
using namespace std;
int dsu[200001], dsu2[200001];
map <pair<int, int> ,int> mp;
int find(int x){
if (dsu[x]) return dsu[x]=find(dsu[x]);
return x;
}
int find2(int x){
if (dsu2[x]) return dsu2[x]=find2(dsu2[x]);
return x;
}
int main(){
int n, k, l;
cin >> n >> k >> l;
int x, y;
for (int i=1; i<=k; i++){
cin >> x >> y;
if (find(x)!=find(y)){
dsu[find(x)]=find(y);
}
}
for (int i=1; i<=l; i++){
cin >> x >> y;
if (find2(x)!=find2(y)){
dsu2[find2(x)]=find2(y);
}
}
for (int i=1; i<=n; i++){
mp[make_pair(find(i),find2(i))]++;
}
for (int i=1; i<n; i++){
cout << mp[make_pair(find(i), find2(i))] << " ";
}
cout << mp[make_pair(find(n), find2(n))] << "\n";
}
| #include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include<algorithm>
#include <iomanip>
#include <math.h>
#include <iostream>
#include <bitset>
#include <map>
#define db double
#define vi vector<int>
#define pb push_back
#define fi first
#define se second
#define ll long long
#define ld long double
#define maxn 5005
#define mod 998244353
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vll vector<ll>
#define vpi vector<pii>
#define vpll vector<pll>
#define vv(z,a,b) vector<vector<int>>z(a,vector<int>(b))
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(c) begin(c),end(c)
#define pp pair<int,int>
#define fix(n) fixed << setprecision(n)
#define gmax(a,b) __gcd(a,b)
#define gmin(a,b) a/__gcd(a,b)*b
using namespace std;
//obj;
class binary{
public:
bool isOK(int index, int key,vi a) {
if (a[index] >= key) return true;
else return false;
}
int binary_search(int key,vi a) {
int ng = -1; //「index = 0」が条件を満たすこともあるので、初期値は -1
int ok = (int)a.size(); // 「index = a.size()-1」が条件を満たさないこともあるので、初期値は a.size()
/* ok と ng のどちらが大きいかわからないことを考慮 */
while (abs(ok - ng) > 1) {
int mid = (ok + ng) / 2;
if (isOK(mid, key,a)) ok = mid;
else ng = mid;
}
return ok;
}
}obj;
int main(){
int n,x,t;
cin>>n>>x>>t;
int b=n%x;
int a=n/x;
if(b!=0)a++;
int ans=t*a;
cout<<ans<<endl;
return 0;
}
| 0 |
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>
#include <cstring>
#include <cassert>
using namespace std;
#define SZ(x) (int)(x.size())
#define REP(i, n) for(int i=0;i<n;++i)
#define FOR(i, a, b) for(int i=a;i<b;++i)
#define RREP(i, n) for(int i=n-1;i>=0;--i)
#define RFOR(i, a, b) for(int i=b-1;i>=a;--i)
#define all(x) begin(x),end(x)
#define dump(x) cerr<<#x<<" = "<<(x)<<endl
#define debug(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<< endl;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<int, int>;
const double eps = 1e-8;
const ll MOD = 1000000007;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;
template<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 (b < a) {
a = b;
return true;
}
return false;
}
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> p) {
os << p.first << ":" << p.second;
return os;
}
template<class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
os << "[";
REP(i, SZ(v)) {
if (i) os << ", ";
os << v[i];
}
return os << "]";
}
// edit
int N;
vector<ll> A;
vector<vector<ll>> dp;
vector<vector<bool>> vis;
ll dfs(int n, int c) {
if (vis[n][c]) return dp[n][c];
if (n == 0) {
if (c <= 1) {
return 0;
} else {
return -LINF;
}
}
if (n == 1) {
if (c == 0) {
return A[0];
} else if (c <= 2) {
return 0;
} else {
return -LINF;
}
}
vis[n][c] = true;
ll &ret = dp[n][c];
ret = dfs(n - 2, c) + A[n - 1];
if (c >= 1) {
chmax(ret, dfs(n - 1, c - 1));
}
return ret;
}
void solve() {
cin >> N;
A.resize(N);
REP(i, N) cin >> A[i];
dp.resize(N + 1, vector<ll>(3));
vis.resize(N + 1, vector<bool>(3));
ll ans = dfs(N, 1 + (N + 0) % 2);
cout << ans << endl;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
// std::ifstream in("input.txt");
// std::cin.rdbuf(in.rdbuf());
solve();
return 0;
}
| #include<bits/stdc++.h>
#define ll long long
const ll mod = 1000000007 ;
using namespace std ;
int main()
{
int n;
cin>>n;
int A[n];
int cnt = 0 ;
for(int i=0 ; i<n ; i++)
cin>>A[i];
for(int i=0 ; i<n ; i++)
{
for(int j=i+1 ; j<n ; j++)
{
for(int k=j+1 ; k<n ; k++)
{
if(A[i]+A[j]>A[k] && A[k]+A[j]>A[i] && A[i]+A[k]>A[j] && A[i]!=A[j] && A[j]!=A[k] && A[k]!=A[i])
cnt++ ;
}
}
}
cout<<cnt;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef pair<ll,ll> l_l;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
const int INF=1001001000;
const int mINF=-1001001000;
const ll LINF=1010010010010010000;
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;
}
//グリッド:(典型)dp,dfs,bfs,最短経路,その他
int main(){
int m;cin>>m;
vl d(m),c(m);
ll D=0,S=0;
rep(i,m)cin>>d[i]>>c[i];
rep(i,m){
D+=c[i];
S+=d[i]*c[i];
}
cout<<(D-1)+(S-1)/9<<endl;
} | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#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, m, n) for (ll i = m; i < n; ++i)
#define FORR(i, m, n) for (ll i = m; i >= n; --i)
#define ALL(v) (v).begin(),(v).end()
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const ll INF=1LL<<60;
const int inf=(1<<30)-1;
const int mod=1e9+7;
int dx[8]={1,0,-1,0,-1,-1,1,1};
int dy[8]={0,1,0,-1,-1,1,-1,1};
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
ll n,a,b,c,d,e;cin >> n >> a >> b >> c >> d >> e;
ll m=min({a,b,c,d,e});
cout << 4+(n+m-1)/m << endl;
} | 0 |
#include <bits/stdc++.h>
#define enter putchar('\n')
#define space putchar(' ')
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
map<pii, int> M;
vector<pii> E[500005];
set<pii> s;
int n, m, ncnt, d[500005];
template <class T> int read(T &x) {
x = 0; int f = 1; char c = getchar();
while (c > -1 && c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); }
if (c == -1) return 0;
while (c >= '0' && c <= '9') { x = x * 10 + c - 48; c = getchar(); }
return x *= f, 1;
}
template <class T> void write(T x) {
if (x < 0) x = -x, putchar('-');
if (x > 9) write(x / 10);
putchar(x % 10 + 48);
}
int main() {
while (read(n), read(m)) {
M.clear();
ncnt = n;
for (int i = 1; i <= n; ++i)
E[i].clear();
for (int i = 1; i <= m; ++i) {
int u, v, c;
read(u), read(v), read(c);
if (!M[mp(u, c)]) {
M[mp(u, c)] = ++ncnt;
E[ncnt].clear();
E[ncnt].pb(mp(u, 1));
E[u].pb(mp(ncnt, 1));
}
if (!M[mp(v, c)]) {
M[mp(v, c)] = ++ncnt;
E[ncnt].clear();
E[ncnt].pb(mp(v, 1));
E[v].pb(mp(ncnt, 1));
}
E[M[mp(u, c)]].pb(mp(M[mp(v, c)], 0));
E[M[mp(v, c)]].pb(mp(M[mp(u, c)], 0));
}
fill(d, d + ncnt + 1, -1);
d[1] = 0;
s.insert(mp(d[1], 1));
while (!s.empty()) {
int u = s.begin() -> se;
s.erase(s.begin());
for (int i = 0; i < E[u].size(); ++i) {
int v = E[u][i].fi;
if (d[v] > d[u] + E[u][i].se || d[v] == -1) {
s.erase(mp(d[v], v));
d[v] = d[u] + E[u][i].se;
s.insert(mp(d[v], v));
}
}
}
write(d[n] >> 1), enter;
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define reps(i,f,n) for(int i=f; i<n; ++i)
#define rep(i,n) reps(i,0,n)
int main()
{
int m, a, b;
while(cin >> m >> a >> b, m){
vector<int> pt(m);
for(int i=0; i<m; ++i){
cin >> pt[i];
}
pair<int, int> ans{-1, -1};
rep(p, 10000){
vector<int> ok, ng;
rep(i, m){
if(pt[i] < p) ng.push_back(pt[i]);
else ok.push_back(pt[i]);
}
int n = ok.size();
if(a <= n && n <= b){
int gap = *min_element(ok.begin(), ok.end()) - *max_element(ng.begin(), ng.end());
ans = max(ans, {gap, ok.size()});
}
}
cout << ans.second << endl;
}
} | 0 |
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>
#include <cstring>
#define rep(i, m, n) for(int i=int(m);i<int(n);i++)
#define all(c) begin(c),end(c)
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; }
//改造
typedef long long int ll;
using ll = long long int;
using ull = long long unsigned int;
using Int = long long int;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0 //デバッグする時1にしてね
#define dump(x) cerr << #x << " = " << (x) << endl
#define MOD 1000000007
//ここから編集する
class Solve {
public:
void solve() {
Int N, M;
cin >> N >> M;
vector<Int> A(M);
int ki = 0;
for (int i = 0; i < M; ++i) {
cin >> A[i];
if (A[i] % 2) ki++;
}
if (ki > 2) {
cout << "Impossible" << endl;
return;
}
sort(all(A), [](Int a, Int b) {
return a % 2 > b % 2;
});
if (A.size() > 1 && A[1] % 2 == 1) {
swap(A[1], A.back());
}
vector<Int> B = A;
if (B.size() == 1) {
B.push_back(1);
B[0]--;
if (B[0] == 0) {
swap(B[0], B[1]);
}
}
B[0]--;
B.back()++;
if (B[0] == 0) {
B.erase(B.begin());
}
if (B.back() == 0) {
B.pop_back();
}
for (int i = 0; i < M; ++i) {
cout << A[i] << " ";
}
cout << endl;
cout << B.size() << endl;
for (auto e : B) {
cout << e << " ";
}
cout << endl;
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
Solve().solve();
return 0;
}
| #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define Rep(i, n) for(int i=0;i<n;++i)
#define rep(i, n) for(int i=1;i<=n;++i)
#define ll long long int
#define INF 1e9
#define LINF 1e18
#define MOD 1000000007
#define PI 3.14159265359
#define EPS 1e-10
#define All(a) (a).begin(), (a).end()
using namespace std;
using P = pair<ll, ll>;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
/**
* Usage:
*
* G[from].push_back(Edge(to, weight));
* using Graph = vector<vector<Edge>>;
*
*/
struct Edge {
int to;
ll weight;
Edge(int t, ll w) : to(t), weight(w) { }
};
/**
* Usage:
*
* chmin(old, new);
* chmax(old, new);
*
*/
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;
}
template<typename T>
void printv(vector<T>& v) {
if (v.size() == 0) {
cout << "\n";
return;
}
Rep(i, v.size()) {
cout << v[i] << ((i + 1 < v.size()) ? ' ' : '\n');
}
}
int H, W;
char maze[1100][1100];
int d[1100][1100];
int main () {
cin >> H >> W;
queue<P> que;
Rep(i, H) {
Rep(j, W) {
cin >> maze[i][j];
if (maze[i][j] == '#') que.emplace(i, j);
}
}
int ans = 0;
while (!que.empty()) {
int n = que.size();
queue<P> new_que;
Rep(i, n) {
P p = que.front(); que.pop();
Rep(k, 4) {
int new_y = p.first + dy[k];
int new_x = p.second + dx[k];
if (new_y >= 0 & new_y < H & new_x >= 0 & new_x < W) {
if (maze[new_y][new_x] == '.') {
new_que.emplace(new_y, new_x);
//cout << new_y << new_x << "\n";
maze[new_y][new_x] = '#';
}
}
}
}
que = new_que;
++ans;
//cout << ans << "\n";
}
cout << ans -1<< "\n";
} | 0 |
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
using namespace std;
using Graph = vector<vector<int>>;
#define MOD 1000000007
#define MOD2 998244353
#define INF ((1<<30)-1)
#define LINF (1LL<<60)
#define EPS (1e-10)
typedef long long ll;
typedef pair<ll, ll> P;
int main(){
int n;
cin >> n;
vector<ll> ab;
ll cnt = 0;
rep(i, n){
int a, b;
cin >> a >> b;
ab.push_back(a+b);
cnt += b;
}
sort(ab.begin(), ab.end(), greater<ll>());
ll ans = 0;
rep(i, (n+1)/2){
ans += ab[2 * i];
}
ans -= cnt;
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define FASTIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define inf 1e18
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define mod 1000000007
#define pb push_back
#define all(s) s.begin(),s.end()
#define pie 3.14159265358979323846
#define fr(i,a,b) for(ll i=a;i<b;i++)
#define frr(i,a,b) for(ll i=b-1;i>=a;i--)
#define gcd __gcd
#define con continue
#define pii pair<ll,ll>
signed main(){
FASTIO;
ll tt=1;
//cin>>tt;
while(tt--){
ll n,ans=0,x,mx=0;cin>>n;
for(ll i=0;i<n;i++){
cin>>x;
if(mx>x)ans+=mx-x;
mx=max(mx,x);
}
cout<<ans<<"\n";
}
}
| 0 |
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define FASTIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define inf 1e9
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define mod 1000000007
#define pb push_back
#define all(s) s.begin(),s.end()
#define pie 3.14159265358979323846
#define fr(i,a,b) for(ll i=a;i<b;i++)
#define frr(i,a,b) for(ll i=b-1;i>=a;i--)
#define gcd __gcd
#define con continue
#define pii pair<ll,ll>
const ll N=5e5+6;
ll seg[4*N];
void build(ll s,ll e,ll idx,ll a[]){
if(s==e){
//cout<<s<<" ";
seg[idx]=a[s];
return;
}
ll mid=(s+e)/2;
build(s,mid,2*idx+1,a);
build(mid+1,e,2*idx+2,a);
seg[idx]=seg[2*idx+1]+seg[2*idx+2];
return ;
}
void update(ll s,ll e,ll idx,ll pos,ll val){
if(s==e){
seg[idx]=val;
return;
}
ll mid=(s+e)/2;
if(pos<=mid)
update(s,mid,2*idx+1,pos,val);
else
update(mid+1,e,2*idx+2,pos,val);
seg[idx]=seg[2*idx+1]+seg[2*idx+2];
return ;
}
ll query(ll s,ll e,ll idx,ll l,ll r){
if(s>=l&&e<=r)return seg[idx];
if(s>r||e<l)return 0;
ll mid=(s+e)/2;
return query(s,mid,2*idx+1,l,r)+query(mid+1,e,2*idx+2,l,r);
}
signed main(){
FASTIO;
ll tt=1;
//cin>>tt;
while(tt--){
ll n,q,x,l,r;cin>>n>>q;
ll a[n];
for(ll i=0;i<n;i++){
cin>>a[i];
}
build(0,n-1,0,a);
while(q--){
cin>>x;
if(x){
cin>>l>>r;
cout<<query(0,n-1,0,l,r-1)<<"\n";
}
else{
cin>>l>>x;
a[l]+=x;
update(0,n-1,0,l,a[l]);
}
}
}
}
| #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define dbg(...) do{cerr<<__LINE__<<": ";dbgprint(#__VA_ARGS__, __VA_ARGS__);}while(0);
using namespace std;
namespace std{template<class S,class T>struct hash<pair<S,T>>{size_t operator()(const pair<S,T>&p)const{return ((size_t)1e9+7)*hash<S>()(p.first)+hash<T>()(p.second);}};template<class T>struct hash<vector<T>>{size_t operator()(const vector<T> &v)const{size_t h=0;for(auto i : v)h=h*((size_t)1e9+7)+hash<T>()(i)+1;return h;}};}
template<class T>ostream& operator<<(ostream &os, const vector<T> &v){os<<"[ ";rep(i,v.size())os<<v[i]<<(i==v.size()-1?" ]":", ");return os;}template<class T>ostream& operator<<(ostream &os,const set<T> &v){os<<"{ "; for(const auto &i:v)os<<i<<", ";return os<<"}";}
template<class T,class U>ostream& operator<<(ostream &os,const map<T,U> &v){os<<"{";for(const auto &i:v)os<<" "<<i.first<<": "<<i.second<<",";return os<<"}";}template<class T,class U>ostream& operator<<(ostream &os,const pair<T,U> &p){return os<<"("<<p.first<<", "<<p.second<<")";}
void dbgprint(const string &fmt){cerr<<endl;}template<class H,class... T>void dbgprint(const string &fmt,const H &h,const T&... r){cerr<<fmt.substr(0,fmt.find(","))<<"= "<<h<<" ";dbgprint(fmt.substr(fmt.find(",")+1),r...);}
typedef long long ll;typedef vector<int> vi;typedef pair<int,int> pi;const int inf = (int)1e9;const double INF = 1e12, EPS = 1e-9;
int main(){
cin.tie(0); cin.sync_with_stdio(0);
int h, w; cin >> h >> w;
vector<string> in(h); rep(i, h) cin >> in[i];
vector<vi> d1(h), d2(h - 1), dp(h - 1, vi(w - 1));
rep(i, h) rep(j, w - 1) d1[i].pb(in[i][j] != in[i][j + 1]);
rep(i, h - 1) rep(j, w - 1) d2[i].pb(d1[i][j] != d1[i + 1][j]);
//rep(i, h - 1) dbg(d2[i]);
ll ans = max(h, w);
rep(i, h - 1){
vi ls(w - 1), rs(w - 1), q;
rep(j, w - 1){
dp[i][j] = d2[i][j] ? 0 : (i ? dp[i - 1][j] : 0) + 1;
ls[j] = j;
while(q.size() && dp[i][q.back()] >= dp[i][j]){
ls[j] = ls[q.back()];
q.pop_back();
}
q.pb(j);
}
q.clear();
for(int j = w - 2; j >= 0; j--){
rs[j] = j;
while(q.size() && dp[i][q.back()] >= dp[i][j]){
rs[j] = rs[q.back()];
q.pop_back();
}
q.pb(j);
ans = max(ans, (rs[j] - ls[j] + 2ll) * (dp[i][j] + 1ll));
}
//dbg(i, ls);
}
cout << ans << endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int,int> ii;
int fact[2000005];
int mod = 998244353;
long long qexp(long long b, long long p){
long long res = 1;
while(p){
if(p&1) res = (res * b) % mod;
b = (b * b) % mod;
p >>= 1;
}
return res;
}
int inv(long long b){ return qexp(b, mod-2); }
int choose(int N, int C){
if(N < C) return 0;
int res = fact[N];
res *= inv(fact[C]);
res %= mod;
res *= inv(fact[N-C]);
res %= mod;
return res;
}
int split(int total, int N){ ///split total objects to N groups
return choose(N + total - 1, N - 1);
}
int ans = 0;
void fix(int &x){
x %= mod;
if(x < 0) x += mod;
}
signed main(){
ios_base::sync_with_stdio(false); cin.tie(0);
fact[0] = 1;
for(int i = 1;i <= 2000000;i++) fact[i] = (fact[i-1] * i) % mod;
int N, M; cin >> N >> M;
for(int ones = 0;ones <= M;ones++){
int zeros = N - ones;
int leftover = 3*M - ones;
if(leftover & 1) continue;
leftover /= 2; ///The number of 2s to assign elsewhere
int ways01 = choose(N, ones); ///ways to arrange the 0s and 1s
int total2 = split(leftover, N); ///total ways to assign
int oneTooBig = ones * split(leftover - M, N);
int zeroTooBig = zeros * split(leftover - M - 1, N);
total2 -= oneTooBig;
total2 -= zeroTooBig;
fix(total2);
ans += total2 * ways01;
fix(ans);
}
cout << ans;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int,int> pii;
#define fir first
#define sec second
#define rep(i,a,b) for (int i = (a) ; i <= (b) ; ++ i)
#define rrp(i,a,b) for (int i = (a) ; i >= (b) ; -- i)
#define gc() getchar()
template <typename tp>
inline void read(tp& x) {
x = 0; char tmp; bool key = 0;
for (tmp = gc() ; !isdigit(tmp) ; tmp = gc())
key = (tmp == '-');
for ( ; isdigit(tmp) ; tmp = gc())
x = (x << 3) + (x << 1) + (tmp ^ '0');
if (key) x = -x;
}
template <typename tp>
inline void ckmn(tp& x,tp y) {
x = x < y ? x : y;
}
template <typename tp>
inline void ckmx(tp& x,tp y) {
x = x < y ? y : x;
}
const int MOD = 998244353;
inline void Add(int& x,int y) {
x = x + y >= MOD ? x + y - MOD : x + y;
}
inline void Sub(int& x,int y) {
x = x - y < 0 ? x - y + MOD : x - y;
}
int power(int a,int b) {
int ret = 1;
while (b) {
if (b&1) ret = 1ll * ret * a % MOD;
a = 1ll * a * a % MOD;
b >>= 1;
}
return ret;
}
const int N = 3000000;
int jc[N], ijc[N], n, m, ans;
void init(int t) {
jc[0] = 1;
rep (i, 1, t) jc[i] = 1ll * jc[i-1] * i % MOD;
ijc[t] = power(jc[t], MOD - 2);
rrp (i, t-1, 0) ijc[i] = 1ll * ijc[i+1] * (i+1) % MOD;
}
int comb(int a,int b) {
if (a < b || b < 0) return 0;
return 1ll * jc[a] * ijc[b] % MOD * ijc[a-b] % MOD;
}
int main() {
read(n), read(m);
init(n + 3 * m);
rep (i, 0, min(n, m)) if ((m-i) % 2 == 0) {
Add(ans, 1ll * comb(n, i) * comb((3 * m - i) / 2 + n - 1, n - 1) % MOD);
if (i <= m-2)
Sub(ans, 1ll * n * comb(n-1,i) % MOD * comb((3*m-i-2*(m+1)) / 2 + n - 1, n - 1) % MOD);
if (i <= m)
Sub(ans, 1ll * n * comb(n-1,i-1) % MOD * comb((3*m-i-2*m)/2 + n-1, n-1) % MOD);
}
printf("%d\n", ans);
return 0;
}
| 1 |
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <functional>
#include <limits.h>
#include <set>
#include <map>
#include <tuple>
using namespace std;
#define ll long long
#define ull unsigned long long
#define rep(i,N) for (ll i=0;i<N;i++)
#define loop(i,N,M) for(ll i=N;i<M;i++)
#define MAX(v) *max_element(v.begin(),v.end())
#define MIN(v) *min_element(v.begin(),v.end())
#define SORTL2S(type,v) sort(v.begin(),v.end(),greater<type>())
#define SORTS2L(type,v) sort(v.begin(),v.end())
#define SORTMF(v,func) sort(v.begin(),v.end(),func)
#define MP(a,b) make_pair(a,b)
ll debugFlag = false;
template<typename T> void debug(T&);
template<typename T> void debug(T&, bool);
template<typename T> void debug(vector<T>&);
template<typename T> void debug(vector<T>&, bool);
template<typename T, typename U> void debug(pair<T, U>&);
template<typename T, typename U> void debug(pair<T, U>&, bool);
template<typename T>
void debug(T& obj) {
debug(obj, true);
}
template<typename T>
void debug(T& obj, bool lnFlag) {
if (!debugFlag) return;
cout << obj;
if (lnFlag) cout << endl;
}
template<typename T>
void debug(vector<T>& obj) {
debug(obj, true);
}
template<typename T>
void debug(vector<T>& obj, bool lnFlag) {
if (!debugFlag) return;
cout << "[";
rep(i, obj.size()) {
if (i != 0) cout << ",";
debug(obj[i], false);
}
cout << "]";
if (lnFlag) cout << endl;
}
template<typename T, typename U>
void debug(pair<T, U>& obj) {
debug(obj, true);
}
template<typename T, typename U>
void debug(pair<T, U>& obj, bool lnFlag) {
if (!debugFlag) return;
cout << "(";
debug(obj.first, false);
cout << ",";
debug(obj.second, false);
cout << ")";
if (lnFlag) cout << endl;
}
void solve() {
string S,T;
cin >> S >> T;
cout << T << S;
}
int main(int argc, char* argv[]) {
if (argc == 2 && string(argv[1]) == "debug") debugFlag = true;
solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//マクロ
//forループ関係
//引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか
//Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる
#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 FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--)
//xにはvectorなどのコンテナ
#define ALL(x) (x).begin(),(x).end() //sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size()) //sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x)) //最大値を求める
#define MIN(x) *min_element(ALL(x)) //最小値を求める
//定数
#define INF 1000000000000 //10^12:極めて大きい値,∞
#define MOD 1000000007 //10^9+7:合同式の法
#define MAXR 100000 //10^5:配列の最大のrange(素数列挙などで使用)
#define PI 3.14159265359 //πの値
//略記
#define PB push_back //vectorヘの挿入
#define MP make_pair //pairのコンストラクタ
#define F first //pairの一つ目の要素
#define S second //pairの二つ目の要素
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
string A, B; cin >> A >> B;
cout << B << A;
} | 1 |
#include <bits/stdc++.h>
using namespace std;
vector<int> primes={2,3,5,7};
int primes_extend(int calc_from, int calc_to);
int factoring(int n, map<int,int> &factors);
const int combination_max=100000+1000;
const int combination_modulo=1000000007;
vector<int64_t> factorials(combination_max);
vector<int64_t> factorials_inv(combination_max);
vector<int64_t> inverse(combination_max);
int combination_mod(int n, int k);
int prepare_combination_mod();
int main()
{
int n,m;
cin >> n >> m;
primes_extend(10,100);
primes_extend(100,10000);
primes_extend(10000,32767);
map<int,int> factors;
factoring(m,factors);
for (pair<int,int> p:factors)
{
// fprintf(stderr,"%d: %d\n",p.first,p.second);
}
prepare_combination_mod();
int64_t patterns=1;
for (pair<int,int> p:factors)
{
int c=combination_mod(p.second+n-1,n-1);
// fprintf(stderr,"%d: %d patterns\n",p.first,c);
patterns*=c;
patterns%=combination_modulo;
}
cout << patterns << endl;
}
int primes_extend(int calc_from, int calc_to)
{
for (int i=calc_from;i<=calc_to;++i)
{
int j;
int loop_max=sqrt(i);
for (j=0;(primes[j]<=loop_max)&&(j<primes.size());++j)
{
if ((i%primes[j])==0)
{
// not prime...
break;
}
}
if ((j>=primes.size())||(primes[j]>loop_max))
{
primes.push_back(i);
}
}
//fprintf(stderr,"count: %d\n",primes.size());
}
int factoring(int n, map<int,int> &factors)
{
int maxfactor=sqrt(n);
for (int i=0;(n>1)&&(primes[i]<=maxfactor);++i)
{
while ((n%primes[i])==0)
{
n/=primes[i];
factors[primes[i]]++;
}
}
if (n!=1)
{
factors[n]++;
}
return 0;
}
int prepare_combination_mod()
{
factorials[0]=1;
factorials[1]=1;
factorials_inv[0]=1;
factorials_inv[1]=1;
inverse[1]=1;
for (int i=2;i<combination_max;++i)
{
factorials[i]=factorials[i-1]*i%combination_modulo;
inverse[i]=combination_modulo - inverse[combination_modulo%i]*(combination_modulo/i)%combination_modulo;
factorials_inv[i]=factorials_inv[i-1]*inverse[i]%combination_modulo;
}
}
int combination_mod(int n, int k)
{
return factorials[n]*(factorials_inv[k]*factorials_inv[n-k]%combination_modulo)%combination_modulo;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int N, K, A = 1;
cin >> N >> K;
for (int i = 0; i < N; i++) {
if (A * 2 < A + K) A *= 2;
else {
for (; i < N; i++) A += K;
break;
}
}
cout << A << endl;
} | 0 |
#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
int v,e;
cin>>v>>e;
int g[v][v];
int INF=1LL<<55LL;
for(int i=0;i<v;i++)
for(int j=0;j<v;j++)
g[i][j]=(i!=j)*INF;
for(int i=0;i<e;i++){
int s,t,d;
cin>>s>>t>>d;
g[s][t]=d;
}
for(int k=0;k<v;k++)
for(int i=0;i<v;i++)
for(int j=0;j<v;j++)
g[i][j]=min(g[i][j],g[i][k]+g[k][j]);
for(int i=0;i<v;i++){
if(g[i][i]<0){
cout<<"NEGATIVE CYCLE"<<endl;
return 0;
}
}
for(int i=0;i<v;i++){
for(int j=0;j<v;j++){
if(j) cout<<" ";
if(g[i][j]>=INF/2) cout<<"INF";
else cout<<g[i][j];
}
cout<<endl;
}
return 0;
} | #include <stdio.h>
#include <limits.h>
int main(){
int n,m,s,i,j;
scanf("%d%d",&n,&m);
int e[m+1][3],dist[m+1],update=1,k,d[200][200];
//printf("xxx");
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(i==j)d[i][j]=0;
else d[i][j]=INT_MAX;
}
}
for(i=0;i<m;i++){
for(j=0;j<3;j++){
scanf("%d",&e[i][j]);
}
d[e[i][0]][e[i][1]]=e[i][2];
}
for(i=0;i<n;i++){
dist[i]=INT_MAX;
}
dist[s]=0;
for(i=0;i<n-1;i++){
update=0;
for(j=0;j<m;j++){
if(dist[e[j][0]]!=INT_MAX&&dist[e[j][1]]>dist[e[j][0]]+e[j][2]){
dist[e[j][1]]=dist[e[j][0]]+e[j][2];
update=1;
}
}
if(update==0)break;
}
for(j=0;j<m;j++){
if(dist[e[j][0]]!=INT_MAX&&dist[e[j][1]]>dist[e[j][0]]+e[j][2]){
printf("NEGATIVE CYCLE\n");
return 0;
}
}
for(k=0;k<n;k++){
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if((d[i][k]!=INT_MAX&&d[k][j]!=INT_MAX)&&d[i][j]>d[i][k]+d[k][j]){
d[i][j]=d[i][k]+d[k][j];
}
}
}
}
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(j>0)printf(" ");
if(d[i][j]==INT_MAX){
printf("INF");
}
else{
printf("%d",d[i][j]);
}
}
printf("\n");
}
return 0;
} | 1 |
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAX_N = 1005;
int c[MAX_N][MAX_N];
int main()
{
string X,Y;
cin >> X;
cin >> Y;
for(int i=0;i<=X.length();i++){
c[i][0] = i;
}
for(int j=0;j<=Y.length();j++){
c[0][j] = j;
}
for(int i=1;i<=X.length();i++){
for(int j=1;j<=Y.length();j++){
int p,q,r;
if(X[i-1] == Y[j-1]){
p = c[i-1][j-1];
}else{
p = c[i-1][j-1] + 1;
}
q = c[i][j-1] + 1;
r = c[i-1][j] + 1;
c[i][j] = min(min(p,q),r);
}
}
cout << c[X.length()][Y.length()] << "\n";
} | #include <bits/stdc++.h>
#define max 1000007
#define ll long long int
using namespace std;
vector <ll> primes;
void sieve()
{
bool isprime[max+1];
for(ll i=0;i<=max;i++) {
isprime[i] = true;
}
isprime[0]=false;
isprime[1]=false;
for(ll i=3;i*i<=(max);i=i+2)
{
if(isprime[i]) {
for (ll j = i*i; j <= max; j=j+i) {
isprime[j] = false;
}
}
}
primes.push_back(2);
for(ll i=3;i<max;i=i+2)
{
if(isprime[i])
primes.push_back(i);
}
}
int main() {
sieve();
ll n,d,b,i,flag,x;
scanf("%lld %lld",&d,&b);
n=__gcd(d%b,b);
set <int> s;
s.insert(1);
for(i=0;primes[i]*primes[i]<=n;i++)
{
while(n%primes[i]==0)
{
s.insert(primes[i]);
n=n/primes[i];
}
}
if(n>1)
s.insert(n);
printf("%lu",s.size());
return 0;
} | 0 |
#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>;
template<typename T> void chmax(T &a, T b) { a = max(a, b); }
template<typename T> void chmin(T &a, T b) { a = min(a, b); }
const ll INF = 1e15;
int main() {
ll x, y;
cin >> x >> y;
ll ans = INF;
if (x <= y) ans = min(ans, y - x);
if (-x <= y) ans = min(ans, y+x+1);
if (x <= -y) ans = min(ans, -y-x+1);
if (-x <= -y) ans = min(ans, -y+x+2);
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int a,b,c,k;
int64_t target;
int64_t remains;
cin >> a >> b >> c >> k;
target = max(a,max(b,c));
remains = a + b + c - target;
target = target * pow(2,k);
cout << target + remains << endl;
return 0;
}
| 0 |
#include <stdio.h>
#include <algorithm>
#define INF 100000000
using namespace std;
int n;
int graph[100][100];
void floyd(){
for(int k=0;k<n;k++)
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
graph[i][j]=min(graph[i][j],graph[i][k]+graph[k][j]);
}
int main(){
int k,i,j,a,b,c,d,e,x;
while(scanf("%d %d",&n,&k),n,k){
for(i=0;i<n;i++)
for(j=0;j<n;j++)
graph[i][j]=i==j?0:INF;
for(i=0;i<k;i++){
if (scanf("%d",&x),x){
scanf("%d %d %d",&c,&d,&e);
if (graph[--c][--d]>e){
graph[c][d]=graph[d][c]=e;
floyd();
}
}else{
scanf("%d %d",&a,&b);
if (graph[--a][--b]==INF)
printf("-1\n");
else
printf("%d\n",graph[a][b]);
}
}
}
return 0;
} | #include <iostream>
#include <vector>
#include <queue>
#include <utility>
#include <algorithm>
using namespace std;
struct edge{
int to,cost;
edge(){}
edge(int to,int cost):to(to),cost(cost){}
};
typedef pair<int,int> P;
int d[101];
const int INF=(int)1e9;
int main(void){
int n,k;
while(cin>>n>>k,!(n==0&&k==0)){
vector<edge> G[1000];
for(int i=0;i<k;i++){
int r,a,b,cost;
cin>>r;
if(r==1){
cin>>a>>b>>cost;
G[a].push_back(edge(b,cost));
G[b].push_back(edge(a,cost));
}
if(r==0){
cin>>a>>b;
priority_queue<P,vector<P>,greater<P> > que;
fill(d,d+101,INF);
d[a]=0;
que.push(P(0,a));
while(!que.empty()){
P p=que.top();
que.pop();
if(d[p.second]<p.first)continue;
for(int j=0;j<G[p.second].size();j++){
edge e=G[p.second][j];
if(d[e.to]>d[p.second]+e.cost){
d[e.to]=d[p.second]+e.cost;
que.push(P(d[e.to],e.to));
}
}
}
if(d[b]==(int)INF){
cout<<"-1"<<endl;
}else{
cout<<d[b]<<endl;
}
}
}
}
return 0;
} | 1 |
#include <bits/stdc++.h>
using namespace std;
#define modulo 1000000007
#define mod(mod_x) ((((long long)mod_x+modulo))%modulo)
#define Inf 10000000000000000
int main(){
int A,B,M;
cin>>A>>B>>M;
vector<int> a(A),b(B);
for(int i=0;i<A;i++)cin>>a[i];
for(int i=0;i<B;i++)cin>>b[i];
int ans = 1000000;
for(int i=0;i<M;i++){
int x,y,c;
cin>>x>>y>>c;
x--;y--;
ans = min(ans,a[x]+b[y]-c);
}
sort(a.begin(),a.end());
sort(b.begin(),b.end());
ans = min(ans,a[0]+b[0]);
cout<<ans<<endl;
return 0;
} | #include <iostream>
using namespace std;
struct Dice
{
Dice(int d[6]){
for(int i = 0; i < 6; ++i)
n[i] = d[i];
}
void toN(){ shift( n[0], n[1], n[5], n[4] ); }
void toS(){ shift( n[0], n[4], n[5], n[1] ); }
void toW(){ shift( n[0], n[2], n[5], n[3] ); }
void toE(){ shift( n[0], n[3], n[5], n[2] ); }
void shift( int& a, int& b, int& c, int& d ){
int t = a;
a = b;
b = c;
c = d;
d = t;
}
int n[6];
};
int main()
{
int i, d[6] = {};
for(i = 0; i < 6; ++i)
cin >> d[i];
Dice dice(d);
char c;
cin.get(c);
while( cin.get(c) ){
if( c == '\n' )
break;
switch(c)
{
case 'N': dice.toN(); break;
case 'S': dice.toS(); break;
case 'W': dice.toW(); break;
case 'E': dice.toE(); break;
}
}
cout << dice.n[0] << "\n";
return 0;
} | 0 |
#include <iostream>
#include<math.h>
#include<iomanip>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
int main()
{
int a=0,b=0,a1=0,b1=0,i=0,n=0,j=0;
for(i=1;;i++){
cin >> n;
if(n==0)break;
for(j=0;j<n;j++){
cin >> a >> b;
if(a>b){
a1+=a+b;
}
if(a<b){
b1+=a+b;
}
if(a==b){
a1+=a;
b1+=b;
}
}
cout << a1 << ' ' << b1 <<endl;
a1=b1=0;
}
//cout << b;
} | #include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define each(a, b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define pb push_back
#define show(x) cout <<#x<<" = "<<(x)<<endl
#define spair(p) cout <<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout <<" "<<kbrni;cout<<endl
using namespace std;
typedef pair<int,int>P;
template<class V> class segtree {
private:
int n;
vector<V> node, lazy;
vector<bool> lazyFlag;
public:
segtree(vector<V> v) {
int sz = (int)v.size();
n = 1;
while(n < sz){
n *= 2;
}
node.resize(2*n-1);
lazy.resize(2*n-1, 0);
lazyFlag.resize(2*n-1,false);
rep(i,sz){
node[i+n-1] = v[i];
}
for(int i=n-2; i>=0; i--){
node[i] = min(node[i*2+1],node[i*2+2]);
}
}
void eval(int k, int l, int r) {
if(lazyFlag[k]) {
node[k] = lazy[k];
if(r - l > 1) {
lazy[k*2+1] = lazy[k*2+2] = lazy[k];
lazyFlag[k*2+1] = lazyFlag[k*2+2] = true;
}
lazyFlag[k] = false;
}
}
void range(int a, int b, V x, int k=0, int l=0, int r=-1) {
if(r < 0) r = n;
eval(k, l, r);
if(b <= l || r <= a){
return;
}
if(a <= l && r <= b) {
lazy[k] = x;
lazyFlag[k] = true;
eval(k, l, r);
}
else {
range(a, b, x, 2*k+1, l, (l+r)/2);
range(a, b, x, 2*k+2, (l+r)/2, r);
node[k] = min(node[2*k+1],node[2*k+2]);
}
}
V query(int a, int b, int k=0, int l=0, int r=-1) {
if(r < 0) r = n;
eval(k, l, r);
if(b <= l || r <= a){
return INT_MAX;
}
if(a <= l && r <= b){
return node[k];
}
V vl = query(a, b, 2*k+1, l, (l+r)/2);
V vr = query(a, b, 2*k+2, (l+r)/2, r);
return min(vl,vr);
}
};
int main()
{
int n,q;
cin >> n >> q;
segtree<int> sg(vector<int>(n,INT_MAX));
rep(i,q){
int k;
cin >> k;
if(k){
int x;
cin >> x;
cout << sg.query(x,x+1) << "\n";
}else{
int x,y,z;
cin >> x >> y >> z;
sg.range(x,y+1,z);
}
}
} | 0 |
#include <stdio.h>
int main() {
char str[100];
scanf("%s", str);
if (str[3] == '7') {
str[3] = '8';
}
printf("%s\n", str);
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define int long long int
int32_t main()
{
string str;
cin>>str;
cout<<2018<<str.substr(str.find('7')+1)<<endl;
} | 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string A,B,C; cin >> A >> B >> C;
int a,b,c;
a = A.size();
b = B.size();
c = C.size();
char s,t,u,v;
s = A.at(a-1);
t = B.at(0);
u = B.at(b-1);
v = C.at(0);
if (s == t && v == u){
cout << "YES";
}
else cout << "NO";
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int N,M,X,Y;
cin >> N >> M >> X >> Y;
vector<int>x(N);
vector<int>y(M);
for(int i = 0; i < N; i++) {
cin >> x[i];
}
for(int i = 0; i < M; i++) {
cin >> y[i];
}
sort(x.begin(),x.end());
sort(y.begin(),y.end());
if(x[N-1] < y[0]) {
if(y[0] > X && x[N-1] < Y) {
cout << "No War" << endl;
return 0;
}
}
cout << "War" << endl;
} | 0 |
#include <iostream>
#include <cstdlib>
using namespace std;
typedef struct node {
int key;
struct node *parent;
struct node *left;
struct node *right;
} Tree;
Tree *root, *nil;
void insert(int key);
Tree *findNode(Tree *node, int key);
bool isFindTree(Tree *node, int key);
Tree *findMaximumTree(Tree *node);
Tree *treeSuccessor(Tree *node);
void treeDelete(Tree *z);
void preorder(Tree *node);
void inorder(Tree *node);
int main() {
int n, key, i;
string command;
cin >> n;
for (i = 0; i < n; i++) {
cin >> command;
if (command[0] == 'i') {
cin >> key;
insert(key);
}
else if (command[0] == 'd') {
cin >> key;
treeDelete(findNode(root, key));
}
else if (command[0] == 'f') {
cin >> key;
if (isFindTree(root, key)) {
cout << "yes" << endl;
} else {
cout << "no" << endl;
}
}
else if (command[0] == 'p') {
inorder(root); cout << endl;
preorder(root); cout << endl;
}
}
return 0;
}
void insert(int key) {
Tree *y = nil;
Tree *x = root;
Tree *z = (struct node*)malloc(sizeof(struct node));
z->key = key;
z->left = nil;
z->right = nil;
while (x != nil) {
y = x;
if (key < x->key) {
x = x->left;
} else {
x = x->right;
}
};
z->parent = y;
if (y == nil) {
root = z;
return;
}
if (key <= y->key) {
y->left = z;
} else {
y->right = z;
}
}
Tree *findNode(Tree *node, int key) {
Tree* target = node;
while (target != nil && target->key != key) {
if (key < target->key) {
target = target->left;
} else {
target = target->right;
}
}
return target;
}
bool isFindTree(Tree *node, int key) {
return findNode(node, key) != nil;
}
Tree *findMaximumTree(Tree *node) {
while (node->left != nil) {
node = node->left;
}
return node;
}
Tree *treeSuccessor(Tree *node) {
if (node->right != nil) {
return findMaximumTree(node->right);
}
Tree *y = node->parent;
while (y != nil && node == y->right) {
node = y;
y = y->parent;
}
return y;
}
void treeDelete(Tree *z) {
Tree *y, *key;
if (z->left == nil || z->right == nil) y = z;
else y = treeSuccessor(z);
if (y->left != nil) {
key = y->left;
} else {
key = y->right;
}
if (key != nil) {
key->parent = y->parent;
}
if (y->parent == nil) {
root = key;
} else {
if (y == y->parent->left) {
y->parent->left = key;
} else {
y->parent->right = key;
}
}
if (y != z) {
z->key = y->key;
}
}
void preorder(Tree *node) {
if (node == nil) return;
cout << " " << node->key;
preorder(node->left);
preorder(node->right);
}
void inorder(Tree *node) {
if (node == nil) return;
inorder(node->left);
cout << " " << node->key;
inorder(node->right);
}
| #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
#define N 600000
#define NIL -1
//array
int key[N];
int parent[N];
int leftc[N];
int rightc[N];
//root
int root=NIL;
//function insert
void insert(int z){
int x,y;
y=NIL;
x=root;
while(x!=NIL){
y=x;
if(key[z]<key[x]) x=leftc[x];
else x=rightc[x];
}
parent[z]=y;
if(y==NIL) root=z;
else if(key[z]<key[y]) leftc[y]=z;
else rightc[y]=z;
}
//function search
int find(int now,int data){
if(now==NIL ||data==key[now]) return now;
if(data<key[now]) return find(leftc[now],data);
else return find(rightc[now],data);
}
int treeminimum(int x){
while(leftc[x]!=NIL){
x=leftc[x];
}
return x;
}
int treesuccessor(int x){
int y;
if(rightc[x]!=NIL) return treeminimum(rightc[x]);
y=parent[x];
while(y!=NIL &&x==rightc[x]){
x=y;
y=parent[y];
}
return y;
}
//function delete
void treedelete(int z){
int x,y;
if(leftc[z]==NIL ||rightc[z]==NIL) y=z;
else y=treesuccessor(z);
if(leftc[y]!=NIL) x=leftc[y];
else x=rightc[y];
if(x!=NIL) parent[x]=parent[y];
if(parent[y]==NIL) root=x;
else if(y==leftc[parent[y]]) leftc[parent[y]]=x;
else rightc[parent[y]]=x;
if(y!=z) key[z]=key[y];
}
//print
void inorderprint(int now){
if(now!=NIL){
inorderprint(leftc[now]);
printf(" %d",key[now]);
inorderprint(rightc[now]);
}
}
void preorderprint(int now){
if(now!=NIL){
printf(" %d",key[now]);
preorderprint(leftc[now]);
preorderprint(rightc[now]);
}
}
int main(){
int n,i,j,data;
int z=0;
int del;
char str[10];
//initialize array
for(i=0;i<N;i++){
parent[i]=NIL;
key[i]=NIL;
rightc[i]=NIL;
leftc[i]=NIL;
}
//load n
scanf("%d",&n);
//load commnad and key
for(i=0;i<n;i++){
scanf("%s",str);
if(strcmp(str,"insert")==0) {
scanf("%d",&data);
key[z]=data;
insert(z);
z++;
}
else if(strcmp(str,"find")==0){
scanf("%d",&data);
if(find(root,data)!=NIL) printf("yes\n");
else printf("no\n");
}
else if(strcmp(str,"delete")==0){
scanf("%d",&data);
del=find(root,data);
treedelete(del);
}
else if(strcmp(str,"print")==0){
inorderprint(root);
cout <<endl;
preorderprint(root);
cout <<endl;
}
}
return 0;
} | 1 |
#include<bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
#define pb push_back
using namespace std;
typedef long long ll;
bool judge(int x, vector<int> d){
bool ans = true;
while (x > 0){
rep(i, d.size()){
if (x % 10 == d[i]) ans = false;
}
x /= 10;
}
return ans;
}
int main(){
int n, k;
cin >> n >> k;
vector<int> d(k);
rep(i, k) cin >> d[i];
for(int i = n; i < 1000000; i++){
if (judge(i, d)){
cout << i << endl;
return 0;
}
}
}
| #include<bits/stdc++.h>
#define ll long long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
static const ll MAX = 1000000000000000;
static const int NIL = -1;
using namespace std;
const long double EPS = 0.0000000001;
const long double PI = (acos(-1));
const ll MOD = 1e9 + 7;
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
vector<ll> m;
vector<bool> num(10,true);
int N,K;
void rec(int n){
if(n > N) return;
rep(i,10){
if(num[i]){
if(n*10 + i == 0) continue;
m.emplace_back(n*10 + i);
rec(n*10 + i);
}
}
}
int main(){
int a; cin >> N >> K;
rep(i,K){
cin >> a;
num[a] = false;
}
rec(0);
sort(m.begin(),m.end());
rep(i,m.size()){
if(m[i] >= N){
cout << m[i];
return 0;
}
}
} | 1 |
#include <bits/stdc++.h>
using namespace std;
long long n,i;
int main()
{
cin >> n;
for(i=111; i<=999; i+=111)
if(i>=n) {
cout<<i<<endl;
break;
}
return 0;
}
| #pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(ll i=0;i<(n);i++)
#define pll pair<ll,ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))
using namespace std;
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+7;
int main()
{
ll n;
cin >> n;
ll digit1=n/100;
if(n<=digit1*111){
cout << digit1*111 << endl;
}
else{
cout << (digit1+1)*111 << endl;
}
return 0;
} | 1 |
#include "bits/stdc++.h"
using namespace std;
void solve()
{
string s;
cin >> s;
int k = s.size();
while (s[k - 1] != 'F')
k--;
cout << s.substr(0, k - 1) << endl;
}
int main()
{
solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1LL<<60;
ll gcd(ll a, ll b) {
if(b == 0) return a;
return gcd(b, a % b);
}
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; }
ll modpow(ll a, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
string S;
cin >> S;
string ans;
ans = "";
for(int i = 0; i + 8 < S.length(); i++){
ans += S[i];
}
cout << ans << endl;
return 0;
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
struct Node{
int key;
Node *parent, *left, *right;
};
Node *root, *NIL;
void insert(int k){
Node *y = NIL;
Node *x = root;
Node *z;
z = (Node *)malloc(sizeof(Node));
z->key = k;
z->left = NIL;
z->right = NIL;
while(x != NIL){
y = x;
if(z->key < x->key) x = x->left;
else x = x->right;
}
z->parent = y;
if(y == NIL){
root = z;
}else{
if(z->key < y->key) y->left = z;
else y-> right = z;
}
}
Node* treeMinimum(Node *x){
while(x->left != NIL){
x = x->left;
}
return x;
}
Node* find(int k){
Node *x = root;
while(x != NIL && x->key != k){
if(k < x->key) x = x->left;
else x = x->right;
}
return x;
}
Node* treeSuccessor(Node *x){
if(x->right != NIL) return treeMinimum(x->right);
Node *y = x->parent;
while(y != NIL && x == y->right){
x = y;
y = y->parent;
}
return y;
}
void deleteNode(Node *z){
Node *y; //削除する対象
Node *x; //yの子
//削除する接点を決める
if(z->left == NIL || z->right == NIL) y = z;
else y = treeSuccessor(z);
//yの子xを決める
if(y->left != NIL){
x = y->left;
}else{
x = y->right;
}
if(x != NIL){
x->parent = y->parent;
}
if(y->parent == NIL){
root = x;
}else{
if(y == y->parent->left){
y->parent->left = x;
}else{
y->parent->right = x;
}
}
if(y != z){
z->key = y->key;
}
free(y);
}
void inorder(Node *u){
if(u == NIL) return;
inorder(u->left);
printf(" %d", u->key);
inorder(u->right);
}
void preorder(Node *u){
if(u == NIL) return;
printf(" %d", u->key);
preorder(u->left);
preorder(u->right);
}
void print(Node *u){
inorder(u);
printf("\n");
preorder(u);
printf("\n");
}
int main(){
int m;
cin >> m;
char com[10];
int k;
rep(i,m){
scanf("%s", com);
if(com[0] == 'i'){
scanf("%d", &k);
insert(k);
}else if(com[0] == 'f'){
scanf("%d", &k);
Node *x = find(k);
if(x == NIL) cout << "no" << endl;
else cout << "yes" << endl;
}else if(com[0] == 'd'){
scanf("%d", &k);
Node *x = find(k);
if(x != NIL){
deleteNode(x);
}
}else{
print(root);
}
}
return 0;
}
| #include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for (int i=0;i<(int)(n);i++)
#define codefor int test;scanf("%d",&test);while(test--)
#define yn(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YN(ans) if(ans)printf("YES\n");else printf("NO\n")
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define umap unordered_map
#define uset unordered_set
using namespace std;
using ll = long long;
const int MOD=1000000007;
//入力系
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
//出力系
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
//デバッグ系
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<endl;}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}
int main(){
struct dice{int u,e,w,n,s,d;
void rotate(char c){
int temp=u;
if(c=='E'){u=w;w=d;d=e;e=temp;}
else if(c=='W'){u=e;e=d;d=w;w=temp;}
else if(c=='N'){u=s;s=d;d=n;n=temp;}
else if(c=='S'){u=n;n=d;d=s;s=temp;}
}
bool hikaku(dice q){
int counter=0;
if(u==q.u)counter++;
if(s==q.s)counter++;
if(e==q.e)counter++;
if(w==q.w)counter++;
if(n==q.n)counter++;
if(d==q.d)counter++;
if(counter==6)return true;else return false;
}
};
int n;
in(n);
vector<dice> vec(n);
rep(i,n){
in(vec[i].u,vec[i].s,vec[i].e,vec[i].w,vec[i].n,vec[i].d);
}
string s="NNNNENNNN";
for(int j=0;j<n-1;j++){
for(int k=j+1;k<n;k++){
dice a=vec[j];
dice b=vec[k];
rep(i,9){
if(a.s==b.s)break;
b.rotate(s[i]);
}
int counter=0;
while(!a.hikaku(b)){
b.rotate('E');
counter++;
if(counter>=5){
goto skip;
}
}
out("No");
return 0;
skip: ;
}
}
out("Yes");
}
| 0 |
/* / \ (Hello)//JSM//*/
/* /*****\ */
/* Jai Shree Mataji / \ |\| |_| $ |-| K */
#include<vector>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<bits/stdc++.h>
#include<cstring>
#include<map>
#include<cmath>
#include<set>
using namespace std;
#define ll long long int
#define ld long double
#define MOD 1000000007
#define PI 3.1415926535897932384626433832795
#define YY cout<<"YES"
#define NN cout<<"NO"
#define EE cout<<"\n"
#define ee cout<<"\n"
#define ne cout<<"-1"
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
#define mkt make_tuple
#define sall(v ) sort(v.begin(), v.end())
#define all(v ) v.begin(), v.end()
#define fo(i , n) for(ll i = 0 ; i < n ; i++)
#define vl vector<ll>
#define vc vector<char>
#define vll vector< pair<ll,ll> >
#define mps map<string ,ll>
#define mpc map<char ,ll>
#define mpl map<ll, ll>
#define pr pair<ll,ll>
#define tpl tuple<ll,ll,ll>
ll containsDigit(ll number, ll digit){while (number != 0){int curr_digit = number % 10;if (curr_digit == digit) return 1;number /= 10;}return 0;}
ll soltn(ll x, ll n) {if(x>=0) return x%n; else return n+x%n;}
ll pd(ll x,ll y){if(x%y==0) return x/y;else return x/y+1;}
ll coda(ll n,ll d){ll count=0;while(n!=0){if(n%10==d)count++;n/=10;}return count;}
ll mdls(ll x){return max(x,-x);}
ll pow1(ll n,ll p){if(p==0)return 1;ll x=pow1(n, p/2);x=(x%MOD*x%MOD)%MOD;if(p%2==0) return x;else return (x*n)%MOD;}
ll isSubstr(string s1, string s2) { ll M = s1.length(); ll N = s2.length(); for (ll i = 0; i <= N - M; i++) { ll j; for (j = 0; j < M; j++) if (s2[i + j] != s1[j]) break; if (j == M) return i; } return -1; }
//s2>s1;if(not) rtrn -1;
ll maxSubArraySum(ll a[], ll size)
{
ll max_so_far = a[0];
ll curr_max = a[0];
for (ll i = 1; i < size; i++)
{
curr_max = max(a[i], curr_max+a[i]);
max_so_far = max(max_so_far, curr_max);
}
return max_so_far;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
ll tt=1;
// cin>>tt;
while(tt--)
{
/*
3 2 3*/
ll n,j,i;
ll a[3];
fo(i,3)
cin>>a[i];
sort(a,a+3);
cout<<a[0]+a[1];
ee;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main(void){
/*
int a, b;
cin >> a >> b ;
//*/
//*
vector<int> vec(3);
cin >> vec.at(0) >> vec.at(1) >> vec.at(2) ;
sort(vec.begin(), vec.end());
//*/
/*
if( a < 6 ){
cout << 0 << endl;
} else if (a < 13){
cout << b / 2 << endl;
} else {
cout << b << endl;
}
//*/
cout << vec.at(0) + vec.at(1) << endl;
return 0;
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
long long sum[200005];
int main()
{
long long mod=1000000007;
long long n;cin>>n;long long a[n];long long sum=0;
for(long long i=0;i<n;i++)
{
cin>>a[i];
sum+=a[i];
sum=sum%mod;
}
long long pum=sum;long long rum=0;
for(long long i=0;i<n-1;i++)
{
//cout<<sum[n-1]<<" "<<sum[i]<<" "<<a[i]<<endl;
pum=pum-a[i];
if(pum<0)
pum+=mod;
rum+=(a[i]*pum);
rum=rum%mod;
}
cout<<rum;
}
| #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const ll Mod = 1e9 + 7;
const ll Inf = 2242545357980376863LL;
const ll Log = 20;
const ll N = 1ll << Log;
const int Maxn = 5e2 + 10;
const int Base = 101;
ll c1[30], c2[30];
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
memset(c2, 31 , sizeof c2);
ll n, m;
cin >> n >> m;
ll s1 = 0, s2 = 0;
ll sm = 0, v;
for(int i = 0; i < n; i++){
cin >> v;
s1 += ((v * i) - sm);
sm += v;
}
sm = 0;
for(int i = 0; i < m; i++){
cin >> v;
s2 += ((v * i) - sm);
sm += v;
}
cout << (s1 % Mod)*(s2 % Mod) % Mod;
return 0;
}
| 0 |
#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;
int main(){
int n; cin >> n;
vector<int> data(n); rep(i,n) cin >> data[i];
vector<int> ans(1,-data[0]);
for(int i = 1; i < n; i++){
int k = upper_bound(all(ans),-data[i]) - ans.begin();
if(k == ans.size()) ans.push_back(-data[i]);
else ans[k] = -data[i];
}
cout << ans.size() << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define rep(i,m,n) for(int i=m;i<n;i++)
ll mod=1e9+7;
int main(){
int n,t;
cin>>n>>t;
int sum=0;
int cur=0;
rep(i,0,n){
int a;
cin>>a;
if(cur<=a){
sum+=t;
}
else{
sum+=a+t-cur;
}
cur=a+t;
}
cout<<sum<<endl;
} | 0 |
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> //required
#include <ext/pb_ds/tree_policy.hpp> //required
using namespace __gnu_pbds;
using namespace std;
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// find_by_order(k) returns iterator to kth element starting from 0;
// order_of_key(k) returns count of elements strictly smaller than k;
typedef long long ll;
typedef vector<ll> VL;
typedef vector<int> VI;
typedef pair<ll,ll> PLL;
typedef pair<int,int> PII;
#define pb push_back
#define F first
#define S second
#define SZ(a) int((a).size())
#define ALL(a) a.begin(),a.end()
#define fr(i,x,y) for(int i=x;i<y;i++)
#define frr(i,x,y) for(int i=x-1;i>=y;i--)
#define inf 1e18+1
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const int mod=1000000007;
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
ll power(ll a,ll b){ll ans=1;while(b!=0){if(b&1){ans*=a;}a=a*a;b>>=1;}return ans;}
ll powerm(ll a,ll b){ll ans=1;while(b!=0){if(b&1){ans*=a;ans%=mod;}a=a*a;a%=mod;b>>=1;}return ans%mod;}
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();
void solve()
{
ll n,x;
cin>>n>>x;
ll ct=0;
VL arr(n);
fr(i,0,n)
cin>>arr[i];
fr(i,1,n)
{
if((arr[i]+arr[i-1])>x)
{
ll rem=(arr[i]+arr[i-1])-x;
ll f=min(rem,arr[i]);
rem-=f;
arr[i]-=f;
ct+=f;
if((arr[i]+arr[i-1])>x)
{
ct+=rem;
}
}
}
cout<<ct<<endl;
return;
}
int main()
{
IOS;
ll t=1;
//cin>>t;
while(t--)
{
//cout<<"Case #"<<pp-t<<":\n";
solve();
}
return 0;
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
*/
/*
recursion - matrix exponential
*/
// BITMASK:
// 1)When some constrall is of the order of 15-20, think of bitmask DP.
// 2)When some constrall is around 40, try out meet in the middle
// 3) See Strings,palindromes,prefix,suffix etc -> KMP,Z algorithm | #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;
const ll MOD=998244353;
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=2e9;
ll N,M,V,P;
ll A[100000];
bool C(int x){
if(x<P) return true;
if(A[x]+M<A[P-1]) return false;
ll sum=0;
sum+=M*P;
sum+=M*(N-1-x);
for(int i=P-1;i<x;i++){
sum+=min(M,A[x]+M-A[i]);
}
return sum>=M*V;
}
int main(){
cin>>N>>M>>V>>P;
for(int i=0;i<N;i++) cin>>A[i];
sort(A,A+N,greater<ll>());
int l=0,r=N;
while(r-l>1){
int m=(l+r)/2;
if(C(m)) l=m;
else r=m;
}
cout<<l+1<<endl;
return 0;
} | 0 |
#include<iostream>
#include<vector>
using namespace std;
typedef unsigned long long int ull;
vector<ull> allPrimes;
void sieve(int n)
{
vector<bool> prime(n+1, true);
for (int p=2; p*p<=n; p++) {
if (prime[p] == true) {
for (int i=p*2; i<=n; i += p)
prime[i] = false;
}
}
for (int p=2; p<=n; p++)
if (prime[p])
allPrimes.push_back(p);
}
ull factorialDivisors(ull n)
{
sieve(n);
ull result = 1;
for (int i=0; i < allPrimes.size(); i++)
{
ull p = allPrimes[i];
ull exp = 0;
while (p <= n)
{
exp = exp + (n/p);
p = p*allPrimes[i];
}
result = (result*(exp+1))%1000000007;
}
return result;
}
int main()
{
int n; cin>>n;
cout << factorialDivisors(n);
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int H, W;
int seen[10000][10000];
char S[10000][10000];
vector<pair<ll, ll>> prime_factorize(ll n) { //素因数分解と各素数の個数
vector<pair<ll, ll>> res;
for (ll i = 2; i * i <= n; i++)
{
if (n % i != 0)
{
continue;
}
int num = 0;
while (n % i == 0)
{
++num;
n /= i;
}
res.push_back(make_pair(i, num));
}
if (n != 1)
{
res.push_back(make_pair(n, 1));
}
return res;
}
long long GCD(long long a, long long b) {
if (b == 0) return a;
else return GCD(b, a % b);
}
ll lcm(ll x, ll y) {
return x / GCD(x, y) * y;//先に割り算をして掛けられる数を小さくして掛け算を行う
}
const int MAX = 510000;
const int MOD = 1000000007;
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;
}
void dfs(int st, int fi) {
// cout << st << " " << fi << endl;
if (st < 0 || fi < 0 || st >= H || fi >= W)
{
return ;
}
if (S[st][fi] == '#')
{
return ;
}
if (seen[st][fi] == 1)
{
return ;
}
if (S[st][fi] == '.')
{
seen[st][fi] = 1;
}
dfs(st + 1, fi);
dfs(st, fi + 1);
dfs(st, fi - 1);
dfs(st - 1, fi);
}
int main() {
int N;
cin >> N;
vector<int> p(N + 1);
p[1] = 0;
for(int i = 2; i <= N; i++) {
int n = i;
vector<pair<int, int>> b;
for(int j = 2; j * j <= n; j++) {
if(n % j != 0) continue;
int ex = 0;
while(n % j == 0) {
ex++;
n /= j;
}
b.push_back({j, ex});
}
if(n != 1) b.push_back({n, 1});
for(auto v : b) {
p[v.first] += v.second;
}
}
/*for(int i = 1; i <= N; i++)
cout << p[i] << endl;*/
ll ans = 1;
for(int i = 2; i <= N; i++) {
ans *= (p[i] + 1);
ans %= 1000000007;
}
cout << ans << endl;
return 0;
}
| 1 |
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <map>
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
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; }
const ll inf = -1e16;
int main(){
int N;
cin >> N;
vector<int> v(N);
ll dp[3];
rep(i,N) cin >> v[i];
dp[0] = v[0];
dp[1] = max(v[0],v[1]);
if(N>2){
dp[0] += v[2];
dp[2] = max(dp[1],(ll)v[2]);
}
for(int i=3; i<N; ++i){
ll tmp1,tmp2;
tmp1 = dp[0];
tmp2 = dp[1];
if((i+1)%2){ // odd
dp[0] += v[i];
dp[1] = tmp1> tmp2? tmp1:tmp2 ;
dp[2] += v[i];
dp[2] = dp[1]>dp[2]? dp[1]:dp[2];
}
else{
dp[1] += v[i];
dp[2] = tmp2> dp[2]? tmp2:dp[2] ;
}
}
if(N%2){
cout << dp[2] << endl;
}else{
cout << dp[1] << endl;
}
return 0;
}
| #include <bits/stdc++.h>
#define Int int64_t
using namespace std;
int main() {
constexpr Int INF = 1e18;
int N;
cin >> N;
vector<Int> a(N);
for (int i = 0; i < N; ++i) { cin >> a[i]; }
int skip = 1 + N % 2;
vector<vector<Int>> dp(N + 2, vector<Int>(skip + 1, -INF));
dp[0][0] = 0;
for (int i = 0; i <= N; ++i) {
for (int j = 0; j <= skip; ++j) {
if (j < skip) {
dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j]);
}
if (i < N) {
dp[i + 2][j] = max(dp[i + 2][j], dp[i][j] + a[i]);
}
}
}
cout << dp[N + 1][skip] << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main(){
long long N,M;
cin>>N>>M;
vector<long long> l(M);
vector<long long> r(M);
for(long long i=0;i<M;i++)
cin>>l.at(i)>>r.at(i);
sort(l.begin(),l.end());
sort(r.begin(),r.end());
long long a=r.at(0)-l.at(M-1)+1;
if(a>0)
cout<<a<<endl;
else
cout<<0<<endl;
} | #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, n); for(ll i = 0; i < (ll)n; i++)
int main() {
int n,m; cin >> n >> m;
int l,u; l = 0; u = (int)1e6;
rep(i, m){
int p, q; cin >> p >> q;
if(l < p) l = p;
if(q < u) u = q;
}
cout << max(u - l + 1, 0) << endl;
} | 1 |
#include <bits/stdc++.h>
using namespace std;
#define INF 1001000100010001000
#define MOD 1000000007
#define EPS 1e-10
#define int long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define Rep(i, N) for (int i = 1; i < N; i++)
#define For(i, a, b) for (int i = (a); i < (b); i++)
#define pb push_back
#define eb emplece_back
#define mp make_pair
#define i_i pair<int, int>
#define vi vector<int>
#define vvi vector<vi >
#define vb vector<bool>
#define vvb vector<vb >
#define vp vector< i_i >
#define all(a) (a).begin(), (a).end()
#define Int(x) int x; scanf("%lld", &x);
#define int2(x, y) int x, y; scanf("%lld %lld", &x, &y);
#define fir first
#define sec second
#define ffir first.first
#define fsec first.second
#define sfir second.first
#define ssec second.second
//int dxy[5] = {0, 1, 0, -1, 0};
// assign
#define Edge pair< i_i, int>
#define Graph vector< vp >
//???????§????????????????(???????????????)
//Dijkstra O((E+V)logV)
//dist: ?§????????????????????????§??????????????¢
//?????????: ??????????????¨????????????(??????-1)
vector<int> dijkstra(Graph &g, int s, vi &dist)
{
int n = g.size();
vi visited(n, 0); visited[s] = 1;
vi prev(n, -1);
dist.assign(n, INF); dist[s] = 0;
priority_queue< Edge, vector< Edge >, greater< Edge > > pq;
pq.push(Edge(i_i((int)0, s), (int)-1)); // ????????? ??? ???
while (pq.size()) {
int d = pq.top().ffir, v = pq.top().fsec, u = pq.top().second;
pq.pop();
if (dist[v] < d) continue; //?????§???????????§???????????°??????
visited[v] = 2; prev[v] = u;
for (i_i &e : g[v]) {
if (visited[e.fir] == 2) continue;
if (dist[e.fir] > dist[v] + e.sec) {
dist[e.fir] = dist[v] + e.sec;
pq.push(Edge(i_i(dist[e.fir], e.fir), v));
visited[e.fir] = 1;
}
}
}
return prev;
}
signed main()
{
int2(n, m);
Int(st);
Graph g(n, vp());
vi ans(n);
rep(i, m) {
int2(s, t);
Int(d);
g[s].pb(i_i(t, d));
}
dijkstra(g, st, ans);
rep(i, n) {
if (ans[i] == INF) {
cout << "INF" << endl;
} else {
cout << ans[i] << endl;
}
}
return 0;
} | #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
//#define int long long
#define DBG 1
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
int n, m, r; const int W(1), B(0);
vector<vector<pair<int, int>>>adj; // dist, cost
void dijkstra() {
vector<int>d(n, INF), color(n, W);
d[r] = 0;
priority_queue<pair<int, int>>pq; // d[v], v
pq.push(make_pair(0, r));
while (!pq.empty()) {
pair<int, int>f(pq.top()); pq.pop();
color[f.second] = B;
if (d[f.second] < (-1)*f.first)continue;
rep(i, 0, adj[f.second].size()) {
pair<int, int>u(adj[f.second][i]);
if (color[u.first] != B&&d[f.second] + u.second < d[u.first]) {
d[u.first] = d[f.second] + u.second;
pq.push(make_pair((-1)*d[u.first], u.first));
}
}
}
rep(i, 0, n) {
if (d[i] == INF)cout << "INF" << endl;
else cout << d[i] << endl;
}
}
signed main() {
cin >> n >> m >> r; adj = vector<vector<pair<int, int>>>(n, vector<pair<int,int>>());
rep(i, 0, m) {
int s, t, d; cin >> s >> t >> d;
adj[s].push_back(make_pair(t, d));
}
dijkstra();
return 0;
} | 1 |
//kruskal tree
#include <cstdio>
#include <algorithm>
using namespace std;
#define M 100000
int parent[M],a[M],b[M];
pair<int,int>node[M];
int root(int a){return parent[a]==a?a:parent[a]=root(parent[a]);}
int unite(int a,int b){
int x=root(a),y=root(b);
if(x==y)return 0;
parent[x]=y;
return 1;
}
int main(){
int i,s,n,m;
for(;scanf("%d%d",&n,&m)>1;printf("%d\n",s)){
for(i=0;i<m;i++)scanf("%d%d%d",a+i,b+i,&node[i].first),node[i].second=i;
sort(node,node+m);
for(i=0;i<n;i++)parent[i]=i;
for(s=i=0;i<m;i++)if(unite(a[node[i].second],b[node[i].second]))s+=node[i].first;
}
} | #include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
#define MAX_V 10000
#define MAX_E 10000
int par[MAX_V];
int rank_[MAX_V];
void init(int V) {
for (int i = 0; i < V; i++) {
par[i] = i;
rank_[i] = 0;
}
}
int root(int x) {
if (par[x] == x) {
return x;
}
return par[x] = root(par[x]);
}
void unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y) {
return;
}
if (rank_[x] < rank_[y]) {
par[x] = y;
}
else if (rank_[x]==rank_[y]){
par[y] = x;
rank_[x]++;
}
else if (rank_[y] < rank_[x]) {
par[y] = x;
}
}
bool same(int x, int y) {
return root(x) == root(y);
}
struct edge {
int from, to, cost;
bool operator<(const edge& other)const {
return cost < other.cost;
}
};
using edges = vector<edge>;
int kruskal(edges& es,int V) {
sort(es.begin(), es.end());
int E = es.size();
init(V);
int res = 0;
for (int i = 0; i < E; i++) {
if (!same(es[i].from, es[i].to)) {
unite(es[i].from, es[i].to);
res += es[i].cost;
}
}
return res;
}
int main() {
int V, E;
cin >> V >> E;
edges es(E);
for(int i=0;i<E;i++){
cin >> es[i].from >> es[i].to >> es[i].cost;
}
cout << kruskal(es, V) << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i< (n); i++)
using ll = long long;
using P = pair<int, int>;
int main()
{
int n;
cin >> n;
string m = to_string(n);
int cnt = 0;
rep(i,m.size()){
cnt += m.at(i) - '0';
}
if(n % cnt == 0)cout << "Yes" << endl;
else cout << "No" << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
int a = (N % 10);
int b = (N-a)/10%10;
int c = (N-10*b-a)/(10*10)%10;
int d = (N-10*10*c-10*b-a)/(10*10*10)%10;
int e = (N-10*10*10*d-10*10*c-10*b-a)/(10*10*10*10)%10;
int f = (N-10*10*10*10*e-10*10*10*d-10*10*c-10*b-a)/(10*10*10*10*10)%10;
int g = (N-10*10*10*10*10*f-10*10*10*10*e-10*10*10*d-10*10*c-10*b-a)/(10*10*10*10*10*10)%10;
int h = (N-10*10*10*10*10*10*g-10*10*10*10*10*f-10*10*10*10*e-10*10*10*d-10*10*c-10*b-a)/(10*10*10*10*10*10*10)%10;
int i = (N-10*10*10*10*10*10*10*h-10*10*10*10*10*10*g-10*10*10*10*10*f-10*10*10*10*e-10*10*10*d-10*10*c-10*b-a)/(10*10*10*10*10*10*10*10)%10;
if (N % (a+b+c+d+e+f+g+h+i) == 0) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin>>n;
long long int ar=0;
int temp[64];
for(int i=0;i<64;i++){
temp[i]=0;
}
for(int i=0;i<n;i++){
cin>>ar;
int j=0;
while(ar>0){
temp[j]+=ar%2;
ar/=2;
j++;
}
}
long long int sum=0,tempsum=0,temp2;
for(int i=0;i<63;i++){
int k1=temp[i],k2=n-temp[i];
// cout <<k1 << " " << k2 << endl;
tempsum=k1;tempsum*=k2;
if(tempsum>1000000007) tempsum=tempsum%1000000007;
temp2=(pow(2,i));
// cout << temp2 <<endl;
// cout << tempsum<< endl;
if(temp2>1000000007) temp2=temp2%1000000007;
tempsum*=temp2;
sum+=tempsum;
if(sum>1000000007) sum=sum%1000000007;
}
cout << sum << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#include <math.h>
#include <iomanip>
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 int mod=1000000007;
const int INF=1001001001;
vector<pair<int64_t,int64_t>>prime_factorize(int64_t x){
vector<pair<int64_t,int64_t>>p;
for(int64_t i=2;i*i<=x;i++){
int cnt=0;
if(x%i==0){
while(x%i==0){cnt++;x/=i;}
p.push_back(make_pair(i,cnt));
}
}
if(x!=1){p.push_back(make_pair(x,1));}
return p;
}
int main() {
string s;
cin>>s;
int n=s.size();
stack<char>k;
for(int i=0;i<n;i++){
if(k.empty()){k.push(s[i]);}
else if(s[i]=='T'&&k.top()=='S'){k.pop();}
else{k.push(s[i]);}
}
cout<<k.size()<<endl;
return 0;
} | 0 |
#include<iostream>
main(){
std::string s;
std::cin>>s;
int n=s.size();
std::cout<<s.substr(0,n-8)<<std::endl;
} | #include <bits/stdc++.h>
using namespace std;
#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
typedef long long ll;
typedef long double lb;
#define int long long
#define double long double
typedef pair<int,int> P;
const int MOD = 1000000007;
const ll INF = 1e16;
const double EPS = 1e-10;
const double PI = 3.141592653589793;
template<class T> inline bool chmin(T& a, T b){if(a>b){a = b;return 1;}return 0;}
template<class T> inline bool chmax(T& a, T b){if(a<b){a = b;return 1;}return 0;}
const pair<int,int> fd[] = {make_pair(1,0),make_pair(-1,0),make_pair(0,1),make_pair(0,-1)};
////////////////////////////////////////////////////////////////////
template<typename T>
struct edge {
int to, from;T cost;
edge(int to) : from(-1), to(to), cost(-1) {}
edge(int to, T cost) : from(-1), to(to), cost(cost) {}
edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}
};
template<typename T>
using edges = vector<edge<T>>;
template<typename T>
using graph = vector<edges<T>>;
template<typename T>
using matrix = vector<vector<T>>;
template<typename T>
bool compByCost(const edge<T>& left, const edge<T>& right){
return left.cost < right.cost;
}
template<typename T>
vector<vector<T>> warshall(matrix<T> &g){
vector<vector<T>> dist(g.size());
rep(i,g.size()){
dist[i].resize(g.size());
rep(j,g.size())dist[i][j] = (i == j ? 0 : g[i][j]);
}
rep(k,g.size()){
rep(i,g.size()){
rep(j,g.size()){
if(dist[i][k] == INF || dist[k][j] == INF)continue;
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
return dist;
}
#define warshall warshall<int>// matの初期化でINFを詰めておく
////////////////////////////////////////////////////////////////////
signed main(){
string s,t;cin>>s;
rep(i,s.length()-8)t += s[i];
cout << t << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll d;
map < ll, ll > mp[20];
ll pw10[20];
ll coef[20];
ll gc[20];
int LEN = 0;
int val[25];
int val0[25];
ll solve(int where, ll need) {
//cout << where << " " << need << endl;
if (where == LEN) {
return (need == 0);
}
if (gc[where] != 0 && need % gc[where] != 0) return 0;
//if (need % gc[where] != 0) return 0;
if (coef[where] == 0) {
if (need == 0) return 10;
else return 0;
}
if (mp[where].find(need) != mp[where].end()) {
return mp[where][need];
}
ll tot = 0;
for (int i = -9; i <= 9; i++) {
if (where) tot += solve(where + 1, need - i * coef[where]) * val[i + 9];
else {
tot += solve(where + 1, need - i * coef[where]) * val0[i + 9];
}
}
return mp[where][need] = tot;
}
ll solve(int len) {
LEN = (len + 1) / 2;
for (int i = 0; i <= LEN; i++) mp[i].clear();
for (int i = 0; i < (len + 1) / 2; i++) {
coef[i] = pw10[len - 1 - i] - pw10[i];
coef[i] /= 9;
}
gc[LEN] = 0;
for (int i = LEN - 1; i >= 0; i--) gc[i] = __gcd(coef[i], gc[i + 1]);
return solve(0, d);
}
int main() {
ios_base::sync_with_stdio(false);
//freopen("input.txt", "r", stdin);
for (int i = 0; i <= 9; i++) {
for (int j = 0; j <= 9; j++) {
val[i - j + 9]++;
if (j) val0[i - j + 9]++;
}
}
pw10[0] = 1;
for (int i = 1; i <= 18; i++) pw10[i] = 10 * pw10[i - 1];
cin >> d;
if (d % 9 != 0) {
cout << 0;
return 0;
}
d /= 9;
ll f = 0;
for (int len = 2; len <= 18; len++) {
f += solve(len);
}
cout << f << endl;
return 0;
}
| #include<cstdio>
#include<cmath>
typedef long long LL;
int D;
LL ans=0;
LL solve(int len){
LL now=0,ans=1;
for(int l=0,r=len-1;l<r;++l,--r){
LL dw=(LL)pow(10,l),qaq=((LL)pow(10,r)-dw)/9;
if(now<D){
int wg=(D-now)/dw%10;
now+=qaq*wg;
ans=ans*(10-wg-(l==0));
}else{
int wg=(now-D)/dw%10;
now-=qaq*wg;
ans=ans*(10-wg-(l==0));
}
}
if(len&1)ans*=10;
if(now==D)return ans;
return 0;
}
int main(){
scanf("%d",&D);
if(D%9)return puts("0"),0;
D/=9;
for(int i=1;i<=18;++i)
if(pow(10,i)>D)ans+=solve(i);
printf("%lld\n",ans);
return 0;
} | 1 |
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
inline int gi()
{
char c;int num=0,flg=1;
while((c=getchar())<'0'||c>'9')if(c=='-')flg=-1;
while(c>='0'&&c<='9'){num=num*10+c-48;c=getchar();}
return num*flg;
}
#define N 200005
#define LL long long
#define lc i<<1
#define rc i<<1|1
const LL INF=0x3f3f3f3f3f3f3f3fll;
int pos[N],A,B,n,Q;
struct node{
int l,r;
LL mi1,mi2,la;
}a[N<<2];
void cal(int i,LL k){a[i].mi1+=k;a[i].mi2+=k;a[i].la+=k;}
void pushdown(int i)
{
if(a[i].la&&a[i].l<a[i].r){
cal(lc,a[i].la);cal(rc,a[i].la);
a[i].la=0;
}
}
void pushup(int i)
{
a[i].mi1=min(a[lc].mi1,a[rc].mi1);
a[i].mi2=min(a[lc].mi2,a[rc].mi2);
}
void build(int i,int l,int r)
{
a[i].l=l;a[i].r=r;
if(l==r){
a[i].mi1=INF;a[i].mi2=INF;
return;
}
int mid=(l+r)>>1;
build(lc,l,mid);build(rc,mid+1,r);
pushup(i);
}
void insert(int i,int l,int r,LL k)
{
if(a[i].l>r||a[i].r<l)return;
pushdown(i);
if(l<=a[i].l&&a[i].r<=r){cal(i,k);return;}
insert(lc,l,r,k);insert(rc,l,r,k);
pushup(i);
}
void modify(int i,int x,LL k)
{
if(a[i].l>x||a[i].r<x)return;
pushdown(i);
if(x==a[i].l&&a[i].r==x){a[i].mi1=min(a[i].mi1,k-x);a[i].mi2=min(a[i].mi2,k+x);return;}
modify(lc,x,k);modify(rc,x,k);
pushup(i);
}
LL query1(int i,int l,int r)
{
if(a[i].l>r||a[i].r<l)return INF;
pushdown(i);
if(l<=a[i].l&&a[i].r<=r)return a[i].mi1;
return min(query1(lc,l,r),query1(rc,l,r));
}
LL query2(int i,int l,int r)
{
if(a[i].l>r||a[i].r<l)return INF;
pushdown(i);
if(l<=a[i].l&&a[i].r<=r)return a[i].mi2;
return min(query2(lc,l,r),query2(rc,l,r));
}
LL query(int i)
{
if(a[i].l==a[i].r)return a[i].mi1+a[i].l;
pushdown(i);
return min(query(lc),query(rc));
}
int ab(int x){return x<0?-x:x;}
int main()
{
int i;LL f;
n=gi();Q=gi();A=gi();B=gi();
build(1,1,n);
pos[0]=A;modify(1,B,0);
for(i=1;i<=Q;i++){
pos[i]=gi();
f=min(query1(1,1,pos[i])+pos[i],query2(1,pos[i]+1,n)-pos[i]);
insert(1,1,n,ab(pos[i]-pos[i-1]));
modify(1,pos[i-1],f);
}
printf("%lld\n",query(1));
} | #include <bits/stdc++.h>
using namespace std;
int u,v,kq,x[100005],y[100005],deg[100005],n,m,slx;
vector<int> a[100005],ng[100005];
queue<int> q;
void sxtp()
{
for (int i=1;i<=n;++i) if (deg[i]==0) q.push(i);
while (!q.empty())
{
int u=q.front();q.pop();
x[++slx]=u;y[u]=slx;
for (int i=0;i<a[u].size();++i)
{
int v=a[u][i];
deg[v]--;
if (deg[v]==0) q.push(v);
}
}
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>m;
for (int i=1;i<=n+m-1;++i)
{
cin>>u>>v;
a[u].push_back(v);deg[v]++;
ng[v].push_back(u);
}
sxtp();
for (int u=1;u<=n;++u)
{
kq=0;
for (int j=0;j<ng[u].size();++j)
{
v=ng[u][j];
// if (u==1) cout<<v<<endl;
if (y[v]>y[kq]) kq=v;
}
cout<<kq<<'\n';
}
// cout<<x[1]<<' '<<y[6]<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
#define err(args...) {}
#ifdef DEBUG
#include "_debug.cpp"
#endif
using namespace std;
using ll = long long;
using ld = long double;
template <typename T> using lim = numeric_limits<T>;
template <typename T> istream& operator>>(istream& is, vector<T>& a) { for(T& x : a) { is >> x; } return is; }
const int N = 50;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
ll k;
cin >> k;
cout << N << endl;
for(int i = 0; i < N; i++) {
if(i < N - 1 - (k - 1) % N) {
cout << (k - 1) / N + i << " ";
} else if(i == N - 1) {
cout << (k - 1) / N + N << " ";
} else {
cout << (k - 1) / N + N - 1 << " ";
}
}
cout << endl;
return 0;
}
| #include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<cstring>
#include<vector>
#include<list>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for(int i=0;i<n;++i)
#define rrep(i, n) for(int i=n;i>=0;--i)
const int inf=1e9+7;
const ll mod=1e9+7;
const ll big=1e18;
const double PI=2*asin(1);
ll mod1 = 998244353;
ll mypow(ll a, ll b) {
ll h = b;
ll two;
ll ans = 1;
ll tmp;
while(h>0) {
two = 1;
tmp = a;
while(2*two<h) {
two *= 2;
tmp *= tmp;
tmp %= mod1;
}
h -= two;
ans *= tmp;
ans %= mod1;
}
return ans;
}
int main() {
int N;
cin>>N;
int D[N];
map<int, int> amap;
for(int i=0;i<N;++i) {
cin>>D[i];
amap[D[i]]++;
}
if(D[0]>0 || amap[0]>1) {
cout<<0<<endl;
return 0;
}
ll ans = 1;
ll num = 1;
int maxnum=0;
bool can = true;
bool owari = false;
for(int i=0;i<N;++i) {
if(amap[i]>0) {
if(owari) can = false;
maxnum = i;
}
else {
owari = true;
}
}
if(!can) {
cout<<0<<endl;
return 0;
}
for(int i=1;i<=maxnum;++i) {
ans *= mypow(num, amap[i]);
ans %= mod1;
num = amap[i];
}
cout<<ans<<endl;
}
| 0 |
#include <stdio.h>
int main(void){
int n,i,j,k;
scanf("%d",&n);
int retsu[n];
for (i = 1; i <= n; i++){
k = n - i;
scanf("%d",&retsu[k]);
}
for (j = 0; j < n; j++){
if( j ) printf(" ");
printf("%d",retsu[j]);
}
printf("\n");
return 0;
}
| #include<stdio.h>
int main()
{
int a,b;
while(scanf("%d",&a)!=EOF&&a)
{
int b[10000]={0};
int i,j;
for(i=0;i<a;i++)
{
scanf("%d",&b[i]);
}
printf("%d",b[i-1]);
for(j=i-2;j>=0;j--)
{
printf(" %d",b[j]);
}
printf("\n");
}
return 0;
} | 1 |
#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
#include <utility>
using namespace std;
template <class T>
using lp_queue=priority_queue<T, deque<T>, greater<T>>;
using Weight=long long;
static const Weight INF=1LL<<57;
struct Edge {
size_t src, dst;
Weight weight;
Edge() {}
Edge(size_t src, size_t dst, Weight weight=1):
src(src), dst(dst), weight(weight)
{}
};
bool operator<(const Edge &e, const Edge &f) {
if (e.weight != f.weight) {
return e.weight > f.weight;
} else {
return e.src!=f.src? e.src<f.src : e.dst<f.dst;
}
}
using Edges=vector<Edge>;
using Vertex=vector<Edge>;
using Graph=vector<Vertex>;
void join(Graph &g, size_t s, size_t d, Weight w=1) {
// non-directed
g[s].push_back(Edge(s, d, w));
g[d].push_back(Edge(d, s, w));
}
void connect(Graph &g, size_t s, size_t d, Weight w=1) {
// directed
g[s].push_back(Edge(s, d, w));
}
vector<vector<Weight>> shortest_distance(Graph &g) {
/* Johnson */
size_t V=g.size();
vector<Weight> h(V);
for (size_t k=0; k<V; ++k)
for (size_t i=0; i<V; ++i)
for (const Edge &e: g[i])
if (h[e.dst] > h[e.src] + e.weight) {
h[e.dst] = h[e.src] + e.weight;
if (k == V-1)
return vector<vector<Weight>>();
}
for (size_t i=0; i<V; ++i)
for (Edge &e: g[i])
e.weight += h[e.src] - h[e.dst];
vector<vector<Weight>> d(V, vector<Weight>(V, INF));
for (size_t s=0; s<V; ++s) {
d[s][s] = 0;
lp_queue<pair<Weight, size_t>> q; q.push(make_pair(0, s));
while (!q.empty()) {
pair<Weight, size_t> p=q.top(); q.pop();
size_t v=p.second;
if (d[s][v] < p.first) continue;
for (const Edge &e: g[v])
if (d[s][e.dst] > d[s][v] + e.weight) {
d[s][e.dst] = d[s][v] + e.weight;
q.push(make_pair(d[s][e.dst], e.dst));
}
}
for (size_t u=0; u<V; ++u)
if (d[s][u] < INF)
d[s][u] += h[u] - h[s];
}
for (size_t v=0; v<V; ++v)
for (Edge &e: g[v])
e.weight += h[e.dst] - h[e.src];
return d;
}
int main() {
size_t V, E;
scanf("%zu %zu", &V, &E);
Graph g(V);
for (size_t i=0; i<E; ++i) {
size_t s, t;
Weight d;
scanf("%zu %zu %lld", &s, &t, &d);
connect(g, s, t, d);
}
vector<vector<Weight>> d=shortest_distance(g);
if (d.empty())
return !printf("NEGATIVE CYCLE\n");
for (vector<Weight> w: d)
for (size_t i=0; i<V; ++i)
if (w[i] < INF) {
printf("%lld%c", w[i], i<V-1? ' ':'\n');
} else {
printf("INF%c", i<V-1? ' ':'\n');
}
return 0;
} | #include <stdio.h>
#include <limits.h>
int main(){
int n,m,s,i,j;
scanf("%d%d",&n,&m);
int e[m+1][3],dist[m+1],update=1,k,d[200][200];
//printf("xxx");
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(i==j)d[i][j]=0;
else d[i][j]=INT_MAX;
}
}
for(i=0;i<m;i++){
for(j=0;j<3;j++){
scanf("%d",&e[i][j]);
}
d[e[i][0]][e[i][1]]=e[i][2];
}
for(i=0;i<n;i++){
dist[i]=INT_MAX;
}
dist[s]=0;
for(i=0;i<n-1;i++){
update=0;
for(j=0;j<m;j++){
if(dist[e[j][0]]!=INT_MAX&&dist[e[j][1]]>dist[e[j][0]]+e[j][2]){
dist[e[j][1]]=dist[e[j][0]]+e[j][2];
update=1;
}
}
if(update==0)break;
}
for(j=0;j<m;j++){
if(dist[e[j][0]]!=INT_MAX&&dist[e[j][1]]>dist[e[j][0]]+e[j][2]){
printf("NEGATIVE CYCLE\n");
return 0;
}
}
for(k=0;k<n;k++){
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if((d[i][k]!=INT_MAX&&d[k][j]!=INT_MAX)&&d[i][j]>d[i][k]+d[k][j]){
d[i][j]=d[i][k]+d[k][j];
}
}
}
}
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(j>0)printf(" ");
if(d[i][j]==INT_MAX){
printf("INF");
}
else{
printf("%d",d[i][j]);
}
}
printf("\n");
}
return 0;
} | 1 |
#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,b,a) for(int i=b;i>=a;i--)
#define fori(a) for(auto i : a )
#define all(a) begin(a), end(a)
#define set(a,b) memset(a,b,sizeof(a))
#define sz(a) a.size()
#define pi 3.14159
#define ll long long
#define ull unsigned long long
#define pb push_back
#define PF push_front //deque
#define mp make_pair
#define pq priority_queue
#define mod 1000000007
#define f first
#define s second
#define pii pair< int, int >
#define vi vector<int>
#define vpii vector<pii>
#define debug(v) for(auto i:v) cout<<i<<" ";
#define tc int t; cin >> t; while(t--)
using namespace std;
string repeat(string s, int n) {
string s1 = "";
for (int i=0; i<n;i++)
s1+=s;
return s1;
}
string getString(char x) {
string s(1, x);
return s;
}
void optimizeIO(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
int gcd(int a, int b){
if (a == 0) return b;
return gcd(b % a, a);
}
const int N=55556;
vector<int> isprime(N,1);
vector<int> prime;
void solve(){
rep(i,2,N){
for(int j=2*i;j<N;j+=i) isprime[j]=0;
}
rep(i,2,N) if(isprime[i]) prime.pb(i);
int n,i=0;
cin>>n;
while(n>0 && i<prime.size()){
if(prime[i]%5==4) cout<<prime[i]<<" ",n--;
i++;
}
cout<<endl;
}
int main(){
optimizeIO();
// tc
{ solve();
}
}
| #include<bits/stdc++.h>
#define del(a,i) memset(a,i,sizeof(a))
#define ll long long
#define inl inline
#define il inl void
#define it inl int
#define ill inl ll
#define re register
#define ri re int
#define rl re ll
#define mid ((l+r)>>1)
#define lowbit(x) (x&(-x))
#define INF 0x3f3f3f3f
using namespace std;
template<class T>il read(T &x){
int f=1;char k=getchar();x=0;
for(;k>'9'||k<'0';k=getchar()) if(k=='-') f=-1;
for(;k>='0'&&k<='9';k=getchar()) x=x*10+k-'0';
x*=f;
}
template<class T>il _print(T x){
if(x>=10) _print(x/10);
putchar(x%10+'0');
}
template<class T>il print(T x){
if(x<0) putchar('-'),x=-x;
_print(x);
}
ll mul(ll a,ll b,ll mod){long double c=1.;return (a*b-(ll)(c*a*b/mod)*mod)%mod;}
it qpow(int x,int k,int mod){
int res=1,bas=x;
while(k){
if(k&1) res=1ll*res*bas%mod;
bas=1ll*bas*bas%mod,k>>=1;
}
return res;
}
const int N = 2e5+5,mod = 1e9+7;
it add(int x,int y){return x+y>=mod?x+y-mod:x+y;}
it mul(int x,int y){return 1ll*x*y%mod;}
il inc(int &x,int y){x=add(x,y);}
int n,m,k,ans,fac[N],ifac[N];
it C(int n,int m){return mul(mul(ifac[n-m],ifac[m]),fac[n]);}
int main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
read(n),read(m),read(k),fac[0]=1;
for(ri i=1;i<=n*m;++i) fac[i]=mul(fac[i-1],i);
ifac[n*m]=qpow(fac[n*m],mod-2,mod);
for(ri i=n*m-1;i>=0;--i) ifac[i]=mul(ifac[i+1],i+1);
for(ri i=1;i<=n-1;++i) inc(ans,mul(mul(mul(m,m),n-i),i));
for(ri i=1;i<=m-1;++i) inc(ans,mul(mul(mul(n,n),m-i),i));
print(mul(ans,C(n*m-2,k-2)));
return 0;
} | 0 |