code_file1
stringlengths 87
4k
| code_file2
stringlengths 82
4k
|
---|---|
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;
typedef long long ll;
int main(){
int i,n; cin >> n;
ll s = 0;
vector<ll> v;
for(i=0;i<n;i++){
ll a,b; cin >> a >> b;
s -= a;
v.push_back(2*a + b);
}
sort(v.rbegin(),v.rend());
for(i=0;i<n;i++){
s += v[i];
if(s>0){
cout << i + 1 << endl;
return 0;
}
}
} | #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
using Graph = vector<vector<int>>;
typedef long long ll;
int main(){
ll N;
cin >> N;
vector<ll> A(N),B(N),get(N);
ll a = 0,b = 0;
rep(i,N){
cin >> A[i] >> B[i];
a += A[i];
b += B[i];
get[i] = A[i]*2 + B[i];
}
ll sub = a;
sort(get.begin(),get.end());
ll ans = 0;
while(sub >= 0){
sub -= get[N-ans-1];
ans++;
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 5005;
const int mod = 998244353;
int h, w, k;
char grid[maxn][maxn];
int dp[maxn][maxn], d[maxn][maxn], r[maxn][maxn], p[maxn];
int recur(int i, int j) {
if (dp[i][j] != -1) return dp[i][j];
if (i == h && j == w) {
if (grid[i][j] == '.') return 3;
else return 1;
}
int res = 0;
// move down
if (grid[i][j] != 'R' && i != h) {
int ways = p[(w-j)-r[i][j+1]];
if (grid[i][j] == '.') ways = (ways*2)%mod;
res += (recur(i+1,j)*ways)%mod;
res %= mod;
}
// move right
if (grid[i][j] != 'D' && j != w) {
int ways = p[3,(h-i)-d[i+1][j]];
if (grid[i][j] == '.') ways = (ways*2)%mod;
res += (recur(i,j+1)*ways)%mod;
res %= mod;
}
dp[i][j] = res;
return res;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
p[0] = 1;
for (int i = 1; i < maxn; i++) p[i] = (p[i-1]*3)%mod;
for (int i = 0; i < maxn; i++) for (int j = 0; j < maxn; j++) {
grid[i][j] = '.';
dp[i][j] = -1;
d[i][j] = r[i][j] = 0;
}
cin >> h >> w >> k;
for (int i = 0; i < k; i++) {
int x, y;
char c;
cin >> x >> y >> c;
grid[x][y] = c;
d[x][y]++;
r[x][y]++;
}
for (int i = 0; i < maxn; i++) {
for (int j = maxn-2; j >= 0; j--) {
d[j][i] += d[j+1][i];
r[i][j] += r[i][j+1];
}
}
int res = recur(1,1);
cout << res << '\n';
}
/*
*/ | #pragma GCC target("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include<stdio.h>
#include<vector>
#include<algorithm>
constexpr unsigned long long MOD = 998244353;
constexpr unsigned long long IM = 18479187003;
constexpr unsigned long long MOD32 = 665496236;
unsigned long long dp[5001];
int main(){
int H=0, W=0, K=0;
char c=getchar_unlocked();
while(c<='9'&&c>='0') {H=(H<<1)+(H<<3)+c-'0'; c=getchar_unlocked();}
c=getchar_unlocked();
while(c<='9'&&c>='0') {W=(W<<1)+(W<<3)+c-'0'; c=getchar_unlocked();}
c=getchar_unlocked();
while(c<='9'&&c>='0') {K=(K<<1)+(K<<3)+c-'0'; c=getchar_unlocked();}
std::vector<std::vector<std::pair<int, char>>> f(H);
unsigned long long y = H * W - K;
while(--K>=0){
int h=0, w=0;
c=getchar_unlocked();
while(c<='9'&&c>='0') {h=(h<<1)+(h<<3)+c-'0'; c=getchar_unlocked();}
c=getchar_unlocked();
while(c<='9'&&c>='0') {w=(w<<1)+(w<<3)+c-'0'; c=getchar_unlocked();}
--h; --w;
f[h].emplace_back(w, getchar_unlocked());
getchar_unlocked();
}
--H; --W;
dp[0] = 1;
for(int i = 0; i < H; ++i){
std::sort(f[i].begin(), f[i].end());
auto itr = f[i].begin();
for (int j = 0; j <= W; ++j){
if(itr != f[i].end() and itr->first == j){
char c = itr->second;
if(c != 'D') dp[j + 1] += dp[j];
if(c == 'R') dp[j] = 0;
++itr;
}
else{
{
dp[j] *= MOD32;
#ifdef _MSC_VER
unsigned long long x;
_umul128(dp[j], IM, &x);
#else
unsigned long long x =
(unsigned long long)(((unsigned __int128)(dp[j])*IM) >> 64);
#endif
dp[j] = dp[j] - x * MOD+MOD;
}
dp[j + 1] += dp[j];
}
}
}
{
std::sort(f[H].begin(), f[H].end());
auto itr = f[H].begin();
for (int j = 0; j < W; ++j){
if(itr != f[H].end() and itr->first == j){
char c = itr->second;
if(c != 'D') dp[j + 1] += dp[j];
++itr;
}
else{
{
dp[j] *= MOD32;
#ifdef _MSC_VER
unsigned long long x;
_umul128(dp[j], IM, &x);
#else
unsigned long long x =
(unsigned long long)(((unsigned __int128)(dp[j])*IM) >> 64);
#endif
dp[j] = dp[j] - x * MOD+MOD;
}
dp[j + 1] += dp[j];
}
}
}
unsigned long long x = 3;
while(y) {
if(y & 1) {
dp[W] *= x;
#ifdef _MSC_VER
unsigned long long x2;
_umul128(dp[W], IM, &x2);
#else
unsigned long long x2 =
(unsigned long long)(((unsigned __int128)(dp[W])*IM) >> 64);
#endif
dp[W] = dp[W] - x2 * MOD+MOD;
}
{
x *= x;
#ifdef _MSC_VER
unsigned long long x2;
_umul128(x, IM, &x2);
#else
unsigned long long x2 =
(unsigned long long)(((unsigned __int128)(x)*IM) >> 64);
#endif
x = x- x2 * MOD + MOD;}
y >>= 1;
}
printf("%d", dp[W] % MOD);
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define dbg(x) cerr << #x << " = " << x << endl
#define rep(i, a, b) for(int i = (a); i <= (b); ++ i)
#define MP make_pair
#define pb push_back
#define fi first
#define se second
typedef long long LL;
typedef unsigned long long u64;
typedef unsigned int u32;
template <typename Tp> void read(Tp &x){
x = 0; int op = 1; char ch = getchar();
while(!isdigit(ch)){ if(ch == '-') op = -1; ch = getchar(); }
while(isdigit(ch)){ x = x*10+ch-'0'; ch = getchar(); } x *= op;
}
template <typename Tp> void CMax(Tp &x, Tp y){ if(y > x) x = y; }
template <typename Tp> void CMin(Tp &x, Tp y){ if(y < x) x = y; }
int a, b, c, d;
int main()
{
cin >> a >> b >> c >> d;
if(b >= c * d) puts("-1");
else {
LL v = d * c - b;
LL ans = ceil((double)a / (double)v);
printf("%lld", ans);
}
return 0;
}
| #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <initializer_list>
#include <cmath>
#define REP(i, n) for(int i = 0; i < (n); i++)
#define REPS(i, a, b) for(int i = (a); i <= (b); i++)
#define REPR(i, n) for(int i = (n) - 1; i >= 0; i--)
#define REPRS(i, b, a) for(int i = (b) ; i >= (a); i--)
#define ALL(x) (x).begin(), (x).end()
#define ALLR(x) (x).rbegin(), (x).rend()
constexpr static int MOD = 1e8 + 7;
inline void scan() {}
template <typename T, typename... U>
inline void scan(T&& t, U&&... u) {
std::cin >> t;
scan(std::forward<U>(u)...);
}
template <typename T>
inline void scanall(T t, int n) {
for (int i = 0; i < n; i++) {
std::cin >> t[i];
}
}
template <typename T>
inline void scanall(T t, int n, int m) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
std::cin >> t[i][j];
}
}
}
inline void print() {}
template <typename T, typename... U>
inline void print(T&& t, U&&... u) {
std::cout << t;
print(std::forward<U>(u)...);
}
inline void println() {
std::cout << "\n";
}
template <typename T, typename... U>
inline void println(T&& t, U&&... u) {
std::cout << t;
println(std::forward<U>(u)...);
}
using graph = std::vector<std::vector<int>>;
using ll = long long;
using ull = unsigned long long;
int main() {
double a, b, c, d;
scan(a, b, c, d);
ll ans;
if (c * d - b > 0) {
ans = static_cast<ll>(std::ceil(a / (c * d - b)));
} else {
ans = -1;
}
print(ans);
return 0;
} |
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cassert>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <iostream>
#include <numeric>
/* debug macros */
#ifdef WAFDAYO
#define DBG_LINE() {std::cerr<<"\e[2m[L"<<__LINE__<<"]\e[m ";}
#define DBG_PRINT(s,t,u) {std::cerr<<(s)<<" \e[2m=\e[m \e[1m"<<(t)<<"\e[m"<<(u);}
#define SELECT_7TH(x1,x2,x3,x4,x5,x6,x7,...) x7
#define dbg1(x1) DBG_PRINT(#x1,x1,std::endl)
#define dbg2(x1,x2) DBG_PRINT(#x1,x1,", ")dbg1(x2)
#define dbg3(x1,x2,x3) DBG_PRINT(#x1,x1,", ")dbg2(x2,x3)
#define dbg4(x1,x2,x3,x4) DBG_PRINT(#x1,x1,", ")dbg3(x2,x3,x4)
#define dbg5(x1,x2,x3,x4,x5) DBG_PRINT(#x1,x1,", ")dbg4(x2,x3,x4,x5)
#define dbg6(x1,x2,x3,x4,x5,x6) DBG_PRINT(#x1,x1,", ")dbg5(x2,x3,x4,x5,x6)
#define dbg(...) DBG_LINE()\
SELECT_7TH(__VA_ARGS__,dbg6,dbg5,dbg4,dbg3,dbg2,dbg1)(__VA_ARGS__)
#else
#define dbg(...) {}
#endif
/* utility functions */
struct read_item{read_item(){};
template<class T>operator T(){T t;std::cin>>t;return t;}};
char splf(int i,int n){return(i+1<n)?' ':'\n';};
template<bool up>
struct _RI{int i;_RI(int a):i(a){}
int operator*(){return i;}void operator++(){i+=(up?1:-1);}
bool operator!=(const _RI& r){return up?i<r.i:i>=r.i;}};
template<bool up>
struct _RX{_RI<up> x,y;_RI<up> begin(){return x;}_RI<up> end(){return y;}
_RX(int a,int b):x(up?a:(a-1)),y(b){}_RX(int a):_RX(up?0:a,up?a:0){}};
typedef _RX<true> range;
typedef _RX<false> revrange;
/* types and constants */
typedef int64_t i64;
typedef uint64_t u64;
// const i64 inf = (i64)1.05e18;
// const int inf = (int)1.05e9;
using namespace std;
int modadd(int x, int y, int m) {
int z = x + y;
if(z >= m) {
z = z - m;
}
return z;
}
int modsub(int x, int y, int m) {
int z = x - y;
if(z < 0) {
z += m;
}
return z;
}
int main() {
const int N = read_item();
const int K = read_item();
const int M = read_item();
const int W = (N + 1) * N / 2 * K + 1;
vector<int> ways((N + 1) * W);
ways[0] = 1;
for(int p : range(1, N + 1)) {
int base = p * W;
int ppp = p * (K + 1);
int maxi = (1 + p) * p / 2 * K;
for(int s : range(maxi + 1)) {
auto& v = ways[base + s];
v = ways[base + s - W];
if(s >= p) {
v = modadd(v, ways[base + (s - p)], M);
}
}
for(int s : revrange( maxi + 1, 0)) {
auto& v = ways[base + s];
if(s >= ppp) {
v = modsub(v, ways[base + (s - ppp)], M);
}
}
}
for(auto x : range(1, N + 1)) {
int l = x - 1;
int r = N - x;
int lm = (1 + l) * l / 2 * K;
int rm = (1 + r) * r / 2 * K;
i64 v = 0;
for(auto s : range(0, min(lm, rm) + 1)) {
i64 vl = ways[l * W + s];
i64 vr = ways[r * W + s];
v = (v + vl * vr) % M;
}
i64 ans = ((v * (K + 1)) + (M - 1)) % M;
printf("%lld\n", ans);
}
return 0;
}
/* wafdayo~~~ */
| #include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vi=vector<int>;
using vvi=vector<vi>;
using vl=vector<ll>;
using vvl=vector<vl>;
using vpii=vector<pair<int,int>>;
using vpll=vector<pair<ll,ll>>;
using vs=vector<string>;
using vvc=vector<vector<char>>;
const int inf=1e9;
const ll INF=1e18;
const double eps=1e-10;
const double pi=acos(-1);
const ll mod=1e9+7;
const ll MOD=998244353;
//数学関連
int num_order(ll N){
int res=0;
while(N>0){
N/=10;
res++;
}
return res;
}
ll GCD(ll A,ll B){
if(B==0) return A;
else return GCD(B,A%B);
}
ll LCM(ll A,ll B){
ll g=GCD(A,B);
return A/g*B;
}
vpll prime_factorize(ll N){
vpll res;
for(ll i=2;i*i<=N;i++){
if(N%i!=0) continue;
ll ex=0;
while(N%i==0){
ex++;
N/=i;
}
res.push_back({i,ex});
}
if(N!=1) res.push_back({N,1});
return res;
}
ll modpow(ll a,ll n,ll p) {
if (n == 0) return 1; // 0乗にも対応する場合
if (n == 1) return a % p;
if (n % 2 == 1) return (a * modpow(a, n - 1, p)) % p;
long long t = modpow(a, n / 2, p);
return (t * t) % p;
}
ll small_nCk(ll n,ll k){
ll res=1,fact=1;
for(int i=0;i<k;i++){
res=res*(n-i)/fact;
fact++;
}
return res;
}
//データ構造
struct UnionFind {
//自身が親であれば、その集合に属する頂点数に-1を掛けたもの
//そうでなければ親のid
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 Egde{
int to;
ll W;
Egde(int to,ll W) : to(to),W(W) {}
};
using Graph=vector<vector<Egde>>;
int main(){
int A,B;
cin>>A>>B;
int ans;
if(A+B>=15&&B>=8) ans=1;
else if(A+B>=10&&B>=3) ans=2;
else if(A+B>=3) ans=3;
else ans=4;
cout<<ans<<endl;
} |
#include <iostream>
#include <string>
using namespace std;
int main(){
int V, T, S, D;
cin >> V >> T >> S >> D;
if (D >= V * T && D <= V * S) {
cout << "No";
} else {
cout << "Yes";
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long
void solve()
{
vector<int> v(3);
cin >> v[0] >> v[1] >> v[2];
sort(v.begin(),v.end());
if(v[2]-v[1] == v[1]-v[0])
cout << "Yes" << endl;
else
cout << "No" << endl;
return;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
//cin >> t;
while(t--)
solve();
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define PB push_back
#define ll long long
int main(){
#ifndef ONLINE_JUDGE
freopen("C:\\Users\\User\\Documents\\input.txt","r",stdin);
#endif
int T;
cin>>T;
while(T--){
int n,x;
cin>>n;
map<int,int> mp;
for(int i=0;i<n;++i){
scanf("%d",&x);
mp[x]++;
}
if(n%2==1){
printf("Second\n");
}
else{
bool ok=0;
for(auto it : mp){
if(it.second%2==1){
ok=1;
break;
}
}
printf("%s\n",ok ? "First" : "Second");
}
}
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() {
int n;
cin >> n;
vl a(n);
rep (i, n) cin >> a[i];
sort(all(a));
if (n % 2 == 0) {
for (int i = 0; i < n; i += 2) {
if (a[i] != a[i + 1]) {
cout << "First" << endl;
return;
}
}
cout << "Second" << endl;
}
else {
cout << "Second" << endl;
}
}
signed main() {
fio();
int t;
cin >> t;
while (t--) {
solve();
}
} |
#include<bits/stdc++.h>
using namespace std;
#define SPEED ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define READ freopen("out.txt","r",stdin)
#define WRITE freopen("out.txt","w",stdout);
#define pb push_back
#define mem(arr,val) memset(arr,val,sizeof(arr))
#define sf(x) scanf("%d",&x)
#define sf2(x,y) scanf("%d %d",&x,&y)
#define sf3(x,y,z) scanf("%d %d %d",&x,&y,&z)
#define sl(x) scanf("%lld",&x)
#define sl2(x,y) scanf("%lld %lld",&x,&y)
#define sl3(x,y,z) scanf("%lld %lld %lld",&x,&y,&z)
#define sd(x) scanf("%lf",&x);
#define pii pair<int,int>
#define pLL pair<long long,long long>
#define pDB pair<double,double>
#define ff first
#define sn second
#define PRINT_CASE printf("Case %d: ",tc++)
#define PRINT_CASENL printf("Case %d:\n",tc++)
#define lnd tree[ind<<1]
#define rnd tree[(ind<<1)+1]
#define cnd tree[ind]
#define lndp b,(b+e)>>1,(ind<<1)
#define rndp ((b+e)>>1)+1,e,(ind<<1)+1
#define IN(a,x,y) (a>=x && a<=y)
#define popcountL __builtin_popcountll
#define popcount __builtin_popcount
/// int other=mask^((1<<n)-1);
typedef long long ll;
typedef long long int lln;
typedef unsigned long long int ull;
ll INF=1<<28;
//typedef long long lld;
const double pi=acos(-1.0);
int fx[]={1,-1,0,0}; //direction array
int fy[]={0,0,1,-1};
int dir[4][2]={1,0,-1,0,0,-1,0,1};
int knight[8][2]={1,2,1,-2,2,1,2,-1,-1,2,-1,-2,-2,1,-2,-1};
const long double EPS=1e-7;
//#define INF 10000
ll lcm(ll a,ll b)
{
return a/__gcd(a,b)*b;
}
//struct compare
//{
// bool operator()(const int& l, const int& r)
// {
// return l>r;
// }
//};
const int mx=201050;
const int mod=998244353;
//const int mod=1e9+7;
// Including Policy Based DS
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//
//////cout<<*X.find_by_order(1)<<endl;
////cout<<X.order_of_key(-5)<<endl;
int vis[mx];
int color[mx];
vector<pii>G[mx];
void dfs(int node,int last,int bosanolagbe)
{
vis[node]=1;
int bosanohoise=-1;
if(bosanolagbe!=0)
{
color[node]=bosanolagbe;
bosanohoise=bosanolagbe;
}
set<int>st;
st.insert(last);
st.insert(0);
for(auto x:G[node])
{
if(!vis[x.ff])
{
if(bosanohoise==-1)
{
if(x.sn!=last)
{
color[node]=x.sn;
bosanohoise=x.sn;
dfs(x.ff,x.sn,0);
}
else
{
st.insert(x.sn);
dfs(x.ff,0,x.sn);
}
}
else
{
if(x.sn==bosanohoise)
{
dfs(x.ff,x.sn,0);
}
else
{
dfs(x.ff,bosanohoise,x.sn);
}
}
}
}
if(bosanohoise==-1)
{
int c=1;
while(st.find(c)!=st.end())
{
c++;
}
color[node]=c;
}
}
int main()
{
// SPEED;
int t=1;
// sf(t);
while(t--)
{
int n,m;
sf2(n,m);
int x,y,z;
for(int i=0;i<m;i++)
{
sf3(x,y,z);
G[x].pb({y,z});
G[y].pb({x,z});
}
dfs(1,0,0);
for(int i=1;i<=n;i++)
{
if(color[i]==0)
{
cout<<"No"<<endl;
// return 0;
}
}
for(int i=1;i<=n;i++)
{
printf("%d\n",color[i]);
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define REP(i,m,n) for(int i=(m); i<(int)(n); i++)
#define RREP(i,m,n) for(int i=(int)((n)-1); i>=m; i--)
#define rep(i,n) REP(i,0,n)
#define rrep(i,n) RREP(i,0,n)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define fi first
#define se second
#define debug(...) {cerr<<"[L"<<__LINE__<<"] "; _debug(__VA_ARGS__);}
template<typename T>
string join(const vector<T>&v, string del=", "){ stringstream s;
for(auto x : v) s << del << x; return s.str().substr(del.size());
}
template<typename T>
ostream& operator<<(ostream& o, const vector<T>&v){
if(v.size()) o << "[" << join(v) << "]"; return o;
}
template<typename T>
ostream& operator<<(ostream& o, const vector<vector<T> >&vv){
int l = vv.size();
if(l){ o<<endl; rep(i,l) o << (i==0 ? "[ " : ",\n " ) << vv[i] << (i==l-1 ? " ]" : ""); }
return o;
}
template<typename T1, typename T2>
ostream& operator<<(ostream& o, const pair<T1, T2>& p){
return o << "(" << p.first << ", " << p.second << ")";
}
inline void _debug(){cerr<<endl;}
template<class First, class... Rest>
void _debug(const First& first, const Rest&... rest){cerr<<first<<" ";_debug(rest...);}
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
const double PI = (1*acos(0.0));
const double EPS = 1e-9;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3fLL;
const ll mod = 1e9 + 7;
inline void finput(string filename) {
freopen(filename.c_str(), "r", stdin);
}
int di[] = {0,0,1,-1};
int dj[] = {1,-1,0,0};
int h,w;
vvi d;
vector<string> g;
vector<pii> ts[26];
void bfs(pii s){
priority_queue<pair<int,pii>, vector<pair<int,pii>>, greater<pair<int,pii>>> Q;
Q.emplace(0,s);
d[s.fi][s.se] = 0;
while(!Q.empty()){
pair<int,pii> p = Q.top(); Q.pop();
int i = p.se.fi;
int j = p.se.se;
char c = g[i][j] - 'a';
if(0 <= c && c <= 26 && ts[c].size() > 0){
for(auto t : ts[c]){
int ti = t.fi;
int tj = t.se;
if(d[ti][tj] > d[i][j] + 1){
d[ti][tj] = d[i][j] + 1;
Q.emplace(d[ti][tj],pii(ti,tj));
}
}
while(ts[c].size() > 0) ts[c].pop_back();
}
rep(k,4){
int ni = i + di[k];
int nj = j + dj[k];
if(ni < 0 || h <= ni) continue;
if(nj < 0 || w <= nj) continue;
if(g[ni][nj] == '#') continue;
if(d[ni][nj] > d[i][j] + 1){
d[ni][nj] = d[i][j] + 1;
Q.emplace(d[ni][nj],pii(ni,nj));
}
}
}
}
int main(){
ios_base::sync_with_stdio(0);
// finput("./input");
cin >> h >> w;
g = vector<string>(h);
d = vvi(h,vi(w, INF));
pii start,goal;
rep(i,h){
cin >> g[i];
rep(j,w){
char c = g[i][j];
if('a' <= c && c <= 'z'){
ts[c-'a'].emplace_back(i,j);
}
if(c == 'S') start = pii(i,j);
if(c == 'G') goal = pii(i,j);
}
}
bfs(start);
if(d[goal.fi][goal.se] == INF) cout << -1 << endl;
else cout << d[goal.fi][goal.se] << endl;
return 0;
} |
// Problem: A - kcal
// Contest: AtCoder - AtCoder Beginner Contest 205
// URL: https://atcoder.jp/contests/abc205/tasks/abc205_a
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
#define int ll
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mod 1000000007
#define N 200010
signed main( )
{
fast
int testcases = 1 ;
// cin >> testcases ;
while( testcases -- )
{
float a,b ;
cin >> a>>b ;
float c=100;
float r = b/c;
float ans = r*a;
cout<<ans<<endl;
}
return 0 ;
} | #include <bits/stdc++.h>
using namespace std;
int cnt[200005];
int main()
{
int a,b;
cin>>a>>b;
for(int i=a;i<=b;i++)
{
for(int j=1;j*j<=i;j++)
{
if(i%j==0)
{
//j, i/j
cnt[j]++;
if(j*j!=i) cnt[i/j]++;
}
}
}
for(int i=200000;i>=1;i--)
{
if(cnt[i]>1)
{
cout<<i;
return 0;
}
}
}
|
//----AUTHOR:kkdrummer----/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
//#include <boost/multiprecision/cpp_int.hpp>
//using namespace boost::multiprecision;
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unordered_set<ll> usll;
typedef unordered_multiset<ll> umsll;
typedef multiset<ll> msll;
typedef set<ll> sll;
typedef vector<ll> vll;
typedef pair<ll,ll> pll;
typedef vector<pll> vpll;
typedef priority_queue<ll> pqll;
typedef vector<int> vi;
typedef set<int> si;
typedef multiset<int> msi;
typedef unordered_multiset<int> umsi;
typedef unordered_set<int> usi;
typedef pair<int,int> pi;
typedef vector<pi> vpi;
typedef priority_queue<int> pqi;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ind_si;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ind_sll;
typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update> ind_msi;
typedef tree<ll,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update> ind_msll;
#define in insert
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define be begin
#define en end
#define itr iterator
#define io ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define mo 1000000007
#define inf 8222372026854775807
#define ninf -inf
#define ima 1147483647
#define imi -ima
#define oncnt __builtin_popcount
#define zerobegin __builtin_clz
#define zeroend __builtin_ctz
#define parity __builtin_parity
#define all(x) x.be(),x.en()
#define eps 1e-9
#define coutd cout<<setprecision(10)<<fixed
#define mems(dp,x) memset(dp,x,sizeof(dp))
#define fbo find_by_order
#define ook order_of_key
#define upb upper_bound
#define lowb lower_bound
#define lte(v,x) (upb(all(v),x)-v.be())
#define gte(v,x) (v.end()-lowb(all(v),x))
#define gt(v,x) (v.en()-upb(all(v),x))
#define lt(v,x) (lowb(all(v),x)-v.be())
const ld PI= 3.1415926535897932384626433832792884197169399375105820974944;
inline ll modpow(ll x,ll n){if(n==0)return 1;if(x==0)return 0;if(n==1)return(x%mo);ll u=(modpow(x,n/2));u=(u*u)%mo;if(n%2!=0)u=(u*x%mo)%mo;return u;}
inline ll modinv(ll x){return modpow(x,mo-2);}
inline ll mmul(ll a,ll b){if(a>=mo)a=a%mo;if(b>=mo)b=b%mo;if(a*b>=mo)return(a*b)%mo;return(a*b);}
inline ll madd(ll a, ll b){if(a>=mo)a=a%mo;if(b>=mo)b=b%mo;if(a+b>=mo)return(a+b)%mo;return(a+b);}
inline ll msub(ll a, ll b){if(a>=mo)a=a%mo;if(b>=mo)b=b%mo;return(((a-b)%mo+mo)%mo);}
inline ll mdiv(ll a,ll bb){if(a>=mo)a=a%mo;ll b=modinv(bb);if(b>=mo)b=b%mo;if(a*b>=mo)return(a*b)%mo;return(a*b);}
inline ll gcd(ll a,ll b){return __gcd(a,b);}
inline ll lcm(ll a,ll b){return (a*b)/gcd(a,b);}
int main()
{ io
int testcases=1; // cin>>testcases;
while(testcases--)
{
int n;
cin>>n;
usi s;
int a[n];
for(int i=0;i<n;i++)cin>>a[i];
int k=0;
for(int i=0;i<n;i++)
{ s.in(a[i]);
while(s.find(k)!=s.en())
k++;
cout<<k<<"\n";
}
}return 0;} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pi pair<int,int>
#define pll pair<ll,ll>
#define ld long double
const int INF = 1e9 + 7;
void solve(){
set<int> possible;
ll B , C;
cin >> B >> C;
if(C == 1){
if(B == 0) cout << 1 << endl;
else cout << 2 << endl;
return;
}
ll l1 , r1 , l2 , r2;
l1 = (-B - C / 2) + (1 - C % 2);
r1 = (-B + C / 2) - (1 - C % 2);
l2 = (B - C / 2);
r2 = (B + C / 2 - 1);
if(l2 < l1){
swap(l1 , l2);
swap(r1 , r2);
}
// cout << "(" << l1 << ", " << r1 << ") " << endl;
// cout << "(" << l2 << ", " << r2 << ") " << endl;
if(l2 > r1) cout << ((r2 - l2 + 1) + (r1 - l1 + 1)) << endl;
else{
ll l = min(l1 , l2);
ll r = max(r1 , r2);
cout << (r - l + 1) << endl;
}
}
int main(){
ios :: sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int tt = 1;
for(int tc = 1; tc <= tt; tc++){
// cout << "Case #" << tc << ": ";
solve();
}
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\n"
template<class T> void chmax(T &a, const T b){ a = max(a, b); }
template<class T> void chmin(T &a, const T b){ a = min(a, b); }
long long gcd(long long a, long long b) { return (a % b) ? gcd(b, a % b) : b; }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
// ifstream in("input.txt");
// cin.rdbuf(in.rdbuf());
ll n;
cin >> n;
vector<ll> x(n), c(n);
rep(i, n) cin >> x[i] >> c[i];
map<ll, pair<ll, ll>> mc;
rep(i, n) {
if (mc.count(c[i]) == 0) {
mc[c[i]] = make_pair(x[i], x[i]);
}
else {
chmin(mc[c[i]].first, x[i]);
chmax(mc[c[i]].second, x[i]);
}
}
mc[n + 1] = make_pair(0, 0);
map<ll, ll> dp;
dp[0] = 0;
for(auto &x : mc) {
map<ll, ll> ndp;
ll minv = x.second.first, maxv = x.second.second;
for(auto &y : dp) {
// max -> min
{
ll val = y.second + abs(y.first - maxv) + abs(maxv - minv);
if (ndp.count(minv) == 0) {
ndp[minv] = val;
}
else {
chmin(ndp[minv], val);
}
}
// min -> max
{
ll val = y.second + abs(y.first - minv) + abs(minv - maxv);
if (ndp.count(maxv) == 0) {
ndp[maxv] = val;
}
else {
chmin(ndp[maxv], val);
}
}
}
dp = ndp;
}
ll ans = LONG_LONG_MAX;
for(auto &x : dp) chmin(ans, x.second);
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#define pb push_back
typedef long long ll;
using namespace std;
const int N=2e5+5;
ll mn[N],mx[N];
int main()
{
int n;
cin>>n;
for (int i=1;i<=n;++i)
mn[i]=2e9,mx[i]=-2e9;
for (int i=0;i<n;++i){
ll x,c;
cin >> x >> c;
mn[c]=min(mn[c],x);
mx[c]=max(mx[c],x);
}
ll dp[n + 1][2];
dp[0][0]=dp[0][1]=0;
bool first = true;
ll res = 0;
ll lmx = -1;
ll lmn = -1;
for (int i = 1; i <= n; ++i)
{
if (mn[i] == 2e9){
dp[i][0] = dp[i - 1][0];
dp[i][1] = dp[i - 1][1];
}
else
{
if (first)//第一次
{
dp[i][0]=mx[i]-mn[i]+abs(mx[i]);
dp[i][1]=mx[i]-mn[i]+abs(mn[i]);
first = false;
lmx = mx[i];
lmn = mn[i];
}
else
{
dp[i][0] = min(abs(lmx - mx[i]) + dp[i - 1][1], abs(lmn - mx[i]) + dp[i - 1][0]) + mx[i] - mn[i];
dp[i][1] = min(abs(lmx - mn[i]) + dp[i - 1][1], abs(lmn - mn[i]) + dp[i - 1][0]) + mx[i] - mn[i];
lmx = mx[i];
lmn = mn[i];
}
res = min(dp[i][0] + abs(mn[i]), dp[i][1] + abs(mx[i]));
}
}
cout << res << endl;
system("pause");
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int nax=32,mod=1e9+7;
int mul(int x,int y)
{
return x*y%mod;
}
int expo(int a,int b)
{
int res=1;
while(b)
{
if(b&1)
res=mul(res,a);
a=mul(a,a);
b>>=1;
}
return res;
}
int32_t main()
{
int tt=1;
//cin>>tt;
while(tt--)
{
int n,p,res=0;
cin>>n>>p;
res=mul(p-1,expo(p-2,n-1));
cout<<res;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
long N, K;
cin >> N >> K;
for(int i = 0; i < K; i++){
if(N % 200 == 0)
N = N / 200;
else
N = N * 1000 + 200;
}
cout << N << endl;
}
|
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define replr(i, l, r) for (int i = (l); i < (r); ++i)
const int mod = 1e9 + 7;
ll a, b, k;
ll ncr(ll n, ll r) {
ll res = 1;
rep(i, r) {
res *= n - i;
res /= (i + 1);
}
return res;
}
int main() {
cin >> a >> b >> k;
string ans = "";
k--;
while (a > 0 && b > 0) {
ll x = ncr(a + b - 1, b);
if (k < x) {
ans += "a";
a--;
} else {
ans += "b";
b--;
k -= x;
}
}
rep(i, a) ans += "a";
rep(i, b) ans += "b";
cout << ans << endl;
return 0;
} | /**
* author: souravrax
* created: 22.05.2021 17:50:47
**/
#include <bits/stdc++.h>
using namespace std;
#define all(x) begin(x), end(x)
#define len(x) int((x).size())
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type>
ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#ifdef LOCAL
#define dbg(...) cerr << "[ " << #__VA_ARGS__ << " ]:", dbg_out(__VA_ARGS__)
#else
#define dbg(...) (void)0x30
#endif
using ll = long long;
ll memo[35][35];
ll dp(ll a, ll b) {
if (a == 0 || b == 0) return memo[a][b] = 1;
ll ans = memo[a][b];
if (~ans) return ans;
return memo[a][b] = dp(a - 1, b) + dp(a, b - 1);
}
int32_t main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
ll a, b, k;
cin >> a >> b >> k, k--;
ll now = k;
memset(memo, -1, sizeof memo);
ll _a = a, _b = b;
string ans;
for (int i = 0; i < a + b; i++) {
if (_a == 0) {
ans.push_back('b');
_b--;
continue;
}
if (_b == 0) {
ans.push_back('a');
_a--;
continue;
}
if (dp(_a - 1, _b) > now) {
_a--;
ans.push_back('a');
} else {
now -= dp(_a - 1, _b);
_b--;
ans.push_back('b');
}
}
cout << ans << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
int main(){
int N, M, K, i, j;
scanf("%d%d%d", &N, &M, &K);
vector<int> is_hazure(N + 1, 0);
for(i = 0; i < K; i++){
int A;
scanf("%d", &A);
is_hazure[A] = 1;
}
int now = 0;
for(i = 0; i <= N; i++){
if(is_hazure[i] == 1){
now++;
}
else{
now = 0;
}
if(now >= M){
printf("-1\n");
return 0;
}
}
vector<long double> x(N + 1), s(N + 1);
long double l = 0, h, r = 1e11;
for(i = 0; i < 200; i++){
h = (l + r) / 2;
x[N] = 0;
s[N] = 0;
for(j = N - 1; j >= 0; j--){
if(is_hazure[j] == 1){
x[j] = h;
}
else{
x[j] = 1 + (s[j + 1] - s[min(N, j + M + 1)]) / M;
}
s[j] = s[j + 1] + x[j];
}
/* printf("h = %lf\n", h);
printf("x:\n");
for(j = 0; j <= N; j++){
printf("%lf ", x[j]);
}
printf("\n");
printf("s:\n");
for(j = 0; j <= N; j++){
printf("%lf ", s[j]);
}
printf("\n");
*/ if(x[0] > h){
l = h;
}
else{
r = h;
}
}
printf("%.10Lf\n", l);
return 0;
} | #include<bits/stdc++.h>
int main(){
using namespace std;
unsigned long N, M, K;
cin >> N >> M >> K;
const auto unavailable{[&N, &M, &K]{
vector<unsigned long> unavailable(N + 1);
vector<unsigned long> ban;
for(unsigned long i{0}, a; i < K; ++i){
cin >> a;
unavailable[a] = 1;
ban.push_back(a);
}
sort(begin(ban), end(ban));
for(unsigned long i{M - 1}; i < K; ++i)if(ban[i - M + 1] + M - 1 == ban[i])exit(puts("-1") & 0);
return unavailable;
}()};
const auto iteration{[&N, &M, &unavailable](long double x) -> long double {
vector<long double> dp(N + 1);
long double now{0}, c{0}, tmp{0}, y{0};
for(unsigned long i{N}; i--; ){
dp[i] = unavailable[i] ? x : now / M + 1;
now += dp[i];
now -= dp[min(N, i + M)];
}
return dp[0];
}};
constexpr long double eps{1e-10};
cout << setprecision(15) << [&iteration]{
auto [l, r]{[&iteration]{
if(2 > iteration(2))return make_pair(1.0L, 2.0L);
long double r{2};
while(r * r < iteration(r * r))r *= r;
return make_pair(r, r * r);
}()};
for(unsigned long i{0}; i < 20; ++i){
long double m{sqrt(l) * sqrt(r)};
(m < iteration(m) ? l : r) = m;
}
return l;
}() << endl;
return 0;
}
|
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(ll i=0;i<(n);i++)
#define pll pair<ll,ll>
#define pii pair<int,int>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define endl '\n'
#define ios ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define lb(c,x) distance(c.begin(),lower_bound(all(c),x))
#define ub(c,x) distance(c.begin(),upper_bound(all(c),x))
using namespace std;
inline int topbit(unsigned long long x){
return x?63-__builtin_clzll(x):-1;
}
inline int popcount(unsigned long long x){
return __builtin_popcountll(x);
}
inline int parity(unsigned long long x){//popcount%2
return __builtin_parity(x);
}
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
const ll INF=1e9+7;
int main(){
ll x;
cin >> x;
if(x<0) cout << 0 << endl;
else cout << x << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#include <math.h>
#include <iomanip>
#include <cstdint>
#include <string>
#include <sstream>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
#define rep(i,n) for (int i = 0; i < (n); ++i)
typedef long long ll;
using P=pair<ll,ll>;
const int INF=1001001001;
const int mod=1e9+7;
int main() {
int x;cin>>x;
cout<<max(0,x)<<endl;
return 0;
} |
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<utility>
using namespace std;
int N, ans, a[100006];
vector<pair<int, int>> s;
int main() {
scanf("%d", &N);
s.push_back(make_pair(0, 0));
for (int i = 0; i < N; i++) {
scanf("%d", &a[i]);
}
for (int i = 0; i <= N; i++) {
if (a[i] > s.back().first) {
s.push_back(make_pair(a[i], 1));
} else if (a[i] == s.back().first) {
s.back().second++;
} else {
while (s.back().first > a[i]) {
pair<int, int> temp = s.back();
s.pop_back();
ans = max(ans, temp.first * temp.second);
if (a[i] > s.back().first) {
s.push_back(make_pair(a[i], temp.second + 1));
} else if (a[i] == s.back().first) {
s.back().second += temp.second + 1;
} else {
s.back().second += temp.second;
}
}
}
}
printf("%d\n", ans);
return 0;
} | #include <iostream>
#include <cstdio>
#include <algorithm>
#include <stack>
using namespace std;
const int SIZE = 1e4 + 1;
struct range{
int l, r;
}R[SIZE];
int a[SIZE], n;
stack<int> stk;
int main()
{
cin >> n;
for(int i = 0; i < n; i++){
cin >> a[i];
}
for(int i = 0; i < n; i++){
if (stk.empty() || a[i] >= a[stk.top()]){
stk.push(i);
}
else{
while (!stk.empty() && a[stk.top()] > a[i]){
R[stk.top()].r = i - 1;
stk.pop();
}
stk.push(i);
}
}
while (!stk.empty()){
R[stk.top()].r = n - 1;
stk.pop();
}
//if (stk.empty()) cout << "empty" << endl;
for(int i = n - 1; i >= 0; i--){
if (stk.empty() || a[i] >= a[stk.top()]){
stk.push(i);
}else{
while (!stk.empty() && a[stk.top()] > a[i]){
R[stk.top()].l = i;
stk.pop();
}
stk.push(i);
}
}
while (!stk.empty()){
//cout << stk.top() << endl;
R[stk.top()].l = -1;
stk.pop();
}
/*for(int i = 0; i < n; i++){
printf("%d %d\n", R[i].l, R[i].r);
}*/
int ans = 0;
for(int i =0 ; i < n; i++){
ans = max(ans, (R[i].r - R[i].l) * a[i]);
}
for(int i = 0; i < n; i++){
//printf("%d %d\n", R[i].l, R[i].r);
}
cout << ans << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define endl '\n'
#define vec vector<ll>
#define mat vector<vector<ll> >
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef long double ld;
typedef complex<double> comp;
const ll INF=1e9+7;
const ll inf=INF*INF;
const ll MOD=1e9+7;
const ll mod=MOD;
const int MAX=200010;
signed main(){
ll n;cin>>n;
ll ans=0;
rep(i,n){
ll a;cin>>a;
ll b;cin>>b;
ans+=b*(b+1)/2;
ans-=a*(a-1)/2;
}
cout<<ans<<endl;
} | #include<bits/stdc++.h>
using namespace std;
#define int long long int
//int arr[500000]
int make_tuple(int l,int r)
{ int y= r-2*l+1;
if(y<0) return 0;
return (y*(y+1))/2;
}
int find_ans()
{ int l,r,ans;
cin>>l>>r;
ans= make_tuple(l,r);
return ans;
}
int32_t main()
{
int t;
cin>>t;
while(t>0)
{ cout<<find_ans()<<"\n";
t--;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
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;
}
} | #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
using namespace std;
int main(){
int N, K;
cin >> N >> K;
int ans = 0;
for (int i=1; i<=N; ++i){
for (int j=1; j<=K; ++j){
ans += stoi(to_string(i)+"0"+to_string(j));
}
}
cout << ans << endl;
return 0;
}
|
#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 int INF=0x3f3f3f3f;
const ll INFL=0x3f3f3f3f3f3f3f3f;
const int MOD=1000000007;
vector<int>E[300000];
ll sum[300000];
int par[300000];
int dep[300000];
void dfs1(int v,int p){
par[v]=p;
if(p!=-1)dep[v]=dep[p]+1;
for(int u:E[v]){
if(u==p)continue;
dfs1(u,v);
}
}
void dfs2(int v,int p){
if(p!=-1){
sum[v]+=sum[p];
}
for(int u:E[v]){
if(u==p)continue;
dfs2(u,v);
}
}
int A[300000],B[300000];
int main(){
int n;cin>>n;
rep(i,n-1){
int a,b;scanf("%d%d",&a,&b);a--;b--;
E[a].push_back(b);
E[b].push_back(a);
A[i]=a;B[i]=b;
}
dfs1(0,-1);
int q;cin>>q;
rep(i,q){
int t,e,x;scanf("%d%d%d",&t,&e,&x);e--;
int a=A[e],b=B[e];
if(dep[a]>dep[b]){
swap(a,b);
t=2-t+1;
}
if(t==1){
sum[0]+=x;
sum[b]-=x;
}
else{
sum[b]+=x;
}
}
dfs2(0,-1);
rep(i,n){
printf("%lld\n",sum[i]);
}
} | #include <bits/stdc++.h>
using namespace std;
const int N = 2e5+69;
int entry[N];
long long scores[N], toAdd[N], tot;
vector<int> tree[N];
void dfs(int c, int p) {
scores[c] = scores[p] + toAdd[c];
entry[c] = entry[p] + 1;
for (int i : tree[c])
if (i != p)
dfs(i, c);
}
void query(int x, int y, int z) {
if (entry[x] > entry[y])
toAdd[x] += z;
else {
tot += z;
toAdd[y] -= z;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, q, x, y, z;
cin >> n;
int a[n][2];
for (int i = 1; i < n; ++i) {
cin >> x >> y;
a[i][0] = x, a[i][1] = y;
tree[x].push_back(y);
tree[y].push_back(x);
}
dfs(1, 0);
cin >> q;
while (q--) {
cin >> x >> y >> z;
if (x == 1)
query(a[y][0], a[y][1], z);
else
query(a[y][1], a[y][0], z);
}
dfs(1, 0);
for (int i = 1; i <= n; ++i)
cout << scores[i] + tot << '\n';
} |
#include <bits/stdc++.h>
using namespace std;
using LL=long long;
using ULL=unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)
int GCD(int a,int b){ return b?GCD(b,a%b):a; }
bool sieve[32768]={};
vector<int> P;
vector<pair<int,int>> factorize(int N){
vector<pair<int,int>> res;
for(int p:P){
if(N%p) continue;
if(N<p) break;
res.push_back({p,0});
while(N%p==0){ N/=p; res.back().second++; }
}
if(N!=1) res.push_back({N,1});
return move(res);
}
vector<int> divs(vector<pair<int,int>> F){
vector<int> res = {1};
for(auto f:F){
int t=res.size() * f.second;
rep(i,t) res.push_back(res[i] * f.first);
}
return move(res);
}
int N;
int A[2000];
map<int,int> D;
int m;
int main(){
cin>>N; rep(i,N) cin>>A[i];
m=1000000000; rep(i,N) m=min(m,A[i]);
for(int i=2; i<32768; i++) if(!sieve[i]){
P.push_back(i);
for(int j=i*i; j<32768; j+=i) sieve[j]=true;
}
rep(i,N){
auto B=divs(factorize(A[i]));
for(int d:B){
int x=D[d];
if(x==0) D[d]=A[i];
else D[d]=GCD(x,A[i]);
}
}
int ans=0;
for(auto d:D) if(d.first==d.second) if(d.first<=m) ans++;
cout<<ans<<endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,b) for(int i=a;i<b;i++)
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define FORe(i,a,b) for(int i=a;i<=b;i++)
#define FORr(i,a,b) for(int i=a;i>b;i--)
#define FORre(i,a,b) for(int i=a;i>=b;i--)
#define pb push_back
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
template<class T> void sort_unique(vector<T> &v){
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
}
void solve(){
int n;
cin >> n;
vector<int> a(n);
rep(i,0,n) cin >> a[i];
sort(a.begin(),a.end());
vector<int> v;
rep(i,0,n){
for(int k = 1; k * k <= a[i]; k++){
if(a[i]%k == 0){
if(k<=a[0]) v.pb(k);
if(a[i]/k <= a[0]) v.pb(a[i]/k);
}
}
}
sort_unique(v);
ll ans = 0;
int len = v.size();
rep(i,0,len){
int factor = v[i];
int g = 0;
rep(j,0,n) if(a[j]%factor == 0) g = __gcd(g,a[j]);
if(g == factor) ans++;
}
cout << ans << endl;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int t;
t = 1;
while(t--){
solve();
}
return 0;
} |
#include <bits/stdc++.h>
typedef long long ll;
typedef std::vector<int> vi;
typedef std::vector<std::vector<int>> vii;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
#define rep(i, n) for(int i = 0; i < (int)(n); ++i)
#define repi(i, a, b) for(int i = a; i < (int)(b); ++i)
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define inf 2147483647
#define inf64 9223372036854775807
#define Q queue
#define PQ priority_queue
#define pb push_back
#define mp make_pair
#define F first
#define S second
using namespace std;
const int MX = 1510;
int di[] = {-1, 0, 1, 0};
int dj[] = {0, 1, 0, -1};
bool ok[MX][MX] = {};
bool light[MX][MX] = {};
bool wall[MX][MX] = {};
bool visited[MX][MX] = {};
bool memo[MX][MX] = {};
bool search(int k, int i, int j, int h, int w) {
if(i < 0 || j < 0 || i >=h || j >= w) return false;
if(wall[i][j]) return false;
if(light[i][j]) return true;
if(visited[i][j]) return memo[i][j];
visited[i][j] = true;
return memo[i][j] = search(k, i+di[k], j+dj[k], h, w);
}
void solution()
{
int h, w, n, m;
cin >> h >> w >> n >> m;
rep(i, n)
{
int a, b;
cin >> a >> b;
light[a-1][b-1] = true;
}
rep(i, m)
{
int a, b;
cin >> a >> b;
wall[a-1][b-1] = true;
}
rep(k, 4) {
rep(i, h) rep(j, w) visited[i][j] = false;
rep(i, h) rep(j, w) if( search(k, i, j, h, w)) ok[i][j] = true;
}
int ans = 0;
rep(i, h) rep(j, w) if(ok[i][j]) ++ans;
cout << ans << endl;
}
int main()
{
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(15);
solution();
return 0;
}
| #define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <algorithm>
#include <climits>
#include <cstring>
#include <cmath>
#include <stack>
#include <iomanip>
#include <tuple>
#include <functional>
#include <cfloat>
#include <map>
#include <set>
#include <array>
#include <stdio.h>
#include <string.h>
#include <random>
#include <cassert>
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
using namespace std;
#define int long long
#define CONTAINS_VEC(v,n) (find((v).begin(), (v).end(), (n)) != (v).end())
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ARY_SORT(a, size) sort((a), (a)+(size))
#define REMOVE(v,a) (v.erase(remove((v).begin(), (v).end(), (a)), (v).end()))
#define REVERSE(v) (reverse((v).begin(), (v).end()))
#define ARY_REVERSE(v,a) (reverse((v), (v)+(a)))
#define REP(i, n) for (int (i)=0; (i) < (n); (i)++)
#define REPE(i, n) for (int (i)=0; (i) <= (n); (i)++)
#define CONTAINS_MAP(m, a) ((m).find((a)) != m.end())
#define CONTAINS_SET(m, a) ((m).find((a)) != m.end())
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; exit(0); }
void Yes() { cout << "Yes" << endl; exit(0); }
void No() { cout << "No" << endl; exit(0); }
int N, M;
int X[101];
vector<int>Y[20];
vector<int>Z[20];
int _dp[1000000];
int bit_count(ull v)
{
ull count = (v & 0x5555555555555555ULL) + ((v >> 1ULL) & 0x5555555555555555ULL);
count = (count & 0x3333333333333333ULL) + ((count >> 2ULL) & 0x3333333333333333ULL);
count = (count & 0x0f0f0f0f0f0f0f0fULL) + ((count >> 4ULL) & 0x0f0f0f0f0f0f0f0fULL);
count = (count & 0x00ff00ff00ff00ffULL) + ((count >> 8ULL) & 0x00ff00ff00ff00ffULL);
count = (count & 0x0000ffff0000ffffULL) + ((count >> 16ULL) & 0x0000ffff0000ffffULL);
return (int)((count & 0x00000000ffffffffULL) + ((count >> 32ULL) & 0x00000000ffffffffULL));
}
signed main()
{
cin >> N >> M;
//REP(i, M) cin >> X[i] >> Y[i] >> Z[i];
REP(i, M)
{
int x, y, z;
cin >> x >> y >> z;
Y[x].push_back(y);
Z[x].push_back(z);
}
int dp_max = (1LL << N);
_dp[0] = 1;
for (int i = 0; i < dp_max; i++)
{
int prev = _dp[i];
for (int j = 0; j < N; j++)
{
int cur = (1LL << j);
if ((i & cur) == 0)
{
int bitcnt = bit_count(i | cur);
if (Y[bitcnt].size() == 0)
{
_dp[i | cur] += _dp[i];
}
else
{
bool is_valid = true;
for (int k = 0; k < Y[bitcnt].size(); k++)
{
int y = Y[bitcnt][k];
int z = Z[bitcnt][k];
int mask = (1LL << y) - 1;
int b = ((i | cur) & mask);
int cnt = bit_count(b);
if (cnt > z)
{
is_valid = false;
}
}
if (is_valid)
{
_dp[i | cur] += _dp[i];
}
}
}
else
{
}
}
}
cout << _dp[(1LL << N) - 1] << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using str = string;
using pi = pair<int,int>;
using pl = pair<ll,ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
#define sz(x) (int)(x).size()
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
constexpr int setbits(int x) { return __builtin_popcount(x); }
constexpr int bits(int x) { return 32-__builtin_clz(x); }
str binary(int x) { return bitset<32>(x).to_string(); }
int decimal(str x) { return stoi(x,nullptr,2); }
ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); }
ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); }
const int nax = INT_MAX;
const int nim = INT_MIN;
const int mod = 1000000007;
void solve(){
ll n, m, t;
cin >> n >> m >> t;
vpi a(m);
ll p = n;
for(int i = 0; i < m; ++i)
cin >> a[i].fi >> a[i].se;
n -= a[0].fi;
if(n <= 0){
cout << "No" << endl;
return;
}
n += (a[0].se - a[0].fi);
n = min(p, n);
for(int i = 1; i < m; ++i){
n -= (a[i].fi - a[i-1].se);
if(n <= 0){
cout << "No" << endl;
return;
}
n += (a[i].se - a[i].fi);
n = min(p, n);
}
n -= (t - a[m-1].se);
if(n <= 0)
cout << "No" << endl;
else
cout << "Yes" << endl;
}
int main(){
ios_base::sync_with_stdio(false), cin.tie(nullptr);
int t = 1;
//cin >> t;
while(t--)
solve();
}
| #include<iostream>
using namespace std;
int main(){
int N,S,D;
cin >> N >> S >> D;
for(int i=0;i<N;i++)
{
int a,b;
cin >> a >> b;
if(a < S && b > D){
cout << "Yes" << endl;
return 0;
}
}
cout << "No" << endl;
} |
#include <bits/stdc++.h>
#define ll long long
#define map unordered_map
#define set unordered_set
#define l_l pair<ll, ll>
#define P pair<ll, ll>
#define vll vector<ll>
#define mll map<ll, ll>
#define mp make_pair
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define rev(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define revs(i, n) for (int i = ((int)(n)); i > 0; --i)
// template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
// template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
using namespace std;
const ll MOD = 1000000007LL;
const ll INF = (1LL << 60LL);
// template <class T> void plus_mod(T &a, const T &b) {a = (a + b) % MOD;}
template <class T>
void join(vector<T> list) {
rep(i, list.size()) {
if (i >= 1) {
cout << " ";
}
cout << (list[i]);
}
cout << ("") << endl;
}
int main() {
// std::cout << std::fixed << std::setprecision(10);
ll K, N, M;
scanf("%lld %lld %lld", &K, &N, &M);
ll ans = 0;
vector<ll> alist;
vector<ll> alist2;
vll blist;
vector<l_l> s_list;
ll total = 0;
rep(i, K) {
ll v;
scanf("%lld", &v);
alist.emplace_back(v);
alist2.emplace_back(v * M);
ll n = v * M / N;
blist.emplace_back(n);
total += n;
ll am = v * M % N;
s_list.emplace_back(mp(am, i));
}
sort(s_list.begin(), s_list.end(), greater<P>());
//
// cout << ("==================") << endl;
ll rest = M - total;
// cout << "rest:" << (rest) << endl;
rep(i, s_list.size()) {
if (rest == 0) break;
// auto s = s_list[i].first;
auto index = s_list[i].second;
blist[index]++;
rest--;
}
join(blist);
// cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> iint;
typedef pair<ll,ll> llll;
#define ALL(x) (x).begin(),(x).end()
const ll zero = 0;
const ll one = 1;
const ll INF = 9223372036854775807; //10^18
const int inINF = 2147483647; //10^9
const ll MOD = 1000000007; //10^9+7
const ll MOD2 = 998244353;
void Yes() {printf("Yes\n");}
void No() {printf("No\n");}
void YES() {printf("YES\n");}
void NO() {printf("NO\n");}
int main(){
ll K, N, M;
cin >> K >> N >> M;
vector<int> A(K);
for (int i = 0; i < K; i++) {
cin >> A[i];
}
ll ok = 2e18+5;
ll ng = -1;
while(abs(ok-ng) > 1){
ll mid = (ok+ng)/2;
ll mi, ma;
mi = 0; ma = 0;
for (ll i = 0; i < K; i++) {
if(A[i]*M - mid <= 0){
mi += 0;
}
else{
mi += (A[i]*M - mid + N-1) / N;
}
ma += min((mid + A[i]*M)/N, M);
}
if(mi <= M && M <= ma){
ok = mid;
}
else{
ng = mid;
}
}
vector<ll> MI(K), MA(K);
for (ll i = 0; i < K; i++) {
if(A[i]*M - ok <= 0){
MI[i] = 0;
}
else{
MI[i] = (A[i]*M - ok + N-1) / N;
}
MA[i] = min((ok + A[i]*M)/N, M);
}
vector<ll> B(K);
ll S = 0;
for (ll i = 0; i < K; i++) {
B[i] = MI[i];
S += MI[i];
}
for (ll i = 0; i < K; i++) {
if(M == S) break;
B[i] += min(M - S, MA[i] - MI[i]);
S += min(M - S, MA[i] - MI[i]);
}
for (ll i = 0; i < K; i++) {
cout << B[i] << " ";
}
cout << endl;
} |
#include "bits/stdc++.h"
// Nagpur ki public bole to taklif
using namespace std;
#define int long long
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define all(x) (x).begin(), (x).end()
#define uniq(v) (v).erase(unique(all(v)), (v).end())
#define sz(x) (int)((x).size())
#define fr first
#define sc second
#define pii pair<int, int>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define mem1(a) memset(a, -1, sizeof(a))
#define mem0(a) memset(a, 0, sizeof(a))
#define ppc __builtin_popcount
#define ppcll __builtin_popcountll
#define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
template <typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &a)
{
in >> a.fr >> a.sc;
return in;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> a)
{
out << a.fr << " " << a.sc;
return out;
}
template <typename T, typename T1>
T amax(T &a, T1 b)
{
if (b > a)
a = b;
return a;
}
template <typename T, typename T1>
T amin(T &a, T1 b)
{
if (b < a)
a = b;
return a;
}
template <typename T_vector>
void output_vector(const T_vector &v, bool addOne = false, int start = -1, int end = -1)
{
if (start < 0)
start = 0;
if (end < 0)
end = (int)v.size();
for (int i = start; i < end; i++)
cout << v[i] + (addOne ? 1 : 0) << (i < end - 1 ? ' ' : '\n');
}
int ceil(int x, int y)
{
return x / y + (x % y > 0);
}
const long long INF = 1e18;
const int32_t M = 1e9 + 7;
const int32_t MM = 998244353;
const int N = 3e5 + 10;
int bit[N];
void update(int i, int x)
{
i++;
while (i <= N)
{
bit[i] += x;
i += i & -i;
}
}
int sum(int i)
{
i++;
int ans = 0;
while (i > 0)
{
ans += bit[i];
i -= i & -i;
}
return ans;
}
void solve()
{
int n;
cin >> n;
int v[n + 1];
for (int i = 1; i <= n; ++i)
{
cin >> v[i];
}
int inversionCount = 0;
unordered_map<int, int> m;
for (int i = 1; i <= n; ++i)
{
int tmp = 1LL * (i - 1LL) - sum(v[i]);
inversionCount += tmp;
update(v[i], 1);
}
for (int i = 1; i <= n; ++i)
{
cout << inversionCount << "\n";
// cout << v[i] << " -> " << (n - 1LL) << " - " << (2LL * v[i]) << "\n";
inversionCount += (n - 1LL) - (2LL * v[i]);
}
}
signed main()
{
fast
#ifndef ONLINE_JUDGE
// For getting input from input.txt file
freopen("input.txt", "r", stdin);
// Printing the Output to output.txt file
freopen("output.txt", "w", stdout);
#endif
cout << setprecision(12);
cout << fixed;
int t = 1;
// cin>>t;
while (t--)
solve();
return 0;
}
| #include <cstdio>
#include <cstring>
#include <algorithm>
#define rep(a,b,c) for(int a=b;a<=c;a++)
#define per(a,b,c) for(int a=b;a>=c;a--)
const int N=105;
const int p=1e9+7;
#define int long long
template<class T>inline void read(T &x) {
T f=1;x=0;char s=getchar();
while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
x*=f;
}
int n,m,k,a[N*N],b[N*N],f[N],iv,ml,inv2=((p+1)>>1),du[N];
struct matrix {
int a[N][N];
matrix() { memset(a,0,sizeof(a)); }
inline void init() { rep(i,1,n) a[i][i]=1; }
}Ans,mul;
inline matrix operator *(matrix a,matrix b) {
matrix c;
rep(i,1,n) rep(j,1,n) rep(k,1,n) (c.a[i][j]+=(a.a[i][k]*b.a[k][j]%p))%=p;
return c;
}
inline matrix ksm(matrix a,int b) { matrix c; c.init(); for(;b;b>>=1,a=a*a) if(b&1) c=c*a; return c; }
inline int ksm(int a,int b) { int ans=1; for(;b;b>>=1,(a*=a)%=p) if(b&1) (ans*=a)%=p; return ans; }
signed main() {
read(n); read(m); read(k); iv=ksm(m,p-2);
rep(i,1,n) read(f[i]);
rep(i,1,m) read(a[i]),read(b[i]),du[a[i]]++,du[b[i]]++;
rep(i,1,n) Ans.a[1][i]=f[i];
rep(i,1,n) mul.a[i][i]=(iv*(m-du[i]))%p;
rep(i,1,m) {
(mul.a[a[i]][a[i]]+=(iv*inv2%p))%=p;
(mul.a[b[i]][b[i]]+=(iv*inv2%p))%=p;
(mul.a[a[i]][b[i]]+=(iv*inv2%p))%=p;
(mul.a[b[i]][a[i]]+=(iv*inv2%p))%=p;
}
mul=ksm(mul,k); Ans=Ans*mul;
rep(i,1,n) printf("%lld\n",Ans.a[1][i]);
} |
#include <bits/stdc++.h>
#include <vector>
#include<math.h>
#include<string.h>
using namespace std;
#define MAX 200005
#define MOD 998244353
#define SMOD 998244353
#define ROOT 512
#define GMAX 20
#define INF 2000000000
#define EPS 0.000000001
#define NIL 0
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
int prime[MAX+1],val[MAX+1];
void sieve()
{
int i,j;
for(i=2; i*i<=MAX; i++)
{
if(prime[i]) continue;
for(j=i; j*i<=MAX; j++)
{
if(prime[i*j]==0) prime[i*j]=i;
}
}
for(i=2;i<=MAX;i++)
{
if(prime[i]==0)
{
val[i]=1;
}
else
{
val[i]=1+val[i/prime[i]];
}
}
}
int main()
{
sieve();
int n,i;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("%d ",val[i]+1);
}
return 0;
}
| #include<bits/stdc++.h>
#define FastRead \
ios_base::sync_with_stdio(false); \
cin.tie(0);
#define ll long long
#define endl "\n"
#define f for
#define ml ll t,g; cin>>t; f(g=0;g<t;g++)
#define pi acos(-1)
using namespace std;
bool cmp(const pair<string,ll> &a,
const pair<string,ll> &b)
{
if(a.second!=b.second)
return (a.second > b.second);
else
return (a.first < b.first);
}
int main()
{
FastRead
ll n,i,m,s=0,c=0;
string a;
cin>>a;
f(i=0; i<a.size(); i++)
{
s=s+((ll)a[i]-48);
}
if(s%3==0)
cout<<"0";
else
{
f(i=0; i<a.size(); i++)
{
if((s-((ll)a[i]-48)!=0)&&(s-((ll)a[i]-48))%3==0)
{
c=1;
break;
}
}
if(c>0)
cout<<"1";
else if(a.size()==1 || a.size()==2)
cout<<"-1";
else
cout<<"2";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using namespace chrono;
typedef int64_t ll;
typedef long double ld;
typedef unsigned long long ULL;
#define endl "\n"
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb push_back
void read(vector<int> &a) {for (auto &x : a)cin >> x;}
void read(vector<ll> &a) {for (auto &x : a)cin >> x;}
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c> {i, j}; }
sim > auto dud(c * x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifndef ONLINE_JUDGE
~debug() { cerr << endl; }
eni( != ) cerr << boolalpha << i; ris;
}
eni( == ) ris << range(begin(i), end(i));
}
sim, class b dor(pair < b, c > d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
const int MOD = 1e9 + 7;
const int INF = (int)2e9 + 7;
const ll LINF = (ll)1e18;
const ld PI = 3.1415926535897932384626433832795;
void solve() {
int a, b, c;
cin >> a >> b >> c;
if (a == b) {
cout << "=";
}
else if (abs(a) == abs(b)) {
if (c % 2 == 0) {
cout << "=";
}
else if (a > 0 && b < 0) {
cout << ">";
}
else if (b > 0 && a < 0) {
cout << "<";
}
else {
assert(false);
}
}
else if (a == 0 || b == 0) {
if (a == 0 && b == 0) {
cout << "=";
}
else if (a == 0) {
if (b > 0) {
cout << "<";
}
else if (b < 0 && c % 2 == 0) {
cout << "<";
}
else {
cout << ">";
}
}
else {
if (a > 0) {
cout << ">";
}
else if (a < 0 && c % 2 == 0) {
cout << ">";
}
else {
cout << "<";
}
}
}
else if (a < 0 && b < 0) {
if (c % 2 == 0) {
if (abs(a) < abs(b)) {
cout << "<";
}
else {
cout << ">";
}
}
else if (a < b) {
cout << "<";
}
else {
cout << ">";
}
}
else if (a > 0 && b > 0) {
if (a < b) {
cout << "<";
}
else {
cout << ">";
}
}
else if (a < 0 && b > 0) {
if (c % 2 == 0) {
if (abs(a) > abs(b)) {
cout << ">";
}
else {
cout << "<";
}
}
else {
cout << "<";
}
}
else if (a > 0 && b < 0) {
if (c % 2 == 0) {
if (abs(a) > abs(b)) {
cout << ">";
}
else {
cout << "<";
}
}
else {
cout << ">";
}
}
}
int32_t main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
//cin >> t;
auto t1 = high_resolution_clock::now();
for (int tt = 1; tt <= t; tt++)
{
//cout << "Case #" << tt << ": ";
solve();
}
auto t2 = high_resolution_clock::now();
auto time = duration_cast<duration<double>>(t2 - t1);
cerr << "Time elapsed = " << time.count() << endl;
}
| #include<bits/stdc++.h>
using namespace std;
int main(){
int A, B, C;
cin>>A>>B>>C;
if(A>=0 && B>=0){
if(A>B) cout<<">";
else if(B>A) cout<<"<";
else cout<<"=";
}
else if(A<0 && B>=0 && C%2==0){
A = -A;
if(A>B) cout<<">";
else if(B>A) cout<<"<";
else cout<<"=";
}
else if(A>=0 && B<0 && C%2==0){
B = -B;
if(A>B) cout<<">";
else if(B>A) cout<<"<";
else cout<<"=";
}
else if(A<0 && B<0 && C%2==0){
if(A<B) cout<<">";
else if(B<A) cout<<"<";
else cout<<"=";
}
else{
if(A>B) cout<<">";
else if(A<B) cout<<"<";
else cout<<"=";
}
return 0;
} |
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class A> ostream& operator<<(ostream &cout, vector<A> const &v) {cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";};
template<class A, class B> ostream& operator<<(ostream &cout, const pair<A,B> &x) {return cout << "(" <<x.first << ", " << x.second << ")";};
template<class T> void pv(T a, T b) {cerr << "["; for (T i = a; i != b; ++i) {if (i != a) cerr << ", "; cerr << *i;}cerr << "]\n";}
void _print() {cerr << "]\n";}
template<class T, class... V> void _print(T t, V... v) {cerr << t; if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "[" << #x << "] = [", _print(x)
#define fi first
#define se second
#define SZ(x) (int)((x).size())
#define pii pair<int,int>
#define uid(x,y) uniform_int_distribution<int>(x,y)(rng)
int a[100005];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for (int q = 1; q <= n; q++) cin >> a[q];
sort(a,a+(n+1));
ll ans = 1;
for (int q = 1; q <= n; q++) {
ans *= (a[q]-a[q-1]+1);
ans %= 1000000007;
}
cout << ans << '\n';
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define rep(i,a,b) for( i=a;i<b;++i)
#define repr(i,a,b) for( i=a;i>=b;i--)
#define up upper_bound
#define lb lower_bound
#define t() int test;cin>>test;while(test--)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cout << name << " : " << arg1 << endl;
//use cerr if u want to display at the bottom
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif
// const ll mod = 1e9 + 7;
// ll nCr(ll n,ll r){
// ll f[1001] = {0};
// f[0] = f[1] = 1;
// for(ll i = 2;i <= n;i++){
// f[i] = 1;
// for(ll j = i-1;j >= 1;j--){
// f[j] = (f[j]+f[j-1])%mod;
// }
// }
// return f[r];
// }
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
IOS;
ll n;
cin >> n;
ll m = (ll)(pow(2,n) + 0.5);
ll a[m];
for(ll i = 0;i < m;i++){
cin >> a[i];
}
ll maxi = 0,index = 0;
vector<pair<ll,ll>> nums;
for(ll i = 0;i < m/2;i++){
if(a[i] > maxi){
maxi = a[i];
index = i;
}
}
nums.push_back({maxi,index});
maxi = 0,index = 0;
for(ll i = m/2;i < m;i++){
if(a[i] > maxi){
maxi = a[i];
index = i;
}
}
nums.push_back({maxi,index});
if(nums[0].first > nums[1].first){
cout << nums[1].second + 1 << "\n";
}else{
cout << nums[0].second + 1 << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
//#include <atcoder/all.hpp>
using namespace std;
//using namespace atcoder;
#define all(v) v.begin(), v.end()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define PA pair<int, int>
typedef long long ll;
#define rep(i, z, n) for(int i = z; i < n; i++)
//const ll INF = 1e9;
const ll INF = 1000000007;
//const ll MOD = 9223372036854775807;
//const ll MOD2 = 998244353;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
int N, M;
vector<int> H(200001, -1), W(200001, -1);
vector<int> sum1(100000, -1);
vector<int> sum2(100000, -1);
ll cal(int tea, int posi){
ll ans = abs(tea - H[2 * posi]);
//cout << posi << " " << ans << endl;
if(posi >= 1) ans += sum1[posi - 1];
//cout << ans << endl;
if(posi < (N - 1)/2) ans += sum2[posi];
//cout << ans << endl;
//cout << endl;
return ans;
}
int main(){
cin >> N >> M;
H.resize(N);
W.resize(M);
rep(i, 0, N) cin >> H[i];
rep(i, 0 ,M) cin >> W[i];
sort(all(H));
sort(all(W));
rep(i, 0, (N - 3) / 2 + 1){
sum1[i] = (H[2 * i + 1] - H[2 * i]);
sum2[i] = (H[2 * i + 2] - H[2 * i + 1]);
if(i >= 1){
sum1[i] += sum1[i - 1];
}
}
for(int i = (N - 3) / 2; i >= 1; i--){
sum2[i - 1] += sum2[i];
}
int position = 0;
ll ans = INF;
rep(i, 0, M){
while((W[i] > H[position]) && (position < N)){
position++;
}
//cout << cal(W[i], position/2) << " " << position << endl;
ans = min(ans, cal(W[i], position/2));
}
rep(i, 0 ,(N - 1)/2){
//cout << i << " " << sum2[i] << endl;;
}
cout << ans << endl;
}
| #include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
int k;
cin>>k;
long long int s(0);
for(int i=1;i<=n; ++i)
{
for(int j=1;j<=k; ++j)
{
s+=100*i+j;
}
}
cout<<s;
//else
}
|
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define sz(v) (int)v.size()
#define ps(y) cout << fixed << setprecision(y)
#define ms(arr, v) memset(arr, v, sizeof(arr))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define trav(x, v) for(auto &x: v)
#define w(t) int t; cin >> t; while(t--)
#define rep0(i, n) for(int i = 0; i < n; i++)
#define rrep0(i, n) for(int i = n - 1; i >= 0; i--)
#define rep1(i, n) for(int i = 1; i <= n; i++)
#define rrep1(i, n) for(int i = n; i > 0; i--)
#define inp(arr, n) rep0(i, n) cin >> arr[i];
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define rrep(i, a, b) for(int i = a; i >= b; i--)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef map<ll, ll> mii;
typedef map<char, ll> mci;
typedef priority_queue<ll> pq_mx;
typedef priority_queue<ll, vi, greater<>> pq_mn;
typedef tree<ll, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> pbds;
/*
* find_by_order(i) -> returns an iterator to the element at ith position (0 based)
* order_of_key(i) -> returns the position of element i (0 based)
*/
const int N = 2e6 + 5;
const int mod = 1e9 + 7;
//const int mod = 998244353;
const ll inf = 1e18;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void fio() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
}
const int M = 1e7 + 5;
ll spf[M], mobius[M];
vi primes;
void sieve() {
rep(x, 2, 1e7) {
if (spf[x] == 0) spf[x] = x, primes.pb(x);
for (int y = 0; y < sz(primes) and primes[y] <= spf[x] and x * primes[y] <= 1e7; y++)
spf[x * primes[y]] = primes[y];
}
}
void Mobius() {
/*
* mobius[x] = 1 => x is a square-free +ve int with even no. of prime factors
* mobius[x] = -1 => x is a square-free +ve int with odd no. of prime factors
* mobius[x] = 0 => x has a squared prime factor
*/
rep1(x, 1e7) {
if (x == 1) mobius[x] = 1;
else {
if (spf[x] == spf[x / spf[x]]) mobius[x] = 0;
else mobius[x] = -mobius[x / spf[x]];
}
}
}
int main() {
fio();
sieve();
Mobius();
int l, r;
cin >> l >> r;
if(l == 1) ++l;
ll res = 0;
rep(x, 2, r) {
ll cur = r / x - (l - 1) / x;
res -= cur * cur * mobius[x];
}
// cout << res << endl;
rep(x, l, r) {
--res;
for (int y = 2 * x; y <= r; y += x) res -= 2;
}
cout << res;
return 0;
} | #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 int MAX_N = 300005;
int cnt[MAX_N];
int main()
{
ll N,K;
cin >> N >> K;
//N = 300000;
rep(i,N)
{
int a;
//a = 0;
cin >> a;
cnt[a]++;
}
ll ans = 0;
while(K > ll(0))
{
int idx = 0;
if (cnt[idx] == 0) break;
while(cnt[idx] > 0)
{
cnt[idx]--;
idx++;
}
ans += idx;
K--;
}
cout << ans << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define int long long
#define pb push_back
#define pii pair<int,int>
#define vi vector<int>
#define mii map<int,int>
#define pqb priority_queue<int>
#define pqs priority_queue<int,vi,greater<int> >
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x,y) cout<<setprecision(y)<<x
#define mk(arr,n,type) type *arr=new type[n];
#define w(x) int x; cin>>x; while(x--)
#define pw(b,p) pow(b,p) + 0.1
#define endl "\n"
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void __print(int32_t 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
char myHeap[200 * 1024 * 1024];
int sz = 0;
void assert_tle(bool q) { while (!q); }
void* operator new ( std::size_t count ) {
sz += count;
assert_tle(sz <= 200 * 1024 * 1024);
return myHeap + sz - count;
}
void operator delete (void *ptr) { }
void fastIO()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}
vector<int> ans;
void dfs(vector<pii> graph[],int cur)
{
for(pii i: graph[cur])
{
if(ans[i.ff]!=-1)
{
continue;
}
else
{
if(ans[cur]!=i.ss)
{
ans[i.ff]=i.ss;
}
else
{
ans[i.ff]=ans[cur]==0?1:(ans[cur]-1);
}
dfs(graph,i.ff);
}
}
}
void solve()
{
int n,m;
cin>>n>>m;
ans.assign(n,-1);
vector<pii> graph[n];
for(int i=0;i<m;i++)
{
int a,b,c;
cin>>a>>b>>c;
a--;b--;
c--;
graph[a].pb({b,c});
graph[b].pb({a,c});
}
ans[0]=0;
dfs(graph,0);
for(int i: ans)
{
cout<<i+1<<endl;
}
}
int32_t main()
{
fastIO();
// w(t)
{
solve();
cout<<endl;
}
return 0;
} | #include<iostream>
#define val first
#define type second
using namespace std;
typedef pair<int, int> Filter;
const int FILTER = 200005;
const int QUERY = 200005;
const long long INF = 1LL << 60;
int nFilter;
Filter filter[FILTER];
int nQuery;
int query[QUERY];
void read() {
cin >> nFilter;
for (int i = 0; i < nFilter; ++i) {
cin >> filter[i].val >> filter[i].type;
}
cin >> nQuery;
for (int i = 0; i < nQuery; ++i) {
cin >> query[i];
}
}
void work() {
long long maxMaxV = -INF;
long long minMinV = INF;
long long sum = 0;
for (int i = 0; i < nFilter; ++i) {
Filter &f = filter[i];
if (f.type == 1) {
maxMaxV += f.val;
minMinV += f.val;
sum += f.val;
} else if (f.type == 2) {
maxMaxV = max(maxMaxV, 1LL * f.val);
minMinV = max(minMinV, 1LL * f.val);
} else if (f.type == 3) {
minMinV = min(minMinV, 1LL * f.val);
maxMaxV = min(maxMaxV, 1LL * f.val);
}
}
for (int i = 0; i < nQuery; ++i) {
cout << max(maxMaxV, min(minMinV, 1LL * query[i] + sum)) << endl;
}
}
int main() {
read();
work();
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
const int p = 31;
const int m = 998244353;
typedef long long ll;
typedef long double d;
typedef unsigned long long ull;
typedef int in;
#define ar array
#define ff first
#define ss second
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
bitset<200005>isp;
vector<int>prm;
ll dp[200005];
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n;
cin >> n;
dp[1] = 1;
for (int i = 2; i <= n; ++i) {
dp[i]+=dp[i-1];
ll tmp = i;
ll ans = 1ll;
for (int x=2;x*x<=tmp;++x) {
if (tmp <= 1)break;
int ctr = 0;
while (tmp % x == 0) {
tmp /= x;
ctr++;
}
if (ctr) {
ans *= (((ctr + 2) * (ctr + 1) / 2));
}
}
if(tmp!=1){
ans*=(3);
}
dp[i]+=ans;
}
cout<<dp[n];
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"; //check time
return 0;
}
| #ifdef xay5421
#define D(...) fprintf(stderr,__VA_ARGS__)
#else
#define D(...) ((void)0)
//#define NDEBUG
#endif
#include<bits/stdc++.h>
#define int long long
#define LL long long
#define MP make_pair
#define PB push_back
#define X first
#define Y second
#define SZ(x) ((int)(x).size())
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
typedef pair<int,int>PII;typedef vector<int>VI;typedef vector<PII>VII;
template<typename T>void rd(T&x){int f=0,c;while(!isdigit(c=getchar()))f^=!(c^45);x=(c&15);while(isdigit(c=getchar()))x=x*10+(c&15);if(f)x=-x;}
template<typename T>void pt(T x,int c=-1){if(x<0)putchar('-'),x=-x;if(x>9)pt(x/10);putchar(x%10+48);if(c!=-1)putchar(c);}
const int N=200005;
int n,m,fa[N],a[N],b[N],sa[N],sb[N];
int fd(int k1){return fa[k1]==k1?k1:fa[k1]=fd(fa[k1]);}
signed main(){
rd(n),rd(m);
rep(i,1,n)rd(a[i]);
rep(i,1,n)rd(b[i]),fa[i]=i;
rep(i,1,n)fa[i]=i;
rep(i,1,m){
int k1,k2;
rd(k1),rd(k2);
fa[fd(k1)]=fd(k2);
}
rep(i,1,n){
sa[fd(i)]+=a[i];
sb[fd(i)]+=b[i];
}
rep(i,1,n)if(sa[i]!=sb[i]){
puts("No");
exit(0);
}
puts("Yes");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define M 100000000000000000
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define PI 3.14159265
#define pb push_back
#define LLMX LONG_LONG_MAX
#define LLMN LONG_LONG_MIN
#define double long double
int n , m , k;
signed main()
{
//fastio ;
int a , b , c ;
cin >> a >> b >> c ;
if( c == 0)
{
if( a > b)
{
cout << "Takahashi" ;
}
else
cout <<"Aoki" ;
}
else
{
if( b > a)
{
cout << "Aoki" ;
}else
cout << "Takahashi" ;
}
} | #include<iostream>
using namespace std;
long A,B,C;
char f(long A,long B,long C)
{
return A>B?'>':A<B?'<':'=';
}
main()
{
cin>>A>>B>>C;
if(A==0)
{
if(B==0)
{
cout<<"="<<endl;
}
else if(B>0)
{
cout<<"<"<<endl;
}
else
{
if(C%2==0)cout<<"<"<<endl;
else cout<<">"<<endl;
}
}
else if(A>0)
{
if(B==0)
{
cout<<">"<<endl;
}
else if(B>0)
{
cout<<f(A,B,C)<<endl;
}
else
{
if(C%2==1)cout<<">"<<endl;
else cout<<f(A,-B,C)<<endl;
}
}
else
{
if(B==0)
{
if(C%2==0)cout<<">"<<endl;
else cout<<"<"<<endl;
}
else if(B>0)
{
if(C%2==1)cout<<"<"<<endl;
else cout<<f(-A,B,C)<<endl;
}
else
{
if(C%2==0)cout<<f(-A,-B,C)<<endl;
else cout<<f(-B,-A,C)<<endl;
}
}
}
|
#include<bits/stdc++.h>
using namespace std;
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/gcd(a,b)
#define ff first
#define ss second
#define int long long
#define float double
#define pb emplace_back
#define mp make_pair
#define pii pair<int,int>
#define vi vector<int>
#define vc vector<char>
#define vb vector<bool>
#define vf vector<float>
#define vs vector<string>
#define mii map<int,int>
#define pqb priority_queue<int>
#define pqs priority_queue<int,vi,greater<int>>
#define hashmap unordered_map
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf INT64_MAX
#define ps(x,y) fixed<<setprecision(y)<<x
#define mk(arr,n,type) type *arr=new type[n];
#define w(x) int x; cin>>x; while(x--)
#define all(x) x.begin(),x.end()
#define digits(x) floor(log10(x))+1
#define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
template<class T>
void printV(vector<T> a) {for (auto v : a)cout << v << " ";}
template<class T>
void readV(vector<T>& a) {int n = a.size(); for (int i = 0 ; i < n ; i++)cin >> a[i];}
template<class T>
void printA(T* a , int n) {for (int i = 0; i < n ; i++)cout << a[i] << " ";}
template<class T>
void readA(T* a , int n) {for (int i = 0 ; i < n ; i++)cin >> a[i];}
auto clk = clock();
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
void run_time() {cout << endl<<"Time elapsed: " << (double)(clock() - clk) / CLOCKS_PER_SEC << endl;}
void aryan_pandeya() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
int32_t main() {
aryan_pandeya();
FIO;
int n ;
cin>>n;
vector<int> a(n),b(n);
readV(a);
readV(b);
int ans = 0;
for(int i =0 ; i < n ; i++){
ans+=(a[i]*b[i]);
}
ans==0?cout<<"Yes":cout<<"No";
return 0;
}
| //praise the lord
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma region
#define int long long
#define ll long long
#define ld long double
#define mod 1000000007
#define mod2 998244353
#define ff first
#define ss second
#define pb push_back
#define endl "\n"
#define vi vector<int>
#define si set<int>
#define pii pair<int,int>
#define mii map<int,int>
#define vii vector<pii>
#define sii set<pii>
#define vvi vector<vi>
#define vvii vector<vii>
#define vsi vector<si>
#define in(arr,n) for(int mm=0;mm<n;mm++)cin >>arr[mm];
#define out(arr,n) for(int mm=0;mm<n;mm++)cout<<arr[mm]<<" ";cout<<endl;
#define inn(arr,m,n) for(int mm=m;mm<n;mm++)cin >>arr[mm];
#define outt(arr,m,n) for(int mm=m;mm<n;mm++)cout<<arr[mm]<<" ";cout<<endl;
#define narr int arr[n];in(arr,n)
#define nbrr int brr[n];in(brr,n)
#define cin1(a) int a;cin>>a;
#define cin2(a,b) int a,b;cin>>a>>b;
#define cin3(a,b,c) int a,b,c;cin>>a>>b>>c;
#define cin4(a,b,c,d) int a,b,c,d;cin>>a>>b>>c>>d;
#define fo(i,a,b) for(int i=a;i<b;i++)
#define rfo(i,a,b) for(int i=a;i>b;i--)
#define zzz 100000000
#define inf 1000000000000000000
#define max4 10007
#define max5 100007
#define max52 200007
#define max6 1000007
using namespace std;
using namespace __gnu_pbds;
template<class T> using oset =tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update> ;
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
#define umii unordered_map<int,int,custom_hash>
void solve1();
//bool allprime[10000100]={0};
/*vector<int> set_of_primes;
void sieve_of_primes_upto_10e7()
{
int n=10000100;
set_of_primes.push_back(2);int p;
for(p=3;p*p<=n;p+=2)
{
if(allprime[p]==0)
{
set_of_primes.push_back(p);
for(int i=p*p;i<=n;i+=p)
{
allprime[i]=1;
}
}
}
for(p;p<=n;p+=2)
{
if(allprime[p]==0)
{
set_of_primes.push_back(p);
}
}
}*/
int power(int x, int y, int p){//if(x==0&&y==0)return 1;
int res = 1;x = x % p;if (x == 0) return 0;
while (y > 0){if (y & 1)res = (res*x) % p;y = y>>1;x = (x*x) % p;}return res%p;
}
int power(int x, int y){//if(x==0&&y==0)return 1;
int res = 1;if (x == 0) return 0;
while (y > 0){if (y & 1)res = (res*x);y = y>>1;x = (x*x);}return res;
}
#pragma endregion
int sum(int arr[],int a,int n){int zz=0;for(int i=a;i<n;i++){zz+=arr[i];}return zz;}
int sum(vi arr ,int a,int n){int zz=0;for(int i=a;i<n;i++){zz+=arr[i];}return zz;}
int sum(int arr[],int n) {int zz=0;for(int i=0;i<n;i++){zz+=arr[i];}return zz;}
int sum(vi arr ,int n) {int zz=0;for(int i=0;i<n;i++){zz+=arr[i];}return zz;}
void solve(){int t;cin>>t;while(t--){solve1();}}
signed main(){
std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
// sieve_of_primes_upto_10e7();
// vec = set_of_primes;
solve1();
return 0;
}
void solve1()
{
cin2(x,y);
if(x==y)cout<<x<<endl;
else cout<<(3-x-y)<<endl;
}
|
#include<cstdio>
#include<cctype>
#include<cstring>
#include<cmath>
#include<algorithm>
#define LL long long
using namespace std;
template <class I>
inline void read(I &z)
{
z=0;
char c=getchar();int base=1;
while (!isdigit(c) && c!='-') c=getchar();
if (c=='-') c=getchar(),base=-1;
while (isdigit(c)) z=z*10+c-'0',c=getchar();
z*=base;
}
struct E
{
int to,next;
};
const int N=200010;
E e[N<<1];
int v[N],point=1;
inline void add(int x,int y)
{
e[++point]=(E){y,v[x]},v[x]=point;
e[++point]=(E){x,v[y]},v[y]=point;
}
int num=0,x;
int l[N],r[N],d,m;
int s[N];
int f[N<<1];
void dfs(int cur,int fa,int h)
{
l[cur]=++num;
f[num]=cur;
s[num]=h;
for (int i=v[cur];i;i=e[i].next)
{
int u=e[i].to;
if (u==fa) continue;
dfs(u,cur,h+1);
}
r[cur]=num;
}
struct Persistent_Segment_Tree
{
struct node
{
int lc,rc,sum;
};
node a[N*70];
int root[N],point;
Persistent_Segment_Tree() { root[0]=point=1; }
int newnode(int cur)
{
a[++point]=a[cur];
return point;
}
void pushup(int cur)
{
a[cur].sum=a[a[cur].lc].sum+a[a[cur].rc].sum;
}
void build(int cur,int l,int r)
{
if (l<r)
{
int mid=l+r>>1;
a[cur].lc=++point,a[cur].rc=++point;
build(a[cur].lc,l,mid),build(a[cur].rc,mid+1,r);
}
}
int inquire(int his,int cur,int l,int r,int k)
{
if (l==r) return a[cur].sum-a[his].sum;
int mid=l+r>>1,ans=0,sum=a[a[cur].lc].sum-a[a[his].lc].sum;
if (k<=mid) return inquire(a[his].lc,a[cur].lc,l,mid,k);
else return inquire(a[his].rc,a[cur].rc,mid+1,r,k);
}
void update(int cur,int l,int r,int pos)
{
if (l==r)
{
++a[cur].sum;
return;
}
int mid=l+r>>1;
if (pos<=mid) update(a[cur].lc=newnode(a[cur].lc),l,mid,pos);
if (pos>mid) update(a[cur].rc=newnode(a[cur].rc),mid+1,r,pos);
pushup(cur);
}
};
Persistent_Segment_Tree T;
int n;
int main(int argc, char const *argv[])
{
read(n);
for (int i=2;i<=n;++i)
{
read(x);
add(x,i);
}
dfs(1,0,1);
T.build(T.root[0],1,n);
for (int i=1;i<=num;++i) T.root[i]=T.newnode(T.root[i-1]),T.update(T.root[i],1,n,s[i]);
read(m);
for (int i=1;i<=m;++i)
{
read(x),read(d);
printf("%d\n",T.inquire(T.root[l[x]-1],T.root[r[x]],1,n,d+1));
}
return 0;
}
| #include<bits/stdc++.h>
#define st first
#define nd second
#define pb(x) push_back(x)
#define pp(x) pop_back(x)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define rev(x) reverse(all(x))
#define sor(x) sort(all(x))
#define sz(x) (int)(x).size()
#define rsz(x) resize(x)
using namespace std;
///~~~~~~~~~~~~~~~~~~~~~~~~~~
void debug(){cerr<<"\n";}
template <typename H, typename... T>
void debug(H h, T... t) {cerr<<h; if (sizeof...(t)) cerr << ", "; debug(t...);}
#define deb(x...) cerr<<#x<<" = ";debug(x);
///~~~~~~~~~~~~~~~~~~~~~~~~~
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii > vii;
typedef vector<ll> vl;
typedef vector<pll> vll;
typedef string str;
#define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const int N=2e5+5, INF=1e9+5, mod=1e9+7;
int r[N];
int find(int v){
if(v==r[v])return v;
return r[v]=find(r[v]);
}
void Union(int a ,int b){
r[find(a)]=find(b);
}
int main(){
BOOST;
for(int i=0; i<N; i++)r[i]=i;
int n, m;
cin>>n>>m;
for(int i=0; i<n; i++){
str s;
cin>>s;
for(int j=0; j<m; j++){
if(s[j]=='#')Union(i, j+n);
}
}
Union(0, n);
Union(n-1, n);
Union(0, n+m-1);
set<int> S1, S2;
for(int i=0; i<n; i++)S1.insert(find(i));
for(int i=n; i<n+m; i++)S2.insert(find(i));
cout<<min(sz(S1), sz(S2))-1;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
static const int MAX_N = 16;
static const int MAX_M = 16;
static const int INF = 10001;
int d[MAX_M][MAX_N];
int h, w, a, b;
ll bfs(vector<vector<int>> input, int a, int b, int cnt, int i, int j){
ll ans = 0;
if (a == 0 && b == 0) {
if (cnt == h*w) {
/*for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cout << input.at(i).at(j) << " ";
}
cout << endl;
}*/
return 1;
}
else return 0;
}
//for (int i = x; i < h; i++) {
//for (int j = 0; j < w; j++) {
//if (i == x && j <= y) continue;
while (i < h && j < w) {
if (a > 0) {
if (input.at(i).at(j) == 0 && j < w - 1 && input.at(i).at(j+1) == 0) {
vector<vector<int>> tmp = input;
tmp.at(i).at(j) = 1;
tmp.at(i).at(j+1) = 1;
ans += bfs(tmp, a-1, b, cnt+2, i, j+1);
//cout << a << endl;
}
if (input.at(i).at(j) == 0 && i < h - 1 && input.at(i+1).at(j) == 0) {
vector<vector<int>> tmp = input;
tmp.at(i).at(j) = 1;
tmp.at(i+1).at(j) = 1;
ans += bfs(tmp, a-1, b, cnt+2, i, j);
}
}
if (b > 0) {
if (input.at(i).at(j) == 0) {
vector<vector<int>> tmp = input;
tmp.at(i).at(j) = 2;
ans += bfs(tmp, a, b-1, cnt+1, i, j);
}
}
j++;
if (j >= w) {
j = 0;
i++;
}
}
//}
//}
return ans;
}
int main() {
cin >> h >> w >> a >> b;
vector<vector<int>> input(h, vector<int>(w, 0));
ll ans = bfs(input, a, b, 0, 0 ,0);
cout << ans << endl;
return 0;
}
| #include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mkp make_pair
#define vi vector<int>
#define pii pair<int,int>
#define FI(n) FastIO::read(n)
#define FO(n) FastIO::write(n)
#define ull unsigned long long
#define mst(a,b) memset(a,b,sizeof(a))
#define foR(i,k,j) for(int i=(k);i>=(j);i--)
#define For(i,k,j) for(int i=(k);i<=(j);i++)
#define Foe(i,u) for(int i=lst[u],v=e[i].v;i;i=e[i].nxt,v=e[i].v)
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define Fin(s) freopen(s,"r",stdin)
#define Fout(s) freopen(s,"w",stdout)
#define file(s) Fin(s".in"),Fout(s".out")
#define INF ((1ll<<60)-1)
#define int long long
const int P=998244353; //
using namespace std;
template<typename T>inline void ckmax(T &a,T b) {(a<b)&&(a=b);}
template<typename T>inline void ckmin(T &a,T b) {(a>b)&&(a=b);}
inline int mul(int a,int b) {return 1ull*a*b%P;}
inline int add(int a,int b) {return a+b>=P?a+b-P:a+b;}
inline int sub(int a,int b) {return a-b>=0?a-b:a-b+P;}
inline void mulmod(int &a,int b) {a=mul(a, b);}
inline void addmod(int &a,int b) {((a+=b)>=P)&&(a-=P);}
inline void submod(int &a,int b) {((a-=b)<0)&&(a+=P);}
inline int ksm(int a,int b) {int ans=1; for(;b;b>>=1) {if(b&1) ans=1ll*ans*a%P;a=1ll*a*a%P;}return ans;}
inline void fprint(const int &x,char c=' ') {fprintf(stderr,"%d%c",x,c);}
inline void fprint(const pii &x,char c='\n') {fprintf(stderr,"%d %d%c",x.first,x.second,c);}
inline void fprint(const int *f,const int &n,char c='\n') {for(int i=1;i<=n;i++) fprint(f[i]); fprintf(stderr,"%c",c);}
inline void fprint(const vector<int> &f,char c='\n') {for(int i=0;i<(int)f.size();i++) fprint(f[i]); fprintf(stderr,"%c",c);}
inline int inv(int a) {return ksm(a,P-2);}
namespace FastIO {
const int SIZE=1<<16; char buf[SIZE],obuf[SIZE],str[64]; int bi=SIZE,bn=SIZE,opt;
int read(char *s) {
while (bn) {for (;bi<bn&&buf[bi]<=' ';bi++);if (bi<bn) break; bn=fread(buf,1,SIZE,stdin),bi=0;}
int sn=0;while (bn) {for (;bi<bn&&buf[bi]>' ';bi++) s[sn++]=buf[bi];if (bi<bn) break; bn=fread(buf,1,SIZE,stdin),bi=0;}s[sn]=0;return sn;
}
bool read(int& x) {if(x)x=0;int bf=0,n=read(str); if(!n) return 0; int i=0; if (str[i]=='-') bf=1,i=1; for(x=0;i<n;i++) x=x*10+str[i]-'0'; if(bf) x=-x; return 1;}
void write(int x) {
if(!x) obuf[opt++]='0'; else {if(x<0) obuf[opt++]='-',x=-x;int sn=0; while(x)str[sn++]=x%10+'0',x/=10;for (int i=sn-1;i>=0;i--) obuf[opt++]=str[i];}
if (opt>=(SIZE>>1)){fwrite(obuf,1,opt,stdout); opt=0;}
}
void write(char x) {obuf[opt++]=x;if (opt>=(SIZE>>1)){fwrite(obuf,1,opt,stdout); opt=0;}}
void Fflush() {if (opt) fwrite(obuf,1,opt,stdout); opt=0;}
};
inline int read() {int x; FI(x); return x;}
const int MN=3e5+5;
int cnt[25],n,m; char s[25];
signed main() {
#ifndef ONLINE_JUDGE
freopen("pro.in","r",stdin);
freopen("pro.out","w",stdout);
#endif
n=read(),m=read();
For(i,1,n) {
FI(s+1); int nowcnt=0;
// cerr<<(s+1)<<endl;
For(j,1,m) nowcnt+=s[j]-'0';
// cerr<<nowcnt<<endl;
cnt[nowcnt]++;
}
int ans=0;
For(i,0,m) {
For(j,i,m) {
if((i+j)&1) {
ans+=cnt[i]*cnt[j];
}
}
}
cout<<ans<<endl;
return FastIO::Fflush(),0;
}
/*
*/
|
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
string s;
cin >> s;
int a[10]={0};
for(int i=0; i<s.size(); ++i)
{
a[s[i]-'0']++;
}
if(s.size()==1)
{
if(s=="8")
{
cout << "Yes" << endl;
return 0;
}
}
else if(s.size()==2)
{
if(((s[0]-'0')*10+(s[1]-'0'))%8==0 || ((s[1]-'0')*10+(s[0]-'0'))%8==0)
{
cout << "Yes" << endl;
return 0;
}
}
for(int i=112; i<1000; i+=8)
{
int d1=i/100;
int d2=i/10%10;
int d3=i%10;
int t[10];
for(int i=0; i<10; ++i)
{
t[i]=a[i];
}
if(t[d1]>0) t[d1]--;
else continue;
if(t[d2]>0) t[d2]--;
else continue;
if(t[d3]<=0) continue;
cout << "Yes" << endl;
return 0;
}
cout << "No" << endl;
return 0;
} | #include "bits/stdc++.h"
#include "random"
#include <unistd.h>
using namespace std;
#define ll long long
#define vi vector<int>
#define vl vector<long long>
#define vvi vector<vi>
#define pi pair<int,int>
#define mp make_pair
#define pb push_back
#define MOD int(1e9) + 7
#define PAI 3.1415926535
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define chmax(x, y) x = max(x,y)
#define chmin(x, y) x = min(x,y)
#define pr(x) cout << x << endl
#define Endl endl
#define rep(i, n) for(int i = 0 ; i < n; ++i)
const int dx[4] = {1,0,-1,0};
const int dy[4] = {0,1,0,-1};
const int ddx[8] = {-1,0,1,-1,1,-1,0,1};
const int ddy[8] = {-1,-1,-1,0,0,1,1,1};
const int inf = 99999999;
const ll linf = 1LL << 62;
ll gcd(ll a,ll b){
if(a < b)swap(a , b);
if(a % b != 0) return(gcd(b, a%b));
return b;
}
ll lcm(ll a,ll b){
if(a < b)swap(a , b);
return (a / gcd(a , b)) * b;
}
int Uniform_Random(int a, int b){ // (a <= x <= b)
random_device rd;
mt19937 mt(rd());
uniform_int_distribution<int> rv1(a, b);
return rv1(mt);
}
bool check(int n){
while(n){
if(n % 10 == 7)
return true;
n /= 10;
}
return false;
}
bool octal(int n){
while(n){
if (n % 8 == 7)
return true;
n /= 8;
}
return false;
}
int main(){
int N; cin >> N;
set<int> st;
for(int i = 1; i <= N; ++i){
if(check(i)){
st.insert(i);
// cout << i << endl;
}
else if(octal(i)){
st.insert(i);
// cout << i << endl;
}
}
cout << N - st.size() << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int long long
#define fs first
#define sc second
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define MP make_pair
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifdef FlameDragon
freopen("in.txt", "r", stdin);
#endif
int n;
cin >> n;
int k = 5;
vector<vector<int>> a(n, vector<int>(k));
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
cin >> a[i][j];
}
}
const int INF = 1e9 + 123;
int l = 0, r = INF;
auto check = [&](int x) -> bool {
vector<int> cnt(1 << k);
for (int i = 0; i < n; i++) {
int mask = 0;
for (int j = 0; j < k; j++) {
if (a[i][j] >= x) {
mask ^= 1 << j;
}
}
cnt[mask] = 1;
}
for (int i = 0; i < (1 << k); i++) {
for (int j = 0; j < (1 << k); j++) {
for (int p = 0; p < (1 << k); p++) {
if (cnt[i] && cnt[j] && cnt[p] && (i | j | p) == ((1 << k) - 1)) {
return true;
}
}
}
}
return false;
};
while (r - l > 1) {
int mid = (l + r) / 2;
if (check(mid)) {
l = mid;
} else {
r = mid;
}
}
cout << l << '\n';
}
| #include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define deb(...)
#endif
#define endl "\n"
#define pb push_back
#define int long long
#define ll long long
#define pii pair<int,int>
#define F first
#define S second
#define all(c) c.begin(),c.end()
#define read(v) for(auto &it:v) cin>>it;
const int inf = 1e18;
const int N = 2e5 + 5;
const int mod = 1000000007;
int madd(int a, int b)
{
return (a + b) % mod;
}
int msub(int a, int b)
{
return (((a - b) % mod) + mod) % mod;
}
int mmul(int a, int b)
{
return ((a % mod) * (b % mod)) % mod;
}
int mpow(int base, int exp)
{
int res = 1;
while (exp)
{
if (exp % 2 == 1)
{
res = (res * base) % mod;
}
exp >>= 1;
base = (base * base) % mod;
}
return res;
}
int minv(int base)
{
return mpow(base, mod - 2);
}
int mdiv(int a, int b)
{
return mmul(a, minv(b));
}
int dp[N][2];
int n;
int a[N];
void test_case() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
dp[0][0] = 1;
dp[0][1] = 0;
dp[1][0] = 1;
dp[1][1] = 1;
for (int i = 2; i <= n; i++) {
dp[i][1] = madd(dp[i - 1][0], dp[i - 1][1]);
dp[i][0] = dp[i - 1][1];
}
int ans = madd(dp[n - 1][0], dp[n - 1][1]);
ans = mmul(ans, a[1]);
int good = madd(dp[n - 1][0], dp[n - 1][1]);
// deb(ans)
for (int i = 2; i <= n; i++) {
int left = madd(dp[i - 2][0], dp[i - 2][1]);
if (i - 2 == 0) {
left = 1;
}
int right = madd(dp[n - i][0], dp[n - i][1]);
if (n - i == 0) {
right = 1;
}
// deb(left, right)
ans = madd(ans, mmul(a[i], mmul(left, right)));
ans = msub(ans, mmul(a[i], msub(good, mmul(left, right))));
}
cout << ans << endl;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t = 1;
// cin >> t;
while (t--)test_case();
return 0;
} |
//#include "bits/stdc++.h"
#define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <functional>
#include <utility>
#include <tuple>
#include <vector>
#include <string>
#include <list>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <queue>
#include <deque>
#include <stack>
#include <iterator>
#include <bitset>
#include <complex>
#include <limits>
#include <random>
#include<fstream>
#include<array>
#include<assert.h>
using namespace std;
#define rep(i,a,b) for(int i=(a), i##_len=(b);i<i##_len;i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
#define int ll
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<double, double> pdd;
typedef vector< vector<int> > mat;
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
signed main()
{
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
int cnt[] = { 0,0,0 };
int tk = N;
int sum = 0;
int d = 0;
while (tk > 0)
{
d++;
cnt[(tk % 10) % 3]++;
sum += tk % 10;
tk /= 10;
}
sum %= 3;
if (sum == 0)cout << 0 << endl;
else if (sum == 1)
{
if (d>1&&cnt[1] > 0)cout << 1 << endl;
else if (d>2&&cnt[2] > 1)cout << 2 << endl;
else cout << -1 << endl;
}
else
{
if (d > 1 && cnt[2] > 0)cout << 1 << endl;
else if (d > 2 && cnt[1] > 1)cout << 2 << endl;
else cout << -1 << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e5 + 9;
const int mod = 1e9 + 7;
int n, res = -1;
char a[20];
void solve(int idx, int sum, int cnt) {
if (idx == n) {
if (sum && sum%3 == 0)
res = max(res, cnt);
return;
}
solve(idx + 1, sum + (a[idx] - '0'), cnt + 1);
solve(idx + 1, sum, cnt);
}
int main() {
scanf("%s",a);
n = strlen(a);
solve(0, 0, 0);
printf("%d", (res == -1 ? res : n - res));
return 0;
}
|
#include <bits/stdc++.h>
#define int long long
#define double long double
using namespace std;
const double ESP = 1e-11;
double X, Y, R;
double dist(double u1, double v1, double u2, double v2) {
return (u1 - u2) * (u1 - u2) + (v1 - v2) * (v1 - v2);
}
int bsl(int x) {
int rs = ceil(Y), l = -1e9, r = floor(Y);
while (l <= r) {
int mid = (l + r) / 2;
if (dist(x, mid, X, Y) <= R * R || abs(dist(x, mid, X, Y) - R * R) < ESP) {
rs = mid;
r = mid - 1;
} else
l = mid + 1;
}
return rs;
}
int bsr(int x) {
int rs = floor(Y), l = ceil(Y), r = 1e9;
while (l <= r) {
int mid = (l + r) / 2;
if (dist(x, mid, X, Y) <= R * R || abs(dist(x, mid, X, Y) - R * R) < ESP) {
rs = mid;
l = mid + 1;
} else
r = mid - 1;
}
return rs;
}
int32_t main() {
#ifdef ACM
freopen("input", "r", stdin);
#endif
cin >> X >> Y >> R;
int rs = 0, l = ceil(X - R), r = floor(X + R);
for (int x = l; x <= r; x++) {
int ll = bsl(x), rr = bsr(x);
rs += rr - ll + 1;
}
cout << rs;
} | #include "bits/stdc++.h"
using namespace std;
#define REP(i, n) for(ll i = 0;i < n;i++)
#define ll long long
#define MOD 1000000007LL
//#define MOD 998244353LL
#define doublecout(a) cout<<setprecision(16)<<a<<endl;
using vi = vector<ll>; // intの1次元の型に vi という別名をつける
using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける
using vvvi = vector<vvi>; // intの2次元の型に vvi という別名をつける
//123.456や-78を10000倍して整数に
ll xch(string ins){
/////////////「.」があるかないかチェック
int flag;
flag=0;
for(ll i=0;i<ins.size();i++){
if (ins[i]=='.'){
flag=1;
break;
}
}
if (flag==0){
ins+=".";
}
/////////////「.」があるかないかチェックここまで
ll ret=0;
ll nm=0;
for(ll i=0;i<ins.size();i++){
if (nm!=0)nm--;
if (ins[i]=='.') nm=4;/////////////////////////ここが0埋めの数になる
}
for(ll i=0;i<nm;i++){
ins+="0";
}
for(ll i=0;i<ins.size();i++){//ここで文字→数値に変換
if (ins[i]=='-')continue;
if (ins[i]=='.')continue;
ret*=10;
ret+=ins[i]-48;
}
if (ins[0]=='-')ret=-ret;
return ret;
}
//整数の割り算、割ったあまりが正の整数になるやつ
//bbbは正の数
constexpr ll floordiv(ll aaa,ll bbb)
{
if (aaa<0){
return (aaa-bbb+1)/bbb;
}else{
return aaa/bbb;
}
}
int main(){
ll ans=0;
string ss0,ss1,ss2;
cin >> ss0>>ss1>>ss2;
ll x=xch(ss0);
ll y=xch(ss1);
ll r=xch(ss2);
ll topn=y+r;
ll rr=r*r;
topn-=topn%10000;
for(ll i=topn;i>=y-r;i-=10000){
ll yy=abs(i-y);
ll xx2=rr-yy*yy;
if (xx2<0)continue;
double dx=sqrt(xx2);
ll ldx=dx;
if (ldx*ldx>xx2)ldx--;
ans+=floordiv(x+ldx,10000)-floordiv(x-ldx+9999,10000)+1;
}
cout<<ans<<endl;
return 0;
} |
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,avx512f")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <stack>
#include <queue>
#include <bitset>
#include <numeric>
#include <cassert>
#include <memory>
#include <random>
#include <functional>
#include <complex>
#include <immintrin.h>
#ifdef DEBUG
#include "./Lib/debug_VC.hpp"
#include "./Lib/Timer.hpp"
#include "./Lib/sample.hpp"
#else
#define dump(...)
#endif
/* macro */
#define FOR(i, b, e) for(ll i = (ll)(b); i < (ll)(e); ++i)
#define RFOR(i, b, e) for(ll i = (ll)(e-1); i >= (ll)(b); --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define REPC(x,c) for(const auto& x:(c))
#define REPI2(it,b,e) for(auto it = (b); it != (e); ++it)
#define REPI(it,c) REPI2(it, (c).begin(), (c).end())
#define RREPI(it,c) REPI2(it, (c).rbegin(), (c).rend())
#define REPI_ERACE2(it, b, e) for(auto it = (b); it != (e);)
#define REPI_ERACE(it, c) REPI_ERACE2(it, (c).begin(), (c).end())
#define ALL(x) (x).begin(),(x).end()
#define cauto const auto&
/* macro func */
template<class T>
inline auto sort(T& t) { std::sort(ALL(t)); }
template<class T>
inline auto rsort(T& t) { std::sort((t).rbegin(), (t).rend()); }
template<class T>
inline auto unique(T& t) { (t).erase(unique((t).begin(), (t).end()), (t).end()); }
template<class T, class S>
inline auto chmax(T& t, const S& s) { if (s > t) { t = s; return true; } return false; }
template<class T, class S>
inline auto chmin(T& t, const S& s) { if (s < t) { t = s; return true; } return false; }
inline auto BR() { std::cout << "\n"; }
/* type define */
using ll = long long;
using PAIR = std::pair<ll, ll>;
using VS = std::vector<std::string>;
using VL = std::vector<long long>;
using VVL = std::vector<VL>;
using VVVL = std::vector<VVL>;
using VD = std::vector<double>;
template<class T>
using V = std::vector<T>;
/* using std */
using std::cout;
constexpr char endl = '\n';
using std::cin;
using std::pair;
using std::string;
using std::stack;
using std::queue;
using std::vector;
using std::list;
using std::map;
using std::unordered_map;
using std::multimap;
using std::unordered_multimap;
using std::set;
using std::unordered_set;
using std::unordered_multiset;
using std::multiset;
using std::bitset;
using std::priority_queue;
/* Initial processing */
struct Preprocessing { Preprocessing() { std::cin.tie(0); std::ios::sync_with_stdio(0); }; }_Preprocessing;
/* Remove the source of the bug */
signed pow(signed, signed) { assert(false); return -1; }
/* define hash */
namespace std {
template <> class hash<std::pair<ll, ll>> { public: size_t operator()(const std::pair<ll, ll>& x) const { return hash<ll>()(1000000000 * x.first + x.second); } };
}
/* input */
template<class T> std::istream& operator >> (std::istream& is, vector<T>& vec) { for (T& x : vec) is >> x; return is; }
/* constant value */
constexpr ll MOD = 1000000007;
//constexpr ll MOD = 998244353;
//=============================================================================================
signed main() {
ll n;
cin >> n;
VL a(n), b(n);
cin >> a >> b;
multiset<ll, std::greater<ll>> odd, even;
REP(i, n) {
((i & 1) ? odd : even).emplace(b[i] - a[i]);
}
ll ans = 0;
REPC(x, a) { ans += x; }
dump(ans, odd, even);
while (!odd.empty() && *odd.begin() + *even.begin() > 0) {
ans += *odd.begin() + *even.begin();
odd.erase(odd.begin());
even.erase(even.begin());
}
cout << ans << endl;
}
| #include <stdio.h>
int main()
{
int i, N, A[2][100001];
scanf("%d", &N);
for (i = 1; i <= N; i++) scanf("%d", &(A[0][i]));
for (i = 1; i <= N; i++) scanf("%d", &(A[1][i]));
int j, k, l, r;
const long long inf = -(1LL << 60);
long long dp[2][100001];
for (i = 0; i <= N; i++) {
dp[0][i] = inf;
dp[1][i] = inf;
}
dp[0][1] = A[0][1];
dp[1][1] = A[1][1];
for (i = 2; i <= N; i++) {
if (i % 2 == 0) {
dp[0][0] = dp[0][1] + A[0][i];
if (dp[0][0] < dp[1][1] + A[1][i]) dp[0][0] = dp[1][1] + A[1][i];
} else dp[1][0] = dp[0][0];
k = (i < N - i)? i: N - i;
l = i % 2;
r = 1 - i % 2;
for (j = 2 - l; j <= k; j += 2) {
dp[0][j] = dp[0][j-1] + A[r][i];
if (dp[0][j] < dp[0][j+1] + A[l][i]) dp[0][j] = dp[0][j+1] + A[l][i];
dp[1][j] = dp[1][j-1] + A[l][i];
if (dp[1][j] < dp[1][j+1] + A[r][i]) dp[1][j] = dp[1][j+1] + A[r][i];
}
}
printf("%lld\n", dp[0][0]);
fflush(stdout);
return 0;
} |
#include <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;
int main()
{
int N,W;
cin >> N >> W;
cout << N/W << endl;
return 0;
} | #include<iostream>
using namespace std;
int main(){
int32_t X{},Y{};
cin >> X >> Y;
if(X-Y<3 && Y-X <3){
cout << "Yes" << endl;
}else{
cout << "No" << endl;
}
return 0;
} |
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
const ll MOD = 998244353;
const double PI = 3.141592653589793238;
int main() {
ll N;
cin >> N;
vector<ll> A(N), sum(N, 0), sumsum(N, 0), maxi(N, 0);
for (ll i = 0; i < N; ++i) {
cin >> A[i];
sum[i] = A[i];
if (i != 0) sum[i] += sum[i - 1];
sumsum[i] = sum[i];
if (i != 0) sumsum[i] += sumsum[i - 1];
if (i != 0) maxi[i] = maxi[i - 1];
maxi[i] = max(maxi[i], A[i]);
}
for (ll k = 0; k < N; ++k) {
cout << (k + 1) * maxi[k] + sumsum[k] << "\n";
}
} | #include <iostream>
long sum(const long &a, const long &b) {
return (a + b) * (b - a + 1) / 2;
}
int main() {
int t;
std::cin >> t;
while(t--) {
long l, r;
std::cin >> l >> r;
long minc = l, maxc = r - l;
if (minc > maxc) std::cout << 0 << '\n'; //if minimum of c > r - l, there is no such pair
else std::cout << sum(r - l + 1 - maxc, r - l + 1 - minc) << '\n';
}
}
|
#include<bits/stdc++.h>
using namespace std;
namespace Sakurajima_Mai{
#define ms(a) memset(a,0,sizeof(a))
#define repi(i,a,b) for(int i=a,bbb=b;i<=bbb;++i)//attention reg int or reg ll ?
#define repd(i,a,b) for(int i=a,bbb=b;i>=bbb;--i)
#define reps(s) for(int i=head[s],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
#define ce(i,r) i==r?'\n':' '
#define pb push_back
#define all(x) x.begin(),x.end()
#define gmn(a,b) a=min(a,b)
#define gmx(a,b) a=max(a,b)
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
const int infi=2e9;//infi??,????inf????int
const ll infl=4e18;
inline ll ceil_div(ll a,ll b){ return (a+b-1)/b; }
inline ll pos_mod(ll a,ll b){ return (a%b+b)%b; }
//std::mt19937 rnd(time(0));//std::mt19937_64 rnd(time(0));
}
using namespace Sakurajima_Mai;
namespace Fast_Read{
inline int qi(){
int f=0,fu=1; char c=getchar();
while(c<'0'||c>'9'){ if(c=='-')fu=-1; c=getchar(); }
while(c>='0'&&c<='9'){ f=(f<<3)+(f<<1)+c-48; c=getchar(); }
return f*fu;
}
inline ll ql(){
ll f=0;int fu=1; char c=getchar();
while(c<'0'||c>'9'){ if(c=='-')fu=-1; c=getchar(); }
while(c>='0'&&c<='9'){ f=(f<<3)+(f<<1)+c-48; c=getchar(); }
return f*fu;
}
inline db qd(){
char c=getchar();int flag=1;double ans=0;
while((!(c>='0'&&c<='9'))&&c!='-') c=getchar();
if(c=='-') flag=-1,c=getchar();
while(c>='0'&&c<='9') ans=ans*10+(c^48),c=getchar();
if(c=='.'){c=getchar();for(int Bit=10;c>='0'&&c<='9';Bit=(Bit<<3)+(Bit<<1)) ans+=(double)(c^48)*1.0/Bit,c=getchar();}
return ans*flag;
}
}
namespace Read{
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define ss(a) scanf("%s",a)
#define rai(x,a,b) repi(i,a,b) x[i]=qi()
#define ral(x,a,b) repi(i,a,b) x[i]=ql()
}
namespace Out{
#define pi(x) printf("%d",x)
#define pl(x) printf("%lld",x)
#define ps(x) printf("%s",x)
#define pc(x) printf("%c",x)
#define pe() puts("")
}
namespace DeBug{
#define MARK false
#define DB if(MARK)
#define pr(x) cout<<#x<<": "<<x<<endl
#define pra(x,a,b) cout<<#x<<": "<<endl; \
repi(i,a,b) cout<<x[i]<<" "; \
puts("");
#define FR(a) freopen(a,"r",stdin)
#define FO(a) freopen(a,"w",stdout)
}
using namespace Fast_Read;
using namespace Read;
using namespace Out;
using namespace DeBug;
ll a[5],m[5];
ll exgcd(ll a,ll b,ll &x,ll &y)
{
if(!a&&!b) return -1;
if(!b){ x=1,y=0; return a; }
ll d=exgcd(b,a%b,y,x);
y-=a/b*x;
return d;
}
ll qmul(ll a,ll b,ll mod)
{
ll res=0; a%=mod,b%=mod;
while(b){
if(b&1){ res=(res+a)%mod; }
b>>=1,a=(a+a)%mod;
}
return res;
}
ll excrt(ll *a,ll *m,int n)// 模m余a 注意所有m的lcm会不会爆ll
{
ll x,y,M=m[1],ans=a[1];
repi(i,2,n){
ll ta=M,tb=m[i],c=(a[i]-ans%tb+tb)%tb;
ll d=exgcd(ta,tb,x,y),bg=tb/d;
if(c%d!=0) return infl;
x=qmul(x,c/d,bg);
ans+=x*M,M*=bg,ans=(ans%M+M)%M;
}
return (ans%M+M)%M;//最小非负解
}
int main()
{
int T=qi();
while(T--)
{
ll x=ql(),y=ql(),p=ql(),q=ql();
ll ans=infl;
m[1]=2*(x+y),m[2]=p+q;
repi(i,x,x+y-1)repi(j,p,p+q-1) a[1]=i,a[2]=j,gmn(ans,excrt(a,m,2));
if(ans==infl) puts("infinity");
else pl(ans),pe();
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, s, n) for(int i = (int)(s); i < (int)(n); ++i)
ll INF = 1ll << 60;
//拡張ユークリッドの互除法
//ap+bq = gcd(a,b)となるp,qを求め、gcd(a,b)を返す
ll extGCD(ll a, ll b, ll &p, ll &q){
if(b == 0){
p = 1;
q = 0;
return a;
}
ll d = extGCD(b, a%b, q, p);
q -= a/b*p;
return d;
}
//中国剰余定理
//x≡b1(mod m1),x≡b2(mod m2)を満たすx≡r(mod lcm(m1,m2))のr, lcmを返す
pll crt(ll b1, ll b2, ll m1, ll m2){
ll p, q;
ll d = extGCD/*拡張ユークリッドの互除法*/(m1, m2, p, q);
if((b2-b1)%d != 0)
return make_pair(0, -1);
ll m = m1*m2/d;
ll tmp = (b2-b1)/d*p%(m2/d);
ll r = ((b1+m1*tmp)%m+m)%m;
return make_pair(r, m);
}
int main(){
int t;
cin >> t;
rep(i, 0, t){
ll ans = INF;
ll x, y, p, q;
cin >> x >> y >> p >> q;
rep(t1, x, x+y){
rep(t2, p, p+q){
auto [r, lcm] = crt(t1, t2, 2*(x+y), p+q);
if(lcm == -1)
continue;
if(ans > r)
ans = r;
}
}
if(ans == INF)
cout << "infinity" << endl;
else
cout << ans << endl;
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define All(a) (a).begin(),(a).end()
#define INF 2e18;
#define MOD 1000000007
int main(){
ll n,m,ans,a,M=0;
cin >> n>>m;
vector<vector<ll>> v(n+1);
for(ll i=0;i<n;i++){
cin >> a;
M=max(M,a);
v[a].push_back(i);
}
for(ll i=0;i<=n;i++){
if(v[i].empty()){
cout << i << endl;
return 0;
}
//cout << v[i].front()<<" "<<v[i].back() << endl;
if(n-m-1>=v[i].back() || v[i].front()>=m){
cout <<i<<endl;
return 0;
}
for(auto itr=v[i].begin();itr!=v[i].end()-1;itr++){
if(*(itr+1)-*itr>m){
//cout<<*(itr+1)<<" "<<*itr<<endl;
cout << i << endl;
return 0;
}
}
}
//cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
constexpr int N = 800;
int dp[N + 1][N + 1];
int a[N + 1][N + 1];
int n, k;
int query(int xi, int yi, int xf, int yf) {
return dp[xf][yf] - dp[xi - 1][yf] - dp[xf][yi - 1] + dp[xi - 1][yi - 1];
}
bool check(int x) {
int i, j;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + (a[i][j] <= x);
}
}
for (i = 1; i + k - 1 <= n; i++) {
for (j = 1; j + k - 1 <= n; j++) {
if (query(i, j, i + k - 1, j + k - 1) >= (k * k + 1) / 2) {
return true;
}
}
}
return false;
}
int main() {
int l, r, m, i, j;
vector<int> v;
scanf("%d%d", &n, &k);
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
scanf("%d", a[i] + j);
v.push_back(a[i][j]);
}
}
sort(v.begin(), v.end());
l = 0;
r = v.size() - 1;
while (l < r) {
m = l + (r - l) / 2;
if (check(v[m])) {
r = m;
}
else {
l = m + 1;
}
}
printf("%d\n", v[l]);
return 0;
} |
#include<bits/stdc++.h>
#include <iterator>
#include <iostream>
#include <numeric>
#include <math.h>
#define ll long long
#define ull long
#define mpa make_pair
#define pb push_back
#define ff first
#define pii pair<ll,ll>
#define dd double
#define trace(x) cerr << #x << " : " << x << endl
#define ss second
#define boost ios_base::sync_with_stdio(0)
#define forp(i,a,b) for(ll i=a;i<=b;i++)
#define rep(i,n) for(ll i=0;i<n;++i)
#define ren(i,n) for(ll i=n-1;i>=0;i--)
#define forn(i,a,b) for(ll i=a;i>=b;i--)
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end();
#define sc(x) scanf("%lld",&x)
#define clr(x,val) memset(x,val,sizeof(x))
#define pr(x) prllf("%lld\n",x)
#define pdd pair<dd,dd>
#define read_arr(a,n) for(ll i=1;i<=n;i++)cin>>a[i];
#define init_arr(a,n) for(ll i=1;i<=n;i++)a[i]=n-i+1;
#define prec(x) cout<<fixed<<setprecision(x)
#define fre freopen("input.txt","r",stdin),freopen("output.txt","w",stdout)
#define arr array
using namespace std;
void solve(ll tc){
ll n;
cin>>n;
vector<pair<ll,string> > m(n);
for(ll i=0;i<n;i++){
cin>>m[i].ss>>m[i].ff;
}
sort(all(m));
cout<<m[n-2].ss;
}
int main(){
// fre;
ll t;
t=1;
// cin>>t;
ll tc=1;
while(t--){
solve(tc);
tc++;
}
}
| #include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[])
{
int n;
cin >> n;
vector<pair<int, string> > v(n);
for (int i = 0; i < n; ++i)
{
cin >> v[i].second >> v[i].first;
}
sort(v.begin(), v.end());
cout << v[n - 2].second;
return 0;
} |
#pragma GCC optimize ("O2")
#pragma GCC target ("avx2")
//#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
#include<iostream>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please
const int mod = 1e9 + 7;
int dp[2][100][100];
int AB[200], *A = AB, *B = AB + 100;
constexpr ll modpow(ll A, ll B) {
ll kotae = 1;
while (B > 0) {
if (B & 1) kotae = kotae * A % mod;
A = A * A % mod;
B >>= 1;
}
return kotae;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, M, K;
cin >> N >> M >> K;
rep(i, N) {
cin >> A[i];
}
auto mae = dp[0], ato = dp[1];
int are = modpow(2 * M, mod - 2);
rep(i, M) {
int x, y;
cin >> x >> y;
x--;
y--;
if (x < y) swap(x, y);
mae[x][x]--;
mae[x][y]++;
mae[y][y]--;
}
rep(x, N) rep(y, x + 1) {
mae[x][y] = (ll(mae[x][y] + mod) * are + (x == y)) % mod;
mae[y][x] = mae[x][y];
}
const ll ma = (1ll << 32) - 1;
rep(i, 30) {
if (K >> i & 1) {
rep(i, N) {
ll t1 = 0, t2 = 0;
rep(j, N) {
auto tmp = (ll)mae[i][j] * A[j];
t1 += tmp >> 32;
t2 += tmp & ma;
}
B[i] = ((t1 % mod << 32) + t2) % mod;
}
swap(A, B);
}
if (!K | i >= 31 - __builtin_clz(K)) break;
rep(x, N) rep(y, x + 1) {
auto tmp1 = mae[x];
auto tmp2 = mae[y];
ll t1 = 0, t2 = 0;
rep(z, N) {
auto tmp = *tmp1++ * (ll)*tmp2++;
t1 += tmp >> 32;
t2 += tmp & ma;
}
ato[y][x] = (ato[x][y] = ((t1 % mod << 32) + t2) % mod);
}
swap(mae, ato);
}
rep(i, N) co(A[i]);
Would you please return 0;
} | // Author : heyuhhh
// Created Time : 2021/03/07 19:18:55
#include<bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 2e5 + 5, M = 17, MOD = 1e9 + 7;
inline int add(int x, int y) {
return x + y >= MOD ? x + y - MOD : x + y;
}
inline int dec(int x, int y) {
return x - y < 0 ? x - y + MOD : x - y;
}
inline int mul(int x, int y) {
return 1ll * x * y % MOD;
}
string s;
int n, k;
int dp[N][M];
int cnt[M];
int trans(char t) {
if (t >= '0' && t <= '9') {
return t - '0';
}
return t - 'A' + 10;
}
void run() {
cin >> s >> k;
n = s.length();
int pre = 0;
dp[0][0] = 1;
for (int i = 0; i < n; ++i) {
for (int j = 0; j <= min(i, k); ++j) {
if (j == 0) {
if (i > 0) {
dp[i + 1][1] = add(dp[i + 1][1], 15);
dp[i + 1][0] = add(dp[i + 1][0], 1);
}
} else {
dp[i + 1][j + 1] = add(dp[i + 1][j + 1], mul(16 - j, dp[i][j]));
dp[i + 1][j] = add(dp[i + 1][j], mul(j, dp[i][j]));
}
}
for (int t = (pre == 0); t < trans(s[i]); ++t) {
if (++cnt[t] == 1) dp[i + 1][pre + 1] = add(dp[i + 1][pre + 1], 1);
else dp[i + 1][pre] = add(dp[i + 1][pre], 1);
--cnt[t];
}
if (++cnt[trans(s[i])] == 1) ++pre;
}
int ans = dp[n][k] + (pre == k);
cout << ans << '\n';
}
int main() {
#ifdef Local
freopen("input.in", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
run();
return 0;
} |
#ifdef _LOCAL
#include "local_include.hpp"
#else
#include <bits/stdc++.h>
using namespace std;
#endif
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define fto(i, s, e) for (int i = (s); i <= (e); ++i)
#define fto1(i, s, e) for (int i = (s); i < (e); ++i)
#define fdto(i, s, e) for (int i = (s); i >= (e); --i)
#define fit(it, a) for (auto it = (a).begin(); it != (a).end(); ++it)
#define fat(i, a) for (auto i : (a))
#define ll long long
#define ii pair<int, int>
#define pll pair<ll, ll>
template<class T, class Cmp = less<T>> using oss = tree<T, null_type, Cmp, rb_tree_tag, tree_order_statistics_node_update>;
#define bc __builtin_popcountll
#define y1 ansdj
#define endl '\n'
#define ff first
#define ss second
#define sz(v) int((v).size())
#define all(v) (v).begin(), (v).end()
#define bug(...) _bug(cout, __VA_ARGS__)
#define buga(a, s, e) cout << '{'; if (e < s) cout << '}'; else fto (__i, s, e) cout << a[__i] << " }"[__i == e]; cout << endl
template<class T1, class T2> ostream& operator<<(ostream &os, pair<T1, T2> const &v) {
return os << '(' << v.ff << ", " << v.ss << ')';
}
template<typename T>
void _bug(ostream &os, T const &var) { os << var << endl; }
template<typename T, typename... Args>
void _bug(ostream &os, T const &var, Args const &... args) {
os << var << ' ';
_bug(os, args...);
}
double const pi = acos(-1);
#define oo 1000000007
#define OO 1000000000000000003LL
int const maxn = 1e5+3;
void gcd(int a, int b, ll &x, ll &y) {
if (b == 0) {
x = 1;
y = 0;
} else {
gcd(b, a%b, x, y);
ll tmp = x;
x = y;
y = tmp - a/b*y;
}
}
#define multi_test 1
void _main() {
int n, s, k;
cin >> n >> s >> k;
int g = __gcd(n-s, __gcd(n, k));
n /= g, s /= g, k /= g;
g = __gcd(k, n);
if (g != 1) bug(-1);
else {
ll x, y;
gcd(k, n, x, y);
x *= (n-s);
x %= n;
if (x < 0) x += n;
bug(x);
}
}
int main() {
#ifdef _LOCAL
freopen("main.inp", "r", stdin);
freopen("main.out", "w", stdout);
#endif
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int t = 1;
if (multi_test) cin >> t;
while (t--) {
_main();
}
#ifdef _LOCAL
cerr << 0.001*clock() << endl;
#endif
return 0;
} | #include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
#define rep(i,n) for(ll i=0; i<n; i++)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rrep(i,n) for(ll i=n-1; i>=0; i--)
#define fi first
#define se second
#define pcnt __builtin_popcountll
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
typedef pair<ll,Pll> PlP;
template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }
template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }
template<class A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}cout << "\n";}
template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);}
string zero_padding(int val, int nf){ ostringstream sout;sout << setfill('0') << setw(nf) << val; return sout.str();};
const ld eps = 1e-10;
ull mo = 1000000007;
ld PI=asin(1)*2;
//using namespace atcoder;
vector<vector<PlP>> G(100010);
int main(){
ll N,M;
cin >> N >> M;
vector<ll> A(M), B(M), C(M), D(M);
rep(i,M){
cin >> A[i] >> B[i] >> C[i] >> D[i];
A[i]--; B[i]--;
}
rep(i,M){
G[A[i]].push_back({C[i],{D[i],B[i]}});
G[B[i]].push_back({C[i],{D[i],A[i]}});
}
vector<ll> d1(N,1e18);
priority_queue<Pll, vector<Pll>, greater<Pll>> que;
que.push(Pll(0,0));
while(!que.empty()){
ll t = que.top().fi;
ll v = que.top().se;
que.pop();
if(d1[v] <= t) continue;
//if(d1[v] != 1e18) continue;
d1[v] = t;
for(auto& p:G[v]){
ll c = p.fi;
ll d = p.se.fi;
ll u = p.se.se;
ll rd1 = max(ll(sqrt(ld(d))-1),ll(t));
ll rd2 = max(ll(sqrt(ld(d))),ll(t));
ll tt1 = ll(rd1) + c + ll(d/(rd1+1));
ll tt2 = ll(rd2) + c + ll(d/(rd2+1));
// cout << L << " " << R << " " << r1 << " " << r2 << endl;
que.push(Pll(min(tt1,tt2),u));
}
}
if(d1[N-1] == 1e18){
drop(-1);
}
cout << d1[N-1] << endl;
}
|
#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<complex>
#include<cstdio>
#define initdp(a,b) for(int i=0;i<=a;i++)for(int j=0;j<=b;j++)dp[i][j]=-1;
#define fi first
#define se second
#define pb push_back
#define pii pair<int,int>
#define ppi pair<pii,int>
#define pip pair<int,pii>
#define ll long long
#define pll pair<ll,ll>
#define rep(i,n) for(int i=0;i<n;i++)
#define repd(i,n) for(int i=n-1;i>=0;i--)
#define inf 1000000001
#define inf1 1000000000000000001
#define mod 1000000007
#define pie 3.14159265358979323846
#define N 100005
#define mid(l,r) l+(r-l)/2
#define removeduplicates(vec) vec.erase( unique( vec.begin(), vec.end() ), vec.end() )
#define memset1(v) memset(v,-1,sizeof(v))
#define memset0(v) memset(v,0,sizeof(v))
using namespace std;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
int ddx[8]={1,1,0,-1,-1,-1,0,1},ddy[8]={0,1,1,1,0,-1,-1,-1};
void mad(ll &a,ll b){a=(a+b)%mod;if(a<0)a+=mod;}
ll gcd(ll a,ll b){ if(!a)return b;return gcd(b%a,a);}
void solve(){
long double r,x,y;
cin>>r>>x>>y;
long double d=sqrt(x*x+y*y);
if(d==r)cout<<1;
else if(d<=r+r)cout<<2;
else cout<<ceil(d/r);
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t=1;
//cin>>t;
while(t--){
solve();
}
} | #include <bits/stdc++.h>
using namespace std;
//const long nPrime = 1000000007;
//const long nPrime = 998244353;
int main() {
long r,x,y;
cin >> r >> x >> y;
if(x*x+y*y == r*r){
cout << "1" << endl;
return 0;
}
long nSq = (x*x+y*y+r*r-1) / (r*r);
long nAns = long(sqrt(double(nSq)));
if(nAns * nAns < nSq){
nAns++;
}
nAns = max(nAns , 2L);
cout << nAns << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr int Inf = 1000000030;
constexpr ll INF= 2000000000000000000;
constexpr ll MOD = 1000000007;
const double PI = 3.1415926535897;
typedef pair<int,int> P;
typedef pair<int,P> PP;
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;
}
ll mod(ll val, ll M) {
val = val % M;
if(val < 0) {
val += M;
}
return val;
}
template<typename T>
T RS(T N, T P, T M){
if(P == 0) {
return 1;
}
if(P < 0) {
return 0;
}
if(P % 2 == 0){
ll t = RS(N, P/2, M);
if(M == -1) return t * t;
return t * t % M;
}
if(M == -1) {
return N * RS(N,P - 1,M);
}
return N * RS(N, P-1, M) % M;
}
int dp[1010][1010];
int main() {
int N,M;
cin >> N >> M;
vector<int> A(N);
vector<int> B(M);
for(int i = 0;i < N;i++) {
cin >> A.at(i);
}
for(int i = 0;i < M;i++) {
cin >> B.at(i);
}
for(int i = 0;i <= N;i++) {
for(int j = 0;j <= M;j++) {
dp[i][j] = 0;
}
}
for(int i = 0;i <= N;i++) {
dp[i][0] = i;
}
for(int i = 0;i <= M;i++) {
dp[0][i] = i;
}
for(int i = 1;i <= N;i++) {
for(int j = 1;j <= M;j++) {
int c = 0;
if(A.at(i - 1) != B.at(j - 1)) c = 1;
dp[i][j] = min({dp[i - 1][j] + 1,dp[i][j - 1] + 1,dp[i - 1][j - 1] + c});
}
}
cout << dp[N][M] << endl;
} | /*ver 7*/
#include <bits/stdc++.h>
using namespace std;
void init() {cin.tie(0);ios::sync_with_stdio(false);cout << fixed << setprecision(15);}
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vd = vector<ld>;
using vs = vector<string>;
using vb = vector<bool>;
using vvl = vector<vector<ll>>;
using vvd = vector<vector<ld>>;
using vvs = vector<vector<string>>;
using vvb = vector<vector<bool>>;
using pll = pair<ll,ll>;
using mll = map<ll,ll>;
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
#define each(x,v) for(auto& x : v)
#define reps(i,a,b) for(ll i=(ll)a;i<(ll)b;i++)
#define rep(i,n) for(ll i=0;i<(ll)n;i++)
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define mp make_pair
const ll INF = 1LL << 60;
#define CLR(mat,f) memset(mat, f, sizeof(mat))
#define IN(a, b, x) (a<=x&&x<=b)
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() ) //被り削除
#define debug cout << "line : " << __LINE__ << " debug" << endl;
#define ini(...) int __VA_ARGS__; in(__VA_ARGS__)
#define inl(...) long long __VA_ARGS__; in(__VA_ARGS__)
#define ind(...) long double __VA_ARGS__; in(__VA_ARGS__)
#define ins(...) string __VA_ARGS__; in(__VA_ARGS__)
#define inc(...) char __VA_ARGS__; in(__VA_ARGS__)
void in(){}
template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);}
template <typename T> void in1(T &s) {rep(i,s.size()){in(s[i]);}}
template <typename T> void in2(T &s,T &t) {rep(i,s.size()){in(s[i],t[i]);}}
template <typename T> void in3(T &s,T &t,T &u) {rep(i,s.size()){in(s[i],t[i],u[i]);}}
template <typename T> void in4(T &s,T &t,T &u,T &v) {rep(i,s.size()){in(s[i],t[i],u[i],v[i]);}}
template <typename T> void in5(T &s,T &t,T &u,T &v,T &w) {rep(i,s.size()){in(s[i],t[i],u[i],v[i],w[i]);}}
void out(){cout << endl;}
template <typename T,class... U> void out(const T &t,const U &...u){cout << t; if(sizeof...(u)) cout << " "; out(u...);}
void die(){cout << endl;exit(0);}
template <typename T,class... U> void die(const T &t,const U &...u){cout << t; if(sizeof...(u)) cout << " "; die(u...);}
template <typename T> void outv(T s) {rep(i,s.size()){if(i!=0)cout<<" ";cout<<s[i];}cout<<endl;}
template <typename T> void out1(T s) {rep(i,s.size()){out(s[i]);}}
template <typename T> void out2(T s,T t) {rep(i,s.size()){out(s[i],t[i]);}}
template <typename T> void out3(T s,T t,T u) {rep(i,s.size()){out(s[i],t[i],u[i]);}}
template <typename T> void out4(T s,T t,T u,T v) {rep(i,s.size()){out(s[i],t[i],u[i],v[i]);}}
template <typename T> void out5(T s,T t,T u,T v,T w) {rep(i,s.size()){out(s[i],t[i],u[i],v[i],w[i]);}}
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
template <typename T> T allSum(vector<T> a){T ans=T();each(it,a)ans+=it;return ans;}
ll ceilDiv(ll a,ll b) {return (a+b-1)/b;}
ld dist(pair<ld,ld> a, pair<ld,ld> b){return sqrt(abs(a.fi-b.fi)*abs(a.fi-b.fi)+abs(a.se-b.se)*abs(a.se-b.se));} // 2点間の距離
ll gcd(ll a, ll b) { return b != 0 ? gcd(b, a % b) : a; }
ll lcm(ll a,ll b){ return a / gcd(a,b) * b;}
template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }
#define YES(n) ((n) ? "YES" : "NO" )
#define Yes(n) ((n) ? "Yes" : "No" )
#define yes(n) ((n) ? "yes" : "no" )
int main(){
init();
inl(n);
out(n-1);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(ll i=0,endrep=(n); i<endrep; ++i)
#define rep1(i,n) for(ll i=1,endrep=(n); i<=endrep; ++i)
#define revrep(i,n) for(ll i=(ll)(n)-1; i>=0; --i)
inline constexpr ll Inf = (1ULL << 60) -123456789;
#define fastio cin.tie(0); ios_base::sync_with_stdio(false); cout<<fixed<<setprecision(10);
#define newl '\n'
#define YN(e) ((e)?"Yes":"No")
#define all(a) begin(a),end(a)
#define rall(a) rbegin(a),rend(a)
#define delif(c,pred) (c).erase(remove_if(all(c),(pred)), end(c))
template <class T,class U> bool updmax(T& a, U b) { if(b>a){ a=b; return true;} return false;}
template <class T,class U> bool updmin(T& a, U b) { if(b<a){ a=b; return true;} return false;}
inline constexpr int Mod = 1000000007;
//inline constexpr int Mod = 998244353;
#define dbg(a) cerr << #a << ": " << a << endl;
#define dbgs(s) cerr << #s << endl;
#define dbg1(a,n) cerr<<#a<<": "; rep(i,n) cerr<<a[i]<<" "; cerr<<endl;
#define dbg2(m,h,w) cerr<<#m<<":"<<endl; rep(i,h){ rep(j,w)cerr<<m[i][j]<<" "; cerr<<endl; }
template <class T, class U> ostream& operator << (ostream& os, pair<T,U> v) {os<<v.first<<","<<v.second;return os;}
template <class T, size_t N> ostream& operator << (ostream& os, array<T,N> v) {rep(i,N)os<<v[i]<<(i+1<(ll)N?" ":"");return os;}
int rng(int n) { return rand()/(RAND_MAX+1.0)*n; }
using P = pair<ll,ll>;
ll a[505][505], b[505][505];
int main() {
fastio;
ll r,c;
cin >> r >> c;
auto z = [&](ll v, ll h, ll f=0)->ll { return f*r*c + v*c + h; };
auto rz = [&](ll p)->array<ll,3> { return {p%(r*c)/c, p%c, p/(r*c)}; };
rep(i,r) rep(j,c-1) cin >> a[i][j];
rep(i,r-1) rep(j,c) cin >> b[i][j];
priority_queue<P,vector<P>, greater<>> q;
vector<ll> cost(r*c*2, Inf);
q.push({0,z(0,0)});
cost[z(0,0)] = 0;
while (!q.empty()) {
auto [co,p] = q.top(); q.pop();
auto [v,h,f] = rz(p);
if (co != cost[p]) continue;
auto push = [&, co=co](ll nc, ll np) { if (updmin(cost[np], co+nc)) q.push({co+nc,np}); };
if (f == 0) {
if (h < c-1) push(a[v][h], z(v,h+1,0));
if (h > 0) push(a[v][h-1], z(v,h-1,0));
if (v < r-1) push(b[v][h], z(v+1,h,0));
if (v > 0) push(1, z(v,h,1));
} else {
if (v < r-1) push(0, z(v,h,0));
if (v > 0) push(1, z(v-1,h,1));
}
}
ll ans = cost[z(r-1,c-1,0)];
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(), (x).end()
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vector<bool>> vvb;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
void debugF(ostream &os) {os << endl;}
template <typename Head, typename... Tail>
void debugF(ostream &os, Head H, Tail... T) { os << " " << H; debugF(os, T...); }
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debugF(cerr, __VA_ARGS__)
template<typename T> istream &operator>>(istream &is, vector<T> &v){ for (auto &x : v) is >> x; return is; }
template<typename T> ostream &operator<<(ostream &os, vector<T> &v){ for(int i = 0; i < v.size(); os << (i>0 ? " ":"") << v[i++]); return os;}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
class Dijkstra{
public:
static vl solve(vector<vector<pii>> &g, int s, int target = -1){
//g[v] = list of (u,w) , where v->u with weight w.
int n = g.size();
assert(s >= 0 && s < n);
vl dist(n, -1);
vector<bool> used(n, false);
set<pair<ll,int>> S;
S.insert({0,s});
while(!S.empty()){
auto min_data = *S.begin();
S.erase(S.begin());
ll min_dist = min_data.first;
int v = min_data.second;
dist[v] = min_dist;
used[v] = true;
if(target == v)
break;
for(auto u_data : g[v]){
int u = u_data.first;
int w = u_data.second;
if(w < 0){
throw new invalid_argument("graph with negative edges");
}
if(used[u])
continue;
if(dist[u] == -1 || (dist[v]+w < dist[u])){
if(dist[u] != -1)
S.erase({dist[u],u});
dist[u] = dist[v] + w;
S.insert({dist[u],u});
}
}
}
return dist;
}
};
void solve(){
int n, m, i, j, u, v;
cin >> n >> m;
auto getI = [&](int i, int j, bool flag){
int idx = i*m + j;
if(flag) idx += n*m;
return idx;
};
int N = 2*n*m;
vector<vector<pii>> g(N);
for(i=0;i<n;i++){
for(j=0;j<m-1;j++){
int a; cin >> a;
u = getI(i,j,false);
if(j+1 < m){
v = getI(i,j+1,false);
g[u].push_back({v, a});
g[v].push_back({u, a});
}
}
}
for(i=0;i<n-1;i++){
for(j=0;j<m;j++){
int b; cin >> b;
u = getI(i,j,false);
int uu = getI(i,j,true);
if(i+1 < n){
v = getI(i+1,j,false);
g[u].push_back({v, b});
int vv = getI(i+1,j,true);
g[vv].push_back({uu,1});
}
}
}
for(i=0;i<n;i++){
for(j=0;j<m;j++){
u = getI(i,j,false);
int uu = getI(i,j,true);
g[u].push_back({uu, 1});
g[uu].push_back({u, 0});
}
}
int sv = getI(0,0,false);
int tv = getI(n-1, m-1, false);
auto ans = Dijkstra::solve(g, sv, tv);
cout << ans[tv] << "\n";
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int nt = 1;
for(int tt=1;tt<=nt;tt++){
solve();
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
cin >> n;
for (int i = 1; i <= n; i++)
cout << (int)log2(2 * i) << ' ';
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const double pi = acos(-1.0);
typedef long long ll;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
int ans = 0;
for(int i = 1; i <= n; i++) {
int a = i;
bool ok = true;
while(a > 0) {
int b = a % 10;
if(b == 7) {
ok = false;
}
a = a / 10;
}
int c = i;
while(c > 0) {
int d = c % 8;
if(d == 7) {
ok = false;
}
c = c / 8;
}
if(ok) {
ans++;
}
}
cout << ans << endl;
} |
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int N = 1e3 + 10;
int n,m;
vector<int>v[N][30];
int mp[N][N];
int dist[N][N];
struct node{
int x,y,sum;
bool operator < (const node &p)const{
return sum > p.sum;
}
};
int flag[N][N],ans = 0x3f3f3f3f;
void dij(){
memset(dist,0x3f,sizeof dist);
priority_queue<node>q;
q.push({1,n,0});
dist[1][n] = 0;
while(q.size()){
node p = q.top();
q.pop();
if(flag[p.x][p.y]) continue;
flag[p.x][p.y] = 1;
if(p.x == p.y){
ans = min(ans,dist[p.x][p.y]);
}
if(mp[p.x][p.y]) ans = min(ans,dist[p.x][p.y] + 1);
for(int i = 0; i <= 25; i++){
for(auto j : v[p.x][i]){
for(auto k : v[p.y][i]){
if(dist[j][k] > dist[p.x][p.y] + 2){
dist[j][k] = dist[p.x][p.y] + 2;
q.push({j,k,dist[j][k]});
}
}
}
}
}
}
int main(){
cin >> n >> m;
for(int i = 1; i <= m; i++){
int a,b;char c;
scanf("%d %d %c",&a,&b,&c);
v[a][c - 'a'].push_back(b);
v[b][c - 'a'].push_back(a);
mp[a][b] = 1;
mp[b][a] = 1;
}
dij();
if(ans == 0x3f3f3f3f) cout << -1 <<endl;
else cout << ans <<endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
#define rep(i, n) for(int i = 0, i##_len=(n); i < i##_len; ++i)
#define per(i, n) for(int i = (n)-1; i >= 0 ; --i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define len(x) ((int)(x).size())
void _cin() {} template <class Head, class... Tail> void _cin(Head&& head, Tail&&... tail) { cin >> head; _cin(forward<Tail>(tail)...); }
#define cin(Type, ...) Type __VA_ARGS__; _cin(__VA_ARGS__)
#define cinv(Type, xs, n) vector<Type> xs(n); rep(i, n) cin >> xs[i]
#define cinv2(Type, xs, ys, n) vector<Type> xs(n), ys(n); rep(i, n) cin >> xs[i] >> ys[i]
#define cinv3(Type, xs, ys, zs, n) vector<Type> xs(n), ys(n), zs(n); rep(i, n) cin >> xs[i] >> ys[i] >> zs[i]
#define cinvv(Type, xs, h, w) vector<vector<Type>> xs(h, vector<Type>(w)); rep(i, h) rep(j, w) cin >> xs[i][j]
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 Type> void print(vector<Type> &vec) { for (auto& a : vec) { cout << a; if (&a != &vec.back()) cout << " "; } cout << endl; }
template <class Type> void print(vector<vector<Type>> &df) { for (auto& vec : df) { print(vec); } }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void yesno(bool b) { cout << (b ? "yes" : "no") << endl; }
template<class Integer>bool chmax(Integer &a, const Integer &b) { if (a < b) { a = b; return 1; } return 0; }
template<class Integer>bool chmin(Integer &a, const Integer &b) { if (b < a) { a = b; return 1; } return 0; }
using ll = long long;
using P = pair<int, int>;
//using mint = modint1000000007;
void Main() {
cin(int, n);
vector<ll> op = { 0ll, -1ll*(1ll<<50), 1ll << 50 };
int lo = 2;
rep(i, n) {
cin(ll, a, t);
if (t == 1ll) {
op[0] += a;
op[1] += a;
op[2] += a;
}
else if (t == 2ll) {
chmax(op[1], a);
chmax(op[2], a);
lo = 2;
}
else if (t == 3ll) {
chmin(op[1], a);
chmin(op[2], a);
lo = 3;
}
}
cin(int, q);
while (q--) {
cin(ll, x);
if(lo==2) print(max(op[1], min(op[2],x + op[0])));
else print(min(op[2], max(op[1],x + op[0])));
}
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
//cout << fixed << setprecision(16);
//int t; cin >> t; while (t--)
Main();
} |
#include <bits/stdc++.h>
using namespace std;
bool check(string s) {
int n = s.length();
for (int i = 0, j = n - 1; i < n; ++i, --j)
if (s[i] ^ s[j])
return false;
return true;
}
signed main() {
string s;
cin >> s;
for (int i = 0; i < 20; ++i) {
if (check(s)) {
cout << "Yes" << '\n';
system("pause");
return 0;
}
s = "0" + s;
}
puts("No");
return 0;
} | //#include <tourist>
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<ll, ll> p;
const int INF = 1e9;
const double eps = 1e-7;
const ll LINF = ll(1e18);
const int MOD = 1000000007;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1}, Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
const long double pi = 4 * atan(1);
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define debug(v) \
cout << #v << ":"; \
for (auto x : v) \
{ \
cout << x << ' '; \
} \
cout << endl;
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>
istream &operator>>(istream &is, vector<T> &v)
{
for (T &x : v)
is >> x;
return is;
}
//cout<<fixed<<setprecision(15);有効数字15桁
//-std=c++14
//g++ yarudake.cpp -std=c++17 -I .
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
bool check(string s)
{
rep(i, s.size() / 2)
{
if (s[i] != s[s.size() - 1 - i])
return false;
}
return true;
}
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
string s;
cin >> s;
reverse(ALL(s));
if (check(s))
return yes, 0;
for (int i = 0; i < 10; i++)
{
s.push_back('0');
if (check(s))
return yes, 0;
}
no;
}
|
#include <bits/stdc++.h>
#define rep(i,n) for (int i=0; i<n; i++)
using namespace std;
int main() {
char S, T;
cin >> S;
cin >> T;
if(S=='Y') cout << char(T-32) << endl;
else cout << T << endl;
return 0;
} | #include<iostream>
#include<algorithm>
#include<string>
#include<map>
#include<vector>
#define rep(i, n) for(int i=0; i<n; i++)
using namespace std;
typedef long long ll;
int main(){
int a,b; cin>>a>>b;
cout << (a+b)/2 << " " << (a-b)/2 << endl;
} |
#include <bits/stdc++.h>
typedef long long LL;
const int MAXN = 1e5 + 10;
int n, m;
int a[MAXN], b[MAXN];
int main() {
scanf("%d", &n), m = n >> 1;
for (int i = 1; i <= n; ++i) scanf("%d", a + i);
for (int i = 1, x; i <= n; ++i) {
scanf("%d", &x);
if (i & 1) b[i / 2 + 1] = x - a[i];
else b[i / 2 + m] = x - a[i];
}
std::sort(b + 1, b + m + 1), std::sort(b + m + 1, b + n + 1);
LL sum = std::accumulate(a + 1, a + n + 1, 0LL);
for (int i = 1; i <= m; ++i) sum += std::max(b[i] + b[i + m], 0);
printf("%lld\n", sum);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
typedef long long ll;
typedef pair<int, int> pi;
string s;
int n, m;
ll odd, even;
int main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
odd = 0, even = 0;
cin >> n >> m;
for(int i = 1;i <= n;i++){
cin >> s;
int cnt = 0;
for(int i = 0;i < s.length();i++){
cnt += s[i] - '0';
}
if(cnt & 1){
odd++;
}else{
even++;
}
}
cout << odd * even << endl;
return 0;
}
|
//oct 10,2020
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define endl "\n"
#define pb push_back
#define PI 3.1415926535897932384626433832795l
#define F first
#define S second
#define mp make_pair
#define f(i,n) for(int i=0;i<n;i++)
#define loop(i,a,b) for(int i=a;i<b;i++)
#define fastio ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define gcd(a,b) __gcd((a),(b))
#define fill(a,value) memset(a,value,sizeof(a));
#define minn(v) *min_element(v.begin(), v.end());
#define maxx(v) *max_element(v.begin(), v.end());
#define print(x) cout<<(x)<<endl;
#define sum(v)+x accumulate(v.begin(), v.end(),x);
#define debug(x) cout<<#x<<'='<<(x)<<endl;
typedef pair<int,int> pii;
typedef vector<int> vi;
const int mod=1e9+7;
const int MOD=998244353;
signed main()
{
char s;
cin>>s;
char t;
cin>>t;
if(s=='Y')print((char)toupper(t))
else print(t)
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#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 PB push_back
#define MP make_pair
#define PH push
#define fst first
#define snd second
typedef long long ll;
typedef unsigned long long ull;
typedef double lf;
typedef long double Lf;
typedef pair<int, int> pii;
const int maxn = 2e5 + 5;
int n, m;
int a[maxn], b[maxn];
class Dsu{
public:
int fa[maxn];
ll f[maxn], g[maxn];
inline int find(int x){ return (x == fa[x] ? x : (fa[x] = find(fa[x]))); }
inline void init(){
FOR(i, 0, maxn)
fa[i] = i, f[i] = a[i], g[i] = b[i];
return;
}
inline void merge(int u, int v){
u = find(u); v = find(v);
if(u == v)
return;
f[v] += f[u];
g[v] += g[u];
fa[u] = v;
return;
}
}dsu;
int main(){
scanf("%d%d", &n, &m);
FOR(i, 0, n)
scanf("%d", a + i);
FOR(i, 0, n)
scanf("%d", b + i);
dsu.init();
FOR(i, 0, m){
int u, v;
scanf("%d%d", &u, &v); --u; --v;
dsu.merge(u, v);
}
FOR(i, 0, n) if(dsu.find(i) == i && dsu.f[i] != dsu.g[i]){
puts("No");
return 0;
}
puts("Yes");
return 0;
}
|
/*
* Author: Moon-light
* 并查集的启发式合并优化
* 因为这里使用的是按集合大小合并,所以可以加上路径压缩
*/
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define lowbit(x) ((x)&(-x))
#define sz(x) ((int)x.size())
#define fr(x) freopen(x,'r',stdin)
#define fw(x) freopen(x,'w',stdout)
#define mst(x,a) memset(x,a,sizeof(x))
#define all(a) begin(a),end(a)
#define bitcnt(x) (__builtin_popcountll(x))
#define rep(i,a,b) for(int i = (a);i<=(b); ++i)
#define per(i,a,b) for(int i = (a);i>=(b); --i)
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef pair<double,double> PDD;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<double> VB;
typedef double db;
template <class T> void clear(T& a) { T().swap(a);}
template <class T,class S>
inline bool upmin(T& a,const S&b){ return a>b ? a=b,1:0;}
template <class T,class S>
inline bool upmax(T&a, const S&b){ return a<b? a=b,1:0;}
//fast read(improved by fread)
char buf[1<<21],*p1=buf,*p2=buf;
inline int getc(){
return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;
}
inline int redi() {
int ret = 0,f = 0;char ch = getc();
while (!isdigit (ch)) {
if (ch == '-') f = 1;
ch = getc();
}
while (isdigit (ch)) {
ret = ret * 10 + ch - 48;
ch = getc();
}
return f?-ret:ret;
}
//global variable
const int N = 2e5+100;
const int mod = 1e9+7;
int n,q;
int p[N];
int a,b,op;
unordered_map<int,int> cs[N];
//functions
LL qmi(LL a,LL k,LL mod){
LL res = 1;
while(k){
if(k&1) res = res*a%mod;
k >>= 1;
a = a*a%mod;
}
return mod;
}
int gcd(int a,int b) {
return b==0? a:gcd(b,a%b);
}
int find(int x){
if(p[x] != x){
p[x] = find(p[x]);
}
return p[x];
}
void merge(int x,int y){
int fa = find(x);
int fb = find(y);
if(fa == fb) return ;
//小集合并到大集合上去
if(cs[fa].size() > cs[fb].size()){
swap(fa,fb);
}
p[fa] = fb;
for(auto &x:cs[fa]){
cs[fb][x.first] += x.second;
}
}
//
//code from here! Come on! Have a pleasant experience~
int main()
{
IO;
cin>>n>>q;
int c;
rep(i,1,n){
cin>>c;
cs[i][c] ++;
p[i] = i;
}
rep(i,1,q){
cin>>op>>a>>b;
if(op == 1){
merge(a,b);
}else{
int f = find(a);
cout<<cs[f][b]<<endl;
}
}
return 0;
} | #include<cstdio>
#include<cctype>
#define maxn 202202
inline int read(){
int r=0,f=0;
char c;
while(!isdigit(c=getchar()))f|=(c=='-');
while(isdigit(c))r=(r<<1)+(r<<3)+(c^48),c=getchar();
return f?-r:r;
}
inline char get_c(){
char c;
while(!isalpha(c=getchar()));
return c;
}
int n,m,f[maxn],rt[maxn];
int find(int x){
return x^f[x]?f[x]=find(f[x]):x;
}
struct val_seg{
#define ls(p) tr[p].ls
#define rs(p) tr[p].rs
#define cnt(p) tr[p].cnt
struct TR{
int ls,rs,cnt;
}tr[maxn<<6];
int tot;
inline int new_tr(){return ++tot;}
void add(int p,int x,int l,int r){
if(l==r){
cnt(p)++;
return;
}
int mid=(l+r)>>1;
if(x<=mid){
if(!ls(p))ls(p)=new_tr();
add(ls(p),x,l,mid);
}
else {
if(!rs(p))rs(p)=new_tr();
add(rs(p),x,mid+1,r);
}
}
int merge(int x,int y,int l,int r){
if(!x||!y)return x+y;
if(l==r){
cnt(x)+=cnt(y);
return x;
}
int mid=(l+r)>>1;
ls(x)=merge(ls(x),ls(y),l,mid);
rs(x)=merge(rs(x),rs(y),mid+1,r);
return x;
}
int query(int p,int l,int r,int pos){
if(!p)return 0;
if(l==r)return cnt(p);
int mid=(l+r)>>1;
if(pos<=mid)return query(ls(p),l,mid,pos);
return query(rs(p),mid+1,r,pos);
}
inline int ask(int p,int k){
if(cnt(p)<k)return 0;
return query(p,k,1,n);
}
#undef ls
#undef rs
#undef cnt
}v_s;
inline int get_new(){return v_s.new_tr();}
inline void Union(int u,int v){
int fu=find(u),fv=find(v);
if(fu==fv)return;
f[fv]=fu;
rt[fu]=v_s.merge(rt[fu],rt[fv],1,n);
}
int main(){
n=read(),m=read();
for(int i=1;i<=n;i++){
int x=read();
f[i]=i;
rt[i]=get_new();
v_s.add(rt[i],x,1,n);
}
for(int i=1;i<=m;i++){
int opt=read(),x=read(),y=read();
if(opt==1)Union(x,y);
else printf("%d\n",v_s.query(rt[find(x)],1,n,y));
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define faster ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pi acos(-1.0)
#define nl '\n'
///Library
#define mem(a,b) memset(a,b,sizeof(a))
#define all(a) a.begin(),a.end()
#define Sort(x) sort(x.begin(),x.end())
#define Reverse(x) reverse(x.begin(),x.end())
#define SortA(ar,s) sort(ar,ar+s)
#define SortD(ar,s) sort(ar,ar+s,greater<int>())
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define sq(x) (x)*(x)
#define for0(a, n) for(int i=a; i<n; i++)
#define for1(a, n) for(int i=a; i<=n; i++)
#define rfor0(n, a) for(int i=n-1; i>=a; i--)
#define rfor1(n, a) for(int i=n; i>a; i--)
#define min3(a,b,c) min(a,min(b,c))
#define min4(a,b,c,d) min(a,min(b,min(c,d)))
#define max3(a,b,c) max(a,max(b,c))
#define max4(a,b,c,d) max(a,max(b,max(c,d)))
#define ABS(x) ((x)<0?-(x):(x))
#define pb push_back
#define mod 1000000007
#define end printf("\n")
#define yes printf("YES\n")
#define no printf("NO\n")
///data type
typedef long long int ll;
typedef unsigned long long int llu;
int main()
{
ll n,a,b;
cin>>n>>a>>b;
cout<<n-a+b<<nl;
return 0;
}
| //Pradeep_7
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#define IO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define tc(x) int x; cin >> x; for(int tt=1;tt<=x;tt++)
#define int long long int
using namespace std;
class Input
{
public:
Input()
{
IO;
}
};
Input setup;
int32_t main()
{
int n,a,b;
cin>>n>>a>>b;
cout<<(n-a)+b;
}
|
#include<fstream>
#include<iostream>
#include<sstream>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<iomanip>
using namespace std;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
int main(){
long long m;
string x;
cin >> x >> m;
if(x.size() == 1){
if(stoi(x)<=m){
cout << 1 << endl;
}
else{
cout << 0 << endl;
}
return 0;
}
int d = 0;
for(auto t: x) d = max(int(t-'0'),d);
long long left = d;
long long right = m+1;
while(right-left>1){
long long mid = (left + right)/2;
long long v = 0;
for(auto c:x){
if(v > m/mid) v = m + 1;
else v = v*mid + (c-'0');
}
if(v <= m) left = mid ;
else right = mid ;
}
cout << left - d << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
int main(void){
int N;
string S;
cin >> N;
cin >> S;
int sum{0};
vector<vector<int>> nums(4,vector<int>(N+1, 0));
for(int i{0}; i<N; ++i){
nums[0][i+1] = nums[0][i];
nums[1][i+1] = nums[1][i];
nums[2][i+1] = nums[2][i];
nums[3][i+1] = nums[3][i];
if(S.at(i)=='A'){
++nums[0][i+1];
}
else if(S.at(i)=='T'){
++nums[1][i+1];
}
else if(S.at(i)=='C'){
++nums[2][i+1];
}
else if(S.at(i)=='G'){
++nums[3][i+1];
}
}
for(int is{0}; is<N-1; ++is){
for(int ie{is+2}; ie<=N; ie+=2){
int num_A = nums[0][ie] - nums[0][is];
int num_T = nums[1][ie] - nums[1][is];
int num_C = nums[2][ie] - nums[2][is];
int num_G = nums[3][ie] - nums[3][is];
if(num_A == num_T and num_C == num_G){
++sum;
}
}
}
cout << sum << endl;
}
|
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#include <set>
#include <limits.h>
#include <math.h>
int main(){
long long n;
cin>>n;
long long a=0;
for(long long i=0;i<n;i++){
long long s;
cin>>s;
if(s>10){
s=s-10;
a+=s;
}
}
cout<<a<<endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vll = vector<ll>;
using P = pair<int, int>;
constexpr int INF = 1e9;
constexpr ll INFLL = 1e18;
constexpr int MOD = 1e9 + 7;
const ld PI = acosl(-1);
#define rep(i,n) for(int i=0; i<(n); ++i)
#define all(n) n.begin(),n.end()
int main(){
ios::sync_with_stdio(false); cin.tie(nullptr);
cout << fixed << setprecision(15);
int n; cin >> n;
vector<ll> x(n);
rep (i, n) cin >> x[i];
ll tmp = 0;
rep (i, n) tmp += abs(x[i]);
cout << tmp << endl;
tmp = 0;
rep (i, n) tmp += x[i]*x[i];
cout << sqrtf64x(tmp) << endl;
tmp = 0;
rep (i, n) tmp = max(tmp, abs(x[i]));
cout << tmp << endl;
return 0;
}
|
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <fstream>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define x first
#define y second
#define Time (double)clock()/CLOCKS_PER_SEC
#define debug(x) std::cerr << #x << ": " << x << '\n';
#define FOR(i, n) for (int i = 0; i < n; ++i)
#define FORN(i, n) for (int i = 1; i <= n; ++i)
#define pb push_back
#define trav(a, x) for (auto& a : x)
using vi = vector<int>;
template <typename T>
std::istream& operator >>(std::istream& input, std::vector<T>& data)
{
for (T& x : data)
input >> x;
return input;
}
template <typename T>
std::ostream& operator <<(std::ostream& output, const pair <T, T> & data)
{
output << "(" << data.x << "," << data.y << ")";
return output;
}
template <typename T>
std::ostream& operator <<(std::ostream& output, const std::vector<T>& data)
{
for (const T& x : data)
output << x << " ";
return output;
}
ll div_up(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up
ll div_down(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down
ll math_mod(ll a, ll b) { return a - b * div_down(a, b); }
#define tcT template<class T
#define tcTU tcT, class U
tcT> using V = vector<T>;
tcT> void re(V<T>& x) {
trav(a, x)
cin >> a;
}
tcT> bool ckmin(T& a, const T& b) {
return b < a ? a = b, 1 : 0;
} // set a = min(a,b)
tcT> bool ckmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0;
}
ll gcd(ll a, ll b) {
while (b) {
tie(a, b) = mp(b, a % b);
}
return a;
}
signed main() {
#ifdef LOCAL
#else
#define endl '\n'
ios_base::sync_with_stdio(0); cin.tie(0);
#endif
int n;
cin >> n;
vi a(2 * n);
cin >> a;
vi sign(2 * n);
V <ii> ord;
FOR (i, 2 * n) {
ord.app(mp(a[i], i));
}
sort(all(ord));
FOR (i, n) {
sign[ord[i].y] = -1;
}
FOR (i, n) {
sign[ord[i + n].y] = 1;
}
string ans;
vi S;
trav (e, sign) {
if (S.empty() || S.back() == e) {
ans += '(';
S.app(e);
}
else {
ans += ')';
S.pop_back();
}
}
assert(S.empty());
cout << ans << endl;
} | #include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <set>
#include <sstream>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define reps(i, s, n) for (int i = s; i < n; i++)
#define debug(s, param) std::cerr << s << param << std::endl;
using namespace std;
using ll = long long;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
const int INF = 1e9;
const ll INFL = 1e18;
const ll MOD = 1000000007;
// https://atcoder.jp/contests/abc196/tasks/abc196_c
int main() {
ll n; cin >> n;
int ans = 0;
reps (i, 1, 1000001) {
ll digit = (ll)floor(log10(i)) + 1;
ll num = i * pow(10, digit) + i;
if (num > n) break;
++ans;
}
cout << ans << endl;
return 0;
}
|
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <vector>
#include <deque>
#include <bitset>
#include <list>
using namespace std;
#define pp pair<long long,long long>
#define f first
#define s second
#define ppp pair<pair<long long,long long>,long long>
#define pb push_back
#define ll long long
pp arr[2010],barr[2010];
int main() {
ll t,n,i,j,k,l;
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n;
ll ans=1e6;
for (i=1;i<=n;i++) {
cin>>arr[i].f>>barr[i].f;
arr[i].s=i;barr[i].s=i;
ans=min(ans,arr[i].f+barr[i].f);
}
sort(arr+1,arr+n+1);
sort(barr+1,barr+n+1);
if (arr[1].s==barr[1].s) {
ans=min(ans,max(arr[1].f,barr[2].f));
ans=min(ans,max(arr[2].f,barr[1].f));
} else {
ans=min(ans,max(arr[1].f,barr[1].f));
}
cout<<ans<<'\n';
return 0;
} | #include <bits/stdc++.h>
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i <= n; i++)
#define FORR(i, m, n) for(int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v+n)
#define MAX 100000
#define inf 1000000007
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;
using graph = vector<vector<int>>;
int main() {
//cin高速化
cin.tie(0);
ios::sync_with_stdio(false);
ll n;
cin >> n;
vll a(n), b(n);
ll ans = inf;
REP(i, n){
cin >> a[i] >> b[i];
}
REP(i, n){
REP(j, n){
if(i != j){
ans = min(ans, max(a[i], b[j]));
}else{
ans = min(ans, a[i] + b[j]);
}
}
}
cout << ans <<"\n";
return 0;
} |
#include <iostream>
#include <cmath>
using namespace::std;
int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int a, b, x, y;
cin >> a >> b >> x >> y;
int dif = a - b;
int t = 0;
if (dif > 0) {
t += x;
t += min(y, 2*x) * (dif - 1);
}
else if (dif < 0) {
t += x;
t += min(y, 2*x) * (-dif);
}
else {
t += x;
}
cout << t;
return 0;
} | #include <algorithm>
// #include <atcoder/all>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
typedef long long ll;
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
constexpr double PI = 3.141592653589793;
constexpr int INF = 1e9 + 10;
constexpr ll INFL = 1e18 + 10;
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>
void dump(const std::vector<T> &ar) {
for (T e : ar) {
std::cout << e << "\n";
}
}
template <typename T>
void dump(const std::set<T> &s) {
for (T e : s) {
std::cout << e << " ";
}
std::cout << "\n";
}
using namespace std;
// using namespace atcoder;
// cout << setprecision(20) << ans << "\n";
template <typename T>
void print(T ans) {
cout << ans << "\n";
}
template <>
void print(double ans) {
cout << setprecision(20) << ans << "\n";
}
ll getDigit(ll x) { return log10(x) + 1; }
int main() {
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int a, b, x, y;
cin >> a >> b >> x >> y;
vector<int> atower(110, INF), btower(110, INF);
if (a == b) {
cout << x << "\n";
return 0;
}
if (a > b) {
atower[a] = 0;
btower[a] = x;
for (int i = a; i > b - 1; --i) {
btower[i - 1] = min(atower[i] + x, btower[i] + y);
atower[i - 1] = min(atower[i] + y, btower[i - 1] + x);
}
cout << btower[b] << "\n";
return 0;
}
atower[a] = 0;
btower[a] = x;
for (int i = a; i < b + 1; i++) {
atower[i + 1] = min(atower[i] + y, btower[i] + x);
btower[i + 1] = min(atower[i + 1] + x, btower[i] + y);
}
cout << btower[b] << "\n";
return 0;
}
|
#pragma GCC optimize("Ofast,unroll-loops,fast-math")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll ;
#define pll pair<ll , ll >
#define all(x) (x).begin(),(x).end()
#define SZ(x) (ll)(x).size()
#define X first
#define Y second
#define mp make_pair
#define pii pair<int , int>
#define vec vector
#define file_io freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#define migmig ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
ll poww(ll a, ll b, ll md) {
return (!b ? 1 : (b & 1 ? a * poww(a * a % md, b / 2, md) % md : poww(a * a % md, b / 2, md) % md));
}
const int maxn = 1000*100+5 ;
const ll inf = 9223372036854775807 ;
const ll mod = 1e9 + 7 ;
const int lg = 20 ;
int main()
{
migmig ;
ll v , t , s , d ;
cin>>v>>t>>s>>d ;
if(d < t * v || d > s * v){
cout<<"Yes" ;
}else cout<<"No" ;
}
| #include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,f,n) for(ll i=(f); (i) < (n); i++)
#define repe(i,f,n) for(ll i=(f); (i) <= (n); i++)
#define repc(i,f,n) for(char i=(f); (i) <= (n); i++)
#define PI 3.14159265358979323846264338327950L
#define debug(x) cout<<#x<<" :: "<<x<<"\n";
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n";
#define debug3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n";
#define P pair<int, int>
#define Pl pair<ll, ll>
#define dvec vector<vector<ll>>
#define OUT(x) cout << x << endl; return 0;
//printf("%.10f\n")
//cout << fixed << setprecision(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 chmaxe(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;}
const ll MOD = 1000000007ll;
const ll INF = 1e+18;
const int iINF = 1e9;
const double EPS = 1e-8;
int main()
{
ll N; cin >> N;
vector<ll> A(N);
rep(i, 0, N)cin >> A[i];
vector<ll> B(N);
rep(i, 0, N)cin >> B[i];
ll ans = 0;
rep(i, 0, N){
ans += A[i] * B[i];
}
if(ans == 0) cout << "Yes" << endl;
else cout << "No" << endl;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){
return b==0?a:gcd(b,a%b);
}
ll lcm(ll a,ll b){
return a/gcd(a,b)*b;
}
int n;
ll dp[40];
int main(){
scanf("%d",&n);
dp[1]=1;
for(int i=2;i<=n;i++)
dp[i]=lcm(dp[i-1],i);
printf("%lld",dp[n]+1);
return 0;
} | #include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long int
#define ld long double
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define mem(x,y) memset(x,y,sizeof(x))
#define pii pair<int,int>
#define pll pair<ll,ll>
#define INF 1e9
#define INFL 1e18
#define mod 1000000007
//#define mod 998244353
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> os;
typedef tree<pii,null_type,less<pii>,rb_tree_tag,tree_order_statistics_node_update> os_pair;
ll power(ll x,ll n){ll res =1;while(n>0){if(n%2==1){res=res*x;}x=x*x;n=n/2;}return res;}
ll powm(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
//cout<< fixed << setprecision(10)
//__builtin_popcountll()
int main(){
fast;
int n,i,j;
cin>>n;
vector<int>v;
for(i=2;i<=n;i++)
{
int f=0;
for(j=2;j*j<=i;j++)
{
if(i%j==0)
{
f=1;
break;
}
}
if(f==0)
v.pb(i);
}
ll ans=1;
for(i=0;i<v.size();i++)
{
for(j=0;j<10;j++)
{
if(power(v[i],j)>n)
{
break;
}
}
j--;
ans=ans*power(v[i],j);
}
cout<<ans+1;
} |
/** (buri nazar wale tera muh kala 😑)
*
* @Author : rudraksh
* @Created : Thursday, November 26th 2020, 8:30:41 pm
* @Email : [email protected]
*
* “Think twice, code once.”
*/
#include<bits/stdc++.h>
#include<unistd.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;
typedef double db;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int,int> PI;
typedef pair<ll,ll> PL;
typedef map<int,int> MI;
typedef pair<int,PI> PIP;
#define ar array
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define eb emplace_back
#define F first
#define S second
#define repa(i,a,n) for(int i=a;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define pera(i,a,n) for(int i=a;i>=n;i--)
#define per(i,n) for(int i=n;i>=0;i--)
#define all(x) (x).begin(),(x).end()
#define loop(it,x) for(auto it=x.begin();it!=x.end();it++)
#define MS0(x) memset((x), 0, sizeof((x)))
#define MS1(x) memset((x), -1, sizeof((x)))
#define CP(dst,src) memcpy(dst, src, sizeof(src))
#define SZ(x) ((ll)(x).size())
#define watch(x) cout << (#x) << " is " << (x) << endl
#define TIME cerr << "\nTime elapsed: " << setprecision(5) <<1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
const ll mod=1e9+7;
const double pi=acos(-1);
void print() { cout<<'\n'; }
// void print() { cout<<endl; }
template <typename T, typename... Params>
void print(const T& var1, const Params&... var2)
{
cout<<var1<<' ';
print(var2...);
}
ll power(ll a,ll b)
{
if(b==0) return 1;
ll c = power(a,b/2);
if(b%2==0) return (c*c)%mod;
else return (a*((c*c)))%mod;
}
/************************* after all those #defines, finally i can code *****************************/
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int a, b, c, d;
cin >> a >> b >> c >> d;
if(a == c && b == d) print(0);
else if(a+b == c+d) print(1);
else if(a-b == c-d) print(1);
else if(abs(a-c) + abs(b-d) <= 3) print(1);
else if(abs(a-c) <= 5 && abs(b-d) <= 5) print(2);
else if(abs(abs(c-a) - abs(d-b)) <= 3) print(2);
else if(((d + (a-c))%2 + 2)%2) print(2);
else print(3);
return 0;
}
| #include<iostream>
#include<climits>
#define int long long
#define endl '\n'
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define infi INT_MAX
#define rinfi INT_MIN
#define inf LLONG_MAX
#define rinf LLONG_MIN
#define ff first
#define ss second
#ifndef ONLINE_JUDGE
#define line cout<<"here - "<<__LINE__<<"\n";
#define dbg(a) cout<<#a<<" --> "<<(a)<<"\n";
#define db(a,b) cout<<#a<<" --> "<<(a)<<"\n";cin>>b;
#else
#define line
#define dbg(a)
#define db(a,b)
#endif
char _;
using namespace std;
int r1,r2,c1,c2;
int solve(int x,int y)
{
if(x==r2 && y==c2)
return 0;
if(abs(x-r2)+abs(y-c2)<=3)
return 1;
int m1=abs(abs(x-r2)-abs(y-c2));
int w=0;
int p=0;
if(abs(x-r2)==0 || abs(y-c2)==0)
p=0;
else p=1;
if(m1==0)
return 1;
if(m1<=3)
return p+1;
if(abs(x-r2)%2 != abs(y-c2)%2)
w=1;
return w+2;
}
main()
{
fast
int tc=1,i,j;
// cin>>tc;
while(tc--)
{
cin>>r1>>c1>>r2>>c2;
int ans=inf;
for(i=-3;i<=3;i++)
{
for(j=-3;j<=3;j++)
{
if(abs(i)+abs(j)>3)
continue;
int x1=r1+i;
int y1=c1+j;
int f=0;
if(x1==r1 && y1==c1)
f=0;
else f=1;
f+=solve(x1,y1);
ans=min(ans,f);
}
}
cout<<ans<<endl;
}
}
|
#include <bits/stdc++.h>
#define pb push_back
#define vii vector<int>
#define task "poetry"
#define pll pair<ll, ll>
#define pii pair< ll, pair< ll, ll > >
#define fi first
#define se second
using namespace std;
using ll = long long;
using ull = unsigned long long;
const int N = 5e3+5;
const ll mod = 1e9+7;
const ll modx = 2019201913;
const ll mody = 2019201949;
ll m, n, k, T, u, v, tong, lab[N];
ll dp[N][N], a[N], d[N], c[N], b[N], ans, len[N];
string s, t ;
vector<ll> kq, adj[N], adt[N], res;
void dfs(ll u)
{
kq.pb(u);
a[u] = -1;
for(ll v : adj[u])
{
if(a[v] == 0)dfs(v);
}
}
void sol()
{
/*
cin >> n;
for(int i = 0; i < n; i ++)cin >> s[i];
for(int i = 0; i < n; i ++)
{
for(int j = 0; j < n; j ++)
{
if(s[i][j] == '1')
{
adj[i].pb(j);
adt[j].pb(i);
}
}
}
for(int i = 0; i < n; i ++)if(a[i] == 0)dfs(i);
for(int i = kq.size()-1; i >= 0; i --)
{
if(a[kq[i]] == 1)
{
++k;
bfs(kq[i], k);
}
}
cout << k << '\n';
for(ll u : res)cout << u <<" ";
*/
queue<ll> ds;
cin >> n >> s >> t;
if(s[0] != t[0])
{
ds.push(1);
}
for(int i = 2; i <= n; i ++)
{
if(s[i-1] == '1')
{
if(!ds.empty())
{
k = ds.front();
ds.pop();
s[i-1] = '0';
ans += i - k;
k = 0;
}
}
if(s[i-1] != t[i-1])ds.push(i);
}
if(!ds.empty())cout << -1;
else cout << ans;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen(task".in", "r"))
{
freopen(task".in","r",stdin);
freopen(task".out","w",stdout);
}
int ntest = 1;
//cin >> ntest;
while(ntest -- > 0)
sol();
}
/*
8 6 5
*/
| #include<bits/stdc++.h>
using namespace std;
const int buffer_size=1e5+5;
char buf[buffer_size],*S,*T;
bool flag_EOF;
inline char read_char()
{
if(S==T)
T=(S=buf)+fread(buf,1,buffer_size,stdin);
return S!=T?*(S++):EOF;
}
inline int read_int()
{
int flag=1;
char c=read_char();
while(c<'0'||c>'9')
{
if(c==EOF)
{
flag_EOF=true;
return 0;
}
flag=(c=='-'?-1:flag);
c=read_char();
}
int x=0;
while(c>='0'&&c<='9')
{
x=x*10+(c^48);
c=read_char();
}
return x*flag;
}
inline void Write(int x)
{
if(!x)
{
putchar('0');
return;
}
if(x<0)
{
putchar('-');
x=-x;
}
static char st[13];
int head=0;
while(x)
{
st[++head]=x%10+'0';
x/=10;
}
while(head>0)
putchar(st[head--]);
}
const int max_n=5e5+5;
char s[max_n],t[max_n];
int q[max_n],head=1,tail;
int main()
{
int n;
scanf("%d%s%s",&n,s+1,t+1);
for(int i=1;i<=n;++i)
{
if(s[i]=='1')
q[++tail]=i;
}
long long ans=0;
for(int i=1;i<=n;++i)
{
if(head<=tail&&q[head]==i)
++head;
if(s[i]!=t[i])
{
if(head>tail)
{
printf("-1");
return 0;
}
ans+=q[head]-i;
s[q[head++]]='0';
}
}
printf("%lld",ans);
return 0;
}
|
#include<iostream>
#include<array>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
#include<cassert>
using namespace std;
typedef long long ll;
const ll mod = 998244353;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
template<class T>bool chmax(T &a, const T &b) {if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a, const T &b) {if(b<a){a=b;return 1;}return 0;}
template<int mod>
struct ModInt {
long long x;
static constexpr int MOD = mod;
ModInt() : x(0) {}
ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
explicit operator int() const {return x;}
ModInt &operator+=(const ModInt &p) {
if((x += p.x) >= mod) x -= mod;
return *this;
}
ModInt &operator-=(const ModInt &p) {
if((x += mod - p.x) >= mod) x -= mod;
return *this;
}
ModInt &operator*=(const ModInt &p) {
x = (int)(1LL * x * p.x % mod);
return *this;
}
ModInt &operator/=(const ModInt &p) {
*this *= p.inverse();
return *this;
}
ModInt operator-() const { return ModInt(-x); }
ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
ModInt operator%(const ModInt &p) const { return ModInt(0); }
bool operator==(const ModInt &p) const { return x == p.x; }
bool operator!=(const ModInt &p) const { return x != p.x; }
ModInt inverse() const{
int a = x, b = mod, u = 1, v = 0, t;
while(b > 0) {
t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
return ModInt(u);
}
ModInt power(long long n) const {
ModInt ret(1), mul(x);
while(n > 0) {
if(n & 1)
ret *= mul;
mul *= mul;
n >>= 1;
}
return ret;
}
ModInt power(const ModInt p) const{
return ((ModInt)x).power(p.x);
}
friend ostream &operator<<(ostream &os, const ModInt<mod> &p) {
return os << p.x;
}
friend istream &operator>>(istream &is, ModInt<mod> &a) {
long long x;
is >> x;
a = ModInt<mod>(x);
return (is);
}
};
using modint = ModInt<mod>;
int n,m,k;
modint power[200010];
void solve(){
cin >> n >> m >> k;
if(n==1){
modint ans=0;
Rep(V,1,k+1){
ans+=((modint)(k-V+1)).power(m)-((modint)(k-V)).power(m);
}
cout << ans << endl;
return;
}
modint ans=0;
Rep(V,1,k+1){
power[V]=((modint)V).power(n);
}
if(m==1){
Rep(V,1,k+1){
ans+=power[V]-power[V-1];
}
cout << ans << endl;
return;
}
Rep(V,1,k+1){
ans+=(power[V]-power[V-1])*((modint)(k-V+1)).power(m);
}
cout << ans << endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(50);
solve();
} | #include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for(int i=0; i<(n); i++)
#define readVec(v) forn(i, v.size()){cin >> v[i];}
#define printArr(arr, n) forn(i, n){if (i) cout << " "; cout << arr[i];} cout << endl;
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define f first
#define s second
typedef long long ll;
typedef double ld;
typedef long double lld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef vector<vi> vii;
//Printing pairs and vectors
template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.f << ", " << p.s << ")"; }
template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) {
cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";
}
//2 147 483 647 int max
//9 223 372 036 854 775 807 ll max
void fast_io(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
}
void file_io(string taskname){
string fin = taskname + ".in";
string fout = taskname + ".out";
const char* FIN = fin.c_str();
const char* FOUT = fout.c_str();
freopen(FIN, "r", stdin);
freopen(FOUT, "w", stdout);
fast_io();
}
struct DSU{
vi parent;//Parent of each element. If parent[i] = i, it is a root of a set
vi num_elements;//Number of elements in a set given by a root.
int n;
DSU(int n):n(n),parent(n),num_elements(n,1){
forn(i,n){
parent[i] = i;
}
}
//Merge the two sets that x and y are in.
void join(int x, int y){
if(parent[x] == parent[y]){
return; //already in same set
}
int xroot = root(x);
int yroot = root(y);
if(num_elements[xroot] < num_elements[yroot]){
parent[xroot] = yroot;
num_elements[yroot] += num_elements[xroot];
}else{
parent[yroot] = xroot;
num_elements[xroot] += num_elements[yroot];
}
}
//Find root of an element. Uses path splitting for efficiency. (compresses path)
int root(int x){
while (parent[x] != x){
int prev = x;
x = parent[x];
parent[prev] = parent[x];
}
return x;
}
//Whether two elements are in the same set.
bool together(int x, int y){
return root(x) == root(y);
}
};
int main(){
fast_io();
int n, m;
cin >> n >> m;
DSU d(n+m);
forn(i, n){
forn(j, m){
char c;
cin >> c;
if(c=='#'){
d.join(i, n+j);
}
}
}
d.join(0, n);
d.join(n-1, n+m-1);
d.join(0, n-1);
int c = 0;
int nsing=0, msing=0;
forn(i, n+m){
if(d.parent[i] == i){
c++;
if(d.num_elements[i] == 1){
if(i<n) nsing++;
else msing++;
}
}
}
cout << c-max(nsing, msing)-1 << "\n";
}
|
#pragma clang optimize on
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <tuple>
#include <vector>
using namespace std;
using ll = long long;
#define shift 30
inline int get_digit() {
int 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;
}
int main() {
int R = get_digit(), C = get_digit();
int A[250000], B[250000];
int dist[500000], seen[500000], cost, pt, to_hide = R * C;
vector<ll> s;
s.reserve(2 * R * C);
priority_queue<ll, vector<ll>, greater<ll>> queue(s.begin(), s.end());
for(int i = 0; i < R * C; ++i)
if(i % C != C - 1) A[i] = get_digit();
for(int i = 0; i < (R - 1) * C; ++i) B[i] = get_digit();
dist[0] = -10000000;
queue.push((ll)(-10000000) << shift);
while(queue.size()) {
pt = queue.top() & ((1 << shift) - 1);
if(pt == to_hide - 1) break;
cost = queue.top() >> shift;
queue.pop();
seen[pt] = true;
if(pt < to_hide) {
if(!seen[pt + 1] && pt % C != C - 1) {
if(cost + A[pt] < dist[pt + 1]) {
dist[pt + 1] = cost + A[pt];
queue.push(((ll)dist[pt + 1] << shift) | (pt + 1));
}
}
if(!seen[pt - 1] && pt % C != 0) {
if(cost + A[pt - 1] < dist[pt - 1]) {
dist[pt - 1] = cost + A[pt - 1];
queue.push(((ll)dist[pt - 1] << shift) | (pt - 1));
}
}
if(!seen[pt + C] && pt < to_hide - C) {
if(cost + B[pt] < dist[pt + C]) {
dist[pt + C] = cost + B[pt];
queue.push(((ll)dist[pt + C] << shift) | (pt + C));
}
}
if(!seen[pt + to_hide] && cost + 1 < dist[pt + to_hide]) {
dist[pt + to_hide] = cost + 1;
queue.push(((ll)dist[pt + to_hide] << shift) | (pt + to_hide));
}
} else {
if(to_hide < pt - C && !seen[pt - C] && cost + 1 < dist[pt - C]) {
dist[pt - C] = cost + 1;
queue.push(((ll)(cost + 1) << shift) | (pt - C));
}
if(!seen[pt - to_hide] && cost < dist[pt - to_hide]) {
dist[pt - to_hide] = cost;
queue.push(((ll)cost << shift) | (pt - to_hide));
}
}
}
printf("%d\n", dist[to_hide - 1] - dist[0]);
} | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 555;
vector<int>G[22];
int ban[maxn];
int n, m;
set<int>se;
void dfs(int nowid, int sta) {
if (nowid == n + 1) {
se.insert(sta);
return;
}
if (ban[nowid]) {
dfs(nowid + 1, sta);
return;
}
//不选
dfs(nowid + 1, sta);
//选
for (int d : G[nowid])
ban[d]++;
dfs(nowid + 1, sta + (1 << nowid));
for (int d : G[nowid])
ban[d]--;
}
int del[22];
int vis[22];
//找有无奇环
int flag = 0;
int col[22];
void bfs(int nowid) {
memset(col, -1, sizeof(col));
col[nowid] = 0;
queue<int>q;
q.push(nowid);
while (!q.empty()) {
int nowid = q.front();
vis[nowid] = 1;
q.pop();
for (int d : G[nowid]) {
if (del[d])
continue;
if (col[d] == -1) {
col[d] = col[nowid] ^ 1;
q.push(d);
}
else if(col[d] == col[nowid]){
flag = 1;
return;
}
}
}
}
void solve() {
scanf("%d %d", &n, &m);
for (int i = 1; i <= m; i++) {
int a, b;
scanf("%d %d", &a, &b);
G[a].push_back(b);
G[b].push_back(a);
}
dfs(1, 0);
ll ans = 0;
ll cnt = 0;
//1号颜色
for (int sta : se) {
for (int i = 1; i <= n; i++) {
if ((sta >> i) & 1)
del[i] = 1;
else
del[i] = 0;
vis[i] = 0;
}
cnt = 1ll;
for (int i = 1; i <= n; i++) {
if (vis[i] || del[i])
continue;
flag = 0;
bfs(i);
if (flag == 1) {
cnt = 0;
break;
}
cnt *= 2ll;
}
ans += cnt;
}
cout << ans << endl;
}
signed main() {
// freopen("in.txt", "r", stdin);
solve();
}
|
#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 STRING(str) #str
#define ll long long
#define ld long double
template <typename T>
void print(T x, string name) {
cout << name+": " << x << endl;
}
template <typename T >
void print_vec_1d(vector<T> x, string name) {
cout << name << endl;
cout << "[ ";
for (int i=0;i<x.size();i++) {
cout << x[i];
if (i != x.size()-1) cout << ", ";
else cout << " ]" << endl;
}
}
template <typename T >
void print_vec_2d(vector<vector<T>> x, string name) {
cout << name << endl;
cout << "[ ";
for (int i=0;i<x.size();i++) {
cout << "[ ";
for (int j=0;j<x[i].size();j++) {
cout << x[i][j];
if (j != x[i].size()-1) cout << ", ";
else {
cout << " ]";
if (!((i == x.size()-1) && (j == x[i].size()-1))) {
cout << "," << endl;
}
}
}
}
cout << " ]" << endl;
}
template <typename T >
void print_vec_3d(vector<vector<vector<T>>> x, string name) {
cout << name << endl;
cout << "[ ";
for (int i=0;i<x.size();i++) {
cout << "[ ";
for (int j=0;j<x[i].size();j++) {
cout << "[ ";
for (int k=0;k<x[i][j].size();k++) {
cout << x[i][j][k];
if (k != x[i][j].size()-1) cout << ", ";
else {
cout << " ]";
if (!((i == x.size()-1) && (j == x[i].size()-1) && (k == x[i][j].size()-1))) {
cout << "," << endl;
}
}
}
if (j != x[i].size()-1) cout << ", ";
else {
cout << " ]";
if (!((i == x.size()-1) && (j == x[i].size()-1))) {
cout << "," << endl;
}
}
}
}
cout << " ]" << endl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
int n = (1<<N)-1;
vector<string> ans_str;
for (int i=0;i<n;i++) {
string tmp = "";
for (int j=0;j<(1<<N);j++) tmp += "B";
ans_str.push_back(tmp);
}
for (int i=1;i<(1<<N);i++) {
for (int j=0;j<(1<<N);j++) {
if (__builtin_popcount(j&i)%2 == 1) ans_str[i-1][j] = 'A';
}
}
cout << n << "\n";
for (int i=0;i<n;i++) {
cout << ans_str[i] << "\n";
}
} | #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;}
vector<string> rec(int n){
if(n==2) return vector<string> (1,"AB");
vector<string> base=rec(n/2);
vector<string> revbase=base;
rep(i,revbase.size()) rep(j,revbase[i].size()){
if(revbase[i][j]=='A') revbase[i][j]='B';
else revbase[i][j]='A';
}
vector<string> result;
rep(i,base.size()) result.push_back(base[i]+base[i]);
rep(i,base.size()) result.push_back(base[i]+revbase[i]);
result.push_back(string(n/2,'A')+string(n/2,'B'));
return result;
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin>>N;
int M=(1<<N);
vector<string> output=rec(M);
cout<<output.size()<<"\n";
rep(i,output.size()) cout<<output[i]<<"\n";
return 0;
}
|
#include<cstdio>
#include<iostream>
#include<vector>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
using namespace std;
// #define NDEBUG
#include<cassert>
namespace Elaina{
#define rep(i, l, r) for(int i=(l), i##_end_=(r); i<=i##_end_; ++i)
#define drep(i, l, r) for(int i=(l), i##_end_=(r); i>=i##_end_; --i)
#define fi first
#define se second
#define mp(a, b) make_pair(a, b)
#define Endl putchar('\n')
#define mmset(a, b) memset(a, b, sizeof a)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template<class T>inline T fab(T x){ return x<0? -x: x; }
template<class T>inline void getmin(T& x, const T rhs){ x=min(x, rhs); }
template<class T>inline void getmax(T& x, const T rhs){ x=max(x, rhs); }
template<class T>inline T readin(T x){
x=0; int f=0; char c;
while((c=getchar())<'0' || '9'<c) if(c=='-') f=1;
for(x=(c^48); '0'<=(c=getchar()) && c<='9'; x=(x<<1)+(x<<3)+(c^48));
return f? -x: x;
}
template<class T>inline void writc(T x, char s='\n'){
static int fwri_sta[1005], fwri_ed=0;
if(x<0) putchar('-'), x=-x;
do fwri_sta[++fwri_ed]=x%10, x/=10; while(x);
while(putchar(fwri_sta[fwri_ed--]^48), fwri_ed);
putchar(s);
}
}
using namespace Elaina;
ll n, N;
ll fib[100]; // fib[82]=99194853094755497
int cnt[100];
vector<pii>need;
vector<int>cmd;
signed main(){
fib[0]=fib[1]=1;
rep(i, 2, 82) fib[i]=fib[i-1]+fib[i-2];
N=n=readin(1ll);
drep(i, 82, 1) if(n>=fib[i]){
cnt[i]=n/fib[i];
n=n%fib[i];
}
int step=0;
rep(i, 1, 82) if(cnt[i]){
// printf("need (%d, %d)\n", i, cnt[i]);
need.push_back(mp(i, cnt[i]));
step=i;
}
ll x=0, y=0; int pre;
if(need.back().fi%2==0){
x=need.back().se, pre=3;
while(need.back().se)
cmd.push_back(1), --need.back().se;
}
else{
y=need.back().se, pre=4;
while(need.back().se)
cmd.push_back(2), --need.back().se;
}
// printf("init :> x == %lld, y == %lld\n", x, y);
need.pop_back();
rep(i, 1, step){
if(pre==4) cmd.push_back(pre=3), x=x+y;
else cmd.push_back(pre=4), y=x+y;
if(step-i==need.back().fi){
if(need.back().fi%2==0){
x+=need.back().se;
while(need.back().se)
cmd.push_back(1), --need.back().se;
}
else{
y+=need.back().se;
while(need.back().se)
cmd.push_back(2), --need.back().se;
}
need.pop_back();
}
}
// printf("x == %lld, y == %lld\n", x, y);
writc((int)cmd.size());
for(int i=0, siz=cmd.size(); i<siz; ++i)
writc(cmd[i]);
return 0;
} | #include <iostream>
#include <vector>
#include <algorithm>
#include <cinttypes>
static const int N = 200005;
int n;
static std::pair<int, int> a[N], b[N];
static int p[N];
static int f[N];
void add(int i, int d) {
for (++i; i < N; i += i & -i) {
f[i] += d;
}
}
int get(int i) {
int s = 0;
for (; i > 0; i -= i & -i) {
s += f[i];
}
return s;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i].first);
a[i].first += i;
a[i].second = i;
}
for (int i = 0; i < n; ++i) {
scanf("%d", &b[i].first);
b[i].first += i;
b[i].second = i;
}
std::sort(a, a + n);
std::sort(b, b + n);
for (int i = 0; i < n; ++i) {
if (a[i].first != b[i].first) {
printf("-1\n");
return 0;
}
p[a[i].second] = b[i].second;
}
long long res = 0;
for (int i = n - 1; i >= 0; --i) {
res += get(p[i]);
add(p[i], 1);
}
printf("%" PRId64 "\n", res);
}
|
#include<bits/stdc++.h>
#define pb push_back
#define ll long long
#define f first
#define s second
using namespace std ;
int main(){
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
int n ;
cin >> n ;
ll ans = 0 ;
int t = 0 ;
while( ans < n ) {
t ++ ;
ans = ans + t ;
}
cout << t << endl ;
} | #include<bits/stdc++.h>
using namespace std;
#define PB push_back
#define fo(i0, n) for(i=0;i<n;i++)
#define fo(i1, n) for(i=1;i<=n;i++)
typedef long long ll;
typedef unsigned long long ull;
ull GCD(ull x,ull y){if(y==0) return x;else return GCD(y,x%y);}
ull LCM(ull a,ull b){return (a*b/(GCD(a,b)));}
bool PRIME(int p){if (p<=1)return false;if (p<=3)return true;if(p%2==0||p%3==0) return false;for(int i=5;p>=i*i;i=i+6){if(p%i==0||p%(i+2)==0) return false;}return true;}
int main()
{
ll a,b,c,d,e,t,i,j,k,l,sum=0,ans=0,x,y,z=-1,m,n;
string S,T,U;
cin>>a;
unordered_set<ll>A;
for(i=2;i*i<=a;i++)
{
b=i*i;
while(b<=a)
{
A.insert(b);
b*=i;
}
}
cout<<a-A.size()<<endl;
return 0;
}
|
// Problem: C - Squared Error
// Contest: AtCoder - AtCoder Beginner Contest 194
// URL: https://atcoder.jp/contests/abc194/tasks/abc194_c
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// Date: 2021-03-09 12:52:00
// --------by Herio--------
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=3e5+5,M=2e4+5,inf=0x3f3f3f3f,mod=1e9+7;
#define mst(a,b) memset(a,b,sizeof a)
#define PII pair<int,int>
#define fi first
#define se second
#define pb emplace_back
#define SZ(a) (int)a.size()
#define IOS ios::sync_with_stdio(false),cin.tie(0)
void Print(int *a,int n){
for(int i=1;i<n;i++)
printf("%d ",a[i]);
printf("%d\n",a[n]);
}
ll a[N],b[N],c[N];
int main(){
int n;cin>>n;
for(int i=1;i<=n;i++) cin>>a[i],b[i]=a[i]+b[i-1],c[i]=c[i-1]+a[i]*a[i];
ll s=0;
for(int i=2;i<=n;i++){
s+=(i-1)*a[i]*a[i]+c[i-1]-2*a[i]*b[i-1];
}cout<<s<<'\n';
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#ifdef NO_PROBLEM_MAIN
#define DPRINTF(x) printf x
#else
static inline void nop() { return; }
#define DPRINTF(x) nop()
#endif
#define SOLVER(x) solve_D x
static void s_shuffle( string &s, int r )
{
int l = s.length();
for ( int i = 0; i < r; i++ )
{
int x, y;
x = random() % l;
y = random() % l;
swap( s[x], s[y] );
}
}
static void s_solve(void)
{
int si, sj, ti, tj;
int x, y;
string s;
srandom(19750220);
for ( int i = 0; i < 1000; i++ )
{
cin >> si >> sj >> ti >> tj;
x = tj - sj;
y = ti - si;
s = "";
if ( x > 0 )
{
for ( int j = 0; j < x; j++ )
{
s += "R";
}
}
else
{
for ( int j = x; j < 0; j++ )
{
s += "L";
}
}
if ( y > 0 )
{
for ( int j = 0; j < y; j++ )
{
s += "D";
}
}
else
{
for ( int j = y; j < 0; j++ )
{
s += "U";
}
}
s_shuffle( s, 100 );
cout << s << endl;
fflush(stdout);
long long b;
cin >> b;
}
}
void SOLVER((volatile bool *do_loop))
{
do {
s_solve();
} while ((do_loop != NULL) && (*do_loop));
}
#ifndef NO_PROBLEM_MAIN
int main(void)
{
SOLVER((NULL));
return 0;
}
#endif
|
#include<bits/stdc++.h>
using namespace std;
int main() {
long long n,w,count=0;
cin>>n>>w;
long long a=w;
for(int i=0;i<n;i++){
if(n>=a){
count++;
}else if(n<a){
break;
}
a+=w;
}
cout<<count<<endl;
}
| #include <iostream>
using namespace std;
int main()
{
int a,b,sum;
cin>>a>>b;
sum=a/b;
cout <<sum;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
#define x first
#define y second
const int N = 2e5 + 10;
int a[N], res[N], pos[N];
void Swap(int p, int &idx) {
swap(pos[a[p]], pos[a[p + 1]]);
swap(a[p], a[p + 1]);
res[idx++] = p;
}
void solve() {
int n;
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
pos[a[i]] = i;
}
int idx = 1;
for(int i = n; i >= 1; i--) {
if(a[i] == i) continue;
while(idx % 2 != pos[i] % 2) {
if(idx & 1) Swap(1, idx);
else Swap(2, idx);
}
while(pos[i] != i) {
Swap(pos[i], idx);
}
}
printf("%d\n", idx - 1);
for(int i = 1; i < idx; i++) {
printf("%d ", res[i]);
}
puts("");
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE
int t; cin >> t; while(t--)
solve();
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
# define ll long long
# define read read1<ll>()
# define Type template<typename T>
Type T read1(){
T t=0;
char k;
bool vis=0;
do (k=getchar())=='-'&&(vis=1);while('0'>k||k>'9');
while('0'<=k&&k<='9')t=(t<<3)+(t<<1)+(k^'0'),k=getchar();
return vis?-t:t;
}
# define fre(k) freopen(k".in","r",stdin);freopen(k".out","w",stdout)
int s,a[505],b[505],pr[505*505];bool ty;
void Swap(int x,int y){b[a[x]]=y;b[a[y]]=x;swap(a[x],a[y]);pr[++*pr]=x;ty^=1;}
void Swap2(int x){Swap(x-1,x);Swap(x,x+1);Swap(x-1,x);Swap(x,x+1),Swap(x-1,x);}
int main(){
for(int T=read;T--;){
s=read;*pr=0;ty=1;
for(int i=1;i<=s;++i)b[a[i]=read]=i;
for(int i=1;i<s-1;++i){
if(b[i]==i)continue;
if((b[i]^ty)&1)
for(int j=b[i]-1;j>=i;--j)Swap(j,j+1);
else{
int w=b[i]-1;
if(w!=i)Swap(w-1,w);
else if(w==s-3){
Swap(w+1,w+2);
Swap(w+2,w+3);
Swap(w+1,w+2);
w=s-1;
}else if(w==s-2){Swap2(i);w=i-1;}
else Swap(w+3,w+4);
for(int j=w;j>=i;--j)Swap(j,j+1);
}
}if(a[s-1]>a[s])
if((b[s-1]^ty)&1)Swap(b[s-1]-1,b[s-1]);
else Swap2(s-1);
printf("%d\n",*pr);
for(int i=1;i<=*pr;++i)printf("%d ",pr[i]);
putchar('\n');
}
return 0;
}/*
x
1 3 2 5 4
a c b
c a b
c b a
b c a
b a c
a b c
*/ |
#include <bits/stdc++.h> // clang-format off
using namespace std;
using i64 = int64_t;
using pii = pair<i64, i64>;
#define rep(i, n) for (i64 i = 0; i < i64(n); i++)
#define per(i, n) for (i64 i = n - 1; i >= 0; i--)
#define REP(i, a, b) for (i64 i = a; i < i64(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fi first
#define se second
#ifdef LOCAL
#define dbg(...) cerr<<__LINE__<<" ["<<#__VA_ARGS__<<"]:",debug(__VA_ARGS__)
#else
#define dbg(...)
#endif
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) {
os << '(' << pa.first << ',' << pa.second << ')'; return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
for (auto& x : vec) { os << x << ' '; } return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &mat) {
os << endl; for (auto& vec : mat) { os << vec << endl; } return os;
}
void debug() { cerr << endl; }
template <typename Head, typename... Tail> void debug(Head H, Tail... T) {
cerr << " " << H; debug(T...);
}
template <typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return 1; } return 0; }
template <typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int INF = (1 << 30) - 1;
const i64 LINF = (1LL << 60) - 1;
// clang-format on
void solve() {
i64 n, m;
cin >> n >> m;
vector<i64> W(n);
rep(i, n) cin >> W[i];
vector<i64> L(m), V(m);
rep(i, m) cin >> L[i] >> V[i];
i64 ma = 0;
rep(i, n) chmax(ma, W[i]);
rep(i, m) if (ma > V[i]) {
cout << -1 << endl;
return;
}
vector<pii> bridges(m);
rep(i, m) {
bridges[i] = {V[i], L[i]};
}
bridges.push_back({0, 0});
sort(all(bridges));
rep(i, m) {
chmax(bridges[i + 1].se, bridges[i].se);
}
vector<int> perm(n);
iota(all(perm), 0);
i64 ans = LINF;
do {
vector<vector<i64>> E(n, vector<i64>(n));
rep(i, n) {
i64 weight = 0;
weight = W[perm[i]];
REP(j, i + 1, n) {
weight += W[perm[j]];
int idx = lower_bound(all(bridges), (pii){weight, -LINF}) - bridges.begin();
if (idx == 0)
continue;
else
E[i][j] = bridges[idx - 1].se;
}
}
vector<i64> dist(n);
rep(i, n) REP(j, i + 1, n) {
chmax(dist[j], dist[i] + E[i][j]);
}
chmin(ans, dist.back());
} while (next_permutation(all(perm)));
cout << ans << endl;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
solve();
return 0;
}
| #include <bits/stdc++.h>
#define rep(i,m,n) for(int i=m;i<n;i++)
#define co(n) cout<<n<<endl
using namespace std;
int main(){
int n,x=0,y=0,ans=100000,c=0;
cin>>n>>x>>y;
y*=n;
rep(i,0,n-1){
cin>>ans;
c+=ans;
}
if(y-c<=x){
if(y-c<0) co(0);
else co(y-c);
}
else co(-1);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define rng(x) x.begin(), x.end()
#define maxi(x, y) x = max(x, (y))
#define mini(x, y) x = min(x, (y))
#define pb push_back
#define F first
#define S second
#define el '\n'
#define ll long long
#define SZ(x) ((int)(x).size())
template<typename T>
istream&operator>>(istream&is,vector<T>&v){for(auto&it:v)is>>it;return is;}
template<class L, class R> ostream& operator<<(ostream &os, pair<L,R> P) {
return os << "(" << P.F << "," << P.S << ")"; }
template<class T> ostream& operator<<(ostream &os, vector<T> V) {
os << "[ "; for(auto v : V) os << v << " "; return os << "]"; }
template<class T> ostream& operator<<(ostream &os, set<T> S){
os << "{ "; for(auto s:S) os<<s<<" "; return os<<"}"; }
#define db(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cout<<name<<" : "<<arg1<<'\n';}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cout.write(names,comma-names)<<" : "<<arg1<<" |";__f(comma+1, args...);}
typedef pair<int,int> pi; typedef vector<int> vi; typedef vector<vi> vvi;
/*-----------------------------Code begins----------------------------------*/
class BIT{
public :
int n;
vi f;
BIT(int n){
this->n = n;
f.assign(n, 0);
}
int get(int pos){
int res = 0;
for (; pos >= 0; pos = (pos & (pos + 1)) - 1){
res += f[pos];
}
return res;
}
int get(int l, int r){
return get(r) - (l ? get(l - 1) : 0);
}
void increase(int pos, int val){
for (; pos < int(f.size()); pos = (pos | (pos + 1))) {
f[pos] += val;
}
}
};
void solve(){
int n, q; cin>>n>>q;
vector <BIT> bit(30, BIT(n));
for(int i = 0; i < n; ++i){
int val; cin>>val;
for(int b = 0; b < 30; ++b){
bit[b].increase(i, val >> b & 1);
}
}
while(q--){
int op; cin>>op;
if(op == 1){
int i, val; cin>>i>>val;
i--;
for(int b = 0; b < 30; ++b){
bit[b].increase(i, val >> b & 1);
}
}
else{
int l, r; cin>>l>>r;
l--, r--;
int ans = 0;
for(int b = 0; b < 30; ++b){
ans += (bit[b].get(l, r) % 2) * (1 << b);
}
cout<<ans<<el;
}
}
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int T = 1;
// cin>>T;
while(T--){
solve();
}
return 0;
} | #include <bits/stdc++.h>
#include <cmath>
#include <vector>
#include <numeric>
using namespace std;
using ll= long long;
#define rep(i,n) for (int i=0; i<(n); ++i)
int main(void)
{
int A;
int B;
int count=0;
cin >> A >>B;
vector<int> E;
E = vector<int>(A+B);
vector<int> E_A;
E_A = vector<int>(A);
vector<int> E_B;
E_B = vector<int>(B);
if(A==B){
rep(i,A){
E_A[i]=i+1;
E_B[i]=E_A[i]*(-1);
}
}
else if(A>B){
rep(i,A){
E_A[i]=i+1;
}
rep(i,B){
if(i<B-1){
E_B[i]=(i+1)*(-1);
}
else{
E_B[i]=(std::accumulate(E_A.begin(), E_A.end(), 0)+std::accumulate(E_B.begin(), E_B.end(), 0))*(-1);
}
}
}
else{
rep(i,B){
E_B[i]=(i+1)*-1;
}
rep(i,A){
if(i<A-1){
E_A[i]=(i+1);
}
else{
E_A[i]=(std::accumulate(E_A.begin(), E_A.end(), 0)+std::accumulate(E_B.begin(), E_B.end(), 0))*(-1);
}
}
}
rep(i,A+B){
if(i<A) E[i] = E_A[i];
else E[i] = E_B[i-A];
}
rep(i,A+B-1) cout << E[i]<< " ";
cout << E.back() << endl;
return 0;
} |
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring>
#include <iostream>
#include <sstream>
#include <numeric>
#include <queue>
#include <vector>
using namespace std;
int64_t solve(int64_t L, int64_t R) {
vector<int64_t> cnt(R + 1);
for (int64_t g = R; g >= 2; --g) {
cnt[g] = (R / g - (L - 1) / g) * (R / g - (L - 1) / g);
for (int64_t x = g * 2; x <= R; x += g) {
cnt[g] -= cnt[x];
}
}
int64_t ans = accumulate(cnt.begin(), cnt.end(), 0LL);
for (int g = max(int64_t(2), L); g <= R; ++g) {
ans -= 2 * (R / g - (L - 1) / g) - 1;
}
return ans;
}
int main() {
int64_t L, R;
std::cin >> L >> R;
cout << solve(L, R) << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef pair<int,int> P ;
#define rep(i,n) for(int i = 0 ; i < n ; i++)
#define rrep(i,a,b) for(int i = a ; i < b ; i++)
int l , r ;
int A[1000007] ;
int ans[1000007] ;
int prime_factor[1000007] ;
void ertstns(){
memset(prime_factor,-1,sizeof(prime_factor)) ;
prime_factor[0] = prime_factor[1] = 1 ;
for(int i = 2 ; i < 1000007 ; i++){
if(prime_factor[i] == -1){
for(int j = i ; j < 1000007 ; j += i) prime_factor[j] = i ;
}
}
}
int main(){
ertstns() ;
cin >> l >> r ;
rrep(i,max(l,2),r+1){
int k = i ;
vector<int> vec ;
map<int,bool> use ;
while(k != 1){
if(!use[prime_factor[k]]) vec.push_back(prime_factor[k]) ;
use[prime_factor[k]] = true ;
k /= prime_factor[k] ;
}
int t = vec.size() ;
for(int S = 1 ; S < 1 << t ; S++){
ll val = 1 ;
for(int bit = 0 ; bit < t ; bit++){
if(S >> bit & 1) val *= vec[bit] ;
}
ans[val]++ ;
}
}
ll res = 0 , mns = 0 ;
for(int i = max(l,2) ; i <= r ; i++){
int k = i ;
vector<int> vec ;
map<int,bool> use ;
while(k != 1){
if(!use[prime_factor[k]]) vec.push_back(prime_factor[k]) ;
use[prime_factor[k]] = true ;
k /= prime_factor[k] ;
}
int t = vec.size() ;
for(int S = 1 ; S < 1 << t ; S++){
ll count = 0 , val = 1 ;
for(int j = 0 ; j < t ; j++) if(S >> j & 1){
count++ ;
val *= vec[j] ;
}
if(count % 2 == 0) res -= ans[val] ;
else res += ans[val] ;
}
if(i * 2 <= r) mns += 2 * (r / i - 1) ;
mns++ ;
}
cout << res - mns << endl ;
} |
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, j, n) for (int i = j; i < (n); ++i)
#define rrep(i, j, n) for (int i = (n)-1; j <= i; --i)
int main() {
cin.tie(0)->sync_with_stdio(0);
int t; cin >> t;
while (t--) {
int n; cin >> n;
vector<ll> a(n);
rep(i, 0, n) cin >> a[i];
// x := 全部出した瞬間の全ての皿のxor
// nが偶数
// 先行: 全部出したとき先行 x!=0を目指す
// 後攻: x=0
// この場合は先行がでかいほうからとっていき同じ山に積むと、すべて同じでない限りx!=0になる。
// 厳密には偶数indexの和>奇数indexの和となれば先行の勝ち
// nが奇数
// 先行: 全部出したとき後攻 x=0を目指す
// 後攻: 全部出したとき先行 x!=0を目指す
// 後攻が勝てそう. 先行が初手で何を選んでも、後攻はそれに一番でかいのを乗せれば最大の山が構成できる?
if (n & 1)cout << "Second";
else {
sort(a.rbegin(), a.rend());
ll even = 0, odd = 0;
for (int i = 0; i < n; ++i) {
if (i & 1) odd += a[i];
else even += a[i];
}
if (even > odd) cout << "First";
else cout << "Second";
}
cout << '\n';
}
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
int T, N;
int A[100010];
signed main()
{
cin >> T;
while(T--)
{
cin >> N;
rep(i, N)cin >> A[i];
if(N % 2) cout << "Second" << endl;
else
{
map<int, int> mp;
rep(i, N)mp[A[i]]++;
bool is_first = false;
for(auto e : mp)
{
if(e.second % 2 == 1)
{
is_first = true;
}
}
if(is_first)cout << "First" << endl;
else cout << "Second" << endl;
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll pow_m(ll a ,ll b,ll m){
ll ans = 1;
while(b){
if(b&1) ans = ans * a %m;
a = a *a % m;
b >>= 1;
}
return ans;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
ll a,b,c;
cin >> a >> b >> c;
ll k = pow_m(b,c,4) ? pow_m(b,c,4) : 4;
cout << pow_m(a,k,10);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using vvvll = vector<vector<vector<ll>>>;
using vpll = vector<pair<ll, ll>>;
#define rep(i,n) for (ll i = 0; i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
#define pb push_back
#define endl '\n'
#define IOS ios::sync_with_stdio(false); cin.tie(nullptr);
const int inf = 1<<30;
const ll INF = 1ll<<60;
const ll mod = 1000000007;
//const ll mod = 998244353;
//const ll dx[4] = {1, 0, -1, 0};
//const ll dy[4] = {0, 1, 0, -1};
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<class T> void disp1(vector<T> &array, bool blank=1) {
rep(i, array.size()) cout << ((i&&blank) ? " " : "") << array[i];
cout << endl;
//cout << " ";
}
template<class T> void disp2(vector<vector<T>> &array, bool blank=1) {
rep(i, array.size()) disp1(array[i], blank);
}
ll rem(ll a, ll b) { return (a % b + b) % b; } // 余りを0~b-1に丸める
// 小数の入力が文字で与えられたとき、10^n倍したものを返す
ll x10(string str_f, ll n) {
if (str_f[0] == '-') return -x10(str_f.substr(1), n);
ll times = 1; rep(i,n) times *= 10; // times = 10^n
int id = -1; rep(i,str_f.size()) if (str_f[i] == '.') id = i;
if (id == -1) { return stoll(str_f)*times; }
string str_int = str_f.substr(0, id), str_dec = str_f.substr(id+1);
while (str_dec.size() < n) str_dec += '0';
return stoll(str_int)*times + stoll(str_dec);
}
// A^N (mod)
ll modpow(ll A, ll N, ll mod) {
ll RES = 1;
while (N) {
if (N & 1) RES = RES * A % mod;
A = A * A % mod;
N >>= 1;
}
return RES;
}
//using mint = modint1000000007;
int main() {
ll a, b, c; cin >> a >> b >> c;
a %= 10;
ll p = a;
vll seen(10, 0), aa;
while (1) {
if (seen[a]) break;
aa.pb(a);
seen[a] = 1;
a = a*p%10;
}
ll ans = aa[(modpow(b, c, sz(aa))+sz(aa)-1)%sz(aa)];
cout << ans << endl;
return 0;
} |
#include<cstdio>
int abs(int u){
return u>0?u:-u;
}
int getDis(int a,int b,int c,int d){
if(a==c && b==d) return 0;
else if((a+b)==(c+d) || (a-b)==(c-d) || abs(a-c)+abs(b-d)<=3) return 1;
else{
int x = (a+b)%2, y = (c+d)%2, z = (a-b)%2, w = (c-d)%2;
if(z<0) z += 2;
if(w<0) w += 2;
if(x==y || z==w) return 2;
return 3;
}
}
int main(){
int a,b,c,d;
scanf("%d%d%d%d",&a,&b,&c,&d);
if(a==c && b==d) printf("0\n");
else if((a+b)==(c+d) || (a-b)==(c-d) || abs(a-c)+abs(b-d)<=3) printf("1\n");
else{
int ans = getDis(a,b,c,d);
for(int i = -2; i <= 2; i++){
for(int j = -2; j <= 2; j++){
for(int k = -2; k <= 2; k++){
for(int l = -2; l <= 2; l++){
int cur = getDis(a+i,b+j,c+k,d+l);
if(i!=0 || j!=0) cur++;
if(k!=0 || l!=0) cur++;
if(ans>cur) ans = cur;
}
}
}
}
printf("%d\n",ans);
}
return 0;
}
| #include<iostream>
#include<array>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
#include<cassert>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
template<class T>bool chmax(T &a, const T &b) {if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a, const T &b) {if(b<a){a=b;return 1;}return 0;}
int r1,c1,r2,c2;
bool ok(int x,int y){
if(abs(x)+abs(y)<=3) return true;
if(x+y==0) return true;
if(x-y==0) return true;
return false;
}
void solve(){
cin >> r1 >> c1 >> r2 >> c2;
if(r1==r2 && c1==c2){
cout << 0 << endl;
return;
}
if(r1+c1==r2+c2){
cout << 1 << endl;
return;
}
if(r1-c1==r2-c2){
cout << 1 << endl;
return;
}
if(abs(r1-r2)+abs(c1-c2)<=3){
cout << 1 << endl;
return;
}
int x=abs(r1-r2),y=abs(c1-c2);
if((x+y)%2==0){
cout << 2 << endl;
return;
}
Rep(i,-2,3){
Rep(j,-2,3){
//cout << x+i << " " << y+j << endl;
if(ok(x+i,y+j)){
cout << 2 << endl;
return;
}
}
}
cout << 3 << endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(50);
solve();
} |
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define ll long long
int a[200010], b[200010];
ll d[200010];
int main() {
int n;
scanf("%d", &n);
for(int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
}
for(int i = 0; i < n; ++i) {
scanf("%d", &b[i]);
}
int maxa = a[0];
d[0] = 1ll*maxa*b[0];
printf("%lld\n", d[0]);
for(int i = 1; i < n; ++i) {
maxa = std::max(maxa, a[i]);
d[i] = std::max(d[i-1], 1ll*b[i]*maxa);
printf("%lld\n", d[i]);
}
return 0;
} | // Created by Pratik
// NIT PATNA
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define M 1000000007
#define pi 3.14159265358979323846
#define ll long long
#define lld long double
// Pair
#define pii pair<int, int>
#define pll pair<ll, ll>
// Vector
#define vl vector<ll >
#define vi vector<int>
#define vpi vector<pii>
#define vpl vector<pll>
#define pb push_back
#define mp make_pair
// Search
#define lb lower_bound
#define ub upper_bound
#define mina *min_element
#define mama *max_element
#define bsrch binary_search
////////
#define F first
#define S second
#define cel(x,a) ((x + a - 1) / a)
#define all(v) v.begin(), v.end()
#define allrev(v) v.rbegin(), v.rend()
#define lcm(m, n) ((m / __gcd(m, n))*n)
#define deb(...) cerr << "(" << #__VA_ARGS__ << "):", dbg(__VA_ARGS__)
#define ms(arr, v) memset(arr, v, sizeof(arr))
#define ps(x,y) fixed << setprecision(y) << x
#define _X_ ios_base::sync_with_stdio(false); cin.tie(NULL)
//pbds
#define ordered tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// Input / Output
#define scn(str) scanf("%s", str)
#define pri(str) printf("%s\n", str)
const int N = 5e5 + 4;
inline void dbg() { cerr << endl; }
template <typename T, typename... V>
inline void dbg(T t, V... v) {cerr << ' ' << t; dbg(v...);}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool good(int arr[],int n)
{
for(int i = 1; i<n; i++)if(arr[i]<arr[i-1])return false;
return true;
}
void solve()
{
int n;
cin >> n;
int arr[n];
for(int i = 0; i<n; i++)cin >> arr[i];
vi ans;
bool turn = 0;
while(1)
{
if(good(arr,n))break;
bool used = 0;
for(int i = n-2; i>=0; i--)
{
if(i%2==turn && arr[i]>arr[i+1])
{
ans.pb(i+1);
swap(arr[i],arr[i+1]);
used = 1;
break;
}
}
if(!used)
{
swap(arr[turn],arr[turn+1]);
ans.pb(turn+1);
}
turn = !turn;
//deb(turn);
}
int k = ans.size();
assert(k<=n*n);
cout << ans.size() << "\n";
for(int &i:ans)cout << i << " ";
cout << "\n";
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0);
// clock_t time_req = clock();
int t = 1;
cin>> t;
while (t--) {
solve();
}
// time_req = clock() - time_req;
// cout << (float)time_req / CLOCKS_PER_SEC;
return 0;
}
|
//#include <bits/stdc++.h>
//using namespace std;
#include <set>
#include <cstdio>
#include <iostream>
using namespace std;
#define rep(i,_l,_r) for(signed i=(_l),_end=(_r);i<=_end;++i)
#define fep(i,_l,_r) for(signed i=(_l),_end=(_r);i>=_end;--i)
#define erep(i,u) for(signed i=head[u],v=to[i];i;i=nxt[i],v=to[i])
#define efep(i,u) for(signed i=Head[u],v=to[i];i;i=nxt[i],v=to[i])
#define print(x,y) write(x),putchar(y)
//#define debug(...) do {cerr<<__LINE__<<" : ("#__VA_ARGS__<<") = "; Out(__VA_ARGS__); cerr<<flush;} while(0)
//template <typename T> void Out(T x) {cerr<<x<<"\n";}
//template <typename T,typename ...I> void Out(T x,I ...NEXT) {cerr<<x<<", "; Out(NEXT...);}
template <class T> inline T read(const T sample) {
T x=0; int f=1; char s;
while((s=getchar())>'9'||s<'0') if(s=='-') f=-1;
while(s>='0'&&s<='9') x=(x<<1)+(x<<3)+(s^48),s=getchar();
return x*f;
}
template <class T> inline void write(const T x) {
if(x<0) return (void) (putchar('-'),write(-x));
if(x>9) write(x/10);
putchar(x%10^48);
}
template <class T> inline T Max(const T x,const T y) {if(x>y) return x; return y;}
template <class T> inline T Min(const T x,const T y) {if(x<y) return x; return y;}
template <class T> inline T fab(const T x) {return x>0?x:-x;}
template <class T> inline T gcd(const T x,const T y) {return y?gcd(y,x%y):x;}
template <class T> inline T lcm(const T x,const T y) {return x/gcd(x,y)*y;}
template <class T> inline T Swap(T &x,T &y) {x^=y^=x^=y;}
typedef pair <int,int> pii;
const int maxn=2e5+5;
set <pii> s;
int n;
long long c[maxn];
set <pii> :: iterator it;
int lowbit(int x) {
return x&-x;
}
void add(int x,int k) {
while(x<=n) c[x]+=k,x+=lowbit(x);
}
long long ask(int x) {
long long r=0;
while(x) r+=c[x],x-=lowbit(x);
return r;
}
int main() {
n=read(9); long long ans=0;
rep(i,1,n) s.insert(make_pair(read(9)+i,i));
rep(i,1,n) {
int x=read(9); it=s.lower_bound(make_pair(x+i,0));
if(it==s.end() || (it->first)!=x+i) return puts("-1"),0;
long long tmp=ask(it->second);
add(1,1),add(it->second,-1);
ans+=tmp+(it->second)-i;
if(tmp+(it->second)-i<0) puts("UN");
s.erase(it);
}
print(ans,'\n');
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
int bit[300010];
int N;
void add(int a, int b){
int x = a;
while(x <= N){
bit[x] += b; x += x & -x;
}
}
int sum(int a){
int res = 0;
int x = a;
while(x > 0){
res += bit[x]; x -= x & -x;
}
return res;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> N;
int A[200010]; int B[200010];
int C[200010]; int D[200010];
set<int> st;
map<int, int> mp;
for(int i = 0; i < N; i++){
cin >> A[i]; A[i] += i; C[i] = A[i];
st.insert(A[i]);
}
for(int i = 0; i < N; i++){
cin >> B[i]; B[i] += i; D[i] = B[i];
}
sort(C, C+N); sort(D, D+N);
for(int i = 0; i < N; i++){
if(C[i] != D[i]){
cout << -1 << "\n"; return 0;
}
}
long long ans = 0; int idx = 1;
for(auto it = st.begin(); it != st.end(); it++){
mp[*it] = idx; idx ++;
}
for(int i = 0; i < N; i++) A[i] = mp[A[i]];
for(int i = 0; i < N; i++) B[i] = mp[B[i]];
vector<int> E[200010];
for(int i = 0; i < N; i++){
E[B[i]].push_back(i);
}
int itr[200010] = {};
int F[200010] = {}; int G[200010] = {};
for(int i = 0; i < N; i++){
F[i] = E[A[i]].at(itr[A[i]]); itr[A[i]] ++;
G[F[i]] = i;
}
// cout << G[0] << G[1] << G[2] << endl;
for(int i = 0; i < N; i++){
ans += (long long) sum(G[i] + 1);
add(G[i] + 1, 1);
}
cout << (long long) N * (N - 1) / 2 - ans << "\n";
} |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
typedef long long LL;
int a, b, c;
double f[105][105][105];
double dp(int nowa, int nowb, int nowc) {
if (nowa >= 100 || nowb >= 100 || nowc >= 100) return 0;
if (f[nowa][nowb][nowc]) return f[nowa][nowb][nowc];
//cout << 1 << endl;
int sum = nowa + nowb + nowc;
return f[nowa][nowb][nowc] =
1.0 + (1.0 * nowa / sum * dp(nowa + 1, nowb, nowc) +
1.0 * nowb / sum * dp(nowa, nowb + 1, nowc) +
1.0 * nowc / sum * dp(nowa, nowb, nowc + 1));
}
int main() {
cin >> a >> b >> c;
printf("%.10lf",dp(a, b, c));
return 0;
} | #include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <vector>
#include <map>
#include <queue>
#include <limits.h>
#include <stack>
using namespace std;
typedef long long ll;
const int maxn = 110;
int H, W, A, B, ans = 0;
int nx, ny;
bool vis[20][20];
void solve(int s, int x, int y, int a, int b)
{
if(s==H*W)
{
ans++;
return ;
}
int nx = x+1, ny = y;
if(x==W) nx = 1, ny++;
if(vis[x][y]) return solve(s+1, nx, ny, a, b);
if(a)
{
if(x!=W && !vis[nx][ny])
{
vis[x][y] = vis[nx][ny] = true;
solve(s+1, nx, ny, a-1, b);
vis[x][y] = vis[nx][ny] = false;
}
if(y<H)
{
vis[x][y] = vis[x][y+1] = true;
solve(s+1, nx, ny, a-1, b);
vis[x][y] = vis[x][y+1] = false;
}
}
if(b)
{
vis[x][y] = true;
solve(s+1, nx, ny, a, b-1);
vis[x][y] = false;
}
}
int main()
{
//freopen("/Users/zhangkanqi/Desktop/11.txt", "r", stdin);
cin >> H >> W >> A >> B;
solve(0, 1, 1, A, B);
cout << ans << endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
cout<<n/m;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(long long(i)=0;(i)<(n);(i)++)
#define kiriage(a,b) ((a)+(b)-1)/(b)
int main(){
long long s,p;
cin >> s >> p;
for(long long i = 1; i * i <= p; i++){
if(p % i == 0){
if(i + p / i == s){
cout << "Yes" << endl;
return 0;
}
}
}
cout << "No" << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define vi vector<int>
// #define vb vector<bool>
// #define vc vector<char>
#define vii vector<pii>
//#define mi map<int,int>
//#define mii map<pii,int>
#define all(a) (a).begin(),(a).end()
#define F first
#define S second
#define sz(x) (int)x.size()
// #define ub(x) upper_bound(x)
// #define lb(x) lower_bound(x)
#define endl '\n'
#define file freopen("b1.in","r",stdin);freopen("output.txt","w",stdout)
#define fastio ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
// #define N 1e5+5
#define M 1000000007
#define MAX 50000
#define mem0(a) memset(a,0,sizeof(a))
#define prec cout<<std::setprecision(9)<<std::fixed
#define forn(i,a,b) for(int i=a;i<b;i++)
#define pi 3.14159265358979323
struct cmp {
bool operator() (pii a,pii b){
return ((a.F-a.F/2)*a.S)<((b.F-b.F/2)*b.S);
}
};
// priority_queue<pii,vector<pii>,cmp> q;
const int N=1e5+1;
int n,m,k;
vector<int> check(int n, vector<int> a, int x)
{
multiset<int> s;
for (auto e : a)
s.insert(e);
vector<int> res;
for (int i = 0; i < n; i++)
{
auto it1 = s.end();
it1--;
int y = x - *it1;
s.erase(it1);
auto it2 = s.find(y);
if (it2 == s.end())
return {};
res.push_back(x - y);
res.push_back(y);
x = max(x - y, y);
s.erase(it2);
}
return res;
}
void Anon_mouS(){
string s;
cin>>s;
// cout<<s[0]<<" "<<s[1]<<" "<<s[2]<<endl;
if(s[0]==s[1] and s[1]==s[2])cout<<"Won"<<endl;
else cout<<"Lost"<<endl;
}
int32_t main(){
fastio;
// func();
int q=1;
// sieve();
// cin>>q;
for(int it=1;it<=q;it++){
// cout<<"Case #"<<it<<": ";
Anon_mouS();
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
string s;
cin>>s;
if((s[0]==s[1])&&(s[1]==s[2])){
cout<<"Won\n";
}else{
cout<<"Lost\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
float satisfaction(float x, float y, float a,
float b, float c, float d, float r){
if(a <= x+0.5 && x+0.5 <= c && b <= y+0.5 && y+0.5 <= d){
float s = (c-a)*(d-b);
return 1 - std::pow(1-min(r,s)/max(r,s), 2.0);
}else{
return 0;
}
}
bool check(float a, float b, float c, float d, float r){
float s = (c-a)*(d-b);
return s == r;
}
int main(){
int n;
cin >> n;
vector<float> x(n), y(n), r(n), a(n), b(n), c(n), d(n);
for (int i=0;i<n;i++){
cin >> x.at(i) >> y.at(i) >> r.at(i);
a.at(i) = x.at(i);
b.at(i) = y.at(i);
c.at(i) = x.at(i)+1;
d.at(i) = y.at(i)+1;
}
/*float sum_p = 0;
for (int i=0;i<n;i++){
sum_p = sum_p +
satisfaction(x.at(i),y.at(i),
a.at(i),b.at(i),
c.at(i),d.at(i),r.at(i));
}
cout << sum_p << endl;*/
for (int i=0;i<n;i++){
cout << a.at(i) << " ";
cout << b.at(i) << " ";
cout << c.at(i) << " ";
cout << d.at(i) << endl;
}
} | /**
* author: shu8Cream
* created: 06.03.2021 11:57:11
**/
#include <bits/stdc++.h>
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>;
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<tuple<int,int,int>> xyr(n);
vector<tuple<int,int,int,int,int>> ans(n);
rep(i,n){
int x,y,r;
cin >> x >> y >> r;
xyr[i]={x,y,i};
}
int prex = 0, prey = 0, prer = 0;
sort(all(xyr));
rep(i,n){
int x,y,r;
tie(x,y,r)=xyr[i];
}
rep(i,n){
int x,y,r;
tie(x,y,r)=xyr[i];
if(prex-1==x){
ans[i-1]={prer,x,prey,x+1,prey+1};
ans[i]={r,x,y,x+1,y+1};
}else{
ans[i]={r,prex, 0, x+1, 10000};
}
prex=x+1;
prey=y;
prer=r;
}
sort(all(ans));
rep(i,n){
int r,a,b,c,d;
tie(r,a,b,c,d)=ans[i];
printf("%d %d %d %d\n", a,b,c,d);
}
} |
//#include <atcoder/all>
#include <bits/stdc++.h>
//using namespace atcoder;
using namespace std;
typedef long long ll;
#define MOD (long long)(1e9+7)
#define INF (1LL<<60)
#define rep(i,n) for(ll i = 0; i < (n); i++)
#define rep1(i,n) for(ll i = 1; i <= (n); i++)
template<class T> inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template<class T> inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
// 最大公約数
ll gcd(ll a, ll b)
{
if(b == 0) return a;
return gcd(b, a % b);
}
// mod m におけるa の逆元
ll modinv(ll a, ll m) {
ll b = m, u = 1, v = 0;
while (b) {
ll t = a / b;
a -= t * b; swap(a, b);
u -= t * v; swap(u, v);
}
u %= m;
if (u < 0) u += m;
return u;
}
// 素因数分解
vector<pair<ll, ll>> prim;
void pf(ll n)
{
ll s = sqrt(n);
ll r = 0;
for(ll i = 2; i <= s; i++) {
if((n % i) == 0) {
r = 0;
do {
r++;
n = n / i;
} while((n % i) == 0);
prim.push_back({i, r});
}
}
if(n > s) {
prim.push_back({n, 1});
}
}
void solve()
{
ll N; cin >> N;
vector<int> atc(5050, 0), cgc(5050, 0);
string s; cin >> s;
int at = 0, cg = 0;
ll ans = 0;
rep(i, N) {
switch(s[i]) {
case 'A':
atc[i + 1] = atc[i] + 1;
cgc[i + 1] = cgc[i];
break;
case 'T':
atc[i + 1] = atc[i] - 1;
cgc[i + 1] = cgc[i];
break;
case 'C':
atc[i + 1] = atc[i];
cgc[i + 1] = cgc[i] + 1;
break;
case 'G':
atc[i + 1] = atc[i];
cgc[i + 1] = cgc[i] - 1;
break;
}
}
rep(i, N) {
for(ll j = i + 1; j <= N; j++) {
if(atc[i] == atc[j] && cgc[i] == cgc[j]) ans++;
}
}
cout << ans << endl;
}
int main(void)
{
// ll t; cin >> t; rep(i, t)
solve();
}
| #include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// 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 rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define rrep1(i, n) for (int i = n; i >= 1; i--)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define eb emplace_back
#define fi first
#define se second
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
typedef long long int ll;
typedef pair<int, int> P;
// typedef modint1000000007 mint;
void speedUpIO() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
}
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
/*--------------------------------------------------*/
const int INF = 1e9;
// const ll INF = 1e18;
const int MAX = 1e5;
int h, w, a, b;
V<int> dh = {0, 1};
V<int> dw = {1, 0};
int ans = 0;
void dfs(VV<bool> used, int maia) {
if (maia == a) {
ans++;
return;
}
rep(hi, h) {
rep(wi, w) {
if (used[hi][wi]) continue;
used[hi][wi] = true;
rep(i, 2) {
int nh = hi + dh[i];
int nw = wi + dw[i];
if (nh < 0 || h <= nh) continue;
if (nw < 0 || w <= nw) continue;
if (used[nh][nw]) continue;
used[nh][nw] = true;
dfs(used, maia + 1);
used[nh][nw] = false;
}
}
}
}
void solve() {
cin >> h >> w >> a >> b;
// a: 2 * 1, b: 1 * 1;
VV<bool> used(h, V<bool>(w));
dfs(used, 0);
cout << ans << "\n";
}
int main() {
speedUpIO();
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fw(p) for(int w=0;w<(p);w++)
#define fx(p) for(int x=0;x<(p);x++)
#define fy(p) for(int y=0;y<(p);y++)
#define fz(p) for(int z=0;z<(p);z++)
#define fyg(p,g) for(int y=(g);y<(p);y++)
#define fzg(p,g) for(int z=(g);z<(p);z++)
#define ce(d) cout<<d<<endl;
#define vecp(p) int aa;cin>>aa;(p).push_back(aa);
#define vecpl(p) long long aa;cin>>aa;(p).push_back(aa);
#define vecps(p) string aa;cin>>aa;(p).push_back(aa);
#define vecp2(p) cin>>aa;(p).push_back(aa);
#define vecpl2(p) long long a b;cin>>ab;(p).push_back(ab);
#define vecps2(p) string ab;cin>>ab;(p).push_back(ab);
#define sorts(c) sort((c).begin(),(c).end());
#define reverses(c) reverse((c).begin(),(c).end());
#define vec(b) vector<int> (b);
#define vecl(b) vector<long long> (b);
#define vecs(b) vector<string> (b);
#define pb(b,a) (b).push_back((a));
#define doublece(a,b) cout<<(a)<<' '<<(b)<<endl;
#define pairs(s) vector<pair<int,int>> (s);
#define pairsp(s) int aa,bb;cin>>aa>>bb;(s).push_back(make_pair(aa,bb));
#define MOD 1000000007
#define cey ce("Yes")
#define cen ce("No")
#define ceY ce("YES")
#define ceN ce("NO")
int bNum[2000][2000];
int cNum[2000][2000];
int main()
{
int H, W, N, M;
cin >> H >> W >> N >> M;
pairs(A)
set<pair<int, int>> K;
set<pair<int, int>> G;
vector<vector<int>> B(H+1);
vector<vector<int>> C(W + 1);
fx(N) {
pairsp(A);
G.insert(A[x]);
}
fx(M) {
int c, d;
cin >> c >> d;
K.insert(make_pair(c, d));
B[c].push_back(d);
C[d].push_back(c);
}
fx(H+1) {
pb(B[x],0)
pb(B[x], W+1)
sorts(B[x]);
}
fx(W + 1) {
pb(C[x], 0)
pb(C[x], H + 1)
sorts(C[x]);
}
ll ans = 0;
fx(N) {
auto b = lower_bound(B[A[x].first].begin(), B[A[x].first].end(), A[x].second) - B[A[x].first].begin();
auto c = lower_bound(C[A[x].second].begin(), C[A[x].second].end(), A[x].first)- C[A[x].second].begin();
ans += B[A[x].first][b] - B[A[x].first][b - 1] + C[A[x].second][c] - C[A[x].second][c - 1] - 2;
bNum[A[x].first][b - 1]++;
cNum[A[x].second][c - 1]++;
}
fx(H) {
fy(W) {
if (K.count(make_pair(x + 1, y + 1))) { continue; }
auto b = lower_bound(B[x+1].begin(), B[x+1].end(), y+1) - B[x+1].begin();
auto c = lower_bound(C[y+1].begin(), C[y+1].end(), x+1) - C[y+1].begin();
ans -= max(bNum[x+1][b - 1] - 1,0);
ans -= max(cNum[y+1][c - 1] - 1, 0);
if (!G.count(make_pair(x+1,y+1))&& bNum[x+1][b - 1]>0&& cNum[y+1][c - 1]>0) {
ans -= 1;
}
}
}
ce(ans-N)
return 0;
}
| //Utkarsh.25dec
#include <bits/stdc++.h>
#include <chrono>
#include <random>
#define ll long long int
#define ull unsigned long long int
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define rep(i,n) for(ll i=0;i<n;i++)
#define loop(i,a,b) for(ll i=a;i<=b;i++)
#define vi vector <int>
#define vs vector <string>
#define vc vector <char>
#define vl vector <ll>
#define all(c) (c).begin(),(c).end()
#define max3(a,b,c) max(max(a,b),c)
#define min3(a,b,c) min(min(a,b),c)
#define deb(x) cerr<<#x<<' '<<'='<<' '<<x<<'\n'
using namespace std;
typedef vector<vector<ll>> matrix;
ll power(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll modInverse(ll a){return power(a,mod-2);}
const int N=500023;
bool vis[N];
vector <int> adj[N];
void solve()
{
ll n,m;
cin>>n>>m;
ll odd=0,even=0;
for(int i=1;i<=n;i++)
{
string s;
cin>>s;
int cnt=0;
for(auto ch:s)
{
if(ch=='1')
cnt++;
}
if(cnt%2==1)
odd++;
else
even++;
}
cout<<odd*even<<'\n';
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int T=1;
//cin>>T;
int t=0;
while(t++<T)
{
//cout<<"Case #"<<t<<":"<<' ';
solve();
//cout<<'\n';
}
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
} |
#include <bits/stdc++.h>
using namespace std;
// input
#define INIT std::ios::sync_with_stdio(false);std::cin.tie(0);
#define VAR(type, ...)type __VA_ARGS__;MACRO_VAR_Scan(__VA_ARGS__); // __VA_ARGS__可変引数マクロ
template<typename T> void MACRO_VAR_Scan(T& t) { std::cin >> t; }
template<typename First, typename...Rest>void MACRO_VAR_Scan(First& first, Rest& ...rest) { std::cin >> first; MACRO_VAR_Scan(rest...); }
#define VEC(type, c, n) std::vector<type> c(n);for(auto& i:c)std::cin>>i;
#define MAT(type, c, m, n) std::vector<std::vector<type>> c(m, std::vector<type>(n));for(auto& R:c)for(auto& w:R)std::cin>>w;
// output
template<typename T>void MACRO_OUT(const T t) { std::cout << t; }
template<typename First, typename...Rest>void MACRO_OUT(const First first, const Rest...rest) { std::cout << first << " "; MACRO_OUT(rest...); }
#define OUT(...) MACRO_OUT(__VA_ARGS__);
#define FOUT(n, dist) std::cout<<std::fixed<<std::setprecision(n)<<(dist); // std::fixed 浮動小数点の書式 / setprecision 浮動小数点数を出力する精度を設定する。
#define SP std::cout<<" ";
#define TAB std::cout<<"\t";
#define BR std::cout<<"\n";
#define SPBR(w, n) std::cout<<(w + 1 == n ? '\n' : ' ');
#define ENDL std::cout<<std::endl;
#define FLUSH std::cout<<std::flush;
// utility
#define ALL(a) (a).begin(),(a).end()
#define FOR(w, a, n) for(int w=(a);w<(n);++w)
#define REP(w, n) for(int w=0;w<int(n);++w)
#define IN(a, x, b) (a<=x && x<b)
template<class T> inline T CHMAX(T & a, const T b) { return a = (a < b) ? b : a; }
template<class T> inline T CHMIN(T& a, const T b) { return a = (a > b) ? b : a; }
template<class T> using V = std::vector<T>;
template<class T> using VV = V<V<T>>;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using PAIR = std::pair<int, int>;
using PAIRLL = std::pair<ll, ll>;
constexpr int INFINT = (1 << 30) - 1; // 1.07x10^ 9
constexpr int INFINT_LIM = (1LL << 31) - 1; // 2.15x10^ 9
constexpr ll INFLL = 1LL << 60; // 1.15x10^18
constexpr ll INFLL_LIM = (1LL << 62) - 1 + (1LL << 62); // 9.22x10^18
constexpr double eps = 1e-7;
constexpr int MOD = 1000000007;
constexpr double PI = 3.141592653589793238462643383279;
template<class T, size_t N> void FILL(T(&a)[N], const T & val) { for (auto& x : a) x = val; } // int a[5] = {1,2,3,4,5}; FILL(a,10);
template<class ARY, size_t N, size_t M, class T> void FILL(ARY(&a)[N][M], const T & val) { for (auto& b : a) FILL(b, val); } // 2次元配列でもそのまま使える
template<class T> void FILL(std::vector<T> & a, const T & val) { for (auto& x : a) x = val; } // 使い方 vector<int> a(3); FILL(a,10);
template<class ARY, class T> void FILL(std::vector<std::vector<ARY>> & a, const T & val) { for (auto& b : a) FILL(b, val); } // 2次元配列でもそのまま使える
// ------------>8------------------------------------->8------------
int main() {
INIT;
// VAR(ld,x,y,a,b)
VAR(ll,x,y,a,b)
// ld n = log(b /x) / log(a); // これだと、小数点桁落ちで、正確に値がでない
ll ans =0;
// for (int i = 1; x < b; i++) {
while(1){
if( x >= y /a ) break;
// if(a*x >= y) break;
if(a*x >= x+b) break;
x *= a;
ans++;
}
// ll ans = (y - pow(a,n) * x ) / b;
ans += (y - 1 - x ) / b;
OUT(ans)
return 0;
}
| #include<iostream>
#include<vector>
#include<cmath>
using namespace std;
int main(){
long long X, Y, A, B;
std::cin >> X >> Y >> A >> B;
long long exp = 0;
int flg_A = 0, flg_B = 0;
while(flg_A == 0){
if(X*(A - 1) < B && (double)X*A < 2e18){
if(X*A < Y){
X = X*A;
exp++;
}else{
flg_A = 1;
flg_B = 1;
}
}else{
flg_A = 1;
}
}
// std::cout << exp;
// std::cout << (long long)round(floorl(((double)Y - (double)X - 1.0)/(double)B));
//あとはずっとB
if(flg_B == 0){
exp = exp + (Y - X - 1)/B;
}
std::cout << exp;
} |
#include <iostream>
#include <cctype>
using namespace std;
int main() {
char S, T;
cin >> S >> T;
cout << (S == 'Y' ? (char)toupper(T) : T) << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second
using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;
const double eps = 1e-10;
template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}
template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}
template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}
template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}
template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
for(auto &e:u) fill_v<T>(e,v...);
}
int main(void){
cin.tie(0);
ios::sync_with_stdio(false);
string S, T;
cin >> S >> T;
if (S == "Y") {
cout << (char)(T[0]+'A'-'a') << endl;
} else {
cout << T << endl;
}
}
|
// {{{
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using vi = vector<int>;
using pii = pair<int, int>;
#define sz(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
#define clr(a, b) memset(a, b, sizeof(a))
#define debug(x...)
#define debug_arr(x...)
#ifdef LOCAL
#include "prettyprint.hpp"
#endif
// }}}
const int N = 2e5 + 10;
int head[N], e;
struct E
{
int v, next;
} edge[N << 1];
void addedge(int u, int v)
{
edge[e].v = v, edge[e].next = head[u];
head[u] = e++;
}
int st[N], ed[N], dep[N];
int tim;
void dfs(int u, int f, int d)
{
tim++;
st[u] = tim;
dep[u] = d;
for (int i = head[u]; ~i; i = edge[i].next)
{
int v = edge[i].v;
if (v == f) continue;
dfs(v, u, d + 1);
}
tim++;
ed[u] = tim;
// debug(u, st[u], ed[u], dep[u]);
}
int n, m;
vi vec[N];
int main()
{
#ifdef LOCAL
freopen("in", "r", stdin);
// freopen("out", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
while (cin >> n)
{
clr(head, -1), e = 0;
for (int i = 2; i <= n; ++i)
{
int f;
cin >> f;
addedge(i, f);
addedge(f, i);
}
tim = 0;
dfs(1, -1, 0);
for (int i = 0; i < n; ++i) vec[i].clear();
for (int i = 1; i <= n; ++i)
{
int curd = dep[i];
vec[curd].push_back(st[i]);
}
for (int i = 0; i < n; ++i) sort(all(vec[i]));
// debug(vec[2]);
cin >> m;
for (int i = 1; i <= m; ++i)
{
int u, d;
cin >> u >> d;
if (d < dep[u])
{
cout << 0 << endl;
continue;
}
int l = st[u], r = ed[u];
// debug(u, l, r);
auto iter1 = lower_bound(all(vec[d]), l);
auto iter2 = lower_bound(all(vec[d]), r);
int cnt = iter2 - iter1;
cout << cnt << endl;
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
const int M = 2e5;
vector<int> adj[M];
long long c[18], k, memo[18][(1 << 18)];
map<long long, map<long long, long long>> dist;
long long solve(int i, int mask) {
if (mask == (1 << k) - 1)
return 0;
long long &res = memo[i][mask];
if (res == -1) {
res = 1e18;
for (int j = 0; j < k; j++)
if (!((1 << j) & mask))
res = min(res, dist[c[i]][c[j]] + solve(j, mask | (1 << j)));
}
return res;
}
void bfs(int i) {
long long d[M] = {};
bool v[M] = {};
queue<int> q;
q.push(i);
v[i] = 1;
while (!q.empty()) {
int cur = q.front();
q.pop();
for (int x : adj[cur])
if (!v[x])
q.push(x), v[x] = 1, d[x] = d[cur] + 1;
}
for (int j = 0; j < k; j++)
dist[i][c[j]] = dist[c[j]][i] = d[c[j]];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
memset(memo, -1, sizeof memo);
int n, m;
cin >> n >> m;
while (m--) {
int a, b;
cin >> a >> b;
a--;
b--;
adj[a].push_back(b);
adj[b].push_back(a);
}
cin >> k;
for (int i = 0; i < k; i++)
cin >> c[i], c[i]--;
for (int i = 0; i < k; i++)
bfs(c[i]);
for (int i = 0; i < k - 1; i++)
for (int j = i + 1; j < k; j++)
if (!dist[c[i]][c[j]])
return cout << "-1\n", 0;
long long ans = 1e18;
for (int i = 0; i < k; i++)
ans = min(ans, solve(i, (1 << i)));
cout << ans + 1 << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
//constexpr ll MOD = 1e9 + 7;
constexpr ll MOD = 998244353;
//constexpr ll MOD = ;
ll mod(ll A, ll M) {return (A % M + M) % M;}
constexpr ll INF = 1LL << 60;
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;}
ll divceil(ll A, ll B) {return (A + (B - 1)) / B;}
#define FINALANS(A) do {cout << (A) << '\n'; exit(0);} while (false)
int main()
{
vector<ll> P = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
31, 37, 41, 43, 47, 53, 59, 61, 67, 71};
ll K = P.size();
ll A, B;
cin >> A >> B;
ll N = B - A + 1;
vector<ll> bb(N, 0);
for (ll i = 0; i < N; i++)
{
for (ll j = 0; j < K; j++)
{
if ((A + i) % P.at(j) == 0)
bb.at(i) ^= (1LL << j);
}
}
vector<vector<ll>> dp(N + 1, vector<ll>((1LL << K), 0));
dp.at(0).at(0) = 1;
for (ll i = 0; i < N; i++)
{
for (ll b = 0; b < (1LL << K); b++)
{
dp.at(i + 1).at(b) += dp.at(i).at(b);
if ((b & bb.at(i)) == 0)
dp.at(i + 1).at(b ^ bb.at(i)) += dp.at(i).at(b);
}
}
ll ans = 0;
for (ll b = 0; b < (1LL << K); b++)
{
ans += dp.at(N).at(b);
//cerr << dp.at(N).at(b) << endl;
}
cout << ans << endl;
} | //Har Har Mahadev
using namespace std;
#include <bits/stdc++.h>
#define booga cerr << "booga" << endl
#define int long long
#define ld long double
#define pb push_back
#define mp make_pair
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define safai(...) Clearing_out(__VA_ARGS__)
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(char c) {
string s;
s += c;
return s;
}
string to_string(const char* s) {
return to_string((string) s);
}
string to_string(bool b) {
return (b ? "1" : "0");
}
string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template <size_t N>
string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++) {
res += static_cast<char>('0' + v[i]);
}
return res;
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
void Clearing_out() { return; }
template <typename Head, typename... Tail>
void Clearing_out(Head &H, Tail & ... T) {
H.clear();
Clearing_out(T...);
}
void testcase(){
// int dp[50][50];
int h, w;
cin >> h >> w;
string grid[h];
for(int i = 0; i < h; i++){
cin >> grid[i];
}
int ans = 1;
const int mod = 998244353;
for(int col = 0; col < w; col++){
int nr= 0, nb =0, nd = 0;
int j = col;
int i = 0;
while(j >= 0 && i < h){
if(grid[i][j] == '.')nd++;
else if(grid[i][j] == 'R')nr++;
else nb++;
j--, i++;
}
if(nr > 0 && nb > 0){
cout << "0";
return;
}
else if(nr > 0) ans *= 1;
else if(nb > 0) ans *= 1;
else ans *= 2, ans %= mod;
}
for(int row = 1; row < h; row++){
int nr= 0, nb =0, nd = 0;
int i = row;
int j = w-1;
while(i < h && j>= 0){
if(grid[i][j] == '.')nd++;
else if(grid[i][j] == 'R')nr++;
else nb++;
j--, i++;
}
if(nr > 0 && nb > 0){
cout << "0";
return;
}
else if(nr > 0) ans *= 1;
else if(nb > 0) ans *= 1;
else ans *= 2, ans %= mod;
}
cout << ans << '\n';
}
int32_t main(){
ios::sync_with_stdio(false);
cin.tie(0);
int tt = 1;
// cin >> tt;
while(tt--){
testcase();
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fo(i,n) for(int i=0;i<n;i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define tr(it,a) for(auto it=a.begin();it!=a.end();it++)
typedef pair<int, int> pii;
typedef pair<long, long> pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<pair<int, int>> vpii;
#define FASTIO ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
int main() {
FASTIO
int t = 1;
// cin >> t;
while (t--) {
int n;
cin >> n;
vpii v(n);
fo(i, n) {
cin>>v[i].F;
cin>>v[i].S;
}
int ans = 0;
fo(i, n) {
int x1 = v[i].F;
int y1 = v[i].S;
// cout<<x1<<" "<<y1<<"points"<<endl;
for (int j = i + 1; j < n; j++) {
int delx=v[j].F-x1;
int dely=v[j].S-y1;
// cout<<delx<<" "<<dely<<endl;
if ( abs(dely)<=abs(delx) ) {
ans++;
}
}
}
cout << ans << endl;
}
return 0;
}
| /*Allah Vorosha*/
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define pb push_back
#define n_p next_permutation
#define p_p prev_permutation
#define in insert
#define rev reverse
#define pf push_front
#define pob pop_back
#define uniq(v) v.resize(distance(v.begin(),unique(v.begin(),v.end())))
#define all(x) (x).begin(),(x).end()
#define pof pop_front
#define ios ios_base::sync_with_stdio(0);cin.tie();cout.tie();
#define scn scanf
#define prt printf
#define rep(i, a, n) for(int i = a; i < n; i++)
#define mod 720720
#define yes cout << "Yes\n"
#define no cout << "No\n";
#define take for(auto &it : a) cin >> it;
#define out cout << a << "\n";
#define l_b lower_bound
#define u_b upper_bound
#define Max 100005
using namespace std;
const int N = 10005;
void solve() {
ll n, m;
cin >> n >> m;
for(ll i = 1; i * i <= m; ++i) {
if(m % i == 0) {
ll k = m / i;
if(i + k == n) {
yes;
return;
}
}
}
no;
}
int main() {
solve();
return 0;
}
|
#include <iostream>
using namespace std;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int x, y;
cin >> x >> y;
if (y > x) swap(x, y);
if (y + 3 > x)
cout << "Yes";
else
cout << "No";
return 0;
} | #include <cmath>
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<queue>
#include<set>
#include<map>
#include<bitset>
#include<iomanip> //setprecision
#include<stack>
#include<set>
#include<deque>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll mod = 1000000007;
ll mod2 = 998244353;
int main() {
ll s, p;
cin >> s >> p;
for (int i = 1; i <= sqrt(p); i++) {
if (p % i == 0) {
ll m = p / i;
if (i + m == s) {
cout << "Yes" << endl;
return 0;
}
}
}
cout << "No" << endl;
return 0;
} |
#include<bits/stdc++.h>
#define mod 998244353
#define int long long
using namespace std;
inline int read() {
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
return x*f;
}
int n;
signed main() {
cin>>n;
for(int i=1;i<=n;i++) {
int ans=1,now=i;
for(int j=2;1ll*j*j<=i;j++) {
while(now%j==0) ans++,now/=j;
}
if(now!=1) ans++;
cout<<ans<<' ';
}
}
| #include<bits/stdc++.h>
using namespace std;
int n,a[101010];
int main()
{
cin>>n;
a[1]=1;
for(int i=2;i<=n;i++)
{
for(int j=1;j*j<=i;j++)
{
if(i%j!=0)
{
continue;
}
a[i]=max(a[i],a[j]+1);
if(j!=1)
{
a[i]=max(a[i],a[i/j]+1);
}
}
}
for(int i=1;i<=n;i++)
{
cout<<a[i]<<' ';
}
cout<<endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, t;
cin >> n;
vector<pair<double, double>> v;
double l, r;
for (int i = 0; i < n; ++i) {
cin >> t >> l >> r;
if (t == 2) {
r -= 0.1;
} else if (t == 3) {
l += 0.1;
} else if (t == 4) {
l += 0.1;
r -= 0.1;
}
v.push_back(make_pair(l, r));
}
int ans = 0;
double l1, r1, l2, r2;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
tie(l1, r1) = v.at(i);
tie(l2, r2) = v.at(j);
if (r1 < l2 || r2 < l1) continue;
ans++;
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
long long extGCD(long long a, long long b, long long c, long long &x, long long &y){
long long a_gcd = a;
long long b_gcd = b;
std::vector< long long > A, B;
A.push_back(a_gcd);
B.push_back(b_gcd);
while(b_gcd != 0){
long long tmp = a_gcd % b_gcd;
a_gcd = b_gcd;
b_gcd = tmp;
A.push_back(a_gcd);
B.push_back(b_gcd);
}
if(c % a_gcd != 0){
x = -1e18;
y = -1e18;
return -1;
}
std::vector< long long > X(A.size()), Y(A.size());
X[A.size() - 1] = c / a_gcd;
Y[A.size() - 1] = 0;
for(int i=A.size()-2; i>=0; i--){
X[i] = Y[i+1];
Y[i] = X[i+1] - (A[i] / B[i]) * X[i];
}
x = X[0];
y = Y[0];
b /= a_gcd;
if(x >= 0){
return x % b;
}else{
long long ret;
ret = b - std::abs(x) % b;
if(ret == b){
ret = 0;
}
return ret;
}
}
int main(){
int T;
std::cin >> T;
std::vector< long long > ans(T);
for(int t=0; t<T; t++){
long long X, Y, P, Q;
std::cin >> X >> Y >> P >> Q;
long long min = 9223372036854775807;
for(int p=P; p<P+Q; p++){
for(int x=X; x<X+Y; x++){
long long ret1, ret2;
long long tmp = extGCD(P+Q, 2*(X+Y), x-p, ret1, ret2);
if(tmp != -1){
min = std::min(min, p + (P+Q) * tmp);
}
}
}
ans[t] = min;
}
for(int i=0; i<T; i++){
if(ans[i] == 9223372036854775807){
std::cout << "infinity" << std::endl;
}else{
std::cout << ans[i] << std::endl;
}
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define rrep1(i,n) for(int i=(n);i>0;i--)
#define fore(i_in,a) for (auto& i_in: a)
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fio() cin.tie(nullptr);ios::sync_with_stdio(false);
#define DEBUG_CTN(v) cerr<<#v<<":";for(auto itr=v.begin();itr!=v.end();itr++) cerr<<" "<<*itr; cerr<<endl
template<class T> bool chmax(T &a, const T &b) {if (a<b) { a = b; return true; } return false;}
template<class T> bool chmin(T &a, const T &b) {if (a>b) { a = b; return true; } return false;}
template<class T> void print(const T &t) { cout << t << "\n"; }
const ll INF = 1LL << 62;
const int iINF = 1 << 30;
int max_num = 101010;
using vi = vector<int>;
using Graph = vector<vi>;
Graph G(max_num);
vi cnt(max_num), color(max_num);
vector<bool> good(max_num);
void dfs(int v, int p = -1, int d = 0) {
if(cnt[color[v]]==0) good[v]=true;
cnt[color[v]]++;
fore(nv, G[v]) {
if (nv == p) continue;
dfs(nv, v, d+1);
}
cnt[color[v]]--;
}
int n,a,b;
int main() {
fio(); cin>>n;
rep(i,n) cin>>color[i];
rep(i,n-1) {
cin>>a>>b;a--;b--;
G[a].emplace_back(b);
G[b].emplace_back(a);
}
dfs(0);
rep(i,n) if(good[i]) print(i+1);
}
| #include <iostream>
#include <iomanip>
#include <sstream>
#include <vector>
#include <string>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <functional>
#include <iterator>
#include <limits>
#include <numeric>
#include <utility>
#include <type_traits>
#include <cmath>
#include <cassert>
#include <cstdio>
using namespace std;
using namespace placeholders;
using LL = long long;
using ULL = unsigned long long;
using VI = vector< int >;
using VVI = vector< vector< int > >;
using VS = vector< string >;
using ISS = istringstream;
using OSS = ostringstream;
using PII = pair< int, int >;
using VPII = vector< pair< int, int > >;
template < typename T = int > using VT = vector< T >;
template < typename T = int > using VVT = vector< vector< T > >;
template < typename T = int > using LIM = numeric_limits< T >;
template < typename T = int > using OSI = ostream_iterator< T >;
template < typename T > inline istream& operator>>( istream &s, vector< T > &v ){ for ( T &t : v ) { s >> t; } return s; }
template < typename T > inline ostream& operator<<( ostream &s, const vector< T > &v ){ for ( int i = 0; i < int( v.size() ); ++i ){ s << ( " " + !i ) << v[i]; } return s; }
void in_impl(){};
template < typename T, typename... TS > void in_impl( T &head, TS &... tail ){ cin >> head; in_impl( tail ... ); }
#define IN( T, ... ) T __VA_ARGS__; in_impl( __VA_ARGS__ );
template < typename T > inline T fromString( const string &s ) { T res; istringstream iss( s ); iss >> res; return res; }
template < typename T > inline string toString( const T &a ) { ostringstream oss; oss << a; return oss.str(); }
#define NUMBERED( name, number ) NUMBERED2( name, number )
#define NUMBERED2( name, number ) name ## _ ## number
#define REP1( n ) REP2( NUMBERED( REP_COUNTER, __LINE__ ), n )
#define REP2( i, n ) REP3( i, 0, n )
#define REP3( i, m, n ) for ( int i = ( int )( m ); i < ( int )( n ); ++i )
#define GET_REP( a, b, c, F, ... ) F
#define REP( ... ) GET_REP( __VA_ARGS__, REP3, REP2, REP1 )( __VA_ARGS__ )
#define FOR( e, c ) for ( auto &&e : c )
#define ALL( c ) begin( c ), end( c )
#define AALL( a ) ( remove_all_extents< decltype( a ) >::type * )a, ( remove_all_extents< decltype( a ) >::type * )a + sizeof( a ) / sizeof( remove_all_extents< decltype( a ) >::type )
#define SZ( v ) ( (int)( v ).size() )
#define EXISTS( c, e ) ( ( c ).find( e ) != ( c ).end() )
template < typename T > inline bool chmin( T &a, const T &b ){ if ( b < a ) { a = b; return true; } return false; }
template < typename T > inline bool chmax( T &a, const T &b ){ if ( a < b ) { a = b; return true; } return false; }
#define PB push_back
#define EM emplace
#define EB emplace_back
#define BI back_inserter
#define MP make_pair
#define fst first
#define snd second
#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
// Λ Λ__
// /(*゚ー゚)/\
// /|  ̄U U ̄|\/
// | |/
int main()
{
cin.tie( nullptr );
ios::sync_with_stdio( false );
cout << setprecision( 12 ) << fixed;
IN( int, N, M );
if ( N == 1 && M == 0 )
{
cout << "1 2" << endl;
return 0;
}
if ( M < 0 || N - 1 <= M )
{
cout << -1 << endl;
return 0;
}
VPII res;
res.EB( 1, 1 );
--N;
++M;
for ( ; M; --M, --N )
{
const int l = res.back().snd + 1;
res.EB( l, l + 1 );
}
res[0].snd = res.back().snd + 1;
while ( N-- )
{
const int l = res.back().snd + 2;
res.EB( l, l + 1 );
}
FOR( p, res )
{
cout << p.fst << ' ' << p.snd << '\n';
}
cout << flush;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define sz(x) (int)x.size()
int lcm(int A,int B){ return A * B / (__gcd(A, B));}
const int mod=1e9+7;
const int mxN=2e5;
//=======================
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; pri(x); cerr << endl;
#else
#define debug(x);
#endif
typedef unsigned long long ull;
typedef long double lld;
void pri(int t) {cerr << t;}
void pri(string t) {cerr << t;}
void pri(char t) {cerr << t;}
void pri(lld t) {cerr << t;}
void pri(double t) {cerr << t;}
void pri(ull t) {cerr << t;}
template <class T, class V> void pri(pair <T, V> p);
template <class T> void pri(vector <T> v);
template <class T> void pri(set <T> v);
template <class T, class V> void pri(map <T, V> v);
template <class T> void pri(multiset <T> v);
template <class T, class V> void pri(pair <T, V> p) {cerr << "{"; pri(p.ff); cerr << ","; pri(p.ss); cerr << "}";}
template <class T> void pri(vector <T> v) {cerr << "[ "; for (T i : v) {pri(i); cerr << " ";} cerr << "]";}
template <class T> void pri(set <T> v) {cerr << "[ "; for (T i : v) {pri(i); cerr << " ";} cerr << "]";}
template <class T> void pri(multiset <T> v) {cerr << "[ "; for (T i : v) {pri(i); cerr << " ";} cerr << "]";}
template <class T, class V> void pri(map <T, V> v) {cerr << "[ "; for (auto i : v) {pri(i); cerr << " ";} cerr << "]";}
//=======================
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("1.txt", "w", stderr);
#endif
int n;
cin >> n;
string s;
cin >> s;
if(s[0] != s[n-1]){
cout << 1 << '\n';
}
else{
bool ok=false;
for(int i = 1; i <n-1; i++){
if(s[i] != s[n-1] and s[i+1]!=s[n-1]){
ok=true;
break;
}
}
if(ok){
cout << 2 << "\n";
}
else{
cout << -1 << '\n';
}
}
} | #include <bits/stdc++.h>
//#include <atcoder/all>
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 vector<bool> VB;
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) LL((a).size())
#define FOR(i,a,b) for(LL i=(a);i<(b);++i)
#define ROF(i,a,b) for(LL 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 LL LARGE_INT = 1e9+100;
const LL INF = 2e9+100;
const LL INF_LL = (LL)INF*(LL)INF;
const LL 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,m;cin>>n>>m;
VL a(n);
REP(i,n){
cin>>a[i];
}
LL result = n;
VL c(n);
REP(i,m){
c[a[i]]++;
}
LL small = n;
REP(i,n){
if(c[i] == 0){
small = i;
result = i;
break;
}
}
FOR(i,m,n){
LL plus = a[i];
LL minus = a[i-m];
c[plus]++;
c[minus]--;
if(c[minus] == 0){
result = min(result,minus);
}
}
cout<<result<<endl;
return;
}
int main()
{
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(15);
Main();
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define rep(i,n) for(ll (i)=0; (i)<(ll)(n); (i)++)
#define frep(i,m,n) for(ll (i)=(m); (i)<=(ll)(n); (i)++)
#define rrep(i,n) for(ll (i)=(n)-1; (i)>-1; (i)--)
#define frrep(i,m,n) for(ll (i)=(n); (i)>(ll)(m); (i)--)
#define ALL(x) (x).begin(), (x).end()
#define PB(x) push_back(x)
#define MP(x, y) make_pair(x, y)
const ll INF = 100100100100100100;
const ll MOD = 1000000007;
// get abs
ll my_abs(ll a);
// a^n
ll a_n(ll a, ll n);
// get gcd
ll my_gcd(ll a, ll b);
// a^(-1) % MOD
ll inv(ll a);
// (a+b+c)%MOD
ll madd(ll a, ll b, ll c);
// (a-b)%MOD
ll msub(ll a, ll b);
// (a*b*c)%MOD
ll mtime(ll a, ll b, ll c);
int n;
priority_queue<int, vector<int>, greater<int>> good;
vector<int> c(100005);
vector<queue<int>> edge(100005);
vector<int> s(100005, 0);
void dfs(int prev, int now) {
if(s[c[now]] == 0) {
good.push(now);
}
s[c[now]]++;
while(!edge[now].empty()) {
int tmp = edge[now].front();
edge[now].pop();
if(tmp != prev) {
dfs(now, tmp);
}
}
s[c[now]]--;
}
int main() {
cin >> n;
rep(i, n) cin >> c[i];
rep(i, n-1) {
int a, b; cin >> a >> b;
a--; b--;
edge[a].push(b);
edge[b].push(a);
}
dfs(-1, 0);
while(!good.empty()) {
cout << good.top()+1 << endl;
good.pop();
}
return 0;
}
ll my_abs(ll a) {
if(a >= 0) return a;
else return -1 *a;
}
ll a_n(ll a, ll n) {
if(n == 0) return 1;
ll ret = a, count = 1;
while(count * 2 < n) {
ret *= ret;
count *= 2;
}
if(count == n) return ret;
else return (ret * a_n(a, n-count));
}
ll my_gcd(ll a, ll b) {
if(b == 0) return a;
return my_gcd(b, a%b);
}
ll inv(ll a) {
return a_n(a, MOD-2);
}
ll madd(ll a, ll b, ll c) {
ll ret = (a+b) % MOD;
return (ret+c) % MOD;
}
ll msub(ll a, ll b) {
if(a < b) return (a-b+MOD) % MOD;
else return (a-b) % MOD;
}
ll mtime(ll a, ll b, ll c) {
ll ret = (a*b) % MOD;
return (ret*c) % MOD;
}
| //
// Created by mihai145 on 23.02.2021.
//
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int NMAX = 1e5;
int N, w[NMAX + 5];
vector<int> g[NMAX + 5];
int dfs(int node, int parent = -1) {
int ans = 1;
w[node] = 1;
vector<int> v;
int evenLeft = 0;
for (auto it : g[node]) {
if (it != parent) {
int nextRes = dfs(it, node);
w[node] += w[it];
if (w[it] % 2 == 0) {
if (nextRes <= 0) {
ans += nextRes;
} else {
evenLeft += nextRes;
}
} else {
v.push_back(nextRes);
}
}
}
sort(v.begin(), v.end());
v.push_back(evenLeft);
for (int i = 0; i < (int) v.size(); i++)
if (i % 2 == 0) {
ans += v[i];
} else {
ans -= v[i];
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N;
for (int i = 2; i <= N; i++) {
int parent;
cin >> parent;
g[parent].push_back(i);
}
cout << (N + dfs(1)) / 2 << '\n';
return 0;
} |
#include<bits/stdc++.h>
#define pb push_back
#define Int long long
using namespace std;
long long N,M,K;
long long h,w;
long long X,Y,Z;
long long B;
const int MOD=1e9+7;
const int MAXN=5e5+5;
const int NAX=1005;
//vector<int> adj[MAXN];
vector<array<int,3>> adj[MAXN];
vector<Int> A;
Int ceilS(Int x,Int y){
return (x+y-1)/y;
}
Int perform(Int x){
return (x*(x+1))/2LL;
}
long long ModExpo(long long x,unsigned long long y,long long M){
Int ans=1;
ans=(long long)ans;
while(y>0){
if(y&1) ans=((ans%M)*(x%M))%M;
y>>=1LL;
x=((x%M)*(x%M))%M;
}
return ans%M;
}
long long ModInv(long long x){
return ModExpo(x,MOD-2,MOD);
}
Int madd(Int x,Int y){
return (x+y)%MOD;
}
Int mmul(Int x,Int y){
return ((x%MOD)*(y%MOD))%MOD;
}
vector<long long> fac(MAXN);
Int performMod(Int x){
return mmul(mmul(x,x+1),ModInv(2))%MOD;
}
void fill(){
fac[0]=1;
for(int i=1;i<MAXN;++i){
fac[i]=mmul(fac[i-1],i)%MOD;
}
}
long long choose(long long n,long long r){
if(r==0||r==n) return 1;
if(r>n) return 0;
return mmul(mmul(fac[n],ModInv(fac[n-r])),ModInv(fac[r]))%MOD;
}
const int INF=1e9;
using D=long double;
vector<pair<int64_t,int64_t>> factorize(int64_t x){
vector<pair<int64_t,int64_t>> factors;
for(int64_t i=2;i*i<=x;++i){
if(x % i) continue;
int cnt=0;
while(x % i ==0){
x/=i;
++cnt;
}
factors.pb({i,cnt});
}
if(x > 1){
factors.pb({x,1});
}
return factors;
}
struct TreeNode{
int data;
struct TreeNode* children[2];
TreeNode(int x){
data=x;
for(int i=0;i<2;++i){
children[i]=nullptr;
}
}
};
TreeNode* recur(int l,int r){
if(r < l) return nullptr;
if(l==r){
return new TreeNode(A[l]);
}
int mx=0,idx;
for(int i=l;i<=r;++i){
if(mx < A[i]){
mx=A[i];
idx=i;
}
}
TreeNode* root=new TreeNode(mx);
root->children[0]=recur(l,idx-1);
root->children[1]=recur(idx+1,r);
return root;
}
vector<int> depth;
void Preorder(TreeNode *root,int lvl){
if(!root) return;
depth[root->data]=lvl;
Preorder(root->children[0],lvl+1);
Preorder(root->children[1],lvl+1);
}
int main()
{
ios_base::sync_with_stdio(false);cin.tie(0);
cout.tie(0);
cin>>N>>M>>X>>Y;
for(int i=0;i<M;++i){
int u,v,w,k;
cin>>u>>v>>w>>k;
--u;--v;
adj[u].pb({v,w,k});
adj[v].pb({u,w,k});
}
auto merge=[&](int64_t x,int64_t k){
if(x % k==0) return 0LL;
return (k - (x%k))*1LL;
};
auto dijkstra=[&](int src,int d){
vector<int64_t> dis(N,1e15);
priority_queue<array<int64_t,3>,vector<array<int64_t,3>>,greater<array<int64_t,3>>> pq;
dis[src]=0;
pq.push({dis[src],src,0});
while(!pq.empty()){
auto u=pq.top();
pq.pop();
if(dis[u[1]] < u[0]) continue;
for(auto &v:adj[u[1]]){
if(dis[u[1]] + v[1] +merge(dis[u[1]],v[2]) < dis[v[0]]){
dis[v[0]]=dis[u[1]] + v[1] + merge(dis[u[1]],v[2]);
pq.push({dis[v[0]],v[0],merge(dis[u[1]],v[2])});
}
}
}
cout<<(dis[d]==1e15 ? -1:dis[d])<<'\n';
};
dijkstra(--X,--Y);
} | #include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
#define int long long
const int N = 2e5+10;
typedef pair<int, int> pii;
struct E{
int v,t,k;
};
vector<E> e[N];
int n, m;
int dis[N];
bool vis[N];
void dijkstra(int s) {
memset(dis, 0x3f, sizeof dis);
dis[s] = 0;
priority_queue<pii, vector<pii>, greater<pii>> q;
q.push({0, s});
while (!q.empty()) {
auto t = q.top(); q.pop();
int u = t.second, distance = t.first;
if (vis[u]) continue;
vis[u] = true;
for (auto to: e[u]) {
int v = to.v, t = to.t, k = to.k;
int W = (distance + k - 1) / k * k + t;
if (dis[v] > W) {
dis[v] = W;
q.push({dis[v], v});
}
}
}
}
signed main() {
int A, B;
scanf("%lld%lld%lld%lld",&n,&m,&A,&B);
while (m--) {
int u, v, t, k;
scanf("%lld%lld%lld%lld",&u,&v,&t,&k);
e[u].push_back({v, t, k});
e[v].push_back({u, t, k});
}
dijkstra(A);
int ans = dis[B];
if (ans == 0x3f3f3f3f3f3f3f3f) ans = -1;
printf("%lld\n",ans);
return 0;
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.