code_file1
stringlengths 87
4k
| code_file2
stringlengths 85
4k
|
---|---|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
const int N = 1e5 + 10;
const ll mod = 1e9 + 7;
int k;
ll res;
int main() {
#ifndef ONLINE_JUDGE
freopen("1.txt", "r", stdin);
#endif
scanf("%d", &k);
for (ll i = 1; i <= k; ++i) {
for (ll j = 1; i * j <= k; ++j) {
res += k / (i * j);
}
}
printf("%lld\n", res);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
void run(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
const int N=2e5+5;
int dp[19][3][19],n;
bool vis[19][3][19];
string s;
int sol(int i,int sum,int erased){
sum%=3;
if(i==n){
return !(sum==0&&erased!=n)*1e9;
}
int &ret=dp[i][sum][erased];
if(vis[i][sum][erased]==1)
return ret;
vis[i][sum][erased]=1;
ret=1e9;
ret=min(sol(i+1,sum,erased+1)+1,sol(i+1,sum+s[i]-'0',erased));
return ret;
}
void solve(){
memset(dp,-1,sizeof dp);
cin>>s;
n=s.size();
cout<<(sol(0,0,0)>=1e9 ? -1:sol(0,0,0));
}
int main()
{
run();
int t = 1;
//cin>>t;
while (t--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct node{
long long val = 0;
node *lc = nullptr, *rc = nullptr;
node() {}
node(long long val): val(val) {}
node(node *lc, node *rc): lc(lc), rc(rc) {
val += lc->val;
val += rc->val;
}
};
typedef node * pnode;
const int maxn = 3e5+4;
int n, a[2*maxn];
pnode roots[2*maxn];
long long ans[maxn];
long long cur_inv = 0;
pnode build(int l, int r){
if (l == r) return new node();
int mid = (l+r)/2;
return new node(build(l,mid),build(mid+1,r));
}
pnode update(pnode p, int l, int r, int pos, int val){
if (l == r) return new node(p->val + val);
int mid = (l+r)/2;
if (pos <= mid) return new node(update(p->lc,l,mid,pos,val),p->rc);
else return new node(p->lc,update(p->rc,mid+1,r,pos,val));
}
long long query(pnode p, int l, int r, int lb, int rb){
if (r < lb || l > rb) return 0;
if (l >= lb && r <= rb) return p->val;
int mid = (l+r)/2;
return query(p->lc,l,mid,lb,rb) + query(p->rc,mid+1,r,lb,rb);
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin >> n;
for (int i=0; i<n; i++){
cin >> a[i];
a[i+n] = a[i];
}
roots[0] = build(0,n-1);
for (int i=0; i<2*n; i++){
cur_inv += query(roots[i],0,n-1,a[i]+1,n-1);
if (i >= n) cur_inv -= query(roots[i],0,n-1,0,a[i-n]-1);
if (i >= n-1) ans[i-n+1] = cur_inv;
roots[i+1] = update(roots[i],0,n-1,a[i],1);
if (i >= n-1) roots[i+1] = update(roots[i+1],0,n-1,a[i-n+1],-1);
}
for (int i=0; i<n; i++) cout << ans[i] << "\n";
}
// 0 1 2 3 0 1 2 3 | #include<bits/stdc++.h>
using namespace std;
#define ll long long
ll read() {
ll x=0, f=1; char ch=' ';
while(!isdigit(ch)) {ch=getchar(); if(ch=='-') f=-1;}
while(isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48), ch=getchar();
return x*f;
}
int n, a[300005];
ll c[500005], ans;
void jia(int i,ll x){
while(i<=n){
c[i]+=x;
i+=(i&-i);
}
}
ll he(int i){
ll s=0;
while(i){
s+=c[i];
i-=(i&-i);
}
return s;
}
int main() {
cin>>n;
for(int i=1;i<=n;i++) a[i]=read()+1;
for(int i=1;i<=n;i++){
jia(a[i],1);
ans+=he(n)-he(a[i]);
}
cout<<ans<<endl;
for(int i=1;i<n;i++) {
ans=ans-(a[i]-1)+(n-a[i]);
cout<<ans<<endl;
}
}
|
#include<cstdio>//JZM YYDS!!!
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#define ll long long
#define MAXN 200005
#define uns unsigned
#define MOD 998244353ll
#define INF 0x7f7f7f7f
using namespace std;
inline ll read(){
ll x=0;bool f=1;char s=getchar();
while((s<'0'||s>'9')&&s>0){if(s=='-')f^=1;s=getchar();}
while(s>='0'&&s<='9')x=(x<<1)+(x<<3)+s-'0',s=getchar();
return f?x:-x;
}
ll x,y,p,q;
inline ll exgcd(ll a,ll b,ll&x,ll&y){
if(b==0){x=1,y=0;return a;}
ll res=exgcd(b,a%b,y,x);
y-=a/b*x;return res;
}
signed main()
{
for(int T=read();T--;){
x=read(),y=read(),p=read(),q=read();
ll md=(x+y)<<1,lim=md*(p+q)+1,ans=lim;
ll b=p,a=p+q,K,f,g=exgcd(p+q,md,K,f);
for(ll i=x;i<x+y;i++){
ll c=((i-b)%md+md)%md,k=K;
if(c%g!=0)continue;
c/=g,k=(k*c%md+md)%(md/g);
if((k*a+b)%md>=x&&(k*a+b)%md<x+y)ans=min(ans,k*a+b);
}
md=p+q,b=x,a=(x+y)<<1,g=exgcd((x+y)<<1,md,K,f);
for(ll i=p;i<p+q;i++){
ll c=((i-b)%md+md)%md,k=K;
if(c%g!=0)continue;
c/=g,k=(k*c%md+md)%(md/g);
if((k*a+b)%md>=p&&(k*a+b)%md<p+q)ans=min(ans,k*a+b);
}
if(ans>=lim)printf("infinity\n");
else printf("%lld\n",ans);
}
return 0;
} | #include <bits/stdc++.h>
#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF (long long)1 << 61
#define MOD 1000000007
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;
typedef vector<int> IV;
typedef vector<llong> LV;
int dy[]={0, 0, 1, -1, 0};
int dx[]={1, -1, 0, 0, 0};
vector<long long> divisor(long long n) {
vector<long long> ret;
for (long long i = 1; i * i <= n; i++) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n) ret.push_back(n / i);
}
}
sort(ret.begin(), ret.end()); // 昇順に並べる
return ret;
}
int main() {
long long n; cin >> n;
vector<long long> ans = divisor(n);
for(auto &x:ans){
cout << x << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define range(a) a.begin(), a.end()
#define endl "\n"
#define Yes() cout << "Yes" << endl
#define No() cout << "No" << endl
#define MP make_pair
const unsigned long long mod = 1e9 + 7;
const long long INF = 1LL<<60;
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};
void chmin(int &a, int b) { if (a > b) a = b; }
void chmax(int &a, int b) { if (a < b) a = b; }
int main(){
int N, M;
cin >> N >> M;
vector<int> value(N);
FOR(i,0,N){
cin >> value.at(i);
}
vector<vector<int>> data(N);
FOR(i,0,M){
int x, y;
cin >> x >> y;
--x;
--y;
data.at(y).push_back(x);
}
int ans = -2e9;
FOR(i,0,N){
if(data.at(i).size()==0){
continue;
}
int nowvalue = value.at(i);
FOR(j,0,data.at(i).size()){
int now = data.at(i).at(j);
chmin(value.at(i), value.at(now));
ans = max(ans, nowvalue - value.at(now));
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int n, m;
vector<long long> a;
int solve();
int main() {
cin >> n >> m;
a.resize(m);
for (int i = 0; i < m; ++i) cin >> a[i];
cout << solve() << endl;
return 0;
}
int solve() {
long long res = 0, len = 3e9;
a.push_back(0);
a.push_back(n + 1);
sort(a.begin(), a.end());
n = a.size();
for (int i = 1; i < n; ++i)
if (a[i] - a[i - 1] - 1) len = min(len, a[i] - a[i - 1] - 1);
if (len == 3e9) return 0;
for (int i = 1; i < n; ++i) res += (a[i] - a[i - 1] - 1 + len - 1) / len;
return res;
} |
#include <bits/stdc++.h>
using namespace std;
/***********************************MACROS***********************************/
#define ll long long
#define pll pair<ll,ll>
#define vll vector<ll>
#define vii vector<int>
#define mll map<ll, ll>
#define all(v) v.begin(), v.end()
#define ce(ele) cout<<ele<<' '
#define cs(ele) cout<<ele<<'\n'
#define CASE(t) ll t; cin>>t; while(t--)
#define forr(i, b) for(ll i=0;i<b;i++)
/***********************************FUNCTIONS***********************************/
void FAST() { //FAST Input Output, DONT use both scnaf,printf / cin,cout , Use any ONE system
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
/***********************************CODE***********************************/
int f(int n){
vector<int> d;
while(n!=0){
d.push_back(n%10);
n=n/10;
}
sort(d.begin(),d.end());
int x=0,y=0;
int k=d.size();
for(int i=0;i<k;++i){
x=x*10;
x+=d[i];
y=y*10;
y+=d[k-1-i];
}
return y-x;
}
int main() {
FAST();
// CASE(t)
{
int n,k;
cin>>n>>k;
for(int i=0;i<k;i++){
if(f(n)==n){
break;
}
else{
n=f(n);
}
}
cout<<n;
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
int n,k,tot;
priority_queue<int, vector<int>, greater<int> > g2;
priority_queue<int ,vector<int>,less<int> >g1;
int G1(int x)
{
int num=0,NUM=1;
int X;
while(x/10!=0)
{
X=x%10;
g1.push(X);
x=x/10;
if(x<10&&x>0) g1.push(x);
}
while(!g1.empty())
{
num+=g1.top()*NUM;
NUM=NUM*10;
g1.pop();
}
return num;
}
int G2(int x)
{
int num=0,NUM=1;
int X;
while(x/10!=0)
{
X=x%10;
g2.push(X);
x=x/10;
if(x<10&&x>0) g2.push(x);
}
while(!g2.empty())
{
num+=g2.top()*NUM;
g2.pop();
NUM=NUM*10;
}
return num;
}
int f(int x)
{
int X,Y;
X=G2(x),Y=G1(x);
int num;
num=X-Y;
return num;
}
int main()
{
cin>>n>>k;
while(k!=0)
{
n=f(n);
k--;
}
cout<<n;
return 0;
} |
#include <bits/stdc++.h>
#include <chrono>
#include <math.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace std::chrono;
using namespace __gnu_pbds;
using ll = long long;
using ld = long double;
#define fo(i,z,n) for(int i = z;i<n;i++)
#define rfo(i,z,n) for(int i = z;i >= n;i--)
#define Fo(i,z,n) for(ll i = z;i<n;i++)
#define INT_BITS 32
#define mod 1000000007
#define vi vector<int>
#define vc vector<char>
#define pii pair<int,int>
#define pcc pair<char,char>
#define pic pair<int,char>
#define pci pair<char,int>
#define vpi vector<pair<int,int> >
#define vll vector<ll>
#define vvi vector<vector<int> >
#define vvpi vector<vector<pair<int,int> > >
#define vvc vector<vector<char> >
#define vvl vector<vector<ll> >
#define pb push_back
#define f first
#define int long long
#define float long double
#define all(c) (c).begin(), (c).end()
#define endl '\n'
#define pi 3.14159265358979323846
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
void __print(int x) {cerr << x;}
// void __print(long x) {cerr << x;}
// void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
// void __print(float x) {cerr << x;}
// void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
ll power(ll a,ll p,ll m=mod)
{ll res=1;while(p>0){if(p&1)res=(res*a)%m;a=(a*a)%m;p>>=1;}
return res;}
int ceil(int a,int b){
if(a%b == 0)return a/b;
else return a/b+1;
}
int madd(int a,int b){
int res = (((a + mod)%mod + (b+mod)%mod)%mod + mod)%mod;
return res;
}
int pow(int a,int b){
if(b == 0)return 1;
int res = pow(a,b/2);
res = res*res;
if(b&1)res *= a;
return res;
}
vi par(200005,-1);
vi siz(200005,1);
int find(int u){
if(par[u] == u)return u;
return par[u] = find(par[u]);
}
void solve(){
int n;
cin >> n;
vi v(n);
fo(i,0,n)cin >> v[i];
fo(i,0,n)par[v[i]] = v[i];
fo(i,0,n/2){
// if(v[i] == v[n-i-1])continue;
int fp1 = find(v[i]);
int fp2 = find(v[n-i-1]);
// debug(fp1,fp2,i);
if(fp1 != fp2){
par[fp2] = fp1;
siz[fp1] += siz[fp2];
}
}
int connectedcom = 0;
int ans = 0;
fo(i,0,200005)
if(par[i] == i){
// debug(par[i],i,siz[i]);
connectedcom++;
if(siz[i] == 1)ans += siz[i];
else
ans += siz[i];
}
// cout << connectedcom << endl;
ans -= connectedcom;
cout << ans << endl;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);
// memset(vis,-1,sizeof(vis));
int t = 1;
// cin >> t;
while(t--){
solve();
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
const int N = 10000;
int n;
bool mem[N][N];
class ad {
public:
int a, b, c, d, r, id;
ad() {}
void read() {
scanf("%d%d%d", &a, &b, &r);
mem[a][b] = true;
c = a + 1, d = b + 1;
}
void print() { printf("%d %d %d %d\n", a, b, c, d); }
int size;
void cal_size() { size = (c - a) * (d - b); }
double grad0, grad1;
void cal_grad() {
double p1 = 1.0 * size, p2 = size + d - b, p3 = size + c - a;
p1 = min(p1 / r, r / p1), p2 = min(p2 / r, r / p2),
p3 = min(p3 / r, r / p3);
p1 = p1 * (2 - p1), p2 = p2 * (2 - p2), p3 = p3 * (2 - p3);
grad0 = p2 - p1, grad1 = p3 - p1;
}
void init() {
cal_size();
cal_grad();
}
bool extend0() {
if (a) {
bool f = true;
for (int j = b; j < d; ++j) {
if (mem[a - 1][j]) {
f = false;
break;
}
}
if (f) {
for (int j = b; j < d; ++j) mem[a - 1][j] = true;
--a;
init();
return true;
}
}
if (c < 9999) {
bool f = true;
for (int j = b; j < d; ++j) {
if (mem[c][j]) {
f = false;
break;
}
}
if (f) {
for (int j = b; j < d; ++j) mem[c][j] = true;
++c;
init();
return true;
}
}
return false;
}
bool extend1() {
if (b) {
bool f = true;
for (int i = a; i < c; ++i) {
if (mem[i][b - 1]) {
f = false;
break;
}
}
if (f) {
for (int i = a; i < c; ++i) mem[i][b - 1] = true;
--b;
init();
return true;
}
}
if (d < 9999) {
bool f = true;
for (int i = a; i < c; ++i) {
if (mem[i][d]) {
f = false;
break;
}
}
if (f) {
for (int i = a; i < c; ++i) mem[i][d] = true;
++d;
init();
return true;
}
}
return false;
}
bool operator<(const ad &rhs) const {
return grad0 + grad1 < rhs.grad0 + rhs.grad1;
}
};
priority_queue<ad> q;
ad ans[200];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
ad t;
t.read();
t.id = i;
t.init();
q.push(t);
}
while (!q.empty()) {
ad now = q.top();
q.pop();
bool f = false;
if (now.grad0 >= now.grad1 && now.grad0 > 0) {
f = true;
if (!now.extend0()) now.grad0 = 0;
}
if (now.grad1 > now.grad0 && now.grad1 > 0) {
f = true;
if (!now.extend1()) now.grad1 = 0;
}
if (f)
q.push(now);
else
ans[now.id] = now;
}
for (int i = 0; i < n; ++i) ans[i].print();
return 0;
} |
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#ifdef ENABLE_DEBUG
#define dump(a) cerr<<#a<<"="<<(a)<<endl
#define dumparr(a,n) cerr<<#a<<"["<<(n)<<"]="<<(a[n])<<endl
#else
#define dump(a)
#define dumparr(a,n)
#endif
#define FOR(i, a, b) for(ll i = (ll)a;i < (ll)b;i++)
#define For(i, a) FOR(i, 0, a)
#define REV(i, a, b) for(ll i = (ll)b-1LL;i >= (ll)a;i--)
#define Rev(i, a) REV(i, 0, a)
#define REP(a) For(i, a)
#define SIGN(a) (a==0?0:(a>0?1:-1))
typedef long long int ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<ll, ll> pll;
typedef pair<ll,pll> ppll;
typedef vector<ll> vll;
typedef long double ld;
typedef pair<ld,ld> pdd;
pll operator+(pll a,pll b){
return pll(a.first+b.first,a.second+b.second);
}
pll operator-(pll a,pll b){
return pll(a.first-b.first,a.second-b.second);
}
pll operator*(ll a,pll b){
return pll(b.first*a,b.second*a);
}
const ll INF=(1LL<<60);
#if __cplusplus<201700L
ll gcd(ll a, ll b) {
a=abs(a);
b=abs(b);
if(a==0)return b;
if(b==0)return a;
if(a < b) return gcd(b, a);
ll r;
while ((r=a%b)) {
a = b;
b = r;
}
return b;
}
#endif
template<class T>
bool chmax(T& a,const T& b){
if(a<b){
a=b;
return true;
}
return false;
}
template<class T>
bool chmin(T& a,const T& b){
if(a>b){
a=b;
return true;
}
return false;
}
template<class S,class T>
std::ostream& operator<<(std::ostream& os,pair<S,T> a){
os << "(" << a.first << "," << a.second << ")";
return os;
}
template<class T>
std::ostream& operator<<(std::ostream& os,vector<T> a){
os << "[ ";
REP(a.size()){
os<< a[i] << " ";
}
os<< " ]";
return os;
}
int main(){
cout<<setprecision(1000);
ll N,M;
cin>>N>>M;
vector<ll> dp(1LL<<N);
vector<vector<pll>> cond(N);
REP(M){
ll x,y,z;
cin>>x>>y>>z;
--x;--y;
cond[x].push_back(pll(y,z));
}
auto f=[](ll num,ll y,ll z){
ll mask=(1LL<<(y+1))-1;
num&=mask;
return __builtin_popcountll(num)<=z;
};
dp[0]=1;
For(j,(1LL<<N)){
ll n=__builtin_popcountll(j);
if(n==N)continue;
ll cur=1;
REP(N){
if((cur&j)==0){
ll jj=(j|cur);
([&]() {
FOR(cidx, n, N)
{
for (auto &&[y, z] : cond[cidx])
{
if (!f(jj, y, z))
{
return;
}
}
}
dp[jj]+=dp[j];
})();
}
cur<<=1;
}
}
dump(dp);
cout<<dp[(1LL<<N)-1]<<endl;
return 0;
}
| #define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <complex>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#include <queue>
#define INF 1010101010LL
#define INFLL 1010101010101010101LL
using namespace std;
int mod = 1000000007;
class Segtree {
public:
Segtree(int n) //サイズ
{
int t = 1;
while (t < n) {
t *= 2;
}
a = vector<long long>(2 * t - 1);
this->n = t;
}
int sum(int l, int r)
{
return query(l, r, 0, n - 1, 0);
}
void update(int p, long long num)
{
p += this->n - 1;
a[p] ^= num;
while (p > 0) {
p = (p - 1) / 2;
a[p] = a[2 * p + 1] ^ a[2 * p + 2];
}
}
private:
int query(int l, int r, int p, int q, int k)
{
if (l <= p && q <= r) return a[k];
else if (q < l || r < p) return 0;
return query(l, r, p, (p + q) / 2, 2 * k + 1) ^ query(l, r, (p + q) / 2 + 1, q, 2 * k + 2);
}
private:
vector<long long> a;
int n;
};
int main()
{
int n, q;
cin >> n >> q;
Segtree seg(n);
for (int i = 0; i < n; i++) {
int t;
cin >> t;
seg.update(i, t);
}
for (int i = 0; i < q; i++) {
int t, x, y;
cin >> t >> x >> y;
if (t == 1) {
seg.update(x - 1, y);
} else {
cout << seg.sum(x - 1, y - 1) << endl;
}
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
template<typename T>inline T read(){
T f=0,x=0;char c=getchar();
while(!isdigit(c)) f=c=='-',c=getchar();
while(isdigit(c)) x=x*10+c-48,c=getchar();
return f?-x:x;
}
namespace run{
int n,m;
int main(){
n=read<int>(),m=read<int>();
if(n==1){
if(m!=0) puts("-1"),exit(0);
puts("1 2"),exit(0);
}
if(m<0 || m>=n-1) puts("-1"),exit(0);
int lim=1e6;
printf("%d %d\n",1,lim);
for(int i=1,k=2;i<=m+1;i++)
printf("%d %d\n",k,k+1),k+=2;
lim++;
for(int i=m+2;i<n;i++) printf("%d %d\n",lim,lim+1),lim+=2;
return 0;
}
}
int main(){
#ifdef my
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
return run::main();
} | /*
* Author : YangDavid
* Created Time : 2020.10.24 20:11:31
*/
#include<bits/stdc++.h>
#define rep(i, n) for(int i = 1; i <= n; ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int B = 10000000;
int main() {
int n, m;
scanf("%d%d", &n, &m);
if(n == 1) {
if(m == 0) {
printf("1 2\n");
return 0;
} else {
puts("-1");
return 0;
}
}
if(abs(m) >= n-1) { puts("-1"); return 0; }
if(m >= 0) {
printf("%d %d\n", 1, 2*m+3);
rep(i, m+1)
printf("%d %d\n", 2*i, 2*i+1+(i==m+1));
} else {
puts("-1");
return 0;
/*m = -m;
printf("%d %d\n",2,2*m+4);
rep(i, m+1)
printf("%d %d\n", 2*i-(i==1), 2*i+1);
*/}
rep(i, n-m-2)
printf("%d %d\n", B+4*i, B+4*i+1);
return 0;
}
|
#include <bits/stdc++.h>
#define endl '\n'
#define ALL(V) V.begin(), V.end()
#define L_B lower_bound
#define U_B upper_bound
#define pb push_back
#define ll long long
#define fr first
#define sc second
#define rep(i,n) for (i = 0; i < n; ++i)
#define REP(i,k,n) for (i = k; i <= n; ++i)
#define REPR(i,k,n) for (i = k; i >= n; --i)
using namespace std;
// taskkill /im new.exe /t /f
template<class T, class T1> int chkmin(T &x, const T1 &y) { return x > y ? x = y, 1 : 0; }
template<class T, class T1> int chkmax(T &x, const T1 &y) { return x < y ? x = y, 1 : 0; }
const int MAXN = (1 << 20);
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
//freopen("error.txt", "w", stderr);
freopen("output.txt", "w", stdout);
#endif
ll T = 1 ;
//cin>>T;
for(ll t=0;t<T;t++)
{
ll ans = 0;
ll n , m;
cin>>n>>m;
string s[n];
for(ll i = 0;i<n;i++)
{
cin>>s[i];
}
// for rows
for(ll i = 0;i<n;i++)
{
ll c = 0;
for(ll j = 0;j<m;j++)
{
// cout<<c<<" ";
if(s[i][j]=='.')
{
c++;
} else {
if(c) ans = ans + c - 1;
c = 0;
}
}
if(c) ans = ans + c - 1;
}
//cout<<endl;
// columns
for(ll i = 0;i<m;i++)
{
ll c = 0;
for(ll j = 0;j<n;j++)
{
//cout<<c<<" ";
if(s[j][i]=='.')
{
c++;
} else {
if(c) ans = ans + c - 1;
c = 0;
}
}
if(c) ans = ans + c - 1;
}
cout<<ans<<endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define forin(in ,n) for(ll i=0; i<n; i++) cin>>in[i]
#define forout(out) for(ll i=0; i<(ll)out.size(); i++) cout<<out[i]<<endl
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rep_up(i, a, n) for (ll i = a; i < n; ++i)
#define rep_down(i, a, n) for (ll i = a; i >= n; --i)
#define P pair<ll, ll>
#define all(v) v.begin(), v.end()
#define fi first
#define se second
#define vvvll vector<vector<vector<ll>>>
#define vvll vector<vector<ll>>
#define vll vector<ll>
#define pqll priority_queue<ll>
#define pqllg priority_queue<ll, vector<ll>, greater<ll>>
constexpr ll INF = (1ll << 60);
//constexpr ll mod = 1000000007;
//constexpr ll mod = 998244353;
constexpr ll mod = 3;
constexpr double pi = 3.14159265358979323846;
template <typename T>
inline bool chmax(T &a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <typename T>
inline bool chmin(T &a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <typename T>
void pt(T val) {
cout << val << "\n";
}
template <typename T>
void pt_vll(vector<T> &v) {
ll vs = v.size();
rep(i, vs) {
cout << v[i];
if (i == vs - 1)
cout << "\n";
else
cout << " ";
}
}
ll mypow(ll a, ll n) {
ll ret = 1;
if(n==0) return 1;
if(a==0) return 0;
rep(i, n) {
if (ret > (ll)(1e18 + 10) / a) return -1;
ret *= a;
}
return ret;
}
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
long long modinv(long long a, long long m) {
long long b = m, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b; swap(a, b);
u -= t * v; swap(u, v);
}
u %= m;
if (u < 0) u += m;
return u;
}
const int MAX = 510000;
long long th[MAX], tw[MAX];
// テーブルを作る前処理
void COMinit() {
th[1]=0; tw[1]=0;
for (ll i = 2; i < MAX; i++){
if(i%3==2) tw[i]=1;
if(i%3==0){
ll x=i;
while(x%3==0){
th[i]++;
x/=3;
}
if(x%3==2) tw[i]=1;
}
tw[i]+=tw[i-1];
th[i]+=th[i-1];
}
}
// 二項係数計算
long long COM(ll n, ll k){
if(th[n]>th[k]+th[n-k]) return 0;
if((tw[n]-tw[k]-tw[n-k])%2==0) return 1;
return 2;
}
int main() {
ll n,m,k,sum=0,ans=0;
string s;
cin>>n>>s;
COMinit();
vll a(n);
rep(i,n){
if(s[i]=='W') a[i]=0;
if(s[i]=='B') a[i]=1;
if(s[i]=='R') a[i]=2;
a[i]*=COM(n-1,i);
}
rep(i,n){
sum+=a[i];
sum%=mod;
}
if(n%2==0) sum=(3-sum)%3;
if(sum%3==0) cout<<"W"<<endl;
if(sum%3==1) cout<<"B"<<endl;
if(sum%3==2) cout<<"R"<<endl;
} |
#include<iostream>
#include<vector>
#include<math.h>
using namespace std;
int main()
{
int n;
cin>>n;
vector<int> a(n);
vector<int> b(n);
for(int i=0;i<n;i++)
{
cin>>a[i];
cin>>b[i];
}
int result=100000000;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
result=min(result, i==j ?(a[i]+b[j]):max(a[i],b[j]));
}
}
cout<<result;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define int int64_t
signed main(){
int n;cin>>n;
vector<string> s(n);
for(int i=0;i<n;++i)cin>>s[i];
vector<vector<int>>dp(n, vector<int>(2,0));
//base case ==> dp[0][0] && dp[0][1]
if(s[0]=="AND"){
dp[0][0]= 3;
dp[0][1]= 1;
}else{
dp[0][0]= 1;
dp[0][1]= 3;
}
for(int i=1;i<n;++i){
if(s[i]=="AND"){
dp[i][0] = 2*dp[i-1][0] + dp[i-1][1];
dp[i][1] = 0 + dp[i-1][1];
}else{
dp[i][0] = dp[i-1][0] + 0;
dp[i][1] = 2*dp[i-1][1] + dp[i-1][0];
}
}
cout<<dp[n-1][1]<<endl;
return 0;}
|
#include<bits/stdc++.h>
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--)
#define pii pair<int,int>
#define fi first
#define se second
#define PB push_back
#define ll long long
#define ull unsigned long long
#define y1 orzkcz
using namespace std;
const int mo=1000000007;
int power(int x,int y){
int s=1;
for (;y;y/=2,x=1ll*x*x%mo)
if (y&1) s=1ll*s*x%mo;
return s;
}
int main(){
int S=0,n,m,x;
scanf("%d%d",&n,&m);
For(i,1,n) scanf("%d",&x),m++,S+=x+1;
int s1=1,s2=1;
For(i,1,S){
s1=1ll*s1*i%mo;
s2=1ll*s2*(m+1-i)%mo;
}
cout<<1ll*s2*power(s1,mo-2)%mo<<endl;
} | //#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
#define rep2(x,fr,to) for(int x=(fr);x<(to);x++)
#define rep(x,to) for(int x=0;x<(to);x++)
#define repr(x,fr,to) for(int x=(fr);x>=(to);x--)
#define all(c) c.begin(),c.end()
#define sz(v) (int)v.size()
typedef long long ll; typedef vector<int> VI; typedef pair<int,int> pii; typedef vector<ll> VL; const int MD = 1e9+7; //998244353;
void dbg(){cerr<<"\n";} template <class F,class ...S> void dbg(const F& f, const S&...s){cerr <<f <<": "; dbg(s...);}
//using mint = atcoder::modint1000000007;
struct Segtree{
typedef ll sgT;
vector<sgT> seg;
int nbn; sgT INF;
void init(int n){
for(nbn=1; nbn<n+2; nbn<<=1); INF=1LL<<61; // min:+inf,max:-inf,0
seg.assign(nbn*2, INF);
}
sgT comp(sgT l, sgT r){return min(l, r);} //min<->max
void update(int k,sgT x){
k+=nbn-1; seg[k]=x;
while(k>0){ k=(k-1)/2; seg[k]=comp(seg[k*2+1],seg[k*2+2]); }
}
sgT query2(int a,int b,int l,int r,int k){
if(b<=l||r<=a) return INF;
if(a<=l&&r<=b) return seg[k];
return comp(query2(a,b,l,(l+r)/2,k*2+1), query2(a,b,(l+r)/2,r,k*2+2));
}
sgT query(int a,int b){ return query2(a,b,0,nbn,0); }
sgT get(int a){return seg[nbn+a-1];} // =query(a,a+1)
int posLeft(sgT v, int l, int r ){//v以上の右端rで最左位置を求める
int lo=l-1, hi=r;
while(hi - lo >1){
int md = (hi + lo) >> 1;
if( query(md, r+1) == v ) hi = md; else lo = md;
}
return hi;
}
int posRight(sgT v, int l, int r ){ //v以上の左端lで最右位置を求める
int lo=l-1, hi=r;
while(hi - lo >1){
int md = (hi + lo) >> 1;
if( query(l, md+1) == v ) lo = md; else hi = md;
}
return lo;
}
};
int main(){
cin.tie(0); ios_base::sync_with_stdio(false);
int n;
cin >>n;
VI a(n);
rep(i, n) cin >>a[i];
map<int, int> mp;
rep(i, n) mp[a[i]] = i;
Segtree sg; sg.init(n);
rep(i, n) sg.update(i, a[i]);
ll ans =0;
rep(i, n){
ll cr = a[i];
int ps1 = sg.posLeft(cr, 0, i);
int ps2 = sg.posRight(cr, i, n);
ans = max(ans, cr * (ps2+1-ps1));
}
cout <<ans <<"\n";
return 0;
} |
#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;
}
const int maxn = 123456, mod = 1000000007;
int n;
int a[maxn], fib[maxn];
void input()
{
ios::sync_with_stdio(false); cin.tie(0);
cin >> n;
F1 (i, n) cin >> a[i];
}
void solve()
{
int fu = 1;
int ans = 0;
fib[0] = fib[1] = 1;
for (int i = 2; i <= n; ++i)
fib[i] = (fib[i - 1] + fib[i - 2]) % mod;
ans = a[1] * fib[n] % mod;
for (int i = 2; i <= n; ++i)
{
int neg = fib[i - 2] * fib[n - i] % mod;
int coef = (fib[n] - neg - neg + mod + mod) % mod;
ans = (ans + a[i] * coef % mod) % mod;
}
cout << ans << '\n';
}
main()
{
input();
solve();
}
| #include<bits/stdc++.h>
#define int long long
using namespace std;
signed main(){
int mod=1000000007;
int N;cin>>N;
vector<vector<int>>V(N,vector<int>(2));//0は正、1は負
vector<int>J(N);J[0]=1;J[1]=1;
for(int X=2;X<N;X++){
J[X]=J[X-1]+J[X-2];J[X]%=mod;
}
for(int X=0;X<N;X++){
int A;cin>>A;
if(X==0){
V[X][0]=A;V[X][1]=A;
}
else if(X==1){
V[X][0]=V[X-1][0]+A;V[X][0]%=mod;
V[X][1]=V[X-1][1]-A;
if(V[X][1]<0){
V[X][1]%=mod;
V[X][1]+=mod;
}
V[X][1]%=mod;
}
else{
V[X][0]=V[X-1][0]+V[X-1][1]+A*J[X];V[X][0]%=mod;
V[X][1]=V[X-1][0]-A*J[X-1];
if(V[X][1]<0){
V[X][1]%=mod;
V[X][1]+=mod;
}
V[X][1]%=mod;
}
//cout<<V[X][0]<<" "<<V[X][1]<<endl;
}
if(N==1)cout<<V[N-1][0]<<endl;
else cout<<(V[N-1][0]+V[N-1][1])%mod<<endl;
} |
#define _USE_MATH_DEFIMES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
const int MOD = 1'000'000'007;
const int MOD2 = 998'244'353;
const int INF = 1'000'000'000; //1e9
const int NIL = -1;
const long long LINF = 1'000'000'000'000'000'000; // 1e18
const long double EPS = 1E-10;
template<class T, class S> inline bool chmax(T &a, const S &b){
if(a < b){
a = b; return true;
}
return false;
}
template<class T, class S> inline bool chmin(T &a, const S &b){
if(b < a){
a = b; return true;
}
return false;
}
template<class T, class S> inline bool exist(std::set<T> &s, const S &e){
return (s.find(e) != s.end());
}
template<unsigned long long m> class comb_perm{
private:
int n;
std::vector<long long> fact, fact_inv;
public:
comb_perm(): n(0), fact(1), fact_inv(1){
fact[0] = fact_inv[0] = 1;
}
comb_perm(int N): n(N), fact(N+1), fact_inv(N+1){
fact[0] = fact[1] = fact_inv[0] = fact_inv[1] = 1;
for(int i{2}; i <= n; ++i) fact[i] = fact[i - 1] * i % m;
for(int i{2}; i <= n; ++i) fact_inv[i] = inv(fact[i]);
}
bool resize(int N){
if(N <= n) return false;
fact.resize(N+1);
fact_inv.resize(N+1);
if(!n && N){
fact[1] = fact_inv[1] = 1;
}
for(int i{std::max(2, n+1)}; i <= N; ++i) fact[i] = fact[i - 1] * i % m;
for(int i{std::max(2, n+1)}; i <= N; ++i) fact_inv[i] = inv(fact[i]);
n = N;
return true;
}
long long power(long long a, unsigned long long b){
long long ret{1};
while(b){
if(b & 1) ret = (ret * a) % m;
a = (a * a) % m;
b /= 2;
if(ret < 0) ret += m;
if(a < 0) a += m;
}
return ret;
}
long long inv(long long a){ // GCD(a, m) = 1
return power(a, m - 2);
}
long long permutation(int n, int r){
if(n < r) return 0;
if(n < 0 || r < 0) return 0;
if(this->n < n) resize(n);
long long ret(fact[n]);
ret = ret * fact_inv[n - r] % m;
return ret;
}
long long combination(int n, int r){
if(n < r) return 0;
if(n < 0 || r < 0) return 0;
long long ret{permutation(n, r)};
return ret * fact_inv[r] % m;
}
long long multi_combination(int n, int r){
return combination(n + r - 1, r);
}
long long factorial(int a){
return fact[a];
}
long long inv_factorial(int a){
return fact_inv[a];
}
};
int main(){
int N, M, K; std::cin >> N >> M >> K;
long long ans{};
if(N <= M + K){
comb_perm<MOD> cp;
ans += cp.combination(N+M, N);
(ans -= cp.combination(N+M, M+K+1)) %= MOD;
if(ans < 0) ans += MOD;
}
std::cout << ans << std::endl;
return 0;
}
| #include <iostream>
#include <cstdint>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cctype>
#include <cassert>
#include <climits>
#include <string>
#include <bitset>
#include <cfloat>
#include <unordered_set>
#pragma GCC optimize("Ofast")
using namespace std;
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<pair<int, int>> vpii;
typedef vector<vector<int>> vvi;
typedef vector<vector<char>> vvc;
typedef vector<vector<string>> vvs;
typedef vector<vector<ll>> vvll;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define irep(it, stl) for (auto it = stl.begin(); it != stl.end(); it++)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define CHOOSE3(a) CHOOSE4 a
#define CHOOSE4(a0, a1, a2, x, ...) x
#define mes_1(a) cout << (a) << endl
#define mes_2(a, b) cout << (a) << " " << (b) << endl
#define mes_3(a, b, c) cout << (a) << " " << (b) << " " << (c) << endl
#define mes(...) \
CHOOSE3((__VA_ARGS__, mes_3, mes_2, mes_1, ~)) \
(__VA_ARGS__)
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x
#define debug_1(x1) cout << #x1 << ": " << x1 << endl
#define debug_2(x1, x2) cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << endl
#define debug_3(x1, x2, x3) cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " << x3 << endl
#define debug_4(x1, x2, x3, x4) cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " << x3 << ", " #x4 << ": " << x4 << endl
#define debug_5(x1, x2, x3, x4, x5) cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": " << x3 << ", " #x4 << ": " << x4 << ", " #x5 << ": " << x5 << endl
#define debug(...) \
CHOOSE((__VA_ARGS__, debug_5, debug_4, debug_3, debug_2, debug_1, ~)) \
(__VA_ARGS__)
#define ynmes(a) (a) ? mes("Yes") : mes("No")
#define YNmes(a) (a) ? mes("YES") : mes("NO")
#define re0 return 0
#define mp(p, q) make_pair(p, q)
#define pb(n) push_back(n)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define Sort(a) sort(a.begin(), a.end())
#define rSort(a) sort(a.rbegin(), a.rend())
#define Rev(a) reverse(a.begin(), a.end())
#define MATHPI acos(-1)
#define itn int;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
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;
}
struct io
{
io()
{
ios::sync_with_stdio(false);
cin.tie(0);
}
};
const int INF = INT_MAX;
const ll LLINF = 1LL << 60;
const ll MOD = 1000000007;
const double EPS = 1e-9;
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;
}
ll nlcm(vector<ll> numbers)
{
ll res;
res = numbers[0];
for (ll i = 1; i < (ll)numbers.size(); i++)
{
res = lcm(res, numbers[i]);
}
return res;
}
uintmax_t ncr(unsigned int n, unsigned int r)
{
if (r * 2 > n)
r = n - r;
uintmax_t dividend = 1;
uintmax_t divisor = 1;
for (unsigned int i = 1; i <= r; ++i)
{
dividend *= (n - i + 1);
divisor *= i;
}
return dividend / divisor;
}
signed main()
{
ll a, b, c;
cin >> a >> b >> c;
ynmes(a * a + b * b < c * c);
}
|
#include<algorithm>
#include<iostream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
#define neinf 0xc0c0c0c0c0c0c0c0ll
#define inf 0x3f3f3f3f3f3f3f3fll
#define uint unsigned int
#define ull unsigned ll
#define ll long long
#define reg register
#define db double
#define il inline
#define gc getchar
#define pc putchar
#define HgS 1000000007
#define to(i) ((ed+(i))->_to)
#define nxt(i) ((ed+(i))->_nxt)
#define pr pair<ll,ll>
#define prpr make_pair
il ll rd()
{
reg ll res=0,lab=1;
reg char ch=gc();
while((ch<'0'||ch>'9')&&ch!=EOF)
{if(ch=='-')lab=-lab;ch=gc();}
while(ch>='0'&&ch<='9')
res=(res<<3)+(res<<1)+(ch&15),ch=gc();
return res*lab;
}
il void prt(ll x,char t='\n')
{
reg char ch[70];reg int tp=0;
if(x<0)pc('-'),x=-x;
while(x)ch[++tp]=(x%10)^48,x/=10;
if(!tp)ch[++tp]=48;
while(tp)pc(ch[tp--]);
if(t)pc(t);
}
il ll uabs(ll x){return x>0?x:-x;}
il ll umax(ll x,ll y){return x>y?x:y;}
il ll umin(ll x,ll y){return x<y?x:y;}
ll n,k,ru,rv,h[200005],d[200005],fa[200005],en,res,ans;
bool vis[200005];
struct edge{ll _to,_nxt;}ed[400005];
il ll add_edge(ll u,ll v){nxt(++en)=h[u];to(en)=v;return h[u]=en;}
pr dfs(int cur,ll x)
{
vis[cur]=1;
ll mxf=neinf,mnf=inf;pr tmp;
for(int i=h[cur];i;i=nxt(i))
{
if(!vis[to(i)])
tmp=dfs(to(i),x),
mxf=umax(mxf,tmp.first+1),mnf=umin(mnf,tmp.second+1);
}
if(mnf>x)mxf=umax(mxf,0);
if(mxf+mnf<=x)mxf=neinf;
if(mxf==x){mxf=neinf;mnf=0;++res;}
return prpr(mxf,mnf);
}
il bool chk(ll r)
{
res=0;memset(vis,0,sizeof(vis));
return res+(dfs(1,r).first>=0)<=k;
}
int main()
{
ans=n=rd();k=rd();
for(int i=1;i<n;++i)ru=rd(),rv=rd(),add_edge(ru,rv),add_edge(rv,ru);
reg ll l=0,r=n,mid;
while(l<=r)
{
mid=(l+r)>>1;
if(chk(mid))ans=mid,r=mid-1;
else l=mid+1;
}
prt(ans);
return 0;
}
| #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(){
int a, b;
cin >> a >> b;
REPR(i, b + 1){
if(b / i > ceil(a / (double)i)){
println(i);
return 0;
}
}
return 0;
} |
#include <algorithm>
#include <bits/stdc++.h>
#include <stdio.h>
#include <string.h>
using namespace std;
#define _CRT_SECURE_NO_WARNINGS
#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 re_rep(i, n) for (int i = (n); i >= 1;)
#define wrep(a) while ((int)a > 0)
#define re_wrep(a, N) while ((int)a < N)
#define scanfd(N) scanf("%d", &N);
#define scanfc(a) scanf("%s", a);
#define scanfca(a) scanf("%s", a);
#define scanfd2(a, b) scanf("%d %d", &a, &b);
#define scanfca2(a, b) scanf("%s%s", a, b);
#define scanfcad(a, b) scanf("%s%d", a, &b);
#define scanfc3(a, b, c) scanf("%s%s%s", &a, &b, &c);
#define scanfc3a(a, b, c) scanf("%s%s%s", a, b, c);
#define scanfdcd(a, b, c) scanf("%d %c %d", &a, &b, &c);
#define scanfsd_if(c, d) scanf("%s %d", &c, &d);
#define scanfcd_switch(c, d) scanf(" %c %d", &c, &d);
#define printfd(a) printf("%d", a);
#define printfca(a) printf("%s", a);
#define printfc3(a, b, c) printf("%c%c%c", a, b, c);
struct MyPair {
string S;
int T;
};
bool t_cmp(const struct MyPair &p, const struct MyPair &q) { return p.T < q.T; }
int main() {
int N, T;
string S, a;
cin >> N;
vector<MyPair> p(N);
rep(i, N) {
cin >> S >> T;
p.at(i).S = S;
p.at(i).T = T;
}
sort(p.begin(), p.end(), t_cmp);
// rep(i, N) { cout << p.at(i).S << endl; }
cout << p.at(N - 2).S << endl;
return 0;
} | #ifdef LOGX
#define _GLIBCXX_DEBUG
#endif
#include <bits/extc++.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 ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
#define mybit(i,j) (((i)>>(j))&1)
/*---------type/const---------*/
typedef long long ll;
typedef unsigned long long ull;
typedef std::string::const_iterator state; //構文解析
const int big=1000000007;
//const int big=998244353;
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)-10;}
constexpr operator ll(){return -ll(1e18)-10;}
}neginf;
struct{
constexpr operator int(){return int(1e9)+10;}
constexpr operator ll(){return ll(1e18)+10;}
constexpr auto operator -(){return neginf;}
}inf;
/*---------debug---------*/
#ifdef LOGX
#include <template/debug.hpp>
#else
#define dbg(...) ;
#define dbgnewl ;
#define prt(x) ;
#define _prt(x) ;
#endif
/*---------function---------*/
template<typename T> T max(const std::vector<T> &a){T ans=a[0];for(T elem:a){ans=max(ans,elem);}return ans;}
template<typename T> T min(const std::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<class T,class 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<class T,class U>T exp(T x,U y){T res=1;while(y){if(y&1)res*=x;x*=x;y>>=1;}return res;}
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.precision(10);
/*------------------------------------*/
int n;cin >> n;
vector<pair<ll,ll>> xc(n);
vector<ll> x(n),c(n);
rep(i,n){
cin >> xc[i].first >> xc[i].second;
}
sort(ALL(xc));
rep(i,n){
x[i]=xc[i].first;
c[i]=xc[i].second, c[i]--;
}
vector<vector<int>> col(n);
rep(i,n){
col[c[i]].emplace_back(i);
}
dbg(x);
dbg(c);
ll x_l=0;
ll x_r=0;
ll val_l=0;
ll val_r=0;
rep(i,n){
if(col[i].size()){
ll ne_l=x[col[i].front()];
ll ne_r=x[col[i].back()];
ll nval_l=inf;
ll nval_r=inf;
chmin(nval_l, val_l+abs(x_l-ne_r)+abs(ne_r-ne_l));
chmin(nval_l, val_r+abs(x_r-ne_r)+abs(ne_r-ne_l));
chmin(nval_r, val_l+abs(x_l-ne_l)+abs(ne_r-ne_l));
chmin(nval_r, val_r+abs(x_r-ne_l)+abs(ne_r-ne_l));
x_l=ne_l;
x_r=ne_r;
val_l=nval_l;
val_r=nval_r;
dbg(x_l, val_l);
dbg(x_r,val_r);
dbgnewl;
}
}
cout << min(val_l+abs(x_l), val_r+abs(x_r)) << newl;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll maxx(ll a, ll b){
if(a>b){
return a;
}
else{
return b;
}
}
ll minn(ll a, ll b){
if(a<b){
return a;
}
else{
return b;
}
}
int main()
{
ll n;
cin>>n;
ll N = pow(2, n);
vector<pair<ll,ll>>v(N);
ll i;
for(i=0; i<N; i++){
ll x;
cin>>x;
v[i].first = x;
v[i].second = i+1;
// cout<<v[i].first<<" "<<v[i].second<<endl;
}
ll cnt =0;
ll res;
while(1){
if(v.size() == 2){
if(v[0].first > v[1].first){
res = v[1].second;
}
else{
res = v[0].second;
}
break;
}
vector<pair<ll,ll>>vec;
for(i=1; i<v.size(); i=i+2){
if(v[i].first > v[i-1].first){
vec.push_back(v[i]);
}
else{
vec.push_back(v[i-1]);
}
}
// for(i=0; i<vec.size(); i++){
// cout<<vec[i].first<<" "<<vec[i].second<<endl;
// }
// cout<<endl;
v.clear();
v = vec;
}
cout<<res;
return 0;
} | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define f(i,a,b) for( ll i = a; i < b ; i++ )
#define af(i,a,b) for( ll i = a; i >= b ; i--)
#define rep(i,a,b,k) for(ll i = a; i < b ; i+= k )
#define arep(i,a,b,k) for( ll i = a; i >= b ; i-= k)
#define ones(numeros) (ll) __builtin_popcountll(numeros)
#define fs first
#define sc second
#define pb push_back
#define po pop_back
#define sz(a) (ll) a.size()
#define all(a) a.begin(), a.end()
#define sor(a) sort( a.begin(), a.end() )
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ller ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr)
#define watch(x) cout << (#x) << " es " << (x) <<"\n"
#define test ll deftestcases;cin>>deftestcases;while(deftestcases--)
#define PI acos(-1)
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> ii;
typedef pair<ll,ii> iii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
template<class T> using ordered_set =tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update> ;
const ll inf = 2e18;
const ll mod = 1e9+7;
const ll MAX = 6e2+15;
int main(){
fastio;
map<string,ll> mp;ll n;
cin>>n;
f(i,0,n){
ll x;string s;
cin>>s>>x;
mp[s]=x;
}
ll maxi = 0;
for(auto it : mp){
if(maxi<it.sc){
maxi = it.sc;
}
}
string ans;ll aux =0;
for(auto it : mp){
if(it.sc == maxi) continue;
if(aux<it.sc){
aux = it.sc;
ans = it.fs;
}
}
cout<<ans<<"\n";
return 0;
} |
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <math.h>
#include <cassert>
#define rep(i,n) for(int i = 0; i < n; ++i )
using namespace std;
using ll = long long;
// auto mod int
const int mod = 1000000007;
struct mint {
ll x; // typedef long long ll;
mint(ll x=0):x((x%mod+mod)%mod){}
mint operator-() const { return mint(-x);}
mint& operator+=(const mint a) {if ((x += a.x) >= mod) x -= mod; return *this;}
mint& operator-=(const mint a) {if ((x += mod-a.x) >= mod) x -= mod; return *this;}
mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
mint operator+(const mint a) const { return mint(*this) += a;}
mint operator-(const mint a) const { return mint(*this) -= a;}
mint operator*(const mint a) const { return mint(*this) *= a;}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t>>1);
a *= a;
if (t&1) a *= *this;
return a;
}
// for prime mod
mint inv() const { return pow(mod-2);}
mint& operator/=(const mint a) { return *this *= a.inv();}
mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
struct E{
vector<mint> v;
E(int n):v(n){}
};
const mint i2 = mint(1)/2;
int main() {
int n,m,k;
cin >> n >> m >> k;
vector<ll> a(n);
rep(i,n) cin >> a[i];
vector<vector<E>> x(1,vector<E>(n,E(n)));
auto& x0 = x[0];
rep(i,n) x0[i].v[i] = m;
rep(i,m){
int u,v;
cin >> u >> v;
--u,--v;
x0[u].v[u] -= i2;
x0[u].v[v] += i2;
x0[v].v[u] += i2;
x0[v].v[v] -= i2;
}
for(int p=1;k>>p;++p){
x.emplace_back(n,E(n));
auto &u = x[p-1], &v = x[p];
rep(k,n){
auto& e = u[k].v;
rep(i,n)rep(j,n){
v[k].v[j] += e[i]*u[i].v[j];
}
}
}
mint im = mint(1)/m;
im = im.pow(k);
rep(u,n){
vector<mint> s(n);
s[u] = 1;
for(int p=0;k>>p;++p)if(k>>p&1){
auto& xi = x[p];
vector<mint> t(n);
rep(i,n)rep(j,n){
t[j] += s[i]*xi[i].v[j];
}
swap(s,t);
}
mint ans;
rep(i,n) ans += s[i]*a[i];
ans *= im;
printf("%lld\n",ans.x);
}
}
| // g++ -std=c++11 a.cpp
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<unordered_map>
#include<utility>
#include<cmath>
#include<random>
#include<cstring>
#include<queue>
#include<stack>
#include<bitset>
#include<cstdio>
#include<sstream>
#include<random>
#include<iomanip>
#include<assert.h>
#include<typeinfo>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define FOR(i,a) for(auto i:a)
#define pb push_back
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
#define show1d(v) {rep(_,v.size())cout<<" "<<v[_];cout<<endl;}
#define show2d(v) {rep(__,v.size())show1d(v[__]);}
using namespace std;
//kaewasuretyuui
typedef long long ll;
#define int ll
typedef int Def;
typedef pair<Def,Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def,pii> pip;
typedef vector<pip>vip;
#define mt make_tuple
typedef tuple<int,int,int> tp;
typedef vector<tp> vt;
typedef vector<vt>vvt;
template<typename A,typename B>bool cmin(A &a,const B &b){return a>b?(a=b,true):false;}
template<typename A,typename B>bool cmax(A &a,const B &b){return a<b?(a=b,true):false;}
const double PI=acos(-1);
const long double EPS=1e-9;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9+10;
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
#define yes cout<<"Yes\n"
#define no cout<<"No\n"
typedef int SegT;
const SegT defvalue=0;
class SegTree{
private:
vector<SegT>val;
int n;
SegT combine(SegT a,SegT b){return a^b;}
public:
SegTree(int size){
n=1;
while(n<size)n<<=1;
val=vector<SegT>(2*n,defvalue);
}
SegTree(const vector<SegT> &in){
n=1;
while(n<in.size())n<<=1;
val=vector<SegT>(2*n,defvalue);
for(int i=n-1+in.size()-1;i>=0;i--){
if(n-1<=i)val[i]=in[i-(n-1)];
else val[i]=combine(val[i*2+1],val[i*2+2]);
}
}
void update(int i,SegT a){
i+=n-1;
val[i]^=a;
while(i>0){
i=(i-1)/2;
val[i]=combine(val[i*2+1],val[i*2+2]);
}
}
SegT query(int a,int b,int k=0,int l=0,int r=-1){//[a,b)
if(r==-1)r=n;
if(r<=a||b<=l)return defvalue;
if(a<=l&&r<=b)return val[k];
else return combine(query(a,b,k*2+1,l,(l+r)/2),query(a,b,k*2+2,(l+r)/2,r));
}
void tmp(){
rep(i,val.size())cout<<" "<<val[i];cout<<endl;
}
};
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n,q;
cin>>n>>q;
SegTree st(n);
rep(i,n){
int a;cin>>a;
st.update(i,a);
}
while(q--){
int t,x,y;
cin>>t>>x>>y;
if(t==1){
st.update(x-1,y);
}else{
cout<<st.query(x-1,y)<<endl;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define YES() printf("YES\n")
#define NO() printf("NO\n")
#define isYES(x) printf("%s\n",(x) ? "YES" : "NO")
#define Yes() printf("Yes\n")
#define No() printf("No\n")
#define isYes(x) printf("%s\n",(x) ? "Yes" : "No")
#define isIn(x,y,h,w) (x >= 0 && x < h && y >= 0 && y < w)
#define int long long
//using ll = long long;
using P = pair<int,int>;
ostream &operator<<(ostream &os,const P &p){ return os << "(" << p.first << "," << p.second << ")"; }
template<class T>
ostream &operator<<(ostream &os,const vector<T> &v){
for(int i = 0;i < v.size();i++) os << (i ? "," : "[") << v[i];
os << "]";
return os;
}
template<class T> T &chmin(T &a,const T &b){ return a = min(a,b); }
template<class T> T &chmax(T &a,const T &b){ return a = max(a,b); }
const int INF=1e+18;
const double EPS=1e-9;
const int MOD=1000000007;
const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
signed main(){
int a[200010],b[200010],c[200010] = {};
int n,q;
cin >> n;
for(int i = 0;i < n;i++){
int x,t;
cin >> x >> t;
a[i] = -INF;
b[i] = INF;
if(t == 1) c[i] = x;
else if(t == 2) a[i] = x;
else b[i] = x;
if(i){
if(b[i - 1] < a[i] - c[i - 1]) a[i] = b[i] = a[i] - c[i - 1];
else if(b[i] - c[i - 1] < a[i - 1]) a[i] = b[i] = b[i] - c[i - 1];
else{
a[i] = max(a[i] - c[i - 1],a[i - 1]);
b[i] = min(b[i] - c[i - 1],b[i - 1]);
}
c[i] += c[i - 1];
}
}
cin >> q;
for(int i = 0;i < q;i++){
int x;
cin >> x;
if(x <= a[n - 1]) x = a[n - 1];
if(x >= b[n - 1]) x = b[n - 1];
cout << x + c[n - 1] << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
typedef long long lli;
const int N = 200000 + 10;
const int Q = 200000 + 10;
struct Item{
lli val;
int id;
};
lli ans[Q];
int a[N], t[N];
Item x[Q];
bool cmp(const Item& u, const Item& v){
return u.val < v.val;
}
int main(){
lli add, v;
int n, q, left, right;
cin>>n;
for (int i=1; i<=n; i++) cin>>a[i]>>t[i];
cin>>q;
for (int i=1; i<=q; i++){
cin>>x[i].val;
x[i].id = i;
}
sort(x + 1, x + q + 1, cmp);
left = 1, right = q;
add = 0;
for (int i=1; i<=n; i++)
if (t[i] == 1) add += a[i];
else if (t[i] == 2){
v = a[i] - add;
if (x[left].val > v) continue;
while (left <= right && x[left].val <= v) left ++;
left --;
x[left].val = v;
}
else{
v = a[i] - add;
if (x[right].val < v) continue;
while (right >= left && x[right].val >= v) right --;
right ++;
x[right].val = v;
}
for (int i=left; i>=1; i--) x[i].val = x[left].val;
for (int i=right; i<=q; i++) x[i].val = x[right].val;
for (int i=1; i<=q; i++) ans[x[i].id] = x[i].val + add;
for (int i=1; i<=q; i++) cout<<ans[i]<<endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
int main(void){
int n; string s1, s2 = "";
cin >> n >> s1;
if (n < 3){cout << n << endl; return 0;}
s2 = s1.substr(n - 2, 2);
s1 = s1.substr(0, n - 2);
reverse(all(s2));
auto size = [&]()->int{return s1.length() + s2.length();};
auto dfs = [&](auto dfs, int i1, int i2)->void{
int n1 = s1.length(), n2 = s2.length();
if (n1 < i1 || n2 < i2) return;
string ss1 = s1.substr(n1 - i1, i1), ss2 = s2.substr(n2 - i2, i2);
reverse(all(ss2));
if (ss1 + ss2 == "fox"){
for (int i = 0; i < i1; ++i) s1.pop_back();
for (int i = 0; i < i2; ++i) s2.pop_back();
dfs(dfs, 1, 2);
dfs(dfs, 2, 1);
}
};
while (!s1.empty()){
dfs(dfs, 1, 2);
dfs(dfs, 2, 1);
if (!s1.empty()) s2.push_back(s1.back()), s1.pop_back();
}
cout << size() << endl;
return 0;
} | #include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_multiset tree<long long , null_type,less_equal<long long >, rb_tree_tag,tree_order_statistics_node_update>
#define ordered_set tree<long long , null_type,less<long long >, rb_tree_tag,tree_order_statistics_node_update>
#define fill(arr,c) memset(arr,c,sizeof(arr))
#define perm(r) next_permutation(all(r))
//it gives bool value ans permute the string lexoggraphically
#define sortdesc greater<int>()
#define ll long long int
//std::setfill('0') << std::setw(5) << 25; it adds leaing zeroes;
#define f(i,a) for(int i=0;i<a;i++)
#define fo(i,a) for(int i=1;i<=a;i++)
#define foo(i,x,n,c) for(ll i=x;i<n;i+=c)
#define foi(i,x,n,c) for(ll i=x;i>=n;i+=c)
#define fauto(i,m) for(auto i:m)
#define forall(a) for( auto itr=a.begin();itr!=a.end();itr++)
#define ld long double
#define in push_back
#define mll map<ll,ll>
#define mcl map<char,ll>
#define msl map<string,ll>
#define vi vector<int >
#define vl vector<ll >
#define vp vector< pair<int,int> >
#define vs vector <string>
#define vc vector <char>
#define o(a) cout<<a
#define os(a) cout<<a<<" "
#define en cout<<"\n";
#define testcase ll t ;cin>>t; while(t--)
#define ff first
#define ss second
#define pl pair<ll,ll>
#define pi pair<int,int>
#define all(a) (a).begin(), (a).end()
#define sz(a) (a).size()
#define prec(n) fixed<<setprecision(n)
#define mp make_pair
#define bitcount __builtin_popcountll
#define imin o("imin");en;
using namespace std;
bool comp(const pair<int,int> &a , const pair<int,int> &b){
if(a.first == b.first) return a.second < b.second;
return a.first < b.first;
}
bool comps(const pair<int,int> &a , const pair<int,int> &b){
if(a.second == b.second) return a.first< b.first;
return a.second < b.second;
}
//ll pi=acos(0.0)*2;
void fastio()
{
ios::sync_with_stdio(0);
cin.tie(0);
}
void inout()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
ll ans=0;
ll chk(ll i,ll gap,ll n,string s)
{
string f;
if(gap==0)
{
if(s[i]=='f' && s[i+1]=='o' && s[i+2]=='x')
{
ans+=3;
// os(i);os(0);en;
ll x=chk(i,gap+3,n,s);
}
// os(f);
}
else
{
if(i-1>=0 && i+2<n)
{
if(s[i-1]=='f' && s[i+gap]=='o' && s[i+1+gap]=='x')
{
ans+=3;
// os(i);os(1);en;
ll x=chk(i-1,gap+3,n,s);
}
// os(f);
}
else if(i-2>=0 && i+1<n)
{
if(s[i-2]=='f' && s[i-1]=='o' && s[i+gap]=='x')
{
// os(i);os(2);en;
ans+=3;
ll x=chk(i-2,gap+3,n,s);
}
//os(f);
}
return 0;
}
// os(f);en;
return 0;
}
int main()
{
fastio();
ll n;
cin>>n;
string s;
cin>>s;
stack<char> bag;
ll ans=0;
char a,b,c;
f(i,n)
{
bag.push(s[i]);
if(sz(bag)>=3)
{
a=bag.top();
bag.pop();
b=bag.top();
bag.pop();
c=bag.top();
bag.pop();
if(a=='x' && b=='o' && c=='f')
ans++;
else
{
bag.push(c);bag.push(b);bag.push(a);
}
}
}
o(n-3*ans);
en;
return 0;
}
|
#include <bits/stdc++.h>
#define REP(i, nn ) for(int i = 0 ; i < (int) nn; i++)
#define REPS(i, ss, nn ) for(int i = ss ; i < (int) nn; i++)
#define REV(i, ss, nn ) for(int i = ss ; i >= nn; i--)
#define deb(x) std::cout << #x << " " << x << endl;
#define debl(x) std::cout << #x << " " << x << " ";
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
namespace std{
template<class Fun>
class y_combinator_result{
Fun fun_;
public:
template<class T>
explicit y_combinator_result(T &&fun) : fun_(std::forward<T>(fun)){}
template<class ...Args>
decltype(auto) operator()(Args&&...args){
return fun_(std::ref(*this), std::forward<Args>(args)...);
}
};
template<class Fun>
decltype(auto) y_combinator(Fun && fun){
return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}
};
template<typename T>
bool u_max(T& a, T b){
if( a < b){
a = b;
return true;
}
return false;
}
template<typename T>
bool u_min(T& a, T b){
if( a > b){
a = b;
return true;
}
return false;
}
// struct edge{
// int to, from;
// ll cost;
// edge(){ edge(0,0);}
// edge(int to_, ll cost_){ edge(0, -1, 0);}
// edge(int to_, int from_, ll cost_) : to(to_), from(from_), cost(cost_){}
// };
template<typename... T>
void read(T& ... a){
((cin >> a),...);
}
void solve(){
ll x, y;
read(x, y);
ll ans = abs(y - x);
ll base = 2 * x;
int n_pow = 1;
auto count_bit = [&](ll d, int bit_num){
ll ret = 0;
int count = 0;
for(int i = 0 ; i < bit_num; i++){
// deb(d) deb(last)
if( d % 2 == 1){
count++;
}else{
if(count >= 2){
ret++;
d += 1;
count = 1;
}else{
ret += count;
count = 0;
}
}
d /= 2;
}
// debl(ret) debl(count) deb(d)
if(count <= 2){
return ret + count + d;
}else{
//count -> 2
// d -> d -1
return ret + d + 2;
}
};
// 11100010 ==> 1000010 - 10000
while(abs(base - y) < 4 * abs(x - y) ){
ll diff = abs(y - base);
// debl(base) deb(diff)
ll op_num = count_bit(diff, n_pow);
// deb(op_num)
u_min(ans, n_pow + op_num);
base *= 2;
n_pow++;
}
cout << ans << endl;
}
int main()
{
//making data IO Fast
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
/****************************/
solve();
return 0;
}
| #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
signed main() {
ios::sync_with_stdio(false); cin.tie(0);
//f
long long x, y;
cin >> x >> y;
map<long long, long long> memo;
auto rec = [&] (auto&& self, long long cur) -> long long {
if (memo.count(cur)) return memo[cur];
if (cur <= x) return x - cur;
if (cur % 2 == 0) {
return memo[cur] = min(cur - x, self(self, cur / 2) + 1);
} else {
return memo[cur] = min({cur - x, self(self, (cur + 1) / 2) + 2, self(self, (cur - 1) / 2) + 2});
}
};
cout << rec(rec, y) << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define INF 1<<30
#define endl '\n'
#define maxn 1000005
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
typedef long long ll;
const double PI = acos(-1.0);
const ll mod = 1e9 + 7;
inline void normal(ll &a) { a %= mod; (a < 0) && (a += mod); }
inline ll modMul(ll a, ll b) { a %= mod, b %= mod; normal(a), normal(b); return (a * b) % mod; }
inline ll modAdd(ll a, ll b) { a %= mod, b %= mod; normal(a), normal(b); return (a + b) % mod; }
inline ll modSub(ll a, ll b) { a %= mod, b %= mod; normal(a), normal(b); a -= b; normal(a); return a; }
inline ll modPow(ll b, ll p) { ll r = 1; while (p) { if (p & 1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
inline ll modInverse(ll a) { return modPow(a, mod - 2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }
///**
template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
return os << "(" << p.first << ", " << p.second << ")";
}
template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
os << "{";
for (auto it = v.begin(); it != v.end(); ++it) {
if ( it != v.begin() ) os << ", ";
os << *it;
}
return os << "}";
}
template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
os << "[";
for (auto it = v.begin(); it != v.end(); ++it) {
if ( it != v.begin()) os << ", ";
os << *it;
}
return os << "]";
}
template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
os << "[";
for (auto it = v.begin(); it != v.end(); ++it) {
if ( it != v.begin() ) os << ", ";
os << it -> first << " = " << it -> second ;
}
return os << "]";
}
#define dbg(args...) do {cerr << #args << " : "; faltu(args); } while(0)
clock_t tStart = clock();
#define timeStamp dbg("Execution Time: ", (double)(clock() - tStart)/CLOCKS_PER_SEC)
void faltu () { cerr << endl; }
template <typename T>
void faltu( T a[], int n ) {
for (int i = 0; i < n; ++i) cerr << a[i] << ' ';
cerr << endl;
}
template <typename T, typename ... hello>
void faltu( T arg, const hello &... rest) { cerr << arg << ' '; faltu(rest...); }
// Program showing a policy-based data structure.
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp>
#include <functional> // for less
#include <iostream>
using namespace __gnu_pbds;
using namespace std;
// GNU link : https://goo.gl/WVDL6g
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
tree_order_statistics_node_update>
new_data_set;
// find_by_order(k) – ফাংশনটি kth ordered element এর একটা পয়েন্টার রিটার্ন করে। অর্থাৎ তুমি চাইলেই kth ইন্ডেক্সে কি আছে, সেটা জেনে ফেলতে পারছো!
// order_of_key(x) – ফাংশনটি x এলিমেন্টটা কোন পজিশনে আছে সেটা বলে দেয়।
//*//**___________________________________________________**/
int main()
{
FASTIO
/*
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
//*/
int T;
//scanf("%d", &T);
T = 1;
for (int cs = 1; cs <= T; cs++) {
int n, k, m;
cin >> n >> k >> m;
int sm = 0;
for (int i = 1; i < n; i++) {
int x;
cin >> x;
sm += x;
}
for (int i = 0; i <= k; i++) {
int x = sm + i;
if (x / n >= m) {
cout << i << endl;
return 0;
}
}
cout << -1 << endl;
return 0;
}
return 0;
} | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
#define ls(x) (x << 1)
#define rs(x) (x << 1 | 1)
#define lowbit(x) (x & -x)
const ll INF=0x3f3f3f3f;
const ll LL_INF=0x3f3f3f3f3f3f3f3f;
const double EPS=1e-7;
ll mod=1e9+7;
const ll MOD=998244353;
ll qpow(ll base,ll q){
ll res=1;
while(q){
if(q&1)res=res*base;
base=base*base;
q>>=1;
}
return res;
}
inline ll read()
{ ll f=1,x=0;char ch=getchar();
for(;ch>'9'||ch<'0';ch=getchar())if(ch=='-') f=-1;
for(;ch<='9'&&ch>='0';ch=getchar())x=x*10+ch-'0';
return x*f;
}
const int N = 500010;
struct node {
ll to, t, k, ne;
bool operator < (const node &a) const{
return t > a.t;
}
}e[N];
ll d[N], st[N];
ll n, m, s, y;
ll h[N], idx;
void add(ll x, ll y, ll t, ll k){
e[++idx] = {y, t, k, h[x]};
h[x] = idx;
}
ll dijkstra(){
memset(d, 0x3f, sizeof d);
priority_queue<node> q;
d[s] = 0;
q.push({s, 0});
while (!q.empty()){
node t = q.top();
q.pop();
ll x = t.to;
if (st[x]) continue;
st[x] = 1;
for (int i = h[x]; i != 0; i = e[i].ne){
ll to = e[i].to, t = e[i].t, k = e[i].k;
ll temp = (d[x] + k - 1) / k * k;
if (d[to] > t + temp){
d[to] = t + temp;
q.push({to, d[to]});
}
}
}
return d[y] == LL_INF ? -1 : d[y];
}
void solve(){
cin >> n >> m >> s >> y;
for (int i = 1; i <= m; i++){
ll a, b, k, t;
scanf("%lld%lld%lld%lld", &a, &b, &t, &k);
add(a, b, t, k);
add(b, a, t, k);
}
cout << dijkstra();
}
int main(){
int t = 1;
while (t--) solve();
return 0;
} |
/* Great things never come from comfort zones,
"whatever the mind of a man can conceive and believe,it can achieve." */
#include <bits/stdc++.h>
using namespace std;
void my_dbg() { cout << endl; }
template<typename Arg, typename... Args> void my_dbg(Arg A, Args... B)
{ cout << ' ' << A; my_dbg(B...); }
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", my_dbg(__VA_ARGS__)
#define ll long long
#define dd double
#define scf(n) scanf("%d",&n)
#define lscf(n) scanf("%lld",&n)
#define lpri(n) printf("%lld ",n)
#define pri(n) printf("%d ",(int)n)
#define prin(n) printf("%d\n",(int)n)
#define lprin(n) printf("%lld\n",n)
#define rep(i,ini,n) for(int i=ini;i<(int)n;i++)
#define show(a) for(auto xy: a) pri(xy); printf("\n");
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define SET(a,b) memset(a,b,sizeof(a))
#define tc int tt; scf(tt); while(tt--)
#define inf INT_MAX
#define ninf INT_MIN
#define gcd __gcd
#define bitcount(n) __builtin_popcount(n)
const int mod=1e9+7;
const int N = 1e6+7;
int pos[N];
int main()
{
int n; scf(n);
vector<ll>a(n),b(n);
vector<int>ind(n);
iota(all(ind),0);// iterates and fill with increaing values
//0 1 2 3 4.......
ll A=0,B=0,tmp;
rep(i,0,n)
lscf(a[i]),lscf(b[i]);
sort(all(ind),[&](const int &i , const int &j)
{
return (2*a[i]+b[i]>2*a[j]+b[j]);
}); // net effect 2*a[k] + b[k] in town of speech
A=accumulate(all(a),0ll);
int c=0,d=0;
rep(i,0,n)
{
if(B>A)
break;
c++;
int j=ind[i];
B+=b[j]+a[j];
A-=a[j];
}
prin(c);
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define veci vector<int>
#define vecll vector<ll>
#define ii pair<ll, ll>
#define iii pair<ii, ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define lb lower_bound
#define up upper_bound
#define rep(x, start, end) for (auto x = (start) - ((start) > (end)); x != (end) - ((end) < (start)); ((start) < (end) ? x++ : x--))
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define indexed_set tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
//change less to less_equal for non distinct pbds, but erase will bug
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin.exceptions(ios::badbit | ios::failbit);
int a, b;
cin >> a >> b;
int c = a + b;
if (c >= 15 && b >= 8)
cout << 1 << endl;
else if (c >= 10 && b >= 3)
cout << 2 << endl;
else if (c >= 3)
cout << 3 << endl;
else
cout << 4 << endl;
return 0;
return 0;
} |
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define replr(i, l, r) for (int i = (l); i < (r); ++i)
const int mod = 1e9 + 7;
int main() {
int a, b;
cin >> a >> b;
if (a == b) {
cout << a << endl;
}
int k = 7;
a = 1 << a;
b = 1 << b;
if ((k ^ a ^ b) == 1) {
cout << 0 << endl;
}
if ((k ^ a ^ b) == 2) {
cout << 1 << endl;
}
if ((k ^ a ^ b) == 4) {
cout << 2 << endl;
}
return 0;
} | #include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <sstream>
#include <stack>
#include <set>
#include <bitset>
#include <deque>
#include <vector>
using namespace std;
int main()
{
char w[55],s[55];
scanf("%s",w);
int j=0;
while(w[j]=='0') j++;
for(int i=j;i<strlen(w);i++)
s[i-j]=w[i];
//printf("%d %d \n",strlen(w),strlen(s));
int len=strlen(w)-j;
if(len==1)
{
printf("0");
return 0;
}
if(len&1)
{
for(int i=0;i<len/2;i++)
printf("9");
}
else
{
int a=0,b=0;
for(int i=0;i<len/2;i++)
a=a*10+(int)(s[i]-'0');
for(int i=len/2;i<len;i++)
b=b*10+(int)(s[i]-'0');
if(a<=b) printf("%d",a);
else printf("%d",a-1);
}
//system("pause");
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
void fast() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
vector<string> vec_splitter(string s) {
s += ',';
vector<string> res;
while(!s.empty()) {
res.push_back(s.substr(0, s.find(',')));
s = s.substr(s.find(',') + 1);
}
return res;
}
void debug_out(
vector<string> __attribute__ ((unused)) args,
__attribute__ ((unused)) int idx,
__attribute__ ((unused)) int LINE_NUM) { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
if(idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") ";
stringstream ss; ss << H;
cerr << args[idx] << " = " << ss.str();
debug_out(args, idx + 1, LINE_NUM, T...);
}
#ifdef LOCAL
#define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
#define debug(...) 42
#endif
double get_time() {
return 1.0 * clock() / CLOCKS_PER_SEC;
}
int main() {
int n;
string s;
cin >> n >> s;
vector<int> a(n);
int k = 1e9;
for(int i = 0; i <= n; i++) {
cin >> a[i];
if(i - 1 >= 0)
k = min(k, abs(a[i] - a[i - 1]));
}
cout << k << '\n';
for(int i = 0; i < k; i++) {
for(int j = 0; j <= n; j++) {
cout << (a[j] + i) / k << ' ';
}
cout << '\n';
}
} |
#include <bits/stdc++.h>
#define ll long long
#define sz(x) ((int) (x).size())
#define all(x) (x).begin(), (x).end()
#define vi vector<int>
#define pii pair<int, int>
#define rep(i, a, b) for(int i = (a); i < (b); i++)
using namespace std;
template<typename T>
using minpq = priority_queue<T, vector<T>, greater<T>>;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
string s;
cin >> n >> s;
vi a(n + 1);
rep(i, 0, n + 1) cin >> a[i];
int ans = INT_MAX;
rep(i, 0, n) ans = min(ans, abs(a[i] - a[i + 1]));
vector<vi> b(n + 1, vi(ans, 0));
int k = 0;
rep(i, 0, n + 1) {
rep(j, 0, ans) b[i][j] = a[i] / ans;
int dif = a[i] - (a[i] / ans) * ans;
if(i % 2 == 0) {
rep(j, 0, dif) {
b[i][k]++;
k = (k + 1) % ans;
}
}else {
rep(j, 0, dif) {
k = (k + ans - 1) % ans;
b[i][k]++;
}
}
}
cout << ans << '\n';
rep(j, 0, ans) {
rep(i, 0, n + 1) {
cout << b[i][j] << ' ';
}
cout << '\n';
}
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
int W;
cin >> N >> W;
int count = 0;
int weight = 0;
while (weight <= N-W){
count++;
weight += W;
}
cout << count << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for(int i = (int)(a); i <= (int)(b); ++i)
#define FORR(i, a, b) for(int i = (int)(a); i >= (int)(b); --i)
#define ALL(c) (c).begin(), (c).end()
using ll = long long;
using VI = vector<int>;
using VL = vector<ll>;
using VD = vector<double>;
using VII = vector<VI>;
using VLL = vector<VL>;
using VDD = vector<VD>;
using P = pair<int, int>;
using PL = pair<ll, ll>;
void solve(long long N, long long W) {
cout << N / W << endl;
}
int main() {
long long N;
scanf("%lld", &N);
long long W;
scanf("%lld", &W);
solve(N, W);
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); i++)
#define reps(i,s,n) for(int i=(s); i<(n); i++)
#define all(v) v.begin(),v.end()
#define outve(v) for(auto i : v) cout << i << " ";cout << endl
#define outmat(v) for(auto i : v){for(auto j : i) cout << j << " ";cout << endl;}
#define in(n,v) for(int i=0; i<(n); i++){cin >> v[i];}
#define out(n) cout << (n) << endl
#define fi first
#define se second
#define pb push_back
#define si(v) int(v.size())
#define len(v) int(v.length())
#define lob(v,n) lower_bound(all(v),n)
#define lobi(v,n) lower_bound(all(v),n) - v.begin()
#define upb(v,n) upper_bound(all(v),n)
#define upbi(v,n) upper_bound(all(v),n) - v.begin()
#define mod 1000000007
#define infi 1010000000
#define infl 1100000000000000000
#define cyes cout << "Yes" << endl
#define cno cout << "No" << endl
#define csp << " " <<
#define outset(n) cout << fixed << setprecision(n);
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vd = vector<double>;
using vvd = vector<vector<double>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
template<typename T> using ve = vector<T>;
template<typename T> using pq2 = priority_queue<T>;
template<typename T> using pq1 = priority_queue<T,vector<T>,greater<T>>;
template<typename T> bool chmax(T &a, T b) {if(a < b) {a = b;return 1;}return 0;}
template<typename T> bool chmin(T &a, T b) {if(a > b) {a = b;return 1;}return 0;}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
string s;
cin >> s;
reverse(all(s));
int N = si(s);
char a = '#';
vi A(30,0);
ll ans = 0;
rep(i,N){
if(s[i] == a){
rep(j,30){
if(j != int(s[i]-'a')) ans += (ll)A[j];
}
A.assign(30,0);
A[int(s[i]-'a')] = i+1;
}else{
A[int(s[i]-'a')]++;
a = s[i];
}
}
cout << ans << endl;
return 0;
}
| //yukicoder@cpp14
//writer:luckYrat(twitter:@luckYrat_)
//競技プログラミングから逃げるな
//https://www.youtube.com/watch?v=z_B9iJ8r4ic
//せんげん!
#include <iostream>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <bitset>
#include <cctype>
#include <utility>
#include <climits>
//なまえくーかん!
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
//てーすう!
const int mod = 1000000007;
const int inf = (1<<30)-1;
const ll linf = (1LL<<62LL)-1;
const double EPS = (1e-10);
//でふぁいん!
#define anyfill(n,s) setw(n) << setfill(s)
#define loop(s) for(int i = 0; s > i; i++)
#define rep(i,q) for(int i = 0; (q) > i; i++)
#define repp(i,n,q) for(int i = n; (q) > i; i++)
#define dep(i,q) for(int i = (q); 0 < i; i--)
//みじかく!
#define pb push_back
#define fir first
#define scn second
#define ednl endl
//いぇすのー!
#define YesNo(a) (a?"Yes":"No")
#define YESNO(a) (a?"YES":"NO")
#define yesno(a) (a?"yes":"no")
//きんぼーnほーこー!!
P ar4[4] = {{0,1},{0,-1},{1,0},{-1,0}};
P ar8[8] = {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
/*
確認ポイント
cout << fixed << setprecision(n) << 小数計算//n桁の小数表記になる
計算量は変わらないが楽できるシリーズ
min(max)_element(iter,iter)で一番小さい(大きい)値のポインタが帰ってくる
count(iter,iter,int)でintがiterからiterの間にいくつあったかを取得できる
*/
//B
template <int mod>
struct ModInt{
int n;
ModInt():n(0){}
ModInt(int n_):n(n_ >= 0 ? n_%mod : mod - ((-n_)%mod) ){}
ModInt &operator+=(const ModInt &p){
if((n+=p.n) >= mod)n-=mod;
return *this;
}
ModInt &operator-=(const ModInt &p){
n+=mod-p.n;
if(n >= mod)n-=mod;
return *this;
}
ModInt &operator*=(const ModInt &p){
n = (int) ((1LL*n*p.n)%mod);
return *this;
}
ModInt &operator/=(const ModInt &p){
*this *= p.inverse();
return *this;
}
ModInt operator-() const {return ModInt(-n);}
ModInt operator+(const ModInt &p) const {return ModInt(*this) += p;}
ModInt operator-(const ModInt &p) const {return ModInt(*this) -= p;}
ModInt operator*(const ModInt &p) const {return ModInt(*this) *= p;}
ModInt operator/(const ModInt &p) const {return ModInt(*this) /= p;}
bool operator==(const ModInt &p) const {return n==p.n;}
bool operator<(const ModInt &p) const {return n<p.n;}
bool operator>(const ModInt &p) const {return n>p.n;}
bool operator>=(const ModInt &p) const {return n>=p.n;}
bool operator<=(const ModInt &p) const {return n<=p.n;}
bool operator!=(const ModInt &p) const {return n!=p.n;}
ModInt inverse() const {
int a = n,b = mod,u = 1,v = 0;
while(b){
int t = a/b;
a -= t*b; swap(a,b);
u -= t*v; swap(u,v);
}
return ModInt(u);
}
ModInt pow(int64_t z) const {
ModInt ret(1),mul(n);
while(z > 0){
if(z & 1) ret *= mul;
mul *= mul;
z >>= 1;
}
return ret;
}
friend ostream &operator<<(ostream &os, const ModInt &p){
return os << p.n;
}
friend istream &operator>>(istream &is, ModInt &a){
int64_t t;
is >> t;
a = ModInt<mod> (t);
return (is);
}
};
using mint = ModInt<mod>;
//E
ll alp[26];
__attribute__((constructor))
void initial() {
cin.tie(0);
ios::sync_with_stdio(false);
}
int main(){
string s;cin>>s;
ll ans = 0;
alp[s[s.size()-1]-'a']++;
alp[s[s.size()-2]-'a']++;
for(ll i = s.size()-3; 0 <= i; i--){
if(s[i]==s[i+1] && s[i] != s[i+2]){
ans += (s.size()-(i+2)-(alp[s[i]-'a']-1));
for(ll j = 0; 26 > j; j++){
if(s[i]=='a'+j){
alp[j] = s.size()-i;
}else{
alp[j] = 0;
}
}
}else{
alp[s[i]-'a']++;
}
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, i, last, cc;
cin >> n;
vector<int> p(n);
vector<int> sw(200001);
last = 0;
cc = 0;
for (i = 0; i < 200001; ++i)
{
sw[i] = 0;
}
for (i = 0; i < n; ++i)
{
cin >> p[i];
sw[p[i]]=1;
while (cc == 0)
{
if (sw[last] == 0)
cc++;
else
last++;
}
cc = 0;
cout << last << endl;
}
return 0;
} | #include <iostream>
#include <algorithm>
#include <list>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <chrono>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#define manx maxn
#define whlie while
#define itn int
#define fro for
#define asn ans
#define scnaf scanf
#define sacnf scanf
#define pritnf printf
#define haed head
#define tmep temp
#define udpate update
#define cosnt const
#define Fastin freopen("/home/rqdmap/Desktop/codes/in", "r", stdin)
#define Fastout freopen("/home/rqdmap/Desktop/codes/main.out", "w", stdout)
#define lowbit(x) ((x) & (-x))
#define clr(a, x) memset((a), x, sizeof (a))
#define TTT int __; scanf("%d", &__); for(int _ = 1; _ <= __; _++)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
/*------------------------------------------------------------------------------------*/
template<typename T>
void prt(T *a, int n, bool withid = 0){
if(!withid) for(int i = 0; i < n; i++) cout << a[i] << ((i == n - 1)? '\n':' ');
else for(int i = 0; i < n; i++) cout << i << ": " << a[i] << '\n';
}
template<typename T>
T ceil(T a, T b){return (a + b - 1) / b;}
template<typename T>
T gcd(T x, T y){return !x? y: gcd(y % x, x);}
template<typename T>
void exgcd(T a, T b, T &x, T &y){
if(!b){x = 1; y = 0;return;}
exgcd(b, a % b, y, x); y -= a / b * x;
}
ll qp(ll a, ll p){
if(p < 0) return 0;
ll ans = 1; while(p){if(p & 1) ans = ans * a ; a = a * a; p >>= 1;}
return ans;
}
ll qp(ll a, ll p, ll M){
ll ans = 1; while(p){ if(p & 1) ans = ans * a % M; a = a * a % M; p >>= 1;}
return (ans % M + M) % M;
}
int euler(int *prime, int *vis, int n){
int top = 0;
for(int i = 0; i <= n; i++) vis[i] = 0;
for(int i = 2; i <= n; i++){
if(!vis[i]) prime[top++] = i;
for(int j = 0; j < top && i * prime[j] <= n; j++){
vis[i * prime[j]] = 1;
if(i % prime[j] == 0) break;
}
}
return top;
}
struct star{int to, next, w;};
/*------------------------------------------------------------------------------------*/
int main(){
// Fastin;
TTT{
ll n; scnaf("%lld", &n);
if(n & 1) puts("Odd");
else{
n /= 2;
if(n % 2 == 0) puts("Even");
else puts("Same");
}
}
return 0;
} |
/* Author: rrrr_wys
**/
#include <bits/stdc++.h>
#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 pb push_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second
typedef double db;
typedef long long ll;
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
const int N = 810;
const int P = 1e9 + 7;
int bn, bm, bit[N][N];
void init(int n, int m) {
memset(bit, 0, sizeof bit);
bn = n, bm = m;
}
void add(int x, int y) {
for (int i = x; i <= bn; i += i & -i)
for (int j = y; j <= bm; j += j & -j)
++bit[i][j];
}
int ask(int x, int y) {
int ans = 0;
for (int i = x; i > 0; i -= i & -i)
for (int j = y; j > 0; j -= j & -j)
ans += bit[i][j];
return ans;
}
int n, k, a[N][N], b[N][N];
int dx[4] = {0, -1, -1, 0};
int dy[4] = {0, -1, 0, -1};
int inb(int x, int y, int k) {
if (x < 1 || x > n || y < 1 || y > n)
return 0;
x += k;
y += k;
if (x < 1 || x > n || y < 1 || y > n)
return 0;
return 1;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> k;
int rnk = k * k - ((k * k) / 2 + 1) + 1;
// cout << rnk << "\n";
vector<pair<ll, pii>> v;
rep(i, 1, n) rep(j, 1, n) cin >> a[i][j], v.pb({a[i][j], {i, j}});
sort(v.begin(), v.end());
auto ck = [&](int m) {
init(n, n);
rep(i, 0, v.size() - 1) if (v[i].fi <= m) add(v[i].se.fi, v[i].se.se);
rep(i, 1, n) rep(j, 1, n) if (i + k - 1 <= n && j + k - 1 <= n) {
int tt = ask(i + k - 1, j + k - 1) - ask(i + k - 1, j - 1) -
ask(i - 1, j + k - 1) + ask(i - 1, j - 1);
if (tt >= rnk)
return 1;
}
return 0;
};
int l = v[0].fi, r = v.back().fi, ans = -1;
while (l <= r) {
int mid = (l + r) >> 1;
if (ck(mid))
r = mid - 1, ans = mid;
else
l = mid + 1;
}
cout << ans << "\n";
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define per(i, a, b) for (auto i = (b); i-- > (a); )
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) int((x).size())
#define lb(x...) lower_bound(x)
#define ub(x...) upper_bound(x)
template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll B = 10000;
const ll inf = B * (2e5 + 30);
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ld p, q, s; cin >> p >> q >> s;
ll a = round(B * p);
ll b = round(B * q);
ll r = round(B * s);
ll ans = 0;
for (ll x = -inf; x <= inf; x += B) {
ll d = r * r - (x - a) * (x - a);
if (d < 0) continue;
ll l = 0, r = sqrt(d) + 30;
while (r - l > 1) {
ll m = l + (r - l) / 2;
(m * m <= d ? l : r) = m;
}
ans += (b + l + inf) / B - (b - r + inf) / B;
}
cout << ans;
} |
#include<bits/stdc++.h>
typedef long long int ll;
#define pb push_back
#define pi 3.141592653589793238462643383279
#define int long long
#define ff first
#define ss second
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define rep(i,x,y) for(int i=(int)x; i<y; i++)
#define high_functioning_sociopath ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
const unsigned int M = 998244353;
//__________THE GAME IS ON__________/
void solve()
{
int n;
cin >> n;
vector<pair<int,int>>v(n),res(n);
for(int i=0;i<n;i++)
{
cin >> v[i].ff >> v[i].ss;
res[i].ff = (2*v[i].ff)+v[i].ss;
res[i].ss = v[i].ff;
}
sort(all(res));
reverse(all(res));
int pre[n+2],suf[n+2];
pre[0] = 0; pre[n+1]=0;suf[0]=0;suf[n+1]=0;
for(int i=0;i<n;i++) pre[i+1] = pre[i] + (res[i].ff-res[i].ss);
for(int i=n;i>0;i--) suf[i]=suf[i+1] + res[i-1].ss;
// for(int i=0;i<=n+1;i++)cout << pre[i] << " ";
// cout << endl;
// for(int i=0;i<=n+1;i++)cout << suf[i] << " ";
// cout << endl;
int lo = 0 ,hi = n-1,mid,ans;
while(lo<=hi)
{
mid = (lo+hi)/2;
int res = pre[mid+1]- suf[mid+2];
// cout<<pre[mid]<<" "<<suf[mid+1]<<endl;
if(res>0)
{
ans = mid+1;
hi = mid-1;
}
else
{
lo = mid+1;
}
}
cout << ans << endl;
}
int32_t main()
{
high_functioning_sociopath
//pre();
ll t=1;
// cin>>t;
while(t--)
{
solve();
}
} | #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>;
using ll = long long;
using P = pair<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){cout << 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;
}
void Main(){
I(ll, N);
ll cnt = 0;
umap<ll, bool> mp;
FOR(i, sqrt(N)+1, 2){
ll cnt = 0;
for(ll j=(ll)i*i; j<=N;){
mp[j] = true;
if(j < 0){
O(i, j, cnt);
}
if(j>N/i){
break;
}
j*=i;cnt++;
}
}
cout << N-mp.size() << el;
}
|
#include<bits/stdc++.h>
using namespace std;
bool dp[1000005];
int main(){
int n,sum=0,x;
cin>>n;
dp[0]=1;
while(n--){
scanf("%d",&x);
for(int i=sum+x;i>=x;i--){
dp[i]|=dp[i-x];
}
sum+=x;
}
int ans=sum;
for(int i=0;i<=sum;i++)
if(dp[i])ans=min(ans,max(i,sum-i));
printf("%d\n",ans);
return 0;
} | #include <iostream>
#include <vector>
int main()
{
int N;
std::cin>>N;
std::vector<int64_t>T;
for(int i=0;i<N;i++)
{
int64_t t;
std::cin>>t;
T.push_back(t);
}
int sum=0;
for(auto e:T)sum+=e;
bool g[101][100001]{0};
g[0][0]=true;
for(int i=0;i<=N-1;i++)
{
for(int64_t j=0;j<=sum;j++)
{
if(j-T[i]>=0)
{
g[i+1][j]=g[i][j]|g[i][j-T[i]];
}
else
{
g[i+1][j]=g[i][j];
}
}
}
int64_t res=0;
for(int64_t i=sum/2+(sum%2);i<=sum;i++)
{
if(g[N][i])
{
res=i;
break;
}
}
std::cout<<res<<std::endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
// #pragma GCC optimize("O2")
typedef long long int ll;
typedef long double ld;
typedef map <ll,ll> mm;
typedef vector <ll> mv;
typedef pair <ll,ll> mp;
typedef set <ll> ms;
typedef multiset <ll> mms;
typedef queue <ll> mq;
typedef deque <ll> mdq;
typedef stack <ll> mst;
typedef priority_queue <ll> mpq;
typedef priority_queue <ll, vector<ll>, greater<ll> > mmh;
typedef complex<double> cd;
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define ordered_multiset tree<ll,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update>
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define flush cout.flush();
#define all(v) v.begin(),v.end()
#define deb(x) cout << #x << " " << x << endl;
#define ff first
#define ss second
#define pb(v) push_back(v)
#define lb(v) lower_bound(v)
#define ub(v) upper_bound(v)
#define cn continue
#define forl(i,n) for(ll i=0;i<n;i++)
#define forlr(i,n) for(ll i=n-1;i>=0;i--)
#define rev(s) reverse(s.begin(),s.end())
void swap(ll &a,ll &b) {
ll tmp=a;
a=b;
b=tmp;
}
ll atoistring(string s) {
stringstream lol(s);
ll x;
lol>>x;
return x;
}
void pv(mv v) {
forl(i,v.size()) cout<<v[i]<<" ";
cout<<"\n";
}
void pa(ll *v, ll size) {
forl(i,size) cout<<v[i]<<" ";
cout<<"\n";
}
void removeDups(mv &v) {
sort(all(v));
mv::iterator it = unique(v.begin(), v.end());
v.resize(distance(v.begin(), it));
}
inline ld kthRoot(ld n, ll k) {
return pow(k, (1.0 / k) * (log(n) / log(k)));
}
ll power(ll x, ll y, ll p) {
ll res=1;
x=x%p;
while(y>0) {
if(y&1) res = (res*x)%p;
y=y>>1;
x=(x*x)%p;
}
return res;
}
void checkmod(ll &x, ll mod) {
if(x>=mod) x%=mod;
}
ll mod = 1e9 + 7;
mv adj[200005], queries[200005];
vector < mp > queriess;
bool vis[200005];
ll vals[200005];
map < mp , ll > ans;
mm mapArr[200005];
ll mapIndex;
void DFSHt(ll curHt, ll x) {
if(vis[x]) return ;
vals[x] = curHt;
vis[x] = 1;
curHt ++ ;
forl(i,adj[x].size()) DFSHt(curHt, adj[x][i]);
}
ll findAns(ll x) {
if(vis[x]) return -1;
vis[x] = 1;
if(x != 0 && adj[x].size() == 1) {
// cout<<x<<"-\n";
mapArr[mapIndex][vals[x]] ++ ;
// for(auto v : mapArr[mapIndex]) cout<<v.ff<<" "<<v.ss<<"\n";
forl(i,queries[x].size()) ans[{x, queries[x][i]}] = mapArr[mapIndex][queries[x][i]];
mapIndex ++ ;
return mapIndex - 1;
}
mv mapps;
forl(i,adj[x].size()) {
ll y = findAns(adj[x][i]);
if(y != -1) mapps.pb(y);
}
// cout<<x<<"-\n";
// pv(mapps);
ll pos = 0, maxSize = -1;
forl(i,mapps.size()) {
if(maxSize < (ll)mapArr[mapps[i]].size()) {
maxSize = mapArr[mapps[i]].size();
pos = i;
}
}
forl(i,mapps.size()) {
if(i != pos) {
for(auto v : mapArr[mapps[i]]) {
// cout<<v.ff<<" "<<v.ss<<"\n";
mapArr[mapps[pos]][v.ff] += v.ss;
}
}
}
// exit(0);
// cout<<x<<"-\n";
// for(auto v : mapArr[mapps[pos]]) cout<<v.ff<<" "<<v.ss<<"\n";
mapArr[mapps[pos]][vals[x]] ++ ;
forl(i,queries[x].size()) ans[{x, queries[x][i]}] = mapArr[mapps[pos]][queries[x][i]];
return mapps[pos];
}
void thisIsMain() {
ll n;
cin>>n;
forl(i,n) {
adj[i].clear();
mapArr[i].clear();
}
queriess.clear();
ans.clear();
forl(i,n-1) {
ll x;
cin>>x;
x -- ;
adj[x].pb(i + 1);
adj[i + 1].pb(x);
}
forl(i,n) vis[i] = 0;
DFSHt(1, 0);
// pa(vals, n);
ll q;
cin>>q;
while(q--) {
ll x, y;
cin>>x>>y;
x -- ;
queriess.push_back({x, y + 1});
queries[x].pb(y + 1);
}
mapIndex = 0;
forl(i,n) vis[i] = 0;
findAns(0);
forl(i,queriess.size()) cout<<ans[queriess[i]]<<"\n";
}
int main(void)
{
fastio
ll t = 1;
// cin>>t;
while(t--) thisIsMain();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll LINF = 1001001001001001001;
#ifdef _DEBUG
#define show(x) \
cerr << #x << " : "; \
showVal(x)
template <typename T>
void showVal(const T &a) { cerr << a << endl; }
template <typename T>
void showVal(const vector<T> &a) {
for (const T &v : a) cerr << v << " ";
cerr << endl;
}
template <typename T>
void showVal(const vector<vector<T>> &a) {
cerr << endl;
for (const vector<T> &v : a) showVal(v);
}
#else
#define show(x)
#endif
struct graph {
int n;
vector<vector<int>> g;
vector<ll> dist;
vector<bool> vis;
vector<int> a, b;
graph(int n) : n(n), g(n), dist(n, LINF), vis(n, false), a(n), b(n) {}
void addEdge(int from, int to) {
g[from].push_back(to);
}
void addEdgeBoth(int n1, int n2) {
addEdge(n1, n2);
addEdge(n2, n1);
}
};
int t = 0;
void dfs(graph &G, int no) {
G.vis[no] = true;
G.a[no] = t;
t++;
for (auto v : G.g[no]) {
if (G.vis[v]) continue;
G.dist[v] = G.dist[no] + 1;
dfs(G, v);
}
G.b[no] = t;
t++;
return;
}
int main() {
int n;
cin >> n;
graph g(n);
for (int i = 0; i < n - 1; i++) {
int p;
cin >> p;
p--;
g.addEdgeBoth(i + 1, p);
}
g.dist[0] = 0;
dfs(g, 0);
show(g.a);
show(g.b);
vector<vector<int>> v(n);
for (int i = 0; i < n; i++) {
v[g.dist[i]].push_back(g.a[i]);
}
for (int i = 0; i < n; i++) {
sort(v[i].begin(), v[i].end());
}
int q;
cin >> q;
for (int qi = 0; qi < q; qi++) {
int u, d;
cin >> u >> d;
u--;
int l = -1, r = v[d].size(), m;
while (abs(l - r) > 1) {
m = (l + r) / 2;
if (v[d][m] < g.a[u]) {
l = m;
} else {
r = m;
}
}
int low = r;
l = -1, r = v[d].size();
while (abs(l - r) > 1) {
m = (l + r) / 2;
if (v[d][m] <= g.b[u]) {
l = m;
} else {
r = m;
}
}
int hi = l;
cout << hi - low + 1 << endl;
}
return 0;
} |
#define _USE_MATH_DEFIMES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
const int MOD = 1'000'000'007;
const int MOD2 = 998'244'353;
const int INF = 1'000'000'000; //1e9
const int NIL = -1;
const long long LINF = 1'000'000'000'000'000'000; // 1e18
const long double EPS = 1E-10;
template<class T, class S> inline bool chmax(T &a, const S &b){
if(a < b){
a = b; return true;
}
return false;
}
template<class T, class S> inline bool chmin(T &a, const S &b){
if(b < a){
a = b; return true;
}
return false;
}
int main(){
int N; long long K; std::cin >> N >> K;
std::vector<std::vector<long long>> dp(4, std::vector<long long>(3*N + 8));
dp[0][0] = 1;
for(int i{0}; i < 3; ++i){
for(int j{0}, j_len{i*N}; j <= j_len; ++j){
dp[i+1][j+1] += dp[i][j];
dp[i+1][j+N+1] -= dp[i][j];
}
for(int j{0}, j_len{(i+1) * N}; j < j_len; ++j){
dp[i+1][j+1] += dp[i+1][j];
}
}
int sm;
for(int i{3}, i_len{3*N}; i <= i_len; ++i){
if(K <= dp[3][i]){
sm = i; break;
}else{
K -= dp[3][i];
}
}
for(int i{1}; i <= N; ++i){
int x{sm - i};
int jmx{std::min(N, x-1)}, jmn{std::max(x-N, 1)};
if(jmx < jmn) continue;
if(K > jmx - jmn + 1){
K -= jmx - jmn + 1;
continue;
}
int j{int(jmn + K - 1)};
std::cout << i << " " << j << " " << (x - j) << std::endl;
return 0;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fr(i,j,k) for(int i=j;i<k;i++)
#define f(n) fr(i,0,n)
#define f1(n) fr(i,1,n+1)
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
const ll mod = 1e9 + 7;
const int maxn = 1e5+5;
void go() {
int n;
cin>>n;
string s = " ";
cin>>s[0]>>s[1]>>s[2]>>s[3];
if (n==2) {puts("1");
return;
}
if (s=="AAAA"||s=="AAAB"||s=="AABA"||s=="AABA"||s=="AABB"||s=="ABAB"||s=="ABBB"||s=="BBAB"||s=="BBBB") return puts("1"),void();
if (s=="ABAA"||s=="BABA"||s=="BABB"||s=="BBAA") {
int ans = 1;
for (int i=1; i<=n-3; ++i) ans = ans*2%mod;
printf("%d\n", ans);
return;
}
int dp[n+5] = {};
dp[1] = 0, dp[2] = 1;
for (int i=3; i<=n; ++i) dp[i] = (dp[i-1]+dp[i-2])%mod;
printf("%d\n", dp[n]);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int c = 0;
int t;
if (!c) {
t = 1;
}
else {
cin >> t;
}
while (t--) {
go();
}
} |
#include<bits/stdc++.h>
using ll = int_fast64_t;
#define REP(i,b,e) for(ll i=b; i<e; i++)
int main(){
ll n, k;
scanf("%ld %ld", &n, &k);
ll ans = 0;
REP(ab, 2, 3*n){
ll cd = ab-k;
if(cd<2) continue;
ll l1 = std::max(0l, ab-n), r1 = std::min(ab, 0+n);
ll l2 = std::max(0l, cd-n), r2 = std::min(cd, 0+n);
ans += std::max(0l, std::min(ab-1, r1-l1+1)) * std::max(0l, std::min(cd-1, r2-l2+1));
}
printf("%ld\n", ans);
return 0;
}
| #include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <ctype.h>
#include <math.h>
#include <stack>
#include <string>
#include <string.h>
#define rep(i,n) for(ll i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int,int>;
const long double PI = acos(-1.0);
const ll mod = 1e9 + 7;
const ll INF = 1e9;
ll gcd(ll a, ll b) {if(a%b==0)return a;else return gcd(b,a%b);}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}
int main() {
ll n,k;
cin >> n >> k;
if(k < 0) k = -k;
vector<ll> f(3*n);
for(ll i = 2; i <= 2*n; i++) {
f[i] = min(i-1, 2*n+1-i);
}
ll ans = 0;
for(ll i = 2; i <= 2*n; i++) {
if(i-k < 2) continue;
ans += f[i] * f[i-k];
}
cout << ans << endl;
} |
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define reps(i, s, n) for(int i = (s); i < (n); i++)
#define rrep(i, n) for(int i = (n) - 1; i >= 0; i--)
#define rreps(i, s, n) for(int i = (n) - 1; i >= (s); i--)
#define INF ~(1 << 31)
#define INFL ~(1LL << 63)
int main(){
int N;
cin >> N;
int minprice = INF;
rep(i, N){
int a, p, x;
cin >> a >> p >> x;
if(a < x){
minprice = min(minprice, p);
}
}
if(minprice == INF) minprice = -1;
cout << minprice << endl;
} | #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
#define ll long long
int main() {
int a, b, w, min, max, goal, dif;
bool stop = false;
cin >> a >> b >> w;
w *= 1000;
if (w < a) cout << "UNSATISFIABLE";
else if (a == b) {
if (w % a == 0) cout << w / a << ' ' << w / a << '\n';
else cout << "UNSATISFIABLE\n";
}
else {
dif = b - a;
goal = w;
min = goal /b;
goal %= b;
if (goal > 0) {
++min;
goal = b - goal;
}
goal = goal - min * dif;
if (goal > 0) {
stop = true;
cout << "UNSATISFIABLE\n";
}
if (!stop) {
goal = w;
max = goal / a;
goal %= a;
goal = goal - max * dif;
if (goal > 0) {
stop = true;
cout << "UNSATISFIABLE\n";
}
if (!stop) cout << min << ' ' << max << '\n';
}
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (int)n; i++)
using ll = long long;
const int MAX = 2e5;
int main(){
int n;
cin >> n;
int t[n];
int st = 0;
rep(i,n) {
cin >> t[i];
st += t[i];
}
vector<int> dp(MAX);
dp[0] = 1;
rep(i,n) {
vector<int> dpn(MAX);
rep(j,MAX) {
if(dp[j]) dpn[j]++;
if(j+t[i] < MAX and dp[j]) dpn[j+t[i]]++;
}
swap(dp,dpn);
}
int ans = MAX;
rep(i,MAX) {
if(dp[i]) ans = min(ans,max(i,st-i));
}
cout << ans << endl;
} | #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define N 105
#define M 100001
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0' || ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while(ch>='0' && ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
return x*f;
}
int n,a[N],sum;
bool f[2][200003];
int main()
{
n=read();
for(int i=1;i<=n;i++) a[i]=read(),sum+=a[i];
int x=1,y=0;
f[x][M+0]=1;
for(int i=1;i<=n;++i)
{
memset(f[y],0,sizeof(f[y]));
for(register int j=-100000;j<=100000;++j)
if(f[x][M+j]) f[y][M+j+a[i]]=f[y][M+j-a[i]]=1;
swap(x,y);
}
for(int i=0;i<=100000;++i)
if(f[x][M+i] || f[x][M-i]){printf("%d\n",(sum+i)/2);return 0;}
return 0;
} |
/**
* Dont raise your voice, improve your argument.
* --Desmond Tutu
*/
#include <bits/stdc++.h>
using namespace std;
const bool ready = [](){
ios_base::sync_with_stdio(false); cin.tie(0);
cout << fixed << setprecision(12);
return true;
}();
using ld=long double;
const ld PI = acos((ld)-1);
using ll= long long;
#define int ll
#define all(v) (v).begin(), (v).end()
#define fori(n) for(int i=0; i<int(n); i++)
#define cini(i) int i; cin>>i;
#define cins(s) string s; cin>>s;
#define cind(d) ld d; cin>>d;
#define cinai(a,n) vi a(n); fori(n) { cin>>a[i]; }
#define cinas(s,n) vs s(n); fori(n) { cin>>s[i]; }
#define cinad(a,n) vd a(n); fori(n) { cin>>a[i]; }
using pii= pair<int, int>;
using pdd= pair<ld, ld>;
using vd= vector<ld>;
using vb= vector<bool>;
using vi= vector<int>;
using vvi= vector<vi>;
using vs= vector<string>;
#define endl "\n"
using t3=tuple<int,int,int>;
/* dijkstra */
const int INF=1e18;
void solve() {
cini(n);
cini(m);
cini(x); x--;
cini(y); y--;
vector<vector<t3>> adj(n);
for(int i=0; i<m; i++) {
cini(a); a--;
cini(b); b--;
cini(t);
cini(k);
adj[a].emplace_back(b,t,k);
adj[b].emplace_back(a,t,k);
}
vi dp(n, INF);
dp[x]=0;
priority_queue<pii> q; /* <-time, vertex> */
q.emplace(0, x);
while(q.size()) {
auto [tt,v]=q.top();
q.pop();
if(dp[v]!=-tt)
continue;
for(auto [chl,t,k] : adj[v]) {
int wait=dp[v]%k;
if(wait==0)
wait=k;
wait=k-wait;
int next=dp[v]+wait+t;
if(dp[chl]>next) {
dp[chl]=next;
q.emplace(-dp[chl], chl);
}
}
}
if(dp[y]==INF)
cout<<-1<<endl;
else
cout<<dp[y]<<endl;
}
signed main() {
solve();
} | #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <cmath>
#include <iomanip>
#include <stack>
#include <queue>
#include <numeric>
#include <map>
#include <unordered_map>
#include <set>
#include <fstream>
#include <chrono>
#include <random>
#include <bitset>
//#include <atcoder/all>
#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 sz(x) ((int)(x).size())
#define pb push_back
using ll = long long;
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
#define n50 250000
int f(int r,int c){
return r*500+c;
}
int main(){
int R,C; cin >> R >> C;
vvi g(n50);
vvi co(n50);
rep(r,R) rep(c,C-1){
int in; cin >> in;
int ff=f(r,c);
g[ff].pb(ff+1);
g[ff+1].pb(ff);
co[ff].pb(in);
co[ff+1].pb(in);
}
rep(r,R-1) rep(c,C){
int in; cin >> in;
int ff=f(r,c);
g[ff].pb(ff+500);
co[ff].pb(in);
}
rep(c,C){
rep(r,R){
rep(s,r){
int ff=f(r,c);
g[ff].pb(f(s,c));
co[ff].pb(r-s+1);
}
}
}
vector<int> D(n50, 1e9);
vector<bool> used(n50, false);
priority_queue<pair<int,int>,
vector<pair<int,int>>,
greater<pair<int,int>>> q;
q.push({0,0});
while(!q.empty()){
int d = q.top().first; int to = q.top().second; q.pop();
if(used[to]) continue;
used[to] = true; D[to] = d;
rep(i,sz(g[to])){
int nxt=g[to][i];
int nd = D[to] + co[to][i];
if(D[nxt] <= nd) continue;
D[nxt] = nd;
q.push({nd, nxt});
}
}
int ans = D[f(R-1,C-1)];
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define loop(i, a) for (int i = 0; i < (a); ++i)
#define cont(i, a) for (int i = 1; i <= (a); ++i)
#define circ(i, a, b) for (int i = (a); i <= (b); ++i)
#define range(i, a, b, c) for (int i = (a); (c) > 0 ? i <= (b) : i >= (b); i += (c))
#define parse(it, x) for (auto &it : (x))
#define pub push_back
#define pob pop_back
#define emb emplace_back
#define mak make_pair
#define mkt make_tuple
typedef long long ll;
typedef long double lf;
const int Inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
int n;
int a[200005], b[200005], p[200005];
int dy[200005];
map<int, int> lss;
vector<int> pos[200005];
struct Fenwick {
ll dt[1 << 18];
void inline add(int a, ll x) { while (a <= n) dt[a] += x, a += a & -a; }
ll inline sum(int a) { ll res = 0; while (a) res += dt[a], a -= a & -a; return res; }
} bit;
int main() {
scanf("%d", &n);
cont(i, n) scanf("%d", a + i), a[i] += i, lss[a[i]];
cont(i, n) scanf("%d", b + i), b[i] += i;
int lsc = 0;
parse(it, lss) it.second = ++lsc;
cont(i, n) {
a[i] = lss[a[i]];
if (!lss.count(b[i])) return puts("-1"), 0;
b[i] = lss[b[i]];
pos[b[i]].pub(i);
}
cont(i, lsc) reverse(all(pos[i]));
cont(i, n) {
if (!SZ(pos[a[i]])) return puts("-1"), 0;
p[i] = pos[a[i]].back();
pos[a[i]].pob();
}
ll ans = 0;
range(i, n, 1, -1) {
ans += bit.sum(p[i]);
bit.add(p[i], 1);
}
printf("%lld\n", ans);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
using vec_int = vector<int>;
using P = pair<int,int>;
// using T = tuple<int,int,int>;
using ll = long long;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
int charToInt(char c){
char zero_num = '0';
return (int)c - (int)zero_num;
}
bool isPrime(int N){
for(int i=2;i*i<=N;i++){
if(N%i==0){
return false;
}
}
return true;
}
signed main(){
int N; cin>>N;
vec_int a(N-1), b(N-1); rep(i,N-1)cin>>a.at(i)>>b.at(i);
int Q; cin>>Q;
vec_int t(Q), e(Q), x(Q);
rep(i,Q)cin>>t.at(i)>>e.at(i)>>x.at(i);
vector<vec_int> G(N+1);
rep(i,N-1){
G.at(a.at(i)).push_back(b.at(i));
G.at(b.at(i)).push_back(a.at(i));
}
vector<int> node_depth(N+1,-1);
queue<P> que;
que.emplace(1,0);
while(!que.empty()){
int node, depth; tie(node, depth) = que.front(); que.pop();
node_depth.at(node) = depth;
for(auto next_node: G.at(node)){
if(node_depth.at(next_node)>=0)continue;
que.emplace(next_node, depth+1);
}
}
vec_int score(N+1, 0);
rep(q, Q){
int from_node = a.at(e.at(q)-1);
int to_node = b.at(e.at(q)-1);
if(t.at(q)==2){
int temp = from_node;
from_node = to_node;
to_node = temp;
}
if(node_depth.at(to_node)>node_depth.at(from_node)){
score.at(1) += x.at(q);
score.at(to_node) -= x.at(q);
}else{
score.at(from_node) += x.at(q);
}
}
vec_int ans(N+1, -1);
queue<P> que2;
que2.emplace(1, 0);
while(!que2.empty()){
int node, parent_score; tie(node, parent_score)= que2.front(); que2.pop();
// cout<<node<<" parent_score:"<<parent_score<<endl;
int temp = parent_score+score.at(node);
ans.at(node) = temp;
for(auto next_node: G.at(node)){
if(ans.at(next_node)>=0)continue;
que2.emplace(next_node, temp);
}
}
rep(i,N){
cout<<ans.at(i+1)<<endl;
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin>>s;
reverse(s.begin(),s.end());
for(int i=0; i<s.size(); i++){
if(s.at(i) == '6')s.at(i)='9';
else if(s.at(i) == '9')s.at(i)='6';
}
cout<<s<<endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define fastio \
ios_base::sync_with_stdio(false); \
cin.tie(NULL)
#define int long long
#define ll long long
#define ld long double
#define all(v) v.begin(), v.end()
#define endl "\n"
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll MAX = 2e5 + 5;
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll add(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
inline ll mul(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
inline ll sub(ll a, ll b) { return ((a % mod) - (b % mod) + mod) % mod; }
inline ll power(ll x, ll n) {
if (!n) {
return 1;
}
return (n & 1 ? mul(x, power(mul(x, x), n / 2)) : power(mul(x, x), n / 2));
}
void solve() {
string s;
cin >> s;
reverse(all(s));
for (auto ch : s) {
cout << (ch == '6' ? '9' : ch == '9' ? '6' : ch);
}
}
signed main() {
fastio;
int t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
} |
///****In the name of ALLAH, most Gracious, most Compassionate****//
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
#define ALL(a) a.begin(), a.end()
#define FastIO ios::sync_with_stdio(false); cin.tie(0);cout.tie(0)
#define IN freopen("input.txt","r+",stdin)
#define OUT freopen("output.txt","w+",stdout)
#define DBG(a) cerr<< "line "<<__LINE__ <<" : "<< #a <<" --> "<<(a)<<endl
#define NL cout<<endl
template < class T1,class T2>
ostream &operator <<(ostream &os,const pair < T1,T2 > &p)
{
os<<"{"<<p.first<<","<<p.second<<"}";
return os;
}
const int N=3e5+5;
const int oo=1e9+7;
vector<pii> g[N];
int ans[N];
int n;
void dfs(int u)
{
for(pii vc: g[u])
{
if(ans[vc.first]==-1)
{
if(vc.second == ans[u])
ans[vc.first] = (vc.second %n)+1;
else
ans[vc.first] = vc.second;
dfs(vc.first);
}
}
}
int32_t main()
{
FastIO;
int m;
cin>>n>>m;
while(m--)
{
int u,v,c;
cin>>u>>v>>c;
g[u].push_back({v,c});
g[v].push_back({u,c});
}
memset(ans , -1 , sizeof ans) ;
ans[1] =1;
dfs(1);
for(int i=1;i<=n;i++)
cout<<ans[i]<<"\n";
}
| #include <bits/stdc++.h>
using namespace std;
#define FOR(i,s,e) for(int i=(s);(i)<(int)(e);++(i))
#define REP(i,e) FOR(i,0,e)
#define each(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define all(o) (o).begin(), (o).end()
typedef long long ll;
const int N = 200000;
int n;
int a[N];
class union_find_tree {
private:
vector<int> v;
vector<int> connected_size;
map<int,int> parent;
map<int,int> rank;
public:
union_find_tree(int m) {
v.resize(m);
connected_size.resize(m);
for (int i=0; i<m; i++) v[i] = i;
for (int i=0; i<(int)v.size(); i++) {
parent[v[i]] = v[i];
rank[v[i]] = 1;
connected_size[v[i]] = 1;
}
}
union_find_tree(vector<int> _v) {
v = _v;
for (int i=0; i<(int)v.size(); i++) {
parent[v[i]] = v[i];
rank[v[i]] = 1;
connected_size[v[i]] = 1;
}
}
~union_find_tree() {
v.clear();
parent.clear();
rank.clear();
connected_size.clear();
}
int root (int x) {
return parent[x] == x ? x : parent[x] = root(parent[x]);
}
bool is_same(int x, int y) {
return root(x) == root(y);
}
void unite(int x, int y) {
x = root(x);
y = root(y);
if (x == y) return;
if (rank[x] < rank[y]) {
parent[x] = y;
connected_size[y] += connected_size[x];
} else {
parent[y] = x;
connected_size[x] += connected_size[y];
if (rank[x] == rank[y]) rank[x]++;
}
return;
}
int get_connected_size(int x) {
return connected_size[x];
}
};
int main() {
fscanf(stdin, "%d ", &n);
REP(i,n) {
fscanf(stdin, "%d ", a+i);
a[i]--;
}
union_find_tree u(N);
REP(i,n/2) u.unite(a[i],a[n-1-i]);
int res = 0;
REP(i,N) {
if (u.root(i) != i) continue;
res += u.get_connected_size(i) - 1;
}
fprintf(stdout, "%d\n", res);
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
struct BIT{
vector<LL> node;
int size;
BIT(int n){
size=n;
node.resize(size,0);
}
void add(int a,int x){
for(int i=a;i<size;i|=i+1) node[i]+=x;
}
LL sum(int a){
LL res=0;
for(int i=a-1;i>=0;i=(i&(i+1))-1){
res+=node[i];
}
return res;
}
};
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int h,w,m;
cin>>h>>w>>m;
vector<int> x(m),y(m);
for(int i=0;i<m;i++){
cin>>x[i]>>y[i];
x[i]--,y[i]--;
}
int clsh=h,clsw=w;
for(int i=0;i<m;i++){
if(x[i]==0) clsw=min(clsw,y[i]);
if(y[i]==0) clsh=min(clsh,x[i]);
}
vector<int> rih(clsh,w);
for(int i=0;i<m;i++){
if(x[i]<clsh) rih[x[i]]=min(rih[x[i]],y[i]);
}
vector<int> riw(clsw,h);
for(int i=0;i<m;i++){
if(y[i]<clsw) riw[y[i]]=min(riw[y[i]],x[i]);
}
LL res=0;
for(int i=0;i<clsh;i++) res+=rih[i];
for(int i=0;i<clsw;i++) res+=riw[i];
vector<vector<int>> dels(w+1);
for(int i=0;i<clsh;i++) dels[rih[i]].push_back(i);
BIT bt(h);
for(int i=0;i<clsh;i++) bt.add(i,1);
for(int i=0;i<clsw;i++){
for(int d:dels[i]) bt.add(d,-1);
res-=bt.sum(riw[i]);
}
cout<<res;
return 0;
} | #include <bits/stdc++.h>
#define ll long long
using namespace std;
#define MAX 1000000007
int nCrModpDP(int n, int r, int p)
{
// The array C is going to store last row of
// pascal triangle at the end. And last entry
// of last row is nCr
int C[r+1];
memset(C, 0, sizeof(C));
C[0] = 1; // Top row of Pascal Triangle
// One by constructs remaining rows of Pascal
// Triangle from top to bottom
for (int i = 1; i <= n; i++)
{
// Fill entries of current row using previous
// row values
for (int j = min(i, r); j > 0; j--)
// nCj = (n-1)Cj + (n-1)C(j-1);
C[j] = (C[j] + C[j-1])%p;
}
return C[r];
}
// Lucas Theorem based function that returns nCr % p
// This function works like decimal to binary conversion
// recursive function. First we compute last digits of
// n and r in base p, then recur for remaining digits
int nCrModpLucas(int n, int r, int p)
{
// Base case
if (r==0)
return 1;
// Compute last digits of n and r in base p
int ni = n%p, ri = r%p;
// Compute result for last digits computed above, and
// for remaining digits. Multiply the two results and
// compute the result of multiplication in modulo p.
return (nCrModpLucas(n/p, r/p, p) * // Last digits of n and r
nCrModpDP(ni, ri, p)) % p; // Remaining digits
}
int main(){
ios::sync_with_stdio(0), cin.tie(0);
ll n,s,d;
cin>>n>>s>>d;
ll x,y;
ll test=0;
for(int i=0;i<n;i++)
{
cin>>x>>y;
if((x<s)and(y>d))
test=1;
}
if(test==1)
cout<<"Yes";
else
cout<<"No";
return(0);
}
|
#define wiwihorz
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma loop-opt(on)
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define rrep(i, a, b) for(int i = b; i >= a; i--)
#define all(x) x.begin(), x.end()
#define ceil(a, b) ((a + b - 1) / (b))
#define MAXN 1000005
#define INF 1000000000000000000
#define MOD 1000000007
#define eps (1e-9)
#define int long long int
#define lld long double
#define pii pair<int, int>
#define random mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count())
using namespace std;
#ifdef wiwihorz
#define print(a...) kout("[" + string(#a) + "] = ", a)
void vprint(auto L, auto R) {
while(L < R) cerr << *L << " \n"[next(L) == R],++L;
}
void kout() {cerr << endl;}
template<class T1, class ... T2> void kout(T1 a, T2 ... e) {
cerr << a << " ", kout(e...);
}
#else
#define print(...) 0
#define vprint(...) 0
#endif
int n;
vector<int> a;
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> n;
a.assign(n + 1, 0);
rep(i, 1, n) cin >> a[i];
int ans = -1, cnt = 0;
rep(i, 2, 1000) {
int cur = 0;
rep(j, 1, n)
cur += bool(a[j] % i == 0);
if(cur > cnt) ans = i, cnt = cur;
}
cout << ans << "\n";
return 0;
} | #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
int n;
cin >> n;
vector<int> v(n);
for(auto& a: v){
cin >> a;
}
int ret = 0;
int maxcount = 0;
for(int i = 2; i <= 1000; ++i){
int count = 0;
for(auto& a : v){
if(a % i == 0){
++count;
}
}
if(maxcount < count){
maxcount = count;
ret = i;
}
}
cout << ret << endl;
return 0;
} |
//#define localsystem 1
#ifdef localsystem
#include"mytimer.h"
#define mytest 1
#ifdef mytest
#include<fstream>
#endif
#endif
#define min(x, y) ((x < y) ? x : y)
#define max(x, y) ((x > y) ? x : y)
#include<iostream>
#include<string>
using namespace std;
int main() {
#ifdef mytest
ifstream mycin("Sample.txt");
ofstream mycout("Test resutl.txt");
if (!mycin.is_open() || !mycout.is_open()) {
cout << "File can not be opened";
return 0;
}
cin.rdbuf(mycin.rdbuf());
cout.rdbuf(mycout.rdbuf());
#endif
#ifdef localsystem
mytimer time_taken{};
#endif
int t{ 1 };
//cin >> t;
while (t--) {
string s;
cin >> s;
int chc[26] = { 0 };
char pc = '-';
long long int ans{};
for (int i = s.size() - 1; i >= 0; --i) {
if (pc == s[i]) {
ans += ((s.size() - i - 2) - chc[s[i] - 'a'] + 1);
for (int k = 0; k < 26; ++k) chc[k] = 0;
chc[s[i] - 'a'] = s.size() - i;
}
else chc[s[i] - 'a']++;
pc = s[i];
}
cout << ans << "\n";
}
#ifdef localsystem
time_taken.timetaken();
#endif
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main()
{
string s;
cin >> s;
while (s.back() == '0')
{
s.pop_back();
}
string t = s;
reverse(t.begin(), t.end());
if (s == t)
cout << "Yes\n";
else
cout << "No\n";
}
|
#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 ll mod = 1e9+7;
const int maxn = 2e3 + 10;
int n, m;
int g[maxn][maxn];
ll dp[maxn][maxn];
void add(ll &x, ll y) {
x %= mod;
y %= mod;
x += y;
x %= mod;
}
ll L[maxn];
ll U[maxn];
ll D[maxn+maxn];
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>>n>>m;
for (int i=1; i<=n; i++) {
string s;
cin>>s;
for (int j=1; j<=m; j++) {
if (s[j-1]=='#') g[i][j]=1;
}
}
dp[1][1]=1;
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++) {
if (g[i][j] == 0) {
add(dp[i][j], U[j]);
add(dp[i][j], L[i]);
add(dp[i][j], D[i-j+maxn]);
add(U[j], dp[i][j]);
add(L[i], dp[i][j]);
add(D[i-j+maxn], dp[i][j]);
} else {
U[j] = 0;
L[i] = 0;
D[i-j+maxn] = 0;
}
}
}
ll res = dp[n][m];
res %= mod;
res += mod;
res %= mod;
out(res);
return 0;
}
| #pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define REP(i, n) FOR(i,0,n)
constexpr unsigned MOD = 1000000007;
inline void add(unsigned &x, const unsigned &y) {
if (x + y >= MOD * 2) x = x + y - MOD * 2;
else if (x + y >= MOD) x = x + y - MOD;
else x += y;
}
int main()
{
cin.tie(nullptr), ios::sync_with_stdio(false);
int H, W;
cin >> H >> W;
vector<unsigned> dp(W);
vector<unsigned> dpu(W);
vector<unsigned> dpn(W);
dp[0] = 1;
while (H--) {
string S;
cin >> S;
vector<unsigned> nxt(W), dpr(W), nxtu(W), nxtn(W);
REP(j, W) {
if (S[j] == '#') {
continue;
}
if (j + 1 < W) {
add(dp[j + 1], dp[j] + dpr[j]);
add(dpr[j + 1], dp[j] + dpr[j]);
add(nxt[j + 1], dp[j] + dpn[j]);
add(nxtn[j + 1], dp[j] + dpn[j]);
}
add(nxt[j], dp[j] + dpu[j]);
add(nxtu[j], dp[j] + dpu[j]);
}
if (H == 0) {
cout << dp.back() << '\n';
return 0;
}
dp = nxt, dpu = nxtu, dpn = nxtn;
}
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#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 REP(i,a,b) for(int i = (a); i < (b); i++)
#define MOD 1000000007
int main(void) {
ll k;
cin >> k;
string s,t;
cin >> s >> t;
ll taka=0,aoki=0;
vector<int> cnt_taka(10,0),cnt_aoki(10,0);
rep(i,4) cnt_taka[s[i]-'0']++;
rep(i,4) cnt_aoki[t[i]-'0']++;
rep(i,10) taka+=i*pow(10,cnt_taka[i]);
rep(i,10) aoki+=i*pow(10,cnt_aoki[i]);
//rep(i,10) printf("cnt_taka[%d]==%d\n",i,cnt_taka[i]);
//rep(i,10) printf("cnt_aoki[%d]==%d\n",i,cnt_aoki[i]);
//# patern
ll tmp_t=taka,tmp_a=aoki;
ll ans=0;
rep2(i,1,10) {
rep2(j,1,10) {
tmp_t=taka,tmp_a=aoki;
tmp_t+=i*pow(10,cnt_taka[i])*9;
tmp_a+=j*pow(10,cnt_aoki[j])*9;
//taka win
if(tmp_a<tmp_t) {
ll a,b;
a=k-cnt_taka[i]-cnt_aoki[i];
b=k-cnt_taka[j]-cnt_aoki[j];
if(i==j) b--;
if(a>0&&b>0) ans+=a*b;
//printf("ans=%d i=%d j=%d\n",ans,i,j);
}
}
}
//cout << ans << endl;
double res;
res=(double)ans/((9*k-4*2)*(9*k-4*2-1));
//cout << res << endl;
printf("%.8lf\n",res);
} | #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--)
#define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++)
#define rrep2(ri,x,n) for(int ri = (int)(n-1); ri >= (int)(x); ri--)
#define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;
const int INF = 1001001001;
const int BASE = 10000;
int main() {
int n;
cin >> n;
vector<tuple<int, int, int>> input(n);
map<tuple<int, int>, int> mp;
rep(i, n) {
int x, y, c;
cin >> x >> y >> c;
input.at(i) = {x, y, c};
mp[{x, y}] = i;
}
// FILE *fp;
// fp = fopen("out.txt", "w");
vector<tuple<int, int, int, int>> ans(n);
set<tuple<int, int, int, int>> se;
int co = 0;
rrep(bh, BASE)rrep(bw, BASE) {
if(mp.find({bw, bh}) == mp.end()) continue;
int idx = mp[{bw, bh}];
auto [sx, sy, c] = input.at(idx);
int ex = BASE, ey = BASE;
set<pair<int, int>> pse;
for(auto [px, py, pw, ph] : se) {
int pex = px + pw, pey = py + ph;
if(sx < pex && sy < py){
if(px <= sx) ey = min(ey, py);
else pse.insert({px, py});
}
if(sy < pey && sx < px){
if(py <= sy) ex = min(ex, px);
else pse.insert({px, py});
}
}
int mnabs = INF;
int rh = 1, rw = 1;
if(idx % 2 == 0) {
for(int h = 1; sy + h <= ey; h++) {
for(int w = 1; sx + w <= ex; w++) {
if(pse.find({sx+w, sy+h}) != pse.end()) {
ex = sx + w;
}
int s = h*w;
if(abs(c - s) < mnabs) {
mnabs = abs(c - s);
rh = h, rw = w;
}
}
}
} else {
for(int w = 1; sx + w <= ex; w++) {
for(int h = 1; sy + h <= ey; h++) {
if(pse.find({sx+w, sy+h}) != pse.end()) {
ey = sy + h;
}
int s = h*w;
if(abs(c - s) < mnabs) {
mnabs = abs(c - s);
rh = h, rw = w;
}
}
}
}
se.insert({sx, sy, rw, rh});
ans.at(idx) = {sx, sy, sx+rw, sy+rh};
}
for(auto [a, b, c, d] : ans) {
// fprintf(fp, "%d %d %d %d\n", a, b, c, d);
cout << a << " " << b << " " << c << " " << d << endl;
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vi=vector<int>;
int main() {
int N; cin>>N;
ll M = 10000000000;
string T; cin>>T;
if(T=="1"){
cout<<2*M<<endl;
return 0;
}else if(T=="11"){
cout<<M<<endl;
return 0;
}
int cnt = 0;
int cur = 0;
if(T[0]=='0'){
cnt++;
cur = 1;
}else if(N>=2 && T.substr(0,2)=="10"){
cnt++;
cur = 2;
}else if(N>=3 && T.substr(0,3)=="110"){
;
}else{
cout<<0<<endl;
return 0;
}
for (; cur+2 < N; cur+=3){
if(T.substr(cur,3)=="110")cnt++;
else{
cout<<0<<endl;
return 0;
}
}
if(cur<N){
if(T.substr(cur)=="1" || T.substr(cur)=="11")cnt++;
else{
cout<<0<<endl;
return 0;
}
}
ll ans = M - cnt + 1;
cout<<ans<<endl;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
string T;
cin >> T;
string S;
while(S.length() < T.length()) {
S += "110";
}
ll ans = 0;
for(size_t i=0; i+T.size() <= S.size(); i++) {
bool ok = true;
for(size_t j=0; j < T.size(); j++) {
if(S[i+j] != T[j]) ok = false;
// cout << i+j << ' ' << j << ' ' << S[i+j] << ' ' << T[j] << '\n';
}
if(ok) ans++;
}
if(ans == 0) {
S += "110";
for(size_t i=0; i+T.size() <= S.size(); i++) {
bool ok = true;
for(size_t j=0; j < T.size(); j++) {
if(S[i+j] != T[j]) ok = false;
// cout << i+j << ' ' << j << ' ' << S[i+j] << ' ' << T[j] << '\n';
}
if(ok) ans++;
}
}
// cout << ans << '\n';
ans *= 10000000000LL - S.size() / 3 + 1;
// ans *= 5 - S.size() / 3 + 1;
cout << ans << endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
ll x; cin >> x;
cout << 100 - (x%100) << endl;
return 0;
}
| #include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
using namespace std;
using ll = long long;
const int MOD = 1000000007;
const int INF = 2147483647;
int main() {
FAST;
int i, s = 0, l = 0, ans = 0;
ll m;
cin >> m;
vector<int> d(3,0);
while (m) {
int t = (m % 10) % 3;
++l;
s += t;
++d[t];
m /= 10;
}
ans = s % 3;
if (ans == 1) {
if ((l > 1) && (d[1] >= 1)) {}
else if ((l > 2) && (d[2] >= 2)) { ans = 2; }
else ans = -1;
} else if (ans == 2) {
if ((l > 1) && (d[2] >= 1)) { ans = 1; }
else if ((l > 2) && (d[1] >= 2)) {}
else ans = -1;
}
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false); cin.tie(0);
ll a, b;
cin >> a >> b;
vector <int> primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71};
vector <ll> dp(1 << 20, 0);
dp[0] = 1;
for(ll x = a; x <= b; x++) {
int curr_mask = 0;
for(int i = 0; i < 20; i++) {
if(x % primes[i] == 0) curr_mask |= (1 << i);
}
for(int mask = 0; mask < (1 << 20); mask++) {
if(mask & curr_mask) continue;
dp[mask ^ curr_mask] += dp[mask];
}
}
cout << accumulate(dp.begin(), dp.end(), 0LL);
} | #include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {
return '"' + s + '"';
}
string to_string(const char* s) {
return to_string((string) s);
}
string to_string(bool b) {
return (b ? "true" : "false");
}
string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template <size_t N>
string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++) {
res += static_cast<char>('0' + v[i]);
}
return res;
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
#define fi first
#define se second
#define pb push_back
#define mod(n,k) ( ( ((n) % (k)) + (k) ) % (k))
#define forn(i,a,b) for(int i = a; i < b; i++)
#define forr(i,a,b) for(int i = a; i >= b; i--)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
const ll mod = 1e9+7;
int sum(int a, int b){return (a+b) % mod;}
int sub(int a, int b){return (a + mod - b) % mod;}
int mul(int a, int b){return (1ll * a * b) % mod;}
int power(int a,int b){
int res = 1;
while(b){
if(b&1)res = mul(res,a);
b >>= 1;
a = mul(a,a);
}
return res;
}
const int maxn = 73;
const int maxp = 21;
vector<int> primos;
vector<pair<ll,int>> C;
ll A,B,dp[2][(1<<maxp)];
//ll dp(int pos,int mask){
//if(pos >= (int)C.size())return 1;
//ll &res = memo[pos][mask];
//if(res != -1)return res;
//res = dp(pos+1,mask);
//if(!(mask&C[pos].se)){
//res += dp(pos+1,mask|C[pos].se);
//}
//return res;
//}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
forn(i,2,maxn){
bool primo = true;
forn(j,2,i)if(i%j == 0)primo = false;
if(primo)primos.pb(i);
}
cin >> A >> B;
for(ll i = A; i <= B; i++){
int mask = 0;
forn(j,0,(int)primos.size()){
ll p = primos[j];
if(i%p == 0)mask |= (1<<j);
}
C.pb({i,mask});
}
//debug(primos);
//debug(C);
forn(mask,0,(1<<maxp))dp[1][mask] = 1;
forr(pos,(int)C.size()-1,0){
forn(mask,0,(1<<maxp)){
dp[0][mask] += dp[1][mask];
if(!(mask&C[pos].se)){
dp[0][mask] += dp[1][mask|C[pos].se];
}
}
forn(mask,0,(1<<maxp)){
dp[1][mask] = dp[0][mask];
dp[0][mask] = 0;
}
}
ll res = dp[1][0];
//debug(dp[1][0],dp[1][C[0].se]);
//forn(mask,0,(1<<maxp))res += dp[1][mask];
cout << res << '\n';
return 0;
}
/*
__builtin_mul_overflow(x,y,&x)
-fsplit-stack
*/
|
#include <bits/stdc++.h>
#define rep(i, n) for (auto i = 0; i < n; ++i)
#define ALL(a) a.begin(), a.end()
using namespace std;
using ll = long long int;
const ll MOD_NUM = 998244353;
// 二分累乗法
ll pow_mod(ll n, ll p, ll m) {
const int N = 64;
vector<ll> memory(N);
memory[0] = n % m;
rep(i, N) memory[i + 1] = memory[i] * memory[i] % m;
ll ans = 1;
rep(i, N) {
ll k = p >> i;
if (k == 0)
break;
else if (k & 1)
ans = (ans * memory[i]) % m;
}
return ans;
}
int main() {
// Input
int N;
cin >> N;
map<int, int> f;
rep(i, N) cin >> f[i + 1];
// Process
vector<bool> watched(N + 1, false);
int watched_num = 0;
int group_num = 0;
int start = 1;
while (watched_num < N) {
while (watched[start]) start++;
vector<int> watch_nums;
int current;
for (current = start; !watched[current]; current = f[current]) {
watch_nums.emplace_back(current);
watched[current] = true;
watched_num++;
}
bool current_watch = false;
for (int n : watch_nums) {
if (n == current) current_watch = true;
}
if (current_watch) group_num++;
}
ll ans = pow_mod(2, group_num, MOD_NUM);
ans = (ans + MOD_NUM - 1) % MOD_NUM;
// Output
cout << ans << endl;
return 0;
}
| #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
const int M = 200;
int N;
cin >> N;
vector<int> A(N);
for (int& a : A) { cin >> a; }
vector<vector<int>> rest(M);
vector<int> ans1, ans2;
bool flag = false;
for (int i = 0; i < N; ++i)
{
int r = A[i] % M;
if (rest[r].empty()) { rest[r].push_back(i); }
else
{
ans1 = rest[r];
ans2.push_back(i);
flag = true;
break;
}
for (int m = 0; m < M; ++m)
{
if (rest[m].empty()) { continue; }
auto it = find(rest[m].begin(), rest[m].end(), i);
if (it != rest[m].end()) { continue; }
vector<int> temp(rest[m]);
temp.push_back(i);
int j = (m + r) % M;
if (rest[j].empty()) { rest[j] = temp; }
else
{
ans1 = rest[j];
ans2 = temp;
flag = true;
break;
}
}
if (flag) { break; }
}
if (flag)
{
cout << "Yes" << endl;
cout << ans1.size();
for (int i : ans1) { cout << " " << (i + 1); }
cout << endl;
cout << ans2.size();
for (int i : ans2) { cout << " " << (i + 1); }
cout << endl;
}
else
{
cout << "No" << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int64_t MOD = 1e9 + 7;
const int64_t MOD2 = 998244353;
const int INF = 2e9;
const long long inf = 1LL<<62;
template<int MOD> struct ModInt {
static const int Mod = MOD; unsigned x; ModInt() : x(0) { }
ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
int get() const { return (int)x; }
ModInt &operator+=(ModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; }
ModInt &operator-=(ModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }
ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
ModInt inverse() const { long long a = x, b = MOD, u = 1, v = 0;
while (b) { long long t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); }
return ModInt(u); }
bool operator==(ModInt that) const { return x == that.x; }
bool operator!=(ModInt that) const { return x != that.x; }
ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : Mod - x; return t; }
};
template<int MOD> ostream& operator<<(ostream& st, const ModInt<MOD> a) { st << a.get(); return st; };
template<int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) {
ModInt<MOD> r = 1; while (k) { if (k & 1) r *= a; a *= a; k >>= 1; } return r; }
typedef ModInt<998244353> mint;
int main() {
int n;
cin >> n;
vector<int> a(n);
mint ans = 0;
for (int i=0; i<n; i++) {
cin >> a[i];
ans += mint(a[i])*mint(a[i]);
}
sort(a.begin(), a.end());
vector<mint> acc(n+1, 0);
for (int i=0; i<n; i++) {
acc[i+1] = mint(2)*acc[i] + mint(a[i]);
}
for (int i=1; i<n; i++) {
ans += mint(a[i])*acc[i];
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key
typedef long long ll;
typedef pair<ll,ll> ii;
typedef vector<int> vi;
typedef long double ld;
typedef pair<ld,ld> state;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
const int MOD = 998244353;
int add(int a, int b)
{
a+=b;
while(a>=MOD) a-=MOD;
return a;
}
void radd(int &a, int b)
{
a=add(a,b);
}
int mult(int a, int b)
{
return (a*1LL*b)%MOD;
}
void rmult(int &a, int b)
{
a=mult(a,b);
}
int a[222222];
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
int n; cin>>n;
for(int i=0;i<n;i++) cin>>a[i];
sort(a,a+n);
int cur=a[0];
int ans=0;
for(int i=1;i<n;i++)
{
radd(ans,mult(a[i],cur));
rmult(cur,2);
radd(cur,a[i]);
}
for(int i=0;i<n;i++)
{
radd(ans,mult(a[i],a[i]));
}
cout<<ans<<'\n';
}
|
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define ld long double
#define TIME clock() * 1.0 / CLOCKS_PER_SEC
#define sz(x) (int)(x.size())
const int modn = 998244353;
inline int add(int a, const int& b) {
// ensure both arguments are in the range [0, modn)
a += b;
if(a >= modn) {
a -= modn;
}
return a;
}
inline void adds(int& a, const int& b) {
a += b;
if(a >= modn) {
a -= modn;
}
}
inline int mul(const int& a, const int& b) {
return ((long long) a * b) % modn;
}
inline void muls(int& a, const int& b) {
a = ((long long) a * b) % modn;
}
template<typename... Args>
inline int add(int a, Args... args) {
return add(a, add(args...));
}
template<typename... Args>
inline int mul(int a, Args... args) {
return mul(a, mul(args...));
}
inline int addm(const int& MOD, int a, const int& b) {
a += b;
if(a >= MOD) {
a -= MOD;
}
return a;
}
inline int mulm(const int& MOD, const int& a, const int& b) {
return ((long long) a * b) % MOD;
}
template<typename... Args>
inline int addm(const int& MOD, int a, Args... args) {
return addm(MOD, a, addm(MOD, args...));
}
template<typename... Args>
inline int mulm(const int& MOD, int a, Args... args) {
return mulm(MOD, a, mulm(MOD, args...));
}
int po(int a, int b) {
int res = 1;
while(b > 0) {
if(b & 1) {
muls(res, a);
}
muls(a, a);
b >>= 1;
}
return res;
}
int pom(const int& MOD, int a, int b) {
int res = 1;
while(b > 0) {
if(b & 1) {
res = mulm(MOD, res, a);
}
a = mulm(MOD, a, a);
b >>= 1;
}
return res;
}
inline int inv(int a) {
return po(a, modn - 2);
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// code here
int n, m, k;
cin >> n >> m >> k;
if(n == 1) {
cout << po(k, m) << '\n';
return 0;
}
if(m == 1) {
cout << po(k, n) << '\n';
return 0;
}
int ans = 0;
for(int x = 1; x <= k; x++) {
int cur = mul(po(k - x + 1, m), add(po(x, n), modn - po(x - 1, n)));
adds(ans, cur);
}
cout << ans << '\n';
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main(){
long long i,j,k,l,m,n,a,b,c;
cin>>a>>b>>c;
if (c&1) {
if (a>b) {
cout<<'>';
}
else if (a<b) {
cout<<'<';
}
else {
cout<<'=';
}
}
else {
a=abs(a);
b=abs(b);
if (a>b) {
cout<<'>';
}
else if (a<b) {
cout<<'<';
}
else {
cout<<'=';
}
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define loop(i,n) for(ll i=0;i<n;i++)
#define loopn(i,j,n) for(ll i=j;i<n;i++)
#define m_arr(arr, n) vector<ll>arr(n)
#define m_arre(arr) vector<ll>arr
#define m_set(sat) set<ll>sat
#define ppn(name) pair<ll,ll>name
#define pp pair<ll,ll>
#define sort_all(arr) sort(arr.begin(), arr.end())
#define pi 3.141592653589793238
//#define mod 1000000007
ll gcd(ll a, ll b){if(a==0)return b;if(b==0)return a;return gcd(b%a, a);}
ll power(ll a, ll b, ll mod)
{
ll res=1;
while(b>0)
{
if(b%2!=0)
res = (res*1ll*a)%(mod*1ll*mod);
b/=2;
a=(a*1ll*a)%(mod*1ll*mod);
}
return res;
}
void solve()
{
ll x, y, a, b;
cin>>x>>y>>a>>b;
if(x>a)
{
swap(x, a);
swap(y, b);
}
double ans = (a*y + b*x)/double(y+b);
cout<<fixed<<setprecision(9)<<ans<<endl;
return;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//int t;cin>>t;while(1)
solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main(){
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
long double X, Y, Z;
cin >> X >> Y >> Z;
long double iti = Y / X;
for(int i = 1; i <= 10000000; i++){
if(Z*iti <= (long double)(i)){
cout << i-1 << '\n';
exit(0);
}
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
//constexpr ll MOD = 1e9 + 7;
constexpr ll MOD = 998244353;
//constexpr ll MOD = ;
ll mod(ll A, ll M) {return (A % M + M) % M;}
constexpr ll INF = 1LL << 60;
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;}
ll divceil(ll A, ll B) {return (A + (B - 1)) / B;}
#define FINALANS(A) do {cout << (A) << '\n'; exit(0);} while (false)
int main()
{
ll N;
cin >> N;
vector<ll> A(N);
for (ll i = 0; i < N; i++)
{
cin >> A.at(i);
}
vector<ll> S(N + 1, 0), M(N + 1, 0);
for (ll i = 0; i < N; i++)
{
S.at(i + 1) = S.at(i) + A.at(i);
M.at(i + 1) = max(M.at(i), A.at(i));
}
vector<ll> SS(N + 1, 0);
for (ll i = 0; i < N; i++)
{
SS.at(i + 1) = SS.at(i) + S.at(i);
}
for (ll k = 0; k < N; k++)
{
ll ans = S.at(k + 1) + (k + 1) * M.at(k + 1) + SS.at(k + 1);
cout << ans << '\n';
}
} | #include <iostream>
#include <string>
#include <vector>
#include <numeric>
#include <algorithm>
using ll = long long;
using namespace std;
ll N;
int main(int argc, char *argv[]) {
cin >> N;
vector<ll> A(N);
for (ll i = 0; i < N; i++) {
cin >> A[i];
}
ll ans = 1LL << 60;
for (ll i = 0; i < (1 << (N - 1)); i++) {
ll tmp = 0;
ll total = 0;
for (ll j = 0; j < N; j++) {
tmp |= A[j];
if ((1 << j) & i) {
total ^= tmp;
tmp = 0;
}
}
total ^= tmp;
ans = min(ans, total);
}
cout << ans << endl;
}
|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = long long;
int main(){
int a,b;
ll k;
cin >> a >> b >> k;
int n = a+b;
string s(n, 'a');
ll f1,f2,f3;
for(int i=0;i<n;i++){
ll sum = 1;
ll j = 1, l = 1, m = 1;
if(b == 0){
s[i] = 'a';
continue;
}else if(a == 0){
s[i] = 'b';
continue;
}
while(1){
if(j <= a+b-1){
sum *= j;
j++;
}
if(l <= a-1){
if(sum % l == 0){
sum /= l;
l++;
}
}
if(m <= b){
if(sum % m == 0){
sum /= m;
m++;
}
}
if(j > a+b-1 && l > a-1 && m > b) break;
}
if(k > sum){
s[i] = 'b';
k -= sum;
b--;
}else{
s[i] = 'a';
a--;
}
}
cout << s << endl;
}
| #include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define dl double
using namespace std;
const int N = 1e6 + 7;
const long long mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int L,R;
int p[N];
vector<int> v[N];
void solve()
{
cin >> L >> R;
for(int i = 2; i < N; i++){
if(!p[i]){
for(int j = i; j < N; j += i){
v[j].push_back(i);
p[j] = 1;
}
}
}
ll ans = 0;
vector<int> g((1 << 10), 1);
for(int i = L; i <= R; i++){
if(i > 1){
ans += R / i * 2;
ans--;
}
int l = (int)v[i].size();
ll res = 0;
g[0] = 1;
for(int j = 0; j < l; j++){
for(int h = 0; h < (1 << j); h++){
g[(1 << j) + h] = g[h] * v[i][j];
}
}
for(int j = 1; j < (1 << l); j++){
if(__builtin_popcount(j) % 2)res += R / g[j] - (L - 1) / g[j];
else res -= R / g[j] - (L - 1) / g[j];
}
res = R - L + 1 - res;
ans += res;
}
ll l = R - L + 1;
cout << l * l - ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//freopen( "input.txt" , "r" , stdin );
//freopen( "output.txt" , "w" , stdout );
int t = 1;
//cin >> t;
while(t--){
solve();
}
}
|
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T> using vt = vector<T>;
template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define pb push_back
#define SZ(x) ((int)((x).size()))
#define EACH(x, a) for (auto& x: a)
#define FOR(i,s,n) for (ll i = (s); (i) < (n); ++i)
#define FORD(i,s,l) for (ll i = (s); (i) >= l; --i)
#define F first
#define S second
#define TC int __tc; cin >> __tc; FOR(case_num,1,__tc+1)
#define TEST(x,i) ((x)&(1ll<<(i)))
#define SET(x,i) ((x)|(1ll<<(i)))
#define FLIP(x,i) ((x)^(1ll<<(i)))
#define CLEAR(x,i) ((x)&~(1ll<<(i)))
const double pi = 4 * atan(1);
using ll = long long;
using pll = pair<ll, ll>;
mt19937 mt_rng(chrono::steady_clock::now().time_since_epoch().count());
ll randint(ll a, ll b) {
return uniform_int_distribution<ll>(a, b)(mt_rng);
}
template<class T> bool umin(T& a, const T& b) {
return b<a?a=b, 1:0;
}
template<class T> bool umax(T& a, const T& b) {
return a<b?a=b, 1:0;
}
template<class ForwardIterator> void read(ForwardIterator first,
ForwardIterator last) {
while (first != last) {
cin >> (*first);
++first;
}
}
template<class T> void read(vector<T> &v) {
read(v.begin(),v.end());
}
template<class ForwardIterator> void print(ForwardIterator first,
ForwardIterator last, string sep = " ", string end = "\n") {
bool ft = true;
while (first != last) {
if (!ft) {cout << sep;} else {ft = false;}
cout << (*first);
++first;
}
cout << end;
}
template<typename T> void print(vector<T> &v, string sep = " ", string end = "\n") {
print(v.begin(),v.end(),sep,end);
}
template<typename T1, typename T2>
std::ostream& operator<<(std::ostream &o, const pair<T1, T2> &p) {
return o << p.F << " " << p.S;
}
template<typename T>
vector<int> argsort(const vector<T> &v, function<bool(T,T)> cmp) {
vector<int> indices(v.size());
iota(indices.begin(), indices.end(), 0);
sort(indices.begin(), indices.end(),[&v,&cmp](int left, int right) -> bool {return cmp(v[left],v[right]);});
return indices;
}
template<typename T>
vector<int> argsort(const vector<T> &v) {
return argsort<T>(v, less<T>());
}
template<typename T, typename T2>
vector<int> argsort(const vector<T> &v, function<T2(T)> key) {
vector<T2> v2(v.size());
for(int i = 0; i < SZ(v); i++) v2[i]=key(v[i]);
return argsort<T2>(v2);
}
inline ll floorDiv(ll x, ll y) {
ll d = x / y;
ll r = x % y;
return r ? (d - ((x < 0) ^ (y < 0))) : d;
}
ll ceilDiv(ll x, ll y) {
return -floorDiv(-x, y);
}
ll bin_search(ll lo, ll hi, function<bool(ll)> predicate) {
int sign = lo <= hi ? 1 : -1;
lo *= sign;
hi *= sign;
hi++;
while (lo < hi) {
ll mid = lo + floorDiv(hi-lo, 2);
if (!predicate(sign * mid)) {
lo = mid + 1;
} else {
hi = mid;
}
}
return sign * lo;
}
const ll MOD = 1000000007;
const int MAXN = 200005;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(8);
double a,b;
cin >> a >> b;
cout << (b*a/100) << "\n";
}
| #include <bits/stdc++.h>
using namespace std;
int main()
{
int h, w, mini = -1, sum = 0;
cin >> h >> w;
vector<vector<int>> a(h, vector<int>(w, 0));
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
cin >> a.at(i).at(j);
if (mini == -1)
{
mini = a.at(i).at(j);
}
else
{
mini = min(mini, a.at(i).at(j));
}
}
}
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
sum += a.at(i).at(j) - mini;
}
}
cout << sum << endl;
}
|
#include<bits/stdc++.h>
using namespace std;
int N;
int a[300005];
map<long long,int>mp;
long long cnt,ans=0;
int main()
{
cin>>N;
for(int i=1;i<=N;i++)
{cin>>a[i];
mp[a[i]]++;
}
if(mp[a[1]]==N){cout<<"0";return 0;}//�״���
for(auto &v:mp)ans+=(long long)v.second*(v.second-1)/2;//v.secondΪ���ͣ���ǿ��
cnt=(long long)N*(N-1)/2-ans;//NΪ���ͣ���Ҫǿ��תΪlonglong
cout<<cnt;
return 0;
}
| #include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define N 100004
#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#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) fixed<<setprecision(y)<<x
#define mk(arr,n,type) type *arr=new type[n];
#define w(x) int x; cin>>x; while(x--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
void c_p_c()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}
int32_t main()
{
c_p_c();
int n; cin >> n;
int *arr = new int[n + 1];
for (int i = 1; i <= n; i++)
cin >> arr[i];
map<int, vector<int>>m;
for (int i = 1; i <= n; i++)
m[arr[i]].pb(i);
int ans = 0;
for (int i = 1; i <= n; i++)
{
int ind = m[arr[i]].end() - upper_bound(m[arr[i]].begin(), m[arr[i]].end(), i);
ans += (n - i) - ind;
//cout << ans << endl;
}
cout << ans << '\n';
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define re register
#define in inline
#define get getchar()
#define ll long long
int main()
{
ll n;
cin>>n;
if(n<=2) cout<<1<<endl;
else
{
ll l=2,r=sqrt(2*n),mid,ans;
while(l<=r)
{
mid=l+r>>1;
if((2+mid)*(mid-1)/2<=n) ans=mid, l=mid+1;
else r=mid-1;
}
cout<<2+((n-1)-ans)<<endl;
}
}
| #include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define mod 1000000007
int main(){
FIO;
ll t,n,k,i,j,ans=0;
string s;
cin >> s;
int a[26]={0};
t=0;
n=s.size();
for(i=n-2;i>=0;i--){
if(s[i]==s[i+1]){
ans+=t-a[s[i]-'a'];
for(j=0;j<26;j++)
if(j!=(s[i]-'a')){
a[s[i]-'a']+=a[j];
a[j]=0;
}
}
a[s[i+1]-'a']++;
t++;
}
cout << ans << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i = (m); i <= (n); i++)
#define zep(i,m,n) for(ll i = (m); i < (n); i++)
#define rrep(i,m,n) for(ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define printa(x,m,n) for(int i = (m); i <= n; i++){cout << (x[i]) << " ";} cout<<endl;
long long extGcd(long long a, long long b, long long &p, long long &q) {
if (b == 0) { p = 1; q = 0; return a; }
long long d = extGcd(b, a%b, q, p);
q -= a/b * p;
return d;
}
pair<long long, long long> ChineseRem(long long b1, long long m1, long long b2, long long m2) {
long long p, q;
long long d = extGcd(m1, m2, p, q);
if ((b2 - b1) % d != 0) return make_pair(0, -1);
long long m = m1 * (m2/d);
long long tmp = (b2 - b1) / d * p % (m2/d);
long long r = ((b1 + m1 * tmp) % m + m) % m;
return make_pair(r, m);
}
int main(){
cin.tie(0); ios::sync_with_stdio(false);
ll t; cin >> t;
while(t--){
ll x, y, p, q; cin >> x >> y >> p >> q;
ll ans = -1;
zep(i, x, x + y){
zep(j, p, p + q){
auto res = ChineseRem(i, 2 * (x + y), j, p + q);
if(res.second != -1 && (ans == -1 || ans > res.first)){
ans = res.first;
}
}
}
if(ans == -1){
print("infinity")
}else{
print(ans)
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
template<class Cost = int>
struct Edge {
int from, to;
Cost cost;
Edge(const int &from = -1, const int &to = -1, const Cost &cost = 1)
: from(from), to(to), cost(cost) {}
bool operator<(const Edge<Cost> &rhs) const { return this->cost < rhs.cost; }
bool operator>(const Edge<Cost> &rhs) const { return this->cost > rhs.cost; }
};
template<class Cost = int>
struct Graph : vector<vector<Edge<Cost>>> {
int n;
Graph(int _n = 0) : n(_n) { this->resize(n); }
void add_edge(const int &src, const int &dst, const Cost &cost = 1) {
assert(0 <= src && src < n && 0 <= dst && dst < n);
(*this)[src].emplace_back(src, dst, cost);
}
void add_biedge(const int &src, const int &dst, const Cost &cost = 1) {
add_edge(src, dst, cost), add_edge(dst, src, cost);
}
};
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int N, M, X, Y; cin >> N >> M >> X >> Y;
using Info = pair<long long, long long>;
Graph<Info> G(N+1);
for (int i = 0; i < M; i++) {
int a, b, t, k;
cin >> a >> b >> t >> k;
G.add_biedge(a, b, Info(t, k));
}
set<pair<long long, int>> S;
vector<long long> T(N+1, 1e18);
T[X] = 0;
S.emplace(0, X);
while (!S.empty()) {
auto u = *S.begin();
int C = u.second;
long long now = u.first;
S.erase(S.begin());
if (T[C] < now) continue;
for (auto &e: G[C]) {
auto [t, k] = e.cost;
long long nx = (now + k - 1) / k * k + t;
if (T[e.to] > nx) {
T[e.to] = nx;
S.emplace(T[e.to], e.to);
}
}
}
if (T[Y] == 1e18) cout << -1 << "\n";
else cout << T[Y] << "\n";
return 0;
} |
#include <bits/stdc++.h>
#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; i >= n; i--)
#define SORT(v, n) sort(v, v + n)
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;
using Graph = vector<vector<int>>;
using Edge = pair<int, ll>;
const ll INF = 1LL << 60;
const int MAX = 200000;
const int MOD = 1000000007;
int main()
{
// cin高速化
cin.tie(0);
ios::sync_with_stdio(false);
ll n, a, b;
cin >> n >> a >> b;
cout << n - a + b << endl;
} | #include <bits/stdc++.h>
using namespace std;
signed main(void){
int n,a,b;
scanf("%d%d%d",&n,&a,&b);
printf("%d",n-a+b);
}
|
#define _USE_MATH_DEFIMES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
const int MOD = 1'000'000'007;
const int MOD2 = 998'244'353;
const int INF = 1'000'000'000; //1e9
const int NIL = -1;
const long long LINF = 1'000'000'000'000'000'000; // 1e18
const long double EPS = 1E-10L;
template<class T, class S> inline bool chmax(T &a, const S &b){
if(a < b){a = b; return true;}
return false;
}
template<class T, class S> inline bool chmin(T &a, const S &b){
if(b < a){a = b; return true;}
return false;
}
template<class T, class S> inline bool exist(std::set<T> &s, const S &e){
return (s.find(e) != std::end(s));
}
int main(){
int L, R; std::cin >> L >> R;
long long ans{};
std::vector<long long> cnt(R+1, 0);
for(int g{R}; g >= 2; --g){
long long k{R/g - (L-1)/g}, c{k * k};
for(int i{2*g}; i <= R; i += g){
c -= cnt[i];
}
cnt[g] = c;
if(std::max(2, L) <= g && g <= R){
c -= 2 * k - 1;
}
ans += c;
}
std::cout << ans << std::endl;
return 0;
}
| #include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;
int main(void){
string str;
int num;
int i,j,k;
int a,b;
int ans=0;
int flag =0;
cin >> a >> b;
for(;;){
a = a-b;
if(a < 0){
break;
}
ans++;
}
cout << ans << endl;
return 0;
}
|
#pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
using ll = int64_t;
using db = double;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vector<int>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vpii = vector<pii>;
using vpll = vector<pll>;
constexpr char newl = '\n';
constexpr double eps = 1e-10;
#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define F0R(i,b) FOR(i,0,b)
#define RFO(i,a,b) for (int i = ((b)-1); i >=(a); i--)
#define RF0(i,b) RFO(i,0,b)
#define show(x) cout << #x << " = " << x << '\n';
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define sz(x) (int)(x).size()
#define YesNo {cout<<"Yes";}else{cout<<"No";}
#define YESNO {cout<<"YES";}else{cout<<"NO";}
#define v(T) vector<T>
#define vv(T) vector<vector<T>>
template<typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }
template<class T> bool lcmp(const pair<T, T>& l, const pair<T, T>& r) {
return l.first < r.first;
}
template<class T> istream& operator>>(istream& i, v(T)& v) {
F0R(j, sz(v)) i >> v[j];
return i;
}
template<class A, class B> istream& operator>>(istream& i, pair<A, B>& p) {
return i >> p.first >> p.second;
}
template<class A, class B, class C> istream& operator>>(istream& i, tuple<A, B, C>& t) {
return i >> get<0>(t) >> get<1>(t) >> get<2>(t);
}
template<class T> ostream& operator<<(ostream& o, const pair<T, T>& v) {
o << "(" << v.first << "," << v.second << ")";
return o;
}
template<class T> ostream& operator<<(ostream& o, const vector<T>& v) {
F0R(i, v.size()) {
o << v[i] << ' ';
}
o << newl;
return o;
}
template<class T> ostream& operator<<(ostream& o, const set<T>& v) {
for (auto e : v) {
o << e << ' ';
}
o << newl;
return o;
}
template<class T1, class T2> ostream& operator<<(ostream& o, const map<T1, T2>& m) {
for (auto& p : m) {
o << p.first << ": " << p.second << newl;
}
o << newl;
return o;
}
#if 1
// INSERT ABOVE HERE
signed main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int N, M; cin >> N >> M;
vv(vi) es(N, vvi(26));
F0R(i, M) {
int a, b; char c; cin >> a >> b >> c;
a--; b--;
es[a][c-'a'].push_back(b);
es[b][c - 'a'].push_back(a);
}
//
int inf = INT32_MAX / 2;
vvi dp(N, vi(N, inf));
dp[0][N - 1] = 0;
queue<pii> qu;
qu.emplace(0, N - 1);
int step = 0;
while (sz(qu)) {
queue<pii> nq;
while (sz(qu)) {
auto [u, v] = qu.front(); qu.pop();
if (dp[u][v] < step) continue;
//cout << (u + 1) << '-' << (v + 1) << newl;
F0R(c, 26) {
for (auto to : es[u][c]) {
for (auto to2 : es[v][c]) {
if (dp[to][to2] <= step + 1) continue;
dp[to][to2] = step + 1;
nq.emplace(to, to2);
}
}
}
}
qu = move(nq);
step++;
}
cout << (dp[N - 1][0] >= inf ? -1 : dp[N- 1][0]) << newl;
}
#endif
| /// Containers Start
#include <algorithm>
#include <bitset>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <regex>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
/// C Header Files
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <math.h>
#include <stdio.h>
/// Containers End
using namespace std;
/// Math Start
#define PI acos(-1.0)
#define Pi 3.141592653589793
#define EPS (1e-7)
#define INF (0x3f3f3f3f)
/// Math End
/// Extra Start
#define nn '\n'
#define pb push_back
#define ull unsigned long long
#define ll long long
#define MOD 1000000007
#define sz(a) int((a).size())
#define space " "
#define All(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
/// Extra End
#define rep(i, n) for (int i = 0; i < n; i++)
#define Cini(a) \
int a; \
cin >> a;
#define Cinii(a, b) \
int a, b; \
cin >> a >> b;
#define Ciniii(a, b, c) \
int a, b, c; \
cin >> a >> b >> c;
#define Cins(s) \
string s; \
cin >> s;
#define Cinss(s1, s2) \
string s1, s2; \
cin >> s1 >> s2;
#define Cinc(c) \
char c; \
cin >> c;
/// Functions End
/// Debug Start
#define deb(x) cout << #x << ": " << x << endl
#define deb2(x, y) cout << #x << ": " << x << '\t' << #y << ": " << y << endl;
#define deb3(x, y, z) \
cout << #x << ": " << x << '\t' << #y << ": " << y << '\t' << #z << ": " \
<< z << endl;
/// Debug End
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
/**>>>>>>>>>>>>>>>>>>> END <<<<<<<<<<<<<<<<<<**/
/// template
signed main(void) {
cin.sync_with_stdio(false);
cin.tie(0);
Cini(N);
vector<int> A(N,0);
rep(i,N) cin >> A[i];
stack<int> st; // <高さ, index>
st.push(-1);
int maxAmount = 0;
for(int i=0;i<N;i++){
auto top = st.top();
while(st.top() != -1 && A[st.top()]>=A[i]){
top = st.top();
st.pop();
maxAmount = max(maxAmount, (i - st.top()-1) * A[top]);
}
st.push(i);
}
while(st.top()!=-1){
auto top = st.top();
st.pop();
maxAmount = max(maxAmount, (N - st.top()-1) * A[top]);
}
cout << maxAmount << nn;
return 0;
} |
#include<bits/stdc++.h>
typedef long long ll;
#define V(a) vector<a>
#define P(a,b) pair<a,b>
#define A(a) set<a>
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define in insert
#define bp __builtin_popcount
#define ppb pop_back
#define FOR(a,b,c) for(ll(a) = b; a < c; a++)
#define FORN(a,b,c) for(ll(a) = b; a <= c; a++)
#define FOREV(a,b,c) for(ll(a)=b; a > c; a--)
#define FOREVN(a,b,c) for(ll(a)=b; a >= c; a--)
#define fastio ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
using namespace std;
bool cprime(int x)
{
if (x == 1)
return false;
if (x <= 3)
return true;
if (x % 2 == 0 || x % 3 == 0)
return false;
for (int i = 5; i <= sqrt(x); i += 6)
if (x % i == 0 || x % (i + 2) == 0)
return false;
return true;
}
bool cpalin(string s)
{
int n = s.length(), l;
if (n == 1)
return true;
if (n % 2 == 0)
l = n / 2 - 1;
else
l = (n - 1) / 2 - 1;
for (int i = 0; i <= l; i++)
if (s[i] != s[n - i - 1])
return false;
return true;
}
void sieveOfEratosthenes(int n)
{
bool arr[n + 1];
FOR(i, 0, n + 1)
arr[i] = true;
for (int i = 2; i * i <= n; i++)
{
if (arr[i] == true)
{
for (int j = i * i ; j <= n; j += i)
{
if (arr[j] == true)
arr[j] = false;
}
}
}
}
ll ndivs(ll n)
{
int N = 100;
bool arr[N];
FOR(i, 0, N)
arr[i] = true;
for (int i = 2; i * i <= N; i++)
{
if (arr[i] == true)
{
for (int j = i * i ; j <= N; j += i)
{
if (arr[j] == true)
arr[j] = false;
}
}
}
ll p = 1;
FOR(i, 2, N)
{
if (arr[i] == true && n % i == 0)
{
ll m = 0; ll cp = n;
while (cp % i == 0)
{
cp /= i;
m++;
}
p *= (m + 1);
}
}
return p;
}
bool compare( ll a, ll b) // false return swap
{
return a > b;
}
ll power( ll a, ll b)
{
ll p = 1;
while (b > 0)
{
if (b & 1)
p *= a;
a *= a;
b >>= 1;
}
return p;
}
string dtobin(ll n)
{
string s;
while (n)
{
s = to_string((n % 2)) + s;
n /= 2;
}
return s;
}
ll bintod(string s)
{
ll n = 0;
FOR(i, 0, s.length())
if (s[i] == '0')
continue;
else
n += pow(2, (s.length() - 1 - i));
return n;
}
ll sdigs(ll n)
{
int s = 0;
while (n)
{
s += n % 10;
n /= 10;
}
return s;
}
ll fact(ll p)
{
int x = 1;
FORN(i, 1, p)
x *= i;
return x;
}
ll ncr(ll n , ll r)
{
ll ans = (fact(n) ) / (fact(r) * fact(n - r));
return ans;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("Input.txt", "r", stdin);
freopen("Output.txt", "w", stdout);
#endif
ll n;
cin >> n;
ll arr[n];
ll s = 0;
ll ss = 0;
FOR(i, 0, n) {
cin >> arr[i];
s += arr[i];
ss += arr[i] * arr[i];
}
ll ans = n * ss - s * s;
cout << ans;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define MP make_pair
#define PB push_back
#define REP(i, L, R) for (int i = L; i < (R); ++i)
#define SZ(x) (int)x.size()
using ll = long long;
using ld = long double;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
template <typename T>
bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <typename T>
bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
void solve()
{
ll n;
cin >> n;
ll sum = 1e19;
for (int b = 60; b >= 0; --b)
{
ll a = n / (1LL << b);
ll c = n - a * (1LL << b);
sum = min(sum, a + b + c);
}
cout << sum << endl;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int T = 1;
while (T--)
{
solve();
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define dd double
#define endl "\n"
#define pb push_back
#define all(v) v.begin(),v.end()
#define mp make_pair
#define fi first
#define se second
#define vll vector<ll>
#define pll pair<ll,ll>
#define fo(i,n) for(int i=0;i<n;i++)
#define fo1(i,n) for(int i=1;i<=n;i++)
ll mod=1000000007;
ll n,k,t,m,q,flag=0;
ll power(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// ordered_set s ; s.order_of_key(a) -- no. of elements strictly less than a
// s.find_by_order(i) -- itertor to ith element (0 indexed)
ll min(ll a,ll b){if(a>b)return b;else return a;}
ll max(ll a,ll b){if(a>b)return a;else return b;}
ll gcd(ll a , ll b){ if(b > a) return gcd(b , a) ; if(b == 0) return a ; return gcd(b , a%b) ;}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#ifdef NOOBxCODER
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
#define NOOBxCODER 0
#endif
//cin>>t;
t=1;
while(t--){
cin>>n;
ll a[n];
fo(i,n)cin>>a[i];
sort(a,a+n);
map<ll,ll>m1;
ll last=0;
ll ans=1;
fo(i,n){
if(m1[a[i]]==0){
ans*= (a[i]-last + 1);
ans%=mod;
last=a[i];
}
m1[a[i]]++;
}
cout<<ans;
}
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<long long, long long>;
constexpr char ln = '\n';
constexpr long long MOD = 1000000007;
//constexpr long long MOD = 998244353;
constexpr long long INF = 1000000000 + 100;
constexpr long long LINF = 1000000000000000000 + 100;
#define whole(f,x,...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x)
#define rep(i,n) for(int i=0;i<(n);i++)
#define rept(i, j, n) for(int i=(j); i<(n); i++)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<int MOD> struct Fp {
long long val;
constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
if (val < 0) v += MOD;
}
constexpr int getmod() { return MOD; }
constexpr Fp operator - () const noexcept {
return val ? MOD - val : 0;
}
constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
constexpr Fp& operator += (const Fp& r) noexcept {
val += r.val;
if (val >= MOD) val -= MOD;
return *this;
}
constexpr Fp& operator -= (const Fp& r) noexcept {
val -= r.val;
if (val < 0) val += MOD;
return *this;
}
constexpr Fp& operator *= (const Fp& r) noexcept {
val = val * r.val % MOD;
return *this;
}
constexpr Fp& operator /= (const Fp& r) noexcept {
long long a = r.val, b = MOD, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b; swap(a, b);
u -= t * v; swap(u, v);
}
val = val * u % MOD;
if (val < 0) val += MOD;
return *this;
}
constexpr bool operator == (const Fp& r) const noexcept {
return this->val == r.val;
}
constexpr bool operator != (const Fp& r) const noexcept {
return this->val != r.val;
}
friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept {
return os << x.val;
}
friend constexpr istream& operator >> (istream &is, Fp<MOD>& x) noexcept {
return is >> x.val;
}
friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
if (n == 0) return 1;
auto t = modpow(a, n / 2);
t = t * t;
if (n & 1) t = t * a;
return t;
}
};
using mint = Fp<MOD>;
int main(){
int N; cin >> N;
vector<int> A(N);
rep(i, N){
cin >> A[i];
}
whole(sort, A);
mint res = A[0] + 1;
rep(i, N-1){
res *= (A[i+1] - A[i] + 1);
}
cout << res << ln;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<long long, long long>;
constexpr char ln = '\n';
constexpr long long MOD = 1000000007;
constexpr long long INF = 1000000000 + 100;
constexpr long long LINF = 1000000000000000000 + 100;
#define all(v) v.begin( ), v.end()
#define rep(i, n) for(int i=0;i<(n);i++)
#define rept(i, j, n) for(int i=(j); i<(n); i++)
#define rrep(i, n) for(int i=(n); i>=0; 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; }
template<int MOD> struct Fp {
long long val;
constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
if (val < 0) v += MOD;
}
constexpr int getmod() { return MOD; }
constexpr Fp operator - () const noexcept {
return val ? MOD - val : 0;
}
constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
constexpr Fp& operator += (const Fp& r) noexcept {
val += r.val;
if (val >= MOD) val -= MOD;
return *this;
}
constexpr Fp& operator -= (const Fp& r) noexcept {
val -= r.val;
if (val < 0) val += MOD;
return *this;
}
constexpr Fp& operator *= (const Fp& r) noexcept {
val = val * r.val % MOD;
return *this;
}
constexpr Fp& operator /= (const Fp& r) noexcept {
long long a = r.val, b = MOD, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b; swap(a, b);
u -= t * v; swap(u, v);
}
val = val * u % MOD;
if (val < 0) val += MOD;
return *this;
}
constexpr bool operator == (const Fp& r) const noexcept {
return this->val == r.val;
}
constexpr bool operator != (const Fp& r) const noexcept {
return this->val != r.val;
}
friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept {
return os << x.val;
}
friend constexpr istream& operator >> (istream &is, Fp<MOD>& x) noexcept {
return is >> x.val;
}
friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
if (n == 0) return 1;
auto t = modpow(a, n / 2);
t = t * t;
if (n & 1) t = t * a;
return t;
}
};
using mint = Fp<MOD>;
mint dp[202020][20];
int main(){
string N; int K; cin >> N >> K;
int mask = 0; //Nに使用された数字のマスクビット
rep(i, N.size()){
char c = N[i]; int y;
if(c>='A' and c<='F')y=10+c-'A';
else y=(c-'0');
//直前まで(1つ以上の)0,初めて0以外の数字が出る
if(i>0)dp[i+1][1] += 15;
//直前までNと同一、初めてN未満になる
rep(j, y){
if(i==0 and j==0)continue; //初桁のみ0は選べない
int x = __builtin_popcount(mask | 1<<j); //数字jを採用したときの種類数
dp[i+1][x] += 1;
}
//その他
rep(j, 17){
dp[i+1][j] += dp[i][j] * j;
dp[i+1][j+1] += dp[i][j] * (16 - j);
}
mask |= (1<<y);
}
mint res = dp[N.size()][K];
if(__builtin_popcount(mask) == K)res += 1;
cout << res << ln;
}
| #include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <functional>
#include <cassert>
#include <stack>
#include <numeric>
#include <iomanip>
#include <limits>
#include <random>
#include <unordered_map>
typedef int64_t ll;
typedef std::pair<int, int> Pii;
typedef std::pair<ll, ll> Pll;
typedef std::pair<double, double> Pdd;
#define rip(_i, _n, _s) for (int _i = (_s); _i < (int)(_n); _i++)
#define all(_l) _l.begin(), _l.end()
#define rall(_l) _l.rbegin(), _l.rend()
#define MM << " " <<
template<typename _T>
using MaxHeap = std::priority_queue<_T>;
template<typename _T>
using MinHeap = std::priority_queue<_T, std::vector<_T>, std::greater<_T>>;
template<typename _T>
inline bool chmax(_T &_l, const _T _b) {
if (_l < _b) {
_l = _b;
return true;
}
return false;
}
template<typename _T>
inline bool chmin(_T &_l, const _T _b) {
if (_l > _b) {
_l = _b;
return true;
}
return false;
}
template<typename _T>
void vdeb(const std::vector<_T> &bb) {
for (unsigned int i = 0;i < bb.size();i++) {
if (i == bb.size() - 1) std::cout << bb[i];
else std::cout << bb[i] << ' ';
}
std::cout << '\n';
}
template<typename _T>
void vdeb(const std::vector<std::vector<_T>> &bb) {
for (unsigned int i = 0;i < bb.size();i++) {
std::cout << i << ' ';
vdeb(bb[i]);
}
std::cout << '\n';
}
using namespace std;
int main() {
int n; cin >> n;
vector<Pll> da(n);
ll lower = LLONG_MIN/2, upper = LLONG_MAX/2, offset = 0, up = LLONG_MAX/2, lo = LLONG_MIN/2;
rip(i,n,0) {
cin >> da[i].first >> da[i].second;
if(da[i].second == 1) {
lo += da[i].first;
up += da[i].first;
offset += da[i].first;
}
else if(da[i].second == 2) {
chmax(up, da[i].first);
chmax(lo, da[i].first);
chmax(lower, da[i].first-offset);
}
else if(da[i].second == 3) {
chmin(up, da[i].first);
chmin(lo, da[i].first);
chmin(upper, da[i].first-offset);
}
}
int q; cin >> q;
vector<ll> ans(q);
rip(i,q,0) {
int x; cin >> x;
if(upper <= x) ans[i] = up;
else if(x <= lower) ans[i] = lo;
else ans[i] = x+offset;
}
for(auto &&i : ans) cout << i << endl;
} |
#include <algorithm>
#include <array>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define sz(x) (int)(x).size()
#define ms(arr, v) memset(arr, v, sizeof(arr))
#define f first
#define s second
#define pb push_back
// clang-format off
template<typename T> ostream &operator<<(ostream &os, const vector<T> &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T_vector>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');}
template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa){ os << "(" << pa.first << "," << pa.second << ")"; return os; }
template<typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << "(" << v.first << ":" << v.second << ")" << ","; os << "}"; return os; }
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);
// clang-format on
void solve() { }
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n);
rep(i, 0, n) cin >> a[i];
int cnt = 0;
for (int j = 0; j < n; j++) {
if (a[j] > 10) {
cnt += a[j] - 10;
}
}
cout << cnt;
// int tests;
// cin >> tests;
// while (tests-- > 0)
// solve();
}
|
#include <iostream>
using namespace std;
int main (){
long long n,a;
cin >> n;
long long accm(0);
for (size_t i=0; i<n; i++){
cin >> a;
if (a <= 10)
continue;
accm += a - 10;
}
cout << accm << endl;
return 0;
}
|
#pragma GCC optimize ("O2")
#pragma GCC target ("avx")
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
//#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
char cn[1200010];
char* ci = cn, * di = cn, ct;
char tc[200002], *d1 = tc + 100000, *d2 = tc + 200000;
const char d[2] = "0";
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
rep(i, 100000) tc[i] = '0';
rep(i, 100000) d1[i] = '1';
d2[0] = '0';
d2[1] = '\n';
fread(cn, 1, 1200010, stdin);
int T = 0;
while ((ct = *ci++) >= '0') T = T * 10 + ct - '0';
rep(t, T) {
int N = 0;
while ((ct = *ci++) >= '0') N = N * 10 + ct - '0';
memcpy(di, d1 - N, N);
di += N;
memcpy(di, d2 - N, N + 2);
di += N + 2;
ci += 6 * N + 3;
}
fwrite(cn, 1, di - cn, stdout);
Would you please return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define ri register int
typedef long long ll;
const int maxn=3010,mod=1e9+7;
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[maxn][maxn],sum[maxn][maxn];
int n;
ll a[maxn][maxn];
int main(){
scanf("%d",&n);
for(ri i=1;i<=n;++i){
ll x;
scanf("%lld",&x);
for(ri j=1;j<=n;++j)a[i][j]=(a[i-1][j]+x)%j;
}
for(ri i=1;i<=n;++i)sum[i][a[n][i]]=1;
for(ri i=n-1;i;--i){
for(ri j=1;j<n;++j){
sum[j][a[i][j]]+=sum[j+1][a[i][j+1]];
}
}
printf("%d",sum[1][0]);
return 0;
} |
/* *****fireice**** */
#include<bits/stdc++.h>
#define ll long long int
#define ld long double
#define vi vector<int>
#define vc vector<char>
#define vl vector<ll>
#define vii vector<pair<int,int>>
#define vll vector<pair<ll,ll>>
#define pii pair<int,int>
#define pb push_back
#define eb emplace_back
#define el '\n'
#define N 1000000007
#define M 2147483647
#define MM 1e18;
#define re return
#define F first
#define S second
#define L length
#define deb(x) cout<<#x<<" "<<x<<el;
#define mp make_pair
#define con continue
#define PI 3.14159265
#define all(a) (a).begin(),(a).end()
#define forup(i,n) for(int i=0;i<n;i++)
#define forf(i,x,n) for(int i=x;i<n;i++)
#define ford(i,n) for(int (i)=(n)-1;i>=0;(i)--)
#define take(a,n) forup(i,n) cin>>a[i];
using namespace std;
int gcdExtended(int a, int b, int *x, int *y)
{
// Base Case
if (a == 0)
{
*x = 0, *y = 1;
return b;
}
int x1, y1; // To store results of recursive call
int gcd = gcdExtended(b%a, a, &x1, &y1);
// Update x and y using results of recursive
// call
*x = y1 - (b/a) * x1;
*y = x1;
return gcd;
}
ll modInverse(int a, int m)
{
int x, y;
int g = gcdExtended(a, m, &x, &y);
// m is added to handle negative x
ll res = ((ll)x%m + (ll)m) % m;
return res;
}
ll poww(ll x,ll n)
{
if(n==0)
return 1;
else if(n==1) return x;
else if(n%2 == 0) //n is even
return poww(x*x,n/2);
else //n is odd
return x*poww(x*x,(n-1)/2);
}
ll modularExponentiation(ll x,ll n,ll Z)
{
ll result=1;
while(n>0)
{
if(n % 2 ==1)
result=(result * x)%Z;
x=(x*x)%Z;
n=n/2;
}
return result;
}
template<typename T>
T maxx(T a,T b)
{
if(a<b) re b;
re a;
}
template<typename T, typename... Args>
T maxx(T a,T b, Args... args)
{
re(maxx(maxx(a,b),args...));
}
template<typename T>
T minn(T a,T b)
{
if(a<b) re a;
re b;
}
template<typename T, typename... Args>
T minn(T a,T b, Args... args)
{
re(minn(minn(a,b),args...));
}
template<typename T>
void read(T& a)
{
cin>>a;
}
template<typename T, typename... Args>
void read(T& a,Args&... args)
{
cin>>a; read(args...);
}
template<typename T>
void write(T a)
{
cout<<a<<' ';
}
template<typename T, typename... Args>
void write(T a,Args... args)
{
cout<<a<<' '; write(args...);
}
inline ll min(ll a,ll b)
{
if(a<b) re a;
else re b;
}
inline ll max(ll a,ll b)
{
if(a<b) re b;
else re a;
}
ll gcd(ll a, ll b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return gcd(a-b, b);
else return gcd(a, b-a);
}
ll ncr(int n,int r)
{
ll ans=1;
r=min(r,n-r);
forup(i,r)
{
ans=((ans*((n-i)%N))%N*modInverse(i+1,N))%N;
}
re ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
int t=1;
//cin>>t;
while(t--)
{
ll n,m,k;
string s;
cin>>n;
cout<<(n+99)/100<<el;
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
using ll=long long;
int main()
{
int tc=1;
// cin>>tc;
while(tc--)
{
ll n;
cin>>n;
cout<<ceil( (n*1.0)/100 )<<endl;
}
return 0;
}
|
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int mod=1e9+7;
const int N=1e5+10;
int a[3000][3000];
struct node
{
int ta,ao;
int id;
} b[3000][3000];
int main()
{
int h,w;
cin>>h>>w;
char c;
for(int i=1; i<=h; i++)
{
for(int j=1; j<=w; j++)
{
cin>>c;
if(c=='+')
{
a[i][j]=1;
}
else
a[i][j]=-1;
}
}
for(int i=h; i>=1; i--)
{
for(int j=w; j>=1; j--)
{
if(i==h&&j==w)
{
b[i][j].id=(i+j)%2;
continue;
}
if(i==h)
{
b[i][j].id=(b[i][j+1].id+1)%2;
if(b[i][j].id%2==0)
{
b[i][j].ta=b[i][j+1].ta+a[i][j+1];
b[i][j].ao=b[i][j+1].ao;
}
else
{
b[i][j].ao=b[i][j+1].ao+a[i][j+1];
b[i][j].ta=b[i][j+1].ta;
}
}
else if(j==w)
{
b[i][j].id=(b[i+1][j].id+1)%2;
if(b[i][j].id%2==0)
{
b[i][j].ta=b[i+1][j].ta+a[i+1][j];
b[i][j].ao=b[i+1][j].ao;
}
else
{
b[i][j].ao=b[i+1][j].ao+a[i+1][j];
b[i][j].ta=b[i+1][j].ta;
}
}
else
{
b[i][j].id=(b[i][j+1].id+1)%2;
if(b[i][j].id%2==0)
{
if(b[i+1][j].ta+a[i+1][j]-b[i+1][j].ao>b[i][j+1].ta+a[i][j+1]-b[i][j+1].ao)
{
b[i][j].ta=b[i+1][j].ta+a[i+1][j];
b[i][j].ao=b[i+1][j].ao;
}
else
{
b[i][j].ta=b[i][j+1].ta+a[i][j+1];
b[i][j].ao=b[i][j+1].ao;
}
}
else
{
if(b[i+1][j].ao+a[i+1][j]-b[i+1][j].ta>b[i][j+1].ao+a[i][j+1]-b[i][j+1].ta)
{
b[i][j].ao=b[i+1][j].ao+a[i+1][j];
b[i][j].ta=b[i+1][j].ta;
}
else
{
b[i][j].ao=b[i][j+1].ao+a[i][j+1];
b[i][j].ta=b[i][j+1].ta;
}
}
}
}
}
if(b[1][1].ao>b[1][1].ta)
{
cout<<"Aoki"<<endl;
}
else if(b[1][1].ao<b[1][1].ta)
{
cout<<"Takahashi"<<endl;
}
else
{
cout<<"Draw"<<endl;
}
return 0;
}
| #include <bits/stdc++.h>
#define rep(i,n) for (ll i = 0; i < (ll)(n); i++)
#define repr(i,a,b) for (ll i = (a); i < (ll)(b); i++)
#define rng(x) (x).begin(), (x).end()
#define rrng(x) (x).rbegin(), (x).rend()
#define popcnt(x) __builtin_popcountll(x)
#define pb push_back
#define eb emplace_back
#define MP make_pair
#define MT make_tuple
#define fr first
#define sc second
using namespace std;
template <class T, class U> inline bool chmax(T &a, const U &b) { return (a < b ? a = b, 1 : 0); }
template <class T, class U> inline bool chmin(T &a, const U &b) { return (a > b ? a = b, 1 : 0); }
template <class T> using vc = vector<T>;
template <class T> using vv = vc<vc<T>>;
template <class T> using vvv = vc<vv<T>>;
template <class T> using PQ = priority_queue<T, vc<T>, greater<T>>;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using ti3 = tuple<int, int, int>;
using tl3 = tuple<ll, ll, ll>;
using vi = vc<int>;
using vl = vc<ll>;
using vs = vc<string>;
using vpl = vc<pll>;
using vpd = vc<pdd>;
using vvi = vv<int>;
using vvl = vv<ll>;
using vvpl = vv<pll>;
const ld pi = 3.141592653589793;
const ll linf = 1001001001001001001LL;
const int inf = 1001001001;
constexpr int MOD = 1e9+7;
// constexpr int MOD = 998244353;
struct mint {
long long x;
mint(long long x = 0) noexcept : x((x%MOD + MOD) % MOD) {}
// basis
mint operator-() const { return mint(-x); }
mint& operator+=(const mint a) noexcept { if((x += a.x) >= MOD) x -= MOD; return *this; }
mint& operator-=(const mint a) noexcept { if((x += MOD-a.x) >= MOD) x -= MOD; return *this; }
mint& operator*=(const mint a) noexcept { (x *= a.x) %= MOD; return *this; }
mint operator+(const mint a) const noexcept { return mint(*this) += a; }
mint operator-(const mint a) const noexcept { return mint(*this) -= a; }
mint operator*(const mint a) const noexcept { return mint(*this) *= a; }
mint mpow(long long t) const noexcept {
if(!t) return 1;
mint a = mpow(t>>1);
a *= a;
if(t&1) a *= *this;
return a;
}
// for prime MOD
mint inv() const noexcept { return mpow(MOD-2); }
mint& operator/=(const mint a) noexcept { return *this *= a.inv(); }
mint operator/(const mint a) const noexcept { return mint(*this) /= a; }
// comparison
bool operator==(const mint &a) const noexcept { return this->x == a.x; }
bool operator!=(const mint &a) const noexcept { return this->x != a.x; }
// I/O stream
friend istream& operator>>(istream& is, mint& a) noexcept { return is >> a.x; }
friend ostream& operator<<(ostream& os, const mint& a) noexcept { return os << a.x; }
};
// additional
mint mpow(const long long &a, long long n) noexcept { return mint(a).mpow(n); }
mint mpow(const mint &a, long long n) noexcept { return a.mpow(n); }
int main() {
ll h, w;
cin >> h >> w;
vs S(h);
rep(i,h) cin >> S[i];
vvl R(h, vl(w, 0)), C(h, vl(w, 0));
rep(i,h) {
ll l = 0, r = 0;
while (l < w) {
while (r < w && S[i][r] == '.') r++;
repr(j,l,r) R[i][j] = r-l;
if (l == r) r++;
l = r;
}
}
rep(i,w) {
ll l = 0, r = 0;
while (l < h) {
while (r < h && S[r][i] == '.') r++;
repr(j,l,r) C[j][i] = r-l;
if (l == r) r++;
l = r;
}
}
ll K = 0;
rep(i,h)rep(j,w) K += S[i][j] == '.';
mint ans = 0;
rep(i,h)rep(j,w) {
if (S[i][j] == '#') continue;
ans += mpow(2, K-(R[i][j]+C[i][j]-1))*(mpow(2, R[i][j]+C[i][j]-1)-1);
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#ifdef ENABLE_DEBUG
#define dump(a) cerr<<#a<<"="<<a<<endl
#define dumparr(a,n) cerr<<#a<<"["<<n<<"]="<<a[n]<<endl
#else
#define dump(a)
#define dumparr(a,n)
#endif
#define FOR(i, a, b) for(ll i = (ll)a;i < (ll)b;i++)
#define For(i, a) FOR(i, 0, a)
#define REV(i, a, b) for(ll i = (ll)b-1LL;i >= (ll)a;i--)
#define Rev(i, a) REV(i, 0, a)
#define REP(a) For(i, a)
#define SIGN(a) (a==0?0:(a>0?1:-1))
typedef long long int ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<ll, ll> pll;
typedef pair<ll,pll> ppll;
typedef vector<ll> vll;
typedef long double ld;
typedef pair<ld,ld> pdd;
pll operator+(pll a,pll b){
return pll(a.first+b.first,a.second+b.second);
}
pll operator-(pll a,pll b){
return pll(a.first-b.first,a.second-b.second);
}
pll operator*(ll a,pll b){
return pll(b.first*a,b.second*a);
}
const ll INF=(1LL<<60);
#if __cplusplus<201700L
ll gcd(ll a, ll b) {
a=abs(a);
b=abs(b);
if(a==0)return b;
if(b==0)return a;
if(a < b) return gcd(b, a);
ll r;
while ((r=a%b)) {
a = b;
b = r;
}
return b;
}
#endif
template<class T>
bool chmax(T& a,const T& b){
if(a<b){
a=b;
return true;
}
return false;
}
template<class T>
bool chmin(T& a,const T& b){
if(a>b){
a=b;
return true;
}
return false;
}
template<class S,class T>
std::ostream& operator<<(std::ostream& os,pair<S,T> a){
os << "(" << a.first << "," << a.second << ")";
return os;
}
template<class T>
std::ostream& operator<<(std::ostream& os,vector<T> a){
os << "[ ";
REP(a.size()){
os<< a[i] << " ";
}
os<< " ]";
return os;
}
void solve(long long A, long long B){
long double ans=100*(A-B)/(long double)A;
cout<<ans<<endl;
}
int main(){
cout<<setprecision(1000);
long long A;
scanf("%lld",&A);
long long B;
scanf("%lld",&B);
solve(A, B);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
float A, B;
cin >> A >> B;
cout << (A - B) * 100 / A << endl;
return 0;
} |
/*{{{*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define PB emplace_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PII> VPII;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(int64_t &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W(x.F); putchar(' '); _W(x.S);}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
#ifdef HOME
#define DEBUG(...) {printf("[DEBUG] ");W(__VA_ARGS__);}
#else
#define DEBUG(...)
#endif
int MOD = 1e9+7;
void ADD(LL& x,LL v){x=(x+v)%MOD;if(x<0)x+=MOD;}
/*}}}*/
const int SIZE = 1<<20;
int N;
VPLL e[SIZE];
LL w[SIZE];
void dfs(int x, int lt) {
for(auto &[y,v]: e[x]) {
if(y==lt){continue;}
w[y]=w[x]^v;
dfs(y,x);
}
}
LL get_bit(LL x, int v) { return (x >> v) & 1; }
void solve() {
dfs(1,1);
int d[60]={};
FOR(i,1,N){
REP(j,60)d[j]+=get_bit(w[i],j);
}
LL an=0;
for(int i=59;i>=0;i--){
an=an*2%MOD;
an=(an+(LL)d[i]*(N-d[i]))%MOD;
}
W(an);
}
void input() {
R(N);
REPP(i,1,N){
int x,y;
LL v;
R(x,y,v);
e[x].PB(MP(y,v));
e[y].PB(MP(x,v));
}
}
int main(){
input();
solve();
return 0;
}
| #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
#define all(x) (x).begin(),(x).end()
using namespace std;
constexpr int INF = 1e9, MOD = 1e9 + 7;
constexpr int64_t LINF = 5e18, LMOD = 998244353;
// #include <atcoder/all>
// using namespace atcoder;
// const int dy[]={0,-1,0,1,1,-1,-1,1};
// const int dx[]={1,0,-1,0,1,1,-1,-1};
using fint64 = int_fast64_t;
template<fint64 MOD>
struct ModInt {
fint64 x;
ModInt():x(0){}
ModInt(fint64 x):
x(x>=0?x%MOD:(MOD-(-x)%MOD)%MOD)
{}
// 負号
ModInt operator -() const{
return ModInt(-x);
}
// 加算
ModInt &operator +=(const ModInt &rhs){
x+=rhs.x;
if(x>=MOD) x-=MOD;
return (*this);
}
ModInt operator +(const ModInt &rhs) const{
return ModInt(*this)+=rhs;
}
// 減算
ModInt &operator -=(const ModInt &rhs){
x+=MOD-rhs.x;
if(x>=MOD) x-=MOD;
return (*this);
}
ModInt operator -(const ModInt &rhs) const{
return ModInt(*this)-=rhs;
}
// 乗算
ModInt &operator *=(const ModInt &rhs){
x*=rhs.x;
if(x>=MOD) x%=MOD;
return (*this);
}
ModInt operator *(const ModInt &rhs) const{
return ModInt(*this)*=rhs;
}
// 除算
ModInt &operator /=(const ModInt &rhs){
(*this)*=rhs.inverse();
return (*this);
}
ModInt operator /(const ModInt &rhs) const{
return ModInt(*this)/=rhs;
}
// 等号
bool operator ==(const ModInt &rhs){
return x==rhs.x;
}
bool operator !=(const ModInt &rhs){
return x!=rhs.x;
}
// 累乗
ModInt pow(fint64 n) const{
fint64 tmp=x;
fint64 ret=1;
while(n>0){
if(n&1) ret=ret*tmp%MOD;
tmp=tmp*tmp%MOD;
n>>=1ll;
}
return ModInt(ret);
}
// 逆元
ModInt inverse() const{
fint64 a=x,b=MOD,s=1,t=0;
while(b>0){
fint64 u=a/b;
a-=u*b;
s-=u*t;
swap(a,b);
swap(s,t);
}
return ModInt(s);
}
// 入出力
friend istream &operator >>(istream &lhs,ModInt<MOD> &rhs){
fint64 x; lhs>>x;
rhs=ModInt<MOD>(x);
return lhs;
}
friend ostream &operator <<(ostream &lhs,const ModInt<MOD> &rhs){
return lhs<<rhs.x;
}
};
using mint = ModInt<LMOD>;
mint dp[5010][5010];
mint _3[5010];
int sum_x[5010][5010];
int sum_y[5010][5010];
int com[5010][5010];
int h, w;
int main() {
int k; cin >> h >> w >> k;
rep(i,h)rep(j,w) com[i][j] = -1;
while(k--) {
int y, x; cin >> y >> x;
--y, --x;
char c; cin >> c;
if(c == 'R') com[y][x] = 0;
if(c == 'D') com[y][x] = 1;
if(c == 'X') com[y][x] = 2;
}
for(int i = h - 1; i >= 0; --i) {
for(int j = w - 1; j >= 0; --j) {
if(j + 1 < w) {
sum_x[i][j] = sum_x[i][j + 1];
sum_x[i][j] += (com[i][j + 1] == -1);
}
}
}
for(int j = w - 1; j >= 0; --j) {
for(int i = h - 1; i >= 0; --i) {
if(i + 1 < h) {
sum_y[i][j] = sum_y[i + 1][j];
sum_y[i][j] += (com[i + 1][j] == -1);
}
}
}
_3[0] = 1;
for(int i = 1; i < 5010; ++i) {
_3[i] = _3[i - 1] * 3;
}
for(int y = h - 1; y >= 0; --y) {
for(int x = w - 1; x >= 0; --x) {
if(y == h - 1 and x == w - 1) {
if(com[y][x] == -1) dp[y][x] = 3;
else dp[y][x] = 1;
continue;
}
mint ret = 0;
if(com[y][x] == -1) {
if(x + 1 < w) ret += dp[y][x + 1] * _3[sum_y[y][x]];
if(y + 1 < h) ret += dp[y + 1][x] * _3[sum_x[y][x]];
ret *= 2;
} else {
if(com[y][x] == 0) {
if(x + 1 < w) ret += dp[y][x + 1] * _3[sum_y[y][x]];
} else if(com[y][x] == 1) {
if(y + 1 < h) ret += dp[y + 1][x] * _3[sum_x[y][x]];
} else {
if(x + 1 < w) ret += dp[y][x + 1] * _3[sum_y[y][x]];
if(y + 1 < h) ret += dp[y + 1][x] * _3[sum_x[y][x]];
}
}
dp[y][x] = ret;
}
}
cout << dp[0][0] << '\n';
return 0;
}
|
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int p[500];
int main() {
int t, i;
scanf("%d", &t);
for (i = 0; i < t; i++) {
int n, c = 1, j, k;
vector<int> v;
scanf("%d", &n);
for (j = 0; j < n; j++) scanf("%d", &p[j]);
for (j = 1; j <= n; j++) {
int x;
for (k = 0; k < n; k++) {
if (p[k] == j) {
x = k;
break;
}
}
if (x == j - 1) continue;
if (x % 2 != c % 2) {
if (j == n - 1) {
v.push_back(n - 3);
v.push_back(n - 2);
v.push_back(n - 3);
v.push_back(n - 2);
v.push_back(n - 3);
break;
} else if (x - 2 >= j - 1) {
v.push_back(x - 2);
swap(p[x - 2], p[x - 1]);
c++;
} else {
v.push_back(x);
v.push_back(x - 1);
swap(p[x], p[x + 1]);
swap(p[x - 1], p[x]);
x++;
}
}
while (x > j - 1) {
v.push_back(x - 1);
swap(p[x - 1], p[x]);
c++;
x--;
}
}
printf("%d\n", v.size());
for (j = 0; j < v.size(); j++) {
if (j > 0) putchar(' ');
printf("%d", v[j] + 1);
}
puts("");
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
/**************************************************/
#define int long long
#define fr(i,st,c) for(int i=st;i<c;i++)
#define rfr(i,e,c) for(int i=e;i>=c;i--)
#define i_output(f1) cout<<(f1?"YES":"NO")<<endl;
#define vi vector<int>
#define pb push_back
#define print(n) cout<<n<<endl
#define thor ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define N 1e9+7
int gcd(int a,int b){
if(b==0)
return a;
return gcd(b,a%b);
}
/***************************************************/
signed main(){
thor
int h,w,x,y;
cin>>h>>w>>x>>y;
vector<string> s(h);
fr(i,0,h){
cin>>s[i];
}
int sum=0;
x--,y--;
fr(i,x,h){
if(s[i][y]=='.'){
sum++;
}
else{
break;
}
}
rfr(i,x,0){
if(s[i][y]=='.'){
sum++;
}
else{
break;
}
}
sum--;
fr(i,y,w){
if(s[x][i]=='.'){
sum++;
}
else{
break;
}
}
sum--;
rfr(i,y,0){
if(s[x][i]=='.'){
sum++;
}
else{
break;
}
}
sum--;
print(sum);
return 0;
}
|
#include<bits/stdc++.h>
#define int long long
#define ri register signed
#define rd(x) x=read()
using namespace std;
const int N=3e5+5;
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){int ret=1;while (y){if (y&1) ret=(ret*x)%z;x=(x*x)%z;y>>=1;}return ret;}
int inv[N],fac[N],ifc[N];
void Init(int n)
{
inv[1]=1;for (ri i=2;i<=n;i++)inv[i]=inv[mod%i]*(mod-mod/i)%mod;
fac[0]=1;for (ri i=1;i<=n;i++) fac[i]=fac[i-1]*i%mod;
ifc[0]=1;for (ri i=1;i<=n;i++) ifc[i]=ifc[i-1]*inv[i]%mod;
}
int C(int n,int m){if (m>n || m<0) return 0;return fac[n]*ifc[m]%mod*ifc[n-m]%mod;}
int C(int n,int a,int b){if (a<0 || b<0 || a+b>n) return 0;return fac[n]*ifc[a]%mod*ifc[b]%mod*ifc[n-a-b]%mod;}
int T,n;
int lcm(int a,int b){return a/__gcd(a,b)*b;}
int exgcd(int a,int b,int &x,int &y)
{
if (!b) {x=1,y=0;return a;}
int ret=exgcd(b,a%b,y,x);y-=a/b*x;
return ret;
}
int a[3],r[3];
int excrt()
{
int M=a[1],R=r[1],x,y,d;
d=exgcd(M,a[2],x,y);
if ((R-r[2])%d) return -1;
x=(R-r[2])/d*x%a[2];
R-=M*x;
M=M/d*a[2];
R%=M;
return (R%M+M)%M;
}
int x,y,p,q,ans;
signed main()
{
rd(T);
while (T--)
{
rd(x);rd(y);rd(p);rd(q);
ans=-1;
n=(p+q)*(2*x+2*y);
for (int i=x;i<x+y;i++) for (int j=p;j<p+q;j++)
{
r[1]=i;r[2]=j;
a[1]=2*x+2*y;a[2]=p+q;
int C=excrt();
if (C==-1) continue;
if (ans==-1) ans=C;
else ans=min(ans,C);
}
if (ans==-1) puts("infinity");
else cout<<ans<<endl;
}
} | #include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
using namespace std;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef long long ll;
template <typename _T>
inline void read(_T &f) {
f = 0; _T fu = 1; char c = getchar();
while (c < '0' || c > '9') { if (c == '-') { fu = -1; } c = getchar(); }
while (c >= '0' && c <= '9') { f = (f << 3) + (f << 1) + (c & 15); c = getchar(); }
f *= fu;
}
template <typename T>
void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x < 10) putchar(x + 48);
else print(x / 10), putchar(x % 10 + 48);
}
template <typename T>
void print(T x, char t) {
print(x); putchar(t);
}
const int N = 1005;
const char str[] = " atcoder";
char c[N];
int T, n;
int main() {
read(T);
while (T--) {
scanf("%s", c + 1); n = strlen(c + 1);
int cando = -1;
for (int i = 1; i <= min(n, 7); i++) {
if (c[i] != str[i]) {
if (c[i] > str[i]) cando = 1;
else cando = 0;
break;
}
}
if (cando == 1 || (cando == -1 && n > 7)) {
printf("0\n");
continue;
}
if (n == 1) {
printf("-1\n");
continue;
}
if (c[2] != 'a') {
printf("1\n");
continue;
}
int ans = 100000;
for (int i = 3; i <= n; i++) {
if (c[i] > 'a') ans = min(ans, i - 1);
if (c[i] > 't') ans = min(ans, i - 2);
}
if (ans == 100000) ans = -1;
print(ans, '\n');
}
return 0;
} |
#include<vector>
#include<cstring>
#include<algorithm>
#include<queue>
#include<iostream>
using namespace std;
int main(){
int a[3];
for(int i=0;i<3;i++) cin>>a[i];
sort(a,a+3);
if(a[2]-a[1]==a[1]-a[0]) cout<<"Yes";
else cout<<"No";
} | #include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define REP(i,j) for(int i=0;i<j;i++)
#define REPA(i,j) for(int i=1;i<=j;i++)
#define FORN(i,j,k) for(int i=j;i<k;i++)
#define vi vector<int>
#define vvi vector<vi >
#define pii pair<int,int>
#define vpii vector<pii >
#define all(a) a.begin(),a.end()
using namespace std;
const int INF=1<<30;
//const ll INF=1ll<<60;
const ll MOD=1e9+7;
inline void read(int &x){
// short neg=1;
x=0;
char c=getchar();
/*while(c<'0'||c>'9'){
if(c=='-')neg=-1;
c=getchar();
}*/
while(c>='0'&&c<='9'){
x=(x<<3)+(x<<1)+(c^48),c=getchar();
}
// x*=neg;
}
ll quick_mod(ll A,ll B){//A^B
ll ret=1;
A%=MOD;
while(B)
{
if(B&1)ret=ret*A%MOD;
B>>=1;
A=A*A%MOD;
}
return ret;
}
int main(void){
int a[3];
REP(i,3)cin>>a[i];
sort(a,a+3);
if(a[1]*2==a[0]+a[2])cout<<"Yes";
else cout<<"No";
return 0;
}
|
#include<bits/stdc++.h>
#define ll long long
#define db double
#define M 200006
#define mo 998244353
using namespace std;
bool s1;
char o;
int Read(){
bool f=1;
while(!isdigit(o=getchar()))if(o=='-')f=0;
int res=o^48;
while(isdigit(o=getchar()))res=(res<<3)+(res<<1)+(o^48);
return f?res:-res;
}
int n,m,ans,cnt[M][14],len[M],Pow[M<<1],Pow1[M<<1];
bool mark[M];
void Mo(int &a){(a>=mo)&&(a-=mo);}
int fast(int x,int y){
int res=1;
while(y){
if(y&1)res=1ll*res*x%mo;
y>>=1;
x=1ll*x*x%mo;
}
return res;
}
int C(int a,int b){
if(a<b)return 0;
return 1ll*Pow[a]*Pow1[b]%mo*Pow1[a-b]%mo;
}
bool s2;
int main(){
// printf("%.6lf\n",(&s2-&s1)/1024.0/1024);
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
n=Read(),m=Read();
for(int i=2;i<=m;++i){
if(mark[i])continue;
for(int j=i;j<=m;j+=i){
mark[j]=1;
++len[j];
int a=j;
while(a%i==0)cnt[j][len[j]]++,a/=i;
}
}
Pow[0]=Pow1[0]=1;
for(int i=1;i<=n+m;++i)Pow[i]=1ll*Pow[i-1]*i%mo;
Pow1[n+m]=fast(Pow[n+m],mo-2);
for(int i=n+m-1;i>=1;--i)Pow1[i]=1ll*Pow1[i+1]*(i+1)%mo;
for(int i=1;i<=m;++i){
int res=1;
for(int j=1;j<=len[i];++j){
int a=cnt[i][j];
res=1ll*res*C(n+cnt[i][j]-1,cnt[i][j])%mo;
}
Mo(ans+=res);
}
printf("%d\n",ans);
return 0;
} | #include <bits/stdc++.h>
#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if(!(p))
#define until(p) while(!(p))
using ll = std::int64_t;
using P = std::tuple<int,int>;
int N;
ll X, Y[60];
ll A[60], B[60];
ll dp[60][10];
ll rec(int i, int c){
if(i == N - 1){
return 1;
}
if(dp[i][c] != -1){
return dp[i][c];
}
ll res = rec(i + 1, (Y[i] + c) / B[i]);
if((Y[i] + c) % B[i] > 0){
res += rec(i + 1, 1);
}
return dp[i][c] = res;
}
int main(){
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
std::cin >> N >> X;
for(int i=0;i<N;++i){
std::cin >> A[i];
}
for(int i=0;i+1<N;++i){
B[i] = A[i+1] / A[i];
}
for(int i=N-1;i>=0;--i){
Y[i] = X / A[i];
X %= A[i];
}
memset(dp, -1, sizeof(dp));
ll res = rec(0, 0);
std::cout << res << std::endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long n;
cin >> n;
long long m = n;
vector<long long> f(88);
f[0] = f[1] = 1;
for (int i = 2; i < (int) f.size(); i++) {
f[i] = f[i - 1] + f[i - 2];
}
debug(f);
vector<int> a(87);
int k = 0;
for (int i = 87; i >= 0; i--) {
if (n >= f[i]) {
n -= f[i];
k = max(k, i);
a[k - i] = 1;
}
}
vector<int> res;
long long x = 0, y = 0;
for (int i = 0; i <= k; i++) {
if (a[i]) {
res.emplace_back(i % 2);
if (i % 2 == 0) {
x++;
} else {
y++;
}
}
if (i != k) {
res.emplace_back(3 - i % 2);
if (i % 2 == 0) {
y += x;
} else {
x += y;
}
}
}
debug(x, y);
if (y == m) {
for (int i = 0; i < (int) res.size(); i++) {
res[i] ^= 1;
}
}
cout << res.size() << '\n';
for (int i : res) {
cout << i + 1 << '\n';
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define loop(i, a) for (int i = 0; i < (a); ++i)
#define cont(i, a) for (int i = 1; i <= (a); ++i)
#define circ(i, a, b) for (int i = (a); i <= (b); ++i)
#define range(i, a, b, c) for (int i = (a); (c) > 0 ? i <= (b) : i >= (b); i += (c))
#define parse(it, x) for (auto &it : (x))
#define pub push_back
#define pob pop_back
#define emb emplace_back
#define mak make_pair
#define mkt make_tuple
typedef long long ll;
typedef long double lf;
const int Inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
mt19937_64 Rand(chrono::steady_clock::now().time_since_epoch().count());
ll n;
ll calc(ll x) {
ll y = n;
ll cs = 0;
while (y) {
cs += x / y;
x %= y;
swap(x, y);
}
return cs + x;
}
int main() {
cin >> n;
while (1) {
ll m = uniform_int_distribution<ll>(0, n)(Rand);
if (calc(m) <= 130) {
ll x = n, y = m;
vector<int> ans;
while (x && y) {
if (x < y) {
y -= x;
ans.pub(4);
}
else {
x -= y;
ans.pub(3);
}
}
while (x) --x, ans.pub(1);
while (y) --y, ans.pub(2);
reverse(all(ans));
printf("%d\n", SZ(ans));
parse(i, ans) printf("%d\n", i);
return 0;
}
}
} |
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
long long mod=1e9+7;
long long modpascal=14348907;
int ic(char c){
if (c=='B') return 0;
if (c=='R') return 1;
return 2;
}
char ci(int i){
if (i==0) return 'B';
if (i==1) return 'R';
return 'W';
}
long long pp(long long a, long long b){
if (b==0) return 1;
if (b==1) return a;
if (b%2) return (a*pp((a*a)%modpascal,b/2))%modpascal;
return pp((a*a)%modpascal,b/2);
}
long long inv(long long x){
return pp(x,modpascal-2);
}
int ccc[3][3]={{1,0,0},{1,1,0},{1,-1,1}};
int cnk3(int m,int n)
{
if (n==0) return 1;
if (m==n) return 1;
return cnk3(m/3,n/3)*ccc[m%3][n%3];
}
void cmain()
{
int n;cin>>n;
long long res=0;
int mult=-1;
if (n%2) mult=1;
char c;
for (int k=0;k<n;k++){
cin >> c;
//cout << n-1 << " " << k << " " << cnk3(n-1,k)<<endl;
res+=cnk3(n-1,k)*ic(c)*mult;
}
res%=3;
res+=3;
res%=3;
cout << ci(res) << endl;
return;
}
void bmain()
{
int n;cin>>n;
set<long> a;
long ai;
for (int i=0;i<n;i++){
cin>>ai;
a.insert(ai);
}
n=a.size();
vector<long> x={0};
int i;
for (set<long>::iterator it=a.begin();it!=a.end();it++){
x.push_back(*it);
}
long long res=1;
while (x.size()>1){
long long old=x.back();
x.pop_back();
res*=old-x.back()+1;
res%=mod;
}
cout << res << endl;
return;
}
void amain()
{
int a,b;cin>>a>>b;
long cpt=800000;
long s=0;
vector<long> x;
if (a>=b){
while (a){
a--;
s+=cpt;
x.push_back(cpt);
cpt++;
}
while (b>1){
cpt--;
b--;
s-=cpt;
x.push_back(-cpt);
}
x.push_back(-s);
}
else{
while (b){
b--;
s-=cpt;
x.push_back(-cpt);
cpt++;
}
while (a>1){
cpt--;
a--;
s+=cpt;
x.push_back(cpt);
}
x.push_back(-s);
}
for (int i=0;i<x.size();i++){
cout << x[i] << " ";
}
return;
}
int main()
{
ios_base::sync_with_stdio(false), cin.tie(nullptr);
cmain();
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair< ll, ll > Pi;
using vl = vector<ll>;
using vs = vector<string>;
using vvl = vector<vector<ll>>;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep2(i,n) for(int i=1;i<=(n);i++)
#define rep3(i,i0,n) for(int i=i0;i<(n);i++)
#define pb push_back
#define MOD 1000000007
const ll INF = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define mp make_pair
bool compare(Pi a, Pi b) {
if(a.first != b.first){
return a.first < b.first;
}else{
return a.second < b.second;
}
}
bool In_map(ll y,ll x,ll h,ll w){
if(y<0 || x<0 || y>=h || x>=w){
return 0;
}else{
return 1;
}
}
const vector<ll> dx{1,0,-1,0};
const vector<ll> dy{0,1,0,-1};
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
int main() {
ll H,W;
cin >>H>>W;
vector<vector<char>>S(H,vector<char>(W));
ll num=0;
rep(i,H){
rep(j,W){
cin>>S[i][j];
if(S[i][j]=='.'){
num++;
}
}
}
if(num==0){
cout<<0<<endl;
return 0;
}else if(num==1){
cout<<1<<endl;
return 0;
}
//vvl yoko(H,vector<ll>(W));
vvl tate(H,vector<ll>(W));
rep(j,W){
ll cnt=0;
ll now=0;
rep(i,H){
if(S[i][j]=='#'){
rep3(k,now,i){
tate[k][j]=cnt;
}
now=i+1;
cnt=0;
}else{
cnt++;
}
}
rep3(k,now,H){
tate[k][j]=cnt;
}
}
ll Y=0;
vvl yoko(H,vector<ll>(W));
rep(i,H){
ll cnt=0;
ll now=0;
rep(j,W){
if(S[i][j]=='#'){
rep3(k,now,j){
yoko[i][k]=cnt;
}
//Y=(Y+cnt*cnt)%MOD;
now=j+1;
cnt=0;
}else{
cnt++;
}
}
rep3(k,now,W){
yoko[i][k]=cnt;
}
}
/*rep(i,H){
rep(j,W){
cout<<tate[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
rep(i,H){
rep(j,W){
cout<<tate[i][j]<<" ";
}
cout<<endl;
}*/
ll ans=0;
rep(i,H){
rep(j,W){
if(tate[i][j]+yoko[i][j]-1>=0)
ans =(ans+modpow(2,num,MOD)-modpow(2,num-(tate[i][j]+yoko[i][j]-1),MOD)+MOD)%MOD;
}
}
/*cout<<num<<endl;*/
cout<<(ans+MOD)%MOD<<endl;
return 0;
} |
#include <bits/stdc++.h>
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;
template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }
pii irange(const vi64 &a, int x) {
auto w = equal_range(all(a), x);
return {w.fi - a.begin(), w.se - a.begin()};
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.precision(10);
cout << fixed;
#ifdef LOCAL_DEFINE
freopen("input.txt", "rt", stdin);
#endif
int n, l;
cin >> n >> l;
vi64 a = {0}, b = {0};
forn(i, n) {
int x;
cin >> x;
a.pb(x - i - 1);
}
a.pb(l - n);
forn(i, n) {
int x;
cin >> x;
b.pb(x - i - 1);
}
b.pb(l - n);
/* for (int x: a) cerr << x << ' ';
cerr << '\n';
for (int x: b) cerr << x << ' ';
cerr << '\n';*/
bool ok = true;
for (int x: b) {
ok &= *lower_bound(all(a), x) == x;
}
if (!ok) cout << -1 << '\n';
else {
i64 ans = 0;
forn(i, n + 2) {
if (i + 1 < n + 2 && b[i] == b[i + 1]) continue;
auto w1 = irange(a, b[i]);
auto w2 = irange(b, b[i]);
if (w1.fi > w2.fi) ans += w1.fi - w2.fi;
if (w1.se < w2.se) ans += w2.se - w1.se;
}
cout << ans << '\n';
}
#ifdef LOCAL_DEFINE
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define oo 1000000010
const int N = 100010;
int n , l , a[N] , b[N];
map < int , int > mp , mp2;
int in[N] , it , val , g[N];
/*
pair< int , int > seg[4 * N];
void update(int s,int e,int idx){
if(s > r || e < l)
return;
if(s >= l && e <= r){
seg[idx] = make_pair(it , val);
return;
}
update(s , ((s+e) >> 1),(idx << 1));
update(((s+e) >> 1) + 1,e,(idx << 1) + 1);
}
int get(int i){
int s = 0 , e = n - 1 , idx = 1;
pair< int , int > ret = make_pair(-1 , -1);
while(true){
ret = max(ret , seg[idx]);
if(s == e)
break;
if(i <= ((s + e) >> 1)){
idx = (idx << 1);
e = ((s + e) >> 1);
}
else{
idx = (idx << 1) + 1;
s = ((s + e) >> 1) + 1;
}
}
return ret.second;
}
*/
int main(){
scanf("%d%d",&n,&l);
n += 2;
a[0] = b[0] = 0;
a[n - 1] = b[n - 1] = l + 1;
for(int i = 1 ;i < n - 1;i++) scanf("%d",&a[i]);
for(int i = 1 ;i < n - 1;i++) scanf("%d",&b[i]);
for(int i = 0 ;i < n;i++){
if(mp.find(a[i] - i) == mp.end())
mp[a[i] - i] = i;
mp2[a[i] - i] = i;
g[i] = i;
}
int cur , mn , mx;
for(int i = 1 ;i < n - 1;i++){
if(b[i] == a[i]) continue;
if(mp.find(b[i] - i) == mp.end()){puts("-1");return 0;}
mx = mp2[b[i] - i];
mn = mp[b[i] - i];
if(i < mn)
g[i] = mn;
else
g[i] = mx;
}
long long ans = 0;
int j , last;
for(int i = 0 ;i < n;i++){
if(g[i] == i) continue;
if(g[i] < i){
j = i;
while(j < n && g[j] < j)
j++;
j--;
last = j;
for(int k = j ;k >= i;k--){
if(last == g[k]) continue;
ans += k - g[k];
last = g[k];
}
i = j;
continue;
}
last = i;
while(i < n && g[i] > i){
if(last == g[i]){
i++;
continue;
}
ans += g[i] - i;
last = g[i];
i++;
}
i--;
}
cout << ans << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n;
vector<ll> divs;
void divisors(ll n) {
divs.clear();
for (ll i = 1; i * i <= n; ++i) {
if (n % i == 0) {
divs.emplace_back(i);
if (i != n / i) divs.emplace_back(n/i);
}
}
sort(divs.begin(), divs.end());
}
int main() {
ios::sync_with_stdio(!cin.tie(0));
cin >> n;
divisors(n);
for (int i = 0; i < (int) divs.size(); ++i) {
cout << divs[i] << "\n";
}
return 0;
}
| #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define fef(i,a,b) for(ll i=a;i<=b;i++)
#define rer(i,a,b) for(ll i=b;i>=a;i--)
#define wew while(true)
#define FILE_IN "D.inp"
#define FILE_OUT "D.out"
#define ofile freopen(FILE_IN,"r",stdin);freopen(FILE_OUT,"w",stdout)
#define fio ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define MOD (ll(1000000007))
#define INF (ll(1000000007))
#define MAXN 300011
#define x first
#define y second
#define pii pair<int,int>
#define piii pair<int,pii>
#define pb push_back
#define endl "\n"
template<typename T> tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update> order_set;
typedef trie<string,null_type,trie_string_access_traits<>,pat_trie_tag,trie_prefix_search_node_update> order_trie;
typedef long long ll;
class DisjointSet{ public:
vector<int> parent;
DisjointSet(int n): parent(n) { for(int i=0; i<n; i++) parent[i] = i; }
void join(int a, int b) { parent[find(b)] = find(a); }
int find(int a){ return a == parent[a] ? a : parent[a] = find(parent[a]); }
bool check(int a, int b){ return find(a) == find(b); }
};
//---------END-------//
template<typename T>
void read(T &X){
X=0;
char c;
int sign=1;
c=getchar();
if (c=='-') sign=-1;else X=X*10+c-'0';
while ((c=getchar()) && ('0'<=c) && (c<='9'))
X=X*10+c-'0';
}
ll N,x,M,H,T;
vector<ll> A;
int main(){
//ofile;
cin >> N;
for (ll i=1;i*i<=N;i++) if (N%i==0){
A.pb(i);
if (N/i!=i) A.pb(N/i);
}
sort(A.begin(),A.end());
for (ll i:A) cout << i << endl;
}
|
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); i++)
#define reps(i,s,n) for(int i=(s); i<(n); i++)
#define all(v) v.begin(),v.end()
#define outve(v) for(auto i : v) cout << i << " ";cout << endl
#define outmat(v) for(auto i : v){for(auto j : i) cout << j << " ";cout << endl;}
#define in(n,v) for(int i=0; i<(n); i++){cin >> v[i];}
#define out(n) cout << (n) << endl
#define fi first
#define se second
#define pb push_back
#define si(v) int(v.size())
#define len(v) int(v.length())
#define lob(v,n) lower_bound(all(v),n)
#define lobi(v,n) lower_bound(all(v),n) - v.begin()
#define upb(v,n) upper_bound(all(v),n)
#define upbi(v,n) upper_bound(all(v),n) - v.begin()
#define mod 1000000007
#define infi 1010000000
#define infl 1100000000000000000
#define cyes cout << "Yes" << endl
#define cno cout << "No" << endl
#define csp << " " <<
#define outset(n) cout << fixed << setprecision(n);
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vd = vector<double>;
using vvd = vector<vector<double>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
template<typename T> using ve = vector<T>;
template<typename T> using pq2 = priority_queue<T>;
template<typename T> using pq1 = priority_queue<T,vector<T>,greater<T>>;
template<typename T> bool chmax(T &a, T b) {if(a < b) {a = b;return 1;}return 0;}
template<typename T> bool chmin(T &a, T b) {if(a > b) {a = b;return 1;}return 0;}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int H,W;
cin >> H >> W;
int x,y;
cin >> x >> y;
ve<string> s(H);
in(H,s);
x--,y--;
int ans = 0;
for (int i = x+1; i < H; i++) {
if(s[i][y] == '.')ans++;
else break;
}
for (int i = x-1; i >= 0; i--) {
if(s[i][y] == '.')ans++;
else break;
}
for (int i = y+1; i < W; i++) {
if(s[x][i] == '.')ans++;
else break;
}
for (int i = y-1; i >= 0; i--) {
if(s[x][i] == '.')ans++;
else break;
}
cout << ans+1 << endl;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
int n,m,x,y;
char a[105][105];
int ans;
signed main()
{
cin>>n>>m>>x>>y;
for(int i=1;i<=n;++i)
{
for(int j=1;j<=m;++j)
{
cin>>a[i][j];
}
}
if(a[x][y]=='#'){
cout<<0<<'\n';
return 0;
}
ans=1;
for(int i=1;i<=n;++i)
{
if(x+i>n || a[x+i][y]=='#') break;
ans++;
}
for(int i=1;i<=n;++i)
{
if(x-i<=0 || a[x-i][y]=='#') break;
ans++;
}
for(int i=1;i<=m;++i)
{
if(y+i>m || a[x][y+i]=='#') break;
ans++;
}
for(int i=1;i<=m;++i)
{
if(y-i<=0 || a[x][y-i]=='#') break;
ans++;
}
cout<<ans<<'\n';
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
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; }
const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 200005;
const int MAXM = 1000005;
int b[MAXN];
struct Node {
long double x;
int id;
Node(){}
Node(long double x, int id):x(x), id(id){}
bool operator < (const Node &B) const {
return x > B.x;
}
}a[MAXN];
priority_queue<pair<double, ll> > Q;
int main(){
int K;
int N, M;
cin >> K >> N >> M;
int S = M;
for(int i = 0; i < K; i++) {
int x;
cin >> x;
long double t = (long double)M*(long double)x/(long double)N;
b[i] = t;
S -= b[i];
a[i].x = t - (long double)b[i];
a[i].id = i;
}
sort(a, a + K);
for(int i = 0; i< S; i++) {
b[a[i].id]++;
}
for(int i = 0; i< K; i++) cout << b[i] << " ";
cout << endl;
return 0;
}
| #include <bits/stdc++.h>
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
using namespace std;
//#define int long long
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
typedef vector<vector<int>> graph;
#define pu push
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define MOD 1000000007
#define MAXX 1.1529215e+18
#define _1 first
#define _2 second
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
#define all(x) x.begin(),x.end()
#define si(x) int(x.size())
template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}
template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}
template<class t> using vc=vector<t>;
template<class T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); }
template<class T>
T extgcd(T a, T b, T &x, T &y) {
T d = a;
if (b == 0) {
x = 1;
y = 0;
} else {
d = extgcd(b, a%b, y, x);
y -= (a/b)*x;
}
return d;
}
template<class t,class u>
ostream& operator<<(ostream& os,const pair<t,u>& p){
return os<<"{"<<p._1<<","<<p._2<<"}";
}
template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
os<<"{";
for(auto e:v)os<<e<<",";
return os<<"}";
}
template<class T>
void g(T &a){
cin >> a;
}
template<class T>
void o(const T &a,bool space=false){
cout << a << (space?' ':'\n');
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int K;
ll N, M;
cin >> K >> N >> M;
vector<ll> A(K);
rep(i, K) {
cin >> A[i];
}
ll l = 0;
ll r = 2e9;
while(r-l>1) {
ll m = (l+r)/2;
bool flag = true;
ll sum1 = 0;
ll sum2 = 0;
rep(i, K) {
ll t1 = (M*A[i]+m)/N;
ll t2 = (M*A[i]-m+N-1)/N;
sum1 += t1;
sum2 += t2;
if (t2 > t1) {
flag = false;
}
}
if (!(sum1 >= M && sum2 <= M))
flag = false;
if (flag) {
r = m;
} else {
l = m;
}
// cout << m << endl;
}
// cout << r << ' ' << l << endl;
vector<ll> v1(K);
vector<ll> v2(K);
int cnt = 0;
rep(i, K) {
v1[i] = (M*A[i]-r+N-1)/N;
v2[i] = (M*A[i]+r)/N-(M*A[i]-r+N-1)/N;
cnt += v1[i];
}
// cout << v1 << endl;
// cout << v2 << endl;
ll res = M-cnt;
rep(i, K) {
if (res < v2[i]) {
v1[i] += res;
res = 0;
} else {
v1[i]+= v2[i];
res -= v2[i];
}
}
// if (res > 0) {
// rep(i, K) {
// if (i < res % K) {
// v1[i] += res/K+1;
// } else {
// v1[i] += res/K;
// }
// }
// }
rep(i, K) {
cout << v1[i] << ' ';
}
cout << endl;
}
|
#include<bits/stdc++.h>
using namespace std;
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define int long long
#define pb push_back
#define mp make_pair
#define for0(i,n) for(int i=0;i<n;i++)
#define for1(i,n) for(int i=1;i<=n;i++)
#define w(x) int x;cin>>x;while(x--)
#define pii pair<int,int>
#define vi vector<int>
#define vpii vector<pair<int,int>>
#define vvi vector<vector<int>>
#define precision(x,k) fixed<<setprecision(k)<<x
#define inf 1e16
int eps = 1e-9;
int arr[300005];
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
//cout << "YES" << endl;
double k, n, m;
cin >> k >> n >> m;
double x = m / n;
//cout<<x<<endl;
vector<int>ans;
vector<pair<double, int>>temp;
for0(i, k)
{
cin >> arr[i];
}
int sum = 0;
for0(i, k)
{
double cc = x * arr[i];
int jj = cc;
double rem = cc - jj;
// cout<<jj<<" "<<cc<<" "<<arr[i]<<endl;
ans.push_back(jj);
sum += jj;
temp.push_back({rem, i});
}
sort(temp.begin(), temp.end());
int remaning = m - sum;
//cout<<remaning<<endl;
int kkk = 0;
for(int i=k-1;i>=0;i--)
{
if (kkk == remaning)
break;
pair<int, int>pp = temp[i];
// cout<<pp.second<<endl;
ans[pp.second]++;
kkk++;
}
for0(i, k)
cout << ans[i] << " ";
cout << endl;
return 0;
}
| #include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<iomanip>
#include<tuple>
#include<cstring>
#include<bitset>
#define REP(i,n) for(ll i=0;i<(ll)n;i++)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((ll)(x).size())
#define pb push_back
#define eb emplace_back
#define INF 1000000000
#define INFLL 1000000000000000000LL
using namespace std;
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
using ll = long long;
using PP = pair<ll, ll>;
#define MAX_N 200000+100
ll N, K, now, ans;
PP X[MAX_N];
int main() {
cin >> N >> K;
REP(i, N)cin >> X[i].first >> X[i].second;
X[N].first = INFLL * 2;
sort(X, X + N + 1);
REP(i, N+1) {
if (X[i].first <= now + K) {
K -= X[i].first - now;
K += X[i].second;
now = X[i].first;
}
else {
break;
}
}
cout << now + K << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define mems(p) memset(p,-1,sizeof(p))
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define nl "\n"
const int mxN = 1e5;
const ll MOD = 1e9 + 7;
void fast()
{ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
ll gcd(ll a, ll b) {
return b ? gcd(b, a%b) : a;
}
vector<ll> factorial;
ll expo(ll x, ll exp) {
if (exp <= 0) return 1;
ll tmp = (x*x)% MOD;
if (exp%2) {
return (expo(tmp, exp/2) * x) % MOD;
}
return (expo(tmp, exp/2)) % MOD;
}
ll choice(ll n, ll r) {
if (n < r) return 0;
ll numer = factorial[n];
ll denom = (factorial[r] * factorial[n-r]) % MOD;
return numer * expo(denom, MOD - 2) % MOD;
}
int main() {
fast();
ll n;
cin>>n;
ll ans = 0;
vector<ll> fromdown, fromup;
for (ll i = 1; i * i <= n; ++i)
{
if (n%i == 0) {
fromdown.pb(i);
if (n/i != i) fromup.pb(n/i);
}
}
for (ll x : fromdown) {
cout<<x<<nl;
}
for (int i = (int)fromup.size() - 1; i >= 0; --i) {
cout<<fromup[i]<<nl;
}
} | #define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
typedef long long ll; typedef long double ld; typedef vector <int> vi; typedef vector <ll> vll; typedef vector <string> vS; typedef vector <vector <int>> vv; typedef map<int, int> mi; typedef map<string, int> ms; typedef map<char, int> mc; typedef string S; typedef map <ll, ll> ml; typedef map <int, bool> mb; typedef vector <ld> vld; typedef vector <bool> vb; typedef map<ll, ll> mll;
#define FOR(i,N) for(int i = 0 ; i < N;i++)
#define eFOR(i,a,b) for(int i = a; i <=b;i++)
#define dFOR(i,N) for(int i = N - 1; i>=0;i--)
#define edFOR(i,a,b) for(int i = b ; i >=a;i--)
#define all(x) x.begin(),x.end()
#define SORT(x) sort(all(x))
#define RSORT(x) sort(x.rbegin(),x.rend())
#define mine(x) min_element(all(x))
#define maxe(x) max_element(all(x))
#define pb push_back
#define PI 3.14159265359
const int mod = 1e9 + 7;
int CASE = 1;
bool prime(ll n)
{
if (n <= 1)
return false;
for (int i = 2; i <= sqrt(n); i++)
if (!(n % i))
return false;
return true;
}
void solve()
{
ll n;
cin >> n;
if (n == 1)
{
cout << 1;
return;
}
if (prime(n))
{
cout << 1 << endl << n;
return;
}
vll ans = { 1 , n };
bool check = false;
if (int(sqrt(n)) * sqrt(n) == n)
ans.pb(sqrt(n)), check = true;;
eFOR(i, 2, sqrt(n - check))
{
if (n % i == 0)
ans.pb(i), ans.pb(n / i);
}
SORT(ans);
FOR(i, ans.size())
cout << ans[i] << endl;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int T = 1;
while (T--)
{
solve();
}
} |
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
typedef pair<string,int> psi;
typedef pair<int,string> pis;
typedef array<int,2> aii;
typedef array<int,3> aiii;
typedef array<int,4> aiiii;
typedef unsigned long long ull;
typedef long long int ll;
typedef array<ll,2> all;
typedef array<ll,3> alll;
#define pb push_back
#define ff first
#define ss second
#define MAX 1000005
#define MOD 1000000007
#define INF 1e9+100
vector<int>adj[MAX];
int xMove[] = { 0,1,0,-1,1,1,-1,-1, 2, 2, -2, -2, 1, 1, -1, -1 };
int yMove[] = { 1,0,-1,0,1,-1,1,-1, 1, -1, 1, -1, 2, -2, 2, -2 };
string arr[65];
ll dp[65][2],n;
ll solve(int i, int p)
{
if(i==n)
return p==1;
if(dp[i][p]!=-1)
return dp[i][p];
ll ans=0;
ans+=solve(i+1,arr[i]=="AND"?p&1:p|1);
ans+=solve(i+1,arr[i]=="AND"?p&0:p|0);
return dp[i][p]=ans;
}
int main()
{
//freopen("joseph.in","r",stdin);
//freopen("output.txt","w",stdout);
cin.tie(0),cout.tie(0);
ios_base::sync_with_stdio(0);
cout.setf(ios::fixed);
cout.precision(10);
int TC=1;
int m,k,q;
//cin>>TC;
for(int t1=1; t1<=TC; t1++)
{
cin>>n;
for(int i=0; i<n; i++)
cin>>arr[i];
memset(dp,-1,sizeof dp);
cout<<solve(0,1)+solve(0,0)<<endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
#define all(x) (x).begin(),(x).end()
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; }
typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vec;
using Graph = vector<vector<int>>;
const long long INF = 1LL<<60;
const int MOD = 1000000007;
struct SegmentTree {
private:
int n;
vector<int> node;
public:
// 元配列 v をセグメント木で表現する
SegmentTree(vector<int> v) {
// 最下段のノード数は元配列のサイズ以上になる最小の 2 冪 -> これを n とおく
// セグメント木全体で必要なノード数は 2n-1 個である
int sz = v.size();
n = 1; while (n < sz) n *= 2;
node.resize(2 * n - 1, -MOD);
// 最下段に値を入れたあとに、下の段から順番に値を入れる
// 値を入れるには、自分の子の 2 値を参照すれば良い
for (int i = 0; i < sz; i++) node[i + n - 1] = v[i];
for (int i = n - 2; i >= 0; i--) node[i] = max(node[2 * i + 1], node[2 * i + 2]);
}
void update(int x, int val) {
// 最下段のノードにアクセスする
x += (n - 1);
// 最下段のノードを更新したら、あとは親に上って更新していく
node[x] = val;
while (x > 0) {
x = (x - 1) / 2;
node[x] = max(node[2 * x + 1], node[2 * x + 2]);
}
}
// 要求区間 [a, b) 中の要素の最小値を答える
// k := 自分がいるノードのインデックス
// 対象区間は [l, r) にあたる
int getmin(int a, int b, int k = 0, int l = 0, int r = -1) {
// 最初に呼び出されたときの対象区間は [0, n)
if (r < 0) r = n;
// 要求区間と対象区間が交わらない -> 適当に返す
if (r <= a || b <= l) return -MOD;
// 要求区間が対象区間を完全に被覆 -> 対象区間を答えの計算に使う
if (a <= l && r <= b) return node[k];
// 要求区間が対象区間の一部を被覆 -> 子について探索を行う
// 左側の子を vl ・ 右側の子を vr としている
// 新しい対象区間は、現在の対象区間を半分に割ったもの
int vl = getmin(a, b, 2 * k + 1, l, (l + r) / 2);
int vr = getmin(a, b, 2 * k + 2, (l + r) / 2, r);
return max(vl, vr);
}
};
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
ll n;cin>>n;
vec a(n);
rep(i,n){
string s;
cin>>s;
if(s=="AND")a[i]=0;
else a[i]=1;
}
vector<vector<ll>> dp(n+1,vector<ll>(2));
dp[0][0]=1;dp[0][1]=1;
rep(i,n){
if(a[i]==0){
dp[i+1][1]=dp[i][1];
dp[i+1][0]=dp[i][1]+2*dp[i][0];
}
else{
dp[i+1][1]=dp[i][0]+dp[i][1]*2;
dp[i+1][0]=dp[i][0];
}
}
cout<<dp[n][1]<<endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
long long a[2000005],s[2000005],mmax[2000005];
int main()
{
int n;
cin>>n;
long long sum=0;
for (int i=1;i<=n;i++){
cin>>a[i];
sum-=1ll*(i-1)*mmax[i-1];
mmax[i]=max(mmax[i-1],a[i]);
s[i]=s[i-1]+a[i];
sum+=i*mmax[i];
sum+=s[i];
cout<<sum<<endl;
}
return 0;
}
| //#include <bits/stdc++.h>
//using namespace std;
#include <cstdio>
#define rep(i,_l,_r) for(signed i=(_l),_end=(_r);i<=_end;++i)
#define fep(i,_l,_r) for(signed i=(_l),_end=(_r);i>=_end;--i)
#define erep(i,u) for(signed i=head[u],v=to[i];i;i=nxt[i],v=to[i])
#define efep(i,u) for(signed i=Head[u],v=to[i];i;i=nxt[i],v=to[i])
#define print(x,y) write(x),putchar(y)
//#define debug(...) do {cerr<<__LINE__<<" : ("#__VA_ARGS__<<") = "; Out(__VA_ARGS__); cerr<<flush;} while(0)
//template <typename T> void Out(T x) {cerr<<x<<"\n";}
//template <typename T,typename ...I> void Out(T x,I ...NEXT) {cerr<<x<<", "; Out(NEXT...);}
template <class T> inline T read(const T sample) {
T x=0; int f=1; char s;
while((s=getchar())>'9'||s<'0') if(s=='-') f=-1;
while(s>='0'&&s<='9') x=(x<<1)+(x<<3)+(s^48),s=getchar();
return x*f;
}
template <class T> inline void write(const T x) {
if(x<0) return (void) (putchar('-'),write(-x));
if(x>9) write(x/10);
putchar(x%10^48);
}
template <class T> inline T Max(const T x,const T y) {if(x>y) return x; return y;}
template <class T> inline T Min(const T x,const T y) {if(x<y) return x; return y;}
template <class T> inline T fab(const T x) {return x>0?x:-x;}
template <class T> inline T gcd(const T x,const T y) {return y?gcd(y,x%y):x;}
template <class T> inline T lcm(const T x,const T y) {return x/gcd(x,y)*y;}
template <class T> inline T Swap(T &x,T &y) {x^=y^=x^=y;}
typedef long long ll;
const int maxn=2e5+5;
int n,a[maxn];
ll mx,sum,ans;
int main() {
n=read(9);
rep(i,1,n) a[i]=read(9);
rep(i,1,n) {
sum+=a[i],ans+=sum;
mx=Max(mx,0ll+a[i]);
print(1ll*mx*i+ans,'\n');
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for(int i=a;i<n;i++)
//#define per(i,a,n) for (int i=n-1;i>=a;i--)
// #define repl(i,a,n) for(long long int i=a;i<n;i++)
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
typedef vector<int> VI;
typedef long long int ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}());
ll MOD=9;
ll mod=9;
//const llint MOD;
const ll N=(ll)(1e6+1);
const ll mx=1e7;
const int inf=10000;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
ll add(ll a,ll b){return ((a%MOD)+(b%MOD))%MOD;}
ll mul(ll a,ll b){return ((a%MOD)*(b%MOD))%MOD;}
ll sub(ll a,ll b){return ((a%MOD)-(b%MOD)+MOD)%MOD;}
ll binpow(ll x, ll y){ll z = 1;while(y){if(y & 1) z = mul(z, x);x = mul(x, x);y >>= 1;}return z;}
ll inv(int x){return binpow(x, MOD - 2);}
ll divide(ll x, ll y){return mul(x, inv(y));}
ll fact[N];
void precalc(){fact[0] = 1;for(ll i = 1; i < N; i++)fact[i] = mul(fact[i - 1], i);}
ll C(ll n, ll k){return divide(fact[n], mul(fact[k], fact[n - k]));}
bool prime[mx];
void sieve() {for(int i=2;i<mx;i++) {prime[i]=true;}for (int p=2; p*p<=N; p++){if (prime[p] == true){for (int i=p*p; i<=N; i += p)prime[i] = false;}}}
vector<ll> primes;
void primecalc(){for(int i=2;i<=mx;i++){
if(prime[i])
{
primes.pb(i);
}
if (primes.size()==(int)5e5){break;}
}}
ll legendpow(ll n, ll p){ll total = 0;while (n){n /= p;total += n;}return total;}
ll phi(ll n) {
ll result = n;
for (ll i = 2; i * i <= n; i++) {
if (n % i == 0) {
while (n % i == 0)
n /= i;
result -= result / i;
}
}
if (n > 1)
result -= result / n;
return result;
}
vector <pair<ll,ll>> prifac;
void primefac(ll n){int state=0;while (n % 2 == 0){n = n/2;state++;} if (state) prifac.pb({2,state});for (ll i = 3; i <= sqrt(n); i = i + 2) { state=0;while (n % i == 0){state++;n = n/i;} if (state) prifac.pb({i,state});} if (n > 2) prifac.pb({n,1});}
//head==================================================================================
// in greedy if comparison see weight or diff
int main(){
ll a,b,c;
cin>>a>>b>>c;
primefac(a);
ll ans=1;
ll h=phi(10);
mod=h;
MOD=h;
ll op=powmod(b,c);
for ( auto x : prifac)
{
if (x.first==2){
ll k=5;
ll io=phi(k);
ll nn=1;
mod=io;
MOD=io;
ll exp=powmod(b,c);
exp=mul(exp,x.second);
exp=exp+io-1;
mod=phi(10);
mod=10;
MOD=10;
ll tu=powmod(x.first,exp);
tu=mul(tu,2);
ans=mul(ans,tu);
}
else if (x.first==5){
ll k=2;
ll io=phi(k);
ll nn=1;
mod=io;
MOD=io;
ll exp=powmod(b,c);
exp=mul(exp,x.second);
exp=exp+io-1;
mod=phi(10);
mod=10;
MOD=10;
ll tu=powmod(x.first,exp);
tu=mul(tu,5);
ans=mul(ans,tu);
}
else
{
mod=phi(10);
MOD=phi(10);
ll tu=mul(x.second,op);
mod=10;
MOD=10;
tu=powmod(x.first,tu);
ans=mul(ans,tu);
} }
cout<<ans;
}
| //g++ t.cpp -o t && t < p.txt
//d>p.txt&&t<p.txt&&t2<p.txt
#include <iostream> //
#include <vector> //
#include <algorithm> //
#include <queue> //
#include <string> //
#include <functional> //
#include <set> //
#include <unordered_map> //
#include <random> //
#include <ctime> //
#include <bitset> //
#include <cassert>
// #include <atcoder/all>
// using namespace atcoder;
// g++ t.cpp -o t -I .
#define DB cerr<<"D"<<endl
struct _zo{_zo(){std::cin.tie(nullptr);std::cout.tie(nullptr);std::ios::sync_with_stdio(false);}}__zo;
using namespace std; using ll=long long; using ld=long double; const int INF=1e9; const ll LINF=1e18; const double dINF = 1e18; const ld ldINF = 1e18; const double EPS = 1e-6;
using P=pair<ll,ll>;
using Q=pair<pair<ll,ll>,ll>;
const ll M = 998244353;
ll mod_pow(ll x, ll a) { ll an = 1; while(a > 0) { if (a&1) an = an * x % M; x = x * x % M; a >>= 1;} return an;}
ll mod_pow(ll x, ll a, ll m) { ll an = 1; while(a > 0) { if (a&1) an = an * x % m; x = x * x % m; a >>= 1;} return an;}
void add(ll& x, ll y) {x+=y; x%=M;}; void mul(ll& x, ll y) {x*=y; x%=M;}; template<typename T, typename U> void chmax(T& x, U y) {if (x<y) x=y;}; template<typename T, typename U> void chmin(T& x, U y) {if (x>y) x=y;} bool vaild(int x, int y, int hh, int ww){return 0<=x&&x<hh&&0<=y&&y<ww;}
ll gcd(ll a, ll b) {if (b==0) return a; else return gcd(b, a%b);}
int keta(ll a) {int res=0; while(a>0) res+=a%10, a/=10; return res;}
const int up[]={1,-1,0,0}, lf[]={0,0,1,-1};
int main() {
ll a,b,c;cin>>a>>b>>c;
a %= 10;
vector<ll> p[10];
p[0] = vector<ll>({0});
p[1] = vector<ll>({1});
p[2] = vector<ll>({2,4,8,6});
p[3] = vector<ll>({3,9,7,1});//3,9,27,81
p[4] = vector<ll>({4,6});//4,16,64
p[5] = vector<ll>({5});//5,25
p[6] = vector<ll>({6});//6,36
p[7] = vector<ll>({7,9,3,1});//7,49,3,1,7
p[8] = vector<ll>({8,4,2,6});//8,64,512,6,8
p[9] = vector<ll>({9,1});//9,81,729,
/*
if (p[a].size() == 1) {
cout<<a<<endl;
} else if (p[a].size() == 2) {
if (b%2==0) cout<<p[a][1]<<endl;
else cout<<a<<endl;
} else {
// b^c mod 4 = ?
}*/
ll dv = p[a].size();
ll id = mod_pow(b%dv,c,dv);
//cout<<dv<<" "<<id<<endl;
id=(id-1+dv)%dv;
cout<<p[a][id]<<endl;
}
//
// ・配列の大きさok? ・priority_queueはgreater? ・debug消した?
// ・落ち着いて。提出まで4分待ってね……WJ……1/10……2/10……
// Thank you for making problems and running the contest
|
#include <bits/stdc++.h>
#define INF 1000000007
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i,a,b) for (int i = a; i < b; i++)
using namespace std;
typedef long long ll;
typedef long long int lli;
typedef long double ld;
typedef vector<int> vi;
typedef unordered_map<int,int> umi ;
typedef unordered_set<int> usi ;
typedef pair<int,int> pi;
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
//declare here -------------------------------------------------------------------------------
const int N =1e5+5 ;
bool ing(ld val){
return ceil(val)==floor(val) ;
}
// solve here ---------------------------------------------------------------------------------
void solve(){
ld s,p ;
cin>>s>>p ;
ld D = s*s - (4*p) ;
ld b= -s ;
ld a= 1 ;
if( D <0 ){
cout<<"No" ;
}
else{
ld val= sqrt(D) ;
debug(D,val) ;
ld x = (-b + val) /(2*a) ;
ld y= (-b -val) /(2*a) ;
debug(x,y) ;
if(ing(x) && ing(y) && x>0 && y>0) cout<<"Yes" ;
else cout<<"No" ;
}
}
//--------------------------------------------------------------------------------------------
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
long long test =1 ;
//cin>>test ;
while(test--){
solve() ;
cout<<"\n" ;
}
return 0 ;
} | #include<bits/stdc++.h>
using namespace std;
int main()
{
long long int i,s,p,j;
cin>>s>>p;
for(i=1;;i++){
if(i*(s-i)==p){
cout<<"Yes\n";
return 0;
}
if(i*(s-i)>p){break;}
}
cout<<"No\n";
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e5 + 10;
int n, m;
int t[N];
void add(int x, int v) {
for(int i = x; i <= n; i += i & (-i)) t[i] ^= v;
}
int cal(int x) {
int res = 0;
for(int i = x; i > 0; i -= i & (-i)) res ^= t[i];
return res;
}
int main() {
scanf("%d%d", &n, &m);
int opt, x, y;
for(int i = 1; i <= n; i++) {
scanf("%d", &x);
add(i, x);
}
for(int i = 1; i <= m; i++) {
scanf("%d%d%d", &opt, &x, &y);
if(opt == 1) add(x, y);
else printf("%d\n", cal(y) ^ cal(x - 1));
}
return 0;
}
| #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<math.h>
#include<unordered_set>
#include<unordered_map>
#include<cassert>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
#define mp make_pair
#define pb push_back
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define rrep(i,l,r) for(int i=l;i<=r;i++)
#define chmin(a,b) a=min(a,b)
#define chmax(a,b) a=max(a,b)
#define all(x) x.begin(),x.end()
#define unq(x) sort(all(x));x.erase(unique(all(x)),x.end())
#define mod 1000000007
//#define mod 998244353
#define ad(a,b) a=(a+b)%mod;
#define mul(a,b) a=a*b%mod;
void readv(vector<ll> &a,ll n){
rep(i,n){
ll x;
cin>>x;
a.push_back(x);
}
}
void outv(vector<ll> &a,ll n){
rep(i,n){
if(i>0)cout<<" ";
cout<<a[i];
}
cout<<"\n";
}
ll po(ll x,ll y){
ll res=1;
for(;y;y>>=1){
if(y&1)res=res*x%mod;
x=x*x%mod;
}
return res;
}
ll gcd(ll a,ll b){
return (b?gcd(b,a%b):a);
}
#define FACMAX 200010
ll fac[FACMAX],inv[FACMAX],ivf[FACMAX];
void initfac(){
fac[0]=ivf[0]=inv[1]=1;
for(ll i=1;i<FACMAX;i++)fac[i]=fac[i-1]*i%mod;
for(ll i=1;i<FACMAX;i++){
if(i>1)inv[i]=(mod-mod/i*inv[mod%i]%mod)%mod;
ivf[i]=po(fac[i],mod-2);
}
}
ll P(ll n,ll k){
if(n<0||n<k)return 0;
return fac[n]*ivf[n-k]%mod;
}
ll C(ll n,ll k){
if(k<0||n<k)return 0;
return fac[n]*ivf[n-k]%mod*ivf[k]%mod;
}
ll H(ll n,ll k){
return C(n+k-1,k);
}
ll n,m,a[10010],b[10010],c[110];
vector<ll> eid[110];
ll ans[10010];
bool ans_ok[10010];
vector<ll> g[110];
ll p[110],d[110];
bool vis[110];
void dfs(ll x,ll from,ll dep){
if(vis[x])return;
vis[x]=1;
d[x]=dep;
p[x]=from;
for(auto y:g[x])if(y!=from){
dfs(y,x,dep+1);
}
}
vector<ll> to[110];
ll dfs2(ll x){
if(vis[x])return 0;
ll res=1;
vis[x]=1;
for(auto y:to[x])res+=dfs2(y);
return res;
}
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
cin>>n>>m;
rep(i,m){
cin>>a[i]>>b[i];
a[i]--,b[i]--;
}
rep(i,n)cin>>c[i];
rep(i,m){
if(c[a[i]]==c[b[i]]){
eid[c[a[i]]].pb(i);
}
else{
if(c[a[i]]>c[b[i]])ans[i]=0;
else ans[i]=1;
}
ans_ok[i]=0;
}
for(int l=1;l<=n;l++){
rep(i,n)g[i].clear();
for(auto i:eid[l]){
g[a[i]].pb(b[i]);
g[b[i]].pb(a[i]);
}
for(int s=0;s<n;s++){
rep(i,n)vis[i]=0,p[i]=-1,d[i]=-1;
dfs(s,-1,0);
vector<ll> comp;
rep(i,n)if(vis[i])comp.pb(i);
vector<ll> es;
for(auto i:eid[l]){
if(vis[a[i]]==1&&vis[b[i]]==1)es.pb(i);
}
rep(i,n)to[i].clear();
for(auto i:es){
if(ans_ok[i])continue;
if(p[a[i]]==b[i]){
to[b[i]].pb(a[i]);
ans[i]=1;
}
else if(p[b[i]]==a[i]){
to[a[i]].pb(b[i]);
ans[i]=0;
}
else if(d[a[i]]<d[b[i]]){
to[b[i]].pb(a[i]);
ans[i]=1;
}
else{
to[a[i]].pb(b[i]);
ans[i]=0;
}
}
bool renketsu=1;
for(auto t:comp){
for(auto i:comp)vis[i]=0;
renketsu&=(dfs2(t)==(ll)comp.size());
}
if(renketsu){
for(auto i:es)ans_ok[i]=1;
}
}
}
rep(i,m){
cout<<(ans[i]==0?"->":"<-")<<"\n";
}
}
|
#include<iostream>
#include<string>
#include<vector>
#include<numeric>
#include<algorithm>
#include<cmath>
#include <iomanip>
using namespace std;
template<class T>
istream& operator >>(istream& is, vector<T>& v){
for(auto&& elem:v)cin >> elem;
return is;
}
template<class T>
ostream& operator <<(ostream& os, const vector<T>& v){
for(auto&& elem: v)os << elem << ", ";
return os;
}
int main(){
int n;
cin >> n;
vector<long long>x(n);
cin >> x;
long long m = 0;
long long e = 0.0;
long long max = 0;
for(auto&& elem:x){
m += abs(elem);
e += abs(elem)* abs(elem);
if(abs(elem) > max) max = abs(elem);
}
cout<< m << endl;
cout<< fixed << setprecision(15) << sqrt(static_cast<double>(e)) <<endl;
cout<< max << endl;
fflush(stdout);
return 0;
} | /** (buri nazar wale tera muh kala 😑)
*
* @Author : rudraksh
* @Created : Saturday, January 16th 2021, 6:13:01 pm
* @Email : [email protected]
*
* “My code works? - don't ask me how”
*/
#include <bits/stdc++.h>
#include <unistd.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;
using namespace __gnu_pbds;
typedef long long int ll;
typedef double db;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int,int> PI;
typedef pair<ll,ll> PL;
typedef map<int,int> MI;
typedef map<ll,ll> ML;
#define ar array
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define eb emplace_back
#define F first
#define S second
#define repa(i,a,n) for(ll i=a;i<n;i++)
#define rep(i,n) for(ll i=0;i<n;i++)
#define pera(i,a,n) for(ll i=a;i>=n;i--)
#define per(i,n) for(ll i=n;i>=0;i--)
#define all(x) (x).begin(),(x).end()
#define loop(it,x) for(auto it=x.begin();it!=x.end();it++)
#define MS0(x) memset((x), 0, sizeof((x)))
#define MS1(x) memset((x), -1, sizeof((x)))
#define CP(dst,src) memcpy(dst, src, sizeof(src))
#define SZ(x) ((ll)(x).size())
#define watch(x) cout << (#x) << " is " << (x) << endl
#define TIME cerr << "\nTime elapsed: " << setprecision(5) << 1000.0*clock()/CLOCKS_PER_SEC << "ms\n";
const ll mod = 1e9+7;
const double pi = acos(-1);
template<typename T> using minpq = priority_queue<T, vector<T>, greater<T>>;
// oset.insert({x, id++}); for multiset. Better than less_equal<T>,erase won't work & lb work as ub and vice versa
template<typename T, typename Comp = less<T> >
using oset = tree<T, null_type, Comp, rb_tree_tag, tree_order_statistics_node_update>;
//find_by_order(k) --> value at kth index (iterator)
//order_of_key(k) --> no of elements strictly less than k (0 based index)
template<typename T, typename T1, typename Comp = less<T> >
using omap = tree<T, T1, Comp, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T>ostream& operator<<(ostream& out,vector<T>& v){for(auto& x: v)out<<x<<' '; return out;}
template<typename T>istream& operator>>(istream& in,vector<T>& v){for(auto& x: v)cin>>x; return in;}
template<typename T,typename T1>bool amax(T &a,T1 b){if(b>a){a=b; return 1;} return 0;}
template<typename T,typename T1>bool amin(T &a,T1 b){if(b<a){a=b; return 1;} return 0;}
void print() { cout<<'\n'; }
// void print() { cout<<endl; }
template <typename T, typename... Params>
void print(T var1, Params... var2) {cout<<var1<<' '; print(var2...);}
ll ceil_div(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b != 0); }
ll power(ll a, ll b)
{
ll res = 1;
a %= mod;
while(b > 0)
{
if(b&1) res = (res*a) % mod;
b >>= 1;
a = (a*a) % mod;
}
return res;
}
/************* I don't know what i'm doing ****************/
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n, box;
cin >> n >> box;
MI m;
rep(i, n)
{
int x;
cin >> x;
m[x]++;
}
int maxi = box;
int ans = 0;
rep(num, n)
{
amin(maxi, m[num]);
ans += maxi;
// print(num, maxi);
if(maxi == 0) break;
}
print(ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,l,r) for(size_t i=(l); i<(r); i++)
#define REP(i,n) FOR(i,0,n)
#define endl "\n"
#define debug(x) cout<<x<<endl;
typedef long long ll;
static const ll INF = 1ll<<60;
static const double EPS = 10e-12;
int main(){
int N;
ll X;
cin>>N>>X;
vector<ll> V(N),P(N);
REP(i,N) cin>>V[i]>>P[i];
ll sum = 0;
int ans = -1;
X *= 100;
REP(i,N){
sum += V[i] * P[i];
if(sum > X){
ans = i+1;
break;
}
}
cout<<ans<<endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(i=0;i<n;i++)
#define Rep(i,x,n) for(i=x;i<=n;i++)
#define foreach(c,itr) for(__typeof(c)::iterator itr=c.begin();itr!=c.end();itr++)
#define all(v) v.begin(),v.end()
#define p_b push_back
#define fr first
#define sc second
#define m_p make_pair
#define setp setprecision
typedef long long ll;
typedef long double ld;
const ll INF=0x3f3f3f3f;
ll gcd(ll x,ll y) {return x%y==0 ? y:gcd(y,x%y);}
int i,j;
int n,x;
int v[1005],p[1005];
double ach[1005];
int main1(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
double num=0;
Rep(i,1,n){
num+=ach[i];
if(num>x){
cout<<i<<endl;
return 0;
}
}
cout<<-1<<endl;
return 0;
}
int main2(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int num=0;
Rep(i,1,n){
num+=ach[i];
if(num>x){
cout<<i<<endl;
return 0;
}
}
cout<<-1<<endl;
return 0;
}
int main(){
cin>>n>>x;
Rep(i,1,n){
cin>>v[i]>>p[i];
ach[i]=v[i]/100.0*p[i];
}
if(n<=3) main1();
else main2();
} |
#include<bits/stdc++.h>
using namespace std;
using namespace std::chrono;
typedef long long ll;
#define REP(i, n) for(ll i = 0; i < (ll)(n); ++i)
#define FOR(i, a, b) for(ll i=(a); i < (ll)(b); ++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;}
enum Walk{
L,R,U,D
};
struct Coord{
int x,y;
void move(Walk walk){
switch(walk){
case L: y--;
break;
case R: y++;
break;
case U: x--;
break;
case D: x++;
break;
}
}
void back(Walk walk){
switch(walk){
case L: y++;
break;
case R: y--;
break;
case U: x++;
break;
case D: x--;
break;
}
}
};
Coord taka;
struct Grid{
int val[50][50];
int get(Coord coord){
return val[coord.x][coord.y];
}
};
Grid t,p;
vector<bool> visited;
string ans = "";
string lastans;
ll lastscore = 0;
ll numofSearch = 0;
const Walk wlist[4][3] = {
{L, U, D},
{R, D, U},
{U, R, L},
{D, L, R},
};
system_clock::time_point time_start = system_clock::now();
constexpr double TIME_END = 1950;
random_device seed_gen;
mt19937_64 engine(seed_gen());
void input(){
scanf("%d %d",&(taka.x),&(taka.y));
int tmax=0;
REP(i,50) REP(j,50) {
scanf("%d" ,&(t.val[i][j]));
chmax(tmax, t.val[i][j]);
}
visited.resize(tmax+1, false);
REP(i,50) REP(j,50) scanf("%d" ,&(p.val[i][j]));
}
void output(){
//cerr << "score: " << lastscore << "\nnum: " << numofSearch << "\n";
// cout << lastans << "\n";
printf("%s\n",lastans.c_str());
}
void addtoAns(Walk walk){
switch(walk){
case L: ans += 'L';
break;
case R: ans += 'R';
break;
case U: ans += 'U';
break;
case D: ans += 'D';
break;
}
}
bool canWalk(Walk walk){
if(walk == L){
if(taka.y == 0) return false;
else return !visited[t.val[taka.x][taka.y-1]];
}
if(walk == R){
if(taka.y == 49) return false;
else return !visited[t.val[taka.x][taka.y+1]];
}
if(walk == U){
if(taka.x == 0) return false;
else return !visited[t.val[taka.x-1][taka.y]];
}
if(walk == D){
if(taka.x == 49) return false;
else return !visited[t.val[taka.x+1][taka.y]];
}
return false;
}
bool dfs(Walk walk, ll score){
if(!canWalk(walk)){
if(score > lastscore){
lastscore = score;
lastans = ans;
}
return false;
}
if(numofSearch & (1<<20)){
auto exe_time = duration_cast<milliseconds>(system_clock::now() - time_start).count();
if((double)exe_time > TIME_END){
if(score > lastscore){
lastscore = score;
lastans = ans;
}
return true;
}
}
numofSearch++;
taka.move(walk);
visited[t.get(taka)] = true;
score += p.get(taka);
addtoAns(walk);
if(walk==L){
for(Walk x: wlist[0]) if(dfs(x, score)) return true;
}else if(walk==R){
for(Walk x: wlist[1]) if(dfs(x, score)) return true;
}else if(walk==U){
for(Walk x: wlist[2]) if(dfs(x, score)) return true;
}else if(walk==D){
for(Walk x: wlist[3]) if(dfs(x, score)) return true;
}
ans.pop_back();
visited[t.get(taka)] = false;
taka.back(walk);
return false;
}
void solve(){
visited[t.get(taka)] = true;
if(dfs(L,0)) return;
if(dfs(R,0)) return;
if(dfs(U,0)) return;
if(dfs(D,0)) return;
}
int main(){
input();
solve();
output();
return 0;
} | // I SELL YOU...!
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<queue>
#include<chrono>
#include<iomanip>
#include<map>
#include<set>
#include<random>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using TP = tuple<ll,ll,ll>;
void init_io(){
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(18);
}
ll n=50;
ll dx[] = {1,0,-1,0};
ll dy[] = {0,1,0,-1};
vector<vector<ll>> t(n,vector<ll>(n));
vector<vector<ll>> p(n,vector<ll>(n));
void reset_d(const vector<ll> pt, vector<ll> &used) {
for(auto p: pt) used[p] = false;
}
bool is_in(ll x,ll y){
return x>=0&&y>=0&&x<n&&y<n;
}
random_device seed_gen;
mt19937 engine(seed_gen());
uniform_int_distribution<int> rand_dist(0, 14);
auto bden = bind(rand_dist, ref(engine));
ll eval(P x){
return x.first * 300 + x.second;
}
TP prob_search(ll x,ll y,ll time,vector<ll> &used,ll cnt) {
if (time != 0) {
used[t[x][y]] = true;
P now = P(-5,-5);
ll sid = -1;
used[t[x][y]] = true;
for(int i=0;i<4;i++){
ll nx = x + dx[i];
ll ny = y + dy[i];
if(!is_in(nx,ny)||used[t[nx][ny]]) {
continue;
}
TP np = prob_search(nx,ny,time-1,used,cnt+1);
P use = P(get<0>(np)+1,get<1>(np)+p[nx][ny]);
if (eval(use) > eval(now)) {
now = use;
sid = i;
}
}
used[t[x][y]] = false;
return TP(now.first+1,now.second+p[x][y],sid);
}
ll sum = 0;
vector<ll> use_t;
ll c = 0;
while(true){
ll sid = -1,v=-1;
used[t[x][y]] = true;
use_t.push_back(t[x][y]);
for(int j=0;j<4;j++){
ll nx = x + dx[j];
ll ny = y + dy[j];
if(is_in(nx,ny)&&!used[t[nx][ny]]&&v<p[nx][ny]) {
v = p[nx][ny];
sid = j;
}
}
if(sid==-1) {
reset_d(use_t,used);
return TP(c, sum,-1);
}
sum += v;
x = x + dx[sid];
y = y + dy[sid];
c++;
}
}
signed main(){
const string st ="DRUL";
init_io();
ll sx,sy;
cin >> sx >> sy;
vector<char> mv;
vector<ll> used(n*n,0);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin >> t[i][j];
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin >> p[i][j];
}
}
ll x = sx,y = sy;
while(true){
auto [sz, score, di] = prob_search(x,y,17,used,0);
if(di==-1) break;
used[t[x][y]] = true;
mv.push_back(st[di]);
x = x + dx[di];
y = y + dy[di];
}
for(auto c:mv) cout << c;
cout << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(ll i=0;i<(ll)n;i++)
#define dump(x) cerr << "Line " << __LINE__ << ": " << #x << " = " << (x) << "\n";
#define spa << " " <<
#define fi first
#define se second
#define ALL(a) (a).begin(),(a).end()
#define ALLR(a) (a).rbegin(),(a).rend()
using ld = long double;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
template<typename T> using V = vector<T>;
template<typename T> using P = pair<T, T>;
template<typename T> vector<T> make_vec(size_t n, T a) { return vector<T>(n, a); }
template<typename... Ts> auto make_vec(size_t n, Ts... ts) { return vector<decltype(make_vec(ts...))>(n, make_vec(ts...)); }
template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){os << "(" << v.first << ", " << v.second << ")"; return os;}
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { for (auto &e : v) os << e << ' '; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<vector<T>> &v){ for(auto &e : v){os << e << "\n";} return os;}
struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
template <class T> void UNIQUE(vector<T> &x) {sort(ALL(x));x.erase(unique(ALL(x)), x.end());}
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; }
void fail() { cout << -1 << '\n'; exit(0); }
inline int popcount(const int x) { return __builtin_popcount(x); }
inline int popcount(const ll x) { return __builtin_popcountll(x); }
template<typename T> void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++)
{cerr<<v[i][0];for(ll j=1;j<w;j++)cerr spa v[i][j];cerr<<"\n";}};
template<typename T> void debug(vector<T>&v,ll n){if(n!=0)cerr<<v[0];
for(ll i=1;i<n;i++)cerr spa v[i];
cerr<<"\n";};
const ll INF = (1ll<<62);
// const ld EPS = 1e-10;
// const ld PI = acos(-1.0);
const ll mod = (int)1e9 + 7;
//const ll mod = 998244353;
ll modpow(ll a, ll exp, ll m){
ll res = 1;
while(exp>0){
if(exp&1){
res *= a;
res %= m;
}
a = a*a;
a %= m;
exp >>= 1;
}
return res;
}
int main(){
ll N, M;
cin >> N >> M;
ll x = modpow(10, N, M*M);
cout << x / M << endl;
return 0;
} | #include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;
using Graph=vector<vector<int>>;
const double pi=3.141592653589793;
const int64_t inf=1L<<60;
const int64_t minf=-inf;
const int64_t mod=998244353; /*1000000007*/
int64_t N,M;
//pow
int64_t power(int64_t a,int64_t b){
int64_t res=1;
while(b>0){
if(b&1){
res=res*a%(M*M);
}
a=a*a%(M*M);
b>>=1;
}
return res;
}
int main(){
cin>>N>>M;
int64_t k=power(10,N);
cout<<k/M<<endl;
} |
#pragma GCC target ("avx2")
#pragma GCC optimize ("unroll-loops")
#pragma GCC optimize ("O3")
#include "bits/stdc++.h"
#include <unordered_set>
#include <unordered_map>
#include <random>
using namespace std;
typedef long long ll;
const ll MOD = 1'000'000'007LL; /*998'244'353LL;*/
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for(int (i)=0; (i)<(n); (i)++)
const int dx[4]={ 1,0,-1,0 };
const int dy[4]={ 0,1,0,-1 };
template<long long Mod>
struct mint{
long long x;
mint(long long x=0) :x((x% Mod+Mod)%Mod){}
mint operator-() const { return mint(-x); }
mint& operator+=(const mint a) {
if((x += a.x) >= Mod) x -= Mod;
return *this;
}
mint& operator-=(const mint a) {
if((x += Mod-a.x) >= Mod) x -= Mod;
return *this;
}
mint& operator*=(const mint a) {
(x *= a.x) %= Mod;
return *this;
}
mint operator^(long long t) const {
mint ret(1);
mint a = *this;
while(t){
if(t&1){
ret *= a;
}
a *= a;
t >>= 1;
}
return ret;
}
mint inv() const { return *this ^ (Mod-2); }
mint& operator/=(const mint a) { return *this *= a.inv(); }
mint operator+(const mint a) const { return mint(*this) += a; }
mint operator-(const mint a) const { return mint(*this) -= a; }
mint operator*(const mint a) const { return mint(*this) *= a; }
mint operator/(const mint a) const { return mint(*this) /= a; }
friend istream& operator>>(istream& is, mint& a) { return is >> a.x; }
friend ostream& operator<<(ostream& os, const mint& a) { return os << a.x; }
};
int H, W, K;
string S[2000];
int g[2000][2000];
signed main(){
cin >> H >> W;
rep(i, H) cin >> S[i];
rep(i, H){
rep(j, W){
if(S[i][j] == '.') K++;
}
}
rep(i, H){
int k;
for(k=0; k<W; k++){
if(S[i][k] == '#') break;
}
for(int l=0; l<k; l++){
g[i][l] += k;
}
rep(j, W){
if(S[i][j] == '#'){
int k;
for(k=j+1; k<W; k++){
if(S[i][k] == '#') break;
}
for(int l=j+1; l<k; l++){
g[i][l] += k-j-1;
}
}
}
}
rep(j, W){
int k;
for(k=0; k<H; k++){
if(S[k][j] == '#') break;
}
for(int l=0; l<k; l++){
g[l][j] += k;
}
rep(i, H){
if(S[i][j] == '#'){
int k;
for(k=i+1; k<H; k++){
if(S[k][j] == '#') break;
}
for(int l=i+1; l<k; l++){
g[l][j] += k-i-1;
}
}
}
}
mint<MOD> ans = 0;
rep(i, H){
rep(j, W){
if(S[i][j] == '#'){
continue;
}
ans += mint<MOD>(2)^K;
ans -= mint<MOD>(2)^(K-g[i][j]+1);
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main()
{
// Input from input.txt
ifstream in("input.txt");
cin.rdbuf(in.rdbuf());
// Speeding up cin
cin.tie(0);
ios::sync_with_stdio(false);
// Main code
int N, M;
cin >> N >> M;
vector<string> S(N);
for (int i = 0; i < N; i++) {
cin >> S[i];
}
int64_t even = 0, odd = 0;
for (int i = 0; i < N; i++) {
bitset<20> bs(S[i]);
if (bs.count() % 2 == 0) {
even++;
} else {
odd++;
}
}
cout << even * odd << endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define rep(i, l, r) for (int i = l; i <= r; ++i)
#define dep(i, r, l) for (int i = r; i >= l; --i)
const int mod = 1e9 + 7;
void read (int &x) {
x = 0; char c = getchar();
while(c < '0' || c > '9') c = getchar();
while(c >= '0' && c <= '9')
x = x * 10 + c - 48, c = getchar();
}
namespace Mint {
struct mint {
int x ;
mint(int _x = 0) { x = _x ; }
mint operator = (int _x) { return mint(x = _x); }
};
inline mint operator * (mint a, mint b) { return 1ll * a.x * b.x % mod ; }
inline mint operator - (mint a, mint b) { return ((a.x -= b.x) < 0) ? a.x + mod : a.x ; }
inline mint operator + (mint a, mint b) { return ((a.x += b.x) >= mod) ? a.x - mod : a.x ; }
inline mint Pow(mint a, int b) { mint r = 1; while(b) { if(b & 1) r = r * a; a = a * a, b >>= 1; } return r; }
inline mint operator / (mint a, mint b) { return a * Pow(b, mod - 2); }
inline bool operator == (mint a, mint b) { return a.x == b.x; }
ostream& operator << (ostream& out, mint b) { out << b.x; return out; }
}
int n, a[21], Lim, ans = 0x7fffffff;
int main () {
scanf("%d", &n), Lim = 1 << n - 1;
rep(i, 1, n) read(a[i]);
dep(dev, Lim - 1, 0) {
int now = 0, sum = 0;
rep(i, 1, n) {
now |= a[i];
if (dev & (1 << i - 1))
sum ^= now, now = 0;
}
sum ^= now, ans = min(ans, sum);
}
printf("%d", ans);
return 0;
} |
// Problem: C - ORXOR
// Contest: AtCoder - AtCoder Beginner Contest 197(Sponsored by Panasonic)
// URL: https://atcoder.jp/contests/abc197/tasks/abc197_c
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
#include<bits/stdc++.h>
using namespace std;
const int INF = INT_MAX;
const int MOD = 1e9 + 7;
//const int ULTRA = 1e14;
const int PROMAX = 1e6 + 5;
const int PRO = 1e5 + 5;
#define ll long long
#define int ll
#define all(x) x.begin(), x.end()
#define lla(x) x.rbegin(), x.rend()
#define pb push_back
#define ThinkTwice ios_base::sync_with_stdio(false); cin.tie(NULL);
int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1};
int dy[] = { 0, 1, 0, -1, -1, 1, 1, -1};
char dir[] = {'U','R','D','L'};
inline int powmod(int a, int b, int mod) {int res=1; while(b>0) {if(b&1) res=(res*a)%mod; a=(a*a)%mod; b>>=1; } return res;}
inline int power(int a, int b) {int res=1; while(b>0) {if(b&1) res*=a; a*=a; b>>=1;} return res; }
//__________________________________________________________________
void solve() {
int n; cin>>n;
int a[n];
for(int i = 0; i<n; i++) cin>>a[i];
int res = 0;
for(int i = 0; i<n; i++) res ^= a[i];
for(int mask = 0; mask < (1<<n); mask++) {
vector <int> dp;
for(int bit = 0; bit<n; bit++) {
if(mask & (1<<bit)) dp.pb(1);
else dp.pb(0);
}
int k = dp[0], tmp = 0, tmp_xor = 0;
for(int i = 0; i<n; i++) {
if(dp[i] == k) {
tmp |= a[i];
}
else {
tmp_xor ^= tmp;
tmp = a[i];
}
}
tmp_xor ^= tmp;
res = min(res, tmp_xor);
}
cout<<res<<endl;
}
signed main() {
ThinkTwice
int t;
t = 1;
// cin>>t;
for(int i = 1; i<=t; i++) {
// cout<<"Case #"<<i<<": ";
solve();
}
}
//__________________________________________________________________
/*
Sample Input:
Sample Output:
*/
|
/// BISMILLA HIRRAHMANIR RAHIM
/// ALLAH IS ALMIGHTY
/// ALLAH IS MUCH MORE CREATIVE
#include<bits/stdc++.h>
#define faster ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define pb push_back
#define ff first
#define ss second
#define mem(a,v) memset(a, v, sizeof(a))
#define all(a) (a).begin(), (a).end()
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
using ll = int64_t;
using ii= pair<ll,ll>;
const double PI = acos(-1.0);
const int mx = 1e5+5;
int dr[]= {0,-1,0,1,-1,-1,1,1}; /// first 4 are right,up,left,bottom
int dc[]= {1,0,-1,0,1,-1,-1,1};
int kx[]= {-1,-2,-2,-1,1,2,2,1}; /// knight move
int ky[]= {2,1,-1,-2,-2,-1,1,2};
bool valid(int row,int col){
return (row>=1) && (row<=8) && (col>=1) && (col<=8);
}
ll a[mx],b[mx],su[mx];
void Solve()
{
ll i,j,x,y,sum=0,ans=0,n,m,k,l=1;
string dd,s="";cin>>dd;
for(i=0; i<dd.size(); i++){
if(dd[i]=='.') break;
else s+= dd[i];
}
cout << s<< "\n";
}
int main()
{
//faster;
int TC=1;
int jj=1;
//cin >> TC;
while(TC--) {
Solve();
}
return 0;
}
| #ifdef __LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
using namespace std;
#pragma region macros
using str=string;
using ll=long long;
using vl=vector<ll>;
using vc=vector<char>;
using pl=pair<ll,ll>;
using ml=map<ll,ll>;
using sl=set<ll>;
using graph=vector<vector<ll>>;
template<class T> using V=vector<T>;
template<class T,class U> using P=pair<T,U>;
#define rep(i,n) for(ll i=0;i<n;i++)
#define reps(i,n) for(ll i=1;i<n;i++)
#define REP(i,m,n) for(ll i=m;i<n;i++)
#define drep(i,n) for(ll i=n-1;i>=0;i--)
#define fore(n) for(auto&& i:n)
#define fors(n) for(auto&&[i,j]:n)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend();
#define sor(v) sort(all(v))
#define rsor(v) sort(all(v),greater<ll>())
#define rev(v) reverse(all(v))
#define low(v,x) lower_bound(all(v),x)-v.begin()
#define up(v,x) upper_bound(all(v),x)-v.begin()
#define acc(v) accumulate(all(v),0LL)
#define ef(x) emplace_front(x)
#define eb(x) emplace_back(x)
#define pf() pop_front()
#define pb() pop_back()
#define mp(a,b) make_pair(a,b)
#define ceil(a,b) (a+b-1)/b
#define bit(n) (1LL<<n)
#define fi first
#define se second
#define be begin()
#define en end()
#define br break
#define cn continue
#define wh while
#define el else if
#define re return
const ll inf=1LL<<60;
const ll mod=1000000007;
const ll MOD=998244353;
const double pi=3.1415926535;
const double eps=1e-10;
const ll dx[4]={1,0,-1,0};
const ll dy[4]={0,1,0,-1};
void input(){}
void inputs(){}
void output(){}
template<class T,class...U> void input(T& x,U&...y){cin>>x;input(y...);}
template<class...T> void in(T&...x){input(x...);}
template<class T> void inputs(const ll t,T& x){cin>>x[t];}
template<class T,class...U> void inputs(const ll t,T& x,U&...y){cin>>x[t];inputs(t,y...);}
template<class T,class...U> void inputs(T& x,U&...y){rep(t,size(x))inputs(t,x,y...);}
template<class...T> void ins(T&...x){inputs(x...);}
template<class T,class...U> void output(T x,U...y){cout<<x<<"\n";output(y...);}
template<class...T> void out(T...x){output(x...);}
template<class...T> void fin(T...x){out(x...);exit(0);}
template<class T> istream&operator>>(istream& i,V<T>& v){for(T& x:v)i>>x;re i;}
template<class T,class U> istream&operator>>(istream& i,P<T,U>& p){re i>>p.fi>>p.se;}
template<class T> ostream&operator<<(ostream& o,V<T>& v){for(T& x:v)o<<x<<" ";re o;}
template<class T,class U> ostream&operator<<(ostream& o,P<T,U>& p){re o<<p.fi<<" "<<p.se;}
void yn(bool b){fin((b?"Yes":"No"));}
void YN(bool b){fin((b?"YES":"NO"));}
void pos(bool b){fin((b?"POSSIBLE":"IMPOSSIBLE"));}
template<class T> void chmax(T& a,T b){if(a<b)a=b;}
template<class T> void chmin(T& a,T b){if(a>b)a=b;}
template<class T,class U> auto max(T a,U b){re a>b?a:b;}
template<class T,class U> auto min(T a,U b){re a<b?a:b;}
ll qp(ll x,ll n){ll r=1;wh(n>0){if(n&1)r*=x;x*=x;n>>=1;}re r;}
ll mdp(ll x,ll n){ll r=1;wh(n>0){if(n&1)r=r*x%mod;x=x*x%mod;n>>=1;}re r;}
ll MDp(ll x,ll n){ll r=1;wh(n>0){if(n&1)r=r*x%MOD;x=x*x%MOD;n>>=1;}re r;}
struct initialize{
initialize(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout<<fixed<<setprecision(10);
}
}init;
#pragma endregion
signed main(){
ll n; in(n);
vl a(n),b(n),c(n); in(a,b,c);
ll ans=0;
sor(a);
rep(i,n){
ll x=b[c[i]-1];
ll y=low(a,x),z=up(a,x);
ans+=max(z-y,0);
}
out(ans);
} |
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007
#define m 998244353
int main()
{
ll a,b,c,ans=0;
cin>>a>>b>>c;
ll x=((a*(a+1))/2)%m;
ll y=((b*(b+1))/2)%m;
ll z=((c*(c+1))/2)%m;
ans=(x*y)%m;
ans=(ans*z)%m;
cout<<ans;
} | #include<bits/stdc++.h>
typedef long long ll;
#define V(a) vector<a>
#define P(a,b) pair<a,b>
#define A(a) set<a>
#define M(a,b) map<a,b>
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define in insert
#define bp __builtin_popcount
#define ppb pop_back
#define FOR(a,b,c) for(ll(a) = b; a < c; a++)
#define FORN(a,b,c) for(ll(a) = b; a <= c; a++)
#define FOREV(a,b,c) for(ll(a)=b; a > c; a--)
#define FOREVN(a,b,c) for(ll(a)=b; a >= c; a--)
#define fastio ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
using namespace std;
bool cprime(int x)
{
if (x == 1)
return false;
if (x <= 3)
return true;
if (x % 2 == 0 || x % 3 == 0)
return false;
for (int i = 5; i <= sqrt(x); i += 6)
if (x % i == 0 || x % (i + 2) == 0)
return false;
return true;
}
bool cpalin(string s)
{
int n = s.length(), l;
if (n == 1)
return true;
if (n % 2 == 0)
l = n / 2 - 1;
else
l = (n - 1) / 2 - 1;
for (int i = 0; i <= l; i++)
if (s[i] != s[n - i - 1])
return false;
return true;
}
void sieveOfEratosthenes(int n)
{
bool arr[n + 1];
FOR(i, 0, n + 1)
arr[i] = true;
for (int i = 2; i * i <= n; i++)
{
if (arr[i] == true)
{
for (int j = i * i ; j <= n; j += i)
{
if (arr[j] == true)
arr[j] = false;
}
}
}
}
ll ndivs(ll n)
{
int N = 1000;
bool arr[N];
FOR(i, 0, N)
arr[i] = true;
for (int i = 2; i * i <= N; i++)
{
if (arr[i] == true)
{
for (int j = i * i ; j <= N; j += i)
{
if (arr[j] == true)
arr[j] = false;
}
}
}
ll p = 1;
FOR(i, 2, N)
{
if (arr[i] == true && n % i == 0)
{
ll m = 0; ll cp = n;
while (cp % i == 0)
{
cp /= i;
m++;
}
p *= (m + 1);
}
}
return p;
}
bool compare( ll a, ll b) // false return swap
{
return a > b;
}
ll power( ll a, ll b)
{
ll p = 1;
while (b > 0)
{
if (b & 1)
p *= a;
a *= a;
b >>= 1;
}
return p;
}
string dtobin(ll n)
{
string s;
while (n)
{
s = to_string((n % 2)) + s;
n /= 2;
}
return s;
}
ll bintod(string s)
{
ll n = 0;
FOR(i, 0, s.length())
if (s[i] == '0')
continue;
else
n += pow(2, (s.length() - 1 - i));
return n;
}
ll sdigs(ll n)
{
int s = 0;
while (n)
{
s += n % 10;
n /= 10;
}
return s;
}
ll fact(ll p)
{
int x = 1;
FORN(i, 1, p)
x *= i;
return x;
}
ll ncr(ll n , ll r)
{
ll ans = (fact(n) ) / (fact(r) * fact(n - r));
return ans;
}
ll count(ll n)
{
ll c = 0;
return c;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("Input.txt", "r", stdin);
freopen("Output.txt", "w", stdout);
#endif
fastio;
ll a, b, c;
cin >> a >> b >> c;
ll N = 998244353;
ll x = ((a % N) * ((a + 1) % N)) / 2 % N;
ll y = ((b % N) * ((b + 1) % N)) / 2 % N;
ll z = ((c % N) * ((c + 1) % N)) / 2 % N;
x %= N;
y %= N;
z %= N;
ll p = (x % N * y % N) % N;
ll q = (p % N * z % N) % N;
cout << q;
return 0;
} |
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <map>
#include <set>
#include <vector>
int main(){
int N, M;
scanf("%d %d", &N, &M);
std::map<int, std::vector<int>> Map;
for(int i = 0; i < M; i++){
int X, Y;
scanf("%d %d", &X, &Y);
Map[X].emplace_back(Y);
}
std::set<int> Set{N};
for(auto [key, Vec] : Map){
std::set<int> SetIn, SetOut;
for(int y : Vec){
if(Set.find(y - 1) != Set.find(y + 1)) SetIn.insert(y);
else SetOut.insert(y);
}
for(int in : SetIn) Set.insert(in);
for(int out : SetOut) Set.erase(out);
}
printf("%d\n", (int)Set.size());
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
using ll = long long;
using P = pair<int,int>;
int n,m;
signed main(){
cin >> n >> m ;
map<int,int> cnt;
vector<P> p(m);
for(int i=0;i<m;i++){
cin >> p[i].first;
cin >> p[i].second;
cnt[p[i].first]++;
}
sort(p.begin(),p.end());
//for(int i=0;i<m;i++){
// cout << "i=" << i << ":" << p[i].first << "," << p[i].second << endl;
//}
vector<int> d(2*m+1,0);
d[m] = 1;
int j = 0;
map<int,int> manip;
for(auto it = cnt.begin(); it != cnt.end(); it++){
int v = it->first;
// cout << "it->first=" << it->first << endl;
// int c = it->second;
manip.clear();
int j2 = j;
while( p[j].first == v ){
int y = p[j].second; // n座標系
int y2 = ( y - n ) + m; // m座標系
if( y2 >= 0 && y2 <= 2*m ){
manip[y2] = 0;
}
j++;
}
j = j2;
while( p[j].first == v ){
int y = p[j].second; //
int y2 = ( y - n ) + m; // m
if( y2 >= 0 && y2 <= 2*m && y2-1 >= 0 && y-1>=0 && d[y2-1]==1 ){
manip[y2]=1;
}
if( y2 >= 0 && y2 <= 2*m && y2+1 <=2*m && y+1<=2*n && d[y2+1]==1 ){
manip[y2]=1;
}
j++;
}
for(auto x: manip){
d[x.first] = x.second;
}
//for(int j=0;j<2*m+1;j++){
// cout << "," << d[j];
//}
//cout << endl;
}
int res = 0;
for(int i=0;i<=2*m;i++){
if(d[i]!=0) res++;
}
cout << res << endl;
}
|
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
#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 mydeb(fmt, ...) fprintf(stderr, fmt, __VA_ARGS__)
//#define mydeb(fmt, ...)
int main()
{
int n,m;
cin >> n >> m;
vector<int> a(m),b(m);
rep(i,m)
{
cin >> a[i] >> b[i];
--a[i];
--b[i];
}
vector<vector<int>> g(n);
rep(i,m)
{
g[a[i]].push_back(b[i]);
}
int total=0;
rep(i,n)
{
int count=0;
vector<bool> visited(n);
queue<int> q;
q.push(i);
while (q.empty()==false)
{
int pp=q.front();
q.pop();
if (visited[pp]!=false)
{
continue;
}
visited[pp]=true;
++count;
for (auto nn: g[pp])
{
if (visited[nn]==false)
{
q.push(nn);
}
}
}
total+=count;
}
cout << total << endl;
}
| #include<bits/stdc++.h>
using namespace std;
#define INF 1e15
#define EPS 1e-9
#define MOD 1000000007
#define Pi 3.14159265358979323846264338327950288419716939937510
#define umap unordered_map
#define uset unordered_set
#define fi first
#define se second
#define ll long long
#define vi vector<int>
#define pii pair<int,int>
#define vll vector<ll>
#define pll pair<ll,ll>
#define vvll vector<vector<ll>>
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define maxE(a,n) *max_element(a,a+n);
#define minE(a,n) *min_element(a,a+n);
#define all(x) x.begin(), x.end()
#define rev(x) reverse(all(x))
#define endl "\n"
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define rep(i,n) for (ll i=0;i<n;i++)
#define repd(i,n) for (ll i = n-1;i>=0;i--)
#define fori(i,a,n) for (ll i=a;i<n;i++)
#define ford(i,a,n) for (ll i=n-1;i>=a;i--)
#define trav(x,a) for(auto x:a){cout<<x<<" ";}cout<<endl;
ll gcd(ll a, ll b) { return __gcd(a, b);}
ll lcm(ll a, ll b) {return (a * b) / gcd(a, b);}
ll power(ll x, ll b, ll m) {ll p = 1; while (b > 0) {if (b & 1) {p = p * x; p %= m;} b >>= 1; x *= x; x %= m;} return p % m;}
void dbg(string s) {cout << s << endl;}
/*PBDS*/
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> PBDS;
// pbds.find_by_order(k) (return iterator to kth largest element(from 0))
// pbds.order_of_key(k) (return number of elements in set which are strictly less than k)
vector<vector<ll>> g;
vector<bool> vis;
vector<ll> dp;
ll cnt;
void dfs(ll i, ll parent) {
if (i != parent)
cnt++;
vis[i] = true;
for (auto nbr : g[i]) {
if (vis[nbr] != true) {
dfs(nbr, i);
}
}
return;
}
void solve() {
ll n, m;
cin >> n >> m;
g = vector<vector<ll>>(n + 1);
dp = vector<ll>(n + 1, 0);
cnt = 0;
rep(i, m) {
ll u, v;
cin >> u >> v;
g[u].pb(v);
}
fori(i, 1, n + 1) {
vis = vector<bool>(n + 1, false);
dfs(i, 0);
}
cout << cnt << endl;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
fast;
ll t = 1;
while (t--) {
solve();
}
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll,ll>
#define FOR(I,A,B) for(ll I = ll(A); I < ll(B); ++I)
#define FORR(I,A,B) for(ll I = ll((B)-1); I >= ll(A); --I)
#define TO(x,t,f) ((x)?(t):(f))
#define SORT(x) (sort(x.begin(),x.end())) // 0 2 2 3 4 5 8 9
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) //xi>=v x is sorted
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) //xi>v x is sorted
#define NUM(x,v) (POSU(x,v)-POSL(x,v)) //x is sorted
#define REV(x) (reverse(x.begin(),x.end())) //reverse
ll gcd_(ll a,ll b){if(a%b==0)return b;return gcd_(b,a%b);}
ll lcm_(ll a,ll b){ll c=gcd_(a,b);return ((a/c)*b);}
#define NEXTP(x) next_permutation(x.begin(),x.end())
const ll INF=ll(1e16)+ll(7);
const ll MOD=1000000007LL;
#define out(a) cout<<fixed<<setprecision((a))
//tie(a,b,c) = make_tuple(10,9,87);
#define pop_(a) __builtin_popcount((a))
ll keta(ll a){ll r=0;while(a){a/=10;r++;}return r;}
#define num_l(a,v) POSL(a,v) //v未満の要素数
#define num_eql(a,v) POSU(a,v) //v以下の要素数
#define num_h(a,v) (a.size() - POSU(a,v)) //vより大きい要素数
#define num_eqh(a,v) (a.size() - POSL(a,v)) //v以上の要素数
// static_cast< long long ll >
//chrono::system_clock::time_point start, end;
clock_t start;
bool did[50*52];
int tile[51][51];
int point[51][51];
int N = 50;
int si,sj;
string now = "";
int len = 0;
string ans="";
int score = 0;
int best_s = 0;
string D = "DURL";
int di[] = {1,-1,0,0};
int dj[] = {0,0,1,-1};
string DD = "AAAA";
int di_[] = {1,-1,0,0};
int dj_[] = {0,0,1,-1};
double t = 82.0;
void calc(int i,int j,char &x){
if(i<0 || j<0 || i>=50 || j>=50)return;
if(did[ tile[i][j] ])return;
clock_t end = clock();
double time = static_cast<double>(end - start) / CLOCKS_PER_SEC * 1000.0;
if(time >= t)return;
now[len] = x;
len++;
did[ tile[i][j] ] = true;
score += point[i][j];
if(score > best_s){
best_s = score;
ans = now.substr(0,len);
}
FOR(k,0,4){
calc(i+di_[k],j+dj_[k],DD[k]);
}
len--;
did[ tile[i][j] ] = false;
score -= point[i][j];
}
int main(){
FOR(i,0,N*N) now += 'D';
mt19937_64 mt(1);
uniform_real_distribution<double> rnd(0.00,1.00);
cin >> si >> sj;
FOR(i,0,N)FOR(j,0,N){
cin >> tile[i][j];
}
FOR(i,0,N)FOR(j,0,N){
cin >> point[i][j];
}
start = clock();
char a = 'U';
vector<int> ind(4);
ind[0] = 0;
ind[1] = 1;
ind[2] = 2;
ind[3] = 3;
do{
for(int i=0;i<4;i++){
DD[i] = D[ind[i]];
di_[i] = di[ind[i]];
dj_[i] = dj[ind[i]];
}
calc(si,sj,a);
t += 82.0;
}while(NEXTP(ind));
if(ans.size() == 0) ans = "U";
cout << ans.substr(1,ans.size()-1) << endl;
}
| #include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int si, sj, ti, tj, t[50][50], p[50][50],
dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
bool done[2500] = {};
char c[4] = {'R', 'D', 'L', 'U'};
string ans = "";
pair<int, int> next(int d){
pair<int, int> res = {ti + dx[d % 4], tj + dy[d % 4]};
return res;
}
bool check(int i, int j, int d){
int ni = i + dx[d % 4], nj = j + dy[d % 4];
bool res = (ni >= 0) && (ni < 50) && (nj >= 0) && (nj < 50);
if(res) res = !done[t[ni][nj]];
return res;
}
void change(int d){
ti = ti + dx[d % 4], tj = tj + dy[d % 4];
done[t[ti][tj]] = true;
ans.push_back(c[d % 4]);
return;
}
int nextok(int d){
if(!check(ti, tj, d)) return -1;
auto [ni, nj] = next(d);
int res = 0;
for(int i = 0; i < 4; i++){
int nni = ni + dx[i], nnj = nj + dy[i];
if((nni >= 0) && (nni < 50) && (nnj >= 0) && (nnj < 50)){
if(!done[t[nni][nnj]] && (t[ni][nj] != t[nni][nnj])) res++;
}
}
return res;
}
int main(){
scanf("%d %d", &si, &sj);
ti = si, tj = sj;
for(int i = 0; i < 50; i++)
for(int j = 0; j < 50; j++)
scanf("%d", &t[i][j]);
for(int i = 0; i < 50; i++)
for(int j = 0; j < 50; j++)
scanf("%d", &p[i][j]);
done[t[si][sj]] = true;
int foo[4] = {49 - sj, 49 - si, sj, si};
int D = (min_element(foo, foo + 4) - foo) + (1<<20);
while(true){
if(!(check(ti, tj, D) || check(ti, tj, D + 1) || check(ti, tj, D + 2) || check(ti, tj, D + 3))) break;
int nd = rand() % 4, md = -1;
for(int i = nd; i < nd + 4; i++){
if(md < nextok(i)){
D = i;
md = nextok(i);
}
}
change(D);
}
/*while(true){
if(!(check(ti, tj, D - 1) || check(ti, tj, D) || check(ti, tj, D + 1))) break;
if(check(ti, tj, D))change(D);
else{
int td = D - 1 + 2 * (rand() & 1);
if(check(ti, tj, td)) change(td);
else change(td + 2);
}
}
D++;
while(true){
if(!(check(ti, tj, D) || check(ti, tj, D + 1) || check(ti, tj, D + 2) || check(ti, tj, D + 3))) break;
for(int d = D - 1; d < D + 3; d++){
if(check(ti, tj, d)){
change(d);
D = d;
break;
}
}
}*/
cout << ans << endl;
return 0;
} |
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, init, end) for(ll i = init; i < end; i++)
#define REP(i, init, end) for(ll i = init; i < end + 1; i++)
#define rev(i, end, init) for(ll i = init - 1; i >= end; i--)
#define REV(i, end, init) for(ll i = init; i >= end; i--)
#define PI 3.14159265359
#define EPS 0.0000000001
#define MOD 998244353
//cout << std::fixed << std::setprecision(15) << y << endl;
int main(){
ll N;
cin >> N;
if(N == 3){
cout << "6 10 15" << endl;
return 0;
}
ll count = 0;
ll i = 6;
vector<ll> ans;
while(count < N){
if((i % 6 == 0 && i % 5 != 0) || (i % 10 == 0 && i % 3 != 0) || (i % 15 == 0 && i % 2 != 0) || (i % 30 == 0)){
count++;
ans.push_back(i);
}
i++;
}
rep(i, 0, ans.size()){
if(i != 0){
cout << " ";
}
cout << ans[i];
}
cout << endl;
// cout << "size: " << ans.size() << endl;
return 0;
}
| #include<stdio.h>
static inline int IN(void)
{
int x=0,f=1,c=getchar();while(c<48||c>57){if(c==45){f=-f;}c=getchar();}
while(c>47&&c<58){x=x*10+c-48,c=getchar();}return f*x;
}
static inline void OUT(int x){if(x<0){putchar('-'),x=-x;}if(x>=10){OUT(x/10);}putchar(x-x/10*10+48);}
int main(void)
{
int N=IN(),M=1,x=1;static int z[9994]={};
while(N--)
{
x=6*M;if(x<=10000&&!z[x-6]){OUT(x);putchar(' ');z[x-6]=1;continue;}
x=1155*M;if(x<=10000&&!z[x-6]){OUT(x);putchar(' ');z[x-6]=1;continue;}
x=14*M;if(x<=10000&&!z[x-6]){OUT(x);putchar(' ');z[x-6]=1;continue;}
x=22*M;if(x<=10000&&!z[x-6]){OUT(x);putchar(' ');z[x-6]=1;continue;}
x=10*M;if(x<=10000&&!z[x-6]){OUT(x);putchar(' ');z[x-6]=1;continue;}
M++;N++;
}
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define SZ(x) ((int)(x).size())
#define FOR(var, begin, end) for (int var = (begin); var <= (end); var++)
#define RFOR(var, begin, end) for (int var = (begin); var >= (end); var--)
#define REP(var, length) FOR(var, 0, length - 1)
#define RREP(var, length) RFOR(var, length - 1, 0)
#define EACH(value, var) for (auto value : var)
#define SORT(var) sort(var.begin(), var.end())
#define REVERSE(var) reverse(var.begin(), var.end())
#define RSORT(var) \
SORT(var); \
REVERSE(var)
#define OPTIMIZE_STDIO \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.precision(10); \
cout << fixed
#define endl '\n'
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;
void solve(istream& cin, ostream& cout) {
int n;
cin >> n;
vector<ll> a(n);
REP(i, n) cin >> a[i];
vector<ll> c(n + 1);
c[0] = 0;
REP(i, n) {
if (i % 2 == 0) c[i + 1] = c[i] + a[i];
else
c[i + 1] = c[i] - a[i];
}
map<ll, ll> mp;
FOR(i, 0, n) mp[c[i]]++;
ll ans = 0;
EACH(e, mp) {
ans += e.second * (e.second - 1) / 2;
}
cout << ans << endl;
}
#ifndef TEST
int main() {
OPTIMIZE_STDIO;
solve(cin, cout);
return 0;
}
#endif
| #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<tuple>
#include<cmath>
#include<iomanip>
using namespace std;
typedef long long ll;
typedef vector<ll> v;
typedef vector<vector<ll>> vv;
#define MOD 1000000007
#define INF 1001001001
#define MIN -1001001001
#define rep(i,k,N) for(int i=k;i<N;i++)
#define MP make_pair
#define MT make_tuple //tie,make_tuple は別物
#define PB push_back
#define PF push_front
#define all(x) (x).begin(),(x).end()
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int main(){
v fib(88,0);
fib[0]=1;fib[1]=1;
rep(i,2,88)fib[i]=fib[i-1]+fib[i-2];
ll N;
cin>>N;
v fbit(88,0);
ll sbit;
bool ok = false;
for(ll i = 87;i>=0;i--){
if(N>=fib[i]){
fbit[i]=1;
N-=fib[i];
if(!ok){
sbit = i;
ok = true;
}
}
}
ll nowxy = sbit%2;
ll x=0,y=0;
ll sb=0;
rep(i,0,sbit+1){
if(fbit[i]==1)sb++;
}
cout<<sb+sbit<<endl;
for(ll i = sbit; i>=1;i--){
if(fbit[i]){
cout<<nowxy+1<<endl;
if(nowxy==0)x++;
else y++;
}
nowxy++;nowxy%=2;
cout<<nowxy+3<<endl;
if(nowxy==0)x=x+y;
else y=x+y;
}
//cout<<x<<" "<<y;
return 0;
} |
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#include <algorithm>
#include <iostream>
#include <bitset>
#include <vector>
#include <array>
using namespace std;
#define f(x,y,z) for(int x=y;x<=z;++x)
vector<int> v[200001];
int n, a, b, it;
array<int, 200001> jr, dlm, nmr;
bitset<200001> sdh;
void jrk(int x) {
for(int &i:v[x]) if(!jr[i]) {
jr[i] = jr[x]+1, jrk(i);
}
}
void trn(int x) {
sdh[x] = true;
for(int &i:v[x]) if(!sdh[i]) {
trn(i), dlm[x] = max(dlm[x], dlm[i]+1);
}
}
void isi(int x) {
sdh[x] = true, nmr[x] = a;
vector<pair<int, int>> mn;
for(int &i:v[x]) if(!sdh[i]) mn.push_back({dlm[i], i});
sort(mn.begin(), mn.end());
for(auto &i:mn) ++a, isi(i.second), ++a;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
cin >> n;
f(i,2,n) {
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
jr[1] = a = 1, jrk(1);
it = max_element(jr.begin()+1, jr.begin()+n+1)-jr.begin();
trn(it), sdh.reset(), isi(it);
f(i,1,n) cout << nmr[i] << ' ';
} | //全力以赴
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define PI 3.141592653L
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const ll INF = (ll)1e18;
const int N = 2e5 + 5;
const ll MOD = 1e9+7;
int parent[N];
const int dx[4] = {+1, 0, -1, 0};
const int dy[4] = {0, -1, 0, +1};
ll gcd(ll a, ll b) {
if (a == 0)
return b;
return gcd(b % a, a);
}
ll extGcd(ll a, ll b, ll &x, ll &y) { // solves for x and y where ax+by=gcd(a,b)
if(b == 0) {
x = 1;
y = 0;
return a;
}
ll d = extGcd(b, a % b, y, x);
y -= a / b * x;
return d;
}
ll modInv(ll a, ll p) { // solve for x=a^-1 where ax + py = 1 % p
if(gcd(a, p) != 1) return -1; // modInv only exists if a and p are coprime (gcd(a, p) = 1)
ll x, y;
extGcd(a, p, x, y);
x = (x % p + p) % p;
return x;
}
ll lcm(ll a, ll b) {
return (a * b) / gcd(a,b);
}
/*
a^b:
if b is even -> a^b = a^(b/2) * a^(b/2)
if b is odd -> a^b = a * a^(b-1/2) * a^(b-1/2)
*/
ll modPow(ll a, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1) res = (res * a) % mod;
a = (a * a) % mod;
n >>= 1;
}
return res;
}
int find(int x) {
return x == parent[x] ? x : parent[x] = find(parent[x]);
}
void Union(int x, int y) {
int xPar = find(x), yPar = find(y);
if(xPar != yPar) parent[xPar] = yPar;
}
ll nCr(ll n, ll r) {
ll res = 1;
for(ll i=1;i<=r;i++) {
res = res * (n - r + i) / i;
}
return res;
}
int ask(int i, int j) {
cout << "? " << i + 1 << ' ' << j + 1 << endl;
int v;
cin >> v;
return v;
}
int msbPos(int n) {
if(n == 0) return 0;
int msbPos = 0;
n /= 2;
while(n != 0) {
n /= 2;
msbPos++;
}
return msbPos;
}
void solve() {
int n, m;
cin >> n >> m;
set<int> st;
for(int i=0;i<n;i++) {
int x;
cin >> x;
st.insert(x);
}
for(int i=0;i<m;i++) {
int x;
cin >> x;
if(st.count(x)) {
st.erase(x);
}
else {
st.insert(x);
}
}
for(auto x : st) cout << x << ' ';
cout << endl;
}
int main()
{
IOS
//int t;
//cin >> t;
//while(t--) solve();
solve();
return 0;
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.