code_file1
stringlengths 87
4k
| code_file2
stringlengths 82
4k
|
---|---|
// AtCoder.cpp : このファイルには 'main' 関数が含まれています。プログラム実行の開始と終了がそこで行われます。
//
#ifndef ONLINE_JUDGE ///???
#endif ///????
#define _USE_MATH_DEFINES
#include<math.h>
#include<deque>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<tuple>
#include<string>
#include<chrono>
#include<functional>
#include<iterator>
#include<random>
#include<unordered_set>
#include<array>
#include<map>
#include<iomanip>
#include<assert.h>
#include<bitset>
#include<stack>
#include<memory>
#include <sstream>
#pragma GCC optimize("Ofast") //???
using namespace std;
typedef long long ll;
#define rad_to_deg(rad) (((rad)/2/M_PI)*360)
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define rep(i,n) for(ll i=0;i<n;i++)
#define show(s) cout<<s<<endl
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define LINF (1LL << 50)
#define MOD (1e9+7)
#define rrep(i,n) for(int i=n-1;i>=0;--i)
typedef pair < ll, ll> P;
//元がintの分けたものを返す p[0]は1桁目
vector<int> divnum(ll num) {
int dig;
vector<int>p;
while (num) {
dig = num % 10;
p.push_back(dig);
num /= 10;
}
return p;
}
//桁数を返す
int digiter(ll num) {
int dig;
vector<int>p;
while (num) {
dig = num % 10;
p.push_back(dig);
num /= 10;
}
return p.size();
}
//元がstringの分けたものを返す d[0]は最高位
vector<int> convertstring(string s) {
vector<int> d;
ll n = s.size();
reverse(s.begin(), s.end());
rep(i, n) {
d.push_back(s[i] - '0');
}
return d;
}
// 1-indexedなので注意。(先頭が1から始まる) O(log N) (Nは要素数)
struct BIT {
private:
vector<int> bit;
int N;
public:
BIT(int size) {
N = size;
bit.resize(N + 1);
}
// 一点更新です
void add(int a, int w) {//a番目にwを加算
for (int x = a; x <= N; x += x & -x) bit[x] += w;
}
// 1~a(以下)までの和を求める。[1,a]
int sum(int a) {
int ret = 0;
for (int x = a; x > 0; x -= x & -x) ret += bit[x];
return ret;
}
};
/*long double は%Lf*/
//queue古い順,stack新しい順
int N; P A[3000005]; ll cnt[3000005],res[3000005];
int32_t main() {
cin >> N;
rep(i, N) {
int a;
cin >> a;
A[i] = P(a, i+1);
}
sort(A, A + N);
BIT bit(N+5);
rep(i, N) {
int sum = bit.sum(A[i].second);
cnt[A[i].first]=A[i].second - 1 - sum;
bit.add(A[i].second,1);
res[0]+=(cnt[A[i].first]);
}
rep(i, N) {
swap(A[i].first, A[i].second);
}
sort(A, A + N);
rep(i, N) {
res[i+1] = res[i]+N-A[i].second-1LL-A[i].second;
}
rep(i, N) {
cout << res[i] << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
#define BUFF ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define ll long long
inline ll read(){ll f=1;ll x=0;char ch=getchar();while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x*f; }
inline ll quick_pow(ll a,ll b,ll mod) {ll ans=1,base=a;while(b){if(b&1)ans=ans*base%mod;base=base*base%mod;b>>=1;}return ans%mod;}
inline ll inv(ll x,ll p){return quick_pow(x,p-2,p);}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll lcm(ll a,ll b){return a*b/gcd(a,b);}
const double pi=acos(-1.0);
const ll inf = LONG_LONG_MAX;
const ll mod = 1e9+7;
const ll maxn = 1e4 + 10;
ll a[maxn];
int main()
{
ll n;
n = read();
rep(i,1,n)
{
a[i] = read();
}
ll ans = 0;
for (int i = 1;i <= n;i++)
{
ll minn = a[i];
for (int j = i; j <= n;j++)
{
minn = min(minn, a[j]);
ans = max(ans, minn * (j - i + 1));
}
}
cout << ans << endl;
return 0;
}
|
/********include********/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#define popcount __builtin_popcount
using namespace std;
//#include <atcoder/all>
/***/
//#include <iomanip>
//#include <cmath>
#include <bits/stdc++.h>
/********define********/
const int MAX = 510000;
const int MOD = 1000000007;
#define rep(i,x) for(long long i=0;i<x;i++)
#define repn(i,x) for(long long i=1;i<=x;i++)
#define rrep(i,x) for(long long i=x-1;i>=0;i--)
#define rrepn(i,x) for(long long i=x;i>1;i--)
#define REP(i,n,x) for(long long i=n;i<x;i++)
#define REPN(i,n,x) for(long long i=n+1;i<x;i++)
#define pr printf
#define re return
#define mod 1000000007
//#define mod 998244353
#define inf INT_MAX//19桁
#define INF 1e18+5//19桁
const double PI=3.14159265358979323846;
#define fi first
#define se second
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define all(x) (x).begin(),(x).end()
typedef long long int ll;
typedef pair<long long, long long> P;
/********変数宣言********/
//vector<long long> g[200020];
vector<pair<long long,long long>> g[200020];
ll s[200020];
bool used[200020];
//bool dp[100005];
ll A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,Q,R,S,T,U,V,W,X,Y,Z;
double dA,dB,dC,dD,dE,dF,dG,dH,dI,dJ,dK,dL,dM,dN,dO,dP,dQ,dR,dS,dT,dU,dV,dW,dX,dY,dZ;
string sa,sb,sc,sd,se,sf,sg,sh,si,sj,sk,sl,sm,sn,so,sp,sq,sr,ss, su,sv,sw,sx,sy,sz;
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
cin>>N;cin>>M;
vector<string>v(M);
vector<pair<long long, string> > p(M);
//sort(p.begin(), p.end());
//sort(p.begin(), p.end(),greater<pair<long long,long long> >());
rep(j,M){
cin>>v[j];
}
rep(i,M){
p[i].fi=v[i].size();
p[i].se=v[i];
}
//sort(p.begin(), p.end());
ll check;
check=0;
ll i;
i=0;
ll flg;
while(check<N*N && i<M){
if(check%N!=0){
if(p[i-1].se[p[i-1].se.size()-1]==p[i].se[0]){
rep(j,p[i].se.size()-1){
p[i].se[j]=p[i].se[j+1];
}
p[i].se.resize(p[i].se.size()-1);
}
}
if(check%N+p[i].se.size()<=N){
cout<<p[i].se;
check+=p[i].se.size();
if(check%N==0){
cout<<"\n";
}
}
else{
while(check%N!=0){
cout<<".";
check++;
}
cout<<"\n";
}
i++;
}
re 0;
}
| #include<bits/stdc++.h>
#define ll long long
#define dbg(x) cout<<#x<<": "<<x<<endl;
#define N 2005
#define M 1000000007
#define pii pair<ll,ll>
#define fast ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
string s[2005];
int h,w;
ll dp[N][N][5];
bool valid(int i,int j)
{
return i>=0&&i<h&&j>=0&&j<w&&s[i][j]=='.';
}
ll run(int i,int j,int c)
{
if(i==h-1&&j==w-1)
{
return 1;
}
if(dp[i][j][c]!=-1)
return dp[i][j][c];
ll ans=0;
if(c==0)
{
if(valid(i,j+1)) ans=(ans+run(i,j+1,1));
if(valid(i+1,j)) ans=(ans+run(i+1,j,2));
if(valid(i+1,j+1)) ans=(ans+run(i+1,j+1,3));
}
else if(c==1)
{
ans=(ans+run(i,j,0));
if(valid(i,j+1)) ans=(ans+run(i,j+1,1));
}
else if(c==2)
{
ans=(ans+run(i,j,0));
if(valid(i+1,j)) ans=(ans+run(i+1,j,2));
}
else if(c==3)
{
ans=(ans+run(i,j,0));
if(valid(i+1,j+1)) ans=(ans+run(i+1,j+1,3));
}
ans%=M;
return dp[i][j][c]=ans;
}
main()
{
memset(dp,-1,sizeof dp);
cin>>h>>w;
for(int i=0;i<h;i++)
cin>>s[i];
cout<<run(0,0,0);
}
|
#include <bits/stdc++.h>
#define M_PI 3.14159265358979323846 /* pi */
using namespace std;
using ll = long long;
int main() {
int N;
int A;
int B;
cin >> N;
cin >> A;
cin >> B;
cout << N - A + B << endl;
} | // Code for A - box
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define FOR(i,x,y) for(ll i=x;i<(ll)(y);i++)
#define MOD 1000000007
#define mod(x) ( (x) % MOD + MOD ) % MOD
#define debug(var) do{std::cout << #var << " : ";view(var);}while(0)
template<typename T> void view(T e){std::cout << e << std::endl;}
template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cout << e << " "; } std::cout << std::endl;}
template<typename T> void view(const std::vector<std::vector<T> >& vv){ for(const auto& v : vv){ view(v); } }
template<typename T, typename U> void view(const std::map<T, U>& m){ for(const auto& e : m){ std::cout << e.first << "=>" << e.second << " "; } std::cout << std::endl; }
int main() {
int N, A, B;
cin >> N >> A >> B;
cout << N - A + B << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = a; i < b; ++i)
int debug = 0;
const int N = 5e5;
int n, strong, cur, start, was[N], a[N], b[N], p[N];
vector <array <int, 2>> ans;
void fail() {
puts("-1");
exit(0);
}
void sw(int i, int j) {
swap(p[i], p[j]);
ans.push_back({i, j});
}
main() {
cin >> n;
rep(i, 1, n + 1) { cin >> a[i]; }
rep(i, 1, n + 1) { cin >> b[i]; }
rep(i, 1, n + 1) { cin >> p[i]; }
rep(i, 1, n + 1) {
if (i != p[i] && a[i] <= b[p[i]]) {
fail();
}
}
rep(i, 1, n + 1) {
if (debug) {
cerr << " i: "<< i << '\n';
}
if (!was[i]) {
strong = i;
start = i;
was[start] = 1;
cur = p[start];
while (cur != start) {
was[cur] = 1;
if (a[cur] > a[strong]) {
strong = cur;
}
cur = p[cur];
}
while (p[strong] != strong) {
if (debug) { cerr << " strong: " << strong << ' ' << p[strong] << '\n'; }
sw(strong, p[strong]);
}
}
}
cout << ans.size() << '\n';
for (auto i : ans) {
cout << i[0] << ' ' << i[1] << '\n';
}
} |
#pragma GCC optimize("O3")
//#pragma GCC target("avx2")
//#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ld> vld;
typedef vector<bool> vb;
typedef pair<ll,ll> Pll;
typedef pair<int,int> Pin;
ll INF = 1e16;
int inf = 1e9;
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, m, n) for (ll i = (m); i < (n); ++i)
#define REVFOR(i, m, n) for (ll i = ((n) - 1); i >= (m); --i)
#define REP(i, n) FOR(i, 0, n)
#define REVREP(i, n) REVFOR(i, 0, n)
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define bcnt __builtin_popcountll
// inputs
#define VLLIN(x, n) vll x(n); REP(i, n) cin >> x[i];
#define VIIN(x, n) vin x(n); REP(i, n) cin >> x[i];
#define VLDIN(x, n) vld x(n); REP(i, n) cin >> x[i];
#define SIN(s, n) string s; cin >> s;
#ifdef LOCAL
#include <prettyprint.hpp>
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", d_err(__VA_ARGS__);
#else
#define debug(...) 83;
#endif
void d_err() {
cerr << endl;
}
template <typename H, typename... T>
void d_err(H h, T... t) {
cerr << h << " ";
d_err(t...);
}
template <typename T>
void print(T x) {
cout << x << "\n";
}
template <typename T>
void print(vector<T>& x) {
int N = x.size();
REP(i, N) {
if (i > 0) cout << " ";
cout << x[i];
}
cout << "\n";
}
int main(){
cin.tie(0);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(20);
int n; cin >> n;
string s1, s2; cin >> s1 >> s2;
int c1 = 0, c2 = 0;
REP(i, n) {
c1 += s1[i] == '1';
c2 += s2[i] == '1';
}
if (c1 != c2) {
print(-1);
return 0;
}
stack<int> q1, q2;
REP(i, n) {
if (s1[i] == '0') q1.push(i);
if (s2[i] == '0') q2.push(i);
}
int ans = 0;
assert(q1.size() == q2.size());
while(q1.size()) {
auto a = q1.top(); q1.pop();
auto b = q2.top(); q2.pop();
ans += a != b;
}
print(ans);
}
|
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <functional>
#include <cmath>
#include <set>
#include <queue>
#include <deque>
#include <vector>
#include <climits>
#include <sstream>
#include <iomanip>
#include <map>
#include <stack>
using namespace std;
using ui = unsigned int;
using ull = unsigned long long;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vvvi = vector<vector<vector<int>>>;
using vui = vector<ui>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vvvl = vector<vector<vector<ll>>>;
template <typename T>
using vv = vector<vector<T>>;
template <typename T>
using vvv = vector<vector<vector<T>>>;
int main()
{
int N, M;
cin >> N >> M;
vi A(N);
vi B(M);
for (auto& i : A)
{
cin >> i;
}
for (auto& i : B)
{
cin >> i;
}
vvi dp(N + 1, vi(M + 1, INT_MAX));
dp[0][0] = 0;
for (auto i = 0; i <= N; ++i)
{
for (auto j = 0; j <= M; ++j)
{
if (dp[i][j] == INT_MAX)
{
continue;
}
if (i < N)
{
dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + 1);
}
if (j < M)
{
dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + 1);
if (i < N)
{
auto temp = dp[i][j];
if (A[i] != B[j])
{
++temp;
}
dp[i + 1][j + 1] = min(dp[i + 1][j + 1], temp);
}
}
}
}
cout << dp[N][M] << endl;
return 0;
} | #include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <iomanip>
#include <functional>
#include <deque>
#include <stack>
#include <queue>
#include <bitset>
#include <map>
#include <set>
#include <unordered_set>
using namespace std;
typedef long long ll;
#define rep(i,n) for (ll i = 0; i < (ll)(n); i++)
#define REP(i,n) for (ll i = 1; i <= (ll)(n); i++)
#define FOR(i,a,b) for(ll i=a;i<=b;i++)
#define all(v) v.begin(), v.end()
#define pb push_back
#define vt vector
#define fcout cout<<fixed<<setprecision(15)
#define PI 3.1415926535897932384626433832795028841971693993751058209749445923078160628620899
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ll INF = 1000000000000000000;
const ll NIL = -1;
const ll dx[4] = { 0,1,0,-1 };
const ll dy[4] = { 1,0,-1,0 };
ll N, X, A[110];
int main() {
cin >> N >> X;
rep(i, N) cin >> A[i];
ll ANS = INF;
for (ll c = 1; c <= N; c++) {
ll dp[105][105][105];
ll q = X % c;
FOR(i, 0, N)FOR(j, 0, N)FOR(k, 0, c - 1) dp[i][j][k] = -1000000000000000000;
FOR(i, 0, N) dp[i][0][0]=0;
FOR(i, 1, N)FOR(j, 1, i)FOR(k, 0, c - 1) {
ll p = k + c - (A[i - 1] % c);
p %= c;
if (i == j) {
dp[i][j][k] = A[i - 1] + dp[i - 1][j - 1][p];
}
else {
dp[i][j][k] = max(dp[i - 1][j][k], A[i - 1] + dp[i - 1][j - 1][p]);
}
}
ll k = dp[N][c][q];
if (k > 0) {
ll tmp = (X - k) / c;
ANS = min(ANS, tmp);
}
}
cout << ANS << endl;
return 0;
}
|
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
#define mp make_pair
#define pb push_back
#define T ll t; cin>>t; while(t--)
#define print(name) for(auto t:name){cout<<t<<" ";}cout<<endl;
#define mod 1000000007
#define mod1 998244353
#define inf 1000000000000000000
#define mem(name,val) memset(name,val,sizeof(name))
#define f(n) for(ll i=0;i<n;i++)
#define rep(i,a,n) for(ll i=a;i<n;i++)
#define endl "\n"
ll gcd(ll a,ll b){if(b==0){return a;}return gcd(b,a%b);}
ll g1(ll n)
{
string s=to_string(n);
sort(s.begin(),s.end(),greater<char>());
ll k1=stoll(s);
return k1;
}
ll g2(ll n)
{
string s=to_string(n);
sort(s.begin(),s.end());
ll k1=stoll(s);
return k1;
}
ll func(ll n)
{
return g1(n)-g2(n);
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll n,k;cin>>n>>k;
//map<ll,ll>m;
for(int i=1;i<=k;i++)
{
n=func(n);
}
cout<<n<<endl;
} | #include<bits/stdc++.h>
using namespace std;
int a,b,c,d,e,i,j,ii,jj,zx,xc,f[1500009],pas;
multiset <int> s,h;
multiset <int>::iterator it,tt;
void ins(int q){
it=h.lower_bound(q);
if(it!=h.end()&&(*it)==q){
h.erase(it);
}
s.insert(q);
}
void er(int q){
it=s.lower_bound(q);
s.erase(it);
it=s.lower_bound(q);
if(it!=s.end()&&(*it)==q){
}else{
h.insert(q);
}
}
void chk(){
tt=h.begin();
if(pas>(*tt)) pas=(*tt);
}
int main(){
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>a>>b;
pas=a+2;
for(i=1; i<=a; i++){
cin>>f[i];
}
for(i=0; i<=a+2; i++){
h.insert(i);
}
for(i=1; i<=b; i++){
//s.insert(f[i]);
ins(f[i]);
}
chk();
for(i=b+1; i<=a; i++){
c=f[i-b];
/*it=s.lower_bound(c);
s.erase(it);*/
er(c);
//s.insert(f[i]);
ins(f[i]);
chk();
}
cout<<pas;
return 0;
} |
#include <assert.h>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <string>
#include <vector>
#include <set>
#include <queue>
#include <map>
using namespace std;
#define all(x) x.begin(), x.end()
const long long mod = (long long) (1e9 + 7);
long long powerf(long long a, long long b, long long c = mod) {
long long res = 1;
while (b > 0) {
if (b%2 != 0)
res = (res * a) % c;
b/=2;
a = (a * a) % c;
}
return res;
}
template<class T>
T gcd(T a, T b) {
return (a == 0 ? b : gcd(b%a, a));
}
const int N = (int)(1e6 + 6);
vector<long long> mul(N, 0);
vector<long long> tmp1(N, 0);
long long nonCoPrimes = 0;
long long L, R;
auto nc2 = [](long long x) {
return (x * (x - 1)) / 2;
};
void init() {
for (long long x = 2; x < N; x++) {
for (long long y = x; y < N; y+=x) {
if (y >= L && y <= R)
mul[x]++;
}
}
for (long long x = N-1; x >= 2; x--) {
long long cur = 0;
cur = nc2(mul[x]);
for (long long y = 2*x; y < N; y+=x) {
cur -= tmp1[y];
}
tmp1[x] = cur;
nonCoPrimes += tmp1[x];
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> L >> R;
init();
long long completelyDivPairs = 0;
for (long long x = max(L, 2LL); x <= R; x++) {
completelyDivPairs += mul[x] - 1;
}
cout << 2 * (nonCoPrimes - completelyDivPairs) << "\n";
return 0;
}
| #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll x,y;
unordered_map<ll,ll> um;
ll solve(ll num){
if(num==1) return abs(x-num);
if(um.find(num)!=um.end()) return um.find(num)->second;
ll result;
if(num%2) result = min(abs(x-num),min(solve((num+1)/2)+2,solve((num-1)/2)+2));
else result = min(abs(x-num),solve(num/2)+1);
um.insert({num,result});
return result;
}
int main(void){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> x >> y;
cout << solve(y);
return 0;
} |
#include <iostream>
#define ll long long
#define f first
#define s second
#define inf 2000000000
using namespace std;
int n, m, nod[20][20], dp[(1<<18)+10], isOK[(1<<18)+10];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int i=1; i<=m; i++)
{ int nod1, nod2;
cin >> nod1 >> nod2;
nod[nod1][nod2] = nod[nod2][nod1] = 1;
}
for(int mask=1; mask<(1<<n); mask++)
{ int cnt = 0, nrbiti = 0;
for(int bit1=0; bit1<n; bit1++)
if(mask & (1 << bit1))
{ nrbiti++;
for(int bit2=bit1+1; bit2<n; bit2++)
if(mask & (1 << bit2) && nod[bit1+1][bit2+1]) cnt++;
}
if(cnt == nrbiti * (nrbiti - 1) / 2) isOK[mask] = 1;
}
for(int mask=1; mask<(1<<n); mask++)
{ dp[mask] = inf;
if(isOK[mask])
{ dp[mask] = 1;
continue;
}
int m = mask;
while(m)
{ m = (m - 1) & (mask);
dp[mask] = min(dp[mask], dp[m] + dp[mask^m]);
}
}
cout << dp[(1<<n)-1] << '\n';
return 0;
}
| #pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
#define itn int
#define ll long long
#define ull unsigned long long
using namespace std;
ll read(){
ll a=0,b=1;char c=getchar();
while(c>'9'||c<'0'){if(c=='-')b=-1;c=getchar();}
while(c>='0'&&c<='9')a=a*10+c-48,c=getchar();
return a*b;
}
const ll mod=1000000007;
ll n,ans;
struct edge{
int v;
ull w;
int nx;
}e[400005];
int hd[200005],cnt;
ull xr[200005],x[200005];
void dfs(int u,int rt,ull v){
xr[u]=v;
for(int i=hd[u];i;i=e[i].nx){
if(e[i].v!=rt){
dfs(e[i].v,u,v^e[i].w);
}
}
}
ll r[200005][64];
int main(){
n=read();
for(int i=1;i<n;i++){
int u=read(),v=read();
ull w=read();
e[++cnt]=edge{v,w,hd[u]},hd[u]=cnt;
e[++cnt]=edge{u,w,hd[v]},hd[v]=cnt;
}
dfs(1,0,0);
ll ans=0;
for(int i=1;i<=n;i++){
for(int j=0;j<64;j++)r[i][j]=r[i-1][j];
ull k=xr[i];
int ct=0;
while(k){
r[i][ct]+=(k&1);
k>>=1;ct++;
}
}
for(int i=1;i<=n;i++){
//cout<<i<<" :\n";
for(int j=0;j<64;j++){
if(xr[i]&(1ull<<j)){
ans+=(i-1-r[i-1][j])*((1ull<<j)%mod)%mod;
ans%=mod;
//if(i-1-r[i-1][j])cout<<j<<' '<<ans<<'\n';
}else{
ans+=(r[i-1][j])*((1ull<<j)%mod)%mod;
ans%=mod;
//if(r[i-1][j])cout<<r[i-1][j]<<' '<<j<<' '<<ans<<'\n';
}
}
//cout<<xr[i]<<' '<<ans<<'\n';
}
cout<<ans<<'\n';
return 0;
}
|
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define MO 1000000007
#define mem(a,s) memset(a,s,sizeof(a))
#define IOS ios_base::sync_with_stdio(0); cin.tie(NULL);
#define lop(i,s,e) for(int i=s;i<e;i++)
#define lopi(i,s,e) for(int i=s;i>=e;i--)
#define prina(a,n) for(int i=0;i<=n;i++)cout<<a[i]<<" ";cout<<endl;
#define prin2da(a,n,m) lop(i,1,n){lop(j,1,m)cout<<a[i][j]<<" ";cout<<endl;}
#define atout(v) for(auto x:v) cout<<x<<" ";cout<<endl;
#define atin(v) for(auto &x:v) cin>>x;
#define vl vector<ll >
#define vi vector<int >
#define lb lower_bound
#define ub upper_bound
#define sort(a) sort(a.begin(),a.end())
#define mp make_pair
#define all(v) v.begin(),v.end()
#define pll pair<ll,ll>
using namespace std;
bool check(ll l,ll r,vector<vector<ll>> &p,vector<ll> &b) {
ll n=p.size();
lop(i,l,r+1) b[i]=0;
lop(i,0,n) {
ll x=p[i][0],y=p[i][1];
if(x!=-1 and y!=-1) {
if(y<l or x>r) continue;
if(y-x!=(r-l+1)/2 or b[x]) return false;
b[x]=1;
}
else if(x!=-1 and y==-1) {
if(x>r or x<l) continue;
if(x>=(r+l+1)/2 or b[x]) return false;
b[x]=1;
}
else if(x==-1 and y!=-1) {
if(y<l or y>r) continue;
x=y-((r-l+1)/2);
if(y<(r+l+1)/2 or b[x]) return false;
b[x]=1;
}
}
return true;
}
void solve() {
ll n;
cin>>n;
vector<vector<ll>> p(n,vector<ll>(2,0));
vector<bool> dp(2*n+1,false);
vector<ll> b(2*n+1);
lop(i,0,n) cin>>p[i][0]>>p[i][1];
dp[0]=true;
for(int i=0 ; i<(2*n)+1 ; i+=2) {
for(int j=i+2 ; j<=(2*n)+1 ; j+=2) {
//cout<<check(i+1,j,p,b)<<endl;
dp[j]=dp[j]or(dp[i] and check(i+1,j,p,b));
}
}
//atout(dp);
cout<<((dp[2*n])?"Yes":"No")<<endl;
return;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll t=1;
while(t--) {
solve();
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<bool> ju(2*n+1);
vector<int> a(n),b(n);
bool res=true;
for(int i=0;i<n;i++){
cin >> a[i] >> b[i];
a[i]--,b[i]--;
if(a[i]>=0 && ju[a[i]]) res=false;
if(b[i]>=0 && ju[b[i]]) res=false;
if(a[i]>=0) ju[a[i]]=true;
if(b[i]>=0) ju[b[i]]=true;
if(a[i]>=0 && b[i]>=0 && a[i]>=b[i]) res=false;
}
if(!res){
cout << "No" << "\n";
return 0;
}
vector<bool> dp(2*n+1);
dp[2*n]=true;
for(int i=2*n-1;i>=0;i--){
for(int j=1;j<2*n;j++){
if((i+2*j)>2*n) continue;
if(!dp[i+2*j]) continue;
if(dp[i]) continue;
//i .. i+2*j-1
vector<bool> ta(2*n);
bool bo=true;
int up=i+2*j-1;
int down=i;
for(int k=0;k<n;k++){
if(a[k]<0 && b[k]<0) continue;
if(b[k]>=0 && b[k]<down) continue;
if(a[k]>=0 && a[k]>up) continue;
if(b[k]>=0 && a[k]<0 && b[k]>up) continue;
if(a[k]>=0 && b[k]<0 && a[k]<down) continue;
if(a[k]>=0 && b[k]>=0 && a[k]<down && b[k]>up){
bo=false;
break;
}
if(a[k]>=0 && b[k]>=0){
if((b[k]-a[k])!=j || ta[b[k]] || ta[a[k]] || a[k]<down || b[k]>up){
bo=false;
break;
}
ta[a[k]]=true;
ta[b[k]]=true;
}else if(a[k]>=0){
int bb=a[k]+j;
if(bb>up || ta[a[k]] || ta[bb] || a[k]<down){
bo=false;
break;
}
ta[a[k]]=true;
ta[bb]=true;
}else{
int aa=b[k]-j;
if(aa<down || ta[aa] || ta[b[k]] || b[k]>up){
bo=false;
break;
}
ta[aa]=true;
ta[b[k]]=true;
}
}
for(int k=down;k<=up;k++){
if(ta[k]) continue;
if((k+j)>up) bo=false;
if(ta[k+j]) bo=false;
ta[k]=true;
ta[k+j]=true;
}
if(!bo) continue;
dp[i]=true;
//cout << i << " " << j << "\n";
}
//cout << dp[i] << "\n";
}
if(dp[0]) cout << "Yes" << "\n";
else cout << "No" << "\n";
return 0;
} |
#include<bits/stdc++.h>
#define LL long long
using namespace std;
LL Pow(LL x,LL y,int mod)
{
if(y==0) return 1ll;
if(y==1) return x;
LL a=Pow(x,y>>1,mod);
if(y&1) return (((a*a)%mod)*x)%mod;
return (a*a)%mod;
}
int main()
{
LL a,b,c;
cin>>a>>b>>c;
LL x=Pow(b%4,c,4)+4;
cout<<Pow(a%10,x,10);
}
| #include <bits/stdc++.h>
#include <cmath>
using ll = long long;
const ll INF = __LONG_LONG_MAX__;
using namespace std;
int main() {
long double x, y, r;
cin >> x >> y >> r;
long double e = 1e-9;
//cout << e << endl;
ll ans = 0ll;
for(ll i = (ll)(floor(y+r)); i >= (ll)(ceil(y-r)); i--) {
long double val = sqrt(r * r - (i-y)*(i-y) + e);
ll l = ceil(x - val);
ll r = floor(x + val);
/*cout << i << " " << val << endl;
cout << l << " " << r << endl;*/
ans += r - l + 1;
}
cout << ans << endl;
return 0;
} |
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define NDEBUG
#define eprintf(...) do {} while (0)
#endif
#include<cassert>
using namespace std;
typedef long long LL;
typedef vector<int> VI;
#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)
template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }
#define rprintf(fmt, begin, end) do { const auto end_rp = (end); auto it_rp = (begin); for (bool sp_rp=0; it_rp!=end_rp; ++it_rp) { if (sp_rp) putchar(' '); else sp_rp = true; printf(fmt, *it_rp); } putchar('\n'); } while(0)
int N;
LL A[101];
LL X;
LL dp[101][101];
void MAIN() {
scanf("%d%lld", &N, &X);
REP (i, N) scanf("%lld", A+i);
LL ans = 1LL<<60;
REP (i, N) {
amin(ans, X - A[i]);
}
for (int B=2; B<=N; B++) {
memset(dp, 0xc0, sizeof dp);
dp[0][0] = 0;
REP (i, N) {
int v = A[i] % B;
for (int c=i; c>=0; c--) {
REP (j, B) {
amax(dp[c+1][(j+v) % B], dp[c][j] + A[i]);
}
}
}
REP (j, B) if (dp[B][j] >= 0 && (X-j) % B == 0) {
assert((X-dp[B][j]) % B == 0);
LL tmp = (X-dp[B][j]) / B;
amin(ans, tmp);
}
}
if (ans == (1LL<<60)) {
puts("-1");
} else {
printf("%lld\n", ans);
}
}
int main() {
int TC = 1;
// scanf("%d", &TC);
REP (tc, TC) MAIN();
return 0;
}
| #include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <complex>
using namespace std;
//#pragma GCC optimize("Ofast")
//#pragma GCC optimization("unroll-loops, no-stack-protector")
//#pragma GCC target("avx,avx2,fma")
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
long long n, x, i, j, k, kk, ans, INF = 1LL << 60, kv;
cin >> n >> x;
vector<long long> a(n);
for (i = 0; i < n; i++) cin >> a[i];
ans = INF;
for (kk = 1; kk <= n; kk++) {
// step, count, mod kk
vector<vector<vector<long long>>> dp(n + 1, vector<vector<long long>>(kk + 1, vector<long long>(kk, -INF)));
dp[0][0][0] = 0;
for (i = 0; i < n; i++) {
for (j = 0; j <= kk; j++) {
for (k = 0; k < kk; k++) {
dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j][k]);
kv = (k + a[i]) % kk;
if (j < kk) dp[i + 1][j + 1][kv] = max(dp[i + 1][j + 1][kv], dp[i][j][k] + a[i]);
}
}
}
if (dp[n][kk][x % kk] > 0) {
ans = min(ans, (x - dp[n][kk][x % kk]) / kk);
}
}
cout << ans << "\n";
return 0;
}
|
// .-""""-.
// / j \
// :.d; ;
// $P :
// .m._ $$ :
// dSMMSSSss.__$b. __ :
// :MMSMMSSSMMMSS$$b $P ;
// SMMMSMMSMMMSSS$$$$ :b
// dSMMMSMMMMMMSSMM$$b.dP SSb.
// dSMMMMMMMMMMSSMMPT$$=-. /TSSSS.
// :SMMMSMMMMMMMSMMP `b_.' MMMMSS.
// SMMMMMSMMMMMMMMM \ .'\ :SMMMSSS.
// dSMSSMMMSMMMMMMMM \/\_/; .'SSMMMMSSSm
// dSMMMMSMMSMMMMMMMM :.;'" :SSMMMMSSMM;
// .MMSSSSSMSSMMMMMMMM; :.; MMSMMMMSMMM;
// dMSSMMSSSSSSSMMMMMMM; ;.; MMMMMMMSMMM
// :MMMSSSSMMMSSP^TMMMMM ;.; MMMMMMMMMMM
// MMMSMMMMSSSSP `MMMM ;.; :MMMMMMMMM;
// "TMMMMMMMMMM TM; :`.: MMMMMMMMM;
// )MMMMMMM; _/\ :`.: :MMMMMMMM
// dSS$$MMMb. |._\\ :`.: MMMMMMMM
// T$S$$$$$$$$$m;O\\"-;`.:_.- MMMMMMM;
// :$$$$$$$$$$$$$$b_l./\ ;`.: mMMSSMMM;
// :$$$$$$$$$$$$$$$$$$$./\;`.: .$MSMMMMMM
// $$$$$$$$$$$$$$$$$$$$. \`.:.$$$SMSSSMMM;
// $$$$$$$$$$$$$$$$$$$$$. \.:$$$$SSMMMMMMM
// :$$$$$$$$$$$$$$$$$$$$$.//.:$$$SSSSSSSMM;
// :$$$$$$$$$$$$$$$$$$$$$$.`.:$SSSSSSSMMMP
// $$$$$$$$$;"^J "^$$$$;.`.$P `SSSMMMM
// :$$$$$$$$$ :$$$;.`.P'.. TMMM$b
// :$$$$$$$$$; $$$$;.`/ c^' d$$$$S;
// $$$$S$$$$; '^^^:_dg:___.$$$$$SSS
// $$$SS$$$$; $$$$$$$$$$$$$SSS;
// :$$SSSS$$$$ : $$$$$$$$$$$$SSS
// :P"TSSSS$$$ ; $$$$$$$$$$$$SSS;
// j `SSSSS$ : :$$$$$$$$$$$$SS$
// : "^S^' : $$$$$$$$$$$$S$;
// ;.____.-;" "--^$$$$$$$$$$$$P
// '-....-" bug ""^^T$$$P"
#include<bits/stdc++.h>
#define pb push_back
#define mk make_pair
#define ll long long
#define ss second
#define ff first
#define pll pair<ll,ll>
#define vll vector<ll>
#define mll map<ll,ll>
#define mod 1000000007
#define sp " "
#define w(x) ll x; cin>>x; while(x--)
#define ps(x,y) fixed<<setprecision(y)<<x;
#define fo(i, j, k, in) for (ll i=j ; i<k ; i+=in)
#define re(i, j) fo(i, 0, j, 1)
#define pi 3.1415926535897932384626433832795
#define all(cont) cont.begin(), cont.end()
#define countbit(x) __builtin_popcount(x)
#define mod 1000000007
#define lo lower_bound
#define de(n) ll n;cin>>n;
#define def(a,n) ll n;cin>>n;ll a[n];re(i,n){cin>>a[i];}
#define defi(a,n,k) ll n;cin>>n; ll k;cin>>k;ll a[n];re(i,n){cin>>a[i];}
#define deb(x) cout<<#x<<"="<<x<<endl;
#define tr(it,a) for(auto it=a.begin();it!=a.end();it++)
#define nl cout<<endl;
using namespace std;
//KnightMareVoid
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout<<setprecision(18);
cout<<fixed;
double a;
double b;
cin>>a>>b;
double c=a-b;
double ans=(100*c)/a;
cout<<ans<<endl;
return 0;
}
| #include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
#define fp(x, a, b) for(int x = a; x < b; x++)
#define fn(x, a, b) for(int x = a; x > b; x--)
#define f(x, m) for(auto x : m)
#define cpu() ios::sync_with_stdio(false); cin.tie(nullptr)
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pair<int ,int>>
#define vll vector<pair<ll ,ll>>
#define all(v) v.begin(),v.end()
#define sor(a) sort( a.begin(), a.end() )
#define ros(a) sort( a.rbegin(), a.rend())
#define prec(n) fixed << setprecision(n)
#define ff first
#define ss second
// #define tt third
#define print(x) for(auto it : x) cout << it << " ";
#define debug(x) cerr << #x << " is " << x << endl;
// #define ordered_multiset tree<int, null_type, less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
typedef long long ll;
using namespace std;
// using namespace __gnu_pbds;
#define dbg(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) {cout << "NEXT\n"; }
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << ", ";
err(++it, args...);
}
template<typename... T>
void rd(T& ... args){
((cin >> args), ...);
}
template<typename... T>
void ps(T ... args){
((cout << args << ' '), ...);
cout<<'\n';
}
const ll MOD = 1e9 + 7, MOD1 = 998244353LL, MAX = 1e2 + 5;
const char nl = '\n';
const int INF = 1e9 + 5;
int n, s, d, x, y;
void solve(){
cin >> n >> s >> d;
for(int i = 0; i < n; i++){
cin >> x >> y;
if(!(x >= s || y <= d)){
cout << "Yes\n";
return;
}
}
cout << "No\n";
}
int main(){
cpu();
// int __;
// cin >> __;
// while(__--){
// solve();
// }
solve();
// cout << nl;
return 0;
}
|
# include <bits/stdc++.h>
using namespace std;
int main(){
int N, K;
cin >> N >> K;
cout << 50*N*(N+1)*K + K*(K+1)*N/2 << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main(void) {
int n, k;
long long int sum = 0;
cin >> n >> k;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= k; j++) {
sum += i*100+j;
}
}
cout << sum << endl;
} |
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
using namespace __gnu_pbds;
using namespace std;
#define int long long
#define endl "\n"
#define deb(x) cout<<#x<<" "<<x<<endl;
#define sc(ar,n) for(int pen=0;pen<n;pen++){ cin>>ar[pen];}
#define pr(ar,n) for(int pen=0;pen<n;pen++){ cout<<ar[pen]<<" ";} cout<<endl;
#define fr(i,x,n) for(int i=x;i<n;i++)
#define frt(it,vb) for(auto it=vb.begin();it!=vb.end();it++)
#define mem(ar,x) memset(ar,x,sizeof(ar));
#define pb push_back
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mod 1000000007
#define rt return 0;
#define ct continue;
#define MAX 1000000000000000000
#define MAX1 1000000000
#define CLK clock_t clk = clock();//Start of main
#define OCLK cerr << "Time (in ms): " << (double)(clock() - clk) * 1000.0 / CLOCKS_PER_SEC << '\n';//End of main
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
inline int solve()
{
int n;
cin>>n;
int ar[n];
sc(ar,n);
int mn = MAX;
fr(i,0,n) mn = min(mn , ar[i]);
map <int,vector<int> > mp;
fr(i,0,n){
int x = ar[i];
for(int j=1;j<=sqrt(x);j++){
if(x%j == 0){
mp[j].push_back(i);
if(j != x/j)
mp[x/j].push_back(i);
}
}
}
int ans = 0;
frt(it,mp){
if(it->first > mn) break;
int g = 0;
frt(ut,it->second){
g = __gcd(g , ar[*ut]);
}
if(g == it->first){
ans++;
}
}
cout << ans << endl;
rt;
}
signed main()
{
// fastio;
int t;
int test_cases=1;
if(test_cases==0)
cin>>t;
else
t=1;
while(t--){
solve();
}
} | #include <bits/stdc++.h>
using namespace std;
#define stp(var, init, end) for (auto var = init; var < end; ++var)
#define stpe(var, init, end) for (auto var = init; var <= end; ++var)
#define ll long long
int main(void)
{
int n, rnum = 0, gnum = 0, bnum = 0;
cin >> n;
vector<ll> rdogs;
vector<ll> gdogs;
vector<ll> bdogs;
ll ina;
char inc;
stp(i, 0, 2 * n)
{
cin >> ina >> inc;
if (inc == 'R')
{
rdogs.emplace_back(ina);
++rnum;
}
else if (inc == 'G')
{
gdogs.emplace_back(ina);
++gnum;
}
else if (inc == 'B')
{
bdogs.emplace_back(ina);
++bnum;
}
}
sort(rdogs.begin(), rdogs.end());
sort(gdogs.begin(), gdogs.end());
sort(bdogs.begin(), bdogs.end());
/*
cout << "R: ";
stp(i, 0, rnum)
{
cout << rdogs[i] << " ";
}
cout << "\n";
cout << "G: ";
stp(i, 0, gnum)
{
cout << gdogs[i] << " ";
}
cout << "\n";
cout << "B: ";
stp(i, 0, bnum)
{
cout << bdogs[i] << " ";
}
cout << "\n\n";
*/
if (rnum % 2 == 0 && gnum % 2 == 0 && bnum % 2 == 0)
{
//cout << "even\n";
cout << 0;
}
else
{
ll rgmin = 10000000000000000ll, gbmin = 10000000000000000ll, brmin = 10000000000000000ll;
if (gnum > 0)
{
stp(i, 0, rnum)
{
auto iter = lower_bound(gdogs.begin(), gdogs.end(), rdogs[i]);
if (iter == gdogs.begin())
rgmin = min(rgmin, abs(rdogs[i] - *iter));
else if (iter == gdogs.end())
rgmin = min(rgmin, abs(rdogs[i] - *(iter - 1)));
else
rgmin = min(rgmin, min(abs(rdogs[i] - *iter), abs(rdogs[i] - *(iter - 1))));
}
}
if (bnum > 0)
{
stp(i, 0, gnum)
{
auto iter = lower_bound(bdogs.begin(), bdogs.end(), gdogs[i]);
if (iter == bdogs.begin())
gbmin = min(gbmin, abs(gdogs[i] - *iter));
else if (iter == bdogs.end())
gbmin = min(gbmin, abs(gdogs[i] - *(iter - 1)));
else
gbmin = min(gbmin, min(abs(gdogs[i] - *iter), abs(gdogs[i] - *(iter - 1))));
}
}
if (rnum > 0)
{
stp(i, 0, bnum)
{
auto iter = lower_bound(rdogs.begin(), rdogs.end(), bdogs[i]);
if (iter == rdogs.begin())
brmin = min(brmin, abs(bdogs[i] - *iter));
else if (iter == rdogs.end())
brmin = min(brmin, abs(bdogs[i] - *(iter - 1)));
else
brmin = min(brmin, min(abs(bdogs[i] - *iter), abs(bdogs[i] - *(iter - 1))));
}
}
if (rnum % 2 == 1 && gnum % 2 == 1)
{
if (bnum > 0)
cout << min(rgmin, gbmin + brmin);
else
cout << rgmin;
}
else if (gnum % 2 == 1 && bnum % 2 == 1)
{
if (rnum > 0)
cout << min(gbmin, brmin + rgmin);
else
cout << gbmin;
}
else if (bnum % 2 == 1 && rnum % 2 == 1)
{
if (gnum > 0)
cout << min(brmin, rgmin + gbmin);
else
cout << brmin;
}
}
return 0;
} |
#include <bits/stdc++.h>
#define rep(i,N) for(int i=0; i<(N); i++)
using namespace std;
int main() {
int N;
cin >> N;
vector<int> a(N);
rep(i,N) cin >> a.at(i);
int ans=1<<30;
rep(s,1<<(N-1)){
int o=0;
int now=0;
rep(i,N){
o |= a.at(i);
if(s>>i&1){
now ^= o;
o = 0;
}
}
now ^= o;
ans = min(ans,now);
}
cout << ans << endl;
} | #include <bits/stdc++.h>
#define endl '\n'
#define umap unordered_map
#define all(var) var.begin(), var.end()
#define input freopen("input", "r", stdin)
#define output freopen("output", "w", stdout)
#define Fast ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL)
#define dbg(x) cerr << #x << " => " << x << endl
using Long = long long;
using namespace std;
signed main () {
Fast;
int n; cin >> n;
int arr[n]; for (int &i: arr) cin >> i;
vector<int> sets;
auto calc = [&] (vector<int>& v) {
int i = 0, idx = 0, Xor = 0;
while (idx < (int)v.size()) {
int Or = 0;
while (i < v[idx]) Or |= arr[i++];
Xor ^= Or;
idx++;
}
return Xor;
};
int ans = INT_MAX;
for (int i = 1; i <= (1 << (n - 1)); i++) {
for (int j = 0; j < n - 1; j++) {
if (i & (1 << j)) {
sets.push_back(j + 1);
}
}
sets.push_back(n);
ans = min(ans, calc(sets));
sets.clear();
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#define FASTIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using ll = long long;
const int MOD = 1e9+7;
const ll INF = 1e18;
const int MAXN = 1e6+5;
int main() {
FASTIO
ll n, m, t;
cin >> n >> m >> t;
vector<pair<ll,ll>> v(m);
for(auto& p : v) cin >> p.first >> p.second;
ll curr = 0, N = n;
for(auto p : v) {
N -= (p.first-curr);
if(N <= 0) break;
N = (N+(p.second-p.first) > n) ? n : N+(p.second-p.first);
curr = p.second;
}
N -= (t-curr);
if(N <= 0) cout << "No\n";
else cout << "Yes\n";
return 0;
} | #include<bits/stdc++.h>
using namespace std;
///******************************** C o n t a i n e r ********************************///
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
///*********************************** C o n s t ***********************************///
const int N=1e6+123;
const double PI = acos(-1);
const ll MOD=1000000007; ///1e9+7
ll dx[] = {+1, 0, -1, 0, +1, +1, -1, -1}; ///first 4 for adjacent
ll dy[] = {0, +1, 0, -1, +1, -1, +1, -1};
ll dx8[]= {+1, +1, -1, -1, +2, +2, -2, -2}; ///knights move
ll dy8[]= {+2, -2, +2, -2, +1, -1, +1, -1};
///********************************** M a r c o ***********************************///
#define pb push_back
#define MP make_pair
#define F first
#define S second
#define test ll tc; cin>>tc; while(tc--)
#define forn(i,n) for(i=0;i<n;i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define sz(x) x.size()
#define el <<'\n'
#define sp <<' '
#define print(a) {for(auto x:a)cout<<x<<" ";cout<<endl;}
#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 sqr(a) (a)*(a)
#define fastio() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define file() freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#define fraction() cout.unsetf(ios::floatfield); cout.precision(10); cout.setf(ios::fixed,ios::floatfield);
///*********************************** F u n c t i o n ***********************************///
ll powmod(ll a,ll b,ll MOD){ a%=MOD;if(!a) return 0;ll pr=1;while(b>0){if(b&1){pr*=a;pr%=MOD;--b;}a*=a;a%=MOD;b/=2;}return pr;}
ll modinverse(ll a){return powmod(a,MOD-2,MOD);}
bool isPrime(ll n){ if(n<=1)return false;if(n<=3)return true;if(n%2==0 or n%3==0)return false;for(ll i=5;i*i<=n;i+=6){if(n%i==0 or n%(i+2)==0)return false;}return true;}
void seive(bool a[]){vl v; ll mx=sqrt(N),ii,jj;for(ii=3;ii<=mx;ii+=2)if(!a[ii])for(jj=ii*ii;jj<N;jj+=2*ii)a[jj]=true; v.pb(2);for(ii=3;ii<N;ii+=2)if(!a[ii])v.pb(ii);}
void numofdiv(ll a[]){ll mx=sqrt(N),ii,jj;for(ii=1;ii<=mx;ii++){for(jj=ii*ii;jj<N;jj+=ii){if(jj==ii*ii) a[jj]++; else a[jj]+=2;}}}
void sumofdiv(ll a[]){ll mx=sqrt(N),ii,jj;for(ii=1;ii<=mx;ii++){for(jj=ii*ii;jj<N;jj+=ii){if(jj==ii*ii) a[jj]+=ii; else a[jj]+=ii+jj/ii;}}}
///**************************************************** C o d e ****************************************************///
///-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=///
void _case()
{
ll n,m,t,ans,i;
cin>>n>>m>>t;
ans=n;
i=0;
bool ok=false;
while(m--){
ll x,y;
cin>>x>>y;
ans-=(x-i);
if(ans<=0) ok=true,ans=0;
ans+=(y-x);
if(ans>=n) ans=n;
i=y;
}
ans-=(t-i);
if(ans<=0) ok=true,ans=0;
if(ok) cout<<"No"<<endl;
else cout<<"Yes"<<endl;
}
main()
{
fastio();
_case();
}
///See you soon
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(long long i=0;i<(long long)(n);i++)
#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)
#define all(a) a.begin(),a.end()
#define rsort(a) {sort(all(a));reverse(all(a));}
#define pb emplace_back
#define eb emplace_back
#define lb(v,k) (lower_bound(all(v),(k))-v.begin())
#define ub(v,k) (upper_bound(all(v),(k))-v.begin())
#define fi first
#define se second
#define pi M_PI
#define PQ(T) priority_queue<T>
#define SPQ(T) priority_queue<T,vector<T>,greater<T>>
#define dame(a) {out(a);return 0;}
#define decimal cout<<fixed<<setprecision(15);
#define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());}
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> PP;
typedef tuple<ll,ll,ll,ll> PPP;
typedef multiset<ll> S;
using vi=vector<ll>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
using vvvvi=vector<vvvi>;
using vp=vector<P>;
using vvp=vector<vp>;
using vb=vector<bool>;
using vvb=vector<vb>;
const ll inf=1001001001001001001;
const ll INF=1001001001;
const ll mod=1000000007;
const double eps=1e-10;
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';}
template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';}
template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);}
template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);}
template<class T> void yesno(T b){if(b)out("yes");else out("no");}
template<class T> void YesNo(T b){if(b)out("Yes");else out("No");}
template<class T> void YESNO(T b){if(b)out("YES");else out("NO");}
template<class T> void noyes(T b){if(b)out("no");else out("yes");}
template<class T> void NoYes(T b){if(b)out("No");else out("Yes");}
template<class T> void NOYES(T b){if(b)out("NO");else out("YES");}
void outs(ll a,ll b){if(a>=inf-100)out(b);else out(a);}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
ll modpow(ll a,ll b,ll MOD){ll res=1;a%=MOD;while(b){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
int main(){
ll n,m;cin>>n>>m;
ll a=modpow(10,n,m*m);
out(a/m);
} | #include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#define amax(a, b) a = std::max(a, b)
#define amin(a, b) a = std::min(a, b)
using ll = long long;
int main() {
ll N, K;
std::cin >> N >> K;
std::vector<ll> sum2cnt(2*N-1);
for (ll i = 0; i < 2*N-1; i++) {
sum2cnt[i] = std::min(i + 1, 2*N - i - 1);
}
// i, j, kをとりあえず0-indexedで出す
K--;
ll begin = 0, end = 1, ijk = 0, dif = 1;
while (K >= end) {
ijk++;
begin = end;
if (ijk < 2*N-1) {
dif += sum2cnt[ijk];
}
if (ijk - N >= 0) {
dif -= sum2cnt[ijk - N];
}
end += dif;
// std::cerr << begin << ' ' << end << ' ' << ijk << std::endl;
}
ll jk = ijk;
end = begin + (jk < 2*N - 1 ? sum2cnt[jk] : 0);
// std::cerr << jk << ' ' << sum2cnt[jk] << ' ' << begin << ' ' << end << std::endl;
for (; K >= end; jk--, begin = end, end += (jk < 2*N - 1 ? sum2cnt[jk] : 0)); //std::cerr << begin << ' ' << end << ' ' << jk << std::endl;;
// std::cerr << begin << ' ' << end << ' ' << jk << std::endl;
// std::cerr << ijk << ' ' << jk << std::endl;
ll i = ijk - jk;
ll j = jk - std::min(jk, N-1) + K - begin;
ll k = jk - j;
std::cout << i + 1 << ' ' << j + 1 << ' ' << k + 1 << '\n';
return 0;
} |
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int main() {
char op1[2], op2[2];
cin >> op1 >> op2;
if (op1[0] == 'Y') op2[0] -= 32;
cout << op2[0] << endl;
return 0;
} | //#include <bits/stdc++.h>
#include<iostream>
#include<vector>
#include<map>
#include<math.h>
#include<algorithm>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rep2(i,a,n) for(int i = (int)(a); i <= (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n-1); i > -1; i--)
#define rrep2(i,n,a) for(int i = (int)(n); i >= (int)(a); i--)
#define fi first
#define se second
using namespace std;
using ll = long long;
using v_int = vector<int>;
using v2_int = vector<v_int>;
using v_ll = vector<ll>;
using v2_ll = vector<v_ll>;
using v_string = vector<string>;
using v_bool = vector<bool>;
using v2_bool = vector<v_bool>;
using pii = pair<int, int>;
using mii = map<int, int>;
const double PI = 3.1415926535897932;
const int INF = (int)2e9;
const ll LINF = (ll)2e18;
const ll MOD = 998244353;//1000000007;
const int dc[8] = {1, -1, 0, 0, 1, -1, 1, -1};
const int dr[8] = {0, 0, 1, -1, 1, 1, -1, -1};
template<typename T, typename U> inline
ostream& operator<<(ostream &o, pair<T,U> &p) {
return o << "{" << p.first << "," << p.second << "}";
}
template<typename T> inline
istream& operator>>(istream &is, vector<T> &vec) {
for(auto &v : vec) { is >> v; }
return is;
}
template<typename T> inline
ostream& operator<<(ostream &os, vector<T> &vec) {
//for(auto &v : vec) { os << v << ",";}
for(auto &v : vec) { os << v << endl;}
return os;
}
template<typename T> inline
ostream& operator<<(ostream &os, vector<vector<T> > &mat) {
for(auto &row : mat) { os << row << endl; }
return os;
}
template<typename T> inline
void chmin(T &a, T b) { a = min(a, b); }
template<typename T> inline
void chmax(T &a, T b) { a = max(a, b); }
int main(void)
{
string s, t;
cin >> s >> t;
if(s == "Y") {
int n = t.size();
rep(i,n) {
t[i] = toupper(t[i]);
}
}
cout << t << endl;
return 0;
} /*atcoder*/
|
#include<bits/stdc++.h>
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/trie_policy.hpp>
//using namespace __gnu_pbds;
using namespace std;
#define ll long long int
#define ld long double
#define mod 1000000007
#define inf 1e18
#define endl "\n"
#define pb push_back
#define vi vector<int>
#define vl vector<ll>
#define vs vector<string>
#define pii pair<ll,ll>
#define ump unordered_map
#define mp make_pair
#define pq_max priority_queue<ll>
#define pq_min priority_queue<ll,vi,greater<ll> >
#define all(n) n.begin(),n.end()
#define ff first
#define ss second
#define mid(l,r) (l+(r-l)/2)
#define bitc(n) __builtin_popcount(n)
#define loop(i,a,b) for(int i=(a);i<=(b);i++)
#define looprev(i,a,b) for(int i=(a);i>=(b);i--)
#define iter(container, it) for(__typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define log(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
#define logarr(arr,a,b) for(int z=(a);z<=(b);z++) cout<<(arr[z])<<" ";cout<<endl;
#define M 1000000007
#define w(x) int x; cin>>x; while(x--)
template <typename T> T gcd(T a, T b) {if (a % b) return gcd(b, a % b); return b;}
template <typename T> T lcm(T a, T b) {return (a * (b / gcd(a, b)));}
vs tokenizer(string str, char ch) {std::istringstream var((str)); vs v; string t; while (getline((var), t, (ch))) {v.pb(t);} return v;}
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cout << *it << " = " << a << endl;
err(++it, args...);
}
//typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
//typedef trie<string,null_type,trie_string_access_traits<>,pat_trie_tag,trie_prefix_search_node_update> pbtrie;
void file_i_o()
{
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 main(int argc, char const *argv[]) {
clock_t begin = clock();
file_i_o();
// Write your code here....
double sx, sy, gx, gy;
cin >> sx >> sy >> gx >> gy;
double ans = double(((sy * (gx - sx)) / (gy + sy)) + sx);
cout << fixed << setprecision(10) << ans << endl;
#ifndef ONLINE_JUDGE
clock_t end = clock();
cout << "\n\nExecuted In: " << double(end - begin) / CLOCKS_PER_SEC * 1000 << " ms";
#endif
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for(i = 0; i < n; i++)
bool dfs(vector<int> &p, int swapFlag, int dep, vector<int> &ans) {
int n = min(4, (int)p.size());
int i;
rep(i, n) if (p[i] != i) break;
if (i == n) return true;
if (dep >= n * n) return false;
rep(i, n - 1) {
if (i % 2 != swapFlag % 2) { continue; }
swap(p[i], p[i + 1]);
ans.push_back(i);
bool res = dfs(p, !swapFlag, dep + 1, ans);
if (res) return true;
swap(p[i], p[i + 1]);
ans.pop_back();
}
return false;
}
vector<int> solve(vector<int> p) {
int n = p.size();
int i, j;
int cnt = 0;
vector<int> ret;
for (i = n; i >= 5; i--) {
rep(j, i) if (p[j] == i - 1) break;
int pos = j;
if (pos == i - 1) continue;
if (pos % 2 != cnt % 2) {
rep(j, i - 1) {
if (j != pos && j + 1 != pos && j % 2 == cnt % 2) {
break;
}
}
swap(p[j], p[j + 1]);
cnt++;
ret.push_back(j);
}
for (j = pos; j + 1 < i; j++) {
swap(p[j], p[j + 1]);
cnt++;
ret.push_back(j);
}
}
dfs(p, cnt % 2, 0, ret);
return ret;
}
int main() {
int T, i;
cin >> T;
while (T--) {
int n;
cin >> n;
vector<int> p(n);
rep(i, n) { cin >> p[i]; p[i]--; }
vector<int> ans = solve(p);
cout << ans.size() << endl;
rep(i, ans.size()) {
cout << ans[i] + 1;
if (i + 1 < ans.size()) cout << " ";
}
cout << endl;
}
return 0;
}
|
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn = 1e6+10,mod = 1e9+7;
int a,b,x,y,f[maxn][2];
int main() {
cin >> a >> b >> x >> y;
f[a<b?a:b][a<b?0:1] = 0;
f[a<b?a:b][a<b?1:0] = x;
for(int i = min(a,b)+1; i <= max(a,b); i++){
f[i][0] = min(f[i-1][0]+y,f[i-1][1]+x);
f[i][1] = min(f[i][0]+x,f[i-1][1]+y);
}
cout << f[a<b?b:a][a<b?1:0] << endl;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
using ll = int64_t;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int a,b,x,y;
scanf("%d",&a);
scanf("%d",&b);
scanf("%d",&x);
scanf("%d",&y);
int ans;
if(y > 2*x){
if(a == b){
ans = x;
}
else if(a < b){
ans = x + (b-a)*2*x;
}
else{
ans = x + (a-b-1)*2*x;
}
}
else{
if(a == b){
ans = x;
}
else if(a < b){
ans = x + (b-a)*y;
}
else{
ans = x + (a-b-1)*y;
}
}
printf("%d\n",ans);
return 0;
} |
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=10009;
struct node {
set<int>q;
} d[N];
int n,ins[N],c[N],dfn[N],low[N],num,cnt=0;
double ans=0;
vector<int>w[N],w1[N],scc[N];
stack<int>st;
set<int>q;
void Tarjan(int x) {
dfn[x]=low[x]=++num;
st.push(x),ins[x]=1;
for(int i=0; i<w[x].size(); i++) {
int y=w[x][i];
if(!dfn[y]) {
Tarjan(y);
low[x]=min(low[x],low[y]);
} else if(ins[y])
low[x]=min(low[x],dfn[y]);
}
if(dfn[x]==low[x]) {
cnt++;
int y;
do {
y=st.top();
st.pop();
ins[y]=0;
c[y]=cnt;
w1[cnt].push_back(y);
} while(x!=y);
}
}
void dfs(int x) {
d[x].q.insert(x);
int si=scc[x].size();
for(int i=0; i<si; i++) {
int y=scc[x][i];
if(d[y].q.empty())
dfs(y);
for(set<int>::iterator it=d[y].q.begin(); it!=d[y].q.end(); it++)
d[x].q.insert(*it);
}
}
int main() {
scanf("%d",&n);
for(int i=1; i<=n; i++) {
char a[N];
scanf("%s",a+1);
for(int j=1; a[j]!='\0'; j++)
if(a[j]=='1')
w[i].push_back(j);
}
for(int i=1; i<=n; i++)
if(!dfn[i])
num=0,Tarjan(i);
for(int i=1; i<=n; i++)
for(int j=0; j<w[i].size(); j++) {
int x=w[i][j];
if(c[i]==c[x])
continue;
scc[c[x]].push_back(c[i]);
}
for(int i=1; i<=cnt; i++) {
if(d[i].q.empty())
dfs(i);
}
for(int i=1;i<=cnt;i++){
int res=0;
for(set<int>::iterator it=d[i].q.begin(); it!=d[i].q.end(); it++)
res+=w1[(*it)].size();
ans+=(double)w1[i].size()/res;
}
printf("%.13lf",ans);
return 0;
}
/*
4
0110
1001
0001
0010
*/
| #include<bits/stdc++.h>
#define mp make_pair
#define pb emplace_back
#define fi first
#define se second
#define ick cout << "ickbmi32.9\n"
using namespace std;
struct edge{
double w = 1;
int t = 0;
};
edge r[30][30], d[30][30];
int dx[]{0, 0, 1, -1};
int dy[]{1, -1, 0, 0};
char mv[]{'R', 'L', 'D', 'U'};
int getDist(int x, int y, int dir) {
if(min(x + dx[dir], y + dy[dir]) < 0) return -1;
if(max(x + dx[dir], y + dy[dir]) >= 30) return -1;
if(dir == 0) return d[x][y].w;
if(dir == 1) return d[x][y - 1].w;
if(dir == 2) return r[x][y].w;
if(dir == 3) return r[x - 1][y].w;
return -1;
}
pair<string, vector<pair<int, int>>> path(int x1, int y1, int x2, int y2) {
double dist[30][30];
for(int i = 0; i < 30; i++) for(int j = 0; j < 30; j++) dist[i][j] = -1;
priority_queue<pair<double, pair<int, int>>, vector<pair<double, pair<int, int>>>, greater<>>dij;
dij.push(mp(0, mp(x1, y1)));
while(!dij.empty()) {
pair<double, pair<int, int>> now = dij.top();
dij.pop();
if(abs(dist[now.se.fi][now.se.se] + 1) > 1e-5) continue;
dist[now.se.fi][now.se.se] = now.fi;
for(int i = 0; i < 4; i++) {
int di = getDist(now.se.fi, now.se.se, i);
if(di == -1) continue;
dij.push(mp(now.fi + di, mp(now.se.fi + dx[i], now.se.se + dy[i])));
}
}
pair<int, int> now = mp(x2, y2);
vector<pair<int, int>> pa;
string pam;
while(now != mp(x1, y1)) {
//cout << now.fi << ' ' << now.se << endl;
for(int i = 0; i < 4; i++) {
int di = getDist(now.fi, now.se, i);
if(di == -1) continue;
if(abs(dist[now.fi][now.se] - di - dist[now.fi + dx[i]][now.se + dy[i]]) < 1e-5) {
pam += mv[i ^ 1];
now.fi += dx[i];
now.se += dy[i];
break;
}
}
pa.pb(now);
}
reverse(pa.begin(), pa.end());
reverse(pam.begin(), pam.end());
return mp(pam, pa);
}
void upd(string ah, vector<pair<int, int>> ps, double avg) {
for(int i = 0; i < ah.length(); i++) {
if(ah[i] == 'D') {
d[ps[i].fi][ps[i].se].w = (d[ps[i].fi][ps[i].se].w * d[ps[i].fi][ps[i].se].t + avg) / (d[ps[i].fi][ps[i].se].t + 1);
d[ps[i].fi][ps[i].se].t++;
}
if(ah[i] == 'U') {
d[ps[i].fi][ps[i].se - 1].w = (d[ps[i].fi][ps[i].se - 1].w * d[ps[i].fi][ps[i].se - 1].t + avg) / (d[ps[i].fi][ps[i].se - 1].t + 1);
d[ps[i].fi][ps[i].se - 1].t++;
}
if(ah[i] == 'R') {
r[ps[i].fi][ps[i].se].w = (r[ps[i].fi][ps[i].se].w * r[ps[i].fi][ps[i].se].t + avg) / (r[ps[i].fi][ps[i].se].t + 1);
r[ps[i].fi][ps[i].se].t++;
}
if(ah[i] == 'L') {
r[ps[i].fi - 1][ps[i].se].w = (r[ps[i].fi - 1][ps[i].se].w * r[ps[i].fi - 1][ps[i].se].t + avg) / (r[ps[i].fi - 1][ps[i].se].t + 1);
r[ps[i].fi - 1][ps[i].se].t++;
}
}
}
void solve() {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
pair<string, vector<pair<int, int>>> go = path(x1, y1, x2, y2);
double in;
cout << go.fi << '\n';
fflush(stdout);
cin >> in;
upd(go.fi, go.se, in / go.fi.length());
}
int main() {
for(int _ = 0; _ < 1000; _++) solve();
} |
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define pi 3.141592653589793238
#define int long long
#define ll long long
#define ld long double
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
long long powm(long long a, long long b,long long mod) {
long long res = 1;
while (b > 0) {
if (b & 1)
res = res * a %mod;
a = a * a %mod;
b >>= 1;
}
return res;
}
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);
#ifndef ONLINE_JUDGE
if(fopen("INPUT.txt","r"))
{
freopen("INPUT.txt","r",stdin);
freopen("OUTPUT.txt","w",stdout);
}
#endif
int v,s,d,t;
cin>>v>>t>>s>>d;
int x=t*v;
int y=v*s;
if(x<=d && y>=d)
{
cout<<"No";
}
else
cout<<"Yes";
} | #include <algorithm>
#include <iostream>
#include <queue>
#include <bitset>
using namespace std;
#define all(x) (x).begin(), (x).end()
using ll = long long;
const int MOD = 1e9 + 7;
void solve() {
int a, b, w;
cin >> a >> b >> w;
w *= 1000;
int x = 1e9, y = -1;
for(int i = 1; i <= w; i++) {
if(a * i <= w && w <= b * i) {
x = min(x, i);
y = max(y, i);
}
}
if(y == -1) {
cout << "UNSATISFIABLE";
} else {
cout << x << ' ' << y;
}
}
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}
|
#include <iostream>
using namespace std;
int main() {
int A, B, C;
cin >> A >> B >> C;
if (A * A + B * B < C * C) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
} | #include <bits/stdc++.h>
const long long INF = 1e9;
const long long MOD = 1e9 + 7;
//const long long MOD = 998244353;
const long long LINF = 1e18;
using namespace std;
#define YES(n) cout << ((n) ? "YES" : "NO" ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No" ) << endl
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE" : "IMPOSSIBLE" ) << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible" ) << endl
#define dump(x) cout << #x << " = " << (x) << endl
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) for(int i=0;i<(n);++i)
#define REPR(i,n) for(int i=n;i>=0;i--)
#define COUT(x) cout<<(x)<<endl
#define SCOUT(x) cout<<(x)<<" "
#define VECCOUT(x) for(auto&youso_: (x) )cout<<right<<setw(10)<<youso_<<" ";cout<<endl
#define ENDL cout<<endl
#define CIN(...) int __VA_ARGS__;CINT(__VA_ARGS__)
#define LCIN(...) long long __VA_ARGS__;CINT(__VA_ARGS__)
#define SCIN(...) string __VA_ARGS__;CINT(__VA_ARGS__)
#define VECCIN(x) for(auto&youso_: (x) )cin>>youso_
#define mp make_pair
#define PQ priority_queue<long long>
#define PQG priority_queue<long long,V,greater<long long>>
typedef long long ll;
typedef vector<long long> vl;
typedef vector<long long> vi;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<vl> vvl;
typedef vector<vi> vvi;
typedef vector<vb> vvb;
typedef vector<vc> vvc;
typedef pair<long long, long long> pll;
#define COUT(x) cout<<(x)<<endl
void CINT(){}
template <class Head,class... Tail>
void CINT(Head&& head,Tail&&... tail){
cin>>head;
CINT(move(tail)...);
}
template<class T>
void mod(T &x) {
x %= MOD;
x += MOD;
x %= MOD;
}
ll GCD(ll a, ll b) {
if(b == 0) return a;
else return GCD(b, a%b);
}
struct COMB{
vl fact, fact_inv, inv;
void init_nCk(long long SIZE) {
fact.resize(SIZE + 5);
fact_inv.resize(SIZE + 5);
inv.resize(SIZE + 5);
fact.at(0) = fact.at(1) = fact_inv.at(0) = fact_inv.at(1) = inv.at(1) = 1;
for(long long i = 2; i < SIZE + 5; i++) {
fact.at(i) = fact.at(i - 1)*i%MOD;
inv.at(i) = MOD - inv.at(MOD%i)*(MOD/i)%MOD;
fact_inv.at(i) = fact_inv.at(i - 1)*inv.at(i)%MOD;
}
}
long long nCk (long long n, long long k) {
assert(!(n < k));
assert(!(n < 0 || k < 0));
return fact.at(n)*(fact_inv.at(k)*fact_inv.at(n - k)%MOD)%MOD;
}
};
ll extGCD(ll a, ll b, ll &x, ll &y) {
if(b == 0) {
x = 1;
y = 0;
return a;
}
ll d = extGCD(b, a%b, y, x);
y -= a/b*x;
return d;
}
void Main() {
LCIN(A, B, C);
if(A*A + B*B < C*C) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
int main() {
cout << fixed << setprecision(15);
Main();
return 0;
} |
//IQ134高知能系Vtuberの高井茅乃です。
//Twitter: https://twitter.com/takaichino
//YouTube: https://www.youtube.com/channel/UCTOxnI3eOI_o1HRgzq-LEZw
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF INT_MAX
#define LLINF LLONG_MAX
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define MODA 1000000007
#define MODB 998244353
template <typename T>
std::istream& operator>>(std::istream& is, std::vector<T>& vec) {
for (T& x: vec) { is >> x; }
return is;
}
int main() {
ll ans = 0;
ll tmp = 0;
int n; cin >> n;
vector<ll> a(n);
cin >> a;
map<ll, ll> m;
m[0]++;
REP(i, n){
if(i%2==0)tmp += a[i];
else tmp -= a[i];
m[tmp]++;
}
for(auto it = m.begin(); it != m.end(); it++){
ans += (it->second) * (it->second - 1LL) / 2;
}
cout << ans << endl;
} | #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 <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <iomanip>
#include <limits>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(a) a.begin(),a.end()
#define P pair<int,int>
#define Pll pair<long,long>
#define mp make_pair
#define INF 1000000001
#define INFl 1000000000000000001
#define ll long long
using namespace std;
struct edge{long to,cost,k;};
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;
}
int main() {
int N;cin>>N;
vector<long>A(N);
vector<long>B(N);
rep(i,N)cin>>A[i];
map<long,int> mp;
B[0]=A[0];
rep(i,N-1){
B[i+1]=A[i+1]-B[i];
}
B[0]=0;
mp[0]=1;
long cnt=0;
rep(i,N-1){
//cout<<B[i+1]<<endl;
long j=B[i+1];
if(i%2==0){
if(mp.count(-j))cnt+=mp[-j];
}else{
if(mp.count(j))cnt+=mp[j];
}
if(i%2==0){
long k=A[i+1]-B[i+1];
if(mp.count(k))mp[k]++;
else mp.emplace(k,1);
}else{
long k=A[i+1]-B[i+1];
if(mp.count(-k))mp[-k]++;
else mp.emplace(-k,1);
}
}
cout<<cnt<<endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int (i)=0; (i)<(int)(n); (i)++)
#define all(x) (x).begin(), (x).end()
#define sqr(x) (x)*(x)
#define CEIL(a,b) ((a)+(b)-1) /(b)
typedef long long ll;
const int INF = 1001001001;
void sort_second(vector<pair<ll, ll>> &A, int N); //A:array of pair, N:size of array
ll a_n(ll a, ll n); // a^n
int main() {
int n, k, m, sum = 0;
cin >> n >> k >> m;
rep(i, n-1) {
int a;
cin >> a;
sum += a;
}
if(n*m - sum < 0) cout << 0 << endl;
else if(n*m - sum <= k) cout << n*m -sum << endl;
else cout << -1 << endl;
return 0;
}
void sort_second(vector<pair<ll, ll>> &A, int N) {
rep(i, N) swap(A.at(i).first, A.at(i).second);
sort(all(A));
rep(i, N) swap(A.at(i).first, A.at(i).second);
}
ll a_n(ll a, ll n) {
ll ret = a, count = 1;
while(count * 2 < n) {
ret *= ret;
count *= 2;
}
if(count == n) return ret;
else return ret * a_n(a, n-count) ;
} | #include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<set>
//#include<bits/stdc++.h>
#define int long long
constexpr long long mod = 1000000007;
#define for0(i, n) for(int i = 0; i < (n); i++)
#define for1(i, n) for(int i = 1; i <= (n);i++)
#define mp make_pair
#define all(x) x.begin(),x.end()
#define puts(x) cout << (x) << "\n"
using namespace std;
int input() {
int r; cin >> r; //scanf("%lld", &r);
return r;
}
char map[2020][2020];
int px[2020][2020], h, w, gi, gj;
queue<pair<int, pair<int, int>>>q1;
vector<pair<int, int>>v[150]; bool b[150];
signed main() {
cin >> h >> w;
for0(i, h + 2)for0(j, w + 2) {
if (i * j == 0)map[i][j] = '#';
else if (i > h || j > w)map[i][j] = '#';
else {
cin >> map[i][j];
px[i][j] = mod;
if (map[i][j] == 'S') {
q1.push(mp(0, mp(i, j)));
}
else if (map[i][j] == 'G') {
gi = i; gj = j;
}
else if (map[i][j] != '.' && map[i][j] != '#')v[map[i][j]].push_back(mp(i, j));
}
}
while (q1.size()) {
int c = q1.front().first, i = q1.front().second.first, j = q1.front().second.second;
q1.pop();
if (px[i][j] < c)continue;
px[i][j] = c;
if (c > px[gi][gj] || (i == gi && j == gj)) {
puts(px[gi][gj]); return 0;
}
if (map[i - 1][j] != '#' && px[i - 1][j] == mod) { px[i - 1][j] = c + 100; q1.push(mp(c + 1, mp(i - 1, j))); }
if (map[i + 1][j] != '#' && px[i + 1][j] == mod) { px[i + 1][j] = c + 100; q1.push(mp(c + 1, mp(i + 1, j))); }
if (map[i][j - 1] != '#' && px[i][j - 1] == mod) { px[i][j - 1] = c + 100; q1.push(mp(c + 1, mp(i, j - 1))); }
if (map[i][j + 1] != '#' && px[i][j + 1] == mod) { px[i][j + 1] = c + 100; q1.push(mp(c + 1, mp(i, j + 1))); }
if ('a' <= map[i][j] && map[i][j] <= 'z' && !b[map[i][j]]) {
b[map[i][j]] = 1;
for (pair<int, int>p1 : v[map[i][j]])
if (px[p1.first][p1.second] == mod) { px[p1.first][p1.second] = c + 100; q1.push(mp(c + 1, mp(p1.first, p1.second))); }
}
}
puts(-1); return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef map<int,int> mii;
typedef map<ll,ll> mll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
#define f(i,n) for(i=0;i<n;i++)
#define f1(i,n) for(i=1;i<n;i++)
#define fr(i,n) for(i=n-1;i>=0;i--)
#define em emplace_back
#define mp make_pair
#define in insert
#define fi first
#define sc second
#define b begin
#define e end
#define er erase
#define l length
#define c clear
#define si size
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);
const double pi=3.141592653;
const ll infi=1000000001;
const ll MOD=1000000007;
const ll mod=998244353;
const string no="NO\n",yes="YES\n";
// ll gcd(ll a,ll b){
// if(a==0) return b;
// gcd(b%a,a);
// }
// void dfs(vl v[],ll node,ll hai[]){
// hai[node]=1;
// for(auto i:v[node]){
// if(hai[i]==0){
// dfs(v,i,hai);
// }
// }
// }
// ll con(vl v[],ll hai[],ll sz){
// ll count=0;
// for(int i=1;i<sz;i++){
// if(hai[i]==0){
// count++;
// dfs(v,i,hai);
// }
// }
// return count;
// }
// void gcde(int a,int b,int *x,int *y){
// if(a==0){
// *x=0;
// *y=1;
// return;
// }
// int x1,y1;
// gcde(b%a,a,&x1,&y1);
// *x=y1-(b/a)*x1;
// *y=x1;
// }
// ll recur(vl v,ll in,ll m,ll n){
// if(in==-1) return 1000000000001;
// ll d,e,c;
// d=max(m/v[in],n*v[in]);
// c=recur(v,in-1,m/v[in],n*v[in]);
// e=recur(v,in-1,m,n);
// return min(c,min(e,d));
// }
// ll bexpo(ll a,ll p){
// ll x=1;
// while(p){
// if(p&1){
// x=(x*a)%mod;
// }
// a=(a*a)%mod;
// p>>=1;
// }
// return x;
// }
// int dx[8]{-1,0,0,1,-1,-1,1,1};
// int dy[8]{0,-1,1,0,-1,1,-1,1};
int main() {
fastio
int t=1;
// cin>>t;
ll n,m,i,l,d,bank,k,j,x,y,ans,q;
while(t--){ans=x=y=0;
cin>>n;
string s;
cin>>s;
f(i,n){
x=y=0;
for(int j=i;j<n;j++){
if(s[j]=='A') x++;
else if(s[j]=='T') x--;
else if(s[j]=='C') y++;
else y--;
if(x==0 && y==0) ans++;
}
}
cout<<ans;
}
return 0;
} | #include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<set>
#include<cmath>
#include<iomanip>
#include<map>
using namespace std;
#define R (long long)(1e9 + 7)
#define M 998244353
#define INF 1e18L
struct dsu
{
vector<int> rank, parent, c;
vector<map<int, int>> f;
int n;
dsu(int n, vector<int>& c)
{
this -> n = n;
this -> c = c;
rank.assign(n, 0);
parent.resize(n);
f.resize(n);
for(int i = 0; i < n; i++)
parent[i] = i;
for(int i = 0; i < n; i++)
f[i][c[i]]++;
}
int find_set(int i)
{
if(i == parent[i])
return i;
return parent[i] = find_set(parent[i]);
}
bool is_same_set(int i, int j)
{
return (find_set(i) == find_set(j));
}
void union_set(int i, int j)
{
if(!is_same_set(i, j))
{
int u = find_set(i), v = find_set(j);
if(rank[u] == rank[v])
{
rank[u]++;
parent[v] = u;
for(auto x : f[v])
{
f[u][x.first] += x.second;
}
}
else if(rank[u] > rank[v])
{
parent[v] = u;
for(auto x : f[v])
{
f[u][x.first] += x.second;
}
}
else
{
parent[u] = v;
for(auto x : f[u])
{
f[v][x.first] += x.second;
}
}
}
}
};
int main()
{
int n, q;
cin >> n >> q;
vector<int> c(n);
for(int& x : c)
cin >> x;
dsu d(n, c);
while(q--)
{
int type;
cin >> type;
if(type == 1)
{
int a, b;
cin >> a >> b;
a--, b--;
d.union_set(a, b);
}
else
{
int x, y;
cin >> x >> y;
x--;
cout << d.f[d.find_set(x)][y] << '\n';
}
}
}
|
#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<string>
#include<limits.h>
#include<utility>
#include<vector>
#include<tuple>
#include <map>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define rep(i,n) for(ll i=0;i<n;i++)
#define ALL(A) A.begin(),A.end()
int main() {
string s;
vector<ll> a(26);
cin>>s;
ll ans=0;
ll n=s.size();
for(ll i=s.size()-3;i>=0;i--){
ll c=s[i]-'a';
ll C=s[i+2]-'a';
a[C]++;
if(s[i]==s[i+1]&&s[i]!=s[i+2]){
ans+=n-2-i-a[c];
rep(i,26){
//cout<<a[i]<<" ";
a[i]=0;
}
//cout<<endl;
a[c]=n-2-i;
//cout<<i<<" "<<ans<<endl;
}
}
cout<<ans<<endl;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repp(i, st, en) for (ll i = (ll)st; i < (ll)(en); i++)
#define repm(i, st, en) for (ll i = (ll)st; i >= (ll)(en); i--)
#define all(v) v.begin(), v.end()
void chmax(ll &x, ll y) {x = max(x,y);}
void chmin(ll &x, ll y) {x = min(x,y);}
void Yes() {cout << "Yes" << endl; exit(0);}
void No() {cout << "No" << endl; exit(0);}
template<class in_Cout> void Cout(in_Cout x) {cout << x << endl; exit(0);}
template<class in_vec_cout>
void vec_cout(vector<in_vec_cout> vec) {
for (in_vec_cout res : vec) {cout << res << " ";}
cout << endl;
}
const ll inf = 1e18;
const ll mod = 1e9 + 7;
// segment tree struct
template<class in_seg>
struct segtree {
in_seg op(in_seg x, in_seg y) {return min(x,y);} // モノイド演算
in_seg e = inf; // 元(+→0, *→1, xor→0, max→-inf, min→inf, gcd→0 : e●a==a●e==a)
ll len = 1LL; // セグメントの長さ
ll N;
vector<in_seg> dat;
segtree(ll n) {init_seg(n);}
segtree(vector<in_seg> vec) {init_seg(vec);}
void init_seg(ll n) {
N = n;
while (len<N) {len *= 2;}
dat.resize(2*len,e);
}
void init_seg(vector<in_seg> vec) {
N = vec.size();
init_seg(N);
rep(i,N) dat[i+len] = vec[i];
for (ll i=len-1; i>=1; i--) dat[i] = op(dat[2*i],dat[2*i+1]);
}
// dat[id]に x を代入
void set(ll id, in_seg x) {
id += len;
dat[id] = x;
while (id>0) {
id /= 2;
dat[id] = op(dat[2*id],dat[2*id+1]);
}
}
// dat[id]を返す O(1)
in_seg get(ll id) {id += len; return dat[id];}
// op(a[l],...,a[r-1])を計算して返す。l==rの時は e を返す O(logN)
in_seg prod(ll l, ll r) {
l += len; r += len;
in_seg res = e;
while (l<r) {
if (l%2) {res = op(res,dat[l]); l++;}
l /= 2;
if (r%2) {res = op(res,dat[r-1]); r--;}
r /= 2;
}
return res;
}
in_seg all_prod() {return dat[1];}
};
int main() {
ll N, M; cin >> N >> M;
vector<ll> A(N);
rep(i,N) cin >> A[i];
vector<ll> cnt(N);
segtree<ll> seg(N+1);
rep(i,seg.len) seg.set(i,i);
rep(i,M) {
ll a = A[i];
cnt[a]++;
seg.set(a,inf);
}
ll ans = inf;
for (ll i=M-1; i<N; i++) {
chmin(ans,seg.all_prod());
if (i==N-1) break;
ll l = A[i-M+1], r = A[i+1];
cnt[l]--;
cnt[r]++;
if (cnt[l]==0) seg.set(l,l);
seg.set(r,inf);
}
Cout(ans);
} |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
const int MOD = 1000000007;
#define fi first
#define se second
#define rep(i,n) for(int i = 0 ; i < n ; i++)
#define repe(i,n) for(int i = 0 ; i <= n ; i++)
#define repb(i,a,b) for(int i = a ; i < b ; i++)
#define repeb(i,a,b) for(int i = a ; i <= b ; i++)
#define rfor(i,n,a) for(ll i = n ; i >= a ; i--)
#define pb push_back
#define endl "\n"
#define vi vector<int>
#define vvi vector<vi>
#define pii pair <ll,ll>
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL)
#define p0(a) cout << a << " "
#define p1(a) cout << a << endl
#define p2(a,b) cout << a << " " << b << endl
#define p3(a,b,c) cout << a << " " << b << " " << c << endl
#define watch(x) cout << (#x) << " is " << (x) << endl
#define w(x) ll x; cin>>x; while(x--)
template <typename T1, typename T2>
inline std::ostream& operator << (std::ostream& os, const std::pair<T1, T2>& p)
{
return os << "(" << p.first << ", " << p.second << ")";
}
template<typename T>
inline std::ostream &operator << (std::ostream & os,const std::vector<T>& v)
{
bool first = true;
for(unsigned int i = 0; i < v.size(); i++)
{
if(!first)
os << " ";
os << v[i];
first = false;
}
return os;
}
/*-------------------------------------------------*/
// read once, read again, think, code
const int MAXM = 2e5+1;
int timestamp = 0;
int in[MAXM];
int out[MAXM];
void dfs(int root, vi *adj, vi *depth, int d) {
in[root] = timestamp;
depth[d].pb(in[root]);
timestamp++;
for(int &child : adj[root]) {
dfs(child,adj,depth,d+1);
}
out[root] = timestamp;
}
void solve() {
int n;
cin >> n;
vi adj[n+1], depth[n+1];
repeb(child,2,n) {
int parent; cin >> parent;
adj[parent].pb(child);
}
dfs(1,adj,depth,0);
int q; cin >> q;
while(q--) {
int u, d; cin >> u >> d;
int ans = lower_bound(depth[d].begin(), depth[d].end(), out[u]) - lower_bound(depth[d].begin(), depth[d].end(), in[u]);
p1(ans);
}
}
int main()
{
fastio;
#ifndef ONLINE_JUDGE
freopen("/home/devang/input.txt","r",stdin);
freopen("/home/devang/output.txt","w",stdout);
#endif
//w(tc)
solve();
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, n, s) for (int i = (s); i < (n); i++)
#define rrep(i, n) for (int i = (n - 1); i >= 0; i--)
#define rreps(i, n, s) for (int i = s; i >= n; i--)
using ll = long long;
using namespace std;
constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int MOD = 1000000007;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll n, m;
cin >> n >> m;
vector<ll> a(n);
rep(i, n) cin >> a[i];
vector<vector<ll>> node(n);
rep(i, m) {
ll x, y;
cin >> x >> y;
x--, y--;
node[x].push_back(y);
}
vector<bool> visit(n, false);
vector<ll> dp(n, INF);
rep(now, n) {
if (visit[now]) continue;
visit[now] = true;
for (auto next : node[now]) { dp[next] = min(dp[next], min(dp[now], a[now])); }
}
ll res = -INF;
rep(i, n) {
if (dp[i] == INF) continue;
res = max(a[i] - dp[i], res);
}
cout << res << endl;
return 0;
} |
#include<bits/stdc++.h>
#define fast {ios_base::sync_with_stdio(false);cin.tie(NULL);}
#define all(x) (x).begin(), (x).end()
#define Unique(a) a.erase(unique(all(a)), a.end())
#define ld long double
#define pb push_back
#define fi first
#define se second
#define inf 1e9
using namespace std;
typedef long long int ll;
const int mxn=2e5+5,mod=1e9+7;
int a[mxn],dis[mxn];
vector<int>g[mxn];
bool vis[mxn];
int timer;
vector<int> tin(mxn), tout(mxn);
vector<vector<int>> up;
vector<int>lev[mxn];
void dfs(int v, int p)
{
tin[v] = ++timer;
lev[dis[v]].pb(timer);
for (int u : g[v]) {
if (u != p){
dis[u]=dis[v]+1;
dfs(u, v);
}
}
tout[v] = ++timer;
}
int ask(int d,int x){
int res=-1;
for(int i=17;i>=0;--i){
int sum=res+(1<<i);
if(sum>=lev[d].size()) continue;
if(lev[d][sum]<=x)res=sum;
}
return res;
}
int main(void){
fast;
int n,i,q,x;
cin>>n;
for(i=2;i<=n;i++){
cin>>x;
g[x].pb(i);
g[i].pb(x);
}
dis[1]=0;
dfs(1,0);
cin>>q;
while(q--){
int x,y,l1=0,l2=0;
cin>>x>>y;
int l=tin[x];
int r=tout[x];
cout<<ask(y,r)-ask(y,l-1)<<'\n';
}
}
| #include <bits/stdc++.h>
using namespace std;
const int N=2e5+2;
int n,q,f[N];
int cnt,hd[N],to[N],nxt[N];
int hd1[N],d[N],nxt1[N],ans[N];
void adde(int x,int y)
{
nxt[++cnt]=hd[x];
to[hd[x]=cnt]=y;
}
void dfs(int x,int dep)
{
for(int i=hd1[x];i;i=nxt1[i])
ans[i]=-f[d[i]];
++f[dep];
for(int i=hd[x];i;i=nxt[i])
dfs(to[i],dep+1);
for(int i=hd1[x];i;i=nxt1[i])
ans[i]+=f[d[i]];
}
int main()
{
scanf("%d",&n);
for(int i=2,x;i<=n;++i)
{
scanf("%d",&x);
adde(x,i);
}
scanf("%d",&q);
for(int i=1,x,y;i<=q;++i)
{
scanf("%d%d",&x,&y);
nxt1[i]=hd1[x];
d[hd1[x]=i]=y;
}
dfs(1,0);
for(int i=1;i<=q;++i)
printf("%d\n",ans[i]);
} |
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
long long k;
cin >> n >> k;
long long dp[4][3000005]={0};
dp[0][0]=1;
for(int i=0;i<3;i++){
for(int j=0;j<=i*n;j++){
dp[i+1][j+1]+=dp[i][j];
dp[i+1][j+n+1]-=dp[i][j];
}
for(int j=1;j<=(i+1)*n;j++){
dp[i+1][j]+=dp[i+1][j-1];
}
}
int x;
for(int i=3;i<=3*n;i++){
if(k<=dp[3][i]){x=i;break;}
else{k-=dp[3][i];}
}
for(int i=1;i<=n;i++){
int jmi=max(1,x-i-n);
int jma=min(n,x-i-1);
if(jmi>jma){continue;}
if(k>(jma-jmi+1)){k-=(jma-jmi+1);continue;}
int y=jmi+k-1;
int z=x-i-y;
cout << i << ' ' << y << ' ' << z << '\n';
return 0;
}
} | #include <bits/stdc++.h>
using namespace std;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c> {i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni( != ) cerr << boolalpha << i; ris;
}
eni( == ) ris << range(begin(i), end(i));
}
sim, class b dor(pair < b, c > d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) "[" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
#define int long long
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
int l, r;
cin >> l >> r;
vector<int> dp(r + 1);
int ans = 0;
for(int i = r; i >= 2; i--){
int mults = (r / i) - (l - 1) / i;
mults = (mults * (mults - 1)) / 2;
//debug() << imie(mults);
for(int j = i + i; j <= r; j += i){
mults -= dp[j];
}
dp[i] = mults;
//debug() << imie(dp[i]) imie(i);
}
for(int i = 2; i <= r; i++){
ans += dp[i];
}
debug() << imie(ans);
for(int i = max(2LL, l); i <= r; i++){
ans -= ((r / i) - 1);
}
cout << ans * 2 << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int main()
{
#ifndef ONLINE_JUDGE
freopen("tpl.txt","r",stdin);
#endif
vector<int> vc;
int n,m;
cin >> n >>m;
for(int i = 0 ; i < n; i ++){
int t; cin >> t;
if(t != m)
vc.push_back(t);
}
for(int i = 0 ; i < vc.size() ; i++)
cout << vc[i] << " ";
return 0;
} | //----AUTHOR:kkdrummer----/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
//#include <boost/multiprecision/cpp_int.hpp>
//using namespace boost::multiprecision;
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unordered_set<ll> usll;
typedef unordered_multiset<ll> umsll;
typedef multiset<ll> msll;
typedef set<ll> sll;
typedef vector<ll> vll;
typedef pair<ll,ll> pll;
typedef vector<pll> vpll;
typedef priority_queue<ll> pqll;
typedef vector<int> vi;
typedef set<int> si;
typedef multiset<int> msi;
typedef unordered_multiset<int> umsi;
typedef unordered_set<int> usi;
typedef pair<int,int> pi;
typedef vector<pi> vpi;
typedef priority_queue<int> pqi;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ind_si;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ind_sll;
typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update> ind_msi;
typedef tree<ll,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update> ind_msll;
#define in insert
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define be begin
#define en end
#define itr iterator
#define io ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define mo 998244353
#define inf 8222372026854775807
#define ninf -inf
#define ima 2047483647
#define imi -ima
#define oncnt __builtin_popcount
#define zerobegin __builtin_clz
#define zeroend __builtin_ctz
#define parity __builtin_parity
#define eps 1e-9
#define coutd cout<<setprecision(10)<<fixed
#define mems(dp,x) memset(dp,x,sizeof(dp))
#define fbo find_by_order
#define ook order_of_key
#define all(x) x.be(),x.en()
#define upb upper_bound
#define lowb lower_bound
#define lte(v,x) (upb(all(v),x)-v.be())
#define gte(v,x) (v.end()-lowb(all(v),x))
#define gt(v,x) (v.en()-upb(all(v),x))
#define lt(v,x) (lowb(all(v),x)-v.be())
const ld PI= 3.1415926535897932384626433832792884197169399375105820974944;
inline ll mpow(ll x,ll n){if(n==0)return 1;if(x==0)return 0;if(n==1)return(x%mo);ll u=(mpow(x,n/2));u=(u*u)%mo;if(n%2!=0)u=(u*x%mo)%mo;return u;}
inline ll minv(ll x){return mpow(x,mo-2);}
inline ll mmul(ll a,ll b){if(a>=mo)a=a%mo;if(b>=mo)b=b%mo;if(a*b>=mo)return(a*b)%mo;return(a*b);}
inline ll madd(ll a, ll b){if(a>=mo)a=a%mo;if(b>=mo)b=b%mo;if(a+b>=mo)return(a+b)%mo;return(a+b);}
inline ll msub(ll a, ll b){if(a>=mo)a=a%mo;if(b>=mo)b=b%mo;return(((a-b)%mo+mo)%mo);}
inline ll mdiv(ll a,ll bb){if(a>=mo)a=a%mo;ll b=minv(bb);if(b>=mo)b=b%mo;if(a*b>=mo)return(a*b)%mo;return(a*b);}
inline ll gcd(ll a,ll b){return __gcd(a,b);}
inline ll lcm(ll a,ll b){return (a*b)/gcd(a,b);}
ll inta(vll v)
{
ll c=0;
for(auto x:v)c=c*10+x;
return c;
}
ll g1(ll n)
{
vll v;
while(n)
{
v.pb(n%10);
n/=10;
}
sort(all(v));
reverse(all(v));
return inta(v);
}
ll g2(ll n)
{
vll v;
while(n)
{
v.pb(n%10);
n/=10;
}
sort(all(v));
//reverse(all(v));
return inta(v);
}
ll f(ll n)
{
return g1(n)-g2(n);
}
int main()
{ io
int testcases=1; // cin>>testcases;
while(testcases--)
{
ll n,k;
cin>>n>>k;
for(int i=0;i<k;i++)
n=f(n);
cout<<n;
}return 0;} |
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
long long X;
scanf("%d %lld", &n, &X);
vector <int> a(n);
for(int i = 0; i < n; i++)scanf("%d", &a[i]);
long long res = X;
for(int k = 1; k <= n; k++){
vector < vector <long long> > dp(k + 1, vector <long long> (k, -1));
dp[0][0] = 0;
for(int i = 0; i < n; i++){
for(int j = k; j >= 1; j--)
for(int r = 0; r < k; r++){
long long c = dp[j - 1][(r - (a[i] % k) + k) % k];
if(c != -1 && (dp[j][r] == -1 || dp[j][r] < c + a[i]))
dp[j][r] = c + a[i];
}
}
if(dp[k][X % k] != -1)res = min(res, (X - dp[k][X % k]) / k);
}
printf("%lld\n", res);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 5005;
int mod = 998244353;
int n , m;
ll pw[N];
ll a[N][N];
int main()
{
// freopen(".inp","r",stdin);
// freopen(".out","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
pw[0] = 1;
for (int i = 1 ; i < N ; ++i) pw[i] = pw[i - 1] * (ll) m % mod;
a[0][0] = 1;
for (int j = 1 ; j < N ; ++j)
{
a[0][j] = 1;
for (int i = 1 ; i < N ; ++i)
{
a[i][j] = a[i - 1][j] * (ll) j % mod;
}
}
for (int i = 0 ; i < N ; ++i)
{
for (int j = 1 ; j < N ; ++j)
{
a[i][j] = (a[i][j] + a[i][j - 1]) % mod;
}
}
ll ans = pw[n] * n;
for (int i = 1 ; i <= n ; ++i)
{
for (int j = i + 1 ; j <= n ; ++j)
{
ans = (ans - a[j - i - 1][m - 1] * pw[n - (j - i + 1)] + (ll) mod * (ll) mod) % mod;
}
}
cout << ans;
}
|
#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;
int n, q;
const int N = 2e5+10;
int p[N], s[N], c[N];
map<int, int>res[N];
int f(int x) {
return (p[x] == x ? x : p[x] = f(p[x]));
}
void merge(int u, int v) {
u = f(u), v = f(v);
if(u == v)
return;
if(s[u] < s[v])
swap(u, v);
p[v] = u, s[u] += s[v];
for(auto i:res[v])
res[u][i.first]+=i.second;
res[v].clear();
}
void solve(){
cin >> n >> q;
for(int i=1;i<=n;++i) {
cin >> c[i]; p[i] = i, s[i] = 1, res[i][c[i]] = 1;
}
while(q--) {
int t, x, y;
cin >> t >> x >> y;
if(t == 1) {
merge(x, y);
} else {
cout << res[f(x)][y] << "\n";
}
}
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int t =1;
while(t--)
solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define range(a) a.begin(), a.end()
#define endl "\n"
#define Yes() cout << "Yes" << endl
#define No() cout << "No" << endl
#define MP make_pair
const unsigned long long mod = 1e9 + 7;
const long long INF = 1LL<<60;
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};
void chmin(long long &a, long long b) { if (a > b) a = b; }
void chmax(long long &a, long long b) { if (a < b) a = b; }
// UnionFind ex.)UnionFind uf(N); という風に定義する
// coding: https://youtu.be/TdR816rqc3s?t=726
// comment: https://youtu.be/TdR816rqc3s?t=6822
struct UnionFind {
vector<int> d;
vector<map<int, int>> mp;
UnionFind(int n = 0) : d(n, -1), mp(n) {} //コンストラクタ(n:頂点数)
int find(int x) {
if (d[x] < 0) return x;
return d[x] = find(d[x]);
}
bool unite(int x, int y) {
x = find(x); y = find(y);
if (x == y) return false;
if (d[x] > d[y]) swap(x,y);
d[x] += d[y];
d[y] = x;
for (auto itr = mp[y].begin(); itr != mp[y].end(); ++itr){
int cnt = itr->second;
while(cnt--){
mp[x][itr->first]++;
}
}
return true;
}
bool same(int x, int y) { return find(x) == find(y);}
int size(int x) { return -d[find(x)];}
void cnt(int x, int y){
cout << mp[find(x)][y] << endl;
}
void check(int c){
cout << c << ":";
for (auto itr = mp[c].begin(); itr != mp[c].end(); ++itr){
cout << "(" << itr->first << "->" << itr->second << "), ";
}
cout << endl;
}
};
int main(void){
ios::sync_with_stdio(0);
cin.tie(0);
int N, Q;
cin >> N >> Q;
UnionFind uf(N);
FOR(i,0,N){
int c;
cin >> c;
--c;
uf.mp[i][c]++;
}
while(Q--){
int t, a, b;
cin >> t >> a >> b;
--a;
--b;
if(t==1){
uf.unite(a, b);
//uf.check(uf.find(a));
}else{
uf.cnt(a, b);
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define fo(a,b) for(int64_t a=0;a<b;a++)
#define sor(a) sort(a.begin(),a.end())
#define rev(a) reverse(a.begin(),a.end())
#define ll int64_t
#define mod 1000000007
#define vl vector<int64_t>
#define vc vector<char>
#define vs vector<string>
#define np(vec) next_permutation(vec.begin(),vec.end())==true
#define en endl
#define vvl vector<vector<int64_t>>
#define stl stack<int64_t>
#define qul que<int64_t>
#define ms(a,b) memset(a,b,sizeof(a))
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pq priority_queue
#define pql priority_queue<int64_t>
#define pu push
#define pop pop()
#define top top()
#define pqr priority_queue<ll,vector<ll>,greater<ll>>
#define co cout
#define ci cin
#define wh while
#define size size()
#define front front()
int main() {
ll n;
cin>>n;
vl v(n);
vl v1(n);
ll a;
fo(i,n){
cin>>v.at(i);
}
v1.at(0)=v.at(0);
fo(i,n-1){
v1.at(i+1)=v1.at(i)+v.at(i+1);
}
vector<vector<ll>> vec(n, vector<ll>(n));
for (ll i = 0; i < n; i++) {
for (ll j = 0; j < n; j++) {
vec.at(i).at(j)=(v1.at(j))%(i+1);
}
}
vector<vector<ll>> ans(n, vector<ll>(n));
fo(i,n){
ans.at(0).at(i)=1;
}
fo(i,n-1){
vl kari(i+2);
fo(j,n){
ans.at(i+1).at(j)=kari.at(vec.at(i+1).at(j));
kari.at(vec.at(i+1).at(j))=(kari.at(vec.at(i+1).at(j))+ans.at(i).at(j))%mod;
}
}
ll f=0;
fo(i,n){
f=(f+ans.at(i).at(n-1))%mod;
}
cout<<f<<en;
} | #include<bits/stdc++.h>
#define ll long long int
#define mk make_pair
#define pb push_back
#define INF (ll)6e18
#define pii pair<ll,ll>
#define mod 1000000007 // 998244353
#define ff first
#define ss second
#define srt(v) if(!v.empty())sort(v.begin(),v.end())
#define rev(v) if(!v.empty())reverse(v.begin(),v.end())
#define PI 3.141592653589793238
#define pqr priority_queue<ll,vector<ll>,greater<ll>()>
using namespace std;
ll pow_mod(ll a,ll b)
{
ll res=1;
while(b!=0)
{
if(b&1)
{
res=(res*a)%mod;
}
a=(a*a)%mod;
b/=2;
}
return res;
}
ll inv_mod(ll x){
return pow_mod(x,mod-2);
}
const ll N=(ll)3e5;
ll inv[N];
ll fac[N];
void init(){
fac[0]=1;
for(ll i=1;i<N;i++)
fac[i]=(i*fac[i-1])%mod;
for(ll i=0;i<N;i++)
inv[i]=inv_mod(fac[i]);
}
ll ncr(ll n,ll r){
if(n<r)
return 0;
if(n==r||r==0)
return 1LL;
ll x=fac[n];
ll y=inv[n-r];
ll z=inv[r];
return ((x*y)%mod*z)%mod;
}
bool test_cases=0;
void solve()
{
ll n;
cin>>n;
ll a[n+1];
for(ll i=1;i<=n;i++)
cin>>a[i];
ll dp[n+1][n+1];
memset(dp,0,sizeof(dp));
ll ans=0;
ll p[n+1]={0};
for(ll i=1;i<=n;i++)
p[i]=p[i-1]+a[i];
for(ll k=1;k<=n;k++){
ll rem[n+1]={0};
for(ll i=0;i<=n;i++)
rem[i]=p[i]%k;
ll sum[k]={0};
if(k==1)
sum[0]=1;
for(ll i=1;i<=n;i++){
ll x=rem[i];
dp[i][k]+=sum[x];
dp[i][k]%=mod;
sum[x]+=dp[i][k-1];
sum[x]%=mod;
//cout<<"i "<<i<<" "<<"k "<<k<<" "<<dp[i][k]<<endl;
}
ans+=dp[n][k];
ans%=mod;
}
cout<<ans<<endl;
}
int main()
{
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// init(); //factorial calculations
//Start from Here.
ll t;
t=1;
if(test_cases)
cin>>t;
while(t--)
solve();
//Good Bye!
return 0;
} |
#include <iostream>
using namespace std;
int main() {
int X,Y,Z,sunuke;
cin >> X >> Y >> Z;
sunuke = (Y * Z - 1)/ X;
cout << sunuke << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define pp pair<int,int>
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rep2(i,m,n) for(int (i)=(m);(i)<(n);(i)++)
#define ll long long
#define ld long double
#define all(a) (a).begin(),(a).end()
#define mk make_pair
ll mod=998244353;
int inf=1000001000;
ll INF=1e18+5;
ll MOD=1000000007;
int main(){
int a,b,c;
cin>>a>>b>>c;
cout<<21-a-b-c;
return 0;
}
|
#include<bits/stdc++.h>
#define Tp template<typename Ty>
#define Ts template<typename Ty,typename... Ar>
#define Reg register
#define RI Reg int
#define Con const
#define CI Con int&
#define I inline
#define W while
#define N 500000
#define LL long long
using namespace std;
int n,nxt1[N+5],nxt2[N+5];char s1[N+5],s2[N+5];
int main()
{
RI i,j;LL t=0;scanf("%d%s%s",&n,s1+1,s2+1);
for(j=0,i=1;i<=n;++i) s1[i]&1&&(nxt1[j]=i,j=i);nxt1[j]=n+1;
for(j=0,i=1;i<=n;++i) s2[i]&1&&(nxt2[j]=i,j=i);nxt2[j]=n+1;
#define NA() return puts("-1"),0;
i=0,j=0;W(i<=n) if(i>=j) t+=i-j,i=nxt1[i],j=nxt2[j];
else if(nxt1[i]<=n) t+=nxt1[i]-i,i=nxt1[nxt1[i]];else NA();
return j>n?printf("%lld\n",t):puts("-1"),0;
} | #include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef pair<pll,ll> ppll;
typedef pair<pll,pll> ppp;
typedef vector<ll> vll;
typedef vector<pll> vp;
typedef priority_queue<pll,vp,greater<pll> > pqpll;
typedef priority_queue<ll, vll, greater<ll> > pqll;
#define rep(i,a,n) for(ll i = a;i < n;i++)
#define rrep(i,a,n) for(ll i = n-1; i >= a;i--)
#define LINF (ll)1e18
#define INF (int)1e9
#define fs first
#define sc second
#define EPS 1e-10
#define ALL(a) (a.begin(), a.end())
template<typename T>
ll sz(vector<T> &vec){ return (ll)vec.size(); }
template<typename T>
ll sz(priority_queue<T, vector<T> > &pq) {return (ll)pq.size(); }
template<typename T>
ll sz(priority_queue<T, vector<T>, greater<T> > &pq) {return (ll)pq.size(); }
ll sz(string &s) {return (ll)s.size(); }
ll gcd(ll a,ll b){ return ((!b) ?a :gcd(b, a%b)); }
ll lcm(ll a,ll b){ return a / gcd(a,b) * b; }
bool checkindex(ll i,ll n){ return (i < n && i >= 0); }
ll dx[4] = {1,0,-1,0},dy[4] = {0,1,0,-1};
int main(){
ll n;
cin >> n;
string s,t;
cin >> s >> t;
vll sum(n+1,0),sum2(n+1,0);
ll lsb = 0;
rep(i,0,n){
if(s[i] == '1') sum[i+1] = 1;
else sum[i+1] = 0;
if(t[i] == '1') {
sum2[i+1] = 1;
if(lsb == 0) lsb = i+1;
}
else sum2[i+1] = 0;
if(i != 0){
sum[i+1] += sum[i];
sum2[i+1] += sum2[i];
}
}
if(sum[n] - sum[max(lsb-1,0ll)] < sum2[n]){
cout << -1 << endl;
return 0;
}
if((sum[max(lsb-1,0ll)] + (sum[n] - sum[max(lsb-1,0ll)] - sum2[n])) % 2 == 1){
cout << -1 << endl;
return 0;
}
stack<ll> sta1,sta2;
ll ans = 0;
rep(i,0,n){
if(t[i] == '1'){
sta2.push(i);
}
if(s[i] == '1'){
if(sta1.size() > 0){
ans += i - sta1.top();
sta1.pop();
}
else if(i+1 < lsb){
sta1.push(i);
}
else if(sta2.size() > 0){
ans += i - sta2.top();
sta2.pop();
}
else{
sta1.push(i);
}
}
}
if(sta1.size() + sta2.size() == 0) cout << ans << endl;
else cout << -1 << endl;
}
|
#include <bits/stdc++.h>
#define mod 1000000007
#define MOD 1000000007
#define ll long long int
#define endl "\n"
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define MAXN 100009
#define pi 3.1415926535897932384626
using namespace std;
ll power(ll x, ll y) { ll ans = 1; x %= MOD; while (y) {if (y & 1)ans = (x * ans) % MOD; x = (x * x) % MOD; y >>= 1;} return ans;}
ll modInv(ll n) { return power(n, MOD - 2);}
void solve()
{
ll n, k, i, ans=0;
cin>>n>>k;
ll a[n+9], b[n+9];
vector<pair<ll, ll>> v;
for(i=0; i<n; i++)
{
cin>>a[i]>>b[i];
v.push_back({a[i], b[i]});
}
sort(v.begin(), v.end());
for(i=0; i<n; i++)
{
if(k>=v[i].first)
{
k+=v[i].second;
}
else
{
break;
}
}
cout<<k;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
fastio
ll t=1;
// cin>>t;
while(t--)
{
solve();
}
} | #include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#define int long long
using namespace std;
const int N = 1e6 + 10, M = 2e6 + 10, inf = 0x3f3f3f3f;
const long long Linf = 0x3f3f3f3f3f3f3f3f;
inline int read() {
bool sym = 0; int res = 0; char ch = getchar();
while (!isdigit(ch)) sym |= (ch == '-'), ch = getchar();
while (isdigit(ch)) res = (res << 3) + (res << 1) + (ch ^ 48), ch = getchar();
return sym ? -res : res;
}
int n, m, cnt, now, vis[N], dat[N], l[N], r[N], t[N];
signed main() {
n = read(); int k = read();
for (int i = 1; i <= n; i++) {
l[i] = read(); r[i] = read(); t[i] = read();
}
for (int i = 1; i <= n; i++) {
vis[l[i]] += t[i]; vis[r[i]] -= t[i]; m = max(m, r[i]);
}
for (int i = 0; i <= m; i++) {
now += vis[i]; if (now > k) {printf("No"); return 0;}
}
printf("Yes");
return 0;
} |
#include<bits/stdc++.h>
#define int long long
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define endl "\n"
using namespace std;
const int max_n = 5e3+10;
const int inf = 1e18;
const int Mod = 998244353;
int n,m;
int dp[22][max_n];
int fact[max_n];
int NCR[max_n][max_n];
int f(int j,int M){
if(j==-1 && M==0){
return 1;
}
if(j==-1) return 0;
if(dp[j][M]!=-1) return dp[j][M];
int ans=0;
for(int k=0;k<=n;k+=2){
if((k*(1LL<<j))>M) break;
int c = (NCR[n][k]*f(j-1,M-(k*(1LL<<j))))%Mod;
ans = (ans+c)%Mod;
}
dp[j][M]=ans;
return ans;
}
void fill(){
for(int n=0;n<max_n;n++){
for(int r=0;r<max_n;r++){
if(r>n){
NCR[n][r]=0;
}else if(r==0){
NCR[n][r]=1;
}else if(n==0){
NCR[n][r]=0;
}else{
NCR[n][r]=(NCR[n-1][r-1]+NCR[n-1][r])%Mod;
}
}
}
}
signed main(){
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
fill();
memset(dp,-1,sizeof(dp));
cin>>n>>m;
cout<<f(15,m)<<endl;
} | #include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <tuple>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <functional>
#include <numeric>
#include <iomanip>
#include <stdio.h>
//end of libraries
#define lnf 3999999999999999999
#define inf 999999999
#define PI 3.14159265359
#define endl "\n"
#define fi first
#define se second
#define pb push_back
#define ll long long
#define all(c) (c).begin(),(c).end()
#define sz(c) (int)(c).size()
#define mkp(a,b) make_pair(a,b)
#define make_unique(a) sort(all(a)),a.erase(unique(all(a)),a.end())
#define rsz(a,n) a.resize(n)
#define pii pair <int,int>
#define rep(i,n) for(int i = 0 ; i < n ; i++)
#define drep(i,n) for(int i = n-1 ; i >= 0 ; i--)
#define crep(i,x,n) for(int i = x ; i < n ; i++)
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
const int max_n = 5500;
using namespace std;
ll mod = 998244353 , fact[max_n];
ll binpow(ll a, ll b) {
a %= mod; ll res = 1;
while (b) { if (b%2) res = res * a % mod; a = a * a % mod; b /= 2; }
return res % mod;
}
void dofact() {
fact[0]=fact[1]=1;
rep(i,max_n-3){
if(i>1)fact[i] = fact[i-1]*i; fact[i] %= mod;
}
}
ll cnk(ll k , ll n){
if(k>n) return 0;
return (fact[n] * binpow(fact[k]*fact[n-k]%mod,mod-2)) % mod;
}
ll sub(ll a , ll b){
a%=mod; b%=mod;
return (((a-b)%mod)+mod)%mod;
}
ll prod(ll a , ll b){
a%=mod; b%=mod;
return ((a*b)%mod);
}
ll add(ll a , ll b){
a%=mod; b%=mod;
return (a+b)%mod;
}
ll dp[max_n] , nedp[max_n];
vector <ll> a;
int n,m;
void read() {
cin >> n >> m;
int x = 1;
while(x<=m) {
a.pb(x);
x*=2;
}
}
int main(){
fcin;
dofact();
read();
dp[0] = 1;
rep(idx,sz(a)){
int i = a[idx];
rep(j,m+1) nedp[j]=dp[j];
for(int j = 0 ; j <= m ; j++) {
if(dp[j]==0) continue;
int x = 2;
while(j+x*i <= m) {
int nxt = j+x*i;
nedp[nxt] = add(nedp[nxt],prod(dp[j],cnk(x,n)));
x+=2;
}
}
rep(j,m+1) dp[j] = nedp[j];
// rep(j,m+1) {
// cout << dp[j] << " ";
// }cout << "\n";
}
cout << dp[m] << "\n";
/*
*/
return 0;
}
|
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++)
cin >> a[i];
int res = 2000000000;
for (int i = 0; i < (1 << (n - 1)); i++)
{
int xored = 0;
int ored = 0;
for (int j = 0; j <= n; j++)
{
if (j < n)
ored |= a[j];
if (j == n || (i >> j & 1))
xored ^= ored, ored = 0;
}
res = min(res, xored);
}
cout << res;
return 0;
}
| #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <math.h>
#include <queue>
#include <numeric>
using namespace std;
using ll = long long;
#define For(i,a,b) for(ll i=(a);i<(ll)(b);i++)
#define rFor(i,a,b) for(ll i=(a);i>=(ll)(b);i--)
#define co(a) cout<<a<<endl
#define SORT(a) sort((a).begin(),(a).end())
#define REV(a) reverse((a).begin(),(a).end())
#define cfs(a) cout<<fixed<<setprecision(a)
int main() {
ll N;
cin >> N;
vector<ll> A(N);
ll sum = 0;
For(i, 0, N) {
cin >> A[i];
}
if (N == 1) {
co(A[0]);
return 0;
}
vector<ll> temps(N);
ll sep, temp;
ll Min = 100000000000;
For(i, 0, 1 << (N - 1)) {
sep = 0;
temp = 0;
For(j, 0, (N - 1)) {
if ((i >> j) & 1) {
temps[j] = 0;
For(k, sep, j + 1) {
temps[j] |= A[k];
}
sep = j + 1;
temp ^= temps[j];
//co(i << " " << j << " " << temp << " " << temps[j]);
}
if (j == N - 2) {
temps[j + 1] = 0;
For(k, sep, N) {
temps[j + 1] |= A[k];
}
temp ^= temps[j + 1];
//co(i << " " << j << " " << temp << " " << temps[j]);
}
}
Min = min(Min, temp);
}
co(Min);
} |
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <list>
#include <set>
#include <map>
#include <vector>
#include <math.h>
#include <fstream>
#include <sstream>
#include <unordered_map>
#include <ctime>
#include <assert.h>
using namespace std;
//#define ATCODER
#ifdef ATCODER
#include <atcoder/all>
#endif
const unsigned long long MOD = (unsigned long long)(1e9 + 7);
#define FROM_0(i, n) for(int i = 0; i < (n); i++)
#define FROM_S(i, s, e) for(int i = (s); i < (e); i++)
#define ITER_BEGIN_END(iter, vec) for(auto iter = vec.begin(); iter != vec.end(); iter++)
string s[200];
int main()
{
int h, w;
cin >> h >> w;
FROM_0(i, h)
cin >> s[i];
int count = 0;
FROM_0(i, h)
{
FROM_0(j, w)
{
if (s[i][j] == '#')
continue;
if (i < h - 1 && s[i + 1][j] == '.')
count++;
if (j < w - 1 && s[i][j + 1] == '.')
count++;
}
}
cout << count << endl;
return 0;
}
| #include <stdint.h>
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define int long long
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);//cout.precision(dbl::max_digits10);
#define pb(x) push_back(x)
#define mod 1000000007ll //998244353ll
#define vi vector<int>
#define v(x) vector< x >
#define lld long double
#define pii pair<int, int>
#define ff first
#define ss second
#define endl "\n"
#define all(x) (x).begin(), (x).end()
#define rep(i,x,y) for(int i=x; i<y; i++)
#define repe(i,x,y) for(int i=x; i<=y; i++)
#define fill(a,b) memset(a, b, sizeof(a))
#define setbits(x) __builtin_popcountll(x)
#define print2d(dp,n,m) for(int i=0;i<=n;i++){for(int j=0;j<=m;j++)cout<<dp[i][j]<<" ";cout<<"\n";}
typedef std::numeric_limits< double > dbl;
using namespace __gnu_pbds;
using namespace std;
lld pi=3.1415926535897932;
/*-----------------------------------------------------------*/
int solve(){
int n,k,x;
cin>>n>>k>>x;
return max(n+k,max(k+x,x+n));
}
int32_t main(){
IOS
int t=1;
// cin>>t;
// while(t--)
// if(!solve())
// cout<<"NO"<<endl;
while(t--)
// solve();
cout<<solve()<<endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+10;
bool p[maxn];
int prime[maxn],mu[maxn],cnt;
long long sum[maxn];
int main()
{
int l,r;
scanf("%d %d",&l,&r);
mu[1]=1;
for(int i=2;i<=r;i++){
if(!p[i]){prime[++cnt]=i,mu[i]=-1;}
for(int j=1;i*prime[j]<=r&&j<=cnt;j++){
p[i*prime[j]]=1;
if(i%prime[j]==0){mu[i*prime[j]]=0;break;}
mu[i*prime[j]]=-mu[i];
}
}
long long ans=0;
for(int d=2;d<=r;d++){
for(int k=1;k<=r/d;k++){
int L=ceil((double)l/(k*d)),R=r/(k*d);
ans+=(long long)(R-L+1)*(R-L+1)*mu[k];
}
}
if(l==1)l++;
for(int i=l;i<=r;i++){
for(int j=i*2;j<=r;j+=i)ans-=2;
}
ans-=r-l+1;
printf("%lld\n",ans);
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_equal<int>,
// rb_tree_tag, tree_order_statistics_node_update> oset;
//========================================================
const int N = 2e5+5, K = 21, M = 5e4+5, mod = 1e9+7, len = 30, inf = 0x3f3f3f3f;
ll l, r;
ll cnt(ll d)
{
ll c = r / d - (l - 1) / d;
return c;
}
void run_case()
{
cin >> l >> r;
ll a[r + 1];
ll ans = 0;
for(int i=r; i>1; i--)
{
a[i] = cnt(i) * (cnt(i) - 1);
for(int j=i + i; j<=r; j += i)
a[i] -= a[j];
}
for(int i=2; i<=r; i++)
{
if(i < l) continue;
a[i] -= (cnt(i) - 1) * 2;
}
for(int i=2; i<=r; i++) ans += a[i];
cout << ans << endl;
}
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 = 1;
// cin >> T;
while(T--)
run_case();
}
/*
1000000000
*/ |
#include<bits/stdc++.h>
#define int long long
#define N 100010
#define re register
using namespace std;
int n,m,p[N],q[N],mi,tmp;
vector<int> a[N],b[N];
char s[N];
template <class T> inline void read(T &x)
{
x=0;int g=1;char s=getchar();
for (;s<'0'||s>'9';s=getchar()) if (s=='-') g=-1;
for (;s>='0'&&s<='9';s=getchar()) x=(x<<1)+(x<<3)+(s^48);
x*=g;
}
signed main()
{
re int i,j,x,z,op;
read(n);
scanf("%s",s+1);
if (s[1]==s[n])
{
if (n<=3) {printf("-1");return 0;}
for (i=2;i<n-1;i++)
if (s[i]!=s[1]&&s[i+1]!=s[1])
{
printf("2");
return 0;
}
printf("-1");
}
else
printf("1");
return 0;
} | #include<bits/stdc++.h>
#define pb push_back
#define eb emplace_back
using namespace std;
//#define DEBUG
#ifdef DEBUG
template<typename ...Args>
int debug(const Args &...args){
return fprintf(stderr,args...);
}
#else
#define debug(...) void()
#endif
typedef unsigned long long ull;
typedef unsigned uint;
typedef long long ll;
#define G getchar()
int read()
{
int x=0; bool flg=false; char ch=G;
for (;!isdigit(ch);ch=G) if (ch=='-') flg=true;
for (;isdigit(ch);ch=G) x=(x<<3)+(x<<1)+(ch^48);
return flg?-x:x;
}
#undef G
#define fi first
#define se second
/*
const int mod=;
inline int upd(const int &x){return x+(x>>31&mod);}
inline void add(int &x,const int &y){x=upd(x+y-mod);}
inline void iadd(int &x,const int &y){x=upd(x-y);}
int qpow(int x,int y){
int res=1;
for (;y;y>>=1,x=1LL*x*x%mod)
if (y&1) res=1LL*res*x%mod;
return res;
}
*/
//typedef pair<int,int> P;
#define rep(i,l,r) for (int i(l),_##i(r);i<=_##i;i++)
#define per(i,l,r) for (int i(r),_##i(l);i>=_##i;i--)
#define all(x) (x).begin(),(x).end()
#define forall(x,y) for (const int &y: e[x])
int n; char s[100010];
int dp[26];
void chk(int &x,int y){
if (y<x) x=y;
}
int solve(){
scanf("%d%s",&n,s+1);
memset(dp,0x3f,sizeof dp);
dp[s[n]-'a']=0;
per(i,1,n){
int t=1e9;
rep(j,0,25) if (j+'a'!=s[i]) t=min(t,dp[j]);
if (t==1e9) continue;
if (i==1) return t+1;
chk(dp[s[i-1]-'a'],t+1);
}
return -1;
}
int main()
{
for (int T=1;T--;) printf("%d\n",solve());
return 0;
} |
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cassert>
#include <string>
#include <set>
#include <map>
#include <random>
#include <bitset>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#include <queue>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using namespace std;
using ll = long long;
using ul = unsigned long long;
using ld = long double;
ll md(ll i, vector<ll> &a) {
auto it = lower_bound(all(a), i);
ll ans = 1e18;
if (it != a.end())
ans = min(ans, *it - i);
if (it != a.begin())
ans = min(ans, i - *prev(it));
return ans;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
ll n;
cin >> n;
vector<vector<ll>> a(3);
ll x;
char c;
string cc = "RGB";
rep(i, 2 * n) {
cin >> x >> c;
a[cc.find(c)].push_back(x);
}
for (auto &p : a)
sort(all(p));
sort(all(a), [](const auto &p1, const auto &p2) { return p1.size() % 2 > p2.size() % 2; });
if (a[0].size() % 2 == 0) {
cout << 0;
return 0;
}
ll ans = 1e18;
for (ll i : a[0])
ans = min(ans, md(i, a[1]));
rep(t, 2) {
ll cm = 1e18;
for (ll i : a[2]) {
ans = min(ans, cm + md(i, a[1]));
cm = min(cm, md(i, a[0]));
}
swap(a[0], a[1]);
}
cout << ans;
}
| #include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair <int, int>
#define pll pair <ll, ll>
#define pci pair <char, int>
#define pld pair <ld, ld>
#define ppld pair <pld, pld>
#define ppll pair <pll, pll>
#define pldl pair <ld, ll>
#define vll vector <ll>
#define vvll vector <vll>
#define vpll vector <pll>
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define mll map <ll, ll>
#define fastmap gp_hash_table
#define cd complex <double>
#define vcd vector <cd>
#define PI 3.14159265358979
#pragma 03
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll INF = 1e18;
char col[] = {'R', 'G', 'B'};
ll suff_min[200005][3], pref_min[200005][3];
pair <ll, char> dogs[200005];
ll mn[3], mx[3], cnt[3];
ll get_id(char c){
for (ll i = 0; i < 3; i++){
if (c == col[i]) return i;
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
ll n; cin >> n;
for (ll i = 0; i < 3; i++) mn[i] = INF;
for (ll i = 1; i <= 2 * n; i++){
cin >> dogs[i].fi >> dogs[i].se;
cnt[get_id(dogs[i].se)]++;
}
if ((cnt[0] % 2 == 0) && (cnt[1] % 2 == 0) && (cnt[2] % 2 == 0)){
cout << 0 << "\n"; return 0;
}
sort(dogs + 1, dogs + 2 * n + 1);
ll ans = INF;
pref_min[0][0] = pref_min[0][1] = pref_min[0][2] = INF;
suff_min[2 * n + 1][0] = suff_min[2 * n + 1][1] = suff_min[2 * n + 1][2] = INF;
for (ll i = 1; i <= 2 * n; i++){
for (ll c = 0; c < 3; c++) pref_min[i][c] = pref_min[i - 1][c];
ll id = get_id(dogs[i].se);
if (cnt[id] & 1){
for (ll c = 0; c < 3; c++){
if (c == id) continue;
if (mx[c] == 0) continue;
if (cnt[c] & 1) ans = min(ans, dogs[i].fi - mx[c]);
else pref_min[i][id] = min(pref_min[i][id], dogs[i].fi - mx[c]);
}
}
else{
for (ll c = 0; c < 3; c++){
if (c == id) continue;
if (mx[c] == 0) continue;
pref_min[i][c] = min(pref_min[i][c], dogs[i].fi - mx[c]);
}
}
mx[id] = max(mx[id], dogs[i].fi);
}
for (ll i = 2 * n; i >= 1; i--){
for (ll c = 0; c < 3; c++) suff_min[i][c] = suff_min[i + 1][c];
ll id = get_id(dogs[i].se);
if (cnt[id] & 1){
for (ll c = 0; c < 3; c++){
if (c == id) continue;
if (mn[c] == INF) continue;
if (cnt[c] % 2 == 0) suff_min[i][id] = min(suff_min[i][id], mn[c] - dogs[i].fi);
}
}
else{
for (ll c = 0; c < 3; c++){
if (c == id) continue;
if (mn[c] == INF) continue;
suff_min[i][c] = min(suff_min[i][c], mn[c] - dogs[i].fi);
}
}
mn[id] = min(mn[id], dogs[i].fi);
}
for (ll s = 1; s < 2 * n; s++){
for (ll c1 = 0; c1 < 3; c1++){
for (ll c2 = 0; c2 < 3; c2++){
if (c1 == c2) continue;
ans = min(ans, pref_min[s][c1] + suff_min[s + 1][c2]);
}
}
}
cout << ans << "\n";
} |
#line 2 "header.hpp"
//%snippet.set('header')%
//%snippet.fold()%
#ifndef HEADER_H
#define HEADER_H
// template version 2.0
using namespace std;
#include <bits/stdc++.h>
// varibable settings
template <class T> constexpr T inf = numeric_limits<T>::max() / 2.1;
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (ll i = (ll)(a); i < (ll)(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define _rrep(i, n) rrepi(i, 0, n)
#define rrepi(i, a, b) for (ll i = (ll)((b)-1); i >= (ll)(a); --i)
#define r_rep(...) _overload3(__VA_ARGS__, rrepi, _rrep, )(__VA_ARGS__)
#define each(i, a) for (auto &&i : a)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define mt(...) make_tuple(__VA_ARGS__)
#define ub upper_bound
#define lb lower_bound
#define lpos(A, x) (lower_bound(all(A), x) - A.begin())
#define upos(A, x) (upper_bound(all(A), x) - A.begin())
template <class T, class U> inline void chmax(T &a, const U &b) { if ((a) < (b)) (a) = (b); }
template <class T, class U> inline void chmin(T &a, const U &b) { if ((a) > (b)) (a) = (b); }
template <typename X, typename T> auto make_table(X x, T a) { return vector<T>(x, a); }
template <typename X, typename Y, typename Z, typename... Zs> auto make_table(X x, Y y, Z z, Zs... zs) { auto cont = make_table(y, z, zs...); return vector<decltype(cont)>(x, cont); }
template <class T> T cdiv(T a, T b){ assert(a >= 0 && b > 0); return (a+b-1)/b; }
#define is_in(x, a, b) ((a) <= (x) && (x) < (b))
#define uni(x) sort(all(x)); x.erase(unique(all(x)), x.end())
#define slice(l, r) substr(l, r - l)
typedef long long ll;
typedef long double ld;
using vl = vector<ll>;
using vvl = vector<vl>;
using pll = pair<ll, ll>;
template <typename T>
using PQ = priority_queue<T, vector<T>, greater<T>>;
void check_input() { assert(cin.eof() == 0); int tmp; cin >> tmp; assert(cin.eof() == 1); }
#if defined(PCM) || defined(LOCAL)
#else
#define dump(...) ;
#define dump_1d(...) ;
#define dump_2d(...) ;
#define cerrendl ;
#endif
#endif /* HEADER_H */
//%snippet.end()%
#line 2 "solve.cpp"
template<class T=ll> using vec = vector<T>;
struct Fast { Fast() { std::cin.tie(0); ios::sync_with_stdio(false); } } fast;
int solve() {
int n;cin>>n;
ll x;cin>>x;
dump(n, x);
vector<ll> a(n+1);
rep(i, 1, n+1) { cin>>a[i]; }
dump(a);
vec<ll> d(n+1);
rep(i, 1, n){
d[i] = a[i+1] / a[i];
}
dump(d);
vec<ll> xb(n+1);
r_rep(i, 1, n+1){
xb[i] = x / a[i];
x %= a[i];
}
dump(xb);
dump(x);
vec<ll> dp0(n+1);
vec<ll> dp1(n+1);
dp0[0] = 1;
rep(i, 0, n){ // kubaru
// o[i+1]は[0, d[i+1])をとる
// from dp0
dp0[i+1] += dp0[i]; // take 0;
if (xb[i+1]>=1){
dp1[i+1] += dp0[i]; // take [d-xb, d)
}
// from dp1
if (xb[i+1]+1 == d[i+1]){
dp1[i+1] += dp1[i]; // take 0;
}
else{
dp0[i+1] += dp1[i]; // take 0;
dp1[i+1] += dp1[i]; // take [d-xb, d)
}
}
dump(dp0);
dump(dp1);
cout << dp0[n] << endl;
return 0;
}
int main(){/*{{{*/
solve();
#if defined(PCM) || defined(LOCAL)
check_input();
#endif
return 0;
}/*}}}*/
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> iint;
typedef pair<ll,ll> llll;
#define ALL(x) (x).begin(),(x).end()
const ll zero = 0;
const ll one = 1;
const ll INF = 9223372036854775807; //10^18
const int inINF = 2147483647; //10^9
const ll MOD = 1000000007; //10^9+7
const ll MOD2 = 998244353;
void Yes() {printf("Yes\n");}
void No() {printf("No\n");}
void YES() {printf("YES\n");}
void NO() {printf("NO\n");}
ll N, X;
map<pair<ll, ll>, ll> memo;
vector<ll> A;
ll solve(ll Y, ll ind){
if(memo.count({Y, ind})){
return memo[{Y, ind}];
}
if(ind == N - 1){
if(Y % A[ind] == 0){
return 1;
}
else{
return 0;
}
}
ll low = Y / A[ind+1] * A[ind+1];
ll up = Y / A[ind+1] * A[ind+1] + A[ind+1];
ll ret = 0;
ret += solve(low, ind+1);
if(Y % A[ind+1] != 0){
ret += solve(up, ind + 1);
}
return memo[{Y, ind}] = ret;
}
int main(){
cin >> N >> X;
A.resize(N);
for (ll i = 0; i < N; i++) {
cin >> A[i];
}
printf("%lld\n", solve(X, 0));
} |
#include <bits/stdc++.h>
constexpr int DEBUG = 0;
using namespace std;
using int64 = long long;
#define REPEAT(i, l, r) for (int i = (int)(l); i < (int)(r); i++)
constexpr int64 P = 998244353;
class FF {
private:
int64 x_;
void Normalize() {
if (0 <= x_ && x_ < P) { return; } x_ %= P; if (x_ < 0) { x_ += P; }
}
public:
FF(int64 x) : x_(x) { Normalize(); }
FF() : x_(0) {}
int64 Value() const { return x_; }
FF operator+(FF o) const { FF r(*this); r += o; return r; }
FF operator-(FF o) const { FF r(*this); r -= o; return r; }
FF operator* (FF o) const { FF r(*this); r *= o; return r; }
FF operator/ (FF o) const { return (*this) * o.Power(P - 2); }
void operator+= (FF o) { x_ += o.x_; if (x_ >= P) x_ -= P; }
void operator-= (FF o) { x_ -= o.x_; if (x_ < 0) x_ += P; }
void operator*= (FF o) { x_ = (x_ * o.x_) % P; }
FF Power(int64 p) const {
if (p < 0) { return Power(-p); } FF x(*this), r(1);
while (p) { if (p % 2) { r *= x; } x *= x; p /= 2; } return r;
}
};
ostream& operator<<(ostream& s, const FF& x) { s << x.Value(); return s; }
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n; cin >> n;
vector<int> fs(n);
REPEAT(i, 0, n) {
cin >> fs[i];
fs[i]--;
}
vector<int> ts(n, -1);
int t = 0;
vector<int> cs;
REPEAT(s, 0, n) {
if (ts[s] >= 0) continue;
ts[s] = t;
t++;
int v = fs[s];
while (true) {
if (ts[v] >= 0) {
if (ts[v] >= ts[s]) {
cs.push_back(t - ts[v]);
}
break;
} else {
ts[v] = t;
t++;
v = fs[v];
}
}
}
FF ans = FF(2).Power(cs.size()) - 1;
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define deb(x) cout<< #x << " " << x << "\n";
#define MAX 9223372036854775807
#define MIN -9223372036854775807
#define setbits(n) __builtin_popcountll(n)
#define mkunique(a) a.resize(unique(a.begin(),a.end())-a.begin());
const ll mod=998244353;
const int N=2e5+10;
vector<ll> a[N];
vector<bool> vis(N);
ll dfs(ll v){
vis[v]=true;
ll s=1;
for(ll u: a[v]){
if(!vis[u])
s+=dfs(u);
}
return s;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll T=clock();
ll n;
cin>>n;
for(ll i=0;i<n;i++){
ll r;
cin>>r; r--;
a[r].pb(i);
a[i].pb(r);
}
ll ans=1;
for(ll i=0;i<n;i++){
if(!vis[i]){
ll num=dfs(i);
ans=ans*2%mod;
}
}
ans=(ans-1+mod)%mod;
cout<<ans;
cerr<<"\n\nTIME: "<<(double)(clock()-T)/CLOCKS_PER_SEC<<" sec\n";
T = clock();
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define int unsigned long long
#define PB push_back
#define ALL(x) (x).begin(), (x).end()
#define ALLR(x) (x).rbegin(), (x).rend()
#define MP make_pair
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {
return '"' + s + '"';
}
string to_string(const char* s) {
return to_string((string) s);
}
string to_string(bool b) {
return (b ? "true" : "false");
}
string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template <size_t N>
string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++) {
res += static_cast<char>('0' + v[i]);
}
return res;
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#ifdef KILLBUG
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
#define FAST_IO ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<string> vs;
typedef vector<bool> vb;
const int INF = LONG_LONG_MAX/10;
const double PI = 3.141592653589793238;
const int MOD = 998244353;
bool many = false;
struct Solve {
string endl = "\n";
Solve() {
int x, y, a, b;
cin >> x >> y >> a >> b;
int str = x;
int exp = 0;
for (int i = 0; str < b/(a-1) && str*a < y; i++) {
str *= a;
exp++;
}
debug(str, exp);
if (str < y) {
exp += (y-str-1)/b;
}
cout << exp << endl;
}
};
signed main() {
FAST_IO;
int CASES=1;
if (many)
cin >> CASES;
for (int i = 1; i <= CASES; i++) {
Solve me;
}
}
// g++ -Wall -Wextra -pedantic -std=c++17 -O2 -Wshadow -Wformat=2 -Wfloat-equal -Wconversion -Wlogical-op -Wshift-overflow=2 -Wduplicated-cond -Wcast-qual -Wcast-align -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -D_FORTIFY_SOURCE=2 -fsanitize=address -fsanitize=undefined -fno-sanitize-recover -fstack-protector
| #include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define db double
#define rep(x,a,b) for(int x=(a);x<=(b);x++)
#define per(x,a,b) for(int x=(a);x>=(b);x--)
#define reP(x,a,b) for(int x=(a);x<(b);x++)
#define Per(x,a,b) for(int x=(a);x>(b);x--)
#define scf(a) scanf("%d",&a)
#define scfll(a) scanf("%lld",&a)
#define scfdb(a) scanf("%lf",&a)
#define ptf(a) printf("%d",a)
#define ptfll(a) printf("%lld",a)
#define ptfsp(a) printf("%d ",a)
#define ptfllsp(a) printf("%lld ",a)
#define pli(a,b) make_pair(a,b)
#define pb push_back
#define el puts("")
#define FS first
#define SE second
#define PI acos(-1)
#define ls (pos<<1)
#define rs (pos<<1|1)
/*ios::sync_with_stdio(false);
freopen("in.txt","r",stdin);
freopen("1.out","w",stdout);*/
using namespace std;
const ll mod=1e9+7;
const int maxn=1e6+5;
int main(){
ull x,y,a,b;
scfll(x);scfll(y);scfll(a);scfll(b);
ull res=0,ans=0;
for(ll i=x;i<y;i=i*a){
ans=max(ans,(y-i-1)/b+res);
if((double)i*a>(double)y) break;
res++;
}
cout<<ans;
}
|
#include<bits/stdc++.h>
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--)
#define pii pair<int,int>
#define fi first
#define se second
#define PB push_back
#define ll long long
#define ull unsigned long long
#define y1 orzkcz
using namespace std;
const int N=3005;
const int mo=998244353;
int n,k,f[N][N];
int main(){
scanf("%d%d",&n,&k);
f[0][0]=1;
For(i,0,n) Rep(j,i,0){
f[i+1][j+1]=(f[i+1][j+1]+f[i][j])%mo;
if (j%2==0) f[i][j/2]=(f[i][j/2]+f[i][j])%mo;
}
cout<<f[n][k]<<endl;
} | #include <stdio.h>
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <deque>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <functional>
#include <iomanip>
#include <cstdint>
#define ll long long
#define rep2(i,a,b) for(int i=a;i<=b;++i)
#define rep(i,n) for(int i=0;i<n;i++)
#define rep3(i,a,b) for(int i=a;i>=b;i--)
#define REP(e,v) for(auto e:v)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
#define mt make_tuple
#define pq priority_queue<int>
#define pqg priority_queue<int,vector<int>,greater<int>>
#define pb push_back
#define vec vector
#define vi vec<int>
#define vl vec<ll>
#define vivi vec<vi>
#define vlvl vec<vl>
#define vpii vec<pii>
#define vpll vec<pll>
#define vbl vec<bool>
#define endl "\n"
#define ALL(c) (c).begin(),(c).end()
#define D(x) cout << (#x) << " is " << x << endl;
using namespace std;
int in() {int x;scanf("%d",&x);return x;}
ll lin() {ll x;scanf("%lld",&x);return x;}
string stin(){string s;cin>>s;return s;}
void drop(string s){cout<<s<<endl; exit(0);}
void drop(int x){cout<<x<<endl; exit(0);}
void drop(ll x){cout<<x<<endl; exit(0);}
int main(){
int n = in();
ll w = lin();
vl plus(200010);
rep(i,n){
int s=in(),t=in(),p=in();
plus[s] += p;
plus[t] -= p;
}
ll tmp = 0;
rep(i,200010){
tmp += plus[i];
if(tmp > w)drop("No");
}
drop("Yes");
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define sz(v) (int)v.size()
#define all(v) v.begin(), v.end()
void dbg_out() { cerr << "\b\b]\n"; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T){ cerr << H << ", "; dbg_out(T...);}
#define watch(...) cerr << "[" << #__VA_ARGS__ << "]: [", dbg_out(__VA_ARGS__)
int main(){
ios_base::sync_with_stdio(false); cin.tie(nullptr);
int n, m; cin >> n >> m;
vector <pair <int, int>> A(m);
for (auto &i: A) cin >> i.first >> i.second;
int k; cin >> k;
vector <int> dishes(n+1, 0);
vector <pair <int, int>> person(k);
for (int i = 0; i < k; ++i) {
int a, b; cin >> a >> b;
person[i] = {a, b};
}
int ans = 0;
function <void(int)> go = [&](int pos) {
if (pos >= k) {
int res = 0;
for (auto i: A) {
if (dishes[i.first] and dishes[i.second]) res++;
}
ans = max(ans, res);
return;
}
dishes[person[pos].first]++;
go(pos+1);
dishes[person[pos].first]--;
dishes[person[pos].second]++;
go(pos+1);
dishes[person[pos].second]--;
};
go(0);
cout << ans;
}
/*
*/ | #include <bits/stdc++.h>
using namespace std;
struct p{
long long int w;
long long int c;
};
p tab[200007];
vector <long long int> edges[200007];
int main()
{
long long int m, x, y, n, j, i, z=-1000000000000000007, a, b=1, m1=0, m2=0;
cin >> n >> m;
for(i=0; i<n; i++){
cin >> a;
tab[i].w=a;
tab[i].c=-1000000000000000007;
}
for(i=0; i<m; i++){
cin >> a >> b;
edges[a-1].push_back(b-1);
}
for(i=0; i<n; i++){
for(j=0; j<edges[i].size(); j++){
tab[edges[i][j]].c=max(tab[edges[i][j]].c, max(tab[edges[i][j]].w-tab[i].w+tab[i].c, tab[edges[i][j]].w-tab[i].w));
z=max(z, tab[edges[i][j]].c);
}
}
cout << z << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define x first
#define y second
#define FOR(i, m, n) for (ll i(m); i < n; i++)
#define DWN(i, m, n) for (ll i(m); i >= n; i--)
#define REP(i, n) FOR(i, 0, n)
#define DW(i, n) DWN(i, n, 0)
#define F(n) REP(i, n)
#define FF(n) REP(j, n)
#define D(n) DW(i, n)
#define DD(n) DW(j, n)
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using tll = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vpll = vector<pll>;
using vtll = vector<tll>;
using gr = vector<vll>;
using wgr = vector<vpll>;
void add_edge(gr&g,ll x, ll y){ g[x].pb(y);g[y].pb(x); }
void add_edge(wgr&g,ll x, ll y, ll z){ g[x].eb(y,z);g[y].eb(x,z); }
template<typename T,typename U>
ostream& operator<<(ostream& os, const pair<T,U>& p) {
cerr << ' ' << p.x << ',' << p.y; return os; }
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
for(auto x: v) os << ' ' << x; return os; }
template <typename T>
ostream& operator<<(ostream& os, const set<T>& v) {
for(auto x: v) os << ' ' << x; return os; }
template<typename T,typename U>
ostream& operator<<(ostream& os, const map<T,U>& v) {
for(auto x: v) os << ' ' << x; return os; }
struct d_ {
template<typename T> d_& operator,(const T& x) {
cerr << ' ' << x; return *this;}
} d_t;
#define dbg(args ...) { d_t,"|",__LINE__,"|",":",args,"\n"; }
#define deb(X ...) dbg(#X, "=", X);
#define EPS (1e-10)
#define INF (1LL<<61)
#define YES(x) cout << (x ? "YES" : "NO") << endl;
#define CL(A,I) (memset(A,I,sizeof(A)))
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
const ll MOD = 998244353;
ll mod(ll a, ll b=MOD) {
return ((a%b)+b)%b;
}
ll extended_euclid(ll a,ll b,ll &x,ll &y) {
if (a == 0) { x = 0; y = 1; return b;}
ll x1, y1; ll d = extended_euclid(b%a, a, x1, y1);
x = y1 - (b / a) * x1;
y = x1;
return d;
}
ll mod_inverse(ll a, ll n=MOD) {
ll x, y;
ll d = extended_euclid(a, n, x, y);
if (d > 1) return -1;
return mod(x,n);
}
struct Mt {
ll x = 0;
Mt(){}
Mt(ll y):x(mod(y)){}
bool operator==(Mt &y){return x==y.x;}
Mt operator+(Mt y){return mod(x+y.x);}
Mt operator-(){return mod(-x);}
Mt operator-(Mt y){return mod(x-y.x);}
Mt operator*(Mt y){return mod(x*y.x);}
Mt operator/(Mt y){return mod(x*mod_inverse(y.x));}
Mt operator+=(Mt y){return x=(*this+y).x;}
Mt operator-=(Mt y){return x=(*this-y).x;}
Mt operator*=(Mt y){return x=(*this*y).x;}
Mt operator/=(Mt y){return x=(*this/y).x;}
Mt pow(ll p) {
Mt ret=1, z=x;
while(p) {
if (p&1) ret*=z;
z*=z; p/=2;
}
return ret;
}
Mt operator^(Mt y){return this->pow(y.x);}
};
ostream& operator<<(ostream& os, const Mt&m){
os << m.x; return os;
}
const ll N = 5e3+7;
Mt pw[N][N];
int main(void) {
ios_base::sync_with_stdio(false);
ll n,m; cin >> n >> m;
F(m+1) pw[i][0] = 1;
FOR(i,1,m+1) FOR(j,1,n+1) {
pw[i][j] = pw[i][j-1] * i;
}
Mt ret = Mt(n) * pw[m][n];
FOR(x,1,m+1) FOR(d,1,n+1) {
Mt cur = pw[m-x][d-1] * pw[m][n-d-1];
ret -= cur * (n-d);
}
cout << ret << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define sync std::ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define int long long
#define loop(i,n) for(int i=0;i<n;i++)
#define FOR(i,a,b,c) for(int i=a;i<=b;i=i+c)
#define RFOR(i,a,b,c) for(int i=a;i>=b;i=i-c)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector <pair<int,int>> vp;
#define deb(x) cout<<#x<<" = "<<x<<endl
#define all(x) (x).begin(),(x).end()
#define reset(a) memset(a,0,sizeof(a))
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define ins insert
#define F first
#define S second
#define mkp make_pair
#define mkt make_tuple
#define bitcount(x) __builtin_popcountll(x)
#define gcd(a,b) (__gcd(a,b))
#define lcm(a,b) ((a*b)/gcd(a,b))
#define PI 3.1415926535897932384626
const int mod = 998244353 ;//1e9+7;
const int N=2e5+5;
//============
void solve()
{
int n,ans=0;
string s,t;
cin>>n>>s;
char a,b,c;
stack<char> stk;
for(auto et: s)
{
stk.push(et);
if(et=='x' && stk.size()>2)
{
a=stk.top(); stk.pop();
b=stk.top(); stk.pop();
c=stk.top(); stk.pop();
if(a=='x' && b=='o' && c=='f')
ans++;
else
{
stk.push(c);
stk.push(b);
stk.push(a);
}
}
}
cout<<n-3*ans;
}
int32_t main()
{
sync;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
int t=1;
//cin>>t;
FOR(i,1,t,1)
{
//cout<<"Case #"<<i<<": "<<;
solve();
cout<<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<functional>
#include<algorithm>
#include<iostream>
#include<iomanip>
#include<cstring>
#include<cstdlib>
#include<utility>
#include<string>
#include<cstdio>
#include<vector>
#include<bitset>
#include<cmath>
#include<ctime>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
using namespace std;
const int inf=2147483647,dx[]={-1,0,1,0},dy[]={0,-1,0,1};// 上 左 下 右
const int N=100005,M=1000005,K=200005,mod=1000000007;
const long long llinf=9223372036854775807ll;
//int & long long
double dp[105][105][105],ans;
int a,b,c;
int main()
{
cin>>a>>b>>c;
dp[a][b][c]=1;
for(int i=a;i<100;i++)
for(int j=b;j<100;j++)
for(int k=c;k<100;k++)
dp[i+1][j][k]+=dp[i][j][k]*i/(i+j+k),dp[i][j+1][k]+=dp[i][j][k]*j/(i+j+k),dp[i][j][k+1]+=dp[i][j][k]*k/(i+j+k);
for(int i=0;i<100;i++)
for(int j=0;j<100;j++)
ans+=dp[100][i][j]*(i+j+100-a-b-c),ans+=dp[i][100][j]*(i+j+100-a-b-c),ans+=dp[i][j][100]*(i+j+100-a-b-c);
cout<<fixed<<setprecision(9)<<ans<<endl;
return 0;
} |
#include<stdio.h>
#include<stdlib.h>
#include<bits/stdc++.h>
//Do DEBUG OR NOT
//#define DEBUG
#ifdef DEBUG
#define debug(var) do{std::cout << #var << " : ";view(var);}while(0)
template<typename T> void view(T e){std::cout << e << std::endl;}
template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cout << e << " "; } std::cout << std::endl;}
template<typename T> void view(const std::vector<std::vector<T> >& vv){ for(const auto& v : vv){ view(v); } }
#else
#define debug(...) true
#endif
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
typedef long long ll;
//prototype declaration
ll pow_mod(ll n, ll k, ll m);//繰り返し二乗法 return val:(n^k)%m
ll gcd(ll a, ll b);//最大公約数 2引数
ll ngcd(vector<ll> a);
ll lcm(ll a, ll b);//最小公倍数 2引数
ll nlcm(vector<ll> numbers);
bool isPrime(ll x);//素数判定
ll digsum(ll n);//桁和
vector<ll> enum_div(ll n);//約数全列挙
ll stringcount(string s, char c);//特定文字カウント
//__________________________main__________________________
int main(){
string N;
cin>>N;
if(N.size()%2==1){
ll temp=1;
rep(i,N.size()-1){
temp*=10;
}
temp--;
N=to_string(temp);
}
debug(N);
string fowardN;
string backN;
fowardN=N.substr(0,N.size()/2);
backN=N.substr(N.size()/2,N.size());
debug(fowardN);
debug(backN);
ll plus=9;
ll start=1;
ll goal=9;
ll ans=0;
rep(i,N.size()/2){
debug(start);
debug(goal);
ll end=goal;
//if((N.size()/2>1)&&(i==N.size()/2-1)&&(stoi(fowardN)<10||stoi(backN)<10)) break;
if(i==N.size()/2-1){
for(int i=start;i<=stoll(fowardN);i++){
debug(i);
if(stoll(N)<stoll(to_string(i)+to_string(i))) continue;
ans++;
}
end=min(end,stoll(fowardN));
end=min(end,stoll(backN));
}else{
ans+=end-start+1;
}
debug(ans);
goal=goal*10+9;
start*=10;
}
cout<<ans<<endl;
return 0;
}
//__________________________functions_____________Ctrl+K,Ctrl+0
//繰り返し二乗法関数
ll pow_mod(ll n, ll k, ll m){//r:(n^k)%m
ll r=1;
for(;k>0;k=k>>1){
if(k&1) r=(r*n)%m;
n = (n*n) % m;
}
return r;
}
//最大公約数 2引数
ll gcd(ll a, ll b) {
return b ? gcd(b, a%b) : a;
}
//最大公約数 n引数
ll ngcd(vector<ll> a){
ll res;
res = a[0];
for(int i = 1; i < a.size() && res != 1; i++) {
res = gcd(a[i], res);
}
return res;
}
//最小公倍数 2引数
ll lcm(ll a, ll b) {
return a / gcd(a, b) * b;
}
//最小公倍数 n引数
ll nlcm(vector<ll> numbers) {
ll res;
res = numbers[0];
for (int i = 1; i < numbers.size(); i++) {
res = lcm(res, numbers[i]);
}
return res;
}
//素数判定
bool isPrime(ll x){
ll i;
if(x < 2)return 0;
else if(x == 2) return 1;
if(x%2 == 0) return 0;
for(i = 3; i*i <= x; i += 2) if(x%i == 0) return 0;
return 1;
}
//桁和
ll digsum(ll n) {
ll res = 0;
while(n > 0) {
res += n%10;
n /= 10;
}
return res;
}
//約数全列挙
vector<ll> enum_div(ll n){
vector<ll> ret;
for(int i = 1 ; i*i <= n ; ++i){
if(n%i == 0){
ret.push_back(i);
if(i != 1 && i*i != n){
ret.push_back(n/i);
}
}
}
return ret;
}
//特定文字カウント
ll stringcount(string s, char c) {
return count(s.cbegin(), s.cend(), c);
} | #include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long
#define ull unsigned long long
#define cint const int&
#define Pi acos(-1)
const int mod = 1e9+7;
const int inf_int = 0x7fffffff;
const ll inf_ll = 0x7fffffffffffffff;
const double ept = 1e-9;
int n;
int h[100100], nx[200200], to[200200], ct;
int siz[100100];
int q[100100], cnt;
void add(cint f, cint t) {
nx[++ct] = h[f];
h[f] = ct;
to[ct] = t;
}
int dfs(cint loc, cint fa) {
int r= -111;
int ans = -1, oth = 0;
vector<int > d;
for(int i=h[loc]; i; i = nx[i])
if(to[i] != fa) {
r = dfs(to[i], loc);
siz[loc] += siz[to[i]];
if(!(siz[to[i]] & 1) && r > 0) ans += r;
else if(!(siz[to[i]] & 1) && r < 0) oth += r;
else if((siz[to[i]] & 1)) d.push_back(r);
}
int nt = d.size();
for(int i=0; i<nt; i++) q[i+1] = d[i];
sort(q+1, q+1+nt);
for(int i=nt; i>=1; i--)
if(!((nt-i) & 1)) ans += q[i];
else ans -= q[i];
if(nt & 1) ans -= oth;
else ans += oth;
return ans;
}
int main() {
cin >> n;
int r;
for(int i=2; i<=n; i++) {
cin >> r;
add(r, i);
add(i, r);
siz[i] = 1;
}
cout << (n-dfs(1, 1))/2 << endl;
return 0;
}
/*
3
1 2
*/ |
#include<bits/stdc++.h>
using namespace std;
#define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define int long long int
#define fi first
#define se second
#define pub push_back
#define pi pair<int,int>
#define all(v) (v).begin(), (v).end()
#define rep(i, l, r) for(int i=(int)(l);i<(int)(r);i++)
#define repd(i, l, r) for (int i=(int)(l);i>=(int)(r);i--)
#define clrg(i, l, r) for(int i=(int)(l);i<(int)(r);i++)vis[i]=0,v[i].clear();
int power(int x, unsigned int y){int res = 1;while (y > 0){ if (y & 1){res = res*x;} y = y>>1;x = x*x;}return res;}
int powermod(int x, unsigned int y, int p){int res = 1;x = x % p;while (y > 0){if (y & 1){res = (res*x) % p;}y = y>>1; x = (x*x) % p;}return res;}
#define print2d(mat,n,m){for(int i=0;i<(int)(n);i++){for(int j=0;j<(m);j++){cout<<mat[i][j]<<" ";}cout<< endl;}}
#define clr(a,x) memset(a,x,sizeof(a))
#define rr(v) for(auto &val:v)
#define print(v) for (const auto itr : v){cout<<itr<<' ';}cout<<"\n";
#define ln length()
#define sz size()
#define mod 1000000007
#define elif else if
int32_t main(){
fastIO
string st; cin>>st;
int ans=0;
rep(i,0,10){
rep(j,0,10){
rep(k,0,10){
rep(p,0,10){
string s="";
s+=char('0'+i);
s+=char('0'+j);
s+=char('0'+k);
s+=char('0'+ p);
bool okk=1;
rep(c,0,10){
if(st[c]=='o'){
bool ok=0;
rep(c1,0,4){
if((s[c1])-'0'==c)ok=1;
}
if(!ok)okk=0;
}
elif(st[c]=='x'){
bool ok=1;
rep(c1,0,4){
if((s[c1])-'0'==c)ok=0;
}
if(!ok)okk=0;
}
}
if(okk)ans++;
}
}
}
}
cout<<ans<<"\n";
return 0;
}
| #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
string s;
long long n=10,a[11],b[11],c[5],ans,p;
int main(){
long long i,j,u,v;
cin>>s;
for(i=0;i<n;i++){
if(s[i]=='o') a[i]=1;
if(s[i]=='x') a[i]=-1;
}
for(i=0;i<=9999;i++){
u=i;
memset(b,0,sizeof(b));
c[1]=u%10;u/=10;b[c[1]]=1;
c[2]=u%10;u/=10;b[c[2]]=1;
c[3]=u%10;u/=10;b[c[3]]=1;
c[4]=u%10;u/=10;b[c[4]]=1;
p=0;
for(j=0;j<n;j++){
if(a[j]==1 && b[j]==0){
p=1;
break;
}
if(b[j]==1 && a[j]==-1){
p=1;
break;
}
}
if(!p) ans++;
}
cout<<ans<<endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define reps(i, a, n) for (int i = (a); i < (n); ++i)
#define rep(i, n) reps(i, 0, n)
#define deps(i, a, n) for (int i = (a); i >= (n); --i)
#define dep(i, n) deps(i, n, 0)
#define inf LLONG_MAX
#define int long long
#define mod 998244353
int h, w;
string s[500];
int ans = 1;
signed main(void)
{
cin >> h >> w;
rep (i, h) cin >> s[i];
rep (k, inf)
{
int i, j;
j = min(w-1, k);
i = k-j;
if (i >= h) break;
int r = 0, b = 0, d = 0;
while (1)
{
if (s[i][j] == 'R') r++;
if (s[i][j] == 'B') b++;
if (s[i][j] == '.') d++;
if (r > 0 && b > 0) {ans *= 0; break;}
j--;
i++;
if (j < 0) break;
if (i >= h) break;
}
if (ans == 0) break;
if (r == 0 && b == 0) {ans *= 2; ans %= mod;}
}
cout << ans << endl;
}
| /*{{{*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define PB emplace_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PII> VPII;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(int64_t &x) { scanf("%lld", &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...); }
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("%lld", 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,class U> void _W(const pair<T,U> &x) {_W(x.F); putchar(' '); _W(x.S);}
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 HOME
#define DEBUG(...) {printf("[DEBUG] ");W(__VA_ARGS__);}
#else
#define DEBUG(...)
#endif
/*}}}*/
int MOD = 998244353;
void ADD(LL& x,LL v){x=(x+v)%MOD;if(x<0)x+=MOD;}
const int SIZE = 1<<20;
string s[SIZE];
int n,m;
int cnt[SIZE][3];
void solve() {
REP(i,n){
REP(j,m){
if(s[i][j]=='B')cnt[i+j][0]++;
else if(s[i][j]=='R')cnt[i+j][1]++;
else cnt[i+j][2]++;
}
}
LL an=1;
REP(i,n+m-1){
if(cnt[i][0]&&cnt[i][1]){
W(0);
return;
}
if(!cnt[i][0]&&!cnt[i][1]){
an=an*2%MOD;
}
}
W(an);
}
void input() {
R(n,m);
REP(i,n)R(s[i]);
}
int main(){
input();
solve();
return 0;
}
|
#include<bits/stdc++.h>
#define ll long long int
#define nl "\n"
#define vec vector<ll>
#define pb push_back
#define MOD 1000000007
#define test ll testc;cin>>testc;while(testc--)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
using namespace std;
ll bpow(ll a,ll b) {ll ans=1;while (b>0){if (b&1) ans=(ans*a)%MOD;b/=2;a=(a*a)%MOD;}return ans;}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
fast
{
ll n; cin>>n;
ll a[n];
ll t=0;
for(int i=0;i<n;i++)
{
cin>>a[i];
t+=a[i];
}
ll x[100000+1]={0};
x[0]=1;
for (int i=0;i<n;i++)
{
for (int j=100000;j>=0;j--)
{
if (x[j]>0) x[j+a[i]]=1;
}
}
for (int i=0;i<=100000;i++)
{
if (i>=(t+1)/2 and x[i]>0)
{
cout<<i<<nl;
break;
}
}
}
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define INCANT cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(12)
const int INF = 1e9 + 7;
const ll LINF = 1e18 + 7;
const int MOD = 1e9 + 7;
const double EPS = 1e-9;
void debug_out();
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T);
#ifdef MY_DEBUG
#define dbg(...) debug_out(__VA_ARGS__)
#define dbgs(x) cout << "\033[33m" << x << "\033[m" << " "
#define dbge cout << endl
#else
#define dbg(...)
#define dbgs(x)
#define dbge
#endif
template <class T> bool chmax(T &, const T &);
template <class T> bool chmin(T &, const T &);
template <class T> T pwr(T, ll);
template <class T> T squ(T);
ll fact(ll);
ll comb(ll, ll);
int main() {
INCANT;
int n;
cin >> n;
vector<int> t(n);
for (int i = 0; i < n; i++) cin >> t[i];
vector<vector<bool>> dp(n, vector<bool>(110000, false));
dp[0][0] = true, dp[0][t[0]] = true;
for (int i = 1; i < n; i++) {
for (int j = 0; j < 110000; j++) {
if (dp[i - 1][j] == true) {
dp[i][j + t[i]] = true;
dp[i][j] = true;
}
}
}
int ans = INF;
int sum = accumulate(t.begin(), t.end(), 0);
for (int i = 0; i < 110000; i++) {
if (dp[n-1][i] == true) chmin(ans, max(i, sum-i));
}
cout << ans << endl;
}
/*
-----------------------------------MY_FUNCTION------------------------------------ */
void debug_out() {
cout << endl;
}
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cout << "\033[33m" << H << "\033[m"
<< " ";
debug_out(T...);
}
template <class T> bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <class T> bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template <class T> T pwr(T x, ll n) {
T ans = 1;
for (int i = 0; i < n; i++) {
ans *= x;
}
return ans;
}
template <class T> T squ(T x) {
return x * x;
}
ll fact(ll n) {
ll res = 1;
for (ll i = 1; i <= n; i++) res *= i;
return res;
}
ll comb(ll n, ll r) { // comb(60, 30)までオーバーフローなし
ll res = 1;
for (int i = 1; i <= r; i++) {
res *= n--;
res /= i;
}
return res;
}
|
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long int
#define ld long double
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define mem(x,y) memset(x,y,sizeof(x))
#define pii pair<int,int>
#define pll pair<ll,ll>
#define INF 1e9
#define INFL 1e18
#define mod 1000000007
//#define mod 998244353
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> os;
typedef tree<pii,null_type,less<pii>,rb_tree_tag,tree_order_statistics_node_update> os_pair;
ll power(ll x,ll n){ll res =1;while(n>0){if(n%2==1){res=res*x;}x=x*x;n=n/2;}return res;}
ll powm(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
//cout<< fixed << setprecision(10)
//__builtin_popcountll()
int main(){
fast;
int n,i,j;
cin>>n;
int a[n+1];
for(i=1;i<=n;i++)
cin>>a[i];
set<int>s;
for(i=1;i<n;i++)
s.insert(i);
vector<int>ans;
for(i=1;i<=n;i++)
{
if(a[i]==i)
{
cout<<-1;
return 0;
}
if(a[i]>i)
continue;
else
{
for(j=i-1;j>=a[i];j--)
{
if(s.find(j)==s.end())
{
cout<<-1;
return 0;
}
else
{
s.erase(j);
ans.pb(j);
}
}
}
}
if(s.size()==0)
{
for(i=0;i<ans.size();i++)
cout<<ans[i]<<" ";
}
else
{
cout<<-1;
}
} | #include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, init, end) for(ll i = init; i < end; i++)
#define REP(i, init, end) for(ll i = init; i < end + 1; i++)
#define rev(i, end, init) for(ll i = init - 1; i >= end; i--)
#define REV(i, end, init) for(ll i = init; i >= end; i--)
#define PI 3.14159265359
// #define EPS 0.0000000001
// #define MOD 1000000007
#define INF 100000000000000000
// #define MAX 100000000000
//cout << std::fixed << std::setprecision(15) << y << endl;
int main(){
ll N;
cin >> N;
string S, X;
cin >> S >> X;
ll dp[N][7]; // 0: Takahashi wins, 1: Aoki wins
rep(i, 0, N){
rep(j, 0, 7){
dp[i][j] = -1;
}
}
rep(j, 0, 7){
if(X[N - 1] == 'A'){
if(j == 0 && (S[N - 1] == '0' || S[N - 1] == '7')){
dp[N - 1][j] = 0;
}else{
dp[N - 1][j] = 1;
}
}else{
if(j == 0){
dp[N - 1][j] = 0;
}else{
if(((j * 10) + (S[N - 1] - '0')) % 7 == 0){
dp[N - 1][j] = 0;
}else{
dp[N - 1][j] = 1;
}
}
}
}
rev(i, 0, N - 1){
rep(j, 0, 7){
if(X[i] == 'A'){
dp[i][j] = max(dp[i + 1][(j * 10) % 7], dp[i + 1][((j * 10) + (S[i] - '0')) % 7]);
}else{
dp[i][j] = min(dp[i + 1][(j * 10) % 7], dp[i + 1][((j * 10) + (S[i] - '0')) % 7]);
}
}
}
if(dp[0][0] == 0){
cout << "Takahashi" << endl;
}else{
cout << "Aoki" << endl;
}
return 0;
}
|
/*
* author: cyclexit
* start from: 2021-01-05 10:21
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline double slope(pair<double, double> x, pair<double, double> y) {
return fabs(y.second - x.second) / fabs(y.first - x.first);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<pair<double, double>> point(n);
for (int i = 0; i < n; ++i) {
cin >> point[i].first >> point[i].second;
}
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (point[i].first != point[j].first) {
if (-1.0 <= slope(point[i], point[j]) && slope(point[i], point[j]) <= 1.0) {
++ans;
}
}
}
}
cout << ans << '\n';
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 pair<int,int> pii;
typedef pair<int,pair<int,int>> piii;
template <typename T>
using ordered_set = __gnu_pbds::tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
ll gcd(ll a, ll b){
if (b==0) return a;
return gcd(b, a%b);
}
const int N = 2000;
ll a[N];
unordered_map<int, int> gcds;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n; cin >> n;
ll lo = 1e18;
for (int i = 0; i < n; i++){
cin >> a[i];
lo = min(a[i], lo);
}
for (int i = 0; i < n; i++){
for (int d = 1; d*d <= a[i]; d++){
if (a[i]%d != 0) continue;
gcds[d] = gcd(gcds[d], a[i]);
gcds[a[i]/d] = gcd(gcds[a[i]/d], a[i]);
}
}
int ans = 1;
for (auto g : gcds){
if (g.first == g.second && g.first < lo) ans++;
}
cout << ans << endl;
return 0;
}
|
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = (1LL << 61);
#define int long long
signed main() {
int H, W; cin >> H >> W;
vector<vector<int>>A(H, vector<int>(W));
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++)cin >> A[i][j];
}
int ans = INF;
for (int k = 0; k <= 100; k++) {
int now = 0;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (A[i][j] < k) {
now = INF;
break;
}
now += A[i][j] - k;
}
}
ans = min(ans, now);
}
cout << ans << endl;
}
| //00:04
#include <bits/stdc++.h>
using namespace std;
long long inf = 1000000007;
typedef long long ll;
int main(){
int h, w, small = 101, cnt = 0;
cin >> h >> w;
vector<vector<int>> a(h, vector<int>(w, 0));
for(vector<int>& a1:a){
for(int& v: a1) cin >> v;
}
for(int i=0; i<h; ++i){
for(int& v: a[i]){
if(v < small) small = v;
}
}
for(int i=0; i<h; ++i){
for(int& v: a[i]){
cnt += v - small;
}
}
cout << cnt << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
//Template Begin
#define ll long long
#define int long long
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define be begin()
#define en end()
#define le length()
#define endl '\n'
using VI=vector<int>;
using VLL=vector<ll>;
using PII=pair<int, int>;
using PLL=pair<ll, ll>;
using VPII = vector< PII >;
#define TC(t) while (t--)
#define REP(i,from,to) for(auto i=(from); i<=(to); ++i)
#define PER(i,from,to) for(auto i=(from); i>=(to); --i)
#define REPITER(it, a) for(__typeof(a.begin()) it = a.begin();it != a.end(); ++it)
#define FOR(i,less_than) for (auto i=0; i<(less_than); ++i)
#define FORI(i, container) for (auto i=0; i<(container).size(); ++i)
#define ROFI(i, container) for (auto i=SZ(container)-1; i>=0; --i)
#define FOREACH(elem, container) for (auto &elem : (container))
#define MEMSET(container, value) memset(container, value, sizeof(container))
#define MEMSET0(container) memset(container, 0, sizeof(container))
#define FILL(container, value) fill(container.begin(), container.end(), value)
#define FILL0(container) fill(container.begin(), container.end(), 0)
#define ALL(container) (container).begin(), (container).end()
#define SZ(container) (int)((container).size())
#define POP(var, container) auto var=(container.front()); container.pop();
const int MOD = 1e9 + 7;
const int INF = 1e9;
template<typename T> T abs(T a) { return a < 0 ? -a : a; }
//Template End
int a[32]={0};
void calc() {
a[0]=1;
REP(i, 1, 32) a[i] = (a[i-1]*i)/__gcd((long long)i, a[i-1]);
}
void solve() {
int n;
cin>>n;
cout<<a[n]+1<<endl;
}
signed main() {
#ifndef ONLINE_JUDGE
// for getting input from input.txt
freopen("input.txt", "r", stdin);
// for writing output to output.txt
freopen("output1.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
// your code goes here
int t=1;
// cin>>t;
calc();
TC(t) solve();
return 0;
} | #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
ll mod = 1000000007;
const double PI = 3.141592653589793238460;
const int INF = 1e9+5;
const ll lINF = 1e16+1;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<ll> isprime = {2,3,5,7,11,13,17,19,23,29};
ll ans = 1;
ans*=16;
ans*=27;
ans*=25;
ans*=7;
ans*=11;
ans*=29;
ans*=23;
ans*=19;
ans*=13;
ans*=17;
ans++;
// for(int i=2; i<=n; i++){
// cout<<(ans)%i<<' ';
// }
// cout<<endl;
cout<<ans;
return 0;
} |
#include <cstdio>
#include <set>
using namespace std;
const int max_l = 1000;
char mp[max_l][max_l+1];
int dsu[max_l<<1];
inline int my_min(int a, int b) { return (a < b)? a:b; }
int find(int x)
{
if (dsu[x] != x) dsu[x] = find(dsu[x]);
return dsu[x];
}
bool sam(int a, int b) { return find(a) == find(b); }
void unite(int a, int b)
{
if (!sam(a, b))
dsu[find(a)] = find(b);
}
int main()
{
int h, w;
set<int> rw, cl;
scanf("%d%d", &h, &w);
for (int i = 0; i < h; i++)
scanf("%s", mp[i]);
for (int i = 0; i < h + w; i++)
dsu[i] = i;
mp[0][0] = mp[0][w-1] = mp[h-1][0] = mp[h-1][w-1] = '#';
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
if (mp[i][j] == '#')
unite(i, j + h);
for (int i = 0; i < h; i++)
rw.insert(find(i));
for (int i = 0; i < w; i++)
cl.insert(find(i+h));
printf("%d\n", my_min(rw.size(), cl.size()) - 1);
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define int long long
vector<set<int>> v;
vector<char> colour;
bool isValid(int i, char c){
if(colour[i]!='.' && colour[i]!=c){
return false;
}
for(auto z:v[i]){
if(colour[z]==c){
return false;
}
}
return true;
}
int ways=0,n;
void f(int i,char c){
if(i>=n || i<0 || !isValid(i,c)){
return;
}
char x=colour[i];
colour[i]=c;
if(i==n-1){
ways++;
colour[i]=x;
return;
}
f(i+1,'R');
f(i+1,'G');
f(i+1,'B');
colour[i]=x;
}
vector<int> vis;
void dfs(int i,vector<int>&ps){
if(vis[i]==0){
vis[i]=1;
ps.push_back(i);
for(auto z:v[i])dfs(z,ps);
}
}
void solve(){
cin>>n; int m; cin>>m;
v.resize(n);
vis.resize(n,0);
colour.resize(n,'.');
while(m--){
int a,b; cin>>a>>b; a--; b--;
v[a].insert(b);
v[b].insert(a);
}
int ans=1;
for(int i=0;i<n;i++){
vector<int> vvv;
dfs(i,vvv);
map<int,int> mv;
int nn=vvv.size();
set<int> sv;
if(nn>0){
for(int j=0;j<nn;j++){
// cout<<vvv[j]<<" ";
mv[vvv[j]]=j;
sv.insert(vvv[j]);
}
vector<set<int>> temp(nn),temp1;
temp1=v;
for(int j=0;j<n;j++){
if(sv.find(j)!=sv.end()){
for(auto z:v[j]){
if(sv.find(z)!=sv.end()){
temp[mv[j]].insert(mv[z]);
}
}
}
}
int xxxx=n;
v=temp;
n=nn;
colour.clear();
colour.resize(n,'.');
f(0,'R');
ways*=3;
ans*=ways;
// cout<<ans<<"\n";
ways=0;
n=xxxx;
v=temp1;
}
}
cout<<ans;
}
signed main(){
// time_t start, end;
// time(&start);
cin.tie(NULL);
ios_base::sync_with_stdio(false);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int tt=1,t=1; //cin>>tt;
while(t<=tt){
//cout<<"Case #"<<t<<": ";
solve();
cout<<"\n";
t++;
}
// time(&end);
// double time_taken = double(end - start);
// cout << "Time taken by program is : " << fixed << time_taken << setprecision(5);
// cout << " sec " << endl;
return 0;
} |
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<ctime>
#include<bitset>
#include<vector>
#include<cstdio>
#include<complex>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define SZ(v) ((int)(v).size())
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
template<class T>
inline void relax(T& a,const T& b) { a=((a>b) ? a : b); }
template<class T>
inline void tense(T& a,const T& b) { a=((a<b) ? a : b); }
template<class T>
inline void read(T& x)
{
x=0;
register bool sign=false;
register char c;
for(c=getchar();c<'0' || c>'9';c=getchar())
if(c=='-') sign=true;
for(;c>='0' && c<='9';c=getchar())
x=(x<<1)+(x<<3)+(c&15);
if(sign) x=~x+1;
}
const int N=4e5+5;
const LL P=998244353;
int n;
LL a[N];
LL power(LL x,LL k,LL MOD)
{
LL res=1; x%=MOD;
while(k) {
if(k&1) res=res*x%MOD;
x=x*x%MOD; k>>=1;
}
return res%MOD;
}
int main()
{
// freopen("1.in","r",stdin);
int i;
read(n);
for(i=1;i<=n;i++) read(a[i]);
sort(a+1,a+n+1);
LL val=0;
for(i=1;i<=n;i++)
val=(val+a[i]*power(2,i-1,P))%P;
LL ans=0;
for(i=1;i<=n;i++) {
val=(val-a[i]+P)*power(2,P-2,P)%P;
ans=(ans+val*a[i])%P;
ans=(ans+a[i]*a[i])%P;
}
printf("%lld\n",ans);
return 0;
}
| #include <bits/stdc++.h>
#define fo(i, k, n) for (ll i = k; i < n; i++)
#define rfo(i, k, n) for (ll i = k; i >= n ; i--)
#define ll long long
#define ld long double
#define que queue
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define vl vector<ll>
#define gcd(m,n) __gcd( m, n)
#define sq(x) (x*x)
#define rev(v) reverse(v.begin(),v.end())
#define srt(v) sort(v.begin(),v.end())
#define grtsrt(v) sort(v.begin(),v.end(),greater<int>())
#define mnv(v) *min_element(v.begin(),v.end())
#define mxv(v) *max_element(v.begin(),v.end())
#define all(v) v.begin(),v.end()
#define toInteger(s) stoll(s)
#define toString(num) to_string(num)
using namespace std;
ll MOD = 1e9 + 7;
//--------------------------------------------functions-------------------------------------------------//
ll power(ll a,ll b){ll result=1;while(b>0){if(b%2 == 1){result *= a;} a *= a;b /= 2;}return result;}
ll countSetBits(ll x){ll Count=0;while(x>0){if(x&1) Count++;x=x>>1;}return Count;}
bool isPerfectSquare(ll n){ll sr = sqrt(n);if (sr * sr == n)return true;else return false;}
ll mod(ll x,ll M){return ((x%M + M)%M);}
ll mul(ll a, ll b,ll M){return mod(mod(a,M)*mod(b,M),M);}
ll powerM(ll a,ll b,ll M){
ll res=1ll;
while(b){
if(b%2ll==1ll){
res=mul(a,res,M);
}
a=mul(a,a,M);b/=2ll;
}
return res;
}
int log22(long long x)
{
return 64 - __builtin_clzll(x) - 1;
}
//--------------------------------------------SieveOfEratosthenes-------------------------------------------------//
//mem will have true if its prime.
void SieveOfEratosthenes( vector<bool>&mem)
{ ll n = 1e6 +1;
bool prime[n + 1];
memset(prime, true, sizeof(prime));
for (int p = 2; p * p <= n; p++)
{
if (prime[p] == true)
{
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
fo(i,2,n)
if(prime[i])mem[i]=true;
}
//--------------------------------------------solve-------------------------------------------------//
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
int n;
cin>>n;
vector<int>v(n);
fo(i,0,n)
cin>>v[i];
ll ans = 0;
fo(i,0,n)
{
if(v[i]>10)
ans += v[i]-10;
}
cout<<ans;
return 0;
}
|
/* -*- coding: utf-8 -*-
*
* d.cc: D - Cooking
*/
#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 = 100;
const int MAX_T = 1000;
const int MAX_S = MAX_N * MAX_T;
/* typedef */
/* global variables */
int ts[MAX_N];
bool dp[MAX_S + 1];
/* subroutines */
/* main */
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", ts + i);
dp[0] = true;
int s = 0;
for (int i = 0; i < n; i++) {
for (int j = s; j >= 0; j--)
if (dp[j]) dp[j + ts[i]] = true;
s += ts[i];
}
int mint = s;
for (int i = 0; i <= s; i++)
if (dp[i]) mint = min(mint, max(i, s - i));
printf("%d\n", mint);
return 0;
}
| //Code by Anshuman Das Gupta
#include<bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define int long long
#define MOD 1000000007
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define loop(I,N) for(int i=I;i<N;i++)
#define sortvec(V) sort(V.begin(),V.end())
#define vec vector<int>
#define endl "\n"
using namespace std;
#define PI 3.14159265359
int fast_power(int base, int power) {
int result = 1;
while (power > 0) {
if (power & 1) {
result = (result * base) % MOD;
}
base = (base * base) % MOD;
power = power >> 1;
}
return result % MOD;
}
bool isPrime(int x) {
for (int i = 2; i <= sqrt(x); i++) {
if (x % i == 0)
return false;
}
if (x == 1)
return false;
return true;
}
int binsearch(int a[], int f, int r, int x) {
while (f <= r) {
int mid = (f + r) / 2;
if (a[mid] == x)
return mid;
else if (a[mid] > x) {
r = mid - 1;
}
else {
f = mid + 1;
}
}
return -1;
}
void sieve(int a[], int n) {
a[0] = 0;
a[1] = 0;
for (int i = 2; i <= n; i++) {
if (a[i] == 1) {
for (int j = i + i; j <= n; j += i) {
a[j] = 0;
}
}
}
}
int32_t main() {
int t = 1;
//cin >> t;
while (t--) {
int n;
cin >> n;
cout << n - 1 << endl;
}
}
|
/* Hardwork allways pays off */
//You never know how close to the solution you are ,so keep practicing
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define pii pair<int,int>
#define endl '\n'
#define REP(i,n) for(int i=1;i<=n;i++)
#define tol(s) transform(s.begin(),s.end(),s.begin(),::tolower)
#define tou(s) transform(s.begin(),s.end(),s.begin(),::toupper)
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define mod1 998244353
#define mod 1000000007
#define pie 3.141592653589793238
#define printv(x) for(i=0;i<x.size();i++) cout<<x[i]<<" ";
#define printA(x,n) for(i=0;i<n;i++) cout<<x[i]<<" ";
void make_unique(vector<int> vec)
{
sort(all(vec));
vec.resize(unique(all(vec)) - vec.begin());
}
bool isPowerOfTwo(int n)
{
if(n==0)
return false;
return (ceil(log2(n)) == floor(log2(n)));
}
int cceil(int x, int y){
return (x+y-1)/y;
}
bool cmp(const pair<int,int> &a,
const pair<int,int> &b)
{
return (a.second+a.first)>(b.second+b.first);
}
int x,y;
int ans=1e18;
// int dp[]
map<int,int> dp,vis;
// int count=0;
int repeat(int y) //return min steps to reach x from y by -1,+1,/2
{
// cout<<y<<endl;
// ans=abs(y-x);
if(vis[y])
return dp[y];
vis[y]=1;
dp[y]=1e18;
int &ans=dp[y];
ans=min(ans,abs(y-x));
if(y==1)
return ans;
else if(y<x)
return x-y;
else
{
if(y%2==0)
ans= min(ans,(1+repeat(y/2)));
else
{
ans= min(ans,1+min(repeat(y-1),repeat(y+1)));
}
}
return ans;
}
signed main()
{ ios;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
// cout.precision(1);
// cout<<fixed;
int t=1;
// cin>>t;
while(t--)
{int n,k,i,m,j,flag=1,moves=0,count=0;
// cin>>n;
// int x,y;
cin>>x>>y;
if(x>=y)
cout<<x-y<<endl;
else
{
int ans=repeat(y);
cout<<ans<<endl;
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int> pi;
#define f first
#define s second
#define FAST ios_base::sync_with_stdio(0); cin.tie(0);
#define all(x) x.begin(),x.end()
typedef pair<int, pi> pii;
const int maxn = 500010;
const int INF = LLONG_MAX/2;
int x, Y;
map <int, int> dp;
int dpf(int y) {
if (y <= x) return x - y;
if (dp.find(y) != dp.end()) return dp[y];
if (y % 2 == 0) {
if (y/2 >= x) dp[y] = dpf(y/2) + 1;
else dp[y] = min(x - y/2 + 1, y - x);
} else {
dp[y] = 1 + min(dpf(y+1), dpf(y-1));
}
return dp[y];
}
int32_t main() {
FAST
cin >> x >> Y;
cout << dpf(Y);
}
|
#include<iostream>
using namespace std;
typedef long long ll;
int main() {
ll n;
cin>>n;
ll ans=0;
for (ll i=1; ; i++) {
string s=to_string(i)+to_string(i);
ll t=stoll(s);
if (t>n) {
cout<<i-1<<"\n";
return 0;
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX_N = 300000 + 5;
const int INF32 = 0x3f3f3f3f;
int n;
double ans;
int main() {
scanf("%d", &n);
for (int i = 1; i < n; i ++) ans += (double)n / i;
printf("%.10lf\n", ans);
return 0;
} |
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define NDEBUG
#define eprintf(...) do {} while (0)
#endif
#include<cassert>
using namespace std;
typedef long long LL;
typedef vector<int> VI;
#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)
template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }
#define rprintf(fmt, begin, end) do { const auto end_rp = (end); auto it_rp = (begin); for (bool sp_rp=0; it_rp!=end_rp; ++it_rp) { if (sp_rp) putchar(' '); else sp_rp = true; printf(fmt, *it_rp); } putchar('\n'); } while(0)
template<unsigned MOD_> struct ModInt {
static constexpr unsigned MOD = MOD_;
unsigned x;
void undef() { x = (unsigned)-1; }
bool isnan() const { return x == (unsigned)-1; }
inline int geti() const { return (int)x; }
ModInt() { x = 0; }
ModInt(int y) { if (y<0 || (int)MOD<=y) y %= (int)MOD; if (y<0) y += MOD; x=y; }
ModInt(unsigned y) { if (MOD<=y) x = y % MOD; else x = y; }
ModInt(long long y) { if (y<0 || MOD<=y) y %= MOD; if (y<0) y += MOD; x=y; }
ModInt(unsigned long long y) { if (MOD<=y) x = y % MOD; else x = y; }
ModInt &operator+=(const ModInt y) { if ((x += y.x) >= MOD) x -= MOD; return *this; }
ModInt &operator-=(const ModInt y) { if ((x -= y.x) & (1u<<31)) x += MOD; return *this; }
ModInt &operator*=(const ModInt y) { x = (unsigned long long)x * y.x % MOD; return *this; }
ModInt &operator/=(const ModInt y) { x = (unsigned long long)x * y.inv().x % MOD; return *this; }
ModInt operator-() const { return (x ? MOD-x: 0); }
ModInt inv() const { return pow(MOD-2); }
ModInt pow(long long y) const {
ModInt b = *this, r = 1;
if (y < 0) { b = b.inv(); y = -y; }
for (; y; y>>=1) {
if (y&1) r *= b;
b *= b;
}
return r;
}
friend ModInt operator+(ModInt x, const ModInt y) { return x += y; }
friend ModInt operator-(ModInt x, const ModInt y) { return x -= y; }
friend ModInt operator*(ModInt x, const ModInt y) { return x *= y; }
friend ModInt operator/(ModInt x, const ModInt y) { return x *= y.inv(); }
friend bool operator<(const ModInt x, const ModInt y) { return x.x < y.x; }
friend bool operator==(const ModInt x, const ModInt y) { return x.x == y.x; }
friend bool operator!=(const ModInt x, const ModInt y) { return x.x != y.x; }
};
constexpr LL MOD = 1000000007;
using Mint = ModInt<MOD>;
int N;
LL M;
LL A[2011];
void MAIN() {
scanf("%d%lld", &N, &M);
REP (i, N) scanf("%lld", A+i);
LL sum = 0;
REP (i, N) sum += A[i];
if (sum > M) {
puts("0");
return;
}
Mint nu = 1;
Mint de = 1;
REP (a, sum+N) {
de *= a+1;
nu *= M+N-a;
}
Mint ans = nu / de;
printf("%d\n", ans.geti());
}
int main() {
int TC = 1;
// scanf("%d", &TC);
REP (tc, TC) MAIN();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
template<int MAX>
struct Combination {
vector<long long> _inv, _fac, _ifac;
const long long MOD;
Combination(long long mod) : _inv(MAX+1), _fac(MAX+1), _ifac(MAX+1), MOD(mod) { init(); }
void init() { init_inv(), init_fac(); }
void init_inv() {
_inv[0] = 0;
for (int i = 1; i <= MAX; i++) {
if (i == 1) _inv[i] = 1;
else {
_inv[i] = (MOD - (MOD / i) * _inv[MOD % i]) % MOD;
if (_inv[i] < 0) _inv[i] += MOD;
}
}
}
void init_fac() {
_fac[0] = _ifac[0] = 1;
for (int i = 1; i <= MAX; i++) {
_fac[i] = _fac[i-1] * i % MOD;
_ifac[i] = _ifac[i-1] * _inv[i] % MOD;
}
}
long long modpow(long long n, long long r) {
n %= MOD, r %= (MOD-1);
if (!n) return 0;
long long ret = 1; long long tmp = n;
while (r != 0) {
if (r % 2) ret *= tmp;
tmp *= tmp; tmp %= MOD; ret %= MOD;
r /= 2;
}
return ret;
}
long long invb(int i) { return (i <= MAX) ? _inv[i] : this->modpow(i, MOD-2); }
long long inv(int i) const { return _inv[i]; }
long long fac(int i) const { return _fac[i]; }
long long ifac(int i) const { return _ifac[i]; }
long long operator()(int n, int r) const {
if (n < r) return 0;
return fac(n) * ifac(r) % MOD * ifac(n-r) % MOD;
}
};
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int N, M; cin >> N >> M;
constexpr long long mod = 1000000007;
Combination<5000000> C(mod);
long long Ans = 1;
vector<int> A(N);
int S = 0;
for (auto &x: A) {
cin >> x; S += x;
}
for (int i = 1; i <= N + S; i++) {
Ans = Ans * C.invb(i) % mod;
Ans = Ans * (M + N + 1 - i) % mod;
}
cout << Ans << "\n";
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define mnto(x, y) x = min(x, (__typeof__(x)) y)
#define mxto(x, y) y = max(x, (__typeof__(x)) y)
#define REP(i, s, e) for (int i = s; i < e; i++)
#define RREP(i, s, e) for (int i = s - 1; i >= e; i--)
typedef long long ll;
#define ALL(_a) _a.begin(), _a.end()
#define pb emplace_back
typedef vector<int> vi;
#define MP make_pair
#define FI first
#define SE second
typedef pair<int, int> ii;
#define MT make_tuple
typedef tuple<int, int, int> iii;
#define INF 1000000005
#define LINF 100000000000000005
#define MOD 1000000007
#define MAXN 200005
#define MAXA 400005
int n;
int in[MAXA];
vector<int> adjList[MAXA];
set<ii> st;
bool erased[MAXA];
int main() {
scanf("%d", &n);
REP (i, 0, n) {
int a, b; scanf("%d%d", &a, &b);
in[a]++;
adjList[a].pb(b);
if (a != b) {
in[b]++;
adjList[b].pb(a);
}
}
REP (i, 1, MAXA) {
if (in[i] == 0) continue;
st.emplace(in[i], i);
}
int ans = 0;
while (!st.empty()) {
ii cur = *st.begin(); st.erase(st.begin());
if (erased[cur.SE]) continue;
for (int v : adjList[cur.SE]) {
if (erased[v]) continue;
st.erase(MP(in[v], v));
in[v]--;
if (in[v] > 0) st.emplace(MP(in[v], v));
break;
}
erased[cur.SE] = true;
ans++;
}
printf("%d\n", ans);
return 0;
}
| #include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=2e5,maxm=4e5;
int n;
int f[maxm+5];
int szp[maxm+5],sze[maxm+5];
int ans;
int find(int x){
return x==f[x]?x:f[x]=find(f[x]);
}
int main(){
scanf("%d",&n);
for(int i=1;i<=maxm;i++) f[i]=i,szp[i]=1;
for(int i=1,a,b;i<=n;i++){
scanf("%d %d",&a,&b);
int fa=find(a),fb=find(b);
if(fa==fb) sze[fa]++;
else{
f[fb]=fa; szp[fa]+=szp[fb]; sze[fa]+=sze[fb]+1;
}
}
ans=maxm;
for(int i=1;i<=maxm;i++)
if(i==find(i))
if(sze[i]==szp[i]-1) ans--;
printf("%d\n",ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main(){
int H, W, N, M;
cin >> H >> W >> N >> M;
vector<int> A(N), B(N), C(M), D1(M);
for(int i = 0; i < N; i++) {
cin >> A[i] >> B[i];
A[i]--;
B[i]--;
}
for(int i = 0; i < M; i++) {
cin >> C[i] >> D1[i];
C[i]--;
D1[i]--;
}
vector<vector<char>> field(H, vector<char>(W, '.'));
for(int i = 0; i < N; i++){
field[A[i]][B[i]] = 'o';
}
for(int i = 0; i < M; i++){
field[C[i]][D1[i]] = '#';
}
vector<vector<int>> U(H, vector<int>(W)), L(H, vector<int>(W)), R(H, vector<int>(W)), D(H, vector<int>(W));
for(int i = 0; i < H; i++){
for(int j = 0; j < W; j++){
if(field[i][j] == 'o'){
L[i][j] = 1;
}
else if(field[i][j] == '.'){
if(j > 0) L[i][j] = L[i][j - 1];
}
}
for(int j = W - 1; j >= 0; j--){
if(field[i][j] == 'o'){
R[i][j] = 1;
}
else if(field[i][j] == '.'){
if(j + 1 < W) R[i][j] = R[i][j + 1];
}
}
}
for(int i = 0; i < W; i++){
for(int j = 0; j < H; j++){
if(field[j][i] == 'o'){
U[j][i] = 1;
}
else if(field[j][i] == '.'){
if(j > 0) U[j][i] = U[j - 1][i];
}
}
for(int j = H - 1; j >= 0; j--){
if(field[j][i] == 'o'){
D[j][i] = 1;
}
else if(field[j][i] == '.'){
if(j + 1 < H) D[j][i] = D[j + 1][i];
}
}
}
int cnt = 0;
for(int i = 0; i < H; i++){
for(int j = 0; j < W; j++){
if(U[i][j] > 0 || D[i][j] > 0 || L[i][j] > 0 || R[i][j] > 0) cnt++;
}
}
cout << cnt << endl;
} | #include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <string>
using namespace std;
#define ll long long int
#define f(i,l,n) for(int i=l;i<n;i++)
#define E "\n"
#define fast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define F first
#define S second
#define all(c) (c).begin(),(c).end()
#define sz(v) (int)(v).size()
#define vi(typ) vector<typ>
#define Ee cout<<"\n";
ll n,m;
vector<ll> graph[2005],visited(2005,0),dis(2005,0);
bool isvalid=0;
ll org,adj[2002][2002],val;
void dfs(ll node)
{
// cout<<node<<E;
for(auto x:graph[node])
{
if(x==org )
{
val=min(val,dis[node]+adj[node][x]);
}
// if(node==3) cout<<x<<" "<<val<<" "<<dis[node]<<" "<<adj[node][x]<<E;
if(dis[node]+adj[node][x]<dis[x])
{
visited[x]=1;
dis[x]=min(dis[x],adj[node][x]+dis[node]);
dfs(x);
}
}
}
ll solve()
{
cin >> n >> m;
f(i,0,m)
{
ll x,y,c;
cin >> x >> y >> c;
if(adj[x][y]==0)
{
adj[x][y]=c;
graph[x].push_back(y);
// graph[y].push_back(x);
}
else adj[x][y]=min(adj[x][y],c);
}
f(i,1,n+1)
{
f(j,1,n+1) visited[j]=0,dis[j]=1e15;
org=i,val=1e15;
visited[i]=1;
dis[i]=0;
dfs(i);
if(val==1e15) cout<<"-1\n";
else cout<<val<<E;
}
return 0;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
ll t = 1;
fast;
// cin >> t;
while(t--)
{
int l=solve();
// cout<<"hii\n";
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define BOOST ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rept(it,v) for(auto it=v.begin();it!=v.end();it++)
#define pre(i,a,b) for(int i=a;i>=b;i--)
#define mp make_pair
#define pb push_back
#define endl "\n"
#define mod 1000000007
#define ll long long
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vl vector<ll>
#define vi vector<int>
#define all(x) x.begin(),x.end()
#define sp(x) setprecision(x)
#define lb lower_bound
#define ub upper_bound
#define ll long long
#define pb push_back
#define pob pop_back
#define mt make_tuple
#define F first
#define S second
#define endl "\n"
#define ct continue
#define fill(a,b) memset(a,b,sizeof(a))
#define bs binary_search
#define gcd __gcd
#define sz(x) (int)((x).size())
#define test cin>>t; while(t--)
ll n,q,i,j,cnt,cnt2,a[1000000],d,b[1000000],x1,x2,sum,c[1000000],t,x,p,y,m,z,l,k,inv,ans,mi,ma,flag,check;
vector <ll> v1,v2;
string s;
map <ll,ll> mp1,mp2;
int main()
{
BOOST
//test
{
cin>>x>>y>>z;
if(x>y)
cout<<"Takahashi"<<'\n';
else if(x<y)
cout<<"Aoki"<<'\n';
else
{
if(z == 0)
cout<<"Aoki"<<'\n';
else
cout<<"Takahashi"<<'\n';
}
}
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
struct UnionFind {
vector<int> p;
UnionFind(int n) : p(n, -1) {}
bool unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y)
return false;
if (-p[x] < -p[y])
swap(x, y);
p[x] += p[y];
p[y] = x;
return true;
}
int root(int x) {
if (p[x] < 0)
return x;
return root(p[x]);
}
int size(int x) { return -p[root(x)]; }
};
int main() {
int n, m;
cin >> n >> m;
int a[n], b[n];
rep(i, n) cin >> a[i];
rep(i, n) cin >> b[i];
UnionFind uf(n);
rep(i, m) {
int c, d;
cin >> c >> d;
uf.unite(--c, --d);
}
vector<ll> s(n);
rep(i, n) s[uf.root(i)] += b[i] - a[i];
bool ans = true;
rep(i, n) if (s[i] != 0) ans = false;
cout << (ans ? "Yes" : "No") << endl;
return 0;
}
|
#pragma region Macros
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP2(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; ++i)
#define REP3(i, l, r) for (int i = (l), i##_len = (int)(r); i < i##_len; ++i)
#define GET_MACRO_REP(_1, _2, _3, NAME, ...) NAME
#define REP(...) GET_MACRO_REP(__VA_ARGS__, REP3, REP2) (__VA_ARGS__)
#define RREP2(i, n) for (int i = (n - 1); i >= 0; --i)
#define RREP3(i, l, r) for (int i = (r - 1), i##_len = (l); i >= i##_len; --i)
#define GET_MACRO_RREP(_1, _2, _3, NAME, ...) NAME
#define RREP(...) GET_MACRO_REP(__VA_ARGS__, RREP3, RREP2) (__VA_ARGS__)
#define IN(type, n) type n; cin >> n
#define INALL(type, v, s) vector<type> v(s); for (auto &e : v) { cin >> e; }
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#ifdef _DEBUG
#define DEBUG(x) cout << #x << ": " << x << endl
#else
#define DEBUG(x)
#endif
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; }
void yes() { cout << "Yes" << endl; }
void no() { cout << "No" << endl; }
#pragma endregion
ll calc_time(ll now, ll c, ll d) {
ll ans = now + c + d / (now + 1);
ll sqr = (ll)sqrtl(d);
for(ll i = max(0LL, sqr - 10); i <= min(d, sqr + 10); ++i) {
if (i < now) continue;
ll tmp = i + c + d / (i + 1);
chmin(ans, tmp);
}
return ans;
}
int main() {
IN(int, N);
IN(int, M);
vector<vector<tuple<int, ll, ll> > > edge(N, vector<tuple<int, ll, ll> >());
REP(i, M) {
IN(int, A);
IN(int, B);
A--; B--;
IN(ll, C);
IN(ll, D);
if (A == B) continue;
edge.at(A).emplace_back(make_tuple(B, C, D));
edge.at(B).emplace_back(make_tuple(A, C, D));
}
vector<ll> time(N, LLONG_MAX);
time.at(0) = 0LL;
priority_queue<pair<ll, int>, vector<pair<ll, int> >, greater<pair<ll, int> > > que;
que.push(make_pair(0LL, 0));
while (!que.empty()) {
auto [now, node] = que.top();
que.pop();
if (time.at(node) < now) continue;
for (auto const &[to, c, d] : edge.at(node)) {
ll arrive = calc_time(now, c, d);
if (chmin(time.at(to), arrive)) {
que.push(make_pair(arrive, to));
}
}
}
if (time.at(N - 1) == LLONG_MAX) cout << -1 << endl;
else cout << time.at(N - 1) << endl;
return 0;
}
| #include <bits/stdc++.h>
#define int long long
using namespace std;
inline void max_self(int &a, int b) {
a = max(a, b);
}
int N, T, ans;
vector < int > a, suf;
void Back(int index, int sum) {
if(sum <= T)
max_self(ans, sum);
for(int k = index + 1; k <= N && ans < T && sum < T; ++k) {
if(sum + a[k] <= T && sum + suf[k] > ans)
Back(k, sum + a[k]);
if(suf[k + 1] + sum > ans && suf[k + 1] + sum <= T)
Back(k, sum);
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> N >> T;
a.resize(N + 1), suf.resize(N + 1);
for(int i = 1; i <= N; ++i)
cin >> a[i];
suf[N] = a[N];
for(int i = N - 1; i > 0; --i)
suf[i] = suf[i + 1] + a[i];
if(N <= 40) {
if(a[1] <= T)
Back(1, a[1]);
Back(1, 0);
}
else {
sort(a.begin() + 1, a.end());
int p = 1, sum = 0;
for(int i = 1; i <= N; ++i) {
while(p <= N && sum + a[p] <= T)
sum += a[p++];
max_self(ans, sum);
sum -= a[i];
}
}
cout << ans;
}
|
#include <bits/stdc++.h>
using namespace std;
int a[7], temp[7];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
string S, X;
cin >> S >> X;
a[0] = 1;
for(int j = n - 1;j >= 0;--j) {
if(X[j] == 'T') {
for(int i = 0;i < 7;++i)
if(a[(i * 10) % 7] == 1 || a[(i * 10 + S[j] - '0') % 7] == 1) temp[i] = 1;
else temp[i] = 0;
for(int i = 0;i < 7;++i) a[i] = temp[i];
}
if(X[j] == 'A') {
for(int i = 0;i < 7;++i) {
if(a[(i * 10) % 7] == 1 && a[(i * 10 + S[j] - '0') % 7] == 1) temp[i] = 1;
else temp[i] = 0;
}
for(int i = 0;i < 7;++i) a[i] = temp[i];
}
}
cout << (a[0] == 1 ? "Takahashi" : "Aoki");
} | #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define rep(i,n) for(ll i=0;i<n;++i)
#define ocut cout
#define ouct cout
#define itn int
struct Union{
vector<ll> par;
Union(ll a){
par=vector<ll>(a,-1);
}
ll find(ll a){
if(par[a]<0){
return a;
}
else{
return par[a]=find(par[a]);
}
}
bool same(ll a,ll b){
return (find(a)==find(b));
}
ll size(ll a){
return -par[find(a)];
}
void unite(ll a,ll b){
ll c=find(a),d=find(b);
if(c==d)
return;
if(size(c)<size(d)){
swap(c,d);
}
par[c]+=par[d];
par[d]=c;
}
};
ll euclidean_gcd(ll a, ll b) {
if(a < b) return euclidean_gcd(b, a);
ll r;
while ((r=a%b)) {
a = b;
b = r;
}
return b;
}
ll euclidean_lcm(ll a, ll b) {
return a/euclidean_gcd(a,b)*b;
}
int main(void){
ll n;
cin >> n;
ll a[n],b[n],c[n],d[n],v=0;
rep(i,n){
cin >> a[i] >> b[i];
v+=a[i];
c[i]=a[i]+b[i];
d[i]=c[i]+a[i];
}
sort(d,d+n);
rep(i,n){
v-=d[n-i-1];
if(v<0){
cout << i+1 << endl;
return 0;
}
}
} |
#include <cstring>
#include <iostream>
using namespace std;
using ll = long long;
const int MAXN = 102;
const int MAXC = 122505;
ll k, mod, table[MAXN][MAXC], ret[MAXN];
int n, now;
void calc() {
memset(table, 0, sizeof(table));
table[0][0] = 1;
for (int i = 1; i <= n; ++i) {
int c = (k * (i + 1) * i) >> 1;
c = min(c, MAXC - 1);
for (int j = 0; j <= c; ++j) {
for (int v = 0; v <= k; ++v) {
int tmp = j + v * i;
if (tmp > c) break;
table[i][tmp] += table[i-1][j];
if (table[i][tmp] >= mod) table[i][tmp] -= mod;
}
}
}
}
int main(){
ios::sync_with_stdio(0), cin.tie(0);
while (cin >> n >> k >> mod) {
calc();
for (now = 1; now * 2 <= n + 1; now++) {
ll ans = 0;
int l = now - 1, r = n - now;
ans += k * table[l][0] * table[r][0];
ans %= mod;
for (int j = 1; j < MAXC; ++j) {
ans += (k + 1LL) * ((table[l][j] * table[r][j]) % mod);
ans %= mod;
}
ret[now] = ans;
ret[n - now + 1] = ans;
}
for (now = 1; now <= n; ++now) cout << ret[now] << '\n';
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
long long MOD;
int main(){
int N, K;
cin >> N >> K >> MOD;
vector<vector<long long>> f(N);
f[0].push_back(1);
for (int i = 1; i < N; i++){
int sz = f[i - 1].size();
f[i] = vector<long long>(sz + i * K, 0);
for (int j = 0; j < sz; j++){
f[i][j] = f[i - 1][j];
}
for (int j = i; j < sz + i * K; j++){
f[i][j] += f[i][j - i];
f[i][j] %= MOD;
}
for (int j = sz + i * K - 1; j >= i * (K + 1); j--){
f[i][j] += MOD - f[i][j - i * (K + 1)];
f[i][j] %= MOD;
}
}
for (int i = 0; i < N; i++){
long long ans = 0;
int d = min(f[i].size(), f[N - 1 - i].size());
for (int j = 0; j < d; j++){
ans += f[i][j] * f[N - 1 - i][j] % MOD;
ans %= MOD;
}
ans *= (K + 1);
ans %= MOD;
ans += MOD - 1;
ans %= MOD;
cout << ans << endl;
}
} |
#include<iostream>
using namespace std;
int main()
{
int a, b, c, d;
cin >> a >> b >> c >> d;
cout << a * d - b * c << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define debug printf("%d %s\n",__LINE__,__FUNCTION__);fflush(stdout)
using ll=long long;using i64=long long;using db=double;
using u32=unsigned int;using u64=unsigned long long;using db=double;
using pii=pair<int,int>;using vi=vector<int>;
using qi=queue<int>;using pqi=priority_queue<int>;using si=set<int>;
#define pb push_back
#define mk make_pair
#define ins insert
#define era erase
#define fi first
#define se second
#define lowbit(x) x&-x
#define ALL(a) a.begin(),a.end()
const int INF=0x3f3f3f3f;
const ll INFLL=0x3f3f3f3f3f3f3f3f;
const double PI=acos(-1.0);
template<class T>inline bool chkmin(T&a,T b){return b<a?a=b,true:false;}
template<class T>inline bool chkmax(T&a,T b){return a<b?a=b,true:false;}
int _w,_t;FILE* _f;
int a,b,c,d;
void solve(){
scanf("%d%d%d%d",&a,&b,&c,&d);
printf("%d\n",a*d-b*c);
}
int main(){
#ifdef MULTI_CASES
_w=scanf("%d",&_t);while(_t--)
#endif
solve();
return 0;
} |
#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
using namespace std;
const int N = 2e5 + 5;
int mod = 998244353;
int pre[N]{};
void add(int pos){
pos = N - pos - 1;
for (int i = pos; i < N; i += i & -i) pre[i]++;
}
int query(int pos){
pos = N - pos - 1;
int ans = 0;
for (int i = pos; i; i -= i & -i) ans += pre[i];
return ans;
}
int main(){
ios::sync_with_stdio(0), cin.tie(0);
int n;
cin >> n;
int a[n], b[n], tar[n];
for (int i = 0; i < n; i++) cin >> a[i];
map<int, queue<int>> v;
for (int i = 0; i < n; i++){
cin >> b[i];
v[b[i] + i].push(i);
}
for (int i = 0; i < n; i++){
if (v[a[i] + i].empty()){
cout << -1 << '\n';
return 0;
}
tar[i] = v[a[i] + i].front();
v[a[i] + i].pop();
}
ll ans = 0;
for (int i = 0; i < n; i++){
//cout << tar[i] - i<< ' ';
ans += tar[i] + query(tar[i]) - i;
add(tar[i]);
}
cout << ans << '\n';
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
const int MOD = 1000000007;
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
vector<int> A(N);
int res = 0;
for (int i = 0; i < N; i++) {
cin >> A[i];
}
map<int, int> mp;
mp[0] = 1;
int sum = 0;
int c = 1;
for (int i = 0; i < N; i++) {
sum += A[i] * c;
c *= -1;
res += mp[sum];
mp[sum]++;
}
cout << res << endl;
} |
//Let's join Kaede Takagaki Fan Club !!
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//#define int long long
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define a first
#define b second
#define fi first
#define sc second
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
#define all(x) x.begin(),x.end()
#define si(x) int(x.size())
#ifdef LOCAL
#define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
#else
#define dmp(x) void(0)
#endif
template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}
template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}
template<class t> using vc=vector<t>;
template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
return os<<"{"<<p.fi<<","<<p.sc<<"}";
}
template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
os<<"{";
for(auto e:v)os<<e<<",";
return os<<"}";
}
template<class T>
void g(T &a){
cin >> a;
}
template<class T>
void o(const T &a,bool space=false){
cout << a << (space?' ':'\n');
}
//ios::sync_with_stdio(false);
const ll mod = 1000000007;//998244353
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
template<class T>
void add(T&a,T b){
a+=b;
}
ll modpow(ll x,ll n){
ll res=1;
while(n>0){
if(n&1) res=res*x%mod;
x=x*x%mod;
n>>=1;
}
return res;
}
ll F[2000005],R[2000005];
void make(){
F[0] = 1;
for(int i=1;i<2000005;i++) F[i] = F[i-1]*i%mod;
for(int i=0;i<2000005;i++) R[i] = modpow(F[i],mod-2);
}
ll C(int a,int b){
return F[a]*R[b]%mod*R[a-b]%mod;
}
ll a, b;
int pr[20]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71};
ll dp[2][(1<<20)];
void solve(){
cin >> a >> b;
int cur = 0, nxt = 1;
dp[cur][0] = 1;
for(ll i=a;i<=b;i++){
memset(dp[nxt], 0, sizeof(dp[nxt]));
int cur_m = 0;
rep(x, 20){
if(i % pr[x] == 0) cur_m |= (1<<x);
}
rep(j, (1<<20)){
if(dp[cur][j] == 0) continue;
add(dp[nxt][j], dp[cur][j]);
if( (j & cur_m) == 0) add(dp[nxt][j+cur_m], dp[cur][j]);
}
swap(cur,nxt);
}
ll ans = 0;
rep(i,(1<<20)) ans += dp[cur][i];o(ans);
}
signed main(){
cin.tie(0);
ios::sync_with_stdio(0);
cout<<fixed<<setprecision(20);
int t; t = 1; //cin >> t;
while(t--) solve();
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
cin.tie(nullptr)->sync_with_stdio(false);
vector<int>primes = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71};
vector<int>dp(1<<20);
dp[0] = 1;
int a,b;
cin >> a >> b;
for(int i = a; i<=b; i++){
int v = 0;
for(int j = 0; j<20; j++){
if(i%primes[j]==0){
v+=(1<<j);
}
}
vector<int>dp2(1<<20);
for(int j = 0; j<(1<<20); j++){
dp2[j] = dp[j];
}
for(int j = 0; j<(1<<20); j++){
if((v|j)==v+j){
dp2[v|j]+=dp[j];
}
}
for(int j = 0; j<(1<<20); j++){
dp[j] = dp2[j];
}
}
int ans = 0;
for(int i = 0; i<(1<<20); i++){
ans+=dp[i];
}
cout << ans << "\n";
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int n;
cin >> n;
int a[n], b[n];
vector <ll> v;
ll sum = 0;
for(int i = 0; i < n; i++) {
cin >> a[i] >> b[i];
v.push_back(2LL * a[i] + b[i]);
sum += a[i];
}
sort(v.begin(), v.end(), greater<ll> ());
for(int i = 0; i < n; i++) {
if(v[i] > sum) {
cout << i + 1;
return 0;
}
sum -= v[i];
}
assert(0);
} | #include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
#define rl register long long
ll n,x,cur,ta,tb;
int main(){
scanf("%lld %lld",&n,&x);x*=100;
for(rl i=1;i<=n;++i){
scanf("%lld %lld",&ta,&tb);
cur+=ta*tb;
if(cur>x) return printf("%lld\n",i),0;
}
printf("-1\n");
return 0;
} |
#pragma GCC diagnostic warning "-Wextra"
#pragma GCC diagnostic warning "-Wshadow"
#include <bits/stdc++.h>
using namespace std;
template <class A, class B> bool cmin(A& a, B b) { return a > b && (a = b, true); }
template <class A, class B> bool cmax(A& a, B b) { return a < b && (a = b, true); }
template <class A, class B> ostream& operator<<(ostream& os, pair<A, B>& p) { return os << '(' << p.first << ", " << p.second << ')'; }
static bool debug = false;
void dump() {}
template <class A, class... B> void dump(A&& a, B&&... b) { if (debug) cout << a << (sizeof...(b) ? ' ' : '\n'), dump(b...); }
template <class A> void dump1D(A& a) { if (debug) for (auto i = a.begin(); i != a.end(); i++) cout << *i << (next(i) != a.end() ? ' ' : '\n'); }
template <class A> void dump2D(A& a) { if (debug) for (auto i = a.begin(); i != a.end(); i++) dump1D(*i), cout << (next(i) != a.end() ? '\0' : '\n'); }
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
debug = true;
long H, W;
cin >> H >> W;
vector<vector<char>> S(H, vector<char>(W));
for (long i = 0; i < H; i++) {
for (long j = 0; j < W; j++) {
cin >> S.at(i).at(j);
}
}
long ans = 0;
for (long h = 0; h < H; h++) {
for (long w = 0; w < W; w++) {
long cnt = 0;
for (long i = 0; i < 2; i++) {
for (long j = 0; j < 2; j++) {
if (h + i >= H) goto NG;
if (w + j >= W) goto NG;
cnt += S.at(h + i).at(w + j) == '#';
}
}
if (cnt == 1 || cnt == 3) ans++;
NG:;
}
}
cout << ans << '\n';
} | #define taskname "test"
#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int)x.size()
#define fi first
#define se second
typedef long long lli;
typedef pair<int, int> pii;
const int mod = 1e9 + 7;
int n, m;
int total = 0;
void read_input()
{
cin >> n >> m;
for(int i = 1; i <= n; ++i)
{
int a;
cin >> a;
total += a;
}
}
int powmod(int x, int k)
{
if(k == 0) return 1;
int t = powmod(x, k / 2);
if(k % 2 == 0) return t * 1LL * t % mod;
else return t * 1LL * t % mod * x % mod;
}
int inv(int x)
{
return powmod(x, mod - 2);
}
int C(int k, int n)
{
if(k > n) return 0;
int res = 1;
for(int i = n - k + 1; i <= n; ++i)
res = res * 1LL * i % mod;
for(int i = 1; i <= k; ++i)
res = res * 1LL * inv(i) % mod;
return res;
}
int calc(int k, int n)
{
return C(k, n + k);
}
void solve()
{
cout << calc(n + total, m - total) << '\n';
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
read_input();
solve();
}
|
#include <bits/stdc++.h>
#define endl "\n"
#define ff first
#define ss second
#define PB push_back
#define MP make_pair
#define ll long long
#define llu unsigned long long
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define ABS(X) ((X) < 0 ? -(X) : (X))
#define LCM(X, Y) (((X) / (GCD((X), (Y)))) * (Y))
#define MEM(A, B) memset((A), (B), sizeof(A))
#define MEM_VEC(V, FILL) fill(V.begin(), V.end(), FILL)
#define FOR_EACH(IT, V) for (auto IT = V.begin(); IT != V.end(); IT++)
#define DEBUG_ARRAY_PRINT(A, I) cout << "at pos " << I << " value is: " << A[I]
#define TURBO \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0)
const double EPS = 1e-9;
const double PI = 3.1415926535897932384626433832795;
using namespace std;
inline ll GCD(ll A, ll B)
{
if (B == 0)
return A;
return GCD((B), (A % B));
}
inline ll POW(ll BASE, ll EXP)
{
ll RES = 1;
for (ll I = 0; I < EXP; I++)
RES *= BASE;
return RES;
}
inline ll BIGMOD(ll a, ll p, ll m)
{
ll res = 1 % m;
ll x = a % m;
while (p > 0)
{
if ((p & 1) > 0)
res = (res * x) % m;
x = (x * x) % m;
p >>= 1;
}
return res;
}
inline void FILE_HANDLE()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
int main()
{
TURBO;
//FILE_HANDLE() ;
ll n;
scanf("%lld", &n);
vector<ll> v;
for (ll i = 1; i * i <= n; i++)
{
if (n % i == 0)
{
if (n / i == i)
v.PB(i);
else
{
v.PB(i);
v.PB(n / i);
}
}
}
sort(v.begin(), v.end());
for (int i = 0; i < v.size(); i++)
printf("%lld\n", v[i]);
} | #include <bits/stdc++.h>
#define fo(a,b,c) for (a=b; a<=c; a++)
#define fd(a,b,c) for (a=b; a>=c; a--)
#define ll long long
//#define file
using namespace std;
int T,n,i,j,k,l;
int a[501];
int d[250001],tot;
bool bz;
void add(int x)
{
swap(a[x],a[x+1]);
d[++tot]=x;
bz^=1;
}
void Checker()
{
int i;
fo(i,1,n) if (a[i]!=i) exit(1);
if (tot>n*n) exit(2);
fo(i,1,tot) if ((d[i]&1)!=(i&1)) exit(3);
}
int main()
{
#ifdef file
freopen("c.in","r",stdin);
freopen("c.out","w",stdout);
#endif
scanf("%d",&T);
for (;T;--T)
{
scanf("%d",&n);tot=0;bz=1;
fo(i,1,n) scanf("%d",&a[i]);
if (n==2)
{
if (a[1]>a[2])
add(1);
}
else
{
fd(i,n,4)
if (a[i]>a[i+1])
{
if ((i&1)!=bz)
{
if (bz) add(1);
else add(2);
}
fo(j,i,n-1) if (a[j]>a[j+1]) add(j); else break;
}
if (bz==1)
{
fo(j,3,n-1)
if (a[j]>a[j+1]) add(j); else break;
}
else
{
add(2);
fo(j,3,n-1)
if (a[j]>a[j+1]) add(j); else break;
}
if (bz)
{
fo(j,1,n-1)
if (a[j]>a[j+1]) add(j); else break;
}
else
{
fo(j,2,n-1)
if (a[j]>a[j+1]) add(j); else break;
}
if (a[1]!=1 || a[2]!=2)
{
if (bz==1)
{
add(1);
fo(j,2,n-1)
if (a[j]>a[j+1]) add(j); else break;
}
else
{
fo(j,2,n-1)
if (a[j]>a[j+1]) add(j); else break;
}
if (a[1]!=1)
{
if (a[1]!=2)
{
if (bz==1)
{
fo(j,1,n-1)
if (a[j]>a[j+1]) add(j); else break;
}
else
{
add(2);
fo(j,1,n-1)
if (a[j]>a[j+1]) add(j); else break;
}
}
if (a[1]>a[2])
{
if (bz) add(1);
else add(2),add(1),add(2),add(1),add(2);
}
}
}
}
Checker();
printf("%d\n",tot);
fo(i,1,tot) printf("%d ",d[i]);
printf("\n");
}
fclose(stdin);
fclose(stdout);
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define N 200005
#define INF 1e17
#define MP make_pair
const int mod = 998244353;
typedef long long LL;
unordered_map<int, int> F;
int f(int n, int k)
{
if (n < k) return 0;
if (n == k) return 1;
if (k == 0) return 0;
if (F.count(n*3005+k)) return F[n*3005+k];
LL ans = f(n, 2*k) + f(n-1, k-1);
ans %= mod;
F[n*3005+k] = ans;
return ans;
}
int main()
{
int n, k;
cin >> n >> k;
cout <<f(n,k) << endl;
return 0;
} | #include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <string.h>
#include <algorithm>
#include <cmath>
#define max(a,b) ((a) > (b) ? (a) : (b))
#define min(a,b) ((a) < (b) ? (a) : (b))
#define pb push_back
#define cl clear
#define MAXN 100005
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const int INF = 1e9;
inline int read(){
int x = 0, f = 1;
char ch = getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')
f = -1;
ch = getchar();
}
while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+(ch^48);
ch = getchar();
}
return x*f;
}
int n, k, cnt;
char s[105][105];
void solve(){
n = read();
k = read();
cin>>(s[0]+1);
if(n==1){
printf("%c\n",s[0][1]);
return ;
}
for(cnt = 1 ; cnt <= k ; ++cnt){
for(int i = 1, idx = 0 ; i <= n ; ++i, idx += 2){
// printf("%d\n",i);
if(s[cnt-1][idx%n+1]==s[cnt-1][(idx+1)%n+1]) s[cnt][i] = s[cnt-1][idx%n+1];
if(s[cnt-1][idx%n+1]=='S'&&s[cnt-1][(idx+1)%n+1]=='R') s[cnt][i] = 'R';
if(s[cnt-1][idx%n+1]=='R'&&s[cnt-1][(idx+1)%n+1]=='S') s[cnt][i] = 'R';
if(s[cnt-1][idx%n+1]=='S'&&s[cnt-1][(idx+1)%n+1]=='P') s[cnt][i] = 'S';
if(s[cnt-1][idx%n+1]=='P'&&s[cnt-1][(idx+1)%n+1]=='S') s[cnt][i] = 'S';
if(s[cnt-1][idx%n+1]=='R'&&s[cnt-1][(idx+1)%n+1]=='P') s[cnt][i] = 'P';
if(s[cnt-1][idx%n+1]=='P'&&s[cnt-1][(idx+1)%n+1]=='R') s[cnt][i] = 'P';
}
}
printf("%c\n",s[k][1]);
}
int main(){
int t = 1;
// t = read();
while(t--) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
map<string, int> m1, m2;
string ans = "satisfiable";
for(int i=0; i<n; i++){
string s;
cin >> s;
if(s[0] == '!'){
s = s.substr(1, s.size() - 1);
if(m2.count(s))
ans = s;
m1[s]++;
}else{
if(m1.count(s))
ans = s;
m2[s]++;
}
}
cout << ans;
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long int;
template<class T> void chmin(T& a, T b) { if (a > b) { a = b; } }
template<class T> void chmax(T& a, T b) { if (a < b) { a = b; } }
int main() {
int N;
cin >> N;
unordered_map<string, int> mp;
rep(i, N) {
string S;
cin >> S;
if (S[0] == '!') {
if (mp.find(S.substr(1, S.size() - 1)) != mp.end()) {
cout << S.substr(1, S.size() - 1) << endl;
return 0;
} else {
mp[S] = i;
}
} else {
if (mp.find("!" + S) != mp.end()) {
cout << S << endl;
return 0;
} else {
mp[S] = i;
}
}
}
cout << "satisfiable" << endl;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double pi = 3.141592653589793;
int inf = 1001001001;
ll INF = 1001001001001001001;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define rep(i, n) FOR(i, 0, n)
#define all(x) x.begin(), x.end()
#define pb push_back
#define pf push_front
ll MOD = 1000000007;
using P = pair<ll, ll>;
double eps = 1e-12;
int main() {
int N;cin >> N;
string S;cin >> S;
string T;cin >> T;
vector<int> cnts;
vector<int> cntt;
vector<int> places;
vector<int> placet;
ll cnt = 0;
rep(i, N) {
if (S[i] == '0') {
cnt++;
cnts.pb(i);
places.pb(cnt);
}
}
cnt = 0;
rep(i, N) {
if (T[i] == '0') {
cnt++;
cntt.pb(i);
placet.pb(cnt);
}
}
if (cnts.size() !=cntt.size()) {
cout << -1 << endl;
return 0;
}
ll ans = 0;
rep(i, cnts.size()) {
if (cnts[i] == cntt[i]&&places[i] == placet[i]) continue;
else ans++;
}
cout << ans << endl;
return 0;
}
| #include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<set>
#include<map>
#include<utility>
#include<queue>
#include<vector>
#include<stack>
#include<sstream>
#include<algorithm>
#define forn(i,a,b)for(int i=(a),_b=(b);i<=_b;i++)
#define fore(i,b,a)for(int i=(b),_a=(a);i>=_a;i--)
#define rep(i,n)for(int i=0,_n=n;i<n;i++)
#define ll long long
#define pii pair<int,int>
#define vi vector<int>
#define vpii vector<pii>
#define p_q priority_queue
#define m_p make_pair
#define pb push_back
#define ld long double
#define F first
#define S second
#define ull unsigned long long
#define mod 1000000007
#define md 998244353
#define x1 XZVJDFADSPFOE
#define y1 GASDIJSLDAEJF
#define x2 DFDAJKVOHKWIW
#define y2 PSFSAODSXVNMQ
#define LLINF 0x3f3f3f3f3f3f3f3fLL
#define foreach(i,c) for(__typeof(c.begin())i=(c.begin());i!=(c).end();i++)
using namespace std;
inline void read(int &x)
{
short negative=1;
x=0;
char c=getchar();
while(c<'0' || c>'9')
{
if(c=='-')
negative=-1;
c=getchar();
}
while(c>='0' && c<='9')
x=(x<<3)+(x<<1)+(c^48),c=getchar();
x*=negative;
}
ll qpow(ll n,ll k){
ll ans=1;
while(k){
if(k%2){
ans*=n;
ans%=mod;
}
n*=n;
n%=mod;
k/=2;
}
return ans;
}
string iots(int n)
{
string s="";
while(n)
{
int now=n%10;
s+=now+'0';
n/=10;
}
reverse(s.begin(),s.end());
return s;
}
int stoi(string s)
{
int n=0;
rep(i,s.size())
{
n*=10;
n+=s[i]-'0';
}
return n;
}
int n,m;
bool g[1111][1111];
int par[2222];
int find(int x)
{
if(par[x]==x)return x;
else return par[x]=find(par[x]);
}
void merge(int x,int y)
{
par[find(x)]=find(y);
}
int main()
{
ios::sync_with_stdio(0);
cin>>n>>m;
rep(i,n)
{
rep(j,m)
{
char c;
cin>>c;
if(c=='#')g[i][j]=1;
}
}
g[0][m-1]=g[n-1][m-1]=g[n-1][0]=1;
rep(i,n+m)par[i]=i;
rep(i,n)
{
rep(j,m)
{
if(g[i][j]==1)
{
merge(i,n+j);
}
}
}
set<int>s1,s2;
rep(i,n)s1.insert(find(i));
rep(i,m)s2.insert(find(i+n));
cout<<min(s1.size(),s2.size())-1<<endl;
return 0;
}
|
#include <bits/stdc++.h>
// clang-format off
using namespace std; using ll = long long; using ull = unsigned long long; using pll = pair<ll,ll>; const ll INF = 4e18;
void print0() {}
template<typename Head,typename... Tail> void print0(Head head,Tail... tail){cout<<fixed<<setprecision(15)<<head;print0(tail...);}
void print() { print0("\n"); }
template<typename Head,typename... Tail>void print(Head head,Tail... tail){print0(head);if(sizeof...(Tail)>0)print0(" ");print(tail...);}
// clang-format on
ll all;
vector<vector<vector<ll>>> memo;
ll h, w, a, b;
ll bit(ll i, ll j) {
return 1ULL << (i * w + j);
}
bool empty(ll stat, ll i, ll j){
if (i >= h) return false;
if (j >= w) return false;
if(stat & bit(i,j)) return false;
return true;
}
ll dfs(ll stat, ll full, ll half, ll bi, ll bj) {
if (full < 0 || half < 0) return 0;
if (stat == all - 1) return 1;
if (memo[stat][full][half] != -INF) return memo[stat][full][half];
// 常に一番左上に置く
ll ti = -1;
ll tj = -1;
for (ll j = bj; j < w; j++) {
if(empty(stat,bi,j)){
ti=bi;
tj=j;
break;
}
}
for (ll i = bi+1; i < h; i++) {
if (ti >= 0) break;
for (ll j = 0; j < w; j++) {
if (empty(stat,i,j)) {
ti = i;
tj = j;
break;
}
}
}
return memo[stat][full][half] =
dfs(stat | bit(ti,tj), full, half - 1, ti, tj) +
(empty(stat,ti+1,tj) ? dfs(stat | bit(ti+1,tj) | bit(ti,tj), full - 1, half, ti, tj) : 0) +
(empty(stat,ti,tj+1) ? dfs(stat | bit(ti,tj+1) | bit(ti,tj), full - 1, half, ti, tj) : 0);
}
int main() {
cin >> h >> w >> a >> b;
all = 1ll << (h * w);
memo.resize(all, vector<vector<ll>>(a + 1, vector<ll>(b + 1, -INF)));
print(dfs(0, a, b, 0, 0));
}
| #include<bits/stdc++.h>
using namespace std;
#define debug printf("%d %s\n",__LINE__,__FUNCTION__);fflush(stdout)
using ll=long long;using i64=long long;using db=double;
using u32=unsigned int;using u64=unsigned long long;using db=double;
using pii=pair<int,int>;using vi=vector<int>;
using qi=queue<int>;using pqi=priority_queue<int>;using si=set<int>;
#define pb push_back
#define mk make_pair
#define ins insert
#define era erase
#define fi first
#define se second
#define lowbit(x) x&-x
#define ALL(a) a.begin(),a.end()
const int INF=0x3f3f3f3f;
const ll INFLL=0x3f3f3f3f3f3f3f3f;
const double PI=acos(-1.0);
template<class T>inline bool chkmin(T&a,T b){return b<a?a=b,true:false;}
template<class T>inline bool chkmax(T&a,T b){return a<b?a=b,true:false;}
int _w,_t;FILE* _f;
const int N=1<<16;
int h,w,a,b,mx,cnt;
ll f[2][N][17];
void solve(){
scanf("%d%d%d%d",&h,&w,&a,&b);
if(h<w){
swap(h,w);
}
mx=1<<w;
f[1][mx-1][0]=1;
for(int i=1;i<=h;i++){
for(int j=0;j<w;j++){
memset(f[cnt],0,sizeof f[cnt]);
cnt^=1;
for(int k=0;k<mx;k++){
for(int l=0;l<=b;l++){
if(j&&(k>>j&1)&&!(k>>(j-1)&1)){
f[cnt^1][k|(1<<(j-1))][l]+=f[cnt][k][l];
}
if(!(k>>j&1)){
f[cnt^1][k|(1<<j)][l]+=f[cnt][k][l];
}
if((k>>j&1)&&(l<b)){
f[cnt^1][k][l+1]+=f[cnt][k][l];
}
if((k>>j&1)){
f[cnt^1][k^(1<<j)][l]+=f[cnt][k][l];
}
}
}
}
}
printf("%lld\n",f[cnt^1][mx-1][b]);
}
int main(){
#ifdef MULTI_CASES
_w=scanf("%d",&_t);while(_t--)
#endif
solve();
return 0;
} |
//#pragma GCC target("avx512f,avx512dq,avx512cd,avx512bw,avx512vl")
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define DB double
#define LD long double
#define ST string
#define BS bitset
#define PA pair<LL,LL>
#define VE vector
#define VL VE<LL>
#define VP VE<PA>
#define VVL VE<VL>
#define VVVL VE<VVL>
#define PQ priority_queue
#define PQS priority_queue<LL,vector<LL>,greater<LL>>
#define FI first
#define SE second
#define PB push_back
#define POB pop_back
#define PF push_front
#define POF pop_front
#define MP make_pair
#define TS to_string
#define TU to_ullong
#define BPL __builtin_popcountll
#define FOR(i,a,n) for(i=a;i<n;++i)
#define FORR(i,a,n) for(i=n-1;i>=a;--i)
#define rep(i,n) FOR(i,0,n)
#define repr(i,n) FORR(i,0,n)
#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
#define SORT(a) sort(ALL(a))
#define REV(a) reverse(ALL(a))
#define UB(a,n) *upper_bound(ALL(a),n)
#define UBn(a,n) upper_bound(ALL(a),n)-a.begin()
#define LB(a,n) *lower_bound(ALL(a),n)
#define LBn(a,n) lower_bound(ALL(a),n)-a.begin()
#define INF 1000000000000000003
#define PI 3.14159265358979323846264338327950288
//#define MOD 1000000007
#define MOD 998244353
#define ERR 1e-10
#define coutl cout<<fixed<<setprecision(15)
#define FAST cin.tie(0);ios::sync_with_stdio(false)
void Yn(LL a){if(a)cout<<"Yes"<<endl;else cout<<"No"<<endl;}
void YN(LL a){if(a)cout<<"YES"<<endl;else cout<<"NO"<<endl;}
LL pwmn(LL a,LL n){LL ans=1;while(ans<a)ans*=n;return ans;}
LL dig(LL n){LL ret=0;while(n)n/=10,++ret;return ret;}
LL GCD(LL a,LL b){LL c=1,tmp=max(a,b);b=min(a,b);a=tmp;while(c!=0){c=a%b;a=b;b=c;}return a;}
LL LCM(LL a,LL b){return a*b/GCD(a,b);}
LL cmod(LL a,LL m){if(a%m<0)return a%m+abs(m);else return a%m;}
LL cdiv(LL a,LL b){return ((a<0&&b<0)||(a>=0&&b>=0)?a/b:(a-b+1)/b);}
LL DIV(LL a,LL d,LL m){LL l=m,x=1,y=0,k;while(l){k=d/l;d-=k*l;swap(l,d);x-=k*y;swap(x,y);}return cmod(a*cmod(x,m),m);}
LL POW(LL a,LL n,LL m){LL ans=1;while(n>0){if(n&1)ans=ans*a%m;a=a*a%m;n>>=1;}return ans;}
VL fact,finv,inv;
void comi(LL n){LL i;fact.resize(max(2LL,n+1));finv.resize(max(2LL,n+1));inv.resize(max(2LL,n+1));fact[0]=fact[1]=1;finv[0]=finv[1]=1;inv[0]=inv[1]=1;FOR(i,2,n+1){fact[i]=fact[i-1]*i%MOD;inv[i]=MOD-inv[MOD%i]*(MOD/i)%MOD;finv[i]=finv[i-1]*inv[i]%MOD;}}
LL com(LL n,LL k){if(n<k||n<0||k<0)return 0;return fact[n]*(finv[k]*finv[n-k]%MOD)%MOD;}
bool cmps(PA a,PA b){if(a.SE!=b.SE)return a.SE<b.SE;return a.FI<b.FI;}
template<typename T>bool chmax(T &a,T b){if(a<b){a=b;return true;}return false;}
template<typename T>bool chmin(T &a,T b){if(a>b){a=b;return true;}return false;}
template<typename T>void vout(VE<T> &v){LL i;rep(i,v.size()){cout<<v[i];if(i<v.size()-1)cout<<" ";}cout<<endl;}
template<typename T>void v2out(VE<VE<T>> &v){for(auto a:v)vout(a);}
VL pri;
void prii(LL n){LL i,j,k,p,f,s,a[8]={7,11,13,17,19,23,29,31};pri.PB(2);pri.PB(3);pri.PB(5);for(i=0;i<=n;i+=30)rep(j,8)if(i+a[j]<=n){p=i+a[j],f=0,s=sqrt(p)+1;FOR(k,3,pri.size()){if(pri[k]>s)break;if(p%pri[k]==0)f=1;}if(f==0)pri.PB(p);}}
int main(){
FAST;
LL i,j,ans=INF,N;
cin>>N;
VL X(N);
rep(i,N)cin>>X[i];
prii(100);
rep(i,1<<15){
LL a=1,f=1;
rep(j,15)if(i&(1<<j))a*=pri[j];
rep(j,N)if(GCD(X[j],a)==1)f=0;
if(f)chmin(ans,a);
}
cout<<ans<<endl;
}
| #include <bits/stdc++.h>
#define ff first
#define ss second
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
#define all(x) x.begin(), x.end()
const int p[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47};
void solve() {
int n, m = sizeof(p) / sizeof(int);
cin >> n;
vector <int> a(n);
ll ans = 1e18;
for (auto &i : a)
cin >> i;
for (int k = 0; k < (1 << m); k++) {
vector <bool> vs(n);
ll res = 1;
for (int i = 0; i < m; i++) {
if ((k >> i) & 1)
continue;
res *= p[i];
for (int j = 0; j < n; j++) {
if (a[j] % p[i] == 0)
vs[j] = true;
}
}
if (count(all(vs), true) == n)
ans = min(ans, res);
}
cout << ans << '\n';
}
int main() {
#ifndef ONLINE_JUDGE
freopen("inp.txt", "r", stdin); freopen("outp.txt", "w", stdout);
#endif
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
solve();
return 0;
} |
#ifdef _LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
string solve(ll n) {
if(n == 0)
return "Yes";
while(n % 10ll == 0ll)
n /= 10ll;
string s = to_string(n);
bool ok = true;
int size = (int)s.size();
for(int i = 0; i < size / 2; i++) {
if(s[i] != s[size - 1 - i]) {
ok = false;
break;
}
}
return ok ? "Yes" : "No";
}
#ifndef _LOCAL
int main() {
ll n;
cin >> n;
cout << solve(n) << endl;
return 0;
}
#endif | #include<bits/stdc++.h>
using namespace std;
int main()
{
int n,i,k,t,c=0,ss=0,ev=0,od=0;
string s;
cin>>s;
for(int i=0;i<s.size();i++){
if(i%2 ==0 && s[i]>='a' && s[i]<='z')c++;
else if(i%2!=0 && s[i]>='A' && s[i]<='Z')ss++;
if(i%2==0)ev++;
else od++;
}
//cout<<ev<<" "<<od<<endl<<c<<" "<<ss<<endl;
if(c==ev && ss==od)cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(long long i=0;i<(n);++i)
using ll = long long;
using P=pair<long long ,long long>;
bool isPalindrome(string s){
string rs=s;
reverse(rs.begin(),rs.end());
return rs==s;
}
int main() {
int n;
cin>>n;
if(n==0){
cout<<"Yes"<<endl;
return 0;
}
while(n%10==0) n/=10;
if(isPalindrome(to_string(n))){
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
}
return 0;
}
| #pragma GCC optimize("Ofast,unroll-loops,fast-math")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll ;
#define pll pair<ll , ll >
#define all(x) (x).begin(),(x).end()
#define SZ(x) (ll)(x).size()
#define X first
#define Y second
#define mp make_pair
#define pii pair<int , int>
#define vec vector
#define file_io freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#define migmig ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
// BIG p : 1000000000000037 , 100000000003
ll poww(ll a, ll b, ll md) {
return (!b ? 1 : (b & 1 ? a * poww(a * a % md, b / 2, md) % md : poww(a * a % md, b / 2, md) % md));
}
const int maxn = 1000*100+5 ;
const ll inf = 9223372036854775807 ;
const ll mod = 1e9 + 7 ;
const int lg = 20 ;
int ans = 1 ;
int main()
{
migmig ;
vec<char> v ;
char c ;
string s ;
cin>>s ;
if(s == "0"){
cout<<"Yes" ; return 0 ;
}
for(int i = 0 ; i < SZ(s) ; i ++ ) v.pb(s[i]) ;
while(v.back() == '0') v.pop_back() ;
for(int i = 0 ; i <= SZ(v) / 2 ; i ++ ){
if(v[i] != v[(SZ(v) - 1 - i)]) ans = 0 ;
}
if(ans) cout<<"Yes" ;
else cout<<"No" ;
}
|
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define ul unsigned long long int
#define vi vector<int>
#define vl vector<ul>
#define ff first
#define ss second
#define INF 1000000000
int main()
{
int n;
ll x;
cin>>n>>x;
vector<ll>v;
for(int i=0;i<n;i++)
{
ll k;
cin>>k;
v.pb(k);
}
for(int i=0;i<v.size();i++)
{
if(v[i]==x)
continue;
else
cout<<v[i]<<" ";
}
cout<<"\n";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main(){char c;string s;cin>>c>>s;cout<<s<<c;} |
#include<iostream>
using namespace std;
int main(){
int a,b;
cin >> a >> b;
cout << (2*a + 100) - b << endl;
} | #include<bits/stdc++.h>
using namespace std;
#define arep(i,x,n) for(int i=int(x);i<(int)(n);i++)
#define rep(i,n) for(long long i = 0;i < n;++i)
#define rrep(i,n) for(int i=int(n-1);i>=0;i--)
#define fs first
#define sc second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define coy cout<<"Yes"<<endl
#define con cout<<"No"<<endl
#define pi 3.141592653589793
#define eps 0.00000000001
#define INF 1e9+7
#define LINF 1e18+10
using ll = long long;
using P = pair<int, int>;
using lP = pair<ll, ll>;
using fP = pair<double, double>;
using PPI = pair<P, int>;
using PIP = pair<int, P>;
using Ps = pair<int, string>;
using vi = vector<int>;
using vl = vector<ll>;
using vc = vector<char>;
using vd = vector<double>;
using vs = vector<string>;
using vp = vector<P>;
using vb = vector<bool>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vvd = vector<vector<double>>;
using vvc = vector<vector<char>>;
using vvp = vector<vector<P>>;
using vvb = vector<vector<bool>>;
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; }
//const ll mod=998244353;
const ll mod = 1e9 + 7;
const ll MAX = 300000;
template <typename T>
T abs(T a) { if (a < 0)return -a; else return a; }//2020/09/30 stdlib has abs(long) abs(long long) error
//////////////////////////////////////
int main(){
int a,b;
cin>>a>>b;
int ans=2*a+100-b;
chmax(ans,0);
cout<<ans<<endl;
return 0;
} |
/* Author: rrrr_wys
**/
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define per(i, a, b) for (int i = (a); i >= (b); --i)
#define pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second
typedef double db;
typedef long long ll;
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
const int N = 2000110;
const int P = 998244353;
int n, q, fa[N], d[N], ans[N], num[N];
vector<int> G[N], Q[N];
void dfs(int u, int dep) {
map<int, int> M;
for (int qi : Q[u]) {
M[d[qi]] = num[d[qi]];
}
for (int v : G[u]) {
dfs(v, dep + 1);
}
num[dep]++;
for (int qi : Q[u]) {
ans[qi] = num[d[qi]] - M[d[qi]];
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
rep(i, 2, n) cin >> fa[i], G[fa[i]].pb(i);
cin >> q;
rep(i, 1, q) {
int u;
cin >> u >> d[i];
Q[u].pb(i);
}
dfs(1, 0);
rep(i, 1, q) cout << ans[i] << "\n";
} | //#pragma GCC optimize ("O2")
//#pragma GCC target ("avx2")
//#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please
const int CM = 1 << 17, CL = 12;
char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint() {
if (ci - owa > 0) {
memcpy(cn, owa, CL);
ci -= CM;
fread(cn + CL, 1, CM, stdin);
}
ll tmp = *(ll*)ci;
int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0);
tmp = tmp << dig & ma1;
tmp = tmp * 10 + (tmp >> 8) & ma2;
tmp = tmp * 100 + (tmp >> 16) & ma3;
tmp = tmp * 10000 + (tmp >> 32) & ma4;
ci += 72 - dig >> 3;
return tmp;
}
const int bm = 200010;
int BIT[bm];
void add(int A) {
while (A <= bm) {
BIT[A]++;
A += A & -A;
}
}
int query(int A) {
int ret = 0;
while (A > 0) {
ret += BIT[A];
A -= A & -A;
}
return ret;
}
int Q[200001], ne[200001], he[200001];
int main() {
//cin.tie(0);
//ios::sync_with_stdio(false);
int H = getint(), W = getint(), M = getint();
int X[200001], Y[200001];
rep1(i, H) X[i] = W;
rep1(i, W) Y[i] = H;
rep(i, M) {
int x = getint(), y = getint();
if (y - 1 < X[x]) X[x] = y - 1;
if (x - 1 < Y[y]) Y[y] = x - 1;
}
ll kotae = 1;
int k = 1;
for (int i = 2; i <= Y[1]; i++) {
kotae += X[i];
Q[k] = i;
ne[k] = he[X[i]];
he[X[i]] = k++;
}
int atta = X[1];
for (int i = W; i > atta; i--) {
for (int q = he[i]; q; q = ne[q]) {
add(Q[q]);
}
}
for (int i = atta; i >= 2; i--) {
for (int q = he[i]; q; q = ne[q]) {
add(Q[q]);
}
int are = query(Y[i]);
kotae += Y[i] - are;
}
printf("%lld\n", kotae);
Would you please return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using lli = long long int;
using ld = long double;
#define FAST ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define JUGAD freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#define pb push_back
#define po pop_back
#define pof pop_front
#define eb emplace_back
#define pf push_front
#define mp make_pair
#define pii pair<int,int>
#define pll pair<lli,lli>
#define pdd pair<ld,ld>
#define pq priority_queue
#define qq queue
#define ff first
#define ss second
#define decimal fixed<<setprecision(20)
const int N = 1e5+3;
const lli MOD = 1e9+7;
lli gcd(lli a,lli b){
if(a==0){return b;}
return gcd(b%a,a);
}
lli binpow(lli a, lli b){
lli res=1;
while(b){
if(b&1){res*=a;}
a*=a;
b>>=1;
}
return res;
}
void solve(){
string s;cin>>s;
int ar[10];
for(int i=0;i<10;i++){
if(s[i]=='o'){
ar[i]=1;
}
else if(s[i]=='x'){ar[i]=0;}
else{ar[i]=2;}
}
int ans=0;
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
for(int k=0;k<10;k++){
for(int l=0;l<10;l++){
bool ok=1;
for(int a=0;a<10;a++){
if(ar[a]==1){
if(i!=a && j!=a && k!=a && l!=a){
ok=0;break;
}
}
else if(ar[a]==0){
if(i==a || j==a || k==a || l==a){
ok=0;break;
}
}
}
if(ok){ans++;}
}
}
}
}
cout<<ans;
}
int main(){
FAST;
solve();
return 0;
int test_cases;cin>>test_cases;
for(int tc=1;tc<=test_cases;tc++){
cout<<"Case #"<<tc<<": ";
}
} | #include<bits/stdc++.h>
#define INF 1e9
#define llINF 1e18
#define MOD 1000000007LL
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define ll long long
#define vi vector<ll>
#define vvi vector<vi>
#define SHIFT(n) (1LL<<((ll)n))
#define ALL(a) (a).begin(),(a).end()
#define Max(a) (*max_element(ALL(a)))
#define Min(a) (*min_element(ALL(a)))
#define Sum(a) (accumulate(ALL(a),0LL))
using namespace std;
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
string s;
cin >> s;
deque<char>deq;
ll fl = false;
for(auto c:s){
if(c == 'R'){
fl++;
fl %= 2;
}else{
if(fl){
if(!deq.empty() && deq.front() == c){
deq.pop_front();
}else{
deq.push_front(c);
}
}else{
if(!deq.empty() && deq.back() == c){
deq.pop_back();
}else{
deq.push_back(c);
}
}
}
}
if(fl){
while(!deq.empty()){
cout << deq.back();
deq.pop_back();
}
}else{
while (!deq.empty()){
cout << deq.front();
deq.pop_front();
}
}
cout << endl;
return 0;
} |
/*
Submitter: Nick Hudson
Anumber: A01634984
*/
#include <iostream>
int main(){
int N,K,M,input;
int sum;
int neededScore;
std::cin>>N>>K>>M;
//std::cout<<N<<K<<M<<std::endl;
for(int i = 0; i<N-1; ++i){
std::cin>>input;
//std::cout<<input<<std::endl;
sum += input;
//std::cout<<sum<<std::endl;
}
int x = N*M-sum;
if(x<0){
std::cout<<0;
} else if(x<=K){
std::cout<<x;
} else
std::cout<<-1;
return 0;
}
| #include <bits/stdc++.h>
// #include <atcoder/all>
#include <cmath>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
const ll INFI = 1001001001;
const ll INFL = 1001001001001001001;
const ll MOD = (int)1e9 + 7;
const double EPS = 1e-9;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
typedef pair<ll, ll> P;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, in, fn) for (ll i = in; i <= fn; i++)
#define debug(x) cerr << #x << ": " << x << endl
void sort2vectors(vector<ll> &av, vector<ll> &bv)
{
int n = av.size();
vector<ll> p(n), av2(n), bv2(n);
iota(p.begin(), p.end(), 0);
sort(p.begin(), p.end(), [&](ll a, ll b) { return av[a] < av[b]; });
for (int i = 0; i < n; i++)
{
av2[i] = av[p[i]];
bv2[i] = bv[p[i]];
}
av = av2;
bv = bv2;
}
int main()
{
long long K;
scanf("%lld", &K);
long long N;
scanf("%lld", &N);
long long M;
scanf("%lld", &M);
std::vector<long long> A(K), A2(K), num(K);
std::vector<long long> B(K), sa(K);
for (int i = 0; i < K; i++)
{
scanf("%lld", &A[i]);
}
ll sum = 0;
rep(i, K)
{
A2[i] = A[i];
num[i] = i;
B[i] = M * A[i] / N;
sum += B[i];
//debug(B[i]);
sa[i] = (llabs((B[i] + 1) * N - A[i] * M)) - (llabs(B[i] * N - A[i] * M));
//debug(sa[i]);
}
ll amari = M - sum;
sort2vectors(sa, num);
rep(i, amari)
{
B[num[i]]++;
}
rep(i, K)
{
cout << B[i] << " ";
}
cout << endl;
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define REP(i,n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define debug(var) do{cout << #var << " : "; view(var);}while(0)
template<class T> bool chmin(T &a, T b) {if(a>b) {a=b;return 1;}return 0;}
template<class T> bool chmax(T &a, T b) {if(a<b) {a=b;return 1;}return 0;}
using namespace std;
template<class T> void view(T e) {cout << e << endl;}
template<class T> void view(const vector<T> &v) {for(const auto &e : v){cout << e << " ";} cout << endl;}
template<class T> void view(const vector<vector<T>> &vv) {for(const auto &v : vv){view(v);}}
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int,int>;
const int inf = 1<<30;
const ll inf_l = 1LL<<61;
const int MAX = 2 * 1e5 + 5;
ll ans[MAX];
int dis[MAX];
vvint g;
void dfs(int v, int d = 0, int p = -1) {
dis[v] = d;
for (int n_v : g[v]) {
if (n_v == p) continue;
dfs(n_v, d + 1, v);
}
}
void solve(int v, int p = -1) {
for (int n_v : g[v]) {
if (n_v == p) continue;
ans[n_v] += ans[v];
solve(n_v, v);
}
}
int main() {
int n; cin >> n;
g.resize(n);
vector<P> dat(n);
rep(i,n-1) {
int a, b; cin >> a >> b;
a--; b--;
dat[i] = P(a, b);
g[a].push_back(b);
g[b].push_back(a);
}
dfs(0);
// rep(i,n) cout << dis[i] << endl;
int q; cin >> q;
rep(i,q) {
int t, e, x; cin >> t >> e >> x;
e--;
auto [a, b] = dat[e];
if (t == 1) {
if (dis[a] < dis[b]) {
ans[0] += x;
ans[b] -= x;
} else {
ans[a] += x;
}
} else {
if (dis[a] < dis[b]) {
ans[b] += x;
} else {
ans[0] += x;
ans[a] -= x;
}
}
}
solve(0);
rep(i,n) cout << ans[i] << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define LL long long
constexpr LL MOD = 1000000007;
typedef vector<vector<LL>> Mat;
Mat mul(Mat A, Mat B) {
int N = A.size();
Mat C(N, vector<LL>(N, 0));
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
for (int k = 0; k < N; k++)
C[i][j] = (C[i][j] + ((A[i][k] * B[k][j]) % MOD)) % MOD;
return C;
}
LL extended_euclidean(LL a, LL b, LL &x, LL &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
LL x1, y1;
LL g = extended_euclidean(b, a % b, x1, y1);
x = y1;
y = x1 - (a / b) * y1;
return g;
}
LL invmod(LL a) {
LL x, y;
extended_euclidean(a, MOD, x, y);
return (x % MOD + MOD) % MOD;
}
vector<vector<int>> adj;
Mat A; // weight
Mat m; // transition matrix
int main() {
int N, M, K;
cin >> N >> M >> K;
A.resize(N);
adj.resize(N);
m.resize(N);
for (int i = 0; i < N; i++) {
A[i].resize(1);
cin >> A[i][0];
}
for (int i = 0; i < M; i++) {
int u, v;
cin >> u >> v;
u--, v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
// calculate the matrix.
LL inv2M = invmod(2 * M);
for (int i = 0; i < N; i++) {
m[i].assign(N, 0);
m[i][i] = 1LL;
for (int j : adj[i]) {
m[i][j] = inv2M;
m[i][i] = (m[i][i] - inv2M + MOD) % MOD;
}
}
while (K > 0) {
if (K & 1)
A = mul(m, A);
m = mul(m, m);
K >>= 1;
}
for (int i = 0; i < N; i++)
cout << A[i][0] << endl;
} |
#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#include <limits.h>
using namespace std;
typedef long long ll;
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;
}
const int INF = INT_MAX / 2;
vector<int> as;
int main(void) {
int N, M;
cin >> N >> M;
as.push_back(0);
for (int i = 0; i < M; i++) {
int a;
cin >> a;
as.push_back(a);
}
as.push_back(N + 1);
sort(as.begin(), as.end());
int max_interval = INF;
for (int i = 0; i < (int) as.size() - 1; i++) {
int l = as[i];
int r = as[i + 1];
int interval = r - l - 1;
if (interval > 0) {
chmin(max_interval, interval);
}
}
int ans = 0;
for (int i = 0; i < (int) as.size() - 1; i++) {
int l = as[i];
int r = as[i + 1];
int interval = r - l - 1;
if (interval > 0) {
ans += (interval + max_interval - 1) / max_interval;
}
}
cout << ans << endl;
return 0;
}
| #include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int M=1e5+5,N=(1<<8)+5;
int n,m,v[M],w[M],a[20],l[N],b[20],ans=998244353,f[N];
int main(){
scanf("%d%d",&n,&m);
rep(i,1,n) scanf("%d",a+i);
rep(i,1,m) scanf("%d%d",v+i,w+i);
int S=(1<<n)-1;
rep(i,1,S){
int sum=0;
rep(j,1,n) if((i>>(j-1))&1) sum+=a[j];
rep(j,1,m) if(sum>w[j]) l[i]=max(l[i],v[j]);
}
rep(i,1,n) rep(j,1,m) if(a[i]>w[j]) return puts("-1"),0;
rep(i,1,n) b[i]=i;
do{
f[0]=0;
rep(i,2,n){
int s=1<<b[i]-1; f[i]=0;
per(j,i-1,1) s|=1<<b[j]-1,f[i]=max(f[i],f[j]+l[s]);
}
ans=min(ans,f[n]);
}while(next_permutation(b+1,b+n+1));
printf("%d",ans);
return 0;
}
|
#include <bits/stdc++.h>
#define f first
#define s second
#define fore(i,a,b) for(int i = (a), ThxMK = (b); i < ThxMK; ++i)
#define pb push_back
#define all(s) begin(s), end(s)
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define sz(s) int(s.size())
#define ENDL '\n'
#define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...) vector<vector<vector<type>>> name(h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
using namespace std;
long long gcd(long long a, long long b){ return b? gcd(b, a%b) : a; }
long long lcm(long long a, long long b){ return (!a or !b)? 0 : a * b / gcd(a,b); }
long long poww(long long a, long long b){
long long res = 1;
while(b){
if(b%2) res = res * a ;
a = a * a; b>>=1;
} return res;
}
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// int rnd(int n){return uniform_int_distribution<int>(0, n-1)(rng);}
template <typename T> static constexpr T inf = numeric_limits<T>::max() / 2;
template<class t,class u>bool mmax(t&a,u b){if(a<b)a=b;return a<b;}
template<class t,class u>bool mmin(t&a,u b){if(b<a)a=b;return b<a;}
template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;
using li = long long;
using vi = vc<int>;
using ii = pair<int,int>;
// ---- しゃけ ツナマヨ ('-')7
int main(){_
auto solve=[&](){
int n; cin>>n;
string s; cin>>s;
n++;
vi v(n); fore(i,0,n)cin>>v[i];
int k = inf<int>;
fore(i,0,n-1)k=min(k,abs(v[i+1]-v[i]));
cout<<k<<ENDL;
fore(i,0,k){fore(j,0,n)cout<<(v[j]+i)/k<<" ";cout<<ENDL;}
};
//int t; cin>>t; while(t--)
solve();
}
| #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int N;
string S;
int a[5050], t[5050], c[5050], g[5050];
int sum_A[5050], sum_T[5050], sum_C[5050], sum_G[5050];
int main(void) {
cin >> N >> S;
S = '#' + S;
for (int i = 1; i <= N; i++) {
if (S[i] == 'A') { a[i] = 1; }
else if (S[i] == 'T') { t[i] = 1; }
else if (S[i] == 'C') { c[i] = 1; }
else if (S[i] == 'G') { g[i] = 1; }
}
for (int i = 1; i <= N; i++) {
sum_A[i] = sum_A[i - 1] + a[i];
sum_T[i] = sum_T[i - 1] + t[i];
sum_C[i] = sum_C[i - 1] + c[i];
sum_G[i] = sum_G[i - 1] + g[i];
}
int ans = 0;
for (int i = 1; i <= N; i++) {
for (int j = i + 1; j <= N; j++) {
if ((sum_A[j] - sum_A[i - 1] == sum_T[j] - sum_T[i - 1]) && (sum_G[j] - sum_G[i - 1] == sum_C[j] - sum_C[i - 1])) { ans++; }
}
}
cout << ans << endl;
return 0;
} |
// Problem: C - To 3
// Contest: AtCoder - AtCoder Beginner Contest 182
// URL: https://atcoder.jp/contests/abc182/tasks/abc182_c
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
#include <bits/stdc++.h>
#define ll long long int
#define ii int
#define du double
#define jmp "\n"
#define vl vector<ll>
#define vvl vector<vl>
#define pb push_back
#define printv(v) \
for (auto x : v) \
cout << x << " "; \
cout << jmp;
#define vi vector<int>
#define vb vector<bool>
#define ump unordered_map
#define SORT(v) sort(v.begin(), v.end())
#define REV(x) reverse(x.begin(), x.end())
#define all(x) x.begin(), x.end()
#define SET(x, a) memset(x, a, sizeof(x))
#define si(x) (ll)x.size()
#define ff first
#define ss second
#define I(a) for(auto &x:a) cin>>x;
#define iin insert
#define deb(x) cout << "test " << #x << "=" << x << jmp
#define deb2(x, y) cout << "test " << #x << "=" << x << "," << #y << "=" << y << jmp
using namespace std;
const ll nax = 1e5 + 5;
void solve()
{
unsigned ll a;
cin>>a;
ll b[3]={0},sum=0,dig=0;
while(a)
{
ll c=a%10;
sum+=(c);
sum%=3;
b[c%3]++,dig++;
a/=10;
}
if(sum==0)
cout<<0;
else if(sum==1)
{
if(b[1]&&dig>1)
cout<<1;
else if(b[2]>1&&dig>2)
cout<<2;
else
cout<<-1;
}
else
{
if(b[2]&&dig>1)
cout<<1;
else if(b[1]>1&&dig>2)
cout<<2;
else
cout<<-1;
}
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll t = 1;
// cin >> t;
while (t--)
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 ll long long int
#define ff first
#define ss second
typedef tree<pair<ll,ll>,null_type,less<pair<ll,ll>>,rb_tree_tag,
tree_order_statistics_node_update>pbds;
//order_of_key:index number if it was there in sorted array
//find_by_order:value at 3rd index in sorted array.
const ll maxs=1e6+5;
ll root[maxs];
class cmp{
bool operator()(const ll &a,const ll &b)
{
return a<b;
}
};
void init()
{
iota(root,root+maxs,0);
}
ll find(ll u)
{
ll f;
if(root[u]==u)
return u;
else
{
ll f=find(root[u]);
root[u]=f;
return f;
}
}
void Union(ll x,ll y)
{
ll u=find(x);
ll v=find(y);
root[v]=u;
}
ll power(ll x,ll y,ll p)
{
ll res=1;
x=x%p;
while(y>0)
{
if(y&1)
res=(res*x)%p;
y=y>>1;
x=(x*x)%p;
}
return res;
}
ll solve(ll mid,vector<ll>&arr)
{
ll cnt=0;
for(auto i:arr)
{
if(i%mid==0)
cnt++;
}
return cnt;
}
int main()
{
ll t,n,i,k,y,x,j,a,b;
cin>>n;
vector<ll>arr;
while(n>0)
{
ll d=n%10;
arr.push_back(d);
n/=10;
}
ll m=arr.size();
ll ans=0;
for(i=0;i<(1<<m);i++)
{
ll sum=0;
for(j=0;j<m;j++)
{
if((1<<j)&i)
{
sum+=arr[j];
}
}
if(sum%3==0)
{
ll f=__builtin_popcount(i);
ans=max(ans,f);
}
}
if(ans==0)
cout<<-1<<endl;
else
{
cout<<arr.size()-ans<<endl;
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
#define pob pop_back
#define b back
#define cout(x) cout<<x<<endl
#define lb long double
#define sor(arr,n) sort(arr,arr+n)
#define inputVector(v,n) for(int i=0;i<n;i++) cin>>v[i]
#define sov(v) sort(v.begin(),v.end())
#define pl pair<ll,ll>
#define all(a) (a).begin(),(a).end()
#define vpp vector<pair<ll,ll>>
#define fastIo ios_base::sync_with_stdio(false),cin.tie(0)
#define endl '\n'
typedef long long int ll;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
const int N=100001;
ll P=1e9+7;
void solve() {
ll i,n;
cin>>n;
vector<ll> a(n);
for(int i=0;i<n;i++) {
cin>>a[i];
}
sov(a);
if(n==1) {cout<<a[0]/2<<endl; return;}
vector<ll> pref(n);
pref[0]=a[0];
for(int i=1;i<n;i++) { pref[i]=pref[i-1]+a[i]; }
long double expectation=LLONG_MAX;
for(int i=1;i<n;i++) {
lb val=(a[i]/2.0);
lb exp=val*n;
exp+=pref[n-1]-pref[i-1];
exp-=(n-i)*2*val;
exp/=n;
expectation=min(expectation,exp);
}
cout<<fixed<<setprecision(10)<<expectation<<endl;
}
int main() {
ll t=1,i=1;
fastIo;
// cin>>t;
while(t--) {
solve();
i++;
}
return 0;
}
// may be a binary search too on the probabilities lets see. | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e3+9;
const int MAX=6e5+9;
const int MAX1=5e6+9;
const int INF=0x3f3f3f3f;
const double ep=1e-8;
const double ep1=1e-4;
const int mod=998244353;
const double inf=1e20;
const double pi=acos(-1);
#define debug1 puts("?");
#define debug(x) cout<<"##"<<(x)<<endl;
#define mk make_pair
#define PII pair<int,int>
#define PDI pair<double,int>
#define PIII pair<int,PII >
#define PIII1 pair<PII,int>
#define PIIII pair<PII,PII >
#define PIL pair<int,ll>
#define PLI pair<ll,int>
#define PLIII pair<PLI,PII >
#define PLL pair<ll,ll>
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define dep(i,a,b) for(int i=(a);i>=(b);--i)
#define sd(x) scanf("%d",&(x))
#define slld(x) scanf("%lld",&(x))
#define sdd(x,y) scanf("%d%d",&(x),&(y))
#define sc(s) scanf("%s",(s))
#define pd(x) printf("%d\n",(x))
#define plld(x) printf("%lld\n",(x))
#define pdk(x) printf("%d ",(x))
#define plldk(x) printf("%lld ",(x))
#define pdd(x,y) printf("%d %d\n",(x),(y))
#define PR(x) printf("Case %d: ",(x))
#define INF 0x3f3f3f3f
typedef unsigned long long ull;
/*
*/
struct node{
int to,nex;
ll val;
}a[maxn];
int n,m;
int head[maxn],cnt;
void add(int fro,int to,ll val)
{
a[++cnt].to=to;
a[cnt].val=val;
a[cnt].nex=head[fro];
head[fro]=cnt;
}
ll d[maxn];
void bfs(int x)
{
priority_queue<PLI,deque<PLI >,greater<PLI > >q;
q.push(mk(0,x));
bool ju=0;
while(!q.empty())
{
PLI p=q.top();q.pop();
int cur=p.se;
ll val=p.fi;
if(cur==x&&val)
{
plld(val);
return;
}
// cout<<cur<<' '<<val<<endl;
for(int i=head[cur];i;i=a[i].nex)
{
int to=a[i].to;
// cout<<to<<' '<<a[i].val<<' '<<d[to]<<endl;
if(val+a[i].val<d[to])
{
d[to]=val+a[i].val;
q.push(mk(d[to],to));
// if(to==x)ju=1;
}
}
}
puts("-1");
}
void solve()
{
sdd(n,m);
rep(i,1,m)
{
int u,v;
ll w;
sdd(u,v);
slld(w);
add(u,v,w);
}
rep(i,1,n)
{
memset(d,INF,sizeof d);
bfs(i);
}
}
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);
int T=1;
// sd(T);
while(T--)solve();
}
|
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <climits>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <bits/stdc++.h>
using namespace std;
#define lson 2*i
#define rson 2*i+1
#define LS l,mid,lson
#define RS mid+1,r,rson
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define ll long long
#define MAX 1000005
#define MOD 1000000007
#define EXP 1e-8
#define lowbit(x) (x&-x)
// const int INF = 0x1fffffffffffffff;
const int INF = INT_MAX;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
void chmin(ll& a, ll b){ if(a > b) a = b; }
void chmax(ll& a, ll b){ if(a < b) a = b; }
int main () {
int n, m; cin >> n >> m;
int x, y, z;
vector<vector<pair<int, int>>> cs(n+1);
for (int i = 0; i < m; ++i){
cin >> x >> y >> z;
cs[x-1].emplace_back(y, z);
}
vector<ll> dp(1 << n);
dp[0] = 1;
queue<int> q;
unordered_set<int> hash;
q.push(0);
int pos = 0;
while (!q.empty()) {
int size = q.size();
while (size--) {
int state = q.front(); q.pop();
for (int i = 0; i < n; ++i) {
int num = (1 << i);
if (state & num) continue;
int newState = state | num;
bool flag = true;
for (auto &it : cs[pos]) {
y = it.first, z = it.second;
int mask = (1 << y) - 1;
if (__builtin_popcount(newState & mask) > z) {
flag = false; break;
}
}
if (flag) dp[newState] += dp[state];
// cout << state << ' ' << newState << ' ' << dp[newState] << endl;
if (!hash.count(newState))
q.push(newState), hash.insert(newState);
}
}
// cout << pos <<endl;
pos++;
}
cout << dp[(1<<n)-1] << 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;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define cool ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define int long long int
#define pb push_back
#define fe first
#define lb lower_bound
#define ub upper_bound
#define pii pair<pair<int,int>,pair<int,int> >
#define se second
#define endl "\n"
#define pi pair<int, int>
#define mi map<int,int>
#define mii map<pi,int>
#define vi vector<int>
#define vvi vector<vi>
#define bs binary_search
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rep1(i,a,b) for(int i=a;i<=b;i++)
#define all(c) (c).begin(),(c).end()
#define sz(x) (int)x.size()
#define PI 3.14159265358979323846
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> o_set;
const int N=3e5+10;
const int INF=1e18;
int mod= 1e9+7;
int dx[4]={0,0,+1,-1};
int dy[4]={+1,-1,0,0};
int po(int a, int b) {
int res = 1;
while (b > 0) {
if (b & 1)
res = (res%mod * a%mod) % mod;
a =(a%mod*a%mod) % mod;
b >>= 1;
}
return res;
}
int gcdExtended(int a,int b,int* x,int* y) {
if(a==0) {
*x=0,*y=1;
return b;
}
int x1,y1;
int g=gcdExtended(b%a,a,&x1,&y1);
*x=y1-(b/a)*x1;
*y=x1;
return g;
}
int modInverse(int a,int m) {
int x,y;
int g=gcdExtended(a,m,&x,&y);
int res=(x%m+m)%m;
return res;
}
void solve() {
int n,s,k;
cin>>n>>s>>k;
int g=__gcd(n,k);
int lt=n-s;
if((lt%g)!=0) {
cout<<-1<<endl;
return;
}
n/=g;
k/=g;
lt/=g;
int yo=modInverse(k,n);
int ans=((lt)*yo)%n;
cout<<ans<<endl;
}
int32_t main() {
cool;
int t=1;
//~ freopen("input.txt","r",stdin);
//~ freopen("output.txt","w",stdout);
cin>>t;
while(t--)
solve();
return 0;
}
|
/* Jai Shree Ganesh
Jai Mata Di
Jai Shree Ram
Jai Shree Krishna
Har Har Mahadev
Jai Bajrangbali
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<ll,ll> pll;
long int INF = 1000000005;
#define lab(i,a,b) for(i=a;i<=b;i++)
#define bal(i,a,b) for(i=b;i>=a;i--)
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define po pop_back
#define se(v) v.begin(), v.end()
#define fst ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define tc ll t;cin>>t;while(t--)
#define fceil(a,b) ceil((ld)a/(ld)b)
#define dis(v) for(auto it:v)cout<<it<<" "
#define yep cout<<"YES\n"
#define nope cout<<"NO\n"
bool isPrime(ll n){if(n <= 1)return false;if(n <= 3)return true;if(n%2==0||n%3==0)return false;for(ll i=5;i*i<= n;i=i+6)if(n%i==0||n%(i+2)==0)return false;return true;}
bool cmp(pll &a, pll &b){ if(a.ss == b.ss)return a.ff < b.ff;return a.ss < b.ss;}
int main()
{
fst
ll n,i,f=0,j,x,y,z;cin>>x>>y;
n=y-x;
while(n>1)
{
z=y/n;z--;
f=z*n;
if(f>=x)break;
n--;
}
cout<<n;
return 0;
} | #include <bits/stdc++.h>
//#define rep(i,a,n) for (int i=a;i<n;i++)
#define overload4(_1, _2, _3, _4, name, ...) name
#define rep1(n) for(ll i = 0; i < (n); ++i)
#define rep2(i, n) for(ll i = 0; i < (n); ++i)
#define rep3(i, a, b) for(ll i = (a); i < (b); ++i)
#define rep4(i, a, b, c) for(ll i = (a); i < (b); i += (c))
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define forn(i, n) for(int i = 0 ; (i) < (n) ; ++i)
#define rrep(i,a,n) for (int i=n-1;i>=a;i--)
#define ALL(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pause "read -p 'Press Enter to continue...' var"
using namespace std;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
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; }
/*
テスト通りますように
●
/⌒ヽ
| |/⌒ヽ(ヽ
(` ∥ー⌒) |
| ̄|| ̄ ̄ ̄ ̄ ̄|
|―||―――――|
| U |
| ̄ ̄ ̄ ̄ ̄ ̄ ̄|
|_______|
|―――――|
|―――――|
wwWwwWwWWw
*/
typedef long long ll;
typedef vector<ll> vll;
typedef pair<ll,ll> pll;
const ll INF = numeric_limits<ll>::max()/4;
const ll MAX = 1000'000'000;
const int MOD = 998244353;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
ll para[3005][5];
ll n;
ll mask(ll i, ll x){
ll res = 0;
rep(j,5){
if(para[i][j] >= x) res += 1 << j;
}
return res;
}
bool check(ll x){
int msk[1 << 5] = {0};
rep(i,n){
msk[mask(i,x)]++;
}
rep(i,1<<5){
rep(j,1<<5){
rep(k,1<<5){
if((i | j | k) == ((1<<5) - 1) && msk[i] > 0 && msk[j] > 0 && msk[k] > 0 && msk[i] + msk[j] + msk[k] >= 3){
return true;
}
}
}
}
return false;
}
int main(){
cin >> n;
rep(i,n){
rep(j,5){
cin >> para[i][j];
}
}
ll right = INF;
ll left = 0;
while((left + 1) != right){
ll md = (right + left) / 2;
if(check(md)) left = md;
else right = md;
}
cout << left;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define sz(v) (int)v.size()
#define precision cout << fixed << setprecision(15);
const int inf = 1e9;
const long long INF = 1e18;
const int mod = 1e9 + 7;
const int bit32 = log2(inf) + 3;
const int bit64 = log2(INF) + 3;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
clock_t time_p = clock();
void ktj() {
time_p = clock() - time_p;
cerr << "Time elapsed : " << (float)(time_p)/CLOCKS_PER_SEC << "\n";
}
void pre() {}
const int N = 2e5 + 5;
int a[N], b[N], order[N];
int bit[N];
void update(int idx, int val){
for(; idx <= N; idx += (idx & (-idx))){
bit[idx] += val;
}
}
int query(int idx){
int sum = 0;
for(; idx > 0; idx -= (idx & (-idx))){
sum += bit[idx];
}
return sum;
}
string to_string(string s) { return '"' + s + '"';}
string to_string(char s) { return string(1, s);}
string to_string(const char* s) { return to_string((string) s);}
string to_string(bool b) { return (b ? "true" : "false");}
template <typename A> string to_string(A);
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) {
bool f = 1; string r = "{";
for (const auto &x : v) {
if (!f)r += ", "; f = 0; r += to_string(x);
}
return r + "}";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H); debug_out(T...);
}
#define pr(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
void solve() {
int h, w, m, i, j, k;
cin >> h >> w >> m;
for (i = 1; i <= h; i++)
a[i] = w;
for (i = 1; i <= w; i++)
b[i] = h;
for (i = 0; i < m; i++) {
int x, y;
cin >> x >> y;
a[x] = min(a[x], y - 1);
b[y] = min(b[y], x - 1);
}
long long ans = 0;
for (i = 1; i <= b[1]; i++) {
ans += a[i];
}
for (i = 1; i <= a[1]; i++) {
order[i] = i;
}
sort(order + 1, order + a[1] + 1, [&](int x, int y) {
return b[x] < b[y];
});
order[0] = 0;
b[0] = 0;
for (i = 1; i <= a[1]; i++) {
for (j = b[order[i - 1]] + 1; j <= b[order[i]] and j <= b[1]; j++) {
update(a[j], 1);
}
ans += b[order[i]] - (query(N) - query(order[i] - 1));
}
cout << ans << '\n';
}
#define GOOGLE 0
#define MULTIPLE_TC 0
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
pre();
int t = 1, T;
if (MULTIPLE_TC)
cin >> t;
for (T = 1; T <= t; T++) {
if (GOOGLE)
cout << "Case #" << T << ": ";
solve();
}
ktj();
}
| #include <bits/stdc++.h>
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll I=1167167167167167167;
ll Q=1e9+7;
#define rep(i,a) for (ll i=0;i<a;i++)
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end());reverse(v.begin(),v.end());}
template<class T> void print_tate(vector<T> &v) {rep(i,v.size()) cout<<v[i]<<"\n";}
void yneos(bool a){if(a) cout<<"Yes"<<"\n"; else cout<<"No"<<"\n";}
int N=50,M=0;
int start_x,start_y;
vector<vector<int>> tile(N,vector<int>(N));
vector<vector<int>> wrote_point(N,vector<int>(N));
vector<int> point;
int start_tile;
vector<int> vari_x={0,0,-1,1},vari_y={-1,1,0,0};
struct date
{
string route;
int x;
int y;
int now_point;
set<int> arrived;
int rate;
bool operator<(const date& another) const {
//メンバ変数であるnum1で比較した結果を
//この構造体の比較とする
return now_point < another.now_point;
}
bool operator>(const date& another) const {
//メンバ変数であるnum1で比較した結果を
//この構造体の比較とする
return now_point > another.now_point;
}
};
int value(int a){
return a;
}
bool check(int x,int y){
return (0<=x&&x<N&&0<=y&&y<N);
}
date add(date a,int x,int y){//add_checkを通過していること
if(x-a.x==1) a.route+="D";
else if(x-a.x==-1) a.route+="U";
else if(y-a.y==1) a.route+="R";
else a.route+="L";
a.arrived.insert(tile[x][y]);
a.now_point+=point[tile[x][y]];
a.x=x;
a.y=y;
return a;
}
bool add_check(date &a,int X,int Y){
if(!check(X,Y)) return false;
if(a.arrived.count(tile[X][Y])) return false;
if(abs(a.x-X)+abs(a.y-Y)!=1) return false;
return true;
}
void solve(){
int range=30;
date ans={"",start_x,start_y,point[start_tile],{start_tile},point[start_tile]};
vector<date> looking={ans};
while(looking.size()!=0&&clock()<1.9*CLOCKS_PER_SEC){
vector<date> next_looking;
ans=looking[0];
rep(i,min(range,(int)looking.size())){
rep(j,4){
int s=looking[i].x+vari_x[j],t=looking[i].y+vari_y[j];
if(add_check(looking[i],s,t)){
next_looking.push_back(add(looking[i],s,t));
}
}
}
if(next_looking.size()==0) break;
Sore(next_looking);
looking=next_looking;
}
cout<<looking[0].route<<endl;
}
//main関数だよーーー
int main() {
cin>>start_x>>start_y;
rep(i,N) rep(j,N) cin>>tile[i][j],chmax(M,tile[i][j]);
rep(i,N) rep(j,N) cin>>wrote_point[i][j];
M++;
point.resize(M);
rep(i,N) rep(j,N) point[tile[i][j]]+=wrote_point[i][j];
start_tile=tile[start_x][start_y];
solve();
}
|
#include <iostream>
#include <cmath>
using namespace std;
long long int b[100005];
double c[100005];
int main()
{
int n,a,ma=-100008;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a);
b[i]=b[i-1]+abs(a);//1
c[i]=c[i-1]+pow(a,2);//2
ma=max(ma,abs(a));
}
printf("%lld\n",b[n]);
printf("%.15lf\n",sqrt(c[n]));
printf("%d",ma);
return 0;
} | #include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
cout<<fixed<<setprecision(15);
int n;
cin>>n;
vector<int>x(n);
for(int i=0; i<n; i++)
cin>>x[i];
long ans1=0;
for(int i=0; i<n; i++)
ans1+=abs(x[i]);
cout<<ans1<<endl;
double ans2=0;
for(int i=0; i<n; i++)
ans2+=((double)abs(x[i])*(double)abs(x[i]));
cout<<sqrt(ans2)<<endl;
int ans3=0;
for(int i=0; i<n; i++)
ans3 = max(ans3, abs(x[i]));
cout<<ans3<<endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ll long long
#define pb push_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define S(x) (int)(x).size()
#define L(x) (int)(x).length()
#define ld long double
#define mem(x,y) memset(x,y,sizeof x)
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int , null_type , less<int> , rb_tree_tag , tree_order_statistics_node_update> ordered_set;
const int mod = 1e9+7;
const ll infl = 0x3f3f3f3f3f3f3f3fLL;
const int infi = 0x3f3f3f3f;
int a[200009];
void solve()
{
int n,m;
cin>>n>>m;
for(int i=0;i<m;i++) cin>>a[i];
sort(a,a+m);
int sz=infi;
int pre=1;
vector<int> segs;
for(int i=0;i<m;)
{
int j=i;
while(j<m && a[j]-a[i]==j-i) j++;
if(pre!=a[i]) sz=min(sz,a[i]-pre),segs.pb(a[i]-pre);
pre=a[j-1]+1;
i=j;
}
if(pre!=n+1) sz=min(sz,n+1-pre),segs.pb(n+1-pre);
int ans=0;
//cout<<sz<<'\n';
for(auto u:segs) ans+=(u+sz-1)/sz;
cout<<ans<<'\n';
}
int main()
{
IOS
int t=1;
//cin>>t;
while(t--)
{
solve();
}
}
| #include<bits/stdc++.h>
#define lint long long int
#define rep(i,n) for(int i=0;i<int(n);i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define arep(i,a,n) for(int i=a;i<n;i++)
#define sort(a) sort(a.begin(),a.end())
#define reverse(a) reverse(a.begin(),a.end())
#define fill(a,x) fill(a.begin(),a.end(),x)
#define eb(data) emplace_back(data)
#define pb(data) emplace_back(data)
#define mp make_pair
#define ALNUM 26
#define vint vector<int>
#define vlint vector<lint>
#define F first
#define S second
#define ALL(data) data.begin(),data.end()
#define GEts(s) getline(cin,s);
#define UNIQUE(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
using namespace std;
template<typename Rast>inline void out(Rast rast){cout<<rast<<"\n";return;}
template<typename Rast>inline void in(Rast& rast){cin>>rast;return;}
template<typename T>istream& operator >> (istream& is, vector<T>& vec){for(T& x: vec) is >> x;return is;}
template<typename First, typename... Rest>void in(First& first, Rest&... rest){cin >> first;in(rest...);return;}
template<typename First, typename... Rest>void out(First first, Rest... rest){cout << first<<" ";out(rest...);return;}
template<typename T>T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}
template<typename T>T lcm(T a,T b){return a * b / gcd(a, b);}
template<typename T1,typename T2>bool chmax(T1& a,T2 b){if(a<b){a=b;return true;}else{return false;}}
template<typename T1,typename T2>bool chmin(T1& a,T2 b){if(a>b){a=b;return true;}else{return false;}}
static const double pi = 3.141592653589793;
int modpow(int a,int n,int p){if (n==0)return 1%p; if(n==1)return a%p;if(n%2==1)return (a*modpow(a,n-1,p))%p;lint t=modpow(a,n/2,p);return (t*t)%p;}//a^n%p
lint MOD=1e9+7;
//lint MOD=998244353;
lint inf=pow(2,50);
int intinf=pow(2,30);
/**int dirx[]={1,0};int diry[]={0,1};//*///右、下
/**int dirx[]={0,1,0,-1};int diry[]={-1,0,1,0};//*///四方位
/**int dirx[]={-1,0,1,1,1,0,-1,-1};int diry[]={-1,-1,-1,0,1,1,1,0};//*///八方位
class unionfind{
public:
vector<int> table;
void init(int size){
table.resize(size);
rep(i,size)table[i]=i;
};
int root(int index){
if(table[index]==index)return index;
else{
int hoge=root(table[index]);
table[index]=hoge;
return hoge;
}
};
bool same(int x,int y){
return(root(x)==root(y));
};
int marge(int x,int y){
int yroot=root(y);
int xroot=root(x);
if(xroot==yroot)return 0;
table[yroot]=xroot;
return 0;
}
int getsize(){
set<int> ma;
rep(i,table.size())ma.insert(root(i));
int re=ma.size();
return re;
}
};
int main(){
//cin.tie(0);ios::sync_with_stdio(false);cout<<std::fixed<<std::setprecision(16);
lint n;
int m;
in(n,m);
if(n==m){
out(0);
return 0;
}
if(m==0){
out(1);
return 0;
}
vlint a(m);
in(a);
a.pb(0);
a.pb(n+1);
sort(a);
vlint b;
arep(i,1,a.size()){
if((a[i]-a[i-1])<=1)continue;
b.pb(a[i]-a[i-1]-1);
}
lint ans=b[0];
rep(i,b.size())ans=min(ans,b[i]);
lint sum=0;
rep(i,b.size())sum+=(b[i]+ans-1)/ans;
out(sum);
return 0;
}
|
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define LL long long
#define int long long
#define ull unsigned long long
#define fi first
#define se second
#define pll pair<LL, LL>
#define pii pair<int, int>
#define fastio ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define SZ(x) ((int)(x.size()))
#define LC (id<<1)
#define RC (id<<1|1)
constexpr int N = 5e5+9;
constexpr int M = 998244353;
#ifdef int
constexpr int INF = 0x3f3f3f3f3f3f3f3f;
constexpr int INF2 = 0xcfcfcfcfcfcfcfcf;
#else
constexpr int INF = 0x3f3f3f3f;
constexpr int INF2 = 0xcfcfcfcf;
#endif
signed main() {
fastio;
int n;
cin >> n;
int lcm = 1;
for (int i = 1; i <= n; i++) {
lcm = lcm / __gcd(lcm, i) * i;
}
cout << lcm+1 << "\n";
return 0;
} | #include<iostream>
#include <numeric>
using namespace std;
int main(){
int N;
long long ans;
cin >> N;
if(N==2)cout << "3" << endl;
if(N==3)cout << "7" << endl;
if(N==4)cout << "25" << endl;
if(N==5)cout << "121" << endl;
if(N==6)cout << "721" << endl;
if(N==7)cout << "5041" << endl;
if(N==8)cout << "40321" << endl;
if(N==9)cout << "362881" << endl;
if(N==10)cout << "3628801" << endl;
if(N==11)cout << "39916801" << endl;
if(N==12)cout << "479001601" << endl;
if(N==13)cout << "6227020801" << endl;
if(N==14)cout << "360361" << endl;
if(N==15)cout << "360361" << endl;
if(N==16)cout << "720721" << endl;
if(N==17)cout << "12252241" << endl;
if(N==18)cout << "12252241" << endl;
if(N==19)cout << "232792561" << endl;
if(N==20)cout << "232792561" << endl;
if(N==21)cout << "232792561" << endl;
if(N==22)cout << "232792561" << endl;
if(N==23)cout << "5354228881" << endl;
if(N==24)cout << "5354228881" << endl;
if(N==25)cout << "26771144401" << endl;
if(N==26)cout << "26771144401" << endl;
if(N==27)cout << "80313433201" << endl;
if(N==28)cout << "80313433201" << endl;
if(N==29){
ans=80313433200*29+1;
cout << ans << endl;
}
if(N==30){
ans=80313433200*29+1;
cout << ans << endl;
}
return 0;
} |
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<cmath>
#include<map>
#include<random>
#include<iomanip>
#include<queue>
#include<stack>
#include<assert.h>
#include<time.h>
#define int long long
#define double long double
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,n) for(int i=1;i<=n;i++)
#define ggr getchar();getchar();return 0;
#define prique priority_queue
constexpr auto mod = 1000000007;
#define inf 1e15
#define key 1e9
using namespace std;
typedef pair<int, int>P;
template<class T> inline void chmax(T& a, T b) {
a = std::max(a, b);
}
template<class T> inline void chmin(T& a, T b) {
a = std::min(a, b);
}
//combination(Nが小さい時はこれを使う
const int MAX = 2330000;
int 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;
}
}
int COMB(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;
}
bool prime(int n) {
int cnt = 0;
for (int i = 1; i <= sqrt(n); i++) {
if (n % i == 0)cnt++;
}
if (cnt != 1)return false;
else return n != 1;
}
int gcd(int x, int y) {
if (y == 0)return x;
return gcd(y, x % y);
}
int lcm(int x, int y) {
return x / gcd(x, y) * y;
}
//繰り返し二乗法(Nが大きい時の場合のcombination)
int mod_pow(int x, int y, int m) {
int res = 1;
while (y) {
if (y & 1) {
res = res * x % m;
}
x = x * x % m;
y >>= 1;
}
return res;
}
int kai(int x, int y) {
int res = 1;
for (int i = x - y + 1; i <= x; i++) {
res *= (i % mod); res %= mod;
}
return res;
}
int comb(int x, int y) {
if (y > x)return 0;
return kai(x, y) * mod_pow(kai(y, y), mod - 2, mod) % mod;
}
//UnionFind
class UnionFind {
protected:
int* par, * rank, * size;
public:
UnionFind(unsigned int size) {
par = new int[size];
rank = new int[size];
this->size = new int[size];
rep(i, size) {
par[i] = i;
rank[i] = 0;
this->size[i] = 1;
}
}
int find(int n) {
if (par[n] == n)return n;
return par[n] = find(par[n]);
}
void unite(int n, int m) {
n = find(n);
m = find(m);
if (n == m)return;
if (rank[n] < rank[m]) {
par[n] = m;
size[m] += size[n];
}
else {
par[m] = n;
size[n] += size[m];
if (rank[n] == rank[m])rank[n]++;
}
}
bool same(int n, int m) {
return find(n) == find(m);
}
int getsize(int n) {
return size[find(n)];
}
};
int dight(int n) {
int ans = 1;
while (n >= 10) {
n /= 10;
ans++;
}
return ans;
}
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 n, t;
int a[44];
int memo1[1050000], memo2[1050000];
signed main() {
cin >> n >> t;
rep(i, n)cin >> a[i];
rep(i, 1 << (n / 2)) {
int cnt = 0;
rep(j, n / 2) {
if ((i >> j) & 1)cnt += a[j];
}
memo1[i] = cnt;
}
rep(i, 1 << (n - n / 2)) {
int cnt = 0;
rep(j, n - n / 2) {
if ((i >> j) & 1)cnt += a[j + n / 2];
}
memo2[i] = cnt;
}
sort(memo1, memo1 + (1 << (n / 2)));
sort(memo2, memo2 + (1 << (n - n / 2)));
int ans = 0;
rep(i, 1 << (n / 2)) {
int p = upper_bound(memo2, memo2 + (1 << (n - n / 2)), t - memo1[i]) - memo2;
if (p == 0)ans = ans;
else ans = max(ans, memo1[i] + memo2[p - 1]);
}
cout << ans << endl;
ggr
}
| #include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
#define debug(a) cout << #a << ": " << a << endl
using namespace std;
typedef long long ll;
const int N = 50;
int n;
ll T, a[N];
vector<ll> v;
ll get(ll x){
int p = upper_bound(v.begin(), v.end(), x) - v.begin();
if (p == 0) return 0;
return v[p-1];
}
int main(void){
scanf("%d%lld", &n, &T);
for (int i = 1; i <= n; ++i){
scanf("%lld", &a[i]);
}
int m = n / 2, k = n - m;
for (int i = 0; i < (1<<m); ++i){
ll s = 0;
for (int j = 0; j < m; ++j){
if ((i >> j) & 1){
s += a[j+1];
}
}
v.push_back(s);
}
sort(v.begin(), v.end());
// debug(v.size());
// debug(m);
// debug(k);
ll ans = 0;
for (int i = 0; i < (1<<k); ++i){
ll s = 0;
for (int j = 0; j < k; ++j){
if ((i >> j) & 1){
s += a[m+j+1];
}
}
//debug(s);
if (s <= T) ans = max(ans, s + get(T-s));
}
printf("%lld\n", ans);
return 0;
}
|
Subsets and Splits