code_file1
stringlengths 87
4k
| code_file2
stringlengths 82
4k
|
---|---|
#include<bits/stdc++.h>
// #pragma optimize("-O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,avx,mmx,tune=native")
// #define ll long long
using namespace std;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
typedef long long ll;
#define vvi vector<vector<ll>>
#define vi vector<ll>
#define sz(a) ll((a).size())
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1)
// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
// typedef tree<
// ll,
// null_type,
// less<ll>,
// rb_tree_tag,
// tree_order_statistics_node_update>
// ordered_set;
#define int long long
const ll MOD = 998244353;
const ll BIG_INF = 4e18;
const ll INF = 1e9;
const ll mod = 998244353;
ll fast_exp(ll a, ll b)
{
if(b <= 0)
return 1;
else
{
ll res = 1;
res = fast_exp(a,b/2);
res = res % mod;
res = (res*res)%mod;
if(b%2 == 1)
res = (res*a)%mod;
return res;
}
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//shuffle(v.begin(),v.end(),rng);
ll true_rand(ll n) {
// Returns a random number between 0 and n - 1 inclusive using mt19937.
uniform_int_distribution<ll> uid(0, n - 1);
return uid(rng);
}
// gambler's ruin: A has i, B has N-i
// P[i] = A wins = (1 - (q/p)^i)/(1 - (q/p)^n)
// ll fact[N] = {};
// ll fac_inv[N] = {};
// void precompute_factorial()
// {
// fact[0] = 1;
// fac_inv[0] = 1;
// for(ll i = 1; i < N; i++)
// {
// fact[i] = (fact[i-1] * i) % mod;
// fac_inv[i] = (fast_exp(i,mod-2) * fac_inv[i-1]) % mod;
// }
// }
// ll binom(ll n, ll r)
// {
// if(r < 0 || r > n || n < 0)
// return 0;
// ll ans = fact[n];
// ans = (ans * fac_inv[n-r]) % mod;
// ans = (ans * fac_inv[r]) % mod;
// return ans;
// }
const ll N = 1e5+1000;
typedef double ld;
const long double pi = acos(-1.0);
using cd = complex<double>;
const double PI = acos(-1);
int dp[102][102][5005] = {};
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
//freopen("list_of_clgs_icpc.txt","r",stdin);
int n;
cin >> n;
int w[n + 1] = {};
int sum = 0;
ll fac[200] = {};
fac[0] = 1;
for(int i = 1; i < 200; i++)
{
fac[i] = (i * fac[i-1]) % mod;
}
for(int i = 1; i <= n; i++)
{
cin >> w[i];
sum += w[i];
}
if(sum % 2 == 1)
{
cout << 0 << endl;
return 0;
}
dp[0][0][0] = 1;
int targ = sum / 2;
for(int i = 1; i <= n; i++)
{
for(int j = 0; j < n; j++)
{
for(int s = 0; s <= targ; s++)
{
dp[i][j][s] += dp[i-1][j][s];
if(dp[i][j][s] > mod)
dp[i][j][s] -= mod;
if(s + w[i] <= targ)
{
dp[i][j+1][s + w[i]] += dp[i-1][j][s];
if(dp[i][j+1][s + w[i]] > mod)
dp[i][j + 1][s + w[i]] -= mod;
}
}
}
}
ll ans = 0;
for(int j = 1; j < n; j++)
{
ll ways = dp[n][j][targ];
ways = (ways * fac[j]) % mod;
ways = (ways * fac[n - j]) % mod;
ans = (ans + ways) % mod;
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
using ll = long long;
using ld = long double;
#define mp make_pair
const int p = 1e9 + 7;
int mul(int a, int b) {
return (1LL * a * b) % p;
}
int add(int a, int b) {
int s = (a+b);
if (s>=p) s-=p;
return s;
}
int sub(int a, int b) {
int s = (a+p-b);
if (s>=p) s-=p;
return s;
}
int po(int a, int deg)
{
if (deg==0) return 1;
if (deg%2==1) return mul(a, po(a, deg-1));
int t = po(a, deg/2);
return mul(t, t);
}
int inv(int n)
{
return po(n, p-2);
}
mt19937 rnd(time(0));
const int N = 1000005;
vector<int> facs(N), invfacs(N);
void init()
{
facs[0] = 1;
for (int i = 1; i<N; i++) facs[i] = mul(facs[i-1], i);
invfacs[N-1] = inv(facs[N-1]);
for (int i = N-2; i>=0; i--) invfacs[i] = mul(invfacs[i+1], i+1);
}
int C(int n, int k)
{
if (n<k) return 0;
if (n<0 || k<0) return 0;
return mul(facs[n], mul(invfacs[k], invfacs[n-k]));
}
/*struct DSU
{
vector<int> sz;
vector<int> parent;
void make_set(int v) {
parent[v] = v;
sz[v] = 1;
}
int find_set(int v) {
if (v == parent[v])
return v;
return parent[v] = find_set(parent[v]);
}
void union_sets(int a, int b) {
a = find_set(a);
b = find_set(b);
if (a != b) {
if (sz[a] < sz[b])
swap(a, b);
parent[b] = a;
sz[a] += sz[b];
}
}
DSU (int n)
{
parent.resize(n);
sz.resize(n);
for (int i = 0; i<n; i++) make_set(i);
}
};*/
int f(char c)
{
if (c=='A') return 0; else return 1;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
int n;
char caa, cab, cba, cbb;
cin>>n>>caa>>cab>>cba>>cbb;
int aa, ab, ba, bb;
aa = f(caa); ab = f(cab); ba = f(cba); bb = f(cbb);
vector<int> fin(n+1);
fin[0] = 1;
fin[1] = 1;
for (int i = 2; i<=n; i++) fin[i] = add(fin[i-1], fin[i-2]);
if (ab == 0)
{
if (aa==0) {cout<<1; return 0;}
//now ab = 0, aa = 1
if (ba==1)
{
cout<<po(2, n-3); return 0;
}
cout<<fin[n-2]; return 0;
//now ba = 0
}
else
{
if (bb==1) {cout<<1; return 0;}
//now ab = 1; bb = 0
if (ba==0)
{
cout<<po(2, n-3); return 0;
}
cout<<fin[n-2]; return 0;
}
}
/*
3 2
1 2 3
2 3 3
*/ |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define all(v) v.begin(), v.end()
int main() {
int N;
cin>>N;
vector<int> A(N),B(N);
rep(i,N){
cin>>A[i]>>B[i];
}
int ans=1000000;
rep(i,N){
rep(j,N){
int memo;
if(i==j){
memo=A[i]+B[i];
}
else{
memo=max(A[i],B[j]);
}
ans=min(ans,memo);
}
}
cout<<ans<<endl;
} | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
int n;
cin>>n;
int a[n][2];
int c[n*n];
for(int i=0;i<n;++i){
int max=0;
for(int j=0;j<2;++j){
cin>>a[i][j];
max=max+a[i][j];
}
c[i]=max;
}
int b=n;
for(int i=0;i<n;++i){
for(int j=0;j<n;++j){
if(i!=j){
c[b]=max(a[i][0],a[j][1]);
++b;
}
}
}
sort(c,c+n*n);
cout<<c[0];
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define MOD (ll)(998244353)
#define INF (ll)(1e18)
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);}
int main()
{
owo
int h,w;
cin>>h>>w;
VII grid(h+2,vector<ll>(w+2)),grid2(h+2,vector<ll>(w+2));
int n,m;
cin>>n>>m;
VII row(h+1),col(w+1);
vector<pii>a(n);
for(int i=0;i<n;i++){
cin>>a[i].fi>>a[i].se;
}
for(int i=0;i<m;i++){
int x,y;
cin>>x>>y;
row[x].pb(y);
col[y].pb(x);
}
//cout<<'\n';
for(int i=1;i<=h;i++){row[i].pb(0);row[i].pb(w+1);sort(all(row[i]));}
for(int i=1;i<=w;i++){col[i].pb(0);col[i].pb(h+1);sort(all(col[i]));}
for(int i=0;i<n;i++){
int x = a[i].fi;
int y = a[i].se;
int p = ub(all(row[x]),y)-row[x].begin();
int l = row[x][p-1]+1;
int r = row[x][p];
//cout<<l<<" "<<r<<'\n';
grid[x][l]++;
grid[x][r]--;
p = ub(all(col[y]),x)-col[y].begin();
l = col[y][p-1]+1;
r = col[y][p];
//cout<<l<<" "<<r<<'\n';
grid2[l][y]++;
grid2[r][y]--;
//cout<<'\n';
}
for(int i=1;i<=h;i++){
for(int j=1;j<=w;j++){
grid[i][j]+=grid[i][j-1];
}
}
for(int j=1;j<=w;j++){
for(int i=1;i<=h;i++){
grid2[i][j]+=grid2[i-1][j];
}
}
ll ans = 0;
for(int i=1;i<=h;i++){
for(int j=1;j<=w;j++){
if(grid[i][j]+grid2[i][j])ans++;
//cout<<grid[i][j]+grid2[i][j]<<" ";
}
//cout<<'\n';
}
cout<<ans;
}
| #include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const static int MAX_H = 1500;
const static int MAX_W = 1500;
bool is_wall_or_light[MAX_H][MAX_W];
int main(){
unsigned H, W, N, M;
scanf("%u %u %u %u", &H, &W, &N, &M);
vector<int> light_row[H], light_column[W];
vector<int> wall_row[H], wall_column[W];
unsigned answer = N;
int a, b, c, d;
for (unsigned i = 0; i < N; i++){
scanf("%d %d", &a, &b);
a--, b--;
light_row[a].push_back(b);
light_column[b].push_back(a);
is_wall_or_light[a][b] = true;
}
for (unsigned i = 0; i < M; i++){
scanf("%d %d", &c, &d);
c--, d--;
wall_row[c].push_back(d);
wall_column[d].push_back(c);
is_wall_or_light[c][d] = true;
}
for (unsigned i = 0; i < H; i++){
light_row[i].push_back(-2);
light_row[i].push_back(W+1);
wall_row[i].push_back(-1);
wall_row[i].push_back(W);
sort(light_row[i].begin(), light_row[i].end());
sort(wall_row[i].begin(), wall_row[i].end());
}
for (unsigned i = 0; i < W; i++){
light_column[i].push_back(-2);
light_column[i].push_back(H+1);
wall_column[i].push_back(-1);
wall_column[i].push_back(H);
sort(light_column[i].begin(), light_column[i].end());
sort(wall_column[i].begin(), wall_column[i].end());
}
for (int i = 0; i < H; i++){
for (int j = 0; j < W; j++){
if(is_wall_or_light[i][j]) continue;
if(auto it_light = lower_bound(light_row[i].begin(), light_row[i].end(), j), it_wall = lower_bound(wall_row[i].begin(), wall_row[i].end(), j);
*it_light < *it_wall || *(--it_light) > *(--it_wall)){
answer++;
continue;
}
if(auto it_light = lower_bound(light_column[j].begin(), light_column[j].end(), i), it_wall = lower_bound(wall_column[j].begin(), wall_column[j].end(), i);
*it_light < *it_wall || *(--it_light) > *(--it_wall)){
answer++;
continue;
}
}
}
printf("%u\n", answer);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define repr(i,a,b) for (int i=a; i<b; i++)
#define rep(i,n) for (int i=0; i< n; i++)
#define PI 3.14159265359
const long long INF = 1LL << 60;
long long MOD = 1000000007;
long long gcd(long long a, long long b) { return b ? gcd(b, a%b) : a; }
long long lcm (int a, int b){return (long long) a*b /gcd(a,b);}
int main(){
int N, K;
cin >> N >> K;
vector<int>ab,cd;
for(int i = 2; i<=2*N ; i++){
if(i - K >= 2 && i- K <= 2*N){
ab.push_back(i);
cd.push_back(i-K);
}
}
long long ans = 0;
for(int i = 0; i<ab.size(); i++){
long long x,y;
if(ab[i] <= N+1) x = ab[i]-1;
else x = (N+1)*2 - ab[i] -1;
if(cd[i] <= N+1) y = cd[i]-1;
else y = (N+1)*2 -cd[i] -1;
ans += x * y;
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<string, string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<ll> vl;
typedef vector<vl> vvl;
double EPS=1e-9;
int INF=1000000005;
long long INFF=1000000000000000005ll;
double PI=acos(-1);
int dirx[8]={ -1, 0, 0, 1, -1, -1, 1, 1 };
int diry[8]={ 0, 1, -1, 0, -1, 1, -1, 1 };
const ll MOD = 1000000007;
ll sum() { return 0; }
template<typename T, typename... Args>
T sum(T a, Args... args) { return a + sum(args...); }
#define DEBUG fprintf(stderr, "====TESTING====\n")
#define VALUE(x) cerr << "The value of " << #x << " is " << x << endl
#define OUT(x) cout << x << endl
#define OUTH(x) cout << x << " "
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define READ(x) for(auto &(z):x) cin >> z;
#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 FORSQ(a, b, c) for (int(a)=(b); (a) * (a) <= (c); ++(a))
#define FORC(a, b, c) for (char(a)=(b); (a) <= (c); ++(a))
#define EACH(a, b) for (auto&(a) : (b))
#define REP(i, n) FOR(i, 0, n)
#define REPN(i, n) FORN(i, 1, n)
#define MAX(a, b) a=max(a, b)
#define MIN(a, b) a=min(a, b)
#define SQR(x) ((ll)(x) * (x))
#define RESET(a, b) memset(a, b, sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(), v.end()
#define ALLA(arr, sz) arr, arr + sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr, sz) sort(ALLA(arr, sz))
#define REVERSEA(arr, sz) reverse(ALLA(arr, sz))
#define PERMUTE next_permutation
#define TC(t) while (t--)
#define FAST_INP ios_base::sync_with_stdio(false);cin.tie(NULL)
#define what_is(x) cerr << #x << " is " << x << endl;
void solve() {
}
int main()
{
FAST_INP;
// #ifndef ONLINE_JUDGE
// freopen("input.txt","r", stdin);
// freopen("output.txt","w", stdout);
// #endif
// int tc; cin >> tc;
// TC(tc) solve();
ll n, k;
cin >> n >> k;
k = abs(k);
ll a, b, c, d, ans = 0;
FORN(i, 2, 2 * n - k) {
b = min(n, (ll)i - 1), a = (ll)i - b;
d = min(n, (ll)i + k - 1), c = (ll)i + k - d;
ans += (b - a + 1) * (d - c + 1);
}
OUT(ans);
return 0;
} |
#include <bits/stdc++.h>
//{ START
using namespace std;
#define int int64_t
#define rep(i, a, n) for (int i = (a); i < (n); ++i)
#define reps(i, a, n) for (int i = (n - 1); i > (a - 1); --i)
#define arep(i, x) for (auto &&i : (x))
#define irep(i, x) for (auto i = (x).begin(); i != (x).end(); ++i)
#define rirep(i, x) for (auto i = (x).rbegin(); i != (x).rend(); ++i)
//้้ ใฏgreater<T>()
#define all(x) (x).begin(), (x).end()
#define rv(s) reverse((s).begin(), (s).end())
// gcd lcmใฏใใฎใพใพok
#define gcd(a, b) __gcd(a, b)
#define bits(n) (1LL << (n))
#define pcnt(x) __builtin_popcountll(x)
//้
ๅๅ
็ญ่ฆ็ด ๅ้ค
#define Unique(x) (x).erase(unique((x).begin(), (x).end()), (x).end())
#define Fixed(n) fixed << setprecision(n)
//็ทๅ
#define sowa(n) (((n) * ((n) + 1)) / 2)
#define updiv(a, b) ((a + b - 1) / b)
#define cauto const auto &
using P = pair<int, int>;
using Graph = vector<vector<P>>;
template <class T> //ๆ้
using min_heap = priority_queue<T, vector<T>, greater<T>>;
template <class T> //้้
using max_heap = priority_queue<T>;
template <class A, class B>
using umap = unordered_map<A, B>;
template <class A>
using uset = unordered_set<A>;
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) { //ๅคๆฌกๅ
ๅๆๅ
std::fill((T *)array, (T *)(array + N), val);
}
template <class A, class B>
bool chmax(A &a, const B &b) { //ๆๅคงๅคๆดๆฐ ่ฟใๅคใฏbool
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class A, class B>
bool chmin(A &a, const B &b) { //ๆๅฐๅคๆดๆฐ ่ฟใๅคใฏbool
if (b < a) {
a = b;
return 1;
}
return 0;
}
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, 1, 1, -1, -1};
constexpr int INF = 0x3f3f3f3f;
constexpr int LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr int mod1 = 1e9 + 7;
constexpr int mod2 = 998244353;
//} END
signed main(void) {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
int a,b;
cin>>a>>b;
cout<<Fixed(10)<<double(double(a-b)/double(a))*100<<'\n';
return 0;
} | #include <cstdint>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <cctype>
#include <limits> // INT_MAX ...
using namespace std;
struct fast_ios {
fast_ios() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout << fixed << setprecision(10);
}
} fast_ios_;
int main() {
int N, K;
cin >> N >> K;
int64_t res = N;
while (K-- > 0) {
if (res % 200 == 0) res /= 200;
else if (K > 0) res = res * 5 + 1, K--;
else res = res * 1000 + 200;
}
cout << res << '\n';
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using pp=pair<int,int>;
int main() {
// ios_base::sync_with_stdio(0); cin.tie(0);
double sx, sy, ex, ey;
cin>>sx>>sy>>ex>>ey;
printf("%.9f", sx+sy*((ex-sx)/(ey+sy)));
return 0;
}
| //:::: Alien :::://
// Muhammad Eid //
#include <bits/stdc++.h>
using namespace std;
void Mo35() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
int main() { Mo35();
int a, b, c, d;
cin >> a >> b >> c >> d;
int mx = -2e9;
for (int x = a; x <= b; ++x) {
for (int y = c; y <= d; ++y)
mx = max(mx, x - y);
}
cout << mx;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
int s0,t0,st;
int main(){
int n;cin>>n;
string s,t;cin>>s>>t;
for(int i=0;i<n;i++){
if(s[i]=='0') s0++;
if(t[i]=='0') t0++;
if(s[i]=='0'&&t[i]=='0'&&t0==s0)st++;
}
if(s0!=t0) cout<<-1;
else cout<<s0-st;
return 0;
} | #include <bits/stdc++.h>
#define fo(a,b,c) for (a=b; a<=c; a++)
#define fd(a,b,c) for (a=b; a>=c; a--)
#define ll long long
//#define file
using namespace std;
int n,i,j,k,l;
char S[500001];
char T[500001];
int s1[500001],s2[500001];
int ans,sum;
int main()
{
#ifdef file
freopen("b.in","r",stdin);
#endif
scanf("%d",&n);
scanf("%s",S+1);
scanf("%s",T+1);
fo(i,1,n)
{
s1[i]=s1[i-1]+(S[i]=='0');
s2[i]=s2[i-1]+(T[i]=='0');
}
fo(i,1,n) sum+=S[i]=='0';
ans=sum;
fo(i,1,n) sum-=T[i]=='0';
if (sum) {printf("-1\n");return 0;}
fo(i,1,n)
if (S[i]==T[i] && S[i]=='0' && s1[i]==s2[i])
--ans;
printf("%d\n",ans);
fclose(stdin);
fclose(stdout);
return 0;
} |
#include <set>
#include <map>
#include <iostream>
using namespace std;
void add(multimap<long long, long long>& D, long long dist, long long i1, long long i2) {
D.emplace(dist, i1 > i2 ? i2 << 32 | i1 : i1 << 32 | i2);
}
int main(void){
// Your code here!
long long N;
multimap<long long, long long> X;
multimap<long long, long long> Y;
multimap<long long, long long> D;
cin>> N;
for (int i = 0; i < N; i++) {
long long x, y;
cin >> x >> y;
X.emplace(x, i);
Y.emplace(y, i);
}
auto xb = X.begin();
auto xr = X.rbegin();
auto yb = Y.begin();
auto yr = Y.rbegin();
add(D, abs(xr->first - xb->first), xr->second, xb->second);
xr++;
add(D, abs(xr->first - xb->first), xr->second, xb->second);
xr = X.rbegin();
xb++;
add(D, abs(xr->first - xb->first), xr->second, xb->second);
add(D, abs(yr->first - yb->first), yr->second, yb->second);
yr++;
add(D, abs(yr->first - yb->first), yr->second, yb->second);
yr = Y.rbegin();
yb++;
add(D, abs(yr->first - yb->first), yr->second, yb->second);
auto d = D.rbegin();
long long i = d->second;
// cout << d->first << d->second << endl;
d++;
// cout << d->first << d->second << endl;
if (i == d->second) {
d++;
// cout << d->first << d->second << endl;
}
cout << d->first;
}
| /**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author tatsumack
*/
#include <iostream>
#include <fstream>
#include <bits/stdc++.h>
#define int long long
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i)
#define REV(i, a, b) for (int i = (a); i >= (b); --i)
#define CLR(a, b) memset((a), (b), sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF 1001001001001001001ll
#define fcout cout << fixed << setprecision(12)
using namespace std;
class A2ndGreatestDistance {
public:
void solve(std::istream& cin, std::ostream& cout) {
int N;
cin >> N;
vector<int> x(N), y(N);
vector<pair<int, int>> vx(N), vy(N);
REP(i, N) {
cin >> x[i] >> y[i];
vx[i] = {x[i], i};
vy[i] = {y[i], i};
}
sort(vx.begin(), vx.end());
sort(vy.begin(), vy.end());
vector<int> cand;
cand.push_back(vx[0].second);
cand.push_back(vx[1].second);
cand.push_back(vx[N - 1].second);
cand.push_back(vx[N - 2].second);
cand.push_back(vy[0].second);
cand.push_back(vy[1].second);
cand.push_back(vy[N - 1].second);
cand.push_back(vy[N - 2].second);
sort(cand.begin(), cand.end());
cand.erase(unique(cand.begin(), cand.end()), cand.end());
vector<int> res;
REP(ci, cand.size()) {
int i = cand[ci];
FOR(cj, ci + 1, cand.size() - 1) {
int j = cand[cj];
res.push_back(max(abs(x[i] - x[j]), abs(y[i] - y[j])));
}
}
sort(res.rbegin(), res.rend());
cout << res[1] << endl;
}
};
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
std::istream& in(std::cin);
std::ostream& out(std::cout);
A2ndGreatestDistance solver;
solver.solve(in, out);
return 0;
}
|
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<math.h>
#include<unordered_set>
#include<unordered_map>
#include<cassert>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
#define mp make_pair
#define pb push_back
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define rrep(i,l,r) for(int i=l;i<=r;i++)
#define chmin(a,b) a=min(a,b)
#define chmax(a,b) a=max(a,b)
#define all(x) x.begin(),x.end()
#define unq(x) sort(all(x));x.erase(unique(all(x)),x.end())
#define mod 1000000007
//#define mod 998244353
#define ad(a,b) a=(a+b)%mod;
#define mul(a,b) a=a*b%mod;
void readv(vector<ll> &a,ll n){
rep(i,n){
ll x;
cin>>x;
a.push_back(x);
}
}
void outv(vector<ll> &a,ll n){
rep(i,n){
if(i>0)cout<<" ";
cout<<a[i];
}
cout<<"\n";
}
ll po(ll x,ll y){
ll res=1;
for(;y;y>>=1){
if(y&1)res=res*x%mod;
x=x*x%mod;
}
return res;
}
ll gcd(ll a,ll b){
return (b?gcd(b,a%b):a);
}
#define FACMAX 200010
ll fac[FACMAX],inv[FACMAX],ivf[FACMAX];
void initfac(){
fac[0]=ivf[0]=inv[1]=1;
for(ll i=1;i<FACMAX;i++)fac[i]=fac[i-1]*i%mod;
for(ll i=1;i<FACMAX;i++){
if(i>1)inv[i]=(mod-mod/i*inv[mod%i]%mod)%mod;
ivf[i]=po(fac[i],mod-2);
}
}
ll P(ll n,ll k){
if(n<0||n<k)return 0;
return fac[n]*ivf[n-k]%mod;
}
ll C(ll n,ll k){
if(k<0||n<k)return 0;
return fac[n]*ivf[n-k]%mod*ivf[k]%mod;
}
ll H(ll n,ll k){
return C(n+k-1,k);
}
ll n,m,a[10010],b[10010],c[110];
vector<ll> eid[110];
ll ans[10010];
bool ans_ok[10010];
vector<ll> g[110];
ll p[110],d[110];
bool vis[110];
void dfs(ll x,ll from,ll dep){
if(vis[x])return;
vis[x]=1;
d[x]=dep;
p[x]=from;
for(auto y:g[x])if(y!=from){
dfs(y,x,dep+1);
}
}
vector<ll> to[110];
ll dfs2(ll x){
if(vis[x])return 0;
ll res=1;
vis[x]=1;
for(auto y:to[x])res+=dfs2(y);
return res;
}
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
cin>>n>>m;
rep(i,m){
cin>>a[i]>>b[i];
a[i]--,b[i]--;
}
rep(i,n)cin>>c[i];
rep(i,m){
if(c[a[i]]==c[b[i]]){
eid[c[a[i]]].pb(i);
}
else{
if(c[a[i]]>c[b[i]])ans[i]=0;
else ans[i]=1;
}
ans_ok[i]=0;
}
for(int l=1;l<=n;l++){
rep(i,n)g[i].clear();
for(auto i:eid[l]){
g[a[i]].pb(b[i]);
g[b[i]].pb(a[i]);
}
for(int s=0;s<n;s++){
rep(i,n)vis[i]=0,p[i]=-1,d[i]=-1;
dfs(s,-1,0);
vector<ll> comp;
rep(i,n)if(vis[i])comp.pb(i);
vector<ll> es;
for(auto i:eid[l]){
if(vis[a[i]]==1&&vis[b[i]]==1)es.pb(i);
}
rep(i,n)to[i].clear();
for(auto i:es){
if(ans_ok[i])continue;
if(p[a[i]]==b[i]){
to[b[i]].pb(a[i]);
ans[i]=1;
}
else if(p[b[i]]==a[i]){
to[a[i]].pb(b[i]);
ans[i]=0;
}
else if(d[a[i]]<d[b[i]]){
to[b[i]].pb(a[i]);
ans[i]=1;
}
else{
to[a[i]].pb(b[i]);
ans[i]=0;
}
}
bool renketsu=1;
for(auto t:comp){
for(auto i:comp)vis[i]=0;
renketsu&=(dfs2(t)==(ll)comp.size());
}
if(renketsu){
for(auto i:es)ans_ok[i]=1;
}
}
}
rep(i,m){
cout<<(ans[i]==0?"->":"<-")<<"\n";
}
}
| #include <bits/stdc++.h>
#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define pb push_back
#define pob pop_back
#define pof pop_front
#define pf push_front
#define mp make_pair
#define ff first
#define ss second
#define endl "\n"
#define rep(i,a,b) for(ll i=a; i<b; i++)
#define rev(i,a,b) for(ll i=a; i>=b; i--)
#define decimal(n) fixed << setprecision(n)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define sz(v) v.size()
#define total_sum(v) accumulate(all(v),0)
#define make_unique(v) v.erase(unique(all(v)),v.end())
#define n_p(v) next_permutation(all(v))
#define p_p(v) prev_permutation(all(v))
#define for_each(it, X) for(__typeof((X).begin()) it = (X).begin(); it != (X).end(); it++)
#define re return
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair < int, int > pii;
typedef pair < ll, ll > pll;
typedef vector < pii > vii;
typedef vector < pll > vll;
typedef vector < int > vi;
typedef vector < ll > vl;
typedef vector < string > vs;
ll gcd ( ll , ll );
ll lcm ( ll , ll );
bool isPrime( ll );
ll power( ll , ll );
ll fact( ll );
void sieve_of_eratosthenes ( ll , vl &a);
//---------------------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------------
void solve()
{
ll a,b,c;
cin>>a>>b>>c;
if(c) {
if(b<=a) {
c=1;
}
else
c=0;
}
else {
if(a<=b) {
c=0;
}
else
c=1;
}
if(c) {
cout<<"Takahashi";
}
else
cout<<"Aoki";
}
int main()
{
boost;
ll t=1;
// cin>>t;
while(t--)
{
solve();
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------------
// HCF
ll gcd ( ll a , ll b ) {
return __gcd ( a , b ) ;
/*
if ( a==0 )
return b ;
return gcd ( b%a , a ) ;
*/
}
// LCM
ll lcm ( ll a , ll b ) {
return ( a * b ) / ( __gcd ( a , b ) ) ;
}
// PRIME
bool isPrime( ll n )
{
ll i=2;
for( i ; i*i<=n ; i++)
{
if( n % i == 0)
return false;
}
return true;
}
// POWER
ll power( ll b , ll a ) {
if(a==0) {
return 1;
}
ll c=1;
rep(i,0,a) {
c*=b;
}
return c;
}
// FACTORIAL
ll fact(ll n) {
ll c=1;
while(n>1) {
c*=n;
n--;
}
return c;
}
// SIEVE of ERATOSTHENES
void sieve_of_eratosthenes (ll n, vl &a) {
a[1]=0;
for(ll i=2; i*i<=n ; i++) {
if(a[i]==1) {
for(ll j=i+i; j<=n ; j+=i) {
a[j]=0;
}
}
}
}
//-------------------------------------------------------------*/--------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------------
|
#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------
int N;
vector<vector<int>> E;
ll vis[202020];
int have[202020];
ll id=1;
pair<int,int> farthest(vector<vector<int>>& E, int cur,int pre,int d,vector<int>& D) {
D[cur]=d;
pair<int,int> r={d,cur};
FORR(e,E[cur]) if(e!=pre) r=max(r, farthest(E,e,cur,d+1,D));
return r;
}
pair<int,vector<int>> diameter(vector<vector<int>>& E) { // diameter,center
vector<int> D[2];
D[0].resize(E.size());
D[1].resize(E.size());
auto v1=farthest(E,0,0,0,D[0]);
auto v2=farthest(E,v1.second,v1.second,0,D[0]);
farthest(E,v2.second,v2.second,0,D[1]);
pair<int,vector<int>> R;
R.first = v2.first;
//ไธก็ซฏใๅใๅ ดๅ
R.second.push_back(v1.second);
R.second.push_back(v2.second);
return R;
}
int dfs1(int cur,int pre,int tar) {
if(cur==tar) have[cur]=1;
vis[cur]=id;
FORR(e,E[cur]) if(e!=pre) {
have[cur]|=dfs1(e,cur,tar);
}
return have[cur];
}
void dfs2(int cur,int pre) {
vis[cur]=id;
FORR(e,E[cur]) if(e!=pre) {
if(have[e]==0) {
id++;
dfs2(e,cur);
}
}
FORR(e,E[cur]) if(e!=pre) {
if(have[e]) {
id++;
dfs2(e,cur);
}
}
id++;
}
void solve() {
int i,j,k,l,r,x,y; string s;
cin>>N;
E.resize(N);
FOR(i,N-1) {
cin>>x>>y;
E[x-1].push_back(y-1);
E[y-1].push_back(x-1);
}
auto a=diameter(E);
x=a.second[0];
y=a.second[1];
dfs1(x,x,y);
dfs2(x,x);
FOR(i,N) cout<<vis[i]<<" ";
cout<<endl;
}
int main(int argc,char** argv){
string s;int i;
if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
cout.tie(0); solve(); return 0;
}
| #include <cstdio>
#include <vector>
const int maxn = 2e5+5;
int n,ans[maxn],maxx,X,Y;
int head[maxn],tot;
struct Edge{
int next,to;
}e[maxn<<1];
void add_edge(int x,int y){
e[++tot].next = head[x];
e[tot].to = y,head[x] = tot;
}
int fa[maxn],top[maxn],siz[maxn],dep[maxn],son[maxn],Flag[maxn];
void dfs1(int u,int F){
siz[u] = 1,fa[u] = F,dep[u] = dep[F] + 1;
for(int i=head[u],v;v=e[i].to,i;i=e[i].next){
if(v == F)continue;
dfs1(v,u);
siz[u] += siz[v];
if(siz[v] > siz[son[u]])son[u] = v;
}
}
void dfs2(int u,int T){
top[u] = T;
if(son[u])dfs2(son[u],T);
for(int i=head[u],v;v=e[i].to,i;i=e[i].next)
if(v != son[u] && v != fa[u])
dfs2(v,v);
}
int LCA(int x,int y){
while(top[x]^top[y]){
if(dep[top[x]]<dep[top[y]])x^=y^=x^=y;
x = fa[top[x]];
}return dep[x]<dep[y]?x:y;
}
void getchain(int u,int F){
for(int i=head[u],v;v=e[i].to,i;i=e[i].next){
if(v == F)continue;
getchain(v,u);
if(Flag[v])Flag[u] = 1;
}
}
int Dis(int x,int y){return dep[x]+dep[y]-2*dep[LCA(x,y)];}
int nowans,pre;
void getans(int u,int F){
ans[u] = (nowans+=Dis(u,pre)),pre = u;
for(int i=head[u],v;v=e[i].to,i;i=e[i].next)
if(Flag[v] == 0 && v != F)
getans(v,u);
for(int i=head[u],v;v=e[i].to,i;i=e[i].next)
if(v != F && Flag[v] == 1)
getans(v,u);
}
int main(){
scanf("%d",&n);
for(int i=1,x,y;i<n;++i)
scanf("%d %d",&x,&y),add_edge(x,y),add_edge(y,x);
dfs1(1,0),maxx = 0;
for(int i=1;i<=n;++i)if(maxx < dep[i])maxx = dep[i],X = i;
for(int i=1;i<=n;++i)fa[i] = son[i] = dep[i] = siz[i] = 0;
dfs1(X,0),maxx = 0,dfs2(X,X);
for(int i=1;i<=n;++i)if(maxx < dep[i])maxx = dep[i],Y = i;
Flag[Y] = 1,getchain(X,0);
nowans = 1,pre = X,getans(X,0);
for(int i=1;i<=n;++i)printf("%d ",ans[i]);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
constexpr long long mod = 998244353;
long long modpow(int n, int r) {
long long ret = 1; long long tmp = (long long) n;
while (r != 0) {
if (r % 2) ret *= tmp;
tmp *= tmp; tmp %= mod; ret %= mod;
r /= 2;
}
return ret;
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int N, M; cin >> N >> M;
long long Ans = 0;
vector<long long> M_pow(N+1, 1);
for (int i = 0; i < N; i++) {
M_pow[i+1] = M_pow[i] * M % mod;
}
for (int min_val = 1; min_val <= M; min_val++) {
for (int interval = 2; interval <= N; interval++) {
long long fac = N - interval + 1;
long long tmp = modpow(M - min_val, interval - 2) * M_pow[N - interval] % mod;
Ans += fac * tmp % mod;
Ans %= mod;
// cout << min_val << " " << interval << " " << fac << tmp % mod << "\n";
}
}
Ans = mod + M_pow[N] * N % mod - Ans;
cout << Ans % mod << "\n";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
template <int MOD_> struct modnum {
static constexpr int MOD = MOD_;
static_assert(MOD_ > 0, "MOD must be positive");
private:
using ll = long long;
int v;
static int minv(int a, int m) {
a %= m;
assert(a);
return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a);
}
public:
modnum() : v(0) {}
modnum(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; }
explicit operator int() const { return v; }
friend std::ostream& operator << (std::ostream& out, const modnum& n) { return out << int(n); }
friend std::istream& operator >> (std::istream& in, modnum& n) { ll v_; in >> v_; n = modnum(v_); return in; }
friend bool operator == (const modnum& a, const modnum& b) { return a.v == b.v; }
friend bool operator != (const modnum& a, const modnum& b) { return a.v != b.v; }
modnum inv() const {
modnum res;
res.v = minv(v, MOD);
return res;
}
friend modnum inv(const modnum& m) { return m.inv(); }
modnum neg() const {
modnum res;
res.v = v ? MOD-v : 0;
return res;
}
friend modnum neg(const modnum& m) { return m.neg(); }
modnum operator- () const {
return neg();
}
modnum operator+ () const {
return modnum(*this);
}
modnum& operator ++ () {
v ++;
if (v == MOD) v = 0;
return *this;
}
modnum& operator -- () {
if (v == 0) v = MOD;
v --;
return *this;
}
modnum& operator += (const modnum& o) {
v += o.v;
if (v >= MOD) v -= MOD;
return *this;
}
modnum& operator -= (const modnum& o) {
v -= o.v;
if (v < 0) v += MOD;
return *this;
}
modnum& operator *= (const modnum& o) {
v = int(ll(v) * ll(o.v) % MOD);
return *this;
}
modnum& operator /= (const modnum& o) {
return *this *= o.inv();
}
friend modnum operator ++ (modnum& a, int) { modnum r = a; ++a; return r; }
friend modnum operator -- (modnum& a, int) { modnum r = a; --a; return r; }
friend modnum operator + (const modnum& a, const modnum& b) { return modnum(a) += b; }
friend modnum operator - (const modnum& a, const modnum& b) { return modnum(a) -= b; }
friend modnum operator * (const modnum& a, const modnum& b) { return modnum(a) *= b; }
friend modnum operator / (const modnum& a, const modnum& b) { return modnum(a) /= b; }
};
template <typename T> T pow(T a, long long b) {
assert(b >= 0);
T r = 1; while (b) { if (b & 1) r *= a; b >>= 1; a *= a; } return r;
}
using num = modnum<998244353>;
vector<num> fact, ifact;
void init(){
int N = 1100000;
fact = {1};
for(int i = 1; i < N; i++) fact.push_back(i * fact[i-1]);
ifact.resize(N);
ifact.back() = 1 / fact.back();
for(int i = N - 1; i > 0; i--) ifact[i-1] = i * ifact[i];
}
num ncr(int n, int k){
if(k < 0 || k > n) return 0;
return fact[n] * ifact[k] * ifact[n-k];
}
int main(){
ios_base::sync_with_stdio(false), cin.tie(nullptr);
init();
int n, m;
cin >> n >> m;
num ans = pow(num(m), n) * n;
vector<num> r(m, 1);
for(int len = 1; len < n; len++){
num mult = pow(num(m), n-1-len) * (n-len);
num v = 0;
for(int a = 0; a < m; a++){
v += r[a];
r[a] *= a;
}
ans -= mult * v;
}
cout << ans << '\n';
}
|
#include "bits/stdc++.h"
using namespace std;
// ๅฎ็พฉ
typedef long long ll;
typedef pair<ll, ll> P;
#define ALL(x) (x).begin(),(x).end()
#define REP(i, n) for(ll i = 0 ; i < (ll)n ; ++i)
#define REPN(i, m, n) for(ll i = m ; i < (ll)n ; ++i)
#define VL vector<ll>
#define VVL vector<vector<ll>>
#define VVVL vector<vector<vector<ll>>>
#define INF (ll)2e9
#define INF_LL 1LL<<60
#define MOD 998244353
//#define MOD 1000000007
ll Ceil(ll val, ll div) { return (val + div - 1) / div; }
ll CeilN(ll val, ll div) { return Ceil(val, div) * div; }
ll FloorN(ll x, ll n) { return (x - x % n); }
bool IsOdd(ll x) { return ((x & 1) == 1); }
bool IsEven(ll x) { return ((x & 1) == 0); }
template<class T>
class UnionFind
{
private:
vector<T> parentNo;
vector<T> parentRank;
vector<T> parentSize;
public:
// ใณใณในใใฉใฏใฟ
UnionFind(int n)
{
parentNo.resize(n);
parentRank.resize(n, 0);
parentSize.resize(n, 1);
for (int i = 0; i < n; i++) {
// ๅงใใฏๅ
จ้จROOT
parentNo[i] = i;
}
}
int root(T x)
{
if (parentNo[x] == x) {
return x;
} else {
T rootParentNo = root(parentNo[x]);
parentNo[x] = rootParentNo;
return rootParentNo;
}
}
bool same(T x, T y)
{
return root(x) == root(y);
}
int size(T x)
{
return parentSize[root(x)];
}
void unite(T xc, T yc)
{
T xr = root(xc);
T yr = root(yc);
if (xr == yr) {
return;
}
if (parentRank[xr] < parentRank[yr]) {
parentNo[xr] = yr;
parentSize[yr] += parentSize[xr];
} else {
parentNo[yr] = xr;
parentSize[xr] += parentSize[yr];
if (parentRank[xr] == parentRank[yr]) {
parentRank[xr]++;
}
}
}
};
void Solve()
{
ll N;
cin >> N;
vector<P> xy(N);
REP(i, N) cin >> xy[i].first >> xy[i].second;
sort(ALL(xy));
auto compExec = [&](double mid) {
ll s = N;
ll t = N + 1;
UnionFind<ll> uf(N + 2);
REP(i, N) {
if (100 - xy[i].second < mid) {
uf.unite(i, s);
}
if (100 + xy[i].second< mid) {
uf.unite(i, t);
}
}
REP(i, N) {
REP(j, N) {
if (i == j) continue;
auto a = xy[i];
auto b = xy[j];
double distOne = pow(b.first - a.first, 2) + pow(b.second - a.second, 2);
if (distOne < pow(mid, 2)) {
uf.unite(i, j);
}
}
}
return !uf.same(s, t);
};
double lb = 0;
double lu = 200;
double mid = 0;
REP(i, 121) {
mid = (lb + lu) / 2;
if (compExec(mid)) {
lb = mid;
} else {
lu = mid;
}
}
cout << lb / 2 << endl;
}
// ใกใคใณ
int main()
{
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(15);
Solve();
return 0;
}
| #include <bits/stdc++.h>
#include <math.h>
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vll>;
using vpl = vector<pair<ll, ll>>;
using pll = pair<ll, ll>;
#define rep(i, k, n) for(ll i = k; i < n; i++)
#define pb push_back
#define mp make_pair
int main(){
ld n; cin >> n;
ld x0, y0; cin >> x0 >> y0;
ld xn, yn; cin >> xn >> yn;
const long double pi = 3.141592653589793238462643;
ld dx = x0-xn;
ld dy = y0-yn;
long double theta = (long double)(pi) / (long double)(n);
// cout << theta << endl;
ld dx1 = dx*(ld)(cos(theta)) - dy*(ld)(sin(theta));
ld dy1 = dx*(ld)(sin(theta)) + dy*(ld)(cos(theta));
ld x1 = xn + dx1*(ld)(cos(theta));
ld y1 = yn + dy1*(ld)(cos(theta));
cout << fixed << setprecision(10) << x1;
cout << " ";
cout << fixed << setprecision(10) << y1 << endl;
} |
#include<algorithm>
#include<iostream>
#include<stdio.h>
#include<iostream>
#include<stdio.h>
#include<vector>
#include<set>
#include<string>
#include<iomanip>
#include<queue>
#include<functional>
#include<cmath>
#include<map>
#include<tuple>
#include <numeric>
#define rep(i,n) for(int i=0; i<(int)n; i++)//nๅ็นฐใ่ฟใ
#define rrep(i,n)for(int i=n-1;i>=0;i--)
#define REP(i,n) for (int i = 1; i <= (int)n; i++)//i=1ใใi=nใพใง
#define RREP(i,n)for(int i=n;i>=1;i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using graph = vector<vector<int>>;
const ll mod = pow(10, 9) + 7;
const int INF = 1e9;
const ll LINF = 1e18;
const double pi = 3.14159265358979323846;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
int main() {
char s, t;
cin >> s >> t;
if (s == 'Y') {
t = t - ('a' - 'A');
cout << t << endl;
}
else {
cout << t << endl;
}
}
| #include<bits/stdc++.h>
typedef long long int ll;
using namespace std;
int main()
{
char S,T;
cin>>S>>T;
if(S=='Y')
{
if(T=='a')
cout<<"A";
else if(T=='b')
cout<<"B";
else if(T=='c')
cout<<"C";
else
cout<<T;
}
else
cout<<T;
return 0;
} |
#include<iostream>
#include<map>
using namespace std;
int a[70000];
int main(){
int n,ans;cin>>n;map<int,bool>mp;
for(int i=0;i<(1<<n);++i)cin>>a[i];
for(int i=0;i<n;++i){int x=0,y=1;
for(int j=0;j<(1<<(n-i-1));++j){
while(mp[x])++x;
while(mp[y]||y<=x)++y;
if(a[x]>a[y])mp[y]=1,ans=y,x=y+1,y=x+1;
else mp[x]=1,ans=x,x=y+1,y=x+1;
}
}
cout<<ans+1<<endl;
return 0;
} | #include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#define rg register
inline int read(){
rg int x=0,fh=1;
rg char ch=getchar();
while(ch<'0' || ch>'9'){
if(ch=='-') fh=-1;
ch=getchar();
}
while(ch>='0' && ch<='9'){
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
return x*fh;
}
const int maxn=1e6+5;
int n,a[maxn],mmax,mmax2,cs1,cs2,jl1,jl2;
int main(){
n=read();
mmax=(1<<n);
mmax2=mmax/2;
for(rg int i=1;i<=mmax;i++) a[i]=read();
for(rg int i=1;i<=mmax2;i++){
if(a[i]>cs1){
cs1=a[i];
jl1=i;
}
}
for(rg int i=mmax2+1;i<=mmax;i++){
if(a[i]>cs2){
cs2=a[i];
jl2=i;
}
}
if(a[jl1]>a[jl2]) printf("%d\n",jl2);
else printf("%d\n",jl1);
return 0;
}
|
/**
* @FileName a.cpp
* @Author kanpurin
* @Created 2021.05.09 01:16:12
**/
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
ll C(__int128_t N, __int128_t L) {
__int128_t ans = 0;
if (L-3 >= 3*N) ans -= (L-3*N)*(L-3*N-1)*(L-3*N-2)/6;
if (L-3 >= 2*N) ans += 3*(L-2*N)*(L-2*N-1)*(L-2*N-2)/6;
if (L-3 >= N) ans -= 3*(L-N)*(L-N-1)*(L-N-2)/6;
if (L-3 >= 0) ans += L*(L-1)*(L-2)/6;
return ans;
}
ll D(__int128_t N, __int128_t L, __int128_t B) {
__int128_t ans = 0;
if (L-3 >= 2*N+B) ans -= (L-3-(2*N+B)+2)*(L-3-(2*N+B)+1)/2;
if (L-3 >= N+B) ans += 2*(L-3-(N+B)+2)*(L-3-(N+B)+1)/2;
if (L-3 >= B) ans -= (L-3-(B)+2)*(L-3-(B)+1)/2;
if (L-3 >= 2*N) ans += (L-3-(2*N)+2)*(L-3-(2*N)+1)/2;
if (L-3 >= N) ans -= 2*(L-3-(N)+2)*(L-3-(N)+1)/2;
if (L-3 >= 0) ans += (L-3-(0)+2)*(L-3-(0)+1)/2;
return ans;
}
int main() {
int N;cin >> N;
ll K;cin >> K;
int L1 = 2, L2 = 3*N;
while(L2 - L1 > 1) {
int mid = (L1+L2)/2;
if (C(N,mid) >= K) L2 = mid;
else L1 = mid;
}
int L = L2;
K -= C(N,L-1);
int B1 = 0,B2 = N;
while(B2 - B1 > 1) {
int mid = (B1+B2)/2;
if (D(N,L,mid) >= K) B2 = mid;
else B1 = mid;
}
int B = B2;
K -= D(N,L,B-1);
if (L-B <= N) cout << B << " " << K << " " << L-B-K << endl;
else cout << B << " " << L-B-N+K-1 << " " << N-K+1 << endl;
return 0;
} | #include <iostream>
#include <algorithm>
#define int long long
#define rep(i, n) for(i = 0; i < n; i++)
using namespace std;
const int MAX = 1000000;
int n, K;
int cnt_ijk[MAX * 3];
int cnt_jk[MAX * 2];
int r_cnt_jk[MAX * 2];
signed main() {
int s, i, j, k;
cin >> n >> K;
//j + k = s, 0 <= j, k < n
for (s = 0; s <= 2 * n - 2; s++) {
if (s < n) cnt_jk[s] = s + 1;
else cnt_jk[s] = 2 * n - 1 - s;
}
//j + k < s, 0 <= j, k < n
for (s = 1; s <= 2 * n - 1; s++) {
r_cnt_jk[s] = r_cnt_jk[s - 1] + cnt_jk[s - 1];
}
//i + j + k = s, 0 <= i, j, k < n
for (s = 0; s <= 3 * n - 3; s++) {
//l <= j + k && j + k <= r
int l = max(0LL, s - (n - 1));
int r = min(2 * n - 2, s);
cnt_ijk[s] = r_cnt_jk[r + 1] - r_cnt_jk[l];
}
//detect. s = i + j + k
for (s = 0; s <= 3 * n - 3; s++) {
K -= cnt_ijk[s];
if (K <= 0) break;
}
K += cnt_ijk[s];
//cout << "s = " << s << ", K = " << K << endl;
//detect. i
for (i = 0; i < n; i++) {
//let's count : j + k = s - i, 0 <= j, k < n
if (s - i < 0 || s - i >= 2 * n - 1) continue;
K -= cnt_jk[s - i];
if (K <= 0) break;
}
K += cnt_jk[s - i];
//cout << "i = " << i << ", K = " << K << endl;
//detect. j
for (j = 0; j < n; j++) {
k = s - i - j;
if (0 <= k && k < n) K--;
if (K == 0) break;
}
//cout << "j = " << j << ", K = " << K << endl;
k = s - i - j;
//to 1-index
i++; j++; k++;
cout << i << " " << j << " " << k << endl;
return 0;
} |
//----------BHAVIK DIWANI(PICT_COMP)---------------
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define test ll t; cin>>t; while(t--)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define mod 1000000007
#define ll long long
#define int long long
#define ull unsigned long long
#define MAX 1000005
#define pb push_back
#define mkp make_pair
#define vi vector<int>
#define pii pair<int,int>
#define endl '\n'
#define vs vector<string>
#define mii map<int,int>
#define msi map<string,int>
#define vpii vector< pair<int, int > >
#define vpsi vector< pair< string ,int > >
#define forci(p,q) for(int i=p;i<q;i++)
ll gcd(ll a,ll b){ if(b==0) return a; return gcd(b,a%b);}
ll lcm(ll a,ll b) { return (a/gcd(a,b)*b);}
ull power(ull a, ull b) {a %= mod; ull res = 1; while (b > 0) { if (b & 1) res = res * a % mod; a = a * a % mod; b >>= 1; } return res%mod; }
ll modmul(ll x, ll y){return (x*y)%mod;}
ll modadd(ll x, ll y){return (x+y)%mod;}
ll modsub(ll x, ll y){return (x-y+mod)%mod;}
ll fact[1000007]={0};
void facto() {fact[0]=1;fact[1]=1;for(int i=2;i<1000007;i++)fact[i]=(fact[i-1]*i)%mod;}
ll ncr(ll n,ll r) {ll res=1; res=fact[n]; res=(res*(power(fact[r],mod-2)))%mod; res=(res*(power(fact[n-r],mod-2)))%mod; return res;}
ll npr(ll n,ll r) {ll res=1; res=fact[n]; res=(res*(power(fact[n-r],mod-2)))%mod; return res;}
inline long long toint(const std::string &s) {std::stringstream ss; ss << s; long long x; ss >> x; return x;}
inline std::string tostring(long long number) {std::stringstream ss; ss << number; return ss.str();}
inline std::string tobin(long long x) {return std::bitset<63>(x).to_string();}
const int inf=1e9;
/*bool prime[MAX];
ull sieve(){memset(prime,true,sizeof(prime));for(ull p=2;p*p<MAX;p++){if(prime[p]==true){for(ull i=2*p;i<MAX;i+=p){prime[i]=false;}}}prime[0]=0;prime[1]=0;
}*/
/*
int inv[1000001]={0};int invf[1000001]={0};
void findinverse(){
inv[0]=1;
inv[1] = 1;
for(int i = 2; i <=1e6 ; ++i)
inv[i] = (mod - (mod/i) * inv[mod%i] % mod) % mod;
invf[0]=1;invf[1]=1;
for(int i=2;i<=1e6;i++){
invf[i]=invf[i-1]*inv[i]%mod;
}
}*/
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> orderedSet;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> orderedMultiset;
int solve()
{
int n,x;
cin>>n>>x;
int alcohol=0;
for(int i=1;i<=n;i++){
int v,p;
cin>>v>>p;
alcohol+=(v*p);
if(alcohol>x*100LL){
cout<<i<<endl;
return 0;
}
}
cout<<"-1"<<endl;
cout<<endl;
return 0;
}
signed main()
{
fastio;
// test
solve();
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
int N, X;
cin >> N >> X;
X = X * 100;
vector<pair<int, int>> V;
for (int i = 0; i < N; i++)
{
int v, p;
cin >> v >> p;
V.push_back(make_pair(v,p));
}
int total_intake = 0;
for(int i = 0; i < N; i++)
{
int intake = V[i].first * V[i].second;
total_intake += intake;
if(total_intake > X)
{
cout<<i + 1<<endl;
return 0;
}
}
cout<<-1<<endl;
return 0;
}
|
#include <iostream>
#include <vector>
#include <queue>
#define rep(i, s, n) for(int i = (s); i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main()
{
int A, B;
cin >> A >> B;
cout << 2 * A + 100 - B;
}
| #include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mkp make_pair
#define vi vector<int>
#define pii pair<int,int>
#define FI(n) FastIO::read(n)
#define FO(n) FastIO::write(n)
#define ull unsigned long long
#define mst(a,b) memset(a,b,sizeof(a))
#define foR(i,k,j) for(int i=(k);i>=(j);i--)
#define For(i,k,j) for(int i=(k);i<=(j);i++)
#define Foe(i,u) for(int i=lst[u],v=e[i].v;i;i=e[i].nxt,v=e[i].v)
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define Fin(s) freopen(s,"r",stdin)
#define Fout(s) freopen(s,"w",stdout)
#define file(s) Fin(s".in"),Fout(s".out")
#define INF ((1<<30)-1)
//#define int long long
const int P=1e9+7; //
using namespace std;
template<typename T>inline void ckmax(T &a,T b) {(a<b)&&(a=b);}
template<typename T>inline void ckmin(T &a,T b) {(a>b)&&(a=b);}
inline int mul(int a,int b) {return 1ull*a*b%P;}
inline int add(int a,int b) {return a+b>=P?a+b-P:a+b;}
inline int sub(int a,int b) {return a-b>=0?a-b:a-b+P;}
inline void mulmod(int &a,int b) {a=mul(a, b);}
inline void addmod(int &a,int b) {((a+=b)>=P)&&(a-=P);}
inline void submod(int &a,int b) {((a-=b)<0)&&(a+=P);}
inline int ksm(int a,int b) {int ans=1; for(;b;b>>=1) {if(b&1) ans=1ll*ans*a%P;a=1ll*a*a%P;}return ans;}
inline void fprint(const int &x,char c=' ') {fprintf(stderr,"%d%c",x,c);}
inline void fprint(const pii &x,char c='\n') {fprintf(stderr,"%d %d%c",x.first,x.second,c);}
inline void fprint(const int *f,const int &n,char c='\n') {for(int i=1;i<=n;i++) fprint(f[i]); fprintf(stderr,"%c",c);}
inline void fprint(const vector<int> &f,char c='\n') {for(int i=0;i<(int)f.size();i++) fprint(f[i]); fprintf(stderr,"%c",c);}
inline int inv(int a) {return ksm(a,P-2);}
namespace FastIO {
const int SIZE=1<<16; char buf[SIZE],obuf[SIZE],str[64]; int bi=SIZE,bn=SIZE,opt;
int read(char *s) {
while (bn) {for (;bi<bn&&buf[bi]<=' ';bi++);if (bi<bn) break; bn=fread(buf,1,SIZE,stdin),bi=0;}
int sn=0;while (bn) {for (;bi<bn&&buf[bi]>' ';bi++) s[sn++]=buf[bi];if (bi<bn) break; bn=fread(buf,1,SIZE,stdin),bi=0;}s[sn]=0;return sn;
}
bool read(int& x) {if(x)x=0;int bf=0,n=read(str); if(!n) return 0; int i=0; if (str[i]=='-') bf=1,i=1; for(x=0;i<n;i++) x=x*10+str[i]-'0'; if(bf) x=-x; return 1;}
void write(int x) {
if(!x) obuf[opt++]='0'; else {if(x<0) obuf[opt++]='-',x=-x;int sn=0; while(x)str[sn++]=x%10+'0',x/=10;for (int i=sn-1;i>=0;i--) obuf[opt++]=str[i];}
if (opt>=(SIZE>>1)){fwrite(obuf,1,opt,stdout); opt=0;}
}
void write(char x) {obuf[opt++]=x;if (opt>=(SIZE>>1)){fwrite(obuf,1,opt,stdout); opt=0;}}
void Fflush() {if (opt) fwrite(obuf,1,opt,stdout); opt=0;}
};
inline int read() {int x; FI(x); return x;}
const int MN=4e6+5;
int fac[MN],ifac[MN];
int C(int n,int m) {
if(n<m||n<0||m<0) return 0; int ans=1,t=1;
For(i,n-m+1,n) mulmod(ans,i);
For(i,1,m) mulmod(t,i);
return mul(ans,inv(t));
}
int n,m,A[MN];
signed main() {
#ifndef ONLINE_JUDGE
file("pro");
#endif
n=read(),m=read(); int sum=0;
For(i,1,n) A[i]=read(),sum+=A[i];
cout<<C(m+n,sum+n)<<endl;
return FastIO::Fflush(),0;
}
|
#include <bits/stdc++.h>
using namespace std;
using tint = long long;
using ld = long double;
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)
using pi = pair<int,int>;
using pl = pair<tint,tint>;
using vi = vector<int>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vvi = vector<vi>;
using vl = vector<tint>;
using vb = vector<bool>;
#define pb push_back
#define pf push_front
#define rsz resize
#define all(x) begin(x), end(x)
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)(x).size()
#define ins insert
#define f first
#define s second
#define mp make_pair
#define DBG(x) cerr << #x << " = " << x << endl;
const int MOD = 1e9+7;
const tint mod = 998244353;
const int MX = 2e5+5;
const tint INF = 1e18;
const int inf = 2e9;
const ld PI = acos(ld(-1));
const ld eps = 1e-5;
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
template<class T> void remDup(vector<T> &v){
sort(all(v)); v.erase(unique(all(v)),end(v));
}
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;
}
bool valid(int x, int y, int n, int m){
return (0<=x && x<n && 0<=y && y<m);
}
int cdiv(int a, int b) { return a/b+((a^b)>0&&a%b); } //redondea p arriba
int fdiv(int a, int b) { return a/b-((a^b)<0&&a%b); } //redondea p abajo
void NACHO(string name = ""){
ios_base::sync_with_stdio(0); cin.tie(0);
if(sz(name)){
freopen((name+".in").c_str(), "r", stdin);
freopen((name+".out").c_str(), "w", stdout);
}
}
pair<pl, tint> a[MX];
tint b[2*MX];
vl todo;
map<tint, tint> m;
int main(){
NACHO();
tint n, c; cin >> n >> c;
F0R(i, n){
cin >> a[i].f.f >> a[i].f.s >> a[i].s;
todo.pb(a[i].f.f);
todo.pb(a[i].f.s+1);
}
remDup(todo);
trav(u, todo){
m[u] = sz(m);
}
F0R(i, n){
b[m[a[i].f.f]] += a[i].s;
b[m[a[i].f.s+1]] -= a[i].s;
}
FOR(i, 1, 2*n+1) b[i] += b[i-1];
tint ret = 0;
F0R(i, sz(todo)-1) ret += min(b[i], c)*(todo[i+1]-todo[i]);
cout << ret << "\n";
}
| #include <iostream>
#include <iomanip>
#include <algorithm>
#include <array>
#include <cassert>
#include <optional>
#include <utility>
#include <vector>
// #include <atcoder/all>
template <class InputIterator>
std::ostream& range_output(std::ostream& os_arg, InputIterator first_arg, InputIterator last_arg){ if(first_arg != last_arg){ do{ os_arg << *(first_arg++); if(first_arg == last_arg) break; os_arg << ' '; } while(true); } return os_arg; }
template <class Tp> std::ostream& operator << (std::ostream& os_arg, const std::vector<Tp>& arr_arg){ return range_output(os_arg, arr_arg.cbegin(), arr_arg.cend()); }
template <class Tp, std::size_t Size> std::ostream& operator << (std::ostream& os_arg, const std::array<Tp, Size>& arr_arg){ return range_output(os_arg, arr_arg.cbegin(), arr_arg.cend()); }
template <class S, class T> std::ostream& operator << (std::ostream& os_arg, const std::pair<S, T>& pair_arg){ return os_arg << '(' << pair_arg.first << ", " << pair_arg.second << ')'; }
#ifndef ONLINE_JUDGE
template <typename Head> void dump_out(Head head_arg){ std::cerr << head_arg << '\n'; }
template <typename Head, typename... Tail>
void dump_out(Head head_arg, Tail... tail_args){ std::cerr << head_arg << ", "; dump_out(tail_args...); }
#define dump(...) do { std::cerr << "[in line " << __LINE__ << "] " << #__VA_ARGS__ << " : "; dump_out(__VA_ARGS__); } while(false)
#else
#define dump(...) (void(0))
#endif
int main(void){
std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(16);
int n, k; std::cin >> n >> k;
std::vector<std::vector<int>> T(n, std::vector<int>(n));
for(int i = 0; i < n; ++i) for(int j = 0; j < n; ++j) std::cin >> T[i][j];
std::vector<int> Lp(n - 1); for(int i = 0; i < n - 1; ++i) Lp[i] = i + 1;
int res = 0;
do{
int d = T[0][Lp.front()] + T[Lp.back()][0];
for(int i = 0; i + 1 < n - 1; ++i) d += T[Lp[i]][Lp[i + 1]];
if(d == k) ++res;
} while(std::next_permutation(Lp.begin(), Lp.end()));
std::cout << res << '\n';
return 0;
} |
#include<bits/stdc++.h>
#define int long long
#define N 200005
#define MOD 1000000007
using namespace std;
int n,d[N],ans;
vector<int> to[N],w[N];
void add(int u,int v,int wt){to[u].push_back(v),w[u].push_back(wt);}
void dfs(int u,int fa){
for(int i=0,v;i<to[u].size();i++)
if((v=to[u][i])!=fa)d[v]=d[u]^w[u][i],dfs(v,u);
}
signed main(){
cin>>n;
for(int i=1,u,v,wt;i<n;i++)scanf("%lld%lld%lld",&u,&v,&wt),add(u,v,wt),add(v,u,wt);
dfs(1,0);
for(int k=0;k<60;k++){
int a=0;
for(int i=1;i<=n;i++)a+=((d[i]>>k)&1);
ans=(ans+a*(n-a)%MOD*((1ll<<k)%MOD)%MOD)%MOD;
}
cout<<ans<<endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ffor(i, a, b) for (int i = a ; i <= (int) b ; i++)
#define rfor(i, a, b) for (int i = (int) a ; i >= b ; i--)
#define vec vector
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define F first
#define S second
using ll = long long int; // watch out for overflows in your code
using pii = pair<int,int>;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void debug() { cout << "\n"; };
template <typename T, typename... Targs>
void debug(T t, Targs... args) {
cout << t << " ";
debug(args...);
}
template <typename T1, typename T2>
inline ostream& operator << (ostream& os, const pair<T1,T2>& p) {
return os << "(" << p.F << ", " << p.S << ")";
}
template <typename T>
inline ostream& operator << (ostream& os, const vec<T>& v) {
for(auto& t : v) {
os << t << " ";
}
return os;
}
template <typename T>
inline ostream& operator << (ostream& os, const vec<vec<T>>& m) {
for(auto& v : m) {
for(auto& t : v) {
os << t << " ";
}
os << "\n";
}
return os;
}
template <typename T>
inline ostream& operator << (ostream& os, const set<T>& v) {
for(auto& t : v) {
os << t << " ";
}
return os;
}
template <typename K, typename V>
inline ostream& operator << (ostream& os, const map<K,V>& m) {
for(auto& p : m) {
os << p.F << " -> " << p.S << "\n";
}
return os;
}
////////////////////////////////////////////////////////////////////////
template <ll mod>
class modular_int {
private:
ll x;
public:
modular_int(ll y) : x( (y % mod + mod) % mod) {}
modular_int() : x(0LL) {}
ll rpr() const { return x; }
modular_int<mod>& operator += (const modular_int<mod>& rhs) {
x = (x + rhs.x) % mod;
return *this;
}
friend modular_int<mod> operator + (modular_int<mod> lhs, const modular_int<mod>& rhs) {
lhs += rhs;
return lhs;
}
modular_int<mod>& operator *= (const modular_int<mod>& rhs) {
x = (x * rhs.x) % mod;
return *this;
}
friend modular_int<mod> operator * (modular_int<mod> lhs, const modular_int<mod>& rhs) {
lhs *= rhs;
return lhs;
}
modular_int<mod>& operator -= (const modular_int<mod>& rhs) {
x = ((x - rhs.x) % mod + mod) % mod;
return *this;
}
friend modular_int<mod> operator - (modular_int<mod> lhs, const modular_int<mod>& rhs) {
lhs -= rhs;
return lhs;
}
};
const ll MOD = 1e9 + 7;
using mint = modular_int<MOD>;
////////////////////////////////////////////////////////////////////////
using edge = pair<int, ll>;
int N;
vec<vec<edge>> g;
vec<ll> d;
void bfs() {
d.assign(N+1, -1);
queue<edge> q;
q.push({1,0});
d[1] = 0;
while(!q.empty()) {
auto [u, pw] = q.front(); q.pop();
for(auto [v, w] : g[u]) {
if(d[v] != -1) continue;
d[v] = pw ^ w;
q.push({v, d[v]});
}
}
}
////////////////////////////////////////////////////////////////////////
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N;
g.assign(N+1, vec<edge>());
ffor(i, 1, N) {
int u, v; ll w;
cin >> u >> v >> w;
g[u].PB({v, w});
g[v].PB({u, w});
}
bfs();
//debug("d=", d);
mint ans(0LL);
ffor(k, 0, 60) {
mint p(1LL << k);
ll ones = 0LL, zeros = 0LL;
ffor(u, 1, N) {
if(d[u] & (1LL << k)) {
ones++;
}
else {
zeros++;
}
}
ans += p * mint(ones) * mint(zeros);
//debug(k, ones, zeros, ans.rpr());
}
cout << ans.rpr() << "\n";
}
|
// Coder: @SumitRaut
#include <bits/stdc++.h>
#define speedup ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr),cout.precision(12)
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef tree<int, int, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_map;
#define sbc __builtin_popcount
#define pb push_back
#define pbk pop_back
#define em emplace
#define emb emplace_back
#define ff first
#define ss second
#define ll long long
const ll mod = 1e9 + 7;
template<typename T> void smax(T& a, T b) { if(a<b) a=b; }
template<typename T> void smin(T& a, T b) { if(a>b) a=b; }
template<typename T> T pw(T a,T b) { T p=1,one=1; while(b) { if(b&one) p=p*a; a=a*a; b >>=1; } return p; }
template<typename T> T pwm(T a,T b,T md=mod) { T p=1,one=1; while(b) { if(b&one) p=p*a%md; a=a*a%md; b >>=1; } return p; }
template<typename T>
istream &operator>>(istream &is, vector<T> &v) {
for (auto &it:v)
is >> it;
return is;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &it) {
os << it.first << ' ' << it.second;
return os;
}
template <typename T>
ostream &operator<<(ostream &os, const array<T, 2> &v) {
for (auto &it : v) os << it << ' ';
return os;
}
template<typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
for (auto &it : v)
os << it << ' ';
return os;
}
#ifndef ONLINE_JUDGE
class Timer { chrono::high_resolution_clock::time_point start_t, end_t; public: Timer() { start_t=chrono::high_resolution_clock::now(); } ~Timer() { end_t = chrono::high_resolution_clock::now(); auto duration = chrono::duration_cast<chrono::milliseconds>(end_t-start_t); cerr<<"\nRunTime: "<<duration.count()<<"ms"<<'\n'; } };
void debug() { cerr << '\n'; }
template<typename T, typename... Args>
void debug(T print, Args... args) {
cerr << ' ' << print;
debug(args...);
}
#define deb(...) cerr << "[" << #__VA_ARGS__ << "]" << " --> ", debug(__VA_ARGS__)
#else
#define deb(...) void(0)
#endif
void solve() {
int a, b, x, y;
cin>>a>>b>>x>>y;
if(a==b||a==b+1) {
cout<<x<<'\n';
return;
}
if(a>b) --a;
cout<<x+min(2*x,y)*(abs(a-b))<<'\n';
}
int main() {
#ifndef ONLINE_JUDGE
Timer timer;
#endif
speedup;
//int t; cin>>t; while(t--)
solve();
}
| #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
#define debug(x) cout << #x << " is " << x << endl
#define inc(i, a, b) for (int i = a; i <= b; ++i)
typedef long long ll;
const int INF = 0x3f3f3f3f;
int a, b, x, y;
int main()
{
scanf("%d%d%d%d", &a, &b, &x, &y);
if (a > b) printf("%d\n", min(2 * (a - b - 1) * x + x, (a - b - 1) * y + x));
else printf("%d\n", min(2 * (b - a) * x, (b - a) * y) + x);
return 0;
} |
#include <iostream>
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
using namespace std;
class BIT {
public:
int size_ = 1;
vector<long long> bit;
void init(int sz) {
size_ = sz + 2;
bit.resize(size_ + 2, 0);
}
void add(int pos, long long x) {
pos++;
while (pos <= size_) {
bit[pos] += x;
pos += (pos & -pos);
}
}
long long sum(int pos) {
long long s = 0; pos++;
while (pos >= 1) {
s += bit[pos];
pos -= (pos & -pos);
}
return s;
}
};
long long N, M, Q;
long long T[1 << 19], X[1 << 19], Y[1 << 19];
long long A[1 << 19], B[1 << 19];
vector<long long> Z;
BIT A1, A2;
BIT B1, B2;
void change1(int p1, int p2) {
A1.add(A[p1], -1);
A2.add(A[p1], -Z[A[p1]]);
A[p1] = p2;
A1.add(A[p1], 1);
A2.add(A[p1], Z[A[p1]]);
}
void change2(int p1, int p2) {
B1.add(B[p1], -1);
B2.add(B[p1], -Z[B[p1]]);
B[p1] = p2;
B1.add(B[p1], 1);
B2.add(B[p1], Z[B[p1]]);
}
int main() {
// Input
cin >> N >> M >> Q;
for (int i = 1; i <= Q; i++) {
cin >> T[i] >> X[i] >> Y[i];
Z.push_back(Y[i]);
}
Z.push_back(0);
sort(Z.begin(), Z.end());
Z.erase(unique(Z.begin(), Z.end()), Z.end());
// Lower Bound
for (int i = 1; i <= Q; i++) {
Y[i] = lower_bound(Z.begin(), Z.end(), Y[i]) - Z.begin();
}
// BIT Init
A1.init(Z.size()); A1.add(0, N);
A2.init(Z.size());
B1.init(Z.size()); B1.add(0, M);
B2.init(Z.size());
// Query
long long CurrentAns = 0;
for (int i = 1; i <= Q; i++) {
if (T[i] == 1) {
long long pos1 = A[X[i]];
long long pos2 = Y[i];
long long sa1 = 1LL * Z[pos2] * B1.sum(pos2) - 1LL * Z[pos1] * B1.sum(pos1);
long long sa2 = 1LL * (B2.sum(Z.size() + 1) - B2.sum(pos2)) - 1LL * (B2.sum(Z.size() + 1) - B2.sum(pos1));
CurrentAns += (sa1 + sa2);
change1(X[i], Y[i]);
}
if (T[i] == 2) {
long long pos1 = B[X[i]];
long long pos2 = Y[i];
long long sa1 = 1LL * Z[pos2] * A1.sum(pos2) - 1LL * Z[pos1] * A1.sum(pos1);
long long sa2 = 1LL * (A2.sum(Z.size() + 1) - A2.sum(pos2)) - 1LL * (A2.sum(Z.size() + 1) - A2.sum(pos1));
CurrentAns += (sa1 + sa2);
change2(X[i], Y[i]);
}
cout << CurrentAns << endl;
}
return 0;
} | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
vector<ll> v;
vector<pair<ll,ll> > aj[2005];
bool vis[2005];
ll dist[2005];
priority_queue<pair<ll,ll> ,vector<pair<ll,ll> > ,greater<pair<ll,ll> > > pq;
ll dijkstra(ll x,ll n){
memset(vis,false,sizeof(vis));
ll maxn = 2e9,a,b;
for(int i=1;i<=n;i++)dist[i] = 2e9;
pq.push(make_pair(0,x));
while(pq.size()){
a = pq.top().first;
b = pq.top().second;
pq.pop();
if(vis[b])continue;
vis[b] = true;
for(auto v:aj[b]){
if(v.first==x){
maxn = min(maxn,v.second+a);
// cout<<a<<" "<<b<<" "<<v.first<<" "<<v.second<<" hi\n";
}
if(dist[v.first]>a+v.second){
dist[v.first] = a+v.second;
pq.push(make_pair(dist[v.first],v.first));
}
}
}
return maxn;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
ll n,m,i,a,b,c,j,k,t,q,sum = 0,cnt = 0,maxn = 0,ans = 0,X,Y,Z;
cin>>n>>m;
for(i=0;i<m;i++){
cin>>a>>b>>c;
aj[a].push_back(make_pair(b,c));
}
for(i=1;i<=n;i++){
a = dijkstra(i,n);
if(a==2e9)cout<<"-1\n";
else cout<<a<<"\n";
}
} |
#include <bits/stdc++.h>
using namespace std;
int main(){
int N;
cin>>N;
cout<<N-1<<endl;
} | #include <bits/stdc++.h>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <climits>
#include <set>
#include <map>
#include <utility>
#include <stack>
#include <numeric>
#include <utility>
#include <complex>
#include <chrono>
using namespace std;
using namespace std::chrono;
typedef long long int lli;
typedef long double ld;
typedef vector<lli> vl;
typedef vector<ld> vd;
typedef map<lli,lli> ml;
typedef unordered_map<lli,lli> uml;
typedef pair<lli,lli> pl;
typedef set<lli> sl;
typedef vector<pl> vp;
typedef queue<lli> ql;
typedef priority_queue<lli> pql;
typedef list<lli> ll;
typedef vector<ll> graph;
# define pb push_back
# define mp make_pair
# define f first
# define s second
# define itr iterator
# define MOD 1000000007
# define INF 1000000000000000
# define all(x) x.begin(), x.end()
# define rall(a) a.begin(), a.end(), greater<lli>()
# define ins insert
# define ms(a,x) memset(a, x, sizeof(a))
# define rep(i,n) for(i=0;i<n;i++)
# define wrep(i,a,b) for(i=a;i<b;i++)
# define M 1500001;
/*
void knapsack(lli n, lli m, vl va, vl vb){
lli i, w;
lli k[n+1][m+1];
k[n][m] = -1;
for(i=0;i<=n;i++){
for(w=0;w<=m;w++){
if(i==0 || w==0){
k[i][w]=0;
}
else if(va[i-1]<=w){
k[i][w] = max(k[i-1][w], k[i-1][w-va[i-1]]+ vb[i-1]);
}
else{
k[i][w] = k[i-1][w];
}
}
}
cout<<k[n][m]<<endl;
}
bool comp(pl a, pl b){
return a.f<b.f;
}
bool comp(lli a, lli b){
return abs(a)<abs(b);
}
lli cmb(lli n, lli r){
if(n==r){
return 1;
}
lli i, pdt = 1;
r = max(r,n-r);
for(i=1;i<=n-r;i++){
pdt *= (r+i);
pdt /= i;
}
return pdt;
}
lli per(lli n, lli r){
lli i;
lli pdt = 1;
for(i=1;i<=r;i++){
pdt *= n-r+i;
}
return pdt;
}
// Binomial coeff Table // Combination Table
lli dp[2001][2001];
void foo(){
dp[0][0] = 1;
lli i, j;
for (i = 1; i < 1001; i++) {
dp[i][0] = 1;
for (j = 1; j <= i; j++) {
dp[i][j] = (dp[i-1][j] + dp[i-1][j-1]) % MOD;
}
}
}
//cout << fixed << setprecision(9) << t << endl;
//sieve Table
int sv[M];
vl prm;
void sieve(){
lli i, j;
for(i=2;i<M;i++){
if(sv[i]!=0){
continue;
}
sv[i] = -1;
prm.pb(i);
for(j=2;j*i<M;j++){
sv[i*j] = 1;
}
}
return;
}
lli fexpo(lli a, lli b){
if(a==1) return 1;
if(b==0) return 1;
if(b==1) return a%MOD;
lli ans = fexpo(a,b/2)%MOD;
if(b%2==0) return (ans*ans)%MOD;
else return (((a*ans)%MOD)*ans)%MOD;
}
lli fact[M];
lli inv_fact[M];
void foo(){
lli i;
fact[0] = 1;
for(i=1;i<M;i++){
fact[i] = (i*fact[i-1])%MOD;
}
inv_fact[M-1] = fexpo(fact[M-1],MOD-2);
for(i=M-2;i>=0;i--){
inv_fact[i] = ((i+1)*inv_fact[i+1])%MOD;
}
}
lli cmb(lli n, lli r){
return (((fact[n]*inv_fact[r])%MOD)*(inv_fact[n-r]))%MOD;
}
*/
lli sum = 0;
bool dec(lli n){
while(n>0){
if(n%10==7){
return true;
}
n/= 10;
}
return false;
}
bool octal(lli n){
while(n>0){
if(n%8==7){
return true;
}
n/= 8;
}
return false;
}
void check(lli n){
if(octal(n) || dec(n)){
return;
}
sum++;
}
void solve(){
lli n, i, m=0, q=0, mx=-INF, mn=INF, k=0, j=0, c=0, d=0, t = 0, sm = 0, pdt = 1;
cin >> n;
//lli v[n+1];
for(i=1;i<=n;i++){
check(i);
}
cout<<sum<<endl;
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
//freopen("input.txt", "rt", stdin);
//freopen("output.txt", "wt", stdout);
//cin >> t;
t = 1;
while(t--){
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define NMAX 200005
int n;
string s, x;
set <int> dp[NMAX];
int main() {
cin>>n;
cin>>s>>x;
dp[n].insert(0);
for(int i = n-1 ; i >= 0; i--){
if(x[i]=='T'){
for(int j=0;j<7;j++){
int p = (10*j)%7;
int q = ((10*j)+(s[i]-'0'))%7;
if(dp[i+1].count(p)) {
dp[i].insert(j);
//cout<<i<<": insert "<<j<<'\n';
}
if(dp[i+1].count(q)) {
dp[i].insert(j);
//cout<<i<<": insert "<<j<<'\n';
}
}
}
else{
for(int j=0;j<7;j++){
int p = (10*j)%7;
int q = ((10*j)+(s[i]-'0'))%7;
if(dp[i+1].count(p) && dp[i+1].count(q)) {
dp[i].insert(j);
//cout<<i<<": insert "<<j<<'\n';
}
}
}
}
if(dp[0].count(0)) cout<<"Takahashi";
else cout<<"Aoki";
}
| #include <bits/stdc++.h>
using namespace std;
void solveCase() {
string s1, s2, s3;
cin >> s1 >> s2 >> s3;
vector<bool> visit(26);
for (auto& c : s1) visit[c - 'a'] = 1;
for (auto& c : s2) visit[c - 'a'] = 1;
for (auto& c : s3) visit[c - 'a'] = 1;
vector<int> can;
for (int k = 0; k < 26; ++k) {
if (visit[k]) can.push_back(k);
}
int SZ = can.size();
if (SZ > 10) {
cout << "UNSOLVABLE" << '\n';
return;
}
vector<int> p(10), v(26);
int64_t x, y, z;
for (int i = 0; i < 10; ++i) p[i] = i;
bool found = false;
do {
for (int i = 0; i < SZ; ++i) v[can[i]] = p[i];
x = 0, y = 0, z = 0;
if (v[s1[0] - 'a'] == 0) continue;
if (v[s2[0] - 'a'] == 0) continue;
if (v[s3[0] - 'a'] == 0) continue;
for (auto& c : s1) x = x * 10 + v[c - 'a'];
for (auto& c : s2) y = y * 10 + v[c - 'a'];
for (auto& c : s3) z = z * 10 + v[c - 'a'];
if (x && y && z && x + y == z) {
found = true;
break;
}
} while (next_permutation(p.begin(), p.end()));
if (!found) {
cout << "UNSOLVABLE" << '\n';
} else {
cout << x << '\n';
cout << y << '\n';
cout << z << '\n';
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T = 1;
// cin >> T;
for (int ti = 1; ti <= T; ++ti) {
// cout << "Case #" << ti << ": ";
solveCase();
}
return 0;
} |
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <iostream>
#include <vector>
#include <unordered_set>
#include <unordered_map>
#include <memory>
#include <list>
#include <deque>
#include <queue>
#include <iomanip>
using namespace std;
#define REP(i,n) for (decltype(n) i = 0; i < n; i++)
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>;
template<typename T>
vector<T> read_args()
{
T N;
cin >> N;
vector<T> v(N);
for (auto& e : v) {
cin >> e;
}
return v;
}
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 answer(bool b)
{
cout << (b ? "Yes" : "No") << endl;
}
typedef unordered_map<int, int> Cluster;
vector<Cluster*> clusters;
ivec c;
void merge2(Cluster& a, Cluster& b)
{
for (auto&e : b) {
if (a.find(e.first) != a.end()) {
a[e.first] += e.second;
} else {
a[e.first] = e.second;
}
}
}
void dump(Cluster* cluster)
{
return;
for (auto &e : *cluster) {
cout << "[" << e.first << "]" << e.second << endl;;
}
}
class UnionFind {
public:
UnionFind(int n)
{
parent.assign(n, 0);
for (int i = 0; i < n; i++)
parent[i] = i;
rank.assign(n, 0);
}
bool unite(int a, int b)
{
a = root(a);
b = root(b);
if (a == b)
return false;
if (rank[a] > rank[b]) {
parent[b] = a;
merge(a, b);
} else {
parent[a] = b;
merge(b, a);
if (rank[a] == rank[b])
rank[b]++;
}
return true;
}
void merge(int a, int b)
{
// cout << "merge " << a << "," << b << endl;
if (!clusters[a]) {
clusters[a] = new Cluster;
(*clusters[a])[c[a]] = 1;
}
if (!clusters[b]) {
clusters[b] = new Cluster;
(*clusters[b])[c[b]] = 1;
}
merge2(*clusters[a], *clusters[b]);
dump(clusters[a]);
delete clusters[b];
clusters[b] = nullptr;
}
bool same(int a, int b) { return root(a) == root(b); }
bool isRoot(int a) { return parent[a] == a; }
int root(int i) { return parent[i] == i ? i : parent[i] = root(parent[i]); }
private:
std::vector<int> parent;
std::vector<int> rank;
};
int main()
{
int n,q;
cin >> n >> q;
UnionFind uf(n);
c.assign(n, 0);
REP(i,n) {
int z;
cin >> z;
c[i] = z;
}
clusters.assign(n, 0);
REP(z, q) {
int p,x,y;
cin >> p >> x >> y;
// cout << p << "," << x << "," << y << endl;
x--;
if (p == 1) {
y--;
uf.unite(x,y);
} else {
// cout << x << "," << y << endl;
x = uf.root(x);
// cout << x << "," << x << endl;
auto p = clusters[x];
if (!p) {
// cout << "no cluster" << endl;
if (c[x] == y)
cout << 1 << endl;
else
cout << 0 << endl;
} else {
dump(p);
auto it = p->find(y);
if (it == p->end()) {
cout << 0 << endl;
} else {
cout << it->second << endl;
}
}
}
}
return 0;
}
| //To debug : g++ -g file.cpp -o code
//to flush output : fflush(stdout) or cout.flush()
//cout<<setprecision(p)<<fixed<<var
//use 1LL<<i to for 64 bit shifting , (ll)2 because by default 2 is ll
//take care of precedence rule of operators
//do not forget to change the sizes of arrays and value of contants and other things after debugging
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,n) for(i=a;i<n;++i)
#define irep(i,n,a) for(i=n;i>a;--i)
#define mod 1000000007
#define pb push_back
#define big 9223372036854775807
#define big1 LONG_MAX
#define big2 ll_MAX
#define big3 1000000000
#define sma1 LONG_MIN
#define sma2 ll_MIN
#define sma3 -1000000000
#define mp make_pair
#define dub double
#define ivec vector<ll>
#define lvec vector<long long>
#define cvec vector<char>
#define svec vector<string>
#define mt make_tuple
#define MOD 998244353
#define ld long double
#define pi acos(-1.0)
#define SZ(x) (ll)(x.size())
//comment the below if not required
/*
#define ss second.second
#define ff first.first
#define f first
#define s second
#define sf second.first
#define fs first.second
*/
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
//cout<<"Case #"<<c<<": "<<ans<<"\n" ;
const int N = 2e5+1;
int n,q,c,x,y,cl,Q,A,B;
vector<int> v[N];
map<int,int> m[N];
int a[N];
int main()
{
int i,i1,j1;
cin>>n>>q;
for(i=1;i<=n;++i)
{
cin>>cl;
v[i].pb(i);
++m[i][cl];
a[i]=i;
}
for(i=1;i<=q;++i)
{
cin>>Q>>A>>B;
if(Q==1)
{
i1=a[A],j1=a[B];
if(i1==j1)
continue;
if(SZ(v[j1])>SZ(v[i1]))
swap(i1,j1);
//put j1 into i1
for(auto u : v[j1])
{
a[u]=i1;
v[i1].pb(u);
}
v[j1].clear();
for(auto i = m[j1].begin();i!=m[j1].end();++i)
m[i1][i->first]+=i->second;
m[j1].clear();
}
else
{
cout<<m[a[A]][B]<<"\n";
}
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int mod = 1e9 + 7;
#define ff first
#define ss second
#define pb push_back
#define all(v) v.begin(), v.end()
const ll inf = 2e18;
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
int add(int a, int b) {
int res = (a + b) % mod;
if (res < 0)
res += mod;
return res;
}
int mult(int a, int b) {
int res = (a * 1LL * b) % mod;
if (res < mod)
res += mod;
return res;
}
int power(int a, int b) {
int res = 1;
while (b) {
if ((b % 2) == 1)
res = mult(res, a);
a = mult(a, a);
b /= 2;
}
return res;
}
template <typename A, typename B> string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string &s) { return '"' + s + '"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(char c) { return to_string(string(1, c)); }
string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template <size_t N> string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++) {
res += static_cast<char>('0' + v[i]);
}
return res;
}
template <typename A> string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B> string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void dbg() { cout << endl; }
template <typename Head, typename... Tail> void dbg(Head H, Tail... T) {
cout << " " << to_string(H);
dbg(T...);
}
#ifdef LOCAL
#define debug(...) cout << "[" << #__VA_ARGS__ << "]:", dbg(__VA_ARGS__)
#else
#define debug(...)
#endif
void solve() {
ll n;
cin >> n;
if (n <= 2) {
cout << "1\n";
return;
}
ll low = 1;
ll high = 2e9 + 2;
ll ans;
while (low <= high) {
ll mid = (low + high) / 2;
if (mid * (mid + 1) <= 2 * (n + 1)) {
ans = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
cout << (n - ans + 1) << "\n";
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cout << fixed << setprecision(10);
int t = 1;
// cin >> t;
for (int i = 1; i <= t; i++) {
// cout<<"Case "<<i<<": ";
solve();
}
} | #include <stdio.h>
#include <iostream>
#include <cmath>
#include <vector>
#include <numeric>
#include <string>
int main() {
int k;
std::cin >> k;
std::vector<int> v(10, k);
std::vector<double> tv(10, 0);
std::vector<double> av(10, 0);
int ta = 0;
int ao = 0;
std::string s;
std::cin >> s;
for (int c = 0; c < 4; c++) {
int f;
f = std::stoi(s.substr(c, 1));
tv[f] += 1;
v[f] -= 1;
}
for (int c = 1; c < 10; c++) {
ta += c*pow(10, tv[c]);
}
std::cin >> s;
for (int c = 0; c < 4; c++) {
int f;
f = std::stoi(s.substr(c, 1));
av[f] += 1;
v[f] -= 1;
}
for (int c = 1; c < 10; c++) {
ao += c*pow(10, av[c]);
}
double ans = 0.;
for (int c = 1; c < 10; c++) {
for (int b = 1; b < 10; b++) {
if (c == b) {
if (v[c] > 1) {
if (ao + b * pow(10, av[b] + 1) - b * pow(10, av[b]) < ta + c * pow(10, tv[c] + 1) - c * pow(10, tv[c])) {
ans += static_cast<double>(v[c]) / static_cast<double>(std::accumulate(v.begin() + 1, v.end(), 0)) * static_cast<double>(v[c] - 1) / static_cast<double>(std::accumulate(v.begin() + 1, v.end(), 0) - 1);
}
}
}
else {
if (v[c] > 0 && v[b] > 0) {
if (ao + b * pow(10, av[b] + 1) - b * pow(10, av[b]) < ta + c * pow(10, tv[c] + 1) - c * pow(10, tv[c])) {
ans += static_cast<double>(v[c]) / static_cast<double>(std::accumulate(v.begin() + 1, v.end(), 0)) * static_cast<double>(v[b]) / static_cast<double>(std::accumulate(v.begin() + 1, v.end(), 0) - 1);
}
}
}
}
}
std::cout << ans << std::endl;
} |
#include <algorithm>
#include <iostream>
using namespace std;
bool ans;
int A[5];
int main(){
cin >> A[1] >> A[2] >> A[3];
sort(A + 1,A + 1 + 3);
if (A[3] - A[2] == A[2] - A[1]) cout << "Yes\n";
else cout << "No\n";
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long int
#define pb push_back
int main()
{ ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
ll t=1;
// cin>>t;
while(t--){
ll i;
ll a,b,c,d;
cin>>a>>b>>c>>d;
cout<<b-c;
}
}
|
// g++ -std=c++11 a.cpp
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<unordered_map>
#include<utility>
#include<cmath>
#include<random>
#include<cstring>
#include<queue>
#include<stack>
#include<bitset>
#include<cstdio>
#include<sstream>
#include<random>
#include<iomanip>
#include<assert.h>
#include<typeinfo>
#include<list>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define FOR(i,a) for(auto i:a)
#define pb push_back
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
#define show1d(v) {rep(_,v.size())cout<<" "<<v[_];cout<<endl;}
#define show2d(v) {rep(__,v.size())show1d(v[__]);}
using namespace std;
//kaewasuretyuui
typedef long long ll;
#define int ll
typedef int Def;
typedef pair<Def,Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def,pii> pip;
typedef vector<pip>vip;
#define mt make_tuple
typedef tuple<double,int,int> tp;
typedef vector<tp> vt;
typedef vector<vt>vvt;
template<typename A,typename B>bool cmin(A &a,const B &b){return a>b?(a=b,true):false;}
template<typename A,typename B>bool cmax(A &a,const B &b){return a<b?(a=b,true):false;}
const double PI=acos(-1);
const long double EPS=1e-9;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9+10;
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
#define yes cout<<"Yes\n"
#define no cout<<"No\n"
typedef ll Def;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin>>n;
vvi in(n,vi(5));
rep(i,n)rep(j,5)cin>>in[i][j];
int l=0,r=1000000010;
while(r-l>1){
int h=(l+r)/2;
vi w(32);
w[0]=1;
vi b(n);
rep(i,n){
int a=0;
rep(j,5)if(h<=in[i][j])a|=1<<j;
w[a]++;
b[i]=a;
}
bool H=false;
rep(i,n)rep(j,i){
int a=b[i]|b[j];
a=31-a;
if(w[a])H=true;
}
if(H)l=h;
else r=h;
}
cout<<l<<endl;
}
| #include <bits/stdc++.h>
using namespace std;
const int maxn = 3000 + 10;
const int s = (1 << 5) - 1;
const int N = 5;
int a[maxn][5];
int n;
int f[s + 10], g[s + 10], h[10][s + 10], fhat[10][s + 10], ghat[10][s + 10], fog[s + 10];
bool check(int x)
{
memset(f, 0, sizeof f);
memset(g, 0, sizeof g);
memset(h, 0, sizeof h);
memset(fhat, 0, sizeof fhat);
memset(ghat, 0, sizeof ghat);
for(int i = 1; i <= n; ++i)
{
int cur = 0;
for(int j = 0; j < 5; ++j)
if(a[i][j] >= x)
cur |= 1 << j;
f[cur] = 1;
}
for(int i = s; i; --i)
for(int j = 0; j < 5; ++j)
if(i & (1 << j))
f[i ^ (1 << j)] |= f[i];
for(int i = 0; i <= s; ++i) g[i] = f[i];
for(int mask = 0; mask < (1 << N); mask++) {
fhat[__builtin_popcount(mask)][mask] = f[mask];
ghat[__builtin_popcount(mask)][mask] = g[mask];
}
// Apply zeta transform on fhat[][] and ghat[][]
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
for(int mask = 0; mask < (1 << N); mask++) {
if((mask & (1 << j)) != 0) {
fhat[i][mask] += fhat[i][mask ^ (1 << j)];
ghat[i][mask] += ghat[i][mask ^ (1 << j)];
}
}
}
}
// Do the convolution and store into h[][] = {0}
for(int mask = 0; mask < (1 << N); mask++) {
for(int i = 0; i < N; i++) {
for(int j = 0; j <= i; j++) {
h[i][mask] += fhat[j][mask] * ghat[i - j][mask];
}
}
}
// Apply inverse SOS dp on h[][]
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
for(int mask = 0; mask < (1 << N); mask++) {
if((mask & (1 << j)) != 0) {
h[i][mask] -= h[i][mask ^ (1 << j)];
}
}
}
}
for(int mask = 0; mask < (1 << N); mask++) fog[mask] = h[__builtin_popcount(mask)][mask];
for(int i = 0; i <= s; ++i)
if(f[i] && fog[s - i])
return 1;
return 0;
}
int main()
{
scanf("%d", &n);
for(int i = 1; i <= n; ++i)
for(int j = 0; j < 5; ++j)
scanf("%d", &a[i][j]);
int l = 1, r = 1e9;
while(l <= r)
{
int mid = l + r >> 1;
if(check(mid)) l = mid + 1;
else r = mid - 1;
}
printf("%d\n", r);
return 0;
} |
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
static const int IINF = (1LL << 31) - 1;
int main() {
ll n, x, a;
cin >> n >> x;
vector<ll> ans;
rep(i, n) {
cin >> a;
if(a != x) ans.push_back(a);
}
rep(i, ans.size()) {
if(i) cout << " ";
cout << ans[i];
}
cout << endl;
} | #include <iostream>
using namespace std;
int n;
long long a[300000],b[300000];
long long maxa=-9999,maxx=-9999;
int main()
{
int i,j,k;
cin>>n;
for(i=1;i<=n;i++)
{
cin>>a[i];
}
for(i=1;i<=n;i++)
{
maxa=max(a[i],maxa);
cin>>b[i];
maxx=max(maxx,maxa*b[i]);
cout<<maxx<<endl;
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
int n;
string s;
int main(){
scanf("%d",&n),cin>>s,s=" "+s;
if(s[1]!=s[n]){puts("1");return 0;}
for(int i=2;i<n-1;++i)if(s[i]!=s[1]&&s[i+1]!=s[n]){puts("2");return 0;}
puts("-1");
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second
#define Fi(a,n) for(int i = a; i < n; i++)
#define Fd(n,a) for(int i = n; i >= a; i--)
#define all(a) a.begin(), a.end()
#define pb push_back
void solve(){
int n;
cin >> n;
string s;
cin >> s;
if (s[0] != s[n-1]){
cout << 1;
return;
}
if (n <= 3)
cout << -1;
else{
vector<int> a(n , 0);
for (int i = 1; i<n; i++){
if (s[i] != s[0])
a[i] = a[i-1] + 1;
}
if (a[n-1]!=0)
cout << 2;
else{
int m = 0, c = 0;
for (int i = 0; i<n; i++){
if (a[i] == 1)
c++;
m = max(m, a[i]);
}
if (m > 1)
cout << 2;
else{
cout << -1;
}
}
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
}
|
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0);
#define endl "\n"
#define int long long
int32_t main()
{
IOS;
int a, b, c;
cin >> a >> b >> c;
if (a * a + b * b < c * c) {
cout << "Yes";
} else {
cout << "No";
}
return 0;
} | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n';
const ll nmax = 1000000007;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll a, b, c;
cin >> a >> b >> c;
ll ans = 0;
if(a == b){
ans = c;
}else if(b == c){
ans = a;
}else if(c == a){
ans = b;
}
cout << ans << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int tsc=1;
//cin>>tsc;
while(tsc--){
int t,n;
cin>>t>>n;
int a=(n*100+t-1)/(t);
cout<<a+n-1;
cout<<endl;}}
| #include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
// using mint = modint1000000007;
// using mint2 = modint998244353;
typedef int64_t Int;
#define all(x) (x).begin(), (x).end()
const double EPS = 1e-10;
const Int INF = 1e18;
const int inf = 1e9;
const Int mod = 1e9+7;
//const Int mod = 998244353;
typedef struct {
int64_t to, weight;
} edge;
Int dx[] = {0, 1, 0, -1, -1, 1, -1, 1};
Int dy[] = {1, 0, -1, 0, -1, -1, 1, 1};
template<class T>
istream &operator>>(istream &is, vector<T> &v) {
for (auto &e : v) {
is >> e;
}
return is;
}
bool print_space_enable = false;
void print() {
std::cout << '\n';
print_space_enable = false;
}
template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
if (print_space_enable) std::cout << " ";
std::cout << fixed << setprecision(15) << head;
print_space_enable = true;
print(std::forward<Tail>(tail)...);
}
template<typename T>
void print(vector<T> v) {
for (size_t i = 0; i < v.size(); i++) {
if (i > 0) std::cout << " ";
std::cout << v[i];
}
std::cout << '\n';
}
template<class T>
vector<T> make_vec(size_t n, T val) {
return vector<T>(n, val);
}
template<class... Ts>
auto make_vec(size_t n, Ts... ts) {
return vector<decltype(make_vec(ts...))>(n, make_vec(ts...));
}
bool is_overflow(Int a, Int b) {
return a > INF / b;
}
void solve() {
Int t, n;
cin >> t >> n;
n *= 100;
double a = (n - 1) / t;
double b = a * (100.0 + (double)t) / 100.0;
Int res = 1 + floor(b);
print(res);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
//Int _t; cin >> _t; while (_t--) solve();
return 0;
}
|
#import<bits/stdc++.h>
#define endl '\n'
using namespace std;
struct H{int i,x,y,r;};
int n,x[204],y[204],a[10005][10005];
H e[204];
int C(H a,H b){return a.y<b.y||a.y==b.y&&a.x<b.x;}
int D(H a,H b){return a.i<b.i;}
main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=0;i<n;i++)
{
e[i].i=i;
cin>>e[i].x>>e[i].y>>e[i].r;
}
sort(e,e+n,C);
for(int k=0;k<n;k++)
{
int v=e[k].x,w=e[k].y;
for(;w>=0&&!a[v][w];)w--;
for(w++;v>=0&&!a[v][w];)v--;
v++;
x[e[k].i]=v;
y[e[k].i]=w;
for(int i=e[k].x;i>=v;i--)
{
for(int j=e[k].y;j>=w;j--)a[i][j]=1;
}
}
sort(e,e+n,D);
for(int i=0;i<n;i++)cout<<x[i]<<' '<<y[i]<<' '<<e[i].x+1<<' '<<e[i].y+1<<endl;
} | #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#ifdef local
#define safe cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
#define pary(a...) danb(#a, a)
#define debug(a...) qqbx(#a, a)
template <typename ...T> void qqbx(const char *s, T ...a) {
int cnt = sizeof...(T);
((std::cerr << "\033[1;32m(" << s << ") = (") , ... , (std::cerr << a << (--cnt ? ", " : ")\033[0m\n")));
}
template <typename T> void danb(const char *s, T L, T R) {
std::cerr << "\033[1;32m[ " << s << " ] = [ ";
for (int f = 0; L != R; ++L)
std::cerr << (f++ ? ", " : "") << *L;
std::cerr << " ]\033[0m\n";
}
#else
#define debug(...) ((void)0)
#define safe ((void)0)
#define pary(...) ((void)0)
#endif // local
#define all(v) begin(v),end(v)
#define pb emplace_back
#define get_pos(u,x) int(lower_bound(all(u),x)-begin(u))
using namespace std;
using ll = int_fast64_t;
const int inf = 1e9;
const int mod = 1000000007;
const ll INF = 1e18;
const int maxn = 200025;
struct Fenwick {
ll b[maxn];
void add(int p, int d) {
for (++p; p < maxn; p += p & -p)
b[p] += d;
}
ll query(int p) {
ll r = 0;
for (++p; p > 0; p -= p & -p)
r += b[p];
return r;
}
} cnt_a, cnt_b, sum_a, sum_b;
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n, m, q;
cin >> n >> m >> q;
vector<tuple<int,int,int>> Qs(q);
vector<int> u;
u.pb(0);
for (auto &[t, x, y]: Qs) cin >> t >> x >> y, u.pb(y), --x;
sort(all(u)), u.erase(unique(all(u)), u.end());
vector<int> a(n), b(m);
auto calc = [&](int val, Fenwick &cnt, Fenwick &sum) {
int pos = get_pos(u, val);
return val * cnt.query(pos - 1) + (sum.query(maxn - 2) - sum.query(pos - 1));
};
ll ans = 0;
cnt_a.add(get_pos(u, 0), n);
cnt_b.add(get_pos(u, 0), m);
for (auto [t, x, y]: Qs) {
if (t == 1) {
cnt_a.add(get_pos(u, a[x]), -1);
sum_a.add(get_pos(u, a[x]), -a[x]);
ans -= calc(a[x], cnt_b, sum_b);
a[x] = y;
ans += calc(a[x], cnt_b, sum_b);
cnt_a.add(get_pos(u, a[x]), 1);
sum_a.add(get_pos(u, a[x]), a[x]);
} else {
cnt_b.add(get_pos(u, b[x]), -1);
sum_b.add(get_pos(u, b[x]), -b[x]);
ans -= calc(b[x], cnt_a, sum_a);
b[x] = y;
ans += calc(b[x], cnt_a, sum_a);
cnt_b.add(get_pos(u, b[x]), 1);
sum_b.add(get_pos(u, b[x]), b[x]);
}
cout << ans << '\n';
}
}
|
#include<bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template<typename T>
// using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long
#define rep(i,j,n) for(ll i=j;i<n;i++)
#define _rep(i,n) for(ll i=n-1;i>=0;i--)
#define vec vector<ll>
#define pb push_back
#define eb emplace_back
#define pairs pair<ll,ll>
#define f first
#define s second
#define all(v) v.begin(),v.end()
#define srt(v) sort(v.begin(),v.end())
#define mem(a,b) memset(a,b,sizeof(a))
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define SZ(a) (int)a.size()
const int N = 21;
ll n, a[N];
ll ans = 1e18;
vec v;
void solve(ll i, ll val, ll xors) {
if(i == n) {
ll temp = 0;
ans = min(ans, xors^val);
return;
}
solve(i+1, val|a[i], xors);
solve(i+1, a[i], xors^val);
}
int Main()
{
cin>>n;
rep(i,0,n) {
cin>>a[i];
}
solve(0, 0, 0);
cout<<ans;
return 0;
}
int main()
{
int queries=1;
// cin>>queries;
while(queries--)
{
Main();
}
}
| #include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <unordered_set>
#define ll long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define P pair<ll, ll>
using namespace std;
int n;
vector<vector<int> > to;
vector<int> c, ans;
multiset<int> ms;
void dfs(int now = 0, int p = -1){
for(auto& next: to[now]){
if(next == p) continue;
if(ms.find(c[next]) == ms.end()) ans.push_back(next);
ms.insert(c[next]);
dfs(next, now);
ms.erase(ms.find(c[next]));
}
}
int main(){
cin >> n;
c.resize(n);
to.resize(n);
rep(i, n) cin >> c[i];
rep(i, n-1){
int a, b; cin >> a >> b;
a--; b--;
to[a].push_back(b);
to[b].push_back(a);
}
ms.insert(c[0]);
ans.push_back(0);
dfs();
sort(ans.begin(), ans.end());
for(auto& e: ans){
cout << e+1 << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#define f(i,a,b) for(ll i = a; i < (ll) b ; i++ )
#define af(i,a,b) for(ll i = a; i >= (ll) b ; i--)
#define rep(i,a,b,k) for(ll i = a; i < (ll) b ; i+= k )
#define arep(i,a,b,k) for(ll i = a; i >= (ll) b ; i-= k)
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define all(a) a.begin(), a.end()
#define sz(a) (ll) a.size()
#define sor(a) sort( a.begin(), a.end() )
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL)
#define ller ios::sync_with_stdio(false);cin.tie(0)
// policy-based
using namespace std;
typedef long long ll; // ll or ll
typedef long double ld;
typedef pair<ll,ll> ii ;
typedef vector<ll> vi ;
typedef vector<ii> vii ;
const ll MAX = 2e6 + 20;
const ll mod = 1e9 + 7;
const ll inf = 1e13;
ll d[MAX],d1[MAX],d2[MAX];
int main(){
fastio;
f(i,1,MAX){
rep(j,i,MAX,i) d[j]++;
}
f(i,1,MAX){
rep(j,i,MAX,i) d1[j]+=d[i];
}
f(i,1,MAX) d1[i] += d1[i-1];
ll k;
cin >> k;
cout << d1[k] << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main()
{
string s,str;
cin>>s;
int n=s.size(),cnt=0,zero=0;
str=s;
reverse(str.begin(),str.end());
vector<char>v;
for(int i=n-1; i>=0; i--)
{
if(s[i]=='0')
{
zero++;
v.push_back(s[i]);
}
else
break;
}
for(int i=0; i<n; i++)
{
v.push_back(s[i]);
}
for(int i=0, j=v.size()-1; j>=i; i++,j--)
{
if(v[i]==v[j])
{
cnt++;
}
}
if((s==str) || (zero>0 && (v.size()+1)/2==cnt))
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
return 0;
} |
//้ช่ฑ้ฃ้ฃๅ้ขจๅฏๅฏ
//ๅคฉๅฐไธ็่ผ่ซ
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define lb lower_bound
#define up upper_bound
#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//change less to less_equal for non distinct pbds, but erase will bug
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
int n,m;
int u[2005];
int v[2005];
char c[2005];
vector<ii> al[1005][1005];
bool adj[1005][1005];
int w[1005][1005];
queue<ii> q;
int ans=1e9;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin.exceptions(ios::badbit | ios::failbit);
cin>>n>>m;
rep(x,0,m) cin>>u[x]>>v[x]>>c[x],adj[u[x]][v[x]]=adj[v[x]][u[x]]=true;
rep(x,m,2*m) u[x]=v[x-m],v[x]=u[x-m],c[x]=c[x-m];
rep(x,0,2*m) rep(y,0,2*m) if (c[x]==c[y]){
al[u[x]][u[y]].pub(ii(v[x],v[y]));
}
memset(w,63,sizeof(w));
w[1][n]=0;
q.push(ii(1,n));
int a,b;
while (!q.empty()){
tie(a,b)=q.front();
q.pop();
if (a==b) ans=min(ans,w[a][b]*2);
else if (adj[a][b]) ans=min(ans,w[a][b]*2+1);
int c,d;
for (auto &it:al[a][b]){
tie(c,d)=it;
if (w[c][d]>1e9){
w[c][d]=w[a][b]+1;
//cout<<"from: "<<a<<"-"<<b<<" to: "<<c<<"-"<<d<<endl;
q.push(ii(c,d));
}
}
}
if (ans==1e9) cout<<"-1"<<endl;
else cout<<ans<<endl;
}
| #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define MT make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
#define RT return
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
const int INF = INT_MAX / 3;
int dp[1 << 17][17];
void solve() {
int N;
cin >> N;
vi X(N), Y(N), Z(N);
rep(i, N) {
cin >> X[i] >> Y[i] >> Z[i];
}
rep(S, 1 << N)rep(i, N)dp[S][i] = INF;
dp[1][0] = 0;
rep(S, 1 << N) {
rep(i, N) {
rep(j, N) {
int d = abs(X[i] - X[j]) + abs(Y[i] - Y[j]) + max(0, Z[j] - Z[i]);
smin(dp[S | 1 << j][j], dp[S][i] + d);
}
}
}
cout << dp[(1 << N) - 1][0] << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(15);
solve();
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int q, n;
map<int, int> cnt;
void increase(map<int, int>& cnt, int x) {
if (cnt.find(x) == cnt.end())
cnt.insert(make_pair(x, 0));
cnt[x]++;
}
int main() {
cin >> q;
while (q--) {
cnt.clear();
cin >> n;
for (int i = 1; i <= n; i++) {
int x;
cin >> x;
increase(cnt, x);
}
if (n % 2 == 1)
cout << "Second\n";
else {
bool pare = true;
for (pair<int, int> c : cnt)
if (c.second % 2 == 1)
pare = false;
if (pare)
cout << "Second\n";
else
cout << "First\n";
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 100005;
template <typename T> inline void read(T &AKGK) {
T x = 0, flag = 1;
char ch = getchar();
while(!isdigit(ch)) {
if (ch == '-') flag = -1;
ch = getchar();
}
while(isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
AKGK = flag * x;
}
int n, a[MAXN];
void solve() {
read(n);
for (int i = 1; i <= n; ++i) {
read(a[i]);
}
if (n & 1) {
puts("Second");
} else {
sort(a + 1, a + n + 1);
for (int i = 1; i <= n; i += 2) {
if (a[i] ^ a[i + 1]) {
puts("First"); return;
}
}
puts("Second");
}
}
int main() {
int T; read(T);
while (T--) {
solve();
}
return 0;
}
|
// Problem: A - Tax Included Price
// Contest: AtCoder - AtCoder Regular Contest 118
// URL: https://atcoder.jp/contests/arc118/tasks/arc118_a
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// Author: abhidot
#include <bits/stdc++.h>
#define int long long
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define mod 1000000007 //998244353
#define lld long double
#define pii pair<int, int>
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define rep(i,x,y) for(int i=x; i<y; i++)
#define fill(a,b) memset(a, b, sizeof(a))
#define vi vector<int>
#define setbits(x) __builtin_popcountll(x)
#define w(x) int x; cin>>x; while(x--)
using namespace std;
const long long N=200005, INF=2000000000000000000;
/*------------- Modular exponentiation -------------*/
int power(int a, int b, int p)
{
if(a==0)
return 0;
int res=1;
a%=p;
while(b>0)
{
if(b&1)
res=(res*a)%p;
b>>=1;
a=(a*a)%p;
}
return res;
}
/*------------- Sieve --------------*/
vector <int> prime;
bool is_composite[N];
void sieve (int n) {
fill (is_composite, false);
for (int i = 2; i < n; ++i) {
if (!is_composite[i]) prime.push_back (i);
for (int j = 0; j < prime.size () && i * prime[j] < n; ++j) {
is_composite[i * prime[j]] = true;
if (i % prime[j] == 0) break;
}
}
}
/*------------ Utitlity function ---------------*/
void print(bool n){
if(n){
cout<<"YES";
}else{
cout<<"NO";
}
}
int32_t main()
{
IOS;
int t,n;
cin>>t>>n;
n--;
set<int> s;
for(int i=1;i<=100+t;i++){
s.insert(i);
}
for(int i=1;i<=100;i++){
int val = (100+t)*i/100;
if(s.find(val)!=s.end()) s.erase(val);
}
// cout<<s.size()<<"\n";
int q = n/s.size();
n%=s.size();
int r=0;
for(auto i:s){
// cout<<i<<" ";
if(n<0) break;
if(n==0){
r = i;
break;
}else{
n--;
}
}
cout<<(100+t)*q + r<<"\n";
}
// Written by abhidot | #include<iostream>
#include<vector>
using namespace std;
long long T, N;
void read() {
cin >> T >> N;
}
void work() {
vector<pair<int, int> > t;
for (int i = 0; i < 100; ++i) {
int a = i * (100 + T) / 100;
int b = (i + 1) * (100 + T) / 100;
if (a + 1 < b) {
t.push_back(make_pair(i, a));
}
}
long long v = 100 * ((N - 1) / T) + t[(N - 1) % T].first + 1;
cout << v * (100 + T) / 100 - 1 << endl;
}
int main() {
read();
work();
return 0;
}
|
#include <bits/stdc++.h>
#define clr(x) memset(x,0,sizeof(x))
#define For(i,a,b) for (int i=(a);i<=(b);i++)
#define Fod(i,b,a) for (int i=(b);i>=(a);i--)
#define fi first
#define se second
#define kill _z_kill
#define y0 _z_y0
#define y1 _z_y1
#define x0 _z_x0
#define x1 _z_x1
#define pb push_back
#define mp(x,y) make_pair(x,y)
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
typedef unsigned uint;
typedef long double LD;
typedef vector <int> vi;
typedef pair <int,int> pii;
void enable_comma(){}
string tostring(char c){
string s="";
s+=c;
return s;
}
string tostring(string s){
return "\""+s+"\"";
}
string tostring(const char *c){
return tostring((string)c);
}
string tostring(long long x){
if (x<0)
return "-"+tostring(-x);
if (x>9)
return tostring(x/10)+tostring(char('0'+x%10));
else
return tostring(char('0'+x));
}
string tostring(int x){
return tostring((long long)x);
}
string tostring(unsigned long long x){
if (x>9)
return tostring((long long)(x/10))+tostring(char('0'+x%10));
else
return tostring(char('0'+x));
}
string tostring(unsigned x){
return tostring((long long)x);
}
string tostring(double x){
static char res[114];
sprintf(res,"%lf",x);
string s=tostring(res);
return s.substr(1,(int)s.size()-2);
}
string tostring(long double x){
return tostring((double)x);
}
template <class A,class B> string tostring(pair <A,B> p){
return "("+tostring(p.fi)+","+tostring(p.se)+")";
}
template <class T> string tostring(T v){
string res="";
for (auto p : v)
res+=(res.size()?",":"{")+tostring(p);
return res.size()?res+"}":"{}";
}
template <class A> string tostring(A* a,int L,int R){
return tostring(vector <A>(a+L,a+R+1));
};
template <class A> string tostring(A a,int L,int R){
return tostring(a.data(),L,R);
}
string tostrings(){
return "";
}
template <typename Head,typename... Tail>
string tostrings(Head H,Tail... T){
return tostring(H)+" "+tostrings(T...);
}
#define User_Time ((double)clock()/CLOCKS_PER_SEC)
#ifdef zzd
#define outval(x) cerr<<#x" = "<<tostring(x)<<endl
#define outvals(...) cerr<<"["<<#__VA_ARGS__<<"]: "<<\
tostrings(__VA_ARGS__)<<endl
#define outtag(x) cerr<<"--------------"#x"---------------"<<endl
#define outsign(x) cerr<<"<"#x">"<<endl
#define outarr(a,L,R) cerr<<#a"["<<(L)<<".."<<(R)<<"] = "<<\
tostring(a,L,R)<<endl
#else
#define outval(x) enable_comma()
#define outvals(...) enable_comma()
#define outtag(x) enable_comma()
#define outsign(x) enable_comma()
#define outarr(a,L,R) enable_comma()
#endif
#ifdef ONLINE_JUDGE
#ifdef assert
#undef assert
#endif
#define assert(x) (!(x)?\
cout<<"Assertion failed!"<<endl<<\
"function: "<<__FUNCTION__<<endl<<\
"line: "<<__LINE__<<endl<<\
"expression: "<<#x<<endl,exit(3),0:1)
#endif
LL read(){
LL x=0,f=0;
char ch=getchar();
while (!isdigit(ch))
f=ch=='-',ch=getchar();
while (isdigit(ch))
x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
return f?-x:x;
}
template <class T> void ckmax(T &x,const T y){
if (x<y)
x=y;
}
template <class T> void ckmin(T &x,const T y){
if (x>y)
x=y;
}
//int Pow(int x,LL y){
//// y=(y%(mod-1)+(mod-1))%(mod-1);
// int ans=1;
// for (;y;y>>=1,x=(LL)x*x%mod)
// if (y&1)
// ans=(LL)ans*x%mod;
// return ans;
//}
//void Add(int &x,int y){
// if ((x+=y)>=mod)
// x-=mod;
//}
//void Del(int &x,int y){
// if ((x-=y)<0)
// x+=mod;
//}
//int Add(int x){
// return x>=mod?x-mod:x;
//}
//int Del(int x){
// return x<0?x+mod:x;
//}
//int md(LL x){
// return (x%mod+mod)%mod;
//}
int main(){
LL n=read();
LL ans=n;
LL v=n+1;
LL L=2,R=2e9;
while (L<=R){
LL mid=(L+R)>>1;
if (mid*(mid+1)/2<=v)
L=mid+1,ans=n-mid+1;
else
R=mid-1;
}
cout<<ans<<endl;
return 0;
}
| #include <bits/stdc++.h>
#include <string>
#define MAX 100007
#define scn1(a) scanf("%d", &a);
#define scn2(a, b) scanf("%d %d", &a, &b);
#define scn3(a, b, c) scanf("%d %d %d", &a, &b, &c);
#define lscn1(a) scanf("%lli", &a);
#define lscn2(a, b) scanf("%lli %lli", &a, &b);
#define lscn3(a, b, c) scanf("%lli %lli %lli", &a, &b, &c);
#define prnt(a) printf("%d\n", a);
#define forr(i, a, n) for(int i=a;i<n;i++)
#define nl puts("");
#define PB push_back
#define enl cout << "\n";
#define CHK(a) cout << (#a) << " = " << (a) << endl;
typedef long long int ll;
using namespace std;
void arraychk(int a[],int n) {for(int i=0;i<n;i++)cout<<a[i]<<" ";enl;}
#define LOOPCHK(a, n) arraychk(a, n);
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);
cout.tie(NULL);
ll tt=1;
// cin >> tt;
for(int cse=1; cse<=tt; cse++)
{
ll i=0, j=0, k=0, l=0, m=0,
n=0, x=0, y=0, ans=0, sum=0;
cin >> n;
m = sqrt( n+1 );
while( m*m + m <= 2*(n+1) ) m++;
m--;
// cout << "Case " << cse << ":";
cout << n-m+1 << endl;
}
return 0;
}
|
#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <string>
#include <bits/stdc++.h>
#include <queue>
using namespace std;
/*
int main(){
long long R,X,Y;
cin >> R >> X >> Y;
long long kyori = sqrt(pow(X,2) + pow(Y,2));
long ans = kyori/R;
if(pow(R*ans,2) < pow(X,2) + pow(Y,2)){
ans++;
}
cout << ans << endl;
return 0;
}
*/
int main(){
long long R,X,Y;
cin >> R >> X >> Y;
long long kyori = pow(X,2) + pow(Y,2);
long ans = 0;
if(R*R > kyori){
cout << 2 << endl;
return 0;
}
while(pow(R*ans,2) < kyori){
ans++;
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define print(x) cout << x << endl
#define all(v) (v).begin(),(v).end()
#define YN(ok) print((ok ? "YES" : "NO"))
#define yn(ok) print((ok ? "Yes" : "No"))
#define decimal(n) setprecision(30) << n
#define Arrays_toString(v) rep(o,(v).size()){cout<<v[o]<<", ";if(o==(v).size()-1){cout<<endl;}}
template<class T> void que_toString(queue<T> q){while(q.size()){cout<<q.front()<<", ";q.pop();if(q.empty()){cout<<endl;}}}
int main(void){
int n, m, k;
cin >> n >> m >> k;
vi v(k);
rep(i, k) cin >> v[i];
// ans:
// ใใน0 โ Nใพใงใใใใใใๆใใตใคใณใญใๆฏใๅๆฐใฎๆๅพ
ๅคใฏ๏ผ
// (ใตใคใณใญใฎ็ฎใฏ1๏ฝMใง็ญ็ขบ็)
// (ใใตใใ ใใซๆปใใใในใใใ)
// how:
// ๆๅพ
ๅคDPใใใ
// (็น0ใใ็นNใซ่กใใฎใซ่ฒผใ่พบใฎๆฌๆฐใฎๆๅพ
ๅคใฏ๏ผใจๅๅค)
// ไพใใฐ6ใพใงๅบใใชใiใฎๆๅพ
ๅคใฏ1/6 * (i+1, i+2, ... i+6) + 1
// DAGใงใชใ(ใซใผใใใใ)DPใชใฎใงใdp[ๅฐ้ท] = dp[0] = xใจใใฆ้ฒใใใ
// dp[i] = ax + bใฎๅฝขใงใใผใฟใๆใคใ(a = dp[i][0], b = dp[i][1])
// -> dp[0] = a * x(=dp[0]) + b
// -> dp[0] = b / (1 - a)
// dp[i]: ใในiใใใดใผใซใซ่กใใพใงใซๆฏใๅๆฐใฎๆๅพ
ๅค
vector<vector<double>> dp(n+1+m, vector<double>(2));
for (int vv : v) {
dp[vv][0] = 1;
dp[vv][1] = 0;
}
// sum: ๅบ้้ทmใฎdpใฎsum
vector<double> sum(2);
for (int i = n+m-1; i >= n; i--) {
sum[0] += dp[i][0];
sum[1] += dp[i][1];
}
for (int i = n-1; i >= 0; i--) {
if (dp[i][0] != 1) {
dp[i][0] = sum[0] / m;
dp[i][1] = sum[1] / m + 1;
}
// update sum
sum[0] -= dp[i+m][0];
sum[1] -= dp[i+m][1];
sum[0] += dp[i][0];
sum[1] += dp[i][1];
}
// for (auto dpi : dp) Arrays_toString(dpi);
// x = dp[0][0] * x + dp[0][1]
// -> dp[0][1] / (1 - dp[0][0]) = x
double res = -1;
if (dp[0][0] != 1) res = dp[0][1] / (1 - dp[0][0]);
if (res > 1e12) res = -1;
print(decimal(res));
} |
// Sajal Asati
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mkp make_pair
#define pf push_front
#define ff first
#define ss second
// #define endl "\n"
#define lpstl(i,v) for(auto &i: v)
#define all(a) a.begin(),a.end()
#define rall(x) (x).rbegin(),(x).rend()
#define sz(a) (ll)(a.size())
#define tr(container, it) \
for(auto it = container.begin(); it != container.end(); ++it)
//containers
#define ii pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vll vector<ll>
#define vld vector<ld>
#define vii vector<ii>
#define vpll vector<pll>
#define pq priority_queue
#define minpq priority_queue <int, vector<int>, greater<int>>
const ll init = 1e9+7;
const ll N = 1e5+7;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll n; cin>>n;
ll sum=0;
while(n--){
ll a,b; cin>>a>>b;
sum += ((a+b)*(b-a+1))/2;
}
cout<<sum<<endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<int> a(m);
vector<int> b(m);
rep(i,m) {cin >> a.at(i); cin >> b.at(i);}
int k;
cin >> k;
vector<int> c(k);
vector<int> d(k);
rep(i,k) {cin >> c.at(i); cin >> d.at(i);}
int ans = 0;
for (int tmp = 0; tmp < (1 << k); ++tmp){
bitset<16> bit(tmp);
vector<int> bkt(105);
int accc = 0;
rep(i, k){
if (bit.test(i)) bkt.at(d.at(i))++;
else bkt.at(c.at(i))++;
}
rep(i, m){
if (bkt.at(b.at(i)) > 0 && bkt.at(a.at(i))>0) accc++;
}
ans = max(ans, accc);
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
#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 pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head
int n;
int main() {
scanf("%d",&n);
for (int i=1;i<=n;i++) {
int cc=1;
int x=i;
for (int j=2;j*j<=x;j++) while (x%j==0) x/=j,cc++;
if (x!=1) cc++;
printf("%d ",cc);
}
puts("");
}
| #include<bits/stdc++.h>
using namespace std;
#define int long long
vector<int> primeFactors(int n)
{
vector<int>ans;
// Print the number of 2s that divide n
while (n % 2 == 0)
{
ans.push_back(2);
n = n/2;
}
// n must be odd at this point. So we can skip
// one element (Note i = i +2)
for (int i = 3; i <= sqrt(n); i = i + 2)
{
// While i divides n, print i and divide n
while (n % i == 0)
{
ans.push_back(i);
n = n/i;
}
}
// This condition is to handle the case when n
// is a prime number greater than 2
if (n > 2)
ans.push_back(n);
return ans;
}
int32_t main(){
int n;cin>>n;
vector<int>ans;
cout<<1<<' ';
for(int i=2;i<=n;i++){
cout<<primeFactors(i).size()+1<<' ';
}
return 0;
} |
#include<bits/stdc++.h>
#define rep(i,a,b) for(R int i=a;i<=b;i++)
#define R register
#define endl putchar('\n')
const int N=500005;
const long long mod=1e9+7;
#define int long long
using namespace std;
int n,a[N],f[N][2],c[N][2];
void dp() {
f[0][0]=a[1],c[0][0]=1;
rep(i,1,n-1) {
f[i][0]=(f[i-1][0]+c[i-1][0]*a[i+1]+f[i-1][1]+c[i-1][1]*a[i+1])%mod;
c[i][0]=(c[i-1][1]+c[i-1][0])%mod;
f[i][1]=(f[i-1][0]-c[i-1][0]*a[i+1]%mod+mod)%mod;
c[i][1]=c[i-1][0];
}
}
signed main() {
scanf("%lld",&n);
rep(i,1,n) scanf("%lld",&a[i]);
dp();
printf("%lld",(f[n-1][1]+f[n-1][0])%mod);
return 0;
} | #include <bits/stdc++.h>
#include <string>
#include <cmath>
#include <sstream>
#include <unordered_map>
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORD(i, a, b) for(ll i = (a-1); i >= (b); i--)
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define ff first
#define ss second
#define spre cout<<fixed<<setprecision(15);
typedef long long int ll;
using namespace std;
//ll mod=67280421310721;
ll mod=998244353;
// ll mod=1000000007;
ll INF=1e18;
ll dp[6005][3005];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int test=1;
while(test--)
{
FOR(i,0,3000)
{
ll a=0;
FOR(j,1,6000)
{
if(i==0)
{
dp[j][i]=1;
continue;
}
if((i-j)>=0 && (2*j)<6000)
{
a+=dp[2*j][i-j];
a%=mod;
}
dp[j][i]=a;
}
}
int n,k;
cin>>n>>k;
cout<<dp[k][n-k];
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
string ans[20];
void output();
void init();
bool yokoni_dekirudake_umeru();
void yokoni_umeru(int,int);
double get_score();
int randomToSeisu(double,int);
double randxorsyosu();
void ten_umeru();
int n,m;
int used_m;
string next_s();
vector<string> s;
double now_max_score = 0;
int main(){ //2,3ใฏ็ก่ฆใงใใใใใใชใใ๏ผ
init();
bool filled = false;
while(used_m < m && !filled){
filled = yokoni_dekirudake_umeru();
}
ten_umeru();
output();
}
void ten_umeru(){
for(int i = 0 ; i < n ; i++){
for(int j = 0 ; j < n ; j++){
if(ans[i][j] == '.')
ans[i][j] = ('A' + randomToSeisu(randxorsyosu(),8));
}
}
}
bool yokoni_dekirudake_umeru(){
bool filled = true;
for(int i = 0 ; i < n ; i++){
for(int j = 0 ; j < n ; j++){
if(ans[i][j] == '.' && (j + (int)(next_s().size()) <= n + 1)){ //tyuui koko
yokoni_umeru(i,j);
filled = false;
}
}
}
return filled;
}
void yokoni_umeru(int i , int j){
for(int k = 0 ; k < (int)next_s().size() ; k++){
ans[i][j+k] = next_s()[k];
}
used_m++;
}
string next_s(){
return s[used_m];
}
void init(){
cin >> n >> m;
for(int i = 0 ; i < 20 ; i++){
for(int j = 0 ; j < 20 ; j++){
if(j==0)ans[i] = "";
ans[i] += ".";
}
}
for(int i = 0 ; i < m ; i++){
string st;
cin >> st;
s.push_back(st);
}
sort(s.begin(),s.end());
reverse(s.begin(),s.end());
}
void output(){
for(int i = 0 ; i < 20 ; i++){
cout << ans[i] << endl;
}
}
double get_score(){
bool ok[m];
double counter = 0;
fill(ok,ok+m,false);
for(int i = 0 ; i < n ; i++){
string te = ans[i] + ans[i];
for(int j = 0 ; j < m ; j++){
if(ok[j])continue;
for(int t = 0 ; t < n ; t++){
for(int r = 0 ; r < s[j].size() ; r++){
if(te[t+r] != s[j][r])continue;
if(r == (int)(s[j].size() - 1)){
ok[j] = true;
counter++;
break;
}
}
if(ok[j])break;
}
}
}
for(int i = 0 ; i < n ; i++){
string te_k = "";
for(int j = 0 ; j < n ; j++){
te_k += ans[j][i];
}
te_k += te_k;
for(int h = 0 ; h < m ; h++){
if(ok[h])continue;
for(int t = 0 ; t < n ; t++){
for(int r = 0 ; r < s[h].size() ; r++){
if(te_k[t+r] != s[h][r])continue;
if(r == (int)(s[h].size() - 1)){
ok[h] = true;
counter++;
break;
}
}
if(ok[h])break;
}
}
}
return 1e8 * counter / (double)m;
}
unsigned int randxor()
{
static unsigned int x=123456789,y=362436069,z=521288629,w=88675123;
unsigned int t;
t=(x^(x<<11));x=y;y=z;z=w; return( w=(w^(w>>19))^(t^(t>>8)) );
}
//0๏ฝ1ใฎใฉใณใใ ใชๅฐๆฐใ่ฟใ
double randxorsyosu(){
return (double)(randxor()%UINT_MAX)/UINT_MAX;
}
//d(0~1)ใใ0~i-1ใพใงใฎๆดๆฐใๅพใ
int randomToSeisu(double d , int i){
return floor((double)i * d);
} | #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <cmath>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <sstream>
#include <iomanip>
using namespace std;
using VI = vector <int>;
using VVI = vector <VI>;
using VVVI = vector <VVI>;
using VLL = vector <long long>;
using VVLL = vector <VLL>;
using UMI = unordered_map<int, int>;
using UMLL = unordered_map<long long, long long>;
using VS = vector <string>;
bool cmp(const string& a, const string& b) {
if (a.size() != b.size()) {
return a.size() < b.size();
}
return a < b;
}
int main() {
int N;
int M;
cin >> N >> M;
VS gene(M);
for (int i = 0; i < M; ++i) {
cin >> gene[i];
}
sort(gene.begin(), gene.end(), cmp);
VS ans(N);
int idx = 0;
for (int i = 0; i < gene.size(); ++i) {
if (ans[idx].size() + gene[i].size() < N) {
ans[idx] += gene[i];
} else {
// fill in
while (ans[idx].size() < N) {
ans[idx] += ".";
}
++idx;
}
if (idx == N) {
break;
}
}
for (int i = 0; i < N; ++i) {
cout << ans[i] << endl;
}
return 0;
} |
///Bismillahir Rahmanir Rahim
#include<bits/stdc++.h>
using namespace std;
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL)
#define int long long
#define pb push_back
#define pp pop_back
#define mp make_pair
#define pi 2*acos(0.0)
#define scan(i) scanf("%lld",&i)
#define scann(i,j) scanf("%lld%lld",&i,&j)
#define print(i) printf("%lld\n",i)
#define printt(i,j) printf("%lld %lld\n",i,j)
#define loop(i,n) for(int i = 0 ; i <n ; i++)
#define loopp(i,n) for(int i = 1 ; i <n ; i++)
#define rep(i,n) for(int i=n-1;i>=0;i--)
#define rev(v) reverse(v.begin(),v.end())
#define reset(a,b) memset(a,b,sizeof(a))
#define vect vector<int>
#define vec_it vector<int>::reverse_iterator vi
#define sorta(v) sort(v.begin(),v.end())
#define sortd(v) sort(v.begin(),v.end(),greater<>())
#define sorted(v) is_sorted(v.begin(),v.end())
#define mapp map<int,int>
#define rmap map<int,int,greater<>>m
#define pairr pair<int,int>
#define sett set<int>
#define rset set<int,greater<>>s
#define set_it set<int>::reverse_iterator si
#define map_it map<int,int> :: reverse_iterator mi
#define digit(n) floor(log10(n)+1)
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define ff first
#define ss second
#define mx INT_MAX
#define lmx LLONG_MAX
#define mn INT_MIN
#define lmn LLONG_MIN
#define inf 0x3f3f3f3f
#define inff 10e18
#define GCD __gcd(a,b)
#define LCM (a/GCD)*b
template <typename T> T gcd(T a,T b){return (b==0)?a:gcd(b,a%b);}
template <typename T> T lcm(T a,T b) {return a*(b/gcd(a,b));}
int modulo (int a, int b) { return a >= 0 ? a % b : ( b - abs ( a%b ) ) % b; }
void solve()
{
// freopen ("(input).txt" , "r", stdin) ;
string st,stt,str;
int low=0,high=0;
int l,r; char ch;
int n,k,t,x,y,z,d=0,cnt=0,temp=0,sum=0,f=0,flag=0;
cin>>n>>st ; vect v ,a ; sett s ; mapp m,mm ; pairr p ; stt="110";
for(int i=0;i<100000;i++)
{
str+=stt;
}
sum=3*10000000000;
if(n==1&&st[0]=='1')
cout<<2*10000000000<<endl;
else if(n==1&&st[0]=='1')
cout<<10000000000<<endl;
else if(n==2&&st[0]=='1'&&st[1]=='1'||n==2&&st[0]=='1'&&st[1]=='0')
cout<<10000000000<<endl;
else if(n==2&&st[0]=='0'&&st[1]=='1')
cout<<10000000000-1<<endl;
else if(str.substr(0,n)==st)
{
if(st[n-2]=='0'&&st[n-1]=='1')
sum-=2;
else if(st[n-2]=='1'&&st[n-1]=='1')
sum--;
sum-=(n-3);
cout<<sum/3<<endl;
}
else if(str.substr(1,n)==st)
{
sum-=1;
if(st[n-2]=='0'&&st[n-1]=='1')
sum-=2;
else if(st[n-2]=='1'&&st[n-1]=='1')
sum--;
sum-=(n-3);
cout<<sum/3<<endl;
}
else if(str.substr(2,n)==st)
{
sum-=2;
if(st[n-2]=='0'&&st[n-1]=='1')
sum-=2;
else if(st[n-2]=='1'&&st[n-1]=='1')
sum--;
sum-=(n-3);
cout<<sum/3<<endl;
}
else
cout<<0<<endl;
}
main()
{
int t;
// cin>>t;
// while(t--)
solve();
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
constexpr char ln = '\n';
constexpr long long MOD = 1000000007;
//constexpr long long MOD = 998244353;
constexpr long long INF = 1e9+100;
constexpr long long LINF = 1e18+100;
#define all(x) (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define rept(i, j, n) for(int i=(j); i<(n); i++)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const ll V = 1e10;
int main(){
int n; string t; cin >> n >> t;
if(t=="1")cout << V*2 << ln;
else{
if(t[0]=='0')t = "11" + t;
else if(t.size()>=2 and t.substr(0, 2)=="10")t = "1" + t;
if(t.size()%3==1){
if(t.back()=='0'){
cout << 0 << ln;
return 0;
}else t += "10";
}else if(t.size()%3==2){
if(t.substr(t.size()-2)=="11")t += "0";
else{
cout << 0 << ln;
return 0;
}
}
rep(i, t.size()){
if(i%3==2 and t[i]!='0' or i%3!=2 and t[i]!='1'){
cout << 0 << ln;
return 0;
}
}
ll res = V - t.size()/3 + 1;
cout << res << ln;
}
} |
#include <bits/stdc++.h>
#define f(i,x,y) for(int i=x; i<=y; ++i)
#define fn(i,x,y) for(int i=x; i>=y; --i)
using namespace std;
int a[5], b, n, m;
int main()
{
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
f(i,1,4) {cin >>a[i];}
sort(a+1,a+4+1);
if(a[1]+a[2]+a[3]==a[4] || a[1]+a[4]== a[3]+ a[2])
cout <<"Yes"; else cout <<"No";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main()
{
std::vector<int> cookies(4);
int total = 0;
for (int i = 0; i < 4; ++i) {
cin >> cookies[i];
total += cookies[i];
}
int eat;
bool flag = false;
for (int bit = 0; bit < (1<<4); ++bit) {
eat = 0;
for (int i = 0; i <= 4; ++i) {
if (bit & (1<<i)) {
eat += cookies[i];
}
}
if (total - eat == eat) {
flag = true;
}
}
if (flag) {
cout << "Yes";
} else {
cout << "No";
}
return 0;
} |
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update> order_set;
typedef long long ll ;
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define what_is(x) cout << #x << " is " << x << endl;
// #define endl '\n'
#define fi first
#define sec second
#define pb push_back
#define vi vector<int>
#define vii vector<pair<int,int>>
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
void solve(){
string str;
cin>>str;
string temp=str;
sort(temp.rbegin(),temp.rend());
string init="atcoder";
if(temp<=init){cout<<-1<<endl;return;}
if(str>init){cout<<0<<endl;return;}
int ln=sz(str);
int pos;
for(int i=0;i<ln;i++){
if(str[i]!='a'){pos=i;break;}
}
if(str[pos]>'t'){cout<<pos-1<<endl;return;}
cout<<pos<<endl;return;
}
int main(){
fio;
int test;
cin>>test;
// test=1;
while(test--){
solve();
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define ll long long
int check(string &s)
{
string ss="atcoder";
if(s>ss)
return 0;
for(int i=1;i<s.size();i++)
{
if(s[i]>'t')
return i-1;
if(s[i]>'a')
return i;
}
return -1;
}
int main()
{
int t;
cin>>t;
while(t--)
{
string a;
cin>>a;
cout<<check(a)<<endl;
}
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
using ll = long long;
using ld = long double;
const ll MOD = 1e9+7;
const ll INF=1LL<<60;
const string YYY="YES";
const string yyy="Yes";
const string NNN="NO";
const string nnn="No";
template<class T>void chmin(T& a,T b){
if(a>b){
a=b;
}
}
template<class T>void chmax(T& a,T b){
if(a>b){
a=b;
}
}
string rot13(string S){
int a=S.size();
for(int i=0;i<a;i++){
if(S[i]=='a'){S[i]='n';continue;}
if(S[i]=='b'){S[i]='o';continue;}
if(S[i]=='c'){S[i]='p'; continue;}
if(S[i]=='d'){S[i]='q'; continue;}
if(S[i]=='e'){S[i]='r'; continue;}
if(S[i]=='f'){S[i]='s'; continue;}
if(S[i]=='g'){S[i]='t'; continue;}
if(S[i]=='h'){S[i]='u'; continue;}
if(S[i]=='i'){S[i]='v'; continue;}
if(S[i]=='j'){S[i]='w'; continue;}
if(S[i]=='k'){S[i]='x'; continue;}
if(S[i]=='l'){S[i]='y'; continue;}
if(S[i]=='m'){S[i]='z'; continue;}
if(S[i]=='n'){S[i]='a'; continue;}
if(S[i]=='o'){S[i]='b'; continue;}
if(S[i]=='p'){S[i]='c'; continue;}
if(S[i]=='q'){S[i]='d';continue;}
if(S[i]=='r'){S[i]='e'; continue;}
if(S[i]=='s'){S[i]='f'; continue;}
if(S[i]=='t'){S[i]='g'; continue;}
if(S[i]=='u'){S[i]='h'; continue;}
if(S[i]=='v'){S[i]='i'; continue;}
if(S[i]=='w'){S[i]='j'; continue;}
if(S[i]=='x'){S[i]='k';continue;}
if(S[i]=='y'){S[i]='l'; continue;}
if(S[i]=='z'){S[i]='m'; continue;}
}
return S;
}
ll GD(ll num){//ๅๆกใฎๅ
ll digit=0;
while(num!=0){
digit+=num%10;
num/=10;
}
return digit;
}
bool if_integer( ld x ){//ๆดๆฐๅคๅฎ
return std::floor(x)==x;
}
bool if_prime(ll x){
bool a=true;
for(ll i=2;i*i<=x;i++){
if(x%i==0){
a=false;break;
}
}
if(x==1)a=false;
return a;
}
ll gcd(ll x, ll y)//ๆๅคงๅ
ฌ็ดๆฐ
{
if (x%y == 0)
{
return(y);
}
else
{
return(gcd(y, x%y));
}
}
ll lcm(ll x, ll y)//ๆๅฐๅ
ฌๅๆฐ
{
return x / gcd(x, y)*y ;
}
inline ll mod(ll a, ll m) {
return (a % m + m) % m;
}
ll modPow(ll a, ll n, ll p) {
if (n == 0) return 1; // 0ไนใซใๅฏพๅฟใใๅ ดๅ
if (n == 1) return a % p;
if (n % 2 == 1) return (a * modPow(a, n - 1, p)) % p;
ll t = modPow(a, n / 2, p);
return (t * t) % p;
}
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;
}
/////////////////////////
/////////////////////////
/////////////////////////
int main() {
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
ll N ;cin >> N ;
ll sum=0;
vector<int> a(N);
ll y=0;
for (int i = 0; i < N; i++) {
cin >> a[i];
sum+=a[i];
}
sort(a.begin(), a.end());
for(int i=0;i<N-1;i++){
y+=a[i];
}
if(y<a[N-1]){
cout<<a[N-1];return 0;
}
ll K=sum/2;
// ไบๆฌกๅ
้
ๅ
vector<vector<bool>> dp(N + 1, vector<bool>(K + 1, false));
// ๅๆๅ
for (int i = 0; i <= N; i++) {
dp[i][0] = true;
}
// ๆดๆฐ
for (int i = 0; i < N; i++) {
for (int k = 0; k <= K; k++) {
if (k - a[i] >= 0) dp[i + 1][k] = dp[i + 1][k] | dp[i][k - a[i]];
dp[i + 1][k] = dp[i + 1][k] | dp[i][k];
}
}
while(true){
if (dp[N][K]) {
cout << sum-K << endl;return 0;
} else {
K--;
}
}
} | # 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;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
#define trav(a,x) for (auto& a : x)
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert
#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end())
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
const int xn = 1e2 + 10;
const int sq = 320;
const int inf = 1e9 + 10;
const ll INF = 1e18 + 10;
const int mod = 1e9 + 7;//998244353;
const int base = 257;
const char nl = '\n';
bool ispow2(int i) { return i && (i & -i) == i; }
void solve() {
int n; cin>>n;
vector<int> t(n);
FOR(i, 0, n) cin>>t[i];
int s = 0;
FOR(i, 0, n) {
s += t[i];
}
vector<vector<bool> > dp(n+1, vector<bool>(s + 1, false));
dp[0][0] = true;
FOR(i, 0, n) {
FOR(j, 0, s) {
if(dp[i][j]) {
dp[i + 1][j] = true;
dp[i + 1][j + t[i]] = true;
}
}
}
int ans = s;
FOR(i, 0, s) {
if(dp[n][i]) {
ans = min(ans, max(i, s - i));
}
}
cout << ans;
return;
}
int main(){
ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
int tc = 1; //cin>>tc;
while(tc--) {
solve();
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
int n;
char s[400011];
int f[400011];
int c3[400011];
int getc(int m,int k)
{
if(c3[m]>c3[k]+c3[m-k])
{
return 0;
}
return f[m]*f[k]*f[m-k]%3;
}
int main()
{
int i,j,k;
cin>>n;
cin>>s+1;
memset(f,0,sizeof(f));
memset(c3,0,sizeof(c3));
f[0]=1;
c3[0]=0;
for(i=1;i<=n;i++)
{
int x=i;
while(x%3==0)
{
c3[i]++;
x/=3;
}
f[i]=f[i-1]*x%3;
c3[i]+=c3[i-1];
//cout<<f[i]<<' '<<c3[i]<<' '<<x<<endl;
}
int ans=0;
for(i=1;i<=n;i++)
{
int cur;
if(s[i]=='B')
{
cur=0;
}
else if(s[i]=='W')
{
cur=1;
}
else if(s[i]=='R')
{
cur=2;
}
int p=getc(n-1,i-1);
//cout<<cur<<' '<<p<<endl;
ans=(ans+p*cur)%3;
}
if(n%2==0)
{
ans=(-ans+3)%3;
}
if(ans==0)
{
cout<<'B'<<endl;
}
else if(ans==1)
{
cout<<'W'<<endl;
}
else if(ans==2)
{
cout<<'R'<<endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define MIN(a) *min_element(all(a))
#define MAX(a) *max_element(all(a))
#define SUM(a) accumulate(all(a), 0LL)
#define REP(i, n) for(int (i)=0; (i)<(n); (i)++)
#define RREP(i, n) for(int (i)=(n)-1; (i)>=0; (i)--)
#define FOR(i, m, n) for(int (i)=(m); (i)<(n); i++)
#define FORR(i, m, n) for(int (i)=(n)-1; (i)>=(m); i--)
#define debug(x) cerr << #x << " = " << x << endl
//#define int long long
typedef long long ll;
//ll const MOD = 998244353;
ll const MOD = 1e9+7;
int const inf = 1e9;
ll const INF = 1e18;
inline void ios_(){cin.tie(0); ios::sync_with_stdio(false);}
template<typename T> int size(const T& a){return (int)a.size();}
template<typename T> T Div(T a, T b){return (a + b - 1) / b;}
template<typename T> bool chmin(T& a, const T& b){if(a > b){a = b; return true;} return false;}
template<typename T> bool chmax(T& a, const T& b){if(a < b){a = b; return true;} return false;}
signed main(){
int n, k, m;
cin >> n >> k >> m;
int score = 0;
REP(i, n-1){
int a;
cin >> a;
score += a;
}
int po = n*m - score;
if(po > k){
cout << -1 << endl;
}else{
cout << max(0, po) << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
#define For(i, l, r) for (register int i = l, _r = r; i <= _r; ++i)
#define Ford(i, l, r) for (register int i = r, _l = l; i >= _l; --i)
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define mk make_pair
const int maxn = 105;
int n, k, mod, tp;
int dp[maxn][maxn * maxn * maxn];
ll ans;
inline int read()
{
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9'){if (ch == '-')f = -1;ch = getchar();}
while (ch >= '0' && ch <= '9'){x = (x << 3) + (x << 1) + ch - '0';ch = getchar();}
return x * f;
}
int main()
{
n = read(), k = read(), mod = read();
tp = k * n * (n - 1) / 2;
dp[0][0] = 1;
For(i,1,n-1)
{
For(j,0,tp)
{
dp[i][j] = dp[i - 1][j];
if(j>=i)
dp[i][j] = (dp[i][j] + dp[i][j - i]) % mod;
}
Ford(j, i * (k + 1), tp)
dp[i][j] = (dp[i][j] - dp[i][j - i * (k + 1)] + mod) % mod;
}
For(i,1,n)
{
ans = 0;
For(j, 0, tp) ans = (ans + 1LL * dp[i - 1][j] * dp[n - i][j]) % mod;
ans = (ans * (k + 1) - 1) % mod;
printf("%lld\n", ans);
}
} | #include<iostream>
using namespace std;
bool test = false;
int h[30][29];
int w[29][30];
int si,sj,ti,tj,a,response;
double e;
string ans;
void init(){
for(int i=0;i<30;i++) for(int j=0;j<29;j++) scanf("%d",&h[i][j]);
for(int i=0;i<29;i++) for(int j=0;j<30;j++) scanf("%d",&w[i][j]);
}
void writeL(int n){for(int i=0;i<n;i++) ans+="L";}
void writeR(int n){for(int i=0;i<n;i++) ans+="R";}
void writeU(int n){for(int i=0;i<n;i++) ans+="U";}
void writeD(int n){for(int i=0;i<n;i++) ans+="D";}
int main(){
if(test) init();
for(int i=0;i<1000;i++){
if(test) cin >> si >> sj >> ti >> tj >> a >> e;
else cin >> si >> sj >> ti >> tj;
//printf("%d %d %d %d %d %lf\n",si,sj,ti,tj,a,e);
ans = "";
if(si<ti) writeD(ti-si);
else if(ti<si) writeU(si-ti);
if(sj<tj) writeR(tj-sj);
else if(tj<sj) writeL(sj-tj);
cout << ans << endl;
if(!test) scanf("%d",&response);
}
} |
#include<cstdio>
#define mod 998244353
#define N 5001
#define M 51
using namespace std;
int n,m;
int stack[N];
long long f[M][N],jc[N],njc[N];
long long ksmi(long long x,long long y)
{
long long total=1,ab=x,p=y;
while (p)
{
if (p%2==1) total=total*ab%mod;
ab=ab*ab%mod;
p/=2;
}
return total;
}
long long C(int n,int m){
return jc[n]*njc[m]%mod*njc[n-m]%mod;
}
int main()
{
scanf("%d%d",&n,&m);
int sm=m;
int top=0;
jc[0]=1;
for (int i=1;i<=n;i++) jc[i]=jc[i-1]*i%mod;
njc[n]=ksmi(jc[n],mod-2);
for (int i=n-1;i>=0;i--) njc[i]=njc[i+1]*(i+1)%mod;
for (int i=1;i<=n;i++)
while (m!=0)
{
stack[++top]=m%2;
m/=2;
}
f[0][0]=1;
for (int i=1;i<=top;i++)
{
for (int j=0;j<=n;j++)
{
if (f[i-1][j]==0) continue;
int cur=j%2;
if (cur!=stack[i]) continue;
for (int k=0;k<=n;k+=2)
f[i][(j/2)+(k/2)]=(f[i][(j+k)/2]+f[i-1][j]*C(n,k)%mod)%mod;
}
}
printf("%lld\n",f[top][0]);
} | #include<iostream>
#include<vector>
using namespace std;
const long long int MAX = 3000;
const long long int MOD = 1000000007;
int main(){
long long int H,W,K=0;
long long int sum,ans=0;
vector<long long int> two;
string S;
bool F[MAX+2][MAX+2]={0};
long long int M[MAX+2][MAX+2][4]={0};
cin>>H>>W;
for(int i=1;i<=H;i++){
cin>>S;
for(int j=1;j<=W;j++){
F[i][j]=(S[j-1]=='.');
K+=F[i][j];
}
}
two.push_back(1);
for(int i=1;i<=K;i++)two.push_back(two[i-1]*2%MOD);
for(int i=1;i<=H;i++){
for(int j=1;j<=W;j++){
if(!F[i][j])continue;
M[i][j][0]=(M[i][j-1][0]+1)*F[i][j-1];
M[i][j][1]=(M[i-1][j][1]+1)*F[i-1][j];
}
}
for(int i=H;i>=1;i--){
for(int j=W;j>=1;j--){
if(!F[i][j])continue;
M[i][j][2]=(M[i][j+1][2]+1)*F[i][j+1];
M[i][j][3]=(M[i+1][j][3]+1)*F[i+1][j];
}
}
for(int i=1;i<=H;i++){
for(int j=1;j<=W;j++){
if(!F[i][j])continue;
sum=1;
for(int k=0;k<4;k++)sum+=M[i][j][k];
ans+=(two[K]-two[K-sum])%MOD;
ans=(ans%MOD+MOD)%MOD;
}
}
cout<<ans;
return 0;
} |
#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); i++)
using namespace std;
using P = pair<int, int>;
using ll = long long;
int n;
vector<vector<int>> to;
vector<int> used, col, vs;
void dfs(int v){
if(used[v]) return;
used[v] = 1;
vs.push_back(v);
for(int u : to[v]) dfs(u);
}
ll now;
void dfs2(int i){
if(i == vs.size()){now++; return;}
int v = vs[i];
rep(c, 3){
col[v] = c;
bool ok = true;
for(int u : to[v]){
if(col[u] == c) ok = false;
}
if(!ok) continue;
dfs2(i+1);
}
col[v] = -1;
}
int main(){
int m;
cin >> n >> m;
to = vector<vector<int>> (n);
rep(i, m){
int a, b;
cin >> a >> b;
a--; b--;
to[a].push_back(b);
to[b].push_back(a);
}
ll ans = 1;
used = vector<int> (n);
col = vector<int> (n, -1);
rep(i, n){
if(used[i]) continue;
ans *= 3;
vs = vector<int> ();
dfs(i);
col[vs[0]] = 0;
now = 0;
dfs2(1);
ans *= now;
}
cout << ans << endl;
} | #include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
using namespace std;
using ll = long long int;
using P = pair<ll, ll>;
// clang-format off
#ifdef _DEBUG_
#define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; debug_print(__VA_ARGS__); } while(false)
template<typename T, typename... Ts> void debug_print(const T &t, const Ts &...ts) { cerr << t; ((cerr << ", " << ts), ...); cerr << endl; }
#else
#define dump(...) do{ } while(false)
#endif
template<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }
template<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); }
template<typename T> bool chmin(T &a, const T& b) { if (a > b) {a = b; return true; } return false; }
template<typename T> bool chmax(T &a, const T& b) { if (a < b) {a = b; return true; } return false; }
template<typename T, typename... Ts> void print(const T& t, const Ts&... ts) { cout << t; ((cout << ' ' << ts), ...); cout << '\n'; }
template<typename... Ts> void input(Ts&... ts) { (cin >> ... >> ts); }
template<typename T> istream &operator,(istream &in, T &t) { return in >> t; }
// clang-format on
class UnionFind {
public:
vector<int> par;
int N;
UnionFind(int n) {
par.resize(n, -1);
N = n;
}
int Find(int n) {
return par[n] < 0 ? n : par[n] = Find(par[n]);
}
bool Union(int x, int y) {
x = Find(x);
y = Find(y);
if (x == y) return false;
if (-par[x] > -par[y]) swap(x, y);
par[y] += par[x];
par[x] = y;
N--;
return true;
}
bool Same(int x, int y) {
return Find(x) == Find(y);
}
int size(int x) {
return -par[Find(x)];
}
int size() {
return N;
}
};
int popcount(ll bits) {
bits = (bits & 0x5555555555555555LL) + (bits >> 1 & 0x5555555555555555LL);
bits = (bits & 0x3333333333333333LL) + (bits >> 2 & 0x3333333333333333LL);
bits = (bits & 0x0f0f0f0f0f0f0f0fLL) + (bits >> 4 & 0x0f0f0f0f0f0f0f0fLL);
bits = (bits & 0x00ff00ff00ff00ffLL) + (bits >> 8 & 0x00ff00ff00ff00ffLL);
bits = (bits & 0x0000ffff0000ffffLL) + (bits >> 16 & 0x0000ffff0000ffffLL);
return (bits & 0x00000000ffffffffLL) + (bits >> 32 & 0x00000000ffffffffLL);
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n, m;
input(n, m);
vector<P> edge(m);
vector<ll> pow2(30, 1);
rep(i, 1, 30) {
pow2[i] = pow2[i - 1] * 2;
}
rep(i, 0, m) {
int a, b;
input(a, b);
a--;
b--;
edge[i] = P(a, b);
}
int bits = 1 << n;
ll ans = 0;
rep(bit, 0, bits) {
vector<bool> used(n, 0);
rep(i, 0, n) {
if ((bit >> i) & 1) {
used[i] = true;
}
}
UnionFind uf(2 * n), uf2(n);
bool ok = true;
for (auto [a, b] : edge) {
if (used[a] && used[b]) {
ok = false;
} else if (used[a] || used[b]) {
continue;
} else {
uf.Union(a, b + n);
uf.Union(a + n, b);
uf2.Union(a, b);
}
}
for (auto [a, b] : edge) {
if (!used[a] && !used[b]) {
if (uf.Same(a + n, b + n)) {
ok = false;
break;
}
}
}
if (ok) {
ans += pow2[uf2.size() - popcount(bit)];
}
}
print(ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 1e9
#define linf 1e18
#define BASE 1000000
#define EPS 1e-10
#define PI acos(-1)
#define pii pair<int,int>
#define piii pair<int, pair<int,int>>
#define pli pair<long long, int>
#define pll pair<long long, long long>
#define pdi pair<double,int>
#define vi vector<int>
#define fi first
#define se second
#define ALL(x) (x).begin(), (x).end()
#define ms(x,val_add) memset(x, val_add, sizeof(x))
#define pb(x) push_back(x)
#define make_unique(x) sort(ALL(x)) ; x.erase( unique(ALL(x)), x.end()) ;
#define dbg(x) do { cout << #x << " = " << x << endl; } while(0)
#define mp(x, y) make_pair(x, y)
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; }
bool exitInput = false;
int ntest = 1, itest = 1 ;
const int dx[4] =
{
-1, 0, 1, 0
};
const int dy[4] =
{
0, 1, 0, -1
};
void addMod( ll &x, ll y, ll Mod )
{
x = ( x + y % Mod ) % Mod;
}
ll modPow( ll x, ll p, ll Mod )
{
if( p == 0 )
return 1;
ll r = modPow( x, p / 2, Mod );
r = ( r * r ) % Mod;
if( p & 1 )
r = ( r * x ) % Mod;
return r;
}
// copy BigInt from Um_nik
/* using Int = __int128;
Int gcd( Int x, Int y )
{
return y == 0 ? x : gcd( y, x % y );
}
void printInt( Int x )
{
if ( x == 0 )
{
cout << 0; //printf("0");
return;
}
vector<int> w;
while( x > 0 )
{
w.push_back( x % 10 );
x /= 10;
}
reverse( ALL( w ) );
for ( int z : w )
cout << z; //printf("%d", z);
}
void printAns(Int p, Int q) {
Int g = gcd(p, q);
p /= g;
q /= g;
printInt(p);
cout << "/";
printInt(q);
cout << endl;
} */
ll gcd(ll p, ll q)
{
return __gcd(p, q);
}
/*** CODE START HERE ***/
const ll Mod = 1000000007LL;
const int maxn = 800 + 5;
int n, K, a[maxn][maxn];
int b[maxn][maxn], sum[maxn][maxn];
int getsum(int i, int j, int r, int c)
{
return sum[r][c] - sum[r][j-1] - sum[i-1][c] + sum[i-1][j-1];
}
bool isok(int h)
{
static const int need = K*K / 2 + (K & 1);
for(int i = 1; i <= n; ++i)
{
for(int j = 1; j <= n; ++j)
{
b[i][j] = a[i][j] <= h;
sum[i][j] = sum[i-1][j] + sum[i][j-1] + b[i][j] - sum[i-1][j-1];
}
}
for(int i = 1; i + K - 1 <= n; ++i)
{
for(int j = 1; j + K - 1 <= n; ++j)
{
int r = i + K - 1, c = j + K - 1;
if( getsum(i, j, r, c) >= need )
{
return true;
}
}
}
return false;
}
int main()
{
#ifdef HOME
freopen( "input.txt", "r", stdin );
//freopen( "output.txt", "w", stdout );
#endif // HOME
ios::sync_with_stdio( false );
cin.tie( 0 );
cout.tie( NULL );
cout << setprecision( 10 );
cout << fixed;
//cin >> ntest;
for( itest = 1; itest <= ntest; ++itest )
{
cin >> n >> K;
int mi = inf, mx = 0;
for(int i = 1; i <= n; ++i)
{
for(int j = 1; j <= n; ++j)
{
cin >> a[i][j];
ckmin(mi, a[i][j]);
ckmax(mx, a[i][j]);
}
}
int lo = mi, hi = mx, mid, res = -1;
while(lo <= hi)
{
mid = (lo + hi)>>1;
if(isok(mid))
{
res = mid;
hi = mid - 1;
}
else lo = mid + 1;
}
cout << res << endl;
}
#ifdef HOME
cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
} | #include<iostream>
#include<vector>
#include<math.h>
#include<algorithm>
#include<stack>
#include<list>
#include<queue>
#include<set>
#include<string>
#include<string.h>
#include <sstream>
#include<map>
#include<iomanip>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization("unroll-loops")
/*Ordered set*/
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define PI 3.14159265
#define yes cout<<"YES"<<"\n"
#define no cout<<"NO"<<"\n"
using namespace std;
const ll MOD = 1000000007;
ll gcd(ll a,ll b){
if(a==0)
return b;
return gcd(b%a,a);
}
ll lcm(ll a,ll b){
return (a*b)/gcd(a,b);
}
bool isPrime(ll n){
for(ll i = 2; i <=sqrt(n); i++)
if(n % i == 0)
return 0;
return 1;
}
string binaryRepresentation(int x){
string s="";
for (int i = 31; i >= 0; i--)
{
if (x&(1<<i)) cout << "1";
else cout << "0";
}
return s;
}
ll combination(ll n,ll r){
if(r==n || r==0)
return 1LL;
return combination(n-1,r-1) + combination(n-1,r);
}
bool isPowerOfTwo(ll n){
if(n==0)
return false;
return (ceil(log2(n)) == floor(log2(n)));
}
ll power(ll a,ll b){
ll ans=1;
while(b)
{
if(b&1)
ans=(ans*a)%MOD;
b/=2;
a=(a*a)%MOD;
}
return ans%MOD;
}
ll inverse(ll i){
if(i==1) return 1;
return (MOD-((MOD/i)*inverse(MOD%i))%MOD+MOD)%MOD;
}
string toString(ll a){
ostringstream temp;
temp << a;
return temp.str();
}
ll toNumber(string s){
stringstream num(s);
ll x=0;
num>>x;
return x;
}
int main()
{
//vector<vector<ll> >ar(n,vector<ll> (m,0));
//vector<ll>::iterator ptr;
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
ll n,k;
cin>>n>>k;
vector<vector<ll> >ar(n,vector<ll>(n,0));
for(ll i=0;i<n;i++)
{
for(ll j=0;j<n;j++)
{
ll x;
cin>>x;
ar[i][j]=x;
}
}
ll low=0;
ll high=1e9+1;
ll med=(k*k)/2+1;
ll r=k-1;
while(low<=high)
{
ll mid=(low+high)/2;
//cout<<" mid : "<<mid<<"\n";
/* Mid will be the possible median value */
vector<vector<ll> >temp=ar;
for(ll i=0;i<n;i++)
{
for(ll j=0;j<n;j++)
{
if(ar[i][j]>mid)
temp[i][j]=1;
else
temp[i][j]=0;
}
}
//for(ll i=0;i<n;i++)
//{
// for(ll j=0;j<n;j++)
// cout<<temp[i][j]<<" ";
// cout<<"\n";
//}
for(ll i=0;i<n;i++)
{
for(ll j=1;j<n;j++)
{
temp[i][j]+=temp[i][j-1];
}
}
for(ll i=1;i<n;i++)
{
for(ll j=0;j<n;j++)
{
temp[i][j]+=temp[i-1][j];
}
}
bool flag=false;
for(ll i=r;i<n;i++)
{
for(ll j=r;j<n;j++)
{
ll sum=temp[i][j];
ll row1=i-r;
ll col1=j-r;
if(row1>0)
sum-=temp[row1-1][j];
if(col1>0)
sum-=temp[i][col1-1];
if(row1>0 && col1>0)
sum+=temp[row1-1][col1-1];
//cout<<" i : "<<i<<" j : "<<j<<" sum : "<<sum<<"\n";
if(sum<med)
{
flag=true;
}
}
}
if(flag)
high=mid-1;
else
low=mid+1;
}
cout<<low;
return 0;
}
|
#pragma GCC optimize("Ofast")
#pragma loop_opt(on)
#include<bits/stdc++.h>
#define Rushia_mywife ios::sync_with_stdio(0);cin.tie(0);
#define F first
#define S second
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define mp make_pair
#define mt make_tuple
#define FL cout.flush()
#define all(x) (x).begin(),(x).end()
#define mem(x,i) memset((x),(i),sizeof((x)))
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<long long,long long>;
using ld = long double;
mt19937 mtrd(chrono::steady_clock::now().time_since_epoch().count());
const int mod = 1000000007;
const int mod2 = 998244353;
const ld PI = acos(-1);
#define Bint __int128
#define int long long
int qpow(int x,int powcnt,int tomod){
int res = 1;
for(;powcnt;powcnt>>=1,x=(x*x)%tomod)
if(1&powcnt)res = (res*x)%tomod;
return (res%tomod);
}
int inv(int x){ return qpow(x,mod-2,mod); }
// --------------------------------------**
vector<int>cyc[10];
bool vis[10];
void solve(){
for(int i=0;i<10;i++){
mem(vis,0);
cyc[i].pb(i);
vis[i] = 1;
int x = i*i%10;
while(!vis[x]){
vis[x] = 1;
cyc[i].pb(x);
x = x*i%10;
}
}
int a,b,c;
cin >> a >> b >> c;
int x = qpow(b,c,(int)cyc[a%10].size());
cout << cyc[a%10][(x-1+cyc[a%10].size())%(int)cyc[a%10].size()] << '\n';
}
signed main(){
Rushia_mywife
int t = 1;
//cin >> t;
while(t--)
solve();
}
| #include<iostream>
#include<algorithm>
#include<map>
#include<queue>
#include<cmath>
#include<stack>
#include<string>
#include<vector>
#include<set>
#define rep(i,n) for(int i = 0;i < n;i++)
using namespace std;
long long int powMod(long long int x, long long int k, long long int m) {
if (k == 0) return 1;
if (k % 2 == 0) return powMod(x*x % m, k/2, m);
else return x*powMod(x, k-1, m) % m;
}
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
//ฯ้ขๆฐ
long long int euler_phi(long long int n) {
long long int ret = n;
for(long long int i = 2; i * i <= n; i++) {
if(n % i == 0) {
ret -= ret / i;
while(n % i == 0) n /= i;
}
}
if(n > 1) ret -= ret / n;
return ret;
}
int main(void){
long long int a,b,c;
cin >> a >> b >> c;
//bใฎๅถๅฅ
int moto = a%10;
if(moto==0||moto == 1){
cout << moto << endl;
return 0;
}
if(moto==4){
int jyou = powMod(b,c,2);
if(jyou ==0){
cout << moto*moto%10 << endl;
return 0;
}
cout << powMod(a%10,jyou,10);
}
if(moto == 5){
cout << moto<<endl;
return 0;
}
if(moto == 6){
cout << moto<<endl;
return 0;
}
if(moto==7||moto==8||moto==2||moto==3){
int jyou = powMod(b,c,4);
if(jyou ==0){
cout << moto*moto*moto*moto%10 << endl;
return 0;
}
cout << powMod(a%10,jyou,10);
return 0;
}
if(moto==9){
if(b%2==0){
cout << 1 << endl;
}
else{
cout << 9<<endl;
}
}
return 0;
} |
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int,int>;
struct UnionFind {
vector<int> d;
UnionFind(int n=0): d(n,-1) {}
int find(int x) {
if (d[x] < 0) return x;
return d[x] = find(d[x]);
}
bool unite(int x, int y) {
x = find(x); y = find(y);
if (x == y) return false;
if (d[x] > d[y]) swap(x,y);
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) { return find(x) == find(y);}
int size(int x) { return -d[find(x)];}
};
int main(){
int n, q;
cin>>n>>q;
vector<int> c(n);
rep(i, n){cin>>c[i]; c[i]--;};
UnionFind uf(n);
vector<map<int, int>> mas(n);
rep(i, n) mas[i][c[i]]=1;
rep(_, q){
int t;
cin>>t;
if(t==1){
int a, b;
cin>>a>>b; a--; b--;
int aa=uf.find(a), bb=uf.find(b);
if(aa!=bb){
uf.unite(a, b);
int c=uf.find(a), d;
if(c==aa) d=bb;
else d=aa;
for(auto p : mas[d]) mas[c][p.first]+=p.second;
}
}
else{
int x, y;
cin>>x>>y; x--; y--;
int a=uf.find(x);
cout<<mas[a][y]<<endl;
}
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int64_t N;
cin >> N;
vector<vector<int64_t>> C(N, vector<int64_t>(N));
for (int64_t i = 0; i < N; i++) {
for (int64_t j = 0; j < N; j++) {
cin >> C[i][j];
}
}
vector<int64_t> diff(N - 1);
for (int64_t i = 0; i < N - 1; i++) {
set<int64_t> curr_diff;
for (int64_t j = 0; j < N; j++) {
curr_diff.insert(C[i + 1][j] - C[i][j]);
}
if (curr_diff.size() > 1) {
cout << "No" << endl;
return 0;
}
diff[i] = *curr_diff.begin();
}
vector<int64_t> A(N, 0);
for (int64_t i = 0; i < N - 1; i++) {
A[i + 1] = A[i] + diff[i];
}
const int64_t min_A = *min_element(A.begin(), A.end());
if (min_A < 0) {
for (int64_t i = 0; i < N; i++) {
A[i] -= min_A;
}
}
vector<int64_t> B(N);
for (int64_t i = 0; i < N; i++) {
B[i] = C[0][i] - A[0];
}
const int64_t min_B = *min_element(B.begin(), B.end());
if (min_B < 0) {
cout << "No" << endl;
return 0;
}
cout << "Yes" << endl;
for (int64_t i = 0; i < N; i++) {
cout << A[i] << " \n"[i == N - 1];
}
for (int64_t i = 0; i < N; i++) {
cout << B[i] << " \n"[i == N - 1];
}
} |
#include<bits/stdc++.h>
typedef long long ll;
ll n,k,a[205];
ll ans;
using namespace std;
int main()
{
cin>>n;
for(int i=0; i<205; i++)
{
a[i]=0;
}
for(int i=0; i<n; i++)
{
cin>>k;
a[k%200]++;
}
ans=0;
for(int i=0; i<205; i++)
{
k=a[i]*(a[i]-1)/2;
ans+=k;
}
cout<<ans;
return 0;
} | #include <bits/stdc++.h>
#define int long long
using namespace std;
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
while(T--)
{
int N;
cin >> N;
map<int,int> mp;
for(int i = 0 ; i < N ; i++)
{
int n;
cin >> n;
mp[n]++;
}
bool flag = true;
for(auto x : mp)
{
if(x.second&1)flag = false;
}
if(N&1)cout << "Second" << '\n';
else
{
if(flag)cout << "Second" << '\n';
else cout << "First" << '\n';
}
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define ff first
#define ss second
typedef long long ll;
ll power(ll a, ll b){//a^b
ll res=1;
a=a%MOD;
while(b>0){
if(b&1){res=(res*a)%MOD;b--;}
a=(a*a)%MOD;
b>>=1;
}
return res;
}
ll fermat_inv(ll y){return power(y,MOD-2);}
ll gcd(ll a, ll b){return (b==0)?a:gcd(b,a%b);}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t=1;
//cin>>t;
while(t--){
ll x,n;
cin>>x>>n;
ll y=100ll;
x+=y;
ll g=gcd(x,y);
x/=g;
y/=g;
vector <bool> vis(x,false);
for(int i=1;i<x;i++){
if((x*i)/y<x){
vis[(x*i)/y]=true;
}
}
vector <ll> a;
for(int i=1;i<x;i++){
if(vis[i]==false){
a.push_back(i);
}
}
ll m=a.size();
ll pos=(n-1ll)%m;
ll ans=(n-1ll)/m;
ans*=x;
ans+=a[pos];
cout<<ans<<"\n";
}
return 0;
} | #include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <iostream>
#include <cassert>
#include <cmath>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <cstdlib>
using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define li long long
#define pii pair<int, int>
#define vi vector<int>
#define li long long
#define forn(i, n) for (int i = 0; i < (int)n; i++)
#define fore(i, b, e) for (int i = (int)b; i <= (int)e; i++)
map<li, int> m;
int main() {
int n;
scanf("%d", &n);
li cur = 0;
m[0]++;
li ans = 0;
forn(i, n) {
int x;
scanf("%d", &x);
if (i % 2 == 0) {
cur += x;
} else {
cur -= x;
}
ans += m[cur];
m[cur]++;
}
cout << ans;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int &x : a) cin >> x;
a.push_back(0);
sort(a.begin(), a.end());
int64_t ans = 1;
constexpr int64_t mod = 1e9 + 7;
for (int i = 1; i < a.size(); ++i) ans = ans * (a[i] - a[i-1] + 1) % mod;
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define fr(i,n) for(int i = 0; i<n; i++)
#define sz(v) (int)(v.size())
#define prin(a) cout << #a << " = " << a << endl
#define prinv(v) cout << #v << " = "; for(auto it : v) cout << it << ", "; cout << endl
#define all(v) (v).begin(),(v).end()
typedef long long ll;
#define rmin(a,b) a = min<ll>(a,b)
#define rmax(a,b) a = max<ll>(a,b)
#define fi first
#define se second
template <class T>
T fp(T x, long long e) {
T ans(1);
for(; e > 0; e /= 2) {
if(e & 1) ans = ans * x;
x = x * x;
}
return ans;
}
const ll mod = round(1e9)+7;
struct mb {
mb(int v = 0) : val(v < 0 ? v + mod : v) {}
mb(ll v){ val = (v%mod+mod)%mod; }
int val;
void operator += (mb o) { *this = *this + o; }
void operator -= (mb o) { *this = *this - o; }
void operator *= (mb o) { *this = *this * o; }
mb operator * (mb o) { return (int)((long long) val * o.val % mod); }
//mb operator / (mb o) { return *this * fp(o, mod - 2); }
//bool operator == (mb o) { return val==o.val; } //usar soh para hashes
mb operator + (mb o) { return val + o.val >= mod ? val + o.val - mod : val + o.val; }
mb operator - (mb o) { return val - o.val < 0 ? val - o.val + mod : val - o.val; }
};
int main(){
ios::sync_with_stdio(0); cin.tie(0);
int n;
set<int> st;
cin >> n;
fr(i,n){
int x; cin >> x;
st.emplace(x);
}
st.emplace(0);
vector<int> v(all(st));
mb ans = 1;
fr(i,sz(v)-1){
ans*=v[i+1]+1-v[i];
}
cout << ans.val << "\n";
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll,ll>
#define FOR(I,A,B) for(ll I = ll(A); I < ll(B); ++I)
#define FORR(I,A,B) for(ll I = ll((B)-1); I >= ll(A); --I)
#define TO(x,t,f) ((x)?(t):(f))
#define SORT(x) (sort(x.begin(),x.end())) // 0 2 2 3 4 5 8 9
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) //xi>=v x is sorted
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) //xi>v x is sorted
#define NUM(x,v) (POSU(x,v)-POSL(x,v)) //x is sorted
#define REV(x) (reverse(x.begin(),x.end())) //reverse
ll gcd_(ll a,ll b){if(a%b==0)return b;return gcd_(b,a%b);}
ll lcm_(ll a,ll b){ll c=gcd_(a,b);return ((a/c)*b);}
#define NEXTP(x) next_permutation(x.begin(),x.end())
const ll INF=ll(1e16)+ll(7);
const ll MOD=1000000007LL;
#define out(a) cout<<fixed<<setprecision((a))
//tie(a,b,c) = make_tuple(10,9,87);
#define pop_(a) __builtin_popcount((a))
ll keta(ll a){ll r=0;while(a){a/=10;r++;}return r;}
#define num_l(a,v) POSL(a,v) //vๆชๆบใฎ่ฆ็ด ๆฐ
#define num_eql(a,v) POSU(a,v) //vไปฅไธใฎ่ฆ็ด ๆฐ
#define num_h(a,v) (a.size() - POSU(a,v)) //vใใๅคงใใ่ฆ็ด ๆฐ
#define num_eqh(a,v) (a.size() - POSL(a,v)) //vไปฅไธใฎ่ฆ็ด ๆฐ
// static_cast< long long ll >
int main(){
ll N;
cin >> N;
double ans = 0;
FOR(i,1,N){
ans += 1.0 * N / i;
}
out(10) << ans << endl;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
return (ull)rng() % B;
}
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n; cin >> n;
vector<double> dp(n+1);
dp[1]=0;
for(int i=1;i<n;i++){
dp[i+1]=dp[i]+n/(double)(n-i);
}
printf("%.9f\n",dp[n]);
}
|
#include <bits/stdc++.h>
using namespace std;
template <class A, class B> bool cmin(A& a, B b) { return a > b && (a = b, true); }
template <class A, class B> bool cmax(A& a, B b) { return a < b && (a = b, true); }
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
long N;
cin >> N;
vector<tuple<long, long, long>> V(N);
for (long i = 0; i < N; i++) {
long a, b, c;
cin >> a >> b >> c;
V.at(i) = {a, b, c};
}
long ans = LONG_MAX;
for (const auto& [a, b, c] : V) {
if (a >= c) continue;
cmin(ans, b);
}
if (ans == LONG_MAX) ans = -1;
cout << ans << '\n';
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
void solve(long long N){
set<ll> se;
for(ll i = 2;i <= sqrt(N) + 10;i++){
ll _i = i * i;
for(;_i <= N;_i *= i){
/*
if(_i == 0){
exit(0);
}
*/
//cout<<_i<<endl;
se.insert(_i);
}
}
cout<<N - se.size()<<endl;
}
int main(){
long long N;
scanf("%lld",&N);
solve(N);
return 0;
}
|
#include<bits/stdc++.h>
#include<bits/extc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
using namespace __gnu_pbds;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define priority_queue std::priority_queue
#define F first
#define S second
ll MOD=998244353;
int h, w, k;
char g[5005][5005];
ll dp[5005][5005], emp[5005][5005], po3[200005], inv;
ll modpow(ll x, ll e){
if(x==0) return 0;
if(e==0) return 1;
ll y=modpow(x, e/2);
if(e%2==0) return (y*y)%MOD;
return ((y*y)%MOD*x)%MOD;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>h>>w>>k;
po3[0]=1;
for(int i=1;i<200005;i++){
po3[i]=(po3[i-1]*3)%MOD;
}
for(int i=0;i<=h;i++){
for(int j=0;j<=w;j++){
g[i][j]='.';
}
}
for(int i=0;i<k;i++){
int x, y;
char c;
cin>>x>>y>>c;
g[x][y]=c;
}
for(int i=1;i<=h;i++){
for(int j=1;j<=w;j++){
emp[i][j]=emp[i-1][j]+emp[i][j-1]-emp[i-1][j-1]+(g[i][j]=='.');
}
}
if(g[1][1]=='.') dp[1][1]=3;
else dp[1][1]=1;
inv=modpow(3, MOD-2);
for(int x=1;x<=h;x++){
for(int y=1;y<=w;y++){
if(x+y==2) continue;
ll &ans=dp[x][y];
ll c=po3[emp[x][y]-emp[x][y-1]], r=po3[emp[x][y]-emp[x-1][y]];
if(g[x][y-1]=='R' || g[x][y-1]=='X'){
ans+=(dp[x][y-1]*c)%MOD;
ans%=MOD;
}
if(g[x-1][y]=='D' || g[x-1][y]=='X'){
ans+=(dp[x-1][y]*r)%MOD;
ans%=MOD;
}
if(g[x][y-1]=='.'){
ans+=(((2*inv*dp[x][y-1])%MOD)*c)%MOD;
ans%=MOD;
}
if(g[x-1][y]=='.'){
ans+=(((2*inv*dp[x-1][y])%MOD)*r)%MOD;
ans%=MOD;
}
//cout<<ans<<" ";
}
//cout<<endl;
}
cout<<dp[h][w]<<endl;
/*for(int i=1;i<=2;i++){
for(int j=1;j<=2;j++){
cout<<dp[i][j]<<" ";
}
cout<<endl;
}*/
} | #pragma GCC optimize("Ofast")
#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
#define rsrt(v) sort(v.begin(), v.end(), greater<int>())
#define rsrtl(v) sort(v.begin(), v.end(), greater<ll>())
#define fi(i, a, b) for (int i = a; i < b; i++)
#define fll(i, a, b) for (ll i = a; i < b; i++)
#define srt(v) sort(v.begin(), v.end())
#define pb push_back
#define g(v, i, j) get<i>(v[j])
#define vi vector<int>
#define vll vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(x) (x).begin(),(x).end()
#define ll long long
ll md = 1000000007;
#define theartofwar \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
#define F first
#define S second
#define sz(v) v.size()
using namespace std;
#define dbg(x) cerr << #x << " = " << x << endl;
template <typename T>
T pw(T a, T b){
T c = 1, m = a;
while (b){
if (b & 1) c = (c * m);
m = (m * m), b /= 2;
}
return c;
}
template <typename T>
T ceel(T a, T b){
if (a % b == 0)
return a / b;
else
return a / b + 1;
}
template <typename T>
T my_log(T n, T b){
T i = 1, ans = 0;
while (1){
if (i > n){
ans--;
break;
}
if (i == n) break;
i *= b, ans++;
}
return ans;
}
template <typename T>
T gcd(T a, T b){
if (b == 0)
return a;
return gcd(b, a % b);
}
ll pwmd(ll a, ll b){
ll c = 1, m = a;
while (b){
if (b & 1) c = (c * m) % md;
m = (m * m) % md;
b /= 2;
}
return c;
}
ll modinv(ll n){
return pwmd(n, md - 2);
}
ll inverse(ll i){
if (i == 1)
return 1;
return (md - ((md / i) * inverse(md % i)) % md + md) % md;
}
bool sortbysec(const pair<ll, ll> &a, const pair<ll, ll> &b){
return (a.second < b.second);
}
int main()
{
theartofwar;
ll n, a;
cin >> n;
vll v;
fll(i, 0, n) cin >> a, v.pb(a);
ll dp[n][n + 1];
fll(i, 0, n) fll(j, 0, n + 1) dp[i][j] = 0;
ll last[n + 1][n];
fll(i, 0, n + 1) fll(j, 0, n) last[i][j] = -1;
dp[0][1] = 1;
fll(i, 2, n + 1) last[i][v[0] % i] = 0;
ll s = v[0];
fll(i, 1, n){
dp[i][1] = 1;
s += v[i];
fll(j, 2, i + 2){
ll p = s % j;
if (last[j][p] == -1) continue;
ll o = last[j][p];
dp[i][j] = (dp[i][j] + dp[o][j]) % md;
dp[i][j] = (dp[i][j] + dp[o][j - 1]) % md;
}
fll(j, 2, n + 1) last[j][s % j] = i;
}
ll ans = 0;
fll(i, 1, n + 1) ans = (ans + dp[n - 1][i]) % md;
cout << ans;
}
|
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int mod = 1e9+7;
int main(){
int N; cin >> N;
vector<ll> A(N), sum(N+1);
for(int i=0; i<N; i++){
cin >> A[i];
sum[i+1] = sum[i]+A[i];
}
vector<vector<int>> dp(N+2,vector<int>(N+2));
dp[1][0]++;
int ans = 0;
for(int i=0; i<N; i++){
for(int j=N; 1<=j; j--){
dp[j+1][sum[i+1]%(j+1)] += dp[j][sum[i+1]%j];
dp[j+1][sum[i+1]%(j+1)] %= mod;
if(i == N-1){
ans += dp[j][sum[i+1]%j];
ans %= mod;
}
}
}
cout << ans << endl;
}
| #include<bits/stdc++.h>
//#pragma GCC optimize "trapv"
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
#define fast_az_fuk ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll long long
#define lll __int128
#define ull unsigned ll
#define ld long double
#define pb push_back
#define pf push_front
#define dll deque<ll>
#define vll vector<ll>
#define vvll vector<vll>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define dpll deque<pll>
#define mapll map<ll,ll>
#define umapll umap<ll,ll>
#define endl "\n"
#define all(v) v.begin(),v.end()
#define ms(a,x) memset(a,x,sizeof(a))
#define random(l,r,T) uniform_int_distribution<T>(l,r)(rng)
//#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//using namespace __gnu_pbds;
#ifdef LOCAL
#include <debug.h>
#endif
template<typename T> istream& operator >>(istream &in,vector<T> &v){ for(auto &x:v) in>>x; return in;}
template<typename T> ostream& operator <<(ostream &out,const vector<T> &v){ for(auto &x:v) out<<x<<' '; return out;}
template<typename T1,typename T2> istream& operator >>(istream &in,pair<T1,T2> &p){ in>>p.first>>p.second; return in;}
template<typename T1,typename T2> ostream& operator <<(ostream &out,const pair<T1,T2> &p){ out<<p.first<<' '<<p.second; return out;}
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);
}
};
template<class T, class H>using umap=unordered_map<T,H,custom_hash>;
template<class T>using uset=unordered_set<T,custom_hash>;
const ll MOD = 1e9 + 7;
int32_t main()
{
#ifdef LOCAL
freopen("error.txt", "w", stderr);
clock_t clk = clock();
#endif
fast_az_fuk
ll n; cin>>n; vll a(n); cin>>a; a.insert(a.begin(),0);
vvll pre(n+1,vll(n+1));
for(int i=1;i<=n;i++){
vll last(n+1,0);
int j=1; ll sum = 0;
while(j <= n){
sum += a[j];
pre[i][j] = last[sum%i];
last[sum%i] = j; j++;
}
}
vvll dp(n+1,vll(n+1));
dp[0][0] = 1;
for(int i=1;i<=n;i++){
dp[i][1] = 1;
for(int j=2;j<=n;j++){
dp[i][j] = (dp[i][j] + dp[pre[j][i]][j])%MOD;
}
if(i == n) break;
for(int j=n;j>=2;j--){
dp[i][j] = (dp[i][j-1] + dp[pre[j][i]][j]) % MOD;
}
}
ll ans = 0;
for(int i=n;i>=1;i--){
ans = (ans + dp[n][i])%MOD;
}
cout<<ans<<endl;
#ifdef LOCAL
cerr << '\n'<<"Time (in s): " << double(clock() - clk) * 1.0 / CLOCKS_PER_SEC << '\n';
#endif
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>
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; }
ll run()
{
ll v,t,s,d;in(v,t,s,d);
if (v * t <= d && d <= v * s) return false;
return true;
}
int main()
{
cout << fixed << setprecision(15);
auto r = run();
YN(r);
//cout << r << endl;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
ll po(ll x,ll n){
ll i,ans=1;
for(i=0;i<n;i++){
ans*=x;
}
return ans;
}
int main(){
ll n,i,j;
cin >> n;
for(i=1;i<=38;i++){
for(j=1;j<=27;j++){
if(po(3,i)+po(5,j)==n){
printf("%lld %lld\n",i,j);
return 0;
}
}
}
cout << -1 << endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define vi vector <int>
#define vp vector <pair<int,int>>
#define f(x,y,z) for(x=y;x<z;x++)
#define pb push_back
#define mp make_pair
#define ld long double
#define fb(x,y,z) for(x=y;x>z;x--)
#define ps(x,y) fixed << setprecision(y) << x
#define PI 3.1415926535
#define mod 1000000007
#define mods 998244353
#define setbits(x) __builtin_popcountll(x)
#define all(x) (x).begin(), (x).end()
#define fill(a,b) memset(a,b,sizeof(a))
// AUTHOR ALOK KUMAR
int lcm(int a,int b)
{
return (a*b)/(__gcd(a,b));
}
int power(int a,int b)
{
int c=1;
while(b)
{
if(b&1)
c*=a;
a*=a;
b>>=1;
}
return c;
}
void solve()
{
int n;
cin>>n;
int a[3];
int sum=0;
fill(a,0);
int count=0;
while(n)
{
int i,j;
j=n%10;
a[j%3]++;
n/=10;
sum+=j;
count++;
}
if(sum%3==0)
cout<<"0\n";
else if(sum%3==1)
{
if(a[1]&&count>1)
cout<<"1\n";
else
{
if(a[2]>=2&&count>2)
cout<<"2\n";
else
cout<<"-1\n";
}
}
else
{
if(a[2]&&count>1)
cout<<"1\n";
else
{
if(a[1]>=2&&count>2)
cout<<"2\n";
else
cout<<"-1\n";
}
}
}
int32_t main()
{
IOS
int t=1;
//cin>>t;
while(t--)
solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int T; cin >> T;
string U = "atcoder";
while(T--){
string S; cin >> S;
int N = S.size();
bool alla = true;
for(int i = 0; i < N; i++){
if(S.at(i) != 'a') alla = false;
}
if(alla){
cout << -1 << "\n"; continue;
}
if(S > U){
cout << 0 << "\n"; continue;
}
int ans = 1919;
for(int i = 1; i < N; i++){
string V = S;
int cnt = 0;
for(int j = i; j >= 1; j--){
swap(V.at(j-1), V.at(j));
cnt++;
if(V > U){
ans = min(ans, cnt);
break;
}
}
}
cout << ans << "\n";
}
} |
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;
int main() {
int prime[20] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71};
//rep(i, 20) cout << i << " " << prime[i] << "\n";
ll A, B;
cin >> A >> B;
vector<vector<ll>> dp(B - A + 2, vector<ll>(1LL << 20, 0));
dp[0][0] = 1;
rep(i, B - A + 1) {
//cout << "i=" << i << "\n";
ll x = A + i;
//cout << "x=" << x << "\n";
int d = 0;
rep(j, 20) {
//cout << "j=" << j << "\n";
d *= 2;
if (x % prime[19 - j] == 0) d++;
}
//cout << "d=" << d << "\n";
rep(j, 1LL << 20) {
dp[i + 1][j] = dp[i][j];
}
rep(j, 1LL << 20) {
if (j & d) continue;
dp[i + 1][j | d] += dp[i][j];
}
}
ll ans = 0;
rep(i, 1LL << 20) ans += dp[B - A + 1][i];
cout << ans << "\n";
/*
rep(i, B - A + 2) {
rep(j, 10) {
cout << dp[i][j] << " ";
}
cout << "\n";
}
*/
} | #include <iostream>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <set>
#include <algorithm>
#include <iomanip>
#include <string.h>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) (a).begin(),(a).end()
typedef long long lint;
using namespace std;
int main(){
int N;
cin>>N;
lint A[N];
REP(i,N)cin>>A[i];
lint ans=0;
lint dmax=0;
lint s=0;
lint p=0;
REP(i,N){
s+=A[i];
dmax=max(dmax,s);
ans=max(ans,p+s);
ans=max(ans,p+dmax);
p+=s;
}
cout<<ans<<endl;
return 0;
}
|
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<utility>
#include<bitset>
using namespace std;
int main(int argc, char *argv[])
{
int n, k;
cin >> n >> k;
int sum = 0;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= k; j++) {
sum += i * 100 + j;
}
}
cout << sum << endl;
return 0;
}
| #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <stdlib.h>
#include <math.h>
#include <numeric>
using namespace std;
int main()
{
int N, K;
cin >> N >> K;
cout << (1 + N) * N / 2 * K * 100 + (1 + K) * K / 2 * N;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int k;
long long x;
int main()
{
scanf("%lld%d",&x,&k);
while(k--)
{
if(x%200) x=x*1000+200;
else x/=200;
}
printf("%lld",x);
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(long long i=0;i<(long long)(n);i++)
#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)
#define pb emplace_back
#define lb(v,k) (lower_bound(all(v),(k))-v.begin())
#define ub(v,k) (upper_bound(all(v),(k))-v.begin())
#define fi first
#define se second
#define pi M_PI
#define PQ(T) priority_queue<T>
#define SPQ(T) priority_queue<T,vector<T>,greater<T>>
#define dame(a) {out(a);return 0;}
#define decimal cout<<fixed<<setprecision(15);
#define all(a) a.begin(),a.end()
#define rsort(a) {sort(all(a));reverse(all(a));}
#define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());}
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> PP;
typedef tuple<ll,ll,ll,ll> PPP;
using vi=vector<ll>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
using vvvvi=vector<vvvi>;
using vp=vector<P>;
using vvp=vector<vp>;
using vb=vector<bool>;
using vvb=vector<vb>;
const ll inf=1001001001001001001;
const ll INF=1001001001;
const ll mod=1000000007;
const double eps=1e-10;
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';}
template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';}
template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);}
template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);}
template<class T> void yesno(T b){if(b)out("yes");else out("no");}
template<class T> void YesNo(T b){if(b)out("Yes");else out("No");}
template<class T> void YESNO(T b){if(b)out("YES");else out("NO");}
template<class T> void outset(T s){auto itr=s.begin();while(itr!=s.end()){if(itr!=s.begin())cout<<' ';cout<<*itr;itr++;}cout<<'\n';}
void outs(ll a,ll b){if(a>=inf-100)out(b);else out(a);}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
ll modpow(ll a,ll b){ll res=1;a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;}
int main(){
ll n,k;cin>>n>>k;
rep(i,k){
if(n%200){
n=n*1000+200;
}
else n/=200;
}
out(n);
} |
// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(a) a.begin(), a.end()
#define sz(x) x.size()
#define yn(p) cout << (p?"Yes":"No") << endl;
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define Pr pair<ll,ll>
#define Tp tuple<ll,ll,ll>
using Graph = vector<vector<int>>;
const int INF = 1000000000;
const int MOD = 1000000007;
const ll llINF = 1000000000000000000;
/**
#include <atcoder/all>
using namespace atcoder;
#include <boost/multiprecision/cpp_int.hpp>
using lll = boost::multiprecision::cpp_int;
using mint = modint1000000007;
using mint2 = modint998244353;
**/
void solve() {
int x,y;
cin>>x>>y;
cout<<(6-x-y)%3<<endl;
}
int main() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(15);
solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main(){
int n,w;
cin >> n >> w;
cout << n/w <<endl;
} |
#include <bits/stdc++.h>
using namespace std;
int main(){
string S;
int64_t M,z=0;
cin>>S>>M;
int64_t N=S.size(),K=*max_element(S.begin(),S.end())-'0';
if(N<2){
cout<<(M<K?0:1);
return 0;
}
int64_t ok=0,ng=M+10;
while(ng-ok>1){
__int128 m=(ok+ng)/2,sum=0,temp=1,l=0;
for(int j=N-1;j>=0;temp*=m,j--){
sum+=(S[j]-'0')*temp;
if(temp>M){
l=1;
break;
}
}
if(l||sum>M)ng=m;
else ok=m;
}
cout<<max(z,ok-K);
} | #include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>
char x[110];
std::vector<int> V;
int main()
{
long long int b;
scanf("%s",x+1);
scanf("%lld",&b);
int a = strlen(x+1);
if(a==1)
{
if(x[1]-'0'<=b) printf("1");
else printf("0");
return 0;
}
for(int i=1;i<=a;i++) V.push_back(x[i]-'0');
long long int min = 2;
for(int i=0;i<V.size();i++) min = min>V[i]+1?min:V[i]+1;
long long int C = min;
long long int ans = min-1;
long long int max = (long long int)1e18;
while(min<=max)
{
long long int h = (min+max)/2;
long long int sum = 0;
for(int i=0;i<V.size();i++)
{
if(sum > (double)(b-V[i])/h)
{
max = h-1;
goto u;
}
sum *= h;
sum += V[i];
}
ans = h;
min = h+1;
u:;
}
//printf("%lld %lld!!\n",ans,C);
printf("%lld",ans-C+1);
} |
#include<bits/stdc++.h>
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
#define ll long long
#define rep(i,n) for (int i = 0; i < (n); i++)
int main()
{
int N;
string S;
cin>>N>>S;
int ans=0;
rep(i,S.size()){
int cnt1=0;
int cnt2=0;
for(int j=i;j<S.size();j++){
if(S[j]=='A') cnt1++;
else if(S[j]=='T') cnt1--;
else if(S[j]=='G') cnt2++;
else cnt2--;
if(cnt1==0 && cnt2==0){
ans++;
}
}
}
cout<<ans<<endl;
return 0;
}
| #include <bits/stdc++.h>
#define sort stable_sort
using namespace std;
int _;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
int n,ans,cnt[26][100050];
string s,str="ATCG";
int main()
{
//for(scanf("%d",&_);_;_--)
scanf("%d",&n);
cin >> s;
for(int i=1;i <= n;i++)
for(int j=0;j<4;j++)
{
cnt[str[j]-'A'][i]=cnt[str[j]-'A'][i-1]+(s[i-1] == str[j]);
// D(i,str[j],cnt[str[j]-'A'][i]);
}
for(int i=0;i<n;i++)
for(int j=0;j <= i;j++)
if(cnt['A'-'A'][i+1]-cnt['A'-'A'][j] == cnt['T'-'A'][i+1]-cnt['T'-'A'][j] && cnt['C'-'A'][i+1]-cnt['C'-'A'][j] == cnt['G'-'A'][i+1]-cnt['G'-'A'][j])
{
// D(i,j);
ans++;
}
printf("%d\n",ans);
return 0;
} |
#include "bits/stdc++.h"
//#include "atcoder/all"
using namespace std;
//using namespace atcoder;
//using mint = modint1000000007;
//const int mod = 1000000007;
//using mint = modint998244353;
//const int mod = 998244353;
//const int INF = 1e9;
//const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n-1); i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define endl "\n"
vector<int>to[100005];
int dp[100005];
int sz[100005];
void dfs(int v, int p = -1) {
int csub = 0;
dp[v] = 1;
sz[v] = 1;
vector<int>c;
for (int e : to[v]) {
if (p == e) {
continue;
}
dfs(e, v);
sz[v] += sz[e];
int num = dp[e];
if ((num < 0) && (0 == (sz[e] % 2))) {
//case1
dp[v] += num;
}
else if ((num >= 0) && (0 == (sz[e] % 2))) {
//case2
csub += num;
}
else {
//case3
c.push_back(num);
}
}
sort(all(c));
c.push_back(csub);
rep(i, c.size()) {
if (0 == i % 2) {
dp[v] += c[i];
}
else {
dp[v] -= c[i];
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
rep(i, N - 1) {
int p;
cin >> p;
p--;
to[i + 1].push_back(p);
to[p].push_back(i + 1);
}
dfs(0);
int ans = (dp[0] + N) / 2;
cout << ans << endl;
return 0;
} | // Problem: C - MAD TEAM
// Contest: AtCoder - ZONe Energy Programming Contest
// URL: https://atcoder.jp/contests/zone2021/tasks/zone2021_c
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int,int>;
#define pb push_back
#define mp make_pair
const double PI = 4*atan(1);
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1e9+7;
const int MAX_N = 3002;
int n;
ll a[MAX_N][5];
void solve()
{
int lo=1, hi=1e9+1;
while(lo+1<hi){
int mid=(lo+hi)/2;
vector<int> b(32, 0);
for(int i=0;i<n;++i){
int f = 0;
for(int j=0;j<5;++j)if(a[i][j]>=mid) f|=(1<<j);
b[f]++;
}
bool found=false;
for(int i=0;i<32;++i)if(b[i])for(int j=i;j<32;++j)if(b[j])
for(int k=j;k<32;++k)if(b[k]){
if((i|j|k) == 31 && b[i]+b[j]+b[k]>=3) found=true;
}
if(!found) hi=mid;
else lo=mid;
}
cout<<lo<<'\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
cin >> n;
for(int i=0;i<n;++i)for(int j=0;j<5;++j)cin>>a[i][j];
solve();
return 0;
} |
// Author : heyuhhh
// Created Time : 2020/12/18 17:02:09
#include<bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 1e5 + 5;
void run() {
int n, m;
cin >> n >> m;
vector<int> a(n), b(m);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < m; i++) {
cin >> b[i];
}
vector<vector<int>> dp(n + 1, vector<int>(m + 1));
for (int i = 0; i <= n; i++) {
dp[i][0] = i;
}
for (int j = 0; j <= m; j++) {
dp[0][j] = j;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
dp[i][j] = min({dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + (a[i - 1] != b[j - 1])});
}
}
cout << dp[n][m] << '\n';
}
int main() {
#ifdef Local
freopen("input.in", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
run();
return 0;
} | #include<bits/stdc++.h>
using namespace std;
const int N = 500000 + 10;
int n, a[N], x;
void Solve() {
scanf("%d %d", &n, &x);
for (int i = 1; i <= n; i++) {
scanf("%d", a + i);
if (x != a[i]) {
printf("%d ", a[i]);
}
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("std.out", "w", stdout);
clock_t st = clock();
#endif
int T = 1;
while (T--) {
Solve();
}
#ifndef ONLINE_JUDGE
clock_t ed = clock();
printf("time : %.6lfs\n", 1.0 * (ed - st) / CLOCKS_PER_SEC);
#endif
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
void solve()
{
int a[4];
int mx=INT_MAX;
int ans=INT_MAX;
for(int i=0;i<4;i++)
{
cin>>a[i];
if(a[i]<ans)
{
ans=a[i];
}
}
cout<<ans<<endl;
}
int main(){
solve();
return 0;
} | #include<iostream>
#include<string>
#include<map>
#include<set>
#include<algorithm>
#include<functional>
#include<vector>
#include<math.h>
#define rep(i,n) for (int i = 0, i < n; i++)
using namespace std;
int main(){
int a,b,w;
cin >> a >> b >> w;
w *= 1000;
int kmax = floor(double(w)/a), kmin = ceil(double(w)/b);
if (kmin * a <= w){
cout << kmin << " " << kmax << endl;
} else {
cout << "UNSATISFIABLE" << endl;
}
} |
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cctype>
#include <climits>
#define rep(i, n) for(int i = 0; i < n; i++)
#define per(i, n) for(int i = n - 1; i >= 0; i--)
using ll = long long;
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define mod 1000000007
using namespace std;
template<class T, class U>
T &chmax(T &a, const U &b){ if(a < b) a = b; return a; }
template<class T, class U>
T &chmin(T &a, const U &b){ if(a > b) a = b; return a; }
const int len = 400010;
vi f(len), g(len);
int main(){
rep(i, len) g[i] = i % 3;
int num = 3, cnt = 1;
while(num < len){
for(int i = 1; i * num < len; i++){
f[i * num] = cnt;
g[i * num] = i % 3;
}
cnt++;
num *= 3;
}
g[0] = 1;
//rep(i, 30) cout << g[i] << " ";
for(int i = 1; i < len; i++){
f[i] += f[i - 1];
(g[i] *= g[i - 1]) %= 3;
}
int n;
cin >> n;
vector<char> c(n);
int ans = 0;
rep(i, n){
cin >> c[i];
//n-1 C i
if(f[n - 1] != f[i] + f[n - i - 1]) continue;
if(c[i] == 'B'){
ans += 0 * g[n - 1] * g[i] * g[n - i - 1] % 3;
}
if(c[i] == 'W'){
ans += 1 * g[n - 1] * g[i] * g[n - i - 1] % 3;
}
if(c[i] == 'R'){
ans += 2 * g[n - 1] * g[i] * g[n - i - 1] % 3;
}
ans %= 3;
}
if(!(n & 1)) ans = (-ans + 3) % 3;
if(ans == 0) cout << "B\n";
if(ans == 1) cout << "W\n";
if(ans == 2) cout << "R\n";
} | #include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double db;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define pll pair<ll,ll>
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define ub(v,val) upper_bound(v.begin(),v.end(),val)
#define np(str) next_permutation(str.begin(),str.end())
#define lb(v,val) lower_bound(v.begin(),v.end(),val)
#define sortv(vec) sort(vec.begin(),vec.end())
#define rev(p) reverse(p.begin(),p.end());
#define v vector
#define pi 3.14159265358979323846264338327950288419716939937510
#define len length()
#define repc(i,s,e) for(ll i=s;i<e;i++)
#define fi first
#define se second
#define mset(a,val) memset(a,val,sizeof(a));
#define mt make_tuple
#define repr(i,n) for(i=n-1;i>=0;i--)
#define rep(i,n) for(i=0;i<n;i++)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define at(s,pos) *(s.find_by_order(pos))
#define set_ind(s,val) s.order_of_key(val)
long long int M = 1e9 + 7 ;
long long int inf = 9 * 1e18;
//CLOCK
ll begtime = clock();
#define time() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//CLOCK ENDED
ll n, m;
// modular exponentiation
ll binpow(ll val, ll deg)
{
if (deg < 0)
return 0;
if (!deg)
return 1 % M;
if (deg & 1)
return binpow(val, deg - 1) * val % M;
ll res = binpow(val, deg >> 1);
return (res * res) % M;
}
//binomial
ll modinv(ll n) {
return binpow(n, M - 2);
}
int main() {
// your code goes here
IOS;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll i, j, t, k, x, y, z, N;
cin >> x >> y >> z;
z *= 1000;
ll mn = 0, mx = 0;
v<ll> vec;
rep(i, 1000005) {
mn += x;
mx += y;
if (z >= mn && z <= mx) {
vec.pb(i + 1);
}
}
if (vec.size() == 0) cout << "UNSATISFIABLE";
else {
cout << vec.front() << ' ' << vec.back();
}
return 0;
}
|
//QwQcOrZ yyds!!!
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
#define ll long long
#define F(i,a,b) for (int i=(a);i<=(b);i++)
#define R(i,a,b) for (int i=(a);i<(b);i++)
#define D(i,a,b) for (int i=(a);i>=(b);i--)
#define go(i,x) for (int i=head[x];i;i=e[i].nx)
#define mp make_pair
#define pb push_back
#define pa pair < int,int >
#define fi first
#define se second
#define re register
#define be begin()
#define en end()
#define sqr(x) ((x)*(x))
#define ret return puts("-1"),0;
#define put putchar('\n')
#define inf 1000000005
#define mod 998244353
#define int ll
#define N 100005
using namespace std;
inline char gc(){static char buf[100000],*p1=buf,*p2=buf;return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;}
#define gc getchar
inline ll read(){char c=gc();ll su=0,f=1;for (;c<'0'||c>'9';c=gc()) if (c=='-') f=-1;for (;c>='0'&&c<='9';c=gc()) su=su*10+c-'0';return su*f;}
inline void write(ll x){if (x<0){putchar('-');write(-x);return;}if (x>=10) write(x/10);putchar(x%10+'0');}
inline void writesp(ll x){write(x),putchar(' ');}
inline void writeln(ll x){write(x);putchar('\n');}
int n,t[N],l[N],r[N],x,y,ans;
signed main()
{
n=read();
for (int i=1;i<=n;i++)
{
t[i]=read(),l[i]=read(),r[i]=read();
}
for (int i=1;i<=n;i++)
for (int j=i+1;j<=n;j++)
{
x=i,y=j;
if (l[x]>l[y]) swap(x,y);
if (l[y]<r[x]) ans++;else
if (l[y]==r[x])
{
if ((t[x]==1||t[x]==3)&&(t[y]==2||t[y]==1)) ans++;
}
}
writeln(ans);
}
/*
*/
| // Problem:
// F - Close Group
//
//
// Contest: AtCoder - AtCoder Beginner Contest 187
// URL: https://atcoder.jp/contests/abc187/tasks/abc187_f
// Memory Limit: 1024 MB
// Time Limit: 3000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 18;
int connect[N],f[1 << N];
int main()
{
int n,m;scanf("%d%d",&n,&m);
for(int i = 0;i < n;++i) connect[i] |= (1 << i);
for(int i = 0;i < m;++i)
{
int u,v;scanf("%d%d",&u,&v);
--u;--v;
connect[u] |= (1 << v);
connect[v] |= (1 << u);
}
f[0] = 0;
for(int i = 1;i < (1 << n);++i)
{
f[i] = 1e5+7;
bool indp = 1;
for(int j = 0;j < n;++j)
if((i >> j & 1) && (connect[j] & i) != i)
indp = 0;
if(indp) f[i] = 1;
for(int s = (i - 1) & i;s;s = (s - 1) & i)
f[i] = min(f[i],f[s] + f[i ^ s]);
}
printf("%d",f[(1 << n) - 1]);
return 0;
}
|
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, s, n) for (ll i = s; i < (n); i++)
const ll big = 1000000001;
int main(){
ll n; cin >> n;
vector<ll> m1(n, big), m2(n, -big);
rep(i, n){
ll x, c;
cin >> x >> c;
c--;
m1[c] = min(m1[c], x);
m2[c] = max(m2[c], x);
}
P now1 = P(0, 0); // now/sum
P now2 = P(0, 0); // now/sum
ll next1, next2;
rep(i, n){
if (m1[i] == big) continue;
next1 = now1.second + abs(now1.first-m2[i]) + abs(m1[i]-m2[i]);
next1 = min(next1, now2.second + abs(now2.first-m2[i]) + abs(m1[i]-m2[i]));
next2 = now1.second + abs(now1.first-m1[i]) + abs(m1[i]-m2[i]);
next2 = min(next2, now2.second + abs(now2.first-m1[i]) + abs(m1[i]-m2[i]));
now1 = P(m1[i], next1);
now2 = P(m2[i], next2);
}
ll ans = min(abs(now1.first)+now1.second, abs(now2.first)+now2.second);
cout << ans << endl;
return 0;
} | #include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <cmath>
#include <iomanip>
#include <bits/stdc++.h>
using namespace std;
vector<pair<int,int>>ve;
int N,l,r,L,R;
long long LL,RR,tr,tl;
string getss(){//็จgetchar()่ฏปๅ
ฅstring
string k="";
char a;
a=getchar();
while(a==-1||a==32||a==10)a=getchar();
while(a!=-1&&a!=32&&a!=10){
k+=a;
a=getchar();
}
return k;
}
int main(){
scanf("%d",&N);
for(int i=0;i<N;i++){
int a,b;
scanf("%d%d",&a,&b);
ve.push_back(make_pair(b,a));
}
sort(ve.begin(),ve.end());
pair<int,int>cur;
cur=*ve.begin();
LL=RR=l=r=L=R=0;
L=cur.second;
R=cur.second;
for(pair<int,int>g:ve){
if(g.first==cur.first){
L=min(L,g.second);
R=max(R,g.second);
} else{
cur=g;
tr=R-L+min(LL+abs(L-l),RR+abs(L-r));
tl=R-L+min(LL+abs(R-l),RR+abs(R-r));
RR=tr;
LL=tl;
//cout<<RR<<" "<<LL<<endl;
l=L;
r=R;
L=R=cur.second;
//cout<<l<<" "<<r<<" "<<L<<" "<<R<<endl;
}
}
tr=R-L+min(LL+abs(L-l),RR+abs(L-r));
tl=R-L+min(LL+abs(R-l),RR+abs(R-r));
RR=tr;
LL=tl;
//cout<<RR<<" "<<LL<<endl;
//cout<<l<<" "<<r<<" "<<L<<" "<<R<<endl;
printf("%lld",min(LL+abs(L),RR+abs(R)));
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define Test int T;cin>>T;while(T--)
#define PI acos(-1)
#define endl "\n"
#define fx(x) fixed<<setprecision(x)
#define sz(s) (int)s.size()
#define all(v) (v).begin(),(v).end()
#define allr(v) (v).rbegin(),(v).rend()
#define mem(a,n) memset((a),n,sizeof (a))
#define INF 1e9
#define ii pair<ll,ll>
ll gcd(ll x,ll y){return(!y?x:gcd(y,x%y));}
ll lcm(ll x,ll y){return x/gcd(x,y)*y;}
void file(){
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#else
//freopen("bridges.in", "r", stdin);
//freopen("bridges.out", "w", stdout);
#endif
}
void fast(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
file();
}
int dx[]= {-1,0,1,0};
int dy[]= {0,1,0,-1};
const double eps=1e-9;
const int mod=1e9+7;
const int N=3e5+5;
int main(){
fast();
int a,b,c;
cin>>a>>b>>c;
if(c==0){
if(a>b)cout<<"Takahashi";
else cout<<"Aoki";
}
else{
if(a>=b)cout<<"Takahashi";
else cout<<"Aoki";
}
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int a,b,c;
cin>>a>>b>>c;
if(c==0)
{
if(a<=b)
{
cout<<"Aoki\n";
}
else
cout<<"Takahashi\n";
}
else if(c==1)
{
if(a>=b)
cout<<"Takahashi\n";
else
cout<<"Aoki\n";
}
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("-Ofast")
//#pragma GCC optimize("trapv")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx2,tune=native")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-funroll-loops")
#define vi vector<int>
#define vii vector<pair<int, int>>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define loop(_) for (int __ = 0; __ < (_); ++__)
#define forn(i, n) for (int i = 0; i < n; ++i)
using namespace std;
using ll = long long;
using ld = long double;
const int N = 5e5 + 7;
const ll mod = 1e9 + 7;
const ll inf = 2e18;
auto ra = [] {char *p = new char ; delete p ; return ll(p) ; };
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count() * (ra() | 1));
int n;
int a, b, x, y;
int d[N];
vector<pair<int, int>> adj[N];
void edge(int u, int v, int w)
{
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
int dijkstra()
{
priority_queue<pair<int, int>> q;
q.push({0, a});
fill(d, d + N, 1e9);
d[a] = 0;
while (q.size())
{
pair<int, int> t = q.top();
q.pop();
for (auto u : adj[t.second])
{
if ( u.second - t.first < d[u.first])
{
d[u.first] = u.second - t.first;
q.push({-d[u.first], u.first});
}
}
}
return d[b + 100];
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("in.in", "r", stdin);
cin >> a >> b >> x >> y;
for (int i = 1; i <= 100; ++i)
{
edge(i, i + 100, x);
if (i < 100)
{
edge(i, i + 1, y);
edge(i + 100, i + 101, y);
edge(i + 100, i + 1, x);
}
}
cout << dijkstra();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
long a,b,x,y,ans;
cin>>a>>b>>x>>y;
ans=0;
if(a-b==1 || a==b){
ans=x;
}
else if(a>b){
ans=x+min(y,2*x)*(abs(a-b)-1);
}
else{
ans=x+min(y,2*x)*(abs(a-b));
}
cout<<ans;
} |
#include<bits/stdc++.h>
using namespace std;
int n,m,h[200003],w[200003];
long long sum[2][200003],ans=2333333333333333;
int main(){
cin>>n>>m;
for(int i=0;i<n;i++)
scanf("%d",h+i);
for(int i=0;i<m;i++)
scanf("%d",w+i);
sort(w,w+m);
sort(h,h+n);
for(int i=0;i+1<n;i+=2)
sum[0][i+2]=sum[0][i]+h[i+1]-h[i];
for(int i=n-1;i>0;i-=2)
sum[1][n+1-i]=sum[1][n-1-i]+h[i]-h[i-1];
for(int i=0,j=0;i<m;i++){
while(j!=n&&w[i]>h[j])j++;
int l=int(j/2)*2,r=n-1-l;
int LST=j;if(j%2)LST--;
ans=min(ans,sum[0][2*int(l/2)]+sum[1][2*int(r/2)]+int(fabs(w[i]-h[LST])));
}
cout<<ans;
} | #include<bits/stdc++.h>
#define LL long long
#define ull unsigned long long
#define F(i,j,k) for(int i=(j);i<=(k);i++)
#define DF(i,j,k) for(int i=(j);i>=(k);i--)
#define P pair
#define dui priority_queue
using namespace std;
template<typename T>inline void read(T &n){
T w=1;n=0;char ch=getchar();
while(!isdigit(ch)&&ch!=EOF){if(ch=='-')w=-1;ch=getchar();}
while(isdigit(ch)&&ch!=EOF){n=(n<<1)+(n<<3)+(ch^48);ch=getchar();}
n*=w;
}
template<typename T>inline void write(T x){
ull y=0;
T l=0;
if(x<0){x=-x;putchar('-');}
if(!x){putchar(48);return;}
while(x){y=y*10+x%10;x/=10;l++;}
while(l){putchar(y%10+48);y/=10;l--;}
}
template<typename T>inline void writeln(T x){
write(x);
puts("");
}
template<typename T>inline void writes(T x){
write(x);
putchar(' ');
}
template<typename T>inline void checkmax(T &a,T b){a=a>b?a:b;}
template<typename T>inline void checkmin(T &a,T b){a=a<b?a:b;}
const int N=1e5+100;
LL ans,a[N],b[N],c[N];
int main(){
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
int n;read(n);
F(i,1,n)read(a[i]);F(i,1,n)read(b[i]);
F(i,1,n)if(i&1)ans+=a[i],c[i]=b[i]-a[i];else ans+=b[i],c[i]=a[i]-b[i];
sort(c+1,c+n+1);
F(i,n/2+1,n)ans+=c[i];
writeln(ans);
return 0;
}
|
#include <bits/stdc++.h>
#define ll long long
#define map unordered_map
#define set unordered_set
#define l_l pair<ll, ll>
#define P pair<ll, ll>
#define vll vector<ll>
#define mll map<ll, ll>
#define mp make_pair
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define rev(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define revs(i, n) for (int i = ((int)(n)); i > 0; --i)
using namespace std;
const ll MOD = 1000000007LL;
const ll INF = (1LL << 60LL);
// template <class T> void plus_mod(T &a, const T &b) {a = (a + b) % MOD;}
int main() {
ll N;
scanf("%lld", &N);
struct record {
ll t;
ll l;
ll r;
};
vector<record> rlist;
rep(i, N) {
ll t, l, r;
scanf("%lld %lld %lld", &t, &l, &r);
rlist.emplace_back(record{t, l, r});
}
ll ans = 0;
for (ll i = 0; i < N - 1; i++) {
for (ll j = i + 1; j < N; j++) {
auto rec1 = rlist[i];
auto rec2 = rlist[j];
if (rec1.r < rec2.l || rec2.r < rec1.l) {
// ๅ
จใ้ใชใใชใ
continue;
}
// ใกใใใฉ้ใชใใใฟใผใณ
if (rec1.r == rec2.l) {
bool ok1 = (rec1.t == 1 || rec1.t == 3);
bool ok2 = (rec2.t == 1 || rec2.t == 2);
if (!(ok1 && ok2)) {
continue;
}
}
// ใกใใใฉ้ใชใใใฟใผใณ
if (rec2.r == rec1.l) {
bool ok1 = (rec2.t == 1 || rec2.t == 3);
bool ok2 = (rec1.t == 1 || rec1.t == 2);
if (!(ok1 && ok2)) {
continue;
}
}
//
// cout << "i:" << (i) << ",j:" << (j) << endl;
ans++;
}
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
#define fo(i,a,b) for(int i=a;i<=b;i++)
#define fod(i,a,b) for(int i=a;i>=b;i--)
#define me0(a) memset(a,0,sizeof(a))
#define me1(a) memset(a,-1,sizeof(a))
#define op freopen("in.txt", "r", stdin)
#define op1 freopen("C:\\acm\\Cproj\\in.txt","r",stdin);
#define pr freopen("C:\\acm\\Cproj\\out.txt","w",stdout)
#define pr2 freopen("C:\\acm\\Cproj\\std.txt","w",stdout)
#define pii pair<int,int>
#define Please return
#define AC 0
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long LL;
template <class T>
void read(T &val) { T x = 0; T bz = 1; char c; for (c = getchar(); (c<'0' || c>'9') && c != '-'; c = getchar()); if (c == '-') { bz = -1; c = getchar(); }for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - 48; val = x * bz; }
const int mod=998244353;
const int maxn = 1e6+10;
int n,m,a[maxn],q,t,k;
char s[maxn];
int p[maxn];
char win[110][110];
char bat(char x,char y){
if(x == y) return x;
if(x == 'R' && y == 'S') return x;
if(x == 'S' && y == 'P') return x;
if(x == 'P' && y == 'R') return x;
return y;
}
int main(){
read(n);read(k);
scanf("%s",s);
p[0]=1;
fo(i,1,k) p[i]=p[i-1]*2%n;
fo(i,0,n-1) {
win[i][0] = s[i];
}
fo(i,1,k){
fo(j,0,n-1){
win[j][i] = bat(win[j][i-1],win[(j+p[i-1])%n][i-1]);
}
}
printf("%c\n",win[0][k]);
Please AC;
} |
#include <iostream>
#include <vector>
#include <algorithm>
#include <array>
using namespace std;
typedef long long ll;
vector<ll> X, C;
vector<array<ll, 2>> v;
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int n; cin >> n; X.resize(n);
C.resize(n); v.resize(n + 1, { (1 << 30), -(1 << 30) });
for (int i = 0; i < n; i++) cin >> X[i] >> C[i];
v[0][0] = v[0][1] = 0; for (int i = 0; i < n; i++) {
v[C[i]][0] = min(v[C[i]][0], X[i]);
v[C[i]][1] = max(v[C[i]][1], X[i]);
}
vector<vector<ll>> dp(n + 1, vector<ll>(2, 0));
while (v[n][0] == (1 << 30)) { n--; }
dp[n][0] = v[n][1] - v[n][0] + abs(v[n][1]);
dp[n][1] = (v[n][1] - v[n][0]) + abs(v[n][0]);
for (int i = n - 1; i >= 0; i--) {
if (v[i][0] == (1 << 30)) {
dp[i][0] = dp[i + 1][0]; dp[i][1] = dp[i + 1][1];
v[i][0] = v[i + 1][0]; v[i][1] = v[i + 1][1];
continue;
}
ll d00 = abs(v[i][0] - v[i + 1][0]), d01 = abs(v[i][0] - v[i + 1][1]);
ll d10 = abs(v[i][1] - v[i + 1][0]), d11 = abs(v[i][1] - v[i + 1][1]);
dp[i][0] = v[i][1] - v[i][0] + min(d10 + dp[i + 1][0], d11 + dp[i + 1][1]);
dp[i][1] = v[i][1] - v[i][0] + min(d00 + dp[i + 1][0], d01 + dp[i + 1][1]);
}
cout << dp[0][0] << '\n';
cin.ignore(2); return 0;
} | #include <iostream>
#include <vector>
#include <map>
#include <stack>
#include <string>
#include <algorithm>
#include <set>
#include <sstream>
#include <time.h>
#include <bitset>
#include <iomanip>
#include <queue>
#include <assert.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rew(i, n) for (int i = 1; i < (int)(n); i++)
using namespace std;
typedef long long int ll;
const double PI = 3.14159265358979323846;
using Graph = vector <vector<pair<ll, ll>>>;
int main() {
ll maxval = 1e18;
ll N; cin >> N;
vector<bool>exist(N + 1, false);
vector<ll>left(N + 1, maxval), right(N + 1, -maxval);
rep(i, N) {
ll X, C; cin >> X >> C;
exist[C] = true;
left[C] = min(left[C], X);
right[C] = max(right[C],X);
}
ll lft = 0, rgt = 0;
ll posl = 0, posr = 0;
rep(i, N + 1) {
if (!exist[i])continue;
ll dist = right[i] - left[i];
ll temp1 = min(lft + abs(posl - left[i]) + dist, rgt + abs(posr - left[i]) + dist);
ll temp2 = min(rgt + abs(posr - right[i]) + dist,lft + abs(posl - right[i]) + dist);
rgt = temp1;
lft = temp2;
posl = left[i];
posr = right[i];
}
ll ans = min(lft + abs(posl), rgt + abs(posr));
cout << ans << endl;
} |
//Har Har Mahadev
using namespace std;
#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pb push_back
#define mp make_pair
#ifdef bhole_ka_saaya
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define booga cerr << "booga" << endl
#else
#define debug(...) ;
#define booga ;
#endif
template <typename T> std::ostream& operator<<(std::ostream& stream, const vector<T>& vec){ for(int x : vec)stream << x << ' '; return stream; }
template <typename T> std::istream& operator>>(std::istream& stream, vector<T>& vec) { for(int &x : vec)stream >> x;return stream; }
template <typename T> void operator+=(vector<T>& vec, const T value) { for(int &x : vec)x += value; }
template <typename T> void operator-=(vector<T>& vec, const T value) { for(int &x : vec)x -= value; }
template <typename T> void operator++(vector<T>& vec) { vec += 1; }
template <typename T> void operator++(vector<T>& vec,T) { vec += 1; }
template <typename T> void operator--(vector<T>& vec) { vec -= 1; }
template <typename T> void operator--(vector<T>& vec,T) { vec -= 1; }
template <typename T> void operator*=(vector<T>& vec, const T value) { for(int &x : vec)x *= value; }
template <typename T> void operator/=(vector<T>& vec, const T value) { for(int &x : vec)x /= value; }
template <typename A, typename B>string to_string(pair<A, B> p);
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) { return '"' + s + '"'; }
string to_string(char c) {string s;s += c;return s; }
string to_string(const char* s) {return to_string((string) s); }
string to_string(bool b) {return (b ? "1" : "0"); }
string to_string(vector<bool> v) {bool first = true;string res = "{";for (int i = 0; i < static_cast<int>(v.size()); i++) {if (!first) {res += ", ";}first = false;res += to_string(v[i]);}res += "}";return res;}
template <size_t N>string to_string(bitset<N> v) {string res = "";for (size_t i = 0; i < N; i++) {res += static_cast<char>('0' + v[i]);}return res;}
template <typename A>string to_string(A v) {bool first = true;string res = "{";for (const auto &x : v) {if (!first) {res += ", ";}first = false;res += to_string(x);}res += "}";return res;}
template <typename A, typename B>string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p) {return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";}
template <typename T> void ckmin(T& change, T val){change = min(change,val);}
template <typename T> void ckmax(T& change, T val){change = max(change,val);}
void debug_out() { cerr << endl; } template <typename Head, typename... Tail>void debug_out(Head H, Tail... T) {cerr << " " << to_string(H);debug_out(T...);}
void bharo(int N_N) { return; }template <typename Head, typename... Tail>void bharo(int N_N, Head &H, Tail & ... T) {H.resize(N_N);bharo(N_N,T...);}
void safai() { return; }template <typename Head, typename... Tail>void safai(Head &H, Tail & ... T) {H.clear();safai(T...);}
void testcase(){
int n;
cin >> n;
vector<ll> a(n);
for(int i = 0; i < n; i++){
cin >> a[i];
}
ll lar = -1,sum = 0,here = 0;
for(int i = 0; i < n; i++){
ckmax(lar,a[i]);
sum += a[i];
here += sum;
cout << here + lar*(ll)(i+1) << '\n';
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int tt = 1;
//cin >> tt;
while(tt--){
testcase();
}
return 0;
}
| #include <cstdio>
#define uint unsigned int
#define i64 long long
#define u64 unsigned i64
int Max(int a, int b) { return (a > b) ? a : b; }
int Min(int a, int b) { return (a < b) ? a : b; }
int rint()
{
int x = 0, fx = 1; char c = getchar();
while (c < '0' || c > '9') { fx ^= (c == '-' ? 1 : 0); c = getchar(); }
while ('0' <= c && c <= '9') { x = (x << 3) + (x << 1) + (c ^ 48); c = getchar(); }
if (!fx) return -x;
return x;
}
const int MAX_n = 2e5;
int n, v[MAX_n + 5];
int maxv[MAX_n + 5];
i64 dp[MAX_n + 5];
i64 sum[MAX_n + 5];
int main()
{
n = rint();
for (int i = 1; i <= n; i++)
{
v[i] = rint();
sum[i] = sum[i - 1] + v[i];
maxv[i] = Max(maxv[i - 1], v[i]);
dp[i] = dp[i - 1] + sum[i];
printf("%lld\n", dp[i] + 1LL * maxv[i] * i);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll; //int:2*10**9
typedef long double ld;
typedef pair<ll,ll> P;
#define REP(i,n) for(ll i = 0; i<(ll)(n); i++)
#define FOR(i,a,b) for(ll i=(a);i<=(b);i++)
#define FORD(i,a,b) for(ll i=(a);i>=(b);i--)
#define vec2(name,i,j,k) vector<vector<ll>> name(i,vector<ll>(j,k))
#define vec3(name,i,j,k,l) vector<vector<vector<ll>>> name(i,vector<vector<ll>>(j,vector<ll>(k,l)))
#define pb push_back
#define MOD 1000000007 //998244353
#define PI 3.141592653
#define INF 100000000000000 //14
//cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);
int main(){
ll n, x; cin >> n >> x;
map<ll,ll> p;
REP(i,n) {
ll a,b,c; cin >> a >> b >> c;
p[a]+=c;
p[b+1]-=c;
}
ll lastat = 0;
ll ans = 0;
ll nowpay = 0;
for (auto ite=p.begin();ite!=p.end();ite++) {
ll nowat = ite->first;
ans += (nowat-lastat)*min(x,nowpay);
ll plus = ite->second;
nowpay+=plus;
lastat = nowat;
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb(x) push_back(x)
#define mp make_pair
#define sz(x) (int)x.size()
#define mem(x ,y) memset(x , y , sizeof x )
#define all(a ) a.begin() , a.end()
#define pii pair<int,int >
#define endl "\n"
#define forn(i,x,y ) for(int i= x; i<=y ; i++ )
#define ford(i,x,y ) for(int i= x; i>=y ; i-- )
#define rep(i,n ) for(int i= 0; i<n ; i++ )
#define repi(i,n ) for(int i= 1; i<=n ; i++ )
#define repit(i, c) for( __typeof((c).begin()) i = (c).begin(); i != (c).end();++i )
#ifndef ONLINE_JUDGE
#define dbg(args...) do { cout << #args << ' ' ; print(args); } while(0); cerr<< endl ;
#else
#define dbg(args...) ;
#endif
typedef long long ll ;
typedef unsigned long long ull ;
ll mod = 1e9+7, mod2 = 998244353;
int setbit(int n, int pos ) { return n = n|(1LL<< pos) ; }
int resetbit(int n,int pos ) { return n = n & ~(1LL<<pos ); }
bool checkbit(int n,int pos) { return (bool ) (n&(1LL<<pos)) ; }
template<typename T>T add(T x, T y, T mod = mod ){ x%= mod ; y%= mod ; return (x+y)%mod ; }
template<typename T>T sub(T x, T y, T mod = mod ){ return ((x-y)%mod+mod )%mod; }
template<typename T>T mul(T x, T y, T mod = mod ){ x %= mod ; y %= mod ; return (x*y)%mod ; }
template<typename T> void print(const T& v) { cout << v << ' ' ;}
template<typename T1,typename... T2> void print( const T1& first, const T2&... rest ){ print(first); print(rest...) ;}
template<typename T> void dbg_a(T a[] ,int n=10 ) {cerr << "[ "; for(int i=0;i <= n ; i++ )cerr<<a[i]<<' ' ; cerr<< " ]" << endl; }
template<typename F,typename S>ostream& operator<<( ostream& os, const pair< F, S > & p ){return os << "[ " << p.first << ", " << p.second << " ]";}
template<typename T>ostream &operator<<(ostream & os, const vector< T > &v ){os << "[ "; for(int i=0; i<sz(v) ; i++ ) os << v[i] << ' ' ; return os << " ]\n" ; }
template<typename T>ostream &operator<<(ostream & os, const map< T ,T> &Map ){os << "[ "; repit(it , Map ) os << "[" <<(*it).ff << ' ' << (*it).ss << "] " ; return os << "]\n" ; }
template<typename T>ostream &operator<<(ostream & os, const set< T > &Set ){os << "[ "; repit(it , Set ) os << *it << ' ' ; return os << " ]\n" ; }
template<typename T>ostream &operator<<(ostream & os, const multiset< T > &Set) {os << "[ "; repit(it , Set )os << *it << ' ' ; return os << " ]\n" ; }
#define int ll
const int maxn = (int) 1e5+ 123;
const int inf = 1e9; //0xc0 ;//0x3f ;
/////////////////////////////////////////////////
int n ,m,sum , k ;
int a[maxn ] , _p[maxn ] , _x[maxn ] ;
void solve() {
int x,y,z,p,q , tot = 0 , ans = 0 , mx =0 , mn ;
cin >> n ;
ans = inf +inf ;
rep( i ,n ) {
cin >> a[i] >> _p[i] >> _x[i] ;
if( _x[i ]-a[i] > 0 ) {
if( ans > _p[i] ) ans = _p[i] ;
}
}
if(ans > inf ) ans = -1 ;
cout << ans << endl ;
}
signed main() {
ios :: sync_with_stdio(false); cin.tie(0);
cout << fixed << setprecision( 12) ;
int t = 1 , cs =0 ;
// cin >> t ;
while(t--) {
solve() ;
}
return 0 ;
}
|
#include <bits/stdc++.h>
#define Rep(i,n,m) for(int i=(int)(n);i<(int)(m);i++)
#define rep(i,n) Rep(i,0,n)
#define all(v) v.begin(),v.end()
using namespace std;
using ll=long long;
using vi=vector<int>;
using vll=vector<ll>;
#define _GLIBCXX_DEBUG
int main(){
int N;cin>>N;
vector<int> a(N); rep(i,N) cin >> a[i];
sort(all(a));
bool test=true;
rep(i,N){
if(a[i]==i+1) continue;
else test=false;
}
if(test) cout << "Yes" << endl;
else cout << "No" << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
int main(){
string s, t, tmp;
cin >> s;
bool flag = false;
for(int i = 0; i < (int)s.size(); ++i){
if(s[i] == 'R'){flag = !flag;}
else if(s[i] != 'R' && flag == false){t += s[i];}
else{t.insert(0, {s[i]});}
}
if(flag == true){reverse(t.begin(), t.end());}
stack<char> stk;
if(t.size() == 0){cout << endl; return(0);}
for(int i = 0; i < t.size(); ++i){
if(stk.empty() || stk.top() != t[i]){stk.push(t[i]);}
else{stk.pop();}
}
char w;
string ans;
int loopnum = stk.size();
for(int i = 0; i < loopnum; ++i){
w = stk.top(); stk.pop(); ans += w;
}
reverse(ans.begin(), ans.end());
cout << ans << endl;
} |
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <numeric>
#include <utility>
#include <iomanip>
#include <limits>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#define debug(x) cout << #x << " = " << x << endl
#define fori(i, ini, lim) for(ll i = ll(ini); i < ll(lim); i++)
#define ford(i, ini, lim) for(ll i = ll(ini); i >= ll(lim); i--)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
ll g1(ll x) {
if (x == 0) return 0;
string s = to_string(x);
sort(begin(s), end(s), greater<char>());
return stoi(s);
}
ll g2(ll x) {
if (x == 0) return 0;
string s = to_string(x);
sort(begin(s), end(s));
return stoi(s);
}
ll f(ll x) {
return g1(x) - g2(x);
}
void solve() {
ll n, k;
cin >> n >> k;
ll x = n;
fori (i, 0, k) {
x = f(x);
}
cout << x << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
}
| #include<bits/stdc++.h>
#include<string>
using namespace std;
//functions like ceil() and floor() are used to truncate the decimal values
//countsort is great
#define ll long long
#define int long long
#define vi vector<int>
#define vlli vector<ll>
#define pii pair<int,int>
#define pb push_back
#define mpr make_pair
#define ft first
#define sec second
#define f0r(n) for(int i=0;i<n;i++)
#define f1r(n) for(int j=0;j<n;j++)
#define rep(i,a,b) for(int i=a;i<b;i++)
#define fastio(); ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define txtinp(); freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#define inp(n,arr); for(int i=0;i<n;i++){cin>>arr[i];}
#define srt(arr); sort(arr.begin(),arr.end());
#define rsrt(arr); sort(arr.rbegin(),arr.rend());
int dp[2];
int solve(int n){
vi arr;
while(n){
arr.pb(n%10);
n /= 10;
}
int s = arr.size();
srt(arr);
dp[0] = 0;dp[1] = 0;
f0r(s){
dp[0] = dp[0]*10 + arr[i];
dp[1] = dp[1]*10 + arr[s-1-i];
}
return abs(dp[0] - dp[1]);
}
signed main(){
fastio();
// txtinp();
int testc=1;//cin>>testc;
while(testc--){
int n,k;cin>>n>>k;
int ans = n;
f0r(k){
ans = solve(ans);
}cout<<ans<<endl;
}
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
return 0;
}
|
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define FI first
#define SE second
#define pb push_back
#define eb emplace_back
#define mod 998244353
#define all(c) (c).begin(),(c).end()
#define LB lower_bound
#define UB upper_bound
#define max3(a,b,c) max(c,max(a,b))
#define min3(a,b,c) min(c,min(a,b))
#define mems(s, n) memset(s, n, sizeof(s))
#define NINF -1e18
#define INF 1e18
#define int ll int
#define endl '\n'
#define double long double
#define OOK order_of_key //no of elements less than
#define FBO find_by_order //iterator pointing kth element;indexing starts from 0
#define CK3(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl
#define CK4(a,b,c,d) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<endl
typedef pair<int,int> PII;
typedef pair<pair<int,int>,int> PPII;
typedef pair<int,pair<int,int>> PIPI;
typedef map<int,int>MII;
typedef vector<int> VI;
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
int power(int a, int b)
{int x=1,y=a;while(b>0){if(b%2){x =(x*y)%mod;}y =(y*y)%mod;b/=2;}return x%mod;}
int fact[100005],fact_inv[100005];
int ncr(int n,int r){
if(n<r){return 0;}
if(n==r||r==0){return 1;}
return (((fact[n]*fact_inv[n-r])%mod)*fact_inv[r])%mod;
//return x;
}
int n;
int dp[5005][15],vis[5005][12];
int solve(int m,int i)
{
if(vis[m][i]==true)
return dp[m][i];
vis[m][i]=true;
if(i==0)
{
if(n>=m&&m%2==0)
{
dp[m][i]=ncr(n,m);
}
else dp[m][i]=0;
return dp[m][i];
}
int ans=0;
for(int j=0;j<=n;j+=2)
{
int x=m-j*(1ll<<i);
if(x>=0)
{
ans+=(solve(x,i-1)*ncr(n,j))%mod;
ans%=mod;
}
}
dp[m][i]=ans;
return dp[m][i];
}
signed main()
{
ios::sync_with_stdio(false); cin.tie(0);
int T=1,T1=0;//cin>>T;
fact[0]=1;
for(int i=1;i<100005;i++){
fact[i]=(fact[i-1]*i)%mod;
fact_inv[i]=power(fact[i],mod-2);
}
while(T1++<T)
{//cout<<"Case #"<<T1<<": ";
int m;
cin>>n>>m;
int ans=solve(m,12);
cout<<ans<<endl;
}
return 0;
}
| #include<iostream>
using namespace std;
typedef long long int ll;
const int mod = 998244353;
int n;
ll fact[5001],inv[5001];
ll dp[5001][51];
ll po(ll a, int p){
if(p==0) return 1ll;
ll ret=po(a,p/2);
ret=ret*ret%mod;
if(p%2) ret=ret*a%mod;
return ret;
}
ll ncr(int n, int r){
return (fact[n]*inv[n-r]%mod)*inv[r]%mod;
}
ll solve(int m, int j){
if(m==0) return 1ll;
if((1<<j)>m) return 0;
if(dp[m][j]!=-1) return dp[m][j];
ll ans=0;
for(int i=0;i<=n and i*(1<<j)<=m;i+=2) ans=(ans+ncr(n,i)*solve(m-i*(1<<j),j+1)%mod)%mod;
return dp[m][j]=ans;
}
int main(){
int m;
cin>>n>>m;
fact[0]=1ll;
inv[0]=1ll;
for(ll i=1;i<=5000;i++){
fact[i]=fact[i-1]*i%mod;
inv[i]=po(fact[i],mod-2);
}
for(int i=0;i<5001;i++) for(int j=0;j<51;j++) dp[i][j]=-1;
cout<<solve(m,0)<<"\n";
}
|
#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
#define ll long long
#define _GLIBCXX_DEBUG
const ll MOD = 1000000007;
const ll Mod = 998244353;
const int MAX = 510000;
const double PI = 3.14159265358979;
const vector<int> dx = {1, 0};
const vector<int> dy = {0, 1};
// mod. m ใงใฎaใฎ้ๅ
a^{-1}ใ่จ็ฎใใ
ll modinv(ll a, ll m) {
ll b = m, u = 1LL, v = 0LL;
while (b) {
ll t = a/b;
a -= t*b;
swap(a, b);
u -= t*v;
swap(u, v);
}
u %= m;
if (u<0) u += m;
return u;
}
int main() {
ll A, B, C;
cin >> A >> B >> C;
ll res = A%Mod;
res = (res%Mod)*(B%Mod)%Mod;
res = (res%Mod)*(C%Mod)%Mod;
res = (res%Mod)*((A+1)%Mod)%Mod;
res = (res%Mod)*((B+1)%Mod)%Mod;
res = (res%Mod)*((C+1)%Mod)%Mod;
cout << res*modinv(8, Mod)%Mod << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
int T;
ll N;
const ll MOD = 998244353;
ll mult(ll a, ll b){
return (a*b)%MOD;
}
ll fexp(ll b, ll e){
ll res = 1;
for(;e;e>>=1){
if(e&1)res = (res*b)%MOD;
b = (b*b)%MOD;
}
return res;
}
// INVERSO MOLTIPLICATIVO
ll inv(ll a){
return fexp(a,MOD-2);
}
int main(){
const ll mod = 998244353;
ll a, b, c;
cin >> a >> b >> c;
cout <<mult(mult(mult(a,a+1),mult(mult(c,c+1),mult(b,b+1))),inv(8));
return 0;
}
|
#include <bits/stdc++.h>
#include <unordered_set>
#include <algorithm>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
using vs = vector<string>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repll(i,n) for (ll i = 0; i < (ll)(n); i++)
#define fore(x,a) for(auto&(x) : (a))
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
#define ALL(a) (a).begin(), (a).end()
const ll INFL = 1e18;
const int INFI = 1e9;
const int MOD = 1e9 + 7;
int main(){
int N;
string S;
cin >> N >> S;
vll x(N+1);
rep(i,N+1) {
cin >> x[i];
}
ll M = INFL;
rep(i,N) {
chmin(M,abs(x[i]-x[i+1]));
}
vector<vector<ll>> res(M,vector<ll>(N+1));
for (int j = 0; j <= N; j++) {
ll r = x[j] % M;
for (int i = 0; i < M; ++i) {
res[i][j] = x[j] / M;
if (i < r) res[i][j]++;
}
}
cout << M << endl;
for (int i = 0; i < M; ++i) {
for (int j = 0; j <= N; ++j) cout << res[i][j] << " ";
cout << endl;
}
} | #include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int maxn=1e2+10;
int arr[maxn];
string s;
int n;
bool judge(int x){
for(int i=0;i<n;i++){
int a=arr[i]/x,b=arr[i+1]/x;
int aa=arr[i]%x,bb=arr[i+1]%x;
if(s[i]=='<'){
if(a>=b) return false;
if(b-a==1){
if(aa>bb) return false;
}
}
else{
if(a<=b) return false;
if(a-b==1){
if(aa<bb) return false;
}
}
}
return true;
}
int main()
{
cin>>n;
cin>>s;
for(int i=0;i<=n;i++) scanf("%d",arr+i);
int l=0,r=20000;
int x=0;
while(l<=r){
int mid=(l+r)>>1;
//cout<<l<<","<<r<<":"<<mid<<endl;
if(judge(mid)){
x=max(x,mid);
l=mid+1;
}
else r=mid-1;
}
printf("%d\n",x);
for(int i=0;i<x;i++){
for(int j=0;j<=n;j++)
printf("%d ",arr[j]/x+(i<arr[j]%x));
putchar('\n');
}
return 0;
}
/*
3
<><
3 8 6 10
3
<><
8 3 6 10
*/
|
#include <bits/stdc++.h>
#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define pb push_back
#define pob pop_back
#define pof pop_front
#define pf push_front
#define mp make_pair
#define ff first
#define ss second
#define endl "\n"
#define rep(i,a,b) for(ll i=a; i<b; i++)
#define rev(i,a,b) for(ll i=a; i>=b; i--)
#define decimal(n) fixed << setprecision(n)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define sz(v) v.size()
#define total_sum(v) accumulate(all(v),0)
#define make_unique(v) v.erase(unique(all(v)),v.end())
#define n_p(v) next_permutation(all(v))
#define p_p(v) prev_permutation(all(v))
#define for_each(it, X) for(__typeof((X).begin()) it = (X).begin(); it != (X).end(); it++)
#define re return
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair < int, int > pii;
typedef pair < ll, ll > pll;
typedef vector < pii > vii;
typedef vector < pll > vll;
typedef vector < int > vi;
typedef vector < ll > vl;
typedef vector < string > vs;
ll gcd ( ll , ll );
ll lcm ( ll , ll );
bool isPrime( ll );
ll power( ll , ll );
ll fact( ll );
void sieve_of_eratosthenes ( ll , vl &a);
//---------------------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------------
void solve()
{
ll a,b,c;
cin>>a>>b>>c;
if(c) {
if(b<=a) {
c=1;
}
else
c=0;
}
else {
if(a<=b) {
c=0;
}
else
c=1;
}
if(c) {
cout<<"Takahashi";
}
else
cout<<"Aoki";
}
int main()
{
boost;
ll t=1;
// cin>>t;
while(t--)
{
solve();
}
}
//---------------------------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------------
// HCF
ll gcd ( ll a , ll b ) {
return __gcd ( a , b ) ;
/*
if ( a==0 )
return b ;
return gcd ( b%a , a ) ;
*/
}
// LCM
ll lcm ( ll a , ll b ) {
return ( a * b ) / ( __gcd ( a , b ) ) ;
}
// PRIME
bool isPrime( ll n )
{
ll i=2;
for( i ; i*i<=n ; i++)
{
if( n % i == 0)
return false;
}
return true;
}
// POWER
ll power( ll b , ll a ) {
if(a==0) {
return 1;
}
ll c=1;
rep(i,0,a) {
c*=b;
}
return c;
}
// FACTORIAL
ll fact(ll n) {
ll c=1;
while(n>1) {
c*=n;
n--;
}
return c;
}
// SIEVE of ERATOSTHENES
void sieve_of_eratosthenes (ll n, vl &a) {
a[1]=0;
for(ll i=2; i*i<=n ; i++) {
if(a[i]==1) {
for(ll j=i+i; j<=n ; j+=i) {
a[j]=0;
}
}
}
}
//-------------------------------------------------------------*/--------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------------------------
| #include <iostream>
using namespace std;
int main(void){
// Your code here!
int a, b, c;
cin >> a >> b >> c;
if (c == 0) {
if (a > b) {
cout << "Takahashi" << endl;
} else {
cout << "Aoki" << endl;
}
} else {
if (a < b) {
cout << "Aoki" << endl;
} else {
cout << "Takahashi" << endl;
}
}
}
|
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
//#include <atcoder/convolution>
//#include <atcoder/modint>
//using Modint = atcoder::modint998244353;
using namespace std;
typedef long long ll;
void ex_gcd(ll a, ll b, ll &d, ll &x, ll &y) {
if (!b) {
d = a;
x = 1;
y = 0;
} else {
ex_gcd(b, a % b, d, y, x);
y -= a / b * x;
}
}
ll inv(ll a, ll n) {
ll d, x, y;
ex_gcd(a, n, d, x, y);
return d == 1 ? (x % n + n) % (n / d) : -1;
}
ll gcd(ll x, ll y) {
if (y == 0) return x;
return gcd(y, x % y);
}
const int maxn = 400005;
const int mod = 998244353;
int a[maxn];
int idx[maxn], b[maxn];
int cal(int x, int n){
return b[x]<n;
}
int main() {
#ifdef suiyuan2009
freopen("/Users/suiyuan2009/CLionProjects/icpc/input.txt", "r", stdin);
freopen("/Users/suiyuan2009/CLionProjects/icpc/output.txt", "w", stdout);
#endif
int n;
cin>>n;
for(int i=0;i<n+n;i++){
cin>>a[i];
idx[i] = i;
}
sort(idx, idx+n+n,[](int x, int y){
return a[x]<a[y];
});
for(int i=0;i<n+n;i++){
b[idx[i]]=i;
}
string ret=string(n+n,' ');
stack<int>st;
for(int i=0;i<n+n;i++){
if((!st.empty())&&cal(st.top(), n)!=cal(i, n)) {
ret[st.top()]='(';
ret[i]=')';
st.pop();
} else {
st.push(i);
}
}
cout<<ret<<endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define Mod(x) ((x>=P)&&(x-=P))
#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)
#define erep(i,a) for(int i=hd[a];i;nxt[i])
typedef long long ll;
void Max(int &x,int y){(x<y)&&(x=y);}
void Min(int &x,int y){(x>y)&&(x=y);}
bool vio;
char IO;
int rd(int res=0){
bool f=0;
while(IO=getchar(),IO<48||IO>57)
f|=IO=='-';
do res=(res<<1)+(res<<3)+(IO^48);
while(IO=getchar(),isdigit(IO));
return f?-res:res;
}
const int M=1e6+10;
int ans[M],stk[M],top,A[M],B[M],mk[M];
bool let;
int main(){
cerr<<(&vio-&let)/1024.0/1024<<endl;
int n=rd();
rep(i,1,2*n)B[i]=A[i]=rd();
sort(B+1,B+2*n+1);
int t=n;
rep(i,1,n*2){
if(A[i]<B[n])mk[i]=1,t--;
if(A[i]>B[n])mk[i]=2;
}
int fl=0;
rep(i,1,n*2)if(t&&A[i]==B[n])mk[i]=1,t--;
rep(i,1,2*n)if(!mk[i])mk[i]=2;
rep(i,1,2*n){
if(top){
if(mk[i]==fl)stk[++top]=i;
else ans[stk[top--]]=1,ans[i]=2;
}else {
fl=mk[i];
stk[++top]=i;
}
}
rep(i,1,2*n)putchar(ans[i]==1?'(':')');
}
|
/*
author : TAPAN SAVANI
codeforces : savanitapan2001
codechef : savanitapan17
*/
/*
------------------------------------------------------------------------
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
------------------------------------------------------------------------
*/
#include <bits/stdc++.h>
using namespace std;
#define HAPPY_CODING \
ios_base::sync_with_stdio(0); \
cin.tie(NULL); \
cout.tie(NULL);
#define pb push_back
#define mp make_pair
#define Debug(x) cout << #x " = " << (x) << endl
#define SORT(a) sort(a.begin(), a.end())
#define SORTR(a) sort(a.rbegin(), a.rend())
#define mod 1000000007
#define pi 3.141592653589793238
#define ll long long int
#define ull unsigned long long
#define be begin()
#define en end()
#define FOR(i, a, b) for (long long int i = a; i < b; i++)
#define FORI(i, a, b) for (int i = a; i >= b; i--)
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PI;
typedef pair<ll, ll> PL;
typedef vector<PI> VPI;
int main()
{
HAPPY_CODING;
ll x, y, z;
cin >> x >> y >> z;
cout << (y * z - 1) / x << "\n";
return 0;
}
| #include <bits/stdc++.h>
#define _overload3(_1,_2,_3,name,...)name
#define _rep(i,n)repi(i,0,n)
#define repi(i,a,b)for(int i=int(a),i##_len=(b);i<i##_len;++i)
#define MSVC_UNKO(x)x
#define rep(...)MSVC_UNKO(_overload3(__VA_ARGS__,repi,_rep,_rep)(__VA_ARGS__))
#define all(c)c.begin(),c.end()
#define write(x)cout<<(x)<<'\n'
using namespace std; using ll = long long; template<class T>using vv = vector<vector<T>>;
template<class T>auto vvec(int n, int m, T v) { return vv<T>(n, vector<T>(m, v)); }
constexpr int INF = 1 << 29, MOD = int(1e9) + 7; constexpr ll LINF = 1LL << 60;
struct aaa { aaa() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(10); }; }aaaa;
int main() {
int N, M;
cin >> N >> M;
vector<pair<int, int>> XY(M);
rep(i, M) cin >> XY[i].first >> XY[i].second;
XY.emplace_back(0, N);
sort(all(XY));
map<int, vector<pair<int, int>>> mp;
rep(i, M + 1) {
mp[XY[i].second].emplace_back(XY[i].first, i);
}
vv<int> graph(M + 1);
rep(i, M + 1) {
auto [x, y] = XY[i];
int z;
if (mp[y].back().second == i) {
z = 2 * N + 1;
}
else {
auto ub = lower_bound(all(mp[y]), make_pair(x + 1, 0));
z = ub->first;
}
if (mp.find(y - 1) != mp.end()) {
auto lb = lower_bound(all(mp[y - 1]), make_pair(x + 1, 0));
auto ub = lower_bound(all(mp[y - 1]), make_pair(z + 1, 0));
for (auto it = lb; it != ub; ++it) {
graph[i].push_back(it->second);
}
}
if (mp.find(y + 1) != mp.end()) {
auto lb = lower_bound(all(mp[y + 1]), make_pair(x + 1, 0));
auto ub = lower_bound(all(mp[y + 1]), make_pair(z + 1, 0));
for (auto it = lb; it != ub; ++it) {
graph[i].push_back(it->second);
}
}
}
vector<bool> can_visit(M + 1);
can_visit[0] = true;
auto dfs = [&](auto self, int u) -> void {
for (int v : graph[u]) {
if (can_visit[v]) continue;
can_visit[v] = true;
self(self, v);
}
};
dfs(dfs, 0);
set<int> ans;
rep(i, M + 1) {
auto [x, y] = XY[i];
if (can_visit[i] && i == mp[y].back().second) {
ans.insert(y);
}
}
write((int)ans.size());
} |
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<algorithm>
#include<numeric>
#include<cmath>
#include<iomanip>
#include<regex>
using namespace std;
#define int long long
const int mod=1e9+7;
signed main(){
int n,m,x,y;
cin>>n>>m;
map<int,vector<int>>mp;
while(cin>>x>>y)
mp[x].push_back(y);
set<int>st={n};
for(auto&e:mp){
vector<int>&ys=e.second,in,out;
for(int y:ys){
if(st.count(y))
out.push_back(y);
if(st.count(y-1)||st.count(y+1))
in.push_back(y);
}
for(int y:out)
st.erase(y);
for(int y:in)
st.insert(y);
}
cout<<st.size()<<endl;
} | // #pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
// #pragma GCC optimize(3 , "Ofast" , "inline")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization("unroll-loops")
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <unordered_map>
#include <vector>
#include <map>
#include <list>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <stack>
#include <set>
#include <bitset>
#include <deque>
using namespace std ;
#define ios ios::sync_with_stdio(false) , cin.tie(0)
#define x first
#define y second
#define pb push_back
#define ls rt << 1
#define rs rt << 1 | 1
typedef long long ll ;
const double esp = 1e-6 , pi = acos(-1) ;
typedef pair<int , int> PII ;
const int N = 1e6 + 10 , INF = 0x3f3f3f3f , mod = 1e9 + 7;
int n , m ;
vector<int> v[N] ;
vector<int> dep[N] ;
int in[N] , out[N] , idx = 0 ;
void dfs(int u , int d) {
in[u] = ++ idx ;
dep[d].pb(in[u]) ;
for(auto x : v[u])
dfs(x , d + 1) ;
out[u] = ++ idx ;
}
int work()
{
cin >> n ;
for(int i = 2; i <= n ;i ++ ) {
int x ;
cin >> x ;
v[x].pb(i) ;
}
cin >> m ;
dfs(1 , 0) ;
while(m -- ) {
int x , y ;
cin >> x >> y ;
cout << (int)(upper_bound(dep[y].begin() , dep[y].end() , out[x]) - lower_bound(dep[y].begin() , dep[y].end() , in[x]))<< "\n";
}
return 0 ;
}
int main()
{
// freopen("C://Users//spnooyseed//Desktop//in.txt" , "r" , stdin) ;
// freopen("C://Users//spnooyseed//Desktop//out.txt" , "w" , stdout) ;
work() ;
return 0 ;
}
/*
*/
|
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
const int N=2e3+100,INF=0x3f3f3f3f;
char g[N][N];
int st[N][N];
int main(){
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
scanf("%s",g[i]+1);
int ans=0;
for(int i=1;i<n;i++){
for(int j=1;j<=m;j++){
if(g[i][j]!=g[i+1][j]){
if(st[i][j-1]==0)
ans++;
st[i][j]=1;
}
}
}
memset(st,0,sizeof st);
for(int i=1;i<=n;i++){
for(int j=1;j<m;j++){
if(g[i][j]!=g[i][j+1]){
if(st[i-1][j]==0)
ans++;
st[i][j]=1;
}
}
}
cout<<ans<<endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define pb push_back
#define mp make_pair
#define sz(s) (int)s.size()
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(a) a.begin(),a.end()
#define allr(a) a.rbegin(),a.rend()
#define rep(i,a,b) for(int i = a; i < b; i++)
#define in insert
#define ff first
#define ss second
#define vt vector
#define up upper_bound
#define lb lower_bound
#define repp(i,n,a) for(int i = n; i >= a; i--)
#define re(a,n) rep(i,0,n) cin>>a[i];
const long double pi = 2 * acos(0.0);
const int mod = 1e9 + 7;
const int maxn = 2e5+5;
void solve(){
int x,y;
cin>>x>>y;
if(x==y){
cout<<x;
}else{
cout<<(3-(x+y)%3)%3;
}
return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t = 1;
//cin >> t;
while(t--){
solve();
}
return 0;
} |
#include <bits/stdc++.h>
#define li long long int
#define lp(i,a,b) for(li i=a;i<b;i++)
using namespace std;
const li inf = 1e18+7;
void solve(){
li n,m,mn=inf,ans=0;
cin>>n>>m;
li a[n][m];
lp(i,0,n)
lp(j,0,m){
cin>>a[i][j];
mn=min(a[i][j],mn);
}
lp(i,0,n)
lp(j,0,m){
if(a[i][j]!=mn) ans+=(a[i][j]-mn);
}
cout<<ans<<"\n";
}
int main()
{ ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// li t=1;
// cin>>t;
// while(t--){
solve();
// }
return 0;
} | #include <iostream>
#include <vector>
#include <array>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <string>
#include <sstream>
#include <algorithm>
#include <random>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <cassert>
#include <climits>
#include <bitset>
#include <functional>
#include <iomanip>
#include <random>
#define FOR_LT(i, beg, end) for (decltype(end) i = beg; i < end; i++)
#define FOR_LE(i, beg, end) for (decltype(end) i = beg; i <= end; i++)
#define FOR_DW(i, beg, end) for (decltype(beg) i = beg; end <= i; i--)
#define REP(n) for (decltype(n) repeat_index = 0; repeat_index < n; repeat_index++)
using namespace std;
template<typename T, size_t S>
T minval(const T(&vals)[S])
{
T val = numeric_limits<T>::max();
FOR_LT(i, 0, S) {
val = min(val, vals[i]);
}
return val;
}
template<typename T, size_t S>
T maxval(const T(&vals)[S])
{
T val = numeric_limits<T>::min();
FOR_LT(i, 0, S) {
val = max(val, vals[i]);
}
return val;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << fixed << setprecision(20);
int n; cin >> n;
vector<int64_t> minc(n + 1, INT64_MAX);
vector<int64_t> maxc(n + 1, INT64_MIN);
vector<bool> hasb(n + 1);
minc[0] = 0;
maxc[0] = 0;
FOR_LT(i, 0, n) {
int64_t x, c; cin >> x >> c;
hasb[c] = true;
minc[c] = min(minc[c], x);
maxc[c] = max(maxc[c], x);
}
vector<array<int64_t, 2>> dp(n + 2, { INT64_MAX, INT64_MAX });
dp[0][0] = 0; dp[0][1] = 0;
int pc = 0;
FOR_LE(i, 0, n) {
if (!hasb[i]) {
dp[i + 1][0] = dp[i][0];
dp[i + 1][1] = dp[i][1];
continue;
}
//cout << minc[pc] << " " << maxc[pc] << " " << minc[i] << " " << maxc[i] << endl;
{
dp[i + 1][0] = min(dp[i + 1][0], dp[i][0] + abs(minc[pc] - maxc[i]) + abs(minc[i] - maxc[i]));
dp[i + 1][1] = min(dp[i + 1][1], dp[i][0] + abs(minc[pc] - minc[i]) + abs(minc[i] - maxc[i]));
}
{
dp[i + 1][0] = min(dp[i + 1][0], dp[i][1] + abs(maxc[pc] - maxc[i]) + abs(minc[i] - maxc[i]));
dp[i + 1][1] = min(dp[i + 1][1], dp[i][1] + abs(maxc[pc] - minc[i]) + abs(minc[i] - maxc[i]));
}
//cout << dp[i + 1][0] << " " << dp[i + 1][1] << endl;
pc = i;
}
cout << min(dp[n + 1][0] + abs(minc[pc]), dp[n + 1][1] + abs(maxc[pc])) << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define x first
#define y second
#define FOR(i, m, n) for (ll i(m); i < n; i++)
#define DWN(i, m, n) for (ll i(m); i >= n; i--)
#define REP(i, n) FOR(i, 0, n)
#define DW(i, n) DWN(i, n, 0)
#define F(n) REP(i, n)
#define FF(n) REP(j, n)
#define D(n) DW(i, n)
#define DD(n) DW(j, n)
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using tll = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vpll = vector<pll>;
using vtll = vector<tll>;
using gr = vector<vll>;
using wgr = vector<vpll>;
void add_edge(gr&g,ll x, ll y){ g[x].pb(y);g[y].pb(x); }
void add_edge(wgr&g,ll x, ll y, ll z){ g[x].eb(y,z);g[y].eb(x,z); }
template<typename T,typename U>
ostream& operator<<(ostream& os, const pair<T,U>& p) {
cerr << ' ' << p.x << ',' << p.y; return os; }
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
for(auto x: v) os << ' ' << x; return os; }
template <typename T>
ostream& operator<<(ostream& os, const set<T>& v) {
for(auto x: v) os << ' ' << x; return os; }
template<typename T,typename U>
ostream& operator<<(ostream& os, const map<T,U>& v) {
for(auto x: v) os << ' ' << x; return os; }
struct d_ {
template<typename T> d_& operator,(const T& x) {
cerr << ' ' << x; return *this;}
} d_t;
#define dbg(args ...) { d_t,"|",__LINE__,"|",":",args,"\n"; }
#define deb(X ...) dbg(#X, "=", X);
#define EPS (1e-10)
#define INF (1LL<<61)
#define YES(x) cout << (x ? "YES" : "NO") << endl;
#define CL(A,I) (memset(A,I,sizeof(A)))
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
string s[3];
ll n[3];
int main(void) {
ios_base::sync_with_stdio(false);
set<char> ch;
F(3) cin >> s[i];
F(3) for(auto c: s[i]) ch.insert(c);
bool ok = 1;
if(ch.size()>10) ok=0;
ll x=0;
while(ch.size()<10) { ch.insert('A'+x); x++; }
vector<char> v(all(ch));
sort(all(v));
if(ok) do {
ok=0;
F(3) {
n[i]=0;
for(auto c: s[i]) {
ll cur;
FF(10) if(c==v[j]) cur=j;
n[i] = 10*n[i]+cur;
}
}
bool cok = 1;
F(3) cok &= to_string(n[i]).size()==s[i].size() && n[i]!=0;
if(cok && n[0]+n[1]==n[2]) {
ok=1;
F(3) cout << n[i] << endl;
break;
}
} while(next_permutation(all(v)));
if(!ok) cout << "UNSOLVABLE" << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
// using namespace atcoder;
#define INCANT cin.tie(0), cout.tie(0), ios::sync_with_stdio(0), cout << fixed << setprecision(20);
#define gcd __gcd
#define int long long
#define double long double
const int INF = 1e18, MOD = 1e9 + 7;
int pow(int x, int n) {
return n < 1 ? 1 : pow(x * x, n / 2) * (n % 2 ? x : 1);
}
int modinv(int a) {
int b = MOD, u = 1, v = 0;
while (b) {
int t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
u %= MOD;
if (u < 0) u += MOD;
return u;
}
map<int, int> factorization(int n) {
map<int, int> res;
for(int i = 2; i * i <= n; i++) {
if(i > 3) i++;
while(!(n % i)) res[i]++, n /= i;
}
if(n > 1) res[n]++;
return res;
}
vector<int> divisors(int n) {
vector<int> res;
for (int i = 1; i * i <= n; i++) {
if(!(n % i)) {
res.push_back(i);
if(i * i < n) res.push_back(n / i);
}
}
return res;
}
int combination(int n, int r) {
int res = 1;
for(int i = 1; i < r + 1; i++) {
res *= n--, res /= i;
}
return res;
}
bool is_prime(int n) {
if (n < 2) return false;
for(int i = 2; i * i <= n; i++) {
if(i > 3) i++;
if(!(n % i)) return false;
}
return true;
}
signed main() {
INCANT;
string s1, s2, s3;
set<char> list;
cin>>s1>>s2>>s3;
reverse(s1.begin(), s1.end());
reverse(s2.begin(), s2.end());
reverse(s3.begin(), s3.end());
for (char c: s1 + s2 + s3) {
list.insert(c);
}
int digits[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int x, y, z, i;
map<char, int> m;
do {
m.clear();
x = y = z = i = 0;
for (char c: list) {
m[c] = digits[i++];
}
if (
0 == m[s1[s1.size() - 1]] ||
0 == m[s2[s2.size() - 1]] ||
0 == m[s3[s3.size() - 1]]
) {
continue;
}
i = 1;
for (char c: s1) {
x += i * m[c];
i *= 10;
}
i = 1;
for (char c: s2) {
y += i * m[c];
i *= 10;
}
i = 1;
for (char c: s3) {
z += i * m[c];
i *= 10;
}
if (z == x + y) {
cout<<x<<endl<<y<<endl<<z<<endl;
return 0;
}
} while (next_permutation(digits, digits + 10));
cout<<"UNSOLVABLE"<<endl;
} |
#include<bits/stdc++.h>
#define pi 3.141592653589793238
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define MOD 998244353
#define INF 999999999999999999
#define pb push_back
#define ff first
#define ss second
#define mt make_tuple
#define ll long long
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const int N = 3e5 + 1;
ll dp[N][30], fac[N];
ll dp2[N];
ll power(ll x, ll y, ll p)
{
ll res = 1;
x = x % p;
while (y > 0)
{
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
ll modi(ll a, ll m)
{
return power(a, m - 2, m);
}
ll nCr(ll n, ll r, ll p)
{
if (r == 0)
return 1;
if(r > n){
return 0;
}
return (fac[n] * modi(fac[r], p) % p *
modi(fac[n - r], p) % p) % p;
}
vector<ll> adj[N];
ll f(ll idx, ll len){
ll res = 0;
if(len == 1){
return dp[idx][len] = 1;
}
if((int)adj[idx].size() == 0){
return dp[idx][len] = 0;
}
if(dp[idx][len] != -1){
return dp[idx][len];
}
for(auto u : adj[idx]){
res += f(u, len - 1);
res %= MOD;
}
return dp[idx][len] = res;
}
int main() {
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
fast;
ll T = 1, i, j;
memset(dp, -1, sizeof(dp));
//cin >> T;
fac[0] = 1;
for(i = 1; i < N; i++){
fac[i] = i * fac[i - 1];
fac[i] %= MOD;
}
while (T--) {
ll n, m;
cin >> n >> m;
for(i = 1; i <= m; i++){
for(j = 2 * i; j <= m; j += i){
adj[i].pb(j);
//cout << i << " $$ " << j << endl;
}
}
ll ans = 0;
for(j = 0; j <= n - 1; j++){
dp2[j] = nCr(n - 1, j, MOD);
}
for(i = 1; i <= m; i++){
for(j = 1; j <= 21; j++){
ll mul = dp2[j - 1];
//cout << n - 1 << " ## " << j - 1 << endl;
ll x = f(i, j);
ans += mul * x;
assert(ans >= 0);
//cout << mul << " " << x << endl;
if(x != 0)
assert(INF / x >= mul);
//cout << mul << " " << f(i, j) << endl;
//cout << f(i, j) << " ";
ans %= MOD;
}
//cout << endl;
}
//cout << dp[m][1] << endl;
cout << ans << endl;
}
return 0;
} | //#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using db = double;
using ld = long double;
template<typename T> using V = vector<T>;
template<typename T> using VV = vector<vector<T>>;
template<typename T> using PQ = priority_queue<T>;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define all(v) (v).begin(),(v).end()
#define siz(v) (ll)(v).size()
#define rep(i,a,n) for(ll i=a;i<(ll)(n);++i)
#define repr(i,a,n) for(ll i=n-1;(ll)a<=i;--i)
#define ENDL '\n'
typedef pair<int,int> Pi;
typedef pair<ll,ll> PL;
constexpr ll mod = 1000000007; // 998244353;
constexpr ll INF = 1000000099;
constexpr ll LINF = (ll)(1e18 +99);
const ld PI = acos((ld)-1);
constexpr ll dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
template<typename T,typename U> inline bool chmin(T& t, const U& u){if(t>u){t=u;return 1;}return 0;}
template<typename T,typename U> inline bool chmax(T& t, const U& u){if(t<u){t=u;return 1;}return 0;}
template<typename T> inline T gcd(T a,T b){return b?gcd(b,a%b):a;}
inline void Yes() { cout << "Yes" << ENDL; }
inline void No() { cout << "No" << ENDL; }
inline void YES() { cout << "YES" << ENDL; }
inline void NO() { cout << "NO" << ENDL; }
template<typename T,typename Y> inline T mpow(T a, Y n) {
T res = 1;
for(;n;n>>=1) {
if (n & 1) res = res * a;
a = a * a;
}
return res;
}
template <typename T>
vector<T> finddivisor(T x) { //ๆดๆฐxใฎ็ดๆฐ(xใๅซใ)
vector<T> divisor;
for(T i = 1; (i * i) <= x; i++) {
if(x % i == 0) {
divisor.push_back(i);
if(i * i != x) { divisor.push_back(x / i);}
}
}
sort(divisor.begin(), divisor.end());
return divisor;
}
template <typename T> V<T> prefix_sum(const V<T>& v) {
int n = v.size();
V<T> ret(n + 1);
rep(i, 0, n) ret[i + 1] = ret[i] + v[i];
return ret;
}
template<typename T>
istream& operator >> (istream& is, vector<T>& vec){
for(auto&& x: vec) is >> x;
return is;
}
template<typename T,typename Y>
ostream& operator<<(ostream& os,const pair<T,Y>& p){
return os<<"{"<<p.fs<<","<<p.sc<<"}";
}
template<typename T> ostream& operator<<(ostream& os,const V<T>& v){
os<<"{";
for(auto e:v)os<<e<<",";
return os<<"}";
}
template<typename ...Args>
void debug(Args&... args){
for(auto const& x:{args...}){
cerr<<x<<' ';
}
cerr<<ENDL;
}
signed main(){
cin.tie(0);cerr.tie(0);ios::sync_with_stdio(false);
cout<<fixed<<setprecision(20);
ll n,m;cin>>n>>m;
V<ll> a(m),b(m);
rep(i,0,m){
cin>>a[i]>>b[i];
a[i]--;
b[i]--;
}
ll k;cin>>k;
V<ll> c(k),d(k);
rep(i,0,k){
cin>>c[i]>>d[i];
c[i]--;
d[i]--;
}
ll ans=0;
rep(bit,0,1<<k){
V<bool> aru(n,false);
rep(i,0,k){
if(bit & (1<<i))aru[c[i]]=true;
else aru[d[i]]=true;
}
ll tmp=0;
rep(i,0,m){
if(aru[a[i]] && aru[b[i]])tmp++;
}
chmax(ans,tmp);
}
cout<<ans<<ENDL;
}
//! ( . _ . ) !
//CHECK overflow,vector_size,what to output?
//any other simpler approach?
//list all conditions, try mathematical and graphic observation |
#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops, no-stack-protector")
#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull long long
#define pll pair<ll,ll>
#define ff first
#define ss second
#define pb push_back
#define endl "\n"
const ll maxn =2e5+30;
const ll mod=998244353 ;
const ll base=1e18;
ll cnt[maxn];
ll mu(ll a,ll n)
{
if (n==0)
return 1;
if (n==1)
return a;
ll t=mu(a,n/2);
if (n%2==0)
return (t*t)%mod;
return ((t*t)%mod*a)%mod;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
if (fopen("test.inp","r"))
{
freopen("test.inp","r",stdin);
freopen("test.out","w",stdout);
}
ll n, m, k ;
cin>> n>> m>> k ;
//1 3 2
if (n==1||m==1)
{
if (n==1&&m==1)
{
cout <<k;
return 0;
}
if (n==1)
{
ll ans=0;
for (int i=1; i<=k; i++)
{
ll nw=(mu(k-i+1,m)-mu(k-i,m)+mod)%mod;
ans=(ans+nw)%mod;
}
cout <<ans;
return 0;
}
else
{
ll ans=0;
for (int i=1;i<=k;i++)
{
ll nw=(mu(i,n)-mu(i-1,n)+mod)%mod;
ans=(ans+nw)%mod;
}
cout <<ans;
return 0;
}
}
ll ans=0;
for (int i=1; i<=k; i++)
{
ll nw=((mu(i,n)-mu(i-1,n)+mod)%mod*mu(k-i+1,m))%mod;
ans=(ans+nw)%mod;
}
cout <<ans;
}
| #include <vector>
#include <set>
#include <iostream>
#include <algorithm>
#include <string>
#include <utility>
#define mod % 998244353
#define MAX 998244353
#define ll long long
using namespace std;
int main() {
int high,wide,k;
cin >> high;
cin >> wide;
cin >> k;
ll ans = 0;
if(high == wide and high == 1){
cout << k mod << endl;
return 0;
}
if(high == 1 or wide == 1){
if(wide == 1){
wide = high;
}
for(int i = 1;i <= k;i ++){
int m = wide;
vector<ll> mul(21);
vector<ll> mult(21);
mul[0] = i;
mult[0] = i-1;
ll pl = 1;
ll plt = 1;
for(int j = 0;j < 20;j++){
mul[j+1] = (mul[j]*mul[j])mod;
mult[j+1] = (mult[j]*mult[j])mod;
if(m % 2 == 1){
pl = (pl*mul[j])mod;
plt = (plt*mult[j])mod;
}
m = m/2;
}
ans = (ans+MAX+pl-plt)mod;
}
cout << ans mod << endl;
return 0;
}
for(int i = 1;i <= k;i ++){
ll pl = 1;
int m = wide;
vector<ll> mul(21);
mul[0] = k-i+1;
for(int j = 0;j < 20;j ++){
mul[j+1] = (mul[j]*mul[j])mod;
if(m % 2 == 1){
pl = (pl*mul[j]) mod;
}
m = m/2;
}
ll kakerua = 1;
ll kakerub = 1;
if(i == 1){
kakerub = 0;
}
m = high;
vector<ll> mult(21);
mul[0] = i;
mult[0] = i-1;
for(int j = 0;j < 20;j ++){
mul[j+1] = (mul[j]*mul[j])mod;
mult[j+1] = (mult[j]*mult[j])mod;
if(m % 2 == 1){
kakerua = (kakerua*mul[j])mod;
kakerub = (kakerub*mult[j])mod;
}
m = m/2;
}
kakerua = (MAX-kakerub+kakerua)mod;
pl = (pl*kakerua)mod;
ans = (ans +pl)mod;
}
cout << ans mod<< 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 = 1000000007;
//998244353;
void randinit(){srand((unsigned)time(NULL));}
int main(){
ll t,N;
cin >> t >> N;
ll c = 0;
ll q;
vector<ll> seen(300,0);
for(ll i = 1;i <= 100;i++){
seen[i * (100 + t) / 100] = 1;
}
for(ll i = 1;i < 100 + t;i++){
if(seen[i] == 0){
q = i;
c++;
}
}
ll p;
ll d = 0;
if(N % c == 0){
cout << (100 + t) * (N / c - 1) + q << endl;
return 0;
}
for(ll i = 1;i < 100 + t;i++){
if(seen[i] == 0){
d++;
if(d == N % c) p = i;
}
}
cout << p + (100 + t) * (N / c) << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define PI acos(-1)
#define rep(i, n) for (int i = 0; i < (n); i++)
int main()
{
ll n;
int k;
cin >> n >> k;
for (int i = 0; i < k; i++)
{
if (n % 200 == 0)
n /= 200;
else
n = n * 1000 + 200;
}
cout << n << endl;
return 0;
} |
#define MOD_TYPE 2
#pragma region Macros
#include <bits/stdc++.h>
using namespace std;
#if 0
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
using Int = boost::multiprecision::cpp_int;
using lld = boost::multiprecision::cpp_dec_float_100;
#endif
#if 1
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#endif
using ll = long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
template <typename Q_type>
using smaller_queue = priority_queue<Q_type, vector<Q_type>, greater<Q_type>>;
constexpr ll MOD = (MOD_TYPE == 1 ? (ll)(1e9 + 7) : 998244353);
constexpr int INF = (int)1e9 + 10;
constexpr ll LINF = (ll)4e18;
constexpr double PI = acos(-1.0);
constexpr double EPS = 1e-7;
constexpr int Dx[] = {0, 0, -1, 1, -1, 1, -1, 1, 0};
constexpr int Dy[] = {1, -1, 0, 0, -1, -1, 1, 1, 0};
#define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define REPI(i, m, n) for (int i = m; i < (int)(n); ++i)
#define repi(i, n) REPI(i, 0, n)
#define MP make_pair
#define MT make_tuple
#define YES(n) cout << ((n) ? "YES" : "NO") << "\n"
#define Yes(n) cout << ((n) ? "Yes" : "No") << "\n"
#define possible(n) cout << ((n) ? "possible" : "impossible") << "\n"
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << "\n"
#define all(v) v.begin(), v.end()
#define NP(v) next_permutation(all(v))
#define dbg(x) cerr << #x << ":" << x << "\n";
struct io_init
{
io_init()
{
cin.tie(0);
ios::sync_with_stdio(false);
cout << setprecision(30) << setiosflags(ios::fixed);
};
} io_init;
template <typename T>
inline bool chmin(T &a, T b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <typename T>
inline bool chmax(T &a, T b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
inline ll CEIL(ll a, ll b)
{
return (a + b - 1) / b;
}
template <typename A, size_t N, typename T>
inline void Fill(A (&array)[N], const T &val)
{
fill((T *)array, (T *)(array + N), val);
}
template <typename T, typename U>
constexpr istream &operator>>(istream &is, pair<T, U> &p) noexcept
{
is >> p.first >> p.second;
return is;
}
template <typename T, typename U>
constexpr ostream &operator<<(ostream &os, pair<T, U> &p) noexcept
{
os << p.first << " " << p.second;
return os;
}
#pragma endregion
void solve()
{
int h, w;
cin >> h >> w;
string s[10];
rep(i, h) cin >> s[i];
int cnt = 0;
rep(i, h - 1) rep(j, w - 1)
{
int t = 0;
t += s[i][j] == '#';
t += s[i][j + 1] == '#';
t += s[i + 1][j] == '#';
t += s[i + 1][j + 1] == '#';
if (t == 1 or t == 3)
cnt++;
}
cout << cnt << "\n";
}
int main()
{
solve();
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n);i++)
using ll = long long;
const ll INF = 1e9+7;
using P = pair<int,int>;
int main(){
int h,w;
cin >> h >> w;
vector<vector<char>> s(h,vector<char>(w));
rep(i,h)rep(j,w) cin >> s[i][j];
int ans =0;
for(int i=1; i<h;i++)for(int j=1; j<w; j++){
int cnt = 0;
if(s[i][j]=='#') cnt++;
if(s[i-1][j]=='#') cnt++;
if(s[i][j-1]=='#') cnt++;
if(s[i-1][j-1]=='#') cnt++;
if(cnt%2==1)ans++;
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define rep1(i,n) for (int i = 1; i <= (n); ++i)
#define bit(n,k) ((n>>k)&1) //nใฎkใbit็ฎ
#define vec(T) vector<T>
#define vvec(T) vector<vector<T>>
using ll = long long;
using P = pair<int,int>;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const ll llINF = 1LL << 60;
const int iINF = 1e9;
//------------------------------------------------
struct Solver{
// ๆๅคงๅ
ฌ็ดๆฐ
int gcd(int a, int b){
if (a%b == 0) return(b);
else return(gcd(b, a%b));
}
void solve(){
int N;
cin >> N;
map<int,int> mp;
auto mpadd = [&](int ai, int xi){
int p = mp[xi];
mp[xi] = gcd(p,ai);
};
int amin=iINF;
rep(i,N){
int A; cin >> A;
chmin(amin,A);
for(int x=1; x*x <=A ; x++){
if(A%x) continue;
mpadd(A,x);
mpadd(A,A/x);
}
}
int ans = 0;
for(auto mi:mp){
if(mi.first <= amin && mi.first == mi.second ) ans++;
}
cout << ans << endl;
}
};
int main(){
int testcasenum=1;
//cin >> testcasenum;
rep1(ti,testcasenum){
Solver solver;
solver.solve();
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define M 100000000000000000
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define PI 3.14159265
#define pb push_back
#define LLMX LONG_LONG_MAX
#define LLMN LONG_LONG_MIN
#define double long double
int n ;
vector < int > arr ;
signed main()
{
fastio ;
cin >> n ;
set < int > st ;
for(int x = 0 ; x <= n-1 ; x++)
{
int t;
cin >> t ;
arr.pb(t) ;
}
sort(arr.begin() , arr.end()) ;
int small = arr[0] ;
map < int , int > dp ;
int ans = 1 ;
for(int x = 0 ; x <= n-1 ; x++)
{
for(int i = 1 ; i <= sqrt(arr[x]) ; i++)
{
if( arr[x] % i !=0)
continue ;
if( i < small)
{
st.insert(i) ;
if( dp[i] == 0)
dp[i] = arr[x] ;
else
dp[i] = __gcd( dp[i] , arr[x]) ;
// cout << i << " " << dp[i] << "\n" ;
}
if( arr[x]/i < small)
{
st.insert( arr[x]/i) ;
if( dp[arr[x]/i] == 0)
dp[arr[x]/i] = arr[x] ;
else
dp[arr[x]/i] = __gcd( dp[arr[x]/i] , arr[x]) ;
//cout << i << " " << dp[i] << "\n" ;
}
}
}
for(auto it = st.begin() ; it != st.end() ; it++)
{
if( dp[*it] == *it)
ans++ ;
}
cout << ans << "\n" ;
} |
//yjy is sb
#include<bits/stdc++.h>
#define inf 1e9
#define eps 1e-6
#define mp make_pair
#define pb push_back
#define re register int
#define fr first
#define sd second
#define pa pair<int,int>
#define FOR(i,a,b) for(re i=a;i<=b;i++)
#define REP(i,a,b) for(re i=a;i>=b;i--)
#define MEM(a) memset(a,0,sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
inline ll read()
{
char ch=getchar();
ll s=0,w=1;
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){s=s*10+ch-'0';ch=getchar();}
return s*w;
}
inline int lowbit(int x){return x&(-x);}
int n,m,S;
const int mod=1e9+7;
inline int ksm(int a,int b)
{
int ans=1;
while(b){if(b&1)ans=1LL*ans*a%mod;b>>=1;a=1LL*a*a%mod;}
return ans;
}
inline int C(int n,int m)
{
int ans=1;
for(re i=1,j=n;i<=m;i++,j--)ans=1LL*ans*j%mod*ksm(i,mod-2)%mod;
return ans;
}
int main()
{
//ios::sync_with_stdio(false);
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
n=read(),m=read();
FOR(i,1,n){int A=read();S+=A;}
cout<<C(m+n,n+S)<<'\n';
return 0;
}
| #include <iostream>
#include <vector>
using namespace std;
vector<pair<long long, long long> > prime_factorize(long long N) {
vector<pair<long long, long long> > res;
for (long long a = 2; a * a <= N; ++a) {
if (N % a != 0) continue;
long long ex = 0;
while (N % a == 0) {
++ex;
N /= a;
}
res.push_back({a, ex});
}
if (N != 1) res.push_back({N, 1});
return res;
}
int main() {
long long N;
cin >> N;
while (N % 2 == 0) {
N /= 2;
}
const auto &pf = prime_factorize(N);
long long res = 1;
for (auto p : pf) res *= p.second + 1;
cout << 2 * res << endl;
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.