code_file1
stringlengths 87
4k
| code_file2
stringlengths 85
4k
|
---|---|
//Coded by Abhijay Mitra (AbJ)
#include "bits/stdc++.h"
#define double long double
#define int long long int
#define ibs ios_base::sync_with_stdio(false)
#define cti cin.tie(0)
#define mp make_pair
#define pb emplace_back
#define F first
#define S second
using vi = std::vector<int>;
using vvi = std::vector<vi>;
using pii = std::pair<int, int>;
using vpii = std::vector<pii>;
using vvpii = std::vector<vpii>;
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, b, a) for (int i = b; i >= a; i--)
#define all(x) x.begin(), x.end()
using namespace std;
const int inf = 1e18 + 10;
const int N = 2e6 + 10;
int n, a[N];
void solve(){
cin >> n;
rep(i, 1LL, n){
cin >> a[i];
}
vi v;
if(n == 1LL){
}
else if(n == 2LL){
if(a[1LL] == 2LL){
v.pb(1LL);
swap(a[1LL], a[2LL]);
}
}
else{
rep(i, 1LL, n - 3LL){
rep(j, i + 1, n){
if(a[j]==i){
if((((int)v.size() ) & 1) != (j & 1)){
if(((int)v.size() & 1) == (n & 1)){
v.pb(n - 1LL);
swap(a[n - 1LL], a[n]);
}
else{
v.pb(n - 2LL);
swap(a[n - 2LL], a[n - 1LL]);
}
}
per(pos, j - 1, i){
v.pb(pos);
swap(a[pos], a[pos + 1LL]);
}
}
}
}
}
while(a[n] != n or a[n - 1LL] != n - 1LL or a[n - 2LL] != n - 2LL){
if(((int)v.size() & 1LL) == (n & 1LL)){
v.pb(n - 1LL);
swap(a[n - 1LL], a[n]);
}
else{
v.pb(n - 2LL);
swap(a[n - 2LL], a[n - 1LL]);
}
}
assert((int)v.size() <= n * n);
assert(is_sorted(a + 1LL, a + 1LL + n));
cout << v.size() << "\n";
for(auto &i: v){
cout << i << " ";
}
}
int32_t main(){
ibs;cti;
// solve(); return 0;
int t; cin >> t;
while (t--){
solve(); cout << "\n";
}
return 0;
} | #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pint = pair<int,int>;
void solve();
int main()
{
srand((unsigned)time(NULL));
int T;
cin >> T;
while(T--)solve();
}
template<class T> ostream& operator<<(ostream& os, const vector<T> arr){ for(int i = 0; i < (int)arr.size(); i++)cout << arr[i] << (i == (int)arr.size() -1 ? "" : " "); return os;}
// #include"debug.hpp"
void solve()
{
int N; cin >> N;
vector<int> a(N);
for(auto &i : a) cin >> i;
int sel = 0;
vector<int> ans;
vector<int> moto = a;
ST:
while(is_sorted(a.begin(), a.end()) == false) {
// print(a);
vector<tuple<int,int,int>> p;
for(int i = sel; i < N - 1; i += 2) {
p.push_back({a[i], a[i + 1], i});
}
sel = !sel;
bool ok = false;
for(auto &[u, v, c] : p) {
if(u > v) {
ans.push_back(c + 1);
swap(a[c], a[c + 1]);
ok = true;
break;
}
}
if(ok)continue;
int v = rand() % (int)p.size();
ans.push_back(get<2>(p[v]) + 1);
swap(a[get<2>(p[v])], a[get<2>(p[v]) + 1]);
}
if(ans.size() > N * N) {
a = moto;
ans.resize(0);
sel = 0;
goto ST;
}
cout << ans.size() << endl;
cout << ans << endl;
}
/*
1 10
1
6
9
2
8
5
3
4
10
7
*/ |
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod = 1000000007LL;
int main(void){
int N, M;
cin >> N >> M;
vector<ll> H(N), W(M);
for(int i = 0; i < N; i++) cin >> H[i];
sort(H.begin(), H.end());
for(int i = 0; i < M; i++) cin >> W[i];
sort(W.begin(), W.end());
vector<ll> L(N, 0), R(N, 0);
for(int i = 1; i < N - 1; i += 2){
L[i + 1] = L[i - 1] + H[i] - H[i - 1];
}
for(int i = N - 2; i > 0; i -= 2){
R[i - 1] = R[i + 1] + H[i + 1] - H[i];
}
// for(int i = 0; i < N; i++) cout << L[i] << "\t";
// cout << endl;
// for(int i = 0; i < N; i++) cout << R[i] << "\t";
// cout << endl;
ll ans = 1000000000000000000LL;
for(int i = 0; i < M; i++){
auto itr = lower_bound(H.begin(), H.end(), W[i]);
int idx = distance(H.begin(), itr);
idx = 2 * (idx / 2);
ll sub = abs(W[i] - H[idx]);
sub += L[idx] + R[idx];
ans = min(sub, ans);
// if(sub == 30) cout << i << endl;
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define dump(...)
#endif
#define endl '\n'
#define ll long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define INF 2147483647
#define LLINF 9223372036854775807LL
#define vi vector<int>
#define vvi vector<vector<int>>
#define pi pair<int, int>
#define pll pair<long long, long long>
#define ALL(a) (a).begin(), (a).end()
#define BIT(n) (1LL << (n))
#define TOUPPER(s) transform(s.begin(), s.end(), s.begin(), ::toupper)
#define TOLOWER(s) transform(s.begin(), s.end(), s.begin(), ::tolower)
#define FILLZERO(s, n) \
do { \
ostringstream os; \
os << setw(n) << setfill('0') << s; \
s = os.str(); \
} while (0);
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) //sortしてからつかうこと
#define INIT \
std::cin.tie(nullptr); \
std::ios_base::sync_with_stdio(false); \
cout << std::fixed << std::setprecision(10);
template <typename T>
vector<T> make_vec(size_t a, T n) { return vector<T>(a, n); }
template <typename T, typename... Ts>
auto make_vec(size_t a, Ts... ts) { return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...)); }
template <class T>
inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
for (T &x : vec)
is >> x;
return is;
}
constexpr ll MOD = 1e9 + 7;
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
ll ans = 0;
signed main() {
INIT;
cin >> N >> W;
if (N > 40)
return 0;
vector<ll> v(N), w(N);
REP(i, N) {
cin >> v[i]; // >> w[i];
w[i] = v[i];
}
vector<pair<ll, ll>> wv1(1);
vector<pair<ll, ll>> wv2(1);
auto merge = [](vector<pll> &wv, pll a) {
auto [w, v] = a;
vector<pll> res;
int y = 0;
REP(x, wv.size()) {
while (wv[y].first + w < wv[x].first) {
res.push_back({wv[y].first + w, wv[y].second + v});
y++;
}
res.push_back(wv[x]);
}
FOR(i, y, wv.size()) {
res.push_back({wv[i].first + w, wv[i].second + v});
}
return res;
};
REP(i, N) {
wv1 = merge(wv1, {w[i], v[i]});
swap(wv1, wv2);
}
auto f = [](vector<pll> &wv) {
ll now = -1;
vector<pair<ll, ll>> res;
REP(i, wv.size()) {
if (chmax(now, wv[i].second)) {
res.push_back(wv[i]);
}
}
return res;
};
wv1 = f(wv1);
wv2 = f(wv2);
dump(wv1, wv2);
int ind = wv2.size() - 1;
for (auto [w1, v1] : wv1) {
if (w1 > W)
break;
while (w1 + wv2[ind].first > W) {
ind--;
}
chmax(ans, v1 + wv2[ind].second);
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
long decimalToOctal(long n) {
int remainder;
long octal = 0, i = 1;
while(n != 0) {
remainder = n%8;
n = n/8;
octal = octal + (remainder*i);
i = i*10;
}
return octal;
}
int main() {
ll n;
cin>>n;
ll ans=0;
set<ll> s;
for (ll i = 1; i <= n; i++)
{
ans=0;
int tem=i;
while (tem!=0)
{
ll digit=tem%10;
if(digit==7){
ans++;
s.insert(i);
// cout<<i<<endl;
break;
}
tem/=10;
if(ans!=0){
}
}
ll oc=decimalToOctal(i);
ans=0;
while (oc!=0)
{
ll digit=oc%10;
if(digit==7){
ans++;
s.insert(i);
// cout<<i<<endl;
break;
}
oc/=10;
}
//cout<<decimalToOctal(i)<<endl;
}
cout<<n-s.size()<<endl;
} | #include <bits/stdc++.h>
#define rep(i,n) for(ll i=0;i<(n);i++)
#define rrep(i,n) for(ll i = 1; i <= (n); ++i)
#define drep(i,n) for(ll i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define all(v) v.begin(),v.end()
#define len(x) (ll)(x).length()
#define maxs(x,y) x = max(x,y)
#define mins(x,y) x = min(x,y)
#define pb push_back
#define pf push_front
#define sz(x) (ll)(x).size()
#define v(T) vector<T>
#define vv(T) vector<vector<T>>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vd> vvd;
typedef vector<P> vp;
ll gcd(ll a,ll b){if(a%b==0){return b;}else{return(gcd(b,a%b));}}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
const int INF=1e9;
const ll MX = 1e18;
const ll mod=INF+7;
const int di[] = {-1,0,1,0};
const int dj[] = {0,-1,0,1};
const double PI=acos(-1);
const string tr="abcdefghijklmnopqrstuvwxyz";
#define dame { puts("-1"); return 0;}
#define yn {puts("Yes");}else{puts("No");}
#define YN {puts("YES");}else{puts("NO");}
ll llpow(ll n,ll i){if(i==0){return 1;}ll cnt=n;for(ll j=0;j<i-1;j++){n*=cnt;}return n;}
bool ip/*is_prime*/(long long N) {if (N == 1) return false;for (long long i = 2; i * i <= N; ++i) {if (N % i == 0) return false;}return true;}
int digit(ll N) {int ans = 0;while (N) {++ans;N /= 10;}return ans;}
vector<pair<ll,ll>> pf/*prime_factorize*/(ll n){vector<pair<ll,ll>> res;for(ll a=2;a*a<=n;a++){if(n%a!=0) continue;ll ex=0;while(n%a==0){ex++;n/=a;}res.pb({a,ex});}if(n!=1) res.pb({n,1});return res;}
vector<ll> div/*divisor*/(ll n){ vector<ll> res={1};for(ll a=2;a*a<=n;a++){if(n%a!=0) continue;ll b=n/a;res.pb(b);if(b!=a) res.pb(a);}sort(all(res));return res;}
struct mint {
ll x; // typedef long long ll;
mint(ll x=0):x((x%mod+mod)%mod){}
mint operator-() const { return mint(-x);}
mint& operator+=(const mint a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint a) {
if ((x += mod-a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
mint operator+(const mint a) const { return mint(*this) += a;}
mint operator-(const mint a) const { return mint(*this) -= a;}
mint operator*(const mint a) const { return mint(*this) *= a;}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t>>1);
a *= a;
if (t&1) a *= *this;
return a;
}
// for prime mod
mint inv() const { return pow(mod-2);}
mint& operator/=(const mint a) { return *this *= a.inv();}
mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
struct UnionFind {
vector<int> d;
UnionFind(int n): d(n,-1) {}
int root(int x) {
if (d[x] < 0) return x;
return d[x] = root(d[x]);
}
bool unite(int x, int y) {
x = root(x); y = root(y);
if (x == y) return false;
if (d[x] > d[y]) swap(x,y);
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) { return root(x) == root(y);}
int size(int x) { return -d[root(x)];}
};
mint f2(ll n) {if (n == 0) return 1;mint x = f2(n/2);x *= x;if (n%2 == 1) x *= 2;return x;}
mint choose(int n,int a){mint x=1,y=1;rep(i,a){x*=n-i;y*=i+1;}return x/y;}
ll xs/*xorsum nまでのxorの和*/(ll n){ll cnt=(n+1)/2;ll ans=cnt%2;if(n%2==0) ans^=n;return ans;}
ll Fa/*Factorial nの階乗*/(ll n){ll ans=1;rrep(i,n){ans*=i;}return ans;}
int main() {
int n;cin>>n;
double ans=0.0;
for(int i=n-1;i>=1;i--){
ans+=double(n)/double(i);
}
printf("%.10f\n",ans);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int a,b;
cin>>a>>b;
if(b<a){
long long sum=0;
long long bum=0;
for(int i=1;i<=a;i++){
cout<<i<<" ";
sum=sum+i;
}
for(int i=1;i<=b-1;i++){
cout<<(-1)*i<<" ";
bum=bum+i;
}
cout<<bum-sum;
}
else{
long long sum=0;
long long bum=0;
for(int i=1000000;i<a+1000000;i++){
cout<<i<<" ";
sum=sum+i;
}
for(int i=1;i<=b-1;i++){
cout<<(-1)*i<<" ";
bum=bum+i;
}
cout<<bum-sum;
}
return 0;
} | #include <iostream>
using namespace std;
int mtf,mf;
int sum;
int main()
{
cin>>mtf>>mf;
sum = mtf+mf;
if(sum>=15&&mf>=8)
cout<<1;
else
if(sum>=10&&mf>=3)
cout<<2;
else
if(sum>=3)
cout<<3;
else
cout<<4;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> edges[26];
vector<pair<int, int>> states[1024][1024];
int d[1024][1024];
int main()
{
int N, M;
scanf("%d %d", &N, &M);
queue<pair<int, int>> q;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
d[i][j] = 1'000'000'000;
}
d[i][i] = 0; q.emplace(i, i);
}
for (int i = 0; i < M; i++) {
int u, v; char c[2];
scanf("%d %d %s", &u, &v, c);
if (u > v) swap(u, v);
edges[c[0] - 'a'].emplace_back(u, v);
if (u != v) d[u][v] = d[v][u] = 1;
q.emplace(u, v);
}
for (int a = 0; a < 26; a++) {
int sz = edges[a].size();
for (int i = 0; i < sz; i++) {
for (int j = i + 1; j < sz; j++) {
auto [u, v] = edges[a][i];
auto [w, x] = edges[a][j];
states[u][w].emplace_back(v, x); states[w][u].emplace_back(v, x);
states[u][x].emplace_back(v, w); states[x][u].emplace_back(v, w);
states[v][w].emplace_back(u, x); states[w][v].emplace_back(u, x);
states[v][x].emplace_back(u, w); states[x][v].emplace_back(u, w);
}
}
}
while (q.size()) {
auto [u, v] = q.front(); q.pop();
if (u > v) swap(u, v);
if (u == 1 && v == N) {
printf("%d\n", d[u][v]); return 0;
}
for (auto [u2, v2] : states[u][v]) {
if (d[u2][v2] == 1'000'000'000) {
d[u2][v2] = d[v2][u2] = d[u][v] + 2;
q.emplace(u2, v2);
}
}
}
printf("-1\n");
return 0;
} | #include <bits/stdc++.h>
using namespace std;
/*
#include <atcoder/all>
using namespace atcoder;
using mint = modint1000000007;
*/
#define all(x) (x).begin(),(x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define endl "\n"
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {os << "["; for (const auto &v : vec) {os << v << ","; } os << "]"; return os;}
template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) {os << "(" << p.first << ", " << p.second << ")"; return os;}
ll mod_pow(ll a, ll n, ll mod) { ll ret = 1; ll p = a % mod; while (n) { if (n & 1) ret = ret * p % mod; p = p * p % mod; n >>= 1; } return ret; }
const int mod = 1e9 + 7;
bool check(vector<vector<int>> &A, int K, int c) {
int N = A.size();
vector<vector<int>> B(N, vector<int>(N));
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
B[i][j] = (A[i][j] <= c);
}
}
vector<vector<int>> acc(N + 1, vector<int>(N + 1));
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
acc[i][j] = acc[i - 1][j] + acc[i][j - 1] - acc[i - 1][j - 1] + B[i - 1][j - 1];
}
}
for (int i = 0; i <= N - K; i++) {
for (int j = 0; j <= N - K; j++) {
int sum = acc[i + K][j + K] - acc[i][j + K] - acc[i + K][j] + acc[i][j];
if (sum >= (K * K + 1) / 2) return true;
}
}
return false;
}
void solve() {
int N, K;
cin >> N >> K;
vector<vector<int>> A(N, vector<int>(N));
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cin >> A[i][j];
}
}
int ok = 1e9 + 1, ng = -1;
while(ok - ng > 1) {
int c = (ok + ng) / 2;
if (check(A, K, c)) {
ok = c;
} else {
ng = c;
}
}
cout << ok << endl;
}
int main() {
#ifdef LOCAL_ENV
cin.exceptions(ios::failbit);
#endif
cin.tie(0);
ios::sync_with_stdio(false);
cout.setf(ios::fixed);
cout.precision(16);
solve();
} |
#include<bits/stdc++.h>
#define endl '\n'
#define QAQ(m, n) for(ll i = m;i <= n;i++)
#define QWQ(m, n) for(ll j = m;j <= n;j++)
#define FOR(k, m, n) for(ll k = m;k <= n;k++)
#define uQAQ(n, m) for(ll i = n;i >= m;--i)
#define uQWQ(n, m) for(ll j = n;j >= m;--j)
#define uFOR(k, n, m) for(ll k = n;k >= m;--k)
#define ALL(a) a.begin(), a.end()
#define all(a, n) a + 1, a + n + 1
#define fin(a) freopen(a, "r", stdin)
#define fout(a) freopen(a, "w", stdout)
#define pb push_back
#define mkp std::make_pair
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
using namespace std;
ll n, m;
string s[801];
int main ()
{
std::cin.tie (0);
std::ios::sync_with_stdio (false);
cin >> n >> m;
QAQ (1, m)
cin >> s[i];
sort (all (s, n), [](string a, string b) { return a.size () > b.size (); });
ll now = 1;
QAQ (1, n)
{
ll t = n;
while (t > (ll) s[now].size ())
cout << s[now], t -= s[now++].size ();
while (t > 0)
cout << '.', --t;
cout << endl;
}
return 0;
} | #include<bits/stdc++.h>
using namespace std;
using ll = long long int;
using ld = long double;
#define pow(n,m) powl(n,m)
#define sqrt(n) sqrtl(n)
const ll MAX = 5000000000000000000;
const ll MOD = 0;
random_device rd;
mt19937 mt(rd());
int main(){
ll N,M;
cin >> N >> M;
vector<string> S(M),ans(N),T;
set<string> ss;
ll m = MAX;
for(ll i = 0;i < M;i++) cin >> S[i];
for(ll i = 0;i < M;i++) ss.insert(S[i]);
for(ll i = 0;i < M;i++) m = min(m,(ll)S[i].size());
for(ll t = 0;t < 35000;t++){
string a = S[mt() % M];
for(ll i = 0;i < 10;i++){
string b = S[mt() % M];
if(a.size() + b.size() <= N){
a += b;
}
}
while(a.size() < N) a += (char)('A' + (mt() % 8));
T.emplace_back(a);
}
vector<ll> aaaa;
do{
for(ll j = 0;j < T.size();j++){
set<string> c;
for(ll k = 0;k < N;k++){
string p = "";
for(ll l = 0;l < m;l++){
p += T[j][(k + l) % N];
}
for(ll l = m;l < m + 5;l++){
p += T[j][(k + l) % N];
if(ss.count(p) != 0) c.insert(p);
}
}
aaaa.emplace_back(c.size());
}
} while(false);
sort(aaaa.begin(),aaaa.end(),greater<ll>());
for(ll i = 0;i < N;i++){
ll C = 0;
string A = "____________________";
for(ll j = 0;j < T.size();j++){
set<string> c;
for(ll k = 0;k < N;k++){
string p = "";
for(ll l = 0;l < m;l++){
p += T[j][(k + l) % N];
}
for(ll l = m;l < m + 5;l++){
p += T[j][(k + l) % N];
if(ss.count(p) != 0) c.insert(p);
}
}
if(C < c.size()){
C = c.size();
A = T[j];
}
if(i == 0 && aaaa[1000] > c.size()){
T.erase(T.begin() + j);
j--;
}
}
for(ll k = 0;k < N;k++){
string p = "";
for(ll l = 0;l < m;l++){
p += A[(k + l) % N];
}
for(ll l = m;l < m + 5;l++){
p += A[(k + l) % N];
if(ss.count(p) != 0) ss.erase(p);
}
}
ans[i] = A;
}
ll C = 0;
vector<string> ansans = ans,ansansans = ans;
if(m < 5){
for(ll t = 0;t < 1000;t++){
swap(ans[mt() % N],ans[mt() % N]);
for(ll i = 0;i < N;i++){
ll qqq = mt() % N;
for(ll j = 0;j < N;j++) ansans[j][i] = ans[i][(j + qqq) % N];
}
set<string> c;
for(ll i = 0;i < N;i++){
for(ll k = 0;k < N;k++){
string p = "";
for(ll l = 0;l < m;l++){
p += ansans[i][(k + l) % N];
}
for(ll l = m;l < m + 5;l++){
p += ansans[i][(k + l) % N];
if(ss.count(p) != 0) c.insert(p);
}
}
}
if(C < c.size()){
C = c.size();
ansansans = ansans;
}
}
}
//cout<<C<<endl;
for(ll i = 0;i < N;i++) cout << ansansans[i] << endl;
}
|
//---------------------------------------------------------JAI HIND------------------------------------------------------------------------
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define all(a) a.begin(), a.end()
#define Summon_Tourist ios::sync_with_stdio(false); cin.tie(0);
ll inf = 998244353;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll extgcd(ll a, ll b, ll *x, ll *y) { if (b == 0){*x = 0;*y = 1;return a;}ll x1, y1;ll g = extgcd(b, a % b, &x1, &y1);*x = y1 - (a / b) * x1;*y = x1;return g; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll modexp(ll base, ll power){if (power == 0)return 1;if (power & 1)return base * modexp(base, power - 1) % inf;return modexp(base * base % inf, power / 2); }
ll modinv(ll a, ll mod){ ll x, y;ll g = extgcd(a, mod, &x, &y); if (g != 1) return 0; return (y + mod) % mod; }
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
int main()
{
Summon_Tourist
ll t = 1;
//cin>>t;
while (t--)
{
int a,b; cin>>a>>b;
int which = 1;
if( b<a ){
int z = b;
b = a,a = z;
which = 0;
}
int avg = b/a,rem = b%a;
rem = a - rem;
vector<int> av,bv;
int count = 1;
for( int i = 0 ; i<a; i++ ){
int correspond = 0;
for( int j = 0 ; j<avg; j++ ){
bv.emplace_back(count);
correspond += count;
++count;
}
if( rem-- <= 0 ){
bv.emplace_back(count);
correspond += count;
++count;
}
av.emplace_back(correspond);
correspond = 0;
}
for( int i = 0 ; i<a; i++ ){
if( which == 1 ) cout<<av[i]<<" ";
else cout<<-av[i]<<" ";
}
for( int i = 0 ; i<b; i++ ){
if( which == 1 ) cout<<-bv[i]<<" ";
else cout<<bv[i]<<" ";
}
}
return 0;
}
//----------------------------------------------------------------------------------------------------------------------------------------------------
| #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 4000000000000000000 //オーバーフローに注意
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;
#define SIZE 2005
ll A,B;
ll table[SIZE];
int main(){
scanf("%lld %lld",&A,&B);
ll sum = 0;
for(ll i = 0; i < A; i++){
table[i] = i+1;
sum += table[i];
}
for(ll i = 0; i < B; i++){
table[A+i] = -(i+1);
sum += table[A+i];
}
if(sum > 0){
table[A+B-1] -= sum;
}else if(sum < 0){
table[A-1] += abs(sum);
}
printf("%lld",table[0]);
for(ll i = 1; i < A+B; i++){
printf(" %lld",table[i]);
}
printf("\n");
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define DD double
#define Pb push_back
#define Bp pop_back
#define Pf push_front
#define Fp pop_front
#define Ub upper_bound
#define Lb lower_bound
#define Bs binary_search
#define In insert
#define Mp make_pair
#define All(x) x.begin(), x.end()
#define mem(a, b) memset(a, b, sizeof(a))
#define fast ios_base::sync_with_stdio(0);cin.tie(0)
#define X first
#define Y second
const int mx1 = 20;
const int mx2 = 505;
const int mx3 = 1005;
const int mx4 = 30005;
const int mx5 = 100005;
const int mx6 = 1000005;
typedef vector<int> Vi;
typedef vector<DD> Vd;
typedef vector<bool> Vb;
typedef vector<Vi> VVi;
typedef pair<int, int> Pii;
typedef pair<DD, DD> Pdd;
typedef vector<Pii> Vpi;
typedef vector<Pdd> Vpd;
typedef queue<int> Qi;
typedef stack<int> Si;
typedef deque<int> Di;
int _toggle(int N, int pos) {return N = N ^ (1 << pos);}
int _set(int N, int pos) {return N = N | (1 << pos);}
int _reset(int N, int pos) {return N = N & ~(1 << pos);}
bool _check(int N, int pos) {return (bool)(N & (1 << pos));}
bool _upper(char a) {return a >= 'A' && a <= 'Z';}
bool _lower(char a) {return a >= 'a' && a <= 'z';}
bool _digit(char a) {return a >= '0' && a <= '9';}
int dx[] = {1, -1, 0, 0, -1, -1, 1, 1};
int dy[] = {0, 0, 1, -1, -1, 1, -1, 1};
///******************************************************///
int n, a[mx3], b[mx3];
void solve()
{
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i] >> b[i];
}
int ans = 1e9;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
if(i == j){
ans = min(ans, a[i] + b[j]);
}
else{
ans = min(ans, max(a[i], b[j]));
}
}
}
cout << ans << '\n';
}
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
fast;
int tc = 1;
//cin >> tc;
while(tc--){
solve();
}
}
| #include "bits/stdc++.h"
using namespace std;
#define mod 1000000007
#define pie 3.14159265
#define ll long long int
#define fr(i,a,n) for(ll i=a;i<n;i++)
#define vi vector<int>
#define vlli vector<ll>
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define fv(i,a) for(auto i:a)
#define sz(a) int((a).size())
#define all(c) (c).begin(),(c).end()
#define frv(i,c) for(auto i = c.begin(); i != c.end(); i++)
void solve()
{
ll n;
cin >> n;
vi a(n);
ll s = 0;
fr(i, 0, n)
{
cin >> a[i];
s += a[i];
}
vi dp(s + 1, 0);
dp[0] = 1;
ll ans = 1e9;
fr (i, 0, n)
{
for (ll j = s - a[i]; j >= 0; j--)
{
if (dp[j] == 1)
{
dp[j + a[i]] = 1;
ll x = s - (j + a[i]);
ans = min(ans, max(x, j + a[i]));
}
}
}
cout << ans << endl;
}
int main() {
solve();
return 0;
} |
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
using namespace std;
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr);
#else
#define eprintf(...) 42
#endif
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
template<typename T>
using pair2 = pair<T, T>;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
return (ull)rng() % B;
}
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
clock_t startTime;
double getCurrentTime() {
return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}
const ll MOD = 998244353;
ll add(ll x, ll y) {
x += y;
if (x >= MOD) return x - MOD;
return x;
}
ll sub(ll x, ll y) {
x -= y;
if (x < 0) return x + MOD;
return x;
}
ll mult(ll x, ll y) {
return (x * y) % MOD;
}
ll bin_pow(ll x, ll p) {
if (p == 0) return 1;
if (p & 1) return mult(x, bin_pow(x, p - 1));
return bin_pow(mult(x, x), p / 2);
}
ll rev(ll x) {
return bin_pow(x, MOD - 2);
}
const int N = 5050;
int n, m;
int pw[N][N];
int main()
{
startTime = clock();
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
for (int x = 0; x < N; x++) {
pw[x][0] = 1;
for (int i = 1; i < N; i++)
pw[x][i] = mult(pw[x][i - 1], x);
}
scanf("%d%d", &n, &m);
int ans = 0;
for (int i = 0; i < n; i++)
for (int x = 1; x <= m; x++) {
ans = add(ans, mult(pw[m - x][i], pw[m][n - 1 - i]));
}
for (int d = 1; d < n; d++)
for (int x = 1; x <= m; x++) {
ans = add(ans, mult(mult(n - d, x - 1), mult(pw[m - x][d - 1], pw[m][n - 1 - d])));
}
printf("%d\n", ans);
return 0;
}
| // Problem: C - Sequence Scores
// Contest: AtCoder - AtCoder Regular Contest 114
// URL: https://atcoder.jp/contests/arc114/tasks/arc114_c
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
//愿神 zhoukangyang 指引我。
#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
const int p=998244353;
int Q[5003][5003];
int qp(int x,int y)
{
return Q[x][y];
}
signed main()
{
for(int i=1; i<=5000; i++)
{
Q[i][0]=1;
for(int j=1; j<=5000; j++) Q[i][j]=Q[i][j-1]*i%p;
}
int n=read(),m=read(),ans=0;
for(int i=1; i<=n-2; i++) for(int j=1; j<=m; j++)
ans+=(j-1)*(j-1)*(n-i-1)%p*(p+qp(m-j+1,i)-qp(m-j,i))%p*qp(m,n-i-2)%p,ans%=p;
for(int r=1; r<=n; r++) for(int i=1; i<=m; i++)
{
int t=1,k=n-r;
if(r^n) t=1ll*t*(i-1)%p,k--;
ans+=1ll*t*(p+qp(m-i+1,r)-qp(m-i,r))%p*qp(m,k)%p,
ans%=p;
}
for(int l=2;l<=n;l++) for(int i=1;i<=m;i++)
{
ans+=1ll*(i-1)*(p+qp(m-i+1,n-l+1)-qp(m-i,n-l+1))%p*qp(m,l-2)%p,
ans%=p;
}
printf("%lld\n",ans);
return 0;
} |
#include <iostream>
#include <stdio.h>
#include <cstdio>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <cstring>
#include <math.h>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
int n,di1,di2,di;
long long t,ans;
long long a[50];
long long sum1[100000006],s,sum2[100000006],sum[10000006];
void ready()
{
ios::sync_with_stdio(false),cin.tie(0);
cin>>n>>t;
for(int i=1;i<=n;i++)
{
cin>>a[i];
s+=a[i];
}
}
void work()
{
for(int i=1;i<=n/2;i++)
{
sum1[++di1]=a[i];
for(int j=di1-1;j>=1;j--)
if(a[i]+sum1[j]<=t)
sum1[++di1]=a[i]+sum1[j];
}
sort(sum1+1,sum1+di1+1);
for(int i=n/2+1;i<=n;i++)
{
sum2[++di2]=a[i];
for(int j=di2-1;j>=1;j--)
if(a[i]+sum2[j]<=t)
sum2[++di2]=a[i]+sum2[j];
}
for(int i=1;i<=di2;i++)
{
int l=0,r=di1,mid=(l+r)/2;
while(l<=r)
{
mid=(l+r)/2;
if(sum2[i]+sum1[mid]<=t)
l=mid+1;
else
r=mid-1;
}
if(sum1[(l+r)/2]+sum2[i]<=t)
ans=max(ans,sum1[(l+r)/2]+sum2[i]);
}
cout<<ans;
}
void work1()
{
if(s<=t)
{
cout<<s;
return ;
}
for(int i=1;i<=n;i++)
{
sum[++di]=a[i];
for(int j=di-1;j>=1;j--)
if(a[i]+sum[j]<=t)
sum[++di]=a[i]+sum[j];
}
sort(sum+1,sum+di+1);
for(int i=di;i>=0;i--)
if(sum[i]<=t)
{
cout<<sum[i];
return ;
}
}
int main()
{
ready();
if(n>20)
work();
else
work1();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define FOR(i, n) for(int i = 0; i < (int) n; i++)
#define PB push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define F first
#define S second
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
const int MOD = 1e9+7;
int main(){
ios::sync_with_stdio(false); cin.tie(0);
ll n, t; cin >> n >> t;
vi v(n); FOR(i, n) cin >> v[i];
vector<ll> v1, v2;
for(int i = 0; i < (1<<(n/2)); i++){
ll aux = 0;
for(int j = 0; j < n/2; j++){
if(i&(1<<j)){
aux += v[j];
}
}
v1.PB(aux);
}
for(int i = 0; i < (1<<((n+1)/2)); i++){
ll aux = 0;
for(int j = 0; j < (n+1)/2; j++){
if(i&(1<<j)){
aux += v[j+n/2];
}
}
v2.PB(aux);
}
sort(all(v2));
ll ans = 0;
FOR(i, v1.size()){
int p = distance(v2.begin(),
lower_bound(all(v2), t-v1[i]));
if(p == (int) v2.size() || v2[p] > t-v1[i]) p--;
if(p >= 0) ans = max(ans, v1[i] + v2[p]);
}
cout << ans << '\n';
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
const long long INF = 1000000000;
typedef long long ll;
#define rep(i,n) for(int i=0; i<(n); ++i)
typedef pair<ll, ll> P;
typedef pair<string, int> Psi;
ll index_max[1234567];
ll A[1234567];
ll ruiseki[1234567];
ll ruiseki_sum[1234567];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin>>n;
vector<pair<ll,ll>> A,B;
vector<pair<ll,pair<ll,ll>>> C;
for(ll i=0; i<n;i++){
ll a,b;cin>>a>>b;
A.push_back(P(a,i));
B.push_back(P(b,i));
}
sort(A.rbegin(), A.rend());
sort(B.rbegin(), B.rend());
C.push_back(make_pair(abs(A[0].first-A[n-1].first),P(A[0].second,A[n-1].second)));
C.push_back(make_pair(abs(A[1].first-A[n-1].first),P(A[1].second,A[n-1].second)));
C.push_back(make_pair(abs(A[0].first-A[n-2].first),P(A[0].second,A[n-2].second)));
C.push_back(make_pair(abs(B[0].first-B[n-1].first),P(B[0].second,B[n-1].second)));
C.push_back(make_pair(abs(B[1].first-B[n-1].first),P(B[1].second,B[n-1].second)));
C.push_back(make_pair(abs(B[0].first-B[n-2].first),P(B[0].second,B[n-2].second)));
sort(C.rbegin(), C.rend());
if(C[0].second.first == C[1].second.first && C[0].second.second == C[1].second.second){
cout<<C[2].first<<endl;
}else{
cout<<C[1].first<<endl;
}
//
// for(auto x: A){
// cout<<x.second<<":"<<x.first<<" ";
// }
// cout<<endl;
// for(auto x: B){
// cout<<x.second<<":"<<x.first<<" ";
// }
// cout<<endl;
// for(auto x: C){
// cout<<x.second.first<<" "<<x.second.second<<":"<<x.first<<" ";
// }
// cout<<endl;
}
| #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 4000000000000000000 //オーバーフローに注意
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;
#define SIZE 200005
struct LOC{
ll x,y;
};
typedef vector<ll> V;
typedef vector<V> MATRIX;
int N = 3;
int num_P;
LOC loc[SIZE];
ll table[SIZE][3][3];
MATRIX calc(MATRIX left,MATRIX right){
MATRIX ret(N,V(N));
for(int i = 0; i < N; i++){
for(int k = 0; k < N; k++)ret[i][k] = 0;
}
for(int row = 0; row < N; row++){
for(int col = 0; col < N; col++){
for(int a = 0; a < N; a++){
ret[row][col] += left[row][a]*right[a][col];
}
}
}
return ret;
}
int main(){
scanf("%d",&num_P);
for(int i = 1; i <= num_P; i++){
scanf("%lld %lld",&loc[i].x,&loc[i].y);
}
int M;
scanf("%d",&M);
MATRIX A1(N,V(N));
A1[0][1] = 1;
A1[1][0] = -1;
A1[2][2] = 1;
MATRIX A2(N,V(N));
A2[0][1] = -1;
A2[1][0] = 1;
A2[2][2] = 1;
MATRIX AX(N,V(N));
AX[0][0] = -1,AX[0][1] = 0;
AX[1][0] = 0,AX[1][1] = 1;
AX[2][2] = 1;
MATRIX AY(N,V(N));
AY[0][0] = 1,AY[0][1] = 0;
AY[1][0] = 0,AY[1][1] = -1;
AY[2][2] = 1;
MATRIX MULT(N,V(N));
MULT[0][0] = 1,MULT[1][1] = 1,MULT[2][2] = 1;
int command;
ll p;
for(int row = 0; row < 3; row++){
for(int col = 0; col < 3; col++){
table[0][row][col] = MULT[row][col];
}
}
for(int i = 1; i <= M; i++){
scanf("%d",&command);
MATRIX work(N,V(N));
switch(command){
case 1:
MULT = calc(A1,MULT);
break;
case 2:
MULT = calc(A2,MULT);
break;
case 3:
scanf("%lld",&p);
for(int row = 0; row < 3; row++){
for(int col = 0; col < 3; col++){
work[row][col] = AX[row][col];
}
}
work[0][2] = 2*p;
MULT = calc(work,MULT);
break;
case 4:
scanf("%lld",&p);
for(int row = 0; row < 3; row++){
for(int col = 0; col < 3; col++){
work[row][col] = AY[row][col];
}
}
work[1][2] = 2*p;
MULT = calc(work,MULT);
break;
}
for(int row = 0; row < 3; row++){
for(int col = 0; col < 3; col++){
table[i][row][col] = MULT[row][col];
}
}
}
int Q;
scanf("%d",&Q);
int count,index;
for(int loop = 0; loop < Q; loop++){
scanf("%d %d",&count,&index);
ll x = loc[index].x;
ll y = loc[index].y;
ll ans_x = table[count][0][0]*x+table[count][0][1]*y+table[count][0][2];
ll ans_y = table[count][1][0]*x+table[count][1][1]*y+table[count][1][2];
/*printf("\n");
for(int row = 0; row < 3; row++){
for(int col = 0; col < 3; col++){
printf(" %lld",table[count][row][col]);
}
printf("\n");
}*/
printf("%lld %lld\n",ans_x,ans_y);
}
return 0;
}
|
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
int nt[200009], id[200009], ds[200009];
bool rm[200009];
queue<int> q;
int uf(int n)
{
if (ds[n] == n) return n;
return ds[n] = uf(ds[n]);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n; cin >> n;
for (int i = 1; i <= n; i++) {
cin >> nt[i];
id[nt[i]]++;
ds[i] = i;
}
for (int i = 1; i <= n; i++)
if (!id[i]) {
rm[i] = true;
q.push(i);
}
while (!q.empty()) {
int hn = q.front(); q.pop();
id[nt[hn]]--;
if (!id[nt[hn]]) {
rm[nt[hn]] = true;
q.push(nt[hn]);
}
}
for (int i = 1; i <= n; i++)
if (!rm[i]) {
int ua = uf(i), ub = uf(nt[i]);
if (ua != ub)
ds[ua] = ub;
}
int c = 0;
for (int i = 1; i <= n; i++)
if (!rm[i] && ds[i] == i)
c++;
long long ans = 1, md = 998244353;
for (int i = 0; i < c; i++)
ans = (ans * 2) % md;
ans = (ans - 1 + md) % md;
cout << ans << '\n';
return 0;
}
| #pragma GCC optimize ("O2")
#pragma GCC target ("avx")
#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please
const int 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;
if ((tmp & ma0) ^ ma0) {
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);
}
else {
tmp = tmp & ma1;
tmp = tmp * 10 + (tmp >> 8) & ma2;
tmp = tmp * 100 + (tmp >> 16) & ma3;
tmp = tmp * 10000 + (tmp >> 32) & ma4;
ci += 8;
if ((ct = *ci++) >= '0') {
tmp = tmp * 10 + ct - '0';
if ((ct = *ci++) >= '0') {
tmp = tmp * 10 + ct - '0';
ci++;
}
}
}
return tmp;
}
ll tmp[400010];
void pakuri_sort(int N, ll A[]) {
const int b = 8;
rep(k, 4) {
int kazu[1 << b] = {}, kazu2[1 << b] = {};
rep(i, N) kazu[A[i] >> k * b & ((1 << b) - 1)]++;
rep(i, (1 << b) - 1) kazu[i + 1] += kazu[i];
for (int i = N - 1; i >= 0; i--) tmp[--kazu[A[i] >> k * b & ((1 << b) - 1)]] = A[i];
k++;
rep(i, N) kazu2[tmp[i] >> k * b & ((1 << b) - 1)]++;
rep(i, (1 << b) - 1) kazu2[i + 1] += kazu2[i];
for (int i = N - 1; i >= 0; i--) A[--kazu2[tmp[i] >> k * b & ((1 << b) - 1)]] = tmp[i];
}
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N = getint(), M = getint();
ll XY[200000];
rep(i, M) {
int x = getint();
XY[i] = x | (ll)getint() << 32;
}
pakuri_sort(M, XY);
auto ST = tmp + 200000;
ST[0] = -1;
const ll ma = (1ll << 32) - 1;
int m = 0;
while (m < M) {
int m0 = m;
int x = (XY[m] & ma);
while (m < M && (XY[m] & ma) == x) {
int y = (XY[m] >> 32) - N;
if (y < -M || y > M) XY[m] = 0;
else if (ST[y - 1] < 0 | ST[y + 1] < 0) XY[m] |= 1ll;
else if (ST[y] < 0) XY[m] &= (~1ll);
else XY[m] = 0;
m++;
}
for (int p = m0; p < m; p++) {
int y = (XY[p] >> 32) - N;
if (XY[p] & 1) ST[y] = -1;
else if (XY[p]) ST[y] = 0;
}
}
int kotae = 0;
for (int i = -M; i <= M; i++) kotae += (ST[i] < 0);
co(kotae);
Would you please return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define PI acos(-1)
#define vi vector<int>
const int kMod = 1e9 + 7;
const int kInf = (~(1<<31)) / 2 - 500;
const ll kInfL = (~(1LL<<63)) / 2 - 2000;
int main() {
int N;
cin >> N;
vector<int> A(N), B(N);
for (int i = 0; i < N; ++i) {
cin >> A[i];
}
for (int i = 0; i < N; ++i) {
cin >> B[i];
}
vector<int> idx(N);
for (int i = 0; i < N; ++i) {
int tmp;
cin >> tmp;
--tmp;
++idx[tmp];
}
map<int, int> cnt;
for (int i = 0; i < N; ++i) {
cnt[B[i]] += idx[i];
}
ll ans = 0;
for (int i = 0; i < N; ++i) {
ans += cnt[A[i]];
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
const int modu = 998244353;
const double eps = 1e-5;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
typedef long long LL;
int n;
LL sum[maxn];
int main() {
// freopen("my.txt", "w", stdout);
cin >> n;
sum[0] = 0;
for (int i = 1; i <= n; ++i) {
scanf("%lld", &sum[i]);
sum[i] += sum[i-1];
}
LL ss = 0, ans = 0, incr = sum[1];
for (int i = 1; i <= n; ++i) {
ans = max(ans, ss+incr);
ss += sum[i];
if (i+1 <= n)
incr = max(incr, sum[i+1]);
}
cout << ans << endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i=0; i<(n);++i)
using ll = long long;
int main(){
double X,Y,Z,ans;
double price_per_gram;
cin >> X >> Y >> Z;
ans = 1000001.0;
price_per_gram = (double) (Y/X);
while (1){
if((double)(ans/Z)>=price_per_gram){
ans-=1.0;
}
else{
break;
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<ll, ll>;
using tp = tuple<ll, ll, ll>;
template <class T>
using vec = vector<T>;
template <class T>
using vvec = vector<vec<T>>;
#define all(hoge) (hoge).begin(), (hoge).end()
#define en '\n'
#define rep(i, m, n) for(ll i = (ll)(m); i < (ll)(n); ++i)
#define rep2(i, m, n) for(ll i = (ll)(n)-1; i >= (ll)(m); --i)
#define REP(i, n) rep(i, 0, n)
#define REP2(i, n) rep2(i, 0, n)
constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;
constexpr long long MOD = (ll)1e9 + 7;
//constexpr long long MOD = 998244353LL;
static const ld pi = 3.141592653589793L;
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
template <class T>
inline bool chmin(T &a, T b) {
if(a > b) {
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmax(T &a, T b) {
if(a < b) {
a = b;
return true;
}
return false;
}
//グラフ関連
struct Edge {
int to, rev;
ll cap;
Edge(int _to, ll _cap, int _rev) : to(_to), cap(_cap), rev(_rev) {}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
void add_edge(Graph &G, int from, int to, ll cap, bool revFlag, ll revCap) {
G[from].push_back(Edge(to, cap, (int)G[to].size()));
if(revFlag)
G[to].push_back(Edge(from, revCap, (int)G[from].size() - 1));
}
template <int mod>
struct ModInt {
int x;
ModInt() : x(0) {}
ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
ModInt &operator+=(const ModInt &p) {
if((x += p.x) >= mod)
x -= mod;
return *this;
}
ModInt &operator-=(const ModInt &p) {
if((x += mod - p.x) >= mod)
x -= mod;
return *this;
}
ModInt &operator*=(const ModInt &p) {
x = (int)(1LL * x * p.x % mod);
return *this;
}
ModInt &operator/=(const ModInt &p) {
*this *= p.inverse();
return *this;
}
ModInt operator-() const { return ModInt(-x); }
ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
bool operator==(const ModInt &p) const { return x == p.x; }
bool operator!=(const ModInt &p) const { return x != p.x; }
ModInt inverse() const {
int a = x, b = mod, u = 1, v = 0, t;
while(b > 0) {
t = a / b;
swap(a -= t * b, b);
swap(u -= t * v, v);
}
return ModInt(u);
}
ModInt pow(int64_t n) const {
ModInt ret(1), mul(x);
while(n > 0) {
if(n & 1)
ret *= mul;
mul *= mul;
n >>= 1;
}
return ret;
}
friend ostream &operator<<(ostream &os, const ModInt &p) {
return os << p.x;
}
friend istream &operator>>(istream &is, ModInt &a) {
int64_t t;
is >> t;
a = ModInt<mod>(t);
return (is);
}
static int get_mod() { return mod; }
};
using mint = ModInt<MOD>;
void solve() {
ll n, p;
cin >> n >> p;
cout << mint(p - 1) * mint(p - 2).pow(n - 1) << en;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// ll t;
// cin >> t;
// REP(i, t - 1) {
// solve();
// }
solve();
return 0;
}
|
//In the name of Allah (swt)
/*
T sort / plano ax
== [g o t c p]
// 我爱雨,花,食物,空气,水,都赞美真主 由 埃姆兰 制造
upper_bound = find the last positon where n could be inserted
lower_boudn = find the first position where n could be inserted
lambda = [](int x){return x<10;}
STL Algorithm Non- modifying----
--------------------------------
vector<int> vec = {9,60,90,8,45,87,90,69,55,7};
--- 1 counting
int n = count(vec.begin(),vec.end(),69); // 2
int m = count_if(vec.begin(),vec.end(),[](int x){return x<10;});//3 ->9,7,8
--- 2 Min and Max
itr = max_element(vec.begin(),vec.end()); // 90
//it returns the first max value
itr = max_element(vec.begin(),vec.end(),[](int x,int y){return (x%10)<(y%10);});// 9
// which number has the biggest last digit---
...Most Algorithms have a simple form and a generalized form
itr = min_element(vec.begin(),vec.end()); // 7
// generalized form : min_element()
pair_of_itr = minmax_element(vec.begin(),vec.end(),[](int x,int y){return (x%10)<(y%10);}); //60 69
...3 linear search (used when the data is not sorted
// return the first match
itr = find(vec.begin(),v.end(),
itr = find_if(vec.begin(),vec.end(),[](int x){return x>80;});
itr = find_if_not(vec.begin(),vec.end(),[](int x){return x>80;});
itr = search_n(vec.begin(),vec.end(),2,69);
//Generalized form: search_n();
//search subrange
vector<int> sub = {45,87,90};
itr = search(vec.begin(),vec.end(),sub.begin(),sub.end());
// search first subrange
itr = find_end(vec.begin(),vec.end(),sub.begin(),sub.end());
........// search last subrange
// generalized form: search(), fidn_end()
....Search any_of
vector<int> items = {87,69};
itr = find_first_of(vec.begin(),vec.end(),items.begin(),items.end());
// Search any one of the item in items
itr = find_first_of(vec.begin(),vec.end(),items.begin(),items.end(),[](int x,int y){return x==y*4});
// search any item in items that satisfy: x==y*4
// Search Adjacent
itr = adjacent_find(vec.begin(),vec.end()); // find two adjacent that are same
itr = adjacent_find(vec.begin(),vec.end(),[](int x,int y){return x==y*4}); // find two adjacent that are same
// find two adjacent items that satisfy : s==y*4
// 4
// 5 check Attributes
is_sorted(vec.begin(),vec.end());//check if vec is sorted
itr = is_sorted_until(vec.begin(),vec.end());
// itr points to first place to where elements are no longer sorted
// generalizedforms: is_sorted(), is_sorted_until()
is_partitioned(vec.begin(),vec.end())[](int x){return x>80;});
// check if vec is partitioned according to the condition of (x>80)
if_heap(vec.begin(),vec.end()); // check if vec is a heap
itr = is_heap_until(vec.begin(),vec.end());
// find the first place where it is no longer a heap
//
// All Any None
all_of(vec.begin(),vec.end(),[](int x){return x>80;});
// If any of vec is bigger than 80
any_of(vec.begin(),vec.end(),[](int x){return x>80});
//if any of vec is bigger than 80
none_of(vec.begin(),vec.end(),[](int x){return x>80;});
// if none of vec is bigger than 80
*/
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int long long
int temp[2005]={0};
vector<vector<int>>G;
void dfs(int v)
{
if(temp[v]==1)return;
temp[v]=1;
for(auto vv:G[v]){
dfs(vv);
}
}
void kick()
{
int n,m;
cin>>n>>m;
G.resize(n);
for (int i=0; i<m ; i++)
{
int x,y;
cin>>x>>y;
G[x-1].pb(y-1);
}
int ans = 0;
for (int i=0; i<n ; i++)
{
for(int j=0; j<n; j++)temp[j]=0;
//temp[i]=1;
dfs(i);
for (int j=0; j<n ; j++)
{
if(temp[j]==1)ans++;
}
}
cout<<ans<<endl;
}
int32_t main()
{
kick();
}
| #include<bits/stdc++.h>
#define int long long
#define ll int
#define itn int
#define ld long double
#define all(c) c.begin(),c.end()
#define vi vector<int>
#define vvi vector<vector<int>>
#define pii pair<int,int>
#define rep(i, n) for(int i = 0 ; i < n ; i++ )
#define repu(i, a, b) for(int i = a; i <=b; ++i)
#define repd(i, a, b) for(int i = a; i >=b; --i)
#define trav(a, x) for(auto& a : x)
#define sz(x) (int)(x).size()
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
using namespace std;
const long long MOD = 1000000007;
void dfs(int root, vvi &adj, vi& vis){
if(vis[root]) return;
vis[root]=1;
trav(x,adj[root]){
if(!vis[x]) dfs(x,adj,vis);
}
}
void solve()
{
int n, m;
cin >> n >> m;
vvi adj(n+1);
rep(i,m){
int x,y;
cin >> x >> y;
adj[x].pb(y);
}
int ans = 0;
repu(i,1,n){
vi vis(n+1,0);
dfs(i,adj,vis);
int tmp = 0;
trav(x, vis){
tmp+=x;
}
ans += tmp;
}
cout << ans << endl;
}
signed main(){
ios::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 t = 1;
// cin >> t;
rep(z,t){
solve();
}
cerr << "Time : " << (double)clock() / (double)CLOCKS_PER_SEC << "s\n";
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long N;
cin >> N;
long long A[N],P[N],X[N];
for (long i = 0; i < N; i++) {
cin >> A[i] >> P[i] >> X[i];
}
long long price = 2000000000;
for (long i = 0; i < N; i++) {
if (X[i] > A[i]) {
price = min(price,P[i]);
}
}
if (price == 2000000000) {
cout << -1 <<endl;
}else{
cout << price <<endl;
}
} | // Author : Chinmay JHa @chinmayajha
//
// Problem: B - Play Snuke
// Contest: AtCoder - Caddi Programming Contest 2021(AtCoder Beginner Contest 193)
// URL: https://atcoder.jp/contests/abc193/tasks/abc193_b
// Date / Time: 02-04-2021 18:58:24
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int t = 1;
// cin >> t;
while (t--) {
lli n;cin>>n;
vector<vector<lli>> a(n);
// vector<lli> d(n);
lli minn = 1e11;
for(lli i = 0;i<n;++i){
vector<lli> temp(3);
cin >> temp[0]>>temp[1]>>temp[2];
a[i] = temp;
}
sort(a.begin(),a.end());
for(lli i = 0;i < n;++i){
// minutes = a[i][0];
// price = a[i][1];
// stock = minutes*i + 0.5;
float stock = a[i][2] - a[i][0];
if(stock>=1){
minn = min(minn, a[i][1]);
// cout << a[i][1] << " ";
} else {
continue;
}
}
if(minn == 1e11){
cout << "-1\n";
} else {
cout << minn << "\n";
}
}
} |
#include <bits/stdc++.h>
using namespace std;
int main(){
vector<string> S(3);
for (auto &s: S)
cin >> s;
string C = "";
for (const auto &s: S)
C += s;
sort(C.begin(), C.end());
C.erase(unique(C.begin(), C.end()), C.end());
if (C.size() > 10){
puts("UNSOLVABLE");
return 0;
}
vector<char> A;
for (int i = 0; i < 10; i++)
A.push_back('0' + i);
do {
vector<string> T = S;
map<char, char> f;
for (int i = 0; i < C.size(); i++)
f[C[i]] = A[i];
for (int i = 0; i < 3; i++){
for (auto &t: T[i])
t = f[t];
}
vector<int64_t> B(3);
bool flag = true;
for (int i = 0; i < 3; i++){
B[i] = stoll(T[i]);
flag &= T[i] == to_string(B[i]);
flag &= B[i] > 0;
}
if (!flag)
continue;
if (B[0] + B[1] == B[2]){
for (const auto &b: B)
cout << b << endl;
return 0;
}
} while (next_permutation(A.begin(), A.end()));
puts("UNSOLVABLE");
} | #include "bits/stdc++.h"
#pragma region header
#define loop(n) for ([[maybe_unused]] size_t lpcnt_ = 0; lpcnt_ < (n); ++lpcnt_)
using i32 = int;
using i64 = long long int;
using u32 = unsigned int;
using u64 = unsigned long long int;
using isize = ptrdiff_t;
using usize = size_t;
template <class T, T Div = 2>
constexpr T infty = std::numeric_limits<T>::max() / Div;
constexpr i32 infi32 = infty<i32, 2>;
constexpr u32 infu32 = infty<u32, 2>;
constexpr i64 infi64 = infty<i64, 4>;
constexpr u64 infu64 = infty<u64, 4>;
constexpr usize operator"" _uz(u64 v) { return usize(v); }
inline u64 popcount(const u64 v) {
#ifdef _MSC_VER
return u64(__popcnt64(v));
#else
return u64(__builtin_popcount(v));
#endif
}
template <class T>
constexpr T difference(const T a, const T b) {
return a > b ? a - b : b - a;
}
template <class T>
constexpr T ceildiv(const T a, const T b) {
return (a + b - 1) / b;
}
class range {
struct range_iterator {
usize itr;
constexpr range_iterator(const usize pos) noexcept : itr(pos) {}
constexpr void operator++() noexcept { ++itr; }
constexpr bool operator!=(const range_iterator& other) const noexcept {
return itr != other.itr;
}
constexpr usize operator*() const noexcept { return itr; }
};
const range_iterator first, last;
public:
constexpr range(const usize first_, const usize last_) noexcept
: first(first_), last(last_) {}
constexpr range_iterator begin() const noexcept { return first; }
constexpr range_iterator end() const noexcept { return last; }
};
template <class F>
class rec_lambda {
F f;
public:
constexpr rec_lambda(F&& f_) : f(std::forward<F>(f_)) {}
template <class... Args>
constexpr auto operator()(Args&&... args) const {
return f(*this, std::forward<Args>(args)...);
}
};
#pragma endregion
void main_() {
std::array<i32, 3> A;
std::cin >> A[0] >> A[1] >> A[2];
std::sort(A.begin(), A.end());
if (A[1] - A[0] == A[2] - A[1])
std::cout << "Yes" << '\n';
else
std::cout << "No" << '\n';
// AtCoder Beginner Contest 201
}
int main() { main_(); } |
#include<bits/stdc++.h>
using namespace std;
int a[200000];
long long b[200000];
int Mod=1e9+7;
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
sort(a+1,a+1+n);
int tot=0;
for(int i=1;i<=n;i++)
if(a[i]!=a[i-1]) b[++tot]=a[i];
long long sum=1;
for(int i=1;i<=tot;i++)
{
sum=sum*(b[i]-b[i-1]+1)%Mod;
}
cout<<sum;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define ll long long
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(), v.end()
#define FOR(a, b, c) for (int(a) = (b); (a) < (c); ++(a))
#define FORN(a, b, c) for (int(a) = (b); (a) <= (c); ++(a))
#define FORD(a, b, c) for (int(a) = (b); (a) >= (c); --(a))
#define FOREACH(a, b) for (auto &(a) : (b))
#define REP(i, n) FOR(i, 0, n)
#define REPN(i, n) FORN(i, 1, n)
#define dbg(x) cout << (#x) << " is " << (x) << endl;
#define dbg2(x, y) cout << (#x) << " is " << (x) << " and " << (#y) << " is " << y << endl;
#define dbgarr(x, sz) \
for (int asdf = 0; asdf < (sz); asdf++) cout << x[asdf] << ' '; \
cout << endl;
#define dbgarr2(x, rose, colin) \
for (int asdf2 = 0; asdf2 < rose; asdf2++) { \
dbgarr(x[asdf2], colin); \
}
#define dbgitem(x) \
for (auto asdf = x.begin(); asdf != x.end(); asdf++) cout << *asdf << ' '; \
cout << endl;
const int mod = 1e9 + 7, MAXN = 1e5 + 1;
int n, A[MAXN];
struct Solution {
int solve() {
sort(A, A + n);
auto it = unique(A, A + n);
n = it - A;
long res = 1;
REP(i, n) {
res = (res * (A[i] - (i ? A[i - 1] : 0) + 1)) % mod;
}
return res;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
Solution test;
string s;
cin >> n;
REP(i, n)
cin >> A[i];
cout << test.solve() << endl;
} |
/*
Author- Aasav Badera
Time- Sat Oct 3 17:21:39 2020
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
using namespace __gnu_pbds;
#ifdef LOCAL
#include <sys/resource.h>
#define meminc rlimit rlim;if (getrlimit(RLIMIT_STACK, &rlim)) return 1;rlim.rlim_cur = 268435456;if (setrlimit(RLIMIT_STACK, &rlim)) return 2;
#else
#define meminc
#endif
#ifdef LOCAL
#define dbg(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
#else
#define dbg(args...)
#endif
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
err(++it, args...);
}
#define ll int
#define vi vector<int >
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// -------------------Standard Traversal Moves---------------------
// vi fx = {1 ,-1 ,0, 0}, fy = {0, 0, -1, 1};
// vi fx = {2, -2, 2, -2, 1, -1, 1, -1}, fy = {1, 1, -1, -1, 2, 2, -2, -2};
// vi fx = {1, 1, 1, -1, -1 , -1, 0, 0}, fy = {1, -1, 0, 1, -1, 0, 1, -1};
// ----------------------------------------------------------------
#define rep(i, a, b) for(int i=a;i<b;i++)
#define all(a) (a).begin(),(a).end()
#define sz(x) (int )x.size()
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define endl '\n'
// const int hell = (int)998244353;
const int hell = (int)1e9 + 7;
const long long int inf = 3223372036854775807;
const double PI = 3.14159265;
const int N = (int) 1e6 + 5;
int n, m, k, a[N];
int32_t main()
{
meminc;
ios::sync_with_stdio(false);
cin.tie(nullptr);
cerr.precision(10);
cout.precision(25);
cout << fixed;
#ifdef LOCAL
// for getting input from input.txt
freopen("input.txt", "r", stdin);
// for writing output to output.txt
freopen("output.txt", "w", stdout);
#endif
int tests = 1;
// cin >> tests;
rep(test, 1, tests+1)
{
cin >> n;
string s;
cin >> s;
long long int ans = 0;
int cntr[n][4] = {0};
map<char, int> m1;
m1['A'] = 0;
m1['T'] = 1;
m1['G'] = 2;
m1['C'] = 3;
rep(i, 0, n)
{
rep(j, 0, 4)
{
if(j == m1[s[i]])cntr[i][j] = 1;
else cntr[i][j] = 0;
}
if(i)
{
rep(j, 0 ,4)
{
cntr[i][j] += cntr[i - 1][j];
}
}
}
rep(i, 0, n)
{
rep(j, i, n)
{
int itm[4];
rep(l, 0 ,4)
{
itm[l] = cntr[j][l];
if(i)
{
itm[l] -= cntr[i - 1][l];
}
dbg(i, j, l, itm[l]);
}
ans += ((itm[0] == itm[1]) and (itm[3] == itm[2]));
}
}
cout << ans << endl;
}
#ifdef LOCAL
// cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
} | #include<bits/stdc++.h>
#define fl(i,a,n) for(int i=a;i<=n;i++)
#define pb push_back
#define int long long int
#define ump unordered_map
#define pii pair<int,int>
#define ff first
#define ss second
#define PI 3.1415926535898
#define inf 1e18
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define endl "\n"
#define print(a) for(auto x:a){cout<<x<<" ";}cout<<endl;
#define mod 1000000007
using namespace std;
template <typename Arg, typename... Args>
void db(Arg&& arg, Args&&... args) {
cout << std::forward<Arg>(arg);
using expander = int[];
(void)expander{0, (void(cout << ',' << std::forward<Args>(args)), 0)...}; cout << endl;
}
bool func(vector<vector<int>>&dp, int i, int rem, string &s, string &x) {
if (i == s.size()) {
return rem == 0;
}
if (dp[i][rem] != -1) {
return dp[i][rem];
}
bool f;
if (x[i] == 'A') {
f = (func(dp, i + 1, (rem * 10) % 7, s, x) and func(dp, i + 1, (rem * 10 + (s[i] - '0')) % 7, s, x));
}
else {
f = (func(dp, i + 1, (rem * 10) % 7, s, x) or func(dp, i + 1, (rem * 10 + (s[i] - '0')) % 7, s, x));
}
return dp[i][rem] = f;
}
inline void solve() {
int n;
cin >> n;
string s;
cin >> s;
string x;
cin >> x;
vector<vector<int>>dp(n, vector<int>(7, -1));
if (func(dp, 0, 0, s, x)) {
cout << "Takahashi";
}
else
{
cout << "Aoki";
}
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t = 1;
// cin >> t;
fl(i, 1, t)
{
solve();
}
return 0;
} |
#include <bits/stdc++.h>
#define all(a) a.begin(), a.end()
#define forn(i,n) for(int i = 0; i < (int) n; i++)
#define ios ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
typedef long long ll;
int n;
vector<int> w, maxL;
vector<pair<int,int>> seg;
ll mindist(vector<int>& o) {
ll ans = 0;
vector<ll> pos(n);
for(int i = 1; i < n; i++) {
int sumw = w[o[i]];
pos[i] = pos[i-1];
for(int st = i-1; st >= 0; st--){
sumw += w[o[st]];
// sumw = weight of cows [st, i]
auto lo = lower_bound(all(seg), pair<int,int>{sumw, -1});
if (lo == seg.begin()) continue;
int idx = (lo - seg.begin()) - 1;
pos[i] = max(pos[i], pos[st] + maxL[idx]);
}
}
return pos.back();
}
int main() {
ios;
int m;
cin >> n >> m;
w.resize(n);
int mxw = 0;
forn(i,n){
cin >> w[i];
mxw = max(mxw, w[i]);
}
//seg[i] = {v,l}
seg.resize(m);
forn(i,m) cin >> seg[i].second >> seg[i].first;
sort(all(seg));
maxL.resize(m);
forn(i,m){
maxL[i] = seg[i].second;
if (i) maxL[i] = max(maxL[i], maxL[i-1]);
}
if (seg[0].first < mxw) {
cout << "-1";
return 0;
}
vector<int> o(n);
iota(all(o), 0);
long long ans = 1ll << 60;
do{
ans = min(ans, mindist(o));
}while(next_permutation(all(o)));
cout << ans << "\n";
}
| #include <bits/stdc++.h>
using namespace std;
int main(){
#define int long long
int N,M,maxW=0,ans=0;
cin>>N>>M;
vector<int> w(N),l(M),v(M),p(N);
vector<pair<int,int>> rock(M);
for(int i=0;i<N;i++){
cin>>w.at(i);
maxW=max(maxW,w.at(i));
p.at(i)=i;
}
for(int i=0;i<M;i++){
cin>>l.at(i)>>v.at(i);
ans+=l.at(i);
if(maxW>v.at(i)){
cout<<-1<<endl;
return 0;
}
rock.at(i)={v.at(i),l.at(i)};
}
ans*=N;
sort(rock.begin(),rock.end());
unordered_map<int,int> minW;
for(int i=0;i<M;i++){
if(i==0){minW[rock.at(i).first+1]=rock.at(i).second;}
if(i>0){minW[rock.at(i).first+1]=max(rock.at(i).second,minW[rock.at(i-1).first+1]);}
}
sort(w.begin(),w.end());
sort(v.begin(),v.end());
do{
vector<int> d(N-1,0);
for(int i=0;i<N;i++){//右端
for(int j=0;j<=i;j++){//左端
int sumW=0,sumD=0;
for(int k=j;k<=i;k++){//sumD,sumWの計算
sumW+=w.at(k);
if(k<i){sumD+=d.at(k);}
}
int left=0,right=M-1,sumWW=-1;
while(left<=right){
int mid=left+(right-left)/2;
if(v.at(mid)+1<=sumW){
left=mid+1;
sumWW=max(sumWW,v.at(mid)+1);
}
else{right=mid-1;}
}
int stkj=minW[sumWW];//必要距離
if(stkj>sumD){d.at(i-1)+=stkj-sumD;}
}
}
int Qans=0;
for(int i=0;i<N-1;i++){Qans+=d.at(i);}
ans=min(ans,Qans);
}while(next_permutation(w.begin(),w.end()));
cout<<ans<<endl;
} |
#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"
using namespace std;
constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-12;
//int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N;
vector<long long int>v(N);
for (auto& i : v)cin >> i;
long long int mx = v[0];
auto w = v;
for (int i = 1; i < N; i++)w[i] += w[i - 1];
long long int ans = 0;
for (int i = 0; i < N; i++) {
ans += w[i];
mx = max(mx, v[i]);
cout << ans + mx * (i + 1) << endl;
}
} | #include <cstdio>
#include <cstring>
#include <cmath>
#include <cassert>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <iostream>
#include <numeric>
/* debug macros */
#ifdef WAFDAYO
#define DBG_LINE() {std::cerr<<"\e[2m[L"<<__LINE__<<"]\e[m ";}
#define DBG_PRINT(s,t,u) {std::cerr<<(s)<<" \e[2m=\e[m \e[1m"<<(t)<<"\e[m"<<(u);}
#define SELECT_7TH(x1,x2,x3,x4,x5,x6,x7,...) x7
#define dbg1(x1) DBG_PRINT(#x1,x1,std::endl)
#define dbg2(x1,x2) DBG_PRINT(#x1,x1,", ")dbg1(x2)
#define dbg3(x1,x2,x3) DBG_PRINT(#x1,x1,", ")dbg2(x2,x3)
#define dbg4(x1,x2,x3,x4) DBG_PRINT(#x1,x1,", ")dbg3(x2,x3,x4)
#define dbg5(x1,x2,x3,x4,x5) DBG_PRINT(#x1,x1,", ")dbg4(x2,x3,x4,x5)
#define dbg6(x1,x2,x3,x4,x5,x6) DBG_PRINT(#x1,x1,", ")dbg5(x2,x3,x4,x5,x6)
#define dbg(...) DBG_LINE()\
SELECT_7TH(__VA_ARGS__,dbg6,dbg5,dbg4,dbg3,dbg2,dbg1)(__VA_ARGS__)
#else
#define dbg(...) {}
#endif
/* utility functions */
struct read_item{read_item(){};
template<class T>operator T(){T t;std::cin>>t;return t;}};
char splf(int i,int n){return(i+1<n)?' ':'\n';};
template<bool up>
struct _RI{int i;_RI(int a):i(a){}
int operator*(){return i;}void operator++(){i+=(up?1:-1);}
bool operator!=(const _RI& r){return up?i<r.i:i>r.i;}};
template<bool up>
struct _RX{_RI<up> x,y;_RI<up> begin(){return x;}_RI<up> end(){return y;}
_RX(int a,int b):x(up?a:(b-1)),y(up?b:(a-1)){}_RX(int a):_RX(0,a){}};
typedef _RX<true> range;
typedef _RX<false> revrange;
/* types and constants */
typedef int64_t i64;
typedef uint64_t u64;
// const i64 inf = (i64)1.05e18;
// const int inf = (int)1.05e9;
using namespace std;
string rev(string x) {
string y;
for(auto c : x) {
y.push_back(c == 'A' ? 'B' : 'A');
}
return y;
}
vector<string> solve(const int N) {
if(N == 1) {
vector<string> ans;
ans.push_back("AB");
return ans;
}
auto prev = solve(N - 1);
vector<string> ans;
for(auto s : prev) {
ans.push_back(s + s);
}
for(auto s : prev) {
ans.push_back(s + rev(s));
}
string last;
for(auto i : range(1 << (N - 1))) {
last.push_back('A');
}
for(auto i : range(1 << (N - 1))) {
last.push_back('B');
}
ans.push_back(last);
return ans;
}
int main() {
const int N = read_item();
auto ans = solve(N);
printf("%d\n", (int)ans.size());
for(auto s : ans) {
printf("%s\n", s.c_str());
}
return 0;
}
/* wafdayo~~~ */
|
#pragma GCC optimize("O3", "unroll-loops")
#pragma GCC target("avx2")
#include <stdio.h>
constexpr int HeighestDim = 505000;
int n, m, k;
long long int res[101];
int dim_a = 0, dim_b = 0;
int A[HeighestDim + 1], B[HeighestDim];
void mult_a(int v){
for(int i = dim_a, j = i - v; j >= 0; --i, --j){ A[i] -= A[j]; if(A[i] < 0) A[i] += m; }
}
void div_a(int v){
for(int i = v, j = 0; i <= dim_a; ++i, ++j){
A[i] += A[j]; if(A[i] >= m) A[i] -= m;
}
}
void mult_b(int v){
dim_b += v;
for(int i = dim_b, j = i - v; j >= 0; --i, --j){
B[i] -= B[j]; if(B[i] < 0) B[i] += m;
}
}
void div_b(int v){
for(int i = v, j = 0; i <= dim_b; ++i, ++j){
B[i] += B[j]; if(B[i] >= m) B[i] -= m;
}
}
int len = 0;
char Buffer[20];
char Output[1500];
int main(void){
A[0] = 1; B[0] = 1;
scanf("%d%d%d", &n, &k, &m);
if(n == 1){ printf("%d\n", k); return 0; }
else if(n == 2){ printf("%d\n%d\n", k, k); return 0; }
else if(n == 3){
printf("%d\n%d\n%d\n", k, k * static_cast<long long int>(k + 2) % m, k);
return 0;
}
res[1] = k; res[n] = k; ++k;
const int hd = (n >> 1) + (n & 1);
const int nmt = n - 2;
{
int i, j;
for(i = 1, j = k; i <= hd; ++i, j += k){ dim_a += j; mult_a(j); div_a(i); }
for(; i <= nmt; ++i, j += k){ mult_a(j); div_a(i); }
mult_b(k); div_b(1);
}
for(int i = 2, j = n - 1, I = 2 * k, J = k * (n - 2); ; I += k, J -= k){
long long int res_v = 0;
for(int d = 0; d <= dim_b; ++d) res_v += static_cast<long long int>(A[d]) * B[d] % m;
if(res_v) res_v = (k * res_v - 1) % m; else res_v = m - 1;
res[i] = res_v; res[j--] = res_v;
if(i + 1 > j) break;
mult_a(j); div_a(J);
mult_b(I); div_b(i++);
}
for(int i = 1; i <= n; ++i){
if(res[i]){
int l = 0;
do { Buffer[l++] = res[i] % 10; res[i] /= 10; } while(res[i]);
while(l--) Output[len++] = Buffer[l] ^ '0';
}
else Output[len++] = '0';
Output[len++] = '\n';
}
fwrite(Output, 1, len, stdout);
return 0;
} | #include<bits/stdc++.h>
using namespace std;
const int MX = 6e5, K = 101;
int M;
int dp[K][2*MX];
int sum[2*MX];
int add(int a, int b) {
return a+b >= M ? a+b-M : a+b;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin>>n>>k>>M;
// n = 100, k = 100, M = 1e9;
dp[0][0] = 1;
for (int x=1; x<=n; x++) {
memset(sum, 0, sizeof sum);
for (int i=0; i<2*MX; i++) {
sum[i] = dp[x-1][i];
if (i>=x) sum[i] = add(sum[i], sum[i-x]);
}
for (int i=0; i<2*MX; i++) {
dp[x][i] = sum[i];
if (i>=(k+1)*x) dp[x][i] = add(sum[i], M-sum[i-(k+1)*x]);
}
}
for (int x=1; x<=n; x++) {
int A = n-x, B = x-1;
long long ans = 0;
for (int i=0; i<2*MX; i++) ans = (ans + 1LL*dp[A][i]*dp[B][i])%M;
ans = (ans * (k+1) + M - 1)%M;
cout<<ans<<endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
typedef pair<int,int> pp;
bool com(pp x,pp y){
if(x.ff==y.ff) return x.ss<y.ss;
return x.ff<y.ff;
}
ll power(ll x,ll y){
ll prod=1;
while(y){
if(y&1) prod=(prod*x)%mod;
x=(x*x)%mod;
y/=2;
}
return prod;
}
const int N=1e5+9;
void solve(){
int n;
cin>>n;
cout<<(n+99)/100<<"\n";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t=1;
//cin>>t;
while(t--)
solve();
return 0;
}
/*
*/ | #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
//#include <stack>
//#include <queue>
//#include <cstdio>
#include <cmath>
#include <iterator>
#include <map>
//#include <list>
#include <iomanip>
using namespace std;
#define IOS ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define prec std::cout << std::fixed << std::setprecision(15);
#define endl "\n"
#define MOD 1000000007
#define Int int64_t
#define PI 3.14159265358979
#define ssort(z) sort(z.begin(), z.end())
#define rsort(z) sort(z.rbegin(), z.rend())
#define eerase(z) z.erase(unique(z.begin(), z.end()), z.end())
#define ccnt(z, w) count(z.begin(), z.end(), w)
#define rep(i,a,n) for(Int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(Int (i)=(a); (i)<=(n); (i)++)
const int MAX_N = 1000000;
const Int MAX_N_Int = 1000000000000;
template <typename T>
void printV(const std::vector<T>& v, const char * const separator = " ")
{
if(!v.empty())
{
std::copy(v.begin(),
--v.end(),
std::ostream_iterator<T>(std::cout, separator));
std::cout << v.back() << "\n";
}
}
bool isPrime(int num)
{
if (num < 2) return false;
else if (num == 2) return true;
else if (num % 2 == 0) return false; // 偶数はあらかじめ除く
double sqrtNum = sqrt(num);
for (int i = 3; i <= sqrtNum; i += 2)
{
if (num % i == 0)
{
// 素数ではない
return false;
}
}
// 素数である
return true;
}
Int gcd(Int a, Int b) {
return b != 0 ? gcd(b, a % b) : a;
}
Int lcm(Int a, Int b) {
return a / gcd(a, b) * b;
//a*bは64bit integer overflow
}
int Max(int a, int b, int c) {
int temp = max(a, b);
return max(temp, c);
}
int Min(int a, int b, int c) {
int temp = min(a, b);
return min(temp, c);
}
bool integer(double num) {
return floor(num) == num;
}
Int fact(int num) {
if (num == 0)
return 1;
else
return num * fact(num - 1);
}
Int yakusu(int n) {
int cnt = 0;
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
cnt++;
if (i * i != n) cnt++;
}
}
return cnt;
}
Int fact_mod(Int n, Int mod) {
Int f = 1; repq(i,2,n) f = f * (i % MOD) % MOD;
return f;
}
// 繰り返し二乗法 (modの世界での累乗)
Int mod_pow(Int x, Int n, Int mod) {
Int res = 1;
while(n > 0) {
if(n & 1) res = (res * x) % mod;
x = (x * x) % mod;
n >>= 1;
}
return res;
}
// 組み合わせ nCr を求める
Int combination_mod(Int n, Int r, Int mod) {
if(r > n-r) r = n-r;
if(r == 0) return 1;
Int a = 1;
rep(i, 0, r) a = a * ((n-i) % mod) % mod;
Int b = mod_pow(fact_mod(r, mod), mod-2, mod);
return (a % mod) * (b % mod) % mod;
}
int main() {
IOS;
prec;
Int a,b,c,x,y,z, ans=0;
string s,t,r;
int N;
cin>>N;
cout<<(N+99)/100;
cout<<endl;
return 0;
}
|
#include<bits/stdc++.h>
#define N ((int)5e4 + 5)
#define MAXL ((ll)1e18 + 5)
#define MAX ((int)2e9 + 5)
#define fastio ios_base::sync_with_stdio(false) , cin.tie(0) , cout.tie(0)
#define endl "\n"
#define ll long long
using namespace std;
int main(){
fastio ;
string s ;
cin>>s ;
if(s[0]==s[1] && s[2]==s[0] && s[1]==s[2]) puts("Won") ;
else puts("Lost") ;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define all(x) (x).begin(),(x).end()
#define uniq(v) (v).erase(unique(all(v)),(v).end())
#define sz(x) (int)((x).size())
#define fr first
#define sc second
#define pii pair<int,int>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define mem1(a) memset(a,-1,sizeof(a))
#define mem0(a) memset(a,0,sizeof(a))
#define ppc __builtin_popcount
#define ppcll __builtin_popcountll
#define endl "\n"
#define vi vector<int>
#define ll long long
void solve()
{
//Code here
char a,b,c;
cin>>a>>b>>c;
if(a==b&& b==c &&a==c)
cout<<"Won"<<endl;
else
cout<<"Lost"<<endl;
}
int main()
{
#ifndef ONLINE_JUDGE
//for getting input from input.txt
freopen("input.txt", "r", stdin);
//for writing output to output.txt
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
ll T=1;
// cin>>T;
while(T--)
{
solve();
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
int main()
{
fast
ll n,x;cin>>n>>x;
ll a[n];
for(ll i=0;i<n;i++)
cin>>a[i];
for(ll i=0;i<n;i++)
{
if(a[i]!=x)
cout<<a[i]<<" ";
}
cout<<"\n";
}
| #pragma GCC optimize("O3")
#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 int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
#define test(t) int t; cin >> t; while(t--)
#define f(i, a, b) for(int (i) = (a); (i) < (b); ++(i))
#define all(a) (a).begin(), (a).end()
#define endl "\n"
#define ff first
#define ss second
#define pb push_back
#define deb(x) cout << #x << ": " << x << endl;
#define deb2(x,y) cout << #x << ": " << x << " ~ " << #y << ": " << y << endl;
#define deba(arr) cout << #arr << " ~ [ "; for(auto n: arr) cout << n << " "; cout << "]" << endl;
#define debap(arr) cout << #arr << " ~ [ "; for(auto n: arr) cout << n.first << "-" << n.second << " "; cout << "]" << endl;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define mx9 1000000007
#define mx7 10000007
#define mx6 1000006
#define mx5 200005
#define inf 1<<30
#define eps 1e-9
#define mod 1000000007
#define PI 3.141592653589793238462643383279502884L
void solve()
{
int n, x;
cin >> n >> x;
f(i, 0, n)
{
ll y;
cin >> y;
if(y != x)
cout << y << " ";
}
cout << endl;
}
int main()
{
fast;
// test(t)
solve();
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(int)(n); ++i)
#define rep_r(i,n) for(int i=(int)(n)-1; i>=0; --i)
#define reps(i,n,m) for(int i=(int)(n); i<(int)(m); ++i)
#define reps_r(i,n,m) for(int i=(int)(m)-1; i>=(int)(n); --i)
#define N 200001
int main(){
//cout<<fixed<<setprecision(10);
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n,q,t,a,b,state=1;
string s,s1,s2;
char c;
cin>>n>>s>>q;
s1 = s.substr(0,n);
s2 = s.substr(n,2*n);
rep(i,q){
cin>>t>>a>>b;
if(t==1){
if(b-1<n){
if(state==1){
c=s1[a-1];
s1[a-1]=s1[b-1];
s1[b-1]=c;
}else{
c=s2[a-1];
s2[a-1]=s2[b-1];
s2[b-1]=c;
}
}else if(n<=a-1){
if(state==1){
c=s2[a-n-1];
s2[a-n-1]=s2[b-n-1];
s2[b-n-1]=c;
}else{
c=s1[a-n-1];
s1[a-n-1]=s1[b-n-1];
s1[b-n-1]=c;
}
}else{
if(state==1){
c=s1[a-1];
s1[a-1]=s2[b-n-1];
s2[b-n-1]=c;
}else{
c=s2[a-1];
s2[a-1]=s1[b-n-1];
s1[b-n-1]=c;
}
}
}else if(t==2){
state *= -1;
}
}
if(state==1) cout<<s1<<s2<<"\n";
else cout<<s2<<s1<<"\n";
} | #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i=0; i<(int)(n); i++)
typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef pair<LL, int> PR;
const LL INF=1e16;
struct Graph{
int N, M; VI A, B; VL C; vector<VI> S, T;
Graph(int n) : N(n), M(0), S(n), T(n) {}
void setEdge(int s, int t, LL u){
A.push_back(s); B.push_back(t); C.push_back(u);
S[s].push_back(M); T[t].push_back(M); M++;
}
VL Dijkstra(int s){
VL D(N, INF); D[s]=0;
priority_queue< PR, vector<PR>, greater<PR> > Q;
REP(i, N){ Q.push(make_pair(D[i], i)); }
while(Q.size()){
PR p=Q.top(); int v=p.second; Q.pop();
if(D[v]<p.first){ continue; }
for(int e:S[v]){
int u=B[e]; LL nw=D[v]+C[e];
if(nw<D[u]){ D[u]=nw; Q.push(make_pair(D[u], u)); }
}
}
return D;
}
};
int main(){
int N, M, a, b; LL c;
cin >> N >> M; Graph G(N); VL J(N, INF);
REP(i, M){
cin >> a >> b >> c; a--; b--;
if(a==b){ J[a]=min(J[a], c); }
G.setEdge(a, b, c);
}
vector<VL> d;
REP(i, N){ d.push_back(G.Dijkstra(i)); }
REP(i, N){
LL ans=J[i];
REP(j, N){
if(i==j){ continue; }
ans=min(ans, d[i][j]+d[j][i]);
}
if(ans<INF){ cout << ans << "\n"; }
else{ cout << -1 << "\n"; }
}
} |
#include<iostream>
#include<algorithm>
#include<cstring>
#define debug(a) cout<<#a<<" = "<<a<<endl
using namespace std;
typedef long long ll;
const int N = 5010;
const int MOD = 998244353;
int n , m;
ll C[N];
ll f[N];
ll Q_power(ll a ,ll b){
ll res = 1;
while(b){
if(b & 1) res = res * a % MOD;
b >>= 1;
a = a*a % MOD;
}
return res;
}
ll X_1(ll a){ return Q_power(a ,MOD - 2) ;}
void init(){
C[0] = 1;
for(int i = 1 ; i <= n ; i ++){
C[i] = C[i - 1] * (n - i + 1) % MOD * X_1(i) % MOD;
}
}
ll F(int m){
if(~f[m] == 0){
f[m] = 0;
if(!(m & 1)){
for(int i = 0 ; i * 2 <= m ; i ++){
f[m] = (f[m] + C[i * 2] * F(m / 2 - i) % MOD) % MOD;
}
}
}
return f[m];
}
int main(){
cin >> n >> m;
memset(f,0xff,sizeof f);
init(); f[0] = 1;
cout << F(m) << endl;
return 0;
}
| #include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<set>
typedef long long ll;
typedef unsigned un;
typedef std::vector<int> P;
typedef std::pair<int,int> pii;
ll read(){ll x=0,f=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}while(c>='0'&&c<='9')x=x*10+(c-'0'),c=getchar();return f*x;}
ll max(ll a,ll b){return a>b?a:b;}
ll min(ll a,ll b){return a<b?a:b;}
template <typename T> bool umax(T& a,T t){if(t>a)return a=t,1;return 0;}
template <typename T> bool umin(T& a,T t){if(t<a)return a=t,1;return 0;}
/**********/
const int MAXN = 5011,mod = 998244353;
int C[MAXN][MAXN];
int f[MAXN],pre[MAXN];
void init(int n)
{
C[0][0]=1;
for(int i=1;i<=n;++i)
{
C[i][0]=1;
for(int j=1;j<=i;++j)C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod;
}
}
int main()
{
init(5000);
int n=read(),m=read(),rest=m;
f[0]=1;
for(int i=0;(1<<i)<=m;++i)
{
memcpy(pre,f,sizeof f),memset(f,0,sizeof f);
for(int j=0;j<=rest;++j)
for(int k=0;k*(1<<i)<=j;k+=2)f[j]=(f[j]+ll(pre[j-k*(1<<i)])*C[n][k])%mod;
}
printf("%d\n",f[m]);
return 0;
}
|
// #include <bits/stdc++.h>
// string s;
// getline(cin, s);
// g++ -std=c++11 -O2 -Wall test.cpp -o test
// printf("%.9f\n", x); PRECISION
#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
using ll = long long;
#define F first
#define S second
const long long MOD = 1e9 + 7;
const double EPS = 1e-9;
const int MAX_LENGTH = (2 * 1e5 + 1);
int n, c;
long long x;
long long dp[MAX_LENGTH][2];
void solve() {
set<int> set_ids;
unordered_map<int, long long> max_pos;
unordered_map<int, long long> min_pos;
int MMIN = 0, MMAX = 1;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> x >> c;
set_ids.insert(c);
if (max_pos.find(c) == max_pos.end()) {
max_pos[c] = x;
min_pos[c] = x;
} else {
max_pos[c] = max(max_pos[c], x);
min_pos[c] = min(min_pos[c], x);
}
}
vector<int> ids(set_ids.begin(), set_ids.end());
n = ids.size();
dp[n - 1][MMIN] = abs(min_pos[ids[n - 1]] - max_pos[ids[n - 1]]) +
abs(max_pos[ids[n - 1]]);
dp[n - 1][MMAX] = abs(min_pos[ids[n - 1]] - max_pos[ids[n - 1]]) +
abs(min_pos[ids[n - 1]]);
for (int i = n - 2; i >= 0; i--) {
int cur = ids[i];
int next = ids[i + 1];
long long d = abs(min_pos[cur] - max_pos[cur]);
dp[i][MMIN] =
d + min(dp[i + 1][MMAX] + abs(max_pos[next] - max_pos[cur]),
dp[i + 1][MMIN] + abs(min_pos[next] - max_pos[cur]));
dp[i][MMAX] =
d + min(dp[i + 1][MMAX] + abs(max_pos[next] - min_pos[cur]),
dp[i + 1][MMIN] + abs(min_pos[next] - min_pos[cur]));
}
cout << min(abs(min_pos[ids[0]]) + dp[0][MMIN],
abs(max_pos[ids[0]]) + dp[0][MMAX]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
// cin >> t;
for (int i = 1; i <= t; i++) {
// cout <<"Case #" << i << ": ";
solve();
}
} | #include<bits/stdc++.h>
using namespace std;
int main(){
int n; cin >> n;
vector<int> x(n);
vector<int> y(n);
for(int i=0; i<n; i++){
cin >> x.at(i);
cin >> y.at(i);
}
for(int i=0; i<n-2; i++){
for(int j=i+1; j<n-1; j++){
for(int k=j+1; k<n; k++){
if((x.at(j)-x.at(i))*(y.at(k)-y.at(j)) == (x.at(k)-x.at(j))*(y.at(j)-y.at(i))){
cout << "Yes" << endl;
return (0);
}
}
}
}
cout << "No" << endl;
} |
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long int a,b,c;
cin>>a>>b>>c;
long long int sqa=pow(a,2)+pow(b,2);
long long int sqc=pow(c,2);
if(sqa<sqc)
{
cout<<"Yes"<<endl;
}
else
{
cout<<"No"<<endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, x, y, ans = 0;
cin >> a >> b >> x >> y;
if (2 * x >= y) {
if (a <= b) ans = abs(a - b) * y + x;
else
ans = (abs(a - b) - 1) * y + x;
} else
ans = abs(a - b) * x * 2 + ((a <= b) ? x : -x);
cout << ans << endl;
return 0;
} |
//https://cp-algorithms.com/algebra/linear_congruence_equation.html
#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define rrep(i,b,a) for(int i=b;i>=a;i--)
#define fori(a) for(auto i : a )
#define all(a) begin(a), end(a)
#define set(a,b) memset(a,b,sizeof(a))
#define sz(a) a.size()
double pi=acos(-1);
#define ll long long
#define ull unsigned long long
#define pb push_back
#define PF push_front //deque
// #define mp make_pair
#define pq priority_queue
const ll mod=1000000007;
#define f first
#define s second
#define pii pair< ll, ll >
#define vi vector<int>
#define vpii vector<pii>
#define debug(v) for(auto i:v) cout<<i<<" ";
#define tc int t; cin >> t; while(t--)
// using namespace boost::multiprecision;
using namespace std;
void optimizeIO(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
ll gcdExtended(ll a, ll b, ll* x, ll* y);
// Function to find modulo inverse of a
ll modInverse(ll a, ll m)
{
ll x, y;
ll g = gcdExtended(a, m, &x, &y);
return (x % m + m) % m;
}
// C function for extended Euclidean Algorithm
ll gcdExtended(ll a, ll b, ll* x, ll* y)
{
// Base Case
if (a == 0)
{
*x = 0, *y = 1;
return b;
}
ll x1, y1; // To store results of recursive call
ll gcd = gcdExtended(b % a, a, &x1, &y1);
// Update x and y using results of recursive
// call
*x = y1 - (b / a) * x1;
*y = x1;
return gcd;
}
void solve(){
// Method 1:(n*x+n-s)%k==0 find min x;
// or Method 2: (x*k+s)%n==0 find min x;
ll n,s,k;
cin>>n>>s>>k;
ll b= (k+(s-n)%k)%k;
ll a=n;
if(b%__gcd(a,k)!=0){cout<<-1<<endl;return;}
ll g=__gcd(a,k);
a/=g;b/=g,k/=g;
ll z=(b*modInverse(a,k))%k;
cout<<(z*n+n-s)/(k*g)<<endl;
}
int main(){
optimizeIO();
int r=1;
tc{solve();}
}
// Method 2:
// void solve(){
// ll n,s,k;
// cin>>n>>s>>k;
// ll b=n-s;
// ll a=k;
// ll m=n;
// ll d=__gcd(__gcd(a,b),m);
// a/=d,b/=d,m/=d;
// // cout<<a<<b<<m<<endl;
// if(__gcd(a,m)!=1){cout<<-1<<endl;return;}
// ll z=(b*modInverse(a,m))%m;
// cout<<z<<endl;
// }
| #include <iostream>
int64_t gcd(int64_t a,int64_t b,int64_t &x,int64_t &y)
{
if(b==0){x=1;y=0;return a;}
int64_t d=gcd(b,a%b,y,x);
y-=a/b*x;
return d;
}
int main()
{
int T;
std::cin>>T;
for(int i=0;i<T;i++)
{
int64_t N,S,K;
std::cin>>N>>S>>K;
int64_t result=0;
int64_t x,y;
int64_t d=gcd(K,-N,x,y);
if(S%d==0)
{
x*=-S/d;
x%=N/d;if(x<0)x+=N/d;
result=x;
}
else
{
result=-1;
}
std::cout<<result<<std::endl;
}
return 0;
} |
#include <bits/stdc++.h>
//#include <bits/extc++.h>
//#define int long long
#define ll long long
#define ull unsigned ll
#define endl "\n"
#define pb push_back
#define ms(v,x) memset(v,x,sizeof(v))
#define ff first
#define ss second
#define td(v) v.begin(),v.end()
#define rep(i,a,n) for (int i=(a);i<(n);i++)
#define per(i,a,n) for (int i=(n-1);i>=a;i--)
#define trav(a, x) for(auto& a : x)
#define re(v) {for(auto &_re : v) cin >> _re;}
#define pr(v) {for(auto _pr : v) cout << _pr << " "; cout << endl;}
#define sz(x) (int)(x).size()
#define all(x) x.begin(), x.end()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define eb emplace_back
using namespace std;
using vvi = vector<vi>;
using vvl = vector<vl>;
//const ll M = 1e9 + 7;
//const ll M = 998244353;
const ll M = 1e9 + 9;
//const ll M = 1e6;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll binpow(ll a, ll b){
ll res = (a!=0);
while(b > 0){
if(b&1) res = res * a % M;
a = a * a % M;
b>>=1;
}
return res;
}
const ll inf = 2e18;
void solve(){
ll n, x;
cin >> n >> x;
vl a(n);
re(a);
ll ans = -1;
for(int i=1;i<=n;i++){
// biggest sum such that (x - s) % i == 0
vector<vl> dp(i, vl(i + 1, -inf));
dp[0][0] = 0;
for(int j=0;j<n;j++){
vector<vl> new_dp = dp;
for(int k=0;k<i;k++){
for(int z=0;z<i;z++){
if(dp[k][z] != -inf){
new_dp[(a[j] + k) % i][z + 1] = max(new_dp[(a[j] + k) % i][z + 1], dp[k][z] + a[j]);
}
}
}
swap(dp, new_dp);
}
if(dp[x%i][i] == -inf) continue;
if(ans == -1){
ans = (x - dp[x%i][i]) / i;
}
else ans = min(ans, (x - dp[x%i][i]) / i);
}
cout << ans << endl;
}
int32_t main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen(".in", "r", stdin);
//freopen(".out", "w", stdout);
int t = 1;
//cin >> t;
while(t--){
solve();
}
} | #define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include <chrono>
#include <random>
#include <unordered_set>
using namespace std;
typedef long long ll;
void solve() {
ll a, b;
cin >> a >> b;
int n = 73;
vector<int> s(n);
vector<int> prime;
for (int i = 2; i < n; i++) {
if (s[i] == 0) {
s[i] = i;
prime.emplace_back(i);
for (int j = i; j < n; j += i) {
s[j] = i;
}
}
}
int m = b - a + 1;
n = prime.size();
vector<int> mask(m);
for (int i = 0; i < m; i++) {
ll x = a + i;
for (int j = 0; j < n; j++) {
if (x % prime[j] == 0) {
mask[i] ^= (1 << j);
}
}
}
vector<ll> dp(1 << n);
dp[0] = 1;
for (int i = 0; i < m; i++) {
vector<ll> dp2(1 << n);
for (int j = 0; j < (1 << n); j++) {
if (dp[j] == 0)
continue;
dp2[j] += dp[j];
if ((mask[i] & j) == 0)
dp2[mask[i] ^ j] += dp[j];
}
dp.swap(dp2);
}
ll ans = 0;
for (int i = 0; i < (1 << n); i++) {
ans += dp[i];
}
cout << ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#if defined(_DEBUG)
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int q = 1;
//cin >> q;
for (; q > 0; q--) {
solve();
//cout << endl;
}
} |
#include <algorithm>
#include <complex>
#include <cstdlib>
#include <ctime>
#include <time.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <numeric>
#include <limits>
#include <type_traits>
#include <locale>
#include <omp.h>
using namespace std;
#define SAY_YES cout << "YES" << endl;
#define SAY_Yes cout << "Yes" << endl;
#define SAY_NO cout << "NO" << endl;
#define SAY_No cout << "No" << endl;
#define IFYES(TRUE_OR_FALSE) \
if (TRUE_OR_FALSE) \
{ \
cout << "YES" << endl; \
} \
else \
{ \
cout << "NO" << endl; \
}
#define IFYes(TRUE_OR_FALSE) \
if (TRUE_OR_FALSE) \
{ \
cout << "Yes" << endl; \
} \
else \
{ \
cout << "No" << endl; \
}
#define IFyes(TRUE_OR_FALSE) \
if (TRUE_OR_FALSE) \
{ \
cout << "yes" << endl; \
} \
else \
{ \
cout << "no" << endl; \
}
#define DEBUG_OUTPUT_ARRAY(XXX, ONE) \
for (int i = 0; i < (ONE); i++) \
{ \
cout << "DEBUG: i = " << i << " -> " << XXX[i] << endl; \
}
#define DEBUG_OUTPUT_ARRAY2(XXX, ONE, TWO) \
for (int i = 0; i < (ONE); i++) \
{ \
cout << "<<< i = " << i << " >>>" << endl; \
for (int j = 0; j < (TWO); j++) \
{ \
cout << "DEBUG: j = " << j << " -> " << XXX[i][j] << endl; \
} \
}
#define DEBUG_OUTPUT_ARRAY2_BOX(XXX, ONE, TWO) \
for (int i = 0; i < (ONE); i++) \
{ \
cout << i << " "; \
for (int j = 0; j < (TWO); j++) \
{ \
cout << XXX[i][j] << " "; \
} \
cout << endl; \
}
typedef pair<long long int, long long int> pll;
typedef pair<long long int, pll> lpll;
const long long int mod = 998244353;
const long long int INF = 1e18;
const long double PI=3.14159265358979323;
//const long long int pl=1000000;
long long int N,K,A[300000]={},B[300000],res=0;
long long int dp[3005][6005]={},memo[3005]={};
int main()
{
cout << fixed << setprecision(18);
cin>>N;
for (long long int i = 0; i < N; i++)
{
cin>>A[i]>>B[i];
}
for (long long int i = 0; i < N; i++)
{
for (long long int j = i+1; j < N; j++)
{
for (long long int k = j+1; k < N; k++)
{
if((B[j]-B[i])*(A[k]-A[i])==(B[k]-B[i])*(A[j]-A[i])){
cout<<"Yes"<<endl;
return 0;
}
}
}
}
cout<<"No"<<endl;
}
| #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
#define rep(i,N) for(int i=0;i<(int)N;i++)
static inline void PUT(char c)
{
static char buf[1<<15],*ptr=buf;
if(ptr==buf+strlen(buf)||c==0){fwrite(buf,1,ptr-buf,stdout),ptr=buf;}*ptr++=c;
}
static inline int IN(void)
{
int x=0,f=0,c=getchar();while(c<48||c>57){f^=c==45,c=getchar();}
while(c>47&&c<58){x=x*10+c-48,c=getchar();}return f?-x:x;
}
static inline void OUT(int a)
{
if(a<0)PUT('-'),a=-a;
int d[40],i=0;do{d[i++]=a%10;}while(a/=10);
while(i--){PUT(d[i]+48);}PUT('\n');
}
int main(void)
{
int N=IN(),A=0,B=0,sz=2*N,tp[sz]={},com[sz];short dp[sz+1]={};
memset(com,-1,sizeof(com));
rep(i,N)
{
A=IN(),B=IN();
if(A!=-1){A--;if(tp[A]){return puts("No"),0;}tp[A]=i+1;}
if(B!=-1){B--;if(tp[B]){return puts("No"),0;}tp[B]=-i-1;}
if(A!=-1&&B!=-1){com[A]=B;com[B]=A;}
}
dp[0]=1;
rep(i,sz)
{
if(!dp[i])continue;
rep(j,sz-i-1)
{
int w=j+2;if(w&1){continue;}else w/=2;
short ok=1,exist[N]={};
rep(k,w)
{
int p=i+k,q=i+k+w;short same=1;
if(com[p]!=-1&&!(i<=com[p]&&com[p]<=i+j+1)){ok=0;break;}
if(com[q]!=-1&&!(i<=com[q]&&com[q]<=i+j+1)){ok=0;break;}
if(tp[p]!=0&&tp[q]!=0){if(tp[p]<0||tp[p]+tp[q]!=0){ok=0;break;}else same=1;}
if(tp[p]<0||tp[q]>0){ok=0;break;}
else
{
if(tp[p]!=0){int v=tp[p]-1;if(exist[v]){ok=0;break;}exist[v]=1;}
if(!same&&tp[q]!=0){int v=-tp[q]-1;if(exist[v]){ok=0;break;}exist[v]=1;}
}
}
if(ok)dp[i+j+2]=1;
}
}
return puts(dp[sz]?"Yes":"No"),0;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fw(p) for(int w=0;w<(p);w++)
#define fx(p) for(int x=0;x<(p);x++)
#define fy(p) for(int y=0;y<(p);y++)
#define fz(p) for(int z=0;z<(p);z++)
#define fyg(p,g) for(int y=(g);y<(p);y++)
#define fzg(p,g) for(int z=(g);z<(p);z++)
#define ce(d) cout<<d<<endl;
#define vecp(p) int aa;cin>>aa;(p).push_back(aa);
#define vecpl(p) long long aa;cin>>aa;(p).push_back(aa);
#define vecps(p) string aa;cin>>aa;(p).push_back(aa);
#define vecp2(p) cin>>aa;(p).push_back(aa);
#define vecpl2(p) long long a b;cin>>ab;(p).push_back(ab);
#define vecps2(p) string ab;cin>>ab;(p).push_back(ab);
#define sorts(c) sort((c).begin(),(c).end());
#define reverses(c) reverse((c).begin(),(c).end());
#define vec(b) vector<int> (b);
#define vecl(b) vector<long long> (b);
#define vecs(b) vector<string> (b);
#define pb(b,a) (b).push_back((a));
#define doublece(a,b) cout<<(a)<<' '<<(b)<<endl;
#define pairs(s) vector<pair<int,int>> (s);
#define pairsp(s) int aa,bb;cin>>aa>>bb;(s).push_back(make_pair(aa,bb));
#define MOD 1000000007
#define cey ce("Yes")
#define cen ce("No")
#define ceY ce("YES")
#define ceN ce("NO")
ll dp2[150000][20];
ll cost[20][20];// wa―シャルフロイド
int main()
{
int N;
cin >> N;
vector<tuple<int, int, int>> B;
fx(N) {
int a, b, c;
cin >> a >> b >> c;
B.push_back(make_tuple(a, b, c));
}
fx(N) {
fy(N) {
cost[x + 1][y + 1] = abs(get<0>(B[x]) - get<0>(B[y])) + abs(get<1>(B[x]) - get<1>(B[y])) + max(0, get<2>(B[y]) - get<2>(B[x]));
}
}
//fy(N) {
// fx(N) {
// fz(N) {
// cost[x+1][z + 1] = min(cost[x + 1][z + 1], cost[x + 1][y + 1] + cost[y + 1][z + 1]);
// }
// }
//}
fx(150000) {
fy(N) {
dp2[x][y+1] = LLONG_MAX;
}
}
dp2[1][1] = 0;
for (int y = 1; y < (1 << N); y += 2) {
fx(N) {
if (((1 << x) & y) == 0) { continue; }
if (dp2[y][x + 1] == LLONG_MAX) { continue; }
fz(N) { // 増やし先
if (((1 << z) & y) != 0) { continue; }
fw(N) {
if (((1 << w) & y) == 0) { continue; }
dp2[y | (1 << z)][z+1] = min(dp2[y | (1 << z)][z+1], dp2[y][x+1] + cost[x + 1][w + 1] + cost[w + 1][z + 1]);
}
}
}
}
ll ans = LLONG_MAX;
fx(N) {
//if (dp2[(1 << N) - 1][x + 1] == LLONG_MAX) { continue; }
if (dp2[(1 << N) - 1][x + 1] < 0) { continue; }
ans = min(ans, dp2[(1 << N) - 1][x + 1] + cost[x + 1][1]);
}
ce(ans)
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define PI 3.14159265358979323846264338327950L
struct times{
bool start;
ll time = 0;
ll value = 0;
};
int N,C;
priority_queue<pair<int,times*>> q ;
void solve(){
cin >> N>> C;
ll a,b,c;
for (int i = 0; i < N; i++)
{
cin >> a >> b >> c;
times* t = new times();
t->start = true;
t->time = a;
t->value = c;
q.push(make_pair(-a*2,t));
times* f = new times();
f->start = false;
f->time = b;
f->value = c;
q.push(make_pair(-b*2-1,f));
}
ll ans = 0;
ll cost = 0;
ll topay = 0;
ll pos = q.top().second->start;
for (int i = 0; i < 2*N; i++)
{
auto e = q.top();
q.pop();
topay = (cost > C) ? C : cost;
if(e.second->start){
cost += e.second->value;
ans += (e.second->time - pos )* topay;
pos = e.second->time;
}
else{
if(pos <= e.second->time){
ans += (e.second->time - pos +1 )* topay;
pos = e.second->time +1;
}
cost -= e.second->value;
}
}
cout << ans<< endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
solve();
}
|
#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()
const long long MOD = 1000000007;
const long long INF = 9999999999999999;
using ll = long long;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll A[3020][5];
ll N;
bool isOK(ll key){
vector<ll> aup(32);
rep(i,N){
ll temp = 0;
rep(k,5){
if(A[i][k]>=key){
temp+=pow(2,k);
}
}
aup[temp]++;
}
bool aru = 0;
rep(i,32){
rep(k,32){
rep(t,32){
if((i|k|t) == 31){
if(aup[i]>0&&aup[k]>0&&aup[t]>0){
//cout << i << k << t << endl;
aru = 1;
}
}
}
}
}
return aru;
}
int main(){
cin>>N;
rep(i,N){
cin>>A[i][0]>>A[i][1]>>A[i][2]>>A[i][3]>>A[i][4];
}
ll right = MOD;
ll left = -1;
while((right-left)>1){
ll mid=left+(right-left)/2;
if(isOK(mid)){
left = mid;
}
else{
right = mid;
}
}
cout << left << endl;
return 0;
} | #include<algorithm>
#include<iostream>
using namespace std;
#define ll long long
#define For(i,l,r) for(int i=l;i<=r;i++)
#define MAXN 200001
int N,ans;ll now=0;struct A{int a,b;bool operator<(A x)const{return 2ll*a+b>2ll*x.a+x.b;}}a[MAXN];
int main()
{
cin>>N;For(i,1,N)cin>>a[i].a>>a[i].b,now-=a[i].a;sort(a+1,a+N+1);
For(i,1,N){if(now>0)break;now+=2ll*a[i].a+a[i].b,ans++;}cout<<ans<<endl;return 0;
} |
#include <bits/stdc++.h>
#define rep(i,n) for(long long i=0; i<n; i++)
#define Rep(i,n) for(long long i=1; i<n; i++)
#define ll long long
#include <math.h>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
#define PI acos(-1)
using P =pair<ll,ll>;
ll mod=1e9+7;
ll dp[2000+4];
vector<pair<ll,ll>>p;
vector<ll> to[100005];
ll inf =1000000007;
ll keta(ll n){
ll ans=0;
while(n!=0){
ans++;
n/=10;
}
return ans;
}
ll kazu(ll i){
ll ans=0;
for(ll k=1;k*k<=i;k++){
if(i%k==0){
if(i/k==k)ans++;
else ans+=2;
}
}
return ans;
}
int main()
{
ll n;
cin>>n;
vector<float>x(n);
vector<float>y(n);
rep(i,n)cin>>x[i]>>y[i];
rep(i,n){
if(x[i]==x[i+1]){
for(ll j=i+2;j<n;j++){
if(x[i]==x[j]){
cout<<"Yes"<<endl;
return 0;
}
}
}else{
for(ll k=i+1;k<n;k++){
float lean=(y[k]-y[i])/(x[k]-x[i]);
for(ll j=k+1;j<n;j++){
float tmp=(y[j]-y[i])/(x[j]-x[i]);
if(lean==tmp){
cout<<"Yes"<<endl;
return 0;
}
}
}
}
}
cout<<"No"<<endl;
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long int in;
int main()
{
in n,i,j,k;
cin>>n;
in a[n],b[n];
for(i=0;i<n;++i)
cin>>a[i]>>b[i];
for(i=0;i<n;++i)
for(j=i+1;j<n;++j)
for(k=j+1;k<n;++k)
{
in val=a[i]*(b[j]-b[k])+a[j]*(b[k]-b[i])+a[k]*(b[i]-b[j]);
if(val==0)
{
cout<<"Yes";
return 0;
}
}
cout<<"No";
return 0;
} |
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
ll pw(ll a, ll b, ll mod) {
if (b == 0) return 1 % mod;
if (b & 1) {
return (a * pw(a, b - 1, mod)) % mod;
}
ll res = pw(a, b / 2, mod);
return (res * res) % mod;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
// freopen("input.txt", "r", stdin);
ll n;
ll m;
cin >> n >> m;
ll p = m * m;
cout << (pw(10 % p, n, p) / m) % m;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
int main()
{
char s,t;
cin>>s>>t;
if(s=='Y')
{
char p=(t-'a'+'A');
cout<<p;
}
else
{
cout<<t;
}
} |
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using ld = long double;
using vld = vector<ld>;
using vb = vector<bool>;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#ifdef LOCAL
#define dbg(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl
#else
#define dbg(x) true
#endif
template <class T> bool chmin(T& a, T b) {
if(a > b) { a = b; return true; }
else return false;
}
template <class T> bool chmax(T& a, T b) {
if(a < b) { a = b; return true; }
else return false;
}
template <class T> ostream& operator<<(ostream& s, const vector<T>& a) {
for(auto i : a) s << i << ' ';
return s;
}
constexpr int INF = 1 << 30;
constexpr ll INFL = 1LL << 62;
constexpr ld EPS = 1e-12;
ld PI = acos(-1.0);
struct mint {
static const long long mod = 998244353;
long long x;
mint(long long x = 0) : x((x % mod + mod) % mod) {}
mint operator-() const { return mint(-x);}
mint& operator+=(const mint& a) {if ((x += a.x) >= mod) x -= mod; return *this;}
mint& operator-=(const mint& a) {if ((x += mod - a.x) >= mod) x -= mod; return *this;}
mint& operator*=(const mint& a) {(x *= a.x) %= mod; return *this;}
friend const mint operator+(const mint& a, const mint& b) { mint ret = a; return ret += b; }
friend const mint operator-(const mint& a, const mint& b) { mint ret = a; return ret -= b; }
friend const mint operator*(const mint& a, const mint& b) { mint ret = a; return ret *= b; }
friend ostream& operator<<(ostream& s, const mint& a) { return s << a.x; }
mint pow(long long t) const {
if (!t) return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1) a *= *this;
return a;
}
// for prime mod
mint inv() const { return pow(mod - 2);}
mint& operator/=(const mint a) { return (*this) *= a.inv();}
friend const mint operator/(const mint& a, const mint& b) { mint ret = a; return ret /= b; }
};
struct combi {
const static long long mod = mint::mod;
int n;
vector<mint> fac, finv, inv;
combi(int _n) : n(_n + 1), fac(n, 1), finv(n, 1), inv(n, 1) {
for (int i = 2; i < n; ++i) {
fac[i] = fac[i - 1] * i;
inv[i] =- inv[mod % i] * (mod / i);
finv[i] = finv[i - 1] * inv[i];
}
};
mint get(int n, int k) {
if(n < k) return 0;
if(n < 0 || k < 0) return 0;
mint ret = fac[n] * finv[k] * finv[n - k];
return ret;
}
};
// 素因数分解
// nの(素因数、指数)のペアを返す。計算量はO(√nlogn)
vector<pair<ll, ll>> prime_factorize(ll n) {
vector<pair<ll, ll>> ret;
for(ll i = 2; i*i <= n; ++i) {
if(n % i != 0) continue;
ll ex = 0;
while(n % i == 0) {
++ex;
n /= i;
}
ret.push_back({i, ex});
}
if(n > 1) ret.push_back({n, 1});
return ret;
}
void solve() {
ll n, m;
cin >> n >> m;
mint ans = 0;
combi com(n+100);
for(ll i = 1; i <= m; ++i) {
vpll vp = prime_factorize(i);
mint ans_ = 1;
for(auto [p, e] : vp) ans_ *= com.get(n-1+e, e);
ans += ans_;
}
cout << ans << endl;
return;
}
int main() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
cout << fixed << setprecision(15);
solve();
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, n) for(int (i)=0; (i)<(n); (i)++)
#define FOR1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FORI(i, n) for(int (i)=n-1; (i)>=0; (i)--)
template<class T, class U> void umin(T& x, const U& y){ x = min(x, (T)y);}
template<class T, class U> void umax(T& x, const U& y){ x = max(x, (T)y);}
template<class T, class U> void init(vector<T> &v, U x, size_t n) { v=vector<T>(n, (T)x); }
template<class T, class U, typename... W> void init(vector<T> &v, U x, size_t n, W... m) { v=vector<T>(n); for(auto& a : v) init(a, x, m...); }
int n, m, k;
vector<vector<int>> edges;
vector<int> c;
vector<int> vis;
vector<int> d;
int main(int argc, char** argv) {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << setprecision(15);
if (argc == 2 && atoi(argv[1]) == 123456789) freopen("d:\\code\\cpp\\contests\\stdin", "r", stdin);
cin >> n >> m;
edges.resize(n+1);
FOR(i, m){
int u, v;
cin >> u >> v;
edges[u].push_back(v);
edges[v].push_back(u);
}
cin >> k;
c.resize(k);
FOR(i, k) cin >> c[i];
vector<vector<int>> dist(k, vector<int>(k));
for (int i = 0; i < k; i++){
vector<int> d(n+1, -1);
d[c[i]] = 0;
queue<int> Q;
Q.push(c[i]);
while (!Q.empty()){
int v = Q.front();
Q.pop();
for (int w : edges[v]){
if (d[w] == -1){
d[w] = d[v] + 1;
Q.push(w);
}
}
}
for (int j = 0; j < k; j++){
dist[i][j] = d[c[j]];
}
}
int K = 1<<k;
const int inf = 1e9;
vector<vector<int>> dp(1 << k, vector<int>(k, inf));
for (int i = 0; i < k; i++){
dp[1 << i][i] = 0;
}
for (int i = 1; i < (1 << k); i++){
for (int j = 0; j < k; j++){
if (dp[i][j] != inf){
for (int k2 = 0; k2 < k; k2++){
if (!(i >> k2 & 1)){
if (dist[j][k2] != -1){
int i2 = i + (1 << k2);
dp[i2][k2] = min(dp[i2][k2], dp[i][j] + dist[j][k2]);
}
}
}
}
}
}
int sol = inf;
FOR(i, k) umin(sol, dp[K-1][i]);
cout << (sol < inf ? sol+1 : -1) << endl;
if (argc == 2 && atoi(argv[1]) == 123456789) cout << clock()*1.0/CLOCKS_PER_SEC << " sec\n";
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define int long long
#define PI 3.1415926535897932384626
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define fast ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vpi;
typedef vector<vi> vvi;
const int M=1000000007;
void solution()
{
int i, j, k, n, m;
cin>>n;
pi arr[n];
for(int i=0;i<n;i++) cin>>arr[i].S>>arr[i].F;
sort(arr,arr+n);
vector<pi>pos;
i=0;
while(i<n)
{
j=i+1;
while(j<n && arr[j].F==arr[i].F) j++;
j--;
pos.pb({arr[i].S,arr[j].S});
i=j+1;
}
int ans=0;
m=pos.size();
int dp[m][2];
dp[0][0]=abs(pos[0].F);
dp[0][1]=abs(pos[0].S);
for(int i=1;i<m;i++)
{
dp[i][0]=min(abs(pos[i].F-pos[i-1].F)+dp[i-1][1],abs(pos[i].F-pos[i-1].S)+dp[i-1][0]);
dp[i][1]=min(abs(pos[i].S-pos[i-1].F)+dp[i-1][1],abs(pos[i].S-pos[i-1].S)+dp[i-1][0]);
}
ans=min(abs(pos[m-1].F)+dp[m-1][1],abs(pos[m-1].S)+dp[m-1][0]);
for(pi p:pos) ans+=abs(p.F-p.S);
cout<<ans;
}
signed main()
{
int t=1;
//cin>>t;
while(t--)
{
solution();
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5+7;
ll seg[N<<2],tag[N<<2];
int fa[N],dep[N],siz[N],son[N],top[N],dfn[N],rk[N], w[N];
int a[N], b[N];
int n,m,root;
int tot=0;
vector<int> G[N];
void pushDown(int node,int t){
if(tag[node]!=0){
tag[node<<1]+=tag[node];
tag[node<<1|1]+=tag[node];
seg[node<<1]=(seg[node<<1]+tag[node]*(t-(t>>1)));
seg[node<<1|1]=(seg[node<<1|1]+tag[node]*(t>>1));
tag[node]=0;
}
return ;
}
void dfs1(int u,int f,int dp){
fa[u]=f;
dep[u]=dp;
siz[u]=1;
son[u]=-1;
for(size_t i=0;i<G[u].size();i++){
int v=G[u][i];
if(v==fa[u]) continue;
dfs1(v,u,dp+1);
siz[u]+=siz[v];
if(son[u]==-1 || siz[v]>siz[son[u]]){
son[u]=v;
}
}
}
void dfs2(int u,int tp){
top[u]=tp;
tot++;
dfn[u]=tot;
rk[tot]=u;
//rk[tot]=w[u];
if(son[u]==-1) return ;
dfs2(son[u],tp);
for(size_t i=0;i<G[u].size();i++){
int v=G[u][i];
if(v==fa[u] || v==son[u]) continue;
dfs2(v,v);
}
}
void build(int node,int l,int r){
if(l==r){
//seg[node]=w[rk[l]];
seg[node] = 0;
//seg[node]=rk[l]%mod;
return ;
}
int mid=(l+r)>>1;
build(node<<1,l,mid);
build(node<<1|1,mid+1,r);
seg[node]=(seg[node<<1]+seg[node<<1|1]);
}
void update(int node,int l,int r,int ul,int ur,ll val){
if(r<ul || l>ur)
return ;
if(l>=ul && r<=ur){
tag[node]+=val;
seg[node]+=val*(r-l+1);
return ;
}
pushDown(node,r-l+1);
int mid=(l+r)>>1;
update(node<<1,l,mid,ul,ur,val);
update(node<<1|1,mid+1,r,ul,ur,val);
seg[node]=(seg[node<<1]+seg[node<<1|1]);
}
void update1(int u,int v,ll val){
while(top[u]!=top[v]){
if(dep[top[u]]<dep[top[v]]) swap(u,v);
update(1,1,n,dfn[top[u]],dfn[u],val);
u=fa[top[u]];
}
if(dep[u]>dep[v]) swap(u,v);
update(1,1,n,dfn[u],dfn[v],val);
}
void update2(int u,ll val){
update(1,1,n,dfn[u],dfn[u]+siz[u]-1,val);
}
ll query(int node,int l,int r,int ql,int qr){
if(ql>r || qr<l) return 0;
if(ql<=l && qr>=r) return seg[node];
pushDown(node,r-l+1);
int mid=(l+r)>>1;
return (query(node<<1,l,mid,ql,qr)+query(node<<1|1,mid+1,r,ql,qr));
}
ll query1(int u,int v){
ll res=0;
while(top[u]!=top[v]){
if(dep[top[u]]<dep[top[v]]) swap(u,v);
res=(res+query(1,1,n,dfn[top[u]],dfn[u]));
u=fa[top[u]];
}
if(dep[u]>dep[v]) swap(u,v);
res=(res+query(1,1,n,dfn[u],dfn[v]));
return res;
}
ll query2(int u){
return query(1,1,n,dfn[u],dfn[u]+siz[u]-1);
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
scanf("%d", &n);
//for(int i=1;i<=n;i++) scanf("%d", &w[i]);
for(int i=1;i<n;i++){
/* int u,v;
scanf("%d %d", &u, &v);
G[u].push_back(v);
G[v].push_back(u); */
scanf("%d %d", &a[i], &b[i]);
G[a[i]].push_back(b[i]);
G[b[i]].push_back(a[i]);
}
root = 1;
dfs1(root,root,1);
dfs2(root,root);
build(1,1,n);
scanf("%d", &m);
int t, e;
ll x, sum = 0;
while(m--){
//int op,u,v,val;
//scanf("%d", &op);
scanf("%d %d %lld", &t, &e, &x);
/* if(op==1){
scanf("%d %d %d", &u, &v, &val);
update1(u,v,val);
}else if(op==2){
scanf("%d %d", &u, &v);
printf("%d\n", query1(u,v));
}else if(op==3){
scanf("%d %d", &u, &val);
update2(u,val);
}else {
scanf("%d", &u);
printf("%d\n", query2(u));
} */
if(dep[b[e]] > dep[a[e]]) {
if(t == 1) {
update2(b[e], -x);
sum += x;
}else update2(b[e], x);
}else {
if(t == 2) {
update2(a[e], -x);
sum += x;
}else update2(a[e], x);
}
}
for(int i = 1; i <= n; i ++) {
printf("%lld\n", query(1, 1, n, dfn[i], dfn[i]) + sum);
}
return 0;
}
|
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
vector<vector<char>> mat;
vector<pair<int,string>> in;
vector<string> in2;
vector<string> in3;
vector<string> in4;
vector<string> in5;
vector<string> in6;
vector<string> in7;
vector<string> in8;
vector<string> in9;
vector<string> in10;
vector<string> in11;
vector<string> in12;
vector<string> concat_in;
int n,m;
void answer_print(){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cout << mat[i][j] ;
if(j==n-1) cout << endl;
}
}
}
bool isPut(int x,int y,string str){
if(mat[y][x] != '.') return false;
if(n-x+1 < str.size()) return false;
return true;
}
void Put(int x,int y,string str){
for(int i = x;i<x+str.size();i++){
mat[y][i] = str[i-x];
}
}
void solve(){
int k=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(isPut(j,i,concat_in[k])) {
Put(j,i,concat_in[k]);
k++;
}
}
}
}
int main(){
// input
cin >> n >> m;
mat.resize(n);in.resize(m);
for(int i=0;i<n;i++) mat[i].resize(n,'.');
for(int i=0;i<m;i++) {
cin >>in[i].second;
in[i].first = in[i].second.size();
}
for(int i=0;i<in.size();i++){
switch(in[i].first){
case 2 : in2.push_back(in[i].second);break;
case 3 : in3.push_back(in[i].second);break;
case 4 : in4.push_back(in[i].second);break;
case 5 : in5.push_back(in[i].second);break;
case 6 : in6.push_back(in[i].second);break;
case 7 : in7.push_back(in[i].second);break;
case 8 : in8.push_back(in[i].second);break;
case 9 : in9.push_back(in[i].second);break;
case 10 : in10.push_back(in[i].second);break;
case 11 : in11.push_back(in[i].second);break;
case 12 : in12.push_back(in[i].second);break;
default: break;
}
}
for(int i=0;i<in10.size();i++) concat_in.push_back(in10[i]);
for(int i=0;i<in5.size();i++) concat_in.push_back(in5[i]);
for(int i=0;i<in12.size();i++) concat_in.push_back(in12[i]);
for(int i=0;i<in8.size();i++) concat_in.push_back(in8[i]);
for(int i=0;i<in11.size();i++) concat_in.push_back(in11[i]);
for(int i=0;i<in9.size();i++) concat_in.push_back(in9[i]);
for(int i=0;i<in7.size();i++) concat_in.push_back(in7[i]);
for(int i=0;i<in6.size();i++) concat_in.push_back(in6[i]);
for(int i=0;i<in4.size();i++) concat_in.push_back(in4[i]);
for(int i=0;i<in3.size();i++) concat_in.push_back(in3[i]);
for(int i=0;i<in2.size();i++) concat_in.push_back(in2[i]);
solve();
answer_print();
} | #line 1 "main.cpp"
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cfloat>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <string.h>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,n) for(int i=1;i<=n;i++)
#define int long long
#define ll long long
#define eps LDBL_EPSILON
#define mod (ll)1000000007
#define INF LLONG_MAX/10
#define P pair<int,int>
#define prique priority_queue
using namespace std;
int par[400010], rankh[400010], siz[400010], edge[400010];
void init(int n) {
for (int i = 0; i <= n; i++) {
par[i] = i;
rankh[i] = 0;
siz[i] = 1;
edge[i] = 0;
}
}
int find(int x) {
if (par[x] == x)return x;
else return par[x] = find(par[x]);
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y) {
edge[x]++;
return;
}
if (rankh[x] < rankh[y]) {
par[x] = y;
siz[y] += siz[x];
edge[y] += edge[x] + 1;
}
else {
par[y] = x;
if (rankh[x] == rankh[y])rankh[x]++;
siz[x] += siz[y];
edge[x] += edge[y] + 1;
}
}
bool same(int x, int y) {
return find(x) == find(y);
}
int size(int x) {
return siz[find(x)];
}
int edges(int x) {
return edge[find(x)];
}
int n, a[200010], b[200010];
map<int, int> mp;
signed main() {
cin >> n;
rep(i, n) {
cin >> a[i] >> b[i];
mp[a[i]] = 0;
mp[b[i]] = 0;
}
int cnt = 1;
for (pair<const int, int>& p : mp) {
p.second = cnt;
cnt++;
}
init(mp.size());
rep(i, n) {
unite(mp[a[i]], mp[b[i]]);
}
set<int> st;
int ans = 0;
REP(i, mp.size()) {
if (st.find(find(i)) != st.end())continue;
ans += min(edges(i), size(i));
st.insert(find(i));
}
cout << ans << endl;
}
|
#include <iostream>
using namespace std;
int main()
{
int N, M, T;
cin>>N>>M>>T;
int max=N;
int A=0,B=0,C=0;
for(int i=0;i<M;i++)
{
cin>>A;
N=N-(B>A?(B-A):(A-B));
if(N<=0)
break;
cin>>B;
N=N+(B-A);
if(N>max)
N=max;
}
N=N-(T-B);
if(N>0)
cout<<"Yes";
else
cout<<"No";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
/*
提出前に確認!
- 配列は+10ぐらい余分にとっておく
- setとmultisetを間違えない
*/
/*
デバッグ時にチェック!
- RE
配列の取りうる最大値、最小値を**すべて**チェックする
*/
int main(void)
{
int n, m, t;
cin >> n >> m >> t;
int now = n;
int pre = 0;
for (int i = 0; i < m; i++)
{
int a, b;
cin >> a >> b;
now -= a - pre;
if (now <= 0)
{
cout << "No" << endl;
return (0);
}
now += b - a;
now = min(now, n);
pre = b;
}
now -= t - pre;
if (now <= 0) cout << "No" << endl;
else cout << "Yes" << endl;
return (0);
}
|
#include<bits/stdc++.h>
using namespace std;
void solve(){
string s; cin >> s;
int go = 0, ma = 0;
for(char c : s){
if(c == 'o')go++;
if(c == '?')ma++;
}
int ans = 0;
for(int i = 0; i < 10000; ++i){
string no = to_string(i);
while(no.size() < 4)no = '0'+no;
string co = "xxxxxxxxxx";
for(char c : no){
co[c-'0']='o';
}
bool ok = 1;
for(int i = 0; i < 10; ++i){
if(co[i] == 'o' && s[i] == 'x')ok = 0;
if(co[i] == 'x' && s[i] == 'o')ok = 0;
}
ans+=ok;
}
cout << ans << "\n";
}
int main(){
solve();
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using ull = unsigned long long;
int main()
{
ull n, a; cin >> n;
ull ans = 0;
vector<ull> mod(200);
rep(i, n)
{
cin >> a;
mod.at(a % 200)++;
}
rep(i, 200)
{
if (mod.at(i) > 0) ans += mod.at(i) * (mod.at(i)-1) / 2;
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define N 200005
vector<int>tree[4*N];
void build_tree( int cur , int l , int r, int a[])
{
if( l==r )
{
tree[cur].push_back(a[l]);
return ;
}
int mid = l+(r-l)/2;
build_tree(2*cur+1 , l , mid, a); // Build left tree
build_tree(2*cur+2 , mid+1 , r, a); // Build right tree
vector<int> arr(tree[2*cur+1].size() + tree[2*cur+2].size());
merge(tree[2*cur+1].begin(), tree[2*cur+1].end(), tree[2*cur+2].begin(), tree[2*cur+2].end(), arr.begin());
tree[cur] = arr; //Merging the two sorted arrays
}
int query(int cur,int l, int r, int x, int y, int k)
{
if( r < x || l > y )
{
return 0; //out of range
}
if( x<=l && r<=y )
{
//Binary search over the current sorted vector to find elements smaller than K
return lower_bound(tree[cur].begin(),tree[cur].end(),k)-tree[cur].begin();
}
int mid=l+(r-l)/2;
return query(2*cur+1,l,mid,x,y,k)+query(2*cur+2,mid+1,r,x,y,k);
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int h, w, m;
cin >> h >> w >> m;
vector<int> row[h+1], col[w+1];
int i, mr = w+1, mc = h+1;
for(i=1;i<=m;i++)
{
int x, y;
cin >> x >> y;
row[x].push_back(y);
col[y].push_back(x);
if(x == 1)
mr = min(mr, y);
if(y == 1)
mc = min(mc, x);
}
for(i=1;i<=h;i++)
sort(row[i].begin(), row[i].end());
for(i=1;i<=w;i++)
sort(col[i].begin(), col[i].end());
// cout << "hello" << endl;
int ans = 0;
vector<pair<int, int>> vec;
int arr[h+1];
for(i=0;i<mc;i++)
arr[i] = w+1;
for(i=mc;i<=h;i++)
arr[i] = 0;
for(i=1;i<mc;i++)
{
if(row[i].size() == 0)
ans+=w, arr[i] = w+1;
else
ans+=(row[i][0] - 1), arr[i] = row[i][0];
}
// cout << ans << " " << "hello" << " " << mc << " " << mr << endl;
build_tree(0, 0, h, arr);
// cout << "hello" << endl;
for(i=2;i<mr;i++)
{
int up;
if(col[i].size() == 0)
up = h;
else
up = col[i][0] - 1;
// how many numbers in range [1, up] are less than 'i' that is count of 'k' such that arr[k] < i, 1 <= k <= up
ans+=query(0, 0, h, 1, up, i);
}
cout << ans << '\n';
} | #include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()
#include <bits/stdc++.h>
using namespace std;
char readchar() {
static char buf[1 << 15], *ptr = buf, *end = buf;
if (ptr == end) ptr = buf, end = buf + fread(buf, 1, 1 << 15, stdin);
return *ptr++;
}
void writechar(char c = 0) {
static char buf[1 << 15], *ptr = buf;
if (ptr == end(buf) or c == 0) fwrite(buf, 1, ptr - buf, stdout), ptr = buf;
*ptr++ = c;
}
template <class Z = int> Z getint() {
char c = readchar();
bool neg = c == '-';
Z res = neg ? 0 : c - '0';
while (isdigit(c = readchar())) res = res * 10 + (c - '0');
return neg ? -res : res;
}
template <class Z> void putint(Z a, char delim = '\n') {
if (a < 0) writechar('-'), a = -a;
int d[40], i = 0;
do d[i++] = a % 10; while (a /= 10);
while (i--) writechar('0' + d[i]);
writechar(delim);
}
string getstring(char delim = '\n') {
string res;
for (char c; (c = readchar()) != delim; ) res += c;
return res;
}
void putstring(string s, char delim = '\n') {
for (char c : s) writechar(c);
writechar(delim);
}
struct bipartite_matching {
vector<vector<int>> g;
vector<int> r, c, vis;
bipartite_matching(int h = 0, int w = 0) : g(h), r(h, -1), c(w, -1), vis(h) {}
void add(int i, int j) { g[i].push_back(j); }
bool dfs(int i) {
if (exchange(vis[i], true)) return false;
for (int j : g[i]) if (c[j] == -1) return r[i] = j, c[j] = i, true;
for (int j : g[i]) if (dfs(c[j])) return r[i] = j, c[j] = i, true;
return false;
}
int run() {
while (true) {
fill(begin(vis), end(vis), false);
bool updated = false;
for (int i = 0; i < (int)r.size(); ++i) if (r[i] == -1) updated |= dfs(i);
if (not updated) break;
}
return r.size() - count(begin(r), end(r), -1);
}
};
signed main(){
int n;cin>>n;
bipartite_matching g(n,400000);
REP(i,n){
int a,b;cin>>a>>b;a--;b--;
g.add(i,a);
g.add(i,b);
}
cout<<g.run()<<endl;
//putint(g.run());
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
using ll = long long;
const ll N = 1e6 + 10;
ll l, r;
cin >> l >> r;
vector<ll> cnt(N, 0);
ll ans = 0;
for (ll g = 1e6; g >= 2; g--) {
ll xk = r / g - (l - 1) / g;
ll val = xk * xk;
for (ll i = 2; i * g < N; i++) {
val -= cnt[i * g];
}
cnt[g] = val;
if (max(2LL, l) <= g and g <= r)
val -= 2 * xk - 1;
ans += val;
}
cout << ans << endl;
} | #pragma once
#include<iostream>
#include<iomanip>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<numeric>
#include<limits>
#include<bitset>
#include<functional>
#include<type_traits>
#include<queue>
#include<stack>
#include<array>
#include<random>
#include<utility>
#include<cstdlib>
#include<ctime>
using namespace std;
int main() {
long long l, r;
cin >> l >> r;
long long res = 0;
vector<int> cnt(1048576, 0);
for (long long i = 2; i <= r; i++) {
if (cnt[i] != 0) { continue; }
for (long long j = i; j <= r; j += i) { cnt[j]++; }
for (long long j = i * i; j <= r; j += i * i) { cnt[j] = -1000000007; }
}
for (long long i = 2; i <= r; i++) {
if (cnt[i] < 0) { continue; }
long long cc = (r / i) - ((l - 1) / i);
if (cnt[i] % 2) { res += (cc * (cc - 1)) / 2; }
else { res -= (cc * (cc - 1)) / 2; }
}
for (long long i = max(2ll, l); i <= r; i++) { res -= (r / i - 1); }
cout << 2 * res << '\n';
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define hi ios_base::sync_with_stdio(false);cin.tie(NULL);
typedef int64_t ll;
typedef long double ld;
typedef long long unsigned llu;
#define test int test; cin>>test; while(test--)
#define loop(i,n,k) for(int i=n;i<k;i++)
#define rloop(i,n,k) for(int i=n;i>=k;i--)
#define INT(a) int a;scanf("%d",&a);
#define intin(temp) scanf("%d",&temp);
#define longin(temp) scanf("%lld",&temp);
#define LLI(a) ll a;scanf("%lld",&a);
#define DOUBLE(a) double a;scanf("%f",&a);
#define LD(a) long double a;scanf("%lf",&a);
#define pb(i) push_back(i)
#define sz(i) i.size()
#define mkp(i,j) make_pair(i,j)
#define ip pair<int,int>
#define lp pair<ll,ll>
#define FOR(i,n) for(i=0;i<n;i++)
#define range(v) v.begin(),v.end()
#define bye return 0;
#define file freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
template<class c>
c gcd (c a,c b){
if(b==0)
return a;
gcd(b,a%b);
}
template<class c>
c lcm(c a,c b){
return a/gcd(a,b)*b;
}
template<class a>
bool prime(a n){
for(a i=2;i<=sqrt(n);i++){
if(n%i==0)
return 0;
}
return 1;
}
bool bigger(pair<int,int>picachu,pair<int,int> qicachu){
return (picachu.first<qicachu.first);
}void printarr(int arr[],int n){
for(int i=0;i<n;i++)
cout<<arr[i]<<' ';
cout<<endl;
}
int chartoint(char c) {
if (c >= 'a' && c <= 'z') return c - 'a';
if (c >= 'A' && c <= 'Z') return c - 'A' + 26;
return 52;
}
int kadane(int arr[],int n){
int maximum= arr[0];
int max_so_far = 0;
for(int i=0;i<n;i++){
max_so_far+=arr[i];
if(max_so_far>maximum)
maximum = max_so_far;
if(max_so_far<0)
max_so_far = 0;
}
return maximum;
}
const int MOD = 1e9+7;
void run(){
int n,s,d;
cin >> n >> s >> d;
int x,y;
bool flag = false;
loop(i,0,n){
cin >> x >> y;
if(x<s && y>d){
flag = true;
}
}
if(flag)
cout << "Yes\n";
else
cout << "No\n";
}
int main(){
hi;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("error.txt", "w", stderr);
freopen("output.txt", "w", stdout);
#endif
{
run();
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl;
bye;
} | #include <iostream>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <queue>
#include <map>
using namespace std;
typedef long long int ll;
typedef pair <int,int> pii;
typedef pair<ll,ll> pll;
/*bool compare_function(const pair<int, int> a, const pair<int, int> b)
{
return a.first < b.first;
}*/
// use case
//sort(V.begin(), V.end(), compare_function);
/*struct compare
{
bool operator ()(const pair<int, int> &a, const pair<int, int> &b) {
if (a.second-a.first==b.second-b.first)
{
return a.first>b.first;
}
return a.second-a.first < b.second-b.first;}
};
priority_queue <pair<int,int>, vector <pair<int,int>>, compare> Q;*/
/*void print1(vector <int> X)
{
for (int i=0; i<X.size(); i++)
{
cout<<X[i]<<" ";
}
cout<<endl;
return;
}
void print2(vector <vector <int>> X)
{
for (int i=0; i<X.size(); i++)
{
for (int j=0;j<X[i].size();j++) {
cout<<X[i][j]<<" ";}
cout<<endl;}
return;
}*/
ll solve(vector <ll> &A, vector <int> &T, int n, ll a) {
ll ats=a;
for (int i=0;i<n;i++) {
if (T[i]==1) {
ats+=A[i];
}
else {
if (T[i]==2) {
ats=max(ats,A[i]);
}
else {ats=min(ats,A[i]);}
}
}
return ats;
}
ll s(ll a,ll maz, ll did, ll suma, ll r1, ll r2) {
if (a<=maz) {return r1;}
if (a>=did) {return r2;}
return a+suma;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin>>n;
vector <ll> A(n);
vector <int> T(n);
for (int i=0;i<n;i++) {cin>>A[i]>>T[i];}
int q;
cin>>q;
vector <ll> X(q);
set<ll> Q;
for (int i=0;i<q;i++) {cin>>X[i];Q.insert(X[i]);}
ll maz=*Q.begin()-1;
ll did=*Q.rbegin()+1;
ll r1=solve(A,T,n,maz+1);
ll r2=solve(A,T,n,did-1);
ll suma=0;
vector <ll> Y;
for (ll x:Q) {Y.push_back(x);}
sort(Y.begin(),Y.end());
int ind1=0;
int ind2=(int)Y.size()-1;
while (ind2-ind1>2) {
int c=(ind1+ind2)/2;
if (solve(A,T,n,Y[c])>r1) {ind2=c;}
else {ind1=c;}
}
while (solve(A,T,n,Y[ind2])>r1) {ind2--;}
maz=Y[ind2];
ind1=0;
ind2=(int)Y.size()-1;
while (ind2-ind1>2) {
int c=(ind1+ind2)/2;
if (solve(A,T,n,Y[c])>=r2) {ind2=c;}
else {ind1=c;}
}
while (solve(A,T,n,Y[ind1])<r2) {ind1++;}
did=Y[ind1];
//cout<<ind1-1<<" "<<solve(A,T,n,Y[ind1-1])<<" "<<Y[ind1-1]<<endl;
if (ind1>0) {
suma=solve(A,T,n,Y[ind1-1])-Y[ind1-1];
}
//cout<<maz<<" "<<did<<" "<<r1<<" "<<r2<<" "<<suma<<endl;
for (int i=0;i<q;i++) {
ll a=X[i];
ll ats=s(a,maz,did,suma,r1,r2);
cout<<ats<<endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int a, b;
cin >> a >> b;
int x = (a + b) / 2, y = (a - b) / 2;
cout << x << " " << y;
} | // https://atcoder.jp/contests/arc104/tasks/arc104_a
#include <bits/stdc++.h>
using namespace std;
// #define cerr if(false) cerr
#define watch(x) cerr << (#x) << " is " << (x) << endl;
#define endl '\n'
#define ld long double
#define int long long
#define pii pair<int, int>
#define fi first
#define se second
#define sz(a) (int)a.size()
#define all(x) (x).begin(), (x).end()
void solve() {
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int a, b;
cin >> a >> b;
for(int x = -1000; x <= 1000; x++) {
for(int y = -1000; y <= 1000; y++) {
if(x+y == a and x-y == b) {
cout << x << " " << y << endl;
return 0;
}
}
}
}
/*
*/
// Did you read the bounds?
// Did you make typos?
// Are there edge cases (N=1?)
// Are array sizes proper (scaled by proper constant, for example 2* for koosaga tree)
// Integer overflow?
// DS reset properly between test cases?
// Is using long longs causing TLE?
// Are you using floating points?
|
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;
#define LETS_GET_SCHWIFTY ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ff first
#define ss second
#define int long long
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define vi vector<int>
#define pqb priority_queue<int>
#define pqs priority_queue<int,vi,greater<int> >
#define setbits(x) __builtin_popcountll(x)
#define zerobits(x) __builtin_ctzll(x)
#define mod 998244353
#define inf 1e18
#define ps(x,y) fixed<<setprecision(y)<<x
#define vpii vector<pair<int,int> >
#define all(x) x.begin(),x.end()
#define matrixprint(arr,a,b,c,d) for(int i=a;i<=c;i++){for(int j=b;j<=d;j++){cout<<arr[i][j]<<" ";}cout<<"\n";}
#define show(arr,x,y) for(int i=x;i<=y;i++){cout<<arr[i]<<" ";}cout<<"\n"
#define sz(x) (int)x.size()
#define db(x) cout<<x<<"\n";
typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
//insert,find_by_order,order_of_key,lower_bound,upper_bound;
#define TRACE
#ifdef TRACE
#define deb(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
#else
#define deb(...)
#endif
//////////////////////////////code//////////////////////////////
const int N = 200;
void solve()
{
int n , k;
cin >> n >> k;
for(int i = 0;i < n; i++)
{
int d;
cin >> d;
if(d != k)
cout << d << " ";
}
}
int32_t main()
{
LETS_GET_SCHWIFTY;
#ifndef ONLINE_JUDGE
freopen("INP.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int t = 1;
// cin >> t;
while (t--)
solve();
}
// check out for following mistakes-
// if using pb operation on vector and then trying to access index..check if sizeof that vec could remain 0 only
// is using prime sieve make sure it fits
// when using factorial template or combinatorics make sure that you edit fillfac fun values and array values
| #pragma region header
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n)for(int i=0;i<(n);i++)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define pb push_back
#define int ll
#define each(i, a) for (auto &&i : (a))
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using vvs = vector<vs>;
using vd = vector<ld>;
using vvd = vector<vd>;
using vb = vector<bool>;
using vvb = vector<vb>;
using P = pair<int, int>;
using vp = vector<P>;
using int128 = __int128_t;//cin coutはできない
template <class T>
using greater_queue = priority_queue<T, vector<T>, greater<T>>;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a / gcd(a, b) * b;};
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
template <class T>
void CVEC(const T &v) {
int c = v.size() - 1;
for (int i = 0; i < c; i++) cout << v[i] << ' ';
if (c > -1) cout << v[c];
cout << '\n';
}
#pragma endregion header
signed main(){
int n, k; cin >> n >> k;
int now = k;
int pos = 0;
vector<P> a(n);
rep(i,n) cin >> a[i].first >> a[i].second;
sort(ALL(a));
rep(i,n){
if(now >= a[i].first - pos){
now -= (a[i].first - pos);
pos = a[i].first;
now += a[i].second;
}else{
break;
}
}
pos += now;
cout << pos << endl;
} |
#include<bits/stdc++.h>
using namespace std;
const int N=3005;
const int MOD=998244353;
int n,m,p=0,q=1;
int dp[N][N];
void add(int &x,int y){
x+=y;
if(x>=MOD) x-=MOD;
}
int main(){
scanf("%d%d",&n,&m);
dp[n][m]=1;
for(int j=n;j>=0;j--){
for(int k=0;k<=j;k++){
if(k%2==0) add(dp[j][k],dp[j][k/2]);
add(dp[j][k],dp[j+1][k+1]);
}
}
printf("%d\n",dp[0][0]);
return 0;
} | #pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp> //required
//#include <ext/pb_ds/tree_policy.hpp> //required
//using namespace __gnu_pbds; //required
using namespace std;
//template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// ordered_set <int> s;
// s.find_by_order(k); returns the (k+1)th smallest element
// s.order_of_key(k); returns the number of elements in s strictly less than k
#define MOD (998244353) // change as required
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define all(x) x.begin(), x.end()
#define print(vec,l,r) for(int i = l; i <= r; i++) cout << vec[i] <<" "; cout << endl;
#define input(vec,N) for(int i = 0; i < (N); i++) cin >> vec[i];
#define debug(x) cerr << #x << " = " << (x) << endl;
#define leftmost_bit(x) (63-__builtin_clzll(x))
#define rightmost_bit(x) __builtin_ctzll(x)
#define set_bits(x) __builtin_popcountll(x)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long int ll;
// highly risky #defines
#define int ll // disable when you want to make code a bit faster
#define endl '\n' // disable when dealing with interactive problems
const int MAXN = 3002;
int dp[MAXN][2*MAXN];
void solve(){
// code starts from here
int N, K;
cin >> N >> K;
for(int i = 0; i < MAXN; i++){
dp[i][i] = 1;
dp[i][0] = 0;
}
for(int i = 0; i < MAXN; i++){
for(int j = i+1; j < 2*MAXN; j++) dp[i][j] = 0;
}
for(int i = 2; i <= N; i++){
for(int j = N; j >= 1; j--){
dp[i][j] = dp[i-1][j-1] + dp[i][2*j];
dp[i][j] %= MOD;
}
}
cout << dp[N][K] << endl;
}
clock_t startTime;
double getCurrentTime() {
return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//startTime = clock();
int T = 1;
// cin >> T;
while(T--){
solve();
}
//cerr << getCurrentTime() << endl;
return 0;
}
|
#include<bits/stdc++.h>
#define rep(i,n) for (int i=0; i<n; i++)
#define REP(i,x,n) for (int i=x; i<n; i++)
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
#define P pair<ll,ll>
#define T tuple<int, int, int>
#define PP pair<P,P>
#define ALL(a) a.begin(), a.end()
int main(){
int n, m, q;
cin >> n >> m >> q;
vi w(n), v(n);
rep(i,n) cin >> w[i] >> v[i];
vi x(m);
rep(i,m) cin >> x[i];
rep(i,q){
int l, r;
cin >> l >> r;
l--; r--;
auto X = x;
REP(j,l,r+1){
X[j] = 1e9;
}
sort(ALL(X));
vector<bool> used(n, false);
int ans = 0;
rep(j,m){
if (X[j] == 1e9) continue;
int index = -1;
int MAX = 0;
rep(k,n){
if (used[k]) continue;
if (w[k] <= X[j] && v[k] > MAX){
index = k;
MAX = v[k];
}
}
if (index == -1) continue;
used[index] = true;
ans += MAX;
}
cout << ans << endl;
}
} | #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<utility>
#include<set>
#include<stack>
#include<list>
#include<deque>
#include<bitset>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<climits>
#include<cmath>
#include<cctype>
#define pb push_back
#define mp make_pair
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ren(i,a,b) for(int i=a;i>=b;i--)
#define ff first
#define ss second
#define pll pair<long long int,long long int>
#define pii pair<int,int>
#define vll vector<long long int>
#define vii vector<int>
#define gi(n) scanf("%d",&n)
#define gll(n) scanf("%lld",&n)
#define gstr(n) scanf("%s",n)
#define gl(n) cin >> n
#define oi(n) printf("%d",n)
#define oll(n) printf("%lld",n)
#define ostr(n) printf("%s",n)
#define ol(n) cout << n
#define os cout<<" "
#define on cout<<"\n"
#define o2(a,b) cout<<a<<" "<<b
#define all(n) n.begin(),n.end()
#define present(s,x) (s.find(x) != s.end())
#define cpresent(s,x) (find(all(s),x) != s.end())
#define tr(container, it) for(__typeof(container.begin()) it = container.begin(); it != container.end(); it++)
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef long double ld;
typedef vector<vector<ll> > mat;
ll mod=1e9+7;
mat I;
mat mul(mat a,mat b)
{
int n=a.size();
mat c(n,vll(n));
rep(i,0,n-1)rep(j,0,n-1)rep(k,0,n-1)
{
c[i][j]=(c[i][j]+a[i][k]*b[k][j])%mod;
}
return c;
}
ll p(ll a,ll b)
{
ll r=1;
while(b)
{
if(b%2)r=(r*a)%mod;
a=(a*a)%mod;
b/=2;
}
return r;
}
mat p(mat a,ll b)
{
mat r=I;
while(b)
{
if(b%2)
r=mul(r,a);
a=mul(a,a);
b/=2;
}
return r;
}
ll n,m,k,e[105][105],a[105];
int main()
{ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m>>k;
I.resize(n);
rep(i,0,n-1)
{
I[i].resize(n);
I[i][i]=1;
}
rep(i,0,n-1)cin>>a[i];
rep(i,0,m-1)
{
int x,y;
cin>>x>>y;
x--;y--;
e[x][y]=1;
e[y][x]=1;
}
mat prod(n,vll(n));
rep(i,0,n-1)
{
ll ed=0;
rep(j,0,n-1)ed+=e[i][j];
prod[i][i]=(2*m-ed)%mod;
rep(j,0,n-1)if(e[i][j])prod[i][j]=1;
}
prod=p(prod,k);
ll den=p(2*m,k);
den=p(den,mod-2);
rep(i,0,n-1)
{
ll ans=0;
rep(j,0,n-1)
{
ans=(ans+prod[i][j]*a[j])%mod;
}
ans=(ans*den)%mod;
ol(ans);on;
}
return 0;
}
|
/**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author
*/
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <fstream>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rrep(i, m, n) for (int i = (int)(m); i > (int)(n); --i)
using namespace std;
template <typename T, typename U>
pair<T, U> tp(T a, U b) {
return std::make_pair(a, b);
}
constexpr int MOD = 1000000007;
class ARockPaperScissors {
public:
void solve(std::istream& cin, std::ostream& cout) {
int64_t x, y;
cin >> x;
if(int(x*1.08) < 206){
cout << "Yay!" << endl;
}else if(int(x*1.08) == 206){
cout << "so-so" << endl;
}else{
cout << ":(" << endl;
}
}
};
int main() {
ARockPaperScissors solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
solver.solve(in, out);
return 0;
}
| #include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
using ll=long long;
using ld=long double;
using pll=pair<ll, ll>;
//using mint = modint1000000007;
#define rep(i,n) for (ll i=0; i<n; ++i)
#define all(c) begin(c),end(c)
#define PI acos(-1)
#define oo 2e18
template<typename T1, typename T2>
bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}
template<typename T1, typename T2>
bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}
//priority_queue<ll, vector<ll>, greater<ll>> Q;
/*
*/
ll N;
bool is_sorted(ll G[])
{
rep(i, N-1){
if (G[i] > G[i+1]) return false;
}
return true;
}
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
cout << fixed << setprecision(10);
ll T;
cin >> T;
rep(i, T){
cin >> N;
ll G[N];
ll cnt = 0;
vector<ll> ans;
rep(j, N) cin >> G[j];
while (!is_sorted(G)){
ll tmp = -1;
if (cnt%2 == 0){
for(ll k=0; k+1<N; k+=2){
if (G[k] < G[k+1]) continue;
tmp = k;
}
if (tmp == -1) tmp = 0;
}
if (cnt%2 == 1){
for(ll k=1; k+1<N; k+=2){
if (G[k] < G[k+1]) continue;
tmp = k;
}
if (tmp == -1) tmp = 1;
}
swap(G[tmp], G[tmp+1]);
ans.push_back(tmp+1);
cnt++;
}
ll asz = ans.size();
cout << asz << endl;
rep(j, asz){
cout << ans[j];
if (j == asz-1) cout << endl;
else cout << " ";
}
}
}
|
//#define DEBUG
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5;
const int INF = 0x3f3f3f3f, MOD = 1000000007;
const long long LINF = LONG_LONG_MAX;
#define INIT(arr, val) fill(arr, arr+(int)(sizeof(arr)/sizeof(arr[0])), val)
#define REP(i, lb, rb, inc) for(int i = (lb); i < (rb); i += inc)
#define RREP(i, rb, lb, dec) for(int i = (rb)-1; i >= (lb); i -= dec)
typedef long long ll;
typedef queue<int> QI;
typedef vector<int> VI;
#define pb push_back
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
#define X real()
#define Y imag()
#define endl '\n'
#define dbg(a, b) cerr << "dbg: a " << b << endl;
#define dbg_itv(a, b, c) cerr << "dbg_itv: " << a << " " << b << " : " << c << endl
#define IOS() cin.tie(0); cout.sync_with_stdio(0); cout.tie(0)
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("O3")
PII ext_gcd(int a, int b) {
if (b == 0) return {1, 0};
else {
int k = a/b;
PII xy = ext_gcd(b, a%b);
return {xy.S, xy.F - k*xy.S};
}
}
signed main() {
IOS();
int t;
cin >> t;
while(t--) {
ll n, s, k;
cin >> n >> s >> k;
ll g = __gcd(n, __gcd(s, k));
n /= g, s /= g, k /= g;
if (s % __gcd(k, n) != 0) {
cout << "-1\n";
continue;
}
ll x = ext_gcd(k, n).F;
if (x < 0) x += n;
cout << (x*(n-s))%n << endl;
}
#ifdef DEBUG
cerr << "\nDebug Info:\n--------------------------------\n";
#endif
} | #include <bits/stdc++.h>
#define LL long long
#define PII pair<int,int>
#define PIL pair<int,LL>
#define PLI pair<LL,int>
#define PIII pair<int,PII>
#define PLL pair<LL,LL>
#define PLII pair<LL,PII>
#define VI vector<int>
#define VVI vector<VI>
#define VL vector<LL>
#define VVL vector<VL>
#define VPII vector<PII>
#define FF first
#define SS second
#define MP make_pair
#define PB push_back
#define sqr(x) ((x) * (x))
#define all(x) x.begin(),x.end()
#define watch(x) cout<<(#x)<<" = "<<(x)<<'\n'
#define mset(a,v) memset(a,v,sizeof(a))
#define setp(x) cout<<fixed<<setprecision(x)
#define EPS 0.00000000001
#define PI acos(-1)
#define loop(i,b,n) for(int i=b;i<n;++i)
#define rev_loop(i,b,n) for(int i=b;i>=n;--i)
using namespace std;
const int MOD = 1e9 + 7;
const LL MX = 1e9;
const LL INF = 1e9;
int main()
{
//ofstream out("output.txt");
//ifstream in("input.txt");
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n;
cin>>n;
int a;
set<int> st;
loop(i,0,n)
{
cin>>a;
st.insert(a);
}
LL ans = 1, p = 0;
for(int x : st)
{
ans = (ans * (x - p + 1)) % MOD;
p = x;
}
cout<<ans<<'\n';
return 0;
}
|
#ifdef LOCAL
#pragma warning(disable:4996)
#pragma warning(disable:4244)
#pragma warning(disable:4554)
#define gets gets_s
#endif
#include<map>
#include<set>
#include<list>
#include<cmath>
#include<stack>
#include<queue>
#include<cmath>
#include<vector>
#include<string>
#include<cctype>
#include<cstdio>
#include<bitset>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<assert.h>
#define ll long long
#define cl(a,x) memset((a),(x),sizeof(a))
#define lson 2*i
#define rson 2*i+1
#define sf scanf
#define ull unsigned long long
#define lowbit(x) (x)&(-x)
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pdd pair<double,double>
#define ti tree[i]
#define endl "\n"
#define debug(num) #num << "=" << num << " "
ll q_pow(ll x, ll n, ll mod) { ll ret = 1; while (n) { if (n & 1) ret = x * ret%mod; x = x * x%mod; n >>= 1; }return ret; }
ll __gcd(ll x, ll y) { if (!y) return x; return __gcd(y, x%y); }
ll getbit(ll now) { int cnt = 0; while (now) { cnt++; now -= lowbit(now); }return cnt; }
int dx[] = { -1,1,0,0 };//上下左右
int dy[] = { 0,0,-1,1 };//
using namespace std;
const ll maxn = 1e5 + 10;
const ll INF = 0x3f3f3f3f;
const ll IINF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-9;
int mod = 1e9 + 7;
void add(ll &a, ll num) { a += num; if (a >= mod) a -= mod; }
vector<int> edge[maxn];
int dp[maxn];
int sz[maxn];
int n;
void dfs(int pos, int fa) {
sz[pos] = 1;
dp[pos] = 1;
int deltaa = 0, deltab = 0;
vector<int> tmp;
for (auto to : edge[pos]) {
if (to == fa) continue;
dfs(to, pos);
sz[pos] += sz[to];
if (sz[to] % 2 == 0) {//子树大小为偶数
if (sz[to] - dp[to] > dp[to]) {//后手赢的多,必然先走
dp[pos] += dp[to];
}
else {//先手赢的多,看最后谁先走这里
deltaa += dp[to];
deltab += sz[to] - dp[to];
}
}
else {//子树大小为奇数,先后手顺序变化
tmp.emplace_back(to);
}
}
sort(tmp.begin(),tmp.end(),[&](const int x,const int y){
return sz[x] - dp[x] - dp[x] > sz[y] - dp[y] - dp[y];//谁后手相对先手赢的多,就先走哪个
});
for (int i = 0; i < tmp.size(); i++) {
if (i % 2 == 1) dp[pos] += sz[tmp[i]] - dp[tmp[i]];
else dp[pos] += dp[tmp[i]];
}
if (tmp.size() % 2 == 0) {//说明此时先手边后手,先拿好东西
dp[pos] += deltaa;
}
else dp[pos] += deltab;
//cerr << debug(pos) << debug(dp[pos]) << endl;
}
void solve() {
cin >> n;
int num;
for (int i = 2; i <= n; i++) {
cin >> num;
edge[num].emplace_back(i);
edge[i].emplace_back(num);
}
dfs(1, 1);
cout << dp[1] << endl;
}
signed main(int argc, char *argv[]) {
if (argc == 2 && strcmp("-debug", argv[1]) == 0) {
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
}
cout << fixed << setprecision(20); ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int t = 1;
//cin >> t;
while (t--) {
solve();
}
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 CL(A,I) (memset(A,I,sizeof(A)))
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
ll n;
gr g;
pll rec(ll u) { // ret (what takes Taker, odd/even)
ll ret = 1, cnt = 1;
vector<vll> o,e;
for(auto v: g[u]) {
auto [val, sz] = rec(v);
if(sz&1) o.pb({2*val-sz, val,sz-val});
else e.pb({2*val-sz, val,sz-val});
cnt += sz;
}
sort(all(o));
F(o.size()) ret += o[i][1+(i&1)];
F(e.size()) if(e[i][0]<=0) {
ret += e[i][1];
} else {
ret += e[i][1+(o.size()&1)];
}
return {ret, cnt};
}
int main(void) {
ios_base::sync_with_stdio(false);
cin >> n;
g = gr(n);
F(n-1) {
ll x; cin >> x; x--;
g[x].pb(i+1);
}
cout << rec(0).x << endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define printV(v) for(int i=0;i<(int)v.size();i++){cout << v[i]<< " ";} cout << endl
#define printRev(v) for(int i=(int)v.size()-1;i >= 0;i--){cout << v[i] << " ";} cout << endl
#define sortV(v) sort(v.begin(),v.end())
#define revV(v) reverse(v.begin(),v.end())
ll a,b,c,d,e,f,g,n,m,k,t,x,h,w,p,q,y;
string str = "", s1 = "", s2 = "";
ll mod = 1e9 + 7;
//ll mod = 998244353;
const int N = 100009;
/**
2021 challenge (16/365) : 16.01.2021
*/
void solve(){
cin >> n >> k;
vector<ll> freq(n+100,0);
for(int i=0; i < n; i++){
cin >> a;
freq[a]++;
}
int active = k;
int curr = 0;
ll sum = 0;
for(int i=0; i <= n; i++){
if(active == 0) break;
if(freq[i] < active){
int diff = active - freq[i];
sum += (diff * i);
active = freq[i];
}
}
cout << sum << endl;
}
int main(){
ios_base::sync_with_stdio(false);
int cases = 1;
//cin >> cases;
while(cases--) solve();
return 0;
} | #include <bits/stdc++.h>
#define int long long
#define sc(x) scanf("%lld",&x);
#define P pair<int,int>
#define fi first
#define se second
#define pb push_back
#define endl '\n'
using namespace std;
int N;
int A[300006];
int B[300005];
signed main()
{
int n,k;
sc(n)sc(k)
int x;
for(int i = 0; i < n; i++){
sc(x)
A[x]++;
}
int ans = 0,r;
for(int i = 0; i < n; i++){
if(A[i] >= k){
continue;
}else{
r = k - A[i];
ans += r*i;
k -= r;
}
if(k == 0)break;
}
cout << ans << '\n';
return 0;
}
|
#include <iostream>
#include <set>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
set<pair<int,int>> s;
set<int> st;
int a[100010],b[100010],d[100010] = {};
vector<int> v;
int main(){
int i,j,n,l; cin >> n >> l;
for(i=1;i<=n;i++) cin >> a[i];
for(i=1;i<=n;i++) cin >> b[i];
for(i=1;i<=n;i++) d[i] = b[i] - a[i];
a[0] = 0; a[i] = l + 1;
for(i=0;i<=n + 1;i++){
s.insert({a[i] - i,i}); st.insert(a[i] - i);
}
for(i=0;i<=n + 1;i++){
if(d[i]==0) v.push_back(i);
}
ll ans = 0;
for(i=1;i<v.size();i++){
vector<pair<ll,ll>> v2,v3;
for(j=v[i - 1] + 1;j<v[i];j++){
if(!st.count(a[j] - j + d[j])){
cout << -1 << endl;
return 0;
}
if(d[j]<0) v2.push_back({(*(--s.upper_bound({a[j] - j + d[j],1000000000}))).second,j});
else v3.push_back({(*s.lower_bound({a[j] - j + d[j],-1})).second,j});
}
for(j=0;j<(int)v2.size() - 1;j++){
if(v2[j + 1].second - v2[j].second>1){
cout << -1 << endl;
return 0;
}
}
for(j=0;j<(int)v3.size() - 1;j++){
if(v3[j + 1].second - v3[j].second>1){
cout << -1 << endl;
return 0;
}
}
if(v2.size()){
ll cnt = 0,x = v2.back().first;
for(j=v2.size() - 1;j>=0;j--){
if(x!=v2[j].first){
ans += cnt; cnt = 0;
}
cnt = max(cnt,v2[j].second - v2[j].first);
x = v2[j].first;
}
ans += cnt;
}
if(v3.size()){
ll cnt = 0,x = v3[0].first;
for(j=0;j<v3.size();j++){
if(x!=v3[j].first){
ans += cnt; cnt = 0;
}
cnt = max(cnt,v3[j].first - v3[j].second);
x = v3[j].first;
}
ans += cnt;
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;
#define rep(i,n) for(int i=0;i<int(n);i++)
#define rep1(i,n) for(int i=1;i<=int(n);i++)
#define per(i,n) for(int i=int(n)-1;i>=0;i--)
#define per1(i,n) for(int i=int(n);i>0;i--)
#define all(c) c.begin(),c.end()
#define si(x) int(x.size())
#define pb emplace_back
#define fs first
#define sc second
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
template<class T,class U> void chmax(T& x, U y){if(x<y) x=y;}
template<class T,class U> void chmin(T& x, U y){if(y<x) x=y;}
template<class T> void mkuni(V<T>& v){sort(all(v));v.erase(unique(all(v)),v.end());}
template<class T> int lwb(const V<T>& v, const T& a){return lower_bound(all(v),a) - v.begin();}
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
o<<"{";
for(const T& v:vc) o<<v<<",";
o<<"}";
return o;
}
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }
#ifdef LOCAL
#define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
void dmpr(ostream& os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
os<<t<<" ~ ";
dmpr(os,args...);
}
#define shows(...) cerr << "LINE" << __LINE__ << " : ";dmpr(cerr,##__VA_ARGS__)
#define dump(x) cerr << "LINE" << __LINE__ << " : " << #x << " = {"; \
for(auto v: x) cerr << v << ","; cerr << "}" << endl;
#else
#define show(x) void(0)
#define dump(x) void(0)
#define shows(...) void(0)
#endif
int main(){
cin.tie(0);
ios::sync_with_stdio(false); //DON'T USE scanf/printf/puts !!
cout << fixed << setprecision(20);
int N;
double D,H;
cin >> N >> D >> H;
double ans = 0;
while(N--){
double d,h; cin >> d >> h;
chmax(ans,h-(H-h)/(D-d)*d);
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
long long int a[50] = {0} , b[50] = {0};
int main() {
// your code goes here
int n;
long long int t;
cin >> n >> t;
int x = n/2;
for(int i = 0 ; i < x ; i++){
cin >> a[i];
}
for(int i = 0 ; i < n-x ; i++){
cin >> b[i];
}
vector<long long int > v , u;
for(int i = 0 ; i < (1 << x) ; i++){
long long int val = 0;
for(int j = 0 ; j < x ; j++){
if((i&(1<<j)))
val += a[j];
}
v.push_back(val);
}
for(int i = 0 ; i < (1 << (n-x)) ; i++){
long long int val = 0;
for(int j = 0 ; j < (n-x) ; j++){
if((i&(1<<j)))
val += b[j];
}
u.push_back(val);
}
sort(u.begin() , u.end());
long long int ans = 0 , k;
for(int i = 0 ; i < v.size() ; i++){
k = t - v[i];
if(k <= 0) continue;
int f = upper_bound(u.begin() , u.end() , k) - u.begin();
f--;
if(f < 0) continue;
else ans = max(ans , v[i] + u[f]);
}
cout << ans << endl;
return 0;
}
|
/**
* Coded by : lucky_21
* --------Lokesh Singh
**/
#include<bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T> using oset=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
#define F first
#define S second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
#define fix fixed<<setprecision(10)
#define rep(i,a,b) for(int i=int(a);i<=int(b);i++)
#define repb(i,b,a) for(int i=int(b);i>=int(a);i--)
#define FastIO ios_base::sync_with_stdio(0),cin.tie(0)
typedef double db;
typedef long long ll;
const int N=2e5+5;
const int mod=1e9+7;
ll n,t,w[N],ans;
void rec(int i,int n,vector<ll>&s,ll sum){
if(i>n){
if(sum<=t){
ans=max(ans,sum);
s.pb(sum);
}
return;
}
rec(i+1,n,s,sum);
rec(i+1,n,s,sum+w[i]);
}
signed main(){
FastIO;
cin>>n>>t;
rep(i,1,n) cin>>w[i];
vector<ll>a,b;
rec(1,n/2,a,0);
rec(n/2+1,n,b,0);
sort(all(a));
sort(all(b));
for(ll i:a){
auto it=ub(all(b),t-i);
if(it==b.begin()) continue;
--it;
if(*it+i<=t) ans=max(ans,*it+i);
}
cout<<ans;
return 0;
}
|
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=401000;
int n;
int a[maxn],rk[maxn];
bool type[maxn];
int scnt,stype;
inline int read(){
int res=0;
char ch=getchar(),ch1=ch;
while(!isdigit(ch))ch1=ch,ch=getchar();
while(isdigit(ch))res=res*10+ch-'0',ch=getchar();
return ch1=='-'?-res:res;
}
int main(){
n=read();
for(int i=1;i<=(n<<1);++i)rk[i]=a[i]=read();
sort(rk+1,rk+1+(n<<1));
int cnt=0;
for(int i=1;i<=(n<<1);++i){
if(a[i]>rk[n+1]){
++cnt;type[i]=true;
}
}
for(int i=1;i<=(n<<1);++i){
if(a[i]==rk[n+1]){
++cnt;type[i]=true;
if(cnt==n)break;
}
}
for(int i=1;i<=(n<<1);++i){
if(!scnt){
++scnt;stype=type[i];
putchar('(');
}
else{
if(stype!=type[i])--scnt,putchar(')');
else ++scnt,putchar('(');
}
}
return 0;
} | #include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a,i##end=b;i<=i##end;i++)
#define drep(i,a,b) for(int i=a,i##end=b;i>=i##end;i--)
inline int read() {
int x=0,f=1;char c=getchar();
while(c<48||c>57){if(c=='-')f=-1;c=getchar();}
while(c>=48&&c<=57)x=(x<<1)+(x<<3)+(c^48),c=getchar();
return x*f;
}
using namespace std;
typedef long long ll;
const int M=1e6+5,mod=1e9+7;
vector <int> E[M];
int A[M],B[M];
int n;
int Q(int a,int b=mod-2){
int ans=1;
for(;b;a=1ll*a*a%mod,b>>=1)if(b&1)ans=1ll*ans*a%mod;
return ans;
}
void init(int m) {
rep(i,1,m){
int u=read(),v=read();
E[u].push_back(v),E[v].push_back(u);
}
}
void graph(){n=read();init(read());}
void tree(){n=read();init(n-1);}
int Mod[M],rest[M];
void exgcd(ll a,ll b,ll &x,ll &y){
if(!b){
x=1,y=0;
return ;
}
exgcd(b,a%b,y,x);
y-=a/b*x;
}
ll inv(ll a,ll b){
ll x,y;
exgcd(a,b,x,y);
return (x%b+b)%b;
}
void CRT() {
ll mul=1;
rep(i,1,n)mul*=Mod[i];
ll ans=0;
rep(i,1,n){
ll m=mul/Mod[i],tmp=inv(m,rest[i]);
(ans+=Mod[i]*m*tmp)%=mul;
}
}
struct node {
int id;ll num;
bool operator <(const node &a)const {
if(num!=a.num)return num>a.num;
return id<a.id;
}
};
int T[M];
int query(int x,int ans=0){
while(x<=n)ans+=T[x],x+=x&-x;
return ans;
}
void add(int x){
while(x)T[x]++,x-=x&-x;
}
int mk[M],st[M],mat[M];
void solve() {
n=read()<<1;
rep(i,1,n)A[i]=B[i]=read();
sort(B+1,B+1+n);
int mid=n/2,tot=0;
rep(i,1,n)if(tot<mid&&A[i]<B[mid])tot++,mk[i]=1;
rep(i,1,n)if(tot<mid&&A[i]==B[mid])tot++,mk[i]=1;
bool op;int tp=0;
rep(i,1,n){
if(mk[i]){
if(!tp)st[++tp]=i,op=1;
else if(op==1)st[++tp]=i;
else {
int tmp=st[tp];
mat[tmp]=i;
mat[i]=tmp;
tp--;
}
}else {
if(!tp)st[++tp]=i,op=0;
else if(op==0)st[++tp]=i;
else {
int tmp=st[tp];
mat[tmp]=i;
mat[i]=tmp;
tp--;
}
}
}
rep(i,1,n){
if(mat[i]>i)putchar('(');
else putchar(')');
}
}
int main() {
int T=1;
while(T--)solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int mn = 1e9, mx = -1e9;
for(int i=0;i<n;++i){
int x; cin >> x;
mx = max(mx, x);
}
for(int i=0;i<n;++i){
int x; cin >> x;
mn = min(mn, x);
}
cout << (mn < mx ? 0 : mn-mx+1);
cout << endl;
return 0;
} | #include <bits/stdc++.h>
#define LL long long
#define PII pair<int,int>
#define PIL pair<int,LL>
#define PLI pair<LL,int>
#define PIII pair<int,PII>
#define PLL pair<LL,LL>
#define PLII pair<LL,PII>
#define VI vector<int>
#define VVI vector<VI>
#define VL vector<LL>
#define VVL vector<VL>
#define VPII vector<PII>
#define FF first
#define SS second
#define MP make_pair
#define PB push_back
#define sqr(x) ((x) * (x))
#define all(x) x.begin(),x.end()
#define watch(x) cout<<(#x)<<" = "<<(x)<<'\n'
#define mset(a,v) memset(a,v,sizeof(a))
#define setp(x) cout<<fixed<<setprecision(x)
#define EPS 0.00000000001
#define PI acos(-1)
#define loop(i,b,n) for(int i=b;i<n;++i)
#define rev_loop(i,b,n) for(int i=b;i>=n;--i)
using namespace std;
const int MOD = 1e9 + 7;
const LL MX = 1e9;
const LL INF = 1e9;
int main()
{
//ofstream out("output.txt");
//ifstream in("input.txt");
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n;
cin>>n;
int a[n], b[n];
loop(i,0,n) cin>>a[i];
loop(i,0,n) cin>>b[i];
int mn = a[0], mx = b[0];
loop(i,1,n)
{
mn = max(mn,a[i]);
mx = min(mx,b[i]);
}
int ans = max(0,mx-mn+1);
cout<<ans<<'\n';
return 0;
}
|
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("O2")
#include"bits/stdc++.h"
using namespace std;
const int p = 31;
const int M = 998244353;
typedef long long ll;
typedef double d;
typedef long double ld;
typedef unsigned long long ull;
#define ar array
#define ff first
#define ss second
#define vt vector
mt19937_64 rnd(time(NULL));
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
ll euclid(ll a, ll b, ll &x, ll &y) {if (!b) return x = 1, y = 0, a; ll d = euclid(b, a % b, y, x); return y -= (a / b) * x, d;}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int l, r;
cin >> l >> r;
ll ans = 0ll;
if (l == r) {
cout << 0;
return 0;
}
vector<ll>pf(r + 3);
for (int i = r + 1; i >= 2; --i) {
ll dif = (r / i - (l + i - 1) / i + 1);
pf[i] = dif*dif;
ll now = 2 * i;
for (; now <= r; now += i) {
pf[i] -= pf[now];
}
}
for(int i=2;i<=r;++i){
ans+=pf[i];
if(i>=l){
ans-=2*((r / i - (l + i - 1) / i + 1));
ans+=1;
}
}
cout<<ans<<'\n';
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"; //check time
return 0;
}
| #include <bits/stdc++.h>
#define ff first
#define ss second
#define ll long long
#define ld long double
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define pii pair<int, int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define sws ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl '\n'
#define teto(a, b) ((a+b-1)/(b))
using namespace std;
// Extra
#define forn(i, n) for(int i = 0; i < (int)n; i++)
#define forne(i, a, b) for(int i = a; i <= b; i++)
#define all(x) x.begin(), x.end()
#define dbg(msg, var) cout << msg << " " << var << endl;
//
const int MAX = 200010;
const ll MOD = (int) 998244353;
const int INF = 1e9;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const ld EPS = 1e-8;
int32_t main() {
// sws;
int n, m;
cin >> n >> m;
vector<vector<char>> mat(n, vector<char>(m, 0));
vi qtr(n+m+10), qtb(n+m+10), qtp(n+m+10);
ll res = 1;
forn(i, n) {
forn(j, m) {
cin >> mat[i][j];
int d = i+j;
if(mat[i][j] == 'R')
qtr[d]++;
if(mat[i][j] == 'B')
qtb[d]++;
if(mat[i][j] == '.')
qtp[d]++;
}
}
forn(d, n+m-1) {
if(qtr[d] and qtb[d]) {
res = 0;
break;
}
if(!qtr[d] and !qtb[d]) {
res = (res + res) % MOD;
}
}
cout << res << endl;
// int overflow, array bounds special cases (n=1?)
// do smth instead of nothing and stay organized - divide in subproblems?
// WRITE STUFF DOWN DON'T GET STUCK ON ONE APPROACH
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repp(i, st, en) for (ll i = (ll)st; i < (ll)(en); i++)
#define repm(i, st, en) for (ll i = (ll)st; i >= (ll)(en); i--)
#define all(v) v.begin(), v.end()
void chmax(ll &x, ll y) {x = max(x,y);}
void chmin(ll &x, ll y) {x = min(x,y);}
void Yes() {cout << "Yes" << endl; exit(0);}
void No() {cout << "No" << endl; exit(0);}
template<class in_Cout> void Cout(in_Cout x) {cout << x << endl; exit(0);}
template<class in_vec_cout>
void vec_cout(vector<in_vec_cout> vec) {
for (in_vec_cout res : vec) {cout << res << " ";}
cout << endl;
}
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll maxN = 110000;
ll N, M, K;
vector<ll> G[maxN];
vector<ll> C;
vector<vector<ll>> dist;
void init() {
cin >> N >> M;
rep(i,M) {
ll x, y; cin >> x >> y;
x--; y--;
G[x].push_back(y);
G[y].push_back(x);
}
cin >> K;
C.resize(K);
rep(i,K) cin >> C[i];
rep(i,K) C[i]--;
dist.resize(K,vector<ll>(K));
}
vector<ll> bfs(ll st) {
vector<ll> tmp(N,inf);
tmp[st] = 0;
queue<ll> que;
que.push(st);
while (que.size()) {
ll x = que.front();
que.pop();
for (ll y : G[x]) {
if (tmp[y]!=inf) continue;
tmp[y] = tmp[x] + 1;
que.push(y);
}
}
vector<ll> res(K);
rep(i,K) res[i] = tmp[C[i]];
return res;
}
int main() {
init();
rep(i,K) dist[i] = bfs(C[i]);
vector<vector<ll>> dp(1<<K,vector<ll>(K,inf));
rep(i,K) dp[1<<i][i] = 1;
rep(bit,1<<K) {
if (bit==0) continue;
rep(i,K) {
if (!((bit>>i)&1)) continue;
rep(j,K) {
if ((bit>>j)&1) continue;
ll nbit = bit | (1<<j);
chmin(dp[nbit][j], dp[bit][i] + dist[i][j]);
}
}
}
ll X = (1<<K) - 1;
ll ans = inf;
rep(i,K) chmin(ans,dp[X][i]);
if (ans==inf) ans = -1;
Cout(ans);
} | #include<bits/stdc++.h>
using namespace std;
const long long maxn=2e5+5;
const long long maxm=20;
const long long inf=0x3f3f3f3f;
vector<long long> g[maxn];
queue<pair<long long,long long> >q;
bool vis[maxn];
long long n,m,k,c[maxm],idx[maxn];
long long dist[maxm][maxm],dp[maxm][1<<maxm];
int main(){
cin>>n>>m;
for(long long i=0;i<m;i++)
{
long long x,y;
cin>>x>>y;
g[x].push_back(y);
g[y].push_back(x);
}
cin>>k;
memset(idx,-1,sizeof(idx));
for(long long i=0;i<k;i++)
{
cin>>c[i];
idx[c[i]]=i;
}
for(long long i=0;i<k;i++)
{
q.push({c[i],0});
memset(vis,0,sizeof(vis));
for(long long j=0;j<k;j++)
dist[i][j]=maxn;
while(!q.empty())
{
auto cur=q.front();
q.pop();
long long node=cur.first,cost=cur.second;
if(vis[node])
continue;
vis[node]=1;
if(idx[node]!=-1)
dist[i][idx[node]]=cost;
for(long long nbr:g[node])
{
if(!vis[nbr])
q.push({nbr,cost+1});
}
}
}
// cout<<"11111111111"<<endl;
// for(int i=0;i<k;i++)
// {
// for(int j=0;j<k;j++)
// cout<<dist[i][j]<<" ";
// cout<<endl;
// }
memset(dp,maxn,sizeof(dp));
for(long long i=0;i<k;i++)
dp[i][1<<i]=0;
for(long long j=0;j<(1<<k);j++)
{
for(long long i=0;i<k;i++)
{
if(j&(1<<i))
{
long long cur=j&~(1<<i);
for(long long l=0;l<k;l++)
if(cur&(1<<l))
dp[i][j]=min(dp[i][j],dp[l][cur]+dist[l][i]);
}
}
}
// cout<<"11111111111"<<endl;
// for(int i=0;i<k;i++)
// {
// for(int j=0;j<k;j++)
// cout<<dp[i][j]<<" ";
// cout<<endl;
// }
long long ans=maxn;
for(long long i=0;i<k;i++)
ans=min(ans,dp[i][(1<<k)-1]);
if(ans==maxn)
cout<<"-1"<<endl;
else
cout<<ans+1<<endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
using Graph = vector<vector<int>>;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
vector<bool> seen;
const ll MOD = 998244353;
void dfs(Graph &G, int v){
seen[v] = true;
for(auto next_v : G[v]) {
if(seen[next_v]) continue;
dfs(G, next_v);
}
}
int main(){
int n;
cin >> n;
vector<int> f(n);
rep(i,n) cin >> f[i];
rep(i,n) f[i]--;
Graph G(n);
rep(i,n){
G[i].push_back(f[i]);
G[f[i]].push_back(i);
}
seen.assign(n, false);
int cnt = 0;
for(int v = 0; v < n; v++){
if(seen[v]) continue;
dfs(G, v);
cnt++;
}
ll ans = 1;
rep(i, cnt){
ans *= 2;
ans %= MOD;
}
cout << ans-1 << endl;
return 0;
} | # pragma GCC optimize ("O3")
# pragma GCC optimize ("Ofast")
# pragma GCC optimize ("unroll-loops")
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#define rep(i,a,b) for(int i = (a); i < (b); i++)
#define rep2(i,a,b) for(int i = (b) - 1; i >= (a); i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define pb push_back
#define x first
#define y second
using namespace std;
using ll = long long;
using tl = tuple<ll, ll, ll, ll>;
using pl = pair<ll, ll>;
using pi = pair<int, int>;
using ld = long double;
const ll MOD = 998244353;
const int MAX = 2e5 + 1;
int N, f[MAX], fin[MAX], vis[MAX], cnt;
bool dfs(int u){
bool ret = 0;
vis[u] = 1;
int v = f[u];
if(!vis[v]) ret |= dfs(v);
else if(!fin[v]) {ret = 1; cnt++;}
fin[u] = 1;
return ret;
}
ll calc(ll a, ll b){
ll ret = 1;
while(b){
if(b & 1) ret = ret * a % MOD;
a = (a * a) % MOD;
b >>= 1;
}
ret = (ret + MOD - 1) % MOD;
return ret;
}
int main() {
cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false);
cin >> N;
rep(i, 1, N + 1) cin >> f[i];
rep(i, 1, N + 1) if(!vis[i]) dfs(i);
cout << calc(2, cnt);
}
|
#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<ll,int>;
const int inf = 1<<30;
const ll inf_l = 1LL<<61;
const int MAX = 1e5;
// dijkstra(O((E+V)log(V)))
template<class T>
struct dijkstra {
struct edge{
int to;
T cost;
int K;
};
vector<T> d;
vector<vector<edge>> G;
T init_v;
int N;
dijkstra(int n, T init_v) : N(n), init_v(init_v) {init(n);}
void init(int n) {
G.resize(n);
d.resize(n);
}
void add_edge(int s, int t, T cost, int k) {
edge e;
e.to = t; e.cost = cost; e.K = k;
G[s].push_back(e);
}
void run(int s) {
rep(i,N) d[i] = init_v;
priority_queue<pair<T,int>, vector<pair<T,int>>, greater<pair<T,int>>> q;
d[s] = 0;
q.push(pair<T,int>(0,s));
while (!q.empty()) {
pair<T,int> e = q.top(); q.pop();
int v = e.second;
if (d[v] < e.first) continue;
for (auto e: G[v]) {
if (chmin(d[e.to], (d[v] + e.K - 1) / e.K * e.K + e.cost)) {
q.push(pair<T,int>(d[e.to],e.to));
}
}
}
}
};
int main() {
int n, m, x, y;
cin >> n >> m >> x >> y;
x--; y--;
dijkstra<ll> dj(n, inf_l);
rep(i,m) {
int a, b, t, k;
cin >> a >> b >> t >> k;
a--; b--;
dj.add_edge(a, b, t, k);
dj.add_edge(b, a, t, k);
}
dj.run(x);
if (dj.d[y] == inf_l) cout << -1 << endl;
else cout << dj.d[y] << endl;
} | #include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5+5;
int head[maxn],tot;
struct Edge{
int next,to,tim,val;
}e[maxn];
void add_edge(int x,int y,int z,int w){
e[++tot] = (Edge){head[x],y,z,w},head[x] = tot;
}
int n,m,x,y;
struct Sta{
long long tim;
int u;
bool operator <(Sta b)const{return tim>b.tim;}
};
int vis[maxn];
long long dis[maxn];
std :: priority_queue <Sta> q;
void Dijkstra(){
memset(dis,0x3f,sizeof(dis));
q.push((Sta){0,x}),dis[x] = 0;
while(!q.empty()){
Sta u = q.top();
q.pop();
if(vis[u.u])continue;
vis[u.u] = 1;
// cout << u.u << ' ' << head[1] << endl;
for(int i=head[u.u],v;v=e[i].to,i;i=e[i].next){
long long to = dis[u.u]?((dis[u.u]-1)/e[i].tim+1)*e[i].tim+e[i].val:e[i].val;
// cout << u.u << ' ' << v<< ' ' << to << endl;
if(to < dis[v] && !vis[v])dis[v] = to,q.push((Sta){to,v});
}
}
if(vis[y])printf("%lld",dis[y]);
else puts("-1");
}
int main(){
scanf("%d %d %d %d",&n,&m,&x,&y);
for(int i=1,a,b,c,d;i<=m;++i){
scanf("%d %d %d %d",&a,&b,&c,&d);
add_edge(a,b,d,c),add_edge(b,a,d,c);
}
// for(int i=1;i<=n;++i)cout << head[i] << ' ';
// cout<< '\n';
Dijkstra();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct level{
int type; //-1 = left, 1 = right
int id;
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(20) << fixed;
int N;
cin >> N;
vector<level> e(2 * N + 1);
vector<bool> dp(2 * N + 1);
dp[0] = true;
for(int i = 0; i < N; i++){
int a, b;
cin >> a >> b;
if(a != -1){
if(e[a].type != 0){
cout << "No" <<endl;
return 0;
}
e[a].type = -1;
e[a].id = i+1;
}
if(b != -1){
if(e[b].type != 0){
cout << "No" <<endl;
return 0;
}
e[b].type = 1;
e[b].id = i+1;
}
}
//*
for(int i = 0; i <= 2 * N; i++){
//cout << i << " " << e[i].id << " " << e[i].type << endl;
}
//*/
for(int i = 0; i <= 2 * N; i+=2){
if(dp[i]){
for(int j = i + 2; j <= 2 * N; j+=2){
//cout << "j " << j <<endl;
bool bad = false;
int length = (j - i) / 2;
for(int t = i+1; t <= i + length; t++){
//cout << "t " << t <<endl;
if(e[t].type == 1 || e[t+length].type == -1){
//cout << "bad due to type" <<endl;
bad = true;
break;
}
if(e[t].id != 0 && e[t+length].id != 0){
if(e[t+length].id != e[t].id){
//cout << "bad due to id" <<endl;
bad = true;
break;
}
}
}
if(!bad) dp[j] = true;
}
}
}
/*
for(int i = 0; i <= 2 * N + 1; i++){
cout << dp[i] << " ";
}
cout <<endl;
//*/
if(dp[2*N]) cout << "Yes" <<endl;
else cout << "No" <<endl;
}
| #include<bits/stdc++.h>
using namespace std;
namespace Ruri{
#define ms(a,b) memset(a,b,sizeof(a))
#define repi(i,a,b) for(int i=a,bbb=b;i<=bbb;++i)//attention reg int or reg ll ?
#define repd(i,a,b) for(int i=a,bbb=b;i>=bbb;--i)
#define reps(s) for(int i=head[s],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
#define ce(i,r) i==r?'\n':' '
#define pb push_back
#define all(x) x.begin(),x.end()
#define gmn(a,b) a=min(a,b)
#define gmx(a,b) a=max(a,b)
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
const int infi=1e9;//infi较大,注意涉及inf相加时爆int
const ll infl=4e18;
inline ll ceil_div(ll a,ll b){ return (a+b-1)/b; }
inline ll pos_mod(ll a,ll b){ return (a%b+b)%b; }
//std::mt19937 rnd(time(0));//std::mt19937_64 rnd(time(0));
}
using namespace Ruri;
namespace Read{
#define ss(a) scanf("%s",a)
inline int ri(){ int x; scanf("%d",&x); return x; }
inline ll rl(){ ll x; scanf("%lld",&x); return x; }
inline db rd(){ db x; scanf("%lf",&x); return x; }
}
namespace DeBug{
#define pr(x) cout<<#x<<": "<<(x)<<endl
#define pra(x,a,b) cout<<#x<<": "<<endl; \
repi(i,a,b) cout<<x[i]<<" "; \
puts("");
#define FR(a) freopen(a,"r",stdin)
#define FO(a) freopen(a,"w",stdout)
}
using namespace Read;
using namespace DeBug;
const int MAX_N=205;
int n,a[MAX_N],b[MAX_N];
int belong[MAX_N],w[MAX_N];
bool dp[MAX_N][MAX_N];
bool Check()
{
ms(belong,-1);
repi(i,1,n){
if(a[i]!=-1){
if(belong[a[i]]!=-1) return false;
belong[a[i]]=0;
}
if(b[i]!=-1){
if(belong[b[i]]!=-1) return false;
belong[b[i]]=1;
}
if(a[i]!=-1&&b[i]!=-1){
if(a[i]>b[i]) return false;
w[a[i]]=w[b[i]]=b[i]-a[i];
}
}
repi(len,1,n)for(int st=1;st+2*len-1<=2*n;st+=2){
int ed=st+2*len-1;
dp[st][ed]=true;
repi(i,st,st+len-1){
if(belong[i]==1) dp[st][ed]=false;
if(w[i]&&w[i]!=len) dp[st][ed]=false;
if(!w[i]&&belong[i]==0&&belong[i+len]==1) dp[st][ed]=false;
}
repi(i,st+len,ed){
if(belong[i]==0) dp[st][ed]=false;
if(w[i]&&w[i]!=len) dp[st][ed]=false;
}
for(int i=st+1;i<ed;i+=2)if(dp[st][i]&&dp[i+1][ed]) dp[st][ed]=true;
}
return dp[1][2*n];
}
int main()
{
n=ri();
repi(i,1,n) a[i]=ri(),b[i]=ri();
puts(Check()?"Yes":"No");
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long t,n,i;
cin >> t;
while(t--){
cin >> n;
if(n%4==0){
cout << "Even" << endl;
}else if(n%2==0){
cout << "Same" << endl;
}else{
cout << "Odd" << endl;
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (intt i = 0; i < (intt)(n); i++)
using intt = int64_t;
intt sum(const long &a, const long &b) { return (a + b) * (b - a + 1) / 2; }
vector<char> alphabet={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
bool is_squere(intt N) {
intt r = (intt)floor(sqrt((long double)N)); // 切り捨てした平方根
return (r * r) == N;
}
int main() {
intt n;
cin>>n;
rep(i,n){
intt x;
cin>>x;
if(x%2!=0){
cout<<"Odd"<<endl;
}
else if(is_squere(x)||x%4==0){
cout<<"Even"<<endl;;
}
else{
cout<<"Same"<<endl;
}
}
} |
//#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
#include <map>
#include <cstdio>
#include <cmath>
#include <iomanip>
#include <queue>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); i++)
#define vi vector<int>
#define vll vector<long long>
#define vb vector<bool>
#define vs vector<string>
#define all(x) x.begin() , x.end()
#define pb push_back
#define eb emplace_back
#define chmax(x,y) (x) = max((x) , (y))
#define chmin(x,y) (x) = min((x) , (y))
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
//const int MOD = int(1e9) + 7;
//const int MOD = 998244353;
//const ll MOD = ll(1e9) + 7;
const ll MOD = 998244353;
const ld PI = 3.1415926535897932384626;
const int INF = 1001001001;
const ll LINF = 1001001001001001001;
const int MX = 220000;
ll modpow (ll x , ll p) {
ll ret = 1;
while (p) {
if (p % 2 == 1) {
ret *= x;
ret %= MOD;
}
x *= x;
x %= MOD;
p /= 2;
}
return ret;
}
vll fact(MX);
vll factinv(MX);
void init_fact () {
fact[0] = 1;
for (int i = 1; i < MX; i++) {
fact[i] = 1LL * i * fact[i - 1] % MOD;
}
for (int i = 0; i < MX; i++) {
factinv[i] = modpow(fact[i] , MOD - 2);
}
}
bool is_prime (int x) {
if (x == 2 || x == 3 || x == 5 || x == 7) {
return true;
}
for (ll i = 2; i*i <= x; i++) {
if (x % i == 0) {
return false;
}
}
return true;
}
vb prime_table(MX , true);
void init_prime() {
for (int i = 2; i < MX; i++) {
for (int j = i + i; j < MX; j += i) {
prime_table[j] = false;
}
}
return;
}
map<ll, int> factrization(ll x) {
map<ll, int> ret;
for (int i = 2; i*i <= x; i++) {
while (x % i == 0) {
ret[i]++;
x /= i;
}
}
if (x > 1) ret[x]++;
return ret;
}
ll moddiv (ll a , ll b) { return a * modpow(b , MOD - 2) % MOD; }
ll modsub (ll a , ll b) { if (a >= b) return (a - b) % MOD; else return (MOD - a + b); }
ll nCk (ll n , ll k) { return ((fact[n] * factinv[k] % MOD) * factinv[n - k] % MOD); }
ll nHk (ll n , ll k) { return nCk(n - 1 + k , k); } // 仕切り- 1 + boll を並べる組み合わせ
ll gcd (ll a , ll b) { if (a < b) swap(a , b); if (a % b == 0) return b; else return gcd(b , a % b); }
ll lcm (ll a , ll b) { return a / gcd(a , b) * b; }
ll lin() { ll x; scanf("%lld" , &x); return x; }
int in() { int x; scanf("%d" , &x); return x; }
bool is_palindrome (string s) { string t = s; reverse(all(t)); return (s == t); }
void io_setup() { cout << fixed << setprecision(20); }
void solve () {
int n , m;
cin >> n >> m;
map<int , int> mp1 , mp2;
rep(i,n) {
int a = in();
mp1[a]++;
}
rep(i,m) {
int a = in();
mp2[a]++;
}
vi ans;
for (auto i : mp1) {
if (mp2[i.fi] == 0) ans.pb(i.fi);
}
for (auto i : mp2) {
if (mp1[i.fi] == 0) ans.pb(i.fi);
}
sort(all(ans));
int SZ = ans.size();
rep(i,SZ) {
cout << ans[i];
if (i != SZ - 1) cout << " ";
else cout << endl;
}
return;
}
int main() {
io_setup();
//init_fact();
//init_prime();
solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, w; cin >> n >> w;
cout << n / w << "\n";
return 0;
} |
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef bitset<16> BS;
struct edge {
int to, cost, id;
};
const double EPS = 1E-09;
const ll MOD = 1E+09 + 7; // =998244353;
const ll INF = 1E18;
const int MAX_N = 1E+05;
ll dx[4] = { -1, 1, 0, 0 }, dy[4] = { 0, 0, -1, 1 };
int N;
string s;
bool fox(string& t, char c);
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> N >> s;
string t;
int ans = N;
for (int i = 0; i < N; i++) {
if (fox(t, s[i]))
ans -= 3;
}
/*
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cout << "i = " << i << ", j = " << j << ", dp = " << dp[i][j] << "\n";
}
}
*/
//cout << "s = " << s << "\n";
cout << ans << "\n";
return 0;
}
bool fox(string& t, char c)
{
t += c;
if (t.size() < 3)
return false;
int n = t.size();
if (t[n - 3] == 'f' && t[n - 2] == 'o' && t[n - 1] == 'x') {
t.erase(t.begin() + n - 3, t.end());
return true;
}
return false;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(ll i=0;i<ll(n);i++)
#define REPE(i,l,n) for(ll i=l;i<=ll(n);i++)
#define FORA(i,I) for(const auto& i:I)
#define ALL(v) v.begin(),v.end()
#define P(str) cout << str << endl
#define VLL vector<ll>
#define chmax(a, b) a = (((a)<(b)) ? (b) : (a))
#define chmin(a, b) a = (((a)>(b)) ? (b) : (a))
constexpr int64_t INF = 1e18;
void c(VLL& v){
REP(i, v.size()){
v[i] -= v[i] * 2;
}
}
int main() {
ll a, b;
cin >> a >> b;
ll mi = min(a, b);
ll ma = max(a, b);
bool f = true;
if(a < b) f = false;
VLL vmi(mi), vma(ma);
ll summa = 0;
REP(i, ma){
vma[i] = i + 1;
summa += vma[i];
}
ll summi = 0;
REP(i, mi){
vmi[i] = i + 1;
summi += vmi[i];
}
// cout << summa << " " << summi << endl;
vmi[mi - 1] += (summa - summi);
if(f) c(vmi);
else c(vma);
FORA(i, vmi) cout << i << " ";
REP(i, vma.size()){
cout << vma[i];
if(i == vma.size() - 1) cout << endl;
else cout << " ";
}
} |
#include<bits/stdc++.h>
using namespace std;
int main(){
int N,K;cin >> N >> K;
vector<vector<int> >A(N,vector<int>(N));
for (int i=0;i<N;i++){
for (int j=0;j<N;j++){
cin >> A[i][j];
}
}
int r=1000000008;
int l=-1;
while (abs(r-l)>1){
int m=(r+l)/2;
vector<vector<int> >high_low(N,vector<int>(N,0));
for (int i=0;i<N;i++){
for (int j=0;j<N;j++){
if (A[i][j]<=m) high_low[i][j]=1;
}
}
vector<vector<int> > dp(N,vector<int>(N));
for (int i=0;i<N;i++){
for (int j=0;j<N;j++){
dp[i][j]+=high_low[i][j];
if (i-1>=0) dp[i][j]+=dp[i-1][j];
if (j-1>=0) dp[i][j]+=dp[i][j-1];
if (i-1>=0 && j-1>=0) dp[i][j]-=dp[i-1][j-1];
}
}
int count=0;
int centers=0;
for (int i=K-1;i<N;i++){
for (int j=K-1;j<N;j++){
count=0;
count+=dp[i][j];
if (i-K>=0) count-=dp[i-K][j];
if (j-K>=0) count-=dp[i][j-K];
if (i-K>=0 && j-K>=0) count+=dp[i-K][j-K];
if (K%2==0){
if (K*K/2<=count) centers++;
}else{
if (K*K/2+1<=count) centers++;
}
}
}
if (centers>=1) r=m;
else l=m;
}
cout << r << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define fst ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long
#define tc int tt; cin >> tt; while(tt--)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
const int N = 2e5 + 5, mod = 1e9 + 7, INF = 1e16 + 6;
int power(int a, int n) {
int res = 1;
while(n) {
if(n&1) res = (res*a)%mod;
n >>= 1;
a = (a*a)%mod;
}
return res;
}
int32_t main()
{
fst
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n, m;
cin >> n >> m;
int mat[n][m];
int mn = 1e18;
for(int i=0;i<n;++i) {
for(int j=0;j<m;++j) cin >> mat[i][j], mn = min(mn, mat[i][j]);
}
int ans = 0;
for(int i=0;i<n;++i) {
for(int j=0;j<m;++j) ans += (mat[i][j] - mn);
}
cout << ans;
} |
#include <bits/stdc++.h>
using namespace std;
#define int unsigned long long
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
int memo[200][20];
int l;
int dp(int i, int j){
if(i == l-1){
return j == 11 ? 1 : 0;
}
if(memo[i][j] != -1) return memo[i][j];
int ans = dp(i+1, j);
if(j < 11){
ans += dp(i+1, j+1);
}
return memo[i][j] = ans;
}
signed main(){
cin >> l;
memset(memo, -1, sizeof(memo));
cout << dp(0, 0) << endl;
return 0;
}
| #include <bits/stdc++.h>
//abcdefghijklmnopqrstuvwxyz
/*
Народ все кто играет в CodeForces вступайте в нашу
Организацию "Megumin's Fan Club" там был Я, Я все еще там
Хорошая организация классная правда сейчас загнивает))))
*/
#define F first
#define S second
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define in(x) insert(x)
#define sz(s) (int)(s.size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define did_you_calis_enough ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
#define int long long
using namespace std;
const int INF = 2147483647;
const int N = 2e5 + 667;
const int mod = 1e9 + 7;
const int BL = 450;
const int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};
const int dy[] = {0, 0, 1, -1, 1, -1, 1, -1};
const double PI = acos(-1.0);
int pw(int a, int n) {
int res = 1;
while (n) {
if (n & 1)
res *= a;
a *= a;
n >>= 1;
}
return res;
}
int lg(int b, int n, int r = 0){
return ((n <= 1)? r : lg(b, n/b,++r));
}// log N with base of B
int32_t main() {
did_you_calis_enough
int n;
cin >> n;
int a = 1, b = 1, cur = n-12;
for(int i = n-1;i >= 12;i--){
a *= i;
b *= cur--;
int g = __gcd(a, b);
a /= g;
b /= g;
}
cout << a;
return 0;
}
//Coded by LoliThief |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef pair<int,int> Pair;
typedef tuple<int,int,int> tpl;
#define ALL(a) (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())
#define EXIST(m,v) (m).find((v)) != (m).end()
#define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin()
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i)
#define RREP(i,n) RFOR(i,n,0)
#define en "\n"
constexpr double EPS = 1e-9;
constexpr double PI = 3.1415926535897932;
constexpr int INF = 2147483647;
constexpr long long LINF = 1LL<<60;
constexpr long long MOD = 1000000007; // 998244353;
template<class T> inline bool chmax(T& a, T b){if(a<b){a=b;return true;}return false;}
template<class T> inline bool chmin(T& a, T b){if(a>b){a=b;return true;}return false;}
template<long long mod=1000000007>
struct modint {
long long x;
modint(long long x=0):x((x%mod+mod)%mod){}
long long val(){
return x;
}
modint<mod> operator-() const { return modint(-x);}
modint<mod>& operator+=(const modint a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
modint<mod>& operator-=(const modint a) {
if ((x += mod-a.x) >= mod) x -= mod;
return *this;
}
modint<mod>& operator*=(const modint a) {
(x *= a.x) %= mod;
return *this;
}
modint<mod> operator+(const modint a) const {
modint<mod> res(*this);
return res+=a;
}
modint<mod> operator-(const modint a) const {
modint<mod> res(*this);
return res-=a;
}
modint<mod> operator*(const modint a) const {
modint<mod> res(*this);
return res*=a;
}
modint<mod> pow(long long t) const {
if (!t) return 1;
modint<mod> a = pow(t>>1);
a *= a;
if (t&1) a *= *this;
return a;
}
// must be gcd(x,mod)==1
modint<mod> inv() const {
// a^{-1} = 1/a mod p (拡張Euclidの互除法)
long long b = mod, u = 1, v = 0, z = x;
while(b){
long long t = z / b;
z -= t * b; swap(z, b);
u -= t * v; swap(u, v);
}
u %= mod;
if (u < 0) u += mod;
return modint<mod>(u);
}
//modint inv() const {
// return pow(mod-2);
//}
modint<mod>& operator/=(const modint a) {
return (*this) *= a.inv();
}
modint<mod> operator/(const modint a) const {
modint<mod> res(*this);
return res/=a;
}
};
using mint = modint<998244353>;
void Main(){
int N,M,K; cin >> N >> M >> K;
if(N==1 || M==1){
cout << mint(K).pow(N*M).x << en;
return;
}
vector<mint> f(K+1,0), g(K+1,0);
FOR(i,1,K+1){
f[i] = mint(i).pow(N) * mint(K-i+1).pow(M);
g[i] = mint(i-1).pow(N) * mint(K-i+1).pow(M);
}
mint ans(0);
REP(i,K) ans += f[i+1] - g[i+1];
cout << ans.x << en;
return;
}
int main(void){
cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);cout<<fixed<<setprecision(15);
int t=1; //cin>>t;
REP(_,t) Main();
return 0;
} | /* bimillahir rahmanir rahim...
* AUTHOR
* mubin_akib */
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair <int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i,a,b) for (int i = (b) - 1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a) - 1; i >= 0; i--)
#define REP(i, a, b) for(int i = a; i <= (b); ++i)
#define trav(a,x) for(auto& a : x)
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int mod = 1e9 + 7;
const char nl = '\n';
void solve() {
int n;
cin >> n;
vi v(2000);
F0R(i, n){
int x;
cin >> x;
v[x]++;
}
int a = 0, ans = 0;
FOR(i, 2, 2000){
int k = 0;
for(int j = i; j < 2000; j += i){
k += v[j];
}
if(ans < k){
ans = k;
a = i;
}
}
cout << a << nl;
}
int main() {
#ifndef ONLINE_JUDGE
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
} |
#include <bits/stdc++.h>
#define rep(i,n) REP(i, 0, n)
#define REP(i, a, n) for(int i = a ; i < (int)n ; i++)
#define mod 1000000007
typedef long long ll;
using namespace std;
int main()
{
ll n; cin >> n;
map<ll, ll> map;
rep(i, n) {
ll x; cin >> x;
map[x]++;
}
ll ans = n*(n-1)/2;
for (auto p : map) {
auto num = p.second;
ans -= num * (num-1) / 2;
}
cout << ans << endl;
} | /*
You Can Do This!
From Madeline
To You
https://www.youtube.com/watch?v=vBUhgkMD5kc
*/
#include <bits/stdc++.h>
#define inf ll (1e18)
#define ninf ll (-1e18)
#define mod ll (1e9 + 7)
#define first F
#define second S
#define fast ios::sync_with_stdio (0); cin.tie (0);
using namespace std;
using ll = long long;
using pi = pair < ll , ll >;
void solution (ll tc = 0) {
ll n;
cin >> n;
map < ll , ll > m;
vector < ll > v (n);
for (ll i = 0; i < n; i++) {
cin >> v[i];
m[v[i]]++;
}
ll current = n - 1;
ll res = 0;
for (ll i = 0; i < n; i++) {
ll x = m[v[i]]--;
x--;
res += current - x;
current--;
}
cout << res << '\n';
}
int main () {
fast
solution ();
} |
#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
using ll = long long;
using lint = long long;
typedef vector<long long> vint;
typedef pair<long long, long long> pint;
#define INF INT32_MAX / 2
#define INF64 INT64_MAX / 2
#define EPS 0.001
#define EPS14 1.0E-14
#define REP(i, n) for (int i = 0; i < n; i++)
#define all(x) (x).begin(),(x).end()
#define ALL(f,c,...) (([&](decltype((c)) cccc) { return (f)(std::begin(cccc), std::end(cccc), ## __VA_ARGS__); })(c))
#define c(n) cout<<n<<endl;
#define cf(n) cout<<fixed<<setprecision(15)<<n<<endl;
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;}
template<class T>inline T sum(T n){return n*(n+1)/2;}
map<ll,ll> prime_fac(ll A) {map<ll,ll>mp;for(ll i=2;i*i<=A;i++){while(A%i== 0){mp[i]++;A/=i;}}if(A!=1){mp[A]=1;}return mp;}
bool is_prime(ll N){if(N<=1)return false;for(ll i=2;i*i<=N;i++){if(N%i==0) return false;}return true;}
template<class T>inline T myceil(T a,T b){return (a+(b-1))/b;}
bool is_product_overflow(long long a,long long b) {long prod=a*b;return (prod/b!=a);}
void solve(long long N, std::string S){
ll ans = 0;
REP (i, N) {
vector<ll> mp(10000, 0);
for (int j = i; j < N; j++) {
mp[S[j]]++;
if (mp['A'] == mp['T'] && mp['G'] == mp['C']) ans++;
}
}
c(ans)
}
int main(){
long long N;
scanf("%lld",&N);
std::string S;
std::cin >> S;
solve(N, S);
return 0;
}
| #include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=55;
int n,vis;
int a[maxn],prime[15]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47};
bool s[maxn][15];
inline int read(){
int ret=0,f=1; char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
while(isdigit(ch)){ret=ret*10+ch-'0';ch=getchar();}
return ret*f;
}
int gcd(int x,int y){return y?gcd(y,x%y):x;}
signed main(){
n=read();
for(int k=1;k<=n;k++) a[k]=read();
int ans=0;
for(int k=0;k<(1<<15);k++){
int sum=1;
vis=0;
for(int j=0;j<15;j++){
int p=1ll<<j,x=prime[j];
if(!(k&p)) continue;
sum*=x;
for(int i=1;i<=n;i++)
if(gcd(x,a[i])>1) vis|=1ll<<i-1;
}
if(vis==(1ll<<n)-1){
if(!ans) ans=sum; else ans=min(ans,sum);
}
}
printf("%lld\n",ans);
return 0;
}
/*
10
2 5 7 12 27 8 18 33 13 32
*/ |
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t; using Vll =vector<ll>; using VVll =vector<vector<ll>>;
template <class T> using Vec = vector<T>; template <class T> using VVec = vector<vector<T>>;
#define INF 9223372036854775807LL/2
void Z(ll i=-1){ if(i==-1)cout<<"Test"<<endl; else cout<<"Test"<<i<<endl; }
template <class T> void VVcout(VVec<T> A){ for(auto Vi:A) { for(auto i:Vi)cout<<i<<' '; cout<<endl;}}
template <class T> void Vcout(Vec<T> A){ for(auto i:A) cout<<i<<' '; cout<<endl;}
template <class T> void chmax(T &x,T y){if(x<y) x=y;} template <class T> void chmin(T &x,T y){if(x==-1 || x>y) x=y;}
#define rep(i,n) for(ll i=0;i<n;i++)
#define reps(si, i,n) for(ll i=si;i<n;i++)
int main(){
ll a,b,c;
cin>>a>>b>>c;
cout<<a+b+c-min(a,min(b,c))<<endl;
}
| #include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <utility>
#include <algorithm>
#include <cmath>
#include <climits>
#include <iomanip>
#include <queue>
#include <stack>
#include <ctype.h>
using namespace std;
typedef long long ll;
const int INF = (1<<30)-1;
const ll LINF = (1LL<<60)-1;
#define rep(i, n) for (int i = 0; i < n; i++)
template<class T>
bool chmax(T &a, T b) {if (a < b) {a = b;return true;}else return false;}
template<class T>
bool chmin(T &a, T b) {if (a > b) {a = b;return true;}else return false;}
#define MAX (ll)3e6+1 //nCrのnの最大値+1
ll fac[MAX], inv[MAX], finv[MAX];
void COMinit(ll mod) {
fac[0] = 1, finv[0] = 1;
fac[1] = 1, inv[1] = 1, finv[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) % mod;
finv[i] = finv[i-1] * inv[i] % mod;
}
}
ll COM(ll n, ll k, ll mod) {
if (n < k)
return 0;
if (n < 0 || k < 0)
return 0;
return fac[n] * finv[k] % mod * finv[n-k] % mod;
}
ll P(ll n, ll r, ll mod) {
if (n < r)
return 0;
if (n < 0 || r < 0)
return 0;
return fac[n] * finv[n-r] % mod;
}
//コーナーケースに気をつけろ!
int main() {
ll n, m, k; cin >> n >> m >> k;
ll mod = 1e9+7;
COMinit(mod);
if (n <= m+k)
cout << (COM(n+m, m, mod) + mod - COM(n+m, m+k+1, mod)) % mod << endl;
else
cout << 0 << endl;
return 0;
}
//小数点精度
//cout << fixed << std::setprecision(15) << y << endl; |
#include <bits/stdc++.h>
#define rep(i,cc,n) for(int i=cc;i<=n;++i)
#define drep(i,cc,n) for(int i=cc;i>=n;--i)
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const long long INF = 1LL <<60;
const long long MOD = 1000000007;
typedef long long ll;
using namespace std;
ll gcd(ll m,ll n){
if(n==0) return m;
return gcd(n,m%n);
}
int main(){
int a,b;
cin >> a >> b;
int ans;
rep(i,1,b){
int tmp = 0;
if(b / i > 1) tmp = b / i;
else break;
if(i * (tmp - 1) >= a) ans = i;
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int a,b,x,y;
cin>>a>>b;
int d=b-a;
while(true){
x=a/d;
if(a%d!=0) x++;
y=b/d;
if(x<y){
cout<<d;
break;
}
d--;
}
} |
#include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std;
using ll = long long;
using VL=vector<ll>;
typedef pair<int,int> pi;
#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
const long long INF = 999999999;
int main(){
ll b,c;
cin>>b>>c;
if(b==0){
cout<<c/2+max(ll(0),ll((c-1)/2))+1<<endl;
}
else if(b>0){
if(-b+((c-1)/2)<b-(c/2)){
cout<<2*((c-1)/2)+(c/2)+max(ll(((c-2)/2)),ll(0))+2;
}
else {
cout<<2*b+((c-1)/2)+max(ll(0),ll(((c-2)/2)))+1;
}
}
else if(b<0){
if(b+(c-2)/2<-b-(c-1)/2)cout<<2*((c-1)/2)+(c/2)+max(ll(((c-2)/2)),ll(0))+2<<endl;
else cout<< -2*b+(c-1)/2+c/2+1 <<endl;
}
}
| #include <bits/stdc++.h>
#include <math.h>
using namespace std;
#define REP(i,n) for (int i=0; i<n; i++)
using ll = long long;
using P = pair<int,int>;
pair<long, long> extract(const long&B, const long&C) {
const auto n = C/2;
if (C%2) {
return {-B-n, -B+n};
} else {
if (!C) return {B,B};
return {B-n, B+n-1};
}
}
int main() {
long B, C;
cin >> B >> C;
const auto [a,b] = extract(B,C);
const auto [c,d] = extract(B,C-1);
cout << (b-a+1) + (d-c+1) - max(0l, min(b,d)-max(a,c)+1) << endl;
return 0;
}
|
#define _USE_MATH_DEFINES
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include <cassert>
#include <numeric>
#include <unordered_map>
#include <queue>
#include <math.h>
#include <climits>
#include <set>
#include <map>
using namespace std;
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
//template<class T> using pq = priority_queue<T, vector<T>, greater<T>>;
#define FOR(i, a, b) for(ll i=(a); i<(b);i++)
#define REP(i, n) for(ll i=0; i<(n);i++)
#define ROF(i, a, b) for(ll i=(b-1); i>=(a);i--)
#define PER(i, n) for(ll i=n-1; i>=0;i--)
#define REPREP(i,j,a,b) for(ll i=0;i<a;i++)for(ll j=0;j<b;j++)
#define VV(type) vector< vector<type> >
#define VV2(type,n,m,val) vector< vector<type> > val;val.resize(n);for(ll i;i<n;i++)val[i].resize(m)
#define vec(type) vector<type>
#define VEC(type,n,val) vector<type> val;val.resize(n)
#define VL vector<ll>
#define VVL vector< vector<ll> >
#define SZ size()
#define all(i) begin(i),end(i)
#define SORT(i) sort(all(i))
#define BITI(i) (1<<i)
#define BITSET(x,i) x | (1<<i)
#define BITCUT(x,i) x & ~(1<<i)
#define EXISTBIT(x,i) ((x>>i) & 1 != 0)
#define ALLBIT(n) (1<<n-1)
class UndirectedGraph {
public:
struct edge { ll to,cost; }; // 行先,容量,逆容量,コスト
UndirectedGraph(ll n) {
edges.resize(n);
}
void addEdge(ll from, ll to, ll cost) {
edge eF = { to,cost };
edges[from].push_back(eF);
edge eT = { from,cost };
edges[to].push_back(eT);
}
void FindConnect(ll v, vector<bool>& out) {
out.assign(edges.size(), false);
vector<ll> stack;
stack.push_back(v);
out[v] = true;
while (!stack.empty()) {
v = stack.back(); stack.pop_back();
for (int i = 0; i < edges[v].size(); i++) {
ll t = edges[v][i].to;
if (!out[t]) {
out[t] = true;
stack.push_back(t);
}
}
}
}
vector<vector<edge>> edges;
};
int main() {
ll n, m;
cin >> n >> m;
VL a(m), b(m), c(m);
UndirectedGraph g(n);
REP(i, m) {
char cc;
cin >> a[i] >> b[i] >> cc;
a[i]--; b[i]--;
c[i] = cc - 'a';
g.addEdge(a[i], b[i], c[i]);
}
queue<tuple<ll, ll, ll>> que;
vector<vector<bool>> used(n);
REP(i, n) {
used[i].assign(n, false);
}
que.push({ 0,0,n - 1 });
used[0][n - 1] = true;
used[n - 1][0] = true;
ll find = -1;
while (!que.empty()) {
auto [dist, l, r] = que.front();
que.pop();
if (find != -1 && find < dist) {
cout << find * 2 + 1;
return 0;
}
if (l == r) {
cout << dist * 2;
return 0;
}
REP(i, g.edges[l].size()) {
REP(j, g.edges[r].size()) {
if (g.edges[l][i].cost == g.edges[r][j].cost) {
if (g.edges[l][i].to == r && g.edges[r][j].to == l) {
find = dist;
continue;
}
if (used[g.edges[l][i].to][g.edges[r][j].to])
continue;
que.push({ dist + 1,g.edges[l][i].to ,g.edges[r][j].to });
used[g.edges[l][i].to][g.edges[r][j].to] = true;
used[g.edges[r][j].to][g.edges[l][i].to] = true;
}
}
}
}
if (find != -1) {
cout << find * 2 + 1;
return 0;
}
cout << -1;
}
| #include<bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define pb push_back
#define fi first
#define se second
#define ll long long
#define tp top()
#define fr front()
#define vi vector<int>
#define sz size()
#define rep(i,a,b) for(int i = a; i < b; ++i)
#define mem(a, b) memset(a, (b), sizeof(a))
#define clr(a) memset(a, 0, sizeof(a))
#define sqr(x) ( (x) * (x) )
#define all(v) v.begin(), v.end()
typedef pair<int, int> pii;
typedef pair<int,pii> pip;
typedef pair<pii,int> ppi;
typedef pair<pii,pii> ppp;
void base(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
void solve(){
// rep(i,1,40){
// cout<<i<<" -> "<<8*i<<"\n";
// }
string s; cin>>s;
char ch[20]={0};
rep(i,0,s.sz) ch[s[i]-'0']++;
bool found = 0;
rep(i,0,10) {
if (ch[i]==0) continue;
ch[i]--;
if (s.sz == 1) {
if (i % 8 == 0) found = 1;
continue;
}
rep(j,0,10){
if (ch[j]==0) continue;
ch[j]--;
int res = i*10 + j;
bool hasodd=0, haseven=0;
rep(k,0,10){
if (k&1) {
if (ch[k]>0) hasodd=1;
} else {
if (ch[k]>0) haseven=1;
}
}
if (s.sz == 2) haseven = 1;
if (haseven && (res % 8 == 0)) found = 1;
if (hasodd && ((res+4) % 8 == 0)) found = 1;
ch[j]++;
}
ch[i]++;
}
if (found) cout<<"Yes\n";
else cout<<"No\n";
}
int main()
{
base();
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int t=1;
// cin>>t;
while(t--){
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef tuple<ll, ll, ll> iii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
#define REP(i,n) for (ll i = 0; i < (n); ++i)
#define REPR(i,n) for (ll i = (n)-1; i >= 0; --i)
#define FOR(i,m,n) for (ll i = (m); i < (n); ++i)
#define FORR(i,m,n) for (ll i = (n)-1; i >= (m); --i)
#define FORE(x,xs) for (const auto& x : (xs))
#define FORI(i,v) for (auto i = v.begin(); i != v.end(); i++)
#define ALL(v) v.begin(), v.end()
#define CHMIN(x,y) x = min(x, (y))
#define CHMAX(x,y) x = max(x, (y))
#define BIT(x,i) (((x) >> (i)) & 1)
#define YES(b) cout << ((b) ? "YES" : "NO") << endl
#define Yes(b) cout << ((b) ? "Yes" : "No") << endl
const int MAX = 2020;
int H, W;
string A[MAX];
int steps[MAX][MAX];
vii teles[30];
bool used[30];
int solve() {
int gx = -1, gy = -1, sx = -1, sy = -1;
REP (h, H) REP (w, W) {
steps[h][w] = -1;
if (A[h][w] == 'S') {
sx = w, sy = h;
steps[h][w] = 0;
}
if (A[h][w] == 'G') gx = w, gy = h;
if ('a' <= A[h][w] && A[h][w] <= 'z') teles[A[h][w]-'a'].push_back(make_pair(w, h));
}
queue<ii> q;
q.push(ii(sx, sy));
while (!q.empty()) {
int x, y; tie(x, y) = q.front();
q.pop();
int next = steps[y][x] + 1;
if (x > 0 && steps[y][x-1] < 0 && A[y][x-1] != '#') {
steps[y][x-1] = next;
q.push(ii(x-1, y));
}
if (x < W-1 && steps[y][x+1] < 0 && A[y][x+1] != '#') {
steps[y][x+1] = next;
q.push(ii(x+1, y));
}
if (y > 0 && steps[y-1][x] < 0 && A[y-1][x] != '#') {
steps[y-1][x] = next;
q.push(ii(x, y-1));
}
if (y < H-1 && steps[y+1][x] < 0 && A[y+1][x] != '#') {
steps[y+1][x] = next;
q.push(ii(x, y+1));
}
if ('a' <= A[y][x] && A[y][x] <= 'z') {
int k = A[y][x]-'a';
if (!used[k]) {
used[k] = 1;
FORE (xy, teles[k]) {
int x_, y_; tie(x_, y_) = xy;
if ((x_ != x || y_ != y) && steps[y_][x_] < 0) {
steps[y_][x_] = next;
q.push(ii(x_, y_));
}
}
}
}
}
return steps[gy][gx];
}
int main() {
cout << fixed << setprecision(15);
cin >> H >> W;
REP (h, H) cin >> A[h];
cout << solve() << endl;
} | #include <vector>
#include <queue>
#include <functional>
#include <utility>
using namespace std;
// E[i]: vertexes which can be reased from i
// W[i]: their weight
template<class T>
vector<T> dijkstra(vector<vector<int>> E, vector<vector<T>> W, int s, T inf)
{
int n = (int)E.size();
vector<T> D(n, inf);
typedef pair<T,int> P;
priority_queue<P,vector<P>,greater<P>> Q;
D[s] = T();
Q.push(make_pair(D[s], s));
while (!Q.empty())
{
T d = Q.top().first;
int p = Q.top().second;
Q.pop();
if (d > D[p])
continue;
for (int i=0; i<(int)E[p].size(); i++)
{
int t = E[p][i];
if (d+W[p][i] < D[t])
D[t] = d+W[p][i],
Q.push(make_pair(D[t], t));
}
}
return D;
}
#include <iostream>
int main()
{
int H, W;
cin>>H>>W;
vector<string> a(H);
for (string &t: a)
cin>>t;
vector<vector<int>> E(W*H+26), We(W*H+26);
int S, G;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
for (int y=0; y<H; y++)
for (int x=0; x<W; x++)
if (a[y][x]!='#')
{
if (a[y][x]=='S')
S = y*W+x;
if (a[y][x]=='G')
G = y*W+x;
for (int d=0; d<4; d++)
{
int tx = x+dx[d];
int ty = y+dy[d];
if (0<=tx && tx<W && 0<=ty && ty<H)
{
E[y*W+x].push_back(ty*W+tx);
We[y*W+x].push_back(1);
}
}
if ('a'<=a[y][x] && a[y][x]<='z')
{
E[y*W+x].push_back(W*H+a[y][x]-'a');
We[y*W+x].push_back(1);
E[W*H+a[y][x]-'a'].push_back(y*W+x);
We[W*H+a[y][x]-'a'].push_back(0);
}
}
int ans = dijkstra(E, We, S, W*H)[G];
cout<<(ans<W*H ? ans : -1)<<endl;
}
|
#include<bits/stdc++.h>
#define rep(i,n) for (int i=0; i<n; i++)
#define REP(i,x,n) for (int i=x; i<n; i++)
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
using T = tuple<int,int,int>;
using P = pair<int,int>;
int main(){
int n, k;
cin >> n >> k;
vvi field(n, vi(n));
rep(i,n)rep(j,n) cin >> field[i][j];
vi num(n-1);
rep(i,n-1){
num[i] = i+1;
}
// for (auto ele : num){
// printf("%d ",ele);
// }
// cout << endl;
int ans = 0;
do {
ll sum = field[0][num[0]];
int pre = num[0];
rep(i,n-1){
sum += field[pre][num[i]];
pre = num[i];
}
sum += field[num[n-2]][0];
if (sum == k) ans++;
} while(next_permutation(num.begin(), num.end()));
cout << ans << endl;
}
| #include <iostream>
#include <string>
#define ll long long
using namespace std;
const int N = 2e5+10;
int cnt[N][26];
int main() {
string s; cin >> s;
int n = s.size();
ll ans = 0;
for(int i = n - 1; i >= 0; --i) {
for(int x = 0; x < 26; ++x) {
cnt[i][x] = cnt[i+1][x];
}
++cnt[i][s[i] - 'a'];
}
int prev = n;
for(int i = n - 1; i >= 0; --i) {
int j = i;
while(j >= 0 && s[i] == s[j]) --j; ++j;
if(i - j + 1 >= 2) {
for(int x = i; x < prev; ++x) {
if(s[i] != s[x]) ++ans;
}
if(prev < n && s[prev] != s[i]) {
ans += n - prev;
}
prev = j;
}
i = j;
}
cout << ans << '\n';
} |
#include <bits/stdc++.h>
#define ll long long
#define lll long long
#define mp make_pair
#define pb push_back
#define inf 1000000001
#define vll vector <ll>
lll p = 1000000007;
using namespace std;
lll sq(lll i){
return i*i;
}
lll fact(ll n){
ll ans=1;
for(ll i=1; i<=n;i++)ans=(ans*i)%p;
return ans;
}
unsigned long long power(unsigned long long x, ll y)
{
unsigned long long res = 1; // Initialize result
x = x % p; // Update x if it is more than p
while (y > 0)
{
// If y is odd, multiply x with result
if (y & 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
lll modInverse(unsigned lll n, unsigned lll p)
{ return power(n, p - 2);
}
lll nCrModPFermat(unsigned lll n, unsigned lll r, lll fac[])
{
// If n<r, then nCr should return 0
if (n < r)
return 0;
// Base case
if (r == 0)
return 1;
return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p;
}
vector <ll> sieve(ll n){
vector <bool> prime (n+1, true);
prime[1]=false;
vll primes;
for(ll i=2; i<=sqrt(n); i++){
if(prime[i]){
for(ll j=i*i; j<=n; j+=i)prime[j]=false;
}
}
for(ll i=2; i<=n;i++)if(prime[i])primes.pb(i);
return primes;
}
vector <ll> factors(ll n){
//vector <ll> primes = sieve(sqrt(n));
vector <ll> v;
for(ll i=2; i<=sqrt(n); i++){
if(n%i==0){v.pb(i);v.pb(n/i);}
}
v.pb(n);
//if(v.size()==0)v.pb(n);
return v;
}
//***********************GRAPH ALGORITHMS*************************************************************
void dfs(vector <ll> v[], vector<ll> &df, ll cov[], ll i){ //Also outputed resultant dfs
df.pb(i);
for(ll j=0; j<v[i].size(); j++){if(cov[v[i][j]]==0){cov[v[i][j]]=cov[i]; dfs(v, df, cov, v[i][j]);}}
}
void djikstra (vector <pair<ll, ll>> v[], ll d[], ll par[], ll s, ll n){ //n^2 || d returns shortest dist // s for start
for(ll i=0; i<n;i++){d[i]=inf; par[i]=-1;}
par[s]=s; d[s]=0;
bool cov[n]; for(ll i=0; i<n;i++)cov[i]=0;
ll u=-1;
for(int j=0; j<n;j++)
{
u=-1;
for(ll i=0; i<n;i++){
if(!cov[i] && (d[i]<d[u] || u==-1))u=i;
}
cov[u]=1;
for(ll i=0; i<v[u].size(); i++){
if(d[v[u][i].first] > d[u]+v[u][i].second){d[v[u][i].first]=d[u]+v[u][i].second; /*cout << d[u] << " " <<v[u][i].second <<"\n"; */par[v[u][i].first]=u;}
}
}
}
ll bfs(vector<ll> v[], ll par[], ll root, ll finish, ll n) //par returns parent array
{
ll cov[n];
for(ll i=0; i<n;i++)cov[i]=0;
queue<ll> q;
cov[root]=0;
q.push(root);
while(!q.empty())
{
for(ll i=0;i<v[q.front()].size();i++)
{
if(cov[v[q.front()][i]]==0)
{
q.push(v[q.front()][i]);
cov[v[q.front()][i]]=cov[q.front()]+1;
par[v[q.front()][i]] = q.front();
}
if(v[q.front()][i]==finish)return cov[v[q.front()][i]];
}
q.pop();
}
return -1;
}
ll findConnComp(vector <ll> v[], ll cov[], ll n){
ll j=0;
vector <ll> df;
for(ll i=0; i<n;i++)cov[i]=0;
for(ll i=0; i<n; i++){
if(!cov[i]){cov[i]=++j; dfs(v, df, cov, i);}
}
return j;
}
//*****************CODE STARTS **************************************
ll summ(ll a[], ll n){
bool sp[n];
for(ll i=0; i<n;i++)sp[i]=0;
for(ll i=1; i<n-1; i++){
if(a[i]>a[i-1] && a[i]>a[i+1])sp[i]=1;
if(a[i]<a[i-1] && a[i]<a[i+1])sp[i]=1;
}
ll cnt=0;
for(ll i=1; i<n-1; i++){
if(sp[i])cnt++;
}
return cnt;
}
int main(){
/*ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);*/
ll x,y; cin >> x >> y;
if(abs(x-y)>=3)cout << "No\n";
else cout << "Yes\n";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef vector<int> vi;
typedef vector<vi> vvi;
int h,w;
vvi a(2010, vi(2010));
vvi opt(2010, vi(2010));
int main(){
cin>>h>>w;
rep(i,h)rep(j,w) {
char A; cin>>A;
if(A=='+') a[i][j]=1; else a[i][j]=-1;
}
opt[h-1][w-1]=0;
for(int i=h-1; i>=0; i--) {
for(int j=w-1; j>=0; j--) {
if((i+j)%2==0) {
int ma = -2000;
if(i==h-1 && j==w-1) continue;
if(i+1<=h-1) ma = max(ma,opt[i+1][j]+a[i+1][j]);
if(j+1<=w-1) ma = max(ma,opt[i][j+1]+a[i][j+1]);
opt[i][j] = ma;
} else {
int mi = 2000;
if(i==h-1 && j==w-1) continue;
if(i+1<=h-1) mi = min(mi,opt[i+1][j]-a[i+1][j]);
if(j+1<=w-1) mi = min(mi,opt[i][j+1]-a[i][j+1]);
opt[i][j] = mi;
}
}
}
if(opt[0][0]>0) puts("Takahashi");
else if (opt[0][0]<0) puts("Aoki");
else puts("Draw");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define intmax 0x7fffffff
#define halfintmax 0x3fffffff
typedef pair<int, int> pii;
typedef pair<int, char> pic;
const int N = 200009;
const int mod = 998244353;
ll n;
int main() {
cin >> n;
ll l = 0, r = 1.5e9;
while(l < r) {
ll mid = l + r + 1 >> 1;
if(mid * (mid + 1) / 2 <= n + 1) l = mid;
else r = mid - 1;
}
cout << n - l + 1;
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 gc getchar_unlocked
#define fo(i,n) for(int i=0;i<n;i++)
#define Fo(i,k,n) for(int i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define ll long long int
#define s(x) cin>>x
#define p(x) cout<<x<<"\n"
#define goog(x, y) cout<<"Case #"<<x<<": "<<y<<"\n"
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define INF 100000000000
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef long double ld;
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) {
uniform_int_distribution<int> uid(0,lim-1);
return uid(rang);
}
int mpow(int base, int exp);
void ipgraph(int n, int m);
int gcd(int a, int b) ;
const int mod = 1'000'000'007;
const int N = 3e5, M = N;
//=======================
vi g[N];
int a[N];
void solve() {
ll n;cin>>n;
//ll tp=1e9+1e8;
ll tp=2*floor(sqrt(n));
ll res;
ll st=0;
while(st<=tp)
{ ll m=(st+tp)/2;
if(m*(m+1)<=2*(n+1))
{res=m;st=m+1;}
else
tp=m-1;
}
p(n+1-res);
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
srand(chrono::high_resolution_clock::now().time_since_epoch().count());
int t = 1;
//cin >> t;
while(t--) {
solve();
}
return 0;
}
int mpow(int base, int exp) {
base %= mod;
int result = 1;
while (exp > 0) {
if (exp & 1) result = ((ll)result * base) % mod;
base = ((ll)base * base) % mod;
exp >>= 1;
}
return result;
}
void ipgraph(int n, int m){
int i, u, v;
while(m--){
cin>>u>>v;
//u--, v--;
g[u].pb(v);
g[v].pb(u);
}
}
int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
} |
#include<bits/stdc++.h>
#include<vector>
#include<string>
#include<time.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
bool flag[2510];
int t[51][51];
int p[51][51];
int dx[4];
int dy[4];
string direction = "UDLR";
string route;
string bestroute;
time_t start_time, end_time;
int nx,ny;
int mx;
int sm,maxsum;
bool is_legal_action(int nx,int ny,bool *flag){
if(0<=nx&&nx<50&&0<=ny&&ny<50&&flag[t[nx][ny]]==0){
return true;
}else{
return false;
}
}
int legallistlength(int x,int y,bool *flag){
int k=0;
rep(i,4){
nx = x + dx[i];
ny = y + dy[i];
if(is_legal_action(nx,ny,flag)){
k++;
}
}
return k;
}
void dfs(int x,int y,bool *flag){
end_time = time(NULL);
if(end_time - start_time>1.95)return;
flag[t[x][y]]=1;
sm+=p[x][y];
//sm++;
rep(i,4){
nx = x + dx[i];
ny = y + dy[i];
if(is_legal_action(nx,ny,flag)){
//cout << x <<" " << y <<" " << i << endl;
route.push_back(direction[i]);
dfs(nx,ny,flag);
route.pop_back();
}
}
if(maxsum<sm){
maxsum=sm;
bestroute=route;
}
sm-=p[x][y];
//sm--;
flag[t[x][y]]=0;
return;
}
/*void func(int x,int y,bool *flag,vector<char>& route){
flag[t[x][y]]=1;
int s=-1;
int nxt=-1;
rep(i,4){
nx = x + dx[i];
ny = y + dy[i];
if(is_legal_action(nx,ny,flag)){
if(s<=p[nx][ny]){
s=p[nx][ny];
nxt=i;
}
sm=0;
maxsum=-1;
dfs(nx,ny,flag,22);
if(maxsum>=s){
s=maxsum;
nxt=i;
}
//cout << i << " " << maxsum <<endl;
}
//cout <<x<<" " << y <<direction[i]<< nxt << endl;
}
flag[t[x][y]]=1;
if(nxt==-1){
for (auto i: route){
cout << i;
}
cout << "\n";
}else{
route.push_back(direction[nxt]);
func(x + dx[nxt],y + dy[nxt],flag,route);
route.pop_back();
}
flag[t[x][y]]=0;
return;
}*/
int main(){
start_time = time(NULL);
int si,sj;
scanf("%d %d",&si,&sj);
int M=0;
rep(i,50)rep(j,50){
cin >> t[i][j];
if(M<t[i][j])M=t[i][j];
}
M++;
rep(i,50)rep(j,50) scanf("%d", &p[i][j]);
rep(i,2500)flag[i]=0;
dx[0]=-1;
dx[1]=1;
dy[2]=-1;
dy[3]=1;
dfs(si,sj,flag);
for(auto i: bestroute){
cout << i;
}
cout << "\n";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define ll long long
#define pp pair<ll,ll>
#define ld long double
#define all(a) (a).begin(),(a).end()
#define mk make_pair
int inf=1000001000;
ll INF=2e18;
ll MOD=1000000007;
ll mod=998244353;
int main() {
int n,l;
cin >> n >> l;
vector<int> a(n+2),b(n+2);
rep(i,n) cin >> a[i+1];
rep(i,n) cin >> b[i+1];
a[0]=0;
a[n+1]=l+1;
b[0]=0;
b[n+1]=l+1;
ll ans=0;
for (int i=1;i<=n;i++){
int l=-1,r=n+1;
while(r-l>1){
int mid=(r+l)/2;
if (mid<=i) {
l=mid;
continue;
}
if (a[mid]-(mid-i)>=b[i]) r=mid;
else l=mid;
}
if (a[r]-(r-i)==b[i] && a[i]!=b[i]){
rep(j,r-i){
a[r-j-1]=a[r]-j-1;
ans++;
}
}
}
for (int i=n;i>=1;i--){
int l=0,r=n+2;
while(r-l>1){
int mid=(r+l)/2;
if (mid>=i) {
r=mid;
continue;
}
if (a[mid]+(i-mid)<=b[i]) l=mid;
else r=mid;
}
if (a[l]+(i-l)==b[i] && a[i]!=b[i]){
rep(j,i-l){
a[l+j+1]=a[l]+j+1;
ans++;
}
}
}
bool w=true;
rep(i,n){
if (a[i+1]!=b[i+1]) w=false;
}
if (w) cout << ans << endl;
else cout << -1 << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define MOD2 998244353
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define reps(i,s,n) for (int i = (int)(s); i < (int)(n); i++)
#define repit(i,C) for (auto i = (C).begin(); i != (C).end(); i++)
#define pr(a) cout << a
#define prl(a) cout << (a) << endl
#define prld(a) cout << setprecision(15)<< (a) << endl
#define allrange(a) a.begin(),a.end()
bool solve_translated(vector<pair<int,int>> &S,vector<pair<int,int>> &T){
int N = S.size();
int dx = S[0].first-T[0].first;
int dy = S[0].second-T[0].second;
bool flg = true;
reps(i,1,N){
if(!((S[i].first==T[i].first+dx) && (S[i].second==T[i].second+dy))){flg = false; break;}
}
return flg;
}
/*ピタゴラ三角形
5 12 13
8 15 17
3 4 5
*/
vector<pair<int,int>> Pitagora_rot(vector<pair<int,int>> &S,int a, int b , int c){
int N = S.size();
vector<pair<int,int>> PS(N);
auto pt0 = S[0];
PS[0] = pt0;
bool flg = true;
reps(i,1,N){
int dx = S[i].first - pt0.first;
int dy = S[i].second - pt0.second;
int x=dx*a-dy*b;
int y=dx*b+dy*a;
if(((x%c)!=0) || ((y%c)!=0)) {flg = false; break;}
PS[i] = make_pair(x/c+pt0.first, y/c+pt0.second);
}
if(flg) return PS;
else return vector<pair<int,int>>();
}
int main(){
std::cin.tie(0); // cinとcoutの同期を解除
std::ios::sync_with_stdio(false);
int N;cin >> N;
vector<pair<int,int>> S(N),T(N);
rep(i,N)
{
int x,y;
cin >> x >> y;
S[i].first = x;S[i].second = y;
}
rep(i,N)
{
int x,y;
cin >> x >> y;
T[i].first = x;
T[i].second = y;
}
sort(allrange(T));
/*ピタゴラ三角形
5 12 13
8 15 17
3 4 5
*/
/* vector<vector<int>> tri(12);
tri[0]={0,1,1};
tri[1]={-1,0,1};
tri[2]={0,-1,1};
tri[3]={1,0,1};
tri[4]={3,4,5};
tri[5]={4,3,5};
tri[6]={-3,4,5};
tri[7]={-4,3,5};
tri[8]={-3,-4,5};
tri[9]={-4,-3,5};
tri[10]={3,-4,5};
tri[11]={4,-3,5};*/
vector<vector<int>> tri(0);
tri.push_back(vector<int>({3,4,5}));
tri.push_back(vector<int>({4,3,5}));
tri.push_back(vector<int>({-1,0,1}));
tri.push_back(vector<int>({0,-1,1}));
tri.push_back(vector<int>({-3,4,5}));
tri.push_back(vector<int>({-4,3,5}));
tri.push_back(vector<int>({0,1,1}));
tri.push_back(vector<int>({1,0,1}));
tri.push_back(vector<int>({-3,-4,5}));
tri.push_back(vector<int>({-4,-3,5}));
tri.push_back(vector<int>({3,-4,5}));
tri.push_back(vector<int>({4,-3,5}));
// tri.push_back(vector<int>({-5, -12, 13}));
// tri.push_back(vector<int>({-12, -5, 13}));
// tri.push_back(vector<int>({-8,-15,17}));
// tri.push_back(vector<int>({-15,-8,17}));
// tri.push_back(vector<int>({5, 12, 13}));
// tri.push_back(vector<int>({12, 5, 13}));
// tri.push_back(vector<int>({8,15,17}));
// tri.push_back(vector<int>({15,8,17}));
// tri.push_back(vector<int>({-5, 12, 13}));
// tri.push_back(vector<int>({-12, 5, 13}));
// tri.push_back(vector<int>({-8,15,17}));
// tri.push_back(vector<int>({-15,8,17}));
// tri.push_back(vector<int>({5, -12, 13}));
// tri.push_back(vector<int>({12, -5, 13}));
// tri.push_back(vector<int>({8,-15,17}));
// tri.push_back(vector<int>({15,-8,17}));
bool flg;
if(S.size()==2 && T.size()==2){
auto sx = S[0].first-S[1].first;
auto sy = S[0].second-S[1].second;
auto tx = T[0].first-T[1].first;
auto ty = T[0].second-T[1].second;
flg= (sx*sx+sy*sy==tx*tx+ty*ty);
} else {
rep(j,tri.size()){
auto S2 = Pitagora_rot(S,tri[j][0],tri[j][1],tri[j][2]);
if(S2.empty()) continue;
sort(allrange(S2));
flg = solve_translated(S2,T);
if(flg) break;
}
}
if(flg) prl("Yes"); else prl("No");
}
| #include<bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1e9+7;
int main(){
int n; cin>>n;
vector<complex<double>>a(n),c(n);
for(int i=0; i<n; i++){
int a1,a2;
cin>>a1>>a2;
a.at(i).real(a1);
a.at(i).imag(a2);
}
for(int i=0; i<n; i++){
int a1,a2;
cin>>a1>>a2;
c.at(i).real(a1);
c.at(i).imag(a2);
}
if(n==1){
cout<<"Yes"<<endl;
return 0;
}
auto a1 = a.at(0);
auto a2 = a.at(1);
for(int t1=0; t1<n; t1++){
for(int t2=0; t2<n; t2++){
if(t1==t2)continue;
auto c1=c.at(t1),c2=c.at(t2);
if(abs( abs(a1-a2)-abs(c1-c2) )>0.0001)continue;
auto b = a;
auto d = c;
for(int i=0; i<n; i++){
b.at(i) -= a1;
d.at(i) -= c1;
}
double ara = arg(b.at(1));
double arc = arg(d.at(t2));
complex<double> e1=polar(1.,-ara),e2=polar(1.,-arc);
for(int i=0; i<n; i++){
b.at(i) *= e1;
d.at(i) *= e2;
}
vector<int> to(n,-1);
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
if(abs(b.at(i)-d.at(j))<0.0001)to.at(i)=1;
}
}
int sum{};
for(int i=0; i<n; i++){
if(to.at(i)==1) sum++;
}
if(sum==n){
cout<<"Yes"<<endl;
return 0;
}
}
}
cout<<"No"<<endl;
} |
#include<bits/stdc++.h>
using namespace std;
int main(){
int N; cin >> N;
int64_t A[1000000],b,c,x=1;
for(int i=0;i<N;i++){
cin >> A[i];
}
b=A[0]; c=A[0]*A[0];
int64_t S=0,T=0;
for(int i=1;i<N;i++){
S+=(A[i]*A[i]*x+c);
T+=(A[i]*b);
b+=A[i]; c+=A[i]*A[i]; x++;
}
S-=2*T;
cout << S << endl;
return 0;
} | #include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <iostream>
#include <vector>
#include <map>
#include <memory>
#include <list>
#include <deque>
#include <queue>
#include <iomanip>
#include <set>
#include <stack>
#include <numeric>
#include <unordered_set>
#include <unordered_map>
#include <string.h>
#include <functional>
#include <sstream>
using namespace std;
#define REP(i,n) for (decltype(n) i = 0; i < n; i++)
#define THE_MOD 1'000'000'007
using ll = long long;
using ivec = vector<int>;
using lvec = vector<long>;
using ipair = pair<int, int>;
using llvec = vector<ll>;
using llpair = pair<ll, ll>;
#define umap unordered_map
#define uset unordered_set
template<typename T> ostream& operator <<(ostream& os, const vector<T> &v) { auto n = v.size(); REP(i,n) os << (i ? " " : "") << v[i]; return os; }
template<typename T> istream& operator >>(istream& is, vector<T> &v) { for(auto &e : v) is >> e; return is; }
template<typename T, typename U> ostream& operator <<(ostream& os, const pair<T, U> &p) { os << p.first << " " << p.second; return os; }
template<typename T, typename U> istream& operator >>(istream& is, pair<T, U> &p) { is >> p.first >> p.second; return is; }
void in() {} template <typename T, typename... Args> void in(T& t, Args& ...args) { cin >> t; in(args...); }
void out() { cout << endl; } template <typename T, typename... Args> void out(const T& t, const Args& ...args) { cout << t; if (sizeof...(args)) cout << " "; out(args...); }
constexpr ll LLINF = numeric_limits<ll>::max();
template<class T> void sort(T& v) { sort(v.begin(), v.end()); }
template<class T> void rsort(T& v) { sort(v.begin(), v.end(), greater<typename T::value_type>()); }
void YN(bool b) { cout << (b ? "Yes" : "No") << endl; }
template <typename T = ll, typename U = ll>
vector<T> Dijkstra(const vector<vector<pair<U,T>>>& edges, U start, U numNodes)
{
using Node = pair<U,T>;
auto compare = [](Node a, Node b){ return a.second > b.second;};
priority_queue<Node, vector<Node>, decltype(compare)> q{compare};
vector<T> costs(numNodes, numeric_limits<T>::max());
q.emplace(start, 0);
while (!q.empty()) {
auto [from, cost] = q.top();
q.pop();
if (cost > costs[from]) continue;
for (const auto& edge : edges[from]) {
auto to = edge.first;
auto newCost = cost + edge.second;
if (newCost < costs[to]) {
costs[to] = newCost;
q.emplace(to, newCost);
}
}
}
return costs;
}
struct Range {
ll x;
ll y;
Range(ll x, ll y) : x(x), y(y)
{
if (y > x) swap(x,y);
}
struct Iterator {
using difference_type = ptrdiff_t;
using value_type = ll;
using pointer = ll*;
using reference = ll&;
using iterator_category = std::random_access_iterator_tag;
Iterator() = default;
Iterator(ll n) : n(n) {}
ll n;
bool operator!=(const Iterator&rhs) const { return n != rhs.n; }
bool operator==(const Iterator&rhs) const { return n == rhs.n; }
Iterator& operator++() { n++; return *this; }
Iterator& operator--() { n--; return *this; }
Iterator& operator+=(ll x) { n+=x; return *this; }
ll operator*() const { return n; }
ptrdiff_t operator-(const Iterator&rhs) const { return n - rhs.n; }
};
Iterator begin() { return Iterator(x); }
Iterator end() { return Iterator(y); }
};
ll extgcd(ll a, ll b, ll &x, ll &y)
{
/*
a = pb*q; // p = a / b, q = a % b
ax + by
= pbx + qx + by
= b(px + y) + qx
= bY + qX // Y = px + y, X = x -> y = Y - px
*/
if (b == 0) {
x = 1;
y = 0;
return a;
}
ll d = extgcd(b, a % b, y, x);
y -= a / b * x;
return d;
}
ll run()
{
ll n;in(n);
ll s = 0;
ll s2 = 0;
REP(i,n) {
ll a;in(a);
s += a;
s2 += a * a;
}
return n * s2 - s * s;
}
int main()
{
cout << fixed << setprecision(15);
auto r = run();
//YN(r);
out(r);
//cout << r << endl;
return 0;
}
|
#include <iostream>
#include <vector>
using namespace std;
#include <iostream>
#include <cassert>
template<long long mod>
class modint{
private:
using T = long long;
T a;
public:
constexpr modint(const long long x = 0) noexcept : a((x%mod+mod)%mod) {}
constexpr T& value() noexcept { return a; }
constexpr const T& value() const noexcept { return a; }
constexpr modint operator-() const noexcept {
return modint(0) -= *this;
}
constexpr modint operator+(const modint& rhs) const noexcept {
return modint(*this) += rhs;
}
constexpr modint operator-(const modint& rhs) const noexcept {
return modint(*this) -= rhs;
}
constexpr modint operator*(const modint& rhs) const noexcept {
return modint(*this) *= rhs;
}
constexpr modint operator/(const modint& rhs) const noexcept {
return modint(*this) /= rhs;
}
constexpr modint& operator+=(const modint& rhs) noexcept {
a += rhs.a;
if(a >= mod) a -= mod;
return *this;
}
constexpr modint &operator-=(const modint& rhs) noexcept {
if(a < rhs.a) a += mod;
a -= rhs.a;
return *this;
}
constexpr modint& operator*=(const modint& rhs) noexcept {
a = a*rhs.a%mod;
return *this;
}
constexpr modint& operator/=(const modint& rhs) noexcept {
return *this *= rhs.inv();
}
constexpr bool operator==(const modint& rhs) const noexcept {
return a == rhs.a;
}
constexpr bool operator!=(const modint& rhs) const noexcept {
return not (*this == rhs);
}
constexpr modint pow(long long k) const noexcept {
modint ret(1);
modint x = k > 0 ? *this : this->inv();
k = abs(k);
while(k > 0){
if(k&1) ret *= x;
x *= x;
k >>= 1;
}
return ret;
}
constexpr modint inv() const noexcept {
return pow(mod-2);
}
friend std::ostream& operator<<(std::ostream &os, const modint &X) noexcept {
return os << X.a;
}
friend std::istream& operator>>(std::istream &is, modint &X) noexcept {
is >> X.a;
X.a %= mod;
if(X.a < 0) X.a += mod;
return is;
}
};
template<typename T>
T power(T x, long long k){
T ret{1};
while(k > 0){
if(k&1) ret *= x;
x *= x;
k /= 2;
}
return ret;
}
int main(){
using mint = modint<998244353>;
int n, m;
cin >> n >> m;
mint ans = 0;
vector<mint> pow_m(n+1);
pow_m[0] = 1;
for(int i = 0; i < n; ++i)
pow_m[i+1] = pow_m[i]*m;
for(int i = 1; i <= m; ++i){
ans += power<mint>(m-i+1,n) - power<mint>(m-i,n);
mint a = 1, b = 1;
for(int j = 1; j+1 <= n; ++j){
a *= m-i+1, b *= m-i;
if(j+2 <= n){
mint p = a - b;// i が 1 回以上使われる
p *= n-j-1;// 長さ j+2 の区間の取り方
p *= (i-1)*(i-1);//両端
p *= pow_m[n-j-2];//残り
ans += p;
}
mint q = a - b;
q *= 2;
q *= i-1;//両端
q *= pow_m[n-j-1];//残り
ans += q;
}
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(x) (int) x.size()
#define cat(x) cerr << #x << " = " << x << endl
#define all(x) x.begin(), x.end()
#define rep(i, j, n) for (int i = j; i <= n; ++i)
#define per(i, j, n) for (int i = n; j <= i; --i)
using ll = long long;
using ld = long double;
using namespace std;
const int N = 1e5 + 505;
int n, L, a[N], b[N], d[N];
ll ans;
void solve(int l, int r) {
if (d[l] > 0) {
rep(i, l, r) {
a[i] = L - a[i] + 1;
b[i] = L - b[i] + 1;
}
reverse(a + l, a + r + 1);
reverse(b + l, b + r + 1);
}
//~ printf ("%d -- %d\n", l, r);
//~ rep(i, l, r)
//~ cout << a[i] << " " << b[i] << endl;
int j = l, i = l + 1;
while (i <= r) {
while (i <= r && a[j] + i - j == b[i]) {
ans++;
i++;
}
if (i == r + 1) break;
//~ cat(i);
//~ cat(j);
//~ cat(a[j + 2]);
//~ cat(b[i]);
j++;
while (j < i && a[j] + i - j != b[i]) j++;
if (i == j) {
printf ("-1\n");
exit(0);
}
while (a[j + 1] + i - (j + 1) == b[i]) j++;
ans += i - j - 1;
}
if (d[l] > 0) {
rep(i, l, r) {
a[i] = L - a[i] + 1;
b[i] = L - b[i] + 1;
}
reverse(a + l, a + r + 1);
reverse(b + l, b + r + 1);
}
}
int sign(int x) {
return (x > 0) - (x < 0);
}
int main() {
scanf ("%d%d", &n, &L);
a[0] = b[0] = 0;
a[n + 1] = b[n + 1] = L + 1;
rep(i, 1, n) scanf ("%d", a + i);
rep(i, 1, n) scanf ("%d", b + i);
rep(i, 1, n) d[i] = b[i] - a[i];
for (int i = 1; i <= n;) {
int j = i;
while (j <= n && sign(d[i]) == sign(d[j])) j++;
if (d[i] < 0)
solve(i - 1, j - 1);
if (d[i] > 0) {
if (d[j] < 0) return !printf ("-1\n");
solve(i, j);
}
i = j;
}
printf ("%lld\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
vector<int> B(N), mem_A(N,0), mem_B(N,0);
for (int i = 0; i < N; i++) {
int A;
cin >> A;
mem_A.at(A - 1)++; //mem_A.at(n - 1) = 数列Aのうちnの値の数字の個数
}
for (int i = 0; i < N; i++) {
cin >> B.at(i); //B.at(n - 1) = 数列Bの第n項の値
}
for (int i = 0; i < N; i++) {
int C;
cin >> C;
mem_B.at(i) = B.at(C - 1); //mem_B.at(n - 1) = 数列Bの第「数列Cの第n項の値」項の値
}
long long count = 0;
for (int j = 0; j < N; j++) {
count += mem_A.at(mem_B.at(j) - 1);
}
cout << count << endl;
}
| #pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <complex>
#include <queue>
#include <set>
#include <unordered_set>
#include <list>
#include <chrono>
#include <random>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <stack>
#include <iomanip>
#include <fstream>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef vector<ll> vll;
typedef vector<int> vi;
ll MOD = 998244353;
double eps = 1e-12;
#define forn(i, e) for (ll i = 0; i < e; i++)
#define forsn(i, s, e) for (ll i = s; i < e; i++)
#define rforn(i, s) for (ll i = s; i >= 0; i--)
#define rforsn(i, s, e) for (ll i = s; i >= e; i--)
#define ln "\n"
#define dbg(x) cout << #x << " = " << x << ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define INF 2e18
#define fast_cin() \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define allrev(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
void solve()
{
int n;
cin >> n;
vi arr(n), b(n);
forn(i, n)
{
cin >> arr[i];
}
forn(i, n)
{
cin >> b[i];
}
map<int, int> m;
forn(i, n)
{
int x;
cin >> x;
--x;
int val = b[x];
m[val]++;
}
ll cnt = 0;
forn(i, n)
{
if (m.find(arr[i]) != m.end())
{
cnt += m[arr[i]];
}
}
cout << cnt << ln;
}
int main()
{
fast_cin();
ll t = 1;
// cin >> t;
for (int it = 0; it < t; it++)
{
// cout << "Case #" << it + 1 << ": ";
solve();
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<ll, ll>;
using Pld = pair<ld, ld>;
using Vec = vector<ll>;
using VecP = vector<P>;
using VecB = vector<bool>;
using VecC = vector<char>;
using VecD = vector<ld>;
using VecS = vector<string>;
template <class T>
using Vec2 = vector<vector<T>>;
#define REP(i, m, n) for(ll i = (m); i < (n); ++i)
#define REPN(i, m, n) for(ll i = (m); i <= (n); ++i)
#define REPR(i, m, n) for(ll i = (m)-1; i >= (n); --i)
#define REPNR(i, m, n) for(ll i = (m); i >= (n); --i)
#define rep(i, n) REP(i, 0, n)
#define repn(i, n) REPN(i, 1, n)
#define repr(i, n) REPR(i, n, 0)
#define repnr(i, n) REPNR(i, n, 1)
#define all(s) (s).begin(), (s).end()
template <class T1, class T2>
bool chmax(T1 &a, const T2 b) { if (a < b) { a = b; return true; } return false; }
template <class T1, class T2>
bool chmin(T1 &a, const T2 b) { if (a > b) { a = b; return true; } return false; }
template <class T>
istream &operator>>(istream &is, vector<T> &v) { for (T &i : v) is >> i; return is; }
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) { for (const T &i : v) os << i << ' '; return os; }
void _co() { cout << '\n'; }
template <class Head, class... Tail>
void _co(Head&& head, Tail&&... tail) { cout << ' ' << head; _co(forward<Tail>(tail)...); }
template <class Head, class... Tail>
void co(Head&& head, Tail&&... tail) { cout << head; _co(forward<Tail>(tail)...); }
void ce() { cerr << '\n'; }
template <class Head, class... Tail>
void ce(Head&& head, Tail&&... tail) { cerr << head << ' '; ce(forward<Tail>(tail)...); }
void sonic() { ios::sync_with_stdio(false); cin.tie(nullptr); }
void setp(const int n) { cout << fixed << setprecision(n); }
constexpr int64_t LINF = 1000000000000000001;
constexpr int64_t MOD = 1000000007;
constexpr int64_t MOD_N = 998244353;
constexpr long double EPS = 1e-11;
const double PI = acos(-1);
int main(void) {
ll n;
cin >> n;
rep(i, n) {
co((i * 2) % n + 1, (i * 2 + 1) % n + 1);
}
return 0;
} | #include <bits/stdc++.h>
const long long INF = 1e9;
const long long MOD = 1e9 + 7;
//const long long MOD = 998244353;
const long long LINF = 1e18;
using namespace std;
#define YES(n) cout << ((n) ? "YES" : "NO" ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No" ) << endl
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE" : "IMPOSSIBLE" ) << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible" ) << endl
#define dump(x) cout << #x << " = " << (x) << endl
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) for(int i=0;i<(n);++i)
#define REPR(i,n) for(int i=n;i>=0;i--)
#define COUT(x) cout<<(x)<<endl
#define SCOUT(x) cout<<(x)<<" "
#define VECCOUT(x) for(auto&youso_: (x) )cout<<right<<setw(1)<<youso_<<" ";cout<<endl
#define ENDL cout<<endl
#define CIN(...) int __VA_ARGS__;CINT(__VA_ARGS__)
#define LCIN(...) long long __VA_ARGS__;CINT(__VA_ARGS__)
#define SCIN(...) string __VA_ARGS__;CINT(__VA_ARGS__)
#define VECCIN(x) for(auto&youso_: (x) )cin>>youso_
#define mp make_pair
#define PQ priority_queue<long long>
#define PQG priority_queue<long long,V,greater<long long>>
typedef long long ll;
typedef vector<long long> vl;
typedef vector<long long> vi;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<vl> vvl;
typedef vector<vi> vvi;
typedef vector<vb> vvb;
typedef vector<vc> vvc;
typedef pair<long long, long long> pll;
#define COUT(x) cout<<(x)<<endl
void CINT(){}
template <class Head,class... Tail>
void CINT(Head&& head,Tail&&... tail){
cin>>head;
CINT(move(tail)...);
}
template<class T>
void mod(T &x) {
x %= MOD;
x += MOD;
x %= MOD;
}
ll GCD(ll a, ll b) {
if(b == 0) return a;
else return GCD(b, a%b);
}
struct COMB{
vl fact, fact_inv, inv;
void init_nCk(long long SIZE) {
fact.resize(SIZE + 5);
fact_inv.resize(SIZE + 5);
inv.resize(SIZE + 5);
fact.at(0) = fact.at(1) = fact_inv.at(0) = fact_inv.at(1) = inv.at(1) = 1;
for(long long i = 2; i < SIZE + 5; i++) {
fact.at(i) = fact.at(i - 1)*i%MOD;
inv.at(i) = MOD - inv.at(MOD%i)*(MOD/i)%MOD;
fact_inv.at(i) = fact_inv.at(i - 1)*inv.at(i)%MOD;
}
}
long long nCk (long long n, long long k) {
assert(!(n < k));
assert(!(n < 0 || k < 0));
return fact.at(n)*(fact_inv.at(k)*fact_inv.at(n - k)%MOD)%MOD;
}
};
ll extGCD(ll a, ll b, ll &x, ll &y) {
if(b == 0) {
x = 1;
y = 0;
return a;
}
ll d = extGCD(b, a%b, y, x);
y -= a/b*x;
return d;
}
void Main() {
LCIN(N);
vl fib(92, 1);
for(int i = 2; i < 92; i++) {
fib.at(i) = fib.at(i - 1) + fib.at(i - 2);
}
vl fibs(92, 1);
ll memo = N;
for(int i = 0; i < 92; i++) {
int j = 91 - i;
fibs.at(j) = N/fib.at(j);
N %= fib.at(j);
}
vl fibsc(30, 1);
for(int i = 0; i < 30; i++) {
fibsc.at(i) = i;
}
// VECCOUT(fibsc);
// VECCOUT(fibs);
queue<int> ans;
bool c = 0;
int KKK = 1;
for(int i = 1; i < 92 - KKK; i++) {
int j = 91 - i;
if(fibs.at(j) && c) {
ans.push(2 - (i)%2);
}
if(c) {
ans.push(4 - (i + 1)%2);
}
if(fibs.at(j) && !c) {
ans.push(1);
ans.push(2);
c = 1;
}
}
cout << ans.size() << endl;
while(ans.size()) {
ll tmp = ans.front();
ll res = tmp;
cout << res << endl;
ans.pop();
}
}
int main() {
cout << fixed << setprecision(15);
Main();
return 0;
}
|
#include <iostream>
#include <algorithm>
using namespace std;
int a=0, i;
long long ans=0, n, s=1;
int main(){
cin>>n;
while(true){
s*=10;
a++;
if(n>=s)
ans+=(s-s/10)*((a-1)/3);
else{
ans+=(n-s/10+1)*((a-1)/3);
break;
}
}
cout<<ans<<endl;
} | // どこが間違っているか分からない時は,簡単なミス(型,スペル,入力or出力など)をしていたり問題文の見落としがあったりする. 一度落ち着いて頭をリセットしよう.
// 計算途中のオーバーフローに注意しよう
#include "bits/stdc++.h"
#define REP(i, n, N) for(ll i=(n); i<(N); i++)
#define RREP(i, n, N) for(ll i=(N-1); i>=(n); i--)
#define LREP(lst,itr) for(auto itr = lst.begin(); itr != lst.end(); ++itr)
#define CK(n, a, b) ((a)<=(n)&&(n)<(b))
#define ALL(v) (v).begin(),(v).end()
#define MCP(a, b) memcpy(b,a,sizeof(b))
#define P(s) cout<<(s)<<endl
#define P2(a, b) cout<<(a)<<" "<<(b)<<endl
#define P3(a, b, c) cout<<(a)<<" "<<(b)<<" "<<(c)<<endl
#define PP(val, pre) cout<<fixed<<setprecision(pre)<<val<<endl
#define V2(T) vector<vector<T>>
typedef long long ll;
using namespace std;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
int main(){
ll N, ans=0;
ll first = 999;
ll second = 999999;
ll third = 999999999;
ll fourth = 999999999999;
ll fifth = 999999999999999;
cin >> N;
if(N>first){
ans += N-first;
}
if(N>second){
ans += N-second;
}
if(N>third){
ans += N-third;
}
if(N>fourth){
ans += N-fourth;
}
if(N>fifth){
ans += N-fifth;
}
P(ans);
} |
//#include<i_am_noob_orz>
#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define int ll
#define ull unsigned long long
#define pii pair<int,int>
#define X first
#define Y second
#define mod ((ll)1e9+7)
#define pb push_back
#define mp make_pair
#define abs(x) ((x)>0?(x):(-(x)))
#define F(n) Fi(i,n)
#define Fi(i,n) Fl(i,0,n)
#define Fl(i,l,n) for(int i=l;i<n;i++)
#define memres(a) memset(a,0,sizeof(a))
#define all(a) a.begin(),a.end()
#define sz(a) ((int)a.size())
#define ceiling(a,b) (((a)+(b)-1)/(b))
#define endl '\n'
#define bit_count(x) __builtin_popcountll((x))
#define ykh mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define jimmy_is_kind false
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> rbtree;
//#define LOCAL
#ifdef LOCAL
#define bug(...) cerr<<"#"<<__LINE__<<' '<<#__VA_ARGS__<<"- ", _do(__VA_ARGS__)
template<typename T> void _do(T && x) {cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T && x, S&&...y) {cerr<<x<<", "; _do(y...);}
#define IOS()
#else
#define IOS() ios_base::sync_with_stdio(0), cin.tie(0)
#define endl '\n'
#define bug(...)
#endif
int add(int a,int b){return(a+b>=mod?a+b-mod:a+b);}
int sub(int a,int b){return(a<b?a+mod-b:a-b);}
int po(int a,int b){
if(b==0)return 1;
if(b==1)return(a%mod);
int tem=po(a,b/2);
if(b&1)return(((tem*tem)%mod)*a)%mod;
else return(tem*tem)%mod;
}
int gcd(int a,int b){if(b==0)return a;return gcd(b,a%b);}
signed main(){
IOS();
int n;
cin>>n;
int lg=__lg(n)+1;
F(n){
int x=(i+1)*2;
if((x%n)==0)x=n;
else x=(x%n);
cout<<x<<" ";
x=(i+1)*2+1;
if((x%n)==0)x=n;
else x=(x%n);
cout<<x<<endl;
}
return 0;
} | #include <bits/stdc++.h>
#define SZ(v) ((int)(v).size())
using namespace std;
typedef long long ll;
const int MAXN = 1001;
int N;
int a[MAXN], b[MAXN];
int main(void)
{
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> N;
for (int i(1); i <= N; ++i)
cout << 1 + (2 * i)%N << ' '<< 1 + (2 * i + 1) % N << '\n';
}
|
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 2e5 + 5, mod = 1e9 + 7;
char s[N];
int a[N], k;
int add (int a, int b) {
return ((a += b) >= mod) ? (a - mod) : a;
}
int dp[N][20], K;
int f (int l, int k) {
if (~dp[l][k]) return dp[l][k];
if (l == 0) return k == K;
dp[l][k] = (ll)k * f(l - 1, k) % mod;
if (k)
dp[l][k] = add(dp[l][k], (ll)f(l - 1, k + 1) * (16 - k) % mod);
return dp[l][k];
}
int main() {
memset (dp, -1, sizeof (dp));
cin >> (s + 1) >> K;
int len = strlen(s + 1);
for (int i = 1; i <= len; ++i) {
if (isdigit(s[i])) a[i] = s[i] - '0';
else a[i] = s[i] - 'A' + 10;
}
int ans = 0;
for (int i = len; i >= 2; --i) {
for (int j = 1; j < 16; ++j) ans += f (len - i, 1), ans %= mod;
ans %= mod;
}
// cerr << ans << '\n';
int st = 0;
for (int i = 1; i <= len; ++i) {
for (int j = ((i == 1) ? 1 : 0); j < a[i]; ++j) {
int newst = st | (1 << j), cnt = __builtin_popcount(newst);
// cerr << len - i << " " << cnt << ":" << f(len - i, cnt) << '\n';
ans = add(ans, f(len - i, cnt));
}
st |= (1 << a[i]);
}
if (__builtin_popcount(st) == K) ans = add(ans, 1);
cout << ans << '\n';
return 0;
}
| #include<bits/stdc++.h>
#define maxn 2000010
using namespace std;
typedef long long LL;
int read()
{
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
return s*w;
}
int n,m,a[maxn],s[maxn<<2],c[maxn<<2];
void ins(int k,int l,int r,int p,int vl)
{
if(l==r) return c[k]+=vl,s[k]=(c[k]?1:0),void(0);int mid=l+r>>1;
p>mid?ins(k<<1|1,mid+1,r,p,vl):ins(k<<1,l,mid,p,vl),s[k]=s[k<<1]+s[k<<1|1];
}
int ask(int k,int l,int r)
{
if(l==r) return l;int mid=l+r>>1;
if(s[k<<1]==mid-l+1) return ask(k<<1|1,mid+1,r);
else return ask(k<<1,l,mid);
}
signed main()
{
int ans=0x7fffffff;
n=read(),m=read();
for(int i=1;i<=n;i++) a[i]=read()+1;
for(int i=1;i<=m;i++) ins(1,1,n+1,a[i],1);
ans=min(ans,ask(1,1,n+1));
for(int i=m+1;i<=n;i++) ins(1,1,n+1,a[i-m],-1),ins(1,1,n+1,a[i],1),ans=min(ans,ask(1,1,n+1));
cout<<ans-1<<'\n';
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main(){
int x;
int k;
cin >> x >> k;
int num = x;
for(int j = 0; j < k; ++j){
int n = 0;
vector<int> v;
while(num != 0){
v.push_back(num%10);
num /= 10;
n++;
}
sort(v.begin(), v.end());
int ans =0;
for(int i = 0; i < n; i++){
ans += (v[n-1-i]-v[i])*pow(10,n-i-1);
}
num = ans;
}
cout << num << endl;
} | #include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
using namespace std;
int inc(int n){
string s = to_string(n);
sort(s.begin(), s.end());
int val = stoi(s);
return val;
}
int dec(int n){
string s = to_string(n);
sort(s.begin(),s.end(), greater<char>());
int val = stoi(s);
return val;
}
int solve(int n){
int x= inc(n);
int y= dec(n);
return y-x;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);
ll n,k;
cin>>n>>k;
while(k){
n= solve(n);
k--;
}
cout<<n<<endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int INF = 1e9;
const int MOD = 1e9+7;
const ll LINF = 1e18;
#define REP(i,n) for(int i=0;i<(n);++i)
#define COUT(x) cout<<(x)<<"\n"
#define COUT16(x) cout << fixed << setprecision(16) << (x) << "\n";
int main(){
//input
int n,q,t,a,b;
string s;
cin >> n >> s >> q;
//input-計算
int flip = 0;//正常
for(int i=0;i<q;i++){
cin >> t >> a >> b;
--a;--b;
if(t==1){
if(flip==0){//正常
char s1 = s[a],s2 = s[b];
s[b] = s1;s[a] = s2;
}
else{//反転
if(a<n&&b<n){
char s1 = s[n+a],s2 = s[n+b];
s[n+b] = s1;s[n+a] = s2;
}
else if(a<n&&n<=b){
char s1 = s[n+a],s2 = s[b-n];
s[b-n] = s1;s[n+a] = s2;
}
else{
char s1 = s[a-n],s2 = s[b-n];
s[b-n] = s1;s[a-n] = s2;
}
}
}
else{
if(flip==0)flip = 1;
else flip = 0;
}
}
if(flip)cout << s.substr(n,n) << s.substr(0,n) << endl;
else COUT(s);
}
| // for(int i = 0; i < n; i++) {cin >> a[i];}
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define gcd __gcd
#define F first
#define S second
#define pii pair<int,int>
using ll = long long;
bool isSubsequence(string s, string t) {
int index = 0;
if(s.length() == 0) {
return true;
}
for(int i = 0; i < t.length(); i++) {
if(s.at(index) == t.at(i)){
index++;
}
if(index == s.length()) {
return true;
}
}
return false;
}
bool isPrime(int n) {
for(int i = 2; i < int(sqrt(n))+1; i++) {
if(n%i==0) {
return false;
}
}
return true;
}
bool isInt(double n) {
if(floor(n) == ceil(n)) {
return true;
}
return false;
}
bool isPalindrome(string s) {
int n = s.length();
for(int i = 0; i < n/2; i++) {
if(s.at(i) != s.at(n-i-1)) {
return false;
}
}
return true;
}
bool distinctDigits(int n) {
string s = to_string(n);
for(int i = 0; i < s.length(); i++) {
for(int j = i+1; j < s.length(); j++) {
if(s.at(i)==s.at(j)) {
return false;
}
}
}
return true;
}
bool distinctDigits(string s) {
for(int i = 0; i < s.length(); i++) {
for(int j = i+1; j < s.length(); j++) {
if(s.at(i)==s.at(j)) {
return false;
}
}
}
return true;
}
int digitSum(int n) {
int ans = 0;
string s = to_string(n);
for(int i = 0; i < s.length(); i++) {
ans += stoi(s.substr(i, 1));
}
return ans;
}
int swapBits(int n, int p1, int p2) {
int bit1 = (n >> p1) & 1;
int bit2 = (n >> p2) & 1;
int x = bit1 ^ bit2;
x = (x << p1) | (x << p2);
return n ^ x;
}
void display(vector<int> a) {
for(auto element : a) {
cout << element << ' ';
}
cout << endl;
}
void display(vector<ll> a) {
for(auto element : a) {
cout << element << ' ';
}
cout << endl;
}
void display(vector<string> a) {
for(auto element : a) {
cout << element << ' ';
}
cout << endl;
}
void solve()
{
int n;
cin >> n;
string s;
cin >> s;
int q;
cin >> q;
int flips = 0;
for(int i = 0; i < q; i++) {
int t, a, b;
cin >> t >> a >> b;
if(flips%2 == 1) {
if(a > n) {
a -= n;
} else {
a += n;
}
if(b > n) {
b -= n;
} else {
b += n;
}
}
if(t==1) {
// cout << a << ' ' << b << endl;
swap(s[a-1], s[b-1]);
} else {
flips++;
}
// cout << s << endl;
}
if(flips%2 == 1) {
s = s.substr(n, n) + s.substr(0, n);
}
cout << s;
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// int t;
// cin >> t;
// for(int i = 1; i <= t; i++) {
// // cout << "Case #" << i << ": ";
// solve();
// }
solve();
cout.flush();
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define FOE(i, s, t) for (int i = s; i <= t; i++)
#define FOR(i, s, t) for (int i = s; i < t; i++)
#define FOD(i, s, t) for (int i = s; i >= t; i--)
#define LL long long
#define mp make_pair
#define pb push_back
#define K 600001
int a[K], b[K], n, l;
int c[K], d[K];
vector<int> v;
int main() {
scanf("%d%d", &n, &l);
FOE(i, 1, n) scanf("%d", &a[i]);
FOE(i, 1, n) scanf("%d", &b[i]);
FOE(i, 1, n) {
c[i] = a[i] - (i > 1 ? a[i - 1] : 0) - 1;
d[i] = b[i] - (i > 1 ? b[i - 1] : 0) - 1;
}
c[n + 1] = l - a[n];
d[n + 1] = l - b[n];
int ptr = 1;
LL tot = 0;
LL ret = 0;
while (ptr <= n + 1 && d[ptr] == 0) ptr++;
FOE(i, 1, n + 1) {
tot += c[i];
if (c[i]) v.pb(i);
if (tot > d[ptr]) {
puts("-1");
return 0;
}
if (ptr <= n + 1 && tot == d[ptr]) {
int l = v[0];
int r = v[v.size() - 1];
if (l < ptr) ret += (ptr - l);
if (ptr < r) ret += (r - ptr);
ptr++;
while (ptr <= n + 1 && d[ptr] == 0) ptr++;
tot = 0;
v.clear();
}
}
if (v.size() >= 1) {
puts("-1");
} else {
printf("%lld\n", ret);
}
} | #include<deque>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<tuple>
#include<string>
#include<chrono>
#include<functional>
#include<iterator>
#include<random>
#include<unordered_set>
#include<array>
#include<map>
#include<iomanip>
#include<assert.h>
#include<list>
#include<bitset>
#include<stack>
#include<memory>
#include<numeric>
#include <utility>
using namespace std;
typedef long long int llint;
typedef long double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
#define REP(i, n) for(int i = 0;i < (n);i++)
/*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/
const llint mod=1000000007;
const llint inf=2.19e15+1;
const long double pai=3.141592653589793238462643383279502884197;
const long double eps=1e-10;
template <class T,class U>bool chmin(T& a,U b){if(a>b){a=b;return true;}return false;}
template <class T,class U>bool chmax(T& a,U b){if(a<b){a=b;return true;}return false;}
llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);}
llint lcm(llint a,llint b){if(a==0){return b;}return a/gcd(a,b)*b;}
template<class T> void SO(T& ve){sort(ve.begin(),ve.end());}
template<class T> void REV(T& ve){reverse(ve.begin(),ve.end());}
template<class T>llint LBI(const vector<T>&ar,T in){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();}
template<class T>llint UBI(const vector<T>&ar,T in){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();}
int main(void){
int i,j,n,L;cin>>n>>L;
llint ans=0;
vector<int>A(n+2);
vector<int>B(n+2);
for(i=1;i<=n;i++){cin>>A[i];A[i]-=i;}
for(i=1;i<=n;i++){cin>>B[i];B[i]-=i;}
A[n+1]=L-n;B[n+1]=L-n;
for(i=1;i<=n;i++){
if(A[i]<B[i]){
int bas=LBI(A,B[i]);
if(A[bas]!=B[i]){cout<<-1<<endl;return 0;}
if(B[i]==B[i+1]){ans++;}
else{ans+=bas-i;}
}
if(A[i]>B[i]){
int bas=UBI(A,B[i])-1;
if(A[bas]!=B[i]){cout<<-1<<endl;return 0;}
if(B[i]==B[i-1]){ans++;}
else{ans+=i-bas;}
}
//cerr<<"ok";
}
cout<<ans<<endl;
return 0;
} |
#define ll long long
#define dd long double
#define pb push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
#define fo(i ,a, n) for(ll i =a ; i < n ; i++)
//#define all(x) x.begin(), x.end()
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)//traverse through a dataset
//#define sortx(x) sort(all(x))
#define maxpq priority_queue<ll>
#define minpq priority_queue<ll,vector<ll>,greater<ll>>
#include<bits/stdc++.h>
#include<iomanip>
#include<cmath>
#include<cstdio>
#include<utility>
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<bitset>
ll fact[200001] ;
ll inf=1e18;
ll z=1e9+7;
ll gdp(ll a , ll b){return (a - (a%b)) ;}
ll ld(ll a , ll b){if(a < 0) return -1*gdp(abs(a) , b) ; if(a%b == 0) return a ; return (a + (b - a%b)) ;} // least number >=a divisible by b
ll gd(ll a , ll b){if(a < 0) return(-1 * ld(abs(a) , b)) ; return (a - (a%b)) ;} // greatest number <= a divisible by b
ll gcd(ll a , ll b){ if(b > a) return gcd(b , a) ; if(b == 0) return a ; return gcd(b , a%b) ;}
ll e_gcd(ll a , ll b , ll &x , ll &y){ if(b > a) return e_gcd(b , a , y , x) ; if(b == 0){x = 1 ; y = 0 ; return a ;}
ll x1 , y1 ; e_gcd(b , a%b , x1 , y1) ; x = y1 ; y = (x1 - ((a/b) * y1)) ; return e_gcd(b , a%b , x1 , y1) ;}
ll power(ll a ,ll b , ll p){if(b == 0) return 1 ; ll c = power(a , b/2 , p) ; if(b%2 == 0) return ((c*c)%p) ; else return ((((c*c)%p)*a)%p) ;}
ll inverse(ll a ,ll n){return power(a , n-2 , n) ;}
ll max(ll a , ll b){if(a > b) return a ; return b ;}
ll min(ll a , ll b){if(a < b) return a ; return b ;}
ll left(ll i){return ((2*i)+1) ;}
ll right(ll i){return ((2*i) + 2) ;}
ll ncr(ll n , ll r){if(n < r|| (n < 0) || (r < 0)) return 0 ; return ((((fact[n] * inverse(fact[r] , z)) % z) * inverse(fact[n-r] , z)) % z);}
void swap(ll&a , ll&b){ll c = a ; a = b ; b = c ; return ;}
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define ss(s) scanf("%s",s)
#define pi(x) printf("%d\n",x)
#define pl(x) printf("%lld\n",x)
#define ps(s) printf("%s\n",s)
#define ub(v,x) upper_bound(v.begin(),v.end(),x);
#define lb(v,x) lower_bound(v.begin(),v.end(),x);
dd pi = acos(-1) ;
ll pow(ll a ,ll b ){if(b == 0) return 1 ; ll c = pow(a , b/2) ; if(b%2 == 0) return (c*c) ; else return ((c*c)*a);}
using namespace std ;
const int N = 2e5+7;
const int M=1e6+7;
//vector<ll>adj[N];
//int mat[N][N];
//vector<bool>used(N,false);
//vector<ll>d(N,-1);
//vector<ll>p(N,-1);
bool cmp(ll a,ll b){
return a>b;
}
struct S
{
int m_n1;
int m_n2;
int m_n3;
S(int n1, int n2, int n3) : m_n1(n1), m_n2(n2), m_n3(n3)
{
}
bool operator<(const struct S& other) const
{
if(m_n1!=other.m_n1)
return m_n1 < other.m_n1;
else
return m_n2 > other.m_n2;
}
};
void solve(){
ll n;
cin>>n;
ll votes[n][2];
ll s[2]={0};
vector<pair<ll,pair<ll,ll>>>v;
fo(i,0,n){
ll x,y;
cin>>x>>y;
votes[i][0]=x;
votes[i][1]=y;
s[1]+=x;
v.pb({y+2*x,{x,y}});
}
ll cnt=0;
sort(v.begin(),v.end());
reverse(v.begin(),v.end());
fo(i,0,n){
if(s[0]>s[1]){
break;
}
else{
s[0]+=(v[i].S.F+v[i].S.S);
s[1]-=v[i].S.F;
cnt++;
}
}
cout<<cnt<<endl;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
//cin>>t;
t=1;
while(t--){
solve();
}
} | #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
using ll = long long;
const int INF = 1001001001;
const ll INF_LL = 1001001001001001001LL;
int main(void){
int n; cin >> n;
vector<ll> a(n),b(n); rep(i,n) cin >> a[i] >> b[i];
ll sum = 0;
vector<pair<ll,int>> s(n);
rep(i,n){
ll k = 0;
k = a[i]+b[i];
sum += k;
k += a[i];
s[i] = make_pair(k,i);
}
sort(s.begin(),s.end());
ll t = 0;
int m = 0;
rep(i,n){
ll aa;
ll ss = s[i].first-a[s[i].second];
aa = ss-b[s[i].second];
if(sum-ss <= t+aa) break;
sum -= ss;
t += aa;
m++;
}
int ans = n-m;
cout << ans << endl;
return 0;
}
|
#include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define pb(a) push_back(a)
#define vll vector<ll>
#define loop(i, n) for(ll i=1;i<=n;i++)
#define loop0(i, n) for(ll i=0;i<n;i++)
#define in(i) scanf("%lld", &i);
#define out(i) printf("%lld", i)
#define pll pair<ll, ll>
#define vpll vector<pair<ll, ll>>
#define mp(i, j) make_pair(i, j);
const ll mod = 1000000007;
const ll big = 2999999999999999999;
const ll small = -2999999999999999999;
void in2(ll& a, ll& b) { in(a); in(b); }
void in3(ll& a, ll& b, ll& c) { in(a); in(b); in(c); }
void swap(ll& a, ll& b) { a = a+b; b = a-b; a = a-b; }
void arrayIn(vll& a, ll& n) { loop0(i, n) in(a[i]); }
ll n, m, a, b, c, u, v, p, q, k;
int main()
{
ll t;
t = 1;
while(t--)
{
in3(a, b, c);
cout<<21-a-b-c<<"\n";
}
}
| #include<bits/stdc++.h>
using namespace std;
# define ll long long
# define read read1<ll>()
# define Type template<typename T>
Type T read1(){
T t=0;
char k;
bool vis=0;
do (k=getchar())=='-'&&(vis=1);while('0'>k||k>'9');
while('0'<=k&&k<='9')t=(t<<3)+(t<<1)+(k^'0'),k=getchar();
return vis?-t:t;
}
# define fre(k) freopen(k".in","r",stdin);freopen(k".out","w",stdout)
int main(){
int x=-1;
for(int T=read;T--;){
int a=read,b=read,c=read;
if(c>a&&(b<x||!~x))x=b;
}cout<<x;
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<n; i++)
using namespace std;
typedef long long ll;
int main(void){
int n,x;
string s;
cin>>n>>x>>s;
rep(i,s.size()){
if(s[i]=='o'){
x++;
}else if(x>0){
x--;
}else{
x=0;
}
}
cout<<x<<endl;
return 0;
} | ///−In the name of God −///
//In mathematics, an unordered pair or pair set is a set of the form {a, b},
//i.e. a set having two elements a and b with no particular relation between them.
//In contrast, an ordered pair (a, b) has a as its first element and b as its second element.
//So, (a,b)!=(b,a) in ordered pair but it is in case of unordered pair.
#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 dl double
#define llu unsigned long long int
#define scll(x) scanf("%lld",&x)
#define scld(x) scanf("%lf",&x)
#define scull(x) scanf("%llu",&x)
#define scch(x) scanf(" %c",&x)
#define eb(x) emplace_back(x)
#define pb(x) push_back(x)
#define pf(x) push_front(x)
#define ppb() pop_back()
#define ppf() pop_front()
#define Pi acos(-1.0)
#define inf 1000000000000000000
#define mod 1000000007
#define ff first
#define ss second
#define mpr make_pair
#define debug(x) cout<<x<<'\n';
#define MAX 1000000
#define eps 1e-8
#define nl cout<<'\n'
#define ump unordered_map
#define mp map
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> indexed_multiset;
//Taking input by EOF
//while(scanf("%lld",&x)!=-1){ }
// Creates a min heap
//priority_queue <int, vector<int>, greater<int> > pq;
//sin((x*pi)/180.0)[x in degree]
//sin(x)[x in radian]
//asin(x)[radian]
//(asin(x)*180)/pi[degree]
//or(|)//and(&)//xor(^)//not(~)
//S.order_of_key(x)[Give me the position of x]
//S.find_by_order(x)[Give me the x th element in set]
//pair can no te used as index in Unordered_map
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//Knight moves
ll dx[8]={-2,-2,2,2,-1,-1,1,1};
ll dy[8]={1,-1,1,-1,2,-2,2,-2};
ll lcm(ll a, ll b) {return (a * b) / __gcd(a, b);}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll a, b, c = 0, d, e, f = 0, g, h, i, j = 0, k, m, n, o, p, s, t, v, w, x = 0, y, z, l, res = 0;
ll cnt = 0, l1 = 0, l2, l3, c1 = 0, c2 = 0, c3 = 0, mx = 0, mn = inf;
ll prevsum = 0, ans1 = 0, ans2 = 0, odd = 0, even = 0, hh, mm;
string str, str1, str2, str3 = "", str4 = "";
ll ide = 0, idx = 0, Q;
ll xa, xy;
scll(a);
scll(b);
cin>>str;
for(i=0;i<str.length();i++)
{
if(str[i]=='x'&&b>0)
b--;
else if(str[i]=='o')
b++;
}
cout<<b;
return 0;
} |
#include <bits/stdc++.h>
#define rep(i,n) for (ll i = 0; i < (ll)(n); i++)
#define repr(i,a,b) for (ll i = (a); i < (ll)(b); i++)
#define rng(x) (x).begin(), (x).end()
#define rrng(x) (x).rbegin(), (x).rend()
#define popcnt(x) __builtin_popcountll(x)
#define pb push_back
#define eb emplace_back
#define fr first
#define sc second
using namespace std;
template <class T, class U> inline bool chmax(T &a, const U &b) { return (a < b ? a = b, 1 : 0); }
template <class T, class U> inline bool chmin(T &a, const U &b) { return (a > b ? a = b, 1 : 0); }
template <class T> using vc = vector<T>;
template <class T> using vv = vc<vc<T>>;
template <class T> using vvv = vc<vv<T>>;
template <class T> using PQ = priority_queue<T, vc<T>, greater<T>>;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using ti3 = tuple<int, int, int>;
using tl3 = tuple<ll, ll, ll>;
using vi = vc<int>;
using vl = vc<ll>;
using vpl = vc<pll>;
using vvi = vv<int>;
using vvl = vv<ll>;
const ld pi = 3.141592653589793;
const ll linf = 1001001001001001001LL;
const int inf = 1001001001;
int main() {
ll n;
cin >> n;
ll res = (ld)(n)*1.08;
if (res == 206) cout << "so-so" << endl;
else if (res < 206) cout << "Yay!" << endl;
else cout << ":(" << endl;
} | #include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ull unsigned long long int
#define ld long double
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define intarr(arr, n) ll arr[n]; for (ll i = 0; i < n; i++) cin >> arr[i]
#define inpvec(v,n) for(ll i=0;i<n;i++){ll var;cin>>var;v.push_back(var);}
#define ff first
#define ss second
#define pb push_back
#define mkp make_pair
#define vec vector<ll>
#define pi pair<ll,ll>
#define changeline cout<<endl
#define loop(i,a,b) for(ll i=a;i<b;i++)
#define rev(i,a,b) for(ll i=a;i>=b;i--)
#define all(v) v.begin(),v.end()
#define NO cout << "NO" <<endl
#define YES cout << "YES" <<endl
#define mem(arr,val) memset(arr,val,sizeof(arr));
const ll mod = 1e9+7;
const ll inf = LLONG_MAX;
#define mod2 998244353
#define piii 3.14159265358979323846264338327950
#define print(a, n, m) for (ll i = n; i < m; i++) { cout << a[i] << " "; } cout << endl
#define con continue
bool prime(ll n)
{
if (n <= 1) return false;
if (n <= 3) return 1;
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 1;
}
bool isPerfectSquare(ll x1) {
long double sr = sqrt(x1);
return ((sr - floor(sr)) == 0);
}
bool isPowerOfTwo(ll n) {
return n > 0 && !(n&(n-1));
}
bool subsequence_checker(string str1, string str2, ll m, ll n)
{
if (m == 0) return 1;
if (n == 0) return false;
if (str1[m-1] == str2[n-1])
return subsequence_checker(str1, str2, m-1, n-1);
return subsequence_checker(str1, str2, m, n-1);
}
bool cmp(const pair<ll,ll> &a, const pair<ll,ll> &b)
{
if(a.ff == b.ff)
return a.ss < b.ss;
return (a.ff > b.ff);
}
ll gcd(ll p, ll q){
if(p % q == 0)return q;
return gcd(q, p % q);
}
ll countDigit(ll n)
{
return floor(log10(n) + 1);
}
ll power(ll a,ll n)
{
if(n==0)
return 1;
if(n%2)
return (a*power((a*a)%mod,n/2))%mod;
return power((a*a)%mod,n/2);
}
ll fast_pow(ll a, ll p) {
ll res = 1;
while (p) {
if (p % 2 == 0) {
a = (a * a) % mod;
p /= 2;
} else {
res = (res * a) % mod;
p--;
}
}
return res;
}
ll setBitNumber(ll n)
{
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
n = n + 1;
return (n >> 1);
}
ll ncr(ll n , ll row)
{
if(n<row) return 0;
if(row==1) return n;
if(row==2) return (n*(n-1))/2;
return (n*(n-1)*(n-2))/6;
}
ll countDivisors(ll n) {
ll count =0 ;
for (ll i = 1; i*i < n; i++)
if (n % i == 0)
count++;
for (ll i = sqrt(n); i >= 1; i--)
if (n % i == 0)
count++;
return count;
}
vector<ll> printDivisors(ll n){
vector<ll> d ;
for (ll i = 1; i*i < n; i++)
if (n % i == 0){
d.push_back(i);
//cout<<i<<" ";
}
for (ll i = sqrt(n); i >= 1; i--)
if (n % i == 0){
d.push_back(n/i);
//cout<<n/i<<" ";
}
return d;
}
///////////////////////////////////Lets Do It///////////////////////////////////
void solve()
{
int n;
cin>>n;
int d = n*108;
d /= 100;
if(d<206)
{
cout<<"Yay!";
}
else if(d==206)
{
cout<<"so-so";
}
else{
cout<<":(";
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "row", stdin);
freopen("output.txt", "w", stdout);
#endif
ll t;
// cin>>t;
t=1;
while(t--){
solve();
cout<<endl;
}
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
} |
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
#include<cassert>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 998244353;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
template<class T>bool chmax(T &a, const T &b) {if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a, const T &b) {if(b<a){a=b;return 1;}return 0;}
template<int mod>
struct ModInt {
long long x;
static constexpr int MOD = mod;
ModInt() : x(0) {}
ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
explicit operator int() const {return x;}
ModInt &operator+=(const ModInt &p) {
if((x += p.x) >= mod) x -= mod;
return *this;
}
ModInt &operator-=(const ModInt &p) {
if((x += mod - p.x) >= mod) x -= mod;
return *this;
}
ModInt &operator*=(const ModInt &p) {
x = (int)(1LL * x * p.x % mod);
return *this;
}
ModInt &operator/=(const ModInt &p) {
*this *= p.inverse();
return *this;
}
ModInt operator-() const { return ModInt(-x); }
ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
ModInt operator%(const ModInt &p) const { return ModInt(0); }
bool operator==(const ModInt &p) const { return x == p.x; }
bool operator!=(const ModInt &p) const { return x != p.x; }
ModInt inverse() const{
int a = x, b = mod, u = 1, v = 0, t;
while(b > 0) {
t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
return ModInt(u);
}
ModInt power(long long n) const {
ModInt ret(1), mul(x);
while(n > 0) {
if(n & 1)
ret *= mul;
mul *= mul;
n >>= 1;
}
return ret;
}
ModInt power(const ModInt p) const{
return ((ModInt)x).power(p.x);
}
friend ostream &operator<<(ostream &os, const ModInt<mod> &p) {
return os << p.x;
}
friend istream &operator>>(istream &is, ModInt<mod> &a) {
long long x;
is >> x;
a = ModInt<mod>(x);
return (is);
}
};
using modint = ModInt<mod>;
int n,k;
modint dp[3010][3010];
bool visited[3010][3010];
modint get_dp(int i,int j){
//cout << i << " " << j << endl;
if(i<j) return 0;
if(min(i,j)<0) return 0;
if(visited[i][j]) return dp[i][j];
visited[i][j]=true;
dp[i][j]=get_dp(i-1,j-1)+get_dp(i,2*j);
return dp[i][j];
}
void solve(){
cin >> n >> k;
visited[0][0]=true;
dp[0][0]=1;
cout << get_dp(n,k) << endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(50);
solve();
} | #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <iomanip>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#define lli long long int
#define br cout<<"\n";
#define plli pair<lli,lli>
#define vlli vector<lli>
#define vplli vector<plli>
#define pqlli priority_queue <lli>
#define pqplli priority_queue <plli>
#define mplli map<lli,lli>
#define pb push_back
#define mp make_pair
#define fr(i,n) for(i=0;i<n;i++)
#define fr1(i,n) for(i=1;i<=n;i++)
#define arr(a,n) lli a[n]; fr(i,n){ cin>>a[i]; }
#define arr1(a,n) lli a[n+5]; fr1(i,n){ cin>>a[i]; }
#define print(a,n) fr(i,n) {cout<<a[i]<<" ";} br
#define print1(a,n) fr1(i,n) {cout<<a[i]<<" ";} br
#define printv(vec) for(lli qq=0;qq<vec.size();qq++) {cout<<vec[qq]<<" ";}
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
#define MAXN 100001
#define mset(a) memset(a,0,sizeof a);
#define init(a) memset(a,-1,sizeof a);
#define MAXNN 200005
#define mod 1000000007
using namespace std;
const lli inf = 1e10;
int main()
{
ios_base::sync_with_stdio(false);
lli i,j,n,k,m,t,d,l,w,r,x,y;
cin>>x>>y>>l>>r;
n=(x*r)-(l*y);
cout<<n<<'\n';
return 0;
} |
#include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl "\n"
#define rep(i,n) repi(i,0,n)
#define repi(i,a,n) for(ll i=a;i<(ll)n;++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define Show(val) cout<<(val)<<" "
#define Showln(val) cout<<(val)<<endl
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
// using P = pair<int, int>;
void YN(bool a) { cout << (a ? "Yes" : "No"); }
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; }
// BIT
// Binary Indexed Tree
// https://scrapbox.io/pocala-kyopro/%E8%BB%A2%E5%80%92%E6%95%B0
// 1-index
struct BIT {
private:
vector<int> bit;
int N;
public:
BIT(int size) {
N = size;
bit.resize(N + 1);
}
// 一点更新
void add(int a, int w) {
for (int x = a; x <= N; x += x & -x) bit[x] += w;
}
// 1~Nまでの和を求める。
int sum(int a) {
int ret = 0;
for (int x = a; x > 0; x -= x & -x) ret += bit[x];
return ret;
}
};
void solve()
{
ll n;
cin >> n;
rep(i, n)
{
cout << i * 2 % n + 1 << " " << (i * 2 + 1) % n + 1 << endl;
}
}
int main()
{
fastio;
solve();
return 0;
} | #include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <ctype.h>
#include <math.h>
#include <stack>
#include <string>
#include <string.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int,int>;
const long double PI = acos(-1.0);
const ll mod = 1e9 + 7;
const ll INF = 1e9;
ll gcd(ll a, ll b) {if(a%b==0)return a;else return gcd(b,a%b);}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}
int main() {
ll n;
cin >> n;
ll a = 1;
for(ll i = 1; i < 100; i++) {
a *= 3;
if(a > 1e18) break;
ll b = 1;
for(ll j = 1; j < 100; j++) {
if(b*5 > 1e18) break;
b *= 5;
if(b > n-a || b > 1e18) break;
if(b == n-a) {
cout << i << " " << j << endl;
return 0;
}
}
if(a*3 > 1e18) break;
}
cout << -1 << endl;
}
|
/*/ Author : Abhishek Chauhan /*/
#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;
// a+b = a^b + 2*(a&b)
// According to Fermat's little theorem, (a^b)%mod = ((a^(b%(mod - 1)))%mod) if mod is a prime number and not the other way round.
// whenever using a comparator return true only if a<b
// make it forward first if it doesnt work do the reverse
// vector a = ax + ay b = bx + by then dot porduct is |a||b|cos@ //and also for eas you can use (ax*bx)+(ay*by);
#define rep(i,n) for(int i=0;i<n;i++)
#define int long long
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define mod 1000000007
#define bpop(x) __builtin_popcountll((x))
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define endl '\n'
template<class T>
using ordered_set = tree<T, null_type,less<T>, rb_tree_tag,tree_order_statistics_node_update> ;
// find_by_order(k) returns iterator to kth element starting from 0;
// order_of_key(k) returns count of elements strictly smaller than k;
template<class T>
T power(T x, T y){T res = 1;while (y > 0){ if (y & 1){res = res*x;} y = y>>1;x = x*x;}return res;}
template<class T>
T powermod(T x, T y, T p){T res = 1;x = x % p;while (y > 0){if (y & 1){res = (res*x) % p;}y = y>>1; x = (x*x) % p;}return res;}
const int N = 20;
vector<int>g[N];
int vis[N],col[N];
vector<int>order;
void get(int node){
vis[node] = 1;
order.pb(node);
for(auto x:g[node]){
if(vis[x]==0){
get(x);
}
}
}
int recur(int i){
if(i>=order.size()){
return 1;
}
int res = 0;
vis[order[i]] = 1;
vector<int>can(4,1);
for(auto x:g[order[i]]){
if(col[x]!=0){
can[col[x]] = 0;
}
}
for(int c=1;c<4;c++){
if(can[c]){
col[order[i]] = c;
res+=recur(i+1);
col[order[i]] = 0;
}
}
return res;
}
void snow(){
int n,m;
cin>>n>>m;
for(int i=0;i<n;i++){
vis[i] = 0;
col[i] = 0;
}
for(int i=0;i<m;i++){
int a,b;
cin>>a>>b;a--;b--;
g[a].pb(b);
g[b].pb(a);
}
int ans = 1;
for(int i=0;i<n;i++){
if(!vis[i]){
order.clear();
get(i);
col[order[0]] = 1;
vis[order[0]] = 1;
if(order.size()==1){
ans = ans*3;
}
else{
ans=ans*3*recur(1);
}
}
}
cout<<ans<<endl;
}
int32_t main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
fastio;
int _;
_=1;
for(int i=1;i<=_;i++){
// cout<<"Case #"<<i<<": ";
snow();
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define yes(x) ((x) ? "yes" : "no")
#define Yes(x) ((x) ? "Yes" : "No")
using i64 = int_fast64_t;
using u64 = uint_fast64_t;
using f64 = double_t;
using p64 = pair<i64, i64>;
constexpr i64 INF = INT_FAST64_MAX / 2;
void solve() {
i64 N, M;
cin >> N >> M;
vector<p64> conds(M);
for (auto& [ A, B ] : conds) cin >> A >> B;
i64 K;
cin >> K;
vector<p64> poss(K);
for (auto& [ C, D ] : poss) cin >> C >> D;
bitset<128> bs;
i64 ans = 0;
for (i64 i = 0; i < (1 << K); i++) {
i64 can = 0;
for (i64 j = 0; j < K; j++) {
auto& [ C, D ] = poss[j];
bs[(i >> j) & 1 ? C : D] = 1;
}
for (i64 j = 0; j < M; j++) {
auto& [ A, B ] = conds[j];
can += bs[A] && bs[B];
}
ans = max(ans, can);
bs.reset();
}
cout << ans << endl;
}
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(15);
solve();
}
|
// Problem:
// D - Orientation
//
//
// Contest: AtCoder - AtCoder Regular Contest 111
// URL: https://atcoder.jp/contests/arc111/tasks/arc111_d
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define forn(i,x,n) for(int i = x;i <= n;++i)
const int N = 105,M = N * N;
int g[N][N],a[M],b[M],c[N];
bool st[N];
int n,m;
void dfs(int u)
{
st[u] = 1;
forn(i,1,n)
{
if(!g[u][i]) continue;
g[i][u] = 0;
if(!st[i]) dfs(i);
}
}
int main()
{
ios::sync_with_stdio(0);cin.tie(0);
cin >> n >> m;
forn(i,1,m) cin >> a[i] >> b[i];
forn(i,1,n) cin >> c[i];
vector<string> ans(m + 1,"");
forn(i,1,m)
{
if(c[a[i]] > c[b[i]]) ans[i] = "->";
else if(c[a[i]] < c[b[i]]) ans[i] = "<-";
else
{
g[a[i]][b[i]] = 1;
g[b[i]][a[i]] = 1;
}
}
forn(i,1,n) if(!st[i]) dfs(i);
forn(i,1,m)
{
if(ans[i] != "") continue;
if(g[a[i]][b[i]]) ans[i] = "->";
else ans[i] = "<-";
}
forn(i,1,m) cout << ans[i] << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "../debug.h"
#else
#define debug(x...) 141
#endif
#define forn(i,x,n) for(int i = x; i < n; ++i)
#define forit(it, m) for(auto it = m.begin(); it!=m.end(); ++it)
typedef long long ll;
ll n,m,k,x,y,p=1e9+7,invm;
void mult(vector<vector<ll>>& a, vector<vector<ll>>& b){
vector<vector<ll>> c(n,vector<ll>(n));
forn(i,0,n)
forn(j,0,n)
forn(k,0,n)
c[i][j]+=a[i][k]*b[k][j], c[i][j]%=p;
swap(a,c);
}
vector<vector<ll>> power(vector<vector<ll>>& F, int N){
vector<vector<ll>> res(n,vector<ll>(n));
forn(i,0,n)
res[i][i]=1;
while(N){
if(N%2)
mult(res,F);
mult(F,F);
N/=2;
}
return res;
}
int inv(ll m){
ll res=1, n=p-2;
while(n){
if(n%2)
res*=m, res%=p;
m*=m, m%=p;
n/=2;
}
return res;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin>>n>>m>>k;
invm = inv(2ll*m);
vector<vector<ll>> adj(n,vector<ll>(n)), a(n,vector<ll>(1));
vector<int> deg(n);
forn(i,0,n)
cin>>a[i][0];
forn(_,0,m){
cin>>x>>y, x--, y--;
adj[x][y]=adj[y][x]=invm, deg[x]++, deg[y]++;
}
forn(i,0,n)
adj[i][i]=(p*p+1-deg[i]*invm)%p;
adj = power(adj,k);
mult(adj,a);
forn(i,0,n)
cout<<adj[i][0]<<'\n';
return 0;
} |
// Problem: F - Rook on Grid
// Contest: AtCoder - Panasonic Programming Contest (AtCoder Beginner Contest 186)
// URL: https://atcoder.jp/contests/abc186/tasks/abc186_f
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define int long long
#define F(i, n) for (int i = 0; i < n; i++)
#define all(x)(x).begin(), (x).end()
using ld = long double;
using vi = vector <int>;
using mi = map <int, int>;
using pii = pair <int, int>;
const int N = 200005;
const int MOD = 1e9 + 7;
#define show(arr) { for (auto x: arr) cout << x << " "; cout << '\n'; }
#define show_(arr, n) F(i, n) show(arr[i])
/*
~Segment tree~
use v=1, tl=0, tr=n-1
*/
int t[4*N];
int operation(int a,int b) {return a+b;}
void build(vi &a, int v, int tl, int tr) {
if (tl == tr) {
t[v] = a[tl];
} else {
int tm = (tl + tr) / 2;
build(a, v*2, tl, tm);
build(a, v*2+1, tm+1, tr);
t[v] = operation(t[v*2],t[v*2+1]);
}
}
int sum(int v, int tl, int tr, int l, int r) {
if (l > r)
return 0;
if (l == tl && r == tr) {
return t[v];
}
int tm = (tl + tr) / 2;
return operation(sum(v*2, tl, tm, l, min(r, tm))
,sum(v*2+1, tm+1, tr, max(l, tm+1), r));
}
void update(int v, int tl, int tr, int pos, int new_val) {
if (tl == tr) {
t[v] = new_val;
} else {
int tm = (tl + tr) / 2;
if (pos <= tm)
update(v*2, tl, tm, pos, new_val);
else
update(v*2+1, tm+1, tr, pos, new_val);
t[v] = operation(t[v*2],t[v*2+1]);
}
}
vi rr[N], cc[N];
int r[N], c[N];
void solve() {
int rows,colmn,k, ans = 0; cin>>rows>>colmn>>k;
vi a(rows,0);
F(i,N){
r[i] = colmn;
c[i] = rows;
}
F(i, k){
int x,y; cin >> x >> y; x--; y--;
r[x] = min(r[x],y);
c[y] = min(c[y],x);
rr[x].pb(y);
cc[y].pb(x);
}
bool ok=1;
F(i,rows){
if(r[i]==0) ok=0;
if(ok)
ans += r[i];
else
a[i]=1;
}
build(a, 1, 0, rows-1);
F(i,colmn){
int bound = c[i];
if(bound == 0) break;
ans += sum(1, 0, rows-1,0,bound-1);
for(int x: cc[i]) update(1, 0, rows-1,x,1);
}
cout << ans << '\n';
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int32_t t=1;
// cin >> t;
while (t--) solve();
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define ll long long
void update(int bit[],int n,int x, int val)
{
for(; x <= n; x += x&-x)
bit[x] += val;
}
int sum1(int bit[],int n)
{
int sum = 0; int x=n;
for(; x > 0; x -= x&-x)
sum += bit[x];
return sum;
}
int main()
{
int h,w,m; cin>>h>>w>>m;
map<int,int>mp,mp1;
map<pair<int,int>,int>mp3;
map<int,vector<int>>v;
for(int i=0;i<m;i++)
{
int x,y;
cin>>x>>y;
if(mp[x]==0) mp[x]=y;
else
mp[x]=min(mp[x],y);
if(mp1[y]==0) mp1[y]=x;
else
mp1[y]=min(mp1[y],x);
mp3[{x,y}]=1;
v[y].push_back(x);
}
ll cnt=0;
for(int i=1;i<=w;i++)
{
if(mp3[{1,i}]==0){
if(mp1[i]==0) cnt+=h;
else cnt+=mp1[i]-1;
//cout<<cnt<<endl;
}
else break;
}
//cout<<cnt<<endl;
int bit[h+1]={0}; bool has[h+1]={0}; bool flag=0;
if(mp1[1]!=0)
for(int i=mp1[1];i<=h;i++) update(bit,h,i,1),has[i]=1;
for(int i=2;i<=w;i++)
{
for(auto it: v[i])
{
if(has[it]==0){
update(bit,h,it,1);
has[it]=1;
}
}
if(flag==0){
if(mp1[i]!=0)
cnt+=h-(sum1(bit,h)-sum1(bit,mp1[i]-1))-(mp1[i]-1);
}
else
cnt+=h-sum1(bit,h);
if(mp3[{1,i}]>=1) flag=1;
}
cout<<cnt<<endl;
}
|
#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for (int i=0;i<(int)(n);i++)
#define codefor int test;scanf("%d",&test);while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define yes(ans) if(ans)printf("yes\n");else printf("no\n")
#define Yes(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YES(ans) if(ans)printf("YES\n");else printf("NO\n")
#define vector1d(type,name,...) vector<type>name(__VA_ARGS__)
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define umap unordered_map
#define uset unordered_set
using namespace std;
using ll = long long;
const int MOD=1000000007;
const int MOD2=998244353;
const int INF=1<<30;
const ll INF2=(ll)1<<60;
//入力系
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T,class L>void scan(pair<T, L>& p){scan(p.first);scan(p.second);}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
//出力系
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T,class L>void print(const pair<T, L>& p){print(p.first);putchar(' ');print(p.second);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
//デバッグ系
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<endl;}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
ll updivide(ll a,ll b){if(a%b==0) return a/b;else return (a/b)+1;}
template<class T> void chmax(T &a,const T b){if(b>a)a=b;}
template<class T> void chmin(T &a,const T b){if(b<a)a=b;}
int main(){
INT(n,m);
vector<ll> h(n),w(m);
in(h,w);
vector<ll> oddsum(1,0),evensum(1,0),odd(1,0),even(1,0);
sort(all(h));
for(int i=0;i<n;i++){
if(i%2==0){
odd.push_back(h[i]);
oddsum.push_back(oddsum[oddsum.size()-1]+h[i]);
}else{
even.push_back(h[i]);
evensum.push_back(evensum[evensum.size()-1]+h[i]);
}
}
int insert_index,oindex,eindex;
ll fsum,bsum,ans=INF2;
for(int i=0;i<m;i++){
insert_index=(int)(upper_bound(all(h),w[i])-h.begin());
oindex=(int)(upper_bound(all(odd),w[i])-odd.begin())-1;
eindex=(int)(upper_bound(all(even),w[i])-even.begin())-1;
fsum=oddsum[oindex];
bsum=evensum[eindex];
fsum+=evensum[evensum.size()-1]-evensum[eindex];
bsum+=oddsum[oddsum.size()-1]-oddsum[oindex];
if(insert_index%2==1){
bsum+=w[i];
}else{
fsum+=w[i];
}
chmin(ans,bsum-fsum);
}
out(ans);
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef long double ld;
#define all(x) (x).begin(),(x).end()
#define X first
#define Y second
#define sep ' '
#define endl '\n'
#define debug(x) cerr << #x << ": " << x << endl;
const ll MAXN = 2e5 + 100;
const ll INF = 8e18;
const ll MOD = 1e9 + 7; // 998244353;
bool used[MAXN];
string s;
ll fans, k, fact[MAXN], inv[MAXN], cval = 0;
inline ll poww(ll a, ll b, ll m = MOD) {
a %= m;
ll res = 1;
while (b > 0) {
if (b & 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
inline int F(char c) {
if (c <= '9' && c >= '0') return int(c - '0');
return int(c - 'A') + 10;
}
inline ll C(int r, int n) {
if (r > n || r < 0) return 0;
return fact[n] * inv[r] % MOD * inv[n - r] % MOD;
}
inline int Check() {
memset(used, false, sizeof used);
for (char c : s) used[F(c)] = true;
int ans = 0;
for (int i = 0; i < 16; i++) if (used[i]) ans++;
return ans;
}
inline ll solve(ll k) {
memset(used, false, sizeof used);
ll n = s.size(), ans = 0;
for (int d = 1; d < n; d++) ans += 15 * C(k - 1, 15) % MOD * poww(k, d - 1, MOD) % MOD;
ans += (F(s.front()) - 1) * C(k - 1, 15) % MOD * poww(k, n - 1, MOD) % MOD;
ans %= MOD;
used[F(s.front())] = true;
for (int i = 1; i < n; i++) {
char c = s[i];
ll P2 = 0, P = 0;
for (int j = 0; j < 16; j++) {
if (used[j]) {
P++;
if (j < F(c)) P2++;
}
}
ans += P2 * C(k - P, 16 - P) % MOD * poww(k, n - i - 1) % MOD;
ans += (F(c) - P2) * C(k - P - 1, 16 - P - 1) % MOD * poww(k, n - i - 1) % MOD;
ans %= MOD;
used[F(c)] = true;
}
ans += C(k - cval, 16 - cval);
return ans % MOD;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
fact[0] = inv[0] = 1;
for (int i = 1; i <= 100; i++) fact[i] = i * fact[i - 1] % MOD, inv[i] = poww(fact[i], MOD - 2, MOD);
cin >> s >> k;
cval = Check();
for (int i = 1; i <= k; i++) {
ll tans = solve(i) * C(k - i, 16 - i) % MOD;
if ((k - i) & 1) tans = -tans;
fans += tans;
fans += 2 * MOD;
fans %= MOD;
}
cout << fans << endl;
return 0;
}
|
#include <algorithm>
#include<cfenv>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <ios>
#include <iostream>
#include <list>
#include <map>
#include<numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
#define itn int
#define retrun return
using namespace std;
typedef long long llong;
typedef unsigned long long ullong;
vector<llong> AllDivisors(llong N) {
vector<llong> ANS;
for (llong i = 1; i * i <= N; i++) {
if (N % i == 0) {
ANS.push_back(i);
if (N / i != i)ANS.push_back(N / i);
}
}
sort(ANS.begin(), ANS.end());
return ANS;
}
bool IsPrime(llong N) {
for (llong i = 2; i * i <= N; i++) {
if (N % i == 0) return false;
}
return true;
}
//ここまで共通
llong N;
int main() {
cin >> N;
llong zero = 0;
llong ANS = 0;
ANS += max(N - 999, zero);
ANS += max(N - 999999, zero);
ANS += max(N - 999999999, zero);
ANS += max(N - 999999999999, zero);
ANS += max(N - 999999999999999, zero);
cout << ANS << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define N 2114514
const ll mod = 998244353;
const ll inf = 1000000000000000000;
int main() {
ll n;
cin >> n;
ll ans = 0, l = 1, r = 10, num = 0;
for (int i = 0;; i++) {
if (r <= n) ans += num * (r - l);
if (l <= n && n < r) {
ans += num * (n - l + 1);
break;
}
if (i % 3 == 2) num++;
l *= 10;
r *= 10;
}
cout << ans << endl;
return 0;
}
|