code_file1
stringlengths 87
4k
| code_file2
stringlengths 85
4k
|
---|---|
#include <bits/stdc++.h>
using namespace std;
string s;
int cnt = 0;
char a[4];
set<char> o, x;
void dfs(int depth)
{
if (depth > 3)
{
bool flag = true;
for (char c : o)
{
if (0 == count(a, a + 4, c))
{
flag = false;
break;
}
}
if (flag)
{
for (char c : x)
{
if (count(a, a + 4, c))
{
flag = false;
break;
}
}
}
cnt += flag;
return;
}
for (int i = 0; i < 10; ++i)
{
a[depth] = '0' + i;
dfs(depth + 1);
}
}
int main()
{
cin >> s;
for (int i = 0; i < 10; ++i)
{
if ('o' == s[i])
{
o.insert('0' + i);
}
else if ('x' == s[i])
{
x.insert('0' + i);
}
}
if (count(s.begin(), s.end(), 'o') > 4)
{
cout << 0 << endl;
}
else
{
dfs(0);
cout << cnt << endl;
}
} | #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
typedef long long ll;
int INF =1001001001;
int main()
{
string s;
cin>>s;
int n=s.size();
vector<int>c(3);
rep(i,n)c[(s[i]-'0')%3]++;
int sum=0;
rep(i,3)sum+=c[i]*i;
int ans =INF;
rep(i1,3)rep(i2,3)
{
if(c[1]<i1)continue;
if(c[2]<i2)continue;
if(i1+i2==n)continue;
int temp=sum;
temp-=(1*i1+2*i2);
if(temp%3==0)ans=min(ans,i1+i2);
}
if(ans==INF)ans =-1;
cout <<ans;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define ios ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ll long long
void solve(){
int a,b,c;
cin >> a >> b >> c;
int sum;
sum=((7-a)+(7-b)+(7-c));
cout << sum;
}
int main()
{
ios;
solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
long long int bpow(long long int a,long long int b)
{
if(b==0)
return 1;
long long int res=bpow(a,b/2);
if(b%2)
return res*res*a;
else
return res*res;
}
long long int bpowm(long long int x,long long int y,long long int m)
{
long long int res=1;
x=x%m;
if (x==0)
return 0;
while(y>0)
{
if(y&1)
res=(res*x)%m;
y=y>>1;
x=(x*x)%m;
}
return res;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(NULL); cout.tie(NULL);
int a,b,c;
cin>>a>>b>>c;
cout<<21-a-b-c<<endl;
// your code goes here
return 0;
}
|
// Author: HarshKumar
#include <bits/stdc++.h>
using namespace std;
#define ll int64_t
const ll N = 1e6 + 6, M = 1e9 + 7;
const ll INF = (1LL << 62) - 1;
vector<ll> dijkstra(vector<vector<pair<ll, pair<ll, ll>>>> &G, ll v)
{
vector<ll> ans(G.size(), INF);
ans[v] = 0;
priority_queue<pair<ll, ll>> que;
que.push({0, v});
while (!que.empty())
{
auto [x, u] = que.top();
que.pop();
if (-x > ans[u])
continue;
for (auto [v, w] : G[u])
{
auto [t, k] = w;
ll s = 0;
if (x)
s = (-(x + 1) / k + 1) * k;
if (s + t < ans[v])
ans[v] = s + t, que.push({-ans[v], v});
}
}
return ans;
}
int main()
{
ios::sync_with_stdio(false), cin.tie(nullptr);
ll n, m, x, y;
cin >> n >> m >> x >> y;
x--;
y--;
vector<vector<pair<ll, pair<ll, ll>>>> g(n);
while (m--)
{
ll a, b, k, t;
cin >> a >> b >> t >> k;
a--;
b--;
g[a].push_back({b, {t, k}});
g[b].push_back({a, {t, k}});
}
auto d = dijkstra(g, x);
cout << (d[y] < INF ? d[y] : -1);
} | #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<map>
#include<cmath>
using namespace std;
#define LL long long
#define DB double
#define MAXN 400000
#define Pr pair<int,int>
#define X first
#define Y second
#define INF 1000000001
#define MOD 1000000007
#define eps 1e-8
#define mem(x,v) memset(x,v,sizeof(x))
LL read(){
LL x=0,F=1;char c=getchar();
while(c<'0'||c>'9'){if(c=='-')F=-1;c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
return x*F;
}
int n,x[MAXN+5],y[MAXN+5],fa[MAXN+5];
int xfind(int x){return (fa[x]==x)?x:fa[x]=xfind(fa[x]);}
DB dist(int i,int j){
return (x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);
}
void bin(int u,int v){
int a=xfind(u),b=xfind(v);
if(a==b)return ;
fa[a]=b;
}
bool check(DB R){
for(int i=0;i<=n+1;i++)fa[i]=i;
for(int i=1;i<=n;i++)if(y[i]-2*R<-100)bin(0,i);
for(int i=1;i<=n;i++)if(y[i]+2*R>100)bin(i,n+1);
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++)
if(dist(i,j)<4*R*R)
bin(i,j);
return (xfind(0)==xfind(n+1));
}
int main(){
n=read();
for(int i=1;i<=n;i++)x[i]=read(),y[i]=read();
DB l=0,r=100;
while(l+eps<r){
DB mid=(l+r)/2.0;
if(!check(mid))l=mid;
else r=mid;
}
printf("%.10f",l);
} |
#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse4")
#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define RFOR(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define SZ(a) (int)((a).size())
#define ALL(a) a.begin(), a.end()
typedef long long LL;
typedef pair<int, int> PII;
const int MAX = 1 << 18;
int a[MAX], b[MAX], p[MAX];
bool used[MAX];
int main()
{
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;
cin >> n;
FOR(i, 0, n)
cin >> a[i];
FOR(i, 0, n)
cin >> b[i];
FOR(i, 0, n)
{
cin >> p[i];
p[i]--;
}
FOR(i, 0, n)
{
if(p[i] != i && a[i] <= b[p[i]])
{
cout << "-1\n";
return 0;
}
}
vector<pair<int, int>> ans;
FOR(i, 0, n)
{
if(!used[i])
{
vector<int> c{i};
for(int v = p[i]; v != i; v = p[v])
c.push_back(v);
int idx = i;
for(int v: c)
{
used[v] = true;
if(a[v] > a[idx])
idx = v;
}
while(p[idx] != idx)
{
//assert(a[idx] > b[p[idx]] && a[p[idx]] > b[p[p[idx]]]);
ans.push_back({idx, p[idx]});
swap(p[idx], p[p[idx]]);
}
}
}
cout << SZ(ans) << "\n";
for(auto [i, j]: ans)
cout << i + 1 << " " << j + 1 << "\n";
return 0;
} | #line 1 "main.cpp"
#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <string>
#include <queue>
using namespace std;
using lint = long long;
void solve() {
int n;
cin >> n;
vector<vector<int>> graph(n);
for (int i = n - 1; i--;) {
int u, v;
cin >> u >> v;
--u, --v;
graph[u].push_back(v);
graph[v].push_back(u);
}
int r = -1;
{
vector<bool> vis(n, false);
vis[0] = true;
queue<int> que;
que.push(0);
while (!que.empty()) {
r = que.front();
que.pop();
for (auto v : graph[r]) {
if (vis[v]) continue;
vis[v] = true;
que.push(v);
}
}
}
vector<int> depth(n);
{
auto dfs = [&](auto&& f, int v, int p) -> int {
depth[v] = 0;
for (auto u : graph[v]) {
if (u == p) continue;
depth[v] = max(depth[v], f(f, u, v) + 1);
}
return depth[v];
};
dfs(dfs, r, -1);
}
vector<int> ans(n, -1);
{
int cur = 1;
auto dfs = [&](auto&& f, int v, int p) -> void {
ans[v] = cur;
vector<int> us;
for (auto u : graph[v]) {
if (u != p) us.push_back(u);
}
sort(us.begin(), us.end(),
[&](auto i, auto j) { return depth[i] < depth[j]; });
for (auto u : us) {
++cur;
f(f, u, v);
}
++cur;
};
dfs(dfs, r, -1);
}
for (auto x : ans) cout << x << " ";
cout << "\n";
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
solve();
return 0;
}
|
#include <bits/stdc++.h>
#define ln '\n'
#define all(dat) dat.begin(), dat.end()
#define loop(i, to) for (int i = 0; i < to; ++i)
#define cont(i, to) for (int i = 1; i <= to; ++i)
#define circ(i, fm, to) for (int i = fm; i <= to; ++i)
#define foreach(i, dat) for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)
typedef long long num;
typedef unsigned long long hsh;
using namespace std;
int ts;
string s, t = "atcoder";
void inline solve() {
cin >> s;
if (t < s) {
cout << 0 << ln;
return;
}
int p = -1;
loop (i, s.size()) {
if (s[i] != 'a') {
p = i;
break;
}
}
if (p == -1) {
cout << -1 << ln;
return;
}
cout << p - (s[p] > 't') << ln;
}
int main() {
ios::sync_with_stdio(0);
cin >> ts;
cont (cs, ts) {
solve();
}
} | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define p_ary(ary,a,b) do { cout << "["; for (int count = (a);count < (b);++count) cout << ary[count] << ((b)-1 == count ? "" : ", "); cout << "]\n"; } while(0)
#define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0)
template<typename T1,typename T2>ostream& operator<<(ostream& os,const pair<T1,T2>& a) {os << "(" << a.first << ", " << a.second << ")";return os;}
const char newl = '\n';
int main() {
int n,m;
cin >> n >> m;
if (0 <= m && m < n-1) {
int x = (n-1-m-1)*2+1,c = 1;
for (int i = 0;i < n-1;++i) {
if (x == c) c++;
cout << c << " " << c+1 << newl;
c += 2;
}
cout << x << " " << 2*n << newl;
} else if (n == 1 && m == 0) {
cout << "1 2\n";
} else {
cout << -1 << newl;
}
} |
#include<bits/stdc++.h>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#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;
template<class T> using ordered_set=tree<T,null_type,less<T>,
rb_tree_tag,tree_order_statistics_node_update>;
#define os_find(k) find_by_order(k)
#define os_order(k) order_of_key(k)*/
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef vector<int> vi;
#define f0(i,a,b) for(int i=a;i<b;i++)
#define f1(i,a,b) for(int i=a;i<=b;i++)
#define f2(i,a,b) for(int i=a;i>b;i--)
#define f3(i,a,b) for(int i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define int long long
#define fi first
#define se second
#define mod 1000000007//998244353
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define make_graph(k) int x,y; f0(i,0,k){cin>>x>>y; adj[x].pb(y); adj[y].pb(x);}
#define test int t;cin>>t;while(t--)
#define MAXN 200001
//int fact[300000];
int st[4*MAXN];
int binExp(int x,int n)
{
int res=1;
while(n)
{
if(n&1) res=(res*x)%mod;
x=(x*x)%mod;
n>>=1;
}
return res;
}
int modInv(int i) {return binExp(i,mod-2);}
//int ncr(int n,int r) {return (n>=r?(fact[n]*modInv(fact[r]))%mod*modInv(fact[n-r])%mod:0);}
/*void build(int s,int e,int i,int a[])
{ if(s==e){st[i]=a[s];return;}
int mid=s+(e-s)/2;
build(s,mid,2*i+1,a);
build(mid+1,e,2*i+2,a);
st[i]=st[2*i+1]+st[2*i+2];
}
int query(int s,int e,int i,int a[],int l,int r)
{if(s>e||l>r)return 0;
if(s>=l&&e<=r)return st[i];
if(s>r||e<l)return 0;
int mid=s+(e-s)/2;
return query(s,mid,2*i+1,a,l,r)+query(mid+1,e,2*i+2,a,l,r);
}
int update(int s,int e,int i,int pos,int val)
{if(s==e&&s==pos)
{st[i]=val;return 0;}
if(s>pos||e<pos)return 0;
int mid=s+(e-s)/2;
update(s,mid,2*i+1,pos,val);
update(mid+1,e,2*i+2,pos,val);
st[i]=st[2*i+1]+st[2*i+2];
}int parent[200000],size[200000];
int find_set(int v) {
if (v == parent[v])
return v;
return parent[v] = find_set(parent[v]);
}
void make_set(int v) {
parent[v] = v;
size[v] = 1;
}
void union_sets(int a, int b) {
a = find_set(a);
b = find_set(b);
if (a != b) {
if (size[a] < size[b])
swap(a, b);
parent[b] = a;
size[a] += size[b];
}
}*/
signed main()
{
fast
int n;
cin>>n;
int a[n+1],b[n+1],c[n+1];
f0(i,0,n)cin>>a[i+1];
f0(i,0,n)cin>>b[i+1];
f0(i,0,n)cin>>c[i+1];
map<int,int>m;
f0(i,1,n+1)
{
m[b[c[i]]]++;
}
int ans=0;
f0(i,1,n+1)ans+=m[a[i]];
cout<<ans;
} | // Created by ash_98
#include<bits/stdc++.h>
using namespace std;
#define mx 200005
#define ll long long
#define mod 1000000007
int ar[mx];
char ch[mx];
int n,m,ii,k;
int cnt1[mx],cnt3[mx];
vector<int>g[mx];
void solve()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
int x;
scanf("%d",&x);
cnt1[x]++;
}
for(int i=1;i<=n;i++)
{
int x;
scanf("%d",&x);
g[x].push_back(i);
}
for(int i=1;i<=n;i++)
{
int x;
scanf("%d",&x);
cnt3[x]++;
}
ll re=0;
for(int i=1;i<=n;i++)
{
if(!cnt1[i])continue;
ll val=0;
for(int j:g[i])val+=cnt3[j];
re+=val*cnt1[i];
}
printf("%lld\n",re );
}
int main()
{
int t=1;
//scanf("%d",&t);
while(t--)solve();
return 0;
} |
#include <bits/stdc++.h>
#define ff first
#define ss second
#define mp make_pair
using namespace std;
typedef long long ll;
int main() {
int v[3];
scanf("%d%d%d", &v[0], &v[1], &v[2]);
sort(v, v+3);
printf("%d\n", v[1]+v[2]);
return 0;
} | #include <iostream>
using namespace std;
int main (){
int a,b,c;
cin >> a >> b >> c;
int cont = 0;
if(a>=b){
cont +=a;
if(b>=c){
cont +=b;
}
else{
cont +=c;
}
}
else {
if(b>=c){
cont +=b;
if(c>=a){
cont +=c;
}
else{
cont +=a;
}
}
else{
cont +=c;
if(a>=b){
cont +=a;
}
else{
cont +=b;
}
}
}
cout << cont << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
char s[2000][2000];
int dd[2000][2000], sumx[2000][2000], sumy[2000][2000], sumxy[2000][2000];
const int N = 1000000007;
int h, w;
int main() {
cin >> h >> w;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> s[i][j];
}
}
dd[0][0] = 1;
for (int i = 0; i < w; i++) {
sumx[0][i] = 0, sumxy[0][i] = 0;
}
for (int i = 0; i < h; i++) {
sumy[i][0] = 0, sumxy[i][0] = 0;
}
for (int i = 0; i < h - 1; i++) {
if (s[i][0] == '.') {
sumx[i + 1][0] = (sumx[i][0] + dd[i][0]) % N;
} else {
sumx[i + 1][0] = 0;
}
dd[i + 1][0] = sumx[i + 1][0];
}
for (int i = 0; i < w - 1; i++) {
if (s[0][i] == '.') {
sumy[0][i + 1] = (sumy[0][i] + dd[0][i]) % N;
} else {
sumy[0][i + 1] = 0;
}
dd[0][i + 1] = sumy[0][i + 1];
}
for (int i = 0; i < h - 1; i++) {
for (int j = 0; j < w - 1; j++) {
if (s[i][j + 1] == '.') {
sumx[i + 1][j + 1] = (sumx[i][j + 1] + dd[i][j + 1]) % N;
} else {
sumx[i + 1][j + 1] = 0;
}
if (s[i + 1][j] == '.') {
sumy[i + 1][j + 1] = (sumy[i + 1][j] + dd[i + 1][j]) % N;
} else {
sumy[i + 1][j + 1] = 0;
}
if (s[i][j] == '.') {
sumxy[i + 1][j + 1] = (sumxy[i][j] + dd[i][j]) % N;
} else {
sumxy[i + 1][j + 1] = 0;
}
dd[i + 1][j + 1] = ((sumx[i + 1][j + 1] + sumy[i + 1][j + 1]) % N + sumxy[i + 1][j + 1]) % N;
}
}
cout << dd[h - 1][w - 1] << endl;
return 0;
} | /**
* author: otera
**/
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef long long ll;
typedef long double ld;
#define rep(i, n) for(int i = 0; i < n; ++ i)
#define per(i,n) for(int i=n-1;i>=0;i--)
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int inf = 1'000'000'007;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
void solve() {
int h, w; cin >> h >> w;
vector<vector<char>> a(h, vector<char>(w));
int sx = -1, sy = -1, gx = -1, gy = -1;
vector<vector<int>> tpos(26, vector<int>());
rep(i, h) {
rep(j, w) {
cin >> a[i][j];
if(a[i][j] == 'S') sx = i, sy = j;
else if(a[i][j] == 'G') gx = i, gy = j;
else if(a[i][j] != '.' and a[i][j] != '#') {
int c = a[i][j] - 'a';
tpos[c].push_back(i * w + j);
}
}
}
vector<int> dist(h * w + 26, inf);
dist[sx * w + sy] = 0;
priority_queue<P, vector<P>, greater<P>> pque;
pque.push({0, sx * w + sy});
while(pque.size()) {
P p = pque.top(); pque.pop();
int pos = p.sc, di = p.fr;
if(di > dist[pos]) continue;
if(pos < h * w) {
int x = pos / w, y = pos % w;
rep(k, 4) {
int nx = x + dx[k], ny = y + dy[k];
// cerr << nx << " " << ny << "\n";
if(0 <= nx and nx < h and 0 <= ny and ny < w and a[nx][ny] != '#') {
if(chmin(dist[nx * w + ny], di + 1)) {
pque.push({di + 1, nx * w + ny});
}
}
}
if(a[x][y] != '.' and a[x][y] != 'S' and a[x][y] != 'G') {
// teleport
int c = a[x][y] - 'a';
if(chmin(dist[h * w + c], di)) {
pque.push({di, h * w + c});
}
}
} else {
int c = pos - h * w;
for(int np: tpos[c]) {
if(chmin(dist[np], di + 1)) {
pque.push({di + 1, np});
}
}
}
}
if(dist[gx * w + gy] == inf) dist[gx * w + gy] = -1;
cout << dist[gx * w + gy] << "\n";
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
//cout << fixed << setprecision(20);
//int t; cin >> t; rep(i, t)solve();
solve();
return 0;
} |
#include<iostream>
using namespace std;
int main()
{
int n,k;
cin >> n >> k;
cout << (n+1)*n/2 * k * 100 + n * (k+1)*k/2 << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main(){
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;
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>;
int main() {
int n;
cin >> n;
cout << (n - 1) / 100 + 1 << endl;
} | #include <bits/stdc++.h>
using ll = long long;
using namespace std;
int main(){
int n;
cin >> n;
for(int i = 1; i < 2888; i++){
if(100 * i >= n){
cout << i;
exit(0);
}
}
return 0;
} |
//Saptak_Roy_Turja
#include<bits/stdc++.h>
#define ll long long int
#define d double
using namespace std;
int main()
{
ll n,x,y;
cin>>n>>x>>y;
d ans=0.0;
while(n--){
d a,b;
cin>>a>>b;
d c=(y-b)/(x-a);
ans=max(ans,y-x*c);
}
printf("%0.20lf\n",ans);
}
| #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define loop(i,a,b) for(ll i=a;i<b;i++)
#define loop1(i,a,b) for(ll i=a;i<=b;i++)
#define rloop(i,b,a) for(ll i=b;i>=a;i--)
#define test() int t;cin>>t;while(t-->0)
#define speed ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define endl "\n"
#define mp make_pair
#define pb push_back
#define mod 1000000007
//const ld PI = 3.141592653589793;
bool isPrime(int n)
{
// Corner cases
if(n<=3){
return true;
}
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
int main()
{
speed
double x,y,z;
cin>>x>>y>>z;
double cost=y/x;
double tt=z*cost;
int t=tt;
if((double)t<tt){
cout<<t;
}
else
cout<<t-1;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using ull = unsigned long long int;
using P = pair<ll, ll>;
using P3 = pair<int,P>;
using PP = pair<P, P>;
constexpr int INF32 = 1 << 30;
constexpr ll INF64 = 1LL << 60;
constexpr ll MOD = 1000000007;
// constexpr ll MOD = 998244353;
constexpr int di[] = {0, 1, 0, -1};
constexpr int dj[] = {1, 0, -1, 0};
constexpr int di8[] = {0, 1, 1, 1, 0, -1, -1, -1};
constexpr int dj8[] = {1, 1, 0, -1, -1, -1, 0, 1};
constexpr double EPS = 1e-10;
const double PI = acos(-1);
#define ALL(v) (v).begin(),(v).end()
#define REP(i,n) for(int i=0,i_len=n; i<i_len; ++i)
template<typename T1,typename T2> bool chmax(T1 &a, T2 b) { if (a<b) { a=b; return 1; } return 0; }
template<typename T1,typename T2> bool chmin(T1 &a, T2 b) { if (b<a) { a=b; return 1; } return 0; }
int main(){
ll n;
cin >> n;
vector<ll> fibo = {1,1};
while(fibo.back() < n){
int k = fibo.size();
fibo.push_back(fibo[k-1]+fibo[k-2]);
}
int m = fibo.size()-1;
vector<int> v;
for(ll i = m-1, s=n; s > 0 && i >= 0; i--){
while(s >= fibo[i]){
v.push_back(m-i-1);
s -= fibo[i];
}
}
// for(auto x : v){
// cout << x << " ";
// }
// cout << endl;
v.push_back(INF32);
ll x = 0, y = 0;
vector<int> ans;
for(int i=0,j=0;x<n;i++){
while(v[j]==i){
if(i%2==0){
y++;
ans.push_back(1);
}else{
x++;
ans.push_back(0);
}
j++;
}
if(i%2==0){
x = x+y;
ans.push_back(2);
}else{
y = x+y;
ans.push_back(3);
}
}
cout << ans.size() << endl;
int f = (x==n ? 0 : 1);
for(auto t : ans){
cout << (t^f)+1 << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
ld rr = 1.618033988;
vector<int> ans, ans_tmp;
ll last = -1;
int flag = -1;
ll cnt_tmp = 1e18;
void func(ll x, ll y) {
if (x == 0) {
last = y;
flag = 0;
for (ll i=0;i<y;i++) {
ans_tmp.push_back(2);
}
} else if (y == 0) {
last = x;
flag = 1;
for (ll i=0;i<x;i++) {
ans_tmp.push_back(1);
}
} else if (x <= y) {
ans_tmp.push_back(4);
func(x, y-x);
} else if (x > y) {
ans_tmp.push_back(3);
func(x-y, y);
}
return;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
ll N;
cin >> N;
ll M = (ll)((ld)N/(rr+1.0));
for (ll j=0;j<1e2;j++) {
if (0 <= M-j && M-j <= N) {
last = -1;
flag = -1;
ans_tmp.clear();
func(N, M-j);
if (ans_tmp.size() < cnt_tmp) {
ans = ans_tmp;
cnt_tmp = ans.size();
// break;
}
}
if (0 <= M+j && M+j <= N) {
last = -1;
flag = -1;
ans_tmp.clear();
func(N, M+j);
if (ans_tmp.size() < cnt_tmp) {
ans = ans_tmp;
cnt_tmp = ans.size();
// break;
}
}
}
reverse(ans.begin(), ans.end());
cout << ans.size() << "\n";
for (auto ele : ans) {
cout << ele << "\n";
}
return 0;
}
// 1000000000000000000
// 999999999999999999
|
#include<bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
std::mt19937 rnd(233);
typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pLL;
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define ls (i<<1)
#define rs (i<<1|1)
#define mem(a,b) memset(a,b,sizeof(a))
const int N=1e6+5;
const int inf=0x3f3f3f3f;
const LL mod=1e9+7;
LL read()
{
LL x=0,f=1;
char ch=getchar();
while(!isdigit(ch)){ if(ch=='-') f=-1; ch=getchar(); }
while(isdigit(ch)){ x=10*x+ch-'0'; ch=getchar(); }
return f*x;
}
pii a[N];
int l[N],w[N],b[N];
LL dp[10],sum[10];
int main()
{
int n=read(),m=read(),ma=0,mi=1e9;
for(int i=1;i<=n;i++) w[i]=read(),ma=max(ma,w[i]);
for(int i=1;i<=m;i++)
{
a[i].se=read();
a[i].fi=read();
mi=min(mi,a[i].fi);
}
if(mi<ma) return 0*printf("-1\n");
sort(a+1,a+m+1);
for(int i=1;i<=m;i++) l[i]=max(a[i].se,l[i-1]);
sort(w+1,w+n+1);
LL ans=1e18;
do
{
//for(int i=1;i<=n;i++) printf("%d%c",w[i],i==n?'\n':' ');
mem(dp,0);
for(int i=1;i<=n;i++) sum[i]=sum[i-1]+w[i];
for(int i=1;i<=n;i++)
for(int j=1;j<i;j++)
{
int tmp=sum[i]-sum[j-1];
int pos=lower_bound(a+1,a+m+1,mk(tmp,0))-a-1; //printf("%d\n",pos);
dp[i]=max(dp[i],dp[j]+l[pos]);
}
ans=min(dp[n],ans);
}while(next_permutation(w+1,w+n+1));
printf("%lld\n",ans);
return 0;
}
| #pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#pragma GCC optimize("Ofast")
//*********************************************0123456789****************************************************************
#include<bits/stdc++.h>
#define int long long
#define pp pair<int,int>
#define ss second
#define ff first
#define all(v) (v).begin(),(v).end()
#define pb push_back
#define mod 1000000007
#define pi 3.14159265359
#define mk(arr,n,type) type *arr=new type[n];
#define sl s1.length();
#define yes cout<< "Yes"<<endl
#define no cout<< "No"<<endl
#define all(v) (v).begin(),(v).end()
#define s(v) sort(v,v+n)
#define r(v) reverse(v,v+n)
#define read(a,n) for(int i=0;i<n;i++)cin>>a[i]
#define print(a,n) for(int i=0;i<n;i++)cout<<a[i]<<" "
#define for1(i,a,n) for(int i=0;i<n;i++)
#define rep(i, c) for(int i = 0; i < (int)c; i++)
#define set(x) cout<<setprecision(x)<<fixed;
#define cone(x) (__builtin_popcountll(x)) // zero indexed
#define fone(x) (__builtin_ffsll(x) -1LL) //give wrong answer if x==0;(zero indexed)
#define mone(x) (63LL-__builtin_clzll(x))
#define mt mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define RUN ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define debug(output) cout<<#output<<"="<<output<<endl
using namespace std;
std::vector<int>adj[10000];
bool visited[10000];
string str;
int getMaxGCD(int arr[], int n) {
int high = 0;
for (int i = 0; i < n; i++)
high = max(high, arr[i]);
int divisors[high + 1] = { 0 }; //array to store all gcd values
for (int i = 0; i < n; i++) {
for (int j = 1; j <= sqrt(arr[i]); j++) {
if (arr[i] % j == 0) {
divisors[j]++;
if (j != arr[i] / j)
divisors[arr[i] / j]++;
}
}
}
for (int i = high; i >= 1; i--)
if (divisors[i] > 1)
return i;
}
signed main()
{
RUN;
int test=1;
//cin>>test;
while(test--)
{
int a,b;
cin>>a>>b;
int ar[1000000],j=0;
for(int i=a;i<=b;i++)
ar[j]=i,j++;
int ans=getMaxGCD(ar,j);
cout<<ans;
}
} |
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int,int> pi;
int main()
{
cin.tie(0);cout.tie(0);
int sx,sy,gx,gy;
cin>>sx>>sy>>gx>>gy;
cout<<fixed<<setprecision(8);
if(sx>gx)
{
swap(sx,gx);
swap(sy,gy);
}
double k=(gx-sx)*1.0/(sy+gy);
k*=sy;
cout<<k+sx<<endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); i++)
using ll = long long;
int main(){
int n;
cin >> n;
vector<int> x(n), y(n);
rep(i,n) cin >> x[i] >> y[i];
int ans = 0;
rep(i,n)rep(j,n){
double ka;
ka = (double)(y[j] - y[i]) / (x[j] - x[i]);
if (ka >= -1 && ka <= 1){
ans++;
}
}
cout << ans/2 << 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>>;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#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);
const vector<ll> dx={-1,1,0,0},dy={0,0,-1,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;}
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> 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;cin>>n;
string s;cin>>s;
V<ll> asum(n+1,0),tsum=asum,csum=asum,gsum=asum;
rep(i,0,n){
asum[i+1]=asum[i]+int(s[i]=='A');
tsum[i+1]=tsum[i]+int(s[i]=='T');
csum[i+1]=csum[i]+int(s[i]=='C');
gsum[i+1]=gsum[i]+int(s[i]=='G');
}
ll ans=0;
rep(i,0,n){
rep(j,i+2,n+1){
if(asum[j]-asum[i]==tsum[j]-tsum[i] && csum[j]-csum[i]==gsum[j]-gsum[i])++ans;
}
}
cout<<ans<<ENDL;
}
//! ( . _ . ) !
//CHECK overflow,vector_size,what to output? | #include <bits/stdc++.h>
using namespace std;
#define inf 1000000000
#define unvisited -1
#define visited 1
#define eps 1e-9
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define uint64 unsigned long long
#define FastSlowInput ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define debug if(true)
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> ii;
typedef vector<ii> vii;
int main(){
int n,i,j;
string s;
map<char, int> mapper;
mapper['A'] = 0;
mapper['T'] = 1;
mapper['C'] = 2;
mapper['G'] = 3;
while(scanf("%d",&n) !=EOF){
cin>>s;
int ans = 0;
for(i=0;i<n;i++){
int cnt[4];
memset(cnt, 0, sizeof cnt);
for(j=i;j<n;j++){
char c = s[j];
cnt[mapper[c]]++;
if(cnt[0] == cnt[1] && cnt[2] == cnt[3]){
ans++;
}
}
}
printf("%d\n",ans);
}
return 0;
}; |
#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
#define fp(x, a, b) for(int x = a; x < b; x++)
#define fn(x, a, b) for(int x = a; x > b; x--)
#define f(x, m) for(auto x : m)
#define cpu() ios::sync_with_stdio(false); cin.tie(nullptr)
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pair<int ,int>>
#define vll vector<pair<ll ,ll>>
#define all(v) v.begin(),v.end()
#define sor(a) sort( a.begin(), a.end() )
#define ros(a) sort( a.rbegin(), a.rend())
#define prec(n) fixed << setprecision(n)
#define ff first
#define ss second
// #define tt third
#define print(x) for(auto it : x) cout << it << " ";
#define debug(x) cerr << #x << " is " << x << endl;
// #define ordered_multiset tree<int, null_type, less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
typedef long long ll;
using namespace std;
// using namespace __gnu_pbds;
#define dbg(args...){ string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {cout << "NEXT\n"; }
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << ", ";
err(++it, args...);
}
template<typename... T>
void rd(T& ... args){
((cin >> args), ...);
}
template<typename... T>
void ps(T ... args){
((cout << args << ' '), ...);
cout<<'\n';
}
const ll MOD = 1e9 + 7, MOD1 = 998244353LL, MAX = 3e5 + 5;
const char nl = '\n';
const int INF = 1e9 + 5;
ll n;
ll a[MAX], tree[MAX * 4], ord[MAX];
ll query(int id, int x, int y, int l, int r){
if(l >= y || r <= x) return 0;
// push(id, l, r);
if(x <= l && r <= y) return tree[id];
int m = (l + r) >> 1;
int ga = query(id << 1, x, y, l, m);
int gb = query(id << 1 | 1, x, y, m, r);
return (ga + gb);
}
void update(int id, int ind, int val, int l, int r){
// push(id, l, r);
tree[id] += val;
if(l + 1 == r) return;
int m = (l + r) >> 1;
if(ind >= m) update(id << 1 | 1, ind, val, m, r);
else update(id << 1, ind, val, l, m);
}
void solve(){
cin >> n;
vii v;
for(int i = 0; i < n; i++){
cin >> a[i];
ord[a[i]] = i;
}
ll res = ((ll)n * (n - 1)) / 2;
for(int i = 0; i < n; i++){
res -= query(1, 0, ord[i], 0, n);
update(1, ord[i], 1, 0, n);
}
cout << res << '\n';
for(int i = 0; i < n - 1; i++){
res -= (a[i] - (n - 1 - a[i]));
cout << res << '\n';
}
}
int main(){
cpu();
// int __;
// cin >> __;
// while(__--){
// solve();
// }
solve();
// cout << nl;
return 0;
}
| /*** keep hungry and calm CoolGuang! ***/
//#pragma GCC optimize(3)
#include <bits/stdc++.h>
#include<stdio.h>
#include<queue>
#include<algorithm>
#include<string.h>
#include<hash_map>
#include<iostream>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const ll INF= 1e18+7;
const ll maxn = 1e6+700;
const int M = 1e6+8;
const ll mod= 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a){char c=getchar();T x=0,f=1;while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
while(isdigit(c)){x=(x<<1)+(x<<3)+c-'0';c=getchar();}a=f*x;}
ll n,m,p;
ll a[maxn];
ll sum[maxn];
void add(int pos){
while(pos<=n){
sum[pos]++;
pos += pos&-pos;
}
}
ll query(int pos){
ll ans = 0;
while(pos){
ans += sum[pos];
pos -= pos&-pos;
}return ans;
}
int main(){
read(n);
for(int i=1;i<=n;i++){
read(a[i]);
a[i]++;
}
ll ans = 0;
for(int i=1;i<=n;i++){
ans += query(n)-query(a[i]);
add(a[i]);
}
dl(ans);
for(int i=1;i<=n-1;i++){
ans += query(n)-query(a[i])-query(a[i]-1);
dl(ans);
}
return 0;
}
/**
5
N 83 2 7475
UN 83 2 27550
EXF 5 2 17298
OVYNH 51 2 14827
XNV 53 1 7591
2
1
XNV
2
83
**/ |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N,i,ans;
cin >> N;
ans = 0;
for (i = 1; i <= N; i++) {
if (i % 10 == 7 || i / 10 % 10 == 7 || i / 100 % 10 == 7 || i / 1000 % 10 == 7 || i / 10000 % 10 == 7) {
continue;
} else if (i % 8 == 7 || i / 8 % 8 == 7 || i / 8 / 8 % 8 == 7 || i / 8 / 8 / 8 % 8 == 7 || i / 8 / 8 / 8 / 8 % 8 == 7 || i / 8 / 8 / 8 / 8 / 8 % 8 == 7) {
continue;
}
ans += 1;
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i,n)for(int i=0;i<(int)(n);++i)
#define fixed(n)fixedb<<setprecision(n)
#define ll long long
#define ALL(a)a.begin(),a.end()
template<class T>void chmin(T& a,T b){
if(a>b){
a=b;
}
}
template<class T>void chmax(T& a,T b){
if(a<b){
a=b;
}
}
const long long INF=1LL<<60;
const long long MOD=1000000007;
int main(){
int n;cin>>n;
set<int>a;
int a10=7,a8=7;
int res=n;
for(int i=1;i<=n;++i){
bool flag=true;
string s=to_string(i);
rep(j,s.size()){
if(s[j]=='7'){
flag=false;
break;
}
}
if(flag){
ll m=i;string t;
while(m>0){
char push=m%8+48;
t=push+t;
if(m/8<=0){
push=m+48;
t=push+t;
}
m/=8;
}
rep(j,t.size()){
if(t[j]=='7'){
flag=false;
break;
}
}
}
if(!flag)res--;
}
cout<<res;
} |
#include <cstdio>
inline int read()
{
int data = 0, w = 1; char ch = getchar();
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') w = -1, ch = getchar();
while (ch >= '0' && ch <= '9') data = data * 10 + (ch ^ 48), ch = getchar();
return data * w;
}
const int N(105);
int n, K, M, f[N][N * N * N], s[N];
inline void Add(int &x, int y) { x = (x + y) % M; }
int main()
{
n = read(), K = read(), M = read(), f[0][0] = 1;
for (int i = 1; i <= n; i++)
for (int j = 0; j <= n * n * K; j++)
{
Add(s[j % i], f[i - 1][j]);
if (j >= (K + 1) * i) Add(s[j % i], M - f[i - 1][j - (K + 1) * i]);
f[i][j] = s[j % i];
}
for (int i = 1; i <= n; i++)
{
int ans = 0;
for (int j = 1; j <= n * n * K; j++)
Add(ans, 1ll * f[i - 1][j] * f[n - i][j] % M);
printf("%lld\n", (K + 1ll * ans * (K + 1)) % M);
}
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define irep(i, n) for (int i = (n); i >= 0; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1 << 25;
const int MOD = 1e9+7;
tuple<ll,ll,ll> extgcd(ll a, ll b) {
if (b == 0) return {a, 1, 0};
ll g, x, y;
tie(g,x,y) = extgcd(b, a%b);
return {g, y, x-a/b*y};
}
void solve() {
int n, s, k;
cin >> n >> s >> k;
ll x,y,g;
tie(g,x,y) = extgcd(k,n);
if (s % g != 0) {
cout << -1 << endl;
return;
}
n /= g;
s /= g;
k /= g;
ll ans = ((x*-s)%n+n)%n;
cout << ans << endl;
}
int main() {
int t;
cin >> t;
rep(i,t) solve();
return 0;
} |
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
typedef long long ll;
using namespace std;
struct edge {
int to;
int cost;
};
using P = pair<ll, ll>;
vector<vector<edge>> G;
vector<ll> d;
void dijkstra(int s) {
priority_queue<P, vector<P>, greater<P>> que;
fill(d.begin(), d.end(), 1e18);
d[s] = 0;
que.push(P(0, s));
while (!que.empty()) {
P p = que.top();
que.pop();
int v = p.second;
if (d[v] < p.first)
continue;
rep(i, G[v].size()) {
edge e = G[v][i];
if (d[e.to] > d[v] + e.cost) {
d[e.to] = d[v] + e.cost;
que.push(P(d[e.to], e.to));
}
}
}
}
int main() {
int R, C;
cin >> R >> C;
vector<vector<int>> A(R, vector<int>(C - 1));
vector<vector<int>> B(R - 1, vector<int>(C));
rep(i, R) rep(j, C - 1) cin >> A[i][j];
rep(i, R - 1) rep(j, C) cin >> B[i][j];
int N = R * C * 2;
G.resize(N);
d.resize(N);
rep(i, R) rep(j, C) {
int idx = C * i + j;
if (j + 1 != C) G[idx].push_back({idx + 1, A[i][j]});
if (j - 1 != -1) G[idx].push_back({idx - 1, A[i][j - 1]});
if (i + 1 != R) G[idx].push_back({idx + C, B[i][j]});
if (i - 1 != -1) G[idx + R * C].push_back({idx + R * C - C, 1});
G[idx].push_back({idx + R * C, 1});
G[idx + R * C].push_back({idx, 0});
}
dijkstra(0);
cout << d[R * C - 1] << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define UniqueVector(vec) sort(vec.begin(), vec.end());vec.erase(unique(vec.begin(), vec.end()), vec.end())
#define rep(i,n) for(ll i=0;i<n;i++)
int main(){
int a,b;
cin>>a>>b;
for(int c=b;c>=1;c--){
if(b/c-(a-1)/c>=2){
cout<<c<<endl;
return 0;
}
}
} |
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <map>
#include <complex>
#include <iomanip>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#define ll long long
#define ld long double
#define HS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define INF 1e15
#define point complex <double>
#define all(a) a.begin(), a.end()
#define pi acos(-1)
#define file freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
using namespace std;
int main()
{
HS;
int n;
cin>>n;
ll arr[n+5];
for(int i=0;i<n;i++)
cin>>arr[i];
ll mini=INF;
for(int i=0;i<(1<<n);i++)
{
ll xr=0;
ll orr=0;
for(int j=0;j<n;j++)
{
if(i & (1<<j))
{
xr^=orr;
orr=0;
}
orr|=arr[j];
}
xr^=orr;
mini=min(mini,xr);
}
cout << mini << endl;
return 0;
}
| #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 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;
const double PI = acos(-1);
//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);
}
// void out(ll x) {
// cout << "Case #" << x << ": ";
// }
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 >> n;
ll a[n];
rep(i, n) cin >> a[i];
ll ans = inf;
rep(i, (1ll << (n - 1))) {
k = i | (1 << (n - 1));
ll xorr = 0;
ll orr = 0;
rep(j, n) {
orr |= a[j];
if ((1 << j)&k) {
xorr ^= orr;
orr = 0;
}
}
// cout << k << ' ';
ans = min(ans, xorr);
}
cout << ans;
return 0;
}
|
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
vector<ll> primes = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71 };
void Main() {
ll A, B;
cin >> A >> B;
vector<ll> AtoB(B - A + 1, 0);
for (ll i = 0; i < B - A + 1; ++i) {
AtoB[i] = A + i;
}
vector<ll> divisibleBits(AtoB.size(), 0);
for (ll i = 0; i < AtoB.size(); ++i) {
for (ll pi = 0; pi < primes.size(); ++pi) {
if (AtoB[i] % primes[pi] == 0) {
divisibleBits[i] |= (1 << pi);
}
}
}
vector<vector<ll>> dp(AtoB.size(), vector<ll>(1 << primes.size(), 0));
for (ll i = 0; i < dp.size(); ++i) {
dp[i][0] = 1;
}
for (ll i = 0; i < AtoB.size(); ++i) {
for (ll bit = 0; bit < (1 << primes.size()); ++bit) {
if (dp[i][bit] == 0) {
continue;
}
ll b = bit | divisibleBits[i];
for (ll ni = i + 1; ni < AtoB.size(); ++ni) {
ll curr = AtoB[i];
ll next = AtoB[ni];
if ((b & divisibleBits[ni]) == 0) {
dp[ni][b] += dp[i][bit];
}
}
}
}
ll ans = 0;
for (int i = 0; i < dp.size(); ++i) {
for (int j = 0; j < dp[i].size(); ++j) {
ans += dp[i][j];
}
}
cout << 1 + ans << endl;
}
int main() {
std::cout << std::fixed << std::setprecision(15);
Main();
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;
template <typename T> using vt = vector<T>;
template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define pb push_back
#define SZ(x) ((int)((x).size()))
#define EACH(x, a) for (auto& x: a)
#define FOR(i,s,n) for (ll i = (s); (i) < (n); ++i)
#define FORD(i,s,l) for (ll i = (s); (i) >= l; --i)
#define F first
#define S second
#define TC int __tc; cin >> __tc; FOR(case_num,1,__tc+1)
#define TEST(x,i) ((x)&(1ll<<(i)))
#define SET(x,i) ((x)|(1ll<<(i)))
#define FLIP(x,i) ((x)^(1ll<<(i)))
#define CLEAR(x,i) ((x)&~(1ll<<(i)))
const double pi = 4 * atan(1);
using ll = long long;
using pll = pair<ll, ll>;
mt19937 mt_rng(chrono::steady_clock::now().time_since_epoch().count());
ll randint(ll a, ll b) {
return uniform_int_distribution<ll>(a, b)(mt_rng);
}
template<class T> bool umin(T& a, const T& b) {
return b<a?a=b, 1:0;
}
template<class T> bool umax(T& a, const T& b) {
return a<b?a=b, 1:0;
}
template<class ForwardIterator> void print_vec(ForwardIterator first,
ForwardIterator last, string sep = " ", string end = "\n") {
bool ft = true;
while (first != last) {
if (!ft) {cout << sep;} else {ft = false;}
cout << (*first);
++first;
}
cout << end;
}
template<typename T1, typename T2>
std::ostream& operator<<(std::ostream &o, const pair<T1, T2> &p) {
return o << p.F << " " << p.S;
}
inline ll floorDiv(ll x, ll y) {
ll d = x / y;
ll r = x % y;
return r ? (d - ((x < 0) ^ (y < 0))) : d;
}
ll ceilDiv(ll x, ll y) {
return -floorDiv(-x, y);
}
ll bin_search(ll lo, ll hi, function<bool(ll)> predicate) {
int sign = lo <= hi ? 1 : -1;
lo *= sign;
hi *= sign;
hi++;
while (lo < hi) {
ll mid = lo + floorDiv(hi-lo, 2);
if (!predicate(sign * mid)) {
lo = mid + 1;
} else {
hi = mid;
}
}
return sign * lo;
}
const ll MOD = 1000000007;
const int MAXN = 200005;
vector<int> pr = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71};
ll A, B;
int n;
ll dp[73][1<<20];
ll ways(int i, int b) {
ll x = A+i;
if (x==B+1) {
return 1;
}
ll &ans = dp[i][b];
if (ans != 0){
return ans;
}
bool good = true;
int nb = 0;
int j = 0;
for (auto p : pr) {
if (x%p ==0) {
if (TEST(b,j)) {
good=false;
break;
}
nb=SET(nb,j);
}
j++;
}
if (good) {
ans+=ways(i+1,b|nb);
}
ans+=ways(i+1,b);
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> A >> B;
cout << ways(0,0) << "\n";
}
|
#include<bits/stdc++.h>
using namespace std;
#define superfastboy ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define mp make_pair
#define int long long
#define ll long long
#define ull unsigned long long
ll const inf=1e9+7,mod=1e9+7;
ll const N=270805;
signed main(){
// freopen(".inp","r",stdin);
// freopen(".outp","w",stdout);
superfastboy
int a,b;
cin>>a>>b;
for(int c=b;c>0;c--){
if((a-1)/c+1<b/c){
cout<<c<<"\n";
return 0;
}
}
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
int t, N;
cin >> t >> N;
int P = 100 + t;
vector<int> A(P,1);
rep(i, 100) {
A.at(P * i / 100) = 0;
}
vector<int> B;
rep(i, P) {
if (A.at(i)) {
B.push_back(i);
}
}
int64_t q, r;
q = (N - 1) / t;
r = (N - 1) % t;
int64_t ans;
ans = P * q + B[r];
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
auto go = [&](int x) -> long long {
return 1LL * x * (x + 1) / 2;
};
long long ans = 0;
while (n--) {
int a, b;
cin >> a >> b;
ans += go(b) - go(a - 1);
}
cout << ans << "\n";
return 0;
}
| // AtCoder template
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i = 0; i < n; ++i)
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
ll ans = 0;
int n; cin >> n;
rep(i,n){
ll a,b; cin >> a >> b;
ans += (a+b)*(b-a+1)/2;
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < n; ++i)
using ll = long long;
using P = pair<int,int>;
int main() {
ll n;cin>>n;
vector<ll> A(n),B(n+1,0),C(n+1,0),D(n+1,0);
rep(i,n)cin>>A[i];
B[0]=0;
rep(i,n)B[i+1]=B[i]+A[i];
C[0]=0;
rep(i,n)C[i+1]=max(C[i],B[i+1]);
D[0]=0;
rep(i,n)D[i+1]=D[i]+B[i+1];
ll ans=0;
rep(i,n){
ans=max(ans,D[i]+C[i+1]);
}
ans=max(ans,D[n]);
cout<<ans<<endl;
} | #include<bits/stdc++.h>
#define ll long long
#define mp make_pair
#define f(i,n) for(int i=0;i<n;i++)
#define F first
#define S second
#define pb push_back
using namespace std;
void test(){
ll n;
cin>>n;
ll a[n],b[n],c[n];
f(i,n)cin>>a[i];
f(i,n)cin>>b[i];
f(i,n)cin>>c[i];
ll freq[n+1];
f(i,n+1)freq[i] = 0;
f(i,n)freq[b[c[i]-1]]++;
ll ans = 0;
f(i,n){
ans = ans + freq[a[i]];
}
cout<<ans<<"\n";
}
int main(){
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tests=1;
// cin>>tests;
while(tests--){
test();
}
}
|
/*
* @Author: AsilenceBTF
* @LastEditTime: 2020-12-15 13:39:37
*/
#include<bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(), x.end()
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int N = 1e3 + 105;
int a[N], b[N], dp[N][N];
int main(){
int n, m; scanf("%d%d", &n, &m);
for(int i = 1; i <= n; ++i) scanf("%d", &a[i]);
for(int i = 1; i <= m; ++i) scanf("%d", &b[i]);
for(int i = 0; i <= n; ++i){
for(int j = 0; j <= m; ++j){
if(i == 0 && j == 0) continue;
dp[i][j] = INF;
if(i > 0){
dp[i][j] = min(dp[i][j], dp[i - 1][j] + 1);
}
if(j > 0){
dp[i][j] = min(dp[i][j], dp[i][j - 1] + 1);
}
if(i > 0 && j > 0){
dp[i][j] = min(dp[i][j], dp[i - 1][j - 1] + (a[i] != b[j]));
}
}
}
printf("%d\n", dp[n][m]);
// system("pause");
} | #include<bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#endif
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef std::vector<vector<int> > vvi;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pi> vpi;
typedef vector<pll> vpll;
typedef vector<vll> vvll;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vector<double>> vvd;
typedef vector<ld> vld;
typedef vector<vld> vvld;
typedef vector<bool> vb;
typedef vector<vb> vvb;
#define rep(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define repi(i, a, b) for (int i = (int)a; i >= (int)b; i--)
#define pb push_back
#define fi first
#define se second
#define all(a) a.begin(), a.end()
#define sz(a) (int)(a.size())
#define umin(a, x) a = min(a, x)
#define umax(a, x) a = max(a, x)
ll nxt() {
ll x; cin >> x; return x;
}
void setIO(string s) {
#ifndef LOCAL
ios_base::sync_with_stdio(0); cin.tie(0);
freopen((s+".in").c_str(),"r",stdin);
freopen((s+".out").c_str(),"w",stdout);
#endif
}
bool done[1005][1005];
int dp[1005][1005];
int a[1005], b[1005];
int n, m;
const int inf = 1e9;
int go(int x, int y) {
if (x >= 0 && y >= 0 && x <= n && y <= m) {
if (!done[x][y]) {
if (x == 0) {
dp[x][y] = y;
} else if (y == 0) {
dp[x][y] = x;
} else {
int one = (int)(a[x] != b[y]) + go(x - 1, y - 1);
int two = 1 + go(x - 1, y);
int three = 1 + go(x, y - 1);
int four = 2 + go(x - 1, y - 1);
dp[x][y] = min({one, two, three, four});
}
done[x][y] = 1;
return dp[x][y];
} else {
return dp[x][y];
}
} else {
return inf;
}
}
void solve() {
cin >> n >> m;
rep(i, 1, n + 1) cin >> a[i];
rep(i, 1, m + 1) cin >> b[i];
go(n, m);
cout << dp[n][m] << "\n";
}
int32_t main(){
// setIO("trapped");
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << fixed << setprecision(10);
int T = 1;
// cin >> T;
for (int tc = 1; tc <= T; tc++) {
// cout << "Case #" << tc << ": ";
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
#define deb(k) cerr << #k << ": " << k << "\n";
#define size(a) (int)a.size()
#define fastcin cin.tie(0)->sync_with_stdio(0);
#define st first
#define nd second
#define pb push_back
#define mk make_pair
#define int long long
typedef long double ldbl;
typedef double dbl;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef map<int, int> mii;
typedef vector<int> vint;
#define MAXN 300100
#define MAXLG 20
const int inf = 0x3f3f3f3f;
const ll mod = 1000000007;
const ll linf = 0x3f3f3f3f3f3f3f3f;
const int N = 300100;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
long double x, y, r;
bool check(long double a, long double b){
return sqrt((a-x)*(a-x) + (b-y)*(b-y)) <= r + 1e-14;
}
void solve(){
cin>>x>>y>>r;
int lo = x-r-10;
int hi = x+r+10;
int ans = 0;
for(int i=lo;i<=hi;i++){
if(!check(i, y)) continue;
long double j = i;
long double val = r*r - (j-x)*(j-x) + 0.1;
int down = -sqrt(val) + y - 10;
int up = sqrt(val) + y + 10;
while(down <= up && !check(j, down)) down++;
while(down <= up && !check(j, up)) up--;
if(down > up) continue;
//cout<<i<<" "<<up<<" "<<down<<"\n";
ans += up - down + 1;
}
cout<<ans<<"\n";
// Have you read the problem again?
// Maybe you understood the wrong problem
}
int32_t main(){
fastcin;
int t_ = 1;
//cin>>t_;
while(t_--)
solve();
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int maxn=222222,mod=998244353;
#define MP make_pair
#define PB push_back
#define lson o<<1,l,mid
#define rson o<<1|1,mid+1,r
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define ROF(i,a,b) for(int i=(a);i>=(b);i--)
#define MEM(x,v) memset(x,v,sizeof(x))
inline ll read(){
char ch=getchar();ll x=0,f=0;
while(ch<'0' || ch>'9') f|=ch=='-',ch=getchar();
while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
return f?-x:x;
}
int n,m,k,a[maxn],b[maxn],ans;
inline int qpow(int a,int b){
int ans=1;
for(;b;b>>=1,a=1ll*a*a%mod) if(b&1) ans=1ll*ans*a%mod;
return ans;
}
int main(){
n=read();m=read();k=read();
if(n==1){
printf("%d\n",qpow(k,m));
return 0;
}
if(m==1){
printf("%d\n",qpow(k,n));
return 0;
}
FOR(i,1,k) a[i]=qpow(i,n);
FOR(i,1,k) b[i]=qpow(k-i+1,m);
ROF(i,k,2) a[i]=(a[i]-a[i-1]+mod)%mod;
FOR(i,1,k) ans=(ans+1ll*a[i]*b[i])%mod;
printf("%d\n",ans);
} |
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP(i, s, n) for (int i = (s); i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
#define CHECK(a) cout << #a << " " << a << endl;
const int INF = 1e9;
const ll MOD = (ll)1e9 + 7LL;
const vector<int> dx = {1, 0, -1, 0, 1, 1, -1, -1};
const vector<int> dy = {0, 1, 0, -1, 1, -1, 1, -1};
void Main () {
int N;
cin >> N;
set<int> A;
REP (i, 0, N) {
int a;
cin >> a;
A.insert(a);
}
while (true) {
int maximum = *rbegin(A);
int minimum = *begin(A);
if (maximum == minimum) break;
else {
A.erase(maximum);
A.insert(maximum - minimum);
}
}
cout << *begin(A) << endl;
}
int main () {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
Main();
} | #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>;
int main(){
int N;
cin >> N;
ll sum1 = 0;
long double sum2 = 0;
ll m = -1;
rep(i, N) {
ll x;
cin >> x;
x = abs(x);
m = max(m, x);
sum1 += x;
sum2 += x*x*1.0;
}
cout << fixed << setprecision(15);
cout << sum1 << endl << sqrt(sum2) << endl << m;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
int main(){
char a,b,c;
cin>>a>>b>>c;
cout<<b<<c<<a<<endl;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define M 1000000007
ll powll(ll x, ll y) {
ll re=1;
rep(i, y) {
re = re * x % M;
}
return re;
}
ll gcd(ll a, ll b)
{
if (a%b == 0)
{
return(b);
}
else
{
return(gcd(b, a%b));
}
}
int main() {
ll n;
cin >> n;
vector<ll> outlist;
for (ll i = 1;; i++) {
ll j = 0;
j = n / i;
if (i > j) {
break;
}
if (n%i == 0) {
outlist.push_back(i);
outlist.push_back(j);
}
}
sort(outlist.begin(), outlist.end());
outlist.erase(unique(outlist.begin(), outlist.end()),outlist.end());
for (auto it = outlist.begin(); it != outlist.end(); ++it) {
cout << *it << endl;
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
template <class T>
void chmax(T& a, T b) {
a = max(a,b);
}
template <class T>
void chmin(T& a, T b) {
a = min(a,b);
}
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define all(container) container.begin(), container.end()
typedef long l;
typedef long long ll;
typedef pair<int, int> ipi;
typedef pair<ll, ll> lpl;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<vector<vector<ll>>> vvvl;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
#define MOD 1'000'000'007LL
ll LLINF = LLONG_MAX / 2;
int main() {
ll A, B;
cin >> A >> B;
for (int i=B-A;i > 0;i--) {
if ( ceil(double(A)/i) != B/i) {
cout << i;
return 0;
}
}
return 0;
}
| #include <algorithm>
#include <iostream>
#include <string>
#include <functional>
#include <vector>
#include <numeric>
#include <iomanip>
#include <utility>
#include <cmath>
#include <climits>
#include <queue>
#include <bitset>
#include <set>
#include <assert.h>
using namespace std;
typedef vector<vector<long> > dim2veclo;
typedef vector<vector<int> > dim2vecin;
typedef vector<vector<double> > dim2vecdo;
typedef vector<vector<long long> > dim2vecll;
typedef vector<int> vecin;
typedef vector<long> veclo;
typedef vector<double> vecdo;
typedef vector<long long> vecll;
typedef vector<bool> vecbo;
typedef vector<vector<bool> > dim2vecbo;
typedef long long ll;
typedef vector < pair<ll, ll> > vecpa;
typedef vector<string> vecst;
typedef pair<ll, ll> pail;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int dx[4] = { -1, 0, 1, 0 };
int dy[4] = { 0, 1, 0, -1 };
const ll INF = 100000000000000;
int main() {
int a, b;
cin >> a >> b;
int yaku = b - a;
for (int i = yaku; i >= 1; i--) {
if (a % i == 0) {
yaku = i;
break;
}
if (a + (i - a % i) + i <= b) {
yaku = i;
break;
}
}
cout << yaku << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
long long int n,p,x,a,i,mn=INT_MAX;
cin>>n;
float time=0.5;
for(i=0;i<n;++i)
{
cin>>a>>p>>x;
if(((float)x)>ceil(((float)a-0.5)))
mn=min(mn,p);
}
if(mn!=INT_MAX)
cout<<mn;
else
cout<<-1;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ul;
typedef long long ll;
#define printV(v) for(int i = 0;i < v.size();++i)cout << v[i] << endl
#define print(x) cout << x << endl
int main() {
int N,M,T;
cin >> N;
cin >> M;
cin >> T;
int n = N;
int t = 0;
bool ok = true;
for(int i = 0;i < M;++i){
int A,B;
cin >> A;
n -= A - t;
if(n <= 0){
ok = false;
break;
}
cin >> B;
n += B - A;
if(n >= N){
n = N;
}
t = B;
//print(n);
//print(t);
}
//print(n);
n -= (T - t);
if(n <= 0){
ok = false;
}
//print(n);
print((ok?"Yes":"No"));
}
|
#include <bits/stdc++.h>
//#include<boost/multiprecision/cpp_int.hpp>
//#include<boost/multiprecision/cpp_dec_float.hpp>
//#include <atcoder/all>
using namespace std;
#define rep(i, a) for (int i = (int)0; i < (int)a; ++i)
#define rrep(i, a) for (int i = (int)a; i > -1; --i)
#define REP(i, a, b) for (int i = (int)a; i < (int)b; ++i)
#define RREP(i, a, b) for (int i = (int)a; i > b; --i)
#define repl(i, a) for (ll i = (ll)0; i < (ll)a; ++i)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define popcount __builtin_popcount
#define popcountll __builtin_popcountll
#define fi first
#define se second
using ll = long long;
constexpr ll mod = 1e9 + 7;
constexpr ll mod_998244353 = 998244353;
constexpr ll INF = 1LL << 60;
// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
// using lll=boost::multiprecision::cpp_int;
// using
// Double=boost::multiprecision::number<boost::multiprecision::cpp_dec_float<128>>;//仮数部が1024桁
template <class T>
inline bool chmin(T &a, T b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <class T>
inline bool chmax(T &a, T b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
ll mypow(ll x, ll n, const ll &p = -1)
{ // x^nをmodで割った余り
if (p != -1)
{
x = (x % p + p) % p;
}
ll ret = 1;
while (n > 0)
{
if (n & 1)
{
if (p != -1)
ret = (ret * x) % p;
else
ret *= x;
}
if (p != -1)
x = (x * x) % p;
else
x *= x;
n >>= 1;
}
return ret;
}
struct myrand{
random_device seed;
mt19937 mt;
myrand():mt(seed()){}
int operator()(int a,int b){//[a,b)
uniform_int_distribution<int>dist(a,b-1);
return dist(mt);
}
};
//using namespace atcoder;
//------------------------
//------------------------
//------------------------
//------------------------
//------------------------
using P=pair<long double,long double>;
long double eps=1e-8;
void solve()
{
int n;
cin>>n;
vector<long double>a(n),b(n),c(n),d(n);
long double ga=0,gb=0,gc=0,gd=0;
rep(i,n)cin>>a[i]>>b[i],ga+=a[i],gb+=b[i],a[i]*=n,b[i]*=n;
rep(i,n)cin>>c[i]>>d[i],gc+=c[i],gd+=d[i],c[i]*=n,d[i]*=n;
if(n==1){
cout<<"Yes\n";
return;
}
int cnt=0,cnt2=0;
rep(i,n){
a[i]-=ga,b[i]-=gb;
c[i]-=gc,d[i]-=gd;
if(abs(a[i])<eps&&abs(b[i])<eps)++cnt;
if(abs(c[i])<eps&&abs(d[i])<eps)++cnt2;
}
if(cnt!=cnt2){
cout<<"No\n";
return;
}
vector<P>q(n);
rep(i,n)q[i]={c[i],d[i]};
auto mul=[](long double x,long double y,long double arg)->P{
P ret={cos(arg)*x+sin(arg)*y,-sin(arg)*x+cos(arg)*y};
return ret;
};
auto myequal=[](P &x,P &y)->bool{
return abs(x.fi-y.fi)<eps&&abs(x.se-y.se)<eps;
};
int id=0;
if(abs(a[id])<eps&&abs(b[id])<eps)id=1;
rep(t,n){
if(abs(c[t])<eps&&abs(d[t])<eps)continue;
vector<P>p(n);
long double argx=atan2(b[id],a[id]),argy=atan2(d[t],c[t]);
long double arg_sub=argx-argy;
rep(i,n){
p[i]=mul(a[i],b[i],arg_sub);
}
vector<bool>used(n);
bool ng=false;
rep(i,n){
bool flag=false;
rep(j,n){
if(!used[j]&&myequal(p[i],q[j])){
used[j]=true;
flag=true;
break;
}
}
if(!flag){
ng=true;
break;
}
}
if(!ng){
cout<<"Yes\n";
return;
}
}
cout<<"No\n";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(15);
solve();
return 0;
} | #include <algorithm>
#include <iostream>
#include <string>
#include <functional>
#include <vector>
#include <numeric>
#include <iomanip>
#include <utility>
#include <cmath>
#include <climits>
#include <queue>
#include <bitset>
#include <set>
#include <assert.h>
#include <map>
using namespace std;
typedef vector<vector<long> > dim2veclo;
typedef vector<vector<int> > dim2vecin;
typedef vector<vector<double> > dim2vecdo;
typedef vector<vector<long long> > dim2vecll;
typedef vector<int> vecin;
typedef vector<long> veclo;
typedef vector<double> vecdo;
typedef vector<long long> vecll;
typedef vector<bool> vecbo;
typedef vector<vector<bool> > dim2vecbo;
typedef long long ll;
typedef vector < pair<ll, ll> > vecpa;
typedef vector<string> vecst;
typedef pair<ll, ll> pail;
typedef pair<int, int> pain;
typedef vector<vecpa > dim2vecpa;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int dx[4] = { -1, 0, 1, 0 };
int dy[4] = { 0, 1, 0, -1 };
const ll MOD = 1000000007;
int dist(double x1, double y1, double x2, double y2) {
return (int)((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
int angG(double X, double Y, double x2, double y2, double x3, double y3) {
double gaiseki = (x2 - X) * (y3 - Y) - (y2 - Y) * (x3 - X);
return (int)(asin(gaiseki / (dist(X, Y, x2, y2) * dist(X, Y, x3, y3))) * 100000);
}
int angN(double X, double Y, double x2, double y2, double x3, double y3) {
double naiseki = (x2 - X) * (x3 - X) + (y2 - Y) * (y3 - Y);
return (int)(acos(naiseki / (dist(X, Y, x2, y2) * dist(X, Y, x3, y3))) * 100000);
}
int main() {
int n; cin >> n;
vecdo a = vecdo(n);
vecdo b = vecdo(n);
vecdo c = vecdo(n);
vecdo d = vecdo(n);
rep(i, n) cin >> a[i] >> b[i];
rep(i, n) cin >> c[i] >> d[i];
if (n == 1) {
cout << "Yes";
return 0;
}
if (n == 2) {
if (dist(a[0], b[0], a[1], b[1]) == dist(c[0], d[0], c[1], d[1])) {
cout << "Yes";
}
else cout << "No";
return 0;
}
ll length = dist(a[0], b[0], a[1], b[1]);
bool res = false;
vector<pair<int, pair<int, int>>> ans = vector<pair<int, pair<int, int>>>(n - 2);
for (int i = 2; i < n; i++) {
ans.push_back(make_pair(dist(a[0], b[0], a[i], b[i]), make_pair(angN(a[0], b[0], a[1], b[1], a[i], b[i]), angG(a[0], b[0], a[1], b[1], a[i], b[i]))));
}
sort(ans.begin(), ans.end());
rep(i, n) rep(j, n) {
if (i == j) continue;
if (length != dist(c[i], d[i], c[j], d[j])) continue;
bool flag = true;
rep(k, n) {
if (i == k) continue;
if (j == k) continue;
pair<int, pair<int, int>> isOK = make_pair(dist(c[i], d[i], c[k], d[k]), make_pair(angN(c[i], d[i], c[j], d[j], c[k], d[k]), angG(c[i], d[i], c[j], d[j], c[k], d[k])));
if (!binary_search(ans.begin(), ans.end(), isOK)) {
flag = false;
break;
}
}
if (flag) {
res = true;
break;
}
}
if (res) cout << "Yes";
else cout << "No";
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
int n;
cin >> n;
vector<pair<ll, int>> a(n);
for (auto& e : a) cin >> e.first >> e.second;
reverse(a.begin(), a.end());
ll s = 0, bot = -1e18, top = 1e18;
for (auto e : a) {
switch (e.second) {
case 1: {
s += e.first;
bot -= e.first;
top -= e.first;
break;
}
case 2: {
if (bot == top) break;
bot = max(bot, e.first);
bot = min(bot, top);
break;
}
case 3: {
if (bot == top) break;
top = min(top, e.first);
top = max(bot, top);
break;
}
}
}
int q;
cin >> q;
while (q--) {
ll x;
cin >> x;
cout << max(bot, min(top, x)) + s << endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a), i##end = (b); i < i##end; ++i)
#define per(i, a, b) for (int i = (a) - 1, i##end = (b); i >= i##end; --i)
#define REP(i, a) rep(i, 0, a)
#define PER(i, a) per(i, a, 0)
namespace IO {
const int MAXIOSIZE = 1 << 24 | 1;
unsigned char buf[MAXIOSIZE], *p1, *p2;
#define gc (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 24, stdin), p1 == p2) ? EOF : *p1++)
template <typename T> void read(T& x) {
x = 0; char ch = gc; bool flg = false;
for (; ch < '0' || '9' < ch; ch = gc) if (ch == '-') flg |= true;
for (; '0' <= ch && ch <= '9'; ch = gc) x = x * 10 + ch - '0';
flg ? x = -x : 0;
}
template <typename T> void out(const T& x) { if (x > 9) out(x / 10); putchar(x % 10 + '0'); }
template <typename T> inline void write(const T& x, const char& ed = ' ') { if (x < 0) putchar('-'), out(-x); else out(x); putchar(ed); }
}
typedef long long ll;
const int MAXN = 2e5 + 10;
int n;
ll add, L = -LONG_LONG_MAX >> 1, R = LONG_LONG_MAX >> 1;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif
IO::read(n);
REP(i, n) {
ll v; int t; IO::read(v), IO::read(t);
if (t == 1) add += v, L += v, R += v;
else if (t == 2) L = max(L, v), R = max(R, v);
else R = min(R, v), L = min(L, v);
}
int q; IO::read(q);
REP(i, q) {
ll x; IO::read(x); x += add;
IO::write(x <= L ? L : (x >= R ? R : x), '\n');
}
return 0;
}
|
#include <cstdio>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <iostream>
#include <sstream>
#include <ostream>
#include <cstring>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <iomanip>
#include <set>
#include <bitset>
//#include<unordered_map>
#define INT_MINs -2000000000
#define INT_MAXs 1000000001
#define MID int mid=(l+r)/2
#define REP1(i,x,y) for(int i=x;i<y;i++)
#define REP2(i,x,y) for(int i=x;i<=y;i++)
#define ls (2*k)
#define rs (2*k+1)
#define lson l,mid,2*k
#define rson mid+1,r,2*k+1
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define IOS ios::sync_with_stdio(0);cin.tie(NULL);
#define pb(a) push_back(a)
#define pi acos(-1)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll mod=20210213;
const ll ba=131;
//ll qk(ll x,ll y){ll ans=1;while(y){if(y&1) ans=ans*x;y>>=1;x=x*x;}return ans;}
const int dx[8] = { 0,-1,0,1,-1,-1,1,1}, dy[8] = { -1,0,1,0,-1,1,-1,1};
const int dxx[8]= {2,1,-1,-2,-2,-1, 1, 2},dyy[8]= {1,2,2,1, -1,-2,-2,-1};
int a[20],b[20],c[20];
int minn=inf;
int n;
int vis[20];
int dp[1000005][20];
void solve()
{
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i]>>b[i]>>c[i];
}
memset(dp,inf,sizeof dp);
dp[1][1]=0;
for(int i=1;i<(1<<n);i++){
for(int j=1;j<=n;j++){
if((1<<(j-1))&i==0) continue;
for(int k=1;k<=n;k++){
if((1<<(k-1))&i) continue;
dp[i|(1<<(k-1))][k]=min(dp[i|(1<<(k-1))][k],dp[i][j]+abs(a[k]-a[j])+abs(b[k]-b[j])+max(0,c[k]-c[j]));
}
}
}
for(int i=1;i<=n;i++){
minn=min(dp[(1<<n)-1][i]+abs(a[1]-a[i])+abs(b[1]-b[i])+max(0,c[1]-c[i]),minn);
}
cout<<minn<<endl;
}
int main()
{
IOS;
solve();
return 0;
}
| #include <bits/stdc++.h>
#include <math.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep1(i, n) for(int i = 1; i < n+1; i++)
#define all(A) A.begin(),A.end()
typedef long long ll;
vector<int> x,y,z;
int dist(int a,int b){
return abs(x[b]-x[a])+abs(y[b]-y[a])+max(0,z[b]-z[a]);
}
void infout(int a){
if(a == 1e9) cout << "INF";
else cout << a;
cout << ",";
}
void chmin(int& a, int b){
a = min(a,b);
}
int main(){
int n;
cin >> n;
x.resize(n);
y.resize(n);
z.resize(n);
rep(i,n) cin >> x[i] >> y[i] >> z[i];
vector<vector<int>> dp(1<<n,vector<int>(n,1e9));
rep(i,n){
if(i == 0) continue;
dp[1<<i][i] = dist(0,i);
}
rep(i,1<<n){
//from where?
rep(from,n){
//to where?
rep(to,n){
chmin(dp[i|1<<to][to],dp[i][from]+dist(from,to));
}
}
}
cout << dp[(1<<n)-1][0] << endl;
} |
#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
#define ll long long
#define maxn 1000005
#define inf 1e9
#define pb push_back
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
inline int read()
{
int x=0,w=1; char c=getchar();
while(c<'0'||c>'9') {if(c=='-') w=-1; c=getchar();}
while(c<='9'&&c>='0') {x=(x<<1)+(x<<3)+c-'0'; c=getchar();}
return w==1?x:-x;
}
inline ll pw(ll a,ll b,ll mod)
{
ll ans=1,base=a;
while(b)
{
if(b&1) ans=(ans*base)%mod;
base=(base*base)%mod; b>>=1;
}
return ans;
}
ll a,b,c;
int main()
{
cin>>a>>b>>c;
ll tmp=pw(b,c,4); tmp+=4;
ll ans=pw(a,tmp,10);
cout<<ans<<endl;
return 0;
} | #include<bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(register int i=(a);i<=(b);i++)
#define per(i,a,b) for(register int i=(a);i>=(b);i--)
using namespace std;
const int N=59;
inline long long read() {
register long long x=0, f=1; register char c=getchar();
while(c<'0'||c>'9') {if(c=='-') f=-1; c=getchar();}
while(c>='0'&&c<='9') {x=(x<<3)+(x<<1)+c-48,c=getchar();}
return x*f;
}
int n,m,col[N],res=0,ans=1;
bool vst[N];
bool ed[N][N];
vector<int>e[N],vec;
void dfs(int u,int ret=0) {
vst[u]=1;
vec.push_back(u);
for(auto v:e[u]) if(!vst[v]) dfs(v);
}
void sfd(int pos) {
if(pos==vec.size()) {
res++;
return;
}
rep(i,1,3) {
bool qwq=1;
rep(j,0,pos-1) if(ed[vec[pos]][vec[j]]&&col[j]==i) qwq=0;
if(!qwq) continue;
col[pos]=i;
sfd(pos+1);
}
}
signed main() {
n=read(), m=read();
rep(i,1,m) {
int u=read(), v=read();
e[u].push_back(v), e[v].push_back(u);
ed[u][v]=ed[v][u]=1;
}
rep(i,1,n) if(!vst[i]) {
vec.clear(), res=0;
dfs(i);
sfd(0);
ans*=res;
}
printf("%lld\n",ans);
return 0;
} |
#include <iostream>
using namespace std;
int main()
{
double x, y, z;
cin >> x >> y >> z;
double a = y / x;
for (double b = 1;; b++) {
if (b / z >= a) {
cout << int(b) - 1 << endl;
return 0;
}
}
} | #include <bits/stdc++.h>
using namespace std;
using ld = long double;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
#define max(p,q) ((p)>(q)?(p):(q))
#define min(p,q) ((p)<(q)?(p):(q))
#define rep(i,n) for (int i = 0, i##_len = (n); i < i##_len; i++)
#define rep1(i,n) for (int i = 1, i##_len = (n); i <= i##_len; i++)
#define repr(i,n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rep1r(i,n) for (int i = ((int)(n)); i >= 1; i--)
#define repl(i,l,n) for (int i = (l), i##_len = (n); i < i##_len; i++)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define RSORT(x) sort(rall(x));
#define mp make_pair
#define has_key(m, k) (m.find(k) != m.end())
#define INF (1e9)
#define EPS (1e-7)
// 総数を1000000007(素数)で割った余り
const ll mod = (1000000007);
ull gcd(ull a, ull b) { return b ? gcd(b, a % b) : a; }
ull lcm(ull a, ull b) { return a / gcd(a, b) * b; }
int main(){
long long a;
scanf("%lld",&a);
long long b;
scanf("%lld",&b);
long long c;
scanf("%lld",&c);
long long d;
scanf("%lld",&d);
cout << b - c << endl;
return 0;
}
|
// 2020-12-27 15:59:19
// clang-format off
#include <bits/stdc++.h>
#ifdef LOCAL
#include "lib/debug.hpp"
#else
#define debug(...) 1
#endif
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define rep(i, n) REP(i, 0, (n))
#define repc(i, n) REPC(i, 0, (n))
#define REP(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define REPC(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define REPCM(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
using namespace std;
using ll = long long;
using ld = long double;
using pr = pair<ll, ll>;
using vll = vector<ll>;
using vpr = vector<pr>;
using P = pair<int, int>;
template<class T> inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } else return false; }
template<class T> inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
// #include <atcoder/convolution>
// #include <atcoder/dsu>
// #include <atcoder/fenwicktree>
// #include <atcoder/lazysegtree>
// #include <atcoder/math>
// #include <atcoder/maxflow>
// #include <atcoder/mincostflow>
// #include <atcoder/modint>
// #include <atcoder/scc>
// #include <atcoder/segtree>
// #include <atcoder/string>
// #include <atcoder/twosat>
// #include <atcoder/all>
// using namespace atcoder;
// clang-format on
void answer() {
int n;
ll x;
cin >> n >> x;
vector<ll> a(n), xs(n);
rep(i, n) cin >> a[i];
{
ll w = x;
rep(i, n - 1) {
xs[i] = w % (a[i + 1] / a[i]);
w /= (a[i + 1] / a[i]);
}
xs[n - 1] = w;
}
vector<vector<ll> > dp(n + 1, vector<ll>(2, 0));
dp[0][0] = 1;
rep(i, n - 1) {
rep(j, 2) rep(k, 2) dp[i + 1][j] += dp[i][k];
if (xs[i] == 0) dp[i + 1][1] -= dp[i][0];
if (xs[i] == a[i + 1] / a[i] - 1) dp[i + 1][0] -= dp[i][1];
}
cout << dp[n - 1][0] + dp[n - 1][1] << '\n';
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
answer();
return 0;
} | //IQ134高知能系Vtuberの高井茅乃です。
//Twitter: https://twitter.com/takaichino
//YouTube: https://www.youtube.com/channel/UCTOxnI3eOI_o1HRgzq-LEZw
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF INT_MAX
#define LLINF LLONG_MAX
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define MODA 1000000007
#define MODB 998244353
template <typename T>
std::istream& operator>>(std::istream& is, std::vector<T>& vec) {
for (T& x: vec) { is >> x; }
return is;
}
int main() {
ll ans = 0;
ll tmp = 0;
int n; cin >> n;
vector<ll> a(n);
cin >> a;
map<ll, ll> m;
m[0]++;
REP(i, n){
if(i%2==0)tmp += a[i];
else tmp -= a[i];
m[tmp]++;
}
for(auto it = m.begin(); it != m.end(); it++){
ans += (it->second) * (it->second - 1LL) / 2;
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
const ll MAX = 1001001;
const ll MOD = 1000000007;
int main()
{
ll t;
cin >> t;
rep(i, t)
{
ll n;
cin >> n;
vector<ll> a(n);
map<ll, ll> m;
rep(i, n)
{
cin >> a[i];
m[a[i]]++;
}
if (n % 2 == 0)
{
bool b = true;
for (auto p : m)
{
auto v = p.second;
if (v % 2 != 0)
{
b = false;
}
}
if (b)
{
cout << "Second" << endl;
}
else
{
cout << "First" << endl;
}
}
else
{
cout << "Second" << endl;
}
}
} | // Template
#include "bits/stdc++.h"
#define rep_override(x, y, z, name, ...) name
#define rep2(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep3(i, l, r) for (int i = (int)(l); i < (int)(r); ++i)
#define rep(...) rep_override(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
constexpr int inf = 1001001001;
constexpr ll infll = 3003003003003003003LL;
template <typename T>
inline bool chmin(T &x, const T &y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template <typename T>
inline bool chmax(T &x, const T &y) {
if (x < y) {
x = y;
return true;
}
return false;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
for (T &element : vec) is >> element;
return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
for (int i = 0, vec_len = (int)vec.size(); i < vec_len; ++i) {
os << vec[i] << (i + 1 == vec_len ? "" : " ");
}
return os;
}
struct IOSET {
IOSET() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
}
} ioset;
// Main
int main() {
int n;
cin >> n;
vector<int> a(n), b(n), p(n);
cin >> a >> b >> p;
rep(i, n) {
--p[i];
}
rep(i, n) {
if (a[i] <= b[p[i]] && p[i] != i) {
cout << "-1\n";
return 0;
}
}
vector<int> idx(n);
iota(all(idx), 0);
sort(all(idx), [&](int i, int j) {return a[i] > a[j];});
vector<int> ansi, ansj;
for (int i : idx) {
while (p[i] != i) {
ansi.push_back(i + 1);
ansj.push_back(p[i] + 1);
swap(p[i], p[p[i]]);
}
}
cout << ansi.size() << "\n";
rep(i, ansi.size()) cout << ansi[i] << " " << ansj[i] << "\n";
}
|
#include "bits/stdc++.h"
using namespace std;
#define rep(i, a, b) for(int i=a; i<=b; i++)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int) x.size()
#define pb push_back
#define f first
#define s second
#define nl "\n"
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int MOD = 1e9+7;
// const int MOD = 998244353;
template<class T> using pqg = priority_queue<T,vector<T>,greater<T>>;
int n, m;
vector<pii> adj[100001];
int val[100001];
bool vis[100001];
void dfs(int x, int v){
val[x]=v;
vis[x]=true;
trav(k, adj[x]){
if(vis[k.f]) continue;
dfs(k.f, (k.s==v?(v==n?1:v+1): k.s));
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
cin >> n >> m;
rep(i, 1, m){
int u, v, c; cin >> u >> v >> c;
adj[u].pb({v, c});
adj[v].pb({u, c});
}
rep(i, 1, n){
if(vis[i]) continue;
dfs(i, 1);
}
rep(i, 1, n) cout << val[i] <<nl;
} | #include<bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
#define rrep(i,n) for(int i = (n)-1; i >= 0; i--)
#define rep1(i,n) for(int i = 1; i <= (n); i++)
#define rrep1(i,n) for(int i = (n); i > 0; i--)
#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>
#define MOD 1000000007
#define INF 1000000000000000LL
using namespace std;
#define MAXN 110000
vector<vector<int>>g(MAXN);
map<pi, int> costs;
vector<int>r(MAXN);
void dfs(int now=0, int p=-1){
//cout<<now<<' '<<p<<endl;
if(now==0)r[now]=0;
for(auto to: g[now]){
if(to==p || r[to]>=0)continue;
int c = costs[{now, to}];
if(r[now]==c)c++;
r[to] = c;
dfs(to, now);
}
}
int main(){
int n, m;cin>>n>>m;
g = vector<vector<int>>(n);
rep(i, m){
int u,v,c;cin>>u>>v>>c;u--,v--,c--;
g[u].push_back(v);
g[v].push_back(u);
costs[{u, v}] = c;
costs[{v, u}] = c;
}
rep(i, n)r[i]=-1;
dfs();
rep(i, n)cout<<r[i]%n+1<<endl;
return 0;
} |
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<map>
#include<cstring>
using namespace std;
int a[100][100];
const int N=1e6;
int L[N],dp[N];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
a[x][y]=1;
a[y][x]=1;
}
int R=1<<n;
for(int S=0;S<R;S++){
int flag=1;
for(int i=1;i<=n;i++){
int x1=i-1;
if(!((S>>x1)&1)) continue;
for(int j=i+1;j<=n;j++){
int x2=j-1;
if(!((S>>x2)&1)) continue;
if(a[i][j]==0){
flag=0;
break;
}
}
if(flag==0) break;
}
L[S]=flag;
}
for(int S=1;S<R;S++){
dp[S]=n+1;
for(int T=S;T;T=(T-1)&S){
if(L[T]) dp[S]=min(dp[S],dp[T^S]+1);
}
}
printf("%d\n",dp[R-1]);
} | #include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define REP(i,n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define debug(var) do{cout << #var << " : "; view(var);}while(0)
template<class T> bool chmin(T &a, T b) {if(a>b) {a=b;return 1;}return 0;}
template<class T> bool chmax(T &a, T b) {if(a<b) {a=b;return 1;}return 0;}
using namespace std;
template<class T> void view(T e) {cout << e << endl;}
template<class T> void view(const vector<T> &v) {for(const auto &e : v){cout << e << " ";} cout << endl;}
template<class T> void view(const vector<vector<T>> &vv) {for(const auto &v : vv){view(v);}}
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int,int>;
const int inf = 1<<30;
const ll inf_l = 1LL<<61;
const int MAX = 1e5;
int main() {
int n, m; cin >> n >> m;
vint g(n, 0);
rep(i,n) g[i] |= 1 << i;
rep(i,m) {
int a, b; cin >> a >> b;
a--; b--;
g[a] |= 1 << b;
g[b] |= 1 << a;
}
vector<bool> connect(1 << n, false);
connect[0] = true;
rep(i,1<<n) {
if (connect[i]) {
rep(j,n) {
if ((i & g[j]) == i) connect[i | (1 << j)] = true;
}
}
}
vint dp(1 << n, inf);
for (int i = 1; i < 1 << n; i++) {
if (connect[i]) dp[i] = 1;
else {
for (int j = i; j > 0; --j &= i) {
if (j == i) continue;
chmin(dp[i], dp[j] + dp[i ^ j]);
}
}
}
cout << dp.back() << endl;
} |
#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
int main() {
int n, m;
cin >> n >> m;
set<int> a, b, ans;
rep(i, n) {
int v;
cin >> v;
a.insert(v);
}
rep(i, m) {
int v;
cin >> v;
b.insert(v);
}
for (auto v : a) {
bool which = false;
for (auto x : b) {
if (v == x) which = true;
}
if (!which) ans.insert(v);
}
for (auto v : b) {
bool which = false;
for (auto x : a) {
if (v == x) which = true;
}
if (!which) ans.insert(v);
}
int count = 0;
for (auto v : ans) {
count++;
cout << v;
if (count < ans.size()) cout << " ";
}
cout << endl;
} | #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define rep1(i,n) for(int i=1;i<=(n);i++)
#define rrep1(i,n) for(int i=(n);i>0;i--)
#define fore(i_in,a) for (auto& i_in: a)
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fio() cin.tie(nullptr);ios::sync_with_stdio(false);
#define DEBUG_CTN(v) cerr<<#v<<":";for(auto itr=v.begin();itr!=v.end();itr++) cerr<<" "<<*itr; cerr<<endl
template<class T> bool chmax(T &a, const T &b) {if (a<b) { a = b; return true; } return false;}
template<class T> bool chmin(T &a, const T &b) {if (a>b) { a = b; return true; } return false;}
template<class T> void print(const T &t) { cout << t << "\n"; }
const ll INF = 1LL << 62;
const int iINF = 1 << 30;
int n;
string s,t;
int main() {
fio(); cin>>n>>s>>t;
vector<set<int>> dp(n+1);
dp[n].insert(0);
rrep(i,n){
int c = s[i]-'0';
if(t[i]=='T'){
rep(j,7) {
int x = (10*j+c)%7;
int y = (10*j)%7;
if(dp[i+1].count(x)!=0 or dp[i+1].count(y)!=0) dp[i].insert(j);
}
} else {
rep(j,7) {
int x = (10*j+c)%7;
int y = (10*j)%7;
if(dp[i+1].count(x)!=0 and dp[i+1].count(y)!=0) dp[i].insert(j);
}
}
}
print(dp[0].count(0)!=0?"Takahashi":"Aoki");
}
|
// Hail god Yato
#include <bits/stdc++.h>
using namespace std;
#define hs ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
typedef long long ll;
const ll MOD = 1000000007;
const ll INF = 1e18;
const ll MAX = 100001;
//
//
void solve(){
int n;
cin>>n;
vector<ll> vec(n);
for(int i = 0; i < n; i++)
cin>>vec[i];
vec.push_back(0);
sort(vec.begin(), vec.end());
ll ans = 1;
for(int i = 1; i <= n; i++){
ans *= (vec[i] - vec[i-1] + 1);
ans %= MOD;
}
cout<<ans;
}
signed main(){
hs;
ll t;
t=1;
// cin>>t;
for (int i=1; i<=t; i++){
//cout<<"Case #"<<i<<": ";
solve();
}
return 0;
} | #include <bits/stdc++.h>
#define LL long long
#define PII pair<int,int>
#define PIL pair<int,LL>
#define PLI pair<LL,int>
#define PIII pair<int,PII>
#define PLL pair<LL,LL>
#define PLII pair<LL,PII>
#define VI vector<int>
#define VVI vector<VI>
#define VL vector<LL>
#define VVL vector<VL>
#define VPII vector<PII>
#define FF first
#define SS second
#define MP make_pair
#define PB push_back
#define sqr(x) ((x) * (x))
#define all(x) x.begin(),x.end()
#define watch(x) cout<<(#x)<<" = "<<(x)<<'\n'
#define mset(a,v) memset(a,v,sizeof(a))
#define setp(x) cout<<fixed<<setprecision(x)
#define EPS 0.00000000001
#define PI acos(-1)
#define loop(i,b,n) for(int i=b;i<n;++i)
#define rev_loop(i,b,n) for(int i=b;i>=n;--i)
using namespace std;
const int MOD = 1e9 + 7;
const LL MX = 1e9;
const LL INF = 1e9;
int main()
{
//ofstream out("output.txt");
//ifstream in("input.txt");
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n;
cin>>n;
int a;
set<int> st;
loop(i,0,n)
{
cin>>a;
st.insert(a);
}
LL ans = 1, p = 0;
for(int x : st)
{
ans = (ans * (x - p + 1)) % MOD;
p = x;
}
cout<<ans<<'\n';
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); i++)
#define ALL(a) (a).begin(),(a).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
template<class T> inline bool chmin(T& a, T b) { if (a > b) {a = b; return true;} else return false;}
template<class T> inline bool chmax(T& a, T b) { if (a < b) {a = b; return true;} else return false;}
struct Edge {int to; int w; Edge(int to, int w) : to(to), w(w) {}; };
using Graph = vector<vector<Edge>>;
const int INF = 1<<30;
const ll INFL = 1LL<<60;
int lcs (vector<int> x, vector<int> y) {
int n = x.size();
int m = y.size();
vector<vector<int>> dp(n+1, vector<int>(m+1, INF));
dp[0][0] = 0;
rep(i, n+1) {
rep(j, m+1) {
if (j > 0) chmin(dp[i][j], dp[i][j-1] + 1);
if (i > 0) chmin(dp[i][j], dp[i-1][j] + 1);
if (i > 0 && j > 0) {
int cost = 0;
if (x[i-1] != y[j-1]) cost = 1;
chmin(dp[i][j], dp[i-1][j-1]+cost);
}
}
}
return dp[n][m];
}
int main() {
int n, m;
cin >> n >> m;
vector<int> a(n), b(m);
rep(i, n) cin >> a[i];
rep(i, m) cin >> b[i];
int ans = lcs(a, b);
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long int
#define double long double
#define pb push_back
#define fi first
#define se second
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pii>
#define pqi priority_queue<int>
#define test int tcase; cin>>tcase; for(int tc = 1; tc <= tcase; tc++)
#define inp(a,n,f) int a[n+f];for(int hh=f;hh<n+f;hh++)cin>>a[hh];
#define printdecimal(k) cout<<fixed<<setprecision(k);
#define mem(a,k) memset(a,k,sizeof(a))
#define ub upper_bound
#define lb lower_bound
#define mii map<int,int>
#define all(v) v.begin(),v.end()
#define mod (int)(1e9+7)
#define inf LLONG_MAX
int exp(int x,int y){int res=1;x=x%mod;while(y>0){if(y&1)res=(res*x)%mod;y=y>>1;x=(x*x)%mod;}return res;}
int modinv(int x){return exp(x,mod-2);}
int add(int a,int b){a%=mod,b%=mod;a=((a+b)%mod+mod)%mod;return a;}
int sub(int a,int b){a=((a-b)%mod+mod)%mod;return a;}
int mul(int a,int b){a%=mod,b%=mod;a=((a*b)%mod+mod)%mod;return a;}
#define N 1003
int dp[N][N],a[N],b[N];
int duck_duck_go(int idx1,int idx2,int n,int m)
{
if(dp[idx1][idx2]!=-1) return dp[idx1][idx2];
if(idx1==n || idx2==m) return (n-idx1)+(m-idx2);
int res=inf;
res=min(res,1+duck_duck_go(idx1+1,idx2,n,m));
res=min(res,1+duck_duck_go(idx1,idx2+1,n,m));
res=min(res,2+duck_duck_go(idx1+1,idx2+1,n,m));
res=min(res,(a[idx1]!=b[idx2])+duck_duck_go(idx1+1,idx2+1,n,m));
return dp[idx1][idx2]=res;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n,m;
cin>>n>>m;
for(int i=0;i<n;i++)
cin>>a[i];
for(int i=0;i<m;i++)
cin>>b[i];
mem(dp,-1);
cout<<duck_duck_go(0,0,n,m);
return 0;
} |
#include <bits/stdc++.h>
#define rep(i, n) for (lli i = 0; i < (n); i++)
#define rrep(i, n) for (lli i = (n)-1; i >= 0; i--)
#define vall(x) (x).begin(), (x).end()
using namespace std;
namespace atcoder {}
using namespace atcoder;
using lli = long long int;
void YESNO(bool), YesNo(bool);
template <class T1, class T2> bool chmin(T1 &l, const T2 &r);
template <class T1, class T2> bool chmax(T1 &l, const T2 &r);
#define int long long int
struct dijkstra {
vector<vector<pair<lli, pair<lli, lli>>>> e;
int n;
lli inf = 1e18;
dijkstra(int n) : n(n) { e.resize(n); }
void add(int a, int b, lli c, lli k) {
e[a].push_back(make_pair(b, make_pair(c, k)));
}
void biadd(int a, int b, lli c, lli k) {
add(a, b, c, k);
add(b, a, c, k);
}
vector<lli> get(int s) {
vector<lli> dist(n, inf);
dist[s] = 0;
using p = pair<lli, lli>;
priority_queue<p, vector<p>, greater<p>> que;
que.push(make_pair(0, s));
while (!que.empty()) {
lli d = que.top().first;
lli cur = que.top().second;
que.pop();
if (dist[cur] < d)
continue;
dist[cur] = d;
for (auto s : e[cur]) {
lli wei = s.second.first;
lli k = s.second.second;
lli to = s.first;
lli t = dist[cur] + wei + (d % k ? k - dist[cur] % k : 0);
if (dist[to] > t) {
dist[to] = t;
que.push(make_pair(t, to));
}
}
}
return dist;
}
};
void solve(long long N, long long M, long long X, long long Y,
std::vector<long long> A, std::vector<long long> B,
std::vector<long long> T, std::vector<long long> K) {
dijkstra dij(N);
rep(i, M) { dij.biadd(A[i] - 1, B[i] - 1, T[i], K[i]); }
auto t = dij.get(X - 1)[Y - 1];
if (t >= 1e16) {
cout << -1 << endl;
} else {
cout << t << endl;
}
}
signed main() {
long long N;
scanf("%lld", &N);
long long M;
scanf("%lld", &M);
long long X;
scanf("%lld", &X);
long long Y;
scanf("%lld", &Y);
std::vector<long long> A(M);
std::vector<long long> B(M);
std::vector<long long> T(M);
std::vector<long long> K(M);
for (int i = 0; i < M; i++) {
scanf("%lld", &A[i]);
scanf("%lld", &B[i]);
scanf("%lld", &T[i]);
scanf("%lld", &K[i]);
}
solve(N, M, X, Y, std::move(A), std::move(B), std::move(T), std::move(K));
return 0;
}
// -- lib
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
template <class T1, class T2> bool chmin(T1 &l, const T2 &r) {
return (l > r) ? (l = r, true) : false;
}
template <class T1, class T2> bool chmax(T1 &l, const T2 &r) {
return (l < r) ? (l = r, true) : false;
}
template <class T1, class T2> void vadd(vector<T1> &v, T2 x) {
for (auto &s : v)
s += T2(x);
} | #include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define ll long long
#define pii pair<ll,ll>
#define vi vector<ll>
#define mii map<ll,ll>
#define pqi priority_queue<ll> //max pq
#define pqd priority_queue<ll,vi,greater<ll> >
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define MOD 1000000007
#define inf 1e18
#define ps(x,y) fixed<<setprecision(y)<<x
#define pw(b,p) pow(b,p) + 0.1
#define f(i,k,n) for(ll i=k;i<n;i++)
#define fd(i,start,end) for(ll i=start;i>=end;i--)
ll power(ll a, ll b, ll mod) {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 power(ll a, ll b) {ll res = 1; assert(b >= 0); for (; b; b >>= 1) {if (b & 1)res = res * a ; a = a * a;} return res;}
ll min( ll a, ll b) { return (a < b) ? a : b;}
ll max(ll a, ll b) {return (a > b) ? a : b;}
ll gcd (ll a, ll b) {if (a == 0) return b; return gcd(b % a, a);}
void bwayne()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output2.txt", "w", stdout);
#endif
}
// ll n;
// cin >> n;
// vi a(n);
// f(i, 0, n) {
// cin >> a[i];
// }
// vector<vi> a(n,vector<vi>(m,0));
// f(i,0,n){
// f(j,0,m){
// cin>>a[i][j];
// }
// }
void solve() {
ll n;
cin >> n;
vi a(n + 1);
mii m;
f(i, 1, n + 1) {
cin >> a[i];
m[a[i]] = i;
}
vi ans;
set<ll> ansset;
fd(i, n, 2) {
ll curr = m[i];
// cout << curr << endl;
if (curr < i) {
while (curr < i) {
ll one = curr;
ll two = curr + 1;
ll el1 = a[one];
ll el2 = a[two];
swap(a[one], a[two]);
m[el2] = one;
m[el1] = two;
if (ansset.find(curr) != ansset.end()) {
cout << -1;
return;
}
ans.pb(curr);
ansset.insert(curr);
// cout << "&" << curr << endl;
// f(i, 1, n + 1) {
// cout << a[i] << " ";
// }
// cout << endl;
curr++;
}
}
else if (curr > i) {
cout << -1;
return;
}
}
if (ans.size() != n - 1) {
cout << -1;
return;
}
f(i, 0, n - 1) {
cout << ans[i] << endl;
}
}
int main()
{
bwayne();// remember
ll t = 1;
// cin >> t;
for (ll tt = 1; tt <= t; tt++)
{
// cout << "Case #" << tt << ": ";
solve();
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int n;
int a[200001];
int main() {
cin >> n;
int prev = 0;
int oPrev = 0;
vector<int> ans;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
if (i > a[i]) {
if (a[i] < prev) {
cout << "-1\n";
return 0;
}
int j = i;
while (j > a[i]) {
ans.push_back(j - 1);
j--;
}
prev = i;
} else {
if (a[i] < oPrev) {
cout << "-1\n";
return 0;
}
oPrev = a[i];
}
}
if (ans.size() != n - 1) {
cout << "-1\n";
return 0;
}
for (int s : ans) {
cout << s << "\n";
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define maxn 100010
const int MOD = 1000000007;
struct fenwick {
int n;
vector<int> bit;
fenwick(int _n) : bit(_n), n(_n) {}
void add(int idx, int val) {
for (; idx <= n; idx += idx & (-idx))
bit[idx] += val;
}
int pref(int idx) {
int ans = 0;
for (; idx > 0; idx -= idx & (-idx))
ans += bit[idx];
return ans;
}
int sum(int a, int b) { return pref(b) - pref(a - 1); }
};
void solve() {
int n;
cin >> n;
vector<int> v(n), inv(n);
for (int i = 0; i < n; ++i) cin >> v[i];
fenwick fw(n + 1);
int ct = 0;
map<int, int> mp;
for (int i = 0; i < n; i++) {
inv[i] = fw.sum(v[i], n);
fw.add(v[i], 1);
ct += inv[i];
mp[v[i]] = i;
}
if (ct != n - 1) {
cout << -1;
return;
}
vector<int> ans;
set<int> now;
for (int i = 0; i < n; i++) {
int pos = mp[i + 1];
if (pos == i)continue;
for (int j = pos; j >= i + 1; j--) {
swap(v[j], v[j - 1]), ans.push_back(j);
if (now.count(j)) {
cout << -1;
return;
}
now.insert(j);
}
}
for (auto x: ans)cout << x << endl;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
// cin >> t;
while (t--)
solve();
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
ll R,X,Y;
cin >> R >> X >> Y;
long double d = sqrt(X*X+Y*Y);
ll i = 1;
while(true){
if(i*R >= d){
if(i == 1 && i*R != d) cout << i+1 << endl;
else cout << i << endl;
return 0;
}
i++;
}
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
void solver1(ll R, ll X, ll Y, ll &ans){
ll dist_sq = X*X + Y*Y;
double dist = sqrt(dist_sq);
// ll move_sq = R*R;
// ll div = 0, mod = 0;
double div = 0;
div = dist/R;
// mod = dist_sq%R;
if (floor(div) == div) ans = div;
else ans = div + 1;
return;
}
void solver2(ll R, ll X, ll Y, ll &ans){
ll dist_sq = X*X + Y*Y;
double dist = sqrt(dist_sq);
if (dist < R){
ans = 2;
return;
}
ll cnt = 0;
while (dist > 0){
dist -= R;
cnt++;
}
ans = cnt;
return;
}
int main(){
ll R, X, Y;
cin >> R >> X >> Y;
ll ans = 0;
// solver1(R, X, Y, ans);
// cerr << ans << endl;
solver2(R, X, Y, ans);
// cerr << ans << endl;
cout << ans << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
int main()
{
int n, m;
cin >> n >> m;
vector<vector<int>> con(m, {0, 0});
rep(i, m) cin >> con[i][0] >> con[i][1];
int k;
cin >> k;
vector<vector<int>> k_s(k, {0, 0});
vector<int> choise(k);
rep(i, k) cin >> k_s[i][0] >> k_s[i][1];
int re = pow(2, k);
int ans = 0;
rep(i, re)
{
rep(j, k)
{
if ((i >> j) & 1 == 1)
{
choise[j] = k_s[j][1];
}
else
{
choise[j] = k_s[j][0];
}
}
int match = 0;
vector<vector<int>> count(m, {0, 0});
rep(j, k)
{
rep(l, m)
{
if (choise[j] == con[l][0])
{
count[l][0]++;
}
if (choise[j] == con[l][1])
{
count[l][1]++;
}
}
}
rep(j, m)
{
if (count[j][0] > 0 && count[j][1] > 0)
{
match++;
}
}
ans = max(ans, match);
}
cout << ans << endl;
}
| #include <algorithm>
#include <iomanip>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <numeric>
#include <vector>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define _rep(i, n) _rep2(i, 0, n)
#define _rep2(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(...) GET_MACRO(__VA_ARGS__, _rep2, _rep)(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using i64 = long long;
template<class T, class U>
bool chmin(T& a, const U& b) { return (b < a) ? (a = b, true) : false; }
template<class T, class U>
bool chmax(T& a, const U& b) { return (b > a) ? (a = b, true) : false; }
template<typename T>istream& operator>>(istream&i,vector<T>&v){rep(j,v.size())i>>v[j];return i;}
template<typename T>string join(vector<T>&v){stringstream s;rep(i,v.size())s<<' '<<v[i];return s.str().substr(1);}
template<typename T>ostream& operator<<(ostream&o,vector<T>&v){if(v.size())o<<join(v);return o;}
template<typename T>string join(vector<vector<T>>&vv){string s="\n";rep(i,vv.size())s+=join(vv[i])+"\n";return s;}
template<typename T>ostream& operator<<(ostream&o,vector<vector<T>>&vv){if(vv.size())o<<join(vv);return o;}
template<class T> using pq = priority_queue<T, vector<T>, greater<T>>;
char winnner(char a, char b)
{
if(a == b) return a;
if(a == 'R' && b == 'S')
{
return 'R';
}
if(a == 'S' && b == 'R')
{
return 'R';
}
if(a == 'P' && b == 'R')
{
return 'P';
}
if(a == 'R' && b == 'P')
{
return 'P';
}
if(a == 'S' && b == 'P')
{
return 'S';
}
if(a == 'P' && b == 'S')
{
return 'S';
}
}
int main()
{
int n, k;
cin >> n >> k;
string s;
cin >> s;
s += s;
rep(j, k)
{
string n_s = "";
for (int i = 0; i < int(s.size()) - 1; i += 2)
{
n_s += winnner(s[i], s[i + 1]);
}
s = n_s + n_s;
}
cout << s[0] << endl;
return 0;
} |
// Username: WillTheBill
// Time of creation: 2020-11-08 15:24
// Original filename: m.cp.cpp
#include<bits/stdc++.h>
using namespace std;
// shortcuts
#define pb push_back
#define fi first
#define se second
#define all(_v) _v.begin(),_v.end()
#define ll long long
// IO
#define multitest signed __T; cin >> __T; while(__T--)
template<typename T>
void _read(T& t);
template<typename T>
void _read(vector<T>&v);
template<typename T1, typename T2>
void _read(pair<T1,T2>&p);
template<typename T>
void _read(T& t){cin >> t;}
template<typename T>
void _read(vector<T>&v){for(unsigned _i=0;_i<v.size();_i++)_read(v[_i]);}
template<typename T1, typename T2>
void _read(pair<T1,T2>&p){_read(p.first);_read(p.second);}
void _masterread(){}
template<typename T,typename... V>
void _masterread(T& t, V&... v){_read(t);_masterread(v...);}
#define re(...)_masterread(__VA_ARGS__)
template<typename T>
void _print(T t);
template<typename T>
void _print(vector<T>&v);
template<typename T1, typename T2>
void _print(pair<T1,T2>&p);
template<typename T>
void _print(T t){cout<<t;}
template<typename T>
void _print(vector<T>&v){for(unsigned _i=0;_i<v.size();_i++)_print(v[_i]),cout<<(_i==v.size()-1?"":" ");}
template<typename T1, typename T2>
void _print(pair<T1,T2>&p){_print(p.first);cout<<" ";_print(p.second);}
void _masterprint(){cout<<endl;}
template<typename T,typename... V>
void _masterprint(T t, V... v){_print(t);if(sizeof...(v))cout<<" ";_masterprint(v...);}
#define pr(...)_masterprint(__VA_ARGS__)
// DEBUG
// colored output???
template <typename T> // start forward declaration
void _debug(T t);
template<typename T1,typename T2>
void _debug(pair<T1,T2> p);
template<typename T>
void _debug(vector<T>v);
template <typename T> // end forward declaration
void _debug(T t){cerr<<t;}
template<typename T1,typename T2>
void _debug(pair<T1,T2> p){cerr<<"{";_debug(p.first);cerr<<", ";_debug(p.second);cerr<<"}";}
template<typename T>
void _debug(vector<T>v){cerr<<"(";for(unsigned _i=0;_i<v.size();_i++)_debug(v[_i]),cerr<<(_i==v.size()-1?"":", ");cerr << ")";}
void _masterdebug(){cerr<<"]\n";}
template<typename T,typename... V>
void _masterdebug(T t,V... v){_debug(t);if(sizeof...(v))cerr<<", ";_masterdebug(v...);}
#ifdef __local__
#define debug(...) cerr<<"["<<#__VA_ARGS__<<"] = [";_masterdebug(__VA_ARGS__)
#else
#define debug(...)
#endif
// TYPES
// #define int long long
signed main(){
ios_base::sync_with_stdio(false); cin.tie(NULL);
int h,w,n,m; re(h,w,n,m);
vector<vector<int>> grid (h,vector<int> (w));
vector<vector<bool>> good (h, vector<bool> (w));
for(int i = 0; i < n; i++){
int a,b; re(a,b); a--,b--;
grid[a][b] = 1;
good[a][b] = 1;
}
for(int i = 0; i < m; i++){
int c,d; re(c,d);c--,d--;
grid[c][d] = 2;
}
for(int i = 0; i < h; i++){
int last = grid[i][0];
for(int j = 1; j < w; j++){
if(last == 1) good[i][j] = 1;
if(grid[i][j] != 0) last = grid[i][j];
}
last = grid[i][w-1];
for(int j = w - 2; j >= 0; j--){
if(last == 1) good[i][j] = 1;
if(grid[i][j] != 0) last = grid[i][j];
}
}
for(int j = 0; j < w; j++){
int last = grid[0][j];
for(int i = 1; i < h; i++){
if(last == 1) good[i][j] = 1;
if(grid[i][j] != 0) last = grid[i][j];
}
last = grid[h-1][j];
for(int i = h - 2; i >= 0; i--){
if(last == 1) good[i][j] = 1;
if(grid[i][j] != 0) last = grid[i][j];
}
}
int cnt = 0;
for(int i = 0; i < h; i++){
for(int j = 0; j < w; j++){
cnt += good[i][j] && grid[i][j] != 2;
}
}
pr(cnt);
}
| #include <bits/stdc++.h>
using namespace std;
int main(){
int H,W,N,M;
cin >> H >> W >> N >> M;
vector<vector<int>> G(H,vector<int>(W,0));
for(int i=0;i<N;i++){
int a,b;
cin >> a >> b;
a--;b--;
G[a][b] = 1;
}
for(int i=0;i<M;i++){
int c,d;
cin >> c >> d;
c--;d--;
G[c][d] = -1;
}
vector<vector<int>> X(H,vector<int>(0)),Y(W,vector<int>(0));
vector<vector<bool>> Xl(H,vector<bool>(0)),Yl(W,vector<bool>(0));
for(int i=0;i<H;i++){
bool light=0;
for(int j=0;j<W;j++){
if(G[i][j]==1) light = 1;
if(G[i][j]==-1){
X[i].push_back(j);
Xl[i].push_back(light);
light = 0;
}
}
X[i].push_back(W);
Xl[i].push_back(light);
}
for(int i=0;i<W;i++){
bool light=0;
for(int j=0;j<H;j++){
if(G[j][i]==1) light = 1;
if(G[j][i]==-1){
Y[i].push_back(j);
Yl[i].push_back(light);
light = 0;
}
}
Y[i].push_back(H);
Yl[i].push_back(light);
}
long long ans=0;
for(int i=0;i<H;i++){
for(int j=0;j<W;j++){
if(G[i][j]==-1) continue;
int a = lower_bound(X[i].begin(),X[i].end(),j) - X[i].begin(),
b = lower_bound(Y[j].begin(),Y[j].end(),i) - Y[j].begin();
if(Xl[i][a] || Yl[j][b]) ans++;
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
double r, x, y;
int main()
{
cin >> r >> x >> y;
double d = hypot(x, y);
cout << (d < r ? 2 : ceil(d / r)) << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define rep(i,n) for(LL i=0;i<(n);i++)
int main()
{
LL r, x, y;
LL output = 0;
long double distance;
cin >> r >> x >> y;
distance = sqrt(x*x + y*y);
if(r <= distance){
output = distance/r;
long double temp = distance/r;
//cout << "temp:" << temp << endl;
//cout << "dis:" << distance << endl;
if(output != temp)
output++;
}
else{
output = 2;
}
cout << output << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
long long dp[31][31] = {};
long long acolyte(int a, int b)
{
long long cost = 0;
if(dp[a][b])
{return dp[a][b];}
if(a + b <= 1)
{return 1;}
if(a)
{cost += acolyte(a - 1, b);}
if(b)
{cost += acolyte(a, b - 1);}
dp[a][b] = cost;
return cost;
}
void solution()
{
long long a, b, k;
cin >> a >> b >> k;
int sum = a + b;
string output = "";
while((b && a >= 0) || (a && b >= 0))
{
long long cost = 0;
if(a)
{
long long cost = 0;
if((cost = acolyte(a - 1, b)) < k)
{output += 'b'; b--; k -= cost;}
else
{output += 'a'; a--;}
}
else
{output += 'b'; b--;}
}
cout << output << endl;
return;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int count, counter = 0;
count = 1;
while(counter++ < count)
{solution();}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#endif
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef std::vector<vector<int> > vvi;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pi> vpi;
typedef vector<pll> vpll;
typedef vector<vll> vvll;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vector<double>> vvd;
typedef vector<ld> vld;
typedef vector<vld> vvld;
typedef vector<bool> vb;
typedef vector<vb> vvb;
#define rep(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define repi(i, a, b) for (int i = (int)a; i >= (int)b; i--)
#define pb push_back
#define fi first
#define se second
#define all(a) a.begin(), a.end()
#define sz(a) (int)(a.size())
#define umin(a, x) a = min(a, x)
#define umax(a, x) a = max(a, x)
ll nxt() {
ll x; cin >> x; return x;
}
void setIO(string s) {
#ifndef LOCAL
ios_base::sync_with_stdio(0); cin.tie(0);
freopen((s+".in").c_str(),"r",stdin);
freopen((s+".out").c_str(),"w",stdout);
#endif
}
void solve() {
ll n, w; cin >> n >> w;
const int maxt = 2e5 + 5;
vll a(maxt);
rep(i, 0, n) {
ll s, t, p; cin >> s >> t >> p;
a[s] += p; a[t] -= p;
}
rep(i, 1, n) a[i] = a[i - 1] + a[i];
cout << ((*max_element(all(a)) > w) ? "No" : "Yes") << endl;
}
int32_t main(){
// setIO("trapped");
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << fixed << setprecision(10);
int T = 1;
// cin >> T;
for (int tc = 1; tc <= T; tc++) {
// cout << "Case #" << tc << ": ";
solve();
}
}
|
// #include <bits/stdc++.h>
// string s;
// getline(cin, s);
// g++ -std=c++11 -O2 -Wall test.cpp -o test
// printf("%.9f\n", x); PRECISION
#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
using ll = long long;
#define F first
#define S second
const long long MOD = 1e9 + 7;
const double EPS = 1e-9;
const int MAX_LENGTH = (2 * 1e5 + 1);
int n, c;
long long x;
long long dp[MAX_LENGTH][2];
void solve() {
set<int> set_ids;
unordered_map<int, long long> max_pos;
unordered_map<int, long long> min_pos;
int MMIN = 0, MMAX = 1;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> x >> c;
set_ids.insert(c);
if (max_pos.find(c) == max_pos.end()) {
max_pos[c] = x;
min_pos[c] = x;
} else {
max_pos[c] = max(max_pos[c], x);
min_pos[c] = min(min_pos[c], x);
}
}
vector<int> ids(set_ids.begin(), set_ids.end());
n = ids.size();
dp[n - 1][MMIN] = abs(min_pos[ids[n - 1]] - max_pos[ids[n - 1]]) +
abs(max_pos[ids[n - 1]]);
dp[n - 1][MMAX] = abs(min_pos[ids[n - 1]] - max_pos[ids[n - 1]]) +
abs(min_pos[ids[n - 1]]);
for (int i = n - 2; i >= 0; i--) {
int cur = ids[i];
int next = ids[i + 1];
long long d = abs(min_pos[cur] - max_pos[cur]);
dp[i][MMIN] =
d + min(dp[i + 1][MMAX] + abs(max_pos[next] - max_pos[cur]),
dp[i + 1][MMIN] + abs(min_pos[next] - max_pos[cur]));
dp[i][MMAX] =
d + min(dp[i + 1][MMAX] + abs(max_pos[next] - min_pos[cur]),
dp[i + 1][MMIN] + abs(min_pos[next] - min_pos[cur]));
}
cout << min(abs(min_pos[ids[0]]) + dp[0][MMIN],
abs(max_pos[ids[0]]) + dp[0][MMAX]);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
// cin >> t;
for (int i = 1; i <= t; i++) {
// cout <<"Case #" << i << ": ";
solve();
}
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define S second
#define F first
#define f(i,n) for(int i=0;i<n;i++)
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define vi vector<int>
#define pii pair<int,int>
const int N = 105;
int n;
pair<double,double> a[N];
pii dsu[N];
int find(int x)
{
if(dsu[x].F == x) return x;
return dsu[x].F = find(dsu[x].F);
}
void merge(int x,int y)
{
int p = find(x);
int q = find(y);
if(p == q) return;
if(dsu[p].S > dsu[q].S) swap(p,q);
dsu[q].S+=dsu[p].S;
dsu[p].F = dsu[q].F;
}
//n is upper
//n+1 is lower
double dis(pair<double,double> x,pair<double,double> y)
{
return sqrt( ((x.F - y.F)*(x.F - y.F)) + ((x.S - y.S)*(x.S - y.S)) );
}
bool check(double r)
{
f(i,n+2) dsu[i] = {i,1};
f(i,n) f(j,n)
if(dis(a[i],a[j]) <= r + r) merge(i,j);
f(i,n) if(100 - a[i].S <= r + r) merge(i,n);
f(i,n) if(a[i].S + 100 <= r + r) merge(i,n+1);
return find(n) != find(n+1);
}
signed main()
{
fast;
cin >> n;
f(i,n) cin >> a[i].F >> a[i].S;
double l = 0.00000;
double r = 100.000000;
f(i,100)
{
double mid = (l+r)/2;
if(check(mid)) l = mid;
else r = mid;
}
cout << fixed << setprecision(4) << l;
} |
// E - Rush Hour 2
#include "bits/stdc++.h"
using namespace std;
#ifndef DEBUG
#define fundri 108
#define debug(...) 1729
#define endl '\n'
#endif
#define int int64_t
typedef pair<int, int> pii;
typedef vector<int> vi;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int rnd(int l = 0, int r = INT_MAX) {return uniform_int_distribution<int>(l, r)(rng);}
bool in_range(int x, int l, int r) {return l <= x && x <= r;}
template<typename H, typename ...T>void inp(H &head) {cin >> head;}
template<typename H, typename ...T>void inp(H &head, T &...tail) {cin >> head;inp(tail...);}
template<typename T>inline istream &operator >>(istream &in, vector<T> &a) {for(T &x : a)in >> x; return in;}
template<typename T, typename U>inline istream &operator >>(istream &in, pair<T, U> &a) {in >> a.first >> a.second; return in;}
// Multi-Dimension Vector
// Usage: vec<n, data-type> dp(dimention-1, dimention-2, ..., dimention-n, default = data-type())
template<int D, typename T> struct vec : public vector<vec<D - 1, T>> {
static_assert(D >= 1, "Vector dimensions must be greater than zero !!");
template<typename... Args>
vec(int n = 0, Args... args) : vector<vec<D - 1, T>>(n, vec<D - 1, T>(args...)){}
};
template<typename T> struct vec<1, T> : public vector<T> {vec(int n = 0, T val = T()) : vector<T>(n, val){}};
const int inf = 1e15;
const bool testcases = false;
const bool ready = []() -> bool {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef DHRUV_GHEEWALA
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
freopen("debug.txt", "w", stderr);
#endif
return true;
}();
void solve(int tc);
int32_t main(int32_t argc, char **argv) {
int TC = 1;
if(testcases) cin >> TC;
// Pre-Processing goes here...
for(int tc = 1; tc <= TC; ++tc) {
solve(tc);
fundri;
}
exit(0);
}
typedef vector<vector<vector<int>>> graph;
graph adj;
void add_edge(graph &a, int u, int v, int c, int d, bool dir = 0)
{
a[u].push_back({v, c, d});
if(!dir) a[v].push_back({u, c, d});
}
void clearAll(int n)
{
adj.clear();
adj.resize(n + 1);
}
void solve(int tc) {
int n, m;
cin >> n >> m;
clearAll(n);
int u, v, c, d;
for(int i = 0; i < m; i++) {
cin >> u >> v >> c >> d;
u--, v--;
add_edge(adj, u, v, c, d);
}
auto calc = [&](int c, int d, int t) {
return c + d / (t + 1) + t;
};
auto get = [&](int c, int d, int t) {
return calc(c, d, max(t, int(sqrt(d))));
int l = t, r = inf, ans = calc(c, d, t);
int m = l;
while(m < r) {
if(ans >= calc(c, d, m)) {
ans = calc(c, d, m);
m++;
} else {
m = r + 1;
}
}
return ans;
};
priority_queue<pii, vector<pii>, greater<pii>> pq;
vector<int> dist(n, inf);
pq.push({0, 0});
while(!pq.empty()) {
const auto fir = pq.top();
pq.pop();
debug(fir);
int t = fir.first;
int u = fir.second;
if(dist[u] <= t) continue;
dist[u] = t;
for(const auto &vec: adj[u]) {
int v = vec[0];
int c = vec[1];
int d = vec[2];
int cost = get(c, d, t);
debug(u, v, c, d, cost);
if(cost < dist[v])
pq.push({cost, v});
}
}
cout << (dist[n - 1] == inf ? -1 : dist[n - 1]) << endl;
} | #include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, k, n) for (int i = k; i < (int)(n); i++)
#define repd(i, n) for (int i = n-1; i >= 0; i--)
#define rrepd(i, k, n) for (int i = n-1; i >= (int)(k); i--)
#define all(x) (x).begin(),(x).end()
#define chmax(x,y) x=max(x,y)
#define chmin(x,y) x=min(x,y)
#define F first //pairの一つ目の要素
#define S second //pairの二つ目の要素
#define PB push_back //挿入
#define MP make_pair //pairのコンストラクタ
//V,Pは大文字i,l,bは小文字
using ll = long long;
using Vi = vector<int>;
using VVi = vector<Vi>;
using Vl = vector<ll>;
using VVl = vector<Vl>;
using Vb = vector<bool>;
using VVb = vector<Vb>;
using P = pair<int,int>;
using Pl = pair<ll, ll>;
using Vs = vector<string>;
//const ll mod = 1000000007;
const ll inf = 1000000000000000000;//10の18乗
#define yn {puts("Yes");}else{puts("No");}
#define dame { puts("-1"); return 0;}
const int mod = 998244353;
struct mint {
ll x; // typedef long long ll;
mint(ll x=0):x((x%mod+mod)%mod){}
mint operator-() const { return mint(-x);}
mint& operator+=(const mint a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint a) {
if ((x += mod-a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
mint operator+(const mint a) const { return mint(*this) += a;}
mint operator-(const mint a) const { return mint(*this) -= a;}
mint operator*(const mint a) const { return mint(*this) *= a;}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t>>1);
a *= a;
if (t&1) a *= *this;
return a;
}
mint inv() const { return pow(mod-2);}
mint& operator/=(const mint a) { return *this *= a.inv();}
mint operator/(const mint a) const { return mint(*this) /= a;}
};
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;
}
int main() {
ll n,m;
cin >> n >> m;
vector<vector<mint>> dp(50,vector<mint>(10000));
vector<mint> v(500005);
v[0]=1;
v[1]=1;
dp[0][0]=1;
rrep(i,1,10005){
v[i+1]=v[i].x*(i+1);
}
//cout << 0 << endl;
rrep(i,1,21){
ll x=modpow(2,i-1,mod);
rep(j,m+1){
if(dp[i-1][j].x==0) continue;
for(ll k=0;j+x*k<=m;k+=2){
//if(dp[i-1][j]==0)
if(k>n) break;
mint plus=v[n].x;
plus/=v[k].x;
plus/=v[n-k].x;
dp[i][j+x*k]+=dp[i-1][j].x*plus.x;
}
}
//cout << 0 << endl;
}
/*rep(i,20){rep(j,m+1){
cout << dp[i][j].x << " ";
}cout << endl;}*/
cout << dp[20][m].x << endl;
} |
#include <iostream>
#include <string>
using namespace std;
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <set>
#include <map>
#define MOD1 1000000007
#define MOD2 998244353
#define LIMIT1 200010
#define LIMIT2 500010
#define LIMIT3 1000010
#define INF ((1<<30)-1)
#define LLINF (1LL<<60)
#define EPS (1e-10)
typedef long long ll;
typedef long double ld;
typedef const void cv;
typedef pair<ll,ll> P;
#define rep(i,n) for((i)=0;(i)<(n);(i)++)
#define per(i,n) for((i)=(n)-1;(i)>=0;(i)--)
#define ALL(a) (a).begin(),(a).end()
#define ALL2(a, n) (a),(a)+(n)
template<class T,class C> T max(T a,C b){ return std::max(a, (T)b); }
template<class T,class C> T min(T a,C b){ return std::min(a, (T)b); }
#define zt(a,b) (max((a),(b))-min((a),(b)))
#define setpre(n) fixed << setprecision(n)
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return (a/gcd(a,b))*b;}
int dx[8]={1,0,-1,0,1,-1,-1,1},dy[8]={0,1,0,-1,1,1,-1,-1};
ll modpow(ll a, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1){
res *= a; if(mod>=1) res %= mod;
}
a *= a; if(mod>=1) a %= mod;
n >>= 1;
}
return res;
}
void initialize(){
}
int main(void){
initialize();
ll n,m,i,j,k,result=0;
int a[LIMIT2]={0};
string s="110",t;
cin >> n;
cin >> t;
m = modpow(10,10,0);
k = 0;
if(t=="1"){//コーナーケース
cout << 2*m << endl;
return 0;
}
for(i=0;i<n;i++){
if(i+2<=n){
if(t.substr(i,2)=="00"){//ダメ
cout << 0 << endl;
return 0;
}
}
if(i+3<=n){
if(t.substr(i,3)=="111"){//ダメ2
cout << 0 << endl;
return 0;
}
if(t.substr(i,3)=="010"){//ダメ3
cout << 0 << endl;
return 0;
}
}
}
for(i=0;i<n;i++){
if(t[i]=='0') k++;
}
if(t[n-1] == '1') k++;
result = m-k+1;
cout << result << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define rng(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); --i)
#define per(i, b) gnr(i, 0, b)
#define all(obj) begin(obj), end(obj)
#define allr(obj) rbegin(obj), rend(obj)
#define cinv(a) rep(i, (int)a.size()) cin >> a[i]
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> Pi;
typedef vector<Pi> vp;
typedef vector<vp> vvp;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }
template <typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, 1, 0, -1};
const int MOD = 1e9 + 7;
const int INF = 1e9;
const ll LINF = 1e18;
const long double eps = 1e-10;
const char nl = '\n';
ll power(ll a, ll b) { return b ? power(a * a % MOD, b / 2) * (b % 2 ? a : 1) % MOD : 1; }
ll nCk(int n, int k)
{
ll x = 1, y = 1;
for (int i = 1; i <= k; ++i)
{
x = x * (n - i + 1) % MOD;
y = y * i % MOD;
}
return x * power(y, MOD - 2) % MOD;
}
struct UF
{
vi d;
UF(int n) : d(n, -1) {}
int root(int x)
{
if (d[x] < 0)
return x;
return d[x] = root(d[x]);
}
bool unite(int x, int y)
{
x = root(x); y = root(y);
if (x == y) return false;
if (d[x] > d[y]) swap(x, y);
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) { return root(x) == root(y); }
int size(int x) { return -d[root(x)]; }
};
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout<<fixed<<setprecision(20);
int n; cin >> n;
vi x(n), y(n);
rep(i, n) cin >> x[i] >> y[i];
int x_min1 = INF, x_min2 = INF, y_min1 = INF, y_min2 = INF;
int x_max1 = 0, x_max2 = 0, y_max1 = 0, y_max2 = 0;
int index_x_max1, index_x_min1, index_y_max1, index_y_min1;
auto two_max = [&](int &max1, int &max2, int &index, vi &vec) -> void
{
rep(i, n)
{
if (max1 < vec[i])
{
max2 = max1;
max1 = vec[i];
index = i;
}
else if (max2 < vec[i])
{
max2 = vec[i];
}
}
};
two_max(x_max1, x_max2, index_x_max1, x);
two_max(y_max1, y_max2, index_y_max1, y);
auto two_min = [&](int &min1, int &min2, int &index, vi &vec) -> void
{
rep(i, n)
{
if (min1 > vec[i])
{
min2 = min1;
min1 = vec[i];
index = i;
}
else if (min2 > vec[i])
{
min2 = vec[i];
}
}
};
two_min(x_min1, x_min2, index_x_min1, x);
two_min(y_min1, y_min2, index_y_min1, y);
if (index_x_max1 == index_y_max1 && index_x_min1 == index_y_min1)
cout << max(max(x_max1 - x_min2, x_max2 - x_min1), max(y_max1 - y_min2, y_max2 - y_min1)) << nl;
else
{
vi ans;
ans.pb(x_max1 - x_min1);
ans.pb(y_max1 - y_min1);
ans.pb(x_max1 - x_min2);
ans.pb(y_max1 - y_min2);
ans.pb(x_max2 - x_min1);
ans.pb(y_max2 - y_min1);
sort(allr(ans));
cout << ans[1] << nl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int N; cin >> N;
int C[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
cin >> C[i][j];
}
}
int A[N], B[N];
A[0] = B[0] = 0;
int min_a = 0;
for (int i = 1; i < N; i++) {
int ok = C[0][i] - C[0][0];
for (int j = 0; j < N; j++) {
if (C[j][i] - C[j][0] != ok) {
cout << "No\n";
return 0;
}
}
A[i] = ok;
min_a = min(min_a, ok);
}
int min_b = 0;
for (int i = 1; i < N; i++) {
int ok = C[i][0] - C[0][0];
for (int j = 0; j < N; j++) {
if (C[i][j] - C[0][j] != ok) {
cout << "No\n";
return 0;
}
}
B[i] = ok;
min_b = min(min_b, ok);
}
for (int i = 0; i < N; i++) A[i] -= min_a;
for (int i = 0; i < N; i++) B[i] -= min_b;
int diff = C[0][0] - A[0] - B[0];
if (diff < 0) {
cout << "No\n";
return 0;
}
cout << "Yes\n";
for (int i = 0; i < N; i++) {
if (i) cout << " ";
cout << B[i] + diff;
}
cout << "\n";
for (int i = 0; i < N; i++) {
if (i) cout << " ";
cout << A[i];
}
cout << "\n";
return 0;
} | #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int,int>;
#define rep(i,n) for(int i=0; i<(int)n; i++)
#define llrep(i,n) for(ll i=0; i<(ll)n; i++)
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) //sortしてから使う
#define INF 2147483647
#define LLINF 9223372036854775807LL
#define PI (double)3.14159265358979323846;
#define fi first
#define se second
#define kiriage(x,y) (((x)+(y)-1)/(y))
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vp;
int main(){
int n; cin>>n;
int c[n][n];
rep(i,n) rep(j,n) cin>>c[i][j];
vi M(n);
rep(i,n){
int mn = INF;
rep(j,n){
mn = min(c[j][i], mn);
}
M[i] = mn;
}
vi ans(n);
rep(i,n){
int dif = c[i][0] - M[0];
rep(j,n){
if (dif != c[i][j] - M[j]){
cout << "No" << endl;
return 0;
}
}
ans[i] = dif;
}
cout << "Yes" << endl;
rep(i,n) cout << ans[i] << " ";
cout << endl;
rep(i,n) cout << M[i] << " ";
} |
#include <bits/stdc++.h>
using namespace std;
int n;
mt19937 mt;
const int TRIES = 750;
const int SHUFFLES = 0;
bool check(string s, vector<vector<char>>& a, int i, int j) {
bool isOk = true;
for (int k = 0; k < s.size() && isOk; ++k) {
if (s[k] != a[i][(j + k) % n]) {
isOk = false;
}
}
if (isOk) {
return true;
}
for (int k = 0; k < s.size() && isOk; ++k) {
if (s[k] != a[(i + k) % n][j]) {
isOk = false;
}
}
return isOk;
}
int calc(vector<string>& v, vector<vector<char>>& a) {
int cnt = 0;
for (string& s : v) {
bool found = false;
for (int i = 0; i < n && !found; ++i) {
for (int j = 0; j < n && !found; ++j) {
if (check(s, a, i, j)) {
found = true;
}
}
}
cnt += found;
}
return cnt;
}
pair<int, vector<vector<char>>> gen(vector<string>& v) {
shuffle(v.begin(), v.end(), mt);
vector<vector<char>> a(n, vector<char>(n));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
a[i][j] = char(mt() % 8 + int('A'));
}
}
int i = 0, j = 0;
for (auto s : v) {
if (j + s.size() >= n) {
j = 0;
++i;
}
if (i >= n) {
break;
}
for (int k = 0; k < s.size(); ++k, ++j) {
a[i][j] = s[k];
}
}
pair<int, vector<vector<char>>> res = {calc(v, a), a};
for (int i = 0; i < SHUFFLES; ++i) {
auto curr = res;
shuffle(curr.second.begin(), curr.second.end(), mt);
curr.first = calc(v, curr.second);
if (curr.first > res.first) {
res = curr;
}
}
return res;
}
int main() {
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
mt.seed(time(nullptr));
int m;
cin >> n >> m;
vector<string> v(m);
for (auto& i : v) {
cin >> i;
}
pair<int, vector<vector<char>>> res = gen(v);
for (int i = 0; i < TRIES; ++i) {
auto curr = gen(v);
if (curr.first > res.first) {
res = curr;
}
}
for (auto& i : res.second) {
for (auto& j : i) {
cout << j;
}
cout << '\n';
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
template<class T>
using V = vector<T>;
using ll = long long;
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for(int i=m; i<n; i++)
vector<int> par;
void init(int n){
for(int i=0; i<n; i++) par.at(i) = i;
}
int root(int x){
if(par.at(x) == x){
return x;
}else{
return par.at(x) = root(par[x]);
}
}
bool same(int x, int y){
return root(x) == root(y);
}
void unite(int x, int y){
x = root(x);
y = root(y);
if(x == y) return;
par.at(x) = y;
}
int main(){
int N, M; cin >> N >> M;
V<int> a(N), b(N), c(M), d(M);
rep(i, N) cin >> a.at(i);
rep(i, N) cin >> b.at(i);
rep(i, M){
cin >> c.at(i) >> d.at(i);
c.at(i)--; d.at(i)--;
}
par.resize(N);
init(N);
rep(i, M) unite(c.at(i), d.at(i));
V<ll> suma(N), sumb(N);
rep(i, N){
int r = root(i);
suma.at(r) += a.at(i);
sumb.at(r) += b.at(i);
}
bool flg = true;
rep(i, N){
if(suma.at(i) != sumb.at(i)) flg = false;
}
if(flg) cout << "Yes" << endl;
else cout << "No" << endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for (int i = 0; i < (n); ++i)
#define DREP(i,s,n) for(int i = (s); i < (n); i++)
template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b;return true;}return false;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b;return true;}return false;}
using ll = long long;
using P = pair<int,int>;
using Pl = pair<long long,long long>;
using veci = vector<int>;
using vecl = vector<long long>;
using vecveci = vector<vector<int>>;
using vecvecl = vector<vector<long long>>;
const int MOD = 998244353;
const double pi = acos(-1);
ll gcd(ll a, ll b) {if(b == 0) return a; else return gcd(b,a%b);}
ll lcm(ll a, ll b) {return a*b/gcd(a,b);}
template<int MOD> struct Fp {
long long val;
constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
if (val < 0) val += MOD;
}
constexpr int getmod() { return MOD; }
constexpr Fp operator - () const noexcept {
return val ? MOD - val : 0;
}
constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
constexpr Fp& operator += (const Fp& r) noexcept {
val += r.val;
if (val >= MOD) val -= MOD;
return *this;
}
constexpr Fp& operator -= (const Fp& r) noexcept {
val -= r.val;
if (val < 0) val += MOD;
return *this;
}
constexpr Fp& operator *= (const Fp& r) noexcept {
val = val * r.val % MOD;
return *this;
}
constexpr Fp& operator /= (const Fp& r) noexcept {
long long a = r.val, b = MOD, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b; swap(a, b);
u -= t * v; swap(u, v);
}
val = val * u % MOD;
if (val < 0) val += MOD;
return *this;
}
constexpr bool operator == (const Fp& r) const noexcept {
return this->val == r.val;
}
constexpr bool operator != (const Fp& r) const noexcept {
return this->val != r.val;
}
friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept {
return os << x.val;
}
friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
if (n == 0) return 1;
auto t = modpow(a, n / 2);
t = t * t;
if (n & 1) t = t * a;
return t;
}
};
using mint = Fp<MOD>;
int main() {
int N,M; cin >> N >> M;
int K; cin >> K;
if(N == 1) {
cout << modpow((mint)K,(ll)M) << endl;
} else if(M == 1) {
cout << modpow((mint)K,(ll)N) << endl;
} else {
mint ans = 0;
for(int x = 1; x <= K; x++) {
ans += (modpow((mint)x,(ll)N)-modpow((mint)(x-1),(ll)N)) * modpow((mint)(K-x+1),(ll)M);
}
cout << ans << endl;
}
return 0;
} |
#include<bits/stdc++.h>
#define int long long
#define ri register signed
#define rd(x) x=read()
#define endl '\n'
using namespace std;
const int N=2e6+5;
const int M=1e6+5;
const int maxn=5e6+5;
const int mod=998244353;
const int inf=0x3f3f3f3f;
inline int read(){int x=0,f=1;char ch=getchar();while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
inline int ksm(int x,int y=mod-2,int z=mod){int ret=1;while (y){if (y&1) ret=(ret*x)%z;x=(x*x)%z;y>>=1;}return ret;}
int inv[N],fac[N],ifc[N];
void gmax(int &x,int y){x=max(x,y);}
void Init(int n)
{
inv[1]=1;for (ri i=2;i<=n;i++)inv[i]=inv[mod%i]*(mod-mod/i)%mod;
fac[0]=1;for (ri i=1;i<=n;i++) fac[i]=fac[i-1]*i%mod;
ifc[0]=1;for (ri i=1;i<=n;i++) ifc[i]=ifc[i-1]*inv[i]%mod;
}
int C(int n,int m){if (n>mod) return 0;if (m>n || m<0)return 0;return fac[n]*ifc[m]%mod*ifc[n-m]%mod;}
int c(int n,int m){if (n<mod) return C(n,m);return C(n%mod,m%mod)*c(n/mod,m/mod)%mod;}
int n,m,k,ans;
int a[N],s[N];
signed main()
{
rd(n);rd(m);rd(k);
if (n==1)
{
cout<<ksm(k,m)<<endl;return 0;
}
if (m==1)
{
cout<<ksm(k,n)<<endl;return 0;
}
Init(2*(n+m));
for (int i=1;i<=k;i++)
{
a[i]=ksm(i,n)-s[i-1];
a[i]%=mod;
s[i]=s[i-1]+a[i];
s[i]%=mod;
ans+=a[i]*ksm(k-i+1,m)%mod;
ans%=mod;
}
cout<<(ans+mod)%mod<<endl;
} |
#include <iostream>
using namespace std;
int main()
{
int N,M;
cin>>N>>M;
cout<<N/M;
} | #include <bits/stdc++.h>
using namespace std;
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define lop ios; ll t; cin>>t; while(t--)
#define pb push_back
#define vi vector<ll>
#define vivi vector<vector<ll>>
#define vipa vector<pair<ll,ll>>
#define F first
#define S second
#define M 1000000007
#define kl "\n"
#define _p(v) for(auto i:v)cout<<i<<" ";
#define all(v) v.begin(),v.end()
#define rll(v) v.rbegin(),v.rend()
typedef long long ll;
ll lcm(ll x, ll y){ll k=x*y; k/=(__gcd(x,y)); return k;}
//ll power(ll x,ll y){ ll r= 1;x = x % M;if(x==0) return 0; while(y>0) {if(y&1) r=(r*x)%M; y=y>>1; x=(x*x)%M;} return r;}
void solve()
{
ll n,m,k,i,j;
cin>>n>>m;
k=n/m;
cout<<k;
}
int main()
{
ios
// ll t;
// cin>>t;
// while(t--)
solve();
return 0;
} |
/*ver 7*/
#include <bits/stdc++.h>
using namespace std;
void init() {cin.tie(0);ios::sync_with_stdio(false);cout << fixed << setprecision(15);}
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vd = vector<ld>;
using vs = vector<string>;
using vb = vector<bool>;
using vvl = vector<vector<ll>>;
using vvd = vector<vector<ld>>;
using vvs = vector<vector<string>>;
using vvb = vector<vector<bool>>;
using pll = pair<ll,ll>;
using mll = map<ll,ll>;
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
#define each(x,v) for(auto& x : v)
#define reps(i,a,b) for(ll i=(ll)a;i<(ll)b;i++)
#define rep(i,n) for(ll i=0;i<(ll)n;i++)
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define mp make_pair
const ll INF = 1LL << 60;
#define CLR(mat,f) memset(mat, f, sizeof(mat))
#define IN(a, b, x) (a<=x&&x<=b)
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() ) //被り削除
#define debug cout << "line : " << __LINE__ << " debug" << endl;
#define ini(...) int __VA_ARGS__; in(__VA_ARGS__)
#define inl(...) long long __VA_ARGS__; in(__VA_ARGS__)
#define ind(...) long double __VA_ARGS__; in(__VA_ARGS__)
#define ins(...) string __VA_ARGS__; in(__VA_ARGS__)
#define inc(...) char __VA_ARGS__; in(__VA_ARGS__)
void in(){}
template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);}
template <typename T> void in1(T &s) {rep(i,s.size()){in(s[i]);}}
template <typename T> void in2(T &s,T &t) {rep(i,s.size()){in(s[i],t[i]);}}
template <typename T> void in3(T &s,T &t,T &u) {rep(i,s.size()){in(s[i],t[i],u[i]);}}
template <typename T> void in4(T &s,T &t,T &u,T &v) {rep(i,s.size()){in(s[i],t[i],u[i],v[i]);}}
template <typename T> void in5(T &s,T &t,T &u,T &v,T &w) {rep(i,s.size()){in(s[i],t[i],u[i],v[i],w[i]);}}
void out(){cout << endl;}
template <typename T,class... U> void out(const T &t,const U &...u){cout << t; if(sizeof...(u)) cout << " "; out(u...);}
void die(){cout << endl;exit(0);}
template <typename T,class... U> void die(const T &t,const U &...u){cout << t; if(sizeof...(u)) cout << " "; die(u...);}
template <typename T> void outv(T s) {rep(i,s.size()){if(i!=0)cout<<" ";cout<<s[i];}cout<<endl;}
template <typename T> void out1(T s) {rep(i,s.size()){out(s[i]);}}
template <typename T> void out2(T s,T t) {rep(i,s.size()){out(s[i],t[i]);}}
template <typename T> void out3(T s,T t,T u) {rep(i,s.size()){out(s[i],t[i],u[i]);}}
template <typename T> void out4(T s,T t,T u,T v) {rep(i,s.size()){out(s[i],t[i],u[i],v[i]);}}
template <typename T> void out5(T s,T t,T u,T v,T w) {rep(i,s.size()){out(s[i],t[i],u[i],v[i],w[i]);}}
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
template <typename T> T allSum(vector<T> a){T ans=T();each(it,a)ans+=it;return ans;}
ll ceilDiv(ll a,ll b) {return (a+b-1)/b;}
ld dist(pair<ld,ld> a, pair<ld,ld> b){return sqrt(abs(a.fi-b.fi)*abs(a.fi-b.fi)+abs(a.se-b.se)*abs(a.se-b.se));} // 2点間の距離
ll gcd(ll a, ll b) { return b != 0 ? gcd(b, a % b) : a; }
ll lcm(ll a,ll b){ return a / gcd(a,b) * b;}
template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }
#define YES(n) ((n) ? "YES" : "NO" )
#define Yes(n) ((n) ? "Yes" : "No" )
#define yes(n) ((n) ? "yes" : "no" )
int main(){
init();
inl(a,b,c);
V<ll> d{a,b,c};
map<V<ll>,ld> memo;
memo[d]=1;
deque<V<ll>> q;
ld ans=0;
q.push_back(d);
while(q.size()!=0){
V<ll> now=q.front();
q.pop_front();
if(now[0]==100 ||now[1]==100 ||now[2]==100){
ans+=(now[0]+now[1]+now[2]-d[0]-d[1]-d[2])*memo[now];
continue;
}
ll sum=now[0]+now[1]+now[2];
rep(i,3){
if(now[i]!=0){
V<ll> next=now;
next[i]++;
if(memo.count(next)==0){
q.push_back(next);
memo[next]=0;
}
memo[next]+=memo[now]*now[i]/sum;
}
}
}
out(ans);
return 0;
} | #include <iostream>
#include <iomanip>
double dp[101][101][101];
double dfs(int a, int b, int c){
if(dp[a][b][c]) return dp[a][b][c];
if(a==100 || b==100 || c==100) return 0;
double ret = (dfs(a+1, b, c) + 1) * a / (a+b+c);
ret += (dfs(a, b+1, c) + 1) * b / (a+b+c);
ret += (dfs(a, b, c+1) + 1) * c / (a+b+c);
dp[a][b][c] = ret;
return ret;
}
int main(){
int A, B, C;
std::cin >> A >> B >> C;
std::cout << std::fixed << std::setprecision(10) << dfs(A, B, C) << std::endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<ll> vll;
#define PI (2*acos(0.0))
#define eps 1e-9
#define pb push_back
#define endl "\n"
#define watch(x) cout << (#x) << " is " << (x) << endl;
#define show(v) for(int fi = 0; fi < v.size(); fi++) cout << v[fi] << " "; cout << endl;
#define showpair(v) for(int fi = 0; fi < v.size(); fi++) cout << v[fi].first << " " << v[fi].second << endl;
#define ff first
#define ss second
#define fu cout << "lol" << endl;
#define precision(n) cout << fixed << setprecision(n);
#define lb lower_bound
#define up upper_bound
#define vscan for(i = 0;i<n;i++){cin>>in; v.pb(in);}
#define all(a) a.begin(), a.end()
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define mem(a,val) memset(a,val,sizeof(a))
#define loop(i,n) for(i = 0; i < n; i++)
#define TC ull T; cin>>T; while(T--)
#define IN(x) {scanf("%d",&x);}
#define LL(x) {scanf("%lld",&x);}
#define CC(x) {scanf("%c",&x);}
#define pfl(x) printf("%d\n",x)
#define pfll(x) printf("%lld\n",x)
#define newl puts("")
#define space printf(" ")
#define MOD 1000000007
#define speed ios_base::sync_with_stdio(false); cin.tie(NULL);
#define ar array
int n, k;
int arr[60][60];
int vis[60];
ll ans = 1;
int sz[60], dsu[60];
ll fact[60];
ll mod = 998244353;
int parent(int node){
if(dsu[node] == node) return node;
return dsu[node] = parent(dsu[node]);
}
void connect(int a, int b){
a = parent(a), b = parent(b);
if(a == b) return;
dsu[a] = b, sz[b] += sz[a];
}
void comprow(int row){
for(int i = 1; i <= n; i++){
if(i == row) continue;
bool f = 1;
for(int j = 1; j <= n; j++){
if(arr[row][j] + arr[i][j] > k) f = 0;
}
if(f) connect(row, i);
}
}
void compcol(int col){
for(int i = 1; i <= n; i++){
if(i == col) continue;
bool f = 1;
for(int j = 1; j <= n; j++){
if(arr[j][col] + arr[j][i] > k) f = 0;
}
if(f) connect(col, i);
}
}
void factorial(){
fact[0] = 1;
for(int i = 1; i <= 50; i++) fact[i] = fact[i-1]*i, fact[i] %= mod;
}
void solve(){
cin>>n>>k;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
cin>>arr[i][j];
}
}
for(int i = 1; i <= n; i++){
vis[i] = false, dsu[i] = i, sz[i] = 1;
}
for(int i = 1; i <= n; i++){
comprow(i);
}
factorial();
for(int i = 1; i <= n; i++){
if(vis[ parent(i) ]) continue;
vis[ parent(i) ] = true;
ans *= fact[sz[ parent(i) ]], ans %= mod;
}
for(int i = 1; i <= n; i++){
vis[i] = false, dsu[i] = i, sz[i] = 1;
}
for(int i = 1; i <= n; i++){
compcol(i);
}
for(int i = 1; i <= n; i++){
if(vis[ parent(i) ]) continue;
vis[ parent(i) ] = true;
ans *= fact[sz[ parent(i) ]], ans %= mod;
}
cout << ans << endl;
}
int main()
{
solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i,x,n) for(int i = x; i<=n; i++)
#define FORO(i,n) for(int i = 0; i<n; i++)
#define nl "\n"
#define sp " "
#define MOD 1000000007
#define INF 1000000000000000000
#define pb push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vector<int> >
#define vvl vector<vector<ll> >
#define vpi vector<pair<int, int> >
#define vpl vector<pair<ll, ll> >
#define pi pair<int, int>
#define pl pair<ll, ll>
#define ff first
#define ss second
ll n, K, a[51][51], parent[51], sizes[51];
ll mod = 998244353;
ll fact(ll a){
ll ans = 1;
FOR(i,2,a){
ans*=i;
ans%=mod;
}
return ans;
}
void make_set(int v){
parent[v] = v;
sizes[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(sizes[a]<sizes[b]) swap(a, b);
parent[b] = a;
sizes[a]+=sizes[b];
}
}
int main(){
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
cin>>n>>K;
FORO(i,n){
FORO(j,n) cin>>a[i][j];
}
FORO(i,n) make_set(i);
FORO(i,n){
FORO(j,n){
bool fl = 1;
FORO(k,n){
if(a[k][i]+a[k][j]>K){
fl = 0;
break;
}
}
if(fl) union_sets(i,j);
}
}
ll ans = 1;
FORO(i,n){
if(parent[i]==i) ans = ans*fact(sizes[i])%mod;
}
FORO(i,n) make_set(i);
FORO(i,n){
FORO(j,n){
bool fl = 1;
FORO(k,n){
if(a[i][k]+a[j][k]>K){
fl = 0;
break;
}
}
if(fl) union_sets(i,j);
}
}
FORO(i,n){
if(parent[i]==i) ans = ans*fact(sizes[i])%mod;
}
cout<<ans;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin >> s;
int n = s.size();
int i = 0, j = n-1;
while(s[i] == '0')
i++;
while(s[j] == '0')
j--;
while(i < j){
if(s[i] != s[j]){
cout << "No";
exit(0);
}
i++, j--;
}
cout << "Yes";
return 0;
} | /*************************************************************************
> File Name: solve.cpp
> Author: liupo
> Mail: [email protected]
> Created Time: 2021-02-28 21:37:24
************************************************************************/
#define GOODOJ
#define SYNC 0
#ifdef GOODOJ
#include <bits/stdc++.h>
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#include <chrono>
#include <random>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#else
#include <iostream>
#include <cstdio>
#include <cmath>
#include <set>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <deque>
#include <vector>
#include <limits>
#include <cassert>
#include <sstream>
#include <iterator>
#include <functional>
#endif
using namespace std;
#define endl '\n'
#define fep(i,b,e) for(int i=(b);i<(e);++i)
#define rep(i,x) for(int i=0;i<(x);++i)
#define rap(i,x) for(auto& i : (x))
#define seg(t) (t).begin(), (t).end()
#define ep emplace_back
#define mkp make_pair
#define qxx(i,x) for(int i = head[x]; ~i; i = node[i].nex)
#define F first
#define S second
#define lowbit(x) ((-(x))&(x))
#define RE register
#define getchar() getchar_unlocked()
#ifdef DEBUG
void err(istream_iterator<string>){}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << ' ';
err(++it, args...);
}
#define debug(args...) {string _s=#args;replace(seg(_s),',',' ');\
cerr<<"DEBUG:";istringstream it(_s);\
err(istream_iterator<string>(it), args);cerr<<endl;}
#else
#define debug(...)
#endif
template<typename T> inline bool cmax(T& a,const T& b) {return a<b?a=b,1:0;}
template<typename T> inline bool cmin(T& a,const T& b) {return a>b?a=b,1:0;}
#ifdef GOODOJ
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
typedef __gnu_pbds::priority_queue<int> pq;
#endif
typedef std::string str;
typedef long long ll;
typedef double db;
typedef pair<int, int> pa;
const double P = acos(-1.0), eps = 1e-9;
struct point { db x ,y;};
inline int sign(db a) {return a < -eps ? -1 : a > eps;}
#define dot(p1,p2,p3) ((p2.x-p1.x)*(p3.x-p1.x)+(p2.y-p1.y)*(p3.y-p1.y))
#define cross(p1,p2,p3) ((p2.x-p1.x)*(p3.y-p1.y)-(p3.x-p1.x)*(p2.y-p1.y))
#define crossOp(p1,p2,p3) sign(cross(p1,p2,p3))
constexpr int Ma = 1e6, inf = 0x3f3f3f3f, mod = 1e9 + 7;
void solve() {
str s; cin >> s;
while (!s.empty() and s.back() == '0') s.pop_back();
for (int i = 0; i < s.length() / 2; i++) if (s[i] != s[s.length() - 1 - i]) {
cout << "No" << endl;
return ;
}
cout << "Yes" << endl;
}
signed main() {
#if SYNC==0
ios::sync_with_stdio(false);
cin.tie(0);
#endif
int T = 1;
while (T--) solve();
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define F first
#define S second
#define all(c) ((c).begin()), ((c).end())
#define sz(x) ((int)(x).size())
#define ld long double
template<class T,class U>
ostream& operator<<(ostream& os,const pair<T,U>& p){
os<<"("<<p.first<<", "<<p.second<<")";
return os;
}
template<class T>
ostream& operator <<(ostream& os,const vector<T>& v){
os<<"{";
for(int i = 0;i < (int)v.size(); i++){
if(i)os<<", ";
os<<v[i];
}
os<<"}";
return os;
}
#ifdef LOCAL
#define cerr cout
#else
#endif
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif
const int N = 100005;
const int M = N / 4;
const ll INF = 1LL << 60;
int A[N], B[N], perm[N];
ll cost[2][N];
int main(){
int n; cin >> n;
ll ans = 0;
for(int i = 1; i <= n; i++) cin >> A[i], ans += A[i];
for(int i = 1; i <= n; i++) cin >> B[i], perm[i] = i;
sort(perm + 1, perm + n + 1, [&](int i, int j){return B[i] - A[i] > B[j] - A[j];});
vector<int> num(2, 0);
for(int i = 1; i <= n; i++){
int ind = perm[i];
int j = ind % 2;
num[j]++;
cost[j][num[j]] = cost[j][num[j] - 1] + B[ind] - A[ind];
}
ll val = ans;
for(int i = 1; i <= n / 2; i++){
ans = max(ans, val + cost[0][i] + cost[1][i]);
}
cout << ans << endl;
} | #include <bits/stdc++.h>
template<class T> inline bool chmin(T&a, T b){if(a > b){a = b; return true;}else{return false;}}
template<class T> inline bool chmax(T&a, T b){if(a < b){a = b; return true;}else{return false;}}
#define ll long long
#define double long double
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for(int i=1;i<=(n);i++)
#define mod (ll)(1e9+7)
#define inf (ll)(3e18+7)
#define eps (double)(1e-9)
#define pi (double) acos(-1)
#define P pair<int,int>
#define PiP pair<int,pair<int,int>>
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n), b(n);
rep(i, n)cin >> a[i];
rep(i, n)cin >> b[i];
vector<int> even, odd;
rep(i, n){
if(i % 2 == 0)even.push_back(b[i]-a[i]);
else odd.push_back(b[i]-a[i]);
}
sort(rall(even)); sort(rall(odd));
ll ans = accumulate(all(a), 0LL);
rep(i, n/2)ans += max(0, even[i]+odd[i]);
cout << ans << endl;
} |
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(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 n;
cin >> n;
int m = 5;
vector<vector<int>> men((n), vector<int>(m));
rep(i, n) rep(j, m) cin >> men[i][j];
int l = 0, r = 1001001001; // 100 100 100 1をすると10^9というのがわかりやすい
while (l + 1 < r) {
int c = (l + r) / 2;
vector<int> s;
rep(i, n) {
int x = 0;
rep(j, m) {
if (men[i][j] >= c) x |= 1 << j; // jbit目にbitを立てに行く
}
s.push_back(x);
}
sort(s.begin(), s.end());
s.erase(unique(s.begin(), s.end()), s.end());
bool ok = false;
rep(i, s.size()) rep(j, i + 1) rep(k, j + 1) { // k<=j<=i
if ((s[i] | s[j] | s[k]) == (1 << m) - 1)
ok = true; //(1<<m)-1でmbitの1...1が作れる
}
if (ok)
l = c;
else
r = c;
}
cout << l << endl;
return 0;
} | #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <math.h>
#include <map>
#include <queue>
#include <cassert>
#include <iomanip>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define rALL(a) (a).rbegin(), (a).rend()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const int iINF = 1001001001;
const long long llINF = 1LL << 60;
int main()
{
//int a, b; cin >> a >> b;
//cout << (a + b) / 2 << " " << (a - b) / 2 << endl;
/*//cin >> n >> s;
int n = 5000; string s(n, 'T');
ll ans = 0;
for (int i = 2; i <= n; i += 2) {
for (int j = 0; j < n - i + 1; ++j) {
int a, t, g, c;
a = t = g = c = 0;
rep(k, i) {
a += s[j + k];
//if (s[j + k] == 'A') a++;
//if (s[j + k] == 'T') t++;
//if (s[j + k] == 'G') g++;
//if (s[j + k] == 'C') c++;
}
if (a == t && g == c) ans++;
}
}
cout << ans << endl;
*/
int n; string s;
cin >> n >> s;
ll ans = 0;
vector<ll> sum(n + 1);
sum[0] = 0;
rep(i, n) {
int num = 0;
if (s[i] == 'A') num = 1;
if (s[i] == 'T') num = -1;
if (s[i] == 'G') num = 10000;
if (s[i] == 'C') num = -10000;
sum[i + 1] = sum[i] + num;
}
rep(i, n + 1) {
for (int j = i + 1; j < n + 1; ++j) {
if (sum[j] - sum[i] == 0) {
//cout << i << "(" << sum[i] << "), " << j << "(" << sum[j] << ")" << endl;
ans++;
}
}
}
cout << ans << endl;
//vector<int> pos;
//for (int i = 0; i < n - 1; ++i) {
// bool ok = false;
// if (s[i] == 'A' && s[i + 1] == 'T') ok = true;
// if (s[i] == 'T' && s[i + 1] == 'A') ok = true;
// if (s[i] == 'C' && s[i + 1] == 'G') ok = true;
// if (s[i] == 'G' && s[i + 1] == 'C') ok = true;
// if (ok) pos.push_back(i);
//}
//ans = pos.size();
//int sz = pos.size();
//rep(i, sz) {
// for (int j = i + 1; j < sz; ++j) {
// if (2 < pos[j] - pos[j - 1]) break;
// ans++;
// }
//}
//cout << ans << endl;
return 0;
} |
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
constexpr int N = 100005;
int n;
std::vector<long long> a[3];
std::string s;
int main() {
std::ios::sync_with_stdio(false), std::cin.tie(nullptr);
std::cin >> n;
for (int i = 1; i <= n << 1; ++i) {
long long x;
std::cin >> x >> s;
if (s[0] == 'R') a[0].push_back(x);
else if (s[0] == 'G') a[1].push_back(x);
else a[2].push_back(x);
}
int x = -1, y = -1;
for (int i = 0; i < 3; ++i) {
if (a[i].size() & 1) {
if (x != -1) y = i;
else x = i;
}
std::sort(a[i].begin(), a[i].end());
}
if (x == -1) return std::cout << "0\n", 0;
auto f = [](int x, int y) -> long long {
long long ans = 1e18;
for (long long i : a[x]) {
long long t = std::lower_bound(a[y].begin(), a[y].end(), i) - a[y].begin();
if (t != a[y].size()) ans = std::min(ans, std::abs(a[y][t] - i));
if (t) ans = std::min(ans, std::abs(i - a[y][t - 1]));
}
return ans;
};
int z = 3 - x - y;
long long ans = f(x, y);
long long t1 = f(x, z), t2 = f(y, z);
if (t1 < 1e18) ans = std::min(ans, t1 + t2);
std::cout << ans << '\n';
return 0;
}
| #pragma GCC optimize ("O2")
#pragma GCC target ("avx")
//#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please
ll A[200000], tmp[200001];
void pakuri_sort(int N, ll A[]) {
const int b = 9;
rep(k, 6) {
int kazu[1 << b] = {}, kazu2[1 << b] = {};
rep(i, N) kazu[A[i] >> k * b & ((1 << b) - 1)]++;
rep(i, (1 << b) - 1) kazu[i + 1] += kazu[i];
for (int i = N - 1; i >= 0; i--) tmp[--kazu[A[i] >> k * b & ((1 << b) - 1)]] = A[i];
k++;
rep(i, N) kazu2[tmp[i] >> k * b & ((1 << b) - 1)]++;
rep(i, (1 << b) - 1) kazu2[i + 1] += kazu2[i];
for (int i = N - 1; i >= 0; i--) A[--kazu2[tmp[i] >> k * b & ((1 << b) - 1)]] = tmp[i];
}
}
const int CM = 1 << 17, CL = 20;
char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
const ll pre[9] = { 1,10,100,1000,10000,100000,1000000,10000000,100000000 };
inline ll getint() {
if (ci - owa > 0) {
memcpy(cn, owa, CL);
ci -= CM;
fread(cn + CL, 1, CM, stdin);
}
ll tmp = *(ll*)ci;
if ((tmp & ma0) ^ ma0) {
int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0);
tmp = tmp << dig & ma1;
tmp = tmp * 10 + (tmp >> 8) & ma2;
tmp = tmp * 100 + (tmp >> 16) & ma3;
tmp = tmp * 10000 + (tmp >> 32) & ma4;
ci += (72 - dig >> 3);
}
else {
tmp = tmp & ma1;
tmp = tmp * 10 + (tmp >> 8) & ma2;
tmp = tmp * 100 + (tmp >> 16) & ma3;
tmp = tmp * 10000 + (tmp >> 32) & ma4;
ci += 8;
ll tmp2 = *(ll*)ci;
if ((tmp2 & ma0) == ma0) {
ci += 9;
return 1000000000000000;
}
int dig = 68 - __builtin_ctzll((tmp2 & ma0) ^ ma0);
if (dig == 64) {
ci++;
return tmp;
}
tmp2 = tmp2 << dig & ma1;
tmp2 = tmp2 * 10 + (tmp2 >> 8) & ma2;
tmp2 = tmp2 * 100 + (tmp2 >> 16) & ma3;
tmp2 = tmp2 * 10000 + (tmp2 >> 32) & ma4;
ci += (72 - dig >> 3);
tmp = tmp * pre[64 - dig >> 3] + tmp2;
}
return tmp;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N = getint();
int K[3] = {};
rep(i, N << 1) {
ll a = getint();
char c = *ci++;
ci++;
ll C = (ll(c) >> 4 | ll(c) << 1) & 3;
K[C]++;
A[i] = a | C << 50;
}
pakuri_sort(N * 2, A);
if (K[0] % 2 == 0 && K[1] % 2 == 0 && K[2] % 2 == 0) {
co(0);
return 0;
}
auto P = A;
auto Q = A + K[0];
auto R = A + K[0] + K[1];
const ll ma = (1ll << 50) - 1;
for(int i = N * 2 - 1; i >= K[0]; i--) A[i] &= ma;
ll sai[3] = {};
ll saiq = 1e18, sair = 1e18;
int q = 0, r = 0;
rep(p, K[0]) {
if (q < K[1]) if (saiq > abs(Q[q] - P[p])) saiq = abs(Q[q] - P[p]);
while (q < K[1] - 1 && Q[q] < P[p]) {
q++;
if (saiq > abs(Q[q] - P[p])) saiq = abs(Q[q] - P[p]);
}
if (r < K[2]) if (sair > abs(R[r] - P[p])) sair = abs(R[r] - P[p]);
while (r < K[2] - 1 && R[r] < P[p]) {
r++;
if (sair > abs(R[r] - P[p])) sair = abs(R[r] - P[p]);
}
}
sai[2] = saiq;
sai[1] = sair;
sair = 1e18;
r = 0;
q = 0;
while (q < K[1]) {
if (r < K[2]) if (sair > abs(R[r] - Q[q])) sair = abs(R[r] - Q[q]);
while (r < K[2] - 1 && R[r] < Q[q]) {
r++;
if (sair > abs(R[r] - Q[q])) sair = abs(R[r] - Q[q]);
}
q++;
}
sai[0] = sair;
ll kotae = 1e18;
if (K[0] % 2 == 0) kotae = min(sai[0], sai[1] + sai[2]);
else if (K[1] % 2 == 0) kotae = min(sai[1], sai[2] + sai[0]);
else kotae = min(sai[2], sai[0] + sai[1]);
co(kotae);
Would you please return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template<class T> using vc = vector<T>;
template<class T> using vvc = vector<vector<T>>;
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i,n) for (auto& i: n)
template<class T1, class T2> inline bool chmax(T1 &a, const T2 &b) {if (a<b) { a=b; return 1;} return 0;}
template<class T1, class T2> inline bool chmin(T1 &a, const T2 &b) {if (b<a) { a=b; return 1;} return 0;}
struct init{init(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}}init_;
#ifdef DEBUG
template <class T, class N> void verr(const T& a, const N& n) { rep(i, n) cerr << a[i] << " "; cerr << "\n" << flush; }
ll dbgt = 1; void err() { cerr << "passed " << dbgt++ << "\n" << flush; }
template<class H, class... T> void err(H&& h,T&&... t){ cerr<< h << (sizeof...(t)?" ":"\n") << flush; if(sizeof...(t)>0) err(forward<T>(t)...); }
#endif
const ll INF = 4e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//
struct UnionFind {
private:
vector<int> par;
vector<int> count;
vector<int> rank;
public:
UnionFind(int N) {
count.assign(N, 1);
rank.assign(N, 0);
par.assign(N, 0);
rep(i, N) par[i] = i;
}
int root(int x) {
if (par[x] == x)
return x;
else {
par[x] = root(par[x]);
return par[x];
}
}
void unite(int x, int y) {
x = root(x), y = root(y);
if (x == y) return;
if (rank[x] < rank[y])
swap(x, y);
else if (rank[x] == rank[y])
rank[y]++;
par[y] = x;
count[x] += count[y];
return;
}
int size(int x) {
return count[root(x)];
}
bool issame(int x, int y) {
return root(x) == root(y);
}
};
using UF = struct UnionFind;
int main() {
ll N;
cin >> N;
vc<string> S(N);
rep(i, N) cin >> S[i];
vvc<int> child(N), parent(N);
rep(i, N){
vl srch(N);
srch[i] = true, child[i].eb(i);
queue<ll> q;
q.emplace(i);
while(!q.empty()){
ll now = q.front();
q.pop();
rep(j, N){
if (srch[j] or S[now][j] == '0') continue;
srch[j] = true;
child[i].eb(j);
q.emplace(j);
}
}
srch = vl(N), q.emplace(i), parent[i].eb(i), srch[i] = true;
while(!q.empty()){
ll now = q.front();
q.pop();
rep(j, N){
if (srch[j] or S[j][now] == '0') continue;
srch[j] = true;
parent[i].eb(j);
q.emplace(j);
}
}
}
ld ans = 0;
rep(i, N) ans += 1.0L / parent[i].size();
cout << ans << endl;
} | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll I=1167167167167167167;
ll Q=998244353;
#define rep(i,a) for (ll i=0;i<a;i++)
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
template<class T> void print_tate(vector<T> &v) {rep(i,v.size()) cout<<v[i]<<"\n";}
void yneos(bool a){if(a) cout<<"Yes"<<"\n"; else cout<<"No"<<"\n";}
//おちこんだりもしたけれど、私はげんきです。
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin>>N;
vector<ll> kai(N+1,1);
int weigt[N];
int sum=0;
rep(i,N) cin>>weigt[i],sum+=weigt[i];
for(int i = 2;i <= N;i++)kai[i]=(kai[i-1]*(i))%Q;
if(sum%2==1){
cout<<"0\n";
return 0;
}
ll dp[N+1][sum+1];
memset(dp,0,sizeof(dp));
dp[0][0]=1;
for(int i =0;i < N;i++){
for(int j=sum/2;j>=weigt[i];j--){
for(int k=N-1;k>=0;k--) dp[k+1][j]=(dp[k+1][j]+dp[k][j-weigt[i]])%Q;
}
}
ll ans=0;
for(int i = 0;i < N;i++){
ans=(ans+(dp[i][sum/2]*((kai[i]*kai[N-i])%Q))%Q)%Q;
}
cout<<(ans)%Q<<endl;
}
|
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
#define chmin(a,b) a=min(a,b)
#define chmax(a,b) a=max(a,b)
#define mod 1000000007
#define ad(a,b) a=(a+b)%mod;
#define X 5010
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 fac[X],ivf[X];
void init(){
fac[0]=1;
for(ll i=1;i<X;i++)fac[i]=fac[i-1]*i%mod;
for(ll i=0;i<X;i++)ivf[i]=po(fac[i],mod-2);
}
ll C(ll n,ll k){
return fac[n]*ivf[n-k]%mod*ivf[k]%mod;
}
int solve(){
string s;
cin>>s;
string ss=s;
sort(ss.begin(),ss.end());
reverse(ss.begin(),ss.end());
string atc="atcoder";
if(atc>=ss){
return -1;
}
int ans=1e9;
for(int i=0;i<=atc.size();i++){
int cnt=0;
ss=s;
for(int j=0;j<i;j++){
for(int k=j;k<ss.size();k++){
if(ss[k]==atc[j]){
cnt+=k-j;
swap(ss[k],ss[j]);
break;
}
}
}
if(i<atc.size()){
for(int k=i;k<ss.size();k++){
if(ss[k]>atc[i]){
cnt+=k-i;
swap(ss[k],ss[i]);
break;
}
}
}
if(atc<ss){
chmin(ans,cnt);
//cerr<<i<<":"<<cnt<<endl;
}
}
return ans;
}
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
ll t;
cin>>t;
while(t--)cout<<solve()<<"\n";
}
| #include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long int ll;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for(auto it = d.b;it != d.e;++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;cin >> t;
while(t--){
string s;cin >> s;
string w = "atcoder";
if(w < s)cout << 0;
else{
int ans = INT_MAX;
for(int i = 0;i < (int)s.length() - 1;i++){
for(int j = i + 1;j < (int)s.length();j++){
swap(s[i], s[j]);
if(w < s)ans = min(ans, abs(i - j));
swap(s[i], s[j]);
}
}
if(ans == INT_MAX)cout << -1;
else cout << ans;
}
cout << endl;
}
}
|
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
int n,m;scanf("%d%d",&n,&m);
if(m==0){
for(int i=1;i<=n;i++) printf("%d %d\n",2*i-1,2*i);
return 0;
}
if(m<0){
printf("-1\n");
return 0;
}
if(m>=n-1){
printf("-1\n");
return 0;
}
m++;
printf("1 %d\n",2*(m+1));
for(int i=1;i<=m;i++) printf("%d %d\n",2*i,2*i+1);
for(int i=m+2;i<=n;i++) printf("%d %d\n",2*i-1,2*i);
return 0;
} | /** Created by: Humberto Yusta
Codeforces User: humbertoyusta
Country: Cuba
Copyright�� */
#include<bits/stdc++.h>
using namespace std;
/// Pragmas:
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline") //Optimization flags
//#pragma GCC option("arch=native","tune=native","no-zero-upper") //Enable AVX
//#pragma GCC target("avx2") //Enable AVX
/// Macros:
#define int long long
#define f first
#define s second
#define db(x) cerr << #x << ": " << (x) << '\n';
#define pb push_back
#define lb lower_bound
#define up upper_bound
#define all(x) x.begin() , x.end()
#define rall(x) x.rbegin() , x.rend()
#define enl '\n'
typedef pair<int,int> ii;
typedef long double ld;
typedef unsigned long long ull;
/// Constraints:
const int maxn = 1000010;
const int mod = 1000000007;
const ld eps = 1e-9;
const int inf = ((1ll<<31ll)-1ll);
const int INF = 1ll * mod * mod;
const ld pi = acos(-1);
/// Prime Numbers:
// 2, 173, 179, 181, 197, 311, 331, 737, 1009, 2011, 2027, 3079, 4001, 10037, 10079, 20011, 20089;
// 100003, 100019, 100043, 200003, 200017, 1000003, 1000033, 1000037, 1000081;
// 2000003, 2000029, 2000039, 1000000007, 1000000021, 2000000099;
/// rng
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
/// Functions:
#define lg2(x) 31 - __builtin_clz(x)
#define lgx(x,b) ( log(x) / log(b) )
/// Red-Black Tree Template ---------------------------------
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//typedef tree < long long , null_type , less<long long> , rb_tree_tag , tree_order_statistics_node_update > ordered_set;
/// Quick Pow------------------------------------------------
int qpow(int b,int e){
if( !e ) return 1;
if( e & 1 ) return qpow(b,e-1) * b % mod;
int pwur = qpow(b,e>>1);
return pwur * pwur % mod;
}
int modinv(int x){
return qpow(x,mod-2);
}
/// My Code -------------------------------------------------
int n, m;
vector<ii> ans;
int32_t main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cout.setf(ios::fixed); cout.precision(0);
srand(time(NULL));
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
cin >> n >> m;
if( n == 1 && m == 0 ){
cout << 1 << ' ' << 2 << '\n';
return 0;
}
if( m >= 0 ){
if( m + 1 > n - 1 ){ cout << -1 << '\n'; return 0; }
ans.pb({1,2*(m+1)+1});
int curr = 2;
for(int i=1; i<=m; i++){
ans.pb({curr,curr+1});
curr += 2;
}
ans.pb({curr,curr+2});
curr += 3;
for(int i=m+1+1+1; i<=n; i++){
ans.pb({curr,curr+1});
curr += 2;
}
}
else{
cout << -1 << '\n';
return 0;
}
for( auto i : ans )
cout << i.f << ' ' << i.s << '\n';
return 0;
}
|
#include <bits/stdc++.h>
template<class T> inline bool chmin(T&a, T b){if(a > b){a = b; return true;}else{return false;}}
template<class T> inline bool chmax(T&a, T b){if(a < b){a = b; return true;}else{return false;}}
#define ll long long
#define double long double
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for(int i=1;i<=(n);i++)
#define mod (ll)(1e9+7)
#define inf (ll)(3e18+7)
#define eps (double)(1e-9)
#define pi (double) acos(-1)
#define P pair<int,int>
#define PiP pair<int,pair<int,int>>
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
using namespace std;
int main() {
int n;
cin >> n;
rep(i, n){
int x, y, r;
cin >> x >> y >> r;
cout << x << " " << y << " " << x+1 << " " << y+1 << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repp(i, st, en) for (ll i = (ll)st; i < (ll)(en); i++)
#define repm(i, st, en) for (ll i = (ll)st; i >= (ll)(en); i--)
#define all(v) v.begin(), v.end()
void chmax(ll &x, ll y) {x = max(x,y);}
void chmin(ll &x, ll y) {x = min(x,y);}
void Yes() {cout << "Yes" << endl; exit(0);}
void No() {cout << "No" << endl; exit(0);}
template<class in_Cout> void Cout(in_Cout x) {cout << x << endl; exit(0);}
template<class in_vec_cout>
void vec_cout(vector<in_vec_cout> vec) {
for (in_vec_cout res : vec) {cout << res << " ";}
cout << endl;
}
const ll inf = 1e18;
const ll mod = 1e9 + 7;
int main() {
ll N; cin >> N;
vector<ll> a(N), t(N);
rep(i,N) cin >> a[i] >> t[i];
ll l = -inf, r = inf, add = 0;
ll L = -inf, R = inf;
bool is_straight = false;
rep(i,N) {
if (t[i]==1) {
if (is_straight) {
L += a[i];
R += a[i];
continue;
}
add += a[i];
if (l!=-inf) L += a[i];
if (r!=inf) R += a[i];
} else if (t[i]==2) {
if (is_straight) {
chmax(L,a[i]);
chmax(R,a[i]);
continue;
}
if (a[i]<=L) continue;
if (R<=a[i]) {
L = a[i];
R = a[i];
is_straight = true;
continue;
}
L = a[i];
l = L - add;
} else {
if (is_straight) {
chmin(L,a[i]);
chmin(R,a[i]);
continue;
}
if (R<=a[i]) continue;
if (a[i]<=L) {
L = a[i];
R = a[i];
is_straight = true;
continue;
}
R = a[i];
r = R - add;
}
}
ll Q; cin >> Q;
vector<ll> res(Q);
rep(q,Q) {
ll x; cin >> x;
if (is_straight) {
res[q] = L;
} else {
if (x<=l) res[q] = L;
else if (x<r) res[q] = x + add;
else res[q] = R;
}
}
for (ll ans : res) cout << ans << endl;
} |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(ll i=0;i<(n);i++)
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define FORC(i,a,b,c) for(ll i=(a);i<(b);i+=(c))
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define ff first
#define ss second
#define dd long double
#define all(x) x.begin(),x.end()
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n,m;
cin>>n>>m;
if(m==0){
cout<<1<<endl;
return 0;
}
vector<ll> arr(m);
REP(i,m){
cin>>arr[i];
arr[i]--;
}
if(n==m){
cout<<0<<endl;
return 0;
}
sort(all(arr));
vector<ll> gaps;
if(arr[0]){
gaps.pb(arr[0]);
}
for(ll i=1;i<m;i++){
ll x=arr[i]-arr[i-1]-1;
if(x){
gaps.pb(x);
}
}
if(n-arr.back()-1!=0){
gaps.pb(n-arr.back()-1);
}
sort(all(gaps));
ll x=gaps[0];
ll ans=0;
for(ll i=0;i<int(gaps.size());i++){
ll temp=gaps[i];
temp=temp%x?temp/x+1:temp/x;
ans+=temp;
}
cout<<ans<<endl;
}
| #include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<queue>
#include<cmath>
#include<stack>
#include <map>
#include <vector>
using namespace std;
int main(){
int n,m,p=0,mp[10][10],ad=0,k=0;
cin>>n>>m;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>mp[i][j];
}
}
int a[10]={0,1,2,3,4,5,6,7,8,9};
while(p==0){
ad=0;
for(int i=1;i<n;i++){
ad+=mp[a[i]][a[i-1]];
}
ad+=mp[a[n-1]][0];
//cout<<ad<<endl;
if(ad==m)k++;
next_permutation(a,a+n);
p=1;
if(a[0]==0)p=0;
}
cout<<k;
return 0;
} |
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <cmath>
#include <queue>
#include <cstring>
#include <map>
#include <string>
#define inf 0x3f3f3f3f
#define _for(i,a,b) for( int i=(a); i<(b); ++i)
#define __for(i,a,b) for( int i=(b); i>=(a); --i)
using namespace std;
typedef pair<int,int>pii;
typedef unsigned long long ll;
const int mod =1e9+7;
const int N=1e5+7;
int main( )
{
int a,b,c;
cin>>a>>b>>c;
cout<<7-a+7-b+7-c;
return 0;
}
|
//================code===================//
#define TLE
#ifdef TLE
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#endif
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <ctime>
#define ci(t) cin>>t
#define co(t) cout<<t
#define LL long long
#define ld long double
#define fa(i,a,b) for(LL i=(a);i<(LL)(b);++i)
#define fd(i,a,b) for(LL i=(a);i>(LL)(b);--i)
#define setp pair<pair<int,int>,int>
#define setl pair<LL,LL>
#define M_PI 3.14159265358979323846
#define micro 0.000001
using namespace std;
#ifdef OHSOLUTION
#define ce(t) cerr<<t
#define AT cerr << "\n=================ANS=================\n"
#define AE cerr << "\n=====================================\n"
LL gcd(LL a, LL b) { return a % b ? gcd(b, a % b) : b; }
LL lcm(LL a, LL b) { return (a * b) / gcd(a, b); }
#else
#define AT
#define AE
#define ce(t)
#define __popcnt __builtin_popcount
#define gcd __gcd
#define lcm __lcm
#endif
pair <int, int> vu[9] = { {0,1},{1,0},{0,-1} ,{-1,0},{0,1},{1,0}, {-1,1} , {1,-1},{-1,-1} }; //RDLU EWSN
template<typename T, typename U> void ckmax(T& a, U b) { a = a < b ? b : a; }
template<typename T, typename U> void ckmin(T& a, U b) { a = a > b ? b : a; }
struct gcmp { bool operator()(LL a, LL b) { return a < b; } bool operator()(setl a, setl b) { return a.second < b.second; } };
struct lcmp { bool operator()(LL a, LL b) { return a > b; } bool operator()(setl a, setl b) { return a.second > b.second; } };
const int max_v = 2e5 + 7;
const int INF = 1e9 + 7;
const LL LNF = (LL)5e18 + 7ll;
const LL mod = 1e9+7;
int main()
{
#ifdef OHSOLUTION
freopen("input.txt", "r", stdin);
#endif
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
LL n; ci(n);
LL x;
fa(i, 1, n+1)
{
if (i * i * i > n) break;
x = i;
}
LL ans = x * x * x;
fa(i, x + 1, n+1)
{
bool ck = false;
fa(j, 1, i+1)
{
if (i * j > n) break;
fa(t, 1,j+1)
{
if (i * j * t <= n)
{
//ce(i << " " << j << " " << t << endl);
int c = (i == j) + (i == t) + (j == t);
ck = true;
if (c == 3) ++ans;
else if (c == 1) ans += 3ll;
else ans += 6ll;
}
else break;
}
}
if (!ck) break;
}
co(ans);
return 0;
} |
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <map>
#include <cmath>
#include <iomanip>
using namespace std;
typedef long long ll;
int main(){
cout << fixed << setprecision(15);
int N; cin >> N;
vector<ll> X; ll x; for (int i=0; i<N; i++) { cin >> x; X.push_back(x); }
ll m_dist = 0;
for (int i=0; i<N; i++) m_dist += abs(X[i]);
cout << m_dist << endl;
double u_dist = 0;
for (int i=0; i<N; i++) u_dist += double(X[i]) * double(X[i]);
u_dist = sqrt(u_dist);
cout << u_dist << endl;
ll c_dist = 0;
for (int i=0; i<N; i++) c_dist = max(c_dist, abs(X[i]));
cout << c_dist << endl;
return 0;
} | #include <bits/stdc++.h>
/* #include <atcoder/all> */
#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)
using namespace std;
/* using namespace atcoder; */
using ll = long long;
using P = pair<int, int>;
template<class T>
inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int main() {
ll x, y, a, b;
cin >> x >> y >> a >> b;
ll tmp = x;
ll mul = 0;
while (y / a > tmp && (tmp * a < tmp + b)) {
tmp *= a;
mul++;
}
ll res = mul + (y - tmp - 1) / b;
cout << res << endl;
return 0;
}
|
#include <bits/stdc++.h> //yaad rkhne layak baatein
#include <bits/stdc++.h> // bool found = false use it to find some number after the given number with some cond.
// while (!found)
#include <iostream> // freq.table-> int freq[26] = {0}; for (ll i = 0; i < s1.length(); i++) freq[s1[i] - 'A']++;
#define ll long long //[s[i] - 'a'] ->this convers char to corr.int eg. 'c' to 3.
// if(a<=min1){
// min2 = min1;
// index2 = index1;
// min1 = a;
// index1 = i; to find index of 2nd smallest element
// }
// else if(a<=min2){
// min2 = a;
// index2 = i;
// }
#define INF 2000000000
#define pb push_back
using namespace std;
//cout<<fixed<<setprecision(12)<<ans<<endl;
const int M = 1e9 + 7;
#define loop0(i, n) for (ll i = 0; i < n; i++) // str.insert(0, 5, '1'); => it will insert 1 five times in start of str.
#define loop00(i, n) for (ll i = 0; i <= n; i++)
#define loop1(i, n) for (ll i = 1; i < n; i++)
#define loop11(i, n) for (ll i = 1; i <= n; i++)
#define loopab(i, a, b) for (ll i = a; i <= b; i++)
void flashSpeed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
}
long long mod(long long x)
{
return ((x % M + M) % M);
}
long long add(long long a, long long b)
{
return mod(mod(a) + mod(b));
}
long long mul(long long a, long long b)
{
return mod(mod(a) * mod(b));
}
ll modPow(ll a, ll b)
{
if (b == 0)
return 1LL;
if (b == 1)
return a % M;
ll res = 1;
while (b)
{
if (b % 2 == 1)
res = mul(res, a);
a = mul(a, a);
b = b / 2;
}
return res;
}
const int N = 2e5 + 2;
int fact[N];
void precalc()
{
fact[0] = 1;
for (int i = 1; i < N; i++)
{
fact[i] = mul(fact[i - 1], i);
}
}
ll inv(ll x)
{
return modPow(x, M - 2);
}
ll divide(ll a, ll b)
{
return mul(a, inv(b));
}
ll nCr(ll n, ll r)
{
return divide(fact[n], mul(fact[r], fact[n - r]));
}
double Round(double var)
{
float value = (int)(var * 100 + .5);
return (float)value / 100;
}
ll ceils(ll x, ll y) {
return x / y + ((x % y) != 0);
}
ll Gcd(ll a, ll b)
{
if (b > a)
{
return Gcd(b, a);
}
if (b == 0)
return a;
else
return Gcd(b, a % b);
}
ll lcm(ll a, ll b) {
return a / Gcd(a, b) * b;
}
bool isPal(string s)
{
for (int i = 0; i < (int)s.size() / 2; i++)
{
if (s[i] != s[(int)s.size() - 1 - i])
return false;
}
return true;
}
ll Sumdigits(ll a)
{
ll total = 0;
while (a)
{
total += a % 10;
a /= 10;
}
return total;
}
bool isPerfectSquare(int n)
{
for (int i = 1; i * i <= n; i++) {
if ((n % i == 0) && (n / i == i)) {
return true;
}
}
return false;
}
bool isPowerOfTwo(int n){
return (ceil(log2(n)) == floor(log2(n)));
}
void lexosmallest(string s,string c){
string t1 = s;
sort(t1.begin(), t1.end());
int index = -1;
for (int i = 0; i < s.length(); i++) {
if (s[i] != t1[i]) {
index = i;
break;
}
}
int j;
for (int i = 0; i < s.length(); i++) { //khela yha pe hua hai..loop poora chala hai
if (s[i] == t1[index])
j = i;
}
swap(s[index], s[j]);
}
void solution(){
int a,b,c;
cin>>a>>b>>c;
cout<<21-(a+b+c)<<endl;
}
int main()
{
flashSpeed();
int t=1;
// cin>>t;
while(t--)
{
solution();
}
return 0;
}
| /*{{{*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define PB emplace_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PII> VPII;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(int64_t &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W(x.F); putchar(' '); _W(x.S);}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
#ifdef HOME
#define DEBUG(...) {printf("[DEBUG] ");W(__VA_ARGS__);}
#else
#define DEBUG(...)
#endif
int MOD = 1e9+7;
void ADD(LL& x,LL v){x=(x+v)%MOD;if(x<0)x+=MOD;}
/*}}}*/
const int SIZE = 1<<13;
LL mypow(LL x,LL y){
x%=MOD;
LL res=1%MOD;
while(y){
if(y&1)res=res*x%MOD;
y>>=1;
x=x*x%MOD;
}
return res;
}
int h,w,K;
char d[SIZE][SIZE];
LL dp[SIZE][SIZE];
int inv3;
void solve() {
inv3=mypow(3,MOD-2);
dp[1][1]=1;
FOR(i, 1, h){
FOR(j, 1, w) {
if(!d[i][j]){
ADD(dp[i+1][j],inv3*dp[i][j]*2);
ADD(dp[i][j+1],inv3*dp[i][j]*2);
}else{
if(d[i][j]&1){
ADD(dp[i][j+1],dp[i][j]);
}
if(d[i][j]&2){
ADD(dp[i+1][j],dp[i][j]);
}
}
}
}
W(dp[h][w]*mypow(3,h*w-K)%MOD);
}
void input() {
R(h,w,K);
REP(i,K){
int x,y;
char c[4];
R(x,y);
RS(c);
if(c[0]=='X'||c[0]=='R')d[x][y]|=1;
if(c[0]=='X'||c[0]=='D')d[x][y]|=2;
}
}
int main(){
MOD=998244353;
input();
solve();
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
const int N = 2005;
const int oo = 0x3f3f3f3f;
int Gcd(int a, int b) {
return (b ? Gcd(b, a % b) : a);
}
int a[N], ans = 0, mn, f = 1;
map<int,int> mp;
void get(int a, int b) {
if (!mp.count(a)) {
mp[a] = b;
return;
}
if (mp[a] == a) return;
int x = mp[a];
x = Gcd(x, b);
if (x == a) {
ans++;
if (a == mn) f = 0;
// cout << a << '\n';
}
mp[a] = x;
}
void solve(){
int n;
cin >> n;
mn = oo;
for (int i = 1; i <= n; i++) {
cin >> a[i];
mn = min(mn, a[i]);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j * j <= a[i]; j++) {
if (a[i] % j == 0) {
if (j <= mn)
get(j, a[i]);
if (j * j != a[i] && a[i] / j <= mn)
get(a[i] / j, a[i]);
}
}
}
cout << ans + f;
}
signed main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#ifdef local
freopen("inp.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
solve();
} | #include <bits/stdc++.h>
using namespace std;
#define closeSync ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
typedef long long ll;
typedef long double ld;
const int MAXN = 3005;
typedef pair<ld,ld> pll;
ll a[MAXN][7];
int flag[MAXN];
int n;
inline bool check(ll x)
{
set<int> s;
for (int i=1;i<=n;i++)
{
ll tmp = 0;
for (int j=1;j<=5;j++)
{
if (a[i][j] >= x)
tmp |= (1LL<<(j-1));
}
s.insert(tmp);
}
for (auto x : s)
for (auto y : s)
for (auto z : s)
{
// cout << x << " " << y << " " << z << "\n";
if ((x|y|z) == 31)
return true;
}
return false;
}
inline void solve()
{
cin >> n;
for (int i=1;i<=n;i++)
{
for (int j=1;j<=5;j++)
{
cin >> a[i][j];
}
}
ll l = 0,r = 1e10;
// cout << check(4) << "\n";
// cout << check(r) << endl;
while (r - l > 1)
{
ll mid = (l + r) >> 1;
if (check(mid))
l = mid;
else
r = mid;
}
if (check(r))
cout << r << "\n";
else
cout << l << "\n";
return ;
}
int main()
{closeSync;
// int T; cin >> T;
// while (T--)
solve();
return 0;
}
/*
aRbRcRd
*/ |
#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <string.h>
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define NDEBUG
#define eprintf(...) \
do \
{ \
} while (0)
#endif
#include <cassert>
using namespace std;
typedef long long LL;
typedef vector<int> VI;
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define EACH(i, c) for (__typeof((c).begin()) i = (c).begin(), i##_end = (c).end(); i != i##_end; ++i)
template <class T>
inline void amin(T &x, const T &y)
{
if (y < x)
x = y;
}
template <class T>
inline void amax(T &x, const T &y)
{
if (x < y)
x = y;
}
#define rprintf(fmt, begin, end) \
do \
{ \
const auto end_rp = (end); \
auto it_rp = (begin); \
for (bool sp_rp = 0; it_rp != end_rp; ++it_rp) \
{ \
if (sp_rp) \
putchar(' '); \
else \
sp_rp = true; \
printf(fmt, *it_rp); \
} \
putchar('\n'); \
} while (0)
int H, W;
char S[111][111];
void MAIN()
{
scanf("%d%d", &H, &W);
REP(i, H)
scanf("%s", S[i]);
int ans = 0;
REP(i, H - 1)
REP(j, W - 1)
{
int cnt = 0;
if (S[i][j] == S[i][j + 1])
cnt++;
if (S[i][j] == S[i + 1][j])
cnt++;
if (S[i][j] == S[i + 1][j + 1])
cnt++;
if (cnt == 0 || cnt == 2)
ans++;
}
printf("%d\n", ans);
}
int main()
{
int TC = 1;
// scanf("%d", &TC);
REP(tc, TC)
MAIN();
return 0;
} | #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>;
int main() {
long long ans = 0;
int L;
int R;
cin >> L >> R;
VI primes;
int UPPER = R+10;
VI np(UPPER);
VI factCnt(UPPER, 0);
VI onlyOne(UPPER, 1);
VI ooo(UPPER, 1);
// 2*2*3
// 2, 3, 2*2, 2*3, 2*2*3
for (long long v = 2; v < np.size(); ++v) {
if (np[v] == 0) {
primes.push_back(v);
++factCnt[v];
for (long long j = v*2; j < np.size(); j += v) {
++factCnt[j];
np[j] = 1;
if (j%(v*v) == 0) {
// onlyOne[j] = 0;
ooo[j] = 0;
}
}
for (long long j = v*v; j < np.size(); j += v*v) {
// if (j == 14369) {
// cout << "DEBUG " << v << endl;
// }
onlyOne[j] = 0;
}
}
// for (int j = 0; j < primes.size() && v*primes[j] < np.size(); ++j) {
// np[v*primes[j]] = 1;
// factCnt[v*primes[j]] = factCnt[v];
// if (v%primes[j] == 0) {
// onlyOne[v*primes[j]] = 0;
// break;
// } else {
// ++factCnt[v*primes[j]];
// }
// }
}
// for (long long v = 2; v < np.size(); ++v) {
// if (ooo[v] != onlyOne[v]) {
// cout << "WHY " << v << " " << ooo[v] << " " << onlyOne[v] << endl;
// }
// }
for (long long g = 2; g <= R; ++g) {
long long from = max(1LL, (L+g-1)/g);
long long to = R/g;
long long cho = to-from+1;
// long long cho = to - (L-1)/g;
long long sign = factCnt[g]%2 == 0 ? -1 : 1;
// cout << "CHO " << cho << endl;
if (cho >= 1 && onlyOne[g]) {
ans += sign * cho * (cho-1) / 2;
}
if (from <= 1 && to >= from) {
ans -= (to-1);
}
// cout << sign << " " << onlyOne[g] << " " << g << " DEBUG " << from << " " << to << " " << ans << endl;
}
cout << ans*2 << endl;
return 0;
} |
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <math.h>
#include <cassert>
#define rep(i,n) for(int i = 0; i < n; ++i )
using namespace std;
using ll = long long;
const int m = 1e9+7;
int main() {
int n;
cin >> n;
const char a='A',b='B';
char aa,ab,ba,bb;
cin >> aa >> ab >> ba >> bb;
auto p = [&]{
int res = 1;
rep(_,n-3) res = (res<<1)%m;
return res;
};
int ans = [&]{
if(n<=3) return 1;
if(ab==a&&aa==a) return 1;
if(ab==b&&bb==b) return 1;
if(ab==a&&ba==b) return p();
if(ab==b&&ba==a) return p();
int x[2] = {0,1};
rep(_,n-2){
int y[2] = {x[1],(x[0]+x[1])%m};
swap(x,y);
}
return x[1];
}();
cout << ans << endl;
} | /*input
2
A
B
A
A
*/
#include <bits/stdc++.h>
using namespace std;
namespace my_template {
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef vector<pi> vpi;
typedef vector<vpi> vpii;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
typedef vector<pd> vpd;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef std::string str;
typedef std::vector<str> vs;
#define x first
#define y second
#define debug(...) cout<<"["<<#__VA_ARGS__<<": "<<__VA_ARGS__<<"]\n"
const ld PI = 3.14159265358979323846264338327950288419716939937510582097494L;
template<typename T>
pair<T, T> operator+(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x + b.x, a.y + b.y); }
template<typename T>
pair<T, T> operator-(const pair<T, T> &a, const pair<T, T> &b) { return pair<T, T>(a.x - b.x, a.y - b.y); }
template<typename T>
T operator*(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.x + a.y * b.y); }
template<typename T>
T operator^(const pair<T, T> &a, const pair<T, T> &b) { return (a.x * b.y - a.y * b.x); }
template<typename T>
void print(vector<T> vec, string name = "") {
cout << name;
for (auto u : vec)
cout << u << ' ';
cout << '\n';
}
}
using namespace my_template;
const int MOD = 1000000007;
const ll INF = std::numeric_limits<ll>::max();
const int MX = 100101;
ll mypow(ll a, ll b) {
a %= MOD;
ll ret = 1;
while (b) {
if (b & 1) ret = (ret * a) % MOD;
b >>= 1;
a = (a * a) % MOD;
}
return ret;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int N;
cin >> N;
vii mp(2, vi(2));
for (int a = 0; a < 2; ++a)
{
for (int b = 0; b < 2; ++b)
{
char ch;
cin >> ch;
mp[a][b] = ch - 'A';
}
}
if (N == 2) return !printf("1\n");
vl fac(N + 1, 1);
for (int i = 2; i <= N; ++i)
{
fac[i] = (i * fac[i - 1]) % MOD;
}
{
int k = mp[0][1];
if (mp[k][k] == k) {
printf("1\n");
return 0;
}
}
if (mp[0][1] != mp[1][0]) {
ll ats = mypow(2, N - 3);
// ll ats = 1;
// for (int i = 0; i < N - 3; ++i)
// {
// ats = (ats * 2) % MOD;
// }
printf("%lld\n", ats);
return 0;
}
// A (N-3) AB
// A A AB
ll ats = 0;
for (int reik = N - 3; reik >= 0; --reik)
{
int vietu = N - 3 - reik + 1;
if (reik > vietu) continue;
ll plus = (fac[vietu] * mypow(fac[vietu - reik], MOD - 2)) % MOD;
plus = (plus * mypow(fac[reik], MOD - 2)) % MOD;
ats = (ats + plus) % MOD;
}
printf("%lld\n", ats);
}
/* Look for:
* special cases (n=1?)
* overflow (ll vs int?)
* the exact constraints (multiple sets are too slow for n=10^6 :( )
* array bounds
*/
|
//#pragma GCC optimize(2)
//#include <bits/stdc++.h>
#include <iostream>
#include <set>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <utility>
#include <map>
#define rush() int T; while(cin>>T) while(T--)
#define ms(a,b) memset(a,b,sizeof a)
#define lowbit(x) ((x)&(-x))
#define inf 0x7f7f7f7fll
#define eps 1e-11
#define sd(a) scanf("%d",&a)
#define sll(a) scanf("%lld",&a)
#define sll2(a,b) scanf("%lld%lld",&a,&b)
#define sll3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define sdd(a,b) scanf("%d%d",&a,&b)
#define sddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define sf(a) scanf("%lf",&a)
//#define sc(a) scanf("%c\n",&a)
#define ss(a) scanf("%s",a)
#define pd(a) printf("%d\n",a)
#define pdd(a,b) printf("%d %d\n",a,b)
#define pddd(a,b,c) printf("%d %d %d\n",a,b,c)
#define pll(a) printf("%lld\n",a)
#define pf(a) printf("%.1lf\n",a)
#define pc(a) printf("%c",a)
#define ps(a) printf("%s\n",a)
#define forn(i,a,b) for(int i=(a);i<=(b);i++)
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define debug(a) cout<<#a<<" : "<<a<<" "<<" ; "
#define dbg(a) cout<<#a<<" : "<<a<<endl
#define show(a,b) cout<<a<<" :\t"<<b<<"\t*"<<endl
#define IOS ios::sync_with_stdio(0); cin.tie(0)
#define PAUSE system("pause")
//#define print(a) cout<<a<<endl;continue;
#define fr first
#define sc second
#define ceils(a,b) ((a-1ll+b)/(b))
#define floors(a,b) floor(double(a)/double(b))
using namespace std;
//inline void swap(int &x,int &y){x^=y^=x^=y;}
typedef long long ll;
//using ui=unsigned int;
typedef unsigned long long ull;
typedef pair<int,int> Pair;
const double pi=acos(-1.0);
const int dx[]={0,1,0,-1};
const int dy[]={1,0,-1,0};
const int limit=5e5;
int read()
{
int s=0;
char c=getchar(),lc='+';
while (c<'0'||'9'<c) lc=c,c=getchar();
while ('0'<=c&&c<='9') s=s*10+c-'0',c=getchar();
return lc=='-'?-s:s;
}
void write(int x)
{
if (x<0) putchar('-'),x=-x;
if (x<10) putchar(x+'0');
else write(x/10),putchar(x%10+'0');
}
void print(int x,char c='\n')
{
write(x);
putchar(c);
}
ll mod=998244353;
const int N=1e6+5;
int n, m, _;
int i, j, k;
//int a[N];
char s[N],t[N];
vector<int> u,v;
signed main()
{
//IOS;
while(cin>>n){
ss(s+1); ss(t+1);
for(int i=1;i<=n;i++){
if(s[i]=='0') u.pb(i);
if(t[i]=='0') v.pb(i);
}
if(u.size()!=v.size()) puts("-1");
else{
int ans=0;
for(int i=0;i<u.size();i++){
if(u[i]==v[i]) continue;
ans++;
}
pd(ans);
}
}
//PAUSE;
return 0;
} | #include<bits/stdc++.h>
int main(){
using namespace std;
unsigned long N;
cin >> N;
vector<unsigned long> A(2 * N);
for(auto&& i : A)cin >> i;
vector<pair<unsigned long, unsigned long>> B;
B.reserve(2 * N);
transform(begin(A), end(A), back_inserter(B), [cnt{0UL}](unsigned long a) mutable {return make_pair(a, cnt++);});
nth_element(begin(B), begin(B) + N, end(B));
vector<unsigned long> HL(2 * N);
for(unsigned long i{}; i < N; ++i)HL[B[i].second] = 1;
vector<pair<unsigned long, unsigned long>> rem_idx{};
string ans(2 * N, ' ');
for(unsigned long i{}; i < 2 * N; ++i)if(!rem_idx.empty() && rem_idx.back().second != HL[i]){
ans[rem_idx.back().first] = '(';
ans[i] = ')';
rem_idx.pop_back();
}else rem_idx.emplace_back(i, HL[i]);
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod=1e9+7;
signed main(){
int n;
string s,t;
cin>>n>>s>>t;
int ans=0;
vector<int>s1;
for(int i=n-1;i>=0;i--)
if(s[i]=='1')
s1.push_back(-i);
for(int i=0;i<n;i++){
if(s[i]==t[i])
continue;
int pos=lower_bound(s1.begin(),s1.end(),-i)-s1.begin()-1;
if(pos==-1){
cout<<-1<<endl;
return 0;
}
ans+=-i-s1[pos];
s[-s1[pos]]='0'+'1'-s[-s1[pos]];
s1[pos]=-i;
}
cout<<ans<<endl;
} | #include <bits/stdc++.h>
// #include <atcoder/all>
// #include "icld.cpp"
using namespace std;
using ll = long long int;
using vi = vector<int>;
using si = set<int>;
using vll = vector<ll>;
using vvi = vector<vector<int>>;
using ss = string;
using db = double;
template<typename T> using minpq = priority_queue <T,vector<T>,greater<T>>;
const int dx[4] = {1,0,-1,0};
const int dy[4] = {0,1,0,-1};
#define V vector
#define P pair<int,int>
#define rep(i,s,n) for(int i=(s);i<(int)(n);i++)
#define rev(i,s,n) for(int i=(s);i>=(int)(n);i--)
#define reciv(v,n) vi (v)((n)); rep(i,0,(n))cin>>v[i]
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define ci(x) cin >> x
#define cii(x) ll x;cin >> x
#define cci(x,y) ll x,y;cin >> x >> y
#define co(x) cout << x << endl
#define pb push_back
#define eb emplace_back
#define rz resize
#define pu push
#define sz(x) int(x.size())
#define vij v[i][j]
// ll p = 1e9+7;
// ll p = 998244353;
#define yn cout<<"Yes"<<endl;else cout<<"No"<<endl
#define YN cout<<"YES"<<endl;else cout<<"NO"<<endl
template<class T>void chmax(T &x,T y){x=max(x,y);}
template<class T>void chmin(T &x,T y){x=min(x,y);}
int main(){
cii(n);
cci(x0,y0);
cci(x2,y2);
db pi=acos(-1);
db S=pi/n*2;
db sinS=sin(S);
db cosS=cos(S);
// co(sinS);co(cosS);
db hx=1.*(x0+x2)/2;
db hy=1.*(y0+y2)/2;
// co(hx);co(hy);
db xd=x0-hx;
db yd=y0-hy;
db x,y;
x= xd*cosS - yd*sinS +hx;
y= xd*sinS + yd*cosS +hy;
// x=xd*sinS-yd*cosS+hx;
// y=xd*cosS+yd*sinS+hy;
printf("%.9lf %.9lf\n",x,y);
} |
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
// Acknowledgement: Special thanks to kyomukyomupurin, who developed this
// template.
template <class T, class U>
std::ostream& operator<<(std::ostream& os, const std::pair<T, U>& p) {
return os << '(' << p.first << ", " << p.second << ')';
}
template <class T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec) {
int n = 0;
for (auto e : vec) os << (n++ ? ", " : "{") << e;
return os << (n ? "}" : "{}");
}
template <class T>
std::ostream& operator<<(std::ostream& os, const std::set<T>& st) {
int n = 0;
for (auto e : st) os << (n++ ? ", " : "{") << e;
return os << (n ? "}" : "{}");
}
template <class T, class U>
std::ostream& operator<<(std::ostream& os, const std::map<T, U>& mp) {
int n = 0;
for (auto e : mp) os << (n++ ? ", " : "{") << e;
return os << (n ? "}" : "{}");
}
template <class T>
std::istream& operator>>(std::istream& is, std::vector<T>& vec) {
for (T& e : vec) is >> e;
return is;
}
#ifdef LOCAL
#define debug(...) \
std::cerr << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
void debug_out() { std::cerr << '\n'; }
template <class Head, class... Tail>
void debug_out(Head&& head, Tail&&... tail) {
std::cerr << head;
if (sizeof...(Tail) != 0) std::cerr << ", ";
debug_out(std::forward<Tail>(tail)...);
}
using namespace std;
using int64 = long long;
char janken(char c1, char c2) {
if (c1 == 'R') {
if (c2 == 'P') return 'P';
if (c2 == 'S') return 'R';
}
if (c1 == 'P') {
if (c2 == 'R') return 'P';
if (c2 == 'S') return 'S';
}
if (c1 == 'S') {
if (c2 == 'R') return 'R';
if (c2 == 'P') return 'S';
}
return c1;
}
int main() {
int n, k;
cin >> n >> k;
string s;
cin >> s;
string tmp = s;
for (int j = 0; j < k; j++) {
tmp = s + s;
for (int i = 0; i < n; i++) {
s[i] = janken(tmp[i * 2], tmp[i * 2 + 1]);
}
}
cout << s[0] << '\n';
return 0;
} | #include <bits/stdc++.h>
#define DEBUG if(0)
#define lli long long int
#define ldouble long double
using namespace std;
struct Data
{
int cnt, value;
Data operator+(const Data &other)
{
if (cnt == 0 && other.cnt == 0)
return value < other.value ? *this: other;
if (cnt == 0)
return *this;
if (other.cnt == 0)
return other;
return *this;
}
};
const Data nil = Data{1, (int)1e9};
// DON'T FORGET TO DEFINE THE NIL!!!
template<class T>
struct Segtree
{
int size;
T nil;
vector<T> data;
vector<T> st;
Segtree() { }
Segtree(int size, T nil) : size(size), nil(nil)
{
data.resize(size, nil);
st.resize(4*size, nil);
}
Segtree(vector<T> &data, T nil) : size(data.size()), data(data), nil(nil)
{
st.resize(4*size);
build();
}
void build() { build(1, 0, size - 1); }
T query(int qlo, int qhi) { return query(qlo, qhi, 1, 0, size - 1); }
void update(int pos, T value) { update(pos, value, 1, 0, size - 1); }
void build(int i, int lo, int hi)
{
if (lo == hi)
{
st[i] = data[lo];
return;
}
int mid = (lo + hi) >> 1;
build(2*i, lo, mid), build(2*i + 1, mid + 1, hi);
st[i] = st[2*i] + st[2*i + 1];
}
T query(int qlo, int qhi, int i, int lo, int hi)
{
if (hi < qlo || lo > qhi) return nil;
if (lo >= qlo && hi <= qhi) return st[i];
int mid = (lo + hi) >> 1;
return query(qlo, qhi, 2*i, lo, mid) + query(qlo, qhi, 2*i + 1, mid + 1, hi);
}
void update(int pos, T &value, int i, int lo, int hi)
{
if (lo == hi)
{
// st[i] = data[lo] = value;
st[i].cnt += value.cnt;
return;
}
int mid = (lo + hi) >> 1;
if (pos <= mid) update(pos, value, 2*i, lo, mid);
else update(pos, value, 2*i + 1, mid + 1, hi);
st[i] = st[2*i] + st[2*i + 1];
}
};
Segtree<Data> segtree;
const int maxN = 1500000; int n, m;
int a[maxN];
int main()
{
while (~scanf("%d %d", &n, &m))
{
segtree = Segtree<Data>(maxN + 1, nil);
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
int ans = 1e9;
for (int i = 0; i <= maxN; i++)
segtree.data[i] = Data{0, i};
segtree.build();
for (int i = 0; i < m; i++)
segtree.update(a[i], Data{1, 0});
for (int i = 0; i + m - 1 < n; i++)
{
// [i : i + m - 1]
ans = min(ans, segtree.query(0, maxN).value);
segtree.update(a[i], Data{-1, 0});
if (i + m < n) segtree.update(a[i + m], Data{1, 0});
}
printf("%d\n", ans);
}
return 0;
} |
#include <bits/stdc++.h>
#define f(i,j,k) for(long long i=j;i<k;i++)
#define f2(i,j,k) for(long long i=j;i>=k;i--)
#define ll long long
#define ld long double
using namespace std;
const long long mod=1e9+7;
const long long mod2=998244353;
const long long INF = (1 << 29);
void chmin(int& a, int b){ if(a > b) a = b; }
void chmax(int& a, int b){ if(a < b) a = b; }
void yn(ll a){
if(a==0){
cout<<"Yes"<<endl;
}
else{
cout<<"No"<<endl;
}
}
int main(){
string s,t,u;
ll a,b,c,d,e,n,k,ans=0,ans3=0;
ld x,y,z,ans2=0;
cin>>n;
f(i,0,n){
cin>>x;
ans+=abs(x);
ans2+=x*x;
a=abs(x);
ans3=max(ans3,a);
}
cout<<ans<<endl;
cout<<fixed<<setprecision(10)<<sqrt(ans2)<<endl;
cout<<ans3<<endl;
return 0;
}
| #include <bits/stdc++.h>
#define repd(i, a, b) for (ll i = (a); i < (b); i++)
#define repb(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep(i, n) repd(i, 0, n)
using namespace std;
using ll = long long;
using ul = unsigned long long;
using ld = long double;
const ul mod = 1000000007;
void output(ll a, ll b, ll c, ll d) {
cout << a << " " << b << " " << c << " " << d << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n;
cin >> n;
vector<ll> x(n), y(n), r(n);
rep(i, n) { cin >> x[i] >> y[i] >> r[i]; }
rep(i, n) { output(x[i], y[i], x[i] + 1, y[i] + 1); }
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
int t;
/* cin>>t;
while(t--)
{*/
ll n;
vector<ll>v,v1;
string s,s1;
ll a,k,rem,b,x,y,coun=0,new_coun=0,sum=0,flag=0,ans=0,c_0=0,c_1=0,c_2=0;
cin>>n>>k;
ans=n;
while(k--)
{
a=n;
while(n != 0)
{
rem= n%10 ;
v.push_back(rem);
n/=10;
}
sort(v.begin(),v.end());
ll temp=0;
for(ll i=0;i<v.size();i++)
{
temp*=10;
temp+=v[i];
}
sort(v.begin(),v.end(),greater<ll>());
ll temp1=0;
for(ll i=0;i<v.size();i++)
{
temp1*=10;
temp1+=v[i];
}
ans =temp1-temp;
n=ans;
v.clear();
}
cout<<ans<<endl;
//}
return 0;
}
| #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define MV 200001
#define LMV 21
#define ff first
#define ss second
#define pb push_back
#define eb emplace_back
#define emp emplace
#define mp make_pair
#define ins insert
#define sz(x) (int)x.size()
#define whoami(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); whi(_it, args); }
void whi(istream_iterator<string> it) { cerr<<"\n"; }
template<typename T, typename... Args>
void whi(istream_iterator<string> it, T a, Args... args) { cerr<<*it<<" "<<a<<" "; whi(++it, args...); }
void FLASH() {ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);}
void SETF() {cout.ios_base::setf(ios_base::fixed); cerr.ios_base::setf(ios_base::fixed);}
void UNSETF() {cout.ios_base::unsetf(ios_base::fixed); cerr.ios_base::unsetf(ios_base::fixed);}
typedef long long ll;
typedef long double ld;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef pair<PII, int> PPII;
typedef pair<PLL, ll> PPLL;
typedef map<int, int> MII;
const int MOD = 1000000007;
const ll INF = 4e18;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
struct h_llint {
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);
}
};
struct h_pair{
size_t operator()(const PLL&x)const{
return hash<ll>()(((ll)x.ff)^(((ll)x.ss)<<32));
}
};
typedef map<ll, ll> MLL;
typedef map<PII, int> MPII;
typedef map<PLL, ll> MPLL;
typedef set<int> SI;
typedef set<ll> SL;
//ordered_set = order_of_key(.)
//ordered_set = find_by_order(.)
typedef ordered_set<int> OSI;
typedef ordered_set<ll> OSL;
typedef ordered_multiset<int> MOSI;
typedef ordered_multiset<ll> MOSL;
typedef unordered_map<ll, int, h_llint> UMLI;
typedef unordered_map<ll, ll, h_llint> UMLL;
typedef unordered_map<PLL, int, h_pair> UMPI;
typedef unordered_map<PLL, ll, h_pair> UMPL;
int ar[MV];
ll arr[MV];
void solve(int T)
{
ll n,k;
cin>>n>>k;
if(!k)
{
cout<<n<<"\n";
return;
}
for(int i=1;i<=k;i++)
{
VL xf;
while(n)
{
xf.pb(n%10);
n /= 10;
}
sort(begin(xf), end(xf), [](ll A, ll B){ return (A < B); });
ll n1 = 0;
for(auto &&u : xf)
n1 = 10*n1 + u;
sort(begin(xf), end(xf), [](ll A, ll B){ return (A > B); });
ll n2 = 0;
for(auto &&u : xf)
n2 = 10*n2 + u;
n = n2 - n1;
}
cout<<n<<"\n";
return;
}
int main(void)
{
FLASH();
//freopen("cowjog.in", "r", stdin);
//freopen("cowjog.out", "w", stdout);
int T;
T = 1;
#ifndef ONLINE_JUDGE
time_t time_t1, time_t2;
time_t1 = clock();
#endif
//cin>>T;
while(T--)
solve(T);
#ifndef ONLINE_JUDGE
time_t2 = clock();
SETF();
cerr<<"Time taken: "<<setprecision(7)<<(time_t2 - time_t1)/(double)CLOCKS_PER_SEC<<"\n";
UNSETF();
#endif
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define db double
#define pii pair<int,int>
#define pli pair<ll,int>
#define pil pair<int,ll>
#define pll pair<ll,ll>
#define ti3 tuple<int,int,int>
#define mat vector<vector<int>>
const int inf = 1 << 30;
const ll mod = 1e9 + 7;
const ll linf = 1LL << 62;
const db EPS = 1e-7;
template<class T> void chmin(T& x, T y){if(x > y) x = y;}
template<class T> void chmax(T& x, T y){if(x < y) x = y;}
int N, M;
vector<pii> v;
map<int, vector<int>> mp;
int ok[400010];
vector<int> xs[400010];
int main() {
cin >> N >> M;
for (int i = 0; i < M; i++) {
int X, Y;
cin >> X >> Y;
v.push_back({X, Y});
}
sort(v.begin(), v.end());
for (int i = 0; i < M; i++) {
if (N - M <= v[i].second && v[i].second <= N + M) xs[v[i].second - N + M].push_back(v[i].first);
}
for (int i = 0; i <= 2 * M; i++) {
xs[i].push_back(2 * N + 1);
}
if (xs[M].empty()) {
ok[M] = 2 * N;
} else {
ok[M] = xs[M][0];
}
vector<pii> up;
for (int i = 0; i < M; i++) {
int y = v[i].second;
if (N - M <= y && y <= N + M) {
y -= N - M;
if (y > 0 && ok[y - 1] >= v[i].first) {
up.push_back({y, *(upper_bound(xs[y].begin(), xs[y].end(), v[i].first))});
}
if (y < 2 * M && ok[y + 1] >= v[i].first) {
up.push_back({y, *(upper_bound(xs[y].begin(), xs[y].end(), v[i].first))});
}
}
if (i == M - 1 || v[i].first != v[i + 1].first) {
for (auto u : up) {
chmax(ok[u.first], u.second);
}
up.clear();
}
}
int cnt = 0;
for (int i = 0; i <= 2 * M; i++) {
cnt += (ok[i] == 2 * N + 1);
}
cout << cnt << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, l, r) for (int i = l; i <= r; ++i)
#define Next(i, u) for (int i = h[u]; i; i = e[i].next)
const int N = 2e3 + 5;
const int M = 3e7 + 5;
const int inf = 1e9;
struct edge { int v, next, w;} e[M];
queue <int> Q;
char s[N][N]; int n, m, S, T, tot, h[N * N], dis[N * N];
int read() {
char c; int x = 0, f = 1;
c = getchar();
while (c > '9' || c < '0') { if(c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int c(int x, int y) { return (x - 1) * m + y;}
void add(int u, int v, int w) { e[++tot].v = v, e[tot].w = w, e[tot].next = h[u], h[u] = tot;}
int main () {
n = read(), m = read();
rep(i, 1, n) scanf("%s", s[i] + 1);
rep(i, 1, n) rep(j, 1, m) if(s[i][j] == 'S') S = c(i, j);
rep(i, 1, n) rep(j, 1, m) if(s[i][j] == 'G') T = c(i, j);
rep(i, 1, n) rep(j, 1, m) if(s[i][j] != '#') {
if(s[i - 1][j] != '#' && i > 1) add(c(i, j), c(i - 1, j), 1);
if(s[i][j - 1] != '#' && j > 1) add(c(i, j), c(i, j - 1), 1);
if(s[i + 1][j] != '#' && i < n) add(c(i, j), c(i + 1, j), 1);
if(s[i][j + 1] != '#' && j < m) add(c(i, j), c(i, j + 1), 1);
if(s[i][j] >= 'a' && s[i][j] <= 'z') add(c(i, j), n * m + s[i][j] - 'a' + 1, 0), add(n * m + s[i][j] - 'a' + 1, c(i, j), 1);
}
memset(dis, 0x3f, sizeof(dis));
dis[S] = 0, Q.push(S);
while (!Q.empty()) {
int u = Q.front(); Q.pop();
Next(i, u) {
int v = e[i].v; if(dis[v] < inf) continue;
dis[v] = dis[u] + e[i].w, Q.push(v);
}
}
printf("%d", dis[T] < inf ? dis[T] : -1);
return 0;
} |
#include <bits/stdc++.h>
using Int = long long; // clang-format off
#define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define ALL(v) std::begin(v), std::end(v)
struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io;
#ifndef dump
#define dump(...)
#endif // clang-format on
/**
* author: knshnb
* created: Sun Oct 11 23:22:41 JST 2020
**/
template <class T> bool chmin(T& a, const T& b) { return a > b ? a = b, true : false; }
template <class T> bool chmax(T& a, const T& b) { return a < b ? a = b, true : false; }
const Int INF = 1e18;
signed main() {
Int n, m;
std::cin >> n >> m;
std::vector<Int> w(n);
REP(i, n) std::cin >> w[i];
std::map<Int, Int> mp;
mp[0] = 0;
mp[INF] = INF;
REP(i, m) {
Int l, v;
std::cin >> l >> v;
if (v < *std::max_element(ALL(w))) {
std::cout << -1 << std::endl;
return 0;
}
chmax(mp[v], l);
}
Int ma = 0;
for (auto& p : mp) {
chmax(ma, p.second);
p.second = ma;
}
Int ans = INF;
std::sort(ALL(w));
do {
std::vector<Int> d(n);
REP(i, 1, n) {
Int sum = w[i];
for (Int j = i - 1; j >= 0; j--) {
sum += w[j];
auto it = std::prev(mp.lower_bound(sum));
chmax(d[i], d[j] + it->second);
}
}
chmin(ans, d.back());
} while (std::next_permutation(ALL(w)));
std::cout << ans << std::endl;
}
| #include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <map>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
vector<int> ws(n);
for (int i = 0; i < n; i++) {
cin >> ws[i];
}
vector<int> ls(m), vs(m), indices(m);
for (int i = 0; i < m; i++) {
cin >> ls[i] >> vs[i];
}
if (*max_element(begin(ws), end(ws)) > *min_element(begin(vs), end(vs))) {
cout << -1 << endl;
return 0;
}
iota(begin(indices), end(indices), 0);
sort(begin(indices), end(indices), [&](int i, int j) { return vs[i] < vs[j]; });
map<int, int, greater<int>> map; // map[x] := 対荷重x以下のパーツの中で最長の長さ
int max_length = 0;
map[0] = 0;
for (int i : indices) {
max_length = max(max_length, ls[i]);
map[vs[i]] = max_length;
}
vector<int> perm(n);
iota(begin(perm), end(perm), 0);
int64_t ans = 1LL << 50;
do {
vector<vector<int>> cost(n, vector<int>(n, 0));
for (int i = 0; i < n; i++) {
int w_sum = ws[perm[i]];
for (int j = i + 1; j < n; j++) {
w_sum += ws[perm[j]];
cost[i][j] = map.upper_bound(w_sum)->second;
}
}
vector<int> table(n, 0);
for (int i = n - 1; i >= 0; i--) {
for (int j = i + 1; j < n; j++) {
table[i] = max(table[i], cost[i][j] + table[j]);
}
}
ans = min(ans, (int64_t)table[0]);
} while (next_permutation(begin(perm), end(perm)));
cout << ans << endl;
return 0;
} |
/*
f[n][k] = f[n - 1][k - 1] + f[n][2k]
*/
#include <cstdio>
#include <algorithm>
const int N = 3000 + 10;
const int MOD = 998244353;
int n, m;
int f[N][N << 1];
inline int qmod(int x) { return (x>=MOD)?(x-MOD):x; }
int main() {
int i, j;
scanf("%d %d", &n, &m);
f[0][0] = 1;
for(i=1; i<=n; i++)
for(j=i; j; j--)
f[i][j] = qmod(f[i - 1][j - 1] + f[i][j << 1]);
printf("%d\n", f[n][m]);
return 0;
} | #include <bits/stdc++.h>
#define fi first
#define se second
#define DB double
#define U unsigned
#define P std::pair
#define LL long long
#define LD long double
#define pb push_back
#define MP std::make_pair
#define SZ(x) ((int)x.size())
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define FOR(i,a,b) for(int i = a;i <= b;++i)
#define ROF(i,a,b) for(int i = a;i >= b;--i)
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl
const int MAXN = 3e5 + 5;
int n,k,cnt[MAXN];
int main(){
scanf("%d%d",&n,&k);
FOR(i,1,n){
int x;scanf("%d",&x);++cnt[x];
}
int rem = k;LL ans = 0;
FOR(i,0,n-1){
rem = std::min(rem,cnt[i]);
ans += rem;
}
printf("%lld\n",ans);
return 0;
}
|
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <ctime>
#include <iterator>
#include <bitset>
#include <numeric>
#include <list>
#include <iomanip>
#include <cassert>
#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>
using namespace std;
#define cauto const auto&
#define ALL(v) begin(v),end(v)
#else
#define ALL(v) (v).begin(),(v).end()
#endif
namespace{
typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;
#define VV(T) vector<vector< T > >
template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
v.assign(a, vector<T>(b, t));
}
template <class T> inline T &chmin(T &x, const T &y){ return x = min(x, y); }
template <class T> inline T &chmax(T &x, const T &y){ return x = max(x, y); }
template <class F, class T>
void convert(const F &f, T &t){
stringstream ss;
ss << f;
ss >> t;
}
template <class Con>
string concat(const Con &c, const string &spr){
stringstream ss;
typename Con::const_iterator it = c.begin(), en = c.end();
bool fst = true;
for(; it != en; ++it){
if(!fst){ ss << spr; }
fst = false;
ss << *it;
}
return ss.str();
}
template <class Con, class Fun>
vector<typename Con::value_type> cfilter(const Con &c, Fun f) {
vector<typename Con::value_type> ret;
typename Con::const_iterator it = c.begin(), en = c.end();
for(; it != en; ++it){
if(f(*it)){
ret.emplace_back(*it);
}
}
return ret;
}
#if __cplusplus >= 201103L
template <class Con, class Fun>
auto cmap(const Con &c, Fun f) -> vector<decltype(f(*c.begin()))> {
vector<decltype(f(*c.begin()))> ret;
ret.reserve(c.size());
for(const auto &x: c){
ret.emplace_back(f(x));
}
return ret;
}
#endif
#if __cplusplus >= 201402L
#define lambda(e) ([&](const auto &_){ return (e); })
#define lambda2(e) ([&](const auto &_a, const auto &_b){ return (e); })
#endif
#define REP(i,n) for(int i=0;i<int(n);++i)
#define RALL(v) (v).rbegin(),(v).rend()
#define tget(t,i) get<i>(t)
#define MOD 1000000007LL
#define EPS 1e-8
vint input(map<char,int> &mp){
string s;
cin >> s;
vint v;
for(char c : s){
v.push_back(mp.emplace(c, mp.size()).first->second);
}
return v;
}
LL calc(const vector<int> &perm, const vector<int> &v){
LL r = 0;
for(int x : v){
r = r * 10 + perm[x];
if(r == 0){ return -1; }
}
return r;
}
void mainmain(){
map<char,int> mp;
vint a = input(mp);
vint b = input(mp);
vint c = input(mp);
if(mp.size() <= 10){
vint perm(10);
iota(ALL(perm), 0);
do{
LL d = calc(perm, a);
LL e = calc(perm, b);
LL f = calc(perm, c);
if(d > 0 && e > 0 && f > 0 && d + e == f){
cout << d << '\n' << e << '\n' << f << '\n';
return;
}
} while(next_permutation(ALL(perm)));
}
cout << "UNSOLVABLE\n";
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
cout << fixed << setprecision(10);
cerr << fixed << setprecision(4);
mainmain();
}
| /**
* author: otera
**/
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<deque>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<array>
using namespace std;
#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60 ;
const ll mod=1e9+7 ;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define pb push_back
#define debug(x) cerr << #x << " = " << (x) << endl;
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; }
void solve() {
char s, t; cin >> s >> t;
if(s == 'Y') {
cout << (char)('A' + (t - 'a')) << endl;
} else {
cout << t << endl;
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
//cout << fixed << setprecision(10);
//int t; cin >> t; rep(i, t)solve();
solve();
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long r, x, y;
cin >> r >> x >> y;
long long cnt = 0;
long long rad = 0;
while (rad * rad < x * x + y * y) {
cnt = cnt + 1;
rad = rad + r;
}
if (cnt == 1) {
if (rad * rad > x * x + y * y) {
cnt = cnt + 1;
}
}
cout << cnt << '\n';
return 0;
} | #include<bits/stdc++.h>
using namespace std;
int n,a,b;
int main()
{
scanf("%d%d%d",&n,&a,&b);
printf("%d\n",n-a+b);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;using ll=long long;using uint=unsigned int;using pii=pair<int,int>;using pll=pair<ll,ll>;using ull = unsigned long long;using ld=long double;template<typename T>void _(const char*s,T h){cerr<<s<<" = "<<h<<"\n";}template<typename T,typename...Ts>void _(const char*s,T h,Ts...t){int b=0;while(((b+=*s=='(')-=*s==')')!=0||*s!=',')cerr<<*s++;cerr<<" = "<<h<<",";_(s+1,t...);}// break continue pop_back 998244353
#define int ll
#define pii pll
#define f first
#define s second
#define pb emplace_back
#define forn(i,n) for(int i=0;i<(n);++i)
#define sz(a)((int)(a).size())
#define sqr(x) ((x)*(x))
struct init{init(){cin.tie(0);iostream::sync_with_stdio(0);cout<<fixed<<setprecision(10);cerr<<fixed<<setprecision(5);}~init(){
#ifdef LOCAL
#define dbg(...) _(#__VA_ARGS__,__VA_ARGS__)
cerr<<"Time elapsed: "<<(double)clock()/CLOCKS_PER_SEC<<"s.\n";
#else
#define dbg(...)
#endif
}}init;template<typename T,typename U>void upx(T&x,U y){if(x<y)x=y;}template<typename T,typename U>void upn(T&x,U y){if(x>y)x=y;}mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());const int D=4,dx[]={+1,0,-1,0},dy[]={0,+1,0,-1};
const int N=202020;
vector<int> g[N];
int c[N];
int cc[N];
int d[N];
int h[N];
void dfs1(int v,int p=0,int dd=0){
d[v]=dd;
for(int to:g[v])
if(to^p)
dfs1(to,v,dd+1);
}
void dfs2(int v,int p=0){
h[v]=0;
for(int to:g[v])
if(to^p){
dfs2(to,v);
upx(h[v],h[to]+1);
}
}
int q;
void dfs3(int v,int p=0){
c[v]=q;
sort(g[v].begin(),g[v].end(),[&](int x,int y){
return h[x]<h[y];
});
for(int to:g[v]){
if(to^p){
q++;
dfs3(to,v);
q++;
}
}
}
int32_t main(){
int n;
cin>>n;
forn(i,n-1){
int x,y;
cin>>x>>y;
g[x].pb(y);
g[y].pb(x);
}
dfs1(1);
int s1=max_element(d+1,d+n+1)-d;
dfs1(s1);
int s2=max_element(d+1,d+n+1)-d;
dfs2(s1);
q=1;
dfs3(s1);
for(int i=1;i<=n;++i){
cc[i]=c[i];
}
dfs2(s2);
q=1;
dfs3(s2);
int o1=*max_element(c+1,c+n+1);
int o2=*max_element(cc+1,cc+n+1);
if(o1<o2){
for(int i=1;i<=n;++i)cout<<c[i]<<' ';
}else{
for(int i=1;i<=n;++i)cout<<cc[i]<<' ';
}
cout<<'\n';
return 0;
}
| #pragma GCC optimize("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target ("avx2")
#define io_init cin.tie(0);ios::sync_with_stdio(0);cout<<fixed<<setprecision(20)
#include <bits/stdc++.h>
constexpr int INF = 2147483647;
constexpr long long int INF_LL = 9223372036854775807;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
//Dijkstra法
typedef pair<ll, ll> P;
void Dijkstra(int s, vector<vector<pair<ll,ll>>>& g, vector<ll>& d) {
priority_queue<P, vector<P>, greater<P>> q;
d = vector<ll>(g.size(), INF_LL);
d[s] = 0;
q.push(P{ 0,s });
while (!q.empty()) {
P p = q.top(); q.pop();
ll v = p.second;
if (d[v] < p.first)continue;
for (int i = 0; i < g[v].size(); i++) {
auto e = g[v][i];
if (d[e.first] > d[v] + e.second) {
d[e.first] = d[v] + e.second;
q.push(P{ d[e.first] , e.first });
}
}
}
}
int main() {
int a, b, x, y;
cin >> a >> b >> x >> y;
a--; b--;
vector < vector<pair<ll, ll>>> G(200);
for (int i = 0; i < 100; i++) {
G[i].push_back({ i+100,x });
G[i+100].push_back({ i,x });
}
for (int i = 0; i < 99; i++) {
G[i + 1].push_back({ i + 100,x });
G[i + 100].push_back({ i + 1 ,x });
G[i + 1].push_back({ i,y });
G[i].push_back({ i + 1,y });
}
vector<ll> d;
Dijkstra(a, G, d);
cout << d[b + 100] << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PP;
#define MOD 1000000007
//#define MOD 998244353
#define INF 2305843009213693951
//#define INF 810114514
#define PI 3.141592653589
#define setdouble setprecision
#define REP(i,n) for(ll i=0;i<(n);++i)
#define OREP(i,n) for(ll i=1;i<=(n);++i)
#define RREP(i,n) for(ll i=(n)-1;i>=0;--i)
#define all1(i) begin(i),end(i)
#define GOODBYE do { cout << "-1" << endl; return 0; } while (false)
#define MM <<" "<<
#define Endl endl
#define debug true
#define debug2 false
class segmenttree{
/*
Copyright (c) 2021 0214sh7
https://github.com/0214sh7/library/
*/
private:
int n;
long long identity = 0;//単位元
std::vector<long long> dat;
public:
void init(int N){
n=1;
while(n<N)n*=2;
dat.clear();
for(int i=0;i<2*n-1;++i){
dat.push_back(identity);
}
}
segmenttree(int N){
init(N);
}
long long calc();
void update(int k,long long a){
k+=n-1;
dat[k]=a;
while(k>0){
k=(k-1)/2;
dat[k]=calc(dat[2*k+1],dat[2*k+2]);
}
}
long long query(long long a,long long b){
a+=n;
b+=n;
long long R=0;
while(a < b){
if(a % 2 == 1){
R = calc(R, dat[a - 1]);
a += 1;
}
a /= 2;
if(b % 2 == 1){
b -= 1;
R = calc(R, dat[b - 1]);
}
b /= 2;
}
return R;
}
long long calc(long long a,long long b){
//your monoid here
return max(a,b);
}
};
int main(void){
ll N;
vector<ll> A,B(214514);
cin >> N;
REP(i,N){
ll a;
cin >> a;
A.push_back(a);
}
B[0]=A[0];
REP(i,N-1){
B[i+1]=B[i]+A[i+1];
}
segmenttree SEG(N);
REP(i,N){
SEG.update(i,A[i]);
}
ll num = 0;
REP(i,N){
ll m = SEG.query(0,i+1);
num += B[i];
ll Ans = num;
Ans += (i+1)*m;
cout << Ans << endl;
}
return 0;
}
| #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--)
#define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++)
#define rrep2(ri,x,n) for(int ri = (int)(n-1); ri >= (int)(x); ri--)
#define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;
bool f(int n){
string s = to_string(n);
for(auto c : s){
if(c == '7') return false;
}
while(n != 0){
int now = n % 8;
if(now == 7) return false;
n /= 8;
}
return true;
}
int main(){
int n;
cin >> n;
n++;
int ans = 0;
rep2(i, 1, n){
if(f(i) == true) ans++;
}
cout << ans << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define N 2114514
const ll mod = 998244353;
const ll inf = 1000000000000000000;
ll a[N],b[N];
int main() {
ll n;
cin >> n;
ll sum = 0;
for(int i=0;i<n;i++)cin>>a[i];
for(int i=0;i<n;i++)cin>>b[i];
for(int i=0;i<n;i++)sum+=a[i]*b[i];
if (sum)
cout << "No";
else
cout << "Yes";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
// #define ll unsigned long long
#define mp make_pair
#define pi pair<int,int>
#define vi vector<int>
#define vc vector<char>
#define vs vector<string>
#define vl vector<ll>
#define vb vector<bool>
#define vu vectorll>
#define vpi vector<pi>
#define ve(a) vector<a>
#define mi map<int,int>
#define all(a) (a).begin(),(a).end()
#define fr(i, a, b) for(int i = a; i < b; i++)
#define rf(i, a, b) for(int i = a; i >= b; i--)
#define M 1000000007
#define faster ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define mem(a) memset(a, 0, sizeof(a))
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) ((a)*(b)) / __gcd((a),(b))
#define exists(v, e) binary_search(v.begin(), v.end(), e)
#define index(v, e) (int)(find(v.begin(), v.end(), e) - v.begin())
#define lw(v, e) lower_bound(v.begin(), v.end(), e) - v.begin()
#define up(v, e) upper_bound(v.begin(), v.end(), e) - v.begin()
#define maxh(a) priority_queue<a>
#define minh(a) priority_queue<a, vector<a>, greater<a>>
#define fi first
#define se second
#define nob(a) __builtin_popcount(a)
ll i, j, k, l, m, n, o, p, q, x, y, z, res, ans;
string s, r, str, s1, s2, s3, s4;
char c, c1, c2, c3, c4;
void solve(){
cin >> n;
ans = 0;
vi a, b;
fr(i, 0, n){
cin >> x;
a.pb(x);
}
fr(i, 0, n){
cin >> x;
b.pb(x);
}
fr(i, 0, n){
ans += a[i] * b[i];
}
if(ans == 0){
cout << "Yes\n";
return;
}
cout << "No\n";
}
int main(){
faster;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("error.txt", "w", stderr);
freopen("output.txt", "w", stdout);
#endif
solve();
return 0;
} |
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
#define int long long
typedef unsigned long long lu;
typedef vector<ll> v;
typedef vector<vector<ll> > vv;
typedef vector<string> vs;
typedef vector<pair<ll,ll>> vpr;
typedef vector<bool>vb;
typedef vector<double>vd;
typedef long double ld;
#define f(i,n) for(ll i = 0; i < n; i++)
#define ff(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define endl "\n"
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define amax(x,y) if(y>x) x=y;
#define amin(x,y) if(y<x)x=y;
#define bg(x) x.begin()
#define sz(x) (ll)x.size()
#define in(x,n) for(ll i=0;i<n;i++)cin>>x[i]
#define out(x,n) for(ll i=0;i<n;i++)cout<<x[i]<<" "
#define mxt(a) *(max_element(a.begin(),a.end()))
#define mnt(a) *(min_element(a.begin(),a.end())
#define tc ll t;cin>>t;while(t--)
typedef pair<ll,ll> pi;
#define yes cout<<"YES\n";
#define no cout<<"NO\n";
#define yesno(f) if(f) yes else no
const v dx = {1, -1, 0, 0};
const v dy = {0, 0, 1, -1};
const ld PI = 2 * acos(0.0);
ll cel(ll x1,ll y1){if((x1%y1)==0)return x1/y1;else return x1/y1+1;}
ll power(ll a,ll b,ll m)
{
if(b==0)
return 1;
ll d=power(a,b/2,m);
d=(d*d)%m;
if(b&1)
d=(d*a)%m;
return d;
}
const ll mod=1e9+7;
int MOD(int a)
{
if(a<0)
a+=mod;
if(a>=mod)
a%=mod;
return a;
}
// set_name.find_by_order(k) It returns to an iterator to the kth element (counting from zero) in the set in O(logn) time
// set_name.order_of_key(k) It returns to the number of items that are strictly smaller than our item k in O(logn) time.
/*string operations :
str.substr (x,y) : returns a substring str[x],str[x+1],...str[x+y-1]
str.substr (x) : returns a substring str[x],... end of string
str.find(qtr) : returns the first occurenece of qtr in str */
int32_t main()
{
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int n;cin>>n;
v a(n);in(a,n);
v b(n);in(b,n);
v M=a;
for (int i = 1; i < n; ++i)
{
M[i]=max(M[i-1],M[i]);
}
v c(n);
c[0]=a[0]*b[0];
for (int i = 1; i < n; ++i)
{
c[i]=max(c[i-1],M[i]*b[i]);
}
for (int i = 0; i < n; ++i)
{
cout<<c[i]<<endl;
}
return 0;
} | #include <bits/stdc++.h>
#define lli long long int
using namespace std;
#define mod 1000000007
#define MOD 1000000037
#define mod1 998244353
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define INF 2147483607
#define common cout << "Case #" << w+1 << ": "
int main()
{
int n;
cin >> n;
char aa,ab,ba,bb;
cin >> aa >> ab >> ba >> bb;
if(ab=='A')
{
if(aa=='A')
cout << "1" << endl;
else
{
if(ba=='A')
{
vector<int>v(1005);
v[2]=1;v[3]=1;
for(int i=4;i<=n;i++)
v[i]=(v[i-1]*1LL+v[i-2])%mod;
cout << v[n] << endl;
}
else
{
int p=max(0,n-3);
int ans=1;
for(int i=0;i<p;i++)
ans=(ans*1LL*2)%mod;
cout << ans << endl;
}
}
}
else
{
if(bb=='B')
cout << "1" << endl;
else
{
if(ba=='B')
{
vector<int>v(1005);
v[2]=1;v[3]=1;
for(int i=4;i<=n;i++)
v[i]=(v[i-1]*1LL+v[i-2])%mod;
cout << v[n] << endl;
}
else
{
int p=max(0,n-3);
int ans=1;
for(int i=0;i<p;i++)
ans=(ans*1LL*2)%mod;
cout << ans << endl;
}
}
}
} |
//:::: Alien :::://
// Muhammad Eid //
#include <bits/stdc++.h>
using namespace std;
void Mo35() {
// ios_base::sync_with_stdio(false);
// cin.tie(nullptr);
// cout.tie(nullptr);
#ifndef ONLINE_JUDGE
freopen("input.txt", "rt", stdin);
freopen("output.txt", "wt", stdout);
#endif
}
int main() { Mo35();
string s;
cin >> s;
int sz = s.size();
for (int i = 1; i < sz; i += 2) {
if (islower(s[i])) {
cout << "No";
return 0;
}
}
for (int i = 0; i < sz; i += 2) {
if (isupper(s[i])) {
cout << "No";
return 0;
}
}
cout << "Yes";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define REP(i, m, n) for (int i = m; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define dup(x, y) (((x) + (y)-1) / (y))
#define PI 3.14159265359
typedef long long LL;
const LL MOD = 1e9 + 7;
const LL LLINF = 1LL << 60;
const int INF = 1 << 30;
template <class T>
inline void chmax(T &a, T b) {
if (a < b) {
a = b;
}
}
template <class T>
inline void chmin(T &a, T b) {
if (a > b) {
a = b;
}
}
template <class T>
inline T lcm(T x, T y) {
return x / __gcd(x, y) * y;
}
template <class T>
inline void print_vector(vector<T> vec) {
for (int i = 0; i < vec.size(); i++) {
cout << vec[i] << " ";
}
cout << endl;
}
template <class T>
inline T mpower(T x, T n) {
if (n == 0) {
return 1;
} else if (n % 2 == 0) {
return mpower(x * x % MOD, n / 2);
} else {
return x * mpower(x, n - 1) % MOD;
}
}
template <class T>
inline T q_power(T x, T n) {
if (n == 0) {
return 1;
} else if (n % 2 == 0) {
return q_power(x * x, n / 2);
} else {
return x * q_power(x, n - 1);
}
}
LL mfrac(LL x) {
LL res = 1;
for (LL i = x; i >= 1; i--) {
res *= i;
res %= MOD;
}
return res;
}
int main(void) {
string S;
cin >> S;
for (int i = 1; i <= S.size(); i++)
{
if(i%2 == 1 and ('a' > S[i-1] or S[i-1] > 'z')){
cout << "No" << endl;
return 0;
}
if(i%2 == 0 and ('A' > S[i-1] or S[i-1] > 'Z')){
cout << "No" << endl;
return 0;
}
}
cout << "Yes" << endl;
return 0;
}
|
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr ll LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
IOSetup() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(20);
}
} iosetup;
int main() {
int n; cin >> n;
set<int> st;
int cur = 0;
REP(_, n) {
int p; cin >> p;
st.emplace(p);
while (st.count(cur) == 1) ++cur;
cout << cur << '\n';
}
return 0;
}
| #pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#pragma GCC optimize("unroll-loops")
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define NDEBUG
#define eprintf(...) do {} while (0)
#endif
#include<cassert>
using namespace std;
typedef long long LL;
typedef vector<int> VI;
#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)
template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }
#define rprintf(fmt, begin, end) do { const auto end_rp = (end); auto it_rp = (begin); for (bool sp_rp=0; it_rp!=end_rp; ++it_rp) { if (sp_rp) putchar(' '); else sp_rp = true; printf(fmt, *it_rp); } putchar('\n'); } while(0)
int N;
char S[100011];
void MAIN() {
scanf("%d%s", &N, S);
int ans = -1;
if (S[0] != S[N-1]) {
ans = 1;
} else {
bool f = false;
REP (i, N-1) {
if (S[i] != S[0] && S[i+1] != S[0]) {
f = true;
}
}
if (f) ans = 2;
else ans = -1;
}
printf("%d\n", ans);
}
int main() {
int TC = 1;
// scanf("%d", &TC);
REP (tc, TC) MAIN();
return 0;
}
|
#define _GLIBCXX_DEBUG
#include<iostream>
#include<iomanip>
#include<string>
#include<cstdlib>
#include<vector>
#include<algorithm>
#include<queue>
#include<ctime>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i=0;i<(n);i++)
constexpr double et = 1.8;
int N = 30;
int Si, Sj, Ti, Tj;
int main() {
srand(time(nullptr));
int ti = clock();
ios::sync_with_stdio(false);
cin.tie(nullptr);
vector<double> est_D(N * N * N * N, 0);
vector<double> checked_num(N * N * N * N, 0);
double epsilon = 1;
rep(qi, 1000) {
// init start
cin >> Si >> Sj >> Ti >> Tj;
// init end
// double cur_time = (double)(clock() - ti) / CLOCKS_PER_SEC;
// while (cur_time < et) {
//
// // main loop start
//
//
// // main loop end
//
// cur_time = (double)(clock() - ti) / CLOCKS_PER_SEC;
// }
vector<char> output;
priority_queue<pair<int, int>> Q;
vector<int> color(N * N, 0);
vector<int> dist(N * N, 1 << 28);
vector<int> prev_dir(N * N, '.');
vector<int> prev_u(N * N, -1);
Q.emplace(0, Si * N + Sj);
dist[Si * N + Sj] = 0;
while (!Q.empty()) {
auto [d, u] = Q.top(); Q.pop();
if (-d > dist[u]) continue;
if (color[u] == 1) continue;
color[u] = 1;
if (u == Ti * N + Tj) break;
vector<pair<int, char>> next_vs;
if (u % N > 0) next_vs.emplace_back(u - 1, 'L');
if (u % N < N - 1) next_vs.emplace_back(u + 1, 'R');
if (u / N > 0) next_vs.emplace_back(u - N, 'U');
if (u / N < N - 1) next_vs.emplace_back(u + N, 'D');
for (auto [next_v, dir_char] : next_vs) {
int cur_edge = min(u, next_v) * N * N + max(u, next_v);
int edge_cost = est_D[cur_edge] / (checked_num[cur_edge] + 0.001);
if (color[next_v] == 0 && dist[next_v] > -d + edge_cost) {
dist[next_v] = -d + edge_cost;
prev_dir[next_v] = dir_char;
prev_u[next_v] = u;
Q.emplace(-dist[next_v], next_v);
}
}
}
vector<int> edges;
int U = Ti * N + Tj;
while (U != Si * N + Sj) {
output.emplace_back(prev_dir[U]);
edges.emplace_back(min(U, prev_u[U]) * N * N + max(U, prev_u[U]));
U = prev_u[U];
}
reverse(output.begin(), output.end());
// output start
cout << string(output.begin(), output.end()) << endl;
// output end
int e;
cin >> e;
double new_est_D = (double) e / edges.size();
for (int edge : edges) {
if (edge / (N * N) + 1 == edge % (N * N)) {
int i = edge / N % N;
rep(j, N - 1) {
double factor = 1 / (abs(edge % N - (j + 1)) / 10 + 1);
int edge2 = (i * N + j) * N * N + (i * N + j + 1);
est_D[edge2] += factor * new_est_D;
checked_num[edge2] += factor;
}
} else {
int j = edge % N;
rep(i, N - 1) {
double factor = 1 / (abs(edge / N % N - (i + 1)) / 10 + 1);
int edge2 = (i * N + j) * N * N + (i * N + j + N);
est_D[edge2] += factor * new_est_D;
checked_num[edge2] += factor;
}
}
}
epsilon = epsilon * 0.996;
}
} | #include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define FOR(i,k,n) for(int(i)=(k);(i)<(n);++(i))
#define rep(i,n) FOR(i,0,n)
#define all(v) begin(v),end(v)
#ifdef NDEBUG
#define assert(e) if(!(e)){stod("");}
#endif
#ifdef ONLINE_JUDGE
#define debug(x)
#define debug2(x,y)
#define debug3(x,y,z)
#else
#define debug(x) std::cerr<<#x<<": "<<x<<"\n"
#define debug2(x,y) std::cerr<<#x<<": "<<x<<", "<<#y<<": "<<y<<"\n"
#define debug3(x,y,z) std::cerr<<#x<<": "<<x<<", "<<#y<<": "<<y<<", "<<#z<<": "<<z<<"\n"
#endif
using ll=long long;
using vi=std::vector<int>;
using vvi=std::vector<vi>;
using vll=std::vector<ll>;
using vvll=std::vector<vll>;
template<typename T> using vvec=std::vector<std::vector<T>>;
template<typename T>
auto make_v(size_t sz){return std::vector<T>(sz);}
template<typename T,typename... Ts>
auto make_v(size_t sz,Ts...ts){return std::vector<decltype(make_v<T>(ts...))>(sz,make_v<T>(ts...));}
template<typename T>
void fill_v(T&var,const T&x){var=x;}
template<typename V,typename T>
void fill_v(V&v,const T&x){for(auto&& w:v){fill_v(w,x);}}
template<typename T> std::ostream& operator<<(std::ostream&s,const std::vector<T>&v){
int sz=v.size();s<<"\n";rep(i,sz){s<<v[i];if(i<sz-1){s<<"\t";}}s<<"\n";return s;}
template<typename T> std::ostream& operator<<(std::ostream&s,const std::vector<std::vector<T>>&v){
for(auto&& w:v){s<<w;}return s;}
template<typename T> std::ostream& operator<<(std::ostream&s,const std::deque<T>&v){
int sz=v.size();s<<"\n";rep(i,sz){s<<v[i];if(i<sz-1){s<<"\t";}}s<<"\n";return s;}
template<typename T> std::ostream& operator<<(std::ostream&s,const std::deque<std::deque<T>>&v){
for(auto&& w:v){s<<w;}return s;}
template<typename T> std::ostream& operator<<(std::ostream&s, const std::set<T>&v){
s<<"\n";for(auto&& elm:v){s<<elm<<"\t";}s<<"\n";return s;}
inline void scan(int&a){scanf("%d",&a);}
inline void scan(ll&a){scanf("%lld",&a);}
inline void scan(char&a){scanf(" %c",&a);}
inline void scan(double&a){scanf("%lf",&a);}
template<typename T>
inline void scan(std::vector<T>&v){for(auto&& sv:v){scan(sv);}}
template<typename First,typename...Args>
inline void scan(First&f,Args&...args){scan(f);scan(args...);}
inline void scan(std::string&s){char BUF[3000000];scanf(" %s",BUF);s=std::string(BUF);}
inline void print(int a){printf("%d\n",a);}
inline void print(ll a){printf("%lld\n",a);}
inline void print(double a){printf("%.12f\n",a);}
inline void print(std::string s){std::cout<<s<<"\n";}
using namespace std;
void solve() {
int a, b, c;
scan(a, b, c);
if ((0 <= a and 0 <= b) or c % 2 == 0) {
a = max(a, -a);
b = max(b, -b);
if (a < b) {
print("<");
} else if (a > b) {
print(">");
} else {
print("=");
}
} else if (a < 0 and 0 <= b) {
print("<");
} else if (b < 0 and 0 <= a) {
print(">");
} else {
if (-a < -b) {
print("<");
} else if (-a > -b) {
print(">");
} else {
print("=");
}
}
}
int main() {
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main(){
long n, m, mod = 998244353, ans = 0, mans, a, k, h = 200001;
cin >> n >> m;
long dp[19][h];
bool s[h] = {};
vector<long> v;
for(int i = 2; i < h; i++){
if(s[i] == 0){
v.push_back(i);
for(int j = 2 * i; j < h; j += i) s[j] = 1;
}
}
for(int i = 0; i < 19; i++) dp[i][0] = 0;
for(int i = 1; i < h; i++){
dp[0][i] = 1;
for(int j = 1; j < 19; j++) dp[j][i] = (dp[j - 1][i] + dp[j][i - 1]) % mod;
}
for(int i = 1; i <= m; i++){
mans = 1;
a = i;
for(int j = 0; j < v.size(); j++){
if(sqrt(a) < v[j]){
if(a > 1) mans = mans * dp[1][n] % mod;
break;
}
k = 0;
while(a % v[j] == 0){
k++;
a /= v[j];
}
mans = mans * dp[k][n] % mod;
}
ans = (ans + mans) % mod;
}
cout << ans << endl;
} | #include<bits/stdc++.h>
typedef int64_t i64;
typedef long double f128;
using namespace std;
template<typename T>
void scan(T& n)
{
cin>>n;
return;
}
void scan()
{
return;
}
template<typename T,class... Args>
void scan(T& n,Args&... args)
{
scan(n);
scan(args...);
return;
}
template<typename T>
void scan_array(T start,T end)
{
T now=start;
for(;now!=end;++now)
{
scan(*now);
}
return;
}
template<typename T>
void print(T n)
{
cout<<n;
return;
}
template<typename T>
void println(T n)
{
print(n);
print('\n');
return;
}
template<typename T,class... Args>
void println(T n,Args... args)
{
print(n);
print(' ');
println(args...);
return;
}
template<typename T>
void print_array(T start,T end)
{
T now=start;
print(*now);
++now;
for(;now!=end;++now)
{
print(' ');
print(*now);
}
print('\n');
return;
}
i64 pow_mod(i64 a,i64 n,i64 mod)
{
i64 res=1,now=a;
while(n)
{
if(n&1)
{
res=res*now%mod;
}
now=now*now%mod;
n>>=1;
}
return res;
}
i64 inv_mod(i64 a,i64 mod)
{
return pow_mod(a,mod-2,mod);
}
int main()
{
i64 constexpr mod=1000000007;
i64 H,W;
scan(H,W);
vector<string> S(H);
scan_array(S.begin(),S.end());
i64 K=0;
for(i64 i=0;i<H;++i)
{
for(i64 j=0;j<W;++j)
{
if(S[i][j]=='.')
{
K+=1;
}
}
}
i64 d[H][W],u[H][W],r[H][W],l[H][W];
for(int j=0;j<W;++j)
{
int now=0;
for(int i=0;i<H;++i)
{
now=(S[i][j]=='#')?0:now+1;
d[i][j]=now;
}
now=0;
for(int i=H-1;i>=0;--i)
{
now=(S[i][j]=='#')?0:now+1;
u[i][j]=now;
}
}
for(int i=0;i<H;++i)
{
int now=0;
for(int j=0;j<W;++j)
{
now=(S[i][j]=='#')?0:now+1;
r[i][j]=now;
}
now=0;
for(int j=W-1;j>=0;--j)
{
now=(S[i][j]=='#')?0:now+1;
l[i][j]=now;
}
}
i64 sum=0;
for(int i=0;i<H;++i)
{
for(int j=0;j<W;++j)
{
if(S[i][j]=='.')
{
sum+=pow_mod(2,K-(l[i][j]+r[i][j]+u[i][j]+d[i][j]-3),mod);
}
}
}
sum%=mod;
i64 ans=pow_mod(2,K,mod)*K%mod-sum;
ans=(ans%mod+mod)%mod;
println(ans);
} |
//*****************************************************************************
//* __ ________ __________ __________ __ __ *
//* | | | | ||_________| ||________| || || *
//* | | | | || || || || *
//* | | |________| || || || || *
//* | | |________| || ______ || ______ || || *
//* ___| | | | ||____| | | | ||_____| | | | ||______|| *
//*|______| | | ||______| |_| ||_______| |_| ||______|| *
//* *
//******************************************************************************
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define f(i,a,b) for(ll i=a;i<b;i++)
#define prnt(a) for(auto _:a) {cout<<_<<' ';}cout<<endl;
#define prnt_map(a) for(auto x : a) {cout<<x.first<<' '<<x.second; cout<<endl;}
#define lower(a) transform(a.begin(),a.end(),a.begin(),::tolower)
#define upper(a) transform(a.begin(),a.end(),a.begin(),::toupper)
#define pll pair<ll,ll>
#define int long long
#define umap unordered_map
#define vi vector< long long >
#define vec_read(x) for(auto &i:x) cin>>i;
#define str string
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define pb push_back
#define min_(a) *min_element(a.begin(),a.end())
#define max_(a) *max_element(a.begin(),a.end())
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define deb(x) cout << #x << ' ' << x << endl;
#define se second
#define fi first
#define si size
#define MOD ll(1e9+7)
template<typename... T>
void read(T&... args) {
((cin>>args), ...);
}
template<typename... T>
void print(T... args) { //rvalue reference is new to C++
((cout<<args<<' '), ...);
cout<<endl;
}
int msb(int N) {
return N ? 32 - __builtin_clz(N) : -MOD;
}
//....................................
void solve(){
//WRITE YOUR CODE HERE.....
ld r,x,y; read(r,x,y);
ld dis = (ld)sqrt(pow(x,2)+pow(y,2));
if (dis<r) print(2);
else print(ceil(dis/r));
}
void test_case(){
int testCase;
cin>>testCase;
while(testCase--) solve();
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// test_case();
solve();
return 0;
}
| #include <bits/stdc++.h>
#define fi first
#define se second
#define sz(v) ((int)v.size())
#define all(v) (v).begin(), (v).end()
#define pb push_back
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
template<int mod>
struct modint{
int v;
modint(ll vv=0){s(vv%mod+mod);}
modint& s(int vv){
v=vv<mod?vv:vv-mod;
return *this;
}
modint operator-()const{return modint()-*this;}
modint& operator+=(const modint&rhs){return s(v+rhs.v);}
modint&operator-=(const modint&rhs){return s(v+mod-rhs.v);}
modint&operator*=(const modint&rhs){
v=ll(v)*rhs.v%mod;
return *this;
}
modint&operator/=(const modint&rhs){return *this*=rhs.inv();}
modint operator+(const modint&rhs)const{return modint(*this)+=rhs;}
modint operator-(const modint&rhs)const{return modint(*this)-=rhs;}
modint operator*(const modint&rhs)const{return modint(*this)*=rhs;}
modint operator/(const modint&rhs)const{return modint(*this)/=rhs;}
modint pow(int n)const{
modint res(1),x(*this);
while(n){
if(n&1)res*=x;
x*=x;
n>>=1;
}
return res;
}
modint inv()const{return pow(mod-2);}
/*modint inv()const{
int x,y;
int g=egcd(v,mod,x,y);
assert(g==1);
if(x<0)x+=mod;
return modint(x);
}*/
friend modint operator+(int x,const modint&y){return modint(x)+y;}
friend modint operator-(int x,const modint&y){return modint(x)-y;}
friend modint operator*(int x,const modint&y){return modint(x)*y;}
friend modint operator/(int x,const modint&y){return modint(x)/y;}
friend ostream& operator<<(ostream&os,const modint&m){return os<<m.v;}
friend istream& operator>>(istream&is,modint&m){
ll x;is>>x;
m=modint(x);
return is;
}
bool operator<(const modint&r)const{return v<r.v;}
bool operator==(const modint&r)const{return v==r.v;}
bool operator!=(const modint&r)const{return v!=r.v;}
explicit operator bool()const{return v;}
};
using mint=modint<998244353>;
int n,a[200010];
mint pw[200010];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n;
int i,j;
pw[0]=1;
for(i=1;i<=n;i++)pw[i]=pw[i-1]*2;
for(i=1;i<=n;i++)cin>>a[i];
sort(a+1,a+n+1);
mint ans,sum;
for(i=1;i<=n;i++) {
// a[i]*a[j]*2^(i-j-1)=a[i]*2^i * a[j]*2^(-j-1)
ans+=a[i]*pw[i]*sum+mint(a[i])*mint(a[i]);
sum+=a[i]*pw[i+1].inv();
}
cout<<ans.v;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define P pair<int,int>
ll cnt(ll y,ll x,map<ll,ll> &data){
if(data.count(y)){
return data[y];
}
if(x>=y){
data[y] = x-y;
return (ll) x-y;
}
if(y%2==0){
ll z = min(cnt(y/2,x,data)+1,abs(y-x));
data[y] = z; return z;
}
else{
ll z = min(min(cnt(y+1,x,data)+1,abs(y-x)),cnt(y-1,x,data)+1);
data[y] = z; return z;
}
}
int main(){
long long X,Y;
cin >> X >> Y;
map<ll,ll> data;
cout << cnt(Y,X,data) << endl;
} | /*
Nguyen Duc Anh Phuc_Jacke
*/
#include <limits.h>
#include <iostream>
#include <iomanip>
#include <vector>
#include <queue>
#include <unordered_set>
#include <set>
#include <fstream>
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> plll;
#define fi first
#define se second.first
#define th second.second
map<ll, ll> mp;
ll sol(ll x, ll y)
{
if (mp[x])
return mp[x];
if (x == y)
{
mp[x] = 0;
return 0;
}
if (x / 2 == y && x % 2 == 0 )
{
mp[x] = 1;
return 1;
}
if (x / 2 < y)
{
if (x % 2 == 0)
return mp[x]=min(x - y, y - x / 2 + 1);
else
return mp[x]=min(x - y, min(y - x / 2 + 2, (y - (x / 2 + 1) + 2)));
}
ll le = LLONG_MAX;
ll ri = LLONG_MAX;
if (x % 2 == 0)
{
le = sol(x / 2, y) + 1;
}
else
{
le = sol(x / 2, y) + 2;
ri = sol(x / 2 + 1, y) + 2;
}
return mp[x]=min(le, ri);
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
ll x, y;
cin >> x >> y;
if (x >= y)
{
cout << x - y;
return 0;
}
cout << sol(y, x);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int, int> ii;
int main() {
int t, n;
scanf("%d %d", &t, &n);
ll l = 0, r = 1e14, ans;
while (l <= r) {
ll mid = (l+r)/2, nex = (100*mid + t*mid) / 100;
if (nex-mid < n)
ans = nex+1, l = mid+1;
else
r = mid-1;
}
printf("%lld\n", ans);
return 0;
}
| #include <bits/stdc++.h>
//#include <atcoder/all>
#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)
#define all(x) x.begin(), x.end()
using namespace std;
//using namespace atcoder;
using ll = long long;
using P = pair<ll, ll>;
int main() {
int n, m;
cin >> n >> m;
vector<P> xy(m);
rep(i, m) {
ll x, y;
cin >> x >> y;
xy.at(i) = make_pair(x, y);
}
sort(xy.begin(), xy.end());
vector<set<ll>> xxyy;
ll curX = -1;
int curI = -1;
rep(i, m) {
ll x = xy.at(i).first;
ll y = xy.at(i).second;
if (curX == x) {
xxyy.at(curI).insert(y);
} else {
curI++;
xxyy.emplace_back();
xxyy.at(curI).insert(y);
curX = x;
}
}
set<ll> res;
res.insert(n);
vector<ll> dj = {1, -1};
for (auto &s : xxyy) {
// cerr << "s: " << endl;
// for (auto v : s) {
// cerr << v << ", ";
// }
// cerr << endl;
vector<ll> insertV;
vector<ll> eraseV;
for (auto &ss : s) {
if (res.count(ss) != 0) {
eraseV.push_back(ss);
}
rep(j, dj.size()) {
ll nj = ss + dj[j];
if (nj >= 0 && nj <= n * 2) {
if (res.count(nj) != 0) {
insertV.push_back(ss);
}
}
}
}
for (auto &x : eraseV) {
res.erase(x);
}
for (auto &x : insertV) {
res.insert(x);
}
// cerr << "res: " << endl;
// for (auto v : res) {
// cerr << v << ", ";
// }
// cerr << endl;
}
cout << res.size() << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i,a,b) for(ll i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
#define FORL(i,x) for(int i=head[x];i;i=nxt[i])
#define ALL(a) (a).begin(),(a).end()
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define PB push_back
#define MP make_pair
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(void){
int n,k,m; cin >> n >> k >> m;
int a[n-1];
int sum = 0;
FOR(i,0,n-2) {
cin >> a[i];
sum += a[i];
}
if(n*m-sum<=k) cout << max(0,n*m-sum) << endl;
else cout << -1 << endl;
} | #include<bits/stdc++.h>
#include<chrono>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace std::chrono;
using namespace __gnu_pbds;
#define ll long long int
#define ull unsigned long long int
#define FOR(I,a,b) for(ll I=a;I<b;I++)
#define FORit(it,a) for(auto it=a.begin();it!=a.end();it++)
#define ROF(I,a,b) for(int I=a;I>=b;I--)
#define vec vector
#define vi vec<int>
#define vll vec<ll>
#define pb push_back
#define pp pop_back
#define all(x) x.begin(),x.end()
#define testcases ll tc;cin>>tc;while(tc--)
#define mem(a,k) memset(a,k,sizeof(a))
#define FF first
#define SS second
#define MP(x,y) make_pair(x,y)
#define rt return
#define br break
#define ct continue
#define elif else if
#define ii pair<ll,ll>
#define vecin(a,n,index) for(int I=index;I<n;I++)cin>>a[I]
#define vecout(a,n,index) for(int I=index;I<n;I++)cout<<a[I]<<" ";cout<<endl;
template<class T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<class T> using omset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
// ll mod = 1000000007;
// int max_size = 1000001;
// vi sv(max_size, 1);
// vi primes;
// void sieve();
// ll power(ll a, ll b);
// ll powerMod(ll a, ll b, ll mod);
// ll modIn(ll n);
// vll fact(200001);
ll val(char c) {
if (c >= '0' && c <= '9')
rt (ll)c - '0';
else
rt (ll)c - 'A' + 10;
}
ll toDecimal(string s, ll base) {
ll l = s.length();
ll power = 1;
ll res = 0;
ROF(i, l - 1, 0) {
if (val(s[i]) >= base) {
rt - 1;
}
// cout << power << endl;
if (res > 1e18 - (val(s[i])*power))rt - 1;
res += val(s[i]) * power;
if (power > (1e18 + base - 1) / base && i >= 1)rt - 1;
power = power * base;
}
rt res;
}
bool fun(string x, ll n, ll m) {
if (toDecimal(x, n) != -1 && toDecimal(x, n) <= m )rt true;
rt false;
}
void solve() {
string x;
ll m;
cin >> x >> m;
string k = x;
sort(all(k));
int d = k[k.size() - 1] - 48;
if (x.length() == 1) {
if (x[0] - 48 <= m)cout << "1\n";
else cout << "0\n";
rt;
}
ll L = d + 1, H = 1e18, res = 0;
while (L <= H) {
ll mid = (L + H) / 2;
// cout << res << endl;
if (fun(x, mid, m)) {
res = mid;
L = mid + 1;
}
else H = mid - 1;
}
cout << max(0LL, res - d) << endl;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
// testcases
solve();
}
/*void sieve() {
sv[0] = 0; sv[1] = 0;
for (int i = 2; i * i <= max_size; i++) {
if (sv[i]) {
for (int j = i * i ; j <= max_size; j += i) {
sv[j] = 0;
}
}
}
}*/
/*ll power(ll a , ll b) {
ll res = 1;
while (b) {
if (b % 2) {res *= a; b--;}
else {a *= a; b /= 2;}
}
rt res;
}*/
/*
ll powerMod(ll a, ll b, ll mod) {
ll res = 1;
while (b != 0) {
if (b % 2) {
res = (res % mod * a % mod) % mod;
b--;
}
else {
a = (a % mod * a % mod) % mod;
b /= 2;
}
}
rt res % mod;
}*/
/*
ll modIn(ll n) {rt powerMod(n, mod - 2) % mod;}
*/ |
#include <bits/stdc++.h>
using namespace std;
int main()
{
char arr[3];
for (int i=0; i<3; i++) {
cin >> arr[i];
}
char temp = arr[0];
for (int i=0; i<2; i++) {
arr[i] = arr[i+1];
}
arr[2] = temp;
for (int i=0; i<3; i++) {
cout << arr[i];
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
const int MOD=1e9+7;
void test_case(){
int n;cin>>n;
vector<ll> arr(n),brr(n),ans(n);
for(ll &x:arr)cin>>x;
for(ll &x:brr)cin>>x;
ll amax=-1;
ll mx=-1;
for(int i=0;i<n;i++){
amax=max(amax,arr[i]);
mx=max(amax*brr[i],mx);
ans[i]=mx;
}
for(ll x:ans)
cout<<x<<'\n';
// cout<<'\n';
}
int main(int argc, char** argv){
// ios_base::sync_with_stdio(false);
// cin.tie(NULL);
int t;
t=1;
// cin>>t;
while(t--){
test_case();
}
} |
//git pull --rebase origin master
#include<bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(i,n) for(int i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define ll long long
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define ss(s) scanf("%s",s)
#define pi(x) printf("%d\n",x)
#define pl(x) printf("%lld\n",x)
#define ps(s) printf("%s\n",s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define wi(t) int t;cin>>t;while(t--)
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) {
uniform_int_distribution<int> uid(0,lim-1);
return uid(rang);
}
int mpow(int base, int exp);
void ipgraph(int n, int m);
void dfs(int u, int par);
const int mod = 1000000007;
const int N = 3e5, M = N;
//=======================
vi adj[N];
vi vis(N);
ll int gcd(ll int a, ll int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int tc=1;
void solve()
{
ll int n,m;
cin>>n>>m;
vl ar(m);
fo(i,m) cin>>ar[i];
sortall(ar);
ll int ans=LONG_MAX;
ll int bck=0;
for(int i=0;i<m;i++)
{
if((ar[i]-bck-1)>0)
ans=min(ans,ar[i]-bck-1);
bck=ar[i];
}
if((n-bck)>0)
ans=min(ans,n-bck);
ll int ok=0;
bck=0;
// deb(ans);
for(int i=0;i<m;i++)
{
if((ar[i]-bck-1)>0)
{
ll int gg=ar[i]-bck-1;
ok+=gg/ans;
if(gg%ans!=0) ok++;
}
bck=ar[i];
}
if((n-bck)>0)
{
ll int gg=n-bck;
ok+=gg/ans;
if(gg%ans!=0) ok++;
}
cout<<ok<<"\n";
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
srand(chrono::high_resolution_clock::now().time_since_epoch().count());
// wi(t)
{
solve();
}
return 0;
}
int mpow(int base, int exp) {
base %= mod;
int result = 1;
while (exp > 0) {
if (exp & 1) result = ((ll)result * base) % mod;
base = ((ll)base * base) % mod;
exp >>= 1;
}
return result;
}
void ipgraph(int n, int m){
int i, u, v;
while(m--){
cin>>u>>v;
u--, v--;
adj[u].pb(v);
adj[v].pb(u);
}
}
void dfs(int u, int par){
for(int v:adj[u]){
if (v == par) continue;
dfs(v, u);
}
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using ull = unsigned long long int;
using P = pair<int, int>;
using P3 = pair<int,P>;
using PP = pair<P, P>;
constexpr ll INF = 1LL << 60;
constexpr ll MOD = ll(1e9+7);
constexpr int di[] = {0, 1, 0, -1};
constexpr int dj[] = {1, 0, -1, 0};
constexpr int di8[] = {0, 1, 1, 1, 0, -1, -1, -1};
constexpr int dj8[] = {1, 1, 0, -1, -1, -1, 0, 1};
constexpr double EPS = 1e-10;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
int main(){
int n, m;
cin >> n >> m;
vector<vector<int> > pos(n+1);
for(int i=0;i<n;i++){
int a;
cin >> a;
pos[a].push_back(i);
}
for(int i=0;i<=n;i++){
pos[i].push_back(-1);
pos[i].push_back(n);
sort(pos[i].begin(), pos[i].end());
}
for(int i=0;i<=n;i++){
int k = pos[i].size();
for(int j=0;j<k-1;j++){
if(pos[i][j+1]-pos[i][j] > m){
cout << i << endl;
return 0;
}
}
}
return 0;
} |
#include <bits/stdc++.h>
#define mod 1000000007
using namespace std;
typedef pair<int, long long> P;
long long n;
long long qq[200001];
vector<P> vc[200001];
void dfs(int pt, int par){
for(auto i : vc[pt]){
if(i.first==par) continue;
qq[i.first]=qq[pt]^i.second;
dfs(i.first, pt);
}
}
int main(){
long long a, b ,w;
cin >> n;
for(int i=1;i<n;i++){
cin >> a >> b >> w;
vc[a].push_back(make_pair(b, w));
vc[b].push_back(make_pair(a, w));
}
dfs(1, 0);
__int128 cnt[61]={0}, temp;
for(int i=1;i<=n;i++){
int ptr=0;
temp=qq[i];
while(temp>0){
if(temp%2==1) cnt[ptr]++;
temp/=2;
ptr++;
}
}
__int128 ans=0, mul=1;
for(int i=1;i<n;i++){
int ptr=0;
temp=qq[i];
for(int j=0;j<=60;j++){
if(temp%2==1){
cnt[ptr]--;
ans+=(mul%mod)*(n-i-cnt[ptr])%mod;
}
else{
ans+=(mul%mod)*(cnt[ptr])%mod;
}
temp/=2;
ptr++;
mul*=2;
ans%=mod;
}
mul=1;
}
long long print=ans;
cout<<print<<'\n';
} | //Bismillahirrahmanirrahim
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long lo;
typedef pair< lo,lo > PII;
#define fi first
#define se second
#define mp make_pair
#define endl "\n"
#define pb push_back
#define fio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define FOR for(int i=1;i<=n;i++)
#define mid ((start+end)/2)
#define ort ((bas+son)/2)
const lo inf = 1000000000000000000;
const lo KOK = 100000;
const lo LOG = 30;
const lo li = 500005;
const lo mod = 1000000007;
int n,m,b[li],a[li],k,flag,t,say,vis[li];
int cev;
string s;
vector<int> v[li];
inline void dfs(int node,int ata){
if(vis[node]){flag=1;return ;}
vis[node]=1;
say++;
for(auto go:v[node]){
if(go==ata)continue;
dfs(go,node);
}
}
int main(void){
scanf("%d",&n);
FOR{
int x,y;
scanf("%d %d",&x,&y);
v[x].pb(y);
v[y].pb(x);
}
cev=0;
for(int i=1;i<=400000;i++){
if(vis[i])continue;
flag=0;
say=0;
dfs(i,0);
if(flag==0)cev+=say-1;
else cev+=say;
}
printf("%d\n",cev);
return 0;
}
|
#include <iostream>
using namespace std;
int gcd(int x, int y) { return y == 0 ? x : gcd(y, x % y); }
int main() {
int n;
cin >> n;
int x = 0;
for (int i = 0; i < n; ++i) {
int a;
cin >> a;
x = gcd(x, a);
}
cout << x;
} | #include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define ppb pop_back()
#define ppf pop_front()
#define all(vec) vec.begin(), vec.end()
#define fol(i,a,b) for (int i = (a), _b = (b); i < _b; i++)
#define loop(i,a,b) for (int i = (a), _b = (b); i >= _b; i--)
#define forr(x,arr) for(auto &x : arr)
#define mod 1000000007
#define INF 0x3f3f3f3f3f3f3f3f
#define EPS 1e-7
#define sz(x) ((lli)(x).size())
template <class my>
using vo = vector<my>;
using lli = long long;
using lld = long double;
using ulli = unsigned long long int;
using pll = pair<lli, lli>;
using ttt = pair<lli, pll>;
using vttt = vector<ttt>;
using vll = vector<pll>;
using vl = vector<lli>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using cd = complex<lld>;
const double PI = acos(-1);
#ifndef tejasp
#define trace(...) {}
#define cotra(...) {}
#define end_routine() {}
#define localsys 0
#define endl '\n'
#endif
int white_horseman()
{
lli n, m, k, q, u, v, temp = 0, ans = 0;
cin >> n;
int arr[n], val = 0;
fol(i,0,n) cin >> arr[i];
fol(i,0,n) val = __gcd(val, arr[i]);
cout << val << endl;
return 0;
}
signed main()
{
#ifdef tejasp
freopen("input.txt", "rt", stdin);
freopen("output.txt", "wt", stdout);
#endif
ios_base::sync_with_stdio(false); cin.tie(NULL);
cout << fixed << setprecision(10);
int t = 1;
//cin >> t;
fol(i,0,t) {
//cout << "Case #" << i + 1 << ": ";
white_horseman();
}
//end_routine(); if(localsys) system("pause");
} |
#pragma region head
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
template <class T>
using vv = vector<vector<T>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rrepi(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bit(n) (1LL << (n))
template <class T>
inline bool chmax(T& a, const T& b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool chmin(T& a, const T& b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
const int INF = 1002003004;
const ll LINF = 1002003004005006007ll;
struct preprocess {
preprocess() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
}
} ____;
#pragma endregion head
#pragma region library
const int MOD = 998244353;
struct mint {
ll x; // typedef long long ll;
mint(ll x = 0) : x((x % MOD + MOD) % MOD) {}
mint operator-() const { return mint(-x); }
mint& operator+=(const mint a) {
if ((x += a.x) >= MOD) x -= MOD;
return *this;
}
mint& operator-=(const mint a) {
if ((x += MOD - a.x) >= MOD) x -= MOD;
return *this;
}
mint& operator*=(const mint a) {
(x *= a.x) %= MOD;
return *this;
}
mint operator+(const mint a) const {
mint res(*this);
return res += a;
}
mint operator-(const mint a) const {
mint res(*this);
return res -= a;
}
mint operator*(const mint a) const {
mint res(*this);
return res *= a;
}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t >> 1);
a *= a;
if (t & 1) a *= *this;
return a;
}
// for prime MOD
mint inv() const {
return pow(MOD - 2);
}
mint& operator/=(const mint a) {
return (*this) *= a.inv();
}
mint operator/(const mint a) const {
mint res(*this);
return res /= a;
}
bool operator==(const mint a) const {
return x == a.x;
}
bool operator!=(const mint a) const {
return x != a.x;
}
friend ostream& operator<<(ostream& os, const mint& value) {
os << value.x;
return os;
}
friend istream& operator>>(istream& is, mint& value) {
ll t;
is >> t;
value = mint(t);
return is;
}
};
struct combination {
vector<mint> fact, ifact;
combination(int n) : fact(n + 1), ifact(n + 1) {
assert(n < MOD);
fact[0] = 1;
for (int i = 1; i <= n; ++i) fact[i] = fact[i - 1] * i;
ifact[n] = fact[n].inv();
for (int i = n; i >= 1; --i) ifact[i - 1] = ifact[i] * i;
}
mint operator()(int n, int k) {
if (k < 0 || k > n) return 0;
return fact[n] * ifact[k] * ifact[n - k];
}
};
#pragma endregion library
int main() {
int n, k;
cin >> n >> k;
vector<mint> a(n);
rep(i, n) cin >> a[i];
combination cb(310);
vector<mint> ak(k + 1);
vector<mint> power(k + 1);
vector<mint> anow(n, 1);
rep(i, k + 1) {
rep(j, n) {
ak[i] += anow[j] * cb.ifact[i];
}
rep(j, n) {
anow[j] *= a[j];
}
}
rep(i, n) {
mint now = a[i] * 2;
repi(j, 1, k + 1) {
power[j] += now;
now *= a[i] * 2;
}
}
mint div2 = (mint)1 / 2;
repi(x, 1, k + 1) {
mint ans = 0;
rep(j, x + 1) {
ans += ak[j] * ak[x - j];
}
ans *= cb.fact[x];
ans -= power[x];
ans *= div2;
cout << ans << '\n';
}
} | #include<bits/stdc++.h>
using namespace std;
const int N=100005;
const long long MOD=998244353;
const long long INV=499122177;
int n,k;
long long p[N],s[N],f[N],inv[N];
long long ksm(long long a,int b)
{
long long res=1;
while(b)
{
if(b&1) res=res*a%MOD;
a=a*a%MOD,b/=2;
}
return res;
}
long long C(int a,int b)
{
return f[a]*inv[b]%MOD*inv[a-b]%MOD;
}
int main()
{
scanf("%d%d",&n,&k);
f[0]=p[0]=inv[0]=1,s[0]=n;
for(int i=1;i<=n;i++)
{
long long a,p;
scanf("%d",&a),s[1]=(s[1]+(p=a))%MOD;
for(int j=2;j<=k;j++) s[j]=(s[j]+(p=p*a%MOD))%MOD;
}
// for(int i=1;i<=k;i++) cout<<i<<' '<<s[i]<<endl;
for(int i=1;i<=k;i++) f[i]=f[i-1]*i%MOD,p[i]=p[i-1]*2%MOD;
inv[k]=ksm(f[k],MOD-2);
for(int i=k;i>=1;i--) inv[i-1]=inv[i]*i%MOD;
for(int i=1;i<=k;i++)
{
long long ans=0;
// cout<<i<<endl;
for(int j=0;j<=i;j++)
{
ans=(ans+s[j]*s[i-j]%MOD*C(i,j)%MOD);
// cout<<i<<' '<<j<<' '<<C(i,j)<<endl;
}
ans=(ans-s[i]*p[i]%MOD+MOD)%MOD;
ans=ans*INV%MOD;
printf("%lld\n",ans);
}
} |
Subsets and Splits