code_file1
stringlengths 87
4k
| code_file2
stringlengths 85
4k
|
---|---|
#include <cstdio>
#include <cstring>
#define N 110
#define mo 998244353
using namespace std;
int f[N][5010];
int n, w[N];
int sum;
int jc[N];
int ans;
int main(){
// freopen("funny.in", "r", stdin);
scanf("%d", &n);
for (int i=1; i<=n; i++) scanf("%d", &w[i]), sum+=w[i];
jc[0]=1;
for (int i=1; i<=n; i++) jc[i]=1ll*jc[i-1]*i%mo;
if (sum%2==1){
printf("%d\n", 0);
return 0;
}
int sx=sum/2;
f[0][0]=1;
for (int i=1; i<=n; i++){
for (int j=n; j; j--){
for (int k=w[i]; k<=sx; k++){
(f[j][k]+=f[j-1][k-w[i]])%=mo;
}
}
}
for (int i=1; i<=n; i++){
(ans+=1ll*jc[i]*jc[n-i]%mo*f[i][sx]%mo)%=mo;
}
printf("%d\n", ans);
return 0;
} | #include<map>
#include<set>
#include<list>
#include<ctime>
#include<cmath>
#include<deque>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<bitset>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<complex>
#include<iostream>
#include<algorithm>
#define rep(i,s,t) for(register int i=s;i<=t;++i)
#define _rep(i,s,t) for(register int i=s;i>=t;--i)
#define Rep(i,s,t) for(register int i=s;i<t;++i)
#define go(x) for(register int e=las[x];e;e=nxt[e])
#define re register
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define is insert
#define es erase
#define pii pair<int,int>
#define ms(f,x) memset(f,x,sizeof f)
#define mc(f,x) memcpy(f,x,sizeof f)
#define open(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout)
#define gi(x) read(x)
#define gii(x,y) read(x),read(y)
#define giii(x,y,z) read(x),read(y),read(z)
namespace IO{
#define gc getchar()
#define pc(x) putchar(x)
template<typename T>inline void read(T &x){
x=0;int f=1;char ch=gc;while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=gc;}
while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=gc;x*=f;return;
}
template<typename T>inline void write(T x=0){
T wr[51];wr[0]=0;if(x<0)pc('-'),x=-x;if(!x)pc(48);
while(x)wr[++wr[0]]=x%10,x/=10;while(wr[0])pc(48+wr[wr[0]--]);
putchar('\n');return;
}
}
using IO::read;
using IO::write;
typedef long long ll;
typedef double db;
typedef long double ld;
typedef unsigned long long ull;
using namespace std;
const int N=111,mod=998244353;
int f[N][N*N];
int n,x,sum,fac[N];
void inc(int &x,int y){
x+=y;
if(x>=mod)
x-=mod;
}
int main(){
gi(n);
f[0][0]=1;
fac[0]=1;
rep(i,1,n)
fac[i]=1ll*fac[i-1]*i%mod;
rep(i,1,n){
gi(x);
sum+=x;
_rep(k,i,1)
_rep(j,sum,x)
inc(f[k][j],f[k-1][j-x]);
}
if(sum&1){
puts("0");
}
else{
int ans=0;
rep(i,0,n)
ans=(ans+1ll*f[i][sum/2]*fac[i]%mod*fac[n-i])%mod;
cout<<ans<<endl;
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
int n,val[100005][10],init[10];
bool life[35][6];
bool ok(int x)
{
for(int i=1;i<init[5];i++)
for(int j=1;j<=3;j++)
life[i][j]=0;
int now;
for(int i=1;i<=n;i++)
{
now=0;
for(int j=0;j<5;j++)
{
if(val[i][j]>=x)
now+=init[j];
}
life[now][1]=1;
for(int j=0;j<init[5];j++)
for(int k=1;k<=2;k++)
life[j|now][k+1]|=life[j][k];
}
return life[init[5]-1][3];
}
int main()
{
init[0]=1;
for(int i=1;i<=5;i++)
init[i]=init[i-1]*2;
scanf("%d",&n);
for(int i=1;i<=n;i++)
for(int j=0;j<5;j++)
scanf("%d",&val[i][j]);
int l=1,r=1e9,mid;
life[0][1]=life[0][2]=life[0][3]=1;
while(l!=r)
{
mid=(l+r+1)/2;
if(ok(mid))
l=mid;
else
r=mid-1;
}
printf("%d\n",l);
return 0;
} | #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;
vector<vl> X(3030,vl(5,0));
bool check(int t){
set<int> s;
rep(i,n) {
int bit=0;
fore(j,X[i]){
bit<<=1; bit|=(j>=t);
}
s.insert(bit);
}
fore(x,s) fore(y,s) fore(z,s){
if((x|y|z)==(1<<5)-1) return true;
}
return false;
}
int main() {
fio(); cin>>n;
rep(i,n) rep(j,5) cin>>X[i][j];
ll ok=0,ng=1LL<<31;
while(ng-ok>1){
ll mid=(ok+ng)/2;
if(check(mid)) ok=mid;
else ng=mid;
}
print(ok);
} |
#include <bits/stdc++.h>
#define lc (o<<1)
#define rc ((o<<1)|1)
#define PB push_back
#define MK make_pair
using namespace std;
#define DebugP(x) cout << "Line" << __LINE__ << " " << #x << "=" << x << endl
const int maxn = 5000 + 5;
const int modu = 998244353; // 1e9 + 7
const int inf = 0x3f3f3f3f;
const double eps = 1e-5;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
typedef long long LL;
void read(LL &x)
{
x=0;int f=0;char ch=getchar();
while(ch<'0'||ch>'9') {f|=(ch=='-');ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
x=f?-x:x;
return;
}
void read(int &x) { LL y; read(y); x = (int)y; }
void read(char &ch) { char s[3]; scanf("%s", s); ch = s[0]; }
void read(char *s) { scanf("%s", s); }
template<class T, class ...U> void read(T &x, U& ... u) { read(x); read(u...); }
int n, m;
int a[maxn];
LL inv[maxn], c[maxn], f[20][maxn];
int b[20];
int main() {
// freopen("my.txt", "w", stdout);
read(n, m);
inv[1] = 1;
for (int i = 2; i <= n; ++i)
inv[i] = (modu - (modu/i))*inv[modu%i]%modu;
c[0] = 1;
for (int i = 1; i <= n; ++i)
c[i] = (n-i+1)*inv[i]%modu*c[i-1]%modu;
int nb = 0;
while (m) {
b[nb++] = m%2;
m /= 2;
}
memset(f, 0, sizeof(f));
f[nb-1][0] = 1;
int tot = b[nb-1];
for (int i = nb-2; i >= 0; --i) {
for (int j = 0; j <= tot*2 + b[i]; ++j) if (j%2 == 0) {
for (int k = 0; k <= j && k <= n; k += 2)
f[i][j] = (f[i][j] + f[i+1][(j-k)/2]*c[k]) % modu;
}
tot = tot*2 + b[i];
}
printf("%lld\n", f[0][tot]);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define IFOR(i, m, n) for (ll i = n - 1; i >= m; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define FOREACH(x, a) for (auto&(x) : (a))
#define ALL(v) (v).begin(), (v).end()
#define SZ(x) ll(x.size())
int main() {
ll n;
cin >> n;
ll ans = 0;
ll c = 1;
while (c * 1000 <= n) {
c *= 1000;
ans += n - c + 1;
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mxN = 2e5 + 5;
const int INF = 1e18 + 7;
int N;
int stringtointeger(string x)
{
int ans = 0, j = 1;
for (int i = x.size() - 1; i >= 0; i--)
{
ans += (x[i] - '0') * j;
j *= 10;
}
return ans;
}
string integertostring(int x)
{
string ans = "";
while (x > 0)
{
ans += (x % 10) + '0';
x /= 10;
}
reverse(ans.begin(), ans.end());
return ans;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("Input.txt", "r", stdin);
freopen("Output.txt", "w", stdout);
freopen("Error.txt", "w", stderr);
#endif
cin >> N;
if (N <= 10)
cout << 0;
else
{
int ans = 0;
if (N >= 100000000000)
{
ans = 99999;
int start = 100000;
string s = integertostring(start);
s += s;
while (stringtointeger(s) <= N)
{
start++;
ans++;
s = integertostring(start);
s += s;
}
cout << ans;
} else if (N >= 1000000000)
{
ans = 9999;
int start = 10000;
string s = integertostring(start);
s += s;
while (stringtointeger(s) <= N)
{
start++;
ans++;
s = integertostring(start);
s += s;
}
cout << ans;
} else if (N >= 10000000)
{
ans = 999;
int start = 1000;
string s = integertostring(start);
s += s;
while (stringtointeger(s) <= N)
{
start++;
ans++;
s = integertostring(start);
s += s;
}
cout << ans;
} else if (N >= 100000)
{
ans = 99;
int start = 100;
string s = integertostring(start);
s += s;
while (stringtointeger(s) <= N)
{
start++;
ans++;
s = integertostring(start);
s += s;
}
cout << ans;
} else if (N >= 1000)
{
ans = 9;
int start = 10;
string s = integertostring(start);
s += s;
while (stringtointeger(s) <= N)
{
start++;
ans++;
s = integertostring(start);
s += s;
}
cout << ans;
} else if (N >= 10)
{
ans = 0;
int start = 1;
string s = integertostring(start);
s += s;
while (stringtointeger(s) <= N)
{
start++;
ans++;
s = integertostring(start);
s += s;
}
cout << ans;
}
}
}
//20-03-2021 18:10:04 | #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() {
ll n; cin >> n;
int ans = 0;
for (int x = 1; ; x++) {
if (stoll(to_string(x) + to_string(x)) > n) {
cout << x - 1 << endl;
return 0;
}
}
} |
#include<bits/stdc++.h>
#define For(i,j,k) for(int i=j;i<=k;++i)
#define Dow(i,j,k) for(int i=k;i>=j;--i)
#define ll long long
#define pb push_back
#define fir first
#define sec second
#define pb push_back
#define pa pair<int,int>
#define mk make_pair
using namespace std;
inline ll read()
{
ll t=0,dp=1;char c=getchar();
while(!isdigit(c)) {if(c=='-') dp=-1;c=getchar();}
while(isdigit(c)) t=t*10+c-48,c=getchar();
return t*dp;
}
inline void write(ll x){if(x<0) {putchar('-');x=-x;} if(x>=10) write(x/10);putchar(x%10+48);}
inline void writeln(ll x){write(x);puts("");}
inline void write_p(ll x){write(x);putchar(' ');}
int main()
{
ll N=read();
ll f[233];
ll x=0,y=0;
f[0]=1;f[1]=2;
int tot=1;
For(i,2,100)
{
f[i]=f[i-1]+f[i-2];
if(f[i]>N) break;
tot=i;
}
int top=0;
int q[233];
Dow(i,0,tot)
{
if(f[i]<=N) q[++top]=i,N-=f[i];
}
reverse(q+1,q+top+1);
int now=3;
int tep=1;
int ans[233],top_ans=0;
For(i,1,tot+1)
{
ans[++top_ans]=now;
if(now==3) x=x+y;
if(now==4) y=x+y;
if(q[tep]==i-1)
{
if(now==4) ans[++top_ans]=1;
else ans[++top_ans]=2;
++tep;
}
now=7-now;
}
writeln(top_ans);
x=y=0;
Dow(i,1,top_ans)
{
writeln(ans[i]);
if(ans[i]==1) x++;
if(ans[i]==2) y++;
if(ans[i]==3) x=x+y;
if(ans[i]==4) y=x+y;
}
// cout<<tot<<' '<<f[tot]<<endl;
// cout<<x<<"=="<<y<<endl;
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
-- Benq
*/ | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define debug(x) cerr << #x << " : " << x << endl;
using namespace std;
using ll = long long;
int main() {
int n;
cin >> n;
vector<int> t(n);
vector<ll> l(n);
vector<ll> r(n);
rep(i,n) cin >> t[i] >> l[i] >> r[i];
vector<pair<double, double>> h(n);
rep(i,n) {
if(t[i]==1 || t[i]==2) {
h[i].first = l[i];
} else {
h[i].first = l[i] + 0.1;
}
if(t[i]==1 || t[i]==3) {
h[i].second = r[i];
} else {
h[i].second = r[i] - 0.1;
}
}
//debug(h[0].first);
//debug(h[0].second);
int ans=0;
rep(i,n-1) {
for(int j=i+1; j<n; j++) {
if(h[i].first <= h[j].first && h[j].first <= h[i].second) {
ans++; //debug(i); debug(j);
}
else if(h[i].first <= h[j].second && h[j].second <= h[i].second) {
ans++; //debug(i); debug(j);
}
else if(h[j].first <= h[i].first && h[i].first <= h[j].second) {
ans++; //debug(i); debug(j);
}
else if(h[j].first <= h[i].second && h[i].second <= h[j].second) {
ans++; //debug(i); debug(j);
}
}
}
cout << ans << endl;
} |
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
struct edge{
long long v, t, k;
};
const long long N = 1e5 + 10;
const long long INF = 1e18;
vector<edge> node[N];
long long n, m, x, y, st[N];
long long d[N];
long long dijkstra() {
for (long long i = 1; i <= n; i++) d[i] = INF;
d[x] = 0;
priority_queue<pair<long long, long long>> q;
q.push({0, x});
while (!q.empty()) {
long long u = q.top().second; q.pop();
if (st[u]) continue;
st[u] = 1;
for (auto e : node[u]) {
long long v = e.v, t = e.t, k = e.k;
if (d[u] % k == 0) {
if (d[u] + t < d[v]) {
d[v] = d[u] + t;
q.push({-d[v], v});
}
} else {
if (d[u] + k - (d[u] % k) + t < d[v]) {
d[v] = d[u] + (k - d[u] % k) + t;
q.push({-d[v], v});
}
}
}
}
if (d[y] == 1e18) return -1;
return d[y];
}
int main() {
cin >> n >> m >> x >> y;
while (m--) {
long long u, v, t, k;
cin >> u >> v >> t >> k;
node[u].push_back({v, t, k});
node[v].push_back({u, t, k});
}
cout << dijkstra() << '\n';
return 0;
} | /**
* author : UshanGhosh
* created : 2021-06-27 12:02:50
**/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define vii vector<int>
#define vcc vector<char>
#define pll pair<long long, long long>
#define mem memset
#define sof sizeof
#define co1 __builtin_popcountll
#define PB push_back
#define UB upper_bound
#define LB lower_bound
#define MP make_pair
#define TS to_string
#define F first
#define S second
#define pi acos(-1)
#define mod (long long)1000000007
#define inf (long long)1e18
#define endl '\n'
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int t=1;
while(t--){
long long a,b,c,d,e,f,i,j,k,m,n,o,x,y;
cin >> a >> b >> c >> d;
ll p1 = a, p2 = 0;
for(i=1; i<=100005; i++) {
p1 += b;
p2 += c;
ll div = (p1 - 1) / p2 + 1;
if(div <= d) {
cout << i << "\n";
return 0;
}
}
cout << -1 << "\n";
}
} |
#include <bits/stdc++.h>
#include <ctime>
#include <cstdlib>
#define int long long
#define ii pair<int, int>
#define pb push_back
#define mp make_pair
#define MOD 1000000009
#define E13 10000000000000
#define DUMP(a) \
do { std::cout << "[" << #a " = " << (a) << "]" << std::endl; } while(false)
#define d0(x) cout<<(x)<<" "
#define d1(x) cout<<(x)<<endl
#define edge pair<int, pair<int, int> >
#define REP0(i, n) for (int i=0; i<n; ++i)
#define REP1(i, n) for (int i=1; i<=n; ++i)
#define d2(x,y) cout<<(x)<<" "<<(y)<<endl
#define d3(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl
#define d4(a,b,c,d) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<endl
#define d5(a,b,c,d,e) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<" "<<(e)<<endl
#define d6(a,b,c,d,e,f) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<" "<<(e)<<" "<<(f)<<endl
using namespace std;
template<class T> ostream& operator<<(ostream &os, vector<T> V) {
os << "[ ";
for(auto v : V) os << v << " ";
return os << "]";
}
template<class T> ostream& operator<<(ostream &os, set<T> S){
os << "{ ";
for(auto s:S) os<<s<<" ";
return os<<"}";
}
template<class L, class R> ostream& operator<<(ostream &os, pair<L,R> P) {
return os << "(" << P.first << "," << P.second << ")";
}
template<class L, class R> ostream& operator<<(ostream &os, map<L,R> M) {
os << "{ ";
for(auto m:M) os<<"("<<m.F<<":"<<m.S<<") ";
return os<<"}";
}
signed main(){
int n; cin >> n;
vector<int> value(n+1, 1);
for (int i=1; i<=n; ++i){
for (int j=2*i; j<=n; j+=i){
value[j] = max(value[j], value[i]+1);
}
}
for (int i=1; i<=n; ++i){
cout << value[i] << " ";
}
cout << endl;
return 0;
} | #include<bits/stdc++.h>
#define fastIO std::ios::sync_with_stdio(false);cin.tie(NULL)
using namespace std;
#define F first
#define S second
const int N = 100001;
vector<int> primes;
int isprime[N] = {0};
void sieve()
{
isprime[0] = 1;
isprime[1] = 1;
primes.push_back(2);
for(int i=4 ; i<N ; i+=2)
isprime[i] = 1;
for(int i=3 ; i*i<N ; i+=2)
{
if(isprime[i]==0)
{
primes.push_back(i);
for(int j=i*i ; j<N ; j+=i)
isprime[j] = 1;
}
}
}
vector<pair<int , int> > retpf(int x)
{
vector<pair<int , int> > ans;
int cnt = 0;
for(int i=0 ; i<primes.size() && (primes[i]*primes[i]<=x) ; i++)
{
if(x%primes[i]==0)
{
cnt = 0;
while(x%primes[i]==0)
{
cnt++;
x/=primes[i];
}
ans.push_back(make_pair(primes[i] , cnt));
}
if(isprime[x]==0)
{
ans.push_back(make_pair(x , 1));
break;
}
}
return ans;
}
int main()
{
fastIO;
sieve();
int n;
cin>>n;
vector<int> dp(n+1 , 1);
for(int i=1 ; i<=n ; i++)
{
if(isprime[i]==0)
dp[i] = 2;
else
{
int ctr = 0;
vector<pair<int , int> > pf = retpf(i);
for(int j=0 ; j<pf.size() ; j++)
{
if(pf[j].F!=1 && pf[j].F!=i)
{
ctr += pf[j].S;
}
}
dp[i] = ctr+1;
}
}
for(int i=1 ; i<=n ; i++)
cout<<dp[i]<<" ";
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define absdiff(a,b) (a>b)?a-b:b-a
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define fast ios_base::sync_with_stdio(false);cin.tie(0);
#define MOD 1000000007
#define ll_MAX 1922337203685912
#define endl "\n"
ll fast_expo(ll x,ll p){
if(p==0) return 1;
else if(p%2==0){ ll t=fast_expo(x,p/2)%MOD;return (t*t)%MOD;}
else return (x*(fast_expo(x,p-1))%MOD)%MOD;}
ll invMod(ll num){
return fast_expo(num, MOD - 2)%MOD;
}
const ll N = 1e2 + 5;
ll dp[N][N][N];
int main(){
fast
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll t_c; t_c = 1;
// cin >> t_c;
for(ll t_i = 0; t_i < t_c; ++t_i){
ll n, x; cin >> n >> x;
ll arr[n + 1]{};
memset(dp, -1, sizeof(dp));
for(ll i = 1; i <= n; ++i)
cin >> arr[i];
for(ll i = 1; i <= n; ++i){ // final number of elements
dp[i][0][0] = 0;
for(ll j = 1; j <= n; ++j){ // current index of element
for(ll k = j; k >= 0; --k){ // current size of elements
for(ll m = 0; m < i; ++m){ // current mod value
if(dp[i][k][m] == -1)
continue;
ll val = dp[i][k][m] + arr[j];
dp[i][k + 1][val%i] = max(dp[i][k + 1][val%i], val);
}
}
}
}
ll ans = LLONG_MAX;
for(ll i = 1; i <= n; ++i){
ll val = x%i;
if(dp[i][i][val] == -1)
continue;
ll diff = (x - dp[i][i][val])/i;
ans = min(ans, diff);
}
cout << ans;
}
return 0;
}
| #include<queue>
#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<string>
#include<limits.h>
#include<utility>
#include<vector>
#include<tuple>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define rep(i,n) for(ll i=0;i<n;i++)
#define ALL(A) A.begin(),A.end()
struct UnionFind{
vector<ll> par;
UnionFind(ll N):par(N){
rep(i,N){
par[i]=i;
}
}
ll root(ll x){
if(par[x]==x)return x;
return par[x]=root(par[x]);
}
void unite(ll x,ll y){
ll rx=root(x);
ll ry=root(y);
if(rx==ry){
return;
}
par[rx]=ry;
}
bool same(ll x, ll y){
ll rx=root(x);
ll ry=root(y);
return rx==ry;
}
};
long long GCD(long long a, long long b) {
if (b == 0) return a;
else return GCD(b, a % b);
}
long long extGCD(long long a, long long b, long long &x, long long &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
long long d = extGCD(b, a%b, y, x);
y -= a/b * x;
return d;
}
int main(){
ll n,x;
cin>>n>>x;
vector<ll> a(n);
rep(i,n)cin>>a[i];
ll ans=LLONG_MAX;
rep(i,n+1){
if(i==0)continue;
// i n mod
vector<vector<vector<ll> > > dp(i+1,vector<vector<ll> > (n+1,vector<ll>(i,-1)));
dp[0][0][0]=0;
rep(j,i){
rep(k,n){
//cout<<i<<j<<k<<endl;
rep(l,i){
//if(i==3&&j==3&&k==3)cout<<" "<<dp[j][k][l]<<endl;
dp[j][k+1][l]=max(dp[j][k][l],dp[j][k+1][l]);
if(dp[j][k][l]!=-1){
//cout<<i<<" "<<j<<" "<<k<<" "<<l<<" "<<dp[j][k][l]<<endl;
dp[j+1][k+1][((l+a[k])%i)]=max(dp[j][k][l]+a[k],dp[j+1][k+1][((l+a[k])%i)]);
//cout<<dp[j+1][k+1][((l+a[k])%i)]<<endl;
//cout<<((l-a[m]+i)%i+i)%i<<" "<<dp[j][k][l]<<" "<<dp[j+1][m+1][((l-a[m]+i)%i+i)%i]<<endl;
}
}
}
}
rep(k,n){
//cout<<i<<j<<k<<endl;
rep(l,i){
//if(i==3&&j==3&&k==3)cout<<" "<<dp[j][k][l]<<endl;
dp[i][k+1][l]=max(dp[i][k][l],dp[i][k+1][l]);
}
}
//rep(j,n+1)cout<<j<<dp[i][j][x%i]<<endl;
//rep(j,n+1){
if(dp[i][n][x%i]==-1)continue;
//cout<<i<<" "<<dp[i][j][0]<<endl;
ans=min(ans,(x-dp[i][n][x%i])/i);
//cout<<i<<" "<<ans<<" "<<dp[i][n][x%i] <<endl;
}
cout<<ans<<endl;
}
|
#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>;
const int mod = 1000000007;
struct mint {
ll x; // typedef long long ll;
mint(ll x=0):x((x%mod+mod)%mod){}
mint operator-() const { return mint(-x);}
mint& operator+=(const mint a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint a) {
if ((x += mod-a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
mint operator+(const mint a) const { return mint(*this) += a;}
mint operator-(const mint a) const { return mint(*this) -= a;}
mint operator*(const mint a) const { return mint(*this) *= a;}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t>>1);
a *= a;
if (t&1) a *= *this;
return a;
}
// for prime mod
mint inv() const { return pow(mod-2);}
mint& operator/=(const mint a) { return *this *= a.inv();}
mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int h, w;
cin >> h >> w;
vector<string> f(h);
rep(i,h) cin >> f[i];
vector<vector<int>> sum(h, vector<int>(w));
rep(i,h){
int tmp, l;
l = -1; tmp = 0;
rep(j,w){
if(f[i][j] == '.'){
if(l == -1) l = j;
tmp++;
}
else{
if(l == -1) continue;
for(int k = l; k < j; k++){
sum[i][k] += tmp;
}
tmp = 0;
l = -1;
}
}
if(l != -1){
for(int k = l; k <= w-1; k++) sum[i][k] = tmp;
}
}
/*rep(i,h){
rep(j,w) cout << sum[i][j] << ' ';
cout << endl;
}*/
rep(j,w){
int tmp, l;
l = -1, tmp = 0;
rep(i,h){
if(f[i][j] == '.'){
if(l == -1) l = i;
tmp++;
}
else{
if(l == -1) continue;
for(int k = l; k < i; k++){
sum[k][j] += tmp;
}
//if(j == 0) cout << tmp << ' ' << l << ' ' << endl;
tmp = 0;
l = -1;
}
}
if(l != -1){
for(int k = l; k < h; k++){
sum[k][j] += tmp;
}
}
}
/*
rep(i,h){
rep(j,w) cout << sum[i][j] << ' ';
cout << endl;
}
*/
vector<mint> p(4000001);
p[0] = 1;
rep(i,4000000){
p[i+1] = p[i];
p[i+1] *= 2;
}
int t = 0;
rep(i,h) rep(j,w) if(f[i][j] == '.') t++;
mint ans = 0;
rep(i,h) rep(j,w) if(sum[i][j] > 0) sum[i][j]--;
/*
rep(i,h){
rep(j,w) cout << sum[i][j] << ' ';
cout << endl;
}
*/
rep(i,h) rep(j,w){
if(sum[i][j] > 0){
mint tmp = 0;
tmp += p[sum[i][j]];
tmp -= 1;
tmp *= p[t-sum[i][j]];
ans += tmp;
}
}
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() {
//ๅ
ฅๅ
int n,m;
cin >> n >> m;
vector<int> a(m), b(m);
rep(i,m) cin >> a[i] >> b[i];
int k;
cin >> k;
vector<int> c(k), d(k);
rep(i,k) cin >> c[i] >> d[i];
//ๅบๅ
int ans = 0;
//2^kใใฟใผใใ2้ฒๆฐใง่กจ็พ
int k2 = 1<<k;
rep(s,k2){ //ๆกไปถๅ
จใใฟใผใ็ถฒ็พ
//ๅ
ฅใใ็ฟใ็จๆ
vector<int> dish(n+1);
//่ฉฒๅฝใใ็ฟใซใใผใซใ่ฟฝๅ
rep(i,k){
//ๅณใทใใใใฆๅ
ฅใใๅ ดๆใซใคใณใฏใชใกใณใ
if(s>>i&1) dish[d[i]]++;
else dish[c[i]]++;
}
//ๆกไปถใๆบใใ็ฟใฎๅๆฐใๆฐใใ
//ๆบใใๅๆฐ
int now = 0;
rep(i,m){
//ๆกไปถใฎ็ฟใฏa[i]ใจb[i]
if (dish[a[i]] == 0) continue;
if (dish[b[i]] == 0) continue;
//ไธ่จไปฅๅคใชใๅขใใ
now++;
}
//็พๅจใฎๅ
ฅใใ็ฟใจไปใพใงใฎๆๅคงๅค
ans = max(ans, now);
}
cout << ans << endl;
return 0;
} |
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<string>
#include<stdio.h>
#include<stdlib.h>
#include<float.h>
#include<tuple>
#include<string.h>
#include<iomanip>
#include<stack>
#include<queue>
#include<map>
#include<deque>
#include<math.h>
using namespace std;
#define ll long long
#define rep(i,n) for(ll i=0;i<n;i++)
#define REP(i,n) for(ll i=1;i<=n;i++)
#define ALLOF(c) (c).begin(), (c).end()
#define Pa pair<ll,ll>
const ll mod=1000000007;
const ll modq=998244353;
const ll INF=1e18;
const ll inf=-1;
ll ABS(ll a){return max(a,-a);}
ll modpow(ll a,ll n) {
ll res = 1;
while (n>0) {
if(n&1) res=res*a%mod;
a=a*a%mod;
n>>=1;
}
return res;
}
int main(void){
vector<ll> Fib(1050);
Fib.at(0)=0,Fib.at(1)=1;
rep(i,1010) Fib.at(i+2)=(Fib.at(i+1)+Fib.at(i))%mod;
ll N;
cin>>N;
char caa,cab,cba,cbb;
cin>>caa>>cab>>cba>>cbb;
if(N<4){
cout<<1<<endl;
return 0;
}
if(caa=='A' && cab=='A'){
cout<<1<<endl;
return 0;
}
if(cbb=='B' && cab=='B'){
cout<<1<<endl;
return 0;
}
if(cab=='B' && cbb=='A'){
if(cba=='A'){
cout<<modpow(2,N-3)<<endl;
return 0;
}
cout<<Fib.at(N-1)<<endl;
return 0;
}
if(cba=='B'){
cout<<modpow(2,N-3)<<endl;
return 0;
}
cout<<Fib.at(N-1)<<endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
int main()
{
int N;
cin>>N;
vector<int> A(N);
vector<int> B(N);
for(int i=0;i<N;i++){
cin>>A[i];
}
for(int i=0;i<N;i++){
cin>>B[i];
}
sort(A.begin(),A.end());
sort(B.begin(),B.end());
int x=A[N-1],y=B[0];
//cout<<x<<","<<y<<endl;
if(y-x+1<0){
cout<<"0"<<endl;
}
else cout<<y-x+1<<endl;
}
|
// Problem C
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string.h>
#include <cassert>
#include <algorithm>
#include <set>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#define MAXN 1000
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long int ll;
typedef pair<int, int> ii;
int N;
vector<pair<pair< ll, bool>, string > > v;
int main() {
cin >> N;
ll P1 = 1000000007;
ll P2 = 1000000009;
string s1;
for (int i=0; i<N; i++) {
cin >> s1;
ll x1 = 0;
ll x2 = 0;
if (s1[0] == '!') {
for (int j=1; j<(int)s1.length(); j++) {
x1 = ((x1 * 229) % P1) + s1[j] - 'a' + 1;
x2 = ((x2 * 1187) % P2) + s1[j] - 'a' + 1;
}
v.pb(mp(mp(x1 * x2, false), s1));
}
else {
for (int j=0; j<(int)s1.length(); j++) {
x1 = ((x1 * 229) % P1) + s1[j] - 'a' + 1;
x2 = ((x2 * 1187) % P2) + s1[j] - 'a' + 1;
}
v.pb(mp(mp(x1 * x2, true), s1));
}
}
sort(v.begin(), v.end());
for (int i=0; i<N-1; i++)
if (v[i].first.first == v[i+1].first.first) {
if (v[i].first.second && !v[i+1].first.second) {
cout << v[i].second << endl;
return 0;
}
else if (!v[i].first.second && v[i+1].first.second) {
cout << v[i+1].second << endl;
return 0;
}
}
cout << "satisfiable" << endl;
return 0;
}
| #include<bits/stdc++.h>
int main(){
using namespace std;
unsigned long N;
cin >> N;
vector<unsigned long> A(N), B(N);
for(auto&& i : A)cin >> i;
for(auto&& i : B)cin >> i;
priority_queue<unsigned long> pq;
reverse(begin(A), end(A));
unsigned long ans{0};
for(unsigned long i{0}; i < N; ++i){
pq.emplace(A.back());
pq.emplace(B.back());
A.pop_back();
B.pop_back();
ans += pq.top();
pq.pop();
}
cout << ans << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
typedef long long ll;
int gcd(int a, int b)
{
return b == 0 ? a : gcd(b, a % b);
}
int main()
{
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
int a, b, i, x, y;
cin >> a >> b;
for (i = b - a; i > 0; i--) {
x = a / i;
if (a % i)
x++;
y = b / i;
if (x == y)
continue;
if (gcd(i * x, i * y) == i) {
cout << i << endl;
break;
}
}
return 0;
} | #include <bits/stdc++.h>
#define DB double
#define LL long long
#define MST(a,b) memset((a),(b),sizeof(a))
#ifdef _DEBUG_
#define MRK() cout<<"Mark"<<endl;
#define WRT(x) cout<<#x<<" = "<<(x)<<endl;
#else
#define MRK() ;
#define WRT(x) ;
#endif
#define MAXN 1010000
#define MAXM 2010000
#define MOD 998244353 //1000000007
#define INF 0x3f3f3f3f
#define LLINF 0x3f3f3f3f3f3f3f3f
#define EPS 1e-5
#define _ 0
using namespace std;
string s;
int n;
void init()
{
}
void work()
{
cin>>n;
cin>>s;
if (s.front()!=s.back())
{
cout<<1<<endl;
return ;
}
for (int i=1;i<n-2;i++)
if (s[i]!=s.back() && s[i+1]!=s.back())
{
cout<<2<<endl;
return ;
}
cout<<-1<<endl;
}
int main()
{
#ifdef _DEBUG_
freopen("C:/Users/DELL/Documents/Dev-C++/sample.in","r",stdin);
#endif
init();
int casenum=1;
//scanf("%d",&casenum);
for (int testcase=1;testcase<=casenum;testcase++)
{
#ifdef _DEBUG_
printf("Case #%d:\n",testcase);
#endif
work();
}
return ~~(0^_^0);
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(ll i=0;i<(ll)n;i++)
string S1,S2,S3;
vector<char> chars;
char used[10];
bool check() {
map<char, char> mp;
rep(i,10) if (used[i] != 0) mp[used[i]] = char('0' + i);
string s1,s2,s3;
for(auto &c: S1) s1 += mp[c];
for(auto &c: S2) s2 += mp[c];
for(auto &c: S3) s3 += mp[c];
if(to_string(stoll(s1)) != s1) return false;
if(to_string(stoll(s2)) != s2) return false;
if(to_string(stoll(s3)) != s3) return false;
if(stoll(s1) + stoll(s2) != stoll(s3)) return false;
if(stoll(s1) == 0) return false;
if(stoll(s2) == 0) return false;
if(stoll(s3) == 0) return false;
cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
return true;
}
bool dfs(int di) {
if (di == chars.size()) return check();
rep(i,10) {
if (used[i] == 0) {
used[i] = chars[di];
if (dfs(di + 1)) return true;
used[i] = 0;
}
}
return false;
}
int main() {
cin >> S1 >> S2 >> S3;
rep(i,S1.size()) chars.push_back(S1[i]);
rep(i,S2.size()) chars.push_back(S2[i]);
rep(i,S3.size()) chars.push_back(S3[i]);
sort(chars.begin(),chars.end());
chars.erase(unique(chars.begin(),chars.end()), chars.end());
if (chars.size() > 10) {
cout << "UNSOLVABLE" << endl;
return 0;
}
bool ans = dfs(0);
if(ans == false) cout << "UNSOLVABLE" << endl;
} | // give up
#include <bitset>
#include <iostream>
#include <iomanip>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <valarray>
// alias, using declaration
using llint = long long; using ldouble = long double;
template <class T = llint> using vector = std::vector<T>;
template <class T = llint> using deque = std::deque<T>;
template <class T = llint, class U = llint, class... V> using map = std::map<T, U, V...>;
template <class T = llint, class... U> using set = std::set<T, U...>;
template <class T = llint, class U = llint> using umap = std::unordered_map<T, U>;
template <class T = llint> using uset = std::unordered_set<T>;
template <class T = llint, class U = T> using pair = std::pair<T, U>;
template <class T = llint> using varray = std::valarray<T>;
template <class T = llint, class... U> using pqueue = std::priority_queue<T, U...>;
using std::array; using std::bitset; using std::string; using std::tuple;
// constant
constexpr llint INF = 0x3FFFFFFFFFFFFFFF;
constexpr llint INVALID = 0x8000000000000000;
namespace io {
// in
template <class... T, size_t... S> auto in(std::index_sequence<S...>) {tuple<T...> t; (..., (std::cin >> std::get<S>(t))); return t;}
template <class... T, size_t... S> auto in(std::index_sequence<S...>, size_t n) {tuple<vector<T>...> t{vector<T>(n)...}; for (size_t i = 0; i < n; i++) (..., (std::cin >> std::get<S>(t)[i])); return t;}
template <size_t N, class T = llint, class... U> auto in() {static_assert(N >= 1); if constexpr (N > 1) return in<N - 1, T, T, U...>(); else return in<T, U...>(std::index_sequence_for<T, U...>());}
template <size_t N, class T = llint, class... U> auto in(size_t n) {static_assert(N >= 1); if constexpr (N > 1) return in<N - 1, T, T, U...>(n); else return in<T, U...>(std::index_sequence_for<T, U...>(), n);}
template <size_t N, class T = llint> auto in(size_t n, size_t m) {static_assert(N == 1); vector<vector<T>> v(n, vector<T>(m)); for (auto&& vi : v) for (auto&& vij : vi) std::cin >> vij; return std::make_tuple(v);}
// out
template <class T, class... U> void out(const T& a, const U&... b) {std::cout << a << (sizeof...(b) ? " " : "\n"); if constexpr (sizeof...(b)) out(b...);}
template <class T, class U = const char*> void out(const vector<T>& a, U d = " ") {for (auto&& b : a) std::cout << b << (&b != &a.back() ? d : "\n");}
}
int main() {
// input
auto [N, M] = io::in<2>();
auto [A, B] = io::in<2>(M);
// solve
vector<vector<>> graph(N);
for (llint i = 0; i < M; i++) {
graph[A[i] - 1].emplace_back(B[i] - 1);
graph[B[i] - 1].emplace_back(A[i] - 1);
}
vector<bool> visited(N);
vector<vector<>> s;
auto dfs_s = [&](auto f, llint n) -> void {
visited[n] = true;
s.back().emplace_back(n);
for (auto g : graph[n]) {
if (!visited[g]) {
f(f, g);
}
}
};
for (llint i = 0; i < N; i++) {
if (!visited[i]) {
s.emplace_back();
dfs_s(dfs_s, i);
}
}
vector<> color(N, INVALID);
auto result = 1LL;
auto dfs_p = [&](auto f, auto b, auto e, llint c) -> llint {
for (auto g : graph[*b]) {
if (color[g] == c) return 0;
}
auto bn = std::next(b);
if (bn == e) return 1;
color[*b] = c;
llint r = f(f, bn, e, 0) + f(f, bn, e, 1) + f(f, bn, e, 2);
color[*b] = INVALID;
return r;
};
for (auto t : s) {
result *= dfs_p(dfs_p, t.begin(), t.end(), 0) * 3;
}
// output
io::out(result);
} |
#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {
return '"' + s + '"';
}
string to_string(const char* s) {
return to_string((string) s);
}
string to_string(bool b) {
return (b ? "true" : "false");
}
string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template <size_t N>
string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++) {
res += static_cast<char>('0' + v[i]);
}
return res;
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
#define fi first
#define se second
#define pb push_back
#define mod(n,k) ( ( ((n) % (k)) + (k) ) % (k))
#define forn(i,a,b) for(int i = a; i < b; i++)
#define forr(i,a,b) for(int i = a; i >= b; i--)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
const int mod = 1e9+7;
int sum(int a, int b){return (a+b) % mod;}
int sub(int a, int b){return (a + mod - b) % mod;}
int mul(int a, int b){return (1ll * a * b) % mod;}
int power(int a,int b){
int res = 1;
while(b){
if(b&1)res = mul(res,a);
b >>= 1;
a = mul(a,a);
}
return res;
}
const int maxn = 10;
int N,P,A[maxn];
//int res;
//void dfs(int pos){
//if(pos >= N){
//int act = 0;
//bool can = true;
//forn(i,0,N){
//act += A[i];
//if(act%P == 0){
//can = false;
//break;
//}
//}
//res += (can == true);
//return;
//}
//forn(i,1,P){
//A[pos] = i;
//dfs(pos+1);
//}
//}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> N >> P;
//dfs(0);
//cout << res << '\n';
int res = mul(P-1,power(P-2,N-1));
cout << res << '\n';
return 0;
}
/*
__builtin_mul_overflow(x,y,&x)
-fsplit-stack
*/
| #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <iomanip>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cassert>
#include <complex>
#include <stdio.h>
#include <time.h>
#include <numeric>
#include <random>
#include <unordered_map>
#include <unordered_set>
#define all(a) a.begin(),a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
#define debug(x) cerr << __LINE__ << ' ' << #x << ':' << (x) << '\n'
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef complex<ld> com;
template<class T> using prique = priority_queue<T, vector<T>, greater<T>>;
constexpr int inf = 1000000010;
constexpr ll INF = 1000000000000000010;
constexpr int mod1e9 = 1000000007;
constexpr int mod998 = 998244353;
constexpr ld eps = 1e-12;
constexpr ld pi = 3.141592653589793238;
constexpr ll ten(int n) { return n ? 10 * ten(n - 1) : 1; };
int dx[] = { 1,0,-1,0,1,1,-1,-1 }; int dy[] = { 0,1,0,-1,1,-1,1,-1 };
void fail() { cout << "-1\n"; exit(0); } void no() { cout << "No\n"; exit(0); }
template<class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; }
template<class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; }
template<class T> istream &operator >> (istream &s, vector<T> &v) { for (auto &e : v) s >> e; return s; }
template<class T> ostream &operator << (ostream &s, const vector<T> &v) { for (auto &e : v) s << e << ' '; return s; }
struct fastio {
fastio() {
cin.tie(0); cout.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
cerr << fixed << setprecision(20);
}
}fastio_;
int main() {
ll n;
cin >> n;
string s, t;
cin >> s >> t;
set<int> st;
rep(i, n) if (s[i] == '1') st.insert(i);
st.insert(inf);
ll ans = 0;
rep(i, n) {
if (t[i] == '1') {
int x = *st.lower_bound(i);
if (x == inf) fail();
ans += x - i;
st.erase(x);
}
}
st.erase(inf);
if (st.size() % 2 == 1) fail();
while (!st.empty()) {
int x = *st.begin();
st.erase(x);
int y = *st.begin();
st.erase(y);
ans += y - x;
}
cout << ans << '\n';
} |
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
int main() {
ll n, k;
cin >> n >> k;
vector<P> a(n);
rep(i, n) cin >> a[i].first >> a[i].second;
sort(a.begin(), a.end());
ll now = 0;
rep(i, n) {
if (now + k >= a[i].first) {
k = k - (a[i].first - now) + a[i].second;
now = a[i].first;
} else {
cout << now + k << endl;
return 0;
}
}
cout << now + k << endl;
return 0;
}
| #include <bits/stdc++.h>
#define setIO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define PI 3.14159265358979
#define f first
#define s second
#define ar array
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
typedef pair<long long, long long> pll;
typedef pair<char, int> pci;
typedef pair<int, char> pic;
typedef pair<double, double> pdd;
typedef pair<int, string> pis;
int main() { // Ladder: Do 43
setIO;
int a, b, c;
cin>>a>>b>>c;
if (pow(a,2)+pow(b,2)<pow(c,2)) cout << "Yes\n";
else cout << "No";
} |
#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main()
{
int n;
cin >> n;
vector<ll> a(n), b(n);
rep(i, 0, n) cin >> a[i] >> b[i];
ll sum = 0;
rep(i, 0, n) sum += b[i]*(b[i] + 1)/2 - a[i]*(a[i] - 1)/2;
cout << sum << endl;
return 0;
}
| #include<bits/stdc++.h>
#define ll long long
using namespace std;
int main(){
int n;
cin>>n;
ll a,b;ll sum=0LL;
for(int i=1;i<=n;i++){
cin>>a>>b;
sum+=(a+b)*(b-a+1)/2;
}
cout<<sum;
return 0;
}
|
#include<bits/stdc++.h>
#define int long long
#define rint regester int
const int maxn = 500 + 10;
const int mod = 998244353;
using std::ios;
using std::cin;
using std::cout;
using std::max;
using std::min;
using std::sort;
using std::unique;
using std::lower_bound;
using std::swap;
using std::abs;
using std::acos;
using std::queue;
using std::map;
using std::string;
int read(){int x = 0,f = 1; char ch = getchar(); while(ch < '0' || ch > '9'){if(ch == '-')f = -1; ch = getchar();}while(ch >= '0' && ch <= '9'){x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar();} return x * f;}
int h, w, p[maxn][maxn], ans = 1;
string s[maxn];
signed main(){
h = read(), w = read();
for(int i = 1; i <= h; i++)cin >> s[i];
for(int i = 1; i <= h; i++){
for(int j = 0; j < w; j++){
if(s[i][j] == 'R')p[i][j + 1] = 1;else
if(s[i][j] == 'B')p[i][j + 1] = 2;else p[i][j + 1] = 0;
}
}
for(int i = 2; i <= h + w; i++){
int c = 0, pp = 0;
for(int j = max(1ll, i - w); j < i && j <= h; j++){
++pp;
int k = i - j;
if(p[j][k] == 1){if(c == 2){cout << 0; return 0;} c = 1;}else
if(p[j][k] == 2){if(c == 1){cout << 0; return 0;} c = 2;}
}
if(c == 0){
ans *= 2;
ans %= mod;
}
}
cout << ans;
} | /*
ID: gurban1
LANG: C++
TASK:
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 24;
const int maxn=2e5+5;
const int mod=998244353;
int n,m;
ll dp[maxn][N];
ll f[maxn],ans;
ll binmod(ll a,ll b) {if(b==0)return 1;ll x=binmod(a*a%mod,b>>1);return b&1?x*a%mod:x;}
ll inv(ll a){
return binmod(a,mod-2);
}
ll choose(int a,int b){
if(a < b) return 0;
ll x = (f[a] * inv(f[b])) % mod;
x = (x * inv(f[a-b]))%mod;
return x;
}
int main(){
// freopen("","r",stdin);
// freopen("","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
f[0] = 1;
for(int i = 1;i <= n;i++) f[i] = (f[i-1] * i) % mod;
for(int i = m;i >= 1;i--){
dp[i][1] = 1;
for(int j = i+i;j <= m;j += i)
for(int k = 1;k < N;k++) dp[i][k+1] += dp[j][k];
for(int j = 1;j < N;j++){
if(j > n) break;
ll nw = (dp[i][j] * choose(n-1,j-1)) % mod;
ans = (ans + nw) % mod;
}
}
cout<<ans;
} |
#include <bits/stdc++.h>
//#include <cmath>
typedef long long ll;
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define mod 1000000007
//#define lld long double
#define mii map<int, int>
#define mci map<char, int>
#define msi map<string, int>
#define pii pair<int, int>
#define ff first
#define ss second
#define bs(yup,x) binary_search(yup.begin(),yup.end(),x) //it will return bollean value
#define lb(yup,x) lower_bound(yup.begin(),yup.end(),x) //it will return the adress of the number which is equal to or just greater than x ,and if it is equal to yup.end() than their in no such number exist
#define ub(yup,x) upper_bound(yup.begin(),yup.end(),x)
#define all(x) (x).begin(), (x).end()
#define rep(i,x,y) for(int i=x; i<y; i++)
#define fill(a,b) memset(a, b, sizeof(a))
#define vi vector<int>
#define setbits(x) __builtin_popcountll(x)
using namespace std;
const long long N=1000001,INF=2000000000000000000;
const long double EPS= 0.000000000001;
// calculate a^b%p
ll power(ll a, ll b, ll p)
{
if(a==0)
return 0;
ll res=1;
//a%=p;
while(b>0)
{
if(b&1)
res=(res*a)%p;
b>>=1;
a=(a*a)%p;
}
return res;
}
//sieve to calculate an array of prime and non prime integers till N
vi prime;
bool isprime[N];
void pre()
{
for(int i=2;i<N;i++)
{
if(isprime[i])
{
for(int j=i*i;j<N;j+=i)
isprime[j]=false;
prime.pb(i);
}
}
return;
}
double fn_ternary(double b,double c,double x){
double ans = (x*x + b*x + c)/(sin(x));
return ans;
}
double ternary_search(double l,double r,double b,double c){
//for finding minimum
//to find max interchange r and l
double eps = 1e-10;
while(r-l>eps){
double m1 = l+(r-l)/3;
double m2 = r-(r-l)/3;
double f1 = fn_ternary(b,c,m1);
double f2 = fn_ternary(b,c,m2);
if(f1<f2){
r = m2;
}else{
l = m1;
}
}
return fn_ternary(b,c,l);
}
ll binary_search(ll a[],int n,ll k){
int l = 0,r = n-1;
while(l<=r){
int m = l + (r-l)/2;
if(a[m]==k){
return m;
}
else if(a[m]>=k){
r = m - 1;
}
else if(a[m]<k){
l = m + 1;
}
}
return -1;
}
// if more values to calculate use pascals triangle (C(n,k) = C(n-1,k-1)+C(n-1,k))
/*
const ll maxn = 1000000;
ll C[maxn+1][maxn+1];
C[0][0] = 1;
void nCr(ll p){
for(ll i=1;i<=maxn;i++){
C[i][0] = C[i][i] = 1;
for(int j = 1;j<=i;j++){
C[i][j] = (C[i-1][j-1]%p + C[i-1][j]%p)%p;
}
}
return ;
}
*/
//calculating ncr modulo using fermats little theorem
//useful while calculating less discrete values of ncr
ll modinverse(ll n,ll p){
return power(n,p-2,p);
}
ll nCr(ll n,ll r,ll p){
ll f[n+1];
f[0] = 1;
for(ll i=1;i<=n;i++){
f[i] = f[i-1]*i%p;
}
return (f[n]%p * modinverse(f[r],p)%p * modinverse(f[n-r],p)%p)%p;
}
bool comp(vector<int> v1,vector<int> v2){
return v2[0]>v1[0];
}
int main() {
IOS;
//fill(isprime, true);
//pre();
int t;
//cin>>t;
t=1;
while(t--){
int n;
cin>>n;
vector<int> v[n];
for(int i=0;i<n;i++){
int id,x,y;
cin>>id>>x>>y;
v[i].push_back(x);
v[i].push_back(y);
v[i].push_back(id);
}
int cnt = 0;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
vector<int> a = v[i],b = v[j];
if(a[0]<=b[0]){
if(a[1]==b[0]&&(a[2]==1||a[2]==3)&&(b[2]==1||b[2]==2)){
cnt++;
}
else if(a[1]>b[0]){
cnt++;
}
}
else{
if(b[1]==a[0]&&(b[2]==1||b[2]==3)&&(a[2]==1||a[2]==2)){
cnt++;
}
else if(b[1]>a[0]){
cnt++;
}
}
}
}
cout<<cnt<<endl;
}
return 0;
} | #include <iostream>
#include <vector>
#pragma GCC optimize ("-O3")
using namespace std;
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
int n; cin >> n;
vector<double> l(n), r(n);
for (int i = 0; i < n; i++) {
int t; cin >> t >> l[i] >> r[i];
t--;
if (t & 1) r[i] -= 0.5;
if (t & 2) l[i] += 0.5;
}
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
ans += (max(l[i], l[j]) <= min(r[i], r[j]));
}
}
cout << ans << endl;
} |
#include <iostream>
using namespace std;
int main() {
long long a, b;
cin >> a >> b;
bool flag = false;
for (long long i = 1; i <= b / i; i ++) {
if (b % i == 0) {
if (b / i + i == a) {
flag = true;
break;
}
}
}
if (flag) cout << "Yes";
else cout << "No";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define MAX( a , b ) ( a > b ) ? a : b
#define MIN( a , b ) ( a < b ) ? a : b
using ull = unsigned long long;
using ll = long long;
using ul = unsigned long;
int main(){
ull A,B;
ull result = 0;
cin >> A >> B;
ull C = B - A;
for(ull i = C; i > 0; i--){
ull j = B % i;
ull k = B - j;
if((k - i) >= A){
cout << i << endl;
return 0;
}
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define test int t;cin>>t;while(t--)
#define REP(i,n) for (int i = 1; i <= n; i++)
#define MOD 1000000007
#define MOD2 998244353
#define ll long long
#define ld long double
#define int ll
#define pb push_back
#define ii pair<int,int>
#define llll pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define vii vector<ii>
#define vllll vector<llll>
#define ff first
#define ss second
#define INF 1000000000
#define HINF 1000000000000000
#define mem(a,b) memset(a,b,sizeof(a))
#define arrin(x,n) int x[n]; for(int o=0;o<n;o++) cin>>x[o]
#define arrout(x,n) for(int o=0;o<n;o++) cout<<x[o]<<" "; cout<<endl
#define vecin(x,n); vi x;int hool; for(int o=0;o<n;o++) {cin>>hool; x.pb(hool);}
#define vecout(x,n) for(int o=0;o<n;o++) cout<<x[o]<<" "; cout<<endl
#define all(x) x.begin(),x.end()
#define deb(x) cout<<#x<<'='<<x<<endl
#define deb2(x,y) cout<<#x<<'='<<x<<" "<<#y<<'='<<y<<endl
#define deb3(x,y,z) cout<<#x<<'='<<x<<" "<<#y<<'='<<y<<" "<<#z<<'='<<z<<endl
#define debarr(arr,n) for(int o=0;o<n;o++) deb2(o,arr[o])
#define debarrall(arr) debarr(arr,arr.size())
#define IO freopen("input.txt", "r", stdin); freopen("output.txt", "w+", stdout)
const double pi = 3.14159265358979323846;
int powersimple(int a, int b){//a^b
int res=1;
while(b>0){
if(b&1)
{res=(res*a);
b--;}
a=(a*a);
b>>=1;
}
return res;
}
int ncr(int n,int k)
{
int ans=1;
if(k>n-k)
k=n-k;
for(int i=1;i<=k;i++)
ans*=(n-i+1),ans/=i;
return ans;
}
int power(int x,int y,int p)
{
int res = 1;
x = x % p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
int modInverse(int n, int p)
{
return power(n, p - 2, p);
}
int ncrModPFermat(int n,int r, int p)
{
if (r == 0)
return 1;
int fac[n + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = (fac[i - 1] * i) % p;
return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p;
}
int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);}
///////////////END OF TEMPLATE//////////////////
void solve()
{
int i=0,j=0,k=0,m,n,p,sum=0,cnt=0;
int x,y,a,b;
long double r;
cin>>x>>y>>a>>b;
m=b/(a-1);
if(y<=m)
{
cnt=((y-1));
r=log(cnt/x)/log(a);
while((r-1)>=0)
{
sum++;
x*=a;
r-=1;
}
cout<<sum;
return;
}
//sum=((m)/x)/a;
cnt=(m);
r=log(cnt/x)/log(a);
while((r-1)>=0)
{
sum++;
x*=a;
r-=1;
}
//x=sum*a*x;
//if(x<m and x*a<y)
{
// sum++;
// x*=a;
}
sum+=(y-1-x)/b;
cout<<sum;
}
int32_t main() {
fast
//IO;
//cout<<fixed<<setprecision(15);
//test
{
solve();
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for(int i=0; i<n; i++)
#define REPi(i, a, b) for(int i=int(a); i<int(b); i++)
#define MEMS(a,b) memset(a,b,sizeof(a))
#define mp make_pair
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const ll MOD = 1e9+7;
int main(){
ll X, Y, A, B;
cin >> X >> Y >> A >> B;
ll L = B / (A-1);
//if(B % (A-1) == 0)
// L--;
ll ans = 0;
while(L >= X){
if(X*A >= Y)
break;
X *= A;
ans++;
}
ll d = Y-1 - X;
ll n = d / B;
ans += n;
X = X + n*B;
cout << ans << endl;
return 0;
}
|
#include <iostream>
#include <cstring>
#include<vector>
#include <algorithm>
#include<cstdlib>
#include<set>
#include<math.h>
#include<map>
#include<unordered_map>
#include<iomanip>
#include<queue>
#include<bitset>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const ll MOD = 1000000007;
#define rep(i,a, n) for (ll i = a; i < (ll)(n); i++)
#define vecsort(myVec) sort(myVec.begin(),myVec.end(),[](const vector<ll> &alpha,const vector<ll> &beta){return alpha[0] < beta[0];});
#define vsort(v) sort(v.begin(),v.end());
#define vdownsort(v) sort(v.begin(),v.end(),greater<ll>());
#define P pair<ll,ll>
ll modpow(ll a, ll n, ll mod)
{
ll res = 1;
while (0 < n)
{
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
int main()
{
ll n;
cin >> n;
vector<ll> a(n);
rep(i, 0, n) cin >> a[i];
vsort(a);
ll ans, p, q;
ans = p = q = 0;
rep(i, 0, n)
{
p += a[i] * (n - 1 - i);
q += a[i] * i;
}
ans = q - p;
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define pi 3.14159265359
#define all(i) i.begin(),i.end()
using namespace std;
const long long INF=1e9+7;
const string alp="abcdefghijklmnopqrstuvwxyz"; //26
const string ALP="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> T;
//const ll MOD=998244353LL;
//const ll MOD=1777777777LL;
int main() {
ll a,b,c,d;
cin>>a>>b>>c>>d;
cout<<a*d-b*c<<endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) begin(v),end(v)
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll<<30;
constexpr ll longINF = 1ll<<60;
constexpr ll MOD = 1000000007;
constexpr bool debug = false;
//---------------------------------//
int main() {
int N;
cin >> N;
vector<int> A(N), T(N);
REP(i, N) scanf("%d %d", &A[i], &T[i]);
ll ub = longINF, lb = -longINF, s = 0;
for (int i = N - 1; i >= 0; --i) {
if (T[i] == 1) {
ub -= A[i];
lb -= A[i];
s += A[i];
}
else if (T[i] == 2) chmax(lb, A[i]);
else chmin(ub, A[i]);
}
auto func = [&](ll st) {
ll res = st;
REP(i, N) {
if (T[i] == 1) res += A[i];
else if (T[i] == 2) chmax(res, A[i]);
else chmin(res, A[i]);
}
return res;
};
ll ubv = func(ub);// cout << ub << ", " << ubv << endl;
ll lbv = func(lb);// cout << lb << ", " << lbv << endl;
// cout << "lb = " << lb << ", ub = " << ub << endl;
// cout << s << endl;
int Q;
cin >> Q;
while (Q--) {
int x;
scanf("%d", &x);
if (lb < x && x < ub) printf("%lld\n", x + s);
else if (x >= ub) printf("%lld\n", ubv);
else printf("%lld\n", lbv);
}
} | /*#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma comment(linker, "/stack:200000000")*/
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <stack>
#include <deque>
#include <iterator>
#include <tuple>
#include <iomanip>
#include <valarray>
#include <limits>
#include <sstream>
#include <cassert>
#include <functional>
#include <numeric>
#define in insert
#define pb push_back
#define mp make_pair
#define int long long
#define fi first
#define double long double
#define se second
#define endl '\n'
#define pii pair<int,int>
#define pdd pair<double,double>
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define mod 998244353
#define MAXN 21
#define all(v) v.begin(),v.end()
using namespace std;
/*using namespace __gnu_pbds;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;*/
int spf[MAXN];
int fac[MAXN];
void sieve()//sieve
{
spf[1] = 1;
for (int i=2; i<MAXN; i++)
spf[i] = i;
for (int i=4; i<MAXN; i+=2)
spf[i] = 2;
for (int i=3; i*i<MAXN; i++)
{
if (spf[i]==i)
{
for (int j=i*i; j<MAXN; j+=i)
if (spf[j]==j)
spf[j]=i;
}
}
}//sieve ends
map<int,int> getfactor(int a)//factor prime
{
map<int,int> m;
while(a>1)
{
m[spf[a]]++;
a/=spf[a];
}
return m;
}//prime factor ends
int power(int x,int y,int p)
{
int res = 1;
x = x%p;
if (x == 0||y < 0) return 0;
while (y > 0)
{
if (y & 1)
res = (res*x)% p;
y = y>>1;
x = (x*x)%p;
}
return res;
}
int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
int inverse(int a, int p)
{
return power(a, p-2, p);
}
int ncr(int n, int r, int p)
{
if(r>n)
return 0;
if (r==0||r==n)
return 1;
return (fac[n]*inverse(fac[r], p) % p *
inverse(fac[n-r], p) % p) % p;
}
void solve()
{
int n;
cin>>n;
int a[n+1];
int ans=0;
map<int,int> m;
for(int i=1;i<=n;i++)
{
cin>>a[i];
ans+=i-1-m[a[i]];
m[a[i]]++;
}
cout<<ans<<endl;
}
signed main()
{
fast
int test=1;
//cin>>test;
while(test--)
{
solve();
}
}
|
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b,c;
cin>>a>>b>>c;
long long sum=0;
if(a==6)
{
sum+=1;
}
else if(a==4){
sum+=3;
}
else if(a==5)
{
sum+=2;
}
else if(a==3)
{
sum+=4;
}
else if(a==2)
{
sum+=5;
}
else if(a==1)
{
sum+=6;
}
if(b==6)
{
sum+=1;
}
else if(b==4){
sum+=3;
}
else if(b==5)
{
sum+=2;
}
else if(b==3)
{
sum+=4;
}
else if(b==2)
{
sum+=5;
}
else if(b==1)
{
sum+=6;
}
if(c==6)
{
sum+=1;
}
else if(c==4){
sum+=3;
}
else if(c==5)
{
sum+=2;
}
else if(c==3)
{
sum+=4;
}
else if(c==2)
{
sum+=5;
}
else if(c==1)
{
sum+=6;
}
cout<<sum<<endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
double dp[101][101][101];
double funct(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 ans = 0;
ans += (funct(a + 1, b, c) + 1)*a/(a + b + c);
ans += (funct(a, b + 1, c) + 1)*b/(a + b + c);
ans += (funct(a, b, c + 1) + 1)*c/(a + b + c);
dp[a][b][c] = ans;
return ans;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int a, b, c;
cin >> a >> b >> c;
printf("%.9lf\n", funct(a, b, c));
return 0;
} |
//bismillahir rahmanir rahim //Author:Fayed Anik
#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 ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define lf double
#define pb(x) push_back(x)
#define ull unsigned long long
#define sfl(a) scanf("%lld",&a)
#define sf(a) scanf("%d",&a)
#define pf(a) printf("%d\n",a)
#define pfl(a) printf("%lld\n",a)
#define FOR(x,n) for(ll x=1;x<=n;++x)
#define pii pair< ll , ll >
#define mp(a,b) make_pair(a,b)
#define mod 1000000007
#define INF 2e18
#define EPS 1e-15
#define f1 first
#define f2 second
#define all(v) v.begin(),v.end()
#define PI acos(-1)
#define printminusone printf("-1\n")
#define bug printf("bug")
#define FILEIN freopen("in.txt","r",stdin)
#define FILEOUT freopen("out.txt","w",stdout)
//ll SET(ll mask,ll pos){ return mask = (mask | (1ll<<pos)); }
//ll RESET(ll mask,ll pos){ return mask = mask & ~(1ll<<pos); }
//bool CHECK(ll mask,ll pos) { return (bool) (mask & (1ll<<pos)); }
//priority_queue <ll, vector<ll>, greater<ll> > pq;
int main(){
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
ll n,k,m,s=0,x;
sfl(n),sfl(k),sfl(m);
FOR(i,n-1){
sfl(x);
s += x;
}
for(ll i=0;i<=k;i++){
ll val = s+i;
if( val>=m*n){
pfl(i);
return 0;
}
}
printf("-1\n");
return 0;
}
| #include<iostream>
#include<cmath>
#include <vector>
using namespace std;
int main(){
int N = 0;
int K = 0;
int M = 0;
cin >> N >> K >> M;
int tar = N * M;
int sum = 0;
for (int i = 0; i < N - 1; i++) {
int A = 0;
cin >> A;
sum += A;
}
if (tar - sum <= 0) {
cout << 0 << endl;
}else if(tar - sum <= K) {
cout << tar - sum << endl;
}else {
cout << -1 << endl;
}
return 0;
} |
#include<bits/stdc++.h>
#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 pb push_back
#define eb emplace_back
#define SZ(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define make_unique(a) sort(all((a))), (a).erase(unique(all((a))),(a).end())
#define x first
#define y second
#define MP make_pair
#define MT make_tuple
#define debug(x) cout << #x << " = " << x << endl
#define debug2(x,y) FOR(i,1,y) cout << "##"; cout << #x << " = " << x << endl
using namespace std;
typedef long long i64;
typedef tuple<int,int,int> iii;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long double ld;
int readInt(){ int a; scanf("%d",&a); return a; }
i64 readLong(){ i64 a; scanf("%lld",&a); return a;}
void readString(char *s){ scanf(" %s",s); }
const int mod = 998244353;
int add(int a, int b){ return ((a+=b)>=mod)?a-mod:a; }
void adding(int &a, int b){ a = add(a,b); }
int mul(int a, int b){ return a*1ll*b%mod; }
int pw(int a, int b){
int res = a, ans = 1;
for(int i=1;i<=b;i<<=1,res=mul(res,res)){
if(i&b) ans = mul(ans, res);
}
return ans;
}
const int N = 600005;
vector<i64> comp;
i64 dp[N], r[N];
int cnt[2][N];
int main(){
int n = readInt();
FOR(i, 1, n){
dp[i] = readLong();
dp[i] -= dp[i-1];
//printf("[%lld] ",dp[i]);
comp.eb(dp[i]);
comp.eb(-dp[i]);
}
comp.eb(0);
make_unique(comp);
FOR(i, 0, n){
r[i] = upper_bound(all(comp), -dp[i])-comp.begin();
dp[i] = upper_bound(all(comp),dp[i])-comp.begin();
}
i64 ans = 0;
cnt[0][dp[0]]++;
FOR(i, 1, n){
int nw = (i&1);
ans += cnt[nw][dp[i]]+cnt[nw^1][r[i]];
cnt[nw][dp[i]]++;
}
printf("%lld",ans);
return 0;
}
// 1
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
map<ll,int> mp[2];
int main(){
int n; cin >> n;
vector<int> a(n);
ll pre=0;
mp[1][0]++;
ll ans = 0;
for(int i=0;i<n;i++){
cin >> a[i];
pre = a[i]-pre;
ans += mp[i&1][pre];
ans += mp[(i+1)&1][-pre];
mp[i&1][pre]++;
}
cout << ans << endl;
return 0;
} |
/*input
1
58
998244353
1
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
#pragma GCC optimize("unroll-loops,no-stack-protector")
//order_of_key #of elements less than x
// find_by_order kth element
typedef long long int ll;
#define ld double
#define pii pair<ll,ll>
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
#define sz(x) (int)x.size()
const ll maxn=4e5+5;
const ll maxlg=__lg(maxn)+2;
const ll INF64=4e18;
const int INF=0x3f3f3f3f;
const ll MOD=(1e9+7);
const ld PI=acos(-1);
const ld eps=1e-9;
#define lowb(x) x&(-x)
#define MNTO(x,y) x=min(x,(__typeof__(x))y)
#define MXTO(x,y) x=max(x,(__typeof__(x))y)
#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())
ll mult(ll a,ll b){
return (a*b)%MOD;
}
ll mypow(ll a,ll b){
if(b<=0) return 1;
a%=MOD;
ll res=1LL;
while(b){
if(b&1) res=mult(res,a);
a=mult(a,a);
b>>=1;
}
return res;
}
int a[maxn],b[maxn],arr[maxn],cur[maxn],dude[maxn];
int main(){
ios::sync_with_stdio(false),cin.tie(0);
int n;
cin>>n;
REP(i,n) cin>>a[i];
REP(i,n) cin>>b[i];
REP(i,n){
cin>>arr[i];
--arr[i];
if(b[arr[i]]>=a[i] and i!=arr[i]){
cout<<-1;
return 0;
}
}
REP(i,n) dude[arr[i]]=i;
vector<pii> v;
REP(i,n) v.pb({a[i],i});
sort(ALL(v));
vector<pii> ans;
REP(i,n){
int z=v[i].s;
int p=dude[z];
if(z==p){
continue;
}
ans.pb({z,p});
dude[arr[z]]=p;
swap(arr[z],arr[p]);
}
cout<<sz(ans)<<'\n';
for(auto x:ans) cout<<x.f+1<<' '<<x.s+1<<'\n';
} | // Artur Kraska, II UWr
#include <bits/stdc++.h>
#define forr(i, n) for(int i=0; i<n; i++)
#define FOREACH(iter, coll) for(auto iter = coll.begin(); iter != coll.end(); ++iter)
#define FOREACHR(iter, coll) for(auto iter = coll.rbegin(); iter != coll.rend(); ++iter)
#define lbound(P,R,PRED) ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;})
#define testy() int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests)
#define CLEAR(tab) memset(tab, 0, sizeof(tab))
#define CONTAIN(el, coll) (coll.find(el) != coll.end())
#define FOR(i, a, b) for(int i=a; i<=b; i++)
#define FORD(i, a, b) for(int i=a; i>=b; i--)
#define MP make_pair
#define PB push_back
#define ff first
#define ss second
#define deb(X) X;
#define M 1000000007
#define INF 1000000007LL
using namespace std;
int n;
int waga[1000007], kto_ma[1000007], bagaz[1000007], ktory[1000007];
vector <pair<int, int> > res, v;
void zamien(int nr, int nr2)
{
res.PB(MP(nr, nr2));
swap(ktory[nr], ktory[nr2]);
kto_ma[ktory[nr]] = nr;
kto_ma[ktory[nr2]] = nr2;
}
int main()
{
scanf("%d", &n);
FOR(i, 1, n)
{
scanf("%d", &waga[i]);
v.PB(MP(waga[i], i));
}
sort(v.begin(), v.end());
FOR(i, 1, n)
scanf("%d", &bagaz[i]);
FOR(i, 1, n)
{
scanf("%d", &ktory[i]);
kto_ma[ktory[i]] = i;
}
for(auto p : v)
{
int nr = p.ss;
//cout << "ogarnia " << nr << endl;
int nr2 = kto_ma[nr];
if(nr == nr2)
continue;
if(bagaz[ktory[nr]] >= waga[nr] || bagaz[ktory[nr2]] >= waga[nr2])
{
printf("-1\n");
return 0;
}
zamien(nr, nr2);
}
printf("%d\n", res.size());
for(auto p : res)
{
printf("%d %d\n", p.ff, p.ss);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin>>
#define ll long long
#define ln cout<<'\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}void pr(){ln;}
template<class A,class...B>void pr(const A &a,const B&...b){cout<<a<<(sizeof...(b)?" ":"");pr(b...);}
template<class A>void PR(A a,ll n){rep(i,n)cout<<(i?" ":"")<<a[i];ln;}
const ll MAX=1e9+7,MAXL=1LL<<61,dx[8]={-1,0,1,0,-1,-1,1,1},dy[8]={0,1,0,-1,-1,1,1,-1};
typedef pair<ll,ll> P;
void Main() {
ll n,m;
cin >> n >> m;
string s[n];
rep(i,n) R s[i];
int ans=0;
rep(i,n) {
rep(j,m) {
if(j+1<m&&s[i][j]=='.'&&s[i][j+1]=='.') ans++;
if(i+1<n&&s[i][j]=='.'&&s[i+1][j]=='.') ans++;
}
}
pr(ans);
}
int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}
| #include <bits/stdc++.h>
using namespace std;
int main() {
long long A,B;
cin>>A>>B;
long long maxx=max(A,B);
long long minn=min(A,B);
//long long sa=maxx-minn;
long long are=((maxx*(1+maxx))/2)-((minn*(1+minn))/2);
if(A>B){
for(long long a=0;a<A;a++){
cout<<a+1<<' ';
}
for(long long a=0;a<B;a++){
if(a==B-1){
cout<<-1*(a+1+are)<<' ';
}
else{
cout<<-1*(a+1)<<' ';
}
}
}
else{
for(long long a=0;a<B;a++){
cout<<-1*(a+1)<<' ';
}
for(long long a=0;a<A;a++){
if(a==A-1){
cout<<(a+1+are)<<' ';
}
else{
cout<<(a+1)<<' ';
}
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
void printmat(const vector<vector<int>>& mat) {
for (auto row : mat) {
for (int elem : row)
cout << elem << " ";
cout << endl;
}
}
void printv(const vector<int>& v) {
for (int elem : v)
cout << elem << " ";
cout << endl;
}
void printvp(const vector<pair<int, int>>& vp) {
for (auto row : vp)
cout << row.first << ", " << row.second << " ; ";
cout << endl;
}
static bool cmp(pair<string,int>& a, pair<string,int>& b) {
if (a.second>b.second) return true;
if (a.second<b.second) return false;
return (a.first<b.first);
}
int main() {
int T=1, caseIdx=0;
//cin >> T;
while (T--) {
caseIdx++;
string s, scopy;
cin >> s;
int ans = 0;
for (int k=0; k<10000; k++) {
string t = to_string(k);
while (t.length()<4)
t.insert(t.begin(), '0');
//cout << t << endl;
scopy = s;
bool bFail = false;
for (int i=0; i<10; i++) {
for (int j=0; j<4; j++) {
int pos = t[j] - '0';
if (scopy[pos]=='x') {
bFail = true;
break;
}
scopy[pos] = '?';
}
if (bFail)
break;
}
if (!bFail) {
//cout << scopy << endl;
for (int i=0; i<10; i++) {
if (scopy[i]=='o') {
bFail = true;
break;
}
}
if (!bFail)
ans++;
}
}
cout << ans << endl;
//cout << "Case #" << caseIdx << ": " << ans << endl;
}
} | #include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define endl "\n"
#define ll long long
#define sz(s) (int)(s.size())
#define INF 0x3f3f3f3f3f3f3f3fLL
#define all(v) v.begin(),v.end()
#define watch(x) cout<<(#x)<<" = "<<x<<endl
const int dr[] { -1, -1, 0, 1, 1, 1, 0, -1 };
const int dc[] { 0, 1, 1, 1, 0, -1, -1, -1 };
#if __cplusplus >= 201402L
template<typename T>
vector<T> create(size_t n) {
return vector<T>(n);
}
template<typename T, typename ... Args>
auto create(size_t n, Args ... args) {
return vector<decltype(create<T>(args...))>(n, create<T>(args...));
}
#endif
void run() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.in", "r", stdin);
#else
//freopen("groups.in", "r", stdin);
#endif
}
void solve() {
ll n;
cin >> n;
bool have = false;
if (n % 2 == 0) {
have = true;
n /= 2;
}
if (have && n % 2)
cout << "Same";
else if (have)
cout << "Even";
else
cout << "Odd";
cout << endl;
}
int main() {
run();
int T;
cin >> T;
while (T--) {
solve();
}
}
|
#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
int n;cin >> n;
vector<int> a(n);
for(int i=0;i<n;i++){
cin >> a.at(i);
}
sort(a.begin(),a.end());
for(int i=0;i<n;i++){
if(!(a.at(i)==i+1)){
cout << "No" << endl;
return 0;
}
}
cout << "Yes" << endl;
} | #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<class 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>;
template<class key, class value, class cmp = std::less<key>>
using ordered_map = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
// find_by_order(k) returns iterator to kth element starting from 0;
// order_of_key(k) returns count of elements strictly smaller than k;
#define WTF ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);cerr.tie(0);
// #define endl "\n"
#define int long long
#define ll long long
#define pi pair<int, int>
#define vi vector<int>
#define vc vector<char>
#define pb push_back
#define mat vector<vi>
#define matc vector<vc>
#define set0(x) memset(x,0,sizeof(x));
#define makemat(n, m, init) vector<vi>(n, vi(m, init))
#define makematc(n, m) vector<vc>(n, vc(m, '.'))
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define RFOR(i, a, b) for (int i = a; i >= b; i--)
#define PI 3.141592653589793
#define set0(x) memset(x,0,sizeof(x));
#define szz size()
#ifdef LOCAL
#include "./dbg.h"
#else
#define vvn(...) 1
#define dbg(...) 2
#endif
const int N = 2e5;
const int nax = 1e7+50;
const int INF = 2e9 + 5;
const int MOD = 998244353;//1e9+7;
void mini(int &a, int b) { a = min(a, b); }
void maxi(int &a, int b) { a = max(a, b); }
const int dx[]={-1, 0, 1, 0}, dy[]={0, 1, 0, -1};
// const int dx[]={-1, -1, 0, 1, 1, 1, 0, -1}, dy[]={0, 1, 1, 1, 0, -1, -1, -1};
void solve(int T){
int n;
cin>>n;
set<int>s;
FOR(i,1,n){
int k;
cin>>k;
s.insert(k);
}
cout<<(((int)s.szz == n && *s.begin()==1 && *prev(s.end())==n)?"Yes\n":"No\n");
}
int32_t main(){
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
WTF;
// init();
// cout<<fixed<<setprecision(10);
int tt=1;
// cin>>tt;
FOR(i,1,tt)solve(i);
return 0;
}
|
#include <bits/stdc++.h>
// 1. dp
typedef long double ld;
#define int long long
#define gcd __gcd
#define endl "\n"
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define mod2 998244353
#define maxe *max_element
#define mine *min_element
#define inf 1e18
#define pb push_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define ins insert
#define sz(x) (int)(x).size()
#define mk make_pair
#define deci(x, y) fixed<<setprecision(y)<<x
#define w(t) int t; cin>>t; while(t--)
#define nitin ios_base::sync_with_stdio(false); cin.tie(nullptr)
#define PI 3.141592653589793238
using namespace std;
void solve() {
int n,m;
cin>>n>>m;
vector<int>v;
v.push_back(0);
for(int i=0;i<m;i++)
{
int val;
cin>>val;
v.push_back(val);
}
sort(v.begin(),v.end());
v.push_back(n+1);
int ans=0;
vector<int>nv;
for(int i=1;i<=v.size()-1;i++)
{
if(v[i]!=v[i-1] && v[i]!=v[i-1]+1)
nv.push_back(v[i]-v[i-1]-1);
}
if(nv.empty())
{
cout<<0<<endl;
exit(0);
}
int val=mine(all(nv));
for(auto c:nv)
{
ans+=c/val;
if(c%val!=0)
ans+=1;
}
cout<<ans<<endl;
}
int32_t main() {
nitin;
solve();
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define per(i, a, b) for (auto i = (b); i-- > (a); )
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) int((x).size())
#define lb(x...) lower_bound(x)
#define ub(x...) upper_bound(x)
template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 1 << 11, inf = 1e9;
int dp[N][N], a[N][N];
void verdict(int x) {
if (x == 0) cout << "Draw\n";
else cout << (x > 0 ? "Takahashi" : "Aoki") << '\n';
exit(0);
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m; cin >> n >> m;
rep(i, 0, n) rep(j, 0, m) {
char c; cin >> c;
a[i][j] = c == '+' ? +1 : -1;
}
per(i, 0, n) {
per(j, 0, m) {
if (i == n - 1 && j == m - 1)
dp[n - 1][m - 1] = 0;
else if ((i + j) % 2 == 0) {
// 0 maximizes
dp[i][j] = -inf;
if (i + 1 < n) ckmax(dp[i][j], dp[i + 1][j] + a[i + 1][j]);
if (j + 1 < m) ckmax(dp[i][j], dp[i][j + 1] + a[i][j + 1]);
} else {
dp[i][j] = +inf;
if (i + 1 < n) ckmin(dp[i][j], dp[i + 1][j] - a[i + 1][j]);
if (j + 1 < m) ckmin(dp[i][j], dp[i][j + 1] - a[i][j + 1]);
}
}
}
verdict(dp[0][0]);
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
scanf("%d", &n);
long long dp[2]{1ll,1ll};
for (int i=0; i<n; ++i) {
char s[10];
scanf("%s", s);
long long nxt[2];
if (s[0]=='A') {
nxt[1] = dp[1];
nxt[0] = dp[0]*2+dp[1];
}
else {
nxt[1] = dp[1]*2+dp[0];
nxt[0] = dp[0];
}
swap(dp, nxt);
}
printf("%lld\n", dp[1]);
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(long long i=0;i<(long long)(n);i++)
#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)
#define all(a) a.begin(),a.end()
#define rsort(a) {sort(all(a));reverse(all(a));}
#define pb emplace_back
#define eb emplace_back
#define lb(v,k) (lower_bound(all(v),(k))-v.begin())
#define ub(v,k) (upper_bound(all(v),(k))-v.begin())
#define fi first
#define se second
#define pi M_PI
#define PQ(T) priority_queue<T>
#define SPQ(T) priority_queue<T,vector<T>,greater<T>>
#define dame(a) {out(a);return 0;}
#define decimal cout<<fixed<<setprecision(15);
#define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());}
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> PP;
typedef tuple<ll,ll,ll,ll> PPP;
typedef multiset<ll> S;
using vi=vector<ll>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
using vvvvi=vector<vvvi>;
using vp=vector<P>;
using vvp=vector<vp>;
using vpp=vector<PP>;
using vb=vector<bool>;
using vvb=vector<vb>;
using vvvb=vector<vvb>;
const ll inf=1001001001001001001;
const ll INF=1001001001;
const ll mod=1000000007;
const double eps=1e-10;
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';}
template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';}
template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);}
template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);}
template<class T> void yesno(T b){if(b)out("yes");else out("no");}
template<class T> void YesNo(T b){if(b)out("Yes");else out("No");}
template<class T> void YESNO(T b){if(b)out("YES");else out("NO");}
template<class T> void noyes(T b){if(b)out("no");else out("yes");}
template<class T> void NoYes(T b){if(b)out("No");else out("Yes");}
template<class T> void NOYES(T b){if(b)out("NO");else out("YES");}
void outs(ll a,ll b){if(a>=inf-100)out(b);else out(a);}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
ll modpow(ll a,ll b){ll res=1;a%=mod;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;}
class UF{
vi data;stack<P> st;
public:
UF(ll n){
data=vi(n,1);
}
bool heigou(ll x,ll y,bool undo=false){
x=root(x);y=root(y);
if(data[x]>data[y])swap(x,y);
if(undo){st.emplace(y,data[y]);st.emplace(x,data[x]);}
if(x==y)return false;
data[y]+=data[x];data[x]=-y;
return true;
}
ll root(ll i){if(data[i]>0)return i;return root(-data[i]);}
ll getsize(ll i){return data[root(i)];}
bool same(ll a,ll b){return root(a)==root(b);}
void undo(){rep(i,2){data[st.top().fi]=st.top().se;st.pop();}}
};
vi fac,finv,inv;
void init(ll n){
n*=3;
fac=vi(n+5);finv=vi(n+5);inv=vi(n+5);
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
REP(i,2,n+5){
fac[i]=fac[i-1]*i%mod;
inv[i]=mod-inv[mod%i]*(mod/i)%mod;
finv[i]=finv[i-1]*inv[i]%mod;
}
}
long long modcom(int n, int k){
if (n < k) return 0;
if (n < 0 || k < 0) return 0;
return fac[n] * (finv[k] * finv[n - k] % mod) % mod;
}
int main(){
ll n;cin>>n;
P dp=P(1,1);
rep(i,n){
string s;cin>>s;
if(s[0]=='A'){
dp=P(dp.fi,dp.fi+dp.se*2);
}
else{
dp=P(dp.fi*2+dp.se,dp.se);
}
}
out(dp.fi);
} |
#include <cstdio>
#include <cstdlib>
using namespace std;
#define ll long long
const ll MAXN = 400011;
struct fhqTreap {
ll tot, rt;
ll val[MAXN], sum[MAXN], sz[MAXN], ls[MAXN], rs[MAXN], rd[MAXN];
void pushup(ll u) {sz[u] = sz[ls[u]] + sz[rs[u]] + 1; sum[u] = sum[ls[u]] + sum[rs[u]] + val[u];}
void split(ll u, ll k, ll &x, ll &y) {
if(!u) {x = y = 0; return;}
if(val[u] <= k) x = u, split(rs[u], k, rs[u], y);
else y = u, split(ls[u], k, x, ls[u]);
pushup(u);
}
ll merge(ll u, ll v) {
if(!u || !v) return u | v;
if(rd[u] < rd[v]) {rs[u] = merge(rs[u], v); pushup(u); return u;}
else {ls[v] = merge(u, ls[v]); pushup(v); return v;}
}
ll add(ll k) {
rd[++tot] = rand();
sz[tot] = 1;
val[tot] = sum[tot] = k;
return tot;
}
void insert(ll k) {
ll x, y;
split(rt, k, x, y);
rt = merge(merge(x, add(k)), y);
}
ll cb(ll k) {
ll x, y;
split(rt, k, x, y);
ll ans = sz[x] * k + sum[y];
rt = merge(x, y);
return ans;
}
void delet(ll k) {
ll x, y, z;
split(rt, k, x, z);
split(x, k - 1, x, y);
y = merge(ls[y], rs[y]);
rt = merge(merge(x, y), z);
}
} t[2];
ll q;
ll len[2];
ll w[2][MAXN];
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 main() {
ll ans = 0;
len[1] = read(); len[0] = read(); q = read();
for(ll i = 0; i < 2; ++i)
for(ll j = 1; j <= len[i]; ++j)
t[i].rt = t[i].merge(t[i].rt, t[i].add(0));
for(ll i = 1, opt, x, y; i <= q; ++i) {
opt = (read() & 1); x = read(); y = read();
t[opt].delet(w[opt][x]);
t[opt].insert(y);
ans -= t[opt^1].cb(w[opt][x]);
w[opt][x] = y;
ans += t[opt^1].cb(w[opt][x]);
printf("%lld\n", ans);
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
using ll = long long;
int n, m, q, t[N], x[N], y[N], dic[N], L;
int a[N], b[N];
ll d[4][N];
void add(int idx, int x, int v) {
for(int i = x; i <= L; i += i & -i) d[idx][i] += v;
}
ll get(int idx, int x) {
ll res = 0;
for(int i = x; i; i -= i & -i) res += d[idx][i];
return res;
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m >> q;
for(int i = 1; i <= q; i++) {
cin >> t[i] >> x[i] >> y[i];
dic[i] = y[i];
}
dic[q + 1] = 0;
sort(dic + 1, dic + q + 2);
L = unique(dic + 1, dic + q + 2) - dic - 1;
add(0, 1, n);
add(2, 1, m);
// for(int j = 0; j < 4; j++) {
// for(int i = 1; i <= L; i++) cout << get(j, i) - get(j, i - 1) << " ";
// cout << endl;
// }
// cout << endl;
ll ans = 0;
for(int i = 1; i <= q; i++) {
int np = lower_bound(dic + 1, dic + L + 1, y[i]) - dic;
// cout << t[i] << " " << x[i] << " " << y[i] << " " << np << endl;
if(t[i] == 1) {
int p = lower_bound(dic + 1, dic + L + 1, a[x[i]]) - dic;
// for(int i = 1; i <= L; i++) cout << get(2, i) << endl;
// cout << np << " " << p << endl;
ans += get(2, np) * y[i] - get(2, p) * a[x[i]];
ans -= get(3, np) - get(3, p);
add(0, p, -1);
add(0, np, 1);
add(1, p, -a[x[i]]);
add(1, np, y[i]);
a[x[i]] = y[i];
} else {
int p = lower_bound(dic + 1, dic + L + 1, b[x[i]]) - dic;
ans += get(0, np - 1) * y[i] - get(0, p - 1) * b[x[i]];
ans -= get(1, np - 1) - get(1, p - 1);
// cout << get(0, np - 1) * y[i] - get(0, p - 1) * b[x[i]] << " " << get(1, np - 1) - get(1, p - 1) << endl;
add(2, p, -1);
add(2, np, 1);
add(3, p, -b[x[i]]);
add(3, np, y[i]);
b[x[i]] = y[i];
}
cout << ans << endl;
// for(int j = 0; j < 4; j++) {
// for(int i = 1; i <= L; i++) cout << get(j, i) - get(j, i - 1) << " ";
// cout << endl;
// }
// cout << endl;
}
return 0;
} |
#include<bits/stdc++.h>
#define fr(i,a,b,step) for(rg int i=a;i<=b;i+=step)
#define rp(i,a,b,step) for(rg int i=a;i>=b;i-=step)
#define inf 0x3f3f3f3f3f3f
#define rs(a,b) memset(a,b,sizeof(a))
#define bug printf("debug");
#define ie inline
#define rg register
using namespace std;
typedef long long ll;
const int _=1e6+10;
int n, k;
ll v;
void init(){}
void file(){freopen(".in","r",stdin);freopen(".out","w",stdout);}
signed main(){
// file();
init();
cin>>n>>k;
fr(i,1,n,1){
fr(j,1,k,1){
v+=i*100+j;
}
}
cout<<v;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const double pi = acos(-1);
#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)
#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
REP(i,v.size()){if(i)os<<" ";os<<v[i];}return os;}
template<typename T> ostream& operator<<(ostream& os, const vector<vector<T>>& v) {
REP(i,v.size()){if(i)os<<endl;os<<v[i];}return os;}
int main() {
cout << fixed << setprecision(15);
int n;
cin >> n;
vector<int>x(n);
for(int i=0;i<n;i++)cin >> x[i];
long ans1=0;
for(int i=0;i<n;i++)ans1+=abs(x[i]);
cout << ans1 << endl;
long ans2=0;
for(int i=0;i<n;i++)ans2+=abs(x[i])*(long)abs(x[i]);
cout << sqrt(ans2) << endl;
int ans3=0;
for(int i=0;i<n;i++)ans3=max(ans3,abs(x[i]));
cout << ans3 << endl;
} |
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define DD double
#define Pb push_back
#define Bp pop_back
#define Pf push_front
#define Fp pop_front
#define Ub upper_bound
#define Lb lower_bound
#define Bs binary_search
#define In insert
#define Mp make_pair
#define All(x) x.begin(), x.end()
#define mem(a, b) memset(a, b, sizeof(a))
#define fast ios_base::sync_with_stdio(0);cin.tie(0)
#define X first
#define Y second
const int mx1 = 85;
const int mx2 = 105;
const int mx3 = 2005;
const int mx4 = 30005;
const int mx5 = 200005;
const int mx6 = 1000005;
typedef vector<int> Vi;
typedef vector<DD> Vd;
typedef vector<bool> Vb;
typedef vector<Vi> VVi;
typedef pair<int, int> Pii;
typedef pair<DD, DD> Pdd;
typedef vector<Pii> Vpi;
typedef vector<Pdd> Vpd;
typedef queue<int> Qi;
typedef stack<int> Si;
typedef deque<int> Di;
int _toggle(int N, int pos) {return N = N ^ (1 << pos);}
int _set(int N, int pos) {return N = N | (1 << pos);}
int _reset(int N, int pos) {return N = N & ~(1 << pos);}
bool _check(int N, int pos) {return (bool)(N & (1 << pos));}
bool _upper(char a) {return a >= 'A' && a <= 'Z';}
bool _lower(char a) {return a >= 'a' && a <= 'z';}
bool _digit(char a) {return a >= '0' && a <= '9';}
int dx[] = {1, -1, 0, 0, -1, -1, 1, 1};
int dy[] = {0, 0, 1, -1, -1, 1, -1, 1};
///******************************************************///
LL x, y, a, b;
void solve()
{
cin >> x >> y >> a >> b;
LL ans = 0;
while(1){
LL c = y / x;
if(a > c || x * a > x + b){
LL p = (y - x) / b - ((y - x) % b == 0);
ans += p;
break;
}
x *= a;
ans++;
}
cout << ans << '\n';
}
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
fast;
int tc = 1;
//cin >> tc;
while(tc--){
solve();
}
}
| // #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,mmx,avx,avx2")
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
//All indexing is 0-based
template<class key, class cmp = std::less<key>>
using oset = tree<key, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
//methods: find_by_order(k); & order_of_key(k);
//To make it an ordered_multiset, use pairs of (value, time_of_insertion)
//to distinguish values which are similar
template<class key, class value, class cmp = std::less<key>>
using omap = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long
#define ull unsigned long long
#define lld long double
#define w(x) ll x;cin>>x;while(x--)
#define all(x) x.begin(), x.end()
#define iceil(n, x) (((n) + (x) - 1) / (x))
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) __detail::__lcm(a,b)
#define goog(tno) cout << "Case #" << tno <<": "
void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define bug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define bug(x...)
#endif
ll dx[]= {-1,-1,-1,0,0,1,1,1};
ll dy[]= {-1,0,1,-1,1,-1,0,1};
const lld pi=3.1415926535897932384626433832795;
const ll INF=1e18;
const ll mod=1000000007;
const ll maxn=1e5+5;
void mm(string s){
cout<<s<<'\n';
}
int main(){
ios_base::sync_with_stdio(false), cin.tie(nullptr);
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
ll a,b,c; cin>>a>>b>>c;
if(c%2==0){
a=abs(a);
b=abs(b);
}
if(a>b){
cout<<">";
}else if(a<b){
cout<<"<";
}else{
cout<<"=";
}
return 0;
} |
#include<cstdio>
using namespace std;
int cf[1000010];
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
int l,r;
scanf("%d%d",&l,&r);
cf[l]++;
cf[r+1]--;
}
long long cnt=0,ans=0;
for(int i=1;i<=1000000;i++)
{
cnt+=cf[i];
ans+=cnt*i;
}
printf("%lld",ans);
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
template <class S, class T> inline bool chmax(S &a, T b) { if (a < b) { a = b; return 1; } return 0; }
template <class S, class T> inline bool chmin(S &a, T b) { if (a > b) { a = b; return 1; } return 0; }
constexpr ll MOD = 1000000007;
/* constexpr ll MOD = 998244353; */
constexpr ll MAX = 5e5;
constexpr ll INF = 1e9;
constexpr ll INFLL = 1e18;
constexpr ld EPS = 1e-10;
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
ll ans = 0;
for (int i = 0; i < n; ++i) {
ll a, b;
cin >> a >> b;
ans += (b - a + 1) * (a + b) / 2;
}
cout << ans << endl;
return (0);
}
|
#include<bits/stdc++.h>
using namespace std;
namespace Ruri{
#define ms(a,b) memset(a,b,sizeof(a))
#define repi(i,a,b) for(int i=a,bbb=b;i<=bbb;++i)//attention reg int or reg ll ?
#define repd(i,a,b) for(int i=a,bbb=b;i>=bbb;--i)
#define reps(s) for(int i=head[s],v=e[i].to;i;i=e[i].nxt,v=e[i].to)
#define ce(i,r) i==r?'\n':' '
#define pb push_back
#define all(x) x.begin(),x.end()
#define gmn(a,b) a=min(a,b)
#define gmx(a,b) a=max(a,b)
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
const int infi=1e9+5;//infi่พๅคง๏ผๆณจๆๆถๅinf็ธๅ ๆถ็int
const ll infl=4e18;
inline ll ceil_div(ll a,ll b){ return (a+b-1)/b; }
inline ll pos_mod(ll a,ll b){ return (a%b+b)%b; }
//std::mt19937 rnd(time(0));//std::mt19937_64 rnd(time(0));
}
using namespace Ruri;
namespace Read{
#define ss(a) scanf("%s",a)
inline int ri(){ int x; scanf("%d",&x); return x; }
inline ll rl(){ ll x; scanf("%lld",&x); return x; }
inline db rd(){ db x; scanf("%lf",&x); return x; }
}
namespace DeBug{
#define pr(x) cout<<#x<<": "<<(x)<<endl
#define pra(x,a,b) cout<<#x<<": "<<endl; \
repi(i,a,b) cout<<x[i]<<" "; \
puts("");
#define FR(a) freopen(a,"r",stdin)
#define FO(a) freopen(a,"w",stdout)
}
using namespace Read;
using namespace DeBug;
const int MAX_N=3e3+5;
int n,a[MAX_N][6];
bool ok[60];
bool Check(int x)
{
ms(ok,0);
repi(i,1,n){
int state=0;
repi(j,0,4)if(a[i][j]>=x) state|=(1<<j);
ok[state]=true;
}
repi(i,1,31)if(ok[i])repi(j,i,31)if(ok[j])repi(k,j,31)if(ok[k])
if((i|j|k)==31) return true;
return false;
}
int main()
{
n=ri();
repi(i,1,n)repi(j,0,4) a[i][j]=ri();
int l=0,r=infi,ans;
while(l<=r){
int mid=(l+r)>>1;
if(Check(mid)) ans=mid,l=mid+1;
else r=mid-1;
}
printf("%d\n",ans);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(ll i=0;i<ll(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=ll(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=ll(b);i--)
const ll INF = 1000000000000; //10^12:โ
const ll MOD = 1000000007; //10^9+7:ๅๅๅผใฎๆณ
// ไฝฟใ้ขๆฐใฎๅใซ่จ่ฟฐใใฆใใ
// mll a = 1;ใจใใๅฎฃ่จใงไฝฟ็จใงใใ
// tabใญใผใงไธใพใง็งปๅ
template<ll MOD> struct Fp {
long long val;
constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
if (val < 0) val += MOD;
}
constexpr ll 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 mll = Fp<MOD>;
int main(){
ll H, W;
cin >> H >> W;
vector<string> s(H);
REP(i, H) {
cin >> s.at(i);
}
vector<vector<mll>> dp(H + 1, vector<mll>(W + 1, 0));
dp.at(1).at(1) = 1;
vector<vector<mll>> X = dp;
vector<vector<mll>> Y = dp;
vector<vector<mll>> Z = dp;
FOR(i, 1, H) {
FOR(j, 1, W) {
if(s.at(i - 1).at(j - 1) == '#') {
continue;
}
if(i == 1 && j == 1) {
continue;
}
dp.at(i).at(j) = X.at(i).at(j - 1) + Y.at(i - 1).at(j) + Z.at(i - 1).at(j - 1);
X.at(i).at(j) = X.at(i).at(j - 1) + dp.at(i).at(j);
Y.at(i).at(j) = Y.at(i - 1).at(j) + dp.at(i).at(j);
Z.at(i).at(j) = Z.at(i - 1).at(j - 1) + dp.at(i).at(j);
}
}
cout << dp.at(H).at(W) << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0; i<(n); ++i)
using ll = long long;
const int INF = 1001001001;
int main(){
int n; cin >> n;
ll a, b;
ll ans = 0;
rep(i,n){
cin >> a >> b;
ll sum = (b-a+1)*(a+b) / 2;
ans += sum;
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
long long int a[n],b[n],ans=0;
for(int i=0;i<n;i++)
{
cin>>a[i]>>b[i];
ans += ((b[i]*(b[i]+1)/2) - (a[i]*(a[i]+1))/2 + a[i]);
}
cout<<ans;
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < n; i++)
#define ll long long
using namespace std;
int main(){
int n;
cin >> n;
vector<ll> dp(2, 1);
rep(i, n){
string s;
cin >> s;
vector<ll> p(2); swap(dp, p);
rep(j, 2)rep(x, 2){
int nj = j;
if (s == "AND") nj &= x;
else nj |= x;
dp[nj] += p[j];
}
}
cout << dp[1] << endl;
return 0;
} | #include<bits/stdc++.h>
#define int ll
#define sz(x) int((x).size())
#define all(x) (x).begin(),(x).end()
#define x first
#define y second
using namespace std;
using ll = long long;
using pi = pair<int,int>;
const int inf = 0x3f3f3f3f3f3f3f3f;
const int minf = 0xc0c0c0c0c0c0c0c0;
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
string s; cin>>s;
int res = 0;
for (int i=0; i<=8; i++) {
if (s.substr(i, 4) == "ZONe") ++res;
}
cout<<res<<'\n';
return 0;
} |
#include <vector>
#include <set>
#include <iostream>
#include <algorithm>
#define MAX 100000000
#define ll long long
using namespace std;
bool decide(int n,vector<vector<int>> g,vector<int> v){
int p = v.size();
for(int i = 0;i < p;i ++){
if(g[n][v[i]] == g[v[i]][n]){
return true;
}
}
return false;
}
int main() {
int num;
cin >> num;
int mem;
cin >> mem;
vector<vector<int>> edge(mem);
vector<vector<int>> vert(num);
vector<vector<int>> graph(num,vector<int>(num,0));
for(int i = 0;i < mem;i ++){
int a,b;
cin >> a;
cin >> b;
a--;
b--;
graph[a][b] = 1;
graph[b][a] = 1;
edge[i] = {a,b};
vert[a].push_back(b);
vert[b].push_back(a);
}
vector<int> need(num);
for(int i = 0;i < num;i ++){
cin >> need[i];
}
for(int i = 0;i < mem;i ++){
if(need[edge[i][0]] != need[edge[i][1]]){
if(need[edge[i][0]] > need[edge[i][1]]){
graph[edge[i][1]][edge[i][0]] = 0;
}else{
graph[edge[i][0]][edge[i][1]] = 0;
}
}
}
for(int i = 0;i < mem;i ++){
if(decide(edge[i][0],graph,vert[edge[i][0]])){
vector<int> tree;
int m = need[edge[i][0]];
vector<int> next = {edge[i][0]};
vector<bool> alr(num,false);
while(not next.empty()){
vector<int> newn;
int p = next.size();
for(int j = 0;j< p;j ++){
if(alr[next[j]] == false and need[next[j]] == m){
tree.push_back(next[j]);
alr[next[j]] = true;
newn.insert(newn.end(),vert[next[j]].begin(),vert[next[j]].end());
}
}
next = newn;
}
int root = tree[0];
int s = tree.size();
vector<int> road = {root};
vector<bool> alre(num,false);
alre[root] = true;
while(not road.empty()){
int d = road.size();
/*
for(int j = 0;j < d;j ++){
cout<< road[j]+1 << ' ';
}
cout << endl;*/
int prev = road[d-1];
vector<int> nex = vert[prev];
d = nex.size();
for(int j = 0;j < d;j ++){
if(alre[nex[j]] == true and m == need[nex[j]]){
if(graph[prev][nex[j]] == 1 and graph[nex[j]][prev] == 1){
graph[nex[j]][prev] = 0;
/*cout << 'a' << nex[j]+1 << endl;*/
}
/*cout << '@' << endl;*/
}else if(alre[nex[j]] == false and m == need[nex[j]]){
road.push_back(nex[j]);
alre[nex[j]] = true;
/*cout << 'b' << endl;*/
graph[nex[j]][prev] = 0;/*ใใจใใจใฏ0*/
break;
}
}
if(prev == road.back()){
road.pop_back();
}
}
for(int j = 0;j < s;j ++){
vector<int> exp = vert[tree[j]];
int d = exp.size();
for(int k = 0;k < d;k ++){
if(graph[tree[j]][exp[k]] == graph[exp[k]][tree[j]]){
graph[exp[k]][tree[j]] = 0;
}
}
}
}
}
for(int i = 0;i < mem;i ++){
/*cout << edge[i][0]+1 << ' ' << edge[i][1]+1 << endl;*/
if(graph[edge[i][0]][edge[i][1]] == 1 and graph[edge[i][1]][edge[i][0]] == 0){
cout << "->" << endl;
}else if(graph[edge[i][1]][edge[i][0]] == 1 and graph[edge[i][0]][edge[i][1]] == 0){
cout << "<-" << endl;
}else if(graph[edge[i][0]][edge[i][1]] == 0){
cout << "errorzero" << endl;
}else{
cout << "erroeone" << endl;
}
}
return 0;
} | #include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
vector<vector<pair<int, int>>> v;
vector<bool> bl;
vector<int> depth, c;
vector<str> dir;
vector<pair<int, int>> edges;
void dfs(int nd, int h, int e_id){
if(bl[nd]) return;
bl[nd] = 1;
depth[nd] = h;
if(e_id == 0){
//do nothing
}
else if(e_id > 0){
dir[e_id-1] = "->";
}
else{
e_id*=-1;
e_id--;
dir[e_id] = "<-";
}
for(auto [x, id]: v[nd]) if(c[x] == c[nd]) dfs(x, h+1, id);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m; cin >> n >> m;
v.resize(n), bl.resize(n, 0), depth.resize(n, -1), c.resize(n), dir.resize(m, "<->");
for(int i = 0; i < m; i++){
int a, b; cin >> a >> b; a--, b--;
v[a].pb({b, i+1});
v[b].pb({a, -(i+1)});
edges.pb({a, b});
}
for(int &x: c) cin >> x;
for(int i = 0; i < n; i++) if(!bl[i]) dfs(i, 0, 0);
for(int i = 0; i < m; i++){
if(dir[i] != "<->") continue;
auto [a, b] = edges[i];
if(c[a] > c[b]) dir[i] = "->";
else if(c[b] > c[a]) dir[i] = "<-";
else if(depth[a] > depth[b]) dir[i] = "->";
else dir[i] = "<-";
}
for(str s: dir) cout << s << "\n";
} |
#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,s,n) for (int i = (s); i < (n); ++i)
#define rrep(i,n,g) for (int i = (n)-1; i >= (g); --i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define len(x) (int)(x).size()
#define dup(x,y) (((x)+(y)-1)/(y))
#define pb push_back
#define eb emplace_back
#define Field(T) vector<vector<T>>
#define pq(T) priority_queue<T,vector<T>,greater<T>>
using namespace std;
using ll = long long;
using P = pair<int,int>;
int main() {
int n;
cin >> n;
vector<ll> a(n);
rep(i,0,n) cin >> a[i];
sort(all(a));
ll ans = a[0] + 1;
rep(i,0,n-1) {
ans *= a[i+1] - a[i] + 1;
ans %= 1000000007;
}
cout << ans << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(a) a.begin(),a.end()
typedef pair<ll,ll> pi;
//__builtin_popcountll(2) (the number of ones in the binary representation)
//__builtin_clz(2) (number of leading zeroes)
//__builtin_ctz(2) (number of trailing zeroes)
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>*/
// s.find_by_order(k)(returns iterator to kth element), s.order_of_key(k)(number of elements less than number)
ll mod = 1e9+7;
vector<ll> fact(1);
/*vector<ll> spf(10000005), cntPrime(10000005);
void spff(){
int i;
for(i=1;i<10000005;i++) spf[i] = i;
for(i=2;i*i<10000005;i++)
{
if(spf[i] != i) continue;
for(int j = i*i; j < 10000005; j+= i){
if(spf[j]==j) spf[j] = i;
}
}
for(i=1;i<10000005;i++){
if(spf[i] != spf[i/spf[i]]){
cntPrime[i] = cntPrime[i/spf[i]]+1;
}
else cntPrime[i] = cntPrime[i/spf[i]];
}
}
while(spf[a[i]]!=a[i]){
z=spf[a[i]];
while(a[i]%z==0) a[i]/=z;
m[z]++;
}
if(a[i]>1) m[a[i]]++;*/
ll gcd(ll a, ll b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
ll power(ll x, ll y)
{
ll temp;
if( y == 0)
return 1;
temp = power(x, y/2);
if (y%2 == 0)
return temp*temp;
else
return x*temp*temp;
}
ll power(ll x, ll y, ll p)
{
ll res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
ll modInv(ll n, ll p)
{
return power(n, p - 2, p);
}
ll ncr(ll n, ll r) {return (n>=r?(fact[n]*modInv(fact[r],mod))%mod*modInv(fact[n-r],mod)%mod:0);}
ll add(ll a, ll b) {ll z=a+b; if(z>=mod) z -= mod; return z;}
ll mul(ll a, ll b) { return (a*b)%mod;}
ll sub(ll a, ll b) { return (a-b+mod)%mod;}
// LLONG_MAX
int main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
ll t=1,n,i,j,m,a,b;
//cin>>t;
//fact[0]=1;
//for(i=1;i<300003;i++)fact[i]=(fact[i-1]*i)%mod;
while(t--)
{
cin>>n;
ll z=0;
for(i=0;i<n;i++){
cin>>a;
if(a<=10) continue;
z += a-10;
}
cout<<z;
}
}
|
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
typedef long long ll;
int n,m;
ll a[N];
int main()
{
scanf("%d%d",&n,&m);
ll ans=0,s;
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
sort(a+1,a+n+1);
while(m--){
ll x,k=0;
scanf("%lld",&x);
if(x<a[1]){
printf("%lld\n",x);
continue;
}
else if(x>a[n]){
printf("%lld\n",x+n);
continue;
}
s=upper_bound(a+1,a+1+n,x)-a;
ans=x+s-1;
for(int i=s;i<=n;i++){
if(a[i]>ans){
printf("%lld\n",ans);
k++;
break;
}
ans++;
}
if(k==0) printf("%lld\n",ans);
}
return 0;
} | #pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
using ll = int64_t;
using db = double;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vector<int>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vpii = vector<pii>;
using vpll = vector<pll>;
constexpr char newl = '\n';
constexpr double eps = 1e-10;
#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define F0R(i,b) FOR(i,0,b)
#define RFO(i,a,b) for (int i = ((b)-1); i >=(a); i--)
#define RF0(i,b) RFO(i,0,b)
#define show(x) cout << #x << " = " << x << '\n';
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define sz(x) (int)(x).size()
#define YesNo {cout<<"Yes";}else{cout<<"No";}
#define YESNO {cout<<"YES";}else{cout<<"NO";}
#define v(T) vector<T>
#define vv(T) vector<vector<T>>
template<typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }
template<class T> bool lcmp(const pair<T, T>& l, const pair<T, T>& r) {
return l.first < r.first;
}
template<class T> istream& operator>>(istream& i, v(T)& v) {
F0R(j, sz(v)) i >> v[j];
return i;
}
template<class A, class B> istream& operator>>(istream& i, pair<A, B>& p) {
return i >> p.first >> p.second;
}
template<class A, class B, class C> istream& operator>>(istream& i, tuple<A, B, C>& t) {
return i >> get<0>(t) >> get<1>(t) >> get<2>(t);
}
template<class T> ostream& operator<<(ostream& o, const pair<T, T>& v) {
o << "(" << v.first << "," << v.second << ")";
return o;
}
template<class T> ostream& operator<<(ostream& o, const vector<T>& v) {
F0R(i, v.size()) {
o << v[i] << ' ';
}
o << newl;
return o;
}
template<class T> ostream& operator<<(ostream& o, const set<T>& v) {
for (auto e : v) {
o << e << ' ';
}
o << newl;
return o;
}
template<class T1, class T2> ostream& operator<<(ostream& o, const map<T1, T2>& m) {
for (auto& p : m) {
o << p.first << ": " << p.second << newl;
}
o << newl;
return o;
}
#if 1
// INSERT ABOVE HERE
signed main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int a, b, c, d;
cin >> a >> b >> c >> d;
cout << (b - c) << newl;
}
#endif
|
#include <bits/stdc++.h>
#include <map>
using namespace std;
#define ll long long
ll a[2000000],res[2000000];
int main()
{
ll n,m,minn;
cin>>n>>m;
for(ll i=0;i<n;i++)scanf("%lld",&a[i]);
for(ll i=0;i<m;i++)
{
res[a[i]]++;
}
for(ll i=0;;i++)
{
if(res[i]==0)
{
minn=i;
break;
}
}
//cout<<minn<<endl;
ll L=1,R=m;
for(;R<n;R++,L++)
{
res[a[L-1]]--;
res[a[R]]++;
//cout<<a[L-1]<<" "<<a[R]<<endl;
if(res[a[L-1]]==0&&a[L-1]<minn)
{
minn=a[L-1];
}
if(minn==0)break;
}
cout<<minn<<endl;
}
| #include<bits/stdc++.h>
using namespace std;
int n, i, j, k, m;
vector<int>v[1500010];
main()
{
for(scanf("%d %d", &n, &m);i++<n;)scanf("%d", &k),v[k].push_back(i);
for(i=0;i<=n;i++)
{
if(v[i].size()==0||v[i][0]>m||v[i].back()+m-1<n)
{
printf("%d", i);return 0;
}
for(int j=1;j<v[i].size();j++)
{
if(v[i][j]-v[i][j-1]>m)
{
printf("%d", i);return 0;
}
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
typedef long long ll;
int main(){
ll k;
cin >> k;
string s,t;
cin >> s >> t;
map <ll, ll> a;
map <ll, ll> b;
vector <ll> cnt(10,0);
for(ll i=1; i<=9; i++){
a[i] = 0;
b[i] = 0;
}
rep(i,4){
ll tmpa = s[i] - '0';
ll tmpb = t[i] - '0';
//cout << tmpa << " " << tmpb <<endl;
cnt[tmpa]++;
cnt[tmpb]++;
a.at(tmpa)++;
b.at(tmpb)++;
}
ll d = 0, n = 0;
for(ll i=1;i<=9;i++){
for(ll j=1;j<=9;j++){
if(i==j){
if(k - cnt[i] < 2) continue;
}
else{
if(k - cnt[i] < 1 || k - cnt[j] < 1) continue;
}
//cout << "ok ";
a.at(i)++;
b.at(j)++;
ll pointa = 0;
ll pointb = 0;
for(ll m=1; m<=9; m++){
pointa += m * pow(10, a.at(m));
pointb += m * pow(10, b.at(m));
}
//cout << pointa << " " <<pointb<< " ";
ll tmp;
if(i==j){
tmp = (k-cnt[i]) * (k - cnt[i] - 1);
}
else tmp = (k-cnt[i]) * (k - cnt[j]);
//cout << i << " " <<j << " "<< tmp << endl;
n += tmp;
if(pointa > pointb) {
d += tmp;
//cout << "win";
}
a.at(i)--;
b.at(j)--;
}
}
//cout << n << endl;
//cout << d << endl;
cout << setprecision(15);
cout << (long double)d / (long double)n <<endl;
} | #include <bits/stdc++.h>
using namespace std;
// binary exponentiation
// O(log e)
template<class T, class U>
T binary_power(T b, U e){
T res = 1;
for(; e; e >>= 1, b *= b) if(e & 1) res *= b;
return res;
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(ios::badbit | ios::failbit);
int k;
string s, t;
cin >> k >> s >> t;
s.pop_back();
t.pop_back();
array<int, 10> cnt{};
for(auto c: s){
++ cnt[c - '0'];
}
for(auto c: t){
++ cnt[c - '0'];
}
long long res = 0, cur = 1;
for(auto d = 1; d < 10; ++ d){
if(++ cnt[d] <= k){
cur *= k - cnt[d] + 1;
for(auto e = 1; e < 10; ++ e){
if(++ cnt[e] <= k){
cur *= k - cnt[e] + 1;
array<int, 10> cnt0{}, cnt1{};
++ cnt0[d];
for(auto c: s){
++ cnt0[c - '0'];
}
++ cnt1[e];
for(auto c: t){
++ cnt1[c - '0'];
}
int s0 = 0, s1 = 0;
for(auto i = 1; i < 10; ++ i){
s0 += i * binary_power(10, cnt0[i]);
s1 += i * binary_power(10, cnt1[i]);
}
if(s0 > s1){
res += cur;
}
cur /= k - cnt[e] + 1;
}
-- cnt[e];
}
cur /= k - cnt[d] + 1;
}
-- cnt[d];
}
cout << fixed << setprecision(15);
cout << res / (1.0 * (9 * k - 8) * (9 * k - 9)) << "\n";
return 0;
}
/*
*/
////////////////////////////////////////////////////////////////////////////////////////
// //
// Coded by Aeren //
// //
//////////////////////////////////////////////////////////////////////////////////////// |
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef long long ll;
#define pb push_back
#define MP make_pair
#define F first
#define S second
#define ff(i, c, n) for (ll i = c; i < n; i++)
#define fr(i, c, n) for (ll i = c; i >= n; --i)
#define all(a) a.begin(), a.end()
#define sz(a) (int)a.size()
typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
typedef pair<int, int> pii;
typedef vector<vector<int>> vvi;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
// DEBUGGING TEMPLATE
#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__) << "] "
const ll mod=1000000007;
const ll mod1=998244353;
ll modpow(ll a, ll b, ll c)
{ //a^b%c in O(logb)
ll res = 1;
while (b > 0)
{
if (b & 1)
{
res = (res * a) % c;
}
a = (a * a) % c;
b >>= 1;
}
return res;
}
ll modinv(ll m, ll n)
{ //modular inverse of n w.r.t m provided 1<=n<=(m-1) in O(logn) and m is prime
ll inv[n + 1];
inv[1] = 1;
for (int i = 2; i < n + 1; ++i)
inv[i] = (m - (m / i) * inv[m % i] % m) % m;
return inv[n];
}
ll n1 = 1e10;
void solve(){
int n;
cin >> n;
string s;
cin >> s;
if(s.length() == 1) {
if(s[0] == '1') {
cout << 2 * n1 << endl;
}
else cout << n1<< endl;
}
else {
int i = 0;
while(i < n && s[i] == '1') {
i++;
}
if(i > 2) {
cout << "0\n";
return;
}
for(int j = i + 1; j < n; j += 3) {
if(j < n && s[j] != '1') {
cout << "0\n";
return;
}
if(j + 1 < n && s[j + 1] != '1') {
cout << "0\n";
return;
}
if(j + 2 < n && s[j + 2] != '0') {
cout << "0\n";
return;
}
}
cout << (3 * n1 - (2 - i) - n + 3) / 3 << endl;
}
}
//a+b=a^b+2*(a&b)
int main()
{
// your code goes here
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
t = 1;
while(t--){
solve();
}
return 0;
}
| /*
LL JJ SSSS ii ssss SSSS BBBB
LL JJ S s S B B
LL JJ SSS ii sss SSS BBBB
LL JJ JJ S ii s S B B
LLLLLL JJJJ SSSS ii ssss SSSS BBBB
SSSS BBBB ii ssss LL JJ SSSS
S B B s LL JJ S
SSS BBBB ii sss LL JJ SSS
S B B ii s LL JJ JJ S
SSSS BBBB ii ssss LLLLLL JJJJ SSSS
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL n;
bool st[200005],sdt[6000005];
LL nxt[200005];
/*
10^10
------- = 10^5
10^5
*/
int main(){
for(LL i=1;i<=6000000;++i) sdt[i]=bool(i%3);
scanf("%lld",&n);
for(LL i=1;i<=n;++i)
{
LL x;
scanf("%1lld",&x);
st[i]=x;
}
LL j=0;
for(LL i=2;i<=n;++i)
{
while(j && st[j+1]!=st[i]) j=nxt[j];
if(st[j+1]==st[i]) ++j;
nxt[i]=j;
}
j=0;
LL ans=0,ans2=0;
for(LL i=1;i<=6000000;++i)
{
while(j && st[j+1]!=sdt[i]) j=nxt[j];
if(sdt[i]==st[j+1]) ++j;
if(j==n)
{
if(i-n+1<=1500000)
{
if(i<=1500000) ++ans;
else ++ans,++ans2;
}
j=nxt[j];
}
}
printf("%lld",ans*20000-ans2);
return 0;
} |
#include <bits/stdc++.h>
// #include <atcoder/modint>
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define INF 2000000000000000000
#define ll long long
#define ld long double
#define pll pair<ll, ll>
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll score(string S) {
vector<ll> num(10, 0);
for (ll i = 0; i < 5; ++i) {
num.at(S.at(i) - '0') += 1;
}
ll res = 0;
for (ll i = 0; i < 10; ++i) {
res += i * pow(10, num.at(i));
}
return res;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll K;
cin >> K;
string S, T;
cin >> S >> T;
// ll s = score(S), t = score(T);
// cout << s << ' ' << t << "\n";
vector<ll> num(10, 0);
for (ll i = 0; i < 4; ++i) {
num.at(S.at(i) - '0') += 1;
num.at(T.at(i) - '0') += 1;
}
// for (ll i = 0; i < 10; ++i) {
// cout << num.at(i) << " ";
// }
// cout << "\n";
ll w = 0;
for (ll i = 1; i < 10; ++i) {
S.at(4) = (char)('0' + i);
ll s = score(S);
for (ll j = 1; j < 10; ++j) {
T.at(4) = (char)('0' + j);
ll t = score(T);
if (s > t) {
if (i != j) {
w += max(0ll, (K - num.at(i)) * (K - num.at(j)));
}
else {
w += max(0ll, (K - num.at(i)) * (K - num.at(i) - 1));
}
}
}
}
ll sum = (9 * K - 8) * (9 * K - 9);
// cout << w << ' ' << sum << "\n";
cout << fixed << setprecision(15);
cout << (ld)w / sum << "\n";
}
| //int a = s - '0'; ๆๅญใใๆฐๅญ
//ๅฐๆๅญใใๅคงๆๅญ
//transform(a.begin(), a.end(), a.begin(), ::toupper);
//map ๅ
จๆข็ดข
//auto begin = p.begin(), end = p.end();
//for (auto it = begin; it != end; it++) {}
//mapใฎใญใผ๏ผit->first mapใฎใใชใฅใผ๏ผit->second
//ๅคงๆๅญๅคๅฎ isupper(ๆๅญ) ๅฐๆๅญๅคๅฎ islower(ๆๅญ)
//do{}while(next_permutation(ALL(้
ๅ)))
//ๅฐๆๅญใซๅฏพๅฟใใๆๅญใณใผใ๏ผS[i] - 'a'
//ๆๅญใณใผใโๅฐๆๅญ๏ผ(char)(ๆฐๅญ+'a')
//ใฐใฉใใฎ่ท้ข:้ฃๆฅ่กๅใงๆฑใ
//boolๅใๅๆๅคใฏTrue
//ๅณถๆธกใใฎๅ้ก๏ผไธญ้ใใผใใซ็็ฎ
//ๆฐใๅคงใใๆใฎๆฏ่ผใฏstringๅใง่กใ
//ๅ
จใฆ0ใซใชใฃใใ่ชฟในใใ->0ใซใชใใใณใซcntใใ
//ไพๅคๅฆ็ใฏๆๅใซใใ
//x = p^m + q^n...ใฎ็ดๆฐใฎๅๆฐ:(n+1)*(m+1)....
//N!ใฎใฉใฎ็ด ๅ ๆฐใงไฝๅๅฒใใใ
//โ1~Nใพใงใฎๆฐใใใใใใฉใฎ็ด ๅ ๆฐใงไฝๅๅฒใๅใใใใฎๅ
//ใใบใซใฎๅ้ก->ไธ่ฌๅใใฆๅ
จๆข็ดข
//stack<ll> s;
//s.push(่ฆ็ด );s.top();s.pop();
//queue<ll> q;
//q.push(่ฆ็ด );q.front();q.pop();
//ๅใไฝๆฅญ็นฐใ่ฟใ็ณปใฎๅ้ก๏ผๅๆๅ
ใ่ฆใคใใ
//้ๅๆฐ๏ผN/2.0ใงๅคๅฎ
//ๅชๅ
ๅบฆไปใใญใฅใผ
//priority_queue<
//ll,
//vector<ll>
//> q;
#include <bits/stdc++.h>
#define rep(i,N) for(int i = 0; i < N;i++)
#define ALL(a) (a).begin(),(a).end()
#define ll long long int
#define PI 3.14159265358979323846264338327950L
using namespace std;
//ๅฒใใใค
const ll MOD = (pow(10, 9) + 7);
// K้ฒๆฐใงใฎNใฎๆกๆฐ
ll dig(ll N, ll K) {
ll dig = 0;
while (N) {
dig++;
N /= K;
}
return dig;
}
// a,bใฎๆๅคงๅ
ฌ็ดๆฐ
ll gcd(ll a, ll b) {
if (b == 0) return a;
return gcd(b, a%b);
}
//a,bใฎๆๅฐๅ
ฌๅๆฐ
ll lcm(ll a, ll b) {
ll g = gcd(a, b);
return a / g * b;
}
//้ไน่จ็ฎ
ll f(ll n) {
if (n == 0 || n == 1) return 1;
else return (n * f(n - 1));
}
//Nใฎdๆก็ฎใฎๆฐ
ll dignum(ll N, ll d) {
ll x = pow(10, d);
N %= x;
ll y = pow(10, d - 1);
N /= y;
return N;
}
//Nใdใงไฝๅๅฒใใใ
ll divcnt(ll N, ll d) {
ll ans = 0;
while (1) {
if (N%d == 0) {
ans++;
N /= d;
}
else break;
}
return ans;
}
//็ด ๆฐๅคๅฎ
bool prime(ll num) {
if (num < 2) return false;
else if (num == 2) return true;
else if (num % 2 == 0) return false;
double sqrtNum = sqrt(num);
for (int i = 3; i <= sqrtNum; i += 2) {
if (num % i == 0) return false;
}
return true;
}
//ใใฃใใใใๆฐๅ
vector<ll> memo(pow(10, 6) + 1);
ll fibo(ll n) {
if (n == 1) return 1;
else if (n == 2) return 1;
else if (memo[n] != 0) return memo[n];
else return memo[n] = fibo(n - 1) + f(n - 2);
}
ll RS(ll N, ll P, ll M) {
if (P == 0) return 1;
if (P % 2 == 0) {
ll t = RS(N, P / 2, M);
return t * t % M;
}
return N * RS(N, P - 1, M);
}
vector<int> IntegerToVector(int bit, int N) {
vector<int> S;
for (int i = 0; i < N; ++i) {
if (bit & (1 << i)) {
S.push_back(i);
}
}
return S;
}
int main() {
int N;cin >> N;
vector<ll> x(N), y(N);
rep(i,N) cin >> x[i] >> y[i];
int cnt = 0;
for(int i = 0; i <= N-2; i++){
for(int j = i+1; j <= N-1; j++){
if( abs(y[i]-y[j]) <= abs(x[i]-x[j])){
cnt++;
}
}
}
cout << cnt << endl;
} |
//#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 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;
constexpr ll INF = 1000000099;
constexpr ll LINF = (ll)(1e18 + 99);
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, 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;
}
ll n, a, b, c, d, x;
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
cin >> x >> n;
if(x>=n){
cout<<x-n<<ENDL;
return 0;
}
a=d=1;
b=c=INF;
using Pa = pair<ll, ll>;
map<ll, ll> D; //----------------
priority_queue<Pa, vector<Pa>, greater<Pa>> q;
D[n] = 0;
q.emplace(D[n], n); //ๆ็ญ่ท้ขใใดใผใซ้ ็น็ชๅท
D[x] = LINF;
ll ans=LINF;
while(!q.empty()) {
ll spath = q.top().first;
ll v = q.top().second;
chmin(ans,D[v]+abs(x-v));
q.pop();
if(D.count(v) && D[v] < spath) continue;
D[v] = spath;
rep(i, -4, 5) {
if(v + i < 0) continue;
if(abs(i) < 2 && (v + i) % 2 == 0 &&
(!D.count((v + i) / 2) || D[(v + i) / 2] > D[v] + d * abs(i) + a)) {
D[(v + i) / 2] = D[v] + d * abs(i) + a;
q.emplace(D[(v + i) / 2], (v + i) / 2);
}
if(v + i == 0) { chmin(D[v + i], D[v] + d * abs(i)); }
}
if(log10(v) + log10(d) < 18.5) chmin(D[0], D[v] + v * d);
//cerr<<v<<ENDL;
}
cout << ans << ENDL;
}
//! ( . _ . ) !
//CHECK overflow,vector_size,what to output? | #include<bits/stdc++.h>
using namespace std;
using P=pair<int,int>;
using ll=long long;
template<class T> using heapq = priority_queue<T,vector<T>,greater<T>>;
template<class T> bool chmax(T &a,const T b) {if (a<b) {a=b;return true;} else return false;}
template<class T> bool chmin(T &a,const T b) {if (a>b) {a=b;return true;} else return false;}
#define rep(i,n) for(int i=0; i<(n); i++)
#define srep(i,a,b) for(int i=(a); i<(b); i++)
#define rrep(i,n) for(int i=(n)-1;i>=0; i--)
#define srrep(i,a,b) for(int i=(b)-1; i>=(a); i--)
int main(){
int n,m; cin >> n >> m;
vector<int> A(n+1),W(m);
vector<ll> even(n+1), odd(n+1);
srep(i,1,n+1) cin >> A[i];
for(int &i:W) cin >> i;
sort(A.begin(),A.end());
srep(i,1,n+1){
if (i%2) odd[i]=A[i];
else even[i]=A[i];
odd[i]+=odd[i-1];
even[i]+=even[i-1];
}
ll ans = 200000000000000L;
for(int w:W){
int ac = 0, wa = n+1;
while(ac+1<wa){
int mid = (ac+wa)/2;
if (w<A[mid]) wa=mid;
else ac = mid;
}
chmin(ans,odd[n]-2*odd[ac]-even[n]+2*even[ac]+((ac%2)?w:-w));
}
cout << ans << endl;
} |
/*
* @Author: Luisvacson
* @LastEditors: Luisvacson
* @Descriptions: None
* @Date: 2020-12-12 19:05:25
* @LastEditTime: 2020-12-12 19:45:19
* @FilePath: \VSC\C++\Practice\ARC107C.cpp
*/
#include <bits/stdc++.h>
using namespace std;
#define mod 998244353
#define int long long
int a[55][55];
int n, K;
int fr[105], fc[105];
int findr(int x)
{
if (fr[x] == x)
return x;
else
return findr(fr[x]);
}
int findc(int x)
{
if (fc[x] == x)
return x;
else
return findc(fc[x]);
}
void unityr(int x, int y)
{
fr[findr(x)] = findr(y);
}
void unityc(int x, int y)
{
fc[findc(x)] = findc(y);
}
int mul(int x)
{
if (x == 1 || x == 0)
return 1;
else
return x * mul(x - 1) % mod;
}
int vis[105];
signed main()
{
scanf("%lld%lld", &n, &K);
register int i, j, k;
for (i = 1; i <= n; ++i)
for (j = 1; j <= n; ++j)
scanf("%lld", &a[i][j]);
for (i = 1; i <= n; ++i)
fr[i] = fc[i] = i;
int row = 1, col = 1;
for (i = 1; i <= n; ++i) {
for (j = i + 1; j <= n; ++j) {
bool frflg = true, fcflg = true;
for (k = 1; k <= n; ++k) {
if (a[i][k] + a[j][k] > K) {
frflg = false;
}
if (a[k][i] + a[k][j] > K) {
fcflg = false;
}
}
if (frflg) {
unityr(i, j);
}
if (fcflg) {
unityc(i, j);
}
}
}
for (i = 1; i <= n; ++i) {
int f = findr(i);
++vis[f];
}
for (i = 1; i <= n; ++i)
row *= mul(vis[i]);
memset(vis, 0, sizeof(vis));
for (i = 1; i <= n; ++i) {
int f = findc(i);
++vis[f];
}
for (i = 1; i <= n; ++i)
col *= mul(vis[i]);
printf("%lld\n", row * col % mod);
return 0;
}
/*
10 165
82 94 21 65 28 22 61 80 81 79
93 35 59 85 96 1 78 72 43 5
12 15 97 49 69 53 18 73 6 58
60 14 23 19 44 99 64 17 29 67
24 39 56 92 88 7 48 75 36 91
74 16 26 10 40 63 45 76 86 3
9 66 42 84 38 51 25 2 33 41
87 54 57 62 47 31 68 11 83 8
46 27 55 70 52 98 20 77 89 34
32 71 30 50 90 4 37 95 13 100
*/ | #include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#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 y1 awefakhlrv
using pii = pair<int, int>; using vpii = vector<pii>;
using vi = vector<int>; using vvi = vector<vi>;
using ll = long long;
using pll = pair<ll, ll>; using vpll = vector<pll>;
using vll = vector<ll>; using vvll = vector<vll>;
const int N = 55, MOD = 998244353;
ll a[N][N], fac[N];
struct dsu{
int p[N], sz[N];
dsu(int n) {
FOR(i, 1, n)
p[i] = i, sz[i] = 1;
}
int find(int u) {
if (p[u] == u) return u;
return p[u] = find(p[u]);
}
void merge(int u, int v) {
u = find(u), v = find(v);
if (u == v) return;
if (sz[u] < sz[v]) swap(u, v);
p[v] = p[u], sz[u] += sz[v];
}
};
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
ll n, k; cin >> n >> k;
dsu dr(n), dc(n);
fac[0] = 1;
FOR(i, 1, n)
fac[i] = fac[i - 1] * i % MOD;
vll r(n + 1), c(n + 1);
FOR(i, 1, n) FOR(j, 1, n) {
cin >> a[i][j];
r[i] = max(r[i], a[i][j]);
c[j] = max(c[j], a[i][j]);
}
vector <bool> vis(n + 1);
FOR(i, 1, n) FOR(j, 1, n) {
bool ok = 1;
FOR(l, 1, n)
ok &= (a[i][l] + a[j][l] <= k);
if (ok) dr.merge(i, j);
}
FOR(i, 1, n) FOR(j, 1, n) {
bool ok = 1;
FOR(l, 1, n)
ok &= (a[l][i] + a[l][j] <= k);
if (ok) dc.merge(i, j);
}
ll ans = 1;
FOR(i, 1, n)
if (dr.find(i) == i)
ans = ans * fac[dr.sz[i]] % MOD;
FOR(i, 1, n)
if (dc.find(i) == i)
ans = ans * fac[dc.sz[i]] % MOD;
cout << ans << endl;
} |
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
bool compair(pair<long long, long long> a, pair<long long, long long> b){
if(a.first + a.second != b.first + b.second){
return a.first + a.second > b.first + b.second;
}
else{
return a.first > b.first;
}
}
bool compair2(pair<long long, long long> a, pair<long long, long long> b){
return 2*a.first + a.second > 2*b.first + b.second;
}
int main(){
long long n;
cin >> n;
vector<pair<long long, long long> > v(n);
long long sum_a = 0;
long long sum_t = 0;
for(long long i = 0; i < n; i++){
cin >> v[i].first >> v[i].second;
sum_a += v[i].first;
}
sort(v.begin(), v.end(), compair2);
/*for(long long i = 0; i < n; i++){
printf("%lld %lld\n", v[i].first, v[i].second);
}*/
long long ans = 0;
for(long long i = 0; i < n; i++){
if(sum_a >= sum_t){
ans++;
sum_a -= v[i].first;
sum_t += v[i].first + v[i].second;
if(sum_a < sum_t){
printf("%lld\n", ans);
return 0;
}
}
else{
printf("%lld\n", ans);
return 0;
}
}
printf("%lld\n", ans);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T, typename U> using ordered_map = tree<T, U, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<str> vstr;
#define FOR(i,j,k,in) for(int i=(j); i < (k);i+=in)
#define FORD(i,j,k,in) for(int i=(j); i >=(k);i-=in)
#define REP(i,b) FOR(i,0,b,1)
#define REPD(i,b) FORD(i,b,0,1)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) begin(x), end(x)
#define rsz resize
#define MANY_TESTS int tcase; cin >> tcase; while(tcase--)
const double EPS = 1e-9;
const int MOD = 1e9+7; // 998244353;
const ll INFF = 1e18;
const int INF = 1e9;
const ld PI = acos((ld)-1);
const vi dy = {1, 0, -1, 0, -1, 1, 1, -1};
const vi dx = {0, 1, 0, -1, -1, 1, -1, 1};
#ifdef DEBUG
#define DBG if(1)
#else
#define DBG if(0)
#endif
#define dbg(x) cout << "(" << #x << " : " << x << ")" << endl;
// ostreams
template <class T, class U>
ostream& operator<<(ostream& out, const pair<T, U> &par) {out << "[" << par.first << ";" << par.second << "]"; return out;}
template <class T>
ostream& operator<<(ostream& out, const set<T> &cont) { out << "{"; for( const auto &x:cont) out << x << ", "; out << "}"; return out; }
template <class T, class U>
ostream& operator<<(ostream& out, const map<T, U> &cont) {out << "{"; for( const auto &x:cont) out << x << ", "; out << "}"; return out; }
template<class T>
ostream& operator<<(ostream& out, const vector<T> &v){ out << "["; REP(i, v.size()) out << v[i] << ", "; out << "]"; return out;}
// istreams
template<class T>
istream& operator>>(istream& in, vector<T> &v){ for(auto &x : v) in >> x; return in; }
template<class T, class U>
istream& operator>>(istream& in, pair<T, U> &p){ in >> p.ff >> p.ss; return in; }
//searches
template<typename T, typename U>
T bsl(T lo, T hi, U f){ hi++; T mid; while(lo < hi){ mid = (lo + hi)/2; f(mid) ? hi = mid : lo = mid+1; } return lo; }
template<typename U>
double bsld(double lo, double hi, U f, double p = 1e-9){ int r = 3 + (int)log2((hi - lo)/p); double mid; while(r--){ mid = (lo + hi)/2; f(mid) ? hi = mid : lo = mid; } return (lo + hi)/2; }
template<typename T, typename U>
T bsh(T lo, T hi, U f){ lo--; T mid; while(lo < hi){ mid = (lo + hi + 1)/2; f(mid) ? lo = mid : hi = mid-1; } return lo; }
template<typename U>
double bshd(double lo, double hi, U f, double p = 1e-9){ int r = 3+(int)log2((hi - lo)/p); double mid; while(r--){ mid = (lo + hi)/2; f(mid) ? lo = mid : hi = mid; } return (lo + hi)/2; }
// some more utility functions
template<typename T>
pair<T, int> get_min(vector<T> &v){ typename vector<T> :: iterator it = min_element(v.begin(), v.end()); return mp(*it, it - v.begin());}
template<typename T>
pair<T, int> get_max(vector<T> &v){ typename vector<T> :: iterator it = max_element(v.begin(), v.end()); return mp(*it, it - v.begin());}
template<typename T> bool ckmin(T& a, const T& b){return b < a ? a = b , true : false;}
template<typename T> bool ckmax(T& a, const T& b){return b > a ? a = b, true : false;}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int n; cin >> n;
vll a(n), b(n);
ll sum = 0;
vll ch;
REP(i, n){
cin >> a[i] >> b[i];
sum += a[i];
ch.pb(2 * -a[i] - b[i]);
}
sort(all(ch));
int ans = 0;
REP(i, ch.size()){
sum += ch[i];
ans++;
if(sum < 0) break;
}
cout << ans << "\n";
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef int_fast32_t int32;
typedef int_fast64_t int64;
const int32 inf = 1e9+7;
const int32 MOD = 1000000007;
const int64 llinf = 1e18;
#define YES(n) cout << ((n) ? "YES\n" : "NO\n" )
#define Yes(n) cout << ((n) ? "Yes\n" : "No\n" )
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE\n" : "IMPOSSIBLE\n" )
#define ANS(n) cout << (n) << "\n"
#define REP(i,n) for(int64 i=0;i<(n);++i)
#define FOR(i,a,b) for(int64 i=(a);i<(b);i++)
#define FORR(i,a,b) for(int64 i=(a);i>=(b);i--)
#define all(obj) (obj).begin(),(obj).end()
#define rall(obj) (obj).rbegin(),(obj).rend()
#define fi first
#define se second
#define pb(a) push_back(a)
typedef pair<int32,int32> pii;
typedef pair<int64,int64> pll;
template<class T> inline bool chmax(T& a, T b) {
if (a < b) { a = b; return true; } return false;
}
template<class T> inline bool chmin(T& a, T b) {
if (a > b) { a = b; return true; } return false;
}
int32 solve(string t, string s){
if(s.size() == 0)return inf;
if(t.size() == 0)return 0;
int32 res = inf;
REP(i,s.size()){
if(s[i] == t[0]){
int32 cnt = 0;
string tmp = s;
while(i > 0){
swap(tmp[i],tmp[i-1]);
++cnt;
--i;
}
chmin(res,cnt+solve(t.substr(1),tmp.substr(1)));
break;
}
}
REP(i,s.size()){
if(s[i] > t[0]){
chmin(res,i);
}
}
return res;
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int32 c;
cin >> c;
while(c--){
string s;
cin >> s;
const string t = "atcoder";
int32 ans = solve(t,s);
if(ans == inf){
ANS(-1);
}else{
ANS(ans);
}
}
return 0;
} | #include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define x first
#define y second
using namespace std;
typedef long long ll;
const string tmp = "atcoder";
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(0); cin.tie(0);
int test;
cin >> test;
for (int rep = 1; rep <= test; rep++)
{
string s;
cin >> s;
int n = (int) s.length();
int pos = -1;
for (int i = 0; i < n; i++) if (s[i] != 'a') {pos = i; break;}
if (pos == -1) {cout << "-1\n"; continue;}
if (tmp < s) {cout << "0\n"; continue;}
if (s[pos] > 't') cout << pos - 1 << '\n';
else cout << pos << '\n';
}
return 0;
} |
#include <bits/stdc++.h>
#ifdef mlocal
#include "./Competitive-Code-Library/snippets/prettyprint.h"
#endif
using namespace std;
#define for_(i, s, e) for (int i = s; i < (int) e; i++)
#define for__(i, s, e) for (ll i = s; i < e; i++)
typedef long long ll;
typedef vector<int> vi;
typedef array<int, 2> ii;
#define endl '\n'
// Ask for sum 1 -> n for full (one based indexing)
class BIT {
private: vector<ll> tree; int n;
int LSOne(int x) {
return x&(-x);
}
public:
BIT(int x) {
n = x;
tree.resize(n+1);
}
ll sum(int a) {
ll sum = 0;
for (; a > 0; a -= LSOne(a)) sum += tree[a];
return sum;
}
ll sum(int a, int b) {
return sum(b) - (a == 1 ? 0 : sum(a-1));
}
void update(int p, ll v) {
for (; p < n+1; p += LSOne(p)) tree[p] += v;
}
};
int main() {
#ifdef mlocal
freopen("test.in", "r", stdin);
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m, q; cin >> n >> m >> q;
vector<array<int, 3>> qrs(q);
map<int, int> mp;
for_(i, 0, q) {
for_(j, 0, 3) cin >> qrs[i][j];
qrs[i][1] -= 1;
mp[qrs[i][2]] = 1;
}
int pt = 3;
for (auto &i: mp) mp[i.first] = pt++;
vector<BIT> tree(2, BIT(pt+1)), ctTree(2, BIT(pt+1));
ll ans = 0;
vi a(n, 2), b(m, 2);
ctTree[0].update(2, n); ctTree[1].update(2, m);
vector<ll> rawa(n, 0), rawb(m, 0);
for_(i, 0, q) {
ll raw = qrs[i][2];
qrs[i][2] = mp[qrs[i][2]];
if (qrs[i][0] == 1) {
if (a[qrs[i][1]] < qrs[i][2]) {
ans -= tree[1].sum(a[qrs[i][1]]+1, qrs[i][2]);
} else if (a[qrs[i][1]] > qrs[i][2]) {
ans += tree[1].sum(qrs[i][2]+1, a[qrs[i][1]]);
}
ans -= ctTree[1].sum(1, a[qrs[i][1]]) * rawa[qrs[i][1]];
ctTree[0].update(a[qrs[i][1]], -1);
tree[0].update(a[qrs[i][1]], -rawa[qrs[i][1]]);
a[qrs[i][1]] = qrs[i][2];
rawa[qrs[i][1]] = raw;
ctTree[0].update(a[qrs[i][1]], 1);
tree[0].update(a[qrs[i][1]], rawa[qrs[i][1]]);
ans += ctTree[1].sum(1, a[qrs[i][1]]) * rawa[qrs[i][1]];
} else {
if (b[qrs[i][1]] < qrs[i][2]) {
ans -= tree[0].sum(b[qrs[i][1]], qrs[i][2]-1);
} else if (b[qrs[i][1]] > qrs[i][2]) {
ans += tree[0].sum(qrs[i][2], b[qrs[i][1]]-1);
}
ans -= ctTree[0].sum(1, b[qrs[i][1]]-1) * rawb[qrs[i][1]];
ctTree[1].update(b[qrs[i][1]], -1);
tree[1].update(b[qrs[i][1]], -rawb[qrs[i][1]]);
b[qrs[i][1]] = qrs[i][2];
rawb[qrs[i][1]] = raw;
ctTree[1].update(b[qrs[i][1]], 1);
tree[1].update(b[qrs[i][1]], rawb[qrs[i][1]]);
ans += ctTree[0].sum(1, b[qrs[i][1]]-1) * rawb[qrs[i][1]];
}
cout << ans << endl;
}
}
| #include <vector>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
template <class E> struct FenwickTree {
int _n;
std::vector<E> data;
FenwickTree(int n) : _n(n), data(n) { }
void add(int p, E x) {
++p;
while (p <= _n) {
data[p - 1] += x;
p += p & -p;
}
}
E sum(int r) const {
E s = 0;
while (r > 0) {
s += data[r - 1];
r -= r & -r;
}
return s;
}
E sum(int l, int r) const {
return sum(r) - sum(l);
}
};
long long proc(
std::vector<int> &cur,
int x,
int y,
FenwickTree<int> &ftcnt0,
FenwickTree<long long> &ftsum0,
const FenwickTree<int> &ftcnt1,
const FenwickTree<long long> &ftsum1,
const std::vector<int> &arr)
{
int v = cur[x];
long long dem = (long long)ftcnt1.sum(0, v) * arr[v];
long long cre = (long long)ftcnt1.sum(0, y) * arr[y];
long long adj = (long long)ftsum1.sum(y, v);
ftsum0.add(v, -arr[v]);
ftcnt0.add(v, -1);
cur[x] = y;
ftsum0.add(y, arr[y]);
ftcnt0.add(y, 1);
return cre + adj - dem;
}
int main(int argc, char **argv)
{
int n, m, q;
scanf("%d %d %d", &n, &m, &q);
std::set<int> s = { 0 };
std::vector<int> ta(q), xa(q), ya(q);
for (int i = 0; i < q; i++) {
scanf("%d %d %d", &ta[i], &xa[i], &ya[i]);
xa[i]--;
s.insert(ya[i]);
}
int ll = s.size();
std::vector<int> arr(ll);
std::map<int, int> map;
int ii = 0;
for (auto &k : s) {
arr[ii] = k;
map.emplace(k, ii);
++ii;
}
FenwickTree<long long> ftsuma(ll);
FenwickTree<long long> ftsumb(ll);
FenwickTree<int> ftcnta(ll);
FenwickTree<int> ftcntb(ll);
std::vector<int> cura(n);
std::vector<int> curb(m);
ftcnta.add(0, n);
ftcntb.add(0, m);
long long sum = 0;
for (int i = 0; i < q; i++) {
int cy = map[ya[i]];
sum += (ta[i] == 1) ? proc(cura, xa[i], cy, ftcnta, ftsuma, ftcntb, ftsumb, arr)
: proc(curb, xa[i], cy, ftcntb, ftsumb, ftcnta, ftsuma, arr);
printf("%lld\n", sum);
}
return 0;
} |
# include<bits/stdc++.h>
using namespace std;
# define l long long
# define db double
# define rep(i,a,b) for(l i=a;i<b;i++)
# define vi vector<l>
# define pb push_back
# define mp make_pair
# define ss second
# define ff first
# define pii pair<l,l>
# define trvi(v,it) for(vi::iterator it=v.begin();it!=v.end();++it)
# define read(a) freopen(a,"r",stdin)
# define write(a) freopen(a,"w",stdout)
# define io ios::sync_with_stdio(false)
#define vertex first.second
#define dist first.first
#define parent second
const l MOD=1e9+7;
const l N=3e5+5;
const l INF=1e14;
void solve() {
l n;
cin>>n;
vi a(n);
rep(i,0,n) {
cin>>a[i];
}
l prefix=0, curMax=0, maxPrefix=0;
l sum=0;
rep(i,0,n) {
maxPrefix=max(maxPrefix,sum+a[i]);
curMax=max(curMax,prefix+maxPrefix);
// cout<<"maxPrefix,curMax,prefix "<<maxPrefix<<" "<<curMax<<" "<<prefix<<" "<<a[i]<<"\n";
sum+=a[i];
prefix+=sum;
}
cout<<curMax<<"\n";
return;
}
int main(){
io;
int t;
// cin >> t;
rep(i,0,1) {
solve();
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ar array
#define ll long long
const int MAX_N = 1e5 + 1;
const ll MOD = 1e9 + 7;
const ll INF = 1e9;
int findMaxGCD(int arr[], int n)
{
// Calculating MAX in array
int high = 0;
for (int i = 0; i < n; i++)
high = max(high, arr[i]);
// Maintaining count array
int count[high + 1] = {0};
for (int i = 0; i < n; i++)
count[arr[i]]++;
// Variable to store the
// multiples of a number
int counter = 0;
// Iterating from MAX to 1
// GCD is always between
// MAX and 1. The first
// GCD found will be the
// highest as we are
// decrementing the potential
// GCD
for (int i = high; i >= 1; i--)
{
int j = i;
counter = 0;
// Iterating from current
// potential GCD
// till it is less than
// MAX
while (j <= high)
{
// A multiple found
if(count[j] >=2)
return j;
else if (count[j] == 1)
counter++;
// Incrementing potential
// GCD by itself
// To check i, 2i, 3i....
j += i;
// 2 multiples found,
// max GCD found
if (counter == 2)
return i;
}
}
}
void solve() {
int a, b; cin >> a >> b;
int n = b - a + 1;
int arr[n];
for (int i = a, j = 0; i <= b; i++, j++) arr[j] = i;
cout << findMaxGCD(arr, n) << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int tc = 1;
// cin >> tc;
for (int t = 1; t <= tc; t++) {
// cout << "Case #" << t << ": ";
solve();
}
} |
#include <bits/stdc++.h>
#define eprintf(args...) fprintf(stderr, args)
#define rep(i, n) for (int i = 0; i < (int)(n); ++ i)
const int mxn = 1e5 + 5;
const int mod = 1e9 + 7;
int f[mxn];
int n, a[mxn];
int main() {
scanf("%d", &n);
rep(i, n) scanf("%d", &a[i]);
f[0] = 1; f[1] = 2;
for (int i = 1; i < n; ++ i) f[i + 1] = (f[i - 1] + f[i]) % mod;
int ans = 0;
rep(i, n) {
int coef = 1LL * (i ? f[i - 1] : 1) * f[n - 1 - i] % mod;
if (i) coef = (coef + mod - 1LL * (i - 1 ? f[i - 2] : 1) * (n - 1 - i ? f[n - 2 - i] : 1) % mod) % mod;
(ans += 1LL * coef * a[i] % mod) %= mod;
}
printf("%d\n", ans);
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define ll long long int
ll dp[100009][2];
vector<ll>vec;
ll M;
ll n;
ll call(ll pos,ll pev)
{
if(pos==0)
{
return 1;
}
if(dp[pos][pev]!=-1)
{
return dp[pos][pev];
}
ll x,y;
if(pev==0)
{
x=call(pos-1,0);
y=call(pos-1,1);
x=(x+y)%M;
}
else
{
x=call(pos-1,0);
x=x%M;
}
return dp[pos][pev]=x;
}
int main()
{
ll a,b,c,d,e,i,j,k,l,m,x,y,t,p;
cin>>n;
for(i=0;i<n;i++)
{
cin>>a;
vec.push_back(a);
}
m=M=(1e9)+7;
memset(dp,-1,sizeof(dp));
for(i=n;i>=0;i--)
{
x=call(i,0);
x=call(i,1);
}
dp[0][0]=dp[0][1]=1;
if(n==1)
{
cout<<vec[0]<<endl;
return 0;
}
if(n==2)
{
x=(vec[0]*2);
cout<<x<<endl;
return 0;
}
ll ans=0;
x=(dp[n-1][0]*vec[0])%M;
ans=x;
//cout<<ans<<endl;
x=(dp[n-2][0]*vec[1])%M;
ans=(ans+x)%M;
x=(dp[n-2][1]*vec[1])%M;
ans=(ans-x)%m;
ans=(ans+M)%M;
//cout<<ans<<endl;
for(i=2;i<n;i++)
{
a=dp[i-1][0];
b=dp[(n-1)-i][0];
//cout<<a<<" "<<b<<endl;
b=(a*b)%M;
b=(b*vec[i])%M;
a=dp[i-1][1];
c=dp[(n-1)-i][1];
//cout<<a<<" "<<c<<endl;
c=(c*a)%M;
c=(c*vec[i])%M;
b=(b-c)%M;
b=(b+M)%M;
ans=(ans+b)%M;
}
cout<<ans<<endl;
}
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <math.h>
#include <queue>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <ctime>
#include <iomanip>
#include <fstream>
#include <random>
#include <numeric>
using namespace std;
#define ALL(c) (c).begin(),(c).end()
#define PB push_back
#define IN(x,c) (find(c.begin(),c.end(),x) != (c).end())
#define REP(i,n) for (int i=0;i<(int)(n);i++)
#define FOR(i,a,b) for (int i=(a);i<=(b);i++)
#define INIT(a,v) memset(a,v,sizeof(a))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
template<class A, class B> A cvt(B x) { stringstream ss; ss<<x; A y; ss>>y; return y; }
#define SPC << " " <<
#define DEBUG(x) cerr << #x << " = "; cerr << x << endl;
#define DEBUG_ITER(x) cerr << #x << " = "; for (auto _ : x) cerr << _ << ' '; cerr << endl;
typedef pair<int,int> PII;
typedef long long int64;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef vector<double> VD;
int main() {
//freopen("test.in","r",stdin);
int64 b,c;
cin >> b >> c;
vector<pair<int64,int64> > seg;
seg.PB({b-c/2, 1}); seg.PB({b+1, -1});
if (c>=1) { seg.PB({-b-(c-1)/2, 1}); seg.PB({-b+(c-1)/2+1, -1}); }
if (c>=2) { seg.PB({b-(c-2)/2, 1}); seg.PB({b+(c-2)/2+1, -1}); }
sort(ALL(seg));
int64 cov=0,y=-3e18;
int64 r=0;
for (auto [x, t] : seg) {
if (cov>0) r+=x-y;
cov+=t;
y=x;
}
cout << r << endl;
return 0;
}
| #include <bits/stdc++.h>
const long long INF = 1e9;
const long long MOD = 1e9 + 7;
//const long long MOD = 998244353;
const long long LINF = 1e18;
using namespace std;
#define YES(n) cout << ((n) ? "YES" : "NO" ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No" ) << endl
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE" : "IMPOSSIBLE" ) << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible" ) << endl
#define dump(x) cout << #x << " = " << (x) << endl
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) for(int i=0;i<(n);++i)
#define REPR(i,n) for(int i=n;i>=0;i--)
#define COUT(x) cout<<(x)<<endl
#define SCOUT(x) cout<<(x)<<" "
#define VECCOUT(x) for(auto&youso_: (x) )cout<<right<<setw(10)<<youso_<<" ";cout<<endl
#define ENDL cout<<endl
#define CIN(...) int __VA_ARGS__;CINT(__VA_ARGS__)
#define LCIN(...) long long __VA_ARGS__;CINT(__VA_ARGS__)
#define SCIN(...) string __VA_ARGS__;CINT(__VA_ARGS__)
#define VECCIN(x) for(auto&youso_: (x) )cin>>youso_
#define mp make_pair
#define PQ priority_queue<long long>
#define PQG priority_queue<long long,V,greater<long long>>
typedef long long ll;
typedef vector<long long> vl;
typedef vector<long long> vi;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<vl> vvl;
typedef vector<vi> vvi;
typedef vector<vb> vvb;
typedef vector<vc> vvc;
typedef pair<long long, long long> pll;
#define COUT(x) cout<<(x)<<endl
void CINT(){}
template <class Head,class... Tail>
void CINT(Head&& head,Tail&&... tail){
cin>>head;
CINT(move(tail)...);
}
template<class T>
void mod(T &x) {
x %= MOD;
x += MOD;
x %= MOD;
}
ll GCD(ll a, ll b) {
if(b == 0) return a;
else return GCD(b, a%b);
}
struct COMB{
vl fact, fact_inv, inv;
void init_nCk(long long SIZE) {
fact.resize(SIZE + 5);
fact_inv.resize(SIZE + 5);
inv.resize(SIZE + 5);
fact.at(0) = fact.at(1) = fact_inv.at(0) = fact_inv.at(1) = inv.at(1) = 1;
for(long long i = 2; i < SIZE + 5; i++) {
fact.at(i) = fact.at(i - 1)*i%MOD;
inv.at(i) = MOD - inv.at(MOD%i)*(MOD/i)%MOD;
fact_inv.at(i) = fact_inv.at(i - 1)*inv.at(i)%MOD;
}
}
long long nCk (long long n, long long k) {
assert(!(n < k));
assert(!(n < 0 || k < 0));
return fact.at(n)*(fact_inv.at(k)*fact_inv.at(n - k)%MOD)%MOD;
}
};
ll extGCD(ll a, ll b, ll &x, ll &y) {
if(b == 0) {
x = 1;
y = 0;
return a;
}
ll d = extGCD(b, a%b, y, x);
y -= a/b*x;
return d;
}
void Main() {
ll ans = LINF;
LCIN(N);
vl X(N);
VECCIN(X);
vl l{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47};
for(int i = 0; i < (1 << 15); i++) {
vl c(N, 1);
for(int j = 0; j < N; j++) {
bool check = 0;
for(int k = 0; k < 15; k++) {
if(i & (1 << k)) {
if(GCD(l.at(k), X.at(j)) != 1) check = 1;
}
}
if(check) c.at(j) = 0;
}
ll memo = 1;
for(int j = 0; j < N; j++) {
if(c.at(j)) {
memo = 0;
}
}
if(memo == 0) continue;
ll tmp = 1;
for(int j = 0; j < 15; j++) {
if(i & (1 << j)) {
tmp *= l.at(j);
}
}
ans = min(ans, tmp);
}
cout << ans << endl;
}
int main() {
cout << fixed << setprecision(15);
Main();
return 0;
} |
#include<bits/stdc++.h>
typedef long long ll;
#define endl "\n"
#define optimize() ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ff first
#define ss second
#define pb push_back
#define testcase ll t; cin>>t; while(t--)
using namespace std;
int main() {
optimize();
ll n;
cin>>n;
ll cnt = 0, ans = 0;
for(ll i = 0; i < 1000000000; i++) {
cnt += i;
if (cnt >= n) {
ans = i;
break;
}
}
cout<<ans<<endl;
} | //ABC_197_C_
//
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
#define reps(i,s,n) for(int i=s;i<n;++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main()
{
int n;
cin >> n;
vector<ll> a(n);
rep(i,n) cin >> a[i];
ll ans = 1e18;
rep(i,1<<20)
{
ll bit = i;
ll tmp = a[0];
vector<ll> v;
rep(j,n-1)
{
ll bit1 = bit & (1<<j);
ll bit2 = bit & (1<<(j+1));
if(bit1*2 == bit2)
{
tmp = tmp | a[j+1];
}
else
{
v.emplace_back(tmp);
tmp = a[j+1];
}
}
v.emplace_back(tmp);
ll now = 0;
for(auto vi:v) now = now ^ vi;
ans = min(ans,now);
}
cout << ans << endl;
return 0;
} |
#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
#include <queue>
#include <string>
using namespace std;
int main()
{
string str;
cin >> str;
vector<int> dig(10, 0);
for(int i=0; i < str.size(); i++)
dig[str[i]-'0']++;
for(int i=0; i < 10; i++) dig[i] = min(dig[i], 3);
vector<int> pos;
for(int i=0; i < 10; i++) for(int j=0; j < dig[i]; j++) pos.push_back(i);
int size = pos.size();
if(size == 1)
{
if(pos[0]%8==0)
{
cout << "Yes" << endl;
return 0;
}
}
else if(size==2)
{
int a=pos[0], b=pos[1];
if((a+2*b)%8==0 || (b+2*a)%8==0)
{
cout << "Yes" << endl;
return 0;
}
}
else
{
for(int i=0; i < size; i++)
for(int j=0; j < size; j++)
if(j!=i)
for(int k=0; k < size; k++)
if(k!=i && k!=j)
if((pos[i]+2*pos[j]+4*pos[k])%8==0)
{
cout << "Yes" << endl;
return 0;
}
}
cout << "No" << endl;
return 0;
} | /* author:hellojim */
#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<set>
#include<map>
#include<utility>
#include<queue>
#include<vector>
#include<stack>
#include<sstream>
#include<algorithm>
#define forn(i,a,b)for(int i=(a),_b=(b);i<=_b;i++)
#define fore(i,b,a)for(int i=(b),_a=(a);i>=_a;i--)
#define rep(i,n)for(int i=0,_n=n;i<n;i++)
#define ll long long
#define pii pair<int,int>
#define vi vector<int>
#define vpii vector<pii>
#define m_p make_pair
#define re return
#define pb push_back
#define si set<int>
#define ld long double
#define X first
#define Y second
#define st string
#define ull unsigned long long
#define mod 1000000007
#define INF 1000000007
#define x1 XZVJDFADSPFOE
#define y1 GASDIJSLDAEJF
#define x2 DFDAJKVOHKWIW
#define y2 PSFSAODSXVNMQ
#define LLINF 0x3f3f3f3f3f3f3f3fLL
#define foreach(i,c) for(__typeof(c.begin())i=(c.begin());i!=(c).end();i++)
using namespace std;
inline void read(int &x)
{
short negative=1;
x=0;
char c=getchar();
while(c<'0' || c>'9')
{
if(c=='-')
negative=-1;
c=getchar();
}
while(c>='0' && c<='9')
x=(x<<3)+(x<<1)+(c^48),c=getchar();
x*=negative;
}
ll quickpower(ll n,ll k){
ll ans=1;
while(k){
if(k%2){
ans*=n;
ans%=mod;
}
n*=n;
n%=mod;
k/=2;
}
return ans;
}
string int_to_string(int n)
{
string s="";
while(n)
{
int now=n%10;
s+=now+'0';
n/=10;
}
reverse(s.begin(),s.end());
return s;
}
int string_to_int(string s)
{
int n=0;
rep(i,s.size())
{
n*=10;
n+=s[i]-'0';
}
return n;
}
int main()
{
ios::sync_with_stdio(0);
//think twice,code once
int x;
cin>>x;
if(x<0)x=0;
cout<<x<<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 int long long
//#pragma GCC optimize("Ofast")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);
#define fastio ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define all(x) x.begin(), x.end()
#define sz(s) (int)s.size()
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define s second
#define f first
typedef pair < long long, long long > pll;
typedef pair < int, int > pii;
typedef unsigned long long ull;
typedef vector < pii > vpii;
typedef vector < int > vi;
typedef long double ldb;
typedef long long ll;
typedef double db;
typedef tree < int, null_type, less < int >, rb_tree_tag, tree_order_statistics_node_update > ordered_set;
const int inf = 1e9, maxn = 2e5 + 48, mod = 998244353, N = 5012;
const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}, block = 300;
const pii base = mp(1171, 3307), Mod = mp(1e9 + 7, 1e9 + 9);
const db eps = 1e-12, pi = acos(-1);
const ll INF = 1e18;
void add (int &x, int y) {
x += y;
if (x >= mod)
x -= mod;
}
int n, m, dp[N][N], pw[N][N], s[N];
main () {
cin >> n >> m;
pw[0][0] = 1;
for (int i = 1; i < N; ++i) {
pw[i][0] = 1;
for (int j = 1; j < N; ++j)
pw[i][j] = pw[i][j - 1] * 1ll * i % mod;
}
for (int len = 1; len < N; ++len)
for (int i = 1; i <= m; ++i) {
dp[len][i] = pw[m - 1][len] - pw[m - i][len];
if (dp[len][i] < 0)
dp[len][i] += mod;
add(s[len], dp[len][i]);
}
int ans = 0;
for (int l = 1; l <= n; ++l) {
for (int r = l + 2; r <= n; ++r)
add(ans, pw[m][l - 1] * 1ll * s[r - l - 1] % mod * pw[m][n - r] % mod);
add(ans, pw[m - 1][l - 1] * 1ll * m % mod * pw[m][n - l] % mod);
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
//const int MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
const double pi = acos(-1.0);
const double EPS = 1e-10;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};
double dp[101][101][101];
double rec(int i, int j, int k){
if(i >= 100 || j >= 100 || k >= 100) return 0;
if(dp[i][j][k] > -1) return dp[i][j][k];
dp[i][j][k] = 1;
int s = i+j+k;
dp[i][j][k] += double(i)/double(s)*rec(i+1, j, k);
dp[i][j][k] += double(j)/double(s)*rec(i, j+1, k);
dp[i][j][k] += double(k)/double(s)*rec(i, j, k+1);
return dp[i][j][k];
}
int main(){
int A, B, C;
cin >> A >> B >> C;
rep(i, 101) rep(j, 101) rep(k, 101) dp[i][j][k] = -2;
cout << sp(15) << rec(A, B, C) << endl;
} |
// R SAI KRISHNA
#include <bits/stdc++.h>
#define for0(i, n) for (int i = 0; i < (ll)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (ll)(n); ++i)
#define forc(i, l, r) for (int i = (ll)(l); i <= (ll)(r); ++i)
#define forr0(i, n) for (int i = (ll)(n) - 1; i >= 0; --i)
#define forr1(i, n) for (int i = (ll)(n); i >= 1; --i)
#define pb push_back
#define fi first
#define debug(x) cout << #x << " -> " << x << endl;
#define endl '\n'
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin, (x).rend()
#define tr(c,i) for(__typeof__((c)).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define sz(a) ll((a).size())
#define mod 1000000007
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef double ld;
void debug_vec(vector<ll>&v){
cout << '[';
for(auto i:v)cout << i << ", "; cout << "]";
cout << "\n";
}
void debug_set(set<ll>&st){
cout << "{";
for(auto i:st)cout << i << ", ";
cout << "}";
cout << "\n";
}
void fast()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
bool prime[1000001];
void sieve()
{
memset(prime, true, sizeof(prime));
prime[1]=false;
for (int p=2; p*p<=1000000; p++)
{
if (prime[p] == true)
{
for (int i=p*p; i<=1000000; i += p)
prime[i] = false;
}
}
}
void print(bool f){
if(f)cout << "YES\n";
else cout << "NO\n";
}
void google(int x){
cout << "Case #" << x << ": " ;
}
void input_array(vector<ll>&arr){
for(auto &i : arr){
cin >> i;
}
}
void solve()
{
long double x,y,r;
cin >> x >> y >> r;
r += (1e-14);
long double lx,rx,ly,ry;
lx = ceil(x - r);
rx = floor(x + r);
ll sum = 0;
for(int i = lx ;i <= rx ; i++) {
long double res = ((long double)(sqrtl((long double)(r*r) - (long double)(x - i)*(x - i))));
ll left_limit = ceil(y - res);
ll right_limit = floor(y + res);
sum += (ll)(right_limit - left_limit + 1) ;
}
cout << sum << endl;
}
int main()
{
fast();
/*ll t;
cin >> t;
for(int tt = 1;tt <= t ;tt++)*/
solve();
return 0;
}
| #define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <array>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
#include <memory>
#include <regex>
using namespace std;
const int MAX = 200000;
const int DECIMAL = 10000;
long long solve(long long cy, long long cx, long long r)
{
cx = ((cx % DECIMAL) + DECIMAL) % DECIMAL;
cy = ((cy % DECIMAL) + DECIMAL) % DECIMAL;
long long ans = 0;
for(int y=(cy+DECIMAL-1)/DECIMAL*DECIMAL; y<cy+r; y+=DECIMAL){
long long left = cx;
long long right = cx + r;
while(left < right){
long long x = (left + right + 1) / 2;
long long dy = y - cy;
long long dx = x - cx;
if(dy * dy + dx * dx <= r * r)
left = x;
else
right = x - 1;
}
ans += left / DECIMAL;
}
return ans;
}
int main()
{
double cy0, cx0, r0;
cin >> cx0 >> cy0 >> r0;
long long cy = (long long)round(cy0 * DECIMAL);
long long cx = (long long)round(cx0 * DECIMAL);
long long r = (long long)round(r0 * DECIMAL);
long long ans = 0;
if(cx % DECIMAL == 0 && cy % DECIMAL == 0)
++ ans;
for(int i=0; i<4; ++i){
ans += solve(cy, cx, r);
swap(cx, cy);
cy *= -1;
}
cout << ans << endl;
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 emplace_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 = 1e6 + 5;
LL res[MAXN];
int main(){
int l,r;scanf("%d%d",&l,&r);
LL ans = 0;
FOR(g,2,r){
int len = r/g - (l-1)/g;
res[g] = 1ll*len*len;
}
ROF(i,r,2){
for(int j = i+i;j <= r;j += i) res[i] -= res[j];
}
FOR(i,2,r) ans += res[i];
FOR(g,l,r){
if(g == 1) continue;
ans -= 2*(r/g) - 1;
}
printf("%lld\n",ans);
return 0;
}
// gcd = i ็ๆฐ็ไธชๆฐ
| #include <bits/stdc++.h>
using namespace std;
#define N 1000001
#define int1 long long int
int1 lpf[N], mobius[N];
void least_prime_factor()
{
for (int1 i = 2; i < N; i++)
if (!lpf[i])
for (int1 j = i; j < N; j += i)
if (!lpf[j]) lpf[j] = i;
}
void Mobius()
{
for (int1 i = 1; i < N; i++) {
if (i == 1) mobius[i] = 1;
else {
if (lpf[i / lpf[i]] == lpf[i]) mobius[i] = 0;
else mobius[i] = -1 * mobius[i / lpf[i]];
}
}
}
int1 gcd_pairs(int1 a[], int1 n)
{
int1 maxi = 0;
int1 fre[N] = { 0 };
for (int1 i = 0; i < n; i++) {
fre[a[i]]++;
maxi = max(a[i], maxi);
}
least_prime_factor();
Mobius();
int1 ans = 0;
for (int1 i = 1; i <= maxi; i++) {
if (!mobius[i])
continue;
int1 temp = 0;
for (int1 j = i; j <= maxi; j += i)
temp += fre[j];
ans += temp * (temp - 1) / 2 * mobius[i];
}
return ans;
}
int main()
{
int1 l , r;
cin>>l>>r;
int1 a[r-l+1];
for(int1 i=0;i<=r-l;i++) a[i] = i+l;
int1 n = r-l+1;
int1 x = 2*gcd_pairs(a, n);
int1 total = (r-l)*(r-l+1);
int1 y = 0;
for(int1 i=0;i<=(r-l)/2;i++)
{
int1 tmp = a[i];
for(int1 j=i+tmp; j<=r-l; j += tmp) y++;
}
if(a[0] == 1) y -= r-l;
y *= 2;
cout<<total-x-y<<endl;
return 0;
}
|
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937/*_64*/ rng(chrono::steady_clock::now().time_since_epoch().count());
#define printArr(arr, n) cerr << #arr << " = ["; for(int i=0; i<(n); i++){if (i) cerr << ", "; cerr << arr[i];} cerr << "]\n"
template<class A> ostream& operator<<(ostream &cout, vector<A> const &v) {cout << "["; for(int i = 0; i < v.size(); i++) {if (i) cout << ", "; cout << v[i];} return cout << "]";};
template<class A, class B> ostream& operator<<(ostream &cout, const pair<A,B> &x) {return cout << "(" <<x.first << ", " << x.second << ")";};
void _print() {cerr << "]\n";}
template <class T, class... V> void _print(T t, V... v) {cerr << t; if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#define fi first
#define se second
#define SZ(x) (int)((x).size())
#define pii pair<int,int>
vector<int> place[26];
vector<int> pos[26];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
string s;
cin >> s;
int n = SZ(s);
for (int q = 0; q < n; q++) {
pos[s[q]-'a'].push_back(q);
}
for (int q = n-2; q >= 0; q--) {
if (s[q] == s[q+1]) {
place[s[q]-'a'].push_back(q);
}
}
for (int q = 0; q < 26; q++) {
place[q].push_back(n);
sort(place[q].begin(), place[q].end());
}
ll ans = 0;
for (int q = 0; q < 26; q++) {
for (int w = 0; w < SZ(place[q]); w++) {
if (w == SZ(place[q])-1) {
continue;
} else {
if (place[q][w]+1==place[q][w+1]) continue;
bool between = false;
int last = n;
for (int e = 0; e < 26; e++) {
if (q == e) continue;
auto it = upper_bound(place[e].begin(), place[e].end(), place[q][w]);
if (*it < place[q][w+1]) {
between = true;
last = min(last, *it);
}
}
if (between) {
ll x = n-2-place[q][w];
ll lo = upper_bound(pos[q].begin(), pos[q].end(), place[q][w]+1)-pos[q].begin();
ll hi = lower_bound(pos[q].begin(), pos[q].end(), last)-pos[q].begin();
hi--;
ll dif = max(0LL,hi-lo+1);
x -= dif;
ans += x;
continue;
}
ll x = place[q][w+1]-place[q][w]-2;
ll lo = upper_bound(pos[q].begin(), pos[q].end(), place[q][w]+1)-pos[q].begin();
ll hi = lower_bound(pos[q].begin(), pos[q].end(), place[q][w+1]) - pos[q].begin();
hi--;
ll dif = max(0LL,hi-lo+1);
x -= dif;
ans += x;
}
}
}
cout << ans << "\n";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;
template<class T>
void debug(T itr1, T itr2) { auto now = itr1; while(now<itr2) { cout << *now << " "; now++; } cout << endl; }
#define repr(i,from,to) for (ll i=(ll)from; i<(ll)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i,N) repr(i,0,N)
#define per(i,N) for (ll i=(ll)N-1; i>=0; i--)
#define popcount __builtin_popcount
const ll MOD = pow(10,9)+7;
const ll LLINF = pow(2,61)-1;
const ll INF = pow(2,30)-1;
vector<ll> fac;
void c_fac(ll x=pow(10,7)+10) { fac.resize(x,true); rep(i,x) fac[i] = i ? (fac[i-1]*i)%MOD : 1; }
ll inv(ll a, ll m=MOD) { ll b = m, x = 1, y = 0; while (b!=0) { ll d = a/b; a -= b*d; swap(a,b); x -= y*d; swap(x,y); } return (x+m)%m; }
ll nck(ll n, ll k) { return fac[n]*inv(fac[k]*fac[n-k]%MOD)%MOD; }
ll modpow(ll x, ll p) { ll result = 1, now = 1, pm = x; while (now<=p) { if (p&now) { result = result * pm % MOD; } now*=2; pm = pm*pm % MOD; } return result; }
ll gcd(ll a, ll b) { if (a<b) swap(a,b); return b==0 ? a : gcd(b, a%b); }
ll lcm(ll a, ll b) { return a/gcd(a,b)*b; }
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
struct UnionFind {
vector<ll> par, s, e;
UnionFind(ll N) : par(N), s(N), e(N) { rep(i,N) { par[i] = i; s[i] = 1; e[i] = 0; } }
ll root(ll x) { return par[x]==x ? x : par[x] = root(par[x]); }
ll size(ll x) { return par[x]==x ? s[x] : s[x] = size(root(x)); }
void unite(ll x, ll y) {
ll rx=root(x), ry=root(y);
if (rx!=ry) { s[rx] += s[ry]; e[rx] += e[ry]+1; par[ry] = rx; }
else e[rx]++;
}
bool same(ll x, ll y) { ll rx=root(x), ry=root(y); return rx==ry; }
};
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
int main() {
ll N; cin >> N;
ll a[N], b[N]; rep(i,N) cin >> a[i] >> b[i];
ll MA = 400010;
UnionFind uf(MA);
rep(i,N) uf.unite(a[i],b[i]);
ll result = 0; rep(i,MA) if (uf.root(i)==i) result += min(uf.s[i],uf.e[i]);
cout << result << endl;
return 0;
} |
/*
ALLAH IS ALMIGHTY ******BISMILLAHIR RAHMANIR RAHIM.****** ALLAH IS ALMIGHTY
```````````````````````````````
AUTHOR: ANISUR RAHAMAN
BSMRSTU(SHIICT)
ID:017
............LET's START OUR JOURNEY.............
*/
#include <bits/stdc++.h>
#define I_O ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
//~~~~~~~~~~~~ Sort Form Of Long~~~~~~~~~~~//
#define ll long long int
#define lls int64_t
#define ld long double
#define db double
#define ull unsigned long long int
//~~~~~~~~~~~~~~Pair~~~~~~~~~~~~~~~~~~//
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<db,db>
#define psi pair<string,int>
#define vi vector<int>
#define vl vector<ll>
#define vd vector<db>
#define vs vector<string>
#define vb vector<bool>
#define vpi vector<pii>
#define vpl vector<pll>
#define vpd vector<pdd>
#define vpsi vector<psi>
//~~~~~~~~~~~~~~Vector~~~~~~~~~~~~~~~~~//
#define pb push_back
#define pf push_front
#define MP make_pair
#define in insert
#define ff first
#define ss second
#define al(v) v.begin(),v.end()
#define alr(v) v.rbegin(), v.rend()
#define srt(v) sort(al(v))
#define srtr(v) sort(al(v), greater<int>());
#define len(x) ((ll)(x).length())
#define sz(x) ((ll)(x).size())
#define rev(v) reverse(al(v))
#define btcnt(n) __builtin_popcount(n)
#define acl(v, n) accumulate(al(v), n)
#define eb emplace_back
#define lb(v, kk) lower_bound(al(v), kk)
#define ub(v, kk) upper_bound(al(v), kk)
#define tpu(str) transform(al(str), str.begin(), ::toupper)
#define tpl(str) transform(al(str), str.begin(), ::tolower)
#define cignr cin.ignore(numeric_limits<streamsize>::max(), '\n');
#define mxv(v) *max_element(al(v))
#define mnv(v) *min_element(al(v))
const int MOD = 1e9 + 7;
const ll INF = 2e18;
const int mxn = 2e9 + 9;
const int mxd = 2e5 + 5;
const int mxa = 1e5 + 5;
//~~~~~~~~~~~~~~~~~~Function~~~~~~~~~~~~~~~~~~~~//
lls gcd(lls a, lls b){
if(b == 0LL) return a;
return gcd(b, a % b);
}
lls lcm(lls a, lls b){
return (a / gcd(a, b) * b);
}
//~~~~~~~~~~~~~~~Loops and Short~~~~~~~~~~~~~~~~//
#define PI acos(-1)
#define Cn continue
#define Br break
#define off return 0
#define N '\n'
#define WT(something) auto az = something; cout << az << N;
#define WTS(something) cout << something << N;
#define rep(i, n) for(lls i = 0; (lls)i < n; i++)
#define repn(i, a, b) for(lls i = (lls)(a); i < (lls)(b); i++)
#define repr(i, a, b) for(lls i = (lls)(a) - 1; i >= (lls)(b); i--)
#define test_case() int T; T = 1; while(T--)
using namespace std;
lls maxll(lls x, lls y){ return x > y ? x : y; }
lls minll(lls x, lls y){ return x < y ? x : y; }
// ===================================~~~~~~ SOLUTION STARTS HERE ~~~~~~=================================== //
//int a, b, c, d, e, res = 0, ans = 0, prod = 1, n, m, cnt = 0, k, l, r, s, t, x, y, f, i, j, p;
void Run_Case()
{
lls n, k;
cin >> n >> k;
for (int i = 0; i < k; i++) {
if (n % 200 == 0) n /= 200;
else {
string str = to_string(n);
str += "200";
n = stoll(str);
}
}
cout << n << N;
}
int main()
{
I_O
test_case()
{
Run_Case();
}
off;
}
| #include<bits/stdc++.h>
#define inf 1e18
#define ll long long
#define endl "\n"
#define pb(a) push_back(a)
#define mp(x,y) make_pair(x,y)
#define instr(str) string str; cin>>str;
#define loop(i,x,y) for(int i = x; i < y ; i++ )
#define rev(i,x,y) for(int i = x; i >= y ; i--)
#define in(n) int n; cin>>n;
#define tc int t;cin>>t;while(t--)
#define inarr(arr,n) int arr[n]; for(int i = 0; i < n ; i++){cin>>arr[i];}
#define ff first
#define sec second
#define mem(a,c) mvemset(a,c,sizeof(a))
#define bitcount(x) __builtin_popcountll(x)
#define maploop(x) for(auto itr = x.begin(); itr != x.end();itr++)
int mod = 1000000007;
#define int long long
using namespace std;
bool isp(int x) {
for (int i = 2; i * i <= x; ++i)
if (x % i == 0) return false;
return true;
}
void pog()
{
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
bool sortinrev(const pair<int, int> &a, const pair<int, int> &b)
{
return (a.first > b.first);
}
// bool prime[1000000 + 1];
// void SieveOfEratosthenes()
// {
// memset(prime, true, sizeof(prime));
// prime[1] = false;
// for (int p = 2; p * p <= 10000000; p++) {
// // If prime[p] is not changed, then it is a prime
// if (prime[p] == true) {
// // Set all multiples of p to non-prime
// for (int i = p * 2; i <= 10000000; i += p)
// prime[i] = false;
// }
// }
// }
bool prime_check(int n)
{
bool flag = 1;
for (int i = 2; i <= sqrt(n); i++) {
// If n is divisible by any number between
// 2 and n/2, it is not prime
if (n % i == 0) {
flag = 0;
break;
}
}
return flag;
}
long long power( long long x,
int y, int p)
{
long long res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
while (y > 0) {
// If y is odd, multiply x with result
if (y & 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
bool sortbysec(const pair<int, int> &a,
const pair<int, int> &b)
{
return (a.second < b.second);
}
/*************************************************************************/
void Main()
{
in(n)
inarr(arr, n)
inarr(arr1, n)
int res[n];
res[0] = arr[0] * arr1[0];
int maxi[n];
maxi[0] = arr[0];
loop(i, 1, n)
{
maxi[i] = max(maxi[i - 1], arr[i]);
}
loop(i, 1, n)
{
res[i] = max(res[i - 1], arr[i] * arr1[i]);
res[i] = max(res[i], maxi[i - 1] * arr1[i]);
}
loop(i, 0, n)
{
cout << res[i] << endl;
}
}
int32_t main()
{
srand(time(0));
pog();
int t = 1 ;
// int t;
// cin >> t;
while (t--)
{
Main();
}
#ifndef ONLINE_JUDGE
cout << "\nTime Elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " sec\n";
#endif
} |
#include <bits/stdc++.h>
using namespace std;
// #include<atcoder/all>
// using namespace atcoder;
using ll = long long;
#define rep(i, e) for (ll i = (ll)0; i < (ll)e; i ++)
#define repd(i, s) for (ll i = (ll)s; i > (ll)0; i--)
#define reps(i, s, e, p) for (ll i = (ll)s; i < (ll)e; i += p)
#define repsd(i, s, e, p) for (ll i = (ll)s; i > (ll)e; i -= p)
#define ALL(vec) vec.begin(), vec.end()
int main(){
int n, q;
string s;
cin >> n >> s >> q;
int t, a, b, r = 0;
rep (i, q){
cin >> t >> a >> b;
if (t == 1){
if (r == 0){
swap(s[a - 1], s[b - 1]);
}else{
if (a <= n) a += n;
else a -= n;
if (b <= n) b += n;
else b -= n;
swap(s[a - 1], s[b - 1]);
}
}else r = 1 - r;
}
cout << (r == 0 ? s.substr(0, n) + s.substr(n, n) : s.substr(n, n) + s.substr(0, n)) << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main(){
float n, D, H; cin >> n >> D >> H;
float ans = 0;
for (int i = 0; i < n; i++){
float d, h; cin >> d >> h;
ans = max(ans, H - (D * (H - h) / (D - d)));
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pint pair<int, int>
#define pll pair<ll, ll>
const ll INF = 1LL << 60;
const int MOD = 1000000007;
// const int MOD = 998244353;
int main()
{
int n;
string s, t;
cin >> n;
cin >> s >> t;
int scnt = 0;
int tcnt = 0;
vector<ll> sindex;
vector<ll> tindex;
for (int i = 0; i < n; i++)
{
if (s[i] == '1')
{
scnt++;
sindex.push_back(i);
}
if (t[i] == '1')
{
tcnt++;
tindex.push_back(i);
}
// if (scnt > tcnt)
// {
// cout << -1 << endl;
// return 0;
// }
}
if (scnt < tcnt)
{
cout << -1 << endl;
return 0;
}
if ((scnt - tcnt) % 2 != 0)
{
cout << -1 << endl;
return 0;
}
ll cnt = 0;
int j = 0;
for (int i = 0; i < tcnt; i++)
{
vector<ll> now;
if (j >= scnt)
{
cout << -1 << endl;
return 0;
}
while (sindex[j] < tindex[i])
{
now.push_back(sindex[j]);
j++;
if (j >= scnt)
{
cout << -1 << endl;
return 0;
}
}
if (now.size() % 2 == 1)
{
now.push_back(sindex[j]);
j++;
}
if (j >= scnt)
{
cout << -1 << endl;
return 0;
}
for (int k = 0; k < now.size(); k += 2)
{
cnt += now[k + 1] - now[k];
}
cnt += sindex[j] - tindex[i];
j++;
// cout << j << " " << cnt << endl;
}
for (int i = j; i < scnt; i += 2)
{
cnt += sindex[i + 1] - sindex[i];
}
cout << cnt << endl;
}
| #include <bits/stdc++.h>
using namespace std;
int main(){
int n;
string s, t;
cin >> n >> s >> t;
long long int ans = 0;
queue<int> q;
for(int i=0; i<n; i++){
if(q.empty() == false && s[i] == '1'){
ans += i - q.front();
q.pop();
s[i] = '0';
}
if(s[i] != t[i]) q.push(i);
}
if(q.empty() == false) cout << -1 << endl;
if(q.empty() == true) cout << ans << endl;
return 0;
} |
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
ll n,q;
cin>>n>>q;
unordered_map<ll,ll> m;
vector<ll> a(n);
for(ll i=0;i<n;i++)
{
cin>>a[i];
m[a[i]]++;
}
sort(a.begin(),a.end());
while(q--)
{
ll d;
cin>>d;
ll l=0,r=(1100000000000000000);
while(r-l > 1)
{
ll m = (l+r)/2;
ll cnt = upper_bound(a.begin(),a.end(),m)-a.begin();
if(m - cnt >= d)
{
r=m;
}
else
{
l=m;
}
}
cout<<r<<"\n";
}
} | #include <bits/stdc++.h>
using namespace std;
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 pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, s, n) for(int i = (int)(s); i < (int)(n); ++i)
ll INF = 1ll << 60;
int main(){
ll n, q;
cin >> n >> q;
vl a(n);
rep(i, 0, n){
cin >> a[i];
}
vector<pll> k(q);
rep(i, 0, q){
ll t;
cin >> t;
k[i] = {t, i};
}
sort(k.begin(), k.end());
vl ans(q);
int id = 0;
rep(i, 0, q){
ll t = k[i].first;
if(id == 0){
if(a[0]-1 >= t){
ans[k[i].second] = t;
continue;
}
}
while(id < n-1 && a[id+1]-(id+2) < t){
id++;
}
ans[k[i].second] = t+id+1;
}
rep(i, 0, q){
cout << ans[i] << endl;
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define FOR(i,a,b) for (int i = a; i < b; ++i)
#define FORR(i,a,b) for (int i = b - 1; i >= a; --i)
#define REP(i,n) FOR(i,0,n)
#define REPR(i,n) FORR(i,0,n)
template <typename T> bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template <typename T> bool chmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; }
template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); }
template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); }
template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; }
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <typename T, size_t sz> ostream &operator<<(ostream &os, const array<T, sz> &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']'; return os; }
template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T, typename TH> ostream &operator<<(ostream &os, const unordered_set<T, TH> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << '(' << pa.first << ',' << pa.second << ')'; return os; }
template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
template <typename TK, typename TV, typename TH> ostream &operator<<(ostream &os, const unordered_map<TK, TV, TH> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
// #define DEBUG
#ifdef DEBUG
const string COLOR_RESET = "\033[0m", BRIGHT_GREEN = "\033[1;32m", BRIGHT_RED = "\033[1;31m", BRIGHT_CYAN = "\033[1;36m", NORMAL_CROSSED = "\033[0;9;37m", RED_BACKGROUND = "\033[1;41m", NORMAL_FAINT = "\033[0;2m";
#define dbg(x) cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << " in " << __FILE__ << ")" << COLOR_RESET << endl
#else
#define dbg(x) (x)
#endif
int main() {
int N, M, Q;
cin >> N >> M >> Q;
vector<pair<ull, ull>> bag(N); // first: value, second: size
REP(n, N) cin >> bag[n].second >> bag[n].first;
sort(bag.begin(), bag.end());
vector<ull> X(M);
REP(m, M) cin >> X[m];
REP(q, Q) {
int L, R;
cin >> L >> R;
--L;
vector<ull> box;
REP(m, L) box.push_back(X[m]);
FOR(m, R, M) box.push_back(X[m]);
sort(box.begin(), box.end());
ull ans = 0;
vector<bool> used(box.size(), false);
REPR(n, N) {
ull value = bag[n].first, size = bag[n].second;
REP(m, box.size()) {
if (used[m]) continue;
if (box[m] < size) continue;
ans += value;
used[m] = true;
break;
}
}
cout << ans << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef long double ld;
const int inf = 1e9;
const ll inf64 = 1e18;
int main() {
#ifdef DEBUG
freopen("input.txt", "r", stdin);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; i++)
cin >> a[i];
sort(a.begin(), a.end());
vector<vector<int>> b(k);
for (int i = 0; i < n; i++) {
int j = i;
while (j < n && a[i] == a[j])
j++;
j--;
for (int q = i; q <= j && q - i < k; q++)
b[q - i].push_back(a[i]);
i = j;
}
ll res = 0;
for (const auto& ar : b) {
unordered_set<int> q(ar.begin(), ar.end());
int cur = 0;
while (q.count(cur))
cur++;
res += cur;
}
cout << res << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
long long sum =0;
for(int i=0;i<N;i++){
long long a,b;
cin >> a >> b;
sum += ((a+b)*(b-a+1))/2;
}
cout << sum << endl;
}
| #pragma GCC optimize(2)
#include <bits/stdc++.h>
#define INF 1000000000
#define LINF 1000000000000000000
#define MOD 1000000007
#define mod 998244353
#define INF63 1061109567
#define INF127 9187201950435737471
#define UINF 18446744073709551615
#define F first
#define S second
#define ll long long
#define N 110
using namespace std;
ll n;
bool vis[N];
vector<ll> vt[N];
vector<pair<ll,ll> > nail;
double calc_dist(ll x1,ll y1,ll x2,ll y2)
{
return sqrt((double)((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)));
}
bool reach(ll x,ll dest)
{
if(x==dest)
{
return true;
}
vis[x]=true;
ll i;
for(i=0;i<vt[x].size();i++)
{
if(vis[vt[x][i]])
{
continue;
}
if(reach(vt[x][i],dest))
{
return true;
}
}
return false;
}
bool check(double mx_d)
{
mx_d*=2;
double dist;
ll i,j;
//n is the upper edge, n+1 is the lower edge
for(i=0;i<N;i++)
{
vt[i].clear();
}
// cout<<mx_d<<endl;
memset(vis,false,sizeof(vis));
for(i=0;i<n;i++)
{
dist=calc_dist(nail[i].F,nail[i].S,nail[i].F,100);
// cout<<dist<<" ";
if(dist<mx_d)
{
vt[i].push_back(n);
vt[n].push_back(i);
}
dist=calc_dist(nail[i].F,nail[i].S,nail[i].F,-100);
// cout<<dist<<endl;
if(dist<mx_d)
{
vt[i].push_back(n+1);
vt[n+1].push_back(i);
}
for(j=i+1;j<n;j++)
{
dist=calc_dist(nail[i].F,nail[i].S,nail[j].F,nail[j].S);
if(dist<mx_d)
{
vt[i].push_back(j);
vt[j].push_back(i);
}
// cout<<i<<","<<j<<": "<<dist<<endl;
}
}
// puts("");
if(reach(n,n+1))
{
return false;
}
return true;
}
int main(){
ll i,x,y;
double l,r,mid;
scanf("%lld",&n);
for(i=0;i<n;i++)
{
scanf("%lld%lld",&x,&y);
nail.push_back(make_pair(x,y));
}
l=0;
r=100;
while(l+0.0000000001<r)
{
mid=(l+r)/2;
if(check(mid))
{
l=mid;
}
else
{
r=mid;
}
}
printf("%.16f",l);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define flush cout.flush
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pl = pair<ll, ll>;
const ll INF = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ld eps = 1e-9;
const ld PI = acos(-1);
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
ll n;
cin >> n;
ll X;
cin >> X;
vector<ll> a(n);
for (ll &i : a) cin >> i;
ll res = INF * INF;
for (ll k = 1; k <= n; ++k) {
vector<vector<ll>> dp(k + 1, vector<ll>(k + 1, -1));
dp[0][0] = 0;
for (auto x : a) {
vector<vector<ll>> new_dp(k + 1, vector<ll>(k + 1, -1));
new_dp[0][0] = 0;
for (ll i = 1; i <= k; ++i) {
for (ll j = 0; j < k; ++j) {
ll back = ((j - x) % k + k) % k;
new_dp[i][j] = dp[i][j];
if (dp[i - 1][back] == -1)continue;
new_dp[i][j] = max(new_dp[i][j], dp[i - 1][back] + x);
}
}
dp = new_dp;
}
if (dp[k][X % k] == -1)continue;
res = min(res, (X - dp[k][X % k]) / k);
}
cout << res << "\n";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
struct math {
__int128 Tgcd(__int128 a, __int128 b) {
if (b == 0) {
return a;
} else {
return Tgcd(b, a % b);
}
}
__int128 d = 0;
pair<__int128, __int128> gcd(__int128 a, __int128 b) {
if (b == 0) {
d = a;
return {1, 0};
} else {
pair<__int128, __int128> it = gcd(b, a % b);
return {it.second, it.first - a / b * it.second};
}
}
pair<__int128, __int128> coef(__int128 a, __int128 b, __int128 c) {
auto it = gcd(a, b);
if (c % d) return {0, 0};
it.first *= (c / d);
it.second *= (c / d);
return it;
}
__int128 crt(__int128 m1, __int128 m2, __int128 r1, __int128 r2) {
pair<__int128, __int128> it = coef(m1, -m2, r2 - r1);
__int128 t = m1 * it.first + r1, mod = m1 * m2 / abs(d);
t %= mod;
if (t < 0) {
t += mod;
}
if (t % m1 != r1 || t % m2 != r2) {
return -1;
}
return t;
}
};
math user;
signed main() {
int t;
cin >> t;
while (t--) {
const __int128 inf = (__int128) 2e23;
long long x, y, p, q;
__int128 ret = inf;
cin >> x >> y >> p >> q;
for (__int128 t1 = x; t1 < x + y; t1++) {
for (__int128 t2 = p; t2 < p + q; t2++) {
__int128 val = user.crt(2 * (x + y), p + q, t1, t2);
if (val != -1) ret = min(ret, val);
}
}
if (ret == inf) {
cout << "infinity\n";
} else {
long long print = ret;
cout << print << "\n";
}
}
}
|
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
typedef long long ll;
int main() {
int h, w;
cin >> h >> w;
vector<string> a(h);
rep(i, h) {
cin >> a[i];
}
int ans = 0;
for (int i = 0; i < h - 1; ++i) {
for (int j = 0; j < w - 1; ++j) {
int d = 0;
if (a[i][j] == '#') ++d;
if (a[i][j + 1] == '#') ++d;
if (a[i + 1][j] == '#') ++d;
if (a[i + 1][j + 1] == '#') ++d;
//cout << d << endl;
if (d == 3 || d == 1) ++ans;
}
}
cout << ans << endl;
} | const bool isDebugMode = true;
int testcase = 1;
#include <bits/stdc++.h>
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
using namespace std;
typedef long long ll;
typedef pair<long long, long long> P;
struct edge{long long to,cost;};
const int inf = 1 << 27;
const long long INF = 1LL << 60;
const int COMBMAX = 1001001;
const int SHOWSIZE = 1 << 5;
const long long MOD = 1000000007; //998244353;
const long long dy[] = {0, 0, 1, 1};
const long long dx[] = {0, 1, 0, 1};
const string abc = "abcdefghijklmnopqrstuvwxyz";
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define eachdo(v, e) for (const auto &e : (v))
#define all(v) (v).begin(), (v).end()
#define lower_index(v, e) (long long)distance((v).begin(), lower_bound((v).begin(), (v).end(), e))
#define upper_index(v, e) (long long)distance((v).begin(), upper_bound((v).begin(), (v).end(), e))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
long long mpow(long long a, long long n, long long mod = MOD){long long res = 1; while(n > 0){if(n & 1)res = res * a % mod; a = a * a % mod; n >>= 1;} return res;}
long long power(long long a, long long n){long long res = 1; while(n > 0){if(n & 1)res = res * a; a = a * a; n >>= 1;} return res;}
void pt(){cout << endl; return;}
template<class Head> void pt(Head&& head){cout << head; pt(); return;}
template<class Head, class... Tail> void pt(Head&& head, Tail&&... tail){cout << head << " "; pt(forward<Tail>(tail)...);}
void dpt(){if(!isDebugMode) return; cout << endl; return;}
template<class Head> void dpt(Head&& head){if(!isDebugMode) return; cout << head; pt(); return;}
template<class Head, class... Tail> void dpt(Head&& head, Tail&&... tail){if(!isDebugMode) return; cout << head << " "; pt(forward<Tail>(tail)...);}
template<class T> void enu(T v){rep(i, v.size()) cout << v[i] << " " ; cout << endl;}
template<class T> void enu2(T v){rep(i, v.size()){rep(j, v[i].size()) cout << v[i][j] << " " ; cout << endl;}}
template<class T> void debug(T v){if(!isDebugMode) return; rep(i, min(v.size(), SHOWSIZE)) cout << v[i] << " " ; cout << endl;}
template<class T> void debug2(T v){if(!isDebugMode) return; rep(i, min(v.size(), SHOWSIZE)){rep(j, min(v[i].size(), SHOWSIZE)) cout << v[i][j] << " " ; cout << endl;}}
template<class T1, class T2> inline bool chmin(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}
template<class T1, class T2> inline bool chmax(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}
template<class T1, class T2> long long recgcd(T1 a, T2 b){return a % b ? recgcd(b, a % b) : b;}
template<typename T> vector<T> make_v(size_t a){return vector<T>(a);}
template<typename T,typename... Ts> auto make_v(size_t a,Ts... ts){return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));}
template <typename T, typename U, typename... V> typename enable_if<is_same<T, U>::value != 0>::type fill_v(U &u, const V... v) { u = U(v...); }
template <typename T, typename U, typename... V> typename enable_if<is_same<T, U>::value == 0>::type fill_v(U &u, const V... v){for (auto &e : u) fill_v<T>(e, v...);}
bool valid(long long H, long long W, long long h, long long w) { return 0 <= h && h < H && 0 <= w && w < W; }
void solve();
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
// cin >> testcase;
while(testcase--) solve();
return 0;
}
void solve(){
ll H, W; cin >> H >> W;
vector<string> s(H);
rep(i, H) cin >> s[i];
ll ans = 0;
rep(i, H - 1)rep(j, W - 1){
ll cnt = 0;
rep(k, 4){
if(valid(H, W, i + dx[k], j + dy[k]) && s[i + dx[k]][j + dy[k]] == '.') cnt++;
}
if(cnt % 2) ans++;
}
pt(ans);
} |
#include <bits/stdc++.h>
using namespace std;
long long n,k;
int main()
{
cin >> n >> k;
while(k--)
{
if(n % 200) n = n * 1000 + 200;
else n /= 200;
}
cout << n;
return 0;
} | #define _GLIBCXX_DEBUG//ใใใๆๅพใฎๆๆฎตใ !
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<numeric>
#include<climits>
#include<limits>//
#include <stdio.h>//
#include<cmath>
#include<iomanip>
#include <cmath>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
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); }
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
/*template<class T> T ipow(T a, int b){
}*/
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int a,b,c;
cin >> a >> b;
c = a+b;
if (c >= 15 && b >= 8){
cout << 1 << '\n';
}else if (c >= 10 && b >= 3){
cout << 2 << '\n';
}else if (c >= 3){
cout << 3 << '\n';
}else{
cout << 4 << '\n';
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int mod = 998244353;
LL pw(LL n, int x) {
LL ret = 1;
LL t = n;
while (x) {
if (x & 1) ret = ret * t % mod;
t = t * t % mod;
x >>= 1;
}
return ret;
}
int main() {
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
if (n == 1)
printf("%lld", pw(k, m));
else if (m == 1)
printf("%lld", pw(k, n));
else {
LL ret = 0;
for (int i = 1; i <= k; ++i)
ret = ((pw(i, n) - pw(i - 1, n) + mod) % mod * pw(k + 1 - i, m) % mod + ret) % mod;
printf("%lld", ret);
}
} | #include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n); i++)
#define length size()
#define int long long
#define ll long long
#include <cstdint>
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
if(n==0) return 0;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
const int MOD = 1000000007;
const int mod = 998244353;
const int MAX = 510000;
const int inf = 400000000000000;
template<typename T> string join(vector<T> &vec ,const string &sp){
int si = vec.length;
if(si==0){
return "";
}else{
stringstream ss;
rep(i,si-1){
ss << vec[i] << sp;
}
ss << vec[si - 1];
return ss.str();
}
}
vector<vector<int>> ruiseki2d(vector<vector<int>> vec){
rep(i,vec.size()-1){
rep(j,vec[0].size()-1){
vec[i+1][j+1] = vec[i][j+1]+vec[i+1][j]-vec[i][j]+vec[i+1][j+1];
}
}
return vec;
}
class CompareDist
{
public:
bool operator()(pair<int,int> p,pair<int,int>q){
return p.second > q.second;
}
};
vector<int> dijkstra(vector<vector<pair<int,int>>> vec,int s){
priority_queue<pair<int,int>,vector<pair<int,int>>,CompareDist> que;
pair<int,int> f = make_pair(s,0);//point,cost;
que.push(f);
bool y = false;
vector<int> cost(vec.size(),inf);
while(!que.empty()){
pair<int,int> tmp = que.top();
que.pop();
int p = tmp.first;
int c = tmp.second;
if(cost[p]>c){
cost[p] = c;
rep(i,vec[p].size()){
if(cost[vec[p][i].first]<=inf){
tmp = make_pair(vec[p][i].first,vec[p][i].second+c);
que.push(tmp);
}
}
}
if(!y) cost[p] = inf;
y = true;
}
return cost;
}
signed main(void){
int n,m,k;
cin >> n >> m >> k;
int ans = 0;
if(n==1||m==1) ans = modpow(k,max(n,m),mod);
else{
for(int i=1;i<=k;i++){
ans += (modpow(i,n,mod)*modpow(k-i+1,m,mod)%mod);
ans %= mod;
ans -= (modpow(i,n,mod)*modpow(k-i,m,mod)%mod);
ans = (ans%mod+mod)%mod;
//cout << modpow(i,n,mod) << " " << modpow(k-i+1,m,mod) << endl;
}
}
cout << ans%mod << endl;
}
|
// Author: Pasindu Wijesena
#include <bits/stdc++.h>
using namespace std;
#define deb(x) cout<<#x<<": "<<x<<endl
#define rep(a,b) for(int a=0;a<b;a++)
#define rep2(a,b,c) for(int a=b;a<c;a++)
#define per2(a,b,c) for(int a=b;a>=c;a--)
#define gcd(a,b) __gcd(a,b) // gcd of two numbers
#define bitCount(a) __builtin_popcount(a) // number of ones in binary form
#define zeroCount(a) __builtin_clz(a) // number of zeros at the end in binary form
typedef long long ll;
template<typename... T>
void rd(T&... args){ ((cin>>args), ...); }
template<typename... T>
void wt(T&&... args) { ((cout<<args<<" "), ...); }
ll mod(ll V, ll N){
return ((V % N) + N) % N;
}
const ll MOD = 1e9+7;
bool solve(){
int N;
rd(N);
vector<int>A(N,0);
for(auto &a:A) rd(a);
sort(A.begin(), A.end());
ll answer = A[0]+1;
ll diff = 0;
rep2(i,1,N){
diff = A[i] - A[i-1];
answer = mod(answer * (diff+1), MOD);
}
cout<<answer<<endl;
return true;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int T=1;
//~ rd(T);
rep(i,T){
//~ cout<<"Case #"<<i+1<<": ";
solve();
//~ bool result = solve();
//~ if(result)cout<<"YES"<<endl;
//~ else cout<<"NO"<<endl;
}
return 0;
}
| /*
written by Pankaj Kumar.
country:-INDIA
Institute: National Institute of Technology, Uttarakhand
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll ;
typedef unsigned long long ull;
typedef vector<ll> vl;
#define speed cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);
/* Abbrevations */
#define ff first
#define ss second
#define mp make_pair
#define line cout<<endl;
#define pb push_back
#define Endl "\n"
// loops
#define forin(arr,n) for(ll i=0;i<n;i++) cin>>arr[i];
// Some print
#define no cout<<"No"<<endl;
#define yes cout<<"Yes"<<endl;
// sort
#define all(V) (V).begin(),(V).end()
#define srt(V) sort(all(V))
#define srtGreat(V) sort(all(V),greater<ll>())
// some extra
#define printv(v) for(ll i=0;i<ll(v.size());i++){cout<<v[i]<<" ";} line;
#define precision(x) cout<<fixed<<setprecision(x);
#define sz(V) ll(V.size())
// template
template <typename T>
T mymax(T x,T y)
{
return (x>y)?x:y;
}
// function
ll power(ll x,ll y,ll mod)
{
ll res=1;
// x=x%mod;
while(y>0)
{
if(y%2==1)
{
res*=x;
// res=res%mod;
}
y/=2; x*=x; // x=x%mod;
}
return res;
}
ll str_to_num(string s)
{
return stoi(s);
}
string num_to_str(ll num)
{
return to_string(num);
}
// datatype definination
#define ordered_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
class Point
{
public:
ll x;
ll y;
ll z;
ll getsum()
{
return x+y+z;
}
};
/* ascii value
A=65,Z=90,a=97,z=122
*/
/* --------------------MAIN PROGRAM----------------------------*/
// to run ctrl+b
const ll INF=LONG_MAX;
const ll mod1=1e9+7;
const ll mod2=998244353;
// Techniques
// divide into cases, brute force, pattern finding
// sort, greedy, binary search, two pointer
// transform into graph
ll solve()
{
ll n;
cin>>n;
vl v(n);
forin(v,n);
srt(v);
ll ans=1;
ll pre=0;
for(ll i=0;i<n;i++){
ans*=(v[i]+1-pre);
ans%=mod1;
pre=v[i];
}
cout<<ans<<endl;
return 0;
}
int main()
{
speed;
/* #ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif */
ll TestCase=1;
// cin>>TestCase;
while(TestCase--)
{
solve();
}
}
/* -----------------END OF PROGRAM --------------------*/
/*
* stuff you should look before submission
* constraint and time limit
* int overflow
* special test case (n=0||n=1||n=2)
* don't get stuck on one approach if you get wrong answer
*/ |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
template <typename T>
inline void read(T &x)
{
x = 0;bool b = 0;char c;
for (c = getchar();!isdigit(c);b |= (c == '-'), c = getchar());
for (;isdigit(c);x = (x << 1) + (x << 3) + (c ^ 48), c = getchar());
if (b) x = -x;
}
template <typename T>
inline void write(T x, int base = 10)
{
if (x == 0)
{
putchar('0');
return ;
}
if (x < 0) putchar('-'), x = -x;
static int stk[70];
int top = 0;
while (x)
{
stk[top++] = x % base;
x /= base;
}
while (top > 0)
{
putchar(stk[--top] ^ 48);
}
}
const int M = 1000005;
int num, mu[M + 5], ol[M + 5], prime[M + 3];
int l, r;
void get()
{
mu[1] = 1;
for (int i = 2;i <= M;++i)
{
if (!ol[i])
prime[++num] = i, mu[i] = -1;
for (int j = 1;j <= num && i * prime[j] <= M;++j)
{
ol[i*prime[j]] = 1;
if (i % prime[j] == 0)
{
mu[i * prime[j]] = 0;
break;
}
mu[i * prime[j]] = -mu[i];
}
}
}
ll tmp(int t)
{
return (ll)t * (t-1);
}
ll ans;
int main()
{
get();
read(l);read(r);
for (int i = 2;i <= r;++i)
ans -= tmp(r / i - (l - 1) / i) * mu[i];
for (int i = l;i <= r;++i)
ans -= ((r / i - 1) << 1);
if (l == 1)
ans += (r - 1) * 2;
write(ans);
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;
}
char s[maxn];
int n,cnt[26];
ll ans;
int main(){
scanf("%s",s+1);
n=strlen(s+1);
cnt[s[n]-'a']++;
ROF(i,n-1,1){
cnt[s[i]-'a']++;
if(s[i]==s[i+1]){
// printf("at %d\n",i);
FOR(j,0,25) if(j!=s[i]-'a') ans+=cnt[j],cnt[s[i]-'a']+=cnt[j],cnt[j]=0;
// printf("ans=%d\n",ans);
}
}
printf("%lld\n",ans);
}
/*
azaazbbzzaaza
*/ |
#include <bits/stdc++.h>
using namespace std;
#define int long long
void read (int &x) {
char ch = getchar(); x = 0; int f = 0;
while (!isdigit(ch)) { if (ch == '-') f = 1; ch = getchar(); }
while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar();
if (f) x = -x;
} const int N = 2e5 + 5, mod = 998244353;
int qpow (int x, int y) {
int t = 1;
while (y) {
if (y & 1) t = t * x % mod;
x = x * x % mod, y >>= 1;
}
return t;
}
int n, m, res, a[N], t, p[N * 2], in[N * 2], b[N], tot, k[N];
int C (int x, int y) {
return p[x] * in[y] % mod * in[x - y] % mod;
}
void prework () {
p[0] = 1;
for (int i = 1; i <= n + m; ++i) p[i] = p[i - 1] * i % mod;
in[n + m] = qpow (p[n + m], mod - 2);
for (int i = n + m; i >= 1; --i) in[i - 1] = in[i] * i % mod;
for (int i = 2; i * i <= m; ++i)
if (!k[i]) {
b[++tot] = i;
for (int j = i; j * j <= m; j += i) k[j] = 1;
}
}
void work (int x) {
t = 0;
for (int i = 1; i <= tot && b[i] <= x; ++i) {
int cnt = 0;
while (x % b[i] == 0) ++cnt, x /= b[i];
if (cnt) a[++t] = cnt;
}
if (x > 1) a[++t] = 1; int tmp = 1;
for (int i = 1; i <= t; ++i) {
tmp = tmp * C (a[i] + n - 1, a[i]) % mod;
}
// printf ("%lld %lld %lld\n", x, tmp, b[1]);
(res += tmp) %= mod;
}
signed main() {
read (n), read (m); prework ();
// printf ("%d\n", tot);
for (int i = 1; i <= m; ++i) work (i);
printf ("%lld\n", res);
}
| #include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10, mod = 998244353;
int n, fa[N], ans = 1;
int find_(int x) {
return x == fa[x] ? x : fa[x] = find_(fa[x]);
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++)
fa[i] = i;
for (int i = 1, x; i <= n; i++) {
scanf("%d", &x);
if (find_(x) == find_(i))
ans = ans*2%mod;
else
fa[find_(i)] = find_(x);
}
printf("%d\n", (ans + mod - 1)%mod);
} |
#include <iostream>
#include <vector>
using namespace std;
long mod = 1000000007;
long sqr(long a) {
return a * a % mod;
}
int main() {
int t;
cin >> t;
for (int i = 0; i < t; i++) {
long n, a, b;
cin >> n >> a >> b;
if (n < a + b) {
cout << 0 << endl;
continue;
}
if (a > b) {
swap(a, b);
}
long s;
long x = n-a+1;
long y = n-b+1;
s = sqr(x)*sqr(y)%mod;
s += mod - sqr(b-a+1)*sqr(y)%mod;
s%=mod;
long z = a - 1;
long s1 = ((b-a+1)*y%mod)*(y - 1)%mod;
long sz = ((b-a+1)*y%mod)*(y - z)%mod;
s += mod - (s1+sz)*z%mod*2%mod;
s%=mod;
long s11 = (y-1)*(y-1)%mod;
long s1z = (y-1)*(y-z)%mod;
long szz = (y-z)*(y-z)%mod;
s1 = (s11 + s1z) * z%mod;
sz = (s1z + szz) * z%mod;
s += mod - (s1+sz)*z%mod;
s %= mod;
cout << s << endl;
}
}
| #pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
using namespace std;
#define int long long
#define pb push_back
#define mp make_pair
#define st(arr,a) memset(arr,a,sizeof arr)
#define nl cout<<'\n'
#define bitcount(x) __builtin_popcountll(x)
const int MOD = 1000000007ll;
#define forn(i,a,b) for(int i=a;i<=b;i++)
#define rfor(i,a,b) for(int i=a;i>=b;i--)
#define all(x) x.begin() , x.end()
#define pi pair<int,int>
#define X first
#define Y second
#define sz(A) (int)A.size()
#define N 1014159
#define vi vector<int>
#define v vector
#define die exit(0)
#define alln(arr) arr+1 , arr+1+n
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update> oset;
// order_of_key (k) : Number of items strictly smaller than k .
// find_by_order(k) : K-th element in a set (counting from zero).
#define in input_int()
#define ins input_string()
bool sb(int a,int i){return (a&(1LL<<i))>0;}
int p2(int a){return (1LL<<a);}
int input_int(){int a;cin>>a;return a;}
string input_string(){string s;cin>>s;return s;}
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int ceil(int a,int b){return (a+b-1)/b;}
int ct,visited[N],ans;
vi adj[N];
int n,m,k,a,b,c,d,x,y,arr[N];
void clr()
{
forn(i,0,n+10)adj[i].clear(),visited[i]=0,arr[i]=0;
}
inline int mul(int a,int b){return (a*1ll*b)%MOD;}
inline int add(int a,int b){a+=b;if(a>=MOD)a-=MOD;return a;}
inline int power(int a,int b){int rt=1;while(b>0){if(b&1)rt=mul(rt,a);a=mul(a,a);b>>=1;}return rt;}
inline int inv(int a){return power(a,MOD-2);}
int get_sum(int a)
{
if(a<=0)return 0;
return mul(add(mul(a,a),a),inv(2));
}
string s;
void solve()
{
cin>>n>>a>>b;
if(a>b)swap(a,b);
int ans1=get_sum(n-b-a+1);
ans1%=MOD;
int ans2=(n-b+1)*(n-a+1);
ans2%=MOD;
//cout<<ans1<<" "<<ans2<<"\n";
int ans3=get_sum(n-b-a+1);
ans3=ans3*ans3;
ans3%=MOD;
ans3*=2;
ans3%=MOD;
int ans=ans1*ans2;
ans%=MOD;
ans*=2;
ans%=MOD;
ans=ans-ans3+MOD;
ans%=MOD;
ans=ans+ans;
ans%=MOD;
cout<<ans;
}
signed main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int t = 1;
cin>>t;
forn(i,1,t)
{
//cout<<":Case #"<<i<<": ";
solve();
nl;
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
# define line cout<<"\n";
# define fast ios_base::sync_with_stdio(false) ; cin.tie(0) ;
# define ll long long
# define C continue;
# define pb push_back
# define vi vector < int >
# define vll vector < ll >
# define I insert
# define pii pair < int , int >
# define pll pair < ll , ll >
# define F first
# define S second
# define vpii vector < pair < int , int > >
# define vpll vector < pll >
# define all(v) (v).begin() , (v).end()
# define allrev(v) (v).rbegin() , (v).rend()
# define ld long double
# define PQ priority_queue
# define vc vector < char >
# define vb vector < bool >
# define vs vector < string >
# define vvi vector < vector < int > >
# define vvll vector < vector < ll > >
# define vvc vector < vector < char > >
# define vvb vector < vector < bool > >
# define allcomp(v) v.begin() , v.end() , comp
# define pp(n) printf("%.10Lf",n);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
string vow = "aeiou";
int month[] = {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int dxhorse[] = {-2, -2, -1, -1, 1, 1, 2, 2};
const int dyhorse[] = {1, -1, 2, -2, 2, -2, 1, -1};
const int dx[] = { -1 , 0 , 1 , 0 , -1 , -1 , 1 , 1 } ;
const int dy[] = { 0 , 1 , 0 , -1 , -1 , 1 , -1 , 1 } ;
const ld pie = 3.1415926535897932384626 ;
const ll mod = 1e9 + 7 ;
/// Tip : If a and b are positive integers ; we may say - ceil (a/b) = 1 + floor ( (a-1)/b ) .
/// Start using lower bound !!
void solve ( int test_case )
{
int n ;
cin >> n ;
ll s = 0 ;
ll &sum = s ;
vi v ( n ) ;
for ( int i=0 ; i < n ; i ++ )
{
int &val = v[i] ;
cin >> val ;
s += val ;
}
vvi temp ( 2 ) ;
for ( int i=0 ; i < n ; i ++ )
{
int val ;
cin >> val ;
val = val - v[i] ;
temp[i&1].pb ( val ) ;
}
sort ( allrev ( temp[0] ) ) ;
sort ( allrev ( temp[1] ) ) ;
int z = min ( temp[0].size() , temp[1].size() ) ;
for ( int i=0 ; i < z ; i ++ )
{
int add = temp[0][i] + temp[1][i] ;
if ( add <= 0 )
break ;
sum += add ;
}
cout << sum ; line ;
}
int main()
{
int t = 1;
// cin >> t;
for ( int i = 1 ; i <= t ; i ++ )
{
solve ( i ) ;
}
return 0;
}
| #include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>
static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;
template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;
template <class T, class U>
vector<T> make_v(U size, const T& init){ return vector<T>(static_cast<size_t>(size), init); }
template<class... Ts, class U>
auto make_v(U size, Ts... rest) { return vector<decltype(make_v(rest...))>(static_cast<size_t>(size), make_v(rest...)); }
template<class T> void chmin(T &a, const T &b){ a = (a < b ? a : b); }
template<class T> void chmax(T &a, const T &b){ a = (a > b ? a : b); }
int main() {
int n;
cin >> n;
vector<int> A(n), B(n);
for (auto &&i : A) scanf("%d", &i);
for (auto &&i : B) scanf("%d", &i);
ll ans = 0;
for (int i = 1; i < n; i += 2) {
swap(A[i], B[i]);
}
priority_queue<int> Q;
for (int i = 0; i < n; ++i) {
ans += A[i];
Q.emplace(B[i]-A[i]);
}
for (int i = 0; i < n/2; ++i) {
ans += Q.top(); Q.pop();
}
cout << ans << "\n";
return 0;
} |
#include <bits/stdc++.h>
//#include <bits/extc++.h>
//#include <immintrin.h>
using namespace std;
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,tune=native")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
template<typename T>
istream &operator>>(istream &in, vector<T> &a) {
for (auto &i : a)
in >> i;
return in;
}
template<typename T>
ostream &operator<<(ostream &out, const vector<T> &a) {
for (auto &i : a) {
out << i << " ";
}
return out;
}
template<typename T, typename D>
istream &operator>>(istream &in, pair<T, D> &a) {
in >> a.first >> a.second;
return in;
}
template<typename T, typename D>
ostream &operator<<(ostream &out, const pair<T, D> &a) {
out << a.first << " " << a.second;
return out;
}
struct LogOutput {
template<typename T>
LogOutput &operator<<(const T &x) {
#ifdef DIVAN
cout << x;
#endif
return *this;
}
} dout, fout;
typedef long long ll;
typedef unsigned long long ull;
typedef double dl;
typedef complex<double> cd;
#define nl '\n'
#define elif else if
#define all(_v) _v.begin(), _v.end()
#define rall(v) v.rbegin(), v.rend()
#define sz(v) (int)(v.size())
#define sqr(_v) ((_v) * (_v))
#define eb emplace_back
#define pb push_back
#define mod(x, m) ((x) >= 0 ? ((x) % m) : ((((x) % m) + m) % m))
#define minq(x, y) x = min((x), (y))
#define maxq(x, y) x = max(x, (y))
#define forn(i, n) for (int i = 0; i < (n); ++i)
typedef vector<pair<int,int>> vpi;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef tuple<int, int, int> ti;
const ll INFL = 9187201950435737471;
const ll nINFL = -9187201950435737472;
const int INF = 2139062143;
const int nINF = -2139062144;
const ull ULINF = numeric_limits<ull>::max();
const long double PI = acos(-1);
auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
mt19937 rnd(seed);
inline void IO() {
#ifdef DIVAN
freopen("../input.txt", "r", stdin);
freopen("../output.txt", "w", stdout);
#else
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
#endif
}
struct Point{
int coord;
bool include;
bool open;
int num;
Point(int a = 0, bool b = 0, bool c = 0, int x = 0) {
coord = a;
include = b;
open = c;
num = x;
}
bool operator<(const Point& x) const {
if (coord != x.coord) {
return coord < x.coord;
}
if (include ^ x.include) {
return include;
}
if (x.open == open) {
return 0;
}
return open;
}
};
void Solve() {
int n;
cin >> n;
vpi a(n);
forn(i, n) {
int t, l, r;
cin >> t >> l >> r;
l *= 2;
r *= 2;
if (t > 2) {
++l;
}
if (t == 2 || t == 4) {
--r;
}
a[i] = {l, r};
}
int ans = 0;
forn(i, n) {
for (int j = i + 1; j < n; ++j) {
if (a[i].first > a[j].second || a[j].first > a[i].second) {
continue;
}
++ans;
}
}
cout << ans;
}
signed main() {
auto startTime = chrono::high_resolution_clock::now();
IO();
int t = 1;
// cin >> t;
for (int i = 1; i <= t; ++i) {
// cout << "Case #" << i << ": ";
Solve();
}
auto endTime = chrono::high_resolution_clock::now();
chrono::duration<long double> duration = endTime - startTime;
cout << setprecision(6) << fixed;
fout << '\n' << "Time: " << duration.count();
return 0;
}
| using namespace std;
#include<bits/stdc++.h>
#define int long long
#define all(x) x.begin(), x.end()
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define double long double
const int N = 2e3+3;
pair<int,int>a[N];
bool intersect(int i, int j)
{
vector< pair<int,int> >v;
v.push_back( a[i] );
v.push_back( a[j] );
sort( all(v) );
return ( v[0].second >= v[1].first );
}
signed main()
{
IOS;
int n; cin >> n;
for ( int i = 1; i <= n; ++i )
{
int t; cin >> t;
cin >> a[i].first >> a[i].second;
a[i].first *= 1e9;
a[i].second *= 1e9;
if ( t == 2 )
a[i].second -= 1;
if ( t == 3 )
a[i].first += 1;
if ( t == 4 )
{
a[i].first += 1;
a[i].second -= 1;
}
}
int ans = 0;
for ( int i = 1; i <= n; ++i )
{
for ( int j = i + 1; j <= n; ++j )
ans += intersect(i, j);
}
cout << ans << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string(string(s)); }
string to_string(bool b) { return to_string(int(b)); }
string to_string(vector<bool>::reference b) { return to_string(int(b)); }
string to_string(char b) { return "'" + string(1, b) + "'"; }
template <typename A, typename B>
string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template <typename A>
string to_string(A v) {
string res = "{";
for (const auto& x : v) res += (res == "{" ? "" : ", ") + to_string(x);
return res + "}";
}
void debug() { cerr << endl; }
template <typename Head, typename... Tail>
void debug(Head H, Tail... T) {
cerr << " " << to_string(H);
debug(T...);
}
#define db(...) cerr << "[" << #__VA_ARGS__ << "]:", debug(__VA_ARGS__)
#else
#define db(...) 42
#endif
typedef long long ll;
typedef long double ld;
const int MOD = 998244353;
struct Mint {
int val;
Mint() { val = 0; }
Mint(const ll& x) {
val = (-MOD <= x && x < MOD) ? x : x % MOD;
if (val < 0) val += MOD;
}
template <typename U>
explicit operator U() const { return (U)val; }
friend bool operator==(const Mint& a, const Mint& b) { return a.val == b.val; }
friend bool operator!=(const Mint& a, const Mint& b) { return !(a == b); }
friend bool operator<(const Mint& a, const Mint& b) { return a.val < b.val; }
Mint& operator+=(const Mint& m) { if ((val += m.val) >= MOD) val -= MOD; return *this; }
Mint& operator-=(const Mint& m) { if ((val -= m.val) < 0) val += MOD; return *this; }
Mint& operator*=(const Mint& m) { val = (ll)val * m.val % MOD; return *this; }
friend Mint modex(Mint a, ll p) {
assert(p >= 0);
Mint ans = 1;
for (; p; p >>= 1, a *= a) if (p & 1) ans *= a;
return ans;
}
Mint& operator/=(const Mint& m) { return *this *= modex(m, MOD - 2); }
Mint& operator++() { return *this += 1; }
Mint& operator--() { return *this -= 1; }
Mint operator++(int) { Mint result(*this); *this += 1; return result; }
Mint operator--(int) { Mint result(*this); *this -= 1; return result; }
Mint operator-() const { return Mint(-val); }
friend Mint operator+(Mint a, const Mint& b) { return a += b; }
friend Mint operator-(Mint a, const Mint& b) { return a -= b; }
friend Mint operator*(Mint a, const Mint& b) { return a *= b; }
friend Mint operator/(Mint a, const Mint& b) { return a /= b; }
friend ostream& operator<<(ostream& os, const Mint& x) { return os << x.val; }
friend string to_string(const Mint& b) { return to_string(b.val); }
};
int main() {
ll a, b, c;
scanf("%lld%lld%lld", &a, &b, &c);
a = a * (a + 1) / 2;
b = b * (b + 1) / 2;
c = c * (c + 1) / 2;
printf("%d\n", (Mint(a) * Mint(b) * Mint(c)).val);
}
| #include <bits/stdc++.h>
using namespace std;
int main(){
int n, a , b;
cin >> n >> a >> b;
cout << (n - a) + b;
} |
#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define all(v) v.begin(),v.end()
#pragma gcc optimize("O3")
#pragma gcc optimize("Ofast")
#pragma gcc optimize("unroll-loops")
using namespace std;
const int INF = 1e9;
const int TMX = 1 << 18;
const long long llINF = 5e18;
const long long mod = 1e9+7;
const long long hmod1 = 1e9+409;
const long long hmod2 = 1e9+433;
const long long base = 257;
typedef long long ll;
typedef long double ld;
typedef pair <int,int> pi;
typedef pair <ll,ll> pl;
typedef vector <int> vec;
typedef vector <pi> vecpi;
typedef long long ll;
int a[205];
int n,k;
int f(int x,int y) {
if(x == y) return x;
if(x > y) swap(x,y);
if(x == 0&&y == 2) return 2;
return x;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
cin >> n >> k;
for(int i = 1;i <= n;i++) {
char x; cin >> x;
if(x == 'R') a[i] = 0;
if(x == 'S') a[i] = 1;
if(x == 'P') a[i] = 2;
a[n+i] = a[i];
}
for(int i = 1;i <= k;i++) {
for(int j = 1;j <= n;j++) a[j] = f(a[j*2-1],a[j*2]);
for(int j = 1;j <= n;j++) a[n+j] = a[j];
}
cout << (!a[1] ? 'R' : (a[1] & 1 ? 'S' : 'P'));
}
| #include <bits/stdc++.h>
#define _overload3(_1,_2,_3,name,...)name
#define _rep(i,n)repi(i,0,n)
#define repi(i,a,b)for(int i=int(a),i##_len=(b);i<i##_len;++i)
#define MSVC_UNKO(x)x
#define rep(...)MSVC_UNKO(_overload3(__VA_ARGS__,repi,_rep,_rep)(__VA_ARGS__))
#define all(c)c.begin(),c.end()
#define write(x)cout<<(x)<<'\n'
using namespace std; using ll = long long; template<class T>using vv = vector<vector<T>>;
template<class T>auto vvec(int n, int m, T v) { return vv<T>(n, vector<T>(m, v)); }
constexpr int INF = 1 << 29, MOD = int(1e9) + 7; constexpr ll LINF = 1LL << 60;
struct aaa { aaa() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(10); }; }aaaa;
int main() {
int n, k;
string s;
cin >> n >> k >> s;
auto convert = [](char a) { return a == 'R' ? 0 : a == 'P' ? 1 : 2; };
auto match = [&](char a, char b) {
int x = convert(a), y = convert(b);
int z = (y - x + 3) % 3;
return z == 1 ? b : a;
};
rep(i, k) {
string t(s.size(), '_');
rep(j, n) {
t[j] = match(s[(2 * j) % n], s[(2 * j + 1) % n]);
}
s = t;
}
write(s[0]);
}
|
// C++(GCC 9.2.1)
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define repex(i, a, b, c) for(int i = a; i < b; i += c)
#define repx(i, a, b) repex(i, a, b, 1)
#define rep(i, n) repx(i, 0, n)
#define repr(i, a, b) for(int i = a; i >= b; i--)
const LL MOD = 1e9 + 7;
LL a[101010], f[101010], dp[101010][2];
int main(){
// 1. ๅ
ฅๅๆ
ๅ ฑ.
int N;
scanf("%d", &N);
rep(i, N) scanf("%lld", &a[i]);
// 2. ใใฃใใใใๆฐๅ.
f[0] = 1;
f[1] = 1;
rep(i, N) f[i + 2] = f[i + 1] + f[i], f[i + 2] %= MOD;
// 3. dpๆดๆฐ.
dp[0][0] = a[0];
repx(i, 1, N){
dp[i][0] = (dp[i - 1][0] + dp[i - 1][1]) + a[i] * f[i];
dp[i][1] = dp[i - 1][0] + MOD * f[i - 1] - a[i] * f[i - 1];
dp[i][0] %= MOD;
dp[i][1] %= MOD;
}
// 4. ๅบๅ.
printf("%lld\n", (dp[N - 1][0] + dp[N - 1][1]) % MOD);
return 0;
} | #include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <map>
#include <algorithm>
#include <string>
#include <utility>
#include <set>
#include <stack>
#include <deque>
#include <ctime>
#include <random>
#include <cassert>
#include <cmath>
#include <climits>
#include <queue>
#include <cstring>
#include <bitset>
#include <iomanip>
#include <chrono>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("popcnt")
#pragma GCC target("avx2")
#ifdef LOCAL
#define dbg(x) cout << #x << " : " << x << endl;
#else
#define dbg(x)
#endif
#define pb push_back
#define ppb pop_back()
#define mp make_pair
#define fi(a, b) for (int i = a; i < b; i++)
#define fj(a, b) for (int j = a; j < b; j++)
#define fk(a, b) for (int k = a; k < b; k++)
#define fi1(a, b) for (int i = a - 1; i >= b; i--)
#define fj1(a, b) for (int j = a - 1; j >= b; j--)
#define fk1(a, b) for (int k = a - 1; k >= b; k--)
#define fx(x, a) for (auto& x : a)
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define rep1(i, a, b) for (int i = a - 1; i >= b; --i)
#define siz(x) (int)x.size()
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;
template<typename T1, typename T2>inline void mine(T1 &x, T2 y) { if (x > y) x = y; }
template<typename T1, typename T2>inline void maxe(T1 &x, T2 y) { if (x < y) x = y; }
ostream& operator << (ostream &a, const vector<int> &b) {
for (auto k : b) cout << k << ' ';
return a;
}
typedef long long ll;
typedef unsigned long long ull;
typedef char ch;
typedef string str;
typedef pair<int, int> pii;
typedef map<int, int> mii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
typedef vector<ch> vch;
typedef vector<vch> vvch;
typedef vector<str> vs;
int MOD;
const int INF = 1000000050;
const long long BIG = (long long)2e18 + 50;
const int MX = 101;
const double EPS = 1e-9;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll add(ll a, ll b) {
a += b;
if (a >= MOD) return a - MOD;
return a;
}
ll sub(ll a, ll b) {
a -= b;
if (a < 0) return a + MOD;
return a;
}
ll mult(ll a, ll b) {
return a * b % MOD;
}
ll powmod(ll a, ll p) {
if (p == 0) return 1;
if (p & 1) return mult(a, powmod(a, p - 1));
return powmod(mult(a, a), p / 2);
}
ll rev(ll a) {
return powmod(a, MOD - 2);
}
int dp[MX][MX * MX * MX];
int mxs[MX];
int n, k;
void init() {
mxs[0] = 0;
dp[0][0] = 1;
int kek = 0;
for (int i = 1; i < n; ++i) {
if (2 * i <= n - 1) {
mxs[i] = mxs[i - 1] + i * k;
} else {
mxs[i] = mxs[n - 1 - i];
}
for (int s = 0; s <= mxs[i]; ++s) {
for (int d = 0; d * i <= s && d <= k; ++d) {
++kek;
dp[i][s] = add(dp[i][s], dp[i - 1][s - d * i]);
}
}
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> k >> MOD;
init();
for (int x = 1; x <= n; ++x) {
int ans = 0;
for (int s = 0; s <= mxs[x - 1]; ++s) {
ans = add(ans, mult(dp[x - 1][s], dp[n - x][s]));
}
cout << sub(mult(ans, k + 1), 1) << '\n';
}
}
|
/*
|| Shree Ganeshay Namah ||
|| Jai Shree Ram ||
|| Nabh Sparsham Diptam ||
*/
#include<bits/stdc++.h>
using namespace std;
//typedef long long int ll;
#define mod 1000000007
#define block 180
#define ff first.first
#define fs first.second
#define sf second.first
#define ss second.second
#define f1 first
#define s1 second
#define pb push_back
// to floor a/b you can do a/b and to ceiling a/b you can do (a+b-1)/b
int jai_shree_ram()
{
return 1;
}
int shree_ganeshay_namah()
{
return 1;
}
//End of Matrix Exponentiation
/*
Segment Tree
ll ar[200005];
ll st[800005];
void build(ll si,ll ss,ll se)
{
if(ss==se)
{
st[si]=ar[ss];
return ;
}
ll mid=(ss+se)/2;
build(2*si,ss,mid);
build(2*si+1,mid+1,se);
st[si]=(st[2*si]+st[2*si+1]);
}
ll query(ll si,ll ss,ll se,ll l,ll r)
{
if(l<=ss && se<=r)
return st[si];
if(l>se || r<ss)
return 0;
ll mid=(ss+se)/2;
ll x=query(2*si,ss,mid,l,r);
ll y=query(2*si+1,mid+1,se,l,r);
return x+y;
}
void update(ll si,ll ss,ll se,ll qi)
{
if(ss==se)
{
st[si]=ar[ss];
return;
}
ll mid=(ss+se)/2;
if(qi<=mid)
update(2*si,ss,mid,qi);
else
update(2*si+1,mid+1,se,qi);
st[si]=st[2*si]+st[2*si+1];
}
*/
int main()
{
/*ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);*/
long long int a,b,p,q,i;
cin>>a>>b>>p>>q;
double f = (double)(q*a+b*p)/(b+q);
cout.precision(17);
cout<<f;
return 0;
}
| #include<bits/stdc++.h>
#define loop(i, n) for(int i = 0; i < n; i++)
#define pb push_back
#define ll long long int
#define vi vector<int>
#define pi pair<int, int>
#define W(t) int t; cin >> t; while(t--)
#define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ff first
#define ss second
#define rep(i, a, b)for(ll i = a; i <= b; i++)
#define dep(i, a, b)for(ll i = a; i >= b; i--)
#define all(x) x.begin(), x.end()
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.14159265
using namespace std;
const ll mod = (ll)(998244353);
vector<ll> read(int n){
vector<ll> v(n);
loop(i, n)
cin >> v[i];
return v;
}
vector<ll> fact, invf;
ll powmod(ll a, ll b) {
ll res = 1;
a %= mod;
for (; b; b >>= 1) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
}
return res;
}
void build(int n) {
fact = vector<ll>(n+1, 1);
invf = vector<ll>(n+1, 1);
for (int i = 2; i <= n; i++)
fact[i] = fact[i-1] * i % mod;
invf[n] = powmod(fact[n], mod-2);
for (int i = n-1; i >= 2; i--)
invf[i] = invf[i+1] * (i+1) % mod;
}
ll nChoosek(int n, int k) {
if (n < k)
return 0;
return fact[n] * invf[k] % mod * invf[n-k] % mod;
}
ll test = 0;
void solve(){
ll i, j, k, m, n, cnt = 0, ans = 0, sum = 0;
double sx, sy, gx, gy;
cin >> sx >> sy >> gx >> gy;
double res = (gy*sx + gx*sy)/(sy+gy);
cout <<fixed << setprecision(10) << res << "\n";
test++;
}
int main(){
FIO
#ifndef ONLINE_JUDGE
freopen("C:/Codeforces/input.txt", "r", stdin);
freopen("C:/Codeforces/output.txt", "w", stdout);
#endif
ll i = 1;
//W(t)
solve();
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
#define _FastIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb emplace_back
#define pii pair<int , int>
#define F first
#define S second
const int mod = 1000000007;
const int MAXX = 1e6 +5;
int t , n;
string s;
int a[MAXX];
signed main()
{
_FastIO;
cin >> n;
cin >> s;/*
vector<int> v(n + 2 , 0);
q[0] = 0;
int m = 0;
int p = 0;
int tp = 0;
for(int i = 0; i < n; i++){
if(s[i] == '<')
m++;
else{
m--;
if(m < p){
p = m;
tp = i + 1;
}
}
q[i + 1] = m;
}*/
for(int i = 0; i <= n; i++){
cin >> a[i];
}
int k = 100000;
for(int i = 1; i <= n; i++){
k = min(k , abs(a[i] - a[i - 1]));
}
cout << k << endl;
vector<int> q(n + 2 , 0);
vector<int> b(n + 2 , 0);
for(int i = 0; i <= n; i++){
b[i] = a[i] / (k);
q[i] = a[i] % k;
}
for(int i = 1; i < k; i++){
for(int j = 0; j <= n; j++){
if(i <= q[j])
cout << b[j] + 1 << " ";
else
cout << b[j] << " ";
}
cout << endl;
}
for(int i = 0; i <= n; i++){
cout << b[i] << " ";
}
cout << endl;
return 0;
}
| #include <bits/stdc++.h>
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("inline")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #define DEBUG
#ifdef DEBUG
#pragma region
#endif
using namespace std;
#define LL long long
#define PII pair<int, int>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define newl '\n'
#ifdef DEBUG
#define debug(...) cerr << '[', _debug(__VA_ARGS__)
#else
#define debug(...)
#endif
const int INF = 1e9;
template <typename T>
void _debug(T a) {
cerr << a << ']' << endl;
}
template <typename T, typename... Args>
void _debug(T a, Args... args) {
cerr << a << ", ";
_debug(args...);
}
#ifdef DEBUG
#pragma endregion
#endif
int n;
array<int, 2> a[100];
void solve() {
cin >> n;
rep(i, n) { cin >> a[i][0] >> a[i][1]; }
rep(i, n) {
rep(j, n) {
rep(k, n) {
debug(a[i][0], a[i][1], a[j][0], a[j][1], a[k][0], a[k][1]);
if (a[i] == a[j] || a[j] == a[k] || a[i] == a[k]) {
continue;
}
if ((a[i][1] - a[j][1]) * (a[i][0] - (a[k][0])) ==
(a[i][1] - a[k][1]) * (a[i][0] - (a[j][0]))) {
cout << "Yes";
return;
}
}
}
}
cout << "No";
}
int main() {
// freopen("live.in", "r", stdin);
// freopen("live.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
// cout << fixed << setprecision(7);
// int n;
// cin >> n;
// rep(i, n)
// //
solve();
cout << flush;
}
|
//
// Created by K R, Siva Kumar on 02/01/21 at 5:31 PM.
//
#include <bits/stdc++.h>
using namespace std;
const double pi = 2 * acos(0.0);
const int mod = 1e9 + 7;
class Solver {
string a, b;
public:
void solve() {
cin >> a >> b;
int aa = 0, bb = 0;
for (auto i : a) {
aa += (i - '0');
}
for (auto i : b) bb += (i - '0');
cout << max(aa, bb);
}
};
int main() {
#ifndef ONLINE_JUDGE
freopen("/Users/sivkr/Workspace/CompetitiveProgramming/HackerEarth/1.txt",
"r", stdin);
freopen("/Users/sivkr/Workspace/CompetitiveProgramming/HackerEarth/2.txt",
"w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
Solver s;
s.solve();
}
| #include<bits/stdc++.h>
using namespace std;
int main(){
int a,b;
cin>>a>>b;
int sum_a = 0;
int sum_b = 0;
while(a){
sum_a += a%10;
a = a/10;
sum_b += b%10;
b = b/10;
}
cout<<max(sum_a,sum_b)<<endl;
return 0;
} |
#include <cmath>
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<queue>
#include<set>
#include<map>
#include<bitset>
#include<iomanip>
#include<stack>
#include<set>
#include<string>
#include<deque>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll mod = 1000000007;
ll mod2 = 998244353;
int main() {
string n;
ll k;
cin >> n >> k;
for (int i = 0; i < k; i++) {
ll index = 1;
ll ans = 0;
for (int j = n.size() - 1; j >= 0; j--) {
ans += (n[j] - '0') * index;
index *= 10;
}
if (ans % 200 == 0) {
ans /= 200;
n = to_string(ans);
}
else {
n += "200";
}
}
ll ans2 = 0;
ll index = 1;
for (int j = n.size() - 1; j >= 0; j--) {
ans2 += (n[j] - '0') * index;
index *= 10;
}
cout << ans2 << endl;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ll n,k;
cin>>n>>k;
while(k){
if(n%200==0)
n/=200;
else
n=n*1000+200;
k--;
}
cout<<n<<endl;
return 0;
}
|
#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
#define print(x) cout << (x) << endl
typedef long long ll;
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
using namespace std;
using Graph = vector<vector<int>>;
struct UnionFind {
vector<int> par, siz;
UnionFind(int sz): par(sz), siz(sz, 1) {
for (int i = 0; i < sz; ++i) par[i] = i;
}
void init(int sz) {
par.resize(sz);
siz.assign(sz, 1);
for (int i = 0; i < sz; i++) par[i] = i;
}
int root(int x) {
while (par[x] != x) {
x = par[x] = par[par[x]];
}
return x;
}
bool merge(int x, int y) {
x = root(x);
y = root(y);
if (x == y) return false;
if (siz[x] < siz[y]) swap(x, y);
siz[x] += siz[y];
par[y] = x;
return true;
}
bool issame(int x, int y) {
return root(x) == root(y);
}
int size(int x) {
return siz[root(x)];
}
};
int main(){
int n, K; cin >> n >> K;
int a[n][n];
for(int i=0; i<n; i++) for(int j=0; j<n; j++) cin >> a[i][j];
//ๅใซ้ขใใฆ
UnionFind ufc(n);
//่กใซ้ขใใฆ
UnionFind ufr(n);
for(int i=0; i<n-1; i++){
for(int j=i+1; j<n; j++){
int cnt = 0;
for(int k=0; k<n; k++){
if(a[i][k] + a[j][k] <= K) cnt++;
}
if(cnt == n) ufc.merge(i,j);
}
}
for(int i=0; i<n-1; i++){
for(int j=i+1; j<n; j++){
int cnt = 0;
for(int k=0; k<n; k++){
if(a[k][i] + a[k][j] <= K) cnt++;
}
if(cnt == n) ufr.merge(i,j);
}
}
vector<ll> floor(K+2,1);
ll tmp = 1;
for(int i=1; i<K+2; i++){
tmp *= i;
tmp %= MOD2;
floor[i] = tmp;
}
vector<int> cc(n,0), cr(n,0);
tmp = 1;
for(int i=0; i<n; i++){
int t = ufc.root(i);
if(cc[t] == 0){
tmp = tmp * floor[ufc.size(i)] % MOD2;
cc[t] = 1;
}
}
for(int i=0; i<n; i++){
int t = ufr.root(i);
if(cr[t] == 0){
tmp = tmp * floor[ufr.size(i)] % MOD2;
cr[t] = 1;
}
}
print(tmp);
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n,k;
cin >> n >> k;
ll ans = 0;
vector<int> cnt(n);
for (int i = 0; i < n; i++) cnt[i] = 0;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
cnt[a]++;
}
unordered_map<int,bool> mp[k];
for (int i = 0; i < n; i++) {
for (int j = 0; j < min(cnt[i],k); j++) {
mp[j][i] = true;
}
}
for (int i = 0; i < k; i++) {
for (int j = 0; j < n; j++) {
if (!mp[i][j]) {
ans += j;
break;
}
}
}
cout << ans << endl;
} |
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <string>
#include <sstream>
#include <set>
#include <functional>
#include <map>
#include <unordered_map>
#include <queue>
#include <iomanip>
#include <stack>
using namespace std;
typedef long long ll;
#define repi(i,a,b) for(ll i=a;i<b;i++)
#define rep(i,a) repi(i,0,a)
#define all(u) u.begin(),u.end()
using Interval = pair<ll,ll>;
using Graph = vector<vector<ll> >;
#define fi first
#define se second
struct Edge{
ll to;
ll w;
Edge(ll to, ll w):to(to),w(w){}
};
using wGraph = vector<vector<Edge> >;
template<class T> void chmax(T& a, T b){
if (a < b){
a=b;
}
}
template<class T> void chmin(T& a, T b){
if (a > b){
a=b;
}
}
template<class T> bool chmaxb(T& a, T b){
if (a < b){
a=b;
return true;
}
return false;
}
template<class T> bool chminb(T& a, T b){
if (a > b){
a = b;
return true;
}
return false;
}
const ll INF = 1LL << 60;
ll a,b;
vector<ll> cnt(200010,0);
int main(){
cin >> a >> b;
repi(k,a,b+1){
repi(i,1,b+1){
if (i*i > k) break;
if (k % i == 0){
cnt[i]++;
cnt[k/i]++;
if (i == k/i){
cnt[i]--;
}
}
}
}
ll ans = 0;
rep(i,200010){
if (cnt[i] >= 2){
ans = max(ans,i);
}
}
cout << ans << endl;
return 0;
} | #include <stdio.h>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <unordered_map>
#include <vector>
#define ALL(obj) (obj).begin()(obj).end()
#define DEBUG(x) cerr << #x << " = " << x << endl
using namespace std;
int main() {
int n;
std::cin >> n;
std::string s;
std::cin >> s;
std::set<std::pair<int, int>> undecided;
for (int i = 0; i < n; i++) {
undecided.insert(std::make_pair(i, s[i]));
}
auto pointer = undecided.begin();
int ans = 0;
while (pointer != undecided.end()) {
auto origin_ptr = pointer;
if (pointer->second == 'f') {
pointer++;
if (pointer != undecided.end() && pointer->second == 'o') {
pointer++;
if (pointer != undecided.end() && pointer->second == 'x') {
pointer = origin_ptr;
for (int i = 0; i < 3; ++i) {
pointer = undecided.erase(pointer);
}
ans += 1;
if (pointer != undecided.begin()) {
pointer = std::prev(pointer);
}
if (pointer != undecided.begin()) {
pointer = std::prev(pointer);
}
continue;
}
}
}
pointer = origin_ptr;
pointer++;
}
std::cout << n - ans * 3 << std::endl;
}
|
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define all(x) (x).begin(),(x).end()
#define uniq(v) (v).erase(unique(all(v)),(v).end())
#define sz(x) (int)((x).size())
#define fr first
#define sc second
#define pii pair<int,int>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define mem1(a) memset(a,-1,sizeof(a))
#define mem0(a) memset(a,0,sizeof(a))
#define ppc __builtin_popcount
#define ppcll __builtin_popcountll
template<typename T1,typename T2>istream& operator>>(istream& in,pair<T1,T2> &a){in>>a.fr>>a.sc;return in;}
template<typename T1,typename T2>ostream& operator<<(ostream& out,pair<T1,T2> a){out<<a.fr<<" "<<a.sc;return out;}
template<typename T,typename T1>T amax(T &a,T1 b){if(b>a)a=b;return a;}
template<typename T,typename T1>T amin(T &a,T1 b){if(b<a)a=b;return a;}
const long long INF=1e18;
const int32_t M=1e9+7;
const int32_t MM=998244353;
const int N=0;
void solve(){
int n;
cin >> n;
int a = 0;
rep(i,0,n){
int x;
cin >> x;
amax(a,x);
}
int b = 1002;
rep(i,0,n){
int x;
cin >> x;
amin(b,x);
}
cout << max(0LL,b-a+1);
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#ifdef SIEVE
sieve();
#endif
#ifdef NCR
init();
#endif
int t=1;
//cin>>t;
while(t--) solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main()
{
int A, B;
cin >> A >> B;
cout << 2 * A + 100 - B << endl;
return 0;
} |
#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
using namespace std;
const long long int INF = 1e18;
const long long int MOD = 1e9+7;
typedef pair<ll, ll> pairs;
typedef vector<pairs> p;
#define ALL(obj) (obj).begin(),(obj).end()
struct edge{
ll to, cost;
};
template<class T>
inline bool chmax(T &a, T b) {
if(a < b) {
a = b;
return true;
}
return false;
}
template<class T>
inline bool chmin(T &a, T b) {
if(a > b) {
a = b;
return true;
}
return false;
}
// ๆฐๅญใฎๆกๆฐ
ll keta(ll N){
int tmp{};
while( N > 0 ){
tmp += ( N % 10 );
N /= 10;
}
N = tmp;
return N;
}
// ในใไนใฎไฝใ
ll modpow(ll a, ll n){
ll res = 1;
while(n){
if(n & 1){
res = res * a % MOD;
}
a = a * a % MOD;
n = n >> 1;
}
return res;
}
// ใณใณใใใผใทใงใณ
ll MAXN = 2e5+10;
vector<ll> inv(MAXN);
ll nCk(ll n, ll k) {
ll res = 1;
for(ll i = 1; i <= k; i++) {
res = res*(n-i+1) % MOD;
}
for(ll i = 1; i <= k; i++) {
res = res*inv[i] % MOD;
}
return res;
}
// ---------------------------------------------
int main(){
ll l;
cin>>l;
ll ans = 1;
for(ll i=1;i<=11;++i){
ans *= (l - i);
ans /= i;
}
cout<<ans<<endl;
return 0;
} | #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
int N; cin >> N;
vector<int> a(N);
rep(i,N) cin >> a[i];
int ans = 0;
for(int l = 0; l < N; l++){
int x = a[l];
for(int r = l; r < N; r++){
x = min(x, a[r]);
ans = max(ans, x * (r - l + 1));
}
}
cout << ans << endl;
} |
#include<bits/stdc++.h>
using namespace std;
int a,b,c;
int main(){
cin>>a>>b>>c;
if(a*a+b*b<c*c)cout<<"Yes";else cout<<"No";
} | #include<bits/stdc++.h>
#define INF 2000000000
#define MOD 1000000007
#define EPS (1e-10)
using namespace std;
int main(int argc, char *argv[]) {
int N,S,D; cin >> N >> S >> D;
vector<int> X(N,0);
vector<int> Y(N,0);
for (int i = 0; i < N; i++) {
cin >> X[i] >> Y[i];
if (!(X[i] >= S or Y[i] <= D)) {
cout << "Yes" << endl;
return 0;
}
}
cout << "No" << endl;
return 0;
}
|
#pragma GCC optimize ("O2")
#pragma GCC target ("avx2")
#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please
const int mod = 1e9 + 7;
int dp[3][100][100];
constexpr ll modpow(ll A, ll B) {
ll kotae = 1;
while (B > 0) {
if (B & 1) kotae = kotae * A % mod;
A = A * A % mod;
B >>= 1;
}
return kotae;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, M, K;
cin >> N >> M >> K;
int A[100];
rep(i, N) {
cin >> A[i];
}
rep(i, N) dp[0][i][i] = 1;
rep(i, N) dp[1][i][i] = 1;
int are = modpow(2 * M, mod - 2);
rep(i, M) {
int x, y;
cin >> x >> y;
x--;
y--;
(dp[0][x][x] += mod - are) %= mod;
(dp[0][x][y] += are) %= mod;
(dp[0][y][x] += are) %= mod;
(dp[0][y][y] += mod - are) %= mod;
}
auto mae = dp[1], ato = dp[2];
rep(i, 30) {
rep(x, N) rep(y, N) {
ll tmp = 0;
rep(z, N) {
tmp += mae[x][z] * (ll)mae[z][y] % mod;
}
ato[x][y] = tmp % mod;
}
swap(mae, ato);
if (K >> 29 - i & 1) {
rep(x, N) rep(y, N) {
ll tmp = 0;
rep(z, N) {
tmp += mae[x][z] * (ll)dp[0][z][y] % mod;
}
ato[x][y] = tmp % mod;
}
swap(mae, ato);
}
}
rep(i, N) {
ll kotae = 0;
rep(j, N) kotae += (ll)mae[i][j] * A[j] % mod;
co(kotae % mod);
}
Would you please return 0;
} | /*
* @date:2021-04-24 20:37:33
* @source:
*/
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100 + 5;
const int MOD = 1e9 + 7;
int N, M, K;
long long A[MAXN];
int Num[MAXN];
struct Matrix {
long long A[MAXN][MAXN];
Matrix() {
memset(A, 0, sizeof A);
}
} a, c;
Matrix mul(const Matrix &x, const Matrix &y) {
memset(c.A, 0, sizeof c.A);
for (int i = 1; i <= N; ++i) {
for (int j = 1; j <= N; ++j) {
for (int k = 1; k <= N; ++k) {
c.A[i][j] = (c.A[i][j] + x.A[i][k] * y.A[k][j] % MOD) % MOD;
}
}
}
return c;
}
long long poww(long long x, int t) {
long long res = 1;
for (; t; t >>= 1) {
if (t & 1) res = res * x % MOD;
x = x * x % MOD;
}
return res;
}
Matrix poww(Matrix &x, int t) {
Matrix res = x;
for (--t; t; t >>= 1) {
if (t & 1) res = mul(res, x);
x = mul(x, x);
}
return res;
}
int main() {
scanf("%d%d%d", &N, &M, &K);
for (int i = 1; i <= N; ++i) scanf("%lld", &A[i]);
if (!K) {
for (int i = 1; i <= N; ++i) {
printf("%lld\n", A[i]);
}
return 0;
}
long long p = poww(M, MOD - 2), two = poww(2, MOD - 2);
for (int i = 1, u, v; i <= M; ++i) {
scanf("%d%d", &u, &v);
++Num[u], ++Num[v];
a.A[u][v] = a.A[v][u] = p * two % MOD;
}
for (int i = 1; i <= N; ++i) {
a.A[i][i] = p * (M - Num[i] + two * Num[i]) % MOD;
}
a = poww(a, K);
for (int i = 1; i <= N; ++i) {
long long x = 0;
for (int j = 1; j <= N; ++j) {
x = (x + A[j] * a.A[i][j]) % MOD;
}
printf("%lld\n", x);
}
return 0;
} |
/*
author : TAPAN SAVANI
codeforces : savanitapan2001
codechef : savanitapan17
*/
/*
------------------------------------------------------------------------
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
------------------------------------------------------------------------
*/
#include <bits/stdc++.h>
using namespace std;
#define HAPPY_CODING \
ios_base::sync_with_stdio(0); \
cin.tie(NULL); \
cout.tie(NULL);
#define pb push_back
#define mp make_pair
#define Debug(x) cout << #x " = " << (x) << endl
#define SORT(a) sort(a.begin(), a.end())
#define SORTR(a) sort(a.rbegin(), a.rend())
#define mod 1000000007
#define pi 3.141592653589793238
#define ll long long int
#define ull unsigned long long
#define be begin()
#define en end()
#define FOR(i, a, b) for (long long int i = a; i < b; i++)
#define FORI(i, a, b) for (int i = a; i >= b; i--)
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PI;
typedef pair<ll, ll> PL;
typedef vector<PI> VPI;
int main()
{
HAPPY_CODING;
int n, k;
cin >> n >> k;
if (n == 0 || k == 0)
{
cout << n << "\n";
return 0;
}
VI x;
int temp = n, ans = 0;
while (temp)
{
x.pb(temp % 10);
temp /= 10;
}
while (k--)
{
int first = 0;
SORT(x);
FOR(i, 0, x.size())
{
first *= 10;
first += x[i];
}
int second = 0;
SORTR(x);
FOR(i, 0, x.size())
{
second *= 10;
second += x[i];
}
ans = second - first;
int temp = ans;
x.clear();
while (temp)
{
x.pb(temp % 10);
temp /= 10;
}
if (ans == 0)
break;
}
cout << ans << "\n";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int g1(int x) {
priority_queue<int> q;
int ans = 0;
while (x > 0) {
q.push(x % 10);
x /= 10;
}
while (!q.empty()) {
ans *= 10;
ans += q.top();
q.pop();
}
return ans;
}
int g2(int x) {
priority_queue<int, vector<int>, greater<int>> q;
int ans = 0;
while (x > 0) {
q.push(x % 10);
x /= 10;
}
while (!q.empty()) {
ans *= 10;
ans += q.top();
q.pop();
}
return ans;
}
int f(int x) { return g1(x) - g2(x); }
int main(void) {
int N, K;
cin >> N >> K;
int a = N;
for (int i = 0; i < K; ++i) {
a = f(a);
}
printf("%d\n", a);
return 0;
}
|
#include <iostream>
#include <algorithm>
using namespace std;
long long N, K;
long long calc(long long x) {
if (x<2) return 0;
if (x > 2*N) return 0;
return min(x-1, N*2-x+1);
}
int main(void) {
cin >> N >> K;
unsigned long long ans = 0;
for (long long ab = 2; ab <= 2*N; ab++) {
long long cd = ab-K;
unsigned long long abptn = calc(ab);
// unsigned long long cdptn = min(cd-1, N*2-cd+1);
unsigned long long cdptn = calc(cd);
ans += abptn*cdptn;
}
cout << ans << endl;
return 0;
}
| #pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define FastIO ios_base::sync_with_stdio(0);cin.tie(0),cout.tie(0)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mem(x,i) memset(x,i,sizeof(x))
#define ff first
#define ss second
#define popcount(x) __builtin_popcount(x)
#define all(x) x.begin(),x.end()
#define Case(t) for(int ks=1;ks<=t;ks++)
#define fileout freopen("output.txt","w",stdout)
#define filein freopen("input.txt","r",stdin)
#define D(x) cerr<< __LINE__ <<": "<< #x << " = "<<(x) <<'\n';
#define E(x,y) cerr<< __LINE__ <<": "<< #x << " = "<<(x)<<' '<< #y << " = "<<(y) <<'\n';
#define endl '\n'
///Input-Output
#define si(x) scanf("%d",&x)
#define sii(x,y) scanf("%d%d",&x,&y)
#define sl(x) scanf("%lld",&x)
#define sll(x,y) scanf("%lld%lld",&x,&y)
///
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pil = pair<int,ll>;
template <typename T> using orderedSet =
tree<T, null_type, less<T>,
rb_tree_tag, tree_order_statistics_node_update>;
/*order_of_key(k) - number of element strictly less than k.
find_by_order(k) - k'th element in set.(0 indexed)(iterator)*/
/*------------------------------Graph Moves----------------------------*/
//const int fx[]= {+1,-1,+0,+0};
//const int fy[]= {+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1}; // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1}; // Kings Move
//const int fx[]={-2, -2, -1, -1, 1, 1, 2, 2}; // Knights Move
//const int fy[]={-1, 1, -2, 2, -2, 2, -1, 1}; // Knights Move
/*---------------------------------------------------------------------*/
//template<typename T>inline T gcd(T a, T b){T c;while (b){c = b;b = a % b;a = c;}return a;}
//ll powmod(ll a,ll b){ll res=1;a%=mod;assert(b>=0);for(; b; b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
/*----------------------BitMask-----------------------------------------*/
//inline int setBit(int N,int pos){return N=N | (1<<pos);}
//inline int resetBit(int N,int pos){return N= N & ~(1<<pos);}
//inline bool checkBit(int N,int pos){return (bool) (N& (1<<pos));}
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod =998244353;
const int mx = 2e5+5;
const int lg = 26;
/*--------------------------------------------------------------*/
int main()
{
ll n,k;
sll(n,k);
ll ans = 0;
for(ll i=1;i<=n*2;i++)
{
ll x = i;
ll y = x-k;
if(x<=0 || y<=0 || x>n*2 || y>n*2) continue;
if(x>n)x=x-(x-n-1)*2;
if(y>n) y=y-(y-n-1)*2;
ans+=(x-1)*(y-1);
//E(x,y);
// D(ans);
}
cout<<ans<<endl;
}
|
#include <iostream>
#define int long long
signed main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int a, b, c;
std::cin >> a >> b >> c;
std::cout << (a + b + c) - std::min(a, std::min(b, c)) << std::endl;
return 0;
}
| #include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/pb_ds/tree_policy.hpp"
using namespace __gnu_pbds;
using namespace std;
/* Written by Nishant Mittal aka nishantwrp */
#define FAST std::ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
/* Long Long Int */
#define int long long int
#define modnum 1000000007
#define mp make_pair
#define pb push_back
#define sz(a) (int)a.size()
#define precise(a) fixed<<setprecision(a)<<
#define F first
#define S second
#define I insert
#define pii pair<int,int>
#define trav(a, x) for(auto& a : x)
#define rep(n) for(int i = 0;i<n;i++)
#define invect(data,n,commands) for(int i = 0;i<n;i++){int tmp;cin>>tmp;data.pb(tmp);commands}
#define display(x) trav(a,x) cout<<a<<" "; cout<<endl;
#define all(data) data.begin(), data.end()
#define endl '\n'
#define last(a) a[sz(a)-1]
#define INF 1000000000000000000
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
std::cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
typedef tree <int, null_type, greater <int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
// order_of_key(val): returns the number of values greater than val
// find_by_order(k): returns an iterator to the kth largest element (0-based)
// NOTE: For reverse ordered set (that does same things for lower than)
// typedef tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
//primes for hashing 937,991,1013,1409,1741
//Returns index of first element not less than key and more than key respectively
#define lb(data,key) lower_bound(all(data),key) - data.begin();
#define ub(data,key) upper_bound(all(data),key) - data.begin();
// Random Number Generator
std::mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
void nishantwrp(int cas) {
vector <int> data;
invect(data,3,)
sort(data.begin(),data.end());
cout<<data[2] + data[1];
}
signed main()
{
FAST
int t = 1;
// cin>>t;
int cas = 1;
while(cas <= t) {
nishantwrp(cas);
cas++;
}
return 0;
} |
#include <bits/stdc++.h>
const long long MOD1 = 1000000007;
const long long MOD2 = 998244353;
#define logn long
#define lnog long
#define itn int
using namespace std;
int main() {
long long N; cin >> N;
string T; cin >> T;
for (long long i = 1; i < N; i++) {
if (T.at(i - 1) == '0' && T.at(i) == '0') {
cout << 0 << endl;
return 0;
}
}
for (long long i = 1; i < N - 1; i++) {
if (T.at(i - 1) == '0' && T.at(i) == '1' && T.at(i + 1) == '0') {
cout << 0 << endl;
return 0;
}
else if (T.at(i - 1) == '1' && T.at(i) == '1' && T.at(i + 1) == '1') {
cout << 0 << endl;
return 0;
}
}
long long num = 0;
for (long long i = 0; i < N; i++) {
num += (T.at(i) == '0');
}
if (T == "1") {
cout << 2 * 10000000000 << endl;
}
else if (T == "11") {
cout << 10000000000 << endl;
}
else {
cout << 10000000000 - num + (T.at(N - 1) == '0') << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second
using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;
const double eps = 1e-10;
template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}
template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}
template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}
template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}
template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
for(auto &e:u) fill_v<T>(e,v...);
}
int main(void) {
cin.tie(0);
ios::sync_with_stdio(false);
int64 N;
cin >> N;
string T;
string S = "110";
cin >> T;
int64 res = 0;
REP(s, 3) {
bool ok = 1;
REP(i, N) {
if (S[(s + i)%3] != T[i]) {
ok = 0;
break;
}
}
if (ok) {
int64 x = (N + s) % 3;
int64 y = (30000000000LL - (3-x)%3 - N + 1);
res += (y + 2) / 3;
}
}
cout << res << endl;
}
|
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring>
#include <iostream>
#include <sstream>
#include <numeric>
#include <map>
#include <set>
#include <queue>
#include <vector>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef pair<LL, LL> II;
typedef pair<LL, II> III;
static const LL INF = 1LL << 60;
static const long long MOD = 998244353;
static const size_t MAX_N = 1000001;
struct modll {
long long x;
modll() { }
modll(int _x) : x(_x) { }
operator int() const { return (int)x; }
modll operator+(int y) { return (x + y + MOD) % MOD; }
modll operator+=(int y) { x = (x + y + MOD) % MOD; return *this; }
modll operator-(int y) { return (x - y + MOD) % MOD; }
modll operator-=(int y) { x = (x - y + MOD) % MOD; return *this; }
modll operator*(int y) { return (x * y) % MOD; }
modll operator*=(int y) { x = (x * y) % MOD; return *this; }
modll operator/(int y) { return (x * modpow(y, MOD - 2)) % MOD; }
modll operator/=(int y) { x = (x * modpow(y, MOD - 2)) % MOD; return *this; }
static modll modinv(int a) { return modpow(a, MOD - 2); }
static modll modpow(int a, int b) {
modll x = a, r = 1;
for (; b; b >>= 1, x *= x) if (b & 1) r *= x;
return r;
}
};
modll combination(LL n, LL r) {
static modll fact[MAX_N + 1], inv[MAX_N + 1];
if (!fact[0]) {
fact[0] = 1;
for (int i = 1; i <= MAX_N; ++i) {
fact[i] = fact[i - 1] * i;
}
inv[MAX_N] = modll::modinv(fact[MAX_N]);
for (int i = MAX_N; i >= 1; --i) {
inv[i - 1] = inv[i] * i;
}
}
if (r > n) {
return 0;
}
return (fact[n] * inv[r]) * inv[n - r];
}
LL solve(long long H, long long W, std::vector<std::string> &S) {
modll ans = 1;
for (int i = 0; i < H + W; ++i) {
int ac = 0, rc = 0, bc = 0;
for (int c = 0; c < W; ++c) {
int r = i - c;
if (r >= 0 && r < H) {
++ac;
if (S[r][c] == 'R') ++rc;
if (S[r][c] == 'B') ++bc;
}
}
if (ac) {
if (rc && bc) {
return 0;
}
if (rc == 0 && bc == 0) {
ans *= 2;
}
}
}
return ans;
}
int main() {
long long H, W;
std::cin >> H >> W;
std::vector<std::string> S(H);
for (int i = 0; i < H; i++) {
std::cin >> S[i];
}
cout << solve(H, W, S) << endl;
return 0;
}
| #line 1 "main.cpp"
/**
* @title Template
*/
#include <iostream>
#include <algorithm>
#include <utility>
#include <numeric>
#include <vector>
#include <array>
#include <cassert>
#include <map>
#line 2 "/Users/kodamankod/Desktop/cpp_programming/Library/other/chmin_chmax.cpp"
template <class T, class U>
constexpr bool chmin(T &lhs, const U &rhs) {
if (lhs > rhs) { lhs = rhs; return true; }
return false;
}
template <class T, class U>
constexpr bool chmax(T &lhs, const U &rhs) {
if (lhs < rhs) { lhs = rhs; return true; }
return false;
}
/**
* @title Chmin/Chmax
*/
#line 2 "/Users/kodamankod/Desktop/cpp_programming/Library/other/range.cpp"
#line 4 "/Users/kodamankod/Desktop/cpp_programming/Library/other/range.cpp"
class range {
struct iter {
std::size_t itr;
constexpr iter(std::size_t pos) noexcept: itr(pos) { }
constexpr void operator ++ () noexcept { ++itr; }
constexpr bool operator != (iter other) const noexcept { return itr != other.itr; }
constexpr std::size_t operator * () const noexcept { return itr; }
};
struct reviter {
std::size_t itr;
constexpr reviter(std::size_t pos) noexcept: itr(pos) { }
constexpr void operator ++ () noexcept { --itr; }
constexpr bool operator != (reviter other) const noexcept { return itr != other.itr; }
constexpr std::size_t operator * () const noexcept { return itr; }
};
const iter first, last;
public:
constexpr range(std::size_t first, std::size_t last) noexcept: first(first), last(std::max(first, last)) { }
constexpr iter begin() const noexcept { return first; }
constexpr iter end() const noexcept { return last; }
constexpr reviter rbegin() const noexcept { return reviter(*last - 1); }
constexpr reviter rend() const noexcept { return reviter(*first - 1); }
};
/**
* @title Range
*/
#line 17 "main.cpp"
using i32 = std::int32_t;
using i64 = std::int64_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;
using isize = std::ptrdiff_t;
using usize = std::size_t;
constexpr i32 inf32 = (i32(1) << 30) - 1;
constexpr i64 inf64 = (i64(1) << 62) - 1;
usize find(std::vector<usize> &vec, usize x) {
if (vec[x] == x) {
return x;
}
return vec[x] = find(vec, vec[x]);
}
int main() {
usize N, Q;
std::cin >> N >> Q;
std::vector<std::map<usize, usize>> map(N);
for (auto i: range(0, N)) {
usize c;
std::cin >> c;
c -= 1;
map[i][c] += 1;
}
std::vector<usize> par(N);
for (auto i: range(0, N)) {
par[i] = i;
}
while (Q--) {
usize t;
std::cin >> t;
if (t == 1) {
usize a, b;
std::cin >> a >> b;
a -= 1;
b -= 1;
a = find(par, a);
b = find(par, b);
if (a != b) {
if (map[a].size() < map[b].size()) {
std::swap(a, b);
}
for (auto [x, y]: std::move(map[b])) {
map[a][x] += y;
}
par[b] = a;
}
}
else {
usize a, b;
std::cin >> a >> b;
a -= 1;
b -= 1;
a = find(par, a);
auto itr = map[a].find(b);
if (itr != map[a].end()) {
std::cout << itr -> second << '\n';
}
else {
std::cout << "0\n";
}
}
}
return 0;
}
|
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
char a , b ,c ;
cin >>a >> b >>c;
cout<< b << c << a;
return 0;
}
| #include <iostream>
#include <string>
#include <vector>
#include <utility>
using ll=long long;
using namespace std;
int main(){
string S;
cin>>S;
int slen=S.size();
int big;
int ans=1;
for(int i=0;i<slen;i++){
if(S[i] >= 'a'){
big=0;
}else{
big=1;
}
if(i%2 != big){
ans=0;
}
}
if(ans){
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define N 10005
bitset<N>p[5],f[3];
int n,i,l,r,cnt,mid,a[N][10],c[10],fl,b[5][5],e[10];
bool check(){
int i;
for(i=1;i<=n;++i)f[0][i]=f[1][i]=f[2][i]=1;
for(i=0;i<5;++i){
f[e[i]]&=p[i];
}
if(f[0].count()&&f[1].count()&&f[2].count())return 1;
return 0;
}
void dfs(int now){
if(now==5){fl|=check();return ;}
e[now]=0;dfs(now+1);e[now]=1;dfs(now+1);e[now]=2;dfs(now+1);
}
int check(int x){
for(int i=0;i<5;++i){
for(int j=1;j<=n;++j){
if(a[j][i]>=x)p[i][j]=1;else p[i][j]=0;
}
}
fl=0;dfs(1);if(fl)return 1;return 0;
}
int main(){
cin>>n;
for(i=1;i<=n;++i)cin>>a[i][0]>>a[i][1]>>a[i][2]>>a[i][3]>>a[i][4];
l=0;r=1e9;
while(l<r){
mid=r-(r-l>>1);
if(check(mid))l=mid;
else r=mid-1;
} cout<<l;
} | #include <bits/stdc++.h>
using namespace std;
#define ll int64_t
#include <cstdlib>
#include <math.h>
#include<cstdio>
#include<cstring>
#include <math.h>
#define vi v(n) vector<ll> v(n);
#define FOR(I, A, B) for (ll I = (A); I <= (B); I++)
#define fo(i,n) for(ll i=0;i<n;i++)
#define sz(a) ll((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define dbg(x) cout << #x << " = " << x << endl
#define dbg2(x,y) cout << #x << " = " << x << ", " << #y << " = " << y << endl
#define dbg3(x,y,z) cout << #x << " = " << x << ", " << #y << " = " << y << ", " << #z << " = " << z << endl
#define dbg4(x,y,z,q) cout << #x << " = " << x << ", " << #y << " = " << y << ", " << #z << " = " << z << ", " << #q << " = " << q << endl
#define scan(char_array) scanf("%[^\n]s",&char_array)
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int main(){
IOS
ll n;
cin>>n;
ll dp[n+1][3][35];
fo(i,n+1){
fo(j,3){
fo(k,35){
dp[i][j][k]=0;
}
}
}
ll a,b,c,d,e;
FOR(i,1,n){
cin>>a>>b>>c>>d>>e;
vector<ll> myVals{a,b,c,d,e};
fo(j,3){
fo(k,32){
ll val=dp[i-1][j][k];
fo(l,32){
if(k|l==k){
if(j==0){
if(l!=k) continue;
ll my=10000000000000;
fo(m,5){
if(l&(1<<m)){
my=min(my,myVals[m]);
}
}
if(my==10000000000000) my=0;
val=max(val,my);
}
else if(i>=j+1){
ll my=10000000000000;
fo(m,5){
if(l&(1<<m)){
my=min(my,myVals[m]);
}
}
if(my==10000000000000) my=0;
my=min(my,dp[i-1][j-1][l^k]);
val=max(val,my);
}
}
}
dp[i][j][k]=val;
}
}
}
// dbg(dp[1][0][1]);
cout<<dp[n][2][31]<<endl;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vz;
typedef vector<vz> vvz;
typedef pair<ll, ll> pll;
#define sz(x) ll((x).size())
#define fo(i,n) for(ll i=0; i<(n); i++)
#define FO(i,a,b) for(ll i=(a); i!=(b); i+=(b)>(a)?1:-1)
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(c) c.begin(), c.end()
#define F first
#define S second
#define os(x) cout<<x<<' '
#define on(x) cout<<x<<"\n"
#define nl cout<<"\n"
#define maxe(x) max_element(all(x))-(x).begin()
#define mine(x) min_element(all(x))-(x).begin()
const ll P=998244353;
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("C:\\Users\\ASISH\\Desktop\\coding\\input1.txt", "r", stdin);
freopen("C:\\Users\\ASISH\\Desktop\\coding\\output1.txt", "w", stdout);
#endif
ll n, m, mi=10000000;
cin>>n>>m;
vll a(n*m);
fo(i, n*m){
cin>>a[i];
mi=min(mi, a[i]);
}
ll ans=0;
fo(i, n*m){
ans+=a[i]-mi;
}
on(ans);
cerr << "Time : " << ((double)clock()) / (double)CLOCKS_PER_SEC << "s\n";
return 0;
} | #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define eb emplace_back
using namespace std;
template <class T = int>
using V = vector<T>;
template <class T = int>
using VV = V<V<T>>;
using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pdd = pair<ld,ld>;
struct edge{
int to, c;
edge(int to, int c): to(to), c(c) {}
};
const int INF = 1<<30;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
VV<edge> g(n);
rep(i,m){
int a, b; char c;
cin >> a >> b >> c;
a--, b--;
g[a].eb(b,c-'a');
g[b].eb(a,c-'a');
}
VV<int> dp(n, V<int>(n, INF));
queue<pii> que;
auto push = [&](int a, int b, int x){
if(dp[a][b] != INF) return;
dp[a][b] = x;
que.emplace(a,b);
};
push(0,n-1,0);
while(!que.empty()){
int a = que.front().first, b = que.front().second;
que.pop();
for(auto ea : g[a]) for(auto eb : g[b]){
if(ea.c != eb.c) continue;
push(ea.to, eb.to, dp[a][b] + 1);
}
}
int ans = dp[n-1][0];
if(ans == INF) ans = -1;
cout << ans << endl;
} |
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define V vector<long long>
#define VV vector<vector<long long>>
#define VVV vector<vector<vector<long long>>>
#define P pair<ll,ll>
#define rep(i,n) for(ll (i)=0;(i)<(n);++(i))
#define per(i,n) for(ll (i)=(n)-1;(i)>=0;--(i))
#ifdef LOCAL
#define debug(x) cerr<<__LINE__<<": "<<#x<<"="<<x<<endl;
#define debugALL(x) cerr<<__LINE__<<": "<<#x<<"=";for(auto i=(x).begin();i!=--(x).end();i++)cerr<<*i<<" ";cerr<<*--(x).end()<<endl;
#else
#define debug(x) true
#define debugALL(x) true
#endif
using namespace std;
int main(){
ll n;
cin>>n;
string s,t;
cin>>s>>t;
V a,b;
rep(i,n){
if(s[i]=='0')a.emplace_back(i);
if(t[i]=='0')b.emplace_back(i);
}
if(a.size()!=b.size()){
cout<<-1<<endl;
return 0;
}
ll ans=0;
rep(i,a.size())if(a[i]!=b[i])ans++;
cout<<ans<<endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
ll int n,a=0,b=0,c=0,d=0,e=0,f=0,g=0,x=0,y=0,z=0,c1=0,c2=0,k=0,l=0;
cin>>n;
string s,t;
cin>>s>>t;
for(ll int i=0;i<n;i++)
{
if(s[i]=='1'){a++;}
if(s[i]=='0'){b++;}
if(t[i]=='1'){c++;}
if(t[i]=='0'){d++;}
}
if(a==c&&b==d)
{
// for(ll int i=0;i<n;i++)
// {
// if(s[i]=='0'){e++;}
// if(t[i]=='0'){f++;}
// if(f>e)
// {
// g=1;
// c1=i;
// break;
// }
// }
// for(ll int i=n-1;i>=0;i--)
// {
// if(s[i]=='0'){x++;}
// if(t[i]=='0'){y++;}
// if(y>x)
// {
// z=1;
// c2=i;
// break;
// }
// }
// if(z==1)
// {
// for(ll int i=c2;i>=0;i--)
// {
// if(t[i]=='0')
// {
// k++;
// }
// }
// cout<<k;
// }
// else if(g==1)
// {
// for(ll int i=c1;i<n;i++)
// {
// if(t[i]=='0')
// {
// k++;
// }
// }
// cout<<k;
// }
// else if(z==0&&g==0)
// {
// cout<<0;
// }
// else
// {
for(ll int i=0;i<n;i++)
{
if(s[i]=='0'){x++;}
if(t[i]=='0'){y++;}
if(y>x&&t[i]=='0')
{
k++;
}
// cout<<k<<" "<<i<<" || ";
}
for(ll int i=n-1;i>=0;i--)
{
if(s[i]=='0'){e++;}
if(t[i]=='0'){f++;}
if(f>e&&t[i]=='0')
{
k++;
}
// cout<<k<<" "<<i<<" || ";
}
cout<<k;
// }
}
else
{
cout<<-1;
}
} |
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
using namespace std;
using LL = long long;
using P = pair<int,string>;
using vv = vector<vector<int>>;
const int INF = (int)1e9;
const LL LINF = (LL)1e18;
const LL sc = (int)1e8;
const double TL = 2.85;
const int Max_N = 20;
const int Max_K = 8;
const int Max_length = 12;
const double blank_poss = 1.00;
int N, M;
vector<P> s;
vector<vector<char>> mat;
map<string,int> mp;
LL score(){
int blank = 0, hit = 0;
rep(i,N){
rep(j,N) if(mat[i][j] == '.') blank++;
}
rep(i,M) if(mp[s[i].second] > 0) hit++;
LL res = sc;
if(hit < M){
res *= hit;
res /= M;
}
else{
res *= N * N * 2;
res /= (N * N * 2 - blank);
}
return res;
}
void del(int h, int w){
for(int l = 2; l <= Max_length; l++){
rep(i,l){
string t1, t2;
int st = h - i;
if(st < 0) st += N;
rep(j,l) t1 += mat[(st+j)%N][w];
st = w - i;
if(st < 0) st += N;
rep(j,l) t2 += mat[h][(st+j)%N];
mp[t1]--;
mp[t2]--;
}
}
}
void add(int h, int w, char c){
mat[h][w] = c;
for(int l = 2; l <= Max_length; l++){
rep(i,l){
string t1, t2;
int st = h - i;
if(st < 0) st += N;
rep(j,l) t1 += mat[(st+j)%N][w];
st = w - i;
if(st < 0) st += N;
rep(j,l) t2 += mat[h][(st+j)%N];
mp[t1]++;
mp[t2]++;
}
}
}
int main(){
//FILE *outputfile;
//outputfile = freopen("test.txt", "w", stdout);
random_device rnd;
mt19937 mt(rnd());
uniform_int_distribution<> pos(0, Max_N - 1);
uniform_int_distribution<> code(0, Max_K - 1);
uniform_real_distribution<> R(0.0, 1.0);
cin >> N >> M;
s.resize(M);
rep(i,M){
string t;
cin >> t;
s[i] = P((int)t.size(), t);
}
sort(s.begin(), s.end());
mat.resize(N);
rep(i,N){
rep(j,N){
int k = code(mt);
char c = 'A' + k;
double r = R(mt);
if(r < blank_poss) mat[i].push_back('.');
else mat[i].push_back(c);
}
}
int now_h = 0, now_w = 0;
rep(i,M){
string t = s[i].second;
int siz = t.size();
if(now_w + siz > N){
if(now_h == N - 1) break;
else{
now_h++;
now_w = 0;
}
}
rep(j,siz) add(now_h, now_w + j, t[j]);
now_w += siz;
}
double T0 = 1e5, T1 = 5e2;
while(1.0 * clock() / CLOCKS_PER_SEC < TL){
double t = 1.0 * clock() / CLOCKS_PER_SEC / TL;
double temp = pow(T0, 1.0 - t) * pow(T1, t);
LL now_score = score();
int h = pos(mt), w = pos(mt);
int k = code(mt);
char new_c = 'A' + k;
char old_c = mat[h][w];
del(h, w);
add(h, w, new_c);
LL new_score = score();
LL dif = new_score - now_score;
if(dif < 0){
double change_poss = exp(dif / temp);
if(change_poss < R(mt)){
del(h, w);
add(h, w, old_c);
}
}
}
vector<string> ans(N);
rep(i,N){
rep(j,N) ans[i].push_back(mat[i][j]);
}
rep(i,N) cout << ans[i] << endl;
//LL fin = score();
//cout << fin << endl;
return 0;
}
| //GIVE ME AC!!!!!!!!!!!!!!!!!
//#pragma GCC target("avx")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define floatset() fixed<<setprecision(15)
#define all(n) n.begin(),n.end()
#define rall(n) n.rbegin(),n.rend()
#define rep(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define pb push_back
#define eb emplace_back
#define INT(...) int __VA_ARGS__;scan(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;scan(__VA_ARGS__)
#define STR(...) string __VA_ARGS__;scan(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__;scan(__VA_ARGS__)
#define DBL(...) double __VA_ARGS__;scan(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__;scan(__VA_ARGS__)
using namespace std;
using vl=vector<ll>;
using vi=vector<int>;
using vs=vector<string>;
using vc=vector<char>;
using vvl=vector<vl>;
using P=pair<ll,ll>;
using vvc=vector<vc>;
using vd=vector<double>;
using vp=vector<P>;
using vb=vector<bool>;
using P=pair<ll,ll>;
const int dx[8]={1,0,-1,0,1,-1,-1,1};
const int dy[8]={0,1,0,-1,1,1,-1,-1};
const ll inf =1e18;
const ll MOD=1000000007;
const ll mod=998244353;
const double pi=acos(-1);
template<typename T1,typename T2 >
ostream &operator<<(ostream&os,const pair<T1,T2>&p) {
os<<p.first<<" "<<p.second;
return os;
}
template<typename T1,typename T2>
istream &operator>>(istream&is,pair<T1,T2>&p) {
is>>p.first>>p.second;
return is;
}
template<typename T>
ostream &operator<<(ostream&os,const vector<T>&v) {
for(int i=0;i<(int)v.size();i++) {
os<<v[i]<<(i+1!=v.size()?" ":"");
}
return os;
}
template<typename T>
istream &operator>>(istream&is,vector<T>&v) {
for(T &in:v)is>>in;
return is;
}
void scan(){}
template<class Head,class... Tail>
void scan(Head&head,Tail&... tail) {
cin>>head;
scan(tail...);
}
template<class T>
void print(const T &t) { cout << t << '\n'; }
template<class Head, class... Tail>
void print(const Head &head, const Tail &... tail) {
cout << head << ' ';
print(tail...);
}
template<class... T>
void fin(const T &... a) {
print(a...);
exit(0);
}
template<typename T1,typename T2>
inline bool chmax(T1&a,T2 b){return a<b&&(a=b,true);}
template<typename T1,typename T2>
inline bool chmin(T1&a,T2 b){return a>b&&(a=b,true);}
random_device seed_gen;
mt19937 engine(seed_gen());
int main(){
LL(n,m);
vs a(m);
cin>>a;
rep(i,0,100){
shuffle(all(a),engine);
}
ll now=0;
rep(i,0,n){
ll cnt=0;
string ans="";
while(cnt+(ll)(a[now].size())<=n){
cnt+=a[now].size();
ans+=a[now];
now++;
}
rep(i,0,n-cnt){
ll x=engine();
x%=8;
cout<<(char)('A'+x);
}
cout<<ans<<endl;
}
} |
#include <bits/stdc++.h>
#define ff first
#define ss second
#define endl "\n"
#define all(x) (x).begin(), (x).end()
using namespace std;
const int INF = (int) 1e9;
const int mod = (int) 1e9+7;
const int MAXN = (int) 3e5+5;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
vector<int> bit[30];
int n,q;
int arr[MAXN];
void upd(int pos,int val,int b){
for(;pos<n;pos+=pos&-pos){
bit[b][pos]+=val;
}
}
int sum(int pos,int b){
int ret = 0;
for(;pos>0;pos-=pos&-pos){
ret += bit[b][pos];
}
return ret;
}
void solve(){
cin >> n >> q;
n++;
for(int i=0;i<30;i++){
bit[i].assign(n,0);
}
for(int i=1;i<n;i++){
cin >> arr[i];
for(int b=0;b<30;b++){
if((1<<b)&arr[i]){
//cout << arr[i] << " "<< b << " ";
upd(i,1,b);
}
}
//cout << endl;
}
while(q--){
int t,x,y;
cin >> t >> x >> y;
if(t == 1){
int val = arr[x] ^y;
for(int i=0;i<30;i++){
if((1<<i) & val && !((1<<i) & arr[x])){
upd(x,-1,i);
}
if(!((1<<i) & val) && (1<<i) & arr[x]){
upd(x,1,i);
}
}
arr[x] = val;
}
if(t == 2){
int ans = 0;
for(int i=0;i<30;i++){
if((sum(y,i)-sum(x-1,i))%2){
ans+=(1<<i);
}
}
cout << ans << endl;
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
#ifdef Local
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t=1;
//cin>>t;
while(t--){
solve();
}
#ifdef Local
cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
#endif
} | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll inf=1000000000000000000,mod=1000000007;
ll bit[300005];
ll update(ll idx,ll val,ll n=300005){
idx++;
while(idx<n){
bit[idx]^=val;
idx+=idx&-idx;
}
return 0;
}
ll getsum(ll idx){
ll sum=0;
if(idx<=0)
return 0;
while(idx>0){
sum^=bit[idx];
idx-=idx&-idx;
}
return sum;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
ll n,q;cin>>n>>q;
ll arr[n];
for(ll i=0;i<n;i++)cin>>arr[i];
for(ll i=1;i<n;i++)arr[i]^=arr[i-1];
ll a,b,rig,lef;
while(q--){
cin>>a;
if(a==1){
cin>>a>>b;
update(a-1,b);
}
else
{
cin>>a>>b;
rig=arr[b-1]^getsum(b);
if(a-2>=0)
lef=arr[a-2]^getsum(a-1);
else
lef=0;
cout<<(rig^lef)<<endl;
}
}
}
|
#include <iostream>
#include <vector>
#include <string>
#include <array>
#include <functional>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <climits>
#include <queue>
#include <bitset>
#include <math.h>
using namespace std;
#define MOD 1000000007LL
#define LL long long
int n, L;
vector<int> convert(vector<int> N) {
vector<int> R;
int lx = 0;
for (const auto &x : N) {
R.push_back(x - lx - 1);
lx = x;
}
R.push_back(L - lx);
return R;
}
LL solve() {
cin >> n >> L;
vector<int> A(n);
vector<int> B(n);
for (int i = 0 ; i < n; i++) cin >> A[i];
for (int i = 0 ; i < n; i++) cin >> B[i];
A = convert(A);
B = convert(B);
int j = 0;
LL sol = 0;
for (int i = 0 ; i < B.size(); i++) {
int sa = 0;
int m1 = i;
int m2 = i;
while (sa < B[i] && j < A.size()) {
sa += A[j];
if (A[j] > 0) {
m1 = min(m1, j);
m2 = max(m2, j);
}
j++;
}
sol += m2 - m1;
if (B[i] != sa) return -1;
}
return sol;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int test = 1; //cin >> test;
while(test--) {
cout << solve() << endl;
}
return 0;
} | #include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
#include <list>
#include <cassert>
#include <climits>
#include <bitset>
#include <chrono>
#include <random>
using namespace std;
#define PB push_back
#define MP make_pair
#define SZ(v) ((int)(v).size())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)
#define REPE(i,n) FORE(i,0,n)
#define FORSZ(i,a,v) FOR(i,a,SZ(v))
#define REPSZ(i,v) REP(i,SZ(v))
std::mt19937 rnd((int)std::chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 100000 + 2;
int n, len;
int a[MAXN];
int b[MAXN];
map<int, pair<int, int>> awho;
int target[MAXN];
ll solve() {
//printf("a':"); REP(i, n) printf(" %d", a[i] - i); puts("");
//printf("b':"); REP(i, n) printf(" %d", b[i] - i); puts("");
REP(i, n) {
int x = a[i] - i;
if (!awho.count(x)) { awho[x] = MP(i, i); continue; }
assert(awho[x].second == i - 1);
++awho[x].second;
}
REP(i, n) {
if (!awho.count(b[i] - i)) return -1;
pair<int, int> cur = awho[b[i] - i];
if (i < cur.first) target[i] = cur.first; else if (i > cur.second) target[i] = cur.second; else target[i] = i;
}
ll ret = 0;
{
int at = 0;
while (at < n) {
if (target[at] <= at) { ++at; continue; }
ret += target[at] - at;
int to = at; while (to < n && target[to] == target[at]) ++to;
at = to;
}
}
{
int at = n - 1;
while (at >= 0) {
if (target[at] >= at) { --at; continue; }
ret += at - target[at];
int to = at; while (to >= 0 && target[to] == target[at]) --to;
at = to;
}
}
return ret;
}
void run() {
scanf("%d%d", &n, &len);
n += 2;
a[0] = b[0] = 0, a[n - 1] = b[n - 1] = len + 1;
FOR(i, 1, n - 1) scanf("%d", &a[i]);
FOR(i, 1, n - 1) scanf("%d", &b[i]);
printf("%lld\n", solve());
}
int main() {
run();
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.