code_file1
stringlengths 87
4k
| code_file2
stringlengths 85
4k
|
---|---|
#include<bits/stdc++.h>
#define rep(i,a,b) for(ll i=(a);i<=(b);i++)
#define per(i,a,b) for(ll i=(a);i>=(b);i--)
#define lc(x) (x<<1)
#define rc(x) (lc(x)|1)
#define lowbit(x) (x&-x)
#define Max(a,b) (a>b?a:b)
#define Min(a,b) (a<b?a:b)
#define next Cry_For_theMoon
#define il inline
#define pb(x) push_back(x)
#define mapit map<int,int>::iterator
#define vit vector<int>::iterator
#define mp(x,y) make_pair(x,y)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef double db;
using namespace std;
const int MAXN=1e5+10;
int n,size[MAXN],ans;
int color[MAXN];
vector<int>v[MAXN];
int main(){
cin>>n;
rep(i,1,n){
sort(v[i].begin(),v[i].end());
color[i]=1;int pre=-1;
for(vit it=v[i].begin();it!=v[i].end();it++){
if(*it==pre)continue;
pre=*it;
if(color[i]==*it)color[i]++;
else break;
}
rep(j,2,n){
if(j*i>n)break;
v[j*i].pb(color[i]);
}
}
rep(i,1,n)printf("%d ",color[i]);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pt;
#define MOD 1000000007
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
vector<int> ans(N, 2);
ans[0] = 1;
for(int i = 2; i <= N; i++) {
for(int j = 2; i * j <= N; j++) {
ans[i * j - 1] = ans[i - 1] + 1;
}
}
for(int i = 0; i < N; i++) cout << ans[i] << " ";
cout << endl;
} |
/**
* author: shu8Cream
* created: 26.04.2021 09:06:13
**/
#include <bits/stdc++.h>
#include <chrono>
using namespace std;
#define rep(i,n) for (int i=0; i<(n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using P = pair<int,int>;
using vi = vector<int>;
using vvi = vector<vi>;
using namespace std::chrono;
inline double get_time_sec(void){
return static_cast<double>(duration_cast<nanoseconds>(steady_clock::now().time_since_epoch()).count())/1000000000;
}
int si,sj;
vvi t(50,vi(50));
vvi p(50,vi(50));
string finans = "";
int finscore = 0;
const vi di = {1,0,-1,0};
const vi dj = {0,1,0,-1};
void input(){
cin >> si >> sj;
rep(i,50)rep(j,50) cin >> t[i][j];
rep(i,50)rep(j,50) cin >> p[i][j];
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
input();
set<int> allrem;
int loop = 16;
while(loop--){
double start = get_time_sec();
int score = 0;
string ans = "";
vector<P> remcell;
while(get_time_sec() - start<0.124){
int tscore = 0;
string tans = "";
set<int> rem;
queue<P> pq;
pq.push({si,sj});
int i=0,j=0;
while(!pq.empty()){
int pi=i,pj=j;
tie(i,j) = pq.front(); pq.pop();
remcell.push_back({i,j});
rem.insert(t[i][j]);
tscore += p[i][j];
queue<P> emp;
swap(pq,emp);
if(!(i == si && j == sj)){
if(i-pi==1) tans+="D";
else if(i-pi==-1) tans+="U";
else if(j-pj==1) tans+="R";
else if(j-pj==-1) tans+="L";
}
// cout << ans << endl;
vi rand = {0,1,2,3};
std::random_device seed_gen;
std::mt19937 engine(seed_gen());
shuffle(all(rand),engine);
for(auto k : rand){
int ni = i + di[k];
int nj = j + dj[k];
if(ni<0 || ni>=50 || nj<0 || nj>=50) continue;
if(t[i][j]==t[ni][nj]) continue;
if(rem.count(t[ni][nj])) continue;
if(allrem.count(t[ni][nj])) continue;
int ngcnt = 0;
rep(kk,4){
int nni = ni + di[kk];
int nnj = nj + dj[kk];
if(nni<0 || nni>=50 || nnj<0 || nnj>=50) {ngcnt++; continue;}
if(t[nni][nnj]==t[ni][nj]) {ngcnt++; continue;}
if(rem.count(t[nni][nnj])) {ngcnt++; continue;}
if(allrem.count(t[nni][nnj])) {ngcnt++; continue;}
}
if(ngcnt==4) continue;
pq.push({ni,nj});
}
}
if(tscore>score){
score = tscore;
ans = tans;
}
}
int anslen = (ans.size()+1)/2;
ans = ans.substr(0,anslen);
finans += ans;
allrem.insert(t[si][sj]);
rep(k,anslen){
if(ans[k]=='U') si--;
if(ans[k]=='D') si++;
if(ans[k]=='L') sj--;
if(ans[k]=='R') sj++;
allrem.insert(t[si][sj]);
}
}
cout << finans << endl;
} | #include <bits/stdc++.h>
using namespace std;
template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
// types
int debug;
struct Input {
int si, sj, num;
vector<vector<int>> t, p;
vector<int> sum_score;
Input(istream &is) : t(50, vector<int>(50)), p(50, vector<int>(50)) {
is >> si >> sj;
for (int i = 0; i < 50; ++i)
for (int j = 0; j < 50; ++j)
is >> t[i][j];
for (int i = 0; i < 50; ++i)
for (int j = 0; j < 50; ++j)
is >> p[i][j];
num = 0;
for (int i = 0; i < 50; ++i)
for (int j = 0; j < 50; ++j)
chmax(num, t[i][j] + 1);
sum_score.resize(num);
for (int i = 0; i < 50; ++i)
for (int j = 0; j < 50; ++j)
sum_score[t[i][j]] += p[i][j];
}
};
struct Eval {
int sx, sy;
int self, other;
};
const int dy[4] = {0, 1, 0, -1};
const int dx[4] = {1, 0, -1, 0};
const char dir_char[8] = "RDLU";
using Output = pair<string,int>;
Output sub_solve(const Input &input, Eval eval) {
vector<int> visited(2500);
int y = input.si, x = input.sj;
const auto &t = input.t;
const auto &p = input.p;
const vector<int> &sum_score = input.sum_score;
int max_score = 0;
string max_res = "";
vector<vector<pair<int,int>>> history;
vector<int> dir_history;
int turn = 0;
int score = p[y][x];
int max_turn = 0;
string res = "";
for (;;) {
// cout << turn << " " << y << " " << x << endl;
// if (turn > 10) break;
int number = t[y][x];
visited[number] = true;
if (int(history.size()) <= turn) {
vector<pair<int,int>> vec;
for (int dir = 0; dir < 4; ++dir) {
const int ny = y + dy[dir];
const int nx = x + dx[dir];
if (ny < 0 || nx < 0 || ny >= 50 || nx >= 50) continue;
if (visited[t[ny][nx]]) continue;
// int score = (nx + ny) * 10 - (sum_score[t[ny][nx]] - p[ny][nx]);
int score = 0;
score += nx * eval.sx;
score += ny + eval.sy;
score += p[ny][nx] * eval.self;
score -= (sum_score[t[ny][nx]] - p[ny][nx]) * eval.self;
vec.emplace_back(score, dir);
}
sort(vec.begin(), vec.end());
history.push_back(vec);
}
else if (int(history.size()) > turn + 1) {
history.pop_back();
}
auto &vec = history[turn];
if (vec.empty()) {
if (turn < max_turn - 30) break;
--turn;
history.pop_back();
history.back().pop_back();
const int dir = dir_history.back();
if (score > max_score) {
max_score = score;
max_res = res;
}
score -= p[y][x];
x -= dx[dir];
y -= dy[dir];
dir_history.pop_back();
visited[number] = false;
res.pop_back();
continue;
}
++turn;
chmax(max_turn, turn);
const int dir = vec.back().second;
res += dir_char[dir];
x += dx[dir];
y += dy[dir];
score += p[y][x];
dir_history.push_back(dir);
}
return {max_res, max_score};
}
Output solve(const Input &input) {
int max_score = 0;
string max_res = "";
mt19937 mt;
for (int loop = 0; loop < 100; ++loop) {
Eval eval;
eval.sx = mt() % 1000;
eval.sy = mt() % 1000;
eval.self = mt() % 1000;
eval.other = mt() % 1000;
Output out = sub_solve(input, eval);
if (out.second > max_score) {
max_score = out.second;
max_res = out.first;
}
}
return {max_res, max_score};
}
int main(int argc, char**) {
debug = (argc > 1);
if (debug) {
char filename[256];
int sum = 0;
for (int i = 0; i < 100; ++i) {
sprintf(filename, "in/%.4d.txt", i);
ifstream ifs(filename);
Input input(ifs);
int r = solve(input).second;
cout << r << endl;
sum += r;
}
cout << sum << endl;
}
else {
const Input input(cin);
auto res = solve(input);
cout << res.first << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
char str[4][30],q[20];
int cnt;
int mymap[30];
bool st[30];
bool jdg[20];
long long temp[10];
bool dfs(int s){
if(s>cnt){
for(int i=1;i<=3;i++){
if(mymap[str[i][1]-'a']==0) return false;
}
temp[1]=temp[2]=temp[3]=0;
for(int i=1;i<=3;i++){
for(int j=1;str[i][j];j++){
temp[i]=temp[i]*10+mymap[str[i][j]-'a'];
}
}
return temp[1]+temp[2]==temp[3];
}
for(int i=0;i<=9;i++){
if(!jdg[i]){
mymap[q[s]-'a']=i;
jdg[i]=true;
if(dfs(s+1)) return true;
jdg[i]=false;
}
}
return false;
}
int main(){
for(int i=1;i<=3;i++) scanf("%s",str[i]+1);
for(int i=1;i<=3;i++){
for(int j=1;str[i][j];j++){
if(!st[str[i][j]-'a']){
st[str[i][j]-'a']=true;
q[++cnt]=str[i][j];
}
}
}
if(cnt>10) puts("UNSOLVABLE");
else{
if(dfs(1)){
for(int i=1;i<=3;i++) cout<<temp[i]<<endl;
}
else puts("UNSOLVABLE");
}
return 0;
} | #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <tuple>
#include <cstdio>
#include <bitset>
#include <sstream>
#include <iterator>
#include <numeric>
#include <map>
#include <cstring>
#include <set>
#include <functional>
#include <iomanip>
using namespace std;
#define DEBUG_ //!!$BDs=P;~$K%3%a%s%H%"%&%H(B!!
#ifdef DEBUG_
#define dump(x) cerr << #x << " = " << (x) << endl;
#else
#define dump(x) ;
#endif
#define equals(a,b) (fabs((a)-(b)) < EPS)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
//#define int long long
typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
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 <typename T>
std::string printVector(const std::vector<T> &data)
{
std::stringstream ss;
std::ostream_iterator<T> out_it(ss, ", ");
ss << "[";
std::copy(data.begin(), data.end() - 1, out_it);
ss << data.back() << "]";
return ss.str();
}
template <typename T>
void print_array(const T &ary, int size){
REP(i,size){
cout << ary[i] << " ";
}
cout << endl;
}
const int mod = 1e9+7;
const LL LINF = 1001002003004005006ll;
const int INF = 1001001001;
const double EPS = (1e-10);
const long double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899;
int dx[] = {0,0,-1,1};
int dy[] = {-1,1,0,0};
int arr[] = {0,1,2,3,4,5,6,7,8,9};
map<char,int> mp;
LL getInt(string s){
reverse(s.begin(), s.end());
LL res = 0;
LL base = 1;
for(char c : s){
res += base * arr[mp[c]];
base *= 10;
}
return res;
}
bool check(string s){
int idx = mp[s[0]];
int x = arr[idx];
if(x == 0) return false;
else return true;
}
signed main(int argc, char const *argv[])
{
cin.tie(0);
ios::sync_with_stdio(false);
cout << setprecision(12);
string A,B,C; cin >> A >> B >> C;
set<char> se;
for(char c : A) se.insert(c);
for(char c : B) se.insert(c);
for(char c : C) se.insert(c);
int len = 0;
for(char c : se) len++;
if(len > 10){
cout << "UNSOLVABLE" << endl;
return 0;
}
int idx = 0;
for(char c : se){
mp[c] = idx;
idx++;
}
do{
if(!check(A) || !check(B) || !check(C)) continue;
LL Ax = getInt(A);
LL Bx = getInt(B);
LL Cx = getInt(C);
if(Ax + Bx == Cx){
cout << Ax << endl;
cout << Bx << endl;
cout << Cx << endl;
return 0;
}
}while(next_permutation(arr, arr+10));
cout << "UNSOLVABLE" << endl;
} |
#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<queue>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#include<complex>
#include<math.h>
using namespace std;
#define pb push_back
#define ll long long int
# define pii pair<int ,int>
# define pll pair<ll,ll>
# define fio ios_base::sync_with_stdio(false); cin.tie(NULL)
# define ft first
# define sec second
//expected values is a*e0+b.
double a[100009]={0},b[100009]={0};
int h[100009];
int main()
{ cout<<fixed<<setprecision(9);
int n,m,k;
cin >>n >>m >>k;
for(int i = 1 ; i <= k ; i++) {cin >>h[i]; a[h[i]]=1;b[h[i]]=0;}
sort(h+1,h+k+1);
a[n]=0;b[n]=0;
for(int i = n-1; i>=0 ; i--)
{
if(a[i] != 1)
{
a[i] = (a[i+1]-a[min(i+m+1,n+1)])/m;
b[i] = 1 + (b[i+1]-b[min(i+m+1,n+1)])/m;
}
a[i]+=a[i+1];
b[i]+=b[i+1];
}
for(int i = 0 ; i <= n ; i++) {a[i]=a[i]-a[i+1]; b[i]=b[i]-b[i+1];}
if( abs(a[0]-1) <= 0.000000001) cout<<"-1";
else cout << b[0]/(1-a[0]);
}
| /*
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
*/
#include<bits/stdc++.h>
#define ALL(x) (x).begin(),(x).end()
#define ll long long
#define db long double
#define ull unsigned long long
#define pii_ pair<int,int>
#define mp_ make_pair
#define pb push_back
#define fi first
#define se second
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define show1(a) cout<<#a<<" = "<<a<<endl
#define show2(a,b) cout<<#a<<" = "<<a<<"; "<<#b<<" = "<<b<<endl
using namespace std;
const ll INF = 1LL<<60;
const int inf = 1<<30;
const int maxn = 2e5+5;
inline void fastio() {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
int flag[maxn];
db f1[maxn],f2[maxn],suf1[maxn],suf2[maxn];
int main()
{
//fastio();
int n,m,k; scanf("%d%d%d",&n,&m,&k);
rep(i,1,k){
int x; scanf("%d",&x);
flag[x] = 1;
}
per(i,n-1,0){
if(flag[i]){
f1[i] = 0;f2[i] = 1;
}else{
f1[i] = 1.0 + (suf1[i+1] - suf1[i+m+1]) / m;
f2[i] = (suf2[i+1] - suf2[i+m+1]) / m;
}//show2(f1[i],f2[i]);
suf1[i] = f1[i] + suf1[i+1];
suf2[i] = f2[i] + suf2[i+1];
}
db ans = f1[0] / (1 - f2[0]);
//printf("%.4Lf\n",ans);
if(ans > 1e13) cout << -1 << endl;
else cout << fixed << setprecision(4) << ans << endl;
return 0;
}
|
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <cstdio>
#include <cmath>
#define rep(i, n) for(i = 0; i < n; i++)
#define int long long
using namespace std;
int n, m;
vector<int> y[18], z[18];
int dp[19][1 << 18];
int bitCnt[1 << 18];
signed main() {
int i, j, k;
cin >> n >> m;
rep(i, (1 << n)) {
rep(j, n) {
if ((i >> j) % 2) bitCnt[i]++;
}
}
rep(i, m) {
int x, cy, cz;
cin >> x >> cy >> cz;
y[x].push_back(cy);
z[x].push_back(cz);
}
dp[0][0] = 1;
rep(i, n) {
rep(j, (1 << n)) {
if (dp[i][j] == 0) continue;
//ๅคๅฎ
rep(k, y[i].size()) {
int bcnt = bitCnt[j & ((1 << y[i][k]) - 1)];
if (bcnt > z[i][k]) break;
}
if (k < y[i].size()) continue;
//้ท็งป
rep(k, n) {
if ((j >> k) % 2) continue;
dp[i + 1][j + (1 << k)] += dp[i][j];
}
}
}
cout << dp[n][(1 << n) - 1] << endl;
return 0;
} | //12252024832524
#include <cstdio>
#include <cstring>
#include <algorithm>
#define TT template<typename T>
using namespace std;
typedef long long LL;
const int MAXN = 18;
const int MAXM = 105;
int n,m;
LL dp[1 << MAXN],pre[MAXN];
LL Read()
{
LL x = 0,f = 1;char c = getchar();
while(c > '9' || c < '0'){if(c == '-')f = -1;c = getchar();}
while(c >= '0' && c <= '9'){x = (x*10) + (c^48);c = getchar();}
return x * f;
}
TT void Put1(T x)
{
if(x > 9) Put1(x/10);
putchar(x%10^48);
}
TT void Put(T x,char c = -1)
{
if(x < 0) putchar('-'),x = -x;
Put1(x); if(c >= 0) putchar(c);
}
TT T Max(T x,T y){return x > y ? x : y;}
TT T Min(T x,T y){return x < y ? x : y;}
TT T Abs(T x){return x < 0 ? -x : x;}
struct node
{
int x,y,z;
bool operator < (const node &px)const{
return x < px.x;
}
}s[MAXM];
int main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
n = Read(); m = Read();
int S = (1 << n) - 1;
for(int i = 0;i < m;++ i) s[i].x = Read(),s[i].y = Read(),s[i].z = Read();
for(int i = 1;i <= S;++ i)
{
int cnt = 0,dz = i;
while(dz)
{
if(dz & 1) cnt++;
dz >>= 1;
}
for(int j = 0;j < n;++ j) pre[j] = 0;
for(int j = 0;j < n;++ j)
if((i >> j) & 1) pre[j]++;
for(int j = 1;j < n;++ j) pre[j] += pre[j-1];
for(int j = 0;j < m;++ j)
{
if(s[j].x == cnt && pre[s[j].y-1] > s[j].z)
{
dp[i] = -1;
break;
}
}
}
dp[0] = 1;
for(int i = 1;i <= S;++ i)
{
if(dp[i] == -1) continue;
for(int j = 0;j < n;++ j)
if((i >> j) & 1)
if(dp[i^(1<<j)] != -1)
dp[i] += dp[i^(1<<j)];
}
Put(Max(dp[S],0ll));
return 0;
}
|
#include"bits/stdc++.h"
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
typedef long long LL;
typedef vector<LL> VL;
typedef vector<VL> VVL;
typedef pair<LL, LL> PLL;
typedef vector<PLL> VPLL;
typedef priority_queue<LL> PQ_DESC;
typedef priority_queue<LL, VL, greater<LL>> PQ_ASC;
typedef priority_queue<PII> PQ_DESC_PII;
typedef priority_queue<PII, vector<PII>, greater<PII>> PQ_ASC_PII;
typedef priority_queue<VL> PQ_DESC_VL;
typedef priority_queue<VL, vector<VL>, greater<VL>> PQ_ASC_VL;
typedef priority_queue<PLL> PQ_DESC_PLL;
typedef priority_queue<PLL, vector<PLL>, greater<PLL>> PQ_ASC_PLL;
#define ALL(c) (c).begin(),(c).end()
#define PB push_back
#define MP make_pair
#define SORT_ASC(c) sort(ALL(c))
//#define SORT_DESC(c) sort(ALL(c), greater<typeof(*((c).begin()))>())
#define SORT_DESC(c) sort((c).rbegin(),(c).rend())
#define REV(c) reverse((c).begin(), (c).end())
#define SIZE(a) int((a).size())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define ROF(i,a,b) for(int i=(b-1);i>=(a);--i)
#define REP(i,n) FOR(i,0,n)
#define PER(i,n) ROF(i,0,n)
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int LARGE_INT = 1e9+100;
const int INF = 2e9+100;
const LL INF_LL = (LL)INF*(LL)INF;
const int MOD = 1e9+7;
//debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
LL modpow(LL a, LL n) {
LL res = 1;
while (n > 0) {
if (n & 1) res = res * a % MOD;
a = a * a % MOD;
n >>= 1;
}
return res;
}
void Main()
{
LL n;cin>>n;
string s;cin>>s;
s = "X"+s;
VL a(n+1);
VL t(n+1);
VL c(n+1);
VL g(n+1);
FOR(i,1,n+1){
if(s[i] == 'A'){
a[i]=1;
}
if(s[i] == 'T'){
t[i]=1;
}
if(s[i] == 'C'){
c[i]=1;
}
if(s[i] == 'G'){
g[i]=1;
}
a[i] += a[i-1];
t[i] += t[i-1];
c[i] += c[i-1];
g[i] += g[i-1];
}
LL result = 0;
FOR(i,0,n){
FOR(j,i+1,n+1){
LL na = a[j]-a[i];
LL nt = t[j]-t[i];
LL nc = c[j]-c[i];
LL ng = g[j]-g[i];
if(na==nt && nc==ng){
result ++;
//cerr<<i<<" "<<j<<endl;
}
}
}
cout<<result<<endl;
return;
}
int main()
{
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(15);
Main();
return 0;
} | /*
author : TAPAN SAVANI
codeforces : savanitapan2001
codechef : savanitapan17
*/
/*
------------------------------------------------------------------------
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
------------------------------------------------------------------------
*/
#include <bits/stdc++.h>
using namespace std;
#define HAPPY_CODING \
ios_base::sync_with_stdio(0); \
cin.tie(NULL); \
cout.tie(NULL);
#define pb push_back
#define mp make_pair
#define Debug(x) cout << #x " = " << (x) << endl
#define SORT(a) sort(a.begin(), a.end())
#define SORTR(a) sort(a.rbegin(), a.rend())
#define mod 1000000007
#define pi 3.141592653589793238
#define ll long long int
#define ull unsigned long long
#define be begin()
#define en end()
#define FOR(i, a, b) for (long long int i = a; i < b; i++)
#define FORI(i, a, b) for (int i = a; i >= b; i--)
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PI;
typedef pair<ll, ll> PL;
typedef vector<PI> VPI;
int main()
{
HAPPY_CODING;
int a, b;
cin >> a >> b;
cout << (a+b)/2 << " " << (a-b)/2 << "\n";
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int digit_sum(int n) {
if (n < 10) return n;
return digit_sum(n/10) + n%10;
}
int main() {
int a, b;
cin >> a >> b;
int sum_a = digit_sum(a), sum_b = digit_sum(b);
if (sum_b <= sum_a) {
cout << sum_a << endl;
} else {
cout << sum_b << endl;
}
return 0;
}
| #define LOCAL
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2")
#define IOS ios::sync_with_stdio(false);cin.tie(0)
#define all(x) x.begin(), x.end()
#define ff first
#define ss second
#define LLINF 0x3f3f3f3f3f3f3f3f
#define INF 0x3f3f3f3f
#define uniq(x) sort(all(x)); x.resize(unique(all(x))-x.begin());
#define sz(x) (int)x.size()
// Copied from Gennady-Korotkevich's template
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {
return '"' + s + '"';
}
string to_string(const char* s) {
return to_string((string)s);
}
string to_string(bool b) {
return (b ? "true" : "false");
}
string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}\n";
return res;
}
template <size_t N>
string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++) {
res += static_cast<char>('0' + v[i]);
}
return res;
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto& x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}\n";
return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
template <typename A, typename B, typename C, typename D, typename E>
string to_string(tuple<A, B, C, D, E> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + "," + to_string(get<4>(p)) + ")";
}
void debug_out() {
cerr << endl;
}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "\n[" << #__VA_ARGS__ << "]:\n", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
// End of Gennady-Korotkevich's template
using pii = pair<int, int>;
using ll = long long;
const ll MOD = 1e9 + 7;
const long double PI = acos(-1.0);
void solve() {
string a, b; cin >> a >> b;
int sum1 = 0, sum2 = 0;
for(char c : a)
sum1 += c-'0';
for(char c : b)
sum2 += c-'0';
cout << max(sum1,sum2);
}
int main() {
IOS;
int t = 1;
while(t--)
solve();
} |
//#define _GLIBCXX_DEBUG
//#include "atcoder/all"
//using namespace atcoder;
#include <bits/stdc++.h>
#define int long long
#define ll long long
using ull = unsigned long long;
using namespace std;
#define Dump(x) \
if (dbg) { \
cerr << #x << " = " << (x) << endl; \
}
#define overload4(_1, _2, _3, _4, name, ...) name
#define FOR1(n) for (ll i = 0; i < (n); ++i)
#define FOR2(i, n) for (ll i = 0; i < (n); ++i)
#define FOR3(i, a, b) for (ll i = (a); i < (b); ++i)
#define FOR4(i, a, b, c) for (ll i = (a); i < (b); i += (c))
#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FORR(i, a, b) for (int i = (a); i <= (b); ++i)
#define bit(n, k) (((n) >> (k)) & 1) /*nใฎk bit็ฎ*/
namespace mydef {
const int INF = 1ll << 60;
const int MOD = 1e9 + 7;
template <class T>
bool chmin(T& a, const T& b) {
if (a > b) {
a = b;
return 1;
} else
return 0;
}
template <class T>
bool chmax(T& a, const T& b) {
if (a < b) {
a = b;
return 1;
} else
return 0;
}
void Yes(bool flag = true) {
if (flag)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
void No(bool flag = true) {
Yes(!flag);
}
void YES(bool flag = true) {
if (flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
void NO(bool flag = true) {
YES(!flag);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T& val) {
std::fill((T*)array, (T*)(array + N), val);
}
bool dbg = true;
} // namespace mydef
using namespace mydef;
#define pb push_back
//#define mp make_pair
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(), (v).end()
#define SZ(x) ((int)(x).size())
#define vi vector<int>
#define vvi vector<vector<int>>
#define vp vector<pair<int, int>>
#define vvp vector<vector<pair<int, int>>>
#define pi pair<int, int>
//#define P pair<int, int>
//#define V vector<int>
//#define S set<int>
#define asn ans
void solve() {
}
signed main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
string A, B;
cin >> A >> B;
auto f = [](string s) {
int ret = 0;
for (int i = 0; i < 3; i++) {
ret += s[i] - '0';
}
return ret;
};
cout << max(f(A), f(B)) << endl;
solve();
return 0;
}
| #include <iostream>
#include <cmath>
using namespace std;
typedef unsigned long long ll;
ll check(string ip, ll base, ll m){
ll ans =0;
ll curr = 0;
for(int i =ip.length()-1;i>=0;i--){
if(ans<0 || pow(base,curr)>m) return m+1;
ans+= (ll)((int)ip[i]-'0')*pow(base,curr);
curr++;
if(ans>m){
//cout<<"ans"<<ans<<endl;
return m+1;
}
}
return ans;
}
int main ()
{
string X;
cin>>X;
ll M;
cin>>M;
if(X.length()==1){
if(stoi(X)<=M) cout<<"1";
else cout<<"0";
return 0;
}
int mx =-1;
for(char a: X){
mx = max(mx,(int)a-'0');
}
ll low = mx+1, high = M;
ll ans=mx;
while(low<=high){
ll mid = low + (high-low)/2;
//cout<<"low:"<<low<<" mid:"<<mid<<" high:"<<high<<endl;
//cout<<"check:"<<check(X,mid,M)<<endl;
if(check(X,mid,M)<=M){
low = mid+1;
ans = max(ans,mid);
}
else{
high = mid-1;
}
}
//cout<<"ans:"<<ans<<" mx:"<<mx<<endl;
cout<<max((ll)0,ans-mx);
return 0;
}
|
/*/ Author: _Math.man /*/
#include<bits/stdc++.h>
using namespace std;
/*/---------------------------Defines-----------------------------------------/*/
#pragma GCC optimize("Ofast")
#define int long long
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define eb emplace_back
#define fn for(int i =0 ;i <n;i++)
#define fn1 for( int i =1;i<=n; i++)
#define fm for(int j =0 ;j <m;j++)
#define fm1 for(int j =1;j<=m;j++)
#define fi first
#define se second
#define endl '\n'
#define PI 3.14159265358979323846
#define MOD 1000000007
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
const int N = 2e6+5;
const int INF = 1e18L;
//mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
/*/-----------------------------Debug begins----------------------------------/*/
vector<string> split(const string& s, char c) {
vector<string> v; stringstream ss(s); string x;
while (getline(ss, x, c)) v.emplace_back(x); return move(v);
}
template<typename T, typename... Args>
inline string arrStr(T arr, int n) {
stringstream s; s << "[";
for(int i = 0; i < n - 1; i++) s << arr[i] << ",";
if(n>0)
s << arr[n - 1] ;
s<< "]";
return s.str();
}
#define trace(args...) {__trace_begin(__LINE__); __trace(split(#args, ',').begin(), args);}
inline void __trace_begin(int line) { cerr << "#" << line << ": "; }
template<typename T> inline void __trace_out_var(vector<T> val) { cerr << arrStr(val, val.size()); }
template<typename T> inline void __trace_out_var(T* val) { cerr << arrStr(val, 10); }
template<typename T> inline void __trace_out_var(T val) { cerr << val; }
inline void __trace(vector<string>::iterator it) { cerr << endl; }
template<typename T, typename... Args>
inline void __trace(vector<string>::iterator it, T a, Args... args) {
cerr << it->substr((*it)[0] == ' ', it->length()) << "=";
__trace_out_var(a);
cerr << "; ";
__trace(++it, args...);
}
/*/-----------------------------Code begins----------------------------------/*/
int ar[N];
int dp[N];
signed main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
IOS;
int T=1;
// cin >> T;
while(T--){
int a,b,c,d;
cin >> a>>b>>c>>d;
cout << a*d -b*c;
}
return 0;
} | //Bismillahir Rahmanir Rahim
// Rabbana Afrig Alayna Sabrao Wa Taffaiyana Muslimin
#include<bits/stdc++.h>
using namespace std;
#define int long long int
int i,j,k;
main()
{
int a,b,c,d;
cin>>a>>b>>c>>d;
cout<<a*d-b*c<<endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#define all(a)a.begin(),a.end()
#define rall(a)a.rbegin(),a.rend()
using ll=long long;
const int INF = 1<<30;
const ll INFll =1LL<<62;
const int mod= int(1e9)+7;
const int mod2=998244353;
using P = pair<int,int>;
using Pl= pair<ll,ll>;
using ld=long double;
using V=vector<int>;
using Vl=vector<ll>;
using Vd=vector<ld>;
using VV=vector<vector<int>>;
using VVl=vector<vector<ll>>;
using VVd=vector<vector<ld>>;
int gcd(int a,int b){
if(b==0)return a;
return gcd(b,a%b);
}
//return (x,y,gcd(a,b)) s.t. ax+by=gcd(a,b)
pair<Pl,ll> extgcd(ll a,ll b){
if(b==0)return {{1,0},a};
else{
auto res=extgcd(b,a%b);
ll X=res.first.first;
ll Y=res.first.second;
ll g=res.second;
//bX+(a%b)Y=g
//a=a/b*b+a%b
//bX+(-a/b*b+a)Y=g
//aY+b(X-a/b*Y)=g
return {{Y,X-a/b*Y},g};
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;cin >>t;
// int b=13;
// for (int a = 1; a <= b; ++a) {
// int ex=extgcd(a,b).first.first;
// ex%=b;
// if(ex<0)ex+=b;
// if(a*ex%b!=1){
// puts("NG");
// cout <<a<<" "<<ex<<"\n";
// return 0;
// }
// }
while(t--){
ll n,s,k;cin >>n>>s>>k;
ll g=gcd(k,n);
if(s%g!=0){
cout <<-1<<"\n";
}
else{
s/=g;
k/=g;
n/=g;
ll x=extgcd(k,n).first.first;//k^-1(mod n)
// cout <<x<<"\n";
ll ans=-s*x;
ans%=n;
if(ans<0)ans+=n;
cout <<ans<<"\n";
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin>>
#define ll long long
#define ln cout<<'\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}void pr(){ln;}
template<class A,class...B>void pr(const A &a,const B&...b){cout<<a<<(sizeof...(b)?" ":"");pr(b...);}
template<class A>void PR(A a,ll n){rep(i,n)cout<<(i?" ":"")<<a[i];ln;}
const ll MAX=1e9+7,MAXL=1LL<<61,dx[8]={-1,0,1,0,-1,-1,1,1},dy[8]={0,1,0,-1,-1,1,1,-1};
typedef pair<ll,ll> P;
void Main() {
ll n;
R n;
ll x=1.08*n;
if(x<206) pr("Yay!");
else if(x>206) pr(":(");
else pr("so-so");
}
int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}
|
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <ctime>
#include <iterator>
#include <bitset>
#include <numeric>
#include <list>
#include <iomanip>
#include <cassert>
#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>
using namespace std;
#define cauto const auto&
#define ALL(v) begin(v),end(v)
#else
#define ALL(v) (v).begin(),(v).end()
#endif
namespace{
typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;
#define VV(T) vector<vector< T > >
template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
v.assign(a, vector<T>(b, t));
}
template <class T> inline bool chmin(T &x, const T &y){ if(y < x){ x = y; return true; } return false; }
template <class T> inline bool chmax(T &x, const T &y){ if(x < y){ x = y; return true; } return false; }
template <class F, class T>
void convert(const F &f, T &t){
stringstream ss;
ss << f;
ss >> t;
}
template <class Con>
string concat(const Con &c, const string &spr){
stringstream ss;
typename Con::const_iterator it = c.begin(), en = c.end();
bool fst = true;
for(; it != en; ++it){
if(!fst){ ss << spr; }
fst = false;
ss << *it;
}
return ss.str();
}
template <class Con, class Fun>
vector<typename Con::value_type> cfilter(const Con &c, Fun f) {
vector<typename Con::value_type> ret;
typename Con::const_iterator it = c.begin(), en = c.end();
for(; it != en; ++it){
if(f(*it)){
ret.emplace_back(*it);
}
}
return ret;
}
#if __cplusplus >= 201103L
template <class Con, class Fun>
auto cmap(const Con &c, Fun f) -> vector<decltype(f(*c.begin()))> {
vector<decltype(f(*c.begin()))> ret;
ret.reserve(c.size());
for(const auto &x: c){
ret.emplace_back(f(x));
}
return ret;
}
#endif
#if __cplusplus >= 201402L
#define lambda(e) ([&](const auto &_){ return (e); })
#define lambda2(e) ([&](const auto &_a, const auto &_b){ return (e); })
#endif
#define REP(i,n) for(int i=0;i<int(n);++i)
#define RALL(v) (v).rbegin(),(v).rend()
#define tget(t,i) get<i>(t)
#define MOD 1000000007LL
#define EPS 1e-8
VV(int) G;
VV(pii) qs;
vint ans;
deque<int> dfs(int u, int d){
deque<int> ret = {1};
for(int v : G[u]){
auto r = dfs(v, d + 1);
r.push_front(0);
if(ret.size() < r.size()){ ret.swap(r); }
for(size_t i = 0; i < r.size(); ++i){
ret[i] += r[i];
}
}
for(cauto p : qs[u]){
int k = p.second - d;
if(k < 0 || k >= (int)ret.size()){
ans[p.first] = 0;
}
else{
ans[p.first] = ret[k];
}
}
return ret;
}
void mainmain(){
int n;
cin >> n;
G.resize(n);
for(int i = 1; i < n; ++i){
int p;
cin >> p;
G[p - 1].push_back(i);
}
int q;
cin >> q;
ans.assign(q, -1);
qs.resize(n);
REP(i, q){
int u, d;
cin >> u >> d;
qs[u - 1].emplace_back(i, d);
}
dfs(0, 0);
for(int x : ans){ cout << x << '\n'; }
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(4);
mainmain();
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i, a, b) for(int i = (a); i < int(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
const int MAX = 200000;
int N, Q;
vector<int> G[MAX + 1], qs[MAX + 1];
int depth[MAX + 1], in[MAX + 1], out[MAX + 1];
void dfs(int u, int p, int d, int& k) {
in[u] = k++;
depth[u] = d;
qs[d].push_back(in[u]);
for(int v : G[u]) {
if(v == p) continue;
dfs(v, u, d+1, k);
}
out[u] = k++;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N;
REP(i, N-1) {
int p;
cin >> p; p--;
G[p].push_back(i+1);
G[i+1].push_back(p);
}
{
int k = 0;
dfs(0, -1, 0, k);
}
cin >> Q;
while(Q--) {
int u, d;
cin >> u >> d;
u--;
int res = upper_bound(ALL(qs[d]), out[u]) - lower_bound(ALL(qs[d]), in[u]);
cout << res << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;//2,3,5,7,11//6,10
//6,10,15,
int A[N];
A[0]=6;A[1]=10;A[2]=15;
int j=-1;
for (int i=3; i<N; i++) {
A[i]=6*(i-1);
if (A[i]>10000) {
j=i;break;
}
}
int jj=-1;
if (j>0) {
int k=2;
for (int i=j; i<N; i++) {
if (k%3==0) k++;
A[i]=10*k;
k++;
if (A[i]>10000) {
jj=i;break;
}
}
}
if (jj>0) {
int k=2;
for (int i=jj; i<N; i++) {
if (k%2==0) k++;
A[i]=15*k;
k++;
}
}
for (int i=0; i<N; i++) {
cout << A[i] << " ";
}
cout << endl;
} | //็บขๅคช้ณzhouakngyang txdy!
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#include<bits/stdc++.h>
#define Macesuted cout<<"kawaii"<<endl
#define MatrixCascade cout<<"npsl"<<endl
#define Karry5307 cout<<"nmsl"<<endl
using namespace std;
#define int long long
inline int read()
{
int sum=0,nega=1;char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-')nega=-1;ch=getchar();}
while(ch<='9'&&ch>='0')sum=sum*10+ch-'0',ch=getchar();
return sum*nega;
}
const int N=1e5+9,M=1e4;
int vis[N],n;
signed main()
{
for(int i=6;i<=M;i+=6)vis[i]=1;
for(int i=10;i<=M;i+=10)vis[i]=1;
for(int i=14;i<=M;i+=14)vis[i]=1;
n=read();cout<<105<<" ";n--;
int now=1;
while(n!=0)
{
if(vis[now])
{
printf("%d ",now);n--;
}
now++;
}
return 0;
}
|
// Created at 2021/02/06 20:47
// {TODO}WA, {TODO}min, {TODO}diff
// goal: {TODO}min
#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 (lint i = 0, i##_len = n; i < i##_len; i++)
#define rep2(i, n) for (lint i = 1, i##_len = n; i <= i##_len; i++)
#define rep3(i, s, e) for (lint i = s, i##_len = e; i < i##_len; i++)
#define all(i) (i).begin(), (i).end()
#define print(s) cout << s << '\n';
#define print2(s1, s2) cout << s1 << ' ' << s2 << '\n';
#define mk_p make_pair
using namespace std;
//using namespace atcoder;
using lint = long long;
using pi = pair<int, int>;
using pl = pair<lint, lint>;
using vi = vector<int>;
using vl = vector<lint>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using qi = queue<int>;
using ql = queue<lint>;
constexpr int INF = 1 << 30;
constexpr lint INFl = 1LL << 62;
template<class T, class U>
istream &operator>>(istream &is, pair <T, U> &pair) {
is >> pair.first >> pair.second;
return is;
}
template<class T, class U>
ostream &operator<<(ostream &os, pair <T, U> &pair) {
os << pair.first << " " << pair.second;
return os;
}
template<class T>
istream &operator>>(istream &is, vector <T> &vec) {
for (auto &v : vec) is >> v;
return is;
}
template<class T>
ostream &operator<<(ostream &os, const vector <T> &vec) {
os << '[';
for (auto v : vec) os << v << ',';
os << ']';
return os;
}
template<class T>
inline bool chmax(T &a, T b) {
if (b > a) {
a = b;
return true;
}
return false;
}
template<class T>
inline bool chmin(T &a, T b) {
if (b < a) {
a = b;
return true;
}
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
// cout << fixed << setprecision(15);
/*----------------------------------------------------*/
int v, t, s, d; cin >> v >> t >> s >> d;
if (v * t <= d && d <= v * s) {
print("No")
} else {
print("Yes")
}
}
// break? continue?
// ๆ้? ใกใขใช?
// map->bucket?
// ใใพใ->้่ฒ ใ็ขบ่ช
// set? multiset?
// ใชใผใใผใใญใผ็ขบ่ช(ๆๅคงๅคใๆฑใใ)
// ๅ
ฅๅ็ขบ่ช
// ๅ็ขบ่ช
// ใซใผใๅๆฐ็ขบ่ช(TLE)
// ๅฐๆฐใฎ่จ็ฎใฏ้ฟใใใ(ๅฒใ็ฎ)(0ๅฒใใ ใ๏ผ็ตถๅฏพ๏ผ)
// Python ใไฝฟใฃใฆใฏ?
// ๆฌไผผใณใผใใไฝใฃใฆใฏ?
// ๆฑใใฆใok
| #include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
void solve()
{
int a,b,c,d;
cin >> a >> b >> c >> d;
cout << a*d-b*c << "\n";
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int tt=1;
///cin >> tt;
while(tt--)
solve();
}
|
#include <bits/stdc++.h>
using namespace std;
int main(){
int N,K,M;
cin >> N >> K >> M;
vector<int> A(N-1);
int sum=0;
for(int i=0;i<N-1;i++){
cin >> A.at(i);
sum += A.at(i);
}
if(N*M-sum <= K && N*M-sum >=0) cout << N*M-sum << endl;
else if(sum >= N*M) cout << 0 << endl;
else cout << -1 << endl;
} | #include <bits/stdc++.h>
#define ll long long
#define ld long double
#define all(v) v.begin(), v.end()
#define sz size()
#define mp make_pair
#define pb push_back
#define rep(p, a, b) for (ll p = a; p < b; p++)
#define F first
#define S second
using namespace std;
typedef vector<ll> vll;
typedef vector<ld> vld;
typedef pair<ll, ll> pll;
int main()
{
ll t, i, j, k, ans, n, m, sum = 0;
//cin>>t;
t = 1;
while (t--)
{
cin >> n >> k >> m;
rep(p, 0, n - 1)
{
cin >> i;
sum += i;
}
ll mi = m * n - sum;
if (mi > k)
cout << "-1";
else
cout << max(0LL, mi);
}
return 0;
}
|
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cctype>
#define N 201000
template<typename T> inline void read(T &x) {
x = 0; char c = getchar(); bool flag = false;
while (!isdigit(c)) { if (c == '-') flag = true; c = getchar(); }
while (isdigit(c)) x = x * 10 + (c ^ 48), c = getchar();
if (flag) x = -x;
}
using namespace std;
int n;
char A[N], B[N], C[N];
inline void work() {
read(n);
scanf("%s", A + 1);
scanf("%s", B + 1);
scanf("%s", C + 1);
int cnt = (A[1] == '1') + (B[1] == '1') + (C[1] == '1');
if (cnt == 0) {
putchar('0');
for (int i = 1; i <= n + n; ++i) putchar('1');
puts("");
} else if (cnt == 3) {
putchar('1');
for (int i = 1; i <= n + n; ++i) putchar('0');
puts("");
} else if (cnt == 1) {
putchar('0');
for (int i= 1; i <= n; ++i) putchar('1');
for (int i = 1; i <= n; ++i) putchar('0');
puts("");
} else {
putchar('1');
for (int i = 1; i <= n; ++i) putchar('0');
for (int i = 1; i <= n; ++i) putchar('1');
puts("");
}
}
int main() {
int _; read(_);
while (_--) work();
return 0;
} | #include<bits/stdc++.h>
//Ctrl + B
using namespace std;
#define endl '\n'
#define nitro {std::ios_base::sync_with_stdio(false); std::cin.tie(NULL);}
#define pi 3.141592653589793
#define mod 1000000007
#define lb lower_bound
#define ub upper_bound
#define all(v) v.begin(), v.end()
#define dbg(x) cout << x << '\n';
#define case_g(x) cout<<"Case "<<x<<": ";
#define fi first
#define se second
#define pb push_back
#define INF (ll)(1e5+10)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef unsigned long ul;
typedef std::vector<int> vi;
typedef std::vector<ll> vl;
typedef std::vector<bool> vb;
typedef std::pair<int, int> pii;
typedef std::pair<ll, ll> pll;
typedef std::pair<int, bool> pib;
typedef std::vector<pii> vii;
typedef std::vector<pll> vll;
typedef std::vector<pib> vib;
typedef std::unordered_map<int, int> umapii;
typedef std::unordered_map<ll, ll> umapll;
typedef std::unordered_map<int , bool> umapib;
typedef std::map<int, int> mapii;
typedef std::map<ll, ll> mapll;
typedef std::map<int, bool> mapib;
ll ncr(ll n, ll r){//ncr formula
ll m=min(r,n-r);
ll ans=1;
for(int i=1;i<=m;i++){
ans=ans*(n-i+1)/i;
}
return ans;
}
ll modpow(ll a, ll b) { //modulo power
ll result = 1, M=mod;
while(b!=0) {
if(b&1) {
result = ((result%M)*(a%M))%M;
}
a = ((a%M)*(a%M))%M;
b >>= 1;
}
return result;
}
ll modinv(ll a) { //modulo inverse
ll m = mod, y = 0, x = 1;
while (a > 1) {
ll q = a / m, t = m;
m = a % m;
a = t;
t = y;
y = x - q * y;
x = t;
}
return x < 0 ? x + mod : x;
}
void solve()
{
string s;
cin>>s;
ll n= s.length();
for(int i=1;i<n;i++)
{
cout<<s[i];
}
cout<<s[0];
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
nitro
int tc;
tc =1;
//cin >> tc;
for(int i=1; i<=tc; ++i)
{
//case_g(i);
solve();
}
}
|
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pint = pair<int,int>;
using p = complex<double>;
void f(vector<p>& B) {
int N = B.size();
double x = 0, y = 0;
for(auto u : B)x += u.real(), y += u.imag();
x /= N, y /= N;
for(auto&u : B) {
u = {u.real() - x, u.imag() - y};
}
sort(B.begin(), B.end(), [&](p a, p b){return (a.real() == b.real() ? a.imag() < b.imag() : a.real() < b.real()) ;});
}
void g(vector<p>& B, double b) {
for(auto &i : B) {
i *= p{cos(b), sin(b)};
}
sort(B.begin(), B.end(), [&](p a, p b){return (a.real() == b.real() ? a.imag() < b.imag() : a.real() < b.real()) ;});
}
// #include"debug.hpp"
int main()
{
std::random_device seed_gen;
std::default_random_engine engine(seed_gen());
std::uniform_real_distribution<> dist(0.0, 2 * acos(-1));
int N; cin >> N;
vector<p> A(N), B(N);
for(auto &u : A) {
double a, b; cin >> a >> b;
u = {a, b};
}
for(auto &u : B) {
double a, b; cin >> a >> b;
u = {a, b};
}
f(A);
f(B);
// print(A);
// print(B);
const double eps = 1e-2;
for(int i = 0; i < 100000; i++) {
g(B, dist(engine));
bool ok = true;
for(int i = 0; i < N; i++) {
auto v = A[i] - B[i];
if(abs(v) > eps) {
ok = false;
}
}
if(ok) {
cout << "Yes" << endl;
return 0;
}
}
cout << "No" << endl;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define MOD int(1e9+7)
#define INF int(1e9)
#define LINF ll(1e18)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define P pair<ll,ll>
int n,t,half1,half2;
ll a[45];
ll f[1<<21];
ll nibutan(ll former){
ll latter=t-former;
int ok=0; int ng=(1<<half1);
while(ng-ok>1){
int mid=(ok+ng)/2;
if(f[mid]<=latter) ok=mid;
else ng=mid;
}
return former+f[ok];
}
int main(){
cin>>n>>t;
rep(i,n) cin>>a[i];
half1=n/2,half2=(n+1)/2;
rep(i,1<<half1){
ll res=0;
rep(j,half1){
if(i>>j & 1) res+=a[j];
}
f[i]=res;
}
sort(f,f+(1<<(half1)));
ll ans=0;
rep(i,1<<half2){
ll res=0;
rep(j,half2){
if(i>>j & 1) res+=a[j+half1];
}
if(res>t) continue;
res = nibutan(res);
ans=max(ans,res);
}
cout<<ans<<endl;
} |
#include <bits/stdc++.h>
#define ll long long
#define lll long long
#define mp make_pair
#define pb push_back
ll inf = 1000000000000000000;
#define deci(n) fixed << setprecision(n)
#define vll vector<ll>
#define vpll vector<pair<ll, ll>>
#define umll unordered_map<ll, ll>
#define mll map<ll, ll>
//#define f first
//#define s second
#define mt make_tuple
#define g(i, name) get<i>(name)
using namespace std;
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 = std::chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
ll p = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// uniform_int_distribution<ll>(l, r)(rng);
//*****************CODE STARTS **************************************
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll n; cin >> n;
ll a[n];
for(ll i=0; i<n; i++)cin >> a[i];
ll dp[n+1][n]; // seq num and start
for(ll i=n; i>0; i--){
unordered_map <ll, ll> m;
m[0]=1;
ll sum=0;
for(ll j=n-1; j>=0; j--){
sum+=a[j]; sum%=i;
dp[i][j] = m[sum];
if(i!=n)m[sum]+=dp[i+1][j];
else m[sum]++;
m[sum]%=p;
}
}
cout << dp[1][0];
}
| ///Bismillahir Rahmanir Rahim
#include "bits/stdc++.h"
#define ll long long
#define int ll
#define fi first
#define si second
#define mp make_pair
#define pb push_back
#define pi pair<int,int>
#define nd(a,b,c) mp(mp(a,b),c)
#define clr(x) memset(x,0,sizeof(x));
#define f(i,l,r) for(int i=l;i<=r;i++)
#define rf(i,r,l) for(int i=r;i>=l;i--)
#define done(i) cout<<"done = "<<i<<endl;
#define show(x,y) cout<<x<<" : ";for(auto z:y)cout<<z<<" ";cout<<endl;
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const ll inf=2e18;
const int mod=1e9+7;
const int M=3005;
inline ll bigmod(ll B,ll P){ll R=1;while(P>0){if(P&1){R=(R*B)%mod;}P>>=1;B=(B*B)%mod;}return R;}
inline ll ad(ll x,ll y){ll ret=(x%mod+y%mod)%mod;if(ret<0){ret+=mod,ret=ret%mod;}return ret;}
inline ll sub(ll x,ll y){ll ret=((x%mod)-(y%mod)+mod)%mod;if(ret<0){ret+=mod,ret=ret%mod;}return ret;}
inline ll gun(ll x,ll y){ll ret=((x%mod)*(y%mod))%mod;if(ret<0){ret+=mod,ret=ret%mod;}return ret;}
int n;
int a[M],pre[M];
int dp[M][M];
int sum[M],nxt[M];
main()
{
fast
cin>>n;
f(i,1,n)cin>>a[i],pre[i]=a[i]+pre[i-1];
rf(k,n,1)
{
f(pos,1,n)
{
int val=pre[pos-1]%k;
sum[val]=sub(sum[val],dp[pos][k+1]);
dp[pos][k]=sum[val];
int z=pre[n]-pre[pos-1];
z=z%k;
if(z==0)dp[pos][k]=(dp[pos][k]+1)%mod;
if(k!=1){
val=pre[pos-1]%(k-1);
nxt[val]=ad(nxt[val],dp[pos][k]);
}
}
f(j,0,k+1)
{
sum[j]=nxt[j];
nxt[j]=0;
}
}
cout<<dp[1][1]<<"\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define Rrep(i,n) for (ll i = n - 1; i >= 0;i--)
#define ll long long int
#define INF 1000000007
#define llINF 1000000000000000007
#define v(i) vector<i>
#define vv(i) vector<vector<i>>
// int num = atoi(string.c_str()); ๆๅญๅใฎๆดๆฐๅคๆ
// sort(a,a+n,greater<int>()); ๆ้ ใฝใผใif(ans>llINF/a&&a !=0)return 0;
int digit(ll n){int ans=0;while(n>0){n /= 10; ans++;}return ans;}//ๆกๆฐ
ll _pow(ll a,ll b){ll ans = 1;rep(i,b){ans = a * ans;}return ans;}//้ไน
ll _modpow(ll a, ll n, ll mod) {ll res = 1;while (n > 0) {if (n & 1){ res = res * a % mod;}a = a * a % mod;n >>= 1;}return res;}
ll gcd(ll a, ll b){if (a%b == 0){return(b);}else{return(gcd(b, a%b));}}//ๆๅคงๅ
ฌ็ดๆฐ
ll lcm(ll a, ll b){return a * b / gcd(a, b);} //ๆๅฐๅ
ฌๅๆฐ
struct UnionFind {
vector<int> par; // par[i]:iใฎ่ฆชใฎ็ชๅทใ(ไพ) par[3] = 2 : 3ใฎ่ฆชใ2
UnionFind(int N) : par(N) { //ๆๅใฏๅ
จใฆใๆ นใงใใใจใใฆๅๆๅ
for(int i = 0; i < N; i++) par[i] = i;
}
int root(int x) { // ใใผใฟxใๅฑใใๆจใฎๆ นใๅๅธฐใงๅพใ๏ผroot(x) = {xใฎๆจใฎๆ น}
if (par[x] == x) return x;
return par[x] = root(par[x]);//็ต่ทฏๅง็ธฎ
}
void unite(int x, int y) { // xใyใฎๆจใซไฝตๅ
int rx = root(x); //xใฎๆ นใrx
int ry = root(y); //yใฎๆ นใry
if (rx == ry) return; //xใจyใฎๆ นใๅใ(=ๅใๆจใซใใ)ๆใฏใใฎใพใพ
par[rx] = ry; //xใจyใฎๆ นใๅใใงใชใ(=ๅใๆจใซใชใ)ๆ๏ผxใฎๆ นrxใyใฎๆ นryใซใคใใ
}
bool same(int x, int y) { // 2ใคใฎใใผใฟx, yใๅฑใใๆจใๅใใชใtrueใ่ฟใ
int rx = root(x);
int ry = root(y);
return rx == ry;
}};
ll cmb(ll n, ll r){ll X = 1;for(int i = n; i >= n - r + 1;i--){X = X * i % INF;}ll Y = 1;for(int i = r;i > 0 ;i--){Y = Y * i % INF;}Y = _modpow(Y,(ll)INF-2,(ll)INF);return X * Y % INF;}
int main(){
ll n;cin>>n;
ll c[n][n];
ll tmp = 0;
ll m = INF;
rep(i,n){
rep(j,n){
cin>>c[i][j];
if(c[i][j] < m){
m = c[i][j];
tmp = i;
}
}
}
v(ll)a,b;
rep(i,n){
b.push_back(c[tmp][i]);
}
rep(i,n){
ll now = c[i][0] - b[0];
rep(j,n){
if(c[i][j] - b[j] != now){
cout<<"No"<<endl;
return 0;
}
}
a.push_back(now);
}
cout<<"Yes"<<endl;
rep(i,n){
cout<<a[i]<<" ";
}cout<<endl;
rep(i,n){
cout<<b[i]<<" ";
}cout<<endl;
return 0;
} | /*
Author: Zcus
Blog: cnblogs.com/Zcus
*/
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define mp make_pair
#define debug(x) cout << #x << " = " << x << endl
#define For(i, x, y) for (int i = x; i <= y; i++)
#define Rep(i, x, y) for (int i = x; i >= y; i--)
#define file(FILE_NAME) freopen(FILE_NAME".in", "r", stdin), freopen(FILE_NAME".out", "w", stdout)
#define filein(FILE_NAME) freopen(FILE_NAME".in", "r", stdin);
#define fileout(FILE_NAME) freopen(FILE_NAME".out", "w", stdout);
template<class T> inline bool Chkmax(T& x, const T& y) { return x < y ? x = y, true : false; }
template<class T> inline bool Chkmin(T& x, const T& y) { return x > y ? x = y, true : false; }
#define I int
inline I ri() {
I s = 0, w = 1;
char c = getchar();
while (c < '0' || c > '9') {if (c == '-') w = -1; c = getchar();}
while (c >= '0' && c <= '9') { s = (s << 1) + (s << 3) + (c ^ 48); c = getchar();}
return s * w;
}
#undef I
#define int long long
const int maxn = 510;
int n;
int c[maxn][maxn];
int x, y;
int ss = 0;
int bb[maxn], aa[maxn];
signed main() {
ios :: sync_with_stdio(0); cin.tie(0);
cin >> n;
For (i, 1, n) For (j, 1, n) cin >> c[i][j];
For (i, 2, n) bb[i] = c[1][i] - c[1][i - 1];
For (i, 2, n) aa[i] = c[i][1] - c[i - 1][1];
//a[2] + b[2] =
//x + aa[2] + y + bb[2] = a[2][2];
aa[1] = 0, bb[1] = c[1][1];
for (int i = 1; i <= n; i++) bb[i] = bb[i - 1] + bb[i];
for (int i = 1; i <= n; i++) aa[i] = aa[i - 1] + aa[i];
int pos1 = -1, minn = 0;
for (int i = 1; i <= n; i++) {
if (aa[i] < 0) {
pos1 = 1;
Chkmin(minn, aa[i]);
}
if (bb[i] < 0) {
pos1 = 2;
Chkmin(minn, bb[i]);
}
}
minn = -minn;
if (pos1 == 1) {
for (int i = 1; i <= n; i++) aa[i] += minn, bb[i] -= minn;
}
if (pos1 == 2) for (int i = 1; i <= n; i++) bb[i] += minn, aa[i] -= minn;
for (int i = 1; i <= n; i++) {
if (aa[i] < 0 || bb[i] < 0) {
puts("No"); return 0;
}
}
int ok = 1;
For (i, 1, n) For (j, 1, n) if (c[i][j] != aa[i] + bb[j]) ok = 0;
if (ok == 1) {
puts("Yes");
for (int i = 1; i <= n; i++) printf("%lld ", aa[i]);
puts("");
for (int i = 1; i <= n; i++) printf("%lld ", bb[i]);
return 0;
}
else {
printf("No");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define ll long long
#define pp pair<ll,ll>
#define ld long double
#define all(a) (a).begin(),(a).end()
#define mk make_pair
int inf=1000001000;
ll INF=2e18;
ll mod=1000000007;
ll MOD=998244353;
int main() {
vector<int> a(4);
rep(i,4) cin >> a[i];
bool t=false;
rep(j,16){
int b=0,c=0;
int f=j;
rep(i,4){
if (f%2==0) b+=a[i];
else c+=a[i];
f/=2;
}
if (b==c) t=true;
}
if (t) cout << "Yes" << endl;
else cout << "No" << endl;
}
| #include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define ll long long int
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> ii;
typedef vector<ii> vii;
int main(){
int n, k, m ;
scanf("%d %d %d", &n, &k, &m);
vi v(n-1);
int sum = 0;
for(int i = 0; i < n-1; i++){
scanf("%d", &v[i]);
sum += v[i];
}
int r = -1;
for(int i = 0; i <= k; i++){
if((sum+i)/n >= m){
r = i;
break;
}
}
printf("%d\n", r);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
long N; cin >> N;
vector<long> used(200000);
for(long i = 0; i < N; i++){
used[i] = 0;
}
long now_min = 0;
for(long i = 0; i < N; i++){
long p; cin >> p;
used[p] = 1;
if(p == now_min){
while(true){
now_min ++;
if(used[now_min] == 0) break;
}
}
cout << now_min << endl;
}
} | #include<bits/stdc++.h>
using namespace std;
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> p2;
#define sz(a) ll(a.size())
void solve(){
ll n;
cin>>n;
set<ll> s;
for(ll i=0;i<n+10;i++)
s.insert(i);
for(ll i=0;i<n;i++){
ll x;
cin>>x;
s.erase(x);
cout<<(*s.begin())<<"\n";
}
}
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
clock_t z=clock();
ll qc=1;
//cin>>qc;
for(ll i=1;i<=qc;i++){
solve();
}
debug("Total Time:%.4Lf\n",(ld)(clock()-z)/CLOCKS_PER_SEC);
} |
#include <cstdlib>
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<ii>
#define vc vector<char>
#define vs vector<string>
#define vd vector<double>
#define vll vector<ll>
#define vvi vector<vi>
#define vvii vector<vii>
#define vvc vector<vc>
#define vvs vector<vs>
#define vvll vector<vll>
#define vvd vector<vd>
#define FOR(x,n) for(int x=0;x<n;x++)
#define FORS(x,n) for(int x=1;x<=n;x++)
#define FORE(x,a) for(auto &x: a)
#define ALL(x) x.begin(),x.end()
#define REP(n) for(int _ = 0; _ < n; _++)
#define MT make_tuple
#define pb push_back
#define endl '\n'
#define F first
#define S second
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<ii,
null_type,
less<ii>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int h,w,m;
cin >> h >> w >> m;
vi mw(w,h),mh(h,w);
FOR(i,m) {
int x,y;
cin >> x >> y;
x--;y--;
mh[x] = min(y,mh[x]);
mw[y] = min(x,mw[y]);
}
ll sm = 0;
FOR(i,mh[0]) {
sm += mw[i];
}
FOR(i,mw[0]) {
sm += mh[i];
}
//cout << " ! " << sm << endl;
// subtract:
ordered_set s;
vii t;
FOR(i,mw[0]) {
t.pb({mh[i],i});
}
sort(ALL(t));
int hp = 0;
FOR(i,mw[0]) {
while(t[i].F > hp && mh[0] > hp){
s.insert({mw[hp],hp});
hp++;
}
int lt = s.order_of_key({t[i].S,1e9});
int gt = s.size() - lt;
sm -= gt;
//cout << " " << gt << " " << lt << " " << t[i].F << " " << t[i].S << endl;
}
cout << sm << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define M1 1000000007
#define M2 998244353
#define INF 1e18
#define ll long long
#define pll pair<ll,ll>
#define REP(i,a,b) for(ll i=a;i<b;i++)
#define REPR(i,a,b) for(ll i=b-1;i>=a;i--)
#define forr(i,n) for(ll i=0;i<n;i++)
#define F first
#define S second
#define pb push_back
#define DB pop_back
#define mp make_pair
#define MT make_tuple
#define V(a) vector<a>
#define vi vector<int>
#define vlli vector <long long>
#define endl '\n'
#define ce(ele) cout<<ele<<' '
#define cs(ele) cout<<ele<<'\n'
#define CASE(t) ll t; cin>>t; while(t--)
/********************************************************************/
const double pi = 3.1415926535;
/********************************************************************/
//FAST IO//
void FAST() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
/********************************************************************/
int main()
{
FAST();
int n; cin>>n;
string s,t; cin>>s>>t;
int ans=0, ct1=0, ct2=0;
for (int i=0; i<n; i++)
{
if (s[i]=='0' && t[i]=='0')
{
if (ct1==ct2){continue; }
ct1++; ct2++; ans++;
}
else if (s[i]=='0')
{
ct1++; ans++;
}
else if (t[i]=='0')
{
ct2++;
}
}
if (ct1!=ct2){cout<<-1<<endl; return 0;}
else {cout<<ans<<endl ;}
} |
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define endl "\n"
#define REP(i, s, n) for (int i = (s); i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
#define CHECK(a) cout << #a << " " << a << endl;
#define CHECK_VEC(V) REP (i, 0, V.size()) cout << i << " " << V[i] << endl;
const int INF = 1e9;
const ll MOD = (ll)1e9 + 7LL;
const vector<int> dx = {0, 1, 0, -1, 0, 1, -1, 1, -1};
const vector<int> dy = {0, 0, 1, 0, -1, 1, 1, -1, -1};
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; }
bool comp (pair<string, int> a, pair<string, int> b) {
if (a.second != b.second) return a.second > b.second;
else return a.first > b.first;
}
void Main () {
int N;
cin >> N;
vector<pair<string, int>> M (N);
REP (i, 0, N) cin >> M[i].first >> M[i].second;
sort(ALL(M), comp);
cout << M[1].first << endl;
}
int main () {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
//int n; cin >> n; while(n--)
Main();
} | #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define rep(i,n) for(int i=0;i<(int)n;++i)
int main(){
long long n,a,s=0,ans=0;
cin>>n;
rep(i,n){
cin>>a;
ans+=a*a*n;
s+=a;
}
ans-=s*s;
cout<<ans<<endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int n;
inline bool check(int x) {
int u=x;
while(u) {
if(u%10==7) return false;
u/=10;
}
u=x;
while(u) {
if(u%8==7) return false;
u/=8;
}
return true;
}
int main() {
cin>>n;
int sum=0;
for(int i=1;i<=n;++i)sum+=check(i);cout<<sum;return 0;
} | // Author: Vinay Khilwani
// Language: C++
// @vok8: Codeforces, AtCoder, LeetCode, HackerEarth, TopCoder, Google, FB, CSES, Spoj, GitHub
// @vok_8: CodeChef, GFG
// @vok8_khilwani: HackerRank
// Never Stop Trying.
// Trying to be Better than Myself.
// while(true)
// {
// if(AC)
// {
// break;
// }
// else if(Contest Over)
// {
// Try.
// Check out Editorial.
// Understand.
// Find out your Mistake.
// Learn the topic (if new).
// Solve Problems on that topic (if new).
// Upsolve that problem.
// break;
// }
// else
// {
// Try.
// Use Pen-Paper.
// Find errors, edge cases, etc.
// continue;
// }
// }
// Optimizations
#pragma GCC optimize("O2")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
#pragma GCC optimize("Os")
// Libraries
#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;
// Debugging
#define dbg(a) cerr<<a<<"\n";
#define debug_a(a) for(auto x:a) {cerr<<x<<" ";} cerr<<"\n";
#define debug_b(a) for(auto x:a) {cerr<<"["<<x.first<<", "<<x.second<<"]"<<"\n";} cerr<<"\n";
#define debug_c(a) for(auto x:a) {debug_a(x)} cerr<<"\n";
#define debug_d(a) for(auto x:a) {debug_b(x)} cerr<<"\n";
#define debug_e(a) cerr<<"["<<a.first<<", "<<a.second<<"]"<<"\n";
// Defines
#define fast ios_base::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL);
#define loop(i,a,n) for(int i=a; i<n; i++)
#define rloop(i,a,n) for(int i=a; i>=n; i--)
#define fr(i,a,n,b) for(int i=a; i<n; i+=b)
#define rfr(i,a,n,b) for(int i=a; i>=n; i-=b)
#define IN cin>>
#define OUT cout<<
#define nl "\n"
#define sz(a) int(a.size())
#define all(a) (a).begin(),(a).end()
#define each(a,b) for(auto &a:b)
#define pb push_back
#define set_bits(a) __builtin_popcountll(a)
#define ar array
#define write(a) for(auto x:a) {OUT x<<" ";} OUT endl;
#define read(a) for(auto &x:a) {IN x;}
// #define oset tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
using ll=long long int;
using ld=long double;
using pll=pair<ll,ll>;
using pii=pair<int,int>;
using vll=vector<ll>;
using vi=vector<int>;
const ll mod=(ll)(1e9)+7LL;
const ll M=998244353LL;
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};
const ld pi=acos(-1);
// General Functions
ll gcd(ll a, ll b)
{
return (b?gcd(b,a%b):a);
}
ll P(ll B, ll power, ll modulo) //Fast Power
{
ll ans=1LL;
while(power>0LL)
{
if(power%2LL==1LL)
{
ans=(ans*B)%modulo;
}
B=(B*B)%modulo;
power/=2LL;
}
return ans;
}
bool isPrime(ll n)
{
if(n<=1LL)
{
return false;
}
if(n<=3LL)
{
return true;
}
if(n%2==0LL || n%3==0LL)
{
return false;
}
for(ll i=5LL; (i*i)<=n; i+=6LL)
{
if(n%i==0LL || n%(i+2LL)==0LL)
{
return false;
}
}
return true;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll get_rand(ll l, ll r)
{
uniform_int_distribution<ll> uid(l,r);
return uid(rng);
}
void vok()
{
fast
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
freopen("error.txt","w",stderr);
#endif
}
// Global Variables
const int mxN=int(1e5)+100;
// Solver Function(s)
void solve()
{
int a,b;
IN a>>b;
int ans=a%10+((a/10)%10)+((a/100)%10);
ans=max(ans,b%10+((b/10)%10)+((b/100)%10));
OUT ans<<nl;
}
// Main Function
int main()
{
vok();
int t=1;
// IN t;
while(t--)
{
solve();
}
return 0;
} |
#include <bits/stdc++.h>
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int a, b;
cin >> a >> b;
cout << (a + b) / 2 << " " << (a - b) / 2 << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int a,b;
// 3 1
cin>>a>>b;
int i,j;
for (i=-100;i<=100;i++){
for( j=-100;j<=100;j++){
if(i+j == a && i-j==b){
cout<<i<<" "<<j<<endl;
return 0;
}
}
}
return 0;
} |
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const ll INF=1e10+1;
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
//freopen("input.txt","r",stdin);
ll n;
cin>>n;
ll ans=0;
if(n>=1e3) ans+=n-(1e3-1);
if(n>=1e6) ans+=n-(1e6-1);
if(n>=1e9) ans+=n-(1e9-1);
if(n>=1e12) ans+=n-(1e12-1);
if(n>=1e15) ans+=n-(1e15-1);
cout<<ans<<endl;
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
ll n; cin >> n;
ll ans = 0;
if (n >= 1000) {
ans += min(n, (ll)999999) - 999;
}
if (n >= 1000000) {
ans += 2 * (min(n, (ll)999999999) - 999999);
}
if (n >= 1000000000) {
ans += 3 * (min(n, (ll)999999999999) - 999999999);
}
if (n >= 1000000000000) {
ans += 4 * (min(n, (ll)999999999999999) - 999999999999);
}
if (n >= 1000000000000000) {
ans += 5 * (n - 999999999999999);
}
cout << ans << endl;
return 0;
} |
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <random>
#include <queue>
#include <stack>
#include <bitset>
#include <cassert>
#include <unordered_map>
#include <iomanip>
typedef long long ll;
#define double long double
#define rep(i,n) for(int i=0;i<n;i++)
#define srep(i,n,m) for(int i=n;i<m;i++)
#define elif else if
#define pi 3.141592653589793
#define prique priority_queue
constexpr auto mod=1000000007;
constexpr auto MOD=1000000000000000;//10^15
using namespace std;
int gcd(int a,int b){
if(b==0) return a;
return gcd(b,a%b);
}
int lcm(int a,int b){
return a*b/gcd(a,b);
}
bool prime(int n){
int cnt=0;
for(int i=1;i<=sqrt(n);i++){
if(n%1==0)cnt++;
}
if(cnt!=1) return false;
else return n!=1;
}
void syou(int a,double b){
cout<<setprecision(a)<<b<<endl;
}
struct UnionFind{
vector<int> r;
UnionFind(int N){
r=vector<int>(N,-1);
}
int root(int x){
if(r[x]<0) return x;
return r[x]=root(r[x]);
}
bool unite(int x,int y){
x=root(x);
y=root(y);
if(x==y) return false;
if(r[x]>r[y]) swap(x,y);
r[x]+=r[y];
r[y]=x;
return true;
}
int size(int x){
return -r[root(x)];
}
};
struct Edge{
int to;
int weight;
};
struct fenwick_tree {
typedef int T;
T n;
vector<T> bit;
fenwick_tree(T num) : bit(num+1, 0) { n = num; }
void add(T i, T w) {
for (T x = i; x <= n; x += x & -x) {
bit[x] += w;
}
}
T sum(T i) {
T ret = 0;
for (T x = i; x > 0; x -= x & -x) {
ret += bit[x];
}
return ret;
}
T sum(T left, T right) {
return sum(right) - sum(left);
}
};
bool flag;
ll n,m,cnt=0,ans=0,aa[220000];
pair<ll,ll> pp[220000];
string s,ss[110000];
queue<ll> que;
signed main(){
cin>>n;
cout<<n-1;
} | #include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
cout<<n-1;
} |
#include <bits/stdc++.h>
using namespace std;
int main()
{
long long int a,b,c;
cin>>a>>b>>c;
if(c%2==0)
{
if(abs(a)==abs(b))
cout<<"=";
else if(abs(a)>abs(b))
cout<<">";
else
cout<<"<";
}
else
{
if(a>b)
cout<<">";
else if(a<b)
cout<<"<";
else
cout<<"=";
}
} | #include <bits/stdc++.h>
#define int long long
#define f first
#define s second
#define rep(i,a,b)for(int i=a;i<b;i++)
#define pb push_back
const int mod = 1e9 + 7;
const int INF = 1e18;
const int SIZE = 1e6 * 5 + 5;
using namespace std;
void solve() {
int a[3];
cin >> a[0];
cin >> a[1];
cin >> a[2];
sort(a, a + 3);
int diff1 = a[1] - a[0];
int diff2 = a[2] - a[1];
if (diff2 == diff1) {
cout << "Yes\n";
}
else {
cout << "No\n";
}
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int T = 1;
// cin >> T;
// sieve();
// op();
while (T--)
{
solve();
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define repx(i, x, n) for (ll i = (ll)(x); i < (ll)(n); i++)
const double PI = 3.1415926535897932;
#define rep(i,n) for (int i = 0; i < (n); ++i)
using ll = long long;
using P = pair<int,int>;
int main(){
int a,b;
cin >> a >> b ;
if(a+b >=15 && b >= 8){
cout << '1' << endl;
}else if (a+b>=10 && b>=3) {
cout << '2' << endl;
}else if(a+b>=3) {
cout << '3' << endl;
}else {
cout << '4' << endl;
}
return 0;
} | /*
Auth: Loxilante
Time: 2020/10/03
Prog: ARC104-A
Lang: cpp
*/
#include <bits/extc++.h>
#define rep(i, l, r) for(int i = l; i < r; i++)
#define hrp(i, l, r) for(int i = l; i <= r; i++)
#define rev(i, r, l) for(int i = r; i >= l; i--)
#define ms(n, t) memset(n, t, sizeof(n))
#define pb push_back
#define int ll
#ifndef JOEON
#define D(...) 97
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
template<typename tn = int> inline tn next(void) { tn k; cin>>k; return k; }
signed main(void)
{
clock_t Begin = clock();
#ifdef JOEON
// freopen("C:\\Users\\Joeon\\Desktop\\IN.txt", "r", stdin);
// freopen("C:\\Users\\Joeon\\Desktop\\OUT.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int a, b;
cin>>a>>b;
cout<<(a+b)/2<<' '<<(a-b)/2<<endl;
clock_t InputFinished = clock();
clock_t End = clock();
D((double)(End-Begin)/CLOCKS_PER_SEC);
D((double)(End-InputFinished)/CLOCKS_PER_SEC);
#ifdef JOEON
system("pause");
#endif
return 0;
}
/*
*/ |
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define deb(x) cout<< #x << " " << x << "\n";
#define MAX 9223372036854775807
#define MIN -9223372036854775807
#define PI 3.141592653589
#define setbits(n) __builtin_popcountll(n)
#define mkunique(a) a.resize(unique(a.begin(),a.end())-a.begin());
const ll mod=1e9+7;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll T=clock();
ll n;
cin>>n;
vector<ll> a(n), b(n), p(n);
set<pair<ll,pair<ll,ll>>> s;
for(ll i=0;i<n;i++)
cin>>a[i];
for(ll i=0;i<n;i++)
cin>>b[i];
for(ll i=0;i<n;i++){
cin>>p[i]; p[i]--;
if(i!=p[i])
s.insert({b[p[i]],{i,p[i]}});
}
vector<pair<ll,ll>> ans;
while(s.size()>0){
ll val=s.rbegin()->first;
ll c=s.rbegin()->second.first;
ll to=s.rbegin()->second.second;
//cout<<val<<" "<<c<<" "<<to<<": ";
if(val>=a[c])
break;
s.erase(--s.end());
ll bagpos=p[to];
ll bagwt=b[bagpos];
//cout<<bagwt<<" "<<to<<" "<<bagpos<<endl;
if(bagwt>=a[to])
break;
ans.pb({c,to});
assert(s.find({bagwt,{to,bagpos}})!=s.end());
p[c]=p[to];
p[to]=to;
s.erase({bagwt,{to,bagpos}});
if(c!=bagpos)
s.insert({bagwt,{c,bagpos}});
}
if(s.size()!=0)
cout<<-1;
else{
cout<<ans.size()<<"\n";
for(ll i=0;i<ans.size();i++)
cout<<ans[i].first+1<<" "<<ans[i].second+1<<"\n";
}
cerr<<"\n\nTIME: "<<(double)(clock()-T)/CLOCKS_PER_SEC<<" sec\n";
T = clock();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define N 2114514
const ll mod = 998244353;
const ll inf = 1000000000000000000;
bool used[N];
void jk(ll s, ll d[], vector<vector<pair<ll, P>>>& v, ll n) {
priority_queue<P, vector<P>, greater<P>> que;
for (int i = 1; i <= n; i++) d[i] = inf;
d[s] = 0;
que.push(P(0, s));
while (!que.empty()) {
ll x;
x = que.top().second;
que.pop();
if (used[x]) continue;
used[x] = 1;
for (auto p : v[x]) {
ll nv, nt, nc;
nv = p.first;
nt = p.second.first;
nc = p.second.second;
ll cos = (d[x] + nc - 1) / nc * nc + nt;
if (cos < d[nv]) {
d[nv] = cos;
que.push(P(d[nv], nv));
}
}
}
}
ll dis[N];
int main() {
ll n, m, x, y;
cin >> n >> m >> x >> y;
vector<vector<pair<ll, P>>> v(n + 1);
for (int i = 0; i < m; i++) {
ll a, b, t, k;
cin >> a >> b >> t >> k;
v[a].push_back({b, {t, k}});
v[b].push_back({a, {t, k}});
}
jk(x, dis, v, n);
cout << (dis[y] == inf ? -1 : dis[y]) << endl;
return 0;
}
|
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <tuple>
#include <vector>
using namespace std;
#define REP(i, n) for (ll i = 0; i < ll(n); i++)
#define FOR(i, a, b) for (ll i = a; i <= ll(b); i++)
#define ALL(x) x.begin(), x.end()
typedef long long ll;
template <typename T>
void print_vec(vector<T> v) {
cout << "[ ";
for (auto const &x : v) {
cout << x;
if (&x != &v.back()) {
cout << " : ";
}
}
cout << " ]" << endl;
}
template <typename T>
void print_vec(vector<vector<T>> v) {
cout << "[" << endl;
for (auto const &x : v) {
cout << " ";
print_vec(x);
}
cout << "]" << endl;
}
template <typename T, typename U, typename Comp = less<>>
bool chmax(T &xmax, const U &x, Comp comp = {}) {
if (comp(xmax, x)) {
xmax = x;
return true;
}
return false;
}
template <typename T, typename U, typename Comp = less<>>
bool chmin(T &xmin, const U &x, Comp comp = {}) {
if (comp(x, xmin)) {
xmin = x;
return true;
}
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll N, a;
cin >> N;
vector<ll> alist(N, 0);
vector<ll> anslist(N, 0);
REP(i, N) { cin >> alist[i]; }
REP(i, N) {
a = alist[i];
ll up = N, down = -1;
for (ll j = i - 1; j >= 0; j--) {
if (a > alist[j]) {
down = j;
break;
}
}
for (ll j = i + 1; j < N; j++) {
if (a > alist[j]) {
up = j;
break;
}
}
// cout << i << " " << up << " " << down << endl;
anslist[i] = (up - down - 1) * a;
}
// print_vec(anslist);
ll ans = 0;
REP(i, N) { chmax(ans, anslist[i]); }
// print_vec(anslist);
cout << ans << endl;
} | //
// Created by K R, Siva Kumar on 23/01/21 at 6:24 PM.
//
#include <bits/stdc++.h>
using namespace std;
const double pi = 2 * acos(0.0);
const int mod = 1e9 + 7;
class Solver {
int n;
vector<int> a;
public:
void solve() {
cin >> n;
a.assign(n, 0);
map<int, int> mp;
for (int i = 0; i < n; i++) {
cin >> a[i];
mp[a[i]]++;
}
int res = 0;
for (int i = 0; i < n; i++) {
int mn = INT_MAX;
for (int j = i; j < n; j++) {
mn = min(mn, a[j]);
res = max(res, (j - i + 1) * mn);
}
}
cout << res;
}
};
int main() {
#ifndef ONLINE_JUDGE
freopen("/Users/sivkr/Workspace/CompetitiveProgramming/HackerEarth/1.txt",
"r", stdin);
freopen("/Users/sivkr/Workspace/CompetitiveProgramming/HackerEarth/2.txt",
"w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
Solver s;
s.solve();
}
|
#pragma GCC optimize("Ofast", "unroll-loops")
#pragma GCC terget("avx2")
#include <bits/stdc++.h>
struct UnionFind{
std::vector<int> data;
/* constructor */
UnionFind() : data(0) {}
UnionFind(int sz){ data.assign(sz, -1); }
/* merge the set to which x belongs and the set to which y belongs */
bool unite(int x, int y){
x = find(x); y = find(y);
if (x == y) return false;
if (data[x] > data[y]) std::swap(x, y);
data[x] += data[y];
data[y] = x;
return true;
}
/* find the root of k */
int find(int k){
if (data[k] < 0) return k;
return data[k] = find(data[k]);
}
/* calculate the size of the set to which k belongs */
int size(int k){ return -data[find(k)]; }
};
using edge = std::pair<int, int>;
using graph = std::vector<edge>;
int N, M;
std::vector<int> a, b;
graph g;
void input(void){
std::cin >> N >> M;
a.resize(N);
for (int& ai : a)
std::cin >> ai;
b.resize(N);
for (int& bi : b)
std::cin >> bi;
g.resize(M);
for (edge& e : g){
std::cin >> e.first >> e.second;
--e.first, --e.second;
}
}
bool solve(void){
UnionFind uf(N);
for (edge& e : g)
(void)uf.unite(e.first, e.second);
std::vector<long long> connectedSum_a(N, 0LL), connectedSum_b(N, 0LL);
for (int i = 0; i < N; ++i){
int idx = uf.find(i);
connectedSum_a[idx] += a[i];
connectedSum_b[idx] += b[i];
}
return connectedSum_a == connectedSum_b;
}
int main(void){
input();
std::cout << (solve() ? "Yes" : "No") << std::endl;
return 0;
} | #include "bits/stdc++.h"
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
class union_find {
private:
vector<int> par;
vector<int> ran;
vector<ll> size;
public:
union_find(int n);
int find(int x);
void unite(int x, int y);
bool same(int x, int y);
ll show_size(int x);
};
union_find::union_find(int n){
par.resize(n);
ran.resize(n);
size.resize(n);
rep(i,n) par[i] = i;
rep(i,n) ran[i] = 0;
rep(i,n) size[i] = 1;
};
int union_find::find(int x) {
if(par[x] == x) return x;
else return par[x] = find(par[x]);
};
void union_find::unite(int x, int y) {
x = find(x);
y = find(y);
if(x == y) return;
ll t = show_size(x)+show_size(y);
size[x] = size[y] = t;
if(ran[x] < ran[y]){
par[x] = y;
//size[y] += size[x];
}
else{
par[y] = x;
//size[x] += size[y];
if(ran[x] == ran[y]) ran[x]++;
}
};
ll union_find::show_size(int x) {
if(par[x] == x) return size[x];
return show_size(par[x] = find(par[x]));
}
bool union_find::same(int x, int y) {
return (find(x) == find(y));
};
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int n, m;
cin >> n >> m;
vector<ll> a(n), b(n);
rep(i,n) cin >> a[i];
rep(i,n) cin >> b[i];
union_find uf(n);
rep(i,m){
int c, d;
cin >> c >> d;
--c; --d;
uf.unite(c, d);
}
vector<ll> sumA(n, 0), sumB(n, 0);
rep(i,n){
int p = uf.find(i);
sumA[p] += a[i];
sumB[p] += b[i];
}
rep(i,n){
if(sumA[i] != sumB[i]){
cout << "No" << endl;
return 0;
}
}
cout << "Yes" << endl;
return 0;
} |
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);++i)
typedef long long ll;
using namespace std;
using P=pair<int,int>;
vector<vector<bool>> dp(101,vector<bool>(100001,false));
int main()
{
int n;
cin>>n;
vector<int> t(n);
rep(i,n) cin>>t[i];
int sum=0;
rep(i,n) sum+=t[i];
dp[0][0]=true;
for(int i=0;i<n;i++){
for(int j=0;j<=sum;j++){
if(dp[i][j]){
dp[i+1][j+t[i]]=true;
dp[i+1][j]=true;
}
}
}
int ans=sum;
for(int j=0;j<=sum;j++){
if(dp[n][j]) ans=min(ans,max(j,sum-j));
}
cout<<ans<<endl;
return 0;
} | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#include <functional> // for less
#include <iostream>
using namespace __gnu_pbds;
using namespace std;
#define int long long
#define mod 1000000007
typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
typedef tree<int, null_type,
less_equal<int>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_multiset;
int ans;
int gosolve(int p,vector<int> &v)
{
//cout<<p<<" p\n";
int ors=0;
int xors=0;
ors=v[0];
vector<int> d;
d.push_back(v[0]);
for(int i=1;i<v.size();i++)
{
if(p&1)
{//cout<<" a\n";
int f=d.back();
f=f|v[i];
d[d.size()-1]=f;
}
else
{d.push_back(v[i]);
//cout<<" b\n";
}
//cout<<i<<" "<<d.size()<<" pi\n";
p=p>>1;
}
for(auto i: d)
{//cout<<i<<" ";
xors=(xors^i);}
//cout<<xors<<" xors\n";
return xors;
}
void go(vector<int> &v)
{
ans=-1;
int d=v.size()-1;
int k=1;
for(int i=1;i<=d;i++)
k=k*2;
//cout<<k<<" k\n";
for(int i=0;i<k;i++)
{
int f=gosolve(i,v);
if(i==0)
ans=f;
else
ans=min(ans,f);
}
cout<<ans<<endl;
return;
}
void solve()
{//ans=INT_MAX;
int n;
cin>>n;
vector<int> v;
for(int i=0;i<n;i++)
{int d;
cin>>d;
v.push_back(d);
}
if(n==1)
{cout<<v.back()<<endl;
return;}
go(v);
//cout<<ans<<endl;
return;
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("inp.txt", "r", stdin);
freopen("m3.txt", "w", stdout);
#endif
int t;
t=1;
for(int i=1;i<=t;i++)
{
solve();
//cout<<endl;
}
return (int)(0);
} |
#include <bits/stdc++.h>
#define int long long
using namespace std;
struct R {
double x, y;
R(double _x, double _y) {
x = _x;
y = _y;
}
R() {}
};
struct Line {
double A, B, C;
Line (R a, R b) {
A = b.y - a.y;
B = a.x - b.x;
C = a.y * b.x-a.x * b.y;
}
Line (double _A, double _B, double _C) {
A = _A;
B = _B;
C = _C;
}
double slope() {
assert(B != 0);
return -A/B;
}
};
signed main() {
ios::sync_with_stdio(0);cin.tie(0);
int n;
cin >> n;
vector<R> p(n);
int answer = 0;
for (int i = 0; i < n; ++i) cin >> p[i].x >> p[i].y;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
Line cur(p[i], p[j]);
if (abs(cur.slope()) <= 1) ++answer;
}
}
cout << answer;
} | #include<bits/stdc++.h>
#define IMAX 1<<29
#define PI 3.14159265359
using namespace std;
template <class T>
using Graph = vector<vector<T>>;
/******** Utility ***************/
int bsize(int bits, int _max){
int c = 0;
for(int ix=0; ix!=_max; ++ix)
if((bits&(1<<ix)) !=0){
++c;
}
return c;
}
long factrial(long n){
assert(n >= 0);
if(n <= 1)
return 1;
long _sum = 1;
for(int ix=0; ix!=(n-1); ++ix)
_sum *= n-ix;
return _sum;
}
template<typename T>
void show(vector<T> v)
{
for(auto _v: v)
cout << _v << " ";
cout << endl;
}
template<typename T>
void show(vector<T> v, int maxrow) // ้ฉๅบฆใซๆน่กๅ
ฅใใver
{
for(int ix=0; ix!=v.size(); ++ix){
if(ix!=0 && ix%maxrow==0)
cout << endl;
cout << v[ix] << " ";
}
cout << endl;
}
template<typename T>
void show2(vector<vector<T>> v)
{
int ix = 0;
for(auto __v: v){
cout << "[" << ix++ << "] (";
for(auto _v: __v){
cout << _v << ", ";
}
cout << ")" << endl;
}
}
// G: Graph
// v: ๆฌกใซ่จชใใ้ ็น
// d: ๆๅใซ้ ็นใ่ฆใคใใใฟใคใ ในใฟใณใ(่กใใใ้ )
// -1ใงๅๆๅใใใฆใใๅฟ
่ฆใใใ
// f: ใใ้ ็นใฎ้ฃๆฅใชในใใๆข็ดขใ็ตใใๆๅปใฎใฟใคใ ในใฟใณใ(ๅธฐใใใ้ )
// -1ใงๅๆๅใใใฆใใๅฟ
่ฆใใใ
// timestamp: ใฟใคใ ในใฟใณใ
// @return: vใฎ้ฃๆฅ้ ็นใฎๆข็ดขใซ่ฆใใๆ้
template<typename T>
void dfs(const Graph<T> &G, int v, vector<T> &d, vector<T> &f, T ×tamp)
{
d[v] = timestamp++;
for(auto next_v: G[v]){ // ้ฃๆฅใใ้ ็นใๆข็ดข
if(d[next_v] != -1){ // ๆขใซๆข็ดขๆธใฟ
continue;
}else{
dfs(G, next_v, d, f, timestamp); // ๆฌกใฎ้ ็นใDFS
}
}
f[v] = timestamp++;
}
// G: Graph
// dist: startใใใฎๆๅฐ่ท้ข
// start: startๅฐ็น
template<typename T>
void bfs(const Graph<T> &G, vector<int> &dist, const int start)
{
// init
queue<int> que;
dist.assign(dist.size(), -1);
dist[start] = 0;
que.push(start);
// BFS
while(!que.empty()){
int v = que.front();
que.pop();
for(auto next_v: G[v]){
if(dist[next_v] != -1) // ่จชๅๆธใฟ
continue;
dist[next_v] = dist[v] + 1;
que.push(next_v);
}
}
}
// ใฐใฉใใฎๅจใใซไฝ็ฝใใคใใฆใใใใจๅๆ
void genGraph(Graph<int> &G, vector<string> &board, int R, int C)
{
for(int row=1; row!=R-1; ++row){
for(int col=1; col!=C-1; ++col){
if(board[row][col] == '#')
continue;
if(board[row-1][col] == '.')
G[(row-1)*C + col].push_back(row*C + col);
if(board[row][col-1] == '.')
G[(row)*C + col-1].push_back(row*C + col);
if(board[row][col+1] == '.')
G[(row)*C + col+1].push_back(row*C + col);
if(board[row+1][col] == '.')
G[(row+1)*C + col].push_back(row*C + col);
}
}
}
/******** end of Utility ***************/
int main(void)
{
// input
int N;
cin >> N;
vector<double> x = vector<double>(N);
vector<double> y = vector<double>(N);
for(int ix=0; ix!=N; ++ix){
cin >> x[ix] >> y[ix];
}
// main
long count = 0;
for(int ix=0; ix<N-1; ++ix){
for(int jx=ix+1; jx<N; ++jx){
if(abs((y[jx]-y[ix])/(x[jx]-x[ix])) <= 1){
++count;
}
}
}
// print
cout << count << endl;
} |
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include <functional>
#include <iomanip>
#include<cmath>
#include<deque>
#include <math.h>
#include <queue>
#include <sstream>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define PI 3.14159265359
using namespace std;
using ll = long long;
int GetDigit(ll num) {
int a = 0;
while (num) {
num /= 10;
a++;
}
return a;
}
ll Get_keta_num(ll num) {
ll z = 0;
while (num) {
z += num % 10;
num /= 10;
}
return z;
}
void print(set<int> s) {
for (set<int>::iterator it = s.begin(); it != s.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
ll count_num_2(ll num) {
ll data = 0;
while (true) {
ll m = num % 1000;
ll k = num % 100;
if ((m % 8) == 0) {
data += 3;
num /= 8;
continue;
}
else if ((k % 4) == 0) {
data += 2;
num /= 4;
continue;
}
else if ((k % 2) == 0) {
data++;
num /= 2;
continue;
}
break;
}
return data;
}
ll facctorialMethod(int k) {
ll sum = 1;
for (int i = 1; i <= k; ++i)
{
sum *= i;
}
return sum;
}
//" "
ll Get_num_in_string(string s) {
int n = s.length(), z;
ll sur = 0;
for (int i = 0; i < n; i++) {
z = int(s[i] - '0');
if (z < 10) {
sur *= 10;
sur += z;
}
}
return sur;
}
ll ll_pow(int num, int zyousuu) {
ll ans = 1;
for (int i = 0; i < zyousuu; i++) {
ans *= num;
}
return ans;
}
string substrBack(std::string str, size_t pos, size_t len) {
const size_t strLen = str.length();
return str.substr(strLen - pos, len);
}
ll ll_gcd(ll x, ll y) {
if (x < y) {
ll a = x;
x = y;
y = a;
}
while (y > 0) {
ll r = x % y;
x = y;
y = r;
}
return x;
}
using Graph = vector<vector<int>>;
ll ll_lcm(ll a, ll b) {
return a * b / ll_gcd(a, b);
}
string get_words_from_end(string word, int num) {
return word.substr(word.size() - num, num);
}
string binary(ll bina) {
string ans = "";
if (bina == 0) {
ans = "0";
return ans;
}
if (bina == 1) {
ans = "1";
return ans;
}
while (bina != 0) {
int a = bina % 2;
char c = '0' + a;
ans += c;
bina -= a;
bina /= 2;
}
reverse(ans.begin(), ans.end());
return ans;
}
void YES() {
cout << "YES" << endl;
}
void NO() {
cout << "NO" << endl;
}
void Yes() {
cout << "Yes" << endl;
}
void No() {
cout << "No" << endl;
}
ll comb(ll N, ll i) {
vector<vector<long long int> > v(N + 1, vector<long long int>(N + 1, 0));
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]);
}
}
return v[N][i];
}
bool is_prime(long long N) {
if (N == 1) return false;
for (long long i = 2; i * i <= N; ++i) {
if (N % i == 0) return false;
}
return true;
}
bool divisor(long long n) {
vector<long long> ret;
for (long long i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n) ret.push_back(n / i);
}
}
sort(ret.begin(), ret.end()); // ๆ้ ใซไธฆในใ
bool x = false;
if (ret.size() == 8)x = true;
return x;
}
vector<bool> seen(100005, false);
vector<int> ans;
int count = 0;
void dfs(const Graph& G, int v) {
seen[v] = true; // v ใ่จชๅๆธใซใใ
ans.push_back(v + 1);
// v ใใ่กใใๅ้ ็น next_v ใซใคใใฆ
for (auto next_v : G[v]) {
if (seen[next_v]) continue; // next_v ใๆข็ดขๆธใ ใฃใใในใซใผ
dfs(G, next_v); // ๅๅธฐ็ใซๆข็ดข
}
}
void cout_data(ll x) {
cout << x << endl;
}
int main() {
int A, B; cin >> A >> B;
int ans = 1;
for (int i = 2; i <= B;i++) {
int a_1 = (A + i - 1) / i;
int b_1 = B / i;
if (a_1 > b_1)continue;
if ((a_1 == 0) || (b_1 == 0))continue;
if (a_1 != b_1) {
if ((a_1 * i >= A) && (b_1 * i <= B)) {
if (a_1 * i < b_1 * i)ans = i;
}
}
}
cout << ans << endl;
return 0;
}
//std::cout << std::setprecision(2) << 3.141; // "3.1" | //#define _GLIBCXX_DEBUG
//#include "atcoder/all"
//using namespace atcoder;
#include <bits/stdc++.h>
#define int long long
#define ll long long
using ull = unsigned long long;
using namespace std;
#define Dump(x) \
if (dbg) { \
cerr << #x << " = " << (x) << endl; \
}
#define overload4(_1, _2, _3, _4, name, ...) name
#define FOR1(n) for (ll i = 0; i < (n); ++i)
#define FOR2(i, n) for (ll i = 0; i < (n); ++i)
#define FOR3(i, a, b) for (ll i = (a); i < (b); ++i)
#define FOR4(i, a, b, c) for (ll i = (a); i < (b); i += (c))
#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FORR(i, a, b) for (int i = (a); i <= (b); ++i)
#define bit(n, k) (((n) >> (k)) & 1) /*nใฎk bit็ฎ*/
namespace mydef {
const int INF = 1ll << 60;
const int MOD = 1e9 + 7;
template <class T>
bool chmin(T& a, const T& b) {
if (a > b) {
a = b;
return 1;
} else
return 0;
}
template <class T>
bool chmax(T& a, const T& b) {
if (a < b) {
a = b;
return 1;
} else
return 0;
}
void Yes(bool flag = true) {
if (flag)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
void No(bool flag = true) {
Yes(!flag);
}
void YES(bool flag = true) {
if (flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
void NO(bool flag = true) {
YES(!flag);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T& val) {
std::fill((T*)array, (T*)(array + N), val);
}
bool dbg = true;
} // namespace mydef
using namespace mydef;
#define pb push_back
//#define mp make_pair
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(), (v).end()
#define SZ(x) ((int)(x).size())
#define vi vector<int>
#define vvi vector<vector<int>>
#define vp vector<pair<int, int>>
#define vvp vector<vector<pair<int, int>>>
#define pi pair<int, int>
//#define P pair<int, int>
//#define V vector<int>
//#define S set<int>
#define asn ans
int A, B;
void solve() {
int ans = 1;
for (int x = 1; x <= 200020; x++) {
if (((A - 1) / x + 2 <= B / x))
chmax(ans, x);
}
cout << ans << endl;
}
signed main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cin >> A >> B;
solve();
return 0;
}
|
#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;
struct Unionfind{
int n;
vector<int> parents;
Unionfind(int n):n(n),parents(n,-1){ }
int find(int x){
if(parents[x] < 0) return x;
return parents[x] = find(parents[x]);
}
void unite(int x,int y){
x = find(x);
y = find(y);
if(x==y) return;
if(parents[x] > parents[y]) swap(x,y);
parents[x] += parents[y];
parents[y] = x;
}
int size(int x){
return -parents[find(x)];
}
bool same(int x,int y){
return find(x)==find(y);
}
vector<int> members(int x){
int root = find(x);
vector<int> member;
for(int i=0;i<n;i++){
if(find(i)==root ){
member.push_back(i);
}
}
return member;
}
int group_cnt(){
int c = 0;
for(int i=0;i<n;i++){
if(parents[i] < 0) ++c;
}
return c;
}
};
int main(){
int n,m;
cin >> n >> m;
vector<vector<int>> G(n);
Unionfind uf(n);
rep(i,m){
int a,b;
cin >> a >> b;
--a;--b;
G[a].push_back(b);
G[b].push_back(a);
uf.unite(a,b);
}
vector<int> seen(n,-1);
auto dfs = [&](auto&& dfs,int i,vector<int>& v) -> void {
v.push_back(i);
seen[i] = 1;
for(int j:G[i]){
if(seen[j] >= 0) continue;
dfs(dfs,j,v);
}
return;
};
auto dfs2 = [&](auto&& dfs2,vector<int>& v,vector<int>& color) -> ll {
for(int j:v){
if(color[j] == -1){
ll res = 0;
vector<int> use(3,0);
for(int k:G[j]){
if(color[k] == -1) continue;
use[color[k]] ++;
}
rep(p,3){
if(use[p]==0){
color[j] = p;
res += dfs2(dfs2,v,color);
}
}
color[j] = -1;
return res;
}
}
return 1;
};
ll res = 1;
rep(i,n){
if(uf.find(i) == i){
vector<int> v;
dfs(dfs,i,v);
vector<int> color(n,-1);
color[v[0]] = 0;
res *= 3*dfs2(dfs2,v,color);
}
}
cout << res << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int n, m;
vector<vector<int>> adj;
long long ans=1;
long long comp_ans[25];
int color[100];
vector<vector<int>> component;
int visited[25] = {};
void discover(int vertex, int cid=-1) {
if (cid==-1) {
cid = component.size();
component.push_back(vector<int>(0));
}
visited[vertex] = true;
component[cid].push_back(vertex);
for (int nxt : adj[vertex]) {
if (not visited[nxt]) {
discover(nxt, cid);
}
}
}
void tryColor(int vid, const int& cid) {
if (vid >= component[cid].size()) {
comp_ans[cid]++;
// for (int i=0; i<n; i++) cout << color[i] << ' ';
return;
}
int vertex = component[cid][vid];
for (int cl=1; cl<4; cl++) {
int flag = 1;
for (int neighbor : adj[vertex]) {
if (color[neighbor]==cl) flag = 0;
}
if (flag) {
color[vertex] = cl;
tryColor(vid+1, cid);
color[vertex] = 0;
}
}
}
int main() {
cin >> n >> m;
adj.resize(n);
for (int i=0; i<m; i++) {
int x, y;
cin >> x >> y;
x--; y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
for (int i=0; i<n; i++) {
if (visited[i] == 0) discover(i);
}
for (int cid=0; cid<component.size(); cid++) {
tryColor(0, cid);
ans *= comp_ans[cid];
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
#define int long long
#define ri register int
using namespace std;
int n,m,ans,s[2];
char ch[25];
template <typename T> inline void read(T &x) {
register T f=0,c=getchar();
for (; c<48||57<c; c=getchar()) if (c=='-') f=1;
for (x=0; 48<=c&&c<=57; c=getchar()) x=(x<<3)+(x<<1)+(c&15);
if (f) x=~(--x);
}
template <typename T> inline void print(T x) {
if (x<0) putchar('-'),x=~(--x);
if (x>9) print(x/10);
putchar(x%10|48);
}
signed main() {
read(n),read(m);
for (ri i=1,cnt; i<=n; ++i) {
scanf("%s",ch),cnt=0;
for (ri j=0; j<m; ++j)
if (ch[j]=='1') ++cnt;
ans+=s[cnt&1^1],++s[cnt&1];
}
print(ans);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vpii> vvpii;
typedef vector<vpll> vvpll;
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define fi first
#define se second
template<class T> bool ckmin(T &a, const T &b) {return a > b ? a = b, 1 : 0;}
template<class T> bool ckmax(T &a, const T &b) {return a < b ? a = b, 1 : 0;}
namespace debug {
void __print(int x) {cerr << x;}
void __print(long long 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 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 z : x) cerr << (f++ ? "," : ""), __print(z); 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 ljuba
#define dbg(x...) cerr << "LINE(" << __LINE__ << ") -> " << "[" << #x << "] = ["; _print(x)
#else
#define dbg(x...)
#endif
}
using namespace debug;
const char nl = '\n';
void solve() {
int n, m;
cin >> n >> m;
vll cnt(2);
for(int i = 0; i < n; ++i) {
string s;
cin >> s;
++cnt[count(all(s), '1')%2];
}
cout << cnt[0] * cnt[1] << nl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifdef ljuba
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
int testCases = 1;
// cin >> testCases;
while(testCases--)
solve();
} |
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define rep2(i,k,n) for (int i = k; i < (n); ++i)
#define ll unsigned long long
using namespace std;
using P = pair<int,int>;
int ceil(int x, int d) {
return (x+d-1) / d;
}
void solve() {
int n, m;
cin >> n >> m;
vector<int> A;
A.push_back(0);
rep(i,m) {
int a;
cin >> a;
A.push_back(a);
}
A.push_back(n+1);
sort(A.begin(), A.end());
int hanko = n;
vector<int> haba;
rep(i,A.size()-1) {
int w = A[i+1] - A[i] - 1;
if (w == 0) continue;
else {
hanko = min(hanko, w);
haba.push_back(w);
}
}
int ans = 0;
for (int x : haba) {
ans += ceil(x, hanko);
}
cout << ans << endl;
}
int main() {
solve();
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define ll long long int
const ll inf=pow(2,31)-1;
int main()
{
int n,m;cin>>n>>m;
int k=99999999,ans=0,a[m+5];a[0]=0;
for(int i=1;i<=m;i++) cin>>a[i];
sort(a+1,a+1+m);
a[m+1]=n+1;
for(int i=1;i<=m+1;i++)
{
if(a[i]-a[i-1]>1)
{
k=min(k,a[i]-a[i-1]-1);
}
}
//cout<<k<<endl;
for(int i=1;i<=m+1;i++)
{
int t=a[i]-a[i-1]-1;
ans+=t/k;
if(t%k) ans++;
}
if(m==0)
{
cout<<1<<endl;
return 0;
}
cout<<ans<<endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#ifdef _DEBUG
#define show(x) \
cerr << #x << " : "; \
showVal(x)
template <typename T>
void showVal(const T &a) { cerr << a << endl; }
template <typename T>
void showVal(const vector<T> &a) {
for (const T &v : a) cerr << v << " ";
cerr << endl;
}
template <typename T>
void showVal(const vector<vector<T>> &a) {
cerr << endl;
for (const vector<T> &v : a) showVal(v);
}
#else
#define show(x)
#endif
int main() {
int n, m;
cin >> n >> m;
vector<int> x(m), y(m), z(m);
for (int i = 0; i < m; i++) {
cin >> x[i] >> y[i] >> z[i];
}
vector<ll> dp(1 << n, 0);
// ๅทฆใใ่ฆใฆใใงใซbitๆฑบใพใฃใฆใใ
dp[0] = 1;
for (int bit = 0; bit < 1 << n; bit++) {
vector<bool> isEx(n, false);
for (int i = 0; i < n; i++) {
if ((bit >> i) & 1) isEx[i] = true;
}
for (int i = 0; i < n; i++) {
if (!isEx[i]) {
vector<int> now;
for (int j = n - 1; j >= 0; j--) {
if (isEx[j] || j == i) now.push_back(j + 1);
}
bool isOK = true;
for (int j = 0; j < m; j++) {
int cnt = 0;
for (int k = 0; k < min(x[j], (int)now.size()); k++) {
if (now[k] <= y[j]) cnt++;
}
if (cnt > z[j]) {
isOK = false;
break;
}
}
if (isOK) {
dp[bit | (1 << i)] += dp[bit];
}
}
}
}
show(dp);
cout << dp[(1 << n) - 1] << endl;
return 0;
} | #include<bits/stdc++.h>
#define ll long long
#define ld long double
#define ff first
#define ss second
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define all(x) begin(x), end(x)
#define endl "\n"
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, -1, 1};
ll dp[100005];
ll mod = 998244353;
bool vis[100000];
using namespace std;
bool isPrime(ll n) {
ll j;
for ( j = 2; j <= sqrt(n); j++) {
if (n % j == 0) {
break;
}
}
if (j > sqrt(n)) {
return 1;
}
else {
return 0;
}
}
ll modexpo(ll a, ll b) {
ll ans = 1;
a = a % mod;
while (b > 0) {
if ((b & 1) == 1) {
ans = ((ans % mod) * (a % mod)) % mod;
}
b = b >> 1;
a = ((a % mod) * (a % mod)) % mod;
}
return ans;
}
ll invmod(ll n) {
return modexpo(n, mod - 2);
}
ll comb(ll n, ll r) {
if (r == 0) {
return 1;
}
ll fact[n + 1];
fact[0] = 1;
for (ll i = 1; i <= n; i++) {
fact[i] = (fact[i - 1] * i) % mod;
}
return (fact[n] * invmod(fact[r]) % mod * invmod(fact[n - r]) % mod) % mod;
}
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);
}
void dfs(ll i, vector<ll>adj[], bool visited[]) {
visited[i] = 1;
for (auto j : adj[i]) {
if (visited[j] == 0) {
dfs(j, adj, visited);
}
}
}
bool sortbysecdesc(const pair<int, int> &a, const pair<int, int> &b)
{
return a.second > b.second;
}
void solve(ll k) {
ll n,m,ti;
cin>>n>>m>>ti;
ll n1=n;
bool ok=0;
ll pr=0;
while(m--){
ll a,b;
cin>>a>>b;
n-=(a-pr);
if(n<=0){
ok=1;
}
n+=b-a;
n=min(n,n1);
pr=b;
}
n-=(ti-pr);
if(n<=0){
ok=1;
}
if(ok==0){
cout<<"Yes";
}
else{
cout<<"No";
}
}
int main() {
#ifndef ONLINE_JUDGE
// for getting input from input.txt
freopen("input.txt", "r", stdin);
// for writing output to output.txt
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t = 1;
// cin >> t;
ll k = 1;
while (k <= t) {
solve(k);
k++;
}
return 0;
} |
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <bitset>
#include <map>
#include <iomanip>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <algorithm>
#include <sstream>
#include <unordered_set>
using namespace std;
#define ll long long
#define vecll vector<long long>
#define vec2ll vector<vector<long long>>
#define vecdouble vector<double>
#define vecchar vector<char>
#define vecstr vector<string>
#define vec2str vector<vector<string>>
#define vecbool vector<bool>
#define vec2bool vector<vector<bool>>
#define pairll pair<ll,ll>
#define vecpairll vector<pair<long long,long long>>
#define vec2pairll vector<vector<pair<long long,long long>>>
#define forll(s,a,b) for(long long s = a;s < b;s++)
#define forllde(s,a) for(long long s = a;s > 0;s--)
#define forbit(s,a) for(int s = 0; s < (1 << a); s++)
#define INF 1000000000000
#define Pi 3.1415926535897932384626
ll gcd(ll a, ll b)
{
if (min(a, b) == 0)
return max(a, b);
return gcd(min(a, b), max(a, b) % min(a, b));
}
ll lcm(ll a, ll b)
{
ll buf = gcd(a, b);
return abs(a * b) / buf;
}
string toBaseNum(ll n, ll base)
{
string s;
while (n > 0)
{
s = to_string(n % base) + s;
n /= base;
}
return s;
}
int main() {
ll n, m,k, ans = 0;
string s = "No";
bool flag = false;
cin >> n;
vecll a(n);
forll(i, 0, a.size())
cin >> a[i];
vecpairll num(n);
forll(i, 0, n)
{
num[i] = { a[i],1 };
forll(j, i+1, n)
{
if (a[j] < num[i].first)
{
ans = max(ans, num[i].first * num[i].second);
num[i].first = a[j];
}
num[i].second++;
}
ans = max(ans, num[i].first * num[i].second);
}
cout << ans << "\n";
} | #include<bits/stdc++.h>
using namespace std;
int main()
{
int n,ans=0;
cin>>n;
int a[n+1];
for(int i=1;i<=n;i++)
cin>>a[i];
for(int l=1;l<=n;l++)
{
int x=a[l];
for(int r=l;r<=n;r++){
x=min(x,a[r]);
ans=max(ans,x*(r-l+1));}
}
cout<<ans;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define Mod(x) (x>=P)&&(x-=P)
#define rep(i,a,b) for(int i=a,i##end=b;i<=i##end;++i)
#define drep(i,a,b) for(int i=a,i##end=b;i>=i##end;--i)
#define erep(i,a) for(int i=hd[a];i;i=nxt[i])
typedef long long ll;
void Max(int &x,int y){(x<y)&&(x=y);}
void Min(int &x,int y){(x>y)&&(x=y);}
bool vio;
char IO;
int rd(int res=0){
bool f=0;
while(IO=getchar(),IO<48||IO>57)
f=IO=='-';
do res=(res<<1)+(res<<3)+(IO^48);
while(IO=getchar(),isdigit(IO));
return f?-res:res;
}
const int M=1e6+10;
int nxt[M<<1],to[M<<1],hd[M],ecnt;
void Add(int a,int b){
nxt[++ecnt]=hd[a],to[hd[a]=ecnt]=b;
}
int Mx,tp,bot,L[M],R[M],n;
void dfs(int x,int f,int d){
static int tot;
if(d>Mx)tp=x,Mx=d;
L[x]=++tot;
erep(i,x){
int y=to[i];
if(y==f)continue;
dfs(y,x,d+1);
}
R[x]=tot;
}
bool FA(int a,int b){
return L[a]<=L[b]&&L[b]<=R[a];
}
int nw=1,ans[M];
void redfs(int x,int f){
ans[x]=nw;
erep(i,x){
int y=to[i];
if(y==f||FA(y,tp))continue;
nw++,redfs(y,x);
}
erep(i,x){
int y=to[i];
if(y!=f&&FA(y,tp))nw++,redfs(y,x);
}
nw++;
}
bool let;
int main(){
cerr<<(&vio-&let)/1024.0/1024<<endl;
rep(i,2,n=rd()){
int a=rd(),b=rd();
Add(a,b),Add(b,a);
}
Mx=0,dfs(1,0,0);
bot=tp,Mx=0,dfs(tp,0,0);
ans[1]=1;
redfs(bot,0);
rep(i,1,n)printf("%d ",ans[i]);
}
| #include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <algorithm>
#include <numeric>
#include <utility>
#include <random>
#include <chrono>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
using PII = pair<int, int>;
using PIL = pair<int, LL>;
using PLL = pair<LL, LL>;
// const int mod = 1e9 + 7;
// const int mod = 998244353;
inline void quickread() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int n, x, y;
vector<vector<int>> edges;
int p = 0, pdist = 0;
bool path[200010];
int ans[200010];
int val = 1;
void dfs(int u, int fa, int dist) {
if (dist > pdist) {
p = u;
pdist = dist;
}
for (int v: edges[u]) {
if (v != fa) {
dfs(v, u, dist + 1);
}
}
}
bool dfs2(int u, int fa) {
if (u == p) {
return path[u] = true;
}
for (int v: edges[u]) {
if (v != fa) {
dfs2(v, u);
path[u] |= path[v];
}
}
return path[u];
}
void dfs3(int u, int fa) {
ans[u] = val;
for (int v: edges[u]) {
if (v != fa && !path[v]) {
++val;
dfs3(v, u);
++val;
}
}
for (int v: edges[u]) {
if (v != fa && path[v]) {
++val;
dfs3(v, u);
}
}
}
inline void work() {
cin >> n;
edges.resize(n + 1);
for (int i = 1; i < n; ++i) {
cin >> x >> y;
edges[x].push_back(y);
edges[y].push_back(x);
}
// u-v is the diameter
dfs(1, 0, 0);
int u = p;
pdist = 0;
dfs(p, 0, 0);
int v = p;
dfs2(u, 0);
dfs3(u, 0);
for (int i = 1; i <= n; ++i) {
cout << ans[i] << "\n";
}
}
int main() {
// freopen("input.txt", "r", stdin);
quickread();
work();
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
#define rep(i,n) for(int i=0;i<int(n);i++)
#define rep1(i,n) for(int i=1;i<=int(n);i++)
#define per(i,n) for(int i=int(n)-1;i>=0;i--)
#define per1(i,n) for(int i=int(n);i>0;i--)
#define all(c) c.begin(),c.end()
#define si(x) int(x.size())
#define pb emplace_back
#define fs first
#define sc second
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
template<class T,class U> void chmax(T& x, U y){if(x<y) x=y;}
template<class T,class U> void chmin(T& x, U y){if(y<x) x=y;}
template<class T> void mkuni(V<T>& v){sort(all(v));v.erase(unique(all(v)),v.end());}
template<class T> int lwb(const V<T>& v, const T& a){return lower_bound(all(v),a) - v.begin();}
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
o<<"{";
for(const T& v:vc) o<<v<<",";
o<<"}";
return o;
}
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }
#ifdef LOCAL
#define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
void dmpr(ostream& os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
os<<t<<" ~ ";
dmpr(os,args...);
}
#define shows(...) cerr << "LINE" << __LINE__ << " : ";dmpr(cerr,##__VA_ARGS__)
#define dump(x) cerr << "LINE" << __LINE__ << " : " << #x << " = {"; \
for(auto v: x) cerr << v << ","; cerr << "}" << endl;
#else
#define show(x) void(0)
#define dump(x) void(0)
#define shows(...) void(0)
#endif
struct BIT{
using D = int;
int N;
vector<D> bit;
BIT(int n){ //nใฏๅบ้ๅน
!!!
N=n;
bit.assign(n+1,0);
}
BIT(){}
void init(int n){
N=n;
bit.assign(n+1,0);
}
D sum(int i){ //iๅใฎๅa[0]+..a[i-1]
D s=0;
while(i>0){
s+=bit[i];
i-=(i&-i);
}
return s;
}
D val(int i){ //return a[i] (0-ind)
return sum(i+1)-sum(i);
}
void add(int i,D x){ //a[i]+=x (0-indexed)
i++;
while(i<=N){
bit[i]+=x;
i+=(i&-i);
}
}
};
int main(){
cin.tie(0);
ios::sync_with_stdio(false); //DON'T USE scanf/printf/puts !!
cout << fixed << setprecision(20);
auto f = [&](int H,int W,VV<int> x2ys,VV<int> y2xs){
V<int> res(H);
y2xs[0].pb(H);
rep(i,y2xs[0][0]){
res[i] = (x2ys[i].empty() ? W : x2ys[i][0]);
}
return res;
};
int H,W,N; cin >> H >> W >> N;
VV<int> x2ys(H),y2xs(W);
rep(i,N){
int x,y;cin>>x>>y;x--,y--;
x2ys[x].pb(y);y2xs[y].pb(x);
}
rep(i,H) sort(all(x2ys[i]));
rep(i,W) sort(all(y2xs[i]));
auto a = f(H,W,x2ys,y2xs);
auto b = f(W,H,y2xs,x2ys);
show(a);show(b);
VV<int> h2bs(H);
rep(i,W){
if(b[i]) h2bs[b[i]-1].pb(i);
}
ll ans = 0;
BIT bit(W);
per(i,H){
for(int j: h2bs[i]) bit.add(j,1);
ans += a[i] + bit.sum(W) - bit.sum(a[i]);
}
cout << ans << endl;
} | #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็ฎ
#define vec(T) vector<T>
#define vvec(T) vector<vector<T>>
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;
//---20210207---------------------------------------------
struct Solver{
struct path{
int to,cost;
path(int to=0, int cost=0):to(to),cost(cost){}
};
void solve(){
int N,M;
cin >> N>>M;
vvec(path) r(N);
vec(int) selfr(N,iINF);
rep(i,M){
int a,b,c;
cin >> a >> b >> c;
a--;b--;
if(a==b) chmin(selfr[a],c);
else r[a].emplace_back(b,c);
}
vvec(int) d(N,vec(int)(N,iINF));
rep(st,N){
d[st][st]=0;
priority_queue<P,vector<P>,greater<P>> q;
q.emplace(0,st);
while(q.size()){
int c0 = q.top().first;
int now = q.top().second;
q.pop();
if(d[st][now]<c0) continue;
for(path t:r[now]){
int c1 = c0+t.cost;
if( d[st][t.to] <= c1 ) continue;
d[st][t.to] = c1;
q.emplace(c1, t.to);
}
}
}
rep(st,N){
int ans = selfr[st];
rep(gl,N){
if(st==gl) continue;
chmin(ans, d[st][gl]+d[gl][st] );
}
if(ans==iINF) ans = -1;
cout << ans << endl;
}
}
};
int main(){
int testcasenum=1;
//cin >> testcasenum;
rep1(ti,testcasenum){
Solver solver;
solver.solve();
}
return 0;
}
//---20210206------------------------------------------
/*
struct Solver{
struct path{
int to,c;
path(int to=0, int c=0):to(to),c(c){}
};
vvec(path) r = vvec(path)(2021);
vec(int) used = vec(int)(2021);
int res;
int start;
void dfs(int now, int c){
for(auto t:r[now]){
if(used[t.to]) continue;
if(t.to==start){
chmin(res,c+t.c);
continue;
}
used[t.to]=1;
dfs(t.to, c+t.c);
}
used[now]=0;
return;
}
void solve(){
int N,M;
cin >> N>>M;
rep(i,M){
int a,b,c;
cin >> a >> b >> c;
r[a].emplace_back(b,c);
}
rep1(st,N){
rep(i,2021) used[i]=0;
res = iINF;
start = st;
dfs(st,0);
if(res==iINF) cout << -1 << endl;
else cout << res << endl;
}
}
};
int main(){
int testcasenum=1;
//cin >> testcasenum;
rep1(ti,testcasenum){
Solver solver;
solver.solve();
}
return 0;
}
*/ |
#include<bits/stdc++.h>
using namespace std;
const int N=1E5+5;
typedef long long ll;
int n,m;
ll a[N],ans[N];
struct hzy{int id;long long x;}q[N];
bool mycmp(hzy a,hzy b){return a.x<b.x;}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
}
for(int i=1;i<=m;i++){
scanf("%lld",&q[i].x);
q[i].id=i;
}
sort(q+1,q+m+1,mycmp);
int t=0;
for(int i=1;i<=m;i++){
q[i].x+=t;
while(t<n&&q[i].x>=a[t+1]){
t++;
q[i].x++;
}
ans[q[i].id]=q[i].x;
}
for(int i=1;i<=m;i++)printf("%lld\n",ans[i]);
} | #include<bits/stdc++.h>
using namespace std;
#define FASTIO ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define ll long long
#define mset(A,val) memset(A,val,sizeof(A))
#define fi(a,b) for(int i=a;i<=b;++i)
#define fj(a,b) for(int j=a;j<=b;++j)
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define pii pair<int,int>
#define int long long
// ---------------------------------------------------------------------------
const int mod = 1e9+7;
const int maxn = 2e5 + 9;
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void test_case(int tc)
{
// cout<<"Case #"<<tc<<": ";
int n,w;cin>>n>>w;
int hash[maxn+100];
mset(hash,0);
fi(1,n){
int l,r,c;cin>>l>>r>>c;
l++,r++;
hash[r]-=c;
hash[l]+=c;
}
fi(1,n+9){
hash[i]+=hash[i-1];
if(hash[i]>w){
cout<<"No";
return;
}
}
cout<<"Yes";
}
int32_t main()
{
FASTIO;
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int t=1;
//cin>>t;
for(int tc=1;tc<=t;++tc)test_case(tc);
} |
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization ("Ofast")
// #pragma GCC optimization ("unroll-loops")
#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;
// template <typename T>
// using o_set = tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// #define sfind(i) find_by_order(i)
// #define ord(i) order_of_key(i)
typedef long long ll;
typedef pair<int , int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
using cd = complex<double>;
const double pi = acos(-1);
#define mod 1000000007
#define rep(i, a, b) for(ll i = a; i < b; i++)
#define repc(i, a, b, c) for (ll i = a; (c > 0 ? i < b : i > b) and c != 0; i += c)
#define vi vector<int>
#define vl vector<ll>
#define _max(vec) (*max_element(all(vec)))
#define _min(vec) (*min_element(all(vec)))
#define fi first
#define si second
#define pb push_back
#define all(name) name.begin(), name.end()
#define ral(name) name.rbegin(), name.rend()
#define fact(n) rep(i, 1, n+1)ft.pb((ft[i-1]*i)%mod)
#define mod_in(a) power(a, mod - 2)
#define ncr(n, r) (n>=r?((ft[n]*mod_in((ft[r]*ft[(n)-(r)])%mod))%mod):0LL)
#define deb(x) cerr << "[" << #x << " " << x << "]" << endl;
#define endl '\n' //....????
#define int ll
vector<ll> ft(1, 1LL);
/*======================================================================*/
void solve(){
int x, y, a, b, ans = 0;
cin >> x >> y >> a >> b;
while(x < (y/a + (y%a!=0)) and b > (a-1)*x)x *= a, ans++;
ans += (y-x-1)/b;
cout << ans;
}
signed main() {
cin.sync_with_stdio(0); cin.tie(0);
cin.exceptions(cin.failbit);
int t = 1;
// cin >> t;
while(t--){
solve();
}
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP(i, n) for(int (i) = 0; (i) < (n); ++(i))
#define REPR(i, n) for(int (i) = (n); (i) >= 0; --(i))
#define FOR(i, n, m) for(int (i) = (n); (i) < (m); ++(i))
constexpr int INF = 1e9;
constexpr ll INFL = 1LL<<61;
constexpr ll mod = 1e9+7;
int main(){
ll X, Y, A, B;
cin >> X >> Y >> A >> B;
Y--;
ll ans = 0;
for(ll i = 0; i < 65; i++){
ll diff = Y - X;
if(diff < 0) break;
ll ex = i + diff / B;
ans = max(ans, ex);
ll before = X;
X *= A;
if(X <= 0) break;
if(Y < X) break;
if(X / A != before) break;
if(X / before != A) break;
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
// type alias
typedef long long LL;
typedef pair<int,int> II;
typedef tuple<int,int,int> III;
typedef vector<int> VI;
typedef vector<string> VS;
typedef unordered_map<int,int> MAPII;
typedef unordered_set<int> SETI;
template<class T> using VV=vector<vector<T>>;
// minmax
template<class T> inline T SMIN(T& a, const T b) { return a=(a>b)?b:a; }
template<class T> inline T SMAX(T& a, const T b) { return a=(a<b)?b:a; }
// repetition
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)
#define REPE(i,n) for(int i=0;i<=(n);++i)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) for(int i=0;i<(n);++i)
#define FORR(x,arr) for(auto& x:arr)
#define SZ(a) int((a).size())
// collection
#define ALL(c) (c).begin(),(c).end()
// DP
#define MINUS(dp) memset(dp, -1, sizeof(dp))
#define ZERO(dp) memset(dp, 0, sizeof(dp))
// stdout
#define println(args...) fprintf(stdout, ##args),putchar('\n');
// debug cerr
template<class Iter> void __kumaerrc(Iter begin, Iter end) { for(; begin!=end; ++begin) { cerr<<*begin<<','; } cerr<<endl; }
void __kumaerr(istream_iterator<string> it) { (void)it; cerr<<endl; }
template<typename T, typename... Args> void __kumaerr(istream_iterator<string> it, T a, Args... args) { cerr<<*it<<"="<<a<<", ",__kumaerr(++it, args...); }
template<typename S, typename T> std::ostream& operator<<(std::ostream& _os, const std::pair<S,T>& _p) { return _os<<"{"<<_p.first<<','<<_p.second<<"}"; }
#define __KUMATRACE__ true
#ifdef __KUMATRACE__
#define dump(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); __kumaerr(_it, args); }
#define dumpc(ar) { cerr<<#ar<<": "; FORR(x,(ar)) { cerr << x << ','; } cerr << endl; }
#define dumpC(beg,end) { cerr<<"~"<<#end<<": "; __kumaerrc(beg,end); }
#else
#define dump(args...)
#define dumpc(ar)
#define dumpC(beg,end)
#endif
// $ cp-batch FlipDigits | diff FlipDigits.out -
// $ g++ -std=c++14 -Wall -O2 -D_GLIBCXX_DEBUG -fsanitize=address FlipDigits.cpp && ./a.out
/*
11/15/2020
7:17-7:30, 8:40-8:55 WA
10:00 WA
11:00 give up
*/
string S,T;
int N;
void nope() {
cout<<-1<<endl;
exit(0);
}
void solve() {
VI ss,ts;
REP(i,N) {
if(S[i]=='1') ss.push_back(i);
if(T[i]=='1') ts.push_back(i);
}
//dumpc(ss);
//dumpc(ts);
if(SZ(ts)>SZ(ss)) nope();
LL res=0;
int i=0;
FORR(ti,ts) {
if(i==SZ(ss)) nope();
//dump(i,ss[i],ti,res);
while(ss[i]<ti) {
if(i+1>=SZ(ss)) nope();
res+=ss[i+1]-ss[i],i+=2;
}
if(i==SZ(ss)) nope();
assert(ss[i]>=ti);
res+=ss[i]-ti,i+=1;
}
if((SZ(ss)-i)%2) nope();
while(i<SZ(ss)) {
res+=ss[i+1]-ss[i],i+=2;
}
cout<<res<<endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout<<setprecision(12)<<fixed;
cin>>N>>S>>T;
solve();
return 0;
}
| #include <bits/stdc++.h>
#include <ctime>
using namespace std;
#define rep(i,n) for(int i = 0; i<n; ++i)
#define REP(i,n) for(int i = 1; i<=n; ++i)
#define all(x) begin(x),end(x)
#define show(obj) for(auto x:obj)cout<<x<<' ';cout<<endl;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<ll,ll> lp;
typedef pair<double, double> FP;
const int inf = 1001001000;
const ll INF = 1LL<<60;
const int MOD = (int)1e9 + 7;
void update(int id, int asp);
struct d1{
int x, y, r;
};
struct d2{
int a, b, c, d;
};
const int EDGE = 10000;
const double tlimit = 3.0000;
int n;
vector<d1> req(200);
vector<d2> ans(200);
vector<vector<bool>> nap(10000, vector<bool>(10000, false));
int main(){
clock_t stime = clock();
random_device seed;
mt19937_64 gen(seed());
cin >> n;
rep(i,n){
cin >> req[i].x >> req[i].y >> req[i].r;
ans[i].a = req[i].x;
ans[i].b = req[i].y;
ans[i].c = req[i].x + 1;
ans[i].d = req[i].y + 1;
nap[req[i].x][req[i].y] = true;
}
while(((clock() - stime) / CLOCKS_PER_SEC) <= tlimit){
int id = gen() % n;
int asp = gen() % 4;
update(id, asp);
}
rep(i,n)printf("%d %d %d %d\n", ans[i].a, ans[i].b, ans[i].c, ans[i].d);
return 0;
}
void update(int id, int asp){
int a = ans[id].a;
int b = ans[id].b;
int c = ans[id].c;
int d = ans[id].d;
bool ok = true;
switch(asp) {
case 0:
if(a == 0)break;
for(int p = b; p < d; ++p)if(nap[a-1][p])ok = false;
if(ok){
for(int p = b; p < d; ++p)nap[a-1][p] = true;
ans[id].a--;
}
break;
case 1:
if(b == 0)break;
for(int p = a; p < c; ++p)if(nap[p][b-1])ok = false;
if(ok){
for(int p = a; p < c; ++p)nap[p][b-1] = true;
ans[id].b--;
}
break;
case 2:
if(c == EDGE)break;
for(int p = b; p < d; ++p)if(nap[c][p])ok = false;
if(ok){
for(int p = b; p < d; ++p)nap[c][p] = true;
ans[id].c++;
}
break;
case 3:
if(d == EDGE)break;
for(int p = a; p < c; ++p)if(nap[p][d])ok = false;
if(ok){
for(int p = a; p < c; ++p)nap[p][d] = true;
ans[id].d++;
}
break;
}
}
|
#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define pii pair<int, int>
#define endl '\n'
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define vvl vector<vl>
#define vld vector<ld>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vpii vector<pii>
#define vvpii vector<vpii>
#define vpll vector<pll>
#define rep(s, l, r) for (int s = l; s < r; s++)
#define per(s, r, l) for (int s = r - 1; s >= l; s--)
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef long double ld;
using namespace std;
template<class T> using minheap = priority_queue<T, vector<T>, greater<T>>;
template<typename T> void setmax(T& a, T b) { a = max(a, b); };
template<typename T> void setmin(T& a, T b) { a = min(a, b); };
template<typename T> bool in(T lo, T v, T hi) { return lo <= v && v <= hi; };
template<ll MOD>
struct ModInt {
ll x = 0;
ModInt() { x = 0; }
ModInt(ll _x) {
x = _x;
if (x >= MOD || x <= -MOD) x %= MOD;
if (x < 0) x += MOD;
}
friend ModInt operator+(ModInt a, ModInt b) {
return ModInt(a.x + b.x);
}
friend ModInt operator-(ModInt a, ModInt b) {
return ModInt(a.x - b.x);
}
friend ModInt operator*(ModInt a, ModInt b) {
return ModInt(a.x * b.x);
}
friend ModInt operator/(ModInt a, ModInt b) {
return a * inv(b);
}
friend ModInt power(ModInt a, ll b) {
if (b == 0) return 1;
ModInt c = power(a, b / 2);
return c * c * ((b & 1) ? a : 1);
}
friend ModInt inv(ModInt a) {
// this only works for nonzero elements
return power(a, MOD - 2);
}
friend ostream& operator<<(ostream &os, const ModInt a) {
os << a.x;
return os;
}
};
using mint = ModInt<1000000007>;
using vm = vector<mint>;
using vvm = vector<vm>;
struct Solution {
vl get(vl x) {
int n = x.size();
vl res(1 << n);
rep(mask, 0, (1 << n)) {
rep(i, 0, n) if (mask >> i & 1) res[mask] += x[i];
}
sort(all(res));
res.resize(unique(all(res)) - res.begin());
return res;
}
void solve() {
int n;
cin >> n;
ll t;
cin >> t;
vl a(n);
rep(i, 0, n) cin >> a[i];
if (n == 1) {
int res = a[0] < t ? a[0] : 0;
cout << res << endl;
return;
}
int h = n / 2;
vl x, y;
rep(i, 0, n) {
if (i < h) x.pb(a[i]);
else y.pb(a[i]);
}
vl u = get(x);
vl v = get(y);
sort(all(u));
sort(all(v));
ll best = 0;
for (ll tt : u) {
if (tt > t) break;
setmax(best, tt);
ll rem = t - tt;
auto it = upper_bound(all(v), rem);
--it;
setmax(best, tt + *it);
}
for (ll tt : v) if (tt <= t) setmax(best, tt);
cout << best << endl;
}
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
while (t--) {
Solution s;
s.solve();
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define sz(v) (int)v.size()
#define all(v) v.begin(), v.end()
void dbg_out() { cerr << "\b\b]\n"; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T){ cerr << H << ", "; dbg_out(T...);}
#define watch(...) cerr << "[" << #__VA_ARGS__ << "]: [", dbg_out(__VA_ARGS__)
/****************************** CODE IS HERE ***********************************/
int main(){
ios_base::sync_with_stdio(false); cin.tie(nullptr);
int n, m; cin >> n >> m;
vector <int> A(n);
for (int &i: A) cin >> i;
int k1 = (n + 1) / 2, k2 = n / 2;
vector <ll> X, Y;
for (int i = 0; i < (1 << k1); ++i) {
ll sum = 0;
for (int j = 0; j < k1; ++j) {
if (i >> j & 1) {
sum += A[j];
}
}
if (sum <= m) {
X.push_back(sum);
}
}
for (int i = 0; i < (1 << k2); ++i) {
ll sum = 0;
for (int j = 0; j < k2; ++j) {
if (i >> j & 1) {
sum += A[j + k1];
}
}
if (sum <= m) {
Y.push_back(sum);
}
}
sort(all(Y));
ll ans = 0;
for (ll i: X) {
int pos = lower_bound(all(Y), m - i) - Y.begin();
if (pos >= sz(Y) or Y[pos] + i > m) pos--;
ans = max(ans, i + Y[pos]);
}
cout << ans;
return 0;
}
/*
--------- Meet in the middle Technique ---------------
Like brute force but here we divide the original array into two parts and apply the brute forces individually and merge them.
*/ |
//YashS
#include <bits/stdc++.h>
using namespace std;
#define test int T;cin>>T;while(T--)
#define int long long
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)((x).size())
#define fr first
#define sc second
#define minf LONG_LONG_MIN
#define inf LONG_LONG_MAX
#define pii pair<int,int>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define mem1(x, arr) memset((arr),x,sizeof((arr))
#define pVec(v) for(auto e:v)cout<<e<<" ";cout<<"\n"
const int MOD = 1e9 + 7;
const int N = 1e5 + 7;
void init_code() {
#ifndef ONLINE_JUDGE
freopen("inputf.txt", "r", stdin);
freopen("outputf.txt", "w", stdout);
#endif // ONLINE_JUDGE
}
void yash()
{
int n;
cin >> n;
vector<int> a(n), b(n), c(n), freq(n + 1);
for (int &x : a)cin >> x;
for (int &x : b)cin >> x;
for (int &x : c) {cin >> x; x = b[x - 1]; freq[x]++;}
int ans = 0;
for (int x : a)ans += freq[x];
cout << ans << "\n";
}
signed main()
{
init_code();
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//test
yash();
return 0;
} | #include <bits/stdc++.h>
// #include <atcoder/all>
// #include "icld.cpp"
using namespace std;
using ll = long long int;
using pii = pair<int,int>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vector<int>>;
using ss = string;
using db = double;
const int dx[4] = {1,0,-1,0};
const int dy[4] = {0,1,0,-1};
//#define pi 3.14159265358979
#define rep(i,s,n) for(int i=(s);i<(int)(n);i++)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define ci(x) cin >> x
#define cii(x) int x;cin >> x
#define cci(x,y) int x,y;cin >> x >> y
#define co(x) cout << x << endl
int main(){
cci(h,w);
cci(n,m);
vvi v(h+2,vi(w+2,-1));
rep(i,1,h+1)rep(j,1,w+1)v[i][j]=0;
vector<pii> el(n);
rep(i,0,n){
cci(x,y);
el[i]=make_pair(x,y);
}
rep(i,0,m){
cci(x,y);
v[x][y]=-1;
}
vvi vx=v,vy=v;
rep(i,0,n){
int x,y;
tie(x,y)=el[i];
int nx=x,ny=y;
while(v[--nx][y]!=-1);
while(v[x][--ny]!=-1);
vx[nx+1][y]=1;
vy[x][ny+1]=1;
}
rep(i,1,h+1){
int now=0;
rep(j,1,w+1){
int y=vy[i][j];
if(y==1)now=1;
else if(y==-1)now=0;
if(now)v[i][j]=1;
}
}
rep(i,1,w+1){
int now=0;
rep(j,1,h+1){
int x=vx[j][i];
if(x==1)now=1;
else if(x==-1)now=0;
if(now)v[j][i]=1;
}
}
int ans=0;
rep(i,1,h+1)rep(j,1,w+1)ans+=(v[i][j]==1);
co(ans);
} |
#include <iostream>
using namespace std;
int main()
{
long long n, k;
cin >> n >> k;
for(int i=1; i<=k; ++i)
{
if(n%200 == 0)
n/=200;
else
{
n *= 1000;
n += 200;
}
}
cout << n << endl;
return 0;
} | #include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define REP(i,j) for(int i=0;i<j;i++)
#define REPA(i,j) for(int i=1;i<=j;i++)
#define FORN(i,j,k) for(int i=j;i<k;i++)
#define vi vector<int>
#define vvi vector<vi >
#define pii pair<int,int>
#define vpii vector<pii >
#define all(a) a.begin(),a.end()
using namespace std;
const int INF=1<<30;
//const ll INF=1ll<<60;
const ll MOD=1e9+7;
inline void read(int &x){
// short neg=1;
x=0;
char c=getchar();
/*while(c<'0'||c>'9'){
if(c=='-')neg=-1;
c=getchar();
}*/
while(c>='0'&&c<='9'){
x=(x<<3)+(x<<1)+(c^48),c=getchar();
}
// x*=neg;
}
ll quick_mod(ll A,ll B){//A^B
ll ret=1;
A%=MOD;
while(B)
{
if(B&1)ret=ret*A%MOD;
B>>=1;
A=A*A%MOD;
}
return ret;
}
ll N;
int k;
int main(void){
cin>>N;
cin>>k;
while(k--){
if(N%200==0){
N/=200;
}
else{
N=N*1000+200;
}
}
cout<<N;
return 0;
}
|
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=2e5+10;
map<int,int>ma;
int n,x[maxn],b[205];
int main () {
cin>>n;
ll ans=0;
for(int i=0;i<200;i++) b[i]=0;
for(int i=0;i<n;i++) {
cin>>x[i];
int tmp=x[i]%200;
ans+=b[tmp];
b[tmp]++;
}
cout<<ans<<endl;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vll = vector<ll>;
using vvll = vector<vll>;
template <typename T> constexpr int inof(const T x) {
return static_cast<int>(x);
}
template <typename T> constexpr ll llof(const T x) {
return static_cast<ll>(x);
}
template <typename T> constexpr double doof(const T x) {
return static_cast<double>(x);
}
template <typename T> constexpr int sz(const T &xs) { return inof(xs.size()); }
ll pairs_num(const vi &xs) {
vi fs(200, 0);
ll ans = 0;
for (const int x : xs) {
ans += fs[x % 200];
++fs[x % 200];
}
return ans;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
cout << setprecision(9) << fixed;
int n;
cin >> n;
vi xs(n);
for (auto &x : xs) cin >> x;
cout << pairs_num(xs) << '\n';
return 0;
}
|
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <cstdio>
#include <set>
#include <map>
#include <list>
#include <cstdlib>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define MOD 1000000007
#define PI 3.1415926535897932
#define INF 1e9
#define rep(i, n) for (int i = 0; i < n; i++)
#define repe(i, j, n) for (int i = j; i < n; i++)
#define repi(i, n) for (int i = 0; i <= n; i++)
#define repie(i, j, n) for (int i = j; i <= n; i++)
#define all(x) x.begin(), x.end()
#define println() cout << endl
#define P pair<int, int>
#define fi first
#define se second
typedef long long ll;
using Graph = vector<vector<ll>>;
long long modinv(long long a, long long m)
{
long long b = m, u = 1, v = 0;
while (b)
{
long long t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
u %= m;
if (u < 0)
u += m;
return u;
}
ll dp[2000][2000];
ll nCr(ll n, ll r)
{
if(n==r) return dp[n][r] = 1;
if(r==0) return dp[n][r] = 1;
if(r==1) return dp[n][r] = n%MOD;
if(dp[n][r]) return dp[n][r]%MOD;
return dp[n][r] = nCr(n-1,r)%MOD + nCr(n-1,r-1)%MOD;
}
ll H(ll n, ll r) {
return nCr(n+r-1, r)%MOD;
}
int prime[10000000];
bool is_prime[100000000 + 1];
int sieve(int n) {
int pcnt = 0;
for(int i = 0; i <= n; i++) {
is_prime[i] = true;
}
is_prime[0] = is_prime[1] = false;
for(int i = 2; i <= n; i++) {
if(is_prime[i]) {
prime[pcnt++] = i;
for(int j = 2*i; j <= n; j += i) {
is_prime[j] = false;
}
}
}
return pcnt;
}
struct UnionFind {
//่ช่บซใ่ฆชใงใใใฐใใใฎ้ๅใซๅฑใใ้ ็นๆฐใซ-1ใๆใใใใฎ
//ใใใงใชใใใฐ่ฆชใฎid
vector<ll> r;
UnionFind(ll N) {
r = vector<ll>(N, -1);
}
ll root(ll x) {
if (r[x] < 0) return x;
return r[x] = root(r[x]);
}
bool unite(ll x, ll y) {
x = root(x);
y = root(y);
if (x == y) return false;
if (r[x] > r[y]) swap(x, y);
r[x] += r[y];
r[y] = x;
return true;
}
ll size(ll x) {
return -r[root(x)];
}
bool same(ll x, ll y) { // 2ใคใฎใใผใฟx, yใๅฑใใๆจใๅใใชใtrueใ่ฟใ
ll rx = root(x);
ll ry = root(y);
return rx == ry;
}
};
template< typename T >
struct BinaryIndexedTree {
vector< T > data;
BinaryIndexedTree(int sz) {
data.assign(++sz, 0);
}
T sum(int k) {
T ret = 0;
for(++k; k > 0; k -= k & -k) ret += data[k];
return (ret);
}
void add(int k, T x) {
for(++k; k < data.size(); k += k & -k) data[k] += x;
}
};
void solve1() {
int n; cin >> n;
BinaryIndexedTree<ll> f_tree(n+1);
vector<int> a(n);
rep(i, n) {
cin >> a[i];
}
ll ans = 0;
for (int j = 0; j < n; j++) {
ans += j - f_tree.sum(a[j]);
f_tree.add(a[j], 1);
}
for(int i = 0; i < n; i++) {
cout << ans << endl;
ans += n - 1-2*a[i];
}
}
int main()
{
solve1();
}
| #include<bits/stdc++.h>
using namespace std;
const int N=3e5+5;
int n,a[N],tr[N];
long long ans;
void add(int x)
{
for (;x<=n;x+=x&-x) tr[x]++;
}
int get(int x)
{
int l=0;
for (;x;x-=x&-x) l+=tr[x];
return l;
}
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
a[i]++;
ans+=i-1-get(a[i]);
add(a[i]);
}
printf("%lld\n",ans);
for (int i=1;i<n;i++)
{
ans+=n-2*a[i]+1;
printf("%lld\n",ans);
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
const int N=5005,mod=998244353;
int n,m,ans,f[N][2],g[N][2];
inline int add(int a,int b){
a+=b;
return a<mod?a:a-mod;
}
inline int dec(int a,int b){
a-=b;
return a<0?a+mod:a;
}
inline int mul(int a,int b){
return 1LL*a*b%mod;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
memset(f,0,sizeof(f));
memset(g,0,sizeof(f));
f[0][0]=1;
g[0][0]=0;
for(int j=1;j<=n;j++){
f[j][0]=add(mul(f[j-1][0],m-1),mul(f[j-1][1],i-1));
g[j][0]=add(mul(g[j-1][0],m-1),mul(g[j-1][1],i-1));
f[j][1]=add(f[j-1][0],mul(f[j-1][1],m-i+1));
if(j==n){
j++;
j--;
}
g[j][1]=add(g[j-1][0],f[j-1][0]);
g[j][1]=add(g[j][1],mul(g[j-1][1],m-i+1));
}
ans=add(ans,add(g[n][0],g[n][1]));
}
printf("%d\n",ans);
return 0;
} | #include <stdio.h>
long long p = 998244353;
long long n, m;
long long d[5001];
long long r[5001];
long long inv[5001];
void makeinv(void) {
long long arraynumber = sizeof inv / sizeof inv[0];
inv[1] = 1;
for (int invcount = 2; invcount < arraynumber; invcount++) {
inv[invcount] = p - inv[p % invcount] * (p / invcount) % p;
}
}
int main() {
makeinv();
scanf("%lld%lld", &n, &m);
d[1] = m;
for (int i = 0; i <= 5000; i++)r[i] = 1;
for (long long i = 2; i <= n; i++) {
d[i] = d[i - 1] * m % p;
for (long long j = 0; j <= 5000; j++)r[j] = r[j] * j % p;
for (long long j = 1; j <= m; j++) {
d[i] += p - ((r[m] - r[m - j]) * inv[j]) % p;
}
d[i] += r[m] * m;
d[i] %= p;
}
printf("%lld\n", d[n]);
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
string X;
ll M;
bool solve(ll mid) {
ll res = 0;
ll multiply = 1;
for(ll i = X.length() - 1; i >= 0; i--) {
ll x = (X.at(i) - '0');
if(multiply > (1e18 / x)) {
return false;
}
if(res > 1e18 - x * multiply) {
return false;
}
res += x * multiply;
if(i != 0) {
if(multiply > 1e18 / mid) {
return false;
}
multiply *= mid;
}
}
return (res <= M);
}
int main() {
cin >> X;
cin >> M;
if(X.length() == 1) {
ll num = X.at(0) - '0';
if(num <= M) {
cout << 1 << endl;
return 0;
}
cout << 0 << endl;
return 0;
}
ll d = 0;
for(const char &c : X) {
d = max(d, (ll)(c - '0'));
}
ll ok = d;
ll ng = (long double)(1e18) + 1;
while(abs(ok - ng) > 1) {
ll mid = (ok + ng) / 2;
if(solve(mid)) {
ok = mid;
} else {
ng = mid;
}
}
cout << ok - d << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define int long long
signed main()
{
int s,p,i;
cin>>s>>p;
for(i=1;i*i<=p;i++)
if(i*(s-i)==p)
{
cout<<"Yes";
return 0;
}
cout<<"No";
return 0;
} |
/*input
3
aba
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long,null_type,less_equal<long long>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
#pragma GCC optimize("O2","unroll-loops","no-stack-protector")
//order_of_key #of elements less than x
// find_by_order kth element
using ll=long long;
using ld=long double;
using pii=pair<ld,ld>;
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
#define sz(x) (int)x.size()
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()),c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
const ll INF64=1e18+1;
const int INF=0x3f3f3f3f;
const ll MOD=1e9+7;
const ld PI=acos(-1);
const ld eps=1e-3;
#define lowb(x) x&(-x)
#define MNTO(x,y) x=min(x,(__typeof__(x))y)
#define MXTO(x,y) x=max(x,(__typeof__(x))y)
inline ll mult(ll a,ll b){
if(a>=MOD) a%=MOD;
if(b>=MOD) b%=MOD;
return (a*b)%MOD;
}
inline ll mypow(ll a,ll b){
if(b<=0) return 1;
ll res=1LL;
while(b){
if(b&1) res=mult(res,a);
a=mult(a,a);
b>>=1;
}
return res;
}
const int maxn=1e4+5;
const int maxlg=__lg(maxn)+2;
int32_t main(){
ios::sync_with_stdio(false),cin.tie(0);
int n;
string s;
cin>>n>>s;
if(s[0]!=s[n-1]) cout<<1;
else{
REP(i,n-1){
if(s[i]!=s[0] and s[i+1]!=s[0]){
cout<<2;
return 0;
}
}
cout<<-1;
}
} | #include<bits/stdc++.h>
int main(){
using namespace std;
unsigned long N;
cin >> N;
string S;
cin >> S;
if(S[0] != S.back())return 0 & puts("1");
for(unsigned long i{1}; i < N; ++i)if(S[0] != S[i - 1] && S[i] != S.back())return 0 & puts("2");
puts("-1");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
#define sz(a) (ll)a.size()
#define deb(x) cerr<<#x<<' '<<x<<endl;
const long long mod = 1000000007;
void _a_b_c_d_e_f_g_h_i_j_k_l_m_n_o_p_q_r_s_t_u_v_w_x_y_z()
{
ll n, k, q = 0, c = 0;
string s;
cin >> n;
ll i = 0;
while (++i)
if (stoll(to_string(i) + to_string(i)) > n)
return void(cout << i - 1);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
// cin >> t; // Test Case :-||-: //
// factorial();
// SOE();
while (t--)
{
_a_b_c_d_e_f_g_h_i_j_k_l_m_n_o_p_q_r_s_t_u_v_w_x_y_z();
}
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for(int i=a;i<b;i++)
int main(){
int n;
cin>>n;
bool flag=true;
int i;
unordered_map<int,int>dic;
REP(i,0,n){
int ele;
cin>>ele;
if(ele>=1&&ele<=n){
if(dic[ele]>=1)
flag=false;
dic[ele]++;
}
else flag=false;
}
string ans="Yes";
if(!flag)ans="No";
cout<<ans<<endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int gcd(int a,int b) {return b==0?a:gcd(b,a%b);}
const int maxn = 1000005;
int prime[maxn],np[maxn],mu[maxn],tot=0;
void init(int n) {
np[1]=mu[1]=1;
for(int i=2;i<=n;++i) {
if(!np[i]) prime[++tot]=i,mu[i]=-1;
for(int j=1;j<=tot;++j) {
if(i*prime[j]>n) break;
if(i%prime[j]==0) {
np[i*prime[j]]=1;
mu[i*prime[j]]=0;
break;
}
np[i*prime[j]]=1;
mu[i*prime[j]]=-mu[i];
}
}
}
int main() {
int l, r;
cin >> l >> r;
init(r);
auto solve = [&](int a, int b) -> ll {
// [1, a] [1, b]
// sum_{d=1}^{max(a,b)} \mu(d) (a/d) * (b/d)
ll ret = 0;
for(int d = 1; d <= a; ++d) {
if(mu[d]) ret += 1ll * mu[d] * (a / d) * (b / d);
}
return ret;
};
ll ans = 0;
for(int g = 2; g <= r; ++g) {
int a = max(2, (l + g - 1) / g), b = r / g;
if(a > b) break;
ll tmp = solve(b, b) + solve(a - 1, a - 1) - 2 * solve(a - 1, b);
// a <= l, r <= b
// l = r = 1
ans += tmp / 2 * 2;
}
cout << ans << '\n';
} | #include<bits/stdc++.h>
#define _USE_MATH_DEFINES
using namespace std;
#define ll long long int
#define pb push_back
#define eb emplace_back
#define rep(i , j , n) for(ll i = j ; i < n ; i++)
#define pre(i , j , n) for(ll i = j ; i >= n ; i--)
#define all(x) x.begin(), x.end()
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef pair<ll,ll> pll;
typedef vector<vector<ll>> vvll;
#define br "\n"
#define ff first
#define ss second
#define debug(a...) cout<<#a<<": ";for(auto it:a)cout<<it<<" ";cout<<endl;
#define MAXIM 1000101
const ll MAX = 2e5 + 1;
ll mod = 1e9 + 7;
ll n;
ll cal(vll &v,ll m){
ll co = 0;
ll ans = 0;
rep(i,0,n){
co |= v[i];
if(((1LL<<i)&m)){
(ans ^= co);
co = 0;
}
}
(ans ^= co);
return ans;
}
void solve(){
cin >> n;
vll v(n);
ll ans = 0;
rep(i,0,n){
cin >> v[i];
ans |= v[i];
}
rep(i,0,(1<<n)){
ans = min(cal(v,i),ans);
}
cout << ans << br;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
ll t = 1;
// cin >> t;
rep(i,0,t){
// cout << "Case #" << i + 1 << ": ";
solve();
// test();
}
} |
#include <bits/stdc++.h>
using namespace std;
int main(){
int x,y;
cin >> x >> y;
if(x == y)
cout << x << endl;
else
cout << 3-(x+y) << endl;
return 0;
} | #include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int x, y;
cin >> x >> y;
if (x == y)
cout << x;
if (x != y) {
if (x != 1 && y != 1)
cout << 1;
else
if (x != 2 && y != 2)
cout << 2;
else
if (x != 0 && y != 0)
cout << 0;
}
return 0;
} |
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>
static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;
template<class T> constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208;
int main() {
int n, m;
cin >> n >> m;
vector<int> a(m), b(m);
for (int i = 0; i < m; ++i) {
scanf("%d %d", &a[i], &b[i]);
a[i]--; b[i]--;
}
vector<int> c(n);
for (auto &&i : c) scanf("%d", &i);
vector<vector<pair<int, int>>> G(n);
vector<int> ans(m, -1);
for (int i = 0; i < m; ++i) {
if(c[a[i]] == c[b[i]]){
G[a[i]].emplace_back(b[i], 2*i);
G[b[i]].emplace_back(a[i], 2*i+1);
}else {
ans[i] = c[a[i]] < c[b[i]];
}
}
vector<int> used(m);
vector<int> visited(n), p(n, -1);
auto dfs = [&](int x, int p, auto &&f) -> void {
visited[x] = 1;
for (auto &[y, id] : G[x]) {
if(p == y || used[id/2]) continue;
ans[id/2] = id%2;
used[id/2] = 1;
if(!visited[y]){
visited[y] = 1;
f(y, x, f);
}
}
};
for (int i = 0; i < n; ++i) {
if(!visited[i]) dfs(i, -1, dfs);
}
for (int i = 0; i < m; ++i) {
if(!ans[i]) printf("->\n");
else printf("<-\n");
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int X[111], Y[111], fa[111];
int ffa(int x) {
if (x == fa[x]) return x;
return fa[x] = ffa(fa[x]);
}
void merge(int x, int y) {
x = ffa(x);
y = ffa(y);
fa[x] = y;
}
struct NODE {
int a, b;
double d;
NODE(int a1 = 0, int a2 = 0, double a3 = 0) : a(a1), b(a2), d(a3) {}
bool operator<(const NODE &P) const {
return d < P.d;
}
};
int n;
int main() {
#ifdef LOCAL
freopen(".a.in", "r", stdin);
#endif
ios_base::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; ++i) cin >> X[i] >> Y[i];
int S = n, T = n + 1;
for (int i = 0; i <= T; ++i) fa[i] = i;
vector<NODE> F;
F.emplace_back(S, T, 200);
for (int i = 0; i < n; ++i) {
F.emplace_back(i, S, abs(Y[i] - 100));
F.emplace_back(i, T, abs(Y[i] + 100));
}
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
F.emplace_back(i, j, sqrt((X[i] - X[j]) * (X[i] - X[j]) + (Y[i] - Y[j]) * (Y[i] - Y[j])));
}
}
sort(F.begin(), F.end());
for (int i = 0; i < (int)F.size(); ++i) {
merge(F[i].a, F[i].b);
if (ffa(S) == ffa(T)) {
cout << fixed << setprecision(10) << F[i].d / 2.0 << "\n";
return 0;
}
}
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<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;
//#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 10007;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acosl(-1.0);
ll mod_pow(ll x, ll n, ll m = mod) {
if (n < 0) {
ll res = mod_pow(x, -n, m);
return mod_pow(res, m - 2, m);
}
if (abs(x) >= m)x %= m;
if (x < 0)x += m;
ll res = 1;
while (n) {
if (n & 1)res = res * x % m;
x = x * x % m; n >>= 1;
}
return res;
}
struct modint {
ll n;
modint() :n(0) { ; }
modint(ll m) :n(m) {
if (n >= mod)n %= mod;
else if (n < 0)n = (n % mod + mod) % mod;
}
operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
if (n == 0)return modint(1);
modint res = (a * a) ^ (n / 2);
if (n % 2)res = res * a;
return res;
}
ll inv(ll a, ll p) {
return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 1;
modint fact[max_n], factinv[max_n];
void init_f() {
fact[0] = modint(1);
for (int i = 0; i < max_n - 1; i++) {
fact[i + 1] = fact[i] * modint(i + 1);
}
factinv[max_n - 1] = modint(1) / fact[max_n - 1];
for (int i = max_n - 2; i >= 0; i--) {
factinv[i] = factinv[i + 1] * modint(i + 1);
}
}
modint comb(int a, int b) {
if (a < 0 || b < 0 || a < b)return 0;
return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
if (a < 0 || b < 0 || a < b)return 0;
return fact[a] * factinv[a - b];
}
void solve() {
int n; cin >> n;
int ans = (n + 99) / 100;
cout << ans << "\n";
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
//cout << fixed << setprecision(10);
//init_f();
//init();
//expr();
//int t; cin >> t;rep(i,t)
//while(cin>>n,n)
//solve();
solve();
return 0;
} | #include<iostream>
#include<algorithm>
using namespace std;
long long n,a[200010],b[200010],p[200010],k,ans,m;
long long f[200010];
int main(){
cin >> n;
for(int i=1;i<=n;i++)
{
cin >>a[i];
p[i]=max(a[i],p[i-1]);}
f[1]=2*a[1];b[1]=f[1];
for(int i=2;i<=n;i++)
{
f[i]=a[i]+i*(p[i]-p[i-1])+b[i-1]+f[i-1];
b[i]=p[i]-p[i-1]+b[i-1]+a[i];
}
for(int i=1;i<=n;i++)
cout << f[i]<<endl;
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 <atcoder/all>
// #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;
// using namespace atcoder;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> p32;
typedef pair<ll, ll> p64;
typedef pair<p64, p64> pp64;
typedef pair<double, double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int>> vv32;
typedef vector<vector<ll>> vv64;
typedef vector<vector<p64>> vvp64;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
typedef pair<ll, p64> tp;
ll MOD = 998244353;
double eps = 1e-12;
#define forn(i, e) for (ll i = 0; i < e; i++)
#define forsn(i, s, e) for (ll i = s; i < e; i++)
#define rforn(i, s) for (ll i = s; i >= 0; i--)
#define rforsn(i, s, e) for (ll i = s; i >= e; i--)
#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())
#define zero ll(0)
#define set_bits(x) __builtin_popcountll(x)
// #define mint modint998244353
ll mpow(ll a, ll b)
{
if (a == 0)
return 0;
if (b == 0)
return 1;
ll t1 = mpow(a, b / 2);
t1 *= t1;
t1 %= MOD;
if (b % 2)
t1 *= a;
t1 %= MOD;
return t1;
}
ll mpow(ll a, ll b, ll p)
{
if (a == 0)
return 0;
if (b == 0)
return 1;
ll t1 = mpow(a, b / 2, p);
t1 *= t1;
t1 %= p;
if (b % 2)
t1 *= a;
t1 %= p;
return t1;
}
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;
}
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
ll range(ll l, ll r)
{
return l + mt() % (r - l + 1);
}
ll rev(ll v)
{
return mpow(v, MOD - 2);
}
ll nc2(ll n)
{
return (n * (n - 1)) / 2;
}
ll ceil(ll p, ll q)
{
return (p + q - 1) / q;
}
void solve()
{
int k, n, m;
cin >> k >> n >> m;
v64 arr(k + 1);
v64 ans(k + 1, 0);
for (int i = 1; i <= k; i++)
{
cin >> arr[i];
ans[i] = arr[i] * m / n;
}
m = m % n;
// v64 f(k + 1);
// v64 c(k + 1);
vp64 temp(k + 1);
ll sum = 0;
for (int i = 1; i <= k; i++)
{
// f[i] = arr[i] * m / n;
// c[i] = f[i] + 1;
temp[i] = {(arr[i] * m) % n,
i};
sum += temp[i].fi;
}
int cnt = sum / n;
sort(temp.begin() + 1, temp.end(), greater<p64>());
for (int i = 1; i <= cnt; i++)
{
ans[temp[i].se]++;
}
for (int i = 1; i <= k; i++)
{
cout << ans[i] << " ";
}
}
int main()
{
fast_cin();
ll t = 1;
// cin >> t;
// mint a = 3;
// cout << (1 - a).val() << endl;
forn(i, t)
{
// cout << "Case #" << i+1 << ": ";
solve();
}
return 0;
} | #include <iostream>
#include <queue>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
int main(void) {
ios::sync_with_stdio(false);
int64_t K, N, M;
cin >> K >> N >> M;
vector<int64_t> A(K);
rep(i, K) cin >> A[i];
vector<int64_t> B(K);
int64_t sumB = M;
rep(i, K) {
B[i] = (int64_t)A[i] * (int64_t)M / (int64_t)N;
sumB -= B[i];
}
using qtype = pair<int64_t, int>;
priority_queue<qtype, vector<qtype>, greater<qtype>> que;
rep(i, K) {
que.emplace(B[i] * N - A[i] * M, i);
}
while(sumB--) {
auto idx = que.top().second;
que.pop();
B[idx]++;
que.emplace(B[idx] * N - A[idx] * M, idx);
}
rep(i, K){
if(i) cout << " ";
cout << B[i];
}
cout << endl;
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define REP(i,n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define debug(var) do{cout << #var << " : "; view(var);}while(0)
template<class T> bool chmin(T &a, T b) {if(a>b) {a=b;return 1;}return 0;}
template<class T> bool chmax(T &a, T b) {if(a<b) {a=b;return 1;}return 0;}
using namespace std;
template<class T> void view(T e) {cout << e << endl;}
template<class T> void view(const vector<T> &v) {for(const auto &e : v){cout << e << " ";} cout << endl;}
template<class T> void view(const vector<vector<T>> &vv) {for(const auto &v : vv){view(v);}}
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int,int>;
const int inf = 1<<30;
const ll inf_l = 1LL<<61;
const int MAX = 1e5;
int main() {
int n, m; cin >> n >> m;
vint a(m), b(m);
rep(i,m) cin >> a[i] >> b[i];
int k; cin >> k;
vint c(k), d(k);
rep(i,k) cin >> c[i] >> d[i];
int ans = -1;
rep(i,1<<k) {
set<int> st;
rep(j,k) {
if (i >> j & 1) {
st.insert(c[j]);
} else st.insert(d[j]);
}
int tmp = 0;
rep(j,m) {
if (st.count(a[j]) && st.count(b[j])) tmp++;
}
ans = max(ans, tmp);
}
cout << ans << endl;
} | #include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define pb push_back
#define COUT(x) cout << (x) << "\n"
#define COUTF(x) cout << setprecision(15) << (x) << "\n"
#define ENDL cout << "\n"
#define DF(x) x.erase(x.begin())
#define ALL(x) x.begin(), x.end()
#define SORT(x) sort(ALL(x))
#define RSORT(x) sort(x.rbegin(), x.rend())
#define REVERSE(x) reverse(ALL(x))
#define MAX(x) *max_element(ALL(x))
#define MAXI(x) max_element(ALL(x)) - x.begin()
#define SUM(x) accumulate(ALL(x), 0ll)
#define COUNT(x, y) count(ALL(x), y);
#define ANS cout << ans << "\n"
#define YES cout << "YES\n";
#define NO cout << "NO\n";
#define Yes cout << "Yes\n";
#define No cout << "No\n";
#define init() \
cin.tie(0); \
ios::sync_with_stdio(false)
#define debug(x) cerr << "[debug] " << #x << ": " << x << endl;
#define debugV(v) \
cerr << "[debugV] " << #v << ":"; \
rep(z, v.size()) cerr << " " << v[z]; \
cerr << endl;
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using mll = map<ll, ll>;
using qll = queue<ll>;
using P = pair<ll, ll>;
using vp = vector<P>;
using vs = vector<string>;
template <typename T>
inline istream& operator>>(istream& i, vector<T>& v) {
rep(j, v.size()) i >> v[j];
return i;
}
template <typename T1, typename T2>
inline istream& operator>>(istream& i, pair<T1, T2>& v) {
return i >> v.first >> v.second;
}
constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
constexpr ld PI = 3.141592653589793238462643383279;
ll get_digit(ll x) {
return to_string(x).size();
}
ll gcd(ll x, ll y) {
return y ? gcd(y, x % y) : x;
}
ll lcm(ll a, ll b) {
return a / gcd(a, b) * b;
}
template <class T>
void chmax(T& a, const T& b) {
if (a < b) a = b;
}
template <class T>
void chmin(T& a, const T& b) {
if (b < a) a = b;
}
ll RAND(ll min, ll max) {
random_device seed_gen;
mt19937 engine(seed_gen());
uniform_int_distribution<ll> distribution(min, max);
return distribution(engine);
}
signed main() {
init();
ll N, M;
cin >> N >> M;
vs S(M);
cin >> S;
vs C = {"A", "B", "C", "D", "E", "F", "G", "H", "."};
rep(i, N) {
rep(j, N) {
cout << C[RAND(0, 8)];
}
ENDL;
}
return 0;
} |
#include <iostream>
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <tuple>
#include <math.h>
#include <set>
#include <cassert>
#include <stack>
#include <bitset>
#include <map>
#include <queue>
#include <random>
#include <unordered_set>
#include <unordered_map>
#define DEBUG
#define fi first
#define se second
#define pqueue priority_queue
#define pb(x) push_back(x)
//#define endl '\n'
#define all(x) x.begin(), x.end()
#define int long long
#define mk(a, b) make_pair(a, b)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<ull> vull;
typedef vector<ll> vll;
// typedef tuple<ll, ll, ll> tiii;
typedef pair<int, int> pii;
typedef vector<pair<int, int> > vpii;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<vector<ll> > vvll;
typedef vector<char> vc;
const int inf = 1e9 + 228;
const ll infll = 1e18;
const ll MOD = 1e9 + 7;
//static const int maxn = 1e6 + 228;
const ld eps = 1e-9;
const int K = 31;
const ld eps2 = 1e-9;
const ll MOD2 = 998244353;
const ll dosz = 5e5;
const ll SZ = (1 << 18);
const ld PI = 3.1415926535;
void fast_io() {
ios_base::sync_with_stdio(0);
cin.tie(0);
// freopen("INPUT.TXT", "r", stdin);
// freopen("OUTPUT.TXT", "w", stdout);
}
const int maxn = 2e5 + 3;
int kek[maxn];
int lol[maxn][301];
int lol2[maxn][301];
int fac[301];
int rfac[301];
int binpow(int n, int k) {
if (!k)
return 1;
else if (k & 1)
return binpow(n, k - 1) * n % MOD2;
int b = binpow(n, k / 2);
return b * b % MOD2;
}
void solve() {
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++)
cin >> kek[i];
fac[0] = rfac[0] = 1;
for (int i = 1; i <= k; i++) {
fac[i] = fac[i - 1] * i;
fac[i] %= MOD2;
rfac[i] = binpow(fac[i], MOD2 - 2);
}
for (int i = 0; i < n; i++) {
lol[i][0] = 1;
for (int j = 1; j <= k; j++) {
lol[i][j] = lol[i][j - 1] * kek[i] % MOD2;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j <= k; j++) {
lol[i][j] = lol[i][j] * rfac[j] % MOD2;
}
}
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j <= k; j++) {
lol2[i][j] = lol2[i + 1][j] + lol[i][j];
if (lol2[i][j] >= MOD2)
lol2[i][j] -= MOD2;
}
}
for (int i = 1; i <= k; i++) {
int ans = 0;
for (int j = 0; j <= i; j++) {
ans += lol2[0][j] * lol2[0][i - j];
ans %= MOD2;
}
ans *= fac[i];
ans %= MOD2;
ans *= binpow(2, MOD2 - 2);
ans %= MOD2;
int abc = binpow(2, i - 1);
for (int j = 0; j < n; j++) {
ans -= lol[j][i] * fac[i] % MOD2 * abc % MOD2;
ans += MOD2;
if(ans >= MOD2)
ans -= MOD2;
}
cout << ans << endl;
}
}
signed main() {
fast_io();
srand(time(NULL));
cout << fixed << setprecision(10);
int q = 1;
// cin >> q;
while (q--)
solve();
} | #include <bits/stdc++.h>
using namespace std;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define rep1(a, b, c) for(int a = (b); a < (c); ++a)
#define each(a, b) for(auto& a : b)
#define sz(x) (int)(x).size()
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define dbg(x) cout << #x << " = " << x << endl;
#define vt vector
#define pii pair<int, int>
#define pll pair<ll, ll>
#define bitcnt(x) __builtin_popcount(x)
using ll = long long;
const int inf = (int)1e9 + 5;
const ll infl = (ll)1e18 + 5;
const int mod = 1e9 + 7;
int d[2000][2000][4];
ll go(int b){
ll ans = 1;
ll t = 2;
while(b){
if(b&1) ans = (ans*t)%mod;
b >>= 1; t = (t*t)%mod;
}
return ans;
}
void solve(){
int h, w; cin >> h >> w;
vt<string> s(h);
each(x, s) cin >> x;
ll t1 = 0;
rep(x, h){
int t = 0;
rep(y, w){
d[x][y][0] = t;
if(s[x][y] == '.') ++t, ++t1;
else t = 0;
}
}
rep(x, h){
int t = 0;
for(int y = w-1; ~y; --y){
d[x][y][1] = t;
if(s[x][y] == '.') ++t;
else t = 0;
}
}
rep(y, w){
int t = 0;
rep(x, h){
d[x][y][2] = t;
if(s[x][y] == '.') ++t;
else t = 0;
}
}
rep(y, w){
int t = 0;
for(int x = h-1; ~x; --x){
d[x][y][3] = t;
if(s[x][y] == '.') ++t;
else t = 0;
}
}
ll t2 = go(t1);
ll ans = 0;
rep(i, h) rep(j, w){
if(s[i][j] == '#') continue;
int t = 0;
rep(k, 4) t += d[i][j][k];
t += (s[i][j] == '.');
ll t3 = go(t1-t);
ans += t2 - t3;
}
ans %= mod; ans += mod; ans %= mod;
cout << ans << '\n';
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
} |
// Problem: E - Unique Color
// Contest: AtCoder - AtCoder Beginner Contest 198
// URL: https://atcoder.jp/contests/abc198/tasks/abc198_e
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define it insert
#define pob pop_back
#define pub push_back
#define emb emplace_back
#define all(v) (v).begin(), (v).end()
#define mkp(a, b) make_pair(a, b)
using LL = long long;
using VI = vector<int>;
using VVI = vector<vector<int>>;
using PII = pair<int, int>;
using PIL = pair<int, LL>;
using PLL = pair<LL, LL>;
const double EPS = 1e-6;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 7;
int h[N], e[2 * N], ne[2 * N], idx;
int color[N], vt[N], cnt[N];
int n;
int ans[N], m;
void add(int a, int b) { e[idx] = b, ne[idx] = h[a], h[a] = idx++; }
void dfs(int u) {
cnt[color[u]] += 1;
vt[u] = 1;
if (cnt[color[u]] == 1) ans[m++] = u;
for (int i = h[u]; i != -1; i = ne[i]) {
int v = e[i];
if (!vt[v]) dfs(v);
}
cnt[color[u]] -= 1;
}
int main() {
memset(h, -1, sizeof h);
cin >> n;
for (int i = 1; i <= n; ++i) cin >> color[i];
for (int i = 1; i < n; ++i) {
int x, y;
cin >> x >> y;
add(x, y), add(y, x);
}
dfs(1);
sort(ans, ans + m);
for (int i = 0; i < m; ++i) cout << ans[i] << '\n';
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define V vector
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define mp make_pair
#define sz(x) int(x.size())
#define pcnt __builtin_popcountll
template <typename T> bool chmin(T &a, const T &b) {if(a > b){a = b; return true;} return false;}
template <typename T> bool chmax(T &a, const T &b) {if(a < b){a = b; return true;} return false;}
template <typename T> void print(T a) {cout << a << ' ';}
template <typename T> void printe(T a) {cout << a << endl;}
template <typename T> void printv(T a) {rep(i, sz(a))print(a[i]); cout<<endl;}
template <typename T> void printp(pair<T, T> a) {print(a.first); cout<<a.second<<endl;}
template <typename A, size_t N, typename T> void Fill (A (&array)[N], const T & val) {fill ((T*)array, (T*)(array+N), val);}
template <typename T> using vc = vector<T>;
template <typename T> using vv = vc<vc<T>>;
template <typename T> using vvv = vc<vv<T>>;
using ll = long long;
using P = pair<int, int>;
using Pl = pair<ll, ll>;
using vi = vc<int>;
using vvi = vv<int>;
using vl = vc<ll>;
using vvl = vv<ll>;
vi di = {-1, 1, 0, 0, -1, -1, 1, 1};
vi dj = { 0, 0, -1, 1, -1, 1, -1, 1};
const int MAX_N = 1e5 + 10;
int n;
vi color(MAX_N);
vi cnt(MAX_N, 0);
vi ans(MAX_N, false);
vvi to(MAX_N);
void dfs (int now, int p) {
for (int nxt : to[now]) {
if (nxt == p) continue;
if (cnt[color[nxt]] == 0) ans[nxt] = true;
cnt[color[nxt]]++;
dfs(nxt, now);
cnt[color[nxt]]--;
}
}
int main () {
cin >> n;
rep(i, n) cin >> color[i];
rep(i, n-1) {
int a, b; cin >> a >> b, a--, b--;
to[a].eb(b);
to[b].eb(a);
}
ans[0] = true;
cnt[color[0]]++;
dfs(0, -1);
rep(i, n) if (ans[i]) cout << i+1 << endl;
return 0;
} |
#include <vector>
#include <array>
#include <stack>
#include <queue>
#include <list>
#include <bitset>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <iomanip>
#include <string>
#include <chrono>
#include <random>
#include <cmath>
#include <cassert>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <functional>
#include <sstream>
using namespace std;
class DisjointSet {
public:
DisjointSet(int n) : N(n), sz(n), pars(n, -1) {
}
void reset() { sz = N, fill(pars.begin(), pars.end(), -1); }
int find(int x) { return pars[x] < 0 ? x : pars[x] = find(pars[x]); }
int size() { return sz; }
int count(int x) { return -pars[find(x)]; }
bool unite(int x, int y) {
x = find(x), y = find(y);
if (x == y) { return false; }
--sz;
if (pars[x] < pars[y]) { swap(x, y); }
pars[y] += pars[x];
pars[x] = y;
return true;
}
bool isSame(int x, int y) { return find(x) == find(y); }
private:
int N, sz;
vector<int> pars;
};
int main(int argc, char** argv) {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(12);
int n, m;
cin >> n >> m;
vector<string> S(n);
for (int i = 0; i < n; ++i) {
cin >> S[i];
}
DisjointSet ds(n + m);
ds.unite(0, n - 1);
ds.unite(0, n);
ds.unite(0, n + m - 1);
vector<int> X{-1, 1, 0, 0};
vector<int> Y{0, 0, -1, 1};
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (S[i][j] == '#') {
ds.unite(i, n + j);
}
}
}
set<int> R;
set<int> C;
int res = 0;
for (int i = 0; i < n; ++i) {
R.insert(ds.find(i));
}
for (int i = 0; i < m; ++i) {
C.insert(ds.find(n + i));
}
res = min(R.size() - 1, C.size() - 1);
cout << res << '\n';
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define rep(x, L, R) for(int x = (L), _x = (R); x <= _x; x++)
#define per(x, R, L) for(int x = (R), _x = (L); x >= _x; x--)
#define broken fprintf(stderr, "running on %s %d\n", __FUNCTION__, __LINE__)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define SZ(v) (int)(v).size()
#define ALL(x) (x).begin(), (x).end()
#define x first
#define y second
#define mp make_pair
#define pb push_back
template<typename T> inline bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template<typename T> inline bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
typedef pair<int, int> par;
typedef long long LL;
typedef unsigned long long uLL;
typedef double db;
const int N = 2e5 + 10, inf = 0x3f3f3f3f;
int n, k;
int a[N], b[N];
vector<int> g[N];
void adde(int u, int v) { g[u].push_back(v); }
void dfs(int u, int fa, int k, int &cnt) {
int mn = n * 2, smn = n * 2;
a[u] = b[u] = 0;
for(auto v : g[u]) {
if(v == fa) continue;
dfs(v, u, k, cnt);
if(mn > b[v]) smn = mn, mn = b[v];
else smn = min(smn, b[v]);
}
for(auto v : g[u]) {
if(v == fa) continue;
int now = mn;
if(b[v] == now) now = smn;
if(now + 2 + a[v] <= k) a[v] = - n * 2;
else a[u] = max(a[u], a[v] + 1);
}
if(mn + 1 <= k && a[u] <= 0) a[u] = - n * 2;
if(a[u] >= k) cnt++, a[u] = - n * 2, b[u] = 0;
else b[u] = mn + 1;
return;
}
int check(int k) {
int cnt = 0;
dfs(1, 0, k, cnt);
if(a[1] >= 0) cnt++;
return cnt;
}
int main() {
scanf("%d%d", &n, &k);
rep(i, 1, n - 1) {
int u, v;
scanf("%d%d", &u, &v);
adde(u, v), adde(v, u);
}
int l = 0, r = n, best = -1;
while(l <= r) {
int mid = (l + r) / 2;
if(check(mid) <= k) best = mid, r = mid - 1;
else l = mid + 1;
}
printf("%d\n", best);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define mp make_pair
#define pb push_back
typedef long long ll;
const ll MOD = 1e9+7;
const ll INF = 1e18+5;
pair <ll, char> p[200005];
multiset <ll> r;
multiset <ll> g;
multiset <ll> b;
ll solve1(multiset <ll> a, multiset<ll> b) {
ll sol = INF;
for (ll x:a) {
multiset <ll> :: iterator it = b.lower_bound(x);
if (it != b.end()) {
sol = min(sol, *it-x);
}
if (it != b.begin()) {
it--;
sol = min(sol, x-*it);
}
}
return sol;
}
ll solve2(multiset <ll> a, multiset<ll> b, multiset <ll> s) {
ll ba = INF;
ll bb = INF;
ll sol = INF;
for (ll x:s) {
ll A = INF;
ll B = INF;
multiset <ll> :: iterator it = a.lower_bound(x);
if (it != a.end()) {
A = min(A, *it-x);
}
if (it != a.begin()) {
it--;
A = min(A, x-*it);
}
it = b.lower_bound(x);
if (it != b.end()) {
B = min(B, *it-x);
}
if (it != b.begin()) {
it--;
B = min(B, x-*it);
}
sol = min(sol, A+bb);
sol = min(sol, B+ba);
ba = min(ba, A);
bb = min(bb, B);
}
return sol;
}
int main() {
int n;
scanf("%d", &n);
n *= 2;
for (int i=0 ; i<n ; i++) {
scanf("%lld %c", &p[i].x, &p[i].y);
if (p[i].y == 'R') r.insert(p[i].x);
if (p[i].y == 'G') g.insert(p[i].x);
if (p[i].y == 'B') b.insert(p[i].x);
}
if (r.size()%2 == 0 and g.size()%2 == 0 and b.size()%2 == 0) {
printf("0\n");
return 0;
}
if (r.size()%2 == 0) printf("%lld\n", min(solve1(g, b), solve2(g, b, r)));
if (g.size()%2 == 0) printf("%lld\n", min(solve1(r, b), solve2(r, b, g)));
if (b.size()%2 == 0) printf("%lld\n", min(solve1(r, g), solve2(r, g, b)));
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define inf 1e18
// 1000000007 , 998244353
// cout<<"? "<<endl for interactive
#define mod 998244353
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define clr(x,y,z) (x).clear(),(x).resize((y),(z))
typedef long long int lli;
typedef vector<lli> vli;
typedef vector<int> vi;
typedef tuple<int, int, long long int>tup;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<lli, lli> pll;
typedef pair<int, int> pii;
#define csb(x) __builtin_popcount(x)
#define clz(x) __builtin_clz(x)
#define ctz(x) __builtin_ctz(x)
#define PI 3.1415926535897932384626
#define deb(x) cout << #x << " = " << x << endl;
#define show(x) {for( auto i : x) cout << i << '\t'; cout << endl;}
#define show2(x,l1,l2) {for( int i=0;i<l1;i++){for(int j=0;j<l2;j++) cout << x[i][j] << '\t'; cout << endl;}}
#define rep(i,st,ed) for(int i=(st);i<(ed);i++)
#define rrep(i,ed,st) for(int i=ed;i>=st;i--)
#define ff first
#define ss second
#define minheap(type) priority_queue<type,vector<type>,greater<type>>
template <typename T> void ckmin(T &a, const T &b) { a = min(a, b); }
template <typename T> void ckmax(T &a, const T &b) { a = max(a, b); }
long long gcd(ull a, ull b) { return b == 0 ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
void sieve(vector<int>&spf) {int n = spf.size(); rep(i, 2, n) {if (!spf[i]) {/*prime.pb(i)*/; for (int j = i; j < n; j += i) spf[j] = i; }}}
lli power(lli x, lli y, long long int m = mod) { lli ans = 1; x %= m; while (y) {if (y & 1)ans = (x * ans) % m; x = (x * x) % m; y >>= 1;} return ans;}
lli ncr(vli&fact, int n, int r) {return (n >= r) ? ((fact[n] * ((power(fact[r], mod - 2) * power(fact[n - r], mod - 2) ) % mod)) % mod) : 0;}
// int dx[] = {1, -1, 0, 0};
// int dy[] = {0, 0, 1, -1};
void solve() {
int n;
cin >> n;
n*=2;
vector<pll> arr(n);
vi odd(3,0);
rep(i,0,n){
cin>>arr[i].ff;
char c;
cin>>c;
if(c=='R')arr[i].ss = 1,odd[0]++;
else if(c=='G')arr[i].ss = 2,odd[1]++;
else arr[i].ss = 4,odd[2]++;
}
rep(i,0,3)odd[i]%=2;
int state = (odd[0])^(odd[1]<<1)^(odd[2]<<2);
sort(all(arr));
vli scene(7,1e16);
rep(i,0,n-1){
if(arr[i].ss != arr[i+1].ss){
scene[arr[i].ss ^ arr[i+1].ss] =min(scene[arr[i].ss ^ arr[i+1].ss],abs(arr[i].ff - arr[i+1].ff ));
}
}
// deb(state)
// show(scene)
if(state == 0){
cout<<0;
}
else if (state == 6){
cout<< min ({scene[3]+scene[5],scene[6]});
}
else if(state == 3){
cout<< min ({scene[6]+scene[5],scene[3]});
}
else{
cout<< min ({scene[3]+scene[6],scene[5]});
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int z = 1;
// cin >> z;
while (z--) {
solve();
}
return 0;
} |
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin>>N;
vector<vector<ll>> C(N,vector<ll> (N,0));
rep(i,N) rep(j,N) cin>>C[i][j];
vector<ll> A(N,0),B(N,0);
int mir=0,mic=0;
rep(i,N) rep(j,N) if(C[i][j]<C[mir][mic]) mir=i,mic=j;
for(int i=0;i<N;i++) A[i]=C[i][mic]-C[mir][mic];
for(int j=0;j<N;j++) B[j]=C[mir][j]-C[mir][mic];
int diff=C[0][0]-(A[0]+B[0]);
for(int i=0;i<N;i++) for(int j=0;j<N;j++){
if(C[i][j]-(A[i]+B[j])!=diff){
cout<<"No"<<endl;
return 0;
}
}
rep(i,N) A[i]+=diff;
cout<<"Yes"<<endl;
rep(i,N) cout<<A[i]<<" ";
cout<<endl;
rep(i,N) cout<<B[i]<<" ";
cout<<endl;
return 0;
}
| #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include <iostream>
#include <array>
#include <algorithm>
#include <vector>
#include <bitset>
#include <set>
#include <unordered_set>
#include <cmath>
#include <complex>
#include <deque>
#include <iterator>
#include <numeric>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <limits>
#include <iomanip>
#include <functional>
#include <cassert>
// #include <atcoder/all>
using namespace std;
using ll=long long;
template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vii = V<int>;
using vvll = V<vll>;
using vvii = V< V<int> >;
using PII = P<int, int>;
using PLL = P<ll, ll>;
#define RevREP(i,n,a) for(ll i=n;i>a;i--) // (a,n]
#define REP(i,a,n) for(ll i=a;i<n;i++) // [a,n)
#define rep(i, n) REP(i,0,n)
#define ALL(v) v.begin(),v.end()
#define eb emplace_back
#define pb push_back
#define sz(v) int(v.size())
template < class T > inline bool chmax(T& a, T b) {if (a < b) { a=b; return true; } return false; }
template < class T > inline bool chmin(T& a, T b) {if (a > b) { a=b; return true; } return false; }
template< class A, class B > ostream& operator <<(ostream& out, const P<A, B> &p) {
return out << '(' << p.first << ", " << p.second << ')';
}
template< class A > ostream& operator <<(ostream& out, const V<A> &v) {
out << '[';
for (int i=0;i<int(v.size());i++) {
if (i) out << ", ";
out << v[i];
}
return out << ']';
}
template<class A, class B> istream& operator >>(istream& in, P<A, B> &p) {
return in >> p.first >> p.second;
}
template<class A> istream& operator >>(istream& in, V<A> &v) {
for (int i = 0; i < int(v.size()); i++) in = in >> v[i];
return in;
}
const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int a, b; cin >> a >> b;
if (a < b) {
rep(i, b) cout << -(i + 1) << ' ';
rep(i, a - 1) cout << i + 1 << ' ';
int aaa = 0;
REP(i, a, b + 1) aaa += i;
cout << aaa << '\n';
} else {
rep(i, a) cout << i + 1 << ' ';
rep(i, b - 1) cout << -(i + 1) << ' ';
int aaa = 0;
REP(i, b, a + 1) aaa += i;
cout << -aaa << '\n';
}
return 0;
}
|
#include<cstdio>
#include<iostream>
#define ll long long
using namespace std;
int main(){
long long n,t;
cin>>t>>n;
long long l=0,r=100000000000000,mid;
while(l<r){
mid=(l+r)/2;
if(
(((100+t)*mid)/100<n+mid)
){
l=mid+1;
}
else{
r=mid;
}
}
cout<<1+((100+t)*(l-1))/100;
// cin>>n;
} | /*Bismillahir Rahmanir Rahim*/
#include<bits/stdc++.h>
#define ll long long int
#define pb push_back
#define mp make_pair
#define PI acos(-1.0)
using namespace std;
int main()
{
ios_base :: sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
double r,x,y,a=0,b=0,s,cnt=0;
cin>>r>>x>>y;
s=sqrt(x*x+y*y);
if(s<r) cnt++;
cout<<ceil(s/r)+cnt<<endl;
}
|
/*
#pragma GCC optimize("Ofast")
#pragma GCC target("avx", "avx2", "fma")
#pragma GCC optimization("unroll-loops")
*/
#include <bits/stdc++.h>
using namespace std;
#define pi acos(-1)
#define IOS ios_base::sync_with_stdio(0); cin.tie(); cout.tie();
#define fi first
#define se second
#define pf push_front
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define debug(x) cerr<<#x<<": "<<(x)<<'\n'
#define lb lower_bound
#define ub upper_bound
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
const ll mod = 998244353;
const ll inf = 0x3f3f3f3f;
const ll llinf = 1e18;
const ll nax = 0;
int main(){
IOS
ll a, b, c; cin >> a >> b >> c;
ll satu=a*(a+1)/2%mod;
ll dua=b*(b+1)/2%mod;
ll tiga=c*(c+1)/2%mod;
cout << ((satu*dua)%mod*tiga)%mod << '\n';
} | #pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rc(x) return cout<<x<<endl,0
#define pb push_back
#define mkp make_pair
#define in insert
#define er erase
#define fd find
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
#define lun(x) (int)x.size()
typedef long long ll;
typedef long double ld;
const ll INF=0x3f3f3f3f3f3f3f3f;
const ll llinf=(1LL<<60);
const int inf=(1<<30);
const int nmax=5e5+50;
const ll mod=1e9+7;
using namespace std;
ll x,y,a,b, ans;
int main()
{
//freopen("sol.in","r",stdin);
//freopen("sol.out","w",stdout);
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
cin >> x >> y >> a >> b;
while((double)a*x<=2e18 && a*x < y && x*a <= x+b){
x *= a;
ans++;
}
cout << ans + (y - x - 1)/b << endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std ;
#define vt vector
#define all(c) (c).begin(), (c).end()
#define sz(x) (int)(x).size()
#define ll long long
#define ld long double
#define ar array
#define inf() ifstream cin("d.txt")
#define onf() ofstream cout("test.txt")
#define mod 1000000007
#define pb push_back
#define endl cout<<"\n";
#define prt1(a) cout<<a<<" "
#define prt2(a,b) cout<<a<<" "<<b<<" "
#define prt3(a,b,c) cout<<a<<" "<<b<<" "<<c<<" "
#define in(a) cin>>a
#define inn(a,b) cin>>a>>b
#define innn(a,b,c) cin>>a>>b>>c
#define rep(i,st,end) for(ll i=st;i<end;i++)
#define repv(i,st,end) for(ll i=st;i>=end;i--)
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define TEST ll tc;cin>>tc;while(tc--)
#define EACH(x, a) for (auto& x: a)
template<class A> void read(vt<A>& v);
template<class A, size_t S> void read(ar<A, S>& a);
template<class T> void read(T& x) {
cin >> x;
}
void read(double& d) {
string t;
read(t);
d=stod(t);
}
void read(long double& d) {
string t;
read(t);
d=stold(t);
}
template<class H, class... T> void read(H& h, T&... t) {
read(h);
read(t...);
}
template<class A> void read(vt<A>& x) {
EACH(a, x)
read(a);
}
template<class A, size_t S> void read(array<A, S>& x) {
EACH(a, x)
read(a);
}
#define debug(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a <<"\n";
err(++it, args...);
}
void solve()
{
ll n, m ,t ;
read(n,m,t) ;
ll mx = n ;
vector<pair<ll,ll>>v ;
rep(i,0,m)
{
ll x, y ;
read(x,y) ;
v.pb({x,y}) ;
}
ll f=0;
ll dec = v[0].first ;
n = n-dec ;
if(n<=0)
{
cout<<"No";
return ;
}
// cout<<"N is "<<n;endl;
rep(i,0,m)
{
if(i!=0)
{
dec = v[i].first - v[i-1].second ;
n=n-dec ;
// cout<<n;endl;
if(n<=0)
{
f=1;
break ;
}
}
ll inc = v[i].second - v[i].first ;
n=n+inc ;
if(n>mx)
n=mx ;
// cout<<n;endl;
}
if(v[m-1].second!=t)
{
dec = t-v[m-1].second ;
n=n-dec ;
}
if(n<=0 || f==1)
cout<<"No";
else
cout<<"Yes" ;
}
int main()
{
clock_t start, end;
start = clock();
fio;
ll k = 1;
ll tc = 1;
// cin>>tc;
while(tc--){
// cout<<"Case #"<<k<<": ";k++;
solve();
endl;
}
end = clock();
double time_taken = double(end - start) / double(CLOCKS_PER_SEC);
// debug(time_taken);
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define BE(x) x.begin(), x.end()
int main() {
ll n, m, t;
cin >> n >> m >> t;
vector<ll> a(m), b(m);
for (int i = 0; i < m; i++) cin >> a[i] >> b[i];
ll bat = n;
for (int i = 0; i < m; i++) {
bat -= (i > 0) ? (a[i] - b[i-1]) : a[i];
if (bat <= 0) {
cout << "No" << endl;
return 0;
}
bat += (b[i] - a[i]);
if (bat > n) bat = n;
}
if (bat <= (t - b.back())) cout << "No" << endl;
else cout << "Yes" << endl;
} |
#include <bits/stdc++.h>
using namespace std;
int main(){
int a,b,c;
cin>>a>>b>>c;
if(a*a + b*b < c*c) cout<<"Yes";
else cout<<"No";
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i,i1,n) for(int i=(int)(i1);i<(int)(n);i++)
#define ll long long
#define vi vector<int>
#define vll vector<long long>
using graph = vector<vector<int>>;
int main() {
int a,b,c=0;
cin>>a>>b>>c;
if(a*a+b*b<c*c){
cout<<"Yes";
}else{
cout<<"No";
}
}
vector<int> BFS(int N, int M) {
// N, M ใฏ้ ็นๆฐใจ่พบๆฐ
// ใฐใฉใๅ
ฅๅๅๅ (็กๅใฐใฉใๆณๅฎ)
graph G(N);
rep (i, 0, M) {
int a, b;
cin >> a >> b;
G[a].push_back(b);
G[b].push_back(a);
}
// BFS ใฎใใใฎใใผใฟๆง้
vector<int> dist(N, -1); // ๅ
จ้ ็นใใๆช่จชๅใใซๅๆๅ
queue<int> que;
// ๅๆๆกไปถ (้ ็น 0 ใๅๆใใผใใจใใ)
dist[0] = 0;
que.push(0); // 0 ใๆฉ่ฒ้ ็นใซใใ
// BFS ้ๅง (ใญใฅใผใ็ฉบใซใชใใพใงๆข็ดขใ่กใ)
while (!que.empty()) {
int v = que.front(); // ใญใฅใผใใๅ
้ ญ้ ็นใๅใๅบใ
que.pop();
// v ใใ่พฟใใ้ ็นใใในใฆ่ชฟในใ
for (int nv : G[v]) {
if (dist[nv] != -1) continue; // ใใงใซ็บ่ฆๆธใฟใฎ้ ็นใฏๆข็ดขใใชใ
// ๆฐใใช็ฝ่ฒ้ ็น nv ใซใคใใฆ่ท้ขๆ
ๅ ฑใๆดๆฐใใฆใญใฅใผใซ่ฟฝๅ ใใ
dist[nv] = dist[v] + 1;
que.push(nv);
}
}
// ็ตๆๅบๅ (ๅ้ ็นใฎ้ ็น 0 ใใใฎ่ท้ขใ่ฆใ)
return dist;
}
//ABC177
struct UnionFind {
vector<int> par; // par[i]:iใฎ่ฆชใฎ็ชๅทใ(ไพ) par[3] = 2 : 3ใฎ่ฆชใ2
UnionFind(int N) : par(N) { //ๆๅใฏๅ
จใฆใๆ นใงใใใจใใฆๅๆๅ
for(int i = 0; i < N; i++) par[i] = i;
}
int root(int x) { // ใใผใฟxใๅฑใใๆจใฎๆ นใๅๅธฐใงๅพใ๏ผroot(x) = {xใฎๆจใฎๆ น}
if (par[x] == x) return x;
return par[x] = root(par[x]);
}
void unite(int x, int y) { // xใจyใฎๆจใไฝตๅ
int rx = root(x); //xใฎๆ นใrx
int ry = root(y); //yใฎๆ นใry
if (rx == ry) return; //xใจyใฎๆ นใๅใ(=ๅใๆจใซใใ)ๆใฏใใฎใพใพ
par[rx] = ry; //xใจyใฎๆ นใๅใใงใชใ(=ๅใๆจใซใชใ)ๆ๏ผxใฎๆ นrxใyใฎๆ นryใซใคใใ
}
bool same(int x, int y) { // 2ใคใฎใใผใฟx, yใๅฑใใๆจใๅใใชใtrueใ่ฟใ
int rx = root(x);
int ry = root(y);
return rx == ry;
}
};
bool IsPrime(int num)
{
if (num < 2) return false;
else if (num == 2) return true;
else if (num % 2 == 0) return false; // ๅถๆฐใฏใใใใใ้คใ
double sqrtNum = sqrt(num);
for (int i = 3; i <= sqrtNum; i += 2){
if (num % i == 0)return false;
}
return true;
}
vector<int> Vecin(vector<int> vec,int n){
rep(i,0,n){
cin>>vec.at(i);
}
return vec;
} |
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define FastIO() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define read() freopen("in.txt","r",stdin)
#define write() freopen("out.txt","w",stdout)
typedef long long ll;
typedef unsigned long long ull;
typedef double dbl;
typedef float flt;
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
#define pie acos(-1)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define MAX 300005
#define MOD 1000000007
#define INF 1000000100
#define for0(i, n) for(ll i=0; i<n; i++)
#define for1(i, n) for(ll i=1; i<=n; i++)
#define forab(i, a, b) for(ll i=a; i<=b; i++)
#define forabx(i, a, b, x) for(ll i=a; i<=b; i+=x)
#define forinv(i, a, b) for(ll i=a; i>=b; i--)
#define clr0(a) memset(a, 0, sizeof(a))
#define ceil(x, y) (x+y-1) / (y)
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) ((a * b) / __gcd(a, b))
#define sort1(a, x, y) sort(a+x, a+y+1)
#define debug(x) cout << "DEBUG " << x << endl;
#define Y() cout << "YES" << endl;
#define N() cout << "NO" << endl;
#define inArr(a, x, y) forab(i, x, y) { cin >> a[i]; }
#define outArr(a, x, y) forab(i, x, y) { cout << a[i] << " "; } cout << endl;
void solve() {
ll n;
cin >> n;
if (n==191) {
cout << "so-so" << endl;
}
else if (n>191) {
cout << ":(" << endl;
}
else {
cout << "Yay!" << endl;
}
}
void testcase() {
ll t;
cin >> t;
while (t--) {
solve();
}
}
int main () {
FastIO()
//testcase();
solve();
return 0;
}
| #include <bits/stdc++.h>
#include <bits/extc++.h>
#define double long double
#define rbtree __gnu_pbds::tree<int,__gnu_pbds::null_type,less<int>,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>
#define int ll
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);
#define pb push_back
#define ALL(X) X.begin(),X.end()
#define F(i,n) FF(i,0,n)
#define F1(i,n) FF(i,1,n+1)
#define FF(i,n,m) for(int i=(int)n;i<(int)m;++i)
#ifndef LOCAL
//#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
#else
#endif // LOCOL
//#define mp make_pair
using namespace std;
//using namespace __gnu_pbds;
template<typename T> bool remax(T& a, const T& b) {return b>a?a=b,1:0;}
template<typename T> bool remin(T& a, const T& b) {return b<a?a=b,1:0;}
inline ostream& operator << (ostream& os,__int128_t a){if(a==0) {return (os<<'0');}bool flag=0;if(a<0) {a=-a,flag=1;}string s;while(a){s+=a%10+'0';a/=10;}s+=flag?"-":"";reverse(ALL(s));return os<<s;}
inline istream& operator >>(istream& is,__int128_t& a){string s;cin>>s;a=0;for(auto c:s) a=a*__int128_t(10)+__int128_t(c-'0');return is;}
template<typename T,typename P> inline ostream& operator << (ostream& os,pair<T,P> a){os<<a.first<<" "<<a.second; return os;}
template<typename T,typename P> inline istream& operator >> (istream& is,pair<T,P> &a){is>>a.first>>a.second; return is;}
using ll=long long;
using ull=unsigned long long;
using int128= __int128_t;
using uint128= __uint128_t;
using pii =pair<int,int>;
const double pi=acos(-1);
const int N=2E6+5;
const ll M=1000000000;
const ll INF_64=0x3f3f3f3f3f3f3f3f;
const int INF_32=0x3f3f3f3f;
const int16_t INF_16=0x3f3f;
const int klog=20;
const int mod=1E9+7;//998244353
const double eps=1E-8;
void gen(){
}
template <class T>
T qpow(T base,ll power){
T re=base,tmp=base;
power--;
while(power){
if(power&(1LL)){
re=re*tmp;re%=mod;
}
power>>=1;
tmp=tmp*tmp;tmp%=mod;
}
return re;
}
int pw[N]{};
int invp[N]{};
int val(int n,int m){
if(n<0||m<0) return 0;
return pw[n+m]*invp[n]%mod*invp[m]%mod;
}
void sol(){
pw[0]=1;
F1(i,N-1) pw[i]=pw[i-1]*i%mod;
F(i,N) invp[i]=qpow(pw[i],mod-2);
int n,m,k;cin>>n>>m>>k;
if(n>m+k){
cout<<"0\n";
return;
}
int an=val(n,m);
int sub=val(n-k-1,m+k+1);
cout<<((an-sub)%mod+mod)%mod<<"\n";
}
int32_t main(){
#ifdef LOCAL
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
#endif // LOCAL
IOS;
int t=1;
gen();
//cin>>t;
FF(i,1,t+1){
//cout<<"Case #"<<i<<": ";
sol();
}
}
|
#include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
#define rep(i,n) for (int i = 0; i < (n); ++i)
using ll = long long;
// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools
int main(){
// Failed to predict input format
int H, W;
cin >> H >> W;
vector<string> S(H);
rep(i, H) cin >> S[i];
int si, sj;
bool last = false;
int ans = 0;
rep(i, H-1) rep(j, W-1) {
bool cur = S[i][j] != S[i+1][j];
if (cur && !last) ans++;
last = cur;
}
last = false;
rep(j, W-1) rep(i, H-1) {
bool cur = S[i][j] != S[i][j+1];
if (cur && !last) ans++;
last = cur;
}
cout << ans << endl;
return 0;
}
| #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <numeric>
#include <cassert>
#define endl '\n'
#define sp ' '
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
string arr[15];
int h,w;
int kk=0;
int gez(int i, int j, int fi , int fj, int pos){//pos 1sag 2sol 3yukari 4 asagi
int ret = 0;
//cerr << i << sp << j << sp << pos << endl;
if(i==fi && j==fj && pos == 3){
if(kk)
return ret;
kk++;
}
if(pos == 1){
if(arr[i+1][j+1] == '#'){
return 1 + gez(i+1,j+1,fi,fj,3);
}
if(arr[i+1][j] == '#'){
return gez(i+1,j,fi,fj,1);
}
//if(arr[i][j-1] == '#'){
// return 1 + gez(i,j-1,fi,fj,4);
// }
return 1 + gez(i,j,fi,fj,4);
}
else if(pos == 2){
if(arr[i-1][j-1] == '#'){
return 1 + gez(i-1,j-1,fi,fj,4);
}
if(arr[i-1][j] == '#'){
return gez(i-1,j,fi,fj,2);
}
//if(arr[i][j+1] == '#'){
// return 1 + gez(i,j,fi,fj,3);
// }
return 1 + gez(i,j,fi,fj,3);
}
else if(pos == 3){
if(arr[i-1][j+1] == '#'){
return 1 + gez(i-1,j+1,fi,fj,2);
}
if(arr[i][j+1] == '#'){
return gez(i,j+1,fi,fj,3);
}
// if(arr[i+1][j] == '#'){
// return 1+gez(i,j,fi,fj,1);
// }
return 1 + gez(i,j,fi,fj,1);
}
else{ // pos == 4
if(arr[i+1][j-1] == '#'){
return 1 + gez(i+1,j-1,fi,fj,1);
}
if(arr[i][j-1] == '#'){
return gez(i,j-1,fi,fj,4);
}
// if(arr[i-1][j] == '#'){
// return 1+gez(i,j,fi,fj,2);
// }
return 1 + gez(i,j,fi,fj,2);
}
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);cerr.tie(nullptr);
cin>>h>>w;
for(int i=0;i<h;i++)
cin>>arr[i];
int fi=-1, fj=-1;
bool flag = false;
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
if(arr[i][j] == '#'){
fi = i;
fj = j;
flag = true;
break;
}
}
if(flag)
break;
}
if(fi == -1 && fj == -1){
cout << 0 << endl;
exit(0);
}
//cerr << fi << sp << fj << endl;
cout << gez(fi,fj,fi,fj,3) << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod = 1000 * 1000 * 1000 + 7;
const int INF = 1e9 + 100;
const ll LINF = 1e18 + 100;
#ifdef DEBUG
#define dbg(x) cout << #x << " = " << (x) << endl << flush;
#define dbgr(s, f) { cout << #s << ": "; for (auto _ = (s); _ != (f); _++) cout << *_ << ' '; cout << endl << flush; }
#else
#define dbg(x) ;
#define dbgr(s, f) ;
#endif
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define fast_io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define endl '\n'
void tcase(void)
{
int n;
string s1, s2, s3;
cin >> n >> s1 >> s2 >> s3;
string ans;
FOR(i, 0, n) ans += '0';
FOR(i, 0, n) ans += '1';
ans += '0';
cout << ans << endl;
}
int32_t main(void)
{
fast_io;
int t;
cin >> t;
while (t--)
tcase();
return 0;
}
| //#define _GLIBCXX_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()
#define rall(v) v.rbegin(), v.rend()
using ll = int64_t;
using ull = uint64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template<class T> using PQ = priority_queue<T>;
template<class T> using PQG = priority_queue<T, vector<T>, greater<T>>;
const int INF = 0xccccccc;
const ll LINF = 0xcccccccccccccccLL;
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);}
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);}
template<typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;}
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;}
//head
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while(t--) {
int n;
cin >> n;
vs s(3);
rep(i, 3) cin >> s[i];
bool u = true;
vi z(3, -1);
rep(i, 3) if(s[i][0] == '0' and s[i][2*n-1] == '0') {
u = false;
z[i] = 0;
}
if(u) {
cout << string(n, '0') + "1" + string(n, '0') << '\n';
continue;
}
u = true;
rep(i, 3) if(s[i][0] == '1' and s[i][2*n-1] == '1') {
u = false;
z[i] = 1;
}
if(u) {
cout << string(n, '1') + "0" + string(n, '1') << '\n';
continue;
}
bool done = false;
rep(i, 3) if(z[i] == -1) {
if(s[i][0] == '0') {
cout << "0" + string(n, '1') + string(n, '0') << '\n';
}
else {
cout << "1" + string(n, '0') + string(n, '1') << '\n';
}
done = true;
}
if(not done) {
cout << "0" + string(n, '1') + string(n, '0') << '\n';
}
}
} |
#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using i64 = int64_t;
using vi64 = vector<i64>;
using vvi64 = vector<vi64>;
using u64 = uint64_t;
#define rep(i, s, n) for (i64 i = (s); i < (n); ++i)
void run() {
i64 n;
cin >> n;
vi64 ps(n);
rep(i, 0, n) {
cin >> ps[i];
--ps[i];
}
vi64 ret{};
i64 p{0};
while (p < n - 1) {
if (ps[p] == p) {
cout << -1 << endl;
return;
}
rep(i, p + 1, n) {
if (ps[i] == p) {
for (i64 j{i - 1}; p <= j; --j) {
if (j < i - 1 && ps[j] != j + 1) {
cout << -1 << endl;
return;
}
ret.push_back(j);
}
ps[i] = ps[i - 1];
p = i;
}
}
}
for (const auto x : ret) {
cout << x + 1 << endl;
}
}
int main() {
cin.tie(nullptr);
cout.precision(std::numeric_limits<double>::max_digits10);
run();
return 0;
}
| // Copyright 2021 Nikita Golikov
#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
using ui64 = uint64_t;
template <class A, class B>
bool smin(A &x, B &&y) {
if (y < x) {
x = y;
return true;
}
return false;
}
template <class A, class B>
bool smax(A &x, B &&y) {
if (x < y) {
x = y;
return true;
}
return false;
}
int main() {
#ifdef GOLIKOV
assert(freopen("in", "rt", stdin));
auto _clock_start = chrono::high_resolution_clock::now();
#endif
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
auto solve = [&](auto&& solve, int n) -> vector<string> {
if (n == 1) {
return {"AB"};
}
auto res = solve(solve, n - 1);
auto resSz = int(res.size());
vector<string> ans;
for (auto const& s : res) {
ans.push_back(s + s);
}
for (auto const& s : res) {
auto is = s;
for (auto& x : is) x = 'A' ^ 'B' ^ x;
ans.push_back(s + is);
}
ans.push_back(string(1 << (n - 1), 'A') + string(1 << (n - 1), 'B'));
return ans;
};
auto ans = solve(solve, n);
cout << ans.size() << '\n';
for (auto const& s : ans) cout << s << '\n';
#ifdef GOLIKOV
cerr << "Executed in " << chrono::duration_cast<chrono::milliseconds>(
chrono::high_resolution_clock::now()
- _clock_start).count() << "ms." << endl;
#endif
return 0;
}
|
#include<bits/stdc++.h>
#pragma GCC optimize(3)
#define ll long long
#define int long long
using namespace std;
const int maxn=200005;
const int inf=1ll<<60;
int a[maxn];
int b[maxn];
int val[maxn];
int f[105][10005];
int vis[105][10005];
signed main() {
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int n,m;
cin>>n>>m;
for(int i=1; i<=n; i++) {
cin>>a[i];
}
int ans=inf;
for(int num=n; num>=1; num--) {
int left=m%num,sum=0;
for(int i=1; i<=n; i++) {
b[i]=a[i]%num;
val[i]=a[i]/num;
sum+=b[i];
}
memset(f,0,sizeof(f));
memset(vis,0,sizeof(vis));
// for(int i=1;i<=num;i++) {
// for(int j=0;j<=sum;j++) {
// vis[i][j]=f[i][j]=0;
// }
// }
for(int i=1; i<=n; i++) {
for(int p=num; p>=2; p--) {
for(int j=sum; j>=b[i]; j--) {
if(vis[p-1][j-b[i]]) {
f[p][j]=max(f[p][j],f[p-1][j-b[i]]+val[i]);
}
}
}
f[1][b[i]]=max(f[1][b[i]],val[i]);
for(int p=num; p>=2; p--) {
for(int j=sum; j>=b[i]; j--) {
vis[p][j]|=vis[p-1][j-b[i]];
}
}
vis[1][b[i]]=1;
}
for(int k=0; k+left<=sum; k+=num) {
if(vis[num][left+k]) {
ans=min(ans,(m-left-k)/num-f[num][left+k]);
}
}
if(1.0*clock()/CLOCKS_PER_SEC>1.82) {
cout<<ans;
return 0;
}
}
cout<<ans;
} | #include <bits/stdc++.h>
using namespace std;
/*
Fix k.
For this k, we will:
Compute the maximum possible sum of k items that is equal to X modulo k.
We will do this by using a DP approach. We go through the items one by one and decide IN ORDER whether we use them.
Our DP state is (# items used, current sum % k, items gone through) -> value.
For each k, we have 10^6 DP states.
The processing for each state is constant time.
So we have at most 10^8 "things" to do.
*/
void maxeq(long long& a, long long b){
a = max(a, b);
}
long long DP[102][101][101];
int main(){
long long n, x;
cin>>n>>x;
vector<long long> A(n);
for (int i = 0; i < n; i++) cin>>A[i];
long long ans = 2e18;
// (# items used, current sum % k, items gone through) -> value.
for (int k = 1; k <= n; k++){
//long long DP[k+1][k][n+1];
for (int a = 0; a <= k+1; a++){
for (int b = 0; b < k; b++){
for (int c = 0; c <= n; c++){
DP[a][b][c] = LLONG_MIN / 3;
}
}
}
DP[0][0][0] = 0;
for (int c = 0; c < n; c++){ // we decide whether to take item c
for (int a = 0; a < k+1; a++){
for (int b = 0; b < k; b++){
// take
maxeq(DP[a + 1][(b + A[c]) % k][c + 1], DP[a][b][c] + A[c]);
// don't take
maxeq(DP[a][b][c + 1], DP[a][b][c]);
}
}
}
if (DP[k][x % k][n] > 0) ans = min(ans, (x - DP[k][x % k][n]) / k);
}
cout << ans << endl;
} |
// time-limit: 2000
// problem-url: https://atcoder.jp/contests/abc202/tasks/abc202_e
#include <bits/stdc++.h>
using namespace std;
#define nl "\n"
#define big int
#ifdef DEBUG
#define ds(statement) cerr << statement << endl
#define dv(var) cerr << (#var) << ": " << var << endl
#else
#define ds(statement)
#define dv(var)
#endif
vector<vector<big>> child;
vector<pair<big, big>> ts;
vector<vector<big>> depth_ent;
big tc = 0;
void dfs(big node, big depth)
{
ts[node].first = tc++;
depth_ent[depth].push_back(ts[node].first);
for (auto c : child[node])
{
dfs(c, depth + 1);
}
ts[node].second = tc++;
}
void solve()
{
big n, t, q;
cin >> n;
child = vector<vector<big>>(n);
depth_ent = vector<vector<big>>(n);
ts = vector<pair<big, big>>(n);
for (big i = 1; i < n; i++)
{
cin >> t;
child[t - 1].push_back(i);
}
dfs(0, 0);
cin >> q;
while (q-- > 0)
{
big d, u, ans;
cin >> u >> d;
u--;
vector<big> v = depth_ent[d];
ans = (lower_bound(v.begin(),v.end(),ts[u].second) - lower_bound(v.begin(),v.end(),ts[u].first));
cout << ans << nl;
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifdef DEBUG
(void)!freopen("/home/sarvjot/error.txt", "w", stderr);
#endif
solve();
return 0;
} | // g++ -std=c++11 a.cpp
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<unordered_map>
#include<utility>
#include<cmath>
#include<random>
#include<cstring>
#include<queue>
#include<stack>
#include<bitset>
#include<cstdio>
#include<sstream>
#include<random>
#include<iomanip>
#include<assert.h>
#include<typeinfo>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define FOR(i,a) for(auto i:a)
#define pb push_back
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
#define show1d(v) {rep(_,v.size())cout<<" "<<v[_];cout<<endl;}
#define show2d(v) {rep(__,v.size())show1d(v[__]);}
using namespace std;
//kaewasuretyuui
typedef long long ll;
#define int ll
typedef int Def;
typedef pair<Def,Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def,pii> pip;
typedef vector<pip>vip;
#define mt make_tuple
typedef tuple<int,int,int> tp;
typedef vector<tp> vt;
typedef vector<vt>vvt;
template<typename A,typename B>bool cmin(A &a,const B &b){return a>b?(a=b,true):false;}
template<typename A,typename B>bool cmax(A &a,const B &b){return a<b?(a=b,true):false;}
const double PI=acos(-1);
const long double EPS=1e-9;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9+10;
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
#define yes cout<<"Yes\n"
#define no cout<<"No\n"
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin>>n;
vi a(n),b(n);
rep(i,n)cin>>a[i];
rep(i,n)cin>>b[i];
int s=0;
rep(i,n)s+=a[i]*b[i];
if(s)no;
else yes;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll P=1000000007;
ll pow(ll x,ll n){
if(n==1)return x;
ll half=pow(x,n/2),ret=half*half%P;
if(n%2)ret=ret*x%P;
return ret;
}
ll inv(ll x){
return pow(x,P-2);
}
vector<vector<ll>> matmul(const vector<vector<ll>> &a,const vector<vector<ll>> &b){
int n=a.size(),p=a[0].size(),m=b[0].size();
vector<vector<ll>> c(n,vector<ll>(m,0));
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
for(int k=0;k<p;k++)
c[i][j]=(c[i][j]+a[i][k]*b[k][j])%P;
return c;
}
vector<vector<ll>> matpow(const vector<vector<ll>> &a,int n){
if(n==1)return a;
if(n==2)return matmul(a,a);
if(n==3)return matmul(matmul(a,a),a);
vector<vector<ll>> half=matpow(a,n/2),ret=matmul(half,half);
if(n%2)ret=matmul(ret,a);
return ret;
}
int main() {
int n,m,k;
cin>>n>>m>>k;
ll inv2m=inv(m)*inv(2)%P;
vector<ll> a(n);
for(ll &x:a)
cin>>x;
if(k==0){
for(ll x:a)cout<<x<<endl;
return 0;
}
vector<vector<ll>> mat(n,vector<ll>(n,0));
for(int i=0;i<n;i++)
mat[i][i]=1;
for(int i=0;i<m;i++){
int u,v;
cin>>u>>v;
u--;
v--;
mat[u][u]=(mat[u][u]-inv2m+P)%P;
mat[u][v]=(mat[u][v]+inv2m)%P;
mat[v][u]=(mat[v][u]+inv2m)%P;
mat[v][v]=(mat[v][v]-inv2m+P)%P;
}
mat=matpow(mat,k);
for(int i=0;i<n;i++){
ll ans=0;
for(int j=0;j<n;j++)
ans=(ans+mat[i][j]*a[j])%P;
cout<<ans<<endl;
}
} | //
// mod.cpp
//
//
// Created by Sagar Singh on 17/12/20.
//
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <ctime>
#include <algorithm>
#include <numeric>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <iterator>
#include <functional>
#include <bitset>
#include <unordered_map>
#include <unordered_set>
#define bug1( x ) { cerr << (#x) <<"="<< x << endl; }
#define bug2( x , y ) { cerr << (#x) <<"="<< (x) << " " << (#y) << "="<< (y) << endl; }
#define bug3( x , y , z ) { cerr << (#x) <<"="<<(x) << " " << (#y) <<"="<< (y) << " " << (#z) <<"="<< (z) << endl; }
#define bug4( x , y , z , w) { cerr << (#x) <<"="<<(x) << " " << (#y) <<"="<< (y) << " " << (#z) <<"="<< (z) << " " << (#w) <<"="<< w << endl; }
#define bug5( x , y , z , w ,p) { cerr << (#x) <<"="<<(x) << " " << (#y) <<"="<< (y) << " " << (#z) <<"="<< (z) << " " << (#w) <<"="<< w << " " << (#p) <<"="<< p << endl; }
#define bug6( x , y , z , w ,p , q) { cerr << (#x) <<"="<<(x) << " " << (#y) <<"="<< (y) << " " << (#z) <<"="<< (z) << " " << (#w) <<"="<< w << " " << (#p) <<"="<< p << " " << (#q) <<"="<< q << endl; }
#define bugn( x , n ) { cerr<<(#x)<<":";for(int i=0;i<n;i++)cout<<(#x)<<"["<<i<<"]:"<< x[i] << " "; cout<<endl; }
#define bugnm( x , n , m ) { cerr<<(#x)<<endl;for(int i=0;i<n;i++){ cout<<"Row #"<<i<<":"; for(int j=0;j<m;j++) cout<<x[i][j]<<" "; cout << endl;} }
typedef unsigned long long ul;
typedef long double ld;
typedef long long ll;
using namespace std;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
const int mod = 1e9+7;
int add(int x,int y){
int z = x + y;
if( z >= mod ){
z -= mod;
}
return z;
}
int sub(int x,int y){
int z = x - y;
if( z < 0 ){
z += mod;
}
return z;
}
int mul(int x,int y){
ll z = 1ll * x * y;
if( z >= mod ){
z %= mod;
}
return z;
}
int binpow(int a,ll p){
int r = 1;
while( p ){
if( p & 1 )
r = mul(r,a);
a = mul(a,a), p >>= 1;
}
return r;
}
int n, m, k;
vvi mul(vvi a, vvi b){
vvi c(n,vi(n));
for(int i=0;i<n;++i){
for(int j=0;j<n;++j){
for(int k=0;k<n;++k){
c[i][j] = add(c[i][j],mul(a[i][k],b[k][j]));
}
}
}
return c;
}
vvi binpow(vvi a,int p){
vvi r(n,vi(n));
for(int i=0;i<n;++i){
r[i][i] = 1;
}
while( p ){
if( p & 1 )
r = mul(r,a);
a = mul(a,a), p >>= 1;
}
return r;
}
int main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> m >> k;
vvi w(n,vi(n));
vi a(n);
for(int i=0;i<n;++i){
cin >> a[i];
}
for(int i=0;i<m;++i){
int u, v; cin >> u >> v;
w[u-1][v-1] += 1;
w[v-1][u-1] += 1;
}
for(int i=0;i<n;++i){
int k = 0;
for(int j=0;j<n;++j){
k += w[i][j];
}
w[i][i] += 2*m - k;
}
w = binpow(w,k);
for(int j=0;j<n;++j){
int tmp = 0;
for(int i=0;i<n;++i){
tmp = add(tmp,mul(a[i],w[i][j]));
}
tmp = mul(tmp,binpow(mul(binpow(2,k),binpow(m,k)),mod-2));
cout << tmp << endl;
}
}
|
#define LOCAL
#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,s,n) for (int i = (ll)s; i < (ll)n; i++)
#define rrep(i,n,e) for (int i = (ll)n; i > (ll)e; i--)
#define ll long long
#define ld long double
#define pb push_back
#define eb emplace_back
#define All(x) x.begin(), x.end()
#define Range(x, i, j) x.begin() + i, x.begin() + j
// #define M_PI 3.14159265358979323846 // CF
#define deg2rad(deg) ((((double)deg)/((double)360)*2*M_PI))
#define rad2deg(rad) ((((double)rad)/(double)2/M_PI)*(double)360)
#define Find(set, element) set.find(element) != set.end()
#define Decimal(x) cout << fixed << setprecision(10) << x << endl; // print Decimal number 10 Rank
#define endl "\n"
#define Case(x) printf("Case #%d: ", x); // gcj
typedef pair<int, int> PI;
typedef pair<ll, ll> PLL;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<vector<vector<ll>>> vvvl;
typedef vector<PI> vpi;
typedef vector<vector<PI>> vvpi;
typedef vector<PLL> vpl;
typedef vector<vector<PLL>> vvpl;
typedef vector<char> vch;
typedef vector<vector<char>> vvch;
constexpr ll INF = 1001002003004005006ll;
constexpr int n_max = 2e5+10;
template<class T>
inline bool chmax(T &a, T b) { if(a<b) { a=b; return true; } return false; };
template<class T>
inline bool chmin(T &a, T b) { if(a>b) { a=b; return true; } return false; };
template<class T, class U>
T POW(T x, U n) {T ret=1; while (n>0) {if (n&1) {ret*=x;} x*=x; n>>=1;} return ret;};
// debug
template <typename A, typename B>
string to_string(pair<A, B> p);
string to_string(const string &s) {return '"' + s + '"';};
string to_string(const char c) {return to_string((string) &c);};
string to_string(bool b) {return (b ? "true" : "false");};
template <size_t N>
string to_string(bitset<N> v){
string res = "";
for(size_t i = 0; i < N; i++) res += static_cast<char>('0' + v[i]);
return res;
};
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for(const auto &x : v) {
if(!first) res += ", ";
first = false; res += to_string(x);
}
res += "}";
return res;
};
template <typename A, typename B>
string to_string(pair<A, B> p){return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}
void debug_out() {cerr << endl;};
template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); };
void LINE_OUT() {
cout << "--------------" << endl;
};
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define LINE LINE_OUT();
#else
#define debug(...) 71
#define LINE 71;
#endif
void print() { cout << endl; }
template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
cout << head;
if (sizeof...(tail) != 0) cout << " ";
print(forward<Tail>(tail)...);
};
template <class T>
void print(vector<T> &vec) {
for (auto& a : vec) {
cout << a;
if (&a != &vec.back()) cout << " ";
}
cout << endl;
};
template <class T>
void print(vector<vector<T>> &df) {
for (auto& vec : df) {
print(vec);
}
};
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N, K; cin >> N >> K;
rep(i,0,K) {
if (N%200 == 0) {
N /= 200;
} else {
N = N*1000+200;
}
}
cout << N << endl;
return 0;
};
| #include "bits/stdc++.h"
using namespace std;
int main() {
long long n, k;
cin >> n >> k;
for (int i = 0; i < k; i++) {
if (n % 200 == 0) {
n /= 200;
}
else {
n *= 1000;
n += 200;
}
}
cout << n << endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long int into;
int main()
{ int h,w,x,y;
cin>>h>>w>>x>>y;
x--;y--;
string s[h];
for(int i=0;i<h;i++)
cin>>s[i];
int ans=0;
for(int i=x+1;i<h;i++)
{ if(s[i][y]=='#')
break;
else ans++; }
for(int i=x-1;i>=0;i--)
{ if(s[i][y]=='#')
break;
else ans++;
} for(int i=y+1;i<w;i++)
{ if(s[x][i]=='#')
break;
else ans++;
}
for(int i=y-1;i>=0;i--)
{ if(s[x][i]=='#')
break;
else
ans++;}
ans++;
cout<<ans<<endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define reps(i, a, n) for (int i = (a); i < (n); ++i)
#define rep(i, n) reps(i, 0, n)
#define deps(i, a, n) for (int i = (a); i >= (n); --i)
#define dep(i, n) deps(i, n, 0)
int si, sj;
int t[52][52];
int p[52][52];
int c[52][52];
int score;
vector<char> ans;
int cnt;
void dfs(int i, int j, int score2, vector<char> ans2)
{
if (cnt++ > 150000)
{
//cout << score << endl;
for (char x : ans) cout << x; cout << endl;
exit(EXIT_SUCCESS);
}
score2 += p[i][j];
if (score < score2)
{
score = score2;
ans = ans2;
}
c[i][j] = 1;
if (t[i][j] == t[i+1][j]) c[i+1][j] = 1;
else if (t[i][j] == t[i-1][j]) c[i-1][j] = 1;
else if (t[i][j] == t[i][j+1]) c[i][j+1] = 1;
else if (t[i][j] == t[i][j-1]) c[i][j-1] = 1;
if (c[i+1][j] == 0) {ans2.push_back('D'); dfs(i+1, j, score2, ans2); ans2.erase(ans2.end()-1);}
if (c[i-1][j] == 0) {ans2.push_back('U'); dfs(i-1, j, score2, ans2); ans2.erase(ans2.end()-1);}
if (c[i][j+1] == 0) {ans2.push_back('R'); dfs(i, j+1, score2, ans2); ans2.erase(ans2.end()-1);}
if (c[i][j-1] == 0) {ans2.push_back('L'); dfs(i, j-1, score2, ans2); ans2.erase(ans2.end()-1);}
c[i][j] = 0;
if (t[i][j] == t[i+1][j]) c[i+1][j] = 0;
else if (t[i][j] == t[i-1][j]) c[i-1][j] = 0;
else if (t[i][j] == t[i][j+1]) c[i][j+1] = 0;
else if (t[i][j] == t[i][j-1]) c[i][j-1] = 0;
}
signed main(void)
{
rep (i, 52) c[0][i] = c[51][i] = c[i][0] = c[i][51] = 1;
cin >> si >> sj;
reps (i, 1, 51) reps (j, 1, 51) cin >> t[i][j];
reps (i, 1, 51) reps (j, 1, 51) cin >> p[i][j];
c[si][sj] = 1;
if (t[si][sj] == t[si+1][sj]) c[si+1][sj] = 1;
else if (t[si][sj] == t[si-1][sj]) c[si-1][sj] = 1;
else if (t[si][sj] == t[si][sj+1]) c[si][sj+1] = 1;
else if (t[si][sj] == t[si][sj-1]) c[si][sj-1] = 1;
vector<char> tmp;
dfs(si+1, sj+1, 0, tmp);
for (char x : ans) cout << x; cout << endl;
}
|
#include <bits/stdc++.h>
#define fo(a,b,c) for (a=b; a<=c; a++)
#define fd(a,b,c) for (a=b; a>=c; a--)
#define min(a,b) (a<b?a:b)
#define ll long long
//#define file
using namespace std;
int a[100002],b[100002],n,L,i,j,k,l,sum;
ll ans;
void Exit() {printf("-1\n");exit(0);}
int main()
{
#ifdef file
freopen("c.in","r",stdin);
#endif
scanf("%d%d",&n,&L);
fo(i,1,n) scanf("%d",&a[i]);
fo(i,1,n) scanf("%d",&b[i]);
a[0]=0,a[n+1]=L+1;
b[0]=0,b[n+1]=L+1;
l=0;
fo(i,0,n)
if (b[i]+1<b[i+1])
{
sum=0;
while (l<=n && a[l]+1==a[l+1]) ++l;
k=min(l,i);
while (l<=n && sum<b[i+1]-b[i]-1)
sum+=a[l+1]-a[l]-1,++l;
if (sum>b[i+1]-b[i]-1) Exit();
ans+=max(l,i+1)-k-1;
}
printf("%lld\n",ans);
fclose(stdin);
fclose(stdout);
return 0;
} | #include <bits/stdc++.h>
#define FASTIO
using namespace std;
using ll = long long;
using Vi = std::vector<int>;
using Vl = std::vector<ll>;
using Pii = std::pair<int, int>;
using Pll = std::pair<ll, ll>;
constexpr int I_INF = std::numeric_limits<int>::max();
constexpr ll L_INF = std::numeric_limits<ll>::max();
template <typename T1, typename T2>
inline bool chmin(T1& a, const T2& b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
inline bool chmax(T1& a, const T2& b) {
if (a < b) {
a = b;
return true;
}
return false;
}
class Prints {
private:
class __Prints {
public:
__Prints(const char* sep, const char* term) : sep(sep), term(term) {}
template <class... Args>
void operator()(const Args&... args) const { print(args...); }
template <typename T>
void pvec(const T& vec, size_t sz) const {
for (size_t i = 0; i < sz; i++)
std::cout << vec[i] << (i == sz - 1 ? term : sep);
}
template <typename T>
void pmat(const T& mat, size_t h, size_t w) {
for (size_t i = 0; i < h; i++)
for (size_t j = 0; j < w; j++)
std::cout << mat[i][j] << (j == w - 1 ? term : sep);
}
private:
const char *sep, *term;
void print() const { std::cout << term; }
void print_rest() const { std::cout << term; }
template <class T, class... Tail>
void print(const T& head, const Tail&... tail) const { std::cout << head, print_rest(tail...); }
template <class T, class... Tail>
void print_rest(const T& head, const Tail&... tail) const { std::cout << sep << head, print_rest(tail...); }
};
public:
Prints() {}
__Prints operator()(const char* sep = " ", const char* term = "\n") const { return __Prints(sep, term); }
};
Prints prints;
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void solve() {
ll N, L;
cin >> N >> L;
Vl A(N + 2), B(N + 2);
for (ll i = 1; i <= N; i++) {
cin >> A[i];
A[i] -= i;
}
for (ll i = 1; i <= N; i++) {
cin >> B[i];
B[i] -= i;
}
A[N + 1] = B[N + 1] = (L + 1) - (N + 1);
Vl pos = B;
pos.erase(unique(pos.begin(), pos.end()), pos.end());
ll ans = 0;
for (auto p : pos) {
int idxl_a = lower_bound(A.begin(), A.end(), p) - A.begin();
int idxr_a = upper_bound(A.begin(), A.end(), p) - A.begin();
int idxl_b = lower_bound(B.begin(), B.end(), p) - B.begin();
int idxr_b = upper_bound(B.begin(), B.end(), p) - B.begin();
if (idxr_a - idxl_a == 0) {
prints()(-1);
return;
}
ans += max(idxr_b - idxr_a, 0) + max(idxl_a - idxl_b, 0);
}
prints()(ans);
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
int main() {
#ifdef FASTIO
std::cin.tie(nullptr), std::cout.tie(nullptr);
std::ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
std::ifstream ifs("./in_out/input.txt");
std::cin.rdbuf(ifs.rdbuf());
#endif
#ifdef FILEOUTPUT
std::ofstream ofs("./in_out/output.txt");
std::cout.rdbuf(ofs.rdbuf());
#endif
std::cout << std::setprecision(18) << std::fixed;
solve();
std::cout << std::flush;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define loop(i, a, n) for (int i = a; i < n; i++)
#define loope(i, b, n) for (int i = b; i <= n; i++)
#define loopit(a) for (auto it = a.begin(); it != a.end(); it++)
#define ms(a, b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define MP make_pair
#define pi pair<int, int>
#define ff first
#define ss second
#define bloop(i, a, b) for (int i = a; i > b; i--)
#define bloope(i, a, b) for (int i = a; i >= b; i--)
#define PQ priority_queue<int> pq;
#define vi vector<int>
#define vii vector<vector<int>>
#define si set<int>
#define NO cout<<"NO\n";
#define YES cout<<"YES\n";
#define MPQ priority_queue<pi, vector<int>, greater<pi>> mpq;
#define io \
ios_base::sync_with_stdio(0); \
cin.tie(NULL); \
// #include < ext / pb_ds / assoc_container.hpp >
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// typedef tree<int, null_type, less<int>, rb_tree_tag,
// tree_order_statistics_node_update>
// ordered_set;
ll ceil(ll a, ll b) {
return (a + b - 1) / b;
}
//graph theory
vector<vi> adj;
vi vis;
int dfs(int cur) {
int ret = 1;
vis[cur] = 1;
// cout << cur << ",";
for (const auto &v : adj[cur]) {
if (vis[v])continue;
ret += dfs(v);
}
return ret;
}
const int maxm = 2 * 1e5 + 1;
void solve()
{
int n; cin >> n;
vi a(n);
loop(i, 0, n)cin >> a[i];
adj = vector<vi>(maxm + 1);
vis = vi(maxm + 1);
int ans = 0;
loop(i, 0, n / 2) {
if (a[i] == a[n - i - 1])continue;
adj[a[i]].pb(a[n - i - 1]);
adj[a[n - i - 1]].pb(a[i]);
}
loop(i, 0, n) {
if (vis[a[i]])continue;
ans += dfs(a[i]) - 1;
// cout << "\n";
// cout << a[i] << " " << ans << "\n";
}
cout << ans << "\n";
}
int main()
{
io;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int test = 1, i = 0;
// cin >> test;
while (i++ != test)
{
// cout<<"Case #"<<i<<": ";
solve();
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
#define ll long long
vector<bool>vis ( 2e5+10 ,false);
vector<int>adj[200009];
void dfs ( int a){
if(vis[a]==false)return;
vis[a]=false;
for ( int x : adj[a]){
dfs(x);
}
}
void solve()
{
int n;
cin >> n;
vector<int> v(n);
set<int>st;
for (int i = 0; i < n; i++)
{
cin >> v[i];
st.insert(v[i]);
vis[v[i]]=true;
}
int ans =st.size();
int i=0,j=n-1;
while ( i< j){
adj[v[i]].push_back(v[j]);
adj[v[j]].push_back(v[i]);
i++;
j--;
}
for ( int i=1 ; i<=200000; i++){
if(vis[i]==true){
ans--;
dfs(i);
}
}
cout << ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define fo(i,a,b) for(int i = a; i<b ; i++)
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define pb push_back
#define M 1000000007
int mod(int x){
return ((x%M + M)%M);
}
int add(int a,int b){
return mod(mod(a)+mod(b));
// return a*1ll + b*1ll;
}
int mul(int a,int b){
return mod(mod(a)*mod(b));
// return a*b*1ll;
}
// ****************************************************************************
pair<int,int> dp[2][100001];
int n;
int a[100001];
int res;
pair<int,int> rec(int f,int i){
if(i==n)
return {0ll,1ll};
// if(dp[f][i]!={-1ll,-1ll})
// return dp[f][i];
auto p = dp[f][i];
if(p.first!=-1)
return p;
// int temp;
if(i==0){
int x = rec(0,i+1).first;
int y = rec(0,i+1).second;
// return {add(a,mul(a[i]))}
// return add({x,mul(a[i],y)});
return {add(x,mul(a[i],y)),y};
}
int ans =0;
if(f==0){
// now we can put both
// int temp = rec(0,i+1);
// int tempp = rec(1,i+1);
// res=add(res,mul(a[i],temp));
// res=add(res,-mul(a[i],tempp));
// // int x = mul(a[i],tempp);
// // res = (res-x)%M;
// // res=add(res,-x);
// // res = mod(mod(res)-x);
// return dp[f][i]=add(temp,tempp);
int ans;
int x1 = rec(0,i+1).first;
int y1 = rec(0,i+1).second;
ans = add(x1,mul(a[i],y1));
int x2 = rec(1,i+1).first;
int y2 = rec(1,i+1).second;
ans=add(ans,x2);
ans=add(ans,-mul(a[i],y2));
return dp[f][i]={ans,add(y1,y2)};
}
else{
// now i can only put +here
// ans=mul(a[i],rec(0,i+1));
// int temp = rec(0,i+1);
// res=add(res,mul(a[i],temp));
// return dp[f][i]=temp;
int x = rec(0,i+1).first;
int y = rec(0,i+1).second;
return dp[f][i]={add(x,mul(a[i],y)),y};
// return dp[f][i]={add}
}
// cout<<ans<<endl;
return {1,1};
}
void solve(){
cin>>n;
fo(i,0,n)
cin>>a[i];
// memset(dp,-1,sizeof dp);
// cout<<rec(0,0);
fo(i,0,2){
fo(j,0,n)
dp[i][j]={-1,-1};
}
// cout<<rec(0,0).first;
auto p = rec(0,0);
cout<<p.first;
// cout<<mod(res);
// cout<<res;
}
signed main()
{
FIO
int t;
t=1;
// cin>>t;
while(t--)
{
solve();
}
} | #include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<utility>
#include<set>
using namespace std;
#define int long long
#define endl "\n"
constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007;
struct fast_io {
fast_io(){
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
};
} fio;
set<int> s;
void rec(int B, int C){
if(C < 0) return ;
s.insert(B);
rec(-B, C - 1);
rec(B-1, C - 2);
}
int solve(int B, int C){
s.clear();
rec(B, C);
cout<<"ANS = "<<endl;
cout<<s.size()<<endl<<endl;
for(int y : s){
// cout<<y<<endl;
cout<<y<<" ";
}
cout<<endl;
}
int solve2(int B, int C){
int a = 0, b = 0, c = 0;
int D, E, F, G;
int res = 0;
int maxi = 0;
a = (C - 1)/ 2;
b = max((C - 2)/2, 0ll);
c =(C + 0)/2;
if(B == 0) {
cout<<C<<endl;
return 0;
} else if(B < 0) {
D = -B + a;
E = -B - a;
F = B + b;
G = B - c;
} else {
D = B + b;
E = B - c;
F = -B + a;
G = -B - a;
}
// cout<<D<<" "<<E<<" "<<F<<" "<<G<<endl;
// cout<<G<<" "<<F<<" "<<E<<" "<<D<<endl;
res += F - G + 1;
res += max(0ll, D - max(E-1, F));
// cout<<a<<" "<<b<<endl;
cout<<res<<endl;
return res;
}
signed main(){
cout<<fixed<<setprecision(10);
int B, C;
cin>>B>>C;
// solve(B, C);
solve2(B, C);
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
string s = "";
map<char, char> mp;
int main(){
mp['0'] = '0' ; mp['1'] = '1' ; mp['6'] = '9' ; mp['8'] = '8' ; mp['9'] = '6' ;
cin >> s;
for(int i = s.length() - 1 ; i >= 0 ; i --) cout << mp[s[i]];
return 0;
}
| #include <bits/stdc++.h>
#include <chrono>
#include <math.h>
using namespace std;
using ll = long long;
using ld = long double;
using namespace std::chrono;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define fo(i,z,n) for(int i = z;i<n;i++)
#define rfo(i,z,n) for(int i = z;i >= n;i--)
#define Fo(i,z,n) for(ll i = z;i<n;i++)
#define INT_BITS 32
#define mod 1000000007
#define vi vector<int>
#define vc vector<char>
#define vpi vector<pair<int,int> >
#define vll vector<ll>
#define vvi vector<vector<int> >
#define vvpi vector<vector<pair<int,int> > >
#define vvc vector<vector<char> >
#define vvl vector<vector<ll> >
#define pb push_back
#define f first
#define int long long
#define float double
#define all(c) (c).begin(), (c).end()
#define endl '\n'
#define pi 3.14159265358979323846
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
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
ll power(ll a,ll p,ll m=mod)
{ll res=1;while(p>0){if(p&1)res=(res*a)%m;a=(a*a)%m;p>>=1;}
return res;}
int ceil(int a,int b){
if(a%b == 0)return a/b;
else return a/b+1;
}
int pow(int a,int b){
if(b == 0)return 1;
int res = pow(a,b/2);
res = res*res;
if(b&1)res *= a;
return res;
}
// ordered_set s;
void solve(){
string s;
cin >> s;
int n = s.length();
reverse(all(s));
for(int i = 0;i < n;i++){
if(s[i] == '6')s[i] = '9';
else if(s[i] == '9')s[i] = '6';
}
cout << s << endl;
return;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);
int t = 1;
// cin >> t;
while(t--){
solve();
}
return 0;
} |
#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
#define DEBUG(x) cerr<<#x<<": "<<x<<endl;
#define DEBUG_VEC(v) cerr<<#v<<":";for(int i=0;i<v.size();i++) cerr<<" "<<v[i]; cerr<<endl;
#define DEBUG_MAT(v) cerr<<#v<<endl;for(int i=0;i<v.size();i++){for(int j=0;j<v[i].size();j++) {cerr<<v[i][j]<<" ";}cerr<<endl;}
typedef long long ll;
// #define int ll
#define vi vector<int>
#define vl vector<ll>
#define vii vector< vector<int> >
#define vll vector< vector<ll> >
#define vs vector<string>
#define pii pair<int,int>
#define pis pair<int,string>
#define psi pair<string,int>
#define pll pair<ll,ll>
template<class S, class T> pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first + t.first, s.second + t.second); }
template<class S, class T> pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first - t.first, s.second - t.second); }
template<class S, class T> ostream& operator<<(ostream& os, pair<S, T> p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
#define X first
#define Y second
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define rrep1(i,n) for(int i=(int)(n);i>0;i--)
#define REP(i,a,b) for(int i=a;i<b;i++)
#define in(x, a, b) (a <= x && x < b)
#define all(c) c.begin(),c.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; }
#define UNIQUE(v) v.erase(std::unique(v.begin(), v.end()), v.end());
const ll inf = 1000000001;
const ll INF = (ll)1e18 + 1;
const long double pi = 3.1415926535897932384626433832795028841971L;
#define Sp(p) cout<<setprecision(25)<< fixed<<p<<endl;
// int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
// int dx2[8] = { 1,1,0,-1,-1,-1,0,1 }, dy2[8] = { 0,1,1,1,0,-1,-1,-1 };
vi dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};
// vi dx2 = { 1,1,0,-1,-1,-1,0,1 }, dy2 = { 0,1,1,1,0,-1,-1,-1 };
#define fio() cin.tie(0); ios::sync_with_stdio(false);
const ll MOD = 1000000007;
// const ll MOD = 998244353;
// #define mp make_pair
//#define endl '\n'
void solve() {
string s;
cin >> s;
int n = s.size();
string t = "atcoder";
int ans = inf;
int off = 0;
for (int i = 0; i <= min(s.size(), t.size()); i++) {
if (i > 0) {
bool flag = false;
for (int j = i - 1; j < s.size(); j++) {
if (s[j] == t[i - 1]) {
s.erase(s.begin() + j);
s.insert(s.begin() + i - 1, t[i - 1]);
off += j - (i - 1);
flag = true;
break;
}
}
if (not flag) break;
}
if (i == t.size()) {
if (s.size() > t.size()) {
chmin(ans, off);
}
break;
}
for (int j = i; j < s.size(); j++) {
if (s[j] > t[i]) {
chmin(ans, off + j - i);
break;
}
}
}
if (ans != inf) {
cout << ans << endl;
}
else {
cout << -1 << endl;
}
}
signed main() {
fio();
int t;
cin >> t;
while (t--) solve();
} | #include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<set>
#include<map>
#include<utility>
#include<queue>
#include<vector>
#include<stack>
#include<sstream>
#include<algorithm>
/************************************************/
#define rep(i,n) for(int i=0;i<n;i++)
#define m_p make_pair
#define pb push_back
#define fr first
#define se second
#define ford(i,n) for(int i=n-1;i>=0;i--)
#define forn(i,a,n) for(int i=a;i<n;i++)
#define foreach(i,c) for(__typeof(c.begin())i=(c.begin());i!=(c).end();i++)
#define pii pair<int,int>
#define vi vector<int>
#define ll long long
#define vll vector<ll>
#define sz(s) (int)(s.size())
#define all(s) s.begin(),s.end()
#define zero(x) memset(x,0,sizeof(x))
#define vii vector<pair<int,int> >
#define mpis map<int,string>
#define mpii map<int,int>
#define mpsi map<string,int>
#define re return
#define mod 1000000007
/************************************************/
using namespace std;
long long get(){
char c=getchar();
long long x=0LL;
while(c<'0'||c>'9')
c=getchar();
while(c>='0'&&c<='9'){
x*=10LL;
x+=(c-'0');
c=getchar();
}
return x;
}
string i_t_s(int x){
string t="";
while(x){
t+=x%10+'0';
x/=10;
}
reverse(all(t));
re t;
}
int s_t_i(string t){
int x=0;
rep(i,sz(t)){
x=x*10+(t[i]-'0');
}
re x;
}
ll q_p(ll x,ll y){
ll res=1;
x%=mod;
while(y){
if(y%2){
res=res*x;
res%=mod;
}
y/=2;
x=x*x;
x%=mod;
}
re res;
}
int n;
pair<int,string> p[1001];
int main(){
ios::sync_with_stdio(0);
cin>>n;
rep(i,n){
cin>>p[i].se>>p[i].fr;
}
sort(p,p+n);
reverse(p,p+n);
cout<<p[1].second;
re 0;
}
/*
ๆฃๆฅๅพช็ฏๆฏrep(i,n)่ฟๆฏrep(i,m)!!
long longๆ่ฆ็ๆถ้ดๆฏint้ฟ!!
ๆฒกๆlong longๅฟ
่ฆไธๅlong long!!
ๅๅ
ฌๅผๅๅ
ๆณไธๆณ!!
ไบๅๅ็ไธ็่พน็ๅฏนไธๅฏน๏ผๆๆฒกๆ็นๆฎๅผ!!
ๆไบคๅ่ฆๆต็นๆฎๆฐๆฎ๏ผ่พน็ไธ็๏ผ!!
*/
|
#include <bits/stdc++.h>
using namespace std;
//#define int long long
template <int mod = 1000000007>
struct modint{
int x;
modint(int y = 0) : x((y % mod + mod) % mod) { }
friend modint operator^(modint a, long long b) {
modint r = 1;
for(; b; b >>= 1, a *= a) if(b & 1) r *= a;
return r;
}
friend modint operator-(modint a) {return modint(0) - a;}
friend modint operator!(modint a) {return a ^ (mod - 2);}
modint& operator/=(modint const& b) {return *this *= !b;}
friend bool operator==(modint a, modint b) {return a.x == b.x;}
friend bool operator!=(modint a, modint b) {return a.x != b.x;}
friend modint operator+(modint a, modint const& b) {return a += b;}
friend modint operator-(modint a, modint const& b) {return a -= b;}
friend modint operator*(modint a, modint const& b) {return a *= b;}
friend modint operator/(modint a, modint const& b) {return a /= b;}
modint& operator*=(modint const& b) {x = 1LL * x * b.x % mod; return *this;}
friend ostream& operator<<(ostream& os, modint const& a) {return os << a.x;}
modint& operator+=(modint const& b) {x += b.x; x = (x >= mod) ? x - mod : x;return *this;}
modint& operator-=(modint const& b) {x = x >= b.x ? x - b.x : x - b.x + mod;return *this;}
};
using mint = modint <>;
mint nCr(int n, int m) {
if(m > n) return mint(0);
mint p = 1, q = 1;
for(int i = n - m + 1; i <= n; i++) p *= i;
for(int i = 1; i <= m; i++) q *= i;
return p / q;
}
int32_t main() {
ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, m, sum = 0; cin >> n >> m;
vector <int> v(n + 1);
for(int i = 1; i <= n; i++) {
cin >> v[i];
sum += v[i];
}
if(sum > m) {
cout << 0 << endl;
return 0;
}
mint a = nCr(m + n, (sum + n));
cout << a << endl;
} | #include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const ll mod = 998244353;
vector<ll> fac(300001); //n!(mod mod)
vector<ll> ifac(300001); //k!^{M-2} (mod mod)
//a,bใฎ็ฏๅฒ็ใซใใใ ใ้
ๅใ็จๆใใฆใใใฐๅๅ
ll mpow(ll x, ll n){ //x^n(mod mod) โๆฎ้ใซpow(x,n)ใงใฏๆบขใใฆใใพใใใ๏ผ้ๆmod่จ็ฎ
ll ans = 1;
while(n != 0){
if(n&1) ans = ans*x % mod;
x = x*x % mod;
n = n >> 1;
}
return ans;
}
ll comb(ll a, ll b){ //aCbใmod่จ็ฎ
if(a == 0 && b == 0)return 1;
if(a < b || a < 0)return 0;
ll tmp = ifac[a-b]* ifac[b] % mod;
return tmp * fac[a] % mod;
}
// union by size + path having
class UnionFind {
public:
vector <ll> par; // ๅๅ
ใฎ่ฆชใ่กจใ้
ๅ
vector <ll> siz; // ็ด ้ๅใฎใตใคใบใ่กจใ้
ๅ(1 ใงๅๆๅ)
// Constructor
UnionFind(ll sz_): par(sz_), siz(sz_, 1LL) {
for (ll i = 0; i < sz_; ++i) par[i] = i; // ๅๆใงใฏ่ฆชใฏ่ชๅ่ช่บซ
}
void init(ll sz_) {
par.resize(sz_);
siz.assign(sz_, 1LL); // resize ใ ใจใชใใๅๆๅใใใชใใฃใ
for (ll i = 0; i < sz_; ++i) par[i] = i; // ๅๆใงใฏ่ฆชใฏ่ชๅ่ช่บซ
}
// Member Function
// Find
ll root(ll x) { // ๆ นใฎๆค็ดข
while (par[x] != x) {
x = par[x] = par[par[x]]; // x ใฎ่ฆชใฎ่ฆชใ x ใฎ่ฆชใจใใ
}
return x;
}
// Union(Unite, Merge)
bool merge(ll x, ll y) {
x = root(x);
y = root(y);
if (x == y) return false;
// merge technique๏ผใใผใฟๆง้ ใใใผใธใใใใฏ๏ผๅฐใๅคงใซใใฃใคใใ๏ผ
if (siz[x] < siz[y]) swap(x, y);
siz[x] += siz[y];
par[y] = x;
return true;
}
bool issame(ll x, ll y) { // ้ฃ็ตๅคๅฎ
return root(x) == root(y);
}
ll size(ll x) { // ็ด ้ๅใฎใตใคใบ
return siz[root(x)];
}
};
int main() {
int N, K; cin >> N >> K;
int a[N][N];
rep(i, N){
rep(j, N){
cin >> a[i][j];
}
}
fac[0] = 1;
ifac[0] = 1;
for(ll i = 0; i<300000; i++){
fac[i+1] = fac[i]*(i+1) % mod; // n!(mod mod)
ifac[i+1] = ifac[i]*mpow(i+1, mod-2) % mod; // k!^{mod-2} (mod mod) โ็ดฏไนใซmpowใๆก็จ
}
ll ans = 1;
UnionFind Ux(N);
for (int i = 0; i < N-1; i++){
for (int j = i+1; j < N; j++){
bool check = true;
for (int k = 0; k < N; k++){
if(a[i][k] + a[j][k] > K){
check = false;
break;
}
}
if(check){
Ux.merge(i, j);
}
}
}
unordered_set<int> setx;
for (int i = 0; i < N; i++){
int x = Ux.root(i);
if(!setx.count(x)){
setx.insert(x);
int y = Ux.size(x);
ans *= fac[y];
ans %= mod;
}
}
UnionFind Uy(N);
for (int i = 0; i < N-1; i++){
for (int j = i+1; j < N; j++){
bool check = true;
for (int k = 0; k < N; k++){
if(a[k][i] + a[k][j] > K){
check = false;
break;
}
}
if(check){
Uy.merge(i, j);
}
}
}
unordered_set<int> sety;
for (int i = 0; i < N; i++){
int x = Uy.root(i);
if(!sety.count(x)){
sety.insert(x);
int y = Uy.size(x);
ans *= fac[y];
ans %= mod;
}
}
cout << ans << endl;
return 0;
} |
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <utility>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <limits>
#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()
{
ll x,y; cin >> x >> y;
if(x >= y) cout << x - y << "\n";
else {
map<ll,ll> dp;
queue<ll> q;
q.push(y);
ll res = infll;
while( !q.empty() ) {
ll num = q.front(); q.pop();
if(num <= x) {
chmin(res, dp[num] + x - num);
continue;
}
chmin(res, dp[num] + num - x);
if(num % 2 == 0) {
if(dp[num/2] == 0) {
dp[num/2] = dp[num]+1;
q.push(num/2);
}
else if(chmin(dp[num/2], dp[num]+1)) {
q.push(num/2);
}
}
else {
if(dp[num/2] == 0) {
dp[num/2] = dp[num]+2;
q.push(num/2);
}
else if(chmin(dp[num/2], dp[num]+2)) {
q.push(num/2);
}
if(dp[num/2+1] == 0) {
dp[num/2+1] = dp[num]+2;
q.push(num/2+1);
}
else if(chmin(dp[num/2+1], dp[num]+2)) {
q.push(num/2+1);
}
}
}
cout << res << "\n";
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M=105;
int a[M],n;ll dp[M][M][M],m;
int main(){
// freopen("n.txt","r",stdin);
cin>>n>>m;ll ans=1e18+5;
for(int i=1;i<=n;i++)cin>>a[i];
// for(int i=1;i<=n;i++)for(int j=)
for(int K=1;K<=n;K++){
memset(dp,0,sizeof(dp));
for(int i=0;i<=n;i++)for(int j=1;j<K;j++)dp[0][i][j]=-1e18;
for(int k=1;k<=K;k++)for(int j=0;j<K;j++)dp[k][0][j]=-1e18;
for(int k=1;k<=K;k++)
for(int i=1;i<=n;i++)
for(int j=0;j<K;j++)
dp[k][i][j]=max(dp[k][i-1][j],dp[k-1][i-1][((j-a[i])%K+K)%K]+a[i]);//,cout<<k<<' '<<i<<' '<<j<<':'<<dp[k][i][j]<<"<- "<<dp[k-1][i-1][((j-a[i])%K+K)%K]<<endl;
// cout<<dp[K][n][m%K]<<endl;
if(dp[K][n][m%K]>0)ans=min(ans,(m-dp[K][n][m%K])/K);//,cout<<'!'<<(m-dp[K][n][m%K])/K<<endl;
}
cout<<ans;
return 0;
} |
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define NDEBUG
#define eprintf(...) do {} while (0)
#endif
#include<cassert>
using namespace std;
typedef long long LL;
typedef vector<int> VI;
#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)
template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }
#define rprintf(fmt, begin, end) do { const auto end_rp = (end); auto it_rp = (begin); for (bool sp_rp=0; it_rp!=end_rp; ++it_rp) { if (sp_rp) putchar(' '); else sp_rp = true; printf(fmt, *it_rp); } putchar('\n'); } while(0)
int N, M;
int A[1011], B[1011];
int dp[1011][1011];
void MAIN() {
scanf("%d%d", &N, &M);
REP (i, N) scanf("%d", A+i);
REP (i, M) scanf("%d", B+i);
memset(dp, 0x3f, sizeof dp);
dp[0][0] = M + N;
REP (i, N+1) REP (j, M+1) {
if (i < N && j < M) {
if (A[i] != B[j]) {
amin(dp[i+1][j+1], dp[i][j] - 1);
} else {
amin(dp[i+1][j+1], dp[i][j] - 2);
}
}
amin(dp[i][j+1], dp[i][j]);
amin(dp[i+1][j], dp[i][j]);
}
int ans = dp[N][M];
printf("%d\n", ans);
}
int main() {
int TC = 1;
// scanf("%d", &TC);
REP (tc, TC) MAIN();
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
int n,m;
int a[1010],b[1010];
int dp[1010][1010];
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<=m;i++)
cin>>b[i];
for(int i=0;i<=n;i++)
for(int j=0;j<=m;j++)
dp[i][j]=1e9;
dp[0][0]=0;
for(int i=0;i<=n;i++)
for(int j=0;j<=m;j++){
if(a[i+1]==b[j+1])
dp[i+1][j+1]=min(dp[i+1][j+1],dp[i][j]);
dp[i+1][j+1]=min(dp[i+1][j+1],dp[i][j]+1);
dp[i+1][j]=min(dp[i+1][j],dp[i][j]+1);
dp[i][j+1]=min(dp[i][j+1],dp[i][j]+1);
}
cout<<dp[n][m]<<endl;
return 0;
} |
#include "bits/stdc++.h"
using namespace std;
#define FAST cin.tie(nullptr)->sync_with_stdio(false);
#define PRECISE cout << fixed << setprecision(19);
#define ONE int tc = 1;
#define GO for (int i = 0; i < tc; ++i) { solve(); }
void solve() {
int a, b, c; cin >> a >> b >> c;
cout << (a * a + b * b < c * c ? "Yes" : "No");
}
int main() {
FAST PRECISE ONE GO
} | #include <iostream>
#include "bits/stdc++.h"
using namespace std;
int main()
{
int a, b, c;
cin >> a >> b >> c;
string winner;
if (c == 0)
{
if (b < a)
{
winner = "Takahashi";
}
else
{
winner = "Aoki";
}
}
else
{
if (a < b)
{
winner = "Aoki";
}
else
{
winner = "Takahashi";
}
}
cout << winner << endl;
}
|
#include <iostream>
using namespace std;
using ll = long long;
int main(){
double A, B;
cin >> A >> B;
cout << 100-100*B/A<<endl;
} | #include <bits/stdc++.h>
using namespace std;
// type alias
typedef long long LL;
typedef pair<int,int> II;
typedef tuple<int,int,int> III;
typedef vector<int> VI;
typedef vector<string> VS;
typedef unordered_map<int,int> MAPII;
typedef unordered_set<int> SETI;
template<class T> using VV=vector<vector<T>>;
// minmax
template<class T> inline T SMIN(T& a, const T b) { return a=(a>b)?b:a; }
template<class T> inline T SMAX(T& a, const T b) { return a=(a<b)?b:a; }
// repetition
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)
#define REPE(i,n) for(int i=0;i<=(n);++i)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) for(int i=0;i<(n);++i)
#define FORR(x,arr) for(auto& x:arr)
#define SZ(a) int((a).size())
// collection
#define ALL(c) (c).begin(),(c).end()
// DP
#define MINUS(dp) memset(dp, -1, sizeof(dp))
#define ZERO(dp) memset(dp, 0, sizeof(dp))
// stdout
#define println(args...) fprintf(stdout, ##args),putchar('\n');
// debug cerr
template<class Iter> void __kumaerrc(Iter begin, Iter end) { for(; begin!=end; ++begin) { cerr<<*begin<<','; } cerr<<endl; }
void __kumaerr(istream_iterator<string> it) { (void)it; cerr<<endl; }
template<typename T, typename... Args> void __kumaerr(istream_iterator<string> it, T a, Args... args) { cerr<<*it<<"="<<a<<", ",__kumaerr(++it, args...); }
template<typename S, typename T> std::ostream& operator<<(std::ostream& _os, const std::pair<S,T>& _p) { return _os<<"{"<<_p.first<<','<<_p.second<<"}"; }
#define __KUMATRACE__ true
#ifdef __KUMATRACE__
#define dump(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); __kumaerr(_it, args); }
#define dumpc(ar) { cerr<<#ar<<": "; FORR(x,(ar)) { cerr << x << ','; } cerr << endl; }
#define dumpC(beg,end) { cerr<<"~"<<#end<<": "; __kumaerrc(beg,end); }
#else
#define dump(args...)
#define dumpc(ar)
#define dumpC(beg,end)
#endif
// $ cp-batch Travel | diff Travel.out -
// $ g++ -std=c++14 -Wall -O2 -D_GLIBCXX_DEBUG -fsanitize=address Travel.cpp && ./a.out
/*
11/15/2020
21:18-
*/
const int MAX_N=10+1;
LL T[MAX_N][MAX_N],K;
int N;
void solve() {
int res=0;
VI P(N); iota(ALL(P),0);
do {
if(P[0]!=0) continue;
LL t=0;
REP(i,N) {
int u=P[i],v=i+1<N?P[i+1]:0;
t+=T[u][v];
}
res+=t==K;
} while(next_permutation(ALL(P)));
cout<<res<<endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout<<setprecision(12)<<fixed;
cin>>N>>K;
REP(i,N)REP(j,N) cin>>T[i][j];
solve();
return 0;
}
|
#include<ctime>
#include<cstdio>
#include<cctype>
using namespace std;
const int N=1e5+10;
int read() {
char c;
int x=0,f=1;
while(!isdigit(c=getchar()))
f-=2*(c=='-');
while (isdigit(c)){
x=x*10+(c-48)*f;
c=getchar();
}
return x;
}
int n,x,a[N],b[N];
int main() {
#ifndef ONLINE_JUDGE
freopen("B.in","r",stdin);
freopen("B.out","w",stdout);
#endif
clock_t t1=clock();
//--------
n=read();
for(int i=1;i<=n;++i)
a[i]=read();
for(int i=1;i<=n;++i)
b[i]=read();
for(int i=1;i<=n;++i)
x+=a[i]*b[i];
if(!x)
puts("Yes");
else
puts("No");
//--------
clock_t t2=clock();
fprintf(stderr,"time:%0.3lfs",1.0*(t2-t1)/CLOCKS_PER_SEC);
return 0;
} | // KNOW NOTHING!
#include <bits/stdc++.h>
#define ll long long int
#define F(i,j,k,in) for(int i=j;i<k;i+=in)
#define DF(i,j,k,in) for(int i=j;i>=k;i-=in)
#define feach(it,l) for (auto it = l.begin(); it != l.end(); ++it)
#define fall(a) a.begin(),a.end()
#define sz(x) (int)x.size()
#define szs(x) (int)x.length()
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define eq equal_range
#define fs first
#define ss second
#define ins insert
#define mkp make_pair
#define endl "\n"
using namespace std;
typedef vector<ll> vll;
typedef vector<int> vin;
typedef vector<char> vch;
typedef vector<string> vst;
typedef set<ll> sll;
typedef set<int> sint;
typedef set<char> sch;
typedef set<string> sst;
typedef queue<ll> qll;
typedef queue<int> qin;
typedef deque<ll> dqll;
typedef deque<int> dqint;
typedef priority_queue<ll> pqll;
typedef priority_queue<int> pqin;
typedef map<ll,ll> mpll;
typedef map<int,int> mpin;
typedef pair<ll,ll> pll;
typedef pair<int,int> pin;
const ll MOD=1000000007;
const long double PI=3.1415926535897932384626433832795;
ll pwr(ll b,ll p){ll res=1;while(p){if(p&1) {res*=b; p--;}else{b*=b; p>>=1;}}return res;}
ll mpwr(ll b,ll p){ll res=1;while(p){if(p&1) {res=(res*b)%MOD; p--;}else{b=(b*b)%MOD; p>>=1;}}return res;}
int msb (int n) {return 31-__builtin_clz(n);}
ll msbl (ll n) {return 63-__builtin_clzll(n);}
int lsb (int n) {return __builtin_ctz(n);}
ll lsbl (ll n) {return __builtin_ctzll(n);}
int setbit (int n) {return __builtin_popcount(n);}
ll setbitl (ll n) {return __builtin_popcountll(n);}
int main()
{
/*
freopen ("input.txt","r","stdin");
freopen ("output.txt","w","stdout");
*/
ios_base::sync_with_stdio(false) , cin.tie(NULL) , cout.tie(NULL);
int n; cin>>n;
vin a (n) , b (n);
F (i,0,n,1) cin>>a[i];
F (i,0,n,1) cin>>b[i];
int pr=0;
F (i,0,n,1) {
pr += (a[i]*b[i]);
}
(pr==0)?cout<<"Yes":cout<<"No";
return 0;
}
/* What we know is a drop , but what we don't know is an ocean !*/ |
#include <bits/stdc++.h>
#include <unordered_set>
using namespace std;
#define ll long long
#define pi 3.1415926535897932384626
#define MX 1e5 + 5
#define MOD 1000000007
#define ll long long
#define int long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define mp make_pair
#define deb(x) cout << #x << " = " << x << endl
#define deb2(x, y) cout << #x << " = " << x << " , " << #y << " = " << y << endl
#define clr(x) memset(x, 0, sizeof(x))
#define ff first
#define ss second
#define min3(a, b, c) min(c, min(a, b))
#define min4(a, b, c, d) min(d, min(c, min(a, b)))
#define fr(i, j, k, in) for (int i = j; i < k; i += in)
#define rfr(i, j, k, in) for (int i = j; i >= k; i -= in)
#define freach(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define rfo(i, n) for (int i = n - 1; i >= 0; i--)
#define fo(i, n) for (int i = 0; i < n; i++)
#define foa(i, a, n) for (int i = a; i < n; i++)
#define endl "\n"
#define sz(a) a.size()
#define digits(n) 1 + floor(log10(n))
#define prec(n) fixed << setprecision(n)
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define sortall(x) sort(all(x))
#define sortrev(x) sort(all(x), greater<int>())
#define sortarr(n, x) sort(n, n + x)
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define miiitr map<int, int>::iterator it;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<pair<int, int>> vii;
typedef vector<vector<int>> vvi;
typedef map<int, int> mpii;
typedef set<int> sti;
/****** Template of some basic operations *****/
template <typename T, typename U>
inline void amin(T &x, U y)
{
if (y < x)
x = y;
}
template <typename T, typename U>
inline void amax(T &x, U y)
{
if (x < y)
x = y;
}
/**********************************************/
/**********output of pair , set , map , vector ********/
template <typename T, typename G>
ostream &operator<<(ostream &out, const pair<T, G> &a)
{
return out << "(" << a.first << " , " << a.second << ")";
}
template <typename T>
ostream &operator<<(ostream &out, const vector<T> &a)
{
out << "[";
for (const auto &i : a)
out << i << " , ";
return out << "]";
}
template <typename T>
ostream &operator<<(ostream &out, const set<T> &a)
{
out << "{";
for (const auto &i : a)
out << i << " , ";
return out << "}";
}
template <typename T, typename G>
ostream &operator<<(ostream &out, const map<T, G> &a)
{
out << "<";
for (const auto &i : a)
out << i << " , ";
return out << ">";
}
/**********************************************/
/*************input output of vector***************/
//vi arr = i_vector<int>(n);
//print_stl(arr);
template <typename T>
vector<T> i_vector(int n)
{
vector<T> v(n);
T temp;
for (auto it = v.begin(); it != v.end(); it++)
{
cin >> *it;
}
return v;
}
template <typename T>
void print_stl(T arr)
{
for (auto itr = arr.begin(); itr != arr.end(); ++itr)
{
cout << *itr << " ";
}
cout << endl;
}
/**********************************************/
int n, m, k , x , q , y , t ;
vi a ;
void solve()
{
cin >> n;
int count = 0;
fo(i , n ){
cin>> m ;
count^=((i+1)^m) ;
}
if(!count){
cout << "Yes" ;
}else{
cout << "No" ;
}
// print_stl(a);
}
int32_t main()
{
fast;
int t = 1;
while (t--)
{
solve();
}
return 0;
}
| //in dp prefix suffix sum helps..
#include<iostream>
#include<vector>
#include<string.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<stack>
#include <iterator>
#include <map>
#include<list>
#include <fstream>
#include<unordered_map>
#include<set>
#include<queue>
#define int long long
#define double long double
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define pip pair<int, pair<int, int> >
#define vip vector<pair<int, pii> >
#define vi vector<int>
#define vd vector<double>
#define mii map<int, int>
#define qu queue<int>
#define set set<int>
#define multiset multiset<int>
#define rep(i, a, b) for(int i=a;i<b;i++)
#define rep1(i, a, b) for(int i=a;i>=b;i--)
#define viv vector<vi>
#define stack stack<int>
#define fi first
#define se second
#define reverse(s) reverse(s.begin(), s.end())
#define sort1(v) sort(v.begin(), v.end())
#define it iterator
#define pq priority_queue<int>
#define pq1 priority_queue <pii, vector<pii>, greater<pii> >
#define mem(x, y) memset(x, (int)y, sizeof(x))
#define ps(x,y) fixed<<setprecision(y)<<x
// author :: Anurag Anand.
using namespace std;
//int z= 1e9+7;
int z= 998244353;
int gcd(int a, int b){
if(a==0)return b;
if(b==0)return a;
return gcd(b, a%b);
}
int power(int a,int b)
{
int res=1;
while(b)
{
if(b&1)
{
res=(res*a)%z;
b--;
}
a=(a*a)%z;
b=b>>1;
}
return res;
}
vi sv;
// vi seg;
// void arrange(int i, int v, int x, int lx, int rx){
// if(rx-lx==1){
// seg[x] =v;
// return;
// }
// int m= (lx+rx)/2;
// if(i<m) arrange(i, v, 2*x+1, lx, m);
// else arrange(i, v, 2*x+2, m, rx);
// seg[x]=min(seg[2*x+1], seg[2*x+2]);
// }
// int calc(int l, int r, int x, int lx, int rx){
// if(l>=rx||r<=lx) return 1e9;
// if(lx>=l&&rx<=r) return seg[x];
// int mid=(lx+rx)/2;
// int sl= calc(l, r, 2*x+1, lx, mid), sr= calc(l, r, 2*x+2, mid, rx);
// return min(sl, sr);
// }
// vi v[200001];
// int dist[200001], ans[200001]={0}, vis[200001]={0};
// void dijkstra(int root){
// int a, d;
// dist[root]=0;
// set s;
// s.insert(mp(0, root));
// while(!s.empty()){
// a= (*s.begin()).se, d= (*s.begin()).fi;
// s.erase(s.begin());
// for(int i=0;i<v[a].size();i++){
// if(dist[a]+1<dist[v[a][i]]){
// s.erase(mp(dist[v[a][i]], v[a][i]));
// dist[v[a][i]]=dist[a]+1;
// s.insert(mp(dist[v[a][i]], v[a][i]));
// }
// }
// }
// }
vi pp;
int col[100001];
vi v[100001];
int g[100001]={0};
mii m;
void dfs(int u, int p=-1){
m[col[u]]++;
rep(i,0,v[u].size()){
if(v[u][i]==p) continue;
if(!m[col[v[u][i]]]){
g[v[u][i]]=1;
}
dfs(v[u][i], u);
}
m[col[u]]--;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
pp.assign(1000001, 0);
for(int i=2;i<1000;i++){
if(!pp[i]){
for(int j=i*i;j<=1000000;j+=i){
pp[j]=1;
}
}
}
for(int i=2;i<=1000000;i++) if(!pp[i]) sv.pb(i);
int t;
t=1;
//cin>>t;
while(t--){
int n;
cin>>n;
rep(i,1,n+1) cin>>col[i];
int x, y;
rep(i,1,n){
cin>>x>>y;
v[x].pb(y);
v[y].pb(x);
}
g[1]=1;
dfs(1);
rep(i,1,n+1) if(g[i]) cout<<i<<"\n";
}
} |
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using pii = pair<int,int>;
template <class T> int sgn(T x) { return (x > 0) - (x < 0); }
template<class T>
struct TPoint {
typedef TPoint P;
T x, y;
static constexpr T eps = static_cast<T>(1e-9);
explicit TPoint(T x=0, T y=0) : x(x), y(y) {}
bool operator<(P p) const { return tie(x,y) < tie(p.x,p.y); }
bool operator==(P p) const {
// return tie(x,y)==tie(p.x,p.y);
return abs(x-p.x) <= eps && abs(y-p.y) <= eps;
}
P operator+(P p) const { return P(x+p.x, y+p.y); }
P operator-(P p) const { return P(x-p.x, y-p.y); }
P operator*(T d) const { return P(x*d, y*d); }
P operator/(T d) const { return P(x/d, y/d); }
T dot(P p) const { return x*p.x + y*p.y; }
T cross(P p) const { return x*p.y - y*p.x; }
T cross(P a, P b) const { return (a-*this).cross(b-*this); }
T dist2() const { return x*x + y*y; }
double dist() const { return sqrt((double)dist2()); }
// angle to x-axis in interval [-pi, pi]
double angle() const { return atan2(y, x); }
P unit() const { return *this/dist(); } // makes dist()=1
P perp() const { return P(-y, x); } // rotates +90 degrees
P normal() const { return perp().unit(); }
// returns point rotated 'a' radians ccw around the origin
P rotate(double a) const {
return P(x*cos(a)-y*sin(a),x*sin(a)+y*cos(a)); }
};
using Point = TPoint<double>;
struct DSU {
struct node {
int p; // parent
int s; // size
};
vector<node> nodes;
DSU(int n) {
nodes.resize(n);
for (int i =0; i < n; i++) {
nodes[i].p = i;
nodes[i].s = 1;
}
}
node& operator[](int index) {
return nodes[index];
}
int size(int v) {
return nodes[find(v)].s;
}
int find(int v) {
if (nodes[v].p==v)
return v;
return nodes[v].p = find(nodes[v].p);
}
bool merge(int a, int b) {
a = find(a);
b = find(b);
if (a==b)
return false;
if (nodes[a].s < nodes[b].s)
swap(a,b);
nodes[b].p = a;
nodes[a].s += nodes[b].s;
return true;
}
};
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<Point> a(n);
for (int i = 0; i < n; i++) {
int x,y;
cin >> x >> y;
a[i] = Point(x,y);
}
const int s = n, t = n+1;
vector<tuple<double,int,int>> edge;
for (int i = 0; i < n; i++) {
edge.emplace_back(a[i].y+100,i,s);
edge.emplace_back(100-a[i].y,i,t);
}
for (int i = 0; i < n; i++) {
for (int j = i+1; j < n; j++) {
edge.emplace_back((a[i]-a[j]).dist(),i,j);
}
}
sort(edge.begin(),edge.end());
DSU d(n+2);
for (auto [l,i,j] : edge) {
d.merge(i,j);
if (d.find(s) == d.find(t)) {
cout << fixed << setprecision(10) << l/2 << endl;
return 0;
}
}
}
// Observations:
// It corresponds to some distance between points
// How to choose which points?
//
// Maybe a binary search? how do i know whether its ok?
| #include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<ll, ll>;
using Pld = pair<ld, ld>;
using Vec = vector<ll>;
using VecP = vector<P>;
using VecB = vector<bool>;
using VecC = vector<char>;
using VecD = vector<ld>;
using VecS = vector<string>;
template <class T>
using Vec2 = vector<vector<T>>;
#define REP(i, m, n) for(ll i = (m); i < (n); ++i)
#define REPN(i, m, n) for(ll i = (m); i <= (n); ++i)
#define REPR(i, m, n) for(ll i = (m)-1; i >= (n); --i)
#define REPNR(i, m, n) for(ll i = (m); i >= (n); --i)
#define rep(i, n) REP(i, 0, n)
#define repn(i, n) REPN(i, 1, n)
#define repr(i, n) REPR(i, n, 0)
#define repnr(i, n) REPNR(i, n, 1)
#define all(s) (s).begin(), (s).end()
#define pb push_back
#define fs first
#define sc second
template <class T1, class T2>
bool chmax(T1 &a, const T2 b){if(a < b){a = b; return true;} return false;}
template <class T1, class T2>
bool chmin(T1 &a, const T2 b){if(a > b){a = b; return true;} return false;}
ll pow2(const int n){return (1LL << n);}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
for (const T &i : v) os << i << ' ';
return os;
}
void co() { cout << '\n'; }
template <class Head, class... Tail>
void co(Head&& head, Tail&&... tail) {
cout << head << ' ';
co(forward<Tail>(tail)...);
}
void ce() { cerr << '\n'; }
template <class Head, class... Tail>
void ce(Head&& head, Tail&&... tail) {
cerr << head << ' ';
ce(forward<Tail>(tail)...);
}
void sonic(){ios::sync_with_stdio(false); cin.tie(0);}
void setp(const int n){cout << fixed << setprecision(n);}
constexpr int INF = 1000000001;
constexpr ll LINF = 1000000000000000001;
constexpr ll MOD = 1000000007;
constexpr ll MOD_N = 998244353;
constexpr ld EPS = 1e-11;
const double PI = acos(-1);
struct union_find {
vector<int64_t> par;
vector<int64_t> sz;
union_find(int64_t _n) {
par.resize(_n);
sz.assign(_n, 1);
for(int64_t i = 0; i < _n; ++i) par[i] = i;
}
int64_t root(int64_t x) {
if (par[x] == x) return x;
return par[x] = root(par[x]);
}
void unite(int64_t x, int64_t y) {
x = root(x), y = root(y);
if (x != y) {
if (sz[x] > sz[y]) swap(x, y);
par[y] = x;
sz[x] += sz[y];
}
}
int64_t size(int64_t x) {
return sz[root(x)];
}
bool same(int64_t x, int64_t y) {
return root(x) == root(y);
}
};
int main(void) {
ll n;
cin >> n;
Vec x(n), y(n);
rep(i, n) cin >> x[i] >> y[i];
ld l = 0, r = LINF;
while(r - l > EPS) {
ld m = (l + r) / 2;
union_find uf(n + 2);
rep(i, n) {
REP(j, i + 1, n) {
if (pow<ld>(x[i] - x[j], 2) + pow<ld>(y[i] - y[j], 2) <= pow(m * 2, 2))
uf.unite(i, j);
}
}
rep(i, n) {
if (100 - y[i] <= m * 2) uf.unite(n, i);
}
rep(i, n) {
if (y[i] + 100 <= m * 2) uf.unite(n + 1, i);
}
if (uf.same(n, n + 1)) r = m;
else
l = m;
}
setp(10);
co(l);
return 0;
} |
#include <bits/stdc++.h>
//end
int main() {
long long int g;
std::cin >> g;
double rez = std::sqrt((double)2) * std::sqrt((double)(g + 1));
long long int maz = (long long int)rez + 1;
for (long long int u = maz; u >= 1; u--) {
if ((u + 1) * u / 2 <= g + 1) {
std::cout << g - u + 1 << std::endl;
break;
}
}
}
//end | #include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m;
signed main(){
cin>>n;
int x=sqrt((n+1)<<1);
if(x*(x+1)>((n+1)<<1)) x--;
cout<<n-x+1<<endl;
return 0;
} |
#include<iostream>
#include<vector>
#include<string>
#include<cstdio>
#include<algorithm>
#include<iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll LLMAX = (1llu<<(sizeof(ll)*8-1)) - 1;
const int IMAX = (1llu<<(sizeof(int)*8-1)) - 1;
const ull Nmax = 2 * 100000 + 100;
ull amax[Nmax];
ull bmax[Nmax];
ull N, a[Nmax], b[Nmax], c[Nmax];
int main(){
cin >> N;
for(ull i = 0; i < N; i++){
cin >> a[i];
if(i == 0){
amax[i] = a[i];
} else {
amax[i] = max(a[i], amax[i-1]);
}
}
for(ull i = 0; i < N; i++){
cin >> b[i];
if(i == 0){
bmax[i] = b[i];
} else {
bmax[i] = max(b[i], bmax[i-1]);
}
}
c[0] = a[0] * b[0];
for(ull i = 1; i < N; i++){
c[i] = max(c[i-1], b[i] * amax[i]);
}
for(ull i = 0; i < N; i++){
cout << c[i] << endl;
}
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>
using namespace std;
#define INF 1000000009
using ll=long long;
struct salesman{
// ้ ็นๆฐNใฎใปใผใซในใใณ 0ในใฟใผใ
ll N;
vector<vector<ll>> g;
ll MAXN=17;
vector<vector<ll>> dp;
ll ans=INF;
salesman(ll n){
N=n;
g.resize(n);
dp.resize(1<<N);
for (ll i=0;i<(1<<N);i++){
dp[i].resize(n);
}
for (ll i=0;i<n;i++){
g[i].resize(n);
for (ll j=0;j<n;j++){
g[i][j]=INF;
}
g[i][i]=0;
}
for (ll r=0;r<(1<<N);r++){
for (ll i=0;i<N;i++){
dp[r][i]=INF;
}
}
};
// ๆๅใฐใฉใ
void add_edge(ll from,ll to,ll cost){
g[from][to]=cost;
};
// 0ใซๆปใฃใฆใใๆใฎๆๅฐใณในใๅ
void run(){
dp[0][0]=0;
for (ll r=0;r<(1<<N);r++){
for (ll l=0;r>>l>0;l++){
if ((r>>l)&1){
for (ll u=0;u<N;u++){
if (g[u][l]!=INF) dp[r][l]=min(dp[r][l],dp[r-(1<<l)][u]+g[u][l]);
}
}
}
}
for (ll i=1;i<N;i++){
if (g[i][0]!=INF && dp[(1<<N)-1][i]!=INF) ans=min(dp[(1<<N)-1][i]+g[i][0],ans);
}
};
};
int main(){
ll N;cin >> N;
vector<ll> X(N);
vector<ll>Y(N);
vector<ll> Z(N);
salesman solver(N);
for (int i=0;i<N;i++){
cin >> X[i] >> Y[i] >> Z[i];
}
for (int i=0;i<N;i++){
for (int j=0;j<N;j++){
ll cost=0;
if (Z[j]-Z[i]>=0) cost=abs(X[i]-X[j])+abs(Y[i]-Y[j])+Z[j]-Z[i];
else cost=abs(X[i]-X[j])+abs(Y[i]-Y[j]);
solver.add_edge(i,j,cost);
}
}
solver.run();
cout << solver.ans << endl;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
int main(){
int N; cin >> N;
vector<double> a(N),b(N),c(N),d(N);
for(int _=0; _<2; _++){
for(int i=0; i<N; i++) cin >> a[i] >> b[i];
int x = 0, y = 0;
for(int i=0; i<N; i++){
x += a[i];
y += b[i];
a[i] *= N;
b[i] *= N;
}
for(int i=0; i<N; i++){
a[i] -= x;
b[i] -= y;
}
swap(a,c);
swap(b,d);
}
for(int i=0; i<N; i++){
if(a[i]!=0 || b[i]!=0){
swap(a[i],a[0]);
swap(b[i],b[0]);
}
}
string ans = "No";
const double eps = 1e-6;
for(int i=0; i<N; i++){
double angle = atan2(d[i],c[i])-atan2(b[0],a[0]);
bool flag = true;
for(int j=0; j<N; j++){
double A = a[j]*cos(angle)-b[j]*sin(angle);
double B = a[j]*sin(angle)+b[j]*cos(angle);
bool flag2 = false;
for(int k=0; k<N; k++){
if(std::abs(A-c[k])<=eps && std::abs(B-d[k])<=eps) flag2 = true;
}
flag &= flag2;
}
if(flag) ans = "Yes";
}
cout << ans << endl;
}
|
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define NDEBUG
#define eprintf(...) do {} while (0)
#endif
#include<cassert>
using namespace std;
typedef long long LL;
typedef vector<int> VI;
#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)
template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }
template<class Iter> void rprintf(const char *fmt, Iter begin, Iter end) {
for (bool sp=0; begin!=end; ++begin) { if (sp) putchar(' '); else sp = true; printf(fmt, *begin); }
putchar('\n');
}
int N;
int A[111], B[111];
int cnt[211];
bool dp[211];
int AB[211], BA[211];
void MAIN() {
scanf("%d", &N);
REP (i, N) {
scanf("%d%d", A+i, B+i);
if (A[i] != -1) A[i]--;
if (B[i] != -1) B[i]--;
}
bool yes = true;
REP (i, N) {
if (A[i] == 2*N-1) yes = false;
if (B[i] == 0) yes = false;
if (A[i] != -1 && B[i] != -1 && B[i] <= A[i]) yes = false;
if (A[i] != -1) cnt[A[i]]++;
if (B[i] != -1) cnt[B[i]]++;
}
if (yes) {
REP (i, 2*N) {
if (cnt[i] >= 2) yes = false;
}
}
if (yes) {
REP (i, N*2) {
AB[i] = -2;
BA[i] = -2;
}
REP (i, N) {
if (A[i] != -1) AB[A[i]] = B[i];
if (B[i] != -1) BA[B[i]] = A[i];
}
dp[0] = true;
for (int i=0; i<N*2; i+=2) if (dp[i]) {
for (int h=1; i+h+h<=N*2; h++) {
bool ok = true;
for (int k=i; ok && k<i+h; k++) {
if (AB[k] == -1 && BA[k+h] != -2) ok = false;
if (BA[k+h] == -1 && AB[k] != -2) ok = false;
if (0 <= AB[k] && AB[k] != k+h) ok = false;
if (0 <= BA[k]) ok = false;
if (0 <= BA[k+h] && BA[k+h] != k) ok = false;
if (0 <= AB[k+h]) ok = false;
}
if (ok) dp[i+h+h] = true;
}
}
yes = dp[N*2];
}
puts(yes? "Yes": "No");
}
int main() {
int TC = 1;
// scanf("%d", &TC);
REP (tc, TC) MAIN();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()
constexpr ll MOD=1000000007;
constexpr ll INF=1e18;
struct Q{
int a;
int b;
int i;
bool operator<(const Q &another) const {return a < another.a;};
};
int main(){
int n; cin >> n;
VI x(n), y(n);
REP(i,n) cin >> x[i] >> y[i];
int m; cin >> m;
VI op(m), p(m,-1);
REP(i,m){
cin >> op[i];
if(op[i]>=3)
cin >> p[i];
}
int q; cin >> q;
vector<Q> qu(q);
REP(i,q){
cin >> qu[i].a >> qu[i].b;
qu[i].b--;
qu[i].i=i;
}
sort(ALL(qu));
qu.push_back({0,0,-1});
int rad=0;
bool xr=0, yr=0;
ll xd=0, yd=0;
int now=0;
vector<P> ans(q);
while(qu[now].a==0){
ans[qu[now].i]={x[qu[now].b],y[qu[now].b]};
now++;
}
REP(i,m){
if(op[i]==1){
rad=(rad+1)%4;
xd=-xd;
swap(xd,yd);
swap(xr,yr);
}
else if(op[i]==2){
rad=(rad+3)%4;
yd=-yd;
swap(xd,yd);
swap(xr,yr);
}
else if(op[i]==3){
xd=-xd+p[i]*2;
xr=!xr;
}
else{
yd=-yd+p[i]*2;
yr=!yr;
}
while(qu[now].a==i+1){
int j=qu[now].i;
ll ansx=x[qu[now].b];
ll ansy=y[qu[now].b];
if(rad==0){
ans[j]={ansx,ansy};
}
else if(rad==1){
ans[j]={ansy,-ansx};
}
else if(rad==2){
ans[j]={-ansx,-ansy};
}
else{
ans[j]={-ansy,ansx};
}
ans[j].first=ans[j].first*(xr?-1:1)+xd;
ans[j].second=ans[j].second*(yr?-1:1)+yd;
now++;
}
}
REP(i,q){
cout << ans[i].first << " " << ans[i].second << endl;
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
#define REP(i,n) for(ll i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()
constexpr ll MOD=998244353;
constexpr ll INF=2e18;
VVI g(300000);
VI in(300000,0);
VI ans(300000,0);
VI kk(300000,0);
void dfs(int v){
for(auto to:g[v]){
ans[to]+=ans[v];
if(--in[to]==0){
dfs(to);
}
}
}
int main(){
int n, m; cin >> n >> m;
VVI c(n+1,VI(n+1,INF));
int x, y, z;
REP(i,m){
cin >> x >> y >> z;
for(int j=x;j>=0;j--)
c[j][y]=min(c[j][y],(ll)z);
for(int k=y;k>=0;k--)
c[x][k]=min(c[x][k],(ll)z);
}
REP(i,1<<n){
int popc=0, cnt=0;
bool no=0;
REP(j,n){
if(i>>j&1)
popc++;
}
REP(j,n){
if(i>>j&1)
cnt++;
if(c[popc][j+1]<cnt){
no=1;
break;
}
}
if(!no)
kk[i]=1;
}
kk[0]++;
REP(i,1<<n){
if(kk[i]==2){
REP(j,n){
if(!(i>>j&1)){
if(kk[i|(1<<j)]){
kk[i|(1<<j)]=2;
g[i].push_back(i|(1<<j));
in[i|(1<<j)]++;
}
}
}
}
}
ans[0]=1;
dfs(0);
cout << ans[(1<<n)-1] << endl;
return 0;
} | #include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1005,mod=1e9+7;
int n,f[N];
char AA,AB,BA,BB;
int qp(int a,int b)
{
int ans=1;
while(b)
{
if(b&1)
ans=ans*a%mod;
a=a*a%mod;
b>>=1;
}
return ans;
}
signed main()
{
ios::sync_with_stdio(false);
cin>>n>>AA>>AB>>BA>>BB;
if(n==2)
{
cout<<1<<endl;
return 0;
}
if(AB=='A')
{
if(AA=='A')
{
cout<<1<<endl;
return 0;
}
if(BA=='A')
{
f[0]=1,f[1]=2;
for(int i=2;i<=n;i++)
f[i]=(f[i-1]+f[i-2])%mod;
cout<<f[n-3]<<endl;
return 0;
}
cout<<qp(2,n-3)<<endl;
}
else
{
if(BB=='B')
{
cout<<1<<endl;
return 0;
}
if(BA=='B')
{
f[0]=1,f[1]=2;
for(int i=2;i<=n;i++)
f[i]=(f[i-1]+f[i-2])%mod;
cout<<f[n-3]<<endl;
return 0;
}
cout<<qp(2,n-3)<<endl;
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll solve(vector<ll> &A1, vector<ll> &A2, ll &T) {
bitset<20> bs;
vector<ll> B;
ll res=0;
for(ll i=0; i<pow(2,A1.size()); ++i) {
ll count=0; bs=i;
for(ll j=0; j<A1.size(); ++j) if(bs.test(j)) count+=A1[j];
B.push_back(count);
}
sort(B.begin(),B.end());
for(ll i=0; i<pow(2,A2.size()); ++i) {
ll count=0; bs=i;
for(ll j=0; j<A2.size(); ++j) if(bs.test(j)) count+=A2[j];
if(count<=T) {
count+=*(upper_bound(B.begin(),B.end(),T-count)-1);
res=max(res,count);
}
}
return res;
}
int main() {
ll N,T; cin>>N>>T;
vector<ll> A1, A2;
for(ll i=0; i<N; ++i) {
ll A; cin>>A;
if(i<N/2) A1.push_back(A);
else A2.push_back(A);
}
cout << solve(A1,A2,T) << endl;
}
| #include <math.h>
#include <assert.h>
#include <algorithm>
#include <set>
#include <iostream>
#include <vector>
#include <iomanip>
#include <queue>
#include <map>
#include <string>
#include <cstring>
#include <functional>
#include <stack>
#include <array>
#include <random>
#include <chrono>
#include <climits>
#include <bitset>
using namespace std ;
#define rep(i, a, b) for (int i=a; i<(b); i++)
#define forn(i, a) for (int i=0; i<(a); i++)
#define repd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define ford(i,a) for (int i = (a)-1; i >= 0; i--)
#define trav(a,x) for (auto& a : x)
#define int long long
#define ii pair<int,int>
#define ar array
#define FAST_IO ios_base::sync_with_stdio(false) ;cin.tie(NULL);cout.tie(NULL)
#define ms(x,a) memset(x,(int)(a),sizeof(x))
#define sz(x) ((int)(x).size())
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define pb push_back
#define endl '\n'
#define ff first
#define ss second
//const int INF = 1e18 ;
//const int MOD = 1e9 + 7 ;
//const int N = 3e5+7 ;
/* Print pair */
template <typename T,typename S>
ostream & operator << (ostream &os , const pair<T,S> &v) {
os << "(" ;
os << v.first << "," << v.second << ")" ;
return os ;
}
/* Print vector */
template <typename T>
ostream & operator << (ostream &os , const vector<T> &v) {
os << "[" ;
int sz = v.size() ;
for(int i = 0 ; i < sz ; ++i) {
os << v[i] ;
if(i!=sz-1)os << "," ;
}
os << "]\n" ;
return os ;
}
/* Print set */
template <typename T>
ostream & operator << (ostream &os , const set<T> &v) {
T last = *v.rbegin() ;
os << "[" ;
for(auto it : v) {
os << it ;
if(it != last) os << "," ;
}
os << "]\n" ;
return os ;
}
/* Print Map */
template <typename T,typename S>
ostream & operator << (ostream &os , const map<T,S> &v) {
for(auto it : v) {
os << it.first << " : " << it.second << "\n" ;
}
return os ;
}
//debug
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif
void solve(){
int n , t ;
cin >> n >> t ;
vector<int> a(n) ;
for(auto& x : a) cin >> x ;
vector<int> s1{0} , s2{0} ;
forn(i,n) {
int m = sz(s1) ;
forn(j,m) s1.push_back(s1[j]+a[i]) ;
swap(s1,s2) ;
}
sort(all(s1)) ;
reverse(all(s1)) ;
int ans = 0 ;
forn(i,sz(s2)) {
if(s2[i] > t) continue ;
ans = max(ans , s2[i]+ *lb(all(s1),t-s2[i],greater<int>())) ;
}
cout << ans << endl ;
}
int32_t main(){
FAST_IO ;
int t = 1 ;
// cin >> t ;
while(t--){
solve() ;
}
}
|
#include<bits/stdc++.h>
using namespace std;
string dfs(int n,string s){
if(n == 1)return s;
while(n%3!=1){
string t;
for(int i=1;i<n;i++){
if(s[i]==s[i-1])t+=s[i];
else {
int x = 'R'+'W'+'B';
x -= s[i] + s[i-1];
t += (char)x;
}
}
swap(t,s);
n--;
}
string t;
for(int i=0;i<n;i+=3)t+=s[i];
return dfs((int)t.size(),t);
}
int main(){
int n;
cin>>n;
string s;
cin>>s;
cout << dfs(n,s) << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL mod = 998244353;
LL f[3010][6010], s[6010];
int n, k;
int main() {
cin >> n >> k;
f[0][0] = 1;
s[0] = 1;
for (int i = 1; i <= n; i++)
for (int j = i; j >= 1; j--) {
f[i][j] = s[i * 2 - j * 2];
s[i * 2 - j] = (s[i * 2 - j] + f[i][j]) % mod;
}
cout << f[n][k] << endl;
return 0;
} |
#include<iostream>
#include<sstream>
#include<iomanip>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<map>
#include<cmath>
#include<string>
#include<numeric>
#include<queue>
#define rep(i,p) for(long long int i=0;i<p;i++)
#define reep(i,p) for(long long int i=1;i<=p;i++)
#define ll long long
#define MOD 1000000007
#define INF 9223372036854775800
using namespace std;
//----------------------------(a1,b1) (a2,b2)ใ้ใ็ด็ทใฎๅพใ(key0)ใจๅ็(key1)
vector<double> strand(long long int a1,long long int a2,long long int b1,long long int b2){
vector<double> x;
double d;
double s;
if(a1==a2){
x.push_back(0);
x.push_back(0);
return x;
}
else{
d = (double)(b1-b2)/(double)(a1-a2);
x.push_back(d);
s=(double)(b1)-d*(double)(a1);
x.push_back(s);
return x;
}
}
int main(){
ll int N , D, H;
cin >> N >> D >> H;
vector< ll int > d(N);
vector< ll int > h(N);
rep(i,N) cin >> d[i] >> h[i];
vector< vector< double > > f(N);
double ans = 0.0;
rep(i,N){
f[i] = strand(D,d[i],H,h[i]);
ans = max( ans, f[i][1] );
}
cout << fixed << setprecision(5) << ans << endl;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define ERR(...) fprintf(stderr,__VA_ARGS__);
#define rep(i,l,r) for(i=(l);i<=(r);++i)
#define per(i,l,r) for(i=(l);i>=(r);--i)
#define REP(i,l,r) for(i=(l);i< (r);++i)
#define PER(i,l,r) for(i=(l);i> (r);--i)
typedef long long ll;
priority_queue<int> Queue;
const int MAXN=200005;
int A[MAXN],B[MAXN],P[MAXN],Q[MAXN];
pair<int,int> S[MAXN];
inline bool ok(int v){return P[v]!=Q[v]&&A[P[v]]>B[v]&&A[v]>B[v]&&A[P[v]]>B[P[v]];}
inline void ac(){
int n,i,p,q,s=0;
scanf("%d",&n);
rep(i,1,n)scanf("%d",&A[i]);
rep(i,1,n)scanf("%d",&P[i]);
rep(i,1,n)scanf("%d",&Q[i]);
rep(i,1,n)B[i]=P[Q[i]];
//rep(i,1,n)printf("%d ",B[i]);printf("\n");
rep(i,1,n)P[Q[i]]=i;
//rep(i,1,n)printf("%d ",P[i]);printf("\n");
rep(i,1,n)if(ok(i))Queue.push(i);
while(!Queue.empty()){
i=Queue.top();Queue.pop();
if(!ok(i))continue;
p=P[i];q=Q[i];
swap(B[p],B[i]);
//printf("%d <- %d <- %d\n",p,i,q);fflush(stdout);
Q[p]=q;P[q]=p;
P[i]=Q[i]=i;
S[++s]=make_pair(p,i);
if(ok(q))Queue.push(q);
}
rep(i,1,n)if(P[i]!=i&&P[i]==Q[i]){
p=P[i];
if(A[i]<=B[i]||A[p]<=B[p]){puts("-1");return;}
P[i]=Q[i]=i;
P[p]=Q[p]=p;
S[++s]=make_pair(p,i);
}
rep(i,1,n)if(P[i]!=i){puts("-1");return;}
printf("%d\n",s);
rep(i,1,s)printf("%d %d\n",S[i].first,S[i].second);
}
int main(){
//freopen("1.in","r",stdin);
//freopen("1.out","w",stdout);
ac();
return 0;
} |
#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;
int show_matrix(vector<vector<int>> &dp) {
loop(0, dp.size(), i) {
loop(0, dp[i].size(), j) {
cout << dp[i][j] << " ";
}
cout << endl;
}
return 0;
}
/*
ๆตฎๅๅฐๆฐ็นใฎๅ
ฅๅ
cout << fixed << setprecision(9) << endl;
*/
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
ll N;
cin >> N;
vector<ll> X(N);
vector<ll> Y(N);
vector<ll> Z(N);
loop(0, N, i) {
cin >> X[i];
cin >> Y[i];
cin >> Z[i];
}
vector<vector<ll>> DP(N, vector<ll>(1 << N, INF));
DP[0][1] = 0;
for (ll bit=1; bit < (1 << N); bit++) {
loop(0, N, i) {
ll next_bit = bit | (1 << i);
loop(0, N, j) {
ll cost = abs(X[i] - X[j]) + abs(Y[i] - Y[j]) + max((ll)0, Z[j] - Z[i]);
DP[j][next_bit] = min(DP[j][next_bit], DP[i][bit] + cost);
}
}
}
ll complete = (1 << N) - 1;
print(DP[0][complete]);
}
| #include<bits/stdc++.h>
using namespace std;
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
typedef long long ll;
int main() {
ll n;
cin >> n;
vector<ll> a(n);
for(ll i = 0; i < n; i++) cin >> a[i];
sort(a.rbegin(), a.rend());
vector<ll> pref(n, 0);
pref[0] = a[0];
for(ll i = 1; i < n; i++) {
pref[i] = pref[i-1] + a[i];
}
ll ans = 0;
for(ll i = 0; i < n-1; i++) {
ans += (n-i-1) * a[i] - (pref[n-1] - pref[i]);
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#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 MOD 1000000007
int main() {
ll r1, c1, r2, c2;
cin >> r1 >> c1 >> r2 >> c2;
int ans = 0;
if (r1 == r2 && c1 == c2) {
cout << ans << endl;
return 0;
}
int limit = 10;
for (int dx = -3; dx <= 3; dx++) {
for (int dy = -3; dy <= 3; dy++) {
if (abs(dx) + abs(dy) > 3) {
continue;
}
ll nr = r1 + dx;
ll nc = c1 + dy;
if (nr + nc == r2 + c2 || nr - nc == r2 - c2 || abs(nr - r2) + abs(nc - c2) <= 3) {
limit = 2;
}
}
}
while (true) {
ans++;
if (r1 + c1 == r2 + c2 || r1 - c1 == r2 - c2 || abs(r1 - r2) + abs(c1 - c2) <= 3) {
break;
}
if (ans == 1) {
ll d = r2 - r1;
r1 += d;
c1 += d;
d = (c2 - c1) / 2;
r1 += d;
c1 += d;
continue;
}
if (ans == 2) {
ans++;
break;
}
}
cout << min(ans, limit) << endl;
return 0;
}
| #include<bits/stdc++.h>
#define int long long
using namespace std;
int x,y,a,b;
signed main()
{
scanf("%lld%lld%lld%lld",&x,&y,&a,&b);
if(x<a)
swap(x,a),swap(y,b);
if(x==a&&y==b)
{
puts("0");
return 0;
}
if(x+y==a+b||x-y==a-b||abs(x-a)+abs(y-b)<=3)
{
puts("1");
return 0;
}
if((a+b)%2==(x+y)%2||(abs(b+(a-x)-y))<=3||(abs(b-(a-x)-y))<=3)
{
puts("2");
return 0;
}
puts("3");
return 0;
}
|
Subsets and Splits