code_file1
stringlengths 87
4k
| code_file2
stringlengths 85
4k
|
---|---|
#ifdef _DEBUG
#define _GLIBCXX_DEBUG
#endif
#if __has_include(<atcoder/all>)
#include <atcoder/all>
#endif
#include <bits/stdc++.h>
using namespace std;
#define int long long
using ll = long long;
using ld = long double;
using P = pair<int, int>;
#define rep(i, a, b) for(int i = (int)(a); i <= (int)(b); i++)
#define rrep(i, a, b) for(int i = (int)(a); i >= (int)(b); i--)
const int MOD = 1000000007;
const int MOD2 = 998244353;
const ll INF = 1e18;
const ld PI = acos(-1);
signed main() {
int N;
cin >> N;
//小さい N こと大きい N 個を組み合わせて作る
vector<int> A(2*N);
vector<P> B(2*N);
rep(i,0,2*N-1){
cin >> A[i];
B[i].first=A[i];
B[i].second=i;
}
sort(B.begin(),B.end());
vector<int> small(2*N,0);
rep(i,0,N-1){
small[B[i].second]=1;
}
stack<int> smalls,larges;
vector<char> ans(2*N);
rep(i,0,2*N-1){
if(small[i]==1){
smalls.push(i);
}
else{
larges.push(i);
}
if(smalls.size()&&larges.size()){
int F=smalls.top();smalls.pop();
int G=larges.top();larges.pop();
ans[min(F,G)]='(';
ans[max(F,G)]=')';
}
}
rep(i,0,2*N-1){
cout << ans[i];
}
cout << endl;
} | #include<algorithm>
#include<cstdio>
#define ll long long
#define MaxN 400500
using namespace std;
int n;
ll a[MaxN],x[MaxN];
int main()
{
scanf("%d",&n);
for (int i=1;i<=n+n;i++){
scanf("%lld",&a[i]);
x[i]=a[i]=a[i]*n+i;
}
sort(x+1,x+n+n+1);
for (int i=1,cnt;i<=n+n;i++){
if (a[i]<=x[n]){
putchar(cnt<0 ? ')' : '(');
cnt++;
}else {
putchar(cnt>0 ? ')' : '(');
cnt--;
}
}return 0;
} |
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
IOSetup() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
std::cout << fixed << setprecision(20);
}
} iosetup;
int main() {
string x; cin >> x;
int comma = find(ALL(x), '.') - x.begin();
cout << x.substr(0, comma) << '\n';
return 0;
}
| #include <iostream>
using namespace std;
int main() {
string s;
getline(cin, s);
int i = s.find('.');
string x = s.substr(0, i);
cout<<x;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using matrix = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repp(i, st, en) for (ll i = (ll)st; i < (ll)(en); i++)
#define repm(i, st, en) for (ll i = (ll)st; i >= (ll)(en); i--)
#define all(v) v.begin(), v.end()
template<class in_chmax> void chmax(in_chmax &x, in_chmax y) {x = max(x,y);}
template<class in_chmin> void chmin(in_chmin &x, in_chmin y) {x = min(x,y);}
void Yes() {cout << "Yes" << endl; exit(0);}
void No() {cout << "No" << endl; exit(0);}
template<class in_Cout> void Cout(in_Cout x) {cout << x << endl; exit(0);}
template<class in_vec_cout>
void vec_cout(vector<in_vec_cout> vec) {
for (in_vec_cout res : vec) {cout << res << " ";}
cout << endl;
}
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll maxN = 110;
ll powmodp(ll x, ll n) {
if (n==0) return 1;
if (n%2) return x * powmodp(x,n-1) % mod;
ll res = powmodp(x,n/2);
return res * res % mod;
}
ll inv(ll x) {return powmodp(x,mod-2);}
matrix E(ll n) {
matrix res(n,vector<ll>(n));
rep(i,n) res[i][i] = 1;
return res;
}
void addmodp(ll &x, ll y) {x = (x + y) % mod;}
matrix prodmatrix(matrix x, matrix y) {
ll H = x.size(), W = y[0].size(), sz = y.size();
matrix res(H,vector<ll>(W));
rep(i,H) rep(j,W) rep(k,sz) {
addmodp(res[i][j], x[i][k] * y[k][j] % mod);
}
return res;
}
vector<ll> prodmatrix(matrix A, vector<ll> x) {
matrix X(x.size(),vector<ll>(1));// |y1| |A11 | |x1|
rep(i,x.size()) X[i][0] = x[i]; // |y2| = | A22 | * |x2|
matrix Y = prodmatrix(A,X); // |y3| | A33| |x3|
vector<ll> y(Y.size());
rep(i,Y.size()) y[i] = Y[i][0];
return y;
}
vector<ll> prodmatrix(vector<ll> x, matrix A) {
matrix X(1,vector<ll>(x.size()));// |A11 |
rep(i,x.size()) X[0][i] = x[i]; // |y1, y2, y3| = |x1, x2, x3| * | A22 |
matrix Y = prodmatrix(X,A); // | A33|
vector<ll> y(Y[0].size());
rep(i,Y[0].size()) y[i] = Y[0][i];
return y;
}
matrix powmatrix(matrix x, ll n) {
if (n==0) return E(x.size());
if (n%2) return prodmatrix(x,powmatrix(x,n-1));
matrix res = powmatrix(x,n/2);
return prodmatrix(res,res);
}
ll N, M, K;
vector<ll> A(maxN);
matrix mat(maxN,vector<ll>(maxN));
vector<ll> G[maxN];
int main() {
cin >> N >> M >> K;
rep(i,N) cin >> A[i];
rep(i,M) {
ll x, y; cin >> x >> y;
x--; y--;
G[x].push_back(y);
G[y].push_back(x);
}
rep(now,N) {
ll n = G[now].size();
ll p = n * inv(M) % mod, q = (1-p+mod) % mod;
mat[now][now] = ((p * inv(2) % mod) + q) % mod;
for (ll to : G[now]) {
mat[now][to] = inv(M) * inv(2) % mod;
}
}
vector<ll> res = prodmatrix(A,powmatrix(mat,K));
rep(i,N) cout << res[i] << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
string n;
int k;
cin >> n >> k;
int sz=n.size();
n="$"+n;
const ll mod=1000000007;
auto add=[&](ll &a,ll b){a=(a+b)%mod;};
auto digit=[&](char c)->int
{
if('0'<=c&&c<='9') return (c-'0');
else return (10+c-'A');
};
vector<ll> dp(17,0);
vector<bool> c(16,0);
int cnt=0;
for(int i=0;i<sz;i++)
{
if(i>0)
{
if(c[digit(n[i])]==0)
{
c[digit(n[i])]=1;
cnt++;
}
}
vector<ll> ndp(17,0);
for(int j=0;j<=16;j++)
{
if(j>0) add(ndp[j],dp[j]*j);
if(j<16) add(ndp[j+1],dp[j]*(16-j));
}
if(i>0) add(ndp[1],15);
for(int j=(i==0);j<digit(n[i+1]);j++)
{
if(c[j]==1) add(ndp[cnt],1);
else add(ndp[cnt+1],1);
}
dp=ndp;
}
add(dp[cnt+(c[digit(n[sz])]==0)],1);
cout << dp[k] << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "/debug.h"
#else
#define db(...)
#endif
#define all(v) v.begin(), v.end()
#define pb push_back
using ll = long long;
const int NAX = 2e5 + 5, MOD = 1000000007;
// int a[101][101];
#define int __int128_t
#define ll __int128_t
__int128_t myabs(__int128_t x)
{
if (x < 0)
return -x;
return x;
}
int gcd(int a, int b, int &x, int &y)
{
if (b == 0)
{
x = 1;
y = 0;
return a;
}
int x1, y1;
int d = gcd(b, a % b, x1, y1);
x = y1;
y = x1 - y1 * (a / b);
return d;
}
bool find_any_solution(int a, int b, int c, int &x0, int &y0, int &g)
{
g = gcd(myabs(a), myabs(b), x0, y0);
if (c % g)
{
return false;
}
x0 *= c / g;
y0 *= c / g;
if (a < 0)
x0 = -x0;
if (b < 0)
y0 = -y0;
return true;
}
std::ostream &
operator<<(std::ostream &dest, __int128_t value)
{
std::ostream::sentry s(dest);
if (s)
{
__uint128_t tmp = value < 0 ? -value : value;
char buffer[128];
char *d = std::end(buffer);
do
{
--d;
*d = "0123456789"[tmp % 10];
tmp /= 10;
} while (tmp != 0);
if (value < 0)
{
--d;
*d = '-';
}
int len = std::end(buffer) - d;
if (dest.rdbuf()->sputn(d, len) != len)
{
dest.setstate(std::ios_base::badbit);
}
}
return dest;
}
void solveCase()
{
int32_t n1, s1, k1;
cin >> n1 >> s1 >> k1;
ll n = n1, s = s1, k = k1;
// cin >> n >> s >> k;
db(n, s, k);
ll x0, y0, g;
if (find_any_solution(n, -k, s, x0, y0, g))
{
if (!(n * x0 + (-k) * y0) == s)
{
exit(1);
}
db(n * x0 + (-k) * y0, s);
db(x0, y0, g);
ll dx = k / g;
ll dy = n / g;
// assert(dx > 0 && dy > 0);
if (!(dx > 0 && dy > 0))
{
exit(1);
}
db(dx, dy);
bool ok = false;
if (x0 < 0)
{
auto t = (-x0 + dx - 1) / dx;
db("a", x0, y0, t);
x0 += t * dx;
y0 += t * dy;
db(x0, y0);
}
else if (x0 >= dx)
{
auto t = x0 / dx;
db("a", x0, y0, t);
x0 -= t * dx;
y0 -= t * dy;
db(x0, y0);
ok = true;
}
if (y0 < 0)
{
auto t = (-y0 + dy - 1) / dy;
db("a", x0, y0, t);
x0 += t * dx;
y0 += t * dy;
db(x0, y0);
}
// else if (y0 >= dy)
// {
// auto t = y0 / dy;
// db("a", x0, y0, t);
// x0 -= t * dx;
// y0 -= t * dy;
// db(x0, y0);
// }
if (y0 > 0)
cout << y0 << '\n';
else
cout << "-1\n";
}
else
cout << "-1\n";
}
int32_t main()
{
#ifndef LOCAL
ios_base::sync_with_stdio(0);
cin.tie(0);
#endif
int32_t t = 1;
cin >> t;
for (int i = 1; i <= t; ++i)
solveCase();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define R register
#define LL long long
inline int read() {
int x = 0, f = 1; char a = getchar();
for(; a > '9' || a < '0'; a = getchar()) if(a == '-') f = -1;
for(; a <= '9' && a >= '0'; a = getchar()) x = x * 10 + a - '0';
return x * f;
}
inline int gcd(int x, int y) { return y ? gcd(y, x % y) : x; }
void exgcd(int a, int b, LL &x, LL &y) {
if(! b) {
x = 1; y = 0; return ;
}
exgcd(b, a % b, x, y);
int t = x; x = y; y = t - a / b * y;
}
void solve() {
int n = read(), s = read(), k = read();
int g = gcd(k, n);
if((n - s) % g != 0) { cout << -1<<endl; return ; }
LL x, y;
//k /= g; n /= g;
//k /= g; n /= g;
exgcd(k, n, x, y);
//cout << k << ' ' << n << ' ' << x << ' ' << y << endl;
x *= (n - s) / g;
x = (x * g % n + n) % n;
x /= g;
cout << x << endl;
}
signed main() {
#ifdef IN
//freopen(".in", "r", stdin);
//freopen(".out", "w", stdout);
#endif
int Case = read();
while(Case --) solve();
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define INF 1000000000
#define MOD 998244353
using ll=long long;
using Graph=vector<vector<int>>;
int n=1;
vector<int> tree;
int solve(int a,int b,int i,int left,int right){
if(b<=left||right<=a){
return 0;
}
if(a<=left&&right<=b){
return tree[i];
}
int x=solve(a,b,2*i+1,left,(left+right)/2);
int y=solve(a,b,2*i+2,(left+right)/2,right);
int res=x^y;
return res;
}
void update(int i,int x){
i+=n-1;
tree[i]=x;
while(i>0){
i=(i-1)/2;
tree[i]=tree[2*i+1]^tree[2*i+2];
}
}
int main(){
int N,Q;
cin>>N>>Q;
vector<int> A(N);
for(int i=0;i<N;i++){
cin>>A[i];
}
while(n<N){
n<<=1;
}
tree.resize(2*n-1,0);
for(int i=0;i<N;i++){
tree[i+n-1]=A[i];
}
for(int i=n-2;i>=0;i--){
tree[i]=tree[2*i+1]^tree[2*i+2];
}
for(int i=0;i<Q;i++){
int T,X,Y;
cin>>T>>X>>Y;
if(T==1){
X--;
int y=tree[X+n-1];
y^=Y;
update(X,y);
}else{
int x=solve(X-1,Y,0,0,n);
cout<<x<<'\n';
}
}
}
| #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<map>
#include<bitset>
#include<deque>
#include<cstdlib>
#include<set>
#include<ctime>
#define ll long long
#define mp make_pair
using namespace std;
ll read()
{
ll x=0,f=1;
char c=getchar();
while(c>'9'||c<'0')
{
if(c=='-') f=-1;
c=getchar();
}
while(c>='0'&&c<='9')
{
x=x*10+c-'0';
c=getchar();
}
return f*x;
}
int mts[12];
ll A,B,C;
ll pow_mod(ll a,ll b,ll mod)
{
ll ans=1;
while(b){
if(b&1) ans=ans*a%mod;
a=a*a%mod;
b>>=1;
}
return ans;
}
int main()
{
for(int i=2;i<=9;++i)
{
int num=1,tmp=i;
num++;
tmp=tmp*i%10;
while(tmp!=i)
{
num++;
tmp=tmp*i%10;
}
mts[i]=num;
// cout<<i<<": "<<mts[i]<<'\n';
}
A=read(),B=read(),C=read();
if(A%10==1) cout<<1<<'\n';
else if(A%10==0) cout<<0<<'\n';
else
{
ll tmp=pow_mod(B,C,mts[A%10]-1);
// cout<<tmp<<'\n';
if(!tmp) tmp+=mts[A%10]-1;
cout<<pow_mod(A,tmp,10)<<'\n';
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int index(int y, int x)
{
return y * 16 + x;
}
int search(bitset<256> ttm, int H, int W, int a, int b)
{
if (a == 0 && b == 0)
return 1;
int ans = 0;
int cx = 0, cy = 0;
bool flag = true;
for (int y = 0; y < H && flag; y++)
{
for (int x = 0; x < W && flag; x++)
{
if (!ttm[index(y, x)])
{
cx = x;
cy = y;
flag = false;
}
}
}
ttm.set(index(cy, cx));
if (b != 0)
ans += search(ttm, H, W, a, b - 1);
if (a != 0)
{
if (cx + 1 < W && !ttm[index(cy, cx + 1)])
{
ttm.set(index(cy, cx + 1));
ans += search(ttm, H, W, a - 1, b);
ttm.reset(index(cy, cx + 1));
}
if (cy + 1 < H && !ttm[index(cy + 1, cx)])
{
ttm.set(index(cy + 1, cx));
ans += search(ttm, H, W, a - 1, b);
ttm.reset(index(cy + 1, cx));
}
}
return ans;
}
void solve(long long H, long long W, long long A, long long B)
{
bitset<256> ttm(0);
cout << search(ttm, H, W, A, B) << endl;
}
// Generated by 2.2.0 https://github.com/kyuridenamida/atcoder-tools (tips: You use the default template now. You can remove this line by using your custom template)
int main()
{
long long H;
scanf("%lld", &H);
long long W;
scanf("%lld", &W);
long long A;
scanf("%lld", &A);
long long B;
scanf("%lld", &B);
solve(H, W, A, B);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, l, n) for (int i = (int)(l); i < (int)(n); i++)
#define drep(i, n, l) for (int i = (int)(n); i > (int)(l); i--)
#define INF INT_MAX
#define def (200007)
#define MOD (1000000007)
typedef vector<int> veci;
typedef vector<vector<int>> Veci;
typedef vector<int64_t> vecit;
typedef vector<vector<int64_t>> Vecit;
typedef int64_t intt;
typedef vector<vector<double>> Vecd;
typedef vector<double> vecd;
typedef pair<int, int> P;
/*veci のときdist.resize(N) Veciのとき2回resize;*/
//powはdouble型
Veci dp;
int sum=0;
int H,W,A,B;
void dfs(int nx,int index){
if(H*W==(2*index+B)){
sum++;
return;
}
if(nx>=H*W)return;
if(dp[nx/W][nx%W]!=0){
return dfs(nx+1,index);
}
if(nx<H*W)dp[nx/W][nx%W]=index+3;
if((nx+1)%W>(nx%W)){
if(dp[(nx+1)/W][(nx+1)%W]==0){
dp[(nx+1)/W][(nx+1)%W]=2;
dfs(nx+2,index+1);
dp[(nx+1)/W][(nx+1)%W]=0;
}
}
if((nx+W)/W<H){
if(dp[(nx+W)/W][(nx+W)%W]==0){
dp[(nx+W)/W][(nx+W)%W]=1;
dfs(nx+1,index+1);
dp[(nx+W)/W][(nx+W)%W]=0;
}
}
dp[nx/W][nx%W]=0;
}
int next_combination(int sub){
int x=sub&-sub,y=sub+x;
return(((sub&~y)/x)>>1)|y;
}
//3*10^6 10!
//16C8 1.2*10^4
//0何もない 1:1*1の畳 2:1*2の畳
int main(){
cin>>H>>W>>A>>B;
dp.resize(H);
if(B>0){
int bit = (1<<B)-1;
for(;bit<(1<<H*W);bit=next_combination(bit)){
rep(i,0,H){
dp[i].assign(W,0);
}
rep(i,0,H*W){
if(bit & (1<<i)){
rep(j,1,H+1){
if(i<W*j){
dp[j-1][i-W*(j-1)]=1;
break;
}
}
}
}
int nex=0;
rep(i,0,H*W){
if(dp[i/W][i%W] == 0 ){
nex=i;
break;
}
}
dfs(nex,0);
}
}else{
rep(i,0,H)dp[i].assign(W,0);
dfs(0,0);
}
cout<<sum<<endl;
} |
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define swap(a,b) (a^=b^=a^=b)
#define ll long long int
#define ull unsigned long long int
#define uint unsigned int
#define format(a) memset(a,0,sizeof(a))
#define rep(x,y,z) for(int x=y;x<=z;x++)
#define dec(x,y,z) for(int x=y;x>=z;x--)
#define mst(x) memset(x,0,sizeof(x))
const int maxn=2e5+20;
inline int read()
{
int ans=0;
char last=' ',ch=getchar();
while(ch<'0'|ch>'9')last=ch,ch=getchar();
while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
if(last=='-')ans=-ans;
return ans;
}
int n;
int a[maxn];
vector<int>save[maxn];
int main()
{
n=read();
rep(i,1,n)a[i]=read();
ll ans=0;
rep(i,1,n)
{
int cur=a[i]%200;
ans+=save[cur].size();
save[cur].push_back(a[i]);
}
cout<<ans<<endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main()
{
long long n, p = 0, a[2500] = {0};
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> p;
a[p % 200]++;
}
long long ans = 0;
for (int i = 0; i < 200; i++)
{
ans += (a[i] * (a[i] - 1)) / 2;
}
cout << ans;
} |
/*Lucky_Glass*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=2e5+10;
typedef long long llong;
int dv[N],prm[N],nprm;
void init(){
for(int i=2;i<N;i++){
if(!dv[i]){
dv[i]=i;
prm[++nprm]=i;
}
for(int j=1;j<=nprm && prm[j]*i<N;j++){
dv[prm[j]*i]=prm[j];
if(i%prm[j]==0) break;
}
}
}
int main(){
init();
int n;scanf("%d",&n);
llong ans=0;
for(int i=1;i<=n;i++){
int tmp=i,las=dv[i],ncnt=0;
llong now=1;
while(tmp>1){
if(las==dv[tmp]) ncnt++;
else{
now*=(ncnt+1ll)*(ncnt+2ll)/2;
las=dv[tmp];
ncnt=1;
}
tmp/=dv[tmp];
}
now*=(ncnt+1ll)*(ncnt+2ll)/2;
ans+=now;
}
printf("%lld\n",ans);
return 0;
} | #define LOCAL
#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,s,n) for (int i = (ll)s; i < (ll)n; i++)
#define rrep(i,n,e) for (int i = (ll)n; i > (ll)e; i--)
#define ll long long
#define ld long double
#define pb push_back
#define eb emplace_back
#define All(x) x.begin(), x.end()
#define Range(x, i, j) x.begin() + i, x.begin() + j
// #define M_PI 3.14159265358979323846 // CF
#define deg2rad(deg) ((((double)deg)/((double)360)*2*M_PI))
#define rad2deg(rad) ((((double)rad)/(double)2/M_PI)*(double)360)
#define Find(set, element) set.find(element) != set.end()
#define Decimal(x) cout << fixed << setprecision(10) << x << endl; // print Decimal number 10 Rank
#define endl "\n"
#define Case(x) printf("Case #%d: ", x); // gcj
typedef pair<int, int> PI;
typedef pair<ll, ll> PLL;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<vector<vector<ll>>> vvvl;
typedef vector<PI> vpi;
typedef vector<vector<PI>> vvpi;
typedef vector<PLL> vpl;
typedef vector<vector<PLL>> vvpl;
typedef vector<char> vch;
typedef vector<vector<char>> vvch;
constexpr ll INF = 1001002003004005006ll;
constexpr int n_max = 2e5+10;
template<class T>
inline bool chmax(T &a, T b) { if(a<b) { a=b; return true; } return false; };
template<class T>
inline bool chmin(T &a, T b) { if(a>b) { a=b; return true; } return false; };
template<class T, class U>
T POW(T x, U n) {T ret=1; while (n>0) {if (n&1) {ret*=x;} x*=x; n>>=1;} return ret;};
// debug
template <typename A, typename B>
string to_string(pair<A, B> p);
string to_string(const string &s) {return '"' + s + '"';};
string to_string(const char c) {return to_string((string) &c);};
string to_string(bool b) {return (b ? "true" : "false");};
template <size_t N>
string to_string(bitset<N> v){
string res = "";
for(size_t i = 0; i < N; i++) res += static_cast<char>('0' + v[i]);
return res;
};
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for(const auto &x : v) {
if(!first) res += ", ";
first = false; res += to_string(x);
}
res += "}";
return res;
};
template <typename A, typename B>
string to_string(pair<A, B> p){return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}
void debug_out() {cerr << endl;};
template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); };
void LINE_OUT() {
cout << "--------------" << endl;
};
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define LINE LINE_OUT();
#else
#define debug(...) 71
#define LINE 71;
#endif
void print() { cout << endl; }
template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
cout << head;
if (sizeof...(tail) != 0) cout << " ";
print(forward<Tail>(tail)...);
};
template <class T>
void print(vector<T> &vec) {
for (auto& a : vec) {
cout << a;
if (&a != &vec.back()) cout << " ";
}
cout << endl;
};
template <class T>
void print(vector<vector<T>> &df) {
for (auto& vec : df) {
print(vec);
}
};
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N, M; cin >> N >> M;
vi mem(1005);
rep(i,0,N) {
int a; cin >> a; mem[a]++;
}
rep(i,0,M) {
int b; cin >> b; mem[b]--;
}
vi ans;
rep(i,0,1005) {
if (mem[i] != 0) ans.pb(i);
}
rep(i,0,ans.size()) {
cout << ans[i];
if (i < ans.size()-1) cout << " ";
}
cout << endl;
return 0;
};
|
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
// #define endl '\n'
#define int long long int
#define rep(i, a, b) for (int i = a; i < b; i++)
#define revrep(i, a, b) for (int i = a; i >= b; i--)
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
ostream &operator<<( ostream &output, const pii &p ) { output << p.first << " " << p.second;return output; }
istream &operator>>( istream &input, pii &p ) { input >> p.first >> p.second;return input; }
template<typename T>
void inline println(vector<T> args){ for(T i: args)cout<<i<<" ";cout<<endl; }
void amax(int& a, int b) { a = max(a, b); }
void amin(int& a, int b) { a = min(a, b); }
int ceilInt(int a, int b) {
if(!b) return 0;
if(a%b) return a/b + 1;
return a/b;
}
int INF = 1e18;
int MOD = 998244353;
void solve()
{
int a, b; cin>>a>>b;
int negs = 0, poss = 0;
vi pos, neg;
rep(i, 1, a+1) negs += i, pos.pb(i);
rep(i, 1, b+1) poss += i, neg.pb(i);
if(negs > poss) {
neg[neg.size()-1] += abs(negs-poss);
} else {
pos[pos.size()-1] += abs(negs-poss);
}
for(int i: pos) cout<<i<<" ";
for(int i: neg) cout<<-i<<" ";
cout<<endl;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
// cin>>t;
rep(i, 0, t)
{
// cout<<"Case #"<<i+1<<": ";
solve();
}
return 0;
} | #include<iostream>
#include<string.h>
using namespace std;
int main(){
long long h,w;
long long a[3000][3000];
long long s[3000][3000];
long long smax; //Takahashi - Aoki
long long posx = 0, posy = 0;
long long mode = 1; //1 xx 2 xy 3 yx 4 yy 5 xのaoki最適解 6 yのaoki最適解
long long temp[50];
long long pt=0;
long long mode5,mode6;
string buf;
cin >> h>> w;
for (int i = 0; i < (h+5); i++){
for (int j = 0; j < (w + 5); j++){
a[i][j] = 0;
}
}
s[1][1] = 0;
for (int i = 1; i <= h; i++){
cin >> buf;
for (int j = 1; j <= w;j++){
if (buf[j-1] == '+'){
a[i][j] = 1;
} else{
a[i][j] = -1;
}
}
}
s[h][w] = 0;
for(int i = h; i >=1; i--){
for (int j = w; j >= 1; j--){
if ((i == h) && (j == w)){
} else {
if (((i + j) % 2) == 0) { //高橋
if (i == h){
s[i][j] = s[i][j+1] + a[i][j+1];
} else if (j == w){
s[i][j] = s[i+1][j] + a[i+1][j];
} else{
s[i][j] = max(s[i+1][j]+a[i+1][j],s[i][j+1]+a[i][j+1]);
}
} else{ //青木
if (i == h){
s[i][j] = s[i][j+1] - a[i][j+1];
} else if (j == w){
s[i][j] = s[i+1][j] - a[i+1][j];
} else{
s[i][j] = min(s[i+1][j]-a[i+1][j],s[i][j+1]-a[i][j+1]);
}
}
}
}
}
if (s[1][1] == 0){
cout << "Draw";
} else if(s[1][1] > 0){
cout << "Takahashi";
} else{
cout << "Aoki";
}
} |
#include <bits/stdc++.h>
using namespace std;
int main(){
string s;
cin>>s;
reverse(s.begin(),s.end());
unsigned int v=s.size();
for(unsigned int i=0;i<v;i++){
if(s[i]=='6'){
s[i]='9';
continue;
}
if(s[i]=='9'){
s[i]='6';
continue;
}
}
cout<<s<<endl;
return 0;
} | #include <iostream>
#include <stdio.h>
#include <climits>
#include <string>
//martin02
using namespace std;
int main() {
string s;
cin >> s;
string s2;
for (int i = s.length() - 1; i >= 0; i--) {
if (s.at(i) == '0') {
s2 += '0';
}
else if (s.at(i) == '1') {
s2 += '1';
}
else if (s.at(i) == '6') {
s2 += '9';
}
else if (s.at(i) == '8') {
s2 += '8';
}
else {
s2 += '6';
}
}
cout << s2 << endl;
return 0;
} |
#include <bits/stdc++.h>
#define rep(i,n) for(ll i=0;i<(n);i++)
#define rrep(i,n) for(ll i = 1; i <= (n); ++i)
#define drep(i,n) for(ll i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define all(v) v.begin(),v.end()
#define len(x) (ll)(x).length()
#define maxs(x,y) x = max(x,y)
#define mins(x,y) x = min(x,y)
#define pb push_back
#define pf push_front
#define sz(x) (ll)(x).size()
#define v(T) vector<T>
#define vv(T) vector<vector<T>>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vd> vvd;
typedef vector<P> vp;
ll gcd(ll a,ll b){if(a%b==0){return b;}else{return(gcd(b,a%b));}}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
const int INF=1e9;
const ll MX = 1e18;
const ll mod=INF+7;
const int di[] = {-1,0,1,0};
const int dj[] = {0,-1,0,1};
const double PI=acos(-1);
const string tr="abcdefghijklmnopqrstuvwxyz";
#define dame { puts("-1"); return 0;}
#define yn {puts("Yes");}else{puts("No");}
#define YN {puts("YES");}else{puts("NO");}
ll llpow(ll n,ll i){if(i==0){return 1;}ll cnt=n;for(ll j=0;j<i-1;j++){n*=cnt;}return n;}
bool ip/*is_prime*/(long long N) {if (N == 1) return false;for (long long i = 2; i * i <= N; ++i) {if (N % i == 0) return false;}return true;}
int digit(ll N) {int ans = 0;while (N) {++ans;N /= 10;}return ans;}
vector<pair<ll,ll>> pf/*prime_factorize*/(ll n){vector<pair<ll,ll>> res;for(ll a=2;a*a<=n;a++){if(n%a!=0) continue;ll ex=0;while(n%a==0){ex++;n/=a;}res.pb({a,ex});}if(n!=1) res.pb({n,1});return res;}
vector<ll> div/*divisor*/(ll n){ vector<ll> res={1};for(ll a=2;a*a<=n;a++){if(n%a!=0) continue;ll b=n/a;res.pb(b);if(b!=a) res.pb(a);}sort(all(res));return res;}
struct mint {
ll x; // typedef long long ll;
mint(ll x=0):x((x%mod+mod)%mod){}
mint operator-() const { return mint(-x);}
mint& operator+=(const mint a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint a) {
if ((x += mod-a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
mint operator+(const mint a) const { return mint(*this) += a;}
mint operator-(const mint a) const { return mint(*this) -= a;}
mint operator*(const mint a) const { return mint(*this) *= a;}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t>>1);
a *= a;
if (t&1) a *= *this;
return a;
}
// for prime mod
mint inv() const { return pow(mod-2);}
mint& operator/=(const mint a) { return *this *= a.inv();}
mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
struct UnionFind {
vector<int> d;
UnionFind(int n): d(n,-1) {}
int root(int x) {
if (d[x] < 0) return x;
return d[x] = root(d[x]);
}
bool unite(int x, int y) {
x = root(x); y = root(y);
if (x == y) return false;
if (d[x] > d[y]) swap(x,y);
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) { return root(x) == root(y);}
int size(int x) { return -d[root(x)];}
};
mint f2(ll n) {if (n == 0) return 1;mint x = f2(n/2);x *= x;if (n%2 == 1) x *= 2;return x;}
mint choose(int n,int a){mint x=1,y=1;rep(i,a){x*=n-i;y*=i+1;}return x/y;}
ll xs/*xorsum nまでのxorの和*/(ll n){ll cnt=(n+1)/2;ll ans=cnt%2;if(n%2==0) ans^=n;return ans;}
ll Fa/*Factorial nの階乗*/(ll n){ll ans=1;rrep(i,n){ans*=i;}return ans;}
int main() {
int m,h;cin>>m>>h;
if(h%m==0) yn;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int m,h;
cin >> m >> h;
if(h % m == 0){
cout << "Yes" << endl;
}else{
cout << "No" << endl;
}
} |
#include<bits/stdc++.h>
using namespace std;
#define LL __int128
long long T,t;
long long X,Y,P,Q;
long long mod1,mod2;
long long a1,a2,b1,b2;
inline long long qr(){
char a=0;long long w=1,x=0;
while(a<'0'||a>'9'){if(a=='-')w=-1;a=getchar();}
while(a<='9'&&a>='0'){x=(x<<3)+(x<<1)+(a^48);a=getchar();}
return w*x;
}
LL exgcd(LL a,LL b,LL &x,LL &y){
if(!b){x=1,y=0;return a;}
LL d=exgcd(b,a%b,y,x);
y-=a/b*x;
return d;
}
LL mul(LL a,LL b,LL p){
LL ans=0;
for(;b;b>>=1){
if(b&1)ans=(ans+a)%p;
a=(a+a)%p;
}
return ans%p;
}
LL excrt(){
LL x,y,M=b1,ans=a1;
LL a=M,b=b2,c=(a2-ans%b+b)%b;
LL gcd=exgcd(a,b,x,y);
LL bg=b/gcd;
if(c%gcd!=0) return -1;
x=mul(x,c/gcd,bg);
ans+=x*M;M*=bg;
ans=(ans%M+M)%M;
return (ans%M+M)%M;
}
int main(){
T=qr();
while(T--){
X=qr();Y=qr();P=qr();Q=qr();
b1=(X+Y)<<1ll;b2=P+Q;
LL ans=2e18;
long long opl=0;
for(long long i=0;i<Y;i++)
for(long long j=P;j<P+Q;j++){
a1=i+X;a2=j;
LL ans1=excrt();
if(ans1==-1) continue;
opl=1;
ans=min(ans1,ans);
}
if(opl)printf("%lld\n",(long long)ans);
else printf("infinity\n");
}
return 0;
} | //#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <stdio.h>
//end of libraries ;
// freopen("input.in","r",stdin);
// freopen("output.out","w",stdout);
#define lnf 3999999999999999999
#define inf 999999999
#define PI 3.14159265359
#define endl "\n"
#define fi first
#define se second
#define pb push_back
#define ll long long
#define all(c) (c).begin(),(c).end()
#define sz(c) (int)(c).size()
#define mkp(a,b) make_pair(a,b)
#define rsz(a,n) a.resize(n)
#define pii pair <int,int>
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
const int N = 2441;
using namespace std;
int n , m , value;
ll a[ N ][ N ] , ro[ N ] , col[ N ] , mod = 1e9+7 ;
map <ll,ll> dia;
char ch;
int main(){
fcin;
cin >> n >> m ;
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < m ; j++) {
cin >> ch;
a[i][j] = (ch == '.');
}
}
ll ans = 0;
for(int i = 0 ; i < n ; i++){
for(int j = 0 ; j < m ; j++){
if(!a[i][j]) {
dia[i-j] = 0;
ro[i] = 0;
col[j] = 0;
continue;
}
if(!i && !j) {
dia[0] = 1;
col[j] = 1;
ro[i] = 1;
}
else {
value = dia[ i - j ] % mod;
value += col[j] % mod; value %= mod;
value += ro[i] % mod; value %= mod;
dia[i - j] += value; dia[i-j] %= mod;
col[j] += value; col[j] %= mod;
ro[i] += value; ro[i] %= mod;
if(i == n - 1 && j == m - 1) ans = value;
}
}
}
cout << ans << "\n";
return 0;
} |
#define _DEBUG
#include "bits/stdc++.h"
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0,a1,a2,a3,a4,x,...) x
#define debug_1(x1) cout<<#x1<<": "<<x1<<endl
#define debug_2(x1,x2) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<endl
#define debug_3(x1,x2,x3) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<endl
#define debug_4(x1,x2,x3,x4) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<endl
#define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<", "#x5<<": "<<x5<<endl
#ifdef _DEBUG
#define debug(...) CHOOSE((__VA_ARGS__,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__)
#else
#define debug(...)
#endif
#define rep(index,num) for(int index=0;index<(int)num;index++)
#define rep1(index,num) for(int index=1;index<=(int)num;index++)
#define brep(index,num) for(int index=(int)num-1;index>=0;index--)
#define brep1(index,num) for(int index=(int)num;index>0;index--)
#define scan(argument) cin>>argument
#define prin(argument) cout<<argument<<endl
#define kaigyo cout<<endl
#define eps 1e-7
#define mp(a1,a2) make_pair(a1,a2)
#define ALL(a) (a).begin(),(a).end()
#define rALL(a) (a).rbegin(),(a).rend()
#define puba(a) emplace_back((a))
typedef long long ll;
typedef long double ld;
using namespace std;
typedef pair<ll,ll> pll;
typedef pair<int,int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
ll INFl=(ll)1e+18+1;
int INF=1e+9+1;
int main(){
ll n;
scan(n);
ll mink=0,maxk=2*INF;
while(mink+1<maxk){
ll midk=(mink+maxk)/2+(mink+maxk)%2;
if((midk-1)*midk<=2*(n+1)){
mink=midk;
}
else{
maxk=midk;
}
}
prin(n-mink+2);
return 0;
}
| #include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <queue>
using ll=long long;
using namespace std;
ll n;
ll bs(ll st, ll ed){
//cout<<st<< " "<<ed<<endl;
if(ed-st<=1){
return st;
}
ll md=(st+ed)/2;
if(md*(md+1)/2<=n+1){
return bs(md,ed);
}else{
return bs(st,md);
}
}
int main(){
cin>>n;
ll ans=bs(1,1420000000);
cout<<n-ans+1<<endl;
return 0;
} |
/**
* author: otera
**/
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60;
#define rep(i, n) for(int i = 0; i < n; ++ i)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
void solve() {
int n, m; cin >> n;
vector<int> x(n), y(n);
rep(i, n) {
cin >> x[i] >> y[i];
}
cin >> m;
vector<int> r(m + 1, 0);
vector<int> sx(m + 1, 1), sy(m + 1, 1);
vector<int> nx(m + 1, 0), ny(m + 1, 0);
rep(i, m) {
int op; cin >> op;
r[i + 1] = r[i];
sx[i + 1] = sx[i], sy[i + 1] = sy[i];
nx[i + 1] = nx[i], ny[i + 1] = ny[i];
if(op == 1) {
r[i + 1] ^= 1;
swap(sx[i + 1], sy[i + 1]);
swap(nx[i + 1], ny[i + 1]);
sy[i + 1] *= (-1); //?
ny[i + 1] *= (-1); //?
} else if(op == 2) {
r[i + 1] ^= 1;
swap(sx[i + 1], sy[i + 1]);
swap(nx[i + 1], ny[i + 1]);
sx[i + 1] *= (-1); //?
nx[i + 1] *= (-1); //?
} else if(op == 3) {
sx[i + 1] *= (-1);
nx[i + 1] *= (-1);
int p; cin >> p;
nx[i + 1] += 2 * p;
} else {
sy[i + 1] *= (-1);
ny[i + 1] *= (-1);
int p; cin >> p;
ny[i + 1] += 2 * p;
}
}
int q; cin >> q;
rep(_, q) {
int a, b; cin >> a >> b;
-- b;
int X = x[b], Y = y[b];
if(r[a]) swap(X, Y);
// cerr << a << " " << sy[a] << " " << ny[a] << "\n";
cout << X * sx[a] + nx[a] << " " << Y * sy[a] + ny[a] << "\n";
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
//cout << fixed << setprecision(20);
//int t; cin >> t; rep(i, t)solve();
solve();
return 0;
} | #include<bits/stdc++.h>
#define int long long
using namespace std;
vector <vector<int> > multi(vector<vector<int> >&a,vector<vector<int> >&b,vector<vector<int> >&ans)
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
for(int k=0;k<3;k++)
{
ans[i][j]+=a[i][k]*b[k][j];
}
}
}
return ans;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t=1;
while(t--)
{
int n;
cin>>n;
pair<int,int> co[n];
for(int i=0;i<n;i++)
{
cin>>co[i].first>>co[i].second;
}
int m;
cin>>m;
vector<vector<vector<int> > > op(m+100,vector<vector<int> >(3,vector<int>(3,0)));
op[0][0][0]=1;
op[0][1][1]=1;
op[0][2][2]=1;
for(int i=1;i<=m;i++)
{
int k;
cin>>k;
if(k==1)
{
vector<vector<int> > temp(3,vector<int>(3,0));
temp[0][1]=1;
temp[1][0]=-1;
temp[2][2]=1;
multi(temp,op[i-1],op[i]);
}
if(k==2)
{
vector<vector<int> > temp(3,vector<int>(3,0));
temp[0][1]=-1;
temp[1][0]=1;
temp[2][2]=1;
multi(temp,op[i-1],op[i]);
}
if(k==3)
{
int p;
cin>>p;
vector<vector<int> > temp(3,vector<int>(3,0));
temp[0][0]=-1;
temp[1][1]=1;
temp[2][2]=1;
temp[0][2]=2*p;
multi(temp,op[i-1],op[i]);
}
if(k==4)
{
int p;
cin>>p;
vector<vector<int> > temp(3,vector<int>(3,0));
temp[0][0]=1;
temp[1][1]=-1;
temp[2][2]=1;
temp[1][2]=2*p;
multi(temp,op[i-1],op[i]);
}
}
int q;
cin>>q;
while(q--)
{
int a,b;
cin>>a>>b;
b--;
int x,y;
x=op[a][0][0]*co[b].first+op[a][0][1]*co[b].second+op[a][0][2];
y=op[a][1][0]*co[b].first+op[a][1][1]*co[b].second+op[a][1][2];
cout<<x<<' '<<y<<endl;
}
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
#define all(arr) arr.begin(),arr.end()
#define f first
#define s second
#define debug1(x) cout<<x<<"\n"
#define debug2(x,y) cout<<x<<" "<<y<<"\n"
#define debug3(x,y,z) cout<<x<<" "<<y<<" "<<z<<"\n"
#define nl cout<<"\n";
#define pq priority_queue
#define inf 0x3f3f3f3f
#define test cout<<"abcd\n";
#define pi pair<int,int>
#define pii pair<int,pi>
#define pb push_back
#define gc getchar_unlocked
#define fo(i,n) for(i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define ll long long
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define ss(s) scanf("%s",s)
#define pl(x) printf("%lld\n",x)
#define ps(s) printf("%s\n",s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define MOD 1000000007
#define space ' '
#define kick(t) cout << "Case #" << t << ":" << endl;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
template <typename T>
void input(vector<T> &arr,lli n) {
T temp;
for(lli i=0;i<n;i++) cin>>temp, arr.push_back(temp);
}
template <typename T>
void output(vector<T> arr) {
for(auto x:arr) cout<<x<<" ";
cout<<endl;
}
template <typename T>
void input_set(set<T> &arr,lli n) {
T temp;
for(lli i=0;i<n;i++) cin>>temp, arr.insert(temp);
}
lli mul(lli a, lli b) {
return (a%MOD*b%MOD)%MOD;
}
lli power(lli a,lli b) {
lli ans = 1;
while(b > 0) {
if(b&1)
ans = mul(ans, a);
a = mul(a,a);;
b >>= 1;
}
return ans;
}
int divisible(string num)
{
int n = num.length();
int sum = accumulate(begin(num), end(num), 0) - '0' * 1;
if (sum % 3 == 0)
return 0;
if (n == 1)
return -1;
for (int i = 0; i < n; i++)
if (sum % 3 == (num[i] - '0') % 3)
return 1;
if (n == 2)
return -1;
return 2;
}
void solve(){
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string s;
cin >> s;
cout << divisible(s);
}
| #include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <map>
#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
#include <random>
#include <stack>
#include <set>
#include <list>
#include <unordered_set>
#define bug(x) cout<<"zdongdebug1: "<<x<<endl;
#define bug2(x, y) cout<<"zdongdebug2: "<<x<<" "<<y<<endl;
#define bug3(x, y, z) cout<<"zdongdebug3: "<<x<<" "<<y<<" "<<z<<endl;
using namespace std;
typedef long long ll;
const int maxn = 500005;
const int mod = 998244353;
int c[3];
int main() {
#ifdef suiyuan2009
freopen("/Users/suiyuan2009/CLionProjects/icpc/input.txt", "r", stdin);
//freopen("/Users/suiyuan2009/CLionProjects/icpc/output.txt", "w", stdout);
#endif
string s;
cin>>s;
int sum = 0;
for(int i=0;i<s.size();i++){
int tt = s[i] - '0';
sum = (sum + tt)%3;
c[tt%3]++;
}
if(sum==0){
cout<<0<<endl;
} else if(sum==1){
if(s.size()>1&&c[1]>0) {
cout<<1<<endl;
} else if(s.size()>2&&c[2]>1){
cout<<2<<endl;
} else cout<<-1<<endl;
} else {
if(s.size()>1&&c[2]>0) {
cout<<1<<endl;
} else if(s.size()>2&&c[1]>1){
cout<<2<<endl;
} else cout<<-1<<endl;
}
return 0;
} |
#include<bits/stdc++.h>
#include<tuple>
typedef long long ll;
#define INF 99999999999999
#define mod 1000000007
#define eps 1e-9
using namespace std;
typedef pair<ll,ll>du;
ll a,b,can=1,ans=0;
ll cnt[200],now[205];
string s,cmp;
vector<string>mut;
void chan(ll x){
string res;
while(x!=0){
res+=(char)(x%10+'0');
x/=10;
}
reverse(res.begin(),res.end());
while(res.size()<3)res="0"+res;
mut.push_back(res);
return ;
}
int main(){
ios::sync_with_stdio(0);
cin>>s;
if(s=="84"||s=="27"||s=="29"||s=="65"){
cout<<"Yes\n";
return 0;
}
cmp=s;
while(cmp.size()<3)cmp="0"+cmp;
for(ll i='0';i<='9';i++)
cnt[i]=0;
for(ll i=0;i<1000;i+=8){
chan(i);
}
for(auto k:s)
cnt[k]++;
for(auto k:mut){
can=1;
for(ll i='0';i<='9';i++)
now[i]=0;
for(auto g:k)
now[g]++;
for(ll i='0';i<='9';i++)
if(now[i]>cnt[i])
can=0;
if(k==cmp)
can=1;
ans=ans|can;
}
if(ans)
puts("Yes");
else
puts("No");
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e2+10;
int n[10];
int main(){
string s;
cin >> s;
for(int i = 0; i < s.size(); i++){
n[s[i]-'0']++;
}
if(s.size() < 2){
if((s[0]-'0')%8 == 0){
cout << "Yes" << endl;
return 0;
}
cout << "No" << endl;
return 0;
}
if(s.size() == 2){
int a = (s[0]-'0')+10*(s[1]-'0');
int b = (s[1]-'0')+10*(s[0]-'0');
if(a%8 == 0 || b%8 == 0)
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
for(int i = 104; i < 999; i+=8){
int w[10];
for(int j = 0; j < 10; j++){
w[j] = n[j];
}
bool q = true;
int a = i;
while(a!=0){
int b = a%10;
if(!w[b]){
q = false;
break;
}
w[b]--;
a/=10;
}
if(q){
cout << "Yes" << endl;
return 0;
}
}
cout << "No" << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long A[3];
cin >> A[0];
cin >> A[1];
cin >> A[2];
sort(A, A+3);
cout << A[1] + A[2] << endl;
}
| #include <bits/stdc++.h>
using namespace std;
int main(int argc, char* argv[]) {
int a, b, c;
cin >> a >> b >> c;
if(c==0) {
if(a==0) {
cout << "Aoki" << endl;
} else {
while(a!=0 && b!=0) {
a--;
if(a==0) {
cout << "Aoki" << endl;
break;
}
b--;
if(b==0) {
cout << "Takahashi" << endl;
break;
}
}
}
}
else
{
if(b==0) {
cout << "Takahashi" << endl;
} else {
while(a!=0 && b!=0) {
b--;
if(b==0) {
cout << "Takahashi" << endl;
break;
}
a--;
if(a==0) {
cout << "Aoki" << endl;
break;
}
}
}
}
return 0;
} |
#include "bits/stdc++.h"
#pragma GCC target("avx,avx2")
#pragma GCC optimize("Ofast,unroll-loops")
#define FOR(a, b, c) for(a = b; a <=c; a++)
#define ROF(a, b, c) for(a = b;a >= c; a--)
#define mem0(A) memset(A,0,sizeof(A))
#define mem1(A) memset(A,-1,sizeof(A))
#define memf(A) memset(A,false,sizeof(A))
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define GEN(T) template<typename T>
#define EB emplace_back
#define SORT(x) sort(all(x))
#define all(x) x.begin(), x.end()
#define newl '\n'
#define IOF ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
using namespace std;
using UL = unsigned long int;
using UI = unsigned int;
using ULL = unsigned long long int;
using LI = long int;
using LL = long long;
using LF = double;
using LLF = long double;
const int mod1 = 1000000007;
const int mod2 = 998244353;
const double PI = acos(-1);
typedef vector<vector<int>> adjList;
LL ans ;
adjList graph;
int vis[200001];
inline void dfs(int v,int par){
vis[v] = 1;
for(auto child : graph[v]){
if(vis[child]==0){
dfs(child,v);
}
else if(vis[child]==1){
ans++;
}
}
vis[v] = 2;
}
LL solver(LL a,LL x){
LL res = 1;
LL mods = mod2 * 1ll;
while(x>0){
if(x&1){
res = (res * a)%mods;
}
a = (a*a)%mods;
x >>= 1;
}
return res;
}
int main(){
IOF
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
int ntc=1,tc,n,i,j;
//cin>>ntc;
//cout<<fixed<<setprecision(16);
FOR(tc,1,ntc){
//cout<<"Case #"<<tc<<": ";
cin>>n;
graph.resize(n);
FOR(i,0,n-1){
cin>>j;
j--;
graph[i].PB(j);
}
FOR(i,0,n-1){
if(vis[i]==0){
dfs(i,-1);
}
}
cout<<(solver(2ll,ans)-1)%mod2;
}
} | #include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define pb(a) push_back(a)
#define vll vector<ll>
#define loop(i, n) for(ll i=1;i<=n;i++)
#define loop0(i, n) for(ll i=0;i<n;i++)
#define in(i) scanf("%lld", &i);
#define out(i) printf("%lld", i)
#define pll pair<ll, ll>
#define vpll vector<pair<ll, ll>>
#define mp(i, j) make_pair(i, j)
const ll mod = 1000000007;
const ll big = 2999999999999999999;
const ll small = -2999999999999999999;
void in2(ll& a, ll& b) { in(a); in(b); }
void in3(ll& a, ll& b, ll& c) { in(a); in(b); in(c); }
void swap(ll& a, ll& b) { a = a+b; b = a-b; a = a-b; }
void arrayIn(vll& a, ll& n) { loop0(i, n) in(a[i]); }
ll n;
vll r, g, b;
ll getAns(vll& a, vll& b)
{
ll ans = big;
for(ll e : b)
{
vll::iterator itr = lower_bound(a.begin(), a.end(), e);
if(itr == a.end())
continue;
ll na = *itr;
ans = min(ans, na-e);
}
for(ll e : a)
{
vll::iterator itr = lower_bound(b.begin(), b.end(), e);
if(itr == b.end())
continue;
ll nb = *itr;
ans = min(ans, nb-e);
}
return ans;
}
ll anotherWay(vll& a, vll& b, vll& c)
{
return getAns(a, c) + getAns(b, c);
}
void solve()
{
cin>>n;
char c;
ll ip;
loop0(i, 2*n)
{
cin>>ip>>c;
if(c == 'R')
r.pb(ip);
if(c == 'G')
g.pb(ip);
if(c == 'B')
b.pb(ip);
}
sort(r.begin(), r.end());
sort(g.begin(), g.end());
sort(b.begin(), b.end());
if(r.size()%2 == 0 && g.size()%2 == 0 && b.size()%2 == 0)
{
cout<<"0\n";
return;
}
ll ans = 0;
if(r.size()%2 == 0)
{
ans = getAns(g, b);
if(r.size())
ans = min(ans , anotherWay(g, b, r));
}
else if(g.size()%2 == 0)
{
ans = getAns(r, b);
if(g.size())
ans = min(ans, anotherWay(r, b, g));
}
else
{
ans = getAns(r, g);
if(b.size())
ans = min(ans, anotherWay(r, g, b));
}
cout<<ans<<"\n";
}
int main()
{
solve();
}
|
#include <iostream>
#include <complex>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <cassert>
#include <queue>
#include <stack>
#include <deque>
#include <random>
#include <iomanip>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> P;
typedef pair<bool,bool> fP;
typedef pair<ll,P> iP;
typedef pair<P,P> PP;
const ll MOD=1000000007;
const ll MAX_N=500010;
const ll INF=999999999999;
const int SEGLEN=1<<19;
vector<int> segtree(SEGLEN*2);
void add(int ind, int num){
ind+=SEGLEN;
while(ind){
segtree[ind]+=num;
ind/=2;
}
}
ll get(int l, int r){
l+=SEGLEN;
r+=SEGLEN;
ll res=0;
while(r>l){
if(l%2){
res+=segtree[l];
l++;
}
l/=2;
if(r%2){
res+=segtree[r-1];
}
r/=2;
}
return res;
}
int main(){
int n;
cin>>n;
vector<int> a(n);
for(int i=0;i<n;i++) cin>>a[i];
ll rev=0;
for(int j=0;j<n;j++){
rev+=j-get(0,a[j]);
add(a[j],1);
}
vector<ll> ans(n);
ans[0]=rev;
for(int i=0;i<n-1;i++){
ans[i+1]=ans[i]+n-1-2*a[i];
}
for(int i=0;i<n;i++){
cout<<ans[i]<<endl;
}
} | #include<bits/stdc++.h>
using namespace std;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args&&... args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
#define pi 3.141592653589
#define MOD 1000000007
#define to() int tt; cin>>tt; while(tt--)
#define pb push_back
#define in insert
#define mp make_pair
#define ff first
#define ss second
#define si(s) s.size()
#define fori(it,v) for(it=v.begin();it!=v.end(); it++)
#define for1(i,low,high) for(int i=low;i<high;i++)
#define for2(i,low,high) for(int i=low;i<=high;i++)
#define rev(i,high,low) for(int i=high;i>=low ;i--)
#define all(x) x.begin(), x.end()
#define fil(x,i) memset(x,i,sizeof(x));
#define setbits(x) __builtin_popcount(x)
#define boost ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define read freopen("input.txt","r",stdin)
#define write freopen("output.txt","w",stdout)
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
void i_o() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin) ;
freopen("output.txt", "w", stdout) ;
#endif
}
const int N=3e5+6;
ll bit[N];
void add(int idx,ll val){
for(;idx<N;idx+=idx&-idx) bit[idx]+=val;
}
ll sum(int idx){
ll res=0;
for(;idx>0;idx-=idx&-idx)
res+=bit[idx];
return res;
}
int main()
{
boost; i_o();
int n;
cin>>n;
vector<ll>v(2*n);
vector<ll>ans(n);
for(int i=0;i<n;i++){cin>>v[i]; v[i]++;}
for1(i,n,2*n) v[i]=v[i-n];
ll tot=0;
for(int i=2*n-1;i>=n;i--){
tot+=sum(v[i]-1);
add(v[i],1LL);
}
ans[0]=tot;
for(int i=n-1;i>0;i--){
tot-=(sum(n+1)-sum(v[i]));
tot+=sum(v[i]-1);
ans[i]=tot;
}
for1(i,0,n) cout<<ans[i]<<endl;
cout<<endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i = (a); i < (b); i++)
#define drep(i,b,a) for(int i = (b)-1; i >= (a); i--)
#define bit(n) (1LL << (n))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define SORT(v) sort(v.begin(),v.end());
#define RSORT(v) sort(v.rbegin(),v.rend());
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<vd> vvd;
typedef vector<vb> vvb;
typedef vector<vc> vvc;
const int inf = 1 << 30; const ll infl = 1LL << 60; const double PI = acos(-1.0);
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T> inline bool odd(T x) { return x & 1; }
template<class T> inline bool even(T x) { return !(x & 1); }
template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
std::fill( (T*)array, (T*)(array+N), val );
}
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << endl; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, set<T> P)
{ for(auto it : P) { s << "<" << it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ for(auto it : P) { s << "<" << it.first << "->" << it.second << "> "; } return s; }
//----------------------------------------------------------------------------------------------
template< typename T >
struct edge {
int to;
T cost;
edge(int to, T cost) : to(to), cost(cost) {}
};
template< typename T >
using Edges = vector< edge< T > >;
template< typename T >
using WeightedGraph = vector< Edges< T > >;
template< typename T >
vector< T > dijkstra(WeightedGraph< T > &g, int s) {
const auto INF = numeric_limits< T >::max();
vector< T > dist(g.size(), INF);
using P = pair< T, int>;
priority_queue<P, vector<P>, greater<P>> que;
dist[s] = 0;
que.emplace(dist[s], s);
while(!que.empty()) {
auto [cost, v] = que.top();
que.pop();
if(dist[v] < cost) continue;
for(auto &e : g[v]) {
auto next_cost = cost + e.cost;
if(dist[e.to] <= next_cost) continue;
dist[e.to] = next_cost;
que.emplace(dist[e.to], e.to);
}
}
return dist;
}
int main()
{
int a,b,x,y;
cin >> a >> b >> x >> y;
a--; b--;
WeightedGraph<int> g(200);
rep(i,0,100) {
g[2*i].emplace_back(2*i+1, x);
g[2*i+1].emplace_back(2*i, x);
if(i > 0) {
g[2*i].emplace_back(2*i-1, x);
g[2*i-1].emplace_back(2*i, x);
}
}
rep(i,0,99) {
g[2*i].emplace_back(2*i+2, y);
g[2*i+2].emplace_back(2*i, y);
g[2*i+1].emplace_back(2*i+3, y);
g[2*i+3].emplace_back(2*i+1, y);
}
auto dis = dijkstra(g,2*a);
cout << dis[2*b+1] << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b,x,y,ans;
cin>>a>>b>>x>>y;
int d=fabs(a-b),mn=min(2*x,y);
if(a==b) cout<<x<<endl;
else if (a>b)
{
cout<<x+(d-1)*mn<<endl;
}
else cout<<x+(d)*mn<<endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (int)n; i++)
using ll = long long;
int main(){
int t;
cin >> t;
while(t--) {
ll l, r;
cin >> l >> r;
if(r < 2*l) cout << 0 << endl;
else cout << (r-2*l+1)*(r-2*l+2)/2 << endl;
}
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:(b-1)),y(up?b:(a-1)){}_RX(int a):_RX(0,a){}};
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;
i64 simulate(vector<int> ops) {
i64 x = 0;
i64 y = 0;
for(auto op : ops) {
if(op == 1) {
x += 1;
} else if(op == 2) {
y += 1;
} else if(op == 3) {
x = x + y;
} else if(op == 4) {
y = x + y;
} else {
assert(false);
}
}
return x;
}
int main() {
const i64 N = read_item();
if(N < 100) {
printf("%lld\n", N);
for(auto i : range((int)N)) {
printf("1\n");
}
return 0;
}
vector<i64> fib = {0, 1};
while(true) {
const int s = fib.size();
i64 last1 = fib[s - 1];
i64 last2 = fib[s - 2];
dbg(last1, last2);
if(last1 + last2 > N) {
break;
}
i64 p = last1 + last2;
fib.push_back(p);
fib.push_back(p + last1);
assert(p > 0);
assert(p + last1 > 0);
}
const int len = fib.size();
const int rounds = fib.size() / 2 - 1;
i64 y = N;
vector<pair<pair<int, int>, int>> ops;
for(auto i : range(rounds)) {
ops.push_back({{i, 0}, 4});
ops.push_back({{i, 1}, 3});
}
for(auto i : revrange(rounds)) {
i64 p = fib[2 + i * 2];
i64 q = fib[3 + i * 2];
if(y >= q) {
ops.push_back({{rounds - 1 - i - 1, 2}, 1});
y = y - q;
dbg(i, q, 1);
}
if(y >= p) {
ops.push_back({{rounds - 1 - i - 1, 2}, 2});
y = y - p;
dbg(i, q, 2);
}
}
dbg(y);
assert(y == 0);
sort(ops.begin(), ops.end());
vector<int> ans;
for(auto& op : ops) {
// dbg(op.first.first, op.first.second, op.second);
ans.push_back(op.second);
}
i64 true_x = simulate(ans);
assert(true_x == N);
printf("%d\n", (int)ans.size());
for(auto& op : ans) {
printf("%d\n", op);
}
return 0;
}
/* wafdayo~~~ */
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define BE(x) x.begin(), x.end()
int main() {
ll n;
string t;
cin >> n >> t;
ll m = 10000000000;
if (t == "1") {
cout << m * 2LL << endl;
return 0;
}
if (t[0] == '0') t = "11" + t, n += 2;
else if (t.substr(0, 2) == "10") t = "1" + t, n++;
string s = "";
while (s.size() < t.size()) s += "110";
if (s.substr(0, n) == t) cout << m - (n+2)/3 + 1 << endl;
else cout << 0 << endl;
} | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const int MAX_N = 1e9 + 5;
const int MAX_L = 20; // ~ Log N
const long long MOD = 1e9 + 7;
const long long INF = 1e9 + 7;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll,ll> par;
typedef vector<par> parv;
typedef vector<vi> vvi;
// Author : Koushal Bhat
#define forr(i,p,n) for( ll i=p; i<n; i++)
#define forn(i,p,n) for(ll i=p; i<=n; i++)
#define rev(i,n,p) for(ll i=n; i>=p; i--)
#define PI 3.14159265358979323846
#define LSOne(S) (S & (-S))
#define isBitSet(S, i) ((S >> i) & 1)
void solve() {
ll n=0;
cin>>n;
forr(i,1,MAX_N)
{
ll op=(i*(i+1))/2;
if(op>=n)
{
cout<<i<<"\n";
return;
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
solve();
} |
#include <bits/stdc++.h>
using namespace std;
int main(void){
int a, b, c, d;
cin >> a >> b >> c >> d ;
cout << b - c << endl;
}
| #include <bits/stdc++.h>
using namespace std;
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define oset_find(k) find_by_order(k)
#define oset_order(k) order_of_key(k)*/
using ll = long long;
using db = long double;
using vi = vector<int>;
using vl = vector<ll>;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
#define FOR(i, a, n) for(int i=(a);i<(n);++i)
#define ROF(i, a, n) for(int i=(n)-1;i>=(a);--i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) int((x).size())
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define endl '\n'
constexpr int pct(int x) { return __builtin_popcount(x); }
template<typename T> bool fmin(T& a, const T& b) { return b<a?a=b,1:0; }
template<typename T> bool fmax(T& a, const T& b) { return a<b?a=b,1:0; }
#define ts to_string
string ts(char c) { return '\'' + string(1, c) + '\''; }
string ts(const char* s) { return (string)s; }
string ts(const string& s) { return '"' + s + '"'; }
string ts(bool b) { return (b ? "true" : "false"); }
string ts(vector<bool> v) {
string res = "{";
FOR(i, 0, sz(v))
res += char('0' + v[i]);
res += "}";
return res;
}
template <size_t SZ> string ts(bitset<SZ> b) {
string res = "";
FOR(i, 0, SZ)
res+=char('0'+b[i]);
return res;
}
template <typename A, typename B> string ts(pair<A, B> p);
template <typename A> string ts(A v) {
bool first = 1; string res = "{";
for (const auto& x: v) {
if (!first) res += ", ";
first = 0; res += ts(x);
}
res += "}"; return res;
}
template <typename A, typename B> string ts(pair<A, B> p) {
return "(" + ts(p.fi) + ", " + ts(p.se) + ")";
}
void dbg_out() { cerr << "]" << endl; }
template <typename H, typename... T> void dbg_out(H h, T... t) {
cerr << ts(h); if (sizeof...(t)) cerr << ", ";
dbg_out(t...);
}
#ifdef LOCAL
#define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", dbg_out(__VA_ARGS__)
#else
#define dbg(...) 0
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//const ll MOD = 1000000007; //998244353;
//const ll INF = LLONG_MAX;
//const db PI = 3.14159265358979323846;
void solve() {
ll a, b, c, d;
cin >> a >> b >> c >> d;
dbg(b - c);
cout << b - c;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int tc = 1;
//cin >> tc;
while (tc--)
solve();
} |
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <functional>
#define rep(i,n) for (int i=0;i<n;i++)
#define DENOM 1000000007
using namespace std;
using ll = long long;
char win_rps(char a, char b) {
if (a==b) {
return a ;
}
if ((a=='R' && b=='S') || (a=='S' && b=='R')) {
return 'R' ;
} else if ((a=='S' && b=='P') || (a=='P' && b=='S')) {
return 'S' ;
} else {
return 'P' ;
}
}
int main(){
int n, k ;
string prev_s, s;
size_t s_length;
cin >> n >> k ;
cin >> prev_s;
for (int i = 0 ; i< k ; ++i) {
prev_s += prev_s ;
s_length = prev_s.size() ;
if (s_length == 1) {
cout << prev_s[0] << endl ;
return 0 ;
}
s = "" ;
for (int j = 0 ; j < s_length ; j+=2) {
s += win_rps(prev_s[j], prev_s[j+1]) ;
}
prev_s = s ;
cerr << i << ": " << prev_s << endl ;
}
cout << prev_s[0] << endl ;
return 0 ;
}
| #include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define f(i,j,n) for(long long i = j ; i < n ; ++i)
#define fr(i,n,j) for(long long i = n ; i >= j ; --i)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef vector<long long> vll;
typedef pair<long long,long long> pll;
typedef vector<pair<long long,long long>> vpll;
const ll N = 2e5 + 100;
const ll M = 1e6 + 100;
const ll mod = 998244353;
const ll inf = 1e17 +100;
ll n,a[N],dp[N],ct = 0;
ll mul(ll a,ll b){
return (a*b)%mod;
}
ll sum(ll a,ll b){
return (a+b)%mod;
}
ll res(ll a,ll b){
ll x = (a-b)%mod;
if(x >= 0) return x;
return (x+2*mod)%mod;
}
void solve(){
cin >> n;
f(i,1,n+1) cin >> a[i];
sort(a+1,a+n+1);
dp[0] = 0;
f(i,1,n+1){
ct = sum(ct,a[i]);
dp[i] = sum(dp[i-1],mul(ct,a[i]));
// cout << ct << " " << a[i] << endl;
ct = res(ct,a[i]);
// cout << ct << " " << 0 << endl;
ct = mul(2,ct);
ct = sum(ct,a[i]);
// cout << ct << endl;
}
cout << dp[n] << endl;
}
int main(){
fastio;
ll tc = 1;
// cin >> tc;
while(tc--) solve();
return 0;
} |
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<bitset>
#include<cmath>
#include<ctime>
#include<queue>
#include<map>
#include<set>
#define int long long
#define fi first
#define se second
#define max Max
#define min Min
#define abs Abs
#define lc (x<<1)
#define rc (x<<1|1)
#define mid ((l+r)>>1)
#define lowbit(x) (x&(-x))
#define fan(x) (((x-1)^1)+1)
#define mp(x,y) make_pair(x,y)
#define clr(f,n) memset(f,0,sizeof(int)*(n))
#define cpy(f,g,n) memcpy(f,g,sizeof(int)*(n))
using namespace std;
inline int read()
{
int ans=0,f=1;
char c=getchar();
while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){ans=(ans<<1)+(ans<<3)+c-'0';c=getchar();}
return ans*f;
}
inline void write(int x)
{
if(x<0) putchar('-'),x=-x;
if(x/10) write(x/10);
putchar((char)(x%10)+'0');
}
template<typename T>inline T Abs(T a){return a>0?a:-a;};
template<typename T,typename TT>inline T Min(T a,TT b){return a<b?a:b;}
template<typename T,typename TT> inline T Max(T a,TT b){return a<b?b:a;}
const int N=2e5+5;
int n,A,B,C,a[N],b[N],c[N],p1[N],p2[N],pre[N],suc[N],ans=1e18;
signed main()
{
n=read();
for(int i=1;i<=n*2;++i)
{
int x=read();
char opt[5];
scanf("%s",opt+1);
if(opt[1]=='R')
a[++A]=x;
else if(opt[1]=='G')
b[++B]=x;
else c[++C]=x;
}
if(A%2==0&&B%2==0&&C%2==0)
{
printf("0\n");
return 0;
}
if(A%2==0&&C%2==1)
swap(a,c),swap(A,C);
if(B%2==0&&C%2==1)
swap(b,c),swap(B,C);
sort(a+1,a+1+A);
sort(b+1,b+1+B);
for(int i=1;i<=A;++i)
{
int pos=lower_bound(b+1,b+1+B,a[i])-b;
if(pos>=1&&pos<=B) ans=min(ans,abs(a[i]-b[pos]));
if(pos>=2&&pos<=B+1) ans=min(ans,abs(a[i]-b[pos-1]));
}
for(int i=1;i<=C;++i)
{
int pos=lower_bound(a+1,a+1+A,c[i])-a;p1[i]=1e18;
if(pos>=1&&pos<=A) p1[i]=min(p1[i],abs(c[i]-a[pos]));
if(pos>=2&&pos<=A+1) p1[i]=min(p1[i],abs(c[i]-a[pos-1]));
pos=lower_bound(b+1,b+1+B,c[i])-b;p2[i]=1e18;
if(pos>=1&&pos<=B) p2[i]=min(p2[i],abs(c[i]-b[pos]));
if(pos>=2&&pos<=B+1) p2[i]=min(p2[i],abs(c[i]-b[pos-1]));
}
pre[0]=1e18;suc[C+1]=1e18;
for(int i=1;i<=C;++i) pre[i]=min(pre[i-1],p2[i]);
for(int i=C;i>=1;--i) suc[i]=min(suc[i+1],p2[i]);
for(int i=1;i<=C;++i) ans=min(ans,p1[i]+min(pre[i-1],suc[i+1]));
printf("%lld\n",ans);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define ar array
#define ll long long
const int MAX_N = 1e5 + 1;
const ll MOD = 1e9 + 7;
const ll INF = 1e15;
void solve()
{
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int N;
cin >> N;
vector<ll> cutenesses;
vector<string> colors;
ll cute;
string last_color = "U";
string color;
int red = 0, blue = 0, green = 0;
bool rb = false, rg = false, bg = false;
ll min_rb = INF, min_rg = INF, min_bg = INF;
for (int t = 1; t <= 2 * N; t++)
{
cin >> cute;
cin >> color;
cutenesses.push_back(cute);
colors.push_back(color);
if (color.compare("R") == 0)
{
red += 1;
}
else if (color.compare("B") == 0)
{
blue += 1;
}
else
{
green += 1;
}
// cout << cute << " " << color << endl;
// solve();
}
if ((red % 2 == 0) && (blue % 2 == 0) && (green % 2 == 0))
{
cout << 0;
}
else
{
vector<size_t> idx(cutenesses.size());
iota(idx.begin(), idx.end(), 0);
// sort indexes based on comparing values in v
// using std::stable_sort instead of std::sort
// to avoid unnecessary index re-orderings
// when v contains elements of equal values
stable_sort(idx.begin(), idx.end(),
[&cutenesses](size_t i1, size_t i2)
{ return cutenesses[i1] < cutenesses[i2]; });
for (int i = 0; i < 2 * N - 1; i++)
{
// cout << idx[i] << " " << idx[i + 1] << endl;
// cout << colors[idx[i]] << " " << colors[idx[i + 1]] << endl;
if (((colors[idx[i]].compare("R") == 0) && ((colors[idx[i + 1]].compare("B") == 0))) || (((colors[idx[i]].compare("B") == 0) && ((colors[idx[i + 1]].compare("R") == 0)))))
{
rb = true;
min_rb = min(min_rb, abs(cutenesses[idx[i]] - cutenesses[idx[i + 1]]));
}
else if (((colors[idx[i]].compare("R") == 0) && ((colors[idx[i + 1]].compare("G") == 0))) || (((colors[idx[i]].compare("G") == 0) && ((colors[idx[i + 1]].compare("R") == 0)))))
{
rg = true;
min_rg = min(min_rg, abs(cutenesses[idx[i]] - cutenesses[idx[i + 1]]));
}
else if (((colors[idx[i]].compare("G") == 0) && ((colors[idx[i + 1]].compare("B") == 0))) || (((colors[idx[i]].compare("B") == 0) && ((colors[idx[i + 1]].compare("G") == 0)))))
{
bg = true;
min_bg = min(min_bg, abs(cutenesses[idx[i]] - cutenesses[idx[i + 1]]));
}
}
// cout << red << " " << blue << " " << green << endl;
// cout << rb << " " << rg << " " << bg << endl;
// cout << min_rb << " " << min_rg << " " << min_bg << endl;
if (red % 2 == 0)
{
cout << min(min_bg, min_rg + min_rb);
}
else if (blue % 2 == 0)
{
cout << min(min_rg, min_rb + min_bg);
}
else
{
cout << min(min_rb, min_rg + min_bg);
}
}
} |
#include <iostream>
long long a[1000000]={0};
long long b[1000000]={0};
using namespace std;
int main(){
long long n,a,b;
cin>>n;
long long sum=0;
while(n>0){
cin>>a>>b;
sum+=(a+b)*(b-a+1)/2;
n--;
}
cout<<sum;
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;
#define ordered_set tree<ll, null_type , less<ll> , rb_tree_tag , tree_order_statistics_node_update>
#define ll long long
#define ull unsigned long long
#define pb push_back
#define inf 1e18
#define mk make_pair
#define ld long double
#define mod 1000000007
#define fi first
#define se second
#define fastIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define test ll t; cin>>t; while(t--)
#define setbits __builtin_popcount
#define endl '\n'
#define LOCAL
#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 {
#ifdef LOCAL
~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__) << "] "
int main()
{
fastIO;
ll t,n;
cin>>t>>n;
ld val,x,y;
val=(ld)t/(ld)100;
ll l,r,mid,ans;
l=1,r=1e18;
y=n-1;
while(l<=r)
{
mid=(l+r)/2;
x=floor(val*(ld)mid);
if(x<n)
{
l=mid+1;
}
else{
ans=mid;
r=mid-1;
}
}
cout<<ans+(ll)y<<endl;
} |
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int,int>;
int dp[105][105];
int n;
string s;
int p[105];
int dfs(int l, int w){
char c = s[l];
int t;
if(c == 'R') t = 0;
if(c == 'P') t = 1;
if(c == 'S') t = 2;
if(w == 0){
dp[0][l] = t;
return t;
}
if(dp[w][l] != -1) return dp[w][l];
int k = l + p[w-1];
k %= n;
int a = dfs(k, w-1);
int b = dfs(l, w-1);
int res;
if(a == b) res = a;
if(a == 0 && b == 1) res = b;
if(a == 0 && b == 2) res = a;
if(a == 1 && b == 0) res = a;
if(a == 1 && b == 2) res = b;
if(a == 2 && b == 0) res = b;
if(a == 2 && b == 1) res = a;
dp[w][l] = res;
return res;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int k;
cin >> n >> k;
cin >> s;
rep(i,105) rep(j,105) dp[i][j] = -1;
p[0] = 1;
rep(i,100) p[i+1] = p[i]*2, p[i+1] %= n;
int ans = dfs(0, k);
if(ans == 0) cout << 'R' << endl;
if(ans == 1) cout << 'P' << endl;
if(ans == 2) cout << 'S' << endl;
} | #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for(ll i = 0; i < (n); ++i)
#define FOR(i, m, n) for(ll i = m; i < (n); i++)
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
using vi = vector<int>;
using vii = vector<vi>;
using pii = pair<int, int>;
using vl = vector<ll>;
using vll = vector<vl>;
using pll = pair<ll, ll>;
ll N, K;
vll T;
ll ans = 0;
void disp(vl arr){
rep(i, arr.size()){
cout << arr[i] << " ";
}
cout << '\n';
}
void f(vl p){
ll tmp = 0;
rep(i, N-1){
tmp += T[p[i]][p[i+1]];
}
tmp += T[p[N-1]][p[0]];
if(tmp == K){
ans++;
}
return;
}
void dfs(vl now, ll n, ll m){
if(n == 0){
// 生成された順列に対して処理を行う関数(要定義)
f(now);
return;
}
for(ll v = 0; v < m; v++){
bool con = false;
for(auto nowv : now){
if(nowv == v){
con = true;
break;
}
}
if(con) continue;
vl next = now;
next.push_back(v);
dfs(next, n-1, m);
}
}
int main() {
cin >> N >> K;
T.resize(N, vl(N));
rep(i, N){
rep(j, N){
cin >> T[i][j];
}
}
vl arg = {0};
dfs(arg, N-1, N);
cout << ans;
} |
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
constexpr lint mod = 1e9 + 7;
#define all(x) (x).begin(), (x).end()
#define bitcount(n) __builtin_popcountl((lint)(n))
#define fcout cout << fixed << setprecision(15)
#define highest(x) (63 - __builtin_clzl(x))
#define rep(i, n) for(int i = 0; i < int(n); i++)
#define rep2(i, l, r) for(int i = int(l); i < int(r); i++)
#define repr(i, n) for(int i = int(n) - 1; i >= 0; i--)
#define repr2(i, l, r) for(int i = int(r) - 1; i >= int(l); i--)
#define SZ(x) int(x.size())
constexpr int inf9 = 1e9; constexpr lint inf18 = 1e18;
inline void YES(bool condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; }
inline void Yes(bool condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
inline void assertNO(bool condition){ if(!condition){ cout << "NO" << endl; exit(0); } }
inline void assertNo(bool condition){ if(!condition){ cout << "No" << endl; exit(0); } }
inline void assertm1(bool condition){ if(!condition){ cout << -1 << endl; exit(0); } }
lint power(lint base, lint exponent, lint module){ if(exponent % 2){ return power(base, exponent - 1, module) * base % module; }else if(exponent){ lint root_ans = power(base, exponent / 2, module); return root_ans * root_ans % module; }else{ return 1; }}
struct position{ int y, x; }; position mv[4] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}}; double euclidean(position first, position second){ return sqrt((second.x - first.x) * (second.x - first.x) + (second.y - first.y) * (second.y - first.y)); }
template<class T, class U> string to_string(pair<T, U> x){ return to_string(x.first) + "," + to_string(x.second); } string to_string(string x){ return x; }
template<class T> string to_string(complex<T> x){ return to_string(make_pair(x.real(), x.imag())); }
template<class itr> void array_output(itr start, itr goal){ string ans; for(auto i = start; i != goal; i++) cout << (i == start ? "" : " ") << (*i); if(!ans.empty()) ans.pop_back(); cout << ans << endl; }
template<class itr> void cins(itr first, itr last){ for(auto i = first; i != last; i++){ cin >> (*i); } }
template<class T> T gcd(T a, T b){ if(b) return gcd(b, a % b); else return a; }
template<class T> T lcm(T a, T b){ return a / gcd(a, b) * b; }
struct combination{ vector<lint> fact, inv; combination(int sz) : fact(sz + 1), inv(sz + 1){ fact[0] = 1; for(int i = 1; i <= sz; i++){ fact[i] = fact[i - 1] * i % mod; } inv[sz] = power(fact[sz], mod - 2, mod); for(int i = sz - 1; i >= 0; i--){ inv[i] = inv[i + 1] * (i + 1) % mod; } } lint P(int n, int r){ if(r < 0 || n < r) return 0; return (fact[n] * inv[n - r] % mod); } lint C(int p, int q){ if(q < 0 || p < q) return 0; return (fact[p] * inv[q] % mod * inv[p - q] % mod); } };
template<class itr> bool next_sequence(itr first, itr last, int max_bound){ itr now = last; while(now != first){ now--; (*now)++; if((*now) == max_bound){ (*now) = 0; }else{ return true; } } return false; }
template<class itr, class itr2> bool next_sequence2(itr first, itr last, itr2 first2, itr2 last2){ itr now = last; itr2 now2 = last2; while(now != first){ now--, now2--; (*now)++; if((*now) == (*now2)){ (*now) = 0; }else{ return true; } } return false; }
template<class T> bool chmax(T &a, const T &b){ if(a < b){ a = b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b){ if(b < a){ a = b; return 1; } return 0; }
inline int at(lint i, int j){ return (i >> j) & 1; }
random_device rnd;
bool is_in_board(lint y, lint x, lint H, lint W){ return (0 <= y && y < H && 0 <= x && x < W); }
int main(){
int N;
cin >> N;
lint x[N];
cins(x, x + N);
lint a = 0, b = 0, c = 0;
for(lint i: x){
a += abs(i);
b += i * i;
c = max(c, abs(i));
}
cout << a << endl;
fcout << sqrt(b) << endl;
cout << c << endl;
}
| #include <bits/stdc++.h>
#define bug1(x) cout<<#x<<" = "<<x<<endl
#define bug2(x,y) cout<<#x<<" = "<<x<<" "<<#y<<" = "<<y<<endl
#define bug3(x,y,z) cout<<#x<<" = "<<x<<" "<<#y<<" = "<<y<<" "<<#z<<" = "<<z<<endl
#define bug4(x,y,z,m) cout<<#x<<" = "<<x<<" "<<#y<<" = "<<y<<" "<<#z<<" = "<<z<<" "<<#m<<" = "<<m<<endl
using namespace std;
template<class L, class R>
ostream& operator<<(ostream &os, const pair<L,R> &input) {
os << "(";
os << input.first << "," << input.second;
return os << ")";
}
template<class T>
ostream& operator<<(ostream &os, const vector<T> &input) {
os << "[ ";
for(auto elem : input) {
os << elem << " ";
}
return os << "]";
}
template<class T>
ostream& operator<<(ostream &os, const set<T> &input) {
os << "{ ";
for(auto elem : input) {
os << elem << " ";
}
return os << "}";
}
template<class T>
ostream& operator<<(ostream &os, const unordered_set<T> &input) {
os << "{ ";
for(auto elem : input) {
os << elem << " ";
}
return os << "}";
}
template<class T>
ostream& operator<<(ostream &os, const multiset<T> &input) {
os << "{ ";
for(auto elem : input) {
os << elem << " ";
}
return os << "}";
}
template<class T>
ostream& operator<<(ostream &os, const unordered_multiset<T> &input) {
os << "{ ";
for(auto elem : input) {
os << elem << " ";
}
return os << "}";
}
template<class L, class R>
ostream& operator<<(ostream &os, const map<L,R> &input) {
os << "{ ";
for(auto elem : input) {
os << "(" << elem.first << " , " << elem.second << ") ";
}
return os << "}";
}
template<class L, class R>
ostream& operator<<(ostream &os, const unordered_map<L,R> &input) {
os << "{ ";
for(auto elem : input) {
os << "(" << elem.first << " , " << elem.second << ") ";
}
return os << "}";
}
#define ll long long
const int MAXN = 200005;
const int INF = 1e9;
const int MOD = 1e9 + 7;
class TaskC {
private:
public:
void solveOne(int it){
int n;
cin >> n;
vector<int> weight(n), baggage(n), p(n);
for (int i = 0; i < n; i++) {
cin >> weight[i];
}
for (int i = 0; i < n; i++) {
cin >> baggage[i];
}
unordered_map<int,int> where;
for (int i = 0; i < n; i++) {
cin >> p[i];
p[i]--;
where[p[i]] = i;
if (baggage[p[i]] >= weight[i] && i != p[i]) {
cout << -1 << endl;
return;
}
}
vector<bool> vis(n, false);
vector<pair<int,int>> ans;
for (int i = 0; i < n; i++) {
if (vis[i]) {
continue;
}
vector<pair<int,int>> vertices;
int curr = i;
while(!vis[curr]) {
vertices.push_back({weight[curr], curr});
vis[curr] = true;
curr = p[curr];
}
sort(vertices.begin(), vertices.end());
for (int k = 0; k + 1 < vertices.size(); k++) {
int f = vertices[k].second;
int s = where[f];
ans.push_back({f, s});
where[p[f]] = s;
p[s] = p[f];
}
}
cout << ans.size() << endl;
for (auto item : ans) {
cout << item.first+1<< " " << item.second+1 << endl;
}
}
void solve(){
int tc = 1;
for(int it = 1; it <= tc; it++){
solveOne(it);
}
}
};
int main(){
ios_base::sync_with_stdio(false);
TaskC solver;
solver.solve();
return 0;
}
|
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
typedef long long ll;
ll gcd (ll a, ll b) {
ll c = a % b;
if (c==0)
return b;
return gcd (b, c);
}
int main () {
ll L;
cin >> L;
ll ans = 1;
ll n[11];
ll d[11];
for (int i=0; i<11; i++) {
n[i] = L-i-1;
d[i] = i+1;
}
for (int i=0; i<11; i++) {
for (int j=0; j<11; j++) {
if (d[j] != 1) {
ll g = gcd (n[i], d[j]);
n[i] /= g;
d[j] /= g;
}
}
ans *= n[i];
}
cout << ans << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define pii pair<int,int>
#define vi vector<int>
#define mii map<int,int>
#define pqb priority_queue<int>
#define pqs priority_queue<int,vi,greater<int> >
#define setbits(x) __builtin_popcountll(x)
#define mod 1000000007
#define hell 100000000000000007 //10e18+7
#define mod 1000000007//10e9+7
#define pi 3.141592653589793238
#define ps(x,y) fixed<<setprecision(y)<<x
#define mk(arr,n,type) type *arr=new type[n];
#define range(a,b) substr(a,b-a+1)
#define w(x) int x; cin>>x; while(x--)
#define trace(x) cerr<<#x<<": "<<x<<" "<<endl;
#define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int dp[201][13];
// int mod_exp(int x, int y, int mm)
// {
// x %= mm;
// if (y == 0)
// return (1);
// else if (y % 2 == 0)
// return (mod_exp((x * x) % mm, y / 2, mm));
// else
// return ((x * mod_exp((x * x) % mm, (y - 1) / 2, mm)) % mm);
// }
// int modInverse(int n, int p)
// {
// return mod_exp(n, p - 2, p);
// }
//remember the approach of suming up the element
// and when it exceeds from our radar
// try removing from the staring part
//priority queue max heap imp!!
// void buildSegtree(ll s1, ll e1, ll p1)
// {
// if (s1 == e1)
// {
// segtree[p1] = (arr[s1]);
// return;
// }
// ll mid = ((s1 + e1) / 2);
// buildSegtree(s1, mid, 2 * p1 + 1);
// buildSegtree(mid + 1, e1, 2 * p1 + 2);
// segtree[p1] = ((segtree[2 * p1 + 1])+segtree[2 * p1 + 2]) ;
// }
// ll rangeQuery(ll q1, ll q2, ll s1, ll e1, ll p1)
// {
// if (q1 <= s1 && q2 >= e1)
// return (segtree[p1]);
// if (q2 < s1 || q1 > e1)
// return (0);//it varies BE CAREFULL!!
// ll mid = (s1 + e1) / 2;
// ll l1 = rangeQuery(q1, q2, s1, mid, 2 * p1 + 1);
// ll l2 = rangeQuery(q1, q2, mid + 1, e1, 2 * p1 + 2);
// return ((l1+l2));
// }
// void updateQuery(ll q1, ll q2, ll id, ll p1, ll val)
// {
// if (q1 == q2)
// {
// segtree[p1] = val;
// return;
// }
// ll mid = (q1 + q2) / 2;
// if (id > mid)
// updateQuery(mid + 1, q2, id, 2 * p1 + 2, val);
// else
// updateQuery(q1, mid, id, 2 * p1 + 1, val);
// segtree[p1] = (segtree[2 * p1 + 1]+segtree[2 * p1 + 2]);
// }
// ll binpow(ll a, ll b, ll m)
// {
// a %= m;
// ll res = 1;
// while (b > 0) {
// if (b & 1)
// res = res * a % m;
// a = a * a % m;
// b >>= 1;
// }
// return res;
// }
// ll inv(ll a) {
// a %= mod;
// if (a < 0) a += mod;
// ll b = mod, u = 0, v = 1;
// while (a) {
// ll t = b / a;
// b -= t * a; swap(a, b);
// u -= t * v; swap(u, v);
// }
// if (u < 0) u += mod;
// return u;
// }
int32_t main()
{
#ifndef ONLINE_JUDGE
freopen("i1.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n;
cin >> n;
for (int i = 0; i < 201; i++)
{
for (int j = 0; j <= (min(i, 12LL)); j++)
{
if (i == j || j == 0)
dp[i][j] = 1;
else
dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j ];
}
}
cout << dp[n - 1][11];
}
|
#include<bits/stdc++.h>
using namespace std;
int Nx,Ny;
const bool debug=false;
struct tile_map{
private:
int m[50][50];
int c[50][50];
bool visited[50][50];
public:
int64_t score=0;
string ans="";
int get(int x,int y){
if(x>=0&&x<50&&y>=0&&y<50){
return m[x][y];
}else{
if(debug)puts("Mindex errer");
return -1;
}
}
void init(){
for(int i=0;i<50;i++){
for(int j=0;j<50;j++)cin>>m[i][j];
}
for(int i=0;i<50;i++){
for(int j=0;j<50;j++)cin>>c[i][j];
}
for(int i=0;i<50;i++){
for(int j=0;j<50;j++)visited[i][j]=false;
}
visited[Nx][Ny]=true;
int t=get(Nx,Ny);
if(get(Nx+1,Ny)==t)visited[Nx+1][Ny]=true;
if(get(Nx-1,Ny)==t)visited[Nx-1][Ny]=true;
if(get(Nx,Ny+1)==t)visited[Nx][Ny+1]=true;
if(get(Nx,Ny-1)==t)visited[Nx][Ny-1]=true;
}
int cost(int x,int y){
if(x>=0&&x<50&&y>=0&&y<50){
if(visited[x][y])return -1;
else return c[x][y];
}else{
if(debug)puts("Cindex errer");
return -1;
}
}
map<char,array<int,3>> next(){
map<char,array<int,3>>mv;
if(Nx+1<50&&!visited[Nx+1][Ny]){
mv['D']={Nx+1,Ny,cost(Nx+1,Ny)};
}
if(Nx-1>=0&&!visited[Nx-1][Ny]){
mv['U']={Nx-1,Ny,cost(Nx-1,Ny)};
}
if(Ny+1<50&&!visited[Nx][Ny+1]){
mv['R']={Nx,Ny+1,cost(Nx,Ny+1)};
}
if(Ny-1>=0&&!visited[Nx][Ny-1]){
mv['L']={Nx,Ny-1,cost(Nx,Ny-1)};
}
return mv;
}
bool go(int x,int y){
if(abs(x-Nx)+abs(y-Ny)!=1){
if(debug)puts("can not go");
return false;
}
if(visited[x][y]){
return false;
}else{
if(x==Nx+1)ans+='D';
if(x==Nx-1)ans+='U';
if(y==Ny+1)ans+='R';
if(y==Ny-1)ans+='L';
score+=cost(x,y);
visited[x][y]=true;
int t=get(x,y);
if(get(x+1,y)==t)visited[x+1][y]=true;
if(get(x-1,y)==t)visited[x-1][y]=true;
if(get(x,y+1)==t)visited[x][y+1]=true;
if(get(x,y-1)==t)visited[x][y-1]=true;
Nx=x,Ny=y;
return true;
}
}
void print(){
for(int i=0;i<50;i++){
for(int j=0;j<50;j++)cout<<(visited[i][j]?'x':'o');puts("");
}
}
};
int main(){
cin>>Nx>>Ny;
tile_map M;
M.init();
if(debug)M.print();
auto t=M.next();
while(!t.empty()){
if(Nx<25&&Ny<25){
if(t.count('D')){
M.go(t['D'][0],t['D'][1]);
}else if(t.count('L')){
M.go(t['L'][0],t['L'][1]);
}else if(t.count('R')){
M.go(t['R'][0],t['R'][1]);
}else if(t.count('U')){
M.go(t['U'][0],t['U'][1]);
}
}else if(Nx<25){
if(t.count('L')){
M.go(t['L'][0],t['L'][1]);
}else if(t.count('D')){
M.go(t['D'][0],t['D'][1]);
}else if(t.count('U')){
M.go(t['U'][0],t['U'][1]);
}else if(t.count('R')){
M.go(t['R'][0],t['R'][1]);
}
}else if(Ny<25){
if(t.count('R')){
M.go(t['R'][0],t['R'][1]);
}else if(t.count('D')){
M.go(t['D'][0],t['D'][1]);
}else if(t.count('U')){
M.go(t['U'][0],t['U'][1]);
}else if(t.count('L')){
M.go(t['L'][0],t['L'][1]);
}
}else{
if(t.count('U')){
M.go(t['U'][0],t['U'][1]);
}else if(t.count('R')){
M.go(t['R'][0],t['R'][1]);
}else if(t.count('L')){
M.go(t['L'][0],t['L'][1]);
}else if(t.count('D')){
M.go(t['D'][0],t['D'][1]);
}
}
t=M.next();
}
cout<<M.ans<<'\n';
if(debug)cout<<M.score<<'\n';
if(debug){
M.print();
cout<<Nx<<' '<<Ny<<'\n';
for(auto [x,y]:M.next()){
cout<<y[0]<<' '<<y[1]<<'\n';
}
}
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
template<class T> bool chmin(T& a,T b){if(a>b) {a = b; return true;} return false;}
template<class T> bool chmax(T& a,T b){if(a<b) {a = b; return true;} return false;}
#define all(x) (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(x) cerr << #x << " = " << (x) << endl;
constexpr int mod = (1<<30)-1;
inline int xor128() {
static int x = 123456789;
static int y = 362436069;
static int z = 521288629;
static int w = 88675123;
int t;
t = x ^ (x << 11);
x = y;
y = z;
z = w;
return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
}
inline int randInt(int l,int r){
return l+xor128()%(r-l);
}
inline double getprob(){
return (xor128()%mod)/(double) mod;
}
class TimeCheck {
private:
chrono::system_clock::time_point start, end;
public:
TimeCheck(){start = chrono::system_clock::now();}
double nowTime() {
end = chrono::system_clock::now();
double ti = static_cast<double>(
chrono::duration_cast<chrono::microseconds>(end-start).count()/1000.0);
return ti;
}
};
constexpr int N = 50;
vec<int> dx = {-1,0,1,0},dy = {0,-1,0,1};
string D = "ULDR";
vvec<int> T(N,vec<int>(N)),P = T;
bool in(int x,int y){
return 0<=x && x<N && 0<=y && y<N;
}
struct state{
int px,py;
int score;
string S;
unordered_set<int> vis;
state(){}
state(int sx,int sy):px(sx),py(sy){
score = P[sx][sy];
vis.insert(T[sx][sy]);
S = "";
}
bool is_valid_move(int nx,int ny){
return in(nx,ny) && !vis.count(T[nx][ny]);
}
void move(int id){
S += D[id];
px += dx[id];
py += dy[id];
vis.insert(T[px][py]);
score += P[px][py];
}
bool operator<(const state& r) const{
return score>r.score;
}
};
vec<state> generate_move(state& s,int len){
vec<state> res;
auto dfs = [&](auto&& self,int x,int y,int t)->void{
if(t==len){
res.push_back(s);
return ;
}
rep(i,4){
int nx = x+dx[i],ny = y+dy[i];
if(!s.is_valid_move(nx,ny)) continue;
s.move(i);
self(self,nx,ny,t+1);
s.S.pop_back();
s.score -= P[nx][ny];
s.vis.erase(T[nx][ny]);
s.px = x,s.py = y;
}
};
dfs(dfs,s.px,s.py,0);
return res;
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int sx,sy;
cin >> sx >> sy;
rep(i,N) rep(j,N){
cin >> T[i][j];
}
rep(i,N) rep(j,N) cin >> P[i][j];
state init(sx,sy);
const int width = 50;
vec<state> que;
que.push_back(init);
state ans = init;
rep(t,2500) if(!que.empty()){
vec<state> nq;
for(auto& s:que){
auto res = generate_move(s,4);
sort(all(res));
// if(res.size()>5) res.resize(5);
for(auto& ns:res) nq.push_back(ns);
}
sort(all(nq));
if(nq.size()>width) nq.resize(width);
if(nq.empty()) break;
que.swap(nq);
}
sort(all(que));
ans = que[0];
cerr << "ok\n";
cout << ans.S << "\n";
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define rep2(i,x,n) for(int i=x; i<(n); i++)
using ll = long long;
int main()
{
int n;
cin >> n;
ll sum = 0;
int i = 1;
while(sum < n){
sum += i;
i++;
}
cout << i-1 << endl;
return 0;
} | /*
* @Date : 2021-03-28 20:23:44
* @Author : ssyze
* @Description :
*/
#include<bits/stdc++.h>
using namespace std;
#ifndef ONLINE_JUDGE
#define dbg(x...) do { cout << "\033[32;1m " << #x << " -> "; err(x); } while (0)
void err() { cout << "\033[39;0m" << endl; }
template<template<typename...> class T, typename t, typename... A>
void err(T<t> a, A... x) { for (auto v: a) cout << v << ' '; err(x...); }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }
#else
#define dbg(...)
#endif
typedef long long ll;
typedef pair<int,int> pi;
typedef vector<int> vi;
template<class T> using vc=vector<T>;
template<class T> using vvc=vc<vc<T>>;
template<class T> void mkuni(vector<T>&v)
{
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
}
ll rand_int(ll l, ll r)
{
static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
return uniform_int_distribution<ll>(l, r)(gen);
}
template<class T>
void print(T x,int suc=1)
{
cout<<x;
if(suc==1) cout<<'\n';
else cout<<' ';
}
template<class T>
void print(const vector<T>&v,int suc=1)
{
for(int i=0;i<v.size();i++)
print(v[i],i==(int)(v.size())-1?suc:2);
}
int t;
ll x;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> t;
while (t--) {
cin >> x;
int cnt = 0;
while (x % 2 == 0)
x /= 2, cnt++;
if (cnt >= 2) cout << "Even\n";
else if (cnt == 1) cout << "Same\n";
else cout << "Odd\n";
}
} |
#pragma GCC optimize("Ofast")
#pragma GCC target ("sse4")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;
//#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acosl(-1.0);
ll mod_pow(ll x, ll n, ll m = mod) {
ll res = 1;
while (n) {
if (n & 1)res = res * x % m;
x = x * x % m; n >>= 1;
}
return res;
}
struct modint {
ll n;
modint() :n(0) { ; }
modint(ll m) :n(m) {
if (n >= mod)n %= mod;
else if (n < 0)n = (n % mod + mod) % mod;
}
operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
if (n == 0)return modint(1);
modint res = (a * a) ^ (n / 2);
if (n % 2)res = res * a;
return res;
}
ll inv(ll a, ll p) {
return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
const int max_n = 1 << 18;
modint fact[max_n], factinv[max_n];
void init_f() {
fact[0] = modint(1);
for (int i = 0; i < max_n - 1; i++) {
fact[i + 1] = fact[i] * modint(i + 1);
}
factinv[max_n - 1] = modint(1) / fact[max_n - 1];
for (int i = max_n - 2; i >= 0; i--) {
factinv[i] = factinv[i + 1] * modint(i + 1);
}
}
modint comb(int a, int b) {
if (a < 0 || b < 0 || a < b)return 0;
return fact[a] * factinv[b] * factinv[a - b];
}
void solve() {
int n; cin >> n;
vector<int> a(n);
vector<int> b(n);
rep(i, n)cin >> a[i];
rep(i, n)cin >> b[i];
ll sum= 0;
vector<ll> vod, vev;
rep(i, n) {
sum += b[i];
if (i % 2)vod.push_back(a[i] - b[i]);
else vev.push_back(a[i] - b[i]);
}
ll ans = sum;
sort(all(vod), greater<ll>());
sort(all(vev), greater<ll>());
rep(i, n / 2) {
sum += vod[i] + vev[i];
ans = max(ans, sum);
}
cout << ans << "\n";
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
//cout << fixed << setprecision(15);
//init_f();
//init();
//expr();
//int t; cin >> t; rep(i, t)
solve();
return 0;
} | #include<bits/stdc++.h>
#define MAX 200000
using namespace std;
long long trianglesum[MAX+10],linesum[MAX+10];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin>>n;
long long x;
for(int i=1;i<=n;++i)
{
cin>>x;
linesum[i] = linesum[i-1]+x;
trianglesum[i] = trianglesum[i-1]+linesum[i];
}
long long ans = max(0LL,trianglesum[1]), maxlinesum = linesum[1];
for(int i=2;i<=n;++i)
{
maxlinesum = max(maxlinesum,linesum[i]);
ans = max(ans,trianglesum[i-1]+maxlinesum);
}
cout<<ans<<endl;
return 0;
} |
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define endl "\n"
#define ll long long
#define sz(s) (int)(s.size())
#define INF 0x3f3f3f3f3f3f3f3fLL
#define all(v) v.begin(),v.end()
#define watch(x) cout<<(#x)<<" = "<<x<<endl
const int dr[] { -1, -1, 0, 1, 1, 1, 0, -1 };
const int dc[] { 0, 1, 1, 1, 0, -1, -1, -1 };
#if __cplusplus >= 201402L
template<typename T>
vector<T> create(size_t n) {
return vector<T>(n);
}
template<typename T, typename ... Args>
auto create(size_t n, Args ... args) {
return vector<decltype(create<T>(args...))>(n, create<T>(args...));
}
#endif
void run() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.in", "r", stdin);
#else
//freopen("groups.in", "r", stdin);
#endif
}
const int N = 2e5 + 9, M = 18;
const int mod = 998244353;
ll fact[N];
ll inv[N];
ll invfact[N];
void factInverse() {
fact[0] = inv[1] = fact[1] = invfact[0] = invfact[1] = 1;
for (long long i = 2; i < N; i++) {
fact[i] = (fact[i - 1] * i) % mod;
inv[i] = mod - (inv[mod % i] * (mod / i) % mod);
invfact[i] = (inv[i] * invfact[i - 1]) % mod;
}
}
ll nCr(int n, int r) {
if (r > n)
return 0;
return (((fact[n] * invfact[r]) % mod) * invfact[n - r]) % mod;
}
void add(ll &a, ll b) {
if ((a += b) >= mod)
a -= mod;
}
int n, m;
ll dp[13][5009];
int solve(int idx, int cnt) {
if (idx < 0)
return cnt == 0;
cnt += ((m >> idx) & 1);
ll &rt = dp[idx][cnt];
if (~rt)
return rt;
rt = 0;
for (int i = 0; i <= cnt; i += 2)
add(rt, (solve(idx - 1, (cnt - i) * 2) * nCr(n, i)) % mod);
return rt;
}
int main() {
run();
factInverse();
memset(dp, -1, sizeof dp);
cin >> n >> m;
cout << solve(12, 0) << endl;
}
| #include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define fastio() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define test() int t;cin>>t;for(int test=1;test<=t;test++)
#define pb push_back
#define nl cout<<"\n"
#define F first
#define S second
#define all(x) x.begin(),x.end()
template<class C> void min_self( C &a, C b ){ a = min(a,b); }
template<class C> void max_self( C &a, C b ){ a = max(a,b); }
const ll MOD = 998244353;
ll mod( ll n, ll m=MOD ){ n%=m;if(n<0)n+=m;return n; }
const int MAXN = 5005;
const int LOGN = 21;
const ll INF = 1e14;
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
template<class T1, class T2> void add( T1 &x, T2 y, ll m = MOD )
{
x += y;
if( x >= m )
x -= m;
}
template<class T1, class T2> void sub( T1 &x, T2 y, ll m = MOD )
{
x -= y;
if( x < 0 )
x += m;
}
template<class T> class Math
{
public:
vector<T>fact,invfact;
Math( int n )
{
fact.resize(n);
invfact.resize(n);
fact[0] = invfact[0] = 1;
for(int i=1;i<n;i++)
{
fact[i] = mod( i * fact[i-1] );
invfact[i] = modinv( fact[i] );
}
}
T modinv( T x, T m = MOD )
{
return expo(x,m-2,m);
}
T expo( T base, T exp, T m = MOD )
{
T res = 1;
while( exp )
{
if( exp&1 )
res = mod( res*base, m );
base = mod( base*base, m );
exp >>= 1;
}
return res;
}
T choose( T n, T k )
{
if( k < 0 || k > n ) return 0;
T ans = fact[n];
ans = mod( ans * invfact[n-k] );
ans = mod( ans * invfact[k] );
return ans;
}
};
Math<ll> mat(MAXN);
ll dp[5005];
int main()
{
// #ifdef gupta_samarth
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
fastio();
int n,m; cin>>n>>m;
dp[0] = 1;
// dp[i] = no of sum
for(int bit=15; bit>=0; bit--)
{
ll now = 1LL<<bit;
vector<int> ndp(5005,0);
for(int sum=0; sum<=m; sum++)
{
for(int times=0; times<=n; times += 2)
{
ll adds = times*now;
if(sum + adds <= m)
{
add(ndp[sum+adds], mod(dp[sum] * mat.choose(n,times)));
}
}
}
for(int sum=0; sum<=m; sum++)
dp[sum] = ndp[sum];
}
cout<<dp[m],nl;
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
char judge(char a, char b){
if(a=='R'){
if(b=='R'){
return 'R';
}else if(b=='P'){
return 'P';
}else if(b=='S'){
return 'R';
}
}else if(a=='P'){
if(b=='R'){
return 'P';
}else if(b=='P'){
return 'P';
}else if(b=='S'){
return 'S';
}
}else if(a=='S'){
if(b=='R'){
return 'R';
}else if(b=='P'){
return 'S';
}else if(b=='S'){
return 'S';
}
}
}
int main(){
int n,k;
cin >> n >> k;
string s;
cin >> s;
for(int i=0;i<k;i++){
string tmp="";
if(s.size()%2!=0){
s+=s;
}
for(int j=0;j<s.size();j+=2){
tmp+=judge(s.at(j),s.at(j+1));
}
s=tmp;
}
cout << s.front() << endl;
return 0;
}
| #include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define all(a) a.begin(), a.end()
#define ff(i,a,b) for(int i=a;i<=b;i++)
#define fb(i,b,a) for(int i=b;i>=a;i--)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 300005;
const int inf = 1e9 + 5;
int n, q;
ll niz[maxn];
ll bor[4*maxn][35];
void build(int v, int tl, int tr, int k){
if(tl == tr){
// bor[v][k] = ((niz[tl] & (1 << k)) == 1 ? 1 : 0);
if(niz[tl] & (1LL << k))bor[v][k] = 1;
else bor[v][k] = 0;
return;
}
int mid = (tl + tr)/2;
build(v*2, tl, mid, k);
build(v*2+1, mid+1, tr, k);
bor[v][k] = bor[v*2][k] + bor[v*2+1][k];
}
void update(int v, int tl, int tr, int poz, int vre, int k){
if(tl == tr){
bor[v][k] += vre;
return;
}
int mid = (tl+tr)/2;
if(poz <= mid)update(v*2,tl,mid,poz,vre,k);
else update(v*2+1,mid+1,tr,poz,vre,k);
bor[v][k] = bor[v*2][k] + bor[v*2+1][k];
}
int kveri(int v, int tl, int tr, int l, int r, int k){
if(tl > r | l > tr)return 0;
if(l <= tl && r >= tr)return bor[v][k];
int mid = (tl + tr)/2;
return kveri(v*2,tl,mid,l,r,k) + kveri(v*2+1,mid+1,tr,l,r,k);
}
int main()
{
ios::sync_with_stdio(false);
cout.tie(nullptr);
cin.tie(nullptr);
cin >> n >> q;
ff(i,1,n)cin >> niz[i];
ff(mask,0,30)build(1,1,n,mask);
// build(1,1,n,2);
while(q--){
int t;
cin >> t;
if(t == 1){
int poz;
ll x;
cin >> poz >> x;
ff(mask,0,30){
if(!(x & (1 << mask)))continue;
int koji = kveri(1,1,n,poz,poz,mask);
// cout << "mask: " << mask << endl;
// cout << "koji: " << koji << endl;
if(koji == 0)update(1,1,n,poz,1,mask);
if(koji == 1)update(1,1,n,poz,-1,mask);
// cout << endl;
}
}
if(t == 2){
int l, r;
cin >> l >> r;
ll ans = 0;
ff(mask,0,30){
int kol = kveri(1,1,n,l,r,mask);
// cout << "kol: " << kol << endl;
ans += (kol % 2 == 1) * (1LL << mask);
}
cout << ans << endl;
}
}
return 0;
}
/**
**/
|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define mp make_pair
#define pb push_back
#define lp(i,s,f) for(ll i = s; i < ll(f); i++)
#define inF freopen("input.in", "r", stdin);
#define outF freopen("output.in", "w", stdout);
#define endl '\n'
#define MOD 1000000007
#define mm(arr) memset(arr, 0, sizeof(arr))
#define F first
#define S second
#define scanArray(a,n) for(int i = 0; i < n; i++){cin >> a[i];}
#define coutArray(a,n) for(int i = 0; i < n; i++){cout << a[i] << " ";};cout << endl;
int main(){
FAST
int t=1;
while(t--){
int x; cin >> x;
if(x>=0){
cout << x;
}
else{
cout << 0;
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
template<class T> bool chmin(T& a,T b){if(a>b) {a = b; return true;} return false;}
template<class T> bool chmax(T& a,T b){if(a<b) {a = b; return true;} return false;}
#define rep(i,n) for(int i=0;i<(n);i++)
#define drep(i,n) for(int i=(n)-1;i>=0;i--)
#define all(x) (x).begin(),(x).end()
#define debug(x) cerr << #x << " = " << (x) << endl;
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
vec<ll> A(N),B(N);
rep(i,N) cin >> A[i];
rep(i,N) cin >> B[i];
ll sum = 0;
rep(i,N) sum += A[i]*B[i];
cout << (sum? "No\n":"Yes\n");
} |
/**
* author: otera
**/
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<deque>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<array>
using namespace std;
#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60 ;
const ll mod=1e9+7 ;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define pb push_back
#define debug(x) cerr << #x << " = " << (x) << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
void solve() {
int n; cin >> n;
vector<int> a(n), b(n);
ll sum = 0;
rep(i, n) {
cin >> a[i];
}
rep(i, n) {
cin >> b[i];
sum += b[i] * 1LL;
}
vector<int> odd, even;
rep(i, n) {
if(i % 2 == 0) {
even.push_back(a[i] - b[i]);
} else {
odd.push_back(a[i] - b[i]);
}
}
ll ans = sum;
sort(all(even));
reverse(all(even));
sort(all(odd));
reverse(all(odd));
rep(i, n / 2) {
sum += even[i];
sum += odd[i];
chmax(ans, sum);
}
cout << ans << endl;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
//cout << fixed << setprecision(10);
//int t; cin >> t; rep(i, t)solve();
solve();
return 0;
} | #include<bits/stdc++.h>
#define ll long long
using namespace std;
char str[200005];
int freq[26];
int main()
{
scanf("%s", str);
vector<int>vec, let;
for(int i=0; str[i]; i++)
{
int x= str[i], cnt= 0;
while(str[i] && str[i]==x)i++, cnt++;i--;
vec.push_back(cnt);
let.push_back(x-'a');
}
ll res= 0, c= 0;
while(!vec.empty())
{
if(vec.back()>1)
{
res+= (c-freq[ let.back() ]);
memset(freq, 0, sizeof freq);
c+= vec.back();
freq[ let.back() ]= c;
}
else c++, freq[ let.back() ]++;
vec.pop_back();
let.pop_back();
}
printf("%lld\n", res);
return 0;
}
|
#include <bits/stdc++.h>
#include <vector>
#include <cstdint>
#define FOR(i,l,r) for(int i=(l);i<(r);++i)
#define RFOR(i,l,r) for(int i=(l);i>=(int)(r);i--)
#define rep(i,n) FOR(i,0,n)
#define rrep(i,n) RFOR(i,n-1,0)
#define int long long
using namespace std;
const int MX = 1e6;
const int inf = 1e9+5;
const int mod = 998244353;
signed main(){
int n,m;
cin>>n>>m;
vector<int> a(n);
rep(i,n) cin>>a[i];
vector<pair<int, int> > p(m);
rep(i,m) {
int x,y;
cin>>x>>y;
x--; y--;
p[i] = make_pair(x,y);
}
sort(p.begin(),p.end());
vector<int> b(n,inf);
rep(i,m) {
int x = p[i].first;
int y = p[i].second;
b[y]=min(b[y],min(b[x], a[x]));
// cout<< x<<" "<<y<<endl;
}
int ans = -inf;
rep(i,n) {
if(b[i]==inf)continue;
ans = max(a[i]-b[i],ans);
// cout<< b[i]<<endl;
}
cout<<ans<<endl;
return 0;
} | #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <cmath>
#include <iomanip>
#include <stack>
#include <queue>
#include <numeric>
#include <map>
#include <unordered_map>
#include <set>
#include <fstream>
#include <chrono>
#include <random>
#include <bitset>
//#include <atcoder/all>
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) ((int)(x).size())
#define pb push_back
using ll = long long;
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}
int N,M,K;
vector<vector<int>> d(17,vector<int>(17,1e9));
vector<vector<int>> dp(1<<17, vector<int>(17, 1e9));
int rec(int S,int v){
if((S&(1<<v))==0) return 1e9;
int &ret = dp[S][v];
if(ret<5*1e8) return ret;
rep(u,K){
chmin(ret,rec(S^(1<<v),u)+d[u][v]);
}
return ret;
}
int main(){
cin >> N >> M;
vector<pair<int,int>> AB(M); rep(i,M) cin >> AB[i].first >> AB[i].second;
cin >> K;
vector<int> C(K);
rep(i,K) cin >> C[i];
rep(i,K) C[i]--;
vector<vector<int>> G(N);
rep(i,M){
int a = AB[i].first-1;
int b = AB[i].second-1;
G[a].pb(b);
G[b].pb(a);
}
const int inf = 1e9;
rep(i,K){
vector<int> D(N,-1);
queue<int> q;
q.push(C[i]); D[C[i]] = 0;
while(!q.empty()){
int now = q.front(); q.pop();
rep(x,sz(G[now])){
int nxt=G[now][x];
if(D[nxt]>=0) continue;
q.push(nxt);
D[nxt] = D[now]+1;
}
}
rep(j,K){
if(D[C[j]]!=-1) chmin(d[i][j], D[C[j]]);
}
}
rep(i,K) d[i][i] = 0;
rep(k,K) rep(i,K) rep(j,K) d[i][j] = min(d[i][j], d[i][k]+d[k][j]);
rep(i,K){
int now=(1<<i);
dp[now][i]=1;
}
int ans = 1e9;
/*
rep(i,K){
chmin(ans, rec((1<<K)-1, i));
}*/
for(int bit=1;bit<(1<<K);bit++){
rep(i,K){
if(bit&(1<<i)){
int bit2=bit^(1<<i);
rep(j,K) if(bit2&(1<<j)) chmin(dp[bit][i], dp[bit2][j]+d[i][j]);
}
}
}
rep(i,K) chmin(ans, dp[(1<<K)-1][i]);
if(ans>5*1e8){
cout << -1 << endl;
}
else cout << ans << endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define pa pair<int,int>
const int Maxn=200010;
const int inf=2147483647;
const double pi=acos(-1.0);
LL read()
{
LL 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*10ll+ch-'0',ch=getchar();
return x*f;
}
int n,k,f[Maxn],g[Maxn],w;
vector<int>G[Maxn];
void dfs(int x,int fa)
{
f[x]=0;
int mx=0,mn=0;bool flag=false;
for(int i=0;i<G[x].size();i++)
{
int y=G[x][i];
if(y==fa)continue;flag=true;
dfs(y,x);f[x]+=f[y];
int t=g[y];
if(t>0)mx=max(mx,t);
else mn=min(mn,t);
}
if(!flag){g[x]=-1;return;}
if(mx-1+mn>=0&&mx)g[x]=mx-1;
else
{
if(mn==-w)
{
// printf("# %d\n",x);
f[x]++;
g[x]=w;
}
else g[x]=mn-1;
}
// printf("%d %d %d\n",x,f[x],g[x]);
}
bool check()
{
dfs(1,0);
return(f[1]+(g[1]<0)<=k);
}
int main()
{
n=read(),k=read();
for(int i=1;i<n;i++)
{
int x=read(),y=read();
G[x].push_back(y),G[y].push_back(x);
}
// w=1;check();return 0;
int l=1,r=n-1;
while(l<=r)
{
int mid=l+r>>1;w=mid;
if(check())r=mid-1;
else l=mid+1;
}
printf("%d",r+1);
} | #include<bits/stdc++.h>
#define ln puts("")
using namespace std;
typedef long long ll;
inline ll read() {
ll sum = 0, ff = 1;
char ch = getchar();
while(ch < '0' || ch > '9') {
if(ch == '-')
ff = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9')
sum = sum * 10 + ch - '0', ch = getchar();
return sum * ff;
}
const int N = 2007;
int n, m;
ll ans;
struct edge {
int to, nxt;
}e[N];
int cnt, he[N];
bool vis[N];
void add(int u, int v) {
e[++cnt].to = v;
e[cnt].nxt = he[u];
he[u] = cnt;
}
void dfs(int u) {
ans++;
vis[u] = true;
for(int i = he[u]; i; i = e[i].nxt) {
int v = e[i].to;
if(vis[v])
continue;
dfs(v);
}
}
int main() {
// freopen("", "r", stdin);
// freopen("", "w", stdout);
n = read(), m = read();
for(int i = 1; i <= m; i++) {
int u = read(), v = read();
add(u, v);
}
for(int i = 1; i <= n; i++) {
memset(vis, false, sizeof(vis));
dfs(i);
}
cout << ans;
return 0;
}
|
#include <iostream>
using namespace std;
int main(void){
// Your code here!
int a;
cin>>a;
cout<<a-1<<endl;
}
| #include<bits/stdc++.h>
using namespace std;
int main()
{
char a;
cin>>a;
char b;
cin>>b;
if(a=='Y') printf("%c",b-32);
else printf("%c",b);
return 0;
} |
//DUEL
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define x first
#define y second
#define pii pair<int,int>
#define pb push_back
#define eb emplace_back
#pragma GCC optimize("unroll-loops")
#define shandom_ruffle(a, b) shuffle(a, b, rng)
#define vi vector<int>
#define vl vector<ll>
#define popcnt __builtin_popcount
#define popcntll __builtin_popcountll
#define all(a) begin(a),end(a)
using namespace std;
using namespace __gnu_pbds;
using ll=long long;
using ull=unsigned long long;
using ld=long double;
int MOD=1000000007;
int MOD2=998244353;
vector<int> bases;
const ll LLINF=1ll<<60;
const char en='\n';
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void yes() {cout<<"YES"<<en; exit(0);}
void no() {cout<<"NO"<<en; exit(0);}
inline int rund() {int x576363482791fuweh=rng();return abs(x576363482791fuweh)%RAND_MAX;}
template<class T>
void prVec(vector<T> w,bool fl=false)
{
cout<<w.size()<<en;
for (int i=0;i<int(w.size())-1;++i) cout<<w[i]<<' ';
if (w.size()) cout<<w[w.size()-1]<<en;
if (fl) cout<<flush;
}
void M998()
{
swap(MOD,MOD2);
}
ll raand()
{
ll a=rund();
a*=RAND_MAX;
a+=rund();
return a;
}
#define rand raand
ll raaand()
{
return raand()*(MOD-7)+raand();
}
template<class T>
vi compress(vector<T>&v)
{
set<T> s;
for (auto a: v) s.insert(a);
vector<T> o(all(s));
vi nv;
for (int i=0;i<(int)v.size();++i) nv.pb(lower_bound(all(o),v[i])-o.begin());
return nv;
}
string to_upper(string a)
{
for (int i=0;i<(int)a.size();++i) if (a[i]>='a' && a[i]<='z') a[i]-='a'-'A';
return a;
}
string to_lower(string a)
{
for (int i=0;i<(int)a.size();++i) if (a[i]>='A' && a[i]<='Z') a[i]+='a'-'A';
return a;
}
ll sti(string a,int base=10)
{
ll k=0;
for (int i=0;i<(int)a.size();++i)
{
k*=base;
k+=a[i]-'0';
}
return k;
}
template<class T>
void eras(vector<T>& a,T b)
{
a.erase(find(a.begin(),a.end(),b));
}
string its(ll k,int base=10)
{
if (k==0) return "0";
string a;
while (k)
{
a.push_back((k%base)+'0');
k/=base;
}
reverse(a.begin(),a.end());
return a;
}
ll min(ll a,int b)
{
if (a<b) return a;
return b;
}
ll min(int a,ll b)
{
if (a<b) return a;
return b;
}
ll max(ll a,int b)
{
if (a>b) return a;
return b;
}
ll max(int a,ll b)
{
if (a>b) return a;
return b;
}
ll gcd(ll a,ll b)
{
if (b==0) return a;
return gcd(b,a%b);
}
ll lcm(ll a,ll b)
{
return a/gcd(a,b)*b;
}
template<class T,class K>
pair<T,K> mp(T a,K b)
{
return make_pair(a,b);
}
inline int mult(ll a,ll b)
{
return (a*b)%MOD;
}
inline int pot(int n,int k)
{
if (k==0) return 1;
ll a=pot(n,k/2);
a=mult(a,a);
if (k%2) return mult(a,n);
else return a;
}
int divide(int a,int b)
{
return mult(a,pot(b,MOD-2));
}
inline int sub(int a,int b)
{
if (a-b>=0) return a-b;
return a-b+MOD;
}
inline int add(int a,int b)
{
if (a+b>=MOD) return a+b-MOD;
return a+b;
}
bool prime(ll a)
{
if (a==1) return 0;
for (int i=2;i<=round(sqrt(a));++i)
{
if (a%i==0) return 0;
}
return 1;
}
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
const int N=300010;
int t;
ll n;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
for (int i=0;i<10;++i) bases.push_back(rand()%(MOD-13893829*2)+13893829);
cin>>t;
while (t--)
{
cin>>n;
if (n%2) cout<<"Odd\n";
else if (n%4) cout<<"Same\n";
else cout<<"Even\n";
}
}
| #include<bits/stdc++.h>
#define pb push_back
using namespace std;
typedef unsigned long long ull;
typedef unsigned uint;
typedef long long ll;
#define G getchar()
ll read()
{
ll x=0; bool flg=false; char ch=G;
for (;!isdigit(ch);ch=G) if (ch=='-') flg=true;
for (;isdigit(ch);ch=G) x=(x<<3)+(x<<1)+(ch^48);
return flg?-x:x;
}
#undef G
#define fi first
#define se second
/*
const int mod=;
inline int upd(const int &x){return x+(x>>31&mod);}
inline void add(int &x,const int &y){x=upd(x+y-mod);}
inline void iadd(int &x,const int &y){x=upd(x-y);}
int qpow(int x,int y){
int res=1;
for (;y;y>>=1,x=1LL*x*x%mod)
if (y&1) res=1LL*res*x%mod;
return res;
}
*/
//typedef pair<int,int> P;
#define rep(i,l,r) for (int i(l);i<=int(r);i++)
#define per(i,l,r) for (int i(r);i>=int(l);i--)
#define all(x) (x).begin(),(x).end()
ll n;
void solve(){
n=read();
if (n%4==0) puts("Even");
else if (n%2==0) puts("Same");
else puts("Odd");
}
int main()
{
for (int T=read();T--;) solve();
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main(void)
{
long long x,y;
cin >> x >> y;
long long a[3]={0};
if(x==y) cout<<x<<endl;
else
{
a[x]++;
a[y]++;
for(long long i=0;i<3;i++)
{
if(a[i]==0) cout<<i<<endl;
}
}
return 0;
} | #include <iostream>
using namespace std;
int main(){
int x,y;
cin >> x >> y;
if(x==y)
cout << x;
else
cout << 3-(x+y);
return 0;
} |
//#include <bits/stdc++.h>
#include <iostream>
#include <iomanip>
#include <math.h>
#include <cmath>
#include <algorithm>
#include <climits>
#include <functional>
#include <cstring>
#include <string>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <complex>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define itn int
#define nit int
#define ll long long
#define ms multiset
#define F(i,a,b) for(register int i=a,i##end=b;i<=i##end;++i)
#define UF(i,a,b) for(register int i=a,i##end=b;i>=i##end;--i)
#define re register
#define ri re int
#define il inline
#define pii pair<int,int>
#define cp complex<double>
//#pra gma G CC opti mize(3)
using namespace std;
using std::bitset;
//using namespace __gnu_pbds;
const double Pi=acos(-1);
namespace fastIO {
template<class T>
inline void read(T &x) {
x=0;
bool fu=0;
char ch=0;
while(ch>'9'||ch<'0') {
ch=getchar();
if(ch=='-')fu=1;
}
while(ch<='9'&&ch>='0') x=(x*10-48+ch),ch=getchar();
if(fu)x=-x;
}
inline int read() {
int x=0;
bool fu=0;
char ch=0;
while(ch>'9'||ch<'0') {
ch=getchar();
if(ch=='-')fu=1;
}
while(ch<='9'&&ch>='0') x=(x*10-48+ch),ch=getchar();
return fu?-x:x;
}
char _n_u_m_[40];
template<class T>
inline void write(T x ) {
if(x==0){
putchar('0');
return;
}
T tmp = x > 0 ? x : -x ;
if( x < 0 ) putchar('-') ;
register int cnt = 0 ;
while( tmp > 0 ) {
_n_u_m_[ cnt ++ ] = tmp % 10 + '0' ;
tmp /= 10 ;
}
while( cnt > 0 ) putchar(_n_u_m_[ -- cnt ]) ;
}
template<class T>
inline void write(T x ,char ch) {
write(x);
putchar(ch);
}
}
using namespace fastIO;
int n;
double x[1002],y[1002],mmin=-1.0000001,mmax=1.0000001;
int main() {int ans=0;
cin>>n;
F(i,1,n)cin>>x[i]>>y[i];
F(i,1,n){
F(j,i+1,n){
if((y[i]-y[j])/(x[i]-x[j])>mmin&&(y[i]-y[j])/(x[i]-x[j])<mmax)ans++;
}
}cout<<ans;
return 0;
}
| #include<cstdio>//JZM YYDS!!!
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<ctime>
#define ll long long
#define MAXN 100005
#define uns unsigned
#define MOD 1000000007ll
#define INF 1e18
using namespace std;
inline ll read(){
ll x=0;bool f=1;char s=getchar();
while((s<'0'||s>'9')&&s>0){if(s=='-')f^=1;s=getchar();}
while(s>='0'&&s<='9')x=(x<<1)+(x<<3)+s-'0',s=getchar();
return f?x:-x;
}
inline ll ksm(ll a,ll b,ll mo){
ll res=1;
for(;b;b>>=1,a=a*a%mo)if(b&1)res=res*a%mo;
return res;
}
int n;
double a[MAXN];
inline double check(double x){
double res=0;
for(int i=1;i<=n;i++)
res=(res+(x+a[i]-min(a[i],x*2))/n);
return res;
}
signed main()
{
n=read();
double mx=0;
for(int i=1;i<=n;i++)a[i]=read(),mx=max(mx,a[i]);
double ans=INF;
long double l=0,r=mx,m1,m2;
while(r-l>1e-10){
// printf("%.5f %.5f\n",l,r);
m1=(l+l+r)/3,m2=(l+r+r)/3;
double c1=check(m1),c2=check(m2);
ans=min(ans,min(c1,c2));
if(c1<c2)r=m2;
else l=m1;
}
printf("%.10f\n",ans);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define Rep(x, a, b) for(int x = a; x <= b; ++ x)
#define Dep(x, a, b) for(int x = a; x >= b; -- x)
int read() {
char c = getchar(); int f = 1, x = 0;
while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); }
while(c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
return x * f;
}
int main() {
int a = read(), b = read(), c = read(), d = read();
printf("%d\n", a * d - b * c);
return 0; int zmy = 123456;
}
| #pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;
//#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acosl(-1.0);
ll mod_pow(ll x, ll n, ll m = mod) {
if (n < 0) {
ll res = mod_pow(x, -n, m);
return mod_pow(res, m - 2, m);
}
if (abs(x) >= m)x %= m;
if (x < 0)x += m;
ll res = 1;
while (n) {
if (n & 1)res = res * x % m;
x = x * x % m; n >>= 1;
}
return res;
}
struct modint {
ll n;
modint() :n(0) { ; }
modint(ll m) :n(m) {
if (n >= mod)n %= mod;
else if (n < 0)n = (n % mod + mod) % mod;
}
operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
if (n == 0)return modint(1);
modint res = (a * a) ^ (n / 2);
if (n % 2)res = res * a;
return res;
}
ll inv(ll a, ll p) {
return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 2;
modint fact[max_n], factinv[max_n];
void init_f() {
fact[0] = modint(1);
for (int i = 0; i < max_n - 1; i++) {
fact[i + 1] = fact[i] * modint(i + 1);
}
factinv[max_n - 1] = modint(1) / fact[max_n - 1];
for (int i = max_n - 2; i >= 0; i--) {
factinv[i] = factinv[i + 1] * modint(i + 1);
}
}
modint comb(int a, int b) {
if (a < 0 || b < 0 || a < b)return 0;
return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
if (a < 0 || b < 0 || a < b)return 0;
return fact[a] * factinv[a - b];
}
void solve() {
int n, p; cin >> n >> p;
if (p == 2) {
if (n == 1)cout << 1 << "\n";
else cout << 0 << "\n"; return;
}
modint ans = p - 1;
ans *= mod_pow(p - 2, n - 1);
cout << ans << "\n";
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
//cout << fixed << setprecision(10);
//init_f();
//init();
//expr();
//int t; cin >> t;rep(i,t)
solve();
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
// #define LOCAL // 提出時はコメントアウト
#define DEBUG_
typedef long long ll;
const double EPS = 1e-9;
const ll INF = ((1LL<<62)-(1LL<<31));
typedef vector<ll> vecl;
typedef pair<ll, ll> pairl;
template<typename T> using uset = unordered_set<T>;
template<typename T, typename U> using mapv = map<T,vector<U>>;
template<typename T, typename U> using umap = unordered_map<T,U>;
#define ALL(v) v.begin(), v.end()
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define sz(x) (ll)x.size()
ll llceil(ll a,ll b) { return (a+b-1)/b; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> vector<vector<T>> genarr(ll n, ll m, T init) { return vector<vector<T>>(n,vector<T>(m,init)); }
///// DEBUG
#define DUMPOUT cerr
#define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
template<typename T>istream&operator>>(istream&is,vector<T>&vec){for(T&x:vec)is>>x;return is;}
template<typename T,typename U>ostream&operator<<(ostream&os,pair<T,U>&pair_var){os<<"("<<pair_var.first<<", "<<pair_var.second<<")";return os;}
template<typename T>ostream&operator<<(ostream&os,const vector<T>&vec){os<<"{";for(int i=0;i<vec.size();i++){os<<vec[i]<<(i+1==vec.size()?"":", ");}
os<<"}";return os;}
template<typename T,typename U>ostream&operator<<(ostream&os,map<T,U>&map_var){os<<"{";repi(itr,map_var){os<<*itr;itr++;if(itr!=map_var.end())os<<", ";itr--;}
os<<"}";return os;}
template<typename T>ostream&operator<<(ostream&os,set<T>&set_var){os<<"{";repi(itr,set_var){os<<*itr;itr++;if(itr!=set_var.end())os<<", ";itr--;}
os<<"}";return os;}
void dump_func(){DUMPOUT<<endl;}
template<class Head,class...Tail>void dump_func(Head&&head,Tail&&...tail){DUMPOUT<<head;if(sizeof...(Tail)>0){DUMPOUT<<", ";}
dump_func(std::move(tail)...);}
#ifndef LOCAL
#undef DEBUG_
#endif
#ifdef DEBUG_
#define DEB
#define dump(...) \
DUMPOUT << " " << string(#__VA_ARGS__) << ": " \
<< "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" \
<< endl \
<< " ", \
dump_func(__VA_ARGS__)
#else
#define DEB if (false)
#define dump(...)
#endif
//////////
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
int main() {
#ifdef LOCAL
ifstream in("../../Atcoder/input.txt");
cin.rdbuf(in.rdbuf());
#endif
ll N;
cin>>N;
vecl A(N);
rep(i,N) cin>>A[i];
ll ans = 0;
rep(i,N) {
ll x = A[i];
ll l = i, r = i;
bool ul = false, ur = false;
rep(j,i) {
ll k = i - j - 1;
if (A[k] < x) {
l = k + 1;
ul = true;
break;
}
}
REP(j,i+1,N) {
if (A[j] < x) {
r = j - 1;
ur = true;
break;
}
}
if (!ul) l = 0;
if (!ur) r = N-1;
dump(x,l,r, (r-l+1) * x);
chmax(ans, (r-l+1) * x);
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main(void){
int n, ans = 0;
cin >> n;
vector<int> a(n);
for(int i = 0; i < n; i++) cin >> a[i];
for(int l = 0; l < n; l++){
int x = a[l];
for(int r = l; r < n; r++){
x = min(x, a[r]);
ans = max(ans, x*(r-l+1));
}
}
cout << ans << endl;
return 0;
}
|
#include<bits/stdc++.h>
#define ll long long
using namespace std;
void readFile(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
const ll MOD = 1e9 + 7;
int main(){
readFile();
ios::sync_with_stdio(false);
cin.tie(NULL);
string s;
cin >> s;
deque<char> d;
int n = (int)s.length();
bool rev = false;
for(int i = 0 ; i < n ; ++i){
if(s[i] == 'R'){
rev = !rev;
}
else{
if(rev){
d.push_front(s[i]);
}
else{
d.push_back(s[i]);
}
}
}
vector<char> a;
for(auto i : d){
a.push_back(i);
}
vector<char> bb;
for(int i = 0 ; i < (int)a.size() ; ++i){
bool ok = false;
while(!bb.empty() && bb.back() == a[i]){
bb.pop_back();
ok = true;
}
if(!ok) bb.push_back(a[i]);
}
if(rev){
reverse(bb.begin(), bb.end());
}
for(auto i : bb) cout << i;
cout << '\n';
return 0;
} | #include <bits/stdc++.h>
#define F first
#define S second
#define MP make_pair
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define LCM(a, b) (a) / __gcd((a), (b)) * (b)
#define log_2(a) (log((a)) / log(2))
#define ln '\n'
using namespace std;
using LL = long long;
using ldouble = long double;
using P = pair<int, int>;
using LP = pair<LL, LL>;
static const int INF = INT_MAX;
static const LL LINF = LLONG_MAX;
static const int MIN = INT_MIN;
static const LL LMIN = LLONG_MIN;
static const int MOD = 1e9 + 7;
static const int SIZE = 200005;
const int dx[] = {0, -1, 1, 0};
const int dy[] = {-1, 0, 0, 1};
template<typename T>
void printArray(vector<T> &printVec) {
if(printVec.empty()) return;
cout << printVec[0];
for(int i = 1; i < printVec.size(); ++i) cout << " " << printVec[i];
cout << endl;
}
vector<LL> Div(LL n) {
vector<LL> ret;
for(LL i = 1; i * i <= n; ++i) {
if(n % i == 0) {
ret.pb(i);
if(i * i != n) ret.pb(n / i);
}
}
sort(all(ret));
return ret;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
string s;
cin >> s;
bool f = false;
deque<char> d;
for(char c : s) {
if(c == 'R') {
f = !f;
} else {
if(f) d.push_front(c);
else d.pb(c);
}
}
stack<char> st;
if(f) {
while(!d.empty()) {
char c = d.back();
d.pop_back();
if(!st.empty() && st.top() == c) {
st.pop();
} else {
st.push(c);
}
}
} else {
while(!d.empty()) {
char c = d.front();
d.pop_front();
if(!st.empty() && st.top() == c) {
st.pop();
} else {
st.push(c);
}
}
}
string t = "";
while(!st.empty()) {
t += st.top();
st.pop();
}
reverse(all(t));
cout << t << endl;
return 0;
}
|
#include <iostream>
#include <vector>
#include <algorithm>
std::vector<std::vector<int>> children, list;
std::vector<int> in, out, depth;
int timer;
void dfs(const int u) {
in[u] = timer++;
list[depth[u]].push_back(in[u]);
for (const int v : children[u]) {
depth[v] = depth[u] + 1;
dfs(v);
}
out[u] = timer++;
}
int main() {
int N;
std::cin >> N;
children = list = std::vector<std::vector<int>>(N);
in = out = depth = std::vector<int>(N);
for (int i = 1; i < N; ++i) {
int p;
std::cin >> p;
children[p - 1].push_back(i);
}
dfs(0);
int Q;
std::cin >> Q;
while (Q--) {
int u, d;
std::cin >> u >> d;
u -= 1;
const auto& v = list[d];
std::cout << std::lower_bound(v.cbegin(), v.cend(), out[u]) - std::lower_bound(v.cbegin(), v.cend(), in[u]) << '\n';
}
return 0;
}
| //#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization("unroll-loops")
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long
template<class T> using indexed_set=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
const int N=2e5+7;
const int mod=1e9+7;
const int INF=1e18+7;
const double EPS=1e-12;
const int dx[]={-1,1,0,0}; // UDLR
const int dy[]={0,0,-1,1}; // UDLR
inline int maximum(int a,int b) {int c=a>b?a:b; return c;}
inline int minimum(int a,int b) {int c=a>b?b:a; return c;}
int n; vector<vector<int>> adj(N); vector<int> dis(N),sub(N),ans(N); map<int, int> *cnt[N];
vector<vector<pair<int,int>>> query(N);
void dfs(int src){
sub[src]=1;
for(auto v:adj[src]){
dis[v]+=dis[src]+1;
dfs(v);
sub[src]+=sub[v];
}
}
void dfsz(int src){
int mx=-1,big=-1;
for(auto v:adj[src]){
dfsz(v);
if(sub[v]>mx) mx=sub[v],big=v;
}
if(big!=-1) cnt[src]=cnt[big];
else cnt[src]=new map<int,int>();
(*cnt[src])[dis[src]] ++;
for(auto v:adj[src]){
if(v==big) continue;
for(auto x:*cnt[v]) (*cnt[src])[x.first]+=x.second;
}
for(auto p:query[src]){
ans[p.first]=(*cnt[src])[p.second];
}
}
int32_t main(){
cin.tie(nullptr)->sync_with_stdio(false);
cin>>n;
for(int i=2;i<=n;i++){
int x; cin>>x; adj[x].push_back(i);
}
dfs(1);
int q; cin>>q;
for(int i=0;i<q;i++){
int u,d; cin>>u>>d;
query[u].push_back({i,d});
}
dfsz(1);
for(int i=0;i<q;i++) cout<<ans[i]<<'\n';
return 0;
}
|
// clang-format off
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#ifndef godfather
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#endif
#define int long long
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef gp_hash_table<int, int> umap;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update> oset;
typedef pair<pii, int> piii;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<pii> vii;
#define INF 1000000000000000000
#define inf 1000000000
// #define MOD 67280421310721
#define MOD 1000000007
// #define MOD 998244353
#define PI 3.1415926535897932385
#define pb push_back
#define bitc __builtin_popcountll
#define mp make_pair
#define all(ar) ar.begin(), ar.end()
#define fr(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define rep(i, n) for (int i = 0, _n = (n); i < _n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define frr(i, a, b) for (int i = (a), _b = (b); i >= _b; i--)
#define foreach(it, ar) for (auto it = ar.begin(); it != ar.end(); it++)
#define fil(ar, val) memset(ar, val, sizeof(ar)) // 0x3f for inf, 0x80 for -INF can also use with pairs
#ifdef godfather
template<typename T>
void __p(T a) { cout << a << " "; }
template<typename T>
void __p(std::vector<T> a) { cout << "{ "; for (auto p : a) __p(p); cout << "}"; }
template<typename T, typename F>
void __p(pair<T, F> a) { cout << "{ "; __p(a.first); __p(a.second); cout << "}"; }
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) { __p(a1); __p(a...); }
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
cout<<name<<" : ";__p(arg1);cout<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
int bracket=0,i=0;
for(; ;i++)
if(names[i]==','&&bracket==0)
break;
else if(names[i]=='(')
bracket++;
else if(names[i]==')')
bracket--;
const char *comma=names+i;
cout.write(names,comma-names)<<" : ";
__p(arg1);
cout<<"| ";
__f(comma+1,args...);
}
#define trace(...) cout<<"Line:"<<__LINE__<<" "; __f(#__VA_ARGS__, __VA_ARGS__)
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define trace(...)
#define end_routine()
#endif
mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count());
inline bool equals(double a, double b) { return fabs(a - b) < 1e-9; }
ll modpow(ll b, ll e, ll mod=MOD) {
ll ans=1; for(;e;b=b*b%mod,e/=2) if(e&1) ans=ans*b%mod; return ans; }
ld modp(ld b, ll e) {
ld ans=1; for(;e;b=b*b,e/=2) if(e&1) ans=ans*b; return ans; }
void solve()
{
// START
ll a,b;
cin >>a>>b;
ld ans = (a*b);
ans/=100;
cout<<ans<<"\n";
// END
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0), cout.precision(15); cout<<fixed;
#ifdef godfather
cin.exceptions(cin.failbit);
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t=1;
// cin>>t;
fr(i,1,t)
{
// cout<<"Case #"<<i<<": ";
solve();
}
end_routine();
} | #include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
ll B,C;
long a,b,c,d;
void fun1()//判断C的
{
ll n=C/2;
if(C%2==1)
{
a=-1*B-n;
b=-1*B+n;
}
else
{
if(C==0)
{
a=B;
b=B;
}
else
{
a=B - n;
b=B + n - 1;
}
}
// cout<<n<<endl;
// cout<<a<<" "<<b<<"**"<<endl;
}
void fun2()//判断C-1的
{
long long cc=C-1;
ll n=(cc)/2;
if(cc%2==1)
{
c=-1*B-n;
d=-1*B+n;
}
else
{
if(cc==0)
{
c=B;
d=B;
}
else
{
c=B - n;
d=B + n - 1;
}
}
// cout<<n<<endl;
// cout<<c<<" "<<d<<"**"<<endl;
}
int main()
{
while(cin>>B>>C)
{
fun1();
fun2();
cout << (b-a+1)+(d-c+1)-max(0l,min(b,d)-max(a,c)+1)<<endl;
}
// system("pause");
} |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
#define in inline
namespace fast_io
{
char buf[1<<12],*p1=buf,*p2=buf,sr[1<<23],z[23],nc;int C=-1,Z=0,Bi=0,ny;
in char gc() {return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<12,stdin),p1==p2)?EOF:*p1++;}
in ll read()
{
ll x=0;ny=1;while(nc=gc(),(nc<48||nc>57)&&nc!=EOF)if(nc==45)ny=-1;Bi=1;if(nc<0)return nc;
x=nc-48;while(nc=gc(),47<nc&&nc<58&&nc!=EOF)x=(x<<3)+(x<<1)+(nc^48),Bi++;return x*ny;
}
in db gf() {int a=read(),y=ny,b=(nc!='.')?0:read();return (b?a+(db)b/pow(10,Bi)*y:a);}
in int gs(char *s) {char c,*t=s;while(c=gc(),c<=32);*s++=c;while(c=gc(),c>32)*s++=c;return s-t;}
in void ot() {fwrite(sr,1,C+1,stdout);C=-1;}
in void flush() {if(C>1<<22) ot();}
template <typename T>
in void write(T x,char t)
{
int y=0;if(x<0)y=1,x=-x;while(z[++Z]=x%10+48,x/=10);
if(y)z[++Z]='-';while(sr[++C]=z[Z],--Z);sr[++C]=t;flush();
}
in void write(char *s) {int l=strlen(s);for(int i=0;i<l;i++)sr[++C]=*s++;sr[++C]='\n';flush();}
};
using namespace fast_io;
const int N=1e6+5;
int n,m,p[N],a[N],v[N];
int main()
{
n=read();a[1]=1;
for(int i=2;i<=n;i++)
{
if(!v[i]) p[++m]=i,a[i]=2;
for(int j=1;i*p[j]<=n&&j<=m;j++)
{
v[i*p[j]]=1;a[i*p[j]]=a[i]+1;
if(i%p[j]==0) break;
}
}
for(int i=1;i<=n;i++) write(a[i]," \n"[i==n]);
return ot(),0;
}
//Author: disangan233 | //a.9
#include<bits/stdc++.h>
#define ll long long
#define all(x) x.begin(),x.end()
#define CASE(t) cout<<"Case #"<<(t)<<": ";
#define endll endl
#define endl '\n'
#define mod 1000000007
#define INF 1e18
#define maxN 500005
#define ios ios_base::sync_with_stdio(false); cin.tie(NULL)
using namespace std;
ll ceill(ll a,ll b){return a/b+bool(a%b);}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b, a % b);}
ll lcm(ll a,ll b){return (a*b)/gcd(a,b);}
ll power(ll x,ll y){ll res=1;while(y>0){if(y&1)res=(res*x)%mod;x=(x*x)%mod;y>>=1;}return res;}
bool isPrime(ll n)
{
if(n<=1)return false;if(n<=3)return true;if(n%2==0 || n%3==0)return false;
for(ll i=5;i*i<=n;i=i+6)if(n%i==0 || n%(i+2)==0) return false; return true;
}
void primeFactors(ll n, vector<ll> &vec)
{
while(n%2==0)vec.push_back(2),n/=2;
for(ll i=3;i<=sqrt(n);i+=2)while(n%i==0)vec.push_back(i),n/=i;
if(n>2)vec.push_back(n);
}
//vector<ll>fact(maxN);
//void factpre(){fact[0]=1;for(ll i=1;i<maxN;i++)fact[i]=(fact[i-1]*1LL*i);}
//ll mul(ll a,ll b){return (a*1LL*b)%mod;}
//ll nCr(ll n,ll k){return mul(fact[n],power(mul(fact[k],fact[n-k]),mod-2));}
bool chk(ll n)
{
return n&&(!(n&(n-1)));
}
void solve()
{
ll n;cin>>n;
ll tmp=0;
for(ll i=1;i<=n;i++)
{
if(i==1 || chk(i))cout<<(++tmp)<<" ";
else cout<<tmp<<" ";
}
cout<<endl;
}
signed main()
{
ios;
ll tc=1; //cin>>tc;
for(ll TTT=1;TTT<=tc;TTT++)
{
//CASE(TTT);
solve();
}
}
|
#include<bits/stdc++.h>
#define MAX 1000000007
#define MOD 998244353
#define ll long long int
#define fo(i,a,n) for(int i=a;i<n;i++)
#define ull unsigned long long int
#define pb push_back
#define eb emplace_back
#define in insert
#define cl clear
#define ii pair<int,int>
#define ld long double
const ll INF = 1e18;
using namespace std;
void print(int arr[], int n){
fo(i,0,n){
cout<<arr[i]<<" ";
}
cout<<endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n,x;
cin>>n>>x;
vector<pair<int, int>> v(n);
fo(i,0,n){
cin>>v[i].first>>v[i].second;
}
int intake = 0;
fo(i,0,n){
intake += (v[i].first * v[i].second);
if( intake > x*100 ){
cout<<i+1<<endl;
return 0;
}
}
cout<<-1<<endl;
return 0;
} | #include<stdio.h>
int main(){
int n,x;
scanf("%d%d",&n,&x);
int sum=0;
for(int i=0;i<n;i++){
int v,p;
scanf("%d%d",&v,&p);
sum+=v*p;
if(sum>x*100){
printf("%d\n",i+1);
return 0;
}
}
puts("-1");
} |
#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
inline int my_getchar_unlocked(){
static char buf[1048576];
static int s = 1048576;
static int e = 1048576;
if(s == e && e == 1048576){
e = fread_unlocked(buf, 1, 1048576, stdin);
s = 0;
}
if(s == e){
return EOF;
}
return buf[s++];
}
inline void rd(int &x){
int k;
int m=0;
x=0;
for(;;){
k = my_getchar_unlocked();
if(k=='-'){
m=1;
break;
}
if('0'<=k&&k<='9'){
x=k-'0';
break;
}
}
for(;;){
k = my_getchar_unlocked();
if(k<'0'||k>'9'){
break;
}
x=x*10+k-'0';
}
if(m){
x=-x;
}
}
inline void rd(char &c){
int i;
for(;;){
i = my_getchar_unlocked();
if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF){
break;
}
}
c = i;
}
inline int rd(char c[]){
int i;
int sz = 0;
for(;;){
i = my_getchar_unlocked();
if(i!=' '&&i!='\n'&&i!='\r'&&i!='\t'&&i!=EOF){
break;
}
}
c[sz++] = i;
for(;;){
i = my_getchar_unlocked();
if(i==' '||i=='\n'||i=='\r'||i=='\t'||i==EOF){
break;
}
c[sz++] = i;
}
c[sz]='\0';
return sz;
}
struct MY_WRITER{
char buf[1048576];
int s;
int e;
MY_WRITER(){
s = 0;
e = 1048576;
}
~MY_WRITER(){
if(s){
fwrite_unlocked(buf, 1, s, stdout);
}
}
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
MY_WRITER_VAR.s = 0;
}
MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
my_putchar_unlocked(a);
}
inline void wt_L(int x){
int s=0;
int m=0;
char f[10];
if(x<0){
m=1;
x=-x;
}
while(x){
f[s++]=x%10;
x/=10;
}
if(!s){
f[s++]=0;
}
if(m){
my_putchar_unlocked('-');
}
while(s--){
my_putchar_unlocked(f[s]+'0');
}
}
inline void wt_L(long long x){
int s=0;
int m=0;
char f[20];
if(x<0){
m=1;
x=-x;
}
while(x){
f[s++]=x%10;
x/=10;
}
if(!s){
f[s++]=0;
}
if(m){
my_putchar_unlocked('-');
}
while(s--){
my_putchar_unlocked(f[s]+'0');
}
}
template<class T> inline T popFirst(multiset<T> &a){
T res = *(a.begin());
a.erase(a.begin());
return res;
}
template<class T> inline T getFirst(multiset<T> &a){
return *(a.begin());
}
template<class T> inline T popFirst(set<T> &a){
T res = *(a.begin());
a.erase(a.begin());
return res;
}
template<class T> inline T getFirst(set<T> &a){
return *(a.begin());
}
int N;
char S[500000+2];
char T[500000+2];
int main(){
int i;
int x;
int y;
long long res = 0;
set<int> ss;
set<int> tt;
rd(N);
rd(S);
rd(T);
for(i=(0);i<(N);i++){
if(S[i]=='1'){
ss.insert(i);
}
}
for(i=(0);i<(N);i++){
if(T[i]=='1'){
tt.insert(i);
}
}
while(ss.size() || tt.size()){
if(ss.size()==0){
wt_L(-1);
wt_L('\n');
return 0;
}
if(tt.size()==0 || getFirst(ss) < getFirst(tt)){
if(ss.size()==1){
wt_L(-1);
wt_L('\n');
return 0;
}
x = popFirst(ss);
y = popFirst(ss);
res += y - x;
continue;
}
else{
x = popFirst(ss);
y = popFirst(tt);
res += x - y;
}
}
wt_L(res);
wt_L('\n');
return 0;
}
// cLay varsion 20201115-1
// --- original code ---
// int N;
// char S[5d5+2], T[5d5+2];
// {
// int x, y;
// ll res = 0;
// set<int> ss, tt;
// rd(N,S,T);
// rep(i,N) if(S[i]=='1') ss.insert(i);
// rep(i,N) if(T[i]=='1') tt.insert(i);
// while(ss.size() || tt.size()){
// if(ss.size()==0) wt(-1), return 0;
// if(tt.size()==0 || getFirst(ss) < getFirst(tt)){
// if(ss.size()==1) wt(-1), return 0;
// x = popFirst(ss);
// y = popFirst(ss);
// res += y - x;
// continue;
// } else {
// x = popFirst(ss);
// y = popFirst(tt);
// res += x - y;
// }
// }
// wt(res);
// }
| #include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mod 1000000007
#define pii pair<ll,ll>
#define inf 1000000000000000000
#define bpc(x) __builtin_popcountll(x)
#define autoit(x,it) for(auto it = x.begin(); it != x.end(); it++)
#define autoitr(x,it) for(auto it = x.rbegin(); it != x.rend(); it++)
#define rep(n) for(ll i = 0; i < n; i++)
#define repi(i,n) for(ll i = 0; i < n; i++)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
int main()
{
FAST/**/
ll n,m;
cin>>n>>m;
string arr[n];
ll cnt = 0;
rep(n)
cin>>arr[i];
rep(n-1)
{
repi(j,m)
if(arr[i][j] == '.' && arr[i+1][j] == '.')
cnt++;
}
repi(j,m-1)
rep(n)
if(arr[i][j] == '.' && arr[i][j+1] == '.')
cnt++;
cout<<cnt;
return 0;
}
|
#include <iostream>
#include <vector>
#include <tuple>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#define ll long long
#define rep(i,n) for(int i = 0;i < (int)n;i ++)
#define mod % 998244353
using namespace std;
int main(void){
ll n;
cin >> n;
ll A[n+1];
A[0] = 1;
rep(i,n){
A[i+1] = (A[i]*(i+1))mod;
}
vector<ll> w(n);
ll S = 3;
rep(i,n){
cin >> w[i];
S += w[i];
}
sort(w.begin(),w.end());
ll Q = S;
vector<vector<vector<ll>>> P(n+1,vector<vector<ll>>(Q,vector<ll>(n+1,0)));
P[0][Q/2][0] = 1;
rep(i,n){
rep(j,Q){
rep(k,n+1){
P[i][j][k] = P[i][j][k] mod;
if(P[i][j][k] > 0){
if(j+w[i]<Q)P[i+1][j+w[i]][k+1] = (P[i+1][j+w[i]][k+1]+P[i][j][k]) mod;
if(j-w[i]>=0)P[i+1][j-w[i]][k] = (P[i+1][j-w[i]][k]+P[i][j][k]) mod;
}
}
}
}
vector<ll> ans = P[n][Q/2];
ll sum = 0;
rep(i,n){
ll M = (A[i]*A[n-i]) mod;
sum = (sum+ans[i]*M) mod;
}
cout << sum << endl;
return 0;
} | #include<bits/stdc++.h>
#define cs const
#define pb push_back
using namespace std;
cs int mod = 998244353;
int add(int a, int b) {
return a + b >= mod ? a + b - mod : a + b;
}
int mul(int a, int b) {
return 1ll * a * b % mod;
}
int dec(int a, int b) {
return a - b < 0 ? a - b + mod : a - b;
}
void Add(int &a, int b) {
a = add(a, b);
}
void Dec(int &a, int b) {
a = dec(a, b);
}
void Mul(int &a, int b) {
a = mul(a, b);
}
int ksm(int a, int b) {
int ans = 1; for(; b; b >>= 1, Mul(a, a))
if(b & 1) Mul(ans, a);
}
cs int N = 105, M = N * N;
int n, a[N];
int dp[N][M], fc[N];
int main() {
#ifdef FSYo
freopen("1.in", "r", stdin);
#endif
cin >> n;
dp[0][0] = 1;
int S = 0;
for(int i = 1, x; i <= n; i++) {
scanf("%d", &x);
for(int j = i - 1; ~j; j--)
for(int k = 0, c; k <= S; k++)
if(c = dp[j][k])
Add(dp[j + 1][k + x], c);
S += x;
}
if(S & 1) return puts("0"), 0;
S >>= 1;
fc[0] = 1;
for(int i = 1; i <= n; i++)
fc[i] = mul(fc[i - 1], i);
int ans = 0;
for(int i = 1; i < n; i++) {
Add(ans, mul(dp[i][S], mul(fc[i], fc[n - i])));
}
cout << ans;
return 0;
} |
#include<bits/stdc++.h>
#define pb emplace_back
#define AI(i) begin(i), end(i)
using namespace std;
using ll = long long;
template<class T>
bool chmax(T &val, T nv) { return val < nv ? (val = nv, true) : false; }
template<class T>
bool chmin(T &val, T nv) { return nv < val ? (val = nv, true) : false; }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() {cerr << endl;}
template<class T1, class ...T2>
void kout (T1 v, T2 ...e) { cerr << v << ' ', kout(e...); }
template<class T>
void debug(T L, T R) { while (L != R) cerr << *L << " \n"[next(L)==R], ++L; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
// What I should check
// 1. overflow
// 2. corner cases
// Enjoy the problem instead of hurrying to AC
// Good luck !
const int MAX_N = 300010;
ll dfs(ll v, ll k) {
static map<ll,ll> dp[100];
if (k == 0) return abs(v);
if (dp[k].count(v)) return dp[k][v];
ll ad = 1ll << k;
if (abs(v) >= 2 * ad) return abs(v);
ll &ret = dp[k][v] = abs(v);
chmin(ret, 0 + dfs(v, k-1));
DE(v, k, ret);
chmin(ret, 1 + dfs(v-ad, k-1));
DE(v, k, ret);
chmin(ret, 1 + dfs(v+ad, k-1));
DE(v, k, ret);
assert(ret >= 0);
return ret;
}
ll solve(ll v, ll k) {
if (k == 0) return abs(v);
ll ad = 1ll << k;
ll d = v / ad;
ll ret = abs(v);
DE(v, k);
chmin(ret, abs(d) + dfs(v - d * ad, k));
cerr << "Sv ", DE(v, k, ret);
chmin(ret, abs(d+1) + dfs(v - (d+1) * ad, k));
cerr << "Sv ", DE(v, k, ret);
if (d)
chmin(ret, abs(d-1) + dfs(v - (d-1) * ad, k));
cerr << "Sv ", DE(v, k, ret);
return ret;
}
int32_t main() {
ios_base::sync_with_stdio(0), cin.tie(0);
ll x, y;
cin >> x >> y;
ll res = abs(x - y);
for (int k = 1;; ++k) {
x *= 2;
ll sv = solve(y - x, k);
DE(k, y - x, sv);
chmin(res, sv + k);
if (x > y) break;
}
cout << res << '\n';
}
| #include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
#define int long long
int h, w;
bool is_in(int i, int j){
return 0 <= i && i < h && 0 <= j && j < w;
}
int INF = 1<<30;
int a[2000][2000];
int point[2000][2000];
int ad(int i, int j){
bool flag1 = false, flag2 = false;
if(point[i][j] != INF) return point[i][j];
int ans1, ans2;
int c = (2*((i+j)%2 == 0)-1);
if(is_in(i+1, j)){
flag1 = true;
ans1 = ad(i+1, j);
ans1 += c * a[i+1][j];
}
if(is_in(i, j+1)){
flag2 = true;
ans2 = ad(i, j+1);
ans2 += c * a[i][j+1];
}
int ans = 0;
if((i+j)%2 == 1 && (flag1 || flag2)){
ans = 1<<30;
if(flag1) ans = min(ans, ans1);
if(flag2) ans = min(ans, ans2);
}
else if(flag1 || flag2){
ans = -(1<<30);
if(flag1) ans = max(ans, ans1);
if(flag2) ans = max(ans, ans2);
}
point[i][j] = ans;
return ans;
}
signed main(){
// cout << fixed << setprecision(10) << flush;
cin >> h >> w;
for(int i=0; i<h; i++){
for(int j=0; j<w; j++){
char c;
cin >> c;
if(c == '+') a[i][j] = 1;
else a[i][j] = -1;
}
}
for(int i=0; i<h; i++){
for(int j=0; j<w; j++){
point[i][j] = INF;
}
}
int ans = ad(0, 0);
// cout << ans << endl;
if(ans > 0) cout << "Takahashi" << endl;
else if(ans < 0) cout << "Aoki" << endl;
else cout << "Draw" << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#include <algorithm>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using ll = long long;
using P = pair<int,int>;
using T = tuple<int,int,int>;
using Graph = vector<vector<int>>;
const int INF = 1001001001;
void printVec(vector<int> &vec) {
for (auto itr = vec.begin(); itr != vec.end(); itr++) cout << *itr << " ";
cout << endl;
}
int main() {
ll N;
cin >> N;
vector<ll> vec;
for(ll i = 2; i <= 100001; i++) {
for(ll j = 2; j <= 35; j++) {
ll a = pow(i, j);
if(a > N) break;
vec.push_back(a);
}
}
sort(vec.begin(), vec.end());
vec.erase(unique(vec.begin(), vec.end()), vec.end());
cout << N - vec.size() << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(0)->sync_with_stdio(0);
long long n; cin >> n;
map<long long,bool> m;
long long ans = n;
for (long long i = 2; i * i <= n; i++) {
if (m.count(i)) continue;
long long t = i * i;
while (t <= n) {
ans--;
m[t] = true;
t *= i;
}
}
cout << ans << '\n';
}
|
#include <bits/stdc++.h>
#include <unordered_set>
#include <cmath>
#include <algorithm>
// #include <atcoder/all>
// URL:
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vs = vector<string>;
#define dump(x) cout << #x << " = " << (x) << endl
#define YES(n) cout << ((n)? "YES": "NO") << endl
#define Yes(n) cout << ((n)? "Yes": "No") << endl
#define rept(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define fore(x, a) for(auto& (x) : (a))
#define reptll(i, a, b) for(ll i = (ll)(a); i < (ll)(b); i++)
#define repll(i, n) for (ll i = 0; i < (ll)(n); i++)
#define ALL(a) (a).begin(), (a).end()
#define VECCIN(x) for(auto& youso_: (x)) cin >> youso_
#define VECCOUT(x) for(auto& youso_: (x)) cout << youso_ << " ";cout << endl
#define pb push_back
#define optimize_cin() cin.tie(0); ios::sync_with_stdio(false)
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const ll INFL = numeric_limits<ll>::max() / 4;
const int INF = 1e9;
const int MOD = 1e9 + 7;
int main(){
optimize_cin();
int N;
cin >> N;
cout << (N - 1)/100 + 1 << endl;
return 0;
} | #include <stdio.h>
#include <string>
#include <algorithm>
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n); i++)
using ll = long long;
using P = pair<int, int>;
int main()
{
int n, start = 0, end = 1005;
int a, b;
cin >> n;
rep(i,n){
cin >> a;
start = max(start, a);
}
rep(i,n){
cin >> b;
end = min(end, b);
}
if(start > end) cout << 0 << endl;
else cout << end - start + 1 << endl;
} |
#include<ctime>
#include<cstdio>
#include<cctype>
#define ll long long
using namespace std;
const ll N=2e6+7;
const ll p=1e9+7;
ll read(){
char c;
ll x=0,f=1;
while(!isdigit(c=getchar()))
f-=2*(c=='-');
while(isdigit(c)){
x=x*10+f*(c-48);
c=getchar();
}
return x;
}
ll n,m,k,jc[N],inv[N];
ll C(ll i,ll j){
if(j>i)
return 0;
return jc[i]*inv[j]%p*inv[i-j]%p;
}
ll HgS(ll a,ll b){
ll res=1;
while(b){
if(b&1)
res=res*a%p;
a=a*a%p;
b>>=1;
}
return res;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("E.in","r",stdin);
freopen("E.out","w",stdout);
#endif
clock_t t1=clock();
//--------
n=read();
m=read();
k=read();
jc[0]=1;
for(ll i=1;i<=n+m;++i)
jc[i]=jc[i-1]*i%p;
inv[n+m]=HgS(jc[n+m],p-2);
for(ll i=n+m-1;~i;--i)
inv[i]=inv[i+1]*(i+1)%p;
if(n>m+k)
puts("0");
else
printf("%lld",(C(n+m,n)-C(n+m,m+k+1)+p)%p);
//--------
fprintf(stderr,"%0.3lfs",1.0*(clock()-t1)/CLOCKS_PER_SEC);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, n) FOR(i, 0, n)
#define FOR(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()
#define CEIL(a, b) ((a - 1) / b + 1)
void solve() {
int N;
cin >> N;
vector<pair<double, double>> A(N);
REP(i, N) {
int t;
double l, r;
cin >> t >> l >> r;
if (t == 2) {
r -= 0.1;
} else if (t == 3) {
l += 0.1;
} else if (t == 4) {
r -= 0.1;
l += 0.1;
}
A[i] = {l, r};
}
int ans = 0;
REP(i, N) {
FOR(j, i + 1, N) {
// 共通区間(左端の最大値 <= 右端の最小値)
ans +=
(max(A[i].first, A[j].first) <= min(A[i].second, A[j].second));
}
}
cout << ans << endl;
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
solve();
}
|
#include <iostream>
#include <vector>
using namespace std;
int main() {
int N, M;
cin >> N >> M;
if (N == 1 && M == 0) {
cout << 1 << " " << 2 << "\n";
return 0;
}
if (M < 0 || M >= N - 1) {
cout << -1 << "\n";
return 0;
}
vector<long long> L(N), R(N);
for (int i = 0; i < N; i++) {
L[i] = 3 * i, R[i] = 3 * i + 1;
}
if (M > 0) {
R[0] = 3 * (M + 2) - 1;
}
for (int i = 0; i < N; i++) {
cout << L[i] + 1 << " " << R[i] + 1 << "\n";
}
return 0;
} |
/**
* Dont raise your voice, improve your argument.
* --Desmond Tutu
*/
#include <bits/stdc++.h>
using namespace std;
const bool ready = [](){
ios_base::sync_with_stdio(false); cin.tie(0);
cout << fixed << setprecision(12);
return true;
}();
const double PI = acos(-1);
using ll= long long;
#define int ll
#define all(v) (v).begin(), (v).end()
#define fori(n) for(int i=0; i<int(n); i++)
#define cini(i) int i; cin>>i;
#define cins(s) string s; cin>>s;
#define cind(d) double d; cin>>d;
#define cinai(a,n) vi a(n); fori(n) { cin>>a[i]; }
#define cinas(s,n) vs s(n); fori(n) { cin>>s[i]; }
#define cinad(a,n) vd a(n); fori(n) { cin>>a[i]; }
using pii= pair<int, int>;
using pdd= pair<double, double>;
using vd= vector<double>;
using vb= vector<bool>;
using vi= vector<int>;
using vvi= vector<vi>;
using vs= vector<string>;
#define endl "\n"
/*
* Both print min 1 and max N
* Value of second cannot be bigger than first since
* first is optimal.
* So M<0 is not possible.
*
* if M==0 choose all non overlapping like
* (1,2),(3,4),...,(n*2-1,n*2)
*
* if M==1 make first overlap the next two, like
* (1,6),(2,3),(4,5),(7,8),...
*
* if M==N-1 ?
* -> Not possible
*/
void solve() {
cini(n);
cini(m);
if(n==1 && m==0) {
cout<<"1 2"<<endl;
return;
}
if(m<0 || m>=n-1) {
cout<<-1<<endl;
return;
}
set<int> s;
for(int i=2; i<=n*2; i++)
s.insert(i);
vi l(n);
vi r(n);
l[0]=1;
if(m>0) {
r[0]=4+m*2;
s.erase(4+m*2);
} else {
r[0]=2;
s.erase(2);
}
for(int i=1; i<n; i++) {
auto it=s.begin();
l[i]=*it;
s.erase(it);
it=s.begin();
r[i]=*it;
s.erase(it);
}
for(int i=0; i<n; i++)
cout<<l[i]<<" "<<r[i]<<endl;
}
signed main() {
solve();
}
// FIRST THINK, THEN CODE
// DO NOT JUMP BETWEEN PROBLEMS
|
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 5010;
const ll mod = 998244353;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a) {
char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {if (c == '-')f = -1; c = getchar();}
while (isdigit(c)) {x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}
int gcd(int a, int b) {return (b > 0) ? gcd(b, a % b) : a;}
ll ksm (ll a , ll b){ ll ans = 1 , base = a;
while (b){if (b & 1) ans = ans * base % mod;b >>= 1;base = base * base % mod;}return ans;}
ll f[N],inv[N];
ll dp[N][N];
int n,m;
ll C (ll a,ll b){
if (a < b) return 0;
return f[a] * inv[a - b] % mod * inv[b] % mod;
}
int main() {
f[0] = inv[0] = 1;
for (int i = 1 ; i < N ; i++){
f[i] = f[i - 1] * i % mod;
inv[i] = ksm(f[i] , mod - 2);
}
read(n),read(m);
dp[0][0] = 1;
for(int i = 1;i <= 20;i++){
if((1 << (i - 1)) > m){
dl(dp[i - 1][m]);
return 0;
}
for(int k = 0;k <= n;k += 2){
ll t = k * (1 << (i - 1));
if(t > m) break;
for(int j = t;j <= m;j++) dp[i][j] = (dp[i][j] + dp[i - 1][j - t] * C(n,k)) % mod;
}
}
return 0;
}
/**
* ┏┓ ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/
| #include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
using ll=long long;
using namespace std;
//const int mod = 1000000007;
const int mod = 998244353;
struct mint {
ll x; // typedef long long ll;
mint(ll x=0):x((x%mod+mod)%mod){}
mint operator-() const { return mint(-x);}
mint& operator+=(const mint a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint a) {
if ((x += mod-a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
mint operator+(const mint a) const { return mint(*this) += a;}
mint operator-(const mint a) const { return mint(*this) -= a;}
mint operator*(const mint a) const { return mint(*this) *= a;}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t>>1);
a *= a;
if (t&1) a *= *this;
return a;
}
// for prime mod
//mint inv() const { return pow(mod-2);}
//supports not prime
mint modinv(ll a) {
long long b = mod, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b; swap(a, b);
u -= t * v; swap(u, v);
}
u %= mod;
if (u < 0) u += mod;
mint res(u);
return res;
}
mint& operator/=(const mint a) { return *this *= modinv(a.x);}
mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
//combination
//under mint
vector<mint> fact;
vector<mint> finv;
void comb_init(int n){
fact=vector<mint>(n+1);
finv=vector<mint>(n+1);
fact[0]=1;
for(int i=1;i<=n;i++){
fact[i]=fact[i-1]*i;
}
finv[0]=1;
finv[n]=mint(1)/fact[n];
for(int i=n;i>=2;i--){
finv[i-1]=finv[i]*i;
}
}
mint comb(int a,int b){
return fact[a]*finv[b]*finv[a-b];
}
int main(){
int N,M;
cin>>N>>M;
comb_init(N+1);
//vector<vector<vector<mint>>> dp(13,vector<vector<mint>> (N+1,vector<mint>(M+1,0)));
vector<vector<mint>> dp(14,vector<mint>(M+1,0));
dp[0][0]=1;
for(int i=1;i<=13;i++){
for(int j=0;j<=M;j++){
for(int k=0;k<=N;k+=2){
int base=1<<(i-1);
if(base*k > j){
continue;
}
dp[i][j]+=comb(N,k)*dp[i-1][j-base*k];
}
}
}
/*
for(auto p:dp){
for(auto q:p){
cout<<q<<" ";
}
cout<<endl;
}
//*/
cout<<dp[13][M]<<endl;
return 0;
} |
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 500500;
int t[maxn];
int n;
void upd(int p,int x) {
for(; p < maxn; p |= (p + 1)) t[p] += x;
}
int get(int p) {
int r = 0;
for(; p >= 0; p = (p & (p + 1)) - 1) r += t[p];
return r;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n;
vector<pair<int, int>> a(n), b(n);
for(int i = 0; i < n; i++) {
cin >> a[i].first;
a[i].first += i;
a[i].second = i;
}
for(int i = 0; i < n; i++) {
cin >> b[i].first;
b[i].first += i;
b[i].second = i;
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
vector<int> p(n);
for(int i = 0; i < n; i++) {
if(a[i].first != b[i].first) {
cout << -1;
return 0;
}
p[a[i].second] = b[i].second;
}
ll ans = 0;
for(int i = n - 1; i >= 0; i--) {
ans += get(p[i]);
upd(p[i], 1);
}
cout << ans;
} | #include <iostream>
#include <algorithm>
#include <unordered_set>
#include <set>
#include <vector>
#include <queue>
#include <map>
#include <numeric>
#include <math.h>
#include <complex>
using namespace std;
#define rep(i, n) for (long long int i = 0; i < (long long int)(n); i++)
#define irep(i, n) for (long long int i = 1; i <= (long long int)(n); i++)
#define drep(i, n, diff) for (long long int i = 0; i < (long long int)(n); i += diff)
#define mod 1000000007
#define INF 1001001001001001011
#define ld(val) printf("%s : %lld\n", #val, val);
#define sd(val) printf("%s : %s\n", #val, val.c_str());
typedef long long int ll;
typedef pair<ll, ll> P;
typedef pair<P, P> PP;
typedef tuple<ll, ll, ll> TP;
int main() {
ll n;
cin >> n;
vector<ll> a(n);
vector<ll> b(n);
vector<ll> c(n);
rep(i, n) {
cin >> a[i];
}
rep(i, n) {
cin >> b[i];
}
rep(i, n) {
cin >> c[i];
c[i]--;
}
map<ll, ll> mp;
rep(i, n) {
mp[b[c[i]]]++;
}
ll ans = 0;
rep(i, n) {
ans += mp[a[i]];
}
cout << ans << endl;
return 0;
} |
#include "bits/stdc++.h"
using namespace std;
#define rep(i, a, b) for(int i=a; i<=b; i++)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int) x.size()
#define f first
#define s second
#define nl "\n"
#define pb push_back
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
const int MOD = 1e9+7;
template<class T> using pqg = priority_queue<T,vector<T>,greater<T>>;
int n, m;
vector<pii> edges[26];
bool e[1000][1000];
vi adj[1000*999+999];
int dist[1000*999+999];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
cin >> n >> m;
rep(i, 1, m){
int u, v; char c;
cin >> u >> v >> c;
u--; v--;
edges[c-'a'].pb({u, v});
edges[c-'a'].pb({v, u});
e[u][v]=e[v][u]=true;
}
rep(c, 0, 25){
rep(i, 0, sz(edges[c])-1){
rep(j, i+1, sz(edges[c])-1){
int a=edges[c][i].f;
int b=edges[c][i].s;
int C=edges[c][j].f;
int d=edges[c][j].s;
adj[1000*min(a, C)+max(a, C)].pb(1000*min(b, d)+max(b, d));
}
}
}
//shortest dist from 1000*1+n to 1000a+a or 1000a+b (a-b)
pqg<pii> pq;
int ans=1e9;
pq.push({0, n-1});
while(!pq.empty()){
pii p=pq.top(); pq.pop();
int d=p.f;
int u=p.s/1000;
int v=p.s%1000;
if(dist[p.s]) continue;
dist[p.s]=d;
if(u==v){
ans=min(ans, 2*d);
}
if(e[u][v]){
ans=min(ans, 2*d+1);
}
trav(k, adj[p.s]){
if(dist[k]==0) pq.push({d+1, k});
}
}
cout << (ans==1e9?-1:ans);
} | #include<bits/stdc++.h>
#define ll long long
#define N 2005
using namespace std;
struct node{
int to,nxt;
}e[N*N*2];
int head[N],tot,d[N];
void build(int a,int b)
{
++d[a],++d[b];
e[++tot].to=b;
e[tot].nxt=head[a];
head[a]=tot;
e[++tot].to=a;
e[tot].nxt=head[b];
head[b]=tot;
}
int n,m,vis[N];
char mp[N][N];
void dfs(int p)
{
vis[p]=1;
for(int i=head[p];i;i=e[i].nxt)
{
if(!vis[e[i].to])
dfs(e[i].to);
}
}
int main()
{
#ifdef lovelyG
Sfile(1);
#endif
scanf("%d%d",&n,&m);
build(1,1+n);
build(1,m+n);
build(n,1+n);
build(n,m+n);
for(int i=1;i<=n;++i)
{
scanf("%s",mp[i]+1);
for(int j=1;j<=m;++j)
{
if(mp[i][j]=='#')
build(i,j+n);
}
}
int numr=0,numc=0;
for(int i=1;i<=n;++i)
{
if(!vis[i])
{
++numr;
if(d[i])++numc;
dfs(i);
}
}
for(int i=1;i<=m;++i)
{
if(!vis[i+n])
{
++numc;
}
}
printf("%d",min(numr,numc)-1);
return 0;
} |
#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
#include<map>
#include<set>
#include<cstdlib>
#include<bitset>
using namespace std;
#define FAST ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
typedef long long ll;
#define N 200005
ll p=1e9+7;
ll n,m,f[N][17][17]={0},v[22]={0},sm;
char a[N];
int main()
{
ll i,j,k,a1,a2,an=0;
scanf("%s",a+1);
n=strlen(a+1);
for(ll i=1;i<=n;i++)
if(a[i]>='0'&&a[i]<='9')
a[i]-='0';
else
a[i]-='A'-10;
scanf("%lld",&m);
for(ll i=0;i<=m;i++)
f[0][i][i]=1;
for(ll i=1;i<=n;i++)
for(ll j=1;j<=m;j++)
for(ll k=j;k<=m;k++)
f[i][j][k]=((ll)f[i-1][j][k-1]*(17ll-k)+(ll)f[i-1][j][k]*k)%p;
for(ll i=1;i<=n;i++)
an=(an+15ll*f[i-1][1][m])%p;
// for(ll i=1;i<=n;i++)
// {
// for(ll j=1;j<=m;j++)
// for(ll k=j;k<=m;k++)printf("%lld\n",f[i][j][1]);
// }
for(ll i=1;i<=n;i++)
{
for(ll j=a[i]+1;j<=15;j++)
an=(an-f[n-i][sm+(!v[j])][m]+p)%p;
//printf("%lld\n",an);
sm+=!v[a[i]];
v[a[i]]=1;
}
printf("%lld",an);
} | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#ifdef ENABLE_DEBUG
#define dump(a) cerr<<#a<<"="<<a<<endl
#define dumparr(a,n) cerr<<#a<<"["<<n<<"]="<<a[n]<<endl
#else
#define dump(a)
#define dumparr(a,n)
#endif
#define FOR(i, a, b) for(ll i = (ll)a;i < (ll)b;i++)
#define For(i, a) FOR(i, 0, a)
#define REV(i, a, b) for(ll i = (ll)b-1LL;i >= (ll)a;i--)
#define Rev(i, a) REV(i, 0, a)
#define REP(a) For(i, a)
#define SIGN(a) (a==0?0:(a>0?1:-1))
typedef long long int ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<ll, ll> pll;
typedef pair<ll,pll> ppll;
typedef vector<ll> vll;
typedef long double ld;
typedef pair<ld,ld> pdd;
pll operator+(pll a,pll b){
return pll(a.first+b.first,a.second+b.second);
}
pll operator-(pll a,pll b){
return pll(a.first-b.first,a.second-b.second);
}
pll operator*(ll a,pll b){
return pll(b.first*a,b.second*a);
}
const ll INF=(1LL<<60);
#if __cplusplus<201700L
ll gcd(ll a, ll b) {
a=abs(a);
b=abs(b);
if(a==0)return b;
if(b==0)return a;
if(a < b) return gcd(b, a);
ll r;
while ((r=a%b)) {
a = b;
b = r;
}
return b;
}
#endif
template<class T>
bool chmax(T& a,const T& b){
if(a<b){
a=b;
return true;
}
return false;
}
template<class T>
bool chmin(T& a,const T& b){
if(a>b){
a=b;
return true;
}
return false;
}
template<class S,class T>
std::ostream& operator<<(std::ostream& os,pair<S,T> a){
os << "(" << a.first << "," << a.second << ")";
return os;
}
template<class T>
std::ostream& operator<<(std::ostream& os,vector<T> a){
os << "[ ";
REP(a.size()){
os<< a[i] << " ";
}
os<< " ]";
return os;
}
const long long MOD = 1000000007;
const long long max_cache=100;
long long cache_fact[max_cache],cache_inv_fact[max_cache];
long long combination_cached(long long n,long long r){
ll m=MOD;
static bool cached=false;
if(cached==false){
cached=true;
cache_inv_fact[1]=1;
for (long long i = 2; i < max_cache; i++) {
cache_inv_fact[i]=(m-m/i)*cache_inv_fact[m%i]%m;
}
for (long long i = 2; i < max_cache; i++){
cache_inv_fact[i]=cache_inv_fact[i]*cache_inv_fact[i-1]%m;
}
cache_fact[0]=1;
for (long long i = 1; i < max_cache; i++) {
cache_fact[i]=cache_fact[i-1]*i%m;
}
}
if(r<0||r>n)return 0;
if(r==0||r==n)return 1;
if(n>m){
std::cerr<<"Combination Error: n is greater than m"<<std::endl;
exit(1);
}
r=min(r,n-r);
return cache_fact[n]*cache_inv_fact[r]%m*cache_inv_fact[n-r]%m;
}
long long modpow(long long a,long long b){
long long base=a,ans=(1LL);
while(b){
if(b&1){
ans=ans*base%MOD;
}
base=base*base%MOD;
b>>=1;
}
return ans;
}
long long invpow(long long a){
return modpow(a,MOD-2);
}
ll solve_nk(ll n,ll k,ll len){
ll ans=combination_cached(16-n,k);
ll tmp=0;
REP(k+1){
tmp+=modpow(MOD-1,i)*combination_cached(k,i)%MOD*modpow(k-i+n,len)%MOD;
}
return ans*tmp%MOD;
}
void solve(std::string N, long long K){
auto c2i=[](char x){
if('0'<=x&&x<='9'){
return (ll)(x-'0');
}else{
return (ll)(x-'A')+10;
}
};
unordered_map<ll,ll> m;
auto inc_m=[&m](ll x){
if(m.count(x)){
++m[x];
}else{
m[x]=1;
}
};
auto dec_m=[&m](ll x){
assert(m.count(x));
--m[x];
if(m[x]==0){
m.erase(x);
}
};
ll ans=0;
REP(N.size()-1){
ans=(ans+15*solve_nk(1,K-1,i)%MOD)%MOD;
}
REP(N.size()){
FOR(j,0,c2i(N[i])){
if(i==0&&j==0)continue;
inc_m(j);
ans=(ans+solve_nk(m.size(),K-m.size(),N.size()-i-1))%MOD;
dec_m(j);
}
inc_m(c2i(N[i]));
}
if(m.size()==K){
++ans;
}
cout<<ans<<endl;
}
int main(){
cout<<setprecision(1000);
std::string N;
std::cin >> N;
long long K;
scanf("%lld",&K);
solve(N, K);
return 0;
}
|
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
#define chmin(a,b) a=min(a,b)
#define chmax(a,b) a=max(a,b)
#define mod 1000000007
#define ad(a,b) a=(a+b)%mod;
#define X 200010
ll po(ll x,ll y){
ll res=1;
for(;y;y>>=1){
if(y&1)res=res*x%mod;
x=x*x%mod;
}
return res;
}
ll gcd(ll a,ll b){
return (b?gcd(b,a%b):a);
}
ll fac[X],ivf[X];
void initfac(){
fac[0]=1;
for(ll i=1;i<X;i++)fac[i]=fac[i-1]*i%mod;
for(ll i=0;i<X;i++)ivf[i]=po(fac[i],mod-2);
}
ll C(ll n,ll k){
return fac[n]*ivf[n-k]%mod*ivf[k]%mod;
}
ll n,a[210],b[210];
vector<ll> A[210],B[210];//A[p]:=(a[i]==p)=なるiのset
bool f[210][210];
bool solve(){
cin>>n;
rep(i,n){
cin>>a[i]>>b[i];
if(a[i]>=0)a[i]--;
if(b[i]>=0)b[i]--;
if(a[i]!=-1&&b[i]!=-1&&a[i]>=b[i])return 0;
if(a[i]>=0)A[a[i]].push_back(i);
if(b[i]>=0)B[b[i]].push_back(i);
}
rep(i,2*n){
if(A[i].size()+B[i].size()>1)return 0;
}//重複なし
for(int i=0;i<=2*n;i++)for(int j=0;j<=2*n;j++)f[i][j]=0;
for(int l=0;l<2*n;l+=2){
for(int k=1;l+2*k<=2*n;k++){
int r=l+2*k;
bool ok=1;
rep(i,n){
bool va=(l<=a[i]&&a[i]<r);
bool vb=(l<=b[i]&&b[i]<r);
if(a[i]>=0&&b[i]>=0){
if(va==0&&vb==0)continue;
if(va==1&&vb==1){
ok&=(a[i]+k==b[i]);
continue;
}
ok=0;
}
else if(a[i]==-1&&b[i]==-1)continue;
else if(a[i]>=0){
if(va==0)continue;
if(not(0<=a[i]+k&&a[i]+k<2*n))ok=0;
else if(A[a[i]+k].size()+B[a[i]+k].size()>0)ok=0;
}
else if(b[i]>=0){
if(vb==0)continue;
if(not(0<=b[i]-k&&b[i]-k<2*n))ok=0;
else if(A[b[i]-k].size()+B[b[i]-k].size()>0)ok=0;
}
}
f[l][r]=ok;
//cerr<<"f["<<l<<"]["<<r<<"]="<<f[l][r]<<endl;
}
}
bool dp[210];
for(int i=0;i<=2*n;i++)dp[i]=0;
dp[0]=1;
for(int i=0;i<2*n;i++){
if(dp[i]==0)continue;
for(int j=i+1;j<=2*n;j++){
if(f[i][j]==1){
dp[j]=1;
}
}
}
return dp[2*n];
}
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
cout<<(solve()?"Yes":"No")<<endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define mod 1000000007ll
#define rep(i, n) for(int i = 0; i < n; i++)
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
int main() {
int n; cin >> n;
vector<pair<string, int>> s(n);
for (int i=0; i<n; i++) {
cin >> s[i].first;
if (s[i].first[0] == '!') {
s[i].first.erase(0, 1);
s[i].second = 1;
} else {
s[i].second = 0;
}
}
sort(s.begin(), s.end());
for (int i=0; i<n-1; i++) {
if (s[i].first == s[i+1].first && s[i].second != s[i+1].second) {
cout << s[i].first << endl;
return 0;
}
}
cout << "satisfiable" << endl;
return 0;
} |
/*
これを入れて実行
g++ code.cpp
./a.out
*/
#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <vector>
#include <string>
#include <cstring>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <math.h>
#include <tuple>
#include <iomanip>
#include <bitset>
#include <functional>
#include <cassert>
#include <random>
#define all(x) (x).begin(),(x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;
typedef long double ld;
int dy4[4] = {-1, 0, +1, 0};
int dx4[4] = {0, +1, 0, -1};
int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const long long INF = 1LL << 60;
const ll MOD = 1e9 + 7;
bool greaterSecond(const pair<int, int>& f, const pair<int, int>& s){
return f.second > s.second;
}
ll gcd(ll a, ll b){
if (b == 0)return a;
return gcd(b, a % b);
}
ll lcm(ll a, ll b){
return a / gcd(a, b) * b;
}
ll conbinationMemo[100][100];
void cmemoInit(){
rep(i, 100){
rep(j, 100){
conbinationMemo[i][j] = -1;
}
}
}
ll nCr(ll n, ll r){
if(conbinationMemo[n][r] != -1) return conbinationMemo[n][r];
if(r == 0 || r == n){
return 1;
} else if(r == 1){
return n;
}
return conbinationMemo[n][r] = (nCr(n - 1, r) + nCr(n - 1, r - 1));
}
ll nPr(ll n, ll r){
r = n - r;
ll ret = 1;
for (ll i = n; i >= r + 1; i--) ret *= i;
return ret;
}
//-----------------------ここから-----------
ll modpow(ll a, ll b){
ll res = 1;
while(b > 0){
if(b & 1){
res *= a;
res %= MOD;
}
a *= a;
a %= MOD;
b >>= 1;
}
return res;
}
int main(void){
ll h, w;
cin >> h >> w;
vector<string> s(h);
rep(i, h) cin >> s[i];
vector<vector<ll>> ve(h, vector<ll>(w, 0)), ho(h, vector<ll>(w, 0));
ll k = 0;
rep(i, h){
ll cnt = 0;
rep(j, w){
if(s[i][j] == '.'){
k++;
cnt++;
} else {
cnt = 0;
}
ho[i][j] = cnt;
}
for(ll j = w - 2; j >= 0; j--){
if(s[i][j] == '.'){
ho[i][j] = max(ho[i][j], ho[i][j + 1]);
}
}
}
rep(i, w){
ll cnt = 0;
rep(j, h){
if(s[j][i] == '.'){
cnt++;
} else {
cnt = 0;
}
ve[j][i] = cnt;
}
for(ll j = h - 2; j >= 0; j--){
if(s[j][i] == '.'){
ve[j][i] = max(ve[j][i], ve[j + 1][i]);
}
}
}
ll ans = 0;
rep(i, h){
rep(j, w){
if(s[i][j] == '.'){
//cout << i << " " << j << " " << ho[i][j] << " " << ve[i][j] << endl;
ans += modpow(2LL,k) - modpow(2LL, k - (ho[i][j] + ve[i][j] - 1));
ans %= MOD;
}
}
}
while(ans < 0) ans += MOD;
cout << ans % MOD << endl;
} | #include<bits/stdc++.h>
using namespace std;
using ll=long long;
static const ll mod=1000000007;
ll H,W;
char f[2005][2005];
ll modpow(ll x,ll y){
if(y==0)
return 1;
else if(y%2==0){
ll z=modpow(x,y/2);
return (z*z)%mod;
}else{
ll z=modpow(x,y/2);
return (x*((z*z)%mod))%mod;
}
}
ll sum[2005][2005];
int main(){
cin>>H>>W;ll K=0;
for(ll i=0;i<H;i++){
string s;cin>>s;
for(ll j=0;j<W;j++){
f[i][j]=s[j];
if(f[i][j]=='.')K++;
}
}
for(ll i=0;i<H;i++){
ll x=0;
for(ll j=0;j<W;j++){
if(f[i][j]=='#')
x=0;
else{
x++;sum[i][j]+=x;
}
}
} for(ll j=0;j<W;j++){
ll x=0;
for(ll i=H-1;0<=i;i--){
if(f[i][j]=='#')
x=0;
else{
x++;sum[i][j]+=x;
}
}
} for(ll i=0;i<H;i++){
ll x=0;
for(ll j=W-1;0<=j;j--){
if(f[i][j]=='#')
x=0;
else{
x++;sum[i][j]+=x;
}
}
} for(ll j=0;j<W;j++){
ll x=0;
for(ll i=0;i<H;i++){
if(f[i][j]=='#')
x=0;
else{
x++;sum[i][j]+=x;
}
}
}ll ans=0;ll k=modpow(2,K);
for(ll i=0;i<H;i++)
for(ll j=0;j<W;j++)
if(f[i][j]!='#'){
ll y=sum[i][j]-3;
ll x=(k-modpow(2,K-y)+mod)%mod;
ans=(ans+x)%mod;
}cout<<ans<<endl;
return 0;
} |
/*
Auth: StardustSphere
Time: 2021/01/10
Prog: ARC111-C
Lang: cpp
*/
#define F_C
#include <bits/extc++.h>
#define rep(i, l, r) for(int i = l; i < r; i++)
#define hrp(i, l, r) for(int i = l; i <= r; i++)
#define rev(i, r, l) for(int i = r; i >= l; i--)
#define ms(n, t) memset(n, t, sizeof(n))
#define pb emplace_back
#define int ll
#ifndef JOEON
#define D(...) 97
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
template<typename tn = int> inline tn next(void) { tn k; cin>>k; return k; }
const int U = 2e5+50;
int afford[U], weight[U], keep[U], keeper[U];
vector<pii> ans;
vector<int> v;
signed main(void)
{
#ifdef JOEON
// freopen("C:\\Users\\Joeon\\Desktop\\IN.txt", "r", stdin);
// freopen("C:\\Users\\Joeon\\Desktop\\OUT.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int n = next();
rep(i, 0, n) cin>>afford[i];
rep(i, 0, n) cin>>weight[i];
rep(i, 0, n)
{
keep[i] = next()-1;
keeper[keep[i]] = i;
if (afford[i] <= weight[keep[i]] && keep[i] != i) return cout<<-1<<endl, 0;
if (keep[i] != i) v.pb(i);
}
sort(v.begin(), v.end(), [=](const int& a, const int& b)
{
return afford[a] < afford[b];
});
rep(i, 0, v.size()) // person
{
if (v[i] == keep[v[i]]) continue;
int ot = keeper[v[i]];
ans.pb(v[i], ot);
swap(keep[v[i]], keep[ot]);
keeper[keep[i]] = i;
keeper[keep[ot]] = ot;
}
cout<<ans.size()<<endl;
for(auto &[f, s] : ans) cout<<f+1<<' '<<s+1<<endl;
return 0;
} | #include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double db;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define pll pair<ll,ll>
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define ub(v,val) upper_bound(v.begin(),v.end(),val)
#define np(str) next_permutation(str.begin(),str.end())
#define lb(v,val) lower_bound(v.begin(),v.end(),val)
#define sortv(vec) sort(vec.begin(),vec.end())
#define rev(p) reverse(p.begin(),p.end());
#define v vector
#define pi 3.14159265358979323846264338327950288419716939937510
#define len length()
#define repc(i,s,e) for(ll i=s;i<e;i++)
#define fi first
#define se second
#define mset(a,val) memset(a,val,sizeof(a));
#define mt make_tuple
#define repr(i,n) for(i=n-1;i>=0;i--)
#define rep(i,n) for(i=0;i<n;i++)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define at(s,pos) *(s.find_by_order(pos))
#define set_ind(s,val) s.order_of_key(val)
long long int M = 1e9 + 7 ;
long long int inf = 9 * 1e18;
//CLOCK
ll begtime = clock();
#define time() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//CLOCK ENDED
ll n, m;
// modular exponentiation
ll binpow(ll val, ll deg)
{
if (deg < 0)
return 0;
if (!deg)
return 1 % M;
if (deg & 1)
return binpow(val, deg - 1) * val % M;
ll res = binpow(val, deg >> 1);
return (res * res) % M;
}
//binomial
ll modinv(ll n) {
return binpow(n, M - 2);
}
int main() {
// your code goes here
IOS;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll i, j, t, k, x, y, z, N;
cin >> n >> m >> k;
map<ll, ll> fail;
rep(i, k) {
cin >> x;
fail[x] = 1;
}
db dp_sum[n + m], dp_carry[n + m];
rep(i, n + m) {
dp_carry[i] = 0; dp_sum[i] = 0;
}
db sum = 0, car = 0;
for (i = n - 1; i > 0; i--) {
if (fail[i]) {
dp_carry[i] = 1;
dp_sum[i] = 0;
}
else {
dp_carry[i] = car / db(m);
dp_sum[i] = 1.0 + sum / db(m);
}
sum += dp_sum[i];
car += dp_carry[i];
sum -= dp_sum[i + m];
car -= dp_carry[i + m];
}
sum = 1.0 + sum / db(m);
car = car / db(m);
// rep(i, n + m) {
// cout << dp_sum[i] << ' ';
// }
db ans = sum / (1.0 - car);
if (ans <= 0 || (1.0 - car) < 1e-6) cout << -1;
else
cout << fixed << setprecision(5) << ans;
return 0;
}
|
#include <bits/stdc++.h>
/* #include <atcoder/all> */
#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)
using namespace std;
/* using namespace atcoder; */
using ll = long long;
using P = pair<int, int>;
const int MOD = 1'000'000'007;
template<class T>
inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int main() {
int h, w;
cin >> h >> w;
vector<string> s(h);
rep(i, h) {
cin >> s.at(i);
}
vector<vector<ll>> r(h, vector<ll>(w));
vector<vector<ll>> d(h, vector<ll>(w));
vector<vector<ll>> rd(h, vector<ll>(w));
vector<vector<ll>> dp(h, vector<ll>(w, 0));
dp.at(0).at(0) = 1;
rep(i, h) {
rep(j, w) {
if (s.at(i).at(j) == '#') {
r.at(i).at(j) = 0;
d.at(i).at(j) = 0;
rd.at(i).at(j) = 0;
continue;
}
// r
int oi = i - 1;
int oj = j;
if (oi < h && oj < w && oi >= 0 && oj >= 0) {
r.at(i).at(j) += dp.at(oi).at(oj);
r.at(i).at(j) += r.at(oi).at(oj);
}
// d
oi = i;
oj = j - 1;
if (oi < h && oj < w && oi >= 0 && oj >= 0) {
d.at(i).at(j) += dp.at(oi).at(oj);
d.at(i).at(j) += d.at(oi).at(oj);
}
// rd
oi = i - 1;
oj = j - 1;
if (oi < h && oj < w && oi >= 0 && oj >= 0) {
rd.at(i).at(j) += dp.at(oi).at(oj);
rd.at(i).at(j) += rd.at(oi).at(oj);
}
dp.at(i).at(j) += r.at(i).at(j);
dp.at(i).at(j) %= MOD;
dp.at(i).at(j) += d.at(i).at(j);
dp.at(i).at(j) %= MOD;
dp.at(i).at(j) += rd.at(i).at(j);
dp.at(i).at(j) %= MOD;
}
}
cout << dp.at(h - 1).at(w - 1) << endl;
return 0;
}
| #include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mod 1000000007
#define pii pair<ll,ll>
#define inf 1000000000000000000
#define bpc(x) __builtin_popcountll(x)
#define autoit(x,it) for(auto it = x.begin(); it != x.end(); it++)
#define autoitr(x,it) for(auto it = x.rbegin(); it != x.rend(); it++)
#define rep(n) for(ll i = 0; i < n; i++)
#define repi(i,n) for(ll i = 0; i < n; i++)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
int main()
{
FAST/**/
ll n,m;
cin>>n>>m;
if(n == 1)
{
if(m == 0)
{
cout<<1<<" "<<2<<"\n";
return 0;
}
cout<<-1<<"\n";
return 0;
}
if(m<0 || m >= n-1)
{
cout<<-1<<"\n";
return 0;
}
if(m == 0)
{
rep(n)
cout<<(2*i+1)<<" "<<(2*i+2)<<"\n";
return 0;
}
if(m == n-1)
{
for(ll i=1;i<=n-1;i++)
cout<<2*i<<" "<<2*i+1<<"\n";
cout<<1<<" "<<2*n<<"\n";
return 0;
}
ll sta = 2;
for(ll i=0;i<m+1;i++)
{
cout<<sta<<" "<<sta+1<<"\n";
sta+=2;
}
cout<<1<<" "<<sta<<"\n";
sta++;
rep(n-(m+2))
{
cout<<sta<<" "<<sta+1<<"\n";
sta+=2;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repp(i, st, en) for (ll i = (ll)st; i < (ll)(en); i++)
#define repm(i, st, en) for (ll i = (ll)st; i >= (ll)(en); i--)
#define all(v) v.begin(), v.end()
void chmax(ll &x, ll y) {x = max(x,y);}
void chmin(ll &x, ll y) {x = min(x,y);}
void Yes() {cout << "Yes" << endl; exit(0);}
void No() {cout << "No" << endl; exit(0);}
template<class in_Cout> void Cout(in_Cout x) {cout << x << endl; exit(0);}
template<class in_vec_cout>
void vec_cout(vector<in_vec_cout> vec) {
for (in_vec_cout res : vec) {cout << res << " ";}
cout << endl;
}
const ll inf = 1e18;
const ll mod = 1e9 + 7;
ll K, N, M;
vector<ll> A, B;
void init() {
cin >> K >> N >> M;
A.resize(K);
B.resize(K);
rep(i,K) cin >> A[i];
}
/*
|N*B[i]-M*A[i]| <= X; (0 <= X <= N*M)
M*A[i] - X <= N*B[i] <= M*A[i] + X;
ceil((M*A[i]-X)/N) <= B[i] <= floor((M*A[i]+X)/N)
*/
ll Ceil(ll x, ll y) {
if (y==0) return inf;
if (x<=0) return 0;
ll res = x / y;
if (x%y) res++;
return res;
}
ll Floor(ll x, ll y) {
if (y==0) return inf;
return x / y;
}
bool ok(ll X) {
vector<ll> l(K), r(K);
ll L = 0, R = 0;
rep(i,K) {
l[i] = Ceil(M*A[i]-X,N);
r[i] = Floor(M*A[i]+X,N);
L += l[i];
R += r[i];
}
if (M<L || R<M) return false;
ll dif = M - L;
rep(i,K) {
B[i] = l[i];
ll add = min(dif,r[i]-l[i]);
B[i] += add;
dif -= add;
}
return true;
}
void solve() {
init();
ll l = -1, r = N*M + 1;
while (r-l>1) {
ll m = (l+r)/2;
if (ok(m)) r = m;
else l = m;
}
vec_cout(B);
}
int main() {
solve();
} | #include <algorithm>
#include <iomanip>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <numeric>
#include <vector>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define _rep(i, n) _rep2(i, 0, n)
#define _rep2(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(...) GET_MACRO(__VA_ARGS__, _rep2, _rep)(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using i64 = long long;
template<class T, class U>
bool chmin(T& a, const U& b) { return (b < a) ? (a = b, true) : false; }
template<class T, class U>
bool chmax(T& a, const U& b) { return (b > a) ? (a = b, true) : false; }
template<typename T>istream& operator>>(istream&i,vector<T>&v){rep(j,v.size())i>>v[j];return i;}
template<typename T>string join(vector<T>&v){stringstream s;rep(i,v.size())s<<' '<<v[i];return s.str().substr(1);}
template<typename T>ostream& operator<<(ostream&o,vector<T>&v){if(v.size())o<<join(v);return o;}
template<typename T>string join(vector<vector<T>>&vv){string s="\n";rep(i,vv.size())s+=join(vv[i])+"\n";return s;}
template<typename T>ostream& operator<<(ostream&o,vector<vector<T>>&vv){if(vv.size())o<<join(vv);return o;}
template<class T> using pq = priority_queue<T, vector<T>, greater<T>>;
int main()
{
int k, n, m;
cin >> k >> n >> m;
vector<long double> a(k);
vector<int> b(k);
vector<pair<long double, int>> p(k);
cin >> a;
rep(i, k) b[i] = (a[i] * m) / n + 0.001;
int r = m - accumulate(all(b), 0);
rep(i, k)
{
p[i].first = b[i] - (a[i] * m) / n;
p[i].second = i;
}
sort(all(p));
rep(i, r)
{
b[p[i].second]++;
}
cout << b << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll _mergeSort(ll arr[], ll temp[],
ll left, ll right);
ll merge(ll arr[], ll temp[], ll left,
ll mid, ll right);
ll mergeSort(ll arr[], ll array_size)
{
ll temp[array_size];
return _mergeSort(arr, temp, 0, array_size - 1);
}
/* An auxiliary recursive function
that sorts the input array and
returns the number of inversions in the array. */
ll _mergeSort(ll arr[], ll temp[],
ll left, ll right)
{
ll mid, inv_count = 0;
if (right > left) {
/* Divide the array into two parts and
call _mergeSortAndCountInv()
for each of the parts */
mid = (right + left) / 2;
/* Inversion count will be sum of
inversions in left-part, right-part
and number of inversions in merging */
inv_count += _mergeSort(arr, temp,
left, mid);
inv_count += _mergeSort(arr, temp,
mid + 1, right);
/*Merge the two parts*/
inv_count += merge(arr, temp, left,
mid + 1, right);
}
return inv_count;
}
/* This funt merges two sorted arrays
and returns inversion count in the arrays.*/
ll merge(ll arr[], ll temp[], ll left,
ll mid, ll right)
{
ll i, j, k;
ll inv_count = 0;
i = left; /* i is index for left subarray*/
j = mid; /* j is index for right subarray*/
k = left; /* k is index for resultant merged subarray*/
while ((i <= mid - 1) && (j <= right)) {
if (arr[i] <= arr[j]) {
temp[k++] = arr[i++];
}
else {
temp[k++] = arr[j++];
/* this is tricky -- see above
explanation/diagram for merge()*/
inv_count = inv_count + (mid - i);
}
}
/* Copy the remaining elements of left subarray
(if there are any) to temp*/
while (i <= mid - 1)
temp[k++] = arr[i++];
/* Copy the remaining elements of right subarray
(if there are any) to temp*/
while (j <= right)
temp[k++] = arr[j++];
/*Copy back the merged elements to original array*/
for (i = left; i <= right; i++)
arr[i] = temp[i];
return inv_count;
}
int main()
{
ll n;
cin>>n;
ll a[n],b[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
b[i]=a[i];
}
ll ans = mergeSort(b, n);
cout<<ans<<endl;
for(int i=0;i<n-1;i++)
{
cout<<ans-a[i]+(n-a[i]-1)<<endl;
ans=ans-a[i]+(n-a[i]-1);
}
return 0;
}
|
/**
* author: upsolver69
**/
#include<bits/stdc++.h>
#define fast ios_base::sync_with_stdio(0); cin.tie(0);
#define f_in(file) freopen(file, "r", stdin)
#define f_out(file) freopen(file, "w", stdout)
#define sz(a) int((a).size())
#define pp pair
#define fi first
#define vt vector
#define se second
#define vt vector
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define all(c) c.begin(), c.end()
#define tr(i, c) for(auto& i : c)
#define present(c,x) (c.find(x) != c.end()) // Set and Maps
#define cpresent(c,x) (find(allc,x) != c.end()) // Vectors
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int inf = (int) 1e9;
using pd = pair<double, double>;
void testcase() {
int n, m, k;
cin >> n >> m >> k;
vt<bool> a(n + 1, 0);
for (int i = 0; i < k; i++) {
int x;
cin >> x;
a[x] = 1;
}
vector<pd> dp(n + m + 1, mp(0.0, 0.0));
pd sum = {0, 0};
for (int i = n - 1; i >= 0; i--) {
sum.fi += dp[i + 1].fi;
sum.se += dp[i + 1].se;
sum.fi -= dp[i + m + 1].fi;
sum.se -= dp[i + m + 1].se;
if (a[i]) {
dp[i] = mp(0, 1);
} else {
dp[i] = mp(1 + sum.fi / (double) m, sum.se / (double) m);
}
}
double p = dp[0].fi;
double q = 1 - dp[0].se;
if (abs(q) < 1e-10) {
cout << -1;
} else {
cout << fixed << setprecision(10) << p / q;
}
}
int main(void) {
fast
int tt = 1;
// cin >> tt;
while (tt--) {
testcase();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<typename T>
void compress(vector<T>& A) {
vector<T> vals=A;
vals.push_back(0);
sort(vals.begin(),vals.end());
vals.erase(unique(vals.begin(),vals.end()),vals.end());
for(auto& e:A) e=distance(vals.begin(),lower_bound(vals.begin(),vals.end(),e));
}
int main() {
ll N,M; cin>>N>>M;
// 座標圧縮っぽい
vector<ll> X,Y;
ll size;
ll ans=0;
for(ll i=0; i<M; ++i) {
ll x,y; cin>>x>>y;
y=y-N+M;
if(y<0 || y>2*M) continue;
X.push_back(x); Y.push_back(y);
}
compress(X);
size=X.size();
map<ll,set<ll>> pawn;
for(ll i=0; i<size; ++i) pawn[Y[i]].insert(X[i]);
queue<pair<ll,ll>> que;
map<pair<ll,ll>,bool> seen;
que.push({0,M});
seen[{0,M}]=true;
while(!que.empty()) {
auto p=que.front();
que.pop();
ll limit=LLONG_MAX;
auto itr=pawn[p.second].upper_bound(p.first);
if(itr==pawn[p.second].end()) ans++;
else limit=*itr;
// 前
if(p.second-1>=0) {
itr=pawn[p.second-1].upper_bound(p.first);
for(;itr!=pawn[p.second-1].end();++itr) {
if(*itr>limit) break;
if(seen.count({*itr,p.second-1})==0) {
que.push({*itr,p.second-1});
seen[{*itr,p.second-1}]=true;
}
}
}
// 後
if(p.second+1<=2*M) {
itr=pawn[p.second+1].upper_bound(p.first);
for(;itr!=pawn[p.second+1].end();++itr) {
if(*itr>limit) break;
if(seen.count({*itr,p.second+1})==0) {
que.push({*itr,p.second+1});
seen[{*itr,p.second+1}]=true;
}
}
}
}
cout<<ans<<endl;
}
| #include<bits/stdc++.h>
using namespace std;
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {
return '"' + s + '"';
}
string to_string(const char& s) {
string res = "'";
res += s;
res += "'";
return res;
}
string to_string(bool b) {
return (b ? "true" : "false");
}
string to_string(vector<bool> v) {
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (i) res += ", ";
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...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define int long long
#define fi first
#define se second
#define pii pair<int, int>
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(a) (int) (a).size()
#define eb emplace_back
#define ld long double
#define ve vector
#define forn(i, n) for (int i = 0; i < n; i++)
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
ve<pii> a(m);
forn (i, m) cin >> a[i].fi >> a[i].se;
sort(all(a));
a.pb({1e18, 1e18});
set<int> tmp;
tmp.insert(n);
int i = 0;
while (true) {
//debug(tmp);
if (i == m) {
cout << sz(tmp);
return 0;
}
int j = i;
while (a[j].fi == a[i].fi) j++;
ve<int> ban;
for (int v = i; v < j; v++) ban.pb(a[v].se);
ve<int> add;
for (auto x : ban) {
if (tmp.find(x - 1) != tmp.end() || tmp.find(x + 1) != tmp.end()) add.pb(x);
}
//debug(ban, add);
for (auto x : ban) {
if (tmp.find(x) != tmp.end()) tmp.erase(x);
}
for (auto x : add) tmp.insert(x);
i = j;
}
}
|
#include <bits/stdc++.h>
#define Fast cin.tie(0), ios::sync_with_stdio(0)
#define All(x) x.begin(), x.end()
#define louisfghbvc int t; cin >> t; for(int tt = 0; tt < t; ++tt)
#define sz(x) (int)(x).size()
#define sort_unique(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()));
using namespace std;
typedef long long LL;
typedef pair<LL, LL> ii;
typedef vector<LL> vi;
template<typename T> istream& operator>>(istream &is, vector<T> &v) { for(auto &it : v) is >> it; return is; }
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep = ""; for(const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
void dbg_out() { cerr << " end.\n"; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
const int N = 5e2+5;
const int INF = 0x3f3f3f3f;
/**
Read problem statement carefully
**/
int dp[N][N];
LL A[N][N], B[N][N];
int r, c;
void solve(int x){
memset(dp, 0x3f, sizeof dp);
cin >> r >> c;
for(int i = 1; i <= r; ++i)
for(int j= 1; j < c; ++j)
cin >> A[i][j];
for(int i = 1; i < r; ++i)
for(int j = 1; j <= c; ++j)
cin >> B[i][j];
dp[1][1] = 0;
priority_queue<ii, vector<ii>, greater<ii>> pq;
pq.push({0, 0});
while(sz(pq)){
auto [cost, idx] = pq.top(); pq.pop();
int x = idx/c, y = idx%c;
x++, y++;
if(cost > dp[x][y]) continue;
dp[x][y] = cost;
if(y < c){
if(dp[x][y+1] > dp[x][y] + A[x][y]){
dp[x][y+1] = dp[x][y] + A[x][y];
pq.push({dp[x][y+1], (x-1) * c + y});
}
}
if(y > 1){
if(dp[x][y-1] > dp[x][y] + A[x][y-1]){
dp[x][y-1] = dp[x][y] + A[x][y-1];
pq.push({dp[x][y-1], (x-1) * c + y - 2});
}
}
if(x < r){
if(dp[x+1][y] > dp[x][y] + B[x][y]){
dp[x+1][y] = dp[x][y] + B[x][y];
pq.push({dp[x+1][y], x * c + y - 1});
}
}
for(int i = 1; i < r && x - i > 0; ++i){
int ga = 1 + i;
if(dp[x][y] + ga < dp[x-i][y]){
dp[x-i][y] = dp[x][y] + ga;
pq.push({dp[x-i][y], (x - i - 1) * c + y-1});
}
}
}
cout << dp[r][c] << "\n";
}
int main()
{
// Fast;
// louisfghbvc
solve(1);
return 0;
}
/**
Enjoy the problem.
**/
| //#include <atcoder/all>
#include <iostream>
#include <numeric>
#include <cmath>
#include <limits>
#include <stdio.h>
#include <iomanip>
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
using namespace std;
//using namespace atcoder;
using ll = long long;
#define all(A) A.begin(),A.end()
using vll = vector<ll>;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
using Graph = vector<vector<pair<ll, ll>>>;
int main(){
ll R,C;
cin>>R>>C;
Graph G(2*R*C);
rep(r,R){
rep(c,C-1){
ll A;
cin>>A;
G[r*C+c].push_back(make_pair(r*C+c+1,A));
G[r*C+c+1].push_back(make_pair(r*C+c,A));
}
}
rep(r,R-1){
rep(c,C){
ll A;
cin>>A;
G[r*C+c].push_back(make_pair((r+1)*C+c,A));
G[R*C+(r+1)*C+c].push_back(make_pair(R*C+(r)*C+c,1));
}
}
rep(r,R){
rep(c,C){
G[r*C+c].push_back(make_pair(R*C+r*C+c,1));
G[R*C+r*C+c].push_back(make_pair(r*C+c,0));
}
}
priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>> Q;
vll dist(2*R*C,1e17);
dist[0]=0;
Q.push(make_pair(0,0));
while(!Q.empty()){
auto p=Q.top();
Q.pop();
ll v=p.second;
for(auto q:G[v]){
ll nv=q.first;
if(dist[nv]<=dist[v]+q.second)continue;
dist[nv]=dist[v]+q.second;
Q.push(make_pair(dist[nv],nv));
}
}
cout<<dist[R*C-1]<<endl;
} |
/*
Auther: ghoshashis545 Ashis Ghosh 😎
College: Jalpaiguri Govt Enggineering College
*/
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#include<algorithm>
#include<string>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template <class type1>
// using ordered_multiset = tree <type1, null_type, less_equal <type1>, rb_tree_tag, tree_order_statistics_node_update>;
// typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
#define ll int
#define int long long
#define ld long double
#define ff first
#define ss string
#define se second
#define sp(x) cout << fixed << setprecision(x)
#define endl "\n"
#define ub upper_bound
#define lb lower_bound
#define vi vector<int>
#define ipair pair<int,int>
#define vpi vector<ipair>
#define clr(a,x) memset(a,x,sizeof(a))
#define alt(v) v.begin(),v.end()
#define ralt(v) v.rbegin(),v.rend()
#define pb emplace_back
#define mp make_pair
#define cntb(x) __builtin_popcount(x)
#define fab(i,a,b) for(int i=(a);i<(b);i++)
#define fba(i,a,b) for(int i=(b);i>=(a);i--)
bool ispoweroftwo(int n){return n&(!(n&(n-1)));}
int mod=1000000007;
// int mod=998244353;
int dx[] = {1,0,-1,0};
int dy[] = {0,-1,0,1};
bool test = 0;
const int inf = 1e9;
const int N = 2e5+5;
int k,n,q;
vector<int>adj[N],dep(N,0),val(N,0);
void dfs(int u,int p)
{
for(auto it : adj[u]){
if(it == p)
continue;
dep[it] = dep[u] + 1;
dfs(it,u);
}
}
void dfs1(int u,int p)
{
for(auto it : adj[u]){
if(it == p)
continue;
val[it] += val[u];
dfs1(it,u);
}
}
void solve(int tc = 0)
{
cin >> n;
vector<ipair>edge;
for(int i = 1,x,y; i < n; ++i){
cin >> x >> y;
adj[x].pb(y);
adj[y].pb(x);
edge.pb(mp(x,y));
}
dfs(1,0);
int s = 0;
cin >> q;
while(q--)
{
int t,e,v;
cin >> t >>e >>v;
s += v;
int x = edge[e-1].ff;
int y = edge[e-1].se;
if(t == 2)
swap(x,y);
if(dep[x] > dep[y]){
val[1] -= v;
val[x] += v;
}
else{
val[y] -= v;
}
}
dfs1(1,-1);
for(int i = 1; i<=n; ++i)
cout<<(val[i]+s)<<"\n";
}
signed main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t=1;
if(test)
cin>>t;
for(int i = 1; i <= t; ++i){
solve(i);
}
return 0;
} | #include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); i++)
#define sz(x) int(x.size())
using namespace std;
using ll = long long;
using P = pair<int,int>;
using tp = tuple<int,int> ;
#include <bits/stdc++.h>
using namespace std;
void dfs(int v, ll x, int from, const vector<vector<int>>& e, vector<ll>& dp){
dp[v] += x;
for(int next : e[v]){
if(next == from) continue;
dfs(next,dp[v], v, e,dp);
}
return ;
}
void deepDFS(int x, int d, vector<int>& deep, const vector<vector<int>>& e){
deep[x] = d;
for(int u : e[x]){
if(deep[u] >= 0) continue;;
deepDFS(u,d+1,deep,e);
}
}
int main(){
int n; cin >> n;
vector<vector<int>> e(n);
vector<int> A(n);
vector<int> B(n);
rep(i,n-1){
int a,b; cin >> a >> b;
a--; b--;
e[a].push_back(b);
e[b].push_back(a);
A[i] = a; B[i] = b;
}
vector<int> deep(n,-1);
deepDFS(0,0,deep,e);
int q; cin >> q;
vector<ll> dp(n);
rep(i,q){
int t,ei,x; cin >> t >> ei >> x;
ei--;
int a,b;
if(t == 1) { a = A[ei]; b = B[ei]; }
else { a = B[ei]; b = A[ei]; }
if(deep[a] < deep[b]){
dp[0] += x;
dp[b] -= x;
} else {
dp[a] += x;
}
}
dfs(0,0,-1,e,dp);
rep(i,n) cout << dp[i] << 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;
class UnionFind {
public:
vector<int> par, rnk;
void init(int n) {
for (int i = 0; i < n; i++) par.push_back(i);
rnk.resize(n, 0);
}
int find(int x) {
if (par[x] == x) return x;
else return par[x] = find(par[x]);
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y) return;
if (rnk[x] < rnk[y]) {
par[x] = y;
} else {
par[y] = x;
if (rnk[x] == rnk[y]) rnk[x]++;
}
}
bool same(int x, int y) {
return find(x) == find(y);
}
};
int main() {
int n;
cin >> n;
vector<int> a(n);
rep(i, n) cin >> a[i];
UnionFind UF;
UF.init(200005);
int ans = 0;
rep(i, int((n+1)/2)) {
if (i * 2 + 1 != n) {
int x = a[i], y = a[n - i - 1];
if (!UF.same(x, y)) {
UF.unite(x, y);
ans++;
}
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
#define int long long
using namespace std;
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')w=-1;ch=getchar();
}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
inline bool read(int& a){
int s=0,w=1;
char ch=getchar();
if(ch==EOF){
return false;
}
while(ch<'0'||ch>'9'){
if(ch=='-')w=-1;ch=getchar();
}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
a=s*w;
return true;
}
inline void write(int x){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
}
template<typename...args>inline bool read(int &a,args&...x){
return (read(a),read(x...));
}
inline void writeln(int x){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
puts("");
}
const int N=200010;
int n,a[N],ans;
bool vis[N];
vector<int> g[N];
void dfs(int x){
if(!vis[x]) return;
vis[x]=false;
for(auto y:g[x]){
dfs(y);
}
}
signed main(){
read(n);
for(int i=1;i<=n;i++){
read(a[i]);
if(!vis[a[i]]){
vis[a[i]]=true;
ans++;
}
}
int l=1,r=n;
while(l<r){
g[a[l]].push_back(a[r]);
g[a[r]].push_back(a[l]);
l++,r--;
}
for(int i=1;i<=N;i++){
if(vis[i]){
ans--;
dfs(i);
}
}
writeln(ans);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define fo(a,b) for(int64_t a=0;a<b;a++)
#define sor(a) sort(a.begin(),a.end())
#define rev(a) reverse(a.begin(),a.end())
#define ll int64_t
#define mod 1000000007
#define vl vector<int64_t>
int64_t rui(int64_t n,int64_t x){
int64_t i,s,ans;
i=0;
ans=1;
vector<int64_t> vec(100);
while(x>0){
vec.at(i)=x%2;
x=x/2;
i++;
}
s=n;
for(int64_t j=0;j<100;j++){
if(vec.at(j)==1){ans=(ans*s)%998244353;}
s=(s*s)%998244353;
}
return ans;
}
int main() {
ll h,w,k,a,b,p,t,q;
t=998244353;
char c;
cin>>h>>w>>k;
vector<vector<ll>> da(h, vector<ll>(w));
vector<vector<ll>> ans(h+1, vector<ll>(w+1));
fo(i,k){
cin>>a>>b;
a--;
b--;
cin>>c;
if(c=='R'){da.at(a).at(b)=1;}
else if(c=='D'){da.at(a).at(b)=2;}
else if(c=='X'){da.at(a).at(b)=3;}
}
ans.at(0).at(0)=1;
fo(i,h){
fo(j,w){
p=ans.at(i).at(j);
if(da.at(i).at(j)==0){
if(p%3==0){q=p/3*2;}
if(p%3==1){q=(p+t)/3*2;}
if(p%3==2){q=(p+t*2)/3*2;}
ans.at(i+1).at(j)+=q;
ans.at(i).at(j+1)+=q;}
else if(da.at(i).at(j)==1){ans.at(i).at(j+1)+=p;}
else if(da.at(i).at(j)==2){ans.at(i+1).at(j)+=p;}
else if(da.at(i).at(j)==3){ans.at(i+1).at(j)+=p;
ans.at(i).at(j+1)+=p;}
if(ans.at(i+1).at(j)>=t){ans.at(i+1).at(j)=(ans.at(i+1).at(j))%t;}
if(ans.at(i).at(j+1)>=t){ans.at(i).at(j+1)=(ans.at(i).at(j+1))%t;}
}
}
cout<<((ans.at(h-1).at(w-1))*rui(3,(h*w)-k))%t<<endl;
} | //
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
const ll p = 998244353;
const int maxn = 5005+10;
ll fac[maxn];
ll Pow(ll x,ll d){
ll tans = 1;
if(d == 0)return 1%p;
ll a = Pow(x,d/2);
tans = a*a%p;
if(d%2)tans = tans*x%p;
return tans%p;
}
char G[maxn][maxn];
ll f[maxn][maxn];
void add(ll & x,ll y){
x = (x + y)%p;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
ll inv3 = Pow(3,p-2)%p;
int n,m,k;
cin >> n >> m >> k;
for(int i = 1;i <= k;i++){
int x,y;char s;
cin >> x >> y >> s;
G[x][y] = s;
}
f[1][1] = 1;
for(int i = 1;i <= n;i++){
for(int j = 1;j <= m;j++)if(i != 1 or j != 1){
if(i > 1){
char u = G[i-1][j];
if(u == 'X' or u == 'D')add(f[i][j],f[i-1][j]);
if(!u)add(f[i][j],f[i-1][j]*inv3%p*2%p);//2/3
}
if(j > 1){
char u = G[i][j-1];
if(u == 'X' or u == 'R')add(f[i][j],f[i][j-1]);
if(!u)add(f[i][j],f[i][j-1]*inv3%p*2%p);
}
}
}
cout << f[n][m]*Pow(3,n*m-k)%p;
return 0;
}
|
#include <bits/stdc++.h>
const long long INF = 1e9;
const long long MOD = 1e9 + 7;
//const long long MOD = 998244353;
const long long LINF = 1e18;
using namespace std;
#define YES(n) cout << ((n) ? "YES" : "NO" ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No" ) << endl
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE" : "IMPOSSIBLE" ) << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible" ) << endl
#define dump(x) cout << #x << " = " << (x) << endl
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) for(int i=0;i<(n);++i)
#define REPR(i,n) for(int i=n;i>=0;i--)
#define COUT(x) cout<<(x)<<endl
#define SCOUT(x) cout<<(x)<<" "
#define VECCOUT(x) for(auto&youso_: (x) )cout<<right<<setw(10)<<youso_<<" ";cout<<endl
#define ENDL cout<<endl
#define CIN(...) int __VA_ARGS__;CINT(__VA_ARGS__)
#define LCIN(...) long long __VA_ARGS__;CINT(__VA_ARGS__)
#define SCIN(...) string __VA_ARGS__;CINT(__VA_ARGS__)
#define VECCIN(x) for(auto&youso_: (x) )cin>>youso_
#define mp make_pair
#define PQ priority_queue<long long>
#define PQG priority_queue<long long,V,greater<long long>>
typedef long long ll;
typedef vector<long long> vl;
typedef vector<long long> vi;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<vl> vvl;
typedef vector<vi> vvi;
typedef vector<vb> vvb;
typedef vector<vc> vvc;
typedef pair<long long, long long> pll;
#define COUT(x) cout<<(x)<<endl
void CINT(){}
template <class Head,class... Tail>
void CINT(Head&& head,Tail&&... tail){
cin>>head;
CINT(move(tail)...);
}
template<class T>
void mod(T &x) {
x %= MOD;
x += MOD;
x %= MOD;
}
ll GCD(ll a, ll b) {
if(b == 0) return a;
else return GCD(b, a%b);
}
struct COMB{
vl fact, fact_inv, inv;
void init_nCk(long long SIZE) {
fact.resize(SIZE + 5);
fact_inv.resize(SIZE + 5);
inv.resize(SIZE + 5);
fact.at(0) = fact.at(1) = fact_inv.at(0) = fact_inv.at(1) = inv.at(1) = 1;
for(long long i = 2; i < SIZE + 5; i++) {
fact.at(i) = fact.at(i - 1)*i%MOD;
inv.at(i) = MOD - inv.at(MOD%i)*(MOD/i)%MOD;
fact_inv.at(i) = fact_inv.at(i - 1)*inv.at(i)%MOD;
}
}
long long nCk (long long n, long long k) {
assert(!(n < k));
assert(!(n < 0 || k < 0));
return fact.at(n)*(fact_inv.at(k)*fact_inv.at(n - k)%MOD)%MOD;
}
};
ll extGCD(ll a, ll b, ll &x, ll &y) {
if(b == 0) {
x = 1;
y = 0;
return a;
}
ll d = extGCD(b, a%b, y, x);
y -= a/b*x;
return d;
}
void Main() {
LCIN(N);
ll n = (1 << N);
vector<pair<ll, ll> > A(n/2), B(n/2);
for(int i = 0; i < n/2; i++) {
cin >> A.at(i).first;
A.at(i).second = i + 1;
}
for(int i = 0; i < n/2; i++) {
cin >> B.at(i).first;
B.at(i).second = i + 1 + n/2;
}
sort(A.rbegin(),A.rend());
sort(B.rbegin(),B.rend());
if(A.at(0).first > B.at(0).first) cout << B.at(0).second << endl;
else cout << A.at(0).second << endl;
}
int main() {
cout << fixed << setprecision(15);
Main();
return 0;
} | //Har Har Mahadev
using namespace std;
#include <bits/stdc++.h>
#define booga cout << "booga" << endl
#define ll long long int
#define pb push_back
#define mp make_pair
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_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 ? "true" : "false");
}
string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template <size_t N>
string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++) {
res += static_cast<char>('0' + v[i]);
}
return res;
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
void testcase(){
int n;
cin >> n;
n = 1 << n;
vector<int> a(n),org(n);
for(int i = 0; i < n; i++){
cin >> a[i];
org[i] = a[i];
}
vector<int> b;
bool alt = true;
int ans = -1;
while(true){
n = max(a.size(),b.size());
if(n == 2){
ans = (a.size() == 2 ? min(a[0],a[1]) : min(b[0],b[1]));
break;
}
if(alt){
for(int i = 0; i < n; i+= 2){
b.pb(max(a[i],a[i+1]));
}
a.clear();
}
else{
for(int i = 0; i < n; i+= 2){
a.pb(max(b[i],b[i+1]));
}
b.clear();
}
//debug((alt ? b : a));
alt = !alt;
}
for(int i = 0; i < (int)org.size(); i++){
if(org[i] == ans){
cout << i+1;
break;
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t = 1;
while(t--){
testcase();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define MOD 1000000007
int main() {
int a, b; cin >> a >> b;
cout << (a - b) / (double)a * 100 << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main()
{
double A, B;
cin >> A >> B;
double ans = B / A;
ans = 1.0 - ans;
ans *= 100.0;
cout << fixed << setprecision(20) << ans << endl;
return 0;
}
|
#include<stdio.h>
#include<bits/stdc++.h>
#define ll long long int
#define inf 1000000000000
#define mod 1000000007
#define all(v) v.begin(),v.end()
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ps(x,y) fixed<<setprecision(y)<<x
using namespace std;
int check, cnt, n;
ll k;
ll mat[9][9];
void ways(int bits, ll curr, int last) {
if (bits == check) {
ll temp = curr + mat[last][0];
if (temp == k) {
cnt++;
// cout << "prices ->" << temp << endl;
}
return;
}
for (int i = 0; i < n; i++)
{
if (!((1 << i)&bits)) {
int temp = (1 << i) | bits;
ll mv = curr + mat[last][i];
ways(temp, mv, i);
}
}
}
void solve()
{
cin >> n >> k;
check = (1 << n) - 1;
cnt = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> mat[i][j];
}
}
ways(1, 0LL, 0);
cout << cnt << endl;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t = 1;
// scanf("%d",&t);
while (t--) {
solve();
}
} | #include<bits/stdc++.h>
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,abm,mmx,avx,avx2")
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define req(i,a,b) for(int i=(a);i>=(b);--i)
#define rep_(i,a,b) for(int i=(a);i<(b).size();++i)
#define F(a) rep(a,1,n)
#define M(a,b) memset(a,b,sizeof a)
#define DC int T;cin>>T;while(T--)
#define ll long long
#define Z(a,b) sort(a+1,a+b+1)
using namespace std;
const unsigned _mod=998244353;
const unsigned mod=1e9+7;
const ll infi=0x3f3f3f3f3f3f3fll;
const int inf=0x3f3f3f3f;
void rd(auto &x){x=0;int f=1;char ch=getchar();while(ch<48||ch>57){if(ch==45)f=-1;ch=getchar();}while(ch>=48&&ch<=57)x=x*10+ch-48,ch=getchar();x*=f;}
ll ksm(ll x,ll y=mod-2,ll m=mod){ll ret=1;while(y){if(y&1)ret=ret*x%m;y>>=1ll;x=x*x%m;}return ret;}
ll qpow(ll x,ll y){ll ret=1;while(y){if(y&1ll)ret=ret*x;y>>=1ll;x=x*x;}return ret;}
/*
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
*/
int h,w,fa[2010],s,t;
char c;
unordered_map<int,bool> mp;
inline int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
inline void merge(int x,int y){fa[find(x)]=find(y);}
int main()
{
cin>>h>>w;
rep(i,1,h+w) fa[i]=i;
merge(1,h+1),merge(1,h+w),merge(h,h+1),merge(h,h+w);
rep(i,1,h) rep(j,1,w) cin>>c,c=='#'&&(merge(i,h+j),1);
rep(i,1,h) s+=!mp[find(i)],mp[find(i)]=1;
mp.clear();
rep(i,h+1,h+w) t+=!mp[find(i)],mp[find(i)]=1;
cout<<min(s,t)-1<<'\n';
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = a; i <= b; i++)
typedef long long ll;
ll h, w, n, m, a[500010], b[500010], c[100010], d[100010];
ll ans;
int mp[2000][2000];
void run(int T) {
cin >> h >> w >> n >> m;
rep(i, 1, n) {
cin >> a[i] >> b[i];
mp[a[i]][b[i]] = 3;
ans++;
}
rep(i, 1, m) {
cin >> c[i] >> d[i];
mp[c[i]][d[i]] = 2;
}
rep(i, 1, n) {
for(int j = a[i] - 1; j && mp[j][b[i]] < 2; j--) {
if(!mp[j][b[i]]) ans++;
mp[j][b[i]] = 1;
}
for(int j = a[i] + 1; j <= h && mp[j][b[i]] < 2; j++) {
if(!mp[j][b[i]]) ans++;
mp[j][b[i]] = 1;
}
for(int j = b[i] - 1; j && mp[a[i]][j] < 2; j--) {
if(!mp[a[i]][j]) ans++;
mp[a[i]][j] = 1;
}
for(int j = b[i] + 1; j <= w && mp[a[i]][j] < 2; j++) {
if(!mp[a[i]][j]) ans++;
mp[a[i]][j] = 1;
}
}
cout << ans << endl;
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
int T = 1;
//cin >> T;
rep(i, 1, T) run(i);
} | #include <bits/stdc++.h>
using namespace std;
const int N = 4e5 + 5;
const int M = 1500 + 10;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
char s[M][M];
int h, w, n, m, mark[M][M];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> h >> w >> n >> m;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
s[i][j] = '.';
}
}
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
x--, y--;
s[x][y] = 'L';
mark[x][y] = 1;
}
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
x--, y--;
s[x][y] = 'X';
}
for (int i = 0; i < h; i++) {
char now = s[i][0];
for (int j = 1; j < w; j++) {
if (s[i][j] == 'X') {
now = s[i][j];
}
if (s[i][j] == 'L') {
now = s[i][j];
}
if (now == 'L') {
mark[i][j]++;
}
}
now = s[i][w - 1];
for (int j = w - 2; j >= 0; j--) {
if (s[i][j] == 'X') {
now = s[i][j];
}
if (s[i][j] == 'L') {
now = s[i][j];
}
if (now == 'L') {
mark[i][j]++;
}
}
}
for (int i = 0; i < w; i++) {
char now = s[0][i];
for (int j = 1; j < h; j++) {
if (s[j][i] == 'X') {
now = s[j][i];
}
if (s[j][i] == 'L') {
now = s[j][i];
}
if (now == 'L') {
mark[j][i]++;
}
}
now = s[h - 1][i];
for (int j = h - 2; j >= 0; j--) {
if (s[j][i] == 'X') {
now = s[j][i];
}
if (s[j][i] == 'L') {
now = s[j][i];
}
if (now == 'L') {
mark[j][i]++;
}
}
}
int64_t ans = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
ans += (mark[i][j] > 0);
}
}
cout << ans << '\n';
return 0;
}
|
#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<set>
#include<map>
#include<utility>
#include<queue>
#include<vector>
#include<stack>
#include<sstream>
#include<algorithm>
#define forn(i,a,b)for(int i=(a),_b=(b);i<=_b;i++)
#define fore(i,b,a)for(int i=(b),_a=(a);i>=_a;i--)
#define rep(i,n)for(int i=0,_n=n;i<n;i++)
#define ll long long
#define pii pair<int,int>
#define vi vector<int>
#define vpii vector<pii>
#define p_q priority_queue
#define m_p make_pair
#define pb push_back
#define ld long double
#define F first
#define S second
#define ull unsigned long long
#define mod 1000000007
#define md 998244353
#define x1 XZVJDFADSPFOE
#define y1 GASDIJSLDAEJF
#define x2 DFDAJKVOHKWIW
#define y2 PSFSAODSXVNMQ
#define LLINF 0x3f3f3f3f3f3f3f3fLL
#define foreach(i,c) for(__typeof(c.begin())i=(c.begin());i!=(c).end();i++)
using namespace std;
inline void read(int &x)
{
short negative=1;
x=0;
char c=getchar();
while(c<'0' || c>'9')
{
if(c=='-')
negative=-1;
c=getchar();
}
while(c>='0' && c<='9')
x=(x<<3)+(x<<1)+(c^48),c=getchar();
x*=negative;
}
ll qpow(ll n,ll k){
ll ans=1;
while(k){
if(k%2){
ans*=n;
ans%=mod;
}
n*=n;
n%=mod;
k/=2;
}
return ans;
}
string itos(int n)
{
string s="";
while(n)
{
int now=n%10;
s+=now+'0';
n/=10;
}
reverse(s.begin(),s.end());
return s;
}
int stoi(string s)
{
int n=0;
rep(i,s.size())
{
n*=10;
n+=s[i]-'0';
}
return n;
}
string s;
bool ok(string s)
{
rep(i,s.size()/2)if(s[i]!=s[s.size()-i-1])return 0;
return 1;
}
bool f=0;
int main()
{
ios::sync_with_stdio(0);
cin>>s;
fore(i,s.size()-1,0)
{
if(s[i]!='0')
{
s=s.substr(0,i+1);
f=1;
break;
}
}
if(ok(s)||f==0)cout<<"Yes\n";
else cout<<"No\n";
return 0;
}
//特判判全
//循环边界
//精度足够
//注意初值
| #include<bits/stdc++.h>
using namespace std;
/*#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
typedef tree<long long,null_type,less<long long>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
s.find_by_order(x) xth element in set
x.order_of_key(x) number of elements <x*/
#define ll long long
#define vi vector<int>
#define si set<int>
#define mii map<int,int>
#define pb push_back
#define pf push_front
#define PI acos(-1)
#define pii pair<int,int>
#define extract_word(s) stringstream str(s); while(str>>word)
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define SET(s) cout<<fixed<<setprecision(s)
#define set0(a) memset(a,0,sizeof(a))
#define endl "\n"
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define lower_string(s) transform(all(s),s.begin(),::tolower)
#define upper_string(s) transform(all(s),s.begin(),::toupper)
#define len(s) (int)s.size()
#define F first
#define S second
template<typename T,typename U>
bool compare(T x,U y){return(abs(x-y)<=1e-9);}
const int MOD=1e9+7;
void solve()
{
int n;
cin>>n;
cout<<n-1;
}
int main()
{
//code
fastio
int t=1;
//cin>>t;
while(t--)
{
solve();
cout<<endl;
}
}
|
#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
using ll = long long int;
const int INF = (1<<30);
const ll INFLL = (1ll<<60);
const ll MOD = (ll)(1e9+7);
#define l_ength size
void mul_mod(ll& a, ll b){
a *= b;
a %= MOD;
}
void add_mod(ll& a, ll b){
a = (a<MOD)?a:(a-MOD);
b = (b<MOD)?b:(b-MOD);
a += b;
a = (a<MOD)?a:(a-MOD);
}
std::set<int> s;
int main(void){
int n,i,p;
for(i=0; i<200002; ++i){
s.insert(i);
}
std::cin >> n;
for(i=0; i<n; ++i){
std::cin >> p;
s.erase(p);
std::cout << *s.begin() << std::endl;
}
return 0;
}
| #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
#define ll long long
const ll mod=1000000007;
inline int roundup(int a, int b) { return (((a + (b -1)) / b)); }
inline ll powmod(ll x, ll y) {ll res=1; for(ll i=0;i<y;i++){ res=res*x%mod; } return res;}
using namespace std;
signed main(void) {
cout << fixed << setprecision(15);
ll n, x[100000], m=0, c=0, u_sq=0;
cin >> n;
rep(i, n) {
cin >> x[i];
}
rep(i, n) {
c = max(c, abs(x[i]));
m += abs(x[i]);
u_sq += abs(x[i])*abs(x[i]);
}
cout << m << '\n' << sqrt(u_sq) <<'\n' << c << '\n';
return 0;
} |
#include <cstdio>
#include <cctype>
using namespace std;
const int max_n = 200000;
int a[max_n], wa[max_n], ps[max_n], wa_cnt = 0;
bool us[max_n] = {};
#define gc getchar
inline int read()
{
int c = gc(), t = 1, n = 0;
while (isspace(c)) { c = gc(); }
if (c == '-') { t = -1, c = gc(); }
while (isdigit(c)) { n = n * 10 + c - '0', c = gc(); }
return n * t;
}
#undef gc
int main()
{
int n;
n = read();
for (int i = 0; i < n; i++)
{
a[i] = read() - 1;
ps[a[i]] = i;
}
for (int i = 0; i < n; i++)
{
if (ps[i] == i)
continue;
for (int j = ps[i] - 1; j >= i; j--)
{
ps[a[j]]++, a[j+1] = a[j];
wa[wa_cnt++] = j + 1;
if (us[j] || wa_cnt >= n)
{
puts("-1");
return 0;
}
us[j] = true;
}
a[i] = ps[i] = i;
/*
for (int i = 0; i < n; i++)
fprintf(stderr, "%d ", a[i]);
putc('\n', stderr);
*/
}
if (wa_cnt != n - 1)
{
puts("-1");
// fprintf(stderr, "%d\n", wa_cnt);
return 0;
}
for (int i = 0; i < wa_cnt; i++)
printf("%d\n", wa[i]);
return 0;
} | #include<bits/stdc++.h>
#define int long long
using namespace std;
int N,P[500];
vector<int> ans;
void f(int a){
ans.push_back(a);
swap(P[a],P[a+1]);
}
signed main(){
int T; cin>>T;
while(T--){
cin>>N;
for(int i=0;i<N;i++) cin>>P[i],P[i]--;
ans.clear();
for(int i=0;i<N-2;i++){
int d=i;
for(;P[d]!=i;d++);
if(d==i)continue;
if(d%2==ans.size()%2){
if(d-i>=2){
if(i%2==ans.size()%2) f(i);
else f(i+1);
}
else if(N-d>2){
f(d);
f(d+1);
f(d);
d+=2;
}
else{
f(d);
f(d-1);
d++;
}
}
for(int j=d-1;j>=i;j--) f(j);
}
if(P[N-2]!=N-2){
if(ans.size()%2==N%2)f(N-2);
else{
f(N-3);
f(N-2);
f(N-3);
f(N-2);
f(N-3);
}
}
cout<<ans.size()<<endl;
for(int i=0;i<ans.size();i++){
cout<<ans[i]+1;
if(i+1!=ans.size())cout<<' ';
}
cout<<endl;
assert(ans.size()<=N*N);
}
} |
#define ll long long
#include<bits/stdc++.h>
using namespace std;
const ll N = 805;
const ll range = 1e9;
ll a[N][N];
ll s[N][N],b[N][N];
int main()
{
ll n,k,l=0,mid,r=range,i,j;
cin>>n>>k;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)cin>>a[i][j];
}
ll lim = ((k*k)/2) + 1;
while(l<=r)
{
mid = (l+r)/2;
for(i=0;i<n;i++)
{
s[i][0] = 0;
s[0][i] = 0;
for(j=0;j<n;j++)
{
if(a[i][j]>mid)b[i][j] = 1;
else
b[i][j] = 0;
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
s[i][j] = s[i-1][j] + s[i][j-1] + b[i-1][j-1] - s[i-1][j-1];
}
}
bool flag = false;
for(i=1;i<=n-k+1;i++)
{
for(j=1;j<=n-k+1;j++)
{
if(s[i+k-1][j+k-1]-s[i+k-1][j-1]-s[i-1][j+k-1]+s[i-1][j-1]<=lim-1)flag = true;
}
}
if(l==r)break;
else{
if(flag)r = mid;
else
l = mid+1;
}
}
cout<<mid<<endl;
}
| #include <iostream>
#include <cstring>
#include <cstdio>
#include <map>
#define LL long long
#define Maxn 200005
using namespace std;
LL read() {
LL x = 0, f = 1;
char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while('0' <= c && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
int n, q, fa[Maxn], siz[Maxn];
map <int, int> S[Maxn];
#define IT map <int, int> ::iterator
int find(int x) {
if(fa[x] == x) return x;
else return find(fa[x]);
}
int main() {
n = read(); q = read();
int c;
for(int i = 1; i <= n; ++i) {
c = read(); fa[i] = i; siz[i] = 1;
S[i][c] = 1;
}
int opt, x, y;
for(int i = 1; i <= q; ++i) {
opt = read(); x = read(); y = read();
if(opt == 1) {
int anx = find(x), any = find(y);
if(anx == any) continue;
if(siz[anx] < siz[any]) swap(anx, any);
fa[any] = anx;
siz[anx] += siz[any];
for(IT it = S[any].begin(); it != S[any].end(); ++it) S[anx][(*it).first] += (*it).second;
}
if(opt == 2) {
int anx = find(x);
printf("%d\n", S[anx][y]);
}
}
return 0;
} |
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <queue>
#include <map>
#include <set>
using namespace std;
using ll = long long int;
using ld = long double;
using P = pair<int, int>;
vector<vector<int>> dp;
vector<vector<bool>> visited;
vector<string> a;
int h, w;
int f(int i, int j){
if(visited[i][j]) return dp[i][j];
else{
visited[i][j] = true;
if((i+j)%2 == 1){
dp[i][j] = 1001001001;
if(i+1 < h) dp[i][j] = min(dp[i][j], f(i+1, j));
if(j+1 < w) dp[i][j] = min(dp[i][j], f(i, j+1));
dp[i][j] += (a[i][j] == '+') ? 1: -1;
}else{
dp[i][j] = -1001001001;
if(i+1 < h) dp[i][j] = max(dp[i][j], f(i+1, j));
if(j+1 < w) dp[i][j] = max(dp[i][j], f(i, j+1));
if(i != 0 || j != 0) dp[i][j] += (a[i][j] == '-') ? 1: -1;
}
}
return dp[i][j];
}
int main(){
cin >> h >> w;
a = vector<string>(h);
for(auto &p: a) cin >> p;
dp = vector<vector<int>>(h, vector<int>(w, -1));
visited = vector<vector<bool>>(h, vector<bool>(w, false));
visited[h-1][w-1] = true;
dp[h-1][w-1] = (a[h-1][w-1] == '+') ? 1: -1;
if((h+w)%2 == 0) dp[h-1][w-1] *= -1;
if(h == 1 && w == 1) dp[h-1][w-1] = 0;
if(f(0, 0) > 0) cout << "Takahashi" << endl;
else if(f(0, 0) < 0) cout << "Aoki" << endl;
else cout << "Draw" << endl;
return 0;
} | //pls work
#include <bits/stdc++.h>
#include <cmath>
#include <cctype>
#include <cstring>
#define fori(i, j, k, in) for (int i = j; i < k; i += in)
#define rfori(i, j, k, in) for (int i = j - 1; i >= k; i -= in)
#define rep(i, j) fori(i, 0, j, 1)
#define rrep(i, j) rfori(i, j, 0, 1)
#define sq(a) (a) * (a)
#define IAMSPEED std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define acci(n) ll n; cin>>n;
#define accia(n, a) ll n; cin>>n; ll a[n]; for(int i=0; i<n; i++) cin>>a[i];
#define pb push_back
#define mp make_pair
using namespace std;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef long long ll;
const int N = 5e+5, m = 1000000007;
//USE ll instead of int
//Don't use rep for dp!!
int main()
{
IAMSPEED
int T = 1, cse = 0;
// cin >> T;
while (T--)
{
//cse++;
//cout<<"Case #"<<cse<<": ";
int h, w;
cin>>h>>w;
char c;
int a[h+1][w+1];
int dp[h+1][w+1];
memset(a,0,sizeof(a));
memset(dp,0,sizeof(dp));
rep(i, h)
{
rep(j, w)
{
cin>>c;
if(c=='+')
a[i][j] = 1;
else
a[i][j] = -1;
}
}
rrep(i, h)
{
rrep(j, w)
{
if(i==h-1&&j==w-1)
continue;
else if((i+j)%2==0)
{
if (i==h-1)
{
dp[i][j] = dp[i][j+1]+a[i][j+1];
}
else if (j==w-1)
{
dp[i][j] = dp[i+1][j]+a[i+1][j];
}
else
{
dp[i][j] = max(dp[i+1][j]+a[i+1][j], dp[i][j+1]+a[i][j+1]);
}
}
else
{
if (i==h-1)
{
dp[i][j] = dp[i][j+1]-a[i][j+1];
}
else if (j==w-1)
{
dp[i][j] = dp[i+1][j]-a[i+1][j];
}
else
{
dp[i][j] = min(dp[i+1][j]-a[i+1][j], dp[i][j+1]-a[i][j+1]);
}
}
}
}
if(dp[0][0]==0)
cout<<"Draw";
else if(dp[0][0]>0)
cout<<"Takahashi";
else cout<<"Aoki";
}
return 0;
} |
#include<bits/stdc++.h>
clock_t __t=clock();
namespace my_std{
using namespace std;
#define pii pair<int,int>
#define fir first
#define sec second
#define MP make_pair
#define rep(i,x,y) for (int i=(x);i<=(y);i++)
#define drep(i,x,y) for (int i=(x);i>=(y);i--)
#define go(x) for (int i=head[x];i;i=edge[i].nxt)
#define templ template<typename T>
#define sz 111111
typedef long long ll;
typedef double db;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
templ inline T rnd(T l,T r) {return uniform_int_distribution<T>(l,r)(rng);}
templ inline bool chkmax(T &x,T y){return x<y?x=y,1:0;}
templ inline bool chkmin(T &x,T y){return x>y?x=y,1:0;}
templ inline void read(T& t)
{
t=0;char f=0,ch=getchar();double d=0.1;
while(ch>'9'||ch<'0') f|=(ch=='-'),ch=getchar();
while(ch<='9'&&ch>='0') t=t*10+ch-48,ch=getchar();
if(ch=='.'){ch=getchar();while(ch<='9'&&ch>='0') t+=d*(ch^48),d*=0.1,ch=getchar();}
t=(f?-t:t);
}
template<typename T,typename... Args>inline void read(T& t,Args&... args){read(t); read(args...);}
char __sr[1<<21],__z[20];int __C=-1,__zz=0;
inline void Ot(){fwrite(__sr,1,__C+1,stdout),__C=-1;}
inline void print(int x)
{
if(__C>1<<20)Ot();if(x<0)__sr[++__C]='-',x=-x;
while(__z[++__zz]=x%10+48,x/=10);
while(__sr[++__C]=__z[__zz],--__zz);__sr[++__C]='\n';
}
void file()
{
#ifdef NTFOrz
freopen("a.in","r",stdin);
#endif
}
inline void chktime()
{
#ifdef NTFOrz
cout<<(clock()-__t)/1000.0<<'\n';
#endif
}
#ifdef mod
ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x%mod) if (y&1) ret=ret*x%mod;return ret;}
ll inv(ll x){return ksm(x,mod-2);}
#else
ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x) if (y&1) ret=ret*x;return ret;}
#endif
// ll mul(ll x,ll y){ull s=1.0*x/mod*y;ll res=1ull*x*y-s*mod;return (res%mod+mod)%mod;}
}
using namespace my_std;
int n,L;
ll a[sz];
ll b[sz];
map<ll,int>mn,mx;
int main()
{
file();
#define GG return puts("-1"),0
read(n,L);
rep(i,1,n) read(a[i]);
rep(i,1,n) read(b[i]);
a[0]=b[0]=0,a[n+1]=b[n+1]=L+1;
rep(i,0,n+1) if (!mn.count(a[i]-i)) mn[a[i]-i]=mx[a[i]-i]=i; else mx[a[i]-i]=i;
ll ans=0;
for (int l=0,r;l<=n+1;l=r+1)
{
if (b[l]<=a[l]) { r=l; continue; }
if (!mn.count(b[l]-l)) GG;
for (r=l;r<=n&&b[r+1]==b[r]+1;++r);
ans+=mn[b[l]-l]-l;
}
for (int r=n+1,l;r>0;r=l-1)
{
if (b[r]>=a[r]) { l=r; continue; }
if (!mx.count(b[r]-r)) GG;
for (l=r;l&&b[l-1]==b[l]-1;--l);
ans+=r-mx[b[r]-r];
}
cout<<ans;
return 0;
} | #include<iostream>
#include<list>
#include<set>
#include<map>
#include<vector>
using namespace std;
typedef long long li;
#define rep(i,n) for(int i=0;i<(n);i++)
#define df 0
template<class T> void print(const T& t){ cout << t << "\n"; }
template<class T, class... Ts> void print(const T& t, const Ts&... ts) { cout << t; if (sizeof...(ts)) cout << " "; print(ts...); }
// Container コンテナ型, map以外
template< template<class ...> class Ctn,class T>
std::ostream& operator<<(std::ostream& os,const Ctn<T>& v){
auto itr=v.begin();
while(itr!=v.end()){
if(itr!=v.begin())cout << " ";
cout << *(itr++);
}
return os;
}
// pair 型
template<class S,class T>
std::ostream& operator<<(std::ostream& os, const pair<T,S>& p){
cout << "(" << p.first << "," << p.second << ")";
return os;
}
pair<int,int> tpt(vector<vector<pair<int,int>>>& tp,pair<int,int> p,
vector<string>& mp){
char c=mp[p.first][p.second];
if(not ('a'<= c and c<= 'z')) return {-1,-1};
int a=c-'a';
if(tp[a][0]==p) return tp[a][1];
return tp[a][0];
}
int q(vector<vector<pair<int,int>>>& tp,pair<int,int> p,
vector<string>& mp){
char c=mp[p.first][p.second];
if(not ('a'<= c and c<= 'z')) return 0;
if(tp[c-'a'].size()<=1) return 0;
return 1;
}
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
int main(){
int h,w; cin >>h >>w;
vector<string> mp(h); rep(i,h) cin >>mp[i];
vector<vector<pair<int,int>>> tp(26);
list<pair<int,int>> que;
vector<vector<int>> te(h,vector<int>(w,-1));
rep(i,h){
rep(j,w){
if('a'<= mp[i][j] and mp[i][j]<='z'){
tp[mp[i][j]-'a'].push_back({i,j});
}
if(mp[i][j]=='S'){
que.push_back({i,j});
te[i][j]=0;
}
}
}
auto illegal
=[&](pair<int,int> p){
int f=p.first,s=p.second;
if(f<0 or s<0 or f>=h or s>=w) return 1;
if(mp[f][s]=='#') return 1;
if(te[f][s]>=0) return 1;
return 0;
};
while(que.size()){
auto p=*que.begin();
que.pop_front();
int f=p.first,s=p.second;
int cnt=te[f][s];
if(mp[f][s]=='G'){
print(te[f][s]);
return 0;
}
auto t=tpt(tp,p,mp);
if(q(tp,p,mp)){
int a=mp[f][s]-'a';
for(auto q:tp[a]){
if(p==q) continue;
if(te[q.first][q.second]>=0) continue;
if(df)print(q);
que.push_back(q);
te[q.first][q.second]=cnt+1;
}
tp[a].clear();
}
rep(i,4){
pair<int,int> t={f+dx[i],s+dy[i]};
if(illegal(t)) continue;
if(df)print(t);
que.push_back(t);
te[t.first][t.second]=cnt+1;
}
if(df)print("que",que);
}
print(-1);
}
|
#include<bits/stdc++.h>
using namespace std;
inline long long read()
{
long long sum=0,naga=1;char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-')naga=-1;ch=getchar();}
while(ch<='9'&&ch>='0')sum=sum*10+ch-'0',ch=getchar();
return sum*naga;
}
inline void write(long long X)
{
if(X<0)putchar('-'),X=-X;
if(X>9)write(X/10);
putchar(X%10+'0');
}
typedef pair<int,int>ss;
priority_queue <ss,vector<ss>,greater<ss> >q;
const long long N=1e6+5,M=5e6+5,INF=1e15;
long long head[N],n,m,s,cnt,dis[N],a,b;
bool vis[N];
struct point
{
int u,v,w;
}edge[M];
inline void dijkstra()
{
while(!q.empty())
{
ss mmp=q.top();
int u=mmp.second;vis[u]=0,q.pop();
for(register int i=head[u];i!=0;i=edge[i].u)
{
int V=edge[i].v;
if(dis[V]>dis[u]+edge[i].w)
{
dis[V]=dis[u]+edge[i].w;
if(vis[V]==0)vis[V]=1,q.push(ss{dis[V],V});
}
}
}
}
inline void addedge(int u,int v,int w)
{
edge[++cnt].u=head[u],edge[cnt].v=v,edge[cnt].w=w,head[u]=cnt;
// edge[++cnt].u=head[u+n],edge[cnt].v=v,edge[cnt].w=w,head[u+n]=cnt;
}
int ww,id[1009][1009];
int main()
{
a=read(),b=read();n=a*b;
for(register int i=1;i<=a;i++)
for(register int j=1;j<=b;j++)
id[i][j]=(i-1)*b+j;
for(register int i=1;i<=a;i++)
for(register int j=1;j<b;j++)
{
ww=read();
addedge(id[i][j],id[i][j+1],ww);
addedge(id[i][j]+n,id[i][j+1],ww);
addedge(id[i][j+1],id[i][j],ww);
addedge(id[i][j+1]+n,id[i][j],ww);
}
for(register int i=1;i<a;i++)
for(register int j=1;j<=b;j++)
{
ww=read();
addedge(id[i][j],id[i+1][j],ww);
addedge(id[i][j]+n,id[i+1][j],ww);
}
for(register int i=1;i<=a;i++)
for(register int j=1;j<=b;j++)
if(i!=1)addedge(id[i][j],id[i-1][j]+n,2);
for(register int i=1;i<=a;i++)
for(register int j=1;j<=b;j++)
if(i!=1)
{
addedge(id[i][j]+n,id[i-1][j]+n,1);
addedge(id[i][j]+n,id[i-1][j],1);
}
s=1;
for(register int i=1;i<=n*2;i++)dis[i]=INF;
q.push(ss{0,s}),dis[s]=0,vis[s]=1;
dijkstra();
write(dis[n]),putchar(' ');
}
| #include <bits/stdc++.h>
using namespace std;
#define st first
#define nd second
#define pb push_back
#define cl(x,v) memset((x), (v), sizeof(x))
#define db(x) cerr << #x << " == " << x << endl
#define dbs(x) cerr << x << endl
#define _ << ", " <<
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<int, pii> piii;
typedef pair<ll,ll> pll;
typedef pair<ll, pll> plll;
typedef vector<int> vi;
typedef vector <vi> vii;
const ld EPS = 1e-9, PI = acos(-1.);
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f, MOD = 1e9+7;
const int N = 2e5+5;
int cost = 0;
vector <piii> edges;
vector <pii> adj[N];
int par[N], sz[N], vis[N];
int find (int a) { return par[a] == a ? a : par[a] = find(par[a]); }
void unite (int a, int b) {
if ((a = find(a)) == (b = find(b))) return;
if (sz[a] < sz[b]) swap(a, b);
sz[a] += sz[b];
par[b] = a;
}
void dfs(int u){
for(auto v: adj[u]){
if(!vis[v.nd]){
if(vis[u] == v.st) vis[v.nd] = (v.st == 1 ? 2 : 1);
else{
vis[v.nd] = v.st;
}
dfs(v.nd);
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,m;
cin >> n >> m;
for(int i=1;i<=m;i++){
int u,v,c;
cin >> u >> v >> c;
edges.pb({c,{u,v}});
}
for (int i = 1; i <= n; i++) par[i] = i, sz[i] = 1;
sort(edges.begin(),edges.end());
for(auto e : edges){
if(find(e.nd.st) != find(e.nd.nd)){
adj[e.nd.st].pb({e.st,e.nd.nd});
adj[e.nd.nd].pb({e.st,e.nd.st});
//cout << e.nd.nd << " " << e.nd.st << "\n";
unite(e.nd.st, e.nd.nd);
}
}
vis[1] = 1;
dfs(1);
for(int i=1;i<=n;i++) cout << vis[i] << "\n";
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
const int maxn=2e5+10;
int n,m,t,is,cnt,ans;
string p;
int sum[10],q[150];
int main(){
#ifdef ONLINE_JUDGE
#else
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
ios::sync_with_stdio(false);
cout<<fixed<<setprecision(2);
memset(q,0,sizeof(q));
memset(sum,0,sizeof(sum));
int c=0;
for(int i=104;;i+=8){
if(i>=1000)break;
q[c++]=i;
}
// for(int i=0;i<c;i++)cout<<q[i]<<"\n";
// return 0;
cin>>p;
bool is=false;
for(int i=0;i<p.length();i++)++sum[p[i]-'0'];
if(p.length()==1&&(p[0]-'0')%8==0)is=true;
else if(p.length()==2){
int g=(p[0]-'0')+(p[1]-'0')*10;
if(g%8==0)is=true;
g=(p[1]-'0')+(p[0]-'0')*10;
if(g%8==0)is=true;
}
else {
for(int i=0;i<c;i++){
int a=q[i]%10,b=q[i]/10%10,c=q[i]/100;
--sum[a],--sum[b],--sum[c];
if(sum[a]>=0&&sum[b]>=0&&sum[c]>=0){
is=true;
break;
}
++sum[a],++sum[b],++sum[c];
}
}
cout<<(is?"Yes":"No")<<"\n";
return 0;
//good job!
} | #include <bits/stdc++.h>
const int N = 200005, P = 1e9 + 7;
char s[N]; int n, K, c[16], fac[20], ifac[20];
int qpow(int a, int b) {
int t = 1;
for (; b; b >>= 1, a = 1LL * a * a % P)
if (b & 1) t = 1LL * t * a % P;
return t;
}
int idx(char c) {
if (isdigit(c)) return c - '0';
else return c - 'A' + 10;
}
int C(int n, int m) {
if (m > n || m < 0) return 0;
return 1LL * fac[n] * ifac[m] % P * ifac[n - m] % P;
}
int f(int m, int n) {
// K - m
int k = K - m, ans = 0;
if (n < k) return 0;
for (int i = 0; i <= k; i++) {
ans = (ans + 1LL * C(k, i) * qpow(P - 1, k - i) % P * qpow(i + m, n)) % P;
}
return 1LL * ans * C(16 - m, k) % P;
}
int main() {
scanf("%s%d", s + 1, &K); n = strlen(s + 1);
fac[0] = ifac[0] = 1;
for (int i = 1; i <= 16; i++) fac[i] = 1LL * fac[i - 1] * i % P;
ifac[16] = qpow(fac[16], P - 2);
for (int i = 15; i; i--) ifac[i] = 1LL * ifac[i + 1] * (i + 1) % P;
int tot = 0, ans = 0;
for (int i = 1; i <= n; i++) {
int a = idx(s[i]);
for (int j = 0; j <= a; j++) {
if (i == 1 && j == 0 || i != n && j == a) continue;
int _tot = tot + (c[j] == 0);
ans = (ans + f(_tot, n - i)) % P;
}
if (c[a] == 0) tot++;
c[a] = 1;
}
for (int i = 2; i <= n; i++) ans = (ans + 15LL * f(1, n - i)) % P;
printf("%d", ans);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
typedef pair<ll,ll> P;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define EFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i, n) FOR(i,0,n)
#define INF 10e9
#define PI acos(-1)
bool cmp(const pair<int, int>& a, const pair<int, int>& b){
if(a.second != b.second){
return a.second < b.second;
}else{
return a.first < b.first;
}
return true;
}
int main() {
ll n; cin >> n;
vector<P> ball(n+1);
EFOR(i,1,n) cin >> ball[i].first >> ball[i].second;
sort(ball.begin(), ball.end(), cmp);
// dp[i][j] : 色iのボールのうち,j=0なら一番左,
// j=1なら一番右のものと同じ位置にいて,
// 色iのボールは全て回収し終わった状態になるまでの最小の時間
vector<vector<ll>> dp(n+1, vector<ll>(2,INF));
ll pr=0, pl=0;
dp[0][0] = 0,dp[0][1] = 0;
int index = 1;
for(int i = 1; i <= n;){
int j = i;
ll l = ball[i].first, r = ball[i].first;
for(;j <= n && ball[i].second == ball[j].second; j++) r = ball[j].first;
ll tmp;
if(pl > r) tmp = pl - l;
else tmp = r - pl + r - l;
dp[index][0] = dp[index-1][0] + tmp;
if(pr > r) tmp = pr - l;
else tmp = r - pr + r - l;
dp[index][0] = min(dp[index][0], dp[index-1][1] + tmp);
if(pr < l) tmp = r - pr;
else tmp = pr - l + r - l;
dp[index][1] = dp[index-1][1] + tmp;
if(pl < l) tmp = r - pl;
else tmp = pl - l + r - l;
dp[index][1] = min(dp[index][1],dp[index-1][0] + tmp);
pr = r;
pl = l;
index++;
i = j;
}
cout << min(dp[index-1][0] +abs(pl), dp[index-1][1] +abs(pr)) << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ff first
#define ss second
#define f(i,a,b) for(int i = a; i < b; i++)
#define fa(i,a,b) for(int i = a; i >= b; i--)
const int inf=1e9+10;
const ll N=1e5+5;
const ll mod=1e9+7;
int a[5];
int main(){
cin >> a[1] >> a[2] >> a[3];
sort(a+1,a+4);
cout << a[2]+a[3] << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
// ap + bq = gcd(a, b) なる (p, q) を求め d = gcd(a, b) を返す
ll extgcd(const ll a, const 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 mod s = a, x mod t = b なる x >= 0 の最小値を求める。存在しなければ -1
ll solve(const ll a, const ll b, const ll s, const ll t){
ll p, q;
const ll d = extgcd(s, t, p, q);
if ((a-b) % d != 0) return -1;
const ll ret = a + s * (((b - a) / d * p) % (t / d));
const ll s_lcm_t = s * t / d;
return (ret % s_lcm_t + s_lcm_t) % s_lcm_t;
}
int main(){
int t;
cin >> t;
for(int ti=0; ti<t; ++ti){
ll x, y, p, q;
cin >> x >> y >> p >> q;
ll ret = -1;
for(ll yi = 0; yi < y; ++yi){
for(ll qi = 0; qi < q; ++qi){
const ll v = solve(x + yi, p + qi, (x + y) * 2, p + q);
if (v >= 0){
if(ret == -1){
ret = v;
}else{
ret = min(ret, v);
}
}
}
}
if(ret == -1){
cout << "infinity" << endl;
}else{
cout << ret << endl;
}
}
return 0;
} | #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define IFOR(i, a, b) for (long long i = (a); i <= (long long)(b); i++)
using namespace std;
using VL = vector<long long>;
using ll = int64_t;
ll mod(ll x,ll m){ll y=x%m;return (y>=0LL)?y:y+m;}
template<class T>bool chmin(T& a, T b){if(a>b){a=b;return true;}else return false;}
ll gcd(ll a,ll b,ll& x,ll& y)
{if(a){ll d=gcd(b%a,a,x,y);y-=(b/a)*x;swap(x,y);if(d<0LL){d=-d;x=-x;y=-y;}return d;}else{x=0;y=1;return b;}}
int main(){
int T;
cin>>T;
string ans = "";
REP(i,T){
ll t = LLONG_MAX;
ll X,Y,P,Q;
scanf("%lld %lld %lld %lld",&X,&Y,&P,&Q);
ll a=(X+Y)<<1, b=P+Q;
ll x, y;
ll d = gcd(a, -b, x, y);
ll u = b/d, v = a/d;
ll stop0=P-X, stop1=P-X+Q-1LL;
ll wake0=P-X-Y+1LL, wake1=P-X;
ll from=min(stop0, wake0), to=max(stop1, wake1);
IFOR(z, from, to){
if(mod(z,d)!=0LL)continue;
if(stop0<=z && z<=stop1){
chmin(t,a*mod(z/d*x,u)+X);
}else{
chmin(t, b*mod(z/d*y,v)+P);
}
}
ans += (t==LLONG_MAX ? "infinity" : (to_string(t)))+"\n";
}
cout << ans;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define rrep(i, a, b) for (int i = a; i >= (b); --i)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
template <typename T>
void update_max(T& a, T b) {
if (b > a) a = b;
}
template <typename T>
void update_min(T& a, T b) {
if (b < a) a = b;
}
struct Tree {
typedef int T;
static constexpr T unit = 0;
T f(T a, T b) { return a ^ b; } // (any associative fn )
vector<T> s;
int n;
Tree(int n = 0, T def = unit) : s(2 * n, def), n(n) {}
void update(int pos, T val) {
for (s[pos += n] = val; pos /= 2;)
s[pos] = f(s[pos * 2], s[pos * 2 + 1]);
}
T query(int b, int e) { // query [ b , e)
T ra = unit, rb = unit;
for (b += n, e += n; b < e; b /= 2, e /= 2) {
if (b % 2) ra = f(ra, s[b++]);
if (e % 2) rb = f(s[--e], rb);
}
return f(ra, rb);
}
};
int main() {
#ifdef LOCAL_PROJECT
freopen("input.txt", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int n, q, a;
cin >> n >> q;
Tree tree(n);
rep(i, 0, n) {
cin >> a;
tree.update(i, a);
}
int t, x, y;
rep(i, 0, q) {
cin >> t >> x >> y;
if (t == 1) {
tree.update(x - 1, tree.s[x - 1 + tree.n] ^ y);
} else {
cout << tree.query(x - 1, y) << endl;
}
}
return 0;
} | #include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
string c;
cin >> c;
char a[] = "BWR";
for (int i = 0; i < n; i++) {
c[i] = c[i] == a[0] ? 0 : c[i] == a[1] ? 1 : 2;
}
int t = 0, s = 0, r = 1;
for (int i = 0; i < n; i++) {
//comb(n - 1, i) * c[i];
if (i > 0) {
int a0 = n - i;
int a1 = i;
while (a0 % 3 == 0) a0 /= 3, s++;
while (a1 % 3 == 0) a1 /= 3, s--;
r *= a0;
r %= 3;
r *= a1;
r %= 3;
}
int x = s > 0 ? 0 : r;
if (n % 2 == 0 && x != 0) x ^= 3;
t += x * c[i];
}
cout << a[t % 3] << endl;
return 0;
} |
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
const ll MOD = 1000000007;
const double PI = 3.141592653589793238;
int main() {
ll N, Q;
cin >> N >> Q;
vector<ll> A(N);
for (ll i = 0; i < N; ++i) cin >> A[i];
for (ll q = 0; q < Q; ++q) {
ll K;
cin >> K;
ll l = -1, r = (LLONG_MAX) / 2;
while (r - l > 1) {
ll m = (l + r) / 2;
ll x = (upper_bound(A.begin(), A.end(), m) - A.begin());
if (m - x >= K) r = m;
else l = m;
}
cout << r << "\n";
}
}
| #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long int
#define double long double
#define pb push_back
#define fi first
#define se second
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int>>
#define pqi priority_queue<int>
#define test int tcase; cin>>tcase; for(int tc = 1; tc <= tcase; tc++)
#define inp(a,n,f) vector<int> a(n+f);for(int hh=f;hh<n+f;hh++)cin>>a[hh];
#define printdecimal(k) cout<<fixed<<setprecision(k);
#define mem(a,k) memset(a,k,sizeof(a))
#define ub upper_bound
#define lb lower_bound
#define all(v) v.begin(),v.end()
#define mod (int)(1e9+7)
#define inf LLONG_MAX
#define OrderedSet tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
int exp(int x,int y){int val=1;x=x%mod;while(y>0){if(y&1)val=(val*x)%mod;y=y>>1;x=(x*x)%mod;}return val;}
int modinv(int x){return exp(x,mod-2);}
int add(int a,int b){a%=mod,b%=mod;a=((a+b)%mod+mod)%mod;return a;}
int sub(int a,int b){a%=mod,b%=mod;a=((a-b)%mod+mod)%mod;return a;}
int mul(int a,int b){a%=mod,b%=mod;a=((a*b)%mod+mod)%mod;return a;}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n,q;
cin>>n>>q;
inp(a,n,0);
vector<int> pre(n);
pre[0]=a[0]-1;
for(int i=1;i<n;i++)
pre[i]=pre[i-1]+a[i]-a[i-1]-1;
while(q--)
{
int k;
cin>>k;
auto it=lb(all(pre),k);
if(it!=pre.end())
{
int pos=it-pre.begin();
//cout<<a[pos]<<' '<<pre[pos]<<'\n';
cout<<a[pos]-(pre[pos]-k+1)<<'\n';
}
else
cout<<a[n-1]+k-pre[n-1]<<'\n';
}
return 0;
} |
#include<iostream>
int main() {
int N;
char cAA,cAB,cBA,cBB;
std::cin>>N>>cAA>>cAB>>cBA>>cBB;
if (cAA=='A'&&cAB=='A') {
std::cout<<'1'<<std::endl;
} else if (cAB=='B'&&cBB=='B') {
std::cout<<'1'<<std::endl;
} else if (N==2) {
std::cout<<'1'<<std::endl;
} else if (
(cAA=='A'&&cAB=='B'&&cBA=='A'&&cBB=='A')||
(cAA=='B'&&cAB=='B'&&cBA=='A'&&cBB=='A')||
(cAA=='B'&&cAB=='A'&&cBA=='B'&&cBB=='A')||
(cAA=='B'&&cAB=='A'&&cBA=='B'&&cBB=='B')
) {
long long ans = 1;
for (int i = 0; i < N-3; i++) {
ans*=2;
ans%=1000000007;
}
std::cout<<ans<<std::endl;
} else {
long long f[1000];
f[0] = 1;
f[1] = 1;
for (int i = 2; i <= 999; i++) {
f[i] = f[i-1] + f[i-2];
f[i] %=1000000007;
}
std::cout<<f[N-2]<<std::endl;
}
return 0;
}
| #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
//template
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(v) (v).begin(),(v).end()
using ll=long long int;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
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;}
//end
template<unsigned mod=998244353>struct fp {
using uint=unsigned; uint v;
static uint get_mod(){return mod;}
int inv() const{
int tmp,a=v,b=mod,x=1,y=0;
while(b)tmp=a/b,a-=tmp*b,swap(a,b),x-=tmp*y,swap(x,y);
if(x<0){x+=mod;} return x;
}
fp(ll x=0){init(x%mod+mod);}
fp& init(uint x){v=(x<mod?x:x-mod); return *this;}
fp operator-()const{return fp()-*this;}
fp pow(ll t){fp res=1,b=*this; while(t){if(t&1)res*=b;b*=b;t>>=1;} return res;}
fp& operator+=(const fp& x){return init(v+x.v);}
fp& operator-=(const fp& x){return init(v+mod-x.v);}
fp& operator*=(const fp& x){v=ll(v)*x.v%mod; return *this;}
fp& operator/=(const fp& x){v=ll(v)*x.inv()%mod; return *this;}
fp operator+(const fp& x)const{return fp(*this)+=x;}
fp operator-(const fp& x)const{return fp(*this)-=x;}
fp operator*(const fp& x)const{return fp(*this)*=x;}
fp operator/(const fp& x)const{return fp(*this)/=x;}
bool operator==(const fp& x)const{return v==x.v;}
bool operator!=(const fp& x)const{return v!=x.v;}
friend istream& operator>>(istream& is,fp& x){is>>x.v; return is;}
friend ostream& operator<<(ostream& os,const fp& x){os<<x.v; return os;}
}; using Fp=fp<>;
template<typename T>struct factorial {
vector<T> Fact,Finv,Inv;
factorial(int maxx){
Fact.resize(maxx); Finv.resize(maxx); Inv.resize(maxx);
Fact[0]=Fact[1]=Finv[0]=Finv[1]=Inv[1]=1;
rep(i,2,maxx){Fact[i]=Fact[i-1]*i;} Finv[maxx-1]=Fact[maxx-1].inv();
for(int i=maxx-1;i>=2;i--){Finv[i-1]=Finv[i]*i; Inv[i]=Finv[i]*Fact[i-1];}
}
T fact(int n,bool inv=0){if(n<0)return 0; return (inv?Finv[n]:Fact[n]);}
T inv(int n){if(n<0)return 0; return Inv[n];}
T nPr(int n,int r,bool inv=0){if(n<0||n<r||r<0)return 0; return fact(n,inv)*fact(n-r,inv^1);}
T nCr(int n,int r,bool inv=0){if(n<0||n<r||r<0)return 0; return fact(n,inv)*fact(r,inv^1)*fact(n-r,inv^1);}
};
Fp memo[3010][3010];
bitset<3010> used[3010];
Fp rec(int n,int k){
if(n<k)return 0;
if(used[n][k])return memo[n][k];
used[n][k]=1;
if(k==0){
return memo[n][k]=(n==0);
}
return memo[n][k]=rec(n-1,k-1)+rec(n,k*2);
}
int main(){
int n,k;
cin>>n>>k;
cout<<rec(n,k)<<'\n';
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ff first
#define ss second
#define mp make_pair
typedef long long int ll;
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll n, m; cin >> n >> m;
ll a[n][m];
ll mi = 9999;
ll sum = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> a[i][j];
sum += a[i][j];
mi = min(mi, a[i][j]);
}
}
cout << sum - mi*(n * m);
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define P pair<int,int>
#define Tp tuple<int,int,int>
using Graph = vector<vector<int>>;
int main(){
int N; cin >> N;
ll left[N+1],right[N+1]; bool col[N+1];
rep(i,N+1){
left[i] = 1000000001;
right[i] = -1000000001;
col[i] = false;
}
rep(i,N){
ll x,c; cin >> x >> c;
col[c] = true;
left[c] = min(left[c],x);
right[c] = max(right[c],x);
}
ll s1 = 0,s2 = 0;
ll dp[N+1][2];
rep(i,2){
rep(j,N+1){
dp[j][i] = 200000000000000000;
}
}
dp[0][0] = 0; dp[0][1] = 0;
rep(i,N){
if(!col[i+1]){
dp[i+1][0] = dp[i][0];
dp[i+1][1] = dp[i][1]; continue;
}
ll len = right[i+1]-left[i+1];
dp[i+1][0] = min(dp[i][0]+abs(s1-right[i+1]),dp[i][1]+abs(s2-right[i+1]));
dp[i+1][1] = min(dp[i][0]+abs(s1-left[i+1]),dp[i][1]+abs(s2-left[i+1]));
s1 = left[i+1]; s2 = right[i+1];
dp[i+1][0] += len; dp[i+1][1] += len;
}
cout << min(dp[N][0]+abs(s1),dp[N][1]+abs(s2)) << endl;
} |
#include <bits/stdc++.h>
// ifブロックはインデントがいる
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
#define repp(i, l, r) for (long long i = (l); i < (r); i++)
#define rep(i, n) for (long long i = 0; i < (n); ++i)
#define per(i, n) for (long long i = (n); i >= 0; --i)
#define all(v) v.begin(), v.end()
const int INF = 1 << 30;
const long long LINF = 1LL << 60;
const long long int MOD = 1000000007;
using namespace std;
using ll = long long;
using P = pair<long long, long long>;
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;
}
using PP = pair<ll, P>;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
vector<vector<ll> > g, dist;
vector<vector<vector<ll>>> predicted_cost;
vector<vector<P>> pre;
const ll h = 30, w = 30;
void dijkstra(int sx, int sy) {
dist = vector<vector<ll> >(h, vector<ll>(w, INF));
pre = vector<vector<P> >(h, vector<P>(w, P(-1, -1)));
dist[sx][sy] = 0;
priority_queue<PP, vector<PP>, greater<PP> > pq;
pq.push(PP(0, P(sx, sy)));
while (!pq.empty()) {
PP p = pq.top();
pq.pop();
ll c = p.first;
ll vx = p.second.first, vy = p.second.second;
rep(i, 4) {
ll nx, ny;
nx = vx + dx[i], ny = vy + dy[i];
if (nx < 0 || ny < 0 || nx >= h || ny >= w) continue;
if (dist[nx][ny] <= g[nx][ny] + c) continue;
dist[nx][ny] = g[nx][ny] + c;
pre[nx][ny] = P(vx, vy);
pq.push(PP(dist[nx][ny], P(nx, ny)));
}
}
}
vector<P> restore(ll tx, ll ty) {
vector<P> path;
ll x, y;
for (; tx != -1 || ty != -1; tx = pre[x][y].first, ty = pre[x][y].second) {
path.push_back(P(tx, ty));
x = tx, y = ty;
}
reverse(path.begin(), path.end());
return path;
}
char compare_pos(P s, P t) {
if (s.first < t.first)
return 'D';
if (s.first > t.first)
return 'U';
if (s.second < t.second)
return 'R';
if (s.second > t.second)
return 'L';
}
ll vector_average(vector<ll> &cost){
ll sum = accumulate(all(cost),0LL);
return sum / cost.size();
}
//ミョ(-ω- ?)
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
g.resize(30);
predicted_cost.resize(30);
rep(i, 30) {
g[i].resize(30);
predicted_cost[i].resize(30);
rep(j, 30) g[i][j] = 6000;
}
rep(q,1000){
P s,t;
cin >> s.first >> s.second >> t.first >> t.second;
dijkstra(s.first, s.second);
auto rstr = restore(t.first, t.second);
rep(i, rstr.size() - 1) { cout << compare_pos(rstr[i], rstr[i + 1]); }
cout << endl;
ll len;
cin >> len;
if(rstr.size())len /= rstr.size();
rep(i, rstr.size() - 1) {
predicted_cost[rstr[i].first][rstr[i].second].push_back(len);
if(q > 100)g[rstr[i].first][rstr[i].second] = vector_average(predicted_cost[rstr[i].first][rstr[i].second]);
}
}
return 0;
}
/*グラフ情報とは別に、パス長からの平均を順次格納していった2+1次元の配列をやりながら、その平均値を取って暫定の頂点間距離にするといい感じに収束していくのでは
-> 84163614602
パス長から乱数パラメーターのV_ip,H_ipを求められれば道のコストの尤度が大きくなって幸せになる
クエリの点数計算式 round(2312311 * \sigma_{k=1}^1000 {0.998^{1000-k} * a_k / b_k})から、序盤より後半のak / bkが重視される
序盤でより正確な道のコストを求められれば点数の改善ができる
パスに関して、M=1のときはパスの評価値に平均を使えるけど、M=2のときはyj(ramdom)によって[0,yj),[yj,2]
*/ | #include<bits/stdc++.h>
using namespace std;
template<class T> inline bool chmin(T& a,const T b){bool x=a>b;if(x)a=b;return x;}
constexpr int inf=1<<30;
double tate[29][30],yoko[30][29];
double dist[30][30];
int tateaccu[29][30],yokoaccu[29][30];
bool vis[30][30];
char rev[30][30];
using P=tuple<double,int,int>;
void solve(int sx,int sy,int ex,int ey){
for(int i=0;i<30;++i)for(int j=0;j<30;++j){
dist[i][j]=inf;
vis[i][j]=false;
}
auto func=[](P& l,P& r){return get<0>(l)>get<0>(r);};
priority_queue<P,vector<P>,decltype(func)> que(func);
que.emplace(0,sx,sy);
dist[sx][sy]=0;
while(!que.empty()){
double d;int i,j;
tie(d,i,j)=que.top();que.pop();
if(vis[i][j])continue;
vis[i][j]=true;
if(i>0&&chmin(dist[i-1][j],d+tate[i-1][j])){
rev[i-1][j]='U';
que.emplace(d+tate[i-1][j],i-1,j);
}
if(i<29&&chmin(dist[i+1][j],d+tate[i][j])){
rev[i+1][j]='D';
que.emplace(d+tate[i][j],i+1,j);
}
if(j>0&&chmin(dist[i][j-1],d+yoko[i][j-1])){
rev[i][j-1]='L';
que.emplace(d+yoko[i][j-1],i,j-1);
}
if(j<29&&chmin(dist[i][j+1],d+yoko[i][j])){
rev[i][j+1]='R';
que.emplace(d+yoko[i][j],i,j+1);
}
}
string ans;
while(sx!=ex||sy!=ey){
ans+=rev[ex][ey];
if(rev[ex][ey]=='U')++ex;
else if(rev[ex][ey]=='D')--ex;
else if(rev[ex][ey]=='L')++ey;
else --ey;
}
reverse(ans.begin(),ans.end());
cout<<ans<<'\n';
int dd;
cin>>dd;
double d=double(dd)/double(ans.size());
for(auto x:ans){
if(x=='U'){
--sx;
if(tateaccu[sx][sy]*2>ans.size()){
tateaccu[sx][sy]=ans.size();
tate[sx][sy]=d;
}
}
if(x=='D'){
if(tateaccu[sx][sy]*2>ans.size()){
tateaccu[sx][sy]=ans.size();
tate[sx][sy]=d;
}
++sx;
}
if(x=='L'){
--sy;
if(yokoaccu[sx][sy]*2>ans.size()){
yokoaccu[sx][sy]=ans.size();
yoko[sx][sy]=d;
}
}
if(x=='R'){
if(yokoaccu[sx][sy]*2>ans.size()){
yokoaccu[sx][sy]=ans.size();
yoko[sx][sy]=d;
}
++sy;
}
}
}
int main(){
for(int i=0;i<29;++i)for(int j=0;j<30;++j){
tate[i][j]=4500;
tateaccu[i][j]=inf/2;
}
for(int i=0;i<30;++i)for(int j=0;j<29;++j){
yoko[i][j]=4500;
yokoaccu[i][j]=inf/2;
}
for(int k=0;k<1000;++k){
int a,b,c,d;
cin>>a>>b>>c>>d;
solve(a,b,c,d);
}
} |
#include<cstdio>
#include<cstring>
#define int long long
const int N=105,MOD=998244353;
int n;
int w[N];
int f[N][N*N],fac[N],ans;
signed main()
{
scanf("%lld",&n);
for(int i=1;i<=n;++i)
{
scanf("%lld",w+i);
w[0]+=w[i];
}
fac[0]=1;
for(int i=1;i<=n;++i)
fac[i]=fac[i-1]*i%MOD;
if(w[0]%2)
printf("0\n");
else
{
w[0]/=2;
// printf("%lld\n",w[0]);
f[0][0]=1;
for(int i=1;i<=n;++i)
for(int j=w[0];j>=0;--j)
for(int k=1;k<=i;++k)
if(j>=w[i])
f[k][j]=(f[k][j]+f[k-1][j-w[i]])%MOD;
for(int k=1;k<n;++k)
{
// printf("%lld\n",f[k][w[0]]);
ans=(ans+f[k][w[0]]*fac[k]%MOD*fac[n-k]%MOD)%MOD;
}
printf("%lld\n",ans);
}
return 0;
} | #include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <bitset>
#include <map>
// #include <bits/stdc++.h>
uintmax_t combination(unsigned int n, unsigned int r) {
if ( r * 2 > n ) r = n - r;
uintmax_t dividend = 1;
uintmax_t divisor = 1;
for ( unsigned int i = 1; i <= r; ++i ) {
dividend *= (n-i+1);
divisor *= i;
}
return dividend / divisor;
}
using namespace std;
typedef long long ll;
#define REP(i, n) for(int i=0; i<(n); ++i)
#define FOR(i, a, b) for(int i=(a); i<(b); ++i)
#define FORR(i, a, b) for(int i=(b)-1; i>=(a); --i)
constexpr ll MOD=1000000007ll;
// constexpr ll MOD=998244353ll;
constexpr int MAX=510000;
ll fact[MAX], fact_inv[MAX];
// 繰り返し二乗法
ll power(ll a, ll b){
ll res=1;
while(b>0){
if(b&1) res=res*a%MOD;
a=a*a%MOD;
b>>=1;
}
return res;
}
ll comb(ll n, ll r){
return (fact[n]*fact_inv[r])%MOD*fact_inv[n-r]%MOD;
}
/*
std::vector<std::vector<long long>> comb(int n, int r) {
std::vector<std::vector<long long>> v(n + 1,std::vector<long long>(n + 1, 0));
for (int i = 0; i < v.size(); i++) {
v[i][0] = 1;
v[i][i] = 1;
}
for (int j = 1; j < v.size(); j++) {
for (int k = 1; k < j; k++) {
v[j][k] = (v[j - 1][k - 1] + v[j - 1][k]);
}
}
return v;
}
*/
int main(void) {
int N;
cin >> N;
vector<long long> A(N, 0);
fact[0]=1;
// input
for (int i = 0; i < N; i++) cin >> A[i];
long long ans = 0;
// 200で割ったあまりにする
for (int i = 0; i < N; i++) {
A[i] = A[i] % 200;
}
// 種類の数え上げ
map<long long, long long> mp;
for (int i = 0; i < N; i++) {
// 存在する
if ( auto iter = mp.find(A[i]); iter != end(mp) ) {
// std::cout << iter->second << std::endl;
mp[A[i]] += 1;
} else {
// std::cout << "not exists" << std::endl;
mp[A[i]] = 1;
}
}
// さらにまとめる
map<long long, long long> mp2;
for (const auto& [key, value] : mp){
// 存在する
if ( auto iter = mp2.find(value); iter != end(mp2) ) {
// std::cout << iter->second << std::endl;
mp2[value] += 1;
} else {
// std::cout << "not exists" << std::endl;
mp2[value] = 1;
}
}
for (const auto& [key, value] : mp2){
// std::cout << key << " => " << value << "\n";
}
for (const auto& [key, value] : mp2){
if (key > 1) {
// std::cout << key << " => " << value << "\n";
// cout << comb(value, 2)[value][2] << endl;
ans += combination(key, 2) * value;
}
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < n; ++i)
using C = complex<double>;
const ll md = 1000000007;
int main(){
int n;
cin >> n;
cout << n - 1 << endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, N) for(int i = 0; i < (int)N; i++)
#define rep1(i, N) for(int i = 1; i <= (int)N; i++)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef vector<int> vi;
int main() {
ll n;
scanf("%lld", &n);
vector<ll> a;
for(ll i=2;i<=n;i++){
a.emplace_back(i);
}
rep(i,(int)a.size()-1) rep(j,(int)a.size()-1){
a[j] = a[j]/__gcd(a[j],a[j+1])*a[j+1];
}
printf("%lld\n", a[0]+1);
} |
//#include <atcoder/all>
#include <bits/stdc++.h>
#include <cassert>
#include <numeric>
using namespace std;
//using namespace atcoder;
typedef long long LONG;
const int MOD = 1000000007;
int gcd(int x, int y) {
if (x % y == 0) {
return y;
}
else {
return gcd(y, x % y);
//x%y==0でないときはユーグリットの互除法を使って再帰的に関数を呼び出す。
}
}
LONG pow2(long long x, long long n) {
long long ret = 1;
while (n > 0) {
if (n & 1) ret = ret * x % MOD; // n の最下位bitが 1 ならば x^(2^i) をかける
x = x * x % MOD;
n >>= 1; // n を1bit 左にずらす
}
return (int)ret;
}
void printVector(const vector<int>& vec) {
for (int value : vec) {
cout << value << " ";
}
cout << endl;
}
LONG t(LONG N,LONG i) {
return N - abs(i);
}
int count(vector<int>&a, int s) {
int result = 0;
for (int i = 0; i < a.size(); i++) {
if (a[i] == s) {
result++;
}
}
return result;
}
LONG kaizyo(int N) {
LONG result = 1;
LONG mod = 998244353;
for (LONG i = 2; i <= N; i++) {
result *= i;
result = result % mod;
}
return result;
}
class Point {
public:
int x;
int y;
};
LONG stock[3500][3500];
LONG test(int N, int task) {
LONG mod = 998244353;
if (N < task)return 0;
if (N < 0)return 0;
if (task == 0) {
if (N == 0) {
return 1;
}
else {
return 0;
}
}
if (stock[N][task] > 0)return stock[N][task];
stock[N][task] =( test(N - 1, task - 1) + test(N, task * 2)) % mod;
return stock[N][task]%mod ;
}
class DP {
public:
DP() {
}
DP(int a, int b) {
other = a;
path = b;
}
int other;
int path;
};
void dp(vector<vector<DP>>& list, vector<int> &able, int pos,int limit) {
for (int i = 0; i < list[pos].size(); i++) {
if (list[pos][i].path <= limit) {
if (able[list[pos][i].other] == 0) {
able[list[pos][i].other] = 1;
dp(list, able, list[pos][i].other, limit);
}
}
}
}
int main() {
LONG N;
cin >> N;
vector<Point>P(N);
for (int i = 0; i < N; i++) {
cin >>P[i].x>>P[i].y;
}
vector<vector<DP>>d(N+2, vector<DP>(0));
for (int i = 0; i < N; i++) {
for (int j = 0; j < i; j++) {
int r = (P[i].x - P[j].x)*(P[i].x - P[j].x) + (P[i].y - P[j].y)*(P[i].y - P[j].y);
if (r < 40000) {
d[i+2].push_back(DP(j+2, r));
d[j+2].push_back(DP(i+2, r));
}
}
}
for (int i = 0; i < N; i++) {
d[i+2].push_back(DP(0, (100-P[i].y)* (100 - P[i].y)));
d[0].push_back(DP(i+2, (100 - P[i].y)*(100-P[i].y)));
}
for (int i = 0; i < N; i++) {
d[i + 2].push_back(DP(1, (-100 - P[i].y)* (-100 - P[i].y)));
d[1].push_back(DP(i + 2, (-100 - P[i].y)* (-100 - P[i].y)));
}
d[0].push_back(DP(1, 40000));
d[1].push_back(DP(0, 40000));
int max = 40000;
int min = 0;
int mid = (max) / 2;
while (max - min > 1) {
bool frag = false;
vector<int> able(N + 2, 0 );//未到達は0、到達は1
able[0] = 1;
dp(d, able, 0, mid);
if (able[1] == 1) {
max = mid;
mid = (max + min) / 2;
}else{
min = mid;
mid = (max + min) / 2;
}
// cout << max<<"\t" << min << "\t" << mid << "\n";
}
cout << std::fixed << std::setprecision(15) << sqrt(mid+1)/2.0;
return 0;
} | #include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ch() getchar()
#define pc(x) putchar(x)
using namespace std;
template<typename T>void read(T&x){
char c;int f;
for(c=ch(),f=1;c<'0'||c>'9';c=ch())if(c=='-')f=-f;
for(x=0;c>='0'&&c<='9';c=ch())x=x*10+(c&15);x*=f;
}
template<typename T>void write(T x){
static char q[64];int cnt=0;
if(x==0)return pc('0'),void();
if(x<0)pc('-'),x=-x;
while(x)q[cnt++]=x%10,x/=10;
while(cnt--)pc(q[cnt]+'0');
}
const int maxn=105;
struct Node{
int v,w;
Node(int v=0,int w=0):
v(v),w(w){}
bool operator < (const Node o)const{
return w<o.w;
}
}hp[maxn*maxn*2];
int sz;
void push(Node x){
int no=++sz;
while(no>1&&x<hp[no>>1])
hp[no]=hp[no>>1],no>>=1;
hp[no]=x;
}
void pop(void){
swap(hp[1],hp[sz--]);
int no=1,nt=2;Node x=hp[1];
while(nt<=sz){
if((nt|1)<=sz&&hp[nt|1]<hp[nt])nt|=1;
if(hp[nt]<x)hp[no]=hp[nt];else break;
no=nt;nt<<=1;
}
hp[no]=x;
}
int top(void){
return hp[1].v;
}
struct Edge{
int v,w,nt;
Edge(int v=0,int w=0,int nt=0):
v(v),w(w),nt(nt){}
}e[maxn*maxn*2];
int hd[maxn],num;
void qwq(int u,int v,int w){
e[++num]=Edge(v,w,hd[u]),hd[u]=num;
}
void qvq(int u,int v,int w){
qwq(u,v,w);qwq(v,u,w);
}
int sq(int x){
return x*x;
}
int dis[maxn],vis[maxn],x[maxn],y[maxn];
int main(){
int n;read(n);
int S=n+1,T=S+1;
qvq(S,T,sq(200));
for(int i=1;i<=n;++i){
read(x[i]),read(y[i]);
qvq(S,i,sq(y[i]+100));
qvq(i,T,sq(100-y[i]));
for(int j=1;j<i;++j){
qvq(i,j,sq(x[i]-x[j])+sq(y[i]-y[j]));
}
}
memset(dis,0x3f,sizeof dis);
push(Node(S,dis[S]=0));
while(sz){
int u=top();pop();
if(vis[u])continue;vis[u]=true;
for(int i=hd[u];i;i=e[i].nt){
int v=e[i].v,w=max(dis[u],e[i].w);
if(dis[v]>w)push(Node(v,dis[v]=w));
}
}
printf("%.20f\n",sqrt(dis[T])/2.0);
return 0;
}
/*
所以,永远不要问丧钟为谁而鸣,它为你而鸣。
*/
|
#include<iostream>
#include<cmath>
#include<vector>
#include<bitset>
#include<bitset>
#include<string>
#include<utility>
#include<queue>
#include <iomanip>
#include <limits>
#include<tuple>
#include<algorithm>
#include<set>
using namespace std;
typedef long long int ll;
// ll
int main(){
string s;
cin >> s;
// vector<pair<int,char>> block(26,{-1,'0'});
int mae=(-1);
ll point=0;
char ima='0';
for(int i=0;i<s.size()-1;i++){
if(s.at(i)==s.at(i+1) && s.at(i)!=ima){
if(mae!=(-1))point+=(s.size()-i);
mae=i;
ima=s.at(i);
}else{
if(ima!='0'){
if(ima!=s.at(i)){
point++;
}
}
}
}
if(s.at(s.size()-1)!=ima && ima!='0')point++;
cout << point << endl;
return 0;
} | #ifndef Local
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("popcnt,abm,mmx,avx2")
#endif
#include <bits/stdc++.h>
using namespace std;
#define popCnt(x) (__builtin_popcountll(x))
#define sz(x) ((int)(x.size()))
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
#define rep(i, l, r) for (int i = l; i < r; ++i)
using Long = long long;
using Double = double;
using vi = vector<int>;
template <class U, class V>
istream& operator>>(istream& is, pair<U, V>& p) {
is >> p.first >> p.second;
return is;
}
template <class T>
istream& operator>>(istream& is, vector<T>& v) {
for (auto& x : v) {
is >> x;
}
return is;
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
for (auto& x : v) {
os << x << " ";
}
return os;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifdef Local
freopen("test.in", "r", stdin);
freopen("test.out", "w", stdout);
#else
#define endl '\n'
#endif
string s;
cin >> s;
Long res = 0;
vector<int> cnt(26);
for (int i = sz(s) - 1; i >= 0; --i) {
++cnt[s[i] - 'a'];
if (i + 1 < sz(s) && s[i] == s[i + 1]) {
res += sz(s) - i - cnt[s[i] - 'a'];
cnt.assign(26, 0);
cnt[s[i] - 'a'] = sz(s) - i;
}
}
cout << res << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a, b, w;
cin >> a >> b >> w;
int m = 1e9, M = 0;
for (int n = 1; n <= 1000000; n++)
{
if (a * n <= 1000 * w && 1000 * w <= b * n)
{
m = min(m, n);
M = max(M, n);
}
}
if (M == 0)
cout << "UNSATISFIABLE";
else
cout << m << ' ' << M;
}
| #include<bits/stdc++.h>
using namespace std;
int main(){
char a,b,c;
cin>>a>>b>>c;
if(a==b&&b==c){
cout<<"Won";
}
else cout<<"Lost";
} |
#include "bits/stdc++.h"
using namespace std;
using ll = int64_t;
using vi = vector<int>;
using vb = vector<bool>;
using si = set<int>;
#define pb push_back
#define ist insert
#define sz(x) int((x).size())
#define ice(i, a, b) for (int (i) = (a); (i) < (b); ++(i))
vector<si> adj(20);
vi comp, colors(20, -1); vb done(20);
ll ans = 1, temp = 0;
void check() {
bool ok = true;
ice(i, 0, sz(comp)) {
ice(j, i + 1, sz(comp)) {
if (adj[comp[i]].count(comp[j])) {
ok &= (colors[i] != colors[j]);
}
if (!ok) {
return;
}
}
}
temp++;
}
void color(int idx) {
if (idx == sz(comp)) {
check();
return;
}
if (!idx) {
colors[0] = 0;
color(1);
colors[0] = -1;
colors[0] = 1;
color(1);
colors[0] = -1;
colors[0] = 2;
color(1);
colors[0] = -1;
}
else {
ice(i, 0, idx) {
if (adj[comp[idx]].count(comp[i])) {
if (colors[i] != 0) {
colors[idx] = 0;
color(idx + 1);
colors[idx] = -1;
}
if (colors[i] != 1) {
colors[idx] = 1;
color(idx + 1);
colors[idx] = -1;
}
if (colors[i] != 2) {
colors[idx] = 2;
color(idx + 1);
colors[idx] = -1;
}
return;
}
}
}
}
void cnt() {
if (ans) {
color(0);
}
ans *= temp; temp = 0;
}
void dfs(int n) {
if (done[n]) {
return;
}
done[n] = true;
comp.pb(n);
for (int m : adj[n]) {
dfs(m);
}
}
void solve() {
int n, m; cin >> n >> m;
ice(i, 0, m) {
int a, b; cin >> a >> b; a--; b--;
adj[a].ist(b); adj[b].ist(a);
}
ice(i, 0, n) {
if (!done[i]) {
dfs(i);
cnt();
vi().swap(comp);
vi(20, -1).swap(colors);
}
}
cout << ans;
}
int main() {
int tc = 1;
ice(i, 0, tc) {
solve();
}
} | // YATIN KWATRA
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ar array
#define sz(v) (int)(v.size())
#define inf 1e18
#define int ll
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ld long double
#define ull unsigned long long
#define endl "\n"
#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 vii vector<int>
#define pq priority_queue
#define uomii unordered_map<int,int,best_hash>
#define all(v) v.begin(),v.end()
#define mp make_pair
#define pb push_back
#define pob pop_back
#define ff first
#define ss second
#define pii pair<int,int>
#define mii map<int,int>
#define vvii vector<vii>
#define mod 1000000007
#define MIN -1e9
#define pi 3.1415926535897932384626433832795
#define cz(x) 63 - __builtin_clzll(x)
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 best_hash {
static uint64_t splitmix64(uint64_t x) {
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);
}
};
void INPUT()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
/*
----------------------------------------------------------------
-> Check For Overflows
-> Out of Bounds
-> Initialisations of global arrays and variables
----------------------------------------------------------------
*/
const int N = 20;
vii v[N];
int jude[N][N];
int col[N];
bool found;
void dfs(int node, int par, int c) {
col[node] = c;
for (auto &i : v[node]) {
if (found) return;
if (i == par or col[i] == 3) continue;
if (!col[i]) dfs(i, node, 3 - c);
else if (col[i] == c) {
found = 1;
return;
}
}
}
int n;
bool valid(int mask) {
fo(i, 0, n - 1) {
fo(j, i + 1, n - 1) {
if (mask & (1 << i) and mask & (1 << j) and jude[i][j]) return 0;
}
}
return 1;
}
void solve()
{
int m;
cin >> n >> m;
while (m--) {
int x, y;
cin >> x >> y;
x--, y--;
jude[x][y] = jude[y][x] = 1;
v[x].pb(y);
v[y].pb(x);
}
int ans = 0;
fo(mask, 0, (1 << n) - 1) {
if (!valid(mask)) continue;
fo(i, 0, n - 1) {
if (mask & (1 << i)) col[i] = 3;
else col[i] = 0;
}
int cnt = 1;
fo(i, 0, n - 1) {
if (!col[i]) {
found = 0;
dfs(i, i, 1);
if (found) {
cnt = 0;
break;
}
else cnt *= 2;
}
}
ans += cnt;
}
cout << ans;
}
signed main() {
FIO
INPUT();
int t;
t = 1;
//cin >> t;
fo(i, 0, t - 1) {
solve();
}
return 0;
}
|
//I'll always miss you like a darling.
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define LD long double
#define ull unsigned long long
#define x first
#define y second
#define pb push_back
#define pf push_front
#define mp make_pair
#define Pair pair<int,int>
#define pLL pair<LL,LL>
#define pii pair<double,double>
#define LOWBIT(x) x & (-x)
#define rep(i,a,b) for (int i=a;i<=b;i++)
#define REP(i,a,b) for (int i=a;i>=b;i--)
const int INF=2e9;
const LL LINF=2e16;
const int magic=348;
const int MOD=1e9+7;
const double eps=1e-10;
const double pi=acos(-1);
struct fastio
{
static const int S=1e7;
char rbuf[S+48],wbuf[S+48];int rpos,wpos,len;
fastio() {rpos=len=wpos=0;}
inline char Getchar()
{
if (rpos==len) rpos=0,len=fread(rbuf,1,S,stdin);
if (!len) return EOF;
return rbuf[rpos++];
}
template <class T> inline void Get(T &x)
{
char ch;bool f;T res;
while (!isdigit(ch=Getchar()) && ch!='-') {}
if (ch=='-') f=false,res=0; else f=true,res=ch-'0';
while (isdigit(ch=Getchar())) res=res*10+ch-'0';
x=(f?res:-res);
}
inline void getstring(char *s)
{
char ch;
while ((ch=Getchar())<=32) {}
for (;ch>32;ch=Getchar()) *s++=ch;
*s='\0';
}
inline void flush() {fwrite(wbuf,1,wpos,stdout);fflush(stdout);wpos=0;}
inline void Writechar(char ch)
{
if (wpos==S) flush();
wbuf[wpos++]=ch;
}
template <class T> inline void Print(T x,char ch)
{
char s[20];int pt=0;
if (x==0) s[++pt]='0';
else
{
if (x<0) Writechar('-'),x=-x;
while (x) s[++pt]='0'+x%10,x/=10;
}
while (pt) Writechar(s[pt--]);
Writechar(ch);
}
inline void printstring(char *s)
{
int pt=1;
while (s[pt]!='\0') Writechar(s[pt++]);
}
}io;
template<typename T> inline void check_max(T &x,T cmp) {x=max(x,cmp);}
template<typename T> inline void check_min(T &x,T cmp) {x=min(x,cmp);}
template<typename T> inline T myabs(T x) {return x>=0?x:-x;}
template<typename T> inline T mygcd(T x,T y) {return y==0?x:mygcd(y,x%y);}
inline int add(int x) {if (x>=MOD) x-=MOD;return x;}
inline int add(int x,int MO) {if (x>=MO) x-=MO;return x;}
inline int sub(int x) {if (x<0) x+=MOD;return x;}
inline int sub(int x,int MO) {if (x<0) x+=MO;return x;}
inline void Add(int &x,int y) {x=add(x+y);}
inline void Add(int &x,int y,int MO) {x=add(x+y,MO);}
inline void Sub(int &x,int y) {x=sub(x-y);}
inline void Sub(int &x,int y,int MO) {x=sub(x-y,MO);}
template<typename T> inline int quick_pow(int x,T y) {int res=1;while (y) {if (y&1) res=1ll*res*x%MOD;x=1ll*x*x%MOD;y>>=1;}return res;}
template<typename T> inline int quick_pow(int x,T y,int MO) {int res=1;while (y) {if (y&1) res=1ll*res*x%MO;x=1ll*x*x%MO;y>>=1;}return res;}
const int MAXN=2000;
int n,a[MAXN+48];
unordered_map<int,int> Mp;
int main ()
{
#ifndef ONLINE_JUDGE
double TIME=clock();
freopen ("a.in","r",stdin);
freopen ("a.out","w",stdout);
cerr<<"Running..."<<endl;
#endif
scanf("%d",&n);rep(i,1,n) scanf("%d",a+i);
int minn=a[1];rep(i,2,n) check_min(minn,a[i]);
rep(i,1,n) rep(j,1,int(sqrt(a[i]))) if (a[i]%j==0) Mp[j]=mygcd(Mp[j],a[i]),Mp[a[i]/j]=mygcd(Mp[a[i]/j],a[i]);
int ans=0;for (auto item : Mp) if (item.x<=minn && item.x==item.y) ans++;
printf("%d\n",ans);
#ifndef ONLINE_JUDGE
cerr<<"Exec Time: "<<(clock()-TIME)/CLOCKS_PER_SEC<<endl;
#endif
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int _ = (cout << fixed << setprecision(9), cin.tie(0), ios::sync_with_stdio(0));
using Int = long long;
template <class T>
using MinHeap = priority_queue<T, vector<T>, greater<T>>;
int main() {
int N; cin >> N;
vector<int> V(2 * N); for (auto &v : V) cin >> v;
MinHeap<int> heap;
for (int i = N - 1; i >= 0; i--) {
int a = i;
int b = 2 * N - 1 - i;
if (V[a] > V[b]) {
heap.emplace(V[a]);
if (!heap.empty() && V[b] > heap.top()) {
heap.pop();
heap.emplace(V[b]);
}
} else {
heap.emplace(V[b]);
if (!heap.empty() && V[a] > heap.top()) {
heap.pop();
heap.emplace(V[a]);
}
}
}
Int ans = 0;
while (!heap.empty()) {
ans += heap.top();
heap.pop();
}
cout << ans << '\n';
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const long long INF = 1LL<<60;
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; }
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(a) (a).begin(),(a).end()
int main() {
ll N,M;
cin >> N >> M;
vector<ll> aa((1+N)/2);
vector<ll> bb((1+N)/2);
vector<ll> hh(N);
vector<ll> ww(M);
for(ll i=0;i<N;i++) cin >> hh[i];
for(ll i=0;i<M;i++) cin >> ww[i];
sort(ALL(hh));
ll ans=INF;
aa[0]=0;
for(ll i=1;i<(N+1)/2;i++){
aa[i]=aa[i-1]+hh[2*i-1]-hh[2*i-2];
}
for(ll i=(N-2)/2;i>=0;i--){
bb[i]=bb[i+1]+hh[2*i+2]-hh[2*i+1];
}
for(auto gg : ww){
ll x=lower_bound(ALL(hh),gg)-hh.begin();
if(x & 1) x^=1;
chmin(ans,aa[x/2]+bb[x/2]+abs(gg-hh[x]));
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pdd;
#define lc u << 1
#define rc u << 1 | 1
#define mid (t[u].l + t[u].r) / 2
#define INF 0x3f3f3f3f
#define lowbit(x) x & (-x)
#define mem(a, b) memset(a , b , sizeof(a))
#define FOR(i, x, n) for(int i = x;i <= n; i++)
// const ll mod = 998244353;
// const ll mod = 1e9 + 7;
// const double eps = 1e-6;
// const double PI = acos(-1);
// const double R = 0.57721566490153286060651209;
void solve() {
int n, m;
cin >> n >> m;
vector<ll> v1(n + 1), v2(m + 1);
for(int i = 1;i <= n; i++) cin >> v1[i];
for(int i = 1;i <= m; i++) cin >> v2[i];
sort(v1.begin() + 1, v1.end());
vector<ll> pre(n + 2), suf(n + 2);
for(int i = 2;i <= n; i += 2) pre[i] = pre[i - 2] + abs(v1[i] - v1[i - 1]);
for(int i = n - 1;i >= 1; i -= 2) suf[i] = suf[i + 2] + abs(v1[i + 1] - v1[i]);
ll ans = 1e18;
for(int i = 1;i <= m; i++) {
int pos = lower_bound(v1.begin(), v1.end(), v2[i]) - v1.begin();
if(pos % 2 == 0) pos--;
ans = min(ans, (pre[pos - 1] + suf[pos + 1] + abs(v2[i] - v1[pos])));
}
cout << ans << endl;
}
signed main() {
ios_base::sync_with_stdio(false);
//cin.tie(nullptr);
//cout.tie(nullptr);
#ifdef FZT_ACM_LOCAL
// int size=40<<20;
// __asm__ ("movq %0,%%rsp\n"::"r"((char*)malloc(size)+size));
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
signed test_index_for_debug = 1;
char acm_local_for_debug = 0;
do {
if (acm_local_for_debug == '$') exit(0);
if (test_index_for_debug > 20)
throw runtime_error("Check the stdin!!!");
auto start_clock_for_debug = clock();
solve();
auto end_clock_for_debug = clock();
cout << "Test " << test_index_for_debug << " successful" << endl;
cerr << "Test " << test_index_for_debug++ << " Run Time: "
<< double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl;
cout << "--------------------------------------------------" << endl;
} while (cin >> acm_local_for_debug && cin.putback(acm_local_for_debug));
#else
solve();
#endif
return 0;
}
|
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long
typedef long double ld;
#define pb push_back
#define f(i,a,b) for(int i=a;i<b;i++)
#define fd(i,a,b) for(int i=a-1;i>=b;i--)
#define print(a,n) f(i,0,n){cout<<a[i]<<" ";}
#define pf push_front
#define fi first
#define se second
#define ps(x,y) fixed<<setprecision(y)<<x
#define INF 1e10
const double pi=3.14159265358979323;
const int mod=998244353;
const int MAX=1000000+5;
int p[MAX]={0};
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
void swap(int &a,int& b)
{
int temp=a;
a=b;
b=temp;
}
int binpow(int a,int b)
{
int res = 1;
while (b > 0) {
if (b%2 == 1)
res = (res * a)%mod;
a = (a* a)% mod;
b >>= 1;
}
return res;
}
int modinv(int y)
{
return binpow(y,mod-2);
}
int fact[500000+5];
void precal()
{
fact[0]=1;
for(int i=1;i<500000+5;i++)
{
fact[i]=(i*fact[i-1])%mod;
}
}
bool isPrime(int n)
{
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
int mm[200000+5]={0};
int pos[200000+5]={0};
bool flag=true;
int pr[200000+5]={0};
vector<pair<int ,int>> vv;
int vis[200000+5]={0};
vector<vector<int >> adj;
queue<int > q;
int dfs(int j)
{
vis[j]=1;
pos[j]=1;
for(auto i:adj[j])
{
if(vis[i]==0)
{
pos[j]+=dfs(i);
}
}
return pos[j];
}
/*void bfs(int j)
{
q.push(j);
while(!q.empty())
{
int r=q.front();
q.pop();
for(auto i:adj[r])
{
if(vis[i]==0)
{
pr[i]=r;
vis[i]=1;
q.push(i);
}
}
}
}*/
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
/*#ifndef ONLINE_JUDGE
if(fopen("INPUT.txt","r"))
{
freopen ("INPUT.txt" , "r" , stdin);
freopen ("OUTPUT.txt" , "w" , stdout);
}
#endif*/
int t=1;
// cin>>t;
while(t--)
{
int n;
cin>>n;
int a[n][2];
f(i,0,n)
{
cin>>a[i][0]>>a[i][1];
}
bool flag=false;
f(i,0,n-2)
{
f(j,i+1,n-1)
{
f(k,j+1,n)
{
if((a[k][0]-a[i][0])*(a[j][1]-a[i][1])==(a[k][1]-a[i][1])*(a[j][0]-a[i][0]))
{
flag=true;
}
}
}
}
if(flag)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
}
| #include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <cstdio>
#include <cmath>
#define rep(i, n) for(i = 0; i < n; i++)
#define int long long
using namespace std;
int n;
int x[200000], y[200000];
signed main() {
int i;
cin >> n;
rep(i, n) cin >> x[i] >> y[i];
int xl = x[0], xr = x[0], yl = y[0], yr = y[0];
int xlc = 0, xrc = 0, ylc = 0, yrc = 0;
rep(i, n) xl = min(xl, x[i]);
rep(i, n) xr = max(xr, x[i]);
rep(i, n) yl = min(yl, y[i]);
rep(i, n) yr = max(yr, y[i]);
rep(i, n) if (xl == x[i]) xlc++;
rep(i, n) if (xr == x[i]) xrc++;
rep(i, n) if (yl == y[i]) ylc++;
rep(i, n) if (yr == y[i]) yrc++;
int cnt = 0;
rep(i, n) {
if (xl == x[i] && yl == y[i]) continue;
if (xr == x[i] && yr == y[i]) continue;
if (xl == x[i] || yl == y[i] || xr == x[i] || yr == y[i]) cnt++;
}
int ans = 0;
if (xlc * xrc >= 2) ans = max(ans, xr - xl);
if (ylc * yrc >= 2) ans = max(ans, yr - yl);
if (cnt > 0) ans = max(ans, min(xr - xl, yr - yl));
rep(i, n) if (x[i] != xr) ans = max(ans, x[i] - xl);
rep(i, n) if (x[i] != xl) ans = max(ans, xr - x[i]);
rep(i, n) if (y[i] != yr) ans = max(ans, y[i] - yl);
rep(i, n) if (y[i] != yl) ans = max(ans, yr - y[i]);
cout << ans << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define all(x) (x).begin(),(x).end()
const string YES = "Yes";
const string NO = "No";
const double PI = 3.14159265359;
struct Gi {
ll a, b;
explicit Gi(ll x = 0, ll y = 0) : a(x), b(y) {}
Gi operator*(const Gi &other) const {
return Gi(a * other.a - b * other.b, a * other.b + b * other.a);
}
Gi operator-(const Gi &other) const {
return Gi(a - other.a, b - other.b);
}
bool operator<(const Gi &other) const {
if (a == other.a) return b < other.b;
return a < other.a;
}
bool operator>(const Gi &other) const {
if (a == other.a) return b > other.b;
return a > other.a;
}
bool operator==(const Gi &other) const {
return a == other.a && b == other.b;
}
[[nodiscard]] ll norm() const {
return a * a + b * b;
}
};
Gi inC() {
ll a, b;
cin >> a >> b;
return Gi(a, b);
}
vector<Gi> cnv(vector<Gi> pos, Gi origin, Gi rad, int N) {
rep(i, N) pos.at(i) = pos.at(i) - origin;
rep(i, N) pos.at(i) = pos.at(i) * rad;
sort(all(pos));
return pos;
}
int main() {
int N;
cin >> N;
vector<Gi> p(N), q(N);
rep(i, N) p.at(i) = inC();
rep(i, N) q.at(i) = inC();
if (N == 1) {
cout << YES << endl;
return 0;
}
rep(i, N) {
rep(j, N) {
if (i == j) continue;
if ((p.at(1) - p.at(0)).norm() != (q.at(j) - q.at(i)).norm()) continue;
vector<Gi> xa = cnv(p, p.at(0), q.at(j) - q.at(i), N);
vector<Gi> xb = cnv(q, q.at(i), p.at(1) - p.at(0), N);
if (xa == xb) {
cout << YES << endl;
return 0;
}
}
}
cout << NO << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct Vec {
ll x, y;
Vec(ll _x = 0, ll _y = 0) : x(_x), y(_y) {}
Vec operator-(const Vec &o) const {
return {x - o.x, y - o.y};
}
Vec operator+(const Vec &o) const {
return {x + o.x, y + o.y};
}
Vec operator*(const ll k) const {
return {x * k, y * k};
}
ll operator^(const Vec &o) const {
return x * o.y - o.x * y;
}
ll operator*(const Vec &o) const {
return x * o.x + y * o.y;
}
bool operator==(const Vec &o) const {
return x == o.x and y == o.y;
}
bool operator!=(const Vec &o) const {
return !operator==(o);
}
bool upper() const {
return x > 0 or (x == 0 and y >= 0);
}
int quad() const {
if(x > 0 && y >=0) return 0;
if(x <=0 && y > 0) return 1;
if(x < 0 && y <=0) return 2;
return 3;
}
bool operator<(const Vec &o) const {
if(quad() != o.quad()) return quad() < o.quad();
auto res = operator^(o);
if(res) return res > 0;
return (*this) * (*this) < o * o;
}
};
vector<int> prefix_function(const auto &s) {
int n = (int)s.size(); vector<int> b(n+1);
b[0] = -1; int i = 0, j = -1;
while(i < n) {
while(j >= 0 && s[i] != s[j]) j = b[j];
b[++i] = ++j;
}
return b;
}
bool kmp(const auto &text, const auto &p) {
vector<int> b = prefix_function(p);
int m = (int)p.size();
int j = 0;
for(auto c : text) {
while(j >= 0 && c != p[j]) j = b[j];
j++;
if(j == m) {
return true;
}
}
return false;
}
auto read(int n) {
vector<Vec> pt(n);
Vec center;
for(Vec &p : pt) {
scanf("%lld %lld", &p.x, &p.y);
center = center + p;
}
for(Vec &p : pt) p = p * n - center;
for(int i = 0; i < (int)pt.size(); i++) {
while(i < (int)pt.size() and pt[i] == Vec()) {
swap(pt[i], pt.back());
pt.pop_back();
}
}
for(Vec p : pt) assert(p != Vec());
sort(pt.begin(), pt.end());
n = (int)pt.size();
vector<tuple<ll, ll, ll>> vec; vec.reserve(n);
for(int i = 0; i < n; i++) {
ll cross = pt[i] ^ pt[(i + 1) % n];
ll dot = pt[i] * pt[(i + 1) % n];
ll size = pt[i] * pt[i];
vec.push_back({cross, dot, 0});
}
return vec;
}
int main() {
int n;
scanf("%d", &n);
if(n == 1) return printf("Yes\n"), 0;
auto s = read(n);
auto t = read(n);
if(s.size() != t.size()) return printf("No\n"), 0;
for(int i = 0; i < (int)t.size(); i++) s.push_back(s[i]);
printf("%s\n", kmp(s, t) ? "Yes" : "No");
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.