code_file1
stringlengths 87
4k
| code_file2
stringlengths 85
4k
|
---|---|
#include <bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
int n, len, sum = 0;
map < int, int > haha;
string str;
cin >> str;
len = str.size();
for(auto &a : str){
n = (a - '0') % 3;
haha[n]++;
sum += n;
}
sum = sum - ((sum / 3) * 3);
if(sum % 3 == 0)
cout << 0 << "\n";
else if(sum % 3 == 2 && len > 1 && haha[2] >= 1)
cout << 1 << "\n";
else if(sum % 3 == 2 && len > 2 && haha[1] >= 2)
cout << 2 << "\n";
else if(sum % 3 == 1 && len > 1 && haha[1] >= 1)
cout << 1 << "\n";
else if(sum % 3 == 1 && len > 2 && haha[2] >= 2)
cout << 2 << "\n";
else
cout << "-1\n";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main () {
int N=0;
cin >> N;
int an;
an=N%2;
if (an==1){
cout << "Black" << endl;
}
if (an==0){
cout << "White" << endl;
}
} |
#include <bits/stdc++.h>
#define ll long long
#define endl "\n"
#define spacex ios_base::sync_with_stdio(false);cin.tie(NULL)
using namespace std;
long long int score(string temp)
{
vector<long long int> cnt(10,0);
// iota(cnt.begin(),cnt.end(),0);
// that is, it will assign or make increasing vector by +1
// 0,1,2,3,4,5,6,7,8,9
for(long long int i=0;i<10;i++)
{
cnt[i]=i;
}
for(char x:temp)
{
cnt[x-'0']*=10;
}
return accumulate(cnt.begin(),cnt.end(),0);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("i.txt", "r", stdin);
freopen("o.txt", "w", stdout);
#endif
spacex;
int k;
cin>>k;
string s,t;
cin>>s>>t;
vector<long long int> count(10,k);
for(int i=0;i<4;i++)
{
count[s[i]-'0']--;
}
for(int i=0;i<4;i++)
{
count[t[i]-'0']--;
}
long long int win=0;
for(int i=1;i<=9;i++)
{
for(int j=1;j<=9;j++)
{
s.back()='0'+i;
t.back()='0'+j;
if(score(s)<=score(t))
{
continue;
}
else
{
win+=(count[i]*(count[j]-(i==j)));
}
}
}
const ll rem = 9 * k - 8;
cout << double(win) / rem / (rem - 1) << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
double eps=1e-7;
char a[10],b[10];
long long shi[7]={1,10,100,1000,10000,100000,1000000};
long long aa,bb,K,sum[15],cnta[15],cntb[15];
long long ans1,ans2;
int main()
{
cin>>K;
scanf("%s",a+1);
scanf("%s",b+1);
for(int i=1;i<=9;i++) sum[i]=K;
for(int i=1;i<=4;i++){
sum[a[i]-'0']--;
sum[b[i]-'0']--;
cnta[a[i]-'0']++;
cntb[b[i]-'0']++;
//cout<<a[i]-'0'<<" "<<b[i]-'0'<<endl;
}
// for(int i=1;i<=9;i++){
// cout<<cnta[i]<<" "<<cntb[i]<<endl;
// }
// cout<<endl;
for(int i=1;i<=9;i++){
if(sum[i]){
for(int j=1;j<=9;j++){
if(sum[j]){
aa=0,bb=0;
for(int k=1;k<=9;k++){
aa=aa+k*(shi[cnta[k]+(k==i?1:0)]);
// if(i==9)
// cout<<aa<<endl;
bb=bb+k*(shi[cntb[k]+(k==j?1:0)]);
// if(j==8)
// cout<<bb<<endl;
//cout<<aa<<" "<<bb<<endl;
}
if(aa>bb){
if(i!=j) ans1=ans1+sum[i]*sum[j];
//cout<<i<<" "<<j<<" ";
else ans1=ans1+sum[i]*(sum[i]-1);
//cout<<ans1<<endl;
}
}
}
}
}
ans2=(K*9-8)*(K*9-9);
//cout<<ans1<<" "<<ans2<<endl;
printf("%.10f",(double)ans1/(double)ans2);
return 0;
}
|
#include <cstdio>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <iostream>
#include <sstream>
#include <ostream>
#include <cstring>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <iomanip>
#include <set>
#include <bitset>
#define INT_MINs -2000000000
#define INT_MAXs 1000000001
#define MID int mid=(l+r)/2
#define REP1(i,x,y) for(int i=x;i<y;i++)
#define REP2(i,x,y) for(int i=x;i<=y;i++)
#define ls (2*k)
#define rs (2*k+1)
#define lson l,mid,2*k
#define rson mid+1,r,2*k+1
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define IOS ios::sync_with_stdio(0);
#define pb(a) push_back(a)
#define pi acos(-1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll mod=1000000007;
const ll ba=131;
ll qk(ll x,ll y){ll ans=1;while(y){if(y&1) ans=ans*x%mod;y>>=1;x=x*x%mod;}return ans;}
const int dx[8] = { 0,-1,0,1,-1,-1,1,1}, dy[8] = { -1,0,1,0,-1,1,-1,1};
const int dxx[8]= {2,1,-1,-2,-2,-1, 1, 2},dyy[8]= {1,2,2,1, -1,-2,-2,-1};
int a[200005],b[200005];
ll c[200005];
int d[400005];
ll num[400005];
int cnt;
void solve()
{
int n;
ll m;
scanf("%d%lld",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d%d%lld",&a[i],&b[i],&c[i]);
d[++cnt]=a[i];
d[++cnt]=b[i]+1;
}
sort(d+1,d+1+cnt);
cnt=unique(d+1,d+1+cnt)-(d+1);
//printf("%d\n",cnt);
for(int i=1;i<=n;i++){
int l=lower_bound(d+1,d+1+cnt,a[i])-d;
int r=lower_bound(d+1,d+1+cnt,b[i]+1)-d;
num[l]+=c[i];
num[r]-=c[i];
}
ll sum=0;
for(int i=1;i<=cnt;i++){
num[i]+=num[i-1];
if(m<=num[i]){
sum+=m*1ll*(d[i+1]-d[i]);
}else{
sum+=num[i]*1ll*(d[i+1]-d[i]);
}
}
printf("%lld\n",sum);
}
int main()
{
//IOS;
solve();
return 0;
}
| #include <bits/stdc++.h>
#define rep(i,n) for (int i=0; i<(n); ++i)
#define sz(x) int(x.size())
#define show(x) {for(auto i:x){cout << i << " ";} cout << endl;}
#define isin(x,l,r) ((l)<=(x) && (x)<(r))
using namespace std;
using ll=long long;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
template<typename T>bool chmin(T&x,const T&y) {if(x>y){x=y;return true;} else return false;}
template<typename T>bool chmax(T&x,const T&y) {if(x<y){x=y;return true;} else return false;}
int main() {
ll N, C;
cin >> N >> C;
vector<pii> event;
rep(i, N) {
int a, b, c;
cin >> a >> b >> c;
--a; --b;
event.emplace_back(a, c);
event.emplace_back(b+1, -c);
}
sort(event.begin(), event.end());
ll ans = 0, temp = 0, now = 0;
for (auto [day, cost] : event) {
if (day != now) {
ans += min(C, temp) * (day - now);
temp += cost;
now = day;
} else temp += cost;
}
cout << ans << '\n';
return 0;
} |
#include <iostream>
using namespace std;
int main(){
double a,b,c,d;
cin>>a>>b>>c>>d;
if(a+b==c+d){
cout<<"Yes"<<endl;
}
else if(a+c==b+d){
cout<<"Yes"<<endl;
}
else if(a+d==b+c){
cout<<"Yes"<<endl;
}
else if(a+b+c==d)
cout<<"Yes"<<endl;
else if(b+d+c==a)
cout<<"Yes"<<endl;
else if(c==a+b+d)
cout<<"Yes"<<endl;
else if(b==a+c+d)
cout<<"Yes"<<endl;
else {
cout<<"No"<<endl;
}
} | #include <bits/stdc++.h>
#define F first
#define S second
#define MP make_pair
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define LCM(a, b) (a) / __gcd((a), (b)) * (b)
#define CEIL(a, b) (a)/(b)+(((a)%(b))?1:0)
#define log_2(a) (log((a)) / log(2))
#define ln '\n'
using namespace std;
using LL = long long;
using ldouble = long double;
using P = pair<int, int>;
using LP = pair<LL, LL>;
static const int INF = INT_MAX;
static const LL LINF = LLONG_MAX;
static const int MIN = INT_MIN;
static const LL LMIN = LLONG_MIN;
static const int MOD = 1e9 + 7;
static const int SIZE = 200005;
const int dx[] = {0, -1, 1, 0};
const int dy[] = {-1, 0, 0, 1};
vector<LL> Div(LL n) {
vector<LL> ret;
for(LL i = 1; i * i <= n; ++i) {
if(n % i == 0) {
ret.pb(i);
if(i * i != n) ret.pb(n / i);
}
}
sort(all(ret));
return ret;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int a[4];
for(int i = 0; i < 4; ++i) cin >> a[i];
for(int i = 1; i < 16; ++i) {
int s = 0, t = 0;
for(int j = 0; j < 4; ++j) {
if((i >> j) & 1) s += a[j];
else t += a[j];
}
if(s == t) {
cout << "Yes" << endl;
return 0;
}
}
cout << "No" << endl;
return 0;
}
|
#include<bits/stdc++.h>
#define BOOM ios_base::sync_with_stdio(false);cin.tie(NULL);cout<<setprecision(10);
#define NEWLINE cout<<'\n';
#define ll long long
#define pb push_back
#define all(v) v.begin(), v.end()
#define MOD 1000000007
#define mod 998244353
#define ff first
#define ss second
#define mset(a,b) memset(a, b, sizeof(a))
using namespace std;
#define MAXN 100005
int fastscan()
{
bool negative = false;
register int c;
int number = 0;
for(c = getchar(); !(c>47 && c<58) && c!= '-'; c= getchar());
if (c=='-') c = getchar(), negative = true;
for (; (c>47 && c<58); c=getchar()) number = (number<<1) + (number<<3) + (c ^ 48);
if (negative) return -number;
return number;
}
int gcd(int a, int b) { if(!b) return a; return gcd(b,a%b); }
//LARGE POWERS
ll power(long long x, long long y, long long p)
{
ll res = 1;
x = x % p;
if (x == 0) return 0;
while (y > 0){
if (y & 1) res = (res*x) % p;
y = y>>1;
x = (x*x) % p;
}
return res;
}
struct 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);
}
};
int N;
bool sortbyff(const pair<int,int> &a, const pair<int,int> &b) {
if(a.ff!=b.ff) return (a.ff<b.ff);
return (abs(N - a.ss) > abs(N - b.ss));
}
int main()
{
BOOM
int _=1, n, m, k, l=0, r=0, b, c, i, j, tt=1, t, sum=0,x,y,z=1;
bool flag=0;
// string s,ss;
// tt = fastscan();
// cin>>tt;
// while(tt--){
for(_=1;_<=tt;_++) {
// int q;
cin>>N>>m;
// for(i=0;i<n;i++) cin>>a[i];
int cur=0, sum=0, ans=0;
pair<int,int> a[m];
for(i=0;i<m;i++) cin>>a[i].ff>>a[i].ss;
sort(a, a+m, sortbyff);
// if(a[0].ff == n && )
// cout<<"a = \n"; for(auto i:a) cout<<i.ff<<' '<<i.ss<<'\n'; NEWLINE
unordered_map<int,pair<int,int>,HASH> w;
set<int> s;
s.insert(N); w[N] = {INT_MAX,0};
// cout<<"set = "; for(int j:s) cout<<j<<' '; NEWLINE
std::vector<int> v;
for(i=0;i<m;i++) {
if (s.count(a[i].ss - 1) > 0 && w.find(a[i].ss - 1) != w.end() && (w[a[i].ss - 1].ff < a[i].ff || w[a[i].ss - 1].ss < a[i].ff) ||
s.count(a[i].ss + 1) > 0 && w.find(a[i].ss + 1) != w.end() && (w[a[i].ss + 1].ff < a[i].ff || w[a[i].ss + 1].ss < a[i].ff)) {
s.insert(a[i].ss);
if (w.find(a[i].ss) == w.end())
w[a[i].ss] = { INT_MAX, a[i].ff };
else
w[a[i].ss] = { w[a[i].ss].ss, a[i].ff };
}
else
// s.erase(a[i].ss), w.erase(a[i].ss);
v.push_back(a[i].ss);
if(i==n-1 || a[i+1].ff != a[i].ff) {
for(int x : v)
s.erase(x), w.erase(x);
v.clear();
}
// cout<<"set = "; for(int j:s) cout<<j<<' '; NEWLINE
}
// w.clear(); for(int i:s) w[i] = 1;
cout<<s.size();
// cout<<"dp = "; for(int i:dp) cout<<i<<' '; NEWLINE
// cout<<*max_element(dp, dp+n);
// cout<<"Case #"<<_<<": "<<ans<<'\n';
// if(!flag)
// cout<<-1<<'\n';
// cout<<"YES"<<"\n";
// else
// cout<<"NO"<<"\n";
}
return 0;
} | #include <bits/stdc++.h>
#define Fast cin.tie(0), ios::sync_with_stdio(0)
#define All(x) x.begin(), x.end()
#define louisfghbvc int t; cin >> t; for(int tt = 0; tt < t; ++tt)
#define sz(x) (int)(x).size()
#define sort_unique(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()));
using namespace std;
typedef long long LL;
typedef pair<LL, LL> ii;
typedef vector<LL> vi;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T> istream& operator>>(istream &is, vector<T> &v) { for(auto &it : v) is >> it; return is; }
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep = ""; for(const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
void dbg_out() { cerr << " end.\n"; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
const int N = 800 + 5;
const int INF = 0x3f3f3f3f;
const int mod = 1e9+7;
/**
Read problem statement carefully
**/
int arr[N][N];
int n, k;
bool check(int mid, int L){
vector<vector<int>> pre(n+1, vector<int>(n+1));
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= n; ++j)
pre[i][j] = pre[i-1][j] + pre[i][j-1] - pre[i-1][j-1] + (arr[i-1][j-1] >= mid);
bool ok = 1;
for(int i = 0; i+k <= n; ++i){
for(int j = 0; j+k <= n; ++j){
int res = pre[i+k][j+k] - pre[i+k][j] - pre[i][j+k] + pre[i][j];
if(res < L) ok = 0;
}
}
return ok;
}
void solve(int T){
int n, m;
cin >> n >> m;
map<int, vector<int>> mp;
for(int i = 0, x, y; i < m; ++i){
cin >> x >> y;
mp[x].push_back(y);
}
set<int> st;
st.insert(n);
for(auto &[x, v]: mp){
vector<int> add;
for(auto &y: v){
if(st.count(y-1) || st.count(y+1))
add.push_back(y);
}
for(auto &y: v) st.erase(y);
for(auto &y: add) st.insert(y);
}
cout << sz(st) << "\n";
}
int main()
{
Fast;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// louisfghbvc
solve(1);
return 0;
}
/**
Enjoy the problem.
**/
|
#include <iostream>
#include <vector>
#include <math.h>
#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;
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; }
int main() {
long long l;
cin >> l;
long long sa = l - 1;
long long ans = 1;
for(int i = 1; i <= 11; i++){
ans *= sa;
ans /= i;
sa--;
}
cout << ans << endl;
} | #include<bits/stdc++.h>
#define ll long long
#define M 1000000007
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
srand(chrono::high_resolution_clock::now().time_since_epoch().count());
int n;
cin>>n;
float res = 1.08*n;
res = round(res);
// cout<<res<<"\n";
if(res<206)
cout<<"Yay!"<<"\n";
else if(res==206.0)
cout<<"so-so"<<"\n";
else
cout<<":("<<"\n";
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define rep(i,j,n) for(int i=j;i<n;i++)
#define readvec(v,n) for(int i=0;i<n;i++){cin>>v[i];}
#define MOD 1000000007
int n,m;
vector<vector<int>> v;
vector<vector<int>> dp;
int rec(int i,int j)
{
if(dp[i][j]!=INT_MIN)return dp[i][j];
int a=INT_MIN,b=INT_MIN;
if(i+1<n)a=rec(i+1,j);
if(j+1<m)b=rec(i,j+1);
dp[i][j]=v[i][j]-max(a,b);
return dp[i][j];
}
int main()
{
cin>>n>>m;
v = vector<vector<int>>(n,vector<int>(m,0));
dp = vector<vector<int>>(n,vector<int>(m,INT_MIN));
rep(i,0,n)rep(j,0,m){char ch;cin>>ch;if(ch=='-')v[i][j]=-1; else v[i][j]=1;}
dp[n-1][m-1] = v[n-1][m-1];
int ans = v[0][0]-rec(0,0);
//cout<<ans;
if(ans<0)cout<<"Aoki\n"; else if(ans>0)cout<<"Takahashi\n"; else cout<<"Draw\n";
return 0;
}
| #include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <deque>
#include <iostream>
#include <iomanip>
#include <locale>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int INT_MAX_SQRT = sqrt(INT_MAX);
int pwi(int x, int p) {
if (p == 0) return 1;
if (p == 1) return x;
int tmp = pwi(x, p / 2);
if (tmp > INT_MAX_SQRT) return 0; // overflow
if (p % 2 == 0)
return tmp * tmp;
else
return tmp * tmp * x;
}
long LONG_MAX_SQRT = sqrt(LONG_MAX);
long pwl(long x, long p) {
if (p == 0) return 1;
if (p == 1) return x;
long tmp = pwl(x, p / 2);
if (tmp > LONG_MAX_SQRT) return 0; // overflow
if (p % 2 == 0)
return tmp * tmp;
else
return tmp * tmp * x;
}
map<long, long> factorization(long N) {
map<long, long> res;
long SQRT_N = ceil(sqrt(N));
for (long i = 2; i < SQRT_N; i++) {
while (N % i == 0) {
if (res.find(i) == res.end()) {
res[i] = 1;
} else {
res[i] += 1;
}
N /= i;
}
}
if (N > SQRT_N) res[N] = 1;
return res;
}
void debug_vec(string name, vector<int> vec) {
cout << name << endl;
for (auto e: vec) cout << e << endl;
}
void debug_vec(string name, vector<long> vec) {
cout << name << endl;
for (auto e: vec) cout << e << endl;
}
void debug_mat(string name, vector<vector<long>> mat) {
cout << name << endl;
for (auto vec: mat) {
for (auto e: vec) cout << e << " ";
cout << endl;
}
}
int tonum(string s, vector<char> c, vector<int> v) {
int ans = 0;
for (int n = 0; n < c.size(); n++) {
int i = distance(c.begin(), find(c.begin(), c.end(), s[n]));
ans = ans * 10 + v[i];
}
return ans;
}
void ppp(string s, vector<char> c, vector<int> v) {
for (int n = 0; n < c.size(); n++) {
int i = distance(c.begin(), find(c.begin(), c.end(), s[n]));
cout << v[i];
}
cout << endl;
}
int main() {
int H, W;
cin >> H >> W;
vector<vector<int>> grid(H), dp(H);
for (int h = 0; h < H; h++) {
grid[h] = vector<int>(W);
char c;
for (int w = 0; w < W; w++) {
cin >> c;
grid[h][w] = (c == '+' ? 1 : -1) * ((h + w) % 2 == 1 ? 1 : -1);
}
dp[h] = vector<int>(W);
}
for (int w = W - 2; w >= 0; w--)
dp[H - 1][w] = dp[H - 1][w + 1] + grid[H - 1][w + 1];
for (int h = H - 2; h >= 0; h--) {
dp[h][W - 1] = dp[h + 1][W - 1] + grid[h + 1][W - 1];
for (int w = W - 2; w >= 0; w--) {
int r = dp[h + 1][w] + grid[h + 1][w];
int d = dp[h][w + 1] + grid[h][w + 1];
if ((h + w) % 2 == 0)
dp[h][w] = r > d ? r : d;
else
dp[h][w] = r > d ? d : r;
}
}
if (dp[0][0] > 0)
cout << "Takahashi" << endl;
else if (dp[0][0] < 0)
cout << "Aoki" << endl;
else
cout << "Draw" << endl;
return 0;
}
|
#include <bits/stdc++.h>
/* #include <atcoder/lazysegtree> */
#include <iostream>
using namespace std;
/* using namespace atcoder; */
using pint = pair<int, int>;
using ll = long long;
using ull = unsigned long long;
using vll = vector<long long>;
using pll = pair<ll, ll>;
#define FOR(i, begin, end) \
for (long long i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end) \
for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define VREP(s, ite) for (auto ite = s.begin(); ite != s.end(); ++ite)
#define FI first
#define SE second
#define ALL(v) v.begin(), v.end()
#define endl "\n"
#define ciosup \
cin.tie(0); \
ios::sync_with_stdio(false);
#define eb emplace_back
#define vint vector<int>
constexpr ll INF = 1e15 + 7LL;
constexpr ll MOD = 1e9 + 7LL;
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
for (int i = 0; i < v.size(); ++i) {
is >> v[i];
}
return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
if (v.size() == 0) return os;
for (int i = 0; i < v.size() - 1; ++i) {
os << v[i] << " ";
}
os << v[v.size() - 1];
return os;
}
void solve() {
int n, k;
cin >> n >> k;
string s;
cin >> s;
string tmpstr = s;
while (k >= 1) {
if (tmpstr.size() % 2 == 1) {
string tmp = tmpstr;
REP(i, tmp.size()) {
tmpstr.push_back(tmp[i]);
}
}
string newstr;
REP(i, tmpstr.size()/2) {
char f1 = tmpstr[2*i], f2 = tmpstr[2*i+1];
if (f1 == f2) {
newstr.push_back(f1);
} else if (((f1 == 'R') && (f2 == 'S')) || ((f2 == 'R') && (f1 == 'S'))) {
newstr.push_back('R');
} else if (((f1 == 'P') && (f2 == 'R')) || ((f2 == 'P') && (f1 == 'R'))) {
newstr.push_back('P');
} else if (((f1 == 'S') && (f2 == 'P')) || ((f2 == 'S') && (f1 == 'P'))) {
newstr.push_back('S');
}
}
tmpstr = newstr;
--k;
}
cout << tmpstr[0] << endl;
}
int main() {
solve();
char tmp;
while (cin >> tmp) {
cin.putback(tmp);
solve();
}
}
| #include <bits/stdc++.h>
#define bug1(x) cout<<#x<<" = "<<x<<endl
#define bug2(x,y) cout<<#x<<" = "<<x<<" "<<#y<<" = "<<y<<endl
#define bug3(x,y,z) cout<<#x<<" = "<<x<<" "<<#y<<" = "<<y<<" "<<#z<<" = "<<z<<endl
#define bug4(x,y,z,m) cout<<#x<<" = "<<x<<" "<<#y<<" = "<<y<<" "<<#z<<" = "<<z<<" "<<#m<<" = "<<m<<endl
using namespace std;
template<class L, class R>
ostream& operator<<(ostream &os, const pair<L,R> &input) {
os << "(";
os << input.first << "," << input.second;
return os << ")";
}
template<class T>
ostream& operator<<(ostream &os, const vector<T> &input) {
os << "[ ";
for(auto elem : input) {
os << elem << " ";
}
return os << "]";
}
template<class T>
ostream& operator<<(ostream &os, const set<T> &input) {
os << "{ ";
for(auto elem : input) {
os << elem << " ";
}
return os << "}";
}
template<class T>
ostream& operator<<(ostream &os, const unordered_set<T> &input) {
os << "{ ";
for(auto elem : input) {
os << elem << " ";
}
return os << "}";
}
template<class T>
ostream& operator<<(ostream &os, const multiset<T> &input) {
os << "{ ";
for(auto elem : input) {
os << elem << " ";
}
return os << "}";
}
template<class T>
ostream& operator<<(ostream &os, const unordered_multiset<T> &input) {
os << "{ ";
for(auto elem : input) {
os << elem << " ";
}
return os << "}";
}
template<class L, class R>
ostream& operator<<(ostream &os, const map<L,R> &input) {
os << "{ ";
for(auto elem : input) {
os << "(" << elem.first << " , " << elem.second << ") ";
}
return os << "}";
}
template<class L, class R>
ostream& operator<<(ostream &os, const unordered_map<L,R> &input) {
os << "{ ";
for(auto elem : input) {
os << "(" << elem.first << " , " << elem.second << ") ";
}
return os << "}";
}
#define ll long long
const int MAXN = 500005;
const int INF = 1e9;
const int MOD = 998244353;
class TaskC {
private:
int n;
vector<int> a , b, who, types;
bool ok(int from, int upto) {
int sz = upto - from + 1;
assert(sz % 2 == 0);
int len = sz / 2;
for(int i = from, it = 0; it < len; i++, it++) {
if(who[i] == -1 && who[i+len] == -1) {
continue;
}
if(who[i+len] == -1) {
if(types[i] == 1) {
return false;
}
} else if(who[i] == -1) {
if(types[i+len] == 0) {
return false;
}
} else {
if(who[i] != who[i+len] || types[i] >= types[i+len]) {
return false;
}
}
}
return true;
}
vector<int> memo;
bool rec(int pos) {
if(pos == 2*n) {
return true;
}
if(memo[pos] != -1) {
return memo[pos];
}
bool ans = 0;
for(int upto = pos+1; upto < 2*n; upto += 2) {
if(ok(pos, upto)) {
ans |= rec(upto+1);
}
}
return memo[pos] = ans;
}
public:
void solveOne(int it){
cin >> n;
a.assign(n, -1);
b.assign(n, -1);
vector<bool> used(2*n, false);
who.assign(2*n, -1);
types.assign(2*n, -1);
for(int i = 0; i < n; i++) {
cin >> a[i] >> b[i];
if(a[i] != -1) {
a[i]--;
if(used[a[i]]) {
cout << "No\n";
return;
}
used[a[i]] = true;
who[a[i]] = i;
types[a[i]] = 0;
}
if(b[i] != -1) {
b[i]--;
if(used[b[i]]) {
cout << "No\n";
return;
}
used[b[i]] = true;
who[b[i]] = i;
types[b[i]] = 1;
}
if(a[i] != -1 && b[i] != -1 && a[i] >= b[i]) {
cout << "No\n";
return;
}
}
memo.assign(2*n+1, -1);
int ans = rec(0);
cout << (ans ? "Yes":"No") << endl;
}
void solve(){
int tc = 1;
for(int it = 1; it <= tc; it++){
solveOne(it);
}
}
};
int main(){
ios_base::sync_with_stdio(false);
TaskC solver;
solver.solve();
return 0;
}
|
//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, n) for (ll i = (s); i < (ll)(n); ++i)
#define all(x) (x).begin(), (x).end()
const int INF = 1e9;
const ll llINF = 1e18;
/*#include <atcoder/all>
using namespace atcoder;
using mint = modint1000000007;
*/
int main() {
int N;
cin >> N;
vector<ll> X(N), Y(N);
rep(i,0,N) cin >> X[i] >> Y[i];
int M;
cin >> M;
vector<pair<int,ll>> op(M+1);
rep(i,1,M+1) {
cin >> op[i].first;
if (op[i].first > 2) cin >> op[i].second;
}
int Q;
cin >> Q;
vector<pair<pair<int,int>,int>> q(Q);
rep(i,0,Q) {
int a, b;
cin >> a >> b;
q[i] = {{a, b-1}, i};
}
sort(all(q));
vector<pair<ll,ll>> ans(Q);
int cur = 0;
while (cur < Q && q[cur].first.first == 0) {
ans[q[cur].second] = {X[q[cur].first.second],Y[q[cur].first.second]};
++cur;
}
vector<ll> x = {1,0,0};
vector<ll> y = {0,1,0};
rep(i,1,M+1) {
vector<ll> tmp_x, tmp_y;
switch (op[i].first)
{
case 1:
tmp_x = y;
y = {-x[0], -x[1], -x[2]};
x = tmp_x;
break;
case 2:
tmp_y = x;
x = {-y[0], -y[1], -y[2]};
y = tmp_y;
break;
case 3:
x[0] *= -1;
x[1] *= -1;
x[2] *= -1; x[2] += 2*op[i].second;
break;
case 4:
y[0] *= -1;
y[1] *= -1;
y[2] *= -1; y[2] += 2*op[i].second;
break;
}
while (cur < Q && q[cur].first.first == i) {
ans[q[cur].second].first = x[0]*X[q[cur].first.second] + x[1]*Y[q[cur].first.second] + x[2];
ans[q[cur].second].second = y[0]*X[q[cur].first.second] + y[1]*Y[q[cur].first.second] + y[2];
++cur;
}
}
rep(i,0,Q) cout << ans[i].first << " " << ans[i].second << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define FI ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define pL pair<ll, ll>
const int MX=300000;
const ll mod=998244353;
int x[MX], y[MX];
vector<pair<ll, pair<int, int> > >dist;
vector<pL>dx, dy;
void cal(int i, int j)
{
ll d=dx[j].ff-dx[i].ff;
int jx=dx[j].ss, ix=dx[i].ss;
if(jx<ix) swap(jx, ix);
dist.push_back({d, {ix, jx}});
}
void cal2(int i, int j)
{
ll d=dy[j].ff-dy[i].ff;
int jx=dy[j].ss, ix=dy[i].ss;
if(jx<ix) swap(jx, ix);
dist.push_back({d, {ix, jx}});
}
ll ans()
{
int m=5;
while(m)
{
if(dist[m].ss==dist[m-1].ss) m--;
else return dist[m-1].ff;
}
}
int main()
{
FI;
int n; cin>>n;
for(int i=1; i<=n; i++) cin>>x[i]>>y[i];
for(int i=1; i<=n; i++) dx.push_back({x[i], i}), dy.push_back({y[i], i});
sort(dx.begin(), dx.end());
sort(dy.begin(), dy.end());
cal(0, n-1);
cal(0, n-2);
cal(1, n-1);
cal2(0, n-1);
cal2(0, n-2);
cal2(1, n-1);
sort(dist.begin(), dist.end());
cout<<ans()<<endl;
return 0;
}
|
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef long double ld;
const long long INF = 1LL << 60;
const int inf = (1 << 30);
const ll mod = 998244353;
const ll MOD = 1000000007;
const ld PI = acos(-1.0L);
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; }
int main(){
string s;
cin >> s;
deque<char>deq;
int n = s.size();
bool frip = false;
for(int i=0;i<n;i++){
char c = s[i];
if(s[i]=='R') frip = !frip;
else{
if(deq.empty()){
if(frip) deq.push_front(c);
else deq.push_back(c);
}
else{
if(frip){
if(deq.front()==c){
deq.pop_front();
}
else{
deq.push_front(c);
}
}
else{
if(deq.back()==c){
deq.pop_back();
}
else{
deq.push_back(c);
}
}
}
}
}
string ans;
for(char c:deq){
ans.push_back(c);
}
if(frip) reverse(ans.begin(),ans.end());
cout << ans << endl;
} | #include <stack>
#include <queue>
#include <set>
#include <vector>
#include <deque>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <utility>
#include <climits>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <cstring>
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
typedef pair<int,ll> pil;
typedef pair<ll,ll> pll;
typedef pair<ull,ull> pull;
typedef vector<int> vi;
typedef vector<ll> vl;
#define mp make_pair
#define pb push_back
#define fr first
#define sc second
#define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
#define rep(s, i, n) for (int(i) = (s); (i) < (n); (i)++)
#define repe(s, i, n) for (int(i) = (s); (i) <= (n); (i)++)
#define rrep(s, i, n) for (int(i) = (s); (i) > (n); (i)--)
#define rrepe(s, i, n) for (int(i) = (s); (i) >= (n); (i)--)
#define allof(a) (a).begin(), (a).end()
int main(int argc, char const *argv[])
{
string s;
cin>>s;
deque<char> T;
int r=0;
rep(0,i,s.length()){
if(s[i]=='R'){
r=1-r;
}
else{
if(r){
if(!T.empty()&&T.front()==s[i]){
T.pop_front();
}
else{
T.push_front(s[i]);
}
}
else{
if(!T.empty()&&T.back()==s[i]){
T.pop_back();
}
else{
T.push_back(s[i]);
}
}
}
}
if(r){
for(auto i=T.rbegin();i!=T.rend();i++){
cout<<*i;
}
cout<<endl;
}
else{
for(auto i=T.begin();i!=T.end();i++){
cout<<*i;
}
cout<<endl;
}
return 0;
}
|
#include<bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define ep emplace
#define epb emplace_back
#define scll static_cast<long long>
#define sz(x) static_cast<int>((x).size())
#define pfll(x) printf("%lld\n", x)
#define ci(x) cin >> x
#define ci2(x, y) cin >> x >> y
#define ci3(x, y, z) cin >> x >> y >> z
#define co(x) cout << x << endl
#define co2(x, y) cout << x << " " << y << endl
#define co3(x, y, z) cout << x << " " << y << " " << z << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef priority_queue<int> PQ;
typedef priority_queue<int, vector<int>, greater<int>> PQG;
typedef priority_queue<P> PQP;
typedef priority_queue<P, vector<P>, greater<P>> PQPG;
const int MAX_N = 2e5, MOD = 1e9 + 7, INF = 1e9;
int n;
string s, t;
int main() {
ci(n);
ci(s);
ci(t);
vector<int> sind, tind;
rep(i, n) {
if (s[n - i - 1] == '1') sind.epb(n - i - 1);
if (t[n - i - 1] == '1') tind.epb(n - i - 1);
}
if (sz(sind) < sz(tind) || sz(sind) % 2 != sz(tind) % 2) {
co(-1);
return 0;
}
for (int i = 0, e = sz(tind); i < e; i++) {
if (sind[i] < tind[i]) {
co(-1);
return 0;
}
}
ll ans = 0;
while (!sind.empty()) {
int i1, i2;
while (tind.empty() || sind.back() < tind.back()) {
i1 = sind.back(); sind.pop_back();
i2 = sind.back(); sind.pop_back();
ans += i2 - i1;
if (sind.empty()) break;
}
if (!tind.empty() && !sind.empty()) {
i1 = tind.back(); tind.pop_back();
i2 = sind.back(); sind.pop_back();
ans += i2 - i1;
}
}
co(ans);
return 0;
}
| #include <algorithm>
#include <cstdio>
#include <cstring>
#include <set>
typedef long long int64;
const int N = 500000 + 10;
int n;
char s[N], t[N];
int main() {
static std::set<int> set;
std::set<int>::iterator it;
int i, x;
int64 tot = 0;
scanf("%d", &n);
scanf("%s %s", s + 1, t + 1);
for (i = 1; i <= n; i++)
if (s[i] == '1')
set.insert(i);
for (i = 1; i <= n; i++) {
if (s[i] ^ t[i]) {
it = set.upper_bound(i);
if (it == set.end()) {
printf("%d\n", -1);
return 0;
}
x = (*it);
set.erase(it);
tot += x - i;
std::swap(s[i + 1], s[x]);
s[i + 1] = '0';
}
}
printf("%lld\n", tot);
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define swap(a,b) (a^=b^=a^=b)
#define ll long long int
#define ull unsigned long long int
#define uint unsigned int
#define format(a) memset(a,0,sizeof(a))
#define rep(x,y,z) for(int x=y;x<=z;x++)
#define dec(x,y,z) for(int x=y;x>=z;x--)
#define mst(x) memset(x,0,sizeof(x))
const int maxn=1e5+20;
inline int read()
{
int ans=0;
char last=' ',ch=getchar();
while(ch<'0'|ch>'9')last=ch,ch=getchar();
while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
if(last=='-')ans=-ans;
return ans;
}
int n,x,y;
int c[maxn],ok[maxn];
map<int,int>ma;
vector<int>node[maxn];
void dfs(int cur,int fa)
{
if(ma[c[cur]])ok[cur]=0;
else ok[cur]=1;
ma[c[cur]]++;
for(int i=0;i<node[cur].size();i++)
{
int nxt=node[cur][i];
if(nxt==fa)continue;
dfs(nxt,cur);
}
ma[c[cur]]--;
}
int main()
{
n=read();
rep(i,1,n)c[i]=read();
rep(i,1,n-1)
{
int x=read(),y=read();
node[x].push_back(y);
node[y].push_back(x);
}
dfs(1,0);
rep(i,1,n)
{
if(ok[i])cout<<i<<"\n";
}
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 main()
{
int r, c;
cin >> r >> c;
vector<vector<P>> g(2 * r * c);
//横方向
rep(i, r)rep(j, c - 1)
{
int a; cin >> a;
int idx = i * c + j;
g[idx].emplace_back(idx+1,a);
g[idx + 1].emplace_back(idx, a);
}
//縦方向
rep(i, r - 1)rep(j, c)
{
int b; cin >> b;
int idx = i * c + j;
g[idx].emplace_back(idx + c, b);
}
//高速道路の出入口
rep(i, r)rep(j, c)
{
int idx = i * c + j;
g[idx].emplace_back(idx + r * c, 1);
g[idx+r*c].emplace_back(idx, 0);
}
//高速道路
reps(i,1,r)rep(j, c)
{
int idx = i * c + j + r * c;
g[idx].emplace_back(idx - c, 1);
}
const int INF = 1e9;
priority_queue<P, vector<P>, greater<P>> pq;
pq.emplace(0, 0);
vector<int> dist(2 * r * c, INF);
dist[0] = 0;
while (!pq.empty())
{
int c, s;
tie(c, s) = pq.top();
pq.pop();
if (c > dist[s])continue;
for (auto [es,ec]: g[s])
{
if (dist[s] + ec >= dist[es])continue;
dist[es] = dist[s] + ec;
pq.emplace(dist[es],es);
}
}
cout << dist[r * c - 1] << endl;
return 0;
} |
// g++ -std=c++11 a.cpp
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<unordered_map>
#include<utility>
#include<cmath>
#include<random>
#include<cstring>
#include<queue>
#include<stack>
#include<bitset>
#include<cstdio>
#include<sstream>
#include<random>
#include<iomanip>
#include<assert.h>
#include<typeinfo>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define FOR(i,a) for(auto i:a)
#define pb push_back
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
#define show1d(v) {rep(_,v.size())cout<<" "<<v[_];cout<<endl;}
#define show2d(v) {rep(__,v.size())show1d(v[__]);}
using namespace std;
//kaewasuretyuui
typedef long long ll;
#define int ll
typedef int Def;
typedef pair<Def,Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def,pii> pip;
typedef vector<pip>vip;
#define mt make_tuple
typedef tuple<int,int,int> tp;
typedef vector<tp> vt;
typedef vector<vt>vvt;
template<typename A,typename B>bool cmin(A &a,const B &b){return a>b?(a=b,true):false;}
template<typename A,typename B>bool cmax(A &a,const B &b){return a<b?(a=b,true):false;}
const double PI=acos(-1);
const long double EPS=1e-9;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9+10;
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
#define yes cout<<"Yes\n"
#define no cout<<"No\n"
class DIJ{
public:
struct edge{
Def to,cost;
};
vector<vector<edge> >G;
Def n;
vi d;//distance
DIJ(Def size){
n=size;
G=vector<vector<edge> >(n);
}
void add_edge(Def a,Def b,Def c){
edge e={b,c},ee={a,c};
G[a].pb(e);
G[b].pb(ee);
}
vi dij(Def s){
d=vi(n,inf);
d[s]=0;
priority_queue<pii>q;
q.push(pii(0,s));
while(!q.empty()){
pii p=q.top();
q.pop();
Def pos=p.second,cost=-p.first;
if(cost>d[pos])continue;
rep(i,G[pos].size()){
edge e=G[pos][i];
Def to=e.to;
Def nowcost=cost+e.cost;
if(nowcost<d[to]){
d[to]=nowcost;
q.push(pii(-d[to],to));
}
}
}
return d;
}
};
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n,m;
cin>>n>>m;
DIJ dij(n);
rep(i,m){
int a,b;
cin>>a>>b;
a--;b--;
dij.add_edge(a,b,1);
}
int k;
cin>>k;
vi in(k);
rep(i,k)cin>>in[i],in[i]--;
vvi G(k);
rep(i,k)G[i]=dij.dij(in[i]);
vvi dp(1<<k,vi(k,inf));
rep(i,k)dp[1<<i][i]=1;
rep(i,1<<k)rep(j,k)if(dp[i][j]+inf)rep(l,k)if(!(i&1<<l)){
cmin(dp[i|1<<l][l],dp[i][j]+G[j][in[l]]);
}
int out=inf;
rep(i,k)cmin(out,dp.back()[i]);
if(out==inf)out=-1;
cout<<out<<endl;
}
| #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 998244353
#define N 400005
#define l(x) (x&(-x))
int n,a[N],b[N],c[N];
vector<int>to[N];
struct lsh{
int pos,val;
}a_[N<<1],b_[N];
inline void add(int x){while(x<N)++c[x],x+=l(x);}
inline int sum(int x){int ret=0;while(x)ret+=c[x],x-=l(x);return ret;}
inline bool cmp(lsh aa,lsh bb){
return aa.val<bb.val;
}
int i,cnt;
long long ans;
int main(){
cin>>n;
for(i=1;i<=n;++i)cin>>a[i],a[i]+=i,a_[i].val=a[i],a_[i].pos=i;
for(i=1;i<=n;++i)cin>>b[i],b[i]+=i,a_[i+n].val=b[i],a_[i+n].pos=i+n;
sort(a_+1,a_+n+n+1,cmp);
for(i=1;i<=n+n;++i){
if(a_[i].val!=a_[i-1].val)++cnt;
if(a_[i].pos>n)b[a_[i].pos-n]=cnt;
else a[a_[i].pos]=cnt;
}
for(i=1;i<=n;++i)++c[a[i]],--c[b[i]];
//,cout<<a[i]<<" "<<b[i]<<"\n";
for(i=1;i<=n+n;++i){
if(c[i])return cout<<"-1",0;
}
for(i=1;i<=n;++i)to[a[i]].push_back(i);
for(i=1;i<=n;++i){
++c[b[i]];
b[i]=to[b[i]][c[b[i]]-1];
}
memset(c,0,sizeof(c));
for(i=1;i<=n;++i){
ans+=sum(b[i]);
add(b[i]);
}
cout<<1ll*n*(n-1)/2-ans;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define mp(a, b) make_pair(a, b)
#define ls (rt << 1)
#define rs (rt << 1 | 1)
inline int lowbit(const int & x) { return x & (-x);}
int n;
int tot;
const int maxn = 200 + 10;
int P[maxn];
bool visa[maxn], visb[maxn];
int f[maxn][maxn];
void no() { puts("No"); exit(0);}
int dp(int i, int c)
{
if(~f[i][c]) return f[i][c];
if(i == 2 * n + 1) return 1;
for(int j = i + 1; j <= 2 * n; j += 2)
{
int cur = c;
bool flag = 1;
int l = (j - i + 1) / 2;
for(int p = i; (p <= i + l - 1) && flag; ++p)
{
if(P[p] != -1 && P[p] != p + l)
flag = 0;
if(P[p + l] != -1 && P[p + l] != p)
flag = 0;
if(visa[p + l] || visb[p])
flag = 0;
if(!visa[p] && !visb[p + l]) --cur;
if(cur < 0)
flag = 0;
}
//cout << i << ' ' << c << ' ' << flag << ' ' << j + 1 << ' ' << cur << ' ' << dp(j + 1, cur) << endl;
if(flag && dp(j + 1, cur))
return f[i][c] = 1;
}
return f[i][c] = 0;
}
int main()
{
memset(f, -1, sizeof f);
memset(P, -1, sizeof P);
scanf("%d", &n);
for(int i = 1; i <= n; ++i)
{
int a, b;
scanf("%d%d", &a, &b);
if(~a && (visa[a] || visb[a])) no();
if(~b && (visa[b] || visb[b])) no();
if(~a && ~b && a >= b) no();
if(a == -1 && b == -1) ++tot;
if(~a) P[a] = b, visa[a] = 1;
if(~b) P[b] = a, visb[b] = 1;
}
//cout << dp(5, tot) << endl;
if(dp(1, tot)) puts("Yes");
else puts("No");
return 0;
}
/*
2
-1 -1
2 3
*/ | #pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x, y) debug(x), debug(y)
#define repn(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(i, a) for(int i = 0; i < (int)(a); i++)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define sq(x) ((x) * (x))
const int mxN = 205;
template<class T> T gcd(T a, T b){ return ((b == 0) ? a : gcd(b, a % b)); }
int st[mxN], en[mxN];
int dp[mxN];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
//freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
int n;
cin >> n;
vector<pi> p(n);
int f2 = 1;
rep(i, n){
cin >> p[i].fi >> p[i].se;
if(p[i].fi != -1 && p[i].se != -1){
if(p[i].fi >= p[i].se){
f2 = 0;
}
}
if(p[i].fi != -1){
if(en[p[i].fi]) f2 = 0;
if(st[p[i].fi]) f2 = 0;
st[p[i].fi] = p[i].se;
}
if(p[i].se != -1){
if(en[p[i].se]) f2 = 0;
if(st[p[i].se]) f2 = 0;
en[p[i].se] = p[i].fi;
}
}
if(!f2){
cout << "No" << endl;
return 0;
}
dp[0] = 1;
rep(i, 2 * n + 1) if(dp[i]){
rep(k, n + 1){
//so k is b - a - 1
int nm = (k + 1) * 2;
if((i + nm) > (2 * n)) continue;
int f1 = 1;
repn(l, i + 1, i + k + 2){
if(st[l] && st[l] != -1){
if((st[l] - l - 1) != k){
f1 = 0;
break;
}
}
if(st[l] && st[l] == -1){
if(en[l + k + 1]) f1 = 0;
}
if(en[l]){
f1 = 0;
break;
}
}
repn(l, i + k + 2, i + nm + 1){
if(st[l]){
f1 = 0;
break;
}
if(en[l] && en[l] != -1){
if((l - en[l] - 1) != k){
f1 = 0;
break;
}
}
if(en[l] && en[l] == -1){
if(st[l - k - 1]) f1 = 0;
}
}
if(f1) dp[i + nm] = 1;
}
}
cout << (dp[2 * n] ? "Yes" : "No") << endl;
return 0;
}
/*
Things to look out for:
- Integer overflows
- Array bounds
- Special cases
Be careful!
*/
|
// {{{ by unolight
#include <bits/stdc++.h>
#include <unistd.h>
#pragma GCC diagnostic ignored "-Wunused-result"
#pragma GCC diagnostic ignored "-Wunused-function"
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x),end(x)
#define RALL(x) rbegin(x),rend(x)
#define REP(i,n) for ( int i=0; i<int(n); i++ )
#define REP1(i,a,b) for ( int i=(a); i<=int(b); i++ )
#define MP make_pair
#define PB push_back
using namespace std;
typedef int64_t LL;
typedef pair<int,int> PII;
typedef vector<int> VI;
namespace { namespace unolight {
// Read Input
template<class T> void _R( T &x ) { cin>>x; }
void _R( int &x ) { scanf("%d",&x); }
void _R( int64_t &x ) { scanf("%" PRId64,&x); }
void _R( double &x ) { scanf("%lf",&x); }
void _R( char &x ) { scanf(" %c",&x); }
void _R( char *x ) { scanf("%s",x); }
void R() {}
template<class T, class... U> void R( T& head, U&... tail ) { _R(head); R(tail...); }
// Write Output
template<class T> void _W( const T &x ) { cout<<x; }
void _W( const int &x ) { printf("%d",x); }
void _W( const int64_t &x ) { printf("%" PRId64,x); }
void _W( const double &x ) { printf("%.16f",x); }
void _W( const char &x ) { putchar(x); }
void _W( const char *x ) { printf("%s",x); }
template<class T> void _W( const vector<T> &x ) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W( const T& head, const U&... tail ) { _W(head); putchar(sizeof...(tail)?' ':'\n'); W(tail...); }
#ifdef UNOLIGHT
#include "dump.hpp"
#else
#define dump(...)
#endif
template<class T> inline bool chmax( T &a, const T &b ) { return b>a ? a=b,true : false; }
template<class T> inline bool chmin( T &a, const T &b ) { return b<a ? a=b,true : false; }
template<class T> using MaxHeap = priority_queue<T>;
template<class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;
template<class T, class F=less<T>> void sort_uniq( vector<T> &v, F f=F() ) {
sort(begin(v),end(v),f);
v.resize(unique(begin(v),end(v))-begin(v));
}
// }}}
void main() {
int a,b,c,d;
R(a,b,c,d);
int ans=-10000;
REP1(i,a,b) REP1(j,c,d) chmax(ans,i-j);
W(ans);
}
// {{{ main
}}
int main() { unolight::main(); return 0; }
// }}}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c, d;
cin >> a >> b;
cin >> c >> d;
cout << (b-c) << endl;
} |
/*
Problem: A.cpp
Time: 2021-05-08 11:22
Author: CraZYali
E-Mail: [email protected]
*/
#define REP(i, s, e) for (register int i(s), end_##i(e); i <= end_##i; i++)
#define DEP(i, s, e) for (register int i(s), end_##i(e); i >= end_##i; i--)
#define DEBUG fprintf(stderr, "Passing [%s] in Line %d\n", __FUNCTION__, __LINE__)
#define chkmax(a, b) (a < (b) ? a = (b) : a)
#define chkmin(a, b) (a > (b) ? a = (b) : a)
#include <iostream>
#include <cstdio>
using namespace std;
template <typename T>
inline T read()
{
T ans = 0, flag = 1;
char c = getchar();
while (!isdigit(c))
{
if (c == '-') flag = -1;
c = getchar();
}
while (isdigit(c))
{
ans = ans * 10 + c - 48;
c = getchar();
}
return ans * flag;
}
#define file(FILE_NAME) freopen(FILE_NAME".in", "r", stdin), freopen(FILE_NAME".out", "w", stdout)
int main()
{
#ifdef CraZYali
file("A");
#endif
int n = read<int>();
REP(i, 1, n) printf("%d %d\n", (2 * i) % n + 1, (2 * i + 1) % n + 1);
return 0;
}
| #include<fstream>
#include<iostream>
#include<sstream>
#include<vector>
#include<stack>
#include<string>
#include<algorithm>
#include<cmath>
#include<unordered_map>
#include<map>
#include<queue>
#include<tuple>
#include<iomanip>
#include<bitset>
#include<stdio.h>
#include<set>
#include<array>
using namespace std;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using graph = vector<vector<int>>;
const long long MOD = 1000000007;
const long long INF = 1e18;
using ll = long long;
using P = pair<int,int>;
using matrix = vector<vector<ll>>;
void chmin(int &x,int y){ x = min(x,y); }
int digit(ll x){
int count = 1;
while(x/10>0){
count ++;
x /= 10;
}
return count;
}
bool prime(ll x){
if(x==1) return false;
for(ll i=2;i*i<=x;i++){
if(x%i==0) return false;
}
return true;
}
void decom(ll x,map<ll,int> &hatano){
for(ll i=2;i*i<=x;i++){
if(x%i==0&&prime(i)){
while(x%i==0){
x /= i;
hatano[i] ++;
}
}
}
if(x!=1) hatano[x] ++;
}
ll speedpow(ll x,ll y){
ll res = 1;
ll test = x;
for(int i=0;i<31;i++){
if(y&(1<<i)) res *= test;
res = res%MOD;
test = (test*test)%MOD;
}
return res;
}
ll combination(ll x,ll y){
ll test = 1;
ll test1 = 1;
for(ll i=0;i<y;i++){
test = (test*(x-i))%MOD;
test1 = (test1*(i+1LL))%MOD;
}
return (test * speedpow(test1,MOD-2))%MOD ;
}
int euclid(int x,int y){
if(y==0) return x;
return euclid(y,x%y);
}
//行列累乗
matrix mul(matrix &a, matrix &b) {
int p = a.size();
matrix res(p, vector<long long>(p, 0));
for(int i = 0; i < p; i++)
for(int j = 0; j < p; j++)
for(int k = 0; k < p; k++)
(res[i][j] += a[i][k] * b[k][j]) %= MOD;
return res;
}
matrix pow(matrix m, long long k) {
int p = m.size();
matrix res(p, vector<long long>(p, 0));
for(int i = 0; i < p; i++)
res[i][i] = 1; // 単位行列にする
while(k > 0) {
if(k & 1) res = mul(res, m);
m = mul(m, m);
k >>= 1;
}
return res;
}
int main(){
int n,m,q;cin >> n >> m >> q;
int v[n],w[n];
vector<P> hatano;
int x[m];
vector<P> konishi;
int l[q],r[q];
rep(i,n){
cin >> w[i] >> v[i];
hatano.push_back(make_pair(v[i],w[i]));
}
sort(hatano.rbegin(),hatano.rend());
rep(i,m){
cin >> x[i];
konishi.push_back(make_pair(x[i],i));
}
sort(konishi.begin(),konishi.end());
vector<int> ans;
rep(i,q){
cin >> l[i] >> r[i];
l[i] --;r[i] --;
}
rep(i,q){
int test=0;
vector<bool> check(m,true);
for(int s=l[i];s<=r[i];s++) check[s] = false;
rep(j,n){
rep(k,m){
if(konishi[k].first>=hatano[j].second&&check[konishi[k].second]){
test += hatano[j].first;
check[konishi[k].second] = false;
// cout << hatano[j].first << endl;
break;
}
}
}
ans.push_back(test);
// cout << endl;
}
for(auto e:ans) cout << e << endl;
}
|
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define sz(x) ((int)x.size())
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define mset(x,y) memset(x,y,sizeof(x))
#define rep(i,n) for(int i=0;i<int(n);i++)
#define per(i,n) for(int i=int(n)-1;i>=0;i--)
#define rng(i,a,b) for(int i=int(a);i<=int(b);i++)
#define gnr(i,b,a) for(int i=int(b);i>=int(a);i--)
using namespace std;
using namespace __gnu_pbds;
using ordered_set = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;
#ifdef LOCAL
#define dmp(...) _dmp(#__VA_ARGS__, __VA_ARGS__)
#else
#define dmp(...) (__VA_ARGS__)
#endif
template<class T> using vt=vector<T>;
template<class T> using vvt=vt<vt<T>>;
template<class TA,class TB> void chmax(TA&a,TB b){if(a<b)a=b;}
template<class TA,class TB> void chmin(TA&a,TB b){if(b<a)a=b;}
template<class TA,class TB>
ostream& operator<<(ostream& os,const pair<TA,TB>& p){
return os<<"{"<<p.fi<<","<<p.se<<"}";
}
template<class T> ostream& operator<<(ostream& os,const vt<T>& v){
os<<"{";for(auto& e:v)os<<e<<",";return os<<"}";
}
template<class TH> void _dmp(const char *sdbg, TH h){cout<<sdbg<<"="<<h<<endl;}
template<class TH, class... TA> void _dmp(const char *sdbg, TH h, TA...a){
while(*sdbg!=',')cout<<*sdbg++;cout<<"="<<h<<","; _dmp(sdbg+1, a...);
}
template<class T> void make_unique(vt<T>& v) {
sort(all(v));v.erase(unique(all(v)),v.end());
}
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
int ri(){int x;cin>>x;return x;}
ll rll(){ll x;cin>>x;return x;}
vi rvi(int n){vi v;rep(i,n)v.pb(ri());return v;}
int n,a[100010];
void MAIN() {
int i;
cin>>n;
for(i=1;i<=n;i++)cin>>a[i];
sort(a+1,a+n+1);
ll sa=0,sb=0;
for(i=n;i>=1;i-=2)sa+=a[i];
for(i=n-1;i>=1;i-=2)sb+=a[i];
bool win=sa==sb;
if(n%2==0)win=!win;
cout<<(win?"First":"Second")<<'\n';
}
int main() {
ios::sync_with_stdio(false);cin.tie(0);
int T=1;
cin>>T;
for(int tt=1;tt<=T;tt++) {
MAIN();
}
return 0;
} | #include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mkp make_pair
#define rint register int
#define INF ((1 << 30) - 1)
#define FI(n) FastIO::read(n)
#define FO(n) FastIO::write(n)
#define Pair pair < int, int >
#define ull unsigned long long
#define mst(a,b) memset(a,b,sizeof(a))
#define foR(i, k, j) for(rint i = (k); i >= (j); i--)
#define For(i, k, j) for(rint i = (k); i <= (j); i++)
#define Foe(i, u) for(rint i = lst[u], v = e[i].v; i; i = e[i].nxt, v = e[i].v)
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define Fin(s) freopen(s, "r", stdin)
#define Fout(s) freopen(s, "w", stdout)
#define file(s) Fin(s".in"), Fout(s".out")
#define int long long
const int P = 1000000007; //
using namespace std;
inline void ckmax(int &a, int b) {a = max(a, b);}
inline void ckmin(int &a, int b) {a = min(a, b);}
inline void mulmod(int &a, int b) {a = 1ll * a * b % P;}
inline void addmod(int &a, int b) {int t = a + b; a = (t >= P ? t - P : t); }
inline int ksm(int a, int b) {int ans=1; for(;b;b>>=1) {if(b&1) ans=1ll*ans*a%P;a=1ll*a*a%P;}return ans;}
inline int inv(int a) {return ksm(a, P-2);}
inline void printarray(int *a, int n) {For(i, 1, n) fprintf(stderr, "%d ", a[i]); fprintf(stderr, "\n");}
namespace FastIO {
const int SIZE=1<<16; char buf[SIZE], obuf[SIZE], str[64]; int bi=SIZE, bn=SIZE, opt;
int read(char *s) {
while (bn) {for (;bi<bn&&buf[bi]<=' ';bi++);if (bi<bn) break; bn=fread(buf,1,SIZE,stdin),bi=0;}
int sn=0;while (bn) {for (;bi<bn&&buf[bi]>' ';bi++) s[sn++]=buf[bi];if (bi<bn) break; bn=fread(buf,1,SIZE,stdin),bi=0;}s[sn]=0;return sn;
}
bool read(int& x) {if(x)x=0;int bf=0,n=read(str); if(!n) return 0; int i=0; if (str[i]=='-') bf=1,i=1; for(x=0;i<n;i++) x=x*10+str[i]-'0'; if(bf) x=-x; return 1;}
void write(int x) {
if(!x) obuf[opt++] = '0'; else {if(x<0) obuf[opt++]='-',x=-x;int sn=0; while(x)str[sn++]=x%10+'0',x/=10;for (int i=sn-1;i>=0;i--) obuf[opt++]=str[i];}
if (opt>=(SIZE>>1)){fwrite(obuf, 1, opt, stdout); opt=0;}
}
void write(char x) {obuf[opt++]=x;if (opt>=(SIZE>>1)){fwrite(obuf, 1, opt, stdout); opt=0;}}
void Fflush() { if (opt) fwrite(obuf, 1, opt, stdout); opt=0;}
};
inline int read() {rint x; FI(x); return x;}
int n, a[100005];
signed main()
{
#ifndef ONLINE_JUDGE
file("pro");
#endif
int T = read();
while(T--) {
n = read();
For(i, 1, n) a[i] = read();
if(n & 1) cout << "Second" << endl;
else {
map < int, int > vis;
For(i, 1, n) vis[a[i]]++;
bool o = 0;
for(auto it : vis) {
if(it.second & 1) {
cout << "First" << endl, o = 1;
break;
}
}
if(!o) cout << "Second" << endl;
}
}
return FastIO::Fflush(), 0;
}
/*
*/
|
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <deque>
#include <stack>
#include <queue>
#include <array>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <random>
#include <chrono>
#include <utility>
#include <numeric>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <sstream>
#include <assert.h>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <unordered_map>
using namespace std;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(bool x) {cerr << (x ? "true" : "false");}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";}
template <typename T, typename... V>void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef HOME
#warning CHECK int:ll::INT_MAX:LLONG_MAX
#define maxn 20
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define maxn 2000006
#define gcd __gcd
#define debug(x...)
#endif
#define ff first
#define endl '\n'
#define ss second
#define inf 0x3f3f3f3f
#define MOD 1000000007
#define PI 3.14159265358979323846264338327950L
#define f(i,x,n) for(int i=x;i<=n;i++)
#define fr(i,x,n) for(int i=x;i>=n;i--)
struct _ { ios_base::Init i; _() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); } } _;
int dx[] = { -1, 0, 1, 0, -1, -1, 1, 1};
int dy[] = {0, 1, 0, -1, -1, 1, 1, -1};
int main() {
int a, b, c, d; cin >> a >> b >> c >> d;
cout << a*d - b*c;
return 0;
} | // ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include <stack>
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <cmath>
#include <queue>
#define MAXCHAR 255
#define ll long long
using namespace std;
const ll dividend = 1000000007;
const ll limit = pow(10, 18);
const ll bigNum = pow(10, 9);
ll funcComb(int n, int k)
{
ll output = 1;
int sum = n + k;
vector<int> dividends;
for (int i = 0; i < k; i++)
{
dividends.push_back(k - i);
}
for (int i = 0; i < k; i++)
{
output *= sum - i;
for (auto it = dividends.begin(); it != dividends.end(); it++)
{
if (*it == 0)
continue;
if (output % *it == 0)
{
output /= *it;
*it = 0;
}
}
if (output > dividend)
{
output %= dividend;
}
}
return output;
}
ll funcPow(int n, int r)
{
ll ret = 1;
for (int i = 1; i <= r; i++)
{
ret *= n;
if (ret > dividend)
ret %= dividend;
}
return ret;
}
vector<int> broken;
ll funcA(int step)
{
for (auto it = broken.begin(); it != broken.end(); it++)
{
if (step == *it)
return 0;
}
if (step == 1)
return 1;
if (step == 2)
return 2;
return funcA(step - 1) % dividend + funcA(step - 2) % dividend;
}
vector<int> C;
vector<vector<int>> tree;
int main()
{
int X, Y, Z, out;
cin >> X >> Y >> Z;
out = Z * Y / X;
if (out * X == Z * Y)
out--;
cout << out << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i=0;i<(n);++i)
using ll = long long;
using P = pair<int, int>;
using C = complex<double>;
int main() {
int n, k;
cin >> n >> k;
vector<vector<int>> a(n, vector<int>(n));
rep(i, n)rep(j, n) cin >> a.at(i).at(j);
int L = k * k / 2 + 1;
int wa = -1, ac = 1001001001;
while (wa + 1 < ac) {
int wj = (wa + ac) / 2;
bool ok = false;
{
vector<vector<int>> s(n + 1, vector<int>(n+1));
rep(i, n)rep(j, n) s.at(i + 1).at(j + 1) = a.at(i).at(j)>wj?1:0;
rep(i, n + 1)rep(j, n) s.at(i).at(j + 1) += s.at(i).at(j);
rep(i, n)rep(j, n + 1) s.at(i + 1).at(j) += s.at(i).at(j);
rep(i, n - k + 1)rep(j, n - k + 1) {
int now = s.at(i + k).at(j + k);
now -= s.at(i).at(j + k);
now -= s.at(i + k).at(j);
now += s.at(i).at(j);
if (now < L) ok = true;
}
}
if (ok) ac = wj; else wa = wj;
}
cout << ac << endl;
} | #include "bits/stdc++.h"
using namespace std;
#define ll long long
#define forn(i,n) for(int i=0;i<n;i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define sz(a) (int)a.size()
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define GR(a,n,m) vector<vector<int>> a(n, vector<int>(m, 0));
#define int long long
const int N = 1000;
int c[N][N];
int a[N][N];
int s[N][N];
int query(int i1,int j1,int i2,int j2){
int ans = s[i2][j2];
if(i1)ans -= s[i1-1][j2];
if(j1)ans -= s[i2][j1-1];
if(i1&&j1)ans += s[i1-1][j1-1];
return ans;
}
void solve()
{
int n, k;
cin >> n >> k;
for(int i = 0;i < n;i++){
for(int j = 0;j < n;j++){
cin >> c[i][j];
}
}
int l = 0,r = 1e9;
int ans = r;
int pos = (k * k - 1) / 2 + 1;
while(l <= r)
{
int mid = l + (r - l) / 2;
for(int i = 0;i < n;i++){
for(int j = 0;j < n;j++){
if(c[i][j] <= mid){
a[i][j] = 1;
}else{
a[i][j] = 0;
}
}
}
for(int i = 0;i < n;i++){
s[i][0] = a[i][0];
if(i)s[i][0] += s[i-1][0];
s[0][i] = a[0][i];
if(i)s[0][i]+=s[0][i-1];
}
for(int i = 1;i < n;i++){
for(int j = 1;j < n;j++){
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j];
}
}
bool ok=false;
for(int i = 0;i + k - 1 < n;i++){
for(int j = 0;j + k - 1 < n;j++){
int sum = query(i,j,i+k-1,j+k-1);
if(sum >= pos)ok=1;
}
}
if(ok){
ans=mid;
r=mid-1;
}else l=mid+1;
}
cout<<ans;
}
int32_t main()
{
fastio;
int t = 1;
//cin >> t;
while(t--)
{
solve();
}
} |
#include "bits/stdc++.h"
//#include "atcoder/all"
using namespace std;
//using namespace atcoder;
//using mint = modint1000000007;
//const int mod = 1000000007;
//using mint = modint998244353;
//const int mod = 998244353;
const int INF = 1e9;
//const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n-1); i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define endl "\n"
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
string s;
cin >> s;
vector<int>a(n + 1);
rep(i, n + 1) { cin >> a[i]; }
int k = INF;
rep(i, n) { k = min(k, abs(a[i] - a[i + 1])); }
cout << k << endl;
rep(i, k) {
rep(j, n + 1) {
cout << (a[j] + i) / k << " ";
}
cout << endl;
}
return 0;
} | #include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < (int)(n); i++)
#define ALL(a) a.begin(), a.end()
#define MOD 1000000007
using namespace std;
using ll = long long;
int main() {
int N; string S; cin >> N >> S;
vector<int> B(N + 1);
for (auto &b : B) cin >> b;
int k = 0;
REP(i, 1, 1e4 + 1) {
bool ok = true;
REP(j, 0, N) {
if (S[j] == '<') {
int d = B[j + 1] / i - B[j] / i;
if (d < 1 || (d == 1 && B[j] % i > B[j + 1] % i)) {
ok = false;
}
} else {
int d = B[j] / i - B[j + 1] / i;
if (d < 1 || (d == 1 && B[j] % i < B[j + 1] % i)) {
ok = false;
}
}
}
if (!ok) break;
k = i;
}
cout << k << endl;
REP(i, 0, k) {
REP(j, 0, N + 1) {
cout << B[j] / k + (i < B[j] % k) << " ";
}
cout << endl;
}
return 0;
}
|
//you can do anything // be strong :|
#include <bits/stdc++.h>
#define ll long long
#define FAST ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define pk push_back
#define f first
#define s second
using namespace std;
int const maxn = 1e6+9 ;
ll pric[maxn] ;
bool vis[maxn] ;
vector<vector<ll>>adj;
ll dp[maxn] ;
void dfs(int cur )
{
if(dp[cur]!=-1) return ;
dp[cur] = pric[cur] ;
vis[cur] =1 ;
for(int i=0 ;i<adj[cur].size() ;i++)
{
if(!vis[ adj[cur][i] ])
dfs(adj[cur][i]) ;
dp[cur] = max( dp[adj[cur][i]] , dp[cur] );
}
}
void solve()
{
ll n ,m ;
cin>>n>>m ;
for(int i=0 ;i<n ;i++) cin>>pric[i];
adj.resize(n);
ll x ,y ;
for(int i=0 ;i<m ;i++)
{
cin>>x>>y;
x-- ; y-- ;
adj[x].pk(y);
}
ll sum =INT_MIN ;
memset(vis ,false ,sizeof(vis));
memset(dp ,-1 ,sizeof(dp));
for(int i=0 ;i<n ;i++)
{
if(!vis[i])
dfs(i);
for(auto j : adj[i])
sum=max(sum , dp[j]- pric[i]);
}
cout<<sum;
}
int main()
{
ll t=1;
// scanf("%lld" ,&t);
while(t--) solve();
} |
#include<bits/stdc++.h>
using namespace std;
#define lli long long int
#define loop(i,a,b) for(lli i=a;i<b;i++)
#define loopb(i,a,b) for (lli i=a;i>=b;i--)
#define pb push_back
#define mod 1000000007
#define fast() ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define mem(a,b) memset(a,b,sizeof(a))
#define gcd(a,b) (__gcd((a),(b)))
#define lcm(a,b)(((a)*(b))/gcd((a),(b)))
#define ma 1e18
#define bc __builtin_popcountll
#define vi vector<lli>
#define pr pair<lli,lli>
#define vp vector<pr>
#define F first
#define S second
#define lld long double
#define PI 3.14159265358979
#define pri priority_queue<lli>
#define prim priority_queue<lli,vector<lli>,greater<lli>>
#define prip priority_queue<pr>
#define primp priority_queue<pr,vector<pr>,greater<pr>>
#define flush cout<<flush
vector<lli>v[200001];
lli a[200001];
lli dis[200001];
lli vis[200001];
void dfs(lli x){
vis[x] = 1;
for(auto itr : v[x]){
if(!vis[itr]){
dfs(itr);
}
dis[x] = max(dis[x] , max(dis[itr] , a[itr]));
}
}
int main(){
lli n,m,x,y;
cin>>n>>m;
loop(i,1,n+1){
cin>>a[i];
dis[i] = -1;
vis[i] = 0;
}
loop(i,0,m){
cin>>x>>y;
v[x].pb(y);
}
loop(i,1,n+1){
if(!vis[i]){
dfs(i);
}
}
lli ans = -1e16;
loop(i,1,n+1){
if(dis[i]!=-1){
ans = max(ans , dis[i] - a[i]);
}
}
cout<<ans<<"\n";
} |
#include<bits/stdc++.h>
using namespace std;
const int N=2000+5;
inline int read()
{
int x=0,f=1;
char c=getchar();
while(c<'0'||c>'9')
{
if(c=='-')
f=-1;
c=getchar();
}
while(c>='0'&&c<='9')
{
x=x*10+c-'0';
c=getchar();
}
return x*f;
}
int n;
int a[N];
inline int exgcd(int x,int y)
{
if(!y) return x;
return exgcd(y,x%y);
}
map<int,int> re;
map<int,int>::iterator it;
int main()
{
n=read();
int minn=0x3f3f3f3f;
for(int i=1;i<=n;i++) a[i]=read(), minn=min(minn,a[i]);
for(int i=1;i<=n;i++)
{
int t=sqrt(a[i]);
for(int j=1;j<=t;j++)
{
if(a[i]%j!=0) continue;
if(j<=minn) re[j]=exgcd(re[j],a[i]);
if(a[i]/j<=minn) re[a[i]/j]=exgcd(re[a[i]/j],a[i]);
}
}
int ans=0;
for(it=re.begin();it!=re.end();it++)
if((*it).first==(*it).second)
ans++;
printf("%d\n",ans);
return 0;
} | #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define int LL
typedef pair<int, int> PII;
typedef pair<int, LL> PIL;
const int N = 5e5 + 10, M = 3e5 + 10;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
#define x first
#define y second
const int mod = 1e9 + 7;
#define gcd __gcd
map<int, int> cnt;
int a[N];
int b[N];
void solve() {
int n;
scanf("%lld", &n);
cnt[0] = 1;
LL res = 0, sum = 0;
for(int i = 1; i <= n; i++) {
int x;
scanf("%lld", &x);
if(i & 1) x = -x;
sum += x;
if(cnt.count(sum)) res += cnt[sum];
cnt[sum]++;
}
printf("%lld\n", res);
}
signed main() {
// freopen("in.txt", "r", stdin);
// int t; cin >> t; while(t--)
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const string YES = "Yes";
const string NO = "No";
using ll = long long;
bool is_kaibun(string s){
for(int i = 0;i < s.size();i++){
if(s[i] != s[int(s.size()) - i - 1]){
return false;
}
}
return true;
}
void solve(long long _N){
string N = to_string(_N);
reverse(N.begin(), N.end());
for(int i = 0;i < 60;i++){
if(is_kaibun(N)){
cout<<"Yes"<<endl;
return;
}
N.push_back('0');
}
cout<<"No"<<endl;
}
int main(){
long long N;
scanf("%lld",&N);
solve(N);
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
typedef unsigned long long ull;
#define loop(i,a,b) for(ll i=a;i<b;i++)
#define f(i,a,b) for(ll i=a;i<=b;i++)
// #define testcases ll t;cin>>t;while(t--)
#define dec(x) greater<x>()
/*** Define fues ***/
#define mx 200005
#define mod 1000000007
#define PI acos(-1.0)
#define eps 1e-7
#define size1 100005
const char nl = '\n';
#define pb push_back
#define ff first
#define ss second
#define mp make_pair
#define mem(name, fue) memset(name, fue, sizeof(name))
/*** STLs ***/
typedef vector <ll> vll;
typedef set <ll> sll;
typedef multiset <ll> msll;
typedef queue <ll> qll;
typedef map <ll, ll> mll;
typedef pair <ll, ll> pll;
typedef vector <pair <ll , ll> > vpll;
/*** Sorts ***/
#define all(v) (v).begin(), (v).end()
#define rev(v) reverse(all(v))
#define srt(v) sort(all(v))
#define srtGreat(v) sort(all(v), greater<ll>())
inline bool cmp(pll a, pll b) { if (a.ff == b.ff)return a.ss < b.ss; return a.ff > b.ff; }
#define en cout << '\n';
#define no cout << "NO" << '\n'
#define yes cout << "YES" << '\n'
#define case cout << "Case " << t++ << ": "
/*** Functions
ll BigMod(ll base, ll pow, ll modfue){ if (pow == 0) return 1; ll ans = BigMod(base, pow / 2, modfue);ll total = ((ans % modfue) * (ans % modfue)) % modfue; if(pow % 2 == 0) return total; else{ return (total * (base % modfue) ) % modfue; } }
ll InverseMod(ll base, ll pow) { if(pow == 0) return 1; ll ans = InverseMod(base, pow / 2); ans = (ans * ans) % mod; if(pow & 1){ return (ans * base) % mod; } else{ return ans; } }
bool checkprime(ll num) { if(num < 2) return false; for(ll i = 2; i * i <= num; i++){ if(num % i == 0) return false; } return true; }
ll EularPHI(ll num) { double ans = num; for(ll i = 2; i * i <= num; i++){ if(num % i == 0){ while (num % i == 0) { num /= i; } ans *= (1.0 - (1.0 / (double)i)); } } if(num > 1) ans *= (1.0 - (1.0 / (double)num)); return (ll)ans; }
ll sumofdigit(ll n){ll sum=0;while(n){sum=sum+n%10;n=n/10;}return sum;}
ll countDigit(ll n) { if (n == 0) return 0; return 1 + countDigit(n / 10); }
ll countDigit(ll n) { return floor(log10(n) + 1); } //only positive
***/
template <class T> inline T gcd(T a, T b) {if (b == 0)return a; return gcd(b, a % b);}
template <class T> inline T lcm(T a, T b) {return a * b / gcd<T>(a, b);}
template <class T> inline T power(T b, T p) {ll ans = 1; while (p--) ans *= b; return ans;}
void solve()
{
string s;
cin>>s;
ll n=s.size(),c=0;
for(ll i=n-1;i>=0;i--)
{
if(s[i]=='0')c++;
else break;
}
ll a=0;
loop(i,0,n)
{
if(s[i]=='0')a++;
else break;
}
// cout<<a<<" "<<c<<nl;
for(ll i=a;i<(n-c)/2;i++)
{
if(s[i]!=s[n-i-1-c])
{
cout<<"No";
return;
}
}
cout<<"Yes";
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll t = 1;
// cin>>t;
while (t--)
{
solve();
}
}
|
#include <iostream>
using namespace std;
int main(void){
int n,s,d;
cin >> n >> s >> d;
for(int i=0;i<n;i++){
int x,y;
cin >> x >> y;
if(x<s){
if(d<y){
cout << "Yes" << endl;
break;
}
else{
if(i+1==n){
cout << "No" << endl;
}
else{
continue;
}
}
}
else{
if(i+1==n){
cout << "No" << endl;
}
else{
continue;
}
}
}
}
| #include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
#define FIO ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define repe(i,a,b) for(int i=a;i<=b;i++)
#define rrep(i,a,b) for(int i=b;i>=a;i--)
#define PI acos(-1.0)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define Unique(a) sort(all(a)),a.erase(unique(all(a)),a.end())
#define sz(s) (int)s.size()
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define ins(a) insert(a)
#define max2(a,b) max(a,b)
#define max3(a,b,c) max(a,max(b,c))
#define max4(a,b,c,d) max(a,max3(b,c,d))
#define min2(a,b) min(a,b)
#define min3(a,b,c) min(a,min(b,c))
#define min4(a,b,c,d) min(a,min3(b,c,d))
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
int main()
{
FIO;
#ifndef ONLINE_JUDGE
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
#endif
int n,s,d;
cin>>n>>s>>d;
string ans = "No";
for(int i=0;i<n;i++){
int x,y;
cin>>x>>y;
if(x>=s || y<=d) continue;
if(x<s && y > d ) ans = "Yes";
}
cout<<ans<<endl;
#ifdef ONLINE_JUDGE
cerr<<"Time elapsed: "<<1.0*clock()/CLOCKS_PER_SEC<<"s.\n";
#endif
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using pl = pair<ll, ll>;
const ll INF = ll(1e18);
const ll mod = ll(998244353);
const double pi = acos(-1);
#define rep0(i,n) for(ll (i) = 0; (i) < (n); ++(i))
#define rrep0(i,n) for(ll (i) = (n) - 1; (i) >= 0; --(i))
#define rep1(i,n) for(ll (i) = 1; (i) <= (n); ++(i))
#define rrep1(i,n) for(ll (i) = (n); (i) >= 1; --(i))
#define nfor(i,a,b) for(ll (i) = (a); (i) < (b); ++(i))
#define rnfor(i,a,b) for(ll (i) = (b) - 1; (i) >= (a); --(i))
#define pf(x) cout << (x) << endl
#define all(x) (x).begin(),(x).end()
#define yes pf("Yes")
#define no pf("No")
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const int dx[8]={1,0,-1,0,1,1,-1,-1};
const int dy[8]={0,1,0,-1,1,-1,1,-1};
int multipf(vector<string>& s){
cout << s[0];
rep1(i, s.size() - 1)cout << " " << s[i];
cout << endl;
return 0;
}
int multipf(vector<ll>& n){
cout << n[0];
rep1(i, n.size() - 1)cout << " " << n[i];
cout << endl;
return 0;
}
ll gcd(ll a,ll b){
if(a < b)swap(a, b);
if(b == 0) return a;
return gcd(b,a%b);
}
ll lcm(ll a,ll b){
ll g = gcd(a,b);
return a / g * b;
}
ll factorial(ll n){
ll ans = 1;
rep1(i, n){
ans *= i;
ans %= mod;
}
return ans;
}
ll power(ll a, ll b){
ll ans = 1;
while(b) {
if(b & 1LL) ans = ans * a % mod;
ans %= mod;
a = a * a;
a %= mod;
b >>= 1;
}
return ans % mod;
}
struct UnionFind {
vector<ll> par;
vector<ll> rank;
vector<ll> Size;
UnionFind(ll n) {
init(n);
}
void init(ll n) {
par.resize(n + 1); rank.resize(n + 1); Size.resize(n + 1);
rep0(i, n + 1){
par[i] = i;
rank[i] = 0;
Size[i] = 1;
}
}
ll root(ll x) {
if (par[x] == x) return x;
else return par[x] = root(par[x]);
}
bool same(ll x, ll y) {
return root(x) == root(y);
}
bool merge(ll x, ll y) {
x = root(x); y = root(y);
if (x == y) return false;
if (rank[x] < rank[y]) swap(x, y);
if (rank[x] == rank[y]) ++rank[x];
par[y] = x;
Size[x] += Size[y];
return true;
}
ll size(ll x){
return Size[root(x)];
}
};
//modの値の確認をすること
int main(){
ll n;
string s;
cin >> n >> s;
vector<tuple<ll,ll,ll,ll> > a(n + 1,make_tuple(0,0,0,0));
rep1(i, n){
if(s[i - 1] == 'A')get<0>(a[i])++;
else if(s[i - 1] == 'T')get<1>(a[i])++;
else if(s[i - 1] == 'C')get<2>(a[i])++;
else get<3>(a[i])++;
}
rep1(i, n){
get<0>(a[i]) += get<0>(a[i - 1]);
get<1>(a[i]) += get<1>(a[i - 1]);
get<2>(a[i]) += get<2>(a[i - 1]);
get<3>(a[i]) += get<3>(a[i - 1]);
}
ll ans = 0;
rep0(i, n)nfor(j, i + 1, n + 1)if(get<0>(a[j]) - get<0>(a[i]) == get<1>(a[j]) - get<1>(a[i]) && get<2>(a[j]) - get<2>(a[i]) == get<3>(a[j]) - get<3>(a[i]))ans++;
pf(ans);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
string s;
cin >> n >> s;
int count = 0;
for (int i = 0; i < n; i++) {
int c1 = 0, c2 = 0;
for (int j = i; j < n; j++) {
switch (s[j]) {
case 'A':
c1++;
break;
case 'T':
c1--;
break;
case 'C':
c2++;
break;
case 'G':
c2--;
break;
}
if (c1 == 0 && c2 == 0) {
count++;
}
}
}
cout << count;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using p = pair<int, int>;
int main() {
//vector<vector<int>>data(h,vector<int>(w));
int n;
cin >> n;
vector<int> a(n), b(n);
for (int i = 0; i < n; i++) {
cin >> a[i] >> b[i];
}
int am = 100000, bm = 100000, ai = 0, bi = 0;
for (int i = 0; i < n; i++) {
if (a[i] < am) {
am = a[i];
ai = i;
}
if (b[i] < bm) {
bm = b[i];
bi = i;
}
}
int counta = 0, countb = 0;
for (int i = 0; i < n; i++) {
if (a[i] == am) {
counta++;
}
if (b[i] == bm) {
countb++;
}
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
int c = min(a[1], b[1]);
if (ai != bi) {
cout << max(am, bm) << endl;
return 0;
} else if (ai == bi && (counta > 1 || countb > 1)) {
cout << max(am, bm) << endl;
return 0;
} else {
cout << min(c, am + bm) << endl;
}
return 0;
}
| #include <bits/stdc++.h>
#define mk make_pair
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
// please, read the question correctly (do you need set or multiset)???
const int N=200010; //check the limits, dummy
int a[N], b[N];
int n, m;
int main(){
scanf("%d",&n);
int ans = 2e9;
for(int i=0; i<n; ++i){
scanf("%d%d",a+i,&b[i]);
}
for(int i=0; i<n; ++i){
for(int j=0; j<n; ++j){
if(i==j){
ans = min(ans, a[i]+b[i]);
}
else{
ans = min(ans, max(a[i], b[j]));
}
}
}
cout<<ans<<endl;
} |
#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rrep(i, n) for(int i = (n-1); i >= 0; i--)
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
//const int MOD = 998244353;
const ll INF = 1LL<<60;
const int IINF = 1000000000;
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; }
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int A, B; cin >> A >> B;
int X = (A+B)/2;
int Y = A - X;
cout << X << " " << Y << endl;
}
| /*
Author: Racer5x
*************************************** UNAUTHORISED COPYING OF CODE PROHIBITED **********************************
*/
// #pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <vector>
#include <iostream>
#include <queue>
#include <map>
#include <algorithm>
#include <functional>
#define int long long
#define double long double
#define pb emplace_back
#define pf emplace_front
#define pii pair<int,int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int,int>
#define mii map<pii,int>
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define x first
#define y second
#define endl '\n'
#define hell 998244353
#define PI 3.141592653589
#define tezz ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define MAX 2000000000000000000
#define M 1000000007
using namespace std;
int dp[1 << 20];
signed main()
{
tezz
vector<int> primes;
for(int i=2; i<72; i++){
bool flag = true;
for(int j=2; j<i; j++){
if(i % j == 0) flag = false;
}
if(flag) primes.emplace_back(i);
}
int A, B;
cin>>A>>B;
//now convert every number into masks.. with respect to prime divisors
vector<int> v(B - A + 1);
for(int i=A; i<=B; i++){
int mask = 0;
for(int j=0; j<20; j++){
if(i % primes[j] == 0){
mask |= (1 << j);
}
}
v[i - A] = mask;
}
//now using dp to form every possible states
dp[0] = 1;
for(int x: v){
for(int mask=0; mask<(1 << 20); mask++){
if(dp[mask]){
int temp = mask & x;
if(temp == 0) dp[mask | x] += dp[mask];
}
}
}
int ans = 0;
for(int i=0; i<(1 << 20); i++) ans += dp[i];
cout<<ans;
} |
#include<bits/stdc++.h>
#define rg register
#define fp( i , x , y ) for( rg int i=(x); i<=(y); ++i )
#define fq( i , x , y ) for( rg int i=(y); i>=(x); --i )
#define dou double
using namespace std ;
void upd( int &x , int y ) {
if( x == -1 ) x = y ;
else x = min( x , y ) ;
}
signed main( ) {
ios::sync_with_stdio(false) ;
cin.tie(0) ;
int n , ans = -1 , a , b , c ;
cin >> n ;
fp( i , 1 , n ) {
cin >> a >> b >> c ;
if( a < c ) upd( ans , b ) ;
}
cout << ans << '\n' ;
return 0 ;
}
| #include<bits/stdc++.h>
using namespace std;
#define en '\n'
#define endl '\n'
#define ll long long signed
#define pb push_back
#define append push_back
#define all(v) (v).begin(),(v).end()
#define FOR(i,a,b,k) for(int i=a; k>0?i<b:i>b; i+=k)
#define fori(a) for(int i=0; i<a; i++)
#define forj(a) for(int j=0; j<a; j++)
#define fork(a) for(int k=0; k<a; k++)
#define MOD 1000000007
#define MAX 1000010
#define mset(a,s,k) memset(a,k,s*sizeof(a[0]))
#define ff first
#define ss second
#define __ <<" "<<
#define _ <<en
ll pow_(ll a,ll e,ll m=MOD){ll r=1;while(e){if(e&1)r=r*a%m;a=a*a%m;e>>=1;}return r;}
int inv(ll a) {return int(pow_(a,MOD-2,MOD));}int add(ll a, ll b){return (a+b+MOD)%MOD;} int mul(ll a, ll b)
{return (MOD+a*b%MOD)%MOD;}int di(ll a, ll b){return mul(a,inv(b));}ll ceil(ll a,ll b){return a/b+(a%b!=0);}
template<typename T1,typename T2>ostream&operator<<(ostream&O,pair<T1,T2>a){O<<"( ";O<<a.ff<<", "<<a.ss;O<<" )";return O;}
template<typename T>ostream&operator<<(ostream&O,vector<T>V){O<<"[ ";fori(V.size())O<<V[i]<<(i==V.size()-1?"":", ");O<<" ]";return O;}
template<typename T>ostream&operator<<(ostream&O,set<T>S){O<<"{ ";for(T i:S)O<<i<<", ";O<<" }";return O;}
template<typename T1,typename T2>ostream&operator<<(ostream&O,map<T1,T2>S){O<<"{ ";for(auto i:S)O<<i<<", ";O<<" }";return O;}
template<typename T=int>class print{public:vector<T>V;print(vector<T>v){V=v;cout<<" <"<<V<<"> ";}
print(T*x,int n){fori(n)V.push_back(x[i]);cout<<" <"<<V<<"> ";}friend ostream&operator<<(ostream&O,print p){return O;}};
#define vi vector<int>
#define tp pair<int,int>
#define ar array<int,4>
ar arr[MAX]; int N;
void reset();
int solve(){
cin>>N;
reset();
fori(N){
int a,b,c; cin>>a>>b>>c;
arr[i]={b,a,c};
}
sort(arr,arr+N);
bool f=1;
fori(N){
if(arr[i][1]<arr[i][2]){
cout<<arr[i][0];
f=false;
break;
}
}
if(f) cout<<-1;
cout<<endl;
// fori(N){ cin>>arr[i];}
// print<int>(arr,N);
return 0;
}
void reset(){
// Clear Vectors, maps, sets
// Set default values of arr
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t=1;
while(t--){
solve();
// cout<<solve()<<en;
// cout<<(solve()?"Yes":"No")<<endl;
}
return 0;
}
|
#include <iostream>
#include <vector>
#include <iomanip>
signed main(){
int N;
std::cin >> N;
std::vector<std::string> S(N);
for(auto &si:S) std::cin >> si;
std::vector<std::vector<int>> A(N,std::vector<int> (N));
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
A[i][j]=S[i][j]-'0';
}
A[i][i]=1;
}
for(int k=0;k<N;k++){
for(int i=0;i<N;i++){
for(int j=0;j<N;j++){
if(A[i][k]+A[k][j]==2) A[i][j]=1;
}
}
}
double answer=0;
for(int i=0;i<N;i++){
double go=0;
for(int j=0;j<N;j++){
if(A[j][i]==1) go++;
}
answer+=1/go;
}
std::cout << std::fixed << std::setprecision(10) << answer << '\n';
} | #include <bits/stdc++.h>
using namespace std;
typedef long double LD;
typedef long long ll;
#define int ll
#define ff(i,a,b) for (int i = a; i < b; i++)
#define bf(i,a,b) for (int i = a; i >= b; i--)
#define all(v) v.begin(),v.end()
#define show(a) for(auto xyz:a)cout<<xyz<<" ";cout<<endl;
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define one cout<<"-1"<<endl
#define pi 3.141592653589793238
#define err() cout<<"=================================="<<endl;
#define errA(A) for(auto i:A) cout<<i<<" ";cout<<endl;
#define err1(a) cout<<#a<<" "<<a<<endl
#define err2(a,b) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<endl
#define err3(a,b,c) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<endl
#define err4(a,b,c,d) cout<<#a<<" "<<a<<" "<<#b<<" "<<b<<" "<<#c<<" "<<c<<" "<<#d<<" "<<d<<endl
#define intmx INT_MAX
#define intmi INT_MIN
const int MOD=1e9+7;
int test=1;
void solve(){
int n,m;
cin>>n>>m;
int x,y;
cin>>x>>y;
x--;
y--;
string s[n];
ff(i,0,n)
cin>>s[i];
int cnt=0;
int i=x,j=y;
i++;
while(i<n&&s[i][j]=='.'){
cnt++;
i++;
}
i=x-1;
while(i>=0&&s[i][j]=='.'){
cnt++;
i--;
}
i=x;j=y+1;
while(j<m&&s[i][j]=='.'){
cnt++;
j++;
}
j=y-1;
while(j>=0&&s[i][j]=='.'){
cnt++;
j--;
}
if(s[x][y]=='.')
cnt++;
cout<<cnt<<endl;
}
signed main() {
int t=1;
//cin>>t;
while(t--){
solve();
}
}
|
#include <bits/stdc++.h>
#define watch(x) cout <<(#x)<<" is "<<(x)<<endl
#define debug cout <<"hi"<<endl
#define maxn 3e5+10
#define inf32 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int,int>pii;
const int MOD=1e9+7;
const int INF32=1<<30;
const ll INF64=1LL<<60;
ll lowbit(int x) {return x&(-x);}
vector<ll>sum(maxn,0);
void update(int i,int x){
while(i<sum.size()){
sum[i] += x;
i += lowbit(i);
}
}
//记得index要从1开始
ll query(int i){
ll ans = 0;
while(i>0){
ans += sum[i];
i -= lowbit(i);
}
return ans;
}
void solve(){
int h,w,m;cin >> h >> w >> m;
vector<ll>x(m),y(m),sc(w+1,h+1),rc(h+1,w+1);
for(int i = 0;i<m;i++){
cin >> x[i] >> y[i];
rc[x[i]] = min(rc[x[i]],y[i]);
sc[y[i]] = min(sc[y[i]],x[i]);
}
ll ans = 0;
vector<pll>a(m);
for(int i = 0;i<m;i++) a[i].first = x[i],a[i].second = y[i];
sort(a.begin(),a.end());
ll lim = w+1;
vector<bool>b(w+1,0);
int j = 0;
for(int i = 1;i<=h;i++){
ll tp = rc[i]-1;
if(tp==0) break;
ans += tp;
update(1,1);
update(rc[i],-1);
while(j<m&&a[j].first==i){
if(a[j].first==1) lim = min(lim,a[j].second);
if(a[j].second<lim){
if(b[a[j].second]==0){
b[a[j].second] = 1;
ans += a[j].first-1-query(a[j].second);//watch(a[j].first-1-query(a[j].first));
}
}
j++;
}
}
for(int i = 1;i<lim;i++){
if(!b[i]){
ans += sc[i]-1-query(i);
}
}
cout << ans;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
solve();
return 0;
}
| #include <bits/stdc++.h>
/* #include <atcoder/all> */
#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)
using namespace std;
/* using namespace atcoder; */
using ll = long long;
using P = pair<int, int>;
const ll INF = 1 << 30;
template<class T>
inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
struct Data {
int to;
ll cost;
};
int main() {
int n, m;
cin >> n >> m;
vector<ll> dist(n, INF);
// vector<vector<ll>> G2(n, vector<ll>(n, INF));
vector<vector<Data>> G(n, vector<Data>());
rep(i, m) {
int a, b;
ll c;
cin >> a >> b >> c;
a--;
b--;
if (a == b) {
chmin(dist.at(a), c);
} else {
// chmin(G2.at(a).at(b), c);
Data d = {b, c};
G.at(a).push_back(d);
}
}
// vector<vector<Data>> G(n, vector<Data>());
// rep(i, n) {
// rep(j, n) {
// if (i == j) continue;
// Data d = {j, G2.at(i).at(j)};
// G.at(i).push_back(d);
// }
// }
vector<vector<ll>> dist2(n, vector<ll>(n, INF));
rep(i, n) {
vector<ll> dist3(n, INF);
dist3.at(i) = 0;
priority_queue<pair<ll, int>, vector<pair<ll, int>>,
greater<pair<ll, int>>> que;
que.push(make_pair(dist3.at(i), i));
while (!que.empty()) {
int v = que.top().second;
ll d = que.top().first;
que.pop();
if (d > dist3.at(v)) continue;
for (auto e : G.at(v)) {
if (e.to == i) {
continue;
}
if (chmin(dist3.at(e.to), dist3.at(v) + e.cost)) {
que.push(make_pair(dist3.at(e.to), e.to));
}
}
}
rep(j, n) {
// cerr << dist3.at(j) << " ";
dist2.at(i).at(j) = dist3.at(j);
}
// cerr << endl;
}
rep(i, n) {
rep(j, n) {
if (i == j) continue;
dist.at(i) = min(dist.at(i), dist2.at(i).at(j) + dist2.at(j).at(i));
}
}
rep(i, n) {
cout << ((dist.at(i) == INF) ? -1 : dist.at(i)) << endl;
}
return 0;
}
|
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long LL;
int main() {
string S; cin >> S;
int l = 0, r = S.size() - 1;
bool is_zero = true;
while (l < r) {
if (S[l] == S[r]) {
++l, --r;
if (S[l] != '0') is_zero = false;
} else {
if (is_zero && S[r] == '0') {
--r;
} else {
cout << "No" << endl;
return 0;
}
}
}
cout << "Yes" << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define FOR(a, c) for (int(a) = 0; (a) < (c); (a)++)
#define FORL(a, b, c) for (int(a) = (b); (a) <= (c); (a)++)
#define FORR(a, b, c) for (int(a) = (b); (a) >= (c); (a)--)
#define INF 1000000000000000003
#define MOD 1000000007
#define F first
#define S second
#define PB push_back
#define POB pop_back
#define MP make_pair
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi>vp;
typedef vector<ll> vl;
int intToBinary (int n) {
string s = bitset< 32 >( n ).to_string();
return stoi(s);
}
vector<bool> primeSeive(ll n){
vector<bool> v(n,true);
v[0]=false;
v[1]=false;
for(ll i=2;i<=sqrt(n);i++){
for(ll j=i*2;j<=n;j+=i){
v[j]=false;
}
}
return v;
}
vector<ll> cumSum(vector<ll> arr, int n){
vector<ll> sumArr;
sumArr.PB(0);
ll sum = 0;
FOR(i,n){
sum+=arr[i];
sumArr.PB(sum);
}
return sumArr;
}
bool checkPrime(ll num){
for(int i = 2;i<=sqrt(num);i++){
if(num%i == 0) return false;
}
return true;
}
ll totalSum(vl v){
ll total = 0;
FOR(i,v.size()) total += v[i];
return total;
}
vector<ll> countDivisors(ll n) {
vector<ll> v(n+1, 1);
v[0]= 0;
for(ll i= 2; i <= n; i++) {
for(ll j= i; j <=n; j+=i) {
v[j]++;
}
}
return v;
}
int main() {
// your code goes here
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string s;
cin >> s;
if(s[0] == s[1] && s[1] == s[2] ) cout << "Won";
else cout << "Lost";
return 0;
}
|
/**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author tatsumack
*/
#include <iostream>
#include <fstream>
#include <bits/stdc++.h>
#define int long long
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i)
#define REV(i, a, b) for (int i = (a); i >= (b); --i)
#define CLR(a, b) memset((a), (b), sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF 1001001001001001001ll
#define fcout cout << fixed << setprecision(12)
using namespace std;
// 約数列挙
vector<int> get(int n) {
vector<int> ret;
for (int i = 1; i * i <= n; ++i) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n) {
ret.push_back(n / i);
}
}
}
return ret;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
class FGCDOrMIN {
public:
void solve(std::istream& cin, std::ostream& cout) {
int N;
cin >> N;
vector<int> A(N);
REP(i, N) cin >> A[i];
int mina = INF;
map<int, int> m;
REP(i, N) {
for (auto n : get(A[i])) {
if (m.count(n) == 0) {
m[n] = A[i];
} else {
m[n] = gcd(m[n], A[i]);
}
}
mina = min(mina, A[i]);
}
int res = 0;
for (auto kv : m) {
if (kv.second <= mina && kv.first == kv.second) {
res++;
}
}
cout << res << endl;
}
};
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
std::istream& in(std::cin);
std::ostream& out(std::cout);
FGCDOrMIN solver;
solver.solve(in, out);
return 0;
}
| #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <math.h>
#include <numeric>
#include <complex>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <unordered_map>
#include <set>
#include <bitset>
#include <functional>
#include <assert.h>
using ll=long long;
#define int ll
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define eb emplace_back
#define all(x) x.begin(),x.end()
#define SZ(x) ((int)x.size())
#define fi first
#define se second
const int INF = 1e15;
using namespace std;
template<class T> using vc=vector<T>;
template<class T> using vvc=vc<vc<T>>;
using pi=pair<int,int>;
using vi=vc<int>;
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;}
signed main() {
cin.tie(0);
ios::sync_with_stdio(0);
cout<<fixed<<setprecision(20);
int T; cin >> T;
rep(i,T) {
int N; cin >> N;
int A[N];
rep(i,N) cin >> A[i];
if(N%2) {
cout << "Second" << endl;
continue;
}
else {
sort(A,A+N);
bool can=true;
rep(i,N/2) {
if(A[i*2]!=A[i*2+1]) can=false;
}
if(can) {
cout << "Second" << endl;
} else {
cout << "First" << endl;
}
}
}
} |
#include<algorithm>
#include<cstdio>
typedef long long ll;
const int maxn=1e5+5;
const int mod=1e9+7;
int n;
int val[maxn];
ll sum[maxn][2];
ll cn[maxn][2];
signed main(){
scanf("%d",&n);
if(n==1){
int x;
scanf("%d",&x);
printf("%d",x);
return 0;
}
for(int i=1;i<=n;i++)scanf("%d",val+i);
sum[2][0]=1ll*(val[1]+val[2])%mod;
sum[2][1]=1ll*(val[1]-val[2]+mod)%mod;
cn[2][0]=cn[2][1]=1;
for(int i=3;i<=n;i++){
cn[i][0]=(cn[i-1][0]+cn[i-1][1])%mod;
cn[i][1]=(cn[i-1][0])%mod;
sum[i][0]=(sum[i-1][0]+sum[i-1][1]+1ll*cn[i][0]*val[i])%mod;
sum[i][1]=((sum[i-1][0]-1ll*cn[i][1]*val[i]+mod)%mod+mod)%mod;
// printf("cn[%d][0]=%lld,[1]=%lld\n",i,cn[i][0],cn[i][1]);
}
// for(int i=2;i<=n;i++)printf("sum[%d][0]=%lld,sum[%d][1]=%lld\n",i,sum[i][0],i,sum[i][1]);
printf("%lld",(sum[n][0]+sum[n][1])%mod);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr int INF = (int)1e9;
constexpr long long LINF = (long long)1e18;
constexpr long long MOD = 1e9 + 7;
//constexpr long long MOD = 998244353;
constexpr double pi = 3.141592653589793238;
int main() {
int n;
cin >> n;
vector<ll> a(n);
vector<vector<ll>> dp(n, vector<ll>(3));
for (int i = 0; i < n; ++i) {
if (i == 0) {
dp.at(0).at(0) = 1;
dp.at(0).at(1) = 0;
dp.at(0).at(2) = 1;
}
else {
dp.at(i).at(0) = (dp.at(i - 1).at(0) + dp.at(i - 1).at(1)) % MOD;
dp.at(i).at(1) = dp.at(i - 1).at(0);
dp.at(i).at(2) = (dp.at(i).at(0) + dp.at(i).at(1)) % MOD;
}
ll ai;
cin >> ai;
a.at(i) = ai;
}
ll ans = 0;
ans += (dp.at(n - 1).at(2) * a.at(0)) % MOD;
if (n <= 2) {
cout << ans << endl;
return 0;
}
ans = (ans + dp.at(n - 2).at(2) * a.at(1)) % MOD;
ans = ans - dp.at(n - 3).at(2) * a.at(1);
if (ans < 0) ans += MOD;
ans += dp.at(n - 2).at(2) * a.at(n - 1) % MOD;
ans %= MOD;
ans -= dp.at(n - 3).at(2) * a.at(n - 1) % MOD;
if (ans < 0) ans += MOD;
for (int i = 2; i < n - 1; ++i) {
ans += dp.at(i - 1).at(2) * dp.at(n - 1 - i).at(2) % MOD * a.at(i) % MOD;
ans %= MOD;
ans -= dp.at(i - 2).at(2) * dp.at(n - 2 - i).at(2) % MOD * a.at(i) % MOD;
if (ans < 0) ans += MOD;
}
cout << ans << endl;
return 0;
} |
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define trav(a,x) for (auto& a : x)
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1000000007;
const char nl = '\n';
const int MX = 100001; //check the limits, dummy
ll read() {
ll K = 0;
string S; cin >> S;
int cnt = 0;
bool found = false;
bool neg = false;
trav(a, S) {
if (a == '.') {
found = true; continue;
} else if (a == '-') {
neg = true; continue;
}
if (found) cnt++;
K *= 10; K += a - '0';
}
FOR(i, cnt, 4) K *= 10;
if (neg) K *= -1;
return K;
}
void solve() {
ll X = read(), Y = read(), R = read();
R *= R;
ll ans = 0;
for (ll x = -2000000000; x <= 2000000000; x += 10000) {
ll lo = Y, hi = 2000000000;
assert(lo < hi);
ll G = (X-x)*(X-x);
if (G > R) continue;
while (lo < hi) {
ll mid = lo + (hi-lo+1)/2;
if ((Y-mid)*(Y-mid) <= R-G) {
lo = mid;
} else {
hi = mid-1;
}
}
ll upB = lo;
lo = -2000000000;
hi = Y;
assert(lo < hi);
while (lo < hi) {
ll mid = lo + (hi-lo)/2;
if ((Y-mid)*(Y-mid) <= R-G) {
hi = mid;
} else {
lo = mid+1;
}
}
ll loB = lo;
loB += 1e12; upB += 1e12;
assert(loB >= 0); assert(upB >= 0);
loB = (loB + 9999) / 10000;
upB = upB / 10000;
assert(upB - loB + 1 >=0);
ans += upB - loB + 1;
}
cout << ans << nl;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int T = 1;
// cin >> T;
while(T--) {
solve();
}
return 0;
}
// read the question correctly (ll vs int)
// template by bqi343
| #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, k; cin >> n >> k;
auto count_pairs = [&](int x) {
if (x <= 1 || x > 2 * n) return 0LL;
return (long long) (x - 1) / 2 * 2 + (x - 1) % 2 - max(0, (x - n - 1) * 2);
};
long long ans = 0;
for (int x = 2; x <= 2 * n; ++x)
ans += (long long) count_pairs(x) * count_pairs(x - k);
cout << ans << '\n';
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()
int n, m;
vector<pair<int, int>> edge[100000];
bool vis[100000];
int ans[1000000];
void dfs(int u, int pe, bool pf) {
vis[u] = true;
if (pf) {
ans[u] = (pe + 1) % n;
}
else {
ans[u] = pe;
}
for (auto e : edge[u]) {
int v = e.first;
if (vis[v])continue;
dfs(v, e.second, e.second == ans[u]);
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
rep(_, m) {
int u, v, c;
cin >> u >> v >> c;
u--; v--; c--;
edge[u].emplace_back(v, c);
edge[v].emplace_back(u, c);
}
dfs(0, 0, false);
rep(i, n) {
cout << ans[i] + 1 << endl;
}
return 0;
}
| #include <bits/stdc++.h>
#define repd(i, a, b) for (ll i = (a); i < (b); i++)
#define repb(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep(i, n) repd(i, 0, n)
using namespace std;
using ll = long long;
using ul = unsigned long long;
using ld = long double;
const ul mod = 1000000007;
ll inf = 1LL << 60;
struct edge {
ll to, cost, time;
};
void djikstra(vector<ll> &d, vector<ll> &f, vector<vector<edge>> &g, ll start) {
// p : (cost, position)
using p = pair<ll, ll>;
ll inf = 1LL << 60;
fill(d.begin(), d.end(), inf);
d[start] = 0;
priority_queue<p, vector<p>, greater<p>> q;
q.push(p(0, start));
for (int i = 0; i < f.size(); i++) {
f[i] = -1;
}
while (!q.empty()) {
p now = q.top();
q.pop();
ll now_cost = now.first, now_position = now.second;
if (d[now_position] < now_cost)
continue;
for (edge e : g[now_position]) {
ll t = e.time;
ll d_temp = ((d[now_position] + t - 1) / t) * t;
if (d[e.to] > d_temp + e.cost) {
d[e.to] = d_temp + e.cost;
f[e.to] = now_position;
q.push(p(d[e.to], e.to));
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n, m, x, y;
cin >> n >> m >> x >> y;
x--;
y--;
vector<ll> a(m), b(m), t(m), k(m);
vector<vector<edge>> g(n);
rep(i, m) {
cin >> a[i] >> b[i] >> t[i] >> k[i];
a[i]--;
b[i]--;
g[a[i]].push_back({b[i], t[i], k[i]});
g[b[i]].push_back({a[i], t[i], k[i]});
}
vector<ll> d(n, inf), f(n);
d[x] = 0;
ll start = x;
djikstra(d, f, g, start);
if (d[y] == inf) {
d[y] = -1;
}
cout << d[y] << endl;
return 0;
}
|
//---------------------------------------------------------------
#include <bits/stdc++.h>
using namespace std;
#include <cstdlib>
#include <cmath>
#include <math.h>
#include <sstream>
#include <numeric>
#include <cassert>
//---------------------------------------------------------------
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define rrep(i, n) for (long long i = 1; i <= (long long)(n); i++)
#define srep(i, s, n) for (long long i = (s); i <= (long long)(n); i++)
#define drep(i,n) for(long long i = (n)-1; i >= 0; --i)
#define SIZE 100005
#define INF 1e18
#define all(x) x.begin(),x.end()
#define fi first
#define se second
#define vec vector
using pint = pair<int,int>;
using ll = long long;
using vll = vector<long long>;
using vvll = vector<vector<long long>>;
using pll = pair<long long, long long>;
//---------------------------------------------------------------
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
//---------------------------------------------------------------
struct Edge {
ll to;
ll w;
Edge(ll to, ll w) : to(to), w(w) {}
};
using Graph = vector<vector<Edge>>;
//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
int main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
ll n,m;
cin>>n>>m;
map<ll , vll> mp;
rep(i,m){
ll x,y;
cin>>x>>y;
mp[x].push_back(y);
}
//sort(all(mp));はしなくてもよい。昇順で入っている。多分。
set<ll> ans;
set<ll> s; //任意のxについて黒ポーンのあるy座標を格納する。
s.insert(n);
for(auto p : mp){
vector<ll> add;//次にいる場所
for(auto y : p.second){
//一つ前の段階でどの座標にいるか調べる。
if(s.count(y+1)) {
add.push_back(y);
}else if(s.count(y-1)){
add.push_back(y);
}
//白の場合はそのまま降ってくるので処理は不要。
}
for(auto i : p.second) s.erase(i);
for(auto i : add) s.insert(i);
}
cout<<s.size()<<endl;
return 0;
} | #include<bits/stdc++.h>
#define int long long
#define maxn 200010
// #define mod
using namespace std;
// template<class T>T ABS(T x) {return x<0?-x:x;}
//int ksm(int x,int y) {int r=1;while(y) y&1?r=1ll*r*x%mod:0,x=1ll*x*x%mod,y>>=1;return r;}
int read()
{
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
return s*w;
}
// struct Edge{int to,next;}a[maxn];
// int h[maxn],cnt;
// void add(int x,int y) {a[++cnt]=(Edge){y,h[x]},h[x]=cnt,a[++cnt]=(Edge){x,h[y]},h[y]=cnt;}
char S[maxn],X[maxn];
int T[10],B[10],pw[maxn],n;
signed main()
{
n=read();
scanf("%s%s",S+1,X+1);
B[0]=1;
pw[0]=1;for(int i=1;i<n;i++) pw[i]=pw[i-1]*10%7;
for(int i=n;i;i--)
if(X[i]=='A')
{
for(int j=0;j<7;j++) T[j]=B[j];
for(int j=0;j<7;j++) B[j]=T[j]&T[((j+(S[i]-'0')*pw[n-i])%7+7)%7];
}
else
{
for(int j=0;j<7;j++) T[j]=B[j];
for(int j=0;j<7;j++) B[j]=T[j]|T[((j+(S[i]-'0')*pw[n-i])%7+7)%7];
}
cout<<(B[0]?"Takahashi":"Aoki")<<'\n';
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
string s;
cin>>n>>s;
n++;
vector<int> a(n);
for(int i=0;i<n;i++)cin>>a[i];
int l=0,r=10001,mid;
bool h;
while(l<r){
mid=(l+r)/2;
vector<vector<int>> A(n,vector<int>(mid));
h=true;
for(int i=0;i<n;i++){
for(int j=0;j<mid;j++){
A[i][j]=a[i]/mid+(j<a[i]%mid);
}
}
for(int j=0;j<mid;j++){
for(int i=0;i<n-1;i++){
if(s[i]=='>'&&A[i][j]<=A[i+1][j])h=false;
if(s[i]=='<'&&A[i][j]>=A[i+1][j])h=false;
if(!h)break;
}
if(!h)break;
}
if(h)l=mid+1;
else r=mid;
}
l--;
cout<<l<<endl;
for(int j=0;j<l;j++){
for(int i=0;i<n;i++){
if(i)cout<<" ";
cout<<a[i]/l+(j<a[i]%l);
}
cout<<endl;
}
} | #include<bits/stdc++.h>
using namespace std;
ostream& operator<<(ostream&o,const pair<auto,auto>&p){return o<<"<"<<p.first<<", "<<p.second<<">";}
ostream& operator<<(ostream&o,const vector<auto>&v){o<<'[';int f=0;for(auto&x:v)(f?o<<", ":o)<<x,f=1;return o<<']';}
#ifdef BIZON
#define rr(...) [](const auto&...x){ char c='='; cerr << boolalpha << "\e[1;38;5;68m" << #__VA_ARGS__ << " "; ((cerr<<"\e[0;38;5;61m"<<exchange(c,',')<<"\e[0m "<<x),...); cerr<<endl; }(__VA_ARGS__);
#else
#define rr(...) 0;
#define endl '\n'
#endif
#define ALL(c) begin(c), end(c)
#define II(...) __VA_ARGS__; [](auto&...x){(cin>>...>>x);}(__VA_ARGS__);
#define ii(...) int II(__VA_ARGS__)
inline bool umin(auto&x, const auto&y){ return y<x && (x=y, 1); }
inline bool umax(auto&x, const auto&y){ return y>x && (x=y, 1); }
using ll = long long;
int main(){
if(auto f="in.txt"; fopen(f,"r") && freopen(f,"r",stdin));
ios::sync_with_stdio(0);cin.tie(0);
ii(n)
vector<int> a(n);
for(auto &_ : a) cin>>_;
vector<ll> s(n+1);
vector<map<ll,int>> m(2);
m[n%2][0] = 1;
ll ans = 0;
for(int i=n-1;i>=0;--i){
s[i] = a[i] - s[i+1];
ans+= m[i%2][s[i]];
ans+= m[1-i%2][-s[i]];
++m[i%2][s[i]];
}
cout<<ans<<endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
vector<int> parent;
int root(int i){
if(parent[i]==i) return i;
parent[i] = root(parent[i]);
return parent[i];
}
int main(){
int n,q;
cin >> n >> q;
vector<int> c(n);
for(auto& i : c) cin >> i;
vector<map<int,int>> cla(n);
parent.resize(n);
//vector<int> len(n,1);
for(int i=0; i<n; ++i) {
cla[i][c[i]]++;
parent[i] = i;
}
for(int i=0; i<q; ++i) {
int t,a,b;
cin >> t >> a >> b;
//cout << t << ' ' << a << ' ' << b << endl;
if(t==1){
int ra = root(a-1), rb = root(b-1);
if(ra == rb) continue;
if(cla[ra].size()>cla[rb].size()){
for(auto j : cla[rb])
cla[ra][j.first] += j.second;
//cla[rb].clear();
parent[rb] = ra;
//len[a-1] += len[b-1];
//len[b-1] = 0;
}else{
for(auto j : cla[ra])
cla[rb][j.first] += j.second;
//cla[ra].clear();
parent[ra] = rb;
//len[b-1] += len[a-1];
//len[a-1] = 0;
}
}
else{
cout << cla[root(a-1)][b] << endl;
}
}
}
| #pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define ins insert
#define ull unsigned long long
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define ff first
#define ss second
#define fast ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
#define nl "\n"
#define sz(a) (int)(a).size()
#define all(a) (a).begin(),(a).end()
#define fl(a,x) memset(a,x,sizeof(a));
#define pt(a,x) cout << get<x>(a) << " " ;
#define mt make_tuple
#define vec vector
#define ld long double
#define eps 1e-9
#define int long long
const int mod = (int) 1e9 + 7 ;
const int MOD = (int) 998244353 ;
const int INF = (int) 1e18 ;
typedef vector<int> vii;
typedef pair<int,int> pii;
typedef vector<pii> vpi;
typedef vector<vector<int> > vv;
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
err(++it, args...);
}
const int N = 2e5 + 5;
vii g[N] ;
int a[N], b[N], A, B;
bool vis[N] ;
void dfs(int node) {
if(vis[node]) return ;
vis[node] = 1 ;
A += a[node], B += b[node];
for(auto i : g[node]) {
if(not vis[i]) {
dfs(i) ;
}
}
}
void solve() {
int n, m; cin >> n >> m ;
rep(i,1,n+1) cin >> a[i] ;
rep(i,1,n+1) cin >> b[i] ;
rep(i,0,m) {
int u,v; cin >> u >> v;
g[u].eb(v) ;
g[v].eb(u) ;
}
rep(i,1,n+1) {
if(not vis[i]) {
A = 0 , B = 0 ;
dfs(i) ;
if(A != B) {
cout << "No"; return ;
}
}
}
cout << "Yes" ;
}
int32_t main() {
#ifdef LOCAL
freopen("error.txt", "w", stderr);
#endif
fast;
// int t; cin >> t; while(t--)
solve() ;
// cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
return 0;
} |
#include <stdio.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <vector>
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
using namespace std;
int main(){
vector<long>viOrd(10,0);
for(int i = 1; i < 10; i++){
viOrd[i] = viOrd[i-1] * 10 + 9;
}
long n,k;
cin >> n >> k;
long nTmp = n;
for(int i = 0; i < k; i++){
vector<long> viTmp;
long nThis = nTmp;
while(nThis > 0){
viTmp.push_back(nThis%10);
nThis /= 10;
}
sort(viTmp.begin(), viTmp.end());
long iSize = long(viTmp.size());
nTmp = 0;
for(long i = 0; i < iSize/2; i++){
nTmp += (viTmp[iSize-i-1]-viTmp[i]) * (viOrd[iSize-i-1]-viOrd[i]);
}
}
cout << nTmp << endl;
return 0;
}
| #include<bits/stdc++.h>
#define debug(a) cout<<#a<<"="<<a<<endl
#define rep(i,a,b) for(ll i=a,i##end=b;i<=i##end;++i)
#define drep(i,a,b) for(ll i=a,i##end=b;i>=i##end;--i)
#define erep(i,a) for(ll i=hd[a],y;(y=to[i]);i=nxt[i])
typedef long long ll;
//57200710018
using namespace std;
char IO;
inline ll rd() {
ll res=0,f=1;
while(IO=getchar(),IO<48||IO>57)if(IO=='-')f=0;
do res=(res<<3)+(res<<1)+(IO^48);
while(IO=getchar(),IO>=48&&IO<=57);
if(f)return res;
return -res;
}
inline void Max(ll &a,ll b) {
((a<b)&&(a=b));
}
inline void Min(ll &a,ll b) {
((a>b)&&(a=b));
}
bool let;
const ll M=2005,P=1e9+7;
ll mp[M][M],s1[M][M],s2[M][M];
char s[M];
bool vis[M][M];
bool vio;
ll Pow(ll a,ll b) {
ll res=1;
for(; b; b>>=1,a=a*a%P)
if(b&1)res=res*a%P;
return res;
}
int main() {
// printf("%.3f",(&vio-&let)/1024.0/1024);
ll n=rd(),m=rd(),tot=0,ans=0;
rep(i,1,n) {
scanf("%s",s+1);
rep(j,1,m)if(s[j]=='.')mp[i][j]=1,tot++;
}
// rep(i,1,n){rep(j,1,m)printf("%d",mp[i][j]);puts("");}
rep(i,1,n) {
rep(j,1,m) {
if(!vis[i][j]&&mp[i][j]) {
ll t=j;
while(mp[i][t])t++;
rep(k,j,t-1) {
s1[i][k]=t-j;
vis[i][k]=1;
}
}
}
}
memset(vis,0,sizeof vis);
rep(i,1,n) {
rep(j,1,m) {
if(!vis[i][j]&&mp[i][j]) {
ll t=i;
while(mp[t][j])t++;
rep(k,i,t-1) {
s2[k][j]=t-i;
vis[k][j]=1;
}
}
}
}
rep(i,1,n)rep(j,1,m)if(mp[i][j]) {
ll t=s1[i][j]+s2[i][j]-1;
ans=(ans+(Pow(2,t)-1)*Pow(2,tot-t)%P)%P;
}
printf("%lld",ans);
} |
#include <bits/stdc++.h>
#define int long long
using namespace std;
long long a;
long long n, m;
long long s=0;
vector <int> dd;
void xy(long long r, vector <vector <int>> &qw, vector <vector <int>> &as, long long e, long long d, vector <int> &dd)
{
if (r!=n+1)
{
for (int i=0; i<qw[e].size(); ++i)
{
dd[r-1]=e;
xy(r+1, qw, as, qw[e][i], d+as[e][i], dd);
}
}
else if (e==0 && d==m)
{
int y=0;
vector <int> ff(n);
for (int i=0; i<n; ++i)
{
ff[i]=dd[i];
}
sort(ff.begin(), ff.end());
for (int i=1; i<n; ++i)
if (ff[i]==ff[i-1])
y=1;
if (y==0)
s+=1;
}
}
signed main ()
{
cin >> n >> m;
vector <vector <int>> qw (n);
vector <vector <int>> as (n);
for (int i=0; i<n; ++i)
{
for (int ii=0; ii<n; ++ii)
{
int f;
cin >> f;
qw[i].push_back(ii);
as[i].push_back(f);
}
}
for (int i=0; i<n; ++i)
dd.push_back(0);
a=n;
xy(1, qw, as, 0, 0, dd);
cout << s << endl;
}
| #include <bits/stdc++.h>
#define pb push_back
typedef long long ll;
using namespace std;
int main() {
int n, k;
cin >> n >> k;
int cost[n + 1][n + 1];
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
cin >> cost[i][j];
vector <int> vec;
for (int i = 1; i <= n; ++i)
vec.pb(i);
vec.pb(1);
int cnt = 0;
do {
int tot = 0;
for (int i = 0; i < n; ++i)
tot += cost[vec[i]][vec[i + 1]];
if (tot == k)
++cnt;
} while (next_permutation(vec.begin() + 1, vec.begin() + n));
cout << cnt << endl;
} |
#include<bits/stdc++.h>
#define it register int
#define ct const int
#define il inline
using namespace std;
#define P 998244353
const int N=1000005;
int n,a[N],f[2][105][10005],fac[N];
il void add(it &p,ct q){p+=q,p>=P?p-=P:0;}
int main(){
scanf("%d",&n);it i,j,sum=0,ans=0;
for(i=fac[0]=1;i<=n;++i) scanf("%d",&a[i]),sum+=a[i],fac[i]=(0ll+fac[i-1])*i%P;
std::sort(a+1,a+1+n);
if(sum&1) return puts("0"),0;
if(a[n]>sum/2) return puts("0"),0;
sum>>=1,f[0][0][0]=1;
//for(i=0;i<=n;++i) f[i][0][0]=1;
for(i=1;i<=n;++i){
memset(f[i&1],0,sizeof(f[i&1]));
for(it k=0;k<=n;++k)
for(j=0;j<=sum;++j)
f[i&1][k][j]=f[(i-1)&1][k][j],a[i]<=j?add(f[i&1][k][j],f[(i-1)&1][k-1][j-a[i]]),0:0;
}
// for(i=1;i<=n;++i) printf("%d ",f[n&1][i][sum]);puts("");
for(i=1;i<=n;++i) ans=(ans+(0ll+f[n&1][i][sum])*fac[i]%P*fac[n-i])%P;
printf("%d",ans);
return 0;
}
| #include "bits/stdc++.h"
using namespace std;
#define ffor(n) for(int i = 0; i < n; i++)
#define fffor(n) for(int j = 0; j < n; j++)
#define uwu ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize("Ofast")
const int INF = 1e9 + 7;
const long long INF2 = 1e17;
int main(void) {
uwu
int n, m; cin >> n >> m;
vector <string> v(n);
ffor(n) cin >> v[i];
const int mod = 998244353;
long long ans = 1;
for(int i = 0, j = 0; i != n - 1 || j != m - 1; ) {
set <char> colours;
for(int x = i, y = j; 0 <= x && x < n && 0 <= y && y < m; x++, y--) {
if (v[x][y] != '.') colours.insert(v[x][y]);
}
if ((int)colours.size() == 0) {
ans *= 2;
ans %= mod;
}
else if ((int)colours.size() == 1) {
}
else if ((int)colours.size() == 2) {
cout << 0 << '\n';
return 0;
}
if (j == m - 1) i++;
else j++;
}
if (v[n - 1][m - 1] == '.') ans *= 2;
ans %= mod;
cout << ans << '\n';
}
/*
C:\Users\kenne\OneDrive\Desktop\competitive_programming\main.cpp
*/
|
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vi=vector<int>;
#define INF 1000000001
int main() {
int N,M; cin>>N>>M;
vi H(N), W(M);
for (int i = 0; i < N; i++)cin>>H[i];
for (int i = 0; i < M; i++)cin>>W[i];
sort(H.begin(),H.end());
sort(W.begin(),W.end());
vi pair_sum((N-1)/2+1, 0);
for (int i = 1; i <= (N-1)/2; i++){
pair_sum[i]=pair_sum[i-1]+H[N-1-2*(i-1)]-H[N-2-2*(i-1)];
}
priority_queue<int, vi, greater<int>> p;
for (int i = 1; i < N; i++)p.push(H[i]);
int waited_partner=H[0];
int made_pair=0;
int made_pair_num=0;
int next=INF;
int ans=INF;
for (int i = 0; i < M; i++){
int join=W[i];
if(p.empty()){
ans=min(ans, made_pair+abs(join-waited_partner));
continue;
}
next=p.top();
while(join>next){
made_pair+=next-waited_partner; p.pop();
made_pair_num++;
waited_partner=p.top(); p.pop();
if(!p.empty())next=p.top();
else next=INF;
}
ans=min(ans, made_pair+abs(join-waited_partner)+pair_sum[(N+1)/2-1-made_pair_num]);
}
cout<<ans<<endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define ll long long
#define pp pair<ll,ll>
#define ld long double
#define all(a) (a).begin(),(a).end()
#define mk make_pair
int inf=1000001000;
ll INF=2e18;
ll MOD=1000000007;
ll mod=998244353;
int main() {
int n,l;
cin >> n >> l;
vector<int> a(n+2),b(n+2);
rep(i,n) cin >> a[i+1];
rep(i,n) cin >> b[i+1];
a[0]=0;
a[n+1]=l+1;
b[0]=0;
b[n+1]=l+1;
ll ans=0;
for (int i=1;i<=n;i++){
int l=-1,r=n+1;
while(r-l>1){
int mid=(r+l)/2;
if (mid<=i) {
l=mid;
continue;
}
if (a[mid]-(mid-i)>=b[i]) r=mid;
else l=mid;
}
if (a[r]-(r-i)==b[i] && a[i]!=b[i]){
rep(j,r-i){
a[r-j-1]=a[r]-j-1;
ans++;
}
}
}
for (int i=n;i>=1;i--){
int l=0,r=n+2;
while(r-l>1){
int mid=(r+l)/2;
if (mid>=i) {
r=mid;
continue;
}
if (a[mid]+(i-mid)<=b[i]) l=mid;
else r=mid;
}
if (a[l]+(i-l)==b[i] && a[i]!=b[i]){
rep(j,i-l){
a[l+j+1]=a[l]+j+1;
ans++;
}
}
}
bool w=true;
rep(i,n){
if (a[i+1]!=b[i+1]) w=false;
}
if (w) cout << ans << endl;
else cout << -1 << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
vector <int> a(n),b(n);
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=0;i<n;i++){
cin>>b[i];
}
int ans=0;
for(int l=1;l<=1000;l++){
for(int r=l;r<=1000;r++){
bool ok=true;
for(int i=0;i<n;i++){
if(l>=a[i] and r<=b[i]){
}
else{
ok=false;
}
}
if(ok){
ans=max(ans,r-l+1);
}
}
}
cout<<ans<<"\n";
return 0;
} | #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> a, b;
int n;
cin >> n;
a.reserve(n);
b.reserve(n);
for (int i = 0; i < n; i++) {
int temp;
cin >> temp;
a.push_back(temp);
}
for (int i = 0; i < n; i++) {
int temp;
cin >> temp;
b.push_back(temp);
}
sort(a.begin(), a.end());
sort(b.begin(), b.end());
if (a[n - 1] > b[0])
cout << 0 << endl;
else
cout << b[0] - a[n - 1] + 1 << endl;
return 0;
} |
#if 1
#ifdef LOCAL
#define _GLIBCXX_DEBUG 1
#define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif
#include <bits/stdc++.h>
using namespace std;
#define all(x) begin(x), end(x)
typedef pair<int, int> pii;
typedef vector<int> vi;
#endif
int cnt[210000];
int sm[420000];
const int mod = 998244353;
const int inv2 = (998244354/2);
long long calc(long long x) {
return ((x * (x+1)) % mod) * inv2 % mod;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int a,b,c;
cin >> a >> b >> c;
long long t = calc(a) * calc(b) % mod;
t = (t * calc(c)) % mod;
cout << t << endl;
} | #include <stdio.h>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <functional>
//#include <atcoder/all>
//using namespace atcoder;
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef vector<vector<int>> v2int;
typedef vector<vector<vector<int>>> v3int;
typedef vector<ll> vll;
typedef vector<vector<ll>> v2ll;
typedef vector<vector<vector<ll>>> v3ll;
typedef list<int> liint;
typedef pair<int, int> pint;
typedef vector<pair<int, int>> vpint;
typedef vector<pair<ll, ll>> vpll;
typedef vector<pair<ll, int>> vpll_int;
typedef vector<pair<int, ll>> vpint_ll;
typedef set<pair<int, int>> spint;
typedef set<pair<ll, int>> spll;
typedef unordered_map<int, unordered_set<int>> Graph;
const int INF = int(2e9);
const ll LINF = ll(2e9) * ll(2e9);
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T, class C> void chmax(T& a, C b) { a > b ? : a = b; }
template<class T, class C> void chmin(T& a, C b) { a < b ? : a = b; }
const int MAX = 510000;
const ll MOD = 998244353;
ll fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
ll 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] % MOD) % MOD;
}
ll modpow(ll a, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
ll modinv(ll a, ll mod) {
return modpow(a, mod - 2, mod);
}
ll mod(ll val, ll m) {
ll res = val % m;
if (res < 0) res += m;
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N, K;
cin >> N >> K;
vll A(N);
rep(i, N) cin >> A[i];
unordered_map<int, ll> SK;
REP(x, 0, K + 1) {
rep(i, N) {
SK[x] += (modpow(A[i], abs(x), MOD));
SK[x] %= MOD;
}
}
COMinit();
REP(X, 1, K + 1) {
ll ans = 0;
rep(k, X + 1) {
ll sk = (SK[X - k] * SK[k]) % MOD;
//sk = (sk * modinv(2, MOD)) % MOD;
ll t = (COM(X, k) * sk) % MOD;
//t = (t * modinv(2, MOD)) % MOD;
ans += t;
ans %= MOD;
}
ll sub = modpow(2, X, MOD) * SK[X] % MOD;
ans = mod(ans - sub, MOD);
ans = ans* modinv(2, MOD) % MOD;
cout << ans << endl;
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, d, h;cin>>n>>d>>h;
cout<<fixed<<setprecision(20);
vector<pair<int, int>>v;
int a[n], b[n];for(int i=0;i<n;i++){
cin>>a[i]>>b[i];
v.push_back({b[i], a[i]});
}
double ans=0.0;
for(int i=0;i<n;i++){
int dd=v[i].second,hh= v[i].first;
double x=((h-hh)*(1.0)*d)/(d-dd);
ans=max(ans, h-x);
}
cout<<ans;
} | #define _DEBUG
#include "bits/stdc++.h"
//#include <atcoder/all>
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0,a1,a2,a3,a4,a5,x,...) x
#define debug_1(x1) cout<<#x1<<": "<<x1<<endl
#define debug_2(x1,x2) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<endl
#define debug_3(x1,x2,x3) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<endl
#define debug_4(x1,x2,x3,x4) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<endl
#define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<", "#x5<<": "<<x5<<endl
#define debug_6(x1,x2,x3,x4,x5,x6) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<", "#x5<<": "<<x5<<", "#x6<<": "<<x6<<endl
#ifdef _DEBUG
#define debug(...) CHOOSE((__VA_ARGS__,debug_6,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__)
#else
#define debug(...)
#endif
#define rep(index,num) for(int index=0;index<(int)num;index++)
#define rep1(index,num) for(int index=1;index<=(int)num;index++)
#define brep(index,num) for(int index=(int)num-1;index>=0;index--)
#define brep1(index,num) for(int index=(int)num;index>0;index--)
#define scan(argument) cin>>argument
#define prin(argument) cout<<argument<<endl
#define prind(argument) cout<<std::fixed<<setprecision(13)<<argument<<endl
#define kaigyo cout<<endl
#define eps 1e-7
#define PI acosl(-1)
#define mp(a1,a2) make_pair(a1,a2)
#define ALL(a) (a).begin(),(a).end()
#define rALL(a) (a).rbegin(),(a).rend()
#define puba emplace_back
#define pubamp(a,b) emplace_back(mp(a,b))
typedef long long ll;
typedef long double ld;
using namespace std;
//using namespace atcoder;
typedef pair<ll,ll> pll;
typedef pair<int,int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
ll INFl=(ll)1e+18+1;
int INF=1e+9+1;
int main(){
int N,D,H;
int d[101],h[101];
ld ymax=0;
scan(N>>D>>H);
rep(i,N){
scan(d[i]>>h[i]);
chmax(ymax,(ld)H-(ld)D*(H-h[i])/(D-d[i]));
}
prind(ymax);
return 0;
}
|
#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define rep(i,n) for(int i = 0; i < n ; ++i)
#define REP(i,a,b) for(int i = a ; i <= b ; ++i)
#define filei freopen("input.txt", "r", stdin);
#define fileo freopen("output.txt", "w", stdout);
#define gooi cout<<"Case #"<<i+1<<" :";
#define s(n) scanf("%d",&n)
#define rev(i,n) for(int i = n ; i >= 0 ; --i)
#define REV(i,a,b) for(int i = a ; i >= b ; --i)
#define miN(a,b) (((a)<(b))?(a):(b))
#define sc(n) scanf("%c",&n)
#define tr(c,i) for(typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define INF 1000000000
#define pii pair<long long int,long long int>
#define pb(a) push_back(a)
#define F first
#define S second
typedef long long ll;
using namespace std;
// Please write the recurances once :(
void pre(){}
void solve(){
ll n;cin>>n;
ll count = 0;
while(n%2==0){
count++;
n/=2;
}
if(count==0){
cout<<"Odd"<<"\n";
}else if(count==1){
cout<<"Same"<<"\n";
}else{
cout<<"Even"<<"\n";
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
pre();
long long int num = 1;
cin>>num;
for(long long int i=0;i<num;i++){
solve();
}
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define INF ((1LL<<62)-(1LL<<31))
typedef long long ll;
int main()
{
int n;
cin >> n;
rep(i,n) {
ll a;
cin >> a;
string ans;
if(a%2==1) ans="Odd";
else {
if((a/2)%2==1) ans="Same";
else ans="Even";
}
cout << ans << endl;
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (int)n; i++)
using ll = long long;
int main(){
int a[4];
int ans = 100;
rep(i,4) {
cin >> a[i];
ans = min(ans,a[i]);
}
cout << ans << endl;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
int a1,a2,a3,a4;
int main(){
cin>>a1>>a2>>a3>>a4;
cout<<min(a1,min(a2,min(a3,a4)));
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
double A, B;
int main() {
scanf("%lf%lf", &A, &B);
printf("%lf\n", 100.0 - B / A * 100);
return 0;
} | //デバッグ用オプション:-fsanitize=undefined,address
//コンパイラ最適化
#pragma GCC optimize("Ofast")
//インクルードなど
#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
typedef long long ll;
using vint = vector<int>;
using vll = vector<ll>;
using vs = vector<string>;
using vbool = vector<bool>;
template <class T> using arr = vector<vector<T>>;
//マクロ
// forループ
//引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか
// Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる
// FORAは範囲for文(使いにくかったら消す)
#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 = a; i < ll(b); i++)
#define FORD(i, a, b) for (ll i = b - 1; i >= ll(a); i--)
#define FORA(i, I) for (const auto &i : I)
// xにはvectorなどのコンテナ
#define ALL(x) x.begin(), x.end()
#define SIZE(x) ll(x.size())
//定数
#define INF 1000000000000 // 10^12:∞
#define MOD 1000000007 // 10^9+7:合同式の法
#define MAXR 100000 // 10^5:配列の最大のrange
// aよりもbが大きいならばaをbで更新する
// (更新されたならばtrueを返す)
template <typename T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b; // aをbで更新
return true;
}
return false;
}
// aよりもbが小さいならばaをbで更新する
// (更新されたならばtrueを返す)
template <typename T> bool chmin(T &a, const T &b) {
if (a > b) {
a = b; // aをbで更新
return true;
}
return false;
}
template <class T> void pl(T x) { cout << x << " "; }
template <class T> void pr(T x) { cout << x << endl; }
template <class T> void prvec(const vector<T> &a) {
REP(i, a.size() - 1) { cout << a[i] << " "; }
pr(a[a.size() - 1]);
}
template <class T> void prarr(const arr<T> &a) {
REP(i, a.size())
if (a[i].empty())
pr("");
else
prvec(a[i]);
}
using P = pair<ll, ll>;
void prp(const P &p) { cout << p.first << " " << p.second << endl; }
struct Constants {
ll N;
Constants() { cin >> N; }
} C;
struct Args {
vector<P> a;
ll s1, s2;
Args() : a(C.N) {
ios::sync_with_stdio(false);
cin.tie(nullptr);
REP(i, C.N) {
ll x, y;
cin >> x >> y;
a.at(i).first = x;
a.at(i).second = y;
s1 += x;
s2 += y;
}
sort(ALL(a), [](P &l, P &r) {
return (2 *l.first + l.second) > (2 * r.first + r.second);
});
}
} args;
struct Solver {
ll ans;
Solver() : ans() {}
//
void solve() {
ll s1 = args.s1;
ll s2 = 0;
FORA(p, args.a) {
if (s1 < s2) {
return;
}
++ans;
s1 -= p.first;
s2 += p.first + p.second;
}
}
void output() { pr(ans); }
} s;
int main() {
s.solve();
s.output();
return 0;
}
|
#include <bits/stdc++.h>
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(),(x).end()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
using namespace std;
typedef double DB;
typedef long double LD;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<PII> VPII;
// head
const LL MOD=1e9+7;
const int N=2e5+5;
int k,d[N];
LL dp[N][17];
string s;
int get(char c) {
if(isdigit(c)) return c-'0';
else return c-'A'+10;
}
LL dfs(int x,int st,bool a,bool b) {
if(x==SZ(s)) return !a&&d[st]==k;
if(!a&&!b&&dp[x][d[st]]!=-1) return dp[x][d[st]];
int mx=b?get(s[x]):15;
LL ret=0;
for(int i=0;i<=mx;i++) {
if(a&!i&&d[st|(1<<i)]>k) continue;
ret=(ret+dfs(x+1,(a&!i)?0:st|(1<<i),a&!i,b&(i==mx)))%MOD;
}
if(!a&&!b) dp[x][d[st]]=ret;
return ret;
}
int main() {
cin>>s>>k;
for(int i=0;i<1<<16;i++) d[i]=__builtin_popcount(i);
memset(dp,-1,sizeof dp);
cout<<dfs(0,0,true,true)<<'\n';
return 0;
} | /* -*- coding: utf-8 -*-
*
* f.cc: F - Digits Paradise in Hexadecimal
*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
using namespace std;
/* constant */
const int MAX_N = 200000;
const int MOD = 1000000007;
/* typedef */
typedef long long ll;
/* global variables */
char s[MAX_N + 4];
int dp[MAX_N + 1][17];
bool used[16];
/* subroutines */
inline int hex2dec(char c) {
return (c >= '0' && c <= '9') ? (c - '0') : (c - 'A' + 10);
}
inline void addmod(int &a, int b) { a = (a + b) % MOD; }
inline int pmod(int a, int b) { return (int)((ll)a * b % MOD); }
/* main */
int main() {
int k;
scanf("%s%d", s, &k);
int n = strlen(s);
int s0 = hex2dec(s[0]);
dp[1][1] = s0 - 1;
used[s0] = true;
int c = 1;
for (int i = 1; i < n; i++) {
for (int j = 1; j <= 16; j++)
if (dp[i][j] > 0) {
addmod(dp[i + 1][j], pmod(dp[i][j], j));
if (j < 16)
addmod(dp[i + 1][j + 1], pmod(dp[i][j], 16 - j));
}
int si = hex2dec(s[i]), e0 = 0, e1 = 0;
for (int j = 0; j < si; j++) {
if (used[j]) e0++;
else e1++;
}
addmod(dp[i + 1][c], e0);
if (c < 16) addmod(dp[i + 1][c + 1], e1);
addmod(dp[i + 1][1], 15);
if (! used[si])
used[si] = true, c++;
}
addmod(dp[n][c], 1);
printf("%d\n", dp[n][k]);
return 0;
}
|
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
#define LL long long
#define DB double
#define MAXN 100
#define Pr pair<int,int>
#define X first
#define Y second
#define INF 1000000001
#define mem(x,v) memset(x,v,sizeof(x))
LL read(){
LL x=0,F=1;char c=getchar();
while(c<'0'||c>'9'){if(c=='-')F=-1;c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
return x*F;
}
int MOD;
int add(int a,int b){return (a+b>=MOD)?a+b-MOD:a+b;}
int dec(int a,int b){return (a-b<0)?a-b+MOD:a-b;}
int mul(int a,int b){return (1LL*a*b)%MOD;}
int fst_pow(int a,int b){
int res=1;
while(b){
if(b&1)res=mul(res,a);
a=mul(a,a),b>>=1;
}return res;
}
inline void upd(int &x,int y){
x=(x+y)%MOD;
if(x<0)x+=MOD;
}
int n,k,lim,res,dp[105][1000005];
int main(){
n=read(),k=read(),MOD=read();
lim=k*n*(n-1)/2;
dp[0][0]=1;
for(int i=1;i<n;i++){
for(int j=0;j<=lim;j++){
upd(dp[i][j],dp[i-1][j]);
if(j>=i)upd(dp[i][j],dp[i][j-i]);
}
for(int j=lim;j>=i*(k+1);j--){
upd(dp[i][j],dec(0,dp[i][j-i*(k+1)]));
}
}
for(int i=1;i<=n;i++){
res=0;
for(int j=0;j<=lim;j++)
res=add(res,mul(dp[i-1][j],dp[n-i][j]));
printf("%d\n",dec(mul(res,k+1),1));
}
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const int INF = 1e9;
ll f(string s) {
vector<ll> c(10);
ll res = 0;
rep(i, 5) c[s[i] - '0']++;
for (int i = 1; i < 10; ++i) {
ll t = i;
while (c[i]--) t *= 10;
res += t;
}
return res;
}
int main() {
ll k;
string s, t;
cin >> k >> s >> t;
vector<ll> card(10, k);
rep(i, 4) card[s[i] - '0']--;
rep(i, 4) card[t[i] - '0']--;
ll res = 9 * k - 8;
ll tot = res * (res - 1);
ll cnt = 0;
for (int i = 1; i < 10; ++i) {
for (int j = 1; j < 10; ++j) {
s[4] = '0' + i;
t[4] = '0' + j;
if (f(s) > f(t)) {
if (i == j)
cnt += card[i] * (card[i] - 1);
else
cnt += card[i] * card[j];
}
}
}
long double z = (long double)cnt / tot;
cout << setprecision(15) << z << endl;
return 0;
}
|
#include <iostream>
#include <iomanip>
double ans[112][112][112];
int main(){
for(int i = 99; i >= 0; --i){
for(int j = 99; j >= 0; --j){
for(int k = 99; k >= 0; --k){
int sum = i + j + k;
ans[i][j][k] += ans[i + 1][j][k] * i;
ans[i][j][k] += ans[i][j + 1][k] * j;
ans[i][j][k] += ans[i][j][k + 1] * k;
ans[i][j][k] /= sum;
ans[i][j][k] += 1;
}
}
}
int a, b, c;
std::cin >> a >> b >> c;
std::cout << std::setprecision(100) << ans[a][b][c] << std::endl;
}
| #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int inf=0x3f,INF=0x3f3f3f3f; const ll LLINF=0x3f3f3f3f3f3f3f3f;
#define sz(x) (int)x.size()
#define af(x) x.begin(), x.end()
#define ms(x, y) memset(x, y, sizeof x);
#define fil(x, y) fill(af(x), y);
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define f first
#define s second
#define lc i << 1
#define rc i << 1 | 1
const int mod = 1e9 + 7;
ll fpow(ll a, ll b){ll ret=1;while(b){if(b&1)ret=ret*a%mod;a=a*a%mod,b>>=1;}return ret;}
ll inv(ll x){return x ? fpow(x, mod - 2) : 1;}
void madd(int &a, int b){a+=b;if(a>=mod)a-=mod;}
void msub(int &a, int b){a+=mod-b;if(a>=mod)a-=mod;}
const int MM = 2e5 + 5;
void solve(){
int a, b, c;
cin >> a >> b >> c;
vector<vector<vector<double>>> dp(101, vector<vector<double>>(101, vector<double>(101)));
function<double(int,int,int)> rec = [&](int x, int y, int z){
if (x == 100 || y == 100 || z == 100) return 0.0;
if (dp[x][y][z] != 0.0) return dp[x][y][z];
int t = x + y + z;
return dp[x][y][z] = 1.0*x/t*(1.0+rec(x+1,y,z)) + 1.0*y/t*(1.0+rec(x,y+1,z)) + 1.0*z/t*(1.0+rec(x,y,z+1));
};
cout << fixed << setprecision(10) << rec(a, b, c) << '\n';
}
int main(){
cin.sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t = 1;
//cin >> t;
while(t--) solve();
}
|
#include <bits/stdc++.h>
//#include <atcoder/all>
#define rep(i,n)for(ll i=0;i<(n);i++)
#define whole(f,x,...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x)
using namespace std;
typedef long long ll; typedef long double ld; typedef pair<ll,ll>pint;
using vll=vector<ll>; using vs=vector<string>;
#define p2a(v, p) v[p.first][p.second]
void arrdot(vector<vector<ll>>& v1,vector<vector<ll>>& v2) {
vector<vector<ll>> buf(3, vll(3,0));
rep(i,3) rep(j,3) rep(k,3) {
buf[i][j] += v1[i][k]*v2[k][j];
}
v1=buf;
}
void rotXY(pint& p, vector<vll>& rot) {
ll x=rot[0][0]*p.first+rot[0][1]*p.second+rot[0][2];
ll y=rot[1][0]*p.first+rot[1][1]*p.second+rot[1][2];
p = {x,y};
}
int main() {
ll n; cin >> n;
vector<pint> point(n);
rep(i,n) {
ll x,y; cin >> x>>y;
point[i]={x,y};
}
ll m; cin >> m;
vector<vector<vector<ll>>> rot={{{1,0,0},{0,1,0},{0,0,1}}};
vector<vector<vll>> rot_op={
{{0,1,0},{-1,0,0},{0,0,1}},
{{0,-1,0},{1,0,0},{0,0,1}},
{{-1,0,2},{0,1,0},{0,0,1}},
{{1,0,0},{0,-1,2},{0,0,1}},
};
rep(i,m) {
int cmd; cin>>cmd; cmd--;
vector<vector<ll>> buf=rot_op[cmd];
if(cmd==2) {
int p; cin>>p;
buf[0][2]*=p;
}
if(cmd==3) {
int p; cin>>p;
buf[1][2]*=p;
}
arrdot(buf, rot.back());
rot.push_back(buf);
}
// read query
ll qn; cin >> qn;
rep(i,qn) {
ll x,y; cin >> x >> y; y--;
vector<vector<ll>> rot_i=rot[x];
pint p = point[y];
rotXY(p,rot_i);
cout << p.first << " ";
cout << p.second << endl;
}
}
| #include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using db = double; using str = string;
using pi = pair<int,int>;
using vi = vector<int>; using vs = vector<str>;
using vl = vector<ll>; using vd = vector<double>;
using vvi = vector<vector<int>>;
using vvs = vector<vector<string>>;
using vvl = vector<vector<ll>>;
using vvd = vector<vector<double>>;
using pqi = priority_queue<int>;
using rpqi = priority_queue<int, vi, greater<int>>;
using umi = unordered_map<int, int>;
using usi = unordered_set<int>;
#define PB push_back
#define PF push_front
#define PK pop_back()
#define PT pop_front()
#define EB emplace_back
#define LB lower_bound
#define UB upper_bound
#define UM unordered_map
#define US unordered_set
#define PQ priority_queue
#define VT vector
#define se second
#define fi first
#define rt return
#define ln "\n"
#define ctu continue
#define F1(e) for(int i=0;i<(e);++i)
#define F2(i,e) for(int i=0;i<(e);++i)
#define F3(i,b,e) for(int i=(b);i<(e);++i)
#define F4(i,b,e,s) for(int i=(b); (s)>0?i<(e):i>(e); i+=(s))
#define Sum(a) accumulate((a).begin(), (a).end() , 0ll);
#define Min(a) *std::min_element((a).begin(), (a).end())
#define Max(a) *std::max_element((a).begin(), (a).end())
#define Sort(c) sort((c).begin(), (c).end())
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define each(x,a) for(auto& x : a)
#define all(c) (c).begin(), (c).end()
#define rall(a) rbegin(a),rend(a)
#define mst(a,x) memset(a, x, sizeof(a))
#define to_unique(a) a.erase(unique(begin(a), end(a)), end(a))
#define bct(x) (__builtin_popcountll(x))
#define in(x, a) a.find((x)) != a.end()
#define sz(x) ((int)(x).size())
#define sort_unique(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define isok(x, y) (x >= 0 && x < n && y >= 0 && y < m && !vis[x][y])
const ll mod = 1e9 + 7;
const double PI=acos(-1.0);
const int fx[4] = {1, 0, -1, 0}, fy[4] = {0, 1, 0, -1};
const int dx[8]={-1, -1, 0, 1, 1, 1, 0, -1}, dy[8]={0, 1, 1, 1, 0, -1, -1, -1};
template<class A> void read(VT<A>& v);
template<class T> void read(T& x) { cin >> x; }
template<class H, class... T> void read(H& h, T&... t) {
read(h); read(t...);
}
template<class A> void read(VT<A>& x) {
each(a, x) read(a);
}
template<class A> void wte(A x) {cout << x;}
template<class H, class... T> void wte(const H& h, const T&... t) {
wte(h); cout<<' '; wte(t...);
}
template<class A> void wte(VT<A>& x) {
bool f=1;
each(a, x){
if(f==1) {
wte(a); f=0;
} else wte(' '),wte(a);
}
cout<<'\n';
}
const int N = 1e5 + 10;
int n,m,k;
int a[N];
int main() {
IOS;
int x,y;
read(x,y);
cout<<x/100.0*y<<ln;
return 0;
} |
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <algorithm>
#include <math.h>
#include <iomanip>
#include <sstream>
#define INF 1000000009
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using p = pair<int, int>;
int main() {
ll k,n,m;
cin >> k >> n >> m;
ll x = m;
vector<ll> ans(k);
vector<p> pa(k);
rep(i,k){
ll a;
cin >> a;
ans[i] = (a*m)/n;
x -= ans[i];
ll sa = (a*m) % n;
pa[i] = {sa,i};
}
if(x == 0){
rep(i,k) cout << ans[i] << " ";
cout << endl;
return 0;
}
sort(pa.begin(), pa.end(), greater<p>() );
rep(i,x){
ans[pa[i].second]++;
}
rep(i,k) cout << ans[i] << " ";
cout << endl;
return 0;
} | #ifdef DEBUG_BUILD
# define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#ifdef DEBUG_BUILD
# include "debugger.hpp"
#else
# define debug(...)
# define debugdo(...)
#endif
using namespace std;
using ll = long long;
using ld = long double;
using pll = std::pair<ll, ll>;
template <class T> using vec = std::vector<T>;
template <class T> using vec2 = std::vector<vec<T>>;
template <class T> using vec3 = std::vector<vec2<T>>;
#define rep(i, N) for (ll i = 0; i < (N); i++)
#define rrep(i, N) for (ll i = (N) - 1; i >= 0; i--)
#define range(i, A, B) for (ll i = (A); i < (B); i++)
constexpr int MOD = 1000000007;
constexpr ll INFL = std::numeric_limits<ll>::max();
template <class T> bool chmax(T &a, const T b) { if (a < b) { a = b; return true; } return false; }
template <class T> bool chmin(T &a, const T b) { if (a > b) { a = b; return true; } return false; }
void Main() {
ll N;
ll D, H;
cin >> N >> D >> H;
ld ret = 0;
rep(i, N) {
ll d, h;
cin >> d >> h;
ld b = (ld)(h * (D - d) - (H - h) * d) / (D - d) ;
chmax(ret, b);
}
cout << ret << endl;
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(10);
Main();
std::cout << std::flush;
std::cerr << "--end--" << std::flush;
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <deque>
#include <climits>
#include <cmath>
#include <map>
#include <set>
#include <list>
#include <vector>
// #include <atcoder/convolution>
using namespace std;
using ll = long long;
#define len(s) static_cast<ll>(s.size())
#define rep(i, n) for (ll i = 0; i < n; i++)
#define all(c) c.begin(), c.end()
template<typename T, const char* fmt>
struct INPUT {
T value;
INPUT() { scanf(fmt, &value); }
operator T() { return value; }
};
const char IC_FMT[] = "%c";
const char ILL_FMT[] = "%lld";
using IC = INPUT<char, IC_FMT>;
using ILL = INPUT<ll, ILL_FMT>;
template <typename T>
void print(T t) {
cout << t << endl;
}
template <typename T, typename S, typename... Args>
void print(T t, S s, Args... args) {
cout << t << ' ';
print(s, args...);
}
template<typename... Args>
void debug(Args... args) {
#ifdef DEBUG
print(args...);
#endif
}
struct Cargo {
ll weight;
ll value;
};
bool operator<(const Cargo& a, const Cargo& b) noexcept {
return a.value < b.value;
}
struct Box {
ll number;
ll capacity;
};
bool operator<(const Box& a, const Box& b) noexcept {
return a.capacity < b.capacity;
}
struct Query {
ll l, r;
};
int main() {
ILL N, M, Q;
vector<Cargo> cargos;
rep(i, N) {
ILL W, V;
cargos.push_back({W, V});
}
vector<Box> boxes;
ll number = 1;
rep(i, M) {
ILL B;
boxes.push_back({number, B});
number++;
}
sort(all(cargos));
reverse(all(cargos));
sort(all(boxes));
set<ll> used;
rep(i, Q) {
used.clear();
ILL L, R;
ll value = 0;
for (auto c : cargos) {
// debug(L, R);
// Find box
for (auto b : boxes) {
if (used.count(b.number)) continue;
if (L <= b.number && b.number <= R) continue;
if (c.weight > b.capacity) continue;
used.insert(b.number);
value += c.value;
break;
}
}
print(value);
}
return 0;
}
| #include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <utility>
using ll = long long;
ll pow2(int x) {
ll i=1;
if (x == 0) return i;
for (int k=0; k<x; k++) {
i *= 2;
}
return i;
}
int main() {
std::vector<ll> a;
int n;
std::cin >> n;
a.reserve(pow2(n));
for (ll i=1; i<=pow2(n); i++) {
ll t;
std::cin >> t;
a.push_back(t);
}
ll m1v = 0;
size_t m1;
for (ll i=0; i<pow2(n-1); i++) {
if (a.at(i) > m1v) {
m1v = a.at(i);
m1 = i+1;
}
}
ll m2v = 0;
size_t m2;
for (ll i=pow2(n-1); i<pow2(n); i++) {
if (a.at(i) > m2v) {
m2v = a.at(i);
m2 = i+1;
}
}
std::cout << (m1v > m2v ? m2 : m1) << std::endl;
return 0;
}
|
#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_ll.hpp>
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define rrep(i,b,a) for(ll i=b;i>=a;i--)
#define fori(a) for(auto i : a )
#define all(a) begin(a), end(a)
#define set(a,b) memset(a,b,sizeof(a))
#define sz(a) a.size()
double pi=acos(-1);
#define ll long long
#define ull unsigned long long
#define pb push_back
#define PF push_front //deque
// #define mp make_pair
#define pq priority_queue
const ll mod=998244353;
#define f first
#define s second
#define pii pair< ll, ll >
#define vi vector<ll>
#define vpii vector<pii>
#define debug(v) for(auto i:v) cout<<i<<" ";
#define tc ll t; cin >> t; while(t--)
// using namespace boost::multiprecision;
using namespace std;
void optimizeIO(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
const ll N=1000005;
ll fact[N],invfact[N];
ll power(ll x,ll y){
if(y<=0) return 1LL;
ll z=power(x,y/2);
if(y%2) return (((z*z)%mod)*x)%mod;
return (z*z)%mod;
}
void pre(){
fact[0]=invfact[0]=invfact[1]=fact[1]=1;
rep(i,2,N) fact[i]=(i*fact[i-1])%mod;
rep(i,2,N) invfact[i]=(invfact[i-1]*power(i,mod-2))%mod;
}
ll nCr(ll n,ll k){ return (((fact[n]*invfact[n-k])%mod)*invfact[k])%mod; }
const ll N1=1e6+1;
vector<ll> isprime(N1,1),prime;
void seive(){
rep(i,2,sqrt(N1)+1){
if(isprime[i]){
for(ll j=i*i;j<N1;j+=i) isprime[j]=0;
prime.pb(i);
}
}
rep(i,sqrt(N1)+1,N1) if(isprime[i]) prime.pb(i);
}
struct dsu {
vector<ll> par, rank;
dsu(ll n): par(n+1), rank(n+1) {
for (ll i = 0; i <= n; i++) {
par[i] = i;
rank[i]= 1;
}
}
ll root(ll a) {
if (a == par[a]) return a;
return par[a] = root(par[a]);
}
void merge(ll a, ll b) {
a = root(a);
b = root(b);
if (a == b) return;
if (rank[a] > rank[b]) swap(a, b);
par[b] = a;
}
set<ll> parent(ll n){
set<ll> s;
for(ll i=1;i<=n;i++){
s.insert(root(i));
}
return s;
}
};
void solve(){
// ll n,k;
// cin>>n>>k;
// vector<vector<ll>> dp(n+1,vector<ll>(k+1,0));
// rep(i,1,n+1) dp[i][1]=1;
// rep(i,2,n+1){
// rep(k1,1,k+1){
// ll lim=i;
// if(k1==2) lim=(i%2==0?i/2:i/2);
// rep(j,1,lim+1){
// if(i-j>=0 ) dp[i][k1]=(dp[i][k1]+(dp[j][k1-1]*dp[i-j][1]))%mod;
// }
// }
// }
// cout<<dp[n][k]<<endl;
// int mod=998244353;
// int n,k,f[3010][3010];
// scanf("%d %d",&n,&k);
// f[0][k]=1;
// for(int i=0;i<n;i++)
// for(int j=1;j<=n;j++) if(f[i][j])
// {
// if(2*j<=n) (f[i][2*j]+=f[i][j])%=mod;
// (f[i+1][j-1]+=f[i][j])%=mod;
// }
// printf("%d",f[n][0]);
ll n,kl;
cin>>n>>kl;
vector<vector<ll>> dp(n+1,vector<ll>(n+1,0));
dp[1][1]=1;
rep(i,2,n+1){
rrep(k,n+1,1){
dp[i][k]=(dp[i][k]+dp[i-1][k-1]+(2*k<=i?dp[i][2*k]:0))%mod;
}
}
cout<<dp[n][kl]<<endl;
}
int32_t main(){
optimizeIO();
{solve();}
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define sz(v) v.size()
#define INF 100000000000000 //10^14
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;
}
// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
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;}
int main()
{
ll N, K;
cin >> N >> K;
vector<vector<mint>> dp(N+2, vector<mint>(N+2, 0));
dp.at(0).at(0) = 1;
rep(i, N+1){
for(ll j = N; j >= 0; j--){
dp.at(i+1).at(j+1) += dp.at(i).at(j);
if(j % 2 == 0 && j > 0){
dp.at(i).at(j/2) += dp.at(i).at(j);
}
}
}
cout << dp.at(N).at(K) << endl;
} |
#include <bits/stdc++.h>
#include <time.h>
#include <queue>
#define ll long long int
#define mp make_pair
#define ff first
#define ss second
#define forr(i,a,n) for(ll i=a;i<n;i++)
#define ford(i,a,n) for(ll i=n-1;i>=a;i--)
#define all(v) v.begin(),v.end()
#define testc ll ts;\
cin>>ts;\
while(ts--)
#define arn ll n;\
cin>>n;\
ll a[n];\
forr(i,0,n)cin>>a[i];\
#define arr(a,n) ll a[n];\
forr(i,0,n)cin>>a[i];\
#define sss <<" "<<
#define nnn <<"\n"
#define nl cout<<"\n";
#define prtitr(v) for(auto itr=v.begin(); itr!=v.end() ; itr++)cout<<*itr<<" ";
#define fitr(v) for(auto itr=v.begin(); itr!=v.end() ; itr++)
using namespace std;
ll M=1e9+7;
ll cdiv(ll a,ll b){
return (a%b==0)?a/b:a/b+1;
}
ll myMod(ll a, ll b){
ll r = a % b;
return r < 0 ? r + b : r;
}
ll ModPow(ll a,ll b,ll M)
{
if(M==1)return 0;
a%=M;
ll ans=1,t=1;
while(t>0&&t<=b)
{
if(t&b)
{
ans*=a;
ans%=M;
}
t<<=1;
a*=a;
a%=M;
}
return ans;
}
ll Pow(ll a,ll b)
{
if(M==1)return 0;
ll ans=1,t=1;
while(t>0&&t<=b)
{
if(t&b)
{
ans*=a;
}
t<<=1;
a*=a;
}
return ans;
}
ll gcd(ll a, ll b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
int countSetBits(int n)
{
int count = 0;
while (n > 0)
{
count++;
n &= (n-1);
}
return count;
}
// Function that return count of
// flipped number
int FlippedCount(int a, int b)
{
// Return count of set bits in
// a XOR b
return countSetBits(a^b);
}
struct Point{
int x,y;
};
bool isPrime(ll n){
if (n <= 1)
return false;
for (ll i = 2; i*i <= n; i++)
if (n % i == 0)
return false;
return true;
}
ll modInverse(ll n, ll p)
{
return ModPow(n, p - 2, p);
}
ll nCrModP(ll n, ll r, ll p)
{
if (n < r)
return 0;
if (r == 0)
return 1;
ll fac[n + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = (fac[i - 1] * i) % p;
return (fac[n] * modInverse(fac[r], p) % p
* modInverse(fac[n - r], p) % p)
% p;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll n;
cin>>n;
ll a[n],b[n];
forr(i,0,n)cin>>a[i];
forr(i,0,n)cin>>b[i];
ll sum=0;
forr(i,0,n)sum+=a[i]*b[i];
if(sum==0)cout<<"Yes" nnn;
else cout<<"No" nnn;
return 0;
}
| // AUTHOR:- Saumya Rangani
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi (3.141592653589)
#define mod 1000000007
#define float double
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define vii vector<pair<int,int>>
#define fr first
#define sc second
#define all(c) c.begin(), c.end()
#define it(a) for(auto &i:a)
#define min3(a, b, c) min(c, min(a, b))
#define min4(a, b, c, d) min(d, min(c, min(a, b)))
#define max3(a,b,c) max(a,max(b,c))
#define max4(a,b,c,d) max(a,max(b,max(c,d)))
#define read(v) for(auto &i:v)cin>>i;
#define rrep(i, n) for(int i=n-1;i>=0;i--)
#define rep(i,n) for(int i=0;i<n;i++)
#define in cin>>
#define out cout<<
#define default(a,n,b) for(int i=0;i<n;i++)a[i]=b;
#define YNans(poss) cout << (poss ? "YES" : "NO" ) << endl;
#define inarray(a,n) for(int i=0;i<n;i++)cin>>a[i];
#define fast ios_base :: sync_with_stdio(false),cin.tie(NULL);
#define MAXN 2005
int gcd(int a, int b){return (b?gcd(b,a%b):a);}
bool isPrime(int n){if(n==1) return false;if(n==2) return true;for(int i=2;i*i<=n;i++){if(n%i==0)return false;}return true;}
int power(long long x, unsigned int y, int p=mod){int res = 1;x = x % p; if (x == 0) return 0;while (y > 0){if (y & 1){res = (res*x) % p;}y = y>>1;x = (x*x) % p;}return res;}
int digit(int n){int ans=0;while(n){ans++;n/=10;}return ans;}
int lcm(int a,int b){return a*b/gcd(a,b);}
vector<int> sieve(int n) {int*arr = new int[n + 1](); vector<int> 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;}
vector<vector<int>> grp(MAXN);
bool vis[MAXN];
int dfs(int node){
int ans=1;
if(vis[node]){
return 0;
}
vis[node]=1;
// cout<<node<<endl;
for(auto i:grp[node]){
ans+=dfs(i);
}
return ans;
}
void solve(){
int n;in n;
int m;in m;
for(int i=0;i<m;i++){
int x,y;in x>>y;
grp[x].pb(y);
}
int ans=0;
for(int i=1;i<=n;i++){
memset(vis,0,sizeof(vis));
ans+=dfs(i);
}
cout<<ans<<endl;
}
int32_t main(){
fast
int t=1;
// cin>>t;
while(t--){
solve();
}
return 0;
}
|
/*
Author : MatsuTaku
Date : 02/20/21
*/
#include <bits/stdc++.h>
using namespace std;
#include <x86intrin.h>
#ifndef ONLINE_JUDGE
#define Assert(cond) assert(cond)
#else
#define Assert(cond)
#endif
#define rep(i, n) for (size_t i = 0, i##_len = (n); i < i##_len; i++)
using lint = long long int;
using ulint = unsigned long long int;
template<typename T>
using vvec = vector<vector<T>>;
template<typename T>
vvec<T> make_vvec(int n, int m, T v) { return vvec<T>(n, vector<T>(m, v)); }
template<typename T>
void chmax(T& dst, T x) { dst = max(dst, x); }
template<typename T>
void chmin(T& dst, T x) { dst = min(dst, x); }
class Solver {
public:
Solver();
void solve();
};
Solver::Solver() {}
void Solver::solve() {
string x; cin>>x;
ulint m; cin>>m;
ulint d = *max_element(x.begin(), x.end())-'0' + 1;
auto check = [&](ulint _d) {
ulint k = 0;
rep(i, x.size()) {
ulint v = x[i]-'0';
if (k > (m - v) / _d)
return false;
k = k * _d + v;
if (k > m)
return false;
}
return k <= m;
};
if (x.size() == 1) {
int ans = check(d);
cout << ans << endl;
return;
}
if (check(d)) {
ulint l = d, r = 11e17;
while (l+1 < r) {
auto c = (l+r)/2;
if (check(c)) {
l = c;
} else {
r = c;
}
}
cout << l-d+1 << endl;
} else {
cout << 0 << endl;
}
}
int main() {
cin.tie(nullptr); ios::sync_with_stdio(false);
cout<<fixed<<setprecision(10);
Solver solver;
int t = 1;
// cin>>t;
while (t--) {
solver.solve();
}
return 0;
}
| #include <bits/stdc++.h>
typedef long long ll;
typedef std::vector<int> vi;
typedef std::vector<std::vector<int>> vii;
typedef std::pair<int, int> pii;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
#define rep(i, n) for(int i = 0; i < (int)(n); ++i)
#define repi(i, a, b) for(int i = a; i < (int)(b); ++i)
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define inf 2147483647
#define inf64 9223372036854775807
#define V vector
#define Q queue
#define PQ priority_queue
#define pb push_back
#define mp make_pair
#define F first
#define S second
using namespace std;
void solution()
{
int n;
cin >> n;
V<ll> v;
ll na = 0;
rep(i, n) {
ll a, b;
cin >> a >> b;
na += a;
v.pb(a + a + b);
}
sort(all(v), greater<ll>());
ll ans = 0;
for(auto val : v) {
na -= val;
ans++;
if(na < 0){
break;
}
}
cout << ans << endl;
}
int main()
{
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(15);
solution();
return 0;
}
|
#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <algorithm>
#include <set>
#include <iomanip>
#include <stdio.h>
#include <sstream>
#include <bits/stdc++.h>
using namespace std;
int main(void){
long long int n;
cin>>n;
vector<pair<long long int,long long int>> hoge(n);
for(long long int i=0;i<n;i++){
cin>>hoge.at(i).first>>hoge.at(i).second;
}
long long int ans=9999999999999;
for(long long int i=0;i<n;i++){
for(long long int j=0;j<n;j++){
long long int pre;
if(i==j){
pre=hoge.at(i).first+hoge.at(i).second;
}else{
pre=max(hoge.at(i).first,hoge.at(j).second);
}
ans=min(ans,pre);
}
}
cout<<ans<<endl;
} | #include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
#define forr(i,n) for(int i = 0 ; i < n ; i++)
#define forr1(i,n) for(int i = 1 ; i <= n ; i++)
#define int long long int
#define pb push_back
#define endl "\n"
const int N = 2e7 + 5 ;
const int inf = 1e9 + 32;
int binary_search(vector<int> &v , int i)
{
int l = 1 , r = v.size() , ans = 0 ;
while ( r >= l)
{
int mid = (r + l ) / 2;
cout << 1 << " " << v[mid - 1] << " " << i << "\n";
cout.flush();
int z ; cin >> z ;
if (z != i)
{
l = mid + 1;
ans = l;
}
else
{
r = mid - 1;
// ans = mid ;
}
}
return ans;
}
int get_val(int digit)
{
for (int i = 0 ; i < 3 ; i++)
digit = digit * 10 + 9;
return digit;
}
bool mysort(pair<int, int> a , pair<int, int> b )
{
return a.second < b.second ;
}
int32_t 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 n ;
cin >> n;
vector<pair<int, int>> p(n);
int total = INT_MAX , min_a = INT_MAX , min_b = INT_MAX ;
int index_a = 0 , index_b = 0 ;
forr(i, n)
{
cin >> p[i].first >> p[i].second;
total = min(total , p[i].first + p[i].second);
if (p[i].first < min_a)
{
min_a = min(min_a, p[i].first) ;
index_a = i ;
}
if (p[i].second < min_b)
{
min_b = min(min_b , p[i].second);
index_b = i ;
}
}
int ans ;
if (index_b == index_a)
{
sort(p.begin() , p.end());
int a2 = p[1].first;
sort(p.begin() , p.end() , mysort);
int a3 = p[1].second ;
ans = min({ max(a2 , min_b) , max(a3 , min_a) , total});
}
else
{
ans = min(total , max(min_a , min_b));
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i = 0; i < (n); i++)
#define REPS(i, n) for(int i = 1; i <= (n); i++)
#define RREP(i, n) for(int i = (n)-1; i >= 0; i--)
#define RREPS(i, n) for(int i = (n); i > 0; i--)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define yes cout << "yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define no cout << "no" << endl
using ll = long long;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vb = vector<bool>;
using vvi = vector<vi>;
using vvl = vector<vl>;
//const int MOD = 1e9 + 7;
const int MOD = 998244353;
const int INF = 1e9 + 7;
const ll INFL = 1e18;
const double PI = 3.141592653589793;
const double EPS = 1e-9;
template<class T> bool chmax(T &a, const T &b) { if(a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if(a > b) { a = b; return true; } return false; }
signed main()
{
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
int N; cin >> N;
vi x(N), y(N); REP(i, N) cin >> x[i] >> y[i];
REP(i, N)
{
for(int j = i+1; j < N; j++)
{
for(int k = j+1; k < N; k++)
{
double tx = x[j]-x[i];
double ty = y[j]-y[i];
double sx = x[k]-x[i];
double sy = y[k]-y[i];
if(tx == 0 && sx != 0) continue;
if(ty == 0 && sy!= 0) continue;
if(sx == 0 && tx != 0) continue;
if(sy == 0 && ty!= 0) continue;
double a = sx / tx;
double b = sy / ty;
if(tx == 0 || ty == 0 || sx == 0 || sy == 0)
{
Yes;
return 0;
}
if(abs(a-b) < EPS)
{
Yes;
return 0;
}
}
}
}
No;
} | #include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
#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()
#define SZ(x) ((int)(x).size())
#define PB push_back
#define MP make_pair
#define V vector
#define P pair<int,int>
#define F first
#define S second
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
using ll = long long;
using namespace std;
const int INF =1e9+1;
// 返り値: a と b の最大公約数
// ax + by = gcd(a, b) を満たす (x, y) が格納される
long long extGCD(long long a, long long b, long long &x, long long &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
long long d = extGCD(b, a%b, y, x);
y -= a/b * x;
return d;
}
// int main() {
// long long x, y;
// extGCD(111, 30, x, y);
// cout << x << ", " << y << endl;
// }
void solve(){
int n,s,k;cin>>n>>s>>k;
long long x,y;
ll g=extGCD(k,n,x,y);
if(s%g>0){
cout<<-1<<endl;
return ;
}
s/=g;
n/=g;
k/=g;
ll ans=((-s)*x)%n;
cout<<(ans+n)%n<<endl;
return ;
}
int main() {
int t;cin>>t;
rep(i,t)solve();
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef pair<ll,int> Pair;
typedef tuple<int,int,int> tpl;
#define ALL(a) (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())
#define EXIST(m,v) (m).find((v)) != (m).end()
#define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin()
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i)
#define RREP(i,n) RFOR(i,n,0)
#define en "\n"
constexpr double EPS = 1e-9;
constexpr double PI = 3.1415926535897932;
constexpr int INF = 2147483647;
constexpr long long LINF = 1LL<<60;
constexpr long long MOD = 1000000007; // 998244353;
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;}
struct Edge{
int from, to, id=-1;
ll cost=1, k;
Edge(int from, int to, ll cost=1, ll k=1): from(from), to(to), cost(cost), k(k) {}
Edge() {}
friend istream &operator>>(istream &input, Edge &e ){
input >> e.from >> e.to >> e.cost >> e.k;
e.from--; e.to--;
return input;
}
Edge rev(void){
return Edge(to, from, cost, k);
}
};
vector<vector<Edge>> edge;
vector<bool> vis;
void Main(){
int N,M,X,Y; cin >> N >> M >> X >> Y; X--; Y--;
edge.resize(N);
REP(_,M){
Edge e; cin >> e;
edge[e.from].emplace_back(e);
edge[e.to].emplace_back(e.rev());
}
VL d(N,LINF);
priority_queue<Pair,vector<Pair>,greater<Pair>> q;
q.emplace(0,X);
while(!q.empty()){
auto [cost, v] = q.top(); q.pop();
if(!chmin(d[v], cost)) continue;
for(auto& e : edge[v]){
ll t = (cost+e.k-1)/e.k * e.k;
q.emplace(t+e.cost, e.to);
}
}
ll ans = d[Y];
cout << (ans<LINF ? ans : -1) << en;
return;
}
int main(void){
cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);cout<<fixed<<setprecision(15);
int t=1; //cin>>t;
REP(_,t) Main();
return 0;
} | #include <bits/stdc++.h>
#define int long long int
#define ll long long int
#define ld long double
#define getFaster ios_base::sync_with_stdio(false), cin.tie(nullptr)
#define rep(i, init, n) for (int i = init; i < (int)n; i++)
#define rev(i, n, init) for (int i = (int)n; i >= init; i--)
#define MOD1 1e9 + 7
#define MOD2 998244353
#define f first
#define s second
#define endl '\n'
#define pii pair<int, int>
#define tii tuple<int, int>
#define all(v) v.begin(), v.end()
#define mt make_tuple
#define precise(i) cout << fixed << setprecision(i)
#define codejam cout << "Case #" << ii + 1 << ": ";
#define impossible cout << "IMPOSSIBLE" << endl;
#define error(s) throw runtime_error(s)
#define hash hash1
std::mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
int myRand(int R)
{
int val = rng() % R;
assert(val >= 0);
return val;
}
const long double PI = atan(1.0) * 4;
const int32_t INF32 = 2e9 + 7;
const int64_t INF64 = 3e18;
const int32_t LOG = 21;
int32_t MOD = MOD1;
using namespace std;
//-------------------DEBUGGING-------------------------
void my_debugger(string s, int LINE_NUM) { cerr << endl; }
template <typename start, typename... end>
void my_debugger(string s, int LINE_NUM, start x, end... y)
{
if (s.back() != ',')
{
s += ',';
cerr << "LINE(" << LINE_NUM << "): ";
}
int i = s.find(',');
cerr << s.substr(0, i) << " = " << x;
s = s.substr(i + 1);
if (!s.empty())
cerr << ", ";
my_debugger(s, LINE_NUM, y...);
}
#ifdef AJIT
#define debug(...) my_debugger(#__VA_ARGS__, __LINE__, __VA_ARGS__);
#else
#define debug(...) ;
#endif
void setMod(int mod_val)
{
MOD = mod_val;
}
//-----------------------------------------------------
void files_init()
{
freopen("maxflow.in", "r", stdin);
freopen("maxflow.out", "w", stdout);
}
const int N = 2e5 + 5;
const int LOGN = 20;
int from[N],to[N],cost[N];
vector<int> g[N];
vector<int> vals;
int pow2[65];
int ans=0;
int getOther(int i,int edgeNo)
{
return from[edgeNo]^to[edgeNo]^i;
}
void dfs(int i,int prev=-1,int cur_xor=0)
{
vals.push_back(cur_xor);
for(auto e:g[i])
{
if(e==prev)continue;
int j=getOther(i,e);
dfs(j,e,cur_xor^cost[e]);
}
}
int32_t main()
{
getFaster;
//files_init();
int tests = 1;
//cin >> tests;
rep(ii, 0, tests)
{
setMod(MOD1);
pow2[0]=1;
rep(i,1,62)
{
pow2[i]=pow2[i-1]*2;
while(pow2[i]>=MOD)pow2[i]-=MOD;
}
int n;
cin>>n;
rep(i,1,n)
{
cin>>from[i]>>to[i]>>cost[i];
g[from[i]].push_back(i);
g[to[i]].push_back(i);
}
dfs(1);
vector<int> cnt0(62),cnt1(62);
rep(i,0,61)
{
for(auto it:vals)
{
if((1LL<<i)&it)
cnt1[i]++;
else
cnt0[i]++;
}
}
for(auto it:vals)
{
rev(i,60,0)
{
if((1LL<<i)&it)
cnt1[i]--;
else
cnt0[i]--;
}
rev(i,60,0)
{
if((1LL<<i)&it)
ans+=pow2[i]*cnt0[i];
else
ans+=pow2[i]*cnt1[i];
}
ans%=MOD;
}
cout<<ans<<endl;
}
return 0;
} |
/* winners never quit and quitters never win.
#swap */
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#pragma GCC optimize ("-O3")
using ll = long long;
using ld = long double;
const ll mod = 1000000007;
const ll inf = 1000000000000000000;
const ll rk = 256;
const ld PI = 3.141592653589793;
typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
template <typename T>
void make_unique(vector<T>& vec) {
sort(all(vec));
vec.erase(unique(all(vec)), vec.end());
}
#define endl '\n'
#define pb push_back
#define mp make_pair
#define vc vector
#define fs first
#define sec second
#define pq priority_queue
#define lb lower_bound
#define ub upper_bound
#define pll pair<ll,ll>
#define pls pair<ll,string>
#define psl pair<string,ll>
#define plc pair<ll,char>
#define pcl pair<char,ll>
#define pss pair<string,string>
#define all(x) (x).begin(), (x).end()
#define tol(s) transform(s.begin(),s.end(),s.begin(),::tolower);
#define tou(s) transform(s.begin(),s.end(),s.begin(),::toupper);
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
err(++it, args...);
}
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define T ll t=0;cin>>t;for(ll test=0;test<t;test++)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll nxt() {
ll TemporaryVariable;
cin >> TemporaryVariable;
return TemporaryVariable;
}
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
//isprime();
//coeff();
/*freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);*/
ll n=nxt(),m=nxt();
string s[n];
rep(a,0,n)
cin>>s[a];
ll ans=0;
rep(a,0,n)
{
rep(b,0,m)
{
if(a<n-1 and s[a][b]==s[a+1][b] and s[a][b]=='.')
ans++;
if(b<m-1 and s[a][b]==s[a][b+1] and s[a][b]=='.')
ans++;
}
}
cout<<ans<<endl;
printclock;
return 0;
} | #include <iostream>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <string.h>
#include <cstdio>
#include <tuple>
#include <numeric>
#include <functional>
#include <fstream>
#include <iostream>
using namespace std; using ll = long long; using ld = long double; using pll = pair<ll, ll>;
using vl = vector<ll>; using vll = vector<vl>; using vlll = vector<vll>; using vpll = vector<pll>;
using vs = vector<string>; using tll = tuple<ll, ll, ll>; using vtll = vector<tll>;
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
const ld PI = 3.1415926535897932;
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define co(i) cout << i << endl;
void in() {}
void debug_out() { cerr << endl; }
template<typename Head, typename... Tail>
void debug_out(Head h, Tail... t) {
cerr << " " << h;
if (sizeof...(t) > 0) cerr << " :";
debug_out(t...);
}
template <typename T>
ostream& operator<<(ostream& os, vector<T> vec) {
for (size_t i = 0; i < vec.size(); i++)os << vec[i] << (i + 1 == vec.size() ? "" : " ");
return os;
}
ll ctoi(char c) {
switch (c) {
case '0': return 0; case '1': return 1; case '2': return 2;
case '3': return 3; case '4': return 4; case '5': return 5;
case '6': return 6; case '7': return 7; case '8': return 8;
case '9': return 9; default: return 0;
}
}
bool pairCompare(const pll firstElof, pll secondElof)
{
return firstElof.second < secondElof.second;
}
//AtCoder以外ではコメントアウトを忘れずに!
//#include <atcoder/all>
//using namespace atcoder;
//**Snippetリスト**//
//rep, vin, all, iteLoop, bitSearch, bitList, nod, LIS, digitDP, treeDP, Rerooting//
//gcdlcm, isPrime, eratos, primeFactorize, Npow, combination, divisor, modinv, doubling//
//dfs, bfs, dijkstra, WarshallFloyd, BellmanFord, UnionFind, Kruskal, RMQ, LCA, FordFulkerson//
//**AC Libraryリスト**//
//pow_mod, inv_mod, floor_sum, convolution, convolution_ll, modint//
//fenwicktree, segtree, lazysegtree, string//
ll i, j, k, l, m; ll N, M, K, H, W, X, Y, Z, R, Q;
ll MOD = 1000000007, INF = 1LL << 60, ans = 0, z = 0, o = 1;
vll path;
//***********//
int main() {
string S, T, U;
cin >> S >> T >> U;
N = S.length();
M = T.length();
ll L = U.length();
map<char, ll> A;
for (i = 0; i < N; i++) {
A[S[i]] += 1;
}
for (i = 0; i < M; i++) {
A[T[i]] += 1;
}
for (i = 0; i < L; i++) {
A[U[i]] += 1;
}
if (A.size() > 10) {
cout << "UNSOLVABLE" << endl;
exit(0);
}
vector<char> B;
for (auto x : A) {
B.push_back(x.first);
}
while (true) {
if (B.size() == 10) break;
B.push_back('*');
}
//debug(B);
sort(B.begin(), B.end());
//reverse(S.begin(), S.end());
//reverse(T.begin(), T.end());
//reverse(U.begin(), U.end());
do {
map<char, ll> P;
vl Q(26);
//debug(B);
for (i = 0; i < 10; i++) {
if(B[i]!='*') Q[B[i]-'a'] = i;
}
ll D=0, E=0, F = 0;
ll valid = 1;
for (i = 0; i < N; i++) {
D += Q[S[i]-'a'] * pow(10, N-i-1);
if (i == 0 && Q[S[i] - 'a'] == 0) {
valid = 0;
}
}
for (i = 0; i < M; i++) {
E += Q[T[i]-'a'] * pow(10, M-i-1);
if (i == 0 && Q[T[i] - 'a'] == 0) {
valid = 0;
}
}
for (i = 0; i < L; i++) {
F += Q[U[i]-'a'] * pow(10, L-i-1);
if (i == 0 && Q[U[i]-'a'] == 0) {
valid = 0;
}
}
//debug(D, E, F);
if (D + E == F && valid) {
for (i = 0; i < N; i++) {
cout << Q[S[i]-'a'];
}
cout << endl;
for (i = 0; i < M; i++) {
cout << Q[T[i]-'a'];
}
cout << endl;
for (i = 0; i < L; i++) {
cout << Q[U[i]-'a'];
}
cout << endl;
exit(0);
}
} while (std::next_permutation(B.begin(), B.end()));
cout << "UNSOLVABLE" << endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);i++)
#define N 1000
#define INF 1000000005
#define INFLL (ll)1000000000000000005
#define PI 3.1415926535897
typedef long long ll;
#define ALL(v) (v).begin(),(v).end()
#define SZ(x) int(x.size())
#define IN(a) cin>>(a)
#define OUT(a) cout<<(a)<<endl
#define per(a,mod) ((a)%(mod)+(mod))%(mod)
typedef pair<int,int> P;
const int MAX = (1<<20);
const int MOD = 1000000007;
void solve(){
int n,m,Q;
cin>>n>>m>>Q;
vector<P>w(n);
REP(i,n)cin>>w[i].second>>w[i].first;
vector<int>x(m);
REP(i,m)cin>>x[i];
sort(ALL(w));
reverse(ALL(w));
vector<P>r(Q);
REP(i,Q){
cin>>r[i].first>>r[i].second;
r[i].first--;r[i].second--;
}
REP(t,Q){
vector<int>tmp;
REP(i,m){
if(r[t].first<=i&&i<=r[t].second)continue;
tmp.push_back(x[i]);
}
//cout<<SZ(tmp)<<"T"<<endl;
sort(ALL(tmp));
map<int,int>mp;
ll cnt=0;
if(SZ(tmp)==0){
OUT(0);
continue;
}
REP(i,n){
int p=lower_bound(ALL(tmp),w[i].second)-tmp.begin();
if(p==SZ(tmp))continue;
while(p<SZ(tmp)&&mp[p]>0)p++;
if(p==SZ(tmp))continue;
cnt+=w[i].first;
mp[p]++;
//cout<<p<<" ";
}
//cout<<endl;
OUT(cnt);
}
}
int main(){
solve();
return 0;
}
| #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <cstdio>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cstdlib>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define MOD 1000000007
#define PI 3.1415926535897932
#define INF 1e9
#define rep(i, n) for (int i = 0; i < n; i++)
#define repe(i, j, n) for (int i = j; i < n; i++)
#define repi(i, n) for (int i = 0; i <= n; i++)
#define repie(i, j, n) for (int i = j; i <= n; i++)
#define all(x) x.begin(), x.end()
#define println() cout << endl
#define P pair<int, int>
#define fi first
#define se second
typedef long long ll;
using Graph = vector<vector<ll>>;
bool bf(P a, P b) {
if(a.se == b.se) {
return a.fi < b.fi;
} else {
return a.se > b.se;
}
}
void solve1() {
ll n, m, q; cin >> n >> m >> q;
vector<P> wv(n);
rep(i, n) {
cin >> wv[i].fi >> wv[i].se;
}
vector<ll> x(m);
rep(i, m) {
cin >> x[i];
}
sort(all(wv), bf);
vector<ll> anss(q);
rep(i, q) {
ll ans = 0;
ll l, r; cin >> l >> r;
vector<ll> nw;
rep(j, m) {
if(j+1 >= l && j+1 <= r) {
continue;
}
nw.push_back(x[j]);
}
sort(all(nw));
vector<bool> fs(n, true);
for(int j = 0; j < nw.size(); j++) {
for(int k = 0; k < n; k++) {
if(fs[k] && wv[k].fi <= nw[j]) {
ans += wv[k].se;
fs[k] = false;
break;
}
}
}
anss[i] = ans;
}
rep(i, q) {
cout << anss[i] << endl;
}
}
int main()
{
solve1();
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int a,b,c;
cin >> a >> b >> c;
if(a != b && a != c && b != c) {
cout << "0";
}
else if(a == b && b == c && a == c) {
cout << a;
}
else if(a == b && a != c) {
cout << c;
}
else if(a == c && a != b) {
cout << b;
}
else if(c == b && a != b) {
cout << a;
}
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define INF 1234567890
#define ll long long
int N;
string s, t;
vector<int> S, T;
int main()
{
ios::sync_with_stdio(0); cin.tie(0);
cin.exceptions(ios::badbit | ios::failbit);
cin >> N >> s >> t;
int sz = 0, so = 0, tz = 0, to = 0;
for(int i=0; i<N; i++)
{
if (s[i] == '0') sz++, S.push_back(i);
else so++;
if (t[i] == '0') tz++, T.push_back(i);
else to++;
}
if (sz != tz) { cout << -1 << "\n"; return 0; }
int Z = sz;
int res = 0;
for(int i=0; i<Z; i++)
{
if (S[i] != T[i])
res++;
}
cout << res << "\n";
return 0;
} |
#include <bits/stdc++.h>
#define pb push_back
// #define int long long
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define ld long double
using namespace std;
const int M = 1000000007;
set<int> qq;
int cnt[4000009];
int mex = 0;
void add(int x) {
cnt[x]--;
if (cnt[x] == 0) {
qq.insert(x);
mex = min(mex, x);
}
}
void del(int x) {
cnt[x]++;
if (cnt[x] == 1) {
qq.erase(x);
if (mex == x) {
mex = *qq.upper_bound(mex);
}
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, m;
cin >> n >> m;
vector<int> A(n);
for (int i = 0; i < n; i++) cin >> A[i];
for (int i = 0; i <= n; i++) qq.insert(i);
for (int i = 0; i < m; i++) del(A[i]);
int ans = mex;
for (int i = m; i < n; i++) {
if (A[i] != A[i - m]) {
add(A[i - m]);
del(A[i]);
}
ans = min(ans, mex);
}
cout << ans << '\n';
} | #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
int N,M; cin >> N >> M;
vector<vector<int>> pos(N + 1);
rep(i,N){
int A; cin >> A;
pos[A].push_back(i);
}
rep(i,N+1){
pos[i].push_back(-1);
pos[i].push_back(N);
sort(pos[i].begin(), pos[i].end());
}
rep(i,N+1){
rep(j,pos[i].size()){
if(pos[i][j + 1] - pos[i][j] > M){
cout << i << endl;
return 0;
}
}
}
} |
#include <bits/stdc++.h>
using namespace std;
#define ms(a, b) memset((a), (b), sizeof(a))
#define eps 1e-8
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
const int N = 4e5 + 5;
const int M = 1e6 + 5;
const int INF = 0x3f3f3f3f;
const ll LL_MAX = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
#define mid (l + r >> 1)
#define ls (o << 1)
#define rs (o << 1 | 1)
#define lson ls, l, mid
#define rson rs, mid + 1, r
inline ll read(){
ll ans = 0; int f = 1; char c = getchar();
while(c < '0' || c > '9'){if (c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') {ans = ans * 10 + c - '0'; c = getchar();}
return ans * f;
}
ll qpow(ll a, ll b){
ll ans = 1;
a %= mod;
while(b){
if (b & 1) ans = ans * a % mod;
a = a * a % mod;
b >>= 1;
}
return ans;
}
ll inv(ll x){
return qpow(x, mod - 2);
}
char a[5003][5003];
int f[5003][5003];
ll n, m, k;
int main(){
n = read(), m = read(), k = read();
int h, w;
char c;
for (int i = 1; i <= k; ++i){
h = read(), w = read();
cin >> a[h][w];
}
f[1][1] = qpow(3, n * m - k);
ll i32 = inv(3) * 2 % mod;
for (int i = 1; i <= n; ++i){
for (int j = 1; j <= m; ++j){
if (a[i][j] == 'R') f[i][j + 1] += f[i][j], f[i][j + 1] %= mod;
else if (a[i][j] == 'D') f[i + 1][j] += f[i][j], f[i + 1][j] %= mod;
else if (a[i][j] == 'X') f[i][j + 1] += f[i][j], f[i + 1][j] += f[i][j],
f[i][j + 1] %= mod, f[i + 1][j] %= mod;
else {
f[i][j + 1] += f[i][j] * i32 % mod, f[i + 1][j] += f[i][j] * i32 % mod,
f[i][j + 1] %= mod, f[i + 1][j] %= mod;
}
}
}
cout << f[n][m] << "\n";
return 0;
}
|
//================code===================//
//#define TLE
#ifdef TLE
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#endif
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <ctime>
#define ci(t) cin>>t
#define co(t) cout<<t
#define LL long long
#define ld long double
#define fa(i,a,b) for(LL i=(a);i<(LL)(b);++i)
#define fd(i,a,b) for(LL i=(a);i>(LL)(b);--i)
#define setp pair<pair<int,int>,int>
#define setl pair<LL,LL>
#define M_PI 3.14159265358979323846
#define micro 0.000001
using namespace std;
#ifdef OHSOLUTION
#define ce(t) cerr<<t
#define AT cerr << "\n=================ANS=================\n"
#define AE cerr << "\n=====================================\n"
LL gcd(LL a, LL b) { return a % b ? gcd(b, a % b) : b; }
LL lcm(LL a, LL b) { return (a * b) / gcd(a, b); }
#else
#define AT
#define AE
#define ce(t)
#define __popcnt __builtin_popcount
#define gcd __gcd
#define lcm __lcm
#endif
pair <int, int> vu[9] = { {0,1},{1,0},{0,-1} ,{-1,0},{0,1},{1,0}, {-1,1} , {1,-1},{-1,-1} }; //RDLU EWSN
template<typename T, typename U> void ckmax(T& a, U b) { a = a < b ? b : a; }
template<typename T, typename U> void ckmin(T& a, U b) { a = a > b ? b : a; }
struct gcmp { bool operator()(LL a, LL b) { return a < b; } bool operator()(setl a, setl b) { return a.second < b.second; } };
struct lcmp { bool operator()(LL a, LL b) { return a > b; } bool operator()(setl a, setl b) { return a.second > b.second; } };
const int max_v = 2e5 + 7;
const int INF = 1e9 + 7;
const LL LNF = (LL)5e18 + 7ll;
const LL mod = 998244353;
LL cpow(LL x, LL k)
{
if (!x) return 0;
LL ret = 1;
while (k)
{
if (k & 1) ret = ret * x % mod;
x = x * x % mod;
k >>= 1;
}
return ret;
}
int main()
{
#ifdef OHSOLUTION
freopen("input.txt", "r", stdin);
#endif
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
LL n, m, k; ci(n >> m >> k);
LL ans = 0;
if (n == 1 || m == 1)
{
co(cpow(k, n + m - 1));
return 0;
}
if (n == 1) swap(n, m);
fa(i, 1, k + 1)
{
ans = (ans + cpow(i, n) * ((cpow(k - i + 1, m) - cpow(k - i, m) + mod) % mod) % mod) % mod;
}
co(ans%mod);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
// 総数を1000000007(素数)で割った余り
const long long mod = 1e9 + 7;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define ull unsigned long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define vc vector<char>
#define vs vector<string>
#define vpii vector<pii>
#define vpll vector<pll>
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; i++)
#define rep1(i, n) for (int i = 1, i##_len = (n); i <= i##_len; i++)
#define repr(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rep1r(i, n) for (int i = ((int)(n)); i >= 1; i--)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define RSORT(x) sort(rall(x));
#define pb push_back
#define mp make_pair
#define INF (1e9)
#define PI (acos(-1))
#define EPS (1e-7)
ull gcd(ull a, ull b) { return b ? gcd(b, a % b) : a; }
ull lcm(ull a, ull b) { return a / gcd(a, b) * b; }
int main() {
// cout << fixed << setprecision(5);
std::string S;
std::cin >> S;
ll size = S.size();
ll Maru = 0;
ll Hatena = 0;
for (ll i = 0; i < size; i++) {
if (S.at(i) == 'o') {
Maru++;
} else if (S.at(i) == '?') {
Hatena++;
}
}
//cout << Maru << endl;
//cout << Hatena << endl;
if (Maru > 4) {
cout << 0 << endl;
}
else if (Maru == 4) {
cout << 24 << endl;
}
else if (Maru == 3) {
cout << 36 + 24 * Hatena << endl;
}
else if (Maru == 2) {
cout << 14 + 36 * Hatena + 24 * Hatena * (Hatena - 1)/2 << endl;
}
else if (Maru == 1) {
cout << 1 + 14 * Hatena + 36 * Hatena * (Hatena - 1)/2 + 24 * Hatena * (Hatena - 1) * (Hatena - 2)/6 << endl;
}
else if (Maru == 0) {
cout << Hatena + 14 * Hatena * (Hatena - 1)/2 + 36 * Hatena * (Hatena - 1) * (Hatena - 2)/6 +
24 * Hatena * (Hatena - 1) * (Hatena - 2) * (Hatena - 3)/24
<< endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(), x.end()
#define mp(x, y) make_pair(x, y)
#define sz(li) (int) (li).size()
#define sum(a) (accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a) (*min_element((a).begin(), (a).end()))
#define maxe(a) (*max_element((a).begin(), (a).end()))
#define mini(a) (min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a) (max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) (lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) (upper_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vi>
#define vvl vector<vl>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vpii vector<pii>
#define vpll vector<pll>
#define endl "\n"
template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v);
template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.f << ", " << p.s << ")"; }
template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) { cout << "[ "; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << " ]"; }
template<typename A> ostream& operator<<(ostream &cout, vector<vector<A>> const &v) { cout << "["; for (int i = 0; i < v.size(); i++) { if (i) { cout << " "; } cout << v[i]; if (i < v.size() - 1) { cout << endl; } } return cout << "]"; }
template<typename A, typename B> istream& operator>>(istream& cin, pair<A, B> &p) { cin >> p.first; return cin >> p.second; }
template<typename T, typename U> void cmin(T& a, U b){ if (a > b) a = b; }
template<typename T, typename U> void cmax(T& a, U b){ if (a < b) a = b; }
void solve() {
string s; cin >> s;
auto check = [&](int n) {
string num = to_string(n);
if (sz(num) != 4) {
reverse(all(num));
while (sz(num) != 4) {
num += '0';
}
reverse(all(num));
}
bool flag1 = true;
for (int i = 0; i <= 9; i++) {
if (s[i] == 'o') {
bool flag2 = false;
for (char c : num) {
flag2 |= (c - '0' == i);
}
flag1 &= flag2;
}
}
for (char c : num) {
flag1 &= (s[c - '0'] != 'x');
}
return flag1;
};
int ans = 0;
for (int i = 0; i <= 9999; i++) {
if (check(i)) {
ans++;
}
}
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
// cerr << "Time: " << 1000.0*clock()/CLOCKS_PER_SEC << " ms\n";
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 1000000007;
int f[100000][2] = {0}, times[100000][2] = {0};
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n;
cin >> n >> f[0][0];
times[0][0] = 1;
for (int i = 1; i < n; ++i) {
times[i][0] = (times[i - 1][0] + times[i - 1][1]) % mod;
times[i][1] = times[i - 1][0];
cin >> f[i][1];
f[i][0] = ((f[i - 1][0] + f[i - 1][1]) % mod + (ll)f[i][1] * times[i][0] % mod) % mod;
f[i][1] = (f[i - 1][0] - (ll)f[i][1] * times[i][1]) % mod;
if (f[i][1] < 0) f[i][1] += mod;
}
cout << (f[n - 1][0] + f[n - 1][1]) % mod;
} | #include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define lli long long int
#define tc int t;cin>>t;while(t--)
#define all(x) x.begin(),x.end()
#define f first
#define s second
#define pb push_back
#define pi pair<int,int>
#define pll pair<lli,lli>
#define vi vector<int>
#define vll vector<lli>
#define vpi vector<pair<int,int> >
#define vpll vector<pair<lli,lli> >
#define vvi vector<vector<int> >
#define vvll vector<vector<lli> >
#define vvpi vector<vector<pair<int,int> > >
#define vvpll vector<vector<pair<lli,lli> > >
#define inf 1000000000
#define maxn 100005
lli mod=1e9+7;
int main(){
fastio;
int n;
cin>>n;
vll a(n);
for(int i=0;i<n;i++) cin>>a[i];
if(n==1){
cout<<a[0]<<"\n";
return 0;
}
if(n==2){
lli tot = (2*a[0])%mod;
cout<<tot<<"\n";
return 0;
}
lli dp[n][2];
dp[0][1]=dp[0][0]=1;
for(int i=1;i<n;i++){
dp[i][0]=(dp[i-1][0]+dp[i-1][1])%mod;
dp[i][1]=dp[i-1][0];
//cout<<dp[i][0]<<" "<<dp[i][1]<<"\n";
}
lli ans=0;
for(int i=0;i<n;i++){
int nl = i;
int nr = n-1-i;
if(i==0){
ans+= ( a[i]*(dp[nr-1][0]+dp[nr-1][1]) )%mod;
ans%=mod;
continue;
}
nl--;
if(nl and nr){
ans+=(a[i]*( ((dp[nl-1][0]+dp[nl-1][1])*(dp[nr-1][0]+dp[nr-1][1]))%mod ) )%mod;
ans%=mod;
ans-=( a[i]*((dp[nl-1][0]*dp[nr-1][0])%mod) )%mod;
ans=(ans+mod)%mod;
}
else{
int cr = max(nl,nr);
ans+=(a[i]*(dp[cr-1][0]+dp[cr-1][1]))%mod;
ans%=mod;
ans-= ( a[i]*dp[cr-1][0] )%mod;
ans=(ans+mod)%mod;
}
}
cout<<ans<<"\n";
return 0;
} |
#include<bits/stdc++.h>
#define int long long
using namespace std;
struct node {
int to,next;
} a[400005];
int aa[200005],b[200005],suma,sumb;
int h[200005],cnt,use[200005],st[200005],top;
void add(int x,int y) {
cnt++;
a[cnt].to=y;
a[cnt].next=h[x];
h[x]=cnt;
}
void dfs(int v) {
st[++top]=v;
use[v]=1;
for(int i=h[v]; i; i=a[i].next) {
if(!use[a[i].to]) {
dfs(a[i].to);
}
}
return ;
}
signed main() {
int n,m,f=1;
scanf("%lld%lld",&n,&m);
for(int i=1; i<=n; i++) {
scanf("%lld",&aa[i]);
suma+=aa[i];
}
for(int i=1; i<=n; i++) {
scanf("%lld",&b[i]);
sumb+=b[i];
}
for(int i=1; i<=m; i++) {
int x,y;
scanf("%lld%lld",&x,&y);
add(x,y);
add(y,x);
}
for(int i=1; i<=n; i++) {
if(aa[i]!=b[i]) {
f=0;
}
}
if(f==1) {
printf("Yes");
return 0;
}
if(suma!=sumb) {
printf("No");
return 0;
}
for(int i=1; i<=n; i++) {
if(!use[i]) {
top=0;
dfs(i);
int sa=0,sb=0;
for(int j=1; j<=top; j++) {
sa+=aa[st[j]];
sb+=b[st[j]];
}
if(sa!=sb) {
printf("No");
return 0;
}
}
}
printf("Yes");
return 0;
}
| #include <bits/stdc++.h>
#define fi first
#define se second
#define lc (x << 1)
#define rc (x << 1 | 1)
#define gc getchar() //(p1==p2&&(p2=(p1=buf)+fread(buf,1,size,stdin),p1==p2)?EOF:*p1++)
#define mk make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define IT iterator
#define vi vector<int>
#define TP template <class o>
#define SZ(a) ((int)a.size())
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int N = 210, size = 1 << 20, mod = 998244353, inf = 2e9;
// char buf[size],*p1=buf,*p2=buf;
template <class o> void qr(o& x) {
char c = gc;
x = 0;
int f = 1;
while (!isdigit(c)) {
if (c == '-')
f = -1;
c = gc;
}
while (isdigit(c))
x = x * 10 + c - '0', c = gc;
x *= f;
}
template <class o> void qw(o x) {
if (x / 10)
qw(x / 10);
putchar(x % 10 + '0');
}
template <class o> void pr1(o x) {
if (x < 0)
x = -x, putchar('-');
qw(x);
putchar(' ');
}
template <class o> void pr2(o x) {
if (x < 0)
x = -x, putchar('-');
qw(x);
putchar('\n');
}
int n, a[N], b[N], p[N];
bool vis[N], f[N];
void ex() {
puts("No");
exit(0);
}
void add(int x, int y) {
if (~x) {
if (vis[x])
ex();
vis[x] = 1;
p[x] = y;
}
}
bool pd(int l, int r) {
int len = (r - l + 1) / 2, mid = l + len;
for (int i = l; i < mid; i++) {
if (vis[i]) {
if (~p[i]) {
if (p[i] != i + len)
return 0;
} else if (vis[i + len])
return 0;
} else {
if (~p[i + len])
return 0;
}
}
return 1;
}
int main() {
qr(n);
memset(p, -1, sizeof p);
for (int i = 1; i <= n; i++) {
qr(a[i]);
qr(b[i]);
add(a[i], b[i]);
add(b[i], a[i]);
if (~a[i] && ~b[i] && a[i] >= b[i])
ex();
}
n *= 2;
f[0] = 1;
for (int i = 2; i <= n; i += 2)
for (int j = 0; j < i; j += 2)
f[i] |= f[j] && pd(j + 1, i);
puts(f[n] ? "Yes" : "No");
return 0;
}
|
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <iostream>
#include <cassert>
#include <cmath>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <cstdlib>
using namespace std;
#define INF 1e+9
#define mp make_pair
#define pb push_back
#define fi first
#define fs first
#define se second
#define i64 long long
#define li long long
#define lint long long
#define pii pair<int, int>
#define vi vector<int>
#define forn(i, n) for (int i = 0; i < (int)n; i++)
#define fore(i, b, e) for (int i = (int)b; i <= (int)e; i++)
const int MAXN = 2e5+5;
int d[MAXN];
int t_in[MAXN];
int t_out[MAXN];
int timer = 0;
vi edges[MAXN];
void dfs(int v, int depth) {
d[v] = depth;
t_in[v] = timer++;
for (int u : edges[v]) {
dfs(u, depth + 1);
}
t_out[v] = timer++;
}
int main() {
int n;
scanf("%d", &n);
fore(i, 2, n) {
int x;
scanf("%d", &x);
edges[x].pb(i);
}
dfs(1, 0);
vector<vi> lst(n + 1);
fore(i, 1, n) {
lst[d[i]].pb(t_in[i]);
}
forn(i, n) {
sort(lst[i].begin(), lst[i].end());
}
int queries;
scanf("%d", &queries);
forn(q, queries) {
int u, d;
scanf("%d%d", &u, &d);
if (lst[d].empty()) {
printf("0\n");
continue;
}
auto beg = lower_bound(lst[d].begin(), lst[d].end(), t_in[u]);
auto end = upper_bound(lst[d].begin(), lst[d].end(), t_out[u]);
printf("%d\n", end - beg);
}
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define fi first
#define se second
#define N 214514
#define all(x) (x).begin(), (x).end()
const ll mod = 1000000007;
// const ll mod = 998244353;
const ll inf = 8000000000000000000;
void dfs(ll from, vector<vector<ll>>& v, vector<P>& pos, ll* cnt, ll hei,
vector<vector<ll>>& num, vector<ll>& h) {
pos[from].fi = *cnt;
(*cnt)++;
num[hei].push_back(pos[from].fi);
h[from] = hei;
for (ll x : v[from]) {
hei++;
dfs(x, v, pos, &(*cnt), hei, num, h);
hei--;
}
pos[from].se = *cnt;
(*cnt)++;
}
void solve() {
ll n;
cin >> n;
vector<vector<ll>> v(n + 1);
for (int i = 2; i <= n; i++) {
ll x;
cin >> x;
v[x].push_back(i);
}
vector<vector<ll>> num(n + 1);
ll cnt = 0, hei = 0;
vector<P> pos(n + 1);
vector<ll> h(n + 1);
dfs(1, v, pos, &cnt, hei, num, h);
ll q;
cin >> q;
while (q--) {
ll u, d;
cin >> u >> d;
auto itl = lower_bound(all(num[d]), pos[u].fi),
itr = lower_bound(all(num[d]), pos[u].se);
cout << distance(num[d].begin(), itr) - distance(num[d].begin(), itl)
<< endl;
}
}
int main() {
solve();
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define all(a) a.begin(),a.end()
#define pii pair<int,int>
#define fi first
#define se second
#define rep(i,k,n) for(int i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
const int M=1e9+7,N=205001;
int mm(int x,int y){x%=M,y%=M;return (x*y)%M;}//Modular Multiply
int po(int x,int y){ if(!y)return 1;int a=po(x,y/2)%M;if(y%2)return mm(a,mm(a,x));return mm(a,a);}//(x**y)%M
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
double pi,th,n,x0,y0,xn,yn,x1,y1,xm,ym;
cin>>n>>x0>>y0>>xn>>yn;
pi=2.0*acos(0.0);
th=(2*pi)/n;
xm=(x0+xn)/2;
ym=(y0+yn)/2;
x0-=xm;y0-=ym;
x1=x0*cos(th)-y0*sin(th)+xm;
y1=x0*sin(th)+y0*cos(th)+ym;
cout<<fixed << setprecision(12)<<x1<<" "<<y1;
}
| #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <unordered_map>
#include <vector>
// #include <atcoder/all>
/*
cd $dir && g++ -std=c++17 -Wall -Wextra -O2 -DATCODERDEBUG
-I/mnt/d/MyProjects/atcoder/lib/ac-library $fileName && echo 'compilation ok!'
&& ./a.out
*/
using namespace std;
#define REP(i, n) for (ll i = 0; i < ll(n); i++)
#define FOR(i, a, b) for (ll i = a; i <= ll(b); i++)
#define ALL(x) x.begin(), x.end()
#define dame(a) \
{ \
cout << a << endl; \
return 0; \
}
typedef long long ll;
class dstream : public ostream {};
template <typename T>
dstream &operator<<(dstream &os, T v) {
#ifdef ATCODERDEBUG
cout << "\033[1;31m" << v << "\033[0m";
#endif
return os;
}
ostream &endd(std::ostream &out) {
#ifdef ATCODERDEBUG
out << std::endl;
#endif
return out;
}
dstream dout;
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
os << "[ ";
for (auto const &x : v) {
os << x;
if (&x != &v.back()) {
os << " : ";
}
}
os << " ]" << endl;
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<vector<T>> &v) {
os << "[" << endl;
for (auto const &x : v) {
os << " ";
os << x;
}
os << "]" << endl;
return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &v) {
os << "[ " << v.first << " " << v.second << " ]" << endl;
return os;
}
template <typename T, typename U, typename Comp = less<>>
bool chmax(T &xmax, const U &x, Comp comp = {}) {
if (comp(xmax, x)) {
xmax = x;
return true;
}
return false;
}
template <typename T, typename U, typename Comp = less<>>
bool chmin(T &xmin, const U &x, Comp comp = {}) {
if (comp(x, xmin)) {
xmin = x;
return true;
}
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll N;
cin >> N;
long double ax, ay, bx, by, cx, cy, v1x, v1y, v2x, v2y;
cin >> ax >> ay;
cin >> bx >> by;
cx = (ax + bx) / 2.0;
cy = (ay + by) / 2.0;
v1x = ax - cx;
v1y = ay - cy;
long double c = cosf64x(2.0 * 3.1415926 / (long double)N);
long double s = sinf64x(2.0 * 3.1415926 / (long double)N);
v2x = c * v1x - s * v1y;
v2y = s * v1x + c * v1y;
long double dx = cx + v2x;
long double dy = cy + v2y;
cout << std::fixed << std::setprecision(15) << dx << " " << dy << endl;
// cout << std::fixed << std::setprecision(15) << sqrt(ans) << endl;
} |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 101;
map<int,int>f;
int n, a[N];
set<int>s;
set<int>::iterator it;
signed main()
{
scanf("%d",&n);
for(int i = 1;i <= n; i++) scanf("%d",&a[i]);
sort(a + 1,a + n + 1);
int ans = 1;
for(int i = 1;i <= n; i++)
for(int j = 1;j * j <= a[i]; j++)
{
if(a[i] % j == 0)
{
if(!f[j] && j < a[1]) f[j] = a[i];
if(j < a[1]) f[j] = __gcd(f[j],a[i]),s.insert(j);
if(j * j == a[i]) continue;
if(!f[a[i]/j] && a[i]/j < a[1]) f[a[i]/j] = a[i];
if((a[i]/j) < a[1]) f[a[i]/j] = __gcd(a[i],f[a[i]/j]),s.insert(a[i]/j);
}
}
for(it = s.begin();it != s.end(); it++)
if(f[*it] == *it) ++ ans;
cout << ans;
}
| #include <bits/stdc++.h>
using namespace std;
template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl
using ll = long long;
const int maxn = 1e6 + 5;
const int inf = 2e9;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n;
cin>>n;
vector<int> a(n);
for (int i=0; i<n; i++) {
cin>>a[i];
}
set<int> facs;
for (int x: a) {
for (int i=1; i*i<=x; i++) {
if (x%i==0) {
facs.insert(i);
facs.insert(x/i);
}
}
}
int res = 0;
for (int f: facs) {
int cur = 0;
int lo = inf;
for (int x: a) {
if (x%f==0) {
cur = __gcd(cur, x);
} else {
lo = min(lo, x);
}
}
//cout<<f<<": "<<cur<<endl;
if (f <= lo && cur==f) {
res++;
}
}
out(res);
return 0;
}
|
// Template begins
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <stdio.h>
#include <stack>
#include<queue>
#include <map>
#include <cmath>
#include<cstring>
using namespace std;
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define ll long long
#define endl "\n"
#define lb lower_bound
#define ub upper_bound
#define fo(i,a,b) for(i=a;i<=b;i++)
#define all(v) (v).begin(),(v).end()
#define sort0(v) sort(all(v))
#define lbb(a,b,arr,x) lower_bound(arr+a,arr+b+1,x)-arr
#define ubb(a,b,arr,x) upper_bound(arr+a,arr+b+1,x)-arr
#define freq(a,b,arr,x) upper_bound(arr+a,arr+b+1,x)-lower_bound(arr+a,arr+b+1,x)
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define d0(x) cout<<(x)<<" "
#define d1(x) cout<<(x)<<endl
#define d2(x,y) cout<<(x)<<" "<<(y)<<endl
#define d3(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl
#define d4(a,b,c,d) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<endl
#define d5(a,b,c,d,e) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<" "<<(e)<<endl
#define d6(a,b,c,d,e,f) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<" "<<(e)<<" "<<(f)<<endl
#define max3(a,b,c) max(max((a),(b)),(c))
#define max4(a,b,c,d) max(max((a),(b)),max((c),(d)))
#define ss second
#define ff first
#define pb push_back
#define mp make_pair
#define printp(x) cout << x.ff << " " << x.ss << endl
template<class T> ostream& operator<<(ostream &os, vector<T> V) {
for(auto v : V) os << v << " ";
return cout << "";
}
template<class T> ostream& operator<<(ostream &os, set<T> S){
for(auto s:S) os<<s<<" ";
return cout << "";
}
template<class L, class R> ostream& operator<<(ostream &os, pair<L,R> P) {
return os << P.first << " " << P.second;
}
const ll mod=998244353;
#define inf 9999999999999
ll inv(ll i){if(i==1) return 1;return (mod-((mod/i)*inv(mod%i))%mod)%mod;}
ll gcd(ll a,ll b){ if (b==0) return a;return gcd(b,a%b);}
ll pwr(ll a, ll b) {a %= mod; ll res = 1;while (b > 0) {if (b & 1) res = res * a % mod; a = a * a % mod; b >>= 1;}return res;}
//Template ends
int main(){
fio;
ll a,b,c;
cin>>a>>b>>c;
ll ans=1;
ans*=a;
ans=(ans*b)%mod;
ans=(ans*c)%mod;
ans=(ans*(a+1))%mod;
ans=(ans*(b+1))%mod;
ans=(ans*(c+1))%mod;
ans=(ans*inv(8))%mod;
d1(ans);
}
| #include <iostream>
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll r=998244353;
int main() {
// your code goes here
ll a,b,c;
cin>>a>>b>>c;
ll a2=(a*(a+1))/2;
ll b2=(b*(b+1))/2;
ll c2=(c*(c+1))/2;;
ll ab=((a2%r)*(b2%r))%r;
ll abc=((ab%r)*(c2%r))%r;
cout<<abc;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string(string(s)); }
string to_string(bool b) { return to_string(int(b)); }
string to_string(vector<bool>::reference b) { return to_string(int(b)); }
string to_string(char b) { return "'" + string(1, b) + "'"; }
template <typename A, typename B>
string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template <typename A>
string to_string(A v) {
string res = "{";
for (const auto& x : v) res += (res == "{" ? "" : ", ") + to_string(x);
return res + "}";
}
void debug() { cerr << endl; }
template <typename Head, typename... Tail>
void debug(Head H, Tail... T) {
cerr << " " << to_string(H);
debug(T...);
}
#define db(...) cerr << "[" << #__VA_ARGS__ << "]:", debug(__VA_ARGS__)
#else
#define db(...) 42
#endif
typedef long long ll;
typedef long double ld;
const int MOD = 1000000007;
struct Mint {
int val;
Mint() { val = 0; }
Mint(const ll& x) {
val = (-MOD <= x && x < MOD) ? x : x % MOD;
if (val < 0) val += MOD;
}
template <typename U>
explicit operator U() const { return (U)val; }
friend bool operator==(const Mint& a, const Mint& b) { return a.val == b.val; }
friend bool operator!=(const Mint& a, const Mint& b) { return !(a == b); }
friend bool operator<(const Mint& a, const Mint& b) { return a.val < b.val; }
Mint& operator+=(const Mint& m) { if ((val += m.val) >= MOD) val -= MOD; return *this; }
Mint& operator-=(const Mint& m) { if ((val -= m.val) < 0) val += MOD; return *this; }
Mint& operator*=(const Mint& m) { val = (ll)val * m.val % MOD; return *this; }
friend Mint modex(Mint a, ll p) {
assert(p >= 0);
Mint ans = 1;
for (; p; p >>= 1, a *= a) if (p & 1) ans *= a;
return ans;
}
Mint& operator/=(const Mint& m) { return *this *= modex(m, MOD - 2); }
Mint& operator++() { return *this += 1; }
Mint& operator--() { return *this -= 1; }
Mint operator++(int) { Mint result(*this); *this += 1; return result; }
Mint operator--(int) { Mint result(*this); *this -= 1; return result; }
Mint operator-() const { return Mint(-val); }
friend Mint operator+(Mint a, const Mint& b) { return a += b; }
friend Mint operator-(Mint a, const Mint& b) { return a -= b; }
friend Mint operator*(Mint a, const Mint& b) { return a *= b; }
friend Mint operator/(Mint a, const Mint& b) { return a /= b; }
friend ostream& operator<<(ostream& os, const Mint& x) { return os << x.val; }
friend string to_string(const Mint& b) { return to_string(b.val); }
};
int main() {
int n;
scanf("%d", &n);
vector<string> C(2, string(2, '_'));
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
scanf(" %c", &C[i][j]);
if (C[0][1] == 'B') {
auto D = C;
int sum = (int)'A' + 'B';
D[0][0] = sum - C[1][1];
D[1][1] = sum - C[0][0];
D[0][1] = sum - C[0][1];
D[1][0] = sum - C[1][0];
swap(D, C);
}
assert(C[0][1] == 'A');
Mint ans = 0;
if (n == 2) ans = 1;
else if (C[0][0] == 'A') ans = 1;
else if (C[1][0] == 'B') ans = modex(Mint(2), n - 3);
else {
Mint a = 1, b = 0;
for (int i = 1; i + 1 < n; ++i) {
Mint na = a + b;
Mint nb = a;
swap(na, a);
swap(nb, b);
}
ans += a;
}
printf("%d\n", ans.val);
}
| #include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
int a[3][3],dp[1005][3],n;
int x = -1;
int power(int p)
{
int ans = 1;
while(p)
{
ans = ((ans%mod) * (2%mod))%mod;
p--;
}
return ans;
}
int solve(int i,int last)
{
if(i == n)
{
if(x==1 && last == 1) return 0;
else return 1;
}
if(dp[i][last] != -1) return dp[i][last];
int ans = solve(i+1,(last^1));
if(last != x) ans = (ans%mod + solve(i+1,last)%mod)%mod;
return dp[i][last] = ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int i,j;
char c;
cin >> n;
for(i=0;i<=n;i++) dp[i][0] = dp[i][1] = -1;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cin >> c;
a[i][j] = c - 'A';
}
}
if(n<=3)
{
cout << 1 << endl;
return 0;
}
if(a[0][1] == 0)
{
if(a[0][0]==0)
{
cout << 1 << endl;
}
else
{
if(a[1][0] == 1)
{
cout << power(n-3) << endl;
}
else
{
x = 1;
n -= 2;
int ans = (solve(1,0)%mod + solve(1,1)%mod)%mod;
cout << ans << endl;
}
}
}
else
{
if(a[1][1] == 1)
{
cout << 1 << endl;
}
else
{
if(a[1][0] == 0)
{
cout << power(n-3) << endl;
}
else
{
x = 0;
n -= 2;
int ans = solve(1,1)%mod;
cout << ans << endl;
}
}
}
}
|
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <string>
#include <cassert>
using namespace std;
typedef long long llint;
const int INF = 0x3f3f3f3f;
const llint INFLL = 0x3f3f3f3f3f3f3f3fLL;
void print() { cout << "\n"; }
template <typename...T, typename X>
void print(X&& x, T... args) { cout << x << " "; print(args...); }
int input() { return 0; }
template <typename...T, typename X>
int input(X& x, T&... args) {
if (!(cin >> x)) return 0;
return input(args...) + 1;
}
const llint MOD = 998244353;
const int N = 5555;
int n, m, k;
char grid[N][N];
int dp[2][N];
int xs[N][N];
int ys[N][N];
llint mypow(llint a, llint b, llint c) {
llint res = 1;
while (b) {
if (b & 1) {
res *= a;
res %= c;
}
b >>= 1;
a = (a * a) % c;
}
return res;
}
int main() {
input(n, m, k);
memset(grid, 0, sizeof(grid));
int a, b;
char c[10];
for (int i = 0; i < k; i++) {
scanf("%d%d%s", &a, &b, c);
grid[a][b] = c[0];
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
xs[i][j] += xs[i][j - 1] + (grid[i][j] == '\0'? 1: 0);
}
}
for (int j = 1; j <= m; j++) {
for (int i = 1; i <= n; i++) {
ys[i][j] += ys[i - 1][j] + (grid[i][j] == '\0'? 1: 0);
}
}
int p = 1;
memset(dp, 0, sizeof(dp));
dp[1][1] = 1;
for (int i = 1; i <= n; i++) {
int q = p ^ 1;
for (int j = 1; j <= m; j++) {
if (i == 1 && j == 1) {
continue;
}
llint cur = 0;
if (i - 1 > 0) {
llint a = 0;
if (grid[i - 1][j] == 'D' || grid[i - 1][j] == 'X') {
a += dp[q][j];
} else if (grid[i - 1][j] == '\0') {
a += dp[q][j] * 2;
}
a *= mypow(3, xs[i][j - 1], MOD);
cur += a;
}
if (j - 1 > 0) {
llint b = 0;
if (grid[i][j - 1] == 'R' || grid[i][j - 1] == 'X') {
b += dp[p][j - 1];
} else if (grid[i][j - 1] == '\0') {
b += dp[p][j - 1] * 2;
}
b *= mypow(3, ys[i - 1][j], MOD);
cur += b;
}
dp[p][j] = cur % MOD;
}
/*
for (int j = 1; j <= m; j++) {
printf("%lld ", dp[p][j]);
}
puts("");
*/
p = q;
}
llint res = dp[p ^ 1][m];
if (grid[n][m] == '\0') {
res = res * 3 % MOD;
}
print(res);
return 0;
}
/*
^^^TEST^^^
2 2 3
1 1 X
2 1 R
2 2 R
---
5
$$$TEST$$$
^^^TEST^^^
3 3 5
2 3 D
1 3 D
2 1 D
1 2 X
3 1 R
-----
150
$$$TEST$$$
^^^TEST^^^
5000 5000 10
585 1323 R
2633 3788 X
1222 4989 D
1456 4841 X
2115 3191 R
2120 4450 X
4325 2864 X
222 3205 D
2134 2388 X
2262 3565 R
----
139923295
$$$TEST$$$
*/
| #include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <algorithm>
#include <math.h>
#include <iomanip>
#include <sstream>
#include <map>
#define INF 100000000
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using p = pair<int, int>;
vector<vector<string>> m(5004,vector<string>(5004,"b"));
vector<vector<ll>> dp(5004,vector<ll>(5004,0));
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;
}
int main() {
ll h,w,k;
ll mod = 998244353;
cin >> h >> w >> k;
rep(i,k){
ll a, b;
string c;
cin >> a >> b >> c;
a--; b--;
m[a][b] = c;
}
ll f = 1;
ll mod3 = modinv(3, mod);
for(ll i = 0; i < h*w-k;i++) f = (f * 3)%mod;
dp[0][0] = f%mod;
rep(i,h){
rep(j,w){
if(m[i][j] == "b"){
dp[i][j+1] = (dp[i][j+1] + dp[i][j]*mod3) % mod;
dp[i+1][j] = (dp[i+1][j] + dp[i][j]*mod3) % mod;
dp[i][j+1] = (dp[i][j+1] + dp[i][j]*mod3) % mod;
dp[i+1][j] = (dp[i+1][j] + dp[i][j]*mod3) % mod;
}else if(m[i][j] == "D"){
dp[i+1][j] = (dp[i+1][j] + dp[i][j]) % mod;
}else if(m[i][j] == "X"){
dp[i][j+1] = (dp[i][j+1] + dp[i][j]) % mod;
dp[i+1][j] = (dp[i+1][j] + dp[i][j]) % mod;
}else{
dp[i][j+1] = (dp[i][j+1] + dp[i][j]) % mod;
}
}
}
cout << dp[h-1][w-1] << endl;
return 0;
} |
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <iomanip>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cassert>
#include <complex>
#include <stdio.h>
#include <time.h>
#include <numeric>
#include <random>
#include <unordered_set>
#include <unordered_map>
#define all(a) a.begin(), a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rrep(i, n) for (ll i = (n) - 1; i >= 0; i--)
#define range(i, a, b) for (ll i = (a); i < (b); i++)
#define rrange(i, a, b) for (ll i = (b) - 1; i >= (a); i--)
#define pb push_back
#define debug(x) cerr << __LINE__ << ' ' << #x << ':' << (x) << '\n'
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef complex<ld> com;
template<class T> using pri_s = priority_queue<T, vector<T>, greater<T>>;
template<class T> using pri_b = priority_queue<T>;
constexpr int inf = 1000000010;
constexpr ll INF = 1000000000000000010;
constexpr int mod1e9 = 1000000007;
constexpr int mod998 = 998244353;
constexpr ld eps = 1e-12;
constexpr ld pi = 3.141592653589793238;
constexpr ll ten(int n) { return n ? 10 * ten(n - 1) : 1; };
int dx[] = { 1,0,-1,0,1,1,-1,-1 }; int dy[] = { 0,1,0,-1,1,-1,1,-1 };
void fail() { cout << "-1\n"; exit(0); } void no() { cout << "No\n"; exit(0); }
template<class T> void er(T a) { cout << a << '\n'; exit(0); }
template<class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; }
template<class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; }
template<class T> istream &operator >> (istream &s, vector<T> &v) { for (auto &e : v) s >> e; return s; }
template<class T> ostream &operator << (ostream &s, const vector<T> &v) { for (auto &e : v) s << e << ' '; return s; }
template<class T, class U> ostream &operator << (ostream &s, const pair<T, U> &p) { s << p.first << ' ' << p.second; return s; }
struct fastio {
fastio() {
cin.tie(0); cout.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
cerr << fixed << setprecision(20);
}
}fastio_;
const int N = ten(5) + 10;
vector<vector<int>> graph(N, vector<int>());
vector<int> sz(N, 1), dp(N, 1);
void dfs(int n) {
vector<P> p;
for (int i : graph[n]) {
dfs(i);
sz[n] += sz[i];
p.pb({ sz[i] - 2 * dp[i],sz[i] % 2 });
dp[n] += sz[i] - dp[i];
}
sort(all(p), greater<P>());
int t = 0; int sum = 0;
for (auto q : p) {
if (q.first > 0 && q.second == 0) {
dp[n] -= q.first;
}
if (q.second == 1) {
if (t == 0) {
dp[n] -= q.first;
}
t ^= 1;
}
if (q.first < 0 && q.second == 0) {
sum += q.first;
}
}
if (t == 0) dp[n] -= sum;
}
int main() {
int n;
cin >> n;
for (int i = 1; i < n; i++) {
int v;
cin >> v;
v--;
graph[v].pb(i);
}
dfs(0);
cout << dp[0] << '\n';
} | //#include <bits/stdc++.h>
//using namespace std;
//typedef long long ll;
//typedef pair<int, int> pi;
//typedef pair<ll, ll> pll;
//
//vector<int> V[100001], odd[100001];
//
//int f[100001], sz[100001];
//
//void dfs(int pos) {
// f[pos] = -1, sz[pos] = 1;
//
// int sum = 0;
// for (int w : V[pos]) {
// dfs(w); sz[pos] += sz[w];
// if (sz[w] & 1) odd[pos].push_back(w);
// else {
// if (f[w] >= 0) f[pos] += f[w];
// else sum += f[w];
// }
// }
// sort(odd[pos].begin(), odd[pos].end(), [&](int a, int b) -> bool {
// return f[a] > f[b];
// });
// bool aoki = false;
// for (int w : odd[pos]) {
// f[pos] += (aoki ? -1 : 1) * f[w];
// aoki = !aoki;
// }
//
// if (odd[pos].size() & 1) f[pos] -= sum;
// else f[pos] += sum;
//}
//
//int main() {
// ios::sync_with_stdio(0);
// cin.tie(0), cout.tie(0);
//
// int n; cin >> n;
// for (int i = 2; i <= n; i++) {
// int a; cin >> a;
// V[a].push_back(i);
// }
// cout << (n - f[1]) / 2;
//
// return 0;
//}
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, fa[N], siz[N], f[N];
vector <int> g[N];
void dfs_(int u) {
f[u] = -1, siz[u] = 1;
int tot = 0;
vector<int> odd;
for (int v : g[u]) {
dfs_(v), siz[u] += siz[v];
if (siz[v] & 1) odd.push_back(v);
else {
if (f[v] >= 0) f[u] += f[v];
else tot += f[v];
}
}
sort(odd.begin(), odd.end(), [&](int a, int b) -> bool {
return f[a] > f[b];
});
bool aoki = false;
for (int w : odd) {
f[u] += (aoki ? -1 : 1) * f[w];
aoki = !aoki;
}
if (odd.size() & 1)
f[u] -= tot;
else
f[u] += tot;
}
int main() {
scanf("%d", &n);
for (int i = 2; i <= n; i++)
scanf("%d", &fa[i]), g[fa[i]].push_back(i);
dfs_(1);
printf("%d\n", (n - f[1]) / 2);
}
|
#include <bits/stdc++.h>
#define int long long
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
const int N = 55, MOD = 998244353;
int dsu[N], sz[N];
int find(int u){
return dsu[u]==u ? u : dsu[u] = find(dsu[u]);
}
void unite(int u, int v){
u = find(u), v = find(v);
if(u==v) return;
if(sz[u] > sz[v]) swap(u,v);
dsu[u] = v;
sz[v] += sz[u];
}
signed main(){
fastio
int n,k;
cin >> n >> k;
iota(dsu,dsu+N,0);
fill(sz,sz+N,1);
int arr[n][n];
for(int i = 0;i < n;i++) for(int j = 0;j < n;j++) cin >> arr[i][j];
int ans = 1;
for(int i = 0;i < n;i++){
for(int j = i+1;j < n;j++){
bool ok = true;
for(int l = 0;l < n;l++){
if(arr[i][l]+arr[j][l] > k) ok = false;
}
if(ok){
unite(i,j);
}
}
}
for(int i = 0;i < n;i++) if(dsu[i] == i) for(int j = 1;j <= sz[i];j++) ans = ans*j%MOD;
iota(dsu,dsu+N,0);
fill(sz,sz+N,1);
for(int i = 0;i < n;i++){
for(int j = i+1;j < n;j++){
bool ok = true;
for(int l = 0;l < n;l++){
if(arr[l][i]+arr[l][j] > k) ok = false;
}
if(ok) unite(i,j);
}
}
for(int i = 0;i < n;i++) if(dsu[i] == i) for(int j = 1;j <= sz[i];j++) ans = ans*j%MOD;
cout << ans << "\n";
} | #include <bits/stdc++.h>
using namespace std;
#define all(hoge) (hoge).begin(), (hoge).end()
#define en '\n'
using ll = long long;
using ull = unsigned long long;
#define rep(i, m, n) for(ll i = (ll)(m); i < (ll)(n); ++i)
#define rep2(i, m, n) for(ll i = (ll)(n)-1; i >= (ll)(m); --i)
#define REP(i, n) rep(i, 0, n)
#define REP2(i, n) rep2(i, 0, n)
template<class T> using vec = vector<T>;
template<class T> using vvec = vector<vec<T>>;
typedef pair<ll, ll> P;
using tp = tuple<ll, ll, ll>;
constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;
constexpr long long MOD = (ll) 1e9 + 7;
//constexpr long long MOD = 998244353LL;
using ld = long double;
static const ld pi = 3.141592653589793L;
using Array = vector<ll>;
using Matrix = vector<Array>;
/*
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
*/
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, cap, rev;
Edge(ll _to, ll _cap, ll _rev) {
to = _to;
cap = _cap;
rev = _rev;
}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
void add_edge(Graph &G, ll from, ll to, ll cap, bool revFlag, ll revCap) {
G[from].push_back(Edge(to, cap, (ll) G[to].size()));
if (revFlag)
G[to].push_back(Edge(from, revCap, (ll) G[from].size() - 1));
}
map<ll, ll> mp;
ll rec(ll x) {
if (mp.find(x) != mp.end()) return mp[x];
ll ret = INF;
if ((ll) 2e9 + 10 >= x) chmin(ret, x * 1);
chmin(ret, rec(x / 2) + 1 + x % 2 * 1);
if ((x + 1) / 2 != x) chmin(ret, rec((x + 1) / 2) + 1 + x % 2 * 1);
return mp[x] = ret;
}
void solve() {
ll x, y;
cin >> x >> y;
if (x >= y) {
cout << x - y << en;
}else{
priority_queue<P, vec<P>, greater<P>> que;
que.push({0, y});
ll ans = INF;
map<ll, ll> mp;
mp[y]++;
while (que.size()) {
auto[num,val] = que.top();
que.pop();
if (num > ans) continue;
if(val<=x){
chmin(ans, num+x-val);
}else{
chmin(ans,num+val-x);
}
if (val % 2 == 1) {
if (!mp.count(val + 1) and ans > num + 1) {
mp[val + 1]++;
que.push({num + 1, val + 1});
}
if (!mp.count(val - 1) and ans > num + 1) {
mp[val - 1]++;
que.push({num + 1, val - 1});
}
}else{
if(mp.count(val/2) or ans <= num+1) continue;
que.push({num+1,val/2});
}
}
cout<<ans<<en;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
/*
ll t;
cin >> t;
REP(i, t - 1) {
solve();
}*/
solve();
return 0;
}
|
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define vl vector<ll>
#define vs vector<string>
#define mll map<ll,ll>
#define pll pair<ll,ll>
#define sl set<ll>
#define pql priority_queue<ll>
#define rep(i,n) for(i=0;i<n;i++)
#define repr(i,n) for(i=n-1;i>=0;i--)
#define REP(i,a,b) for(i=a;i<b;i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(), a.rend()
#define vsort(a) sort(all(a))
#define rvsort(a) sort(rall(a))
#define F first
#define S second
#define pi acos(-1)
#define inf 10000000000000000
#define mod 1000000007
#define MA LLONG_MAX
#define MI LLONG_MIN
long long power(long long x,ll y)
{
long long res = 1;
x = x % mod;
while (y > 0)
{
if (y & 1)
res = (res * x) % mod;
y = y >> 1;
x = (x * x) % mod;
}
return res;
}
long modInverse(long long n)
{
return power(n, mod - 2);
}
long long ncr(long long n, ll r)
{
if (n < r)
return 0;
if (r == 0)
return 1;
long long fac[n + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = (fac[i - 1] * i) % mod;
return (fac[n] * modInverse(fac[r]) % mod*modInverse(fac[n - r])% mod)% mod;
}
void solve()
{
ll n,i,ans=0;
cin>>n;
ll sum=0;
vl a(n);
rep(i,n)
cin>>a[i];
ordered_set s;
for(i=0;i<n;i++)
{
ans+=s.size()-s.order_of_key(a[i]);
s.insert(a[i]);
}
cout<<ans<<endl;
for(i=0;i<n-1;i++)
{
ans+=(n-a[i]-1);
ans-=a[i];
cout<<ans<<endl;
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
// #ifdef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
int t=1;
//cin>>t;
while (t--) {
solve();
}
return 0;
} | #include <bits/stdc++.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define vb vector<bool>
#define vpi vetor<pii>
#define vpl vector<pll>
#define pb push_back
#define all(v) v.begin(),v.end()
#define fast_io ios_base::sync_with_stdio(false),cin.tie(NULL);
void cout_vector(vi v){
for (ll unsigned i=0; i<v.size(); i++) cout << v[i] << " ";
cout<<endl;
}
int sum_vector(vi v){
return accumulate(all(v),0);
}
void sort_vector(vi&v){
sort(all(v));
}
void sort_comp(vi &v, bool func(int,int)){
sort(all(v),func);
}
bool comp(array<int,2> a,array<int,2> b){
return a[0]+a[1]<b[0]+b[1];
}
ll gcd(ll a, ll b){
while(a){
ll temp =a;
a=b%a;
b=temp;
}
return b;
}
ll lcm(ll a, ll b){
return (a*b)/gcd(a,b);
}
string binary(ll num){
string ans="";
do{
ans=to_string(num%2)+ans;
num/=2;
}while(num);
return ans;
}
const int mxn = 2e3+7;
const ll mod = 1e9+7;
ll pwr(ll num, ll p, ll modi){
ll res=1;
while(p>0){
if(p&1){res*=num; res%=modi;}
num*=num;
num%=modi;
p/=2;
}
return res;
}
ll inverse(ll num){
return pwr(num,mod-2,mod);
}
void solve(){
int n;
cin>>n;
string a,b;
cin>>a;
for(int i=0; i<n;i++){
if(b.size()<=1) b+=a.at(i);
else{
int bs=b.size();
if(b.at(bs-2)=='f' && b.at(bs-1)=='o' && a.at(i)=='x'){
b.pop_back();
b.pop_back();
}
else{
b+=a.at(i);
}
}
}
cout<<b.size();
}
int main()
{
fast_io
int t;
//cin>>t;
t=1;
while(t--) solve();
return 0;
}
|
/*~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
*$* WRITER:kakitamasziru/OxOmisosiru *$*
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=*/
#ifdef LOCAL_JUDGE
#define _GLIBCXX_DEBUG
#endif
#include <stdio.h>
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <iomanip> //setprecision
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set,multiset
#include <stack> // stack
#include <deque> // deque
#include <math.h>//pow,,,
#include <cmath>//abs,,,
#include <bitset> // bitset
#include <numeric> //accumulate,,,
#include <time.h> //clock
#define endl "\n";
using namespace std;
using PLL = pair<long long,long long>;
using P = pair<int,int>;
const long long INF = 3000000000000000001;
const int inf = 1001001001;
long long MOD = 1000000007;
//Solve N^M. This, mod_pow use Iterative Square Method.
long long mod_pow(long long N, long long M, long long mod) {
if (M == 0) return 1;
long long res = mod_pow((N * N) % mod, M / 2,mod);
//When end-of-a bit is 1, times simple N.
if (M & 1) res = (res * N) % mod;
return res %= mod;
}
long long gcd(long long a, long long b) {
if (b == 0) return a; else return gcd(b, a % b);
}
long long lcm(long long a, long long b) {
return a / gcd(a, b) * b ;
}
long long get_mod(long long res){
if(res < 0) res += MOD;
return res % MOD;
}
int main() {
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int N;cin >> N;
string S,T;cin >> S >> T;
set<int> index1;
for(int i = 0;i<N;i++){
if(S.at(i) == '1') index1.insert(i);
}
long long ans = 0;
for(int i = 0;i<N;i++){
if(S.at(i) != T.at(i)){
auto itr = index1.upper_bound(i);
if(itr == index1.end()){
cout << -1 << endl;
return 0;
}
int near1 = *index1.upper_bound(i);
S.at(near1) = '0';
S.at(i) = T.at(i);
ans += near1-i;
//cout << i << " " << near1 << " " << ans << endl;
index1.erase(near1);
}
}
cout << ans << endl;
} | #include<bits/stdc++.h>
using namespace std;
int main(){
int N, A, B;
cin >> N >> A >> B;
cout << N - A + B << endl;
return 0;
} |
#include<iostream>
#include<math.h>
#include<string>
#include<stack>
using namespace std;
int solvea(){
long long int ans=0;
long long int s;
cin>>s;
for(long long int i=1LL;i<=s;i++){
long long int hoge=0LL;
for(long long int j=1LL;j*j*i<=s;j++){
long long int temp=(s/(i*j));
hoge+=temp;
}
long long int sq=sqrt(s/i);
ans+=(2LL*hoge-sq*sq);
}
cout<<ans<<endl;
return 0;
}
int solveb(){
long long int a,b,c;
cin>>a>>b>>c;
int table[10][4]=
{{0,0,0,0},
{1,1,1,1},
{6,2,4,8},
{1,3,9,7},
{6,4,6,4},
{5,5,5,5},
{6,6,6,6},
{1,7,9,3},
{6,8,4,2},
{1,9,1,9}};
a=a%10LL;
b=b%4LL;
if(b==0LL){
cout<<table[a][0]<<endl;
}
else if(b==2LL){
if(c>1){
cout<<table[a][0]<<endl;
}
else{
cout<<table[a][2]<<endl;
}
}
else{
if(c%2==0LL){
cout<<table[a][1]<<endl;
}
else{
cout<<table[a][b]<<endl;
}
}
return 0;
}
class letter{
public:
long long int itr;
char ch;
void print(){
cout<<itr<<" "<<ch<<endl;
}
};
int main(){
string text;
cin>>text;
stack<letter> hoge;
long long int ruiseki[26][200002]={};
ruiseki[int(text[0]-97)][0]++;
for(int i=0;i<26;i++){
for(int j=1;j<text.size();j++){
if(int(text[j])==i+97){
ruiseki[i][j]=ruiseki[i][j-1]+1;
}
else{
ruiseki[i][j]=ruiseki[i][j-1];
}
}
ruiseki[i][text.size()]=ruiseki[i][text.size()-1];
}
for(int i=0;i<text.size()-1;i++){
if(text[i]==text[i+1]){
letter temp;
temp.itr=i;
temp.ch=text[i];
hoge.push(temp);
}
}
letter before;
before.itr=text.size();
before.ch='9';
long long int ans=0LL;
while(!hoge.empty()){
letter now=hoge.top();
hoge.pop();
//now.print();
//cout<<"ans="<<ans<<endl;
if(before.itr!=now.itr+1){
if(now.ch!=before.ch){
ans+=(text.size()-before.itr);
}
ans+=((before.itr-now.itr-1)-(ruiseki[int(now.ch)-97][before.itr-1]-ruiseki[int(now.ch)-97][now.itr]));
}
before=now;
}
cout<<ans<<endl;
return 0;
}
| #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <iomanip>
#include <climits>
#include <functional>
#include <cassert>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<string> VS;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<PII> VPI;
typedef vector<PLL> VPL;
#define rep(i,n) for(ll i=0;i<(n);i++)
#define all(a) (a).begin(),(a).end()
#define pf push_front
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ub upper_bound
#define lb lower_bound
#define fi first
#define se second
void YES(int a){
if(a) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
void Yes(int a){
if(a) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
//#include <atcoder/all>
//using namespace atcoder;
//typedef modint998244353 mint;
int main(){
string S;
cin>>S;
int N=S.size();
VVI X(26,VI(N+1));
reverse(all(S));
rep(i,N) X[S[i]-'a'][i+1]++;
rep(i,26) rep(j,N+1){
if(j) X[i][j]+=X[i][j-1];
}
reverse(all(S));
VPI P(0);
rep(i,N-1){
if(S[i]==S[i+1]) P.pb(mp(S[i]-'a',i));
}
int tmp=-1;
VPI Q(0);
rep(i,P.size()){
if(tmp!=P[i].fi){
Q.pb(P[i]);
tmp=P[i].fi;
}
}
reverse(all(Q));
ll ans=0;
//for(auto e: Q) cout<<e.fi<<" "<<e.se<<endl;
rep(i,Q.size()){
if(!i){
ans+=N-2-Q[i].se;
ans+=2-(X[Q[i].fi][N-Q[i].se]-X[Q[i].fi][0]);
}
else{
ans+=N-2-Q[i].se;
//cout<<ans<<endl;
ans+=2-(X[Q[i].fi][N-Q[i].se]-X[Q[i].fi][N-Q[i-1].se]);
}
//cout<<(X[Q[i].fi][N-Q[i].se]-X[Q[i-1].fi][0])<<" "<<ans<<endl;
}
cout<<ans<<endl;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define mp(a, b) make_pair(a, b)
const int maxn = 1000 + 10;
const ll mod = 1e9 + 7;
char s[4][10];
int tr[4];
ll f[maxn], g[maxn];
ll powMod(ll a, ll k)
{
ll ret = 1;
while(k > 0)
{
if(k & 1) ret = ret * a % mod;
a = a * a % mod; k >>= 1;
}
return ret;
}
int main()
{
int n;
scanf("%d", &n);
g[1] = g[0] = f[0] = 1;
for(int i = 2; i <= n; ++i)
{
f[i] = g[i - 2];
g[i] = (g[i - 1] + f[i]) % mod;
}
for(int i = 0; i < 4; ++i)
{
scanf("%s", s[i]);
tr[i] = s[i][0] - 'A';
}
if(tr[1] == 0)
{
if(tr[0] == 0) puts("1");
else
{
if(tr[2] == 0)
printf("%lld\n", f[n]);
else
printf("%lld\n", powMod(2, n - 3));
}
}
else
{
if(tr[3] == 1) puts("1");
else
{
if(tr[2] == 1)
printf("%lld\n", f[n]);
else
printf("%lld\n", powMod(2, n - 3));
}
}
return 0;
} | #include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 1e9+7;
int main(){
int N; cin >> N;
vector<ll> A(N), sum(N+1);
for(int i=0; i<N; i++){
cin >> A[i];
sum[i+1] = sum[i]+A[i];
}
vector<vector<int>> dp(N+2,vector<int>(N+2));
dp[1][0]++;
int ans = 0;
for(int i=0; i<N; i++){
for(int j=N; 1<=j; j--){
dp[j+1][sum[i+1]%(j+1)] += dp[j][sum[i+1]%j];
dp[j+1][sum[i+1]%(j+1)] %= mod;
if(i == N-1){
ans += dp[j][sum[i+1]%j];
ans %= mod;
}
}
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/priority_queue.hpp>
//#include <ext/pb_ds/hash_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
//typedef gp_hash_table<int, int> hashmap;
//typedef tree<pii, null_type, std::less<pii>, splay_tree_tag, tree_order_statistics_node_update> splaytree;
//typedef tree<pii, null_type, std::less<pii>, rb_tree_tag, tree_order_statistics_node_update> rbtree;
//typedef __gnu_pbds::priority_queue<int, std::greater<int>, __gnu_pbds::binary_heap_tag> binheap;
//typedef __gnu_pbds::priority_queue<int, std::greater<int>, __gnu_pbds::pairing_heap_tag> pairingheap;
template <typename T>
inline void read(T &x)
{
T data = 0, f = 1;
char ch = getchar();
while (!isdigit(ch))
{
if (ch == '-')
f = -1;
ch = getchar();
}
while (isdigit(ch))
{
data = (data << 3) + (data << 1) + ch - '0';
ch = getchar();
}
x = f * data;
}
template <typename T, typename... Args>
inline void read(T &t, Args &...args)
{
read(t);
read(args...);
}
//mt19937 rnd(time(0));
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const int maxn = 2e5 + 9;
const ll mod = 998244353;
signed main()
{
//freopen("in.txt","r",stdin);
//freopen("data.txt","w",stdout);
//std::ios::sync_with_stdio(false);
//std::cin.tie(0);
//std::cout.tie(0);
ll n;
cin >> n;
cout << (100 - (n % 100)) << endl;
//fclose(stdin);
//fclose(stdout);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define sp(x) cout<< setprecision(x);
#define all(a) (a).begin(), (a).end()
#define inf INT64_MAX*0.99
#define pi 3.141592653589793238;
#define int long long
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
typedef pair<int, int> P;
signed main(){
int n; cin>>n;
int cnt = 0;
REP(i,n){
int in;
cin>>in;
if(in>10) cnt+=in-10;
}
cout<<cnt<<endl;
return 0;
} |
// Problem: A - Three Dice
// Contest: AtCoder - AISing Programming Contest 2021(AtCoder Beginner Contest 202)
// URL: https://atcoder.jp/contests/abc202/tasks/abc202_a
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// Author: abhidot
#include <bits/stdc++.h>
#define int long long
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define mod 1000000007 //998244353
#define lld long double
#define pii pair<int, int>
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define rep(i,x,y) for(int i=x; i<y; i++)
#define fill(a,b) memset(a, b, sizeof(a))
#define vi vector<int>
#define setbits(x) __builtin_popcountll(x)
#define w(x) int x; cin>>x; while(x--)
using namespace std;
const long long N=200005, INF=2000000000000000000;
/*------------- Modular exponentiation -------------*/
int power(int a, int b, int p)
{
if(a==0)
return 0;
int res=1;
a%=p;
while(b>0)
{
if(b&1)
res=(res*a)%p;
b>>=1;
a=(a*a)%p;
}
return res;
}
/*------------- Sieve --------------*/
vector <int> prime;
bool is_composite[N];
void sieve (int n) {
fill (is_composite, false);
for (int i = 2; i < n; ++i) {
if (!is_composite[i]) prime.push_back (i);
for (int j = 0; j < prime.size () && i * prime[j] < n; ++j) {
is_composite[i * prime[j]] = true;
if (i % prime[j] == 0) break;
}
}
}
/*------------ Utitlity function ---------------*/
void print(bool n){
if(n){
cout<<"YES";
}else{
cout<<"NO";
}
}
int32_t main()
{
IOS;
int a,b,c;
cin>>a>>b>>c;
cout<<21-a-b-c;
}
// Written by abhidot | #include <bits/stdc++.h>
using namespace std;
using i128 = __int128_t;
using u128 = __uint128_t;
using u64 = uint64_t;
//define
#define int long long
#define ll int
#define trav(i,v) for(auto i: v)
#define rep(i,n) for(int i=0;i<n;i++)
#define repu(i,k,n) for(int i=k;i<=n;i++)
#define repd(i,k,n) for(int i=k;i>=n;i--)
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define itn int
#define sz(x) (int)x.size()
//typedef
typedef unsigned long long ull;
typedef long double ld;
typedef std::pair<int, int> pii;
typedef std::vector<int> vi;
typedef std::vector< pii > vii;
typedef std::vector< std::vector<int> > vvi;
typedef std::vector< std::pair < pii, int > > vpp;
// const long long MOD = 1000000007;
const long long MOD = 998244353;
const long double PI = 3.141592653589793238;
const long long pi = 31415926;
const long long inf = 1000000000000000000;
const long long small_inf = INT_MAX;
int modpow(int x, int n, int mod = MOD){ int res=1; while(n>0){ if(n&1) res=res*x%mod; x=x*x%MOD; n>>=1;} return res;}
int power(int x, int n){ int res=1; while(n>0){ if(n&1) res=res*x; x=x*x; n>>=1; } return res;}
void init(){
}
void solve()
{
/*Don't hurry, nothing good comes if you rush*/
int a, b, c;
cin>>a>>b>>c;
a-=7, b-=7, c-=7;
cout<<abs(a+b+c)<<endl;
}
signed main(){
// #ifndef ONLINE_JUDGE
// freopen("./input.txt", "r", stdin);
// freopen("./output.txt", "w", stdout);
// #endif
ios_base::sync_with_stdio(0);
cin.tie(NULL);
int t23 = 1, tt23 = 1;
// cin>>t23;
init();
while(tt23<=t23)
{
// cout<<"Case #"<<tt23<<": ";
solve();
tt23++;
}
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
} |
#include<bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &x){
x=0;char ch=getchar();bool f=false;
while(!isdigit(ch)){if(ch=='-'){f=true;}ch=getchar();}
while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
x=f?-x:x;
return ;
}
template <typename T>
inline void write(T x){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10^48);
return ;
}
#define ll long long
const int N=2e5+5,M=1e8;
int n,m,q;
ll a[N],b[N];
struct SGTree{
int cur,root;
ll sum[N*80];
int num[N*80],ls[N*80],rs[N*80];
void Modify(int &x,int l,int r,int pos,int v,int tp){
if(!x) x=++cur;
int mid=l+r>>1;sum[x]+=v*tp;num[x]+=tp;
if(l==r) return ;
if(pos<=mid) Modify(ls[x],l,mid,pos,v,tp);
else Modify(rs[x],mid+1,r,pos,v,tp);
return ;
}
ll QuerySum(int x,int l,int r,int ql,int qr){
if(ql>qr||!x) return 0;
if(ql<=l&&r<=qr) return sum[x];
int mid=l+r>>1;ll res=0;
if(ql<=mid) res+=QuerySum(ls[x],l,mid,ql,qr);
if(qr>mid) res+=QuerySum(rs[x],mid+1,r,ql,qr);
return res;
}
int QueryNum(int x,int l,int r,int ql,int qr){
if(ql>qr||!x) return 0;
if(ql<=l&&r<=qr) return num[x];
int mid=l+r>>1,res=0;
if(ql<=mid) res+=QueryNum(ls[x],l,mid,ql,qr);
if(qr>mid) res+=QueryNum(rs[x],mid+1,r,ql,qr);
return res;
}
}t1,t2;
ll Ans;
int main(){
read(n),read(m),read(q);
for(int i=1;i<=n;i++) t1.Modify(t1.root,0,M,a[i],a[i],1);
for(int i=1;i<=m;i++) t2.Modify(t2.root,0,M,b[i],b[i],1);
// for(int i=1;i<=n;i++){
// Ans+=1ll*t2.QueryNum(t2.root,0,M,1,a[i])*a[i];
// Ans+=1ll*t2.QuerySum(t2.root,0,M,a[i]+1,M);
// }
for(int i=1;i<=q;i++){
int op,pos;ll v;
read(op),read(pos),read(v);
if(op==1){
Ans-=1ll*t2.QueryNum(t2.root,0,M,0,a[pos])*a[pos];
Ans-=1ll*t2.QuerySum(t2.root,0,M,a[pos]+1,M);
t1.Modify(t1.root,0,M,a[pos],a[pos],-1);
a[pos]=v;
Ans+=1ll*t2.QueryNum(t2.root,0,M,0,a[pos])*a[pos];
Ans+=1ll*t2.QuerySum(t2.root,0,M,a[pos]+1,M);
t1.Modify(t1.root,0,M,a[pos],a[pos],1);
}
else{
Ans-=1ll*t1.QueryNum(t1.root,0,M,0,b[pos])*b[pos];
Ans-=1ll*t1.QuerySum(t1.root,0,M,b[pos]+1,M);
t2.Modify(t2.root,0,M,b[pos],b[pos],-1);
b[pos]=v;
Ans+=1ll*t1.QueryNum(t1.root,0,M,0,b[pos])*b[pos];
Ans+=1ll*t1.QuerySum(t1.root,0,M,b[pos]+1,M);
t2.Modify(t2.root,0,M,b[pos],b[pos],1);
}
write(Ans),putchar('\n');
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <typename T>
class fenwick {
private:
vector <T> sum;
vector <T> cnt;
int size;
public:
fenwick(int _n) : size(_n) {
sum.assign(size + 1, 0);
cnt.assign(size + 1, 0);
}
void upd_sum(int i, T v) {
for(; i <= size; i += i&-i) {
sum[i] += v;
}
}
void upd_cnt(int i, T v) {
for(; i <= size; i += i&-i) {
cnt[i] += v;
}
}
T qry_sum(int i) {
T curr = 0;
for(; i; i -= i&-i) {
curr += sum[i];
}
return curr;
}
T qry_cnt(int i) {
T curr = 0;
for(; i; i -= i&-i) {
curr += cnt[i];
}
return curr;
}
};
const int maxN = 2e5 + 5;
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int n, m, q;
cin >> n >> m >> q;
vector <tuple<int, int, int>> queries;
vector <int> order = {0};
for(int i = 0; i < q; i++) {
int t, x, y;
cin >> t >> x >> y; --x;
queries.emplace_back(t, x, y);
order.push_back(y);
}
order.erase(unique(order.begin(), order.end()), order.end());
sort(order.begin(), order.end());
vector <int> A(n, 0), B(m, 0);
fenwick <ll> ft_A(maxN), ft_B(maxN);
ft_A.upd_cnt(1, n); ft_B.upd_cnt(1, m);
ll ans = 0;
for(auto &[t, x, y] : queries) {
int pos = lower_bound(order.begin(), order.end(), y) - order.begin() + 1;
assert(order[pos - 1] == y);
if(t == 1) {
int prev = lower_bound(order.begin(), order.end(), A[x]) - order.begin() + 1;
assert(order[prev - 1] == A[x]);
ans -= 1LL * ft_B.qry_cnt(prev - 1) * A[x];
ans -= (ft_B.qry_sum(pos - 1) - ft_B.qry_sum(prev - 1));
ans += 1LL * ft_B.qry_cnt(pos - 1) * y;
ft_A.upd_cnt(prev, -1); ft_A.upd_cnt(pos, 1);
ft_A.upd_sum(prev, -A[x]); ft_A.upd_sum(pos, y);
A[x] = y;
}
else {
int prev = lower_bound(order.begin(), order.end(), B[x]) - order.begin() + 1;
assert(order[prev - 1] == B[x]);
ans -= 1LL * ft_A.qry_cnt(prev - 1) * B[x];
ans -= (ft_A.qry_sum(pos - 1) - ft_A.qry_sum(prev - 1));
ans += 1LL * ft_A.qry_cnt(pos - 1) * y;
ft_B.upd_cnt(prev, -1); ft_B.upd_cnt(pos, 1);
ft_B.upd_sum(prev, -B[x]); ft_B.upd_sum(pos, y);
B[x] = y;
}
cout << ans << '\n';
}
} |
#include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long ll;
typedef pair<ll,ll> P;
const ll mod=1000000007;
const ll LINF=1LL<<60;
const int INF=1<<30;
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; }
int main(){
int n;cin>>n;
vector<ll> a(n);
for(int i = 0; i < n; i++) {
cin>>a[i];
}
vector<ll> sum_a(n + 1,0);
vector<ll> max_a(n + 1,0);
for(int i = 0; i < n; i++) {
sum_a[i + 1] = sum_a[i] + a[i];
}
for(int i = 0; i < n; i++) {
max_a[i + 1] = max(max_a[i],sum_a[i + 1]);
}
ll ans = 0;
ll res = 0;
for(int i = 1; i <= n; i++) {
chmax(res,ans + max_a[i]);
ans += sum_a[i];
}
cout << res << 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 ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define codeWizard ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define testCase(tc) while(tc--)
#define vi vector<int>
#define vvi vector<vi>
#define pi pair<int,int>
#define int long long
#define lb lower_bound
#define ub upper_bound
#define mod 1000000007
//#define mod 998244353
#define ff first
#define ss second
#define inf (int)1e17
#define ninf -inf
#define endl "\n"
#define fill(a,b) memset(a,b,sizeof(a))
#define all(v) v.begin(),v.end() //codeWizard
int LCM(int a, int b) {return (a * b) / __gcd(a, b);}
void init_code() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
int n;
vvi player;
bool check(int x){
set<int> st;
for(auto k:player){
int bit=0;
int i=0;
for(auto i:k){
bit <<= 1;
bit |= i >= x;
}
st.insert(bit);
}
for(auto x:st){
for(auto y:st){
for(auto z:st){
if((x|y|z)==31)
return true;
}
}
}
return false;
}
void solve(){
cin>>n;
for(int i=1;i<=n;i++){
vector<int> temp(5);
for(int j=0;j<5;j++)
cin>>temp[j];
player.push_back(temp);
}
int low=1,high=1000000001;
int ans=0;
while(high>low+1){
//cout<<low<<" "<<high<<endl;
int mid=(low+high)/2;
if(check(mid)){
low=mid;
}
else
high=mid;
}
cout<<low<<endl;
}
signed main()
{
codeWizard;
init_code();
int tc = 1;
//cin >> tc;
int num=1;
testCase(tc) {
//cout<<"Case #"<<num<<": ";
num++;
solve();
}
//cout << "Time execute: " << clock() / (double)CLOCKS_PER_SEC << " sec" << endl;
}
|
#include <bits/stdc++.h>
#define ls (rt<<1)
#define rs (rt<<1|1)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=4e5+10;
const int INF=0x3f3f3f3f;
int a[N],n;
pii pa[N];
int mx[N<<2],lazy[N<<2];
int main() {
auto push_down=[&](int rt){
if(lazy[rt]){
lazy[ls]+=lazy[rt];
lazy[rs]+=lazy[rt];
mx[ls]-=lazy[rt];
mx[rs]-=lazy[rt];
lazy[rt]=0;
}
};
function<void(int,int,int)>build=[&](int l,int r,int rt){
if(l==r){mx[rt]=l;return ;}
int mid=l+r>>1;
build(l,mid,ls);
build(mid+1,r,rs);
};
function<int(int,int,int,int)>query=[&](int l,int r,int x,int rt){
if(l==r)return mx[rt];
push_down(rt);int mid=l+r>>1;
if(x<=mid)return query(l,mid,x,ls);else return query(mid+1,r,x,rs);
};
function<void(int,int,int,int,int,int)>update=[&](int l,int r,int L,int R,int val,int rt){
if(L<=l&&r<=R){mx[rt]-=val;lazy[rt]+=val;return ;}
push_down(rt);int mid=l+r>>1;
if(L<=mid)update(l,mid,L,R,val,ls);
if(mid<R)update(mid+1,r,L,R,val,rs);
mx[rt]=max(mx[ls],mx[rs]);
};
scanf("%d",&n);
for(int i=1;i<=2*n;i++)scanf("%d",&a[i]),pa[i]=make_pair(a[i],i>n?i-n:n-i+1);
sort(pa+1,pa+2*n+1);
reverse(pa+1,pa+2*n+1);
ll ans=0;
build(1,n,1);
for(int i=1;i<=2*n;i++){
int x=pa[i].second;
int val=query(1,n,x,1);
if(val==0)continue;
ans+=pa[i].first;
int l=1,r=n,res=-1;
while (l<=r){
int mid=l+r>>1;
if(query(1,n,mid,1)>=val){
res=mid;
r=mid-1;
}else l=mid+1;
}
update(1,n,res,n,1,1);
}
cout<<ans;
return 0;
} | #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <fstream>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define x first
#define y second
#define Time (double)clock()/CLOCKS_PER_SEC
#define debug(x) std::cerr << #x << ": " << x << '\n';
#define FOR(i, n) for (int i = 0; i < n; ++i)
#define FORN(i, n) for (int i = 1; i <= n; ++i)
#define pb push_back
#define trav(a, x) for (auto& a : x)
using vi = vector<int>;
template <typename T>
std::istream& operator >>(std::istream& input, std::vector<T>& data)
{
for (T& x : data)
input >> x;
return input;
}
template <typename T>
std::ostream& operator <<(std::ostream& output, const pair <T, T> & data)
{
output << "(" << data.x << "," << data.y << ")";
return output;
}
template <typename T>
std::ostream& operator <<(std::ostream& output, const std::vector<T>& data)
{
for (const T& x : data)
output << x << " ";
return output;
}
ll div_up(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up
ll div_down(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down
ll math_mod(ll a, ll b) { return a - b * div_down(a, b); }
#define tcT template<class T
#define tcTU tcT, class U
tcT> using V = vector<T>;
tcT> void re(V<T>& x) {
trav(a, x)
cin >> a;
}
tcT> bool ckmin(T& a, const T& b) {
return b < a ? a = b, 1 : 0;
} // set a = min(a,b)
tcT> bool ckmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0;
}
ll gcd(ll a, ll b) {
while (b) {
tie(a, b) = mp(b, a % b);
}
return a;
}
signed main() {
#ifdef LOCAL
#else
#define endl '\n'
ios_base::sync_with_stdio(0); cin.tie(0);
#endif
int n;
cin >> n;
vi a(2 * n);
cin >> a;
multiset <int> ms;
int ans = 0;
trav (e, a)
ans += e;
FOR (i, n) {
int l = n - 1 - i;
int r = n + i;
ms.insert(a[l]);
ms.insert(a[r]);
ans -= *ms.begin();
ms.erase(ms.begin());
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
// (setq-default c-basic-offset 2)
int main() {
int n;
cin >> n;
vector<long long> a(n);
vector<long long> b(n);
for (long long& ai : a) {
cin >> ai;
}
for (long long& bi : b) {
cin >> bi;
}
long long dot = 0;
for (int i = 0; i < n; ++i) {
dot += a[i] * b[i];
}
if (dot == 0) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
}
| #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef set<int> si;
typedef map<int,int> mii;
typedef map<ll,ll> mll;
typedef vector<pii> vii;
typedef vector<pll> vll;
#define fi first
#define se second
#define pi 3.141592653589793
#define mod 998244353
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define pqmax priority_queue<int>
#define pqmin priority_queue<int,vi,greater<int>>
#define fio ios_base::sync_with_stdio(0), cin.tie(NULL)
#define tc int tt;cin>>tt;for(int ti=1;ti<=tt;ti++)
#define case_g "Case #"<<ti<<": "
#define RED "\033[31m"
#define GREEN "\033[32m"
#define RESET "\033[0m"
#define sleep for (int i = 1, a;i < 100000000;i++, a = a * a)
typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> ranked_pairset;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ranked_set;
typedef tree<int, int, less<int>, rb_tree_tag, tree_order_statistics_node_update> ranked_map;
int main() {
fio;
int n;
cin >> n;
vl a(n);
for (int i = 0;i < n;i++)
cin >> a[i];
ll ans = 0;
for (int i = 0;i < n;i++) {
ll b;
cin >> b;
ans += b * a[i];
}
(ans && cout << "No\n") || cout << "Yes\n";
}
|
#include<bits/stdc++.h>
#include<algorithm>
#define int long long
using namespace std;
signed main(){
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
//MessageBox(GetForegroundWindow(),"标题","内容",MB_YESNO);
int a,b,x,y;
cin>>a>>b>>x>>y;
int sum=0;
if(x+x<y){
y=x+x;
}
if(a>b){
sum=(a-b-1)*y;
sum+=x;
}else if(a==b){
sum=x;
}else{
sum=(b-a)*y;
sum+=x;
}
cout<<sum<<endl;
return 0;
}
| #include <bits/stdc++.h>
#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 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 uni(x) x.erase(unique(rng(x)),x.end())
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
#define show(x) cerr<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,v(T),greater<T> >
#define bn(x) ((1<<x)-1)
#define dup(x,y) (((x)+(y)-1)/(y))
#define newline puts("")
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef tuple<int,int,int> T;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<T> vt;
const double eps = 1e-10;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define dame { puts("-1"); return 0;}
#define yn {puts("Yes");}else{puts("No");}
const int MX = 200005;
int main() {
int a,b,x,y; cin >> a >> b >> x >> y;
int ans;
if(a>b){
ans = min(((a-b)*2-1)*x, x+(a-b-1)*y);
}else if(a == b){
ans = x;
}else{
ans = min(((b-a)*2+1)*x, x+(b-a)*y);
}
cout << ans << endl;
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, int>;
using T = tuple<int,int,int>;
int main()
{
int n, m;
cin >> n >> m;
vector<vector<T>> g(n);
rep(i, m)
{
ll a, b,c,d;
cin >> a >> b >> c >> d;
--a, --b;
g[a].emplace_back(b, c, d);
g[b].emplace_back(a, c, d);
}
const ll INF = 1e18;
vector<ll> d(n, INF);
priority_queue<P, vector<P>, greater<P>> pq;
pq.emplace(0, 0);
d[0] = 0;
while (!pq.empty())
{
ll c = pq.top().first;
int s = pq.top().second;
pq.pop();
if (d[s] < c) continue;
for (const auto& gi: g[s])
{
int e, ci, di;
tie(e, ci, di) = gi;
int t = round(sqrt(di));
ll next = d[s] + ci + di / max((ll)t, d[s] + 1) + max(0LL, t - 1 - d[s]);
if (d[e] <= next) continue;
d[e] = next;
pq.emplace(d[e], e);
}
}
ll ans = d[n - 1];
if (ans == INF) cout << -1 << endl;
else cout << ans << endl;
return 0;
}
| // #pragma GCC target ("avx,avx2")
// #include <bits/extc++.h>
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#define f first
#define s second
using namespace std;
// using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
// typedef tree<int, null_type, less<int>,
// rb_tree_tag, tree_order_statistics_node_update> oset;
//========================================================
const int N = 1e5+5, K = 19, M = 5e4+5, mod = 1e9+7, len = 30, inf = 0x3f3f3f3f;
int n, m;
ll dist[N];
vector<pair<int, pair<ll, ll>>> adj[N];
void dijkstra(int src)
{
priority_queue<pair<ll, int>> q;
fill(dist, dist+n, 1e18);
q.push({0, src});
dist[src] = 0;
while(!q.empty())
{
auto t = q.top();
q.pop();
int u = t.s;
if(dist[u] != -t.f)
continue;
if(u == n-1)
break;
for(auto e : adj[u])
{
ll v = e.f, c = e.s.f, d = e.s.s;
auto dis = [&](ll T)
{
ll res = c + ll(d / (T + 1)) + T;
return res;
};
ll s = 1ll * sqrt(d) - 1;
s = max(s, 0LL);
if(dist[u] > s)
s = dist[u];
ll D = min(dis(s), dis(s+1));
// assert(D > 0);
if(dist[v] > D)
dist[v] = D, q.push({-D, v});
}
}
if(dist[n-1] == 1e18)
dist[n-1] = -1;
}
void run_case()
{
scanf("%d %d", &n, &m);
for(int i=0, u, v, c, d; i<m; i++)
{
scanf("%d %d %d %d", &u, &v, &c, &d);
adj[u-1].push_back({v-1, {c, d}});
adj[v-1].push_back({u-1, {c, d}});
}
dijkstra(0);
printf("%lld\n", dist[n-1]);
}
int main()
{
// ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
// freopen("input.txt", "rt", stdin);
// freopen("output.txt", "wt", stdout);
int t;
t = 1;
// cin >> t;
while(t--)
run_case();
}
|
#include <bits/stdc++.h>
using namespace std;
struct edge{
long long v;
long long c;
long long d;
};
long long f(long long x, long long y){
long long l = x, r = y+1;
while(l<r){
long long m1 = (r-l+1)/3 + l, m2 = (r-l+1)*2/3 + l;
long long c1 = y/(m1+1) + m1 - x, c2 = y/(m2+1) + m2 - x;
if(c1<c2) r = m2 - 1;
else if(c1>c2) l = m1 + 1;
else{
for(int i=1;;i++){
if(m1-i<l && m2+i>r){
l = m1;
r = m2;
if(l+1==r) return c1;
break;
}
if(m1-i>=l){
long long c = y/(m1-i+1) + m1-i - x;
if(c>c1){
l = m1;
break;
}
if(c<c1){
r = m1-1;
break;
}
}
if(m2+i<=r){
long long c = y/(m2+i+1) + m2+i - x;
if(c>c1){
r = m2;
break;
}
if(c<c1){
l = m2+1;
break;
}
}
}
}
}
return y/(l+1) + l - x;
}
int main(){
long long N,M;
cin >> N >> M;
vector<vector<edge>> G(N,vector<edge>(0));
for(int i=0;i<M;i++){
long long a,b,c,d;
cin >> a >> b >> c >> d;
a--;
b--;
edge e1 = {b,c,d}, e2 = {a,c,d};
G[a].push_back(e1);
G[b].push_back(e2);
}
vector<long long> V(N,-1);
priority_queue<pair<long long,long long>> Q;
Q.push(make_pair(0,0));
V[0] = 0;
while(!Q.empty()){
auto p = Q.top();
Q.pop();
long long t = -p.first, u = p.second;
if(t!=V[u]) continue;
for(int i=0;i<G[u].size();i++){
edge e = G[u][i];
long long cost = e.c;
if(e.d>=t+1) cost += f(t,e.d);
if(V[e.v]==-1 || V[e.v]>V[u]+cost){
V[e.v] = V[u] + cost;
Q.push(make_pair(-V[e.v],e.v));
}
}
}
cout << V[N-1] << endl;
return 0;
}
| /*input
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <iomanip>
#include <climits>
#define mod 1000000007
// templates
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define sz(x) (int)x.size()
#define po(x,y) fixed<<setprecision(y)<<x
#define ss(s) scanf(" %[^\n]%*c", s)
#define ps(s) printf("%s\n",s)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
void sc(int&n) {int number; scanf("%d", &number); n = number;}
void sc(ll&n) {ll number; scanf("%lld", &number); n = number;}
void _print(ll t) {printf("%lld", t);}
void _print(int t) {printf("%d", t);}
void _print(string t) {cout << t;}
void _print(char t) {printf("%c\n", t);}
void _print(lld t) {cout << t;}
void _print(double t) {cout << t;}
void _print(ull t) {cout << t;}
template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cout << "{"; _print(p.F); cout << ","; _print(p.S); cout << "}";}
template <class T> void _print(vector <T> v) {cout << "vec->[ "; for (T i : v) {_print(i); cout << " ";} cout << "]";}
template <class T> void _print(set <T> v) {cout << "set->[ "; for (T i : v) {_print(i); cout << " ";} cout << "]";}
template <class T> void _print(multiset <T> v) {cout << "multiset->[ "; for (T i : v) {_print(i); cout << " ";} cout << "]";}
template <class T, class V> void _print(map <T, V> v) {cout << "map->[ "; for (auto i : v) {_print(i); cout << " ";} cout << "]";}
const ll inf = (ll)1e17 + 10;
const int N = (int)1e5 + 10;
vector<array<ll, 3>>adj[N];
void bfs(int n) {
vector<ll>dist(n + 1, 0);
for (int i = 0; i <= n; ++i)
{
dist[i] = inf;
}
set<array<ll, 2>>pq;
dist[0] = 0;
pq.insert({0, 0});
while (!pq.empty()) {
auto [t, node] = *pq.begin();
pq.erase(pq.begin());
for (auto c : adj[node]) {
ll cost = 0;
ll sq = 0;
cost = t + c[1] + (c[2] / (t + 1));
sq = sqrt(c[2]);
if (sq * sq != c[2])sq++;
sq--;
if (t < sq) {
cost = min(cost, sq + c[1] + (c[2] / (sq + 1)));
}
if (dist[c[0]] > cost) {
pq.erase({dist[c[0]], c[0]});
dist[c[0]] = cost;
pq.insert({dist[c[0]], c[0]});
}
}
}
if (dist[n - 1] == inf)dist[n - 1] = -1;
_print(dist[n - 1]);
}
void solve()
{
int n, m;
sc(n), sc(m);
for (int i = 0; i < m; ++i)
{
ll u, v, c, d;
sc(u), sc(v), sc(c), sc(d);
u--, v--;
adj[u].push_back({v, c, d});
adj[v].push_back({u, c, d});
}
bfs(n);
}
int main()
{
//#ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
//#endif
int t = 1;
// sc(t);
while (t--) {
solve();
}
return 0;
} |
/*
皮卡丘冲鸭!
へ /|
/\7 ∠_/
/ │ / /
│ Z _,< / /`ヽ
│ ヽ / 〉
Y ` / /
イ● 、 ● ⊂⊃〈 /
() へ | \〈
>ー 、_ ィ │ //
/ へ / ノ<| \\
ヽ_ノ (_/ │//
7 |/
>―r ̄ ̄`ー―_
*/
#include <iostream>
#define For(i,x,y) for(int i=(x); i<=(y); i++)
#define fori(i,x,y) for(int i=(x); i<(y); i++)
#define rep(i,y,x) for(int i=(y); i>=(x); i--)
#define mst(x,a) memset(x,a,sizeof(x))
#define pb push_back
#define sz(a) (int)a.size()
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int,int>pa;
typedef pair<ll,ll>pai;
const int N = 1e3;
const int M = 1e5;
ll f[66][2];
const string ju[]={"AND","OR"};
int main()
{
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n;
cin>>n;
string s[70];
ll a[70];
For(i,1,n){
cin>>s[i];
a[i] = s[i]==ju[0]?1:0;
}
int pre = 0;
int ans = 0;
f[0][0] = 1;
f[0][1] = 1;
For(i,1,n){
if(a[i]){
f[i][0] = f[i-1][1] + f[i-1][0]*2;
//f[i][1] = f[pre][1];
f[i][1] = f[i-1][1];//if(pre!=0&&i-pre>1)f[i][1] = f[pre][1]*(1<<(i-pre-1)) + f[pre][0]*(1<<(i-pre-2));
//else f[i][1] = f[pre][1];
}else {
f[i][1] = f[i-1][0] + f[i-1][1]*2;
f[i][0] = f[i-1][0];
}//ans = max(f[i][1],ans);
}
cout<<f[n][1]<<endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define nl "\n"
#define nf endl
#define ll long long
#define pb push_back
#define _ << ' ' <<
#define INF (ll)1e18
#define mod 998244353
#define maxn 110
ll i, i1, j, k, k1, t, n, m, res, flag[10], a, b;
string s;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
#if !ONLINE_JUDGE && !EVAL
ifstream cin("input.txt");
ofstream cout("output.txt");
#endif
cin >> n >> s; s = '#' + s;
if (s[1] != s[n]) {
cout << 1 << nl; return 0;
}
for (i = 1; i <= n - 1; i++) {
if (s[i] != s[1] && s[i + 1] != s[1]) {
cout << 2 << nl; return 0;
}
}
cout << -1 << nl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define repp(i, n, m) for (int i = m; i < int(n); ++i)
//alias g++='g++ -I/mnt/c/Users/Owner/Desktop/ac-library'
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using PI = pair<pair<int, int>, int>;
using PL = pair<long long, long long>;
using PLL = pair<pair<long long, long long>, long long>;
using Pxy = pair<long double, long double>;
const int INF = 1001001007;
const int modd = 1000000007;
const long long modl = 1000000007LL;
const long long mod = 998244353LL;
const ll inf = 2e18;
void yes() { cout << "Yes" << endl; }
void no() { cout << "No" << endl; }
void yn(bool t) {if(t) yes(); else no();}
void Yes() { cout << "YES" << endl; }
void No() { cout << "NO" << endl; }
void YN(bool t) {if(t) Yes(); else No();}
vector<int> dx = { 0, 1, 0, -1 };
vector<int> dy = { 1, 0, -1, 0 };
const string ALP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string alp = "abcdefghijklmnopqrstuvwxyz";
int main() {
int N; cin >> N;
int cou = 0;
rep(i,N){
int a; cin >>a;
if((a-10)<0) continue;
cou += (a-10);
}
cout << cou << endl;
} | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
//#define ACL
#ifdef ACL
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
#if __has_include("acl-all.h")
#include "acl-all.h" //on Wandbox
using namespace atcoder;
#endif
#endif //#ifdef ACL
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<pair<ll, ll> > vpll;
typedef vector<vector<int> > vvi;
typedef vector<vector<char> > vvc;
typedef vector<vector<string> > vvs;
typedef vector<vector<ll> > vvll;
typedef vector<vector<bool> > vvb;
typedef pair<ll, ll> pll;
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define rrep(i, n) for (int i = 1; i <= int(n); ++i)
#define irep(it, stl) for (auto it = stl.begin(); it != stl.end(); it++)
#define drep(i, n) for (int i = int(n)-1; i >= 0; --i)
#define MES(a) MES2 a
#define MES2(a0,a1,a2,a3,a4,x,...) x
#define mes_1(x1) cout<<x1<<endl
#define mes_2(x1,x2) cout<<x1<<" "<<x2<<endl
#define mes_3(x1,x2,x3) cout<<x1<<" "<<x2<<" "<<x3<<endl
#define mes_4(x1,x2,x3,x4) cout<<x1<<" "<<x2<<" "<<x3<<" "<<x4<<endl
#define mes_5(x1,x2,x3,x4,x5) cout<<x1<<" "<<x2<<" "<<x3<<" "<<x4<<" "<<x5<<endl
#define mes(...) CHOOSE((__VA_ARGS__,mes_5,mes_4,mes_3,mes_2,mes_1,~))(__VA_ARGS__)
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0,a1,a2,a3,a4,x,...) x
#define debug_1(x1) cout<<#x1<<": "<<x1<<endl
#define debug_2(x1,x2) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<endl
#define debug_3(x1,x2,x3) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<endl
#define debug_4(x1,x2,x3,x4) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<endl
#define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<", "#x5<<": "<<x5<<endl
#define debug(...) CHOOSE((__VA_ARGS__,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__)
#define Ynmes(a) (a) ? mes("Yes") : mes("No")
#define YNmes(a) (a) ? mes("YES") : mes("NO")
#define re0 return 0
#define mp(p, q) make_pair(p, q)
#define pb(n) push_back(n)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define Sort(a) sort(a.begin(), a.end())
#define rSort(a) sort(a.rbegin(), a.rend())
#define Rev(a) reverse(a.begin(), a.end())
int dx[8] = { 1, 0, -1, 0, 1, -1, -1, 1 };
int dy[8] = { 0, 1, 0, -1, 1, 1, -1, -1 };
template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
struct io { io() { ios::sync_with_stdio(false); cin.tie(0); } };
const int INF = INT_MAX;
const ll LLINF = 1LL << 60;
const ll MOD = 1000000007;
const double EPS = 1e-9;
ll fact_mod(ll n, ll mod) {
ll f = 1; for (ll i = 2; i <= n; i++) f = f * (i % mod) % mod;
return f;
}
ll modpow(ll x, ll n, ll mod) {
if(n == 0) return 1;
ll res = modpow((x * x) % mod, n / 2 , mod);
if(n & 1) res = (res * x) % mod;
return res;
}
ll modncr(ll n, ll r, ll mod) {
if(r > n-r) r = n-r;
if(r == 0) return 1;
ll a = 1;
rep(i, r) a = a * ((n-i) % mod) % mod;
ll b = modpow(fact_mod(r, mod), mod-2, mod);
return (a % mod) * (b % mod) % mod;
}
signed main() {
ll n;
cin >> n;
if (n < 999) {
mes(0);
re0;
}
ll r = 0;
if (n > 999) {
r += n-999;
}
if (n > 999999) {
r += n-999999;
}
if (n > 999999999) {
r += n-999999999;
}
if (n > 999999999999) {
r += n-999999999999;
}
if (n > 999999999999999) {
r += n-999999999999999;
}
mes(r);
}
|
#include <bits/stdc++.h>
#define all(a) a.begin(), a.end()
#define forn(i,n) for(int i = 0; i < (int) n; i++)
#define ios ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
typedef long long ll;
ll stars3(ll x) {
return (x + 2) * (x + 1) / 2;
}
ll n;
// a + b + c = x
// 0 <= a,b,c < n
ll count3(ll x) {
ll ans = stars3(x);
if (x >= n) ans -= stars3(x - n) * 3;
if (x >= 2 * n) ans += stars3(x - 2 * n) * 3;
if (x >= 3 * n) ans -= stars3(x - 3 * n);
return ans;
}
// b + c = x
// 0 <= b,c < n
ll count2(ll x) {
ll ans = x + 1;
if (x >= n) ans -= (x - n + 1) * 2;
if (x >= 2 * n) ans += (x - 2 * n + 1);
return ans;
}
int main() {
ll k;
cin >> n >> k;
ll sm, tot = 0;
for(sm = 0; sm <= 3 * n - 3; sm++) {
ll cnt = count3(sm);
if (tot + cnt >= k) break;
tot += cnt;
}
int a;
for(a = 0; a < n; a++) {
ll cnt = count2(sm - a);
if (tot + cnt >= k) break;
tot += cnt;
}
int fs = max(0ll, sm - a - n + 1);
int b = fs + k - tot - 1;
int c = sm - a - b;
cout << a + 1 << " " << b + 1 << " " << c + 1 << "\n";
}
| #include <bits/stdc++.h>
using namespace std;
int N;
long long K, dp[4][3000001];
int main() {
scanf("%d %lld", &N, &K);
memset(dp,0,sizeof(dp));
dp[0][0] = 1;
for (int i = 1; i <= 3; ++i) {
long long x = 0;
for (int j = 0; j <= N*i; ++j) {
dp[i][j] = x, x += dp[i-1][j];
if (j >= N) x -= dp[i-1][j-N];
}
}
int sum = 0;
for (sum = 0; sum <= N*3 && K > dp[3][sum]; ++sum) K -= dp[3][sum];
int num = 1;
while (num <= N && K > max(min(N,sum-num-1) - max(1,sum-num-N) + 1,0))
K -= max(min(N,sum-num-1) - max(1,sum-num-N) + 1,0), num++;
int x = max(1,sum-num-N) + K - 1;
printf("%d %d %d", num, x, sum - num - x);
return 0;
}
|
#include <algorithm>
#include <iostream>
using namespace std;
using ll = long long;
int a[100001];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
fill(a + 1, a + n + 1, 2);
for(int i = 2; i + i <= n; i++) {
for(int j = i + i; j <= n; j += i) {
a[j] += a[i] == a[j];
}
}
cout << 1;
for(int i = 2; i <= n; i++) {
cout << ' ' << a[i];
}
return 0;
}
| #include <cstdio>
#include <vector>
using namespace std;
int main (){
bool notprime[100001] = {};
int N;
int v[100001] = {};
vector<int> prime;
scanf("%d", &N);
notprime[1] = true;
for (int i = 2; i <= N; i++)
if (!notprime[i]){
prime.push_back(i);
for (long long j = (long long) i * i; j <= N; j += i)
notprime[j] = true;
}
v[1] = 1;
v[2] = v[3] = 2;
for (int i = 4; i <= N; i++){
int j = i;
int index = 0;
while (index < prime.size() && j > 1){
if (prime[index] > j)
break;
else if (j % prime[index])
index++;
else {
v[i] = max(v[i], v[i / prime[index]] + 1);
while (j % prime[index] == 0)
j /= prime[index];
}
}
}
for (int i = 1; i <= N; i++)
printf("%d%c", v[i], i < N ? ' ' : '\n');
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define watch(x) cerr << (#x) << ": " << (x) << endl
typedef long long LL;
const int N = 100009;
int n, l, a[N], b[N];
LL ans;
void nope() {
cout << -1 << endl;
exit(0);
}
LL rgt(int i, int j) {
LL ret = 0;
int prevadd = 0;
int cur = i+1;
for(int index = i; index < j; index++) {
while(cur <= j) {
int seek = b[index] + (cur-index);
if(seek < a[cur]) {
nope();
} else if (seek > a[cur]) {
cur++;
} else {
int curadd = cur-index;
if(prevadd-1 != curadd) {
ret += cur-index;
}
prevadd = curadd;
break;
}
}
if(cur > j) {
nope();
}
}
return ret;
}
LL lft(int i, int j) {
LL ret = 0;
int prevadd = 0;
int cur = j-1;
for(int index = j; index > i; --index) {
while(cur >= i) {
int seek = b[index] - (index-cur);
if(seek > a[cur]) {
nope();
} else if(seek < a[cur]) {
cur--;
} else {
int curadd = index - cur;
if(prevadd-1 != curadd) {
ret += index-cur;
}
prevadd = curadd;
break;
}
}
if(cur < i) {
nope();
}
}
return ret;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> l;
for(int i = 1; i <= n; ++i) {
cin >> a[i];
}
for(int i = 1; i <= n; ++i) {
cin >> b[i];
}
a[n+1] = b[n+1] = l+1;
int state = 0; // 0 is adding to right chain, 1 is adding to left chain
int prev = 0;
for(int i = 0; i <= n+1; ++i) {
if(a[i] == b[i]) {
if(state == 0) {
ans += rgt(prev, i);
state = 1;
prev = i;
} else {
ans += lft(prev, i-1);
prev = i;
}
} else if(b[i] > a[i]) { //going right now
if(state == 1) {
ans += lft(prev, i-1);
prev = i;
state = 0;
}
} else { //going left now
if(state == 0) {
nope();
}
}
}
cout << ans << '\n';
return 0;
}
| /*
“The only way that we can live is if we grow.
The only way we can grow is if we change.
The only way we can change is if we learn.
The only way we can learn is if we are exposed.
And the only way that we are exposed is if we throw ourselves into the open.”
*************************************** ************************ **********************************
*/
#include <bits/stdc++.h>
using namespace std;
//---------------------mishra ka idea------------------
#define int long long
#define double long double
#define pb push_back
#define pf push_front
#define pii pair<int,int>
#define vi vector<int>
#define vii vector<pii>
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define x first
#define y second
#define endl '\n'
#define sz(x) (int)(x).size()
#define ull unsigned long long
//------------------------mishra ka idea-------------------
//-------------------------alsi hu mai-----------------------
#define fo(i,l,u) for(i=l;i<u;i++)
#define rfo(i,l,u) for(i=l;i>=u;i--)
#define allfo(s) for(auto it=(s).begin();it!=(s).end();it++)
#define _init(b) memset(b,-1,sizeof(b))
#define _init0(b) memset(b,0,sizeof(b))
#define MOD 1000000007
#define hell 998244353
#define output(x) cout << (x ? "YES" : "NO")<<endl;
#define Output(x) cout << (x ? "Yes" : "No")<<endl;
//-------------------------alsi hu mai-----------------------
//--------------------het se uthaya---------------------
#define MOD2 (998244353)
#define MOD3 (1000000009)
#define PI acos(-1)
#define eps (1e-8)
#define INF (1e18)
template<class A,class B>ostream&operator<<(ostream&out,const pair<A,B>&a){return out<<"("<<a.first<<","<<a.second<<")";}
template<class A>ostream&operator<<(ostream&out,const vector<A>&a){for(const A &it:a)out<<it<<" ";return out;}
template<class A,class B>istream&operator>>(istream&in,pair<A,B>&a){return in>>a.first>>a.second;}
template<class A>istream&operator>>(istream&in,vector<A>&a){for(A &i:a)in>>i;return in;}
ifstream cinn("in.txt");ofstream coutt("out.txt");
int poww(const int &a,int b,const int &m=MOD){if(b==0)return 1;int x=poww(a,b/2,m);x=x*x%m;if(b&1)x=x*a%m;return x;}
//--------------------het se uthaya-------------------------------------------------------------------
int gcd(int a, int b) {if(a>b)swap(a,b) ; if (a == 0) return b; return gcd(b % a, a); }
int rint(int l , int r){return rand()%(r-l+1) + l ;}
//---------------------------aab code dekho bahut template dekh liya---------------------------------
const int N = 1e5+5;
vii graph[N];
int lable[N],vis[N];
void dfs(int p , int par , int l ){
if(vis[p]!=-1)return ;
vis[p]=1;
//cout<<p<<" "<<par<<" "<<l<<endl;
if(l!=-1)lable[p]=l;
for(auto aa : graph[p]){
if(aa.x==par)continue;
if(vis[aa.x]==-1){
//cout<<aa.y<<"<<"<<endl;
if(aa.y==lable[p])dfs(aa.x,p,-1);
else dfs(aa.x,p,aa.y);
}
}
int ans=1;
if(l==-1){
vi v;
for(auto aa:graph[p]){
v.pb(lable[aa.x]);
}
sort(all(v));
for(auto aa:v){
if(ans==aa)ans++;
}
lable[p]=ans;
}
}
signed main()
{
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int i,j,x;
int n , m;
cin>>n>>m;
int a, b, c;
fo(i,0,m){
cin>>a>>b>>c;
graph[a].pb({b,c});
graph[b].pb({a,c});
}
_init(vis);
dfs(1,0,-1);
fo(i,1,n+1){
cout<<lable[i]<<" ";
}
return 0;
}
|
#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <array>
#include <numeric>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, int> pdi;
typedef tuple<ll, ll, ll> tl3;
//typedef modint998244353 mint;
const int BIG_NUM = 1e9;
const ll INF = 1000000000000000000;
const ll MOD = 1e9 + 7;
//const ll MOD = 998244353;
const double EPS = 1e-7;
const int MAX = 1e5 + 5;
const string ATC = "atcoder";
int main() {
int n;
cin >> n;
vector<int> a(n);
vector<int> b(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
cin >> b[i];
}
vector<int> c(n);
ll sum = 0;
for (int i = 0; i < n; i++) {
sum += b[i];
c[i] = a[i] - b[i];
}
vector<priority_queue<ll>> pq(2);
for (int i = 0; i < n; i++) {
pq[i % 2].push(c[i]);
}
ll ans = sum;
while (!pq[0].empty()) {
ll p = pq[0].top() + pq[1].top();
pq[0].pop();
pq[1].pop();
if (p <= 0) {
break;
}
ans += p;
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
template <typename T> T my_gcd(T a, T b) {
if (b == 0) {
return a;
}
else {
return my_gcd(b, a%b);
}
}
int main() {
int n;
cin >> n;
vector<int> x(n);
for (int i = 0; i < n; i++) {
cin >> x.at(i);
}
set<int> s;
for (int i = 0; i < n; i++) {
int now = x.at(i);
for (int j = 2; j*j <= now; j++) {
if (now%j == 0) {
s.insert(j);
while (now%j == 0) {
now /= j;
}
}
}
if (now > 1) {
s.insert(now);
}
}
long long res = 1;
vector<int> p;
for (int e : s) {
res *= e;
p.push_back(e);
}
int m = p.size();
for (int i = 0; i < (1 << m); i++) {
long long now = 1;
for (int j = 0; j < m; j++) {
if (i & (1 << j)) {
now *= p.at(j);
}
}
if (now > res) {
continue;
}
bool check = true;
for (int k = 0; k < n; k++) {
if (my_gcd(now, (long long)x.at(k)) == 1) {
check = false;
break;
}
}
if (check) {
res = min(res, now);
}
}
cout << res << endl;
} |
#include <iostream>
using namespace std;
int main(){
int a,b,c,d;
cin>>a>>b>>c>>d;
cout<<-(b*c-a*d);
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define pi 3.1415926535
#define endl "\n"
#define yes cout << "YES\n"
#define no cout << "NO\n"
#define ll long long
#define setbits(x) __builtin_popcountll(x)
#define pb push_back
#define mp make_pair
#define sz(x) (int)x.size()
#define f(i,a,n) for(int i = a ; i < n ; i++)
#define rf(i,n,a) for(int i = n-1 ; i >= a ; i--)
#define F first
#define S second
#define mem(a,x) memset(a,x,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define get(a,n) f(i,0,n) cin >> a[i];
#define sp(x) fixed << setprecision(10) << x
#define nitro ios:: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define trace(x) cerr << '>' << #x << ':' << (x) << "\n"
#define trace2(x,y) cerr<< '>' << #x << ':' << (x) << " | " << #y << ':' << (y) << "\n"
#define trace3(a,b,c) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define trace4(a,b,c,d) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define show(a,n) f(i,0,n) cout << a[i] << " "; cout << endl;
int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};
int dy[] = {0, 0, -1, 1, 1, -1, 1, -1};
string ds = "DULR";
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef map<int,int> mii;
typedef map<ll,ll> mll;
const int inf = 1e9;
const ll inf64 = 1e18;
const int mod = inf + 7;
const int N = 1e6 + 10;
void solve()
{
int a, b, c, d;
cin >> a >> b >> c >> d;
cout << a*d - b*c;
}
int main()
{
nitro;
int tc = 1;
// cin >> tc;
while(tc--) solve();
}
|
#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);
#define ll long long int
#define INFL 1e18L
#define INF 2e9
#define nl '\n'
#define ld long double
#define precision(x) fixed << setprecision(x)
const ll mod = 998244353;
ll gcd(ll a, ll b) {
if (b == 0) return a;
return gcd(b , a % b);
}
ll powm(ll a, ll b) {
ll res = 1;
while (b) {
if (b & 1)
res = (res * 1LL * a) % mod;
b >>= 1LL;
a = (a * 1LL * a) % mod;
}
return res;
}
const int N = 2e5 + 5;
const int M = 10;
//--------------------------------------------------------------------------------------------------------------------------//
void solve() {
int n;
cin >> n;
ll mn[n][n];
ll a[n];
for (int i = 0; i < n; i++) cin >> a[i];
ll ans = 0;
for (int len = 1; len <= n; len++) {
for (int i = 0; i <= n - len; i++) {
int j = i + len - 1;
if (i == j) {
mn[i][j] = a[i];
ans = max(ans, mn[i][j]);
} else {
mn[i][j] = min(a[j], mn[i][j - 1]);
ans = max(ans, mn[i][j] * len);
}
}
}
cout << ans << nl;
}
int main() {
fast
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int tc = 1;
// cin >> tc;
int _ = 0;
while (_++ < tc) {
solve();
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define endl "\n"
#define mod 1000000007
#define rep(i,n) for(int i=0;i<n;i++)
#define repr(i,n) for(int i=n-1;i>=0;i--)
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll tt,n;
// cin >> tt;
// while(tt--){
cin>>n;
int a[n+1];
rep(i,n)
cin>>a[i];
stack<pair<int,int> >s;
vector<int>v1,v2;
for(int i=0;i<n;i++) //smallest nearest to left
{
if(s.empty())
v1.pb(0);
else if(a[i]>s.top().second)
v1.pb(s.top().first);
else
{
while(s.size()>0&&s.top().second>=a[i])
s.pop();
if(s.empty())
v1.pb(0);
else
v1.pb(s.top().first);
}
s.push(mp(i+1,a[i]));
}
while(!s.empty())
s.pop();
for(int i=n-1;i>=0;i--) //smallest nearest to right
{
if(s.empty())
v2.pb(n+1);
else if(a[i]>s.top().second)
v2.pb(s.top().first);
else
{
while(s.size()>0&&s.top().second>=a[i])
s.pop();
if(s.empty())
v2.pb(n+1);
else
v2.pb(s.top().first);
}
s.push(mp(i+1,a[i]));
}
reverse(v2.begin(),v2.end());
ll ans=0,area=0;
for(int i=0;i<n;i++)
{
area=a[i]*(v2[i]-v1[i]-1);
ans=max(ans,area);
}
cout<<ans<<endl;
// }
return 0;
} |
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <cmath>
#define rep(i,begin, end) for (ll i = begin; i < (ll)(end); i++)
using namespace std;
using ll = long long;
template<typename T>
using vec2 = vector<vector<T>>;
template<typename T>
using vec3 = vec2<vector<T>>;
template<typename T>
using vec4 = vec3<vector<T>>;
template<typename T>
using vec5 = vec4<vector<T>>;
template<typename T>
using vec6 = vec5<vector<T>>;
ll extGCD(ll a, ll b, ll &x, ll &y){
if(b == 0){
x = 1;
y = 0;
return a;
}
ll g = extGCD(b, a%b, y, x);
y -= a/b * x;
return g;
}
ll MOD = 1000000007;
vector<ll> mfact(1,1);
ll minv(ll a){
ll x;ll y;
extGCD(a, MOD, x, y);
if(x<0){
x+= ((-x)/MOD+1)*MOD;
x %= MOD;
}
return x;
}
ll mpow(ll a,ll b){
ll res = 1;
while(b > 0){
if(b%2==1)res=(res*a)%MOD;
b/=2;
a = (a*a)%MOD;
}
return res;
}
ll mfactorial(ll a){
if(mfact.size()-1 >= a)return mfact[a];
for(ll i = mfact.size();i<=a;i++){
mfact.push_back((i * mfact[i-1])%MOD);
}
return mfact[a];
}
ll mcomb(ll n, ll r){
return (((mfactorial(n) * minv(mfactorial(r)))%MOD) * minv(mfactorial(n-r)))%MOD;
}
ll ipow(ll a, ll b){
ll res = 1;
while(b > 0){
if(b % 2 == 1)res*=a;
a*=a;
b/=2;
}
return res;
}
void factorize(map<ll,ll>& fac, vector<ll>& sieved, ll a){
while(a != 1){
if(fac.find(sieved[a]) == fac.end())fac[sieved[a]] = 0;
fac[sieved[a]]++;
a/=sieved[a];
}
}
void primesieve(vector<ll>& res,ll n){
res = vector<ll>(n+1, -1);
for(int i = 2;i <= n;i++){
if(res[i] != -1)continue;
res[i] = i;
for(int j = 2;j*i <= n;j++){
res[j*i] = i;
}
}
}
vector<ll> primesuntil(ll n){
vector<ll> res;
vector<ll> sieved;
primesieve(sieved, n);
for(int i = 2;i <= n;i++){
if(sieved[i] == i)res.push_back(i);
}
return res;
}
int gcd(int a, int b){
if(b == 0)return a;
return gcd(b, a%b);
}
void solve(){
// cout<<"##########"<<endl;
ll X,Y,P,Q;cin>>X>>Y>>P>>Q;
ll res = -1;
rep(q,0,Q){
rep(y,0,Y){
ll d = gcd(2*X+2*Y, -(P+Q));
if((P+q-X-y) % d != 0)continue;
// cout<<(P+q-X-y)<<endl;
// cout<<d<<endl;
ll s,t;
extGCD((2*X+2*Y)/d, -(P+Q)/d, s,t);
// cout<<s<<" "<<t<<endl;
s *= (P+q-X-y)/d;
t *= (P+q-X-y)/d;
ll k = d<0 ? floor(min(-(double)d*(double)s/(double)(P+Q), -(double)d*(double)t/(double)(2*X+2*Y)))
: ceil(max(-(double)d*(double)s/(double)(P+Q), -(double)d*(double)t/(double)(2*X+2*Y)));
// cout<<k<<endl;
s += k*(P+Q)/d;
t += k*(2*X+2*Y)/d;
// cout<<2*X+2*Y<<" "<< (P+Q)<<" "<<s<<" "<<t<<endl;
// cout<<2*X+2*Y<<" "<< (P+Q)<<" "<<s<<" "<<t<<endl;
ll a = (2*X+2*Y)*s+X+y;
if(res == -1)res = a;
res = min(res,a);
// cout<<a<<endl;
// cout<<"!!!!!!!!!!!!!!!!"<<endl;
}
}
cout<<(res==-1 ? "infinity" : to_string(res))<<endl;
}
int main(){
int T;cin>>T;
rep(i,0,T)solve();
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for(int i=0; i<n; i++)
#define REPi(i, a, b) for(int i=int(a); i<int(b); i++)
#define MEMS(a,b) memset(a,b,sizeof(a))
#define mp make_pair
#define MOD(a, m) ((a % m + m) % m)
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const ll MOD = 1e9+7;
// 負の数にも対応した mod
// 例えば -17 を 5 で割った余りは本当は 3 (-17 ≡ 3 (mod. 5))
// しかし単に -17 % 5 では -2 になってしまう
inline long long mod(long long a, long long m) {
return (a % m + m) % m;
}
// 拡張 Euclid の互除法
// ap + bq = gcd(a, b) となる (p, q) を求め、d = gcd(a, b) をリターンします
//
// ## TODO : 要確認
// ## ax + by = Aを満たす一般解(x, y)を求める
// auto d = extGcd(a, b, p, q);
// if(A % d != 0) continue;
// p *= A/d, q *= A/d;
// (x, y) = (p + k * (b/d), q - k * (a/d));
long long extGcd(long long a, long long b, long long &p, long long &q) {
if (b == 0) { p = 1; q = 0; return a; }
long long d = extGcd(b, a%b, q, p);
q -= a/b * p;
return d;
}
int main(){
ll T;
cin >> T;
while(T--){
ll X, Y, P, Q;
cin >> X >> Y >> P >> Q;
ll T_train = 2*X + 2*Y;
ll T_player = P + Q;
const ll INF = LLONG_MAX;
ll ans = INF;
for(ll t1 = X; t1 < X+Y; t1++){
for(ll t2 = P; t2< P+Q; t2++){
//// t1 - t2 = -T_train * n + T_player * m;
ll n, m;
auto d = extGcd(T_train, T_player, n, m);
ll rhs = t1 - t2;
if(rhs % d != 0) continue;
n *= -rhs/d, m *= rhs/d;
n = mod(n, T_player/d);
//printf("%lld, %lld\n", t1 + T_train * n, t2 + T_player * m);
chmin(ans, t1 + T_train * n);
}
}
if(ans == INF){
cout << "infinity" << endl;
}
else{
cout << ans << endl;
}
}
return 0;
}
|
/**
* Dont raise your voice, improve your argument.
* --Desmond Tutu
*/
#include <bits/stdc++.h>
using namespace std;
const bool ready = [](){
ios_base::sync_with_stdio(false); cin.tie(0);
cout << fixed << setprecision(12);
return true;
}();
using ld=long double;
const ld PI = acos((ld)-1);
using ll= long long;
#define int ll
#define all(v) (v).begin(), (v).end()
#define fori(n) for(int i=0; i<int(n); i++)
#define cini(i) int i; cin>>i;
#define cins(s) string s; cin>>s;
#define cind(d) ld d; cin>>d;
#define cinai(a,n) vi a(n); fori(n) { cin>>a[i]; }
#define cinas(s,n) vs s(n); fori(n) { cin>>s[i]; }
#define cinad(a,n) vd a(n); fori(n) { cin>>a[i]; }
using pii= pair<int, int>;
using pdd= pair<ld, ld>;
using vd= vector<ld>;
using vb= vector<bool>;
using vi= vector<int>;
using vvi= vector<vi>;
using vs= vector<string>;
#define endl "\n"
/*
* Consider 1,3,2
* We would like to choose pos=2 to swap 3,2, but that not possible in
* first operation.
* So we must choose pos=1, which yields 3,1,2
* Then we must choose pos=2, 3,2,1
* 2,3,1
* 2,1,3
* 1,2,3
* So, we swap the smallest inversion if possible, else the next position,
* else the previous position.
* How to maintain the "next inversion"?
* -> Just use a pointer to current element, go left to right.
*
* How do we go left to right?
* Invariant: a[0]..a[i] is sorted
*/
void solve() {
cini(n);
cinai(a,n);
int i=0;
while(i+1<n && a[i]<a[i+1])
i++;
int op=0;
vi ans;
while(i+1<n) {
if(a[i]>a[i+1] && (i%2==op)) {
swap(a[i], a[i+1]);
ans.push_back(i+1);
if(i>0)
i--;
} else if(i+2<n) {
swap(a[i+1], a[i+2]);
ans.push_back(i+2);
} else {
assert(i>0);
i--;
swap(a[i], a[i+1]);
ans.push_back(i+1);
if(i>0)
i--;
}
while(i+1<n && a[i]<a[i+1])
i++;
op=!op;
}
cout<<ans.size()<<endl;
for(int i : ans)
cout<<i<<" ";
cout<<endl;
}
signed main() {
cini(t);
while(t--)
solve();
} | #include<bits/stdc++.h>
using namespace std;
#ifndef ONLINE_JUDGE
#define dbg(x...) do { cout << "\033[33;1m " << #x << " -> "; err(x); } while (0)
void err() { cout << "\033[39;0m" << endl; }
template<template<typename...> class T, typename t, typename... A>
void err(T<t> a, A... x) { for (auto v: a) cout << v << ' '; err(x...); }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }
#else
#define dbg(...)
#endif
typedef long long ll;
typedef pair<int,int> pi;
typedef vector<int> vi;
template<class T> using vc=vector<T>;
template<class T> using vvc=vc<vc<T>>;
template<class T> void mkuni(vector<T>&v)
{
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
}
ll rand_int(ll l, ll r) //[l, r]
{
static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
return uniform_int_distribution<ll>(l, r)(gen);
}
template<class T>
void print(T x,int suc=1)
{
cout<<x;
if(suc==1) cout<<'\n';
else cout<<' ';
}
template<class T>
void print(const vector<T>&v,int suc=1)
{
for(int i=0;i<v.size();i++)
print(v[i],i==(int)(v.size())-1?suc:2);
}
void solve()
{
int n;
cin>>n;
vi p(n);
vi pos(n);
for(int i=0;i<n;i++)
{
cin>>p[i];
p[i]--;
pos[p[i]]=i;
}
if(n==4&&p[0]==3&&p[1]==2&&p[2]==1&&p[3]==0)
{
print(6);
cout<<"1 2 3 2 1 2\n";
return;
}
if(n==4&&p[0]==2&&p[1]==3&&p[2]==0&&p[3]==1)
{
print(6);
cout<<"1 2 1 2 3 2\n";
return;
}
//dbg(p);
vi ans;
auto change=[&](int x){
assert(x%2==ans.size()%2&&x<n-1);
ans.push_back(x+1);
pos[p[x+1]]=x;
pos[p[x]]=x+1;
swap(p[x],p[x+1]);
};
auto waf=[&](){
for(int i=0;i<n-1;i++)
{
if(i%2==0&&p[i]&&p[i+1])
{
change(i);
break;
}
}
};
while(pos[0]!=0)
{
int tar=pos[0]-1;
if(tar%2!=ans.size()%2)
waf();
change(tar);
}
auto cwp=[&](int x){
assert(x);
if(x%2==ans.size()%2) change(x);
else{
change(x-1);
change(x);
change(x-1);
change(x);
change(x-1);
}
};
for(int i=1;i<n;i++)
{
//dbg(p);
while(pos[i]!=i)
{
int tar;
if(pos[i]>i) tar=pos[i]-1;
else tar=pos[i];
//dbg(tar);
if(tar%2!=ans.size()%2)
{
if(abs(tar-i)<=1) cwp(tar);
else {
for(int j=min(i,tar);j<=max(i,tar);j++)
{
if(j%2==ans.size()%2)
{
change(j);
break;
}
}
}
}
else change(tar);
}
}
print(ans.size());
print(ans);
//dbg(n,ans.size());
assert(ans.size()<=n*n);
}
int main()
{
cin.tie(nullptr)->sync_with_stdio(false);
int T;
cin>>T;
while(T--) solve();
}
// 1 3 2
// 3 1 2
// 3 2 1
// 2 3 1
// 2 1 3
// 1 2 3
|
#include<bits/stdc++.h>
using namespace std;
const int N=505;
char pic[N][N];
int book[N][N];
const int mod=998244353;
vector<char> c[N*2];
int n,m;
struct node
{
int x,y,d;
};
queue<node> q;
int check(int p)
{
int b,r,d;
b=r=d=0;
for (int i=0;i<c[p].size();i++)
{
char x=c[p][i];
//printf("%c \n",x);
if (x=='.')
d++;
else if (x=='B')
b++;
else
r++;
}
//printf("%d %d %d %d %d\n",p,b,r,d,c[p].size());
if (r==0&&b==0)
return 2;
if (r!=0&&b!=0)
return 0;
return 1;
}
int main()
{
scanf("%d%d\n",&n,&m);
for (int i=0;i<n;i++)
scanf("%s",pic[i]);
q.push({0,0,0});
int mx=0;
while (!q.empty())
{
node t=q.front();
q.pop();
if (book[t.x][t.y])
continue;
book[t.x][t.y]=1;
mx=max(mx,t.d);
int x=t.x,y=t.y,d=t.d;
//printf("%d %d %d %c\n",x,y,d,pic[x][y]);
c[t.d].push_back(pic[t.x][t.y]);
if (x+1<n&&!book[x+1][y])
q.push({x+1,y,d+1});
if (x-1>=0&&!book[x-1][y])
q.push({x-1,y,d+1});
if (y+1<m&&!book[x][y+1])
q.push({x,y+1,d+1});
if (y-1>=0&&!book[x][y-1])
q.push({x,y-1,d+1});
}
long long ans=1;
for (int i=0;i<=mx;i++)
{
ans=ans*check(i)%mod;
}
printf("%lld\n",ans);
}
| #include <bits/stdc++.h>
#define int long long int
#define all(x) x.begin(), x.end()
#define send ios_base::sync_with_stdio(false);
#define help cin.tie(NULL)
#define inf (int)(1e17+1)
#define mod (int)(998244353)
#define N (int)(2e5+5)
#define fi first
#define se second
#define endl "\n"
#define double long double
#define eps (double)(1e-9)
#define sa cout<<"sa"<<endl
using namespace std;
namespace math{
vector <int> fac(N,1);
int exp(int a,int b){
if(!b) return 1;
if(b&1) return a*exp(a,b-1)%mod;
int t=exp(a,b/2);
return t*t%mod;
}
void fc(){for(int i=2;i<N;i++)fac[i]=i*fac[i-1]%mod;}
int com(int n,int r){return (fac[n]*exp(fac[r]*fac[n-r]%mod,mod-2))%mod;}
int per(int n,int r){return (fac[n]*exp(fac[n-r],mod-2))%mod;}
vector <int> getpr(int x){
vector <int> res;if(x!=1) res.push_back(x);
for(int i=2;i*i<=x;i++){
if(i*i==x) res.push_back(i);
else if(x%i==0) res.push_back(i),res.push_back(x/i);;
}return res;}
}using namespace math;
void solve(){
int n,m;
cin>>n>>m;
vector <string> v(n);
for(int i=0;i<n;i++) cin>>v[i];
int ans=0;
vector <int> path(m+n);
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(v[i][j]=='B'&&path[i+j]==1) {cout<<0;return;}
if(v[i][j]=='R'&&path[i+j]==2) {cout<<0;return;}
if(v[i][j]=='B') path[i+j]=2;
else if(v[i][j]=='R') path[i+j]=1;
}
}
for(int i=0;i<n+m-1;i++) ans+=!path[i];
cout<<exp(2,ans);
}
int32_t main(){
send help;
#ifdef LOCAL
freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);
#endif
int t=1;
//cin>>t;
while(t--) solve();
return 0;
} |
Subsets and Splits