code_file1
stringlengths 87
4k
| code_file2
stringlengths 85
4k
|
---|---|
//#pragma GCC optimize("Ofast")
#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;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//~ while (clock()<=69*CLOCKS_PER_SEC)
#define ll long long
#define ld long double
#define pi pair<int,int>
#define pd pair<ld,ld>
#define ft first
#define sd second
#define pb push_back
#define eb emplace_back
#define FOR(i,a,b) for(int i=(a); i<=(b);i++)
#define F(i,a,b) FOR(i,(a),(b)-1)
#define REV(i,a,b) for(int i=(a); i>=(b);i--)
#define VI vector<int>
#define VPI vector<pi>
#define VPD vector<pd>
#define PI 3.14159265
#define all(x) (x).begin(), (x).end()
#define sz(a) (int)((a).size())
#define int long long
template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";}
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...);
}
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const long long K = 1000;
const long long KK = K*K;
const long long MIL = KK*K;
const long long INF = MIL*MIL;
const long long MOD = 1e9 + 7;
const long long N = 1e5 + 10, M=310;
const int base = 1 << 18;
int gcd(int a, int b, int& x, int& y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
int x1, y1;
int d = gcd(b, a % b, x1, y1);
x = y1;
y = x1 - y1 * (a / b);
return d;
}
void solve() {
int n, s, k;
int res = 0;
cin >> n >> s >> k;
if (__gcd(n, k) != 1) {
int tmp = __gcd(n, k);
if (s % tmp) {
cout << "-1\n";
return;
}
}
int tmp = (n - s) / k;
res += tmp;
s += tmp * k;
s %= n;
if (s == 0) {
cout << res << "\n";
return;
}
int x = 1, y = 1;
gcd(n, k, x, y);
int nww = ((n * k) / __gcd(n, k));
while( y < 0 ) {
x -= nww / n;
y += nww / k;
}
res += (n - s) / __gcd(n, k) * y;
s = 0;
int cofaj = nww / k;
res %= cofaj;
cout << res << "\n";
}
int32_t main() {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
// cout.tie(0);
cerr.tie(0);
cout << setprecision(9) << fixed;
cerr << setprecision(20) << fixed;
int test = 1, f;
cin >> test;
F(_test, 0, test) {
//cout<<"Case #"<<_test + 1<<": ";
solve();
// if(_test == 1)
// return 0;
}
}
/*
*/ | #define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <complex>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#include <queue>
#define INF 1010101010LL
#define INFLL 1010101010101010101LL
using namespace std;
int mod = 1000000007;
//int mod = 998244353;
long long extgcd(long long a, long long b, long long &x, long long &y)
{
long long d = a;
if (b != 0) {
d = extgcd(b, a % b, y, x);
y -= a / b * x;
} else {
x = 1;
y = 0;
}
return d;
}
int main()
{
int t;
cin >> t;
for (int i = 0; i < t; i++) {
long long n, s, k;
cin >> n >> s >> k;
long long x, y;
long long d = extgcd(k, n, x, y);
if (s % d != 0) {
cout << -1 << endl;
continue;
}
n /= d;
s /= d;
k /= d;
long long ans = ((x * -s) % n + n) % n;
cout << ans << endl;
}
return 0;
} |
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
#define int long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define range(i,a,b) ((a)<=(i) && (i)<(b))
#define debug(x) cout << #x << " = " << (x) << endl;
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define SP << " " <<
typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef vector<int> vec;
typedef vector<P> pvec;
typedef vector<vector<int>> vvec;
typedef vector<vector<P>> pvvec;
typedef priority_queue<int> PQI;
typedef priority_queue<P> PQP;
typedef priority_queue<int,vector<int>,greater<int>> PQIG;
typedef priority_queue<P,vector<P>,greater<P>> PQPG;
const vector<int> DX = {0, -1, 0, 1, 1, 1, -1, -1};
const vector<int> DY = {1, 0, -1, 0, 1, -1, 1, -1};
constexpr int MOD = (1000000007);
// const int MOD = (998244353);
// const int INF = (1 << 30); // 1073741824
const ll INF = (1LL << 60); // 1152921504606846976
const double PI = (3.141592653589794);
const double EPS = (0.0000000001); // 10^(-10)
template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return 1;} return 0;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return 1;} return 0;}
template<class T> inline T ceil(T a, T b) {return T((a + b - 1) / b);}
template<class T> inline T round(T a, T b) {return T(a / b);}
template< typename T1, typename T2 > istream &operator>>(istream &is, pair< T1, T2 > &p) { is >> p.first >> p.second; return is; }
template <class T> inline void out(T &a) { bool flag=true; for(auto&x:a){if(flag) {flag=false;} else{ cout << ' '; } cout<<x; } cout << endl; }
//----------------------------------------------------------------
int nmax=200000; // 2*(10^5)
vvec G(nmax);
void solve4ts()
{
string s;
cin>>s;
int n=s.size();
rep(i,n){
if(s[i]=='.') break;
cout<<s[i];
}
cout<<endl;
}
//-----------------------------------------------------------------
signed main(){ ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(15);
int repeat = 1;
// cin >> repeat;
while(repeat--) solve4ts();
}
/*
g++ -std=c++1z code.cpp
./a.out
python3 expander.py code.cpp
*/ | #include<bits/stdc++.h>
#define st string
#define sz(x) (x).size()
#define pb push_back
#define pp pop_back
using namespace std;
int main()
{
st s;
cin >> s;
while (s[sz(s) - 1] == '0')s.pp();
for (int i = 0; i <sz(s) / 2; i++) {
if(s[i] != s[sz(s) -1 -i]){
cout << "No";
return 0;
}
}
cout << "Yes";
return 0;
}
|
#define _DEBUG
#include "bits/stdc++.h"
//#include <atcoder/all>
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0,a1,a2,a3,a4,a5,x,...) x
#define debug_1(x1) cout<<#x1<<": "<<x1<<endl
#define debug_2(x1,x2) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<endl
#define debug_3(x1,x2,x3) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<endl
#define debug_4(x1,x2,x3,x4) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<endl
#define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<", "#x5<<": "<<x5<<endl
#define debug_6(x1,x2,x3,x4,x5,x6) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<", "#x5<<": "<<x5<<", "#x6<<": "<<x6<<endl
#ifdef _DEBUG
#define debug(...) CHOOSE((__VA_ARGS__,debug_6,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__)
#else
#define debug(...)
#endif
#define rep(index,num) for(int index=0;index<(int)num;index++)
#define rep1(index,num) for(int index=1;index<=(int)num;index++)
#define brep(index,num) for(int index=(int)num-1;index>=0;index--)
#define brep1(index,num) for(int index=(int)num;index>0;index--)
#define scan(argument) cin>>argument
#define prin(argument) cout<<argument<<endl
#define prind(argument) cout<<std::fixed<<setprecision(13)<<argument<<endl
#define kaigyo cout<<endl
#define eps 1e-7
#define PI acosl(-1)
#define mp(a1,a2) make_pair(a1,a2)
#define ALL(a) (a).begin(),(a).end()
#define rALL(a) (a).rbegin(),(a).rend()
#define puba emplace_back
#define pubamp(a,b) emplace_back(mp(a,b))
typedef long long ll;
typedef long double ld;
using namespace std;
//using namespace atcoder;
typedef pair<ll,ll> pll;
typedef pair<int,int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<vint> vvint;
typedef vector<vll> vvll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
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; }
ll INFl=(ll)1e+18+1;
int INF=1e+9+1;
int main(){
int N;
int x1[101],y1[101],x2[101],y2[101];
scan(N);
rep(i,N){
scan(x1[i]>>y1[i]);
}
rep(i,N){
scan(x2[i]>>y2[i]);
}
if(N==1){
prin("Yes");
return 0;
}
int a01x=x1[1]-x1[0],a01y=y1[1]-y1[0];
map<pint,int> acount;
rep(i,N){
int atox=x1[i]-x1[0],atoy=y1[i]-y1[0];
int naiseki=a01x*atox+a01y*atoy;
int gaiseki=a01x*atoy-atox*a01y;
acount[mp(naiseki,gaiseki)]++;
}
rep(ii,N){
rep(jj,N){
if(ii==jj) continue;
int b01x=x2[ii]-x2[jj],b01y=y2[ii]-y2[jj];
map<pint,int> bcount;
rep(i,N){
int btox=x2[i]-x2[jj],btoy=y2[i]-y2[jj];
int naiseki=b01x*btox+b01y*btoy;
int gaiseki=b01x*btoy-btox*b01y;
bcount[mp(naiseki,gaiseki)]++;
}
bool ok=1;
for(const auto& fs:acount){
if(bcount[fs.first]!=fs.second){
ok=0;
}
}
if(ok){
prin("Yes");
return 0;
}
}
}
prin("No");
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
using vec_int = vector<int>;
using P = pair<int,int>;
using T = tuple<int,int,int>;
using ll = long long;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
int charToInt(char c){
char zero_num = '0';
return (int)c - (int)zero_num;
}
void cout_line(vector<int> &a){
for(int i=0;i<a.size();i++){
if(i<a.size()-1){
cout<<a.at(i)<<" ";
}else{
cout<<a.at(i)<<endl;
}
}
}
signed main(){
int N; cin>>N;
int M; cin>>M;
vec_int A(M);
vec_int B(M);
vec_int C(M);
vec_int D(M);
rep(i,M)cin>>A.at(i)>>B.at(i)>>C.at(i)>>D.at(i);
vector<vector<T>> G(N+1);
rep(i,M){
G.at(A.at(i)).push_back(make_tuple(B.at(i), C.at(i), D.at(i)));
G.at(B.at(i)).push_back(make_tuple(A.at(i), C.at(i), D.at(i)));
}
vec_int visited(N+1, -1);
priority_queue<P, vector<P>, greater<P>> pq;
pq.emplace(0, 1);
while(!pq.empty()){
int cost, pos; tie(cost, pos) = pq.top(); pq.pop();
if(visited.at(pos)>=0)continue;
visited.at(pos) = cost;
if(pos==N){
cout<<cost<<endl;
return 0;
}
for(auto temp: G.at(pos)){
int next, c, d; tie(next, c, d) = temp;
if(visited.at(next)>=0)continue;
int t = (sqrt((double)(1+4*d)) -1 )/2 + 1;
int next_time;
if(t+5<cost){
next_time = cost+c+(int)(d/(cost+1));
}else{
next_time = (int)pow(10,18);
for(int i=t-5;i<=t+5;i++){
if(i<cost)continue;
int temp = cost+c+(int)(d/(i+1))+(i-cost);
next_time = min(temp, next_time);
}
}
pq.emplace(next_time, next);
}
}
cout<<-1<<endl;
return 0;
} |
#include <bits/stdc++.h>
#define rep(a,n) for (ll a = 0; a < (n); ++a)
using namespace std;
//using namespace atcoder;
using ll = long long;
typedef pair<ll,ll> P;
typedef pair<ll,P> PP;
typedef vector<vector<int> > Graph;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a >= b) { a = b; return 1; } return 0; }
const ll INF = 1e18;
#define debug(v) cout<<#v<<": ",prt(v);
template <typename A,typename B>
inline void prt(pair<A,B> p){cout<<"("<<p.first<<", "<<p.second<<")\n";}
template <typename A,typename B,typename C>
inline void prt(tuple<A,B,C> p){cout<<"("<<get<0>(p)<<", "<<get<1>(p)<<", "<<get<2>(p)<<")\n";}
inline void prt(bool p){if(p)cout<<"True"<<'\n';else cout<<"False"<<'\n';}
template <typename T>
inline void prt(vector<T> v){cout<<'{';for(ll i=0;i<v.size();i++){cout<<v[i];if(i<v.size()-1)cout<<", ";}cout<<'}'<<'\n';}
template<typename T>
inline void prt(vector<vector<T> >& vv){ for(const auto& v : vv){ prt(v); } }
template <typename T>
inline void prt(deque<T> v){cout<<'{';for(ll i=0;i<v.size();i++){cout<<v[i];if(i<v.size()-1)cout<<", ";}cout<<'}'<<'\n';}
template <typename A,typename B>
inline void prt(map<A,B> v){cout<<'{';ll c=0;for(auto &p: v){cout<<p.first<<":"<<p.second;c++;if(c!=v.size())cout<<", ";}cout<<'}'<<'\n';}
template <typename A,typename B>
inline void prt(unordered_map<A,B> v){cout<<'{';ll c=0;for(auto &p: v){cout<<p.first<<":"<<p.second;c++;if(c!=v.size())cout<<", ";}cout<<'}'<<'\n';}
template <typename T>
inline void prt(set<T> v){cout<<'{';for(auto i=v.begin();i!=v.end();i++){cout<<*i;if(i!=--v.end())cout<<", ";}cout<<'}'<<'\n';}
template <typename T>
inline void prt(multiset<T> v){cout<<'{';for(auto i=v.begin();i!=v.end();i++){cout<<*i;if(i!=--v.end())cout<<", ";}cout<<'}'<<'\n';}
ll n;
vector<ll>c;
Graph g;
multiset<ll>st;
vector<ll>ans;
void dfs(ll v,ll p){
auto it = st.find(c[v]);
if(it==st.end()){
ans.push_back(v);
}
st.insert(c[v]);
for(auto x:g[v]){
if(x==p)continue;
dfs(x,v);
}
st.erase(st.find(c[v]));
return;
}
int main(){
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
cin >> n;
c.resize(n);
rep(i,n)cin>>c[i];
g.resize(n);
rep(i,n-1){
ll a,b;
cin >> a >> b;
a--;b--;
g[a].push_back(b);
g[b].push_back(a);
}
dfs(0,-1);
sort(ans.begin(),ans.end());
rep(i,ans.size()){
cout << ans[i]+1 << endl;
}
return 0;
} | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
int arr[100005];
bool vis[100005];
vector<int>vec;
multiset<int>ms;
void dfs(int node,vector<vector<int>>&v)
{
vis[node]=1;
if(!ms.count(arr[node-1]))
vec.push_back(node);
ms.insert(arr[node-1]);
for(int i=0;i<v[node].size();i++){
if(!vis[v[node][i]])
dfs(v[node][i],v);
}
ms.erase(ms.find(arr[node-1]));
}
int main()
{
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
cin>>n;
for(int i=0; i<n; i++)
cin>>arr[i];
vector<vector<int>>v(n+5);
for(int i=0; i<n-1; i++) {
int a,b;
cin>>a>>b;
v[a].push_back(b);
v[b].push_back(a);
}
dfs(1,v);
sort(vec.begin(),vec.end());
for(auto it:vec)
cout<<it<<"\n";
}
|
//...Bismillahir Rahmanir Rahim...
// Code by Asad Bin Saber
#include <bits/stdc++.h>
using namespace std;
// typedefs...
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<ll, ll> pll;
// constants...
const double PI = acos(-1);
const int MOD = 1e9+7; // 998244353;
const int MXS = 2e5+5;
const int MXI = 1e9+5;
const ll MXL = 1e15+5;
// defines...
#define MP make_pair
#define PB push_back
#define fi first
#define se second
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define sl(a) scanf("%lld", &a)
#define sll(a, b) scanf("%lld%lld", &a, &b)
#define slll(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
// chess moves...
// int dx[] = {+0, +0, -1, +1}; ///Up-down, Left-Right
// int dy[] = {+1, -1, +0, +0};
// int dx[] = {+0,+0,+1,-1,-1,+1,-1,+1}; ///King's Move
// int dy[] = {-1,+1,+0,+0,+1,+1,-1,-1};
// int dx[] = {-2, -2, -1, -1, 1, 1, 2, 2}; ///Knight's Move
// int dy[] = {-1, 1, -2, 2, -2, 2, -1, 1};
// structs...
struct hash_pair {
template <class T1, class T2>
size_t operator()(const pair<T1, T2>& p) const
{
auto hash1 = hash<T1>{}(p.first);
auto hash2 = hash<T2>{}(p.second);
return hash1 ^ hash2;
}
};
// functions...
ll gcd(ll a, ll b) {
while (b) {
a %= b;
swap(a, b);
}
return a;
}
ll lcm(ll a, ll b)
{
return (a/gcd(a, b)*b);
}
ll comb(ll a, ll b)
{
ll x = max(a-b, b), ans=1;
for(ll K=a, L=1; K>=x+1; K--, L++){
ans *= K;
ans /= L;
}
return ans;
}
ll ans = 0;
vi ara;
void solve(ll i, ll num, ll cnt)
{
if(num%3==0){
ans = max(ans, cnt);
}
if(i==sz(ara)) return;
solve(i+1, num*10+ara[i], cnt+1);
solve(i+1, num, cnt);
}
void task()
{
// Code here...
ll n;
cin >> n;
while(n){
ara.PB(n%10);
n /= 10;
}
reverse(all(ara));
solve(0, 0, 0);
if(0==ans) cout << "-1\n";
else cout << sz(ara)-ans << "\n";
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
task();
return 0;
}
| // #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 _rep(i,n) for(ll i=n-1;i>=1;i--)
#define scn(a) scanf("%lld",&a)
#define scns(a,b) scanf("%lld %lld",&a,&b)
#define print(a) printf("%lld",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 yeS(GOOD) GOOD ? cout<<"YES\n" : cout<<"NO\n"
#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=20000;
long long n;
vec v;
ll dp[20][20][N],k;
ll f(ll i, ll j, ll sum=0)
{
// cout<<sum<<endl;
if(i==(int)v.size() && j<=k-1)
{
// cout<<sum<<endl;
if(sum%3==0 && sum)
return 0;
return 1e9;
}
if(i==(int)v.size() || j>k-1)
return 1e9;
if(dp[i][j][sum]!=-1)
return dp[i][j][sum];
ll ans=0;
ans=min(f(i+1,j+1,sum)+1,f(i+1,j,sum+v[i]));
return dp[i][j][sum]=ans;
}
void solve()
{
cin>>n;
// cout<<n<<endl;
v.clear();
mem(dp,-1);
while(n)
{
int dig=(int)(n%10);
v.pb(dig);
n/=10;
k++;
}
ll ans=f(0,0);
if(ans>=1e9) ans=-1;
cout<<ans<<endl;
}
int main()
{
int queries=1;
// cin>>queries;
while(queries--)
{
solve();
}
} |
/*
Author: QAQAutomaton
Lang: C++
Code: C.cpp
Mail: lk@qaq-am.com
Blog: https://www.qaq-am.com/
*/
#include<bits/stdc++.h>
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define DEBUG printf("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#define Debug debug("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#define all(x) x.begin(),x.end()
#define x first
#define y second
using namespace std;
typedef unsigned uint;
typedef long long ll;
typedef unsigned long long ull;
typedef complex<double> cp;
typedef pair<int,int> pii;
int inf;
const double eps=1e-8;
const double pi=acos(-1.0);
template<class T,class T2>int chkmin(T &a,T2 b){return a>b?a=b,1:0;}
template<class T,class T2>int chkmax(T &a,T2 b){return a<b?a=b,1:0;}
template<class T>T sqr(T a){return a*a;}
template<class T,class T2>T mmin(T a,T2 b){return a<b?a:b;}
template<class T,class T2>T mmax(T a,T2 b){return a>b?a:b;}
template<class T>T aabs(T a){return a<0?-a:a;}
template<class T>int dcmp(T a,T b){return a>b;}
template<int *a>int cmp_a(int x,int y){return a[x]<a[y];}
#define min mmin
#define max mmax
#define abs aabs
struct __INIT__{
__INIT__(){
fill((unsigned char *)(&inf),(unsigned char *)(&inf)+sizeof(inf),0x3f);
}
}__INIT___;
namespace io {
const int SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr;
// getchar
#define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
// print the remaining part
inline void flush () {
fwrite (obuf, 1, oS - obuf, stdout);
oS = obuf;
}
// putchar
inline void putc (char x) {
*oS ++ = x;
if (oS == oT) flush ();
}
template<typename A>
inline bool read (A &x) {
for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;else if(c==EOF)return 0;
for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
return 1;
}
inline bool read (char &x) {
while((x=gc())==' '||x=='\n' || x=='\r');
return x!=EOF;
}
inline bool read(char *x){
while((*x=gc())=='\n' || *x==' '||*x=='\r');
if(*x==EOF)return 0;
while(!(*x=='\n'||*x==' '||*x=='\r'||*x==EOF))*(++x)=gc();
*x=0;
return 1;
}
template<typename A,typename ...B>
inline bool read(A &x,B &...y){
return read(x)&&read(y...);
}
template<typename A>
inline bool write (A x) {
if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;
while (x) qu[++ qr] = x % 10 + '0', x /= 10;
while (qr) putc (qu[qr --]);
return 0;
}
inline bool write (char x) {
putc(x);
return 0;
}
inline bool write(const char *x){
while(*x){putc(*x);++x;}
return 0;
}
inline bool write(char *x){
while(*x){putc(*x);++x;}
return 0;
}
template<typename A,typename ...B>
inline bool write(A x,B ...y){
return write(x)||write(y...);
}
//no need to call flush at the end manually!
struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
}
using io :: read;
using io :: putc;
using io :: write;
int s[1<<8|5],atl[1<<8|5];
pii e[100005];
int g[9][9];
int dis[9][9],p[9];
int n,m;
signed main(){
#ifdef QAQAutoMaton
freopen("C.in","r",stdin);
freopen("C.out","w",stdout);
#endif
read(n,m);
int mx=0;
for(int i=0;i<n;++i){
read(s[1<<i]);
chkmax(mx,s[1<<i]);
}
for(int i=1;i<(1<<n);++i)if(i&(i-1))s[i]=s[i&(i-1)]+s[i&(-i)];
for(int i=1;i<=m;++i){
read(e[i].y,e[i].x);
if(mx>e[i].x)return write("-1\n");
}
sort(e+1,e+m+1);
for(int i=1;i<=m;++i)chkmax(e[i].y,e[i-1].y);
for(int i=0;i<(1<<n);++i){
atl[i]=(lower_bound(e+1,e+m+1,make_pair(s[i],-1))-1)->y;
}
for(int i=1;i<=n;++i)p[i]=i-1;
int ans=inf;
do{
for(int i=1;i<=n;++i){
int w=0;
for(int j=i;j;--j){
w|=1<<p[j];
dis[j][i]=-atl[w];
}
for(int j=i+1;j<=n;++j)dis[j][i]=inf;
}
for(int k=1;k<=n;++k)
for(int i=1;i<=n;++i)for(int j=1;j<=n;++j)chkmin(dis[i][j],dis[i][k]+dis[k][j]);
chkmin(ans,-dis[1][n]);
}while(next_permutation(p+1,p+n+1));
write(ans,'\n');
return 0;
}
| // Pratiyush Mishra
#include <bits/stdc++.h>
#define ull unsigned long long int
#define ll long long int
#define LL_MAX 9223372036854775807
#define pb push_back
#define pf push_front
#define mp make_pair
#define popb pop_back
#define vl vector<ll>
#define bs(v, x) binary_search(v.begin(), v.end(), x)
#define popf pop_front
#define p() cout << '\n'
#define p0(x) cout << x << " "
#define p1(x) cout << x << '\n'
#define p2(x, y) cout << x << " " << y << '\n'
#define p3(x, y, z) cout << x << " " << y << " " << z << '\n'
#define printv(v) \
for (ll i = 0; i < v.size(); ++i) \
cout << v[i] << " "; \
cout << '\n'
#define pr1(x) cout << fixed << setprecision(15) << x << '\n'
#define mod 1000000007
#define mod1 998244353
#define fio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL)
#define get(n) \
ll n; \
cin >> n
#define getvec(v, n) \
vector<ll> v(n); \
for (ll i = 0; i < n; i++) \
cin >> v[i];
#define getstr(s) \
string s; \
cin >> s
#define all(x) x.begin(), x.end()
#define countBits(x) __builtin_popcount(x)
using namespace std;
void mainSolve()
{
get(n);
get(m);
getvec(wt, n);
sort(all(wt));
vector<ll> sum(1 << n, 0), dis(1 << n, 0);
for (int i = 0; i < (1 << n); i++)
{
int x = 0;
for (int j = 0; j < n; j++)
if (i >> j & 1)
x += wt[j];
sum[i] = x;
}
int need = *max_element(all(wt));
bool flag = true;
// for (int i = 0; i < m; i++)
// {
// int l, v;
// cin >> l >> v;
// if (v < need)
// {
// flag = false;
// }
// for (int j = 0; j < (1 << n); j++)
// {
// if (sum[j] > v)
// dis[j] = max(dis[j], l);
// }
// }
for (int j = 0; j < m; j++)
{
get(l);
get(w);
if (w < need)
flag = false;
for (int i = 0; i < (1 << n); i++)
{
if (sum[i] > w)
dis[i] = max(dis[i], l);
}
}
if (!flag)
{
p1(-1);
return;
}
ll ans = 1e15;
vl per(n);
iota(all(per), 0);
do
{
vl pos(n, 0);
for (int i = 1; i < n; i++)
{
ll s = (1 << per[i]);
for (int j = i - 1; j >= 0; j--)
{
s |= (1 << per[j]);
pos[i] = max(pos[i], pos[j] + dis[s]);
}
}
ans = min(ans, pos[n - 1]);
} while (next_permutation(all(per)));
p1(ans);
}
int main()
{
fio;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
//get(t);
ll t = 1;
while (t--)
{
mainSolve();
}
return 0;
}
|
/*
written by Pankaj Kumar.
country:-INDIA
Institute: National Institute of Technology, Uttarakhand
*/
#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 unsigned long long ull;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
#define speed cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);
/* Abbrevations */
#define ff first
#define ss second
#define mp make_pair
#define line cout<<endl;
#define pb push_back
#define Endl "\n"
// loops
#define forin(arr,n) for(ll i=0;i<n;i++) cin>>arr[i];
// Some print
#define no cout<<"No"<<endl;
#define yes cout<<"Yes"<<endl;
// sort
#define all(V) (V).begin(),(V).end()
#define srt(V) sort(all(V))
#define srtGreat(V) sort(all(V),greater<ll>())
// some extra
#define printv(v) for(ll i=0;i<ll(v.size());i++){cout<<v[i]<<" ";} line;
#define precision(x) cout<<fixed<<setprecision(x);
#define sz(V) ll(V.size())
// template
template <typename T>
T mymax(T x,T y)
{
return (x>y)?x:y;
}
// function
ll power(ll x,ll y,ll mod)
{
ll res=1;
// x=x%mod;
while(y>0)
{
if(y%2==1)
{
res*=x;
// res=res%mod;
}
y/=2; x*=x; // x=x%mod;
}
return res;
}
ll str_to_num(string s)
{
return stoi(s);
}
string num_to_str(ll num)
{
return to_string(num);
}
// datatype definination
#define ordered_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
class Point
{
public:
ll x;
ll y;
ll z;
ll getsum()
{
return x+y+z;
}
};
/* ascii value
A=65,Z=90,a=97,z=122
*/
/* Some syntax
//Syntax to create a min heap for priority queue
//priority_queue <int, vector<int>, greater<int>>pq;
*/
/* --------------------MAIN PROGRAM----------------------------*/
// to run ctrl+b
const ll INF=1e18;
const ll mod1=1e9+7;
const ll mod2=998244353;
// Techniques :
// divide into cases, brute force, pattern finding
// sort, greedy, binary search, two pointer
// transform into graph
// Experience :
// Cp is nothing but only observation and mathematics.
vector<int>aa[200001];
int vis[200001];
void dfs(int node, int par, int& dis){
vis[node]=1;
dis++;
for(auto child:aa[node]){
if(vis[child]==0 and child!=par){
dfs(child, node, dis);
}
}
}
ll solve()
{
int n;
cin>>n;
int a[n];
for(ll i=0;i<n;i++)
cin>>a[i];
vector<pair<int, int>>v;
set<int>s;
for(int i=0;i<n/2;i++){
if(a[i]!=a[n-1-i]){
v.pb({a[i], a[n-1-i]});
s.insert(a[i]);
s.insert(a[n-1-i]);
}
}
if(v.size()==0){
cout<<0<<endl;
return 0;
}
for(auto x : v){
aa[x.ff].pb(x.ss);
aa[x.ss].pb(x.ff);
}
int ans =0;
for(auto i:s){
if(vis[i]==0){
int d = 0;
dfs(i, 0, d);
// cout<<d<<endl;
ans += max(0, (d-1));
}
}
cout<<ans<<endl;
return 0;
}
int main()
{
speed;
/* #ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif */
ll TestCase=1;
// cin>>TestCase;
while(TestCase--)
{
solve();
}
}
/* -----------------END OF PROGRAM --------------------*/
/*
* stuff you should look before submission
* constraint and time limit
* int overflow
* special test case (n=0||n=1||n=2)
* don't get stuck on one approach if you get wrong answer
*/ | #include <bits/stdc++.h>
#define sz(c) int(c.size())
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define per(i, a, b) for (int i = (b)-1; i >= (a); --i)
using namespace std;
using ll = long long;
#ifdef LOCAL
#include <local/debug.h>
#else
#define debug(...) (void)0
#endif
struct DSU {
vector<int> r;
DSU(int n) : r(n, -1) {}
int get(int x) { return r[x] < 0 ? x : r[x] = get(r[x]); }
int size(int x) { return -r[get(x)]; }
bool join(int x, int y) {
x = get(x), y = get(y);
if (x == y)
return false;
if (r[x] > r[y])
swap(x, y);
r[x] += r[y];
r[y] = x;
return true;
}
};
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
int n;
cin >> n;
vector<int> a(n);
rep(i, 0, n) cin >> a[i];
int const maxa = 200001;
DSU dsu(maxa);
rep(i, 0, n / 2) dsu.join(a[i], a[n - i - 1]);
ll res = 0;
rep(i, 0, maxa) if (i == dsu.get(i)) res += dsu.size(i) - 1;
cout << res << "\n";
}
|
#include<bits/stdc++.h>
#define w(x) int x; cin>>x; for(int tc=1;tc<=x;tc++)
#define trace(x) cerr<<#x<<": "<<x<<" "<<endl;
#define trace1(x,y) cerr<<#x<<": "<<x<<" "<<endl;cerr<<#y<<": "<<y<<" "<<endl;
#define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long
#define pb push_back
#define endl "\n"
#define mod 1000000007
#define inp(x) cin>>x;
#define f first
#define s second
#define inf 1e18
using namespace std;
void solve(int tc) {
int a, b, c;
cin >> a >> b >> c;
cout << a - b + c << endl;
}
int32_t main() {
FIO
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
// w(x) {
solve(1);
// }
return 0;
} | #pragma GCC optimize("Ofast")
#include <bits/stdc++.h> // -_-bruh u copying mah code?!
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace std;
#define lli long long int
#define i int
#define cr char
#define f float
#define uns unsigned
#define input(x) cin >>x
#define output(x) cout <<x
#define space() cout <<endl
#define gc getchar_unlocked
#define vi(x) vector<x>
#define loop(I,N) for(lli I=0;I<N;I++)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define PI 3.1415926535897932384626
const i mod = 1000000007;
#define Fill(a,v) memset(a,v,sizeof(a))
#define you -> Noob
//written by Aden Frost
void fastaf(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
void solve(void)
{
lli N,a,b;
input(N)>>a>>b;
output(N-a+b);
}
int main()
{
fastaf();
// i T;
//input (T);
//while(T--)
//{
solve();
space();
//}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
int a, b, c;
cin >> a >> b >> c;
if ( a == b )
cout << c << endl;
else if ( b == c )
cout << a << endl;
else if ( c == a )
cout << b << endl;
else
cout << 0 << endl;
}
| #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i))
#define REP_R(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i))
#define REP3R(i, m, n) for (int i = (int)(n)-1; (i) >= (int)(m); --(i))
#define ALL(x) ::std::begin(x), ::std::end(x)
#define SZ(x) ((int)(x).size())
#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 endl "\n"
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
using namespace std;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vll = vector<long long>;
using vvi = vector<vi>;
using vvll = vector<vll>;
using vs = vector<string>;
using pii = pair<int, int>;
template <class T>
bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
} else
return false;
}
template <class T>
bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
} else
return false;
}
template <typename T>
inline void print(const T &x) {
cout << x << "\n";
}
template <typename T>
inline void print(const vector<T> &v, string s = " ") {
REP (i, v.size())
cout << v[i] << (i != (ll)v.size() - 1 ? s : "\n");
}
const int IINF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
ll solve(ll a, ll b, ll c) {
// TODO: edit here
if (a == b) return c;
if (b == c) return a;
if (c == a) return b;
return 0;
}
// generated by oj-template v4.7.2 (https://github.com/online-judge-tools/template-generator)
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
ll a, b, c;
cin >> a >> b >> c;
auto ans = solve(a, b, c);
cout << ans << endl;
return 0;
}
|
#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
#define f(x, m) for(auto x : m)
#define cpu() ios::sync_with_stdio(false); cin.tie(nullptr)
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pair<int ,int>>
#define vll vector<pair<ll ,ll>>
#define all(v) v.begin(),v.end()
#define sor(a) sort( a.begin(), a.end() )
#define ros(a) sort( a.rbegin(), a.rend())
#define prec(n) fixed << setprecision(n)
#define ff first
#define ss second
#define print(x) for(auto it : x) cout << it << " ";
#define debug(x) cerr << #x << " is " << x << endl;
typedef long long ll;
using namespace std;
// using namespace __gnu_pbds;
#define dbg(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> ) {cout << "NEXT\n"; }
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << ", ";
err(++it, args...);
}
template<typename... T>
void rd(T& ... args){
(( cin >> args), ...);
}
template<typename... T>
void ps(T ... args){
((cout << args << ' '), ...);
cout << '\n';
}
// mp.max_load_factor(0.25);
// using ordered_set = tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update>;
const int MOD = 1e9 + 7, MOD1 = 998244353, MAX = 5e3 + 5;
const char nl = '\n';
const ll INF = 2e18 + 5;
int N, M;
int dp[MAX][MAX];
int inv_[MAX];
int pot_[MAX];
ll pot(ll x, ll y){
if(y == 0) return 1;
if(y & 1) return (pot(x, y - 1) * x) % MOD1;
ll k = pot(x, y / 2);
return (k * k) % MOD1;
}
ll inv(ll x){
return pot(x, MOD1 - 2);
}
void add_self(int& x, int y){
x += y;
if(x >= MOD1){
x -= MOD1;
}
}
int op(int m, int x){
x = m - x;
int xd1 = pot_[m];
int xd2 = pot_[x];
int gu = (xd1 - xd2);
if(gu < 0) gu += MOD1;
gu = int(((ll)gu * inv_[m - x]) % MOD1);
int th = pot_[m];
th -= gu;
if(th < 0){
th += MOD1;
}
return th;
}
void solve(){
cin >> N >> M;
for(int i = 1; i <= M; i++){
dp[1][i] = 1;
pot_[i] = 1;
}
for(int i = 2; i <= N; i++){
int aux = 0;
for(int j = 1; j <= M; j++){
pot_[j] = int((ll(pot_[j]) * j) % MOD1);
add_self(aux, dp[i - 1][j]);
}
for(int j = 1; j <= M; j++){
add_self(dp[i][j], aux);
add_self(dp[i][j], op(M, j));
}
}
int ans = 0;
for(int i = 1; i <= M; i++){
add_self(ans, dp[N][i]);
}
cout << ans << "\n";
}
int main(){
cpu();
for(int i = 1; i < MAX; i++){
inv_[i] = int(inv(i));
}
solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
template <typename T>
void print(const vector<vector<T>>& matrix, const string& name = "") {
if (!name.empty()) cout << name << ": " << endl;
for (const auto& row : matrix) {
for (const auto& element : row) {
cout << element << " ";
}
cout << endl;
}
}
int main() {
int L;
cin >> L;
// dp(i, j) = number of ways to cut prefix [:i] int j pieces
const int64_t INFTY = 1e18;
vector<vector<int64_t>> dpv(L, vector<int64_t>(13, 0));
dpv[0][1] = 1;
for (int i = 1; i < L; ++i) {
for (int j = 1; j < dpv[i].size(); ++j) {
// dpv[2][2] += dpv[1][2]
// We don't cut.
dpv[i][j] += dpv[i - 1][j];
// We cut.
dpv[i][j] += dpv[i - 1][j - 1];
}
}
cout << dpv.back().back() << endl;
} |
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
ll n,k;
cin>>n>>k;
vector<ll> dp2(3*n+1);
for (ll s2=2;s2<=2*n;++s2){
dp2[s2]=min(s2-1,n)-max(s2-n,1LL)+1;
}
vector<ll> dp3(3*n+1);
ll acu2=0;
for (int i=0;i<=3*n;++i){
dp3[i]=acu2;
acu2+=dp2[i];
if (i>=n) acu2-=dp2[i-n];
}
ll s=0;
ll acu=0;
while (true){
if (acu>=k) break;
s+=1;
acu+=dp3[s];
}
ll a,b;
// phase beauty
k-=acu-dp3[s]+1;
acu=0;
for (a=max(1LL,s-2*n);a<=min(n,s-2);++a){
ll sumbc=s-a;
ll nxt=min(sumbc-1,n)-max(sumbc-n,1LL)+1;
if (acu+nxt>k) break;
acu+=nxt;
}
// phase taste
k-=acu;
acu=0;
for (b=max(1LL,s-a-n);b<=min(n,s-a-1);++b){
if (acu+1>k) break;
acu+=1;
}
cout<<a<<' '<<b<<' '<<s-a-b<<'\n';
return 0;
} | #include <bits/stdc++.h>
#define double long double
using namespace std;
struct Complex {
double r, i;
Complex(double _r = 0, double _i = 0) : r(_r), i(_i) {}
};
Complex operator + (const Complex &a, const Complex &b) {
return Complex(a.r + b.r, a.i + b.i);
}
Complex operator - (const Complex &a, const Complex &b) {
return Complex(a.r - b.r, a.i - b.i);
}
Complex operator * (const Complex &a, const Complex &b) {
return Complex(a.r * b.r - a.i * b.i, a.r * b.i + a.i * b.r);
}
Complex operator / (const Complex &a, const Complex &b) {
return Complex((a.r * b.r + a.i * b.i) / (b.r * b.r + b.i * b.i), (a.i * b.r - a.r * b.i) / (b.r * b.r + b.i * b.i));
}
Complex operator * (const Complex &a, const double &b) {
return Complex(a.r * b, a.i * b);
}
const int N = 5e6 + 10;
int r[N];
void get_r(int lim) {
for (int i = 0; i < lim; i++) {
r[i] = (i & 1) * (lim >> 1) + (r[i >> 1] >> 1);
}
}
void FFT(Complex *f, int lim, int rev) {
for (int i = 0; i < lim; i++) {
if (i < r[i]) {
swap(f[i], f[r[i]]);
}
}
const double pi = acos(-1.0);
for (int mid = 1; mid < lim; mid <<= 1) {
Complex wn = Complex(cos(pi / mid), rev * sin(pi / mid));
for (int len = mid << 1, cur = 0; cur < lim; cur += len) {
Complex w = Complex(1, 0);
for (int k = 0; k < mid; k++, w = w * wn) {
Complex x = f[cur + k], y = w * f[cur + mid + k];
f[cur + k] = x + y, f[cur + mid + k] = x - y;
}
}
}
if (rev == -1) {
for (int i = 0; i < lim; i++) {
f[i].r /= lim;
}
}
}
long long a[N], b[N], n, k;
Complex A[N], B[N];
int main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) {
A[i] = Complex(1.0, 0);
}
int lim = 1;
while (lim <= 3 * n) {
lim <<= 1;
}
get_r(lim);
FFT(A, lim, 1);
for (int i = 0; i < lim; i++) {
B[i] = A[i] * A[i] * A[i];
A[i] = A[i] * A[i];
}
FFT(A, lim, -1);
FFT(B, lim, -1);
for (int i = 3; i <= 3 * n; i++) {
b[i] = (long long)(B[i].r + 0.5);
}
for (int i = 2; i <= 2 * n; i++) {
a[i] = (long long)(A[i].r + 0.5);
}
// for (int i = 1; i <= 3 * n; i++) {
// printf("%lld ", a[i]);
// }
// puts("");
// for (int i = 1; i <= 3 * n; i++) {
// printf("%lld ", b[i]);
// }
// puts("");
long long sum;
for (int i = 3; i <= 3 * n; i++) {
if (k > b[i]) {
k -= b[i];
}
else {
sum = i;
break;
}
}
// cout << k << "\n";
for (int i = 1; i <= n; i++) {
if (k > a[sum - i]) {
k -= a[sum - i];
}
else {
for (int j = 1; j <= n; j++) {
if (i + j < sum && sum - i - j >= 1 && sum - i - j <= n) {
k--;
if (k == 0) {
printf("%d %d %d\n", i, j, sum - i - j);
return 0;
}
}
}
break;
}
}
return 0;
} |
#include <iostream>
using namespace std;
int H, W, A, B, ans = 0;
void dfs(int i, int bit, int A, int B){
if(i == H * W) return (void)ans++;
if(bit & 1 << i) return dfs(i + 1, bit, A, B);
if(B) dfs(i + 1, bit | 1 << i, A, B - 1);
if(A){
if(i % W != W - 1 && ~bit & 1 << (i + 1)) dfs(i + 1, bit | 1 << i | 1 << (i + 1), A - 1, B);
if(i + W < H * W) dfs(i + 1, bit | 1 << i | 1 << (i + W), A - 1, B);
}
}
int main(){
cin >> H >> W >> A >> B;
dfs(0, 0, A, B);
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
long long INF = 1000000000000000000;
int ans = 0;
int H, W;
void dfs(vector<vector<int>> &f, int A){
if(A == 0) ans++;
for(int i = 0; i < H; i++){
for(int j = 0; j < W; j++){
if(f[i][j] == 0){
if(i + 1 < H && f[i + 1][j] == 0){
f[i][j] = 1;
f[i + 1][j] = 1;
dfs(f, A - 1);
f[i][j] = 0;
f[i + 1][j] = 0;
}
if(j + 1 < W && f[i][j + 1] == 0){
f[i][j] = 1;
f[i][j + 1] = 1;
dfs(f, A - 1);
f[i][j] = 0;
f[i][j + 1] = 0;
}
}
}
}
}
int main(){
cin >> H >> W;
int A, B;
cin >> A >> B;
vector<vector<int>> f(H, vector<int>(W));
dfs(f, A);
int t = 1;
for(int i = 1; i <= A; i++) t *= i;
cout << ans / t << endl;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long i64;
typedef unsigned long long ui64;
typedef vector<i64> vi;
typedef vector<vi> vvi;
typedef pair<i64, i64> pi;
#define pb push_back
#define sz(a) i64((a).size())
#define all(c) (c).begin(), (c).end()
#define REP(s, e, i) for(i=(s); i < (e); ++i)
inline void RI(i64 &i) {scanf("%lld", &(i));}
inline void RVI(vi &v) { for(i64 i=0;i<sz(v);++i) { RI(v[i]); } }
inline void RVVI(vvi &vv) { for(i64 i=0;i<sz(vv);++i) { RVI(vv[i]); } }
inline void WI(const i64 &i) {printf("%lld\n", i);}
inline void WVI(const vi &v, char sep=' ') { for(i64 i=0;i<sz(v);++i) { if(i != 0){ printf("%c", sep); } printf("%lld", v[i]);} printf("\n"); }
inline void WS(const string &s) { printf("%s\n", s.c_str()); }
inline void WB(bool b, const string &yes, const string &no) { if(b){ WS(yes);} else { WS(no);} }
inline void YESNO(bool b) { WB(b, "YES", "NO"); }
inline void YesNo(bool b) { WB(b, "Yes", "No"); }
#define BUF_LENGTH 1000000
inline void RS(string &s) {static char buf[BUF_LENGTH]; scanf("%s", buf); s = buf;}
template<typename T> inline bool IN(T &S, const typename T::key_type &key) {
return S.find(key) != S.end();
}
template<typename T> inline bool ON(const T &b, i64 idx) {
return ((T(1) << idx) & b) != 0;
}
int main(int argc, char *argv[]) {
i64 i, j, k;
i64 N, M; RI(N); RI(M);
vector<string> S(M);
i64 L_min = 100, L_max = 0;
REP(0, M, i) {
RS(S[i]);
L_min = min(L_min, sz(S[i]));
L_max = max(L_max, sz(S[i]));
}
i64 L_avg = (L_min + L_max) / 2;
// cerr << L_min << " " << L_max << endl;
unordered_map<i64, vi> L;
REP(0, M, i) {
L[sz(S[i])].pb(i);
}
vector<string> ans(N, string(N, '.'));
// horizontal
i64 N_h = 0, lh;
i64 Ch;
REP(L_min, L_max+1, lh) {
N_h += sz(L[lh]);
Ch = N / lh; // how many on each row
if(Ch * N <= N_h) {
break;
}
}
// cerr << Ch << " " << lh << endl;
i64 l_cur = L_min;
REP(0, N, i) {
i64 pos = 0;
REP(0, Ch, j) {
i64 idx = L[l_cur].back();
L[l_cur].pop_back();
for(auto &c: S[idx]) {
ans[i][pos] = c;
++pos;
}
if(L[l_cur].empty()) {
L.erase(L.find(l_cur));
++l_cur;
}
}
assert(l_cur <= lh);
assert(pos <= Ch * lh);
}
i64 C_begin = Ch * lh;
// vertical
i64 N_c = 0, lc;
i64 Cc;
REP(l_cur, L_max+1, lc) {
N_c += sz(L[lc]);
Cc = N / lc; // how many on each column
if(Cc * (N - C_begin) <= N_c) {
break;
}
}
//cerr << Cc << " " << lc << endl;
REP(C_begin, N, i) {
i64 pos = 0;
REP(0, Cc, j) {
i64 idx = L[l_cur].back();
L[l_cur].pop_back();
for(auto &c: S[idx]) {
assert(ans[pos][i] == '.');
ans[pos][i] = c;
++pos;
}
if(L[l_cur].empty()) {
L.erase(L.find(l_cur));
++l_cur;
}
}
assert(l_cur <= lc);
assert(pos <= Cc * lc);
}
REP(0, N, i) {
WS(ans[i]);
}
return 0;
}
| //Bismillahir Rahmanir Raheem
#include<bits/stdc++.h>
typedef long long ll;
const ll INFLL = 1e18;
const ll INF =1e9;
using namespace std;
void fast()
{
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
}
ll n,a[300005],t,k;
int main()
{
cin>>n>>k;
for(int i=0;i<n;i++)
{
cin>>t;
a[t]++;
}
ll ans=0;
for(int i=0;i<=n;i++)
{
if(a[i]<k)
{
ans=ans+i*(k-a[i]);
k=a[i];
}
}
ans=ans+(n+1)*k;
cout<<ans<<endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifndef LOCAL
ios::sync_with_stdio(0);
cin.tie(0);
#endif
long long k, n, m;
cin >> k >> n >> m;
vector<int> arr(k);
for (int &it : arr) {
cin >> it;
}
int left = m;
vector<int> brr(k);
priority_queue<pair<long long, int>> pq;
for (int i = 0; i < k; i++) {
int b = (int)(arr[i] * m / n);
left -= b;
brr[i] = b;
pq.push({-abs((b + 1) * n - arr[i] * m) + abs(b * n - arr[i] * m), i});
}
for (int i = 0; i < left; i++) {
auto v = pq.top(); pq.pop();
int j = v.second;
brr[j]++;
pq.push({-abs((brr[j] + 1) * n - arr[j] * m) + abs(brr[j] * n - arr[j] * m), j});
}
for (int it : brr) {
cout << it << " ";
}
cout << '\n';
}
| #include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using P = pair<int, int>;
using vec = vector<int>;
using mat = vector<vector<int>>;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define endl "\n"
constexpr int MOD = 1000000007;
constexpr int INF = 1001001001;
constexpr bool is_multiple = false;
i64 choose2(i64 n) {
return n * (n-1);
}
void solve() {
int k;
cin >> k;
string s, t;
cin >> s >> t;
int score_s = 0, score_tt = 0;
map<int, int> map_s, map_tt;
rep(i, 4) {
map_s[s[i] - '0']++;
map_tt[t[i] - '0']++;
}
long double rem = 9 * k - 8;
long double res = 0;
auto score_calc_s = [&](int i) {
int ret = 0;
map_s[i]++;
for (int j = 1; j <= 9; j++) {
int tmp = j;
rep(_, map_s[j]) tmp *= 10;
ret += tmp;
}
map_s[i]--;
return ret;
};
auto score_calc_tt = [&](int i) {
int ret = 0;
map_tt[i]++;
for (int j = 1; j <= 9; j++) {
int tmp = j;
rep(_, map_tt[j]) tmp *= 10;
ret += tmp;
}
map_tt[i]--;
return ret;
};
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= 9; j++) {
if (score_calc_s(i) > score_calc_tt(j)) {
if (i != j) {
res += ((k - map_s[i] - map_tt[i]) / rem) * ((k - map_s[j] - map_tt[j]) / (rem - 1));
}
else {
res += ((k - map_s[i] - map_tt[i]) / rem) * (max(k - map_s[j] - map_tt[j] - 1, 0) / (rem - 1));
}
}
}
}
cout << fixed << setprecision(10) << res << endl;
}
int main() {
int t = 1;
if (is_multiple) cin >> t;
while (t--) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2005;
int n, m, fa[N], ans1, ans2, siz[N];
char a[N][N];
int find_(int x) {
return x == fa[x] ? x : fa[x] = find_(fa[x]);
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n + m; i++)
fa[i] = i, siz[i] = 1;
for (int i = 1; i <= n; i++)
scanf("%s", a[i] + 1);
a[1][1] = a[n][1] = a[1][m] = a[n][m] = '#';
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (a[i][j] == '#') {
int x = find_(i), y = find_(j + n);
if (x^y)
fa[x] = y, siz[y] += siz[x];
}
for (int i = 1; i <= n + m; i++)
if (find_(i) == i)
ans1 += (i <= n)|(siz[i] >= 2), ans2 += (i > n)|(siz[i] >= 2);
printf("%d\n", min(ans1, ans2) - 1);
} | #include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define pb push_back
#define ll long long
#define int long long
template <typename T>
using ordered_set = tree<T, null_type,
less<T>, rb_tree_tag,
tree_order_statistics_node_update>;
const int MOD1 = 1000000007;
const int MOD2 = 998244353;
const int N = 2e6 + 10;
#ifdef LOCAL
#include "debug.h"
#else
#define trace(...) 420;
#endif
struct DSU
{
int32_t _parent_[N], _rank_[N], _size_[N], comp = 0;
void make_set(int32_t v) {
_parent_[v] = v;
_rank_[v] = 0;
_size_[v] = 1;
comp++;
}
void init(int32_t n) {
for (int32_t i = 0; i < n; ++i) {
make_set(i);
}
}
int32_t find_set(int32_t v) {
if (v == _parent_[v]) {
return v;
}
return _parent_[v] = find_set(_parent_[v]);
}
void merge(int32_t a, int32_t b) {
a = find_set(a);
b = find_set(b);
if (a != b) {
if (_rank_[a] < _rank_[b]) { //merge by height
swap(a, b);
}
_parent_[b] = a;
if (_rank_[a] == _rank_[b]) {
_rank_[a]++;
}
// if (_size_[a] < _size_[b]) //merge by size
// swap(a, b);
// _parent_[b] = a;
_size_[a] += _size_[b];
comp--;
}
}
} dsu;
vector<int> R[1001], C[1001];
vector<int> out;
void solve() {
int h, w;
cin >> h >> w;
if (h == 2 || w == 2) {
cout << 0;
return;
}
string rink[h];
dsu.init(h * w);
int useless = 0;
for (int i = 0; i < h; ++i) {
cin >> rink[i];
for (int j = 0; j < w; ++j) {
if (rink[i][j] == '#') {
R[i].pb(i * w + j);
C[j].pb(i * w + j);
if (i == 0 || i == h - 1) {
out.pb(i * w + j);
}
} else {
useless++;
}
}
if (rink[i][0] == '#') { out.pb(i * w); }
if (rink[i][w - 1] == '#') { out.pb(i * w + w - 1); }
}
int er = 0;
for (int i = 0; i < h; ++i) {
if (i && i != h - 1) { er += R[i].empty(); }
for (int t : R[i]) {
dsu.merge(t, R[i][0]);
}
}
int ec = 0;
for (int i = 0; i < w; ++i) {
if (i && i != w - 1) { ec += C[i].empty(); }
for (int t : C[i]) {
dsu.merge(t, C[i][0]);
}
}
for (int t : out) {
dsu.merge(t, out[0]);
}
int x = min(er, ec);
if (sz(out) == 0) { if (x == 0) { x = 1; } dsu.comp++; }
cout << x + max(0LL, dsu.comp - useless - 1);
// trace(er, ec, dsu.comp - useless);
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t = 1;
// cin >> t;
while (t--) {
solve();
cout << '\n';
}
return 0;
} |
#include <bits/stdc++.h>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
using ll = long long;
#define rep(i,n) for (ll i=0; i < (n); ++i)
#define rep2(i,n,m) for(ll i=n;i<=m;i++)
#define rep3(i,n,m) for(ll i=n;i>=m;i--)
#define P pair<ll,ll>
#define pb push_back
#define eb emplace_back
#define ppb pop_back
#define mpa make_pair
#define fi first
#define se second
#define set20 cout<<fixed<<setprecision(20) ;
inline void chmax(ll& a,ll b){a=max(a,b);}
inline void chmin(ll& a,ll b){a=min(a,b);}
double pi=acos(-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;}
int main(){
ll a, b,c,d ; cin>>a>>b>>c>>d ;
ll ans= min(min(a,b),min(c,d)) ;
cout<<ans<<endl ;
return 0 ;
} | #include <bits/stdc++.h>
using namespace std;
int main(){
int a[4];
for(int i=0;i<4;i++){
cin>>a[i];
}
int min=1000;
for(int i=0;i<4;i++){
if(a[i]<min)min=a[i];
}
cout<<min;
} |
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <climits>
#include <cmath>
#include <bitset>
#include <complex>
#include <functional>
#include <cassert>
#include <stack>
#include <numeric>
typedef long long ll;
typedef std::pair<int, int> Pii;
typedef std::pair<long long, long long> Pll;
typedef std::pair<double, double> Pdd;
#define rip(i, n, _s) for (int i = (_s);i < (int)( n ); i++)
#define all(_l) _l.begin(), _l.end()
#define rall(_l) _l.rbegin(), _l.rend()
#define MM << " " <<
template<typename T>
using MaxHeap = std::priority_queue<T>;
template<typename T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template<typename T>
inline bool chmax(T &_l, const T b) {
if (_l < b) {
_l = b;
return true;
}
return false;
}
template<typename T>
inline bool chmin(T &_l, const T b) {
if (_l > b) {
_l = b;
return true;
}
return false;
}
# ifdef LOCAL_DEBUG
template<typename T>
void vdeb(const std::vector<T> &bb) {
for (unsigned int i = 0;i < bb.size();i++) {
if (i == bb.size() - 1) std::cout << bb[i];
else std::cout << bb[i] << ' ';
}
std::cout << '\n';
}
template<typename T>
void vdeb(const std::vector<std::vector<T>> &bb) {
for (unsigned int i = 0;i < bb.size();i++) {
std::cout << i << ' ';
vdeb(bb[i]);
}
std::cout << '\n';
}
# endif
using namespace std;
map<ll, ll> mp;
ll rec(const ll x, const ll y) {
ll ret = abs(x-y);
if(y == 1) return ret;
if(mp.count(y)) return mp[y];
if(y&1) {
chmin(ret, min(rec(x, (y+1)>>1), rec(x, (y-1)>>1))+2);
}
else {
chmin(ret, rec(x, y>>1)+1);
}
mp[y] = ret;
return ret;
}
int main() {
ll x, y; cin >> x >> y;
// if(x > y) cout << abs(x-y) << endl;
cout << rec(x,y) << endl;
} | #include <stdio.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <iostream>
using namespace std;
int main(){
long x,y;
scanf("%ld %ld", &x, &y);
long nAns = abs(x-y);
long nTmp = 0;
while (y>x) {
if (y%2 == 0) {
y /= 2;
nTmp++;
} else {
nAns = min(nAns, nTmp+2+abs((2*x-y)/2));
y = (y+1)/4;
nTmp += 3;
}
nAns = min(nAns, nTmp+abs(x-y));
}
printf("%ld", nAns);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double PI=acos(-1);
const double eps=1e-5;
int main(){
int n,a,b,c,d;
scanf("%d",&n);
scanf("%d%d%d%d",&a,&b,&c,&d);
double x=1.0*(a+c)/2.0,y=1.0*(b+d)/2.0;
double len=sqrt(1.0*(a-c)*(a-c)+1.0*(b-d)*(b-d))/2;
double al=atan2(1.0*(d-b),1.0*(c-a));//用atan()会WA,
double bl=2.0*PI/n;
double cl=al+bl;
double ax=x-len*cos(cl),ay=y-len*sin(cl);
if(fabs(ay)<eps) ay=0;//-0的情况
if(fabs(ax)<eps) ax=0;
printf("%.5f %.5f\n",ax,ay);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll solve() {
ll N;
cin >> N;
double x, y;
cin >> x >> y;
complex<double> z0(x,y);
cin >> x >> y;
complex<double> zh(x,y), zo = (z0+zh);
zo /= 2;
complex<double> a(cos(2*M_PI/N),sin(2*M_PI/N));
complex<double> ans;
ans = zo + (z0-zo)*a;
cout << ans.real() << " " << ans.imag() << "\n";
return 0;
}
int main() {
cout.setf(ios::fixed);
cout.precision(16);
solve();
return 0;
} |
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using namespace std;
int main(){
int n, D, H;
cin >> n >> D>> H;
double ans = 0;
rep(i, n){
int d, h;
cin >> d >> h;
double now = H - double (H-h) * D/ (D-d);
ans = max(ans, now);
}
printf("%.10f\n", ans);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
// using ll = long long;
#define int long long
void solve(){
int n, d, h;
cin>>n>>d>>h;
vector<pair<int,int>> a(n);
for(int i=0;i<n;i++){
cin>>a[i].first>>a[i].second;
}
sort(a.begin(), a.end());
double ans = 0;
for(int i=0;i<n;i++){
double now = double(a[i].second - h)/ double(a[i].first - d);
ans = max(ans, a[i].second - now*a[i].first);
}
cout<<fixed<<setprecision(10);
cout<<ans<<"\n";
}
int32_t main(){
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int t;
// cin>>t;
t = 1;
int _=1;
while(t--){
// cout<<"Case #"<<_++<<": ";
solve();
}
return 0;
} |
/**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author tatsumack
*/
#include <iostream>
#include <fstream>
#include <bits/stdc++.h>
#define int long long
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i)
#define REV(i, a, b) for (int i = (a); i >= (b); --i)
#define CLR(a, b) memset((a), (b), sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF 1001001001001001001ll
#define fcout cout << fixed << setprecision(12)
using namespace std;
typedef pair<int, int> P;
class DSumOfDifference {
public:
void solve(std::istream& cin, std::ostream& cout) {
int N;
cin >> N;
vector<int> A(N);
REP(i, N) cin >> A[i];
sort(A.begin(), A.end());
int sum = A[0];
int res = 0;
FOR(i, 1, N-1) {
res += A[i] * i - sum;
sum += A[i];
}
cout << res << endl;
}
};
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
std::istream& in(std::cin);
std::ostream& out(std::cout);
DSumOfDifference solver;
solver.solve(in, out);
return 0;
}
| #include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
#define endl "\n";
using namespace std;
const int INFINT = 2e9;
const ll INFLL = 2e18;
const ll mod = 1000000007;
template<ll mod> class modint{
public:
ll val=0;
//コンストラクタ
modint(ll x=0){while(x<0)x+=mod;val=x%mod;}
//コピーコンストラクタ
modint(const modint &r){val=r.val;}
//算術演算子
modint operator -(){return modint(-val);} //単項
modint operator +(const modint &r){return modint(*this)+=r;}
modint operator -(const modint &r){return modint(*this)-=r;}
modint operator *(const modint &r){return modint(*this)*=r;}
modint operator /(const modint &r){return modint(*this)/=r;}
//代入演算子
modint &operator +=(const modint &r){
val+=r.val;
if(val>=mod)val-=mod;
return *this;
}
modint &operator -=(const modint &r){
if(val<r.val)val+=mod;
val-=r.val;
return *this;
}
modint &operator *=(const modint &r){
val=val*r.val%mod;
return *this;
}
modint &operator /=(const modint &r){
ll a=r.val,b=mod,u=1,v=0;
while(b){
ll t=a/b;
a-=t*b;swap(a,b);
u-=t*v;swap(u,v);
}
val=val*u%mod;
if(val<0)val+=mod;
return *this;
}
//等価比較演算子
bool operator ==(const modint& r){return this->val==r.val;}
bool operator <(const modint& r){return this->val<r.val;}
bool operator !=(const modint& r){return this->val!=r.val;}
};
using mint = modint<mod>;
//入出力ストリーム
istream &operator >>(istream &is,mint& x){//xにconst付けない
ll t;is >> t;
x=t;
return (is);
}
ostream &operator <<(ostream &os,const mint& x){
return os<<x.val;
}
//累乗
mint modpow(const mint &a,ll n){
if(n==0)return 1;
mint t=modpow(a,n/2);
t=t*t;
if(n&1)t=t*a;
return t;
}
void solve() {
int N;
ll M;
cin >> N >> M;
int s = N;
for (int i=0; i<N; i++) {
int a; cin >> a;
s += a;
}
mint ans = 1;
for (int i=1; i<=s; i++) {
ans *= (N+M-i+1);
ans /= i;
}
cout << ans << endl;
return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
} |
#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()
int dp[3000000];
int main(){
int n;cin>>n;
vector<int> v(n);
REP(i,n)cin>>v[i];
int sum=0;
REP(i,n)sum+=v[i];
dp[0]=1;
REP(i,n){
for(int j=sum;j>=0;j--){
if(dp[j])dp[j+v[i]]=1;
}
}
int ans=1e9;
for(int i=0;i<=sum;i++)if(dp[i])ans=min(ans,max(sum-i,i));
cout<<ans<<endl;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
constexpr int kN = int(1E5 + 10);
int a[kN], b[kN], y[kN], x[kN];
int main() {
int n, l, idx = -1, now = 0, lst = 0;
ll ans = 0;
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]);
a[0] = b[0] = 0, a[n + 1] = b[n + 1] = l + 1;
for (int i = 0; i <= n; i++) x[i] = a[i + 1] - a[i] - 1;
for (int i = 0; i <= n; i++) y[i] = b[i + 1] - b[i] - 1;
for (int i = 0; i <= n; i++) if (y[i]) {
now = 0;
while (x[lst] == 0) lst++;
while (now < y[i]) now += x[++idx];
if (now > y[i]) {
printf("-1\n");
return 0;
}
ans += max(i - lst, 0) + max(idx - i, 0);
lst = idx + 1;
}
printf("%lld\n", ans);
}
|
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<map>
using std::cin;using std::cerr;
using std::max;using std::min;
#define N 100005
#define ll long long
#define db double
#define dbg1(x) cerr<<#x<<"="<<(x)<<" "
#define dbg2(x) cerr<<#x<<"="<<(x)<<"\n"
int a,b,c,flag,ans;
int Pow(int x,int y,int mod,int &f)
{
int res=1;
for(;y;y>>=1,f|=(ll)x*x>=mod,x=(ll)x*x%mod)
if(y&1) f|=(ll)res*x>=mod,res=(ll)res*x%mod;
return res;
}
int main()
{
scanf("%d%d%d",&a,&b,&c);
b=Pow(b,c,4,flag);
if(flag) b+=4,flag=0;
ans=Pow(a,b,10,flag);
printf("%d\n",ans);
return 0;
} | #include <bits/stdc++.h>
#define FAST_IO ios::sync_with_stdio(false); cin.tie(0), cout << setprecision(15);
#define ll long long int
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rrep(i, k, n) for (ll i = k; i>=n; i--)
#define repp(i, k, n) for (ll i = k; i < (n); i++)
#define pb push_back
#define all(x) begin(x), end(x)
#define vc vector<ll>
using namespace std;
using namespace std::chrono;
typedef std::chrono::high_resolution_clock Clock;
typedef std::chrono::milliseconds Milliseconds;
ll mPow(ll a, ll n, ll mod) {
ll ans = 1;
while (n > 0) {
if (n & 1) ans = (ans * a) % mod;
a = (a * a) % mod;
n = n >> 1;
}
return ans % mod;
}
ll fact[1000005];
ll mod = 998244353;
void initialize() {
ll n = 1000005;
fact[0] = 1;
fact[1] = 1;
repp(i, 2, n) {
fact[i] = (fact[i - 1] * i) % mod;
}
}
ll ncr(ll a, ll b) {
ll k = (fact[b] * fact[a - b]) % mod;
ll g = mPow(k, mod - 2, mod);
return (fact[a] * g) % mod;
}
void dd() {
ll n, m, k;
cin >> n >> m >> k;
ll ans = 0;
ll q = mPow(k, n, mod);
repp(i, 1, n + 1) {
ans += ((i) * (ncr(n - 1, i - 1) * ((k * (mPow(k - 1, i - 1, mod))) % mod)) % mod) % mod;
cout << "Segements: " << i << " NCR " << ncr(n - 1, i - 1) << " WAYS " << k * (mPow(k - 1, i - 1, mod)) << "\n";
ans %= mod;
}
q = mPow(q, mod - 2, mod);
cout << "Inverse Q " << q << "\n";
cout << "P " << ans << "\n";
ans *= q;
ans %= mod;
cout << ans << "\n";
}
void d() {
ll n, m, k;
cin >> n >> m >> k;
ll ans = 0;
ll q = pow(k, n);
repp(i, 1, n + 1) {
ans += (i) * (ncr(n - 1, i - 1) * k * (mPow(k - 1, i - 1, mod)));
ans %= mod;
}
q = mPow(q, mod - 2, mod);
ans *= q;
ans %= mod;
cout << ans << "\n";
}
void atcoder() {
ll n; cin>>n;
ll a[n], b[n];
rep(i, n) cin>>a[i];
rep(i, n) cin>>b[i];
ll ans = 0;
rep(i, n){
ans+=a[i]*b[i];
}
if(ans == 0) cout<<"Yes";
else cout<<"No";
}
int main() {
FAST_IO
ll test = 1;
// initialize();
// cin >> test;
while (test-- > 0) {
atcoder();
}
} |
#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 int long long
#define S second
#define F first
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define lb lower_bound
#define ub upper_bound
#define si(c) (int)((c).size())
#define lcm(a, b) (a * (b / __gcd(a,b)))
#define inf (int)(1e10)
#define endl '\n'
#define mp make_pair
#define time(s) (double(clock()-s)/double(CLOCKS_PER_SEC))
#define debug(args...) _F(#args, args)
#define vi std::vector<int>
#define pii pair<int, int>
#define vpi vector<pii>
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
clock_t start;
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
template<typename T> void _F(const char *name, T arg1){ cerr << name << " = " << arg1 << endl;}
template<typename T, typename... Args> void _F(const char *names, T arg1, Args... args)
{ const char *name = strchr(names, ',');cerr.write(names, name-names) << " = " << arg1 << endl;_F(name+2, args...);}
template< typename T1, typename T2 > istream& operator>>(istream& in, pair<T1, T2> &q){ in >> q.F >> q.S; return in;}
template< typename T1, typename T2 > ostream& operator<<(ostream& out, pair<T1, T2> &q){ out << q.F << " " << q.S; return out;}
template< typename T1, typename T2 > pair<T1, T2> operator+(pair<T1, T2> p1, pair<T1, T2> p2){ return {p1.F+p2.F, p1.S+p2.S};}
template< typename T1, typename T2 > pair<T1, T2> operator-(pair<T1, T2> p1, pair<T1, T2> p2){ return {p1.F-p2.F, p1.S-p2.S};}
template< typename T1, typename T2 > bool operator<(pair<T1, T2> p1, pair<T1, T2> p2){ return p1 < p2 ;}
template<typename T> void Unique(vector<T> &v) {
sort(all(v)), v.resize(distance(v.begin(), unique(all(v))));
}
void No() {
cout << -1 << endl;
exit(0);
}
void solve() {
int n;
cin >> n;
int p[n], pos[n];
for(int i = 0; i < n; i++) {
cin >> p[i], p[i]--;
pos[p[i]] = i;
}
vpi ans;
int mx = 0;
for(int i = 0; i < n-1; i++) {
if(pos[i] == i && mx == i) No();
int t = pos[i];
for(int j = pos[i]; j > max(i, mx); j--) {
ans.pb(mp(j, j-1));
swap(p[j], p[j-1]);
swap(pos[p[j]], pos[p[j-1]]);
}
mx = max(mx, t);
// cout << mx << endl;
// for(int j = 0; j < n; j++)
// cout << p[j] << " ";
// cout << endl;
}
for(int i = 0; i < n; i++) {
if(p[i] != i)No();
}
for(auto u : ans) {
u.F += 1, u.S += 1;
cout << u.S << endl;
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
start = clock();
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
int test = 1;
// cin >> test;
cout << fixed << setprecision(12);
for(int i = 1; i <= test; ++i){
solve();
}
cerr << time(start);
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 P pair<ll,ll>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
int main(){
int n;
cin>>n;
vector<int> p(n);
rep(i,n) cin>>p[i];
int tmp=0;
vector<int> ans;
bool flag=0;
while(tmp!=n-1){
for(int i=tmp+1; i<n; i++){
if(p[i]==tmp+1){
for(int a=i; a>=tmp+1; a--){
ans.push_back(a);
p[a]=p[a-1];
}
p[tmp]=tmp+1;
tmp=i;
break;
}
if(i==n-1) flag=1;
}
if(flag) break;
}
bool ok=1;
rep(i,n){
if(p[i]!=i+1) ok=0;
}
if(ok && ans.size()==n-1){
rep(i,n-1) cout<<ans[i]<<" ";
cout<<endl;
}
else cout<<-1<<endl;
} |
/*It is better to taste failure
than to regret*/
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <climits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <ratio>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef long double LD;
#define int ll
#define double LD
#define pb push_back
#define mp make_pair
#define REP(i,n) for (int i = 0; i < n; i++)
#define loop(i,n) for (int i = 0; i < n; i++)
#define FOR(i,a,b) for (int i = a; i < b; i++)
#define REPD(i,n) for (int i = n-1; i >= 0; i--)
#define FORD(i,a,b) for (int i = a; i >= b; i--)
#define remax(a,b) a = max(a,b)
#define remin(a,b) a = min(a,b)
#define mem(a, b) memset(a, b, sizeof a)
#define append push_back
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define print(x) cout << (x) << endl
#define PRECISION(x) cout << fixed << setprecision(x)
#define FAST_IO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define Test(){int t;cin >> t;for(int i=1;i<=t;i++){cout << "Case #" << i << ": ";solve();}}
#define test(){int t; cin>>t; for(int i=1;i<=t;i++){solve();}}
#define vi vector<int>
#define vii vector<pair<int,int> >
#define vci vector<pair<char,int> >
#define vic vector<pair<int,char> >
#define N 200005
#define pb push_back
const double PI=acos(-1);
const int MOD = 1000000007;
const int FMOD = 998244353;
void solve()
{
int a,b,c,d;
cin>>a>>b>>c>>d;
cout<<a*d-b*c<<endl;
}
signed main()
{
// test();
solve();
} | #include<iostream>
using namespace std;
int main()
{
int a,b,c,d;
cin>>a>>b>>c>>d;
cout<<a*d-b*c;
return 0;
} |
#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#define N 2005
using namespace std;
int n, m, d[N][N], cnt[30];
vector<int> er[30], ec[30];
int dr[4] = {-1, 0, 0, 1}, dc[4] = {0, -1, 1, 0};
bool v[N][N];
int va[30];
char a[N][N];
queue<int> qr, qc, qs;
void f(int p, int q, int w) {
int t;
if (v[p][q]) return;
t = a[p][q] - 'a';
if (t >= 0 && t <= 25 && va[t] == 0) va[t] = w;
if (t >= 0 && t <= 25 && w - va[t] >= 2) return;
// printf("%d %d\n", p, q);
v[p][q] = 1;
qr.push(p);
qc.push(q);
qs.push(w);
}
int main()
{
int i, j, t, rr, cc, tr, ts, tc, sr, sc;
cin >> n >> m;
// for (i = 0; i <= n + 1; i++) {
// for (j = 0; j <= m + 1; j++) a[i][j] = '#';
// }
for (i = 1; i <= n; i++) {
scanf ("%s", &a[i][1]);
}
for (i = 0; i <= n + 1; i++) {
a[i][0] = a[i][m + 1] = '#';
}
for (i = 0; i <= m + 1; i++) {
a[0][i] = a[n + 1][i] = '#';
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
if (a[i][j] == '#' || a[i][j] == '.' ||a[i][j] == 'G') continue;
if (a[i][j] == 'S') {
sr = i;
sc = j;
} else {
t = a[i][j] - 'a';
cnt[t]++;
er[t].push_back(i);
ec[t].push_back(j);
d[i][j] = cnt[t];
}
}
}
f(sr, sc, 0);
while (!qr.empty()) {
tr = qr.front(); qr.pop();
tc = qc.front(); qc.pop();
ts = qs.front(); qs.pop();
if (a[tr][tc] == 'G') {
cout << ts << endl;
return 0;
}
t = a[tr][tc] - 'a';
if (ts - va[t] == 0) {
if (a[tr][tc] - 'a' >= 0 && a[tr][tc] - 'a' <= 25) {
for (i = 0; i < er[t].size(); i++) {
f(er[t][i], ec[t][i], ts +1);
}
}
}
for (i = 0; i < 4; i++) {
rr = tr + dr[i];
cc = tc + dc[i];
if (a[rr][cc] == '#') continue;
f(rr, cc, ts + 1);
}
}
cout << -1 << endl;
return 0;
}
| #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<utility>
#include<set>
#include<stack>
#include<list>
#include<deque>
#include<bitset>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<climits>
#include<cmath>
#include<cctype>
#define pb push_back
#define mp make_pair
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ren(i,a,b) for(int i=a;i>=b;i--)
#define ff first
#define ss second
#define pll pair<long long int,long long int>
#define pii pair<int,int>
#define vll vector<long long int>
#define vii vector<int>
#define gi(n) scanf("%d",&n)
#define gll(n) scanf("%lld",&n)
#define gstr(n) scanf("%s",n)
#define gl(n) cin >> n
#define oi(n) printf("%d",n)
#define oll(n) printf("%lld",n)
#define ostr(n) printf("%s",n)
#define ol(n) cout << n
#define os cout<<" "
#define on cout<<"\n"
#define o2(a,b) cout<<a<<" "<<b
#define all(n) n.begin(),n.end()
#define present(s,x) (s.find(x) != s.end())
#define cpresent(s,x) (find(all(s),x) != s.end())
#define tr(container, it) for(__typeof(container.begin()) it = container.begin(); it != container.end(); it++)
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef long double ld;
typedef vector<vector<ll> > mat;
string s[2005];
int n,m,dis[2005][2005],used[26];
bool good(int i,int j)
{
if(i<0||i>=n||j<0||j>=m)return 0;
if(dis[i][j]>-1)return 0;
if(s[i][j]=='#')return 0;
return 1;
}
int main()
{ios_base::sync_with_stdio(false);
cin>>n>>m;
rep(i,0,n-1)cin>>s[i];
rep(i,0,n-1)rep(j,0,m-1)dis[i][j]=-1;
queue<pii> q;
rep(i,0,n-1)rep(j,0,m-1)
{
if(s[i][j]=='S')
{
q.push({i,j});
dis[i][j]=0;
}
}
while(!q.empty())
{
pii p=q.front();
q.pop();
char c=s[p.ff][p.ss];
int d=dis[p.ff][p.ss];
if(c>='a'&&c<='z')
{
if(used[c-'a']==0)
{
used[c-'a']=1;
rep(i,0,n-1)rep(j,0,m-1)
{
if(dis[i][j]==-1&&s[i][j]==c)
{
dis[i][j]=1+d;
q.push({i,j});
}
}
}
}
if(good(p.ff-1,p.ss))
{
q.push({p.ff-1,p.ss});
dis[p.ff-1][p.ss]=1+d;
}
if(good(p.ff+1,p.ss))
{
q.push({p.ff+1,p.ss});
dis[p.ff+1][p.ss]=1+d;
}
if(good(p.ff,p.ss-1))
{
q.push({p.ff,p.ss-1});
dis[p.ff][p.ss-1]=1+d;
}
if(good(p.ff,p.ss+1))
{
q.push({p.ff,p.ss+1});
dis[p.ff][p.ss+1]=1+d;
}
}
rep(i,0,n-1)
{
rep(j,0,m-1)
{
if(s[i][j]=='G')ol(dis[i][j]);
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int cnt;
ll n;
unordered_set<ll> factor;
int main()
{
cin.tie(0); ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; (ll)i * i <= 2 * n; ++i)
{
if (2 * n % i == 0)
{
factor.insert(i);
factor.insert(2 * n / i);
}
}
for (auto item: factor)
cnt += (((2 * n / item) - item + 1) % 2 == 0);
cout << cnt;
} | #include <bits/stdc++.h>
#define int long long
using namespace std;
int n;
vector<int> a;
int k;
vector<pair<int, int>> c;
int m;
int maxi = 0;
void f(vector<int> a, vector<pair<int, int>> v, int k) {
if(k == 0) {
int cc = 0;
// for(auto e: a) cout << e << ' ';
// cout << endl;
for(int i = 0; i < m; i++) {
// cout << c[i].first << c[i].second << endl;
if((a[c[i].first] > 0) and (a[c[i].second] > 0)) {
cc++;
}
}
// cout << cc << endl;
maxi = max(maxi, cc);
return;
}
// cout << v[k].first << ' ' << v[k].second << endl;
a[v[k].first] += 1;
f(a, v, k - 1);
a[v[k].first] -= 1;
a[v[k].second] += 1;
f(a, v, k - 1);
a[v[k].second] -= 1;
}
int32_t main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t;
t = 1;
while(t--) {
int n;
cin >> n;
// si - sx = n
// si - n = sx
//
// i*(i+1) - 2n = x(x+1)
//2n + x*(x+1) = (i * (i + 1))
// 2n + x2 + x = i2+i
// 2n = i2 - x2 + i - x
// 2n = (i-x)(i + x + 1)
// 2n = l(l + 2x + 1)
//
int ans = 0;
for(int l = 1; l * (l + 1) <= 2 * n; l++) {
if((2 * n) % l) continue;
int y = (2 * n);
y /= l;
int z = y - l - 1;
if(z % 2 == 0) ans += 2;
}
cout << ans;
return 0;
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int INF = (int)1e9;
constexpr long long LINF = (long long)1e18;
//constexpr long long MOD = 1e9 + 7;
constexpr long long MOD = 998244353;
constexpr double pi = 3.141592653589793238;
int main() {
int n;
cin >> n;
set<int> ans;
for (int i = 0; i < n; ++i) {
int a;
cin >> a;
if (ans.count(a)) {
cout << "No" << endl;
return 0;
}
else {
ans.insert(a);
}
}
cout << "Yes" << endl;
return 0;
} | #include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
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,cc,n) for(int i=cc;i<n;++i)
#define lrep(i,cc,n) for(long long i=cc;i<n;++i)
#define sqrep(i,cc,n) for(long long i=cc;i*i<=n;++i)
#define rrep(i,cc,n) for(long i=cc;i>n;--i)
#define pii pair<int, int>
#define pll pair<long long, long long>
using ll = long long;
//using mint = modint;
const vector<int> dx = {1, 1, 1, 0, 0, 0, -1, -1, -1};
const vector<int> dy = {1, 0, -1, 1, -1, 0, 1, 0, -1};
const double PI = 3.141592653589793;
const ll inf = 1001001001;
const ll e9 = 1000000000;
const ll mod = 1000000007;
const ll mod2 = 998244353;
const int MAX = 1000000;
const ll MOD = 998244353;
const ll big = 1ll<<60;
ll gcd(ll x, ll y) { return (x % y)? gcd(y, x % y): y; }
int main(){
int n;
cin >> n;
vector<int>a(n), b(n);
rep(i, 0, n){
cin >> a[i];
a[i]--;
b[i] = i;
}
sort(a.begin(),a.end());
if(a==b){
cout << "Yes" << endl;
}else{
cout << "No" << endl;
}
return 0;
} |
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <assert.h>
#include <complex>
#include <utility>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <tuple>
#include <cmath>
#include <bitset>
#include <cctype>
#include <set>
#include <map>
#include <unordered_map>
#include <numeric>
#include <functional>
#include <chrono>
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(ll i=a;i<b;++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define _rrep(i,a) rrepi(i,a,0)
#define rrepi(i,a,b) for(ll i=a-1;i>=b;--i)
#define rrep(...) _overload3(__VA_ARGS__,rrepi,_rrep,)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()
#define PRINT(V) cout << V << "\n"
#define SORT(V) sort((V).begin(),(V).end())
#define RSORT(V) sort((V).rbegin(), (V).rend())
using namespace std;
using ll = long long;
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; }
inline void Yes(bool condition){ if(condition) PRINT("Yes"); else PRINT("No"); }
template<class itr> void cins(itr first,itr last){
for (auto i = first;i != last;i++){
cin >> (*i);
}
}
template<class itr> void array_output(itr start,itr goal){
string ans = "",k = " ";
for (auto i = start;i != goal;i++) ans += to_string(*i)+k;
if (!ans.empty()) ans.pop_back();
PRINT(ans);
}
ll gcd(ll a, ll b) {
return a ? gcd(b%a,a) : b;
}
const ll INF = 1e18;
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ll MOD3 = 1e6;
const ll EPS = 1e-10;
int sgn(const double a){
return (a < -EPS ? -1 : (a > EPS ? +1 : 0));
}
typedef pair<int,int> pi;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> tri;
typedef pair<double,double> point;
typedef complex<double> Point;
typedef string::const_iterator State;
const ll MAX = 125000;
constexpr ll nx[4] = {-1,0,1,0};
constexpr ll ny[4] = {0,1,0,-1};
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
ll n;
cin >> n;
ll ans = INF;
rep(i,n){
ll a,p,x;
cin >> a >> p >> x;
if (x-a > 0) chmin(ans,p);
}
if (ans == INF) PRINT(-1);
else PRINT(ans);
}
| #include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) begin(v),end(v)
template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll<<30;
constexpr ll longINF = 1ll<<60;
constexpr ll MOD = 1000000007;
constexpr bool debug = 0;
//---------------------------------//
int main() {
int N;
cin >> N;
string S, T;
cin >> S >> T;
vector<int> A(N + 1), B(N + 1);
REP(i, N) {
A[i + 1] = A[i] ^ (S[i] == '1');
B[i + 1] = B[i] ^ (T[i] == '1');
}
cout << [&]() -> ll {
ll ans = 0;
int pos = -1, col = 0;
REP(i, N + 1) {
int cur = i < pos ? A[pos] : A[i];
if (cur != B[i]) {
++pos;
chmax(pos, i);
while (pos <= N && A[pos] != B[i]) ++pos;
if (pos > N) return -1;
ans += pos - i;
}
}
return ans;
}() << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin>>s;
if(s[0]==s[1] and s[1]==s[2] and s[0]==s[2])
cout<<"Won"<<endl;
else
cout<<"Lost"<<endl;
return 0;
} | #ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include<bits/stdc++.h>
using namespace std;
#define rep(i,s,t) for(ll i = (ll)(s); i < (ll)(t); i++)
#define rrep(i,s,t) for(ll i = (ll)(s-1);(ll)(t) <= i; i--)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> Pll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
constexpr ll INF = numeric_limits<ll>::max()/4;
constexpr ll n_max = 2e5+10;
#define int ll
const long double pi = 3.14159265358979323846;
const long double eps = 1e-12;
template <typename T>
int comp(T a){
if (abs(a) < eps) return 0;
return a > 0 ? 1 : -1;
}
template <typename T>
int comp(T a, T b){
// a > b -> 1, a < b -> -1
return comp(a - b);
}
template <typename A, typename B>
string to_string(pair<A, B> p);
string to_string(const string &s) {return '"' + s + '"';}
string to_string(const char *c) {return to_string((string) c);}
string to_string(bool b) {return (b ? "true" : "false");}
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) + ")";}
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
template<class T>
bool chmax(T &a, T b){if(a < b){a = b; return true;} return false;}
template<class T>
bool chmin(T &a, T b){if(a > b){a = b; return true;} return false;}
signed main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
string s;cin >> s;
auto YES = [](bool ok){
cout << (ok ? "Won" : "Lost") << endl;
};
YES(s[0] == s[1] && s[1] == s[2]);
} |
#include <bits/stdc++.h>
using namespace std;
const int P = 998244353;
long long power(long long x, long long t) {
long long ret = 1;
for (; t; t >>= 1, x = x * x % P) {
if (t & 1) {
ret = ret * x % P;
}
}
return ret;
}
int main() {
int N, M;
cin >> N >> M;
long long ans = power(M, N) * N % P;
/* cerr << "ans = " << ans << '\n';
vector<vector<long long>> pw(5001, vector<long long> (5001));
for (int i = 1; i <= 5000; ++i) {
pw[i][0] = 1;
for (int j = 1; j <= 5000; ++j) {
pw[i][j] = pw[i][j - 1] * i % P;
}
}
auto pre = pw;
for (int i = 1; i <= 5000; ++i) {
for (int j = 1; j <= 5000; ++j) {
pre[i][j] += pre[i - 1][j];
pre[i][j] %= P;
}
}*/
for (int i = 1; i < N; ++i) {
long long tmp = 0;
for (int k = 1; k < M; ++k) {
tmp += power(k, i - 1) * power(M, N - i - 1) % P;
}
if (i == 1) {
ans -= power(M, N - 2) * (N - 1) % P;
ans += P;
ans %= P;
}
tmp = tmp * (N - i) % P;
ans -= tmp;
ans += P;
ans %= P;
} /*
for (int i = 0; i < N; ++i) {
for (int j = i + 1; j < N; ++j) {
if (j == i + 1) {
ans -= power(M, N - 2);
ans += P;
ans %= P;
}
long long tmp = 0;
tmp += pre[M - 1][j - i - 1];
tmp = tmp * pw[M][N - (j - i + 1)] % P;
ans -= tmp;
ans += P;
ans %= P;
for (int k = 1; k < M; ++k) {
ans -= power(k, j - i - 1) * power(M, N - (j - i + 1)) % P;
ans += P;
ans %= P;
}
}
}*/
cout << ans << '\n';
return 0;
}
| #include <bits/stdc++.h>
#define be(v) (v).begin(),(v).end()
#define pb(q) push_back(q)
#define rep(i, n) for(int i=0;i<n;i++)
typedef long long ll;
using namespace std;
const ll mod=1000000007, INF=(1LL<<60);
#define doublecout(a) cout<<fixed<<setprecision(10)<<a<<endl;
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
ll h, w;
cin >> h >> w;
ll sum = 0, mi = INF, a;
rep(i, h)rep(j, w) {
cin >> a;
sum += a;
mi= min(mi, a);
}
cout << sum - mi * h * w << endl;
return 0;
}
|
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define MOD (ll)(998244353)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);}
vii adj(30);
vector<bool>vis(30);
int n,m;
vector<int>comp;
void get(int v){
comp.pb(v);
vis[v] = 1;
for(int x:adj[v]){
if(!vis[x])get(x);
}
}
ll cnt = 0;
ll ans = 1;
void re(int cur,vector<int>comp,vector<int>color){
if(cur == sz(comp)){
cnt++;
return;
}
vector<bool>a(4,0);
int v = comp[cur];
for(int x:adj[v]){
a[color[x]] = 1;
}
for(int i=1;i<=3;i++){
if(!a[i]){
color[v] = i;
re(cur+1,comp,color);
}
}
}
int main()
{
cin>>n>>m;
for(int i=0;i<m;i++){
int v,u;
cin>>v>>u;
adj[v].pb(u);
adj[u].pb(v);
}
for(int i=1;i<=n;i++){
if(!vis[i]){
cnt = 0;
comp.clear();
get(i);
//for(int x:comp)cout<<x<<" ";
//cout<<'\n';
vector<int>color(30);
re(0,comp,color);
ans*=cnt;
}
}
cout<<ans;
}
| #include <bits/stdc++.h>
using namespace std;
int main()
{
int N, M, A, B;
cin >> N >> M;
vector<vector<int>> graph(N);
for (int i = 0; i < M; i++)
{
cin >> A >> B;
A--, B--;
graph.at(A).push_back(B);
graph.at(B).push_back(A);
}
vector<bool> checked(N,false);
vector<int> colors(N,-1), blank(N,-1);
pair<queue<int>, vector<int>> now_state;
queue<pair<queue<int>, vector<int>>> big_que;
queue<int> que;
int now_node;
bool is_OK;
long long ans = 0, tmp_ans;
for (int i = 0; i < N; i++)
{
if (checked.at(i))
continue;
colors = blank;
colors.at(i) = 0;
que.push(i);
big_que.push(make_pair(que,colors));
que.pop();
checked.at(i) = true;
tmp_ans = 0;
while (!big_que.empty())
{
now_state = big_que.front();
big_que.pop();
is_OK = true;
while (!now_state.first.empty())
{
now_node = now_state.first.front();
for (int j = 0; j < graph.at(now_node).size(); j++)
{
if (now_state.second.at(graph.at(now_node).at(j)) == now_state.second.at(now_node))
{
is_OK = false;
break;
}
}
if (!is_OK) break;
for (int j = 0; j < graph.at(now_node).size(); j++)
{
if (now_state.second.at(graph.at(now_node).at(j)) >= 0)
continue;
now_state.second.at(graph.at(now_node).at(j)) = (now_state.second.at(now_node)+1)%3;
now_state.first.push(graph.at(now_node).at(j));
big_que.push(now_state);
now_state.second.at(graph.at(now_node).at(j)) = (now_state.second.at(now_node)+2)%3;
checked.at(graph.at(now_node).at(j)) = true;
}
now_state.first.pop();
}
if (is_OK)
tmp_ans++;
}
tmp_ans *= 3;
if (ans)
ans *= tmp_ans;
else
ans += tmp_ans;
}
cout << ans << endl;
}
|
#include <iostream>
#include <string>
#include <stdio.h>
#include <math.h>
#include <queue>
#include <algorithm>
#include <utility>
#include <vector>
#include <tuple>
#include <numeric>
using namespace std;
int main(int argc, char* argv[]){
int N;
cin >> N;
vector<int>P(N);
for (int i=0; i<N; i++){
cin >> P[i];
}
vector<int> num_list(300000, 0);
int min=0;
for(int i=0; i<N; i++){
num_list[P[i]] = 1;
for(int num=min; num<300000; num++){
if(num_list[num]==0){
cout << num<< endl;
if(num > min){
min = num;
}
break;
}
}
}
}
| #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define ll long long
int a[200010], b[200010];
ll d[200010];
int main() {
int n;
scanf("%d", &n);
for(int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
}
for(int i = 0; i < n; ++i) {
scanf("%d", &b[i]);
}
int maxa = a[0];
d[0] = 1ll*maxa*b[0];
printf("%lld\n", d[0]);
for(int i = 1; i < n; ++i) {
maxa = std::max(maxa, a[i]);
d[i] = std::max(d[i-1], 1ll*b[i]*maxa);
printf("%lld\n", d[i]);
}
return 0;
} |
#include<iostream>
#include<ctype.h>
using namespace std;
int main(void)
{
char S,T;
cin >> S;
cin >> T;
if(S == 'Y')
{
T = toupper(T);
}
cout << T << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main () {
char S, T;
cin >> S >> T;
if (S == 'Y') {
T = T - 0x20;
cout << T << endl;
return 0;
}
if (S == 'N') {
cout << T << endl;
return 0;
}
}
|
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define rep3(i, s, n) for (int i = (s); i > (int)(n); i--)
#define all(v) v.begin(), v.end()
#define pb push_back
#define sz(x) ((int)(x).size())
typedef long long ll;
using namespace std;
using Graph = vector<vector<int>>;
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; }
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int X, Y; cin >> X >> Y;
bool f;
cout << (abs(X - Y) < 3 ? "Yes\n" : "No\n");
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = ll(1e18) + 5;
struct edge {
ll to, cost;
};
int main() {
ll N, M;
cin >> N >> M;
vector<vector<edge>> g(N);
rep(i, M) {
ll A, B, C;
cin >> A >> B >> C;
--A;
--B;
g[A].push_back(edge{B, C});
}
rep(i, N) {
vector<ll> d(N, INF);
priority_queue<P, vector<P>, greater<P>> pq;
pq.push(P(0, i));
while (!pq.empty()) {
P p = pq.top();
pq.pop();
if (p.first > d[p.second]) {
continue;
}
for (edge e : g[p.second]) {
if (d[e.to] > p.first + e.cost) {
d[e.to] = p.first + e.cost;
pq.push(P(d[e.to], e.to));
}
}
}
if (d[i] == INF) {
cout << -1 << "\n";
} else {
cout << d[i] << "\n";
}
}
return 0;
} |
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
ios::sync_with_stdio(0);
ll a,b,x,y;
cin>>a>>b>>x>>y;
if(a<=b) cout<<min(x+(b-a)*y,(b-a)*2*x+x)<<endl;
else cout<<min(x+(a-b-1)*y,(a-b-1)*2*x+x)<<endl;
return 0;
}
| #include<bits/stdc++.h>
#include<tuple>
typedef long long ll;
#define INF 9999999999
#define mod 1000000007
using namespace std;
typedef pair<ll,ll>du;
ll dp[5][105],a,b,x,y;
int main(){
ll i,j,k;
ios::sync_with_stdio(0);
cin>>a>>b>>x>>y;
for(ll i=0;i<=100;i++)
dp[1][i]=dp[0][i]=INF;
dp[0][a]=0;
for(ll i=a;i<=100;i++){
dp[1][i]=min(dp[1][i],dp[0][i]+x);
dp[0][i]=min(dp[0][i],dp[1][i]+x);
dp[1][i+1]=min(dp[1][i+1],dp[1][i]+y);
dp[0][i+1]=min(dp[0][i+1],dp[0][i]+y);
dp[0][i+1]=min(dp[0][i+1],dp[1][i]+x);
}
for(ll i=a;i>=1;i--){
dp[1][i]=min(dp[1][i],dp[0][i]+x);
dp[0][i]=min(dp[0][i],dp[1][i]+x);
dp[1][i-1]=min(dp[1][i-1],dp[1][i]+y);
dp[0][i-1]=min(dp[0][i-1],dp[0][i]+y);
dp[1][i-1]=min(dp[1][i-1],dp[0][i]+x);
}
cout<<dp[1][b]<<endl;
return 0;
} |
#include<cstdio>
#include<iostream>
using namespace std;
#define N 2005
#define ll long long
const int mod=1e9+7;
int n,m;
string s;
ll sum1[N][N],sum2[N][N],sum3[N][N];
int dp[N][N],map[N][N];
int main()
{
scanf("%d%d\n",&n,&m);
for (int i=1 ; i<=n ; i++){
cin>>s;
for (int j=1 ; j<=m ; j++)
map[i][j]=s[j-1]=='.' ?1:0;
}
dp[1][1]=1;
for (int i=1 ; i<=n ; i++){
for (int j=1 ; j<=m ; j++){
if (i==1 && j==1) continue;
if (map[i][j]==0) dp[i][j]=0;
else{
sum1[i][j]=(sum1[i][j-1]+dp[i][j-1])%mod;
sum2[i][j]=(sum2[i-1][j]+dp[i-1][j])%mod;
sum3[i][j]=(sum3[i-1][j-1]+dp[i-1][j-1])%mod;
dp[i][j]=(sum1[i][j]+sum2[i][j]+sum3[i][j])%mod;
}
}
}
printf("%d\n",dp[n][m]%mod);
return 0;
} | #include <bits/stdc++.h>
#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 MP make_pair
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int MOD = 1e9 + 7;
int N;
LL a[3005], dp[3005], dp2[3005];
LL sum[3005];
int main()
{
scanf("%d", &N);
rep1(i, N) scanf("%lld", &a[i]);
rep1(i, N) a[i] += a[i - 1];
dp2[0] = 1;
LL ret = 0;
rep1(k, N) {
rep(i, k) sum[i] = 0;
rep1(i, N) {
sum[a[i - 1] % k] += dp2[i - 1];
if (sum[a[i - 1] % k] >= MOD) sum[a[i - 1] % k] -= MOD;
dp[i] = sum[a[i] % k];
}
ret += dp[N];
if (ret >= MOD) ret -= MOD;
rep(i, N + 1) dp2[i] = dp[i];
}
printf("%lld\n", ret);
return 0;
} |
#define _DEBUG
#include "bits/stdc++.h"
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0,a1,a2,a3,a4,x,...) x
#define debug_1(x1) cout<<#x1<<": "<<x1<<endl
#define debug_2(x1,x2) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<endl
#define debug_3(x1,x2,x3) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<endl
#define debug_4(x1,x2,x3,x4) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<endl
#define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<", "#x5<<": "<<x5<<endl
#ifdef _DEBUG
#define debug(...) CHOOSE((__VA_ARGS__,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__)
#else
#define debug(...)
#endif
#define rep(index,num) for(int index=0;index<(int)num;index++)
#define rep1(index,num) for(int index=1;index<=(int)num;index++)
#define brep(index,num) for(int index=(int)num-1;index>=0;index--)
#define brep1(index,num) for(int index=(int)num;index>0;index--)
#define scan(argument) cin>>argument
#define prin(argument) cout<<argument<<endl
#define kaigyo cout<<endl
#define eps 1e-7
#define mp(a1,a2) make_pair(a1,a2)
#define ALL(a) (a).begin(),(a).end()
#define rALL(a) (a).rbegin(),(a).rend()
typedef long long ll;
typedef long double ld;
using namespace std;
typedef pair<ll,ll> pll;
typedef pair<int,int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
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; }
ll INFl=(ll)1e+18+1;
int INF=1e+9+1;
int main(){
int N,A,B;
scan(N>>A>>B);
prin(N-A+B);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repp(i, st, en) for (ll i = (ll)st; i < (ll)(en); i++)
#define repm(i, st, en) for (ll i = (ll)st; i >= (ll)(en); i--)
#define all(v) v.begin(), v.end()
void chmax(ll &x, ll y) {x = max(x,y);}
void chmin(ll &x, ll y) {x = min(x,y);}
void Yes() {cout << "Yes" << endl; exit(0);}
void No() {cout << "No" << endl; exit(0);}
template<class in_Cout> void Cout(in_Cout x) {cout << x << endl; exit(0);}
template<class in_vec_cout>
void vec_cout(vector<in_vec_cout> vec) {
for (in_vec_cout res : vec) {cout << res << " ";}
cout << endl;
}
const ll inf = 1e18;
const ll mod = 1e9 + 7;
ll N, X, S, T;
vector<ll> a, b, A, B;
int main() {
cin >> N >> X;
rep(i,N) {
ll x; cin >> x;
if (i%2) a.push_back(x);
else b.push_back(x);
}
S = a.size(), T = b.size();
rep(bit,1<<S) {
ll t = 0;
rep(i,S) if ((bit>>i)&1) t += a[i];
A.push_back(t);
}
rep(bit,1<<T) {
ll t = 0;
rep(i,T) if ((bit>>i)&1) t += b[i];
B.push_back(t);
}
sort(all(A));
sort(all(B));
ll ans = 0;
for (ll x : A) {
if (x>X) break;
ll l = -1, r = B.size();
while (r-l>1) {
ll m = (l+r)/2;
if (x+B[m]<=X) l = m;
else r = m;
}
chmax(ans,x+B[l]);
}
Cout(ans);
} |
#include <bits/stdc++.h>
#define FastIO ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
int main() {
FastIO;
int n , a , b;
cin >> n >> a >> b;
cout << n-a+b;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
// 総数を1000000007(素数)で割った余り
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 Graph vector<vector<int>>
#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(v.begin(), v.end());
#define RSORT(x) sort(rall(x));
#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; }
void func(long long N, long long A, long long B){
}
int main(){
long long N;
scanf("%lld",&N);
long long A;
scanf("%lld",&A);
long long B;
scanf("%lld",&B);
cout << N-A+B << endl;
return 0;
}
|
#include <bits/stdc++.h>
#define pan(i,l,r) for(int i=l ; i < r; i++)
#define ll long long int
#define pb push_back
#define PI 3.141592653
#define pll pair<ll,ll>
#define V vector
#define F first
#define S second
#define INF INT_MAX
using namespace std;
ll lcm(ll a, ll b){ return (a / __gcd(a, b)) * b;}
void solve(){
ll a, b, c;
cin >> a >> b >> c;
map<ll,ll> m;
m[1] = 6;
m[6] = 1;
m[2] = 5;
m[5] = 2;
m[3] = 4;
m[4] = 3;
cout << m[a]+m[b]+m[c] << endl;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int tst = 1;
//cin >> tst;
while(tst--)
solve();
return 0;
}
| #include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
using namespace std;
int main(){
ios::sync_with_stdio(0), cin.tie(0);
int a, b , c;
cin >> a >> b >> c;
cout << 21 - (a + b + c) << '\n';
} |
#include<bits/stdc++.h>
#define ll long long int
#define pii pair<int,int>
#define F first
#define S second
#define mod 1000000007
using namespace std;
ll add(ll a,ll b){ return ((a%mod)+(b%mod))%mod; }
ll mul(ll a,ll b){ return ((a%mod)*(b%mod))%mod; }
ll sub(ll a,ll b){ ll curr=((a%mod)-(b%mod))%mod; if(curr<0) curr+=mod; return curr; }
ll power(ll a,ll b){
if(b==0) return 1;
if(b==1) return a;
ll res=power(a,b/2);
res=mul(res,res);
if(b%2) res=mul(res,a);
return res;
}
void solve(){
double a,b; cin>>a>>b;
double ans=(100.0*a-100.0*b)/a;
cout << ans <<endl;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(NULL);
int t=1; ///cin>>t;
while(t--){
solve();
}
}
| #include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
#define INF 1e9
#define rep(i, n) for(int i = 0; i < n; i++)
#define repr(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define all(x) (x).begin(),(x).end()
int main() {
int A,B;
cin >> A >> B;
double ans;
ans = 100.0 * (A - B) / A;
cout << ans << endl;
} |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
ll n ;
cin >> n;
vector<ll> v(n);
for(ll i=0; i<n; i++)
cin >> v[i];
sort(v.rbegin(),v.rend());
ll suff[n+10];
suff[n-1] = v[n-1];
for(ll i=n-2; i>=0; i--)
{
suff[i] = suff[i+1] + v[i];
}
ll res = 0;
for(ll i=0; i<n-1; i++)
{
res += v[i]*(n-i-1) - suff[i+1] ;
}
cout << res << endl;
}
| #include<bits/stdc++.h>
#define ll long long int
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vpii vector< pii >
#define vpll vector< pll >
#define mpii map<int,int>
#define mpll map<ll,ll>
#define MOD 1000000007
#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 FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORB(i,a,b) for(int i=a;i>=b;i--)
#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;
}
bool octal(ll n)
{
while(n>0)
{
ll p=n%8;
if(p==7)return 1;
n/=8;
}
return 0;
}
bool decimal(ll n)
{
while(n>0)
{
ll p=n%10;
if(p==7)return 1;
n/=10;
}
return 0;
}
ll dp[200006];
int main()
{
fast
int n;
cin>>n;
vec v(n);read0(v,n);
sort(all(v));
ll sum=0;
vec temp;
FOR(i,1,s(v)-1)
{
temp.pb(v[i]-v[i-1]);
}
ll m=n-1;
dp[0]=temp[0];
sum=dp[0];
FOR(i,1,m-1)
{
dp[i]=dp[i-1]+temp[i];
sum+=dp[i];
}
ll prev=0;
ll k=sum;
FOR(i,1,m-1)
{
// cout<<sum<<' '<<prev<<nl;
prev+=(m-i+1)*temp[i-1];
sum+=k-prev;
}
cut(sum)
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD=1000000007;
#define INF 1LL<<30
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
int did[202020];
vector<int> ret;
int main() {
int n;
cin>>n;
vector<int> p(n),r(n);
rep(i,n){
cin>>p[i];
p[i]--;
r[p[i]]=i;
}
rep(i,n){
int x=r[i];
while(x>i){
if(did[x-1]==1){
cout<<-1<<endl;
return 0;
}
did[x-1]=1;
ret.push_back(x);
swap(p[x-1],p[x]);
r[p[x]]=x;
r[p[x-1]]=x-1;
x--;
}
}
rep(i,n-1){
if(did[i]==0){
cout<<-1<<endl;
return 0;
}
}
for(auto e : ret) cout<<e<<endl;
} | #include<bits/stdc++.h>
#define lint long long
#define st first
#define nd second
#define INF 0x3f3f3f3f
#define N 28
using namespace std;
lint log5(lint n){
lint ans = 0;
while(n > 0){
ans++;
n/=5;
}
return ans;
}
int bb(const vector<lint> &pot5, lint val){
if(val > pot5.back()) return -1;
int l=0, r = (int) pot5.size() -1;
while(l <= r){
int m = (l+r)/2;
if(pot5[m] == val) return m;
if(pot5[m] < val) l = m + 1;
else r = m-1;
}
return -1;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
lint n;
cin>>n;
vector<lint> pot5(N);
pot5[0] = 1;
for(int i=1; i<N; i++){
pot5[i] = pot5[i-1] * 5LL;
assert(pot5[i] > pot5[i-1]);
}
assert(pot5.back() > (lint) 1e18);
lint pot = 1;
int id = 0;
while(pot <= n){
int id5 = bb(pot5, n - pot);
if(id > 0 && id5 > 0){
cout<<id<<" "<<id5<<"\n";
return 0;
}
pot *= 3LL;
id++;
}
cout<<"-1\n";
return 0;
}
|
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
# define rep(i,a,b) for(int i=(a); i<=(b); ++i)
# define drep(i,a,b) for(int i=(a); i>=(b); --i)
typedef long long int_;
inline int readint(){
int a = 0; char c = getchar(), f = 1;
for(; c<'0'||c>'9'; c=getchar())
if(c == '-') f = -f;
for(; '0'<=c&&c<='9'; c=getchar())
a = (a<<3)+(a<<1)+(c^48);
return a*f;
}
const int MaxN = 400005;
pair<int,int> a[MaxN];
pair<int,int> b[MaxN];
vector<int> v;
int main(){
int n = readint();
rep(i,1,n<<1){
a[i].first = readint();
a[i].second = i;
b[i] = a[i]; // copy
}
sort(b+1,b+(n<<1)+1);
rep(i,1,n<<1)
if(v.empty())
v.push_back(i), putchar('(');
else{
int t = v.back();
if((a[t] > b[n]) == (a[i] > b[n]))
v.push_back(i), putchar('(');
else v.pop_back(), putchar(')');
}
putchar('\n');
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>
using namespace std;
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("avx,avx2,fma")
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef set<int> si;
typedef set<ll> sll;
typedef map<int, int> mii;
typedef map<ll, ll> mll;
const double pi = acos(-1.0);
const long long INF = 2e18;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define sz size()
#define in insert
#define endl "\n"
#define sqr(a) ((a) * (a))
#define jor(a) !(a&1) //jor means EVEN number
#define bjor(a) (a&1) //bjor means ODD number
#define all(a) a.begin(), a.end()
#define mem(arr, b) memset(arr, b, sizeof(arr)) //memset only for -1 and 0
//fill(a, a+n, 5) //fill for any value
#define for0(i,b) for(int i=0;i<(b);i++)
#define for1(i,b) for(int i=1;i<=(b);i++)
#define forab(i,a,b) for(int i=(a);i<=(b);i++)
#define forba(i,b,a) for(int i=(b);i>=(a);i--)
#define rep(i,a,b,c) for(int i=(a);i!=(b);i+=(c))
#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define fileRead freopen("input.txt", "r", stdin);
#define fileWrite freopen("output.txt", "w", stdout);
void print(int a[], int n){ // for testing or debugging
for(int i=0;i<n;i++)cout<<a[i]<<" "; cout<<endl;
}
#define watch(x) cout << (#x) << " is " << (x) << endl;
//-------------------------------------------------------------------
int main()
{
FAST
int a,b; cin>>a>>b;
cout<<a*2+100 - b;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i = (m); i <= (n); i++)
#define zep(i,m,n) for(ll i = (m); i < (n); i++)
#define rrep(i,m,n) for(ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define printa(x,m,n) for(int i = (m); i <= n; i++){cout << (x[i]) << " ";} cout<<endl;
int main(){
cin.tie(0); ios::sync_with_stdio(false);
ll n; cin >> n;
string s, t; cin >> s >> t;
ll ans = 0;
queue<ll> p, q;
zep(i, 0, n){
if(t[i] == '1')q.push(i);
if(s[i] == '1'){
if(!p.empty()){
ans += i - p.front();
p.pop();
}else if(!q.empty()){
ans += i - q.front();
q.pop();
}else{
p.push(i);
}
}
}
if(p.empty() && q.empty()){
print(ans)
}else{
print(-1)
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
const int N = 50 + 9;
pair<int, int> a[N];
int x[N];
int main(){
int n, m, q;
scanf("%d %d %d", &n, &m, &q);
for (int i = 1; i <= n; ++i){
scanf("%d %d", &a[i].first, &a[i].second);
}
sort(a + 1, a + n + 1);
for (int i = 1; i <= m; ++i){
scanf("%d", x + i);
}
for (int i = 1; i <= q; ++i){
int l, r;
scanf("%d %d", &l, &r);
vector<int> vc;
for (int j = 1; j <= m; ++j){
if (!(l <= j && j <= r)) vc.push_back(x[j]);
}
sort(vc.begin(), vc.end());
priority_queue<int> pq;
int ans = 0;
for (int j = 0, sz = vc.size(), k = 1; j < sz; ++j){
while (k <= n && a[k].first <= vc[j]){
pq.push(a[k].second);
++k;
}
if (!pq.empty()){
ans += pq.top();
pq.pop();
}
}
printf("%d\n", ans);
}
return 0;
}
| #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 = 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 long 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) {
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 << 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, m, q; cin >> n >> m >> q;
vector<int> w(n), v(n);
rep(i, n)cin >> w[i] >> v[i];
vector<int> x(m);
rep(i, m)cin >> x[i];
vector<P> vp;
rep(i, n) {
vp.push_back({ v[i],w[i] });
}
sort(all(vp), greater<P>());
rep(i, q) {
int l, r; cin >> l >> r; l--; r--;
multiset<int> st;
rep(j, m) {
if (l <= j && j <= r)continue;
st.insert(x[j]);
}
int ans = 0;
for (P p : vp) {
int val = p.first;
int wei = p.second;
if (st.find(wei) != st.end()) {
ans += val; st.erase(st.find(wei));
}
else {
st.insert(wei);
auto itr = st.find(wei);
itr++;
if (itr != st.end()) {
ans += val; st.erase(itr);
}
st.erase(st.find(wei));
}
}
cout << ans << "\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;
} |
#include <iostream>
using namespace std;
using ll = long long;
int mod = 1e9 + 7;
char s[2008][2008];
ll ans[4][2008][2008];
ll dp[2008][2008];
int main()
{
int n, m;
cin >> n >> m;
dp[1][1] = 1;
for (int i = 1; i <= n; i++) cin >> s[i] + 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (s[i][j] == '#') {
ans[1][i][j] = 0;
ans[2][i][j] = 0;
ans[3][i][j] = 0;
continue;
}
dp[i][j] += ans[1][i - 1][j];
dp[i][j] %= mod;
dp[i][j] += ans[2][i][j - 1];
dp[i][j] %= mod;
dp[i][j] += ans[3][i - 1][j - 1];
dp[i][j] %= mod;
ans[1][i][j] = (ans[1][i - 1][j] + dp[i][j]) % mod;
ans[2][i][j] = (ans[2][i][j - 1] + dp[i][j]) % mod;
ans[3][i][j] = (ans[3][i - 1][j - 1] + dp[i][j]) % mod;
}
}
cout << dp[n][m] << endl;
return 0;
}
| //#pragma GCC optimize ("O2")
//#pragma GCC target ("avx2")
//#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
#include<cstdio>
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
ll dph[2000], dphw[4000], *naname = dphw + 2000;
const int mod = 1e9 + 7;
int main() {
//cin.tie(0);
//ios::sync_with_stdio(false);
int H = 0, W = 0;
char c;
while ((c = getchar_unlocked()) >= '0') H = H * 10 + c - '0';
while ((c = getchar_unlocked()) >= '0') W = W * 10 + c - '0';
getchar_unlocked();
dph[0] = 1;
naname[0] = 1;
ll w = 1;
ll tmp = 1;
rep1(i, W - 1) {
tmp = w;
if (getchar_unlocked() == '.') {
dph[i] = w;
naname[i] = w;
w = w * 2 % mod;
}
else {
dph[i] = 0;
naname[i] = 0;
w = 0;
}
}
getchar_unlocked();
rep1(i, H - 1) {
if (getchar_unlocked() == '.') {
tmp = dph[0];
w = tmp;
dph[0] = (tmp + dph[0]) % mod;
naname[-i] = (tmp + naname[-i]) % mod;
}
else {
w = 0;
dph[0] = 0;
naname[-i] = 0;
}
if (!(i & 3)) {
rep1(j, W - 1) {
if (getchar_unlocked() == '.') {
tmp = (w + dph[j] + naname[j - i]) % mod;
w = (w + tmp) % mod;
dph[j] = (tmp + dph[j]) % mod;
naname[j - i] = (tmp + naname[j - i]) % mod;
}
else {
w = 0;
dph[j] = 0;
naname[j - i] = 0;
}
}
}
else {
rep1(j, W - 1) {
if (!(j & 3)) {
if (getchar_unlocked() == '.') {
tmp = (w + dph[j] + naname[j - i]) % mod;
w = (w + tmp) % mod;
dph[j] = (tmp + dph[j]) % mod;
naname[j - i] = (tmp + naname[j - i]) % mod;
}
else {
w = 0;
dph[j] = 0;
naname[j - i] = 0;
}
}
else {
if (getchar_unlocked() == '.') {
tmp = w + dph[j] + naname[j - i];
w = w + tmp;
dph[j] = tmp + dph[j];
naname[j - i] = tmp + naname[j - i];
}
else {
w = 0;
dph[j] = 0;
naname[j - i] = 0;
}
}
}
}
getchar_unlocked();
}
printf("%lld\n", tmp % mod);
Would you please return 0;
} |
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define FIO ios_base::sync_with_stdio(false); cin.tie(0);
#define trav(x,a) for (auto& x: a)
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define mem(a,v) memset((a), (v), sizeof (a))
#define endl "\n"
#define case(t) cout << "Case #" << (t) << ": "
#define reada(a, n) for (int _i = 0; _i < (n); _i++) read(a[_i])
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define st first
#define nd second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int,int,hash<int>> ht;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
const double pi = acos(-1);
const int mod = 1e9 + 7;
const int inf = 1e9 + 7;
const int N = 1e6 + 5;
const double eps = 1e-9;
template<class T> void read(T& x) { cin >> x; }
template<class X, class Y> void read(pair<X,Y>& a) { read(a.first), read(a.second); }
template<class T, size_t U> void read(array<T,U>& x) { for (int i = 0; i < U; i++) read(x[i]); }
template<class T> void read(vector<T>& x) { trav(y, x) read(y); }
template<class T, class... O> void read(T& x, O&... y) { read(x), read(y...); }
string to_string(const char& x) { return string(1,x); }
string to_string(const char*& x) { return (string)x; }
string to_string(const string& x) { return x; }
template<class T, class U> string to_string(const pair<T,U>& x) { return to_string(x.first) + " " + to_string(x.second); }
template<class T, size_t U> string to_string(const array<T,U>& x) { string ret = ""; for (int i = 0; i < U; i++) ret += (!i ? "" : " ") + to_string(x[i]); return ret; }
template<class T> string to_string(const vector<T>& x) { string ret = ""; bool f = 0; trav(y, x) ret += (!f ? "" : " ") + to_string(y), f = 1; return ret; }
template<class T> string to_string(const set<T>& x) { string ret = ""; bool f = 0; trav(y, x) ret += (!f ? "" : " ") + to_string(y), f = 1; return ret; }
void print() { cout << endl; }
template<class T> void pr(const T& x) { cout << to_string(x); }
template<class T, class... O> void print(const T& x, const O&... y) { pr(x); if (sizeof...(y)) pr(" "); print(y...); }
int a[N], b[N];
ll solvePre(int l, int r) {
deque<int> q;
ll ret = 0;
for (int i = r; i >= l-1; i--) {
if (sz(q) && a[i] + sz(q) == b[q.back()])
ret += sz(q);
while (sz(q) && a[i] + sz(q) == b[q.back()])
q.pop_back();
q.push_front(i);
}
if (sz(q) > 1) {
print(-1);
exit(0);
}
return ret;
}
void flip(int l, int r) {
reverse(a + l, a + r + 1);
reverse(b + l, b + r + 1);
for (int i = l; i <= r; i++)
a[i] *= -1, b[i] *= -1;
}
ll solveSuff(int l, int r) {
flip(l-1, r+1);
ll ret = solvePre(l, r);
flip(l-1, r+1);
return ret;
}
ll solve(int l, int r) {
for (int i = l; i <= r+1; i++)
if (a[i] <= b[i])
return solvePre(l, i-1) + solveSuff(i, r);
return 0;
}
int main() {
FIO
int n, l; read(n, l);
for (int i = 1; i <= n; i++)
read(a[i]);
for (int i = 1; i <= n; i++)
read(b[i]);
a[0] = 0, a[n+1] = l+1;
b[0] = 0, b[n+1] = l+1;
ll ans = 0;
for (int i = 1, j = 1; i <= n+1; i++)
if (a[i] == b[i])
ans += solve(j, i-1), j = i+1;
print(ans);
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
using ll=long long;
int main(void)
{
ll N, M;
cin >> N >> M;
vector<string> S(N);
for (ll i = 0; i < N; i++)
{
cin >> S[i];
}
ll a = 1;
ll b = 0;
for (ll i = 1; i < N; i++)
{
ll count = 0;
for (ll j = 0; j < M; j++)
{
if (S[0][j] != S[i][j])
{
count ++;
}
}
if (count % 2 == 0)
{
a++;
}
else
{
b++;
}
}
cout << a * b << endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) begin(v),end(v)
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll<<30;
constexpr ll longINF = 1ll<<60;
constexpr ll MOD = 1000000007;
constexpr bool debug = false;
//---------------------------------//
template<typename T>
struct BinaryIndexedTree {
using value_type = T;
using const_reference = const value_type &;
using F = std::function<value_type(const_reference, const_reference)>;
using size_type = std::size_t;
BinaryIndexedTree(size_type n, const F & f, const_reference id_elem) : n(n), f(f), id_elem(id_elem) {
node.resize(n + 1, id_elem);
}
size_type size() const noexcept {
return n;
}
void add(size_type i, const_reference x) {
assert(i < size());
++i;
for (; i <= size(); i += i & -i) node[i] = f(node[i], x);
}
// [0, i)
value_type sum(size_type i) const {
assert(i <= size());
value_type res = id_elem;
for (; i > 0; i -= i & -i) res = f(res, node[i]);
return res;
}
// sum[0, r] <= x を満たす最小の r を返す (存在しなければ size())
size_type lower_bound(const_reference x) const {
size_type res = 0;
size_type s = id_elem, w = 1;
while (w < size()) w <<= 1;
for (; w > 0; w >>= 1) {
if (res + w <= size()) {
value_type cur = f(s, node[res + w]);
if (cur < x) {
res += w;
s = cur;
}
}
}
return res;
}
private:
size_type n;
F f;
value_type id_elem;
std::vector<value_type> node;
};
int main() {
int N;
cin >> N;
vector<int> A(N);
REP(i, N) scanf("%d", &A[i]);
BinaryIndexedTree<int> bit(N, [](int a, int b) { return a + b; }, 0);
ll ans = 0;
REP(i, N) {
ans += bit.sum(N) - bit.sum(A[i]);
bit.add(A[i], 1);
}
REP(i, N) {
printf("%lld\n", ans);
if (i + 1 == N) break;
ans -= bit.sum(A[i]);
ans += bit.sum(N) - bit.sum(A[i] + 1);
}
} | #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 = 3e5+100;
const ll INF = 1e9;
const int mod1 = 1e9+7;
const int mod2 = 1e9+21;
int f[MAXN];
void add(int pos,int x)
{
for (;pos<MAXN;pos|=pos+1)
f[pos]+=x;
}
int res(int l)
{
int sum=0;
for (;l>=0;l=((l+1)&l)-1)
sum+=f[l];
return sum;
}
int res(int l,int r)
{
if (l>r)
return 0;
return res(r)-res(l-1);
}
void solve()
{
int n;
cin>>n;
vector <int> p(n);
in(p);
ll ans=0;
for (int i=0;i<n;i++)
{
ans+=res(p[i]+1,n-1);
add(p[i],1);
}
cout<<ans<<'\n';
for (int i=0;i<n-1;i++)
{
ans-=p[i];
ans+=n-1-p[i];
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>
#define mod 1000000007
#define fast_io ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define vi vector<int>
#define vl vector<ll>
#define REP(s, e) for (auto i = s; i <= e; i++)
#define show(s) cout<<s<<" "
#define MAXI 2147483647
#define MAXL 9223372036854775807
#define FOR(a,b) for (ll i = a; i < b; i++)
using namespace std;
typedef signed long long int ll;
int main() {
fast_io;
int n; cin >> n;
vector<string> opers = {"dummy"};
for (int i = 0; i < n; i++)
{
string inp; cin >> inp;
opers.push_back(inp);
}
vector<pair<ll,ll>> dp;
dp.push_back({1,0});
for (int i = 1; i <= n; i++)
{
if (opers[i] == "OR")
{
dp.push_back({dp[i-1].first * 2 + dp[i-1].second, dp[i-1].second});
} else
{
dp.push_back({dp[i-1].first, dp[i-1].second * 2 + dp[i-1].first});
}
}
ll ans1 = dp[n].first;
dp.clear();
dp.push_back({0,1});
for (int i = 1; i <= n; i++)
{
if (opers[i] == "OR")
{
dp.push_back({dp[i-1].first * 2 + dp[i-1].second, dp[i-1].second});
} else
{
dp.push_back({dp[i-1].first, dp[i-1].second * 2 + dp[i-1].first});
}
}
ll ans2 = dp[n].first;
cout << ans1 + ans2;
}
| #include<bits/stdc++.h>
using namespace std;
int main(){
int N;cin>>N;
vector<long> A(N);
for(int i=0;i<N;i++) cin>>A.at(i);
map<long,vector<int>> x;
long cnt=1,m=0;
x[0].push_back(-1);
for(int i=0;i<N;i++){
m+=cnt*A[i];
x[m].push_back(i);
cnt*=-1;
}
long ans=0;
for(auto [c,v]:x){
m=v.size();
ans+=m*(m-1)/2;
}
cout<<ans<<endl;
} |
#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);
ll H, W;
// template <class T> void plus_mod(T &a, const T &b) {a = (a + b) % MOD;}
ll grid[2005][2005];
ll dp[2005][2005];
ll TE;
ll calc(ll y, ll x) {
if (dp[y][x] != INF) return dp[y][x];
ll te = y + x;
bool is_taka = (te % 2 == 0);
ll plus = is_taka ? grid[y][x] : -grid[y][x];
if (y == H - 1 && x == W - 1) {
dp[y][x] = -plus;
return -plus;
}
ll v;
if (x < W - 1 && y < H - 1) {
//
ll v1 = calc(y, x + 1);
ll v2 = calc(y + 1, x);
if (is_taka) {
v = max(v1, v2);
} else {
v = min(v1, v2);
}
} else if (x < W - 1) {
v = calc(y, x + 1);
} else if (y < H - 1) {
v = calc(y + 1, x);
}
v -= plus;
dp[y][x] = v;
return v;
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
std::fill((T *)array, (T *)(array + N), val);
}
void show_table(ll I, ll J) {
rep(i, I) {
rep(j, J) { cout << dp[i][j] << " "; }
cout << "" << endl;
}
cout << "" << endl;
}
int main() {
// std::cout << std::fixed << std::setprecision(10);
Fill(dp, INF);
// + 青
// - 赤
scanf("%lld %lld", &H, &W);
rep(y, H) {
string line;
cin >> line;
rep(x, W) {
char ch = line[x];
if (ch == '-') grid[y][x] = -1;
if (ch == '+') grid[y][x] = 1;
}
}
// cout << (calc(1, 0)) << endl;
if (H == 1 && W == 1) {
cout << ("Draw") << endl;
return 0;
}
ll TE = H + W - 2;
ll c = calc(0, 0);
ll v;
if (W >= 2 && H >= 2) {
ll v1 = dp[0][1];
ll v2 = dp[1][0];
v = max(v1, v2);
} else if (W >= 2) {
v = dp[0][1];
} else {
v = dp[1][0];
}
// cout << "v:" << (v) << endl;
if (v == 0) {
cout << ("Draw") << endl;
} else if (v >= 1) {
cout << ("Takahashi") << endl;
} else {
cout << ("Aoki") << endl;
}
// show_table(H, W);
}
| // https://atcoder.jp/contests/abc207/editorial/2153
#include<bits/stdc++.h>
using namespace std;
int main(){
int N; cin >> N;
vector<double> a(N),b(N),c(N),d(N);
for(int _=0; _<2; _++){
for(int i=0; i<N; i++) cin >> a[i] >> b[i];
int x = 0, y = 0;
for(int i=0; i<N; i++){
x += a[i];
y += b[i];
a[i] *= N;
b[i] *= N;
}
for(int i=0; i<N; i++){
a[i] -= x;
b[i] -= y;
}
swap(a,c);
swap(b,d);
}
for(int i=0; i<N; i++){
if(a[i]!=0 || b[i]!=0){
swap(a[i],a[0]);
swap(b[i],b[0]);
}
}
string ans = "No";
const double eps = 1e-6;
for(int i=0; i<N; i++){
double angle = atan2(d[i],c[i])-atan2(b[0],a[0]);
bool flag = true;
for(int j=0; j<N; j++){
double A = a[j]*cos(angle)-b[j]*sin(angle);
double B = a[j]*sin(angle)+b[j]*cos(angle);
bool flag2 = false;
for(int k=0; k<N; k++){
if(std::abs(A-c[k])<=eps && std::abs(B-d[k])<=eps) flag2 = true;
}
flag &= flag2;
}
if(flag) ans = "Yes";
}
cout << ans << endl;
}
|
/* OVERKILLED */
// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("avx,avx2,sse,sse2")
#include <array>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include<climits>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <complex>
#include <cmath>
#include <numeric>
#include <bitset>
#include <functional>
#include <random>
#include <ctime>
#include <chrono>
using namespace std;
#define ld long double
#define int long long
#define pii pair<int,int>
#define pb push_back
#define endl '\n'
const int inf = LLONG_MAX;
#define pi acos(-1)
#define init(x,a) memset(x,a,sizeof(x))
#define all(c) c.begin(),c.end()
//int const mod = 998244353;
int const mod = 1e9 + 7;
int const hashmod = 1610612741;
#define bitcount(x) __builtin_popcountll(x)
#define l first
#define r second
int toint(const string &s) {stringstream ss; ss << s; int x; ss >> x; return x;}
string tostring ( int number ) {stringstream ss; ss << number; return ss.str();}
#define trace1(x) cerr<<#x<<": "<<x<<endl
#define trace2(x, y) cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z) cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define haha ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
// * Using PBDS *
/*typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
os; */
struct cmp
{
bool operator() (pair<int, int> a, pair<int, int> b) {
return a.r > b.r;
}
};
int modexp(int x, int n) {
if (n == 0) {
return 1;
}
else if (n & 1) {
return (modexp((x * x) % mod, n / 2) % mod * x % mod) % mod;
}
else {
return (modexp((x * x) % mod, n / 2) % mod);
}
}
int modinv(int n)
{
return modexp(n, mod - 2) % mod;
}
int fact[400005];
void fact_pre() {
fact[0] = 1;
for (int i = 1; i <= 400000; i++) {
fact[i] = (fact[i - 1] * i) % mod;
}
}
int ncr(int n, int r) {
if (r > n) return 0;
int a = fact[n];
a *= modinv((fact[n - r] * fact[r]) % mod);
return a % mod;
}
int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
bool comp1(pii a, pii b) {
return a.l > b.l;
}
bool comp2(pair<pii, int> a, pair<pii, int> b) {
if (a.l.l == b.l.l)
return a.r < b.r;
return (a.l.l < b.l.l );
}
bool comp(string a, string b) {
return a[0] > b[0];
}
#define nax 10000005
int t1 = 0;
void go() {
int n;
cin >> n;
int arr[n];
int ans = inf;
for (int i = 0; i < n; i++)
cin >> arr[i];
int pre[n];
pre[0] = arr[0];
for (int i = 1; i < n; i++) {
pre[i] = pre[i - 1] | arr[i];
}
for (int i = 0; i < (1 << n); i++) {
int cn = i;
vector<int> v(n, 0);
int cx = 0;
int cn1 = 0;
for (int j = 0; j < n; j++) {
if (((i >> j) & 1) == 1)
{
v[j] = 1;
}
}
for (int j = 0; j < n; j++) {
if (v[j] == 1) {
cn1 |= arr[j];
cx ^= cn1;
cn1 = 0;
}
else {
cn1 |= arr[j];
}
}
cx ^= cn1;
ans = min(ans, cx);
}
cout << ans;
}
signed main() {
haha
int t;
t = 1;
while (t--) {
go();
}
} | #include <bits/stdc++.h>
#define F first
#define S second
#define MP make_pair
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define LCM(a, b) (a) / __gcd((a), (b)) * (b)
#define log_2(a) (log((a)) / log(2))
#define ln '\n'
using namespace std;
using LL = long long;
using ldouble = long double;
using P = pair<int, int>;
using LP = pair<LL, LL>;
static const int INF = INT_MAX;
static const LL LINF = LLONG_MAX;
static const int MIN = INT_MIN;
static const LL LMIN = LLONG_MIN;
static const int MOD = 1e9 + 7;
static const int SIZE = 200005;
const int dx[] = {0, -1, 1, 0};
const int dy[] = {-1, 0, 0, 1};
template<typename T>
void printArray(vector<T> &printVec) {
if(printVec.empty()) return;
cout << printVec[0];
for(int i = 1; i < printVec.size(); ++i) cout << " " << printVec[i];
cout << endl;
}
vector<LL> Div(LL n) {
vector<LL> ret;
for(LL 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;
}
LL res = LINF;
void dfs(vector<LL> &v, int pos, LL val, LL xo) {
if(pos == v.size()) {
res = min(res, xo ^ val);
return;
}
dfs(v, pos + 1, val | v[pos], xo);
xo ^= val;
dfs(v, pos + 1, v[pos], xo);
return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N;
cin >> N;
vector<LL> v(N);
for(int i = 0; i < N; ++i) {
cin >> v[i];
}
dfs(v, 0, 0, 0);
cout << res << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define REP(i, n) for(ll i = 0LL; i < (ll)(n); i++)
#define REPR(i, n) for (ll i = (ll)(n) - 1; i >= 0; i--)
#define FOR(i, n, m) for(ll i = (ll)n; i < (ll)(m); i++)
#define ALL(x) (x).begin(),(x).end()
#define MOD 1000000007
#define INF (1LL << 62)
#define PI (acos(-1))
#define PRINT(x) std::cout << x << endl
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; }
ll countDigit(ll n) { return floor(log10(n) + 1); }
typedef pair <ll,ll> P;
static const ll dx[8] = {0,1,0,-1, 1, -1, 1, -1}, dy[8] = {1,0,-1,0,1,1,-1,-1};
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
for (int i = 0; i < (int)v.size(); ++i) os << v[i] << " ";
return os;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& os, const map<T1, T2>& m)
{
for (auto p : m) os << "<" << p.first << ", " << p.second << "> ";
return os;
}
int main()
{
char S, T;
cin >> S >> T;
if (S == 'Y') {
char t = T - 'a' + 'A';
cout << t << endl;
} else cout << T << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
void Yes() {cout << "Yes\n";}
void No() {cout << "No\n";}
void YES() {cout << "YES\n";}
void NO() {cout << "NO\n";}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
string s, t;
cin >> s >> t;
if (s == "Y") t[0] += 'A' - 'a';
cout << t << '\n';
} |
#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define ceil(x) static_cast<ll>(ceil(x))
#define floor(x) static_cast<ll>(floor(x))
#define pow(x, y) static_cast<ll>(pow(x, y))
#define gcd(x, y) static_cast<ll>(__gcd(x, y))
#define lcm(x, y) static_cast<ll>(a * (b / gcd(x, y)))
#define F first
#define S second
#define eb emplace_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define FI(i, a, b) for (ll i{a}; i <= b; i++)
#define FD(i, a, b) for (ll i{a}; i >= b; i--)
typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<pair<ll, ll>> vpii;
typedef pair<ll, ll> pi;
const ll mod{static_cast<ll>(1e9) + 7};
const ld Pi{3.14159265358979323846};
auto int_from(istream &in) -> int
{
int x;
in >> x;
return x;
}
auto lng_from(istream &in) -> ll
{
ll x;
in >> x;
return x;
}
auto str_from(istream &in) -> string
{
string x;
in >> x;
return x;
}
auto chr_from(istream &in) -> char
{
char x;
in >> x;
return x;
}
int main()
{
IOS;
int n{int_from(cin)};
vi v;
bool ok{1};
FI(i, 1, n)
{
v.eb(int_from(cin));
}
sort(all(v));
FI(i, 0, n - 1)
{
if (v[i] != i + 1)
{
ok = 0;
break;
}
}
if (ok)
{
cout << "Yes\n";
}
else
{
cout << "No\n";
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int n,x,v[1020],z;
int main() {
cin>>n;
for(int i=1;i<=n;i++){
cin>>x;
z+=v[x]++;
}
if(z!=0){
cout<<"No";
}else{
cout<<"Yes";
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = (1LL << 60);
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
int main()
{
ll n;
cin >> n;
vector<pair<ll, ll>> x(n), y(n);
rep(i, n) cin >> x[i].first >> y[i].first;
rep(i, n)
{
x[i].second = i;
y[i].second = i;
}
sort(x.begin(), x.end());
sort(y.begin(), y.end());
vector<ll> ans;
if (x[n - 1].second == y[n - 1].second && x[0].second == y[0].second)
ans.push_back(max(abs(x[n - 1].first - x[0].first), abs(y[n - 1].first - y[0].first)));
else{
ans.push_back(abs(x[n - 1].first - x[0].first));
ans.push_back(abs(y[n - 1].first - y[0].first));
}
if (x[n - 1].second==y[n-1].second&&x[1].second==y[1].second)
ans.push_back(max(abs(x[n - 1].first - x[1].first), abs(y[n - 1].first - y[1].first)));
else{
ans.push_back(abs(x[n - 1].first - x[1].first));
ans.push_back(abs(y[n - 1].first - y[1].first));
}
if(x[0].second==y[0].second&&x[n-2].second==y[n-2].second)
ans.push_back(max(abs(x[n - 2].first - x[0].first), abs(y[n - 2].first - y[0].first)));
else{
ans.push_back(abs(x[n - 2].first - x[0].first));
ans.push_back(abs(y[n - 2].first - y[0].first));
}
sort(ans.begin(), ans.end());
cout << ans[(int)ans.size()-2] << endl;
} | #include <bits/stdc++.h>
#define rep(i,n) for(long long i = 0; i < (long long)(n); i++)
#define per(i,n) for(long long i = (long long)(n-1);i >= 0;--i)
#define REP(i,s,t) for(long long i=s;i<t;i++)
#define PER(i,s,t) for(long long i=s;i>t;--i)
#define all(x) x.begin(),x.end()
#define sp(n) setprecision(n)
typedef long long ll;
using namespace std;
int main(){
int n,k,m;
cin>>n>>k>>m;
int l=0;
rep(i,n-1){int j;
cin>>j;
l+=j;}
if(l+k<n*m){cout<<-1<<endl;}
else cout<<max((n*m-l),0)<<endl;
} |
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<ll, ll> PP;
#define overload4(_1, _2, _3, _4, name, ...) name
#define overload3(_1, _2, _3, name, ...) name
#define rep1(n) for (ll i = 0; i < n; ++i)
#define rep2(i, n) for (ll i = 0; i < n; ++i)
#define rep3(i, a, b) for (ll i = a; i < b; ++i)
#define rep4(i, a, b, c) for (ll i = a; i < b; i += c)
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep1(n) for (ll i = n; i--;)
#define rrep2(i, n) for (ll i = n; i--;)
#define rrep3(i, a, b) for (ll i = b; i-- > (a);)
#define rrep4(i, a, b, c) for (ll i = (a) + ((b) - (a)-1) / (c) * (c); i >= (a); i -= c)
#define rrep(...) overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)
#define elif else if
ll beki(ll i)
{
return 1ll < i;
}
#define skip(M) \
if (M) \
continue
#define all(v) v.begin(), v.end()
#define allm(x, y, M) for (auto [(x), (y)] : (M))
#define alls(i, S) for (auto(i) : (S))
#define pb push_back
#define fi first
#define se second
#pragma warning(disable : 4312)
template <class X>
void print(X x)
{
cout << x << endl;
}
template <class... T>
void print(T &...t)
{
(..., (cout << t << " "));
}
void print(vl x)
{
for (ll i : x)
{
cout << i << " ";
}
cout << endl;
}
void print(vector<PP> x)
{
for (PP i : x)
{
cout << i.first << " " << i.second << endl;
}
cout << endl;
}
template <class X>
int lbound(X a, vector<X> &Y)
{
return lower_bound(all(Y), a) - Y.begin();
}
template <class X>
int ubound(X a, vector<X> &Y)
{
return upper_bound(all(Y), a) - Y.begin();
}
template <class... T>
void zero(T &...t)
{
(..., (t = 0));
}
template <class... T>
void cl(T &...t)
{
(..., (t.clear()));
}
template <class... T>
void in(T &...t)
{
(..., (cin >> t));
}
template <class... T>
void put(vl &V, int n)
{
ll k;
rep(i, n)
{
cin >> k;
V.pb(k);
}
}
int max_index(vl &V)
{
return max_element(all(V)) - V.begin();
}
int min_index(vl &V)
{
return min_element(all(V)) - V.begin();
}
ll sum(vl &V)
{
return accumulate(all(V), 0);
}
void Uni(vl &V)
{
V.erase(unique(all(V)));
}
const ll INF = (1LL << 61) - 1;
const ll MOD = 1000000007;
const ll MOD2 = 998244355;
const ll MAX_N = 500010;
ll a, b, c, d, e, f, h, x, y, z, p, q, n, t, r, k, w, l, ans, u, v, m;
ll codeforces = 1;
string S, T;
vl A, B, C, D, ANS;
vector<PP> VP, VP2;
set<ll> s;
vector<vl> g(MAX_N);
void solve()
{
in(n);
rep(i, n)
{
cin >> a >> b >> c;
A.pb(a);
B.pb(b);
C.pb(c);
}
ans = INF;
rep(i, n)
{
if (A[i] < C[i])
ans = min(ans, B[i]);
}
if (ans == INF)
print(-1);
else
print(ans);
}
int main()
{
// cout<<fixed<<setprecision(15);
cin.tie(0);
ios::sync_with_stdio(false);
//cin >> codeforces;
while (codeforces--)
{
solve();
}
} | #include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef pair<int, int> pii;
int main(){
ll k, n, m; cin >> k >> n >> m;
vector<int> a(k), b(k);
vector<pair<double, ll>> v(k);
rep(i, k) cin >> a[i];
ll sum = 0;
rep(i, k){
b[i] = (m*a[i])/n;
sum += b[i];
v[i].first = ((double)(m*a[i]))/(double)n - b[i];
v[i].second = i;
}
sort(v.begin(), v.end());
reverse(v.begin(), v.end());
rep(i, m-sum){
b[v[i].second]++;
}
for(auto x : b)
cout << x << ' ';
cout << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define int long long
using vec_int = vector<int>;
using P = pair<int,int>;
using T = tuple<int,int,int>;
using ll = long long;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
int charToInt(char c){
char zero_num = '0';
return (int)c - (int)zero_num;
}
signed main(){
int N; cin>>N;
vec_int a(N), b(N);
rep(i,N)cin>>a.at(i);
rep(i,N)cin>>b.at(i);
vec_int max_a(N);
max_a.at(0) = a.at(0);
for(int i=1;i<N;i++){
max_a.at(i) = max(max_a.at(i-1), a.at(i));
}
vec_int max_mult(N);
for(int i=0;i<N;i++){
max_mult.at(i) = b.at(i) * max_a.at(i);
}
int max_val = 0;
for(int i=0;i<N;i++){
max_val = max(max_mult.at(i), max_val);
cout<<max_val<<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;
using P = pair<ll, ll>;
void solve(long long N, std::vector<long long> a, std::vector<long long> b) {
ll maxA = 0;
ll maxC = 0;
rep(i, N) {
maxC = max(maxC, a[i] * b[i]);
maxC = max(maxC, maxA * b[i]);
maxA = max(maxA, a[i]);
cout << maxC << "\n";
}
}
// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools (tips: You use the default template now. You can remove this line by using your custom template)
int main() {
long long N;
scanf("%lld", &N);
std::vector<long long> a(N);
for (int i = 0; i < N; i++) {
scanf("%lld", &a[i]);
}
std::vector<long long> b(N);
for (int i = 0; i < N; i++) {
scanf("%lld", &b[i]);
}
solve(N, std::move(a), std::move(b));
return 0;
}
|
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> p;
vector<p> v;
const int maxn=1e6+10;
ll a[maxn], b[maxn];
ll k, n, m;
int main()
{
ll sum=0;
cin>>k>>n>>m;
for(int i=0; i<k; i++){
cin>>a[i];
b[i]=a[i]*m/n;
sum+=b[i];
p temp;
temp.first=(double)(a[i]*m-b[i]*n);
temp.second=i;
v.push_back(temp);
}
sort(v.begin(), v.end(), greater<p>());
for(int i=0; i<m-sum; i++){
b[v[i].second]+=1;
}
for(int i=0; i<k; i++)
cout<<b[i]<<" ";
cout<<endl;
return 0;
} | //vec[i]の範囲外エラー表示
#define _GLIBCXX_DEBUG
//includeとusing
#include <bits/stdc++.h>
using namespace std;
//型名省略
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
template <class T> using P = pair<T,T>;
template <class T> using M = map<T,T>;
template <class T> using S = set<T>;
template <class T> using PQ = priority_queue<T>;
template <class T> using PQG = priority_queue<T,V<T>,greater<T>>;
using ll = long long;
using st = string;
using vl = V<ll>;
using vvl = V<V<ll>>;
using vc = V<char>;
using vvc = V<V<char>>;
using vs = V<st>;
using vvs = V<V<st>>;
using vb = V<bool>;
using vvb = V<V<bool>>;
//定数
const ll INF = 1e9;
const ll MOD = 1e9+7;
const vl dx = {1,0,-1,0,1,0,-1,0};
const vl dy = {0,1,0,-1,0,1,0,-1};
//マクロ
#define re(n) for(ll _ = 0; _ < (ll) n; _++)
#define rep(i,n) for(ll i = 0; i < (ll) n; i++)
#define rrep(i,n) for(ll i = (ll) n - 1; i >= 0;i--)
#define repp(i,x,n) for(ll i = (ll) x; i < (ll) n; i++)
#define rrepp(i,x,n) for(ll i = (ll) n - 1; i >= x; i--)
#define bitrep(i,n) for(ll i = 0; i < (1<<(ll)n); i++)
#define each(x,A) for(auto &(x): (A))
#define ALL(A) A.begin(), A.end()
#define SIZE(A) ll(A.size())
#define pb(a) push_back(a)
#define mp make_pair
#define PRECISE_COUT cout << setprecision(15) << fixed
//入力
void CINT(){}
template <class Head,class... Tail>
void CINT(Head&& head,Tail&&... tail){ cin>>head; CINT(move(tail)...);}
#define CIN(...) ll __VA_ARGS__;CINT(__VA_ARGS__)
#define SCIN(...) string __VA_ARGS__;CINT(__VA_ARGS__)
#define DCIN(...) double __VA_ARGS__;CINT(__VA_ARGS__)
#define VCIN(A) for(auto &a : (A)) cin >> a
//出力
#define COUT(a) cout << a << endl
#define TEST(i,a) cout << (ll) i << " : " << (ll) a << endl
#define vout(A) for(auto &a : (A)) cout << a << " ";cout << endl
//正誤判定
void Yes(bool ans){cout << (ans? "Yes" : "No") << endl;}
void YES(bool ans){cout << (ans? "YES" : "NO") << endl;}
//余り切り上げ
ll ceil(ll a,ll b) {return (a + b - 1) / b;}
//最大値・最小値
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;}
//最大公約数
ll gcd(ll a,ll b){if(a<b)swap(a,b); if(a%b==0)return b; return gcd(b,a%b);}
//最小公倍数
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
//n乗の余り
ll modpow(ll x,ll n,ll mod){ll res=1;for(ll i=0;i<n;i++){res=res*x%mod;}return res;}
ll MODpow(ll x,ll n){ll res=1;for(ll i=0;i<n;i++){res=res*x%MOD;}return res;}
//グリッドグラフの範囲外エラー
bool range(ll ny, ll nx, ll H, ll W){if(ny < 0 || ny >= H || nx < 0 || nx >= W) return false; return true;}
/*----------------------------------------------------------------------*/
//main関数
int main() {
CIN(N);
ll sum = 0;
re(N){
CIN(a,b);
sum += (b - a + 1) * (a + b) / 2;
}
COUT(sum);
}
|
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <deque>
#include <stack>
#include <queue>
#include <array>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <random>
#include <chrono>
#include <utility>
#include <numeric>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <sstream>
#include <assert.h>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <unordered_map>
using namespace std;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(bool x) {cerr << (x ? "true" : "false");}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
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...);}
#ifdef HOME
#warning CHECK int:ll::INT_MAX:LLONG_MAX
#define maxn 20
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define maxn 2000006
#define gcd __gcd
#define debug(x...)
#endif
#define ff first
#define endl '\n'
#define ss second
#define inf 0x3f3f3f3f
#define MOD 1000000007
#define PI 3.14159265358979323846264338327950L
#define f(i,x,n) for(int i=x;i<=n;i++)
#define fr(i,x,n) for(int i=x;i>=n;i--)
struct _ { ios_base::Init i; _() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); } } _;
int dx[] = { -1, 0, 1, 0, -1, -1, 1, 1};
int dy[] = {0, 1, 0, -1, -1, 1, 1, -1};
int main() {
long long sx, sy, gx, gy; cin >> sx >> sy >> gx >> gy;
cout << fixed << setprecision(15);
cout << (long double)(gx * sy + sx * gy) / (sy + gy);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
using Graph = vector<vector<int>>;
const int INT_INF = 2147483647;
//stoi(s) : string→int stoll(s) :string→longlong int→string to_string(i)
const double PI = acos(-1.0);
//小数点の表し方 cout << fixed << setprecision(5);
const ll LLMAX = 9223372036854775805;
int main(){
double sx,sy,gx,gy;
cin >> sx >> sy >> gx >> gy;
double ans = sx - (sx - gx)/(sy + gy) * sy;
cout << fixed << setprecision(10) << ans << endl;
} |
#include<bits/stdc++.h>
using namespace std;
using ll = long long int;
using ld = long double;
#define pow(n,m) powl(n,m)
#define sqrt(n) sqrtl(n)
const ll MAX = 5000000000000000000;
const ll MOD = 0;
random_device rd;
mt19937 mt(rd());
int main(){
ll N,M;
cin >> N >> M;
vector<string> S(M),ans(N),T;
map<string,ll> ss;
ll m = MAX;
for(ll i = 0;i < M;i++) cin >> S[i];
for(ll i = 0;i < M;i++) ss[S[i]] = i + 1;
for(ll i = 0;i < M;i++) m = min(m,(ll)S[i].size());
for(ll t = 0;t < 4000;t++){
string a = S[mt() % M];
for(ll i = 0;i < 10;i++){
string b = S[mt() % M];
for(ll j = min(a.size(),b.size());j >= 0;j--){
ll ok = 1;
for(ll k = 0;k < j;k++){
if(a[a.size() - j + k] != b[j]) ok = 0;
}
if(ok){
ll k = j;
while(a.size() < N && k < b.size()){
a += b[k];
k++;
}
break;
}
}
if(a.size() + b.size() <= N){
a += b;
}
}
while(a.size() < N) a += (char)('A' + (mt() % 8));
T.emplace_back(a);
}
for(ll i = 0;i < N;i++){
ll C = 0;
string A = "____________________";
for(ll j = 0;j < T.size();j++){
ll c = 0;
string a = T[j] + T[j];
for(ll k = 0;k < N;k++){
for(ll l = m;l < m + 5;l++){
if(ss[a.substr(k,l)] != 0) c++;
}
}
if(C < c){
C = c;
A = T[j];
}
}
string a = A + A;
for(ll k = 0;k < N;k++){
for(ll l = m;l < m + 5;l++){
if(ss[a.substr(k,l)] != 0) ss[a.substr(k,l)] = 0;
}
}
ans[i] = A;
}
for(ll i = 0;i < N;i++) cout << ans[i] << endl;
}
| #include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std;
int main(){
int N,Q;
cin >> N >> Q;
vector<unsigned long long> A(N);
vector<unsigned long long> low(N);
for (int i = 0; i < N; i++)
{
cin >> A[i];
low[i]=A[i]-i-1;
}
vector<unsigned long long> K(Q);
for (int i = 0; i < Q; i++)
{
cin >> K[i];
if (low[N-1]<K[i])
{
K[i]+=N;
}else{
int idx = lower_bound(low.begin(),low.end(),K[i])-low.begin();
K[i] +=idx;
}
cout << K[i] << endl;
}
return 0;
} |
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
vector<int> v;
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n; cin >> n; v.resize(n);
for (auto& x : v) { cin >> x; }
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
set<int> ans; for(auto &x : v)
for(int i = 1; i * i <= x; i++)
if (x % i == 0) {
if (i <= v[0]) ans.insert(i);
if (x / i <= v[0]) ans.insert(x / i);
}
int res = 0; for (auto& x : ans) {
int g = 0; for (auto& y : v)
if (y % x == 0) g = gcd(y, g);
res += g == x;
}
cout << res << '\n';
cin.ignore(2); return 0;
} | /**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author tatsumack
*/
#include <iostream>
#include <fstream>
#include <bits/stdc++.h>
#define int long long
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i)
#define REV(i, a, b) for (int i = (a); i >= (b); --i)
#define CLR(a, b) memset((a), (b), sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF 1001001001001001001ll
#define fcout cout << fixed << setprecision(12)
using namespace std;
// 約数列挙
vector<int> get(int n) {
vector<int> ret;
for (int i = 1; i * i <= n; ++i) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n) {
ret.push_back(n / i);
}
}
}
return ret;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
class FGCDOrMIN {
public:
void solve(std::istream& cin, std::ostream& cout) {
int N;
cin >> N;
vector<int> A(N);
REP(i, N) cin >> A[i];
int mina = INF;
map<int, int> m;
REP(i, N) {
for (auto n : get(A[i])) {
if (m.count(n) == 0) {
m[n] = A[i];
} else {
m[n] = gcd(m[n], A[i]);
}
}
mina = min(mina, A[i]);
}
int res = 0;
for (auto kv : m) {
if (kv.second <= mina && kv.first == kv.second) {
res++;
}
}
cout << res << endl;
}
};
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
std::istream& in(std::cin);
std::ostream& out(std::cout);
FGCDOrMIN solver;
solver.solve(in, out);
return 0;
}
|
//in dp prefix suffix sum helps..
#include<iostream>
#include<vector>
#include<string.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<stack>
#include <iterator>
#include <map>
#include<list>
#include <fstream>
#include<unordered_map>
#include<set>
#include<queue>
#define int long long
#define double long double
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define pip pair<int, pair<int, int> >
#define viv vector<vector<int> >
#define vip vector< pair<int, int > >
#define vi vector<int>
#define mii map<int, int>
#define li list<int>
#define qu queue<int>
#define vd vector<double>
#define set set<int>
#define stack stack<int>
#define fi first
#define se second
#define mem(x, y) memset(x, y, sizeof(x))
#define ps(x,y) fixed<<setprecision(y)<<x
using namespace std;
// author :: Anurag Anand.
using namespace std;
int z= 1e9+7;
//int z= 998244353;
int gcd(int a, int b){
if(a==0)return b;
if(b==0)return a;
return gcd(b, a%b);
}
int power(int a,int b)
{
int res=1;
while(b)
{
if(b&1)
{
res=(res*a)%z;
b--;
}
else
{
a=(a*a)%z;
b=b>>1;
}
}
return res;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
t=1;
//cin>>t;
while(t--){
int x;
cin>>x;
if(x>=0) cout<<x<<"\n";
else cout<<"0\n";
}
} | #include <bits/stdc++.h>
using namespace std;
int gcd(long long a, long long b) {
if (a % b == 0) return b;
else return gcd(b, a%b);
}
int lcm(long long a, long long b) {
return a * b / gcd(a, b);
}
int main() {
int N;
cin >> N;
long long x = 2;
for (int i = 3; i < N+1; i++) {
x = lcm(x, i);
}
cout << x + 1 << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
const int mxn=3e3+3;
int n,m;
int par[mxn];
inline int find(int x){return par[x]==x?x:par[x]=find(par[x]);}
inline void uni(int x,int y){par[find(x)]=find(y);}
set<int>s1,s2;
int main(){
ios_base::sync_with_stdio(false);
cin>>n>>m;
for(int i=1;i<=n+m;++i)par[i]=i;
uni(1,n),uni(1,n+1),uni(1,n+m);
for(int i=1;i<=n;++i)for(int j=1;j<=m;++j){
char c;cin>>c;
if(c=='#')uni(i,n+j);
}
for(int i=1;i<=n;++i)s1.insert(find(i));
for(int i=1;i<=m;++i)s2.insert(find(i+n));
cout<<min(s1.size(),s2.size())-1<<'\n';
return 0;
} | #include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int maxn = 2e5+5;
#define mod 1000000009
void solve(){
int n,m;
cin >> n>> m;
vector<int> a(m);
for(int i=0;i<m;i++){
cin >> a[i];
}
if( m==0){
cout << 1 << endl;
return;
}
sort(a.begin(),a.end());
vector<int> widths;
for(int i=0;i<m-1;i++){
if(a[i+1]-a[i]-1 > 0)
widths.push_back(a[i+1]-a[i]-1);
}
if(a[0] > 1)
widths.push_back(a[0]-1);
if( n-a[m-1] > 0)
widths.push_back(n-a[m-1]);
// for(auto i: widths) cout << i <<" ";
// cout << endl;
if( widths.empty() || m == 0) {
cout << 0 <<endl;
return;
}
int mn = *min_element(widths.begin(),widths.end());
int ans =0;
for(auto i : widths){
ans += (i+mn-1)/mn;
}
if(widths.empty()) cout << 0 << endl;
else cout << ans << endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tc = 1;
// cin >> tc;
while(tc--) solve();
return 0;
} |
#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#pragma GCC optimize(2)
#include<set>
#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<random>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<unordered_set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
int main()
{
IO;
int T=1;
//cin>>T;
while(T--)
{
double x,y,a,b;
cin>>x>>y>>a>>b;
double res=x+(y/(y+b))*(a-x);
printf("%.8lf\n",res);
}
return 0;
} | #include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
void read(int &x){
static char c;
while(!isdigit(c=getchar())); x=c^48;
while(isdigit(c=getchar()))x=(x*10)+(c^48);
}
void read(ll &x){
static char c;
while(!isdigit(c=getchar())); x=c^48;
while(isdigit(c=getchar()))x=(x*10)+(c^48);
}
void chkmax(int &x,int y){if(y>x)x=y;}
void chkmin(int &x,int y){if(y<x)x=y;}
void chkmax(ll &x,ll y){if(y>x)x=y;}
void chkmin(ll &x,ll y){if(y<x)x=y;}
int A,B;
void Work(){
read(A),read(B);
for(int i=B;i;--i){
int d=(A-1)/i+1;
if(1ll*i*(d+1)<=B) return printf("%d\n",i),void();
}
}
int main(){Work();}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define __ ios_base::sync_with_stdio(false);cin.tie(NULL);
#define bs 97
#define mx 4000007
int main() {
__
ll s , p ;
cin >> s >> p ;
ll i ;
ll ans = 0 ;
for (i = 1 ; i * i <= p ; i++) {
if (p%i == 0) {
ll x = p/i , y = i ;
if (x + y == s) return cout << "Yes\n", 0 ;
}
}
cout << "No\n" ;
return 0 ;
}
| #include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <utility>
#include <algorithm>
#include <cmath>
#include <climits>
#include <iomanip>
#include <queue>
#include <stack>
#include <ctype.h>
using namespace std;
typedef long long ll;
const int INF = (1<<30)-1;
const ll LINF = 1e18;
#define rep(i, n) for (int i = 0; i < n; i++)
template<class T>
bool chmax(T &a, T b) {if (a < b) {a = b;return true;}else return false;}
template<class T>
bool chmin(T &a, T b) {if (a > b) {a = b;return true;}else return false;}
ll mod_pow(ll a, ll n, ll p) {
ll ans = 1, tmp = a % p;
while (n > 0) {
if (n % 2)
ans = ans * tmp % p;
tmp = tmp * tmp % p;
n /= 2;
}
return ans;
}
int main() {
ll n, m; cin >> n >> m;
cout << mod_pow(10, n, m*m) / m << endl;
return 0;
}
//小数点精度
//cout << fixed << std::setprecision(15) << y << endl; |
#include <iostream>
using namespace std;
using ll = long long;
int main() {
ll K; cin >> K;
ll res = 0;
for (int a=1; a<=K; ++a) {
for (int b=1; b<=K/a; ++b) {
ll prod = a*b;
res += K/(a*b);
}
}
cout << res << endl;
}
| #include <bits/stdc++.h>
using namespace std;
template <typename T, typename U> bool cmin(T &a, U b) { return a > b && (a = b, true); }
template <typename T, typename U> bool cmax(T &a, U b) { return a < b && (a = b, true); }
signed main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
string S;
cin >> S;
int N = S.size();
int mx = INT_MIN;
for (int bit = 0; bit < 1 << N; bit++) {
int tmp = 0;
for (int i = 0; i < N; i++) {
if (bit >> i & 1) tmp += S.at(i) - '0';
}
if (tmp % 3 == 0) cmax(mx, __builtin_popcount(bit));
}
cout << ((mx == 0) ? -1 : N - mx) << "\n";
} |
#include<deque>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<tuple>
#include<string>
#include<chrono>
#include<functional>
#include<iterator>
#include<random>
#include<unordered_set>
#include<array>
#include<map>
#include<iomanip>
#include<assert.h>
#include<list>
#include<bitset>
#include<stack>
#include<memory>
#include<numeric>
using namespace std;
using namespace std::chrono;
typedef long long int llint;
typedef long double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
#define REP(i, n) for(int i = 0;i < (n);i++)
/*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/
const llint mod=1000000007;
const llint inf=2.19e15+1;
const long double pai=3.141592653589793238462643383279502884197;
const long double eps=1e-10;
template <class T,class U>bool chmin(T& a,U b){if(a>b){a=b;return true;}return false;}
template <class T,class U>bool chmax(T& a,U b){if(a<b){a=b;return true;}return false;}
llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);}
llint lcm(llint a,llint b){if(a==0){return b;}return a/gcd(a,b)*b;}
template<class T> void SO(T& ve){sort(ve.begin(),ve.end());}
template<class T> void REV(T& ve){reverse(ve.begin(),ve.end());}
template<class T>llint LBI(const vector<T>&ar,T in){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();}
template<class T>llint UBI(const vector<T>&ar,T in){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();}
int main(void){
cout<<fixed<<setprecision(20);
cin.tie(0);ios::sync_with_stdio(false);
llint i,n;cin>>n;
//x円保険すると、 失うのはmax(x,A-x)
//失うのの中央値/2保険する
vector<double>A(n);
for(i=0;i<n;i++){cin>>A[i];}
SO(A);
double x=A[n/2]/2;
double ans=0;
for(i=0;i<n;i++){ans+=max(x,A[i]-x);}
cout<<ans/n<<endl;
return 0;
}
| #include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
#define f(x, m) for(auto x : m)
#define cpu() ios::sync_with_stdio(false); cin.tie(nullptr)
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pair<int ,int>>
#define vll vector<pair<ll ,ll>>
#define all(v) v.begin(),v.end()
#define sor(a) sort( a.begin(), a.end() )
#define ros(a) sort( a.rbegin(), a.rend())
#define prec(n) fixed << setprecision(n)
#define ff first
#define ss second
#define print(x) for(auto it : x) cout << it << " ";
#define debug(x) cerr << #x << " is " << x << endl;
typedef long long ll;
using namespace std;
// using namespace __gnu_pbds;
#define dbg(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> ) {cout << "NEXT\n"; }
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << ", ";
err(++it, args...);
}
template<typename... T>
void rd(T& ... args){
(( cin >> args), ...);
}
template<typename... T>
void ps(T ... args){
((cout << args << ' '), ...);
cout << '\n';
}
// mp.max_load_factor(0.25);
// using ordered_set = tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update>;
const int MOD = 998244353;
const char nl = '\n';
const int INF = 1e9 + 5;
int n;
void add(int& x, int y){
x += y;
if(x >= MOD){
x -= MOD;
}
}
void res(int& x, int y){
x -= y;
if(x < 0){
x += MOD;
}
}
ll pot(ll x, ll y){
if(y == 0) return 1;
if(y & 1) return (pot(x, y - 1) * x) % MOD;
ll k = pot(x, y / 2);
return (k * k) % MOD;
}
ll inv(ll x){
return pot(x, MOD - 2);
}
void solve(){
cin >> n;
vi a(n);
for(int& it : a){
cin >> it;
}
sor(a);
int cur = 0, ans = 0;
ll id = 1;
for(int i = 0; i < n; i++){
cur = int((id * a[i] + cur) % MOD);
id = (id * 2) % MOD;
add(ans, int((ll(a[i]) * a[i]) % MOD));
}
// dbg(cur);
for(int i = 0; i < n; i++){
res(cur, a[i]);
cur = int((499122177LL * cur) % MOD);
add(ans, int((ll(a[i]) * cur) % MOD));
}
cout << ans << "\n";
}
int main(){
cpu();
solve();
return 0;
} |
#include<bits/stdc++.h>
#include <iterator>
#include <iostream>
#include <numeric>
#include <math.h>
#define ll long long
#define ull long
#define mpa make_pair
#define pb push_back
#define ff first
#define pii pair<ll,ll>
#define dd double
#define trace(x) cerr << #x << " : " << x << endl
#define ss second
#define boost ios_base::sync_with_stdio(0)
#define forp(i,a,b) for(ll i=a;i<=b;i++)
#define rep(i,n) for(ll i=0;i<n;++i)
#define ren(i,n) for(ll i=n-1;i>=0;i--)
#define forn(i,a,b) for(ll i=a;i>=b;i--)
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end();
#define sc(x) scanf("%lld",&x)
#define clr(x,val) memset(x,val,sizeof(x))
#define pr(x) printf("%lld\n",x)
#define gc getchar
#define pdd pair<dd,dd>
#define read_arr(a,n) for(ll i=1;i<=n;i++)cin>>a[i];
#define init_arr(a,n) for(ll i=1;i<=n;i++)a[i]=n-i+1;
#define prec(x) cout<<fixed<<setprecision(x)
#define fre freopen("input.txt","r",stdin),freopen("output.txt","w",stdout)
#define arr array
using namespace std;
int main(){
ll n;
cin>>n;
cout<<max(0ll,n)<<endl;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using pll = pair<ll, ll>;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define rrep(i, m, n) for(ll i = m; i > n; i--)
#define Rep(i, m, n) for(ll i = m; i < n; i++)
#define all(vec) vec.begin(), vec.end()
#define lmax(x, y) max<ll>(x, y)
#define lmin(x, y) min<ll>(x, y)
#define tmax(x, y, z) lmax((x), lmax((y), (z)))
#define tmin(x, y, z) lmin((x), lmin((y), (z)))
#define MINUS(a) memset(a, 0xff, sizeof(a))
#define ZERO(a) memset(a, 0, sizeof(a))
const ll INF = 1LL << 60;
const ll MM = 1000000000; const ll MOD = MM + 7; const ll MMM=9223372036854775807;//2^63 -1
#define ADD(a,b) a = (a + ll(b)) % MOD
#define MUL(a,b) a = (a * ll(b)) % MOD
ll GCD(ll x, ll y){ if(y == 0) return x; else return GCD(y, x % y);}
ll LCM(ll x, ll y){ return x / GCD(x, y) * y;}
template<class T> inline bool chmin(T& a, T b){ if(a > b){ a = b; return true;} return false;}
template<class T> inline bool chmax(T& a, T b){ if(a < b){ a = b; return true;} return false;}
template<typename A, size_t N, typename T> void FILL(A (&array)[N], const T &val){ fill( (T*)array, (T*)(array+N), val );}
// struct edge{ ll from, to, cost; };
// struct edge{ ll to, cost; };
// using Graph = vector<vector<ll>>; // 重み無し
// using Graph = vector<edge>; // 辺
// using Graph = vector<vector<edge>>; // 重み付き
ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
ll ddx[8] = {1, 1, 0, -1, -1, -1, 0, 1}, ddy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
ll table[1010];
map<ll, ll> prime_factor(ll n){
map<ll, ll> res;
for(ll i=2; i*i<=n; i++){
while(n%i == 0){
++res[i];
n /= i;
}
}
if(n != 1) res[n] = 1;
return res;
}
int main(){
FILL(table, 0);
ll n; cin >> n;
rep(i, n){
ll a; cin >> a;
auto mp = prime_factor(a);
for(auto p: mp){
table[p.first] += 1;
}
}
ll res, ans = -1;
rep(i, 1002){
if(chmax(ans, table[i])) res = i;
}
cout << res << endl;
}
|
#include<bits/stdc++.h>
using namespace std;
using P=pair<int,int>;
using ll=long long;
template<class T> using heapq = priority_queue<T,vector<T>,greater<T>>;
template<class T> bool chmax(T &a,const T b) {if (a<b) {a=b;return true;} else return false;}
template<class T> bool chmin(T &a,const T b) {if (a>b) {a=b;return true;} else return false;}
#define rep(i,n) for(int i=0; i<(n); i++)
#define srep(i,a,b) for(int i=(a); i<(b); i++)
#define rrep(i,n) for(int i=(n)-1;i>=0; i--)
#define srrep(i,a,b) for(int i=(b)-1; i>=(a); i--)
int main(){
string s; cin >> s;
for(char &c:s){
if (c=='6') c='9';
else if (c=='9') c='6';
}
reverse(s.begin(),s.end());
cout << s << endl;
} | #include <iostream>
#include <cstring>
using namespace std;
int main()
{
string str,s;
cin>>str;
for(int i=str.size()-1;i>=0;i--)
{
if(str[i]=='0') cout<<"0";
if(str[i]=='6') cout<<"9";
if(str[i]=='8') cout<<"8";
if(str[i]=='9') cout<<"6";
if(str[i]=='1') cout<<"1";
}
return 0;
} |
#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a,i##end=b;i<=i##end;i++)
#define drep(i,a,b) for(int i=a,i##end=b;i>=i##end;i--)
inline int read() {
int 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;
typedef long long ll;
const int M=1000;
ll C[M][M];
void solve() {
int A=read(),B=read();long long k;scanf("%lld",&k);
rep(i,0,M-1){
C[i][0]=1;
rep(j,1,i)C[i][j]=C[i-1][j]+C[i-1][j-1];
}
int a=A,b=B;
rep(i,1,A+B){
if(a==0){
putchar('b');
continue;
}
if(b==0){
putchar('a');
continue;
}
ll cnt=C[a+b-1][a-1];
if(cnt>=k)putchar('a'),a--;
else k-=cnt,putchar('b'),b--;
}
}
int main() {
int T=1;
while(T--)solve();
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (decltype(+n) i = 0; i < (n); i++)
using namespace std;
int main() {
int N, M;
cin >> N >> M;
vector<int> a(M), b(M), c(N);
rep(i, M) {
cin >> a[i] >> b[i];
a[i]--, b[i]--;
}
for (int &x: c) cin >> x;
vector<vector<int>> e(N);
rep(i, M) {
if (c[a[i]] >= c[b[i]]) e[a[i]].emplace_back(i);
if (c[a[i]] <= c[b[i]]) e[b[i]].emplace_back(i);
}
vector<string> ans(M);
function<void(int)> dfs = [&](int x) {
for (int i: e[x]) if (ans[i] == "") {
bool d = a[i] == x;
ans[i] = d ? "->" : "<-";
dfs(d ? b[i] : a[i]);
}
};
rep(i, N) dfs(i);
for (string x: ans) cout << x << endl;
}
|
/**
* author: otera
**/
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60;
#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++)
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
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; }
void solve() {
int n, m; cin >> n >> m;
vector<int> c(n);
vector<vector<int>> g(n, vector<int>());
vector<int> a(m), b(m);
vector<int> ans(m, -1);
map<P, P> mp;
rep(i, m) {
cin >> a[i] >> b[i]; -- a[i], -- b[i];
g[a[i]].push_back(b[i]);
g[b[i]].push_back(a[i]);
mp[{a[i], b[i]}] = {i, 1}, mp[{b[i], a[i]}] = {i, 0};
}
rep(i, n) {
cin >> c[i];
}
vector<bool> used(m, 0);
rep(i, m) {
if(c[a[i]] < c[b[i]]) {
// <-
ans[i] = 0;
used[i] = 1;
} else if(c[a[i]] > c[b[i]]) {
// ->
ans[i] = 1;
used[i] = 1;
}
}
auto rec = [&](auto && self, int p, int v) -> void {
for(int nv: g[v]) {
if(nv == p) continue;
if(c[v] == c[nv]) {
// v -> nv
if(mp.count({v, nv})) {
P p = mp[{v, nv}];
int i = p.fr, val = p.sc;
if(used[i]) continue;
used[i] = 1;
ans[i] = val;
self(self, v, nv);
}
}
}
};
rep(i, m) {
if(!used[i]) {
assert(c[a[i]] == c[b[i]]);
used[i] = 1;
ans[i] = 1;
rec(rec, a[i], b[i]);
}
}
rep(i, m) {
assert(ans[i] != -1);
if(ans[i] == 1) cout << "->" << "\n";
else cout << "<-" << "\n";
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
//cout << fixed << setprecision(20);
//int t; cin >> t; rep(i, t)solve();
solve();
return 0;
} | //#pragma GCC optimize("Ofast", "unroll-loops")
//#pragma GCC target("sse", "sse2", "sse3", "ssse3", "sse4", "avx")
#ifdef __APPLE__
# include <iostream>
# include <cmath>
# include <algorithm>
# include <stdio.h>
# include <cstdint>
# include <cstring>
# include <string>
# include <cstdlib>
# include <vector>
# include <bitset>
# include <map>
# include <queue>
# include <ctime>
# include <stack>
# include <set>
# include <list>
# include <random>
# include <deque>
# include <functional>
# include <iomanip>
# include <sstream>
# include <fstream>
# include <complex>
# include <numeric>
# include <immintrin.h>
# include <cassert>
# include <array>
# include <tuple>
# include <unordered_map>
# include <unordered_set>
# include <thread>
#else
# include <bits/stdc++.h>
#endif
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define all(a) a.begin(),a.end()
#define len(a) (int)(a.size())
#define mp make_pair
#define pb push_back
#define fir first
#define sec second
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
const int max_n = 100+10, inf = 1000111222;
vector<int> reb[max_n];
int c[max_n];
bool use[max_n];
bool ans[max_n][max_n];
void dfs(int now)
{
use[now]=1;
for (auto wh:reb[now]){
if (c[now]==c[wh]){
if (!ans[now][wh] && !ans[wh][now]){
ans[now][wh]=1;
}
if (!use[wh]){
dfs(wh);
}
}
}
}
int main() {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int n,m;
cin>>n>>m;
vector<pii> edges;
for (int i=1;i<=m;i++){
int a,b;
cin>>a>>b;
reb[a].pb(b);
reb[b].pb(a);
edges.pb(mp(a,b));
}
for (int i=1;i<=n;i++){
cin>>c[i];
}
for (auto& i:edges){
if (c[i.fir]>c[i.sec]){
ans[i.fir][i.sec]=1;
}
swap(i.fir,i.sec);
if (c[i.fir]>c[i.sec]){
ans[i.fir][i.sec]=1;
}
swap(i.fir,i.sec);
}
for (int i=1;i<=n;i++){
if (!use[i]){
dfs(i);
}
}
for (auto i:edges){
if (ans[i.fir][i.sec]){
cout<<"->"<<"\n";
}
else{
cout<<"<-"<<"\n";
}
}
}
|
#include<bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define deb(...)
#endif
#define int long long int
void solve() {
int n;
cin >> n;
vector<int>v(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
}
vector<int>pref(n);
int sum = 0;
for (int i = 0; i < n; i++) {
sum += v[i];
pref[i] = sum;
}
vector<int>pref1(n);
sum = 0;
for (int i = 0; i < n; i++) {
if (i == 0) {
pref1[i] = sum;
sum += v[i];
continue;
}
pref1[i] = pref1[i - 1] + sum;
sum += v[i];
}
vector<int>ans(n);
int maxi = 0;
for (int i = 0; i < n; i++) {
maxi = max(maxi, v[i]);
ans[i] = pref[i] + pref1[i] + (i + 1) * maxi;
}
for (auto it : ans) {
cout << it << endl;
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t = 1;
//cin >> t;
while (t--) solve();
return 0;
} | #pragma GCC optimize ("trapv")
#include <bits/stdc++.h>
#include<algorithm>
#include <vector>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<string.h>
using namespace std;
#define pb push_back
#define all(v) v. begin(),v. end()
#define rep(i,n,v) for(i=n;i<v;i++)
#define per(i,n,v) for(i=n;i>v;i--)
#define ff first
#define ss second
#define pp pair<ll,ll>
#define ll long long
#define endl '\n'
vector<vector<ll>>v;
void dfs(vector<vector<ll>>&a, ll i,ll j, ll n)
{
if(v[i][j]==0 && a[i][j]==1)
{
v[i][j]=3;
if(i+1<n) dfs(a, i+1,j,n);
if(i+1<n && j+1<n) dfs(a, i+1,j+1,n);
if(i+1<n && j-1>=0) dfs(a, i+1,j-1,n);
}
}
void solve()
{
ll n,m=INT_MIN,b=0, c=0,k=0, i, j, l=1e9+7;
string s, r, y;
ll hit=0;
cin>>n;
ll ar[n];
vector<ll>vec;
rep(i, 0,n) cin>>ar[i];
vec.pb(0);
rep(i, 0,n)
{
vec.pb(ar[i]+vec[i]);
}
//rep(i, 0,vec.size()) cout<<vec[i]<<" ";
rep(i,0,n)
{
m=max(m,ar[i]);
k+=vec[i+1];
cout<<m*(i+1)+k<<endl;
}
}
int main()
{
ll i, j, b=0, c=1,yog=0,hit=1;
ios_base::sync_with_stdio(false);
cin. tie(0);cout. tie(0);
ll t=1;
//cin>>t;
while(t--)
{
solve();
}
return 0;
} |
#include <bits/stdc++.h>
#define lowbit(x) ((x)&(-(x)))
#define mkp make_pair
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
int main()
{
int n,s,d;
scanf("%d%d%d",&n,&s,&d);
int ans=0;
for(int i=1;i<=n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
if(x<s && y>d) ans|=1;
}
puts(ans==1 ? "Yes" : "No");
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, start, n) for (ll i = (ll)(start); i < (ll)(n); ++i)
static const ll INFTY = 1L << 62L;
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
ll n, s, d;
cin >> n >> s >> d;
ll x[n], y[n];
rep(i, 0, n)
{
cin >> x[i] >> y[i];
}
rep(i, 0, n)
{
if (x[i] < s && y[i] > d)
{
cout << "Yes\n";
return 0;
}
}
cout << "No\n";
} |
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <functional>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <bitset>
#include <math.h>
#include <random>
#include <chrono>
using namespace std ;
using ll = long long ;
using ld = long double ;
template<class T> using V = vector<T> ;
template<class T> using VV = V<V<T>> ;
using pll = pair<ll,ll> ;
#define all(v) v.begin(),v.end()
ll mod = 1000000007 ;
long double pie = acos(-1) ;
ll INF = 1000000000000 ;
void yorn(bool a){if(a) cout << "Yes" << endl ; else cout << "No" << endl ;}
//void YorN(bool a){if(a) cout << "YES" << endl ; else cout << "NO" << endl ;}
ll gcd(long long a,long long b){if(b==0) return a ; return gcd(b,a%b) ;}
ll lcm(long long a,long long b){return a/gcd(a,b)*b ;}
void fix_cout(){cout << fixed << setprecision(20) ;}
template<class T> void chmax(T &a,T &b){if(a<b) a = b ;}
template<class T> void chmin(T &a,T &b){if(a>b) a = b ;}
int main(){
int n ;
string s ;
cin >> n >> s ;
s = "###"+s+"###" ;
string t = "fox" ;
string cur = "###" ;
if(n<3){
cout << n << endl ;
return 0 ;
}
set<int> st ;
for(int i=0;i<s.size();i++) st.insert(i) ;
int ans = 0 ;
int p1=-1,p2=-1,p3=-1 ;
p1 = 3 ;
p2 = 4 ;
p3 = 5 ;
while(p3<n+3){
cur[0] = s[p1] ;
cur[1] = s[p2] ;
cur[2] = s[p3] ;
if(cur==t){
ans++ ;
st.erase(p1) ;
st.erase(p2) ;
st.erase(p3) ;
auto itr = st.lower_bound(p1) ;
itr-- ;
p3 = *itr ;
itr-- ;
p2 = *itr ;
itr-- ;
p1 = *itr ;
}else{
auto itr = st.upper_bound(p3) ;
p3 = *itr ;
itr-- ;
p2 = *itr ;
itr-- ;
p1 = *itr ;
}
}
cout << n-ans*3 << endl ;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
int t; cin >> t;
rep(i,t){
int n; cin >> n;
map<int,int> mp;
rep(i,n){
int a; cin >> a;
mp[a] ^= 1;
}
bool even = true;
for(auto [a,v]: mp) if(v) even = false;
puts(n&1 || even ? "Second" : "First");
}
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T> using vc = vector<T>;
template<class T> using vvc = vc<vc<T>>;
template<class T> using vvvc = vc<vvc<T>>;
template<class T> using vvvvc = vvc<vvc<T>>;
template<class T> using PQ = priority_queue<T>;
template<class T> using invPQ = priority_queue<T, vector<T>, greater<T>>;
using IP = pair<int, int>;
using LP = pair<ll, ll>;
#define all(x) begin(x), end(x)
#define rep(i, n) for (int i = 0; (i) < (int)(n); i++)
#define rep3(i, m, n) for (int i = (m); (i) < (int)(n); i++)
#define repr(i, n) for (int i = (n) - 1; (i) >= 0; i--)
#define rep3r(i, m, n) for (int i = (n) - 1; (i) >= (int)(m); i--)
template<class T> inline bool chmax(T & a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T & a, T b) { if (a > b) { a = b; return 1; } return 0; }
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
ll K, N, M;
cin >> K >> N >> M;
vc<ll> A(K);
rep (i, K) cin >> A[i];
vc<tuple<long double, ll, ll>> cond;
int cnt = 0;
rep (i, K) {
int b = M*A[i]/N;
cnt += b;
cond.push_back({ (long double)b/M - (long double)A[i]/N, i, M*A[i]/N });
}
sort(cond.begin(), cond.end());
rep (i, K) {
if (cnt >= M) break;
get<2>(cond[i])++;
get<0>(cond[i]) = (long double)get<2>(cond[i])/M;
cnt++;
}
vc<int> B(K);
rep (i, K) {
B[get<1>(cond[i])] = get<2>(cond[i]);
}
rep (i, K) {
if (i != 0) cout << " ";
cout << B[i];
}
cout << endl;
} | #include <bits/stdc++.h>
// #define x first
// #define y second
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 1e5 + 20;
int n, m, k;
LL a[N], b[N], c[N], mi[N], mx[N];
bool check(LL mid)
{
for(int i = 1; i <= k; ++ i)
{
mx[i] = min((a[i] + mid) / n, (LL)m) + mx[i - 1];
mi[i] = max(0ll, (a[i] - mid + n - 1) / n) + mi[i - 1];
}
return mi[k] <= m && mx[k] >= m;
}
int main()
{
IOS; cin >> k >> n >> m;
for(int i = 1; i <= k; ++ i) cin >> a[i], a[i] *= m;
LL l = 0, r = 1e18;
while(l <= r)
{
LL mid = l + r >> 1;
if(check(mid))
{
memcpy(b, mx, sizeof b), memcpy(c, mi, sizeof c);
r = mid - 1;
}
else l = mid + 1;
}
for(int i = k; i >= 1; -- i)
{
a[i] = min(m - c[i - 1], b[i] - b[i - 1]);
m -= a[i];
}
for(int i = 1; i <= k; ++ i) cout << a[i] << " ";
return 0;
} |
//
// _oo0oo_
// o8888888o
// 88" . "88
// (| -_- |)
// 0\ = /0
// ___/`---'\___
// .' \\| |// '.
// / \\||| : |||// \
// / _||||| -:- |||||- \
// | | \\\ - /// | |
// | \_| ''\---/'' |_/ |
// \ .-\__ '-' ___/-. /
// ___'. .' /--.--\ `. .'___
// ."" '< `.___\_<|>_/___.' >' "".
// | | : `- \`.;`\ _ /`;.`/ - ` : | |
// \ \ `_. \_ __\ /__ _/ .-` / /
// =====`-.____`.___ \_____/___.-`___.-'=====
// `=---='
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#define ll long long
#define gmax(x,y) x=max(x,y)
#define gmin(x,y) x=min(x,y)
#define F first
#define S second
#define P pair
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,a,b) for(int i=a;i<b;i++)
#define V vector
#define RE return
#define ALL(a) a.begin(),a.end()
#define MP make_pair
#define PB emplace_back
#define PF emplace_front
#define FILL(a,b) memset(a,b,sizeof(a))
#define lwb lower_bound
#define upb upper_bound
#define lc (x<<1)
#define rc ((x<<1)|1)
using namespace std;
V<int> v[200005];
V<P<int,int> > g[200005];
int d[200005],ma[200005],it[200005],pl[200005];
void dfs(int x,int y){
for(auto u:v[x])if(u!=y)g[x].PB(MP(pl[u],u));
sort(ALL(g[x]));
ma[x]=d[x]+it[x];
for(auto u:g[x])if(u.S!=y){
it[u.S]=ma[x]+1-d[x];
d[u.S]=d[x]+1;
dfs(u.S,x);
gmax(ma[x],ma[u.S]);
}
}
int siz[200005],in[200005];
int n;
void get(int x,int y){
siz[x]=1;pl[x]=0;
for(auto u:v[x])if(u!=y){
d[u]=d[x]+1;
get(u,x);
gmax(pl[x],pl[u]);
siz[x]+=siz[u];
gmax(in[x],siz[u]);
}
pl[x]++;
gmax(in[x],n-siz[x]);
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin>>n;
FOR(i,2,n){
int x,y;
cin>>x>>y;
v[x].PB(y);
v[y].PB(x);
}
get(1,-1);
int st=1;
FOR(i,2,n){
if(d[i]>d[st])st=i;
}
get(st,-1);
it[st]=1;
dfs(st,-1);
FOR(i,1,n)cout<<it[i]<<' ';
RE 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,m,cnt;
struct Obstacle {
int x,y;
bool operator<(const Obstacle rhs) const {
if(x==rhs.x) {
return y<rhs.y;
}
return x<rhs.x;
}
}a[200005];
int lenn[200005];//Maximum x you can go with y=i
int lenm[200005];//Maximum y you can go with x=i
int tr[200005];
bool vis[200005];
ll ans;
int lowbit(int x) {
return x&-x;
}
void add(int pos,int num) {
for(int i=pos;i<=m;i+=lowbit(i)) {
tr[i]+=num;
}
}
int sum(int pos) {
int res=0;
for(int i=pos;i>=1;i-=lowbit(i)) {
res+=tr[i];
}
return res;
}
vector<int> ob[200005];
int main() {
scanf("%d %d %d",&n,&m,&cnt);
for(int i=1;i<=n;i++) {
lenm[i]=m;
}
for(int i=1;i<=m;i++) {
lenn[i]=n;
}
for(int i=1;i<=cnt;i++) {
scanf("%d %d",&a[i].x,&a[i].y);
ob[a[i].x].push_back(a[i].y);
lenm[a[i].x]=min(lenm[a[i].x],a[i].y-1);
lenn[a[i].y]=min(lenn[a[i].y],a[i].x-1);
}
for(int i=1;i<=lenm[1];i++) {
ans+=lenn[i];
}
for(int i=lenm[1]+1;i<=m;i++) {
vis[i]=1;
add(i,1);
}
for(int i=2;i<=lenn[1];i++) {
ans+=sum(lenm[i]);
for(int j=0;j<ob[i].size();j++) {
if(!vis[ob[i][j]]) {
vis[ob[i][j]]=1;
add(ob[i][j],1);
}
}
}
printf("%lld",ans);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pint;
typedef pair<ll,ll> pll;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<string> vstr;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
#define vint2(v,n,m,init) vector<vector<int>> v(n, vector<int>(m, init))
#define vll2(v,n,m,init) vector<vector<ll>> v(n, vector<ll>(m, init))
#define rep(i,n) for(ll i=(ll)0; i<(ll)n; i++)
#define REP(i,m,n) for(ll i=(ll)m; i<(ll)n; i++)
#define arr(var, n) vint var(n); rep(i,n){cin >> var[i];}
#define arrll(var, n) vll var(n); rep(i,n){cin >> var[i];}
#define arrst(var, n) vstr var(n); rep(i,n){cin >> var[i];}
#define ALL(var) (var).begin(), (var).end()
#define sortall(var) sort(ALL(var))
#define uniqueall(v) v.erase(unique(v.begin(), v.end()), v.end());
#define prt(var) cout << (var) << "\n"
#define prt2(v1, v2) cout << (v1) << " " << (v2) << "\n"
#define prt3(v1, v2, v3) cout << (v1) << " " << (v2) << " " << (v3) << "\n"
#define prtd(n, var) cout << fixed << setprecision(n) << (var) << "\n"
#define prtfill(n, var) cout << setw(n) << setfill('0') << (var);
#define prtall(v) rep(i,v.size()){cout<<v[i]<<(i!=v.size()-1?" ":"\n");}
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;}
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;}
//----------------------------------------------------------------------------
int main(void) {
int n, a, b;
cin >> n >> a >> b;
prt(n-a+b);
}
| #include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
int main() {
// cinを高速にするためのおまじない
cin.tie(0);
ios::sync_with_stdio(false);
/////////////////////
// Write code below
long long int N,A,B,ans;
cin >> N >> A >> B;
ans = N - A + B;
cout << ans << endl;
/////////////////////
return 0;
} |
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using namespace chrono;
#define ll long long
#define ld long double
#define mod 1000000007
#define pb push_back
#define inf 1000000000000000000
#define ff first
#define ss second
#define deb(x) cout<<#x<<" "<<x<<"\n"
#define Clear(x) memset(x,0,sizeof(x))
#define all(x) (x).begin(),(x).end()
void checkpoint1()
{
/******think more code less******/
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
freopen("Error.txt", "w", stderr);
#endif
}
bool comp(pair< ll,ll > &a,pair< ll,ll > &b)
{
if(a.ff!=b.ff)
return a.ff<b.ff;
else
{
if(a.ss>b.ss)
return b.ss<a.ss;
else if(a.ss<b.ss)
return a.ss<b.ss;
}
return a.ff>b.ff;
}
ll powe(ll a,ll b)
{
ll res=1;
while(b>0)
{
if(b&1)
{
res*=a;
res%=mod;
}
a*=a;
a%=mod;
b>>=1;
}
return res;
}
void upd(ll st[],ll v,ll stl,ll str,ll pos,ll val)
{
if(stl==str)
st[v]=1;
else
{
ll stm=(stl+str)/2;
if(pos<=stm)
upd(st,2*v,stl,stm,pos,val);
else
upd(st,2*v+1,stm+1,str,pos,val);
st[v]=st[2*v]+st[2*v+1];
}
}
ll ans(ll st[],ll v,ll stl,ll str,ll l,ll r)
{
if(l>r)
return 0LL;
if(stl==l && r==str)
return st[v];
else
{
ll stm=(stl+str)/2;
return ans(st,2*v,stl,stm,l,min(r,stm))+ans(st,2*v+1,stm+1,str,max(stm+1,l),r);
}
}
void terminAtor()
{
ll n;
cin>>n;
ll a[n+1];
for(ll i=1;i<=n;i++)
cin>>a[i];
ll st[4*(n+1)];
for(ll i=0;i<4*n+1;i++)
st[i]=0;
ll tot=0,temp=0;
vector< ll > pos;
for(ll i=1;i<=n;i++)
{
tot+=(ans(st,1,1,n,a[i]+1,n));
if(temp!=tot)
{
temp=tot;
pos.pb(i);
}
upd(st,1,1,n,a[i],1);
}
if(tot!=n-1){
cout<<-1;
return;
}
ll k=0;
for(ll i=0;i<pos.size();i++)
{
ll tt=pos[i]-1;
pos[i]--;
while(pos[i]>k)
{
cout<<pos[i]<<"\n";
pos[i]--;
}
k=tt;
}
}
int main()
{
checkpoint1();
/********************************************************/
auto startrrr = high_resolution_clock::now();
cout << setprecision(20);
/*******************************************************/
terminAtor();
/*******************************************************/
auto stoprrr = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stoprrr - startrrr);
#ifndef ONLINE_JUDGE
cerr << "Time: " << duration.count()/1000.0<<"\n";
#endif
/*******************************************************/
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define ll long long
int a[2000005], b[2000005],c[2000005];
int main()
{
int n;
cin >> n;
for(int i=1;i<=n;i++) {
cin >> a[i];
b[a[i]]=i;
c[i] = a[i];
}
unordered_map<int,int> mp;
vector<int>v;
for(int i=1;i<=n;i++)
{
for(int j = b[i]-1;j>=i;j--)
{
if(mp[j]==0){
b[a[j]]++;
a[j+1]= a[j];
v.push_back(j);
mp[j]=1;
}
else
{
cout << -1 << endl;
return 0;
}
}
b[i]=i;
a[i]=i;
}
if(mp.size()==n-1)
{
for(auto it: v) cout << it << " ";
cout << endl;
}
else cout << -1 << endl;
}
|
#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=1e9+7;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}
ll dp[202][15];
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int L;
cin>>L;
dp[0][0]=1;
for(int i=0;i<L;i++){
for(int j=0;j<=11;j++){
dp[i+1][j]+=dp[i][j];
if(j+1<=11){
if(j+1==11&&i+1==L) continue;
dp[i+1][j+1]+=dp[i][j];
}
}
}
cout<<dp[L][11]<<endl;
return 0;
}
| #include <bits/stdc++.h>
#define endl "\n"
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
constexpr int N = 1e5 + 5;
constexpr int mod = 1e9 + 7;
constexpr int LOG = 18;
template<typename T> void dout(const T& x) { cout << x << ' '; } // change separator!
template<typename T, typename ...Ts> void dout(const T& v, const Ts&... args) { dout(v); dout(args...); }
template<typename ...Ts> void vout(const Ts&... args) { dout(args...); cout << endl; exit(0); }
bitset<N> dp;
int n, v, s = 0;
void solve() {
cin >> n;
dp[0] = 1;
while(n--) {
cin >> v;
s += v;
dp |= (dp << v);
}
int ans = N;
for (int i = 0; i <= s; i++)
if (dp[i])
ans = min(ans, max(i, s - i));
cout << ans << endl;
}
int main() {
//freopen("zhopa.txt", "r", stdin);
//freopen("logins.txt", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
// int tests;
// cin >> tests;
// while(tests--)
// auto start = chrono::high_resolution_clock::now();
solve();
// auto end = chrono::high_resolution_clock::now();
// cout << endl << (chrono::duration_cast<chrono::duration<double>>(end - start)).count();
return 0;
}
|
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
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 = 1000000007;
const char nl = '\n';
const int MX = 150001; //check the limits, dummy
set<pii> visited;
void solve(int t) {
int n, m;
cin>>n >> m;
map<int, map<int, char>> adj;
F0R(i,m) {
int u,v;
char c;
cin>>u>>v>>c;
u--, v--;
adj[u][v]=c;
adj[v][u]=c;
}
// negative length of string
priority_queue<pair<ll, pii>> pq;
pq.push(mp(0,mp(0,n-1)));
int u,v;
set<int> pos;
while(!pq.empty()) {
auto cur = pq.top();
pq.pop();
ll l = -cur.f;
tie(u,v) = cur.s;
// cout << l <<" "<<st<<" "<<u<<" "<<v<<nl;
if(visited.find({u,v})!=visited.end()) {
// cout << "hi\n";
continue;
}
visited.ins({u,v});
if(u==v) {
pos.ins(2*l);
// cout << 2*l<<nl;
// return;
}
if(adj[u].find(v)!=adj[u].end()) {
pos.ins(2*l+1);
// cout << 2*l+1<<nl;
// return;
}
for(auto au : adj[u]) {
for(auto av : adj[v]) {
if(au.s==av.s) {
pq.push(mp(-l-1,mp(au.f,av.f)));
}
}
}
}
if(!pos.empty()) cout << *(pos.begin())<<nl;
else
cout << "-1\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout.precision(20);
cout << fixed;
int T = 1;
F0R(i,T) {
solve(i+1);
}
}
// read the question correctly (ll vs int)
// template by bqi343 | #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++(i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++(i))
#define REP_R(i, n) for (int i = (int)(n)-1; (i) >= 0; --(i))
#define REP3R(i, m, n) for (int i = (int)(n)-1; (i) >= (int)(m); --(i))
#define ALL(x) ::std::begin(x), ::std::end(x)
using namespace std;
template <typename T>
void print_vector(T& vec) {
stringstream ss;
ss << "{";
for (auto v : vec) ss << v << " ";
ss << "}" << endl;
cout << ss.str() << endl;
}
template <typename T>
void print_pairs(T& vec) {
stringstream ss;
ss << "{";
for (auto v : vec) ss << v.first << "," << v.second << " ";
ss << "}" << endl;
cout << ss.str() << endl;
}
template <typename T>
string string_vector(vector<T>& vec) {
stringstream ss;
ss << "{";
for (auto v : vec) ss << v << " ";
ss << "}\n";
return ss.str();
}
template <typename T>
string string_vector(vector<vector<T>>& vec) {
stringstream ss;
ss << "{";
for (auto v : vec) {
ss << "{";
for (auto vv : v) ss << vv << " ";
ss << "}";
}
ss << "}\n";
return ss.str();
}
using ll = long long;
class Solution {
public:
vector<int> dp;
vector<int> dp2;
vector<int> bad;
bool is_good(int s, int N, int M, const vector<int>& A, const vector<int>& B) {
for(int i = 0; i < N; ++i) {
if(!(s & (1<<i))) continue;
if(s & bad[i]) return false;
}
return true;
}
ll fix(int s, int N, int M, const vector<int>& A, const vector<int>& B) {
if(s == 0) return 0;
if(dp[s]) return dp[s]-1;
//if(!is_good(s, N, M, A, B)) return N;
ll min_cc = N;
int b = 0;
do {
//res = fix(s & ~b, N, M, A, B);
//if(b!=0 and is_good(s,N,M,A,B)) min_cc = min(min_cc, 1 + fix(s & ~b, N, M, A, B));
bool good = false;
if(!dp2[b]) dp2[b] = is_good(b, N, M, A, B) + 1;
if(b!= 0 and dp2[b] - 1) {
min_cc = min(min_cc, 1 + fix(s & ~b, N, M, A, B));
}
} while((b = (b - s) & s));
dp[s] = min_cc + 1;
return min_cc;
}
ll solve(int N, int M, const vector<int>& A, const vector<int>& B) {
dp.resize(1<<N);
dp2.resize(1<<N);
bad.resize(N, (1<<N) - 1);
for(int i = 0; i < M; ++i) {
bad[A[i]] ^= (1<<B[i]);
bad[B[i]] ^= (1<<A[i]);
}
for(int i = 0; i < N; ++i) {
bad[i] = bad[i] & ( ((1<<N) - 1) - ((1<<i)));
}
int min_cc = fix((1<<N) - 1, N, M, A, B);
return min_cc;
}
};
// generated by online-judge-template-generator v4.7.1 (https://github.com/online-judge-tools/template-generator)
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
Solution sol;
int N, M;
cin >> N >> M;
vector<int> A(M), B(M);
REP (i, M) { cin >> A[i] >> B[i];
A[i]--, B[i]--;}
auto ans = sol.solve(N, M, A, B);
cout << ans << endl;
return 0;
}
|
#include <iomanip>
#include <iostream>
#include <unordered_map>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
unordered_map<int64_t, double> memo;
double dfs(int a, int b, int c) {
if (a == 100 || b == 100 || c == 100) {
return 0.0;
}
int64_t key = 10000 * a + 100 * b + c;
if (memo.count(key)) return memo[key];
double ret = 0.0;
if (a < 100) {
ret += (dfs(a + 1, b, c) + 1) * (double)a / (double)(a + b + c);
}
if (b < 100) {
ret += (dfs(a, b + 1, c) + 1) * (double)b / (double)(a + b + c);
}
if (c < 100) {
ret += (dfs(a, b, c + 1) + 1) * (double)c / (double)(a + b + c);
}
return memo[key] = ret;
}
int main(void) {
ios::sync_with_stdio(false);
int A, B, C;
cin >> A >> B >> C;
double answer = dfs(A, B, C);
cout << setprecision(20) << answer << endl;
return 0;
}
| #include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define all(v) v.begin(),v.end()
#pragma gcc optimize("O3")
#pragma gcc optimize("Ofast")
#pragma gcc optimize("unroll-loops")
using namespace std;
const int INF = 1e9;
const int TMX = 1 << 18;
const long long llINF = 1e16;
const long long mod = 998244353;
const long long hashmod = 100003;
const int MAXN = 100000;
const int MAXM = 1000000;
typedef long long ll;
typedef long double ld;
typedef pair <int,int> pi;
typedef pair <ll,ll> pl;
typedef vector <int> vec;
typedef vector <pi> vecpi;
typedef long long ll;
ll mpow(ll x,ll M) {
if(!M) return 1;
ll tmp = mpow(x,M/2);
tmp = tmp*tmp%mod;
if(M % 2) return tmp*x%mod;
return tmp;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
int n,m,k;
cin >> n >> m >> k;
ll ans = 0;
if(n > m) swap(n,m);
if(n == 1) {
for(int X = 1;X <= k;X++) {
ans += (mpow(X,m)-mpow(X-1,m)+mod)%mod;
ans %= mod;
}
cout << ans;
return 0;
}
for(int X = 1;X <= k;X++) {
ans += ((mpow(X,n)-mpow(X-1,n)+mod)%mod)*mpow(k-X+1,m)%mod;
ans %= mod;
}
cout << ans;
} |
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
int main(){
double R, X, Y;
cin >> R >> X >> Y;
double distance = sqrt(X*X+Y*Y);
int count = ceil(distance / R);
if(count == 1 && distance != R) count++;
cout << count << endl;
} | //khodaya khodet komak kon
# include <bits/stdc++.h>
/*
// ordered_set
# include <ext/pb_ds/assoc_container.hpp>
# include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
# define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
*/
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <pii, int> ppi;
typedef pair <int, pii> pip;
typedef pair <pii, pii> ppp;
typedef pair <ll, ll> pll;
# define A first
# define B second
# define endl '\n'
# define sep ' '
# define all(x) x.begin(), x.end()
# define kill(x) return cout << x << endl, 0
# define SZ(x) int(x.size())
# define InTheNameOfGod ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
ll power(ll a, ll b, ll md) {return (!b ? 1 : (b & 1 ? a * power(a * a % md, b / 2, md) % md : power(a * a % md, b / 2, md) % md));}
const int xn = 5e5 + 10;
const int xm = - 20 + 10;
const int sq = 320;
const int inf = 1e9 + 10;
const ll INF = 1e18 + 10;
const int mod = 998244353;
const int base = 257;
ll n, ans, res;
string s, t;
int main(){
InTheNameOfGod;
cin >> n >> s >> t;
s = '.' + s, t = '.' + t;
for (int i = 1; i <= n; ++ i){
ans += res;
if (s[i] == t[i])
continue;
if (s[i] == '0' || !res)
++ res;
else
-- res;
}
if (res)
kill(- 1);
kill(ans);
return 0;
}
|
#include <iostream>
#include <assert.h>
#include <vector>
#include <unordered_map>
#include <queue>
#include <climits>
#include <cmath>
#include <algorithm>
#include <iomanip>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int,int>;
constexpr ll MOD = 1000000007;
//cout << std::fixed << std::setprecision(15) << res << endl;
//int di[] = {-1,0,1,0};
//int dj[] = {0,-1,0,1};
//printf("%.10f %.10f\n), ans.real(), ans.imag());
int main(){
std::cin.tie(0);
std::ios::sync_with_stdio(false);
int N;
cin >> N;
vector<ll> A(N);
rep (i, N) cin >> A[i];
if (N == 1) {
cout << A[0] << endl;
return 0;
} else if (N == 2) {
cout << 2 * A[0] % MOD << endl;
return 0;
}
vector<ll> X(N+1);
X[0] = 1;
X[1] = 1;
ll psum = 0;
vector<ll> pow2(N,0);
pow2[0] = 1;
for (ll k = 1; k < N; k++) {
pow2[k] = (2 * pow2[k-1]) % MOD;
if (k - 2 >= 0) {
psum *= 2;
psum %= MOD;
psum += X[k-2];
psum %= MOD;
}
X[k+1] = pow2[k] - psum;
if (X[k+1] < 0 ) X[k+1] += MOD;
}
ll res = 0;
res += (A[0] * X[N]);
res %= MOD;
res += A[1] * ((X[N] - X[N-2]) - X[N-2]);
//if (res < 0) res += MOD;
res %= MOD;
for (ll k = 2; k < N - 1; k++) {
ll tmp = (X[N-k-1] * X[k-1]) % MOD;
res += A[k] * (X[N] - tmp - tmp);
//if (res < 0) res += MOD;
res %= MOD;
}
res += A[N-1] * ((X[N] - X[N-2]) - X[N-2]);
res %= MOD;
if (res < 0) res += MOD;
cout << res << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define PI 3.14159265358979323
#define ll long long int
#define vi vector <int>
#define vl vector <ll>
#define all(v) (v).begin(),(v).end()
#define pb push_back
#define ff first
#define ss second
#define MOD 1000000007
const ll mod = 1e9 + 7;
ll power(ll a, ll b) { //a^b
ll res = 1;
a = a % MOD;
while (b > 0) {
if (b & 1) {res = (res * a) % MOD; b--;}
a = (a * a) % MOD;
b >>= 1;
}
return res;
}
ll gcd(ll a, ll b) {return (b == 0) ? a : gcd(b, a % b);}
int main() {
#ifndef ONLINE_JUDGE
// for getting input from input.txt
freopen("input.txt", "r", stdin);
// for writing output to output.txt
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n;
cin >> n;
vl v(n);
for (auto &i : v)
cin >> i;
vector <vl> dp(n, vl (2, 0));
dp[0][0] = 1;
dp[0][1] = 1;
for (ll i = 1; i < n; i++) {
dp[i][0] = (dp[i - 1][0] + dp[i - 1][1]) % mod;
dp[i][1] = dp[i - 1][0];
}
ll ans = 0;
for (ll i = 0; i < n; i++) {
ll l, r;
l = i;
r = n - i - 1;
if (l == 0)
l = 1;
else if (l == 1)
l = 1;
else
l = (dp[l - 2][0] + dp[l - 2][1]) % mod;
if (r == 0)
r = 1;
else
r = (dp[r - 1][0] + dp[r - 1][1]) % mod;
ans = (ans + ((l * r) % mod * v[i]) % mod) % mod;
if (i == 0)
continue;
l = i;
r = n - i - 1;
if (l == 1)
l = 1;
else
l = (dp[l - 2][0]) % mod;
if (r == 0)
r = 1;
else
r = (dp[r - 1][0]) % mod;
ans = (ans - ((l * r) % mod * v[i]) % mod + mod) % mod;
//cout << ans << "\n";
}
cout << ans;
}
|
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;
//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;
int n,m,s,t;
int e[N],ne[N],w[N],k[N],h[N],idx;
LL dis[N];
bool st[N];
struct Node
{
int u;
LL d;
bool operator < (const Node &W) const
{
return d>W.d;
}
};
void add(int a,int b,int c,int d)
{
e[idx]=b,w[idx]=c,k[idx]=d,ne[idx]=h[a],h[a]=idx++;
}
void dijkstra()
{
priority_queue<Node>q;
q.push({s,0});
memset(dis,INF,sizeof(dis));
dis[s]=0;
while(q.size())
{
Node cur=q.top(); q.pop();
int u=cur.u; LL d=cur.d;
if(st[u]) continue;
st[u]=true;
for(int i=h[u];~i;i=ne[i])
{
int ver=e[i];
LL now=d;
if(d%k[i]!=0&&d!=0) now/=k[i],now++,now=now*k[i];
if(dis[ver]>now+w[i])
{
dis[ver]=now+w[i];
q.push({ver,dis[ver]});
}
}
}
}
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);
memset(h,-1,sizeof(h)); idx=0;
scanf("%d%d%d%d",&n,&m,&s,&t);
for(int i=1;i<=m;i++)
{
int a,b,c,d; scanf("%d%d%d%d",&a,&b,&c,&d);
add(a,b,c,d); add(b,a,c,d);
}
dijkstra();
printf("%lld\n",dis[t]==0x3f3f3f3f3f3f3f3f? -1:dis[t]);
return 0;
}
/*
*/
| #include <bits/stdc++.h>
#define F first
#define S second
#define rep(i, a, b) for(int i = (a); i < (b); ++i)
#define per(i, a, b) for(int i = (b)-1; i >= (a); --i)
#define bck(i, a, b) if ((i) >= (a) && (i) < (b))
#define trav(x, a) for (auto &x : (a))
#define sz(a) (int)(a).size()
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define pb push_back
#define eb emplace_back
using namespace std;
typedef long long ll;
typedef string str;
template<typename T> using vec = vector<T>;
template<typename T> using pq = priority_queue<T, vector<T>, std::greater<T>>;
template<typename T> using mxpq = priority_queue<T>;
typedef pair<int,int> pii;
typedef vec<int> vi;
typedef vec<pii> vii;
typedef vec<vi> vvi;
typedef pair<ll,ll> pll;
typedef vec<ll> vl;
typedef vec<pll> vll;
typedef vec<vl> vvl;
template<typename A, typename B>
istream& operator>>(istream& s, pair<A,B>& p) { return s>>p.first>>p.second; }
template<typename T>
istream& operator>>(istream& s, vec<T>& p) {
for (T& t : p) s >> t;
return s;
}
ll dijk(vec<vec<pair<int, pll>>> &g, int s, int e) {
pq<pll> q;
q.push({0, s});
vec<bool> v(sz(g));
while (q.size()) {
auto [d, i] = q.top(); q.pop();
if (v[i]) continue;
v[i] = true;
if (i == e) return d;
trav(ch, g[i]) {
int next_city = ch.F;
ll k = ch.S.S;
ll t = ch.S.F;
if (!v[ch.F]) {
ll next_train = (d + k-1) / k * k; // ceil(d / k) * k
q.push({next_train + t, next_city});
}
}
}
return -1;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n, m, s, e;
cin >> n >> m >> s >> e;
vec<vec<pair<int, pll>>> g(n);
rep(i, 0, m) {
int a, b, t, k;
cin >> a >> b >> t >> k;
g[a-1].pb({b-1, {t, k}});
g[b-1].pb({a-1, {t, k}});
}
cout<<dijk(g, s-1, e-1)<<endl;
}
|
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define MOD (ll)(998244353)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);}
int main()
{
int x;
cin>>x;
x = max(x,0);
cout<<x;
}
|
// Problem : A - ReLU
// Contest : AtCoder - AtCoder Beginner Contest 183
// URL : https://atcoder.jp/contests/abc183/tasks/abc183_a
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
// Problem : E. Tree Queries
// Contest : Codeforces - Codeforces Round #629 (Div. 3)
// URL : https://codeforces.com/contest/1328/problem/E
// Memory Limit : 256 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
#include<bits/stdc++.h>
using namespace std;
#define _rep(i, x, y) for(int i = (int)x; i < (int)y; ++i)
#define _dep(i,x,y) for(int i = (int)x; i > (int)y; i--)
#define PII pair<int,int>
#define eb emplace_back
#define pb push_back
#define fi first
#define se second
#define PQ priority_queue
#define lb lower_bound
#define ub upper_bound
typedef long long ll;
typedef vector<int> VI;
constexpr int mod = 1e9 + 7;
constexpr int KINF = 0x3f3f3f3f;
constexpr double eps = 1e-7;
int main(){
ios::sync_with_stdio(false); cin.tie(0);
int x;
cin >> x;
if(x >= 0) cout << x << endl;
else cout << 0 << endl;
return 0;
}
|
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using P=pair<ll,ll>;
template<class T> using V=vector<T>;
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
const ll inf=(1e18);
const ll mod=998244353;
//const ll mod=1000000007;
const vector<int> dy={-1,0,1,0},dx={0,-1,0,1};
ll GCD(ll a,ll b) {return b ? GCD(b,a%b):a;}
ll LCM(ll c,ll d){return c/GCD(c,d)*d;}
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(20);}} __init;
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; }
template<class T>void debag(const vector<T> &a){cerr<<"debag :";for(auto v:a)cerr<<v<<" ";cerr<<"\n";}
template<class T>void print(const vector<T> &a){for(auto v:a)cout<<v<<" ";cout<<"\n";}
struct mint{
using ull=unsigned long long int;
ull v;
mint(ll vv=0){s(vv%mod+mod);}
mint& s(ull vv){
v=vv<mod?vv:vv-mod;
return *this;
}
//オーバーロード
mint operator-()const{return mint()-*this;}//mint型にキャスト
mint&operator+=(const mint&val){return s(v+val.v);}
mint&operator-=(const mint&val){return s(v+mod-val.v);}
mint&operator*=(const mint&val){
v=ull(v)*val.v%mod;
return *this;
}
mint&operator/=(const mint&val){return *this*=val.inv();}
mint operator+(const mint&val){return mint(*this)+=val;}
mint operator-(const mint&val){return mint(*this)-=val;}
mint operator*(const mint&val){return mint(*this)*=val;}
mint operator/(const mint&val){return mint(*this)/=val;}
mint pow(ll n)const{
mint res(1),x(*this);
while(n){
if(n&1)res*=x;
x*=x;
n>>=1ll;
}
return res;
}
mint inv()const{return pow(mod-2);}
//拡張ユークリッドの互除法
/* mint inv()const{
int x,y;
int g=extgcd(v,mod,x,y);
assert(g==1);
if(x<0)x+=mod;
return mint(x);
}*/
friend ostream& operator<<(ostream&os,const mint&val){
return os<<val.v;
}//出力
bool operator<(const mint&val)const{return v<val.v;}
bool operator==(const mint&val)const{return v==val.v;}
bool operator>(const mint&val)const{return v>val.v;}
};
const ll MAX = 2000010;//設定
mint fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void init(){
fac[0] = fac[1] = 1;
for(int i=1;i<MAX;i++)fac[i]=fac[i-1]*i;
finv[MAX-1]=fac[MAX-1].inv();
for(int i=MAX-2;i>=0;i--)finv[i]=finv[i+1]*(i+1);
for(int i=MAX-2;i>=1;i--)inv[i]=finv[i]+fac[i-1];
}
//階乗
mint factor(ll n,ll k){
if (n<k) return 0;
if (n<0 || k<0) return 0;
return fac[n]*finv[k];
}
// 二項係数計算
mint COM(int n, int k){
if (n < k) return 0;
if (n < 0 || k < 0) return 0;
return fac[n] * finv[k] * finv[n - k];
}
class UF{
public:
vector<int> par,num;
int find(int v){
return (par[v]==v)? v: (par[v]=find(par[v]));
}
explicit UF(int N):par(N),num(N,1){
iota(all(par),0);
}
void unite(int u,int v){
u=find(u),v=find(v);
if(u==v)return;
if(num[u]<num[v])swap(u,v);
num[u]+=num[v];
par[v]=u;
}
bool same(int u,int v){
return find(u)==find(v);
}
bool ispar(int v){
return v=find(v);
}
int size(int v){
return num[find(v)];
}
};
int main(){
init();
int n,K;
cin>>n>>K;
V<V<int>> a(n,V<int>(n));
for(int i=0;i<n;i++){
for(int j=0;j<n;j++)cin>>a[i][j];
}
UF l(n);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
bool ok=true;
for(int k=0;k<n;k++){
if(a[i][k]+a[j][k]>K)ok=false;
}
if(ok)l.unite(i,j);
}
}
mint ans=mint(1);
for(int i=0;i<n;i++){
if(l.find(i)!=i)continue;
ans*=fac[l.size(i)];
}
UF r(n);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
bool ok=true;
for(int k=0;k<n;k++){
if(a[k][i]+a[k][j]>K)ok=false;
}
if(ok)r.unite(i,j);
}
}
for(int i=0;i<n;i++){
if(r.find(i)!=i)continue;
ans*=fac[r.size(i)];
}
cout<<ans<<"\n";
} |
// Problem: C - Shuffle Permutation
// Contest: AtCoder - AtCoder Regular Contest 107
// URL: https://atcoder.jp/contests/arc107/tasks/arc107_c
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define endl '\n'
#define pb push_back
#define int long long
#define vi vector<int>
#define mii map<int, int>
#define pii pair<int, int>
#define all(x) (x).begin(), (x).end()
#define fill(a,b) memset(a, b, sizeof(a))
#define setbits(x) __builtin_popcountll(x)
#define rep(i,x,y) for(int i=(int)x; i<y; i++)
#define px(a) for(auto x: a) cout<<x<<" "; cout<<'\n';
#define pxy(a,n) rep(i,0,n) {px(a[i])};
const long long MOD=998244353,N=1e5+5;
int power(int x, unsigned int y)
{
int res = 1;
x = x % MOD;
if (x == 0) return 0;
while (y > 0)
{
if (y & 1)
res = (res*x) % MOD;
y = y>>1;
x = (x*x) % MOD;
}
return res;
}
int fact[N];
void factorial(int n){
fact[0]=1;
for(int i=1;i<=n;i++){
fact[i]=fact[i-1]*i;
if(fact[i]>=MOD)
fact[i]%=MOD;
}
}
class dsu {
public:
vector<int> p;
int n;
dsu(int _n) : n(_n) {
p.resize(n);
iota(p.begin(), p.end(), 0);
}
inline int get(int x) {
return (x == p[x] ? x : (p[x] = get(p[x])));
}
inline bool unite(int x, int y) {
x = get(x);
y = get(y);
if (x != y) {
p[x] = y;
return true;
}
return false;
}
};
int32_t main()
{
std::ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
//Remove incase of Online Query
factorial(55);
int n,kk,ans=1; cin>>n>>kk;
int a[n][n];
rep(i,0,n) rep(j,0,n) cin>>a[i][j];
dsu r(n),c(n);
rep(i,0,n) rep(j,i+1,n){
rep(k,0,n){
if(a[i][k]+a[j][k]>kk) goto end;
}
r.unite(i,j);
end:;
}
rep(i,0,n) rep(j,i+1,n){
rep(k,0,n){
if(a[k][i]+a[k][j]>kk) goto end1;
}
c.unite(i,j);
end1:;
}
int r_val[n]={0};
int c_val[n]={0};
rep(i,0,n){
r_val[r.get(i)]++;
c_val[c.get(i)]++;
}
// px(r_val);
// px(c_val);
rep(i,0,n){
ans*=fact[r_val[i]];
ans%=MOD;
ans*=fact[c_val[i]];
ans%=MOD;
}
cout<<ans<<endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define pii pair<int,int>
#define vi vector<int>
#define mii map<int,int>
#define pqb priority_queue<int>
#define pqs priority_queue<int,vi,greater<int> >
#define setbits(x) __builtin_popcountll(x)
#define mod 1000000007
#define hell 100000000000000007 //10e18+7
#define mod 1000000007//10e9+7
#define pi 3.141592653589793238
#define ps(x,y) fixed<<setprecision(y)<<x
#define mk(arr,n,type) type *arr=new type[n];
#define range(a,b) substr(a,b-a+1)
#define w(x) int x; cin>>x; while(x--)
#define trace(x) cerr<<#x<<": "<<x<<" "<<endl;
#define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int arr[300002];
int segtree[4 * 300002 + 1];
// int mod_exp(int x, int y, int mm)
// {
// x %= mm;
// if (y == 0)
// return (1);
// else if (y % 2 == 0)
// return (mod_exp((x * x) % mm, y / 2, mm));
// else
// return ((x * mod_exp((x * x) % mm, (y - 1) / 2, mm)) % mm);
// }
// int modInverse(int n, int p)
// {
// return mod_exp(n, p - 2, p);
// }
//remember the approach of suming up the element
// and when it exceeds from our radar
// try removing from the staring part
//priority queue max heap imp!!
void buildSegtree(int s1, int e1, int p1)
{
if (s1 == e1)
{
segtree[p1] = (arr[s1]);
return;
}
int mid = ((s1 + e1) / 2);
buildSegtree(s1, mid, 2 * p1 + 1);
buildSegtree(mid + 1, e1, 2 * p1 + 2);
segtree[p1] = ((segtree[2 * p1 + 1])^segtree[2 * p1 + 2]) ;
}
int rangeQuery(int q1, int q2, int s1, int e1, int p1)
{
if (q1 <= s1 && q2 >= e1)
return (segtree[p1]);
if (q2 < s1 || q1 > e1)
return (0);//it varies BE CAREFUint!!
int mid = (s1 + e1) / 2;
int l1 = rangeQuery(q1, q2, s1, mid, 2 * p1 + 1);
int l2 = rangeQuery(q1, q2, mid + 1, e1, 2 * p1 + 2);
return ((l1 ^ l2));
}
void updateQuery(int q1, int q2, int id, int p1, int val)
{
if (q1 == q2)
{
segtree[p1] = segtree[p1] ^ val;
return;
}
int mid = (q1 + q2) / 2;
if (id > mid)
updateQuery(mid + 1, q2, id, 2 * p1 + 2, val);
else
updateQuery(q1, mid, id, 2 * p1 + 1, val);
segtree[p1] = (segtree[2 * p1 + 1] ^ segtree[2 * p1 + 2]);
}
// int binpow(int a, int b, int m)
// {
// a %= m;
// int res = 1;
// while (b > 0) {
// if (b & 1)
// res = res * a % m;
// a = a * a % m;
// b >>= 1;
// }
// return res;
// }
// int inv(int a) {
// a %= mod;
// if (a < 0) a += mod;
// int b = mod, u = 0, v = 1;
// while (a) {
// int t = b / a;
// b -= t * a; swap(a, b);
// u -= t * v; swap(u, v);
// }
// if (u < 0) u += mod;
// return u;
// }
int32_t main()
{
#ifndef ONLINE_JUDGE
freopen("i1.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++)
cin >> arr[i];
buildSegtree(0, n - 1, 0);
while (m--)
{
int a, b, c;
cin >> a >> b >> c;
if (a == 1)
{
b--;
updateQuery(0, n - 1, b, 0, c);
}
else if (a == 2)
{
b--;
c--;
int ans = rangeQuery(b, c, 0, n - 1, 0);
cout << ans << endl;
}
}
}
| /*author @dhanush*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define boost \
ios_base::sync_with_stdio(false); \
cin.tie(NULL)
#define pb push_back
#define all(x) x.begin(), x.end()
#define se second
#define fi first
ll MOD = 1e9 + 7;
const ll N = 200000 + 10;
vector<ll>adj[N];
ll sz[N];
ll dp[N];
void dfs(ll n,ll p)
{
for(auto x:adj[n])
{
if(x!=p)
{
sz[x]+=sz[n];
dfs(x,n);
}
}
}
void dfsdp(ll n,ll p)
{
for(auto x:adj[n])
{
if(x!=p)
{
dp[x]=dp[n]+1;
dfsdp(x,n);
}
}
}
void peace()
{
ll n;
cin>>n;
vector<pair<ll,ll>>v;
for(int i=1;i<n;i++)
{
ll a,b;
cin>>a>>b;
v.pb({a,b});
adj[a].pb(b);
adj[b].pb(a);
}
dfsdp(1,-1);
ll q;
cin>>q;
while(q--)
{
ll t,e,x;
cin>>t>>e>>x;
ll nxt=v[e-1].se;
ll par=v[e-1].fi;
if(dp[par]>dp[nxt])
{
swap(par,nxt);
t^=3;
}
if(t==1)
{
sz[1]+=x;
sz[nxt]-=x;
}
else
{
//de
sz[nxt]+=x;
}
}
dfs(1,-1);
for(int i=1;i<=n;i++)
{
cout<<sz[i]<<endl;
}
}
int main()
{
boost;
ll t;
t = 1;
//----------
// cin >> t;
//----------
while (t--)
peace();
}
|
#include<bits/stdc++.h>
using namespace std;
using LL=long long;
using P=pair<int,int>;
using T=tuple<int,int,int>;
constexpr int mod=1000000007;
constexpr int inf=1e9;
int in(){
int x;
scanf("%d",&x);
return x;
}
struct graph{
vector<vector<int>>e;
vector<int>used;
int cnt=0;
graph(int n){
e.resize(n);
used.resize(n);
}
void add_edge(int v,int u){
e[v].emplace_back(u);
}
void dfs(int v){
cnt++;
for(int u:e[v])if(used[u]==0)used[u]=1,dfs(u);
}
int solve(int s){
fill(used.begin(),used.end(),0);
used[s]=1;
cnt=0;
dfs(s);
return cnt;
}
};
int main(){
int n=in();
graph g(n);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
char c;
cin>>c;
if(c=='1')g.add_edge(j,i);
}
}
double ans=0.0;
for(int i=0;i<n;i++)ans+=1.0/double(g.solve(i));
printf("%.10lf",ans);
return 0;
} | #include <bits/stdc++.h>
using Int = long long; // clang-format off
#define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define RREP_(i, a_, b_, a, b, ...) for (Int i = Int(b) - 1, low##i = (a); i >= low##i; i--)
#define RREP(i, ...) RREP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define ALL(v) std::begin(v), std::end(v)
struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io;
#ifndef dump
#define dump(...)
#endif // clang-format on
struct in {
template <class T> operator T() {
T t;
std::cin >> t;
return t;
}
};
void out() { std::cout << "\n"; }
template <class Head, class... Tail> void out(Head&& h, Tail&&... t) {
std::cout << h << (sizeof...(Tail) == 0 ? "" : " "), out(std::forward<Tail>(t)...);
}
template <class T> bool chmin(T& a, const T& b) { return a > b ? a = b, true : false; }
template <class T> bool chmax(T& a, const T& b) { return a < b ? a = b, true : false; }
template <class T> using V = std::vector<T>;
/**
* author: knshnb
* created: Sun Feb 28 00:29:14 JST 2021
**/
V<V<Int>> solve(Int n) {
V<V<Int>> ret(1);
REP(i, 1 << n) ret[0].push_back(i < (1 << (n - 1)));
if (n == 1) return ret;
auto sub = solve(n - 1);
REP(t, 2) {
for (auto& v : sub) {
ret.push_back({});
for (Int x : v) ret.back().push_back(x);
for (Int x : v) ret.back().push_back(t ? !x : x);
}
}
return ret;
}
signed main() {
Int n = in();
auto res = solve(n);
out(res.size());
for (auto& v : res) {
for (Int x : v) std::cout << char('A' + x);
out();
}
}
|
#include<bits/stdc++.h>
#define LL long long
using namespace std;
LL n,ans;
int main()
{
cin>>n;
n*=2;
for(LL i=1;i*i<=n;i++)
{
if(n%i==0)
{
LL a=i,b=n/i;
if(a%2!=b%2)ans+=2;
}
}
cout<<ans;
return 0;
}
| #include<iostream>
#include<iterator>
#include<algorithm>
#include<climits>
#include<map>
#include<set>
#include<cmath>
#include<numeric>
#include<string>
#include<sstream>
#include<cstdio>
#include<vector>
#include<bitset>
// #include <atcoder/segtree>
#define ll long long
using namespace std;
int main()
{
ll n,s,c=0;
cin >> n;
s = (ll)sqrt(n);
for(ll i = 1; i <= s; ++i){
ll cp = n/i;
if(n%i==0){
if(i%2==1){
c++;
}
if (cp%2==1){
if (cp!=i)c++;
}
}
}
cout<<(2*c)<<endl;
} |
#include <iostream>
#include <queue>
using namespace std;
using ll = long long;
int n;
priority_queue<ll> q;
void solve()
{
q.push(0);
ll res = 1, before = q.top(), now;
q.pop();
while (q.size() > 0)
{
now = q.top();
q.pop();
if (before > now)
{
res = res * (before - now + 1) % (1000000007);
before = now;
}
}
cout << res << '\n';
}
int main()
{
ll qtemp;
cin >> n;
for (int i=0; i<n; i++)
{
cin >> qtemp;
q.push(qtemp);
}
solve();
return 0;
}
| #pragma warning(disable: 4996)
#include <string>
#include <vector>
#include <iostream>
#include <cstdio>
#include <sstream>
#include <fstream>
#include <math.h>
#include <algorithm>
#include <map>
#include <bitset>
#include <queue>
using namespace std;
typedef long long ll;
#define rep(i,n) for (int i = 0; i < (n); i++)
int main() {
ll n;
cin >> n;
ll tmp = 0, s = 0;
while (s <= n + 1) {
tmp++;
s += tmp;
if (s > n + 1)tmp -= 1;
}
ll ans = n - tmp + 1;
cout << ans << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ll b,c;
scanf("%lld%lld",&b,&c);
ll cnt=0;
if(b<=0){
cnt=c+1;
c--;
if(b==0)cnt--;
if(c>0&&b<0)cnt+=min(-2*b-1,c-1);
}else {
cnt=c+(c==1?1:0);
cnt+=min(2*b-1,c-1);
}printf("%lld\n",cnt);
} | #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>;
ll INF = 1e18;
int main() {
int n;
cin >> n;
vector<ll> a(n);
ll m = 0;
rep(i,n){
ll x, y;
cin >> x >> y;
a[i] = 2 * x + y;
m += x;
}
sort(a.begin(),a.end());
reverse(a.begin(),a.end());
ll count = 0;
rep(i,n){
count += a[i];
if(count > m){
cout << i+1 << endl;
break;
}
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
#define ALL(obj) obj.begin(),obj.end()
#define SORT(obj) sort(obj.begin(),obj.end())
#define pb(obj) push_back(obj)
#define rep(i,n) for(int i=0; i<n; i++)
typedef long long ll;
ll const MOD=1e9+7;
const ll INF = 9000000000000000000;
vector<bool> seen;//グラフ用の配列
long long pow(long long x, long long n) {
long long 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;
}
int ctoi(const 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 -1;
}
}
int kakuketa(int n){
int ans = 0;
while(n>0){
ans += n%10;
n /= 10;
}
return ans;
}
void dfs2(int y,int x,vector<vector<bool>> &flags,Graph &G){//二次元dfs
if(G[y][x] == 0) return;
if(flags[y][x] == true) return;
else{
flags[y][x] = true;
}
dfs2(y+1,x,flags,G);
dfs2(y+1,x+1,flags,G);
dfs2(y+1,x-1,flags,G);
dfs2(y,x+1,flags,G);
dfs2(y,x-1,flags,G);
dfs2(y-1,x+1,flags,G);
dfs2(y-1,x,flags,G);
dfs2(y-1,x-1,flags,G);
}
int main(){
ll r,x,y;
cin>>r>>x>>y;
long double kyori0 = sqrt(x*x+y*y);
ll kyori;
if(floor(kyori0) == ceil(kyori0)){
kyori = floor(kyori0);
if(kyori < r){
cout<<2<<endl;
return 0;
}
if(kyori%r==0){
cout<<kyori/r<<endl;
return 0;
}
else{
cout<<kyori/r+1<<endl;
return 0;
}
}
else{
kyori = floor(kyori0)+1;
if(kyori-1 < r){
cout<<2<<endl;
return 0;
}
if(kyori%r==0){
cout<<kyori/r<<endl;
return 0;
}
else{
cout<<kyori/r+1<<endl;
return 0;
}
}
} | #include <iostream>
#include <bits/stdc++.h>
#define ll long long int
#define max_value 0x3f3f3f
/*freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);*/
using namespace std;
const int inf=1e9+7;
void solve(){
double r,x,y;
cin>>r>>x>>y;
double z=sqrt(x*x+y*y);
if(z==r)
cout<<"1"<<endl;
else if(z<=2*r)
cout<<"2"<<endl;
else {
int ans=ceil(z/r);
cout<<ans<<endl;}
}
int main(){
int t;
t=1;
while(t--){
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main(){
int n,m;
cin>>n>>m;
vector<vector<tuple<int,long,long>>>g(n+1);
for(int i=0;i<m;i++){
int a,b,c,d;
cin>>a>>b>>c>>d;
g[a].emplace_back(b,c,d);
g[b].emplace_back(a,c,d);
}
auto w=[](long t,long d){
int l=sqrt(d)-t-10;
l=max(l,0);
int m=l;
for(int i=l;i<20+l;i++)
if(i+d/(t+i+1)<m+d/(t+m+1))
m=i;
return m;
};
priority_queue<pair<long,int>,vector<pair<long,int>>,greater<>>q;
vector<long>D(n+1,1e18);
q.emplace(D[1]=0,1);
while(!q.empty()){
auto[t,u]=q.top();
q.pop();
if(u==n)break;
if(t>D[u])continue;
for(auto[v,c,d]:g[u]){
auto T=t+w(t,d);
auto x=T+c+d/(T+1);
if(D[v]>x)q.emplace(D[v]=x,v);
}
}
cout<<(D[n]<1e18?D[n]:-1)<<endl;
}
| //#define MULTICASES
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,a,b) for(i=(a);i<=(b);++i)
#define per(i,a,b) for(i=(a);i>=(b);--i)
#define REP(i,a,b) for(i=(a);i< (b);++i)
#define PER(i,a,b) for(i=(a);i> (b);--i)
#define ERR(...) fprintf(stderr,__VA_ARGS__)
inline void ac();
int main(){
#ifdef MULTICASES
int t;scanf("%d",&t);while(t--)ac();
#else
ac();
#endif
return 0;
}
const int MAXN=200005;
int d,Ans;
vector<int>E[MAXN];
inline int dfs(const int &v=1,const int &f=0){
int a=-1,b=0,t,u;
vector<int>::iterator e,ee;
for(e=E[v].begin(),ee=E[v].end();e!=ee;++e)if((u=*e)!=f){
t=dfs(u,v);
if(t<0){if(t<b)b=t;}
else {if(t>a)a=t;}
}
//ERR("v=%d a=%d b=%d\n",v,a,b);
//if(a>-b){ERR("I. %d\n",a-1);}
//else{if(b-1<-d){ERR("II. %d (Ans=%d)\n",d,Ans+1);}else{ERR("III. %d\n",b-1);}}
if(a>-b)return a-1;
else{if((--b)<-d){++Ans;return d;}else return b;}
}
inline void ac(){
int n,k,i,v,u,l,r;
scanf("%d%d",&n,&k);
rep(i,2,n){
scanf("%d%d",&v,&u);
E[v].push_back(u);
E[u].push_back(v);
}
i=n-1;l=1;r=n-2;
while(l<=r){
d=(l+r)>>1;Ans=0;
if(dfs()<0)++Ans;
//ERR("\nd=%d Ans=%d\n\n\n\n",d,Ans);
if(Ans>k)l=d+1;
else r=(i=d)-1;
}
printf("%d\n",i);
}
/*
0. int overflow, array bounds
1. special cases (n=1? n=0?)
2. do smth instead of nothing and stay organized
3. WRITE STUFF DOWN
4. DON'T GET STUCK ON ONE APPROACH
5. STAY CALM AND DON'T GET EXPLODED
*/
|
#include <bits/stdc++.h>
#define ls (rt<<1)
#define rs (rt<<1|1)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=4e5+10;
const int INF=0x3f3f3f3f;
int a[N],n;
pii pa[N];
int mx[N<<2],lazy[N<<2];
int main() {
auto push_down=[&](int rt){
if(lazy[rt]){
lazy[ls]+=lazy[rt];
lazy[rs]+=lazy[rt];
mx[ls]-=lazy[rt];
mx[rs]-=lazy[rt];
lazy[rt]=0;
}
};
function<void(int,int,int)>build=[&](int l,int r,int rt){
if(l==r){mx[rt]=l;return ;}
int mid=l+r>>1;
build(l,mid,ls);
build(mid+1,r,rs);
};
function<int(int,int,int,int)>query=[&](int l,int r,int x,int rt){
if(l==r)return mx[rt];
push_down(rt);int mid=l+r>>1;
if(x<=mid)return query(l,mid,x,ls);else return query(mid+1,r,x,rs);
};
function<void(int,int,int,int,int,int)>update=[&](int l,int r,int L,int R,int val,int rt){
if(L<=l&&r<=R){mx[rt]-=val;lazy[rt]+=val;return ;}
push_down(rt);int mid=l+r>>1;
if(L<=mid)update(l,mid,L,R,val,ls);
if(mid<R)update(mid+1,r,L,R,val,rs);
mx[rt]=max(mx[ls],mx[rs]);
};
cin>>n;
vector<pair<int,int>>pa;
for(int i=1;i<=2*n;i++){
cin>>a[i],pa.emplace_back(a[i],i>n?i-n:n-i+1);
}
sort(pa.rbegin(),pa.rend());
ll ans=0;
build(1,n,1);
for(auto& i:pa){
int x=i.second;
int val=query(1,n,x,1);
if(val==0)continue;
ans+=i.first;
int l=1,r=n,res=-1;
while (l<=r){
int mid=l+r>>1;
if(query(1,n,mid,1)>=val){
res=mid;
r=mid-1;
}else l=mid+1;
}
update(1,n,res,n,1,1);
}
cout<<ans;
return 0;
}
| #include<iostream>
#include<string>
#include<vector>
#include<set>
#include<iomanip>
#include<algorithm>
#include<cmath>
#include<bitset>
#include<queue>
#include<stack>
#include<utility>
#include<cstdlib>
#include<cstdio>
#include<map>
#include<unordered_set>
#include<unordered_map>
#include<list>
#include<tuple>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
#define FOR(i, a, b) for(ll i=(a); i<(b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define NREP(i, n) FOR(i, 1, n+1)
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; }
struct IOSetup{
IOSetup(){
cin.tie(0);
ios::sync_with_stdio(0);
cout<<fixed<<setprecision(12);
}
} iosetup;
struct Edge{
ll to, cost;
Edge(ll t, ll c) : to(t), cost(c) { }
};
using Graph = vector<vector<Edge>>;
ll MODpow(ll x, ll n, ll mod){
ll res=1;
while(n>0){
if(n&1) res=res*x%mod;
x=x*x%mod;
n >>= 1;
}
return res;
}
const ll MOD=1e9+7;
const ll nmax=200000+10;
ll N;
Graph G(nmax);
ll solve(){
vvl color(nmax,vl(60,-1));
vl cnt1(60,0);
vl seen(nmax,0);
queue<ll> q;
q.push(0);
REP(i,60) color[0][i]=0;
while(!q.empty()){
ll v=q.front();
q.pop();
seen[v]=1;
for(auto e:G[v]){
if(seen[e.to]) continue;
ll et=e.to, ec=e.cost;
q.push(e.to);
REP(i,60){
bool eci=ec&1;
ec>>=1;
color[et][i]=color[v][i]^eci;
if(color[et][i]) cnt1[i]++;
}
}
}
ll res=0,rec=1;
REP(i,60){
ll mid1=rec;
rec=rec*2%MOD;
ll mid2=(N-cnt1[i])*cnt1[i]%MOD;
ll mid=mid1*mid2%MOD;
res+=mid;
res%=MOD;
}
return res;
}
int main(void){
scanf(" %ld",&N);
REP(i,N-1){
ll u,v,w; scanf(" %ld%ld%ld",&u,&v,&w);
u--,v--;
G[u].push_back(Edge{v,w});
G[v].push_back(Edge{u,w});
}
ll res=solve();
printf("%ld\n",res);
return 0;
} |
#include <iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<iomanip>
#include<map>
#include<random>
#include<complex>
#include<math.h>
#include<functional>
#include<stack>
#include<queue>
#include<unordered_map>
#include<set>
#include<numeric>
#include <cassert>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define req(i,n) for(int i = 1;i <= n; i++)
#define rrep(i,n) for(ll i = n-1;i >= 0;i--)
#define ALL(obj) begin(obj), end(obj)
#define RALL(a) rbegin(a),rend(a)
typedef long long ll;
typedef long double ld;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int inf = 0x3fffffff;
const ll INF = 1e18;
int n, m; vector<vector<pair<int, int>>> p;
bool check(int msk) {
int cnt = 0; vector<int> tp(20,0);
rep(i, n) {
if (msk & (1 << i)) cnt++,tp[i]++;
}rep(i, n) tp[i + 1] += tp[i];
for (pair<int, int> i : p[cnt]) {
if (tp[i.first] > i.second) return false;
}return true;
}
int main() {
cin >> n >> m; p.resize(n + 1); vector<ll> dp(1 << n,0);
dp[0] = 1;
rep(i, m) {
int x, y, z; cin >> x >> y >> z; y--;
p[x].push_back({ y,z });
}rep(i, 1 << n) {
rep(j, n) if (!(i & (1 << j)) && check(i)) dp[(1<<j)| i] += dp[i];
}cout << dp[(1 << n)-1] << endl;
} | #include<bits/stdc++.h>
//#include<atcoder/all>
using namespace std;
//using namespace atcoder;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long ll;
typedef pair<double,double> P;
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; }
template<typename T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
const ll mod=998244353;
const ll LINF=1ll<<60;
const int INF=1<<28;
int dx[]={1,0,-1,0,1,-1,1,-1};
int dy[]={0,1,0,-1,1,-1,-1,1};
int main(){
int n,m;cin >> n >> m;
vector<int> x(m), y(m), z(m);
for (int i = 0; i < m; i++) {
cin >> x[i] >> y[i] >> z[i];
}
vector<ll> dp(1 << (n + 1), 0);
dp[0] = 1;
for (int bit = 0; bit < 1<<n; bit++) {
int C = __builtin_popcount(bit);
for (int i = 0; i < m; i++) {
if(C == x[i]){
int cnt = 0;
for (int j = 0; j < n; j++) {
if((bit >> j) & 1){
if(j < y[i]){
cnt++;
}
}
}
if(cnt > z[i]){
dp[bit] = 0;
}
}
}
if(dp[bit] < 0) continue;
for (int i = 0; i < n; i++) {
if((bit >> i) & 1) continue;
int nbit = bit | (1 << i);
dp[nbit] += dp[bit];
}
}
cout << max(0ll, dp[(1 << n) - 1]) << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int64_t> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
int64_t maxA = 0;
int64_t lastC = 0;
for (int i = 0; i < n; i++) {
int64_t b;
cin >> b;
maxA = max(a[i], maxA);
lastC = max(b * maxA, lastC);
cout << lastC << endl;
}
return 0;
} | #include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
int T;
cin>>T;
for (int i=0; i<T; i++)
{
int N;
cin>>N;
string S[3];
for (string &s: S)
{
cin>>s;
s = s+s;
}
auto check = [&](string t)
{
for (int i=0; i<3; i++)
{
int p = 0;
for (int j=0; j<4*N && p<2*N+1; j++)
if (S[i][j]==t[p])
p++;
if (p<2*N+1)
return false;
}
return true;
};
bool ok = false;
for (int j=0; j<2 && !ok; j++)
for (int k=0; k<2 && !ok; k++)
for (int l=0; l<2 && !ok; l++)
for (int k=0; k<3 && !ok; k++)
{
string s1 = string(N, j==0 ? '0' : '1');
string s2 = string(N, k==0 ? '0' : '1');
string s3 = string(1, l==0 ? '0' : '1');
string s;
if (k==0)
s = s3+s1+s2;
if (k==1)
s = s1+s3+s2;
if (k==2)
s = s1+s2+s3;
if (check(s))
{
cout<<s<<endl;
ok = true;
}
}
}
}
|
//BY: YASH JAIN, CF: BitSane
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
#define May_the_fork_be_with_you ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define rall(v) v.rbegin(),v.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, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define deb(x) cout<<#x<<": "<<x<<endl;
#define debn(x) cout<<#x<<": "<<x<<" ";
#define MOD 1000000007
#define mod 998244353
#define INF 1e18
#define ll long long
#define f first
#define s second
#define pb push_back
#define eb emplace_back
#define endl "\n"
#define int long long
#define N 100005
#define sq(x) ((x)*(x))
typedef vector<int> vi;
ll gcd(ll a, ll b) {if (!b)return a; return gcd(b, a % b);}
ll power(ll x, ll y, ll p = INF) {ll res = 1; x %= p; while (y > 0) {if (y & 1)res = (res * x) % p; y = y >> 1; x = (x * x) % p;} return res;}
// Do Not use power when calculating powers of 2 (its inefficient)
bool comp(pair<int, int>& a, pair<int, int>& b) {
if (a.s != b.s) return a.s > b.s;
else return a.f > b.f;
}
void solve() {
int n, m, q; cin >> n >> m >> q;
vi x(m);
vector<pair<int, int>> wv(n);
forn(i, n) cin >> wv[i].f >> wv[i].s;
forn(i, m) cin >> x[i];
sort(all(wv), comp);
while (q--) {
int l, r; cin >> l >> r; l--; r--;
vi cur;
forn(i, m) {
if (i == l) i = r;
else cur.eb(x[i]);
}
sort(rall(cur));
int res = 0;
for (int i = 0; i < n; i++) {
int j = 0;
if (j >= (int)cur.size() || cur[j] < wv[i].f) continue;
while (j < (int)cur.size() && cur[j] >= wv[i].f) j++;
j--;
res += wv[i].s;
cur[j] = -INF;
sort(rall(cur));
}
cout << res << endl;
}
}
int32_t main() {
#ifndef ONLINE_JUDGE
// for geting input form input.txt
freopen("input.txt", "r", stdin);
// for wrting output to output.txt
freopen("output.txt", "w", stdout);
#endif
May_the_fork_be_with_you
int t = 1;
int x = 1;
// cin >> t;
cout << fixed << setprecision(12);
while (t--) {
// cout << "Case #" << x << ": ";
solve();
x++;
}
#ifndef ONLINE_JUDGE
cerr << "Time Taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
#endif
} |
//#include <atcoder/maxflow.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#include <memory>
#include <iostream>
#include <map>
#include <list>
#include <set>
#include <algorithm>
#include <vector>
#include <sstream>
#include <string>
#include <functional>
#include <queue>
#include <deque>
#include <stack>
#include <limits>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <fstream>
#include <iterator>
#include <random>
#include <chrono>
#include <complex>
#include <thread>
#include <bitset>
#include <assert.h>
#define forr(i,start,count) for (int i = (start); i < (start)+(count); ++i)
#define set_map_includes(set, elt) (set.find((elt)) != set.end())
#define readint(i) int i; cin >> i
#define readll(i) ll i; cin >> i
#define readdouble(i) double i; cin >> i
#define readstring(s) string s; cin >> s
typedef long long ll;
//using namespace __gnu_pbds;
//using namespace atcoder;
using namespace std;
ll modd = (1000LL * 1000LL * 1000LL + 7LL);
//ll modd = 998244353;
long long gcd(long long a, long long b) {
if (a == 0) { return b; }
return gcd(b % a, a);
};
ll coprimesets(const vector<ll>& nos) {
vector<ll> a;
ll ret = 0;
forr(i,0,1<<nos.size()) {
int pp = i;
int ct = 0;
a.resize(0);
while (pp>0) {
if (pp%2==1) { a.push_back(nos[ct]); }
pp/=2;
++ct;
}
bool is_ok = true;
forr(j,0,a.size()) {
for(int k = j+1; k < a.size(); ++k) {
is_ok &= (gcd(a[j], a[k])==1);
}
}
ret += is_ok;
}
return ret;
}
int main(int argc, char *argv[]) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.precision(17);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
uniform_int_distribution<int> rand_gen(0, modd/10); // rand_gen(rng) gets the rand no
// readint(test_cases);
int test_cases = 1;
forr(t, 1, test_cases) {
readint(n); readint(m); readint(q);
vector<pair<int,int>> bagg;
forr(i,0,n) {
readint(ww); readint(vv);
bagg.push_back(make_pair(vv,ww));
}
sort(bagg.begin(), bagg.end());
reverse(bagg.begin(), bagg.end());
vector<int> box;
forr(i,0,m) {
readint(bb);
box.push_back(bb);
}
forr(i,0,q) {
readint(l); readint(r); --l; --r;
multiset<int> box_av;
forr(i,0,m) {
if ((i<l) || (i>r)) { box_av.insert(box[i]); }
}
int ret = 0;
for(auto x : bagg) {
auto it = box_av.lower_bound(x.second);
if (it!=box_av.end()) {
ret += x.first;
box_av.erase(it);
}
}
cout << ret << endl;
}
}
return 0;
}
|
#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define forn(i,n) for(int i=0;i<n;i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define sz(a) (int)a.size()
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define GR(a,n,m) vector<vector<int>> a(n, vector<int>(m, 0));
struct Point
{
double x;
double y;
};
bool onSegment(Point p, Point q, Point r)
{
if (q.x <= max(p.x, r.x) && q.x >= min(p.x, r.x) &&
q.y <= max(p.y, r.y) && q.y >= min(p.y, r.y))
return true;
return false;
}
int orientation(Point p, Point q, Point r)
{
double val = (q.y - p.y) * (r.x - q.x) -
(q.x - p.x) * (r.y - q.y);
if (val == 0) return 0;
return (val > 0)? 1: 2;
}
bool doIntersect(Point p1, Point q1, Point p2, Point q2)
{
int o1 = orientation(p1, q1, p2);
int o2 = orientation(p1, q1, q2);
int o3 = orientation(p2, q2, p1);
int o4 = orientation(p2, q2, q1);
if (o1 != o2 && o3 != o4)
return true;
if (o1 == 0 && onSegment(p1, p2, q1)) return true;
if (o2 == 0 && onSegment(p1, q2, q1)) return true;
if (o3 == 0 && onSegment(p2, p1, q2)) return true;
if (o4 == 0 && onSegment(p2, q1, q2)) return true;
return false;
}
void solve()
{
ll n, d, h;
cin >> n >> d >> h;
vector<int> a(n),b(n);
for(int i = 0;i < n;i++)
cin >> a[i] >> b[i];
double l = 0, r = 1000;
double ans = r;
for(int p = 0;p < 100;p++)
{
double mid = (double)(l + r) / 2.0;
bool ok = true;
Point A1 = {0, mid};
Point A2 = {d,h};
for(int i = 0;i < n;i++)
{
Point B1 = {a[i], 0};
Point B2 = {a[i], b[i]};
if(doIntersect(A1,A2,B1,B2)){
ok=false;
}
}
if(ok){
ans = mid;
r = mid;
}else{
l=mid;
}
}
cout << fixed << setprecision(16) << ans << "\n";
}
int32_t main()
{
fastio;
int t = 1;
//cin >> t;
for(int tt = 0;tt < t;tt++)
{
solve();
}
} | #include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
using ll=long long;
using ld=long double;
using pll=pair<ll, ll>;
//using mint = modint1000000007;
#define rep(i,n) for (ll i=0; i<n; ++i)
#define all(c) begin(c),end(c)
#define PI acos(-1)
#define oo 2e18
template<typename T1, typename T2>
bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}
template<typename T1, typename T2>
bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}
//priority_queue<ll, vector<ll>, greater<ll>> Q;
/*
*/
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
cout << fixed << setprecision(10);
ll N;
ld D, H;
cin >> N >> D >> H;
ld ans=0.0;
rep(i, N){
ld d, h;
cin >> d >> h;
// 下半分
ld tmp = h;
// 比率をひく
tmp -= (H-h)/(D-d) * d;
chmax(ans, tmp);
}
cout << ans << endl;
}
|
#include <iostream>
#include <string>
#include <utility>
#include <stack>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
#include <climits>
#include <set>
#include <cmath>
#include <numeric>
#include <cfloat>
#include <iomanip>
using namespace std;
int a[50][50];
const long long MOD = 998244353;
vector <int> edges[50];
long long fact[51];
bool done[50];
long long dfs(int cur){
done[cur] = true;
int ret = 1;
for(int i = 0; i < edges[cur].size(); i++){
if(!done[edges[cur][i]]){
ret += dfs(edges[cur][i]);
}
}
return ret;
}
int main(){
int N;
int K;
cin >> N >> K;
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
cin >> a[i][j];
}
}
fact[0] = 1;
for(int i = 0; i < N; i++){
fact[i + 1] = (fact[i] * (i + 1)) % MOD;
}
if(N == 1){
cout << 1 << endl;
return 0;
}
for(int i = 0; i < N - 1; i++){
for(int j = i + 1; j < N; j++){
bool swappable = true;
for(int k = 0; k < N; k++){
if(a[i][k] + a[j][k] > K){
swappable = false;
break;
}
}
if(swappable){
edges[i].push_back(j);
edges[j].push_back(i);
}
}
}
long long ans = 1;
for(int i = 0; i < N; i++){
if(!done[i]){
ans = (ans * fact[dfs(i)]) % MOD;
}
}
for(int i = 0; i < N; i++){
edges[i].clear();
done[i] = false;
}
for(int i = 0; i < N - 1; i++){
for(int j = i + 1; j < N; j++){
bool swappable = true;
for(int k = 0; k < N; k++){
if(a[k][i] + a[k][j] > K){
swappable = false;
break;
}
}
if(swappable){
edges[i].push_back(j);
edges[j].push_back(i);
}
}
}
for(int i = 0; i < N; i++){
if(!done[i]){
ans = (ans * fact[dfs(i)]) % MOD;
}
}
cout << ans << endl;
return 0;
}
| #include<bits/stdc++.h>
//#include<atcoder/all>
using namespace std;
//using namespace atcoder;
using ll = long long;
using Graph = vector<vector<int>>;
#define rep(i,m,n) for(int (i)=(m);(i)<(n);++(i))
#define rrep(i,m,n) for(int (i)=(n)-1;(i)>=(m);--(i))
#define all(x) (x).begin(),(x).end()
#define out(y,x,h,w) (y)<0||(x)<0||(y)>=(h)||(x)>=(w)
constexpr ll INF = (ll)1e9;
constexpr ll mod = 998244353;
constexpr double PI = 3.1415926535897932;
template<class T>inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template<class T>inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
inline void init() {
cin.tie(nullptr); cout.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
}
struct mint {
ll x; // typedef long long ll;
mint(ll x = 0) :x((x% mod + mod) % mod) {}
mint operator-() const { return mint(-x); }
mint& operator+=(const mint a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint a) {
if ((x += mod - a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint a) const {
mint res(*this);
return res += a;
}
mint operator-(const mint a) const {
mint res(*this);
return res -= a;
}
mint operator*(const mint a) const {
mint res(*this);
return res *= a;
}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1) a *= *this;
return a;
}
// for prime mod
mint inv() const {
return pow(mod - 2);
}
mint& operator/=(const mint a) {
return (*this) *= a.inv();
}
mint operator/(const mint a) const {
mint res(*this);
return res /= a;
}
};
struct UnionFind {
vector<int>d;
UnionFind(int N) :d(N, -1) {}
int find(int x) {
if (d[x] < 0)return x;
return d[x] = find(d[x]);
}
bool unite(int x, int y) {
x = find(x); y = find(y);
if (x == y)return false;
if (d[x] > d[y])swap(x, y);
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) {
return find(x) == find(y);
}
int size(int x) {
return -d[find(x)];
}
};
mint func(int a) {
if (a == 0)return 1;
return (mint)a * func(a - 1);
}
int main() {
init();
int N, K; cin >> N >> K;
vector<vector<int>>mat(N, vector<int>(N));
rep(i, 0, N)rep(j, 0, N)cin >> mat[i][j];
UnionFind uf1(N), uf2(N);
rep(i, 0, N) {
rep(j, i + 1, N) {
bool a = true, b = true;
rep(k, 0, N) {
if (mat[k][i] + mat[k][j] > K)a = false;
if (mat[i][k] + mat[j][k] > K)b = false;
}
if (a)uf1.unite(i, j);
if (b)uf2.unite(i, j);
}
}
mint a = 1, b = 1;
rep(i, 0, N) {
if (uf1.d[i] < 0)a *= func(-uf1.d[i]);
if (uf2.d[i] < 0)b *= func(-uf2.d[i]);
}
mint ans = a * b;
cout << ans.x << "\n";
return 0;
} |
//#pragma GCC optimize ("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define pb push_back
#define F first
#define S second
#define ins insert
#define mp make_pair
#define fo(i, n1, n, x) for(int i = n1; i <= n; i += x)
#define foo(i, n, n1, x) for(int i = n; i >= n1; i -= x)
#define bit __builtin_popcount
#define md (l + ((r - l) / 2))
#define all(x) x.begin(),x.end()
#define eb emplace_back
#define ub upper_bound
#define lb lower_bound
#define ios ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
using namespace std;
//using namespace __gnu_pbds;
//using namespace __gnu_cxx;
using ll = long long;
//#define int ll
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<int>;
const int N = 2e5 + 11, mod = 1e9 + 7, mod2 = 998244353;
const int MAX = 1e5 + 11;
const int INF1 = 2e9 + 11;
const ll INF2 = 6e18 + 11;
const double INF3 = 1e8 + 11;
const int base = 500;
const int P = 31;
const int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};
const int dy[] = {0, 0, 1, -1, 1, -1, 1, -1};
const double EPS = 1e-4;
const double PI = acos(-1.0);
//template<typename T> using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int l, int r) { return uniform_int_distribution<int> (l, r)(rng); }
int r, c, a[507][507], b[507][507], d[507][507][2];
main() {
file("paint");
ios;
cin >> r >> c;
fo(i, 1, r, 1) fo(j, 1, c - 1, 1) cin >> a[i][j];
fo(i, 1, r - 1, 1) fo(j, 1, c, 1) cin >> b[i][j];
fo(i, 1, r, 1) fo(j, 1, c, 1) d[i][j][0] = d[i][j][1] = INF1;
d[1][1][0] = 0;
priority_queue < pair <pii, pii> > q;
q.push(mp(mp(0, 0), mp(1, 1)));
function<bool(int, int)> valid = [&](int x, int y) { return (1 <= x and x <= r and 1 <= y and y <= c); };
while (!q.empty()) {
pair <pii, pii> v = q.top();
q.pop();
int nx, ny;
int t = v.F.S;
if (-v.F.F != d[v.S.F][v.S.S][t]) continue;
nx = v.S.F;
ny = v.S.S + 1;
if (valid(nx, ny) and d[nx][ny][0] > d[v.S.F][v.S.S][t] + a[v.S.F][v.S.S]) {
d[nx][ny][0] = d[v.S.F][v.S.S][t] + a[v.S.F][v.S.S];
q.push(mp(mp(-d[nx][ny][0], 0), mp(nx, ny)));
}
nx = v.S.F;
ny = v.S.S - 1;
if (valid(nx, ny) and d[nx][ny][0] > d[v.S.F][v.S.S][t] + a[v.S.F][v.S.S - 1]) {
d[nx][ny][0] = d[v.S.F][v.S.S][t] + a[v.S.F][v.S.S - 1];
q.push(mp(mp(-d[nx][ny][0], 0), mp(nx, ny)));
}
nx = v.S.F + 1;
ny = v.S.S;
if (valid(nx, ny) and d[nx][ny][0] > d[v.S.F][v.S.S][t] + b[v.S.F][v.S.S]) {
d[nx][ny][0] = d[v.S.F][v.S.S][t] + b[v.S.F][v.S.S];
q.push(mp(mp(-d[nx][ny][0], 0), mp(nx, ny)));
}
if (t) {
nx = v.S.F - 1;
ny = v.S.S;
if (valid(nx, ny) and d[nx][ny][t] > d[v.S.F][v.S.S][t] + 1) {
d[nx][ny][t] = d[v.S.F][v.S.S][t] + 1;
q.push(mp(mp(-d[nx][ny][t], t), mp(nx, ny)));
}
} else {
nx = v.S.F;
ny = v.S.S;
t ^= 1;
if (valid(nx, ny) and d[nx][ny][t] > d[v.S.F][v.S.S][1-t] + 1) {
d[nx][ny][t] = d[v.S.F][v.S.S][1-t] + 1;
q.push(mp(mp(-d[nx][ny][t], t), mp(nx, ny)));
}
}
}
cout << min(d[r][c][0], d[r][c][1]);
return 0;
}
/**
*/
| #include <bits/stdc++.h>
#define eprintf(args...) fprintf(stderr, args)
#define rep(i, n) for (int i = 0; i < (int)(n); ++ i)
const int mxn = 2e5 + 5;
int n, a[mxn], b[mxn];
std::map <int, std::vector <int> > oa, ob;
int s[mxn];
void update(int x, int v) {
for (++ x; x < mxn; x += x & -x) s[x] += v;
}
int query(int x) {
int ans = 0;
for (++ x; x; x -= x & -x) ans += s[x];
return ans;
}
int frm[mxn];
int main() {
scanf("%d", &n);
rep(i, n) scanf("%d", &a[i]), a[i] += i;
rep(i, n) scanf("%d", &b[i]), b[i] += i;
rep(i, n) oa[a[i]].push_back(i);
rep(i, n) ob[b[i]].push_back(i);
for (auto x : oa) {
std::vector <int> va = x.second, vb = ob[x.first];
if (va.size() != vb.size()) return printf("%d\n", -1), 0;
rep(i, va.size()) frm[vb[i]] = va[i];
}
rep(i, n) update(i, 1);
long long ans = 0;
rep(i, n) ans += query(frm[i] - 1), update(frm[i], -1);
printf("%lld\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1050;
vector<int> g[N][26];
bool vis[N][N];
int dp[N][N];
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, m;
cin >> n >> m;
for (int i = 0; i < m; i++) {
int a, b;
char c;
cin >> a >> b >> c;
g[a][c - 'a'].push_back(b);
g[b][c - 'a'].push_back(a);
}
fill(&dp[0][0], &dp[n + 1][0], LLONG_MAX);
queue<pair<int, int> > bfs;
for (int i = 1; i <= n; i++) {
bfs.emplace(i, i);
vis[i][i] = true;
dp[i][i] = 0;
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j < 26; j++) {
for (auto to: g[i][j]) {
if (vis[i][to])
continue;
bfs.emplace(i, to);
vis[i][to] = vis[to][i] = true;
dp[i][to] = dp[to][i] = 1;
}
}
}
while (!bfs.empty()) {
pair<int, int> nodes = bfs.front();
bfs.pop();
int u = nodes.first;
int v = nodes.second;
for (int j = 0; j < 26; j++) {
for (auto nu: g[u][j]) {
for (auto nv: g[v][j]) {
if (vis[nu][nv])
continue;
vis[nu][nv] = vis[nv][nu] = true;
dp[nu][nv] = dp[nv][nu] = dp[u][v] + 2;
bfs.emplace(nu, nv);
}
}
}
}
cout << (dp[1][n] == LLONG_MAX ? -1 : dp[1][n]);
}
| #pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
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 = 1000000007;
const char nl = '\n';
const int MX = 150001; //check the limits, dummy
set<pii> visited;
void solve(int t) {
int n, m;
cin>>n >> m;
map<int, map<int, char>> adj;
F0R(i,m) {
int u,v;
char c;
cin>>u>>v>>c;
u--, v--;
adj[u][v]=c;
adj[v][u]=c;
}
// negative length of string
priority_queue<pair<ll, pii>> pq;
pq.push(mp(0,mp(0,n-1)));
int u,v;
set<int> pos;
while(!pq.empty()) {
auto cur = pq.top();
pq.pop();
ll l = -cur.f;
tie(u,v) = cur.s;
// cout << l <<" "<<st<<" "<<u<<" "<<v<<nl;
if(visited.find({u,v})!=visited.end()) {
// cout << "hi\n";
continue;
}
visited.ins({u,v});
if(u==v) {
pos.ins(2*l);
// cout << 2*l<<nl;
// return;
}
if(adj[u].find(v)!=adj[u].end()) {
pos.ins(2*l+1);
// cout << 2*l+1<<nl;
// return;
}
for(auto au : adj[u]) {
for(auto av : adj[v]) {
if(au.s==av.s) {
pq.push(mp(-l-1,mp(au.f,av.f)));
}
}
}
}
if(!pos.empty()) cout << *(pos.begin())<<nl;
else
cout << "-1\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout.precision(20);
cout << fixed;
int T = 1;
F0R(i,T) {
solve(i+1);
}
}
// read the question correctly (ll vs int)
// template by bqi343 |