code_file1
stringlengths 87
4k
| code_file2
stringlengths 82
4k
|
---|---|
#include <bits/stdc++.h>
using namespace std;
///* input vector */
//template <typename T>
//istream& operator >>(istream& in, vector<T>& v) {
// for (size_t i=0; i<v.size(); ++i) {
// in >> v[i];
// }
// return in;
//}
///* output vector */
//template <typename T>
//ostream& operator <<(ostream& out, vector<T>& v) {
// string sep = "";
// for (size_t i=0; i<v.size(); ++i, sep=" ") {
// out << sep << v[i];
// }
// return out;
//}
/* shortcuts */
#define all(v) begin(v),end(v)
#define rep(i,n) for(int i=0; i<n; ++i)
#define endl '\n'
/* data types shortened */
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
int main()
{
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
freopen("Input.txt", "r", stdin);
//freopen("Output.txt", "w", stdout);
//freopen("Eroor.txt", "w", stderr);
#endif // ONLINE_JUDGE
int n, q; cin >> n >> q;
vector<ull> a(n);
rep(i,n) cin >> a[i];
vector<ull> pre(n); // pre_sum: sum of nbr of included nbrs so far, index: zone
rep(i,n) pre[i] = a[i]-i-1;
while (q--) {
ull k; cin >> k;
int zone = lower_bound(all(pre), k) - pre.begin();
ull ans;
if (zone == 0) ans = k;
else ans = a[zone-1]+(k-pre[zone-1]);
cout << ans << endl;
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
int main()
{
int a,c,u;
cin>>a>>c;
for(int i=1;i<=a;i++)
{
cin>>u;
if(u!=c)
{
cout<<u<<" ";
}
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i=0;i<(n);i++)
void chmax(ll& a, ll b) {if (b>a) a=b;}
int main() {
int N;
cin >> N;
vector<int> a(N), b(N);
rep(i,N) cin >> a[i];
rep(i,N) cin >> b[i];
ll ans = 0, maxa = 0;
rep(i,N) {
chmax(maxa, a[i]);
chmax(ans, maxa*b[i]);
cout << ans << endl;
}
}
| #include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); i++)
using namespace std;
int main() {
long long N;
cin >> N;
vector<long long> A(N);
map<long long,long long> mpA;
rep(i,N) {
cin >> A[i];
mpA[A[i]]++;
}
vector<pair<long long,long long>> B(N);
rep(i,N) {
B[i].first = A[i];
B[i].second = mpA[A[i]];
}
sort(B.begin(), B.end());
long long ans = 0LL;
ans = N * (N-1LL) / 2LL;
rep(i,N) {
if(i>0 && B[i].first==B[i-1].first) continue;
else if(B[i].second>=2) {
ans -= B[i].second * (B[i].second-1LL) / 2LL;
}
}
cout << ans << endl;
} |
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <unordered_map>
#include <vector>
using namespace std;
#define MP make_pair
#define PB push_back
#define REP(i, L, R) for (int i = L; i < (R); ++i)
#define SZ(x) (int)x.size()
using ll = long long;
using ld = long double;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
template <typename T>
bool ckmin(T& a, const T& b) {
return b < a ? a = b, 1 : 0;
}
template <typename T>
bool ckmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0;
}
ll p[6] = {1, 10, 100, 1000, 10000, 100000};
ll score(const string& s, int d) {
int f[10];
fill(f, f + 10, 0);
++f[d];
for (int i = 0; i < s.length() - 1; ++i) {
f[s[i] - '0']++;
}
ll sum = 0;
for (int i = 1; i <= 9; ++i) {
sum += (ll)i * p[f[i]];
}
return sum;
}
void solve() {
int k;
string s, t;
cin >> k;
cin >> s;
cin >> t;
int f[10];
fill(f, f + 10, 0);
for (int i = 0; i < s.length() - 1; ++i) {
f[s[i] - '0']++;
}
for (int i = 0; i < t.length() - 1; ++i) {
f[t[i] - '0']++;
}
// map<pi, bool> w;
unordered_map<int, bool> w;
for (int i = 1; i <= 9; ++i) {
for (int j = 1; j <= 9; ++j) {
// (i,j) -> i*10+j
w[i * 10 + j] = (score(s, i) > score(t, j));
}
}
ll cnt = 0;
for (int i = 1; i <= 9; ++i) {
ll remi = k - f[i];
if (remi <= 0) {
continue;
}
for (int j = 1; j <= 9; ++j) {
ll remj = k - f[j] - (i == j);
if (remj <= 0) {
continue;
}
if (w[i * 10 + j]) {
cnt += remi * remj;
}
}
}
ld total = ((ld)9 * k - 8) * ((ld)9 * k - 9);
cout << fixed << setprecision(14) << (ld)cnt / total << endl;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int T = 1;
while (T--) {
solve();
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
using ll = int64_t;
#define rep(i,n) for(int i=0; i<(n); i++)
#define ALL(n) n.begin(), n.end()
int main(){
ll a, b, k;
cin >> a >> b >> k;
// パスカルの三角形
vector<vector<ll>> p(60, vector<ll>(60));
p[0][0] = 1;
for(int i=1; i<60; i++)for(int j=0; j<60; j++){
if(j == 0) p[i][j] = p[i-1][j];
else p[i][j] = p[i-1][j-1] + p[i-1][j];
}
/**
rep(i,10){
rep(j,10){
if(p[i][j] == 0) break;
else cout << p[i][j] << " ";
}
cout << endl;
}
**/
string ans = "";
// 先頭がaの時、何通りあるか
for(int i=a+b; 0<a+b; i--){
ll num = p[a+b-1][b];
//cout << num << endl;
// 次の文字がbのとき
if(k > num){ // 次の文字がbのとき
ans += 'b';
k -= num;
b--;
}else{ // 次の文字がaのとき
ans += 'a';
a--;
}
if(a == 0){
rep(j,b) ans += 'b';
break;
}else if(b == 0){
rep(j,a) ans += 'a';
break;
}
}
cout << ans << endl;
} |
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <utility>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <limits>
#include <stack>
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
using namespace std;
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> inline int sz(T &a) { return a.size(); }
using ll = long long; using ld = long double;
using pi = pair<int,int>; using pl = pair<ll,ll>;
using vi = vector<int>; using vvi = vector<vi>;
using vl = vector<ll>; using vvl = vector<vl>;
const int inf = numeric_limits<int>::max();
const ll infll = numeric_limits<ll>::max();
int main()
{
int n; cin >> n;
vi a(2*n);
rep(i,2*n) cin >> a[i];
vector<pi> p(2*n);
rep(i,2*n) p[i] = {a[i], i};
sort(p.begin(),p.end());
vi b(2*n, 0);
rep(i,n) {
b[p[i].second] = 1;
}
vector<char> res(2*n);
stack<pi> sta;
rep(i,2*n) {
if(!sta.empty()) {
if(sta.top().first != b[i]) {
res[sta.top().second] = '(';
res[i] = ')';
sta.pop();
}
else {
sta.push({b[i], i});
}
}
else {
sta.push({b[i], i});
}
}
rep(i,2*n) cout << res[i];
cout << "\n";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll,ll>;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rep2(i,m,n) for(int (i)=(m);(i)<(n);(i)++)
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
const ll INF = 1LL<<60;//1152921504606846976
const int mod = 1000000007;
vector<vector<ll>> G;
vector<bool> vis;
vector<ll> dist;
ll dmax, farnode;
void dfs(ll r, ll d){
vis[r]=true;
dist[r]=d;
if(d>dmax){
dmax=d;
farnode=r;
}
for(ll c:G[r])if(!vis[c])dfs(c,d+1);
}
ll u,v;
vector<bool> st_has_v;
void dfs_findv(ll r){
vis[r]=true;
if(r==v)st_has_v[r]=true;
for(ll c:G[r])if(!vis[c]){
dfs_findv(c);
if(st_has_v[c])st_has_v[r]=true;
}
}
ll now;
vector<ll> label;
void dfs_labelling(ll r){
vis[r]=true;
label[r]=now;
ll hasv = -1;
for(ll c:G[r])if(!vis[c]){
if(st_has_v[c]){hasv=c;continue;}
now++;
dfs_labelling(c);
now++;
}
if(hasv!=-1){
now++;
dfs_labelling(hasv);
now++;
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
ll n; cin>>n;
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);
}
//直径と両端u,vを求める
vis=vector<bool>(n,false);
dist=vector<ll>(n,INF);
dmax = 0;
dfs(0,0);
u = farnode;
vis=vector<bool>(n,false);
dist=vector<ll>(n,INF);
dmax=0;
dfs(u,0);
v = farnode;
ll R = dmax;
//uからDFS、subtreeにvがあるか判定
vis=vector<bool>(n,false);
st_has_v = vector<bool>(n,false);
dfs_findv(u);
//uからDFS、vがあるsubtreeは最後に回す
//貪欲にラベルをつけていく
vis=vector<bool>(n,false);
label=vector<ll>(n);
now=1;
dfs_labelling(u);
for(ll x:label)cout<<x<<" ";
return 0;
} |
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using pii = pair <int, int>;
using pll = pair <ll, ll>;
#define FIO() ios_base::sync_with_stdio(0);cin.tie(NULL);
const ll inf = 9e18;
int main() {
FIO();
string atc = "atcoder";
int tc; cin >> tc;
while (tc--) {
string s; cin >> s;
ll ans = inf;
ll temp = 0;
int m = min(atc.size(), s.size());
int n = s.size();
bool ok = true;
for (int i = 0; i < m; i++) {
if (s[i] > atc[i]) {
ans = min(ans, temp);
break;
}
for (int j = i + 1; j < n; j++) {
if (s[j] > atc[i]) {
ans = min(ans, temp + j - i);
break;
}
}
bool fnd = false;
for (int j = i; j < n; j++) {
if (s[j] != atc[i]) continue;
fnd = true;
while (s[i] != atc[i]) {
swap(s[j], s[j - 1]);
j--;
temp++;
}
break;
}
if (!fnd) {
ok = false;
break;
}
}
if (ok and m < s.size()) ans = min(ans, temp);
if (ans == inf) ans = -1;
cout << ans << '\n';
}
}
| #include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <functional>
#include <bitset>
using namespace std;
using lint = long long int;
long long int INF = 1001001001001001LL;
int inf = 1000000007;
long long int MOD = 1000000007LL;
double PI = 3.1415926535897932;
template<typename T1,typename T2>inline void chmin(T1 &a,const T2 &b){if(a>b) a=b;}
template<typename T1,typename T2>inline void chmax(T1 &a,const T2 &b){if(a<b) a=b;}
#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
int main() {
vector<int> cnt(10, 0);
string s; cin >> s;
for (auto c : s) {
cnt[c - '0']++;
}
if (s.size() <= 5) {
sort(ALL(s));
do {
int num = stoi(s);
if (num % 8 == 0) {
cout << "Yes" << endl;
return 0;
}
} while (next_permutation(ALL(s)));
cout << "No" << endl;
return 0;
}
for (int x = 104; x <= 999; x += 8) {
vector<int> nums(10, 0);
// cerr << "x = " << x << endl;
int y = x;
while (y) {
// cerr << y % 10 << endl;
nums[y % 10]++;
y /= 10;
}
bool ok = true;
for (int i = 0; i <= 9; i++) {
if (cnt[i] < nums[i]) ok = false;
}
if (ok) {
cerr << "x = " << x << endl;
cout << "Yes" << endl;
return 0;
}
}
cout << "No" << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define fore(i, a) for (auto &i : a)
using ll = long long;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
const ll INF = 1LL << 60;
const ll mod = 1E9 + 7;
ll gcd(ll a, ll b) {
if (a < b) {
ll temp = a;
a = b;
b = temp;
}
if (b == 0) return a;
return gcd(b, a % b);
}
int main() {
int N;
cin >> N;
vector<int> a = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47};
vector<int> x(N);
rep(i, N) cin >> x[i];
ll ans = INF;
for (int bit = 1; bit < (1 << 15); bit++) {
ll temp = 1;
rep(i, 15) {
if (bit & (1 << i)) temp *= a[i];
}
rep(i, N) {
if (gcd(temp, x[i]) == 1) {
temp = INF;
break;
}
}
ans = min(temp, ans);
}
cout << ans << endl;
return 0;
} | #include<bits/stdc++.h>
#define ll long long int
#define pi acos(-1)
#define FAST() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
using namespace std;
ll mod=1000000007;
bool sortBysec(pair<ll,ll>a,pair<ll,ll>b)
{
return a.second>b.second;
}
bool prime[2000010];
vector<ll>vv;
void SieveOfEratosthenes(ll n)
{
memset(prime, true, sizeof(prime));
for (ll p=3; p*p<=n; p+=2)
{
if (prime[p] == true)
{
for (ll i=p*p; i<=n; i +=(2*p))
prime[i] = false;
}
}
}
ll binExp(ll a,ll n)
{
ll res=1;
while(n)
{
if(n%2==1)
{
res=(res*a)%mod;
n--;
}
else
{
a=(a*a)%mod;
n=n/2;
}
}
return res;
}
ll ncr(ll n,ll r)
{
if(r>n-r)
{
r=n-r;
}
ll i,j,ans=1;
for(i=n,j=1; j<=r; j++,i--)
{
ans=((ans*i)%mod);
ans=(ans*binExp(j,mod-2))%mod;
}
return ans;
}
ll power(ll n,ll m)
{
ll ans=1;
while(m)
{
ans*=n;
m--;
}
return ans;
}
ll digsum(ll n)
{
ll sum=0;
while(n)
{
sum+=n%10;
n=n/10;
}
return sum;
}
ll fact(ll n)
{
ll ans=1;
for (ll i=1; i<=n; i++)
{
ans*=i;
}
return ans;
}
ll npr(ll n,ll r)
{
ll ans=1;
for(ll i=n,j=1; j<=r; j++,i--)
{
ans=((ans%mod)*(i%mod))%mod;
}
return ans;
}
int main()
{
FAST();
ll t=1,i,m,n,j,k,x,y,p,c,d,q,a,b;
//cin>>t;
while(t--)
{
cin>>n;
vector<ll>v,u,w;
for(i=0;i<n;i++)
{
cin>>k;
v.push_back(k);
}
map<ll,ll>mp;
for(i=2;i<=25;i++)
{
mp[i*2]=1;
}
for(i=3;i<=50;i+=2)
{
if(mp[i]==0)
{
for(j=i*i;j<=50;j+=2*i)
{
mp[j]=1;
}
}
}
for(i=2;i<=50;i++)
{
if(mp[i]==0)
{
u.push_back(i);
// cout<<i<<" ";
}
}
// cout<<endl;
map<ll,ll>mcp;
for(i=0;i<u.size();i++)
{
ll cc=w.size();
for(j=0;j<cc;j++)
{
if(mcp[w[j]*u[i]]==0)
{
w.push_back(w[j]*u[i]);
mcp[w[j]*u[i]]=1;
}
}
if(mcp[u[i]]==0)
{
w.push_back(u[i]);
mcp[u[i]]=1;
}
}
//
sort(w.begin(),w.end());
ll f,ans;
for(i=0;i<w.size();i++)
{
f=0;
for(j=0;j<v.size();j++)
{
if(__gcd(v[j],w[i])<=1)
{
f=1;
break;
}
//cout<<__gcd(v[j],w[i])<<" ";
}
// cout<<endl;
if(f==0)
{
ans=w[i];
break;
}
}
cout<<ans<<endl;
}
return 0;
}
/*
❀(◕‿◕)❀
*/
|
#include <iostream>
#include <vector>
//#include <string>
#include <algorithm>
//#include <math.h>
//#include <queue>
//#include <stack>
//#include <iomanip>
// sometimes used
//#include <set>
//#include <map>
//#include <numeric>
//#include <list>
//#include <deque>
//#include <unordered_map>
typedef long long LL;
//typedef long double LD;
using namespace std;
//#define MOD 1000000007
//#define MOD 998244353
//#define MAX 100100
//#define NIL -1
//#define INF 1000000000000000000
//const LL SEG_VAL = 1 << 19;
LL modinv(LL a, LL m) {
LL b = m, u = 1, v = 0;
while (b) {
LL t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
u %= m;
if (u < 0){
u += m;
}
return u;
}
LL gcd(LL a, LL b){
LL tmp;
if(a<b){
tmp = a;
a = b;
b = tmp;
}
tmp = a%b;
while(tmp!=0){
a = b;
b = tmp;
tmp = a%b;
}
return b;
}
int main(){
LL t;
cin >> t;
vector<LL> n(t);
vector<LL> s(t);
vector<LL> k(t);
for(LL i=0; i<t; i++){
cin >> n[i] >> s[i] >> k[i];
}
for(LL i=0; i<t; i++){
s[i]=n[i]-s[i];
LL d=gcd(n[i], gcd(s[i], k[i]));
n[i]/=d;
s[i]/=d;
k[i]/=d;
if(gcd(k[i],n[i])!=1){
cout << -1 << endl;
}else{
cout << (s[i]*modinv(k[i], n[i]))%n[i] << endl;
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 10;
#define ll long long
#define lowbit(i) ((i) & -(i))
ll exgcd(ll a, ll b, ll &x, ll &y)
{
if (!b)
{
x = 1, y = 0;
return a;
}
ll gd = exgcd(b, a % b, y, x);
y -= a / b * x;
return gd;
}
void solve()
{
ll N, S, K;
cin >> N >> S >> K;
if (S % __gcd(K, N) == 0)
{
ll x, y;
exgcd(N,K, x, y);
ll o=S / __gcd(K, N);
x*=o,y*=o;
N/=__gcd(K, N);
y=-y;
y=(y%N+N)%N;
cout <<y<<endl;
}
else cout << -1 << endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T ;
cin >>T;
while (T--)
{
solve();
}
} |
#include <chrono>
#include <thread>
#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define T top()
#define E end()
#define PS push
#define Q queue
#define PP pop()
#define L0 (LL)0
#define V vector
#define ST stack
#define FI first
#define MAX 4e18
#define MIN -MAX
#define DQ deque
#define SZ size()
#define C clear()
#define B begin()
#define F front()
#define SE second
#define DL double
#define G getline
#define IN insert
#define endl "\n"
#define EM empty()
#define cnn continue
#define MD 1000000007
#define PSB push_back
#define PI acos(-1.00)
#define PPB pop_back()
#define PSF push_front
#define ub upper_bound
#define lb lower_bound
#define PPF pop_front()
#define CF cout.flush()
#define line cout<<endl
#define LL long long int
#define one cout<<1<<endl
#define zer cout<<0<<endl
#define PQ priority_queue
#define kth find_by_order
#define nil cout<<-1<<endl
#define N cout<<"NO"<<endl
#define NN cout<<"No"<<endl
#define Y cout<<"YES"<<endl
#define fndidx order_of_key
#define YY cout<<"Yes"<<endl
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#define MAU cout<<"Case "<<mau<<": "
#define SP(pre) fixed<<setprecision(pre)
#pragma GCC optimization ("unroll-loops")
#define MMAU cout<<"Case "<<mau<<":"<<endl
#define __lcm(A1,A2) (A1/(__gcd(A1,A2)))*A2
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define file freopen("input.txt","r",stdin);freopen("output.txt","w",stdout)
#define UOSMOY v.C;w.C;ww.C;uu.C;uo.C;vo.C;vu.C;ab.C;bc.C;cc.C;uuu.C;a.C;b.C;c.C;u.C
typedef tree<LL, null_type, less<LL>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
#define remembermeforcenturies using namespace std::chrono_literals;std::this_thread::sleep_for(-4611686018427387904ms);
V<LL>v,w,ab,bc;
set<LL>uo;
map<LL,LL>ww,cc,u;
V<pair<LL,LL> >uu,su;
map<LL,V<LL> >uuu;
map<pair<LL,LL>,LL> vo,vu;
map<LL,pair<LL,pair<LL,LL > > > vs;
priority_queue<LL,V<LL>,greater<LL> > moy;
LL dx[]= {-1,0,1,0,1,1,-1,-1};
LL dy[]= {0,1,0,-1,-1,1,1,-1};
LL dxx[]= {2,1,-1,-2,-2,-1,1,2};
LL dyy[]= {1,2,2,1,-1,-2,-2,-1};
LL dp[101][10001];
LL ar[10][10];
int main()
{
// BRR;
// file;
FAST;
char ch;
string a,b,c;
DL x,y,d,e,f,g,h;
LL n,i,j,k,p,q,o,l,s,tt=1,m,t,z,aa,r=0;
// cin>>tt;
for(LL mau=1; mau<=tt; mau++)
{
UOSMOY;
ordered_set os;
remembermeforcenturies;
cin>>n;
k=0;
l=0;
for(i=1; ;i++)
{
l+=i;
k++;
if(l>=n)
{
cout<<k<<endl;
break;
}
}
}
return 0;
//IN
//AL
}
| /*************************************************************************
> File Name: 1.cpp
> Author: Knowledge_llz
> Mail: 925538513@qq.com
> Blog: https://blog.csdn.net/Pig_cfbsl
> Created Time: 2020/10/11 22:45:26
************************************************************************/
#include<bits/stdc++.h>
#define For(i,a,b) for(register int i=(a);i<=(b);++i)
#define pb push_back
#define pr pair<int,int>
#define fi first
#define se second
#define LL long long
#define mk(a,b) make_pair(a,b)
using namespace std;
int read(){
char x=getchar(); int u=0,fg=0;
while(!isdigit(x)){ if(x=='-') fg=1; x=getchar(); }
while(isdigit(x)){ u=(u<<3)+(u<<1)+(x^48); x=getchar(); }
return fg?-u:u;
}
const int maxx=1e5+10;
int n,m,sum=0,cnt=0,mx,ans=1e9;
int w[10],tmp[10],s[10],dis[10],v[maxx],l[maxx];
int p[10]={0,1,2,3,4,5,6,7,8};
pr a[maxx];
bool cmp(pr x,pr y){
if(x.fi!=y.fi) return x.fi<y.fi;
return x.se>y.se;
}
int iso(int x){
x=lower_bound(v+1,v+cnt+1,x)-v-1;
return l[x];
}
int solve(){
s[0]=0;
For(i,1,n){ tmp[i]=w[p[i]]; s[i]=s[i-1]+tmp[i]; dis[i]=0;}
For(i,2,n) For(j,1,i-1){
dis[i]=max(dis[i],dis[j]+iso(s[i]-s[j-1]));
if(dis[i]>=ans) return dis[i];
}
return dis[n];
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
#endif
int u1,u2;
n=read(); m=read();
For(i,1,n){ w[i]=read(); mx=max(w[i],mx);}
For(i,1,m){
u1=read(); u2=read();
a[i]=mk(u2,u1);
if(u2<mx){ printf("-1\n"); return 0; }
}
sort(a+1,a+m+1,cmp);
v[++cnt]=a[1].fi; l[cnt]=a[1].se;
For(i,2,m)
if(a[i].se>l[cnt]){
v[++cnt]=a[i].fi;
l[cnt]=a[i].se;
}
do{
ans=min(ans,solve());
}while(next_permutation(p+1,p+n+1));
printf("%d\n",ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
const ll mod = 1000000007;
int main() {
int N; cin >> N;
vector<bool> memo(222222, false);
int ans = 0;
rep(i, N) {
int p; cin >> p;
memo[p] = true;
while(true){
if(!memo[ans]){
cout << ans << endl;
break;
}
ans++;
}
}
return 0;
} | #include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>
#include<unordered_map>
using namespace std;
#define max(a,b) (((a) > (b)) ? (a) : (b))
#define min(a,b) (((a) < (b)) ? (a) : (b))
const double pi = acos(-1);
const int N = 2e5 + 100;
const int mod = 1e9 + 7;
typedef long long ll;
template<typename T>
inline void read(T &x) {
x = 0;
int w = 1;
char ch = 0;
while (ch < '0' || ch > '9') {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch - 48);
ch = getchar();
}
x *= w;
}
int p[N],a[N],t[N]{0}, x = 0;
void qwq() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i){
scanf("%d",&p[i]);
t[p[i]] = 1;
while(t[x]) x++;
a[i] = x;
}
for(int i = 1; i <= n; ++i){
printf("%d\n", a[i]);
}
}
int main() {
int _ = 1;
//read(_);
while (_--) qwq();
return 0;
}
|
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <list>
#include <cstring>
#include <map>
#include <set>
#define ll long long
#define mod 1000000007
#define pi 3.141592653589
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
int main(){
int n;
cin>>n;
vector<int> v(n);
for(int i{};i<n;i++)
cin>>v.at(i);
vector<int> par(200001,-1);
vector<int> count(200001,0);
for(int i{};i<n;i++){
if(v.at(i)==v.at(n-1-i))
continue;
else{
int f=v.at(i);
int l=v.at(n-1-i);
if(par[f]==-1&&par[l]==-1){
par[f]=f;
par[l]=f;
}
else if(par[f]!=-1&&par[l]==-1)
par[l]=par[f];
else if(par[f]==-1&&par[l]!=-1)
par[f]=par[l];
else{
int j{f},k{l};
while(par[j]!=j){
par[j]=par[par[j]];
j=par[j];
}
while(par[k]!=k){
par[k]=par[par[k]];
k=par[k];
}
par[k]=par[j];
}
}
}
for(int i{};i<200001;i++){
if(par.at(i)==-1)
continue;
else{
int j=i;
while(par[j]!=j){
par[j]=par[par[j]];
j=par[j];
}
count.at(j)++;
}
}
int ans{};
for(int i{};i<200001;i++){
if(count.at(i)==0)
continue;
ans+=count.at(i)-1;
}
cout<<ans<<endl;
} | #include <stdio.h>
#include <map>
int x[100010],y[100010];
int type[100010];
std::map<int,int> check;
int main()
{
int a,b;
scanf("%d%d",&a,&b);
for(int i=1;i<=a;i++) scanf("%d",&x[i]);
for(int i=1;i<=a;i++) scanf("%d",&y[i]);
x[0] = 0, y[0] = 0;
x[a+1] = b+1, y[a+1] = b+1;
for(int i=1;i<=a;i++)
{
if(x[i]>y[i]) type[i] = 1; // <<
if(x[i]==y[i]) type[i] = 2; // --
if(x[i]<y[i]) type[i] = 3; // >>
}
for(int i=1;i<a;i++)
{
if(type[i]==3&&type[i+1]==1)
{
printf("-1");
return 0;
}
}
// x[j] +(i-j) == y[i]
long long int ans = 0;
check[0] = 0;
for(int i=1;i<=a;i++)
{
if(type[i]==1)
{
if(check.find(y[i]-i)==check.end())
{
printf("-1");
return 0;
}
if(y[i-1]==y[i]-1) ans++;
else
{
int k = check[y[i]-i];
ans += (i-k);
}
check[x[i]-i] = i;
}
else if(type[i]==2)
{
check.clear();
check[x[i]-i] = i;
}
}
check.clear();
//x[j] - (j-i) == y[i]
check[(b+1)-(a+1)] = a+1;
for(int i=a;i>=1;i--)
{
if(type[i]==3)
{
if(check.find(y[i]-i)==check.end())
{
printf("-1");
return 0;
}
if(y[i+1]==y[i]+1) ans++;
else
{
int k = check[y[i]-i];
ans += (k-i);
}
check[x[i]-i] = i;
}
else if(type[i]==2)
{
check.clear();
check[x[i]-i] = i;
}
}
printf("%lld",ans);
} |
//fake_name
#include<bits/stdc++.h>
#define int long long
using namespace std ;
#define F first
#define S second
int MOD = 998244353 ;
int mod = 1e9 + 7 ;
string s[2005] ;
int vis[2005][2005] ;
int n , m ;
int check( int i , int j )
{
if( i >= n || j >= m || i < 0 || j < 0 )
return 0 ;
if( vis[i][j] || s[i][j] == '#' )
return 0 ;
return 1 ;
}
signed main()
{
ios_base::sync_with_stdio(false) ;
cin.tie(0) ; cout.tie(0) ;
cin >> n >> m ;
for( int i = 0 ; i < n ; i++ )
cin >> s[i] ;
vector<pair<int,int> > adj[30] ;
pair<int,int> start , end ;
for( int i = 0 ; i < n ; i++ ){
for( int j = 0 ; j < m ; j++ ){
if( s[i][j] == '.' || s[i][j] == '#' )
continue ;
if( s[i][j] == 'S' )
start = { i , j } ;
else if( s[i][j] == 'G' )
end = { i , j } ;
else{
//cout << s[i][j] << endl ;
adj[s[i][j]-'a'].push_back( { i , j } ) ;
}
}
}
queue<pair<int,int> > q ;
q.push(start) ;
vis[start.F][start.S] = 1 ;
int h[26] = {0} ;
while( !q.empty() ){
pair<int,int> p = q.front() ;
q.pop() ;
int i = p.F , j = p.S ;
if( check( i+1 , j )){
vis[i+1][j] = vis[i][j]+1 ;
q.push( { i+1 , j } ) ;
}
if( check( i-1 , j ) ){
vis[i-1][j] = vis[i][j] + 1 ;
q.push( { i-1 , j } ) ;
}
if( check( i , j+1 ) ){
vis[i][j+1] = vis[i][j] + 1 ;
q.push( { i , j+1 } ) ;
}
if( check( i , j-1 ) ){
vis[i][j-1] = vis[i][j] + 1 ;
q.push( { i , j-1 } ) ;
}
if( isalpha( s[i][j] ) && s[i][j] > 'Z' ){
//cout << s[i][j] << endl ;
if( h[s[i][j]-'a' ] )
continue ;
for( int k = 0 ; k < (int)adj[s[i][j] - 'a' ].size() ; k++ ){
if( check( adj[s[i][j] - 'a'][k].F , adj[s[i][j]-'a'][k].S) ){
vis[adj[s[i][j]-'a'][k].F][adj[s[i][j]-'a'][k].S] = vis[i][j]+1 ;
q.push( adj[s[i][j]-'a'][k] ) ;
}
}
h[s[i][j]-'a'] = 1 ;
}
}
if( vis[end.F][end.S] == 0 )
cout << -1 ;
else
cout << vis[end.F][end.S] - 1 ;
cerr << "Time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " sec \n";
} | #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define REP(i,n) for(int _n=n, i=0;i<_n;++i)
#define FOR(i,a,b) for(int64_t i=(a),_b=(b);i<=_b;++i)
#define FORD(i,a,b) for(int64_t i=(a),_b=(b);i>=_b;--i)
using ull = uint64_t;
using ll = int64_t;
using PII = pair<int, int>;
using VI = vector<int>;
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string) s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B> string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template <typename A> string to_string(A v) {
bool first = true; string res = "{";
for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); }
res += "}"; return res; }
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H); debug_out(T...); }
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int h, w;
cin >> h >> w;
vector<string> grid(h);
vector<VI> dp(h, VI(w, -1));
REP(i, h) cin >> grid[i];
queue<PII> Q;
vector<vector<PII>> letters(26);
REP(i, h) {
REP(j, w) {
if (grid[i][j] == 'S') {
Q.push({i, j});
dp[i][j] = 0;
}
if (grid[i][j] >= 'a' && grid[i][j] <= 'z') {
letters[grid[i][j] - 'a'].pb({i, j});
}
}
}
vector<bool> F(26, false);
int d1[4] = {1, -1, 0, 0};
int d2[4] = {0, 0, -1, 1};
while (!Q.empty()) {
auto [r, c] = Q.front();
Q.pop();
if (grid[r][c] == 'G') {
cout << dp[r][c] << "\n";
return 0;
}
if (grid[r][c] >= 'a' && grid[r][c] <= 'z' && !F[grid[r][c] - 'a']) {
F[grid[r][c] - 'a'] = true;
for (auto p : letters[grid[r][c] - 'a']) {
if (p.first == r && p.second == c) continue;
if (dp[p.first][p.second] != -1) continue;
Q.push({p.first, p.second});
dp[p.first][p.second] = dp[r][c] + 1;
}
}
FOR(i, 0, 3) {
int rr = r + d1[i];
int cc = c + d2[i];
if (rr < 0 || rr >= h || cc < 0 || cc >= w) continue;
if (grid[rr][cc] == '#') continue;
if (dp[rr][cc] != -1) continue;
dp[rr][cc] = dp[r][c] + 1;
Q.push({rr, cc});
}
}
cout << "-1\n";
} |
#pragma GCC optimize("Ofast")
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <iomanip> // setprecision
#include <complex> // complex
#include <math.h>
#include <cmath>
#include <functional>
#include <cassert>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr double EPS = 1e-10;
// constexpr ll mod = 1000000007;
constexpr ll mod = 998244353;
const int dx[8] = {1, 0, -1, 0,1,1,-1,-1};
const int dy[8] = {0, 1, 0, -1,1,-1,1,-1};
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 char eol = '\n';
// --------------------------------------------------------------------------
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int X,Y,Z;
cin >> X >> Y >> Z;
for(int i=1000000; i>=0; i--){
if(Y*Z > (ll)i*X){
cout << i << endl;
return 0;
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main(){
char a,b,c;
cin >> a >> b >>c;
if (a == b && b == c){
cout << "Won";
}else {
cout<< "Lost";
}
return 0;
}
|
#include<bits/stdc++.h>
//#include <atcoder/all>
#define rep(i,n)for(int i=0;(i)<(int)(n);i++)
#define REP(i,a,b)for(int i=(int)(a);(i)<=(int)(b);i++)
#define ALL(a) (a).begin(),(a).end()
#define pb push_back
#define fi first
#define se second
#define sz(x) ((int)x.size())
using namespace std;
//using namespace atcoder;
using ld = long double;
using ll = long long;
using P = pair<ll, ll>;
template<typename T> bool chmin(T& a, const T& b) { if(a >= b){ a = b; return 1;} return 0; }
template<typename T> bool chmax(T& a, const T& b) { if(a <= b){ a = b; return 1;} return 0; }
const ll MOD = 1e9 + 7;
const ll INF = 1e9;
int main(){
vector<int> a(3);
rep(i, 3)cin >> a[i];
sort(ALL(a));
bool f = false;
do{
if(a[1] - a[0] == a[2] - a[1])f = true;
}while(next_permutation(ALL(a)));
cout << (f ? "Yes" : "No") << 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))
using namespace std;
int64_t solve(int N, const vector<int64_t> &A)
{
vector<vector<int64_t>> DP(N, vector<int64_t>(N, INT64_MAX));
rep(l, N){
rep3(r, l, N){
if (l == r){
DP[l][r] = A[r];
}else{
DP[l][r] = min(DP[l][r-1], A[r]);
}
}
}
int64_t ans = INT64_MIN;
rep(l, N){
rep3(r, l, N){
ans = max(ans, (r-l+1)*DP[l][r]);
}
}
return ans;
return 1;
// TODO: edit here
}
// 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);
constexpr char endl = '\n';
int N;
cin >> N;
vector<int64_t> A(N);
rep(i, N) { cin >> A[i]; }
auto ans = solve(N, A);
cout << ans << endl;
return 0;
}
|
#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<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
#include<cassert>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 998244353;
const ll INF = (ll)1000000007 * 1000000007;
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 Per(i,sta,n) for(int i=n-1;i>=sta;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++)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-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;}
template<int mod>
struct ModInt {
long long x;
static constexpr int MOD = mod;
ModInt() : x(0) {}
ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
explicit operator int() const {return x;}
ModInt &operator+=(const ModInt &p) {
if((x += p.x) >= mod) x -= mod;
return *this;
}
ModInt &operator-=(const ModInt &p) {
if((x += mod - p.x) >= mod) x -= mod;
return *this;
}
ModInt &operator*=(const ModInt &p) {
x = (int)(1LL * x * p.x % mod);
return *this;
}
ModInt &operator/=(const ModInt &p) {
*this *= p.inverse();
return *this;
}
ModInt operator-() const { return ModInt(-x); }
ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
ModInt operator%(const ModInt &p) const { return ModInt(0); }
bool operator==(const ModInt &p) const { return x == p.x; }
bool operator!=(const ModInt &p) const { return x != p.x; }
ModInt inverse() const{
int a = x, b = mod, u = 1, v = 0, t;
while(b > 0) {
t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
return ModInt(u);
}
ModInt power(long long n) const {
ModInt ret(1), mul(x);
while(n > 0) {
if(n & 1)
ret *= mul;
mul *= mul;
n >>= 1;
}
return ret;
}
ModInt power(const ModInt p) const{
return ((ModInt)x).power(p.x);
}
friend ostream &operator<<(ostream &os, const ModInt<mod> &p) {
return os << p.x;
}
friend istream &operator>>(istream &is, ModInt<mod> &a) {
long long x;
is >> x;
a = ModInt<mod>(x);
return (is);
}
};
using modint = ModInt<mod>;
int n,k;
modint dp[3010][3010];
bool visited[3010][3010];
modint get_dp(int i,int j){
//cout << i << " " << j << endl;
if(i<j) return 0;
if(min(i,j)<0) return 0;
if(visited[i][j]) return dp[i][j];
visited[i][j]=true;
dp[i][j]=get_dp(i-1,j-1)+get_dp(i,2*j);
return dp[i][j];
}
void solve(){
cin >> n >> k;
visited[0][0]=true;
dp[0][0]=1;
cout << get_dp(n,k) << endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(50);
solve();
} | #include <bits/stdc++.h>
using namespace std;
//gcd
int gcd (int a, int b) {
return b ? gcd (b, a % b) : a;
}
//lcm
int lcm (int a, int b) {
return a / gcd(a, b) * b;
}
//faster gcd
int fgcd(int a, int b) {
if (!a || !b)
return a | b;
unsigned shift = __builtin_ctz(a | b);
a >>= __builtin_ctz(a);
do {
b >>= __builtin_ctz(b);
if (a > b)
swap(a, b);
b -= a;
} while (b);
return a << shift;
}
void solve()
{
int a,b;
cin>>a;
cin>>b;
int total=2*a+100;
int remain=total-b;
cout<<remain;
}
int main()
{
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
int main(){
ll n,k;
cin >> n >> k;
string s;
cin >> s;
for(ll i=0;i<k;i++){
string t=s+s;
for(ll j=0;j<n;j++){
if(t[j*2]=='R'&&t[j*2+1]=='R') s[j]='R';
else if(t[j*2]=='R'&&t[j*2+1]=='S') s[j]='R';
else if(t[j*2]=='S'&&t[j*2+1]=='R') s[j]='R';
else if(t[j*2]=='S'&&t[j*2+1]=='R') s[j]='R';
else if(t[j*2]=='P'&&t[j*2+1]=='P') s[j]='P';
else if(t[j*2]=='R'&&t[j*2+1]=='P') s[j]='P';
else if(t[j*2]=='P'&&t[j*2+1]=='R') s[j]='P';
else if(t[j*2]=='S'&&t[j*2+1]=='S') s[j]='S';
else if(t[j*2]=='P'&&t[j*2+1]=='S') s[j]='S';
else if(t[j*2]=='S'&&t[j*2+1]=='P') s[j]='S';
}
}
cout << s[0] << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define LLMAX 9223372036854775807
#define ALL(x) x.begin(),x.end()
using ll = int64_t;
using vl = vector<ll>;
template<class T> inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template<class T> inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
//int の最大値 2147483647≒10^9
//long longの最大値 9,223,372,036,854,775,807≒10^18
int main() {
ll a,b,c;
cin >> a >> b >> c;
string ans;
if(a*a + b*b < c*c){
ans = "Yes";
}else{
ans = "No";
}
cout << ans << endl;
} |
#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;
using matrix = vector<vector<ll>>;
ll powmodp(ll x, ll n) {
if (n==0) return 1;
if (n%2) return x * powmodp(x,n-1) % mod;
ll res = powmodp(x,n/2);
return res * res % mod;
}
ll inv(ll x) {return powmodp(x,mod-2);}
ll N, M, K;
vector<ll> A, G[110];
ll p, half;
matrix E(ll n) {
matrix res(n,vector<ll>(n));
rep(i,n) res[i][i] = 1;
return res;
}
void addmodp(ll &x, ll y) {x = (x + y) % mod;}
matrix prodmatrix(matrix x, matrix y) {
ll H = x.size(), W = y[0].size(), sz = y.size();
matrix res(H,vector<ll>(W));
rep(i,H) rep(j,W) rep(k,sz) {
addmodp(res[i][j], x[i][k] * y[k][j] % mod);
}
return res;
}
vector<ll> prodmatrix(matrix x, vector<ll> y) {
matrix z(y.size(),vector<ll>(1));
rep(i,y.size()) z[i][0] = y[i];
matrix tmp = prodmatrix(x,z);
vector<ll> res(tmp.size());
rep(i,tmp.size()) res[i] = tmp[i][0];
return res;
}
vector<ll> prodmatrix(vector<ll> x, matrix y) {
matrix z(1,vector<ll>(x.size()));
rep(i,x.size()) z[0][i] = x[i];
matrix tmp = prodmatrix(z,y);
vector<ll> res(tmp[0].size());
rep(i,tmp[0].size()) res[i] = tmp[0][i];
return res;
}
matrix powmodp(matrix x, ll n) {
if (n==0) return E(x.size());
if (n%2) return prodmatrix(x,powmodp(x,n-1));
matrix res = powmodp(x,n/2);
return prodmatrix(res,res);
}
void init() {
cin >> N >> M >> K;
A.resize(N);
rep(i,N) cin >> A[i];
rep(i,M) {
ll x, y; cin >> x >> y;
x--; y--;
G[x].push_back(y);
G[y].push_back(x);
}
p = inv(M);
half = inv(2);
}
int main() {
init();
matrix mat(N,vector<ll>(N));
rep(i,N) {
ll cnt = G[i].size();
for (ll j : G[i]) mat[j][i] = p * half % mod;
ll q = cnt * p % mod;
q = q * half % mod;
mat[i][i] = (1 + mod - q) % mod;
}
matrix F = powmodp(mat,K);
vector<ll> res = prodmatrix(A,F);
for (ll ans : res) cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#define F first
#define S second
#define all(x) begin(x), end(x)
#define allr(x) rbegin(x), rend(x)
using ll = long long;
using ull = unsigned long long;
using ld = long double;
constexpr ll INF = 2e18;
constexpr ld EPS = 1e-9;
constexpr int MOD = 998244353;
#define int long long
// Custom hash map
struct custom_hash
{
static uint64_t splitmix64(uint64_t x)
{
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const
{
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
// Use int type for keys
template <typename T1, typename T2>
using safe_map = unordered_map<T1, T2, custom_hash>;
// Operator overloads
// cin >> pair<T1, T2>
template<typename T1, typename T2>
istream& operator>>(istream &istream, pair<T1, T2> &p)
{
istream >> p.first >> p.second;
return istream;
}
// cin >> vector<T>
template<typename T>
istream& operator>>(istream &istream, vector<T> &v)
{
for (auto &it : v) cin >> it;
return istream;
}
// cout << pair<T1, T2>
template<typename T1, typename T2>
ostream& operator<<(ostream &ostream, const pair<T1, T2> &p)
{
ostream << p.first << " " << p.second;
return ostream;
}
// cout << vector<T>
template<typename T>
ostream& operator<<(ostream &ostream, const vector<T> &c)
{
for (auto &it : c) cout << it << " ";
return ostream;
}
// Utility functions
template <typename T>
void print(T &&t)
{
cout << t << "\n";
}
template <typename T, typename... Args>
void print(T &&t, Args &&... args)
{
cout << t << " ";
print(forward<Args>(args)...);
}
template <typename T>
int32_t size_i(T &container) { return static_cast<int32_t>(container.size()); }
// Mathematical functions
int GCD(int a, int b)
{
if (!b) return a;
return GCD(b, a % b);
}
int LCM(int a, int b)
{
return (a * b) / GCD(a, b);
}
int modpow(ll x, int n, int m = MOD) {
ll res = 1;
while (n > 0)
{
if (n & 1)
res = (res * x) % m;
x = (x * x) % m;
n >>= 1;
}
return res;
}
int binpow(int x, int n) {
int res = 1;
while (n > 0)
{
if (n & 1)
res = res * x;
x = (x * x);
n >>= 1;
}
return res;
}
// @param m should be prime
int modinv(int x, int m = MOD)
{
return modpow(x, m - 2, m);
}
// Flags to use: -std=c++17 -O2 -Wshadow -DLOCAL_PROJECT -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -fsanitize=address -fsanitize=undefined
////////////////////////////////// START CODE HERE //////////////////////////////////
void preSolve()
{
}
void solve(int tc)
{
int r, x, y;
cin >> r >> x >> y;
ld d = sqrtl((ld)x * x + y * y);
int di = (int)d;
if (abs(di - d) <= 1e-6 && di % r == 0)
return void(print(di / r));
int ans = d/ r;
ans--;
ans = max(0LL, ans);
print(ans + 2);
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(12) << fixed;
preSolve();
int tests = 1;
// cin >> tests;
for (int t = 1; t <= tests; t++)
solve(t);
return 0;
} |
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main() {
int h, w, x, y;
cin >> h >> w >> x >> y;
string s[101];
for (int i = 1; i <= h; i++)
cin >> s[i];
int sum = 0;
for (int i = y - 1; i >= 0; i--) {
if (s[x][i] == '.')
sum++;
else break;
}
for (int i = y; i < w; i++) {
if (s[x][i] == '.')
sum++;
else break;
}
for (int i = x - 1; i > 0; i--) {
if (s[i][y - 1] == '.')
sum++;
else break;
}
for (int i = x + 1; i <= h; i++) {
if (s[i][y - 1] == '.')
sum++;
else break;
}
cout << sum << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using P =pair<int,int>;
int main (){
int H,W,X,Y,count=0;
cin >>H>>W>>X>>Y;
vector<vector<char>> det(H,vector<char>(W));
rep(i,H){
rep(j,W){
cin >>det.at(i).at(j);
}
}
if(det.at(X-1).at(Y-1)=='#'){
cout <<0;
return 0;
}
count++;
for(int i=1;;i++){
if(X-1-i<0)
break;
if(det.at(X-1-i).at(Y-1)=='#')
break;
count++;
}
for(int i=1;;i++){
if(Y-1-i<0)
break;
if(det.at(X-1).at(Y-1-i)=='#')
break;
count++;
}
for(int i=1;;i++){
if(X-1+i>H-1)
break;
if(det.at(X-1+i).at(Y-1)=='#')
break;
count++;
}
for(int i=1;;i++){
if(Y-1+i>W-1)
break;
if(det.at(X-1).at(Y-1+i)=='#')
break;
count++;
}
cout<<count;
} |
#include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vvi=vector<vi>;
constexpr int inf=1e9+7;
constexpr ll longinf=1LL<<60 ;
constexpr ll mod=1e9+7 ;
int main(){
int n,m;
cin>>n>>m;
vi a(m),b(m);
vvi g(n);
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]);
}
int K;cin>>K;
vi c(K);
rep(i,K){
cin>>c[i];
c[i]--;
}
//cout<<1;
vvi dist(K,vi(n,inf));
rep(i,K){
queue<int>q;
q.push(c[i]);
dist[i][c[i]]=0;
while(q.size()){
int u=q.front();q.pop();
for(auto v:g[u]){
if(dist[i][v]>dist[i][u]+1){
dist[i][v]=dist[i][u]+1;
q.push(v);
}
}
}
}
//cout<<dist[0][1];
vvi dp(1<<K,vi(K,inf));
rep(i,1<<K){
int cnt=0,memo=-1;
rep(j,K){
if(i & (1<<j)){
cnt++;
memo=j;
}
}
if(cnt==1)dp[i][memo]=1;
rep(t,K){
rep(j,K){
dp[i|(1<<j)][t]=min(dp[i|(1<<j)][t], dp[i][j]+dist[j][c[t]]);
}
}
}
int ans=inf;
rep(i,K){
ans=min(ans,dp[(1<<K)-1][i]);
}
if(ans==inf)cout<<-1;
else cout<<ans;
} | #include <bits/stdc++.h>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define rep1(i,n) for (int i = 1; i <= (n); ++i)
#define bit(n,k) ((n>>k)&1) //nのk bit目
using ll = long long;
using P = pair<int,int>;
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 llINF = 1LL << 60;
const int iINF = 1e9;
//---main---------------------------------------------
int main(){
//main
int A,B;
cin >> A >> B;
int sa=0,sb=0;
rep(i,3){
sa += A%10;
A /=10;
sb += B%10;
B /= 10;
}
if(sa>=sb) cout << sa << endl;
else cout << sb << endl;
return 0;
} |
#ifdef _DEBUG
#include "../../../library/src/debug_template.hpp"
#define DMP(...) dump(#__VA_ARGS__, __VA_ARGS__)
#else
#define DMP(...) ((void)0)
#endif
#include <cassert>
#include <cstdio>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <numeric>
#include <algorithm>
#include <bitset>
#include <variant>
using namespace std;
using lint = long long;
constexpr int INF = 1010101010;
constexpr lint LINF = 1LL << 60;
struct init {
init() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
}
} init_;
template<class T>
vector<T> make_vec(size_t s, T val) { return vector<T>(s, val); }
template<class... Size>
auto make_vec(size_t s, Size... tail) {
return vector<decltype(make_vec(tail...))>(s, make_vec(tail...));
}
template<const int &Modulo>
struct Mint {
lint val;
constexpr Mint(lint v = 0) noexcept: val(v % Modulo) { if (val < 0) val += Modulo; }
constexpr Mint &operator+=(const Mint &r) noexcept {
val += r.val;
if (val >= Modulo) val -= Modulo;
return *this;
}
constexpr Mint &operator-=(const Mint &r) noexcept {
val -= r.val;
if (val < 0) val += Modulo;
return *this;
}
constexpr Mint &operator*=(const Mint &r) noexcept {
val = val * r.val % Modulo;
return *this;
}
constexpr Mint &operator/=(const Mint &r) noexcept {
lint a{r.val}, b{Modulo}, u{1}, v{0};
while (b) {
lint t = a / b;
a -= t * b;
a ^= b, b ^= a, a ^= b;
u -= t * v;
u ^= v, v ^= u, u ^= v;
}
assert(a == 1);
val = val * u % Modulo;
if (val < 0) val += Modulo;
return *this;
}
constexpr Mint operator+(const Mint &r) const noexcept { return Mint(*this) += r; }
constexpr Mint operator-(const Mint &r) const noexcept { return Mint(*this) -= r; }
constexpr Mint operator*(const Mint &r) const noexcept { return Mint(*this) *= r; }
constexpr Mint operator/(const Mint &r) const noexcept { return Mint(*this) /= r; }
constexpr Mint operator-() const noexcept { return Mint(-val); }
constexpr bool operator==(const Mint &r) const noexcept { return val == r.val; }
constexpr bool operator!=(const Mint &r) const noexcept { return !((*this) == r); }
constexpr bool operator<(const Mint &r) const noexcept { return val < r.val; }
constexpr friend ostream &operator<<(ostream &os, const Mint<Modulo> &x) noexcept { return os << x.val; }
constexpr friend istream &operator>>(istream &is, Mint<Modulo> &x) noexcept {
lint tmp{};
is >> tmp;
x = Mint(tmp);
return is;
}
[[nodiscard]] constexpr Mint pow(lint n) const noexcept {
Mint res{1}, tmp{*this};
while (n > 0) {
if (n & 1) res *= tmp;
tmp *= tmp;
n >>= 1;
}
return res;
}
};
constexpr int MOD = 7;
using mint = Mint<MOD>;
int main() {
int N;
cin >> N;
string S, X;
cin >> S >> X;
auto memo = make_vec(N, 7, -1);
auto dfs = [&](auto &&f, const int &i, const mint &m) -> bool {
if (i == N) return m == 0;
if (memo[i][m.val] != -1) return memo[i][m.val];
if (X[i] == 'T') {
return (memo[i][m.val] = f(f, i + 1, m * 10 + (S[i] - '0')) || f(f, i + 1, m * 10));
}
else {
return (memo[i][m.val] = f(f, i + 1, m * 10 + (S[i] - '0')) && f(f, i + 1, m * 10));
}
};
if (dfs(dfs, 0, 0)) cout << "Takahashi\n";
else cout << "Aoki\n";
return 0;
}
| #include<bits/stdc++.h>
#define ll long long int
#define mp make_pair
#define pii pair<int,int>
#define pll pair<long long int,long long int>
#define mii map<int,int>
#define mll map<long long int,long long int>
#define rep(it,m) for(auto it=m.begin();it!=m.end();it++)
#define pb push_back
#define fr first
#define sc second
#define mod 1000000007LL
#define setbiti(x) __builtin_popcount(x)
#define setbitll(x) __builtin_popcountll(x)
#define vi vector<int>
#define vll vector<long long int>
#define f(i,x,n) for(ll i=x;i<n;i++)
#define l(i,a,b) for(ll i=a;i>b;i--)
#define w(x) int x; cin>>x; while(x--)
#define all(x) x.begin(),x.end()
#define fill(a,x) memset(a,x,sizeof(a))
#define pie 3.14159265
using namespace std;
void fio() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
ll bpow(ll a, ll b, ll m) {
if (b == 0) {
return 1;
}
if (b == 1) {
return a % m;
}
if (b & 1) {
return (a * bpow(a, b - 1, m)) % m;
}
ll x = bpow(a, b / 2, m);
return (x * x) % m;
}
int main() {
fio();
ll n, m;
cin >> n >> m;
ll ans = bpow(10, n, m * m);
ans = (ans / m) % m;
cout << ans << endl;
return 0;
} |
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;++i)
signed main(){
int n,k,m;cin>>n>>k>>m;
vector<int> a(n,0);
rep(i,n-1)cin>>a[i];
int sum=0;
rep(i,n-1){
sum+=a[i];
}
bool tag=false;
int ans=n*m-sum;
if(ans>k){
tag=true;
}
if(ans<0)ans=0;
if(tag)ans=-1;
cout<<ans<<endl;
return 0;
} | #include <iostream>
using namespace std;
int main()
{
int n,k,m,x,y=0,z;
cin>>n>>k>>m;
int a[100];
x=m*n;
for(int i=0; i<n-1; i++)
{
cin>>a[i];
y=y+a[i];
}
z=x-y;
if(k>=z && z>0)
{
cout<<z<<endl;
}else if(k<z){
cout<<"-1"<<endl;
}else{
cout<<"0"<<endl;
}
} |
#include<bits/stdc++.h>
using namespace std;
int main(){
long long n,x;
cin>>n>>x;
string s;
cin>>s;
int m= s.length();
for(int i=0; i<m;++i){
if(x==0 && s[i]== 'x'){
x+=0;
}
else if (s[i]== 'x'){
x--;
}
else{
x++;
}
}
cout<<x;
}
| #include<cstdio>
inline int in();
inline void wr(int);
inline int mx(int,int);
int main(int argc,char**argv){
register int n=in(),x=in();
while(n--){
register char c=getchar();
while(c!='o'&&c!='x')c=getchar();
if(c=='x')x=mx(0,x-1);
else ++x;
}
wr(x),putchar('\n');
}
inline int mx(int x,int y){
return x>y?x:y;
}
inline int in(){
register char c=getchar();
register int x=0,f=1;
for(;c<'0'||c>'9';c=getchar())
if(c=='-')f=-1;
for(;c>='0'&&c<='9';c=getchar())
x=(x<<1)+(x<<3)+(c&15);
return x*f;
}
inline void wr(int x){
if(x<0)putchar('-'),x=-x;
if(x/10)wr(x/10);
putchar(x%10+'0');
} |
#include <bits/stdc++.h>
#define ls rt << 1
#define rs rt << 1|1
#define lson ls,l,m
#define rson rs,m+1,r
#define st first
#define ed second
#define Point pair<int,int>
typedef long long ll;
using namespace std;
const int N = 1e6+5;
const int PI = 314;
int main(){
ll n,k;
scanf("%lld%lld",&n,&k);
n += 100;
ll l = 1ll,r = 2e12;
while(l < r){
ll m = (l+r+1) >> 1;
ll t = m*n/100ll;
if((t-m) < k) l = m;
else r = m-1;
}
printf("%lld\n",(l*n)/100ll+1ll);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pii pair <int, int>
#define pb push_back
#define F first
#define S second
#define ll long long
#define io ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define M_PI 3.14159265358979323846
const int N = 100005;
const int mod = 1e9 + 7;
int main() {
io;
int t;
cin >> t;
while(t--){
ll l, r;
cin >> l >> r;
ll k = r - l - l + 1;
k = max(k, 0ll);
k = k * (k + 1) / 2;
cout << k << endl;
}
}
|
#include <cstdio>
int main()
{
int n, k, f1, r1;
scanf("%d%d", &n, &k);
r1=(1+k)*k*n/2;
f1=(1+n)*n*k/2;
printf("%d\n", f1*100+r1);
return 0;
} | #pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <complex>
#include <queue>
#include <set>
#include <unordered_set>
#include <list>
#include <chrono>
#include <random>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <stack>
#include <iomanip>
#include <fstream>
using namespace std;
typedef long long ll;
typedef long double ld;
ll MOD = 1e9 + 7;
double eps = 1e-12;
#define ln "\n"
#define dbg(x) cout << #x << " = " << x << ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define INF 2e18
#define fast_cin() \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
void solve()
{
int n, k, digit = 0;
cin >> n >> k;
ll sum = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= k; j++)
{
digit = i * 100 + j;
sum += digit;
}
}
cout << sum << endl;
}
int main()
{
fast_cin();
solve();
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using WGraph = vector<vector<pair<int, ll>>>;
template<class T>inline bool chmax(T &a, const T &b) { if (b > a) { a = b; return true; } return false; }
template<class T>inline bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
constexpr double EPS = 1e-10;
constexpr double PI = M_PI;
char win(char c1, char c2) {
if (c1 > c2) swap(c1, c2);
if (c1 == c2) return c1;
if (c1 == 'P' && c2 == 'R') return 'P';
else if (c1 == 'P' && c2 == 'S') return 'S';
else if (c1 == 'R' && c2 == 'S') return 'R';
}
char dfs(int n, int k, const string &s) {
map<char, int> mp;
for (char c : s) ++mp[c];
if ((int)mp.size() == 1) {
return s.front();
} else if ((int)mp.size() == 2) {
return win(mp.begin()->first, (++mp.begin())->first);
} else {
int p2 = 0;
while ((1<<(p2+1)) <= n) ++p2;
if (k <= p2) {
string ss = s.substr(0, 1<<k), tt = "";
while ((int)ss.size() > 1) {
for (int i=0; i<(int)ss.size(); i+=2) tt.push_back(win(ss[i], ss[i+1]));
ss = tt;
tt = "";
}
return ss.front();
} else {
int sz = n * (1<<p2);
string ss = "";
while ((int)ss.size() < sz) ss += s;
string tt = "";
for (int i=0; i<n; ++i) {
string sss = ss.substr(i*(1<<p2), 1<<p2), ttt = "";
while ((int)sss.size() > 1) {
for (int i=0; i<(int)sss.size(); i+=2) ttt.push_back(win(sss[i], sss[i+1]));
sss = ttt;
ttt = "";
}
tt.push_back(sss.front());
}
return dfs(n, k-p2, tt);
}
}
}
void solve() {
int n, k;
string s;
cin >> n >> k >> s;
cout << dfs(n, k, s) << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(20);
solve();
return 0;
}
| #include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <math.h>
#include <cassert>
#define rep(i,n) for(int i = 0; i < n; ++i )
using namespace std;
using ll = long long;
int main() {
int n,k; string s;
cin >> n >> k >> s;
auto f = [](char l,char r){
if(l=='R'&&r=='P') return r;
if(l=='S'&&r=='R') return r;
if(l=='P'&&r=='S') return r;
return l;
};
rep(_,k){
string t = s;
s += s;
rep(i,n) t[i] = f(s[2*i],s[2*i+1]);
swap(s,t);
}
cout << s[0] << endl;
} |
#include <bits/stdc++.h>
#define loop(s, e, i) for (int i = s; i < e; ++i)
#define print(s) cout << s << endl;
#define DIV 1000000007
#define ll long long
using namespace std;
const int INF = 1e9+7;
/*
浮動小数点の入力
cout << fixed << setprecision(9) << endl;
*/
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
ll A, B;
cin >> A >> B;
ll mx = 2*A+100;
ll left = mx - B;
if (left >= 0) {
print(left);
} else {
print(0);
}
}
| #include<bits/stdc++.h>
using namespace std;
int main()
{
long long int a,b;
cin>>a>>b;
cout<<((2*a)+100)-b<<endl;
return 0;
}
|
#include<iostream>
#include<string>
using namespace std;
int main(){
int n,k;
string s;
char win[222][222];
win['R']['S']=win['S']['R']=win['R']['R']='R';
win['S']['P']=win['P']['S']=win['S']['S']='S';
win['P']['R']=win['R']['P']=win['P']['P']='P';
cin>>n>>k>>s;
while(k--){
const auto t=s+s;
for(int i=0;i<n*2;i+=2){
s[i/2]=win[t[i]][t[i+1]];
}
}
cout<<s[0];
} | #include<bits/stdc++.h>
#define ll long long
#define re register
#define INF 2147483647
using namespace std;
inline int read()
{
int f=1,x=0;char s=getchar();
while(s<'0'||s>'9')
{
if(s=='-') f=-1;
s=getchar();
}
while(s>='0'&&s<='9')
{
x=x*10+s-48;
s=getchar();
}
return f*x;
}
inline char cmp(char a,char b)
{
if(a==b) return a;
if(a=='R')
{
if(b=='P') return b;
else return a;
}
if(a=='P')
{
if(b=='S') return b;
else return a;
}
if(a=='S')
{
if(b=='R') return b;
else return a;
}
}
char s[105],f[105][105];
int nxt[105][105];
int main()
{
int n=read(),k=read();
scanf("%s",s);
for(int i=0;i<n;i++)
{
nxt[i][1]=(i+1)%n;
//cout<<nxt[i][1]<<endl;
f[i][1]=cmp(s[nxt[i][1]],s[i]);
//putchar(f[i][1]);
//puts("");
}
for(int j=2;j<=k;j++)
{
for(int i=0;i<n;i++)
{
nxt[i][j]=nxt[(nxt[i][j-1]+1)%n][j-1];
f[i][j]=cmp(f[i][j-1],f[(nxt[i][j-1]+1)%n][j-1]);
//cout<<nxt[i][j]<<endl;
}
}
putchar(f[0][k]);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
#pragma region DEBUG
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef
tree<
pair<int,int>,
null_type,
less<pair<int,int>>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
#pragma endregion DEBUG
#define MOD 1000000007
#define MAX 500005
#define endl "\n"
vector <lli> fact(MAX,1);
vector <lli> invfact(MAX,1);
lli binpow(lli a, lli b, lli m){
//a %= m;
lli res = 1;
while(b > 0){
if(b&1)
res = a*res%m;
b >>=1;
a = a*a%m;
}
return res;
}
void solve(){
lli a,b,c;
cin>>a>>b>>c;
lli X = binpow(b,c,4);
//debug(X);
if(X == 0)
X = 4;
lli ans = binpow(a%10, X, MOD);
cout<<(ans%10)<<endl;
}
int main(){
/* freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout); */
ios_base::sync_with_stdio(false);
cin.tie(NULL);
/* for(lli i = 1; i<MAX-5; i++){
fact[i] = i*fact[i-1]%MOD;
invfact[i] = binpow(fact[i],MOD-2);
} */
lli t;
t = 1;
//cin>>t;
while(t--)
solve();
} | #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int a, B, c;
inline int fpow(int x, int b, int m) {
int res = 1;
while (b) {
if (b & 1) res = 1ll * res * x % m;
x = 1ll * x * x % m;
b >>= 1;
}
return res;
}
int main() {
cin >> a >> B >> c;
cout << fpow(a, 4 + fpow(B, c, 4), 10);
} |
//#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;
} | #include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <string>
#include <iomanip>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <complex>
#include <ctime>
#include <bitset>
// #include <atcoder/all>
#include <fstream>
#include <random>
//#include <boost/multiprecision/cpp_int.hpp>
//namespace mp = boost::multiprecision;
using namespace std;
// using namespace atcoder;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef int itn;
const ll LINF = 1e18;
const int INF = 1e9;
//マクロ定義
#define vvint(vec,n,m,l) vector<vector<int>> vec(n, vector<int>(m,l)); // lで初期化
#define vvll(vec,n,m,l) vector<vector<ll>> vec(n,vector<ll>(m,l));
#define vint vector<int>
#define pint pair<int,int>
#define rep(i,a) for(ll i=0;i<(a);i++)
#define all(x) (x).begin(),(x).end()
#define debug system("pause") //デバッグ用
#define ret return 0
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 Graph = vector<vector<ll>>;
#define ketasitei setprecision(15) //15桁表示
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const double PI = 3.1415926535897932;
//繰り返し自乗法(N^P mod M)
ll RepeatSquaring(ll N, ll P, ll M) {
if (P == 0) return 1;
if (P % 2 == 0) {
ll t = RepeatSquaring(N, P / 2, M);
return (t * t) % M;
}
return (N * RepeatSquaring(N, P - 1, M)) % M;
}
int main(void)
{
cin.tie(0);
ios::sync_with_stdio(false);
cout << ketasitei;
ll h, w;
cin >> h >> w;
vector<string> s(h);
// 0:左 1:上 2:左上
vector<vector<vector<ll>>> dp(h + 1, vector<vector<ll>>(w + 1, vector<ll>(3, 0)));
rep(i, h)
cin >> s[i];
dp[0][0][0] = 0;
dp[0][0][1] = 0;
dp[0][0][2] = 0;
for (ll i = 1; i < h; i++)
{
if (s[i][0] == '#')
break;
dp[i][0][1] = (RepeatSquaring(2, i - 1LL, MOD));
}
for (ll i = 1; i < w; i++)
{
if (s[0][i] == '#')
break;
dp[0][i][0] = RepeatSquaring(2, i - 1LL, MOD);
}
for (ll i = 1; i < h; i++)
{
for (ll j = 1; j < w; j++)
{
if (s[i][j] == '#')
continue;
if (i == 1 && j == 1)
{
dp[i][j][2] = 1;
}
dp[i][j][0] += 2 * dp[i][j - 1][0] + dp[i][j - 1][1] + dp[i][j - 1][2];
dp[i][j][0] %= MOD;
dp[i][j][1] += dp[i - 1][j][0] + 2 * dp[i - 1][j][1] + dp[i - 1][j][2];
dp[i][j][1] %= MOD;
dp[i][j][2] += dp[i - 1][j - 1][0] + dp[i - 1][j - 1][1] + 2 * dp[i - 1][j - 1][2];
dp[i][j][2] %= MOD;
}
}
cout << (dp[h - 1][w - 1][0] + dp[h - 1][w - 1][1] + dp[h - 1][w - 1][2]) % MOD << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
int a[200005];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n,i,j;
cin >> n;
for(i=1;i<=n;i++)
cin >> a[i];
int last = 1;
vector<int>ans;
for(i=2;i<=n;i++)
{
if(a[i] < i)
{
if(a[i] < last)
{
cout << -1 << endl;
return 0;
}
int pos = a[i];
for(j=i;j>pos;j--)
{
swap(a[j],a[j-1]);
ans.push_back(j-1);
}
last = i;
}
}
if(ans.size() != (n-1)) cout << -1 << endl;
else
{
for(i=1;i<=n;i++)
{
if(a[i] != i)
{
cout << -1 << endl;
return 0;
}
}
for(int x:ans) cout << x << endl;
}
}
| #include <bits/stdc++.h>
int ri() {
int n;
scanf("%d", &n);
return n;
}
int main() {
int n = ri();
int a[n];
for (auto &i : a) i = ri() - 1;
int plus[n];
int minus[n];
memset(plus, 0, sizeof(plus));
memset(minus, 0, sizeof(minus));
for (int i = 0; i < n; i++) {
if (a[i] < i) {
plus[a[i]]++;
plus[i]--;
} else if (a[i] > i) {
minus[i]++;
minus[a[i]]--;
}
}
for (int i = 0; i + 1 < n; i++) plus[i + 1] += plus[i], minus[i + 1] += minus[i];
bool ok = true;
for (int i = 0; i + 1 < n; i++) if (plus[i] != 1 || minus[i] != 1) ok = false;
if (!ok) {
puts("-1");
return 0;
}
for (int i = 0; i < n; i++) {
if (a[i] < i) {
for (int j = a[i]; j < i; j++) plus[j] = a[i];
} else if (a[i] > i) {
for (int j = i; j < a[i]; j++) minus[j] = a[i];
}
}
std::set<int> avail;
auto update = [&] (int i) {
if (a[i] == minus[i] && a[i + 1] == plus[i]) {
avail.insert(i);
} else avail.erase(i);
};
for (int i = 0; i + 1 < n; i++) update(i);
std::vector<int> res;
while (avail.size()) {
int cur = *avail.begin();
res.push_back(cur);
avail.erase(avail.begin());
std::swap(a[cur], a[cur + 1]);
if (cur) update(cur - 1);
update(cur);
if (cur + 1 < n - 1) update(cur + 1);
}
if ((int) res.size() != n - 1) {
puts("-1");
return 0;
}
for (auto i : res) printf("%d\n", i + 1);
return 0;
}
|
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
constexpr int inf=1e9+7;
constexpr ll longinf=1LL<<60 ;
constexpr ll mod=1e9+7 ;
int dp[111][111][111];
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n;
cin>>n;
ll x;
cin>>x;
vector<int> a(n);
rep(i,n)cin>>a[i];
ll ans = x + 1;
REP(t,1,n+1){
rep(i,n+1)rep(j,n+1)rep(k,t){
dp[i][j][k] = -inf;
}
dp[0][0][0]=0;
rep(i,n)rep(j,i+1)rep(k,t){
if(dp[i][j][k]<0)continue;
dp[i+1][j+1][(k+a[i])%t]=max(dp[i+1][j+1][(k+a[i])%t],dp[i][j][k]+a[i]);
dp[i+1][j][k]=max(dp[i+1][j][k],dp[i][j][k]);
}
if(dp[n][t][x%t]<0)continue;
ans = min((x-dp[n][t][x%t])/t,ans);
}
cout<<ans<<endl;
return 0;
} | #include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pll pair<ll, ll>
#define pb push_back
#define pii pair<ll ,pll>
#define task "parentheses"
using namespace std;
const int N = 105;
const ll mod = 1e18+7;
const int base = 350;
ll n, t, m, k, tong, a[N], dp[N][N], ans;
vector<ll> kq;
vector<ll> adj[N];
void sol()
{
cin >> n >> m;
for(int i = 1; i <= n; i ++)cin >> a[i];
ans = mod;
for(int i = 1; i <= n; i ++)
{
for(int l = 0; l <= i; l ++)
{
for(int r = 0; r <= i; r ++)dp[l][r] = -1;
}
dp[0][0] = 0;
for(int j = 1; j <= n; j ++)
{
for(int l = j-1; l >= 0; l --)
{
for(int r = i-1; r >= 0; r --)
{
if(dp[l][r] == -1)continue;
k = (r + a[j] % i) % i;
dp[l+1][k] = max(dp[l+1][k], dp[l][r] + a[j]);
}
}
}
if(dp[i][m%i] != -1)ans = min(ans, (m - dp[i][m%i]) / i);
}
cout << ans;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen(task".inp", "r"))
{
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int ntest = 1;
//cin >> ntest;
while(ntest -- > 0)
sol();
}
//https://docs.google.com/document/d/1JARtZUcSiWp9F95IoR8hhMp65eDu3vrRqvK_iUOQiFI/edit#heading=h.j89qf85z2g73
|
#include <bits/stdc++.h>
#define pii pair<int, int>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int h, w;
cin >> h >> w;
bool g[h][w];
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
char temp;
cin >> temp;
g[i][j] = (temp == '#');
}
}
/*
*/
int cnt = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (g[i][j]) {
// top
if (!g[i - 1][j] && (g[i - 1][j - 1] || !g[i][j - 1])) cnt++;
// bottom
if (!g[i + 1][j] && (g[i + 1][j - 1] || !g[i][j - 1])) cnt++;
// left
if (!g[i][j - 1] && (g[i - 1][j - 1] || !g[i - 1][j])) cnt++;
// right
if (!g[i][j + 1] && (g[i - 1][j + 1] || !g[i - 1][j])) cnt++;
}
}
}
cout << cnt;
}
| #include <bits/stdc++.h>
using namespace std;
int h,w;
int dp[2001][2001];
int c[2001][2001];
int main(){
char cha;
cin>>h>>w;
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
cin>>cha;
if(cha=='+') c[i][j]=1;
else c[i][j]=-1;
}
}
for(int j=h-1;j>=0;j--){
for(int k=w-1;k>=0;k--){
if(j>=h||k>=w) continue;
if((j+k)%2==1){
if(j==h-1) dp[j][k]=dp[j][k+1]+c[j][k+1];
else if(k==w-1) dp[j][k]=dp[j+1][k]+c[j+1][k];
else dp[j][k]=max(dp[j+1][k]+c[j+1][k],dp[j][k+1]+c[j][k+1]);
}
else{
if(j==h-1) dp[j][k]=dp[j][k+1]-c[j][k+1];
else if(k==w-1) dp[j][k]=dp[j+1][k]-c[j+1][k];
else dp[j][k]=min(dp[j+1][k]-c[j+1][k],dp[j][k+1]-c[j][k+1]);
}
}
}
if(dp[0][0]<0) cout<<"Takahashi\n";
else if(dp[0][0]>0) cout<<"Aoki\n";
else cout<<"Draw\n";
} |
#include <bits/stdc++.h>
using namespace std;
#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 all(a) a.begin(), a.end()
#define All(a) a.rbegin(), a.rend()
typedef vector<int> VI;
typedef long long int ll;
int main() {
int x;
cin >> x;
cout << 100-(x%100) << endl;
return 0;
} | #include <bits/stdc++.h>
#define _overload3(_1,_2,_3,name,...)name
#define _rep(i,n)repi(i,0,n)
#define repi(i,a,b)for(int i=int(a),i##_len=(b);i<i##_len;++i)
#define MSVC_UNKO(x)x
#define rep(...)MSVC_UNKO(_overload3(__VA_ARGS__,repi,_rep,_rep)(__VA_ARGS__))
#define all(c)c.begin(),c.end()
#define write(x)cout<<(x)<<'\n'
using namespace std; using ll = long long; template<class T>using vv = vector<vector<T>>;
template<class T>auto vvec(int n, int m, T v) { return vv<T>(n, vector<T>(m, v)); }
constexpr int INF = 1 << 29, MOD = int(1e9) + 7; constexpr ll LINF = 1LL << 60;
struct aaa { aaa() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(10); }; }aaaa;
long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
ll extgcd(ll a, ll b, ll& x, ll& y) {
for (ll v = x = 1, u = y = 0; b; ) { ll q = a / b; swap(a -= q * b, b); swap(x -= q * u, u); swap(y -= q * v, v); }
return a;
}
int main() {
int T;
cin >> T;
rep(t, T) {
ll N, S, K;
cin >> N >> S >> K;
ll G = gcd(N, K);
if (S % G) {
write(-1);
continue;
}
ll n = N / G, k = K / G, s = S / G;
ll x, y;
ll ret = extgcd(k, -n, x, y);
assert(abs(ret) == 1);
if (ret == 1) {
x *= n - s;
y *= n - s;
}
else {
x *= s;
y *= s;
}
ll z = min(x / n, y / k);
x -= n * z;
write(x);
}
}
|
#include <bits/stdc++.h>
#define rep(begin, end) for (i=begin;i<end;i++)
#define repj(begin, end) for (j=begin;j<end;j++)
#define init(arr, end, val) for (i=0;i<end;i++) arr[i] = val;
#define printint(i0, i1) printf("%d %d\n", i0, i1)
using namespace std;
typedef long long int ll;
const int inf = 1000000000;
const int mod = 1000000007;
const int nil = -1;
ll i, j, n, w, s, t, p;
ll a[200001];
bool ans;
int main() {
scanf(" %lld %lld", &n, &w);
rep(0,n+1) a[i] = 0;
rep(0,n) {
scanf(" %lld %lld %lld", &s, &t, &p);
a[s] += p; a[t] -= p;
}
ans = a[0] <= w;
rep(1,n+1) {
a[i] += a[i-1];
if (a[i] > w) ans = false;
}
if (ans) printf("Yes");
else printf("No");
} | #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<class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << '['; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << ']'; }
template<typename T, size_t size> ostream& operator<<(ostream &os, const array<T, size> &arr) { os << '{'; string sep; for (const auto &x : arr) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '{' << p.first << ", " << p.second << '}'; }
template<typename T> ostream& operator<<(ostream &os, const set<T> &s) { os << '{'; string sep; for (const auto &x : s) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream& operator<<(ostream &os, const map<A, B> &mp) { os << '['; string sep; for (const auto &x : mp) os << sep << x, sep = ", "; return os << ']'; }
template<typename T> ostream& operator<<(ostream &os, const multiset<T> &s) { os << '{'; string sep; for (const auto &x : s) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#ifndef ONLINE_JUDGE
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "] =", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
#define int long long
#define setbits(x) __builtin_popcountll(x)
#define mod 1000000007
#define inf 1e18
#define endl '\n'
#define pb push_back
#define all(x) x.begin(),x.end()
#define ff first
#define ss second
int powmod(int a, int b) {int res = 1; a %= mod; for (; b; b >>= 1) {if (b & 1)res = res * a % mod; a = a * a % mod;} return res;}
int fast_pow(int a, int b) {if (b == 1) return a; if (b == 0) return 1; int x = fast_pow(a, b / 2); x *= x; if (b & 1) x *= a; return x;}
const int nax = 2e5 + 5;
vector<pair<int, int>> v(nax);
vector<int> timee(nax);
vector<int> ans(nax);
vector<pair<int, int>> marks(nax);
vector<pair<int, int>> pre(nax);
int n, t;
void solve()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cout << fixed << setprecision(10);
/*#ifndef ONLINE_JUDGE
freopen("inp.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif*/
cin >> n >> t;
for (int i = 0; i < n; i++) {
cin >> v[i].ff >> v[i].ss >> timee[i];
}
for (int i = 0; i < n; i++) {
marks[v[i].ff + 1].ff++;
marks[v[i].ff + 1].ss += timee[i];
marks[v[i].ss + 1].ff--;
marks[v[i].ss + 1].ss -= timee[i];
// dbg(marks);
}
pre[0].ff = marks[0].ff;
pre[0].ss = marks[0].ss;
for (int i = 1; i < nax; i++) {
pre[i].ff = pre[i - 1].ff + marks[i].ff;
pre[i].ss = pre[i - 1].ss + marks[i].ss;
}
// dbg(marks);
// dbg(pre);
bool c = 0;
for (int i = 0; i < nax; i++) {
if (pre[i].ss > t) {
cout << "No" << endl;
return;
}
}
cout << "Yes" << endl;
}
int32_t main()
{
solve();
return 0;
}
// 0 0 0 0 0 0 0 0 0 0 0
// 0 1 0 -1 0 0 0 0 0 0 0
// 0 1 1 -1 -1 0 0 0 0 0 0
// 0 1 1 0 -1 0 0 0 0 0 0 -1
// 0 1 2 0 -2 0 0 0 0 0 0 -1 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<typename T> using v2 = vector<vector<T>>;
template<typename T> inline v2<T> fill(int r, int c, const T& t){ return v2<T>(r, vector<T>(c, t)); }
const int MOD = 1e9+7;
void solve(){
int h, w;
cin >> h >> w;
v2<char> grid = fill(h, w, '?');
for(int i = 0; i < h; ++i){
for(int j = 0; j < w; ++j){
cin >> grid[i][j];
}
}
v2<int> dp = fill(h, w, -1), horiz, vert, diag;
horiz = vert = diag = dp;
dp[0][0] = horiz[0][0] = vert[0][0] = diag[0][0] = 1;
for(int i = 0; i < h; ++i){
for(int j = 0; j < w; ++j){
if(!i&&!j) continue;
if(grid[i][j] == '#'){
dp[i][j] = horiz[i][j] = vert[i][j] = diag[i][j] = 0;
}
else{
int h = (j-1>=0) ? horiz[i][j-1] : 0;
int v = (i-1>=0) ? vert[i-1][j] : 0;
int d = (i-1>=0 && j-1>=0) ? diag[i-1][j-1] : 0;
dp[i][j] = ((ll)h+v+d)%MOD;
horiz[i][j] = (dp[i][j] + h)%MOD;
vert[i][j] = (dp[i][j] + v)%MOD;
diag[i][j] = (dp[i][j] + d)%MOD;
}
}
}
cout << dp[h-1][w-1] << "\n";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define PI 3.14159265359
int main() {
int N;
int64_t W;
cin >> N >> W;
vector<vector<int>> T(N, vector<int>(N));
vector<int> v;
for (int i = 0; i < N; i++){
v.push_back(i);
for (int j = 0; j < N; j++){
cin >> T[i][j];
}
}
v.erase(remove(v.begin(), v.end(), 0), v.end());
int ans = 0;
do {
int64_t dist = 0;
for (int i = 0; i < N - 2; i++) dist += T[v[i]][v[i + 1]];
dist += T[0][v[0]] + T[v[N - 2]][0];
if (dist == W) ans++;
} while(next_permutation(v.begin(), v.end()));
cout << ans << endl;
}
|
/*
/^--^\
\____/
/ \ _____ _ __ __ ____ _ ____ ____ _____
| || ()_)| |\ \/ /| ===|| |__ / (__` / () \|_ _|
\__ __/ |_| |_|/_/\_\|____||____|\____)/__/\__\ |_|
|^|^\ \^|^|^|^|^|^|^|^|^|^|^|^|^|^|^|^|^|^|^|^|^|^|^|^|^|
| | |\ \| | | | | | | | | | | | | | | | | | | | | | | | |
#####/ /#################################################
| | |\/ | | | | | | | | | | | | | | | | | | | | | | | | |
|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|*/
#pragma GCC optimize("O4,unroll-loops,no-stack-protector")
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using ull=unsigned long long;
using pii=pair<ll,ll>;
#define int ll
#define double long double
#define For(i,a,b) for(int i=a;i<=b;i++)
#define Forr(i,a,b) for(int i=a;i>=b;i--)
#define Fors(i,a,b,s) for(int i=a;i<=b;i+=s)
#define F first
#define S second
#define L(id) (id*2+1)
#define R(id) (id*2+2)
#define eb emplace_back
#define all(x) x.begin(),x.end()
#define sz(x) ((int)x.size())
#define mkp make_pair
#define MOD (ll)(998244353)
#define INF (1e17)
#define EPS (1e-6)
#ifdef LOCALMEOW
#define debug(...) do{\
cerr << __PRETTY_FUNCTION__ << " - " << __LINE__ <<\
" : ("#__VA_ARGS__ << ") = ";\
_OUT(__VA_ARGS__);\
}while(0)
template<typename T> void _OUT(T x) { cerr << x << "\n"; }
template<typename T,typename...I>
void _OUT(T x,I ...tail) { cerr << x << ", "; _OUT(tail...); }
#else
#define debug(...)
#endif
inline void IOS(){ ios::sync_with_stdio(false); cin.tie(0); }
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int gcd(int a,int b) { return b==0?a:gcd(b,a%b); }
int lcm(int a,int b) { return a/gcd(a,b)*b; }
int fpow(int b,int p){
int ans=1,now=b;
while(p){
if(p&1) ans=ans*now%MOD;
p/=2; now=now*now%MOD;
}
return ans;
}
void minify(int &a,int b) { if(b<a) a=b; }
void maxify(int &a,int b) { if(b>a) a=b; }
vector<pii> adj[2020];
int dis[2020];
int dijk(int n){
For(i,0,2019) dis[i]=INF;
priority_queue<pii,vector<pii>,greater<pii>> pq;
for(auto &i:adj[n])
pq.emplace(i.S,i.F);
while(!pq.empty()){
auto p=pq.top(); pq.pop();
if(p.S==n) return p.F;
if(dis[p.S]!=INF) continue;
dis[p.S]=p.F;
for(auto &e:adj[p.S]){
pq.emplace(e.S+p.F,e.F);
}
}
return -1;
}
int32_t main(){
IOS();
//code...
int n,m; cin>>n>>m;
int a,b,c;
For(i,0,m-1){
cin>>a>>b>>c;
adj[a].eb(b,c);
}
For(i,1,n) cout<<dijk(i)<<"\n";
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define pf push_front
#define sz(x) (int)(x).size()
#define lb lower_bound
#define ub upper_bound
#define mp make_pair
#define fi first
#define se second
#define setbits(x) __builtin_popcount(x)
#define zerobits(x) __builtin_ctz(x)
#define setbitsll(x) __builtin_popcountll(x)
#define zerobitsll(x) __builtin_ctzll(x)
#define ps(x,y) fixed<<setprecision(y)<<x
typedef vector<int> vi;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef long double ld;
typedef map<int,int> mii;
const int MOD = 1e9+7;
const ld PI = acos((ld)-1);
const int maxN = 2001;
vector<pii> adj[maxN];
vi res(maxN,MOD);
vector<vector<int>>dist(maxN,vector<int>(maxN,MOD));
void dijkstra (int src) {
dist[src][src] = 0;
priority_queue<pii>pq;
pq.push({0,src});
while (!pq.empty()) {
pii p = pq.top();
pq.pop();
int d = -p.fi;
int v = p.se;
if (d > dist[src][v]) {
continue;
}
for (pii edge : adj[v]) {
int to = edge.fi;
int w = edge.se;
if (dist[src][to] > dist[src][v]+w) {
dist[src][to] = dist[src][v]+w;
pq.push({-dist[src][to],to});
}
}
}
}
void solve () {
int n,m;
cin >> n >> m;
for (int i = 0;i < m;++i) {
int u,v,w;
cin >> u >> v >> w;
if (u == v) {
res[u] = min(res[u],w);
continue;
}
adj[u].pb({v,w});
}
for (int i = 1;i <= n;++i) {
dijkstra(i);
}
for (int i = 1;i <= n;++i) {
for (pii edge : adj[i]) {
int to = edge.fi;
int w = edge.se;
res[i] = min(res[i],dist[to][i]+w);
}
}
for (int i = 1;i <= n;++i) {
if (res[i] == MOD) {
cout << -1 << "\n";
} else {
cout << res[i] << "\n";
}
}
}
int main () {
#ifndef ONLINE_JUDGE
freopen("input1.txt","r",stdin);
freopen("output1.txt","w",stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(ll i=0,endrep=(n); i<endrep; ++i)
#define rep1(i,n) for(ll i=1,endrep=(n); i<=endrep; ++i)
#define revrep(i,n) for(ll i=(n)-1; i>=0; --i)
inline constexpr ll Inf = (1ULL << 60) -123456789;
#define fastio cin.tie(0); ios_base::sync_with_stdio(false); cout<<fixed<<setprecision(10);
#define newl '\n'
#define YN(e) ((e)?"Yes":"No")
#define all(a) begin(a),end(a)
#define rall(a) rbegin(a),rend(a)
template <class T,class U> bool updmax(T& a, U b) { if(b>a){ a=b; return true;} return false;}
template <class T,class U> bool updmin(T& a, U b) { if(b<a){ a=b; return true;} return false;}
inline constexpr int Mod = 1000000007;
//inline constexpr int Mod = 998244353;
#define dbg(a) cout << #a << ": " << a << endl;
#define dbg1(a,n) cout<<#a<<": "; rep(i,n) cout<<a[i]<<" "; cout<<endl;
#define dbg2(m,h,w) cout<<#m<<":"<<endl; rep(i,h){ rep(j,w)cout<<m[i][j]<<" "; cout<<endl; }
int rng(int n) { return rand()/(RAND_MAX+1.0)*n; }
struct room {
int r[17][17]{};
int na{};
};
int main() {
fastio;
ll ans{};
ll H,W,A,B;
cin >> H >> W >> A >> B;
auto nxt = [&](int i, int j)->pair<int,int> {
if (j == W-1) {
j = 0;
++i;
return {i,j};
}
return {i,j+1};
};
function<void(room,int,int)> dfs = [&](room r,int i, int j) {
if (i == H) {
ll t=0;
rep(k,H) rep(l,W) {
if (r.r[k][l] > 0) ++t;
}
if (t == A*2) {
++ans;
}
return;
}
auto [ni,nj] = nxt(i,j);
if (r.r[i][j] > 0) {
dfs(r, ni,nj);
return;
}
dfs(r, ni,nj);
room q = r;
if (j<W-1 && r.r[i][j+1] == 0) {
r.r[i][j] = 1;
r.r[i][j+1] = 1;
dfs(r,ni,nj);
}
if (i<H-1 && r.r[i+1][j] == 0) {
q.r[i][j] = 1;
q.r[i+1][j] = 1;
dfs(q,ni,nj);
}
};
dfs(room{}, 0,0);
cout << ans << endl;
}
| #include<bits/stdc++.h>
#define for0(i, n) for(int i = 0; i < (n); i++)
#define for1(i, n) for(int i = 1; i <= (n);i++)
#define puts(x) cout << (x) << "\n"
using namespace std;
int si, sj, t[60][60], p[60][60], M;
bool b1[50 * 51];
int score(string s) {
int xs = si, ys = sj, r = p[xs][ys];
for (char c : s) {
if (c == 'U')xs--;
if (c == 'D')xs++;
if (c == 'L')ys--;
if (c == 'R')ys++;
r += p[xs][ys];
}
return r;
}
char fr1(int pu, int pd, int pl, int pr) {
int r = rand() % (pu + pd + pl + pr);
if (r < pu)return 'U';
if (r < pu + pd)return 'D';
if (r < pu + pd + pl)return 'L';
return 'R';
}
int main() {
srand((unsigned)time(NULL));
cin >> si >> sj; si++; sj++; b1[2525] = 1;
for0(i, 52)for0(j, 52) {
if (i * j == 0 || i == 51 || j == 51)t[i][j] = 2525;
else { cin >> t[i][j]; M = max(M, t[i][j]); }
}
for1(i, 50)for1(j, 50)cin >> p[i][j];
string s1 = ""; int v1 = score(s1), ti = clock();
while (clock() - ti <= 1800000) {
string sp = "";
int xi = si, yi = sj;
while (1) {
b1[t[xi][yi]] = 1;
int pu = 10; for1(i, 10)if (b1[t[xi - i][yi]]) { pu = i - 1; break; }
int pd = 10; for1(i, 10)if (b1[t[xi + i][yi]]) { pd = i - 1; break; }
int pl = 10; for1(i, 10)if (b1[t[xi][yi - i]]) { pl = i - 1; break; }
int pr = 10; for1(i, 10)if (b1[t[xi][yi + i]]) { pr = i - 1; break; }
if (pu + pd + pl + pr == 0)break;
char rc = fr1(pu, pd, pl, pr); sp += rc;
if (rc == 'U')xi--;
if (rc == 'D')xi++;
if (rc == 'L')yi--;
if (rc == 'R')yi++;
}
int vi = score(sp);
if (vi > v1) {
s1 = sp; v1 = vi;
}
for0(i, M)b1[i] = 0;
}
puts(s1);
} |
#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp> // #include<ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp> //#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds; //using namespace __gnu_pbds;
#define pb push_back
#define ins insert
#define var auto
#define F first
#define S second
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
template <class T> using oset = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
const int Max = 1e6 + 10;
const int LOG = 30;
const int Mod = 998244353;
const int SQ = 3000;
const int INF = 1e9 + 10;
const int BMSK = 4e6;
void solve()
{
ll x;cin >> x;
// x * (x + 1) / 2 - (x + 1) * (x + 2) / 2 =
// x + 1 >= y * (y + 1) / 2
ll r = 0 , l = 2000000000;
while(l - r > 1)
{
ll mid = (l + r) / 2;
if(mid * (mid + 1) / 2 <= x + 1) r = mid;
else l = mid;
}
cout << x - r + 1 << '\n';
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
/*int t;cin >> t;
while(t--)*/
solve();
}
| #include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(ll i=0;i<(ll)n;i++)
#define dump(x) cerr << "Line " << __LINE__ << ": " << #x << " = " << (x) << "\n";
#define spa << " " <<
#define fi first
#define se second
#define ALL(a) (a).begin(),(a).end()
#define ALLR(a) (a).rbegin(),(a).rend()
using ld = long double;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
template<typename T> using V = vector<T>;
template<typename T> using P = pair<T, T>;
template<typename T> vector<T> make_vec(size_t n, T a) { return vector<T>(n, a); }
template<typename... Ts> auto make_vec(size_t n, Ts... ts) { return vector<decltype(make_vec(ts...))>(n, make_vec(ts...)); }
template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){os << "(" << v.first << ", " << v.second << ")"; return os;}
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { for (auto &e : v) os << e << ' '; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<vector<T>> &v){ for(auto &e : v){os << e << "\n";} return os;}
struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
template <class T> void UNIQUE(vector<T> &x) {sort(ALL(x));x.erase(unique(ALL(x)), x.end());}
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
void fail() { cout << -1 << '\n'; exit(0); }
inline int popcount(const int x) { return __builtin_popcount(x); }
inline int popcount(const ll x) { return __builtin_popcountll(x); }
template<typename T> void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++)
{cerr<<v[i][0];for(ll j=1;j<w;j++)cerr spa v[i][j];cerr<<"\n";}};
template<typename T> void debug(vector<T>&v,ll n){if(n!=0)cerr<<v[0];
for(ll i=1;i<n;i++)cerr spa v[i];
cerr<<"\n";};
const ll INF = (1ll<<62);
// const ld EPS = 1e-10;
// const ld PI = acos(-1.0);
const ll mod = (int)1e9 + 7;
//const ll mod = 998244353;
int main(){
ll n;
cin >> n;
ll sqn = sqrt(2*(n+1));
ll k = 0;
for(ll i=-10;i<=10;i++){
ll v = sqn + i;
if(v<0) continue;
if(v*(v+1) <= 2*(n+1)) chmax(k, v);
}
// dump(k)
cout << n+1 - k << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 5;
char s[MAXN];
struct Deque {
bool rev;
char buffer[MAXN << 1];
int head = MAXN, tail = MAXN - 1;
bool empty() {
return tail < head;
}
int size() {
return tail - head + 1;
}
char front() {
return rev ? buffer[tail] : buffer[head];
}
char back() {
return rev ? buffer[head] : buffer[tail];
}
void push_front(int x) {
rev ? buffer[++tail] = x : buffer[--head] = x;
}
void push_back(int x) {
rev ? buffer[--head] = x : buffer[++tail] = x;
}
void pop_back() {
rev ? head++ : tail--;
}
void pop_front() {
rev ? tail-- : head++;
}
void reverse() {
rev ^= 1;
}
}Q;
signed main(int argc, char *argv[]) {
cin >> s + 1;
int n = strlen(s + 1);
for (int i = 1; i <= n; ++i) {
if (s[i] == 'R') Q.reverse();
else {
if (!Q.empty() && Q.back() == s[i]) {
Q.pop_back();
continue;
}
else Q.push_back(s[i]);
}
}
while (!Q.empty()) cout << Q.front(), Q.pop_front();
system("pause");
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main(){
string S; cin >> S;
list<char> T;
bool f=false;
for(int i=0; i<S.size(); i++){
if(S[i]=='R'){
f=!f;
}else if(f==true){
if(T.front()==S[i]) T.pop_front();
else T.push_front(S[i]);
}else{
if(T.back()==S[i]) T.pop_back();
else T.push_back(S[i]);
}
}
string T2="";
for(char t: T){
T2+=t;
}
if(f==true){
reverse(T2.begin(), T2.end());
}
cout << T2 << endl;
} |
#include <math.h>
#include <stdio.h>
#include <time.h>
#include <algorithm>
//#include <atcoder/all>
#include <cassert>
#include <complex>
#include <cstdint>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
// using namespace atcoder;
using pii = pair<int, int>;
using ll = long long;
using pll = pair<ll, ll>;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using vii = vector<int>;
using vvii = vector<vector<int>>;
#define pb push_back
#define mp make_pair
#define sc second
#define fr first
#define stpr setprecision
#define cYES cout << "YES" << endl
#define cNO cout << "NO" << endl
#define cYes cout << "Yes" << endl
#define cNo cout << "No" << endl
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define Rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define rrep(i, n) for (ll i = n - 1; i >= 0; i--)
#define rRep(i, a, b) for (ll i = a; i >= b; i--)
#define crep(i) for (char i = 'a'; i <= 'z'; ++i)
#define psortsecond(A, N) \
sort(A, A + N, \
[](const pii& a, const pii& b) { return a.second < b.second; });
#define ALL(x) (x).begin(), (x).end()
#define debug(v) \
cout << #v << ":"; \
for (auto x : v) { \
cout << x << ' '; \
} \
cout << endl;
#define endl '\n'
int ctoi(const char c) {
if ('0' <= c && c <= '9') return (c - '0');
return -1;
}
ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
constexpr ll MOD = 1000000007;
constexpr ll INF = 1000000011;
constexpr ll MOD2 = 998244353;
constexpr ll LINF = 1001002003004005006ll;
constexpr ld EPS = 10e-8;
template <class T, class U>
inline bool chmax(T& lhs, const U& rhs) {
if (lhs < rhs) {
lhs = rhs;
return 1;
}
return 0;
}
template <class T, class U>
inline bool chmin(T& lhs, const U& rhs) {
if (lhs > rhs) {
lhs = rhs;
return 1;
}
return 0;
}
template <typename T>
istream& operator>>(istream& is, vector<T>& v) {
for (auto&& x : v) is >> x;
return is;
}
template <typename T, typename U>
istream& operator>>(istream& is, pair<T, U>& p) {
is >> p.first;
is >> p.second;
return is;
}
template <typename T, typename U>
ostream& operator>>(ostream& os, const pair<T, U>& p) {
os << p.first << ' ' << p.second;
return os;
}
template <class T>
ostream& operator<<(ostream& os, vector<T>& v) {
for (auto i = begin(v); i != end(v); ++i) {
if (i != begin(v)) os << ' ';
os << *i;
}
return os;
}
int main() {
ll N, K, M;
cin >> N >> K >> M;
vvll dp(102, vll(125000, 0));
dp[0][0] = 1;
rep(i, 101) {
rep(j, 125000) {
dp[i + 1][j] += dp[i][j];
if (j >= i + 1) {
dp[i + 1][j] += dp[i + 1][j - i - 1];
}
if (j >= (i + 1) * (K + 1)) {
dp[i + 1][j] -= dp[i][j - (i + 1) * (K + 1)];
}
dp[i + 1][j] = (dp[i + 1][j] + 2 * M) % M;
}
}
// rep(i, 3) {
// rep(j, 10) { cout << dp[i][j] << " "; }
// cout << endl;
// }
rep(i, N) {
ll l = i, r = N - i - 1;
ll E = 0;
rep(i, 125000) {
E += dp[l][i] * dp[r][i];
E %= M;
}
E *= (K + 1);
E = (E - 1 + M) % M;
cout << E << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
int n, TTT;
const int mod = 1e9 + 7;
long long ksm(long long a, int n)
{
if (n == 1)
return a;
if (n == 0)
return 1;
long long ans = ksm(a, n >> 1);
ans = ans * ans % mod;
if (n & 1)
ans = ans * a % mod;
return ans;
}
struct matrix
{
long long a[101][101], n, m;
friend matrix operator*(matrix a, matrix b)
{
matrix c;
memset(c.a, 0, sizeof(c.a));
c.n = a.n;
c.m = b.m;
// cerr<<c.n<<' '<<c.m<<endl
for (int i = 1; i <= a.n; i++)
{
for (int j = 1; j <= a.m; j++)
{
for (int k = 1; k <= b.m; k++)
{
// cerr << c.a[i][k] << " ";
c.a[i][k] += a.a[i][j] * b.a[j][k] % mod;
c.a[i][k] %= mod;
// cerr << a.a[i][j] << " " << b.a[j][k] << " " << c.a[i][k] << " " << i << " " << j << " " << k << endl;
}
}
}
return c;
}
} e, f;
matrix ksmm(matrix a, int n)
{
matrix ans = a;
memset(ans.a, 0, sizeof(ans.a));
for (int i = 1; i <= ans.n; i++)
ans.a[i][i] = 1;
for (; n; n >>= 1, a = a * a)
if (n & 1)
ans = ans * a;
return ans;
}
int m, k;
int in[1001];
int main()
{
cin >> n >> m >> k;
f.m = e.n = e.m = n;
f.n = 1;
for (int i = 1; i <= n; i++)
cin >> f.a[1][i];
// for(int i=1;i<=n;i++)cout<<f.a[1][n]
long long tmp = ksm(2 * m % mod, mod - 2);
cerr << tmp << endl;
for (int i = 1; i <= m; i++)
{
int x, y;
cin >> x >> y;
++in[x];
++in[y];
e.a[x][y] = tmp;
e.a[y][x] = tmp;
}
// cerr << "!!!" << endl;
for (int i = 1; i <= n; i++)
{
e.a[i][i] = (2 * m - in[i] + mod) % mod * tmp % mod;
// cout << e.a[i][i] << endl;
}
// cerr << "!!!" << endl;
e = ksmm(e, k);
f = f * e;
for (int i = 1; i <= n; i++)
{
cout << f.a[1][i] << 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;
#define rq(x, y) range_query(1, 0, n, x, y)
#define ru(x, y, val) range_update(1, 0, n, x, y, val)
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> it) {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';
}
// using ordered_set = tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update>;
const ll MOD = 1e9 + 7, MOD1 = 998244353LL, MAX = 2e5 + 5;
const char nl = '\n';
const int INF = 2e9 + 5;
int n;
void solve(){
cin >> n;
cout << 100 - (n % 100) << '\n';
}
int main(){
cpu();
int __ = 1;
// cin >> __;
while(__--){
solve();
}
return 0;
} | #include<bits/stdc++.h>
#define pb push_back
#define mod 1000000007
#define endl '\n'
using namespace std;
#define ll long long int
ll ceil_div(ll a, ll b) {
return (a + b - 1) / b;
}
ll maxi(ll a,ll b){
return (a>b)?a:b;
}
ll mini(ll a,ll b){
return (a<b)?a:b;
}
long double nthRoot(ll A, ll N)
{
long double xPre = rand() % 10;
long double eps = 1e-3;
long double delX = INT_MAX;
long double xK;
while (delX > eps)
{
xK = ((N - 1.0) * xPre +
(long double)A/pow(xPre, N-1)) / (long double)N;
delX = abs(xK - xPre);
xPre = xK;
}
return xK;
}
ll powe(ll x, unsigned ll y)
{
ll res = 1; // Initialize result
// Update x if it is more than or
// equal to p
while (y > 0)
{
// If y is odd, multiply x with result
if (y & 1)
res = (res*x);
// y must be even now
y = y>>1; // y = y/2
x = (x*x);
}
return res;
}
bool isInteger( long double value )
{
long double flr = floor( value + 1e-5 );
long double diff = value - flr;
return diff < 1e-5;
}
void solve(){
ll n;
cin>>n;
ll arr[n];
ll i;
ll x=0,y=0;
for(i=0;i<n;i++){
cin>>arr[i];
x+=abs(arr[i]);
y+=abs(arr[i])*abs(arr[i]);
arr[i]=abs(arr[i]);
}
cout<<x<<endl;
cout<<fixed<<setprecision(15)<<(long double)sqrt(y)<<endl;
cout<<*max_element(arr,arr+n);
}
int32_t main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
ll t=1;
// cin>>t;
while(t--){
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define endl "\n"
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {os << "["; for (const auto &v : vec) {os << v << ","; } os << "]"; return os;}
template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) {os << "(" << p.first << ", " << p.second << ")"; return os;}
double h0(long double P, int N, int M, unordered_set<int> &A) {
vector<long double> h(N + M + 1);
for (int i = 0; i <= M; i++) {
h[N + i] = 0;
}
long double sum = 0;
for(int i = N - 1; i >= 0; i--) {
if (A.find(i) != A.end()) {
h[i] = P;
} else {
h[i] = sum / M + 1.0;
}
sum += h[i];
sum -= h[i + M];
}
return h[0];
}
void solve() {
int N, M, K;
cin >> N >> M >> K;
vector<int> A(K), B(100001);
for (int i = 0; i < K; i++) {
cin >> A[i];
B[A[i]]++;
}
int cnt = 0;
for (int i = 0; i < 100001; i++) {
if (B[i]) cnt++; else cnt = 0;
if (cnt >= M) {
cout << -1 << endl;
return;
}
}
unordered_set<int> A_;
for (int i = 0; i < K; i++) {
A_.insert(A[i]);
}
long double ok = 0.0;
long double ng = 8e10;
for (int k = 0; k < 60; k++) {
long double P = (ok + ng) / 2;
if (h0(P, N, M, A_) <= P) {
ng = P;
} else {
ok = P;
}
}
cout << ok << endl;
}
int main() {
#ifdef LOCAL_ENV
cin.exceptions(ios::failbit);
#endif
cin.tie(0);
ios::sync_with_stdio(false);
cout.setf(ios::fixed);
cout.precision(16);
solve();
} | #include <iostream>
#include <stack>
#include <string>
#include <vector>
#pragma warning(disable:4996)
using namespace std;
#define int long long
stack<int>way;
int N,M,K;
double ex[200100];
double ep[200100];
double epex[200100];
vector<pair<int, int>>x;
signed main()
{
ios_base::sync_with_stdio(false);
cin >> N>>M >> K;
int i;
for (i = 0; i < K; i++)
{
int a;
cin >> a;
ep[a] = 1;
}
for (i = N; i <= N + M; i++)
{
ex[i] = 0;
}
double curex = 0, curep = 0,expex=0;
for (i = N - 1; i >= 0; i--)
{
int isb = ep[i] >= 0.99999999;
if (isb)
goto T;
ep[i] = curep;
if (ep[i] <=0.999999999)
{
ex[i] = ((curex))/(M-curep*M)+1;
if(curep>0)
epex[i] = expex / (curep*M)+1;
}
else
{
if (!isb)
{
if (curep > 0)
epex[i] = expex / (curep*M) + 1;
}
}
T:
curex -= ex[i + M]*(1-ep[i+M]);
curep -= ep[i + M]/M;
expex -= epex[i + M]*ep[i+M];
curex += ex[i]*(1-ep[i]);
curep += ep[i]/M;
expex += epex[i]*ep[i];
}
i = 0;
if (ep[i] >= 0.99999999)
cout << -1;
else
{
cout << ((1 - ep[i]) * ex[i] +epex[i]*ep[i]) / (1 - ep[i]);
}
} |
#include <bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define whilet int T;cin>>T;while(T--)
#define clr(a,x) memset(a,x,sizeof(a));
#define ll long long
#define PI 3.1415926535897932384626
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define endl "\n"
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
int d4x[] = {0, 0, 1, -1};
int d4y[] = {1, -1, 0, 0};
int d8x[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int d8y[] = {-1, 0, 1, -1, 1, -1, 0, 1};
const int MOD = (int) 1e9+7;
int h, w;
string s[2005];
ll dp[2005][2005];
ll up[2005][2005], rr[2005][2005], dd[2005][2005];
ll f() {
for(int i = h-1; i >= 0; i--) {
for(int j = w-1; j >= 0; j--) {
if(i == h-1 and j == w-1) {
dp[i][j] = 1;
up[i][j] = 1;
rr[i][j] = 1;
dd[i][j] = 1;
continue;
}
if(s[i][j] == '#') {
dp[i][j] = 0;
up[i][j] = 0;
rr[i][j] = 0;
dd[i][j] = 0;
continue;
}
int res = 0;
if(i+1 < h) res += up[i+1][j];
res %= MOD;
if(j+1 < w) res += rr[i][j+1];
res %= MOD;
if(i+1 < h and j+1 < w) res += dd[i+1][j+1];
res %= MOD;
dp[i][j] = res;
if(i+1 < h) up[i][j] = dp[i][j] + up[i+1][j];
else up[i][j] = dp[i][j];
up[i][j] %= MOD;
if(j+1 < w) rr[i][j] = dp[i][j] + rr[i][j+1];
else rr[i][j] = dp[i][j];
rr[i][j] %= MOD;
if(i+1 < h and j+1 < w) dd[i][j] = dp[i][j] + dd[i+1][j+1];
else dd[i][j] = dp[i][j];
dd[i][j] %= MOD;
}
}
return dp[0][0];
}
int main() {
IO;
cin >> h >> w;
for(int i = 0; i < h; i++) {
cin >> s[i];
}
clr(dp, -1);
cout << f();
return 0;
}
| #include<iostream>
#include<vector>
using namespace std;
typedef long long li;
#define MOD 1000000007
#define rep(i,n) for(int i=0;i<(n);i++)
#define df 0
template<class T> void print(const T& t){ cout << t << "\n"; }
template<class T, class... Ts> void print(const T& t, const Ts&... ts) { cout << t; if (sizeof...(ts)) cout << " "; print(ts...); }
// Container コンテナ型, map以外
template< template<class ...> class Ctn,class T>
std::ostream& operator<<(std::ostream& os,const Ctn<T>& v){
auto itr=v.begin();
while(itr!=v.end()){
if(itr!=v.begin())cout << " ";
cout << *(itr++);
}
return os;
}
struct modint{
li num;
modint(li a=0){
num=(a%MOD+MOD)%MOD;
}
modint operator+(modint a) {return{(num+a.num)%MOD};}
modint operator-(modint a) {return{((num-a.num)%MOD+MOD)%MOD};}
modint operator*(modint a) {return{num*a.num%MOD};}
modint inv(const modint& a);
modint operator/(modint a) {return (*this)*inv(a);}
modint& operator+=(modint a) {
(num += a.num)%=MOD;
return *this;
}
modint& operator-=(modint a) {
(num += MOD-a.num)%=MOD;
return *this;
}
modint& operator*=(modint a) {
(num*=a.num)%=MOD;
return *this;
}
};
std::ostream& operator<<(std::ostream& os, const modint& m){
// ここでストリームに obj を書き込みます。
li a=m.num;
a%=MOD;
a+=MOD;
a%=MOD;
cout << a;
return os;
}
std::istream& operator>>(std::istream& os,modint& m){
// ここでストリームに obj を書き込みます。
li a; cin >>a;
m=a;
return os;
}
int main(){
int h,w; cin >>h >>w;
vector<string> mp(h);
rep(i,h) cin >>mp[i];
auto judge=
[&](int i,int j){
if(i<0 or j<0) return 0;
if(mp[i][j]=='#') return 0;
return 1;
};
vector<vector<modint>> dp(h,vector<modint>(w,0));
vector<vector<modint>> tt(h,vector<modint>(w,0));
vector<vector<modint>> yk(h,vector<modint>(w,0));
vector<vector<modint>> nn(h,vector<modint>(w,0));
dp[0][0]=tt[0][0]=yk[0][0]=nn[0][0]=1;
rep(i,h) rep(j,w){
if(!judge(i,j))continue;
int ii=i-1,jj=j-1;
if(df)print("i,j",i,j);
if(judge(ii,jj)) dp[i][j]+=nn[ii][jj];
if(df)if(judge(ii,jj)) print("nn",nn[ii][jj],dp[i][j]);
if(judge(i,jj)) dp[i][j]+=yk[i][jj];
if(df)if(judge(i,jj)) print("yk",yk[i][jj],dp[i][j]);
if(judge(ii,j)) dp[i][j]+=tt[ii][j];
if(df)if(judge(ii,j)) print("tt",tt[ii][j],dp[i][j]);
tt[i][j]=yk[i][j]=nn[i][j]=dp[i][j];
if(judge(ii,jj)) nn[i][j]+=nn[ii][jj];
if(judge(i,jj)) yk[i][j]+=yk[i][jj];
if(judge(ii,j)) tt[i][j]+=tt[ii][j];
if(df){
if(df)print("dp");
rep(i,h) print(dp[i]);
if(df)print("tt");
rep(i,h) print(tt[i]);
if(df)print("yk");
rep(i,h) print(yk[i]);
if(df)print("nn");
rep(i,h) print(nn[i]);
}
}
if(df){
if(df)print("dp");
rep(i,h) print(dp[i]);
if(df)print("tt");
rep(i,h) print(tt[i]);
if(df)print("yk");
rep(i,h) print(yk[i]);
if(df)print("nn");
rep(i,h) print(nn[i]);
}
print(dp[h-1][w-1]);
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll; //int:2*10**9
typedef long double ld;
typedef pair<ll,ll> P;
#define REP(i,n) for(ll i = 0; i<(ll)(n); i++)
#define FOR(i,a,b) for(ll i=(a);i<=(b);i++)
#define FORD(i,a,b) for(ll i=(a);i>=(b);i--)
#define pb push_back
#define MOD 1000000007 //998244353
#define PI 3.141592653
#define INF 100000000000000 //14
//cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);
#define N 100100
vector<vector<ll>> conn(N);
vector<vector<ll>> ansc(N,vector<ll>(3,0));
void dfs(ll now, ll prev) {
ll first = 1; ll second = 0; ll third = 1;
ll secondf = 0; ll seconds = 0;
vector<vector<ll>> ones;
for (ll nex : conn[now]) {
// cout << now << " " << nex << endl;
if (nex==prev) continue;
dfs(nex,now);
ll pfirst = ansc[nex][0];
ll psecond = ansc[nex][1];
ll pthird = ansc[nex][2];
third += pthird;
if (pthird==0) {
if (pfirst<=psecond) {
first += pfirst;
second += psecond;
}
else {
secondf += pfirst;
seconds += psecond;
}
}
else {
ones.pb({pfirst-psecond,pfirst,psecond});
}
}
third%=2;
// cout << now << " " << first << " " << second << " " << third << endl;
if (third%2) {
first += secondf; second += seconds;
}
else {
second += secondf; first += seconds;
}
sort(ones.begin(),ones.end());
REP(i,ones.size()) {
if (i%2==0) {
first += ones[i][1]; second += ones[i][2];
}
else {
first += ones[i][2]; second += ones[i][1];
}
}
ansc[now]={first,second,third};
return;
}
int main(){
ll n; cin >> n;
REP(i,n-1) {
ll p; cin >> p; p--;
conn[i+1].pb(p); conn[p].pb(i+1);
}
dfs(0,-1);
// REP(i,n) {
// cout << ansc[i][0] << " " << ansc[i][1] << " " << ansc[i][2] << endl;
// }
cout << ansc[0][0] << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define int long long
#define For(i,a,b) for(register int i=a;i<=b;++i)
#define Down(i,a,b) for(register int i=a;i>=b;i--)
#define reg register
namespace yspm{
inline int read(){
int res=0,f=1; char k;
while(!isdigit(k=getchar())) if(k=='-') f=-1;
while(isdigit(k)) res=res*10+k-'0',k=getchar();
return res*f;
}
const int N=1e5+10;
inline int min(int x,int y){return x<y?x:y;}
inline int max(int x,int y){return x>y?x:y;}
inline void swap(int &x,int &y){int t=x; x=y; y=t; return ;}
vector<int>g[N];
int sz[N],f[N],n;
inline void dfs(int x){
sz[x]=1; vector<int> ev,od; f[x]=-1;
for(auto t:g[x]) dfs(t),sz[x]^=sz[t],sz[t]?od.push_back(f[t]):ev.push_back(f[t]);
sort(od.begin(),od.end()); reverse(od.begin(),od.end());
sort(ev.begin(),ev.end()); reverse(ev.begin(),ev.end());
int ne=0,no=0,fl=0;
while(ne<ev.size()&&ev[ne]>0) f[x]+=ev[ne],++ne;
while(no<od.size()) fl?f[x]-=od[no]:f[x]+=od[no],++no,fl^=1;
while(ne<ev.size()) fl?f[x]-=ev[ne]:f[x]+=ev[ne],++ne;
return ;
}
signed main(){
n=read(); for(reg int i=2,tf;i<=n;++i) tf=read(),g[tf].push_back(i); dfs(1);
printf("%lld\n",(n-f[1])/2);
return 0;
}
}
signed main(){return yspm::main();}
|
#define _GIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
int main(void) {
ll N;
cin >> N;
ll num = 2 << (N-1);
vector<pair<int, int>> A(num);
rep(i, num) {
A[i].first = i + 1;
cin >> A[i].second;
}
num /= 2;
for (int i = 0; i < N - 1; ++i) {
for (int j = 0; j < num; ++j) {
if (A[j * 2].second > A[j * 2 + 1].second) {
A[j].first = A[j * 2].first;
A[j].second = A[j * 2].second;
} else {
A[j].first = A[j * 2 + 1].first;
A[j].second = A[j * 2 + 1].second;
}
}
}
if (A[0].second < A[1].second) {
cout << A[0].first << endl;
} else {
cout << A[1].first << endl;
}
return 0;
} | #include<bits/stdc++.h>
#define ll long long
#define inf 1e18
#define mod 1000000007
using namespace std;
int main()
{
ll n;
cin>>n;
vector<ll> a;
n=pow(2,n);
a.resize(n);
unordered_map<ll,ll> ump;
for(ll p=0;p<n;p++)
{
cin>>a[p];
ump[a[p]]=p;
}
ll max1=0;
ll max2=0;
for(ll p=0;p<n/2;p++)
{
max1=max(max1,a[p]);
}
for(ll p=n/2;p<n;p++)
{
max2=max(max2,a[p]);
}
// cout<<min(max1,max2)<<endl;
cout<<ump[min(max1,max2)]+1<<endl;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pint;
typedef vector<int> vi;
typedef vector<ll> vll;
#define all(v) v.begin(), v.end()
#define rep(i,s,n) for(int i=(int)(s);i<(int)(n);i++)
#define lep(i,s,n) for(ll i=(ll)(s);i<(ll)(n);i++)
#define vep(v) for(auto&& x : v)
#define sz(s) (int)(s.size())
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 graph = vector<vector<int>>;
int main(){
string s;
cin >> s;
int n = sz(s), cnt = 0, ans;
vi p(3);
rep(i, 0, n){
int q = s[i]-'0';
q%=3;
cnt += q;
p[q]++;
}
cnt%=3;
if(cnt==0){
cout << 0 << endl;
return 0;
}
if(cnt==2){
if(p[2])ans = 1;
else if(p[1]>=2)ans = 2;
else ans = -1;
}
else if(cnt==1){
if(p[1])ans = 1;
else if(p[2]>=2)ans = 2;
else ans = -1;
}
if(ans>=n)cout << -1 << endl;
else cout << ans << endl;
} | #include <iostream>
#include <vector>
using namespace std;
int main() {
string s;
cin >> s;
vector<int>a(10,0);
int sum=0;
for(int i=0; i<s.size(); i++){
sum += s[i]-'0';
a[s[i]-'0']++;
}
if(sum%3==0){
cout << 0 << endl;
return 0;
}else if(sum%3==1){
if(s.size()==1){
puts("-1");
}else if(a[1]||a[4]||a[7]){
cout << 1 << endl;
}else if(s.size()==2){
puts("-1");
}else if(a[2]>=1&&a[5]>=1){
cout << 2 << endl;
}else if(a[2]>=1&&a[8]>=1){
cout << 2 << endl;
}else if(a[8]>=1&&a[5]>=1){
cout << 2 << endl;
}else if(a[2]>=2){
cout << 2 << endl;
}else if(a[5]>=2){
cout << 2 << endl;
}else if(a[8]>=2){
cout << 2 << endl;
}else{
puts("-1");
}
}else{
if(s.size()==1){
puts("-1");
}else if(a[2]||a[5]||a[8]){
cout << 1 << endl;
}else if(s.size()==2){
puts("-1");
}else if(a[1]>=1&&a[4]>=1){
cout << 2 << endl;
}else if(a[1]>=1&&a[7]>=1){
cout << 2 << endl;
}else if(a[4]>=1&&a[7]>=1){
cout << 2 << endl;
}else if(a[1]>=2){
cout << 2 << endl;
}else if(a[4]>=2){
cout << 2 << endl;
}else if(a[7]>=2){
cout << 2 << endl;
}else{
puts("-1");
}
}
return 0;
} |
#include<bits/stdc++.h>
using i28 = __int128_t;
using namespace std;
// using i28 = long long;
using pint = pair<i28,i28>;
const i28 G = 10000;
const i28 lim = 2e9;
// #include"debug.hpp"
ostream &operator<<(ostream &dest, __int128_t value) {
ostream::sentry s(dest);
if (s) {
__uint128_t tmp = value < 0 ? -value : value;
char buffer[128];
char *d = std::end(buffer);
do {
--d;
*d = "0123456789"[tmp % 10];
tmp /= 10;
} while (tmp != 0);
if (value < 0) {
--d;
*d = '-';
}
int len = std::end(buffer) - d;
if (dest.rdbuf()->sputn(d, len) != len) {
dest.setstate(ios_base::badbit);
}
}
return dest;
}
__int128 parse(string &s) {
__int128 ret = 0;
for (int i = 0; i < (int)s.length(); i++)
if ('0' <= s[i] && s[i] <= '9')
ret = 10 * ret + s[i] - '0';
return ret;
}
istream &operator>>(istream &is, __int128_t &a){
string s;
is>>s;
a = parse(s);
return (is);
}
// using Int = __int128_t;
i28 in() {
string s; cin >> s;
auto itr = find(s.begin(), s.end(), '.');
if(itr == s.end()) return stoi(s) * G;
i28 idx = s.end() - itr;
s.erase(itr);
if(count(s.begin(), s.end(), '0') == (i28)s.size()) return 0;
while(s.front() == '0') s.erase(s.begin());
i28 x = stoi(s);
i28 coef = 1;
for(i28 i = 0; i < 5 - idx; i++)coef *= 10;
return x * coef;
}
i28 f(i28 rem, i28 y) {
i28 lower, upper;
{ //lower
i28 lef = -1e18, rig = y; //xxxxooo
while(rig - lef > 1) {
i28 mid = (lef + rig) / 2;
(mid * (mid - 2 * y) <= rem ? rig : lef) = mid;
}
lower = rig;
}
{
i28 lef = y, rig = 1e18; //oooooxxx
while(rig - lef > 1) {
i28 mid = (lef + rig) / 2;
(mid * (mid - 2 * y) <= rem ? lef : rig) = mid;
}
upper = lef;
}
auto myci = [&](i28 num) {
if(num <= 0) return -(-num + G - 1) / G;
else return num / G;
};
i28 lownum = myci(lower-1);
i28 uppnum = myci(upper);
// print(lower, upper, lownum, uppnum);
return uppnum - lownum;
}
int main()
{
i28 x, y, r;
x = in();
y = in();
r = in();
// print(x, y, r);
i28 r2 = (i28)r * r;
i28 num = 0;
for(i28 t1 = -lim; t1 <= lim; t1 += G) {
i28 xa = (i28)(t1 - x) * (t1 - x);
if(xa > r2) continue;
// print(t1);
i28 rem = r2 - xa;
// print(rem);
rem -= (i28)y * y;
num += f(rem, y);
// print(num, rem);
}
cout << num << endl;
} | #include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <cstdio>
#include <string>
#include <cmath>
#include <queue>
#include <tuple>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <random>
#include <set>
#include <stack>
#include <time.h>
#include <unordered_map>
//#include <bits/stdc++.h>
#define maxs(x,y) x = max(x,y)
#define mins(x,y) x = min(x,y)
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define repr(i, n) for (int i = (n) - 1; i >= 0; i--)
#define FOR(i,i0,n) for(int (i)=(i0);(i)<(n);(i)++)
#define FORR(i,i0,n) for(int (i)=(n)-1; (i)>=(i0);(i)--)
#define SORT(x) sort(x.begin(),x.end())
#define SORTR(x) sort(x.begin(),x.end(),greater<int>())
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
using namespace std;
using ll = long long;
typedef std::pair<int, int> pii;
typedef std::pair<int, double> pid;
typedef std::vector<int> vi;
typedef std::vector<pii> vii;
#define PI 3.14159265358979323846264338327950L
const int mod = 1000000007;
struct container{
int bottom, top, id;
container(int id, int bottom, int top): top(top), bottom(bottom),id(id){
}
};
struct Data {
int ind;
ll time;
Data(int ind,ll time): ind(ind),time(time){}
bool operator<(const Data& a) const {
return time > a.time;
}
};
void solve(){
int n;
cin >> n;
string s;
cin >> s;
n = s.size();
int cnt = 0;
string cur;
for(int i = 0; i < n; i++){
cur += s[i];
while (cur.size()>=3 && cur.substr(cur.size()-3) == "fox"){
cur.pop_back();
cur.pop_back();
cur.pop_back();
cnt++;
}
}
cout << n-cnt*3;
}
int main() {
int T = 1;
//cin >> T;
while (T--){
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
typedef int int2;
#define int long long
#define pi pair<int, int>
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define f first
#define s second
const int inf = 1e18;
int t;
const int maxn = 2e5 + 5;
int a[maxn], suf[maxn];
int2 main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for (int i = 1;i <= n; i++) {
cin >> a[i];
}
sort(a + 1, a + n + 1);
suf[n] = a[n];
for (int i = n - 1; i >= 1; i--) {
suf[i] = suf[i + 1] + a[i];
}
int ans = 0;
for (int i = 1; i < n; i++) {
ans += suf[i + 1] - (n - i) * a[i];
}
cout << ans << "\n";
} | #include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
int n;
cin>>n;
ll ans=0;
while(n--)
{
ll a,b;
cin>>a>>b;
ans+=b*(b+1)/2-a*(a-1)/2;
}
cout<<ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, x, y;
cin >> a >> b >> x >> y;
const int y_ = min(y, x + x);
a = 2 * a;
b = 2 * b + 1;
int d = abs(a - b);
cout << d / 2 * y_ + d % 2 * x << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
template <typename T> using vec = vector<T>;
template <typename T> using vvec = vector<vector<T>>;
template <typename T> using lqueue = priority_queue<T, vector<T>, greater<T>>;
template <typename T> using gqueue = priority_queue<T, vector<T>, less<T>>;
int main() {
constexpr int A = 0, B = 1;
int a, b, x, y; cin >> a >> b >> x >> y; --a; --b;
auto edges = [=](int b, int n) -> array<array<int, 3>, 4> {
if (b == A) {
return {{
{ x, B, n }, { x, B, n - 1 }, { y, A, n - 1 }, { y, A, n + 1 },
}};
}
else {
return {{
{ x, A, n }, { x, A, n + 1 }, { y, B, n - 1 }, { y, B, n + 1 },
}};
}
};
array<array<int, 100>, 2> dist;
lqueue<array<int, 3>> que;
for (auto &e1 : dist) for (auto &e2 : e1) e2 = -1;
que.push({ 0, A, a });
while (!que.empty()) {
auto [c, b, n] = que.top(); que.pop();
if (dist[b][n] != -1) continue;
dist[b][n] = c;
for (auto [nc, nb, nn] : edges(b, n)) {
if (nn < 0 || 99 < nn) continue;
que.push({ c + nc, nb, nn });
}
}
cout << dist[B][b] << endl;
}
|
/*
** author : Dhiraj Govindvira
** date : 08 May 2021 ~ Saturday
** time : 05:35:49 PM
*/
#include <bits/stdc++.h>
using namespace std;
#ifdef DHIRAJ
#include "D:/dhiraj/Programming/debug.h"
#else
#define dbg(...) 1
#define cerr if(0) cerr
#endif
using ll = long long int;
#define endl '\n'
template <typename T, typename U>
inline istream & operator >> (istream &in, pair<T, U> &p) {
in >> p.first >> p.second;
return in;
}
template <typename T>
inline istream & operator >> (istream &in, vector<T> &v) {
for(T &x : v) in >> x;
return in;
}
// count total non empty subset with given sum
const ll MX = 202;
ll n;
ll a[MX];
ll dp[MX][MX];
const ll MOD = 200;
inline ll md(ll x, ll M = MOD)
{
x %= M;
return ((x < 0) ? (x + M) : x);
}
ll rec(ll item, ll sum)
{
assert(sum < 200);
if(item == 0) return sum == 0;
ll &ans = dp[item][sum];
if(ans != -1) return ans;
ans = 0;
ans += rec(item - 1, md(sum - a[item]));
ans += rec(item - 1, sum);
ans = min(5ll, ans);
return ans;
}
void f1(ll item, ll sum, vector<ll> &v)
{
if(item == 0) {
return;
}
if(v.empty() and rec(item - 1, a[item]) > 1) {
v.push_back(item);
f1(item - 1, md(sum - a[item]), v);
}
else if(rec(item - 1, sum)) {
f1(item - 1, sum, v);
}
else {
v.push_back(item);
f1(item - 1, md(sum - a[item]), v);
}
}
void f2(ll item, ll sum, vector<ll> &v)
{
if(item == 0) {
return;
}
if(rec(item - 1, md(sum - a[item]))) {
v.push_back(item);
f2(item - 1, md(sum - a[item]), v);
}
else {
f2(item - 1, sum, v);
}
}
void solve(ll &tc)
{
cin >> n;
for(ll i = 1; i <= n; i++) {
cin >> a[i];
a[i] = md(a[i]);
}
memset(dp, -1, sizeof(dp));
for(ll i = 0; i < 200; i++)
{
ll cnt = rec(n, i);
assert(cnt >= 0);
if(i == 0) cnt--;
if(cnt > 1)
{
vector<ll> b, c;
f1(n, i, b);
f2(n, i, c);
ll sm1 = 0;
for(ll x : b) {
sm1 += a[x];
sm1 = md(sm1);
}
ll sm2 = 0;
for(ll x : c) {
sm2 += a[x];
sm2 = md(sm2);
}
sort(b.begin(), b.end());
sort(c.begin(), c.end());
if(!b.empty() and !c.empty() and b != c and sm1 == i and sm2 == i) {
cout << "Yes\n";
cout << b.size() << " ";
for(auto &X : b) {
cout << X << " ";
}
cout << endl;
cout << c.size() << " ";
for(auto &X : c) {
cout << X << " ";
}
cout << endl;
return;
}
}
}
cout << "No\n";
}
int main()
{
#ifdef DHIRAJ
freopen("D:/dhiraj/Programming/i1.txt", "r", stdin);
freopen("D:/dhiraj/Programming/o1.txt", "w", stdout);
freopen("D:/dhiraj/Programming/e1.txt", "w", stderr);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
ll rep = 3;
while(rep--) {
ll tc = 1;
for(ll i = 1; i <= tc; i++) {
cerr << "Case #" << i << "\n";
solve(i);
}
if(dbg()) break;
}
return 0;
} | #include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
#define fr(i, a, b) for (ll i = a; i < b; i++)
#define rf(i, a, b) for (ll i = a; i >= b; i--)
typedef std::vector<long long> vi;
#define F first
#define S second
#define fast \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0);
#define mod 1000000007
#define PB push_back
#define MP make_pair
#define PI 3.14159265358979323846
#define all(a) a.begin(), a.end()
#define mx(a) *max_element(all(a))
#define mn(a) *min_element(all(a))
const ll INF = LLONG_MAX / 2;
const ll N = 2e5 + 1;
using namespace std;
void solve()
{
ll n;
cin >> n;
ll a[n];
map<ll, ll> mp;
ll ans = -1;
fr(i, 0, n)
{
cin >> a[i];
a[i] = a[i] % 200;
mp[a[i]]++;
if (mp[a[i]] > 1)
{
ans = a[i];
break;
}
}
if (ans != -1)
{
cout << "Yes\n";
ll co = 0;
fr(i, 0, n)
{
if (a[i] == ans)
{
co++;
cout << "1 " << i + 1 << "\n";
if (co == 2)
return;
}
}
}
else
{
map<ll, vi> mp;
for (ll i = 0; i < min((2 << n), 201); i++)
{
vi subset;
ll sum = 0;
fr(j, 0, n)
{
ll c = 1 << j;
if (c & i)
{
subset.PB(j);
sum = (sum + a[j]) % 200;
}
}
if (subset.size() == 0)
continue;
if (mp.find(sum) == mp.end())
mp[sum] = subset;
else if (subset != mp[sum])
{
// cout << sum << "\n";
cout << "Yes\n";
cout << mp[sum].size() << " ";
fr(i, 0, mp[sum].size())
cout
<< mp[sum][i] + 1 << " ";
cout << "\n";
cout << subset.size() << " ";
fr(i, 0, subset.size())
cout
<< subset[i] + 1 << " ";
cout << "\n";
return;
}
}
cout << "No\n";
return;
}
}
int main()
{
fast;
ll t = 1;
// std::cin >> t;
while (t--)
{
solve();
}
} |
#include<bits/stdc++.h>
typedef int64_t i64;
typedef long double f128;
using namespace std;
template<typename T>
void scan(T& n)
{
cin>>n;
return;
}
void scan()
{
return;
}
template<typename T,class... Args>
void scan(T& n,Args&... args)
{
scan(n);
scan(args...);
return;
}
template<typename T>
void scan_array(T start,T end)
{
T now=start;
for(;now!=end;++now)
{
scan(*now);
}
return;
}
template<typename T>
void print(T n)
{
cout<<n;
return;
}
template<typename T>
void println(T n)
{
print(n);
print('\n');
return;
}
template<typename T,class... Args>
void println(T n,Args... args)
{
print(n);
print(' ');
println(args...);
return;
}
template<typename T>
void print_array(T start,T end)
{
T now=start;
print(*now);
++now;
for(;now!=end;++now)
{
print(' ');
print(*now);
}
print('\n');
return;
}
i64 pow_mod(i64 a,i64 n,i64 mod)
{
i64 res=1,now=a;
while(n)
{
if(n&1)
{
res=res*now%mod;
}
now=now*now%mod;
n>>=1;
}
return res;
}
i64 inv_mod(i64 a,i64 mod)
{
return pow_mod(a,mod-2,mod);
}
i64 power(i64 a,i64 n)
{
i64 res=1;
for(i64 i=0;i<n;++i)
{
res*=a;
}
return res;
}
bool is_prime(i64 N)
{
if(N<2)
{
return 0;
}
for(i64 i=2;i*i<=N;++i)
{
if(N%i==0)
{
return 0;
}
}
return 1;
}
struct combination
{
vector<i64> fact,inv,fact_inv;
i64 mod=-1;
combination(int Max,const i64 mod_):fact(Max+1,1),inv(Max+1,1),fact_inv(Max+1,1)
{
mod=mod_;
for(i64 i=2;i<=Max;++i)
{
fact[i]=fact[i-1]*i%mod;
inv[i]=mod-(mod/i*inv[mod%i]%mod);
fact_inv[i]=fact_inv[i-1]*inv[i]%mod;
}
}
i64 nCk(i64 n,i64 k)
{
if(n<k)
{
return 0;
}
return fact[n]*fact_inv[k]%mod*fact_inv[n-k]%mod;
}
};
i64 comb(i64 n,i64 k)
{
if(n<k)
{
return 0;
}
i64 res=1;
for(i64 i=1;i<=k;++i)
{
res*=n-i+1;
res/=i;
}
return res;
}
int main()
{
string S;
scan(S);
reverse(S.begin(),S.end());
for(size_t i=0;i<S.size();++i)
{
if(S[i]=='6')
{
S[i]='9';
continue;
}
if(S[i]=='9')
{
S[i]='6';
}
}
println(S);
return 0;
} | #include <iostream>
using namespace std;
typedef long long ll;
const ll INF=2e18;
ll clamp(ll x, ll min, ll max) {
if (x>max) {
return max;
}
if (x<min) {
return min;
}
return x;
}
int main() {
ll n;
cin>>n;
ll low=-INF;
ll high=INF;
ll bias=0;
for (ll i=1; i<=n; i++) {
ll a,t;
cin>>a>>t;
if (1==t) {
low+=a;
high+=a;
bias+=a;
} else if (2==t) {
low=max(low, a);
high=max(high,a);
} else {
low=min(low, a);
high=min(high, a);
}
}
ll q;
cin>>q;
for (ll i=1; i<=q; i++) {
ll x;
cin>>x;
cout<<clamp(x+bias, low, high)<<"\n";
}
return 0;
} |
/*
@author : ashnove
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define lld long double
#define F first
#define S second
#define Th third
// #define endl "\n"
#define nl cout << endl;
#define pb push_back
#define f(i, a, b) for (ll i = a; i < b; i++)
#define what_is(x) cerr << #x << " is " << x << endl;
#define ar vector<ll>
#define mat vector<vector<ll>>
mat cn(ll n, ll m) {return vector<vector<ll>>(n, vector<ll>(m));}
bool comp1(char &allVal, char &s2) { return allVal > s2; }
bool comp2(const pair<ll, pair<ll, ll>> &a, const pair<ll, pair<ll, ll>> &b) {
if (a.first > b.first)
return 1;
if (a.first == b.first && a.S.S > b.S.S)
return 1;
return 0;
}
class Pair { public: ll first, second;};
bool comp3(const Pair &a, const Pair &b) {
if (a.F > b.F) return 1; return 0;
}
class Trips { public: ll first, second, third;};
bool comp4(const Trips &a, const Trips &b) {
if (a.Th > b.Th) return 1; return 0;
}
ll const mod = 1e9 + 7;
ll const inf = 1e9;
ll const maxn = 5e5 + 1;
void solve()
{
ll t = 1;
// cin >> t;
for (ll tt = 1; tt <= t; tt++) {
string s; cin >> s;
ll rev = 0;
deque<char> ans;
for(ll i = 0; i < s.size(); i++){
if(s[i] == 'R') rev^=1;
else{
if(rev){
ans.push_front(s[i]);
}
else ans.push_back(s[i]);
}
}
string res = "";
if(!rev){
map<char,ll> used;
while(!ans.empty()){
// if(used[ans.front()]) {ans.pop_front();continue;}
used[ans.front()] = 1;
res += ans.front();
ans.pop_front();
}
}else{
map<char,ll> used;
while(!ans.empty()){
// if(used[ans.back()]) {ans.pop_back();continue;}
used[ans.back()] = 1;
res += ans.back();
ans.pop_back();
}
}
stack<char> ss;
for(ll i = 0; i < res.size(); i++){
if(ss.empty())
ss.push(res[i]);
else{
bool ok = 0;
while(!ss.empty() && ss.top() == res[i])
ss.pop(), ok = 1;
if(!ok) ss.push(res[i]);
}
}
string pp = "";
while(!ss.empty())
{
pp+=ss.top(); ss.pop();
}
for(ll i = pp.size()-1; i >= 0; i--)
cout <<pp[i];
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
//*
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif/**/
solve();
// cout << "Case #" <<tt<<": ";
// printf("TIME: %.3lf\n", double(clock()) / CLOCKS_PER_SEC);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
#define PI 3.14159265359
#define mod 1000000007
#define ALL(a) (a).begin(),(a).end()
int main(){
string s;
cin >> s;
string a = "";
string b = "";
int count = 0;
rep(i,s.size()){
if(s[i] == 'R'){
count++;
continue;
}
if(count % 2 == 0){
a+=s[i];
}
else{
b+=s[i];
}
}
reverse(ALL(b));
b = b + a;
if(count % 2 == 1)reverse(ALL(b));
string ans = "";
rep(i,b.size()){
ans += b[i];
if(ans[ans.size() -1] == ans[ans.size() -2] ){
ans.pop_back();
ans.pop_back();
}
}
cout << ans << endl;
} |
#include <iostream>
#include <numeric>
#include <vector>
#include <algorithm>
#include <iostream>
#include <functional>
#include <cstdlib>
#include <cmath>
#include <utility>
#include <cmath>
long long FindCentral(std::vector<std::vector<long long>> &A, long long K, long long si, long long sj, std::vector<long long> &AK)
{
long long index = 0;
for(long long i = si; i < (si + K); i++){
for(long long j = sj; j < (sj + K); j++){
AK[index] = A[j][i];
index++;
}
}
std::sort(AK.begin(), AK.end());
//std::cout << AK[(K*K)/2 -1] << std::endl;
long long t = 0;
if((((K*K)) % 2) == 0){
t = -1;
}
return AK[(K*K)/2 + t];
}
long long FindCentral2(std::vector<std::vector<long long>> &A, long long K, long long si, long long sj, std::vector<long long> &AK, long long & index)
{
for(long long j = sj; j < (sj + K); j++){
AK[index] = A[j][si];
index++;
if(index == K*K){
index = 0;
}
}
std::sort(AK.begin(), AK.end());
for(long long i = 0 ; i < K*K; i++){
std::cout << AK[i] << " ";
}
long long t = 0;
if((((K*K)) % 2) == 0){
t = -1;
}
std::cout << AK[(K*K)/2 + t] << std::endl;
return AK[(K*K)/2 + t];
}
long long find_max_dp(long long N, std::vector<long long> &num_list, long long MAX_NUM) {
int tmp_choice, tmp_not_choice;
std::vector<std::vector<long long>> table(N, std::vector<long long>(MAX_NUM+1, 0));
// DP表の一番上(i = 0)を初期化
for(int j = 0; j <= MAX_NUM; j += 1) {
if(num_list[0] > j) {
table[0][j] = 0; // 何も入れられないとき
}
else {
table[0][j] = num_list[0]; // 0番目の数字を足せるとき
}
}
for(int i = 1; i < N; i += 1) {
for(int j = 0; j <= MAX_NUM; j += 1) {
tmp_not_choice = table[i-1][j];
if(num_list[i] > j) {
table[i][j] = tmp_not_choice;
}
else {
tmp_choice = table[i-1][j - num_list[i]] + num_list[i];
if(tmp_choice >= table[i-1][j])
table[i][j] = tmp_choice;
else
table[i][j] = tmp_not_choice;
}
}
}
return table[N-1][MAX_NUM];
}
int main()
{
long long N;
std::cin >> N;
long long Tsum = 0;
std::vector<long long> T(1001, 0);
std::vector<long long> T2(N, 0);
for(long long i = 0; i < N; i++){
long long temp;
std::cin >> temp;
T2[i] = temp;
Tsum += temp;
T[temp]++;
}
std::sort(T2.begin(), T2.end());
long long ans = find_max_dp(N, T2, Tsum/2);
ans = Tsum - ans;
std::cout << ans << std::endl;
return 0;
}
| #include<bits/stdc++.h>
#define forn(i,a,b)for(int i=(a),_b=(b);i<=_b;i++)
#define fore(i,b,a)for(int i=(b),_a=(a);i>=_a;i--)
#define rep(i,n)for(int i=0,_n=n;i<n;i++)
#define ll long long
#define pii pair<int,int>
#define m_p make_pair
#define pb push_back
#define fi first
#define se second
#define foreach(i,c) for(__typeof(c.begin())i=(c.begin());i!=(c).end();i++)
using namespace std;
const int N=110;
int n;
int a[N];
bool dp[N][N*2000];
int sum=0;
int main(){
ios::sync_with_stdio(0);
cin>>n;
forn(i,1,n)cin>>a[i];
forn(i,1,n)sum+=a[i];
dp[0][100000]=1;
forn(i,1,n){
rep(j,200001){
if(dp[i-1][j]){
dp[i][j+a[i]]=dp[i][j-a[i]]=1;
}
}
}
forn(d,0,100000){
if(dp[n][100000+d]||dp[n][100000-d]){
cout<<(sum+d)/2;
return 0;
}
}
return 0;
}
|
#include <algorithm>
#include <cmath>
#include <climits>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <unordered_set>
#include <vector>
#define REP(i, n) for(int i = 0; i < n; ++i)
using namespace std;
using LLONG = long long;
const LLONG MOD = 998244353;
// 番号が若い順から順に0にしていくと
// 偶数番・奇数番をそれぞれ独立に足し合わせたものが残っていく
// -> 偶数番の合計と奇数番の合計が等しくなればよい
// -> 偶数番と奇数番の差を計算し,等しくなればよい
int main()
{
int N; cin >> N;
vector<LLONG> as(N); REP(i, N) cin >> as[i];
vector<LLONG> diffs(N);
diffs[0] = as[0];
for(int i = 1; i < N; ++i)
{
if (i % 2 == 0) diffs[i] = diffs[i - 1] + as[i];
else diffs[i] = diffs[i - 1] - as[i];
}
map<LLONG, LLONG> diff2NumMap;
for (LLONG diff : diffs) ++diff2NumMap[diff];
LLONG ans = 0;
++diff2NumMap[0];
for (const auto& kvp : diff2NumMap)
{
LLONG num = kvp.second;
ans += num * (num - 1) / 2;
}
cout << ans << endl;
}
| #include<bits/stdc++.h>
#include <iostream>
#include <stack>
#include <list>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pi=pair<int,int>;
using pl=pair<ll,ll>;
#define pb push_back
#define f first
#define s second
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define repd(i,b,a) for(ll i=b;i>a;i--)
#define repi(i,b,a,c) for(ll i=b;i<a;i+=c)
#define cas ll cas;cin>>cas ; for(ll i=0;i<cas;i++)
const ll N=1e5 + 10;
const ll INF=1e18;
const ll M=1e5 +10;
const ll mod=1e9 + 7;
const ll L=31;
const ll hp=293;
ll _pow(ll a,ll b){
if(b==0){
return 1;
}
if(b==1){
return a%mod;
}
ll k=_pow(a,b/2);
k%=mod;
k=k*k;
k%=mod;
if(b%2==1){
k*=a;
k%=mod;
}
return k;
}
bool prime[N];
ll seive(){
for(ll i=0;i<N;i++){
prime[i]=true;
}
for(ll i=2;i<=sqrt(N);i++){
if(prime[i]){
for(ll j=i*i;j<N;j+=i){
prime[j]=false;
}
}
}
}
ll solve(){
// seive();
ll n;
cin>>n;
// ll cnt=0;
set<ll>st;
for(ll i=2;i<=sqrt(n);i++){
// if(prime[i]){
// cout<<i<<" ";
ll p=i*i;
while(p<=n){
st.insert(p);
p=p*i;
// cnt++;
}
// }
}
ll sz=st.size();
cout<<(n-sz)<<'\n';
return 0;
}
signed main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
//solve();
// cas{
solve();
// }
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define ss second
#define ff first
#define ub upper_bound
#define lb lower_bound
const ll M=1000000007;
#define inf 1000000000000000000
#define infn -1000000000000000000
ll ncr(int n,int r){if(r>n)return 0;if(r > n - r) r = n - r;ll ans = 1;
int i;for(i = 1; i <= r; i++){ans *= n - r + i;ans /= i;}return ans;}
ll power(ll x,ll n,ll m){if(n==0)return 1;else if(n%2==0)return (power(((x%m)*(x%m))%m,n/2,m))%m;else return ((x%m)*(power(x,n-1,m)%m))%m;}
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
ll modInverse(ll a,ll m){ll m0=m;ll y=0,x=1;if(m==1)return 0;while(a>1){
ll q=a/m;ll t=m;m=a%m,a=t;t=y;y=x-q*y;x=t;}if(x<0)x+=m0;return x;}
// std::rotate(vec.begin(), vec.begin()+rotL, vec.end());//rotl=no of left ro
// td::rotate(vec.begin(), vec.begin()+vec.size()-rotR, vec.end());
//str.substr(1,3); // takes(0 based indexing all from [1,3])
//transform(str.begin(),str.end(),str.begin(),::tolower);//also "toupper"
// to_string(ll),stoll(string)
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL),cout.tie(NULL);
ll n,m,q,l,r,w,v;
cin>>n>>m>>q;
vector<pair<ll,ll>>vec,pec;
for(int i=1; i<=n; i++){
cin>>w>>v;
vec.pb({w,v});
}
pec=vec;
ll num[m],vis[n]={0};
for(int i=0; i<m; i++){
cin>>num[i];
}
while(q--){
cin>>l>>r;
l-=1,r-=1;
for(int i=0; i<n; i++)
vis[i]=0;
vector<ll>vec;
for(int i=0; i<m; i++){
if(l>i||i>r){
vec.pb(num[i]);
}
}
sort(vec.begin(),vec.end());
ll ans=0;
for(int i=0; i<vec.size(); i++){
ll d=infn,ind=-1,w=-1;
for(int j=0; j<pec.size(); j++){
if(vis[j]==0&&pec[j].ff<=vec[i]&&pec[j].ss>d){
d=pec[j].ss;
ind=j;
w=pec[j].ff;
}
if(vis[j]==0&&pec[j].ff<=vec[i]&&pec[j].ss==d){
if(w<pec[j].ff){
d=pec[j].ss;
ind=j;
w=pec[j].ff;
}
}
}
if(ind!=-1)
vis[ind]=1,ans+=d;
}
cout<<ans<<"\n";
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define SZ(x) ((int)(x).size())
#define FOR(var, begin, end) for (int var = (begin); var <= (end); var++)
#define RFOR(var, begin, end) for (int var = (begin); var >= (end); var--)
#define REP(var, length) FOR(var, 0, length - 1)
#define RREP(var, length) RFOR(var, length - 1, 0)
#define EACH(value, var) for (auto value : var)
#define SORT(var) sort(var.begin(), var.end())
#define REVERSE(var) reverse(var.begin(), var.end())
#define RSORT(var) SORT(var); REVERSE(var)
#define OPTIMIZE_STDIO ios::sync_with_stdio(false); cin.tie(0); cout.precision(10); cout << fixed
#define endl '\n'
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;
void solve(istream& cin, ostream& cout) {
int t;
cin >> t;
REP(i, t) {
ll n;
cin >> n;
string ans;
if (n % 4 == 0) {
ans = "Even";
} else if (n % 2 == 0) {
ans = "Same";
} else {
ans = "Odd";
}
cout << ans << endl;
}
}
#ifndef TEST
int main() {
OPTIMIZE_STDIO;
solve(cin, cout);
return 0;
}
#endif
|
#include <bits/stdc++.h>
using namespace std;
int isprime (int n)
{
int j;
for ( j = 2 ; j <= n / 2 ; j ++ )
{
if ( !(n % j) )
return 0;
}
return 1;
}
int main (void)
{
int n;
while (cin >> n)
{
long long ans = 1;
int i;
for ( i = 2; i <= n ; i ++)
{
if (isprime (i))
{
if ( i == 2)
ans *=2 * 2 * 2 * 2;
else if (i == 3)
{
ans *=3 * 3 * 3;
}
else if (i == 5)
{
ans *= 5 * 5;
}
else
ans *= i;
}
}
printf ("%lld\n",ans+1);
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll x,ll y)
{
return __gcd(x,y);
}
ll lcm(ll x,ll y)
{
return (x*y)/(gcd(x,y));
}
int main()
{
long long n,i,a;
cin>>n;
a=1;
for(i=2;i<=n;i++)
{
a=lcm(a,i);
}
cout<<a+1<<endl;
}
|
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using Graph= vector<vector<int>>;
#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 pb push_back
#define eb emplace_back
#define ppb pop_back
#define fi first
#define se second
#define mpa make_pair
const ll INF=1e18 ;
inline void chmax(ll& a,ll b){a=max(a,b);}
inline void chmin(ll& a,ll b){a=min(a,b);}
ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) { return a/gcd(a,b)*b;}
#define set20 cout<<fixed<<setprecision(20) ;
// ミント
//int mod ; cin>>mod ;
const ll mod = 1e9+7 ;//924844033;
//998244353;
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 { return mint(*this) += a;}
mint operator-(const mint a) const { return mint(*this) -= a;}
mint operator*(const mint a) const { return mint(*this) *= 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 { return mint(*this) /= a;}
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
//昆布
struct combination {
vector<mint> fact, ifact;
combination(int n):fact(n+1),ifact(n+1) {
assert(n < mod); //任意modではここ消す
fact[0] = 1;
for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
ifact[n] = fact[n].inv();
for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
}
mint operator()(int n, int k) {
if (k < 0 || k > n) return 0;
return fact[n]*ifact[k]*ifact[n-k];
}
mint p(int n,int k){
return fact[n]*ifact[n-k] ; //kは個数
}
} c(1000005);
mint modpow(ll a,ll b){
if(b==0) return 1 ;
mint c= modpow(a,b/2) ;
if(b%2==1) return c*c*a ;
else return c*c ;
}
mint komb(ll n,ll m){
mint x=1 ;mint y=1 ;
rep(i,m){
x*= n-i ;
y*= i+1 ;
}
return x/y ;
}
map<ll,ll> factor(ll n){ //素因数とオーダーをマップで管理
map <ll,ll> ord ;
for(ll i=2;i*i<=n;i++){
if(n%i==0){
int res=0;
while(n%i==0){
n/=i;
res++;
}
ord[i]=res;
}
}
if(n!=1) ord[n]++;
return ord ;
}
int main(){
ios::sync_with_stdio(false) ;
cin.tie(nullptr) ;
ll n ; cin>>n ;
string s ; cin>>s ;
if(s[0]!=s[n-1]) cout<<1<<endl ;
else{
char a=s[0] ;
bool ok=0 ;
rep(i,n-1){
if(s[i]!=a && s[i+1]!=a) ok=1 ;
}
if(ok) cout<<2<<endl ;
else cout<<-1<<endl ;
}
return 0 ;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < n; ++i)
using ll = long long;
using P = pair<int,int>;
const int N=2000;
const int INF=1e9;
const int White=0,Gray=1,Black=2;
vector<P> G[N];
int dist[N][N];
int color[N], self[N];
priority_queue<P,vector<P>, greater<P>> que;
void dijkstra(int v){
que.push({0,v});
color[v]=Gray;
while(!que.empty()){
P now=que.top();que.pop();
if(dist[v][now.second]<now.first)continue;
color[now.second]=Black;
for (auto &&i : G[now.second]){
int d=i.first, next=i.second;
if(color[next]==Black)continue;
if(dist[v][next] > dist[v][now.second]+d) {
dist[v][next] = dist[v][now.second]+d;
que.push({dist[v][next],next});
color[next]=Gray;
}
}
}
}
int main() {
int n,m;
cin >> n >> m;
rep(i,n)self[i]=INF;
rep(i,m) {
int ai, bi, ci;
cin >> ai >> bi >> ci;
ai--; bi--;
if(ai==bi){
self[ai]=min(self[ai],ci);
continue;
}
G[ai].push_back({ci,bi});
}
rep(i,n)rep(j,n)dist[i][j]=INF;
rep(i,n)dist[i][i]=0;
rep(j,n){
rep(k,n)color[k] = White;
dijkstra(j);
}
rep(i,n) {
int ans = INF;
rep(j,n) {
if(i==j){
ans=min(ans,self[i]);
continue;
}
ans = min(ans,dist[i][j]+dist[j][i]);
}
cout << ((ans==INF) ? -1 : ans)<< endl;
}
} |
#pragma GCC optimize ("O2")
#pragma GCC target ("avx2")
//#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please
int P[100001];
int sei[100001], fu[100001], kisuu[100001], kazu[100001], kazu2[100001];
void sort_ganbare(int N, int A[]) {
if (N < (1 << 12)) {
sort(A, A + N);
return;
}
const int b = 9;
int tmp[100001];
int kazu[1 << b] = {}, kazu2[1 << b] = {};
rep(i, N) kazu[A[i] & ((1 << b) - 1)]++;
rep(i, (1 << b) - 1) kazu[i + 1] += kazu[i];
for (int i = N - 1; i >= 0; i--) tmp[--kazu[A[i] & ((1 << b) - 1)]] = A[i];
rep(i, N) kazu2[tmp[i] >> b & ((1 << b) - 1)]++;
rep(i, (1 << b) - 1) kazu2[i + 1] += kazu2[i];
for (int i = N - 1; i >= 0; i--) A[--kazu2[tmp[i] >> b & ((1 << b) - 1)]] = tmp[i];
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
rep1(i, N - 1) {
cin >> P[i + 1];
kazu[P[i + 1]]++;
}
rep(i, N) kazu2[i + 1] = (kazu[i + 1] += kazu[i]);
for (int i = N; i >= 2; i--) {
int ret = sei[i] - 1;
auto ki = kisuu + kazu[i - 1];
int sz = kazu2[i - 1] - kazu[i - 1];
sort_ganbare(sz, ki);
if (sz & 1) {
ret -= fu[i];
rep(j, sz) {
if (j & 1) ret -= ki[j] - (1 << 17);
else ret += ki[j] - (1 << 17);
}
}
else {
ret += fu[i];
rep(j, sz) {
if (j & 1) ret += ki[j] - (1 << 17);
else ret -= ki[j] - (1 << 17);
}
}
if (ret & 1) {
kisuu[kazu2[P[i] - 1]++] = ret + (1 << 17);
}
else {
if (ret >= 0) sei[P[i]] += ret;
else fu[P[i]] += ret;
}
}
int ret = sei[1] - 1;
auto ki = kisuu + kazu[0];
int sz = kazu2[0] - kazu[0];
sort_ganbare(sz, ki);
if (sz & 1) {
ret -= fu[1];
rep(j, sz) {
if (j & 1) ret -= ki[j] - (1 << 17);
else ret += ki[j] - (1 << 17);
}
}
else {
ret += fu[1];
rep(j, sz) {
if (j & 1) ret += ki[j] - (1 << 17);
else ret -= ki[j] - (1 << 17);
}
}
co((N - ret) / 2);
Would you please return 0;
} | //QwQcOrZ yyds!!!
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
#define ll long long
#define F(i,a,b) for (int i=(a);i<=(b);i++)
#define R(i,a,b) for (int i=(a);i<(b);i++)
#define D(i,a,b) for (int i=(a);i>=(b);i--)
#define go(i,x) for (int i=head[x];i;i=e[i].nx)
#define mp make_pair
#define pb push_back
#define pa pair < int,int >
#define fi first
#define se second
#define re register
#define be begin()
#define en end()
#define sqr(x) ((x)*(x))
#define ret return puts("-1"),0;
#define put putchar('\n')
#define inf 1000000005
#define mod 998244353
#define int ll
//#define N
using namespace std;
inline char gc(){static char buf[100000],*p1=buf,*p2=buf;return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;}
#define gc getchar
inline ll read(){char c=gc();ll su=0,f=1;for (;c<'0'||c>'9';c=gc()) if (c=='-') f=-1;for (;c>='0'&&c<='9';c=gc()) su=su*10+c-'0';return su*f;}
inline void write(ll x){if (x<0){putchar('-');write(-x);return;}if (x>=10) write(x/10);putchar(x%10+'0');}
inline void writesp(ll x){write(x),putchar(' ');}
inline void writeln(ll x){write(x);putchar('\n');}
long long x,y,z,p;
signed main()
{
x=read(),y=read(),z=read();
p=y*y+z*z;
if (sqrt(p*1.0)/x<1) writeln(2); else
cout<<ceil(sqrt(p*1.0)/x);
}
/*
*/
|
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define chmin(x,y) x = min((x),(y))
#define chmax(x,y) x = max((x),(y))
#define popcount(x) __builtin_popcount(x)
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
const int INF = 1e9;
const long long LINF = 1e17;
const int MOD = 1000000007;
//const int MOD = 998244353;
const double PI = 3.14159265358979323846;
int main(){
int n;
cin >> n;
vector<ll> a(n);
rep(i,n) cin >> a[i];
ll ans = 2*a[0];
ll last = 2*a[0];
ll ma = a[0];
//cout << last << endl;
cout << ans << endl;
for(int i=1;i<n;i++){
if(ma < a[i]){
ans += i*(a[i]-ma) + last+(a[i]-ma)+a[i];
last = last+(a[i]-ma)+a[i];
ma = a[i];
}else{
ans += last+a[i];
last += a[i];
}
//cout << last << endl;
cout << ans << endl;
}
return 0;
} | #include <cstdio>
#include <algorithm>
using namespace std;
int n;
long long k;
long long a[3000006];
long long sum[3000006];
int main(){
scanf("%d%lld",&n,&k);
int l=3;
for(int i=1;i<=n;++i)a[l++]=1ll*i;
for(int i=n-1;i>=1;--i)a[l++]=1ll*i;
for(int i=3;i<=n+2;++i){
sum[i]=sum[i-1]+a[i];
a[i]=sum[i];
}
for(int i=n+3;i<=3*n;++i){
sum[i]=sum[i-1]+a[i];
a[i]=(sum[i]-sum[i-n]);
}
for(int s=3;s<=3*n;++s){
if(k<=a[s]){
for(int i=1;i<=min(s-2,n);++i){
int js;
if(s-i-1<=n)js=s-i-1;
else js=max(2*n-(s-i-1),0);
if(k<=js){
int j=k+(s-i-1>n?s-i-1-n:0);
printf("%d %d %d\n",i,j,(s-i-j));
return 0;
}
k-=1ll*js;
}
}
k-=a[s];
}
return 0;
}
|
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#ifdef ONLINE_JUDGE
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
#pragma GCC optimize("unroll-loops")
#define int128 __int128
#else
#define int128 long long
#endif
#define all(x) x.begin(), x.end()
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
#define mp make_pair
#define X first
#define Y second
using namespace std;
using namespace __gnu_pbds;
template<typename T> // order_of_key(), *find_by_order()
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
mt19937_64 gen(time(nullptr));
namespace {
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
template<typename T, typename F>
using HashMap = unordered_map<T, F, custom_hash>;
template<typename T>
using HashSet = unordered_set<T, custom_hash>;
}
ll mod = 1e9 + 7;
namespace {
ll mul(ll a, ll b) {
ll val = a * b - (ll)((ld)a * b / mod) * mod;
if (val < 0) val += mod;
if (val >= mod) val -= mod;
return val;
}
ll poww(ll a, ll b) {
ll val = 1;
a %= mod;
while (b > 0) {
if (b % 2) val = mul(a, val);
a = mul(a, a);
b >>= 1;
}
return val % mod;
}
ll inv(ll a) {
return poww(a, mod - 2);
}
ll gcd(ll a, ll b) {
return (a == 0 ? b : gcd(b % a, a));
}
ll gcdex(ll a, ll b, ll& x, ll& y) {
if (a == 0) {
x = 0; y = 1;
return b;
}
ll x1, y1;
ll d = gcdex(b % a, a, x1, y1);
x = y1 - (b / a) * x1;
y = x1;
return d;
}
ll inv_eu(ll a, ll m) {
ll x, y;
ll g = gcdex(a, m, x, y);
if (g != 1) {
assert(0);
return 0;
} else {
x = (x % m + m) % m;
return x;
}
}
}
ll const maxn = 3e5 + 5;
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll n; cin >> n;
vector<int>a(n),b(n);
for(int &x : a) cin >> x;
for(int &x : b) cin >> x;
ll ans = 0;
for(int x : a) ans += x;
vector<vector<int>>v(2);
for(int i=0;i<n;i++)
v[i%2].push_back(b[i] - a[i]);
for(int i=0;i<2;i++){
sort(all(v[i]));
reverse(all(v[i]));
}
for(int i=0;i<n/2;i++){
//cerr << v[0][i] << " " << v[1][i] << endl;
if(v[0][i] + v[1][i] > 0) ans += v[0][i] + v[1][i];
}
cout << ans << endl;
return 0;
}
/*
*/
| #include <stdio.h>
int i, N, A[2][100001];
long long dp[2][100001];
int main()
{
scanf("%d", &N);
for (i = 1; i <= N; i++) scanf("%d", &(A[0][i]));
for (i = 1; i <= N; i++) scanf("%d", &(A[1][i]));
int j, k, l, r;
const long long inf = -(1LL << 60);
for (i = 0; i <= N; i++) {
dp[0][i] = inf;
dp[1][i] = inf;
}
dp[0][1] = A[0][1];
dp[1][1] = A[1][1];
for (i = 2; i <= N; i++) {
if (i % 2 == 0) {
dp[0][0] = dp[0][1] + A[0][i];
if (dp[0][0] < dp[1][1] + A[1][i]) dp[0][0] = dp[1][1] + A[1][i];
} else dp[1][0] = dp[0][0];
k = (i < N - i)? i: N - i;
l = i % 2;
r = 1 - i % 2;
for (j = 2 - l; j <= k; j += 2) {
dp[0][j] = dp[0][j-1] + A[r][i];
if (dp[0][j] < dp[0][j+1] + A[l][i]) dp[0][j] = dp[0][j+1] + A[l][i];
dp[1][j] = dp[1][j-1] + A[l][i];
if (dp[1][j] < dp[1][j+1] + A[r][i]) dp[1][j] = dp[1][j+1] + A[r][i];
}
}
printf("%lld\n", dp[0][0]);
fflush(stdout);
return 0;
} |
#include <cstdio>
#include <cstdlib>
using namespace std;
#define ll long long
const ll MAXN = 400011;
struct fhqTreap {
ll tot, rt;
ll val[MAXN], sum[MAXN], sz[MAXN], ls[MAXN], rs[MAXN], rd[MAXN];
void pushup(ll u) {sz[u] = sz[ls[u]] + sz[rs[u]] + 1; sum[u] = sum[ls[u]] + sum[rs[u]] + val[u];}
void split(ll u, ll k, ll &x, ll &y) {
if(!u) {x = y = 0; return;}
if(val[u] <= k) x = u, split(rs[u], k, rs[u], y);
else y = u, split(ls[u], k, x, ls[u]);
pushup(u);
}
ll merge(ll u, ll v) {
if(!u || !v) return u | v;
if(rd[u] < rd[v]) {rs[u] = merge(rs[u], v); pushup(u); return u;}
else {ls[v] = merge(u, ls[v]); pushup(v); return v;}
}
ll add(ll k) {
rd[++tot] = rand();
sz[tot] = 1;
val[tot] = sum[tot] = k;
return tot;
}
void insert(ll k) {
ll x, y;
split(rt, k, x, y);
rt = merge(merge(x, add(k)), y);
}
ll cb(ll k) {
ll x, y;
split(rt, k, x, y);
ll ans = sz[x] * k + sum[y];
rt = merge(x, y);
return ans;
}
void delet(ll k) {
ll x, y, z;
split(rt, k, x, z);
split(x, k - 1, x, y);
y = merge(ls[y], rs[y]);
rt = merge(merge(x, y), z);
}
} t[2];
ll q;
ll len[2];
ll w[2][MAXN];
ll read() {
ll X = 0, F = 1; char C = getchar();
while(C < '0' || C > '9') {if(C == '-')F=-1; C = getchar();}
while(C >= '0' && C <= '9') {X = X*10+C-'0'; C = getchar();}
return X * F;
}
int main() {
ll ans = 0;
len[1] = read(); len[0] = read(); q = read();
for(ll i = 0; i < 2; ++i)
for(ll j = 1; j <= len[i]; ++j)
t[i].rt = t[i].merge(t[i].rt, t[i].add(0));
for(ll i = 1, opt, x, y; i <= q; ++i) {
opt = (read() & 1); x = read(); y = read();
t[opt].delet(w[opt][x]);
t[opt].insert(y);
ans -= t[opt^1].cb(w[opt][x]);
w[opt][x] = y;
ans += t[opt^1].cb(w[opt][x]);
printf("%lld\n", ans);
}
return 0;
} | #include <iostream>
using namespace std;
long long n,k,x,y,a,b,c,d, ansx, ansy;
long long ans;
main(){
cin>>n>>k;
k=abs(k);
for(x=1; x<=2*n+1; x++){
ansx=1; ansy=1;
y=x-k;
//cout<<"----"<<endl;
//cout<<x<<" "<<y<<endl;
if(x-1>=n) ansx=2*n-x+1;
else ansx=x-1;
if(y-1>=n) ansy=2*n-y+1;
else ansy=y-1;
if(ansx<0 || ansy<0) continue;
//cout<<ansx<<" "<<ansy<<endl;
ans+= (ansx*ansy);
}
cout<<ans<<endl;
} |
#include<iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
int main(){
int a[3];
cin >> a[0] >> a[1] >> a[2];
sort(a, a+3);
if(a[0]==a[1]){
cout << a[2] <<endl;
}else if(a[1]==a[2]){
cout << a[0] <<endl;
}else{
cout << 0 <<endl;
}
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;
}
|
/**
* author: Pocket7878
**/
#include <algorithm>
#include <cctype>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
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;
}
/* Consts */
const int INF = 1000000000;
const long long LINF = 1e18;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
const long double LEPS = 1.0E-14;
/* Types */
typedef long long ll;
typedef unsigned long long ull;
/* Macros */
#define RNG(i, a, b) for (int i = int(a); i < int(b); ++i)
#define REP(i, b) RNG(i, 0, b)
#define GNR(i, a, b) for (int i = int(b) - 1; i >= int(a); --i)
#define PER(i, b) GNR(i, 0, b)
bool start_with(const std::string& s, const std::string& prefix, int offset) {
if (s.size() < prefix.size()) return false;
return std::equal(std::begin(prefix), std::end(prefix),
std::begin(s) + offset);
}
// #include <algorithm> // std::equal
// #include <iterator> // std::rbegin, std::rend
// ends_with("abc", "bc") == true
bool ends_with(const std::string& s, const std::string& suffix) {
if (s.size() < suffix.size()) return false;
return std::equal(std::rbegin(suffix), std::rend(suffix), std::rbegin(s));
}
template <class T>
double deg2rad(T& deg) {
return deg * (M_PI / 180.0);
}
// Debugs
#ifndef ONLINE_JUDGE
#define _LIBCPP_DEBUG 1
#endif
#ifdef ONLINE_JUDGE
#define debug(var) 42
#endif
#ifndef ONLINE_JUDGE
#define debug(var) \
do { \
std::cout << #var << " : "; \
view(var); \
} while (0)
#endif
template <typename T>
void view(T e) {
std::cout << e << std::endl;
}
template <typename T>
void view(const std::vector<T>& v) {
for (const auto& e : v) {
std::cout << e << " ";
}
std::cout << std::endl;
}
template <typename T>
void view(const std::vector<std::vector<T>>& vv) {
for (const auto& v : vv) {
view(v);
}
}
void comb(vector<vector<long long int>>& v) {
for (int i = 0; i < v.size(); i++) {
v[i][0] = 1;
v[i][i] = 1;
}
for (int k = 1; k < v.size(); k++) {
for (int j = 1; j < k; j++) {
v[k][j] = (v[k - 1][j - 1] + v[k - 1][j]);
}
}
}
ll mod_fact(unordered_map<ll, ll>& memo, ll i) {
if (memo.find(i) != memo.end()) {
return memo[i];
} else if (i == 1) {
return memo[i] = 1;
} else {
return memo[i] = (i % MOD) * (mod_fact(memo, i - 1) % MOD) % MOD;
}
}
vector<long long> enum_divisors(long long N) {
vector<long long> res;
for (long long i = 1; i * i <= N; ++i) {
if (N % i == 0) {
res.push_back(i);
// 重複しないならば i の相方である N/i も push
if (N / i != i) res.push_back(N / i);
}
}
// 小さい順に並び替える
sort(res.begin(), res.end());
return res;
}
struct Edge {
ll to;
ll ki;
ll ti;
Edge(ll to, ll ki, ll ti) : to(to), ki(ki), ti(ti) {}
};
using Graph = vector<vector<Edge>>;
std::vector<ll> Eratosthenes( const ll N )
{
std::vector<bool> is_prime( N + 1 );
for( ll i = 0; i <= N; i++ )
{
is_prime[ i ] = true;
}
std::vector<ll> P;
for( ll i = 2; i <= N; i++ )
{
if( is_prime[ i ] )
{
for( ll j = 2 * i; j <= N; j += i )
{
is_prime[ j ] = false;
}
P.emplace_back( i );
}
}
return P;
}
int main() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
ll A, B;
cin >> A >> B;
ll C = A + B;
if (C >= 15 && B >= 8) {
cout << 1 << endl;
} else if (C >= 10 && B >= 3) {
cout << 2 << endl;
} else if (C >= 3) {
cout << 3 << endl;
} else {
cout << 4 << endl;
}
} | #include <iostream>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
using namespace std;
typedef long long int ll;
typedef pair <int,int> pii;
typedef pair<ll,ll> pll;
/*bool compare_function(const pair<int, int> a, const pair<int, int> b)
{
return a.first < b.first;
}*/
// use case
//sort(V.begin(), V.end(), compare_function);
/*struct compare
{
bool operator ()(const pair<int, int> &a, const pair<int, int> &b) {
if (a.second-a.first==b.second-b.first)
{
return a.first>b.first;
}
return a.second-a.first < b.second-b.first;}
};
priority_queue <pair<int,int>, vector <pair<int,int>>, compare> Q;*/
/*void print1(vector <int> X)
{
for (int i=0; i<X.size(); i++)
{
cout<<X[i]<<" ";
}
cout<<endl;
return;
}
void print2(vector <vector <int>> X)
{
for (int i=0; i<X.size(); i++)
{
for (int j=0;j<X[i].size();j++) {
cout<<X[i][j]<<" ";}
cout<<endl;}
return;
}*/
int solve(int a, int b) {
if (a+b>=15 && b>=8) {return 1;}
if (a+b>=10 && b>=3) {return 2;}
if (a+b>=3) {return 3;}
return 4;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int a,b;
cin>>a>>b;
int x=solve(a,b);
cout<<x<<endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(v) v.begin(), v.end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repr(i, n) for (ll i = (n-1); i >= 0; i--)
#define Rep(n) for (ll _ = 0; _ < (ll)(n); _++)
template <class S>S sum(vector<S>& a) {return accumulate(all(a), S());}
template <class S>S max(vector<S>& a) {return *max_element(all(a));}
template <class S>S min(vector<S>& a) {return *min_element(all(a));}
ll max(int a, ll b) { return max((ll)a, b); } ll max(ll a, int b) { return max(a, (ll)b); }
ll min(int a, ll b) { return min((ll)a, b); } ll min(ll a, int b) { return min(a, (ll)b); }
void Main();
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(20);
Main();
return 0;
}
using P = pair<int,int>;
int n,m;
vector<vector<P>> graph;
vector<int> label;
void dfs(int node){
for(auto [a,c]: graph[node]){
if(label[a] < 0){
if(c == label[node]){
label[a] = (c+1) % n;
}else{
label[a] = c;
}
dfs(a);
}
}
}
void Main(){
cin >> n >> m;
graph = vector<vector<P>>(n);
label = vector<int>(n, -1);
Rep(m){
int u,v,c; cin >> u >> v >> c;
u--; v--; c--;
graph[u].push_back(P(v,c));
graph[v].push_back(P(u,c));
}
label[0] = 0;
dfs(0);
for(auto i: label){
cout << i+1 << endl;
}
}
| #include "bits/stdc++.h"
using namespace std;
using P = pair<int, int>;
bool seen[100010], seen2[100010];
vector<vector<P>>graph;
int ans[100010];
void dfs(vector<vector<P>> &G, int v) {
seen[v] = true;
for (auto nv : G[v]) {
if (seen[nv.first])continue;
graph[v].push_back({ nv.first,nv.second });
graph[nv.first].push_back({ v,nv.second });
dfs(G, nv.first);
}
}
void dfs2(int v) {
if (v == 0) {
ans[v] = 1;
}
seen2[v] = true;
for (auto nv : graph[v]) {
if (seen2[nv.first])continue;
if (ans[v] == nv.second) {
int tmp = 1;
if (tmp == nv.second)tmp++;
ans[nv.first] = tmp;
}
else {
ans[nv.first] = nv.second;
}
dfs2(nv.first);
}
}
signed main() {
int N, M; cin >> N >> M;
graph.resize(N);
vector<vector<P>>G(N);
for (int i = 0; i < M; i++) {
int u, v, c; cin >> u >> v >> c; u--; v--;
G[u].push_back({ v,c });
G[v].push_back({ u,c });
}
dfs(G, 0);
dfs2(0);
for (int i = 0; i < N; i++) {
cout << ans[i] << endl;
}
} |
#include <iostream>
#include <cstdio>
using namespace std;
int m;
long long n;
struct node
{
int res, rem;
};
node work(long long x)
{
if(x == 0)
{
return (node){0, 1};
}
if(x % 2 == 1)
{
node tmp = work(x - 1);
int Res = (tmp.res * 10 + (tmp.rem * 10) / m) % m;
int Rem = (tmp.rem * 10) % m;
return (node){Res, Rem};
}
else
{
node tmp = work(x / 2);
int Res = (tmp.res * tmp.res % m * m % m + 2 * tmp.res * tmp.rem % m + (tmp.rem * tmp.rem) / m) % m;
int Rem = (tmp.rem * tmp.rem) % m;
return (node){Res, Rem};
}
}
int main()
{
scanf("%lld%d", &n, &m);
node tmp = work(n);
printf("%d", tmp.res);
return 0;
} | //#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx,avx2,fma")
#include <algorithm>
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define PI acos(-1.0)
#define No cout<<"No\n"
#define Yes cout<<"Yes\n"
#define no cout<<"no\n"
#define yes cout<<"yes\n"
#define NO cout<<"NO\n"
#define YES cout<<"YES\n"
// #define MOD (int)1000000007
#define int long long
#define ll long long
#define pii pair<int, int>
#define fi first
#define se second
#define sf(a) scanf("%lld", &a)
#define pf(a) printf("%lld ", a)
#define pfn(a) printf("%lld\n", a)
#define case(a) cout<<"Case "<<a<<": ";
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define pb push_back
#define mem(arr) memset(arr, 0, sizeof(arr))
#define mem1(arr) memset(arr, -1, sizeof(arr))
#define all(a) a.begin(),a.end()
#define f(i,a,b) for (int i = a; i<=b; i++)
#define fr(i,a,b) for (int i = a; i>=b; i--)
#define rr return 0
#define prec(n) fixed<<setprecision(n)
#define maxpq priority_queue<int>
#define minpq priority_queue<int, vector<int>, greater<int> >
#define inf (int)(1e18)
#define vi vector<int>
#define vii vector<pii>
#define sz(s) s.size()
#define lcm(a,b) (a*(b/gcd(a,b)))
int gcd(int a, int b) {return __gcd(a,b);}
int MOD;
inline void normal(ll &a) { a %= MOD; (a < 0) && (a += MOD); }
inline ll modMul(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a*b)%MOD; }
inline ll modAdd(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); return (a+b)%MOD; }
inline ll modSub(ll a, ll b) { a %= MOD, b %= MOD; normal(a), normal(b); a -= b; normal(a); return a; }
inline ll modPow(ll b, ll p) { ll r = 1; while(p) { if(p & 1LL) r = modMul(r, b); b = modMul(b, b); p >>= 1LL; } return r; }
inline ll modInverse(ll a) { return modPow(a, MOD-2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }
inline bool checkBit(ll n, int i) { return n&(1LL<<i); }
inline ll setBit(ll n, int i) { return n or (1LL<<i);; }
inline ll resetBit(ll n, int i) { return n&(~(1LL<<i)); }
const double eps = 1e-9;
int dx[5] = {+1, +0, -1, -0};
int dy[5] = {+0, +1, -0, -1};
int XX[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int YY[] = { -1, 0, 1, -1, 1, -1, 0, 1 };
inline bool Equal(double x, double y) { return fabs(x-y)<eps; }
inline bool Greater(double x, double y){ return x-eps>y; }
inline bool Lesser(double x, double y){ return x+eps<y; }
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); cerr << '\n'; }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << " "; err(++it, args...); }
///Write what you need -->
/// nt, ft, kmp, tri, sufarr, ub_lb, lis
/// dsu, combi
///Template Ends Here////////////////////////////
signed main()
{
// root = new trinode();
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
FASTIO
// int n;
// cin>>n;
//
// int a[n+1];
// f(i, 1, n){
// cin>>a[i];
// }
int n, m;
cin>>n>>m;
MOD = m*m;
int r = modPow(10, n);
MOD = m;
int r1 = modPow(10, n);
cout<<(r-r1)/m;
// tridel(root);
}
|
#include <sstream>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <array>
#include <deque>
#include <numeric>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <list>
#include <cassert>
#include <cmath>
#include <climits>
#include <map>
#include <queue>
#include <functional>
#include <bitset>
#include <cassert>
using namespace std;
using ll = int64_t;
#define rep(i, a) for (int i = 0; i < (int)(a); ++i)
#define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define repr(i, a) for (int i = (int)((a) - 1); i >= 0; --i)
template <class T> using V1 = vector<T>;
template <class T> using V2 = vector<V1<T>>;
template <class T> using V3 = vector<V2<T>>;
template <class T> using V4 = vector<V3<T>>;
template <class T> using V5 = vector<V4<T>>;
template <class T> V1<T> MV1(size_t n1, T init = T()) { return V1<T>(n1, init); }
template <class T> V2<T> MV2(size_t n1, size_t n2, T init = T()) { return V2<T>(n1, MV1(n2, init)); }
template <class T> V3<T> MV3(size_t n1, size_t n2, size_t n3, T init = T()) { return V3<T>(n1, MV2(n2, n3, init)); }
template <class T> V4<T> MV4(size_t n1, size_t n2, size_t n3, size_t n4, T init = T()) { return V4<T>(n1, MV3(n2, n3, n4, init)); }
template <class T> V5<T> MV5(size_t n1, size_t n2, size_t n3, size_t n4, size_t n5, T init = T()) { return V5<T>(n1, MV4(n2, n3, n4, n5, init)); }
struct dstream { ofstream fout; dstream(const char* fname) : fout(fname) {} };
template <typename T> dstream& operator << (dstream& lhs, const T& rhs) { lhs.fout << rhs; cout << rhs; return lhs; }
template <typename T> vector<T> GetDivisors(T n)
{
vector<T> v;
for (T i = 1; i * i <= n; ++i) {
if ((n % i) == 0) {
v.push_back(i);
if ((n / i) != i)
v.push_back(n / i);
}
}
sort(v.begin(), v.end());
return v;
}
int main()
{
#ifdef ENV_LOCAL
ifstream fin("input.txt");
dstream dout("output.txt");
auto& in = fin;
auto& out = dout;
#else
auto& in = cin;
auto& out = cout; out << "";
#endif
int A, B;
in >> A >> B;
vector<int> ds(200001);
for (int i = A; i <= B; ++i) {
auto divs = GetDivisors(i);
rep(j, divs.size())
ds[divs[j]]++;
}
int vmax = 0;
rep(i, ds.size()) {
if (ds[i] >= 2)
vmax = max(vmax, i);
}
cout << vmax;
return 0;
}
| /**
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡀⠀⠀⠀⠀⢀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⡖⠁⠀⠀⠀⠀⠀⠀⠈⢲⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣼⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⣧⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣸⣿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⣇⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣿⣿⡇⠀⢀⣀⣤⣤⣤⣤⣀⡀⠀⢸⣿⣿⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⢻⣿⣿⣔⢿⡿⠟⠛⠛⠻⢿⡿⣢⣿⣿⡟⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⣀⣤⣶⣾⣿⣿⣿⣷⣤⣀⡀⢀⣀⣤⣾⣿⣿⣿⣷⣶⣤⡀⠀⠀⠀⠀
⠀⠀⢠⣾⣿⡿⠿⠿⠿⣿⣿⣿⣿⡿⠏⠻⢿⣿⣿⣿⣿⠿⠿⠿⢿⣿⣷⡀⠀⠀
⠀⢠⡿⠋⠁⠀⠀⢸⣿⡇⠉⠻⣿⠇⠀⠀⠸⣿⡿⠋⢰⣿⡇⠀⠀⠈⠙⢿⡄⠀
⠀⡿⠁⠀⠀⠀⠀⠘⣿⣷⡀⠀⠰⣿⣶⣶⣿⡎⠀⢀⣾⣿⠇⠀⠀⠀⠀⠈⢿⠀
⠀⡇⠀⠀⠀⠀⠀⠀⠹⣿⣷⣄⠀⣿⣿⣿⣿⠀⣠⣾⣿⠏⠀⠀⠀⠀⠀⠀⢸⠀
⠀⠁⠀⠀⠀⠀⠀⠀⠀⠈⠻⢿⢇⣿⣿⣿⣿⡸⣿⠟⠁⠀⠀⠀⠀⠀⠀⠀⠈⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣼⣿⣿⣿⣿⣧⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠐⢤⣀⣀⢀⣀⣠⣴⣿⣿⠿⠋⠙⠿⣿⣿⣦⣄⣀⠀⠀⣀⡠⠂⠀⠀⠀
⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠉⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠋⠁⠀⠀
* author: Daredevil666
* institution: IIT Patna
**/
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define nl cout<<"\n";
#define ll long long int
#define F(i,a,b) for(i=a;i<b;i++)
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
const double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781641;
const ll M=1e16;
const ll MAXN=200200;
ll i,j,mask;
void solve()
{
ll x,y;
cin>>x>>y;
if(x>=y)
{
cout<<x-y;
return ;
}
queue<ll> q;
q.push(y);
ll lvl=-1;
ll ans=y-x;
set<ll> st;
st.insert(y);
while(!q.empty())
{
lvl++;
if(lvl>=ans)break;
ll s=q.size();
F(i,0,s)
{
ll p=q.front();
q.pop();
if(p<=x)
{
ans=min(ans,lvl+x-p);
}
else
{
ans=min(ans,lvl+p-x);
if(p%2)
{
if(st.count(p+1)==0)
{st.insert(p+1);q.push(p+1);}
if(st.count(p-1)==0)
{st.insert(p-1);q.push(p-1);}
}
else
{
if(st.count(p/2)==0)
{st.insert(p/2);q.push(p/2);}
}
}
}
}
cout<<ans;
}
int main()
{
IOS
/*#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#*/
/*ll t,test;
cin>>test;
F(t,1,test+1)
{
//cout<<"Case #"<<t<<": ";
solve();
nl
}*/
solve();
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a),i##formax=(b);i< i##formax;i++)
#define FORR(i,a,b) for(int i=(a),i##formin=(b);i>=i##formin;i--)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define pcnt __builtin_popcount
#define sz(x) (int)(x).size()
#define maxs(x,y) x=max((x),(y))
#define mins(x,y) x=min((x),(y))
#define show(x) cout<<#x<<" = "<<(x)<<endl;
#define all(a) ((a).begin()),((a).end())
#define each(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define bit(n) (1LL<<(n))
typedef long long ll;
typedef __int128_t lll;
template<class T>using V=vector<T>;
template<class T>using VV=V<V<T>>;
template<class T,class Y>using P=pair<T,Y>;
template<class T,class Y>ostream& operator<<(ostream&o,P<T,Y>&p){return o<<"("<<p.fi<<","<<p.se<<")";}
template<class T>ostream& operator<<(ostream&o,V<T>&v){for(T&t:v)o<<t<<",";return o;}
template<class T>void uniq(V<T>&v){sort(all(v));v.erase(unique(all(v)), v.end());}
template<class A,size_t N,class T>void Fill(A(&a)[N],const T&v){fill((T*)a,(T*)(a+N),v);}
const int MOD = 1e9+7; //998244353;
lll gcd(lll a,lll b,lll&x,lll&y){if(!b){x=1;y=0;return a;}lll d=gcd(b,a%b,y,x);y-=a/b*x;return d;}
ll modInv(ll a,ll m=MOD){lll x,y;gcd(a,m,x,y);return(x%m+m)%m;}
ll modPow(lll a,lll n,ll m=MOD){lll p=1;for(;n;n>>=1,a=a*a%m)if(n&1)p=p*a%m;return p;}
//V<ll>Fac,Rac;void setF(int n,int m=MOD){Fac=Rac=V<ll>(++n);Fac[0]=1;FOR(i,1,n)Fac[i]=Fac[i-1]*i%m;Rac[n-1]=modInv(Fac[n-1],m);FORR(i,n-1,1)Rac[i-1]=Rac[i]*i%m;}
//ll comb(int a,int b,int m=MOD){return a<b||b<0?0:Fac[a]*Rac[b]%m*Rac[a-b]%m;}
const int IINF = 1e9+6;
const ll LINF = 1e18;
const int N = 2e5;
int n, q;
int p[N];
map<int, int> m[N];
int find(int a){
return (p[a]<0) ? a : (p[a] = find(p[a]));
}
void merge(int a, int b){
a=find(a);
b=find(b);
if(a==b) return;
if(sz(m[a]) < sz(m[b])) swap(a, b);
for(auto p:m[b]) m[a][p.fi] += p.se;
p[b] = a;
m[b].clear();
}
main(){
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n >> q;
FOR(i, 0, n){
int c;
cin >> c;
m[i][c]++;
p[i] = -1;
}
FOR(_, 0, q){
int t, a, b;
cin >> t >> a >> b;
if(t==1){
a--; b--;
merge(a, b);
}else{
a--;
a = find(a);
cout << m[a][b] << endl;
}
}
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define FOR(i,a,b) for(ll i=a; i<=b; i++)
#define all(v) v.begin(), v.end()
#define F first
#define S second
#define INF 2147483647
#define INFLL 1000000000000
#define MOD 998244353
#define PI 3.14159265359
#define dij priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>
#define cout_all(v) for(auto e:v)cout<<e<<" ";cout<<endl
#define smart(v) v.erase(all(v),v.end())
typedef vector<vector<int>> graph;
typedef long long ll;
typedef pair<int,int> pii;
typedef map<int,int> mii;
typedef pair<ll,ll> pll;
//
int main(){
int a,b,c;cin>>a>>b>>c;
string ans;
if(c==0){
if(a>b)ans="Takahashi";
else ans="Aoki";
}else{
if(a>=b)ans="Takahashi";
else ans="Aoki";
}
cout<<ans<<endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int main(){
int n,temp;
cin >> n;
vector<int> myvec(n);
for(int i = 0;i < n;i++){
cin >> myvec[i];
}
int result = 0;
for(int i = 0;i < n;i++){
cin >> temp;
result += temp*myvec[i];
}
if(result == 0){
cout << "Yes";
}
else{
cout << "No";
}
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define vec vector<int>
#define mii map<int,int>
#define pqb priority_queue<int>
#define no_of_test(zzz) int zzz; cin>>zzz; while(zzz--)
#define p(c) cout<<c<<'\n';
const int mod = 1e9 + 7;
void speed()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
//Actual Code Is At The Top
int32_t main()
{
speed();
//no_of_test(zzz)
{
string s;
int n;
cin >> n;
set<string>set;
for (int i = 0; i < n; i++)
{
cin >> s;
set.insert(s);
}
for (auto x : set)
{
if (set.count('!' + x))
{
cout << x << '\n';
return 0;
}
}
cout << "satisfiable" << '\n';
}
return 0;
}
//cout<<"###"<<'\n';
//cout<<"."<<'\n';
//cout<<"****"<<'\n';
/*
&& abs(a) + abs(b) < abs(c) + abs(d)
*/
|
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(ll i=0;i<(n);i++)
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define FORC(i,a,b,c) for(ll i=(a);i<(b);i+=(c))
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define ff first
#define ss second
#define dd long double
#define all(x) x.begin(),x.end()
ll maxx=0;
ll n,m;
const ll N=18;
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v){
os << "[";
for (ll i=0; i < v.size(); ++i) {
os << v[i];
if (i != v.size() - 1)
os << ", ";
}
os << "]";
return os;
}
template <typename T>
ostream& operator<<(ostream& os, const set<T>& v){
os << "{";
for (auto it : v) {
os << it;
if (it != *v.rbegin())
os << ", ";
}
os << "}";
return os;
}
template <typename T, typename S>
ostream& operator<<(ostream& os, const map<T, S>& v){
os<<"{";
for (auto it : v){
os << it.first << " : "
<< it.second ;
if (it != *v.rbegin())
os << ", ";
}
os << "}";
return os;
}
template <typename T, typename S>
ostream& operator<<(ostream& os, const pair<T, S>& v){
os << "(";
os << v.first << ", "
<< v.second << ")";
return os;
}
vector<vector<ll>> graph(N,vector<ll>(N,0));
void ans(ll idx, vector<vector<ll>> l){
if(idx==n){
maxx=min(int(maxx),int(l.size()));
//cout<<l<<endl;
return;
}
if(l.size()>=maxx){
return;
}
for(ll i=0;i<l.size();i++){
bool flag=true;
for(ll j=0;j<l[i].size();j++){
if(graph[idx][l[i][j]]==0){
flag=false;
break;
}
}
if(flag){
l[i].pb(idx);
ans(idx+1,l);
l[i].pop_back();
}
}
vector<ll> temp{idx};
l.pb(temp);
ans(idx+1,l);
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n>>m;
REP(i,m){
ll a,b;
cin>>a>>b;
a--,b--;
graph[a][b]=1;
graph[b][a]=1;
}
maxx=n;
vector<vector<ll>> l;
ans(0,l);
cout<<maxx<<endl;
}
| #include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mod 1000000007
#define pii pair<ll,ll>
#define inf 1000000000000000000
#define bpc(x) __builtin_popcountll(x)
#define autoit(x,it) for(auto it = x.begin(); it != x.end(); it++)
#define autoitr(x,it) for(auto it = x.rbegin(); it != x.rend(); it++)
#define rep(n) for(ll i = 0; i < n; i++)
#define repi(i,n) for(ll i = 0; i < n; i++)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
int main()
{
FAST/**/
string req = "fox";
ll n;
cin>>n;
string s;
cin>>s;
map<char,ll> st;
repi(j,3)
st[req[j]] = j;
stack<string> sta;
ll ans = 0;
for(ll i=0;i<n;i++)
{
if(st.find(s[i]) == st.end())
{
while(sta.size()>0)
sta.pop();
continue;
}
if(s[i] == 'f')
{
sta.push("f");
continue;
}
else if(s[i] == 'o')
{
if(sta.size() == 0)
continue;
if(sta.top() == "f")
{
sta.pop();
sta.push("fo");
continue;
}
while(sta.size()>0)
sta.pop();
}
else
{
if(sta.size() == 0)
continue;
if(sta.top() != "fo")
{
while(sta.size()>0)
sta.pop();
continue;
}
ans++;
sta.pop();
}
}
cout<<n-(3*ans)<<"\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
using ll =long long;
using P =pair<int,int>;
int main() {
// codehere
string s;
cin >> s;
cout << s[1] << s[2] << s[0]<<'\n';
return 0;
}
| #include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<map>
#include<vector>
#include<math.h>
#include<stdio.h>
#include<stack>
#include<queue>
#include<tuple>
#include<cassert>
#include<set>
#include<bitset>
#include<functional>
#include <fstream>
//#include<bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define rep(i, x) for(ll i = 0; i < x; i++)
#define rep2(i, x) for(ll i = 1; i <= x; i++)
#define all(a) (a).begin(),(a).end()
#define puts(x) cout << (x) << "\n"
using ll = long long;
using ld = long double;
using namespace std;
const ll INF = 1000000000000000000;
const int intINF = 1000000000;
const ll mod = 1000000007;
const ll MOD = 998244353;
const ld pi = 3.141592653589793238;
//const ld EPS = 1e-9;
bool isprime(int p) {
if (p == 1) return false;
for (int i = 2; i < p; i++) {
if (p % i == 0) return false;
}
return true;
}
ll gcd(ll a, ll b) {
if (a < b)swap(a, b);
if (a % b == 0)return b;
return gcd(b, a % b);
}
// 返り値: a と b の最大公約数
// ax + by = gcd(a, b) を満たす (x, y) が格納される
//main関数内に extGCD(a, b, x, y); でx, yに解が格納
ll extGCD(ll a, ll b, ll& x, ll& y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
ll d = extGCD(b, a % b, y, x);
y -= a / b * x;
return d;
}
ll lcm(ll a, ll b) {
return a / gcd(a, b) * b;
}
ll keta(ll n) {
ll res = 0;
while (n >= 1) {
res += n % 10; n /= 10;
}
return res;
}
ll modpow(ll x, ll y) {
ll res = 1;
while (y) {
if (y % 2) { res *= x; res %= MOD; }
x = x * x % MOD; y /= 2;
}
return res;
}
ll nCk(ll n, ll k) {
ll a = 1, b = 1;
for (int h = n - k + 1; h <= n; h++) { a *= h; a %= mod; }
for (int h = 1; h <= k; h++) { b *= h; b %= mod; }
return a * modpow(b, mod - 2) % mod;
}
//printf("%.10f\n", n);
typedef pair <ll, ll> P;
typedef pair <ld, ll> pp;
ll dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 };
//struct edge { ll to, cost; };
struct status {
ll to;
ll cost;
ll bai;
bool operator<(const status& rhs) const { return cost < rhs.cost; };
bool operator>(const status& rhs) const { return cost > rhs.cost; };
};
signed main() {
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
//cout << fixed << setprecision(15);
//input
string s; cin >> s;
string t; t.push_back(s[1]); t.push_back(s[2]); t.push_back(s[0]);
cout << t << endl;
return 0;
} |
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int N = 1e3 + 10;
int main(){
int a,b,c;
cin >> a>> b >>c;
if(a - b == b - c || a - c == c - b || b - a == a - c || b - c == c - a || c - a == a - b){
cout << "Yes" <<endl;
}else cout <<"No" <<endl;
return 0;
}
| #include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <utility>
#include <map>
#include <set>
#include <queue>
using namespace std;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define pb push_back
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
using ll = long long;
typedef vector<int> VI;
typedef vector<VI> VVI;
ll a,b,c;
int main() {
cin >> a>>b>>c;
if(a==b)cout<<c<<endl;
else if(a==c)cout<<b<<endl;
else if(c==b)cout<<a<<endl;
else cout<<"0"<<endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void potato() {
long long n;
cin >> n;
int ans = 0;
for(auto x = 1LL; ; x++) {
if(stoll(to_string(x) + to_string(x)) > n)
break;
ans++;
}
cout << ans << '\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int tt=1;
while(tt--) potato();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main(){
double N;
cin >> N;
if((int)(N*1.08)<206) cout << "Yay!" << endl;
else if((int)(N*1.08)==206) cout << "so-so" << endl;
else cout << ":(" << endl;
} |
#include<bits/stdc++.h>
typedef int LL;
typedef double dl;
#define opt operator
#define pb push_back
const LL maxn=1e6+9,mod=998244353,inf=0x3f3f3f3f;
LL Read(){
LL x(0),f(1); char c=getchar();
while(c<'0' || c>'9'){
if(c=='-') f=-1; c=getchar();
}
while(c>='0' && c<='9'){
x=(x<<3ll)+(x<<1ll)+c-'0'; c=getchar();
}return x*f;
}
void Chkmin(LL &x,LL y){
if(y<x) x=y;
}
void Chkmax(LL &x,LL y){
if(y>x) x=y;
}
LL add(LL x,LL y){
return x+=y,x>=mod?x-mod:x;
}
LL dec(LL x,LL y){
return x-=y,x<0?x+mod:x;
}
LL mul(LL x,LL y){
return 1ll*x*y%mod;
}
LL Pow(LL base,LL b){
LL ret(1); while(b){
if(b&1) ret=mul(ret,base); base=mul(base,base); b>>=1;
}return ret;
}
LL n,k;
LL fa[maxn],size[maxn],fac[maxn];
LL a[100][100];
LL Find(LL x){
return fa[x]==x?x:fa[x]=Find(fa[x]);
}
void Merge(LL x,LL y){
LL fx(Find(x)),fy(Find(y));
if(fx==fy) return;
size[fx]+=size[fy]; fa[fy]=fx;
}
void Init(){
for(LL i=1;i<=n;++i) fa[i]=i,size[i]=1;
}
int main(){
n=Read(); k=Read();
fac[0]=1; for(LL i=1;i<=n;++i) fac[i]=mul(fac[i-1],i);
for(LL i=1;i<=n;++i){
for(LL j=1;j<=n;++j) a[i][j]=Read();
}
Init();
for(LL x=1;x<=n;++x){
for(LL y=x+1;y<=n;++y){
LL flag(1);
for(LL i=1;i<=n;++i) flag&=(a[i][x]+a[i][y]<=k);
if(flag){
Merge(x,y);
}
}
}
LL ret(1);
for(LL i=1;i<=n;++i) if(Find(i)==i){
ret=1ll*ret*fac[size[i]]%mod;
}
Init();
for(LL x=1;x<=n;++x){
for(LL y=x+1;y<=n;++y){
LL flag(1);
for(LL i=1;i<=n;++i) flag&=(a[x][i]+a[y][i]<=k);
if(flag){
Merge(x,y);
}
}
}
for(LL i=1;i<=n;++i) if(Find(i)==i){
ret=1ll*ret*fac[size[i]]%mod;
}
printf("%d\n",ret);
return 0;
}
| #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.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>;
#define endl '\n'
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define fi first
#define se second
#define int long long
typedef long long ll;
typedef long double ld;
#define pii pair<int,int>
#define pll pair<ll,ll>
#define sz(x) ((long long)x.size())
#define fr(a,b,c) for(int a=b; a<=c; a++)
#define frev(a,b,c) for(int a=c; a>=b; a--)
#define rep(a,b,c) for(int a=b; a<c; a++)
#define trav(a,x) for(auto &a:x)
#define all(con) con.begin(),con.end()
#define done(x) {cout << x << endl;return;}
#define mini(x,y) x=min(x,y)
#define maxi(x,y) x=max(x,y)
const ll infl = 0x3f3f3f3f3f3f3f3fLL;
const int infi = 0x3f3f3f3f;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
//const int mod = 998244353;
const int mod = 1e9 + 7;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<vector<int>> vvi;
typedef vector<pair<int, int>> vpii;
typedef map<int, int> mii;
typedef set<int> si;
typedef set<pair<int,int>> spii;
typedef queue<int> qi;
uniform_int_distribution<int> rng(0, 1e9);
//DEBUG FUNCTIONS START
#define cerr cout
void __print(int x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "\n";}
template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef LOCAL
#define deb(x...) _print(x)
#else
#define deb(x...)
#endif
// DEBUG FUNCTIONS END
const int N = 1e3 + 5;
int a[N][N];
int x[N][N];
int y[N][N];
void solve(){
int n, k;
cin >> n >> k;
fr(i,1,n){
fr(j,1,n){
cin >> a[i][j];
}
}
int l = 0, h = 1e9;
while(l < h){
int mid = (l + h) / 2;
fr(i,1,n){
fr(j,1,n){
x[i][j] = (a[i][j] <= mid) ? -1 : 1;
}
}
fr(i,1,n){
fr(j,1,n){
y[i][j] = y[i-1][j] + y[i][j-1] - y[i-1][j-1] + x[i][j];
}
}
bool ok = 0;
fr(i,k,n){
fr(j,k,n){
int z = y[i][j] - y[i-k][j] - y[i][j-k] + y[i-k][j-k];
if(z <= 0){
ok = 1;
}
}
}
if(ok)
h = mid;
else
l = mid + 1;
}
cout << l << endl;
}
signed main() {
#ifdef LOCAL
(void)!freopen("input.txt","r",stdin);
(void)!freopen("output.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0), cin.tie(0);
cout << fixed << setprecision(15);
int t = 1;
//cin >> t;
while (t--)
solve();
return 0;
}
int powm(int a, int b){
int res = 1;
while (b) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
int divide(int a, int b) {
return (a % mod) * powm(b % mod, mod - 2) % mod;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define endl "\n"
#define F first
#define S second
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define debug(a) cout << #a << " = " << a << " ";
template<typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; }
template<typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
const int MOD=1000000007;
const int N=100005;
int n,m;
int a[1505][1505];
bool vis[1505][1505][4];
map< pair<int,int> , int > mp;
void Run(int x,int y,int dx,int dy) {
if (x <= 0 || y <= 0 || x > n || y > m)
return;
if (a[x][y] == -1)
return;
if (vis[x][y][mp[{dx, dy}]])
return;
vis[x][y][mp[{dx, dy}]] = true;
Run(x + dx, y + dy, dx, dy);
}
void solve() {
cin >> n >> m;
memset(a, 0, sizeof(a));
memset(vis, false, sizeof(vis));
long bulb, block;
cin >> bulb >> block;
while (bulb--) {
long x, y;
cin >> x >> y;
a[x][y] = 1;
}
while (block--) {
long x, y;
cin >> x >> y;
a[x][y] = -1;
}
mp[{-1, 0}] = 0;
mp[{1, 0}] = 1;
mp[{0, -1}] = 2;
mp[{0, 1}] = 3;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (a[i][j] == 1) {
Run(i, j, -1, 0);
Run(i, j, 1, 0);
Run(i, j, 0, -1);
Run(i, j, 0, 1);
}
}
}
int Ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++)
Ans += (vis[i][j][0] || vis[i][j][1] || vis[i][j][2] || vis[i][j][3]) * 1;
}
cout << Ans << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long int t=1;
// cin>>t; // DON'T FORGET IT. ^^^
for(long int tt=1;tt<=t;tt++){
solve();
}
cerr << "Time : " << 1000 * ((double) clock()) / (double) CLOCKS_PER_SEC << "ms\n";
return 0;
} | #include <iostream>
#include <vector>
//#include <string>
//#include <algorithm>
//#include <math.h>
//#include <queue>
//#include <stack>
//#include <iomanip>
// sometimes used
//#include <set>
//#include <map>
//#include <numeric>
//#include <list>
//#include <deque>
//#include <unordered_map>
typedef long long LL;
//typedef long double LD;
using namespace std;
//#define MOD 1000000007
//#define MOD 998244353
//#define MAX 100100
#define NIL -1
//#define INFTY 1000000000000000000
int main(){
LL h;
LL w;
LL n;
LL m;
cin >> h >> w >> n >> m;
vector<vector<LL>> brk(h, vector<LL>(w, 0));
vector<vector<bool>> x_sft(h, vector<bool>(w, false));
vector<vector<bool>> y_sft(h, vector<bool>(w, false));
LL a;
LL b;
for(LL i=0; i<n; i++){
cin >> a >> b;
a--;
b--;
brk[a][b]=1;
x_sft[a][b]=true;
y_sft[a][b]=true;
}
for(LL i=0; i<m; i++){
cin >> a >> b;
a--;
b--;
brk[a][b]=NIL;
}
for(LL i=0; i<h; i++){
for(LL j=1; j<w; j++){
if(x_sft[i][j-1]==true && brk[i][j]!=NIL){
x_sft[i][j]=true;
}
}
for(LL j=w-2; j>=0; j--){
if(x_sft[i][j+1]==true && brk[i][j]!=NIL){
x_sft[i][j]=true;
}
}
}
for(LL i=0; i<w; i++){
for(LL j=1; j<h; j++){
if(y_sft[j-1][i]==true && brk[j][i]!=NIL){
y_sft[j][i]=true;
}
}
for(LL j=h-2; j>=0; j--){
if(y_sft[j+1][i]==true && brk[j][i]!=NIL){
y_sft[j][i]=true;
}
}
}
/*
for(LL i=0; i<h; i++){
for(LL j=0; j<w; j++){
if(x_sft[i][j]==true){
cout << 1;
}else{
cout << 0;
}
}
cout << endl;
}
for(LL i=0; i<h; i++){
for(LL j=0; j<w; j++){
if(y_sft[i][j]==true){
cout << 1;
}else{
cout << 0;
}
}
cout << endl;
}
*/
LL ans=0;
for(LL i=0; i<h; i++){
for(LL j=0; j<w; j++){
if(x_sft[i][j]==true || y_sft[i][j]==true){
ans++;
}
}
}
cout << ans << endl;
return 0;
}
|
#undef _GLIBCXX_DEBUG
// Problem: F - Rook on Grid
// Contest: AtCoder - Panasonic Programming Contest (AtCoder Beginner Contest 186)
// URL: https://atcoder.jp/contests/abc186/tasks/abc186_f
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
ordered_set;
struct splitmix64_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = std::chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
template <typename K, typename V, typename Hash = splitmix64_hash>
using hash_map = __gnu_pbds::gp_hash_table<K, V, Hash>;
template <typename K, typename Hash = splitmix64_hash>
using hash_set = hash_map<K, __gnu_pbds::null_type, Hash>;
#define FOR(i,a,b) for(int i = (a); i <= (b); ++i)
#define FORD(i,a,b) for(int i = (a); i >= (b); --i)
#define RI(i,n) FOR(i,1,(n))
#define REP(i,n) FOR(i,0,(n)-1)
#define mini(a,b) a=min(a,b)
#define maxi(a,b) a=max(a,b)
#define pb push_back
#define st first
#define nd second
#define sz(w) (int) w.size()
typedef vector<int> vi;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<pii, int> para;
const ll inf = 1e18 + 7;
const ll maxN = 1e6 + 5;
const ll MOD = 1e9 + 7;
int n, m, o;
int row[maxN], col[maxN];
// sprawdz MODULO!
int main() {
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m >> o;
RI(i, m) col[i] = -1;
RI(i, n) row[i] = -1;
REP(_, o) {
int a, b; cin >> a >> b;
if (row[a] == -1) row[a] = b - 1;
else row[a] = min(row[a], b - 1);
if (col[b] == -1) col[b] = a - 1;
else col[b] = min(col[b], a - 1);
}
ll ans = 0;
vector<pii> vec;
if (row[1] == -1) row[1] = m;
RI(i, m) {
if (col[i] == -1) col[i] = n;
if (i <= row[1]) {
ans += col[i];
vec.pb({col[i], i});
} else {
vec.pb({0, i});
}
}
// cout << ans << endl;
ordered_set s;
sort(vec.begin(), vec.end());
int cnt = 0;
RI(i, col[1]) {
while (cnt != m && vec[cnt].st < i) {
s.insert(vec[cnt].nd);
cnt++;
}
if (row[i] == -1) row[i] = m;
// cout << i << " " << cnt << " " << row[i] << endl;
ans += s.order_of_key(row[i] + 1);
}
cout << ans;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define ALL(obj) (obj).begin(),(obj).end()
template<class T> using priority_queue_reverse = priority_queue<T,vector<T>,greater<T>>;
constexpr long long MOD = 1'000'000'000LL + 7; //'
constexpr long long MOD2 = 998244353;
constexpr long long HIGHINF = (long long)1e18;
constexpr long long LOWINF = (long long)1e15;
constexpr long double PI = 3.1415926535897932384626433L;
template <class T> vector<T> multivector(size_t N,T init){return vector<T>(N,init);}
template <class... T> auto multivector(size_t N,T... t){return vector<decltype(multivector(t...))>(N,multivector(t...));}
template <class T> void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; exit(0);}}
template <class T, class U>ostream &operator<<(ostream &o, const map<T, U>&obj) {o << "{"; for (auto &x : obj) o << " {" << x.first << " : " << x.second << "}" << ","; o << " }"; return o;}
template <class T>ostream &operator<<(ostream &o, const set<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const multiset<T>&obj) {o << "{"; for (auto itr = obj.begin(); itr != obj.end(); ++itr) o << (itr != obj.begin() ? ", " : "") << *itr; o << "}"; return o;}
template <class T>ostream &operator<<(ostream &o, const vector<T>&obj) {o << "{"; for (int i = 0; i < (int)obj.size(); ++i)o << (i > 0 ? ", " : "") << obj[i]; o << "}"; return o;}
template <class T, class U>ostream &operator<<(ostream &o, const pair<T, U>&obj) {o << "{" << obj.first << ", " << obj.second << "}"; return o;}
void print(void) {cout << endl;}
template <class Head> void print(Head&& head) {cout << head;print();}
template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) {cout << head << " ";print(forward<Tail>(tail)...);}
template <class T> void chmax(T& a, const T b){a=max(a,b);}
template <class T> void chmin(T& a, const T b){a=min(a,b);}
vector<string> split(const string &str, const char delemiter) {vector<string> res;stringstream ss(str);string buffer; while( getline(ss, buffer, delemiter) ) res.push_back(buffer); return res;}
int msb(int x) {return x?31-__builtin_clz(x):-1;}
void YN(bool flg) {cout << (flg ? "YES" : "NO") << endl;}
void Yn(bool flg) {cout << (flg ? "Yes" : "No") << endl;}
void yn(bool flg) {cout << (flg ? "yes" : "no") << endl;}
/*
* @title NBase - N進数
* @docs md/util/NBase.md
*/
class NBase{
public:
inline static vector<long long> translate(long long X,long long N) {
assert(abs(N)>1);
vector<long long> res;
while(1) {
long long b = (X%abs(N)+abs(N)) % abs(N);
res.push_back(b);
(X -= b) /= N;
if(X==0) break;
}
return res;
}
//Digit Sum
inline static constexpr long long digit_sum(long long N, long long K) {
long long sum = 0;
for (; N > 0; N /= K) sum += N % K;
return sum;
}
};
/**
* @url
* @est
*/
int main() {
cin.tie(0);ios::sync_with_stdio(false);
int A,B; cin >> A >> B;
cout << max(NBase::digit_sum(A,10),NBase::digit_sum(B,10)) << endl;
return 0;
}
|
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <sys/time.h>
using namespace std;
class XorShift128 {
private:
uint32_t x, y, z, w;
public:
XorShift128() : x(123456789), y(362436069), z(521288629), w(88675123) { }
uint32_t rnd() {
uint32_t t = x ^ (x << 11);
x = y;
y = z;
z = w;
return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
}
uint32_t rnd(int n) {
return rnd() % n;
}
uint32_t rnd(int l, int r) {
return rnd() % (r - l + 1) + l;
}
};
long long get_time() {
struct timeval tv;
gettimeofday(&tv, NULL);
long long result = tv.tv_sec * 1000LL + tv.tv_usec / 1000LL;
return result;
}
template<typename TYPE>
ostream& operator<<(ostream& ostr, const vector<TYPE>& vec) {
ostr << "(";
for(TYPE e : vec) {
ostr << e << ", ";
}
ostr << ")";
return ostr;
}
long long start;
XorShift128 xor_shift_128;
const int SIZE = 50;
class Solver {
int sh, sw;
int tile[SIZE][SIZE];
int point[SIZE][SIZE];
public:
Solver();
void run();
};
Solver::Solver() {
cin >> sh >> sw;
for(int hi = 0; hi < SIZE; ++hi) {
for(int wi = 0; wi < SIZE; ++wi) {
cin >> tile[hi][wi];
}
}
for(int hi = 0; hi < SIZE; ++hi) {
for(int wi = 0; wi < SIZE; ++wi) {
cin >> point[hi][wi];
}
}
}
void Solver::run() {
}
int main() {
start = get_time();
ios::sync_with_stdio(false);
cin.tie(nullptr);
Solver solver;
solver.run();
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define mat vector<vector<int>>
#define vi vector<int>
#define ff first
#define ss second
#define ll long long
#define pb push_back
#define pi pair<int, int>
#define inf 1000000000
#define mod 1000000007
#define rep(i,a,b) for(int i=a; i<b; i++)
#define rrep(i,a,b) for(int i=b; i>=a; i--)
//#define endl "\n"
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
char dir[4]={'D','R','U','L'};
//****DO NOT TOUCH ABOVE THIS LINE****//
//#define kk
const int maxN=1e5+5;
int arr[50][50];
int joiner[50][50];
int vis[50][50];
int maxCost;
string ans;
bool isValid(int x, int y){
if(x<0 || x>49 || y<0 || y>49 || vis[x][y]) return false;
return true;
}
void dfs(int x, int y, int cost, string path){
vis[x][y]=1;
rep(i,0,4){
int a=x+dx[i], b=y+dy[i];
if(isValid(a,b) && joiner[a][b]==joiner[x][y]){
vis[a][b]=1;
}
}
int cnt=0;
rep(i,0,4){
int a=x+dx[i], b=y+dy[i];
if(isValid(a,b)){
cnt++;
dfs(a,b,cost+arr[a][b],path+dir[i]);
}
}
if(cnt==0){
if(maxCost<cost){
maxCost=cost;
ans=path;
}
}
//vis[x][y]=0;
//rep(i,0,4){
//int a=x+dx[i], b=y+dy[i];
//if(joiner[a][b]==joiner[x][y]){
//vis[a][b]=0;
//}
//}
}
void solve(){
int x,y;
cin>>x>>y;
rep(i,0,50){
rep(j,0,50){
cin>>joiner[i][j];
}
}
rep(i,0,50){
rep(j,0,50){
cin>>arr[i][j];
}
}
dfs(x,y,arr[x][y],"");
cout<<ans<<endl;
}
//****DO NOT TOUCH BELOW THIS LINE****//
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout<<fixed<<setprecision(5);
#ifdef kk
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int T=1;
//cin>>T;
while(T--){
solve();
}
}
|
//#include "bits/stdc++.h"
#define _USE_MATH_DEFINES
#include<cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <algorithm>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <array>
#include <unordered_map>
#include<unordered_set>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <iterator>
#include<iomanip>
#include<complex>
#include<fstream>
#include<assert.h>
#include<stdio.h>
using namespace std;
#define rep(i,a,b) for(int i=(a), i##_len=(b);i<i##_len;i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
//#define int ll
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, double> pid;
typedef pair<double, int> pdi;
typedef pair<double, double> pdd;
typedef vector< vector<int> > mat;
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
signed main()
{
cin.tie(0);
ios::sync_with_stdio(false);
int R, C;
cin >> R >> C;
vector<vector<int>> A(R, vector<int>(C-1)), B(R-1, vector<int>(C));
rep(i, 0, R)rep(j, 0, C - 1)cin >> A[i][j];
rep(i, 0, R - 1)rep(j, 0, C)cin >> B[i][j];
vector<vector<pii>> G(R*C);
rep(i, 0, R)rep(j, 0, C)
{
int p = i * C + j;
if (i < R - 1)
{
int np = (i + 1)*C + j;
G[p].push_back(mp(np, B[i][j]));
}
if (j < C - 1)
{
int np = (i)*C + j + 1;
G[p].push_back(mp(np, A[i][j]));
}
if (j > 0)
{
int np = (i)*C + j - 1;
G[p].push_back(mp(np, A[i][j - 1]));
}
rep(h, 0, i)
{
int np = h * C + j;
G[p].push_back(mp(np, 1 + (i - h)));
}
}
vector<int> D(R*C, INF);
D[0] = 0;
priority_queue<pii, vector<pii>, greater<pii>> pq;
pq.push(mp(D[0], 0));
while (!pq.empty())
{
auto p = pq.top();
pq.pop();
if (D[p.second] < p.first)continue;
for (auto& e : G[p.second])
{
if (chmin(D[e.first], p.first + e.second))
{
pq.push(mp(D[e.first], e.first));
}
}
}
cout << D[R*C-1] << endl;
return 0;
} | #include<iostream>
#include<vector>
#include<math.h>
#include<algorithm>
#include<stack>
#include<list>
#include<queue>
#include<set>
#include<string>
#include<string.h>
#include <sstream>
#include<map>
#include<iomanip>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization("unroll-loops")
/*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>
#define ll long long
#define PI 3.14159265
#define yes cout<<"YES"<<"\n"
#define no cout<<"NO"<<"\n"
using namespace std;
const ll MOD = 1000000007;
ll gcd(ll a,ll b){
if(a==0)
return b;
return gcd(b%a,a);
}
ll lcm(ll a,ll b){
return (a*b)/gcd(a,b);
}
bool isPrime(ll n){
for(ll i = 2; i <=sqrt(n); i++)
if(n % i == 0)
return 0;
return 1;
}
string binaryRepresentation(int x){
string s="";
for (int i = 31; i >= 0; i--)
{
if (x&(1<<i)) cout << "1";
else cout << "0";
}
return s;
}
ll combination(ll n,ll r){
if(r==n || r==0)
return 1LL;
return combination(n-1,r-1) + combination(n-1,r);
}
bool isPowerOfTwo(ll n){
if(n==0)
return false;
return (ceil(log2(n)) == floor(log2(n)));
}
ll power(ll a,ll b){
ll ans=1;
while(b)
{
if(b&1)
ans=(ans*a)%MOD;
b/=2;
a=(a*a)%MOD;
}
return ans%MOD;
}
ll inverse(ll i){
if(i==1) return 1;
return (MOD-((MOD/i)*inverse(MOD%i))%MOD+MOD)%MOD;
}
string toString(ll a){
ostringstream temp;
temp << a;
return temp.str();
}
ll toNumber(string s){
stringstream num(s);
ll x=0;
num>>x;
return x;
}
int main()
{
//vector<vector<ll> >ar(n,vector<ll> (m,0));
//vector<ll>::iterator ptr;
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
ll n,k;
cin>>n>>k;
vector<vector<ll> >ar(n,vector<ll>(n,0));
for(ll i=0;i<n;i++)
{
for(ll j=0;j<n;j++)
{
ll x;
cin>>x;
ar[i][j]=x;
}
}
ll low=0;
ll high=1e9+1;
ll med=(k*k)/2+1;
ll r=k-1;
while(low<=high)
{
ll mid=(low+high)/2;
//cout<<" mid : "<<mid<<"\n";
/* Mid will be the possible median value */
vector<vector<ll> >temp=ar;
for(ll i=0;i<n;i++)
{
for(ll j=0;j<n;j++)
{
if(ar[i][j]>mid)
temp[i][j]=1;
else
temp[i][j]=0;
}
}
//for(ll i=0;i<n;i++)
//{
// for(ll j=0;j<n;j++)
// cout<<temp[i][j]<<" ";
// cout<<"\n";
//}
for(ll i=0;i<n;i++)
{
for(ll j=1;j<n;j++)
{
temp[i][j]+=temp[i][j-1];
}
}
for(ll i=1;i<n;i++)
{
for(ll j=0;j<n;j++)
{
temp[i][j]+=temp[i-1][j];
}
}
bool flag=false;
for(ll i=r;i<n;i++)
{
for(ll j=r;j<n;j++)
{
ll sum=temp[i][j];
ll row1=i-r;
ll col1=j-r;
if(row1>0)
sum-=temp[row1-1][j];
if(col1>0)
sum-=temp[i][col1-1];
if(row1>0 && col1>0)
sum+=temp[row1-1][col1-1];
//cout<<" i : "<<i<<" j : "<<j<<" sum : "<<sum<<"\n";
if(sum<med)
{
flag=true;
}
}
}
if(flag)
high=mid-1;
else
low=mid+1;
}
cout<<low;
return 0;
}
|
#include <bits/stdc++.h>
#define mid(l,r) ((l + r) >> 1)
#define lsb(x) (x & (-x))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define FOR(X,L,R) for(int X=L;X<R;++X)
#define endl '\n'
#define int long long
#define MOD ((int)1e9 + 7)
#define INF (0x3f3f3f3f)
#define LLINF (0x3f3f3f3f3f3f3f3fLL)
using namespace std;
template<typename T>
inline void input(vector<T>& v)
{
for(T& x: v)
cin >> x;
}
void solve()
{
int n; cin >> n;
vector<int> a(n), b(n), c(n);
input(a); input(b); input(c);
vector<int> cnt_pos(n);
FOR(i, 0, n)
{
cnt_pos[c[i] - 1] ++;
}
vector<int> sum_pos(n);
FOR(i, 0, n)
{
sum_pos[b[i] - 1] += cnt_pos[i];
}
int ans = 0;
FOR(i, 0, n)
{
ans += sum_pos[a[i] - 1];
}
cout << ans << endl;
}
signed main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
#if defined(CODEFORCES) || defined(CF)
int t; cin >> t;
while(t--) solve();
#else
solve();
#endif
return 0;
} | #include <iostream>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <sstream>
#include <limits.h>
#include <stdio.h>
using namespace std;
int main()
{
int n;
cin >> n;
int a[n];
for(int i=0; i<n; i++){
int tmp;
cin >> tmp;
a[i] = tmp;
}
int mx = 0;
int ans = 2;
for(int i=2; i<=1000; i++){
int cnt = 0;
for(int j=0; j<n; j++){
if(a[j]%i == 0){
cnt++;
}
}
if(mx < cnt){
mx = cnt;
ans = i;
}
}
cout << ans << endl;
return 0;
} |
#include <iostream>
#include <limits>
#include <vector>
#include <algorithm>
using namespace std;
long long count(int x, const vector<int> primes) {
// {i : i <= x, gcd(i, prod(primes) ) == 1}
int n = primes.size();
long long res = 0;
for (int msk = 0; msk < (1<<n); msk++) {
int c = 0;
int y = 1;
for (int i=0; i < n; i++) {
if (msk & (1 << i)) {
c++;
y *= primes[i];
}
}
if (c % 2 == 0) res += x / y;
else res -= x / y;
}
return res;
}
int main () {
int L, R;
cin >> L >> R;
long long answer = 0;
for (int x = L; x <= R; x++) {
if (x == 1) continue;
vector<pair<long long, long long> > factors;
vector<int> primes;
{
int f = x;
for (int i = 2; (long long)i * i <= f; i++) {
if (f % i != 0) continue;
long long ex = 1;
while (f % i == 0) {
f /= i;
ex *= i;
}
factors.push_back({ex, ex/i});
primes.push_back(i);
}
if (f > 1) primes.push_back(f);
}
//cerr << x << " " << R << " " << primes.size() << " " << count(R, primes) << endl;
answer += (long long)(R-x+1) - (count(R, primes) - count(x-1, primes));
answer -= R / x;
}
cout << answer * 2 << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define p(x,y) pair<x,y>
#define oset(x) tree<x, null_type, less<x>, rb_tree_tag, tree_order_statistics_node_update>
#define all(x) (x).begin(),(x).end()
#define ll long long
#define scan(a) for(auto &it:a)cin>>it;
#define print(a) {for(auto it:a)cout<<it<<" ";cout<<endl;}
#define out(x) cout<<((x)?"YES":"NO")<<endl;
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define int ll
void solve()
{
int l,r;
cin>>l>>r;
int cnt[r+1]={};//count of distinct prime if more than 1 same prime than INT_MIN
rep(i,2,r+1)
{
if(cnt[i]!=0)
continue;
for(ll j=i;j<=r;j+=i)
cnt[j]++;
for(ll j=i*i*1LL;j<=r;j+=i*i*1LL)
cnt[j]=INT_MIN;
}
//counting comprime in l,r;
int copairs=0;//coprime pairs(x,y) where x<y
rep(i,1,r+1)
{
if(cnt[i]<0)
continue;
ll mul=(r/i)-((l-1)/i);
ll pairs=(mul*(mul-1))/2;
if(cnt[i]%2)
copairs-=pairs;
else
copairs+=pairs;
}
copairs*=2LL;
int gcdgpairs=0; //(x,y) pairs where k*x=y k>1
for(int i=max(l,2LL);i<=r;i++)
gcdgpairs+=(r/i)-1;
gcdgpairs*=2LL;
int equalpairs=r-l+1;
int ans=(r-l+1)*(r-l+1)-gcdgpairs-copairs-equalpairs;
cout<<ans;
}
int32_t main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int t=1;
//init();
//cin>>t;
while(t--)
{
solve();
}
return 0;
}
|
//Common Header Simple over C++11
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define memset0(x) memset((x),0,sizeof(x));
//#define endl '\n'
#define count2(x) __builtin_popcount(x)
#define countleadingzero(x) __builtin_clz(x)
#define dd(x) cerr << #x << " = " << x << ' ';
#define de(x) cerr << #x << " = " << x << endl;
//inline char nc() {
// static char buf[1000000], *p = buf, *q = buf;
// return p == q && (q = (p = buf) + fread(buf, 1, 1000000, stdin), p == q)
// ? EOF
// : *p++;
//}
inline ll rd(){//LLONG_MIN LMAX=9,223,372,036,854,775,807
ll s=0,w=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0' && ch<='9')s=s*10+ch-'0',ch=getchar();
return s*w;
}
const int N=3e5+1;
const int maxn=2e5+1;
int tot;
struct seg{
int root,ls,rs;
long long val;
}tre[maxn*100];
inline void insert(int &now,int l,int r,int x,int val){//单点插入
if(!now) now=++tot;
tre[now].val+=val;
if(l==r) return ;
const int mid=(l+r-1)/2;//因为可能修改负数位置的值,求mid时不能用(cl+cr)/2而要用(cl+cr-1)/2
if(x<=mid)insert(tre[now].ls,l,mid,x,val);
else insert(tre[now].rs,mid+1,r,x,val);
}
int L=-1e6,R=1e6;
inline long long ask(int l,int r,int& now,int cl=L,int cr=R){//区间询问
if(!now) now=++tot;
if(cl>r||cr<l) return 0;
else if(cl>=l&&cr<=r) return tre[now].val;
else{
int mid=(cl+cr-1)/2;
return ask(l,r,tre[now].ls,cl,mid)
+ask(l,r,tre[now].rs,mid+1,cr);
}
}
inline void ins(int v){//单点插入
insert(tre[1].root,L,R,v,1); //根节点插入 值域L-R +1
}
inline void remove(int v){//单点删除
insert(tre[1].root,L,R,v,-1);//根节点插入 值域L-R +1
}
inline long long countless(int v){//查小于v的有多少个
return ask(L,v-1,tre[1].root);//计数[L,v-1]的桶
}
inline long long countgreat(int v){//查大于v的有多少个
return ask(v+1,R,tre[1].root);//计数[v+1,R]的桶
}
inline int Rank(int v){//查数是第几大
return countless(v)+1;//计数[L,v-1]的桶+1
}
inline int kth(int &now,int l,int r,int k){//查第k大的数
if(l==r) return l;
int mid=(l+r-1)/2;
if(tre[tre[now].ls].val>=k){
return kth(tre[now].ls,l,mid,k);
}
else{
return kth(tre[now].rs,mid+1,r,k-tre[tre[now].ls].val);
}
}
inline int pre(int v){//前驱
int t=countless(v);
return kth(tre[1].root,L,R,t);
}
inline int suc(int v){//后继
int t=tre[1].val-countgreat(v)+1;
return kth(tre[1].root,L,R,t);
}
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n;
cin>>n;
deque<int>dq;
for(int i=1;i<=n;++i){
int fafa;
cin>>fafa;
dq.push_back(fafa+1);
}
vector<ll>ans;
ll now=0;
for(int i=n-1;i>=0;--i){
now+=countless(dq[i]);
ins(dq[i]);
}
ans.push_back(now);
for(int i=1;i<n;++i){
int FR=dq.front();
dq.pop_front();
now-=countless(FR);
dq.push_back(FR);
now+=countgreat(FR);
ans.pb(now);
}
for(auto i:ans)cout<<i<<'\n';
return 0;
}
| //##### ##### ##### ##### ##### # ##### # #//
//# # # # # # # # # # # ## ##//
//##### ##### ##### # ##### # ##### # # #//
//## # # # # # # # # # # #//
//# # # # # # # # # # # # #//
//# # # # # # # # # # # # #//
//# # # # ##### ##### # # ##### # # # #//
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define pbds tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
#define ll long long
#define pb push_back
#define ppb pop_back
#define cs(n) cout<<n<<" "
#define rep(i,j,k) for(int i=j;i<k;i++)
#define rrep(i,j,k) for(int i=j;i>=k;i--)
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define de2(x) cout << #x << " " << x << endl;
#define rept(it,v) for(auto it=v.begin(); it!=v.end(); it++)
#define sz(v) (int)(v.size())
#define printdec(x,y) cout<< fixed << setprecision(y) << x
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define deb(x) cout<<#x<<" : "<<x<<"\n"
#define int long long
template<typename... T>
void read(T&... args) {
((cin >> args), ...);
}
template<typename... T>
void write(T&&... args) {
((cout << args << " "), ...);
}
template<typename... T>
void writen(T&&... args) {
((cout << args << "\n"), ...);
}
template<typename... T>
void cn(T&&... args) {
((cout << args << " "), ...);
cout << "\n";
}
ll bin(ll a, ll b, ll mod)
{
if (b == 0)
return 1;
if (b % 2 == 0)
return bin((a * a) % mod , b / 2 , mod) % mod;
return ((a % mod) * (bin((a * a) % mod , b / 2 , mod) % mod)) % mod;
}
void solve()
{
int n;
read(n);
int ar[n + 5];
rep(i, 0, n) {
read(ar[i]);
}
pbds s;
s.insert(ar[0]);
int cnt = 0;
rep(i, 1, n) {
s.insert(ar[i]);
int inv = s.order_of_key(ar[i] + 1);
cnt += sz(s) - inv;
}
cout << cnt << "\n";
rep(i, 0, n - 1) {
cnt -= ar[i];
cnt += (n - 1 - ar[i]);
cout << cnt << "\n";
}
}
signed main()
{
fastio;
int t;
t = 1;
// cin >> t;
while (t--)
{
solve();
}
#ifndef ONLINE_JUDGE
cout << "\nTime Elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " sec\n";
#endif
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
queue<pair<int,int> >Q;
VI ans;
map<int, int> m;
int a[300010];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i ++ ) {
cin >> a[i];
m[a[i]] ++;
}
sort(a + 1, a + n + 1);
//for (int i = 1; i <= n; i ++ ) cout << a[i] << " ";
for (int i = 1; i <= n; i ++ ){
if (ans.size() && ans.back() == a[i]);
else ans.push_back(a[i]);
}
long long sum = n * 1LL * (n - 1) / 2 ;
for(auto x : ans){
sum -= m[x] * 1LL * (m[x] - 1) / 2;
}
cout << sum;
return 0;
} | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ff first
#define ss second
#define int long long int
#define pb push_back
#define mp make_pair
#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_popcountint(x)
#define zrobits(x) __builtin_ctzint(x)
#define mod 1000000007
#define INF 1e18
#define ps(x,y) fixed<<setprecision(y)<<x
#define mk(arr,n,type) type *arr=new type[n];
#define w(x) int x; cin>>x; while(x--)
#define LCM(a,b) ((a*b)/__gcd(a,b))
#define PI 2*acos(0.0)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// If want to add duplicate values replace int with pair below
// and give the second pair value always unique in order to add duplicates in set.
typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> pbds;
// W,N,E,S
int dx[] = { -1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
void c_p_c()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
// seive prime
void seive_prime(int N)
{
vector<bool>prime(N + 1, true);
vector<int>prime_no;
prime[0] = false;
prime[1] = false;
for (int i = 4; i <= N; i += 2)
{
prime[i] = false;
}
for (int i = 3; i * i <= N; i += 2)
{
if (prime[i])
{
int m = i * i;
while (m <= N)
{
prime[m] = false;
m += i;
}
}
}
int i = 0;
for (auto v : prime)
{
if (v)
prime_no.push_back(i);
i++;
}
// return prime_no;
}
// Primality Test
bool isPrime(int n)
{
if (n == 2)
return true;
for (int i = 2; i * i <= n; i++)
{
if (n % i == 0)
return false;
}
return true;
}
// Modular Exponentiation
int modularExpo(int a, int b, int modVal)
{
int res = 1;
while (b > 0)
{
if (b & 1)
{
res = (res * a) % modVal;
}
b = b >> 1;
a = (a * a) % modVal;
}
return (res % modVal);
}
// Euler Totient function
void find_phi(vector<int>&phi)
{
for (int i = 1; i < phi.size(); i++)
phi[i] = i;
for (int i = 2; i < phi.size(); i++)
{
if (phi[i] == i)
{
for (int j = i; j < phi.size(); j += i)
{
phi[j] = phi[j] - (phi[j] / i);
}
}
}
// for(int i=3; i<phi.size(); i++)
// {
// phi[i]+=phi[i-1];
// }
}
int32_t main()
{
c_p_c();
// int case_no = 1;
// w(t)
// {
// }
int n;
cin >> n;
int arr[n];
unordered_map<int, queue<int> >umap;
for (int i = 0; i < n; i++)
{
int no;
cin >> no;
umap[no].push(i);
arr[i] = no;
}
int ans = 0;
for (int i = 0; i < n; i++)
{
int val = arr[i];
int size = umap[val].size();
size -= 1;
int remArr = (n - i - 1);
ans += (remArr - size);
umap[val].pop();
}
cout << ans;
#ifndef ONLINE_JUDGE
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
#endif
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";}
int main(){
int n;
cin>>n;
string s;
string t;
cin>>s>>t;
int a=0,b=0;
for(int i=0;i<n;i++){
if(s[i]=='1')a++;
if(t[i]=='1')b++;
}
if(a<b||a%2!=b%2){
cout<<-1<<"\n";
return 0;
}
queue<ll> c,d;
ll ans=0;
for(int i=0;i<n;i++){
if(t[i]=='1'){
d.emplace(i);
}
if(s[i]=='1'){
if(c.size()&&d.size()){
if(c.front()<d.front()){
ans+=i-c.front();
c.pop();
}else{
ans+=i-d.front();
d.pop();
}
}else if(c.size()){
ans+=i-c.front();
c.pop();
}else if(d.size()){
ans+=i-d.front();
d.pop();
}else{
c.emplace(i);
}
}
}
if(c.size()||d.size()){
cout<<-1<<"\n";
return 0;
}
cout<<ans<<"\n";
} | #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
#define dbg(X) std::cerr << "[DBG]("<<__FUNCTION__<<":"<<__LINE__<<") " << #X << ": " << X << std::endl;
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<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
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; }
template<typename T>
ostream& operator<<(ostream& s, const vec<T>& p) { for (const T& t : p) s << t << " "; return s; }
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n; cin >> n;
vl a(n); cin >> a;
for (int i=0; i<n; i+=2) a[i] *= -1;
map<ll,ll> seen;
seen[0]=1;
ll sum = 0, c = 0;
rep(i, 0, n) {
sum += a[i];
c += seen[sum]++;
}
cout << c << endl;
}
|
#ifdef GCC_VERSION
#include<bits/stdc++.h>
#endif
#include <iostream>
#include <set>
#include <vector>
#include <string>
#include <cmath>
#include <map>
#include <tuple>
#include <algorithm>
#include <functional>
#include <deque>
#include <queue>
#include <cstring>
#include <chrono>
#include <numeric>
#ifdef _DEBUG
#define DEBUG_INPUT
#endif
#ifdef DEBUG_INPUT
#include "../__common/Helper.h"
#endif
using ll = long long int;
using ld = long double;
static const ll INF = 6'000'000'000'000'000'000; // -INF for min value
template<typename T> using Vec = std::vector<T>;
template<typename T> using VecVec = std::vector<std::vector<T>>;
#define fe(e,cont) for(auto& e : cont) // others loop types by snippets
#define all(v) std::begin(v),std::end(v)
// only for md-arrays
template<class T> void unsafe_clear(T& val) { auto sz = sizeof(val); std::memset(&val, 0, sz); }
template<class T1, class T2> bool isMemEqual(const T1& v1, const T2& v2) { static_assert(sizeof(v1) == sizeof(v2)); return std::memcmp(&v1, &v2, sizeof(v1)) == 0; }
ll ipow(ll v, ll p) { ll res = 1; while (p--) { res *= v; } return res; }
ll div_ceil(ll a, ll b) { ll res = a / b; if (res * b != a) res++; return res; }
class Timer { // Sometimes measurement is required...
using sc = std::chrono::steady_clock; sc::time_point start;
public:
Timer() :start(sc::now()) {}
ll elapsedMs() { return std::chrono::duration_cast<std::chrono::milliseconds>(sc::now() - start).count(); }
};
extern const char* INPUT_DATA;
#ifdef DEBUG_INPUT
static Helper helper(INPUT_DATA);
#endif
template<class T>
T __get() {
#ifdef DEBUG_INPUT
return helper.get<T>();
#else
T val; std::cin >> val; return val;
#endif
}
ll get() { return __get<ll>(); }
std::string gets() { return __get<std::string>(); }
std::tuple<ll, ll> get2() { return { get(), get() }; } std::tuple<ll, ll, ll> get3() { return { get(), get(), get() }; } std::tuple<ll, ll, ll, ll>
get4() { return { get(), get(), get(), get() }; } std::tuple<ll, ll, ll, ll, ll> get5() { return { get(), get(), get(), get(), get() }; }
template<class T> void print(const T& val) { std::cout << val << " "; }
template<class T, class ... Types> void print(const T& var, const Types&...var2) { print(var); print(var2...); }
void endl() { std::cout << "\n"; }
void onetime(); void prepare() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); onetime(); }
void task(); void tcase() { ll t = get(); while (t--) task(); }
int main() {
Timer timer; prepare();
//tcase();
task();
#ifdef DEBUG_INPUT
while (helper.hasInput()) task();
//print("Execution time:", timer.elapsedMs());
#endif
return 0;
}
//-------------------------------------------------------------
void onetime()
{
}
const char* INPUT_DATA = R"(
4 9 9
5 5
15 5
5 15
15 15
)";
void task()
{
auto [n, S, D] = get3();
bool f = false;
for (ll i = 0; i < n; i += 1)
{
ll s = get();
ll d = get();
if (s < S && d > D)
{
f = true;
}
}
print(f?"Yes":"No");
}
| #include<bits/stdc++.h>
using namespace std;
int const maxn = 1e6+10;
long double const eps = 1e-3;
#define ll long long
int a[1100];
int b[1100];
int main(){
int n;
cin>>n;
int mini2=1e9;
int mini=1e9;
for(int i=1;i<=n;i++){
scanf("%d%d",&a[i],&b[i]);
mini=min(mini,a[i]+b[i]);
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(i==j) continue;
mini2=min(mini2,max(a[i],b[j]));
}
}
cout<<min(mini,mini2)<<endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main(){
int N, C=206;
scanf("%d", &N);
if( N+((N*8)/100) > C) cout<<":("<<'\n';
else if( N+((N*8)/100) == C) cout<<"so-so"<<'\n';
else cout<<"Yay!"<<'\n';
}
| #include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
long long int ans=1;
for(long long int i=2;i<=n;i++)
{
ans=(ans*i)/(__gcd(ans,i));
}
cout<<ans+1<<endl;
}
|
#include <bits/stdc++.h>
#include <chrono>
#include <ctime>
using namespace std;
using chrono::steady_clock;
using chrono::milliseconds;
#define sp " "
#define INF 1e18
#define INT_INF 1e9
typedef long long ll;
typedef pair<int,int> P;
chrono::steady_clock::time_point now_t;
chrono::steady_clock::time_point start_t;
random_device x;
mt19937 mt(x());
struct coord { int x; int y; };
const int N = 50;
vector<vector<int>> t(50,vector<int>(50));
vector<vector<int>> p(50,vector<int>(50));
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
char direct[] = {'D','R','U','L'};
struct state {
//状態を管理
int l;
vector<coord> route;
vector<char> direct;//D,R,U,L
vector<int> score;
vector<int> reach;//0 false 1 true
};
int get_rnd(int mod){
return mt()%mod;
}
bool TIME_CHECK( int Limit ){
now_t = steady_clock::now();
if(chrono::duration_cast<milliseconds>(now_t-start_t) < milliseconds(Limit))return true;
return false;
}
void enter(state &S){
//入力
int sx,sy;
cin >> sx >> sy;
coord s = {sx,sy};
S.route.push_back(s);
for(int i = 0;i < 2500;i++)S.reach.push_back(0);
for(int i = 0;i < 50;i++){
for(int j = 0;j < 50;j++){
cin >> t[i][j];
}
}
for(int i = 0;i < 50;i++){
for(int j = 0;j < 50;j++){
cin >> p[i][j];
}
}
S.reach[t[S.route[0].x][S.route[0].y]] = 1;
S.score.push_back(p[S.route[0].x][S.route[0].y]);
S.l = 1;
return;
}
void output(state S){
//出力
int l = S.l;
for(int i = 0;i < l-1;++i){
//cout << i << sp << S.route[i+1].x-S.route[0].x << sp << S.route[i+1].y-S.route[0].y << endl;
// if(S.route[i+1].x - S.route[i].x == 1)cout << "D";
// if(S.route[i+1].x - S.route[i].x == -1)cout << "U";
// if(S.route[i+1].y - S.route[i].y == 1)cout << "R";
// if(S.route[i+1].y - S.route[i].y == -1)cout << "L";
// //cout << endl;
cout << S.direct[i];
}
}
void dfs(state &S){
state best_s = S;
stack<state> st;
st.push(S);
while(!st.empty() && st.size() < 1000 && TIME_CHECK(1800)){
state s = st.top();
st.pop();
int cx,cy,l;
l = s.l;
cx = s.route[l-1].x;
cy = s.route[l-1].y;
for(int i = 0;i < 4;i++){
state T = s;
if(cx+dx[i] < 0 || cx+dx[i] >= 50 || cy+dy[i] < 0 || cy+dy[i] >= 50)continue;
else if(T.reach[t[cx+dx[i]][cy+dy[i]]] == 0){
coord c = {cx+dx[i],cy+dy[i]};
T.route.push_back(c);
T.score.push_back(T.score[l-1] + p[c.x][c.y]);
if(T.score[l] > best_s.score[best_s.l-1])best_s = T;
if(st.size() > 50){
if(T.score[l] < best_s.score[min(l,best_s.l - 1)]*9/10)continue;
}
T.reach[t[c.x][c.y]] = 1;
T.direct.push_back(direct[i]);
T.l += 1;
st.push(T);
}
}
}
S = best_s;
}
//void mountain( state &S){
//山登り
// int l = S.l;
// int n = get_rnd(l);
//}
// void AS ( state &S){
// //焼きなまし
// }
/*
近傍の取り方でスコア悪化の確立が高すぎるもの、
変化がおおきすぎるものを選ぶとスコア改善しないので、
適切な近傍の取り方を考える
思いつかないなら山登りでよい
極力連続でなだらかな関数にする
近傍の取り方は2種類用意したほうがいい(できれば)
焼きなましの温度は対数的に減らす->確率は線形になる
線形的に減らす->確率は指数的になる
rand()は遅いので使わない
*/
int main(){
start_t = steady_clock::now();
state S;
enter(S);
// while(true){
// int res = greed(S);
// if(res == 0)continue;
// else break;
// }
dfs(S);
output(S);
}
| #include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <iostream>
#include <cassert>
#include <cmath>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <cstdlib>
using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define fs first
#define se second
#define li long long
#define lint long long
#define pii pair<int, int>
#define vi vector<int>
#define forn(i, n) for (int i = 0; i < (int)n; i++)
#define fore(i, b, e) for (int i = (int)b; i <= (int)e; i++)
using Vertex = pii;
struct Move {
int fi, se, dir;
};
using Moves = vector<Move>;
const int QUERIES = 1000;
const int STARTW = 5000;
const int N = 30;
const int INF = 2e9;
const int dx[4] = {-1, +1, 0, 0};
const int dy[4] = {0, 0, -1, +1};
const int opp[4] = {1, 0, 3, 2};
const char symbol[4] = {'U', 'D', 'L', 'R'};
int edge[N][N][4];
bool valid(Vertex v) {
return v.fi >= 0 && v.fi < N && v.se >= 0 && v.se < N;
}
Vertex apply(Vertex v, int move) {
return {v.fi + dx[move], v.se + dy[move]};
}
pair<Moves, int> find_path(Vertex s, Vertex f) {
vector<vi> dist(N, vi(N, INF));
vector<vi> last_move(N, vi(N));
set<pair<int, Vertex>> order;
order.insert({0, s});
dist[s.fi][s.se] = 0;
while (!order.empty()) {
auto cur = *order.begin();
order.erase(order.begin());
Vertex v = cur.se;
// cerr << v.fi << " " << v.se << endl;
if (v == f) {
break;
}
if (dist[v.fi][v.se] != cur.fi) {
continue;
}
forn(move, 4) {
Vertex u = apply(v, move);
if (valid(u)) {
int new_dist = cur.fi + edge[v.fi][v.se][move];
if (new_dist < dist[u.fi][u.se]) {
dist[u.fi][u.se] = new_dist;
last_move[u.fi][u.se] = move;
order.insert({new_dist, u});
}
}
}
}
vector<Move> moves;
Vertex cur = f;
while (cur != s) {
Vertex prev = apply(cur, opp[last_move[cur.fi][cur.se]]);
moves.pb({prev.fi, prev.se, last_move[cur.fi][cur.se]});
cur = prev;
}
reverse(moves.begin(), moves.end());
return {moves, dist[f.fi][f.se]};
}
string moves_to_string(const Moves& moves) {
vector<char> s;
for (Move move : moves) {
s.pb(symbol[move.dir]);
}
return string(s.begin(), s.end());
}
void upd(Move move, int prediction) {
// cerr << move.fi << " " << move.se << " " << move.dir << " " << prediction << endl;
int new_val = (edge[move.fi][move.se][move.dir] == STARTW ? prediction : (edge[move.fi][move.se][move.dir] + prediction) / 2);
edge[move.fi][move.se][move.dir] = new_val;
}
Move opp_move(Move move) {
Vertex dest = apply({move.fi, move.se}, move.dir);
return {dest.fi, dest.se, opp[move.dir]};
}
int main() {
int avg = STARTW;
forn(i, N)
forn(j, N)
forn(dir, 4) {
edge[i][j][dir] = avg;
}
forn(q, QUERIES) {
Vertex s, f;
cin >> s.fi >> s.se >> f.fi >> f.se;
auto result = find_path(s, f);
// cerr << result.se << endl;
Moves& moves = result.fi;
string path = moves_to_string(moves);
cout << path << endl;
fflush(stdout);
int correct_length;
cin >> correct_length;
for (Move move: moves) {
double ratio = (double)edge[move.fi][move.se][move.dir] / result.se;
//double ratio = 1. / moves.size();
double prediction = ratio * correct_length;
upd(move, prediction);
upd(opp_move(move), prediction);
}
// recalc_avg();
}
}
|
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
#define fore(i, a) for (auto &i : a)
#define all(x) (x).begin(), (x).end()
//#pragma GCC optimize ("-O3")
using namespace std;
void _main();
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
_main();
}
typedef long long ll;
const int inf = INT_MAX / 2;
const ll infl = 1LL << 60;
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;
}
//---------------------------------------------------------------------------------------------------
/*---------------------------------------------------------------------------------------------------
∧_∧
∧_∧ (´<_` ) Welcome to My Coding Space!
( ´_ゝ`) / ⌒i @hamayanhamayan0
/ \ | |
/ / ̄ ̄ ̄ ̄/ |
__(__ニつ/ _/ .| .|____
\/____/ (u ⊃
---------------------------------------------------------------------------------------------------*/
int N, M;
//---------------------------------------------------------------------------------------------------
void _main()
{
cin >> N >> M;
map<int, vector<int>> pawns;
rep(i, 0, M)
{
int X, Y;
cin >> X >> Y;
pawns[X].push_back(Y);
}
set<int> ans;
ans.insert(N);
fore(p, pawns)
{
set<int> ng;
set<int> ok;
fore(y, p.second)
{
if (ans.count(y - 1))
ok.insert(y);
else if (ans.count(y + 1))
ok.insert(y);
else
ng.insert(y);
}
fore(i, ok) if (0 <= i && i <= 2 * N) ans.insert(i);
fore(i, ng) ans.erase(i);
}
cout << ans.size() << endl;
}
| #include <bits/stdc++.h>
using namespace std;
int main(){
int N, M;
while(scanf("%d%d", &N, &M) > 0){
map<int, unordered_set<int>> pawns;
for(int i = 0; i < M; i++){
int X, Y;
scanf("%d%d", &X, &Y);
pawns[X].insert(Y);
}
unordered_set<int> reachable;
reachable.insert(N);
for(auto kv : pawns){
unordered_set<int> row_pawns = kv.second;
vector<int> to_remove, to_insert;
for(int Y : row_pawns){
if(reachable.count(Y)){
to_remove.push_back(Y);
}
if(reachable.count(Y-1) or
reachable.count(Y+1)){
to_insert.push_back(Y);
}
}
for(int Y : to_remove){
reachable.erase(Y);
}
for(int Y : to_insert){
reachable.insert(Y);
}
}
printf("%d\n", (int)reachable.size());
}
}
|
#include <bits/stdc++.h>
#define rep(i,n) for (int i=0; i<n; i++)
#define rng(i,l,r) for (int i=l; i<r; i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define endl "\n"
#define INF 1000000007 // 1e9+7
#define LINF 1152921504606846976 // 1LL<<60
using namespace std;
using ll = long long;
using P = pair<int,int>;
template <class T> using V = vector<T>;
template<class T> bool chmin(T& a, T b){ if (a > b){ a = b; return true; } return false; }
template<class T> bool chmax(T& a, T b){ if (a < b){ a = b; return true; } return false; }
struct in { template <class T> operator T() { T t; std::cin >> t; return t; } };
int dx[8] = {0,1,0,-1,1,1,-1,-1};
int dy[8] = {1,0,-1,0,1,-1,1,-1};
auto solve() {
int h = in(), w = in();
V<V<int>> s(h+1, V<int>(w+1)), t(h+1, V<int>(w+1));
int n = in();
int m = in();
rep(i,n) {
int a, b;
cin >> a >> b;
s[a][b]++;
}
rep(i,m) {
int c, d;
cin >> c >> d;
s[c][d]--;
}
int state;
rng(i,1,h+1) {
state = 0;
for (auto j=1; j<w+1; j++) {
if (s[i][j] == 1) state = 1;
if (s[i][j] == -1) state = 0;
if (state) t[i][j] = 1;
}
state = 0;
for (auto j=w; j>=1; j--) {
if (s[i][j] == 1) state = 1;
if (s[i][j] == -1) state = 0;
if (state) t[i][j] = 1;
}
}
rng(j,1,w+1) {
state = 0;
for (auto i=1; i<h+1; i++) {
if (s[i][j] == 1) state = 1;
if (s[i][j] == -1) state = 0;
if (state) t[i][j] = 1;
}
state = 0;
for (auto i=h; i>=1; i--) {
if (s[i][j] == 1) state = 1;
if (s[i][j] == -1) state = 0;
if (state) t[i][j] = 1;
}
}
int ans = 0;
rng(i,1,h+1) rng(j,1,w+1) ans += t[i][j];
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << solve() << endl;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long ul;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ull> vull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
template<typename T>
using maxheap = priority_queue<T>;
template<typename T>
using minheap = priority_queue<T, vector<T>, greater<>>;
template<class t, class u>
ostream &operator<<(ostream &os, const pair<t, u> &p) {
os << p.first << " " << p.second;
return os;
}
template<class t, class u>
istream &operator>>(istream &is, pair<t, u> &p) {
is >> p.first >> p.second;
return is;
}
template<typename T>
istream &operator>>(istream &is, vector<T> &v) {
for (auto &i: v) {
is >> i;
}
return is;
}
template<typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
for (auto i: v) {
os << i << " ";
}
os << endl;
return os;
}
string f(string &s) {
return "asd" + s;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(nullptr);
// read the test case from test.txt
// freopen("test/test1.txt","r", stdin);
// freopen("test/ans1.txt", "w", stdout);
// std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
// program begin
int h, w, n, m;
cin >> h >> w >> n >> m;
vector<vector<int>> v(h, vi(w, 0));
for (int i = 0; i < n; i++) {
int a, b;
cin >> a >> b;
// bulb
v[a - 1][b - 1] = 1;
}
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
// block
v[a - 1][b - 1] = 2;
}
int ans = 0;
map<pii, bool> c;
for (int i = 0; i < h; i++) {
int cnt = 0;
bool bul = false;
for (int j = 0; j < w; j++) {
if (v[i][j] == 0) {
if (bul) {
c[pii{i, j}] = true;
ans++;
} else {
cnt++;
}
} else if (v[i][j] == 2) {
cnt = 0;
bul = false;
} else if (v[i][j] == 1) {
c[pii{i, j}] = true;
if (!bul) {
for (int t = j - 1; t >= 0; t--) {
if (v[i][t] == 0) {
c[pii{i, t}] = true;
} else {
break;
}
}
}
bul = true;
ans += cnt + 1;
cnt = 0;
}
}
}
// cout << ans << endl;
for (int j = 0; j < w; j++) {
int cnt = 0;
bool bul = false;
for (int i = 0; i < h; i++) {
if (v[i][j] == 0) {
if (bul && !c[pii{i, j}]) {
ans++;
} else if (!c[pii{i, j}]) {
cnt++;
}
} else if (v[i][j] == 2) {
cnt = 0;
bul = false;
} else if (v[i][j] == 1) {
bul = true;
ans += cnt;
if (!c[pii{i, j}]) {
ans++;
}
cnt = 0;
}
}
}
cout << ans;
// program end
// std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
// std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count() << "[ms]" << std::endl;
return 0;
}
|
#include <algorithm>
#include <cmath>
#include <cstdlib>
template <typename T, typename P>
T bisect(T ok, T ng, const P& is_ok)
{
while (std::abs(ok - ng) > 1) {
auto mid = ok + (ng - ok) / 2;
if (is_ok(mid)) {
ok = mid;
}
else {
ng = mid;
}
}
return ok;
}
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int P = 5;
int main()
{
int N;
cin >> N;
vector<vector<int>> A(N, vector<int>(P));
for (auto& v : A) {
for (auto& x : v) {
cin >> x;
}
}
int ok = 0;
int ng = 1000 * 1000 * 1000 + 1;
auto is_ok = [&](int m) {
auto B = A;
for (auto& v : B) {
for (auto& x : v) {
if (x < m) {
x = 0;
}
else {
x = 1;
}
}
}
sort(B.begin(), B.end());
B.erase(unique(B.begin(), B.end()), B.end());
const int M = B.size();
for (int i = 0; i < M; ++i) {
for (int j = 0; j < M; ++j) {
for (int k = 0; k < M; ++k) {
vector<int> T(P);
for (int s = 0; s < P; ++s) {
T[s] = max({ B[i][s], B[j][s], B[k][s] });
}
if (*min_element(T.begin(), T.end()) == 1) {
return true;
}
}
}
}
return false;
};
cout << bisect(ok, ng, is_ok) << endl;
}
| #include<iostream>
#include<algorithm>
#include<map>
#include<queue>
#include<cmath>
#include<stack>
#include<string>
#include<vector>
#include<set>
#define rep(i,n) for(int i = 0;i < n;i++)
using namespace std;
std::vector<bool> IsPrime;
void sieve(long long int max){
if(max+1 > IsPrime.size()){ // resizeで要素数が減らないように
IsPrime.resize(max+1,true); // IsPrimeに必要な要素数を確保
}
IsPrime[0] = false; // 0は素数ではない
IsPrime[1] = false; // 1は素数ではない
for(long long int i=2; i*i<=max; ++i) // 0からsqrt(max)まで調べる
if(IsPrime[i]) // iが素数ならば
for(size_t j=2; i*j<=max; ++j) // (max以下の)iの倍数は
IsPrime[i*j] = false; // 素数ではない
}
int main(void){
//n以下の素数列挙
long long int n;
cin >> n;
long long int ans=0;
set<long long int> se;
// sieve(n);
//n = sqrt(n);
long long int n_temp=n;
for(long long int i=2;i*i<=n;i++){
long long int j=2;
long long int kari=i*i;
while((kari)<=n){
se.insert(kari);
kari*=i;
}
}
//求めるのは表せないものなので、
cout << n-se.size()<< endl;
return 0;
} |
//bismillahir rahmanir rahim
//@uthor : mubin_akib
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair <int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i,a,b) for (int i = (b) - 1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a) - 1; i >= 0; i--)
#define REP(i, a, b) for(int i = (a); i <= (b); ++i)
#define trav(a,x) for(auto& a : x)
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int mod = 1e9 + 7;
const char nl = '\n';
const int N = 10005;
void solve() {
string n;
ll k;
cin >> n >> k;
while(k--){
if(stoll(n) % 200 == 0){
n = to_string(stoll(n) / 200);
}
else{
n += "200";
}
}
cout << n << nl;
}
int main() {
#ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
// int t;
// cin >> t;
// while(t--){
// solve();
// }
return 0;
} | #include <bits//stdc++.h>
using namespace std;
#define int long long
#define rep(i,n,k) for(int i=n;i<k;i++)
#define P make_pair
#define F first
#define S second
#define M LLONG_MAX
signed main(void){
int n,m,p,q,ans;
ans=0;
string s;
cin>>n>>m;
rep(i,0,m){
if(n%200)
n=n*1000+200;
else
n=n/200;
}
cout<<n<<endl;
} |
/* @memendra_singh */
#include<bits/stdc++.h>
using namespace std;
#define m7 1000000007
#define m9 1000000009
#define ll long long
#define boost ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define F first
#define S second
#define pb push_back
#define f1(i,n) for(int i=0;i<n;i++)
#define all(c) c.begin(),c.end()
#define ldb long double
#define pi acos(-1)
int main()
{
boost;
int t = 1;
//cin>>t;
while(t--)
{
string s;
cin>>s;
int ans = 0;
for(int i=0;i<=9999;i++)
{
vector<bool> arr(10,false);
int x = i;
for(int j=0;j<4;j++)
{
//int k = x%10;
arr[x%10] = true;
x = x/10;
}
bool flag = true;
for(int j=0;j<10;j++)
{
if(s[j] == 'o' && arr[j]==false)
flag = false;
if(s[j] == 'x' && arr[j] == true)
flag = false;
}
if(flag)
ans++;
}
cout<<ans<<'\n';
}
return 0;
} | #include<bits/stdc++.h>
#define LOCAL
#define ft first
#define sd second
#define endl '\n'
#define io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define weishu setprecision
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lowbit(x) ((x)&(-x))
#define debug1() cout<<"hello world!"<<endl
#define debug2() cout<<"hello"<<endl
#define RAND(a,b) (rand() % ((b)-(a)+1))+ (a)
#define SRAND srand((unsigned)time(NULL))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
const lld pi = acos(-1.0);
const ll maxn=1e5+5;
ll mod=123456789;
void init(void);
ll pow_mod(ll a,ll k);
ll ni(int x);
void get_prime();
//ll prime[maxn];
//bool v[maxn];
int a[11],c[11];
vector<int> b;
void solve(){
string t;
cin >> t;
int len=t.size();
for(int i=0;i<len;i++){
if(t[i]=='o')b.push_back(i);
else if(t[i]=='?')c[i]++;
}
if(b.size()>=5){
cout<<0<<endl;
}
else{
int j,num=0,ans=0;
for(int i=0;i<=9999;i++){
for(int k=0;k<11;k++)a[k]=0;
j=i;
num=4;
while(j){
a[j%10]++;
j/=10;
num--;
}
a[0]+=num;
int flag=1;
for(int k=0;k<b.size();k++){
if(a[b[k]]!=0)a[b[k]]=0;
else{
flag=0;
break;
}
}
if(flag==1){
for(int i=0;i<10;i++){
if(a[i]!=0){
if(c[i]!=0)continue;
else{
flag=0;
break;
}
}
}
}
if(flag==1)ans++;
}
cout<<ans<<endl;
}
}
signed main(){
io;
//init();
//get_prime();
ll T=1;
//cin >> T;
while(T--){
solve();
}
return 0;
}
void init (void)
{
#ifdef LOCAL
freopen ("data.txt", "w", stdout);
#endif
}
ll pow_mod(ll a,ll k)
{
ll ans = 1;
a %= mod;
while(k)
{
if(k & 1) ans *= a;
a = (a * a) % mod;
k >>= 1;
ans %= mod;
}
return ans % mod;
}
ll ni(int x){
return pow_mod(x,mod-2);
}
//void get_prime(){
// int count = 1;
// for (int i = 2; i<maxn; i++){
// if(!v[i]){
// prime[count++] = i;
// }
// for (int t = 1; prime[t] * i < maxn &&t<count; t++){
// v[prime[t] * i] = 1;
// if(i % prime[t] == 0)
// break;
// }
// }
//}
|
#include <iostream>
#include <map>
#include <algorithm>
#include <string>
#include <vector>
#include <numeric>
#include <cmath>
#include <queue>
#include <iomanip>
#include <functional>
#include <stack>
#define CHMAX(a,b) a = std::max(a,b)
#define CHMIN(a,b) a = std::min(a,b)
#define MAXMIN(a,b,c) std::max(a,min(b,c))
#define CHABS(a) a = std::abs(a)
#define COUT(a) std::cout << a << std::endl
#define CERR(a) std::cerr << a << std::endl
#define FOR(n) for(lli i = 0; i < n; i++)
using namespace std;
using lli = long long int;
using pll = pair<lli, lli>;
using tlll = tuple<lli, lli, lli>;
using vll = vector<lli>;
lli mod197 = 1000000007LL;
lli INF = 10000000000000;
// ax + by = gcd(a,b) 最大公約数
template< typename T >
T extgcd(T a, T b, T& x, T& y) {
T d = a;
if (b != 0) {
d = extgcd(b, a % b, y, x);
y -= (a / b) * x;
}
else {
x = 1;
y = 0;
}
return d;
}
int members[3000][5] = {};
int sorted[5][3000] = {};
int main(void) {
int N;
cin >> N;
for (int i = 0; i < N; i++) {
for (int j = 0; j < 5; j++) {
cin >> members[i][j];
sorted[j][i] = members[i][j];
}
}
for (int j = 0; j < 5; j++) {
sort(sorted[j],sorted[j] + N);
}
int result = 0;
for (int i = 0; i < N; i++) {
for (int k = 0; k < N; k++) {
int t[5] = {};
for (int m = 0; m < 5; m++) {
t[m] = max({ members[i][m],members[k][m]});
}
for (int m = 0; m < 5; m++) {
int tmp = t[m];
t[m] = sorted[m][N - 1];
result = max(result, *min_element(t, t + 5));
t[m] = tmp;
}
}
}
cout << result << endl;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define rep(i, r, n) for(int i = r; i < (int)(n); i++)
// 答えで二分探索
int N, X[3030][5];
int getMask(int i, int lim) {
int res = 0;
rep(j, 0, 5) if(X[i][j] >= lim) res += 1 << j;
return res;
}
int cnt[1 << 5];
bool check(int lim) {
rep(i, 0, 1 << 5) cnt[i] = 0;
rep(i, 0, N) cnt[getMask(i, lim)]++;
rep(msk1, 0, 1 << 5) rep(msk2, 0, 1 << 5) rep(msk3, 0, 1 << 5) {
if((msk1 | msk2 | msk3) == (1 << 5) -1 && cnt[msk1] > 0 && cnt[msk2] > 0 && cnt[msk3] > 0) return true;
}
return false;
}
int main() {
cin >> N;
rep(i, 0, N) rep(j, 0, 5) cin >> X[i][j];
int ok = 0, ng = 1000000001;
while(ok + 1 != ng) {
int mid = (ok + ng) / 2;
if(check(mid)) ok = mid;
else ng = mid;
}
cout << ok << endl;
} |
// E - Rush Hour 2
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
#define vec vector
using vl = vec<ll>;
#define rep(i,e) for(int i=0;i<(e);++i)
using PR = pair<ll,int>;
// 本問用に修正
// dijkstra O(|E|log|V|)
int V;
vec<tuple<int,int,int,int>> E;
vec<vec<int>> G;
ll INF = 1e18;
ll cost(ll c, ll d, ll t){
ll ok = t, ng = d+1;
while(ng - ok > 1){
ll m = (ok+ ng) / 2;
(m*(m+1) < d? ok:ng) = m;
}
return ok + c + d / (ok+1);
}
vl dijkstra(int s){
vl dist(V, INF);
dist[s] = 0;
priority_queue<PR, vec<PR>, greater<PR>> pq;
pq.push({0, s});
while(pq.size()){
auto[t, v] = pq.top(); pq.pop();
if(dist[v] < t) continue;
for(int&i:G[v]){
auto&[a, b, c, d] = E[i];
int u = a; // b->a
if(u == v) u = b; // a->b
ll nt = cost(c, d, dist[v]);
if(dist[u] <= nt) continue;
dist[u] = nt;
pq.push({nt, u});
}
}
return dist;
}
int main(){
int m; cin>>V>>m;
G.resize(V);
rep(i, m){
int a, b, c, d; cin>>a>>b>>c>>d; --a, --b;
E.emplace_back(a, b, c, d);
G[a].push_back(i), G[b].push_back(i);
}
ll ans = dijkstra(0)[V-1];
if(ans == INF) ans = -1;
cout<< ans <<endl;
}
| #include<bits/stdc++.h>
#define int long long
#define nb 200010
using namespace std;
bool vis[nb];
int n, m, cnt, head[nb], dis[nb];
struct edge{int to, next, c, d;}e[nb];
void add_edge(int u, int v, int c, int d){
e[++cnt].to = v;
e[cnt].c = c;
e[cnt].d = d;
e[cnt].next = head[u];
head[u] = cnt;
}
struct node{
int id, d;
bool operator < (const node &tmp) const{
return tmp.d < d;
}
};
priority_queue<node> q;
void dijkstra(){
memset(dis, 0x3f, sizeof(dis));
dis[1] = 0;
q.push((node){1, 0});
while(!q.empty()){
node tmp = q.top();
q.pop();
int u = tmp.id;
if(vis[u]) continue;
vis[u] = 1;
for(int i = head[u]; i; i = e[i].next){
int c = e[i].c, d = e[i].d, v = e[i].to, s = sqrt(d), t;
if(dis[u] < s) t = s + d / (s + 1) + c;
else t = dis[u] + d / (dis[u] + 1) + c;
if(dis[v] > t){
dis[v] = t;
if(!vis[v]){
q.push((node){v, dis[v]});
}
}
}
}
}
signed main(){
cin >> n >> m;
for(int i = 1; i <= m; i++){
int u, v, c, d;
cin >> u >> v >> c >> d;
add_edge(u, v, c, d);
add_edge(v, u, c, d);
}
dijkstra();
cout << (dis[n] > 1e16 ? -1 : dis[n]);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
int main(){
int N,K;
cin >> N >> K;
vector<vector<int>> T(N,vector<int>(N));
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
cin >> T[i][j];
}
}
vector<int> town = {1,2,3,4,5,6,7};
int ans = 0;
do{
int cnt = 0;
cnt = T[0][town[0]];
for(int i=1; i<N-1; i++){
cnt += T[town[i-1]][town[i]];
}
cnt += T[town[N-2]][0];
if(cnt == K) ans++;
}while(next_permutation(town.begin(),town.begin()+N-1));
cout << ans << endl;
return 0;
} | #include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
#include<cmath>
#include<cstdio>
#include<tuple>
#include<bitset>
#include<map>
using namespace std;
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;++i)
#define ALL(x) x.begin(),x.end()
#define ll long long
using lint=long long;
#define debug(output) cout<<#output<<"= "<<output<<endl
typedef pair<int,int> P;
const int inf=1000000007;
const int MOD=1000000007;
signed main(){
lint n,x;cin>>n>>x;
vector<lint> a(n);
rep(i,n)cin>>a[i];
a.push_back(((x/a[n-1])+2)*a[n-1]);
vector<lint> dp(2,0);
dp[0]=1;
rep(i,n){
vector<lint> nxt(2,0);
lint d=x%a[i+1];
d/=a[i];
if(d==0){
nxt[1]=dp[1];
}
else{
nxt[1]=dp[1]+dp[0];
}
if(d==a[i+1]/a[i]-1){
nxt[0]=dp[0];
}
else{
nxt[0]=dp[1]+dp[0];
}
swap(dp,nxt);
}
cout<<dp[0]<<"\n";
return 0;
} |
#include <iostream>
using namespace std;
int main() {
int N;
cin >> N;
cout << (N + 99) /100 << endl;
} | #include<bits/stdc++.h>
using namespace std;
long long k,n;
vector<long long>v;
int main(){
cin>>n;
for(k=1;k<=sqrt(n);k++){
if(n%k==0){
v.push_back(k);
if(k*k!=n) v.push_back(n/k);
}
}
sort(v.begin(),v.end());
for(k=0;k<v.size();k++)
cout<<v[k]<<endl;
} |
#include <bits/stdc++.h>
using namespace std;
int main(){
int N, M, Q; cin >> N >> M >> Q;
vector<pair<int, int>> vp;
for(int i = 0; i < N; i++){
int w, v; cin >> w >> v;
vp.emplace_back(w, v);
}
vector<int> X;
for(int i = 0; i < M; i++){
int x; cin >> x;
X.push_back(x);
}
sort(vp.begin(), vp.end(), [](pair<int,int> a, pair<int,int> b){
return a.second > b.second;
});
for(int i = 0; i < Q; i++){
int left, right; cin >> left >> right;
left--; right--;
vector<int> C;
for(int i = 0; i < M; i++){
if(i >= left && i <= right)continue;
C.push_back(X.at(i));
}
sort(C.begin(), C.end());
int len = C.size();
vector<bool> vb(len, true);
int64_t ans = 0;
for(int i = 0; i < N; i++){
for(int p = 0; p < len; p++){
if(vp.at(i).first <= C.at(p) && vb.at(p)){
ans += vp.at(i).second;
vb.at(p) = false;
break;
}
}
}
cout << ans << endl;
}
} | #include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
#define rep(i,x,y) for(ll i=(x);i<(y);i++)
#define rrep(i,x,y) for(ll i=(ll)(y)-1;i>=(x);i--)
#define all(x) (x).begin(),(x).end()
#define itrout(x) for(int i=0;i<x.size();i++) {cout << x[i] << (i==x.size()-1 ? "\n" : " ");}
#ifdef LOCAL
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl
#define debugbit(x, n) cerr << #x << " = " << bitset<n>(x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl
#define itrdebug(x) cerr << #x << " "; for (auto & el : (x)) {cerr << (el) << " ";} cerr << endl
#define dassert(...) assert(__VA_ARGS__)
#else
#define debug(x)
#define debugbit(x, n)
#define itrdebug(x)
#define dassert(...)
#endif
//#define int long long
//using mint = atcoder::modint;
typedef long long ll;
const ll MOD = 1e9 + 7;
const long double EPS = 1e-8;
signed main(){
int N, M, Q;
cin >> N >> M >> Q;
vector<int> W(N), V(N), X(M);
rep(i,0,N) cin >> W[i] >> V[i];
rep(i,0,M) cin >> X[i];
vector<pair<int,int>> v(N);
rep(i,0,N) v[i] = {V[i], W[i]};
sort(all(v), greater<>());
rep(i,0,Q) {
ll answer = 0;
int L, R;
cin >> L >> R;
multiset<int> s;
rep(j,0,M) {
if (L <= j+1 && j+1 <= R) continue;
s.insert(X[j]);
}
rep(j,0,N) {
auto itr = s.lower_bound(v[j].second);
if (itr != s.end()) {
answer += v[j].first;
s.erase(itr);
}
}
cout << answer << endl;
}
return 0;
}
|
#include <cstdio>
const int mod=998244353;
const int maxn=2e5+10;
struct E{
int to;
int next;
}ed[maxn*2];
int head[maxn];
int tot;
void J(int a,int b){
ed[++tot].to=b;
ed[tot].next=head[a];
head[a]=tot;
}
int vis[maxn];
void Dfs(int x){
vis[x]=1;
for(int i=head[x];i;i=ed[i].next){
if(vis[ed[i].to])
continue;
Dfs(ed[i].to);
}
}
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
int x;
scanf("%d",&x);
J(x,i);
J(i,x);
}
int now=0;
for(int i=1;i<=n;i++)
if(!vis[i]){
now++;
Dfs(i);
}
int ans=1;
for(int i=1;i<=now;i++)
ans=ans*2%mod;
printf("%d",(ans-1+mod)%mod);
return 0;
} | #include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#include <complex>
#define rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)
#define reps(x, s) for(llint (x) = 0; (x) < (llint)(s).size(); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define sz(x) ((ll)(x).size())
#define ceil(x, y) (((x)+(y)-1) / (y))
#define all(x) (x).begin(),(x).end()
#define outl(...) dump_func(__VA_ARGS__)
#define inf 1e18
using namespace std;
typedef long long llint;
typedef long long ll;
typedef pair<ll, ll> P;
bool exceed(ll x, ll y, ll m){return x >= m / y + 1;}
struct edge{
ll to, cost;
edge(){}
edge(ll a, ll b){
to = a, cost = b;
}
};
template<typename T>
ostream& operator << (ostream& os, vector<T>& vec) {
for(int i = 0; i<vec.size(); i++) {
os << vec[i] << (i + 1 == vec.size() ? "" : " ");
}
return os;
}
template<typename T, typename U>
ostream& operator << (ostream& os, pair<T, U>& pair_var) {
os << "(" << pair_var.first << ", " << pair_var.second << ")";
return os;
}
template<typename T, typename U>
ostream& operator << (ostream& os, map<T, U>& map_var) {
for(typename map<T, U>::iterator itr = map_var.begin(); itr != map_var.end(); itr++) {
os << "(" << itr->first << ", " << itr->second << ")";
itr++;
if(itr != map_var.end()) os << ",";
itr--;
}
return os;
}
template<typename T>
ostream& operator << (ostream& os, set<T>& set_var) {
for(typename set<T>::iterator itr = set_var.begin(); itr != set_var.end(); itr++) {
os << *itr;
++itr;
if(itr != set_var.end()) os << " ";
itr--;
}
return os;
}
void dump_func() {cout << endl;}
template <class Head, class... Tail>
void dump_func(Head &&head, Tail &&... tail) {
cout << head;
if(sizeof...(Tail) > 0) cout << " ";
dump_func(std::move(tail)...);
}
#define mod 998244353
struct UnionFind{
int size;
vector<int> parent;
vector<int> rank;
vector<llint> v, e;
UnionFind(){}
UnionFind(int size){
this->size = size;
parent.resize(size+1);
rank.resize(size+1);
v.resize(size+1);
e.resize(size+1);
init();
}
void init(){
for(int i = 0; i <= size; i++){
parent[i] = i, rank[i] = 0;
v[i] = 1, e[i] = 0;
}
}
int root(int i){
if(parent[i] == i) return i;
return parent[i] = root(parent[i]);
}
bool same(int i, int j){
return root(i) == root(j);
}
void merge(int i, int j){ // j will become new root
parent[i] = j;
v[j] += v[i];
e[j] += e[i] + 1;
}
void unite(int i, int j){
int root_i = root(i), root_j = root(j);
if(root_i == root_j){
e[root_i]++;
return;
}
if(rank[root_i] < rank[root_j]) merge(root_i, root_j);
else merge(root_j, root_i);
if(rank[root_i] == rank[root_j]) rank[root_i]++;
}
};
ll n;
ll a[200005];
UnionFind uf(200005);
int main(void)
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
rep(i, 1, n) cin >> a[i], uf.unite(i, a[i]);
set<ll> S;
rep(i, 1, n) S.insert(uf.root(i));
ll ans = 1;
rep(i, 1, sz(S)) ans *= 2, ans %= mod;
ans += mod - 1, ans %= mod;
outl(ans);
return 0;
} |
#include<bits/stdc++.h>
#include<string>
#include <algorithm>
using namespace std;
const int N=2e5+10;
typedef long long ll;
int n,k,q,m,h,w;
const ll mod=998244353;
int a[N];
int b[N];
map<int,map<int,int> > mpp;
map<int,int> mp;
set<int> se;
string s;
stack <int> st;
int X[]={0,0,1,-1};
int Y[]={-1,1,0,0};
void solve(){
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<=n;i++) cin>>b[i];
ll ans=0;
for(int i=1;i<=n;i++){
ans+=a[i]*b[i];
}
if(ans==0) cout<<"Yes";
else cout<<"No";
}
int main()
{
solve();
}
| #include<bits/stdc++.h>
#include<iostream>
#include<set>
#include<map>
#include<iterator>
#define ll long long
#define lli long long int
#define pb push_back
#define mp make_pair
#define RIP(i,n) for(int i=0; i<n; i++)
#define RIP1(i,n) for(int i=n-1; i>=0; i--)
#define FOR(i,a,b) for(int i=a;i<(b); i++)
#define FOR1(i,a,b) for(int i=a; i>=(b); i--)
#define sc(a) scanf("%lld",&a)
#define SC(a) scanf("%d",&a)
#define cin(a) cin >> a
#define cout(a) cout << a
#define pi acos(-1)
#define pr(a) printf("%lld\n",a)
#define PR(a) printf("%lld ",a)
#define s(a,b) sort(a,b)
#define sz(x) (int)(x).size()
#define nl '\n'
#define Max 110
#define mod 1e9 + 7
using namespace std;
void c_p_c()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
/*#ifdef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif*/
}
int main()
{
ll n;
sc(n);
ll a[n],b[n];
RIP(i,n)
{
sc(a[i]);
}
RIP(i,n)
{
sc(b[i]);
}
ll ans=0;
RIP(i,n)
{
ans+=(a[i]*b[i]);
}
if(ans)
{
cout << "No" << nl;
}
else
{
cout << "Yes" << nl;
}
}
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using pii = pair<int, int>;
int main() {
int n;
cin >> n;
vector<int> p(n);
rep(i, n) {
cin >> p[i];
p[i]--;
}
vector<int> s(n - 1), m(n - 1);
rep(i, n) {
if (i == p[i]) {
cout << -1 << endl;
return 0;
}
if (i < p[i]) {
for (int j = i; j < p[i]; j++) {
s[j]++;
if (s[j] > 2) {
cout << -1 << endl;
return 0;
}
m[j] = max(m[j], j - i + 1);
}
}
else {
for (int j = i; j > p[i]; j--) {
s[j - 1]++;
if (s[j - 1] > 2) {
cout << -1 << endl;
return 0;
}
m[j - 1] = max(m[j - 1], i - j + 1);
}
}
}
rep(i, n - 1) {
if (s[i] != 2) {
cout << -1 << endl;
return 0;
}
}
vector<pii> ans(n - 1);
rep(i, n - 1) ans[i] = pii{m[i], i};
sort(ans.begin(), ans.end());
rep(i, n - 1) cout << ans[i].second + 1 << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef unsigned int ui;
typedef long long ll;
typedef unsigned long long ull;
#define SZ(a) int((a).size())
#define _REP(_1,_2,_3,_4,name,...) name
#define _REP4(i,b,e,s) for(decltype(e) _b=(b),_e=(e),i=_b+(0<(s)?0:(s));(0<(s)?i<_e:_e<=i);i+=(s))
#define _REP3(i,b,e) for(decltype(e) _b=(b),_e=(e),i=(_b<_e?_b:_b-1);(_b<_e?i<_e:_e<=i);(_b<_e?i++:i--))
#define _REP2(i,n) for(decltype(n) i=0,_n=(n);i<_n;i++)
#define _REP1(n) for(decltype(n) _i=0,_n=(n);_i<_n;_i++)
#define REP(...) _REP(__VA_ARGS__,_REP4,_REP3,_REP2,_REP1)(__VA_ARGS__)
#define FOR(it,c) for(auto&& it=begin(c);it!=end(c);it++)
#define ROF(it,c) for(auto&& it=rbegin(c);it!=rend(c);it++)
#define EB emplace_back
#define PB push_back
#define MP make_pair
#define INT(n) int n;assert(0<scanf("%d",&n))
#define STR(s) string s;cin>>s
#define VS(v,n) vector<string> v(n);REP(i,n){cin>>v[i];}
int main(){
INT(h); INT(w);
VS(a, h);
vector<int> dr = {1, -1, 0, 0}, dc = {0, 0, 1, -1}, checked(26, 0);
int rg, cg;
vector<vector<int>> v(h);
vector<vector<pair<int, int>>> wp(26);
queue<pair<int, int>> q;
REP(i, h){
REP(j, w){
v[i].PB(-1);
if (a[i][j] == 'S'){
v[i][j] = 0;
q.push(MP(i, j));
} else if (a[i][j] == 'G'){
rg = i; cg = j;
} else if ('a' <= a[i][j] && a[i][j] <= 'z'){
wp[a[i][j] - 'a'].EB(i, j);
}
}
}
while (!q.empty()){
auto p = q.front(); q.pop();
int rp = p.first, cp = p.second, r, c;
REP(i, 4){
r = rp + dr[i];
c = cp + dc[i];
if (r < 0 || h <= r || c < 0 || w <= c){continue;}
if (a[r][c] == '#'){continue;}
if (0 <= v[r][c]){continue;}
v[r][c] = v[rp][cp] + 1;
q.push(MP(r, c));
}
if ('a' <= a[rp][cp] && a[rp][cp] <= 'z' and !checked[a[rp][cp] - 'a']) {
for(auto pw: wp[a[rp][cp] - 'a']){
r = pw.first; c = pw.second;
if (rp == r && cp == c){continue;}
if (0 <= v[r][c]){continue;}
v[r][c] = v[rp][cp] + 1;
q.push(MP(r, c));
checked[a[rp][cp] - 'a'] = 1;
}
}
}
printf("%d\n", v[rg][cg]);
return 0;
}
|
// clang-format off
#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;
#ifndef godfather
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#endif
#define int long long
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef gp_hash_table<int, int> umap;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update> oset;
typedef pair<pii, int> piii;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<pii> vii;
#define INF 10000000000000000
#define inf 1000000000
// #define MOD 1000000007
#define MOD 998244353
#define PI 3.1415926535897932385
#define pb push_back
#define bitc __builtin_popcountll
#define mp make_pair
#define all(ar) ar.begin(), ar.end()
#define fr(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define rep(i, n) for (int i = 0, _n = (n); i < _n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define frr(i, a, b) for (int i = (a), _b = (b); i >= _b; i--)
#define foreach(it, ar) for (auto it = ar.begin(); it != ar.end(); it++)
#define fil(ar, val) memset(ar, val, sizeof(ar)) // 0x3f for inf, 0x80 for -INF can also use with pairs
#ifdef godfather
template<typename T>
void __p(T a) { cout << a << " "; }
template<typename T>
void __p(std::vector<T> a) { cout << "{ "; for (auto p : a) __p(p); cout << "}"; }
template<typename T, typename F>
void __p(pair<T, F> a) { cout << "{ "; __p(a.first); __p(a.second); cout << "}"; }
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) { __p(a1); __p(a...); }
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
cout<<name<<" : ";__p(arg1);cout<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
int bracket=0,i=0;
for(; ;i++)
if(names[i]==','&&bracket==0)
break;
else if(names[i]=='(')
bracket++;
else if(names[i]==')')
bracket--;
const char *comma=names+i;
cout.write(names,comma-names)<<" : ";
__p(arg1);
cout<<"| ";
__f(comma+1,args...);
}
#define trace(...) cout<<"Line:"<<__LINE__<<" "; __f(#__VA_ARGS__, __VA_ARGS__)
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define trace(...)
#define end_routine()
#endif
mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count());
#define rand(l, r) uniform_int_distribution<int>(l, r)(rng32)
inline bool equals(double a, double b) { return fabs(a - b) < 1e-9; }
ll modpow(ll b, ll e, ll mod=MOD) {
ll ans=1; for(;e;b=b*b%mod,e/=2) if(e&1) ans=ans*b%mod; return ans; }
ld modp(ld b, ll e) {
ld ans=1; for(;e;b=b*b,e/=2) if(e&1) ans=ans*b; return ans; }
void solve()
{
// START
ll n;
cin>>n;
vi vec(n);
rep(i,n)cin>>vec[i];
sort(all(vec));
ll ans=0;
frr(i,n-1,0){
ll z=vec[i];
ll lg=n-i-1;
ans+=i*z-lg*z;
}
cout<<ans<<"\n";
// END
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0), cout.precision(15); cout<<fixed;
#ifdef godfather
// cin.exceptions(cin.failbit);
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t=1;
// cin>>t;
fr(i,1,t)
{
solve();
}
end_routine();
} | #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=2e5+6;
//const int mod=1e9+7;
string x;
LL m;
bool check(LL mod){
LL res=0;
LL base=1;
for(int i=0;i<(int)x.size();i++){
LL temp=x[i]-'0';
if(temp>m/base) return 0;
if(res>m-temp*base) return 0;
res+=temp*base;
if(i!=x.size()-1 && base>m/mod) return 0;
base*=mod;
}
return 1;
}
int main(){
cin>>x;
cin>>m;
if(x.size()==1){
if(x[0]-'0'<=m)
cout<<1<<endl;
else
cout<<0<<endl;
return 0;
}
reverse(x.begin(),x.end());
int d=0;
for(int i=0;i<(int)x.size();i++){
int temp=x[i]-'0';
d=max(d,temp);
}
LL l=d+1,r=m+1;
LL ans=d;
while(l<=r){
LL mid=(l+r)/2;
//cout<<l<<" "<<r<<" check "<<mid<<endl;
if(check(mid)){
ans=mid;
l=mid+1;
}
else r=mid-1;
}
//cout<<"max: "<<ans<<endl;
cout<<ans-d<<endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define faster ios_base::sync_with_stdio(false), cin.tie(0)
#define read freopen("in.txt", "r", stdin)
#define write freopen("out.txt", "w", stdout)
#define var(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define mem(x, n) memset(x, n, sizeof(x))
#define all(x) x.begin(), x.end()
#define endl "\n"
int dp[101][int(1e5 + 5)];
int n, mx, a[101];
int solve(int pos, int sum)
{
if (pos == n)
{
return max(sum, mx - sum);
}
if (dp[pos][sum] != -1)
return dp[pos][sum];
int ans = INT_MAX;
ans = min(solve(pos + 1, sum), solve(pos + 1, sum + a[pos]));
return dp[pos][sum] = ans;
}
int main()
{
faster;
mem(dp, -1);
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i], mx += a[i];
cout << solve(0, 0) << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
int dp[2][200000],ara[1000];
int main()
{
int n,a,sum=0,s,i;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a); sum=sum+a; ara[i]=a;
}
int fr,sc,tr,ans=90000000;
int idx,cap;
for(idx=n;idx>=0;idx--)
{
fr=idx&1; sc=(idx+1)&1;
for(cap=0;cap<=sum;cap++)
{
if(idx==n)
{
if(cap==0)dp[fr][cap]=1;
else{ dp[fr][cap]=0; }
}
else
{
int x=0,y=0,z;
x=dp[sc][cap];
if(cap-ara[idx]>=0)
{
y=dp[sc][cap-ara[idx]];
}
z=x|y;
dp[fr][cap]=z;
}
if(idx==0)
{
if(dp[fr][cap]==1)
{
s=max(cap,sum-cap);
//cout<<" "<<cap<<" "<<sum-cap<<endl;
ans=min(ans,s);
}
}
}
}
printf("%d\n",ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define P pair<ll,ll>
using Graph = vector<vector<int>>;
int main() {
ll N,M,Q; cin >> N;
ll x[N+1],y[N+1];
rep(i,N){
cin >> x[i+1] >> y[i+1];
}
vector<P> op; ll a,b,c; cin >> M;
rep(i,M){
cin >> a;
if(a==1||a==2) op.push_back(make_pair(a,0));
else{
cin >> b;
op.push_back(make_pair(a,b));
}
}
//Query(input / tuple化/順次処理)
cin >> Q;
vector<tuple<int,int,int>> q;
for(int i=0;i<Q;i++){
cin >> a >> b;
q.push_back(make_tuple(a,b,i));
}
//必要ならsort
sort(q.begin(),q.end());
vector<P> ans(Q);
ll cnt = 0;
ll xm[3],ym[3];
xm[0] = 0; ym[0] = 0; xm[1] = 1; ym[1] = 0;
xm[2] = 0; ym[2] = 1;
for(ll i=0;i<Q;i++){
tuple<int,int,int> qn = q.at(i);
a = get<0>(qn);
b = get<1>(qn);
c = get<2>(qn);
//ここに処理を追記
while(cnt<a){
auto p = op[cnt];
ll s = p.first; ll t = p.second;
cnt++; //cout << cnt << endl;
//rotate, flip
if(s==1){
rep(j,3){
xm[j] *= -1;
swap(xm[j],ym[j]);
}
}
if(s==2){
rep(j,3){
ym[j] *= -1;
swap(xm[j],ym[j]);
}
}
if(s==3){
rep(j,3){
xm[j] = 2*t-xm[j];
}
}
if(s==4){
rep(j,3){
ym[j] = 2*t-ym[j];
}
}
}
//座標復元
ll xa,ya;
ll dx1 = xm[1]-xm[0]; ll dy1 = ym[1]-ym[0];
ll dx2 = xm[2]-xm[0]; ll dy2 = ym[2]-ym[0];
xa = xm[0] + dx1*x[b] + dx2*y[b];
ya = ym[0] + dy1*x[b] + dy2*y[b];
ans[c] = make_pair(xa,ya);
}
for(ll i=0;i<Q;i++){
cout << ans[i].first <<" "<< ans[i].second << endl;
}
} | #include<bits/stdc++.h>
//#include <atcoder/all>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define rep(i,n)for(int i=0;(i)<(int)(n);i++)
#define REP(i,a,b)for(int i=(int)(a);(i)<=(int)(b);i++)
#define ALL(a) (a).begin(),(a).end()
#define pb push_back
#define fi first
#define se second
#define sz(x) ((int)x.size())
using namespace std;
//using namespace atcoder;
using ld = long double;
using ll = long long;
using P = pair<ll, ll>;
template<typename T> bool chmin(T& a, const T& b) { if(a >= b){ a = b; return 1;} return 0; }
template<typename T> bool chmax(T& a, const T& b) { if(a <= b){ a = b; return 1;} return 0; }
const ll MOD = 998244353;
const ll INF = 1e9;
//BIT
template <typename T>
struct BIT{
int n; //配列の要素数
vector<T> bit; //データの格納先
BIT(int n_) : n(n_ + 1), bit(n, 0){}
void add(int i, T x){
for(int idx = i; idx < n; idx += (idx & -idx)){
bit[idx] += x;
}
}
T sum(int i){
T s(0);
for(int idx = i; idx > 0; idx -= (idx & -idx)){
s += bit[idx];
}
return s;
}
//sum(a_i) >= w となるような最小のxを求める
int lower_bound(T w) { // a_1 + a_2 + ... + a_x >= w となるような最小の x を求める(ただし a_i >= 0)
if (w <= 0) return 0;
else {
int x = 0, r = 1;
while (r < n) r = r << 1;
for (int len = r; len > 0; len = len >> 1) { // 長さlenは1段下るごとに半分に
if (x + len < n && bit[x + len] < w) { // 採用するとき
w -= bit[x + len];
x += len;
}
}
return x + 1;
}
}
T num(int i){
T res = sum(i);
if(i != 1)res -= sum(i - 1);
return res;
}
};
vector<vector<int>> G;
vector<int> eul, dep;
void dfs(int v, int p){
eul.pb(v);
for(auto nv : G[v]){
if(nv == p)continue;
dep[nv] = dep[v] + 1;
dfs(nv, v);
}
eul.pb(v);
}
int main(){
int n;
cin >> n;
G.resize(n);
dep.resize(n);
vector<int> p(n - 1);
rep(i, n - 1)cin >> p[i];
rep(i, n - 1){
p[i]--;
G[i + 1].pb(p[i]);
G[p[i]].pb(i + 1);
}
int q;
cin >> q;
vector<int> U(q), D(q);
rep(i, q)cin >> U[i] >> D[i];
//Dごとに管理する
vector<vector<P>> qry(n + 1);
rep(i, q){
U[i]--;
qry[D[i]].pb(P(U[i], i));
}
dfs(0, -1);
//dep[v] -> v
vector<vector<int>> rdep(n + 1);
rep(i, n){
rdep[dep[i]].pb(i);
}
//vを根とする部分木に対応する区間を持つ
vector<int> L(n, -1), R(n);
rep(i, sz(eul)){
if(L[eul[i]] == -1)L[eul[i]] = i;
R[eul[i]] = i;
}
BIT<int> bit(sz(eul));
vector<int> res(q);
REP(i, 0, n){
//bitに深さiの点を加える
for(auto v : rdep[i]){
bit.add(L[v] + 1, 1);
}
//クエリ処理
for(auto qi : qry[i]){
auto [ui, id] = qi;
res[id] = bit.sum(R[ui] + 1);
if(L[ui] > 0)res[id] -= bit.sum(L[ui]);
}
//足した分引く
for(auto v : rdep[i]){
bit.add(L[v] + 1, -1);
}
}
for(auto ri : res)cout << ri << endl;
} |