code_file1
stringlengths 87
4k
| code_file2
stringlengths 82
4k
|
---|---|
#line 1 "main_a.cpp"
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include <vector>
/* template start */
#line 2 "/home/imperi/cp-library/lib/utility/type_alias.hpp"
#line 4 "/home/imperi/cp-library/lib/utility/type_alias.hpp"
using u64 = std::uint64_t;
using u32 = std::uint32_t;
using u16 = std::uint16_t;
using u8 = std::uint8_t;
using i64 = std::int64_t;
using i32 = std::int32_t;
using i16 = std::int16_t;
using i8 = std::int8_t;
using usize = std::size_t;
using isize = std::ptrdiff_t;
i64 operator"" _i64(unsigned long long num) { return i64(num); }
u64 operator"" _u64(unsigned long long num) { return u64(num); }
#line 33 "main_a.cpp"
#define rep(i, a, b) for (i64 i = (a); (i) < (b); (i)++)
#define all(i) i.begin(), i.end()
#ifdef LOCAL
#define debug(...) \
std::cerr << "LINE: " << __LINE__ << " [" << #__VA_ARGS__ << "]:", \
debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
void debug_out() { std::cerr << std::endl; }
template <typename Head, typename... Tail>
void debug_out(Head h, Tail... t) {
std::cerr << " " << h;
if (sizeof...(t) > 0) std::cerr << " :";
debug_out(t...);
}
template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, std::pair<T1, T2> pa) {
return os << pa.first << " " << pa.second;
}
template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> vec) {
for (std::size_t i = 0; i < vec.size(); i++)
os << vec[i] << (i + 1 == vec.size() ? "" : " ");
return os;
}
template <typename T1, typename T2>
inline bool chmax(T1& a, T2 b) {
return a < b && (a = b, true);
}
template <typename T1, typename T2>
inline bool chmin(T1& a, T2 b) {
return a > b && (a = b, true);
}
template <typename Num,typename Func = std::multiplies<Num>>
constexpr Num mypow(Num a, u64 b, Num id = 1,Func mul =Func() ) {
if (b == 0) return id;
Num x = id;
while (b > 0) {
if (b & 1) x = mul(x,a);
a = mul(a,a);
b >>= 1;
}
return x;
}
/* template end */
int main() {
std::cin.tie(nullptr);
std::cerr.tie(nullptr);
std::ios::sync_with_stdio(false);
i64 testcase;
std::cin>>testcase;
while(testcase--){
i64 n;
std::cin>>n;
std::string s;
std::cin>>s>>s>>s;
rep(i,0,n)std::cout<<0;
rep(i,0,n)std::cout<<1;
std::cout<<0<<"\n";
}
return 0;
}
| #pragma GCC optimize ("O2")
#pragma GCC target ("avx")
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
//#include<atcoder/all>
//using namespace atcoder;
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please
char cn[1200010];
char* ci = cn, * di = cn;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint() {
ll tmp = *(ll*)ci;
int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0);
tmp = tmp << dig & ma1;
tmp = tmp * 10 + (tmp >> 8) & ma2;
tmp = tmp * 100 + (tmp >> 16) & ma3;
tmp = tmp * 10000 + (tmp >> 32) & ma4;
ci += 72 - dig >> 3;
return tmp;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
fread(cn, 1, 1200010, stdin);
int T = getint();
rep(t, T) {
int N = getint();
rep(i, N)* di++ = '0';
rep(i, N)* di++ = '1';
*di++ = '0';
*di++ = '\n';
ci += 6 * N + 3;
}
fwrite(cn, 1, di - cn, stdout);
Would you please return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using pint = pair<int, int>;
using ll = long long;
using ull = unsigned long long;
using vll = vector<long long>;
using pll = pair<ll, ll>;
#define FOR(i, begin, end) \
for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end) \
for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define VREP(s, ite) for (auto ite = s.begin(); ite != s.end(); ++ite)
#define FI first
#define SE second
#define ALL(v) v.begin(), v.end()
#define endl "\n"
#define ciosup \
cin.tie(0); \
ios::sync_with_stdio(false);
#define eb emplace_back
#define vint vector<int>
constexpr ll INF = 1e15 + 7LL;
constexpr ll MOD = 1e9 + 7LL;
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
for (int i = 0; i < v.size(); ++i) {
is >> v[i];
}
return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
if (v.size() == 0) return os;
for (int i = 0; i < v.size() - 1; ++i) {
os << v[i] << " ";
}
os << v[v.size() - 1];
return os;
}
void solve() {
char s,t;
cin >> s >> t;
if (s == 'Y') {
t += 'A' - 'a';
cout << t << endl;
} else {
cout << t << endl;
}
}
int main() {
solve();
char tmp;
while (cin >> tmp) {
cin.putback(tmp);
solve();
}
}
| #include <bits/stdc++.h>
int main(){
long long B, C;
std::cin >> B >> C;
if(B > 0){
long long ans = 1;
ans = ans + std::min(C / 2, B);
ans = ans + std::max(C / 2 - 1, 0ll);
ans = ans + (C >= 1);
ans = ans + std::min((C-1) / 2, B-1);
ans = ans + std::max((C-1) / 2, 0ll);
std::cout << ans << std::endl;
}else if(B < 0){
long long ans = 1;
ans = ans + (C >= 1);
ans = ans + std::min((C-1) / 2, std::abs(B));
ans = ans + std::max((C-1) / 2, 0ll);
ans = ans + std::max(std::min(C / 2 - 1, std::abs(B)-1), 0ll);
ans = ans + std::max(C / 2, 0ll);
std::cout << ans << std::endl;
}else{
if(C <= 1){
std::cout << 1 << std::endl;
}else{
std::cout << C << std::endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define llu unsigned long long int
#define sa(a,n,t) for(int ii=0;ii<n;ii++) scanf("%"#t"",&(a)[ii])
#define rep(i,n) for(int i=0;i<n;i++)
#define reps(i,a,n) for(int i=a;i<n;i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define rreps(i,a,n) for(int i=n-1;i>=a;i--)
#define validl(i,a,b) ((i>=a)&&(i<=b))?1:0
#define validmat(x,y,a,b,c,d) (validl(x,a,b) && validl(y,a,b))?1:0
#define validmn(x,y,m,n) validmat(x,y,0,m,0,n)
#define valid(i,n) validl(i,0,n)
#define v(t) vector<t>
#define vv(t) vector<vector<t> >
#define mk make_pair
#define pb push_back
#define pa(a,n,t) for(int i=0;i<n;i++) printf("%"#t"",a[i])
#define p2 pair<int,int>
#define p3 pair<p2,int>
#define fi first
#define se second
#define sd(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define pl(a) printf("%lld",a)
#define pd(a) printf("%d",a)
#define sf(a) scanf("%lf",&a)
#define pf(a) printf("%lf",a)
#define nline printf("\n")
#define ss(a) scanf("%s",a)
#define ps(a) printf("%s",a)
#define sc(a) scanf("%c",&a)
#define pc(a) printf("%c",&a)
#define mf(m,f) m.find(f)!=m.end()
#define pp3(m) cout<<m.fi.fi<<" "<<m.fi.se<<" "<<m.se<<" "
#define pp2(m) cout<<m.fi<<" "<<m.se<<" "
#define debug 0
/*v(int) adj[200001];
void dfs(int i,int p){
rep(j,adj[i].size()){
if(p!=adj[i][j])
dfs(adj[i][j],i);
}
}*/
unsigned long long int ex(unsigned long long int a,unsigned long long int n,unsigned long long int m)
{
//a=
if(n==0)
return 1;
if(n==1)
return a%m;
//n=n%(m-1);
unsigned long long int y=ex(a,n/2,m);
if(n%2==1)
return (a*((y*y)%m))%m;
else
return (y*y)%m;
}
int main()
{
lli n,m;
cin>>n>>m;
lli mod1=ex(10,n,m*m);
// lli mod2=ex(10,n,m);
//cout<<mod1<<" "<<mod2<<endl;
cout<<mod1/m<<endl;
// rep(i,m){
// if(i*m==(mod1-mod2+m*m)%(m*m)) {cout<<i<<endl;break;}
// }
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template <typename T> using ordered_set =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long
#define rep(i,j,n) for(int i=j;i<n;i++)
#define scn(a) scanf("%d",&a)
#define scns(a,b) scanf("%d %d",&a,&b)
#define print(a) printf("%d\n",a)
#define vec vector<int>
#define pb push_back
#define pairs pair<int,int>
#define f first
#define s second
#define all(v) v.begin(),v.end()
#define mem(a,b) memset(a,b,sizeof(a))
#define inf (int)1e9+7
const int mod = 1e9+7;
const int N = 5e5+5;
ll n;
int power(int a,ll b,int mod)
{
int res = 1;
while(b>0)
{
if(b&1)
res = (1LL*res*a)%mod;
a = (1LL*a*a)%mod, b >>= 1;
}
return res;
}
int main()
{
int test; test = 1;
while(test--)
{
int m; scanf("%lld %d",&n,&m);
// we can write any number in the form of N = (m*m)*k + c where c = remainder when N
// is divided by (m*m) and k = quotient. Here N = 10^n. We want (N/m)%m. So divide the
// equation by m we get on RHS = m*k + c/m. Now taking modulo by m our RHS becomes =
// 0 + (c/m)%m. Thus we have to calculate (c/m)%m. First calculate c and then final result.
int c = power(10,n,m*m), ans = (c/m)%m;
print(ans);
}
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ll n;
cin >> n;
ll left = 0;
ll right = 2000000000;
while(right - left > 1)
{
ll x = (left + right) / 2;
if(x * (x + 1) / 2 <= n + 1) left = x;
else right = x;
}
cout << n + 1- left << endl;
return 0;
} | #include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>
#include<cstdio>
#include<map>
#include<cassert>
using namespace std;
#define ll long long
#define reps(i, a, b) for(int i = a; i < b; i++)
#define rreps(i, a, b) for(int i = a-1; i >= b; i--)
#define rep(i, n) reps(i, 0, n)
#define rrep(i, n) rreps(i, n, 0)
#define P pair<int, int>
const ll mod = 1000000007;
const int INF = 1001001001;
// bool C(ll x){
// if(x > 1e9){
// }
// }
int main(){
ll n;
cin >> n;
ll m;
ll ans = n+1;
ll num = 1;
for (ll i = 2; num <= n + 1; i++){
ans--;
num += i;
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) x.begin(), x.end()
const ll M = 998244353;
int main(){
cin.tie(0)->sync_with_stdio(0);
ll h, w; cin >> h >> w;
vector<ll> p2(1005);
p2[0] = 1;
for(ll i = 1; i <= 1004; ++i) p2[i] = p2[i - 1] * 2 % M;
vector<string> g(h);
for(auto &v : g) cin >> v;
ll dot_row = 0;
vector<array<ll, 2>> pos;
for(int i = 0; i < h; ++i)
pos.push_back({i, 0});
for(int i = 1; i < w; ++i)
pos.push_back({h - 1, i});
for(auto &[x, y] : pos){
//cout << x << ' ' << y << '\n';
ll red = 0, blue = 0;
for(int i = 0; i <= min(x, w - 1 - y); ++i){
if(g[x - i][y + i] == 'R')
++red;
else if(g[x - i][y + i] == 'B')
++blue;
//cout << "!!" << ' ' << x - i << ' ' << y + i << ' ' << g[x - i][y + i] << '\n';
}
if(red == 0 && blue == 0) ++dot_row;
if(red && blue){
cout << 0 << '\n';
exit(0);
}
}
//cout << dot_row << '\n';
cout << p2[dot_row] % M << '\n';
}
| #include "bits/stdc++.h"
using namespace std;
// 定義
typedef long long ll;
typedef pair<ll, ll> P;
#define ALL(x) (x).begin(),(x).end()
#define REP(i, n) for(ll i = 0 ; i < (ll)n ; ++i)
#define REPN(i, m, n) for(ll i = m ; i < (ll)n ; ++i)
#define VL vector<ll>
#define VVL vector<vector<ll>>
#define VVVL vector<vector<vector<ll>>>
#define INF (ll)2e9
#define INF_LL 1LL<<60
//#define MOD 998244353
#define MOD 1000000007
ll Ceil(ll val, ll div) { return (val + div - 1) / div; }
ll CeilN(ll val, ll div) { return Ceil(val, div) * div; }
ll FloorN(ll x, ll n) { return (x - x % n); }
bool IsOdd(ll x) { return ((x & 1) == 1); }
bool IsEven(ll x) { return ((x & 1) == 0); }
void Solve()
{
ll N;
string S, X;
cin >> N >> S >> X;
reverse(ALL(S));
reverse(ALL(X));
VL amariRev(7);
REP(i, 7) {
amariRev[10 * i % 7] = i;
}
ll now;
ll amari;
ll prevIdx;
static ll dp[(ll)2e5 + 5][7];
dp[0][0] = 1;
REP(i, N - 1) {
ll matchVal = X[i] == 'T' ? 1 : 0;
ll fillVal = X[i] == 'T' ? 0 : 1;
fill(dp[i + 1], dp[i + 1] + 7, fillVal);
REP(j, 7) {
if (dp[i][j] == matchVal) {
now = S[i] - '0';
amari = (j - now + 14) % 7;
prevIdx = amariRev[amari];
dp[i + 1][prevIdx] = matchVal;
now = 0;
amari = (j - now + 14) % 7;
prevIdx = amariRev[amari];
dp[i + 1][prevIdx] = matchVal;
}
}
}
string ans;
ll sVal = (S.back() - '0') % 7;
if (X.back() == 'T') {
ans = (dp[N - 1][0] || dp[N - 1][sVal]) ? "Takahashi" : "Aoki";
} else {
ans = (dp[N - 1][0] && dp[N - 1][sVal]) ? "Takahashi" : "Aoki";
}
cout << ans << endl;
}
// メイン
int main()
{
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(15);
Solve();
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
long long sum[5][3000005];
int main() {
long long N,K;
cin >> N >> K;
for(int i = 1; i <= N; i++) {
sum[0][i] = 1;
}
for(int i = 1; i <= 1000000; i++) {
sum[1][i+1] += sum[0][i];
sum[1][i+N+1] -= sum[0][i];
}
for(int i = 1; i <= 2000000; i++) {
sum[1][i+1] += sum[1][i];
}
for(int i = 1; i <= 2000000; i++) {
sum[2][i+1] += sum[1][i];
sum[2][i+N+1] -= sum[1][i];
}
for(int i = 1; i <= 3000000; i++) {
sum[2][i+1] += sum[2][i];
}
for(int i = 1; i <= 3000000; i++) {
sum[2][i+1] += sum[2][i];
}
long long l = 0,r = N*3;
while (l+1 < r) {
long long mid = (l+r)/2;
if(sum[2][mid] >= K) {
r = mid;
}
else {
l = mid;
}
}
long long sum2 = 0;
for(int i = 1; i <= N; i++) {
int b = r-N-i;
int c = N;
if(b > N) {
continue;
}
if(b <= 0) {
c += b-1;
b = 1;
}
if(c <= 0) {
continue;
}
if(sum[2][r-1]+sum2+c-b+1 >= K) {
int a = i;
int d = b+K-sum[2][r-1]-sum2-1;
cout << a << " " << d << " " << r-a-d << endl;
return 0;
}
sum2 += c-b+1;
}
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long lli;
lli pref[2000005];
int main() {
lli n, k, m, sum, a, b, mini, maxi;
scanf("%lld %lld", &n, &k);
m = 2 * n;
for (lli i = 2; i <= m; i++)
pref[i] = pref[i - 1] + i - 1 - (2 * max(0LL, i - n - 1));
sum = 2;
m = 0;
while (m < k) {
k -= m;
sum++;
mini = max(1LL, sum - n - n);
maxi = min(n, sum - 2);
m = pref[sum - mini] - pref[sum - maxi - 1];
}
a = mini - 1;
m = 0;
while (m < k) {
k -= m;
a++;
m = pref[sum - a] - pref[sum - a - 1];
}
b = k - 1 + max(1ll, sum - a - n);
printf("%lld %lld %lld\n", a, b, sum - a - b);
return 0;
} |
#include <iostream>
#include <functional>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <iomanip>
#include <complex>
#include <random>
#include <bitset>
#include <list>
// #include <prettyprint.hpp>
using namespace std;
#define repi(i,n) for(int i=0;i<n;++i)
#define rep(i,n) for(ll i=0;i<n;++i)
#define repinvi(i,n) for(int i=n-1;i>=0;--i)
#define sll(n) scanf("%lld", &n);
#define sii(n) scanf("%d", &n);
#define slf(n) scanf("%lf", &n);
#define pll pair<ll,ll>
#define pii pair<int,int>
#define psi pair<si,si>
#define v(t) vector<t>
#define vv(t) vector<vector<t>>
#define vvv(t) vector<vector<vector<t>>>
typedef long long ll;
typedef double lf;
typedef short int si;
static const ll INF = 1e18;
static const ll max_n = 200010;
v(v(ll)) dp(max_n, v(ll)(2, 0));
// v(ll) cs(max_n);
// v(ll) xs(max_n);
void Main(){
ll N, X, C, x, c;
// cout << dp << endl;
cin >> N;
map<ll, v(ll)> cs2x;
v(ll) color_l;
rep(i, N){
cin >> x >> c;
cs2x[c].emplace_back(x);
}
for(auto p: cs2x){
sort(p.second.begin(), p.second.end());
cs2x[p.first] = p.second;
color_l.emplace_back(p.first);
}
// cout << cs2x <<endl;
sort(color_l.begin(), color_l.end());
// cout << "color_l:" << color_l << endl;
for(ll i=0;i<color_l.size(); ++i){
c = color_l[i];
ll now_x0, now_x1;
if(i==0){
now_x0 = dp[i][0];
now_x1 = dp[i][1];
}else{
ll pc = color_l[i-1];
now_x0 = cs2x[pc][0];
now_x1 = cs2x[pc][cs2x[pc].size()-1];
}
ll color_le = cs2x[c][0];
ll color_re = cs2x[c][cs2x[c].size()-1];
ll this_color_l = color_re - color_le;
ll dist_from0 = abs(now_x0 - color_re) + this_color_l + dp[i][0];
ll dist_from1 = abs(now_x1 - color_re) + this_color_l + dp[i][1];
dp[i+1][0] = min(dist_from0, dist_from1);
dist_from0 = abs(now_x0 - color_le) + this_color_l + dp[i][0];
dist_from1 = abs(now_x1 - color_le) + this_color_l + dp[i][1];
dp[i+1][1] = min(dist_from0, dist_from1);
// cout << "color r,l=" << color_re << "," << color_le <<endl;
// cout << "dp[" << i+1 << "]=" << dp[i+1] <<endl;
}
ll max_color = color_l[color_l.size()-1];
ll max_color_i = color_l.size();
ll ans = min(dp[max_color_i][0]+abs(cs2x[max_color][0]), dp[max_color_i][1]+abs(cs2x[max_color][cs2x[max_color].size()-1]));
cout << ans <<endl;
}
int main(){
Main();
} | #include <bits/stdc++.h>
using namespace std;
// template {{{
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
#define range(i, l, r) for (i64 i = (i64)(l); i < (i64)(r); (i) += 1)
#define rrange(i, l, r) for (i64 i = (i64)(r) - 1; i >= (i64)(l); (i) -= 1)
#define whole(f, x, ...) ([&](decltype((x)) container) { return (f)( begin(container), end(container), ## __VA_ARGS__); })(x)
#define rwhole(f, x, ...) ([&](decltype((x)) container) { return (f)( rbegin(container), rend(container), ## __VA_ARGS__); })(x)
#define debug(x) cerr << "(" << __LINE__ << ")" << #x << ": " << (x) << endl
constexpr i32 inf = 1001001001;
constexpr i64 infll = 1001001001001001001ll;
constexpr i32 dx[] = {0, -1, 1, 0, -1, 1, -1, 1};
constexpr i32 dy[] = {-1, 0, 0, 1, -1, -1, 1, 1};
struct IoSetup { IoSetup(i32 x = 15){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(x); cerr << fixed << setprecision(x); } } iosetup;
template <typename T = i64> T input() { T x; cin >> x; return x; }
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { range(i, 0, v.size()) { os << v[i] << (i + 1 != (int)v.size() ? " " : ""); } return os; }
template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; }
template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; }
template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }
// }}}
void solve() {
vector< int > as(3);
cin >> as;
rwhole(sort, as);
cout << as[0] + as[1] << endl;
}
signed main() {
solve();
}
|
#include <bits/stdc++.h>
/* #include <atcoder/all> */
using namespace std;
/* using namespace atcoder; */
using pint = pair<int, int>;
using ll = long long;
using ull = unsigned long long;
using vll = vector<long long>;
using vvll = vector<vector<long long>>;
using pll = pair<ll, ll>;
#define FOR(i, begin, end) \
for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end) \
for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define VREP(s, ite) for (auto ite = s.begin(); ite != s.end(); ++ite)
#define FI first
#define SE second
#define ALL(v) v.begin(), v.end()
#define endl "\n"
#define ciosup \
cin.tie(0); \
ios::sync_with_stdio(false);
#define eb emplace_back
#define vint vector<int>
#define vvint vector<vector<int>>
#define vbl vector<bool>
#define vvbl vector<vector<bool>>
constexpr ll INF = 1e15 + 7LL;
constexpr ll MOD = 1e9 + 7LL;
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
for (int i = 0; i < v.size(); ++i) {
is >> v[i];
}
return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
if (v.size() == 0) return os;
for (int i = 0; i < v.size() - 1; ++i) {
os << v[i] << " ";
}
os << v[v.size() - 1];
return os;
}
void solve() {
ll s,p;
cin >> s >> p;
for (ll i = 1; i*i <= p; ++i) {
if (p % i == 0) {
ll tmp = p / i;
if (tmp + i == s) {
cout << "Yes" << endl;
return;
}
}
}
cout << "No" << endl;
}
int main() {
solve();
char tmp;
while (cin >> tmp) {
cin.putback(tmp);
solve();
}
}
| #include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
using ll = long long;
using namespace std;
ll n, x;
ll a[50];
ll v[50];
ll l[50];
ll dp[51];
ll dq[51];
int main() {
cin >> n >> x;
for (int i = 0; i < n; ++i) cin >> a[i];
for (int i = 1; i < n; ++i) l[i - 1] = a[i] / a[i - 1] - 1;
l[n - 1] = 2e18;
ll s = x;
for (int i = n - 1; i >= 0; --i) {
v[i] = s / a[i];
s %= a[i];
}
dp[0] = 1;
for (int i = 0; i < n; ++i) {
if (v[i] == 0) {
dp[i + 1] = dp[i] + dq[i];
dq[i + 1] = dq[i];
} else {
dp[i + 1] = dp[i] + (v[i] < l[i]) * dq[i];
dq[i + 1] = dp[i] + dq[i];
}
}
cout << dp[n] << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
ll score(string S) {
vector<ll> cnt(10);
iota(cnt.begin(), cnt.end(), 0);
for (char c : S) cnt[c - '0'] *= 10;
return accumulate(cnt.begin(), cnt.end(), 0);
}
int main() {
ll K;
string S, T;
cin >> K >> S >> T;
vector<ll> cnt(10, K);
for (char c : S + T) cnt[c - '0']--;
ll win = 0;
for (ll x = 1; x <= 9; x++)
for (ll y = 1; y <= 9; y++) {
S.back() = '0' + x;
T.back() = '0' + y;
if (score(S) <= score(T)) continue;
win += cnt[x] * (cnt[y] - (x == y));
}
const ll rem = 9 * K - 8;
cout << fixed << setprecision(12) << (double)win / rem / (rem - 1) << endl;
}
| #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
#define fi first
#define se second
#define pb push_back
#define mod(n,k) ( ( ((n) % (k)) + (k) ) % (k))
#define forn(i,a,b) for(int i = a; i < b; i++)
#define forr(i,a,b) for(int i = a; i >= b; i--)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
const ll mod = 1e9+7;
int sum(int a, int b){return (a+b) % mod;}
int sub(int a, int b){return (a + mod - b) % mod;}
int mul(int a, int b){return (1ll * a * b) % mod;}
int power(int a,int b){
int res = 1;
while(b){
if(b&1)res = mul(res,a);
b >>= 1;
a = mul(a,a);
}
return res;
}
ll get(string str,char c){
map<int,int> cnt;
for(auto &x : str)cnt[x]++;
cnt[c]++;
ll res = 0;
for(char act = '1'; act <= '9'; act++){
ll tmp = act-'0';
res += tmp*pow(10,cnt[act]);
}
return res;
}
int K;
ld asd(char c1,char c2,map<char,int> cnt){
if(c1 != c2){
ld tmp1 = ld(K-cnt[c1])/(9*K-8);
ld tmp2 = ld(K-cnt[c2])/(9*K-8-1);
return tmp1*tmp2;
}
ld tmp1 = ld(K-cnt[c1])/(9*K-8);
ld tmp2 = ld(K-cnt[c2]-1)/(9*K-8-1);
return tmp1*tmp2;
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> K;
string s1,s2; cin >> s1 >> s2;
map<char,int> cnt;
for(auto &x : s1)cnt[x]++;
for(auto &x : s2)cnt[x]++;
ld res = 0;
for(char a = '1'; a <= '9'; a++){
for(char b = '1'; b <= '9'; b++){
cnt[a]++;
cnt[b]++;
if(cnt[a] <= K && cnt[b] <= K){
ll r1 = get(s1,a);
ll r2 = get(s2,b);
cnt[a]--;
cnt[b]--;
if(r1 > r2){
res += asd(a,b,cnt);
//cout << a << ' ' << b << '\n';
//debug(asd(a,b,cnt));
}
}else{
cnt[a]--;
cnt[b]--;
}
}
}
//debug(num,den);
//ld res = num/den;
cout << fixed << setprecision(15) << res << '\n';
return 0;
}
/*
__builtin_mul_overflow(x,y,&x)
-fsplit-stack
*/
|
#include <bits/stdc++.h>
using namespace std;
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << '['; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << ']'; }
template<typename T, size_t size> ostream& operator<<(ostream &os, const array<T, size> &arr) { os << '{'; string sep; for (const auto &x : arr) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '{' << p.first << ", " << p.second << '}'; }
template<typename T> ostream& operator<<(ostream &os, const set<T> &s) { os << '{'; string sep; for (const auto &x : s) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream& operator<<(ostream &os, const map<A, B> &mp) { os << '['; string sep; for (const auto &x : mp) os << sep << x, sep = ", "; return os << ']'; }
template<typename T> ostream& operator<<(ostream &os, const multiset<T> &s) { os << '{'; string sep; for (const auto &x : s) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#ifndef ONLINE_JUDGE
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "] =", dbg_out(__VA_ARGS__)
#define show_testcase(k) cerr << "test-case : " << k << endl;
#else
#define show_testcase(...)
#define dbg(...)
#endif
#define int long long
#define mod 1000000007
#define inf 1e18
#define endl '\n'
#define pb push_back
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define sz(x) (int)(x).size()
#define sb(x) __builtin_popcountll(x)
#define all(x) x.begin(),x.end()
#define rall(x) (x).rbegin(), (x).rend()
const string no = "NO";
const string yes = "YES";
void solve()
{
//cout << fixed << setprecision(10);
int n; cin >> n;
int ans = 0;
vector<pair<int, int>> v(n);
for (int i = 0; i < n; i++) {
cin >> v[i].ff;
}
for (int i = 0; i < n; i++) {
cin >> v[i].ss;
}
for (int num = 0; num <= 1000; num++) {
int cnt = 0;
for (int i = 0; i < n; i++) {
if (num >= v[i].ff && num <= v[i].ss) {
cnt++;
}
}
if (cnt == n) {
ans++;
}
}
cout << ans << endl;
}
int32_t main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t = 1;
// cin >> t;
for (int tc = 1; tc <= t; tc++) {
show_testcase(tc);
solve();
}
return 0;
}
| #include <iostream>
using namespace std;
int main() {
int N;
cin>>N;
int A[N];
int B[N];
for(int i=0;i<N;i++)
{
cin>>A[i];
}
for(int i=0;i<N;i++)
{
cin>>B[i];
}
int x=A[0],y=B[0];
for(int i=1;i<N;i++)
{
if((x < A[i]))
{
x =A[i];
}
if((y > B[i]))
{
y =B[i];
}
}
if(y >= x)
{
cout<<y-x+1<<endl;
}
else
{
cout<<0<<endl;
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n); i++)
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
int main(){
ll r1, c1, r2, c2;
cin >> r1 >> c1 >> r2 >> c2;
auto ok = [&](ll a, ll b, ll c, ll d) {
if (a == c && b == d) return 0;
if (a + b == c + d || a - b == c - d || abs(a - c) + abs(b - d) <= 3) return 1;
if ((a + b) % 2 == (c + d) % 2) return 2;
for (ll i = c - 3; i <= c + 3; i++) {
for (ll j = d - 3; j <= d + 3; j++) {
if (abs(i - c) + abs(j - d) > 3) continue;
if (i + j == a + b) return 2;
if (i - j == a - b) return 2;
}
}
return 3;
};
cout << ok(r1, c1, r2, c2) << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i,a,b) for(ll i=a,i##end=b;i<=i##end;i++)
#define drep(i,a,b) for(ll i=a,i##end=b;i>=i##end;i--)
typedef long long ll;
inline ll read() {
ll x=0,f=1;char c=getchar();
while(c<48||c>57){if(c=='-')f=-1;c=getchar();}
while(c>=48&&c<=57)x=(x<<1)+(x<<3)+(c^48),c=getchar();
return x*f;
}
using namespace std;
ll dp[1<<21];
int P[]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71};
int main() {
ll l=read(),r=read();
dp[0]=1;
rep(i,l,r){
int op=0;
rep(j,0,19)
if(i%P[j]==0)op|=(1<<j);
drep(j,(1<<20)-1,0)
if((j&op)==0)dp[j|op]+=dp[j];
}
ll ans=0;
rep(i,0,(1<<20)-1)ans+=dp[i];
printf("%lld\n",ans);
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define int long long
#define rep(i,a,b) for(int i=a;i<b;i++)
#define repn(i,a,b) for(int i=a;i>=b;i--)
#define F first
#define S second
#define pii pair<int,int>
#define vi vector<int>
#define vii vector<pii>
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define mod 1000000007
const int N = 100001;
int fexp(int a, int b,int m){int ans = 1;while(b){if(b&1)ans=(ans*a)%m; b/=2;a=(a*a)%m;}return ans;}
int inverse(int a, int p){return fexp(a, p-2,p);}
// int fac[2000001];
// int nCrModPFermat(int n, int r,int p) {
// if (r==0) return 1;
// return (((fac[n]*((((inverse(fac[r],p)%p)*inverse(fac[n-r],p))%p))))%p);
// }
void solve(){
int n,a,b;cin>>n>>a>>b;
cout<<n-a+b;
}
signed main(){
IOS;
int t=1;
// cin>>t;
// fac[0] = 1;
// for (int i=1 ; i<=100001; i++) fac[i] = fac[i-1]*i%mod;
rep(i,0,t){
// cout<<"Case #"<<i+1<<": ";
solve();
cout<<"\n";
}
// return 0;
} | #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;
using ll = long long;
constexpr int INF = 1001001001;
// constexpr int mod = 1000000007;
constexpr int mod = 998244353;
template<class T>
inline bool chmax(T& x, T y){
if(x < y){
x = y;
return true;
}
return false;
}
template<class T>
inline bool chmin(T& x, T y){
if(x > y){
x = y;
return true;
}
return false;
}
struct mint {
int x;
mint() : x(0) {}
mint(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
mint& operator+=(const mint& p){
if((x += p.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint& p){
if((x -= p.x) < 0) x += mod;
return *this;
}
mint& operator*=(const mint& p){
x = (int)(1LL * x * p.x % mod);
return *this;
}
mint& operator/=(const mint& p){
*this *= p.inverse();
return *this;
}
mint operator-() const { return mint(-x); }
mint operator+(const mint& p) const { return mint(*this) += p; }
mint operator-(const mint& p) const { return mint(*this) -= p; }
mint operator*(const mint& p) const { return mint(*this) *= p; }
mint operator/(const mint& p) const { return mint(*this) /= p; }
bool operator==(const mint& p) const { return x == p.x; }
bool operator!=(const mint& p) const { return x != p.x; }
mint pow(int64_t n) const {
mint res = 1, mul = x;
while(n > 0){
if(n & 1) res *= mul;
mul *= mul;
n >>= 1;
}
return res;
}
mint inverse() const { return pow(mod - 2); }
friend ostream& operator<<(ostream& os, const mint& p){
return os << p.x;
}
friend istream& operator>>(istream& is, mint& p){
int64_t val;
is >> val;
p = mint(val);
return is;
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, M;
cin >> N >> M;
vector<vector<mint>> powx(M + 1, vector<mint>(N + 1, 1));
for(int i = 0; i <= M; ++i){
for(int j = 0; j < N; ++j){
powx[i][j + 1] = powx[i][j] * i;
}
}
mint ans = powx[M][N] * N;
for(int d = 1; d < N; ++d){
for(int x = 1; x <= M; ++x){
ans -= powx[M - x][d - 1] * powx[M][N - d - 1] * (N - d);
}
}
cout << ans << endl;
return 0;
} |
#include <set>
#include <iostream>
using namespace std;
#define int long long
const int N = 2e5 + 10;
int par[N], c[N];
set<pair<int, int>> s[N];
void make_set(int x) {
par[x] = x;
s[x].insert({c[x], 1});
}
int find_set(int x) {
if (x == par[x]) {
return x;
}
return par[x] = find_set(par[x]);
}
void union_sets(int x, int y) {
x = find_set(x);
y = find_set(y);
if (x != y) {
if (s[x].size() > s[y].size()) {
swap(x, y);
}
par[x] = y;
for (auto z : s[x]) {
auto it = s[y].lower_bound({z.first, -1});
int cur = z.second;
if (!(it == s[y].end() || it->first != z.first)) {
cur += it->second;
s[y].erase(it);
}
s[y].insert({z.first, cur});
}
}
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, q;
cin >> n >> q;
for (int i = 1; i <= n; ++i) {
cin >> c[i];
make_set(i);
}
while (q--) {
int x, y, z;
cin >> x >> y >> z;
if (x - 1) {
int w = find_set(y);
auto it = s[w].lower_bound({z, -1});
if (it == s[w].end() || it->first != z) {
cout << "0\n";
} else {
cout << it->second << '\n';
}
} else {
union_sets(y, z);
}
}
return 0;
} | // C++(GCC 9.2.1)
#include <bits/stdc++.h>
using namespace std;
#define repex(i, a, b, c) for(int i = a; i < b; i += c)
#define repx(i, a, b) repex(i, a, b, 1)
#define rep(i, n) repx(i, 0, n)
#define repr(i, a, b) for(int i = a; i >= b; i--)
#define a first
#define b second
int c[202020];
// https://github.com/atcoder/live_library/blob/master/uf.cpp
// UnionFind
// coding: https://youtu.be/TdR816rqc3s?t=726
// comment: https://youtu.be/TdR816rqc3s?t=6822
// -> 一部改変.
struct UnionFind{
vector<int> d; // 生徒同士が, 同じ集団に属するか否かを管理.
vector<map<int, int>> c; // 生徒を含む集団が属するクラス情報を管理.
set<int> s; // 合流済みの生徒であるか判定.
UnionFind(int n = 0): d(n, -1), c(n) {}
int find(int x){
return (d[x] < 0) ? x : (d[x] = find(d[x]));
}
bool unite(int ox, int oy, int cx, int cy){
int x = find(ox);
int y = find(oy);
if(x == y) return false;
if(d[x] > d[y]) swap(x, y);
d[x] += d[y];
d[y] = x;
// 生徒を含む集団が属するクラス情報を更新.
if(s.count(ox) == 0) c[x][cx]++;
if(s.count(oy) == 0) c[x][cy]++;
for(auto &p : c[y]) c[x][p.a] += p.b;
// 合流済みフラグ設定.
s.insert(ox);
s.insert(oy);
return true;
}
bool same(int x, int y){
return find(x) == find(y);
}
int size(int x){
return -d[find(x)];
}
int getClassSize(int ox, int cy){
int x = find(ox);
return c[x][cy];
}
void debug(){
puts("--- d[i] start ---");
rep(i, d.size()) printf("%d ", d[i]);
puts("\n--- d[i] end ----");
puts("--- find start ---");
rep(i, d.size()) printf("%d ", find(i));
puts("\n--- find end ----");
return;
}
void debugClass(int ox){
int x = find(ox);
puts("--- debugClass start ---");
for(auto &p : c[x]) printf("class=%d size=%d\n", p.a, p.b);
puts("\n--- debugClass end ----");
return;
}
};
int main(){
// 1. 入力情報.
int N, Q;
scanf("%d %d", &N, &Q);
rep(i, N) scanf("%d", &c[i]);
// 2. クエリ回答.
UnionFind uf(N);
rep(i, Q){
// 2-1. クエリ種別.
int f;
scanf("%d", &f);
// 2-2. f = 1 の 場合.
if(f == 1){
int a, b;
scanf("%d %d", &a, &b);
a--, b--;
uf.unite(a, b, c[a], c[b]);
}
// 2-3. f = 2 の 場合.
if(f == 2){
int x, y;
scanf("%d %d", &x, &y);
x--;
int ans = uf.getClassSize(x, y);
if(uf.size(x) == 1 && c[x] == y) ans++; // 生徒 x のみの場合を考慮.
printf("%d\n", ans);
}
}
return 0;
} |
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 1e5 + 5;
long long temp[N];
int main()
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n;i++)
{
int a;
scanf("%d", &a);
temp[a % 200]++;
}
long long ans = 0;
for (int i = 0; i < 200;i++)
ans += (temp[i] * (temp[i] - 1)) / 2;
printf("%lld\n", ans);
} | #include<bits/stdc++.h>
#define re register int
#define ll long long
#define dl long double
#define LL ll
#define V inline void
#define I inline int
#define FOR(i,a,b) for(re i=(a),i##i=(b);i<=i##i;++i)
#define ROF(i,a,b) for(re i=(a),i##i=(b);i>=i##i;--i)
#define gc getchar()
//#define gc (fs==ft&&(ft=(fs=buf)+fread(buf,1,1<<18,stdin),fs==ft))?0:*fs++
using namespace std;
char *fs,*ft,buf[1<<18];
const int N=5e5+10,mo=998244353,G=3;
LL read(){
ll p=0; bool w=0; char ch=gc;
while(!isdigit(ch)) w=ch=='-',ch=gc;
while(isdigit(ch)) p=p*10+ch-'0',ch=gc;
return w?-p:p;
}
int n;
ll a[N],s1[N],s2[N],ans;
int main(){
n=read();
FOR(i,1,n) a[i]=read(),s1[i]=s1[i-1]+a[i],s2[i]=s2[i-1]+a[i]*a[i];
FOR(i,2,n) ans+=a[i]*a[i]*(i-1)-2*a[i]*s1[i-1]+s2[i-1];
cout<<ans;
return 0;
} |
#include<bits/stdc++.h>
#include<iomanip>
#include<cmath>
using namespace std;
typedef long long int lli;
#define PB push_back
#define MP make_pair
#define FR first
#define SR second
#define LB lower_bound
#define UB upper_bound
typedef vector<long long int> vi;
typedef pair<long long int,long long int> pi;
#define all(a) (a).begin(),(a).end()
#define allr(a) (a).rbegin(),(a).rend()
#define FOR(i,a,b) for(int i=a;i<b;i++)
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
lli N,M;
cin>>N>>M;
vi a(M);
FOR(i,0,M)
{
cin>>a[i];
}
if(M==0)
{
cout<<1;
}
else
{
sort(all(a));
vi interval;
if(a[0]-1-0!=0)
interval.PB(a[0]-1-0);
FOR(i,1,M)
{
if(a[i]-a[i-1]-1!=0)
interval.PB(a[i]-a[i-1]-1);
}
if(N+1-a[M-1]-1!=0)
interval.PB(N-a[M-1]);
lli ans=INT_MAX;
FOR(i,0,interval.size())
{
ans=min(ans,interval[i]);
}
lli fintime=0;
FOR(i,0,interval.size())
{
lli cal=(interval[i]+ans-1)/ans;
fintime=fintime+cal;
}
cout<<fintime;
}
} | #pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define pi 3.141592653589793238
#define int long long
#define ld long double
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
long long powm(long long a, long long b,long long m) {
long long res = 1;
while (b > 0) {
if (b & 1)
res = res * a %m;
a = a * a %m;
b >>= 1;
}
return res;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);
#ifndef ONLINE_JUDGE
if(fopen("INPUT.txt","r"))
{
freopen ("INPUT.txt" , "r" , stdin);
freopen ("OUTPUT.txt" , "w" , stdout);
}
#endif
int n,m;
cin>>n>>m;
int a[m+4];
a[0]=0;
a[m+1]=n+1;
for(int i=1;i<=m;i++)
{
cin>>a[i];
}
sort(a,a+m+2);
int mn=1e17;
for(int i=1;i<=m+1;i++)
{
if(a[i]-a[i-1]>1)
mn=min(mn,a[i]-a[i-1]-1);
}
int ans=0;
for(int i=1;i<=m+1;i++)
{
ans+=(a[i]-a[i-1]-1+mn-1)/mn;
}
cout<<ans;
}
|
#include <iostream>
#include <math.h>
#include <iomanip>
#include <bits/stdc++.h>
#include <string.h>
#include <string>
#include <algorithm>
#define ll long long int
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
ll binomialCoeff(ll n,ll k)
{
ll res = 1;
if(k >n - k)
k = n - k;
for(int i = 0; i < k; ++i)
{
res *= (n - i);
res /= (i + 1);
}
return res;
}
ll power(ll x,ll y,ll mod = 0)
{
ll temp;
if(y == 0)
return 1;
temp = power(x, y/2,mod);
if(!mod)
{
if(y%2)
return temp*temp;
else
return temp*temp*x;
}
if (y%2 == 0)
return (temp*temp)%mod;
else
return (((x*temp)%mod)*temp)%mod;
}
//nCr % mod
//ll mod = 1e9 + 7;
//const int N0 = 2e5 + 5;
//ll fact[6*N0];
//ll inv[6*N0],invfac[6*N0];
//
//void factorial()
//{
// fact[0] = invfac[0] = fact[1] = invfac[1] = 1;
// inv[1] = 1;
// for(int i=2;i<=5*N0 + 10;i++)
// {
// fact[i] = (fact[i-1]*i)%mod;
// inv[i] = (inv[mod%i]*(mod - mod/i))%mod;
// invfac[i] = (invfac[i-1]*inv[i])%mod;
// }
//}
vector<ll> primes;
void Sieve(int n)
{
bool prime[n+1];
memset(prime, true, sizeof(prime));
for (int p=2; p*p<=n; p++)
{
if (prime[p] == true)
{
for (int i=p*p; i<=n; i += p)
prime[i] = false;
}
}
for (int p=2; p<=n; p++)
if (prime[p])
primes.push_back(p);
}
//****************************************************** CHECK CONSTRAINTS ***************************************************************//
const int N = 1005;
int main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int t;
cin >> t;
while(t--)
{
ll l,r;
cin >> l >> r;
ll ans = 0;
ll take = r - l;
ll x = r - l - l + 1;
if(x <= 0)
{
cout << 0 << "\n";
continue;
}
ans = x*(x + 1)/2;
cout << ans << "\n";
}
}
| #include<bits/stdc++.h>
using namespace std;
#define fi(a,b) for(int i=a;i<b;i++)
#define fj(a,b) for(int j=a;j<b;j++)
#define ff first
#define ss second
#define ll long long
#define ld long double
#define ull unsigned long long
#define bp(x) __builtin_popcount(x)
#define pr(x) for(auto it: x) cout<<it<<" "; cout<<endl;
#define getMax(x) max_element(x.begin(),x.end())
#define getMin(x) min_element(x.begin(),x.end())
#define endl "\n"
typedef vector<int> vi;
typedef vector< pair<int, int> > vii;
typedef vector<long long> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector< pair<ll, ll> > vll;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// auto dist = uniform_int_distribution<int>(l, r);
// int dx[] = {1, 0, -1, 0};
// int dy[] = {0, 1, 0, -1};
// int dx[] = { -1, 0, 1, 1, 1, 0, -1, -1};
// int dy[] = { -1, -1, -1, 0, 1, 1, 1, 0};
void omae_wa_mou_shindeiru(int tc) {
ll n, k;
cin >> n >> k;
vl dp2(3 * n + 3);
vl dp3(3 * n + 3);
fi(1, n + 1) {
dp2[i + 1]++;
dp2[i + n + 1]--;
}
fi(1, 3 * n + 2) dp2[i] += dp2[i - 1];
fi(1, 2 * n + 2) {
dp3[i + 1] += dp2[i];
dp3[i + n + 1] -= dp2[i];
}
fi(1, 3 * n + 1) dp3[i] += dp3[i - 1];
ll x;
for (int i = 3; i <= 3 * n; i++) {
if (k <= dp3[i]) {x = i; break;}
else {k -= dp3[i];}
}
for (int i = 1; i <= n; i++) {
int jmi = max(1LL, x - i - n);
int jma = min(n, x - i - 1);
if (jmi > jma) {continue;}
if (k > (jma - jmi + 1)) {k -= (jma - jmi + 1); continue;}
int y = jmi + k - 1;
int z = x - i - y;
cout << i << ' ' << y << ' ' << z << '\n';
return;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int tc = 1;
// cin >> tc;
fi(1, tc + 1) {
omae_wa_mou_shindeiru(i);
}
}
|
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <queue>
#include <bitset>
#include <stack>
#include <functional>
// AtCoder
// #include <atcoder/all>
// using namespace atcoder;
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...)
#endif
#define rep_(i, a_, b_, a, b, ...) for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define reprev_(i, a_, b_, a, b, ...) for (int i = (b)-1, i##_min = (a); i >= i##_min; --i)
#define reprev(i, ...) reprev_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define all(x) (x).begin(), (x).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; }
#define fls(x) (64 - __builtin_clzll(x))
#define pcnt(x) __builtin_popcountll(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int,int> P;
typedef long double ld;
int main (void)
{
cin.tie(0);
ios::sync_with_stdio(false);
int n; cin >> n;
vector<ll> a(n), m(n); rep (i, n) cin >> a[i];
partial_sum(all(a), m.begin(), [](ll a, ll b){return max(a, b);});
partial_sum(all(a), a.begin());
partial_sum(all(a), a.begin());
rep (i, n) cout << a[i] + m[i] * (i + 1) << '\n';
return 0;
} | #include <bits/stdc++.h>
using namespace std;
const int nxm = 2e5 + 10;
long long n, a[nxm], Max, s1[nxm], s2[nxm];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> a[i];
s1[i] = s1[i - 1] + a[i];
}
for(int i = 1; i <= n; i++)
s2[i] = s2[i - 1] + s1[i];
Max = a[1];
for(int i = 1; i <= n; i++)
{
Max = max(Max, a[i]);
cout << 1ll*(s1[i] + i*Max + s2[i - 1]) << "\n";
}
return 0;
}
|
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
#ifdef DEBUG
#define eprintf(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#else
#define eprintf(...) ;
#endif
#define sz(x) ((int) (x).size())
#define TASK "text"
const int inf = (int) 1.01e9;
const long long infll = (long long) 1.01e18;
const ld eps = 1e-9;
const ld pi = acos((ld) -1);
#ifdef DEBUG
mt19937 mrand(300);
#else
mt19937 mrand(chrono::steady_clock::now().time_since_epoch().count());
#endif
int rnd(int x) {
return mrand() % x;
}
void precalc() {
}
const int maxn = (int) 1e5 + 5;
int n, l;
int a[maxn], b[maxn];
bool read() {
if (scanf("%d%d", &n, &l) < 2) {
return false;
}
l -= n - 1;
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
a[i] -= i + 1;
}
for (int i = 0; i < n; ++i) {
scanf("%d", &b[i]);
b[i] -= i + 1;
}
return true;
}
void solve() {
for (int i = 0; i < n; ++i) {
if (b[i] == 0 || b[i] == l - 1) {
continue;
}
int pos = lower_bound(a, a + n, b[i]) - a;
if (pos >= n || a[pos] != b[i]) {
printf("-1\n");
return;
}
}
long long res = 0;
for (int i = 0; i < n; ++i) {
if (b[i] == a[i]) {
continue;
}
if (b[i] < a[i]) {
if (b[i] == 0 || (i && b[i] == b[i - 1])) {
++res;
continue;
}
int pos = upper_bound(a, a + n, b[i]) - a - 1;
res += i - pos;
} else {
if (b[i] == l - 1 || (i + 1 < n && b[i] == b[i + 1])) {
++res;
continue;
}
int pos = lower_bound(a, a + n, b[i]) - a;
res += pos - i;
}
}
printf("%lld\n", res);
}
int main() {
precalc();
#ifdef DEBUG
assert(freopen(TASK ".in", "r", stdin));
assert(freopen(TASK ".out", "w", stdout));
#endif
while (read()) {
solve();
#ifdef DEBUG
eprintf("Time %.2f\n", (double) clock() / CLOCKS_PER_SEC);
#endif
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
const int N = 1e5 + 5;
int n, L, a[N], b[N];
int main() {
scanf("%d %d", &n, &L);
for (int i = 1; i <= n; ++i)
scanf("%d", &a[i]);
for (int i = 1; i <= n; ++i)
scanf("%d", &b[i]);
++n;
a[n] = b[n] = L + 1;
++n;
for (int i = 0; i < n; ++i) {
a[i] -= i, b[i] -= i;
}
int l1 = 0, l2 = 0;
ll ans = 0;
while (l1 < n && l2 < n) {
int r2 = l2 + 1;
while (r2 < n && b[r2] == b[l2])
++r2;
while (l1 < n && a[l1] < b[l2])
++l1;
if (l1 >= n || a[l1] != b[l2]) {
puts("-1");
exit(0);
}
int r1 = l1 + 1;
while (r1 < n && a[r1] == a[l1])
++r1;
r1--, r2--;
if (l2 < l1) ans += l1 - l2;
if (r2 > r1) ans += r2 - r1;
l1 = r1 + 1;
l2 = r2 + 1;
}
printf("%lld\n", ans);
}
|
// #pragma GCC optimise ("O1")
#include<bits/stdc++.h>
using namespace std;
#define ll int
#define rep(i,j,n) for(ll i=j;i<n;i++)
#define scn(a) scanf("%d",&a)
#define scns(a,b) scanf("%d %d",&a,&b)
#define print(a) printf("%0.10lf",a)
#define vec vector<ll>
#define pb push_back
#define pairs pair<ll,ll>
#define f first
#define s second
#define all(v) v.begin(),v.end()
#define srt(v) sort(v.begin(),v.end())
#define read(a) for(auto &it:a) cin>>it;
#define write(a) for(auto it:a) cout<<it<<" ";
#define mem(a,b) memset(a,b,sizeof(a))
#define nl printf("\n")
#define inf 1e9
#define zer INT_MIN
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
const int N=2e3+5;
int main()
{
int t=1;
// cin>>t;
while(t--)
{
int n,k; scns(n,k);
int cost[n+5][n+5];
rep(i,1,n+1)
rep(j,1,n+1)
scn(cost[i][j]);
vec v;
int cnt=0;
rep(i,2,n+1)
v.pb(i);
do
{
int amt=0;
amt+=cost[1][v[0]];
// for(auto it:v)
// cout<<it<<" ";
// cout<<endl;
rep(i,1,v.size())
amt+=(cost[v[i-1]][v[i]]);
amt+=cost[v[v.size() - 1]][1];
// cout<<amt<<" ";
if(amt==k)
cnt++;
} while(next_permutation(all(v)));
cout<<cnt;
}
}
| //Luka Mitrovic
#include <iostream>
#include <cmath>
#include <iomanip>
#include <algorithm>
using namespace std;
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int array[9][9];
int s_arr[9];
int n,k;
int ans = 0, sum = 0;
cin >> n >> k;
for (int i = 0; i < n; i++) {
s_arr[i] = i;
for (int j = 0; j < n; j++) {
cin >> array[i][j];
}
}
for (int i = 0; i < n; i++) {
sum += array[s_arr[i]][s_arr[(i+1)%n]];
}
if (sum == k) ans++;
//cout << sum;
for (int z = 0; z < 10000; z++) {
bool f = false;
sum = 0;
for (int i = n-2; i > 0; i--) {
if (f) break;
if (s_arr[i] < s_arr[i+1]) { // i = 1
for (int j = n-1; j > i; j--) { // i = 3, j = 4
if (s_arr[j] > s_arr[i]) { // j = 3
int a = s_arr[j];
s_arr[j] = s_arr[i];
s_arr[i] = a;
if (i < n-2) { // i = 3, j = 4 ..
int start = i+1; // start = 4
int end = start + (n-i)/2; // end = 4 + (5-3)/2 = 4 + 1 = 5
for (int s = start, l = 1; s < end; s++,l++) {
int b = s_arr[s]; // s = 2,3
s_arr[s] = s_arr[n-l]; // arr[2] <-> arr[3]
s_arr[n-l] = b; // arr[3] <-> arr[2]
}
}
for (int s = 0; s < n; s++) {
sum += array[s_arr[s]][s_arr[(s+1)%n]];
}
if (sum == k) ans++;
f = true;
break;
}
}
}
}
}
cout << ans << endl;
} |
#include<bits/stdc++.h>
#define rep(i, m, n) for(int i = m; i < (int)(n); i++)
using namespace std;
using ll = long long;
int main() {
int n;
cin >> n;
vector<ll> a(n), p(n), x(n);
rep(i, 0, n) cin >> a[i] >> p[i] >> x[i];
ll ans = 100100100100;
ll karians = 100100100100;
bool kau = false;
rep(i, 0, n) {
x[i] -= a[i];
if(x[i] > 0) {
karians = p[i];
kau = true;
}
ans = min(ans, karians);
karians = 100100100100;
}
if(kau) cout << ans << endl;
else cout << "-1" << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, s, e) for (int i = (int)(s); i < (int)(e); i++)
#define _rep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define _REP(i, e, s) for (int i = (int)(e - 1); i >= (int)(s); i--)
#define yes cout << "yes" << endl;
#define Yes cout << "Yes" << endl;
#define YES cout << "YES" << endl;
#define no cout << "no" << endl;
#define No cout << "No" << endl;
#define NO cout << "NO" << endl;
#define AC cout << "AC" << endl;
#define WA cout << "WA" << endl;
#define out(s) cout << s << endl;
#define ll long long
#define ull unsigned long long
#define ALL(x) x.begin(), x.end()
#define SIZE(x) ll(x.size())
const unsigned int BIT_FLAG_0 = (1 << 0); // 0000 0000 0000 0001
const unsigned int BIT_FLAG_1 = (1 << 1); // 0000 0000 0000 0010
const unsigned int BIT_FLAG_2 = (1 << 2); // 0000 0000 0000 0100
const unsigned int BIT_FLAG_3 = (1 << 3); // 0000 0000 0000 1000
const unsigned int BIT_FLAG_4 = (1 << 4); // 0000 0000 0001 0000
const unsigned int BIT_FLAG_5 = (1 << 5); // 0000 0000 0010 0000
const unsigned int BIT_FLAG_6 = (1 << 6); // 0000 0000 0100 0000
const unsigned int BIT_FLAG_7 = (1 << 7); // 0000 0000 1000 0000
const double PI = 3.14159265358979323846;
const int dy[4] = {0, 1, 0, -1};
const int dx[4] = {1, 0, -1, 0};
#define H_MAX 500
#define W_MAX 500
#define INF 1e9 + 7
const long long MOD = 998244353;
int main() {
int N, A, P, X;
cin >> N;
int ans = INF;
for (int i = 0; i < N; i++) {
cin >> A >> P >> X;
if (X - A > 0) {
ans = min(ans, P);
}
}
if (ans == INF) {
cout << -1 << endl;
} else {
cout << ans << endl;
}
} |
#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << "(" << p.first << "," << p.second << ")";
return os;
}
#ifdef __LOCAL
#define debug(x) cerr << __LINE__ << ": " << #x << " = " << (x) << '\n'
#define debugArray(x, n) \
cerr << __LINE__ << ": " << #x << " = {"; \
for (long long hoge = 0; (hoge) < (long long)(n); ++(hoge)) \
cerr << ((hoge) ? "," : "") << x[hoge]; \
cerr << "}" << '\n'
#define debugMatrix(x, h, w) \
cerr << __LINE__ << ": " << #x << " =\n"; \
for (long long hoge = 0; (hoge) < (long long)(h); ++(hoge)) { \
cerr << ((hoge ? " {" : "{{")); \
for (long long fuga = 0; (fuga) < (long long)(w); ++(fuga)) \
cerr << ((fuga ? ", " : "")) << x[hoge][fuga]; \
cerr << "}" << (hoge + 1 == (long long)(h) ? "}" : ",") << '\n'; \
}
#else
#define debug(x) (void(0))
#define debugArray(x, n) (void(0))
#define debugMatrix(x, h, w) (void(0))
#endif
signed main() {
cin.tie(0);
ios::sync_with_stdio(0);
int N;
cin >> N;
int info[2 * N];
fill(info, info + 2 * N, -1);
int A[N], B[N];
bool ng = false;
for (int i = 0; i < N; i++) {
cin >> A[i] >> B[i];
if (A[i] != -1) {
if (info[--A[i]] != -1) ng = true;
info[A[i]] = i;
}
if (B[i] != -1) {
if (info[--B[i]] != -1) ng = true;
info[B[i]] = i + N;
}
}
if (ng) {
cout << "No" << '\n';
return 0;
}
auto check = [&](int l, int r) {
int k = r - l + 1;
for (int i = l; i - l < k / 2; i++) {
if (info[i] != -1 && info[i] >= N) return false;
if (info[i + k / 2] != -1 && info[i + k / 2] < N) return false;
if (info[i] != -1 && info[i + k / 2] != -1
&& info[i] != info[i + k / 2] - N)
return false;
if (info[i] != -1 && B[info[i]] != -1 && B[info[i]] != i + k / 2)
return false;
if (info[i + k / 2] != -1 && A[info[i + k / 2] - N] != -1
&& A[info[i + k / 2] - N] != i)
return false;
}
return true;
};
bool dp[2 * N][2 * N];
for (int k = 2; k <= 2 * N; k += 2) {
for (int l = 0; l + k - 1 < 2 * N; l += 2) {
int r = l + k - 1;
dp[l][r] = check(l, r);
for (int j = l + 1; j + 1 < r; j += 2)
dp[l][r] |= dp[l][j] & dp[j + 1][r];
}
}
cout << (dp[0][2 * N - 1] ? "Yes" : "No") << '\n';
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
template<typename T>
void read(T&x){
x=0;
char ch=getchar();
int f=1;
while(!isdigit(ch)){
if(ch=='-')f*=-1;
ch=getchar();
}
while(isdigit(ch)){
x=x*10+(ch-'0');
ch=getchar();
}x*=f;
}
//========================================================
typedef long long ll;
ll t,l,r;
int main(){
read(t);
while(t--){
read(l),read(r);
printf("%lld\n",r>=2*l?(r-l+1)*(r-2*l+1)-r*(r-2*l+1)/2:0ll);
}
return 0;
} |
/* ** ** ****** ****** ****** ** ****** ***
** ** ** ** ** ** ** ** ** ** ** *********
**\../** ****** ****** ** ** ****** ** ** ***
** ** ** ** ** ** ** ** ** ** ** ** *****
** ** ** ** ** ** ** ****** ****** ****** *****
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define PI 3.14159265358979323846
#define lp(i,n) for(int i = 0; i <n;i++)
#define rlp(i,n) for(int i = int(n);i >= 0;i--)
#define range(i,a,b) for(int i = int(a);i <= int(b);i++)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define MP make_pair
#define fr first
#define sc second
#define vt vector
#define sz(v) ((int)((v).size()))
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
template<class T> using ordered_set=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
const ll MOD = 1e9 + 7;
const int OO = (int)2e9;
const int N = (int)1e7;
int dx[] = {0,0,-1,1};
int dy[] = {1,-1,0,0};
void init(){ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);}
template<class T> void read(T& x) {
cin >> x;
}
void read(double& d) {
string t;
read(t);
d=stod(t);
}
void read(long double& d) {
string t;
read(t);
d=stold(t);
}
template<class H, class... T> void read(H& h, T&... t) {
read(h);
read(t...);
}
template<class A> void read(vt<A>& x) {
for(auto& a:x)
read(a);
}
template<class A, size_t S> void read(array<A, S>& x) {
for(auto& a:x)
read(a);
}
void _run(){
int n;
ll c;
read(n,c);
map<ll,ll>mp;
lp(i,n){
ll a,b,C;
read(a,b,C);
mp[a]+=C;
mp[b+1]-=C;
}
int last =0;
ll sum =0;
pair<ll,ll>pre;
for(auto &x:mp){
if(!last){
pre=x;
last = 1;
continue;
}
x.sc+=pre.sc;
pre=x;
}
last =0;
for(auto x:mp){
if(!last){
pre=x;
last=1;
continue;
}
sum+=(x.fr-pre.fr)*min(c,pre.sc);
pre=x;
}
cout<<sum<<endl;
}
int main() {
# ifdef LOCAL_PROJECT
freopen("in.txt", "r", stdin);
# endif
init();
int t=1;
// read(t);
while(t--)_run();
return 0;
} | #include <algorithm>
#include <array>
#include <cmath>
#include <iostream>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
using ll = long long;
struct event {
ll t, c;
};
int main()
{
ll n, c;
std::cin >> n >> c;
std::vector<event> events;
for (int i = 0; i < n; i++) {
event a, b;
std::cin >> a.t >> b.t >> a.c;
b.t++;
b.c = -a.c;
events.push_back(a);
events.push_back(b);
}
std::sort(events.begin(), events.end(), [](const event& a, const event& b) -> bool {
return a.t < b.t;
});
ll ans = 0;
ll prev = 0;
ll cur = 0;
for (auto&& e : events) {
if (e.t != prev) {
ans += std::min(c, cur) * (e.t - prev);
prev = e.t;
}
cur += e.c;
}
std::cout << ans << "\n";
return 0;
}
|
//12252024832524
#include <cstdio>
#include <cstring>
#include <algorithm>
#define TT template<typename T>
using namespace std;
typedef long long LL;
const int MAXN = 505;
int n;
int a[MAXN],ID[MAXN],ans[MAXN * MAXN],tot;
LL Read()
{
LL x = 0,f = 1;char c = getchar();
while(c > '9' || c < '0'){if(c == '-')f = -1;c = getchar();}
while(c >= '0' && c <= '9'){x = (x*10) + (c^48);c = getchar();}
return x * f;
}
TT void Put1(T x)
{
if(x > 9) Put1(x/10);
putchar(x%10^48);
}
TT void Put(T x,char c = -1)
{
if(x < 0) putchar('-'),x = -x;
Put1(x); if(c >= 0) putchar(c);
}
TT T Max(T x,T y){return x > y ? x : y;}
TT T Min(T x,T y){return x < y ? x : y;}
TT T Abs(T x){return x < 0 ? -x : x;}
void Swap(int x)
{
int y = x+1;
int v1 = a[x],v2 = a[y];
swap(a[x],a[y]);
swap(ID[v1],ID[v2]);
ans[++tot] = x;
}
int main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
for(int T = Read(); T ;-- T)
{
tot = 0;
n = Read();
for(int i = 1;i <= n;++ i) a[i] = Read(),ID[a[i]] = i;
int now = 1;
for(int i = n;i >= 2;-- i)
{
if(i == 2 && ID[i] != i)
{
if(!now)
{
if(!((n-1)&1)) ans[++tot] = n-1,ans[++tot] = 1,ans[++tot] = n-1;
else ans[++tot] = n-2,ans[++tot] = 1,ans[++tot] = n-2;
}
else ans[++tot] = 1;
}
else if(i == 3)
{
while(a[1] != 1 || a[2] != 2 || a[3] != 3)
{
if(now) Swap(1);
else Swap(2);
now ^= 1;
}
}
else
{
while(ID[i] != i)
{
if((ID[i] & 1) != now)
{
if(ID[i] >= 4)
{
if(!now) Swap(2);
else Swap(1);
}
else
{
if(!now) Swap(2);
else Swap(3);
}
}
else Swap(ID[i]);
now ^= 1;
}
}
}
Put(tot,'\n');
for(int i = 1;i <= tot;++ i) Put(ans[i],' ');
if(tot) putchar('\n');
}
return 0;
} | #include <bits/stdc++.h>
//#pragma GCC optimize("Ofast")
/*#pragma comment(linker, "/stack:200000000")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")*/
/*#pragma GCC optimize("O2")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,fma")*/
using namespace std;
using ll = long long int;
using ld = long double;
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define lc v<<1
#define rc v<<1|1
#define fast_io ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
const int N=5e5+10,LN=18,M=1.1e7+10,SQ=350,BS=737,inf=1e9+10;
const ll INF=1e15;
const int MH=1000696969,MOD=1000000007 /*998244353*/;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using pll=pair<ll,ll>;
using pii=pair<int,int>;
#define ordered_set tree<pll, null_type,less<pll>, rb_tree_tag,tree_order_statistics_node_update>
ll pow(ll x, ll y, ll mod){
ll ans=1;
while (y != 0) {
if (y & 1) ans = ans * x % mod;
y >>= 1;
x = x * x % mod;
}
return ans;
}
ll t,n,p[N];
int main(){
fast_io;
cin >> t;
while(t--){
cin >> n;
set<ll> inv[2];
vector<ll> ans;
for(ll i=1; i<=n; i++) cin >> p[i];
if(n==2){
if(p[1]==1) cout << 0 << '\n';
else cout << 1 << '\n' << 1 << '\n';
continue;
}
for(ll i=1; i<n; i++){
if(p[i]>p[i+1]) inv[i%2].insert(i);
}
for(ll i=1; !inv[0].empty() || !inv[1].empty(); i++){
if(i>n*n) return 0;
ll k=i%2;
if(inv[k].empty()){
if(k==1){
ans.pb(1);
swap(p[1],p[2]);
inv[1].insert(1);
if(p[2]<p[3]) inv[0].erase(2);
}else{
ans.pb(2);
swap(p[3],p[2]);
inv[0].insert(2);
if(p[1]<p[2]) inv[1].erase(1);
if(n>=4 && p[3]<p[4]) inv[1].erase(3);
}
}else{
ll j=*inv[k].rbegin();
ans.pb(j);
swap(p[j],p[j+1]);
inv[k].erase(j);
if(j>1 && p[j-1]>p[j]) inv[1-k].insert(j-1);
if(j+2<=n && p[j+1]>p[j+2]) inv[1-k].insert(j+1);
}
}
cout << ans.size() << '\n';
for(ll i : ans) cout << i << ' ';
cout << '\n';
}
return 0;
}
|
#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int n;
vector<pii> v;
bool check(int idx, vector<int>& u)
{
if (u.size() == 3)
{
int dx1 = v[u[0]].first-v[u[1]].first, dy1 = v[u[0]].second-v[u[1]].second;
int dx2 = v[u[1]].first-v[u[2]].first, dy2 = v[u[1]].second-v[u[2]].second;
if (!dx1 && !dx2) return true;
if (!dy1 && !dy2) return true;
if (dy1*dx2 == dy2*dx1) return true;
return false;
}
/////////////////////////////////////////////////////
bool ret = false;
for (int i = idx+1; i < n; i++)
{
u.push_back(i);
ret = (ret || check(i, u));
u.pop_back();
if (ret) break;
}
return ret;
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
v.resize(n); for (pii& p : v) cin >> p.first >> p.second;
vector<int> temp;
cout << (check(-1, temp) ? "Yes" : "No") << endl;
return 0;
}
| #include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <queue>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define print(x) cout << x << "\n"
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))
typedef long long ll;
using namespace std;
using vl = vector<ll>;
using Graph = vector<vector<int> >;
int main() {
int n; cin >> n;
vector<int> x(n);
vector<int> y(n);
rep(i,n) cin >> x[i] >> y[i];
bool ans =false;
rep(i,n-2){
for(int j=i+1;j<n-1;j++){
for(int k=j+1;k<n;k++){
int x1=x[i]; int y1=y[i];
int x2=x[j]; int y2=y[j];
int x3=x[k]; int y3=y[k];
int k1,k2;
k1 = (y2-y1)*(x3-x1);
k2 = (y3-y1)*(x2-x1);
if(k1==k2) ans = true;
}
}
}
if(ans) cout << "Yes" << endl;
else cout << "No" << endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for(int i = (int)(a); i <= (int)(b); ++i)
#define FORR(i, a, b) for(int i = (int)(a); i >= (int)(b); --i)
#define ALL(c) (c).begin(), (c).end()
using ll = long long;
using VI = vector<int>;
using VL = vector<ll>;
using VD = vector<double>;
using VII = vector<VI>;
using VLL = vector<VL>;
using VDD = vector<VD>;
using P = pair<int, int>;
using PL = pair<ll, ll>;
template <typename T>
bool chmax(T& a, const T& b) {
if(a < b) {
a = b;
return true;
}
return false;
}
template <typename T>
bool chmin(T& a, const T& b) {
if(a > b) {
a = b;
return true;
}
return false;
}
int main() {
int N;
cin >> N;
VI t(N);
VI l(N);
VI r(N);
REP(i, N) {
cin >> t[i] >> l[i] >> r[i];
l[i] *= 2;
r[i] *= 2;
if(t[i] == 2) {
r[i]--;
}
if(t[i] == 3) {
l[i]++;
}
if(t[i] == 4) {
l[i]++;
r[i]--;
}
}
vector<PL> p(N);
REP(i, N) {
p[i].first = l[i];
p[i].second = r[i];
}
sort(p.begin(), p.end());
ll ans = 0;
FOR(i, 0, N - 2) FOR(j, i + 1, N - 1) {
if(p[i].second >= p[j].first) ans++;
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int t;
vector<double> l(n);
vector<double> r(n);
for(int i = 0;i < n;i++){
cin >> t >> l[i] >> r[i];
if(t >= 3) l[i] += 0.1f;
if(t % 2 == 0) r[i] -= 0.1f;
}
int ans = 0;
for(int i = 0;i < n - 1;i++){
for(int j = i + 1;j < n;j++){
if(l[i] <= r[j] && r[i] >= l[j]) ans++;
}
}
cout << ans << endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 3e5 + 5;
ll tr[N << 1];
struct Node {
int num, idx;
bool operator<(const Node& a) {
return num < a.num;
}
};
Node a[N];
int n, b[N];
inline int lowbit(int x) { return x & -x; }
void add(int idx, int val) {
while (idx <= 2 * n) tr[idx] += val, idx += lowbit(idx);
return;
}
int getsum(int idx) {
int cnt = 0;
while (idx) cnt += tr[idx], idx -= lowbit(idx);
return cnt;
}
ll res;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i].num;
a[i].idx = i;
}
sort(a + 1, a + 1 + n);
for (int i = 1; i <= n; ++i) {
add(a[i].idx, 1);
b[a[i].num] = i - getsum(a[i].idx);
res += b[a[i].num];
}
sort(a + 1, a + 1 + n, [](Node& c, Node& d){
return c.idx < d.idx;
});
for (int i = 1; i <= n; ++i) {
cout << res << endl;
res =res - a[i].num ;
res = res + n - a[i].num - 1ll;
}
return 0;
} | #pragma comment(linker, "/STACK:512000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#include <bits/stdc++.h>
#ifdef AIM
#include <sys/resource.h>
#endif
using namespace std;
#define all(a) a.begin(), a.end()
typedef long long li;
typedef long double ld;
void solve(__attribute__((unused)) bool);
void precalc();
clock_t start;
#define FILENAME ""
int main() {
#ifdef AIM
string s = FILENAME;
// assert(!s.empty());
freopen("/home/alexandero/ClionProjects/cryptozoology/input.txt", "r", stdin);
//freopen("/home/alexandero/ClionProjects/cryptozoology/output.txt", "w", stdout);
#else
// freopen(FILENAME ".in", "r", stdin);
// freopen(FILENAME ".out", "w", stdout);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif
start = clock();
int t = 1;
#ifndef AIM
cout.sync_with_stdio(0);
cin.tie(0);
#endif
#ifdef AIM
const rlim_t kStackSize = 256 * 1024 * 1024;
struct rlimit rl;
int result;
result = getrlimit(RLIMIT_STACK, &rl);
if (result == 0)
{
if (rl.rlim_cur < kStackSize)
{
rl.rlim_cur = kStackSize;
result = setrlimit(RLIMIT_STACK, &rl);
if (result != 0)
{
fprintf(stderr, "setrlimit returned result = %d\n", result);
}
}
}
#endif
precalc();
cout.precision(10);
cout << fixed;
//cin >> t;
int testNum = 1;
while (t--) {
//cout << "Case #" << testNum++ << ": ";
solve(true);
}
cout.flush();
#ifdef AIM1
while (true) {
solve(false);
}
#endif
#ifdef AIM
cout.flush();
auto end = clock();
usleep(10000);
print_stats(end - start);
usleep(10000);
#endif
return 0;
}
template<typename T>
T binpow(T q, T w, T mod) {
if (!w)
return 1 % mod;
if (w & 1)
return q * 1LL * binpow(q, w - 1, mod) % mod;
return binpow(q * 1LL * q % mod, w / 2, mod);
}
template<typename T>
T gcd(T q, T w) {
while (w) {
q %= w;
swap(q, w);
}
return q;
}
template<typename T>
T lcm(T q, T w) {
return q / gcd(q, w) * w;
}
template <typename T>
void make_unique(vector<T>& a) {
sort(all(a));
a.erase(unique(all(a)), a.end());
}
template<typename T>
void relax_min(T& cur, T val) {
cur = min(cur, val);
}
template<typename T>
void relax_max(T& cur, T val) {
cur = max(cur, val);
}
void precalc() {
}
#define int li
//const int mod = 1000000007;
void solve(__attribute__((unused)) bool read) {
int n;
cin >> n;
vector<int> a(n), b(n);
int ans = 0;
for (int i = 0; i < n; ++i) {
cin >> a[i];
ans += a[i];
}
for (int i = 0; i < n; ++i) {
cin >> b[i];
}
vector<vector<pair<int, int>>> res(2);
for (int i = 0; i < n; ++i) {
res[i & 1].push_back({a[i], b[i]});
}
for (auto& vec : res) {
sort(all(vec), [&](const pair<int, int>& q, const pair<int, int>& w) {
return q.first - q.second < w.first - w.second;
});
}
int cur_ans = ans;
for (int i = 0; i < res[0].size(); ++i) {
for (int j = 0; j < 2; ++j) {
cur_ans -= res[j][i].first;
cur_ans += res[j][i].second;
}
relax_max(ans, cur_ans);
}
cout << ans << "\n";
} |
/*
17/10/2017
*/
#include <bits/stdc++.h>
#define ll long long
#define mod 1e9 + 7
using namespace std;
int main()
{
int a,b;
cin>>a>>b;
cout<<a/b<<endl;
} | #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <ctype.h>
#include <iomanip>
#include <math.h>
#include <cmath>
using namespace std;
typedef long long int ll;
typedef unsigned long long lls;
int main()
{
//129a
/*int n;
cin >> n;
int contP(0);
int contImp(0);
int num;
for(int i = 0; i < n; i++)
{
cin >> num;
if(num % 2 == 0)
{
contP++;
}
else
{
contImp++;
}
}
//RES
int res(0);
if(/*contImp > 2 &&// contImp % 2)
{
res += contImp;
}
else
{
res += contP;
}
cout << res;*/
//ATCODER 182
int a, b;
cin >> b >> a;
cout << ((2*b) + 100) - a;
return 0;
} |
#include<bits/stdc++.h>
#define SZ(X) ((int)(X).size())
#define MP make_pair
#define PB emplace_back
using namespace std;
const int SIZE = 1 << 20;
int a[SIZE];
int b[SIZE];
int A[40];
int T;
void dfs(int x,int bound, int v, int d[], int &num) {
if(v > T) { return; }
if(x == bound) {
d[num++] = v;
return;
}
dfs(x + 1, bound, v, d, num);
dfs(x + 1, bound, v + A[x], d, num);
}
void solve() {
int N;
scanf("%d%d", &N, &T);
for(int i = 0; i < N; i++) {
scanf("%d", &A[i]);
}
int a_num = 0;
dfs(0, N/2, 0, a, a_num);
int b_num = 0;
dfs(N/2, N, 0, b, b_num);
sort(a, a + a_num);
sort(b, b + b_num);
int ptr = b_num - 1;
int ma = 0;
for(int i = 0; i < a_num; i++) {
while(ptr >= 0 && a[i] + b[ptr] > T) {
ptr--;
}
if(ptr < 0) { break; }
ma = max(ma, a[i] + b[ptr]);
}
printf("%d\n", ma);
}
int main() {
solve();
return 0;
}
| // -----------------------------------
// author : MatsuTaku
// country : Japan
// created : 10/24/20 20:53:04
// -----------------------------------
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
cin.tie(nullptr); ios::sync_with_stdio(false);
int n,m; cin>>n>>m;
if (m < 0 or (m != 0 and m >= n-1)) {
cout << -1 << endl;
} else {
if (m == 0) {
vector<pair<int,int>> LR;
int k = 1;
for (int i = 0; i < n; i++) {
LR.emplace_back(k, k+1);
k += 2;
}
for (auto [l,r] : LR)
cout << l << " " << r << endl;
} else {
vector<pair<int,int>> LR;
int k = 1;
for (int i = 0; i < n-m-2; i++) {
LR.emplace_back(k, k+1);
k += 2;
}
LR.emplace_back(k, k+1+2*(m+1));
k++;
for (int i = 0; i < m+1; i++, k+=2)
LR.emplace_back(k, k+1);
for (auto [l,r] : LR)
cout << l << " " << r << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int mod = 1e9 + 7;
int add(int a, int b) {
return (a + b) % mod;
}
int mul(int a, int b) {
ll ans = a * 1ll * b;
return ans % mod;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int h, w;
cin >> h >> w;
vector<vector<bool>> grid(h, vector<bool>(w, false));
int cnt = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
char c;
cin >> c;
if (c == '.') {
grid[i][j] = true;
cnt++;
}
}
}
int res = 0;
vector<int> pow(cnt + 1);
pow[0] = 1;
for (int i = 1; i <= cnt; i++) {
pow[i] = add(pow[i - 1], pow[i - 1]);
}
vector<vector<pair<int, int>>> gridn(h, vector<pair<int, int>>(w, make_pair(0, 0)));
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (grid[i][j]) {
if (gridn[i][j].first == 0) {
int temp = 1;
for (int k = j + 1; k < w; k++) {
if (!grid[i][k]) {
break;
}
temp++;
}
//cout << "asd" << temp << endl;
for (int k = j; k < w; k++) {
if (!grid[i][k]) {
break;
}
gridn[i][k].first = temp;
}
}
if (gridn[i][j].second == 0) {
int temp = 1;
for (int k = i + 1; k < h; k++) {
if (!grid[k][j]) {
break;
}
temp++;
}
//cout << "asdf" << temp << endl;
for (int k = i; k < h; k++) {
if (!grid[k][j]) {
break;
}
gridn[k][j].second = temp;
}
}
int temp = gridn[i][j].first + gridn[i][j].second - 1;
//cout << temp << i << j << endl;
res = add(res, mul(pow[cnt - temp], pow[temp] - 1));
}
}
}
cout << res << endl;
return 0;
} | #include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <numeric>
#include <iomanip>
#include <algorithm>
using namespace std;
string rus;
int point = 0;
void move(vector<vector<int>> &mdata, vector<vector<int>> &pdata, vector<int> cdata,int x,int y,int p,int dir, string sdata,int c){
if (point < p) {
rus = sdata;
point = p;
}
if (x > 50 || y > 50 || x <= 0 || y <= 0) {
return;
}
if (cdata.at(mdata.at(y).at(x)) == 0) {
return;
}
cdata.at(mdata.at(y).at(x)) = 0;
p = p + pdata.at(y).at(x);
if (dir == 1)
sdata += 'D';
else if (dir == 2)
sdata += 'U';
else if (dir == 3)
sdata += 'R';
else if (dir == 4)
sdata += 'L';
map<int, int> hp;
int cnt = 0;
if(cdata.at(mdata.at(y + 1).at(x)) != 0)
hp[pdata.at(y + 1).at(x)] = 1;
if (cdata.at(mdata.at(y - 1).at(x)) != 0)
hp[pdata.at(y - 1).at(x)] = 2;
if (cdata.at(mdata.at(y).at(x + 1)) != 0)
hp[pdata.at(y).at(x + 1)] = 3;
if (cdata.at(mdata.at(y).at(x - 1)) != 0)
hp[pdata.at(y).at(x - 1)] = 4;
for (auto i = hp.rbegin();i != hp.rend();i++) {
if (i->second == 1) {
move(mdata, pdata, cdata, x, y + 1, p, 1, sdata,c+1);
}
else if (i->second == 2) {
move(mdata, pdata, cdata, x, y - 1, p, 2, sdata,c+1);
}
else if (i->second == 3) {
move(mdata, pdata, cdata, x + 1, y, p, 3, sdata,c+1);
}
else {
move(mdata, pdata, cdata, x - 1, y, p, 4, sdata,c+1);
}
//cout << i->second << endl;
cnt++;
if ((cnt == 1 && c > 15) ||(cnt == 2 && c > 10)) break;
}
}
int main(void) {
int sx, sy;
string sdata;
cin >> sy >> sx;
vector<int> cdata(52 * 52, 0);
vector<vector<int>> mdata(52, vector<int>(52));
vector<vector<int>> pdata(52, vector<int>(52));
for (int i = 1;i <= 50;i++) {
for (int j = 1;j <= 50;j++) {
cin >> mdata.at(i).at(j);
cdata.at(mdata.at(i).at(j)) = 1;
}
}
for (int i = 1;i <= 50;i++) {
for (int j = 1;j <= 50;j++) {
cin >> pdata.at(i).at(j);
}
}
move(mdata,pdata, cdata, sx+1, sy+1,pdata.at(sy+1).at(sx+1),0,sdata,0);
cout << rus << endl;
} |
#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;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, int> plli;
typedef pair<int, ll> pill;
#define pb push_back
#define ms(a, b); memset(a, b, sizeof(a));
#define all(x) x.begin(), x.end()
#define endl "\n"
#define ins insert
#define bck back
#define lb lower_bound
#define ub upper_bound
#define popcount(x) __builtin_popcount(x)
#define popcountll(x) __builtin_popcountll(x)
#define clz(x) __builtin_clz(x)
const int inf = 0x3f3f3f3f, mod = 1e9+7, mx = 2e5+10, LOGN = 21;
const ll infll = 0x3f3f3f3f3f3f3f3f;
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
int n; cin >> n;
vector<pair<int, string>> a(n);
for(int i=0; i < n; i++)cin >> a[i].second >> a[i].first;
sort(all(a));
cout << a[n-2].second << "\n";
return 0;
}
| #include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
int main(){
int n;
cin>>n;
map<int,string> mount{};
vector<int> vec;
rep(i,n) {
string s;
int x;
cin>>s>>x;
mount[x]=s;
vec.push_back(x);
}
sort(vec.begin(),vec.end(),greater<int>());
cout<<mount[vec[1]]<<endl;
} |
#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;
ll Fib[89];
int main(){
Fib[0]=Fib[1]=1;
for(int i=2;i<89;i++)Fib[i]=Fib[i-1]+Fib[i-2];
ll n;cin>>n;
vector<int> A;
for(int i=88;i>=0;i--){
if(n>=Fib[i]){
A.push_back(i);
n-=Fib[i];
}
}
reverse(ALL(A));
vector<int> ans;
int now=0;
for(int i=0;i<88&&now<A.size();i++){
if(A[now]==i){
ans.push_back(1+(i&1));
now++;
}
ans.push_back(3+(i&1));
}
reverse(ALL(ans));
cout<<ans.size()<<endl;
for(int p:ans)cout<<p<<endl;
}
| #include<bits/stdc++.h>
using namespace std;
const double SQUP=0.618033992;
const double SQLO=0.618033982;
bool Deal(long long x,long long y,int dep)
{
if(dep>129)return false;
if(x<5 && y<5)
{
int tot=dep+x+y+1;
if(tot>129)return false;
printf("%d\n",tot);
for(int i=1;i<=x;i++)puts("1");
for(int i=1;i<=y;i++)puts("2");
return true;
}
// printf("%lld %lld %d %.10lf\n",x,y,dep,1.0*x/y);
if(rand()%25==0)
{
bool ret=Deal(x,y-1,dep+1);
if(ret)puts("2");
return ret;
}
if(rand()%25==0)
{
bool ret=Deal(x-1,y,dep+1);
if(ret)puts("1");
return ret;
}
if(x>y)
{
// if(y>x*SQUP && rand()%6)Deal(x,y-1,dep+1);
// else if(x<200 && y<200 && x%y<y/16)Deal(x,y-1,dep+1);
// else
bool ret=Deal(x-y,y,dep+1);
if(ret)puts("3");
return ret;
}
else
{
// if(x>y*SQUP && rand()%6)Deal(x-1,y,dep+1);
// else if(x<200 && y<200 && y%x<x/16)Deal(x-1,y,dep+1);
// else
bool ret=Deal(x,y-x,dep+1);
if(ret)puts("4");
return ret;
}
}
int main()
{
long long tar;scanf("%lld",&tar);
long long x=tar*(sqrt(5)-1)/2,y=tar-x;
while(!Deal(x,y,0));
puts("3");
return 0;
} |
#define _USE_MATH_DEFIMES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
const int MOD = 1'000'000'007;
const int MOD2 = 998'244'353;
const int INF = 1'000'000'000; //1e9
const int NIL = -1;
const long long LINF = 1'000'000'000'000'000'000; // 1e18
const long double EPS = 1E-10;
template<class T, class S> inline bool chmax(T &a, const S &b){
if(a < b){
a = b; return true;
}
return false;
}
template<class T, class S> inline bool chmin(T &a, const S &b){
if(b < a){
a = b; return true;
}
return false;
}
template<class T, class S> inline bool exist(std::set<T> &s, const S &e){
return (s.find(e) != s.end());
}
struct edge{
int to;
long long C, D;
edge(int To, long long c, long long d): to(To), C(c), D(d){}
};
void dijkstra(int s, std::vector<std::vector<edge>> &G, std::vector<long long> &d, std::vector<int> &prv){
//pair<int, int> first: 距離 second: 頂点
std::priority_queue<std::pair<long long, int>,
std::vector<std::pair<long long, int>>,
std::greater<std::pair<long long, int>>> que;
int V{int(G.size())};
d.resize(V, LINF+3);
prv.resize(V, NIL);
d[s] = 0;
que.emplace(0, s);
while(!que.empty()){
std::pair<long long, int> p{que.top()}; que.pop();
int v{p.second};
if(d[v] < p.first) continue;
for(edge &e: G[v]){
long long TT{(long long)(std::sqrt((long double)(e.D)) - 1)};
long long mn{LINF+8};
for(int i{-300}; i <= 300; ++i){
long long t{TT + i};
if(t < d[v]) t = d[v];
chmin(mn, t + e.C + e.D/(t + 1));
}
if(d[e.to] > mn){
d[e.to] = mn;
prv[e.to] = v;
que.emplace(d[e.to], e.to);
}
}
}
}
int main(){
int N, M; std::cin >> N >> M;
std::vector<std::vector<edge>> G(N);
{
int A, B; long long C, D;
for(int i{0}; i < M; ++i){
std::cin >> A >> B >> C >> D;
G[A-1].emplace_back(B-1, C, D);
G[B-1].emplace_back(A-1, C, D);
}
}
std::vector<long long> d;
std::vector<int> p;
dijkstra(0, G, d, p);
std::cout << (d[N-1] <= LINF ? d[N-1]: -1LL) << std::endl;
return 0;
}
| #include <bits/stdc++.h>
typedef long long int LL;
typedef long double LD;
#define nl '\n'
#define ff first
#define ss second
#define inf INT_MAX
#define SQR(a) (a) * (a)
#define pb push_back
#define GCD(a, b) __gcd(a, b)
#define PI 2.0*acos(0.0)
#define pii pair<int,int>
#define pll pair<long long,long long>
#define LCM(a, b) (a * b) / GCD(a, b)
#define mem(a,b) memset(a,b,sizeof(a))
#define srtv(v) sort(v.begin(),v.end())
#define Rep(i,a,b) for(int i = a; i <= b; i++)
#define rep(i,a,b) for(int i = a; i >= b; i--)
#define FOR(i,a) for(int i=0;i<a;i++)
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
#define boost ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define inout freopen("input.txt","r",stdin);freopen("output.txt","w",stdout)
#define si(x) scanf("%d",&x)
#define pi(x) printf("%d",x)
#define sss(str) scanf("%s",str)
#define pl(x) printf("%lld",x)
#define sl(x) scanf("%lld",&x)
#define sii(x,y) scanf("%d %d",&x,&y)
#define sll(x,y) scanf("%lld %lld",&x,&y)
#define siii(x,y,z) scanf("%d %d %d",&x,&y,&z)
#define slll(x,y,z) scanf("%lld %lld %lld",&x,&y,&z)
using namespace std;
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};
//--------------------------code starts here---------------------------------------
const int maxn=2e5+5;
vector<LL> adj[maxn];
set<LL> st;
vector<pll> v;
int n;
LL dp[maxn][2];
LL calc(LL x,LL y)
{
/*if(x<0 && y<0)
{
return abs(fabs(x)-fabs(y));
}
if(x<0 && y>=0)
{
return fabs(x)+y;
}
if(x>0 && y>0)
return abs(y-x);*/
return abs(x-y);
}
LL lowest=100000000000000;
LL fun(int pos,int flag)
{
if(pos==n)
{
if(flag==0)
{
return abs(lowest-v[pos-1].ff);
}
else
{
return abs(lowest-v[pos-1].ss);
}
}
if(dp[pos][flag]!=-1)
return dp[pos][flag];
LL res=10000000000000000;
if(flag==0)
{
LL dist=calc(v[pos-1].ff,v[pos].ff);
dist+=calc(v[pos].ff,v[pos].ss);
res=min(res,dist+fun(pos+1,1));
dist=calc(v[pos-1].ff,v[pos].ss);
dist+=calc(v[pos].ff,v[pos].ss);
res=min(res,dist+fun(pos+1,0));
}
else
{
LL dist=calc(v[pos-1].ss,v[pos].ff);
dist+=calc(v[pos].ff,v[pos].ss);
res=min(res,dist+fun(pos+1,1));
dist=calc(v[pos-1].ss,v[pos].ss);
dist+=calc(v[pos].ff,v[pos].ss);
res=min(res,dist+fun(pos+1,0));
}
return dp[pos][flag]=res;
}
void solve(int test)
{
si(n);
FOR(i,n)
{
LL x,y;
sll(x,y);
adj[y].pb(x);
st.insert(y);
}
v.pb({0,0});
for(auto i:st)
{
sort(adj[i].begin(),adj[i].end());
LL frst=adj[i][0];
LL lst=adj[i][adj[i].size()-1];
v.pb({frst,lst});
lowest=min(lowest,frst);
lowest=min(lowest,lst);
}
for(int i=0;i<v.size();i++)
{
v[i].ff+=lowest;
v[i].ss+=lowest;
}
n=v.size();
mem(dp,-1);
LL res=fun(1,0);
printf("%lld\n",res);
}
int main()
{
int test=1;
//si(test);
Rep(i,1,test)
{
solve(test);
}
return 0;
}
|
#include<bits/stdc++.h>
#include <iomanip>
using namespace std;
typedef long long int ll;
#include <stdio.h>
int main(void) {
ll n,i,c=0,d=0,ma,min;
cin>>n;
ll a[n];
for(i=0;i<n;i++){
cin>>a[i];
}
for(i=0;i<n;i++){
c+=abs(a[i]);
d+=a[i]*a[i];
}
ma=*max_element(a,a+n);
min=*min_element(a,a+n);
cout<<c<<"\n"<< fixed << setprecision(15) << sqrt(d)<<"\n"<<max(ma,-1*min);
return 0;
}
|
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization ("unroll-loops")
#include<bits/stdc++.h>
#include<string.h>
using namespace std;
#define pb push_back
#define all(v) v.begin(),v.end()
#define ya cout<<"YES"<<endl;
#define no cout<<"NO"<<endl;
#define ff first
#define sc second
#define inf 999999999
#define pi 3.14159265359
#define printv(v) for(auto x:v)cout<<x<<' ';cout<<endl;
#define takev(v) for(auto &x:v)cin>>x;
inline int random(int a=1,int b=10)
{
return a+rand()%(b-a+1);
}
typedef long long ll;
inline ll lcm(ll a,ll b)
{
return (a*b)/__gcd(a,b);
}
//#define see(x) cout<<endl<<#x<<" : "<<(x)<<endl;
#define see(args...) \
{ \
string _s = #args; replace(_s.begin(), _s.end(), ',', ' ');\
stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args);\
}
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args)
{
cerr<< *it << " = " << a <<",\n"[++it==istream_iterator<string>()];
err(it, args...);
}
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
const int N=2e5+9,mod=998244353;
int main()
{
// ios::sync_with_stdio(0);
// cin.tie(NULL);
// cout.tie(NULL);
ll n,i,j,k,ans=0,tot=0,ta=0,ao=0,win=0;
string s,t;
cin>>k;
cin>>s>>t;
vector<ll>ft(11),fa(11),have(11,k);
for(int i=0;i<4;i++)ft[s[i]-'0']++,have[s[i]-'0']--;
for(int i=0;i<4;i++)fa[t[i]-'0']++,have[t[i]-'0']--;
for(i=1;i<=9;i++)
{
ta += i*pow(10,ft[i]);
ao += i*pow(10,fa[i]);
}
tot = k*9 - 8;
tot *= tot-1;
for(i=1;i<=9;i++)
{
for(j=1;j<=9;j++)
{
ft[i]++;
fa[j]++;
ll taka = ta - i*pow(10,ft[i]-1);
taka += i*pow(10,ft[i]);
ll aoki = ao - j*pow(10,fa[j]-1);
aoki += j*pow(10,fa[j]);
ft[i]--;
fa[j]--;
if(taka > aoki)
{
if(i==j)
win += have[i]*(have[j]-1);
else
win += (have[i]*have[j]);
}
}
}
printf("%.10lf\n",win/(tot*1.0));
return 0;
}
|
//Author: AnandRaj doubleux
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef pair<ll,ll> pll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<pll> vpll;
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define test() int t;cin>>t;while(t--)
#define all(v) v.begin(),v.end()
#define prinp(p) cout<<p.first<<" "<<p.second<<endl
#define prinv(V) for(auto v:V) cout<<v<<" ";cout<<endl
#define take(V,f,n) for(int in=f;in<f+n;in++) cin>>V[in]
#define what(x) cerr<<#x<<" = "<<x<<endl
#define KStest() int t,t1;cin>>t;t1=t;while(t--)
#define KScout cout<<"Case #"<<t1-t<<": "
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9+7,MAX = 1e6+5;
const ll INF = 1e18+5;
int main()
{
int n,q;
cin>>n>>q;
vl A(n);
take(A,0,n);
sort(all(A));
while(q--)
{
ll k;
cin>>k;
ll low = 0, high = 2e18;
ll ans=-1;
while(low<=high)
{
ll mid = low + (high-low)/2;
int smol = 0;
int l = 0, h = n-1;
while(l<=h)
{
int m = l + (h-l)/2;
if(A[m]<=mid)
{
smol = m+1;
l = m+1;
}
else h = m-1;
}
if(mid - smol >=k)
{
ans = mid;
high = mid-1;
}
else low = mid+1;
}
cout<<ans<<endl;
}
} | #include<bits/stdc++.h>
#define int long long
using namespace std;
int a[100010],n,q;
signed main(){
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>a[i];
}
sort(a+1,a+1+n);
for(int i=1;i<=n;i++){
a[i]-=i;
}
while(q--){
int k;
cin>>k;
int rk=lower_bound(a+1,a+n+1,k)-a-1;
if(rk==n+1){
cout<<k+n<<endl;
}else{
cout<<k+rk<<endl;
}
}
} |
// EXPLOSION!
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include<unordered_set>
#include<unordered_map>
#include<chrono>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pair<int, int>> vpi;
typedef vector<pair<ll, ll>> vpll;
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)
#define pb push_back
#define mp make_pair
#define rsz resize
#define sz(x) int(x.size())
#define all(x) x.begin(),x.end()
#define f first
#define s second
#define cont continue
#define endl '\n'
//#define ednl '\n'
#define test int testc;cin>>testc;while(testc--)
#define pr(a, b) trav(x,a)cerr << x << b; cerr << endl;
#define message cout << "Hello World" << endl;
const int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 }; // for every grid problem!!
const ll linf = 4000000000000000000LL;
const ll inf = 1000000007;//998244353
void pv(vi a) { trav(x, a)cout << x << " "; cout << endl; }void pv(vll a) { trav(x, a)cout << x << " "; cout << endl; }void pv(vector<vi>a) {
F0R(i, sz(a)) { cout << i << endl; pv(a[i]); cout << endl; }
}void pv(vector<vll>a) { F0R(i, sz(a)) { cout << i << endl; pv(a[i]); }cout << endl; }void pv(vector<string>a) { trav(x, a)cout << x << endl; cout << endl; }
void setIO(string s) {
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef arwaeystoamneg
if (sz(s))
{
freopen((s + ".in").c_str(), "r", stdin);
if (s != "test1")
freopen((s + ".out").c_str(), "w", stdout);
}
#endif
}
template<int MOD, int RT> struct mint {
static const int mod = MOD;
static constexpr mint rt() { return RT; } // primitive root for FFT
int v; explicit operator int() const { return v; } // explicit -> don't silently convert to int
mint() { v = 0; }
mint(ll _v) {
v = int((-MOD < _v&& _v < MOD) ? _v : _v % MOD);
if (v < 0) v += MOD;
}
friend bool operator==(const mint& a, const mint& b) {
return a.v == b.v;
}
friend bool operator!=(const mint& a, const mint& b) {
return !(a == b);
}
friend bool operator<(const mint& a, const mint& b) {
return a.v < b.v;
}
mint& operator+=(const mint& m) {
if ((v += m.v) >= MOD) v -= MOD;
return *this;
}
mint& operator-=(const mint& m) {
if ((v -= m.v) < 0) v += MOD;
return *this;
}
mint& operator*=(const mint& m) {
v = int((ll)v * m.v % MOD); return *this;
}
mint& operator/=(const mint& m) { return (*this) *= inv(m); }
friend mint power(mint a, ll p) {// MAKE SURE YOU ARE USING THE CORRECT VERSION OF POW!!!!!!!!
mint ans = 1; assert(p >= 0);
for (; p; p /= 2, a *= a) if (p & 1) ans *= a;
return ans;
}
friend mint inv(const mint& a) {
assert(a.v != 0);
return power(a, MOD - 2);
}
mint operator-() const { return mint(-v); }
mint& operator++() { return *this += 1; }
mint& operator--() { return *this -= 1; }
friend mint operator+(mint a, const mint& b) { return a += b; }
friend mint operator-(mint a, const mint& b) { return a -= b; }
friend mint operator*(mint a, const mint& b) { return a *= b; }
friend mint operator/(mint a, const mint& b) { return a /= b; }
};
typedef mint<inf, 5> mi; // 5 is primitive root for both common mods
const int MAX = 1111111;
int n, m, k;
mi fac[2 * MAX];
mi choose(int x, int y)
{
if (y == -1)return 0;
return fac[x] * inv(fac[y]) * inv(fac[x - y]);
}
int main()
{
setIO("test1");
cin >> n >> m >> k;
if (m + k < n)
{
cout << 0 << endl;
return 0;
}
fac[0] = 1;
F0R(i, 2 * MAX - 1)
{
fac[i + 1] = fac[i] * (i + 1);
}
cout << (int)(choose(n + m, n) - choose(n + m, n - k - 1)) << endl;
} | #include<iostream>
#include<string>
#include<iomanip>
#include<set>
#include<stack>
#include<queue>
#include<cmath>
#include<algorithm>
#include<vector>
#include<limits.h>
#include<math.h>
#include<array>
#include<map>
#include<cstring>
#include<stack>
#include<numeric>
using ll = long long;
constexpr int MOD = 1e9+7;
int power(int x,int y){
int res = 1;
while(y > 0){
if(y%2){
res = (res * 1LL * x)%MOD;
}
x = (x * 1LL * x)%MOD;
y /= 2;
}
return res;
}
int main(){
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int n,m,k;
std::cin >> n >> m >> k;
if(n > m+k){
std::cout << "0\n";
}
else{
std::vector<int> fact(n+m+1);
fact[0] = 1;
for(int i = 1; i <= n+m;++i){
fact[i] = (fact[i-1] * 1LL * i)%MOD;
}
int temp = (fact[n] * 1LL * fact[m])%MOD;
int ans = (fact[n+m] * 1LL * power(temp,MOD-2))%MOD;
if(k+1 <= n){
temp = (fact[n-k-1] * 1LL * fact[m+k+1])%MOD;
temp = (fact[n+m] * 1LL * power(temp,MOD-2))%MOD;
ans = ((ans - temp)%MOD + MOD)%MOD;
}
std::cout << ans << '\n';
}
}
|
#include <iostream>
#include <cmath>
#include <vector>
#include <map>
using namespace std;
const int maxn = 1e6 + 13;
int e[20];
bool dp[maxn];
int dp2[maxn];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; ++i) {
int u, v;
cin >> u >> v;
--u; --v;
e[u] |= 1 << v;
e[v] |= 1 << u;
}
dp[0] = 1;
for (int mask = 1; mask < (1 << n); ++mask) {
for (int i = 0; i < n; ++i) {
if (mask >> i & 1)
if(((mask & ~(1 << i)) & e[i]) == (mask & ~(1 << i)))
dp[mask] |= dp[mask & ~(1 << i)];
}
}
for (int mask = 1; mask < (1 << n); ++mask) {
if (dp[mask]) {
dp2[mask] = 1;
continue;
}
int cnt = 0;
for (int i = 0; i < n; ++i)cnt += mask >> i & 1;
dp2[mask] = cnt;
for (int s = mask; s; s = (s - 1) & mask) {
dp2[mask] = min(dp2[mask], dp2[s] + dp2[~s & mask]);
}
}
cout << dp2[(1 << n) - 1] << "\n";
return 0;
} | #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
const ll MOD=998244353;
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;}
#define MAX_N 1000010
ll inv[MAX_N+10],fac[MAX_N+10],ifac[MAX_N+10];
void setComb(){
inv[0]=1;inv[1]=1;fac[1]=1;ifac[1]=1;fac[0]=1;ifac[0]=1;
for(int i=2;i<MAX_N;i++){
inv[i]=(-MOD/i)*inv[MOD%i]%MOD;
fac[i]=fac[i-1]*i%MOD;
ifac[i]=ifac[i-1]*inv[i]%MOD;
inv[i]=(inv[i]+MOD)%MOD;
fac[i]=(fac[i]+MOD)%MOD;
ifac[i]=(ifac[i]+MOD)%MOD;
}
return;
}
ll comb(ll n,ll k){
if(n<k||n<0||k<0) return 0;
else return ((fac[n]*ifac[k]%MOD*ifac[n-k]%MOD+MOD)%MOD);
}
ll hcomb(ll n,ll r){// this size is really ok??
if(n==0&&r==0) return 1;
else if(n<0||r<0) return 0;
else return comb(n+r-1,r);
}
ll binom(ll n,ll k){
if(n<k||n<0||k<0) return 0;
ll res=1;
for(ll i=0;i<k;i++) res=res*(n-i)%MOD;
res=res*ifac[k]%MOD;
return res;
}
ll mod_pow(ll x,ll n){
x%=MOD;
ll res=1;
while(n>0){
if(n&1) res=res*x%MOD;
x=x*x%MOD;
n>>=1;
}
return res;
}
ll mod_inverse(ll x){
return mod_pow(x,MOD-2);
}
void add(ll &a,ll b){
a=(a+b)%MOD;
}
void mul(ll &a,ll b){
a%=MOD;b%=MOD;
a=a*b%MOD;
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
setComb();
ll N,K;
cin>>N>>K;
vector<ll> A(N);
rep(i,N) cin>>A[i];
vector<ll> sum(K+1,0);
vector<ll> num(N,1);
for(int k=1;k<=K;k++){
rep(i,N){
mul(num[i],A[i]);
add(sum[k],num[i]);
}
}
vector<ll> dp(K+1,0);
for(int k=1;k<=K;k++){
add(dp[k],(N)*sum[k]%MOD);
add(dp[k],(N)*sum[k]%MOD);
for(int x=1;x<k;x++){
ll res=sum[x];
mul(res,sum[k-x]);
mul(res,comb(k,x));
add(dp[k],res);
}
}
vector<ll> ans=dp;
rep(i,N) num[i]=1;
for(int k=1;k<=K;k++){
rep(i,N){
mul(num[i],(A[i]+A[i])%MOD);
add(ans[k],MOD-num[i]);
}
mul(ans[k],inv[2]);
}
rep(i,K) cout<<ans[i+1]<<"\n";
return 0;
}
|
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1500005;
int n,m;
int a[N];
int queue[N];
int book[N];
int l,r=0;
int main()
{
cin>>n>>m;
for(int i=0;i<n;i++)
cin>>a[i];
for(int i=0;i<m;i++)
queue[r++]=a[i],book[a[i]]++;
int ans=9999999;
for(int i=0;i<N;i++)
if(book[i]==0)
{
ans=i;
break;
}
while(r<n)
{
int temp=queue[l++];
book[temp]--;
queue[r]=a[r];
book[a[r]]++;
if(book[temp]==0&&ans>temp)
ans=temp;
r++;
//cout<<ans<<endl;
}
cout<<ans<<endl;
}
| /*
I love the sound you make when you shut up.
*/
#include <bits/stdc++.h>
using namespace std;
/*----typedefs--------*/
typedef long long ll;typedef long double ld;typedef complex<ld> cd;
typedef pair<int, int> pi;typedef pair<ll, ll> pl;typedef pair<ld, ld> pd;
typedef vector<int> vi;typedef vector<pi> vpi;typedef vector<ll> vl;typedef vector<ld> vd;typedef vector<pl> vpl;
typedef vector<cd> vcd;typedef vector<vi> vvi;
typedef map<int, int> mi;typedef map<ll, ll> ml;typedef map<int, vi> mivi;typedef map<int, pi> mipi;
typedef set<int> si;typedef set<ll> sl;typedef set<pair<int, int>> spi;
typedef queue<int> qi;typedef queue<pi> qpi;
/*-----in and out--------*/
#define sc(a) cin >> a
#define pf(a) cout << a << endl
#define forIn(arr, num) for(ll i = 0; i < num; i++) cin >> arr[i];
#define vpnt(ans) for(int i = 0; i < int(ans.size()); i++) cout << ans[i] << (i + 1 < int(ans.size()) ? ' ' : '\n');
/*-----traversals--------*/
#define trav(it, a) for(auto &it : a)
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
/*---useful defines------*/
#define sz(x) (int)(x).size()
#define pb push_back
#define mem(a, b) memset(a,(b), sizeof(a))
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert
/*--- bit-stuff ----*/
#define GET_SET_BITS(a) (__builtin_popcount(a))
#define GET_SET_BITSLL(a) ( __builtin_popcountll(a))
#define GET_TRAIL_ZERO(a) (__builtin_ctz(a))
#define GET_LEAD_ZERO(a) (__builtin_clz(a))
#define GET_PARITY(a) (__builtin_parity(a))
/*----- the binary answer of life-----*/
#define no cout << "NO" << endl
#define yes cout << "YES" << endl
/*---checking and pushing-----*/
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
/*---- FAST I/O and file read ---*/
void go() {
ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
/*------ single or multiple testcase-----*/
#define ssolve solve();
#define msolve int t;sc(t);while(t--) {solve();}
#define mcsolve int t;sc(t);for(int tt = 1;tt <= t;tt++) {cout << "Case #" << tt << ": ";solve();}
/*-------- movement in a 2D array ------*/
const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};
/*----------------------------------------------------------------*/
const int MOD = 1e9 + 7;
const int INF = 1e9;
const int N = 2e5 + 5;
const int MAX = 2e5 + 5;
/*-------------- Push your limits here ---------------------------*/
void solve() {
int n, k;
int a[8][8];
scanf("%d%d", &n, &k);
F0R(i, n) {
F0R(j, n) {
scanf("%d", &a[i][j]);
}
}
int cnt = 0, s = 0;
int b[8] = {0, 1, 2, 3, 4, 5, 6, 7};
while(b[0] == 0) {
s = 0;
for(int i = 0;i + 1 < n;i++) {
s += a[b[i]][b[i + 1]];
}
s += a[b[n - 1]][b[0]];
if(s == k) {
cnt++;
}
next_permutation(b, b + n);
}
printf("%d\n", cnt);
}
int main() {
ssolve
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define endl '\n'
#define pb push_back
#define pob pop_back
#define pii pair<int,int>
#define mod 1000000007
// #define mod 1000000009
// #define mod 163577857
// #define mod 998244353
#define rep(i,n) for (int i = 0; i < n; i++)
#define repp(i,a,b) for(int i = a ; i<b ; i++)
#define reppr(i,a,b) for(int i = a-1 ; i>=b ; i--)
#define repr(i,n) for (int i = n - 1; i >= 0; i--)
#define ff first
#define ss second
#define inf 9223372036854775807
#define infn -9223372036854775807
#define pi 3.14159265358979323846
#define eps 0.0000000001
#define setprec(x) cout << fixed << setprecision(x);
#define REVERSE(a) reverse(all(a));
#define SORT(a) sort(all(a));
#define all(n) n.begin(),n.end()
#define setbits(x) __builtin_popcountll(x)
//GCD and LCM
int gcd (int a, int b) { return b ? gcd (b, a % b) : a; }
int lcm (int a, int b) { return a / gcd(a, b) * b; }
//Modular Exponentiation
int powmod(int x,int y)
{
if (y == 0) return 1;
int p = powmod(x, y/2) % mod;
p = (p * p) % mod;
return (y%2 == 0)? p : (x * p) % mod;
}
//Modular Inverse
int inverse(int a)
{
return powmod(a,mod-2);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////NO TEXT ABOVE IT//////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool codejam = 0, testcases = 0;
void solve()
{
int a , b , x , y ;
cin >> a >> b >> x >> y;
int a1 = abs(2*(a-b)-1)/2*2*x + x;
int a2 = abs(2*(a-b)-1)/2*y + x;
cout << min(a1,a2) << endl;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////NO TEXT BELOW IT//////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
signed main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int t = 1;
if(testcases)
cin >> t;
rep(i,t)
{
if(codejam)
cout << "Case #" << i+ 1<< ": ";
solve();
}
} | #include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
#define rep(i,n) for(ll i=0;i<(n);++i)
#define rep2(i,n) for(ll i=1;i<=(n);++i)
#define rep3(i,i0,n) for(ll i=i0;i<(n);++i)
#define rrep(i,n) for(ll i=((n)-1); i>=0; --i)
#define rrep2(i,n) for(ll i=(n); i>0; --i)
#define pb push_back
#define mod 1000000007
#define fi first
#define se second
#define len(x) ((ll)(x).size())
using namespace std;
using ll = long long;
using ld = long double;
using Pi = pair< ll, ll >;
using vl = vector<ll>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vp = vector<Pi>;
using vvc = vector<vector<char>>;
using vvl = vector<vector<ll>>;
using vvvl = vector<vector<vector<ll>>>;
const ll INF = 1LL << 60;
const ld PI = 3.1415926535897932385;
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; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define mp make_pair
void printb(ll N,ll d=16){
rep(i,d){
cout<<(N/(1<<(d-i-1)))%2;
}
cout<<endl;
}
void printv(vector<ll>a){
rep(i,a.size()){
if(i==a.size()-1){
cout<<a[i]<<endl;
}else{
cout<<a[i]<<" ";
}
}
}
bool In_map(ll y,ll x,ll h,ll w){
if(y<0 || x<0 || y>=h || x>=w){
return 0;
}else{
return 1;
}
}
bool compare(Pi a, Pi b) {
if(a.first != b.first){
return a.first < b.first;
}else{
return a.second < b.second;
}
}
//const vector<ll> dx = {1, 0, -1, 0, 1, -1, 1, -1};
//const vector<ll> dy = {0, 1, 0, -1, 1, 1, -1, -1};
const vector<ll> dx{1,0,-1,0};
const vector<ll> dy{0,1,0,-1};
int main() {
ll A,B;
cin>>A>>B;
vl ans;
if(A > B){
ll sum=0;
rep2(i,A){
ans.pb(i);
sum+=i;
}
rep2(i,B-1){
ans.pb(-i);
sum-=i;
}
ans.pb(-sum);
}else{
ll sum=0;
rep2(i,B){
ans.pb(-i);
sum-=i;
}
rep2(i,A-1){
ans.pb(i);
sum+=i;
}
ans.pb(-sum);
}
printv(ans);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N; cin >> N;
multiset<string> st;
for (string s; cin >> s;) {
st.insert(s);
}
for (const string &s: st) {
if (st.count('!' + s)) {
cout << s << '\n';
return 0;
}
}
cout << "satisfiable\n";
} | #include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string>
#include <bits/stdc++.h>
#include <queue>
#include <math.h>
#include <bitset>
#include <map>
#include <vector>
#include <cstdio>
#include <climits>
#define white 0
#define gray 1
#define black 2
#define LIMIT (1<<30)
#define MOD 1000000007
//#define MOD 998244353
#define ll long long
using namespace std;
//const ll INF = LLONG_MAX;
//cout << fixed << setprecision(10) << ans << endl;
//pair<ll, ll> vec[4] = {make_pair(1, 0), make_pair(0, 1), make_pair(-1, 0), make_pair(0, -1)};
int main(){
int n;
cin >> n;
cout << n - 1 << endl;
}
|
// #pragma GCC optimize ("O3")
// #pragma GCC target ("sse4")
#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
#define int long long int
#define vi vector<int>
#define ff first
#define ss second
#define pb push_back
#define MOD 1000000007
//const int nax = 1e6+5;
#define inf 4e18
#define Graph vector<vi>
#define Graphw vector<vector<pair<int,int>>>
#define sz(x) (int)x.size()
#define pr pair<int,int>
#define display(v) for(int i =0;i<(int)v.size();++i)cout << v[i] << " "
#define all(v) v.begin(),v.end()
#define read(a) for(int i = 0;i<(int)a.size();++i)cin>>a[i]
#define show(x) cout << #x << " is " << x << "\n";
// typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ind_si;
// typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update> ind_msi;
#define show_arr(a) cout << #a << "\n"; display(a) ; cout << "\n";
inline int add(int a, int b){ int c = (a%MOD) + (b%MOD); if(c >= MOD) c -= MOD; return c; }
inline int mul(int a, int b){ return ((a%MOD) * 1ll * (b%MOD)) % MOD; }
inline int sub(int a, int b){ int c = (a%MOD) - (b%MOD); if(c < 0) c += MOD; return c; }
inline int sum_till(int n) { return mul(mul(n, n + 1), (MOD + 1) / 2); }
const int nax = 300*1000 + 10;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//#define ook order_of_key
//#define fbo find_by_order
// order_of_key(x) -> number of elements less than x.
// * find_by_order(x) -> gives the element at index x (0 - indexing)
void test_case();
int32_t main()
{
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
// cout.tie(NULL);
int t = 1; //cin>>t;
while(t--){
test_case();
}
return 0;
}
int sumXOR(vi arr)
{
int sum = 0;
for (int i = 0; i < 61; i++)
{
// Count of zeros and ones
int zc = 0, oc = 0;
// Individual sum at each bit position
int idsum = 0;
for (int j = 0; j < sz(arr); j++)
{
if (arr[j] % 2 == 0)
zc++;
else
oc++;
arr[j] /= 2;
}
// calculating individual bit sum
idsum = mul(oc , mul(zc , (1LL<<i)));
// final sum
sum = add(sum , idsum);
}
return sum;
}
int n;
Graphw G;
vi val;
void dfs(int st , int par = -1 , int cur = 0LL){
val[st] = cur;
//cout << st << '\n';
for(pr to : G[st]){
if(to.ff != par){
dfs(to.ff , st , cur^to.ss);
}
}
}
void test_case(){
cin>>n;
val = vi(n + 1 , 0);
G = Graphw(n + 1);
for(int i = 0;i < n - 1 ; ++i){
int u ,v , w; cin>>u>>v>>w;
G[u].pb({v , w});
G[v].pb({u , w});
}
//cout << "Hi";
dfs(1);
vi arr;
for(int i = 1; i <= n ; ++i){
arr.pb(val[i]);
}
cout << sumXOR(arr);
}
// Химаншу
// check overflow
// special case n == 1
// 🍪🍪🍪
|
// Code Created BY LEO_VALDEZ
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define sz(x) (int)(x).size()
#define sarr(x) (int)(sizeof(x)/sizeof(x[0]))
#define mp make_pair
#define fast() ios_base::sync_with_stdio(false); cin.tie(NULL);
#define pb push_back
#define p2(x) (1ll<<x)
#define pob pop_back
#define f first
#define s second
#define in pair<int, int>
#define INF 1000000000000000
#define MX (int)2e5+5
const int mod = 1e9 + 7;// make this 98953... if reqd
vector<in> Adj[MX];
int dp[MX];
void DFS(int u, int p, int val)
{
dp[u] = val;
for(in vw : Adj[u])
{
int v = vw.f;
int w = vw.s;
if(v==p)
continue;
DFS(v, u, val^w);
}
return;
}
signed main()
{
int n;
cin >> n;
int m = n-1;
while(m--)
{
int a, b, c;
cin >> a >> b >> c;
a--;
b--;
Adj[a].pb({b, c});
Adj[b].pb({a, c});
}
DFS(0, -1, 0);
vector<int> a(60, 0);
vector<int> b(60, 0);
for(int i = 0; i < 60; i++)
{
for(int j = 0; j < n; j++)
{
if((dp[j]>>i)&1)
a[i]++;
else
b[i]++;
}
}
int ans = 0;
for(int i = 0; i < 60; i++)
{
int po = (1ll<<i)%mod;
int other = (a[i]*b[i])%mod;
ans+=((po*other)%mod);
ans%=mod;
}
cout << ans << endl;
return 0;
} |
#include<bits/stdc++.h>
#include<algorithm>
#include<cmath>
#include<climits>
using namespace std;
typedef long long int lli;
typedef vector<int> vi;
typedef vector<long long int> vlli;
typedef pair<int,int> pii;
typedef pair<long long int,long long int> plli;
typedef vector< vi > vvi ;
typedef vector< vlli > vvlli ;
#define fi(i,a,b) for(int i=a;i<=b;i++)
#define flli(i,a,b) for(long long int i=a;i<=b;i++)
#define bi(i,a,b) for(int i=a;i>=b;i--)
#define blli(i,a,b) for(long long int i=a;i>=b;i--)
#define fast ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define all(x) x.begin(),x.end()
#define sz(x) x.size()
#define pi 2*acos(0.0)
#define pb push_back
#define tr(v,it) for(decltype(v.begin()) it=v.begin();it!=v.end();it++)
#define present(v,num) (v.find(num)!=v.end())
#define cpresent(v,num) (find(v.begin(),v.end(),num)!=v.end())
#define pq priority_queue
#define mp make_pair
const int inf=INT_MAX;
const lli INF =LLONG_MAX;
const lli mod = 1e9+7;
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
fast;
lli a,b;cin>>a>>b;
lli sum=0;
flli(i,1,a)
{
lli var=500500+i;
cout<<var<<" ";
sum+=var;
}
flli(i,1,b-1)
{
cout<<-i<<" ";
sum-=i;
}
cout<<-sum;
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#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(int i = m; i <= n; i++)
#define FORR(i, m, n) for(int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define VSORTR(v) sort(v.rbegin(), v.rend());
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
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 MOD = 1e9 + 7;
const ll INF = 1e18;
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
ll n, m;
cin >> n >> m;
ll a, b;
vvll g(n);
REP(i, m) {
cin >> a >> b;
a--, b--;
g[a].push_back(b);
g[b].push_back(a);
}
ll k;
cin >> k;
vll c(k);
REP(j, k) {
cin >> c[j];
c[j]--;
}
auto BFS = [&](int s){
vll cost(n, INF);
cost[s] = 0;
queue<int> q;
q.push(s);
while(q.size()){
int x = q.front();
q.pop();
for(int y : g[x]) if(chmin(cost[y], cost[x] + 1)) q.push(y);
}
for(int i = 0; i < k; i++) cost[i] = cost[c[i]];
cost.resize(k);
return cost;
};
vvll d(k);
REP(i, k) d[i] = BFS(c[i]);
vvll dp(k, vll(1 << k, INF));
REP(i, k) dp[i][0] = 0;
for(int bit = 1; bit < (1 << k); bit++){
REP(j, k) if(bit >> j & 1) {
REP(x, k){
ll cost = dp[j][bit - (1 << j)] + d[j][x];
if (bit == ((1 << k) - 1)) chmin(dp[x][bit], dp[j][bit - (1 << j)]);
chmin(dp[x][bit], cost);
}
}
}
ll ans = INF;
REP(i, k) chmin(ans, dp[i][(1 << k) - 1]);
if(ans == INF) ans = -2;
cout << ans + 1 << endl;
return 0;
} |
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <string.h>
#include <cstdio>
#include <tuple>
#include <numeric>
#include <functional>
using namespace std; using ll = long long; using ld = long double; using pll = pair<ll, ll>;
using vl = vector<ll>; using vll = vector<vl>; using vlll = vector<vll>; using vpll = vector<pll>;
using vs = vector<string>; using tll = tuple<ll, ll, ll>; using vtll = vector<tll>;
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 ld PI = 3.1415926535897932;
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define co(i) cout << i << endl;
void in() {}
void debug_out() { cerr << endl; }
template<typename Head, typename... Tail>
void debug_out(Head h, Tail... t) {
cerr << " " << h;
if (sizeof...(t) > 0) cerr << " :";
debug_out(t...);
}
template <typename T>
ostream& operator<<(ostream& os, vector<T> vec) {
for (size_t i = 0; i < vec.size(); i++)os << vec[i] << (i + 1 == vec.size() ? "" : " ");
return os;
}
ll ctoi(char c) {
switch (c) {
case '0': return 0; case '1': return 1; case '2': return 2;
case '3': return 3; case '4': return 4; case '5': return 5;
case '6': return 6; case '7': return 7; case '8': return 8;
case '9': return 9; default: return 0;
}
}
bool pairCompare(const pll firstElof, pll secondElof)
{
return firstElof.second < secondElof.second;
}
//AtCoder以外ではコメントアウトを忘れずに!
//#include <atcoder/all>
//using namespace atcoder;
//**Snippetリスト**//
//rep, vin, all, iteLoop, bitSearch, bitList, nod, LIS, digitDP, treeDP, Rerooting//
//gcdlcm, isPrime, eratos, primeFactorize, Npow, combination, divisor, modinv, doubling//
//dfs, bfs, dijkstra, WarshallFloyd, BellmanFord, UnionFind, Kruskal, RMQ, LCA, FordFulkerson//
//**AC Libraryリスト**//
//pow_mod, inv_mod, floor_sum, convolution, convolution_ll, modint//
//fenwicktree, segtree, lazysegtree, string//
ll i, j, k, l; ll N, M, K, H, W, X, Y, Z, R, Q;
ll MOD = 1000000007, INF = 1LL << 60, ans = 0, z = 0, o = 1;
vector<vtll> path;
//***********//
inline long long mod2(long long a, long long m) {
return (a % m + m) % m;
}
// 拡張 Euclid の互除法
// ap + bq = gcd(a, b) となる (p, q) を求め、d = gcd(a, b) をリターンします
long long extGcd(long long a, long long b, long long& p, long long& q) {
if (b == 0) { p = 1; q = 0; return a; }
long long d = extGcd(b, a % b, q, p);
q -= a / b * p;
return d;
}
// 中国剰余定理
// リターン値を (r, m) とすると解は x ≡ r (mod. m)
// 解なしの場合は (0, -1) をリターン
pair<long long, long long> ChineseRem(long long b1, long long m1, long long b2, long long m2) {
long long p, q;
long long d = extGcd(m1, m2, p, q); // p is inv of m1/d (mod. m2/d)
if ((b2 - b1) % d != 0) return make_pair(0, -1);
long long m = m1 * (m2 / d); // lcm of (m1, m2)
long long tmp = (b2 - b1) / d * p % (m2 / d);
long long r = mod2(b1 + m1 * tmp, m);
return make_pair(r, m);
}
int main() {
ll T;
cin >> T;
for (l = 0; l < T; l++) {
ll X, Y, P, Q;
cin >> X >> Y >> P >> Q;
ll ans = INF;
for (i = 0; i < Y; i++) {
for (j = 0; j < Q; j++) {
ll m1 = X * 2 + Y * 2;
ll b1 = X + i;
ll m2 = P + Q;
ll b2 = P + j;
pll Ans = ChineseRem(b1, m1, b2, m2);
//debug(b1,m1, b2,m2, Ans.first,Ans.second);
if (Ans.second != -1) {
chmin(ans, Ans.first);
}
}
}
//debug(1000000000999999999 - ans);
if (ans == INF) cout << "infinity" << endl;
else cout << ans << endl;
//debug(ChineseRem(3, 2, 5, 3).first);
}
}
| #include <bits/stdc++.h>
#define DEBUG if(0)
#define lli __int128
#define ldouble long double
using namespace std;
/*
(2x + 2y)n + x <= t < (2x + 2y)n + x + y
range = y (max 500)
(p + q)n + p <= t < (p + q)(n + 1)
range = q (max 500)
try all combinations of mod (500^2)
t - (2x + 2y)n - x == remY
t - (2x + 2y)n = remY + x
t = remY + x + (2x + 2y)n1
t - (p + q)n - p == remQ
t - (p + q)n = remQ + p
t = remQ + p + (p + q)n2
remQ + p + (p + q)n2 = remY + x + (2x + 2y)n1
(p + q)n2 - (2x + 2y)n1 = remY + x - remQ - p
*/
lli read() {
lli x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
void print(lli x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) print(x / 10);
putchar(x % 10 + '0');
}
lli abslol(lli num)
{
return num < 0 ? -num : num;
}
lli gcd(lli a, lli b, lli& x, lli& y)
{
if (b == 0) {
x = 1;
y = 0;
return a;
}
lli x1, y1;
lli d = gcd(b, a % b, x1, y1);
x = y1;
y = x1 - y1 * (a / b);
return d;
}
bool find_any_solution(lli a, lli b, lli c, lli &x0, lli &y0, lli &g)
{
g = gcd(abslol(a), abslol(b), x0, y0);
if (c % g) {
return false;
}
x0 *= c / g;
y0 *= c / g;
if (a < 0) x0 = -x0;
if (b < 0) y0 = -y0;
return true;
}
const lli inf = LONG_LONG_MAX;
lli x, y, p, q;
lli findMinSolution(lli remY, lli remQ)
{
// t - (2x + 2y)n - x == remY
// t - (2x + 2y)n = remY + x
// t = remY + x + (2x + 2y)x0
// t - (p + q)n - p == remQ
// t - (p + q)n = remQ + p
// t = remQ + p + (p + q)y0
// remQ + p + (p + q)y0 = remY + x + (2x + 2y)x0
// -(2x + 2y)x0 + (p + q)y0 = remY + x - remQ - p
lli x0, y0, g;
lli a = -(2*x + 2*y), b = p + q, c = remY + x - remQ - p;
if (find_any_solution(a, b, c, x0, y0, g))
{
lli t1 = remY + x + (2*x + 2*y)*x0, t2 = remQ + p + (p + q)*y0;
bool isNegative = a / g < 0;
// lli lo = 0, hi = 1e18;
// while (lo < hi)
// {
// lli mid = (lo + hi + 1) >> 1;
// if ((y0 + mid*(a / g)) != 0 && LONG_LONG_MAX / abslol(y0 + mid*(a / g)) < (p + q))
// hi = mid - 1;
// else
// lo = mid;
// }
// lli maxHi = lo;
// lo = -1e18, hi = 0;
// while (lo < hi)
// {
// lli mid = (lo + hi) >> 1;
// if ((y0 + mid*(a / g)) != 0 && LONG_LONG_MAX / abslol(y0 + mid*(a / g)) < (p + q))
// lo = mid + 1;
// else
// hi = mid;
// }
// lli minLo = lo;
lli lo = -1e18, hi = 1e18;
while (lo < hi)
{
lli mid = (lo + hi + isNegative) >> 1;
lli e = remQ + p + (p + q)*(y0 + mid*(a / g));
// DEBUG printf("\t\t%lld %lld\n", mid, e);
if (isNegative)
{
if (e >= 0) lo = mid;
else hi = mid - 1;
}
else
{
if (e >= 0) hi = mid;
else lo = mid + 1;
}
}
x0 -= lo * b / g;
y0 += lo * a / g;
// DEBUG printf("\t%lld %lld - %lld %lld | %lld %lld\n", remY, remQ, t1, t2, 0LL, b / g);
return remQ + p + (p + q)*y0;
}
else
return inf;
}
int main()
{
int t; scanf("%d", &t);
for (int tt = 1; tt <= t; tt++)
{
// scanf("%lld %lld %lld %lld", &x, &y, &p, &q);
x = read();
y = read();
p = read();
q = read();
lli ans = inf;
for (int i = 0; i < y; i++)
for (int j = 0; j < q; j++)
ans = min(ans, findMinSolution(i, j));
if (ans == inf) printf("infinity\n");
else
{
print(ans);
printf("\n");
}
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, from, to) for (int i = from; i < (to); ++i)
#define mp(x,y) make_pair(x,y)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define pb push_back
using ll = long long;
using vin=vector<int>;
using vvin=vector<vin>;
using vll=vector<ll>;
using vvll=vector<vll>;
using vst=vector<string>;
using P = pair<ll,ll>;
const int inf=1e9+7;
const ll INF=9e18;
template <typename T> bool chmin(T &a, const T& b){if(a > b){a = b;return true;}return false;}
template <typename T> bool chmax(T &a, const T& b){if(a < b){a = b;return true;}return false;}
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; }
const int dx[4] = { 1, 0, -1, 0 };
const int dy[4] = { 0, 1, 0, -1 };
int main(){cout<<fixed<<setprecision(20);
//半分全列挙
int n;
ll x;
cin>>n>>x;
vll w(n);
rep(i,0,n)cin>>w[i];
vll cnt_l,cnt_r;
int l=n/2;
int r=n-l;
for(int bid=0;bid<(1<<l);bid++){
ll sum=0;
rep(i,0,l){
if(bid & (1<<i))sum+=w[i];
}
cnt_l.pb(sum);
}
for(int bid=0;bid<(1<<r);bid++){
ll sum=0;
rep(i,0,r){
if(bid & (1<<i))sum+=w[l+i];
}
cnt_r.pb(-sum);
}
sort(all(cnt_r));
sort(all(cnt_l));
ll ans=0;
for(auto v:cnt_l){
ll t=v;
ll p=*lower_bound(cnt_r.begin(), cnt_r.end(), t-x);
if(t-p>x)continue;
chmax(ans,t-p);
}
cout<<ans<<endl;
}
| //伟大的导师,伟大的领袖,伟大的统帅,伟大的舵手
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define int long long
using namespace std;
typedef long long ll;
char __buf[1<<20],*__p1,*__p2;
//#define getchar() (__p1==__p2?(__p2=__buf+fread(__p1=__buf,1,1<<20,stdin),__p1==__p2?EOF:*__p1++):*__p1++)
int read() {
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
return s*w;
}
const int maxn = 110, mod = 998244353;
int n,f[maxn][maxn*maxn],s,w[maxn],ans,pw[maxn];
signed main() {
n=read(),f[0][0]=1;
pw[0]=1;for(int i=1;i<=n;i++) pw[i]=1ll*pw[i-1]*i%mod;
for(int i=1;i<=n;i++) s+=(w[i]=read());
if(s&1) return cout<<0<<'\n',0;s/=2;
for(int i=1;i<=n;i++)
for(int j=i;j;j--)
for(int k=s;k>=w[i];k--) f[j][k]=(f[j][k]+f[j-1][k-w[i]])%mod;
for(int i=1;i<=n;i++) ans=(ans+1ll*pw[i]*pw[n-i]%mod*f[i][s])%mod;
cout<<ans<<'\n';
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int P = 998244353;
set<multiset<int>> s[111];
int dp[6333][6333];
int dfs(int n, int k) {
if (!k) return 1;
if (!n) return 0;
n = min(n,k);
int &ans = dp[n][k];
if (~ans) return ans;
return ans = (dfs(n-1,k)+dfs(2*n,k-n))%P;
}
int main() {
memset(dp,-1,sizeof dp);
int n, k;
scanf("%d%d", &k, &n);
printf("%d\n", dfs(n,k-n));
}
| #include <bits/stdc++.h>
#define rep(i,n) for(ll i=0;i<n;++i)
#define rrep(i, n) for(ll i=n-1;i>=0;--i)
#define rep1(i, n) for(ll i=1; i<=n; ++i)
#define repitr(itr,mp) for(auto itr=mp.begin();itr!=mp.end();++itr)
#define ALL(a) (a).begin(),(a).end()
template<class T> void chmax(T &a, const T &b){if(a < b){a = b;}}
template<class T> void chmin(T &a, const T &b){if(a > b){a = b;}}
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using pll = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll LINF = 1LL << 60;
const int INF = 1e9 + 7;
const double PI = 3.1415926535897932384626;
ll mod(ll a, ll m){return (a % m + m) % m;}
int main(){
ll n;
cin >> n;
ll rui[n], ruipow[n];
ll a[n];
rep(i, n){
cin >> a[i];
if(i){
rui[i] = a[i] + rui[i-1];
ruipow[i] = a[i]*a[i] + ruipow[i-1];
}
else {
rui[i] = a[i];
ruipow[i] = a[i] * a[i];
}
}
ll ans = 0;
rep1(i, n-1){
ll c1 = i * a[i] * a[i];
ll c2 = 2 * a[i] * rui[i-1];
ll c3 = ruipow[i-1];
// cout << rui[n-1] - rui[i] << ", " << c3 << endl;
ans += c1 - c2 + c3;
}
cout << ans << endl;
} |
#include<bits/stdc++.h>
using namespace std;
const int N=1005;
char a[N][N];
int n,m;
vector<int>g[N*2];
bool vis[N*2];
void add(int u,int v){
g[u].push_back(v);
g[v].push_back(u);
}
void dfs(int u){
vis[u]=1;
for(auto v:g[u])if(!vis[v])dfs(v);
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)cin>>a[i][j];
add(1,n),add(1,n+1),add(1,n+m);
for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)if(a[i][j]=='#')add(i,n+j);
int ans=0;
for(int i=1;i<=n;i++)if(!vis[i])dfs(i),ans++;
ans--;
memset(vis,0,sizeof(vis));
int now=0;
for(int i=n+1;i<=n+m;i++)if(!vis[i])dfs(i),now++;
now--;
cout<<min(ans,now)<<endl;
return 0;
} | #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <numeric>
#include <utility>
#include <iomanip>
#include <limits>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#define debug(x) cout << #x << " = " << x << endl
#define fori(i, ini, lim) for(int i = int(ini); i < int(lim); i++)
#define ford(i, ini, lim) for(int i = int(ini); i >= int(lim); i--)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
const int MAX = 1000 + 5;
char grid[MAX][MAX], grid_cpy[MAX][MAX];
bool line_unblocked[MAX], column_unblocked[MAX];
int n, m;
void unblock(int x, int y);
void unblock_line(int x) {
if (line_unblocked[x]) return;
line_unblocked[x] = true;
fori (i, 1, m + 1) {
if (grid[x][i] == '#') unblock(x, i);
}
}
void unblock_column(int y) {
if (column_unblocked[y]) return;
column_unblocked[y] = true;
fori (i, 1, n + 1) {
if (grid[i][y] == '#') unblock(i, y);
}
}
void unblock(int x, int y) {
unblock_line(x);
unblock_column(y);
}
void solve() {
cin >> n >> m;
fori (i, 1, n + 1) fori (j, 1, m + 1) cin >> grid[i][j];
grid[1][1] = grid[1][m] = grid[n][1] = grid[n][m] = '#';
memcpy(grid_cpy, grid, sizeof(char) * MAX * MAX);
int line_cost = 0;
unblock(1, 1);
fori (i, 1, n + 1) {
if (!line_unblocked[i]) {
grid[i][1] = '#';
line_cost++;
unblock(i, 1);
}
}
memcpy(grid, grid_cpy, sizeof(char) * MAX * MAX);
memset(line_unblocked, false, sizeof line_unblocked);
memset(column_unblocked, false, sizeof column_unblocked);
int column_cost = 0;
unblock(1, 1);
fori (i, 1, m + 1) {
if (!column_unblocked[i]) {
grid[1][i] = '#';
column_cost++;
unblock(1, i);
}
}
cout << min(line_cost, column_cost) << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct Fast {Fast() {std::cin.tie(0); ios::sync_with_stdio(false);}} fast;
using ull = unsigned long long;
using ll = long long;
using vi = vector<int>;
using vl = vector<long>;
using vll = vector<long long>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using vvll = vector<vll>;
using vs = vector<string>;
using pii = pair<int, int>;
#define pb push_back
#define mp make_pair
#define YESNO(bool) if(bool){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}
#define yesno(bool) if(bool){cout<<"yes"<<endl;}else{cout<<"no"<<endl;}
#define YesNo(bool) if(bool){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}
#define reps(i, a, n) for (ll i = (a); i < (ll)(n); ++i)
#define rep(i, n) reps(i, 0, n)
#define rrep(i, n) reps(i, 1, n + 1)
#define repd(i,n) for(ll i=n-1;i>=0;i--)
#define rrepd(i,n) for(ll i=n;i>=1;i--)
#define debug(x) cerr << "\033[33m(line:" << __LINE__ << ") " << #x << ": " << x << "\033[m" <<endl;
inline int in_int() {int x; cin >> x; return x;}
inline ll in_ll() {ll x; cin >> x; return x;}
inline string in_str() {string x; cin >> x; return x;}
template <typename T> inline bool chmin(T& a, const T& b) {bool compare = a > b; if (a > b) a = b; return compare;}
template <typename T> inline bool chmax(T& a, const T& b) {bool compare = a < b; if (a < b) a = b; return compare;}
struct node {
int col;
vi ns;
};
vector<node> t;
unordered_set<int> seen; // seen colors
set<int> ans;
void dfs(int x, int parent)
{
//debug(x);
int c = t[x].col;
bool is_new = seen.count(c) == 0;
if (is_new) ans.emplace(x);
seen.emplace(c);
for (auto y: t[x].ns) {
if (y != parent) dfs(y, x);
}
if (is_new)//
seen.erase(c);
}
int main()
{
int n = in_int();
t.reserve(n);
seen.reserve(n);
rep(i, n) {
int cc = in_int();
t[i].col = cc;
}
rep(i, n-1) {
int x = in_int()-1;
int y = in_int()-1;
t[x].ns.pb(y);
t[y].ns.pb(x);
}
dfs(0, -1);
for (auto it: ans) cout << it+1 << endl;
return 0;
}
| #pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { for (auto e : v) os << e << ' '; return os; }
template <typename T> ostream &operator<<(ostream &os, const set<T> &v) { for (auto e : v) os << e << ' '; return os; }
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &v) { for (auto e : v) os << e << ' '; return os; }
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << '(' << p.first << ", " << p.second << ')'; return os; }
template <typename T1, typename T2> ostream &operator<<(ostream &os, const map<T1, T2> &mp) { for (auto e : mp) os << e << ' '; return os; }
template <typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } else return false; }
template <typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } else return false; }
inline bool in(int x, int y, int h, int w) { return 0 <= x && x < h && 0 <= y && y < w; }
inline void print() { cout << '\n'; }
template <typename T1, typename... T2> void print(const T1 a, const T2 &... b) { cout << a << ' '; print(b...); }
#define DEBUG
#ifdef DEBUG
inline void debug_print() { cerr << endl; }
template <typename T1, typename... T2> void debug_print(const T1 a, const T2 &... b) { cerr << a << ' '; debug_print(b...); }
#define debug(...) cerr << __LINE__ << ": [" << #__VA_ARGS__ << "] = " , debug_print(__VA_ARGS__);
#else
#define debug(...) true
#endif
const int INF = (1<<30)-1;
const long long LINF = 1LL<<60;
const double EPS = 1e-9;
const int MOD = 1000000007;
//const int MOD = 998244353;
const int dx[8] = {-1,0,1,0,1,-1,1,-1};
const int dy[8] = {0,1,0,-1,1,-1,-1,1};
//-------------------------- Libraries --------------------------//
//--------------------------- Solver ----------------------------//
using Graph = vector<vector<int>>;
int N;
vector<int> C;
Graph g;
vector<int> ans;
void dfs(int v, int p, multiset<int> &se) {
if (!se.count(C[v])) ans.push_back(v+1);
se.insert(C[v]);
for (auto &&u : g[v]) {
if (u == p) continue;
dfs(u, v, se);
}
se.erase(se.find(C[v]));
}
void solve() {
cin >> N;
C.resize(N);
for (int i = 0; i < N; i++) cin >> C[i];
g.resize(N);
for (int i = 0; i < N-1; i++) {
int a, b;
cin >> a >> b;
a--; b--;
g[a].push_back(b);
g[b].push_back(a);
}
multiset<int> se;
dfs(0, -1, se);
sort(ans.begin(), ans.end());
print(ans);
}
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(20);
int t = 1;
//cin >> t;
while (t--) solve();
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 123;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
int ans = 0;
for(int i = 0; i < n; i++) {
int x;
cin >> x;
ans += max(0, x - 10);
}
cout << ans;
return 0;
}
| #include <bits/stdc++.h>
typedef long long ll;
#define ALL(l) (l).begin(),(l).end()
#define rep(i,n) for(ll (i)=0;(i)<(n);(i)++)
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
using namespace std;
//const ll mod = 998244353;
//const ll maxn = 1000000000;
//const ll mod = 1000000007;
//---------------------------------------------------------------------------------------------------
using vi = vector<int>; // intの1次元の型に vi という別名をつける
using vll = vector<ll>; // intの1次元の型に vi という別名をつける
using vvll = vector<vll>; // intの2次元の型に vvi という別名をつける
using vs = vector<string>; // stringの1次元の型に vs という別名をつける
using pll = pair<ll, ll>; // これ以降 pii という型名はpair<ll, ll> と同じ意味で使える
//---------------------------------------------------------------------------------------------------
template <typename T>
void chmax(T &a, const T& b) {
if (a < b) {
a = b; // aをbで更新
}
}
template <typename T>
void chmin(T &a, const T& b) {
if (a > b) {
a = b; // aをbで更新
}
}
int main() {
ll n;
cin>>n;
ll ans=0;
vll a(n);
rep(i,n)cin>>a[i];
rep(i,n){
if(a[i]>10)ans+=a[i]-10;
}
cout<<ans<<endl;
} |
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <stdexcept>
#include<math.h>
using namespace std;
using ll=long long ;
#define rep(i,n) for(int i=0;i<(n);i++)
#define f first
#define sc second
int fact(int n){
if(n==0){
return 1;
}else{
return n*fact(n-1);
}
}
bool is_prime(int x){
if(x<=1) return false;
for(int i=2;i*i<x;i++){
if(x%i==0) return false;}
return true;}
template<class T>void chmin(T& a,T b){
if(a>b){
a=b;
}
}
template<class T>void chmax(T& a,T b){
if(a<b){
a=b;
}
}
int gcd(int x, int y) {
if(x % y == 0) {
return y;
} else {
return gcd(y, x % y);
}
}
ll f(ll x,ll c,ll d){
return x-x/c-x/d+x/(c/(gcd(c,d))*d);
}
string to_oct(int n){
string s;
while(n){
s = std::to_string(n%8) + s;
n /= 8;
}
return s;
}
using namespace std;
int main(){
ll n,m;
cin>>n>>m;
ll a[n];
set<ll> d;
rep(i,n+1)d.insert(i);
ll mex=n;
map<ll,int> mp;
rep(i,n)cin>>a[i];
rep(i,m){
mp[a[i]]++;
d.erase(a[i]);
}
mex=*begin(d);
int j=0;
for(int i=m; i<=n-1;i++){
mp[a[i]]++;
d.erase(a[i]);
mp[a[j]]--;
if(mp[a[j]]==0)d.insert(a[j]);
mex=min<ll>(mex,*begin(d));
j++;
}
cout << mex << endl;
} | // Problem: E - Mex Min
// Contest: AtCoder - AtCoder Beginner Contest 194
// URL: https://atcoder.jp/contests/abc194/tasks/abc194_e
// Memory Limit: 1024 MB
// Time Limit: 4000 ms
//
// Powered by CP Editor (https://cpeditor.org)
// krishrawat
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define pb insert
#define eb emplace_back
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
#define show(x) cerr << #x << " is " << x << "\n"
#define show2(x, y) cerr << #x << " " << #y << " " << x << " " << y << "\n"
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vl;
typedef pair<ll, ll> pp;
typedef vector<pp> vp;
typedef map<ll, ll> ii;
typedef set<ll> sl;
typedef multiset<ll> msl;
typedef set<pair<ll, ll>> sp;
const ll MOD = 1e9 + 7;
const ll N = 2e5 + 1;
ll mod_mul(ll x, ll y) { return ((x % MOD) * (y % MOD)) % MOD; }
ll mod_add(ll x, ll y) { return ((x % MOD) + (y % MOD)) % MOD; }
// main code
void solve() {
ll n, m;
cin >> n >> m;
vl v(n);
for (auto &i : v) cin >> i;
sl s;
for (int i = 0; i <= m; i++) s.pb(i);
ll ans = 1e18;
ii mp;
for (int i = 0; i < m; i++) {
auto it = s.find(v[i]);
mp[v[i]]++;
if (it != s.end()) s.erase(it);
}
auto it = s.lower_bound(0);
ans = *it;
for (int i = m; i < n; i++) {
mp[v[i - m]]--;
if (v[i - m] < m && !mp[v[i - m]]) s.pb(v[i - m]);
it = s.find(v[i]);
mp[v[i]]++;
if (it != s.end()) s.erase(it);
auto it = s.lower_bound(0);
ans = min(ans, *it);
}
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll t;
t = 1;
// cin >> t;
while (t--) solve();
return 0;
}
|
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#ifdef ENABLE_DEBUG
#define dump(a) cerr<<#a<<"="<<a<<endl
#define dumparr(a,n) cerr<<#a<<"["<<n<<"]="<<a[n]<<endl
#else
#define dump(a)
#define dumparr(a,n)
#endif
#define FOR(i, a, b) for(ll i = (ll)a;i < (ll)b;i++)
#define For(i, a) FOR(i, 0, a)
#define REV(i, a, b) for(ll i = (ll)b-1LL;i >= (ll)a;i--)
#define Rev(i, a) REV(i, 0, a)
#define REP(a) For(i, a)
#define SIGN(a) (a==0?0:(a>0?1:-1))
typedef long long int ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<ll, ll> pll;
typedef pair<ll,pll> ppll;
typedef vector<ll> vll;
typedef long double ld;
typedef pair<ld,ld> pdd;
pll operator+(pll a,pll b){
return pll(a.first+b.first,a.second+b.second);
}
pll operator-(pll a,pll b){
return pll(a.first-b.first,a.second-b.second);
}
pll operator*(ll a,pll b){
return pll(b.first*a,b.second*a);
}
const ll INF=(1LL<<60);
#if __cplusplus<201700L
ll gcd(ll a, ll b) {
a=abs(a);
b=abs(b);
if(a==0)return b;
if(b==0)return a;
if(a < b) return gcd(b, a);
ll r;
while ((r=a%b)) {
a = b;
b = r;
}
return b;
}
#endif
template<class T>
bool chmax(T& a,const T& b){
if(a<b){
a=b;
return true;
}
return false;
}
template<class T>
bool chmin(T& a,const T& b){
if(a>b){
a=b;
return true;
}
return false;
}
template<class S,class T>
std::ostream& operator<<(std::ostream& os,pair<S,T> a){
os << "(" << a.first << "," << a.second << ")";
return os;
}
template<class T>
std::ostream& operator<<(std::ostream& os,vector<T> a){
os << "[ ";
REP(a.size()){
os<< a[i] << " ";
}
os<< " ]";
return os;
}
const string YES = "Yes";
const string NO = "No";
void solve(long long S, long long P){
for(ll i=1;i*i <= P; ++i){
if(P%i==0){
if(S==i+P/i){
cout<<YES<<endl;
return;
}
}
}
cout<<NO<<endl;
}
int main(){
cout<<setprecision(1000);
long long S;
scanf("%lld",&S);
long long P;
scanf("%lld",&P);
solve(S, P);
return 0;
}
| #pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef unordered_map<ll, ll> U_MAP;
typedef priority_queue<ll> pq;
typedef priority_queue<ll, vector<ll>, greater<ll>> rpq;
constexpr ll INF = 1e9, MOD = 1e9 + 7, ohara = 1e6 + 10;
constexpr ll LINF = 1e18;
#define rep(i, n) for (ll i = 0LL; (i) < (ll)(n); (i)++)
#define rrep(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rrrep(i, a, b) for (ll i = (ll)(a); i >= (ll)(b); i--)
#define all(v) (v).begin(), (v).end()
#define Size(n) (n).size()
#define Cout(x) cout << (x) << endl
#define doublecout(a) cout << fixed << setprecision(15) << a << endl;
#define fi first
#define se second
#define m_p make_pair
#define p_b push_back
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
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;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
//------ Believe yourself as a genius!!!!!! ------
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
// int dy[]={-1,0,1,-1,1,-1,0,1};int dx[]={-1,-1,-1,0,0,1,1,1};
string alph("abcdefghijklmnopqrstuvwxyz"), s;
ll n, cnt, ans, a[ohara], b[ohara], c, d, tmp, m, h, w, x, y, sum, k, q;
ll ri2[ohara], le2[ohara];
int main(void) {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
cin >> n >> m;
rep(i, n)cin >> a[i];
rep(i, m)cin >> b[i];
sort(a, a + n);
rep(i, n){
le2[i + 1] = a[i + 1] - a[i];
i++;
}
rrrep(i, n - 1, 0){
ri2[i - 1] = a[i] - a[i - 1];
i--;
}
//p(i,n)Cout(le2[i]);
rrep(i, 1, n)le2[i] += le2[i - 1];
rrrep(i, n - 1, 0)ri2[i] += ri2[i + 1];
ans = LINF;
rep(i, m){
ll le = 0, ri = n - 1, mid;
ll idx_ = n;
while(ri - le > 1){
mid = (le + ri) / 2;
if(b[i] < a[mid])ri = mid;
else le = mid;
}
rrep(j, le, ri + 1){
if(b[i] < a[j]){
idx_ = j;
break;
}
}
if(idx_ % 2 == 1)idx_--;
cnt = 0;
if(idx_ >= 1)cnt = le2[idx_ - 1];
cnt += ri2[idx_ + 1];
cnt += abs(b[i] - a[idx_]);
//Cout(cnt);
ans = min(ans, cnt);
}
Cout(ans);
return 0;
}
|
#define ll long long
#define lld double
#define M 202
#define f first
#define s second
#define pi 3.141592653589793238
#define vl vector<ll>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define N 100002
#define Endl endl
#define maxx(a,b,c,d) max(max(a,b),max(d,c))
#define minn(a,b,c,d) min(min(a,b),min(d,c))
#define pb push_back
#define mod 1000000007
#define pp 31
#define ss(v) sort(v.begin(),v.end())
#define sr(v) sort(v.begin(),v.end(),greater<pll>())
#define rin(A, B, C) (((A)<=(B))&&((B)<=(C)))
#include <bits/stdc++.h>
using namespace std;
ll power(ll a, ll b)
{
if(!b)return 1LL;
ll aa=power(a,b/2);
if(b%2)return aa*aa%mod*a%mod;
return aa*aa%mod;
}
ll im(ll a)
{
return power(a,mod-2);
}
// ll ncr(ll n,ll r)
// {
// return f[n]*im(f[r])%mod*im(f[n-r])%mod;
// }
bool check(ll n)
{
ll m=n;
while(n)
{
if(n%10!=0 && m%(n%10)!=0)
{
return false;
}
n/=10;
}
return true;
}
pll ol(ll a, ll b, ll c, ll d)
{
a=max(a,c);
b=min(b,d);
return {a,b};
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// freopen("output.txt", "w", stdout);
// freopen("input.txt", "r", stdin);
ll a,b;
cin>>a>>b;
ll ans1=0,ans2=0;
while(a)
{
ans1+=a%10;
a/=10;
}
while(b)
{
ans2+=b%10;
b/=10;
}
cout<<max(ans1,ans2)<<endl;
return 0;
}
| #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
template< int mod >
struct ModInt {
int x;
ModInt() : x(0) {}
ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
ModInt &operator+=(const ModInt &p) {
if((x += p.x) >= mod) x -= mod;
return *this;
}
ModInt &operator-=(const ModInt &p) {
if((x += mod - p.x) >= mod) x -= mod;
return *this;
}
ModInt &operator*=(const ModInt &p) {
x = (int) (1LL * x * p.x % mod);
return *this;
}
ModInt &operator/=(const ModInt &p) {
*this *= p.inverse();
return *this;
}
ModInt operator-() const { return ModInt(-x); }
ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
bool operator==(const ModInt &p) const { return x == p.x; }
bool operator!=(const ModInt &p) const { return x != p.x; }
ModInt inverse() const {
int a = x, b = mod, u = 1, v = 0, t;
while(b > 0) {
t = a / b;
swap(a -= t * b, b);
swap(u -= t * v, v);
}
return ModInt(u);
}
ModInt pow(int64_t n) const {
ModInt ret(1), mul(x);
while(n > 0) {
if(n & 1) ret *= mul;
mul *= mul;
n >>= 1;
}
return ret;
}
friend ostream &operator<<(ostream &os, const ModInt &p) {
return os << p.x;
}
friend istream &operator>>(istream &is, ModInt &a) {
int64_t t;
is >> t;
a = ModInt< mod >(t);
return (is);
}
static int get_mod() { return mod; }
};
constexpr int MOD = 998244353;
using mint = ModInt< MOD >;
int main(){
int n; cin >> n;
vector<int> a(n);
for(int i = 0; i < n; i++) cin >> a[i];
sort(a.begin(), a.end());
mint ans = 0;
mint sum = 0, base = 1;
for(int i = 0; i < n; i++){
sum += base * a[i];
base *= 2;
}
mint inv2 = mint(2).inverse();
for(int i = 0; i < n; i++){
ans += mint(a[i]) * a[i];
sum -= a[i];
sum *= inv2;
ans += sum * a[i];
}
cout << ans << endl;
} |
#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>;
struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_;
const int INF = (int)1e9;
const ll INFL = (ll)1e18;
const int MOD = 1e9 + 7;
const double EPS = 1e-10;
int dx[]={0, 0, -1, 1};
int dy[]={1, -1, 0, 0};
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()
{
ll a, b, c;
cin >> a >> b >> c;
if (c % 2 == 0)
{
a = abs(a);
b = abs(b);
}
if (a > b)
cout << '>' << endl;
else if (a < b)
cout << '<' << endl;
else
cout << '=' << endl;
return 0;
}
| /*WELCOME TO AYUSH RANJAN CODE'S*/
#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define endl "\n"
#define IOS std::ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define all(x) (x).begin(), (x).end()
#define loop(i,a,b) for(int i=(a);i<(b);i++)
#define rloop(i,a,b) for(int i=(a);i>(b);i--)
#define ft first
#define sd second
using namespace std;
const ll mod = 1e9+7;
const ll INF = 1e18;
const ll MAX = 1e6+1;
bool check_prime(ll n)
{
ll i;
if(n<=1)
{
return false;
}
if(n==2)
{
return true;
}
if(n%2==0)
{
return false;
}
ll sn=sqrt(n);
for(i=3;i<=sn;i++){
if(n%i==0)
{
return false;
}
}
return true;
}
vector<bool> seive(ll n)
{
ll i,j;
vector<bool> prime(n+1);
for(i=0;i<=n;i++)
{
prime[i]=true;
}
prime[0]=false;
prime[1]=false;
ll sn=sqrt(n);
for(i=2;i<=sn;i++)
{
if(prime[i])
{
for(j=i*i;j<=n;j+=i)
{
prime[j]=false;
}
}
}
return prime;
}
ll GCD(ll a,ll b)
{
if(b==0)
{
return a;
}
return GCD(b,a%b);
}
vector<ll> primeFactors(ll n)
{
vector<ll> v;
while (n%2 == 0)
{
v.push_back(2);
n = n/2;
for (ll i = 3; i <= sqrt(n); i = i+2)
{
while (n%i == 0)
{
v.push_back(i);
n = n/i;
}
}
v.push_back(n);
return v;
}
}
ll factorial[1000001];
void fact()
{
factorial[0]=1;
for(int i = 1; i <= 1000000; i++)
factorial[i] = factorial[i - 1] * i % mod;
}
ll power(ll x, unsigned ll y, unsigned ll m)
{
if (y == 0)
return 1;
ll p = power(x, y/2, mod) % mod;
p = (p * p) % mod;
return (y%2 == 0)? p : (x * p) % mod;
}
ll inverse(ll a)
{
return power(a, mod-2, mod);
}
long long binomial_coefficient(ll n, ll k)
{
return factorial[n] * inverse(factorial[k]) % mod * inverse(factorial[n - k]) % mod;
}
void solve()
{
ll i,j,k,n,m,d,l,r,y,x,z,b,c,s;
cin>>n>>s>>d;
ll a[n][2];
for(i=0;i<n;i++){
cin>>a[i][0]>>a[i][1];
}
ll ans=0;
for(i=0;i<n;i++){
if(a[i][0]<s&&a[i][1]>d){
cout<<"Yes";
return;
}
}
cout<<"No";
return;
}
int main()
{
// IOS;
int t;
t=1;//cin>>t;
while(t--)
{
solve();
cout<<"\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> results;
string res, a, b, c;
vector<bool> used(10, 0);
bool found = 0;
long long getNum(string a)
{
long long num = 0;
for(int i = 0; i < a.size(); ++i)
{
// find where it is in res;
num = num * 10 + results[res.find(a[i])];
}
return num;
}
void dp(int depth)
{
if(found) return;
else if(depth == results.size())
{
// try and check
long long d = getNum(a), e = getNum(b), f = getNum(c);
if(d + e == f && d > 0 && e > 0 && f > 0)
{
found = 1;
cout << d << endl << e << endl << f << endl;
}
return;
}
for(int i = 0; i < 10; ++i)
{
if(!used[i] && (i != 0 || (res[depth] != a[0] && res[depth] != b[0] && res[depth] != c[0])))
{
results[depth] = i;
used[i] = 1;
dp(depth + 1);
used[i] = 0;
}
}
}
int main()
{
cin >> a >> b >> c;
res = a + b + c;
sort(res.begin(), res.end());
res.resize(distance(res.begin(), unique(res.begin(), res.end())));
if(res.size() > 10)
{
cout << "UNSOLVABLE" << endl;
return 0;
}
results = vector<int>(res.size());
// dp results
dp(0);
if(!found)
cout << "UNSOLVABLE" << endl;
return 0;
}
| #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
long long n,m,q,usx=0,usy=200001;
long long getint()
{
char ch;do {ch=getchar();}while (ch!='-'&&(ch<'0'||ch>'9'));
long long ans=0,f=0;if (ch=='-') f=1; else ans=ch-'0';
while (isdigit(ch=getchar())) ans=ans*10+ch-'0';
if (f) ans*=-1;
return ans;
}
long long tr[200001];
bool vis[200001];
int lowbit(int x)
{
return x&-x;
}
void add(int x)
{
while(x<=n)
{
tr[x]+=1;
x+=lowbit(x);
}
return ;
}
long long query(int x)
{
long long ans=0;
while(x)
{
ans+=tr[x];
x-=lowbit(x);
}
return ans;
}
struct Q
{
int x,y;
void in()
{
x=n-getint()+1;
y=getint();
if(x==n)usy=usy<y? usy:y;
if(y==1)usx=usx>x? usx:x;
}
}nod[600011];
bool cmp(const Q& x,const Q& y)
{
return x.y==y.y? x.x<y.x:x.y<y.y;
}
int main()
{
n=getint();m=getint();q=getint();
for(int i=1;i<=q;i++)
nod[i].in();
for(int i=1;i<usx;i++)
{
nod[++q].x=i;
nod[q].y=1;
}
for(int i=usy+1;i<=m;i++)
{
nod[++q].x=n;
nod[q].y=i;
}
sort(nod+1,nod+q+1,cmp);
long long ans=0;
for(int i=1;i<=q;i++)
{
if(!vis[nod[i].x])
{
vis[nod[i].x]=true;
add(nod[i].x);
}
if(nod[i].y!=nod[i+1].y)
ans+=query(nod[i].x);
}
cout<<n*m-ans;
} |
#include <bits/stdc++.h>
#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)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define maxs(x,y) (x = max(x,y))
#define mins(x,y) (x = min(x,y))
#define limit(x,l,r) max(l,min(x,r))
#define lims(x,l,r) (x = max(l,min(x,r)))
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)),x.end())
#define show(x) cout<<#x<<" = "<<x<<endl;
#define print(x)cout<<x<<endl;
#define PQ(T) priority_queue<T,v(T),greater<T> >
#define bn(x) ((1<<x)-1)
#define dup(x,y) (((x)+(y)-1)/(y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef tuple<int,int,int> T;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<T> vt;
struct UnionFind {
vector< int > data;
UnionFind(int sz) {
data.assign(sz, -1);
}
int root(int x) {
if (data[x] < 0) return x;
else return data[x] = root(data[x]);
}
bool same(int x, int y) {
return root(x) == root(y);
}
bool unite(int x, int y) {
x = find(x), y = find(y);
if(x == y) return (false);
if(data[x] > data[y]) swap(x, y);
data[x] += data[y];
data[y] = x;
return (true);
}
int find(int k) {
if(data[k] < 0) return (k);
return (data[k] = find(data[k]));
}
int size(int k) {
return (-data[find(k)]);
}
};
int A[55][55];
const ll mod = 998244353;
int main() {
int n,m;
cin >> n >> m;
rep(i,n) rep(j,n)cin >> A[i][j];
UnionFind R(n),C(n);
rep(i,n){
rep(j,n){
bool ok1 = true, ok2 = true;
rep(k,n){
ok1&=(A[i][k]+A[j][k]<=m);
ok2&=(A[k][i]+A[k][j]<=m);
}
if(ok1)R.unite(i,j);
if(ok2)C.unite(i,j);
}
}
ll a = 1,b = 1;
for(auto v:R.data){
if(v<0){
ll x = -v,tmp = 1;
while(x>=1){
tmp*=x;
tmp%=mod;
x--;
}
a*=tmp;
a%=mod;
}
}
for(auto v:C.data){
if(v<0){
ll x = -v,tmp = 1;
while(x>=1){
tmp*=x;
tmp%=mod;
x--;
}
b*=tmp;
b%=mod;
}
}
ll ans = (a*b)%mod;
cout<<ans<<endl;
return 0;
} | #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
using namespace std;
template <class T, class U>
std::ostream& operator<<(std::ostream& os, const std::pair<T, U>& p) {
return os << '(' << p.first << ", " << p.second << ')';
}
template <class Tuple, std::size_t... Is>
void tuple_out(std::ostream& os, const Tuple& tp, std::index_sequence<Is...>) {
((os << (Is ? ", " : "(") << std::get<Is>(tp)), ...) << ')';
}
template <class... Args>
std::ostream& operator<<(std::ostream& os, const std::tuple<Args...>& tp) {
tuple_out(os, tp, std::index_sequence_for<Args...>{});
return os;
}
template <class T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {
int n = 0;
for(auto e : vec) os << (n++ ? ", " : "(") << e;
return os << (n ? ")" : "{}");
}
template <class T, class Compare>
std::ostream& operator<<(std::ostream& os, const std::set<T, Compare>& st) {
int n = 0;
for(auto e : st) os << (n++ ? ", " : "(") << e;
return os << (n ? ")" : "{}");
}
template<class T, class U, class Compare>
std::ostream& operator<<(std::ostream& os, const std::map<T, U, Compare>& mp) {
int n = 0;
for(auto e : mp) os << (n++ ? ", " : "(") << e;
return os << (n ? ")" : "{}");
}
#ifdef LOCAL
#define debug(...) std::cout << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__)
#else
#define debug(...) void(0)
#endif
void debug_out() { std::cout << '\n'; }
template <class Head, class... Tail>
void debug_out(Head&& head, Tail&&... tail) {
std::cout << head;
if(sizeof...(Tail) != 0) std::cout << ", ";
debug_out(std::forward<Tail>(tail)...);
}
using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;
template<class T>
inline bool chmax(T& x, T y){
if(x < y){
x = y;
return true;
}
return false;
}
template<class T>
inline bool chmin(T& x, T y){
if(x > y){
x = y;
return true;
}
return false;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, T;
cin >> N >> T;
vector<ll> A(N);
for(int i = 0; i < N; ++i) cin >> A[i];
vector<ll> dat(1, 0LL);
int M = N / 2;
for(int mask = 0; mask < 1 << M; ++mask){
ll sum = 0;
for(int i = 0; i < M; ++i){
if(mask >> i & 1) sum += A[i];
}
// cout << sum << ' ';
if(sum <= T) dat.emplace_back(sum);
}
// cout << '\n';
sort(dat.begin(), dat.end());
dat.erase(unique(dat.begin(), dat.end()), dat.end());
// for(auto x : dat) cout << x << ' ';
// cout << '\n';
ll ans = 0;
//cout << N - M << '\n';
for(int mask = 0; mask < (1 << (N - M)); ++mask){
//cout << mask << ' ';
ll sum = 0;
for(int i = 0; i < N - M; ++i){
//if(mask >> i & 1) cout << i << '\n';
if(mask >> i & 1) sum += A[M + i];
}
//cout << sum << ' ';
if(sum > T) continue;
auto it = upper_bound(dat.begin(), dat.end(), T - sum);
it = prev(it);
chmax(ans, sum + *it);
}
//cout << '\n';
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<ll, ll>;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define POPCOUNT(x) __builtin_popcount(x)
template <typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template <typename T> void chmax(T &a, const T &b) { a = max(a, b); }
const ll INF = 1LL << 60;
struct FastIO {
FastIO() {
cin.tie(0);
ios::sync_with_stdio(0);
}
} fastiofastio;
const ll MOD = 1e9 + 7;
int main() {
int N, M;
cin >> N >> M;
vector<int> A(M), B(M);
REP(i, M) {
cin >> A[i] >> B[i];
A[i]--;
B[i]--;
}
int K;
cin >> K;
vector<int> C(K), D(K);
REP(i, K) {
cin >> C[i] >> D[i];
C[i]--;
D[i]--;
}
int ans = 0;
for (int bit = 0; bit < (1 << K); bit++) {
vector<int> cnt(N);
REP(i, K) {
if (bit & (1 << i))
cnt[C[i]]++;
else
cnt[D[i]]++;
}
int s = 0;
REP(i, M) {
if (cnt[A[i]] >= 1 && cnt[B[i]] >= 1)
s++;
}
chmax(ans, s);
}
cout << ans << endl;
} | #include <iostream>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <sstream>
#include <cmath>
#include <limits.h>
#include <stdio.h>
using namespace std;
int main()
{
int n,m;
cin >> n >> m;
int a[m], b[m];
std::vector<int> v;
for(int i=0; i<m; i++){
cin >> a[i] >> b[i];
}
int k;
cin >> k;
int c[k], d[k];
for(int i=0; i<k; i++){
cin >> c[i] >> d[i];
}
long long int ans = 0;
for(long long int i=0; i<pow(2.0, k); i++){
std::map<int, int> mp;
std::bitset<16> bs(i);
for(long long int j=0; j<k; j++){
if(bs[j]==0){
mp[c[j]]++;
}else{
mp[d[j]]++;
}
}
long long int cnt = 0;
for(long long int j=0; j<m; j++){
if(mp[a[j]]>0 && mp[b[j]]>0){
cnt++;
}
}
if(ans < cnt) ans = cnt;
}
cout << ans << endl;
return 0;
} |
#include <stdio.h>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <functional>
//#include <atcoder/all>
//using namespace atcoder;
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef vector<vector<int>> v2int;
typedef vector<vector<vector<int>>> v3int;
typedef vector<ll> vll;
typedef vector<vector<ll>> v2ll;
typedef vector<vector<vector<ll>>> v3ll;
typedef list<int> liint;
typedef pair<int, int> pint;
typedef vector<pair<int, int>> vpint;
typedef vector<pair<ll, ll>> vpll;
typedef vector<pair<ll, int>> vpll_int;
typedef vector<pair<int, ll>> vpint_ll;
typedef set<pair<int, int>> spint;
typedef set<pair<ll, int>> spll;
typedef unordered_map<int, unordered_set<int>> Graph;
const int INF = int(2e9);
const ll LINF = ll(2e9) * ll(2e9);
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T, class C> void chmax(T& a, C b) { a > b ? : a = b; }
template<class T, class C> void chmin(T& a, C b) { a < b ? : a = b; }
void solveD() {
int N;
cin >> N;
vll a(N);
rep(i, N) cin >> a[i];
if (N % 2 == 1) {
cout << "Second" << endl;
return;
}
map<ll, int> num;
rep(i, N) {
num[a[i]]++;
}
for(auto n : num){
if (n.second %2 != 0) {
cout << "First" << endl;
return;
}
}
cout << "Second" << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int T;
cin >> T;
rep(t, T) {
solveD();
}
/*ll ans = 0;
cout << ans << endl;*/
return 0;
} | #include <cstdio>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <cstring>
#include <queue>
#include <map>
#define LL long long
#define LD long double
#define MAXN 100005
#define MAXM
#define P
#define INF 0x3f3f3f3f
using namespace std;
int t, n, a[MAXN];
int main()
{
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
for(int i=1; i<=n; ++i) scanf("%d", &a[i]);
if(n%2) printf("Second\n");
else
{
int tag=0;
sort(a+1, a+n+1);
for(int i=1, cnt=0; i<=n; ++i)
{
if(a[i]!=a[i-1] && cnt%2) tag=1;
cnt++;
}
if(tag) printf("First\n");
else printf("Second\n");
}
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define absdiff(a,b) (a>b)?a-b:b-a
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define fast ios_base::sync_with_stdio(false);cin.tie(0);
#define MOD 1000000007
#define ll_MAX 19223372036859
#define endl "\n"
ll fast_expo(ll x,ll p){
if(p==0) return 1;
else if(p%2==0){ ll t=fast_expo(x,p/2)%MOD;return (t*t)%MOD;}
else return (x*(fast_expo(x,p-1))%MOD)%MOD;}
ll invMod(ll num){
return fast_expo(num, MOD - 2)%MOD;
}
int main(){
fast
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll t_c; t_c = 1;
// cin >> t_c;
for(ll t_i = 0; t_i < t_c; ++t_i){
ll n; cin >> n;
vector<ll> AdjL[n + 1];
ll a[n], b[n];
for(ll i = 1; i < n; ++i){
cin >> a[i] >> b[i];
AdjL[a[i]].pb(b[i]);
AdjL[b[i]].pb(a[i]);
}
ll lev[n + 1], vis[n + 1]{};
queue<ll> Q;
Q.push(1);
vis[1] = lev[1] = 1;
while(!Q.empty()){
ll node = Q.front();
Q.pop();
for(auto i: AdjL[node]){
if(!vis[i]){
vis[i] = 1;
Q.push(i);
lev[i] = lev[node] + 1;
}
}
}
ll c[n + 1]{};
ll q; cin >> q;
for(ll i = 1; i <= q; ++i){
ll t, e, x; cin >> t >> e >> x;
ll ini = a[e], fin = b[e];
if(t == 2) swap(ini, fin);
if(lev[ini] < lev[fin]){
c[1] += x;
c[fin] -= x;
} else {
c[ini] += x;
}
}
ll ans[n + 1]{};
fill_n(vis, n + 1, 0);
Q.push(1);
vis[1] = 1; ans[1] = c[1];
while(!Q.empty()){
ll node = Q.front();
Q.pop();
for(auto i: AdjL[node]){
if(!vis[i]){
vis[i] = 1;
Q.push(i);
ans[i] = ans[node] + c[i];
}
}
}
for(ll i = 1; i <= n; ++i)
cout << ans[i] << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#include <math.h>
#include <iomanip>
#include <cstdint>
#include <string>
#include <sstream>
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; }
#define rep(i,n) for (int i = 0; i < (n); ++i)
typedef long long ll;
using P = pair<ll,ll>;
const int mod=998244353;
const int INF=1001001001;
int main() {
int N;
cin>>N;
vector<ll>x(N),y(N);
rep(i,N){cin>>x[i]>>y[i];}
int M;
cin>>M;
vector<P>b1(M+1),b2(M+1),b3(M+1);
b3[0]=P(1,0);
b2[0]=P(0,1);
rep(i,M){
int s;
cin>>s;
if(s==1){
b1[i+1]=P(b1[i].second,-b1[i].first);
b2[i+1]=P(b2[i].second,-b2[i].first);
b3[i+1]=P(b3[i].second,-b3[i].first);
}
else if(s==2){
b1[i+1]=P(-b1[i].second,b1[i].first);
b2[i+1]=P(-b2[i].second,b2[i].first);
b3[i+1]=P(-b3[i].second,b3[i].first);
}
else{
int p;
cin>>p;
if(s==3){
b1[i+1]=P(2*p-b1[i].first,b1[i].second);
b2[i+1]=P(2*p-b2[i].first,b2[i].second);
b3[i+1]=P(2*p-b3[i].first,b3[i].second);
}
if(s==4){
b1[i+1]=P(b1[i].first,2*p-b1[i].second);
b2[i+1]=P(b2[i].first,2*p-b2[i].second);
b3[i+1]=P(b3[i].first,2*p-b3[i].second);
}
}
}
int Q;
cin>>Q;
rep(i,Q){
int a,b;
cin>>a>>b;
b--;
ll X=(b3[a].first-b1[a].first)*x[b]+(b2[a].first-b1[a].first)*y[b]+b1[a].first;
ll Y=(b3[a].second-b1[a].second)*x[b]+(b2[a].second-b1[a].second)*y[b]+b1[a].second;
cout<<X<<" "<<Y<<endl;
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
#define ll long long
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rep_up(i, a, b) for (ll i = a; i < b; ++i)
#define rep_down(i, a, b) for (ll i = a; i > b; --i)
#define P pair<ll, ll>
#define Graph vector<vector<ll>>
#define fi first
#define se second
#define vvvvll vector<vector<vector<vector<ll>>>>
#define vvvll vector<vector<vector<ll>>>
#define vvll vector<vector<ll>>
#define vll vector<ll>
#define vvvvdo vector<vector<vector<vector<double>>>>
#define vvvdo vector<vector<vector<double>>>
#define vvdo vector<vector<double>>
#define vdo vector<double>
#define pqll priority_queue<ll>
#define pqllg priority_queue<ll, vector<ll>, greater<ll>>
constexpr ll INF = (1ll << 60);
constexpr ll mod = 998244353;
// constexpr ll mod = 67280421310721;
// constexpr ll mod = 1000000007;
constexpr double pi = 3.14159265358979323846;
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;
}
template <typename T>
void pt_vll(vector<T> &v) {
ll vs = v.size();
rep(i, vs) {
cout << v[i];
if (i == vs - 1)
cout << "\n";
else
cout << " ";
}
}
template <typename T>
void pt_vvll(vector<vector<T>> &v) {
ll vs = v.size();
rep(i, vs) pt_vll(v[i]);
}
template <typename T>
void pt(T val) {
cout << val << "\n";
}
ll gcd(ll a, ll b) {
if (a % b == 0) return b;
return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll modinv(ll a, ll m) {
ll b = m, u = 1, v = 0;
while (b) {
ll t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
u %= m;
if (u < 0) u += m;
return u;
}
ll modpow(ll x, ll n) {
ll ret = 1;
while (n > 0) {
if (n & 1)
ret = ret * x % mod; // n の最下位bitが 1 ならば x^(2^i) をかける
x = x * x % mod;
n >>= 1; // n を1bit 左にずらす
}
return ret;
}
ll minv(ll a, ll m) { return modpow(a, m - 2); }
void pre_process(vector<ll> &a, vector<ll> &s) { //累積和のvector
ll n = (ll)a.size();
s.assign(n + 1, 0);
for (ll i = 0; i < n; i++) {
s[i + 1] = s[i] + a[i];
s[i + 1] %= mod;
}
}
#define MAX 1000100
ll fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (ll 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;
}
}
// 二項係数計算
ll combi(ll n, ll k) {
if (n < k) return 0;
if (n < 0 || k < 0) return 0;
return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
// mainに COMinit(); を入れる
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
COMinit();
ll N, K;
cin >> N >> K;
vll A(N);
rep(i, N) cin >> A[i];
vvll mp(K + 1, vll(N)), sp(K + 1);
rep(i, K + 1) {
rep(j, N) { mp[i][j] = modpow(A[j], i); }
}
rep(i, K + 1) { pre_process(mp[i], sp[i]); }
rep_up(X, 1, K + 1) {
ll ans = 0;
rep(k, X + 1) {
ll m1 = (sp[k][N] * sp[X - k][N]) % mod;
ll m2 = combi(X, k);
(ans += (m1 * m2) % mod) %= mod;
}
((ans -= (modpow(2, X) * sp[X][N]) % mod) += 100 * mod) %= mod;
ans *= minv(2, mod);
ans %= mod;
(ans += 100 * mod) %= mod;
pt(ans);
}
return 0;
} | /* include */
#include <bits/stdc++.h>
using namespace std;
/* definition */
#define _GLIBCXX_DEBUG
#define rep(i, N) for (ll i = 0; i < (ll)(N); i++)
#define repp(i, N) for (ll i = 1; i <= (ll)(N); i++)
#define pb(n) push_back(n)
#define all(v) (v).begin(), (v).end()
#define co(ans) cout << (ans)
#define fin << endl
#define enter cout << endl
#define ll long long
#define spr(i) cout << fixed << setprecision((ll)(i))
#define lmax 9223372036854775807
#define lmin -9223372036854775807
#define pi 3.1415926535897932384626433832795028
/* own maid function */
ll ctol(char c){
if(c == '0')return 0;
else if(c == '1')return 1;
else if(c == '2')return 2;
else if(c == '3')return 3;
else if(c == '4')return 4;
else if(c == '5')return 5;
else if(c == '6')return 6;
else if(c == '7')return 7;
else if(c == '8')return 8;
else if(c == '9')return 9;
else return -1;
}
ll gcd(ll a, ll b){
return b ? gcd(b, a % b ) : a;
}
ll lcm(ll a, ll b){
return a / gcd(a, b) * b;
}
bool isPrime(ll x){
ll i;
if (x < 2)
return 0;
else if (x == 2)
return 1;
if (x % 2 == 0)
return 0;
for (i = 3; i * i <= x; i += 2)
if (x % i == 0)
return 0;
return 1;
}
ll digsum(ll n){
ll res = 0;
while (n > 0)
{
res += n % 10;
n /= 10;
}
return res;
}
ll dignum(ll n){
ll res = 0;
while (n > 0)
{
res += n % 10;
n++;
}
return res;
}
vector<ll> enum_div(ll n){
vector<ll> ret;
for (ll i = 1; i * i <= n; ++i)
{
if (n % i == 0)
{
ret.push_back(i);
if (i != 1 && i * i != n)
{
ret.push_back(n / i);
}
}
}
return ret;
}
ll stringcount(string s, char c){
return count(s.cbegin(), s.cend(), c);
}
/*
vector<> ();
vector<vector<>> (, vector<>(, ));
vector<vector<vector<>>> (, vector<vector<>>(, vector<>(, )));
*/
/* cins */
ll cinl(){ll l; cin >> l; return l;}
double cind(){double d; cin >> d; return d;}
char cinc(){char c; cin >> c; return c;}
string cins(){string s; cin >> s; return s;}
/* function */
/* main */
int main(void){
ll A = cinl(), B = cinl(), C = cinl();
ll a, b, c;
a = A * (A+1) / 2;
b = B * (B+1) / 2;
c = C * (C+1) / 2;
a %= 998244353;
b %= 998244353;
c %= 998244353;
co((a*b%998244353)*c%998244353) fin;
}
/* fin */ |
#include<iostream>
#include<cstdio>
using namespace std;
struct Edge{
int to,nxt;
}e[400005];
int n,k,h[200005],num,f[200005],g[200005],cnt;
void add(int u,int v){
e[++num].to=v;
e[num].nxt=h[u];
h[u]=num;
}
void dfs(int u,int fa,int lim){
int i,v;
f[u]=1e9;
g[u]=0;
for(i=h[u];i;i=e[i].nxt){
v=e[i].to;
if(v!=fa){
dfs(v,u,lim);
f[u]=min(f[u],f[v]+1);
g[u]=max(g[u],g[v]+1);
}
}
if(f[u]+g[u]<=lim)
g[u]=-1e9;
if(g[u]>=lim)
++cnt,f[u]=0,g[u]=-1e9;
}
bool judge(int lim){
cnt=0;
dfs(1,0,lim);
return (cnt+(g[1]>=0))<=k;
}
int solve(){
int l=1,r=n,mid,res;
while(l<=r){
mid=(l+r)>>1;
if(judge(mid))
r=mid-1,res=mid;
else
l=mid+1;
}
return res;
}
int main(){
ios::sync_with_stdio(false);
int i,u,v;
cin>>n>>k;
for(i=1;i<n;i++){
cin>>u>>v;
add(u,v);add(v,u);
}
cout<<solve()<<endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
using P = pair<int,int>;
P dfs(int u, int p, vector<vector<int>>&G, int t){
int ac = 0;
int mu = -1e9, ma = t;
for(int v: G[u]) if(v != p){
auto [fs, sd] = dfs(v,u,G,t); fs--;
ac += sd;
if(fs < 0) mu = max(mu,fs);
else ma = min(ma,fs);
}
if(mu+ma >= 0) return {mu,ac};
else if(ma == 0) ac++;
return {ma, ac};
}
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
int n, k; cin >> n >> k;
vector<vector<int>> G(n);
rep(i,n-1){
int u, v; cin >> u >> v; v--; u--;
G[v].push_back(u);
G[u].push_back(v);
}
int l=-1, r=n-1;
while(l+1<r){
int mid = (l+r) / 2;
auto [m, kk] = dfs(0,0,G,mid); if(m > 0) kk++;
(kk <= k ? r : l) = mid;
}
cout << r << "\n";
} |
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main()
{
int n;
cin >> n;
ll am = 0;
double ae = 0;
int ac = 0;
rep(i, n)
{
ll x;
cin >> x;
x = abs(x);
am += x;
ae += x * x;
ac = max(ac, (int)x);
}
ae = sqrt(ae);
cout << setprecision(9) << fixed;
cout << am << endl;
cout << ae << endl;
cout << ac << endl;
return 0;
} | /* include c++ libraries */
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <map>
#include <queue>
#include <set>
/* include c-lang libraries */
#include <climits>
#include <cmath>
/* include my libraries */
//#include <show_vec.hh>
using namespace std;
#define LINE DEBUG{ printf("LINE : %d\n", __LINE__); }
#define DEBUG if(false)
using ll = long long;
template<class T> using vec = vector<T>;
template<class T>void sort(vector<T> &v){
sort(v.begin(), v.end());
return;
}
template<class T>void rsort(vector<T> &v){
sort(v.begin(), v.end(), std::greater<int>());
return;
}
const int MOD = 1000000007;
ll Manhattan_Distance(vec<int> v){
ll D = 0;
for(int i = 0; i < v.size(); i++){
D += abs(v[i]);
}
return D;
}
double Euclidean_Distance(vec<int> v){
ll D = 0;
for(int i = 0; i < v.size(); i++){
D += (ll)v[i] * (ll)v[i];
}
return sqrt(D);
}
int Chebyshev_Distance(vec<int> v){
int D = 0;
for(int i = 0; i < v.size(); i++){
if(D < abs(v[i])){
D = abs(v[i]);;
}
}
return D;
}
int main(int argc, char* argv[]){
int N;
cin >> N;
vec<int> x(N);
for(int i = 0; i < N; i++){
cin >> x[i];
}
cout << Manhattan_Distance(x) << endl;
printf("%.10lf\n", Euclidean_Distance(x));
cout << Chebyshev_Distance(x) << endl;
return 0;
}
|
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define all(x) (x).begin(),(x).end()
#define uniq(v) (v).erase(unique(all(v)),(v).end())
#define sz(x) (int)((x).size())
#define fr first
#define sc second
#define pii pair<int,int>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define mem1(a) memset(a,-1,sizeof(a))
#define mem0(a) memset(a,0,sizeof(a))
#define ppc __builtin_popcount
#define ppcll __builtin_popcountll
template<typename T1,typename T2>istream& operator>>(istream& in,pair<T1,T2> &a){in>>a.fr>>a.sc;return in;}
template<typename T1,typename T2>ostream& operator<<(ostream& out,pair<T1,T2> a){out<<a.fr<<" "<<a.sc;return out;}
template<typename T,typename T1>T amax(T &a,T1 b){if(b>a)a=b;return a;}
template<typename T,typename T1>T amin(T &a,T1 b){if(b<a)a=b;return a;}
const long long INF=1e18;
const int32_t M=1e9+7;
const int32_t MM=998244353;
const int N=0;
void solve(){
int a,b,c;
cin >> a >> b >> c;
cout << 21 - a - b - c;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#ifdef SIEVE
sieve();
#endif
#ifdef NCR
init();
#endif
int t=1;
//cin>>t;
while(t--) solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
int a,b,c;
cin>>a>>b>>c;
ll sum=a+b+c;
ll p=21-sum;
cout<<p<<endl;
return 0;
}
|
#include <iostream>
#include <string>
#include <utility>
#include <stack>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
#include <climits>
#include <set>
#include <numeric>
#include <iomanip>
#include <cmath>
#include <cstring>
using namespace std;
long long dp[61][2];
string S[60];
int main(){
int N;
cin >> N;
for(int i = 0; i < N; i++){
cin >> S[i];
}
dp[0][0] = 1;
dp[0][1] = 1;
for(int i = 1; i <= N; i++){
if(S[i - 1] == "OR"){
dp[i][0] = dp[i - 1][0];
dp[i][1] = dp[i - 1][0] + (long long) 2 * dp[i - 1][1];
} else {
dp[i][0] = (long long) 2 * dp[i - 1][0] + dp[i - 1][1];
dp[i][1] = dp[i - 1][1];
}
}
cout << dp[N][1] << endl;
return 0;
}
| //int a = s - '0'; 文字から数字
//小文字から大文字
//transform(a.begin(), a.end(), a.begin(), ::toupper);
//map 全探索
//auto begin = p.begin(), end = p.end();
//for (auto it = begin; it != end; it++) {}
//mapのキー:it->first mapのバリュー:it->second
//大文字判定 isupper(文字) 小文字判定 islower(文字)
//do{}while(next_permutation(ALL(配列)))
//小文字に対応する文字コード:S[i] - 'a'
//文字コード→小文字:(char)(数字+'a')
//グラフの距離:隣接行列で扱う
//絶対値はソートで取れる
//島渡りの問題:中間ノードに着目
//数が大きい時の比較はstring型で行う
//全て0になったか調べたい->0になるたびにcntする
//例外処理は最初にする
//x = p^m × q^n ×...の約数の個数:(n+1)*(m+1)....
//N!のどの素因数で何回割れるか
//⇔1~Nまでの数がそれぞれどの素因数で何回割り切れるかの和
//パズルの問題->一般化して全探索
//stack<ll> s;
//s.push(要素);s.top();s.pop();
//queue<ll> q;
//q.push(要素);q.front();q.pop();
//同じ作業繰り返す系の問題:収束先を見つける
//過半数:N/2.0で判定
//優先度付きキュー
//priority_queue<
//ll,
//vector<ll>
//> q;
#include <bits/stdc++.h>
#define rep(i,N) for(int i = 0; i < N; i++)
#define ALL(a) (a).begin(),(a).end()
#define ll long long int
#define MOD pow(10, 9) + 7
#define PI 3.14159265358979323846264338327950L
using namespace std;
// K進数でのNの桁数
ll dig(ll N, ll K) {
ll dig = 0;
while (N) {
dig++;
N /= K;
}
return dig;
}
// a,bの最大公約数
ll gcd(ll a, ll b) {
if (b == 0) return a;
return gcd(b, a%b);
}
//a,bの最小公倍数
ll lcm(ll a, ll b) {
ll g = gcd(a, b);
return a / g * b;
}
//階乗計算
ll f(ll n) {
if (n == 0 || n == 1) return 1;
else return (n * f(n - 1));
}
//Nのd桁目の数
ll dignum(ll N, ll d) {
ll x = pow(10, d);
N %= x;
ll y = pow(10, d - 1);
N /= y;
return N;
}
//Nをdで何回割れるか
ll divcnt(ll N, ll d) {
ll ans = 0;
while (1) {
if (N%d == 0) {
ans++;
N /= d;
}
else break;
}
return ans;
}
//素数判定
bool prime(ll num) {
if (num < 2) return false;
else if (num == 2) return true;
else if (num % 2 == 0) return false;
double sqrtNum = sqrt(num);
for (int i = 3; i <= sqrtNum; i += 2) {
if (num % i == 0) return false;
}
return true;
}
//フィボナッチ数列
vector<ll> memo(pow(10, 6) + 1);
ll fibo(ll n) {
if (n == 1) return 1;
else if (n == 2) return 1;
else if (memo[n] != 0) return memo[n];
else return memo[n] = fibo(n - 1) + f(n - 2);
}
ll RS(ll N, ll P, ll M) {
if (P == 0) return 1;
if (P % 2 == 0) {
ll t = RS(N, P / 2, M);
return t * t % M;
}
return N * RS(N, P - 1, M);
}
vector<int> IntegerToVector(int bit, int N) {
vector<int> S;
for (int i = 0; i < N; ++i) {
if (bit & (1 << i)) {
S.push_back(i);
}
}
return S;
}
int main() {
int N; cin >> N;
vector<string> S(N); rep(i, N) cin >> S[i];
ll T=1, F=1;
for(int i = 0; i < N; i++){
if(S[i] == "AND") {
F = 2*F + T;
}
else{
T = 2*T + F;
}
}
cout << T << endl;
} |
#include<bits/stdc++.h>
#define ll long long
#define vi vector<int>
#define vll vector<long long>
#define all(x) x.begin(),x.end()
#define rep(i,a,b) for(int i=a;i<b;i++)
#define F first
#define S second
#define endl "\n"
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL)
#define pb push_back
#define deb(x) cout<<#x<<" "<<x<<endl;
#define sz(a) (int)a.size()
#define Q(x) for(int i=0;i<x;i++)
#define show(x) cout<<#x<<" ";for(auto i:x)cout<<i<<" ";cout<<endl;
// ctrl + shift + q -- stop execution
// use brackets after modulo operation
//if ((i & (1 << j) ) == 0) and if(i & (i << j) == 0) are different
const ll mod = 1e9 + 7;
using namespace std;
/*void solve() {
}*/
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
/*int t;
cin >> t;
while (t--) {
solve();
}*/
ll n, x;
cin >> n >> x;
vll a(n);
rep(i, 0, n)cin >> a[i];
rep(i, 0, n) {
if (a[i] != x)cout << a[i] << " ";
}
return 0;
} | #include <bits/stdc++.h>
#define int long long
using namespace std;
void fastIO() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
void test_case() {
int a, b, c;
cin >> a >> b >> c;
if (a * 2 == b + c || b * 2 == a + c || c * 2 == a + b)
cout << "Yes\n";
else cout << "No\n";
}
void solve() {
int T = 1;
for (int tc = 0; tc < T; ++tc)
test_case();
}
int32_t main() {
fastIO();
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define mii map <int, int>
#define mll map <ll, ll>
#define pii pair <int, int>
#define pll pair <ll, ll>
#define vi vector <int>
#define vd vector <double>
#define vll vector <ll>
#define fi first
#define se second
#define si set <int>
#define sll set <ll>
#define spii set <pii>
#define vs vector <string>
#define vpii vector <pair <int, int> >
#define vpll vector <pair <long long, long long> >
#define vvi vector <vector <int> >
#define vvpii vector <vector <pii > >
#define vb vector <bool>
#define vvb vector <vb>
#define mp make_pair
#define vvll vector <vll>
#define vsi vector <si>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define MANX MAXN
#define itn int
#define dbg(x); {cout << #x << "=" << x << ", ";}
#define in(x); { for (auto &to : x) cin >> to;}
#define out(x); { for (auto &to : x) cout << to << " "; cout << '\n'; }
const ll INFLL = 1e18;
const int MAXN = 1e6+100;
const ll INF = 1e9;
const int mod1 = 1e9+7;
const int mod2 = 1e9+21;
void solve()
{
int n;
cin>>n;
ll need;
cin>>need;
vector <int>a(n);
in(a);
// sum==X (mod%k)
vector <vector <vi> > dp(n+1,vvi(n+1,vi(n+1,-INF)));
for (int i=1;i<=n;i++)
{
dp[i][0][0]=0;
}
for (auto to:a)
{
for (int total=1;total<=n;total++)
{
for (int got=total-1;got>=0;got--)
{
for (int rem=0;rem<total;rem++)
{
if (dp[total][got][rem]==-INF)
continue;
dp[total][got+1][(rem+to)%total]=max(dp[total][got+1][(rem+to)%total],dp[total][got][rem]+to);
}
}
}
}
ll ans=INFLL;
for (int total=1;total<=n;total++)
{
if (dp[total][total][need%total]==-INF)
continue;
// cout<<need%total<<" "<<(need-dp[total][total][need%total])/total<<" "<<dp[total][total][need%total]<<" "<<total<<'\n';
ans=min(ans,(need-dp[total][total][need%total])/total);
}
cout<<ans<<'\n';
}
int main() {
#ifdef Mip182
freopen("a.in", "r", stdin);
#else
ios_base::sync_with_stdio(0);
cin.tie(0);
#endif
int _t;
_t=1;
// cin>>_t;
while (_t--)
solve();
#ifdef Mip182
cout<<'\n'<<"Time : "<<(double)(clock())/CLOCKS_PER_SEC<<'\n';
#endif
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
int main() {
int n;
int ans = 1 << 30;
cin >> n;
vector<int> A(n);
for(int i = 0; i < n; i++){
cin >> A.at(i);
}
for(int bit = 0; bit < (1 << (n-1)); bit++){
int index = 0;
int tmp = 0;
vector<int> B(n, 0);
for(int i = 0; i < n; i++){
if(((bit >> (i-1)) & 1) && (i > 0)){
index++;
}
B[index] |= A[i];
}
for(int i = 0; i <= index; i++){
tmp ^= B[i];
}
ans = min(tmp, ans);
}
cout << ans << endl;
} |
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cassert>
#include <functional>
using ll = long long;
using namespace std;
template<typename A, typename B>
bool chmin(A &a, const B b) {
if (a <= b) return false;
a = b;
return true;
}
template<typename A, typename B>
bool chmax(A &a, const B b) {
if (a >= b) return false;
a = b;
return true;
}
#ifndef LOCAL
#define debug(...) ;
#else
#define debug(...) cerr << __LINE__ << " : " << #__VA_ARGS__ << " = " << _tostr(__VA_ARGS__) << endl;
template<typename T>
ostream &operator<<(ostream &out, const vector<T> &v);
template<typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
out << "{" << p.first << ", " << p.second << "}";
return out;
}
template<typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
out << '{';
for (const T &item : v) out << item << ", ";
out << "\b\b}";
return out;
}
void _tostr_rec(ostringstream &oss) {
oss << "\b\b \b";
}
template<typename Head, typename... Tail>
void _tostr_rec(ostringstream &oss, Head &&head, Tail &&...tail) {
oss << head << ", ";
_tostr_rec(oss, forward<Tail>(tail)...);
}
template<typename... T>
string _tostr(T &&...args) {
ostringstream oss;
int size = sizeof...(args);
if (size > 1) oss << "{";
_tostr_rec(oss, forward<T>(args)...);
if (size > 1) oss << "}";
return oss.str();
}
#endif
constexpr int mod = 1'000'000'007; //1e9+7(prime number)
constexpr int INF = 1'000'000'000; //1e9
constexpr ll LLINF = 2'000'000'000'000'000'000LL; //2e18
constexpr int SIZE = 200010;
// GCC __gcd(long long A, long long B)
ll gcd(ll a, ll b) {
if (a == 0) return b;
return gcd(b % a, a);
}
ll lcm(ll a, ll b) {
return a / gcd(a, b) * b;
}
template<typename T>
vector<T> divisor(T n) {
vector<T> res, res2;
for (T i = 1; i * i <= n; i++) {
if (n % i == 0) {
res.push_back(i);
if (i * i < n) res2.push_back(n / i);
}
}
reverse(res2.begin(), res2.end());
res.insert(res.end(), res2.begin(), res2.end());
return res;
}
int main() {
int N, A[SIZE];
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%d", A + i);
}
sort(A, A + N);
map<int, int> mm;
for (int i = 0; i < N; i++) {
auto divs = divisor(A[i]);
for (int d : divs) {
auto it = mm.find(d);
if (it != mm.end()) {
mm[d] = gcd(A[i], it->second);
} else {
mm[d] = A[i];
}
}
}
int ans = 0;
for (auto v : mm) {
ans += v.first == v.second && v.first <= A[0];
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#define REP(i,n) for (int i=0;i<(n);++i)
using namespace std;
using ll = long long;
template <class T, class U> void amax(T& x, U y) {if (x < y) x = y;}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
ll x = 0;
ll S = 0;
ll M = 0;
ll result = 0;
REP(i, N) {
int A;
cin >> A;
S += A;
amax(M, S);
amax(result, x + M);
x += S;
}
cout << result << endl;
return 0;
}
|
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
unsigned int oi[4];
bool flag = false;
int sum = 0;
cin >> oi[0] >> oi[1] >> oi[2] >> oi[3];
sum = oi[0] + oi[1] + oi[2] + oi[3];
for(int i=0; i<4; i++){
if(oi[i] == (sum - oi[i])){
flag = true;
break;
}
}
for(int i=0; i<4-1; i++){
for(int j=1; j<4; j++){
if((oi[i] + oi[j]) == (sum - oi[i] - oi[j]))
flag = true;
}
}
if(flag == true)
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
} | #include <iostream>
int main(){
int A,B,C,D;
std::cin >> A >> B >> C >> D;
if (A==B+C+D || B==A+C+D || C==A+B+D || D==A+B+C || A+B==C+D || A+C==B+D || A+D==B+C){
std::cout << "Yes" << std::endl;
}
else{
std::cout << "No" << std::endl;
}
return 0;
}
|
#include <bits/stdc++.h>
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 REPR(i,n) for(int i=(n)-1;i>=0;i--)
#define ALL(s) (s).begin(), (s).end()
#define so(V) sort(ALL(V))
#define rev(V) reverse(ALL(V))
#define uni(v) v.erase( unique(ALL(v)) , v.end());
typedef long long unsigned int llu;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<vi> vvi;
const double EPS = 1e-9;
const int MOD = 1e9 + 7;
const int INF = (1 << 29);
const ll LINF = 1e18;
const double PI = acos(-1);
template<typename T>
vector<T> make_v(size_t a) { return vector<T>(a); }
template<typename T, typename... Ts>
auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type
fill_v(T& t, const V& v) { t = v; }
template<typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type
fill_v(T& t, const V& v) {
for (auto& e : t) fill_v(e, v);
}
template<class T> bool chmax(T& a, const T& b) {
if (a < b) { a = b; return 1; }
return 0;
}
template<class T> bool chmin(T& a, const T& b) {
if (a > b) { a = b; return 1; }
return 0;
}
struct Edge {
public:
int from, to;
ll C, D;
Edge() {}
Edge(int _from, int _to, ll _C, ll _D) {
from = _from;
to = _to;
C = _C;
D = _D;
}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef pair<ll, int> P;
ll getTime(ll time, Edge e) {
return time + e.C + (e.D / (time + 1ll));
}
ll fastestTime(ll time, Edge e) {
ll mid = sqrt(e.D);
ll best = mid;
for (ll i = max(mid - 100, 0ll);i <= mid + 100;i++) {
if (getTime(best, e) > getTime(i, e)) {
best = i;
}
}
if (best >= time)return getTime(best, e);
else return getTime(time, e);
}
vll dijkstra(int s, Graph G) {//s=>始点
priority_queue<P, vector<P>, greater<P> > que;
vll D(SZ(G), LINF);
D[s] = 0;
que.push(P(0, s));
while (!que.empty()) {
P p = que.top(); que.pop();
int v = p.second;
if (D[v] < p.first)continue;
for (Edge e : G[v]) {
ll fastest = fastestTime(D[v], e);
if (D[e.to] > fastest) {
D[e.to] = fastest;
que.push(P(D[e.to], e.to));
}
}
}
return D;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, M;
cin >> N >> M;
Graph G(N);
REP(i, M) {
int A, B;
cin >> A >> B;
ll C, D;
cin >> C >> D;
A--;B--;
G[A].push_back(Edge(A, B, C, D));
G[B].push_back(Edge(B, A, C, D));
}
vll ans = dijkstra(0, G);
if (ans[N - 1] == LINF) {
cout << -1 << endl;
}
else {
cout << ans[N - 1] << endl;
}
return 0;
} | #include "bits/stdc++.h"
using namespace std;
using ll = long long;
const ll MOD = 998244353;
const double PI = 3.141592653589793238;
int main() {
auto start = chrono::system_clock::now();
ll si, sj;
cin >> si >> sj;
ll nowi = si, nowj = sj;
vector<ll> t(2500);
for (ll i = 0; i < 50; ++i) {
for (ll j = 0; j < 50; ++j) {
cin >> t[50 * i + j];
}
}
vector<ll> p(2500);
for (ll i = 0; i < 50; ++i) for (ll j = 0; j < 50; ++j) cin >> p[50 * i + j];
string lastans = "";
ll lastpnt = 0;
map<ll, bool> used;
string ans = "";
while (true) {
ll id = 50 * nowi + nowj;
ll maxi = -1;
used[t[id]] = true;
char togo = ' ';
if (nowj != 0 && !used[t[id - 1]] && maxi < p[id - 1]) {
togo = 'L';
maxi = p[id - 1];
}
if (nowj != 49 && !used[t[id + 1]] && maxi < p[id + 1]) {
togo = 'R';
maxi = p[id + 1];
}
if (nowi != 0 && !used[t[id - 50]] && maxi < p[id - 50]) {
togo = 'U';
maxi = p[id - 50];
}
if (nowi != 49 && !used[t[id + 50]] && maxi < p[id + 50]) {
togo = 'D';
maxi = p[id + 50];
}
if (togo != ' ')ans += togo;
switch (togo)
{
case 'L': --nowj;
break;
case 'R': ++nowj;
break;
case 'U': --nowi;
break;
case 'D': ++nowi;
break;
default:
goto exit;
}
}
exit:;
nowi = si, nowj = sj;
lastans = ans;
ll now = 50 * nowi + nowj;
lastpnt += p[now];
for (ll i = 0; i < ans.size(); ++i) {
switch (ans[i]) {
case 'L': --nowj;
break;
case 'R': ++nowj;
break;
case 'U': --nowi;
break;
case 'D': ++nowi;
break;
}
lastpnt += p[now];
}
string sample = "LRUD";
random_device rnd;
mt19937 mt(rnd());
uniform_int_distribution<> cha(0, 3), rand(1, 1000);
auto dur = chrono::system_clock::now() - start;
while (chrono::duration_cast<std::chrono::milliseconds>(dur).count() < 1990) {
ans = lastans;
uniform_int_distribution<> change(0, ans.size());
ll ch = change(mt), at = cha(mt), r = rand(mt);
if (ch == ans.size()) ans.push_back(sample[at]);
else ans[ch] = sample[at];
nowi = si, nowj = sj;
now = 50 * nowi + nowj;
map<ll, bool> used;
used[t[now]] = true;
ll pnt = p[now];
for (ll i = 0; i < ans.size(); ++i) {
switch (ans[i]) {
case 'L': --nowj;
break;
case 'R': ++nowj;
break;
case 'U': --nowi;
break;
case 'D': ++nowi;
break;
}
if (nowi < 0 || 49 < nowi || nowj < 0 || 49 < nowj) {
pnt = -1;
break;
}
now = 50 * nowi + nowj;
if (used[t[now]]) {
pnt = -1;
break;
}
used[t[now]] = true;
pnt += p[now];
}
if (pnt >= lastpnt) {
lastpnt = pnt;
lastans = ans;
}
else if (pnt != -1 && r <= 125) {
lastpnt = pnt;
lastans = ans;
}
dur = chrono::system_clock::now() - start;
}
cout << lastans << "\n";
} |
#include<bits/stdc++.h>
#include<string>
using namespace std;
#define ll long long int
#define ld long double
#define pb push_back
#define all(v) v.begin(),v.end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
#define deb(x) cout<< #x << '=' << x <<endl
#define MOD 1000000007
vector<ll> cnt(10 , 0) , cnt1(10 , 0);
ll p(ll a , ll n){
if(n == 0){
return 1;
}
ll res = p(a*a , n / 2);
if(n % 2){
res *= a;
}
return res;
}
ll calc(string b){
ll res = 0;
for(ll i = 1; i <= 9; i++){
res += (i * p(10LL , cnt1[i]));
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll k;
cin>>k;
string s , t;
cin>>s>>t;
for(int i = 0; i < 4; i++){
cnt[s[i] - '0']++;
cnt[t[i] - '0']++;
}
ld m = 9*k - 8;
ld total = m*(m - 1);
ld ans = 0;
for(int i = 1; i <= 9; i++){
for(int j = 1; j <= 9; j++){
for(int l = 0; l < 10; l++){
cnt1[l] = 0;
}
cnt1[i]++;
for(int l = 0; l < 4; l++){
cnt1[s[l] - '0']++;
}
ll tak = calc(s);
for(int l = 0; l < 10; l++){
cnt1[l] = 0;
}
cnt1[j]++;
for(int l = 0; l < 4; l++){
cnt1[t[l] - '0']++;
}
ll aoki = calc(t);
if(tak > aoki){
if(i == j){
ans += ((k - cnt[i]) * (k - cnt[j] - 1));
}else{
ans += ((k - cnt[i]) * (k - cnt[j]));
}
}
}
}
ans /= total;
cout<<fixed<<setprecision(16)<<ans<<"\n";
return 0;
} | #include <iostream>
#include <vector>
#include <numeric>
using namespace std;
using ll = int64_t;
ll score(string S){
vector<ll> cnt(10);
iota(cnt.begin(), cnt.end(), 0);
for(char c : S) cnt[c - '0'] *= 10;
return accumulate(cnt.begin(), cnt.end(), 0);
}
int main(){
ll K;
string S, T;
cin >> K >> S >> T;
vector<ll> cnt(10, K);
for(auto c : S + T) cnt[c - '0']--;
ll win = 0;
for(ll x = 1; x <= 9; x++) for(ll y = 1; y <= 9; y++){
S.back() = '0' + x;
T.back() = '0' + y;
if(score(S) <= score(T)) continue;
win += cnt[x] * (cnt[y] - (x == y));
}
const ll rem = 9 * K - 8;
cout << double(win) / rem / (rem - 1) << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PP;
//#define MOD 1000000007
#define MOD 998244353
#define INF 2305843009213693951
//#define INF 810114514
#define PI 3.141592653589
#define setdouble setprecision
#define REP(i,n) for(ll i=0;i<(n);++i)
#define OREP(i,n) for(ll i=1;i<=(n);++i)
#define RREP(i,n) for(ll i=(n)-1;i>=0;--i)
#define all1(i) begin(i),end(i)
#define GOODBYE do { cout << "-1" << endl; return 0; } while (false)
#define MM <<" "<<
#define Endl endl
#define debug true
#define debug2 false
int main(void){
ll N;
vector<ll> A;
cin >> N;
REP(i,N){
ll a;
cin >> a;
A.push_back(a);
}
ll Ans=0;
for(ll i=0;i<N;i++){
ll mn = INF;
for(ll j=i;j<N;j++){
mn = min(mn,A[j]);
Ans = max(Ans,mn*(j-i+1));
}
}
cout << Ans << endl;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define rrep1(i,n) for(int i=(n);i>0;i--)
#define fore(i_in,a) for (auto& i_in: a)
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fio() cin.tie(nullptr);ios::sync_with_stdio(false);
#define DEBUG_CTN(v) cerr<<#v<<":";for(auto itr=v.begin();itr!=v.end();itr++) cerr<<" "<<*itr; cerr<<endl
template<class T> bool chmax(T &a, const T &b) {if (a<b) { a = b; return true; } return false;}
template<class T> bool chmin(T &a, const T &b) {if (a>b) { a = b; return true; } return false;}
template<class T> void print(const T &t) { cout << t << "\n"; }
const ll INF = 1LL << 62;
const int iINF = 1 << 30;
int n;
double d,h,x,y;
int main() {
fio(); cin>>n>>d>>h;
double cln=99999;
rep(i,n){cin>>x>>y; chmin(cln,(h-y)/(d-x));}
double ans = max(0.0,h-cln*d);
cout << fixed << setprecision(12) << ans << "\n";
}
//9min |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
static const ll mod=1000000007;
int main(){
int n;
cin>>n;
ll a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
sort(a,a+n);
ll ans=1;
for(int i=0;i<n-1;i++){
ans*=(a[i+1]-a[i]+1);
ans%=mod;
}
ans*=(a[0]+1);
ans%=mod;
cout<<ans<<endl;
return 0;
}
| #include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<utility>
using namespace std;
#define int long long
#define endl "\n"
constexpr long long INF = (long long)1e18;
constexpr long long MOD = 998244353;
struct fast_io {
fast_io(){
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
};
} fio;
class binomial_coefficients {
long long MAX_VAL;
vector<long long> fac, mmi;
public:
binomial_coefficients(){
}
binomial_coefficients(long long num){
init(num);
}
~binomial_coefficients(){
}
void init(long long num){
MAX_VAL = num+1;
fac.resize(MAX_VAL);
mmi.resize(MAX_VAL);
factorial_mod();
modular_multiplicatibe_inverse();
}
void factorial_mod(){
fac[0] = 1;
for(long long i = 1; i < MAX_VAL; fac[i] %= MOD, i++)
fac[i] = fac[i - 1] * (i % MOD);
}
long long power(long long x, long long n){
long long ans = 1;
for(;n;n >>= 1, x *= x, ans %= MOD, x %= MOD) if(n&1)ans*=x;
return ans % MOD;
}
void exgcd(long long a, long long b, long long &x, long long &y){
if(b == 0){
x = 1;
y = 0;
return ;
}
exgcd(b, a % b, y, x);
y -= a / b * x;
}
void modular_multiplicatibe_inverse(){
long long x, y;
exgcd(fac[MAX_VAL - 1], MOD, x, y);
mmi[MAX_VAL-1] = (x%MOD + MOD) % MOD;
// mmi[MAX_VAL-1] = power(fac[MAX_VAL-1], MOD-2);
for(long long i = MAX_VAL - 2; i >= 0; mmi[i]%=MOD, i--)
mmi[i] = mmi[i + 1] * ((i + 1) % MOD);
}
long long combination(long long n, long long r){
return n < r ? 0 :fac[n] * (mmi[r] * mmi[n-r] % MOD) % MOD;
}
};
signed main(){
cout<<fixed<<setprecision(10);
binomial_coefficients BC;
int N, M;
static int dp[5100][20];
int ans = 0;
cin>>N>>M;
BC.init(N + M);
dp[0][0] = 1;
for(int i = 0; i < 16; i++){
int X = 1, Y = (1<<(i+1));
for(int j = 0; j <= M; j += X){
for(int k = 0, l = 0; j+k <= M; k += Y, l += 2){
dp[j+k][i+1] += dp[j][i] * BC.combination(N, l) % MOD;
dp[j+k][i+1] %= MOD;
}
}
}
cout<<dp[M][16]<<endl;
return 0;
} |
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <cassert>
#include <iomanip>
using namespace std;
#define pb push_back
#define ff first
#define ss second
#define endl '\n'
#define fast ios::sync_with_stdio(false); cin.tie(NULL);
#define trace(x) cerr << #x << " = " << x << endl;
// memset(a,0xc0,sizeof(a)); // initializing with negative infinity
// memset(a,0x3f,sizeof(a)); // initializing with positive infinity
/////////////////////////////////////////////
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
///////////////////////////////////////////////////////////////////////
#define int long long
const int N = 1010;
int dp[N][N];
void solve(){
int n , m;
cin>>n>>m;
vector<int> a(n) , b(m);
for(int i = 0 ; i < n ; ++i)
cin>>a[i];
for(int i = 0 ; i < m ; ++i)
cin>>b[i];
memset(dp , 0x3f , sizeof dp);
for(int i = 0 ; i <= n ; ++i){
for(int j = 0 ; j <= m ; ++j){
if(i==0 && j==0)
dp[i][j] = 0;
else if(i==0)
dp[i][j] = j;
else if(j==0)
dp[i][j] = i;
else{
dp[i][j] = dp[i - 1][j - 1] + (a[i - 1]!=b[j - 1]);
int val = 1 + min(dp[i - 1][j] , dp[i][j - 1]);
dp[i][j] = min(dp[i][j] , val);
}
}
}
cout<<dp[n][m]<<endl;
}
signed main(){
fast;
int t = 1;
//cin>>t;
while(t--)
solve();
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
#define rep(i,n) for(int i=0;i<int(n);i++)
#define rep1(i,n) for(int i=1;i<=int(n);i++)
#define per(i,n) for(int i=int(n)-1;i>=0;i--)
#define per1(i,n) for(int i=int(n);i>0;i--)
#define all(c) c.begin(),c.end()
#define si(x) int(x.size())
#define pb emplace_back
#define fs first
#define sc second
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
template<class T,class U> void chmax(T& x, U y){if(x<y) x=y;}
template<class T,class U> void chmin(T& x, U y){if(y<x) x=y;}
template<class T> void mkuni(V<T>& v){sort(all(v));v.erase(unique(all(v)),v.end());}
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
o<<"{";
for(const T& v:vc) o<<v<<",";
o<<"}";
return o;
}
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }
#ifdef LOCAL
#define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
void dmpr(ostream& os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
os<<t<<" ~ ";
dmpr(os,args...);
}
#define shows(...) cerr << "LINE" << __LINE__ << " : ";dmpr(cerr,##__VA_ARGS__)
#define dump(x) cerr << "LINE" << __LINE__ << " : " << #x << " = {"; \
for(auto v: x) cerr << v << ","; cerr << "}" << endl;
#else
#define show(x) void(0)
#define dump(x) void(0)
#define shows(...) void(0)
#endif
int main(){
cin.tie(0);
ios::sync_with_stdio(false); //DON'T USE scanf/printf/puts !!
cout << fixed << setprecision(20);
int H,W; cin >> H >> W;
V<string> s(H); rep(i,H) cin >> s[i];
int ans = 0;
V<int> dx={0,1},dy={1,0};
rep(i,H) rep(j,W) if(s[i][j]=='.'){
rep(d,2){
int x=i+dx[d],y=j+dy[d];
if(0<=x&&x<H&&0<=y&&y<W&&s[x][y]=='.') ans++;
}
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <algorithm>
#include <bitset>
#include <math.h>
#include <utility>
typedef long long ll;
typedef unsigned long long ull;
#define vi std::vector<int>
#define vl std::vector<ll>
#define floop(n) for(int i = 0; i < n; ++i)
#define print(x) std::cout << x << std::endl;
const ll INF = 1e18;
const ll MOD = 1e9+7;
ll gcd(ll a, ll b) { return a == 0 ? b : gcd(b%a, a); }
ll mod(ll a) { return a % MOD < 0 ? (a % MOD) + MOD : a % MOD; }
ll mod_add(ll a, ll b) { return (a%MOD + b%MOD) % MOD; }
ll mod_sub(ll a, ll b) { return mod((a%MOD - b%MOD) % MOD); }
ll mod_mul(ll a, ll b) { return (a%MOD * b%MOD) % MOD; }
ll mod_pow(ll a, ll b)
{
if(b == 0) return 1;
ll p = mod_pow(a, b/2) % MOD;
p = mod_mul(p, p);
return (b%2 == 0) ? p : mod_mul(a, p);
}
ll mod_inv(ll a) { return mod_pow(a, MOD-2); }
ll mod_div(ll a, ll b) { return a%MOD * mod_inv(b) % MOD; }
ll mod_summation(ll N) { return mod_div(mod_mul(N, mod_add(N, 1)), 2); }
int main()
{
char a, b, c;
std::cin >> a >> b >> c;
print((a == b && b == c ? "Won" : "Lost"))
return 0;
} |
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;
//#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);
ll mod_pow(ll x, ll n, ll m = mod) {
if (n < 0) {
ll res = mod_pow(x, -n, m);
return mod_pow(res, m - 2, m);
}
if (abs(x) >= m)x %= m;
if (x < 0)x += m;
ll res = 1;
while (n) {
if (n & 1)res = res * x % m;
x = x * x % m; n >>= 1;
}
return res;
}
struct modint {
ll n;
modint() :n(0) { ; }
modint(ll m) :n(m) {
if (n >= mod)n %= mod;
else if (n < 0)n = (n % mod + mod) % mod;
}
operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
if (n == 0)return modint(1);
modint res = (a * a) ^ (n / 2);
if (n % 2)res = res * a;
return res;
}
ll inv(ll a, ll p) {
return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f() {
fact[0] = modint(1);
for (int i = 0; i < max_n - 1; i++) {
fact[i + 1] = fact[i] * modint(i + 1);
}
factinv[max_n - 1] = modint(1) / fact[max_n - 1];
for (int i = max_n - 2; i >= 0; i--) {
factinv[i] = factinv[i + 1] * modint(i + 1);
}
}
modint comb(int a, int b) {
if (a < 0 || b < 0 || a < b)return 0;
return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
if (a < 0 || b < 0 || a < b)return 0;
return fact[a] * factinv[a - b];
}
void solve() {
int a, b, c; cin >> a >> b >> c;
cout << max({ a + b,b + c,c + a }) << "\n";
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
//cout << fixed << setprecision(10);
//init_f();
//init();
//expr();
//int t; cin >> t; rep(i, t)
solve();
return 0;
}
|
//Bismillah Hir Rehmanir Rahim
//Allah knows best
using namespace std;
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#define endl "\n"
#define mini -10000000000000000
#define ios ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define f(i, a, n) for( i=a; i<n;i++)
#define ll long long
#define pi 3.141592653589793
#define vi vector<int>
#define si set<int>
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define rr return 0
#define ff first
#define in insert
#define ss second
#define setbits(x) __builtin_popcountll(x)
#define zrbits(x) __builtin_ctzll(x)
#define deb(x) cout << #x << " = " << x << endl
#define set_bit(x, idx) x = x|1LL<<idx
#define check_bit(x, idx) min(x&(1LL<<idx),1LL)
#define toggle_bit(x, idx) x = x^(1LL<<idx)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define multi_ordered_set tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef tree<int, null_type, greater<int>, rb_tree_tag, tree_order_statistics_node_update> rordered_set;
int main()
{
ios;
#ifdef DEBUG
freopen("input.txt", "r", stdin);
#endif
ll t;
cin>>t;
while(t--){
ll n,i,j,k,m;
cin>>n;
ll a[n];
ll o=0;
f(i,0,n)
{
cin>>a[i];
if(a[i]>1)
o++;
}
if(n%2)
cout<<"Second"<<endl;
else
{
bool f=0;
sort(a,a+n);
for(i=0;i<n-1;i+=2)
{
if(a[i]!=a[i+1])
{
f=1;
}
}
if(f)
cout<<"First"<<endl;
else cout<<"Second"<<endl;
}
}
rr;
} | //#pragma GCC optimize("Ofast")
//#pragma GCC target ("sse4")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;
//#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acosl(-1.0);
ll mod_pow(ll x, ll n, ll m = mod) {
ll res = 1;
while (n) {
if (n & 1)res = res * x % m;
x = x * x % m; n >>= 1;
}
return res;
}
struct modint {
ll n;
modint() :n(0) { ; }
modint(ll m) :n(m) {
if (n >= mod)n %= mod;
else if (n < 0)n = (n % mod + mod) % mod;
}
operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
if (n == 0)return modint(1);
modint res = (a * a) ^ (n / 2);
if (n % 2)res = res * a;
return res;
}
ll inv(ll a, ll p) {
return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
const int max_n = 1 << 2;
modint fact[max_n], factinv[max_n];
void init_f() {
fact[0] = modint(1);
for (int i = 0; i < max_n - 1; i++) {
fact[i + 1] = fact[i] * modint(i + 1);
}
factinv[max_n - 1] = modint(1) / fact[max_n - 1];
for (int i = max_n - 2; i >= 0; i--) {
factinv[i] = factinv[i + 1] * modint(i + 1);
}
}
modint comb(int a, int b) {
if (a < 0 || b < 0 || a < b)return 0;
return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
if (a < 0 || b < 0 || a < b)return 0;
return fact[a] * factinv[a - b];
}
void solve() {
int n; cin >> n; vector<int> a(n), b(n);
rep(i, n) {
a[i] = (2 * i)%n;
b[i] = (2 * i + 1) % n;
}
rep(i, n)cout << a[i] + 1 << " " << b[i] + 1 << "\n";
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
//cout << fixed << setprecision(15);
//init_f();
//init();
//expr();
//int t; cin >> t; rep(i, t)
solve();
return 0;
} |
#include <cstdio>
#include <map>
#include <vector>
using namespace std;
int Max(int x, int y) {return x > y ? x : y;}
const int MAXN = 2 * 1e5 + 5;
int c[MAXN], fa[MAXN], dep[MAXN];
map<int, int> ans[MAXN];
vector<int> num[MAXN];
bool flag[MAXN];
void Make_Set(int n) {
for(int i = 1; i <= n; i++) {
fa[i] = i;
dep[i] = 0;
ans[i][c[i]] = 1;
num[i].push_back(c[i]);
}
}
int Find_Set(int x) {
if(fa[x] != x)
return fa[x] = Find_Set(fa[x]);
return x;
}
int main() {
int n, q, ma = 0;
scanf ("%d %d", &n, &q);
for(int i = 1; i <= n; i++)
scanf ("%d", &c[i]);
Make_Set(n);
for(int i = 1; i <= q; i++) {
int opt, x, y;
scanf ("%d %d %d", &opt, &x, &y);
if(opt == 1) {
int u = Find_Set(x), v = Find_Set(y);
if(u != v) {
if(dep[u] > dep[v]) {
for(int j = 0; j < num[v].size(); j++) {
bool flag = false;
if(ans[u][num[v][j]] == 0)
flag = true;
ans[u][num[v][j]] += ans[v][num[v][j]];
if(ans[u][num[v][j]] > 0 && flag)
num[u].push_back(num[v][j]);
}
fa[v] = u;
}
else if(dep[u] < dep[v]) {
for(int j = 0; j < num[u].size(); j++) {
bool flag = false;
if(ans[v][num[u][j]] == 0)
flag = true;
ans[v][num[u][j]] += ans[u][num[u][j]];
if(ans[v][num[u][j]] > 0 && flag)
num[v].push_back(num[u][j]);
}
fa[u] = v;
}
else {
for(int j = 0; j < num[v].size(); j++) {
bool flag = false;
if(ans[u][num[v][j]] == 0)
flag = true;
ans[u][num[v][j]] += ans[v][num[v][j]];
if(ans[u][num[v][j]] > 0 && flag)
num[u].push_back(num[v][j]);
}
fa[v] = u;
if(dep[u] == dep[v])
dep[u]++;
}
}
}
else
printf("%d\n", ans[Find_Set(x)][y]);
}
return 0;
} | //dp d[i][j]表示是i到j的最短回文串长度
#include<bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define vvi vector<vi>
#define rep(i,l,r) for(int i=l;i<r;i++)
#define pii pair<int,int>
#define inf 0x3f3f3f3f
typedef long long ll;
const int maxn=1005;
vector<int> g[maxn][26];
int d[maxn][maxn];
int n,m;
void solve()
{
memset(d,-1,sizeof(d));
cin>>n>>m;
queue<pii> q;
rep(i,1,n+1){
d[i][i]=0;
q.emplace(i,i);
}
while(m--){
int x,y,z;
char ch;
cin>>x>>y>>ch;
z=ch-'a';
g[x][z].emplace_back(y);
g[y][z].emplace_back(x);
if(x!=y){
d[x][y]=d[y][x]=1;
q.emplace(x,y);
}
}
while(!q.empty()){
auto [u,v]=q.front();
q.pop();
for(int i=0;i<26;i++){
if(!g[u][i].empty()&&!g[v][i].empty()){
int len1=g[u][i].size(),len2=g[v][i].size();
for(int j=0;j<len1;j++){
for(int k=0;k<len2;k++){
int u1=g[u][i][j],v1=g[v][i][k];
if(d[u1][v1]==-1){
d[u1][v1]=d[v1][u1]=d[u][v]+2;
q.emplace(u1,v1);
}
}
}
}
}
}
cout<<d[1][n]<<endl;
}
int main()
{
solve();
return 0;
}
|
#include<iostream>
#include<numeric>
using namespace std;
const int VAL = 105;
const int BUF = 5005;
const int MOD = 998244353;
int fact[VAL];
void makeTable() {
fact[0] = 1;
for (int i = 1; i < VAL; ++i) {
fact[i] = 1LL * i * fact[i - 1] % MOD;
}
}
int nVal;
int val[VAL];
void read() {
cin >> nVal;
for (int i = 0; i < nVal; ++i) {
cin >> val[i];
}
}
void work() {
int total = accumulate(val, val + nVal, 0);
if (total % 2 != 0) {
cout << 0 << endl;
return;
}
int dp[VAL][BUF] = {};
dp[0][0] = 1;
for (int loop = 0; loop < nVal; ++loop) {
for (int nPick = loop; nPick >= 0; --nPick) {
for (int sum = 0; sum + val[loop] <= total / 2; ++sum) {
if (!dp[nPick][sum]) continue;
dp[nPick + 1][sum + val[loop]] = (dp[nPick + 1][sum + val[loop]] + dp[nPick][sum]) % MOD;
}
}
}
int ans = 0;
for (int nPick = 0; nPick <= nVal; ++nPick) {
ans = (ans + 1LL * dp[nPick][total / 2] * fact[nPick] % MOD * fact[nVal - nPick]) % MOD;
}
cout << ans << endl;
}
int main() {
makeTable();
read();
work();
return 0;
}
| //#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define re register int
#define ll long long
#define I inline int
#define V inline void
#define LL inline ll
#define FOR(i,a,b) for(re i=(a),i##i=(b);i<=i##i;++i)
#define ROF(i,a,b) for(re i=(a),i##i=(b);i>=i##i;--i)
#define gc (_ft==_fs&&(_ft=(_fs=buf)+fread(buf,1,1<<20,stdin),_ft==_fs))?0:*_fs++
//#define gc getchar()
using namespace std;
const int N=2e4+10,M=110,mo=998244353;
char buf[1<<20],*_fs,*_ft;
LL read(){
ll w=0,p=0; char ch=gc;
while(!isdigit(ch)) w=ch=='-',ch=gc;
while(isdigit(ch)) p=p*10+ch-'0',ch=gc;
return w?-p:p;
}
int n,a[M],tot,fl;
ll ans,dp[M][N],f[M],fc[M],fv[M];
LL Pow(ll x,ll y){ ll as=1; for(;y;y>>=1,x=x*x%mo) if(y&1) as=as*x%mo; return as;}
LL ck(ll x){ return x-=mo,x+=x>>63&mo; }
V ipt(){ n=read(); FOR(i,1,n) a[i]=read(),tot+=a[i];}
V ycl(){
fc[0]=1;
FOR(i,1,n) fc[i]=fc[i-1]*i%mo;
return ;
}
V sol(){
if(tot&1) return ;
dp[0][0]=1,tot>>=1;
FOR(i,1,n) ROF(j,i-1,0) ROF(k,tot,0){
dp[j+1][k+a[i]]=ck(dp[j+1][k+a[i]]+dp[j][k]);
}
FOR(i,0,n) f[i]=dp[i][tot];
FOR(i,0,n){
if(2*i==n) ans=(ans+f[i]*fc[i]%mo*fc[i]%mo)%mo;
else ans=(ans+f[i]%mo*fc[i]%mo*fc[n-i]%mo)%mo;
}
return ;
}
V opt(){ cout<<ans; }
int main(){
ipt();
ycl();
sol();
opt();
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
int main()
{
string a;
cin>>a;
for(int i=0;i<a.size();i++)
{
if(a[i]=='6') a[i]='9';
else if(a[i]=='9') a[i]='6';
}
for(int i=a.size()-1;i>=0;i--) cout<<a[i];
return 0;
} | #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 mod2 998244353
#define pi acos(-1)
#define all(v) v.begin(),v.end()
#define vi vector<int>
#define vl vector<ll>
int dx[4] = {0,-1,0,1};
int dy[4] = {-1,0,1,0};
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;
}
int main(){
vi n(4);
rep(i,4) cin >> n[i];
sort(all(n));
cout << n[0] <<endl;
return 0;
}
|
#include <bits/stdc++.h>
#define VA_NUM_ARGS(...) VA_NUM_ARGS_IMPL_((0,__VA_ARGS__, 5,4,3,2,1))
#define VA_NUM_ARGS_IMPL_(tuple) VA_NUM_ARGS_IMPL tuple
#define VA_NUM_ARGS_IMPL(_0,_1,_2,_3,_4,_5,N,...) N
#define macro_dispatcher(macro,...) macro_dispatcher_(macro, VA_NUM_ARGS(__VA_ARGS__))
#define macro_dispatcher_(macro,nargs) macro_dispatcher__(macro, nargs)
#define macro_dispatcher__(macro,nargs) macro_dispatcher___(macro, nargs)
#define macro_dispatcher___(macro,nargs) macro ## nargs
#define DBN(...) macro_dispatcher(DBN, __VA_ARGS__)(__VA_ARGS__)
#define DBN1(a) cerr<<#a<<"="<<(a)<<"\n"
#define DBN2(a,b) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBN3(a,b,c) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define DBN4(a,b,c,d) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define DBN5(a,b,c,d,e) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"
#define endl '\n'
#define DBN1D(x) cerr<<#x<<endl;for(auto _:x){cerr<<_<<" ";}cerr<<endl
#define DBN2D(x) cerr<<#x<<endl;for(auto _:x){for(auto __:_){cerr<<__<<" ";}cerr<<endl;}cerr<<"------"<<endl
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define mp(x,y) make_pair(x,y)
#define mpp(w,x,y,z) mp(mp(w,x),mp(y,z))
using namespace std;
typedef long long loli;
typedef vector<loli> vl;
typedef vector<vl> vvl;
typedef pair<loli,loli> pll;
typedef pair<pll,pll> pllll;
void solve(){
loli n,k;
cin>>n>>k;
map<loli,loli> mp;
for(loli i=0;i<n;++i){
loli x,y;
cin>>x>>y;
mp[x]+=y;
}
vector<pll> f(1);
f[0]=mp(0,k);
for(auto&x:mp){
f.push_back(x);
}
// // for(int i=1;i<=n;++i){
// // int x,y;
// // cin>>x>>y;
// // f[i]={x,y};
// // }
// // sort(f.begin(),f.end());
// for(int i=0;i<=n;++i){
// }
loli tmp=0;
for(loli i=0;i<=f.size();++i){
f[i].second+=tmp;
tmp=f[i].second;
}
// for(auto x:f){
// cout<<x.first<<" "<<x.second<<endl;
// }
for(loli i=0;i<f.size();++i){
if(f[i].second<f[i+1].first){
cout<<f[i].second<<endl;
return;
}
}
cout<<f.back().second<<endl;
}
int main(){
sync;
solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
using ll = long long;
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define _rrep(i,n) rrepi(i,0,n)
#define rrepi(i,a,b) for(int i=int(b)-1;i>=int(a);--i)
#define rrep(...) _overload3(__VA_ARGS__,rrepi,_rrep,)(__VA_ARGS__)
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; }
void Main()
{
int n;
ll k;
cin >> n >> k;
vector<pair<ll, ll>> v(n);
rep(i, n){
ll a, b;
cin >> a >> b;
v[i] = {a, b};
}
sort(v.begin(), v.end());
rep(i, n){
auto [a, b] = v[i];
if (a > k) {
cout << k << endl;
return;
} else{
k += b;
}
}
cout << k << endl;
}
int main(int argc, char **argv)
{
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
// int t; cin >> t; rep(i, t)
Main();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vecI = vector<int>;
using vec2I = vector<vecI>;
using vec3I = vector<vec2I>;
ll mod{(ll)1e9 + 7};
double pi = 2 * acos(0.0);
//Check for long
void solve()
{
ll n;
cin >> n;
ll ct{}, minus{1LL}, i{1000LL};
int mul{};
for(; i <= n; i *= 1000)
{
ct += (i - minus) * mul;
minus *= 1000;
mul++;
}
ct += (n - minus + 1) * mul;
cout << ct;
}
int main()
{
//Ignore from
//Godspeed cin to cout
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
//for getting input from input text
freopen("input.txt", "r", stdin);
//for writing output to output.txt
freopen("output.txt", "w", stdout);
#endif
// Ignore to
// int t;
// cin >> t;
// while(t--)
solve();
return 0;
}
//Clock
// auto start = chrono::steady_clock::now();
// auto end = chrono::steady_clock::now();
// auto diff = end - start;
// cerr << chrono::duration <double, milli> (diff).count() << " ms" << endl;
//Napolean Cake
// void solve()
// {
// int n;
// cin >> n;
// vecI vec(n);
// for(int &x : vec)
// cin >> x;
// vecI diffArr(n + 1);
// for(int i{}; i < n; i++)
// {
// if(vec[i] == 0)
// continue;
// diffArr[max(i - vec[i] + 1, 0)]++;
// diffArr[min(i + 1, n)]--;
// }
// int sum{};
// for(int i{}; i < n; i++)
// {
// sum += diffArr[i];
// diffArr[i] = sum;
// if(diffArr[i] == 0)
// cout << 0;
// else
// cout << 1;
// cout << ' ';
// }
// cout << '\n';
// }
//Alexey & Train
// void solve()
// {
// int n;
// cin >> n;
// vector<pair<int, int>> vec(n);
// for(auto &it : vec)
// cin >> it.first >> it.second;
// vector<int> delay(n);
// for(auto &it : delay)
// cin >> it;
// int cur{};
// int prev{};
// for(int i{}; i < n; i++)
// {
// cur += vec[i].first - prev + delay[i];
// //cerr << cur << ' ';
// if(i == n - 1)
// break;
// int min_time{(int)ceil((vec[i].second - vec[i].first) / 2.0)};
// cur = max(cur + min_time, vec[i].second);
// prev = vec[i].second;
// }
// cout << cur << '\n';
// } | #include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <utility>
#include <queue>
#include <map>
#include <assert.h>
#include <stack>
#include <string>
#include <ctime>
#include <chrono>
#include <random>
#define int long long int
using namespace std;
const int MAX = 1e4;
void solve()
{
int n;
cin >> n;
int cnt = 3;
cout << 6 << ' ' << 10 << ' ' << 15 << ' ';
for (int j = 16; j <= MAX; j++)
{
if (cnt == n) break;
if (j % 6 == 0 || j % 10 == 0 || j % 15 == 0)
{
cnt++;
cout << j << ' ';
}
}
cout << '\n';
return;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(NULL); cout.tie(NULL);
int t;
//cin >> t;
t=1;
while (t--)
{
solve();
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main(){
ios::sync_with_stdio(0),cin.tie(0);
int a,b,c,d;
cin >> a >> b >> c >> d;
cout << min({a,b,c,d});
} | #include <iostream>
using namespace std;
int main() {
int arr[4];
for(int i=0; i<4; i++) cin >> arr[i];
int sum=arr[0];
for(int i=0; i<4; i++) sum = arr[i]<sum? arr[i]: sum;
cout << endl << sum;
} |
#include <bits/stdc++.h>
using namespace std;
#define f0r(a, b) for (long long a = 0; a < b; a++)
#define f1r(a, b, c) for (long long a = b; a < c; a++)
#define f0rd(a, b) for (long long a = b; a >= 0; a--)
#define f1rd(a, b, c) for (long long a = b; a >= c; a--)
#define ms(arr, v) memset(arr, v, sizeof(arr))
#define mp(a, b) make_pair(a, b)
#define pb push_back
#define f first
#define s second
#define ao(a, n) {for (int ele = 0; ele < n; ele++) { if (ele) cout << " "; cout << a[ele]; } cout << '\n';}
typedef long long ll;
typedef double ld;
typedef long double lld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
const ll inf = 1e16;
const int MOD = 1e9 + 7;
typedef decay<decltype(MOD)>::type T;
struct mi {
T val;
explicit operator T() const { return val; }
mi() { val = 0; }
mi(const long long& v) {
val = (-MOD <= v && v <= MOD) ? v : v % MOD;
if (val < 0) val += MOD; }
friend ostream& operator<<(ostream& os, const mi& a) { return os << a.val; }
friend bool operator==(const mi& a, const mi& b) { return a.val == b.val; }
friend bool operator!=(const mi& a, const mi& b) { return !(a == b); }
friend bool operator<(const mi& a, const mi& b) { return a.val < b.val; }
mi operator-() const { return mi(-val); }
mi& operator+=(const mi& m) {
if ((val += m.val) >= MOD) val -= MOD;
return *this; }
mi& operator-=(const mi& m) {
if ((val -= m.val) < 0) val += MOD;
return *this; }
mi& operator*=(const mi& m) { val = (long long) val * m.val % MOD;
return *this; }
friend mi pow(mi a, long long p) {
mi ans = 1; assert(p >= 0);
for (; p; p /= 2, a *= a) if (p & 1) ans *= a;
return ans; }
friend mi inv(const mi& a) { assert(a != 0); return pow(a, MOD - 2); }
mi& operator/=(const mi& m) { return (*this) *= inv(m); }
friend mi operator+(mi a, const mi& b) { return a += b; }
friend mi operator-(mi a, const mi& b) { return a -= b; }
friend mi operator*(mi a, const mi& b) { return a *= b; }
friend mi operator/(mi a, const mi& b) { return a /= b; }
};
ll trade[200005];
vl graph[200005];
ll mx[200005];
ll vis[200005];
ll rec(ll v) {
if (vis[v]) {
return max(trade[v], mx[v]);
}
vis[v] = 1;
ll m = -inf;
for (ll u: graph[v]) {
m = max(m, rec(u));
}
//cout << v << " " << m << " " << trade[v] << endl;
mx[v] = m;
return max(mx[v], trade[v]);
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
ll n, m; cin >> n >> m;
f0r(i, n) {
cin >> trade[i];
}
f0r(i, m) {
ll x, y; cin >> x >> y;
x--; y--;
graph[x].pb(y);
}
f0r(i, n) {
sort(graph[i].begin(), graph[i].end());
reverse(graph[i].begin(), graph[i].end());
}
ms(vis, 0);
f0r(i, n) {
if (!vis[i]) {
rec(i);
}
}
ll ret = -inf;
f0r(i, n) {
ret = max(ret, mx[i] - trade[i]);
}
cout << ret << endl;
}
| #include<bits/stdc++.h> //Written by ThiDaiLoc
using namespace std; //Team Three Wolves
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef pair<ll,pll> trp;
typedef vector<ll> vi;
typedef vector<pll> vp;
#define fu(i,a,b) for(ll i=a;i<=b;i++)
#define f1(i,n) for(ll i=1;i<=n;i++)
#define fs(i,s) for(ll i=0;i+1<=s.length();i++)
#define fd(i,b,a) for(ll i=b;i>=a;i--)
#define fuv(i,a) for(ll i=0;i<a.size();i++)
#define fdv(i,a) for(ll i=(ll)a.size()-1;i>=0;i--)
#define ms(a,x) memset(a, x, sizeof a)
#define prec(n) fixed<<setprecision(n)
#define uni(a) (a).erase(unique(all(a)), (a).end())
#define pb(i) push_back(i)
#define pob pop_back()
#define sc(a) cin>>a
#define sc2(a,b) cin>>a>>b
#define sc3(a,b,c) cin>>a>>b>>c
#define pr(a) cout<<a<<endl
#define pr2(a,b) cout<<a<<" "<<b<<endl
#define rpr(a) return cout<<a<<endl,0
#define prY cout<<"YES"<<endl
#define prN cout<<"NO"<<endl
#define bit(n,i) (((n)>>(i))&1)
#define lowb(a,n,x) lower_bound(a,a+n,x) -a
#define lowb2(a,x) lower_bound(all(a),x) -a.begin()
#define all(x) (x).begin(), (x).end()
#define sz(a) (ll)a.size()
#define le(s) (ll)s.length()
#define re return
#define mp(a,b) make_pair(a,b)
#define mp3(a,b,c) make_pair(a,make_pair(b,c))
#define se second
#define fi first
#define sse second.second
#define sfi second.first
#define soA sort(A+1,A+1+n)
#define so(A,n) sort(A+1,A+1+n)
#define sov(v) sort(all(v))
#define sovr(v) sort(all(v),greater<ll>())
#define debug(x) cerr << #x << " = " << x << endl
#define INPUT freopen("locin.txt", "r", stdin)
#define OUTPUT freopen("locout.txt", "w", stdout)
inline ll isqrt(ll k) {ll r = sqrt(k) + 1; while (r * r > k) r--; return r;}
inline ll icbrt(ll k) {ll r = cbrt(k) + 1; while (r * r * r > k) r--; return r;}
inline ll mnz(ll& a,ll b){return a=(a>b?b:a);}
inline ll mxz(ll& a,ll b){return a=(a<b?b:a);}
inline string toString(ll n) {stringstream ss; ss << n;return ss.str();}
double const eps = 1e-6;
ll const Base=1e9+7,oo=1e17,MAXN=1e6;
ll A[MAXN+5],dp[MAXN+5];
vi dske[MAXN+5];
ll Solves(){
ll n,m,k,cnt=0,ans=0,x,y,q,c,sum=0,v,t;
// PROCESS IN HERE
sc2(n,m);
f1(i,n)sc(A[i]);
while(m--){
sc2(x,y);
dske[x].pb(y);
}
ms(dp,1);
x=oo;
y=-oo;
ans=-oo;
f1(i,n){
if(dp[i]>1e9);
else mxz(ans,A[i]-dp[i]);
fuv(j,dske[i])mnz(dp[dske[i][j]],min(A[i],dp[i]));
}
pr(ans);
re 0;
// Hack it if you can :)
}
int main(){
//INPUT;
if(fopen("locin.txt", "r"))INPUT;
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
ll test=1;
// sc(test);
fu(T,1,test){
// cout<<"Case #"<<T<<": ";
Solves();
}
} |
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
ll n,m;
cin>>n>>m;
ll x;
set<ll>s;
for(int i=0;i<n;i++)
{
cin>>x;
s.insert(x);
}
for(int i=0;i<m;i++)
{
cin>>x;
if(s.find(x)==s.end())
{
s.insert(x);
}
else
{
s.erase(x);
}
}
for(auto it:s)
{
cout<<it<<" ";
}
}
| #ifdef __LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int,int>;
using PIL = pair<int,ll>;
using PLI = pair<ll,int>;
using PLL = pair<ll,ll>;
using Graph = vector<vector<int>>;
using Cost_Graph = vector<vector<PIL>>;
template<class T> bool chmin(T &a, T b) {if(a>b){a=b;return 1;}return 0;}
template<class T> bool chmax(T &a, T b) {if(a<b){a=b;return 1;}return 0;}
#define REP(i,n) for(int i=0;i<int(n);i++)
#define ROUNDUP(a,b) ((a+b-1)/b)
#define YESNO(T) cout<<(T?"YES":"NO")<<endl
#define yesno(T) cout<<(T?"yes":"no")<<endl
#define YesNo(T) cout<<(T?"Yes":"No")<<endl
const int INFint = 1 << 30;
const ll INFLL = 1LL << 60;
const ll MOD = 1000000007LL;
const double pi = 3.14159265358979;
const vector<int> h_idx = {-1, 0,0,1};
const vector<int> w_idx = { 0,-1,1,0};
int si,sj;
int now_i,now_j;
vector<vector<int>> tile(50, vector<int>(50));
vector<vector<int>> profit(50, vector<int>(50));
vector<bool> seen(2505,false);
string ans;
string all_ans;
ll best_score;
clock_t start_time;
clock_t now_time;
mt19937 mt((int)time(0));
bool check_time_limit(){
now_time = clock();
if ((double)(now_time - start_time) / CLOCKS_PER_SEC > 1.95) return true;
else return false;
}
// 実行時間が1.95秒を越したらtrue,それまではfalse
void all_resize(){
return;
}
void all_input(){
cin >> si >> sj;
all_resize();
for (int i = 0; i < 50; i++){
for (int j = 0; j < 50; j++){
cin >> tile[i][j];
}
}
for (int i = 0; i < 50; i++){
for (int j = 0; j < 50; j++){
cin >> profit[i][j];
}
}
seen[tile[si][sj]] = true;
return;
}
ll calc_score(string &s){
int tmp_now_i = si,tmp_now_j = sj;
ll res = profit[si][sj];
for (auto x : s){
if (x == 'U'){
tmp_now_i += h_idx[0];
tmp_now_j += w_idx[0];
}
else if (x == 'L'){
tmp_now_i += h_idx[1];
tmp_now_j += w_idx[1];
}
else if (x == 'R'){
tmp_now_i += h_idx[2];
tmp_now_j += w_idx[2];
}
else{
tmp_now_i += h_idx[3];
tmp_now_j += w_idx[3];
}
res += profit[tmp_now_i][tmp_now_j];
}
return res;
}
void all_output(){
cout << all_ans << endl;
return;
}
bool check_range_and_seen(int i,int j){
if (i < 0 || i >= 50 || j < 0 || j >= 50) return false;
if (seen[tile[i][j]]) return false;
else return true;
}
bool random_walk(int &i,int &j){
if (check_time_limit()) return false;
vector<int> possible_direction;
for (int k = 0; k < 4; k++){
int tmp_next_i = i + h_idx[k];
int tmp_next_j = j + w_idx[k];
if (check_range_and_seen(tmp_next_i,tmp_next_j)) possible_direction.push_back(k);
}
if (possible_direction.size() == 0) return false;
int num = mt() % (possible_direction.size());
int direction = possible_direction[num];
int next_i = i + h_idx[direction];
int next_j = j + w_idx[direction];
if (direction == 0){
ans.push_back('U');
}
else if (direction == 1) ans.push_back('L');
else if (direction == 2) ans.push_back('R');
else ans.push_back('D');
i = next_i;
j = next_j;
seen[tile[i][j]] = true;
return true;
}
void random_walk_all(){
ans = "";
now_i = si;
now_j = sj;
while (random_walk(now_i,now_j));
if (chmax(best_score,calc_score(ans))){
// cout << best_score << endl;
all_ans = ans;
}
return;
}
void solve(){
ll counter = 0;
while (!check_time_limit()){
random_walk_all();
counter++;
}
// cout << counter << endl;
return;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(15);
all_input();
solve();
all_output();
}
// 乱択 |
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=200000;
int n,a[N*2+9],ord[N*2+9];
void into(){
scanf("%d",&n);
for (int i=1;i<=n<<1;++i){
scanf("%d",&a[i]);
ord[i]=i;
}
}
stack<int>sta;
char ans[N*2+9];
void Get_ans(){
sort(ord+1,ord+n*2+1,[&](const int &x,const int &y){return a[x]<a[y];});
for (int i=1;i<=n;++i) a[ord[i]]=0;
for (int i=n+1;i<=n<<1;++i) a[ord[i]]=1;
for (int i=1;i<=n<<1;++i)
if (sta.empty()||a[i]==a[sta.top()]) sta.push(i);
else{
ans[sta.top()]='(';
ans[i]=')';
sta.pop();
}
}
void work(){
Get_ans();
}
void outo(){
for (int i=1;i<=n<<1;++i)
putchar(ans[i]);
puts("");
}
int main(){
int T=1;
//scanf("%d",&T);
for (;T--;){
into();
work();
outo();
}
return 0;
} | #include <bits/stdc++.h>
#define FOR(i, a, n) for(ll i = (ll)a; i < (ll)n; i++)
#define FORR(i, n) for(ll i = (ll)n - 1LL; i >= 0LL; i--)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)
using namespace std;
using ll = long long;
constexpr ll Mod = 998244353;
constexpr ll mod = 1e9 + 7;
constexpr ll inf = 1LL << 60;
const double PI = acos(-1);
template <typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {
return a < b && (a = b, true);
}
template <typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {
return a > b && (a = b, true);
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
for(T &x : v) is >> x;
return is;
}
struct IoSetup {
IoSetup() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(10);
}
} iosetup;
/*-------------------------------------------*/
int main() {
int n;
cin >> n;
vector<int> a(n * 2);
cin >> a;
vector<int> p(n * 2);
iota(ALL(p), 0);
sort(ALL(p), [&](int i, int j) { return a[i] < a[j]; });
rep(i, n * 2) a[p[i]] = (i < n);
stack<int> st;
string ans(n * 2, '.');
rep(i, n * 2) {
if(st.empty() || a[st.top()] == a[i])
st.push(i);
else {
ans[st.top()] = '(';
ans[i] = ')';
st.pop();
}
}
cout << ans << endl;
} |
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>
static const int MOD = 998244353;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;
template<class T> constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208;
template <u32 M>
struct modint {
u32 val;
public:
static modint raw(int v) { modint x; x.val = v; return x; }
modint() : val(0) {}
template <class T>
modint(T v) { ll x = (ll)(v%(ll)(M)); if (x < 0) x += M; val = u32(x); }
modint(bool v) { val = ((unsigned int)(v) % M); }
modint& operator++() { val++; if (val == M) val = 0; return *this; }
modint& operator--() { if (val == 0) val = M; val--; return *this; }
modint operator++(int) { modint result = *this; ++*this; return result; }
modint operator--(int) { modint result = *this; --*this; return result; }
modint& operator+=(const modint& b) { val += b.val; if (val >= M) val -= M; return *this; }
modint& operator-=(const modint& b) { val -= b.val; if (val >= M) val += M; return *this; }
modint& operator*=(const modint& b) { u64 z = val; z *= b.val; val = (u32)(z % M); return *this; }
modint& operator/=(const modint& b) { return *this = *this * b.inv(); }
modint operator+() const { return *this; }
modint operator-() const { return modint() - *this; }
modint pow(long long n) const { modint x = *this, r = 1; while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; }
modint inv() const { return pow(M-2); }
friend modint operator+(const modint& a, const modint& b) { return modint(a) += b; }
friend modint operator-(const modint& a, const modint& b) { return modint(a) -= b; }
friend modint operator*(const modint& a, const modint& b) { return modint(a) *= b; }
friend modint operator/(const modint& a, const modint& b) { return modint(a) /= b; }
friend bool operator==(const modint& a, const modint& b) { return a.val == b.val; }
friend bool operator!=(const modint& a, const modint& b) { return a.val != b.val; }
};
using mint = modint<MOD>;
int main() {
int n, m, k;
cin >> n >> m >> k;
vector<mint> A(k+1), B(k+1);
for (int i = 1; i <= k; ++i) {
A[i] = mint(i).pow(n);
}
for (int i = k; i >= 1; --i) {
A[i] -= A[i-1];
}
for (int i = k; i >= 1; --i) {
B[i] = mint(k-i+1).pow(m);
}
if(n == 1 || m == 1) for (int i = 1; i < k; ++i) {
B[i] -= B[i+1];
}
mint ans = 0;
for (int i = 1; i <= k; ++i) {
ans += A[i]*B[i];
}
cout << ans.val << "\n";
return 0;
} | #include<bits/stdc++.h>
#define ll long long int
#define pll pair<ll,ll>
#define vpll vector< pll >
#define mpll map<ll,ll>
#define MOD 998244353
#define all(v) v.begin(),v.end()
#define s(v) v.size()
#define test ll t;cin>>t;while(t--)
#define vec vector<ll>
#define read0(v,n) for(int i=0;i<n;i++)cin>>v[i];
#define read1(v,n) for(int i=1;i<=n;i++)cin>>v[i];
#define trav(a,x) for (auto& a: x)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define cut(x) {cout<<x;return 0;}
#define print(x) {cout<<x<<nl;continue;}
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORB(i,a,b) for(int i=a;i>=b;i--)
#define err1(a) {cout<<#a<<' '<<a<<nl;}
#define err2(a,b) {cout<<#a<<' '<<a<<' '<<#b<<' '<<b<<nl;}
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define f first
#define sc second
#define lb lower_bound
#define ub upper_bound
#define nl '\n'
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define oset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
ll gcd(ll a, ll b)
{
if (b==0)return a;
return gcd(b, a % b);
}
ll lcm(ll a,ll b)
{
return (a*b)/gcd(a,b);
}
ll bpow(ll a, ll b)
{
ll ans=1;
while(b)
{
if(b&1)
ans=(ans*a)%MOD;
b/=2;
a=(a*a)%MOD;
}
return ans;
}
int main()
{
fast
ll n,m,k;
cin>>n>>m>>k;
ll ans=0;
if(n==1 and m==1)cut(k)
if(n==1)
{
ans=bpow(k,m);
cut(ans)
}
if(m==1)
{
ans=bpow(k,n);
cut(ans)
}
FOR(i,1,k)
{
ll p=(ll)i,q=(ll)(k-i+1);
if(q<0)break;
p=(bpow(p,n)-bpow(p-1,n)+MOD)%MOD;q=bpow(q,m);
ans=(ans+(p*q)%MOD)%MOD;
}
cut(ans)
}
|
#include <bits/stdc++.h>
#define ll long long
#define map unordered_map
#define set unordered_set
#define l_l pair<ll, ll>
#define P pair<ll, ll>
#define vll vector<ll>
#define mll map<ll, ll>
#define mp make_pair
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define rev(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define revs(i, n) for (int i = ((int)(n)); i > 0; --i)
// 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; }
using namespace std;
const ll MOD = 1000000007LL;
const ll INF = (1LL << 60LL);
// template <class T> void plus_mod(T &a, const T &b) {a = (a + b) % MOD;}
int main() {
// std::cout << std::fixed << std::setprecision(10);
// ll ans = 0;
string str;
cin >> str;
deque<char> dq;
bool rev = false;
rep(i, str.size()) {
char ch = str[i];
if (ch == 'R') {
rev = !rev;
continue;
}
if (!rev) {
if (dq.size() >= 1 && dq.front() == ch) {
dq.pop_front();
} else {
dq.push_front(ch);
}
} else {
if (dq.size() >= 1 && dq.back() == ch) {
dq.pop_back();
} else {
dq.push_back(ch);
}
}
}
string ans;
for (;;) {
if (dq.size() == 0) break;
ans += dq.back();
dq.pop_back();
}
if (rev) {
reverse(ans.begin(), ans.end());
}
cout << (ans) << endl;
// cout << ans << endl;
}
| /**
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡀⠀⠀⠀⠀⢀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⡖⠁⠀⠀⠀⠀⠀⠀⠈⢲⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣼⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⣧⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣸⣿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⣇⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣿⣿⡇⠀⢀⣀⣤⣤⣤⣤⣀⡀⠀⢸⣿⣿⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⢻⣿⣿⣔⢿⡿⠟⠛⠛⠻⢿⡿⣢⣿⣿⡟⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⣀⣤⣶⣾⣿⣿⣿⣷⣤⣀⡀⢀⣀⣤⣾⣿⣿⣿⣷⣶⣤⡀⠀⠀⠀⠀
⠀⠀⢠⣾⣿⡿⠿⠿⠿⣿⣿⣿⣿⡿⠏⠻⢿⣿⣿⣿⣿⠿⠿⠿⢿⣿⣷⡀⠀⠀
⠀⢠⡿⠋⠁⠀⠀⢸⣿⡇⠉⠻⣿⠇⠀⠀⠸⣿⡿⠋⢰⣿⡇⠀⠀⠈⠙⢿⡄⠀
⠀⡿⠁⠀⠀⠀⠀⠘⣿⣷⡀⠀⠰⣿⣶⣶⣿⡎⠀⢀⣾⣿⠇⠀⠀⠀⠀⠈⢿⠀
⠀⡇⠀⠀⠀⠀⠀⠀⠹⣿⣷⣄⠀⣿⣿⣿⣿⠀⣠⣾⣿⠏⠀⠀⠀⠀⠀⠀⢸⠀
⠀⠁⠀⠀⠀⠀⠀⠀⠀⠈⠻⢿⢇⣿⣿⣿⣿⡸⣿⠟⠁⠀⠀⠀⠀⠀⠀⠀⠈⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣼⣿⣿⣿⣿⣧⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠐⢤⣀⣀⢀⣀⣠⣴⣿⣿⠿⠋⠙⠿⣿⣿⣦⣄⣀⠀⠀⣀⡠⠂⠀⠀⠀
⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠉⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠋⠁⠀⠀
**/
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define nl cout<<"\n";
#define ll long long int
#define ld double
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define F(i,a,b) for(i=a;i<b;i++)
//#include <ext/pb_ds/assoc_container.hpp> // policy based data structure header files
//#include <ext/pb_ds/tree_policy.hpp> // policy based data structure header files
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
//using namespace __gnu_pbds; // for pbds
//#define ordered_set tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> // have functions like order_of_key, find_by_order
const double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781641;
const ll M=1e18;
const ll MAXN=100200;
ll i,j,mask;
void solve()
{
string s;
cin>>s;
deque<char> dq;
ll f=0;
for(auto x:s)
{
if(x=='R')
{
f^=1;
continue;
}
if(!f)
{
if(dq.empty() || dq.back()!=x)
{
dq.push_back(x);
}
else
if(!dq.empty())
{
dq.pop_back();
}
}
else
{
if(dq.empty() || dq.front()!=x)
{
dq.push_front(x);
}
else
if(!dq.empty())
{
dq.pop_front();
}
}
}
if(f)
reverse(all(dq));
for(auto x:dq)
{
cout<<x;
}
}
int main()
{
IOS
/*#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif // ONLINE_JUDGE*/
ll t,test=1;
//cin>>test;
F(t,1,test+1) {
//cout<<"Case #"<<t<<": ";
solve();
nl
}
return 0;
}
|
#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, a, b) for (int i = (int)(a); i >= (int)b; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define per(i, r, l) for (int i = (r); i >= (l); i--)
#define ms(x, y) memset(x, y, sizeof(x))
#define SZ(x) int(x.size())
#define fk cerr<<"fk"<<endl
#define db(x) cerr<<(#x)<<'='<<(x)<<endl
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;
template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }
//1.integer overflow (1e5 * 1e5) (2e9 + 2e9)
//2.runtime error
//3.boundary condition
const int N=2100;
int h,w,vis[N][N],dir[4][2]={{-1,0},{1,0},{0,1},{0,-1}};
char mat[N][N];
vector< pair<int,int> > idx[30];
bool ok(int x,int y){
if(x<1 || x>h || y<1 || y>w) return false;
if(mat[x][y]=='#') return false;
if(vis[x][y]==1) return false;
return true;
}
int bfs(int sx,int sy){
queue< pair< pair<int,int>,int > > q;
q.push(mp(mp(sx,sy),0));
vis[sx][sy]=1;
while(!q.empty()){
pair< pair<int,int>,int > now=q.front();
q.pop();
int nx=now.fi.fi,ny=now.fi.se;
if(mat[nx][ny]=='G') return now.se;
char temp=mat[nx][ny];
if(temp>='a' && temp<='z'){
for(pair<int,int> np:idx[int(temp-'a')]){
vis[np.fi][np.se]=1;
q.push(mp(mp(np.fi,np.se),now.se+1));
}
idx[int(temp-'a')].clear();
}
for(int i=0; i<4; ++i){
int tx=nx+dir[i][0];
int ty=ny+dir[i][1];
if(ok(tx,ty)){
// if(mat[tx][ty]=='G') return now.se+1;
vis[tx][ty]=1;
q.push(mp(mp(tx,ty),now.se+1));
}
}
}
return -1;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.precision(10);
cout << fixed;
#ifdef LOCAL_DEFINE
freopen("input.txt", "r", stdin);
#endif
cin>>h>>w;
int sx,sy;
for1(i, h){
for1(j, w){
cin>>mat[i][j];
if(mat[i][j]>='a' && mat[i][j]<='z'){
int num=int(mat[i][j]-'a');
idx[num].eb(mp(i,j));
}
if(mat[i][j]=='S'){
sx=i;
sy=j;
}
}
}
ms(vis,0);
cout<<bfs(sx,sy)<<'\n';
#ifdef LOCAL_DEFINE
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}
|
//BELIEVE IN YOURSELF
//next_permutation
#include<bits/stdc++.h>
#define ll long long int
#define ld long double
#define pb push_back
#define pob pop_back
#define vi vector<ll>
#define mp make_pair
#define sz size()
#define rep1(i,n) for(ll i=1;i<=n;i++)
#define rep(i,n) for(ll i=0;i<n;i++)
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define maxx 100005
#define sec second
#define fi first
#define be begin()
#define en end()
#define sortv(v) sort(v.begin(),v.end());
#define sortr(v) sort(v.rbegin(),v.rend());
#define w() ll _;cin>>_;while(_--)
#define edge pair<ll,ll>
#define pq priority_queue<ll>
#define mod 1000000007
#define Endl "\n"
#define cy cout<<"YES"<<endl;
#define cn cout<<"NO"<<endl;
#define lb(v,x) std::lower_bound(v.begin(),v.end(),x)
#define ub(v,x) std::upper_bound(v.begin(),v.end(),x)
using namespace std;
ll dx[]={-1,0,1,0};
ll dy[]={0,-1,0,1};
ll n,m;
bool isvalid(ll x,ll y)
{
if(x>=0 && y>=0 && x<n && y<m)
{
return true;
}
return false;
}
int main()
{
fast;
cin>>n>>m;
ll i,j;
string s;
vector<string>g;
for(i=0;i<n;i++)
{
cin>>s;
g.pb(s);
}
ll a[n+1][m+1];
// ll vis[n+1][m+1];
ll inf=1e17;
ll sx=0,sy=0,ex=0,ey=0;
vector<vector<pair<ll,ll> > >tp(26);
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
a[i][j]=inf;
if(g[i][j]=='S')
{
a[i][j]=0;
sx=i;
sy=j;
}
if(g[i][j]=='G')
{
ex=i;
ey=j;
}
if(g[i][j]>='a' && g[i][j]<='z')
{
char c=g[i][j];
tp[c-'a'].pb(mp(i,j));
}
}
}
queue<pair<ll,ll> >q;
q.push(mp(sx,sy));
while(!q.empty())
{
ll x,y;
pair<ll,ll>now =q.front();
q.pop();
x=now.fi;
y=now.sec;
for(i=0;i<4;i++)
{
ll nx,ny;
pair<ll,ll>next = mp(x+dx[i],y+dy[i]);
nx=next.fi;
ny=next.sec;
if(isvalid(nx,ny))
{
if(g[nx][ny]!='#' && a[nx][ny]==inf)
{
a[nx][ny]=a[x][y]+1;
q.push(mp(nx,ny));
}
}
}
char c=g[x][y];
if(islower(c))
{
for(auto next:tp[c-'a'])
{
ll nx,ny;
nx=next.fi;
ny=next.sec;
if(a[nx][ny]==inf)
{
a[nx][ny]=a[x][y]+1;
q.push(mp(nx,ny));
}
}
tp[c-'a'].clear();
}
}
ll ans=a[ex][ey];
if(ans==inf)
{
cout<<-1<<Endl;
}
else
{
cout<<ans<<endl;
}
return 0;
}
|
#include<iostream>
#include<string>
using namespace std;
int main() {
long int n, x;
string s;
cin>>n>>x;
cin>>s;
for(int i = 0; i < s.size(); i++) {
if(s[i] == 'x' && x != 0)
x--;
else if(s[i] == 'o')
x++;
}
cout<<x<<endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,a,b;
cin>>n>>a>>b;
cout<<n-a+b;
} |
#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
#define fi first
#define se second
#define pb push_back
#define mod(n,k) ( ( ((n) % (k)) + (k) ) % (k))
#define forn(i,a,b) for(int i = a; i < b; i++)
#define forr(i,a,b) for(int i = a; i >= b; i--)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
int sum(int a, int b,int mod){return (a+b) % mod;}
int sub(int a, int b,int mod){return (a + mod - b) % mod;}
int mul(int a, int b,int mod){return (1ll * a * b) % mod;}
int power(int a,int b,int mod){
int res = 1;
while(b){
if(b&1)res = mul(res,a,mod);
b >>= 1;
a = mul(a,a,mod);
}
return res;
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
ll a,b,c; cin >> a >> b >> c;
vi res;
set<int> seen;
ll act = a%10;
while(1){
if(seen.count(act))break;
res.pb(act);
seen.insert(act);
act = mul(act,a,10);
}
int pos = power(b,c,res.size());
pos = sub(pos,1,res.size());
//debug(res,pos);
cout << res[pos] << '\n';
return 0;
}
/*
__builtin_mul_overflow(x,y,&x)
-fsplit-stack
*/
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, N) for(int i = 0; i < N; i++)
int main() {
int N, A[20], min = INT32_MAX;
cin >> N;
rep(i, N) cin >> A[i];
for (int bit = 0; bit < (1 << (N - 1)); bit++) {
int OR = 0, XOR = 0;
for (int i = 0; i < N - 1; i++) {
OR = OR | A[i];
if (bit & (1 << i)) {
XOR = XOR ^ OR;
OR = 0;
}
}
OR = OR | A[N - 1];
XOR = XOR ^ OR;
if (min > XOR) min = XOR;
}
cout << min << endl;
} |
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<vector<int>> vvi;
typedef vector<vector<long long>> vvl;
int main(){
int a,b,x,y;
cin >> a >> b >> x >> y;
int z=min(2*x,y);
if(a==b){cout << x;return 0;}
if(a>b){cout << x+(a-b-1)*z;return 0;}
cout << x+(b-a)*z;
return 0;
} | //Created By:Hardik Kapoor
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
//template begins
#define int long long int
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);
#define itt vector<int>:: iterator
#define pb push_back
#define mp make_pair
#define max3(a,b,c) max(max((a),(b)),(c))
#define max4(a,b,c,d) max(max((a),(b)),max((c),(d)))
#define min3(a,b,c) min(min((a),(b)),(c))
#define min4(a,b,c,d) min(min((a),(b)),min((c),(d)))
#define lb lower_bound
#define ub upper_bound
#define all(v) v.begin(),v.end()
#define db long double
#define itp vector<pair<int,int> >:: iterator
#define ff first
#define ss second
#define endl "\n"
typedef pair<int,int> pii;
template<typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
/*
find_by_order() - Returns an iterator to the k-th largest element (counting from zero)
order_of_key() - The number of items in a set that are strictly smaller than our item
Rest same as set
*/
const int inf=1000000000000000000;
const long double pi = 3.14159265358979323846264338;
const long long mod = 1000000007;
// const long long mod = 998244353;
int pow(int x, unsigned int y, int p){
int res=1;
x=x%p;
if (x==0) return 0;
while (y > 0)
{
if (y & 1)
res = (res*x) % p;
y = y>>1;
x = (x*x) % p;
}
return res;
}
int _pow(int a, int b){
if(!b)
return 1;
int temp = _pow(a, b / 2);
temp = (temp * temp);
if(b % 2)
return (a * temp);
return temp;
}
int invmod(int a,int m){
return pow(a,m-2,m);
}
int cl(int a,int x){
if(a%x==0)
return a/x;
else
return a/x+1;
}
//template ends
signed main(){
ios
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int Tests=1;
while(Tests--)
{
int a,b,c;
cin>>a>>b>>c;
if(a==b)
cout<<c<<endl;
else if(b==c)
cout<<a<<endl;
else if(a==c)
cout<<b<<endl;
else
cout<<0<<endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define each(e, v) for(auto &e: v)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
//const int MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};
struct io_setup{
io_setup(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << fixed << setprecision(15);
}
} io_setup;
template<typename T>
vector<T> divisors(const T &n){
vector<T> ret;
for(T i = 1; i*i <= n; i++){
if(n%i == 0){
ret.pb(i);
if(i*i != n) ret.pb(n/i);
}
}
sort(all(ret));
return ret;
}
template<typename T>
vector<pair<T, int>> prime_factor(T n){
vector<pair<T, int>> ret;
for(T i = 2; i*i <= n; i++){
int cnt = 0;
while(n%i == 0) cnt++, n /= i;
if(cnt > 0) ret.emplace_back(i, cnt);
}
if(n > 1) ret.emplace_back(n, 1);
return ret;
}
template<typename T>
bool is_prime(const T &n){
if(n == 1) return false;
for(T i = 2; i*i <= n; i++){
if(n%i == 0) return false;
}
return true;
}
vector<bool> Eratosthenes(const int &n){
vector<bool> ret(n+1, true);
if(n >= 0) ret[0] = false;
if(n >= 1) ret[1] = false;
for(int i = 2; i*i <= n; i++){
if(!ret[i]) continue;
for(int j = i+i; j <= n; j += i) ret[j] = false;
}
return ret;
}
vector<int> Eratosthenes2(const int &n){
vector<int> ret(n+1);
iota(begin(ret), end(ret), 0);
if(n >= 0) ret[0] = -1;
if(n >= 1) ret[1] = -1;
for(int i = 2; i*i <= n; i++){
if(ret[i] < i) continue;
for(int j = i+i; j <= n; j += i) ret[j] = min(ret[j], i);
}
return ret;
}
int main(){
ll A, B; cin >> A >> B;
vector<bool> p = Eratosthenes(B-A);
vector<int> ps;
rep2(i, 1, B-A){
if(p[i]) ps.eb(i);
}
//cout << sz(ps) << '\n';
int n = sz(ps);
vector<int> s(B-A+1, 0);
rep2(i, 0, B-A){
ll k = A+i;
rep(j, n){
if(k%ps[j] == 0) s[i] |= (1<<j);
}
}
vector<ll> dp(1<<n, 0);
dp[0] = 1;
rep(i, B-A+1){
rep3(j, (1<<n)-1, 0){
if((j&s[i]) == 0) dp[j|s[i]] += dp[j];
}
}
cout << accumulate(all(dp), 0LL) << '\n';
} | #include <bits/stdc++.h>
using namespace std;
int diri[] = {1, 0, -1, 0}, dirj[] = {0, 1, 0, -1};
#define mp make_pair
typedef pair <int, int> ii;
#define INF 1000000005
int h, w;
char grid[2005][2005];
vector<ii> tele[30];
int dist[2005][2005];
int gi, gj;
queue<ii> q;
int main() {
scanf("%d%d", &h, &w);
for (int i = 0; i < h; i++) {
scanf(" %s", grid[i]);
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
dist[i][j] = INF;
}
}
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (grid[i][j] == 'S') {
dist[i][j] = 0;
q.push(mp(i, j));
} else if (grid[i][j] == 'G') {
gi = i, gj = j;
} else if (grid[i][j] - 'a' >= 0 && grid[i][j] - 'a' < 27) {
tele[grid[i][j] - 'a'].push_back(mp(i, j));
}
}
}
while (!q.empty()) {
int i, j; tie(i, j) = q.front(); q.pop();
for (int k = 0; k < 4; k++) {
int ni = i + diri[k], nj = j + dirj[k];
if (ni < 0 || ni >= h || nj < 0 || nj >= w) continue;
if (grid[ni][nj] == '#') continue;
if (dist[ni][nj] > dist[i][j] + 1) {
dist[ni][nj] = dist[i][j] + 1;
q.push(mp(ni, nj));
}
if (ni == gi && nj == gj) {
printf("%d\n", dist[ni][nj]);
return 0;
}
}
if (grid[i][j] - 'a' >= 0 && grid[i][j] - 'a' < 27) {
for (ii ports : tele[grid[i][j] - 'a']) {
int ni, nj; tie(ni, nj) = ports;
if (ni == i && nj == j) continue;
if (dist[ni][nj] > dist[i][j] + 1) {
dist[ni][nj] = dist[i][j] + 1;
q.push(mp(ni, nj));
}
}
}
}
printf("-1\n");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i,t,n) for(int64_t i=t;i<n;i++)
#define Sort(a) sort(a.begin(),a.end())
#define rSort(a,n,m) sort(a.begin()+n-1,a.begin()+m)
#define Reverse(a) reverse(a.begin(),a.end())
#define rReverse(a,n,m) reverse(a.begin()+n-1,a.begin()+m)
#define MOD 1000000007
#define sign(i) -1*pow(-1,i)
#define vi(A,N,i) vector<int64_t> A(N,i)
#define vs(A,N,i) vector<string> A(N,i)
#define vb(A,N,i) vector<bool> A(N,i)
#define INF ((1LL<<62)-(1LL<<31))
//((a+b+c)-max(a,max(b,c))-min(a,min(b,c)))
/*template <class T>
}*/
//cout << fixed << setprecision(桁数);
int64_t def_str(string x,string y){
int64_t def=0;
rep(i,0,(int64_t)x.size()){
if(x[i]!=y[i]){
def++;
}
}
return def;
}
int main() {
int64_t a,b,c,d,e,f,ans=0,now_ans=0,mlt_ans=1;
//double a,b,c,d,e,f,ans=0,now_ans=0,mlt_ans=1;
string x,y,z;
char s,t,u;
bool frag=false,frag1=false,frag2=false;
vector<int64_t> A{},B{};
vector<string> C{},D{};
vector<char> E{},F{};
vector<bool> G(26,false);
cin>>a;
//cin>>x>>y;
//cin>>s>>t;////////////////////////
ans=INF;
rep(i,0,a){
cin>>b>>c>>d;
if(b<d){
ans=min(ans,c);
}
}
if(ans==INF){
cout<<-1;
return 0;
}
cout<<ans;
} | #include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <list>
using namespace std;
#define PB push_back
#define MP make_pair
#define REP(i,n) for(i=0;i<(n);++i)
#define PER(i,n) for(i=(n - 1);i>=0;--i)
#define FOR(i,l,h) for(i=(l);i<=(h);++i)
#define FORD(i,h,l) for(i=(h);i>=(l);--i)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define lINF 0x3f3f3f3f3f3f3f3f
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long LL;
typedef pair<int,int> PII;
typedef map<int, int> MII;
int main()
{
int n,i;
while(cin>>n){
int a,p,x;
int res = INF;
FOR(i,1,n){
cin>>a>>p>>x;
if(x>a) res = min(res, p);
}
if(res==INF) res=-1;
cout<<res<<endl;
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define int long long
int a[100];int n;const int mod=998244353;
int f[210000],finv[210000];
inline int qpow(int a,int b,int p)
{
int k=1;
while(b)
{
if(b&1) k=k*a%p;
a=a*a%p;b>>=1;
}return k;
}
inline int comb(int n,int m)
{
if(n<m) return 0;
if(n==m||m==0) return 1;
return f[n]*finv[n-m]%mod*finv[m]%mod;
}int ans=0;
/*inline int lowbit(int x){return x&(-x);}
int num=0;
inline void dfs(int x,int left,int bas,int mul)
{
if(left==0)
{
int res=1;
//cout<<">> "<<r<<endl;
num++;
if(num%10000000==0) cout<<num<<endl;
ans+=mul;
return ;
}
if(lowbit(left)<bas) return ;
if(bas>left) return ;
for(int i=0;i*bas<=left&&i<=n;i+=2)
{
a[x]=i;
dfs(x+1,left-i*bas,bas<<1,mul*comb(n,i)%mod);
}
}*/
int dp[6000];
signed main()
{
f[0]=finv[0]=1;
for(int i=1;i<=201000;i++) f[i]=f[i-1]*i%mod;
finv[201000]=qpow(f[201000],mod-2,mod);
for(int i=201000-1;i>=1;i--) finv[i]=finv[i+1]*(i+1)%mod;
int m;
cin>>n>>m;
ans=0;
dp[0]=1;
for(int i=2;i<=m;i+=2)
{
for(int j=0;j<=i;j+=2)
{
dp[i]+=dp[(i-j)>>1]*comb(n,j)%mod;
dp[i]%=mod;
}
}
cout<<dp[m]<<endl;
} | #pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
#define FOR(i, a, b) for (int i = a; i <= (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b); i >= a; i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto &a : x)
#define sz(x) (int)(x).size()
#define popcnt(x) __builtin_popcount(x)
#define low_bo(a, x) (lower_bound(a.begin(), a.end(), x) - a.begin())
#define up_bo(a, x) (upper_bound(a.begin(), a.end(), x) - a.begin())
#define unique(a) a.resize(unique(a.begin(), a.end()) - a.begin())
#define shuffle(a) shuffle(a.begin(), a.end(), rnd)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 998244353;
const char nl = '\n';
const int MX = 20000; //check the limits, dummy
int n, m, np;
ull fac[MX];
ll c[5001];
ll rev(ll x, ll m) { return x == 1 ? 1 : (1 - rev(m % x, x) * (ll)m) / x + m; }
ll choose(ll n, ll r) {
if (n < r)
return 0;
if (r == 0)
return 1;
return ((fac[n] * (rev(fac[r], MOD) % MOD)) % MOD * (rev(fac[n - r], MOD) % MOD)) % MOD;
}
void init() {
fac[0] = 1;
for (int i = 1; i < MX; i++)
fac[i] = (fac[i - 1] * i) % MOD;
}
void solve(int t) {
cin >> n >> m;
np = (n / 2) * 2;
if (m % 2) {
cout << "0\n";
return;
}
ll dp[5001][13];
for (int ma = 0; ma < 5001; ma++) {
if (ma % 2) continue;
dp[ma][0] = choose(n, ma);
}
for (int d = 1; d < 13; d++) {
for (int ma = 0; ma < 5001; ma++) {
if (ma % 2) continue;
int p2 = (1 << d);
int mx = min(ma / 2 / p2, n / 2);
ll ret = 0;
for (int i = 0; i <= mx; i++) {
ret = ((dp[2 * i][0] * dp[ma - i * 2 * p2][d - 1]) % MOD + ret) % MOD;
}
dp[ma][d] = ret;
}
}
int d = 0;
while ((1 << d) <= m)
d++;
cout << dp[m][d - 2] << nl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout.precision(20);
cout << fixed;
init();
int T = 1;
F0R(i, T) {
solve(i + 1);
}
}
// read the question correctly (ll vs int)
// template by bqi343 |
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <unordered_map>
#include <vector>
// #include <atcoder/all>
/*
cd $dir && g++ -std=c++17 -Wall -Wextra -O2 -DATCODERDEBUG
-I/mnt/d/MyProjects/atcoder/lib/ac-library $fileName && echo 'compilation ok!'
&& ./a.out
*/
using namespace std;
#define REP(i, n) for (ll i = 0; i < ll(n); i++)
#define FOR(i, a, b) for (ll i = a; i <= ll(b); i++)
#define ALL(x) x.begin(), x.end()
#define dame(a) \
{ \
cout << a << endl; \
return 0; \
}
typedef long long ll;
class dstream : public ostream {};
template <typename T>
dstream &operator<<(dstream &os, T v) {
#ifdef ATCODERDEBUG
cout << "\033[1;31m" << v << "\033[0m";
#endif
return os;
}
ostream &endd(std::ostream &out) {
#ifdef ATCODERDEBUG
out << std::endl;
#endif
return out;
}
dstream dout;
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
os << "[ ";
for (auto const &x : v) {
os << x;
if (&x != &v.back()) {
os << " : ";
}
}
os << " ]" << endl;
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<vector<T>> &v) {
os << "[" << endl;
for (auto const &x : v) {
os << " ";
os << x;
}
os << "]" << endl;
return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &v) {
os << "[ " << v.first << " " << v.second << " ]" << endl;
return os;
}
template <typename T, typename U, typename Comp = less<>>
bool chmax(T &xmax, const U &x, Comp comp = {}) {
if (comp(xmax, x)) {
xmax = x;
return true;
}
return false;
}
template <typename T, typename U, typename Comp = less<>>
bool chmin(T &xmin, const U &x, Comp comp = {}) {
if (comp(x, xmin)) {
xmin = x;
return true;
}
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll N;
cin >> N;
vector<vector<ll>> C(N, vector<ll>(N, 0));
REP(i, N) {
REP(j, N) { cin >> C[i][j]; }
}
// check
for (ll i = 1; i < N; i++) {
ll d = C[i][0] - C[0][0];
for (ll j = 1; j < N; j++) {
if (C[0][j] + d != C[i][j]) {
dout << "no1" << endd;
cout << "No" << endl;
return 0;
}
}
}
ll upper = 1e12;
REP(j, N) { chmin(upper, C[0][j]); }
ll downer = 0;
for (ll i = 1; i < N; i++) {
ll e = C[i][0] - C[0][0];
chmax(downer, -e);
}
if (downer > upper) {
cout << "No" << endl;
return 0;
}
ll d = downer;
cout << "Yes" << endl;
// A
REP(i, N) {
cout << (C[i][0] - C[0][0] + d);
if (i != N - 1)
cout << " ";
else
cout << endl;
}
// B
REP(j, N) {
cout << (C[0][j] - d);
if (j != N - 1)
cout << " ";
else
cout << endl;
}
// cout << std::fixed << std::setprecision(15) << sqrt(ans) << endl;
} | #include <bits/stdc++.h>
#include <cmath>
#define fl(i,a,b) for(long long i=a;i<b;i++)
#define test long long t_c;cin>>t_c;for(long long ks=1;ks<=t_c;ks++)
#define pb push_back
#define vi vector<int>
#define all(x) x.begin(),x.end()
#define cy cout<<"YES\n";
#define cn cout<<"NO\n";
#define nl cout<<endl;
#define case(x) cout<<"Case "<<x<<": ";
typedef long long ll;
typedef long long int lli;
void sc(int &x){scanf("%d",&x);}
void sc(lli &x){scanf("%lld",&x);}
void sc(char *x){scanf("%s",x);}
void sc(double &x){scanf("%lf",&x);}
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
lli a,b;
cin>>a>>b;
if(abs(a-b)<3)
{
cout<<"Yes\n";
}
else
{
cout<<"No\n";
}
}
|
#include<ctime>
#include<cstdio>
#include<cctype>
using namespace std;
const int N=1e2+7;
int read() {
char c;
int x=0,f=1;
while(!isdigit(c=getchar()))
f-=2*(c=='-');
while (isdigit(c)){
x=x*10+(c-48)*f;
c=getchar();
}
return x;
}
char s[N];
int n,m,a[N];
int chk(int x){
for(int i=1;i<=n;++i){
if(s[i-1]=='<'){
if(a[i-1]%x>a[i]%x){
if(a[i-1]/x+1>=a[i]/x)
return 0;
}
else{
if(a[i-1]/x>=a[i]/x)
return 0;
}
}
else{
if(a[i-1]%x<a[i]%x){
if(a[i-1]/x<=a[i]/x+1)
return 0;
}
else{
if(a[i-1]/x<=a[i]/x)
return 0;
}
}
}
return 1;
}
void solve(){
int l=1,r=1e4,ans=0;
while(l<=r){
int mid=(l+r)>>1;
if(chk(mid)){
l=mid+1;
ans=mid;
}
else
r=mid-1;
}
printf("%d\n",ans);
for(int i=1;i<=ans;++i){
for(int j=0;j<=n;++j)
printf("%d ",a[j]/ans+(a[j]%ans>=i));
puts("");
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("A.in","r",stdin);
freopen("A.out","w",stdout);
#endif
clock_t t1=clock();
//--------
n=read();
scanf("%s",s);
for(int i=0;i<=n;++i)
a[i]=read();
solve();
//--------
clock_t t2=clock();
fprintf(stderr,"time:%0.3lfs",1.0*(t2-t1)/CLOCKS_PER_SEC);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
double a[101][101][101];
int main()
{
for(int i=99;i>=0;--i)
{
for(int j=99;j>=0;--j)
{
for(int k=99;k>=0;--k)
{
a[i][j][k]=i*1.0/(i+j+k)*(1+a[i+1][j][k])+j*1.0/(i+j+k)*(1+a[i][j+1][k])+k*1.0/(i+j+k)*(1+a[i][j][k+1]);
}
}
}
int i,j,k;
scanf("%d%d%d",&i,&j,&k);
printf("%f",a[i][j][k]);
return 0;
} |
#pragma GCC optimize ("O2")
#pragma GCC target ("avx2")
//#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please
int to[400001], ne[400001], he[200001];
int Q[200001], pl[200001], dl[200001];
const int CM = 1 << 17, CL = 12;
char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint() {
if (ci - owa > 0) {
memcpy(cn, owa, CL);
ci -= CM;
fread(cn + CL, 1, CM, stdin);
}
ll tmp = *(ll*)ci;
int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0);
tmp = tmp << dig & ma1;
tmp = tmp * 10 + (tmp >> 8) & ma2;
tmp = tmp * 100 + (tmp >> 16) & ma3;
tmp = tmp * 10000 + (tmp >> 32) & ma4;
ci += 72 - dig >> 3;
return tmp;
}
int main() {
//cin.tie(0);
//ios::sync_with_stdio(false);
int N = getint(), K = getint();
int k = 1;
rep(i, N - 1) {
int u = getint(), v = getint();
to[k] = v;
ne[k] = he[u];
he[u] = k++;
to[k] = u;
ne[k] = he[v];
he[v] = k++;
}
int q = 0, p = 0;
Q[q++] = 1;
while (p < N) {
int u = Q[p++];
int tmp = 0;
int ind = he[u];
he[u] = 0;
while (ind) {
int v = to[ind];
tmp = ne[ind];
if (he[v] > 0) {
Q[q++] = v;
ne[ind] = he[u];
he[u] = -ind;
}
ind = tmp;
}
}
int are = 1000000, pd0 = are;
int L = 0, R = (N + K - 1) / K;
while (L + 1 < R) {
int wj = (L + R) / 2;
int num = 0;
for (int i = N - 1; i >= 0; i--) {
int po = pd0 - 1;
int dd = pd0;
int u = Q[i];
for (int ind = -he[u]; ind; ind = -ne[ind]) {
int v = to[ind];
if (po < pl[v]) po = pl[v];
if (dd < dl[v]) dd = dl[v];
}
if (po >= dd) dd = 0;
if (dd >= pd0 + wj) {
num++;
po = pd0 + wj;
dd = 0;
}
pl[u] = po - 1;
dl[u] = dd + 1;
if (num > K) break;
}
int are = num + (dl[1] > pd0);
if (are > K) L = wj;
else R = wj;
pd0 += are;
}
printf("%d", R);
Would you please return 0;
} |
/*
* TheMartian0x48
*/
#include <bits/stdc++.h>
using namespace std;
// clang-format off
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define per(i, a, b) for (int i = a; i >= (b); --i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define rll(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vii = vector<long long>;
// (* read and write *)
template <class T>
void re(vector<T> &v, int n) {v.resize(n); for (auto &e : v) cin >> e;}
template <class T>
void re(vector<T> &v){for (auto &e : v) cin >> e;}
// void usaco(string s) {freopen((s + ".in").c_str(), "r", stdin );freopen((s + ".out").c_str(), "w", stdout );}
// clang-format on
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
double a, b; cin >> a >> b;
cout << a * (b / 100.0);
}
|
#include "bits/stdc++.h"
using namespace std;
#define dbg(var) cout<<#var<<"="<<var<<" "
#define nl cout<<"\n"
#define fr(i,n) for(int i=0;i<n;i++)
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define fast ios::sync_with_stdio(false);cin.tie(0);
#define vi vector<int>
#define vvi vector<vi>
#define pb push_back
#define fa(v) for(auto &i:v)
#define all(v) v.begin(),v.end()
#define sz(v) (int)(v.size())
#define int long long
int32_t main()
{
int n; cin >> n;
vi v(n);fr(i,n) cin >> v[i];
vi pfx = v;
rep(i,1,n-1) pfx[i] += pfx[i-1];
vi mxpfx = pfx;
rep(i,1,n-1) mxpfx[i] = max(mxpfx[i-1], pfx[i]);
int mx = 0;
rep(i,1,n-1) pfx[i] += pfx[i-1];
rep(i,0,n-2) mx = max(mx, pfx[i] + mxpfx[i+1]);
mx = max(mx, pfx[n-1]);
cout << mx;
} | #include<iostream>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<cmath>
#include<map>
using namespace std;
#define rep(i,N) for(ll i=0;i<N;i++) //0から
typedef long long ll;
ll binary_search(vector<ll> a, ll n, ll key){
ll right = n, left = -1;
ll md = (right + left) / 2;
while(right - left > 1){
if(a[md] <= key){
right = md;
}else{
left = md;
}
md = (right + left) / 2;
}
if(left == -1) return -1; //無い場合
return right;
}
vector<ll> prime;
void Prime(ll n){ //線形篩,素数列挙
vector<ll> p(n,0);
p[0]=1;
p[1]=1;
for(ll i=2;i<n;i++){
if(p[i]==0){
prime.push_back(i);
p[i]=i;
}
ll k=prime.size();
for(ll j=0;j<k && i*prime[j]<n && prime[j]<=p[i];j++){
p[i*prime[j]]=prime[j];
}
}
}
ll gcd(ll a,ll b){
if(a<b){
swap(a,b);
}
//a>=b
ll r=a%b;
while(r!=0){
a=b;
b=r;
r=a%b;
}
return b;
}
ll modexp(ll x, ll a, ll m){ //x^a mod m
ll ret = 1;
while (a > 0) {
if (a & 1) ret = ret * x % m; // n の最下位bitが 1 ならば x^(2^i) をかける
x = x * x % m;
a >>= 1; // n を1bit 左にずらす
}
return ret;
}
ll modinv(ll a, ll m){ //a^{-1} mod m
ll b=m,u=1,v=0;
while(b){
ll t=a/b;
a-=t*b; swap(a,b);
u-=t*v; swap(u,v);
}
u=(u+m)%m;
return u;
}
//cout<<fixed<<setprecision(10);
int main(){
ll a,b,x,y;
cin>>a>>b>>x>>y;
ll ans=0;
if(a>b){
ans=min(x+y*(a-b-1),x+2*x*(a-b-1));
}
else{
ans=min(x+y*(b-a),x+2*x*(b-a));
}
cout<<ans<<endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll INF = numeric_limits<ll>::max() / 4;
// const int INF = numeric_limits<int>::max() / 4;
int main() {
// ll N;
int R, C;
cin >> R >> C;
vector<vector<int> > A(R,vector<int>(C,0));
vector<vector<int> > B(R,vector<int>(C,0));
for(int i = 0; i < R; i++){
for(int j = 0; j < C - 1; j++){
cin >> A[i][j];
}
}
for(int i = 0; i < R - 1; i++){
for(int j = 0; j < C; j++){
cin >> B[i][j];
}
}
vector<vector<ll> > X(R*2,vector<ll>(C,INF));
multimap<int,pair<int,int> > P;
X[0][0] = 0;
P.insert(make_pair(0,make_pair(0,0)));
while(!P.empty()){
auto p = P.begin();
P.erase(p);
int r = p->second.first;
int c = p->second.second;
int x = X[r][c];
if(R <= r){
r -= R;
if(0 < r){
if(x + 1 < X[r-1][c]){
X[r-1][c] = x + 1;
P.insert(make_pair(X[r-1][c],make_pair(r-1,c)));
}
if(x + 1 < X[r-1+R][c]){
X[r-1+R][c] = x + 1;
P.insert(make_pair(X[r-1+R][c],make_pair(r-1+R,c)));
}
}
continue;
}
if(c < C - 1){
if(x + A[r][c] < X[r][c+1]){
X[r][c+1] = x + A[r][c];
P.insert(make_pair(X[r][c+1],make_pair(r,c+1)));
}
}
if(0 < c){
if(x + A[r][c-1] < X[r][c-1]){
X[r][c-1] = x + A[r][c-1];
P.insert(make_pair(X[r][c-1],make_pair(r,c-1)));
}
}
if(r < R -1){
if(x + B[r][c] < X[r+1][c]){
X[r+1][c] = x + B[r][c];
P.insert(make_pair(X[r+1][c],make_pair(r+1,c)));
}
}
if(x + 1 < X[r + R][c]){
X[r + R][c] = x + 1;
P.insert(make_pair(X[r+R][c],make_pair(r +R,c)));
}
}
cout << X[R-1][C-1] << endl;
return(0);
}
| #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
#define rep(i,n) for(ll i=0;i<ll(n);i++)
#define REP(i,k,n) for(ll i=k;i<ll(n);i++)
#define all(a) a.begin(),a.end()
#define eb emplace_back
#define lb(v,k) (lower_bound(all(v),k)-v.begin())
#define ub(v,k) (upper_bound(all(v),k)-v.begin())
#define fi first
#define se second
#define PQ(T) priority_queue<T>
#define SPQ(T) priority_queue<T,vector<T>,greater<T>>
#define UNIQUE(a) sort(all(a));a.erase(unique(all(a)),a.end())
#define decimal cout<<fixed<<setprecision(10)
#define summon_tourist cin.tie(0);ios::sync_with_stdio(false)
using ll=long long;
using P=pair<ll,ll>;
using vi=vector<ll>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
constexpr ll inf=1001001001001001;
constexpr int INF=1001001001;
constexpr int mod=1000000007;
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';}
template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);}
void YesNo(bool b){if(b)out("Yes");else out("No");}
void yesno(bool b){if(b)out("yes");else out("no");}
void YESNO(bool b){if(b)out("YES");else out("NO");}
ll modpow(ll a,ll b){ll c=1;while(b>0){if(b&1){c=a*c%mod;}a=a*a%mod;b>>=1;}return c;}
vi calc(ll x){vi v;while(x>0){v.eb(x%10);x/=10;}reverse(all(v));return v;}
int main(){
int r,c;
cin>>r>>c;
int n=r*c;
vector<vector<P>> edge(2*n);
rep(i,n){
edge[i].eb(P(i+n,1));
edge[i+n].eb(P(i,0));
}
rep(i,r)rep(j,c-1){
int a; cin>>a;
edge[i*c+j].eb(P(i*c+j+1,a));
edge[i*c+j+1].eb(P(i*c+j,a));
}
rep(i,r-1)rep(j,c){
int b; cin>>b;
edge[i*c+j].eb(P((i+1)*c+j,b));
edge[(i+1)*c+j+n].eb(P(i*c+j+n,1));
}
vi cost(2*n,INF);
SPQ(P) q;
q.push(P(0,0));
cost[0]=0;
while(!q.empty()){
ll x=q.top().se,z=q.top().fi;
q.pop();
if(cost[x]<z) continue;
for(P y:edge[x]){
if(chmin(cost[y.fi],cost[x]+y.se)){
q.push(P(cost[y.fi],y.fi));
}
}
}
out(cost[r*c-1]);
//rep(i,2*n) outvp(edge[i]);
} |
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long; // int64_t
using ull = unsigned long long;
using pll = pair<ll,ll>;
using pii = pair<int,int>;
#define REP(i,m,n) for(int i = m; i <= (n); ++i)
#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 = 1e9+7; //10oku7
int main() {
int V, T,S,D;
cin >> V >> T >> S >> D;
if (V* T <= D && D <= V*S){
cout<< "No" << endl;
} else {
cout << "Yes" << endl;
}
}
| #include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <deque>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <cassert>
#include <iostream>
#include <stdio.h>
#include <time.h>
using namespace std;
typedef long long ll;
#define sz size()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(c) (c).begin(), (c).end()
#define rep(i,a,b) for(ll i=(a);i<(b);++i)
#define per(i,a,b) for(ll i=b-1LL;i>=(a);--i)
#define clr(a, b) memset((a), (b) ,sizeof(a))
#define ctos(c) string(1,c)
#define MOD 1000000007
ll d[20][20];
int main(){
ll h,w,a,b;
cin>>h>>w>>a>>b;
vector<ll> v;
rep(i,0,b){
v.pb(0);
}
rep(i,0,a){
v.pb(1);
}
ll ans = 0;
do{
rep(i,0,1<<a){
vector<ll> v1;
rep(j,0,a){
if((i>>j)&1){
v1.pb(0);
}
else{
v1.pb(1);
}
}
clr(d,-1);
rep(y,0,h){
rep(x,0,w){
d[y][x] = 0;
}
}
ll flag = 1;
ll index1 = 0;
ll index2 = 0;
rep(y,0,h){
rep(x,0,w){
if(d[y][x]!=0)continue;
if(v[index1]==0){
d[y][x] = 1;
}
else{
if(v1[index2]==0){
if(d[y][x+1]==0){
d[y][x] = 2;
d[y][x+1] = 2;
}
else{
flag = 0;
}
}
else{
if(d[y+1][x]==0){
d[y][x] = 2;
d[y+1][x] = 2;
}
else{
flag = 0;
}
}
index2++;
}
index1++;
}
}
if(flag==1)ans++;
}
}while(next_permutation(all(v)));
cout << ans << endl;
return 0;
} |
#include <iostream>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
int main()
{
int n;
cin >> n;
ll a[100005];
for(int i = 0; i < n; i++) cin >> a[i];
ll dp[2];
dp[0] = a[0], dp[1] = 0;
ll c[2];
c[0] = 1, c[1] = 0;
for(int i = 1; i < n; i++){
ll d[2];
d[0] = (dp[0] + dp[1] + a[i] * (c[0] + c[1])) % MOD;
d[1] = (dp[0] + MOD - a[i] * c[0] % MOD) % MOD;
swap(dp, d);
ll e[2];
e[0] = (c[0] + c[1]) % MOD;
e[1] = c[0];
swap(c, e);
}
cout << (dp[0] + dp[1]) % MOD << endl;
}
| //#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define forr(i,n) for(int i=0;i<n;i++)
#define f(i,a,b) for(ll i=a;i<b;i++)
#define fi first
#define se second
#define pb push_back
//#define mp make_pair
#define endl '\n'
#define ce(ele) cout<<ele<<' '
#define CASE(t) ll t; cin>>t; while(t--)
#define sor(v) sort(v.begin(),v.end())
#define rev(v) reverse(v.begin(),v.end())
#define vi vector<ll>
#define cs(ele) cout<<ele<<'\n'
#define pii pair<ll,ll>
#define MAXN 100001
#define all(d) d.begin(),d.end()
const ll mod=1e9 +7;
//const ll mod=998244353;
//ll Mod(ll n){return ((n%mod)+mod)%mod;}
//ll power(ll x,ll y){ll res=1;x=x;while(y>0){if(y&1)res=mod(res*x);x=mod(x*x);y=mod(y>>1);}return mod(res);}
void fast(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
ll f[200005];
ll invfact[200005];
ll power(ll x,ll y){
ll res=1;
while(y>0){
if(y&1){
res=(res*x)%mod;
}
x=(x*x)%mod;
y>>=1;
}
return res;
}
void precal(){
f[0]=1;
invfact[0]=1;
for(int i=1;i<200005;i++){
f[i]=(f[i-1]*i)%mod;
invfact[i]=power(f[i],mod-2);
}
}
ll ncr(ll n,ll k){
if(n<k||n<0||k<0){
return 0;
}
return ((f[n]*invfact[k])%mod*invfact[n-k])%mod;
}
bool prime[10000011];
void primenumbers(ll n)
{
memset(prime, true, sizeof(prime));
for (ll p=2; p*p<=n; p++)
{
if (prime[p] == true)
{
for (ll i=p*p; i<=n; i += p)
prime[i] = false;
}
}
}
ll modI(ll a, ll m)
{
ll m0=m,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 spf[MAXN];
void sieve()
{
spf[1] = 1;
for (int i=2; i<MAXN; i++)
spf[i] = i;
for (int i=4; i<MAXN; i+=2)
spf[i] = 2;
for (int i=3; i*i<MAXN; i++)
{
if (spf[i] == i)
{
for (int j=i*i; j<MAXN; j+=i)
if (spf[j]==j)
spf[j] = i;
}
}
}
vector<int> getFactorization(int x)
{
vector<int> ret;
while (x != 1)
{
ret.push_back(spf[x]);
x = x / spf[x];
}
return ret;
}
int main()
{
fast();
ll a,b;
cin>>a>>b;
double ans=(double)(a-b)/(double)a;
ans=ans*100.00;
cout<<fixed<<setprecision(9)<<ans;
return 0;
}
|
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cmath>
#include <numeric>
#include <queue>
using namespace std;
int64_t min(int64_t a,int64_t b) {
if (a > b)
{
return b ;
}else
{
return a ;
}
}
int64_t gcd(int64_t a,int64_t b){
if(a < b)
{
int64_t temp = a;
a = b ;
b = temp;
}
int64_t copya = a;
int64_t copyb = b;
int64_t r = a%b ;
while (r != 0)
{
a = b ;
b = r ;
r = a%b ;
}
return b ;
}
int solve(string s, int start){
if (start >= s.size())
{
return 100000000 ;
}
string at = "atcoder" ;
if (start >= at.size())
{
return 0 ;
}
int l = 100000000 ;
int same = 100000000 ;
for (int i = start; i < s.size(); i++)
{
if (l == 100000000)
{
if (s.at(i) > at.at(start))
{
l = i -start ;
break ;
}
}
if (same == 100000000)
{
if (s.at(i) == at.at(start))
{
same = i - start ;
}
}
}
if (same != 100000000)
{
for (int i = 0; i < same; i++)
{
swap(s.at(same+start -i),s.at(same+start -i-1)) ;
}
}
if(l != 100000000 && same != 100000000)
{
return min(l,solve(s,start+1)+same) ;
}else if (same == 100000000)
{
return l ;
}else
{
return solve(s,start+1)+same ;
}
}
int main(){
string at = "atcoder" ;
int t ;
cin >> t ;
for (int cases = 0; cases < t; cases++)
{
string s ;
cin >> s ;
string copy = s ;
sort(copy.begin(),copy.end()) ;
reverse(copy.begin(),copy.end()) ;
if (at >= copy)
{
cout << -1 << endl ;
continue ;
}
int ans = solve(s,0) ;
cout << ans << endl ;
}
}
| #include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define ull unsigned long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define vc vector<char>
#define vs vector<string>
#define vpii vector<pii>
#define vpll vector<pll>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; i++)
#define rep1(i, n) for (int i = 1, i##_len = (n); i <= i##_len; i++)
#define repr(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rep1r(i, n) for (int i = ((int)(n)); i >= 1; i--)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(all(v));
#define RSORT(v) sort(rall(v));
#define pb push_back
#define mp make_pair
#define INF (1e9)
#define PI (acos(-1))
#define EPS (1e-7)
ull gcd(ull a, ull b) { return b ? gcd(b, a % b) : a; }
ull lcm(ull a, ull b) { return a / gcd(a, b) * b; }
ll comb(int n, int r) {
if (r * 2 > n) r = n - r;
ll ans = 1;
for (int i = 1; i <= r; ++i) {
ans *= n - i + 1;
ans /= i;
}
return ans;
}
void solve(long long L) { cout << comb(L - 1, 11) << endl; }
int main() {
// cout << fixed << setprecision(5);
long long L;
scanf("%lld", &L);
solve(L);
return 0;
}
|
#include <stdio.h>
#include <string>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n); i++)
using ll = long long;
using P = pair<int, int>;
vector<vector<int>> to;
vector<int> used, color, order;
int now;
void dfs(int i){
if(used[i] == 1) return;
used[i] = 1;
order.push_back(i);
for(int s: to[i]) dfs(s);
}
void color_dfs(int i){
if(i == order.size()){
// for(int c: color) cout << c << " ";
// cout << endl;
now++;
return;
}
int o = order[i];
rep(c,3){
bool ok = true;
for(int s: to[o]) if(c == color[s]) ok = false;
if(ok){
color[o] = c;
color_dfs(i+1);
}
}
color[o] = -1;
return;
}
int main()
{
int n, m;
cin >> n >> m;
to = vector<vector<int>>(n);
int a, b;
rep(i,m){
cin >> a >> b;
a--; b--;
to[a].push_back(b);
to[b].push_back(a);
}
long long ans = 1;
used = vector<int>(n,0);
color = vector<int>(n,-1);
rep(i,n){
if(used[i] == 1) continue;
// used[i] = 1;
order = vector<int>();
dfs(i);
// cout << "dfs is finished" << endl;
// cout << "order" << endl;
// for(int o: order) cout << o << " ";
// cout << endl;
now = 0;
color[i] = 0;
color_dfs(1);
ans *= 3*now;
}
cout << ans << endl;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using pii = pair<int, int>;
int main() {
int h, w;
cin >> h >> w;
vector<string> s(h);
rep(i, h) cin >> s[i];
int ans = 0;
rep(i, h) {
rep(j, w - 1) {
if (s[i][j] == '.' && s[i][j + 1] == '.') ans++;
}
}
rep(j, w) {
rep(i, h - 1) {
if (s[i][j] == '.' && s[i + 1][j] == '.') ans++;
}
}
cout << ans << endl;
return 0;
} |
#include <bits/stdc++.h>
using i32 = std::int32_t;
using i64 = std::int64_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
using usize = std::size_t;
const i32 INF = 1001001001;
const i64 LINF = 1001001001001001;
const u32 MOD1 = 1000000007;
const u32 MOD2 = 998244353;
using namespace std;
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
i64 k;
string s,t;
cin>>k>>s>>t;
vector<i64> r(9,k);
vector<i64> taka(9,0),aoki(9,0);
for(i64 i=0;i<4;i++){
r[(s[i]-'0')-1]--;
r[(t[i]-'0')-1]--;
taka[(s[i]-'0')-1]++;
aoki[(t[i]-'0')-1]++;
}
i64 tp=0,ap=0;
for(i64 i=0;i<9;i++){
tp+=(i+1)*pow(10,taka[i]);
ap+=(i+1)*pow(10,aoki[i]);
}
i64 sum=9*k-8;
double ans=0;
for(i64 i=0;i<9;i++){
if(r[i]==0)continue;
for(i64 j=0;j<9;j++){
if(r[j]==0||(i==j&&r[j]<=1))continue;
i64 ntp=tp-(i+1)*pow(10,taka[i]);
i64 nap=ap-(j+1)*pow(10,aoki[j]);
ntp+=(i+1)*pow(10,taka[i]+1);
nap+=(j+1)*pow(10,aoki[j]+1);
if(ntp>nap){
ans+=((double)(r[i])/sum)*((double)((i!=j?r[j]:r[j]-1))/(sum-1));
}
}
}
cout<<fixed<<setprecision(12)<<ans<<endl;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, n) for (ll i = s; i < (ll)(n); i++)
// DFSの実装例
bool vis[21];
ll par[21];
map<ll, vector<ll>> edges;
stack<ll> st;
vector<ll> con;
vector<vector<ll>> cons;
void dfs(ll num) {
vis[num] = true;
con.push_back(num);
auto es = edges[num];
for (auto e : es) {
if (!vis[e]) {
st.push(e);
vis[e] = true;
par[e] = num;
}
}
while (!st.empty()) {
auto top = st.top();
st.pop();
dfs(top);
}
};
int main() {
ll N, M;
cin >> N >> M;
rep(i, 0, M) {
ll ai, bi;
cin >> ai >> bi;
edges[ai].push_back(bi);
edges[bi].push_back(ai);
}
vis[0] = true;
rep(i, 1, N + 1) {
if (!vis[i]) {
con = {};
dfs(i);
cons.push_back(con);
}
}
ll ans = 1;
for (auto co : cons) {
map<ll, ll> col;
ll comb = 0;
col[co[0]] = 1; // 1 for red, 2 for green, 3 for blue;
rep(bit, 0, 1 << (co.size() - 1)) {
for (ll i = 1; i < (ll)co.size(); ++i) {
ll flag = bit & (1 << (co.size() - 1 - i));
if (col[par[co[i]]] == 1) {
col[co[i]] = flag ? 3 : 2;
} else if (col[par[co[i]]] == 2) {
col[co[i]] = flag ? 3 : 1;
} else if (col[par[co[i]]] == 3) {
col[co[i]] = flag ? 2 : 1;
}
}
bool abl = true;
for (ll i = 0; i < (ll)co.size(); ++i) {
for (auto edge : edges[co[i]]) {
if (col[edge] == col[co[i]]) {
abl = false;
break;
}
if (abl == false)
break;
}
}
if (abl) {
comb += 1;
}
}
comb *= 3;
ans *= comb;
}
cout << ans << endl;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.