code_file1
stringlengths 87
4k
| code_file2
stringlengths 82
4k
|
---|---|
#include <bits/stdc++.h>
using namespace std;
#define reps(i, a, n) for (int i = (a); i < (n); ++i)
#define rep(i, n) reps(i, 0, n)
#define deps(i, a, n) for (int i = (a); i >= (n); --i)
#define dep(i, n) deps(i, n, 0)
#define inf 2147483647
#define int long long
signed main(void)
{
int x, y, tmp; cin >> x >> y;
if (x > y) {tmp = x; x = y; y = tmp;}
x += 3;
if (x > y) cout << "Yes" << endl;
else cout << "No" << endl;
}
| #include<bits/stdc++.h>
using namespace std;
signed main(){
int x, y;
cin >> x >> y;
if(abs(x - y) < 3)
cout << "Yes\n";
else
cout << "No\n";
} |
#include<bits/stdc++.h>
using namespace std;
int main(){
int N;
cin>>N;
string A,B;
cin>>A>>B;
int cnt1=0;
for(int i=0;i<N;i++){
if(A[i]=='1')cnt1++;
if(B[i]=='1')cnt1--;
}
if(cnt1!=0){
cout<<"-1\n";
return 0;
}
long long ans=0;
for(int i=0,cnt1move=0,cntA1=0,cntA0=0,lastA1=-1;i<N;i++){
if(B[i]=='1'&&A[i]=='0'){
if(lastA1<i-1){
lastA1=i-1;
}
if(cntA1<i){
cntA1=i;
}
while(A[cntA1]=='0')cntA1++;
ans+=cntA1-lastA1-1;
lastA1=cntA1;
swap(A[i],A[cntA1]);
cntA1++;
}else if(B[i]=='0'&&A[i]=='1'){
if(cntA0<i){
cntA0=i;
}
ans++;
while(A[cntA0]=='1')cntA0++;
swap(A[i],A[cntA0]);
cntA0++;
}
}
cout<<ans<<'\n';
} | #include<stdio.h>
#include<iostream>
#include<vector>
#include<math.h>
#include<queue>
#include<map>
#include<algorithm>
#include<string.h>
#include<functional>
#include<limits.h>
#include<stdlib.h>
#include<string>
#include<unordered_map>
#include <iomanip>
using namespace std;
#define intmax INT_MAX
#define lmax LONG_MAX
#define uintmax UINT_MAX
#define ulmax ULONG_MAX
#define llmax LLONG_MAX
#define ll long long
#define rep(i,a,N) for((i)=(a);(i)<(N);(i)++)
#define rrp(i,N,a) for((i)=(N)-1;(i)>=(a);(i)--)
#define llfor ll i,j,k
#define sc(a) cin>>a
#define pr(a) cout<<a<<endl
#define pY puts("YES")
#define pN puts("NO")
#define py puts("Yes")
#define pn puts("No")
#define pnn printf("\n")
#define sort(a) sort(a.begin(),a.end())
#define reverse(a) reverse(a.begin(),a.end())
#define push(a,b) (a).push_back(b)
#define llvec vector<vector<ll>>
#define charvec vector<vector<char>>
#define sizeoof(a,b) (a,vector<ll>(b))
#define llpvec vector<pair<ll,ll>>
/*繰り上げ除算*/ll cei(ll x,ll y){ll ans=x/y;if(x%y!=0)ans++;return ans;}
/*最大公約数*/ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
/*最小公倍数*/ll lcm(ll x,ll y){return x/gcd(x,y)*y;}
/*n乗*/ll llpow(ll x,ll n){ll i,ans=1;rep(i,0,n)ans*=x;return ans;}
/*階乗*/ll fact(ll x){ll i,ans=1;rep(i,0,x)ans*=(x-i);return ans;}
/*nCr*/ll ncr(ll n,ll r){return fact(n)/fact(r)/fact(n-r);}
/*nPr*/ll npr(ll n,ll r){return fact(n)/fact(n-r);}
/*primejudge*/bool prime(ll a){if(a<=1)return false;ll i;for(i=2;i*i<=a;i++){if(a%i==0)return false;}return true;}
ll ans=0;llfor;///////////////////////////////////////////////////////////
int main(){
int N;
vector<int> V(2510);
int count=0;
for(int i=1;i<10000;i++){
if(i%6==0 || i%10==0 || i%15==0){
V[count]=i;
count++;
}
}
cin>>N;
for(int i=1;i<N+1;i++) cout<<V[i]<<" ";
return 0;}
|
#include <bits/stdc++.h>
using namespace std;
const int c=5002;
long long n, m, sum, mod=998244353;
long long po[c][c];
int main()
{
ios_base::sync_with_stdio(false);
cin >> n >> m;
for (int i=0; i<=m; i++) {
po[i][0]=1;
}
for (int i=0; i<=m; i++) {
for (int j=1; j<=n; j++) {
po[i][j]=po[i][j-1]*i%mod;
}
}
sum=po[m][n]*n%mod;
for (int i=1; i<n; i++) {
for (int j=1; j<=m; j++) {
sum-=(n-i)*po[m][n-i-1]%mod*po[m-j][i-1]%mod;
}
}
sum%=mod;
if (sum<0) {
sum+=mod;
}
cout << sum << "\n";
return 0;
}
| #include <bits/stdc++.h>
#define fi first
#define se second
#define DB double
#define U unsigned
#define P std::pair
#define LL long long
#define LD long double
#define pb emplace_back
#define MP std::make_pair
#define SZ(x) ((int)x.size())
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define FOR(i,a,b) for(int i = a;i <= b;++i)
#define ROF(i,a,b) for(int i = a;i >= b;--i)
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl
const int MAXN = 5000+5;
const int ha = 998244353;
int n,m;
inline void add(int &x,int y){
x += y-ha;x += x>>31&ha;
}
int pw[MAXN],pw1[MAXN];
int main(){
scanf("%d%d",&n,&m);
if(m == 1){
puts("1");
return 0;
}
pw[0] = 1;FOR(i,1,MAXN-1) pw[i] = 1ll*pw[i-1]*m%ha;
pw1[0] = 1;FOR(i,1,MAXN-1) pw1[i] = 1ll*pw1[i-1]*(m-1)%ha;
int ans = 0;
FOR(x,2,m){
int c = 1;
FOR(k,2,n-1){
c = 1ll*c*(m-x)%ha;
int d = (pw1[k-1]+ha-c)%ha;
add(ans,1ll*d*(n-k)%ha*pw[n-(k+1)]%ha);
}
}
FOR(i,1,n) add(ans,1ll*pw1[i-1]*pw[n-i+1]%ha);
printf("%d\n",ans);
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
using LL = long long;
constexpr int maxn = 200000 + 1;
void no(){
cout << -1;
exit(0);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int N, M;
cin >> N >> M;
if(M < 0) no();
if(M == 0){
for(int i = 1; i <= N; i += 1) cout << i * 10 << " " << i * 10 + 1 << "\n";
return 0;
}
if(M > N - 2) no();
for(int i = 1; i < N; i += 1) cout << i * 10 << " " << i * 10 + 1 << "\n";
cout << "1 " << (M + 1) * 10 + 2 << "\n";
return 0;
} | #include<bits/stdc++.h>
#define pb emplace_back
#define AI(i) begin(i), end(i)
using namespace std;
using ll = long long;
#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
const int MAX_N = 300010;
int n, m;
int l[MAX_N], r[MAX_N];
int tak() {
vector<pair<int,int>> a;
for (int i = 1;i <= n;++i)
a.pb(l[i], r[i]);
sort(AI(a), [&](auto x, auto y) {
return x.second < y.second;
});
int len = 0;
int cur = -1;
for (auto [l, r] : a)
if (l > cur) ++len, cur = r;
return len;
}
int aok() {
vector<pair<int,int>> a;
for (int i = 1;i <= n;++i)
a.pb(l[i], r[i]);
sort(AI(a));
int len = 0;
int cur = -1;
for (auto [l, r] : a)
if (l > cur) ++len, cur = r;
return len;
}
void construct(int n, int m) {
for (int i = 1;i <= n;++i)
l[i] = 4 * i+1, r[i] = l[i] + 2;
r[1] = r[m+2]+1;
for (int i = 1;i <= n;++i) {
cout << l[i] << ' ' << r[i] << '\n';
assert(l[i] < r[i]);
assert(l[i] >= 1 && r[i] <= 1000000000);
}
set<int> v;
for (int i = 1;i <= n;++i)
v.insert(l[i]), v.insert(r[i]);
assert(v.size() == n*2);
assert(tak() - aok() == m);
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
if (n == 1 && m == 0) {
cout << 1 << ' ' << 2 << '\n';
return 0;
}
if (m < 0 || m > n-2)
return puts("-1"), 0;
construct(n, m);
}
|
#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n); i++)
#define length size()
#define int long long
#define ll long long
const int MOD = 1000000007;
const int MAX = 510000;
template<typename T> string join(vector<T> &vec ,const string &sp){
int si = vec.length;
if(si==0){
return "";
}else{
stringstream ss;
rep(i,si-1){
ss << vec[i] << sp;
}
ss << vec[si - 1];
return ss.str();
}
}
signed main(void){
int h,w;
cin >> h >> w;
vector<vector<char>> vec(h+1,vector<char>(w+1,'#'));
int sum = 0;
rep(i,h){
rep(j,w){
cin >> vec[i][j];
}
}
rep(i,h){
rep(j,w){
if(vec[i][j]=='.'){
if(vec[i][j+1]=='.') sum++;
if(vec[i+1][j]=='.') sum++;
}
}
}
cout << sum << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define int long long int
#define mod 1000000007
#define vi vector<int>
#define pint pair<int,int>
#define vint vector<vector<int>>
#define IOS ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
const int N = 1e6+5;
int A[N], B[N], P[N], Q[N][3], D[N][3];
string S[105];
int mat[5005][5005];
pint edges[N];
unordered_map<int, set<int>> m;
bool compare(vi a, vi b)
{
return a[0] < b[0];
}
int lcm(int a, int b)
{
return (a*b)/(__gcd(a,b));
}
int mypow(int a, int x)
{
if(a<=1 || x==1) return a;
if(x==0) return 1;
int temp = mypow(a,x/2);
if(x%2==0) return ((temp%mod) * (temp%mod))%mod;
else return (a%mod * ((temp%mod) * (temp%mod))%mod)%mod;
}
int count_bits(int n)
{
int cnt = 0;
while(n)
{
n = n & (n-1);
cnt++;
}return cnt;
}
void bfs(int src, int n)
{
unordered_map<int, bool> visited;
for(int i=0;i<n;i++) visited[i] = false;
queue<int> q;
q.push(src);
visited[src] = true;
while(!q.empty())
{
int curr = q.front();
q.pop();
B[curr] -= min(B[curr], A[src]);
//cout<<curr<<" "<<endl;
for(auto neigh: m[curr])
{
if(visited[neigh] == false)
{
//cout<<neigh<<endl;
q.push(neigh);
visited[neigh] = true;
m[src].erase(neigh);
m[neigh].erase(src);
}
}
}
}
int length_of_lis(int n)
{
if(n<=1) return n;
vector<int> res;
res.push_back(A[0]);
int i;
for(i=1;i<n;i++)
{
if(A[i] > res.back()) res.push_back(A[i]);
else
{
int index = lower_bound(res.begin(), res.end(), A[i]) - res.begin();
res[index] = A[i];
}
}return res.size();
}
int solve(int h, int w)
{
int i,j,k;
int ans = 0;
for(i=0;i<h;i++)
{
for(j=0;j<w-1;j++)
{
if(S[i][j] == '.' && S[i][j+1] == '.')
ans++;
}
}
for(i=0;i<w;i++)
{
for(j=0;j<h-1;j++)
{
if(S[j][i] == '.' && S[j+1][i] == '.')
ans++;
}
}
return ans;
}
int32_t main()
{
//code
IOS;
int t,n,m,a,b,x,y,i,j,z;
//cin>>t;
t = 1;
while(t--)
{
int h,w;
cin>>h>>w;
for(i=0;i<h;i++)
cin>>S[i];
cout<<solve(h,w)<<endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
// type alias
typedef long long LL;
typedef pair<int,int> II;
typedef tuple<int,int,int> III;
typedef vector<int> VI;
typedef vector<string> VS;
typedef unordered_map<int,int> MAPII;
typedef unordered_set<int> SETI;
template<class T> using VV=vector<vector<T>>;
// minmax
template<class T> inline T SMIN(T& a, const T b) { return a=(a>b)?b:a; }
template<class T> inline T SMAX(T& a, const T b) { return a=(a<b)?b:a; }
// repetition
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)
#define REPE(i,n) for(int i=0;i<=(n);++i)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) for(int i=0;i<(n);++i)
#define FORR(x,arr) for(auto& x:arr)
#define SZ(a) int((a).size())
// collection
#define ALL(c) (c).begin(),(c).end()
// DP
#define MINUS(dp) memset(dp, -1, sizeof(dp))
#define ZERO(dp) memset(dp, 0, sizeof(dp))
// stdout
#define println(args...) fprintf(stdout, ##args),putchar('\n');
// debug cerr
template<class Iter> void __kumaerrc(Iter begin, Iter end) { for(; begin!=end; ++begin) { cerr<<*begin<<','; } cerr<<endl; }
void __kumaerr(istream_iterator<string> it) { (void)it; cerr<<endl; }
template<typename T, typename... Args> void __kumaerr(istream_iterator<string> it, T a, Args... args) { cerr<<*it<<"="<<a<<", ",__kumaerr(++it, args...); }
template<typename S, typename T> std::ostream& operator<<(std::ostream& _os, const std::pair<S,T>& _p) { return _os<<"{"<<_p.first<<','<<_p.second<<"}"; }
//#define __KUMATRACE__ true
#ifdef __KUMATRACE__
#define dump(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); __kumaerr(_it, args); }
#define dumpc(ar) { cerr<<#ar<<": "; FORR(x,(ar)) { cerr << x << ','; } cerr << endl; }
#define dumpC(beg,end) { cerr<<"~"<<#end<<": "; __kumaerrc(beg,end); }
#else
#define dump(args...)
#define dumpc(ar)
#define dumpC(beg,end)
#endif
// $ cp-batch GameInMomotetsuWorld | diff GameInMomotetsuWorld.out -
// $ g++ -std=c++14 -Wall -O2 -D_GLIBCXX_DEBUG -fsanitize=address GameInMomotetsuWorld.cpp && ./a.out
/*
5/16/2021
11:16-12:13
*/
const int MAX_N=2000+10;
string A[MAX_N];
int N,M;
const int Inf=1e8;
II memo[MAX_N][MAX_N];
II f(int i, int j) {
//if(i>=N) return (j<M-1?make_pair(-Inf,-Inf):make_pair(0,0));
//if(j>=M) return (i<N-1?make_pair(-Inf,-Inf):make_pair(0,0));
//if(i>=N||j>=M) return {-Inf,-Inf};
//if(i==N-1&&j==M-1) return {0,0};
if(memo[i][j].first!=-Inf) return memo[i][j];
II &res=memo[i][j];
vector<II> xs;
REP(di,2)REP(dj,2)if(di+dj==1) {
int ii=i+di,jj=j+dj;
if(ii>=N||jj>=M) continue;
auto x=f(ii,jj);
swap(x.first,x.second);
int d=(A[ii][jj]=='+')?1:-1;
x.first+=d;
xs.push_back(x);
}
if(SZ(xs)==0) {
assert(i==N-1&&j==M-1);
return {0,0};
}
if(SZ(xs)==1) return res=xs[0];
if(xs[0].first-xs[0].second>xs[1].first-xs[1].second) {
res=xs[0];
} else {
res=xs[1];
}
/*
if(xs[0].first!=xs[1].first) {
res=xs[0].first>xs[1].first?xs[0]:xs[1];
} else {
res=xs[0].second<xs[1].second?xs[0]:xs[1];
}*/
dump(i,j,res.first,res.second);
return res;
}
void solve() {
REP(i,N)REP(j,M) memo[i][j]={-Inf,-Inf};
II res=f(0,0);
dump(res.first, res.second);
string ans="Draw";
if(res.first>res.second) ans="Takahashi";
else if(res.first<res.second) ans="Aoki";
cout<<ans<<endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout<<setprecision(12)<<fixed;
cin>>N>>M;
REP(i,N) cin>>A[i];
solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define d(x) cout<<x<<" "
#define nl cout<<endl
#define rep(var,init_val,final_val) for(int var = init_val; var < final_val; ++var)
#define tt ll t;cin>>t;while(t--)
#define show(a,b) rep(i,0,b) d(a[i]);nl;
#define vi std::vector<int>
#define pi pair<int,int>
#define mod 1000000007
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll n , y;
cin>>n>>y;
ll maxp,minp,maxn,minn;
ll ans = 0;
if(n>0){
maxp=(n+(y-2)/2);
minp=(n-y/2);
maxn=-(n-(y-1)/2);
minn=-(n+(y-1)/2);
if(maxn>=minp) ans = maxp-minn+1;
else ans = maxp-minp+maxn-minn+2;
}
else if(n<0){
maxp=-(n-(y-1)/2);
minp=-(n+(y-1)/2);
minn=(n-y/2);
maxn=(n+(y-2)/2);
if(maxn>=minp) ans = maxp-minn+1;
else ans = maxp-minp+maxn-minn+2;
}
else{
maxp=(y-1)/2;
minp=-y/2;
ans=maxp-minp+1;
}
cout<<ans<<endl;
return 0;
} |
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long
#define ull unsigned long long
#define cint const int&
#define Pi acos(-1)
const int mod = 1e9+7;
const int inf_int = 0x7fffffff;
const ll inf_ll = 0x7fffffffffffffff;
const double ept = 1e-9;
int h, w;
int bcj[2][1010];
vector<int> row[1001], col[1001];
int fd(cint x, bool st) {
return bcj[st][x] == x ? x : bcj[st][x] = fd(bcj[st][x], st);
}
int main() {
int s = -1, ss = -1;
cin >> h >> w;
char a;
for(int i=1; i<=h; i++) bcj[0][i] = i;
for(int i=1; i<=w; i++) bcj[1][i] = i;
bcj[1][w] = 1; bcj[0][h] = 1;
for(int i=1; i<=h; i++)
for(int j=1; j<=w; j++) {
cin >> a;
if(a == '#') {
row[i].push_back(j);
col[j].push_back(i);
if(i == 1 || i == h) bcj[1][fd(bcj[1][j], 1)] = fd(bcj[1][1], 1);
if(j == 1 || j == w) bcj[0][fd(bcj[0][i], 0)] = fd(bcj[0][1], 0);
}
}
int a1, a2;
for(int i=1; i<=h; i++) {
for(int j=0; j<(int)row[i].size()-1; j++) {
a1 = row[i][j], a2 = row[i][j+1];
bcj[1][fd(bcj[1][a1], 1)] = fd(bcj[1][a2], 1);
}
}
for(int i=1; i<=w; i++) {
for(int j=0; j<(int)col[i].size()-1; j++) {
// cout << j << '-' << (int)col[i].size() << ' ' << i << endl;
a1 = col[i][j], a2 = col[i][j+1];
bcj[0][fd(bcj[0][a1], 0)] = fd(bcj[0][a2], 0);
}
}
int tmp = 0;
for(int i=1; i<=w; i++)
if(bcj[1][i] == i) ++tmp;
int tmp1 = 0;
for(int i=1; i<=h; i++)
if(bcj[0][i] == i) ++tmp1;
// cout << tmp << ' ' << tmp1 << endl;
cout << min(tmp1, tmp) - 1 << endl;
return 0;
}
/*
5 9
.........
.#######.
.........
.........
.........
*/ | #pragma GCC optimize ("O2")
#pragma GCC target ("avx2")
#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please
const int SZ = 2010;
class UF {
public:
int P[SZ + 1];
UF() : P() {
rep1(i, SZ) P[i] = -1;
}
int find(int A) {
if (P[A] < 0) return A;
return P[A] = find(P[A]);
}
bool unite(int A, int B) {
int a = find(A);
int b = find(B);
if (a == b) return false;
if (P[a] > P[b]) swap(a, b);
P[a] += P[b];
P[b] = a;
return true;
}
} uf;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int H, W;
cin >> H >> W;
uf.unite(1, 1001);
uf.unite(1, W + 1000);
uf.unite(H, 1001);
uf.unite(H, W + 1000);
rep1(i, H) {
string S;
cin >> S;
rep(j, W) {
if (S[j] == '#') {
uf.unite(i, j + 1001);
}
}
}
int kotae = 1000;
unordered_set<int> ST;
rep1(i, H) {
ST.insert(uf.find(i));
}
kotae = ST.size() - 1;
ST.clear();
rep1(j, W) {
ST.insert(uf.find(j + 1000));
}
chmin(kotae, int(ST.size()) - 1);
co(kotae);
Would you please return 0;
} |
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using P=pair<ll,ll>;
template<class T> using V=vector<T>;
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
const ll inf=(1e18);
//const ll mod=998244353;
const ll mod=1000000007;
const vector<int> dy={-1,0,1,0},dx={0,-1,0,1};
ll GCD(ll a,ll b) {return b ? GCD(b,a%b):a;}
ll LCM(ll c,ll d){return c/GCD(c,d)*d;}
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(20);}} __init;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<class T>void debag(const vector<T> &a){cerr<<"debag :";for(auto v:a)cerr<<v<<" ";cerr<<"\n";}
template<class T>void print(const vector<T> &a){for(auto v:a)cout<<v<<" ";cout<<"\n";}
struct mint{
using ull=unsigned long long int;
ull v;
mint(ll vv=0){s(vv%mod+mod);}
mint& s(ull vv){
v=vv<mod?vv:vv-mod;
return *this;
}
//オーバーロード
mint operator-()const{return mint()-*this;}//mint型にキャスト
mint&operator+=(const mint&val){return s(v+val.v);}
mint&operator-=(const mint&val){return s(v+mod-val.v);}
mint&operator*=(const mint&val){
v=ull(v)*val.v%mod;
return *this;
}
mint&operator/=(const mint&val){return *this*=val.inv();}
mint operator+(const mint&val){return mint(*this)+=val;}
mint operator-(const mint&val){return mint(*this)-=val;}
mint operator*(const mint&val){return mint(*this)*=val;}
mint operator/(const mint&val){return mint(*this)/=val;}
mint pow(ll n)const{
mint res(1),x(*this);
while(n){
if(n&1)res*=x;
x*=x;
n>>=1ll;
}
return res;
}
mint inv()const{return pow(mod-2);}
//拡張ユークリッドの互除法
/* mint inv()const{
int x,y;
int g=extgcd(v,mod,x,y);
assert(g==1);
if(x<0)x+=mod;
return mint(x);
}*/
friend ostream& operator<<(ostream&os,const mint&val){
return os<<val.v;
}//出力
bool operator<(const mint&val)const{return v<val.v;}
bool operator==(const mint&val)const{return v==val.v;}
bool operator>(const mint&val)const{return v>val.v;}
};
const ll MAX = 2000010;//設定
mint fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void init(){
fac[0] = fac[1] = 1;
for(int i=1;i<MAX;i++)fac[i]=fac[i-1]*i;
finv[MAX-1]=fac[MAX-1].inv();
for(int i=MAX-2;i>=0;i--)finv[i]=finv[i+1]*(i+1);
for(int i=MAX-2;i>=1;i--)inv[i]=finv[i]+fac[i-1];
}
//階乗
mint factor(ll n,ll k){
if (n<k) return 0;
if (n<0 || k<0) return 0;
return fac[n]*finv[k];
}
// 二項係数計算
mint COM(int n, int k){
if (n < k) return 0;
if (n < 0 || k < 0) return 0;
return fac[n] * finv[k] * finv[n - k];
}
mint dp[200005][17][17]={};
int main(){
init();
string s;
cin>>s;
int k;
cin>>k;
int n=s.size();
V<int> a(n);
map<char,int> mp;
for(char c='0';c<='9';c++){
mp[c]=int(c-'0');
}
for(char c='A';c<='F';c++){
mp[c]=int(c-'A')+10;
}
for(int i=0;i<n;i++){
a[i]=mp[s[i]];
}
for(int i=1;i<=k;i++)dp[0][i][0]=mint(1); //すでにi個決まっている
for(int i=0;i<n;i++){
for(int j=0;j<=k;j++){
for(int r=0;r<=k;r++){
dp[i+1][j][r+1]+=dp[i][j][r]*mint(16-j-r);
dp[i+1][j][r]+=dp[i][j][r]*mint(j+r);
}
}
}
ll v=0;
mint ans=mint(0);
for(int i=0;i<n;i++){
if(__builtin_popcount(v)<=k){
for(int j=(i==0?1:0);j<a[i];j++){
ll c=(v|(1ll<<j));
int num=__builtin_popcount(c);
if(num>k)continue;
ans+=dp[n-i-1][num][k-num];
}
}
if(i!=0){
ans+=dp[n-i-1][1][k-1]*mint(15);
}
v|=(1ll<<a[i]);
}
set<int> st;
for(int i=0;i<n;i++)st.insert(a[i]);
if(int(st.size())==k)ans+=mint(1);
cout<<ans<<"\n";
} | #include<bits/stdc++.h>
#include<set>
using namespace std;
int main(){
int N,M;
cin >> N >> M;
int A[N];
vector<int> candidate,cnt(N+1);
for(int i = 0;i < N;i++){
cin >> A[i];
}
set<int> x;
for(int i = 0;i <= N;i++){
x.insert(i);
}
for(int i = 0;i < M;i++){
x.erase(A[i]);
cnt.at(A[i])++;
}
for(int i = 0;i < N - M;i++){
candidate.push_back(*x.begin());
cnt.at(A[i])--;
cnt.at(A[i+M])++;
if(cnt.at(A[i]) == 0) x.insert(A[i]);
x.erase(A[i+M]);
}
candidate.push_back(*x.begin());
sort(candidate.begin(),candidate.end());
cout << candidate.at(0) << endl;
} |
#include <bits/stdc++.h>
#define arr array
#define pb push_back
#define fs first
#define sc second
#define eb emplace_back
#define vt vector
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
using namespace std;
typedef long long ll;
const int maxn=2e5;
const int MAXINT=1e9;
const ll MAXLL=1e18;
void solve(){
int n;
cin >> n;
n*=2;
vt<arr<int, 3>> a(n);
rep(i, 0, n) cin >> a[i][0], a[i][1]=i;
sort(a.begin(), a.end(), [&](arr<int, 3> x, arr<int, 3> y){
return x[0]<y[0];
});
rep(i, 0, n/2) a[i][2]=1;
rep(i, n/2, n) a[i][2]=0;
sort(a.begin(), a.end(), [&](arr<int, 3> x, arr<int, 3> y){
return x[1]<y[1];
});
//rep(i, 0, n) cout << a[i][2] << " "; cout << endl;
vt<int> st;
vt<char> ans(n);
rep(i, 0, n){
if (!st.empty()&&a[i][2]!=a[st.back()][2]) ans[st.back()]='(', ans[i]=')', st.pop_back();
else st.pb(i);
}
rep(i, 0, n) cout << ans[i]; cout << endl;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t=1;
while(t--){
solve();
}
} | #include<bits/stdc++.h>
#define For(i,a,b) for(register int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(register int i=(a);i>=(b);--i)
#define int long long
using namespace std;
inline int read()
{
char c=getchar();int x=0;bool f=0;
for(;!isdigit(c);c=getchar())f^=!(c^45);
for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
if(f)x=-x;return x;
}
#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;
#define mod 998244353
struct modint{
int x;
modint(int o=0){x=o;}
modint &operator = (int o){return x=o,*this;}
modint &operator +=(modint o){return x=x+o.x>=mod?x+o.x-mod:x+o.x,*this;}
modint &operator -=(modint o){return x=x-o.x<0?x-o.x+mod:x-o.x,*this;}
modint &operator *=(modint o){return x=1ll*x*o.x%mod,*this;}
modint &operator ^=(int b){
modint a=*this,c=1;
for(;b;b>>=1,a*=a)if(b&1)c*=a;
return x=c.x,*this;
}
modint &operator /=(modint o){return *this *=o^=mod-2;}
modint &operator +=(int o){return x=x+o>=mod?x+o-mod:x+o,*this;}
modint &operator -=(int o){return x=x-o<0?x-o+mod:x-o,*this;}
modint &operator *=(int o){return x=1ll*x*o%mod,*this;}
modint &operator /=(int o){return *this *= ((modint(o))^=mod-2);}
template<class I>friend modint operator +(modint a,I b){return a+=b;}
template<class I>friend modint operator -(modint a,I b){return a-=b;}
template<class I>friend modint operator *(modint a,I b){return a*=b;}
template<class I>friend modint operator /(modint a,I b){return a/=b;}
friend modint operator ^(modint a,int b){return a^=b;}
friend bool operator ==(modint a,int b){return a.x==b;}
friend bool operator !=(modint a,int b){return a.x!=b;}
bool operator ! () {return !x;}
modint operator - () {return x?mod-x:0;}
};
#define maxn 500005
int n,a[maxn],id[maxn],col[maxn];
bool cmp(int x,int y){
return a[x]<a[y];
}
queue< pii >q;
bool vis[maxn];
int pre[maxn],nxt[maxn];
char s[maxn];
void print(){
For(i,1,n)if(!vis[i])cout<<a[i]<<' ';
}
signed main()
{
n=read()*2;
For(i,1,n)a[i]=read(),id[i]=i;
For(i,1,n-1)nxt[i]=i+1;
For(i,2,n)pre[i]=i-1;
sort(id+1,id+n+1,cmp);
For(i,n/2+1,n)col[id[i]]=1;
For(i,1,n-1)
if(col[i]!=col[i+1])q.push(mkp(i,i+1));
int orz=0;
while(!q.empty()){
pii t=q.front();q.pop();
int u=t.fi,v=t.se;
if(vis[u]||vis[v])continue;
vis[u]=vis[v]=1;
// ++orz,cout<<orz<<" "<<u<<' '<<v<<endl;
// print();
s[u]='(',s[v]=')';
int uu=pre[u],vv=nxt[v];
if(uu&&vv&&col[uu]!=col[vv]) q.push(mkp(uu,vv));
nxt[pre[u]]=nxt[v];
pre[nxt[v]]=pre[u];
pre[u]=pre[v]=nxt[u]=nxt[v]=0;
}
For(i,1,n)if(!s[i])return -1;
cout<<s+1;
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef long long ll; typedef pair<int,int>pint; typedef long double ld;
using vll=vector<ll>; using vs=vector<string>;
#define b2e(v)v.begin(),v.end()
#define p2a(v, p) v[p.first][p.second]
class var { public: ll min,max,ct,sum,dif,result; };
int main() {
ll n;
cin>>n;
vector<pint> v;
const int INF=0x3f3f3f3f;
const ll INFL=0x3f3f3f3f3f3f3f3f;
pint lmax={INF,INF},idx={0,0};
rep(i,n) {
ll a,b;
cin >> a >> b;
if (lmax.first > a) { lmax.first=a; idx.first=i; }
if (lmax.second > b) { lmax.second=b; idx.second=i; }
v.push_back({a,b});
}
pint lmax2={INF,INF};
rep(i,n) {
ll a=v[i].first,b=v[i].second;
if (idx.first!=i && lmax2.first > a) { lmax2.first=a; }
if (idx.second!=i && lmax2.second > b) { lmax2.second=b; }
}
ll result = min(max(lmax.first,lmax2.second),max(lmax2.first,lmax.second));
if (idx.first!=idx.second) {
result = max(lmax.first, lmax.second);
} else {
result = min(lmax.first+lmax.second, min(max(lmax.first,lmax2.second), max(lmax2.first,lmax.second)));
}
cout << result << endl;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vpii> vvpii;
typedef vector<vpll> vvpll;
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define fi first
#define se second
template<class T> bool ckmin(T &a, const T &b) {return a > b ? a = b, 1 : 0;}
template<class T> bool ckmax(T &a, const T &b) {return a < b ? a = b, 1 : 0;}
namespace debug {
void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for(auto z : x) cerr << (f++ ? "," : ""), __print(z); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if(sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef ljuba
#define dbg(x...) cerr << "LINE(" << __LINE__ << ") -> " << "[" << #x << "] = ["; _print(x)
#else
#define dbg(x...)
#endif
}
using namespace debug;
const char nl = '\n';
void solve() {
int n;
cin >> n;
vpii v(n);
for(auto &[a, b] : v)
cin >> a >> b;
int ans = 1e9;
for(auto [a, b] : v) {
ckmin(ans, a + b);
}
for(int i = 0; i < n; ++i) {
for(int j = 0; j < n; ++j) {
if(i == j)
continue;
ckmin(ans, max(v[i].fi, v[j].se));
}
}
cout << ans << nl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifdef ljuba
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
int testCases = 1;
// cin >> testCases;
while(testCases--)
solve();
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define ln cout<<'\n'
void pr(){ln;}
template<class A,class...B>void pr(const A &a,const B&...b){cout<<a<<' ';pr(b...);}
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;}
bool temp[2010];
vector<vector<int>> to;
void dfs(int a) {
if (temp[a]) return;
temp[a] = true;
for (auto v:to[a]) dfs(v);
}
void solve() {
int n , m; cin >> n >> m;
to.resize(n);
rep(i, m) {
int a, b; cin >> a >> b;
to[a-1].push_back(b-1);
}
int ans = 0;
rep(i, n) {
rep(i, n) temp[i] = false;
dfs(i);
rep(i, n) if (temp[i]) ++ans;
}
cout << ans << endl;
}
int main(){
ios_base::sync_with_stdio(false), cin.tie(nullptr);
solve();
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
int n,m;
int vdnsddglkdk;
cin>>n>>m;
vector<pair<int,int>>a(m);
for(int i=0;i<m;i++)
{
cin>>a[i].first>>a[i].second;
}
int k;
cin>>k;
vector<pair<int,int>>b(k);
for(int j=0;j<k;j++)
{
cin>>b[j].first>>b[j].second;
}
ll l=0;
ll tot=1<<k;
for(ll mask=0;mask<tot;mask++)
{
vector<int>v(n+1);
ll ans=0;
for(int i=0;i<k;i++)
{
if(mask & (1<<i))
v[b[i].second]=1;
else
v[b[i].first]=1;
}
for(int i=0;i<m;i++)
{
if(v[a[i].first] && v[a[i].second])
ans++;
}
l=max(l,ans);
}
cout<<l<<endl;
return 0;
} |
/*
AUTHOR:SOURABH
CREATED:10:10:20
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ordered_set tree<ll , null_type, less< ll >, rb_tree_tag, tree_order_statistics_node_update>
#define ll long long int
#define ld long double
#define fi first
#define se second
#define pb push_back
#define pob pop_back
#define W while
#define fn(i,x) for(i=0;i<x;i++)
#define fs(i,s,x) for(i=s;i<x;i++)
#define fr(i,x) for(i=x;i>=0;i--)
#define fit(it,s) for(it=s.begin();it!=s.end();it++)
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vii vector<int>
#define vll vector<ll>
#define mii map<int,int>
#define mll map<ll,ll>
#define mod 998244353
#define MAXN 10000001
#define M 32
using namespace std;
using namespace __gnu_pbds;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
//freopen("input.txt","r",stdin);
//freopen("out.txt","w",stdout);
ll t;
char s,tt;
t=1;
//cin>>t;
W(t--)
{
cin>>s>>tt;
if(s=='Y')
cout<<char(tt-32);
else
cout<<tt;
}
return 0;
}
| #pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define deb(x) cout << #x << " " << x << endl;
#define mod 1000000007
#define fast std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
#define endl "\n"
#define all(x) (x).begin(), (x).end()
#define rall(v) v.rbegin(), v.rend()
const ll INF = 1e18;
//const ll NEGINF = -1 * INF;
ll gcd(ll a, ll b)
{
if (b == 0)
{
return a;
}
return gcd(b, a % b);
}
ll my_pow(ll a, ll n, ll m = INF)
{
ll res = 1;
while (n)
{
if (n % 2)
{
res = (res * a) % m;
n--;
}
a = (a * a) % m;
n /= 2;
}
return res;
}
#define double long double
const int N = 2e5 + 5;
//g(i) = a*x+b;
double a[N];
double b[N];
bool bad[N];
void solve()
{
ll n, m, k;
cin >> n >> m >> k;
ll pt;
for (int i = 0; i < k; i++)
{
cin >> pt;
bad[pt] = true;
}
double c = 0;
double v = 0;
double sum = 0;
for (int i = n - 1; i >= 0; i--)
{
if (bad[i])
{
a[i] = 1.0;
b[i] = 0;
}
else
{
a[i] = (double)c / m;
b[i] = (double)v / m + 1;
}
sum += bad[i] - bad[i + m];
if (sum == m)
{
cout << -1 << endl;
return;
}
c += a[i] - a[i + m];
v += b[i] - b[i + m];
}
cout << fixed << setprecision(10) << b[0] / (1 - a[0]) << endl;
}
int main()
{
fast;
#ifndef ONLINE_JUDGE
freopen("/home/kalit/Desktop/Data Structures-Algo-Competitive/src/codeforces/input.txt", "r", stdin);
freopen("/home/kalit/Desktop/Data Structures-Algo-Competitive/src/codeforces/output.txt", "w", stdout);
#endif
int T = 1;
while (T--)
{
solve();
}
//cout<< "Done in " << clock() / CLOCKS_PER_SEC <<"sec"<< endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define _GLIBCXX_DEBUG
typedef long long int ll;
typedef long double ld;
const int inf = 1e8;
const ll llinf = 1e18;
const double PI = acos(-1);
#define yes "Yes"
#define no "No"
#define all(n) n.begin(), n.end()
template <class T>
void chmin(T &a, T b) {
if (a > b) a = b;
}
template <class T>
void chmax(T &a, T b) {
if (a < b) a = b;
}
struct UnionFind {
vector<int> par, siz;
UnionFind(int n) : par(n, -1), siz(n, 1) {}
int root(int x) {
if (par[x] == -1) return x;
return par[x] = root(par[x]);
}
bool isSame(int x, int y) { return root(x) == root(y); }
bool unite(int x, int y) {
x = root(x), y = root(y);
if (x == y) return false;
if (siz[x] < siz[y]) swap(x, y);
par[y] = x;
siz[x] += siz[y];
return true;
}
int size(int x) { return siz[root(x)]; }
};
using Graph = vector<vector<int>>;
vector<int> visited(false);
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
// const int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
// const int dy[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
void dfs(const Graph &G, int v) {
visited[v] = true;
for (auto next : G[v]) {
if (visited[next]) continue;
dfs(G, next);
}
}
bool isPrime(ll n) {
if (n < 2) return false;
for (ll i = 2; i * i <= n; ++i)
if (n % i == 0) return false;
return true;
}
// 最大公約数
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
// 最小公倍数
ll lcm(ll a, ll b) { return abs(a) / gcd(a, b) * abs(b); }
ll gcd2(const vector<ll> &v) {
ll val = v[0];
for (ll i = 1; i < v.size(); ++i) val = gcd(val, v[i]);
return val;
}
ll lcm2(const vector<ll> &v) {
ll val = v[0];
for (ll i = 1; i < v.size(); ++i) val = lcm(val, v[i]);
return val;
}
vector<ll> divisors(ll n) {
vector<ll> a;
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
a.push_back(i);
if (n / i != i) a.push_back(n / i);
}
}
sort(a.begin(), a.end());
return a;
}
// pairを要素に持つvectorをsecondを基準にソートする比較関数
bool comp(pair<ll, ll> a, pair<ll, ll> b) { return a.second < b.second; }
vector<int> eratosthenes(int n) {
vector<bool> is_prime(n + 1, true);
vector<int> p;
for (int i = 2; i <= n; ++i) {
if (is_prime[i]) {
for (int j = i * 2; j <= n; j += i) {
is_prime[j] = false;
}
p.push_back(i);
}
}
return p;
}
int main() {
double n, d, h;
cin >> n >> d >> h;
double ans = 0;
rep(i, n) {
double dn, hn;
cin >> dn >> hn;
double a = (h - hn) / (d - dn);
double b = h - a * d;
ans = max(ans, b);
}
cout << fixed << setprecision(9) << ans << endl;
}
| #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;
using ll = long long int;
using iPair = pair<int,int>;
using lPair = pair<ll, ll>;
using ivector = vector<int>;
using lvector = vector<ll>;
using istack = stack<int>;
using iqueue = queue<int>;
using ivv = vector<vector<int>>;
using lvv = vector<vector<ll>>;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
vector<iPair> dir = {{1,0}, {-1,0}, {0,1}, {0,-1}};
#define dump(x) cout << #x << " = " << (x) << endl
#define ALL(x) begin(x),end(x)
#define rep(i,s,e) for(ll i=(s); i<(ll)(e); ++i)
#define repRev(i,s,e) for(ll i=(e-1); i>=(ll)(s); --i)
#define range(i,s,n) for(ll i=(s), i_stop=(s)+(n); i<i_stop; ++i)
#define rangeRev(i,s,n) for(ll i=(s), i_stop=(s)-(n); i>i_stop; --i)
#define foreach(x,container) for(auto &&x:container)
bool chmax(ll& a, ll b) {if(a<b) {a=b;return true;} return false;}
bool chmin(ll& a, ll b) {if(a>b) {a=b;return true;} return false;}
template<typename T> void printArr(vector<T> &arr){
for(auto &&x:arr) {cout << x << " ";} cout << endl;
}
/*
## 關鍵詞:
二分,二維前綴和,中值
## 題意:
給定長寬爲N的正方形,求所有K*K的正方形區域中中值的最小值。
1 <= K <= N <= 800。
中值定義: K*K方塊中,第 (K*K/2)+1 大的數值。
## 思路:
題解:https://atcoder.jp/contests/abc203/editorial/1955
將問題轉化爲求「對於任意X,判斷是否存在一個K*K區域使得該區域的中值小於等於X」。
然後二分去尋找滿足條件的最小X。
「存在一個K*K區域使得該區域的中值小於等於X」
等價於
「存在一個K*K區域,其中大於X的方塊數量小於(K*K/2)+1」,證明很簡單。
判斷某個X是否滿足條件時,花費O(N^2)生成前綴和,然後可以在O(1)判斷K*K中大於X的方塊數量。
總的復雜度是O(max(A_i)N^2)
## 雜多:
*/
ll n,k;
ll A[810][810];
ll s[810][810];
bool check(ll x) {
range(i, 0, n) range(j, 0, n) {
s[i+1][j+1] = s[i+1][j] + s[i][j+1] - s[i][j];
if(A[i][j] > x) ++s[i+1][j+1];
}
ll thres = k*k/2+1;
for(int i=0; i+k<=n; ++i) for(int j=0; j+k<=n; ++j) {
ll cnt = s[i+k][j+k];
cnt -= s[i+k][j] + s[i][j+k];
cnt += s[i][j];
if(cnt < thres) return true;
}
return false;
}
void solve(){
cin>>n>>k;
range(i, 0, n) range(j, 0, n) cin>>A[i][j];
range(i, 0, k) {
s[i][0] = 0;
s[0][i] = 0;
}
ll l = 0, r = 1000000001;
while(l<r) {
ll mid = l + (r-l) / 2;
if(!check(mid)) l = mid+1;
else r = mid;
}
cout << l << endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
void solve(){
ll n;
cin>>n;
if(n==2) {
cout<<1;
return;
}
ll i=1LL;
while(((i*(i+1))/2)<=(n+1))
i++;
ll res= n-(i-1)+1;
cout<<res;
}
int main() {
solve();
// your code goes here
return 0;
} | #include <bits/stdc++.h>
#define rep(i,n) for (int i=0;i<(n);i++)
#define rep1(i,n) for (int i=1;i<(n);i++)
using namespace std;
using ll = long long;
using P = pair<int, ll>;
int main() {
int n;
cin >> n;
int mx = 1, th = 2;
rep(i,n){
if(i+1 == th){
mx++;
th *= 2;
}
cout << mx << " ";
}
cout << endl;
return 0;
} |
#include<cstdio>
#include<algorithm>
typedef long long ll;
ll ans[2000005];
inline ll read() {
register ll x=0,f=1;register char s=getchar();
while(s>'9'||s<'0') {if(s=='-') f=-1;s=getchar();}
while(s>='0'&&s<='9') {x=x*10+s-'0';s=getchar();}
return x*f;
}
int main() {
ll n=read(); int cnt=0;
for(register ll i=1;i*i<=n;++i) {
if(n%i==0) {
ans[++cnt]=i;
if(i!=n/i) ans[++cnt]=n/i;
}
}
std::sort(ans+1,ans+1+cnt);
for(register int i=1;i<=cnt;++i) printf("%lld\n",ans[i]);
return 0;
} | #include <bits/stdc++.h>
#define M 1000000007
#define fio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define d(x) cout << #x << " " << x << "\n";
#define ll long long int
#define ul unsigned long long int
#define p pair<int, int>
#define ld long double
#define inf INT_MAX
using namespace std;
int main()
{
ld n;
cin >> n;
ld ans = 0.0;
for (ld i = n - 1; i >= 1; --i)
{
ans += (n / i);
}
cout << setprecision(9)<<ans;
return 0;
} |
#include <iostream>
using namespace std;
typedef long long ll;
int main () {
int x;
cin >> x;
cout << ((x>0)?x:0) << endl;
return 0;
} | #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
#include <set>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
int main(){
int x;
cin >> x;
if(x >= 0)cout << x << "\n";
else cout << 0 << "\n";
}
|
// Keep it simple
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define Br cout<<endl
#define int long long
#define fe first
#define se second
#define double long double
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define PT(x) cout << (x) << endl
#define last(x) prev(x.end())
#define PP(x) cout << (x) << " "
#define PS(x) for(auto it: x)cout << it <<" "; cout << endl;
#define PTT(x) cout << (#x) << " is " << (x) << endl
#define lets_go ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
void PV(vi v) { for (int i = 0 ; i < (int)v.size() ; i++) cout << v[i] << " " ; cout << endl;}
void PVV(vii v) {for (int i = 0 ; i < (int)v.size() ; i++) cout << v[i].first << " " << v[i].second << endl;}
void PA(int v[], int n, int x = 0) { for (int i = x ; i < n + x; i++)cout << v[i] << ' ' ; cout << endl;}
void IN(int a[], int n, int x = 0) {for (int i = x; i < n + x; i++)cin >> a[i];}
inline void op() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
// <=================================================================================================================================
// <=================================================================================================================================
const int M = 1e9 + 7;
const int IM = 1e18 + 37;
const int N = 2e5 + 10;
signed main() {
lets_go
op();
int n;
cin >> n;
set<int> s = {10, 6, 15};
for (int i = 10; i <= 10000; i += 10) {
if (s.size() == n)
break ;
s.insert(i);
}
for (int i = 6; i <= 10000; i += 6) {
if (s.size() == n)
break ;
s.insert(i);
}
for (int i = 15; i <= 10000; i += 15) {
if (s.size() == n)
break ;
s.insert(i);
}
PS(s);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
void solve() {
string s;
cin >> s;
int ans = 0;
for (int i = 0; i <= 9999; ++i) {
set<char> have;
int t = i;
for (int j = 0; j < 4; ++j) {
have.insert(t % 10);
t /= 10;
}
int res = 1;
for (int j = 0; j < s.size(); ++j) {
if ((s[j] == 'o' && !have.count(j)) || (s[j] == 'x' && have.count(j))) {
res = 0;
break;
}
}
ans += res;
}
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define END '\n'
#define int long long
#define pb push_back
#define pii pair<int, int>
#define ff first
#define ss second
#define bug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define loop(i, a, b) for(int i = (a); i < (b); i++)
#define loopb(i, b, a) for(int i = (b); i > (a); --i)
const int inf = 1e18;
const int mod = 1e9+7;
const int mod1 = 998244353;
const int nax = 200005;
struct road
{
int v, t, k;
};
int n, m, a, b, x, y, t, k;
vector<road> adj[nax];
int dp[nax];
void calc()
{
priority_queue<pii> q;
q.push({0, x});
while(!q.empty())
{
int time = -q.top().ff;
int node = q.top().ss;
q.pop();
for(auto x : adj[node])
{
int div = (time + x.k - 1)/x.k;
int tot = div*x.k + x.t;
if(dp[x.v] > tot)
{
dp[x.v] = tot;
q.push({-dp[x.v], x.v});
}
}
}
}
void solve()
{
cin>>n>>m>>x>>y;
for(int i = 1; i <= n; i++)
dp[i] = inf;
dp[x] = 0;
for(int i = 1; i <= m; i++)
{
cin>>a>>b>>t>>k;
road A;
A.v = b;
A.t = t;
A.k = k;
adj[a].pb(A);
A.v = a;
adj[b].pb(A);
}
calc();
if(dp[y] == inf)
dp[y] = -1;
cout<<dp[y];
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int t = 1;
//cin>>t;
while(t--)
solve();
// cerr<<END<<1.0*clock()/CLOCKS_PER_SEC;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
const ll mod = 10e8 + 7;
const ll inf = (1LL << 60);
vector<ll> x(20), y(20), z(20);
ll c(int i, int j) {
return abs(x[i] - x[j]) + abs(y[i] - y[j]) + max(0LL,z[j] - z[i]);
}
int main(){
int n; cin >> n;
rep(i,n) cin >> x[i] >> y[i] >> z[i];
vector<vector<pair<ll,int>>> dp((1<<n),vector<pair<ll,int>>(n,make_pair(inf,-1)));
rep(i,n) dp[(1<<i)][i] = make_pair(c(i,0),i);
rep(i,(1<<n)){
bitset<20> s(i);
rep(j,n){
if(s.test(j)) continue;
rep(k,n){
if(!s.test(k)) continue;
ll x, y, z, w;
tie(x,y) = dp[i|(1<<j)][j];
tie(z,w) = dp[i][k];
if(x > z + c(j,k)){
dp[i|(1<<j)][j] = make_pair(z + c(j,k),w);
}
}
}
}
cout << dp[(1<<n)-1][0].first << endl;
} |
#include<bits/stdc++.h>
//#include <atcoder/all>
#define INF 1e9
#define rep(i,n)for(long long i=0;(i)<(long long)(n);i++)
#define REP(i,a,b)for(long long i=(long long)(a);(i)<=(long long)(b);i++)
#define ALL(a) (a).begin(),(a).end()
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define pb push_back
using namespace std;
//using namespace atcoder;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
using Graph = vector<vector<int>>;
//using mint = modint1000000007;
const ll ZER = 0;
const ll MOD = 1e9 + 7;
map<ll, ll> memo;
ll x, y;
// y -> x
ll rec(ll n){
if(memo.find(n) != memo.end())return memo[n];
if(n <= x)return x - n;
ll res = n - x;
chmin(res, rec(n / 2) + (n % 2) + 1);
if(n % 2 != 0)chmin(res, rec(n / 2 + 1) + (2 - (n % 2)) + 1);
return memo[n] = res;
}
int main(){
cin >> x >> y;
cout << rec(y) << endl;
} | #include "bits/stdc++.h"
#define rep(i,b) for(ll i=0;i<b;i++)
#define ll long long
using namespace std;
/*--Input//////////////////////////////////////////////////*/
inline void IN(void){return;}
template <typename First, typename... Rest>
void IN(First& first, Rest&... rest){
cin >> first;
IN(rest...);
return;
}
#define SS(T, ...) T __VA_ARGS__; IN(__VA_ARGS__);
#define SV(type,c,n) vector<type> c(n);for(auto& i:c)cin >> i;
/*--Output/////////////////////////////////////////////////*/
#define Yes(n) cout << ((n) ? "Yes" : "No" ) << endl
inline int p(void){cout << endl; return 0;}
template<class Head> int p(Head&& head){cout << head;p();return 0;} template<class Head,class... Tail> int p(Head&& head,Tail&&... tail){cout<<head<<" ";p(forward<Tail>(tail)...);return 0;}
struct ProconInit {
static constexpr int IOS_PREC = 15;
static constexpr bool AUTOFLUSH = false;
ProconInit() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(IOS_PREC);
if(AUTOFLUSH)
cout << unitbuf;
}
} PROCON_INIT;
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vvvl vector<vector<vector<ll>>>
#define debug(x) cerr<<#x<<" "<<x<<'\n';
#define all(x) (x).begin(),(x).end()
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
bool cmp(pair<ll,ll> a, pair<ll,ll> b) { return a.second < b.second; }
bool P[200001];
int main() {
ll now =0;
SS(ll,n);
rep(i,n){
SS(ll,po);
P[po]=true;
for(ll j=now;;j++){
if(P[j]==false){
now=j;
p(now);
break;
}
}
}
}
|
#pragma GCC optimize ("O2")
#pragma GCC target ("avx2")
//#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please
const int MAX = 10010;
class shuturyoku_unko {
public:
char C[MAX * 6];
constexpr shuturyoku_unko() : C() {
rep(i, MAX) {
int X = i;
rep(j, 5) {
C[i * 6 + 4 - j] = '0' + X % 10;
X /= 10;
}
C[i * 6 + 5] = ' ';
}
}
};
constexpr shuturyoku_unko f;
char T[800], *ci = T, c, *di;
int N, A[110], B[110];
bool furagu[10001];
inline void keisan(int s) {
di = T;
rep(i, N + 1) {
int tmp = A[i] + (B[i] > s);
int tmp2;
if (tmp >= 1000) {
if (tmp >= 10000) tmp2 = 6;
else tmp2 = 5;
}
else {
if (tmp >= 100) tmp2 = 4;
else if (tmp >= 10) tmp2 = 3;
else tmp2 = 2;
}
memcpy(di, f.C + tmp * 6 + 6 - tmp2, tmp2);
di += tmp2;
}
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
fread(T, 800, 1, stdin);
while ((c = *ci++) >= '0') N = N * 10 + c - '0';
ci += N + 1;
rep(i, N + 1) {
while ((c = *ci++) >= '0') A[i] = A[i] * 10 + c - '0';
}
int saishou = 1e9;
rep(i, N) if (saishou > abs(A[i] - A[i + 1])) saishou = abs(A[i] - A[i + 1]);
rep(i, N + 1) {
B[i] = A[i] % saishou;
A[i] = A[i] / saishou;
furagu[B[i]] = 1;
}
printf("%d\n", saishou);
keisan(0);
rep(s, saishou) {
if (furagu[s]) keisan(s);
fwrite(T, 1, di - T, stdout);
}
Would you please return 0;
} | // author : ytczy666
// 21.6.29 10:09
#include <bits/stdc++.h>
const int N = 110;
#define inf 0x3f3f3f3f
int n, a[N], b[N * N][N];
char str[N];
int main() {
scanf("%d", &n);
scanf("%s", str + 1);
for (int i = 0; i <= n; i ++) scanf("%d", &a[i]);
int k = inf;
for (int i = 1; i <= n; i ++) k = std::min(k, abs(a[i] - a[i - 1]));
printf("%d\n", k);
for (int j = 0; j <= n; j ++) {
int d = a[j] / k, m = a[j] % k;
for (int i = 1; i <= k; i ++) {
if (i <= m) b[i][j] = d + 1;
else b[i][j] = d;
}
}
for (int i = 1; i <= k; i ++, puts(""))
for (int j = 0; j <= n; j ++)
printf("%d ", b[i][j]);
return 0;
}
// 考虑最优答案最多是k=min{d_i},再想是否一定可以构造出这样一组方案。
// 我们直接就考虑平均分配,为什么这么想,因为我们要让各部分都满足整体的关系。
// 而因为k=min{d_i},所以一定不会出现相邻两个数相等的情况,否则就把前几个数+1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
cout << 100*n*(n+1)*k/2+n*k*(k+1)/2 << endl;
} | #include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
#define MAXN 100005
#define lowbit(x) (x&-x)
#define reg register
typedef long long LL;
typedef unsigned long long uLL;
typedef unsigned int uint;
typedef pair<int,int> pii;
const LL INF=0x7f7f7f7f7f7f;
const int mo=998244353;
const double PI=acos(-1.0);
template<typename _T>
_T Fabs(_T x){return x<0?-x:x;}
template<typename _T>
void read(_T &x){
_T f=1;x=0;char s=getchar();
while(s>'9'||s<'0'){if(s=='-')f=-1;s=getchar();}
while('0'<=s&&s<='9'){x=(x<<3)+(x<<1)+(s^48);s=getchar();}
x*=f;
}
int n,d,h;double maxx;
signed main(){
read(n);read(d);read(h);
for(int i=1;i<=n;i++){
int x,y;read(x);read(y);
double tmp=1.0*y-1.0*(x)/(d-x)*(h-y);
maxx=max(maxx,tmp);
}
printf("%.3f\n",maxx);
return 0;
}
|
// E - Unique Color
#include <bits/stdc++.h>
using namespace std;
#define vec vector
using vi = vec<int>;
#define rep(i,e) for(int i=0;i<(e);++i)
int n;
vi C, ans;
vec<vi> G;
map<int,int> cc;
void dfs(int v){
if(ans[v] > 0) return;
cc[C[v]]++;
ans[v] = cc[C[v]];
for(auto&u:G[v]) dfs(u);
cc[C[v]]--;
}
int main(){
cin>>n;
C.resize(n);for(auto&c:C) cin>>c;
G.resize(n);
rep(i, n-1){
int a, b; cin>>a>>b; --a, --b;
G[a].push_back(b);
G[b].push_back(a);
}
ans.resize(n, -1);
dfs(0);
rep(i, n) if(ans[i] == 1) cout<< i+1 <<endl;
}
| // Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools
#include <bits/stdc++.h>
// #include "atcoder/all"
using namespace std;
using i64 = long long;
const i64 MOD = 1e9 + 7;
const i64 INF = i64(1e18) + 7;
template <typename T>
bool chmin(T& x, T y){
if(x > y){
x = y;
return true;
}
return false;
}
template <typename T>
bool chmax(T& x, T y){
if(x < y){
x = y;
return true;
}
return false;
}
template <int B = 20>
struct Mex{
vector<vector<int>> v;
vector<int> cnt;
Mex() : v(B), cnt(1 << B, 0){
for(int i = 0; i < B; ++i){
int siz = 1 << (B - i - 1);
v[i].resize(siz);
for(int j = 0; j < siz; ++j){
v[i][j] = j << i;
}
}
}
void inc(int x){
if(cnt[x]++ == 0){
v[0][x] = (1 << (B - 1));
for(int i = 1; i < B; ++i){
v[i][x >> i] = min(v[i - 1][(x >> i) * 2], v[i - 1][(x >> i) * 2 + 1]);
}
}
}
void dec(int x){
if(--cnt[x] == 0){
v[0][x] = x;
for(int i = 1; i < B; ++i){
v[i][x >> i] = min(v[i - 1][(x >> i) * 2], v[i - 1][(x >> i) * 2 + 1]);
}
}
}
int get(){
return v[B - 1][0];
}
};
void solve(long long n, long long m, std::vector<long long> a){
int ans = n;
Mex<22> d;
for(int i = 0; i < n; ++i){
d.inc(a[i]);
if(i >= m){
d.dec(a[i - m]);
}
if(i >= m - 1){
ans = min(ans, d.get());
}
}
cout << ans << endl;
}
signed main(){
long long N;
scanf("%lld",&N);
long long M;
scanf("%lld",&M);
std::vector<long long> A(N);
for(int i = 0 ; i < N ; i++){
scanf("%lld",&A[i]);
}
solve(N, M, std::move(A));
return 0;
}
|
//In The Name Of Allah
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define F first
#define S second
//#pragma GCC optimize("Ofast")
const int maxn = 1e5 + 5;
ll x, ans = 1e18 + 5;
map<ll, int> ca;
void calc(ll y)
{
queue<pair<ll, int> > q;
q.push({y, 0});
while(q.size())
{
auto b = q.front();
q.pop();
int t = b.S;
if(ca.count(b.F)&& ca[b.F] < t)
continue;
ca[b.F] = t;
y = b.F;
ans = min(ans, abs(y - x) + t);
if(y % 2 == 0)
q.push({y / 2, t + 1});
else
{
q.push({y / 2, t + 2});
q.push({y / 2 + 1, t + 2});
}
}
/*if(ca.count(y))
return;
ca.insert(y);
ans = min(ans, abs(y - x) + t);
if(y == 0)
return;
if(y % 2 == 0)
calc(y / 2, t + 1);
else
{
calc(y / 2, t + 2);
calc(y / 2 + 1, t + 2);
}*/
}
int main()
{
//ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
ll y;
cin >> x >> y;
if(x >= y)
{
cout << x - y << '\n';
return 0;
}
else
{
calc(y);
cout << ans << '\n';
}
}
| #include<bits/stdc++.h>
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define sc(a) scanf("%c",&a)
#define ss(a) scanf("%s",a)
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define pc(a) putchar(a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i, a, b) for(register int i=a;i<=b;++i)
#define repd(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pii pair<int,int>
#define mii unordered_map<int,int>
#define msi unordered_map<string,int>
#define lowbit(x) ((x)&(-(x)))
#define ce(i, r) i==r?'\n':' '
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define INF 0x3f3f3f3f
#define pr(x) cout<<#x<<": "<<x<<endl
using namespace std;
inline int qr() {
int f = 0, fu = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')fu = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
f = (f << 3) + (f << 1) + c - 48;
c = getchar();
}
return f * fu;
}
const int N = 1e3 + 10;
int n, m;
int a[N], b[N];
int f[N][N];
int main() {
n = qr(), m = qr();
repi(i, 1, n)a[i] = qr();
repi(i, 1, m)b[i] = qr();
memset(f, 0x3f, sizeof(f));
f[0][0] = 0;
int ans = INF;
repi(i, 0, m)f[0][i] = i;
repi(i, 0, n)f[i][0] = i;
repi(i, 1, n)
repi(j, 1, m) {
if (a[i] != b[j]) {
f[i][j] = min({f[i][j], f[i - 1][j - 1] + 1, f[i][j - 1] + 1, f[i - 1][j] + 1});
} else {
f[i][j] = min({f[i][j], f[i - 1][j - 1], f[i][j - 1] + 1, f[i - 1][j] + 1});
}
}
repi(i, 1, n)repi(j, 1, m)ans = min(ans, f[i][j] + n - i + m - j);
pi(ans);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;
#define rep(i,n) for(int i=0;i<int(n);i++)
#define rep1(i,n) for(int i=1;i<=int(n);i++)
#define per(i,n) for(int i=int(n)-1;i>=0;i--)
#define per1(i,n) for(int i=int(n);i>0;i--)
#define all(c) c.begin(),c.end()
#define si(x) int(x.size())
#define pb emplace_back
#define fs first
#define sc second
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
template<class T,class U> void chmax(T& x, U y){if(x<y) x=y;}
template<class T,class U> void chmin(T& x, U y){if(y<x) x=y;}
template<class T> void mkuni(V<T>& v){sort(all(v));v.erase(unique(all(v)),v.end());}
template<class T> int lwb(const V<T>& v, const T& a){return lower_bound(all(v),a) - v.begin();}
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
o<<"{";
for(const T& v:vc) o<<v<<",";
o<<"}";
return o;
}
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }
#ifdef LOCAL
#define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
void dmpr(ostream& os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
os<<t<<" ~ ";
dmpr(os,args...);
}
#define shows(...) cerr << "LINE" << __LINE__ << " : ";dmpr(cerr,##__VA_ARGS__)
#define dump(x) cerr << "LINE" << __LINE__ << " : " << #x << " = {"; \
for(auto v: x) cerr << v << ","; cerr << "}" << endl;
#else
#define show(x) void(0)
#define dump(x) void(0)
#define shows(...) void(0)
#endif
struct UnionFind{
vector<int> par,rank,hasL,hasR,marked;
UnionFind(int N){
par.assign(N,0);
rank.assign(N,0);
hasL.resize(N);hasR.resize(N);marked.resize(N);
rep(i,N) par[i]=i;
}
int find(int x){
if(par[x]==x) return x;
return par[x]=find(par[x]);
}
bool same(int x,int y){
return find(x)==find(y);
}
void unite(int x,int y){
x=find(x),y=find(y);
if(x==y) return;
if(rank[x]<rank[y]) swap(x,y);
//x becomes root
par[y]=x;
if(rank[x]==rank[y]) rank[x]++;
hasL[x] += hasL[y];
hasR[x] += hasR[y];
marked[x] += marked[y];
}
};
int main(){
cin.tie(0);
ios::sync_with_stdio(false); //DON'T USE scanf/printf/puts !!
cout << fixed << setprecision(20);
int H,W; cin >> H >> W;
V<string> s(H); rep(i,H) cin >> s[i];
UnionFind UF(H+W);
rep(i,H) UF.hasL[i] = 1;
rep(i,W) UF.hasR[i+H] = 1;
UF.marked[0] = true;
rep(i,H) rep(j,W) if(s[i][j] == '#'){
UF.unite(i,j+H);
}
for(int i:{0,H-1}) for(int j:{0,W-1}) UF.unite(i,j+H);
int ans = 1e9;
{
int waf = 0;
rep(v,H+W) if(UF.find(v) == v && UF.hasL[v] && !UF.marked[v]) waf++;
chmin(ans,waf);
}
{
int waf = 0;
rep(v,H+W) if(UF.find(v) == v && UF.hasR[v] && !UF.marked[v]) waf++;
chmin(ans,waf);
}
cout << ans << endl;
}
| //starusc
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
inline int read(){
int x=0,f=1,c=getchar();
while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
while(isdigit(c)){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
return f==1?x:-x;
}
const int M=1004,N=1e6+4;
struct edge{
int v,nxt;
}e[N<<2];
int first[N],cnt;
inline void addedge(int u,int v){
e[++cnt].v=v;e[cnt].nxt=first[u];first[u]=cnt;
}
struct node{
int x,y,id;
}a[N];
inline bool comp_x(const node &a,const node &b){
return a.x<b.x||(a.x==b.x&&a.y<b.y);
}
inline bool comp_y(const node &a,const node &b){
return a.y<b.y||(a.y==b.y&&a.x<b.x);
}
inline bool comp_id(const node &a,const node &b){
return a.id<b.id;
}
int n,m,tot,cx,cy,rx,ry,vx[M],vy[M],vis[N];
char s[M];
void dfs(int x){
vis[x]=1;
if(!vx[a[x].x])cx++;
if(!vy[a[x].y])cy++;
vx[a[x].x]=vy[a[x].y]=1;
for(int i=first[x];i;i=e[i].nxt)
if(!vis[e[i].v])
dfs(e[i].v);
}
int main(){
n=read();m=read();
for(int i=1;i<=n;i++){
scanf("%s",s+1);
for(int j=1;j<=m;j++){
if(s[j]=='#'){
a[++tot].x=i;a[tot].y=j;a[tot].id=tot;
}
}
}
sort(a+1,a+tot+1,comp_x);
for(int i=1;i<=tot;i++){
if(a[i].x==a[i-1].x){
addedge(a[i].id,a[i-1].id);
addedge(a[i-1].id,a[i].id);
}
}
sort(a+1,a+tot+1,comp_y);
for(int i=1;i<=tot;i++){
if(a[i].y==a[i-1].y){
addedge(a[i].id,a[i-1].id);
addedge(a[i-1].id,a[i].id);
}
}
rx=n-2;ry=m-2;
vx[1]=vx[n]=vy[1]=vy[m]=1;
sort(a+1,a+tot+1,comp_id);
for(int i=1;i<=tot;i++)if(!vis[i]&&(a[i].x==1||a[i].y==1||a[i].x==n||a[i].y==m)){
cx=cy=0;
dfs(i);
rx-=cx;ry-=cy;
}
for(int i=1;i<=tot;i++)if(!vis[i]){
cx=cy=0;
dfs(i);
rx-=cx-1;ry-=cy-1;
}
cout<<min(rx,ry)<<"\n";
return (0-0);
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const long long INF = 1LL<<60;
const double PI = acos(-1.0);
/*const double PI = atan2(0.0,-1.0)*/
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(ll i = 0; i < (ll)(n); i++)
#define rep1(i,aa,n) for(ll i = aa; i <= (ll)(n); i++)
#define ALL(a) (a).begin(),(a).end()
#define ar_init(aa,val) rep(i,aa.size()) aa[i]=val;
#define v2d(aa,val1,val2,ini) vector<vector<ll>> aa(val1,vector<ll>(val2,ini));
#define pqe priority_queue<ll>
#define pqeg priority_queue<ll,vector<ll>,greater<ll>>
#define lcin(...) ll __VA_ARGS__;CINT(__VA_ARGS__)
#define eout(...) COU(__VA_ARGS__);
#define sout(...) SCOU(__VA_ARGS__);
#define scin(...) string __VA_ARGS__;CINT(__VA_ARGS__)
#define lb(aa,val) lower_bound(ALL(aa),val)
#define pb push_back
#define mkp make_pair
#define si size()
void CINT(){}
template <class Head,class... Tail>
void CINT(Head&& head,Tail&&... tail){
cin>>head;
CINT(move(tail)...);
}
void COU(){}
template <class Head,class... Tail>
void COU(Head&& head,Tail&&... tail){
cout<<head<<endl;
COU(move(tail)...);
}
void SCOU(){}
template <class Head,class... Tail>
void SCOU(Head&& head,Tail&&... tail){
cout<<head<<" ";
SCOU(move(tail)...);
}
template <class T = ll>
T IN(){T x;cin>>x;return (x);}
using pii = pair<ll,ll>;
using v2 = vector<vector<ll>>;
using v1 = vector<ll>;
using v2p = vector<vector<pii>>;
using v1p = vector<pii>;
//ll mo=1000000007;
ll mo=998244353;
ll mod_pow(ll N,ll n){
if(n==0) return 1;
ll pp,hh;
pp=n/2;
hh=n%2;
int tt=mod_pow(N,n/2);
if(hh==0)return tt%mo*tt%mo;
else {return tt%mo*tt%mo*N%mo;}
}
ll pp[6000][6000];
int main(){
//cout<<fixed<<setprecision(15);
lcin(n,m);
rep1(i,1,m){
pp[i][0]=1;
rep1(k,1,n){
pp[i][k]=pp[i][k-1]*i;
pp[i][k]%=mo;
}
}
ll res=0;
rep1(i,1,m){
rep1(k,1,n){
if(k+2<=n){res+=pp[m][n-k-2]*(pp[m-i+1][k]-pp[m-i][k])%mo*max((ll)0,n-k+1-2)*(i-1)%mo*(i-1)%mo;
}
if(k!=n){res+=2*pp[m][max((ll)0,n-k-1)]*(pp[m-i+1][k]-pp[m-i][k])%mo*(i-1)%mo;
}
if(k==n){res+=pp[m-i+1][k]-pp[m-i][k];
res%=mo;
}
}
}
eout(res);
} | #define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(...) begin(__VA_ARGS__) , end(__VA_ARGS__)
#define boost {ios_base::sync_with_stdio(false); cin.tie(); cout.tie();}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector <int> vi;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
constexpr ll nax = 2e5+6969, INF = 2e9+6969, mod = 998244353;
ll Pow(ll a, ll b)
{
if(b < 0) return 0;
ll w = 1;
while(b)
{
if(b&1) w = (w * a) % mod;
a = (a * a) % mod;
b >>= 1;
}
return w;
}
int main()
{
ll n,m, ans = 0;
cin >> n >> m;
ll last = Pow(m, n-2);
ll inv = Pow(m, mod - 2);
for(ll x = 1; x <= m; x++)
{
ll A = last, B = 1;
for(ll d = 1; d < n; d++)
{
ll cur = (n - d) * A % mod * B % mod;
ans = (ans + cur) % mod;
// cout << "For d=" << d << ", x=" << x << " A,B,ans " << A << " " << B << " " << ans << "\n";
A = (A * inv) % mod;
B = (B * (m-x)) % mod;
}
}
ans = n * Pow(m, n) - ans;
ans %= mod;
if(ans < 0) ans += mod;
cout << ans << "\n";
return 0;
}
|
#include <bits/stdc++.h>
#define fir first
#define sec second
#define PB push_back
#define MP make_pair
#define all(a) (a).begin(), (a).end()
#define sz(a) (a).size()
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i < (int)(n); ++i)
#define foreach(itr, c) for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); ++itr)
template <class T> void chkmax(T& x, T y) { x = (x > y ? x : y); }
template <class T> void chkmin(T& x, T y) { x = (x < y ? x : y); }
typedef long long LL;
typedef pair <int, int> pii;
typedef vector <int> vi;
const int N = 1e5 + 5;
int n, x, a[N];
int main(){
scanf("%d%d", &n, &x);
rep1(i, n + 1){
scanf("%d", &a[i]);
if (a[i] != x) printf("%d ", a[i]);
}
printf("\n");
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long double ld;
const int N = (int) 2e5 + 7;
int n;
int m;
int k;
int x[N];
bool is[N];
ld dp[N];
ld algo(ld dp0) {
ld sum = 0;
for (int i = n - 1; i >= 0; i--) {
if (is[i]) {
dp[i] = dp0;
} else {
dp[i] = 1 + sum / (ld) m;
}
sum += dp[i];
sum -= dp[i + m];
}
return dp[0];
/// cout << dp0 << " " << dp[0] << "\n";
}
bool ok(ld dp0) {
return dp0 - algo(dp0) < 1e-14;
}
const ld INF = (ld) 1e12;
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
/// freopen ("input", "r", stdin);
cin >> n >> m >> k;
for (int i = 1; i <= k; i++) {
int x;
cin >> x;
is[x] = 1;
}
ld l = 0, r = INF;
if (ok(r)) {
cout << "-1\n";
return 0;
}
for (int i = 1; i <= 100; i++) {
ld m = (l + r) * 0.5;
if (ok(m)) {
l = m;
} else {
r = m;
}
}
cout << fixed << setprecision(3) << l << "\n";
return 0;
ld x = 0;
while (1) {
algo(x);
ld y = dp[0];
if (fabs(x - y) < 1e-14) {
break;
}
x = y;
}
cout << fixed << setprecision(6) << x << "\n";
}
|
#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define ll long long
#define ALL(a) (a).begin(),(a).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define Pii pair<int,int>
#define Pll pair<long long,long long>
#define fout(num) cout << fixed << setprecision(20) << (num) << endl
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
//vector<vector<ll>> dp(n,vector<ll>(n))
//2-dim:vector<vector<Type>> vv(n, vector<Type>(m, d));
//3-dim:vector<vector<vector<Type>>> vvv(n, vector<vector<Type>>(m, vector<Type>(l, d)));
using namespace std;
signed main()
{
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
string s; cin >> s;
ll M; cin >> M;
ll ma = -1;
rep(i,s.size()) chmax(ma,s[i]-'0');
if(s.size()==1){
if((s[0]-'0')<=M) cout << 1 << endl;
else cout << 0 << endl;
return 0;
}
ll ok = ma,ng=1e18*2;
while(ng-ok>1)
{
ll mid = (ok+ng)/2LL;
vector<ll> v;
ll m = M;
while(m>0){
v.pb(m % mid);
m /= mid;
}
reverse(ALL(v));
if(s.size()<v.size()) ok = mid;
else if(s.size()>v.size()) ng = mid;
else{
rep(i,s.size()){
if((ll)(s[i]-'0')<v[i]){ ok = mid; break;}
if((ll)(s[i]-'0')>v[i]){ ng = mid; break;}
}
if(ok!=mid&&ng!=mid) ok=mid;
}
}
cout << ok-ma << endl;
return 0;
}
// g++ main.cpp -o a.out && ./a.out
// 9223372036854775807 | #include <bits/stdc++.h>
using namespace std;
int N, Q;
const int maxn = 2e5 + 5;
typedef long long ll;
inline ll read()
{
ll x = 0;
int s = 1;
char c = getchar();
while (c < 48 || c > 57)
{
if (c == '-')
s = -1;
c = getchar();
}
while (c >= 48 && c <= 57)
{
x = (x << 3) + (x << 1) + c - '0';
c = getchar();
}
return x * s;
}
int main()
{
int N, Q;
N = read();
ll a, t;
ll l = -1e9, r = 1e9, b = 0;
for (int i = 0; i < N; i++)
{
a = read();
t = read();
if (t == 1)
{
l += a;
r += a;
b += a;
}
else if (t == 2)
{
l = max(l, a);
r = max(r, a);
}
else
{
l = min(l, a);
r = min(r, a);
}
}
Q = read();
while (Q--)
{
ll x = read() + b;
if (x < l)
cout << l;
else if (x > r)
cout << r;
else
cout << x;
cout << ' ';
}
} |
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int a, b, c;
cin >> a >> b >> c;
cout << a + b + c - min(min(a, b), c) << endl;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define int long long
#define ordered_set tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update>
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define ll long long
typedef unsigned long long ull;
#define f(i,a,n) for(int i=a;i<n;i++)
#define f1(i,a,n) for(int i=a;i<=n;i++)
#define fab(i,a,b) for(int i=a;i<=b;i++)
#define fr(i,a) for(int i=a;i>=0;i--)
#define sc second
#define ft first
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vpii;
#define mp make_pair
#define pb push_back
#define mod 1000000007
#define maxN 1000000
const int INF=1e18;
/*---------------------------------------------------------------------------------------------------------------------------*/
vector<ll> sieve(int n) {int*arr = new int[n + 1](); vector<ll> vect; for (int i = 2; i <= n; i++)if (arr[i] == 0) {vect.push_back(i); for (int j = 2 * i; j <= n; j += i)arr[j] = 1;} return vect;}
void google(int t) {cout << "Case #" << t << ": ";}
int mul_mod(int a,int b,int m){ a%m; b%m; return ((((a*b)%m)+m)%m);}
int mul_add(int a,int b,int m){ a%m; b%m; return ((((a+b)%m)+m)%m);}
int mul_sub(int a,int b,int m){ a%m; b%m; return ((((a-b)%m)+m)%m);}
bool cmp(pair<int,int> a,pair<int,int> b){if(a.ft+a.sc!=b.ft+b.sc) return a.ft+a.sc<b.ft+b.sc; return a.ft<b.ft;}
bool isPrime(int n){if(n==1 or n==0) return false; if(n==2 or n==3) return true; if(n%2==0 or n%3==0) return false; for(int i=5;i*i<=n;i+=6){if(n%i==0 or n%(i+2)==0)return false;}return true;}
int power(int base,int n,int modulo){int res=1; while(n){if(n&1)res=mul_mod(res,base,modulo); base=mul_mod(base,base,modulo);n>>1;} return res;}
int dig(int n){int cnt=0; while(n){cnt++; n>>1;} return cnt;}
bool isPalindrome(string s){int n=s.size(); int l=0,r=n-1; bool flag=true; while(l<r){ if(s[l]!=s[r]){flag=false;break;}l++;r--;}if(flag)return true; return false;}
/*---------------------------------------------------------------------------------------------------------------------------*/
void solve(){
int a,b,c;
cin>>a>>b>>c;
int ans=max((a+b),(b+c));
ans=max(ans,(a+c));
cout<<ans;
}
int32_t main(){
fastio();
int tc=1;
//scanf("%lld",&tc);
//cin>>tc;
//cin.get();
while (tc--)
{
solve();
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 7;
char s[N];
int main() {
cin >> (s + 1);
int n = strlen(s + 1);
ll ans = 0;
char last = '0';
int i;
for (i = 1; i <= n - 2; i++) {
if (s[i] == s[i + 1] && s[i + 2] != s[i] && last != s[i]) {
ans += 1ll*(n - (i + 2)) + 1;
// cout << ans << endl;
last = s[i];
i++;
} else {
if (s[i] == last) {
ans--;
}
}
}
for (; i <= n; i++) {
if (s[i] == last) {
ans--;
}
}
cout << ans << endl;
}
//a
| #include <bits/stdc++.h>
#define ll long long
ll md = 1000000007;
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define pb push_back
#define g(v, i, j) get<i>(v[j])
#define vi vector<int>
#define vll vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define srt(v) sort(v.begin(), v.end())
#define rsrt(v) sort(v.begin(), v.end(), greater<int>())
#define fi(i, a, b) for (int i = a; i < b; i++)
#define fll(i, a, b) for (ll i = a; i < b; i++)
using namespace std;
//using namespace std::chrono;
template <typename T>
T pw(T a, T b)
{
T c = 1, m = a;
while (b)
{
if (b & 1)
c = (c * m);
m = (m * m), b /= 2;
}
return c;
}
template <typename T>
T ceel(T a, T b)
{
if (a % b == 0)
return a / b;
else
return a / b + 1;
}
template <typename T>
T my_log(T n, T b)
{
T i = 1, ans = 0;
while (1)
{
if (i > n)
{
ans--;
break;
}
if (i == n)
break;
i *= b, ans++;
}
return ans;
}
template <typename T>
T gcd(T a, T b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
template <typename T>
T mysqt(T a)
{
T ans = 1;
while (ans * ans <= a)
ans++;
ans--;
return ans;
}
ll pwmd(ll a, ll b)
{
ll c = 1, m = a;
while (b)
{
if (b & 1)
c = (c * m) % md;
m = (m * m) % md;
b /= 2;
}
return c;
}
ll modinv(ll n)
{
return pwmd(n, md - 2);
}
ll inverse(ll i)
{
if (i == 1)
return 1;
return (md - ((md / i) * inverse(md % i)) % md + md) % md;
}
bool sortbysec(const pair<ll, ll> &a,
const pair<ll, ll> &b)
{
return (a.second < b.second);
}
/* auto start = high_resolution_clock::now();
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
cerr << "Time taken: "
<< duration.count()<< "ms" <<"\n";*/
vector<char> capl = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
vector<char> sml = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
template <typename T>
string conv(T a)
{
string b;
T e = 2;
T c = my_log(a, e);
c++, b = '1';
for (T i = 0; i < c - 1; i++)
{
T k = a >> (c - 2 - i);
if (k & 1)
b += '1';
else
b += '0';
}
if (a == 0)
return "0";
return b;
}
int main()
{
fast;
string s;
cin >> s;
ll n = s.length(), ans = 0;
vi cnt(26);
for (ll i = n - 1; i >= 1; i--){
if (s[i] == s[i - 1]){
char c = s[i];
ans += n - 1 - i - cnt[c - 'a'];
while (s[i] == c) i--, cnt[c - 'a']++;
i++;
fi(j, 0, 26){
if (j == c - 'a') cnt[j] = n - i;
else cnt[j] = 0;
}
continue;
}
cnt[s[i] - 'a']++;
}
cout << ans;
}
|
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <vector>
#include <queue>
#define lc u << 1
#define rc u << 1 | 1
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
LL n,s,k;
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 -= x * (a / b);
return d;
}
void solve()
{
scanf("%lld%lld%lld",&n,&s,&k);
LL x,y;
LL d = exgcd(k,n,x,y);
n /= d;
if(s % d) puts("-1");
else
{
printf("%lld\n",(-x * (s / d) % n + n) % n);
}
}
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#else
#endif // LOCAL
int T = 1;
scanf("%d",&T);
while(T --)
{
solve();
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll n, ll m){
if(n < m) swap(n, m);
if(n % m == 0) return m;
return gcd(m, n % m);
}
ll extgcd(ll a, ll b, ll& x, ll& y){
if (b == 0) {
x = 1;
y = 0;
return a;
}
ll gcd = extgcd(b, a % b, x, y);
ll oldX = x;
x = y;
y = oldX - a / b * y;
return gcd;
}
int main(){
int t;
cin >> t;
for(int loop = 0; loop < t; loop++){
ll n, s, k, x, y;
cin >> n >> s >> k;
ll g = gcd(n, k);
if(s % g != 0){
cout << -1 << "\n";
continue;
}
n /= g; k /= g; s /= g;
int a = extgcd(n, k, x, y);
x *= s; y *= s;
//cout << n << " " << x << " " << k << " " << y << " " << s << " ";
if(y < 0) y %= n;
if(y > 0){
y -= (y / n) * n;
}
if(y > 0) y -= n;
cout << -y << "\n";
}
} |
#include "bits/stdc++.h"
using namespace std;
#define REP(i, n) for(ll i = 0;i < n;i++)
#define ll long long
#define MOD 998244353LL
using vi = vector<ll>; // intの1次元の型に vi という別名をつける
using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける
ll n,k,x,y;
//moddivとnCkとgarner
//c剰余下でaをbで割った値
ll moddiv(ll a,ll b,ll c){
ll x0=c,x1=b,x2,n0=0LL,n1=1LL,n2,t=a%b,m,ans;
if (t==0LL) return a/b;
for(int i=0;i<900;i++){
m=x0/x1;
x2=x0-x1*m;
n2=(n0-m*n1)%c;
if (x2==1LL){
ans=(n2+c)%c;
break;
}
x0=x1;x1=x2;
n0=n1;n1=n2;
}
return (a+((t*ans)%c)*b-t)/b;
}
//nCk
vi mulmod(301);//0!,1!,2!,3!,4!,,,,
void first_nCk(){
mulmod[0]=1LL;
for(ll i=1;i<301;i++){
mulmod[i]=mulmod[i-1]*i%MOD;
}
}
ll nCk(ll nn,ll kk){
return moddiv(mulmod[nn],mulmod[kk]*mulmod[nn-kk]%MOD,MOD);
}
ll modinv(ll a,ll b){
return moddiv(1,a,b);
}
int main(){
ll ans=0;
first_nCk();
cin >> n >> k;
k++;
vi a(n),tmsum(k),datasum(k);
vvi data(n, vi(k));
REP(i,n) cin>>a[i];
for(ll i=0;i<n;i++){
ll gege=1;
ll ggg=1;
for(ll j=0;j<k;j++){
data[i][j]=gege;
gege=a[i]*gege%MOD;
tmsum[j]+=ggg;
ggg=ggg*a[i]*2%MOD;
}
}
for(ll i=0;i<n;i++){
for(ll j=0;j<k;j++){
datasum[j]+=data[i][j];
}
}
for(ll j=0;j<k;j++){
tmsum[j]%=MOD;
datasum[j]%=MOD;
}
for(ll i=1;i<k;i++){
ans=0;
ll tmp=MOD;
for(ll j=0;j<i+1;j++){
tmp=tmp+datasum[i-j]*datasum[j]%MOD*nCk(i,j)%MOD;
}
tmp=(tmp-tmsum[i])%MOD;
cout<<(moddiv(tmp,2,MOD))<<endl;
}
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 = 1e9;
ll qexp(ll a, ll b, ll m) {
ll res = 1;
while (b) {
if (b % 2) res = res * a % m;
a = a * a % m;
b /= 2;
}
return res;
}
void solve() {
// (M + N) choose (sum A_i + N)
int N, M;
cin >> N >> M;
int sum = 0;
for (int i = 0; i < N; i++) {
int a;
cin >> a;
sum += a;
}
int n = M + N;
int r = sum + N;
ll choose;
if (n < r) {
choose = 0;
} else {
choose = 1;
for (int i = 0; i < r; i++)
choose = choose * (n - i) % MOD * qexp(i + 1, MOD - 2, MOD) % MOD;
}
cout << choose << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int tc = 1;
// cin >> tc;
for (int t = 1; t <= tc; t++) {
// cout << "Case #" << t << ": ";
solve();
}
} |
#include<bits/stdc++.h>
#include<string.h>
#define ll long long
#define pf printf
#define sf scanf
#define ff first
#define ss second
#define clr clear()
#define sz size()
#define pb push_back
#define mk make_pair
#define pi acos(-1)
#define inf 200000000000
#define mod 998244353
#define ull unsigned long long int
#define f(i,k,n) for(i=k;i<n;i++)
#define fr(i,n,k) for(i=n;i>=k;i--)
#define ent(a) scanf("%lld",&a)
#define ent2(a,b) scanf("%lld%lld",&a,&b)
#define ent3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define mem(a) memset(a,0,sizeof(a))
#define vec(v,s) vector<ll>v[s]
#define arr(a,s) ll a[s];
/*#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
random_device rd;
mt19937 random(rd());*/
//knight and king//
int dr[]= {2,2,-2,-2,1,-1,1,-1};
int dc[]= {1,-1,1,-1,2,2,-2,-2};
int dr1[]= {0,0,1,1,1,-1,-1,-1};
int dc1[]= {1,-1,1,0,-1,0,1,-1};
int dr2[]= {0,0,1,-1};
int dc2[]= {1,-1,0,0};
////////////////////////////
using namespace std;
#define ma 200005
ll in[ma],a[ma];
vector<ll>adj[ma];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll i,j,tc,w,q,x,y,n;
string s;
while(cin>>n)
{
cin>>s;
stack<ll>q;
for(i=0;i<s.sz;i++)
{
if(s[i]=='x')
{
if(q.sz>1)
{
ll r=q.top();
q.pop();
ll r1=q.top();
q.pop();
if(s[r1]=='f'&&s[r]=='o')continue;
q.push(r1);q.push(r);
q.push(i);
}
else q.push(i);
}
else q.push(i);
}
cout<<q.sz<<endl;
}
return 0;
}
| #pragma region Region_1
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
using ll = long long;
using P = pair<int, int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using VP = vector<P>;
using VVP = vector<VP>;
using VS = vector<string>;
using VC = vector<char>;
using VVC = vector<VC>;
#define MOD 1000000007
const int INF = 1e9 + 10;
const long long INFL = 2e18 + 10; // ll max > 9*10^18
template <class T, class C>
bool chmax(T& a, C b) {
if (a <= b) {
a = b;
return true;
}
return false;
}
template <class T, class C>
bool chmin(T& a, C b) {
if (a >= b) {
a = b;
return true;
}
return false;
}
template <class T>
T sum(const vector<T>& v) {
T res = 0;
for (size_t i = 0; i < v.size(); ++i) res += v[i];
return res;
}
/////////////////////////////////////////////////////////
// print like python
// https://qiita.com/Lily0727K/items/06cb1d6da8a436369eed
/////////////////////////////////////////////////////////
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);
}
}
#pragma endregion Region_1
/////////////////////////////////////////////////////////
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
string s;
cin >> s;
VC v;
int cnt=0;
for (char c : s) {
if (c != 'x' || v.size() < 2) {
v.push_back(c);
continue;
}
if (v.back() == 'o' && v[v.size() - 2] == 'f') {
v.pop_back();
v.pop_back();
cnt++;
} else {
v.push_back(c);
}
}
print(s.size()-cnt*3);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
long long int S = 0;
vector<long long int> V(2 * N);
for (int i = 0; i < 2 * N; i++) {
cin >> V.at(i);
S += V.at(i);
}
priority_queue<long long int, vector<long long int>, greater<long long int>> Q;
long long int T = 0;
for (int i = 0; i < N; i++) {
Q.push(V.at(N - 1 - i));
Q.push(V.at(N + i));
T += Q.top();
Q.pop();
}
cout << S - T << endl;
} | #include <bits/stdc++.h>
using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;
class rep {
struct Iter {
usize itr;
constexpr Iter(const usize pos) noexcept : itr(pos) {}
constexpr void operator++() noexcept { ++itr; }
constexpr bool operator!=(const Iter& other) const noexcept {
return itr != other.itr;
}
constexpr usize operator*() const noexcept { return itr; }
};
const Iter first, last;
public:
explicit constexpr rep(const usize first, const usize last) noexcept
: first(first), last(std::max(first, last)) {}
constexpr Iter begin() const noexcept { return first; }
constexpr Iter end() const noexcept { return last; }
};
template <class T> using Vec = std::vector<T>;
template <class T> using Heap = std::priority_queue<T, Vec<T>, std::greater<T>>;
void B_main() {
usize N;
std::cin >> N;
Vec<u64> V(2 * N);
for (auto& x : V) {
std::cin >> x;
}
Heap<u64> heap;
for (const auto i : rep(0, N)) {
heap.push(V[N - i - 1]);
heap.push(V[N + i]);
heap.pop();
}
u64 ans = 0;
while (!heap.empty()) {
ans += heap.top();
heap.pop();
}
std::cout << ans << '\n';
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
B_main();
return 0;
}
|
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using P=pair<ll,ll>;
template<class T> using V=vector<T>;
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
const ll inf=(1e18);
//const ll mod=998244353;
const ll mod=1000000007;
ll GCD(ll a,ll b) {return b ? GCD(b,a%b):a;}
ll LCM(ll c,ll d){return c/GCD(c,d)*d;}
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<class T>void debag(const vector<T> &a){cerr<<"debag :";for(auto v:a)cerr<<v<<" ";cerr<<"\n";}
template<class T>void print(const vector<T> &a){for(auto v:a)cout<<v<<" ";cout<<"\n";}
//#include <unistd.h>
void solve(){
int n;
cin>>n;
V<int> a(n);
for(int i=0;i<n;i++){
cin>>a[i];
a[i]--;
}
int t=0;
V<int> ans;
V<bool> ok(n,false);
while(1){
bool ok=true;
for(int j=0;j<n;j++){
if(a[j]<j){
ok=false;
if(j%2==t){
if(t==0){
swap(a[0],a[1]);
ans.emplace_back(1);
}else{
swap(a[1],a[2]);
ans.emplace_back(2);
}
t^=1;
}
while(a[j]<j){
swap(a[j-1],a[j]);
ans.emplace_back(j);
j--;
t^=1;
}
break;
}
}
if(ok)break;
}
// print(a);
cout<<ans.size()<<"\n";
print(ans);
}
int main(){
int t;
cin>>t;
while(t--)solve();
} | /**
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡀⠀⠀⠀⠀⢀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⣠⡖⠁⠀⠀⠀⠀⠀⠀⠈⢲⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⣼⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⣧⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣸⣿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⣇⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⣿⣿⡇⠀⢀⣀⣤⣤⣤⣤⣀⡀⠀⢸⣿⣿⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⢻⣿⣿⣔⢿⡿⠟⠛⠛⠻⢿⡿⣢⣿⣿⡟⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⣀⣤⣶⣾⣿⣿⣿⣷⣤⣀⡀⢀⣀⣤⣾⣿⣿⣿⣷⣶⣤⡀⠀⠀⠀⠀
⠀⠀⢠⣾⣿⡿⠿⠿⠿⣿⣿⣿⣿⡿⠏⠻⢿⣿⣿⣿⣿⠿⠿⠿⢿⣿⣷⡀⠀⠀
⠀⢠⡿⠋⠁⠀⠀⢸⣿⡇⠉⠻⣿⠇⠀⠀⠸⣿⡿⠋⢰⣿⡇⠀⠀⠈⠙⢿⡄⠀
⠀⡿⠁⠀⠀⠀⠀⠘⣿⣷⡀⠀⠰⣿⣶⣶⣿⡎⠀⢀⣾⣿⠇⠀⠀⠀⠀⠈⢿⠀
⠀⡇⠀⠀⠀⠀⠀⠀⠹⣿⣷⣄⠀⣿⣿⣿⣿⠀⣠⣾⣿⠏⠀⠀⠀⠀⠀⠀⢸⠀
⠀⠁⠀⠀⠀⠀⠀⠀⠀⠈⠻⢿⢇⣿⣿⣿⣿⡸⣿⠟⠁⠀⠀⠀⠀⠀⠀⠀⠈⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣼⣿⣿⣿⣿⣧⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠐⢤⣀⣀⢀⣀⣠⣴⣿⣿⠿⠋⠙⠿⣿⣿⣦⣄⣀⠀⠀⣀⡠⠂⠀⠀⠀
⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠉⠀⠀⠀⠀⠀⠈⠉⠛⠛⠛⠛⠋⠁⠀⠀
**/
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define nl cout<<"\n";
#define ll long long int
#define ld double
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define F(i,a,b) for(i=a;i<b;i++)
//#include <ext/pb_ds/assoc_container.hpp> // policy based data structure header files
//#include <ext/pb_ds/tree_policy.hpp> // policy based data structure header files
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
//using namespace __gnu_pbds; // for pbds
//#define ordered_set tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> // have functions like order_of_key, find_by_order
const double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781641;
const ll M=1e18;
const ll MAXN=100200;
ll i,j,mask;
void solve()
{
ll n;
cin>>n;
vector<ll> v(n), ans;
F(i,0,n) {
cin>>v[i];
}
for(i=0; ; i++) {
bool check=true;
F(j,0,n-1) {
if(v[j]>v[j+1]) {
check=false;
break;
}
}
if(check)
break;
ll counter=i%2;
while(counter+1<n && v[counter]<v[counter+1])
counter+=2;
if(counter+1>=n) {
ans.pb(counter-2);
swap(v[counter-2], v[counter-1]);
} else {
ans.pb(counter);
swap(v[counter], v[counter+1]);
}
}
cout<<ans.size();
nl
for(auto x:ans) {
cout<<(x+1)<<" ";
}
}
int main()
{
IOS
/*#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif // ONLINE_JUDGE*/
ll t,test=1;
cin>>test;
F(t,1,test+1) {
//cout<<"Case #"<<t<<": ";
solve();
nl
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
//#define int ll
#define ll long long
#define db double
#define rep(i,be,en) for(int i=be;i<=en;i++)
const int INF = 0x3f3f3f3f;
const ll inf=0x3f3f3f3f3f3f3f3f;
//const double pi=acos(-1);
const int mod=1e9+7;
const double eps = 1e-8;
using namespace std;
const int N=1e5+7;
const int M=1e6+7;
inline int read(){
char c=getchar();int x=0,f=1;
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 T,n,m,fl,ans;
int a[N],b[N],c[N];
vector<int> g[N];
void dfs(int u,int fa){
if(c[a[u]]==0){
c[a[u]]=1;
b[u]=1;
for(int v:g[u]){
if(v==fa) continue;
dfs(v,u);
}
c[a[u]]=0;
}else{
for(int v:g[u]){
if(v==fa) continue;
dfs(v,u);
}
}
return ;
}
signed main(){
n=read();
rep(i,1,n){
a[i]=read();
}
rep(i,2,n){
int u=read(),v=read();
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1,-1);
rep(i,1,n){
if(b[i]) printf("%d\n",i);
}
}
| #include <bits/stdc++.h>
using namespace std;
#define watch(x) cerr << (#x) << ": " << (x) << endl
typedef long long LL;
const int N = 200009;
int n,m;
void nope() {
cout << -1 << endl;
exit(0);
}
void pr(int a,int b) {
cout << a << ' ' << b << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
if(n == 1) {
if(m == 0) {
pr(1, 2);
return 0;
} else {
nope();
}
}
int tp = n-m-2;
if(m < 0 || tp < 0) {
nope();
}
for(int i = 0; i < n; ++i) {
if(i < tp) {
pr(2*i+1, 2*i+2);
} else if (i < n-1) {
pr(2*i+2, 2*i+3);
} else {
pr(2*tp+1, 2*n);
}
}
return 0;
}
|
#line 2 "/home/snow/competitive-programming/competitive-programming-library/snow/template.hpp"
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
constexpr long long INF = 1LL << 60;
constexpr long double PI = 3.141592653589;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep1(i, n) for (ll i = 1; i <= (n); ++i)
#define rrep(i, n) for (ll i = (n - 1); i >= 0; --i)
#define ALL(obj) (obj).begin(), (obj).end()
#define RALL(obj) (obj).rbegin(), (obj).rend()
#define pb push_back
#define to_s to_string
#define len(v) (ll) v.size()
#define debug(x) cout << #x << ": " << (x) << '\n'
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> tpl;
template <typename T = ll>
using vec = vector<T>;
template <typename T = ll>
using vec2 = vector<vector<T>>;
template <typename T = ll>
using vec3 = vector<vector<vector<T>>>;
template < typename T >
inline bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template < typename T >
inline bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
#line 4 "/home/snow/competitive-programming/competitive-programming-library/snow/io/setup.hpp"
namespace snow{
struct IoSetup {
IoSetup() {
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(10);
}
} iosetup;
}
#line 5 "/home/snow/competitive-programming/competitive-programming-library/snow/io/helper.hpp"
template< typename T1, typename T2 >
std::ostream &operator << (std::ostream &os, const std::pair< T1, T2 > &p) {
os << p.first << " " << p.second;
return os;
}
template< typename T1, typename T2 >
std::istream &operator >> (std::istream &is, std::pair< T1, T2 > &p) {
is >> p.first >> p.second;
return is;
}
template< typename T1, typename T2, typename T3 >
std::ostream &operator << (std::ostream &os, const std::tuple< T1, T2, T3 > &t) {
auto &[a, b, c] = t;
os << a << " " << b << " " << c;
return os;
}
template< typename T1, typename T2, typename T3 >
std::istream &operator >> (std::istream &is, std::tuple< T1, T2, T3 > &t) {
auto &[a, b, c] = t;
is >> a >> b >> c;
return is;
}
template< typename T >
std::ostream &operator << (std::ostream &os, const std::vector< T > &v){
for (int i = 0; i < (int)v.size(); ++i) {
os << v[i] << (i + 1 != v.size() ? " " : "");
}
return os;
}
template< typename T >
std::istream &operator >> (std::istream &is, std::vector< T > &v){
for(T &in : v) is >> in;
return is;
}
template< typename T >
std::ostream &operator << (std::ostream &os, const std::set< T > &st){
int ct = 0;
for(auto& s : st) os << s << (++ct != st.size() ? " " : "");
return os;
}
template<class... T>
void input(T&... a){
(std::cin >> ... >> a);
}
void print() {
std::cout << '\n';
}
template<class T, class... Ts>
void print(const T& a, const Ts&... b){
std::cout << a;
(std::cout << ... << (std::cout << ' ', b));
std::cout << '\n';
}
int drop() {
std::cout << '\n';
exit(0);
}
template<class T, class... Ts>
int drop(const T& a, const Ts&... b){
std::cout << a;
(std::cout << ... << (std::cout << ' ', b));
std::cout << '\n';
exit(0);
}
#line 4 "main.cpp"
using namespace snow;
// #include "snow/algorithm/binary-search.hpp"
// #include "atcoder/modint"
// using namespace atcoder;
// using mint = modint1000000007;
int main() {
ll A, B;
cin >> A >> B;
ll ans = 0;
for(ll g = 1; g <= B; ++g){
ll k = ceil(ld(A) / g);
ll l = B / g;
if(k < l) chmax(ans, g);
}
print(ans);
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
int main(){
int l,r,m,n;
cin>>l>>r;
int nowmax=0;
for(int i=1;i<=r-l;i++){
if(l%i!=0) m=(l-l%i)+i;
else m=l;
n=(r-r%i);
if(m!=n){
nowmax=i;
}
}
cout<<nowmax;
}
|
#include <bits/stdc++.h>
using namespace std;
int main(){
int n,x;
cin>>n>>x;
vector<int> a(n);
for(int i = 0; i < n; i++){
cin >> a[i];
}
int c = 0;
for(int i = 0; i < n; i++){
if(a[i] != x){
cout << a[i] << ' ';
c++;
}
}
if(c == 0){cout << endl;}
} | #include<bits/stdc++.h>
using namespace std;
// ---------- Debugger
#define TRACE
string to_string(string s) {
return '"' + s + '"';
}
string to_string(char * s) {
return to_string((string) s);
}
string to_string(const char * s) {
return to_string((string) s);
}
string to_string(bool b) {
return (b ? "true" : "false");
}
template < typename A >
string to_string(A v);
template < typename A, typename B >
string to_string(pair < A, B > p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template < typename C > struct rge { C b, e; };
template < typename C > rge<C> range(C i, C j) { return rge<C>{i, j}; }
template < typename C >
string to_string(rge<C> v) {
bool first = true;
string res = "[";
for(auto it = v.b; it != v.e; it++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(*it);
}
res += "]";
return res;
}
template < typename A >
string to_string(A v) {
return to_string(range(v.begin(), v.end()));
}
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template < typename Head >
void __f(const string names, Head arg1) {
cerr << names << " : " << to_string(arg1) << endl;
}
template < typename Head, typename...Tail >
void __f(const string names, Head arg1, Tail...args) {
int b = 0, comma = names.size();
for(int i=0;i<names.size();i++) {
if(names[i] == ',' && b == 0) {
comma = i;
break;
}
if(names[i] == '(' || names[i] == '{' || names[i] == '[' || names[i] == '<') b++;
else if(names[i] == ')' || names[i] == '}' || names[i] == ']' || names[i] == '>') b--;
}
// size_t comma = names.find(', ');
cerr << names.substr(0, comma) << " : " << to_string(arg1) << " | ";
__f(names.substr(comma + 1), args...);
}
#else
#define trace(...)
#endif
// ---------- Debugger
#define LL long long
#define FI ios_base::sync_with_stdio(false); cin.tie(NULL);
#define PREC cout << setprecision(10) << fixed;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 9;
const int MX = 1e5 + 5;
int main() {
int n, x;
int t;
cin >> n >> x;
vector<int> ans;
for(int i=0;i<n;i++) {
cin >> t;
if(t != x) {
ans.push_back(t);
}
}
for(int i=0;i<ans.size();i++) {
cout << ans[i] << " ";
}
cout << endl;
return 0;
}
|
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<cmath>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<numeric>
#include<functional>
#include<algorithm>
#include<bitset>
#include<tuple>
#include<unordered_set>
#include<unordered_map>
#include<random>
#include<array>
#include<cassert>
using namespace std;
#define INF ((1<<30)-1)
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define all(v) v.begin(),v.end()
struct TrieNode{
int n = 0;
long long sum = 0;
TrieNode* l = nullptr, * r = nullptr;
};
class Trie {
public:
Trie() { root = new TrieNode(); }
void insert(int x) {
TrieNode* t = root;
for (int i = 30; i >= 0; i--) {
t->n++;
t->sum += x;
if (x >> i &1) {
if (t->r == nullptr) t->r = new TrieNode;
t = t->r;
}
else {
if (t->l == nullptr) t->l = new TrieNode;
t = t->l;
}
}
t->n++;
t->sum += x;
}
void erase(int x) {
TrieNode* t = root;
for (int i = 30; i >= 0; i--) {
t->n--;
t->sum -= x;
if (x >> i & 1) {
t = t->r;
}
else {
t = t->l;
}
}
t->n--;
t->sum -= x;
}
pair<int, long long> query(int x) const{
pair<int, long long > ret(0, 0);
TrieNode* t = root;
for (int i = 30; i >= 0; i--) {
if (t == nullptr)break;
if (x >> i & 1) {
if (t->l) {
ret.first += t->l->n;
ret.second += t->l->sum;
}
t = t->r;
}
else {
t = t->l;
}
}
return ret;
}
private:
TrieNode* root;
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m, q;
cin >> n >> m >> q;
vector<int> a(n, 0), b(m, 0);
Trie ta, tb;
rep(i, n) ta.insert(0);
rep(i, m)tb.insert(0);
long long ans = 0;
rep(_, q) {
int t, x, y;
cin >> t >> x >> y;
x--;
if (t == 1) {
auto p = tb.query(a[x]);
ans -= (long long)p.first * a[x];
ans += p.second;
p = tb.query(y);
ans += (long long)p.first * y;
ans -= p.second;
ta.erase(a[x]);
ta.insert(y);
a[x] = y;
}
else {
auto p = ta.query(b[x]);
ans -= (long long)p.first * b[x];
ans += p.second;
p = ta.query(y);
ans += (long long)p.first * y;
ans -= p.second;
tb.erase(b[x]);
tb.insert(y);
b[x] = y;
}
cout << ans << endl;
}
return 0;
}
| #include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<set>
typedef long long ll;
typedef unsigned un;
typedef std::vector<int> P;
typedef std::pair<int,int> pii;
ll read(){ll x=0,f=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}while(c>='0'&&c<='9')x=x*10+(c-'0'),c=getchar();return f*x;}
ll max(ll a,ll b){return a>b?a:b;}
ll min(ll a,ll b){return a<b?a:b;}
template <typename T> bool umax(T& a,T t){if(t>a)return a=t,1;return 0;}
template <typename T> bool umin(T& a,T t){if(t<a)return a=t,1;return 0;}
/**********/
const int MAXN = 200011;
struct one{int t,x,y;}a[MAXN];
int fx[MAXN],diff=0;
int place(int val){return std::lower_bound(fx+1,fx+diff+1,val)-fx;}
struct sgt
{
struct node
{
ll cnt,sum;
}t[MAXN<<2|1];
#define rt t[num]
void modify(un pos,ll k,un l=1,un r=diff,un num=1)
{
rt.cnt+=k,rt.sum+=k*fx[pos];
if(l==r)return;
un mid=(l+r)>>1;
if(pos<=mid)modify(pos,k,l,mid,num<<1);
else modify(pos,k,mid+1,r,num<<1|1);
}
ll Qcnt(un ql,un qr,un l=1,un r=diff,un num=1)
{
if(ql>qr)return 0;
if(ql<=l&&r<=qr)return rt.cnt;
un mid=(l+r)>>1;
ll res=0;
if(ql<=mid)res+=Qcnt(ql,qr,l,mid,num<<1);
if(qr>mid)res+=Qcnt(ql,qr,mid+1,r,num<<1|1);
return res;
}
ll Qsum(un ql,un qr,un l=1,un r=diff,un num=1)
{
if(ql>qr)return 0;
if(ql<=l&&r<=qr)return rt.sum;
un mid=(l+r)>>1;
ll res=0;
if(ql<=mid)res+=Qsum(ql,qr,l,mid,num<<1);
if(qr>mid)res+=Qsum(ql,qr,mid+1,r,num<<1|1);
return res;
}
}t[2];
int seq[2][MAXN];
ll f[2][MAXN];
int main()
{
int n1=read(),n2=read(),m=read();
fx[++diff]=0;
for(int i=1;i<=m;++i)a[i].t=read()-1,a[i].x=read(),fx[++diff]=a[i].y=read();
std::sort(fx+1,fx+diff+1),diff=std::unique(fx+1,fx+diff+1)-fx-1;
t[0].modify(1,n1),t[1].modify(1,n2);
for(int i=1;i<=n1;++i)seq[0][i]=1;
for(int i=1;i<=n2;++i)seq[1][i]=1;
ll ans=0;
for(int i=1;i<=m;++i)
{
int tp=a[i].t,val=place(a[i].y);
int& pre=seq[tp][a[i].x];
///printf("%d to %d(%d)\n",pre,val,a[i].y);
if(pre<=val)
{
//printf("%d\n",a[i].y-fx[pre]);
ans+=t[!a[i].t].Qcnt(1,pre)*(a[i].y-fx[pre])+ t[!a[i].t].Qcnt(pre+1,val)*a[i].y-t[!a[i].t].Qsum(pre+1,val);
}
else
{
ans-=t[!a[i].t].Qcnt(1,val)*(fx[pre]-a[i].y)+ t[!a[i].t].Qcnt(val+1,pre)*fx[pre]-t[!a[i].t].Qsum(val+1,pre);
}
t[a[i].t].modify(pre,-1);
t[a[i].t].modify(pre=val,1);
printf("%lld\n",ans);
}
return 0;
}
|
#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
}
int n,m,k;
vector<vector<int> >g;
vector<int>c;
vector<int>d[20];
int dp[1<<18][20];
void bfs(int i){
d[i][c[i]]=0;
queue<int>q;
q.push(c[i]);
while(!q.empty()){
int cur=q.front(); q.pop();
for(int j:g[cur]){
if(d[i][j]>d[i][cur]+1){
if(d[i][j]==1e7) q.push(j);
d[i][j]=d[i][cur]+1;
}
}
}
}
int fun(int mask,int i){
if(mask==0|| mask==1<<i)
return 0;
if(dp[mask][i]!=-1) return dp[mask][i];
int ans=1e7;
if(i==k){
for(int j=0;j<k;j++){
ans=min(ans,fun(mask^(1<<j),j)+1);
}
}
else
for(int j=0;j<k;j++){
if(!(mask&(1<<j))) continue;
ans=min(ans,fun(mask^(1<<j),j)+d[j][c[i]]);
}
return dp[mask][i]=ans;
}
int main()
{
boost; i_o();
cin>>n>>m;
g.resize(n+1);
for1(i,0,m){
int a,b; cin>>a>>b;
g[a].pb(b); g[b].pb(a);
}
cin>>k;
c.resize(k);
for1(i,0,k) d[i].resize(n+1,1e7);
for1(i,0,k) cin>>c[i];
for1(i,0,k) bfs(i);
/* for1(i,0,k){
cout<<"src= "<<c[i]<<endl;
for1(j,1,n+1) cout<<d[i][j]<<" ";
cout<<endl;
}
*/
fil(dp,-1);
/*
for1(mask,0,(1<<k)) for1(i,0,k+1) dp[mask][i]=1e7;
for1(i,0,k) dp[1<<i][i]=d[0][i]=0;
for(int mask=0;mask<(1<<k);mask++){
for(int i=0;i<k;i++){
if(!(mask&(1<<i))) continue;
for(int j=0;j<k;j++){
if(!(mask&(i<<j))) continue;
dp[mask][i]=min(dp[mask][i],dp[mask^i][j]+d[c[j]][c[i]]);
}
}
}
*/
int ans=fun((1<<k)-1,k);
if(ans>=1e7) ans=-1;
cout<<ans<<endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
#define repn(i,n) for(int i=1;i<=n;++i)
#define LL long long
#define pii pair <int,int>
#define fi first
#define se second
#define pb push_back
#define mpr make_pair
using namespace std;
const LL MOD=1e9+7;
int n,m,c[110],depth[110],ans[10010];
vector <pair <pii,int> > g[110];
vector <pii> e;
bool vis[110];
void dfs(int pos,int par,int d)
{
vis[pos]=true;
depth[pos]=d;
bool bk=false;
rep(i,g[pos].size())
{
if(g[pos][i].fi.fi==par&& !bk)
{
bk=true;
continue;
}
if(vis[g[pos][i].fi.fi])
{
if(depth[g[pos][i].fi.fi]<d) ans[g[pos][i].fi.se]=(g[pos][i].se==0 ? 1:2);
}
else
{
ans[g[pos][i].fi.se]=(g[pos][i].se==0 ? 1:2);
dfs(g[pos][i].fi.fi,pos,d+1);
}
}
}
int main()
{
cin>>n>>m;
int x,y;
rep(i,m)
{
scanf("%d%d",&x,&y);
e.pb(mpr(x,y));
}
repn(i,n) scanf("%d",&c[i]);
rep(i,m)
{
if(c[e[i].fi]>c[e[i].se]) ans[i]=1;//->
else if(c[e[i].fi]<c[e[i].se]) ans[i]=2;//<-
else
{
g[e[i].fi].pb(mpr(mpr(e[i].se,i),0));
g[e[i].se].pb(mpr(mpr(e[i].fi,i),1));
}
}
repn(i,n)
{
if(vis[i]) continue;
dfs(i,0,1);
}
rep(i,m) puts(ans[i]==1 ? "->":"<-");
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin>>n;
if (n==1){
cout<<"1\n";
return 0;
}
if (n==2){
cout<<"1 2\n";
return 0;
}
vector<int> div(n+1,1);
for(int i=3;i<=n;i++){
for(int j=2;j*j<=i;j++){
if (i%j==0){
div[i]=i/j;
break;
}
}
}
vector<int> ans(n+1);
ans[0]=0;
ans[1]=1;
for(int i=2;i<=n;i++){
ans[i]=ans[div[i]]+1;
}
for(int i=1;i<=n;i++){
if (i!=1) cout<<" ";
cout<<ans[i];
}
cout<<"\n";
return 0;
}
| #include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <cstdio>
#include <string>
#include <cmath>
#include <queue>
#include <tuple>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <random>
#include <set>
#include <stack>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
//#include <bits/stdc++.h>
#define maxs(x,y) x = max(x,y)
#define mins(x,y) x = min(x,y)
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define repr(i, n) for (int i = (n) - 1; i >= 0; i--)
#define FOR(i,i0,n) for(int (i)=(i0);(i)<(n);(i)++)
#define FORR(i,i0,n) for(int (i)=(n)-1; (i)>=(i0);(i)--)
#define SORT(x) sort(x.begin(),x.end())
#define SORTR(x) sort(x.begin(),x.end(),greater<int>())
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
using namespace std;
using ll = long long;
using LL = long long;
typedef std::pair<int, int> pii;
typedef std::pair<int, double> pid;
typedef std::vector<int> vi;
typedef std::vector<pii> vii;
const double PI = 3.14159265358979323846264338327950L;
const int mod = 1e9+7;
struct mint {
ll x; // typedef long long ll;
mint(ll x=0):x((x%mod+mod)%mod){}
mint operator-() const { return mint(-x);}
mint& operator+=(const mint a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint a) {
if ((x += mod-a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint a) const {
mint res(*this);
return res+=a;
}
mint operator-(const mint a) const {
mint res(*this);
return res-=a;
}
mint operator*(const mint a) const {
mint res(*this);
return res*=a;
}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t>>1);
a *= a;
if (t&1) a *= *this;
return a;
}
// for prime mod
mint inv() const {
return pow(mod-2);
}
mint& operator/=(const mint a) {
return (*this) *= a.inv();
}
mint operator/(const mint a) const {
mint res(*this);
return res/=a;
}
};
const ll INF = 1LL<<60;
struct container {
int bottom, top, id;
container(int id, int bottom, int top) : top(top), bottom(bottom), id(id) {
}
};
struct Edge {
ll to, from, cost;
Edge(ll to, ll from, ll cost) :to(to), from(from), cost(cost) {}
Edge(): to(-1), from(-1), cost(0){}
};
struct Data {
ll cost,val;
Data(ll cost=0, ll val=0) : cost(cost), val(val){}
bool operator<(const Data& a) const {
return cost > a.cost;
}
};
struct edge{
int a,b,c;
edge(int a = -1,int b = -1, int c = -1):a(a),b(b),c(c){}
bool operator<(const edge& a) const {
return c > a.c;
}
};
struct combination {
vector<mint> fact, ifact;
combination(int n):fact(n+1),ifact(n+1) {
assert(n < mod);
fact[0] = 1;
for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
ifact[n] = fact[n].inv();
for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
}
mint operator()(int n, int k) {
if (k < 0 || k > n) return 0;
return fact[n]*ifact[k]*ifact[n-k];
}
};
void solve(){
int n;
cin >> n;
const int NMAX = 100100;
int cnt[NMAX] = {};
int p = 2;
while(p<NMAX){
int cur = p;
cnt[cur] = 1;
while(cur+p < NMAX){
cur += p;
int tmp = cur;
while(tmp%p==0){
cnt[cur]++;
tmp /= p;
}
}
while(p < NMAX && cnt[p] != 0){
p++;
}
}
rep(i,n) cout << cnt[i+1]+1 << " ";
}
int main() {
int T = 1;
// cin >> T;
while (T--) {
solve();
// cout << endl;
}
}
|
#include<iostream>
#include<set>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
int N;
vector<pair<int,int> >S,T;
const double EPS=1e-7;
bool eq(double x,double y){return abs(x-y)<EPS;}
main()
{
cin>>N;
for(int i=0;i<N;i++)
{
int x,y;cin>>x>>y;
S.push_back(make_pair(x,y));
}
for(int i=0;i<N;i++)
{
int x,y;cin>>x>>y;
T.push_back(make_pair(x,y));
}
if(N==1)
{
cout<<"Yes"<<endl;
return 0;
}
for(pair<int,int>p:S)
{
pair<int,int>q=T[0];
map<int,vector<double> >s,t;
for(pair<int,int>r:S)if(r!=p)
{
int dx=r.first-p.first,dy=r.second-p.second;
s[dx*dx+dy*dy].push_back(atan2(dy,dx));
}
for(pair<int,int>r:T)if(r!=q)
{
int dx=r.first-q.first,dy=r.second-q.second;
t[dx*dx+dy*dy].push_back(atan2(dy,dx));
}
vector<double>X;
for(pair<int,vector<double> >PP:s)
{
vector<double>P=PP.second,Q=t[PP.first];
if(Q.size()!=P.size())
{
X.clear();
break;
}
vector<double>now;
sort(P.begin(),P.end());
sort(Q.begin(),Q.end());
for(int i=0;i<P.size();i++)
{
double X=P[i]-Q[0];
if(X<0)X+=2*M_PI;
bool ok=true;
for(int j=0;j<Q.size();j++)
{
int id=i+j;
if(id>=P.size())id-=P.size();
double Y=P[id]-Q[j];
if(Y<0)Y+=2*M_PI;
if(!eq(X,Y))
{
ok=false;
break;
}
}
if(ok)now.push_back(X);
}
if(X.empty())X=now;
else
{
sort(now.begin(),now.end());
vector<double>nX;
int xi=0;
for(double p:now)
{
while(xi<X.size()&&X[xi]+EPS<p)xi++;
if(xi<X.size()&&eq(X[xi],p))
{
xi++;
nX.push_back(p);
}
}
X=nX;
}
if(X.empty())break;
}
if(!X.empty())
{
cout<<"Yes"<<endl;
return 0;
}
}
cout<<"No"<<endl;
}
| #include <bitset>
#include <deque>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <algorithm>
#include <functional>
#include <iterator>
#include <locale>
#include <memory>
#include <stdexcept>
#include <utility>
#include <string>
#include <fstream>
#include <ios>
#include <iostream>
#include <iosfwd>
#include <iomanip>
#include <istream>
#include <ostream>
#include <sstream>
#include <streambuf>
#include <complex>
#include <numeric>
#include <valarray>
#include <exception>
#include <limits>
#include <new>
#include <typeinfo>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <climits>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdlib>
#include <cstddef>
#include <cstdarg>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <cwchar>
#include <cwctype>
using namespace std;
static const double EPS = 1e-8;
static const double PI = 4.0 * atan(1.0);
static const double PI2 = 8.0 * atan(1.0);
using ll = long long;
using ull = unsigned long long;
#define ALL(c) (c).begin(), (c).end()
#define CLEAR(v) memset(v,0,sizeof(v))
#define MP(a,b) make_pair((a),(b))
#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define ABS(a) ((a)>0?(a):-(a))
template<class T> T MIN(const T& a, const T& b) { return a < b ? a : b; }
template<class T> T MAX(const T& a, const T& b) { return a > b ? a : b; }
template<class T> void MIN_UPDATE(T& a, const T& b) { if (a > b) a = b; }
template<class T> void MAX_UPDATE(T& a, const T& b) { if (a < b) a = b; }
constexpr int mo = 1000000007;
int qp(int a,ll b){int ans=1;do{if(b&1)ans=1ll*ans*a%mo;a=1ll*a*a%mo;}while(b>>=1);return ans;}
int qp(int a,ll b,int mo){int ans=1;do{if(b&1)ans=1ll*ans*a%mo;a=1ll*a*a%mo;}while(b>>=1);return ans;}
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int N;
vector<complex<double>> S;
vector<complex<double>> T;
bool check(int from, int to) {
vector<complex<double>> TT;
complex<double> offset = T[from];
double theta = arg(T[to] - T[from]);
for (auto p : T) {
p -= offset;
p /= polar(1.0, theta);
TT.push_back(p);
}
for (int i = 0; i < N; ++i) {
bool found = false;
for (int j = 0; !found && j < N; ++j) {
if (abs(S[i] - TT[j]) < EPS) {
found = true;
}
}
if (!found) {
return false;
}
}
return true;
}
int main() {
std::ios::sync_with_stdio(false);
cin >> N;
if (N == 1) {
cout << "Yes" << endl;
return 0;
}
REP(n, N) {
double a, b;
cin >> a >> b;
S.emplace_back(a, b);
}
complex<double> offset = S[0];
double theta = arg(S[1] - S[0]);
for (auto& p : S) {
p -= offset;
p /= polar(1.0, theta);
}
REP(n, N) {
double c, d;
cin >> c >> d;
T.emplace_back(c, d);
}
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
if (i == j) {
continue;
}
if (check(i, j)) {
cout << "Yes" << endl;
return 0;
}
}
}
cout << "No" << endl;
}
|
#include<bits/stdc++.h>
#define ll long long
#define ri register int
using namespace std;
int main(){
int a,b;
cin>>a>>b;
if(a == b) cout<<a<<endl;
else cout<<(3-a-b)<<endl;
return 0;
} | #include <bits/stdc++.h>
int main() {
using namespace std;
cin.tie(nullptr)->sync_with_stdio(false);
int n;
cin >> n;
string s;
cin >> s;
if (s[0] != s.back()) {
cout << "1\n";
exit(0);
} else {
for (int i = 1; i + 1 < n - 1; ++i)
if (s[i] != s[0] and s[i + 1] != s[0]) {
cout << "2\n";
exit(0);
}
}
cout << "-1\n";
}
|
#include<bits/stdc++.h>
using namespace std;
int main()
{
int v,t,s,d;
cin>>v>>t>>s>>d;
float x = (float)d/v;
int flag =1;
if(x>=(float)t && x<=(float)s)
flag =0;
if(flag == 1)
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
return 0;
} | #include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < (t); ++i)
#define dsrep(i,t,s) for(int i = (t)-1; i >= (s); --i)
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
#define show(x) cerr<<#x<<" = "<<x<<endl;
#define bn(x) ((1<<(x))-1)
#define newline puts("")
using namespace std;
template<typename T> using vc = vector<T>;
template<typename T> using vv = vc<vc<T>>;
template<typename T> using PQ = priority_queue<T,vc<T>,greater<T> >;
using ll = long long;
using uint = unsigned;
using ull = unsigned long long;
using P = pair<int,int>;
using T3 = tuple<int,int,int>;
using vi = vc<int>;
using vvi = vv<int>;
using vl = vc<ll>;
using vp = vc<P>;
using vt = vc<T3>;
int getInt(){int x;scanf("%d",&x);return x;}
vi pm(int n, int s=0) { vi a(n); iota(rng(a),s); return a;}
template<typename T>istream& operator>>(istream&i,vc<T>&v){rep(j,sz(v))i>>v[j];return i;}
template<typename T>string join(const vc<T>&v,const string& d=""){stringstream s;rep(i,sz(v))(i?s<<d:s)<<v[i];return s.str();}
template<typename T>ostream& operator<<(ostream&o,const vc<T>&v){if(sz(v))o<<join(v," ");return o;}
template<typename T1,typename T2>istream& operator>>(istream&i,pair<T1,T2>&v){return i>>v.fi>>v.se;}
template<typename T1,typename T2>ostream& operator<<(ostream&o,const pair<T1,T2>&v){return o<<v.fi<<","<<v.se;}
vc<string> split(const string& s,char d=' '){vc<string> r(1);for(char c:s)if(c==d)r.pb("");else r.back()+=c;return r;}
string operator*(const string& s,int t){return join(vc<string>(t,s));}
template<typename T>bool mins(T& x,const T&y){if(x>y){x=y;return true;}else return false;}
template<typename T>bool maxs(T& x,const T&y){if(x<y){x=y;return true;}else return false;}
template<typename T>T dup(T x, T y){return (x+y-1)/y;}
template<typename T>ll suma(const vc<T>&a){ll res(0);for(auto&&x:a)res+=x;return res;}
template<typename T>void uni(vc<T>& a){sort(rng(a));a.erase(unique(rng(a)),a.end());}
const double eps = 1e-10;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define dame { puts("-1"); return;}
#define yn {puts("Yes");}else{puts("No");}
const int MX = 200005;
struct Solver {
void solve() {
int k,n,m;
scanf("%d%d%d",&k,&n,&m);
vl a(k);
cin>>a;
vl b(k);
rep(i,k) b[i] = a[i]*m/n;
int r = m-suma(b);
assert(0 <= r && r < k);
vp p;
rep(i,k) p.eb(a[i]*m%n,i);
sort(rrng(p));
rep(i,r) b[p[i].se]++;
cout<<b<<endl;
}
};
int main() {
int ts = 1;
// scanf("%d",&ts);
rrep(ti,ts) {
Solver solver;
solver.solve();
}
return 0;
}
|
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);++i)
typedef long long ll;
using namespace std;
using P=pair<int,int>;
int main()
{
string s;
cin>>s;
string t="ZONe";
int ans=0;
// rep(i,s.size()){
// if(s[i]==t[0]) cnt++;
// if(cnt==1&&s[i]==t[1])cnt++;
// if(cnt==2&&s[i]==t[2]) cnt++;
// if(cnt==3&&s[i]==t[3]) cnt++;
// if(cnt==4){
// ans++;
// cnt=0;
// }
// }
rep(i,s.size()-t.size()+1){
int cnt=0;
rep(j,t.size()){
if(s[j+i]==t[j]) cnt++;
else {
cnt=0;
continue;
}
if(cnt==4) {
ans++;
cnt=0;
}
}
}
cout<<ans<<endl;
return 0;
} | #include<bits/stdc++.h>
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL)
#define ll unsigned long long int
#define Test ll t; cin>>t; while(t--)
#define en cout<<endl;
#define ub upper_bound
#define lb lower_bound
#define pb push_back
#define PI 3.141592653589793238
#define str string s; cin>>s; ll len=s.length();
#define flag ll flag=0;
#define MAX 1000000000
#define pob pop_back
using namespace std;
int main()
{ fast;
ll n,ans=0;
cin>>n;
vector<char> v;
char ar[n];
for(ll i=0;i<n;i++) cin>>ar[i];
for(ll i=0;i<n;i++)
{
v.pb(ar[i]);
ll k=v.size();
if(k>=3)
{
if(v[k-1]=='x' && v[k-2]=='o' && v[k-3]=='f')
{ans++;
v.pob(); v.pob(); v.pob();
}
}
}
cout<<n-(3*ans);
} |
#include <bits/stdc++.h>
using namespace std;
int main()
{
int N,M;
string a;
cin >> N >> M;
for(int i=0; i<M; i++) {
cin >> a;
}
for(int y=0; y<N; y++) {
for(int x=0; x<N; x++)
cout << ".";
cout << endl;
}
};
| #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
vector<vector<char>> mat;
vector<pair<int,string>> in;
vector<string> in2;
vector<string> in3;
vector<string> in4;
vector<string> in5;
vector<string> in6;
vector<string> in7;
vector<string> in8;
vector<string> in9;
vector<string> in10;
vector<string> in11;
vector<string> in12;
vector<string> concat_in;
int n,m;
void answer_print(){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cout << mat[i][j] ;
if(j==n-1) cout << endl;
}
}
}
bool isPut(int x,int y,string str){
if(mat[y][x] != '.') return false;
if(n-x+1 < str.size()) return false;
return true;
}
void Put(int x,int y,string str){
for(int i = x;i<x+str.size();i++){
mat[y][i] = str[i-x];
}
}
void solve(){
int k=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(isPut(j,i,concat_in[k])) {
Put(j,i,concat_in[k]);
k++;
}
}
}
}
int main(){
// input
cin >> n >> m;
mat.resize(n);in.resize(m);
for(int i=0;i<n;i++) mat[i].resize(n,'.');
for(int i=0;i<m;i++) {
cin >>in[i].second;
in[i].first = in[i].second.size();
}
for(int i=0;i<in.size();i++){
switch(in[i].first){
case 2 : in2.push_back(in[i].second);break;
case 3 : in3.push_back(in[i].second);break;
case 4 : in4.push_back(in[i].second);break;
case 5 : in5.push_back(in[i].second);break;
case 6 : in6.push_back(in[i].second);break;
case 7 : in7.push_back(in[i].second);break;
case 8 : in8.push_back(in[i].second);break;
case 9 : in9.push_back(in[i].second);break;
case 10 : in10.push_back(in[i].second);break;
case 11 : in11.push_back(in[i].second);break;
case 12 : in12.push_back(in[i].second);break;
default: break;
}
}
for(int i=0;i<in2.size();i++) concat_in.push_back(in2[i]);
for(int i=0;i<in3.size();i++) concat_in.push_back(in3[i]);
for(int i=0;i<in4.size();i++) concat_in.push_back(in4[i]);
for(int i=0;i<in5.size();i++) concat_in.push_back(in5[i]);
for(int i=0;i<in6.size();i++) concat_in.push_back(in6[i]);
for(int i=0;i<in7.size();i++) concat_in.push_back(in7[i]);
for(int i=0;i<in8.size();i++) concat_in.push_back(in8[i]);
for(int i=0;i<in9.size();i++) concat_in.push_back(in9[i]);
for(int i=0;i<in10.size();i++) concat_in.push_back(in10[i]);
for(int i=0;i<in11.size();i++) concat_in.push_back(in11[i]);
for(int i=0;i<in12.size();i++) concat_in.push_back(in12[i]);
solve();
answer_print();
} |
/* これを翻訳している間、あなたはあなたの人生のいくつかの貴重な瞬間を無駄にしました */
#include <bits/stdc++.h>
using namespace std;
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define S(x) (int)(x).size()
#define L(x) (int)(x).length()
#define ld long double
#define mem(x,y) memset(x,y,sizeof x)
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int , null_type , less<int> , rb_tree_tag , tree_order_statistics_node_update> ordered_set;
const int mod = 1e9+7;
const ll infl = 0x3f3f3f3f3f3f3f3fLL;
const int infi = 0x3f3f3f3f;
const int maxn = 60;
ll C[maxn + 1][maxn + 1];
void solve()
{
int a,b;
cin>>a>>b;
ll K;
cin>>K;
/*string s = "aaabbb";
do
{
cout<<s<<'\n';
}while(next_permutation(all(s)));
*/
C[0][0] = 1;
for (int n = 1; n <= maxn; ++n)
{
C[n][0] = C[n][n] = 1;
for (int k = 1; k < n; ++k)
C[n][k] = C[n - 1][k - 1] + C[n - 1][k];
}
int n=a+b;
for(int i=0;i<n;i++)
{
if(a+b==1)
{
if(a==1) cout<<'a';
else cout<<'b';
break;
}
if(a>0)
{
if(C[a+b-1][b] >= K)
{
a--;
cout<<'a';
continue;
}
K-=C[a+b-1][b];
b--;
cout<<'b';
}
else
{
b--;
cout<<'b';
}
}
cout<<'\n';
}
int main()
{
IOS
int t=1;
//cin>>t;
while(t--)
{
solve();
}
}
| #include<bits/stdc++.h>
using namespace std;
int a,b;
long long k;
long long C(int x,int y){
if(x<0||x<y)return 0;
long long ans=1;
for(long long i=0;i<y;++i)ans*=x-i,ans/=i+1;
return ans;
}
int main(){
scanf("%d%d%lld",&a,&b,&k);
while(a||b){
if(a&&k<=C(a+b-1,a-1))putchar('a'),a--;
else k-=C(a+b-1,a-1),putchar('b'),b--;
}
return 0;
} |
#include <cstdio>
using namespace std;
using LL = long long;
char s[7], t[7];
int score(const char* cards)
{
int cnt[10];
for(int i=0; i<10; i++) cnt[i] = i;
for(int i=0; i<5; i++)
cnt[cards[i] - '0'] *= 10;
int res = 0;
for(int x: cnt) res += x;
return res;
}
int main()
{
int k;
scanf("%d%s%s", &k, s, t);
int cnt[10];
for(int i=1; i<10; i++) cnt[i] = k;
for(int i=0; i<4; i++)
cnt[s[i] - '0'] --,
cnt[t[i] - '0'] --;
LL win = 0LL;
for(int x=1; x<10; x++)
if(cnt[x])
{
s[4] = '0' + x;
int sscore = score(s);
for(int y=1; y<10; y++)
if(cnt[y])
{
t[4] = '0' + y;
if(sscore > score(t))
win += cnt[x] * LL(cnt[y] - (x == y));
}
}
LL tmp = 9LL * k - 8LL;
printf("%.8lf\n", double(win) / tmp / double(tmp - 1LL));
return 0;
} | #include<iostream>
#include<vector>
#include<map>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
int main()
{
int n,i;
ll s=0,res=0;
map<ll,ll> m;
decltype(m)::iterator it;
cin >> n;
vector<ll> a(n);
for(i=0;i<n;i++)
cin >> a[i];
m[0]=1;
for(i=0;i<n;i++){
if(i%2==0)
s+=a[i];
else
s-=a[i];
it=m.find(s);
if(it!=m.end())
it->second++;
else
m.insert(make_pair(s,1));
}
for(it=m.begin();it!=m.end();it++){
s=it->second;
res+=s*(s-1)/2;
}
cout << res << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define INF32 2147483647 //2.147483647×10^{9}:32bit整数のinf
#define INF64 9223372036854775807 //9.223372036854775807×10^{18}:64bit整数のinf
#define MOD 1000000007
ll myceil(ll a,ll b){return (a + (b - 1)) / b;}
ll myfloor(ll a,ll b){return a / b;}
int ReLU(int x){return x >= 0 ? x : 0;}
int gcd(int a, int b){ return (a % b) ? gcd(b, a % b) : b;}
int lcm(int a, int b){ return (a / gcd(a, b)) * b;}
template<class t,class u> void chmax(t& a,u b){if(a < b) a = b;}
template<class t,class u> void chmin(t& a,u b){if(b < a) a = b;}
template<class T> void inner_product(const vector<pair<T, T>>& vec, T& product){for(const auto& e:vec) product += e.first * e.second;}
int factorial(int n){
if(n == 0) return 1;
else return factorial(n - 1) * n;
}
int is_prime(int x){
if(x <= 1) return 0;
for(int i = 2; i * i <= x; i++){
if(x % i == 0) return 0;
}
return 1;
}
int ngcd(int n, int a[])
{
int i, d;
d = a[0];
for (i = 1; i < n && d != 1; i++)
d = gcd(a[i], d);
return d;
}
int my_calc(const int a, const int b, const char& ope){
if(ope == '+') return a + b;
else if(ope == '-') return a - b;
else return 0;
}
int main(){
ios::sync_with_stdio(false); cin.tie(nullptr);
int n, a, b;
cin >> n >> a >> b;
int ans = n - a + b;
cout << ans << '\n';
return 0;
}
| #include <iostream>
using namespace std;
int main(){
int a, b, c, d;
cin >> a >> b >> c >> d;
cout << b - c << endl;
} |
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <map>
using namespace std;
int main() {
int N;
cin>>N;
cout<<N-1<<endl;
} | #include<bits/stdc++.h>
using namespace std;
int main()
{
long long int n;
cin>>n;
long long int r=sqrt(2*n);
long long int ans=0;
for(long long int i=1;i<=r;i++)
{
if(i%2!=0)
{
if(n%i==0)
{
long long int temp=n/i;
long long int diff=i/2;
long long int gr=i;
long long int smallest=temp-diff;
if(temp-diff>0&&(gr+(2*smallest-1))>r)
{
ans+=2;
}
else
{
ans++;
}
}
}
else
{
long long int check=i/2;
long long int div=n%i;
if(div==check)
{
long long int temp_n=n-check;
temp_n=temp_n/i;
long long int new_a=temp_n-(check-1);
long long int gr=i;
long long int smallest=new_a;
if(smallest>0&&(gr+(2*smallest-1))>r)
{
ans+=2;
}
else
{
ans++;
}
//ans++;
}
}
}
cout<<ans;
return 0;
}
|
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 2e5+8, inf = 1e18+9, mod = 1e9+7;
int n, m, a[maxn], b[maxn], p[maxn], vs[maxn];
void solve() {
int i, j;
//cin >> n;
scanf("%lld", &n);
vector<pair<int, int>> vec;
for (i = 1; i <= n; i++) scanf("%lld", &a[i]); //cin >> a[i];
for (i = 1; i <= n; i++) scanf("%lld", &b[i]); //cin >> b[i];
for (i = 1; i <= n; i++) scanf("%lld", &p[i]); //cin >> p[i];
for (i = 1; i <= n; i++) if (a[i] <= b[p[i]] && i != p[i]) return (void)(printf("-1\n"));
for (i = 1; i <= n; i++) if (!vs[i]) {
int start = i; vs[i] = 1;
if (i == p[i]) continue;
for (j = p[i]; j != i; j = p[j]) {
vs[j] = 1;
if (a[j] > a[start]) start = j;
}
for (j = p[start]; j != start; j = p[j]) vec.push_back({start, j});
}
//cout << vec.size() << endl;
printf("%lld\n", (int)vec.size());
for (auto [x, y] : vec) printf("%lld %lld\n", x, y); //cout << x << " " << y << endl;
}
signed main() {
//ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); //cout << fixed << setprecision(15);
int t = 1; //cin >> t;
while (t--) solve();
return 0;
}
| #include<deque>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<tuple>
#include<string>
#include<chrono>
#include<functional>
#include<iterator>
#include<random>
#include<unordered_set>
#include<array>
#include<map>
#include<iomanip>
#include<assert.h>
#include<list>
#include<bitset>
#include<stack>
#include<memory>
#include<numeric>
#include <utility>
using namespace std;
typedef long long int llint;
typedef long double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
#define REP(i, n) for(int i = 0;i < (n);i++)
/*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/
const llint mod=998244353;
const llint inf=2.19e15+1;
const long double pai=3.141592653589793238462643383279502884197;
const long double eps=1e-10;
template <class T,class U>bool chmin(T& a,U b){if(a>b){a=b;return true;}return false;}
template <class T,class U>bool chmax(T& a,U b){if(a<b){a=b;return true;}return false;}
llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);}
llint lcm(llint a,llint b){if(a==0){return b;}return a/gcd(a,b)*b;}
template<class T> void SO(T& ve){sort(ve.begin(),ve.end());}
template<class T> void REV(T& ve){reverse(ve.begin(),ve.end());}
template<class T>llint LBI(const vector<T>&ar,T in){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();}
template<class T>llint UBI(const vector<T>&ar,T in){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();}
int main(void){
//限界値が軽い方から処理
int n,i;cin>>n;
vector<int>a(n);
vector<int>b(n);
vector<int>p(n);
vector<int>g(n);
vector<pair<int,int>>sa(n);
for(i=0;i<n;i++){cin>>a[i];sa[i]=mp(a[i],i);}
for(i=0;i<n;i++){cin>>b[i];}
for(i=0;i<n;i++){cin>>p[i];p[i]--;g[p[i]]=i;}
for(i=0;i<n;i++){
if(p[i]!=i){if(a[i]<=b[p[i]]){cout<<-1<<endl;return 0;}}
}
SO(sa);
vector<pair<int,int>>ans;
for(i=0;i<n;i++){
int t=sa[i].sec;
if(t==p[t]){continue;}
int s=g[t];
swap(p[t],p[s]);
swap(g[p[t]],g[p[s]]);
ans.pub({t+1,s+1});
}
cout<<ans.size()<<endl;
for(auto it:ans){cout<<it.fir<<" "<<it.sec<<endl;}
}
|
#include <bits/stdc++.h>
#define MOD 1000000007
#define MAXN 1000000
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define vd vector<double>
#define vll vector<long long>
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define pi pair<int, int>
#define pll pair<long long, long long>
#define mp make_pair
#define fi first
#define se second
#define rep(i,s,n) for(ll i = s;i <= n;i++)
#define repi(n) for (int i = 0; i < n; i++)
#define repj(n) for (int j = 0; j < n; j++)
#define deb_vec(a) for(auto i : a) cout << i << " "; cout << "\n"
#define FAST ios_base::sync_with_stdio(false); cin.tie(NULL)
#define t_cases ll t; cin >> t; while(t--)
#define debug(a) cout << a << "\n"
using namespace std;
void solve() {
int a, b, c; cin >> a >> b >> c;
if (a >= 0 and b >= 0) {
if (a > b) cout << ">";
else if (a < b) cout << "<";
else cout << "=";
} else if (a < 0 and b >= 0) {
if (c & 1) cout << "<";
else if (abs(a) > abs(b)) cout << ">";
else if (abs(a) < abs(b)) cout << "<";
else cout << "=";
} else if (a >= 0 and b < 0) {
if (c & 1) cout << ">";
else if (abs(a) > abs(b)) cout << ">";
else if (abs(a) < abs(b)) cout << "<";
else cout << "=";
} else {
if (c & 1) {
if (abs(a) > abs(b)) cout << "<";
else if (abs(a) < abs(b)) cout << ">";
else cout << "=";
} else {
if (abs(a) > abs(b)) cout << ">";
else if (abs(a) < abs(b)) cout << "<";
else cout << "=";
}
}
}
int main() {
FAST;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
solve();
}
| /*
*/
#pragma GCC optimize("O3")
#define _CRT_SECURE_NO_WARNINGS
#include <assert.h>
#include <math.h>
#include <memory.h>
#include <stdio.h>
#include <algorithm>
#include <complex>
#include <ctime>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define y0 sdkfaslhagaklsldk
#define y1 aasdfasdfasdf
#define yn askfhwqriuperikldjk
#define j1 assdgsdgasghsf
#define tm sdfjahlfasfh
#define lr asgasgash
#define norm asdfasdgasdgsd
#define left asdgashgrketwjklrej
#define have adsgagshdshfhds
#define ends asdgahhfdsfshdshfd
#define prev asdgSHJsfgsdfhdsh
#define hash asdgasdgasdgdfrywewery
#define eps 1e-12
#define M_PI 3.141592653589793
#define bsize 1024
#define ldouble long double
using namespace std;
const int bs = 1000000007;
const int N = 200031;
int tests;
long long count_splits(long long len){
// we have to split len over 3 blocks, zeroes allowed.
// this is C(len+2,2)
long long A=len+2;
long long B=len+1;
if (A%2)
B/=2;
else
A/=2;
return A*B%bs;
}
long long solver(long long n,long long a,long long b){
long long free_spots=n-a-b;
if (free_spots<0)
return 0;
return count_splits(free_spots);
}
int main() {
//freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(0);
// cin.tie(0);
cin>>tests;
for (;tests;--tests){
long long n,a,b;
cin>>n>>a>>b;
long long ans=solver(n,a,b);
ans=ans*2%bs;
long long all_placements=1ll*(n-a+1)*(n-b+1)%bs;
long long bad=all_placements-ans;
if (bad<0)
bad+=bs;
long long really_bad=1ll*bad*bad%bs;
long long good=all_placements*all_placements%bs-really_bad;
if (good<0)
good+=bs;
// cout<<bad<<" "<<really_bad<<" "<<all_placements<<endl;
cout<<good<<endl;
}
//cin.get(); cin.get();
return 0;
}
|
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pii pair<int,int>
#define piii pair<int,pii>
#define sp " "
#define nl "\n"
#define all(x) x.begin(),x.end()
#define fastio() ios_base::sync_with_stdio(0);cin.tie(0);
#define ll long long
#define int ll
using namespace std;
constexpr int N = 2e5+5;
constexpr int INF = 1e9+5;
constexpr int mod = 1e9+7;
int n,ans,st[N];
void update(int x,int val){
for(x++;x<=n;x+=x&-x)
st[x]+=val;
}
int get(int x){
int res=0;
for(x++;x>0;x-=x&-x)
res+=st[x];
return res;
}
signed main(){
#ifdef LOCAL
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
fastio()
cin >> n;
vector<pii> a(n),b(n);
for(int i=0;i<n;i++){
int x;
cin >> x;
a[i].ff=x+i;
a[i].ss=i;
}
for(int i=0;i<n;i++){
int x;
cin >> x;
b[i].ff=x+i;
b[i].ss=i;
}
sort(all(a));
sort(all(b));
vector<int> s(n);
for(int i=0;i<n;i++){
if(a[i].ff!=b[i].ff){
cout << -1 << nl;
return 0;
}
s[a[i].ss]=b[i].ss;
}
for(int i=n-1;i>=0;i--){
ans+=get(s[i]);
update(s[i],1);
}
cout << ans << nl;
} | #include<bits/stdc++.h>
#include<vector>
#include<math.h>
#include<string>
using namespace std;
#define MOD 998244353
#define MAX 200005
#define PMAX 18
#define PRECISION 0.000001
#define INF 1000000000000000000
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
vector<int> pos[MAX*2][2];
int arr[MAX][2];
int permut[MAX];
map<int,int> M;
int segment_tree[MAX*4];
void update(int index,int l,int r,int ind)
{
if(l>ind||r<ind) return;
if(l==r)
{
segment_tree[index]=1;
return;
}
int m=(l+r)/2;
update(index*2,l,m,ind);
update(index*2+1,m+1,r,ind);
segment_tree[index]=segment_tree[index*2]+segment_tree[index*2+1];
}
int query(int index,int l,int r,int L,int R)
{
if(l>R||r<L) return 0;
if(L<=l&&r<=R)
{
return segment_tree[index];
}
int m=(l+r)/2;
return query(index*2,l,m,L,R)+query(index*2+1,m+1,r,L,R);
}
int main()
{
int n,i,j;
scanf("%d",&n);
for(j=0;j<2;j++)
{
for(i=0;i<n;i++)
{
scanf("%d",&arr[i][j]);
}
for(i=0;i<n-1-i;i++)
{
swap(arr[i][j],arr[n-1-i][j]);
}
}
for(j=0;j<2;j++)
{
for(i=0;i<n;i++)
{
int ss=M.size();
if(!M.count(arr[i][j]-i))
{
M[arr[i][j]-i]=ss;
ss++;
}
pos[M[arr[i][j]-i]][j].push_back(i);
}
//M.clear();
}
for(i=0;i<MAX;i++)
{
int a=pos[i][0].size(),b=pos[i][1].size();
if(a!=b)
{
printf("-1");
return 0;
}
for(j=0;j<pos[i][0].size();j++)
{
permut[pos[i][1][j]]=pos[i][0][j];
//printf("%d<-%d\n",pos[i][1][j],pos[i][0][j]);
}
}
long long ans=0;
for(i=0;i<n;i++)
{
//printf("%d ",permut[i]);
if(permut[i]<n-1)
{
ans=ans+query(1,0,n-1,permut[i]+1,n-1);
}
update(1,0,n-1,permut[i]);
}
//printf("\n");
printf("%lld",ans);
return 0;
}
|
#include <stdio.h>
typedef long long ll;
int main(void) {
ll i, j, l, n, k, a, b;
scanf("%lld%lld", &n, &k);
char s[n + 10], t[n], r[4] = "RPS";
scanf("%s", s);
for(j = 0; j < k; ++j) {
for(i = 0; i < n; ++i) {
a = (i * 2) % n, b = (i * 2 + 1) % n;
for(l = 0; l < 3; ++l) if(s[a] == r[l] && s[b] == r[(l + 1) % 3]) {
t[i] = s[b];
break;
}
if(l == 3) t[i] = s[a];
}
for(i = 0; i < n; ++i) s[i] = t[i];
}
printf("%c", s[0]);
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;
int main() {
int n, k;
string s;
cin >> n >> k >> s;
rep(i, k) {
string t = "";
rep(j, n) {
char c = s[(j * 2) % n], d = s[(j * 2 + 1) % n];
if (c == d) t.push_back(c);
else if (c == 'R' && d == 'S') t.push_back(c);
else if (c == 'P' && d == 'R') t.push_back(c);
else if (c == 'S' && d == 'P') t.push_back(c);
else if (c == 'S' && d == 'R') t.push_back(d);
else if (c == 'R' && d == 'P') t.push_back(d);
else if (c == 'P' && d == 'S') t.push_back(d);
}
s = t;
}
cout << s[0] << "\n";
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rep3(i,m,n) for(int (i)=m;(i)<=(n);(i)++)
#define rep3rev(i,m,n) for(int (i)=m;(i)>=(n);(i)--)
#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
using ll = long long;
using vll = vector<ll>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using P = pair<int, int>;
using Pll = pair<ll, ll>;
using LD = long double;
template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }
template <typename T> void coutall(T v) { if(v.empty()){cout << endl; return;} for(auto i = v.begin(); i != --v.end(); i++){cout << *i << " ";} cout << *--v.end() << endl; }
template <typename T> void DEBUGall(T v) { if(v.empty()){cerr << endl; return;} for(auto i = v.begin(); i != --v.end(); i++){cerr << *i << " ";} cerr << *--v.end() << endl; }
inline void IN(void){ return; }
template <typename First, typename... Rest> void IN(First& first, Rest&... rest){ cin >> first; IN(rest...); return; }
inline void OUT(void){ cout << "\n"; return; }
template <typename First, typename... Rest> void OUT(First first, Rest... rest){ cout << first << " "; OUT(rest...); return; }
inline void DEBUG(void){ cerr << "\n"; return; }
template <typename First, typename... Rest> void DEBUG(First first, Rest... rest){ cerr << first << " "; DEBUG(rest...); return; }
void yes(bool ok = true){ cout << (ok ? "yes" : "no") << endl; }
void Yes(bool ok = true){ cout << (ok ? "Yes" : "No") << endl; }
void YES(bool ok = true){ cout << (ok ? "YES" : "NO") << endl; }
ll myceil(ll a, ll b) { return a >= 0 ? (a+b-1)/b : -((-a)/b); }
ll myfloor(ll a, ll b) { return a >= 0 ? a/b : -myceil(-a, b); }
map<int, int> compress(vi a){
int n = a.size();
map<int, int> cnt;
rep(i, n) cnt[a[i]] = 1;
int now = 0;
for(auto [k, v] : cnt){
cnt[k] = now;
now++;
}
return cnt;
}
void Main(){
int n, m; IN(n, m);
vi x(m), y(m);
rep(i, m) IN(x[i], y[i]);
map<int, int> c = compress(x);
int sz = c.size();
vvi black(sz);
rep(i, m) {
black[c[x[i]]].pb(y[i]);
}
set<int> st;
st.insert(n);
for(auto b : black){
vi stadd, stdel;
for(auto pos : b){
if(st.find(pos-1) != st.end() || st.find(pos+1) != st.end()) stadd.pb(pos);
if(st.find(pos-1) == st.end() && st.find(pos+1) == st.end()) stdel.pb(pos);
}
for(auto aa : stadd) st.insert(aa);
for(auto bb : stdel) {
auto it = st.find(bb);
if(it != st.end()) st.erase(it);
}
}
OUT(st.size());
return;
}
int main(){
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(15);
Main();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF=20;
int main(){
ll n,m;cin>>n>>m;
vector<ll> g(n);
while(m--){
ll a,b;cin>>a>>b;a--;b--;
g[a]|=(1<<b);
g[b]|=(1<<a);
}
vector<ll> d(1<<n,INF);
d[0]=1;
for(ll i=0;i<n;i++)for(ll j=0;j<(1<<n);j++)if(d[j]==1 && (g[i]&j)==j)d[j|(1<<i)]=1;
for(ll i=1;i<(1<<n);i++)for(ll j=i;j;j--,j&=i)d[i]=min(d[i],d[i^j]+d[j]);
cout<<d[(1<<n)-1]<<endl;
}
|
#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define LL long long int
vector<pair<LL,LL> > adj[100001];
vector <LL> djikstra(LL src,LL N)
{
vector <LL> dis(N,INT_MAX);
priority_queue< pair<LL,LL> ,vector< pair <LL,LL> >, greater< pair<LL,LL> > > pq;
for(auto it:adj[src])
{
pq.push(it);
dis[it.second]=it.first;
}
while(!pq.empty())
{
pair <LL,LL> temp=pq.top();
pq.pop();
LL s=temp.second;
for(auto it:adj[s])
{
if( dis[it.second] > dis[s] + it.first )
{
dis[it.second]= dis[s] + it.first;
pq.push({dis[s] + it.first,it.second});
}
}
}
return dis;
}
int main()
{
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
LL N,M;
cin >> N >> M;
map< pair<LL,LL> ,LL > hash;
map<LL,LL> Self;
for(LL i=0;i<N;i++)
{
Self[i]=INT_MAX;
}
for(LL i=0;i<M;i++)
{
LL x,y,z;
cin>>x>>y>>z;
x--;
y--;
if(x==y)
{
Self[x]=min(Self[x],z);
}
if( hash.find( {x,y} )==hash.end())
{
hash[{x,y}]=z;
}
else
{
hash[{x,y}]=min(hash[{x,y}],z);
}
}
for(auto it:hash)
{
adj[it.first.first].push_back({it.second,it.first.second});
}
for(LL i=0;i<N;i++)
{
vector<LL> arr= djikstra(i,N);
if(arr[i]==INT_MAX)
{
if(Self[i]==INT_MAX)
cout<<-1<<endl;
else
cout<<Self[i]<<endl;
}
else
cout<<arr[i]<<endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
int N, Q;
const int maxn = 2e5 + 5;
typedef long long ll;
inline ll read()
{
ll x = 0;
int s = 1;
char c = getchar();
while (c < 48 || c > 57)
{
if (c == '-')
s = -1;
c = getchar();
}
while (c >= 48 && c <= 57)
{
x = (x << 3) + (x << 1) + c - '0';
c = getchar();
}
return x * s;
}
int main()
{
int N, Q;
N = read();
ll a, t;
ll l = -1e9, r = 1e9, b = 0;
for (int i = 0; i < N; i++)
{
a = read();
t = read();
if (t == 1)
{
l += a;
r += a;
b += a;
}
else if (t == 2)
{
l = max(l, a);
r = max(r, a);
}
else
{
l = min(l, a);
r = min(r, a);
}
}
Q = read();
while (Q--)
{
ll x = read() + b;
if (x < l)
cout << l;
else if (x > r)
cout << r;
else
cout << x;
cout << ' ';
}
} |
//agrawal117
//chahatagrawal117
#include<bits/stdc++.h>
#define endl '\n'
#define mod 1000000007
typedef long long int ll;
using namespace std;
#define MAX 100005
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll n,m; cin>>n>>m;
char arr[n+10][m+10];
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>arr[i][j];
ll dp[n+10][m+10];
memset(dp,0,sizeof(dp));
ll col[m+10]={0};
dp[1][1]=1;
col[1]=1;
ll di[n+10][m+10];
memset(di,0,sizeof(di));
di[1][1]=1;
ll sum=1;
for(int i=2;i<=m;i++){
if(arr[1][i]=='#') sum=0;
dp[1][i]=sum;
sum=(sum+dp[1][i]%mod);
col[i]=dp[1][i];
di[1][i]=dp[1][i];
}
sum=1;
for(int i=2;i<=n;i++){
if(arr[i][1]=='#') sum=0;
dp[i][1]=sum;
sum=(sum+dp[i][1]%mod);
di[i][1]=dp[i][1];
}
for(int i=2;i<=n;i++){
sum=dp[i][1];
for(int j=2;j<=m;j++){
if(arr[i][j]=='#') {
sum=0;
col[j]=0;
di[i][j]=0;
dp[i][j]=0;
}
else{
ll val=sum;
val=(val+col[j])%mod;
val=(val+di[i-1][j-1]);
dp[i][j]=val%mod;
sum=(sum+val)%mod;
col[j]=(val+col[j])%mod;
di[i][j]=(val+di[i-1][j-1])%mod;
}
}
}
// for(int i=1;i<=n;i++) {
// for(int j=1;j<=m;j++) cout<<dp[i][j]<<" ";
// cout<<endl;
// }
cout<<dp[n][m]%mod<<endl;
} | #include <bits/stdc++.h>
#define debug(x) cerr << #x << " = " << x << endl
using namespace std;
typedef long long LL;
const int MAXN = 55;
const int MOD = 1E9 + 7;
LL n, x, a[MAXN], g[MAXN], dp[MAXN][2];
template <class T>
void read(T& x) {
x = 0;
T f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') f = (ch == '-' ? -1 : 1), ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - 48, ch = getchar();
x *= f;
}
template <class T, class... Args>
void read(T& x, Args&... args) {
read(x), read(args...);
}
signed main() {
read(n, x);
for (int i = 1; i <= n; i++) read(a[i]);
for (int i = n; i >= 1; i--) {
if (!x) break;
g[i] = x / a[i], x %= a[i];
}
for (int i = n - 1; i >= 1; i--) {
LL lim = a[i + 1] / a[i];
if (g[i] == 0) {
dp[i][0] = dp[i + 1][0];
dp[i][1] = dp[i + 1][1] + dp[i + 1][0] + 1;
} else if (g[i] == lim - 1) {
dp[i][0] = dp[i + 1][0] + dp[i + 1][1] + 1;
dp[i][1] = dp[i + 1][1];
} else {
dp[i][1] = dp[i + 1][1] + dp[i + 1][0] + 1;
dp[i][0] = dp[i + 1][0] + dp[i + 1][1] + 1;
}
}
cout << dp[1][0] + 1;
return 0;
} |
#include <bits/stdc++.h>
#include <cmath>
#define fl(i,a,b) for(long long i=a;i<b;i++)
#define test long long t_c;cin>>t_c;for(long long ks=1;ks<=t_c;ks++)
#define pb push_back
#define vi vector<int>
#define all(x) x.begin(),x.end()
#define cy cout<<"YES\n";
#define cn cout<<"NO\n";
#define nl cout<<endl;
#define case(x) cout<<"Case "<<x<<": ";
typedef long long ll;
typedef long long int lli;
void sc(int &x){scanf("%d",&x);}
void sc(lli &x){scanf("%lld",&x);}
void sc(char *x){scanf("%s",x);}
void sc(double &x){scanf("%lf",&x);}
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
ll a,b,c,d;
cin>>a>>b>>c>>d;
if(c*d<=b)
{
cout<<-1<<endl;
}
else
{
ll x= ceil(a*1.0/(c*d-b)*1.0);
cout<<x<<endl;
}
}
| #include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define speed ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr)
#define all(vec) vec.begin(),vec.end()
#define int long long int
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
const int mod=1e9+7;
void solve(){
int a,b,c,d;
cin >> a >> b >> c >> d;
if(b >= c*d ){
cout<<-1;
return;
}
int x=0;
int cnt=0;
while(a > d * x)
{
a += b;
x += c;
cnt++;
}
cout<<cnt;
}
signed main(){
// #ifndef ONLINE_JUDGE
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// #endif
speed;
int t=1;
//cin>>t;
while(t--){
solve();
}
//cout << "\n\n"<<"Time elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << " seconds.\n";
return 0;
} |
// Problem : B - Abbreviate Fox
// Contest : AtCoder - AtCoder Regular Contest 108
// URL : https://atcoder.jp/contests/arc108/tasks/arc108_b
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
#include <bits/stdc++.h>
#define DEBUG fprintf(stderr, "Passing [%s] line %d\n", __FUNCTION__, __LINE__)
#define File(x) freopen(x".in","r",stdin); freopen(x".out","w",stdout)
using namespace std;
typedef long long LL;
typedef pair <int, int> PII;
typedef pair <int, PII> PIII;
template <typename T>
inline T gi()
{
T f = 1, x = 0; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return f * x;
}
const int INF = 0x3f3f3f3f, N = 200003, M = N << 1;
int n;
char s[N], stk[N];
int tp;
int main()
{
//File("");
n = gi <int> ();
scanf("%s", s + 1);
for (int i = 1; i <= n; i+=1)
{
stk[++tp] = s[i];
if (tp >= 3 && stk[tp] == 'x' && stk[tp - 1] == 'o' && stk[tp - 2] == 'f')
tp -= 3;
}
printf("%d\n", tp);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using vint = vector<int>;
using vll = vector<ll>;
using vld = vector<ld>;
using vpii = vector<pii>;
using vpil = vector<pil>;
using vpli = vector<pli>;
using vpll = vector<pll>;
#define x first
#define y second
#define all(v) v.begin(),v.end()
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
string s;
cin >> s;
vector<char> v;
for(char c : s) {
v.push_back(c);
if(v.size() >= 3 && v[int(v.size()) - 3] == 'f' && v[int(v.size()) - 2] == 'o'
&& v.back() == 'x') {
v.pop_back(); v.pop_back(); v.pop_back();
}
}
cout << v.size() << '\n';
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 510 + 10;
#define gcd __gcd
const int mod = 200;
const int inf = 0x3f3f3f3f;
#define x first
#define y second
int a[N];
void solve() {
int n;
scanf("%d", &n);
printf("%d\n", n / 100 + (n % 100 != 0));
}
int main() {
// freopen("in.txt", "r", stdin);
solve();
return 0;
} | // Problem C
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string.h>
#include <cassert>
#include <algorithm>
#include <set>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#define MAXN 1000
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long int ll;
typedef pair<int, int> ii;
int c[3];
int main() {
long long n;
cin >> n;
int ncifre = 0;
while (n!=0) {
c[(n%10)%3]++;
n = n/10;
ncifre++;
}
if ((c[1] + 2*c[2] )%3 == 0)
cout << "0\n";
else if ((c[1] + 2*c[2]) % 3 == 1) {
if (c[1] >= 1 && ncifre >= 2) cout << "1\n";
else if (c[2] >= 2 && ncifre >= 3) cout << "2\n";
else cout << "-1\n";
}
else {
if (c[2] >= 1 && ncifre >= 2) cout << "1\n";
else if (c[1] >= 2 && ncifre >= 3) cout << "2\n";
else cout << "-1\n";
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define MOD 998244353
#define ll long long
int main(){
ll a=0,b=0,c=0;
cin>>a>>b>>c;
ll moda = a*(a+1)/2, modb = b*(b+1)/2, modc = c*(c+1)/2;
ll modx = ((moda % MOD)*(modb % MOD))%MOD;
ll mody = ((modx % MOD)*(modc % MOD)% MOD);
cout<<mody<<endl;
} | #include <bits/stdc++.h>
using namespace std;
struct Fast {
Fast() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
}
} fast;
template <typename A, size_t N, typename T> void Fill(A (&array)[N], const T& val) {
std::fill((T*)array, (T*)(array + N), val);
}
// long long mod = 998244353;
#define REP(i, n) for (long long i = 0; i < (n); i++)
#define rep(i, a, n) for (long long i = a; i < (n); i++)
typedef long long ll;
typedef pair<ll, ll> P;
long long mod = 1000000007;
long long modpow(long long m, long long n, long long modvalue) {
if (n == 0) return 1;
if (n % 2 == 0) {
long long t = modpow(m, n / 2, modvalue);
return t * t % modvalue;
} else {
return modpow(m, n - 1, modvalue) * m % modvalue;
}
}
long long moddiv(long long a, long long b, ll modvalue) { return (a * modpow(b, mod - 2, modvalue)) % modvalue; }
const ll INF = 1LL << 60 + 2;
string change_binary(long long a) {
string ret;
for (int i = 31; i >= 0; i--) {
ret += (a >> i & 1) + '0';
}
return ret;
}
ll nCkmod(ll n, ll k, ll modvalue) {
ll numera = 1;
for (int i = n; i > n - k; i--) {
numera *= i;
numera %= modvalue;
}
ll denomi = 1;
REP(i, k) {
denomi *= (i + 1);
denomi %= modvalue;
}
return moddiv(numera, denomi, modvalue);
}
map<ll, ll> cnt;
void primeFactors(long long n) {
while (n % 2 == 0) {
cnt[2]++;
n = n / 2;
}
for (int i = 3; i <= sqrt(n); i = i + 2) {
while (n % i == 0) {
cnt[i]++;
n = n / i;
}
}
if (n > 2) {
cnt[n]++;
}
}
vector<ll> dijkstra(vector<vector<P>> graph, ll s) {
vector<ll> ans(graph.size(), -1);
priority_queue<P, vector<P>, greater<P>> pq;
pq.push({0, s});
while (pq.size() > 0) {
ll cur1 = 0;
ll cur2 = 0;
if (ans[pq.top().second] != -1) {
pq.pop();
} else {
cur1 = pq.top().first;
cur2 = pq.top().second;
pq.pop();
ans[cur2] = cur1;
for (ll i = 0; i < graph[cur2].size(); i++) {
pq.push({cur1 + graph[cur2][i].first, graph[cur2][i].second});
}
}
}
return ans;
}
vector<string> maze;
ll h, w;
bool check(ll y, ll x) {
if (x < 0 || x >= w || y < 0 || y >= h || maze[y][x] == '#') {
return false;
}
return true;
}
vector<vector<ll>> graph;
vector<vector<ll>> visited;
void dfs(ll curx, ll cury) {
if (-1 < curx && curx < h && -1 < cury && cury < w && visited[curx][cury] == 0) {
if (graph[curx][cury] == 1) {
// cout << curx << " " << cury << endl;
// cout << visited[curx][cury] << endl;
visited[curx][cury]++;
dfs(curx + 1, cury);
dfs(curx, cury + 1);
dfs(curx + 1, cury + 1);
dfs(curx + 1, cury - 1);
dfs(curx, cury - 1);
dfs(curx - 1, cury + 1);
dfs(curx - 1, cury);
dfs(curx - 1, cury - 1);
}
}
}
int main() {
ll A, B, C;
cin >> A >> B >> C;
ll x = ((A * (A + 1)) / 2) % 998244353;
ll y = ((B * (B + 1)) / 2) % 998244353;
ll z = ((C * (C + 1)) / 2) % 998244353;
cout << ((((x * y) % 998244353) * z) % 998244353) << endl;
} |
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
#include <queue>
#include <deque>
#include <iomanip>
#include <cmath>
#include <set>
#include <stack>
#include <map>
#include <unordered_map>
#include<cstring>
typedef unsigned long long ull;
typedef long long ll;
using namespace std;
void solve()
{
ll a,b,c,d;
cin>>a>>b>>c>>d;
ll cyan=a;
ll red=0;
ll ans=0;
if(c*d-b==0||c*d-b<0)
{
cout<<-1;
return;
}
while (cyan>d*red)
{
cyan+=b;
red+=c;
ans++;
}
cout<<ans;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t=1;
//cin>>t;
while (t--)
{
solve();
}
} | #include <bits/stdc++.h>
using namespace std;
#define int long long int
#define f first
#define s second
#define pb push_back
#define pob pop_back
#define mk make_pair
#define vi vector<int>
#define pii pair<int, int>
#define mod 1000000007
#define set_pre(x, y) fixed << setprecision(y) << x
#define w(t) \
int t; \
cin >> t; \
while (t--)
void init() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
void solve() {
int a, b, c, d, n = 0, k = 0, cnt = 0, ans = 0;
cin >> a >> b >> c >> d;
if (b >= c * d) {
cout << -1;
return;
}
cout << ceil((a * 1.0) / (c * d - b));
}
int32_t main() {
clock_t begin = clock();
init();
// Write your code here....
solve();
// code end.....
#ifndef ONLINE_JUDGE
clock_t end = clock();
std::cout << "\n\nExecuted In: " << double(end - begin) / CLOCKS_PER_SEC * 1000 << " ms";
#endif
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using i64 = int_fast64_t;
template <class Tp, class Comp = std::less<Tp>>
bool chle(Tp &x, const Tp &y, Comp comp = Comp()) {
return comp(y, x) ? x = y, true : false;
}
template <class Tp, class Comp = std::less<Tp>>
bool chge(Tp &x, const Tp &y, Comp comp = Comp()) {
return comp(x, y) ? x = y, true : false;
}
void solve() {
i64 n;
cin >> n;
while (~n & 1) {
n >>= 1;
}
i64 ans = 0;
for (i64 d = 1; d * d <= n; ++d) {
if (n % d) {
continue;
}
ans++;
if (d < n / d) {
++ans;
}
}
ans += ans;
cout << ans << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
solve();
}
| #include <iostream>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ll n;
scanf("%lld", &n);
ll cnt = 0;
for(ll i = 1; i * i <= n; i++)
{
if(n % i == 0)
{
if(i % 2 == 1 )
cnt++;
if(i != n / i && (n / i) % 2 == 1)
cnt++;
}
}
printf("%lld\n", cnt * 2);
return 0;
} |
#include <bits/stdc++.h>
#define ln '\n'
#define all(dat) dat.begin(), dat.end()
#define loop(i, to) for (int i = 0; i < to; ++i)
#define cont(i, to) for (int i = 1; i <= to; ++i)
#define circ(i, fm, to) for (int i = fm; i <= to; ++i)
#define foreach(i, dat) for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)
typedef long long num;
using namespace std;
template<int mod> struct modulo {
typedef modulo T;
int w;
operator int() const { return w; }
modulo() {}
modulo(int w) { this->w = w; fix(); }
template<class A> modulo(A w) { this->w = w % mod; fix(); }
inline void fix() { if (w >= mod || w < -mod) w %= mod; if (w < 0) w += mod; }
inline T operator - () const { return mod - w; }
inline T& operator += (const T &b) { if ((w += b.w) >= mod) w -= mod; return *this; }
inline T& operator -= (const T &b) { if ((w -= b.w) < 0) w += mod; return *this; }
inline T& operator *= (const T &b) { w = (unsigned long long) w * b.w % mod; return *this; }
inline T& operator /= (const T &b) { return *this *= b.inv(); }
#define make_operation(P)\
friend inline T operator P (const T &a, const T &b) { return T(a) P##= b; }\
template<class U> friend inline T operator P (const T &a, const U &b) { return T(a) P##= b; }\
template<class U> friend inline T operator P (const U &a, const T &b) { return T(a) P##= b; }
make_operation(+) make_operation(-) make_operation(*) make_operation(/)
#undef make_operation
inline T inv() const {
int a = w, m = mod, u = 0, v = 1, t;
for (; a != 0; t = m / a, m -= t * a, swap(a, m), u -= t * v, swap(u, v));
return u;
}
friend inline T pow(const T &w, num p) {
T a = w, r = 1; for (; p > 0; a *= a, p >>= 1) if (p & 1) r *= a; return r;
}
};
const int mod = 998244353;
typedef modulo<mod> rem;
const int nsz = 5e2;
char s[nsz + 5][nsz + 5];
int n, m, d[2 * nsz + 5];
rem ans = 1;
int main() {
scanf("%d%d", &n, &m);
cont (i, n) scanf("%s", s[i] + 1);
cont (i, n + m - 1) d[i] = -2;
cont (i, n) cont (j, m) if (s[i][j] != '.') {
int id = i + j - 1, c = s[i][j] == 'R' ? 0 : 1;
if (d[id] == -2) d[id] = c;
else if (d[id] != c) d[id] = -1;
}
cont (i, n + m - 1) {
if (d[i] == -1) ans = 0;
else if (d[i] == -2) ans *= 2;
}
printf("%d\n", ans.w);
} | #include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define ALL(x) (x).begin(),(x).end()
template<class T>bool umax(T &a, const T &b) {if(a<b){a=b;return 1;}return 0;}
template<class T>bool umin(T &a, const T &b) {if(b<a){a=b;return 1;}return 0;}
#ifdef LOCAL
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 eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...) 42
#define eprintf(...) 42
#endif
bool chikai(ll a,ll b,ll c,ll d) {
return abs(a-c)+abs(b-d) <= 3;
}
ll r1, c1, r2, c2;
ll solve() {
if(r1==r2 && c1==c2) return 0;
{
ll tasu1 = r1+c1;
ll tasu2 = r2+c2;
ll hiku1 = r1-c1;
ll hiku2 = r2-c2;
if(chikai(r1,c1,r2,c2)) return 1;
if(tasu1==tasu2) return 1;
if(hiku1==hiku2) return 1;
if((tasu1 %2) == (tasu2%2)) return 2;
if((hiku1 %2) == (hiku2%2)) return 2;
}
rep(x,-3,4) rep(y,-3,4) {
if(abs(x)+abs(y)>3) continue;
ll rr1 = r1+x;
ll cc1 = c1+y;
ll tasu1 = rr1+cc1;
ll tasu2 = r2+c2;
ll hiku1 = rr1-cc1;
ll hiku2 = r2-c2;
if(chikai(rr1,cc1,r2,c2)) return 2;
if(tasu1==tasu2) return 2;
if(hiku1==hiku2) return 2;
}
rep(x,-3,4) rep(y,-3,4) {
if(abs(x)+abs(y)>3) continue;
if(x+y>3) continue;
ll rr2 = r2+x;
ll cc2 = c2+y;
ll tasu1 = r1+c1;
ll tasu2 = rr2+cc2;
ll hiku1 = r1-c1;
ll hiku2 = rr2-cc2;
if(chikai(r1,c1,rr2,cc2)) return 2;
if(tasu1==tasu2) return 2;
if(hiku1==hiku2) return 2;
}
return 3;
}
int main() {
cin >> r1 >> c1 >> r2 >> c2;
cout << solve() <<endl;
}
|
#include <bits/stdc++.h>
#define ln '\n'
#define all(dat) dat.begin(), dat.end()
#define loop(i, to) for (int i = 0; i < to; ++i)
#define cont(i, to) for (int i = 1; i <= to; ++i)
#define circ(i, fm, to) for (int i = fm; i <= to; ++i)
#define foreach(i, dat) for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)
typedef long long num;
using namespace std;
const int nsz = 5e5;
char s[nsz + 5], t[nsz + 5];
int n, ans;
int main() {
scanf("%d", &n);
scanf("%s", s);
scanf("%s", t);
int c0 = 0, c1 = 0;
loop (i, n) {
c0 += s[i] == '0';
c1 += t[i] == '0';
}
if (c0 != c1) {
printf("-1\n");
return 0;
}
for (int i0 = 0, j0 = 0, i1 = 0, j1 = 0; i0 < n && i1 < n; i0 = j0 + 1, i1 = j1 + 1) {
for (j0 = i0; j0 < n && s[j0] == '1'; ++j0);
for (j1 = i1; j1 < n && t[j1] == '1'; ++j1);
ans += j0 != j1;
}
printf("%d\n", ans);
} | #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define ud unsigned int
#define ll long long
#define ull unsigned long long
#define MAX_INF 0x3f
#define MAX_INF_VAL 0x3f3f3f3f
#define MAX_INF_VAL_LL 0x3f3f3f3f3f3f3f3f
//#define pi 3.141592653589
#define eps 1e-9
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
//#define p 2173412051LL
//#define sz 2
using namespace std;
template< typename T > void read( T &x ) {
x = 0;
char ch = getchar();
ll f = 1;
while( !isdigit( ch ) ) {
if( ch == '-' )
f *= -1;
ch = getchar();
}
while( isdigit( ch ) ) {
x = x * 10 + ch - 48;
ch = getchar();
}
x *= f;
}
struct custom_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 );
}
};
char s[ 500010 ], t[ 500010 ];
vector< int > a, b;
int main() {
ios::sync_with_stdio( false );
cin.tie( 0 ), cout.tie( 0 );
int n;
cin >> n;
cin >> s + 1 >> t + 1;
for( int i = 1; i <= n; ++i ) {
if( s[ i ] == '0' )
a.push_back( i );
if( t[ i ] == '0' )
b.push_back( i );
}
if( a.size() != b.size() ) {
cout << "-1";
return 0;
}
int ans = 0;
for( int i = 0; i < a.size(); ++i ) {
if( a[ i ] != b[ i ] )
++ans;
}
cout << ans;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define P pair<ll, ll>
#define Graph vector<vector<ll>>
#define fi first
#define se second
#define vvvll vector<vector<vector<ll>>>
#define vvll vector<vector<ll>>
#define vll vector<ll>
constexpr ll INF = (1ll << 60);
constexpr ll mod = 1000000007;
constexpr double pi = 3.14159265358979323846;
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;
}
ll V;
ll G[20][20];
ll dp[200000][20];
int main() {
ll x[20],y[20],z[20];
cin >> V;
rep(i, V) {
cin>>x[i]>>y[i]>>z[i];
}
rep(i,V){
rep(j,V){
G[i][j]=abs(x[i]-x[j])+abs(y[i]-y[j])+max(0ll,z[j]-z[i]);
}
}
rep(i, 1 << V) {
rep(j, 20) { dp[i][j] = INF; }
}
dp[0][0] = 0;
rep(S, 1 << V) {
rep(v, V) {
rep(u, V) {
if (S != 0 && !(S & (1 << u))) continue; // u を含まない場合を除く
if ((S & (1 << v)) == 0) {
if (v != u) chmin(dp[S | (1 << v)][v], dp[S][u] + G[u][v]);
}
}
}
}
if (dp[(1 << V) - 1][0] != INF) {
cout << dp[(1 << V) - 1][0] << endl;
}
return 0;
} | #include <cstdio>
#include <iostream>
#define N 300005
using namespace std;
long long val[N];
inline long long read()
{
long long f(1),x(0);char a=getchar();
while(a!='-'&&(a<'0')||a>'9')a=getchar();
a=='-'?f=-1:x=a-'0';a=getchar();
while(a>='0'&&a<='9')x=(x<<3)+(x<<1)+a-'0',a=getchar();
return x*f;
}
struct node
{
long long val,lazy,l,r;
}tree[N<<2];
void build(long long root,long long l,long long r)
{
tree[root].l=l;tree[root].r=r;
if(l==r)
{
tree[root].val=val[r];
return;
}
long long mid=(l+r)/2;
build((root<<1),l,mid);build((root<<1)+1,mid+1,r);
tree[root].val=tree[(root<<1)].val ^ tree[(root<<1)+1].val;
}
void pushdown(long long root)
{
if(tree[root].lazy)
{
tree[(root<<1) ].lazy^=tree[root].lazy;
tree[(root<<1)+1].lazy^=tree[root].lazy;
tree[(root<<1) ].val^=tree[root].lazy*(tree[(root<<1) ].r-tree[(root<<1) ].l+1);
tree[(root<<1)+1].val^=tree[root].lazy*(tree[(root<<1)+1].r-tree[(root<<1)+1].l+1);
tree[root].lazy=0;
}
}
void update(long long root,long long l,long long r,long long val)
{
if(tree[root].l>r||tree[root].r<l)return;
if(tree[root].r<=r&&tree[root].l>=l)
{
tree[root].lazy^=val;
tree[root].val^=val*(tree[root].r-tree[root].l+1);
return;
}
pushdown(root);
update((root<<1),l,r,val);update((root<<1)+1,l,r,val);
tree[root].val=tree[(root<<1)].val^tree[(root<<1)+1].val;
}
long long queryval(long long root,long long l,long long r)
{
if(tree[root].l>r||tree[root].r<l)return 0;
if(tree[root].r<=r&&tree[root].l>=l)return tree[root].val;
pushdown(root);
return queryval((root<<1),l,r)^queryval((root<<1)+1,l,r);
}
int main()
{
long long n,m;
n=read();m=read();
for(int i=1;i<=n;i++)val[i]=read();
build(1,1,n);
while(m--)
{
long long opt=read();
if(opt==1)
{
long long x=read(),k=read();
update(1,x,x,k);
}
else
{
long long x=read(),y=read();
printf("%lld\n",queryval(1,x,y));
}
}
return 0;
} |
#include <bits/stdc++.h>
#define rep(a,n) for (ll a = 0; a < (n); ++a)
using namespace std;
//using namespace atcoder;
using ll = long long;
typedef pair<ll,ll> P;
typedef pair<ll,P> PP;
typedef vector<vector<int> > Graph;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const ll INF = 1e18;
#define debug(v) cout<<#v<<": ",prt(v);
template <typename A,typename B>
inline void prt(pair<A,B> p){cout<<"("<<p.first<<", "<<p.second<<")\n";}
template <typename A,typename B,typename C>
inline void prt(tuple<A,B,C> p){cout<<"("<<get<0>(p)<<", "<<get<1>(p)<<", "<<get<2>(p)<<")\n";}
inline void prt(bool p){if(p)cout<<"True"<<'\n';else cout<<"False"<<'\n';}
template <typename T>
inline void prt(vector<T> v){cout<<'{';for(ll i=0;i<v.size();i++){cout<<v[i];if(i<v.size()-1)cout<<", ";}cout<<'}'<<'\n';}
template<typename T>
inline void prt(vector<vector<T> >& vv){ for(const auto& v : vv){ prt(v); } }
template <typename T>
inline void prt(deque<T> v){cout<<'{';for(ll i=0;i<v.size();i++){cout<<v[i];if(i<v.size()-1)cout<<", ";}cout<<'}'<<'\n';}
template <typename A,typename B>
inline void prt(map<A,B> v){cout<<'{';ll c=0;for(auto &p: v){cout<<p.first<<":"<<p.second;c++;if(c!=v.size())cout<<", ";}cout<<'}'<<'\n';}
template <typename A,typename B>
inline void prt(unordered_map<A,B> v){cout<<'{';ll c=0;for(auto &p: v){cout<<p.first<<":"<<p.second;c++;if(c!=v.size())cout<<", ";}cout<<'}'<<'\n';}
template <typename T>
inline void prt(set<T> v){cout<<'{';for(auto i=v.begin();i!=v.end();i++){cout<<*i;if(i!=--v.end())cout<<", ";}cout<<'}'<<'\n';}
template <typename T>
inline void prt(multiset<T> v){cout<<'{';for(auto i=v.begin();i!=v.end();i++){cout<<*i;if(i!=--v.end())cout<<", ";}cout<<'}'<<'\n';}
// auto mod int
// depends on Template
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);}
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;}
long long modpow(long long a, long long n) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
int main(){
ll n,m;
cin >> n >> m;
vector<ll>a(n);
rep(i,n)cin>>a[i];
mint ans = 1;
ll s = 0;
rep(i,n)s+=a[i];
for(ll i = m+n;i>m-s;i--){
ans *= i;
}
for(ll i = s+n;i>=1;i--){
ans /= i;
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int n, x;
void solve(int Case) {
scanf("%d %d", &n, &x);
x *= 100;
int ans = -1;
int v, p, y = 0;
for (int i = 0; i < n; ++i) {
scanf("%d %d", &v, &p);
y += v * p;
if (ans == -1 && y > x) ans = i + 1;
}
printf("%d\n", ans);
}
int main () {
int T = 1;
// scanf("%d", &T);
for (int t = 1; t <= T; ++t) solve(t);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;
#define mp make_pair
#define fr first
#define sc second
// full credit ecnerwala
template <int& MOD_>
struct modint {
static constexpr int& MOD = MOD_;
// static_assert(MOD_ > 0, "MOD must be positive");
private:
using ll = long long;
int v;
static int minv(int a, int m) {
a %= m;
assert(a);
return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a);
}
public:
modint() : v(0) {}
modint(ll v_) : v(int(v_ % MOD)) {
if (v < 0) v += MOD;
}
explicit operator int() const { return v; }
friend std::ostream& operator<<(std::ostream& out, const modint& n) {
return out << int(n);
}
friend std::istream& operator>>(std::istream& in, modint& n) {
ll v_;
in >> v_;
n = modint(v_);
return in;
}
friend bool operator==(const modint& a, const modint& b) {
return a.v == b.v;
}
friend bool operator!=(const modint& a, const modint& b) {
return a.v != b.v;
}
modint inv() const {
modint res;
res.v = minv(v, MOD);
return res;
}
friend modint inv(const modint& m) { return m.inv(); }
modint neg() const {
modint res;
res.v = v ? MOD - v : 0;
return res;
}
friend modint neg(const modint& m) { return m.neg(); }
modint operator-() const { return neg(); }
modint operator+() const { return modint(*this); }
modint& operator++() {
v++;
if (v == MOD) v = 0;
return *this;
}
modint& operator--() {
if (v == 0) v = MOD;
v--;
return *this;
}
modint& operator+=(const modint& o) {
v -= MOD - o.v;
v = (v < 0) ? v + MOD : v;
return *this;
}
modint& operator-=(const modint& o) {
v -= o.v;
v = (v < 0) ? v + MOD : v;
return *this;
}
modint& operator*=(const modint& o) {
v = int(ll(v) * ll(o.v) % MOD);
return *this;
}
modint& operator/=(const modint& o) { return *this *= o.inv(); }
friend modint operator++(modint& a, int) {
modint r = a;
++a;
return r;
}
friend modint operator--(modint& a, int) {
modint r = a;
--a;
return r;
}
friend modint operator+(const modint& a, const modint& b) {
return modint(a) += b;
}
friend modint operator-(const modint& a, const modint& b) {
return modint(a) -= b;
}
friend modint operator*(const modint& a, const modint& b) {
return modint(a) *= b;
}
friend modint operator/(const modint& a, const modint& b) {
return modint(a) /= b;
}
modint qpow(ll pow) const {
modint ans = 1, base = v;
while (pow) {
if (pow & 1) ans *= base;
base *= base;
pow >>= 1;
}
return ans;
}
friend modint qpow(const modint& a, ll pow) { return a.qpow(pow); }
};
int MOD;
void solve() {
ll N;
int M;
cin >> N >> M;
MOD = M * M;
using mint = modint<MOD>;
cout << (int)qpow(mint(10), N) / M % M << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t = 1;
// cin >> t;
while (t--) solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define all(x) (x).begin(),(x).end()
using ll = long long;
using P = pair<ll, ll>;
string char_to_string(char val) {
return string(1, val);
}
int char_to_int(char val) {
return val - '0';
}
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;
}
struct edge {
ll to, cost;
};
int main() {
ll n; cin >> n;
ll N = n + 1;
ll res = 0; ll i = 1;
while(N > res) {
res += i;
if(N < res) break;
++i;
}
if(n == 2) cout << 1 << endl;
else cout << n - (i - 1) + 1 << endl;
} |
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,x;
cin>>n>>x;
vector<int> vec;
int c;
for(int i=1;i<=n;i++)
{
cin>>c;
vec.push_back(c);
}
auto it=find(vec.begin(),vec.end(),x);
while(it!=vec.end())
{
vec.erase(it);
it=find(vec.begin(),vec.end(),x);
}
for(auto i:vec)cout<<i<<" ";
return 0;
} | #include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define nl cout<<endl
#define pii pair<ll,ll>
#define vi vector<ll>
#define vii vector<pii>
#define mi map<ll,ll>
#define all(a) (a).begin(),(a).end()
#define pb push_back
#define ff first
#define ss second
#define hell 1000000007
#define test4(x,y,z,a) cout<<"x is "<<x<<" y is "<<y<<" z is "<<z<<" a is "<<a<<endl;
#define test3(x,y,z) cout<<"x is "<<x<<" y is "<<y<<" z is "<<z<<endl;
#define test2(x,y) cout<<"x is "<<x<<" y is "<<y<<endl;
#define test1(x) cout<<"x is "<<x<<endl;
#define N 300009
ll power(ll a,ll b,ll m)
{
ll ans=1;
while(b)
{
if(b&1)
ans=(ans*a)%m;
b/=2;
a=(a*a)%m;
}
return ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll n;cin>>n;
vector<pair<ll,string>> v;
rep(iu,0,n) {
string x;
ll y;
cin>>x>>y;
v.pb({y,x});
}
sort(all(v));
reverse(all(v));
cout << v[1].ss << endl;
} |
#include <bits/stdc++.h>
#ifdef LOCAL
#include "debuger.hpp"
#else
#define DUMP(...) 1145141919810
#define CHECK(...) (__VA_ARGS__)
#endif
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
printf("%d %d\n", 2 * i % n + 1, (2 * i + 1) % n + 1);
}
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;
template<class T>
void debug(T itr1, T itr2) { auto now = itr1; while(now<itr2) { cout << *now << " "; now++; } cout << endl; }
#define repr(i,from,to) for (ll i=(ll)from; i<(ll)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i,N) repr(i,0,N)
#define per(i,N) for (ll i=(ll)N-1; i>=0; i--)
const ll MOD = pow(10,9)+7;
const ll LLINF = pow(2,61)-1;
const ll INF = pow(2,30)-1;
vector<ll> fac;
void c_fac(ll x=pow(10,7)+10) { fac.resize(x,true); rep(i,x) fac[i] = i ? (fac[i-1]*i)%MOD : 1; }
ll inv(ll a, ll m=MOD) { ll b = m, x = 1, y = 0; while (b!=0) { ll d = a/b; a -= b*d; swap(a,b); x -= y*d; swap(x,y); } return (x+m)%m; }
ll nck(ll n, ll k) { return fac[n]*inv(fac[k]*fac[n-k]%MOD)%MOD; }
ll modpow(ll x, ll p) { ll result = 1, now = 1, pm = x; while (now<=p) { if (p&now) { result = result * pm % MOD; } now*=2; pm = pm*pm % MOD; } return result; }
ll gcd(ll a, ll b) { if (a<b) swap(a,b); return b==0 ? a : gcd(b, a%b); }
ll lcm(ll a, ll b) { return a/gcd(a,b)*b; }
int main() {
ll N; cin >> N;
rep(i,N) {
ll a = i*2, b = i*2+1;
ll tca = 0; while ((1<<tca)<=a) tca++;
while (a>=N) { if (a&(1<<tca)) a ^= (1<<tca); tca--; }
ll tcb = 0; while ((1<<tcb)<=b) tcb++;
while (b>=N) { if (b&(1<<tcb)) b ^= (1<<tcb); tcb--; }
a++; b++;
cout << a << " " << b << endl;
}
return 0;
} |
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
using ll=int;
using ld=long double;
using pll=pair<ll, ll>;
//using mint = modint1000000007;
#define rep(i,n) for (ll i=0; i<n; ++i)
#define all(c) begin(c),end(c)
#define PI acos(-1)
#define oo 2e18
template<typename T1, typename T2>
bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}
template<typename T1, typename T2>
bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}
//priority_queue<ll, vector<ll>, greater<ll>> Q;
/*
01234でタイルの方向管理?
*/
#define TIMEOVER_M 15000
#define TIMEOVER 1980000
#define DIV 500
ll T[50][50];
bool B[50][50];
ll P[50][50];
ll D[50][50];
char *ans;
char *W;
ll score=0;
ll high=0;
ll dx[]={0, 1, 0, -1};
ll dy[]={-1, 0, 1, 0};
char dc[]={'U', 'R', 'D', 'L'};
bool all_fin;
bool fin;
clock_t start;
clock_t start_m;
ll loop;
ll node=0;
void steped(ll y, ll x, bool b){
ll d=D[y][x];
if (d){
--d;
ll ny=y+dy[d];
ll nx=x+dx[d];
B[ny][nx]=b;
}
B[y][x] = b;
}
// 行き止まりになったら、その時点でのstringを出力
void dfs(ll y, ll x, ll c){// {y, x, 歩数}
bool used[4]={};
rep(i, 4){
clock_t now = clock();
if(now-start_m>TIMEOVER_M) fin=true;
if(now-start>TIMEOVER) all_fin=true;
ll r=(node++/DIV)%4;
while(used[r]) (r+=1)%=4;
used[r]=true;
ll ny=y+dy[r];
ll nx=x+dx[r];
// cout << "time:" << now-start << endl;
if(ny<0 || nx<0 || ny>49 || nx>49 || B[ny][nx] || fin || all_fin){
if(chmax(high, score)){
if(ans) free(ans);
ans=strdup(W);
}
}
else{
//反映
score+=P[ny][nx];
W[c]=dc[r];
steped(ny, nx, true);
dfs(ny, nx, c+1);
//復元
score-=P[ny][nx];
W[c]='\0';
steped(ny, nx, false);
}
}
}
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
cout << fixed << setprecision(10);
W=(char*)calloc(2000, 1);
start = clock();
ll si, sj;
cin >> si >> sj;
rep(i, 50) rep(j, 50) cin >> T[i][j];
rep(i, 50) rep(j, 50) cin >> P[i][j];
// タイルの方向D
rep(i, 50) rep(j, 50){
rep(k, 4){
ll ny=i+dy[k];
ll nx=j+dx[k];
if(ny<0 || nx<0 || ny>49 || nx>49) continue;
if(T[i][j]==T[ny][nx]) D[i][j]=k+1;
}
}
while(!all_fin){
loop++;
fin=false;
start_m = clock();
score = P[si][sj];
chmax(high, score);
rep(y, 50) rep(x, 50) B[y][x]=false;
rep(i, 2000) W[i]='\0';
steped(si, sj, true);
dfs(si, sj, 0);
}
// cout << high << " " << loop << " " << node << endl;
printf("%s\n", ans);
}
| #include <bits/stdc++.h>
using namespace std;
#define d(x) cerr << #x ":" << x << endl;
#define dd(x, y) cerr << "(" #x "," #y "):(" << x << "," << y << ")" << endl
#define rep(i, n) for (int i = (int)(0); i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define dump(v) \
cerr << #v ":[ "; \
for (auto macro_vi : v) { \
cerr << macro_vi << " "; \
} \
cerr << "]" << endl;
#define ddump(v) \
cerr << #v ":" << endl; \
for (auto macro_row : v) { \
cerr << "["; \
for (auto macro__vi : macro_row) { \
cerr << macro__vi << " "; \
} \
cerr << "]" << endl; \
}
using lint = long long;
const int INF = 1e9;
const lint LINF = 1e18;
const double EPS = 1e-10;
int main() {
int N;
string T;
cin >> N >> T;
string s = "";
rep(i, 4 * N) { s += "110"; }
lint len = 3;
lint sum = 1;
rep(i, 10) {
len *= 10;
sum *= 10;
}
if (s.substr(0, N) != T and s.substr(1, N) != T and s.substr(2, N) != T) {
cout << 0 << endl;
return 0;
}
if (T == "11") {
cout << sum << endl;
} else if (T == "10") {
cout << sum << endl;
} else if (T == "01") {
cout << sum - 1 << endl;
} else if (T == "0") {
cout << sum << endl;
} else if (T == "1") {
cout << sum * 2 << endl;
} else {
// 必ず0がいる
lint from, to;
if (T[0] == '0') {
from = 2;
} else if (T.substr(0, 2) == "11") {
from = 0;
} else if (T.substr(0, 2) == "10") {
from = 1;
}
if (T.back() == '0') {
to = len;
} else if (T.substr(N - 2, 2) == "11") {
to = len - 1;
} else if (T.substr(N - 2, 2) == "01") {
to = len - 2;
}
cout << ((to - from) - N) / 3 + 1 << endl;
}
// d(sum);
// d(len);
// dump(s);
return 0;
} |
#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < n; i++)
#define repr(i, n) for(int i = n; i >= 0; i--)
#define reps(i,a,b) for(int i= a; i < b; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
int main()
{
int a, b, c = 0, d = 0;
cin >> a >> b;
while(a) {
c += a % 10;
a /= 10;
}
while(b) {
d += b % 10;
b /= 10;
}
cout << max(c,d) << endl;
return 0;
} | /*
* Author : Arman Sykot
* Date & time : 02.01.2021 18:00:36 +06
*/
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define I_am_the_one_who_KNOCKS ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
const double EPS = 1e-9;
int main() {
#ifndef ONLINE_JUDGE
freopen ("in.txt", "r", stdin);
freopen ("out.txt", "w", stdout);
#endif
I_am_the_one_who_KNOCKS;
int a, b;
cin >> a >> b;
int suma = 0, sumb = 0;
while (a) {
int rem = a % 10;
a /= 10;
suma += rem;
}
while (b) {
int rem = b % 10;
b /= 10;
sumb += rem;
}
if (suma > sumb)
cout << suma << "\n";
else
cout << sumb << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main(void){
int a;
double b;
cin>>a>>b;
cout<<a*b/100.0<<endl;
} | #line 1 "main.cpp"
#include <bits/stdc++.h>
using namespace std;
// template {{{
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
#define range(i, l, r) for (i64 i = (i64)(l); i < (i64)(r); (i) += 1)
#define rrange(i, l, r) for (i64 i = (i64)(r) - 1; i >= (i64)(l); (i) -= 1)
#define whole(f, x, ...) ([&](decltype((x)) container) { return (f)( begin(container), end(container), ## __VA_ARGS__); })(x)
#define rwhole(f, x, ...) ([&](decltype((x)) container) { return (f)( rbegin(container), rend(container), ## __VA_ARGS__); })(x)
#define debug(x) cerr << "(" << __LINE__ << ")" << #x << ": " << (x) << endl
constexpr i32 inf = 1001001001;
constexpr i64 infll = 1001001001001001001ll;
constexpr i32 dx[] = {0, -1, 1, 0, -1, 1, -1, 1};
constexpr i32 dy[] = {-1, 0, 0, 1, -1, -1, 1, 1};
struct IoSetup { IoSetup(i32 x = 15){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(x); cerr << fixed << setprecision(x); } } iosetup;
template <typename T = i64> T input() { T x; cin >> x; return x; }
template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { range(i, 0, v.size()) { os << v[i] << (i + 1 != (int)v.size() ? " " : ""); } return os; }
template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; }
template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; }
template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }
// }}}
void solve() {
int n = input();
vector< i64 > as(n);
cin >> as;
for (auto &a : as) a *= 2;
whole(sort, as);
i64 x = as[(n-1)/2] / 2;
i64 ans = x * n + whole(accumulate, as, 0ll);
for (auto &a : as) {
ans -= min(a, 2 * x);
}
cout << (long double)ans / n / 2 << endl;
}
signed main() {
solve();
}
|
// C++(GCC 9.2.1)
// 解き直し.
// https://atcoder.jp/contests/arc108/editorial/348
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using P = pair<int, int>;
#define repex(i, a, b, c) for(int i = a; i < b; i += c)
#define repx(i, a, b) repex(i, a, b, 1)
#define rep(i, n) repx(i, 0, n)
#define repr(i, a, b) for(int i = a; i >= b; i--)
#define pb push_back
#define a first
#define b second
int main(){
// 1. 入力情報.
int N, M;
scanf("%d %d", &N, &M);
vvi G(N);
map<P, int> m; // グラフの辺情報(最大一つ)を保存.
rep(i, M){
int u, v, c;
scanf("%d %d %d", &u, &v, &c);
u--, v--;
G[u].pb(v);
G[v].pb(u);
m[{u, v}] = c;
m[{v, u}] = c;
}
// 2. 頂点 1 から 各頂点までの距離を計算.
// https://ja.wikipedia.org/wiki/幅優先探索
function<void(vvi&, int, int*)> bfs = [&](vvi &G, int s, int* d){
// 2-1. 空のキュー.
queue<int> q;
// 2-2. 探索地点 s をキュー q に追加.
q.push(s);
while(!q.empty()){
// 2-3. キューから取り出す.
int u = q.front();
q.pop();
// 2-4. 取り出した要素を処理.
for(auto &e : G[u]){
// 2-5. 訪問済であれば, 処理をスキップ.
if(d[e]) continue;
if(!d[e] && e != s) d[e] = d[u] + 1, q.push(e);
}
}
return;
};
int d[N];
memset(d, 0, sizeof(d));
bfs(G, 0, d);
// rep(i, N) printf("d[%d]=%d\n", i, d[i]);
// 3. 頂点 1 から 近い順 に並び替え.
// -> {距離, 頂点番号}
priority_queue<P, vector<P>, greater<P>> pq;
rep(i, N) pq.push({d[i], i});
// 4. 各頂点に, 整数を書き込む.
int ans[N];
memset(ans, 0, sizeof(ans));
ans[0] = 1; // 頂点 1 に 1 を書き込む.
while(!pq.empty()){
// 4-1. 頂点 v を一つ取り出す.
P p = pq.top();
pq.pop();
// 4-2. 頂点 v の 頂点番号は?
int v = p.b;
// 4-3. 頂点 v を 根とする部分木 について, 整数を書き込む.
for(auto &u : G[v]){
// 4-4. 頂点 v に 書き込まれた整数.
int nv = ans[v];
// 4-5. 頂点 v の 子供 に 相当する頂点 u を 選択し, 書き込み.
if(d[u] > d[v] && !ans[u]){
// 4-6. 辺のラベル.
int ne = m[{v, u}];
// 4-7. 辺のラベルと比較して, 書き込む整数を選択.
if(nv == ne) ans[u] = (nv == 1) ? N : 1;
else ans[u] = ne;
}
}
}
// 5. 出力.
rep(i, N) printf("%d\n", ans[i]);
return 0;
} | // Author : Sarthak Kapoor
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int ll
#define rep(i,n) for(int i=0;i<n;++i)
#define repa(i,a,n) for(int i=a;i<n;++i)
#define repr(i,n) for(int i=n-1;i>=0;--i)
#define ll long long
#define pi pair<int,int>
#define pii pair<pi,int>
#define vi vector<int>
#define pb push_back
#define fi first
#define sec second
#define all(v) v.begin(),v.end()
#define s(v) v.size()
ll mod = 1000000007;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());//use rng()%n for numbers in range [0,n-1]
ll gcd(ll a,ll b){
if(b==0)return a;
return gcd(b,a%b);
}
ll fastexp(ll x,ll a){
ll res = 1;
while(a>0){
if(a&1){
res = (res*x)%mod;
}
a=a>>1;
x=(x*x)%mod;
}
return res;
}
ll inverse(ll n){
return fastexp(n,mod-2);
}
template <typename T>
void add(T &a, T b){
a += b;
if(a >= mod)a -= mod;
}
template <typename T>
void sub(T &a, T b){
a -= b;
if(a < 0)a += mod;
}
template <typename T>
void mul(T &a, T b){
a *= b;
if(a >= mod)a %= mod;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,x;
cin>>n>>x;
vi v(n);
rep(i,n){
cin>>v[i];
}
map<int,int> mp;
mp[x] = 1;
rep(i,n-1){
map<int,int> newmp;
for(auto x:mp){
// x.fi ke sabse pass vaale 2 numbers which
// are divisible by v[i+1] (might be 1)
int num = x.fi;
int r = num%v[i+1];
if(r == 0){
newmp[num] += x.sec;
}
else{
newmp[num-r] += x.sec;
newmp[num+v[i+1]-r] += x.sec;
}
}
mp = newmp;
}
int ans = 0;
for(auto x:mp){
ans += x.sec;
}
cout<<ans;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
const int N = 321123;
int n, k, ans, black[N], white[N];
vector<int> g[N];
void dfs(int v, int p, int x) {
black[v] = N;
white[v] = 0;
for (auto u : g[v]) {
if (u == p) continue;
dfs(u, v, x);
if (black[v] + 1 + white[u] <= x)
white[u] = -1;
if (black[u] + 1 + white[v] <= x)
white[v] = -1;
if (white[u] != -1)
white[v] = max(white[v], white[u] + 1);
black[v] = min(black[v], black[u] + 1);
}
if (white[v] == x || (white[v] != -1 && v == 1)) {
ans++;
white[v] = -1;
black[v] = 0;
}
}
int main() {
cin >> n >> k;
for (int i = 0; i < n - 1; i++) {
int a, b; cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
int l = 1, r = n - 1;
while (l < r) {
int m = (l + r) / 2;
ans = 0;
dfs(1, 0, m);
if (ans <= k)
r = m;
else
l = m + 1;
}
cout << l << "\n";
return 0;
}
| #pragma GCC optimize ("O2")
#pragma GCC target ("avx2")
//#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please
int to[400001], ne[400001], he[200001];
int Q[200001], pl[200001], dl[200001];
const int CM = 1 << 17, CL = 12;
char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint() {
if (ci - owa > 0) {
memcpy(cn, owa, CL);
ci -= CM;
fread(cn + CL, 1, CM, stdin);
}
ll tmp = *(ll*)ci;
int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0);
tmp = tmp << dig & ma1;
tmp = tmp * 10 + (tmp >> 8) & ma2;
tmp = tmp * 100 + (tmp >> 16) & ma3;
tmp = tmp * 10000 + (tmp >> 32) & ma4;
ci += 72 - dig >> 3;
return tmp;
}
int main() {
//cin.tie(0);
//ios::sync_with_stdio(false);
int N = getint(), K = getint();
int k = 1;
rep(i, N - 1) {
int u = getint(), v = getint();
to[k] = v;
ne[k] = he[u];
he[u] = k++;
to[k] = u;
ne[k] = he[v];
he[v] = k++;
}
int q = 0, p = 0;
Q[q++] = 1;
while (p < N) {
int u = Q[p++];
int tmp = 0;
int ind = he[u];
he[u] = 0;
while (ind) {
int v = to[ind];
tmp = ne[ind];
if (he[v] > 0) {
Q[q++] = v;
ne[ind] = he[u];
he[u] = -ind;
}
ind = tmp;
}
}
int are = 1000000, pd0 = are;
int L = 0, R = (N + K - 1) / K;
while (L + 1 < R) {
int wj = (L + R) / 2;
int num = 0;
for (int i = N - 1; i >= 0; i--) {
int po = pd0 - 1;
int dd = pd0;
int u = Q[i];
for (int ind = -he[u]; ind; ind = -ne[ind]) {
int v = to[ind];
if (po < pl[v]) po = pl[v];
if (dd < dl[v]) dd = dl[v];
}
if (po >= dd) dd = 0;
if (dd >= pd0 + wj) {
num++;
po = pd0 + wj;
dd = 0;
}
pl[u] = po - 1;
dl[u] = dd + 1;
if (num > K) break;
}
int are = num + (dl[1] > pd0);
if (are > K) L = wj;
else R = wj;
pd0 += are;
}
printf("%d", R);
Would you please return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void solve() {
string s;
cin >> s;
ll m;
cin >> m;
ll maxi = 0LL;
for (char c : s) {
maxi = max(maxi, (ll)c - '0');
}
maxi += 1;
if (s.size() == 1) {
cout << (m >= stoll(s) ? 1 : 0) << '\n';
return;
}
ll n = (int)s.size();
auto check = [&](ll x) {
if((n + 1) > m * logl(x)) return false;
ll cp = 0, res = 1LL;
for (ll i = n - 1LL; i >= 0LL; i--) {
cp += (ll)(s[i] - '0') * res;
if (res > m or cp > m) return false;
res *= x;
}
if (cp <= m) return true;
return false;
};
if(!check(maxi)) return void(cout << 0 << "\n");
ll l = maxi, r = maxi, ans;
while(check(r)) r <<= 1;
while (l <= r) {
ll mid = l + (r - l) / 2LL;
if (check(mid)) {
ans = mid;
l = mid + 1LL;
} else {
r = mid - 1LL;
}
}
cout << max(0LL, ans - maxi + 1) << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int t = 1;
//cin >> t;
for (int tt = 1; tt <= t; ++tt) {
//cout << "Case #" << tt << ": ";
solve();
}
return 0;
} //Hajimemashite
| #include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <cstdint>
bool IsFeasible(const std::vector<int>& digits, std::int64_t base, std::int64_t M) {
std::int64_t X = 0;
for (const auto digit : digits) {
if (X > M / base) {
return false;
}
X = X * base + digit;
}
return X <= M;
}
int main() {
std::string X;
std::int64_t M;
std::cin >> X >> M;
const int D = X.size();
if (D < 2) {
std::cout << (std::stoi(X) <= M) << std::endl;
return 0;
}
std::vector<int> digits(D);
for (int i = 0; i < D; ++i) {
digits[i] = X[i] - '0';
}
const std::int64_t d = *std::max_element(digits.begin(), digits.end());
std::int64_t feasible = d;
for (std::int64_t b = static_cast<std::int64_t>(1) << 60; b; b >>= 1) {
const auto base = feasible + b;
if (IsFeasible(digits, base, M)) {
feasible = base;
}
}
std::cout << (feasible - d) << std::endl;
return 0;
}
|
#include <cstdio>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <tuple>
#include <bitset>
#include <complex>
using namespace std;
//typedef long long ll;
//typedef pair<ll, ll> P;
//const ll INF = 1000000000000000000;
//const ll MOD = 1000000000 + 7;
//typedef pair<int, int> P;
//const int INF = 1000000001;
//const int MOD = 1000000000 + 7;
//const int MAX_N = 100000 + 10;
//int a[MAX_N];
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int N, K;
cin >> N >> K;
int ans = 0;
for(int n = 1; n <= N; n++) {
for (int k = 1; k <= K; k++) {
int tmp = 100*n + k;
ans += tmp;
}
}
cout << ans << endl;
return 0;
}
| #include<iostream>
using namespace std;
int main()
{
int A,B=0;
int maxFollow=0;
cin >> A;
cin >> B;
maxFollow = 2*A + 100;
int diff = maxFollow - B;
cout << diff << endl;
} |
#include <iostream>
#include <map>
#include <algorithm>
#include <vector>
using namespace std;
#define ll long long
vector<ll> vec;
void solve(){
for(char i = '1';i<='9';i++){
string temp = "";
temp += i;
temp += temp;
vec.push_back(stoll(temp));
}
for(char i = '1';i<='9';i++){
for(char j = '0';j<='9';j++){
string temp = "";
temp += i;
temp += j;
temp += temp;
vec.push_back(stoll(temp));
}
}
for(char i = '1';i<='9';i++){
for(char j = '0';j<='9';j++){
for(char k = '0';k<='9';k++){
string temp = "";
temp += i;
temp += j;
temp += k;
temp += temp;
vec.push_back(stoll(temp));
}
}
}
for(char i = '1';i<='9';i++){
for(char j = '0';j<='9';j++){
for(char k = '0';k<='9';k++){
for(char l = '0';l<='9';l++){
string temp = "";
temp += i;
temp += j;
temp += k;
temp += l;
temp += temp;
vec.push_back(stoll(temp));
}
}
}
}
for(char i = '1';i<='9';i++){
for(char j = '0';j<='9';j++){
for(char k = '0';k<='9';k++){
for(char l = '0';l<='9';l++){
for(char m = '0';m<='9';m++){
string temp = "";
temp += i;
temp += j;
temp += k;
temp += l;
temp += m;
temp += temp;
vec.push_back(stoll(temp));
}
}
}
}
}
for(char i = '1';i<='9';i++){
for(char j = '0';j<='9';j++){
for(char k = '0';k<='9';k++){
for(char l = '0';l<='9';l++){
for(char m = '0';m<='9';m++){
for(char n = '0';n<='9';n++){
string temp = "";
temp += i;
temp += j;
temp += k;
temp += l;
temp += m;
temp += n;
temp += temp;
vec.push_back(stoll(temp));
}
}
}
}
}
}
sort(vec.begin(),vec.end());
ll n;
cin>>n;
int c = 0;
for(int i = 0;i<vec.size();i++){
if(vec[i] > n)break;
c++;
}
cout<<c;
}
int main(){
int t = 1;
while(t--){
solve();
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG //これつけるとA[N]でもいいらしい
//for文のマクロ
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define mp(a,b) make_pair(a,b)
#define big 1000000007
#define all(a) sort((a).begin(),(a).end()) //ソートのマクロ
#define Re(a) reverse((a).begin(),(a).end())
#define YN(a) if(a){cout<<"Yes"<<endl;}else cout<<"No"<<endl;//条件によってYes、Noを出力する
int main(){
#define int int64_t
int n;
cin>>n;
int ans=0;
for(int i=1;i<=n;i++){
string s = to_string(i);
int num = stoll(s+s);
//cout<<num<<endl;
if(num>n){
break;
}
ans++;
}
cout<<ans<<endl;
return 0;
} |
#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
typedef unsigned long long int ull;
typedef long int li;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define test ll t; cin >> t; while(t--)
#define print_tc(t, c) cout << "Case #" << t << ": " << c << "\n";
const long long int dx[4] = {0, 0, -1, 1}, dy[4] = {1, -1, 0, 0};
const long long int cons = 100005, ncr_cons = 100005;
const long long int MOD = 1000000007; // const long long int MOD = 998244353;
const long long int const_INT_MAX = 1000000000000000000, const_INT_MIN = -1 * 1000000000000000000;
using namespace std;
bool sortinrev(const pair<ll,ll> &a, const pair<ll,ll> &b){return (a.first > b.first);}
bool sortbysec(const pair<ll,ll> &a, const pair<ll,ll> &b){return (a.second < b.second);}
bool sortbysecinrev(const pair<ll,ll> &a, const pair<ll,ll> &b){return (a.second > b.second);}
ll gcd(ll x, ll y){return (ll)(__gcd(x, y));}
ll lcm(ll x, ll y){return (ll)((x * y) / gcd(x, y));}
ll mod_expo(ll x, ll y, ll p){
if((x + p) % p == 0) return 0;
ll res = 1; x = (x + p) % p;
while (y > 0){
if(y & 1) res = (res*x + p) % p;
y = y>>1; x = (x*x + p) % p;
}
return res;
}
void usaco(string str = ""){
fastio;
if(str.size()){
freopen((str + ".in").c_str(), "r", stdin);
freopen((str + ".out").c_str(), "w", stdout);
}
}
int main(){
usaco();
ll x, y, z; cin >> x >> y >> z; cout << (y * z - 1) / x << "\n";
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
}
| #pragma GCC optimize("Ofast")
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <stack>
#include <deque>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <unordered_map>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Po = pair<double, double>;
template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T>>;
#define REP(i,n) for(ll i=0;i<ll(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=ll(a);i<=ll(b);i++)
#define FORD(i,a,b) for(ll i=ll(a);i>=ll(b);i--)
#define PB push_back
#define MP make_pair
#define SZ(x) ll(x.size())
#define ALL(x) x.begin(),x.end()
#define NIL -1
#define INF 1ll<<63-1
//#define MOD 1000000007
#define PI 3.14159265358979323846
#define endl "\n"
#define EPS 1e-9
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
template <class T, class U>
bool chmin(T& t, const U& u) {
if (t > u) {
t = u;
return true;
}
else return false;
}
template <class T, class U>
bool chmax(T& t, const U& u) {
if (t < u) {
t = u;
return true;
}
else return false;
}
bool YN(bool b) {
if (b)Yes;
else No;
return true;
}
ll modpow(ll a, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
template <unsigned int MOD>
struct ModInt {
using uint = unsigned int;
using ull = unsigned long long;
using M = ModInt;
uint v;
ModInt(ll _v = 0) { set_norm(_v % MOD + MOD); }
M& set_norm(uint _v) { //[0, MOD * 2)->[0, MOD)
v = (_v < MOD) ? _v : _v - MOD;
return *this;
}
explicit operator bool() const { return v != 0; }
M operator+(const M& a) const { return M().set_norm(v + a.v); }
M operator-(const M& a) const { return M().set_norm(v + MOD - a.v); }
M operator*(const M& a) const { return M().set_norm(ull(v) * a.v % MOD); }
M operator/(const M& a) const { return *this * a.inv(); }
M& operator+=(const M& a) { return *this = *this + a; }
M& operator-=(const M& a) { return *this = *this - a; }
M& operator*=(const M& a) { return *this = *this * a; }
M& operator/=(const M& a) { return *this = *this / a; }
M operator-() const { return M() - *this; }
M& operator++(int) { return *this = *this + 1; }
M& operator--(int) { return *this = *this - 1; }
M pow(ll n) const {
if (n < 0) return inv().pow(-n);
M x = *this, res = 1;
while (n) {
if (n & 1) res *= x;
x *= x;
n >>= 1;
}
return res;
}
M inv() const {
ll a = v, b = MOD, p = 1, q = 0, t;
while (b != 0) {
t = a / b;
swap(a -= t * b, b);
swap(p -= t * q, q);
}
return M(p);
}
bool operator==(const M& a) const { return v == a.v; }
bool operator!=(const M& a) const { return v != a.v; }
friend ostream& operator<<(ostream& os, const M& a) { return os << a.v; }
static uint get_mod() { return MOD; }
};
using Mint = ModInt<998244353>;
V<ll> divisor(ll n) {
V<ll> ret;
for (ll i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n) ret.push_back(n / i);
}
}
sort(ALL(ret));
return (ret);
}
signed main() {
cout << fixed << setprecision(15);
ll x, y, a, b; cin >> x >> y >> a >> b;
ll cnt = 0;
while (double(x)<=double(x+b)/double(a)&&double(x)<double(y)/double(a)) {
x *= a;
cnt++;
}
cnt += (y - x-1) / b;
cout << cnt << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define reps(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define uniq(a) (a).erase(unique(all(a)), (a).end())
#define bit(n) (1LL << (n))
#define dump(a) cerr << #a " = " << (a) << endl
#define fin(a) \
cout << (a) << endl; \
return 0
using vll = vector<ll>;
using pll = pair<ll, ll>;
template <typename T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>;
constexpr double PI = 3.1415926535897932384626433832795028;
constexpr ll DY[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
constexpr ll DX[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
ll sign(ll a) { return (a > 0) - (a < 0); }
ll cdiv(ll a, ll b) { return (a - 1 + b) / b; }
template <typename T> T sq(T a) { return a * a; }
template <typename T, typename U> bool chmax(T &a, const U &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T, typename U> bool chmin(T &a, const U &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &a) {
os << "(" << a.first << ", " << a.second << ")";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
os << "(";
for (auto itr = a.begin(); itr != a.end(); itr++) { os << *itr << (next(itr) != a.end() ? ", " : ""); }
os << ")";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const deque<T> &a) {
os << "(";
for (auto itr = a.begin(); itr != a.end(); itr++) { os << *itr << (next(itr) != a.end() ? ", " : ""); }
os << ")";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &a) {
os << "{";
for (auto itr = a.begin(); itr != a.end(); itr++) { os << *itr << (next(itr) != a.end() ? ", " : ""); }
os << "}";
return os;
}
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &a) {
os << "{";
for (auto itr = a.begin(); itr != a.end(); itr++) { os << *itr << (next(itr) != a.end() ? ", " : ""); }
os << "}";
return os;
}
template <typename T, typename U> ostream &operator<<(ostream &os, const map<T, U> &a) {
os << "{";
for (auto itr = a.begin(); itr != a.end(); itr++) { os << *itr << (next(itr) != a.end() ? ", " : ""); }
os << "}";
return os;
}
struct setup {
static constexpr ll PREC = 20;
setup() {
cout << fixed << setprecision(PREC);
cerr << fixed << setprecision(PREC);
};
} setup;
int main() {
ll n;
cin >> n;
ll ans = LLONG_MAX;
ll b = 0;
while (bit(b) <= n) {
ll a = n / bit(b);
ll c = n % bit(b);
chmin(ans, a + b + c);
b++;
}
cout << ans << endl;
} | #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <bitset>
#include <cmath>
#include <set>
#include <map>
#include <stdio.h>
#include <stdlib.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=ll(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=ll(b);i--)
#define FORA(i,I) for(const auto& i:I)
#define ALL(x) x.begin(),x.end()
#define SIZE(x) ll(x.size())
#define INF64 9223372036854775807
#define INF32 2147483647
#define F first
#define S second
typedef long long ll;
using namespace std;
using ull = unsigned long long;
using ll = long long;
using vi = vector<int>;
using vl = vector<long>;
using vll = vector<long long>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using vvll = vector<vll>;
using vs = vector<string>;
using pii = pair<int, int>;
using vd = vector<double>;
const int mod = 1000000007;
ll myceil(ll a, ll b) { return (a + (b - 1)) / b; }
ll myfloor(ll a, ll b) { return a / b; }
template<class T>void chmin(T& a, T b) {
if (a > b)a = b;
}
ll gcd(ll a, ll b) {
if (b == 0)return a;
return gcd(b, a%b);
}
int main() {
ll n; cin >> n;
string s = to_string(n);
if (s.size() % 2 == 0) {
string s1 = s.substr(0, s.size() / 2);
string s2 = s.substr(s.size() / 2, s.size());
int pre = stoi(s1); int lat = stoi(s2);
if (pre <= lat)cout << pre << endl;
else
cout << pre - 1 << endl;
}
else {
int key = s.size() / 2;
cout << pow(10, key) - 1 << endl;
}
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0;i < (int)(n); ++i)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
typedef long long ll;
ll comb[61][61];
int main() {
ll A, B, K;
cin >> A >> B >> K;
comb[0][0] = 1;
rep(i, 60) {
rep(j, i + 1) {
comb[i + 1][j] += comb[i][j];
comb[i + 1][j + 1] += comb[i][j];
}
}
string ans = "";
while(A + B > 0) {
if(A == 0) {
ans += 'b';
B--;
continue;
}
ll x = comb[A + B - 1][A - 1];
if(x < K) {
K -= x;
ans += 'b';
B--;
} else {
ans += 'a';
A--;
}
}
cout << ans << endl;
} | #include <iostream>
using namespace std;
__uint128_t T = 1;
long long MOD = (1LL << 61) - 1;
long long modinv(long long a, long long m) {
long long b = m, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b; swap(a, b);
u -= t * v; swap(u, v);
}
u %= m;
if (u < 0) u += m;
return u;
}
void solve(int a, int b, long long k) {
if (a + b == 1) {
cout << (a == 1 ? 'a' : 'b') << endl;
return;
}
long long t = T;
if ((a + b) * k > a * t) {
cout << 'b';
T *= b;
T /= a + b;
solve(a, b - 1, k - (a * t) / (a + b));
} else {
cout << 'a';
T *= a;
T /= a + b;
solve(a - 1, b, k);
}
}
int main() {
int A, B; cin >> A >> B;
long long K; cin >> K;
for (int i = max(A, B) + 1; i <= A + B; i++) {
T *= i;
T %= MOD;
}
for (int i = 2; i <= min(A, B); i++) {
T *= modinv(i, MOD);
T %= MOD;
}
solve(A, B, K);
}
|
//For higher stack size use g++ -O2 -std=c++11 -Wall -Wl,--stack=268435456 a.cpp -o a.exe in cmd
#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstring>
#include <chrono>
#include <complex>
#define ll long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define vvi vector < vi >
#define pii pair<int,int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define all(c) c.begin(),c.end()
#define mp(x,y) make_pair(x,y)
#define mem(a,val) memset(a,val,sizeof(a))
#define eb emplace_back
#define pb push_back
#define f first
#define s second
using namespace std;
// Disjoint Set DataStructure
int parent[151]; // reserve space for map if n > 10000
int urank[151];
int maxi[151],mini[151];
void create(ll x, ll y)
{
parent[x] = x ;
urank[x] = 1 ; // rank = no. of nodes in its subtree
maxi[x]=y;
mini[x]=y;
}
ll find(ll x)
{
if( parent[x] != x ) //path compression
{
parent[x] = find(parent[x]) ;
}
return parent[x];
}
void merge(ll x, ll y)
{
ll xroot = find(x);
ll yroot = find(y);
if(xroot==yroot)
return;
if( urank[xroot] <= urank[yroot] ) // Union by rank
{
parent[xroot] = yroot ;
urank[yroot] = urank[yroot] + urank[xroot] ;
maxi[yroot] = max(maxi[yroot],maxi[xroot]);
mini[yroot] = min(mini[yroot],mini[xroot]);
}
else
{
parent[yroot] = xroot;
urank[xroot] = urank[xroot] + urank[yroot] ;
maxi[xroot] = max(maxi[yroot],maxi[xroot]);
mini[xroot] = min(mini[yroot],mini[xroot]);
}
}
int n;
pii ar[101];
bool intersect(ll x1,ll y1,ll x2,ll y2,ld r)
{
if(4*r*r > (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2))
return true;
return false;
}
bool func(ld r)
{
int i,j,x;
for(i=1;i<=n;++i)
{
create(i,ar[i].s);
}
for(i=1;i<=n;++i)
{
for(j=i+1;j<=n;++j)
{
if(intersect(ar[i].f,ar[i].s,ar[j].f,ar[j].s,r))
{
// cout<<i<<"..."<<j<<" "<<r<<endl;
merge(i,j);
}
}
}
for(i=1;i<=n;++i)
{
x=find(i);
if(maxi[x]+2*r > 100 && mini[x]-2*r < -100)
{
// cout<<i<<" "<<x<<" "<<r<<endl;
return false;
}
}
return true;
}
int main()
{
std::ios::sync_with_stdio(false);
int T;
T=1;
// cin.ignore(); must be there when using getline(cin, s)
while(T--)
{
int i,j;
cin>>n;
for(i=1;i<=n;++i)
{
cin>>ar[i].f>>ar[i].s;
}
ld l=0.0,r=200,mid,ans=0.0;;
for(i=1;i<=100;++i)
{
mid=(l+r)/2;
if(func(mid))
{
ans=mid;
l=mid;
}
else
{
r=mid;
}
}
printf("%.9Lf\n",ans);
}
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;
const double eps = 1e-10;
const double INF = 1e9;
class Union_Find{
vector<int> par; vector<int> rank; vector<int> num;
public:
Union_Find(int n) {
par = vector<int>(n); rank = vector<int>(n,0); num = vector<int>(n,1);
rep(i,n) par[i] = i;
}
int find(int x) {
if(par[x] == x) return x;
else return par[x] = find(par[x]);
}
int number(int x) {
return num[find(x)];
}
void unite(int x, int y) {
x = find(x); y = find(y);
if (x == y) return;
if (rank[x] < rank[y]) swap(x, y);
if (rank[x] == rank[y]) rank[x]++;
par[y] = x;
num[x] += num[y];
}
bool same(int x, int y) {
return find(x) == find(y);
}
};
int main(){
int n;
cin >> n;
double x[n], y[n];
rep(i,n) cin >> x[i] >> y[i];
double l = 0, r = 100.0;
while(r - l > eps) {
double m = (l + r) / 2;
Union_Find uf(n+2);
rep(i,n) {
if(y[i]+m > -m+100.0) uf.unite(n,i);
if(y[i]-m < m-100.0) uf.unite(n+1,i);
}
rep(i,n) rep(j,n) if((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]) < 4.0*m*m) uf.unite(i,j);
if(uf.find(n) == uf.find(n+1)) r = m;
else l = m;
}
printf("%.12lf\n",l);
return 0;
}
|
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <iomanip>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cstdint>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define rep(i,n) for(int i = 0; i < (int)(n); ++i)
#define all(x) (x).begin(),(x).end()
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 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;
}
map<pair<ll,ll>,ll> mp;
ll calc(ll a,ll k){
if(a==0)return 0;
if(k==0)return a;
if(mp.find(MP(a,k))!=mp.end())return mp[MP(a,k)];
ll K = (1LL<<k);
return mp[MP(a,k)] = min(a/K + calc(a-K*(a/K),k-1), a/K+1 + calc((K)*(a/K + 1)-a,k-1));
}
int main(){
ll x,y;
cin >> x >> y;
ll mi = (1LL<<62);
for(int i=0;i<=62;i++){
ll X = x;
bool ng = 0;
rep(j,i){
X *= 2;
if(X >=4000000000000000000LL){
ng = 1;
break;
}
}
if(ng)break;
ll p = abs(X-y);
// cerr << i << " " << p << endl;
ll res = 0;
res = calc(p,i);
// for(int j=i;j>=0;j--){
// ll K = (1LL<<j);
// res += (p/K);
// p -= K * (p/K);
// ll s = __builtin_popcount(p);
// ll q = abs(p-K);
// ll t = __builtin_popcount(q);
// if(s>t){
// p = q;
// res++;
// }else{
// }
// }
chmin(mi,res+i);
}
cout << mi << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long x, y;
cin >> x >> y;
map<long long, long long> dp;
auto sol = [&](auto &&self, long long val){
if(dp[val]) return dp[val];
if(x >= val){
return x - val;
}
long long rv = val - x;
if(val % 2){
rv = min(rv, self(self, (val - 1) / 2) + 2);
rv = min(rv, self(self, (val + 1) / 2) + 2);
}
else{
rv = min(rv, self(self, val / 2) + 1);
}
dp[val] = rv;
return rv;
};
cout << sol(sol, y);
return 0;
} |
#include <bits/stdc++.h>
#pragma GCC optimize("O2")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,fma")
using namespace std;
using ll = long long int;
#define F first
#define S second
#define pb push_back
#define fast_io ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
const int N=2e5+10,LN=20,SQ=550,M=1e5+10;
const ll INF=1e18;
const int MOD=1000000007 /*998244353*/;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using pll=pair<ll,ll>;
using pii=pair<int,int>;
#define ordered_set tree<pair<pll,ll>, null_type,less<pair<pll,ll>>, rb_tree_tag,tree_order_statistics_node_update>
ll pow(ll x, ll y, ll mod){
ll ans=1;
while (y != 0) {
if (y & 1) ans = ans * x % mod;
y >>= 1;
x = x * x % mod;
}
return ans;
}
ll n,m;
pll s[N];
int main(){
fast_io;
cin >> n >> m;
if(n==1 && m==0) return cout << 1 << ' ' << 2 << '\n', 0;
if(m>n-2 || m<0) return cout << -1 << '\n', 0;
cout << 1 << ' ' << 2*m+4 << '\n';
for(ll i=1; i<=m+1; i++){
cout << i*2 << ' ' << i*2+1 << '\n';
}
for(ll i=m+2; i<n; i++) cout << 2*i+1 << ' ' << 2*i+2 << '\n';
return 0;
}
| //#include<cstdio>
//#include<cassert>
//#include<iostream>
//#include<cstring>
//#include<algorithm>
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define MAX ((int)1e8 + 5)
#define MAXP ((int)2e5)
#define MAXL ((ll)1e18 + 5)
#define MAX_X ((ll)1e5 + 2)
#define pi acos(-1)
#define M ((int)1e6 + 7)
#define MOD ((int)1e9 + 7)
#define MOD1 ((int)998244353 + 0)
#define BAS ((int)1e6 + 3)
#define BAS1 ((int)2e5 + 3)
#define NN ((int)1e5 + 5)
#define N ((int)2e5 + 5)
#define eps (1e-8)
#define fastio ios_base::sync_with_stdio(false),cin.tie(NULL)
#define logn 10
#define endl "\n"
#define mp make_pair
#define BUCK 105
#define LEF (idx<<1)
#define RIG ((idx<<1)|1)
//#define int ll
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
/*fast io
ios_base::sync_with_stdio(false);
cin.tie(NULL);
*/
typedef tree < int, null_type, less < int >, rb_tree_tag, tree_order_statistics_node_update > o_set;
typedef tree < pair < int, int >, null_type, less < pair < int, int > >, rb_tree_tag, tree_order_statistics_node_update > o_setp;
/// o_set s;
/// s.order_of_key(k) : Number of items strictly smaller than k .
/// *(s.find_by_order(k)) : K-th element in a set (counting from zero).
void ses()
{
cout<<"-1\n";
exit(0);
}
int main()
{
fastio;
int n , m;
cin>>n>>m;
if(m < 0 || m == n || (m == n-1 && n > 1) ) ses();
else if(m == 0){
for(int i = 1 ; i<=n ; i++) cout<<i+i-1<<" "<<i+i<<endl;
}
else{
int rem = n - m-1 , from = 1 , too = MAX;
while(rem--){
cout<<from++<<" "<<too--<<endl;
}
rem = m+1;
while(rem--){
cout<<from<<" "<<1+from<<endl;
from += 2;
}
}
return 0;
}
|
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
using VI = vector<int>;
int g(VI a, int i, int d);
int f(VI const& v) {
int i = find(cbegin(v), cend(v), 0) - cbegin(v);
return i < v.size() ? g(v, i, 0) + g(v, i, 1) : 1;
}
int w;
int g(VI v, int i, int d) {
int j = i + (d ? w : 1);
if (j >= v.size() || !(d || j % w) || v[j]) {
return 0;
}
v[i] = v[j] = 1;
return f(v);
}
int main() {
int h, a, b;
scanf("%d%d%d%d", &h, &w, &a, &b);
VI v(2 * a);
v.insert(end(v), b, 1);
int r = 0;
do r += f(v);
while (next_permutation(begin(v), end(v)));
printf("%d\n", r);
return 0;
}
| #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
#define reps(i,s,n) for(int i=s;i<n;++i)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
int ans0[4] = { 0,0,0,0 };
int ans1[4] = { 1,1,1,1 };
int ans2[4] = { 6,2,4,8 };
int ans3[4] = { 1,3,1,3 };
int ans4[4] = { 6,4,6,4 };
int ans5[4] = { 5,5,5,5 };
int ans6[4] = { 6,6,6,6 };
int ans7[4] = { 1,7,9,3 };
int ans8[4] = { 6,8,4,2 };
int ans9[4] = { 1,9,1,9 };
int main()
{
ll a, b, c;
cin >> a >> b >> c;
int mod = 4;
//コーナーケース
if (b % mod == 2)
{
if (c == 1) b = 2;
else b = 0;
}
else
{
//cが偶数なら2乗、奇数なら1乗(なにもしない)
if (c % 2 == 0) b *= b;
b %= mod;
}
a %= 10;
int ans = 0;
if (a == 0) ans = ans0[b];
if (a == 1) ans = ans1[b];
if (a == 2) ans = ans2[b];
if (a == 3) ans = ans3[b];
if (a == 4) ans = ans4[b];
if (a == 5) ans = ans5[b];
if (a == 6) ans = ans6[b];
if (a == 7) ans = ans7[b];
if (a == 8) ans = ans8[b];
if (a == 9) ans = ans9[b];
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
// input
#define INIT std::ios::sync_with_stdio(false);std::cin.tie(0);
#define VAR(type, ...)type __VA_ARGS__;MACRO_VAR_Scan(__VA_ARGS__); // __VA_ARGS__可変引数マクロ
template<typename T> void MACRO_VAR_Scan(T& t) { std::cin >> t; }
template<typename First, typename...Rest>void MACRO_VAR_Scan(First& first, Rest& ...rest) { std::cin >> first; MACRO_VAR_Scan(rest...); }
#define VEC(type, c, n) std::vector<type> c(n);for(auto& i:c)std::cin>>i;
#define MAT(type, c, m, n) std::vector<std::vector<type>> c(m, std::vector<type>(n));for(auto& R:c)for(auto& w:R)std::cin>>w;
// output
template<typename T>void MACRO_OUT(const T t) { std::cout << t; }
template<typename First, typename...Rest>void MACRO_OUT(const First first, const Rest...rest) { std::cout << first << " "; MACRO_OUT(rest...); }
#define OUT(...) MACRO_OUT(__VA_ARGS__);
#define FOUT(n, dist) std::cout<<std::fixed<<std::setprecision(n)<<(dist); // std::fixed 浮動小数点の書式 / setprecision 浮動小数点数を出力する精度を設定する。
#define SP std::cout<<" ";
#define TAB std::cout<<"\t";
#define BR std::cout<<"\n";
#define SPBR(w, n) std::cout<<(w + 1 == n ? '\n' : ' ');
#define ENDL std::cout<<std::endl;
#define FLUSH std::cout<<std::flush;
// utility
#define ALL(a) (a).begin(),(a).end()
#define FOR(w, a, n) for(int w=(a);w<(n);++w)
#define REP(w, n) for(int w=0;w<int(n);++w)
#define IN(a, x, b) (a<=x && x<b)
template<class T> inline T CHMAX(T & a, const T b) { return a = (a < b) ? b : a; }
template<class T> inline T CHMIN(T& a, const T b) { return a = (a > b) ? b : a; }
template<class T> using V = std::vector<T>;
template<class T> using VV = V<V<T>>;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using PAIR = std::pair<int, int>;
using PAIRLL = std::pair<ll, ll>;
constexpr int INFINT = (1 << 30) - 1; // 1.07x10^ 9
constexpr int INFINT_LIM = (1LL << 31) - 1; // 2.15x10^ 9
constexpr ll INFLL = 1LL << 60; // 1.15x10^18
constexpr ll INFLL_LIM = (1LL << 62) - 1 + (1LL << 62); // 9.22x10^18
constexpr double eps = 1e-7;
constexpr int MOD = 1000000007;
constexpr double PI = 3.141592653589793238462643383279;
template<class T, size_t N> void FILL(T(&a)[N], const T & val) { for (auto& x : a) x = val; } // int a[5] = {1,2,3,4,5}; FILL(a,10);
template<class ARY, size_t N, size_t M, class T> void FILL(ARY(&a)[N][M], const T & val) { for (auto& b : a) FILL(b, val); } // 2次元配列でもそのまま使える
template<class T> void FILL(std::vector<T> & a, const T & val) { for (auto& x : a) x = val; } // 使い方 vector<int> a(3); FILL(a,10);
template<class ARY, class T> void FILL(std::vector<std::vector<ARY>> & a, const T & val) { for (auto& b : a) FILL(b, val); } // 2次元配列でもそのまま使える
// ------------>8------------------------------------->8------------
int main() {
INIT;
VAR(ll,n)
V<PAIRLL> p(n);
ll a,b;
__int128_t a_sum =0;
REP(i,n){
cin >> a >> b;
p[i] = {2*a+b,b};
a_sum += a;
}
sort(p.rbegin(),p.rend());
ll i =0;
__int128_t t_sum =0;
while(1){
t_sum += (p[i].first - p[i].second) /2 + p[i].second ;
a_sum -= (p[i].first - p[i].second) /2 ;
i++;
if(t_sum > a_sum)
break;
}
OUT(i)
return 0;
}
| #define ll long long
#define dd long double
#define pb push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
#define fo(i ,a, n) for(ll i =a ; i < n ; i++)
//#define all(x) x.begin(), x.end()
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)//traverse through a dataset
//#define sortx(x) sort(all(x))
#define maxpq priority_queue<ll>
#define minpq priority_queue<ll,vector<ll>,greater<ll>>
#include<bits/stdc++.h>
#include<iomanip>
#include<cmath>
#include<cstdio>
#include<utility>
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<bitset>
ll fact[200001] ;
ll inf=1e18;
ll z=1e9+7;
ll gdp(ll a , ll b){return (a - (a%b)) ;}
ll ld(ll a , ll b){if(a < 0) return -1*gdp(abs(a) , b) ; if(a%b == 0) return a ; return (a + (b - a%b)) ;} // least number >=a divisible by b
ll gd(ll a , ll b){if(a < 0) return(-1 * ld(abs(a) , b)) ; return (a - (a%b)) ;} // greatest number <= a divisible by b
ll gcd(ll a , ll b){ if(b > a) return gcd(b , a) ; if(b == 0) return a ; return gcd(b , a%b) ;}
ll e_gcd(ll a , ll b , ll &x , ll &y){ if(b > a) return e_gcd(b , a , y , x) ; if(b == 0){x = 1 ; y = 0 ; return a ;}
ll x1 , y1 ; e_gcd(b , a%b , x1 , y1) ; x = y1 ; y = (x1 - ((a/b) * y1)) ; return e_gcd(b , a%b , x1 , y1) ;}
ll power(ll a ,ll b , ll p){if(b == 0) return 1 ; ll c = power(a , b/2 , p) ; if(b%2 == 0) return ((c*c)%p) ; else return ((((c*c)%p)*a)%p) ;}
ll inverse(ll a ,ll n){return power(a , n-2 , n) ;}
ll max(ll a , ll b){if(a > b) return a ; return b ;}
ll min(ll a , ll b){if(a < b) return a ; return b ;}
ll left(ll i){return ((2*i)+1) ;}
ll right(ll i){return ((2*i) + 2) ;}
ll ncr(ll n , ll r){if(n < r|| (n < 0) || (r < 0)) return 0 ; return ((((fact[n] * inverse(fact[r] , z)) % z) * inverse(fact[n-r] , z)) % z);}
void swap(ll&a , ll&b){ll c = a ; a = b ; b = c ; return ;}
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define ss(s) scanf("%s",s)
#define pi(x) printf("%d\n",x)
#define pl(x) printf("%lld\n",x)
#define ps(s) printf("%s\n",s)
#define ub(v,x) upper_bound(v.begin(),v.end(),x);
#define lb(v,x) lower_bound(v.begin(),v.end(),x);
dd pi = acos(-1) ;
ll pow(ll a ,ll b ){if(b == 0) return 1 ; ll c = pow(a , b/2) ; if(b%2 == 0) return (c*c) ; else return ((c*c)*a);}
using namespace std ;
const int N = 2e5+7;
const int M=1e6+7;
//vector<ll>adj[N];
//int mat[N][N];
//vector<bool>used(N,false);
//vector<ll>d(N,-1);
//vector<ll>p(N,-1);
bool cmp(ll a,ll b){
return a>b;
}
struct S
{
int m_n1;
int m_n2;
int m_n3;
S(int n1, int n2, int n3) : m_n1(n1), m_n2(n2), m_n3(n3)
{
}
bool operator<(const struct S& other) const
{
if(m_n1!=other.m_n1)
return m_n1 < other.m_n1;
else
return m_n2 > other.m_n2;
}
};
void solve(){
ll n;
cin>>n;
ll votes[n][2];
ll s[2]={0};
vector<pair<ll,pair<ll,ll>>>v;
fo(i,0,n){
ll x,y;
cin>>x>>y;
votes[i][0]=x;
votes[i][1]=y;
s[1]+=x;
v.pb({y+2*x,{x,y}});
}
ll cnt=0;
sort(v.begin(),v.end());
reverse(v.begin(),v.end());
fo(i,0,n){
if(s[0]>s[1]){
break;
}
else{
s[0]+=(v[i].S.F+v[i].S.S);
s[1]-=v[i].S.F;
cnt++;
}
}
cout<<cnt<<endl;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
//cin>>t;
t=1;
while(t--){
solve();
}
} |
#include <map>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#define ll long long
#define DEBUG pritnf("Passing [%s] in Line %d\n" , __FUNCTION__ , __LINE__) ;
const int MAX_N = 2e5 + 10 ;
std::map<int , int> bh ;
std::stack<int> st[MAX_N] ;
int n , tot , b[MAX_N] , BIT[MAX_N] ;
inline int lowbit(int x) {return x & (-x) ;}
int getsum(int x) {
if (!x) return 0 ;
int s = 0 ;
for (; x ; x -= lowbit(x)) s += BIT[x] ;
return s ;
}
void add(int x) {
if (!x) return ;
for (; x <= n ; x += lowbit(x)) ++BIT[x] ;
}
int main() {
scanf("%d" , &n) ;
for (int i = 1 ; i <= n ; ++i) {
int a , p ;
scanf("%d" , &a) ; a += i ;
if (!bh.count(a)) bh[a] = ++tot ;
p = bh[a] ; st[p].push(i) ;
}
for (int i = 1 ; i <= n ; ++i) scanf("%d" , &b[i]) ;
///
ll ans = 0 ;
for (int i = n ; i ; --i) {
int ned = b[i] + i , p = bh.count(ned) ? bh[ned] : 0 ;
if (st[p].empty()) {ans = -1 ; break ;}
int nw = st[p].top() ; st[p].pop() ;
add(nw + 1) ; nw -= getsum(nw) ; ans += i - nw ;
}
printf("%lld\n" , ans) ;
return 0 ;
} | //#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,tune=native")
#include<bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define X first
#define Y second
#define mp make_pair
typedef long long ll;
typedef unsigned long long ull;
typedef long double dbl;
typedef pair<int, int> pii;
const int N = (int)1e5 + 5;
int A[N], B[N];
int n;
void solve(){
cin >> n;
ll sum = 0;
for(int i = 0; i < n; i++){
cin >> A[i];
sum += A[i];
}
for(int i = 0; i < n; i++)
cin >> B[i];
vector<int> d1, d2;
for(int i = 0; i < n; i++)
if(i&1)d2.pb(B[i] - A[i]);
else d1.pb(B[i] - A[i]);
sort(all(d1));
sort(all(d2));
ll ans = sum;
for(int pos = n/2-1; pos >= 0; pos--){
sum += d1[pos] + d2[pos];
ans = max(ans, sum);
}
cout << ans << endl;
}
signed main(){
#ifdef LOCAL
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);
int T = 1;
while(T--)
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
int h, w;
cin >> h >> w;
vector<string> vs;
for(int i = 0; i < h; i++){
string s;
cin >> s;
vs.push_back(s);
}
/*
i,j i+1,j
i,j+1 i+1,j+1
*/
int res = 0;
for(int i = 0; i < h - 1; i++){
for(int j = 0; j < w - 1; j++){
int sharp = 0;
if(vs[i][j] == '#') ++sharp;
if(vs[i + 1][j] == '#') ++sharp;
if(vs[i][j + 1] == '#') ++sharp;
if(vs[i+1][j+1] == '#') ++sharp;
if(sharp == 1 || sharp == 3) ++res;
}
}
cout << res;
}
| #include <bits/stdc++.h>
using namespace std;
using LL=long long;
using ULL=unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)
string S;
int main(){
cin>>S;
reverse(S.begin(),S.end());
int right_block=0;
char right_c='$';
LL ans=0;
stack<char> buf;
for(char c:S){
if(buf.size()!=0) if(buf.top()==c){
int tmp=2; buf.pop();
while(buf.size()!=0){ if(buf.top()!=c){ ans++; } tmp++; buf.pop(); }
if(c!=right_c) ans+=right_block;
right_c=c;
right_block+=tmp;
continue;
}
buf.push(c);
}
cout<<ans<<endl;
return 0;
}
|
#include <bits/stdc++.h>
typedef long long ll;
#define llinf 0x3f3f3f3f3f3f3f3f
#define inf 0x3f3f3f3f
#define SZ(x) (int)x.size()
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define pb push_back
using namespace std;
char s[1005], t[1005];
string atc = "0atcoder";
vector<int> vec[26];
int main() {
int T;
scanf("%d", &T);
atc += 'a' - 1;
while (T--) {
scanf("%s", s + 1);
int n = strlen(s + 1);
if (n == 1) {
if (s[1] == 'a') {
printf("-1\n");
continue;
} else {
printf("0\n");
continue;
}
}
int fg = 0;
for (int i = 1; i <= min(n, 8); i++) {
if (s[i] < atc[i]) break;
else if (s[i] > atc[i]) {
fg = 1;
break;
}
}
for (int i = 0; i < 26; i++) vec[i].clear();
for (int i = 1; i <= n; i++) vec[s[i] - 'a'].pb(i);
if (fg) {
printf("0\n");
continue;
}
int cnt = 1;
for (int i = 25; i >= 0; i--) {
for (int j = 0; j < SZ(vec[i]); j++) {
t[cnt++] = 'a' + i;
}
}
fg = 1;
for (int i = 1; i <= min(n, 8); i++) {
if (t[i] < atc[i]) {
fg = 0;
break;
} else if (t[i] > atc[i]) break;
}
if (!fg) {
printf("-1\n");
continue;
}
int minn = inf;
for (int i = 1; i <= min(n, 7); i++) {
if (s[i] < atc[i]) {
for (int j = atc[i] - 'a' + 1; j < 26; j++) {
if (!SZ(vec[j])) continue;
int pos = upper_bound(vec[j].begin(), vec[j].end(), i) - vec[j].begin();
if (pos == SZ(vec[j])) continue;
minn = min(minn, vec[j][pos] - i);
}
break;
} else if (s[i] == atc[i]) {
for (int j = atc[i] - 'a' + 1; j < 26; j++) {
if (!SZ(vec[j])) continue;
int pos = upper_bound(vec[j].begin(), vec[j].end(), i) - vec[j].begin();
if (pos == SZ(vec[j])) continue;
// printf("%d %d %d\n", j, vec[j][pos], i);
minn = min(minn, vec[j][pos] - i);
}
}
}
printf("%d\n", minn);
}
} | #include<bits/stdc++.h>
#define int long long
using namespace std;
const int mod=1e9+7;
int dist[2000][2000];
bool done[26];
signed main(){
int h,w;
string a[2000];
cin>>h>>w;
pair<int,int>s,g;
vector<pair<int,int>>pos[26];
for(int i=0;i<h;i++){
cin>>a[i];
for(int j=0;j<w;j++){
if(a[i][j]>='a'&&a[i][j]<='z')
pos[a[i][j]-'a'].emplace_back(i,j);
if(a[i][j]=='S')
s=make_pair(i,j);
if(a[i][j]=='G')
g=make_pair(i,j);
}
}
for(int i=0;i<h;i++)
for(int j=0;j<w;j++)
dist[i][j]=mod;
int dx[]={1,0,0,-1},dy[]={0,1,-1,0};
queue<pair<int,int>>que;
que.push(s);
dist[s.first][s.second]=0;
while(!que.empty()){
pair<int,int>p=que.front();
que.pop();
if(g==p){
cout<<dist[g.first][g.second]<<endl;
return 0;
}
if(a[p.first][p.second]>='a'&&a[p.first][p.second]<='z'&&!done[a[p.first][p.second]-'a']){
done[a[p.first][p.second]-'a']=1;
for(int i=0;i<pos[a[p.first][p.second]-'a'].size();i++){
pair<int,int>np=pos[a[p.first][p.second]-'a'][i];
if(dist[np.first][np.second]==mod){
dist[np.first][np.second]=dist[p.first][p.second]+1;
que.push(np);
}
}
}
for(int i=0;i<4;i++){
pair<int,int>np=make_pair(p.first+dx[i],p.second+dy[i]);
if(np.first<0||np.first>=h||np.second<0||np.second>=w)
continue;
if(a[p.first][p.second]>='a'&&a[p.first][p.second]<='z'&&a[p.first][p.second]==a[np.first][np.second])
continue;
if(a[np.first][np.second]=='#')
continue;
if(dist[np.first][np.second]==mod){
dist[np.first][np.second]=dist[p.first][p.second]+1;
que.push(np);
}
}
}
cout<<-1<<endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
int d[N], a[N];
ll f[N];
int n, m;
int h[N], e[N], ne[N], idx;
void add(int x, int y) {
e[++idx] = y, ne[idx] = h[x], h[x] = idx;
}
int main()
{
memset(h, -1, sizeof h);
idx = 0;
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; ++i) scanf("%d", &a[i]);
for(int i = 1; i <= m; ++i) {
int x, y;
scanf("%d%d", &x, &y);
add(x, y);
++d[y];
}
queue<int> q;
for(int i = 1; i <= n; ++i) if(!d[i]) q.push(i);
memset(f, 0x3f, sizeof f);
ll res = -1e18;
while(!q.empty()) {
auto u = q.front(); q.pop();
res = max(res, 1ll * a[u] - f[u]);
f[u] = min(f[u], 1ll * a[u]);
for(int i = h[u]; ~i; i = ne[i]) {
int v = e[i];
f[v] = min(f[v], f[u]);
--d[v];
if(!d[v]) q.push(v);
}
}
printf("%lld\n", res);
} | // abc188e.cpp
#include <iostream>
#include <vector>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
const int INF = 1000000009;
int main()
{
int n, m;
cin >> n >> m;
vector<int>a(n);
rep(i, n) cin >> a[i];
vector<vector<int>>w(n);
int x, y;
rep(i, m) {
cin >> x >> y;
x--; y--;
w[y].push_back(x);
}
vector<int>dp(n,INF);
int ans = -INF;
for(int i=1;i<n;i++) { // dp[1]から計算する
int now = INF;
for (int j : w[i]) {
now = min(now, dp[j]);
now = min(now, a[j]); // 町jに来るまでで最安値を調べる
}
dp[i] = now; // 町iに来るまでで金の最安値(町iを含まない)
//cout << "dp[" << i << "]=" << dp[i] << endl;
if(now!=INF) ans = max(ans, a[i] - now);
//cout << "ans=" << ans << endl;
}
cout << ans << endl;
}
/*
2 1
1000000000 1
1 2
3 1
1 1000000000 1
2 3
*/
|
#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
#define SZ(v) ((int)(v).size())
typedef pair<int,int> pii;
#define ll long long
int n;
const int Maxv=1e5+5;
bool dp[Maxv];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
dp[0]=1;
int tot=0;
for(int i=1;i<=n;i++){
int x;cin>>x;
tot+=x;
for(int j=Maxv-1;j>=0;j--){
if(dp[j]&&j+x<Maxv){
dp[j+x]=1;
}
}
}
vector<int>v;
for(int i=0;i<Maxv;i++){
if(dp[i]){
v.push_back(i);
}
}
if(tot%2==1)tot++;
auto it=lower_bound(v.begin(),v.end(),tot/2);
cout<<*it<<endl;
return 0;
}
| #include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cin>>n;
vector<int>set(n);
int sum = 0;
int x1= 0 ,x2 = 0;
for(int i=0;i<n;i++){
cin>>set[i];
sum+=set[i];
}
bool subset[n + 1][sum + 1];
// If sum is 0, then answer is true
for (int i = 0; i <= n; i++)
subset[i][0] = true;
// If sum is not 0 and set is empty,
// then answer is false
for (int i = 1; i <= sum; i++)
subset[0][i] = false;
// Fill the subset table in botton up manner
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= sum; j++) {
if (j < set[i - 1])
subset[i][j] = subset[i - 1][j];
if (j >= set[i - 1])
subset[i][j] = subset[i - 1][j]
|| subset[i - 1][j - set[i - 1]];
}
}
int mini = 1e8;
int ans;
for(int j=1;j<=sum;j++){
if(subset[n][j]){
// cout<<j<<endl;
if(max(j,(sum-j))<mini){
mini = max(j,sum-j);
//mini = j-(sum-j);
}
}
}
cout<<mini<<endl;
//cout<<max(ans,sum-ans)<<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>
using namespace std;
typedef vector<int> vec;
typedef vector<vec> grid;
int main() {
int h, w;
cin>>h>>w;
grid dp(h+1, vec(w+1, -1));
grid cell_score(h+1, vec(w+1, -1));
vector<string> game(h);
for (int i=0; i<h; ++i) {
cin>>game[i];
}
for (int i=0; i<h; ++i) {
for (int j=0; j<w; ++j) {
if (game[i][j] == '+')
cell_score[i][j] = 1;
else
cell_score[i][j] = -1;
}
}
dp[h-1][w-1] = 0;
for (int i=h-1; i>=0; --i) {
for (int j=w-1; j>=0; --j) {
if (i==h-1 && j==w-1) {
continue;
}
int res = numeric_limits<int>::min();
int temp_res;
if ( i!= h-1) {
temp_res = cell_score[i+1][j] - dp[i+1][j];
res = max(res, temp_res);
}
if (j != w-1) {
temp_res = cell_score[i][j+1] - dp[i][j+1];
res = max(res, temp_res);
}
dp[i][j] = res;
}
}
if (dp[0][0] > 0) {
cout<<"Takahashi";
}
else if(dp[0][0] < 0) {
cout<<"Aoki";
}
else {
cout<<"Draw";
}
} |
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define pii pair<int,int>
#define pll pair<ll,ll>
#define ldb double
template<typename T>void ckmn(T&a,T b){a=min(a,b);}
template<typename T>void ckmx(T&a,T b){a=max(a,b);}
void rd(int&x){scanf("%i",&x);}
void rd(ll&x){scanf("%lld",&x);}
void rd(char*x){scanf("%s",x);}
void rd(ldb&x){scanf("%lf",&x);}
void rd(string&x){scanf("%s",&x);}
template<typename T1,typename T2>void rd(pair<T1,T2>&x){rd(x.first);rd(x.second);}
template<typename T>void rd(vector<T>&x){for(T&i:x)rd(i);}
template<typename T,typename...A>void rd(T&x,A&...args){rd(x);rd(args...);}
template<typename T>void rd(){T x;rd(x);return x;}
int ri(){int x;rd(x);return x;}
template<typename T>vector<T> rv(int n){vector<T> x(n);rd(x);return x;}
template<typename T>void ra(T a[],int n,int st=1){for(int i=0;i<n;++i)rd(a[st+i]);}
template<typename T1,typename T2>void ra(T1 a[],T2 b[],int n,int st=1){for(int i=0;i<n;++i)rd(a[st+i]),rd(b[st+i]);}
template<typename T1,typename T2,typename T3>void ra(T1 a[],T2 b[],T3 c[],int n,int st=1){for(int i=0;i<n;++i)rd(a[st+i]),rd(b[st+i]),rd(c[st+i]);}
void re(vector<int> E[],int m,bool dir=0){for(int i=0,u,v;i<m;++i){rd(u,v);E[u].pb(v);if(!dir)E[v].pb(u);}}
template<typename T>void re(vector<pair<int,T>> E[],int m,bool dir=0){for(int i=0,u,v;i<m;++i){T w;rd(u,v,w);E[u].pb({v,w});if(!dir)E[v].pb({u,w});}}
// C
const int N=200050;
int p[N],pos[N];
int main(){
int n=ri();
ra(p,n);
for(int i=1;i<=n;i++)pos[p[i]]=i;
vector<int> ans;
int mx=0;
for(int i=1;i<=n;i++){
int j=pos[i];
if(j==i)continue;
if(mx!=i-1)return 0*printf("-1\n");
mx=j-1;
while(j>i){
ans.pb(j-1);
swap(pos[p[j]],pos[p[j-1]]);
swap(p[j],p[j-1]);
j--;
}
}
if(ans.size()!=n-1)return 0*printf("-1\n");
for(int i:ans)printf("%i\n",i);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int sz = 2e5+5;
int n, a[sz], w[sz], bit[sz];
void add(int x) {
while(x <= n) {
bit[x]++;
x += x & (-x);
}
}
int quer(int x) {
int ans = 0;
while(x) {
ans += bit[x];
x -= x & (-x);
}
return ans;
}
int main() {
cin >> n;
for(int i=1; i<=n; i++) scanf("%d", &a[i]), w[a[i]] = i;
int tot = 0;
vector <int> res;
for(int i=n; i; i--) {
int x = quer(w[i]);
if(x) tot += x, res.push_back(x);
add(w[i]);
}
if(tot == n-1) {
reverse(res.begin(), res.end());
int s = 0;
for(int x : res) {
for(int i=s+x; i>s; i--) printf("%d\n", i);
s += x;
}
}
else puts("-1");
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template<class T> using vc = vector<T>;
template<class T> using vvc = vector<vector<T>>;
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i,n) for (auto& i: n)
template<class T> inline bool chmax(T &a, const T &b) {if (a<b) { a=b; return 1;} return 0;}
template<class T> inline bool chmin(T &a, const T &b) {if (b<a) { a=b; return 1;} return 0;}
struct init{init(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}}init_;
#ifdef DEBUG
template <class T, class N> void verr(const T& a, const N& n) { rep(i, n) cerr << a[i] << " "; cerr << "\n" << flush; }
ll dbgt = 1; void err() { cerr << "passed " << dbgt++ << "\n" << flush; }
template<class H, class... T> void err(H&& h,T&&... t){ cerr<< h << (sizeof...(t)?" ":"\n") << flush; if(sizeof...(t)>0) err(forward<T>(t)...); }
#endif
const ll INF = 4e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//
int main() {
ll N, L;
cin >> N >> L;
vl A(N), B(N);
rep(i, N) cin >> A[i];
rep(i, N) cin >> B[i];
ll k = 0, ans = 0, last = 0;
rep(i, N){ // 右に走査
if (i < last) A[i] = A[last] - (last - i);
if (A[i] >= B[i] or B[i] == i + 1 or B[i] == L - (N - i - 1)) continue;
chmax(k, i + 1);
while (k < N and B[i] != A[k] - (k - i)) k++;
if(k == N){
cout << -1 << endl;
return 0;
}
ans += k - i, last = k;
A[i] = B[i];
}
k = N, last = N;
repr(i, N){ // 左に走査
if (i > last) A[i] = A[last] + (i - last);
if (A[i] <= B[i] or B[i] == i + 1 or B[i] == L - (N - i - 1)) continue;
chmin(k, i - 1);
while (k >= 0 and B[i] != A[k] + (i - k)) k--;
if(k == -1){
cout << -1 << endl;
return 0;
}
ans += i - k, last = k;
A[i] = B[i];
}
rep(i, N){ // 端点
if (A[i] == B[i]) continue;
ans++;
}
cout << ans << endl;
} | #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
#define all(x) (x).begin(),(x).end()
using namespace std;
constexpr int INF = 1e9, MOD = 1e9 + 7;
constexpr int64_t LINF = 5e18, LMOD = 998244353;
// #include <atcoder/all>
// using namespace atcoder;
// const int dy[]={0,-1,0,1,1,-1,-1,1};
// const int dx[]={1,0,-1,0,1,1,-1,-1};
int n, m;
int k;
int c[18];
vector<int> edge[100010];
int64_t dist[18][100010];
void f() {
rep(i,k)rep(j,n) {
dist[i][j] = INF;
}
for(int i = 0; i < k; ++i) {
queue<int> que;
dist[i][c[i]] = 0;
que.push(c[i]);
while(not que.empty()) {
int from = que.front();
que.pop();
for(int to : edge[from]) {
if(dist[i][from] + 1 >= dist[i][to]) continue;
que.push(to);
dist[i][to] = dist[i][from] + 1;
}
}
}
}
int64_t dp[1 << 18][18];
int main() {
cin >> n >> m;
rep(i,m) {
int a, b; cin >> a >> b;
--a, --b;
edge[a].push_back(b);
edge[b].push_back(a);
}
cin >> k;
rep(i,k) {
cin >> c[i];
--c[i];
}
f();
rep(i,(1<<k))rep(j,k) dp[i][j] = INF;
rep(i,k) {
dp[1 << i][i] = 1;
}
for(int mask = 1; mask < (1 << k); ++mask) {
for(int i = 0; i < k; ++i) {
if(mask >> i & 1) {
rep(j,k) {
if(dist[i][c[j]] == INF) continue;
dp[mask | (1 << j)][j] = min(dp[mask | (1 << j)][j], dp[mask][i] + dist[i][c[j]]);
}
}
}
}
int64_t ans = INF;
int ma = (1 << k) - 1;
rep(i,k) {
ans = min(ans, dp[ma][i]);
}
if(ans == INF) {
cout << -1 << '\n';
} else {
cout << ans << '\n';
}
return 0;
}
|
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <string>
#include <list>
#include <cassert>
#include <numeric>
#include <cstdint>
#include <queue>
#include <deque>
using ll = long long;
using ld = long double;
using namespace std;
using Graph = vector<vector<int>>;
using Priority = priority_queue<int>;
// typedef pair<int,int> P;
#define teisu 1000000007
#define MAX_WIDTH 60
#define MAX_HEIGHT 60
#define INF 1e16
ll N,A[100010],B[100010],C[100010];
map<ll,ll> link;
map<ll,ll> cnt;
int main(){
cin >> N;
for(int i=0;i<N;i++){
cin >> A[i];
cnt[A[i]]++;
}
for(int i=0;i<N;i++) {
cin >> B[i];
}
for(int i=0;i<N;i++) {
cin >> C[i];
C[i]--;
int index = B[C[i]];
link[index]++;
}
ll ans = 0;
for(int i=1;i<=N;i++){
ans += cnt[i]*link[i];
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define int long long
typedef vector<int> vi;
#define all(x) x.begin(), x.end()
#define FIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define endl "\n"
#define pb push_back
#define mp make_pair
#define re(i,n) for(int i=0;i<(n);i++)
#define re1(i, k, n) for (int i = k; k < n ? i <= n : i >= n; k < n ? i += 1 : i -= 1)
#define FORD(i,a,b) for(int i=(a);i>=(b);i--)
typedef pair<int, int> pii;
typedef priority_queue<pii, vector<pii>, greater<pii>> minpq;
typedef priority_queue<pii> maxpq;
#define mod 1000000007
//===================================DEBUG TEMPLATE=================================================
void __print(int x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x)
{
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T>
void __print(const T &x)
{
int f = 0;
cerr << '{';
for (auto &i : x)
cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v)
{
__print(t);
if (sizeof...(v))
cerr << ", ";
_print(v...);
}
//====================================DEBUG TEMPLATE==============================================
int32_t main()
{
FIO;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin) ;
freopen("output.txt", "w", stdout) ;
#endif
int n;
cin >> n;
vi a(n + 1), b(n + 1), c(n + 1);
re(i, n) {
cin >> a[i + 1];
}
re(i, n) {
cin >> b[i + 1];
}
re(i, n) {
cin >> c[i + 1];
}
map<int, int> mp;
re(i, n) {
mp[b[c[i + 1]]]++;
}
int ans = 0;
re(i, n) {
auto it = mp.find(a[i + 1]);
if (it != mp.end())
ans += it->ss;
}
cout << ans;
return 0;
} |
#include<iostream>
#include<cstdio>
using namespace std;
const int M=1e9+7;
int T,n,a,b,ans;
int mo2(int x){
return (x<0)?x+M:x;
}
int get_sum(int l,int r,int k,int t){
int key1=l*k+t,key2=r*k+t;
return 1ll*(key1+key2)*(r-l+1)/2%M;
}
int main(){
scanf("%d",&T);
while (T--){
scanf("%d%d%d",&n,&a,&b);
if (n<a+b){
puts("0");
continue;
}
ans=mo2((1ll*(n-b+1)*(a+b-1)-get_sum(0,a-1,-1,a-1)-get_sum(n-a-b+1,n-b,1,-(n-a-b+1)))%M);
printf("%d\n",mo2((1ll*(n-a+1)*(n-a+1)%M*(n-b+1)%M*(n-b+1)-1ll*ans*ans)%M));
}
return 0;
} | #include <bits/stdc++.h>
typedef long long LL;
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define RALL(X) (X).rbegin(), (X).rend()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define DRVI(N, X) VI X(N); for (int ___I=0; ___I<N; ___I++) scanf("%d", &(X[___I]))
#define RS(X) scanf("%s", (X))
#define CASET int ___T, case_n = 1; scanf("%d ", &___T); while (___T-- > 0)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define PII pair<int,int>
#define VI vector<int>
#define VL vector<long long>
#define VPII vector<pair<int,int> >
#define PLL pair<long long,long long>
#define VPLL vector<pair<long long,long long> >
#define F first
#define S second
using namespace std;
int main(){
DRII(h,w);
DRII(n,m);
vector<VI> v(h, VI(w,0));
vector<pair<PII,int>> b;
REP(i,n){
DRII(x,y);
x--;y--;
v[x][y]=1;
b.PB({{x,y},0});
}
REP(i,m){
DRII(x,y);
x--;y--;
v[x][y]=-1;
}
int it=0;
VI rv(h*w,0);
int res=0;
while (it<SZ(b)){
int rr;
if (b[it].S==0) rr=15;
else rr=(1<<(b[it].S-1));
if (rr^(rr&rv[b[it].F.F*w+b[it].F.S])){
if (rv[b[it].F.F*w+b[it].F.S]==0) res++;
rv[b[it].F.F*w+b[it].F.S]|=rr;
}
else{
it++;
continue;
}
if (b[it].S==0 or b[it].S==1){
int x=b[it].F.F;
int y=b[it].F.S;
if (x+1<h and v[x+1][y]!=-1){
b.PB({{x+1,y},1});
}
}
if (b[it].S==0 or b[it].S==2){
int x=b[it].F.F;
int y=b[it].F.S;
if (y+1<w and v[x][y+1]!=-1){
b.PB({{x,y+1},2});
}
}
if (b[it].S==0 or b[it].S==3){
int x=b[it].F.F;
int y=b[it].F.S;
if (x>0 and v[x-1][y]!=-1){
b.PB({{x-1,y},3});
}
}
if (b[it].S==0 or b[it].S==4){
int x=b[it].F.F;
int y=b[it].F.S;
if (y>0 and v[x][y-1]!=-1){
b.PB({{x,y-1},4});
}
}
it++;
}
printf("%d\n",res);
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#define trace(...) debug(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void debug(const char* name, Arg1&& arg1){
cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void debug(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << ": " << arg1 << " | ";
debug(comma + 1, args...);
}
#define mp(x,y) make_pair(x,y)
const int INF=1000000000+5;
const int N=1e6+5;
const ll oo=1e18+5;
const ll mod=998244353;
ll gcd(ll a,ll b,ll& num1,ll& num2){
if(b==0){
num1=1;
num2=0;
return a;
}
ll x,y;
ll g=gcd(b,a%b,x,y);
num1=y;
num2=x-y*(a/b);
return g;
}
void solve(){
ll n,s,k;
cin>>n>>s>>k;
//~ s+a*k==b*n
//~ b*n-a*k=s;
//~ s%gcd(n,k)==0
//~ b*n/g-a*k/g=s/g
//~ b'*n-a'*k=1
ll num1,num2;
ll g=gcd(k,n,num1,num2);
if(s%g){
cout<<-1<<endl;
return;
}
num1*=s/g;
num2*=s/g;
n/=g;
k/=g;
if(num1>=0){
ll w=num1/n;
num1-=w*n;
num1-=n;
num1=-num1;
}
else{
ll w=abs(num1)/n;
num1+=w*n;
num1=-num1;
}
cout<<num1<<endl;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0);
int t;
cin>>t;
while(t--) solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
#define FOR(i, a, b) for(int i = a; i < b; i++)
ll MOD = 1000000007;
ll power(ll base, ll n){
if (n == 0) return 1;
if (n == 1) return base;
ll halfn = power(base, n/2);
if (n % 2 == 0) return (halfn * halfn) % MOD;
return (((halfn * halfn) % MOD) * base) % MOD;
}
ll inverse(ll n){
return power(n, MOD-2);
}
int main(){
ios::sync_with_stdio(false);
int a, b, c; cin >> a >> b >> c;
double dp[101][101][101];
FOR(i, 0, 101){
FOR(j, 0, 101){
FOR(k, 0, 101){
dp[i][j][k] = 0;
}
}
}
dp[99][99][99] = 1;
for (int i = 99; i >= 0; i--){
for (int j = 99; j >= 0; j--){
for (int k = 99; k >= 0; k--){
if (i == 99 && j == 99 && k == 99){
dp[i][j][k] = 1; continue;
}
if (a > 0) dp[i][j][k] += (dp[i+1][j][k] + 1) * i;
if (b > 0) dp[i][j][k] += (dp[i][j+1][k] + 1) * j;
if (c > 0) dp[i][j][k] += (dp[i][j][k+1] + 1) * k;
dp[i][j][k] = dp[i][j][k] / (i+j+k);
}
}
}
cout << setprecision(18) << dp[a][b][c] << '\n';
return 0;
} |
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#define f first
#define s second
#define fore(i,a,b) for(int i = (a), ThxMK = (b); i < ThxMK; ++i)
#define pb push_back
#define all(s) begin(s), end(s)
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define sz(s) int(s.size())
#define ENDL '\n'
using namespace std;
typedef long double ld;
typedef long long lli;
typedef pair<lli,lli> ii;
typedef vector<lli> vi;
#define deb(x) cout << #x": " << (x) << endl;
int main(){ _
lli n; cin >> n;
if(n == 1) {
cout << 2 << ENDL;
return 0;
}
lli ans = 0;
lli cur = 0;
fore(i, 1, 1e8) {
cur += i;
if(cur > n) break;
lli x = n - cur;
if(x % i == 0) ++ans;
}
cout << ans * 2LL << ENDL;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
bool ok (ll len, ll tot)
{
ll tmp = tot / len;
return (tmp + 1 - len) % 2 == 0;
}
int main()
{
ll n; scanf("%lld", &n);
n <<= 1;
int cnt = 0;
for (ll i = 1; i * i <= n; i++)
{
if (n % i) continue;
cnt += ok(i, n);
if (i * i != n) cnt += ok(n / i, n);
}
printf("%d\n", cnt);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define lc (rt << 1)
#define rc ((rt << 1) | 1)
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define per(i, r, l) for (int i = (r); i >= (l); --i)
#define PE(i, u) for (int i = head[u]; i != -1; i = edge[i].next)
typedef long long LL;
const int maxn = 1e6 + 20;
const int mod = 1e9 + 7;
LL y;
LL qpow(LL a, int n){
LL res = 1;
while(n){
if(n & 1){
res = res * a % mod;
}
n >>= 1;
a = a * a % mod;
}
return res;
}
LL cal(LL t){
return t * (t + 1) % mod * y % mod;
}
int main(int argc, char const *argv[])
{
y = qpow(2, mod - 2);
int t;
scanf("%d", &t);
while(t--){
LL n, a, b;
scanf("%lld%lld%lld", &n, &a, &b);
// printf("[%lld %lld %lld]\n", n, a, b);
if(a > b) swap(a, b);
// printf("[%lld %lld %lld]\n", n, a, b);
LL num1 = (n - a + 1) * (n - a + 1) % mod, num2 = (n - b + 1) * (n - b + 1) % mod;
// printf("num1 = %lld num2 = %lld\n", num1, num2);
LL ans = num1 * num2 % mod;
// printf("777ans = %lld\n", ans);
ans = (ans - (b - a + 1) * (b - a + 1) % mod * num2 % mod + mod) % mod;
// printf("88ans = %lld\n", ans);
LL ma = min(a - 1, n - b);
// printf("ma = %lld\n", ma);
// printf("7777\n");
LL res = cal(ma) * (b - a + 1) % mod * (n - b + 1) % mod;
// printf("res = %lld\n", res);
// printf("9999\n");
if(n - b > a - 1){
res = (res + (n - b - a + 1) * (a - 1) % mod * (b - a + 1) % mod * (n - b + 1) % mod) % mod;
}
// printf("res = %lld\n", res);
ans = (ans - 4LL * res % mod + mod) % mod;
ma = min(b + a - 1, n);
LL mi = min(b + 1, ma);
// printf("a = %lld\n", a);
if(a != 1){
// printf("ansans = %lld\n", ans);
// printf("[ma = %lld mi = %lld]\n", ma, mi);
LL num3 = (cal(ma - 1) - cal(mi - 2) + mod) % mod;
// printf("num3 = %lld\n", num3);
res = (n * n % mod * (ma - mi + 1) % mod * (ma - mi + 1) % mod - n * num3 % mod * (ma - mi + 1) % mod * 2LL % mod + num3 * num3 % mod + 10LL * mod) % mod;
// printf("res = %lld\n", res);
ans = (ans - 4LL * res % mod + mod) % mod;
}
printf("%lld\n", ans);
}
return 0;
} | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DEBUG 1
using namespace std;
constexpr int kMod = 1000000007;
typedef long long LL;
LL sub(LL a, LL b) { return (a - b + kMod) % kMod; }
LL mul(LL a, LL b) { return a * b % kMod; }
LL solve() {
LL N, A, B; cin >> N >> A >> B;
if (N < A + B) return 0;
LL all = mul(N - A + 1, N - B + 1);
LL X3 = mul(N - A - B + 1, N - A - B + 2);
LL X2 = sub(all, X3);
LL X1 = mul(X2, X2);
return sub(mul(all, all), X1);
}
int main() {
int T; cin >> T;
for (int i = 0; i < T; ++i) {
std::cout << solve() << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main()
{
char s , t;
cin >> s >> t;
if(s == 'Y' )cout << char (toupper(t)) << endl;
else cout << t << endl;
}
| #include<stdio.h>
#include<string.h>
int main(void){
char S,T;
S=fgetc(stdin);
T=fgetc(stdin);
scanf("%c",&T);
if(S=='Y'){
if(T=='a'){
printf("A");
}
else{
if(T=='b'){
printf("B");
}
else{
if(T=='c'){
printf("C");
}
}
}
}
else{
printf("%c\n",T);
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define MS(a, b) memset(a, b, sizeof(a))
#define REP(a, b, c) for (register int a = b, _n = c; a <= _n; ++a)
#define DREP(a, b, c) for (register int a = b, _n = c; a >= _n; --a)
#define FOR(a, b, c) for (register int a = b, _n = c; a < _n; ++a)
#define EREP(a, b) for (register int a = head[b]; a; a = edge[a].nxt)
inline int rd() {
int x = 0; char c = getchar(); bool f = 0;
while (c < '0' && c != '-') c = getchar();
if (c == '-') f = 1, c = getchar();
while (c >= '0') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
return f ? -x : x;
}
const int SIZE = 2e5 + 100;
int n;
int head[SIZE], ecnt;
struct Edge {
int v, nxt;
} edge[SIZE << 1];
void Add_Edge (int u, int v) {
edge[++ecnt] = (Edge) { v, head[u] };
head[u] = ecnt;
}
int dep[SIZE], F[SIZE];
int mx[SIZE][2];
int Len = -1, A, B;
void dfs (int u, int fa) {
F[u] = fa;
dep[u] = dep[fa] + 1;
mx[u][0] = mx[u][1] = u;
EREP (i, u) {
int v = edge[i].v;
if (v == fa) continue;
dfs(v, u);
if (dep[mx[v][0]] > dep[mx[u][0]])
mx[u][1] = mx[u][0], mx[u][0] = mx[v][0];
else if (dep[mx[v][0]] > dep[mx[u][1]])
mx[u][1] = mx[v][0];
}
if (dep[mx[u][0]] + dep[mx[u][1]] - 2 * dep[u] > Len) {
Len = dep[mx[u][0]] + dep[mx[u][1]] - 2 * dep[u];
A = mx[u][0];
B = mx[u][1];
}
}
bool mark[SIZE];
int E;
int ans[SIZE];
void D (int u, int fa) {
++E;
ans[u] = E;
EREP (i, u) {
int v = edge[i].v;
if (v == fa) continue;
if (!mark[v]) D(v, u);
}
EREP (i, u) {
int v = edge[i].v;
if (v == fa) continue;
if (mark[v]) D(v, u);
}
++E;
}
void _main () {
n = rd();
FOR (i, 1, n) {
int u = rd(), v = rd();
Add_Edge(u, v);
Add_Edge(v, u);
}
dfs(1, 0);
int T = A;
while (A != B) {
if (dep[A] < dep[B]) swap(A, B);
mark[A] = 1;
A = F[A];
} mark[A] = 1;
D(T, 0);
REP (i, 1, n) printf ("%d ", ans[i]);
}
int main () {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif
_main();
return 0;
}
| #include <cstdio>
#include <iostream>
#include <cassert>
#include <string>
#include <algorithm>
#include <cstring>
#include <utility>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <unordered_map>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0)
using namespace std;
using namespace __gnu_pbds;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> order_set;
const int MAXN = 123;
int dp[MAXN][MAXN];
char s[MAXN];
int ds[MAXN];
int pw2[MAXN];
int vs(int a, int b) {
if (a == 1) {
return b != 2;
} else if (a == 2) {
return b != 3;
}
return b != 1;
}
int dfs(int n, int S, int N) {
if (dp[n][S]) return dp[n][S];
if (S == 1) {
int a = ds[(n % N) + 1], b = ds[((n + 1) % N) + 1];
return dp[n][S] = (vs(a, b) ? a : b);
}
int l = dfs(n, S - 1, N), r = dfs((n + pw2[S - 1]) % N, S - 1, N);
return dp[n][S] = (vs(l, r) ? l : r);
}
int main() {
int N, K;
scanf("%d %d", &N, &K);
scanf("%s", s + 1);
for (int i = 1; i <= N; i++) {
if (s[i] == 'R') ds[i] = 1;
else if (s[i] == 'P') ds[i] = 2;
else ds[i] = 3;
}
pw2[0] = (1 % N);
for (int i = 1; i <= N; i++)
pw2[i] = (pw2[i - 1] * 2) % N;
char V[] = {0, 'R', 'P', 'S'};
printf("%c\n", V[dfs(0, K, N)]);
return 0;
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
by Benq;
*/
|
Subsets and Splits