code_file1
stringlengths 87
4k
| code_file2
stringlengths 85
4k
|
---|---|
#include <bits/stdc++.h>
using namespace std;
unsigned long long kaijou(unsigned long long k){
unsigned long long sum = 1;
for (unsigned long long i = 1; i <= k; ++i)
{
sum *= i;
}
return sum;
}
unsigned long long bekijou(unsigned long long p, unsigned long long q){
unsigned long long sum = 1;
for (unsigned long long i = 1; i <= q; ++i)
{
sum = sum * p;
}
return sum;
}
int main() {
unsigned long long x;
cin >> x;
vector<unsigned long long> y(x);
for (unsigned long long i = 0; i < x; i++) {
cin >> y.at(i);
}
vector<unsigned long long> s(x+1, 0);
for (unsigned long long i = 0; i < x; ++i){s[i+1] = s[i] + y[i];}
unsigned long long k = 0;
unsigned long long n = 0;
unsigned long long m = 0;
unsigned long long p = 0;
for (unsigned long long i = 1; i <= x; i++) {
k=max(k, y.at(i - 1));
n = n + s.at(i - 1);
cout << k * i + n + s.at(i) << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;
#define rep(i,n) for(int i=0;i<int(n);i++)
#define rep1(i,n) for(int i=1;i<=int(n);i++)
#define per(i,n) for(int i=int(n)-1;i>=0;i--)
#define per1(i,n) for(int i=int(n);i>0;i--)
#define all(c) c.begin(),c.end()
#define si(x) int(x.size())
#define pb emplace_back
#define fs first
#define sc second
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
template<class T,class U> void chmax(T& x, U y){if(x<y) x=y;}
template<class T,class U> void chmin(T& x, U y){if(y<x) x=y;}
template<class T> void mkuni(V<T>& v){sort(all(v));v.erase(unique(all(v)),v.end());}
template<class T> int lwb(const V<T>& v, const T& a){return lower_bound(all(v),a) - v.begin();}
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
o<<"{";
for(const T& v:vc) o<<v<<",";
o<<"}";
return o;
}
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }
#ifdef LOCAL
#define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
void dmpr(ostream& os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
os<<t<<" ~ ";
dmpr(os,args...);
}
#define shows(...) cerr << "LINE" << __LINE__ << " : ";dmpr(cerr,##__VA_ARGS__)
#define dump(x) cerr << "LINE" << __LINE__ << " : " << #x << " = {"; \
for(auto v: x) cerr << v << ","; cerr << "}" << endl;
#else
#define show(x) void(0)
#define dump(x) void(0)
#define shows(...) void(0)
#endif
int main(){
cin.tie(0);
ios::sync_with_stdio(false); //DON'T USE scanf/printf/puts !!
cout << fixed << setprecision(20);
int N; cin >> N;
V<ll> a(N); rep(i,N) cin >> a[i];
ll ans = 0, s = 0, x = 0;
rep(i,N){
chmax(x,a[i]);
s += a[i];
ans += s;
cout << ans+x*(i+1) << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a;
for(int i = 0; i < n; i++) {
int x;
cin >> x;
a.push_back(x);
}
sort(a.begin(), a.end());
bool sequence = false;
for(int i = 0; i < n-1; i++) {
if(a.at(i+1) - a.at(i) == 1) {
sequence = true;
} else {
sequence = false;
break;
}
}
if(a.size() < 2) sequence = true;
if(sequence) {
cout << "Yes";
} else {
cout << "No";
}
return 0;
} | #include <bits/stdc++.h>
//#include "debugger.h"
#define int long long
#define pb push_back
#define pp pop_back
#define f first
#define s second
#define all(x) x.begin(),x.end()
using namespace std;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin >> n;
int ans = 0;
for (int i = 1; i <= n; i++) {
string s;
cin >> s;
if (s == "OR") {
ans += (1ll << i);
}
}
cout << ans + 1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
int main() {
int N;
cin >> N;
cout << (N + (100 - 1)) / 100 << endl;
return 0;
} | #include <iostream>
#include<vector>
#include<algorithm>
#include<utility>
#include<math.h>
#include<iomanip>
#include<string>
#include <cassert>
#include <complex>
#include<math.h>
#include<bits/stdc++.h>
#define rep(s,i,n) for(int i = s;i < n;i++)
using namespace std;
typedef long long ll; //10e18ใใใ
typedef long double ld;
typedef __int128_t lll;
typedef pair<ll, ll> pll;
const int dx[] = { 0,1,0,-1 };
const int dy[] = { 1,0,-1,0 };
const ll LongINF = 1e13 + 7;
const int INF = 1e9 + 7;
const ll LL_MAX = 9223372036854775807;
const ll LL_MIN = -9223372036854775807;
int dsp[3] = {-1,0,1};
int keta(ll i){
int keta = 0;
do{
keta++;
}while(i /= 10);
return keta;
}
//็ด ๅ ๆฐๅ่งฃใO(โn)
vector<pair<ll,ll>> pfactor(ll n){
vector<pair<ll,ll>> factor;
ll ex; //ๆๆฐ
for(ll a = 2; a * a <= n; a++){
if(n % a != 0)continue;
ll ex = 0;
while(n % a == 0){
n /= a;
ex++;
}
factor.push_back({a,ex});
}
if(n != 1) factor.push_back({n,1});
return factor;
}
int main(void){
ll n;
cin >> n;
set<ll> s;
for(ll a = 2; a*a <= n;a++){
ll x = a * a;
while(x <= n){
s.insert(x);
x *= a;
}
}
cout << n-s.size();
return 0;
} |
#ifdef Rahul
#include "RAHUL.h"
#else
#include <bits/stdc++.h>
using namespace std;
#define error(...) 42;
#endif
#define SZ(v) int((v).size())
#define ALL(vec) begin(vec), end(vec)
typedef long long i64;
template<typename T> inline bool uax(T &x, T y) {return (y > x) ? x = y, true : false;}
template<typename T> inline bool uin(T &x, T y) {return (y < x) ? x = y, true : false;}
template<typename T> void kek(T ans) {cout << ans << endl; exit(0);}
#define Luv(...) [&] (auto &&u, auto &&v) { return __VA_ARGS__; }
const i64 INF = (i64) 1e18 + 42;
const int MOD = (int) 1e9 + 7;
inline int add(int a, int b, int mod = MOD) {
a += b; return a >= mod ? a - mod : a;
}
inline int sub(int a, int b, int mod = MOD) {
a -= b; return a < 0 ? a + mod : a;
}
inline int mul(int a, int b, int mod = MOD) {
return int((long long) a * b % mod);
}
inline int mpow(int base, long long ex, int mod = MOD) {
int res = 1;
for (; ex > 0; ex >>= 1) {
if (ex & 1) res = mul(res, base, mod);
base = mul(base, base, mod);
}
return res;
}
inline int inv(int a, int mod = MOD) {
return mpow(a, mod - 2, mod);
}
inline int mdiv(int a, int b, int mod = MOD) {
return mul(a, mpow(b, mod - 2, mod));
}
inline void adds(int &a, int b, int mod = MOD) {
a += b; if (a >= mod) a -= mod;
}
inline void subs(int &a, int b, int mod = MOD) {
a -= b; if (a < 0) a += mod;
}
inline void muls(int &a, int b, int mod = MOD) {
a = int((long long) a * b % mod);
}
inline void mdivs(int &a, int b, int mod = MOD) {
a = mdiv(a, b, mod);
}
char a[2010][2010];
int dp[4][2010][2010];
int main() {
cin.tie(nullptr) -> sync_with_stdio(false);
int k = 0;
int n, m; cin >> n >> m;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cin >> a[i][j];
if (a[i][j] == '.') {
dp[0][i][j] = 1 + dp[0][i - 1][j];
dp[1][i][j] = 1 + dp[1][i][j - 1];
++k;
}
}
}
int ans = 0;
for (int i = n; i > 0; --i) {
for (int j = m; j > 0; --j) {
if (a[i][j] == '.') {
dp[2][i][j] = 1 + dp[2][i + 1][j];
dp[3][i][j] = 1 + dp[3][i][j + 1];
int tot = k - (dp[0][i][j] + dp[1][i][j] + dp[2][i][j] + dp[3][i][j] - 3);
adds(ans, mpow(2, tot));
}
}
}
ans = sub(mul(k, mpow(2, k)), ans);
kek(ans);
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef double dd;
#define tst int tttt;cin>>tttt;for(int TTTT=1;TTTT<=tttt;TTTT++)
#define nl cout<<"\n";
#define forn(a,b) for (int ii=a;ii<b;ii++)
const ll MOD1=1e9+7;
const ll MOD2=998244353;
#define pii pair<int,int>
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define all(a) a.begin(),a.end()
#define vi vector<int>
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define hhh cout<<"here"<<endl;
ll mod=MOD1;
ll po(ll x,ll y) {if(y<0)return 0;y%=mod;ll res=1;while(y>0){if(y&1)res=(res*x)%mod;x=(x*x)%mod;y>>=1;}return res;}
ll gcd(ll a, ll b){if(a<b) swap(a,b);if(b==0) return a;return gcd(a%b,b);}
void dfs(int u,vi&a,vector<vi>&adj,vi&vis,vi&dp){
vis[u]=1;
if(adj[u].size()==0){
dp[u]=-1e9;return;
}
for(auto v:adj[u]){
if(!vis[v])dfs(v,a,adj,vis,dp);
dp[u]=max(dp[u],max(a[v],dp[v]));
}
}
void solve(){
int n,m;cin>>n>>m;
vi a(n+1);forn(0,n)cin>>a[ii+1];
vector<vi>adj(n+1);
forn(0,m){
int u,v;cin>>u>>v;
adj[u].pb(v);
}vi dp(n+1);
vi vis(n+1);
for(int i=1;i<=n;i++){
if(!vis[i]){
dfs(i,a,adj,vis,dp);
vis[i]=1;
}
}int mx=-1e9;
forn(1,n+1){
mx=max(mx,dp[ii]-a[ii]);
}cout<<mx;
}
int main(){
// fastio
// ++*(int*)0;// crash
// freopen("in.txt","r",stdin);freopen("outt.txt","w",stdout);
// tst
{
solve();nl;
}
}
/*
4
13 12 7 11 16 15 2 14
*/
|
#include "bits/stdc++.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr<<"hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x,y) (lower_bound(all(x),y)-x.begin())
#define ubd(x,y) (upper_bound(all(x),y)-x.begin())
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline long long rand(long long x, long long y) { return rng() % (y+1-x) + x; } //inclusive
string inline to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){ return a==0?llabs(b):gcd(b%a,a); }
using ll=long long;
using ld=long double;
#define FOR(i,s,e) for(ll i=s;i<=ll(e);++i)
#define DEC(i,s,e) for(ll i=s;i>=ll(e);--i)
using pi=pair<ll,ll>; using spi=pair<ll,pi>; using dpi=pair<pi,pi>;
long long LLINF = 1e18;
int INF = 1e9+1e6;
#define MAXN (5006)
int n, m, mod=998244353, ans, gap[MAXN], memo[MAXN];
ll qexp(ll x,ll e) {
ll sum=1;
for(;e;e>>=1,x*=x,x%=mod) if(e&1) sum*=x, sum%=mod;
return sum;
}
int main() {
FAST
cin>>n>>m;
ans = qexp(m, n) * n % mod;
// cerr<<ans<<'\n';
FOR(i,0,m) memo[i]=1;
FOR(i,0,n) {
FOR(j,0,m-1) {
gap[i] += memo[j];
if(gap[i] >= mod) gap[i] -= mod;
memo[j] = ll(memo[j]) * j % mod;
}
}
memo[0] = 1; FOR(i,1,n) memo[i] = ll(memo[i-1]) * m % mod;
FOR(i,1,n) {
DEC(j,i-1,1) {
// cerr<<gap[i-j]<<'\n';
// cerr<<i<<' '<<j<<": "<<qexp(m, j-1 + n-i) * gap[i-j-1] % mod<<'\n';
ans -= ll(memo[j-1 + n-i]) * gap[i-j-1] % mod;
if(ans < 0) ans += mod;
}
}
cout<<ans<<'\n';
}
|
/**
* author: akifpathan
* created: Wednesday 23.06.2021 01:28:14 PM
**/
#ifdef akifpathan
#include "debug.h"
#else
#include<bits/stdc++.h>
using namespace std;
#define debug(x...)
#endif
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
//mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const int mx=1e6+5;
ll f(int n)
{
return 1LL*n*n;
}
ll dp[mx];
int fre[mx];
int range(int l,int r,int i)
{
return r/i-(l-1)/i;
}
void solve()
{
int l,r;
cin>>l>>r;
for(int i=l;i<=r;i++)
{
int x=i;
for(int j=1;j*j<=x;j++)
{
if(x%j==0)
{
fre[j]++;
fre[x/j]++;
if(j*j==x) fre[j]--;
}
}
}
for(int i=r;i>=1;i--)
{
dp[i]=f(fre[i]);
for(int j=2*i;j<=r;j+=i) dp[i]-=dp[j];
}
for(int i=l;i<=r;i++)
{
int temp=range(l,r,i);
dp[i]--;
temp--;
dp[i]-=(2*temp);
}
cout<<accumulate(dp+2,dp+r+1,0LL);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int testcase=1;
//cin>>testcase;
for(int i=1;i<=testcase;i++)
{
//cout<<"Case "<<i<<": ";
solve();
}
#ifdef akifpathan
cerr<<"\nTime elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms\n";
#endif
return 0;
}
|
#include <iostream>
#include <vector>
using namespace std;
using LL = long long;
const int kMaxN = 2e5 + 1;
const int kMaxM = 101;
const int kM = 1e9 + 7;
LL n, ans, u, v, w;
LL c[kMaxN], d[kMaxM];
bool vis[kMaxN];
vector<LL> a[kMaxN], b[kMaxN];
void Dfs(LL x, LL y) {
if (vis[x]) {
return;
}
vis[x] = true;
c[x] = y;
for (LL i = 0; i < a[x].size(); i++) {
Dfs(a[x][i], y ^ b[x][i]);
}
}
int main() {
cin >> n;
for (LL i = 1; i < n; i++) {
cin >> u >> v >> w;
a[u].push_back(v), b[u].push_back(w);
a[v].push_back(u), b[v].push_back(w);
}
Dfs(1, 0);
for (LL i = 1; i <= n; i++) {
for (LL j = 0; j <= 61; j++) {
if (c[i] & (1ll << j)) {
d[j]++;
}
}
}
for (LL i = 0; i <= 61; i++) {
ans = (ans + 1ll * (n - d[i]) * d[i] % kM * ((1ll << i) % kM) % kM) % kM;
}
cout << ans;
return 0;
} | #include "bits/stdc++.h"
using namespace std;
using ll = int64_t;
#define eb emplace_back
#define F first
#define S second
#define ice(i, a, b) for (int (i) = (a); (i) < (b); ++(i))
const ll MOD = 1e9 + 7;
vector<vector<pair<int, ll>>> adj(200001);
vector<ll> val(200001, 0);
void dfs(int n, int m = -1) {
for (auto b : adj[n]) {
if (b.F != m) {
val[b.F] = val[n] ^ b.S;
dfs(b.F, n);
}
}
}
void solve() {
int n; cin >> n;
ice (i, 0, n - 1) {
int u, v; ll w; cin >> u >> v >> w; --u; --v;
adj[u].eb(v, w);
adj[v].eb(u, w);
}
dfs(0);
ll ans = 0;
ice (i, 0, 60) {
ll zero = 0, one = 0;
ice (j, 0, n) {
val[j] & (1LL << i) ? one++ : zero++;
}
ll cnt = (((zero * one) % MOD) * ((1LL << i) % MOD)) % MOD;
ans = (ans + cnt) % MOD;
}
cout << ans;
}
int main() {
solve();
} |
#include<bits/stdc++.h>
#define int long long
#define ri register signed
#define rd(x) x=read()
using namespace std;
const int N=2e5+5;
const int M=21;
const int maxn=5e6+5;
const int mod=998244353;
const int inf=0x3f3f3f3f3f3f3f3f;
inline int read(){int x=0,f=1;char ch=getchar();while(ch>'9'||ch<'0'){if (ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
inline int ksm(int x,int y=mod-2,int z=mod){if (y==0) return 1;int ret=1;while (y){if (y&1) ret=(ret*x)%z;x=(x*x)%z;y>>=1;}return ret;}
int n,m,ans;
signed main()
{
rd(n);rd(m);
ans=n*ksm(m,n)%mod;
for (int l=0;l<=n-2;l++) for (int v=1;v<=m;v++)
{
ans-=ksm(m,n-l-2)*ksm(m-v,l)%mod*(n-1-l);
//printf("l=%lld,v=%lld,-=%lld\n",l,v,ksm(m,n-l-2)*ksm(m-v,l));
ans%=mod;
}
cout<<(ans+mod)%mod<<endl;
} | #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 A[100001];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N;
cin >> N;
rep1(i, N) cin >> A[i];
sort(A + 1, A + N + 1);
int kotae = 1;
rep(i, N) {
kotae = kotae * ll(A[i + 1] - A[i] + 1) % mod;
}
co(kotae);
Would you please return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define ar array
#define ll long long
#define For(i,k,n) for(int i=k;i<n;i++)
#define Forll(i,k,n) for(long long int i=k;i<n;i++)
#define Forull(i,k,n) for(long long int i=k;i<n;i++)
const ll MOD = 1e9 + 7;
const ll INF = 1e9;
void solve(){
int a,b,c;
cin>>a>>b>>c;
if(a*a+b*b<c*c)
cout<<"Yes"<<"\n";
else
cout<<"No"<<"\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
ll int tc=1;
//cin >> tc;
for (ll int t = 1; t <= tc; t++) {
//cout << "Case #" << t << ": ";
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
int main(){
int v,t,s,d,p;
cin>>v>>t>>s>>d;
if (t*v<=d && d<=s*v)
{
cout<<"No";
}
else
{
cout<<"Yes";
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define int long long
//#define ll long long
#include <cmath>
#define ff first
#define ss second
#define pb push_back
#define vi vector<int>
#define vp vector<pair<int,int>>
#define vii vector<vector<int>>
#define tp tuple<double,ll,ll>
#define sd(a) scanf("%lld",&a)
#define pr(a) printf("%d ",a)
//#define mod 1000000007
const double EPS = 1e-9;
#define f(o,g,h) for(ll o=g;o<h;o++)
#define getunique(v) {sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end());}
const int M = 1e9+7;
int mod(int a){return (a%M + M)%M;}
int add(int a,int b){return mod(mod(a)+mod(b));}
namespace number_theory {
int gcd(int x, int y) {
if (x == 0) return y;
if (y == 0) return x;
return gcd(y, x % y);
}
bool isprime(int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i += 6)
if (n % i == 0 || n % (i+2) == 0)
return false;
return true;
}
bool prime[15000105];
void sieve(int n) {
for (int i = 0; i <= n; i++) prime[i] = 1;
for (int p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
prime[1] = prime[0] = 0;
}
vector<int> primelist;
bool __primes_generated__ = 0;
void genprimes(int n) {
__primes_generated__ = 1;
sieve(n + 1);
for (int i = 2; i <= n; i++) if (prime[i]) primelist.push_back(i);
}
vector<int> factors(int n) {
if (!__primes_generated__) {
cerr << "Caint genprimes you dope" << endl;
exit(1);
}
vector<int> facs;
for (int i = 0; primelist[i] * primelist[i] <= n && i < primelist.size(); i++) {
if (n % primelist[i] == 0) {
while (n % primelist[i] == 0) {
n /= primelist[i];
facs.push_back(primelist[i]);
}
}
}
if (n > 1) {
facs.push_back(n);
}
sort(facs.begin(), facs.end());
return facs;
}
vector<int> getdivs(int n) {
vector<int> divs;
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
divs.push_back(i);
divs.push_back(n / i);
}
}
getunique(divs);
return divs;
}
}
using namespace number_theory;
void solve(){
double a,b; cin>>a>>b;
cout<<(a*b/100);
}
int32_t main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
// genprimes(1e7);
//init();
int t; //cin>>t;
t=1;
while(t--)
solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll,ll>
#define FOR(I,A,B) for(ll I = ll(A); I < ll(B); ++I)
#define FORR(I,A,B) for(ll I = ll((B)-1); I >= ll(A); --I)
#define TO(x,t,f) ((x)?(t):(f))
#define SORT(x) (sort(x.begin(),x.end())) // 0 2 2 3 4 5 8 9
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) //xi>=v x is sorted
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) //xi>v x is sorted
#define NUM(x,v) (POSU(x,v)-POSL(x,v)) //x is sorted
#define REV(x) (reverse(x.begin(),x.end())) //reverse
ll gcd_(ll a,ll b){if(a%b==0)return b;return gcd_(b,a%b);}
ll lcm_(ll a,ll b){ll c=gcd_(a,b);return ((a/c)*b);}
#define NEXTP(x) next_permutation(x.begin(),x.end())
const ll INF=ll(1e16)+ll(7);
const ll MOD=1000000007LL;
#define out(a) cout<<fixed<<setprecision((a))
//tie(a,b,c) = make_tuple(10,9,87);
#define pop_(a) __builtin_popcount((a))
ll keta(ll a){ll r=0;while(a){a/=10;r++;}return r;}
#define num_l(a,v) POSL(a,v) //vๆชๆบใฎ่ฆ็ด ๆฐ
#define num_eql(a,v) POSU(a,v) //vไปฅไธใฎ่ฆ็ด ๆฐ
#define num_h(a,v) (a.size() - POSU(a,v)) //vใใๅคงใใ่ฆ็ด ๆฐ
#define num_eqh(a,v) (a.size() - POSL(a,v)) //vไปฅไธใฎ่ฆ็ด ๆฐ
uniform_real_distribution<double> rnd(0.00,1.00);
int main(){
ll N,X;
string s;
cin >> N >> X;
cin >> s;
FOR(i,0,N){
if(s[i]=='o')X++;
if(s[i]=='x')X--;
if(X<0)X=0;
}
cout << X << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
typedef long double ld;
typedef pair<int,int> pii;
#define x first
#define y second
const int N=2e5+100;
int a[N];
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
sort(a+1,a+n+1);
int pref=0;
int ans=0;
for(int i=1;i<=n;i++)
ans+=a[i]*(i-1)-pref,
pref+=a[i];
cout<<ans;
}
| #include <bits/stdc++.h>
#define ll long long
using namespace std;
class BIT
{
public:
ll n;
vector<ll> tree;
BIT(ll nn)
{
n = nn;
tree = vector<ll>(n + 1, 0);
}
ll sum(ll k)
{
k++;
ll s = 0;
while (k >= 1)
{
s += tree[k];
k -= k&-k;
}
return s;
}
void add(ll k, ll x)
{
k++;
while (k <= n)
{
tree[k] += x;
k += k&-k;
}
}
};
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll n;
cin >> n;
vector<ll> v(n);
for(ll i = 0; i < n; i++)
{
cin >> v[i];
}
ll nidx = 0;
auto vv = v;
sort(vv.begin(), vv.end());
unordered_map<ll, ll> numToIdx, idxToNum;
for(ll i = 0; i < n; i++)
{
if(numToIdx.count(vv[i]))
continue;
numToIdx[vv[i]] = nidx++;
}
for(auto& [num, idx] : numToIdx)
{
idxToNum[idx] = num;
}
BIT bit(nidx), bitCnt(nidx);
ll ret = 0;
bit.add(numToIdx[v[n - 1]], v[n - 1]);
bitCnt.add(numToIdx[v[n - 1]], 1);
for(ll i = n - 2; i > -1; i--)
{
ll idx = numToIdx[v[i]];
ll left = bit.sum(idx);
ll leftCnt = bitCnt.sum(idx);
ll right = bit.sum(nidx - 1) - left;
ll rightCnt = bitCnt.sum(nidx - 1) - leftCnt;
ret += leftCnt * v[i] - left + right - v[i] * rightCnt;
bit.add(idx, v[i]);
bitCnt.add(idx, 1);
}
cout << ret << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
#define mp make_pair
#define fr first
#define sc second
int lg(ll n) {
assert(n > 0);
int ans = -1;
while (n) {
ans++;
n >>= 1;
}
return ans;
}
int score(string s) {
vector<int> cnt(10);
iota(cnt.begin(), cnt.end(), 0);
for (char ch : s) cnt[ch - '0'] *= 10;
return accumulate(cnt.begin(), cnt.end(), 0);
}
void solve() {
int K;
cin >> K;
string S, T;
cin >> S >> T;
S.pop_back(), T.pop_back();
vector<int> cnt(10, K);
for (char ch : S) cnt[ch - '0']--;
for (char ch : T) cnt[ch - '0']--;
ll ans = 0;
for (int i = 1; i < 10; i++) {
for (int j = 1; j < 10; j++) {
if (score(S + (char)(i + '0')) > score(T + (char)(j + '0'))) {
ans += (i == j ? 1LL * cnt[i] * (cnt[i] - 1)
: 1LL * cnt[i] * cnt[j]);
}
}
}
const int remain = 9 * K - 8;
cout << fixed << setprecision(12) << (double)ans / remain / (remain - 1)
<< '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t = 1;
// cin >> t;
while (t--) solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
long long rnd(long double n)
{
long double f, c;
f = n - (long double)floor(n);
c = (long double)ceil(n) - n;
if (c < f)
{
return ceil(n);
}
else
{
return floor(n);
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long k, n, m, i, s = 0, d;
long double r;
cin >> k >> n >> m;
r = (long double)m / (long double)n;
vector<long long> a(k);
vector<long double> b1(k);
vector<long long> b2(k);
vector<long long> c(k);
vector<pair<long double, long long>> di(k);
for (i = 0; i < k; i++)
{
cin >> a[i];
}
for (i = 0; i < k; i++)
{
b1[i] = (long double)a[i]*r;
}
for (i = 0; i < k; i++)
{
b2[i] = rnd(b1[i]);
}
for (i = 0; i < k; i++)
{
if (b1[i] > (long double)b2[i])
{
c[i] = -1;
}
else if (b1[i] < (long double)b2[i])
{
c[i] = 1;
}
else
{
c[i] = 0;
}
}
for (i = 0; i < k; i++)
{
di[i] = make_pair((long double)b2[i] - b1[i], i);
}
for (i = 0; i < k; i++)
{
s += b2[i];
}
d = m - s;
std::sort(di.begin(), di.end(), [](const std::pair<float, int>& first, const std::pair<float, int>& second)
{
return first.first < second.first;
});
if (d > 0)
{
for (i = 0; i < d; i++)
{
b2[di[i].second]++;
}
}
else if (d < 0)
{
d = d*(-1);
for (i = 0; i < d; i++)
{
b2[di[k-1-i].second]--;
}
}
for (i = 0; i < k; i++)
{
cout << b2[i] << " ";
}
}
|
#include "bits/stdc++.h"
using namespace std;
#define ffor(n) for(int i = 0; i < n; i++)
#define fffor(n) for(int j = 0; j < n; j++)
#define uwu ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize("Ofast")
const int INF = 1e9 + 7;
const long long INF2 = 1e17;
int main(void) {
uwu
int n; cin >> n;
vector <int> v(n);
ffor(n) cin >> v[i];
for(int i = 0; i < n; i += 2) v[i] *= -1;
map <long long, int> count;
long long current = 0, ans = 0;
for(int i = 0; i < n; i++) {
current += v[i];
if (current == 0) ans++;
ans += count[current];
count[current] += 1;
}
cout << ans << '\n';
}
/*
C:\Users\kenne\OneDrive\Desktop\competitive_programming\main.cpp
*/
| #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define forn(a, e) for (i64 a = 0; a < (i64)(e); a++)
#define forr(a, s, e) for (i64 a = s; a < (i64)(e); a++)
#define fore(e, a) for (auto& e : a)
#ifdef LOCAL
#define logv(a) {cerr << #a << " = "; fore(e, a) {cerr << e << " ";} cerr << "\n";}
#define logvp(a) {cerr << #a << " = "; fore(e, a) {cerr << "(" << e.first << ", " << e.second << ") ";} cerr << "\n";}
#define logvv(a) {cerr << #a << " = \n"; fore(r, a) { fore(e, r) {cerr << e << " ";} cerr << "\n";} }
#define logvf(a, field) {cerr << #a"."#field << " = \n"; fore(e, a) { cerr << e.field << " ";} cerr << "\n"; }
#define logvff(a, f1, f2) {cerr << #a".{"#f1 << ", "#f2 << "} = \n"; fore(e, a) { cerr << "(" << e.f1 <<", " << e.f2 << ") ";} cerr << "\n"; }
#define logs(a) cerr << #a << " = " << (a) << "\n";
#define logss(a, b) cerr << #a << " = " << (a) << ", " << #b << " = " << (b) << "\n";
#define logp(a) cerr << #a << " = " << "(" << a.first << ", " << a.second << ")" << "\n";
#define cond(pred, stmt) if (pred) { stmt }
#else
#define logv(a)
#define logvp(a)
#define logvv(a)
#define logvf(a, field)
#define logvff(a, f1, f2)
#define logs(a)
#define logss(a, b)
#define logp(a)
#define cond(pred, stmt)
#endif
using iip = pair<int, int>;
using llp = pair<i64, i64>;
using ivec = vector<int>;
using llvec = vector<i64>;
using svec = vector<string>;
template<typename T> using vec = vector<T>;
template<typename T, typename Dim>
auto make_vec(T value, Dim dim) { return vector<T>(dim, value); }
template<typename T, typename Dim1, typename... Dim>
auto make_vec(T value, Dim1 dim1, Dim... dims) { return make_vec(make_vec(value, dims...), dim1); }
template<typename T>
bool uax(T& v, const T& newv) { if (v < newv) { v = newv; return true; } else return false; }
template<typename T>
bool uin(T& v, const T& newv) { if (v > newv) { v = newv; return true; } else return false; }
template<typename T>
istream& operator>>(istream& is, vector<T>& c) { for (auto& e : c) is >> e; return is; }
template<typename T, size_t N>
istream& operator>>(istream& is, array<T, N>& c) { for (auto& e : c) is >> e; return is; }
template<typename ...T>
istream& read(T&... args) { return (cin >> ... >> args); }
static mt19937 rande(123123);
template<typename T>
T rand_int(T from, T to) { uniform_int_distribution<T> distr(from, to); return distr(rande); }
// const i64 INF = 2e18;
const int INF = 2e9;
const i64 M = 998244353;
const int MXB = 20;
using bin = bitset<MXB + 1>;
const double EPS = 1e-8;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
rande.seed(chrono::steady_clock::now().time_since_epoch().count());
int n;
while (read(n)) {
llvec a(n);
read(a);
llvec sa[2] = {llvec(n + 1), llvec(n + 1)};
forn(i, n) {
int pi = i % 2;
int ni = pi ^ 1;
sa[pi][i + 1] = sa[pi][i] + a[i];
sa[ni][i + 1] = sa[ni][i];
}
map<i64, i64> cc;
i64 ans = 0;
forn(i, n + 1) {
ans += cc[sa[0][i] - sa[1][i]];
cc[sa[0][i] - sa[1][i]]++;
}
cout << ans << endl;
}
} |
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b,c,d;
cin>>a>>b>>c>>d;
int t=min(a,b);
int t1=min(c,d);
cout<<min(t,t1);
}
| #include <iostream>
#include <iomanip>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <array>
#include <vector>
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#define foi(i,a,b) for(int i=a;i<=b;++i)
#define iof(i,a,b) for(int i=a;i>=b;--i)
#define fol(i,a,b) for(int i=a;i<=b;++i)
#define lof(i,a,b) for(int i=a;i>=b;--i)
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
typedef unsigned int u32;
typedef unsigned short u16;
typedef pair<int,int> PII;
typedef pair<LL,int> PLI;
typedef pair<int,LL> PIL;
typedef pair<LL,LL> PLL;
const int Inf=(1<<30)-1;
const LL lInf=(1LL<<62)-1;
const int mInf=0x7f7f7f7f;
int main()
{
ios::sync_with_stdio(false);
int minn=Inf;
for(int a,i=1;i<=4;i++) cin>>a,minn=min(minn,a);
cout<<minn<<endl;
return 0;
} |
#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wunused-result"
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#endif
#include <algorithm>
#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;
typedef priority_queue<III, vector<III>, greater<>> Queue;
static const LL INF = 1LL << 60;
LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a; }
LL lcm(LL a, LL b) { return (a / gcd(a, b)) * b; }
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;
}
};
static modll fact[MAX_N + 1], inv[MAX_N + 1];
modll combination(LL n, LL r) {
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];
}
struct UnionFind {
std::vector<int> _parent;
std::vector<int> _size;
UnionFind(int size) : _parent(size, -1), _size(size, 1) { }
void unite(int a, int b) {
int ra = root(a), rb = root(b); if (ra == rb) { return; }
if (_size[ra] >= _size[rb]) { _parent[rb] = ra, _size[ra] += _size[rb]; } else { _parent[ra] = rb, _size[rb] += _size[ra]; }
}
int root(int a) {
int p = _parent[a];
if (p < 0) { return a; }
while (_parent[p] >= 0) { p = _parent[p]; }
return _parent[a] = p;
}
int size(int a) { return _size[root(a)]; }
};
void solve(long long N, long long K, std::vector<std::vector<long long>> &A) {
UnionFind h(N), v(N);
for (LL a = 0; a < N; ++a) {
for (LL b = 0; b < N; ++b) {
if (a == b) continue;
LL i;
for (i = 0; i < N; ++i) {
if (A[i][a] + A[i][b] > K) {
break;
}
}
if (i >= N) {
h.unite(a, b);
}
for (i = 0; i < N; ++i) {
if (A[a][i] + A[b][i] > K) {
break;
}
}
if (i >= N) {
v.unite(a, b);
}
}
}
combination(0, 0);
modll ans = 1;
for (LL a = 0; a < N; ++a) {
if (h.root(a) == a) {
ans *= fact[h.size(a)];
}
if (v.root(a) == a) {
ans *= fact[v.size(a)];
}
}
cout << ans << endl;
}
int main() {
long long N;
std::cin >> N;
long long K;
std::cin >> K;
std::vector<std::vector<long long>> a(N, std::vector<long long>(N));
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
std::cin >> a[i][j]; // a[i][j]--;
}
}
solve(N, K, a);
return 0;
}
| #include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <cstring>
#include <numeric>
#include <algorithm>
#include <functional>
#include <fstream>
#include <array>
#include <map>
#include <queue>
#include <time.h>
#include <limits.h>
#include <set>
#include <stack>
#include <random>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#define rep(i,s,n) for(int i = (s); (n) > i; i++)
#define REP(i,n) rep(i,0,n)
#define RANGE(x,a,b) (min(a,b) <= (x) && (x) <= max(a,b)) //hei
#define DUPLE(a,b,c,d) (RANGE(a,c,d) || RANGE(b,c,d) || RANGE(c,a,b) || RANGE(d,a,b))
#define INCLU(a,b,c,d) (RANGE(a,c,d) && (b,c,d))
#define PW(x) ((x)*(x))
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define bitcheck(a,b) ((a >> b) & 1)
#define bitset(a,b) ( a |= (1 << b))
#define bitunset(a,b) (a &= ~(1 << b))
#define MP(a,b) make_pair((a),(b))
#define Manh(a,b) (abs((a).first-(b).first) + abs((a).second - ((b).second))
#define pritnf printf
#define scnaf scanf
#define itn int
#define PI 3.141592653589
#define izryt bool
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template<typename A, size_t N, typename T>
void Fill(A(&array)[N], const T & val) {
std::fill((T*)array, (T*)(array + N), val);
}
//[a, b)
#define Getsum(ar, a,b) (ar[b] - ar[a])
#define INF 10000000000000000LL
#define chmax(a,b) a = max(a,b)
#define chmin(a,b) a = min(a,b)
struct Edge {
int from, to;
ll w;
bool operator<(const Edge& rhs) const {
return MP(w, MP(from, to)) < MP(rhs.w, MP(rhs.from, rhs.to));
}
};
typedef vector<vector<Edge>> Graph;
typedef unsigned long long ull;
#define MODU 998244353LL
struct UnionFind {
vector<int> data;
UnionFind(int size) : data(size, -1) { }
bool unionSet(int x, int y) { //๏ฝใฎๅ
ฅใฃใฆใ้ๅใจ yใฎๅ
ฅใฃใฆใ้ๅใไฝตๅ
x = root(x); y = root(y);
if (x != y) {
if (data[y] < data[x]) swap(x, y);
data[x] += data[y]; data[y] = x;
}
return x != y;
}
bool findSet(int x, int y) { //xใจyใๅใ้ๅใซๅ
ฅใฃใฆใใใใฉใใใๅคๅฎ
return root(x) == root(y);
}
int root(int x) {
return data[x] < 0 ? x : data[x] = root(data[x]);
}
bool isroot(int x) {
return data[x] < 0;
}
int size(int x) {
return -data[root(x)];
}
};
ll fuc(ll a){
ll ans = 1;
REP(i,a){
ans *= i+1;
ans %= MODU;
}
return ans;
}
ll check(vector<vector<int>>& m, int s){
int n = m.size();
UnionFind uf(n);
REP(i,n){
rep(j, i + 1, n){
bool f = true;
REP(k, n){
if(m[i][k] + m[j][k] > s) f = false;
}
if(f) uf.unionSet(i, j);
}
}
ll ret = 1;
REP(i, n){
if(uf.data[i] < 0){
ret *= fuc(-uf.data[i]);
ret %= MODU;
}
}
return ret;
}
int main(){
int n,k;
cin >> n >> k;
vector<vector<int>> m(n,vector<int> (n));
vector<vector<int>> r(n,vector<int> (n));
REP(i,n){
REP(j,n){
cin >> m[i][j];
r[j][i] = m[i][j];
}
}
ll a = check(m, k);
ll b = check(r, k);
cout << (a * b) % MODU << endl;
return 0;
} |
/*************************************************************************
> File Name: e.cpp
> Full Path: /Users/zhanghang/Downloads/e.cpp
> Author: Hang Zhang
> Created Time: ไธ 4/28 14:59:02 2021
************************************************************************/
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<stack>
#include<string>
#include<queue>
#include<set>
#include<map>
#include<cmath>
using namespace std;
typedef long long ll;
const int MAXN=105;
const ll mod=1e9+7;
const int inf=0x3f3f3f3f;
struct Matrix
{
ll ma[MAXN][MAXN];
int n;
Matrix(int n):n(n){ //nn is the dimension of matrix
memset(ma,0,sizeof ma);
}
};
Matrix mul(Matrix A,Matrix B,long long mod)
{
int n=A.n;
Matrix C(n);
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
for(int k=0;k<n;k++)
{
C.ma[i][j]=(C.ma[i][j]+A.ma[i][k]*B.ma[k][j])%mod;
}
}
}
return C;
}
Matrix pow_mod(Matrix A,long long k, long long mod)
{
int n=A.n;
Matrix B(n);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
B.ma[i][j]=0;
}
B.ma[i][i]=1;
}
while(k)
{
if(k&1) B=mul(B,A,mod);
A=mul(A,A,mod);
k>>=1;
}
return B;
}
long long quick_pow(long long a, long long b, long long mod) {
a %= mod;
long long res = 1;
while (b) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
int a[MAXN];
vector<int>G[MAXN];
int main(){
int n,m,k;
cin>>n>>m>>k;
for(int i=0;i<n;i++)cin>>a[i];
for(int i=1;i<=m;i++){
int x,y;cin>>x>>y;
x--;y--;
G[x].push_back(y);
G[y].push_back(x);
}
ll tmp=quick_pow(m*2LL,1LL*mod-2,mod);
Matrix t(n);
for(int i=0;i<n;i++){
int d=G[i].size();
t.ma[i][i]=((1LL-tmp*d%mod)%mod+mod)%mod;
for(auto v:G[i]){
t.ma[i][v]=tmp;
}
}
Matrix e=pow_mod(t,k,mod);
for(int i=0;i<n;i++){
ll ans=0;
for(int j=0;j<n;j++){
ans+=e.ma[i][j]*a[j];
ans%=mod;
}
cout<<ans<<endl;
}
return 0;
}
| #include<bits/stdc++.h>
#define int long long
#define MOD 1000000007
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL)
#define ll long long
#define f(a,b) for(int i=a; i<b; i++)
#define rep(i,a,b) for(int i=a;i< b;i++)
#define endl "\n"
#define all(a) a.begin(),a.end()
#define pii pair<int,int>
#define fr first
#define sc second
#define INF 1000000000
#define pb push_back
#define ppb pop_back()
#define sz(x) (int)((x).size())
#define vi vector<int>
#define vvi vector<vector<int>>
#define PI 3.14159265358979323846
using namespace std;
const int N=100+5;
int d[N];
const long long MOD2 = static_cast<long long>(MOD) * MOD;
struct Matrix
{
vector< vector<int> > mat;
int n_rows, n_cols;
Matrix() {}
Matrix(vector< vector<int> > values): mat(values), n_rows(values.size()),
n_cols(values[0].size()) {}
static Matrix identity_matrix(int n)
{
vector< vector<int> > values(n, vector<int>(n, 0));
for(int i = 0; i < n; i++)
values[i][i] = 1;
return values;
}
Matrix operator*(const Matrix &other) const
{
int n = n_rows, m = other.n_cols;
vector< vector<int> > result(n_rows, vector<int>(n_cols, 0));
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++) {
long long tmp = 0;
for(int k = 0; k < n_cols; k++) {
tmp += mat[i][k] * 1ll * other.mat[k][j];
while(tmp >= MOD2)
tmp -= MOD2;
}
result[i][j] = tmp % MOD;
}
return move(Matrix(move(result)));
}
inline bool is_square() const
{
return n_rows == n_cols;
}
};
// binary exponentiation, returns a^p
Matrix pw(Matrix a,int p){
Matrix result = Matrix::identity_matrix(a.n_cols);
while (p > 0) {
if (p & 1)
result = a * result;
a = a * a;
p >>= 1;
}
return result;
}
int binpow(int a,int b){
int res=1;
while(b){
if(b&1) res*=a , res%=MOD;
a*=a , a%=MOD;
b>>=1;
}
return res;
}
void test_case()
{
int n,m,k;
cin>>n>>m>>k;
vector<vector<int>> v(n,vector<int>(1));
vector<vector<int>> mat(n,vector<int>(n,0));
rep(i,0,n) cin>>v[i][0];
int oneby2m=binpow(2*m,MOD-2);
rep(i,0,m){
int x,y;
cin>>x>>y;
x--;y--;
d[x]++;d[y]++;
mat[x][y]=mat[y][x]=oneby2m;
}
rep(i,0,n){
mat[i][i]=(2*m-d[i]) * oneby2m %MOD;
}
Matrix ans=pw(mat,k)*v;
rep(i,0,n) cout<< ans.mat[i][0] << endl ;
}
signed main()
{
fast_io;
#ifdef NCR
init();
#endif
int t=1;
// cin>>t;
rep(i,0,t){
//cout<< "Case #" << i+1 << ": ";
test_case();
}
return 0;
}
//taskkill -im main.exe -f |
//https://atcoder.jp/contests/abc183/tasks/abc183_e
//E - Queen on Grid
#include <bits/stdc++.h>
using namespace std;
const int MAXH=2e3+2;
const int MAXW=2e3+2;
char nums[MAXH][MAXW];//็จไบไฟๅญ่ฟทๅฎซ
long long row[MAXH];//่ก่ตฐๆณ
long long col[MAXW];//ๅ่ตฐๆณ
long long dia[MAXH+MAXW];//ๅฏน่ง็บฟ
const long long MO=1e9+7;
/*่ฎก็ฎๅฏน่ง็บฟ็ๆถๅ i-j ๅฏ่ฝๅบ็ฐ่ดๆฐ๏ผๆๅคง็่ดๆฐๆฏ 1-2000=-1999๏ผๆไปฅ้่ฆไธไธช OFFSET ไฟ่ฏๆญฃไธๆ */
const int OFFSET=max(MAXH, MAXW);//ๅช่ฆ่ถ
่ฟMAXHๅMAXWๆๅคงๅผๅฐฑๅฏไปฅ
int main() {
#if 1
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
#endif
int h,w;
cin>>h>>w;
for (int i=1; i<=h; i++) {
for (int j=1; j<=w; j++) {
cin>>nums[i][j];
}
}
long long ans=-1;
for (int i=1; i<=h; i++) {
for (int j=1; j<=w; j++) {
if ('#'==nums[i][j]) {
row[i] = 0;
col[j] = 0;
dia[i-j+OFFSET] = 0;
} else {
long long res = (row[i]+col[j]+dia[i-j+OFFSET])%MO;
if (1==i && 1==j) {
res++;
}
ans = res;
//ๆดๆฐ
row[i] = (row[i]+res)%MO;
col[j] = (col[j]+res)%MO;
dia[i-j+OFFSET] = (dia[i-j+OFFSET]+res)%MO;
}
}
}
cout<<ans<<"\n";
return 0;
} | #include<bits/stdc++.h>
#include<cmath>
#define SZ(x) ((int)x.size())
#include<vector>
#include<set>
#include<map>
#define ll long long
#define lli long long int
#define REP(i,a,b) for(ll i=a;i<b;i++)
#define pb push_back
#include<string>
#include<cctype>
#define F first
#define rep(i,a,b) for(int i=a;i<b;i++)
#define S second
#include<queue>
#include <sstream>
#define lli long long int
#define pairs pair<int,int>
#define ld long double
#define mod 1000000007;
const double PI = 3.141592653589793238460;
typedef std::complex<double> Complex;
typedef std::valarray<Complex> CArray;
using namespace std;
//first try to write all condition on copy then go for implement
ll pows(ll a,ll n,ll m)
{
a=a%mod;
ll res=1;
while(n)
{
if(n%2!=0)
{
res=(res*a)%m;
n--;
}
else
{
a=(a*a)%m;
n=n/2;
}
}
return res%m;
}
int gcd(int a,int b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
bool isprime(ll n)
{
if(n==1)
{
return false;
}
for(ll i=2;i*i<=n;i++)
{
if(n%i==0)
{
return false;
}
}
return true;
}
bool istrue(string s)
{
int i=0;
int j=s.size()-1;
while(i<j)
{
if(s[i]==s[j])
{
i++;
j--;
}
else
{
return false;
}
}
return true;
}
const int N=2005;
string s[N];
ll dp[N][N];
ll phor[N][N];
ll pver[N][N];
ll pdia[N][N];
bool wall(int x,int y){
if(x < 1 || y < 1 || s[x-1][y-1] == '#') return true;
return false;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int h,w;
cin >> h >> w;
rep(i,0,h){
cin >> s[i];
}
dp[1][1] = 1;
phor[1][1] = 1;
pver[1][1] = 1;
pdia[1][1] = 1;
rep(i,1,h+1){
rep(j,1,w+1){
if(i == 1 && j == 1) continue;
if(wall(i,j)) continue;
dp[i][j] = (phor[i-1][j] + pver[i][j-1] + pdia[i-1][j-1]) % mod;
phor[i][j] = dp[i][j];
pver[i][j] = dp[i][j];
pdia[i][j] = dp[i][j];
phor[i][j] += phor[i-1][j];
pver[i][j] += pver[i][j-1];
pdia[i][j] += pdia[i-1][j-1];
phor[i][j] %= mod;
pver[i][j] %= mod;
pdia[i][j] %= mod;
}
}
cout << dp[h][w]<<endl;
} |
/* author : sgupta_2001 */
#include<bits/stdc++.h>
using namespace std;
using db = double;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
//containers
#define sz(x) int((x).size())
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define rall(x) x.rbegin(), x.rend()
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pb push_back
#define eb emplace_back
#define pf push_front
//pairs
#define mp make_pair
#define ff first
#define ss second
//loops
#define rep(i, a, b) for(int i = a; i < b; ++i)
#define per(i, a, b) for(int i = b - 1; i >= a; --i)
#define each(x, a) for(auto &x : a)
//popular constants
const int mod = 1e9 + 7; //998244353;
const ll inf = 1e18;
const ld pie = acos((ld) - 1);
//grid problems
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
//modulo operations
ll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}
ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}
ll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}
#ifdef SGUPTA_2001
#define deb(x) cerr<< #x <<" "; __print(x); cerr<<'\n';
#else
#define deb(x);
#endif
//debug containers
template<class T> void __print(T x) {cerr<<x;}
template<class T, class U> void __print(pair<T,U> p) {cerr<<"{"<<p.ff<<", "<<p.ss<<"}";}
template<class T> void __print(vector<T> arr) { cerr<<"[ "; for(auto x:arr) { __print(x);cerr<<" "; } cerr<<"]"; }
template<class T> void __print(set<T> st) { cerr<<"{ "; for(auto x:st) { __print(x); cerr<<" "; } cerr<<"}";}
template<class T, class U> void __print(vector<pair<T,U>> arr) { cerr<<"[ "; for(auto x:arr) {__print(x);cerr<<", ";}cerr<<"]";}
template<class T, class U> void __print(unordered_map<T,U> arr) { cerr<<"{ "; for(auto x:arr) {__print(x);cerr<<", ";}cerr<<"}";}
template<class T, class U> void __print(map<T,U> arr) { cerr<<"{ "; for(auto x:arr) {__print(x);cerr<<", ";}cerr<<"}";}
template<typename... T> void read(T&... args) { ((cin>>args),...);}
template<typename... T> void write(T&&... args){((cout<<args<<" "),...);cout<<'\n';}
void solve() {
int N;
scanf("%d", &N);
vector<int> arr(N);
rep(i, 0, N) scanf("%d", &arr[i]);
unordered_map<int, vector<int>> cur;
rep(i, 0, N) {
cur[arr[i]].pb(i);
}
ll ans = 0;
int foo = 1;
rep(i, 0, N) {
int idx = upper_bound(all(cur[arr[i]]), i) - cur[arr[i]].begin();
deb(idx);
ans += N - (cur[arr[i]].size() - idx) - foo;
deb(ans);
foo++;
}
printf("%lld\n", ans);
return;
}
int main() {
#ifdef SGUPTA_2001
freopen("input.txt", "r", stdin);
freopen("/home/shivansh/Desktop/error.txt", "w", stderr);
#endif
auto start = std::chrono::high_resolution_clock::now();
int t;
t=1;
while(t--) solve();
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(stop - start);
// cerr << "Time taken : " << ((long double)duration.count())/((long double) 1e9) <<"s "<< '\n';
/*
STUFF TO LOOK:
1. Check the constraints
2. Corner cases(n == 0 || n == 1)
3. Variables in loops
4. Make sure two ints arenโt multiplied to get a long long
*/
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#include<cmath>
#include<algorithm>
int main()
{
long long int n,i;
cin>>n;
long long int a[n];
for(i=0;i<n;i=i+1)
{
cin>>a[i];
}
sort(a,a+n);
long long int sum=0;
for(i=0;i<n;i=i+1)
{
sum=sum+n-(upper_bound(a,a+n,a[i])-a);
}
cout<<sum<<endl;
}
|
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 1e9+7;
int main(){
int N; cin >> N;
vector<vector<ll>> edge(N), weight(N);
for(int i=1; i<N; i++){
ll u,v,w; cin >> u >> v >> w;
edge[--u].push_back(--v);
edge[v].push_back(u);
weight[u].push_back(w);
weight[v].push_back(w);
}
vector<ll> dist(N,-1);
std::queue<int> que;
que.push(0);
dist[0] = 0;
while(!que.empty()){
int now = que.front(); que.pop();
for(int i=0; i<edge[now].size(); i++){
int next = edge[now][i];
ll sum = dist[now]^weight[now][i];
if(dist[next] == -1){
dist[next] = sum;
que.push(next);
}
}
}
ll ans = 0;
for(int i=0; i<60; i++){
vector<int> cnt(2);
for(int j=0; j<N; j++) cnt[dist[j]>>i&1]++;
ans += (1ll<<i)%mod*cnt[0]%mod*cnt[1];
ans %= mod;
}
cout << ans << endl;
} | #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mkp make_pair
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define ff first
#define ss second
#define endl "\n"
#define MOD 1000000007
#define MOD1 998244353
#define inf 1e18
ll GCD(ll x,ll y){if(y==0)return x;return GCD(y,x%y);}
ll LCM(ll x,ll y){return (x*y)/(GCD(x,y));}
ll LOGK(ll x,ll k){if(x>=k)return 1+LOGK(x/k,k);return 0;}
ll MPOW( ll a, ll b, ll m) { if(b==0) return 1; ll x=MPOW(a,b/2,m); x=(x*x)%m; if(b%2==1) x=(x*a)%m; return x;}
ll MINV(ll a,ll m) {return MPOW(a,m-2,m);}
class pnc{
ll FACT_MAX,MODU;
vector<ll> fact;
public:
pnc( ll n, ll m) { FACT_MAX = n; fact.resize(FACT_MAX); MODU = m; MFACT_INIT(MODU);}
void MFACT_INIT( ll m) { fact[0]=1; for(ll i=1;i<FACT_MAX;++i) fact[i]=(i*fact[i-1])%MODU;}
ll MFACT( ll n) { return fact[n];}
ll PERM( ll n, ll r) { return (fact[n]*::MINV(fact[n-r],MODU))%MODU;}
ll COMB( ll n , ll r) { return (PERM(n,r)*::MINV(fact[r],MODU))%MODU;}
};
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll test=1;
cin>>test;
while(test--)
{
ll n,a,b;
cin>>n>>a>>b;
if(a+b>n)
{
cout<<0<<endl;
continue;
}
ll x,y;
x=n-a+1;
y=n-b+1;
ll ans=(x*x)%MOD;
ans*=(y*y)%MOD;
ans%=MOD;
ll no=(x*y)%MOD;
ll g=((n-a-b+1)*(n-a-b+2))%MOD;
no-=g;
no+=MOD;
no%=MOD;
no*=no;
no%=MOD;
ans-=no;
ans+=MOD;
ans%=MOD;
cout<<ans<<endl;
}
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define f first
#define s second
#define pb push_back
#define mod 1000000007
int main(){
ios::sync_with_stdio(0);cin.tie(0);
int t;cin >> t;
while(t--){
ll l,r,count;
cin >> l >> r;
if(r/2<l)count =0;
else count = r-l-l+1;
cout << count*(count+1)/2 << '\n';
}
}
| #include<bits/stdc++.h>
using namespace std;
namespace hyy {
#define DEBUG 1
#define isdigit(x) (x >= '0' && x <= '9')
const int MAXSIZE = (1 << 20);
inline char gc() {
#if DEBUG
return getchar();
#endif
static char buf[MAXSIZE];
static char *p1 = buf + MAXSIZE;
static char *p2 = buf + MAXSIZE;
if(p1 == p2) {
p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin);
}
if(p1 == p2) {
return -1;
}
return *p1++;
}
inline bool blank(char ch) {
return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
}
template<class T> inline void read(T &x) {
register double tmp = 1;
register bool sign = 0;
x = 0;
register char ch = gc();
for(; !isdigit(ch); ch = gc()) {
if(ch == '-') {
sign = 1;
}
}
for(; isdigit(ch); ch = gc()) {
x = (x << 1) + (x << 3) + (ch ^ 48);
}
if(ch == '.') {
for(ch = gc(); isdigit(ch); ch = gc()) {
tmp /= 10.0;
x += tmp * (ch - 48);
}
}
if(sign) {
x = -x;
}
}
inline void read(char *s) {
register char ch = gc();
for(; blank(ch); ch = gc());
for(; !blank(ch); ch = gc()) {
*s++=ch;
}
*s = 0;
}
inline void read(char &c) {
for(c = gc(); blank(c); c = gc());
}
inline void push(const char &c) {
char pbuf[MAXSIZE];
char *pp = pbuf;
if (pp - pbuf == MAXSIZE) {
fwrite(pbuf, 1, MAXSIZE, stdout);
pp = pbuf;
}
*pp++=c;
}
template<class T> inline void write(T x) {
static T sta[35];
T top=0;
do {
sta[top++] = x % 10;
x /= 10;
}
while(x);
#if DEBUG
while(top) {
putchar(sta[--top] + '0');
}
return;
#endif
while(top) {
push(sta[--top] + '0');
}
}
template<class T> inline void write(T x, char lastChar) {
write(x);
putchar(lastChar);
}
}
using namespace hyy;
#define MAXN 200005
int lowbit(int x) {
return x & (-x);
}
int n,sum[MAXN];
void Add(int x,int w) {
for(;x<=n;sum[x]+=w,x+=lowbit(x));
}
int Sum(int x) {
int ans=0;
for(;x;ans+=sum[x],x-=lowbit(x));
return ans;
}
struct Edge {
int next,to;
}E[MAXN<<1];
int head[MAXN],cnt;
void AddE(int x,int y) {
E[++cnt].next=head[x],E[cnt].to=y,head[x]=cnt;
}
int Q,fa[MAXN],ans[MAXN],dep[MAXN];
struct Qry {
int y,id;
}p;
vector<Qry>q[MAXN];
void dfs(int x,int fa) {
dep[x]=dep[fa]+1;
for(int i=0;i<q[x].size();++i) {
ans[q[x][i].id]-=Sum(q[x][i].y)-Sum(q[x][i].y-1);
}
for(int i=head[x];i;i=E[i].next) {
dfs(E[i].to,x);
}
Add(dep[x],1);
for(int i=0;i<q[x].size();++i) {
ans[q[x][i].id]+=Sum(q[x][i].y)-Sum(q[x][i].y-1);
}
}
int main() {
scanf("%d",&n);
for(int i=2;i<=n;++i) {
scanf("%d",&fa[i]),AddE(fa[i],i);
}
scanf("%d",&Q);
for(int i=0,x,y;i<Q;++i) {
scanf("%d%d",&x,&y),p.y=y+1,p.id=i,q[x].push_back(p);
}
dfs(1,0);
for(int i=0;i<Q;++i) {
printf("%d\n",ans[i]);
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define ff first
#define ss second
#define pb push_back
#define print(...) cout << " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << " ]"
#define printv(...) cout << " [" << #__VA_ARGS__ ": "; for (int i = 0; i < (__VA_ARGS__).size(); ++i) cout << (__VA_ARGS__)[i] <<" "; cout << "] "
#define printa(...) cout << " [" << #__VA_ARGS__ ": "; for (int i = 0; i < sizeof((__VA_ARGS__))/sizeof((__VA_ARGS__)[0]); ++i) cout << (__VA_ARGS__)[i] <<" "; cout << "] "
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> dist(1,10000000); // distribution in range [1, 6]
int32_t main() {
cin.tie(NULL);
cout.precision(10);
int tt = 1;
// cin >> tt;
while (tt--) {
double a, b;
cin >> a >> b;
cout << fixed << a / 100 * b << "\n";
}
return 0;
}
| #include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define pf printf
#define ff first
#define ss second
#define sef second.first
#define ses second.second
#define PI 3.14159265 /// tan inverse = atan(value)*(180/PI)
#define ms(a,b) memset(a, b, sizeof(a))
#define lp(i,a,b) for (int i = a; i <= b; i++)
#define pii pair <int,int>
#define SL(a) scanf("%I64d",&a)
#define SLL(a,b) scanf("%I64d %I64d",&a,&b)
#define PL(x) printf("%I64d\n",(x))
#define PLL(x,y) printf("%I64d %I64d\n",x,y)
#define S(a) scanf("%d",&a)
#define SS(a,b) scanf("%d %d",&a,&b)
#define P(x) printf("%d\n",(x))
#define PP(x,y) printf("%d %d\n",x,y)
using namespace std ;
typedef long long ll ;
const int imin = 1e9+100;
const ll maxx = 1e5+10,mod=1e9+7,imax=1e18+10;
int main()
{
ll a,b,c ;
cin >> a >> b >> c ;
if((a*a+b*b)<(c*c))
{
cout << "Yes" << endl ;
}
else
{
cout << "No" << endl;
}
return 0 ;
}
|
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long int
#define double long double
#define pb push_back
#define fi first
#define se second
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pii>
#define pqi priority_queue<int>
#define test int tcase; cin>>tcase; for(int tc = 1; tc <= tcase; tc++)
#define inp(a,n,f) int a[n+f];for(int hh=f;hh<n+f;hh++)cin>>a[hh];
#define printdecimal(k) cout<<fixed<<setprecision(k);
#define mem(a,k) memset(a,k,sizeof(a))
#define ub upper_bound
#define lb lower_bound
#define all(v) v.begin(),v.end()
#define mod (int)(1e9+7)
#define inf LLONG_MAX
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
int exp(int x,int y){int res=1;x=x%mod;while(y>0){if(y&1)res=(res*x)%mod;y=y>>1;x=(x*x)%mod;}return res;}
int modinv(int x){return exp(x,mod-2);}
int add(int a,int b){a%=mod,b%=mod;a=((a+b)%mod+mod)%mod;return a;}
int sub(int a,int b){a%=mod,b%=mod;a=((a-b)%mod+mod)%mod;return a;}
int mul(int a,int b){a%=mod,b%=mod;a=((a*b)%mod+mod)%mod;return a;}
#define N (int)(2e6+6)
struct segtree
{
int size;
vector<int> values;
void ini(int n)
{
size=1;
while(size<n)
size*=2;
values.assign(2*size,1);
}
void set(int i,int v,int cur,int lx,int rx)
{
if(rx-lx==1)
{
values[cur]=v;
return;
}
int m=(lx+rx)/2;
if(i<m) set(i,v,2*cur+1,lx,m);
else set(i,v,2*cur+2,m,rx);
values[cur]=max(values[2*cur+1],values[2*cur+2]);
}
void set(int i,int v)
{
set(i,v,0,0,size);
}
int cal(int v,int cur,int lx,int rx)
{
if(values[cur]<v) return -1;
if(rx-lx==1) return lx;
int m=(lx+rx)/2;
int val=cal(v,2*cur+1,lx,m);
if(val==-1) return cal(v,2*cur+2,m,rx);
return val;
}
int cal(int v)
{
return cal(v,0,0,size);
}
};
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
for(int i=0;i<100;i++){}
int n,m;
cin>>n>>m;
segtree st;
st.ini(N);
inp(a,n,0);
int h[n+1]={0};
for(int i=0;i<m-1;i++)
st.set(a[i],0),h[a[i]]++;
int ans=inf;
for(int j=m-1,i=0;j<n;j++,i++)
{
if(h[a[j]]==0) st.set(a[j],0);
h[a[j]]++;
int idx=st.cal(1);
ans=min(ans,idx);
h[a[i]]--;
if(h[a[i]]==0) st.set(a[i],1);
}
for(int i=0;i<100;i++){}
cout<<ans;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define yes(x) ((x) ? "yes" : "no")
#define Yes(x) ((x) ? "Yes" : "No")
using i64 = int_fast64_t;
using u64 = uint_fast64_t;
using f64 = double_t;
using p64 = pair<i64, i64>;
constexpr i64 INF = INT_FAST64_MAX / 2;
void solve() {
i64 N, M;
cin >> N >> M;
vector<i64> A(N);
for (auto &e : A) cin >> e;
set<i64> unused;
for (i64 i = 0; i < N; i++) unused.insert(i);
vector<i64> usedcnt(N);
for (i64 i = 0; i < M; i++) usedcnt[A[i]]++, unused.erase(A[i]);
i64 ans = unused.empty() ? N : *unused.begin();
for (i64 i = M; i < N; i++) {
if (++usedcnt[A[i]] == 1) unused.erase(A[i]);
if (--usedcnt[A[i - M]] == 0) unused.insert(A[i - M]);
ans = min(ans, *unused.begin());
}
cout << ans << endl;
}
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(15);
solve();
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll; //int:2*10**9
typedef long double ld;
typedef pair<ll,ll> P;
#define REP(i,n) for(ll i = 0; i<(ll)(n); i++)
#define FOR(i,a,b) for(ll i=(a);i<=(b);i++)
#define FORD(i,a,b) for(ll i=(a);i>=(b);i--)
#define pb push_back
#define MOD 1000000007 //998244353
#define PI 3.141592653
#define INF 100000000000000 //14
//cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);
int main(){
ll n; cin >> n;
vector<vector<ll>> c(n,vector<ll>(n,0));
ll minc = INF;
REP(i,n)REP(j,n) {
cin >> c[i][j];
}
vector<ll> b(n,0);
vector<ll> a(n,0);
REP(i,n) {
if (i==0) {
REP(j,n) b[j]=c[i][j];
}
else {
a[i]=c[i][0]-b[0];
REP(j,n) {
if (a[i]+b[j]!=c[i][j]) {
cout << "No" << endl;
return 0;
}
}
}
}
ll amin = INF; ll bmin = INF;
// cout << "test" << endl;
// REP(i,n) cout << a[i] << " ";
// cout << endl;
// REP(i,n) cout << b[i] << " ";
// cout << endl;
REP(i,n) amin = min(amin,a[i]);
REP(i,n) bmin = min(bmin,b[i]);
if (amin<0 && bmin<0) {
cout << "No" << endl;
return 0;
}
if (amin<0) {
if (bmin+amin<0) {
cout << "No" << endl;
return 0;
}
else {
ll add = 0-amin;
REP(i,n) {
a[i]+=add;
b[i]-=add;
}
}
}
if (bmin<0) {
if (bmin+amin<0) {
cout << "No" << endl;
return 0;
}
else {
ll add = 0-bmin;
REP(i,n) {
a[i]-=add;
b[i]+=add;
}
}
}
cout << "Yes" << endl;
REP(i,n) cout << a[i] << " ";
cout << endl;
REP(i,n) cout << b[i] << " ";
cout << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
template <typename T> inline T gcd(T a, T b) { while (b != 0) swap(b, a %= b); return a;}
template <typename T> inline T lcm(T a, T b) { return ((a/gcd(a,b))*b);}
#define all(x) (x).begin(),(x).end()
#define sort(x) sort(x.begin(),x.end())
#define maxin(x) *max_element(x.begin(),x.end())
#define minin(x) *min_element(x.begin(),x.end())
#define lint long long int
#define nl '\n'
#define INF (lint)1e18
#define MOD (lint)998244353
void solve(){
lint n;
cin>>n;
vector<lint> arr(n);
for(auto &a: arr) cin>>a;
sort(arr);
lint ans = arr[n-1]*arr[n-1];
lint t = arr[n-1];
for(lint i=n-2;i>=0;i--){
ans+=(arr[i]*arr[i])%MOD;
ans=(ans+t*arr[i])%MOD;
t=(2*t+arr[i])%MOD;
}
cout<<ans%MOD;
}
int main()
{ ios_base::sync_with_stdio(0);cin.tie(0);lint testnum=1,totalnum=1;
//cin>>totalnum;
for(testnum=1;testnum<=totalnum;++testnum){
//cout<<"Case #"<<testnum<<": ";
solve();
cout<<nl;
}
return 0;}
/*
created on: 01.04.2021
*/
|
#include<bits/stdc++.h>
using namespace std;
int solve(int n)
{
int cnt=0;
vector<bool>arr(n+1);
for(int i=7; i<=n; i++)
{
int temp=i;
while(temp>0)
{
if(temp%10==7)
{
arr[i]=true;
break;
}
temp/=10;
}
}
for(int i=7; i<=n; i++)
{
int dig = 1;
int num = 0;
int l=i;
while(l>0)
{
int x = l%8;
num += x*dig;
dig*=10;
l/=8;
}
int temp=num;
while(temp>0)
{
if(temp%10==7)
{
arr[i]=true;
break;
}
temp/=10;
}
}
for(int i=7; i<=n; i++)
{
if(arr[i])
cnt++;
}
return n-cnt;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//int t;cin>>t;
//while(t--)
{
int n;
cin>>n;
cout<<solve(n)<<endl;
}
return 0;
}
| #pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <random>
#include <fstream>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> p32;
typedef pair<ll, ll> p64;
typedef pair<double, double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int>> vv32;
typedef vector<vector<ll>> vv64;
typedef vector<vector<p64>> vvp64;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
#define mod 256
double eps = 1e-12;
#define fors(i, e) for (ll i = 0; i < e; i++)
#define forsn(i, s, e) for (ll i = s; i < e; i++)
#define rforn(i, s) for (ll i = s; i >= 0; i--)
#define rforsn(i, s, e) for (ll i = s; i >= e; i--)
#define ln "\n"
#define dbg(x) cout << #x << " = " << x << ln
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define INF 2e18
#define fast_cin() \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
#define iterate(x) for (auto itr = x.begin(); itr != x.end(); ++itr)
ll myceil(ll a, ll b)
{
if (a % b == 0)
return a / b;
else
return a / b + 1;
}
vector<ll> vec;
bool isPerSquare(long double a)
{
if (a < 0)
return false;
ll sr = sqrt(a);
return (sr * sr == a);
}
ll power(long long x, unsigned int y, int p)
{
int res = 1;
x = x % p;
if (x == 0)
return 0;
while (y > 0)
{
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
unsigned int reverseBits(unsigned int n)
{
unsigned int rev = 0;
while (n > 0)
{
rev <<= 1;
if (n & 1 == 1)
rev ^= 1;
n >>= 1;
}
return rev;
}
string decimalToBinary(int n)
{
string s = bitset<64>(n).to_string();
const auto loc1 = s.find('1');
if (loc1 != string::npos)
return s.substr(loc1);
return "0";
}
void solve()
{
double n;
cin >> n;
double k = floor((double)(n * (double)(1.08)));
if (k == 206)
{
cout << "so-so";
return;
}
if (k < 206)
{
cout << "Yay!";
return;
}
cout << ":(";
}
int main()
{
solve();
// ll t;
// cin >> t;
// for (ll i = 1; i <= t; i++)
// {
// solve();
// }
} |
#include <bits/stdc++.h>
using namespace std;
#define repr(i,a,b) for (int i=a; i<b; i++)
#define rep(i,n) for (int i=0; i< n; i++)
#define PI 3.14159265359
const long long INF = 1LL << 60;
long long MOD = 1000000007;
long long gcd(long long a, long long b) { return b ? gcd(b, a%b) : a; }
long long lcm (int a, int b){return (long long) a*b /gcd(a,b);}
int main(){
int r1, c1, r2, c2;
cin >> r1 >> c1 >> r2 >> c2;
int r = abs(r2-r1), c = abs(c2-c1);
int ans = 3;
if( r == 0 && c == 0)ans = 0;
else if( r == c || r + c <=3 )ans = 1;
else if ((r + c)% 2 == 0 || r + c <= 6 || abs(r-c) <= 3)ans = 2;
cout << ans << endl;
} | #include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) begin(x),end(x)
#define F(i,n) for (int i = 0; i < n; ++i)
#define F1(i,n) for (int i = 1; i <= n; ++i)
#define dbg(x) cerr << #x << " = " << x << endl
#define dbgg(x) cerr << #x << " = " << x << ' '
#define T(x) x[pool]
#define mineq(x,y) { if ((x) > (y)) (x) = (y); }
#define maxeq(x,y) { if ((x) < (y)) (x) = (y); }
#define MEOW cout << "meowwwww" << '\n'; system("pause");
#define int long long
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
template<typename T>
ostream& operator <<(ostream &s, const vector<T> &c)
{
s << "[ "; for (auto it : c) s << it << " "; s << "\b]\n";
return s;
}
template<typename T>
ostream& operator <<(ostream &s, const pair<int, T> &c)
{
s << "[ "; cout << c.fi << " , " << c.se << " ] ";
return s;
}
int n;
void input()
{
ios::sync_with_stdio(false); cin.tie(0);
cin >> n;
}
void solve()
{
vi p1, p2, p3, p4, ans;
int k = n / 3;
int cur = 0;
F1 (i, 10000) while (++cur < 10000)
if (cur % 5 != 0 && cur * 6 <= 10000)
{
p1.pb(cur * 6);
break;
}
cur = 0;
F1 (i, 10000) while (++cur < 10000)
if (cur % 2 != 0 && cur * 15 <= 10000)
{
p2.pb(cur * 15);
break;
}
cur = 0;
F1 (i, 10000) while (++cur < 10000)
if (cur % 3 != 0 && cur * 10 <= 10000)
{
p3.pb(cur * 10);
break;
}
cur = 0;
F1 (i, 10000) while (++cur < 10000)
if (cur * 30 <= 10000)
{
p4.pb(cur * 30);
break;
}
ans.pb(p1[0]);
ans.pb(p2[0]);
ans.pb(p3[0]);
n -= 3;
F1 (i, p1.size() - 1) if (n) { ans.pb(p1[i]); --n; }
F1 (i, p2.size() - 1) if (n) { ans.pb(p2[i]); --n; }
F1 (i, p3.size() - 1) if (n) { ans.pb(p3[i]); --n; }
F1 (i, p4.size() - 1) if (n) { ans.pb(p4[i]); --n; }
for (auto x : ans) cout << x << ' ';
cout << '\n';
}
main()
{
input();
solve();
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n,m,i,j,k,l,ans,cur,res;
cin >> n;
vector<long long> a(n);
for(i=0; i<n; i++){
cin >> a[i];
}
map<long long,long long> mp;
cur=0;
mp[cur]=1;
ans=0;
for(i=0; i<n; i++){
if(i%2==0){
cur+=a[i];
}else{
cur-=a[i];
}
ans+=mp[cur];
mp[cur]=mp[cur]+1;
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
template<class T> using V = vector<T>;
using VI = V<int>;
using VL = V<ll>;
using VS = V<string>;
template<class T> using PQ = priority_queue<T, V<T>, greater<T>>;
using graph = V<VI>;
template<class T> using w_graph = V<V<pair<int, T>>>;
#define FOR(i,a,n) for(int i=(a);i<(n);++i)
#define eFOR(i,a,n) for(int i=(a);i<=(n);++i)
#define rFOR(i,a,n) for(int i=(n)-1;i>=(a);--i)
#define erFOR(i,a,n) for(int i=(n);i>=(a);--i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define out(y,x) ((y)<0||h<=(y)||(x)<0||w<=(x))
#ifdef _DEBUG
#define line cout << "-----------------------------\n"
#define stop system("pause")
#endif
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 60;
constexpr ll mod = 1000000007;
constexpr ll MOD = 998244353;
constexpr ld eps = 1e-10;
template<class T>inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; }return false; }
template<class T>inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; }return false; }
inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); }
template<class T>inline istream& operator>>(istream& is, V<T>& v) { for (auto& a : v)is >> a; return is; }
template<class T, class U>inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template<class T>inline V<T> vec(size_t a) { return V<T>(a); }
template<class T>inline V<T> defvec(T def, size_t a) { return V<T>(a, def); }
template<class T, class... Ts>inline auto vec(size_t a, Ts... ts) { return V<decltype(vec<T>(ts...))>(a, vec<T>(ts...)); }
template<class T, class... Ts>inline auto defvec(T def, size_t a, Ts... ts) { return V<decltype(defvec<T>(def, ts...))>(a, defvec<T>(def, ts...)); }
template<class T>inline void print(const T& a) { cout << a << "\n"; }
template<class T, class... Ts>inline void print(const T& a, const Ts&... ts) { cout << a << " "; print(ts...); }
template<class T>inline void print(const V<T>& v) { for (int i = 0; i < v.size(); ++i)cout << v[i] << (i == v.size() - 1 ? "\n" : " "); }
template<class T>inline void print(const V<V<T>>& v) { for (auto& a : v)print(a); }
template<class T>inline T sum(const V<T>& a, int l, int r) { return a[r] - (l == 0 ? 0 : a[l - 1]); }
template<class T>inline void END(T s) { print(s); exit(0); }
void END() { exit(0); }
int main() {
init();
int n; cin >> n;
VL a(n); cin >> a;
FOR(i, 1, n) {
if (i & 1)a[i] = a[i - 1] - a[i];
else a[i] = a[i - 1] + a[i];
}
ll ans = 0;
map<ll, int> m;
++m[0];
FOR(i, 0, n) {
ans += m[a[i]];
++m[a[i]];
}
print(ans);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ld = long double;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
#define max(p,q) ((p)>(q)?(p):(q))
#define min(p,q) ((p)<(q)?(p):(q))
#define rep(i,n) for (int i = 0, i##_len = (n); i < i##_len; i++)
#define rep1(i,n) for (int i = 1, i##_len = (n); i <= i##_len; i++)
#define repr(i,n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rep1r(i,n) for (int i = ((int)(n)); i >= 1; i--)
#define repl(i,l,n) for (int i = (l), i##_len = (n); i < i##_len; i++)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define RSORT(x) sort(rall(x));
#define mp make_pair
#define has_key(m, k) (m.find(k) != m.end())
#define INF (1e9)
#define EPS (1e-7)
// ็ทๆฐใ1000000007๏ผ็ด ๆฐ๏ผใงๅฒใฃใไฝใ
const ll mod = (1000000007);
ull gcd(ull a, ull b) { return b ? gcd(b, a % b) : a; }
ull lcm(ull a, ull b) { return a / gcd(a, b) * b; }
int main(){
long long a;
scanf("%lld",&a);
long long b;
scanf("%lld",&b);
long long c;
scanf("%lld",&c);
long long d;
scanf("%lld",&d);
cout << b - c << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define MOD 2000000011
#define MAXL 26
#define MAXN 100010
#define MAXT 200
#define MAXM 101
#define PI 3.14159265359
// #define all(v) v.begin(),v.end()
#define f first
#define pb push_back
#define pii pair<int, int>
#define s second
// #define sortva(v) sort(all(v))
// #define sortvd(v) sort(v.rbegin(),v.rend())
// #define sortaa(a,n) sort(a,a+n)
// #define sortad(a,n) sort(a,a+n),reverse(a,a+n)
// #define sfi1(v) scanf("%d",&v)
// #define sfi2(v1,v2) scanf("%d %d",&v1,&v2)
// #define sfi3(v1,v2,v3) scanf("%d %d %d",&v1,&v2,&v3)
// #define sfll1(v) scanf("%I64d",&v);
// #define sfll2(v1,v2) scanf("%I64d %I64d",&v1,&v2)
// #define sfll3(v1,v2,v3) scanf("%I64d %I64d %I64d",&v1,&v2,&v3)
// #define sfstr(v) scanf("%s", v);
// #define sz(v) (int)v.size()
#define vi vector<int>
#define X real()
#define Y imag()
typedef double C;
typedef complex<C> P;
typedef long long LL;
typedef unsigned long long uLL;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
int n;
pair<int, pii> store[MAXN];
int main(int argc, char const *argv[])
{
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
cin.tie(0);
ios_base::sync_with_stdio(NULL);
cin >> n;
int a, p, x;
for (int i = 0; i < n; ++i)
{
cin >> a >> p >> x;
store[i] = {p, {a, x}};
}
sort(store, store + n);
for (int i = 0; i < n; ++i)
{
if (store[i].s.f < 0.5 + (double)store[i].s.s - 1)
{
printf("%d\n", store[i].f);
return 0;
}
}
printf("-1\n");
return 0;
} |
#include <bits/stdc++.h>
#define MN 1001000
using namespace std;
typedef long long ll;
ll t, n, a[MN], med;
//char s[MN]; string str;
double ans;
int main() {
scanf("%lld", &n);
for(int i = 0; i < n; i++) {
scanf("%lld", &a[i]);
ans += a[i] / 2.0;
}
sort(a, a + n);
med = a[n/2];
for(int i = 0; i < n; i++) {
ans += abs(a[i] - med) / 2.0;
}
printf("%.9f\n", ans / n);
return 0;
}
| #define _USE_MATH_DEFINES
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <iostream>
#include <fstream>
#include <chrono>
using namespace std;
void solve(int test, std::istream &in) {
int64_t n;
in >> n;
int64_t n0 = n;
vector<int64_t> f(2, 1);
while (f.back() < n)
f.push_back(f.back() + f[f.size() - 2]);
vector<int> num;
int idx = f.size() - 1;
while (n > 0) {
while (f[idx] > n)
idx--;
n -= f[idx];
num.push_back(idx);
}
vector<int> op;
idx = 0;
int plus = 0, wait = 0, nextPlus = (num[0] + 1) & 1;
int64_t x[2] = { 0, 0 };
while (x[0] != n0) {
if (wait > 0) {
plus++;
op.push_back(3 + nextPlus);
x[nextPlus] += x[1 - nextPlus];
nextPlus = 1 - nextPlus;
wait--;
}
else {
op.push_back(1 + 1 - nextPlus);
x[1 - nextPlus]++;
idx++;
if (idx >= num.size()) {
wait = num[0];
}
else {
wait = num[idx - 1] - num[idx];
}
}
}
printf("%d\n", (int)op.size());
for (int i = 0; i < op.size(); i++)
printf("%d\n", op[i]);
//printf("x = %lld, y = %lld\n", x[0], x[1]);
}
int main(int argc, char *argv[]) {
#ifdef VLAD_LOCAL
ifstream in("in.txt");
auto start = std::chrono::steady_clock::now();
#else
istream &in = cin;
#endif
solve(0, in);
#ifdef VLAD_LOCAL
auto end = std::chrono::steady_clock::now();
double seconds = (double)std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() / 1000;
printf("%.3lf seconds\n", seconds);
#endif
return 0;
}
|
#include "bits/stdc++.h"
using namespace std;
#define REP(i, n) for(ll i = 0;i < n;i++)
#define ll long long
#define MOD 1000000007LL
//#define MOD 998244353LL
#define doublecout(a) cout<<setprecision(16)<<a<<endl;
using vi = vector<ll>; // intใฎ1ๆฌกๅ
ใฎๅใซ vi ใจใใๅฅๅใใคใใ
using vvi = vector<vi>; // intใฎ2ๆฌกๅ
ใฎๅใซ vvi ใจใใๅฅๅใใคใใ
using vvvi = vector<vvi>; // intใฎ2ๆฌกๅ
ใฎๅใซ vvi ใจใใๅฅๅใใคใใ
const ll llMAX=9223372036854775807LL;
const ll llMIN=-9223372036854775808LL;
void myprint1D(vi &data)
{
REP(i,data.size())
cout<<data[i]<<" ";
cout<<endl;
}
//้
ๅใ[y][x]ใง่กจ็คบ
void myprint2D_T(vvi &data)
{
REP(i,data.size())
myprint1D(data[i]);
}
//้
ๅใ[x][y]ใง่กจ็คบ
void myprint2D(vvi &data)
{
ll l1=data.size();
ll l2=data[0].size();
REP(j,l2){
REP(i,l1)
cout<<data[i][j]<<" ";
cout<<endl;
}
}
//print(a,b...)ใฃใฆไฝฟใๆน
void print1(ll a){cout<<a<<endl;}
void print2(ll a,ll b){cout<<a<<" "<<b<<endl;}
void print3(ll a,ll b,ll c){cout<<a<<" "<<b<<" "<<c<<endl;}
void print4(ll a,ll b,ll c,ll d){cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl;}
void print5(ll a,ll b,ll c,ll d,ll e){cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<endl;}
//moddivใจnCkใจgarner
//cๅฐไฝไธใงaใbใงๅฒใฃใๅค
ll moddiv(ll a,ll b,ll c){
ll x0=c,x1=b,x2,n0=0LL,n1=1LL,n2,t=a%b,m,ans;
if (t==0LL) return a/b;
for(int i=0;i<900;i++){
m=x0/x1;
x2=x0-x1*m;
n2=(n0-m*n1)%c;
if (x2==1LL){
ans=(n2+c)%c;
break;
}
x0=x1;x1=x2;
n0=n1;n1=n2;
}
return (a+((t*ans)%c)*b-t)/b;
}
//ๅฐไฝใฎๅฒใใ
//aใฎnไน mod c
ll expmod(ll a,ll n,ll c){
ll ans=1LL,aa=a,beki=n;
for(int i=0;i<64;i++){
if (beki%2==1LL) ans=ans*aa%c;
aa=aa*aa%c;
beki/=2LL;
if (beki==0LL)break;
}
return ans;
}
//nCk
vi mulmod(500001);//0!,1!,2!,3!,4!,,,,
void first_nCk(){
mulmod[0]=1LL;
for(ll i=1;i<500001;i++){
mulmod[i]=mulmod[i-1]*i%MOD;
}
}
ll nCk(ll nn,ll kk){
return moddiv(mulmod[nn],mulmod[kk]*mulmod[nn-kk]%MOD,MOD);
}
ll modinv(ll a,ll b){
return moddiv(1,a%b,b);
}
//ๆกไปถgcd(a,b)==1,ar<a,br<b
ll garner(__int128_t a,__int128_t b,__int128_t ar,__int128_t br){
__int128_t tmp=(br-ar)%b*modinv(a,b)%b;
if (tmp<0)tmp+=b;
return ar+tmp*a;
}
ll gcd(ll a,ll b){
ll r = a % b;
while(r!=0LL){
a = b;
b = r;
r = a % b;
}
return b;
}
ll t,x,y,p,q;
int main(){
ll ans=0;
ll t=0;
cin>>t;
for(ll ttt=0;ttt<t;ttt++){
cin >> x >> y>>p>>q;
ll xy=x*2+y*2;
ll pq=p+q;
ans=llMAX;
/*
ll t=gcd(max(x-p,p-x),gcd(xy,pq));
if (t>=y+q){
cout<<"infinity"<<endl;
continue;
}
*/
for(ll i=0;i<y;i++){
for(ll j=0;j<q;j++){
ll tttt=max(x+i-(p+j),(p+j)-(x+i));
ll gd=gcd(xy,pq);
if (tttt%gd!=0)continue;
if (gd!=1){
if (x+i-(p+j)>0){
ll g;
if ((xy/gd==1)|(pq/gd==1)){
g=(x+i-(p+j))/gd;
}else{
g=garner(xy/gd,pq/gd,(x+i-(p+j))/gd,0);
}
g=g*gd;
g+=(p+j);
ans=min(ans,g);
}else{
ll g;
if ((xy/gd==1)|(pq/gd==1)){
g=((p+j)-(x+i))/gd;
}else{
g=garner(xy/gd,pq/gd,0,((p+j)-(x+i))/gd);
}
g=g*gd;
g+=(x+i);
ans=min(ans,g);
}
}else{
ll g=garner(xy,pq,x+i,p+j);
ans=min(ans,g);
}
}
}
if (ans==llMAX){
cout<<"infinity"<<endl;
continue;
}
cout<<ans<<endl;
}
return 0;
}
/*
298 2 98 102
*/ | #include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define IFOR(i, a, b) for (long long i = (a); i <= (long long)(b); i++)
using namespace std;
using VL = vector<long long>;
using ll = int64_t;
ll mod(ll x,ll m){ll y=x%m;return (y>=0LL)?y:y+m;}
template<class T>bool chmin(T& a, T b){if(a>b){a=b;return true;}else return false;}
ll gcd(ll a,ll b,ll& x,ll& y)
{if(a){ll d=gcd(b%a,a,x,y);y-=(b/a)*x;swap(x,y);if(d<0LL){d=-d;x=-x;y=-y;}return d;}else{x=0;y=1;return b;}}
int main(){
int T;
cin>>T;
string ans = "";
REP(i,T){
ll t = LLONG_MAX;
ll X,Y,P,Q;
scanf("%lld %lld %lld %lld",&X,&Y,&P,&Q);
ll a=(X+Y)<<1, b=P+Q;
ll x, y;
ll d = gcd(a, -b, x, y);
ll u = b/d, v = a/d;
ll stop0=P-X, stop1=P-X+Q-1LL;
ll wake0=P-X-Y+1LL, wake1=P-X;
ll from=min(stop0, wake0), to=max(stop1, wake1);
IFOR(z, from, to){
if(mod(z,d)!=0LL)continue;
if(stop0<=z && z<=stop1){
chmin(t,a*mod(z/d*x,u)+X);
}else{
chmin(t, b*mod(z/d*y,v)+P);
}
}
ans += (t==LLONG_MAX ? "infinity" : (to_string(t)))+"\n";
}
cout << ans;
return 0;
} |
#include "iostream"
using namespace std;
//#include "string"
#include "vector"
#include "set"
#include "map"
#include "stack"
#include "algorithm"
#include "queue"
#define int long long
#define pi pair<int,int>
#define ff first
#define ss second
#define boost ios::sync_with_stdio(false);cin.tie(nullptr)
#define endl '\n'
#define vi vector<int>
int32_t main() {
boost;
int n, m;
cin >> n >> m;
vi a(n);
for(int &i : a)
cin >> i;
set<pi> s;
for(int i = 0; i <= n; i++)
s.insert(make_pair(0, i));
vi f(n + 1, 0);
int ans = n;
for(int i = 0; i < n; i++) {
s.erase(make_pair(f[a[i]], a[i]));
f[a[i]]++;
s.insert(make_pair(f[a[i]], a[i]));
if(i >= m - 1) {
ans = min(ans, s.begin() -> ss);
s.erase(make_pair(f[a[i - m + 1]], a[i - m + 1]));
f[a[i - m + 1]]--;
s.insert(make_pair(f[a[i - m + 1]], a[i - m + 1]));
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vi=vector<int>;
using vvi=vector<vi>;
int main() {
int N,M; cin>>N>>M;
vvi pos(N, {-1});
for (int i = 0; i < N; i++){
int A; cin>>A;
pos[A].push_back(i);
}
for (int i = 0; i < N; i++)pos[i].push_back(N);
for (int i = 0; i < N; i++){
for (int j = 0; j < pos[i].size()-1; j++){
if(pos[i][j+1]-pos[i][j]-1>=M){
cout<<i<<endl;
return 0;
}
}
}
cout<<N<<endl;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i=0;i<n;i++)
int main(){
int n,a,b;
double grad;
int cnt = 0;
cin >> n;
vector<double> x,y;
rep(i,n){
scanf("%d", &a);
scanf("%d", &b);
x.push_back(double(a));
y.push_back(double(b));
}
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
grad = (y[i]-y[j])/(x[i]-x[j]);
if(-1 <= grad && grad <= 1){\
cnt++;
}
}
}
cout << cnt << "\n";
}
| // C++ program to count number of ways to express
// N as sum of consecutive numbers.
#include <bits/stdc++.h>
using namespace std;
long long int countConsecutive(long long int N)
{
// constraint on values of L gives us the
// time Complexity as O(N^0.5)
long long int count = 0;
for (long long int L = 1; L * (L + 1) < 2 * N; L++)
{
//long long double a = ;
if ((1.0 * N-(L * (L + 1)) / 2) / (L + 1)-(long long int)((1.0 * N-(L * (L + 1)) / 2) / (L + 1)) == 0.0)
count++;
}
return count;
}
// Driver Code
int main()
{
long long int N;
cin>>N;
cout << 2*countConsecutive(N)+2 << endl;
return 0;
}
|
#include <cstdio>
#include <vector>
#include <queue>
const int N = 2000 + 5;
const int d[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
#define toint(ch) ((int)((ch) - 'a' + 1))
char a[N][N];
int n, m;
int stx, sty, edx, edy;
struct Point { int x, y; };
std::vector<Point> tos[30];
std::queue<Point> q;
int dis[N][N];
bool visch[30];
int main() {
std::scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++) {
std::scanf("%s", a[i] + 1);
for(int j = 1; j <= m; j++) {
if('a' <= a[i][j] && a[i][j] <= 'z')
tos[toint(a[i][j])].push_back({i, j});
else if(a[i][j] == 'S') stx = i, sty = j;
else if(a[i][j] == 'G') edx = i, edy = j;
}
}
q.push({stx, sty});
dis[stx][sty] = 1; // dis[][] is also vis[][]
while(!q.empty()) {
int px = q.front().x, py = q.front().y;
char pc = a[px][py];
q.pop();
if('a' <= pc && pc <= 'z' && !visch[toint(pc)]) {
for(const Point &d : tos[toint(pc)]) {
int dx = d.x, dy = d.y;
if(!(1 <= dx && dx <= n && 1 <= dy && dy <= m && a[dx][dy] != '#' && !dis[dx][dy])) continue;
dis[dx][dy] = dis[px][py] + 1;
q.push({dx, dy});
}
visch[toint(pc)] = true;
}
for(int i = 0; i < 4; i++) {
int dx = px + d[i][0], dy = py + d[i][1];
if(!(1 <= dx && dx <= n && 1 <= dy && dy <= m && a[dx][dy] != '#' && !dis[dx][dy])) continue;
dis[dx][dy] = dis[px][py] + 1;
q.push({dx, dy});
}
}
// for(int i = 1; i <= n; i++)
// for(int j = 1; j <= m; j++)
// std::printf("%2d%c", dis[i][j] - 1, j == m ? '\n' : ' ');
std::printf("%d\n", (dis[edx][edy] == 0 ? -1 : dis[edx][edy] - 1));
return 0;
} | #include<iostream>
#include<vector>
#include<deque>
using namespace std;
template<typename T>class Graph{
public:
struct edge{int to;T cost;};
int Vertex_num,Edge_num;
vector<edge>*g;
const T INF=1000000000;
Graph(int V){//0๏ฝV-1
Vertex_num=V;
g=new vector<edge>[V];
}
Graph(int V,int E){//0๏ฝV-1
Vertex_num=V;
Edge_num=E;
g=new vector<edge>[V];
}
void emplace_back(int from,int to,T cost){
edge e={to,cost};
g[from].push_back(e);
//edges->push_back(e);//for Bellman-Ford
}
void input(bool direct,bool weight){
int from,to;
T cost;
for(int i=0;i<Edge_num;i++){
cin>>from>>to;
from--,to--;
if(weight)cin>>cost;
else cost=1;
this->emplace_back(from,to,cost);
if(!direct)this->emplace_back(to,from,cost);
}
}
void check(){
for(int i=0;i<Vertex_num;i++){
cout<<"Vertex "<<i<<endl;
for(auto x:g[i])cout<<x.to<<" "<<x.cost<<endl;
}
}
};
template<typename T>vector<T>bfs(Graph<T>graph,int start){
vector<T>dist(graph.Vertex_num,-1);//่จชๅ:startใใใฎ่ท้ข ๆช่จชๅ:-1
deque<int>q;
q.push_front(start);
dist[start]=0;
while(!q.empty()){
int p=q.front();q.pop_front();
for(auto x:graph.g[p])if(dist[x.to]==-1){
dist[x.to]=dist[p]+x.cost;
if(x.cost)q.push_back(x.to);
else q.push_front(x.to);
}
//visited[]ใฎๆดๆฐใฏpushใใใจใใซใใใชใใจqueueใใใตใใโ
}
return dist;
}
int H,W,S,G;
int main(){
cin>>H>>W;
char a[H][W];
for(int i=0;i<H*W;i++)cin>>a[i/W][i%W];
int d4[5]={1,0,-1,0,1};
Graph<int>g(H*W+26);
for(int i=0;i<H;i++){
for(int j=0;j<W;j++){
if(a[i][j]=='#')continue;
if(a[i][j]=='S')S=i*W+j;
if(a[i][j]=='G')G=i*W+j;
if('a'<=a[i][j]&&a[i][j]<='z'){
g.emplace_back(i*W+j,H*W+a[i][j]-'a',1);
g.emplace_back(H*W+a[i][j]-'a',i*W+j,0);
g.Edge_num+=2;
}
for(int k=0;k<4;k++){
int x=i+d4[k],y=j+d4[k+1];
if (x<0||x>=H||y<0||y>=W||a[x][y]=='#')continue;
g.emplace_back(i*W+j,x*W+y,1);
g.Edge_num++;
}
}
}
auto d=bfs(g,S);
cout<<d[G]<<endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define LL long long
#define ULL unsigned long long
const LL INF = 1LL<<62;
const double PI = 2.0 * acos(0.0);
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
typedef vector<int> vi;
typedef vector<LL> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;
////// Including Policy Based DS
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//
/////cout<<*X.find_by_order(1)<<endl;
/////cout<<X.order_of_key(-5)<<endl;
//
//typedef tree<
//LL,
//null_type,
//less< pll >,
//rb_tree_tag,
//tree_order_statistics_node_update>
//ordered_set;
#define READ freopen("in.txt","r",stdin)
#define WRITE freopen("out.txt","w",stdout)
#define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define sf scanf
#define pf printf
#define F first
#define S second
#define pb push_back
#define NL pf("\n")
#define ALL(v) v.begin(),v.end()
#define si(x) sf("%d",&x);
#define sii(x,y) sf("%d%d",&x,&y);
#define siii(x,y,z) sf("%d%d%d",&x,&y,&z);
#define sl(x) sf("%lld",&x);
#define sll(x,y) sf("%lld%lld",&x,&y);
#define slll(x,y,z) sf("%lld%lld%lld",&x,&y,&z);
#define sqr(x) ( (x) * (x) )
#define mem(x,y) memset(x,y,sizeof x )
#define ch printf("Came Here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n")
#define deb(x) cerr<<#x<<" :: "<<x<<"\n"
#define SV(v) sort(v.begin(),v.end())
#define SVR(v,cmp) sort(v.begin(),v.end(),cmp)
#define FOR(i,n) for(int i=0;i<n;i++)
#define cnd tree[idx]
#define lc tree[idx*2]
#define rc tree[idx*2+1]
#define lnd (2*idx),(b),( (b+e) /2 )
#define rnd ((2*idx)+1),(((b+e)/2)+1),(e)
// graph direction array [4]
int X[4]={0,0,-1,1};
int Y[4]={1,-1,0,0};
// graph direction array [8]
//int X[8]={0,0,1,-1,-1,1,1,-1};
//int Y[8]={1,-1,0,0,-1,-1,1,1};
// Bishop direction array [8]
//int BX[8]={0,0,1,-1,-1,1,1,-1};
//int BY[8]={1,-1,0,0,-1,-1,1,1};
// Knight Direction Array
//int KX[8] = {1,1,2,2,-1,-1,-2,-2};
//int KY[8] = {2,-2,1,-1,2,-2,1,-1};
// Bit Operations
#define popcountL __builtin_popcountll
#define popcount __builtin_popcount
inline bool checkBit(int N,int pos){return (bool)(N & (1<<pos));} // check bit
inline int setBit(int N,int pos){ return N=N | (1<<pos);}// set bit
inline int unsetBit(int N,int pos){ return N=( N & ( ~( 1<<pos ) ) );}// unset bit
inline int toggleBit(int N,int pos){ return N = ( N ^(1<<pos) ); }// toggle bit
inline LL modMul(LL a, LL b,LL mod){
LL ans = 0;
a = a % mod;
while (b > 0){
if ( b % 2 )ans = (ans%mod+ a%mod) % mod;
a = (a%mod * 2%mod) % mod;
b /= 2;
}
return ans % mod;
}
inline LL powerMod(LL a,LL b,LL mod){
if(b==0)return 1LL%mod;
LL x=powerMod( a,b/2,mod );
x = ( x%mod * x%mod ) % mod;
if( b%2==1 )x = ( x%mod * a%mod ) % mod;
return x%mod;
}
inline LL lcm( LL a,LL b ){
LL g = __gcd(a,b);
return ((a/g) * b);
}
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
const int mx = 2234567;
long double P[mx];
void solve(){
int n;
cin>>n;
P[1] = 0.0;
for(int i=2;i<=n;i++){
long double cur = (long double)(1.0*n) / (long double)(n-i+1);
P[i] = P[i-1] + cur;
}
cout<<fixed<<setprecision(12);
cout<<P[n]<<endl;
}
int main(){
BOOST;
#ifdef MujahidPC
double start = clock();
// READ;
// WRITE;
#endif
int t;
t = 1;
// cin>>t;
while(t--)solve();
#ifdef MujahidPC
double tim = (clock() - start)/CLOCKS_PER_SEC;
cerr<<"Running Time : "<<tim<<" \n";
#endif
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------
template<class V, int ME> class BIT {
public:
V bit[1<<ME];
V operator()(int e) {if(e<0) return 0;V s=0;e++;while(e) s^=bit[e-1],e-=e&-e; return s;}
void add(int e,V v) { e++; while(e<=1<<ME) bit[e-1]^=v,e+=e&-e;}
};
BIT<int,20> bt;
int N,Q;
void solve() {
int i,j,k,l,r,x,y; string s;
cin>>N>>Q;
FOR(i,N) {
cin>>x;
bt.add(i+1,x);
}
while(Q--) {
cin>>i>>x>>y;
if(i==1) {
bt.add(x,y);
}
else {
cout<<(bt(y)^bt(x-1))<<endl;
}
}
}
int main(int argc,char** argv){
string s;int i;
if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
cout.tie(0); solve(); return 0;
}
|
#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
int n;
int main(void){
scanf("%d",&n);
printf("%d\n",n-1);
return 0;
}
| #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
//#pragma GCC optimize("O3")
using namespace std;
using namespace __gnu_pbds;
auto random_address = [] { char *p = new char; delete p; return uint64_t(p); };
const uint64_t SEED = chrono::steady_clock::now().time_since_epoch().count() * (random_address() | 1);
mt19937_64 rng(SEED);
typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll n,nr;
ll f[1001];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
ll rest=n%3;
if(rest==0)
{
cout<<0;
return 0;
}
while(n)
{
ll cif=n%10;
f[cif%3]++;
n/=10;
nr++;
}
if(rest==1)
{
if(f[1]>=1&&nr>1)
{
cout<<1;
return 0;
}
else if(f[2]>=2&&nr>2)
{
cout<<2;
return 0;
}
else
{
cout<<-1;
return 0;
}
}
if(rest==2)
{
if(f[2]>=1&&nr>1)
{
cout<<1;
return 0;
}
else if(f[1]>=2&&nr>2)
{
cout<<2;
return 0;
}
else
{
cout<<-1;
return 0;
}
}
return 0;
}
|
#include <bits/stdc++.h>
// #include <atcoder/all>
// #include "icld.cpp"
using namespace std;
using ll = long long int;
using vi = vector<int>;
using si = set<int>;
using vll = vector<ll>;
using vvi = vector<vector<int>>;
using ss = string;
using db = double;
template<typename T> using minpq = priority_queue <T,vector<T>,greater<T>>;
const int dx[4] = {1,0,-1,0};
const int dy[4] = {0,1,0,-1};
#define V vector
#define P pair<int,int>
#define PLL pair<ll,ll>
#define rep(i,s,n) for(int i=(s);i<(int)(n);i++)
#define rev(i,s,n) for(int i=(s);i>=(int)(n);i--)
#define reciv(v,n) vll (v)((n)); rep(i,0,(n))cin>>v[i]
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define ci(x) cin >> x
#define cii(x) ll x;cin >> x
#define cci(x,y) ll x,y;cin >> x >> y
#define co(x) cout << x << endl
#define pb push_back
#define eb emplace_back
#define rz resize
#define pu push
#define sz(x) int(x.size())
#define vij v[i][j]
ll p = 1e9+7;
// ll p = 998244353;
// n do -> n*pi/180
#define yn cout<<"Yes"<<endl;else cout<<"No"<<endl
#define YN cout<<"YES"<<endl;else cout<<"NO"<<endl
template<class T>void chmax(T &x,T y){x=max(x,y);}
template<class T>void chmin(T &x,T y){x=min(x,y);}
ll f(ll x,ll n){
if(n==0)return 1;
if(n==1)return x;
if(n%2)return x*f(x,n-1)%p;
ll r=f(x,n/2);
return r*r%p;
}
int main(){
cci(n,k);
ll ans=(k-1)*f(k-2,n-1)%p;
co(ans);
} | //"IN THE NAME OF ALLAH"//
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int,int> pii;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
// --------------------------------------------------------------------------------------------------------------->
#define el << '\n'
#define pb push_back
#define PI 3.141592653589793238
#define _test_ int _ ; cin >> _ ; while( _-- )
#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define file() freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout)
// --------------------------------------------------------------------------------------------------------------->
int dx[]={0,0,+1,-1,+1,-1,-1,+1};
int dy[]={+1,-1,0,0,+1,-1,+1,-1};
const ll infLL = 1e17+123;
const ll inf = 1e16+7;
const ll mod = 1e9+7;
const ll mx = 1e6+123;
// --------------------------------------------------------------------------------------------------------------->
ll n,a[mx],Q[mx];
void _case_()
{
cin >> n;
for(int i=1;i<=n;i++)
{
cin >> a[i];
}
sort(a+1,a+n+1);
ll ans=1 ;
for(int i=1;i<=n;i++)
{
Q[i]=(a[i]-a[i-1]+1);
}
for(int i=1;i<=n;i++)
{
ans=ans*Q[i];
ans%=mod;
}
cout << ans el;
}
int main()
{
optimize();
//_test_
_case_();
return 0;
}
// _๐ท๐ธ๐ธ๐ซ๐ถ๐ช๐ผ๐ฝ๐ฎ๐ป_ |
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define FOR(i,n,m) for(ll i=(ll)(n); i<=(ll)(m); i++)
#define RFOR(i,n,m) for(int i=(int)(n); i>=(int)(m); i--)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define RITR(x,c) for(__typeof(c.rbegin()) x=c.rbegin();x!=c.rend();x++)
#define setp(n) fixed << setprecision(n)
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll,ll>
#define pi pair<int,int>
#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second
#define pb push_back
#define ins insert
#define debug(a) cerr<<(a)<<endl
#define dbrep(a,n) rep(_i,n) cerr<<(a[_i])<<" "; cerr<<endl
#define dbrep2(a,n,m) rep(_i,n){rep(_j,m) cerr<<(a[_i][_j])<<" "; cerr<<endl;}
using namespace std;
template<class A, class B>
ostream &operator<<(ostream &os, const pair<A,B> &p){return os<<"("<<p.fi<<","<<p.se<<")";}
template<class A, class B>
istream &operator>>(istream &is, pair<A,B> &p){return is>>p.fi>>p.se;}
/* Some Libraries */
//-------------------------------------------------
const __int128_t geta = (__int128_t)100000000000LL * 100000000000LL;
int main(void)
{
string Xs,Ys,Rs; cin>>Xs>>Ys>>Rs;
__int128_t X,Y,R;
{
bool flg=false;
ll m=10000;
string tmp;
for(auto c:Xs){
if (flg) m/=10;
if (c!='.') tmp.pb(c);
if (c=='.') flg=true;
}
X = stoll(tmp)*m;
}
{
bool flg=false;
ll m=10000;
string tmp;
for(auto c:Ys){
if (flg) m/=10;
if (c!='.') tmp.pb(c);
if (c=='.') flg=true;
}
Y = stoll(tmp)*m;
}
{
bool flg=false;
ll m=10000;
string tmp;
for(auto c:Rs){
if (flg) m/=10;
if (c!='.') tmp.pb(c);
if (c=='.') flg=true;
}
R = stoll(tmp)*m;
}
ll ans=0LL;
__int128_t xl = (X-R)/10000LL - 10;
__int128_t xr = (X+R)/10000LL + 10;
FOR(i,xl,xr){
__int128_t x = (__int128_t)10000LL * i;
if (x<X-R || X+R<x) continue;
__int128_t top,bottom;
{
__int128_t lo=Y, hi=Y+R+100;
while(hi-lo>1){
__int128_t c = (lo+hi)/2;
__int128_t dx = x-X;
__int128_t dy = c-Y;
if (dx*dx+dy*dy<=R*R){
lo = c;
}else{
hi = c;
}
}
top = lo;
}
{
__int128_t lo=Y-R-100, hi=Y;
while(hi-lo>1){
__int128_t c = (lo+hi)/2;
__int128_t dx = x-X;
__int128_t dy = c-Y;
if (dx*dx+dy*dy<=R*R){
hi = c;
}else{
lo = c;
}
}
bottom = hi;
}
top += geta;
bottom += geta;
ans += (ll)(top/10000LL - (bottom-1)/10000LL);
}
cout<<ans<<"\n";
return 0;
}
| #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<deque>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include<string>
#include<chrono>
#include<random>
#include<unordered_map>
#include<ctype.h>
#include<cassert>
using namespace std;
typedef long long ll; typedef pair<ll, ll> pll; typedef pair<int, int> pii; typedef unsigned long long ull; typedef long double ldb; typedef vector<long long> vll; typedef vector<int> vi; typedef pair<ll, ldb>pllldb; typedef double db;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(),(a).end()
const double Pi = acos(-1.0); const int inf = 0x3f3f3f3f, Mod = 1e9 + 7, N = 2e3+1;
ll qpow(ll a, ll b, ll mod = Mod)
{
ll ans = 1;
while (b)
{
if (b & 1)ans = ans * a % mod;
a = a * a % mod;
b >>= 1;
}
return ans;
}
ll gcd(ll a, ll b) { return (b ? gcd(b, a % b) : a); }
bool cmp(pii a, pii b)
{
return a.second - a.first > b.second - b.first;
}
/*----------------------------------------------------------------------Code follows-----------------------------------------------------------------------*/
/*----------------------------------------------------------------------Code follows-----------------------------------------------------------------------*/
ldb x, y, r;ll ans;
int main()
{
ios::sync_with_stdio(0); cin.tie(0), cout.tie(0);
cin >> x >> y >> r;
r = nextafter(r, INFINITY);
for (int i = ceil(x - r); i <= floor(x + r); i++)
{
ldb t = sqrt(pow(r, 2) - pow(i - x,2));
ans += floor(y + t) - ceil(y - t) + 1;
}
cout << ans << '\n';
}
/*
*/ |
#include<stdio.h>
int main()
{
int x;
scanf("%d",&x);
if (x>=0)
{ printf("%d",x);}
if (x<0)
{printf("0");}
return 0;
}
| #include <bits/stdc++.h>
#define log(x) cout<<x<<endl
#define pb push_back
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
#define uni(x) (x).erase(unique(x.begin(),x.end()),x.end())
typedef long long lint;
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const long long INF = 1LL << 60;
using Graph=vector<vector<int>>;
int gcd(lint x, lint y){
return y==0 ? x:gcd(y,x % y);
}
int digsum(int n) {
int res = 0;
while(n > 0) {
res += n%10;
n /= 10;
}
return res;
}
int dignum(int n) {
int res = 0;
while(n > 0) {
res++;
n /= 10;
}
return res;
}
int stringcount(string s, char c) {
return count(s.cbegin(), s.cend(), c);
}
int lcm(lint x, lint y){
return x/ gcd(x, y) * y;
}
int combi(int n, int r){
int num = 1;
for(int i = 1; i <= r; i++){
num = num * (n - i + 1) / i;
}
return num;
}
vector<lint> divisor(lint n){
vector<lint> ret;
for(lint i=1;i*i<=n;i++){
if(n%i==0){
ret.pb(i);
if(i*i!=n) ret.pb(n/i);
}
}
sort(all(ret));
return ret;
}
int main(){
int n;
cin>>n;
if(n>=0) cout<<n;
else cout<<"0";
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
int64_t C, M, T;
cin >> C >> M >> T;
int64_t time = 0;
int64_t cap = C;
for (int i = 0; i < M; ++i) {
int64_t start, end;
cin >> start >> end;
cap -= (start - time);
if (cap <= 0) {
cout << "No" << endl;
return 0;
}
cap += (end - start);
cap = min(cap, C);
time = end;
}
cap -= (T - time);
if (cap <= 0) {
cout << "No" << endl;
} else {
cout << "Yes" << endl;
}
} | #include<iostream>
#include<cstring>
#include<algorithm>
#include<unordered_map>
using namespace std;
typedef long long LL;
const int N=3e3+10;
struct Node{
int x,v;
bool operator<(const Node& a)const{
return x<a.x;
}
}node[N<<1];
int main(){
int n,m,t;
cin>>n>>m>>t;
int cnt=0;
for(int i=1;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
node[++cnt]={x,2};
node[++cnt]={y,-2};
}
node[++cnt]={0,0};
node[++cnt]={t,0};
sort(node+1,node+1+cnt);
int res=-1,ans=n;
for(int i=1;i<=cnt;i++){
res+=node[i].v;
if(node[i].x!=node[i+1].x){
ans+=(node[i+1].x-node[i].x)*res;
ans=min(n,ans);
if(ans<=0)return puts("No"),0;
}
}
return puts("Yes"),0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T L, T R) { while (L != R) cerr << *L << " \n"[next(L) == R], ++L; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
const int MAX_N = 300010;
int32_t main() {
ios_base::sync_with_stdio(0), cin.tie(0);
ll N;
cin >> N;
ll res = N;
for (int b = 0;1ll << b <= N;++b) {
ll c = N % (1ll<<b);
ll a = N / (1ll<<b);
chmin(res, a + b + c);
}
cout << res << '\n';
}
|
//================code===================//
//#define TLE
#ifdef TLE
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#endif
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <ctime>
#define ci(t) cin>>t
#define co(t) cout<<t
#define LL long long
#define ld double
#define fa(i,a,b) for(LL i=(a);i<(LL)(b);++i)
#define fd(i,a,b) for(LL i=(a);i>(LL)(b);--i)
#define setp tuple<LL,LL,LL>
#define setl pair<LL,LL>
#define micro 0.000001
using namespace std;
LL gcd(LL a, LL b) { return a % b ? gcd(b, a % b) : b; }
#ifdef OHSOLUTION
#define ce(t) cerr<<t
#define AT cerr << "\n=================ANS=================\n"
#define AE cerr << "\n=====================================\n"
#define DB(a) cerr << __LINE__ << ": " << #a << " = " << (a) << endl;
#define __builtin_popcount __popcnt
#define __builtin_popcountll __popcnt64
#else
#define AT
#define AE
#define ce(t)
#endif
pair <int, int> vu[9] = { {0,-1},{1,0},{0,1} ,{-1,0},{0,0},{1,1}, {-1,1} , {1,-1},{-1,-1} }; //RDLU EWSN
template<typename T, typename U> void ckmax(T& a, U b) { a = a < b ? b : a; }
template<typename T, typename U> void ckmin(T& a, U b) { a = a > b ? b : a; }
struct gcmp { bool operator()(LL a, LL b) { return a < b; } bool operator()(setl& a, setl& b) { return a.second < b.second; } };
struct lcmp { bool operator()(LL a, LL b) { return a > b; } bool operator()(setl& a, setl& b) { return a.second > b.second; } };
const int max_v = 2e5+ 7;
const int max_k = 1e1 + 7;
const int INF = 1e9 + 7;
const LL LNF = (LL)5e18 + 7ll;
LL mod = 998244353;
template<typename T, typename U> void MOD(T& a, U b) { a += b; if (a >= mod) a -= mod; }
bool open[max_v],vist[max_v];
vector<int> adj[max_v];
int sn = 0;
void dfs(int u)
{
open[u] = 1;
for (auto& v : adj[u]) if (!vist[v])
{
if (open[v])
{
++sn;
open[0] = 0;
return;
}
dfs(v);
}
open[u] = 0;
}
void dfs2(int u)
{
vist[u] = 1;
for (auto& v : adj[u]) if (!vist[v])
{
dfs2(v);
}
}
int main()
{
#ifdef OHSOLUTION
freopen("input.txt", "r", stdin);
#endif
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n; ci(n);
fa(i, 0, n)
{
int x; ci(x), --x;
adj[i].push_back(x);
}
fa(i, 0, n) if (!vist[i]) dfs(i),dfs2(i);
LL base = 2;
LL ret = 1;
while (sn)
{
if (sn & 1) ret = ret * base % mod;
base = base * base % mod;
sn >>= 1;
}
if (ret) --ret;
else ret = mod - 1;
co(ret << "\n");
return 0;
}
|
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
#define rep(i,cc,n) for(int i=cc;i<n;++i)
#define lrep(i,cc,n) for(long long i=cc;i<n;++i)
#define sqrep(i,cc,n) for(long long i=cc;i*i<=n;++i)
#define rrep(i,cc,n) for(long i=cc;i>=n;--i)
#define pii pair<int, int>
#define pll pair<long long, long long>
using ll = long long;
const vector<int> dx = {1, 1, 1, 0, 0, 0, -1, -1, -1};
const vector<int> dy = {1, 0, -1, 1, -1, 0, 1, 0, -1};
const double PI = 3.1415926535;
const ll inf = 1001001001;
const ll e9 = 1000000000;
const ll mod = 1000000007;
const int MAX = 1000000;
const int MOD = 1000000007;
ll gcd(ll x, ll y) { return (x % y)? gcd(y, x % y): y; }
int main(){
int n;
ll ans = 0;
cin >> n;
rep(i, 0, n){
ll a, b;
cin >> a >> b;
ans += (a+b)*(b-a+1)/2;
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for (int i = 0; i < (int)(n) ; i++)
int main() {
int n; cin>>n;
vector<ll> a(n), ac(n), mx(n);
rep(i, n) {
cin >> a[i];
if(i==0) {
ac[i]=a[i];
mx[i]=ac[i];
}
else {
ac[i]+=ac[i-1]+a[i];
mx[i]=max(mx[i-1], ac[i]);
}
}
ll co=0, comx=0;
rep(i, n) {
comx=max(comx, co+mx[i]);
co += ac[i];
comx=max(comx, co);
}
cout << comx << endl;
} |
#include<bits/stdc++.h>
using ll = int_fast64_t;
int main(){
int n;
scanf("%d", &n);
char s[n+1], t[n+1];
scanf("%s %s", s, t);
ll ans = 0;
int dis = 0;
while(dis<n && t[dis]!='1') dis++;
int prev = -1;
for(int i=0; i<n; i++){
if(s[i]=='1') {
if(prev>=0){
s[prev] = s[i]= '0';
ans += i-prev;
prev = -1;
continue;
}
if(dis>i) prev = i;
else{
s[i] = '0';
s[dis] = '1';
ans += i-dis;
dis++;
while(dis<n && t[dis]!='1') dis++;
}
}
}
bool ok = true;
for(int i=0; i<n; i++){
if(s[i]!=t[i]) ok = false;
}
if(ok) printf("%ld\n", ans);
else puts("-1");
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl
using ll = long long;
const int maxn = 5e5 + 10;
int n;
string s, t;
const int inf = 1e9;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>>n;
cin>>s;
cin>>t;
ll ans = 0;
for (int i=0,j=-1; i<n; i++) {
if (s[i]!=t[i]) {
j=max(j,i+1);
while (j<n && s[j]!='1') {
s[j]='0';
j++;
}
if (j==n) out(-1);
ans += ll(j-i);
s[j]=(1^s[j]);
j++;
s[i]=(1^s[i]);
}
// cout<<i<<": "<<j<<endl;
// watch(s);
}
out(ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
// clang-format off
// #include <atcoder/all>
// using namespace atcoder;
// using mint = modint1000000007;
// using mint = modint998244353
using ll = int64_t;
template <class T>
istream& operator>>(istream& is, vector<T>& v) {
for (auto& a : v) cin >> a;
return is;
}
template <class T>
istream& operator>>(istream& is, vector<pair<T, T>>& v) {
for (auto& a : v) cin >> a.first >> a.second;
return is;
}
template <class T>
istream& operator>>(istream& is, vector<tuple<T, T, T>>& v) {
for (auto& a : v) {
T a1, a2, a3;
cin >> a1 >> a2 >> a3;
a = {a1, a2, a3};
}
return is;
}
template<typename T>
istream& operator>>(istream &is, complex<T>& c) {
double x, y;
is >> x >> y;
c.real(x);
c.imag(y);
return is;
}
template <class T>
ostream& operator<<(ostream& os, vector<T>& v) {
for (auto& a : v) os << a << " ";
os << endl;
return os;
}
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;
}
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
#define TRC1(a) cout << #a ":" << a
#define TRC2(a, b) TRC1(a); cout << " "; TRC1(b)
#define TRC3(a, b, c) TRC2(a, b); cout << " "; TRC1(c)
#define TRC4(a, b, c, d) TRC3(a, b, c); cout << " "; TRC1(d)
#define GET_NAME(_1,_2,_3,_4,NAME,...) NAME
#define TRC(...) GET_NAME(__VA_ARGS__, TRC4, TRC3, TRC2, TRC1) (__VA_ARGS__) << endl
using veci = vector<int>;
using vecll = vector<ll>;
using Pi = pair<int, int>;
using Ti = tuple<int, int, int>;
#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<int(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
const int IINF = INT32_MAX;
const ll LINF = INT64_MAX;
// cout << fixed << setprecision(15);
void solve();
int main() {
cin.tie(0); ios::sync_with_stdio(0);
cout << fixed << setprecision(15); solve(); return 0; }
// clang-format on
void solve() {
int N, M, Q;
cin >> N >> M >> Q;
veci W(N), V(N), X(M);
rep(i, N) cin >> W[i] >> V[i];
cin >> X;
rep(qi, Q) {
int l, r;
cin >> l >> r;
--l, --r;
veci boxes;
rep(i, M) {
if (i < l || i > r) boxes.push_back(X[i]);
}
sort(all(boxes));
ll ans = 0;
vector<bool> used(N);
for (auto x : boxes) {
Pi best(-1, -1);
rep(i, N) {
if (used[i] || x < W[i]) continue;
best = max(best, Pi(V[i], i));
}
auto [v, i] = best;
if (i == -1) continue;
used[i] = true;
ans += v;
}
cout << ans << endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n;
cin >> n;
int mini = 2e9;
for(int i=0;i<n;i++){
int a,b,c;
cin >> a >> b >> c;
if(a < c){
mini = min(mini,b);
}
}
cout << (mini < 2e9 ? mini:-1);
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
// --------------------------------------------------------
template<class T> bool chmax(T& a, const T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> bool chmin(T& a, const T b) { if (b < a) { a = b; return 1; } return 0; }
#define FOR(i,l,r) for (int i = (l); i < (r); ++i)
#define RFOR(i,l,r) for (int i = (r)-1; (l) <= i; --i)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define MIN(c) *min_element(ALL(c))
#define MAX(c) *max_element(ALL(c))
#define SUM(c) accumulate(ALL(c), 0)
#define SUMLL(c) accumulate(ALL(c), 0LL)
#define SZ(c) ((int)(c).size())
#define CIN(c) cin >> (c)
#define COUT(c) cout << (c) << '\n'
#define debug(x) cerr << #x << " = " << (x) << '\n';
using PP = pair<int,int>;
using VP = vector<PP>;
// using VVP = vector<VP>;
using VS = vector<string>;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<ll>;
using VVLL = vector<VLL>;
using VB = vector<bool>;
using VVB = vector<VB>;
using VD = vector<double>;
using VVD = vector<VD>;
static const double EPS = 1e-10;
static const double PI = acos(-1.0);
static const ll MOD = 1000000007;
// static const ll MOD = 998244353;
static const int INF = (1 << 30) - 1; // 1073741824 - 1
// static const ll INF = (1LL << 62) - 1; // 4611686018427387904 - 1
// --------------------------------------------------------
// #include <atcoder/all>
// using namespace atcoder;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(15);
int N; cin >> N;
VI P(N+1); FOR(i,1,N+1) cin >> P[i];
VP memo;
ll L = 0;
ll R = 0;
FOR(i,1,N+1) {
if (P[i] < i) {
L += (ll)(i - P[i]);
memo.push_back(PP(i-1, (i - P[i])));
} else if (i < P[i]) {
R += (ll)(P[i] - i);
}
}
if (L != R || L != N-1) {
COUT(-1); return 0;
}
VI ans(N-1);
int i = 0;
for (auto p : memo) {
int x = p.first;
int n = p.second;
REP(_,n) {
ans[i++] = x--;
}
}
VI check(ans);
SORT(check);
REP(i,N-1) {
if (i+1 != check[i]) {
COUT(-1);
return 0;
}
}
REP(i,N-1) {
swap(P[ans[i]], P[ans[i]+1]);
}
FOR(i,1,N+1) {
if (i != P[i]) {
COUT(-1);
return 0;
}
}
for (int x : ans) {
cout << x << '\n';
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
using ll=long long;
#define rep(i,n) for(int i=0;i<n;i++)
#define Rep(i,n) for(int i=0;i<=n;i++)
int main(){
int n;
cin>>n;
vector<int> p(n);
for(int i=0;i<n;i++){
cin>>p.at(i);
}
//๏ผ๏ฝ๏ฝใพใงใฎๆฐๅญใใฉใฎไฝ็ฝฎใซใใใใ่จ้ฒใใพใ
vector<int> position(n+1);
for(int i=0;i<n;i++){
position.at(p.at(i))=i;
}
//ๅใใใ้ ็ชใ่จ้ฒใใใใใงใ
vector<int> ans(n-1,0);
//ใฉใฎไฝ็ฝฎใswapใใใ่จ้ฒใใใใใซไฝฟใใพใ
vector<bool> ok(n,false);
int j=0;
//ๅคงใใ้ ใซๅณใซๆใฃใฆใใใพใ
for(int i=n;0<i;i--){
int b=position.at(i);
//่จ้ฒใใไฝ็ฝฎใใๅณใซๆใฃใฆ่กใฃใฆๅฐใใ้ ใซไธฆใใ ๆใฎๆญฃใใไฝ็ฝฎใซใชใใพใง็งปๅใใใพใ
while(p.at(i-1)!=i){
if(ok.at(b)){
cout<<-1<<endl;
return 0;
}
//ใใใใ
if(b+1>=n){
cout<<-1<<endl;
return 0;
}
//ใใใพใงใ็กใใจ๏ผฒ๏ผฅใใพใ
//ใwhileใงๅผพใใฆใใจๆใฃใฆใใใงใใใฉใใใกใฟใใใงใ
swap(p.at(b),p.at(b+1));
//swapใใๆฐๅญใฎไฝ็ฝฎใๅ
ฅใๆฟใใพใ
position.at(p.at(b+1))--;
position.at(p.at(b))++;
//ไฝฟใฃใๅ ดๆใ่จ้ฒใใฆใใใพใ
ans.at(j)=b+1;
j++;
ok.at(b)=true;
b++;
if(b==n-1)break;
}
}
//ๅ
จใฆใฎๅ ดๆใไฝฟใใใฆใใใใใงใใฏใใพใ
for(int i=0;i<n-1;i++){
if(ok.at(i)==false){
cout<<-1<<endl;
return 0;
}
}
for(int i=0;i<n-1;i++){
cout<<ans.at(i)<<endl;
}
}
|
//GOD PLEASE HELP ME ,GOD PLEASE HELP ME , GOD PLEASE HELP ME
//Name Rishiraj Kalita
// sc id : 1912041
#include<bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(i,n) for(i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define ll long long
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define ss(s) scanf("%s",s)
#define pi(x) printf("%d\n",x)
#define pl(x) printf("%lld\n",x)
#define ps(s) printf("%s\n",s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define deb3(x, y, z) cout << #x << "=" << x << "," << #y << "=" << y <<","<< #z << "=" << z << endl
#define debv(v) for(auto x:v) cout<<x<<" "
#define debg(g,n) for(int i=1;i<=n;i++){cout<<i<<"--->";for(auto x:g[i]){cout<<x<<" ";}cout<<"\n";}
#define clrg(g,n) for(int i=0;i<=n;i++) g[i].clear();
#define pb push_back
#define mp make_pair
#define e endl
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define inf 9000000000000000000
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) {
uniform_int_distribution<int> uid(0,lim-1);
return uid(rang);
}
int mpow(int base, int exp);
void ipgraph(int n, int m);
void dfs(int u, int par);
const int mod = 1000000007;
const int N = 3e5, M = N;
//=======================
vi g[N];
int a[N];
vpl v;
void solve() {
ll i, j, n,k,data,m,f=1;
cin>>n;
//deb(n);
unsigned ll ans=inf;
data= log(n)/log(2);
for(i=0;i<=data+10;i++)
{
unsigned ll x=pow(2,i);
unsigned ll y=n/x;
unsigned ll z=n-(x*y);
//deb3(i,y,z);
ans=min(ans,i+y+z);
if(y==0)
{
break;
}
}
cout<<ans<<"\n";
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
srand(chrono::high_resolution_clock::now().time_since_epoch().count());
int t = 1;
//cin >> t;
while(t--) {
solve();
}
return 0;
}
int mpow(int base, int exp) {
base %= mod;
int result = 1;
while (exp > 0) {
if (exp & 1) result = ((ll)result * base) % mod;
base = ((ll)base * base) % mod;
exp >>= 1;
}
return result;
}
void ipgraph(int n, int m){
int i, u, v;
while(m--){
cin>>u>>v;
// u--, v--;
g[u].pb(v);
g[v].pb(u);
}
}
void dfs(int u, int par){
for(int v:g[u]){
if (v == par) continue;
dfs(v, u);
}
}
| #include <bits/stdc++.h>
using namespace std;
struct cake {
int a;
int b;
int c;
};
int fact(int n) {
if (n <= 0) {
return 1;
}
return n * fact(n-1);
}
void solve() {
long long n, m;
cin >> n;
cin >> m;
/*
vector<cake> v;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
for (int k = 1; k <= n; k++) {
cake c;
c.a = i;
c.b = j;
c.c = k;
v.push_back(c);
}
}
}
sort(v.begin(), v.end(), [](const cake & x, const cake & y) {
if (x.a + x.b + x.c == y.a + y.b + y.c) {
if (x.a == y.a) {
return x.b < y.b;
}
return x.a < y.a;
}
return x.a + x.b + x.c < y.a + y.b + y.c;
});
map<int, int> dupa;
for (cake & c : v) {
dupa[c.a+c.b+c.c]++;
//cout << c.a+c.b+c.c << ": " << c.a << " " << c.b << " " << c.c << endl;
}
*/
for (long long k = 0; k <= 3*n-3; k++) {
long long sum = k+3;
long long f;
if (k < n) {
f = (k+2) * (k+1) / 2;
} else if (k < 2*n) {
f = (9*n-3*n*n+6*k*n-6*k-2*k*k-4)/2;
} else {
f = (3*n-k-1) * (3*n-k-2) / 2;
}
//cout << sum << " " << dupa[sum] << " " << f << endl;
if (m <= f) {
for (long long a = 1; a <= n; a++) {
long long rest = sum - a;
if (rest >= 2 && rest <= 2 * n) {
long long mn = max(1LL, rest - n);
long long mx = min(rest - 1, n);
long long cnt = mx - mn + 1;
if (m <= cnt) {
for (long long b = 1; b <= n; b++) {
long long c = rest - b;
if (c >= 1 && c <= n) {
m--;
if (m == 0) {
cout << a << " " << b << " " << c << endl;
return;
}
}
}
} else {
m -= cnt;
}
}
}
break;
} else {
m -= f;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout << fixed;
cout.precision(12);
int T = 1;
//cin >> T;
for (int t = 0; t < T; t++) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1000 * 1000;
int main()
{
int A, B, W;
cin >> A >> B >> W;
W *= 1000;
int max_i = W / A;
int min_ans = INF;
int max_ans = -1;
for (int i = 1; i <= max_i; i++)
{
if (A * i <= W && W <= B * i)
{
min_ans = min(min_ans, i);
max_ans = max(max_ans, i);
}
}
if (max_ans == -1)
{
cout << "UNSATISFIABLE" << endl;
}
else
{
cout << min_ans << ' ' << max_ans << endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
int a,b,c; cin >> a >> b >> c;
string ans = "No";
if (a * a + b * b < c * c) {
ans = "Yes";
}
cout << ans << endl;
return 0;
} |
#include "bits/stdc++.h"
using namespace std;
int sy,sx;
int cell_group[50][50];
int cell_point[50][50];
int dx[]={1,0,-1,0}, dy[]={0,1,0,-1};
string dcoms[]={"R","D","L","U"};
string ans="";
chrono::system_clock::time_point start;
chrono::system_clock::time_point _end;
bool comp(const pair<pair<int,string>,pair<int,int>>& lh, const pair<pair<int,string>,pair<int,int>>& rh) {
return (lh.first.first+cell_point[lh.second.first][lh.second.second]) < (rh.first.first+cell_point[rh.second.first][rh.second.second]);
}
pair<string,int> dfs(int h, int w, int point, unordered_set<int>& used){
_end = chrono::system_clock::now();
double elapsed = chrono::duration_cast<chrono::milliseconds>(_end-start).count();
if(elapsed>1850){
string ans_reversed (ans.rbegin(), ans.rend());
cout<<ans_reversed<<endl;
exit(0);
}
vector<pair<pair<int,string>,pair<int,int>>> target;
point+=cell_point[h][w];
for(int i=0;i<4;i++){
int next_h=h+dy[i],next_w=w+dx[i];
if(next_h<0 or 50<=next_h or next_w<0 or 50<=next_w) continue;
if(used.find(cell_group[next_h][next_w])==used.end()){
target.push_back(make_pair(make_pair(point,dcoms[i]),make_pair(next_h, next_w)));
}
}
if(target.size()==0) return make_pair("", point);
sort(target.begin(), target.end(), comp);
pair<string,int> fixed_ret=make_pair("", 0);
if(true){
int j=target.size()-1;
int next_h=target[j].second.first;
int next_w=target[j].second.second;
string dcom=target[j].first.second;
int tmp_point=target[j].first.first;
used.emplace(cell_group[next_h][next_w]);
pair<string,int> ret = dfs(next_h,next_w,tmp_point,used);
used.erase(cell_group[next_h][next_w]);
if(fixed_ret.second<ret.second){
fixed_ret=make_pair(ret.first+dcom,ret.second);
}
}
random_device seed_gen;
mt19937 engine = mt19937(seed_gen());
uniform_real_distribution<> dist1(0, 1.0);
if(target.size()>=2 && dist1(engine)<0.15){
random_device seed_gen;
mt19937 engine = mt19937(seed_gen());
uniform_int_distribution<> randint(1,target.size()-1);
int j=randint(engine);
int next_h=target[j].second.first;
int next_w=target[j].second.second;
string dcom=target[j].first.second;
int tmp_point=target[j].first.first;
used.emplace(cell_group[next_h][next_w]);
pair<string,int> ret = dfs(next_h,next_w,tmp_point,used);
used.erase(cell_group[next_h][next_w]);
if(fixed_ret.second<ret.second){
fixed_ret=make_pair(ret.first+dcom,ret.second);
}
}
return fixed_ret;
}
int main(void){
start = chrono::system_clock::now();
cin>>sy>>sx;
for(int i=0;i<50;i++){
for(int j=0;j<50;j++){
cin>>cell_group[i][j];
}
}
for(int i=0;i<50;i++){
for(int j=0;j<50;j++){
cin>>cell_point[i][j];
}
}
int mx_point=0;
while(1){
unordered_set<int> used;
used.emplace(cell_group[sy][sx]);
pair<string,int> tmp=dfs(sy,sx,0,used);
string ans_command=tmp.first;
int point=tmp.second;
if(mx_point<point){
ans=ans_command;
mx_point=point;
}
}
}
| #include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); i++)
#define rng(a) a.begin(),a.end()
using namespace std;
using ll = long long;
// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
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 {
mint res(*this);
return res+=a;
}
mint operator-(const mint a) const {
mint res(*this);
return res-=a;
}
mint operator*(const mint a) const {
mint res(*this);
return res*=a;
}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t>>1);
a *= a;
if (t&1) a *= *this;
return a;
}
// for prime mod
mint inv() const {
return pow(mod-2);
}
mint& operator/=(const mint a) {
return (*this) *= a.inv();
}
mint operator/(const mint a) const {
mint res(*this);
return res/=a;
}
};
mint dp[2010][2010];
mint x[2010][2010], y[2010][2010], z[2010][2010];
char s[2010][2010];
int main() {
int h, w;
cin >> h >> w;
rep(i,h) {
rep(j,w) cin >> s[i][j];
}
dp[0][0] = 1;
rep(i,h) {
rep(j,w) {
if(i==0&&j==0) continue;
if(s[i][j]=='#') continue;
if(j>0) x[i][j] = x[i][j-1]+dp[i][j-1];
if(i>0) y[i][j] = y[i-1][j]+dp[i-1][j];
if(i>0&&j>0) z[i][j] = z[i-1][j-1]+dp[i-1][j-1];
dp[i][j] = x[i][j]+y[i][j]+z[i][j];
}
}
cout << dp[h-1][w-1].x << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll =long long;
#define all(v) v.begin(),v.end()
int main() {
ll H,W,N,M;
cin>>H>>W>>N>>M;
vector<vector<ll>> vec(H,vector<ll> (W,0));
for(ll i=0;i<N;i++) {
ll A,B;
cin>>A>>B;
A--;
B--;
vec[A][B]=1;
}
for(ll i=0;i<M;i++) {
ll C,D;
cin>>C>>D;
C--;
D--;
vec[C][D]=2;
}
vector<vector<bool>> note(H,vector<bool> (W,false));
ll ans=0;
for(ll i=0;i<W;i++) {
ll count=0;
for(ll j=0;j<H;j++) {
if(vec[j][i]==0) {
if(count<=0) {
count--;
}
else {
count++;
}
}
else if(vec[j][i]==1) {
if(count<=0) {
count*=-1;
}
count++;
}
else {
if(count<=0) {
count=0;
}
else {
ans+=count;
for(ll h=j-count;h<j;h++) {
note[h][i]=true;
}
count=0;
}
}
if(j==H-1&&count>0) {
ans+=count;
for(ll h=j-count+1;h<=j;h++) {
note[h][i]=true;
}
}
}
}
for(ll i=0;i<H;i++) {
ll count=0;
for(ll j=0;j<W;j++) {
if(vec[i][j]==0) {
if(count<=0) {
count--;
}
else {
count++;
}
}
else if(vec[i][j]==1) {
if(count<=0) {
count*=-1;
}
count++;
}
else {
if(count<=0) {
count=0;
}
else {
ans+=count;
for(ll h=j-count;h<j;h++) {
if(note[i][h]) {
ans--;
}
}
count=0;
}
}
if(count>0&&j==W-1) {
ans+=count;
for(ll h=j-count+1;h<=j;h++) {
if(note[i][h]) {
ans--;
}
}
}
}
}
cout<<ans<<endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define testing 1
#define FOR(ii,ss,ee) for (ll ii = (ss); ii<=(ll)(ee);++ii)
#define DEC(ii,ss,ee) for (ll ii = (ss); ii>=(ll)(ee);--ii)
#define IAMSPEED ios_base::sync_with_stdio(false); cin.tie(0);
#define db(x) cerr << #x << '=' << x << endl
#define db2(a,b) cerr << #a << "=" << a << " , " << #b << "=" << b << endl
#define dbv(v) cerr << #v << ":"; for (auto it : v) cerr << it << ' '; cerr << endl
#define dba(a,ss,ee) cerr #a << ":"; FOR(i,ss,ee) cerr << a[i] << ' '; cerr << endl
#define ll long long
#define pb push_back
#define inf (int)1e9
#define oo (ll)1e18
#define all(x) (x).begin(), (x).end()
#define f first
#define s second
#define reach cerr << "endl\n";
typedef pair<int,int> pi;
typedef tuple<int,int,int> ti3;
const int MOD = 1e9+7;
template<typename T> void maxself(T &a, T b) {
a = max(a,b);
}
template <typename T> void minself(T & a, T b) {
a = min(a,b);
}
#define MAXN
#if !testing
#define cerr if(0)cout
#endif
int bulb,block;
int32_t main() {
IAMSPEED
int n,m; cin >> n >> m >> bulb >> block;
char A[n+5][m+5];
int a[n+5][m+5], b[n+5][m+5], c[n+5][m+5], d[n+5][m+5];
FOR(i,1,n)FOR(j,1,m)A[i][j]='x';
memset(a,0,sizeof a);
memset(b,0,sizeof b);
memset(c,0,sizeof c);
memset(d,0,sizeof d);
FOR(i,1,bulb) {
int x,y; cin >> x >> y;
A[x][y] = '.';
}
FOR(i,1,block) {
int x,y; cin >> x >> y;
A[x][y]='#';
}
FOR(i,1,n) {
a[i][1] = A[i][1]=='.';
FOR(j,2,m) {
a[i][j] = a[i][j-1];
if(A[i][j]=='.')a[i][j]=1;
if (A[i][j] == '#') a[i][j] = 0;
}
}
FOR(i,1,m) {
b[1][i] = A[1][i]=='.';
FOR(j,2,n) {
b[j][i] = b[j-1][i];
if(A[j][i]=='.')b[j][i]=1;
if (A[j][i]=='#') b[j][i] = 0;
}
}
FOR(i,1,n) {
c[i][m] = A[i][m]=='.';
DEC(j,m-1,1) {
c[i][j] = c[i][j+1];
if(A[i][j]=='.')c[i][j]=1;
if (A[i][j]=='#')c[i][j]=0;
}
}
FOR(i,1,m) {
d[n][i] = A[n][i]=='.';
DEC(j,n-1,1) {
d[j][i] = d[j+1][i];
if(A[j][i]=='.')d[j][i]=1;
if (A[j][i]=='#')d[j][i]=0;
}
}
ll ans = 0;
FOR(i,1,n) {
FOR(j,1,m) {
//db2(i,j);
//db2(a[i][j],b[i][j]);
//db2(c[i][j],d[i][j]);
ans += !!max({a[i][j],b[i][j],c[i][j],d[i][j]});
}
}
cout << ans;
}
/*
1 5
*/
|
#include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define int long long
#define pb push_back
#define pii pair<int,int>
#define vi vector<int>
#define mii map<int,int>
#define pqb priority_queue<int>
#define pqs priority_queue<int,vi,greater<int> >
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x,y) cout<<setprecision(y)<<x
#define mk(arr,n,type) type *arr=new type[n];
#define w(x) int x; cin>>x; while(x--)
#define pw(b,p) pow(b,p) + 0.1
#define endl "\n"
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void __print(int32_t x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
void fastIO()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}
void solve()
{
int n,k;
cin>>n>>k;
while(k--)
{
if(n%200==0)
{
n/=200;
}
else
{
n*=1000;
n+=200;
}
}
cout<<n;
}
int32_t main()
{
fastIO();
//w(t)
{
solve();
cout<<endl;
}
return 0;
} | #include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
int main(){
int n,k;
cin >> n >> k;
ll ans = n;
for(int i=0;i<k;i++){
if(ans%200 == 0){
ans = ans/200;
}
else{
ans = ans*1000 + 200;
}
}
cout << ans << 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
int to[400001], ne[400001], he[200001];
int Q[200001], pl[200001], dl[200001];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, K;
cin >> N >> K;
int k = 1;
rep(i, N - 1) {
int u, v;
cin >> u >> v;
to[k] = v;
ne[k] = he[u];
he[u] = k++;
to[k] = u;
ne[k] = he[v];
he[v] = k++;
}
int q = 0, p = 0;
Q[q++] = 1;
while (p < N) {
int u = Q[p++];
int tmp = 0;
int ind = he[u];
he[u] = 0;
while (ind) {
int v = to[ind];
tmp = ne[ind];
if (he[v] > 0) {
Q[q++] = v;
ne[ind] = he[u];
he[u] = -ind;
}
ind = tmp;
}
}
int are = 1000000, pd0 = are;
int L = 0, R = (N + K - 1) / K;
while (L + 1 < R) {
int wj = (L + R) / 2;
int num = 0;
for (int i = N - 1; i >= 0; i--) {
int po = pd0 - 1;
int dd = pd0;
int u = Q[i];
for (int ind = -he[u]; ind; ind = -ne[ind]) {
int v = to[ind];
if (po < pl[v]) po = pl[v];
if (dd < dl[v]) dd = dl[v];
}
if (po >= dd) dd = 0;
if (dd >= pd0 + wj) {
num++;
po = pd0 + wj;
dd = 0;
}
pl[u] = po - 1;
dl[u] = dd + 1;
}
int are = num + (dl[1] > pd0);
if (are > K) L = wj;
else R = wj;
pd0 += are;
}
co(R);
Would you please return 0;
} | #include <iostream>
#include <cstdio>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <vector>
#include <queue>
#define lowbit(x) ((x) & (-(x)))
typedef long long ll;
#define For(i , a , b) for(ll i = a ; i <= b ; i ++)
#define FoR(i , a , b) for(ll i = a ; i >= b ; i --)
ll MIN(ll x, ll y) { return x < y ? x : y; }
ll MAX(ll x, ll y) { return x > y ? x : y; }
const ll MAXN = 523;
const ll MO = 998244353;
using namespace std;
ll H , W;
char mp[MAXN][MAXN];
char col[MAXN << 1 | 1];
int ans;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> H >> W;
For (i , 1 , H)
cin >> (mp[i] + 1);
For (i , 1 , H)
For (j , 1 , W) {
const char &w = mp[i][j];
if (w == '.')
continue;
if (!col[i + j])
col[i + j] = w;
else {
if (col[i + j] != w) {
printf ("0\n");
return 0;
}
}
}
ans = 1;
For (i , 2 , H + W)
if (!col[i])
ans = (ans * 2) % MO;
printf ("%lld\n" , ans);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#include <math.h>
#include <iomanip>
#include <cstdint>
#include <string>
#include <sstream>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
#define rep(i,n) for (int i = 0; i < (n); ++i)
typedef long long ll;
using P=pair<ll,ll>;
const ll INF=1e18;
const int mod=1e9+7;
void solve(){
ll l,r;
cin>>l>>r;
vector<ll>x(r+1);
ll ans=0;
for(int i=r;i>=2;i--){
ll p=r/i-(l-1)/i;
x[i]=p*p;
for(int j=2*i;j<=r;j+=i){
x[i]-=x[j];
}
ans+=x[i];
}
for(ll i=l;i<=r;i++){
if(i==1){continue;}
ans-=r/i*2-1;
}
cout<<ans<<endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef pair<int,int> P ;
#define rep(i,n) for(int i = 0 ; i < n ; i++)
#define rrep(i,a,b) for(int i = a ; i < b ; i++)
int l , r ;
int A[1000007] ;
int ans[1000007] ;
int prime_factor[1000007] ;
void ertstns(){
memset(prime_factor,-1,sizeof(prime_factor)) ;
prime_factor[0] = prime_factor[1] = 1 ;
for(int i = 2 ; i < 1000007 ; i++){
if(prime_factor[i] == -1){
for(int j = i ; j < 1000007 ; j += i) prime_factor[j] = i ;
}
}
}
int main(){
ertstns() ;
cin >> l >> r ;
rrep(i,max(l,2),r+1){
int k = i ;
vector<int> vec ;
map<int,bool> use ;
while(k != 1){
if(!use[prime_factor[k]]) vec.push_back(prime_factor[k]) ;
use[prime_factor[k]] = true ;
k /= prime_factor[k] ;
}
int t = vec.size() ;
for(int S = 1 ; S < 1 << t ; S++){
ll val = 1 ;
for(int bit = 0 ; bit < t ; bit++){
if(S >> bit & 1) val *= vec[bit] ;
}
ans[val]++ ;
}
}
ll res = 0 , mns = 0 ;
for(int i = max(l,2) ; i <= r ; i++){
int k = i ;
vector<int> vec ;
map<int,bool> use ;
while(k != 1){
if(!use[prime_factor[k]]) vec.push_back(prime_factor[k]) ;
use[prime_factor[k]] = true ;
k /= prime_factor[k] ;
}
int t = vec.size() ;
for(int S = 1 ; S < 1 << t ; S++){
ll count = 0 , val = 1 ;
for(int j = 0 ; j < t ; j++) if(S >> j & 1){
count++ ;
val *= vec[j] ;
}
if(count % 2 == 0) res -= ans[val] ;
else res += ans[val] ;
}
if(i * 2 <= r) mns += 2 * (r / i - 1) ;
mns++ ;
}
cout << res - mns << endl ;
} |
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define FOR(i,n,m) for(int i=(int)(n); i<=(int)(m); i++)
#define RFOR(i,n,m) for(int i=(int)(n); i>=(int)(m); i--)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define RITR(x,c) for(__typeof(c.rbegin()) x=c.rbegin();x!=c.rend();x++)
#define setp(n) fixed << setprecision(n)
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll,ll>
#define pi pair<int,int>
#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second
#define pb push_back
#define ins insert
#define debug(a) cerr<<(a)<<endl
#define dbrep(a,n) rep(_i,n) cerr<<(a[_i])<<" "; cerr<<endl
#define dbrep2(a,n,m) rep(_i,n){rep(_j,m) cerr<<(a[_i][_j])<<" "; cerr<<endl;}
using namespace std;
template<class A, class B>
ostream &operator<<(ostream &os, const pair<A,B> &p){return os<<"("<<p.fi<<","<<p.se<<")";}
template<class A, class B>
istream &operator>>(istream &is, pair<A,B> &p){return is>>p.fi>>p.se;}
template<class T>
vector<T> make_vec(size_t a){
return vector<T>(a);
}
template<class T, class... Ts>
auto make_vec(size_t a, Ts... ts){
return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
/* Some Libraries */
//-------------------------------------------------
int main(void)
{
cin.tie(0);
ios::sync_with_stdio(false);
int N,M; cin>>N>>M;
vi X(M),Y(M),Z(M);
rep(i,M) cin>>X[i]>>Y[i]>>Z[i];
vll dp(1<<N);
dp[0] = 1;
rep(S,1<<N){
int popcnt = __builtin_popcount(S);
bool ok=true;
rep(i,M){
if (popcnt>X[i]) continue;
int mask = (1<<Y[i])-1;
if (__builtin_popcount(S&mask) > Z[i]){
ok=false;
break;
}
}
if (!ok) continue;
rep(i,N)if(!(S>>i&1)){
int T = S|(1<<i);
dp[T] += dp[S];
}
}
cout<<dp[(1<<N)-1]<<endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pb push_back
#define ff first
#define ss second
#define YES printf("YES\n")
#define NO printf("NO\n")
#define nn "\n"
#define sci(x) scanf("%d", &x)
#define LL_INF (1LL << 62)
#define INF (1 << 30)
#define SetBit(x, k) (x |= (1LL << k))
#define ClearBit(x, k) (x &= ~(1LL << k))
#define CheckBit(x, k) (x & (1LL << k))
#define mod 1000000007
#define N 300005
int n, q;
int arr[N];
int tree[3*N];
void init(int b, int e, int node = 1)
{
if (b == e)
{
tree[node] = arr[b];
return;
}
int left = node * 2;
int right = node * 2 + 1;
int mid = (b + e) / 2;
init(b, mid, left);
init(mid + 1, e, right);
tree[node] = tree[left] ^ tree[right];
}
int query(int b, int e, int i, int j, int node = 1)
{
if (b >= i && e <= j)
return tree[node];
if (b > j || e < i)
return 0;
int left = node * 2;
int right = node * 2 + 1;
int mid = (b + e) / 2;
return query(b, mid, i, j, left) ^ query(mid + 1, e, i, j, right);
}
void update(int b, int e, int i, int val, int node = 1)
{
if (b > i || e < i)
return;
if (b == e && e == i)
{
tree[node] = val;
return;
}
int left = node * 2;
int right = node * 2 + 1;
int mid = (b + e) / 2;
update(b, mid, i, val, left);
update(mid + 1, e, i, val, right);
tree[node] = tree[left] ^ tree[right];
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> q;
for(int i = 1; i <= n; i++){
cin >> arr[i];
}
init(1, n);
while(q--){
int t, x, y;
cin >> t >> x >> y;
if(t==1){
update(1, n, x, y^query(1, n, x, x));
}
else {
cout << query(1, n, x, y) << nn;
}
}
return 0;
}
|
#include<bits/stdc++.h>
//#pragma GCC optimize "trapv"
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
#define fast_az_fuk ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll long long
#define lll __int128
#define ull unsigned ll
#define ld long double
#define pb push_back
#define pf push_front
#define dll deque<ll>
#define vll vector<ll>
#define vvll vector<vll>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define dpll deque<pll>
#define mapll map<ll,ll>
#define umapll umap<ll,ll>
#define endl "\n"
#define all(v) v.begin(),v.end()
#define ms(a,x) memset(a,x,sizeof(a))
#define random(l,r,T) uniform_int_distribution<T>(l,r)(rng)
//#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//using namespace __gnu_pbds;
template<typename T> istream& operator >>(istream &in,vector<T> &v){ for(auto &x:v) in>>x; return in;}
template<typename T> ostream& operator <<(ostream &out,vector<T> &v){ for(auto &x:v) out<<x<<' '; return out;}
template<typename T1,typename T2> istream& operator >>(istream &in,pair<T1,T2> &p){ in>>p.first>>p.second; return in;}
template<typename T1,typename T2> ostream& operator <<(ostream &out,pair<T1,T2> &p){ out<<p.first<<' '<<p.second; return out;}
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
template<class T, class H>using umap=unordered_map<T,H,custom_hash>;
template<class T>using uset=unordered_set<T,custom_hash>;
int32_t main()
{
clock_t clk = clock();
fast_az_fuk
ll n,t; cin>>t>>n; ll a = (n*100+t-1)/t; ll ans = a + (a*t)/100; ans--;cout<<ans;
// cerr << '\n'<<"Time (in s): " << double(clock() - clk) * 1.0 / CLOCKS_PER_SEC << '\n';
return 0;
} | #include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
#define x first
#define y second
#define ndl '\n'
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define up_b upper_bound
#define low_b lower_bound
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;
//using namespace __gnu_pbds;
//template<typename T> using indexed_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());//uniform_int_distribution<int> (l, r)
typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<int, ull> piu;
typedef vector<vector<int>> matrix;
const ll INF = 1e18 + 123;
const ld EPS = 1e-9;
const int inf = 1e9 + 123;
const int MOD = 1e9 + 7;
const int N = 1e5 + 123;
const int M = 1e6 + 123;
const double pi = 3.14159265359;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
void solve(){
ll r, x, y;
cin >> r >> x >> y;
ld d = sqrt((x*x)+(y*y));
ll z = d/r;
if ((x*x)+(y*y) == r*r*z*z){
cout << z;
}
else{
cout << max(2ll, z+1);
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
//cin >> t;
for (int i = 1; i <= t; i++){
//cout << "Case #" << i << ": ";
solve();
}
#ifdef KAZAKH
// cout << endl << 1.0 * clock() / CLOCKS_PER_SEC << endl;
#endif
return 0;
} |
//
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
const int maxn = 3050;
const int maxm = maxn*maxn*4;
int head[maxm],vis[maxm];
char G[maxn][maxn];
int dis[maxm];
struct Edge{
int f,t,w,next;
Edge(int f = 0,int t = 0,int w = 0,int next = 0):f(f),t(t),w(w),next(next){}
}edge[maxm];
int cnt;
const int INF = 1e9;
void addedge(int f,int t,int w){
edge[cnt] = Edge(f,t,w,head[f]);
head[f] = cnt++;
}
void dijkstra(int s){
typedef pair <int,int> node;
priority_queue<node,vector<node>,greater<node> >q;
for(int i =1;i< maxm;i++)dis[i] = INF;
dis[s] = 0;
q.push(make_pair(dis[s],s));
while(!q.empty()){
node tmp = q.top();
q.pop();
int u = tmp.second;
if(vis[u])continue;
vis[u] = 1;
for(int i = head[u];~i;i = edge[i].next){
int v = edge[i].t;
if(dis[v] > dis[u] + edge[i].w){
dis[v] = dis[u] + edge[i].w;
if(!vis[v]){
q.push(make_pair(dis[v],v));
}
}
}
}
return;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
memset(head,-1,sizeof(head));
int n,m;
const int B = 2050*2050+1;
int S = B+100;
int T = B+101;
cin >> n >> m;
for(int i = 1;i <= n;i++){
string s;
cin >> s;
for(int j = 1;j <= m;j++){
G[i][j] = s[j-1];
if(s[j-1] == 'S')S = i*2010+j;
if(s[j-1] == 'G')T = i*2010+j;
}
}
for(int i = 1;i <= n;i++){
for(int j = 1;j <= m;j++){
if(G[i][j] != '#'){
for(int k = -1;k <= 1;k++){
for(int l = -1;l <= 1;l++)if(abs(k+l) == 1){
int x = i+k,y = j+l;
if(x < 1 or x > n or y < 1 or y > m)continue;
if(G[i][j] != '#')addedge(i*2010+j,x*2010+y,2);
}
}
}
if(G[i][j] >= 'a' and G[i][j] <= 'z'){
addedge(i*2010+j,B+G[i][j],1);
addedge(B+G[i][j],i*2010+j,1);
}
}
}
dijkstra(S);
if(dis[T] == INF)cout << -1;
else cout << dis[T]/2;
return 0;
}
| #include <bits/stdc++.h>
// #include <bits/extc++.h>
using namespace std;
// using namespace __gnu_pbds;
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
#define pii pair<int,int>
#define vi vector<int>
#define vii vector<vi >
#define vpii vector<pii >
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
#define mod(x) (((LL)(x)%MOD+MOD)%MOD)
#define o(X) (1<<(X))
#define oL(X) (1LL<<(X))
#define LL long long
#define ppt(x) ((int)__builtin_popcount(x))
#define mst(x,a) memset(x,a,sizeof(x))
const int INF=0x3f3f3f3f,MOD=1023694381;
const LL INF_LL=0x3f3f3f3f3f3f3f3fLL;
template <typename T>
int getplc(T x,int y) {
return (int)((x>>y)&1);
}
LL qpow(LL a,LL b=MOD-2,LL _MOD=MOD){
LL res=1;
for(;b;b>>=1,a=a*a%_MOD){
if(b&1)
res=res*a%_MOD;
}
return res;
}
template <typename T>
void ckmin(T &a,T b){ a>b?a=b:0; }
template <typename T>
void ckmax(T &a,T b){ a<b?a=b:0; }
template <typename T>
#define errorl(args...) { \
string _s = #args; \
replace(_s.begin(), _s.end(), ',', ' '); \
stringstream _ss(_s); \
istream_iterator<string> _it(_ss); \
errl(_it, args); \
}
void errl(istream_iterator<string> it) {}
template<typename T, typename... Args>
void errl(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
errl(++it, args...);
}
#define error(args...) { \
string _s = #args; \
replace(_s.begin(), _s.end(), ',', ' '); \
stringstream _ss(_s); \
istream_iterator<string> _it(_ss); \
err(_it, args); \
cerr<<endl; \
}
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << "=" << a << " # ";
err(++it, args...);
}
int dx4[4]={1,0,-1,0},dy4[4]={0,1,0,-1};
int dx8[8]={1,1,0,-1,-1,-1,0,1},dy8[8]={0,1,1,1,0,-1,-1,-1};
const int N=1e6+5;
char s[2005][2005];
int vis[2005][2005],dis[2005][2005],ins[30];
vector<pair<int,int>> g[30];
void Solve() {
int n,m,sx,sy,dx,dy;
cin>>n>>m;
rep(i,1,n){
cin>>(s[i]+1);
rep(j,1,m){
if(s[i][j]=='S')sx=i,sy=j;
else if(s[i][j]=='G')dx=i,dy=j;
else if(islower(s[i][j])){
g[s[i][j]-'a'].eb(i,j);
}
}
}
queue<pair<int,int>>que;
auto push=[&](int x,int y,int stp){
if(!vis[x][y]){
vis[x][y]=1;
dis[x][y]=stp;
que.push({x,y});
}
};
que.push({sx,sy});
vis[sx][sy]=1;
while(!que.empty()){
auto u=que.front();que.pop();
if(islower(s[u.fi][u.se])&&!ins[s[u.fi][u.se]-'a']){
ins[s[u.fi][u.se]-'a']=1;
for(auto c:g[s[u.fi][u.se]-'a']){
push(c.fi,c.se,dis[u.fi][u.se]+1);
}
}
rep(i,0,3){
int tx=dx4[i]+u.fi,ty=dy4[i]+u.se;
if(tx<1||tx>n||ty<1||ty>m||s[tx][ty]=='#')continue;
push(tx,ty,dis[u.fi][u.se]+1);
}
}
if(dis[dx][dy]==0)cout<<-1<<'\n';
else cout<<dis[dx][dy]<<'\n';
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
// freopen("o1.txt","w",stdout);
#endif
ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
Solve();
return 0;
} |
//#pragma GCC target("avx512f,avx512dq,avx512cd,avx512bw,avx512vl")
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define DB double
#define LD long double
#define ST string
#define BS bitset
#define PA pair<LL,LL>
#define VE vector
#define VL VE<LL>
#define VP VE<PA>
#define VVL VE<VL>
#define VVVL VE<VVL>
#define PQ priority_queue
#define PQS priority_queue<LL,vector<LL>,greater<LL>>
#define FI first
#define SE second
#define PB push_back
#define POB pop_back
#define PF push_front
#define POF pop_front
#define MP make_pair
#define TS to_string
#define TU to_ullong
#define BPL __builtin_popcountll
#define FOR(i,a,n) for(i=a;i<n;++i)
#define FORR(i,a,n) for(i=n-1;i>=a;--i)
#define rep(i,n) FOR(i,0,n)
#define repr(i,n) FORR(i,0,n)
#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
#define SORT(a) sort(ALL(a))
#define REV(a) reverse(ALL(a))
#define UB(a,n) *upper_bound(ALL(a),n)
#define UBn(a,n) upper_bound(ALL(a),n)-a.begin()
#define LB(a,n) *lower_bound(ALL(a),n)
#define LBn(a,n) lower_bound(ALL(a),n)-a.begin()
#define INF 1000000000000000003
#define PI 3.14159265358979323846264338327950288
//#define MOD 1000000007
#define MOD 998244353
#define ERR 1e-10
#define coutl cout<<fixed<<setprecision(15)
#define FAST cin.tie(0);ios::sync_with_stdio(false)
void Yn(LL a){if(a)cout<<"Yes"<<endl;else cout<<"No"<<endl;}
void YN(LL a){if(a)cout<<"YES"<<endl;else cout<<"NO"<<endl;}
LL pwmn(LL a,LL n){LL ans=1;while(ans<a)ans*=n;return ans;}
LL dig(LL n){LL ret=0;while(n)n/=10,++ret;return ret;}
LL GCD(LL a,LL b){LL c=1,tmp=max(a,b);b=min(a,b);a=tmp;while(c!=0){c=a%b;a=b;b=c;}return a;}
LL LCM(LL a,LL b){return a*b/GCD(a,b);}
LL cmod(LL a,LL m){if(a%m<0)return a%m+abs(m);else return a%m;}
LL cdiv(LL a,LL b){return ((a<0&&b<0)||(a>=0&&b>=0)?a/b:(a-b+1)/b);}
LL DIV(LL a,LL d,LL m){LL l=m,x=1,y=0,k;while(l){k=d/l;d-=k*l;swap(l,d);x-=k*y;swap(x,y);}return cmod(a*cmod(x,m),m);}
LL POW(LL a,LL n,LL m){LL ans=1;while(n>0){if(n&1)ans=ans*a%m;a=a*a%m;n>>=1;}return ans;}
VL fact,finv,inv;
void comi(LL n){LL i;fact.resize(max(2LL,n+1));finv.resize(max(2LL,n+1));inv.resize(max(2LL,n+1));fact[0]=fact[1]=1;finv[0]=finv[1]=1;inv[0]=inv[1]=1;FOR(i,2,n+1){fact[i]=fact[i-1]*i%MOD;inv[i]=MOD-inv[MOD%i]*(MOD/i)%MOD;finv[i]=finv[i-1]*inv[i]%MOD;}}
LL com(LL n,LL k){if(n<k||n<0||k<0)return 0;return fact[n]*(finv[k]*finv[n-k]%MOD)%MOD;}
bool cmps(PA a,PA b){if(a.SE!=b.SE)return a.SE<b.SE;return a.FI<b.FI;}
template<typename T>bool chmax(T &a,T b){if(a<b){a=b;return true;}return false;}
template<typename T>bool chmin(T &a,T b){if(a>b){a=b;return true;}return false;}
template<typename T>void vout(VE<T> &v){LL i;rep(i,v.size()){cout<<v[i];if(i<v.size()-1)cout<<" ";}cout<<endl;}
template<typename T>void v2out(VE<VE<T>> &v){for(auto a:v)vout(a);}
int main(){
FAST;
LL i,ans=0,N,K;
cin>>N>>K;
if(N+K>=15&&K>=8)cout<<1<<endl;
else if(N+K>=10&&K>=3)cout<<2<<endl;
else if(N+K>=3)cout<<3<<endl;
else cout<<4<<endl;
}
| //In the name of Allah :)
#include <bits/stdc++.h>
using namespace std;
string to_string(char c) { return string(1,c); }
string to_string(bool b) { return b ? "true" : "false"; }
string to_string(const char* s) { return (string)s; }
string to_string(string s) { return s; }
template<class A> string to_string(complex<A> c) {
stringstream ss; ss << c; return ss.str(); }
string to_string(vector<bool> v) {
string res = "{"; for(int i = 0; i < (int)v.size(); i++) res += char('0'+v[i]);
res += "}"; return res; }
template<size_t SZ> string to_string(bitset<SZ> b) {
string res = ""; for(size_t i = 0; i < SZ; i++) res += char('0'+b[i]);
return res; }
template<class A, class B> string to_string(pair<A,B> p);
template<class T> string to_string(T v) { // containers with begin(), end()
bool fst = 1; string res = "{";
for (const auto& x: v) {
if (!fst) res += ", ";
fst = 0; res += to_string(x);
}
res += "}"; return res;
}
template<class A, class B> string to_string(pair<A,B> p) {
return "("+to_string(p.first)+", "+to_string(p.second)+")"; }
void DBG() { cerr << "]" << endl; }
template<class H, class... T> void DBG(H h, T... t) {
cerr << to_string(h); if (sizeof...(t)) cerr << ", ";
DBG(t...); }
#ifdef LOCAL // compile with -DLOCAL
#define wis(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "] : [", DBG(__VA_ARGS__)
#else
#define wis(...) 0
#endif
typedef long long ll;
#define all(x) (x).begin(), (x).end()
const int MAXN = 2010, MOD = 1e9 + 7;
ll dp[MAXN][MAXN], psum[3][MAXN][MAXN];
int main(){
ios::sync_with_stdio(0);
int n, m;
cin >> n >> m;
vector<string> a(n);
for(int i = 0; i < n; i++){
cin >> a[i];
}
dp[0][0] = 1;
psum[0][1][1] = psum[1][1][1] = psum[2][1][1] = 1;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(a[i - 1][j - 1] == '#'){
continue;
}
if(i == 1 && j == 1){
continue;
}
dp[i][j] = (dp[i][j] + psum[0][i - 1][j]) % MOD;
dp[i][j] = (dp[i][j] + psum[1][i][j - 1]) % MOD;
dp[i][j] = (dp[i][j] + psum[2][i - 1][j - 1]) % MOD;
psum[0][i][j] = psum[0][i - 1][j] + dp[i][j];
psum[1][i][j] = psum[1][i][j - 1] + dp[i][j];
psum[2][i][j] = psum[2][i - 1][j - 1] + dp[i][j];
}
}
cout << dp[n][m] << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
int n;
cin >> n;
const int64_t inf = LLONG_MAX / 2;
vector<int64_t> a(n);
vector<int> t(n);
for (int i = 0; i < n; ++i) cin >> a[i] >> t[i];
auto f = [&](int64_t x) {
for (int i = 0; i < n; ++i) {
if (t[i] == 1) x += a[i];
else if (t[i] == 2) x = max(x, a[i]);
else x = min(x, a[i]);
}
return x;
};
int64_t lb = -inf;
int64_t ub = inf;
int64_t d = 0;
for (int i = 0; i < n; ++i) {
if (t[i] == 1) lb += a[i], ub += a[i], d += a[i];
else if (t[i] == 2) lb = max(lb, a[i]);
else ub = min(ub, a[i]);
if (lb > ub) {
if (t[i] == 2) ub=lb;
else lb=ub;
}
}
lb -= d, ub -= d;
int q;
cin >> q;
for (int i = 0; i < q; ++i) {
int64_t x;
cin >> x;
if (x <= lb) cout << lb + d << endl;
else if (x >= ub) cout << ub + d << endl;
else cout << x + d << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 4;
vector<int> adj[N];
int c[20] , d[20][N];
int inf;
void bfs(int idx){
int src = c[idx];
queue<int> q;
q.push(src);
d[idx][src] = 0;
while((int)q.size()){
int tp = q.front();
q.pop();
for(int v : adj[tp]){
if(d[idx][v] >= inf){
d[idx][v] = d[idx][tp] + 1;
q.push(v);
}
}
}
}
int memo[18][(1<<18)+3] , k;
int solve(int idx , int msk){
if(__builtin_popcount(msk) == k) {
return 0;
}
int &ret = memo[idx][msk];
if(~ret)
return ret;
ret = 2e7;
for(int i = 0 ; i < k ; ++i){
if((msk >> i) & 1)continue;
ret = min(ret , d[idx][c[i]] + solve(i , msk | (1 << i)));
}
return ret;
}
void solve(){
int n , m;
cin >> n >> m;
for(int i = 0 ; i < m ; ++i){
int u , v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
memset(d , '?' , sizeof d);
inf = d[0][0];
cin >> k;
for(int i = 0 ; i < k ; ++i){
cin >> c[i];
bfs(i);
}
memset(memo , -1 , sizeof(memo));
int rs = 1 + solve(0 , 1);
for(int i = 1 ; i < k ; ++i){
rs = min(rs , 1 + solve(i , (1 << i)));
}
cout << (rs >= 2e7 ? -1 : rs) << '\n';
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int t = 1;
// cin >> t;
while(t--){
solve();
}
} |
//-- In the name of ALLAH --
// We're nothing and you're everything.
// Ya Ali!
#include<bits/stdc++.h>
using namespace std;
#define pai acos(-1)
#define ff first
#define ss second
#define ll long long
#define pb push_back
#define mp make_pair
#define pll pair<ll,ll>
#define sz(a) (ll)a.size()
#define endl "\n"
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) ((a)*((b)/gcd(a,b))
#define all(vec) vec.begin(),vec.end()
#define ms(a, b) memset(a,b,sizeof(a))
#define rep(i,n) for(int i= 0; i < int(n); i++)
#define rep1(i,n) for(int i= 1; i <= n; i++)
#define rev(i,n) for(int i= (int)(n-1);i>=0;i--)
#define rev1(i,n) for(int i= (int) n;i>= 1;i--)
#define TEST_CASE(t) for(int zz=1;zz<=t;zz++)
#define PRINT_CASE printf("Case %d: ",zz)
#define fii freopen("input.txt","r",stdin);
#define foo freopen("output.txt","w",stdout);
#define FAST ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
const ll M = 1e9+7;
const ll mxn = 1e5+7;
int main()
{
ll i,j,k,a,b,c,d,n,m,t,x,y,z,u,v;
cin>>n>>k;
if(k==0)
{
cout<<n<<endl;
return 0;
}
map<ll,ll>frq;
for(i=1;i<=(n+1);i++)
{
frq[i]=(i-1);
}
ll tem=0;
for(i=(2*n);i>(n+1);i--)
{
tem++;
frq[i]=tem;
}
k=abs(k);
ll ans=0;
map<ll,bool>vis;
for(i=1;i<=(2*n);i++)
{
if(!vis[i]&&frq[i]>=1)
{
a = i+k;
if(!vis[a]&&frq[a]>=1)
{
ans+=(frq[i]*frq[a]);
vis[i]=true;
//vis[a]=true;
}
}
}
cout<<ans<<endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
/*---------macro---------*/
#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 Clear(a) a = decltype(a)()
#define all(a) begin(a),end(a)
#define rall(a) rbegin(a),rend(a)
#define bit(i,j) ((i>>j)&1)
/*---------type/const---------*/
typedef long long ll;
typedef unsigned long long ull;
typedef string::const_iterator state; //ๆงๆ่งฃๆ
const ll big=1000000007;
//const ll big=998244353;
const double PI=acos(-1);
const double EPS=1e-8;//้ฉๅฎๅคใใ
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};
const char newl='\n';
struct{
constexpr operator int(){return -int(1e9)-1;}
constexpr operator ll(){return -ll(1e18)-1;}
}neginf;
struct{
constexpr operator int(){return int(1e9)+1;}
constexpr operator ll(){return ll(1e18)+1;}
constexpr auto operator -(){return neginf;}
}inf;
/*---------debug---------*/
#ifdef LOGX
#include <template/debug.hpp>
#define _GLIBCXX_DEBUG
#else
#define dbg(x) ;
#define prt(x) ;
#define _prt(x) ;
#endif
/*---------function---------*/
template<typename T,typename U> T max(const T x,const U y){if(x>y)return x;else return y;}
template<typename T,typename U> T min(const T x,const U y){if(x<y)return x;else return y;}
template<typename T> T max(const vector<T> &a){T ans=a[0];for(T elem:a){ans=max(ans,elem);}return ans;}
template<typename T> T min(const vector<T> &a){T ans=a[0];for(T elem:a){ans=min(ans,elem);}return ans;}
template<typename T,typename U> bool chmin(T &a,const U b){if(a>b){a=b;return true;}return false;}
template<typename T,typename U> bool chmax(T &a,const U b){if(a<b){a=b;return true;}return false;}
bool valid(int i,int j,int h,int w){return (i>=0 && j>=0 && i<h && j<w);}
template<typename T,typename U>
T expm(T x,U y,const ll mod=big){
T res=1;
while(y){
if(y&1)(res*=x)%=mod;
(x*=x)%=mod;
y>>=1;
}
return res;
}
template<typename T,typename U>
T exp(T x,U y){
T res=1;
while(y){
if(y&1)res*=x;
x*=x;
y>>=1;
}
return res;
}
int n;
vector<int> p;
vector<vector<int>> g;//g[i][0]ใiใฎ่ฆช.
vector<vector<bool>> notmy;
vector<int> ans;
//ใ่ชๅใใ็ธๆใใฎ่พบใๆฑบๅฎใใ.
bool dfs(int i){
if(i!=0 && g[i].size()==1)return true;
bool res=false;
rep2(j,int(i!=0),g[i].size()){
res^=(notmy[i][j]=dfs(g[i][j]));
}
return !res;
}
//ใใฎ้ ็นใใซใ็งปๅใใใใใจใ้ธๆใใไบบใใฉใใ ใ็ฒๅพใใใ๏ผ
int calc(int i){
if(g[i].size()==1 && i!=0){
return ans[i]=-1;
}
vector<int> mine;
vector<int> ene;
rep2(j,int(i!=0),g[i].size()){
if(notmy[i][j])ene.emplace_back(calc(g[i][j]));
else mine.emplace_back(calc(g[i][j]));
}
sort(rall(mine));
sort(rall(ene));
int res=-1;
//ใeneใใฎๅๆฐใๅถๆฐๅใชใ, mineใฏๅ
จใฆ่ชๅใงใจใใใจใซใชใ.
if(ene.size()%2==0){
for(auto &e:mine)res+=e;
rep(j,ene.size()){
if(j%2==0)res+=ene[j];
else res-=ene[j];
}
}
else{
for(auto &e:mine)res+=abs(e);
rep(j,ene.size()){
if(j%2==0)res+=ene[j];
else res-=ene[j];
}
}
return ans[i]=res;
}
int main(){
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
cout.precision(10);
/*--------------------------------*/
cin >> n;
p.resize(n);
g.resize(n);
rep(i,n-1){
cin >> p[i+1];
p[i+1]--;
g[i+1].emplace_back(p[i+1]);
g[p[i+1]].emplace_back(i+1);
}
notmy.resize(n);
rep(i,n){
notmy[i].resize(g[i].size());
}
ans.resize(n,0);
dfs(0);
dbg(notmy);
calc(0);
dbg(ans);
cout << (n-ans.front())/2 << newl;
} |
//#define _GLIBCXX_DEBUG
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include<algorithm>
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
#define all(x) (x).begin(),(x).end()
typedef long long ll;
typedef unsigned int uint;
template<class T> inline bool chmax(T& a, const T& b) {if (a<b) {a=b; return true;} return false;}
template<class T> inline bool chmin(T& a, const T& b) {if (b<a) {a=b; return true;} return false;}
const int INF = 1000000000;
int main() {
IOS;
int K;
cin >> K;
ll ans = 0;
set<tuple<int,int>>st;
map<ll, int> mp;
for (int i=1; i*i<=K; i++){
for (int j=i; j*j<=K; j++){
if (i*j > K) continue;
int kmax = K / i / j;
chmax(mp[i*j], kmax);
vector<int>v = {i,j};
sort(all(v));
st.insert(make_tuple(v[0],v[1]));
}
}
map<vector<int>, bool>isthere;
for (tuple<int,int> tp: st){
int first = get<0>(tp);
int second = get<1>(tp);
int kmax = mp[first*second];
for (int i=max(first,second); i<=kmax; i++){
int third2 = i;
vector<int> v = {first,second,i};
sort(all(v));
//if (isthere[v]) continue;
if (first == second and second == third2 and first == third2) ans++;
else if (first == second or second==third2 or first==third2 ){
ans += 3;
}
else {
ans += 6;
}
//isthere[v] = true;
}
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
int k, t;
long long ans;
int main(){
scanf("%d", &k);
for(int i=1; i<=k; i++) for(int j=1; j*i<=k; j++) ans += k/i/j;
printf("%lld\n", ans);
} |
#include <iostream>
using namespace std;
int main(){
string n;
cin >> n;
int N = n.size();
for(int i=0;i<N;i++){
bool flag = true;
for(int j=0;j<n.size()/2;j++){
if(n[j] != n[n.size()-j-1]){
flag = false;
break;
}
}
if(flag){
cout << "Yes\n";
return 0;
}
n = '0'+n;
}
cout << "No\n";
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define ff first
#define ss second
int main(void){
// Your code here!
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
string s,r;
cin >> n;
while(n%10==0 && n>0)
n/=10;
s=to_string(n);
r=s;
reverse(r.begin(),r.end());
cout << (s==r ? "Yes" : "No");
return 0;
} |
#include <bits/stdc++.h>
typedef long long LL;
#define rep(i, n) for (LL i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define repB(i, m, n) for (LL i = m; i <= n; ++i)
#define repR(i, n) for (LL i = n - 1; i >= 0; --i)
#define all(n) n.begin(), n.end()
using namespace std;
int main() {
ios::sync_with_stdio(0);
int x, y;
cin >> x >> y;
if (abs(x - y) < 3) {
cout << "Yes";
} else {
cout << "No";
}
return 0;
}
| #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<deque>
#include<iomanip>
#include<tuple>
#include<cassert>
#include<set>
#include<complex>
#include<numeric>
#include<functional>
using namespace std;
typedef long long int LL;
typedef pair<int,int> P;
typedef pair<LL,int> LP;
const int INF=1<<30;
const LL MAX=1e9+7;
void array_show(int *array,int array_n,char middle=' '){
for(int i=0;i<array_n;i++)printf("%d%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(LL *array,int array_n,char middle=' '){
for(int i=0;i<array_n;i++)printf("%lld%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){
if(vec_n==-1)vec_n=vec_s.size();
for(int i=0;i<vec_n;i++)printf("%d%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}
void array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){
if(vec_n==-1)vec_n=vec_s.size();
for(int i=0;i<vec_n;i++)printf("%lld%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}
int main(){
LL n,m;
int i,j,k;
LL a,b,c;
int s=0;
cin>>n>>m;
for(a=1;a<=1e6+7;a++){
if(m%a)continue;
b=m/a;
if(b<a)break;
if(a+b==n)s++;
}
if(s)cout<<"Yes"<<endl;
else cout<<"No"<<endl;
} |
// Problem: C - Compass Walking
// Contest: AtCoder - AtCoder Beginner Contest 198
// URL: https://atcoder.jp/contests/abc198/tasks/abc198_c
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define vi vector<int>
#define pii pair<int, int>
#define pll pair<long long, long long>
const int p = 1e9 + 7;
const int lim = 1e6;
// //Number theory
// int facto[lim];
// void prefacto(){
// facto[0]=0;
// for(int i=1; i<=lim; i++)
// facto[i] = (facto[i-1]*i)%p;
// }
// //Graph theory
// vi status(lim, 0);
// vi adj[lim];
// void edgeInput(int m){
// for(int i=1; i<=m; i++){
// int a, b;
// cin>>a>>b;
// adj[a].push_back(b);
// adj[b].push_back(a);
// }
// }
//Binary search
bool check(int elem){
return true;
}
void bsearch(){
int lo=0, hi=1e9, res=-1, mid;
while(lo<=hi){
mid = lo + (hi-lo)/2;
if(check(mid)){
res = mid;
hi = mid-1;
}else{
lo = mid+1;
}
}
}
//Custom comparator
struct Node{};
struct Compare{
bool operator() (const Node &a, const Node &b){
return true;
}
};
bool compare(const Node &a, const Node &b){
return true;
}
void pre_io(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
}
int32_t main()
{
pre_io();
ll r, x, y;
cin>>r>>x>>y;
ll dsq = x*x + y*y;
ll divo = r*r;
ll quo = dsq/divo, rt = sqrt(quo);
if(rt*rt*divo==dsq){
cout<<rt;
}else{
cout<<rt+1+(rt==0);
}
} | #include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <numeric>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using db = double; using str = string;
using pi = pair<int,int>;
using vi = vector<int>; using vs = vector<str>;
using vl = vector<ll>; using vd = vector<double>;
using vvi = vector<vector<int>>;
using vvs = vector<vector<string>>;
using vvl = vector<vector<ll>>;
using vvd = vector<vector<double>>;
using pqi = priority_queue<int>;
using rpqi = priority_queue<int, vi, greater<int>>;
using umi = unordered_map<int, int>;
using usi = unordered_set<int>;
#define PB push_back
#define PF push_front
#define PK pop_back()
#define PT pop_front()
#define EB emplace_back
#define LB lower_bound
#define UB upper_bound
#define UM unordered_map
#define US unordered_set
#define PQ priority_queue
#define VT vector
#define se second
#define fi first
#define rt return
#define ln "\n"
#define ctu continue
#define F1(e) for(int i=0;i<(e);++i)
#define F2(i,e) for(int i=0;i<(e);++i)
#define F3(i,b,e) for(int i=(b);i<(e);++i)
#define F4(i,b,e,s) for(int i=(b); (s)>0?i<(e):i>(e); i+=(s))
#define Sum(a) accumulate((a).begin(), (a).end() , 0ll);
#define Min(a) *std::min_element((a).begin(), (a).end())
#define Max(a) *std::max_element((a).begin(), (a).end())
#define Sort(c) sort((c).begin(), (c).end())
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define each(x,a) for(auto& x : a)
#define all(c) (c).begin(), (c).end()
#define rall(a) rbegin(a),rend(a)
#define mst(a,x) memset(a, x, sizeof(a))
#define to_unique(a) a.erase(unique(begin(a), end(a)), end(a))
#define bct(x) (__builtin_popcountll(x))
#define in(x, a) a.find((x)) != a.end()
#define sz(x) ((int)(x).size())
#define sort_unique(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define isok(x, y) (x >= 0 && x < n && y >= 0 && y < m && !vis[x][y])
const ll mod = 1e9 + 7;
const double PI=acos(-1.0);
const int fx[4] = {1, 0, -1, 0}, fy[4] = {0, 1, 0, -1};
const int dx[8]={-1, -1, 0, 1, 1, 1, 0, -1}, dy[8]={0, 1, 1, 1, 0, -1, -1, -1};
template<class A> void read(VT<A>& v);
template<class T> void read(T& x) { cin >> x; }
template<class H, class... T> void read(H& h, T&... t) {
read(h); read(t...);
}
template<class A> void read(VT<A>& x) {
each(a, x) read(a);
}
template<class A> void wte(A x) {cout << x;}
template<class H, class... T> void wte(const H& h, const T&... t) {
wte(h); cout<<' '; wte(t...);
}
template<class A> void wte(VT<A>& x) {
bool f=1;
each(a, x){
if(f==1) {
wte(a); f=0;
} else wte(' '),wte(a);
}
}
int main() {
IOS;
vi a(3);
read(a);
Sort(a);
if(a[0]==a[1]) cout<<a[2]<<ln;
else if(a[2]==a[1]) cout<<a[0]<<ln;
else cout<<0<<ln;
return 0;
} |
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
//using vi = vector<int>;
int start_time;
int si, sj;
int highscore;
string best_S = "";
vector<vector<int>> t(50, vector<int>(50));
vector<vector<int>> p(50, vector<int>(50));
vector<int> near_i = {1,-1,0,0};
vector<int> near_j = {0,0,1,-1};
vector<char> near = {'D','U','R','L'};
void dfs(int pi, int pj, int score, string &S, vector<bool> &used){
if(clock() - start_time > 190 * CLOCKS_PER_SEC / 100){
return;
}
if(used.at(t.at(pi).at(pj))){
return;
}
used.at(t.at(pi).at(pj)) = true;
score += p.at(pi).at(pj);
if(score > highscore){
best_S = S;
highscore = score;
}
rep(i, 4){
if(pi + near_i.at(i) >= 50 || pi + near_i.at(i) < 0 || pj + near_j.at(i) >= 50 || pj + near_j.at(i) < 0){
continue;
}
S += near.at(i);
dfs(pi + near_i.at(i), pj + near_j.at(i), score, S, used);
S.erase(S.size()-1);
}
score -= p.at(pi).at(pj);
used.at(t.at(pi).at(pj)) = false;
}
int main(){
cin>>si>>sj;
rep(i,50)rep(j,50){
cin >> t.at(i).at(j);
}
rep(i,50)rep(j,50){
cin >> p.at(i).at(j);
}
start_time = clock();
vector<bool> used(2500, false);
string empty_S;
dfs(si,sj,0,empty_S,used);
//cout << start_time << endl;
cout << best_S << endl;
} | #include <iostream>
#include <vector>
#include <queue>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
#define rep(i, n) for(int i = 0; i < n;++i)
#define reps(i, s, n) for(int i = s; i < n;++i)
const int MAX_W = 50, MAX_H = 50;
int dx[4] = {+1,0,-1,0};
int dy[4] = {0,+1,0,-1};
char dm[4] = {'R', 'D', 'L', 'U'};
vvi v(MAX_H, vi(MAX_W, 0)), T(MAX_H, vi(MAX_W, 0)), P(MAX_H, vi(MAX_W, 0));
int f_w(int sx, int sy){
int allS=P[sy][sx];
v[sy][sx] = 1;
rep(i,4){
int ssx = sx + dx[i], ssy = sy + dy[i];
if(ssx>=0 && ssx<MAX_W && ssy>=0 && ssy<MAX_H && T[sy][sx]==T[ssy][ssx]) v[ssy][ssx]=1;
}
queue<pair<int,int>> Q;
Q.push(make_pair(sx, sy));
while(!Q.empty()){
int px = Q.front().first, py = Q.front().second;
Q.pop();
v[py][px] = 1;
allS += P[py][px];
rep(i,4){
int ppx = px + dx[i], ppy = py + dy[i];
if(ppx>=0 && ppx<MAX_W && ppy>=0 && ppy<MAX_H && T[py][px]==T[ppy][ppx]) v[ppy][ppx]=1;
}
int tx = px, ty = py, s=0, w=-1;
rep(i,4){
int ttx = px + dx[i], tty = py + dy[i];
if(ttx>=0 && ttx<MAX_W && tty>=0 && tty<MAX_H && v[tty][ttx]==0){
if(s<P[tty][ttx]){
s = P[tty][ttx];
tx = ttx;
ty = tty;
w = i;
}
}
}
if(s>0){
Q.push(make_pair(tx, ty));
cout<< dm[w];
}
}
return allS;
}
int main() {
int si, sj;
cin >> si >> sj;
rep(i, MAX_H)rep(j, MAX_W) cin >> T[i][j];
rep(i, MAX_H)rep(j, MAX_W) cin >> P[i][j];
f_w(sj, si);
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 1e9
#define linf 1e18
#define BASE 1000000
#define EPS 1e-10
#define PI acos(-1)
#define pii pair<int,int>
#define pdi pair<double,int>
#define vi vector<int>
#define fi first
#define se second
#define ALL(x) (x).begin(), (x).end()
#define ms(x,val_add) memset(x, val_add, sizeof(x))
#define pb(x) push_back(x)
#define make_unique(x) sort(ALL(x)) ; x.erase( unique(ALL(x)), x.end()) ;
#define dbg(x) do { cout << #x << " = " << x << endl; } while(0)
#define mp(x, y) make_pair(x, y)
#define minimize(x, y) x = min(x, y)
#define maximize(x, y) x = max(x, y)
/*** IMPLEMENTATION ***/
bool exitInput = false;
int ntest = 1, itest = 1 ;
const int dx[4] =
{
-1, 0, 1, 0
};
const int dy[4] =
{
0, 1, 0, -1
};
const ll Mod = 1000000007LL;
const int maxn = 200000 + 5;
const int maxv = 500 + 5;
const int maxe = 10000 + 5;
void addMod(ll &x, ll y)
{
x = (x + y) % Mod;
}
ll modPow(ll x, ll p)
{
if(p == 0)
return 1;
ll r = modPow(x, p / 2);
r = (r * r) % Mod;
if(p & 1)
r = (r * x) % Mod;
return r;
}
string s;
int n, K;
ll fact[17], choose[17][17];
ll woz[maxn][17], wz[maxn][17];
ll f[maxn][17]; // handle missing
ll res = 0;
int main()
{
#ifdef HOME
freopen( "input.txt", "r", stdin );
freopen( "output.txt", "w", stdout );
#endif // HOME
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << setprecision(10);
cout << fixed;
fact[0] = 1;
for(int i = 1; i <= 15; ++i)
fact[i] = (fact[i-1] * i) % Mod;
for(int i = 0; i <= 15; ++i)
choose[i][0] = choose[i][i] = 1;
for(int i = 1; i <= 15; ++i)
for(int j = 1; j < i; ++j)
choose[i][j] = (choose[i-1][j-1] + choose[i-1][j]) % Mod;
cin >> s >> K;
n = s.size();
// First step: count numbers with smaller length
woz[0][0] = 1;
for(int h = 1; h < n; ++h)
{
woz[h][1] = 1;
for(int k = 2; k <= K && k <= h; ++k)
{
woz[h][k] = (k * (woz[h-1][k-1] + woz[h-1][k])) % Mod;
wz[h][k] = ((k-1) * (woz[h-1][k-1] + woz[h-1][k])) % Mod;
}
addMod(res, choose[15][K] * woz[h][K]);
addMod(res, choose[15][K-1] * wz[h][K]);
}
//cout << "first step, res = " << res << endl;
// Second step: count numbers with equal length
for(int h = 0; h < n; ++h)
{
f[h][0] = modPow(K, h);
}
for(int h = 1; h < n; ++h)
{
for(int k = 1; k <= K && k <= h; ++k)
{
f[h][k] = (f[h-1][k-1] * k + f[h-1][k] * (K - k)) % Mod;
}
}
int cnt[16] ={0};
for(int i = 0; i < n; ++i)
{
int h = n - 1 - i;
int val = ('A' <= s[i] && s[i] <= 'Z'? s[i] - 'A' + 10 : s[i] - '0');
int lo = (i == 0 ? 1 : 0);
int hi = (i == n - 1 ? val : val - 1);
for(int k = lo; k <= hi; ++k)
{
cnt[k]++;
int cnt_existed = 0;
for(int x = 0; x <= 15; ++x)
cnt_existed += cnt[x] > 0;
int cnt_missing = K - cnt_existed;
if(cnt_missing >= 0)
{
addMod(res, choose[16 - cnt_existed][cnt_missing] * f[h][cnt_missing]);
}
cnt[k]--;
}
cnt[val]++;
}
cout << res << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(), x.end()
#define wa(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define int ll
const int mod = 1e9 + 7;
const int N = 2e5 + 5;
int a[N], cnt = 0;
int dp[N][20];
bool vis[20];
int mult(int x, int y){
return ((x % mod) * (y % mod)) % mod;
}
int sum(int x, int y){
return ((x % mod) + (y % mod)) % mod;
}
void solve(){
string s; cin >> s;
int k; cin >> k;
int n = s.size();
for(int i = 1; i <= n; ++i){
if('0' <= s[i - 1] && s[i - 1] <= '9')
a[i] = s[i - 1] - '0';
else
a[i] = s[i-1] - 'A' + 10;
}
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= 16; ++j){
dp[i][j + 1] = sum(dp[i][j + 1], mult(dp[i - 1][j], (16 - j)));
dp[i][j] = sum(dp[i][j], mult(dp[i - 1][j], j));
}
if(i != 1) dp[i][1] = sum(dp[i][1], 15);
for(int j = (int) (i == 1); j < a[i]; ++j){
dp[i][cnt + !vis[j]]++;
}
if(!vis[a[i]]) vis[a[i]] = 1, ++cnt;
}
cout << ( sum(dp[n][k], (cnt == k)) % mod) << "\n";
}
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int test = 1;
while(test--) solve();
}
|
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false); cin.tie(0);
#define FOR(i,s,n) for(int i = (s); i < (int)(n); i++)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) for(int i = (n); i >= 0; i--)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
#define ATYN(n) cout << ( (n) ? "Yes":"No") << '\n';
#define CFYN(n) cout << ( (n) ? "YES":"NO") << '\n';
#define OUT(n) cout << (n) << '\n';
using ll = long long;
using ull = unsigned long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pis = pair<int,string>;
const int N = 50;
const int LIMIT = 1e7;
const vector<int> dx{0,1,0,-1}, dy{-1,0,1,0};
const vector<char> D{'U','R','D','L'};
int cnt = 0;
vector<vector<int>> t,p;
bool in_range(const pii &pl) {
if (pl.first < 0 || pl.first >=N) return false;
if (pl.second < 0 || pl.second >=N) return false;
return true;
}
int xy_to_t(const pii &pl) {
return t[pl.first][pl.second];
}
pis dfs(pii &pos,vector<bool> &tile) {
pis ret = {0,""};
cnt++;
if (cnt > LIMIT) return ret;
if (!in_range(pos)) return ret;
if (tile[xy_to_t(pos)]) return ret;
tile[xy_to_t(pos)] = true;
pis tmp;
int best = -1;
REP(i,D.size()) {
pos.first += dy[i];
pos.second += dx[i];
tmp = dfs(pos,tile);
pos.first -= dy[i];
pos.second -= dx[i];
if (ret.first < tmp.first) {
best = i;
swap(ret,tmp);
}
}
tile[xy_to_t(pos)] = false;
ret.first += p[pos.first][pos.second];
ret.second.push_back(D[best]);
return ret;
}
int main(void)
{
IOS
pii s;
cin >> s.first >> s.second;
REP(i,N) {
vector<int> t1(N);
REP(j,N) cin >> t1[j];
t.push_back(t1);
}
REP(i,N) {
vector<int> p1(N);
REP(j,N) cin >> p1[j];
p.push_back(p1);
}
vector<bool> tile(N*N);
pis ans = dfs(s,tile);
reverse(ALL(ans.second));
ans.second.pop_back();
OUT(ans.second)
return 0;
} |
//================code===================//
//#define TLE
#ifdef TLE
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#endif
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <ctime>
#include <random>
#include <chrono>
//#include <stdint.h>
#define ci(t) cin>>t
#define co(t) cout<<t
#define LL long long
#define ld long double
#define fa(i,a,b) for(LL i=(a);i<(LL)(b);++i)
#define fd(i,a,b) for(LL i=(a);i>(LL)(b);--i)
#define setp pair<int,ld>
#define setl pair<LL,LL>
#define micro 0.000000000001
using namespace std;
LL gcd(LL a, LL b)
{
if (!(a && b)) return max(a, b);
return a % b ? gcd(b, a % b) : b;
}
#ifdef OHSOLUTION
#define ce(t) cerr<<t
#define AT cerr << "\n=================ANS=================\n"
#define AE cerr << "\n=====================================\n"
#define DB(a) cerr << __LINE__ << ": " << #a << " = " << (a) << endl;
#define __builtin_popcount __popcnt
#define __builtin_popcountll __popcnt64
#define LLL LL
#else
#define AT
#define AE
#define ce(t)
#define LLL __int128
#endif
pair <int, int> vu[9] = { {0,1},{1,0},{0,-1} ,{-1,0},{-1,0},{-1,-1}, {0,-1} , {1,-1},{-1,-1} }; //RDLU EWSN
template<typename T, typename U> void ckmax(T& a, U b) { a = a < b ? b : a; }
template<typename T, typename U> void ckmin(T& a, U b) { a = a > b ? b : a; }
struct gcmp { bool operator()(LL a, LL b) { return a < b; } bool operator()(setl& a, setl& b) { return a.second < b.second; } };
struct lcmp { bool operator()(LL a, LL b) { return a > b; } bool operator()(setl& a, setl& b) { return a.second > b.second; } };
const int max_v = 1e5 + 7;
const int max_k = 5e2 + 7;
const int bsz = (1ll << 10) + 7;
const int INF = 1e9 + 7;
const LL LNF = (LL)5e18 + 7ll;
LL mod = 998244353;//1e9 + 7;
template<typename T, typename U> void MOD(T& a, U b) { a += b; if (a >= mod) a -= mod; };
int main()
{
#ifdef OHSOLUTION
freopen("input.txt", "r", stdin);
#endif
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int a, b; ci(a >> b);
vector<int> vi(3);
vi[0] = 0;
vi[1] = 1;
vi[2] = 2;
if (a == b) co(a);
else
{
for (auto& x : vi)
{
if (x != a && x != b)
{
co(x);
break;
}
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
string S;
cin >> S;
char T;
cin >> T;
if (S == "Y")
{
T = toupper(T);
}
cout << T << endl;
} | #include <bits/stdc++.h>
using namespace std;
template<class T> void ckmin(T &a, T b) { a = min(a, b); }
template<class T> void ckmax(T &a, T b) { a = max(a, b); }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define pb push_back
#define mp make_pair
#define cotu cout
#define itn int
#define Red ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define F first
#define S second
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for(int i = 0; i < n; ++i)
#define repr(i,n) for(int i = n - 1; i >= 0; --i)
#define Rep(i, a, n) for(int i = (a); i <=(n); ++i)
#define repst(i, n) for(auto it = n.begin(); it != n.end(); ++it)
#define Repr(i, a, n) for(int i = (n); i >= (a); --i)
#define setp(x) fixed << setprecision(x)
#define ordered_set tree<pair<long double, int> , null_type,less<pair<long double, int> >, rb_tree_tag,tree_order_statistics_node_update>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
const int inf = int(1e9);
const ll INF = ll(1e16);
const int mod = inf + 7;
const int N = 3e5 + 555; // TL MB
const double PI = acos(-1.0);
void mul(ll &x, ll y){
x *= y;
if(x >= mod) x %= mod;
}
ll bp(ll a, ll n){
ll r = 1;
while(n){
if(n & 1) mul(r, a);
mul(a, a);
n >>= 1;
}
return r;
}
template<typename T_vector> // neal
void output_vector(const T_vector &v, bool add_one = false, int start = -1, int end = -1) {
if (start < 0) start = 0;
if (end < 0) end = int(v.size());
for (int i = start; i < end; i++)
cout << v[i] + (add_one ? 1 : 0) << (i < end - 1 ? ' ' : '\n');
}
void solve(){
char a, b;
cin >> a >> b;
if(a == 'Y') b = b - 'a' + 'A';
cout << b;
}
int main()
{
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
Red;
int T = 1;
// cin >> T;
for(int i = 1; i <= T; ++i){
solve();
}
return 0;
} |
#ifdef Rahul
#include "RAHUL.h"
#else
#include <bits/stdc++.h>
using namespace std;
#define error(...) 42;
#endif
#define SZ(v) int((v).size())
#define ALL(vec) begin(vec), end(vec)
typedef long long i64;
template<typename T> inline bool uax(T &x, T y) {return (y > x) ? x = y, true : false;}
template<typename T> inline bool uin(T &x, T y) {return (y < x) ? x = y, true : false;}
template<typename T> void kek(T ans) {cout << ans << endl; exit(0);}
#define Luv(...) [&] (auto &&u, auto &&v) { return __VA_ARGS__; }
const i64 INF = (i64) 4e18 + 42;
const int MOD = (int) 1e9 + 7;
int main() {
cin.tie(nullptr) -> sync_with_stdio(false);
int n, k; cin >> n >> k;
vector<int> a(n), b(n);
for (int &x : a) cin >> x;
for (int &x : b) cin >> x;
int m = k - n;
set<int> sa{0, m};
for (int i = 0; i < n; ++i) {
a[i] -= i + 1;
b[i] -= i + 1;
sa.insert(a[i]);
}
error(sa);
error(a);
error(b);
int ok = 1;
for (int x : b) {
ok &= (int) sa.count(x);
}
if (!ok) kek(-1);
a.insert(begin(a), 0);
b.insert(begin(b), 0);
a.push_back(m);
b.push_back(m);
i64 ans = 0;
int nx = -1, val = -1;
for (int i = 1, j = 1; i <= n; ++i) {
if (nx >= i) a[i] = val;
if (a[i] < b[i]) {
j = i;
while (1) {
if (nx >= j) a[j] = val;
if (a[j] >= b[i]) break;
++j;
}
nx = j;
val = a[j];
a[i] = val;
ans += j - i;
if (a[j] != b[i]) kek(-1);
}
error(i, a[i], b[i], ans);
}
error(a);
error(b);
for (int &x : a) x *= -1;
for (int &x : b) x *= -1;
reverse(ALL(a));
reverse(ALL(b));
nx = -1, val = -1;
for (int i = 1, j = 1; i <= n; ++i) {
if (nx >= i) a[i] = val;
if (a[i] < b[i]) {
j = i;
while (1) {
if (nx >= j) a[j] = val;
if (a[j] >= b[i]) break;
++j;
}
nx = j;
val = a[j];
a[i] = val;
ans += j - i;
if (a[j] != b[i]) kek(-1);
}
error(i, a[i], b[i], ans);
}
error(a);
error(b);
kek(ans);
}
| #include <bits/stdc++.h>
using namespace std;
#define per(i,a,b) for(int i=(b)-1;i>=a;i--)
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define de(x) cout<<#x<<" = "<<x<<"\n"
#define dd(x) cout<<#x<<" = "<<x<<" "
#define mes(p) memset(p,0,sizeof(p))
#define all(x) x.begin(),x.end()
#define rson mid+1,r,rt<<1|1
#define sz(x) (int)x.size()
#define lson l,mid,rt<<1
#define mp make_pair
#define pb push_back
#define ls (rt<<1)
#define rs (ls|1)
#define se second
#define fi first
typedef pair <int ,int > pii;
typedef vector <int > vi;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f;
const int mod = 1e9+7;
const int maxn= 1e5+5;
const int N = 1e6 + 6;
int a[maxn], b[maxn], c[maxn];
int main(){
int n, L;
scanf("%d%d", &n, &L);
rep(i,1,n+1) scanf("%d", &a[i]);
rep(i,1,n+1) scanf("%d", &b[i]);
a[n+1] = b[n+1] = L + 1;
rep(i,0,n+2) c[i] = a[i] - i;// dd(i),de(c[i]);
int f = 1;
ll ans = 0;
rep(i,1,n+1){
if(a[i] == b[i]) continue;
if(b[i] == b[i-1] + 1 || b[i] == a[i-1] + 1)
ans += (b[i-1] < a[i]);
else {
int k = lower_bound(c, c + n + 2, b[i] - i) - c;
if(k < i) k = upper_bound(c, c+n+2, b[i]-i) - c - 1;
if(0 <= k && k < n + 2 && c[k] == b[i] - i)
ans += abs(k - i);
else f = 0;
// dd(i), de(k);
}
// dd(i), de(ans);
//a[i] = b[i];
}
if(!f) ans = -1;
cout << ans;
return 0;
}
/*
10 1000
1 3 5 7 9 11 13 15 17 19
6 8 10 12 14 15 16 998 999 1000
10 1000
2 4 6 8 10 12 14 16 18 20
1 2 3 4 5 7 9 11 13 15
10 100
2 3 4 5 6 7 8 9 10 11
1 2 3 4 5 6 7 8 9 10
10 1000
1 3 5 7 9 11 13 15 17 19
6 7 8 9 10 14 15 16 998 999 1000
*/
|
#include <iostream>
#include <chrono>
#include <string>
#include <vector>
#include <algorithm>
class DNA {
private:
char base[20][20];
public:
void inputV(const int& i, const int& j, const std::string& s) {
for (int k = 0; k < (int)s.size(); k++) {
if (i + k < 20) base[i + k][j] = s[k];
else base[i + k - 20][j] = s[k];
}
}
void inputH(const int& i, const int& j, const std::string& s) {
for (int k = 0; k < (int)s.size(); k++) {
if (j + k < 20) base[i][j + k] = s[k];
else base[i][j + k - 20] = s[k];
}
}
void output() {
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
std::cout << base[i][j];
}
std::cout << std::endl;
}
}
//constructor
DNA() {
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
base[i][j] = '.';
}
}
}
};
int main() {
DNA alien;
int m;
std::cin >> m >> m;
std::vector<std::pair<int, std::string>> sequences(m, { 0, "" });
for (int i = 0; i < m; i++) {
std::cin >> sequences[i].second;
sequences[i].first = (int)sequences[i].second.size();
}
sort(sequences.begin(), sequences.end());
int now = 0;;
int k = 0;
while(now < 20) {
for (int i = 0; i < 20; i++) {
alien.inputH(i, now + 1, sequences[i].second);
}
k++;
now += sequences[k * 20 - 1].first;
}
alien.output();
return 0;
}
| #include <vector>
#include <iostream>
#include <string>
#include <algorithm>
#include <random>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
int main()
{
int N;
int M;
vector<string> S(M, "");
int L = 0;
cin >> N >> M;
for (int i = 0; i < M; i++)
{
cin >> S.at(i);
L = max(L, (int)S.at(i).size());
}
/*
todo ่ฆชๅๆงใฎ้ซใ้จๅๅๅๅฃซใใใใใใ็ตๅใใฆใใ
//็ตๅๅบฆ3ไปฅไธใฎไบใคใ็ตๅใใใ
for(int ketsugou = L; L > 2; L--){
for(int i = 0; i)
}
*/
//ใฉใณใใ ใซใใฆใฏใใใ
vector<vector<char>> board(20, vector<char>(20, '.'));
//todo:Sใไธฆในๆฟใใ
//mt19937 get_rand_mt;
//shuffle( S.begin(), S.end(), get_rand_mt );
//sort(S.begin(), S.end(), [](string &a, string &b){return a.size() >= b.size();});
for (string s : S)
{
int best_score = -1;
int best_pos = -1;
//ๆจช0็ธฆ1
bool best_muki_is_tate = true;
for (int i = 0; i < 400; i++)
{
int pos_i = i / 20;
int pos_j = i % 20;
int goodness = 0;
for (int s_index = 0; s_index < (int)s.size(); s_index++)
{
if (board.at((pos_i + s_index) % 20).at(pos_j) == '.')
{
continue;
}
else if (board.at((pos_i + s_index) % 20).at(pos_j) == s.at(s_index))
{
goodness += 1;
}
else
{
goodness = -1;
break;
}
}
if (goodness > best_score)
{
best_score = goodness;
best_pos = i;
best_muki_is_tate = false;
}
goodness = 0;
for (int s_index = 0; s_index < (int)s.size(); s_index++)
{
if (board.at(pos_i).at((pos_j + s_index) % 20) == '.')
{
continue;
}
else if (board.at(pos_i).at((pos_j + s_index) % 20) == s.at(s_index))
{
goodness += 1;
}
else
{
goodness = -1;
break;
}
}
if (goodness > best_score)
{
best_score = goodness;
best_pos = i;
best_muki_is_tate = true;
}
}
//boardใๆดๆฐใใใ
if (best_score == -1)
{
continue;
}
int pos_i = best_pos / 20;
int pos_j = best_pos % 20;
for (int s_index = 0; s_index < (int)s.size(); s_index++)
{
board.at((pos_i + !best_muki_is_tate * s_index)%20).at((pos_j + best_muki_is_tate * s_index)%20) = s.at(s_index);
}
}
for(int i = 0; i < 20; i++){
for(int j = 0; j < 20; j++){
cout << board.at(i).at(j);
}
cout << endl;
}
return 0;
}
|
// Fearless
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define pii pair <int, int>
#define pll pair <long long, long long>
#define PI (2.0 * acos(0.0))
#define testcase ll T; cin >> T; for (ll tc = 1; tc <= T; tc++)
#define info pair<int, pii>
#define mkp make_pair
const long long M = 1e5 + 10, M2 = 1e6 + 10, oo = 1e9 + 7;
void solve () {
ll a, b, c, d;
cin >> a >> b >> c >> d;
cout << fixed << setprecision(10) << 1.0 * (c * b + a * d) / (b + d + 0.0) << endl;
}
int main () {
//freopen("E:/newts/in.txt", "r", stdin);
//freopen("E:/newts/out.txt", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//testcase {
solve ();
//}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
if (N <= 3) {
cout << 1 << endl;
return 0;
}
char caa, cab, cba, cbb;
cin >> caa >> cab >> cba >> cbb;
if (caa == 'A' && cab == 'A') cout << 1 << endl;
else if (cab == 'B' && cbb == 'B') cout << 1 << endl;
else if (caa == 'B' && cab == 'A' && cba == 'A') {
int count = 1, count2 = 1, count3 = 1 ;
for (int i = 3; i < N; i++) {
count = (count2 + count3) % 1000000007;
count3 = count2;
count2 = count;
}
cout << count << endl;
}
else if (cab == 'B' && cba == 'B' && cbb == 'A') {
int count = 1, count2 = 1, count3 = 1 ;
for (int i = 3; i < N; i++) {
count = (count2 + count3) % 1000000007;
count3 = count2;
count2 = count;
}
cout << count << endl;
}
else {
int count = 1;
for (int i = 3; i < N; i++) {
count = (count * 2) % 1000000007;
}
cout << count << endl;
}
return 0;
} |
//abc202_c.cpp
//Fri Jun 18 20:08:47 2021
#include <bits/stdc++.h>
#define INTINF 2147483647
#define LLINF 9223372036854775807
#define MOD 1000000007
#define rep(i,n) for (int i=0;i<(n);++i)
using namespace std;
using ll=long long;
typedef pair<int,int> P;
int main(){
ll n;
cin >> n;
map<ll,ll> a;
vector<ll> b(n),c(n);
rep(i,n){
ll tmp;
cin >> tmp;
a[tmp]++;
}
rep(i,n) cin >> b[i];
rep(i,n) cin >> c[i];
ll ans = 0;
rep(i,n){
// cout << b[c[i]-1] << endl;
ans += a[b[c[i]-1]];
}
cout << ans << endl;
// printf("%.4f\n",ans);
} | #include <climits>
#include <iostream>
#include <unordered_map>
#include <map>
#include <vector>
#include <unordered_set>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <set>
#include <cmath>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int tt=1;
while(tt--){
int n;
cin>>n;
map<long long, multiset<long long>> balls;
for(int i=0;i<n;i++){
long long x, id;
cin>>x>>id;
if(balls.find(id)==balls.end()){
multiset<long long> locations;
locations.insert(x);
balls.insert(make_pair(id, locations));
continue;
}
(balls.find(id)->second).insert(x);
}
long long dp[balls.size()][2];
for(int i=0;i<balls.size();i++){
for(int j=0;j<2;j++){
dp[i][j]=0;
}
}
dp[balls.size()-1][0]=abs(*((balls.rbegin()->second).begin()));
dp[balls.size()-1][1]=abs(*((balls.rbegin()->second).rbegin()));
int i=balls.size()-2;
auto it=balls.rbegin();
it++;
for(it;it!=balls.rend();it++){
long long lastmn=*((prev(it)->second).begin());
long long lastmx=*((prev(it)->second).rbegin());
long long mn=*((it->second).begin());
long long mx=*((it->second).rbegin());
dp[i][0]=min(dp[i+1][1]+abs(mn-lastmn)+lastmx-lastmn, dp[i+1][0]+abs(lastmx-mn)+lastmx-lastmn);
dp[i][1]=min(dp[i+1][1]+abs(mx-lastmn)+lastmx-lastmn, dp[i+1][0]+abs(lastmx-mx)+lastmx-lastmn);
i--;
}
long long lastmx=*((balls.begin()->second).rbegin());
long long lastmn=*((balls.begin()->second).begin());
long long zero=0;
long long ans=min(dp[0][1]+abs(lastmn)+lastmx-lastmn, dp[0][0]+abs(lastmx)+lastmx-lastmn);
cout<<ans<<"\n";
}
}
//3 4 2 1 5 6
//1 2 3
|
#include<bits/stdc++.h>
using namespace std;
#define Max(a,b) ((a)<(b))&&((a)=(b))
#define Min(a,b) ((a)>(b))&&((a)=(b))
#define LL long long
#define db double
bool f1;
inline int rd() {
int res=0;
char ch;
while(ch=getchar(),ch<48||ch>57);
do res=(res<<1)+(res<<3)+(ch^48);
while(ch=getchar(),ch>47&&ch<58);
return res;
}
const int M=4e5+5;
int n;
int A[M];
struct info {
int x,num;
bool operator <(const info &_) const {
if(x^_.x)return x<_.x;
return num<_.num;
}
}P[M];
int l[M],r[M];
int topl,topr;
char S[M];
bool f2;
int main() {
// printf("%lf\n",(&f2-&f1)/1024.0/1024.0);
// freopen("a.in","r",stdin);
// freopen("a.out","w",stdout);
n=rd();
for(int i=1; i<=n*2; ++i) A[i]=rd(),P[i]=(info)<%A[i],i%>;
sort(P+1,P+n*2+1);
for(int i=1; i<=n*2; ++i) {
int ss=lower_bound(P+1,P+n*2+1,(info)<%A[i],i%>)-P;
if(ss<=n) {
if(topr)S[r[topr--]]='(',S[i]=')';
else l[++topl]=i;
}else {
if(topl)S[l[topl--]]='(',S[i]=')';
else r[++topr]=i;
}
}for(int i=1; i<=n*2; ++i)printf("%c",S[i]);
return 0;
}
| #include<iostream>
#include<iomanip>
#include<cmath>
#include<algorithm>
#include<numeric>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<stack>
#include<cassert>
//#include<atcoder/all>
#define fi first
#define se second
#define pb(x) emplace_back(x)
using namespace std;
using ll = long long;
using v_ll = vector <ll>;
using vv_ll = vector <v_ll>;
ll max(ll x,ll y){return x>y?x:y;}
ll min(ll x,ll y){return x<y?x:y;}
template<class T> bool chmax(T& x,const T& y){ if (x>=y) return false; x=y; return true;}
template<class T> bool chmin(T& x,const T& y){ if (x<=y) return false; x=y; return true;}
template<class T> void quit(T x){std::cout << x << "\n"; exit(0);}
template<typename T> void vshow(T x){for(auto itr = x.begin();itr!=x.end();itr++){std::cerr << *itr << " ";} std::cerr << "\n";}
template<typename T> void vvshow(T x){for(auto itr = x.begin();itr!=x.end();itr++){vshow(*itr);} std::cerr << "\n";}
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
ll N;
cin >> N;
v_ll A(2*N,0),B(2*N,0),C(2*N,0);
for(ll i=0;i<2*N;i++){
cin >> A[i];
B[i]=A[i];
// C[i]=B[i];
}
sort(B.begin(),B.end());
ll th = B[N];
ll cnt1=0;
for(ll i=0;i<2*N;i++){
if (A[i]>th)cnt1++;
}
for(ll i=0;i<2*N;i++){
if (A[i]>th || (A[i]==th && cnt1<N)){
if (A[i]==th) cnt1++;
A[i]=1;
}
else {A[i]=0;}
}
stack<ll>S;
string ans="";
for(ll i=0;i<2*N;i++){
if (S.empty()){ans.push_back('('); S.push(A[i]);}
else if (S.top()==A[i]) {ans.push_back('('); S.push(A[i]);}
else {ans.push_back(')'); S.pop();}
}
cout << ans << "\n";
}
|
#include<bits/stdc++.h>
using namespace std;
#define ri register int
typedef long long ll;
const int maxn=110,mod=998244353;
inline bool ckmin(int &x,const int &y){return x>y?x=y,1:0;}
inline bool ckmax(int &x,const int &y){return x<y?x=y,1:0;}
struct modint{
int val;
inline modint(int val_=0):val(val_){}
inline modint &operator=(int val_){return val=val_,*this;}
inline modint &operator+=(const modint &k){return val=val+k.val>=mod?val+k.val-mod:val+k.val,*this;}
inline modint &operator-=(const modint &k){return val=val-k.val<0?val-k.val+mod:val-k.val,*this;}
inline modint &operator*=(const modint &k){return val=1ll*val*k.val%mod,*this;}
inline modint &operator^=(int k){
modint ret=1,tmp=*this;
for(;k;k>>=1,tmp*=tmp)if(k&1)ret*=tmp;
return val=ret.val,*this;
}
inline modint &operator/=(modint k){return *this*=(k^=mod-2);}
inline modint &operator+=(int k){return val=val+k>=mod?val+k-mod:val+k,*this;}
inline modint &operator-=(int k){return val=val<k?val-k+mod:val-k,*this;}
inline modint &operator*=(int k){return val=1ll*val*k%mod,*this;}
inline modint &operator/=(int k){return *this*=((modint(k))^=mod-2);}
template<class T>friend modint operator+(modint a,T b){return a+=b;}
template<class T>friend modint operator-(modint a,T b){return a-=b;}
template<class T>friend modint operator*(modint a,T b){return a*=b;}
template<class T>friend modint operator/(modint a,T b){return a/=b;}
friend modint operator^(modint a,int b){return a^=b;}
friend bool operator==(modint a,int b){return a.val==b;}
friend bool operator!=(modint a,int b){return a.val!=b;}
inline bool operator!(){return !val;}
inline modint operator-(){return val?mod-val:0;}
inline modint &operator++(int){return *this+=1;}
};
using mi=modint;
mi ans,f[maxn*maxn][maxn],fac[maxn];
int a[maxn],m,n;
int main(){
scanf("%d",&n);
fac[0]=1;
for(ri i=1;i<=n;++i){
scanf("%d",a+i);
m+=a[i];
fac[i]=fac[i-1]*i;
}
if(m&1)return puts("0"),0;
m>>=1;
f[0][0]=1;
for(ri i=1;i<=n;++i)
for(ri j=m;j>=a[i];--j)
for(ri k=n;k;--k)
f[j][k]+=f[j-a[i]][k-1];
for(ri i=1;i<n;++i)ans+=f[m][i]*fac[i]*fac[n-i];
printf("%d",ans);
return 0;
} | #include <bits/stdc++.h>
#define f(i,j,k) for(long long i=j;i<k;i++)
#define f2(i,j,k) for(long long i=j;i>=k;i--)
#define ll long long
using namespace std;
const long long mod=1e9+7;
const ll infl = 1LL << 60;
ll H,W,A,B;
bool used[16][16];
ll dfs(ll x,ll y,ll a){
ll ans=0;
if(y==H){
return a==0;
}
if(x==W){
return dfs(0,y+1,a);
}
if(used[x][y]){
return dfs(x+1,y,a);
}
if(y+1<H&&!used[x][y+1]&&a>0){
used[x][y]=used[x][y+1]=true;
ans+=dfs(x+1,y,a-1);
used[x][y]=used[x][y+1]=false;
}
if(x+1<W&&!used[x+1][y]&&a>0){
used[x][y]=used[x+1][y]=true;
ans+=dfs(x+1,y,a-1);
used[x][y]=used[x+1][y]=false;
}
ans+=dfs(x+1,y,a);
return ans;
}
int main(){
ll h,w,a,b;
cin>>H>>W>>A>>B;
cout<<dfs(0,0,A)<<endl;
return 0;
}
|
#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\n"
template<class T> void chmax(T &a, const T b){ a = max(a, b); }
template<class T> void chmin(T &a, const T b){ a = min(a, b); }
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
// ifstream in("input.txt");
// cin.rdbuf(in.rdbuf());
ll a, b, c, d;
cin >> a >> b >> c >> d;
if (
(a == (b + c + d)) ||
(b == (a + c + d)) ||
(c == (a + b + d)) ||
(d == (a + b + c)) ||
((a + b) == (c + d)) ||
((a + c) == (b + d)) ||
((a + d) == (b + c))
) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
return 0;
}
| #include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <vector>
int main(void){
int a, b, c, d;
int x, y;
scanf("%d %d %d %d", &a, &b, &c, &d);
printf("%d", b - c);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main(){
int x, y; cin >> x >> y;
int high = max(x, y);
int low = min(x, y);
if(high - low < 3) cout << "Yes";
else cout << "No";
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#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 io ios::sync_with_stdio(false); cin.tie(0)
int main()
{
io;
ll t, n;
cin >> t >> n;
ll res = (100*n+(t-1))/t + n-1;
cout << res << endl;
return 0;
} |
/* Goal to be a Master */
#include <algorithm>
#include <bits/stdc++.h>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <cstdio>
#include <bitset>
#include <vector>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include<tuple>
using namespace std;
#define endl "\n"
#define fo1(i,n) for(int i=1;i<=n;i++)
#define min3(a,b,c) min(a,min(b,c))
#define s(v) (ll)v.size()
#define e(v) v.empty()
#define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define vi vector<int>
#define fi first
#define se second
#define db double
#define U unsigned
#define P pair<int,int>
#define ll long long
#define vll vector<ll>
#define pll pair<ll,ll>
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define fo(i,a,b) for(int i = a;i <= b;++i)
#define ro(i,a,b) for(int i = a;i >= b;--i)
#define ppll pair < pll , pll >
#define sd(x) scanf("%d",&x)
#define sld(x) scanf("%lld",&x)
#define INF 1e18
#define eps 0.00001
#define le length
#define debug(n1) cout << n1 << "\n"
int gcd(int a,int b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
int binex(int base,int pow)
{
int res=1;
while(pow){
if(pow%2)
{ res*=base;pow--;}
else
{base*=base;pow=pow/2;}
}
return res;
}
void sieve(int a[],int N)
{
for(int i=1;i<=N;i++)
a[i]=1;
a[1]=0;
for(int i= 2;i*i<=N;i++)
{
if(a[i])
{
for(int j=i*i;j<=N;j+=i)
a[j]=0;
}
}
}
string CONVERT_TO_BINARY(int s) {
string res = "";
while(s != 0) {
res += (char)('0' + s % 2);
s /= 2;
}
reverse(res.begin() , res.end());
return res;
}
int STOI(string s) {
int num = 0;
int po = 1;
for(int i=s.length()-1;i>=0;i--)
{
num += po * (s[i] - '0');
po *= 10;
}
return num;
}
int main()
{
IOS
int n,k;
cin>>n>>k;
int a[n];
int m[n+1]={0};
int h=0;
for(int i=0;i<n;i++)
{
cin>>a[i];h=max(h,a[i]);
m[a[i]]++;
}
ll ans=0;
if(m[0]<k)
{
int diff=k-m[0];
k-=diff;
}
for(int i=1;i<h;i++)
{
if(m[i]<k)
{
int diff=k-m[i];
ans+=i*diff;
k-=diff;
}
}
if(m[h]<k)
{
int diff=k-m[h];
ans+=h*diff;
k-=diff;
ans+=(h+1)*m[h];
}
else
{
ans+=(h+1)*k;
}
cout<<ans;
cout.precision(8);
cout << fixed;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define MOD int(1e9+7)
#define INF int(1e9)
#define LINF ll(1e18)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define P pair<ll,ll>
int n,k;
int a[310000];
int main(){
cin>>n>>k;
rep(i,n){
int val; cin>>val;
a[val]++;
}
int ans=0;
rep(i,n){
if(a[i]<k){
ans += a[i];
k=a[i];
}else{
ans += k;
}
}
cout<<ans<<endl;
} |
#include <math.h>
#include <algorithm>
#include <array>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <vector>
using namespace std;
using Graph = vector<vector<int>>;
using ll = long long;
typedef pair<ll, ll> P_ll;
typedef pair<int, int> P;
const ll INF_ll = 1e17;
const int INF = 1e8;
ll sqrt_ll(ll n) {
ll l = 0LL;
ll r = 1e9 + 10;
while (r - l > 1LL) {
ll m = (l + r) / 2;
if (m * m > n)
r = m;
else
l = m;
}
return l;
}
int main() {
long double X, Y, R;
cin >> X >> Y >> R;
const ll a = 10000;
X *= (long double)a;
Y *= (long double)a;
R *= (long double)a;
X = roundl(X);
Y = roundl(Y);
R = roundl(R);
ll x = (ll)X;
ll y = (ll)Y;
ll r = (ll)R;
ll s = (x - r) / a;
if (s * 10000 < x - r) {
s++;
}
ll ans = 0;
for (ll i = s * a; i <= x + r; i += a) {
ll p = r * r - (x - i) * (x - i);
ll q = sqrtl(p);
ll b = (y - q) / a;
if (b * a < y - q) {
b++;
}
ll c = (y + q) / a;
if (c * a > y + q) {
c--;
}
ans += (c * a - b * a) / a + 1;
}
cout << ans << endl;
return 0;
} | #include<bits/stdc++.h> // 57 68 "Baklol, Take it easy"
using namespace std;
#define rep(x, k, n) for(int x = (k); x < (n); ++x)
#define repr(x, k, n) for(int x = (k); x > (n); --x)
#define tr(it, a) for(auto it = (a).begin(); it != (a).end(); ++it)
#define ll long long
#define siz(x) ((int)(x).size())
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define o2(x) ( (x) * (x) )
#define all(x) (x).begin(), (x).end()
#define clr(x, k) memset(x, k, sizeof(x)) // 0, -1
#define sortall(x) sort(all(x))
#define sortalld(x) sort(all(x), greater<int>())
#define printv(v, x) rep(i, 0, x) cout << v[i] << " \n"[i+1==x];
#define print2dv(V, y, x) rep(j, 0, y) printv(V[j], x);
#define out(x) cout << ((x) ? "Yes" : "No") << '\n'
#define setbits(x) __builtin_popcountll(x)
#define inf INT_MAX
#define ninf INT_MIN
#define int long long // "be carefull"
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef vector<vb> vvb;
template <typename Arg1>
void debug_out(const char* name, Arg1&& arg1){
cout << name << " = " << arg1 << " ]" << '\n';
}
template <typename Arg1, typename... Args>
void debug_out(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');
cout.write(names, comma - names) << " = " << arg1 <<",";
debug_out(comma+1, args...);
}
#ifndef ONLINE_JUDGE
#define deb(...) cout << "[ ", debug_out(#__VA_ARGS__, __VA_ARGS__)
#else
#define deb(...)
#endif
const int mod = 1e9 + 7; // 998244353;
const int N = 3e5 + 5, M = N;
//-----------------------------------------------------------------------------
void Solve_main() {
int a, b, c, d;
cin >> a >> b >> c >> d;
cout << min({a, b, c, d});
}
//-----------------------------------------------------------------------------
int32_t main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
srand(chrono::high_resolution_clock::now().time_since_epoch().count());
// cout << setprecision(12) << fixed;
int tc = 1;
// cin >> tc;
for(int i = 0; i < tc; i++) {
Solve_main();
}
cerr << "[time:" << 1.0*clock()/CLOCKS_PER_SEC << "s] ";
return 0;
}
/*
-> edge cases.
-> Submit - right file.
-> Move on - if completely_stuck > 30 minute.
-> entire input - multiple testcases.
-> time complexity - 1 sec : 4e8 will work but risky.
-> space complexity - 256 mb : 6e7(int), 3e7(ll), 2e8(bool, char), will work.
-> mod.
-> builtin fuction - add ll.
-> Iterative > Recursive.
-> clear - global variables.
*/ |
#pragma GCC optimize ("Ofast")
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#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 PPC(x) __builtin_popcount(x)
#define MSB(x) (31 - __builtin_clz(x))
#define SZ(x) ((int)(x).size())
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define ithBit(m, i) ((m) >> (i) & 1)
#define ft first
#define sd second
#define kw(a) ((a) * (a))
#ifdef DEBUG
#include "debug.h"
#else
#define dbg(...) 0
#endif
using namespace std;
const int maxN = 1 << 19, mod = 1000000007;
template <class T1, class T2> inline void addMod(T1& a, T2 b) { a = (a + b) % mod; }
template <class T1, class T2> inline void multMod(T1& a, T2 b) { a = a * b % mod; }
long long sum(long long a, long long b)
{ return (a+b) * (b-a+1) / 2 % mod; }
void solve()
{
long long n, a, b;
scanf ("%lld%lld%lld", &n, &a, &b);
if (a > b)
swap(a, b);
long long all = kw(n-a+1) % mod;
multMod(all, kw(n-b+1) % mod);
long long x = n+1-min(a+b-1, n);
long long y = n+1-(b+1);
long long bad = sum(x, y);
multMod(bad, 2);
addMod(bad, (n-b+1) * (b-a+1));
multMod(bad, bad);
multMod(bad, mod-1);
long long res = all;
addMod(res, bad);
printf("%lld\n", res);
}
int main()
{
int t = 1;
scanf ("%d", &t);
FOR(tid, 1, t+1)
{
//printf("Case #%d: ", tid);
solve();
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
int main(){
int a,b,c;
cin>>a>>b>>c;
a%=10;
if(a==0||a==1||a==5||a==6){
cout<<a<<'\n';
return 0;
}else if(a==4||a==9){
if(b%2==0){
a=a*a%10;
}
cout<<a<<'\n';
return 0;
}else{
b%=4;
int t=b;
b=1;
for(int i=0;i<32;i++){
if(c>>i&1)b=b*t%4;
t=t*t%4;
}
b+=4;
int ret=1;
for(int i=0;i<b;i++)ret=ret*a%10;
cout<<ret<<'\n';
return 0;
}
} |
/*
* Author : Siddhant Khare
*/
#include <bits/stdc++.h>
using namespace std;
/******* All Required define Pre-Processors and typedef Constants *******/
#define ll long long
#define ld long double
#define arr array
#define si(t) scanf("%d", &t)
#define sl(t) scanf("%ld", &t)
#define sll(t) scanf("%lld", &t)
#define sc(t) scanf("%c", &t)
#define ss(t) scanf("%s", &t)
#define sf(t) scanf("%f", &t)
#define slf(t) scanf("%lf", &t)
#define pi(x) printf("%d\n", x)
#define pl(x) printf("%ld\n", x)
#define pll(x) printf("%lld\n", x)
#define pf(x) printf("%f")
#define sz(x) (int)(x).size()
#define mem(a, b) memset(a, (b), sizeof(a))
#define FOR(i, j, k, in) for (int i = j; i < k; i += in)
#define RFOR(i, j, k, in) for (int i = j; i >= k; i -= in)
#define REP(i, j) FOR(i, 0, j, 1)
#define RREP(i, j) RFOR(i, j, 0, 1)
#define all(x) x.begin(), x.end()
#define rall(cont) cont.end(), cont.begin()
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert(B <= A && A <= C)
#define sortall(x) sort(all(x))
#define mp make_pair
#define pb push_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define read(type) readInt<type>()
const double pi = acos(-1.0);
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef map<int, int> MPII;
typedef set<int> SETI;
typedef multiset<int> MSETI;
/**************************************/
/******** User-defined Function *******/
void solve()
{
ll n;
cin >> n;
ll a[n], b[n];
set<ll> st;
for (ll i = 0; i < n; i++)
{
cin >> a[i] >> b[i];
st.insert(a[i]);
st.insert(b[i]);
}
cout << st.size() << "\n";
// Algorithm
// Output
// pll(ans) // long long print
}
/**************************************/
/********** Main() function **********/
ll prt[400001];
ll find_set(ll v)
{
if (v == prt[v])
return v;
return find_set(prt[v]);
}
void union_sets(ll a, ll b)
{
a = find_set(a);
b = find_set(b);
if (a != b)
prt[b] = a;
}
signed main()
{
ll n, a, b;
cin >> n;
set<ll> res;
for (ll i = 0; i <= 400000; i++)
prt[i] = i;
map<ll, vector<ll>> v;
for (ll i = 0; i < n; i++)
{
cin >> a >> b;
if (a > b)
swap(a, b);
v[a].push_back(b);
}
for (auto i : v)
{
for (auto j : i.second)
{
if (res.find(find_set(j)) == res.end())
res.insert(find_set(j)), union_sets(i.first, j);
else
res.insert(find_set(i.first));
}
}
cout << res.size() << endl;
}
/******** Main() Ends Here *************/
| //#pragma GCC target("avx2")
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
//#pragma warning(disable : 4996)
//
////#define ATCODER
//#ifdef ATCODER
//#include<atcoder/all>
//using namespace atcoder;
//#endif
#include <algorithm>
#include <utility>
#include <vector>
#include <limits.h>
#include <math.h>
#include <time.h>
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cassert>
#include <numeric>
#include <type_traits>
using namespace std;
#ifdef _MSC_VER
#include"stdafx.h"
#include <intrin.h>
#define __builtin_popcount __popcnt
#define __builtin_popcountll __popcnt64
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << (H);
debug_out(T...);
}
#define DEBUG(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define DEBUG(...) 42
#endif
#define ll long long
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define REPR(i, n) for (ll i = (n) - 1; i >= 0; --i)
#define FOR(i, m, n) for (ll i = (m); i < (n); ++i)
#define VSORT(v) sort(v.begin(), v.end());
#define VREVERSE(v) reverse(v.begin(), v.end())
#define print(x) cout << (x) << '\n'
#define spa <<" "<<
#define lb(v, n) lower_bound(v.begin(), v.end(), (n))
#define ub(v, n) upper_bound(v.begin(), v.end(), (n))
//#define int long long
//#define double long double
#define all(x) (x).begin(), (x).end()
#define print_space(v) REP(i, v.size()) cout << v[i] << " \n"[i==(int)v.size()-1]
template <typename T1, typename T2> inline bool chmin(T1 & a, T2 b) { if (a > b) { a = b; return true; }return false; }
template <typename T1, typename T2> inline bool chmax(T1& a, T2 b) { if (a < b) { a = b; return true; }return false; }
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
std::random_device rd;
std::mt19937 mt(rd());
constexpr ll MOD = 1e9 + 7;
constexpr int MAX = 400040;
const double pi = acos(-1);
constexpr long double EPS = 1e-14;
const int dx[4] = { 0,0,-1,1 }, dy[4] = { -1,1,0,0 };
using ld = long double;
bool G[20][20];
int INF = 1e9;
int dp[1 << 18];
int N;
bool possible[1 << 18];
int ans = INF;
int rec(int bit) {
if (dp[bit] != INF)return dp[bit];
int n = __builtin_popcountll(bit);
if (n == 0)return dp[n] = 0;
if (possible[bit])return dp[bit] = 1;
vector<int>id(n);
int idx = 0;
REP(i, N) {
if (bit >> i & 1)id[idx++] = i;
}
int ret = n;
for (int nbit = bit;; nbit = (nbit - 1)&bit) {
if (nbit == bit)continue;
if (nbit == 0)break;
ret = min(ret, rec(nbit) + rec(bit^nbit));
}
//for (int k = n-1; k >= 1; k--) {
// vector<int>p(n);
// FOR(i, n - k, n)p[i] = true;
// do {
// int bb = 0;
// REP(i, n) {
// if (p[i])bb |= 1 << id[i];
// }
// int bbb = bit ^ bb;
// ret = min(ret, rec(bb) + rec(bbb));
// } while (next_permutation(all(p)));
//}
//
//DEBUG(b, ret);
return dp[bit] = ret;
}
void solve() {
cin >> N;
int M; cin >> M;
REP(i, 1 << N)dp[i] = INF;
if (M == 0) {
print(N);
return;
}
REP(i, M) {
int u, v; cin >> u >> v;
u--, v--;
G[u][v] = G[v][u] = true;
}
REP(i, N)G[i][i] = true;
REP(bit, 1 << N) {
int n = __builtin_popcount(bit);
if (n <= 1)possible[bit] = true;
bool ok = true;
REP(i, N) {
if (bit >> i & 1) {
FOR(j, i + 1, N) {
if (bit >> j & 1) {
if (!G[i][j]) {
ok = false;
break;
}
}
}
}
}
if (ok)possible[bit] = true;
}
//print(1); return;
ans = INF;
int bit = 0;
REP(i, N)bit |= 1 << i;
print(rec(bit));
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
//int q; cin >> q;
//while (q--)
solve();
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
constexpr int MX = 200005;
int p[MX];
map<int,int> mp[MX];
int find(int x){return (p[x] < 0 ? x : p[x] = find(p[x]));}
void uni(int x, int y){
x = find(x); y = find(y);
if(x != y){
if(p[x] > p[y]) swap(x, y);
p[x] += p[y];
p[y] = x;
for(auto [i,v]: mp[y]) mp[x][i] += v;
}
}
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
int n, q; cin >> n >> q;
fill(p, p+n, -1);
rep(i,n){
int c; cin >> c;
mp[i][c-1] = 1;
}
rep(i,q){
int t, a, b; cin >> t >> a >> b; a--; b--;
if(t == 1) uni(a,b);
else cout << mp[find(a)][b] << "\n";
}
} | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//turn on extra precision
//#pragma GCC target("fpmath=387")
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
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;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ordered_multiset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
#define mp make_pair
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define fi first
#define se second
#define fs first.second
#define ss second.second
#define ff first.first
#define sf second.first
#define newl '\n'
#define fbo find_by_order
#define ook order_of_key
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(),x.rend()
#define watch(x) cout << (#x) << " is : " << (x) << newl
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
vi dirx = {0,0,1,-1};
vi diry = {1,-1,0,0};
char to_upper (char x){
if( 97 <= int(x) && int(x) <= 122) return char(x-32);
if( 65 <= int(x) && int(x) <= 90) return x;
return -1;
}
char to_lower (char x){
if( 97 <= int(x) && int(x) <= 122) return x;
if( 65 <= int(x) && int(x) <= 90) return char(x+32);
return -1;
}
int numerize (char x){
if(48 <= int(x) && int(x) <= 57) return int(x-'0');
if(97 <= int(x) && int(x) <= 122) return int(x-96);
if(65 <= int(x) && int(x) <= 90) return int(x-64);
return -1;
}
bool isect (int l1, int r1, int l2, int r2){
pii p1 = {l1,r1};
pii p2 = {l2,r2};
if(p1>p2)swap(p1,p2);
return (p2.fi <= p1.se);
}
ll quickpow (ll num1, ll num2, ll MOD){
if(num2==0)return 1%MOD;
else if(num2==1)return num1%MOD;
else{
ll temp = quickpow (num1,num2>>1LL,MOD); ll res = ((temp%MOD) * (temp%MOD))%MOD;
if(num2&1) res = ((res%MOD)*(num1%MOD))%MOD; return res;
}
}
ll invmod (ll num, ll MOD){return quickpow (num,MOD-2,MOD);}
ll gcd (ll num1, ll num2){
if(num1 < num2) swap(num1,num2); ll num3 = num1 % num2 ;
while(num3 > 0){ num1 = num2; num2 = num3; num3 = num1 % num2;}
return num2;
}
ll lcm (ll num1 , ll num2){return (ll) (num1/__gcd(num1,num2))*num2;}
// end of Template
int par[200010],n,c[200010];
map <int,int> m[200010];
int findrep(int x) {return par[x] == x ? x : par[x] = findrep(par[x]);}
void join(int a, int b){
int repa = findrep(a);
int repb = findrep(b);
if(repa == repb) return;
if(m[repa].size() < m[repb].size()) swap(repa,repb);
for(auto i : m[repb]){
int x = i.fi; int y = i.se;
m[repa][x] += y;
}
m[repb].clear();
par[repb] = repa;
}
int main(){
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int q; cin >> n >> q;
for(int i = 1; i <= n; ++i){
cin >> c[i];
m[i][c[i]] = 1;
par[i] = i;
}
while(q--){
int t, a, b; cin >> t >> a >> b;
if(t == 1) join(a,b);
else{
int rep = findrep(a);
int ans = 0;
if(m[rep].count(b)) ans = m[rep][b];
else ans = 0;
cout << ans << newl;
}
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 1e9 + 7;
int run_test(){
int n;
cin >> n;
int maxi = 0, mini = mod;
for(int i = 0; i < n; i++){
int k;
cin >> k;
maxi = max(maxi, k);
}
for(int i = 0; i < n; i++){
int k;
cin >> k;
mini = min(mini, k);
}
cout << max(0, mini-maxi+1);
return 0;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int tt;
tt = 1;
while(tt--){
run_test();
}
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define N 100005
#define MOD 1000000007
#define dd double
#define vi vector<int>
#define vll vector<ll>
#define forr(i, n) for (ll i = 0; i < n; i++)
#define revf(i, n) for (ll i = n - 1; i >= 0; i--)
#define REP(i, a, b) for (ll i = a; i < b; i++)
#define rep1(i, b) for (ll i = 1; i <= b; i++)
#define inp(a, n) \
for (ll i = 0; i < n; i++) \
cin >> a[i]
#define outp(a, n) \
for (ll i = 0; i < n; i++) \
cout << a[i] << " "
#define gcd(a, b) __gcd((a), (b))
#define lcm(a, b) (((a) * (b)) / gcd((a), (b)))
#define pb push_back
#define mp make_pair
#define BINSC binary_search
#define BITOUT __builtin_popcount
#define mem(x, n) memset(x, n, sizeof(x))
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(NULL);
#define test \
ll t; \
cin >> t; \
while (t--)
#define cn cout << "\n";
int main()
{
fast
ll n;
cin>>n;
ll ar[n],br[n];
inp(ar,n);
inp(br,n);
set<ll>s;
for(ll i=1;i<=1000;i++)
s.insert(i);
ll q=0;
REP(i,1,1001)
{
forr(j,n)
{
if(ar[j]<=i&&i<=br[j])
q=1;
else
s.erase(i);
}
}
cout<<s.size();
} |
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define FORR(i,m,n) for(int i = m - 1; i >= n; i--)
#define ALL(v) v.begin(), v.end()
#define itn int
#define Yes() cout << "Yes" << endl
#define No() cout << "No" << endl
#define YES() cout << "YES" << endl
#define NO() cout << "NO" << endl
#define println(x) cout << x << endl
#define print(x) cout << x << " "
template<typename T, typename U>
inline bool CMAX(T &m, U x) { if (m < x) { m = x; return true; } return false; }
template<typename T, typename U>
inline bool CMIN(T &m, U x) { if (m > x) { m = x; return true; } return false; }
typedef long long lint;
typedef long double ldouble;
const int INF = 1e9;
const lint LINF = 1e18;
const int MOD = 1e9+7;
int main(){
lint n;
cin >> n;
int digit = 0;
lint ans = 0;
lint temp = n;
while(temp != 0){
temp /= 10;
digit++;
}
if(digit <= 3){
println(0);
return 0;
}
FOR(i,3,digit - 1){
ans += (i / 3) * (powl(10,i + 1) - powl(10,i));
}
ans += ((digit - 1) / 3) * (n - lint(powl(10,digit - 1)) + 1 );
println(ans);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main(){
double n, x0, y0, xh, yh;
cin >> n >> x0 >> y0 >> xh >> yh;
double xc = (x0 + xh)/2;
double yc = (y0 + yh)/2;
double arc = 3.1415926535897932384 * 2;
cout << setprecision(10) << ((x0 - xc)* cos(arc/n)) - ((y0 - yc) * sin(arc/n)) + xc <<" " <<
((x0 - xc) * sin(arc/n)) + ((y0 - yc) * cos(arc/n)) + yc << endl;
} |
#include<bits/stdc++.h>
using namespace std;
using ll = long long int;
using vi = vector<int>;
#define f(n) for(int i = 0; i < n; i++)
#define fj(n) for(int j = 0; j < n; j++)
#define fk(n) for(int k = 0; k < n; k++)
#define ci(n) cin >> n;
#define ic(n) int n; cin >> n;
#define lc(n) int n; cin >> n;
#define all(a) a.begin(), a.end()
#define pri(a, n) for(int i = 0; i < n; i++) cout << a[i] << " "; cout << "\n";
#define scl(a, n) ll a[n] = {0}; for(int i = 0; i < n; i++) cin >> a[i];
#define pb push_back
#define eb emplace_back
#define sc second
#define ft first
#define int long long
void prip(int a[], int n){f(n) cout << a[i] << ' '; cout << '\n';}
const int inf = 2e9, mxp = 10, maxn = 2e6, mod = 1e9 + 7;
int bp(int a, int n)
{
if(n == 0) return 1;
if(n & 1ll) return (a * bp(a, n - 1)) % mod;
int res = bp(a, n >> 1);
return (res * res) % mod;
}
int fc[maxn + 1], rc[maxn + 1];
int cnk(int n, int k)
{
if(k > n) return 0;
int ans = (fc[n] * rc[k]) % mod;
return (ans * rc[n - k]) % mod;
}
long long ans = 0;
void INVERSIONS(long long a[], int l, int r)
{
if (r - l <= 1)
{
return;
}
int m = (l + r) / 2;
INVERSIONS(a, l, m);
INVERSIONS(a, m, r);
long long b[r - l];
int ll = l, rr = m;
while (ll < m && rr < r)
{
if (a[ll] <= a[rr])
{
b[ll - l + rr - m] = a[ll];
ll++;
}
else
{
b[ll - l + rr - m] = a[rr];
rr++;
ans += m - ll;
}
}
while (ll < m)
{
b[ll - l + rr - m] = a[ll];
ll++;
}
while (rr < r)
{
b[ll - l + rr - m] = a[rr];
rr++;
}
for (int i = l; i < r; i++)
{
a[i] = b[i - l];
}
}
main()
{
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
ic(n)
int a[2 * n], ta[n];
f(n) cin >> a[i], a[i + n] = a[i], ta[i] = a[i];
INVERSIONS(ta, 0, n);
cout << ans << '\n';
for(int i = 0; i < n - 1; i++) {
ans += (n - a[i + n] - 1);
ans -= a[i];
cout << ans << '\n';
}
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define f first
#define s second
const int N = 3e5 + 100;
const int sq = 100;
const ll mod = 95542721;
const int inf = 1e7;
ll seg[4*N], a[N], ind[N], n;
void add(int x, int b = 0, int e = n, int ind = 1)
{
if(b + 1 == e)
{
seg[ind]++;
return;
}
int mid = (b + e) / 2;
if(x < mid)
add(x, b, mid, ind*2);
else
add(x, mid, e, ind*2+1);
seg[ind] = seg[ind*2] + seg[ind*2+1];
}
ll get(int l, int r, int b = 0, int e = n, int ind = 1)
{
if(r <= b || e <= l)
return 0;
if(l <= b && e <= r)
return seg[ind];
int mid =(b + e) / 2;
return get(l, r, b, mid, ind*2) + get(l, r, mid, e, ind*2+1);
}
int main()
{
ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 0; i < n; i++)
{
cin >> a[i];
ind[a[i]] = i;
}
ll inv = 0;
for(int i = n-1; i >= 0; i--)
{
add(ind[i]);
inv += get(0, ind[i]);
//cout << ind[i] << ' ' << get(0, ind[i]) << endl;
}
cout << inv << endl;
for(int i = 0; i < n-1; i++)
{
inv = inv - a[i] + (n - a[i] - 1);
cout << inv << endl;
}
return 0;
}
|
#include <iostream>
#include <bits/stdc++.h>
#include <string>
using namespace std;
void bmain()
{
int n; cin >> n;
long c[n][n];
for (int i=0;i<n;i++){
for (int j=0;j<n;j++){
cin >> c[i][j];
}
}
long a0[n];
for (int i=0;i<n;i++) a0[i]=c[0][i];
long b0[n];
b0[0]=0;
for (int i=1;i<n;i++){
b0[i]=c[i][0]-c[0][0];
for (int j=1;j<n;j++){
if (c[i][j]-a0[j]!=b0[i]){
cout << "No" << endl;
return;
}
}
}
long minb=b0[0];
for (int i=1;i<n;i++) minb=min(b0[i],minb);
if (minb<0){
for (int i=0;i<n;i++){
b0[i]-=minb;
a0[i]+=minb;
if (a0[i]<0){
cout << "No" << endl;
return;
}
}
}
cout << "Yes" << endl;
for (int i=0;i<n;i++) cout << b0[i] << " ";
cout << endl;
for (int j=0;j<n;j++) cout << a0[j] << " ";
cout << endl;
return;
}
void amain()
{
int n,m; cin >> n >> m;
string s;
long x[2];
x[0]=0;
x[1]=0;
for (int i=0;i<n;i++) {
cin>>s;
int cpt=0;
for (int j=0;j<m;j++){
if (s[j]=='1') cpt++;
}
x[cpt%2]++;
}
cout << x[0]*x[1] << endl;
return;
}
int main()
{
ios_base::sync_with_stdio(0);
bmain();
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rrep(i, n) for(int i = 1; i <= (int)(n); i++)
#define drep(i, n) for(int i = (n)-1; i >= 0; i--)
#define ALL(x) (x).begin(), (x).end()
#define dup(x,y) (((x)+(y)-1)/(y))
#define srep(i,s,t) for (int i = s; i < t; ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vs = vector<string>;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int mod = 1000000007;
// const int mod = 998244353;
int main() {
int n;
cin >> n;
vi l(n, INF), r(n, -INF);
rep(i, n) {
int x, c;
cin >> x >> c;
--c;
chmin(l[c], x);
chmax(r[c], x);
}
vector<P> d;
d.emplace_back(0,0);
rep(i, n) if (l[i] != INF) d.emplace_back(l[i], r[i]);
d.emplace_back(0,0);
vl dp(2);
for (int i = 1; i < d.size(); ++i) {
vl p(2, LINF);
swap(p, dp);
int l = d[i].first, r = d[i].second;
rep(j, 2) {
int x = j ? d[i-1].second : d[i-1].first;
chmin(dp[0], p[j] + abs(x-r) + r-l);
chmin(dp[1], p[j] + abs(x-l) + r-l);
}
}
cout << dp[0] << endl;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
template<typename T>
ostream& operator<<(ostream &os, vector<T> &v){
string sep = " ";
if(v.size()) os << v[0];
for(int i=1; i<v.size(); i++) os << sep << v[i];
return os;
}
template<typename T>
istream& operator>>(istream &is, vector<T> &v){
for(int i=0; i<v.size(); i++) is >> v[i];
return is;
}
#ifdef DBG
void debug_(){ cout << endl; }
template<typename T, typename... Args>
void debug_(T&& x, Args&&... xs){
cout << x << " "; debug_(forward<Args>(xs)...);
}
#define dbg(...) debug_(__VA_ARGS__)
#else
#define dbg(...)
#endif
int main() {
ios_base::sync_with_stdio(false);
cout << setprecision(20) << fixed;
int n; ll x; cin >> n >> x;
vector<ll> a(n);
cin >> a;
vector<ll> k(n);
for(int i=0; i<n-1; i++){
k[i] = a[i+1]/a[i];
}
k[n-1] = 1e18;
vector<ll> xs(n);
for(int i=n-1; i>=0; i--){
xs[i] = x/a[i];
x -= a[i] * xs[i];
}
ll dp[51][2];
dp[0][0] = 1;
dp[0][1] = 0;
for(int i=0; i<n; i++){
dp[i+1][0] = dp[i+1][1] = 0;
dp[i+1][0] += dp[i][0];
if(xs[i]>0){
dp[i+1][1] += dp[i][0];
}
if(xs[i]<k[i]-1){
dp[i+1][0] += dp[i][1];
}
dp[i+1][1] += dp[i][1];
}
cout << dp[n][0] << endl;
return 0;
} | #include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, n, s) for (int i = (s); i < (n); i++)
#define rrep(i, n) for (int i = (n - 1); i >= 0; i--)
#define rreps(i, n, s) for (int i = s; i >= n; i--)
using ll = long long;
using namespace std;
constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int MOD = 1000000007;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int a, b, c, d;
cin >> a >> b >> c >> d;
cout << b - c << endl;
return 0;
} |
//IQ134้ซ็ฅ่ฝ็ณปVtuberใฎ้ซไบ่
ไนใงใใ
//Twitter: https://twitter.com/takaichino
//YouTube: https://www.youtube.com/channel/UCTOxnI3eOI_o1HRgzq-LEZw
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF INT_MAX
#define LLINF LLONG_MAX
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define MODA 1000000007
#define MODB 998244353
template <typename T>
std::istream& operator>>(std::istream& is, std::vector<T>& vec) {
for (T& x: vec) { is >> x; }
return is;
}
int main() {
ll ans = 0;
ll tmp = 0;
int n; cin >> n;
vector<int> a(n), b(n), p(n);
cin >> a >> b >> p;
bool ch = true;
REP(i, n) p[i]--;
REP(i, n){
if(p[i] != i && a[i] <= b[p[i]]) ch = false;
}
if(ch == false){
cout << -1 << endl;
}
else{
priority_queue<pair<int, int>> q;
vector<pair<int, int> > answer;
REP(i, n){
q.push(make_pair(a[i], i));
}
while(!q.empty()){
int tai = q.top().first;
int num = q.top().second;
q.pop();
//cout << "human" << num << "(weight:" << tai << ") has bag" << p[num] << endl;
if(p[num] == num) continue;
else{
answer.push_back(make_pair(num, p[num]));
swap(p[num], p[p[num]]);
q.push(make_pair(tai, num));
}
}
cout << answer.size() << endl;
REP(i, answer.size()) cout << answer[i].first + 1 << " " << answer[i].second + 1 << endl;
}
//cout << ans << endl;
} | /*
-------------- | /
| | /
| | /
| * |/ | | ------ *
| | | | / \
| | |\ | | | |\ |
\ | | | \ | | | | \ |
\ | | | \ | | \ / \ |
V | | \ \__/| ----- \ |
*/
#include <bits/stdc++.h>
using namespace std;
#define EmiliaMyWife ios::sync_with_stdio(0); cin.tie(NULL);
using ll = int64_t;
using ull = uint64_t;
using ld = long double;
using uint = uint32_t;
const double EPS = 1e-8;
const int INF = 0x3F3F3F3F;
const ll LINF = 4611686018427387903;
const int MOD = 1e9+7;
/*--------------------------------------------------------------------------------------*/
signed main() { EmiliaMyWife
int n;
cin >> n;
vector<int> arr(n + 1), brr(n + 1), p(n + 1);
for(int i = 1; i <= n; i++)
cin >> arr[i];
for(int i = 1; i <= n; i++)
cin >> brr[i];
for(int i = 1; i <= n; i++)
cin >> p[i];
vector<pair<int, int>> owo(n);
for(int i = 0; i < n; i++)
owo[i] = {arr[i + 1], i + 1};
sort(owo.begin(), owo.end(), greater<pair<int, int>>());
vector<pair<int, int>> ans;
for(const auto &[a, id] : owo) {
if(p[id] == id)
continue;
if(a <= brr[p[id]])
return cout << "-1\n", 0;
while(p[id] != id) {
if(arr[p[id]] <= brr[p[p[id]]])
return cout << "-1\n", 0;
ans.push_back({id, p[id]});
swap(p[id], p[p[id]]);
}
}
cout << ans.size() << '\n';
for(auto [a, b] : ans)
cout << a << ' ' << b << '\n';
}
|
#include <time.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#include <iomanip>
#include <bitset>
using namespace std;
#define pi pair<int ,int>
long long n , m , p[5001][5001] , mod = 998244353 , kq = 0;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
srand (time(NULL));
int test = 1;
//cin >> test;
while (test--)
{
cin >> n >> m;
for (int i = 1 ; i <= n || i <= m ; i++)
{
p[i][0] = 1;
for (int e = 1 ; e <= n || e <= m ; e++)
{
p[i][e] = 1ll * p[i][e-1] * i % mod;
}
}
p[0][0] = 1;
for (int i = 1 ; i <= n ; i++)
{
for (int e = 1 ; e <= m ; e++)
{
long long res = p[m - e][i - 1] * p[m][n - i - 1] % mod;
res *= 1ll * (n-i);
res %= mod;
kq += res;
kq %= mod;
}
}
cout <<(1ll * n * p[m][n] % mod - kq + mod) % mod<<'\n';
}
}
| // In the name of Arceus
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cassert>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define For(i, l, r) for (i = int(l); i <= int(r); i++)
#define Fo(i, n) For(i, 1, n)
#define Rof(i, r, l) for (i = int(r); i >= int(l); i--)
#define Ro(i, n) Rof(i, n, 1)
#define clr(a) memset(a, 0, sizeof(a))
#define cpy(a, b) memcpy(a, b, sizeof(b))
#define fc(a, ch) memset(a, ch, sizeof(a))
typedef unsigned long long uint64;
typedef long long int64;
#define T1 template <class A>
#define T2 template <class A, class B>
#define T3 template <class A, class B, class C>
#define T4 template <class A, class B, class C, class D>
inline void read(char &x) {
do
x = getchar();
while (x <= ' ');
}
inline void read(char *s) {
char ch;
do
ch = getchar();
while (ch <= ' ');
while (ch > ' ') {
(*s) = ch;
s++;
ch = getchar();
}
(*s) = 0;
}
inline void read(std::string &s) {
char ch;
do
ch = getchar();
while (ch <= ' ');
while (ch > ' ') {
s.push_back(ch);
ch = getchar();
}
}
T1 inline void readint(A &x) {
bool neg = false;
char ch;
do
ch = getchar();
while (ch <= ' ');
if (ch == '-') {
neg = true;
ch = getchar();
}
x = 0;
while (ch > ' ') {
x = x * 10 + (ch & 15);
ch = getchar();
}
if (neg)
x = -x;
}
inline void read(int &x) { readint(x); }
inline void read(int64 &x) { readint(x); }
T2 inline void read(A &a, B &b) {
read(a);
read(b);
}
T3 inline void read(A &a, B &b, C &c) {
read(a);
read(b);
read(c);
}
T4 inline void read(A &a, B &b, C &c, D &d) {
read(a);
read(b);
read(c);
read(d);
}
inline void writeln() { putchar('\n'); }
T1 inline void writeint(A x) {
static char buf[20];
int top = 0;
if (!x) {
putchar('0');
return;
}
if (x < 0) {
putchar('-');
x = -x;
}
while (x) {
buf[++top] = (x % 10) | 48;
x /= 10;
}
while (top)
putchar(buf[top--]);
}
inline void write(int x) { writeint(x); }
inline void write(unsigned x) { writeint(x); }
inline void write(int64 x) { writeint(x); }
inline void write(uint64 x) { writeint(x); }
inline void write(char ch) { putchar(ch); }
inline void write(char *s) {
while(*s) {
putchar(*s);
s++;
}
}
inline void write(const char *s) { printf(s); }
T1 inline void write_(A x) {
write(x);
putchar(' ');
}
T1 inline void writeln(A x) {
write(x);
putchar('\n');
}
T2 inline void write(A a, B b) {
write_(a);
write(b);
}
T2 inline void writeln(A a, B b) {
write_(a);
writeln(b);
}
T3 inline void writeln(A a, B b, C c) {
write_(a);
write_(b);
writeln(c);
}
T4 inline void writeln(A a, B b, C c, D d) {
write_(a);
write_(b);
write_(c);
writeln(d);
}
#undef T1
#undef T2
#undef T3
#undef T4
// f[n][m]: ้ฟๅบฆไธบ n๏ผๆๅคงๅผไธ่ถ
่ฟ m
// f[n][m] = \sum_{i = 1}^n
// 1) (m - 1)^{n - 1} + m^{n - i} f[i - 1][m - 1] + (m - 1)^{i - 1} f[n - i][m]
// = n(m - 1)^{n - 1} + m^n \sum_{i = 1}^n m^{-i} f[i - 1][m - 1] + (m - 1)^{-1} \sum_{i = 1}^n (m - 1)^i f[n - i][m]
// g[m][n]
const int N = 5000 + 10;
const int MOD = 998244353;
int n0, m0;
int g[N][N];
int main() {
int n, m, mp;
int fx, fy;
int i;
read(n0, m0);
Fo(i, n0)
g[1][i] = 1;
For(m, 2, m0) {
fx = fy = 0;
mp = 1;
Fo(n, n0) {
fx = (1LL * fx * m + g[m - 1][n - 1]) % MOD;
fy = (1LL * fy * (m - 1) + g[m][n - 1]) % MOD;
g[m][n] = (g[m - 1][n] + 1LL * n * mp + fx + fy) % MOD;
mp = mp * (m - 1LL) % MOD;
}
}
writeln(g[m0][n0]);
return 0;
} |
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
#include <cmath>
using namespace std;
typedef long long ll;
const long double PI = (acos(-1));
const long long MOD = 1000000007;
static const int MAX_INT = std::numeric_limits<int>::max(); // 2147483647 = 2^31-1
static const long MAX_LONG = std::numeric_limits<long>::max();
static const ll MAX_LL = std::numeric_limits<long long>::max();
static const int INF = (1 << 30);
#define rep(i,n) REP(i,0,n)
#define REP(i,x,n) for(int i=x;i<n;++i)
///////////////////////////////////////////////////
// ------------------- utils ------------------- //
///////////////////////////////////////////////////
// change min/max
template<class T> inline bool chMin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chMax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
///////////////////////////////////////////////////
// ------------------- main -------------------- //
///////////////////////////////////////////////////
ll cnvtoN(ll ten, ll n) {
ll res = 0;
ll factor = 1;
while (ten > 0) {
res += (ten % n) * factor;
ten /= n;
factor *= 10;
}
return res;
}
//bool hasSeven(ll nbin) {
// ll tgtNb = nbin;
// while (tgtNb > 0) {
// const ll res = tgtNb % 10;
// if (res == 7) return true;
// tgtNb /= 10;
// if (tgtNb == 0) break;
// }
//}
void Main() {
ll N; cin >> N;
ll cnt = 0;
for (ll i = 1; i <= N; ++i) {
// ten
// if (hasSeven(i)) continue;
bool hasS = false;
ll tgtNb = i;
while (tgtNb > 0) {
const ll res = (tgtNb % 10);
if (res == 7) {hasS = true; break;};
tgtNb /= 10;
}
if (hasS) continue;
// e
ll ite = cnvtoN(i, 8);
// if (hasSeven(e)) continue;
tgtNb = ite;
while (tgtNb > 0) {
const ll res = (tgtNb % 10);
if (res == 7) {hasS = true; break;};
tgtNb /= 10;
}
if (hasS) continue;
++cnt;
}
cout << cnt << endl;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
std::cout << std::fixed << std::setprecision(15);
Main();
double tmp;
cin >> tmp;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main(){
long long n,ans=0;
cin>>n;
if (n>=1000)
ans+=n-999;
if (n>=1000000)
ans+=n-999999;
if (n>=1000000000)
ans+=n-999999999;
if (n>=1000000000000)
ans+=n-999999999999;
if (n>=1000000000000000)
ans+=n-999999999999999;
cout<<ans;
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 = 200000;
const int MOD = 998244353;
const int MAXPW = 300;
const int INV2 = (MOD + 1) / 2;
void inc(int& a, int b) { if ((a += b) >= MOD) a -= MOD; }
int n, mxpw;
int a[MAXN];
int ans[MAXPW + 1];
int sum[MAXPW + 1];
int choose[MAXPW + 1][MAXPW + 1];
void solve() {
REPE(i, mxpw) { choose[i][0] = choose[i][i] = 1; FOR(j, 1, i) choose[i][j] = (choose[i - 1][j - 1] + choose[i - 1][j]) % MOD; }
REPE(i, mxpw) sum[i] = 0;
REP(i, n) {
int x = 1;
inc(sum[0], x);
FORE(j, 1, mxpw) { x = (ll)x * a[i] % MOD; inc(sum[j], x); }
}
//printf("sum:"); REPE(i, mxpw) printf(" %d", sum[i]); puts("");
FORE(x, 1, mxpw) {
ans[x] = 0;
REPE(i, x) {
int num = ((ll)sum[i] * sum[x - i] - sum[x]) % MOD;
//printf("(%d,%d) -> %d*%d/2\n", i, x, choose[x][i], num);
ans[x] = (ans[x] + (ll)choose[x][i] * num % MOD * INV2) % MOD;
}
}
}
void run() {
scanf("%d%d", &n, &mxpw);
REP(i, n) scanf("%d", &a[i]);
solve();
FORE(i, 1, mxpw) printf("%d\n", ans[i]);
}
int main() {
run();
return 0;
}
| #include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <string>
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep_e(i, n) for(int i = 0; i <= n; i++)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
using namespace std;
int main() {
long long n;
string t;
cin >> n >> t;
long long ans = 0;
string s = "110";
int count = 1;
long long max = pow(10, 10);
if(t == "1"){
ans = max * 2;
}else if(t == "0"){
ans = max;
}else if(t == "00"){
ans = 0;
}else if(t == "01"){
ans = max - 1;
}else if(t == "10"){
ans = max;
}else if(t == "11"){
ans = max;
}else{ // 3ๆๅญไปฅไธ
while((int)s.size() - 3 < n){
s = s + "110";
count++;
}
rep(i, (int)s.size()){
if(s.substr(i, n) == t){
ans++;
}
}
if(ans == 0){
ans = 0;
}else{
if(t[0] == '0' and t[1] == '1' and t[2] == '1' and (int)t.size() % 3 == 2){
ans = max - (int)t.size() / 3 - 1;
}else if(t[0] == '0' and t[1] == '1' and t[2] == '1' and (int)t.size() % 3 != 2){
ans = max - (int)t.size() / 3;
}else if(t[0] == '1' and t[1] == '0' and t[2] == '1'){
ans = max - (int)t.size() / 3;
}else if(t[0] == '1' and t[1] == '1' and t[2] == '0' and (int)t.size() % 3 == 0){
ans = max - (int)t.size() / 3 + 1;
}else if(t[0] == '1' and t[1] == '1' and t[2] == '0' and (int)t.size() % 3 != 0){
ans = max - (int)t.size() / 3;
}
}
}
cout << ans << endl;
}
|
#include<bits/stdc++.h>
#define matsuri pair<int,int>
#define iris 1000000007
using namespace std;
vector<int> G[111];
matsuri arr[11111];
int aoi[111][111],c[111],ans[11111];
bool v[111];
void dfs(int a,int f)
{
v[a]=1;
for(int b:G[a])
{
if(b==f)
continue;
int e=max(aoi[a][b], aoi[b][a]);
if(ans[e]==0)
{
if(aoi[a][b])
ans[e]=1;
else
ans[e]=2;
}
if(!v[b])
dfs(b,a);
}
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n,m,i,a,b;
cin>>n>>m;
for(i=1;i<=m;i++)
{
cin>>a>>b;
arr[i]={a,b};
aoi[a][b]=i;
}
for(i=1;i<=n;i++)
{
cin>>c[i];
}
for(i=1;i<=m;i++)
{
a=arr[i].first;
b=arr[i].second;
if(c[a]==c[b])
{
G[a].emplace_back(b);
G[b].emplace_back(a);
}
else if(c[a]>c[b])
ans[i]=1;
else
ans[i]=2;
}
for(i=1;i<=n;i++)
if(!v[i])
dfs(i,0);
for(i=1;i<=m;i++)
{
if(ans[i]==1)
cout<<"->\n";
else
cout<<"<-\n";
}
return 0;
}
| #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
#define repn(i,n) for(int i=1;i<=n;++i)
#define LL long long
#define pii pair <int,int>
#define fi first
#define se second
#define pb push_back
#define mpr make_pair
using namespace std;
const LL MOD=1e9+7;
int n,m,c[110],depth[110],ans[10010];
vector <pair <pii,int> > g[110];
vector <pii> e;
bool vis[110];
void dfs(int pos,int par,int d)
{
vis[pos]=true;
depth[pos]=d;
bool bk=false;
rep(i,g[pos].size())
{
if(g[pos][i].fi.fi==par&& !bk)
{
bk=true;
continue;
}
if(vis[g[pos][i].fi.fi])
{
if(depth[g[pos][i].fi.fi]<d) ans[g[pos][i].fi.se]=(g[pos][i].se==0 ? 1:2);
}
else
{
ans[g[pos][i].fi.se]=(g[pos][i].se==0 ? 1:2);
dfs(g[pos][i].fi.fi,pos,d+1);
}
}
}
int main()
{
cin>>n>>m;
int x,y;
rep(i,m)
{
scanf("%d%d",&x,&y);
e.pb(mpr(x,y));
}
repn(i,n) scanf("%d",&c[i]);
rep(i,m)
{
if(c[e[i].fi]>c[e[i].se]) ans[i]=1;//->
else if(c[e[i].fi]<c[e[i].se]) ans[i]=2;//<-
else
{
g[e[i].fi].pb(mpr(mpr(e[i].se,i),0));
g[e[i].se].pb(mpr(mpr(e[i].fi,i),1));
}
}
repn(i,n)
{
if(vis[i]) continue;
dfs(i,0,1);
}
rep(i,m) puts(ans[i]==1 ? "->":"<-");
return 0;
} |
#define _LIBCPP_DEBUG 0
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<ll>>;
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;}
#define rep(i,n) for(ll i=0;i<ll(n);i++)
#define YESNO(T) if(T){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}
#define yesno(T) if(T){cout<<"yes"<<endl;}else{cout<<"no"<<endl;}
#define YesNo(T) if(T){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
const double pi = 3.14159265358979;
const ll ju = 10000000000;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(15);
ll n;
string s;
cin >> n >> s;
if (n == 1){
if (s == "0"){
cout << ju << endl;
return 0;
}
else{
cout << 2*ju << endl;
return 0;
}
}
if (n == 2){
if (s == "11"){
cout << ju << endl;
return 0;
}
else if (s == "10"){
cout << ju << endl;
return 0;
}
else if (s == "01"){
cout << ju - 1 << endl;
return 0;
}
else{
cout << 0 << endl;
return 0;
}
}
ll type = -1;
if (s[0] == '1' && s[1] == '1' && s[2] == '0') type = 1;
else if (s[0] == '1' && s[1] == '0' && s[2] == '1') type = 2;
else if (s[0] == '0' && s[1] == '1' && s[2] == '1') type = 3;
if (type == -1){
cout << 0 << endl;
return 0;
}
bool pos = true;
for (ll i = 3; i < n; i++){
if (s[i] != s[i-3]){
pos = false;
break;
}
}
if (!pos){
cout << 0 << endl;
return 0;
}
if (type == 1){
if (n %3 == 0){
cout << ju + 1 - n/3 << endl;
return 0;
}
else{
cout << ju - n/3 << endl;
return 0;
}
}
else if (type == 2){
cout << ju - n/3 << endl;
return 0;
}
else{
if (n %3 == 0 || n %3 == 1){
cout << ju - n/3 << endl;
return 0;
}
else{
cout << ju - 1 - n/3 << endl;
return 0;
}
}
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
string S = "110", T;
cin >> N >> T;
long long all= 10000000000;
long long res = 0;
if (T == "0") res = all;
else if (T == "1") res = all * 2;
else{
//110ใฎใฉใใใๅงใพใใใงๅ
จๆข็ดข
for (int i = 0; i < 3; ++i) {
//ๆถ่ฒปใใ110ใฎๆฐใไพใใฐใ0110ใฏi=2ใฎใจใใฎใฟใ ใใ(4+2+2)/3=2ใง2ๅๆถ่ฒปใ
//i=0ใชใ2่ถณใใฆใ็ตๆใฏๅคใใใชใใi=1ไปฅไธใชใ3ใใฒใจใคๅขใใใใใซ2ใใใใใใ
//2ใ่ถณใใฎใฏใ3ๅใใใใกใฎ1ๅใงใๆถ่ฒปใใฆใใใใใใใๆถ่ฒปใใใใฎใจ่ฆใชใใใใ
long long a = (T.size() + i + 2)/3;
//ๅซใพใใฆใใๅ ดๅใฎใใฎๅๆฐ
long long add = all - a + 1;
//ๅซใพใใฆใใใadd ใใใงใชใใใฐaddใใชใ
bool contain = true;
for(int j=0; j < T.size();j++){
if(T[j]!=S[(i+j)%3])contain=false;
}
if(contain) res+=add;
}
}
cout << res << endl;
} |
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define rep(i,N) for(int i=0;i<(int)N;i++)
static inline int Asc(const void *a,const void *b){return strcmp((char*)a,(char*)b);}
static inline int IN(void)
{
int x=0,f=0,c=getchar();while(c<48||c>57){f^=(c==45),c=getchar();}
while(c>47&&c<58){x=x*10+c-48,c=getchar();}return f?-x:x;
}
int main(void)
{
int N=IN(),check=0,len=0,sizeS=0,sizeT=0,idxS=0,idxT=0;char S[N][11],T[N][11],temp[11];
rep(i,N)
{
if(!scanf("%s",temp)){exit(EXIT_FAILURE);}
len=(int)strlen(temp);
if(temp[0]=='!'){len--;rep(j,len){T[sizeT][j]=temp[j+1];}if(len<10){T[sizeT][len]=0;}sizeT++;}
else{rep(j,len){S[sizeS][j]=temp[j];}if(len<10){S[sizeS][len]=0;}sizeS++;}
}
qsort(S,sizeS,11,Asc);qsort(T,sizeT,11,Asc);
while(idxS<sizeS&&idxT<sizeT)
{
check=strcmp(S[idxS],T[idxT]);
if(!check){return !puts(S[idxS]);}
else if(check<0){idxS++;}else{idxT++;}
}
return !puts("satisfiable");
} | #include <bits/stdc++.h>
using namespace std;
int main() {
string S;
cin >> S;
int N = (int) S.size();
for (int i = 0; i < N; i++) {
if (S.at(i) == '6') {
S.at(i) = '9';
}
else if (S.at(i) == '9') {
S.at(i) = '6';
}
}
for (int i = N - 1; i >= 0; i--) {
cout << S.at(i);
}
cout << endl;
return 0;
} |
#pragma GCC optimize(2)
#include<iostream>
#include<unordered_map>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<map>
#include<set>
#define Buff ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define rush() int Case = 0; int T; cin >> T; while(T--)
#define rep(i, a, b) for(int i = a; i <= b; i ++)
#define per(i, a, b) for(int i = a; i >= b; i --)
#define reps(i, a, b) for(int i = a; b; i ++)
#define clc(a, b) memset(a, b, sizeof(a))
#define Buff ios::sync_with_stdio(false)
#define readl(a) scanf("%lld", &a)
#define readd(a) scanf("%lf", &a)
#define readc(a) scanf("%c", &a)
#define reads(a) scanf("%s", a)
#define read(a) scanf("%d", &a)
#define lowbit(n) (n&(-n))
#define pb push_back
#define sqr(x) x*x
#define lson rt<<1
#define rson rt<<1|1
#define ls lson, l, mid
#define rs rson, mid+1, r
#define y second
#define x first
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int>PII;
const int mod = 1e9+7;
const double eps = 1e-6;
const int N = 1e6+7;
string s, t;
bool judge()
{
for(int i = 0; s[i]; i ++) if(s[i] != 'a') return false;
return true;
}
int main()
{
int T;
cin >> T;
while(T --)
{
cin >> s; t = s;
if(judge())
{
cout << -1 <<endl;
continue;
}
int res = mod;
for(int i = 0; s[i]; i ++)
{
s = t;
for(int j = i; s[j]; j ++)
{
swap(s[i], s[j]);
if(s > "atcoder")
{
res = min(res, j-i);
break;
}
}
}
cout << res <<endl;
}
return 0;
}
| #include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define FORX(i, a, b, x) for (ll i = (a); i <= (b); i+=x)
#define debug(x) cout <<" || "<< #x << " is= " << x <<endl; //use for debug
#define read(a) ll a; cin >> a;
#define FORD(i, a, b) for (ll i = (a); i >= (b); i--)
#define pb(a) push_back(a)
#define pf(a) push_front(a)
#define fast_io ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vpii vector<pair<int,int>>
#define pr pair<long double,long double>
#define vpll vector<pair<ll,ll>>
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend() //reverse of above;
#define sz(container) int((container).size())
#define cut(x) {cout<<x<<"\n"; continue;}
#define setprec(x) cout << fixed << showpoint << setprecision(15)<<x<<"\n";
#define time cout << clock() / double(CLOCKS_PER_SEC) << endl;
#define endl '\n';
#define mod 1000000007
#define mod1 998244353
#define pi 3.141592653589793238
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
//*s.find_by_order() ->returns an iterator to the k-th largest element (counting from zero);
//s.order_of_key() ->number of elements strictly smaller then our item;
//st.rbegin()->second; last element in set
//st.erase(prev(st.end())); delete last element in set;
//sort(a+1,a+n+1,[](ll x,ll y){return abs(x)>abs(y);});
//sort(v.begin(), v.end(), greater<int>()); -> not sure work for first also if second are equal;
//q=lower_bound(all(v[a[i]]),i)-v[a[i]].begin()+1;
// s.substr(start_ind,length);
// vpll shift={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}} ->shift operator;
//bool comp(pr a,pr b){return a.second<b.second;} // use for differentiate order of comparision;
// cntbit(x) __builtin_popcount(x); -> no. of bits in given no.;
//โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
const ll MAXN=2e7+5;
const ll inf=1e18+5;
int main()
{
fast_io;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
//โDonโt wish it were easier. Wish you were better.โ โ Jim Rohn;
ll n; cin>>n;
long double a[10];
FOR(i,1,4) cin>>a[i-1];
long double h,k,x,y,radius;
h=(a[0]+a[2])/2.0;
k=(a[1]+a[3])/2.0;
// shift p0 wrt h,k ant. clkwise;
long double theta=2*pi/(n*1.0);
//debug(theta);
a[0]-=h,a[1]-=k;
x=a[0]*cos(theta)-a[1]*sin(theta);
y=a[0]*sin(theta)+a[1]*cos(theta);
x+=h,y+=k;
setprec(x); setprec(y);
} |
#include<bits/stdc++.h>
using namespace std;
int main(){
int n; cin >> n;
vector<int> a(n), b(n), c(n);
for(int i = 0; i < n; ++i){cin >> a[i]; --a[i];}
for(int i = 0; i < n; ++i){cin >> b[i]; --b[i];}
for(int i = 0; i < n; ++i){cin >> c[i]; --c[i];}
vector<long long> aa(n), bc(n);
for(int i = 0; i < n; ++i){
aa[a[i]] += 1;
bc[b[c[i]]] += 1;
}
long long ans = 0;
for(int i = 0; i < n; ++i){ans += aa[i]*bc[i];}
cout << ans << endl;
} | #pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <complex>
#include <queue>
#include <set>
#include <unordered_set>
#include <list>
#include <chrono>
#include <random>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <stack>
#include <iomanip>
#include <fstream>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef vector<ll> vll;
typedef vector<int> vi;
ll MOD = 998244353;
double eps = 1e-12;
#define forn(i, e) for (ll i = 0; i < e; i++)
#define forsn(i, s, e) for (ll i = s; i < e; i++)
#define rforn(i, s) for (ll i = s; i >= 0; i--)
#define rforsn(i, s, e) for (ll i = s; i >= e; i--)
#define ln "\n"
#define dbg(x) cout << #x << " = " << x << ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define INF 2e18
#define fast_cin() \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define allrev(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
void solve()
{
int n;
cin >> n;
vi arr(n), b(n);
forn(i, n)
{
cin >> arr[i];
}
forn(i, n)
{
cin >> b[i];
}
map<int, int> m;
forn(i, n)
{
int x;
cin >> x;
--x;
int val = b[x];
m[val]++;
}
ll cnt = 0;
forn(i, n)
{
if (m.find(arr[i]) != m.end())
{
cnt += m[arr[i]];
}
}
cout << cnt << ln;
}
int main()
{
fast_cin();
ll t = 1;
// cin >> t;
for (int it = 0; it < t; it++)
{
// cout << "Case #" << it + 1 << ": ";
solve();
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
//ofstream cout("output.out");
#define fast() {ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);}
#define pb push_back
#define vi vector<int>
#define vl vector<ll>
#define vii vector<vector<int>>
#define mii map<int, int>
#define pii pair<int, int>
#define qii priority_que<int, vector<int>, greater<int>>
#define For(i, n) for(int i = 0; i < n; ++i)
#define For1(i, n) for(int i = 1; i <= n; ++i)
#define Forlr(i, l, r) for(int i = l; i <= r; ++i)
#define Forrl(i, r, l) for(int i = r; i >= l; --i)
#define o(a) { cout << #a << ":" << (a) <<" ";}
#define ov(a) {For(i, a.size()) cout << a[i] <<" ";}
#define ovv(a) {For(i, a.size()) {For(j, a[i].size()) cout << a[i][j] <<" ";cout <<"\n";}}
using ll = long long;
int n, m, k, q;
const int INF = 1e9 + 5;
const int nax = 1e5 + 5;
const ll mod = 1e9 + 7;
const int maxn = 3e5 + 7;
int main()
{
fast();
cin >> n;
vi a(n);
For(i, n) cin >> a[i];
unordered_map<int, int> g;
For(i, n){
for(int j = 1; j * j <= a[i]; ++j){
if(a[i] % j == 0){
g[j] = __gcd(a[i], g[j]);
g[a[i] / j] = __gcd(a[i], g[a[i] / j]);
}
}
}
int mi = *min_element(a.begin(), a.end());
int rs = 1;
for(auto i : g){
if(i.first == i.second && i.first < mi) rs++;
}
cout << rs <<"\n";
} | #include<bits/stdc++.h>
template <typename _Tp>void read(_Tp &x){
char ch(getchar());bool f(false);while(!isdigit(ch))f|=ch==45,ch=getchar();
x=ch&15,ch=getchar();while(isdigit(ch))x=x*10+(ch&15),ch=getchar();
if(f)x=-x;
}
template <typename _Tp,typename... Args>void read(_Tp &t,Args &...args){read(t);read(args...);}
const int N=105,mod=1000000007,inv2=(mod+1)/2;
typedef long long ll;
template<typename _Tp1,typename _Tp2>inline void add(_Tp1 &a,_Tp2 b){(a+=b)>=mod&&(a-=mod);}
template<typename _Tp1,typename _Tp2>inline void sub(_Tp1 &a,_Tp2 b){(a-=b)<0&&(a+=mod);}
ll ksm(ll a,ll b=mod-2){ll res=1;while(b){if(b&1)res=res*a%mod;a=a*a%mod,b>>=1;}return res;}
int a[N],b[N];
int X[N*N],Y[N*N],n;
struct mat{
int a[N][N];
mat operator * (const mat &o)const{
mat c;memset(c.a,0,sizeof(c.a));
for(int i=0;i<n;++i)for(int k=0;k<n;++k)for(int j=0;j<n;++j)add(c.a[i][j],1ULL*a[i][k]*o.a[k][j]%mod);
return c;
}
}I,M,orig;
mat ksm(mat a,int b){
mat res=I;
while(b){
if(b&1)res=res*a;
a=a*a,b>>=1;
}
return res;
}
int main(){
int m,k;read(n,m,k);
for(int i=0;i<n;++i)I.a[i][i]=1;
for(int i=0;i<n;++i)read(orig.a[i][0]);
for(int i=1;i<=m;++i)read(X[i],Y[i]);
ll inv=ksm(m);
for(int i=0;i<n;++i)M.a[i][i]=1;
for(int i=1;i<=m;++i){
int x=X[i]-1,y=Y[i]-1;
add(M.a[x][x],1LL*inv*(mod-inv2)%mod),add(M.a[x][y],1LL*inv*inv2%mod);
add(M.a[y][y],1LL*inv*(mod-inv2)%mod),add(M.a[y][x],1LL*inv*inv2%mod);
}
mat o=ksm(M,k)*orig;
for(int i=0;i<n;++i)printf("%d\n",o.a[i][0]);
return 0;
} |
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <cmath>
using namespace std;
int main(void){
int n, m;
cin >> n >> m;
vector< int > a(m), b(m), c(m), d(m);
vector< vector< int > > edg(n+1, vector< int >());
for(int i = 0; i < m; i++){
cin >> a[i] >> b[i] >> c[i] >> d[i];
edg[a[i]].push_back(i);
edg[b[i]].push_back(i);
}
vector< long long > dist(n+1, 1e18);
using P = pair<long long, int>;
priority_queue< P, vector<P>, greater<P> > q;
dist[1] = 0;
q.push(P(0, 1));
while(!q.empty()){
P p = q.top();
q.pop();
int v = p.second;
if(dist[v] < p.first) continue;
// cerr << v << " " << dist[v] << endl;
for(int i: edg[v]){
int to = b[i];
if(to == v) to = a[i];
long long cost = c[i] + (d[i] / (dist[v]+1));
if(dist[v] + cost < dist[to]){
dist[to] = dist[v] + cost;
q.push(P(dist[to], to));
}
cost = c[i] + (d[i] / (dist[v]+2)) + 1;
if(dist[v] + cost < dist[to]){
dist[to] = dist[v] + cost;
q.push(P(dist[to], to));
}
int rtd = sqrt(d[i]) - dist[v];
for(int j = -10; j <= 10; j++){
int w = rtd + j;
if(w <= 0) continue;
cost = c[i] + (d[i] / (dist[v]+1+w)) + w;
if(dist[v] + cost < dist[to]){
dist[to] = dist[v] + cost;
q.push(P(dist[to], to));
}
}
}
}
if(dist[n] == 1e18){
cout << -1 << endl;
}else{
cout << dist[n] << endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template<class T> using vc = vector<T>;
template<class T> using vvc = vector<vector<T>>;
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i,n) for (auto& i: n)
template<class T1, class T2> inline bool chmax(T1 &a, const T2 &b) {if (a<b) { a=b; return 1;} return 0;}
template<class T1, class T2> inline bool chmin(T1 &a, const T2 &b) {if (b<a) { a=b; return 1;} return 0;}
struct init{init(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}}init_;
#ifdef DEBUG
template <class T> void verr(const set<T> &st) { repa(a, st) cerr << a << " "; cerr << endl; }
template <class S, class T> void verr(const map<S, T> &mp) { repa(a, mp) cerr << "{" << a.first << ", " << a.second << "} "; cerr << endl; }
template <class S, class T, class N> void verr(const vector<pair<S,T>>& a, const N& n) { rep(i, n) cerr << "{" << a[i].first << ", " << a[i].second << "} "; cerr << endl; }
template <class T, class N> void verr(const vector<T>& a, const N& n) { rep(i, n) cerr << a[i] << " "; cerr << endl; }
ll dbgt = 1; void err() { cerr << "passed " << dbgt++ << endl; }
template<class H, class... T> void err(H&& h,T&&... t){ cerr<< h << (sizeof...(t)?" ":"\n") << flush; if(sizeof...(t)>0) err(forward<T>(t)...); }
#endif
const ll INF = 4e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//
vector<ll> topologycal_sort(vector<vector<ll>> L) {
ll N = L.size();
vector<ll> degree(N, 0), sorted_array;
rep(i, N) for (auto v : L[i]) degree[v]++;
queue<ll> que;
rep(i, N) if (degree[i] == 0) que.emplace(i);
while (!que.empty()) {
ll u = que.front();
que.pop();
for (auto v : L[u])
if (--degree[v] == 0) que.emplace(v);
sorted_array.emplace_back(u);
}
// ๅใใผใ็ชๅทใใใใญใธใซใซ้ ใซใใใชในใ
return sorted_array;
}
int main() {
ll N, M;
cin >> N >> M;
vl A(N);
rep(i, N) cin >> A[i];
vvc<ll> G(N);
rep(i, M){
ll a, b;
cin >> a >> b, a--, b--;
G[a].eb(b);
}
vl T = topologycal_sort(G);
ll ans = -INF;
vl B(N, INF), S(N, -INF);
rep(i, N){
ll now = T[i];
chmax(ans, A[now] - B[now]);
repa(to, G[now]){
chmin(B[to], A[now]);
chmin(B[to], B[now]);
}
}
cout << ans << endl;
} |
#define ll long long
#define dn double
#define mp make_pair
#define pb push_back
#define se second
#define fi first
#define mod 1000000007
#define sob(v) v.begin(),v.end()
#define sobr(v) v.rbegin(),v.rend()
#define same(v) v.erase(unique(v.begin(),v.end()),v.end())
#define fr(i,a,b) for(int i=a;i<=b;++i)
#define frr(i,a,b) for(int i=a;i>=b;--i)
#define pi acos(-1.00)
#define inf 1e9
#define EPS 1e-9
#define sf(n) scanf("%d",&n)
#define sfl(n) scanf("%lld",&n)
#define check(n) cout<<n<<"**CHECK"<<endl
#define ffix(x) cout<<fixed<<setprecision(x)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define ones(mask) __builtin_popcount(mask) // count set bit
#define onesLL(mask) __builtin_popcountll(mask) // for long long
#define lzeros(mask) __builtin_clz(mask) // no of leading zeros
#define tzeros(mask) __builtin_ctz(mask) // no of trailing zeros
#include<bits/stdc++.h>
#include<string.h>
using namespace std;
typedef vector<int> vi;
typedef map<int,int> mii;
typedef pair<int,int> pii;
typedef pair<int,vi> pp;
typedef vector<pii> vpii;
typedef pair<pair<int,int>,int> pain;
int dr[8]= {-1,0,+1,0,-1,-1,+1,+1};
int dc[8]= {0,+1,0,-1,-1,+1,-1,+1};
int kx[8]= {-1,+1,+1,-1,+2,+2,-2,-2};
int ky[8]= {+2,+2,-2,-2,-1,+1,+1,-1};
const int N=100005;
int main()
{
int n;
cin>>n;
ll a[n+1],b[n+1],pref[n+1];
memset(pref,0,sizeof pref);
memset(b,0,sizeof b);
fr(i,1,n)
{
cin>>a[i];
pref[i]=pref[i-1]+a[i];
}
fr(i,1,n)
{
b[i]=pref[i]+b[i-1];
}
ll mx=0;
fr(i,1,n)
{
mx=max(mx,a[i]);
ll ans=mx*i*1LL+b[i];
cout<<ans<<endl;
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
string s[105];
ll a[25];
int f[25];
int n;
vector<int>v;
void make(int x)
{
for(int i=n;i>1;i--)
{
f[i]=x%2;
x=x>>1;
}
}
int main()
{
ios::sync_with_stdio(false);
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
ll pre=0;
ll ans=1e18;
int nm=pow(2,n-1);
for(int cnt=0;cnt<=nm-1;cnt++)
{
make(cnt);
f[1]=1;
pre=0;
// cout<<cnt<<"cnt"<<endl;
v.clear();
for(int i=1;i<=n;i++)
{
if(f[i]==1)
{
pre=pre|a[i];
}
else
{
v.push_back(pre);
pre=a[i];
}
// cout<<pre<<endl;
}
v.push_back(pre);
ll x=v[0];
for(int i=1;i<v.size();i++)
{
x=x^v[i];
}
ans=min(ans,x);
// cout<<x<<endl;
}
cout<<ans<<endl;
}
/*3
1 5 7
0cnt
1
5
7
3
1cnt
1
5
7
5
2cnt
1
5
7
7
3cnt
1
5
7
0
0
*/
|
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <string>
#include <utility>
#include <tuple>
#include <set>
#include <map>
#include <cassert>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= k; j++) {
ans += 100*i + j;
}
}
cout << ans << endl;
return 0;
}
| #include<iostream>
#include<cstdio>
#include<string>
using namespace std;
int m,n;
int num;
long long ans1,ans2;
string in;
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
cin>>in;
num=0;
for(int j=0;j<m;j++)
if(in[j]=='0')
num++;
if(num&1)
ans1++;
else
ans2++;
}
printf("%lld",ans1*ans2);
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
void ans_add(string &t,vector<int>i){
t+=to_string(i[0])+' '+to_string(i[1])+' '+to_string(i[2])+' '+to_string(i[3])+'\n';
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin>>n;
vector<vector<int>>ad(n,vector<int>(4));
for(int i=0;i<n;i++){
cin>>ad[i][1]>>ad[i][2]>>ad[i][0];
ad[i][3]=i;
}
sort(ad.begin(),ad.end(),[](vector<int> a,vector<int> b){
return abs(a[0]-9999*50)<abs(b[0]-9999*50);
});
vector<vector<int>>ans(n,vector<int>(4));
vector<bool>al(n);
int base=(10000/n);
for(int i=0;i<n;i++){
if(al[(ad[i][1])/base]){
ans[ad[i][3]]=(vector<int>){i,9998,i+1,9999};
}else{
if(ad[i][0]>=base*9999){
ans[ad[i][3]]=(vector<int>){(ad[i][1])/base*base,0,min((ad[i][1])/base*base+base,9999),9998};
}
else{
int b=min((ad[i][1])/base*base+base,9999)-(ad[i][1])/base*base;
int u=max(0,ad[i][2]-ad[i][0]/base+1);
int d=min(9998,ad[i][0]/b+u);
if(abs((d-u)*b-ad[i][0])>abs((d-u+1)*b-ad[i][0])){
d=min(9998,d+1);
}
ans[ad[i][3]]=(vector<int>){(ad[i][1])/base*base,u,min((ad[i][1])/base*base+base,9999),d};
}
al[(ad[i][1])/base]=true;
}
}
string ret="";
for(auto i:ans){
ans_add(ret,i);
}
cout<<ret;
} | #define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(...) begin(__VA_ARGS__) , end(__VA_ARGS__)
#define boost {ios_base::sync_with_stdio(false); cin.tie(); cout.tie();}
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define random_shuffle(...) shuffle(__VA_ARGS__, rng)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector <int> vi;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
constexpr ll nax = 2e5+6969, INF = 2e9+6969;
int n,point[2][205],rec[205],ans[4][205];
bool br[10005][10005];
priority_queue<pair<ld,int>> q;
ld add(ll s, ll r, ll x)
{
ld tmp = (ld)((r-s)*2 - x) / r;
return tmp * x / r;
}
int random(int a, int b)
{
return uniform_int_distribution<int>(a,b)(rng);
}
int main()
{
scanf("%d", &n);
for(int i=0;i<n;i++)
{
scanf("%d%d%d", &point[0][i], &point[1][i], &rec[i]);
if(br[point[0][i]][point[1][i]] == 0)
{
ans[0][i] = point[0][i];
ans[1][i] = point[1][i];
ans[2][i] = point[0][i]+1;
ans[3][i] = point[1][i]+1;
br[point[0][i]][point[1][i]] = 1;
q.push(mp(add(1, rec[i], 1), i));
}
}
int REPS = 1000'000;
for(int i=0;;i++)
{
if(q.empty()) break;
vi changes;
for(int j=0;j<15;j++)
{
if(q.empty()) break;
changes.pb(q.top().se);
q.pop();
}
random_shuffle(all(changes));
for(auto who: changes)
{
int X = ans[2][who] - ans[0][who];
int Y = ans[3][who] - ans[1][who];
bool lf,rg,up,dw;
lf = rg = up = dw = 0;
{
if(ans[1][who] > 0)
{
up = 1;
for(int i=ans[0][who];i<ans[2][who];i++)
{
if(br[i][ans[1][who]-1])
{
up = 0;
break;
}
}
}
if(ans[3][who] < 10000)
{
dw = 1;
for(int i=ans[0][who];i<ans[2][who];i++)
{
if(br[i][ans[3][who]])
{
dw = 0;
break;
}
}
}
if(ans[0][who] > 0)
{
lf = 1;
for(int i=ans[1][who];i<ans[3][who];i++)
{
if(br[ans[0][who]-1][i])
{
lf = 0;
break;
}
}
}
if(ans[2][who] < 10000)
{
rg = 1;
for(int i=ans[1][who];i<ans[3][who];i++)
{
if(br[ans[2][who]][i])
{
rg = 0;
break;
}
}
}
}
if(X*(Y+1) > rec[who]) up = dw = 0;
if((X+1)*Y > rec[who]) lf = rg = 0;
if((((lf|rg)|up)|dw) == 0) continue;
vi options;
if(up) options.pb(0);
if(dw) options.pb(1);
if(lf) options.pb(2);
if(rg) options.pb(3);
int choice = options[random(0,options.size()-1)];
if(choice == 0)
{
ans[1][who]--;
for(int i=ans[0][who];i<ans[2][who];i++) br[i][ans[1][who]] = 1;
int R = X*(Y+1);
if(R < rec[who])
{
q.push(mp(add(R, rec[who], (X+1+Y)/2), who));
}
}
if(choice == 1)
{
for(int i=ans[0][who];i<ans[2][who];i++) br[i][ans[3][who]] = 1;
ans[3][who]++;
int R = X*(Y+1);
if(R < rec[who])
{
q.push(mp(add(R, rec[who], (X+1+Y)/2), who));
}
}
if(choice == 2)
{
ans[0][who]--;
for(int i=ans[1][who];i<ans[3][who];i++) br[ans[0][who]][i] = 1;
int R = (X+1)*Y;
if(R < rec[who])
{
q.push(mp(add(R, rec[who], (X+1+Y)/2), who));
}
}
if(choice == 3)
{
for(int i=ans[1][who];i<ans[3][who];i++) br[ans[2][who]][i] = 1;
ans[2][who]++;
int R = (X+1)*Y;
if(R < rec[who])
{
q.push(mp(add(R, rec[who], (X+Y+1)/2), who));
}
}
}
}
PII last = mp(0,0);
for(int i=0;i<n;i++)
{
if(ans[2][i] == ans[3][i] && ans[2][i] == 0)
{
for(int x = last.fi; x <= 10000; x++)
{
for(int y = 0; y <= 10000; y++)
{
if(br[x][y] == 0)
{
ans[0][i] = x;
ans[1][i] = y;
ans[2][i] = x+1;
ans[3][i] = y+1;
last = mp(x,y);
}
}
}
}
for(int j=0;j<4;j++) cout << ans[j][i] << " " ;
puts("");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int a,b;
int main(){
scanf("%d%d",&a,&b);
printf("%d",a==b?a:3-a-b);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rer(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);(i)++)
using ll = long long;
using P = pair<int,int>;
const int INF = 1001001001;
int main(){
int A,B,C;
cin >> A >> B;
if(A==B) cout << A << endl;
else{
if(A+B==1) cout << 2 << endl;
if(A+B==2) cout << 1 << endl;
if(A+B==3) cout << 0 << endl;
}
} |
/**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author aryssoncf
*/
// Taken from: https://github.com/bqi343/USACO/blob/master/Implementations/content/contest/templateShort.cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<ull> vul;
typedef vector<ll> vl;
typedef vector<pi> vpi;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define F0R(i, a) FOR(i,0,a)
#define ROF(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i, a) ROF(i,0,a)
#define trav(a, x) for (auto& a: x)
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rsz resize
#define mp make_pair
#define pb push_back
#define f first
#define s second
const int MOD = 1e9 + 7; // 998244353; // = (119<<23)+1
const int MX = 2e5 + 5;
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((uint32_t) chrono::steady_clock::now().time_since_epoch().count());
//
// Created by aryssoncf on 28/10/2019.
//
#include <type_traits>
#include <iterator>
#include <utility>
template<typename T>
constexpr auto hasBegin(int) -> decltype(std::begin(std::declval<T>()), true) {
return true;
}
constexpr bool hasBegin(...) {
return false;
}
template<typename T>
using IsContainer = std::integral_constant<bool, hasBegin<T>(0)>;
#ifndef JHELPER_EXAMPLE_PROJECT_IO_HPP
#define JHELPER_EXAMPLE_PROJECT_IO_HPP
#endif //JHELPER_EXAMPLE_PROJECT_IO_HPP
template<typename T>
std::istream &operator>>(std::istream &is, std::vector<T> &vec) {
for (T &element: vec) {
is >> element;
}
return is;
}
template<typename T, typename U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) {
is >> p.first;
is >> p.second;
return is;
}
template<class X, class Y>
std::ostream &operator<<(std::ostream &os, std::pair<X, Y> const &p) {
return os << p.first << " " << p.second;
}
template<class Ch, class Tr, class Container>
std::basic_ostream<Ch, Tr> &operator<<(std::basic_ostream<Ch, Tr> &os, Container const &x) {
bool first = true;
for (auto &y : x) {
if (first) {
first = false;
} else {
os << " ";
}
os << y;
}
return os;
}
using namespace std;
/*
PROG:
LANG: C++
*/
class BAchieveTheGoal {
public:
void solve(std::istream &in, std::ostream &out) {
int n, k, m;
in >> n >> k >> m;
int tot = n * m;
vi A(n - 1);
in >> A;
int sum = accumulate(all(A), 0), res = max(tot - sum, 0);
if (res > k) {
res = -1;
}
out << res << "\n";
}
void setup() {}
};
int main() {
std::ios_base::sync_with_stdio(false);
BAchieveTheGoal solver;
solver.setup();
std::istream &in(std::cin);
std::ostream &out(std::cout);
in.tie(nullptr);
out << std::fixed;
out.precision(20);
solver.solve(in, out);
return 0;
}
| #include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
int main(void)
{
int T,t,x,y,n,m,k,s=0;
int a[2000]={0};
scanf("%d %d %d",&n,&k,&m);
for(int i=0;i<n-1;i++)
{
scanf("%d",&a[i]);
s+=a[i];
}
if(m*n-s<=k)
{
for(int i=0;i<=k;i++)
{
if(s+i>=m*n)
{
printf("%d\n",i);
break;
}
}
}
else
printf("%d\n",-1);
return 0;
}
|
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cinttypes>
#include<vector>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<stack>
#define FSCNd64 "%" SCNd64
#define FPRId64 "%" PRId64
using namespace std;
using ll=long long;
using vi=vector<int>;
using vvi=vector<vi>;
using pii=pair<int,int>;
using vll=vector<ll>;
using vvll=vector<vll>;
using vpii=vector<pii>;
#define PI 3.1415926535897932384626433832795
template<typename X>
bool max_u(X&m, X v)
{
if(m<v)
{
m=v;
return true;
}
return false;
}
template<typename X>
bool min_u(X&m, X v)
{
if(m>v)
{
m=v;
return true;
}
return false;
}
bool next1(string&s, int&i)
{
int l=s.length();
if(i>=l) return false;
for(i++;i<l;i++)
{
if(s[i]=='1')
{
return true;
}
}
return false;
}
struct solve
{
solve(){}
ll operator()(int n, string s, string t) {
ll ans=0;
int si=-1, ti=-1;
for(int k=0;;k++)
{
if(si>=n && ti>=n)
{
return ans;
}
//cout << si << " " << ti << "\n";
if(si<ti)
{
int psi=si;
if(!next1(s, si)) break;
ans+=si-psi;
next1(s, si);
}
else
{
if(si>=n) break;
ans+=si-ti;
next1(s, si);
next1(t, ti);
}
}
return -1;
}
};
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
for(;;)
{
int n;
cin >> n;
if(cin.fail()) break;
string s, t;
cin >> s >> t;
cout << solve()(n, s, t) << "\n";
}
return 0;
}
| #include <iostream>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;
int main(void){
int n;
long long ans = 0;
string s, t;
cin >> n >> s >> t;
set< int > st;
for(int i=0;i<n;i++) if(s[i] == '1') st.insert(i);
for(int i=0;i<n;i++){
if(s[i] == t[i]) continue;
if(st.empty()){
puts("-1");
return 0;
}
auto itr = st.begin();
int j = *itr;
st.erase(itr);
while(j <= i){
if(st.empty()){
puts("-1");
return 0;
}
itr = st.begin();
j = *itr;
st.erase(itr);
}
s[j] = '0';
ans += j-i;
}
cout << ans << endl;
}
|
#pragma region Macros
#include <bits/stdc++.h>
#if defined(LOCAL) || defined(ONLINE_JUDGE) || defined(_DEBUG)
#include <atcoder/all>
#endif
using namespace std;
#define REP(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define REPR(i, n) for(int i=(n); i>=0; --i)
#define FOR(i, n, m) for(int i=(m), i##_len=(n); i<i##_len; ++i)
#define EACH(i, v) for(const auto& i : v)
#define ALL(x) (x).begin(),(x).end()
#define ALLR(x) (x).rbegin(),(x).rend()
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template<class T>using vec = vector<T>;
template<class T, class U>using umap = unordered_map<T, U>;
template<class T>using uset = unordered_set<T>;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
//using T = tuple<ll, ll, ll>;
using vl = vec<ll>;
#define fi first
#define se second
#define el endl
constexpr ll INF = numeric_limits<ll>::max()/2-1;
#pragma endregion
#pragma region IOMacros
template<class T>
istream &operator>>(istream &stream, vec<T>& o){REP(i, o.size())stream >> o[i];return stream;}
template<class T>
ostream &operator<<(ostream &stream, vec<T>& objs){REP(i, objs.size())stream << objs[i] << " ";stream << el;return stream;}
#define I(T, ...) ;T __VA_ARGS__;__i(__VA_ARGS__);
void __i() {}
template<class T, class... Ts> void __i(T&& o, Ts&&... args){cin >> o;__i(forward<Ts>(args)...);}
void O() {cout << el;}
template<class T, class... Ts> void O(T&& o, Ts&&... args){cerr << o << " ";O(forward<Ts>(args)...);}
#pragma endregion
void Main();
int main(){
std::cin.tie(nullptr);
std::cout << std::fixed << std::setprecision(15);
Main();
return 0;
}
vl fibb(const vl& seed, ll N){
vl fib(seed.size());
copy(ALL(seed), fib.begin());
for(ll i = seed.size();;i++){
ll t = fib[i-1]+fib[i-2];
if(t > N) break;
fib.push_back(t);
}
return fib;
}
void Main(){
I(ll, N);
vl seed({0, 1});
vl fib = fibb(seed, N);
ll cnt = 1 + fib.size() - 2;
ll i = 1;
ll siz = fib.size();
if(fib[fib.size()-1] == N){
goto last;
}
while(1){
seed[i]++;cnt++;
fib = fibb(seed, N);
if(fib[fib.size()-1] == N){
goto last;
}
if(siz > fib.size()){
seed[i]--;
seed.push_back(seed[i-1] + seed[i]);
i++;
}
}
last:
//cout << fib << el;
bool x = fib.size()&1;
vl ans;
// ้ใใ
REP(i, fib[1]){
ans.push_back(x+1);
}
x = !x;
ans.push_back(x+3);
FOR(i, fib.size(), 2){
REP(j, fib[i]-fib[i-1]-fib[i-2]){
ans.push_back(x+1);
}
x = !x;
ans.push_back(x+3);
}
ll s = min(130, (int)ans.size());
cout << s << el;
REP(i, s){
cout << ans[i] << el;
}
}
| #include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
#define int long long
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i)
#define REV(i, a, b) for (int i = (a); i >= (b); --i)
#define CLR(a, b) memset((a), (b), sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF 1001001001001001001ll
#define fcout cout << fixed << setprecision(12)
using namespace std;
class CCalculator {
public:
map<int, int> memo;
int fib(int n) {
if (n == 1 || n == 2) return 1;
if (memo.count(n)) return memo[n];
return memo[n] = fib(n - 1) + fib(n - 2);
}
void solve(std::istream& cin, std::ostream& cout) {
int N;
cin >> N;
set<int> s;
REV(i, 90, 0) {
int val = fib(i + 1);
if (N >= val) {
N -= val;
s.insert(i + 1);
}
}
int num = *s.rbegin();
if (num % 2 == 0) num++;
vector<int> res;
FOR(i, 1, num) {
if (i % 2 == 1) {
res.push_back(3);
} else {
res.push_back(4);
}
if (s.count(num - i + 1)) {
if (i % 2 == 1) {
res.push_back(1);
} else {
res.push_back(2);
}
}
}
cout << res.size() << endl;
REP(i, res.size()) cout << res[i] << endl;
}
};
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
std::istream& in(std::cin);
std::ostream& out(std::cout);
CCalculator solver;
solver.solve(in, out);
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e5 + 5;
int a[N], b[N];
ll cnt;
int n;
void fun(int x, int y) {
if (x == y)return;
int mid = (x + y) / 2;
fun(x, mid), fun(mid + 1, y);
int p1 = x, p2 = mid + 1, p3 = x;
while (p3 <= y) {
if (p1 > mid) {
b[p3++] = a[p2++];
}
else if (p2 > y) {
b[p3++] = a[p1++];
}
else {
if (a[p1] > a[p2]) {
cnt += ll(y - p2 + 1);
b[p3++] = a[p1++];
}
else {
b[p3++] = a[p2++];
}
}
}
for (int i = x; i <= y; ++i)a[i] = b[i];
}
ll c[N];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; ++i)scanf("%d", &a[i]);
for (int i = 0; i < n; ++i)c[i] = a[i];
fun(0, n - 1);
for (ll i = 0; i < n; ++i) {
printf("%lld\n", cnt);
cnt += ((ll)n - c[i] - 1ll) - c[i];
}
return 0;
} | /** @kutbilimone **/
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define rall(x) (x).rbegin(),(x).rend()
#define all(x) (x).begin(),(x).end()
#define arring(a, n) for(int i = 0; i < n; cin >> a[i++])
#define int long long
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(1+n), sum(1+n), sqr(1+n);
for(int i = 1; i <= n; i++){
cin >> a[i];
sum[i] = a[i] + sum[i-1];
sqr[i] = a[i]*a[i] + sqr[i-1];
}
int res = 0;
for(int i = 2; i <= n; i++){
int n = i-1, x = a[i];
res += n*x*x - 2*x*sum[n] + sqr[n];
}
cout << res;
return 0;
}
|
Subsets and Splits