code_file1
stringlengths 87
4k
| code_file2
stringlengths 82
4k
|
---|---|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double pi = acos(-1);
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const ull base = 131;
const ull mod1 = 1e9 + 9;
const ull mod2 = 1610612741;
const int maxn = 2e5 + 10;
int n, m, k;
bool vis[maxn];
double dp[maxn][2], now[2];
int main()
{
scanf("%d%d%d", &n, &m, &k);
while (k--)
{
int x;
scanf("%d", &x);
vis[x] = 1;
}
dp[n][0] = dp[n][1] = 0;
int l, r;
l = r = n;
while (l >= 1)
{
int i = l - 1;
if (vis[i]) dp[i][0] = 1, dp[i][1] = 0;
else dp[i][0] = now[0] / m, dp[i][1] = now[1] / m + 1;
now[0] += dp[i][0];
now[1] += dp[i][1];
l = i;
if (r - l + 1 > m) now[0] -= dp[r][0], now[1] -= dp[r][1], --r;
}
if (fabs(dp[0][0] - 1) < 1e-8) printf("-1\n");
else printf("%.4f\n", dp[0][1] / (1 - dp[0][0]));
return 0;
} | #pragma GCC optimize("O3") //
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define per(i, b, a) for(int i = b - 1; i >= a; i--)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef long double ld;
typedef unsigned long long ull;
unsigned seed = chrono::system_clock::now().time_since_epoch().count();
mt19937 eng(seed);
ll random2(){
return (1ll << 31ll)*eng()+eng();
}
ll n,m,k,q,T;
const ll big = 1000000007;
const ll big2 = 1000000009;
const ll mod = 998244353;
const int MAXN = 300001;
const ll TOT = 10000000000;
ll upp(ll i, ll j){
if(j == 0)return 1;
ll h = upp(i,j/2);
if(j%2 == 0)return (h*h)%big;
return (i*((h*h)%big))%big;
}
ll FAC[2001] = {0};
vl A;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("fhc.txt","r",stdin);
//freopen("autput.txt","w",stdout);
ll a,b,c,d;
cin >> n >> m;
ll sa = 0;
rep(c1,0,n){
cin >> a;
A.push_back(a);
sa += a;
}
a = n+m;
b = sa+n;
ll t = 1;
ll tt = 1;
for(ll c1 = 0; c1 < b; c1++){
t *= c1+1;
t %= big;
tt *= a-c1;
tt %= big;
}
ll ans = tt*upp(t,big-2);
cout << ans%big << "\n";
return 0;
}
|
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <string>
#include <algorithm>
#include <bitset>
#include <queue>
using namespace std;
#define ll long long
#define MOD 1000000007
#define rep(i, n) for( int i = 0; i < (n); i++ )
int main(){
int n;
cin >> n;
vector<int> a(n);
rep(i, n) cin >> a.at(i);
bool ans = true;
sort( a.begin(), a.end () );
rep(i, n){
if( a.at(i) != i + 1 ){
ans = false;
}
}
if( ans ){
cout << "Yes" << endl;
}
else{
cout << "No" << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main(void){
int N;
cin >> N;
vector<int> A(N), B(N);
for(int i = 0; i < N; i++)cin >> A[i];
for(int i = 0; i < N; i++)cin >> B[i];
int Bmin = 10000, Amax = 0;
for(int i = 0; i < N; i++){
Bmin = min(B[i], Bmin);
//cout << Bmin << endl;
}
for(int i = 0; i < N; i++){
Amax = max(A[i],Amax);
}
int Ans = Bmin-Amax;
//cout << Amax << Bmin;
if(Ans < 0){
cout << 0 << endl;
}
else{
cout << Ans+1 << endl;
}
}
|
#include <bits/stdc++.h>
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
using namespace std;
#define all(v) v.begin(), v.end()
#define ll long long
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rep_up(i, a, b) for (ll i = a; i < b; ++i)
#define rep_down(i, a, b) for (ll i = a; i > b; --i)
#define P pair<ll, ll>
#define Graph vector<vector<ll>>
#define fi first
#define se second
#define vvvvll vector<vector<vector<vector<ll>>>>
#define vvvll vector<vector<vector<ll>>>
#define vvll vector<vector<ll>>
#define vll vector<ll>
#define vvvvdo vector<vector<vector<vector<double>>>>
#define vvvdo vector<vector<vector<double>>>
#define vvdo vector<vector<double>>
#define vdo vector<double>
#define pqll priority_queue<ll>
#define pqllg priority_queue<ll, vector<ll>, greater<ll>>
constexpr ll INF = (1ll << 62);
// constexpr ll mod = 998244353;
// constexpr ll mod = 67280421310721;
constexpr ll mod = 1000000007;
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 << " ";
}
}
template <typename T>
void pt_vvll(vector<vector<T>> &v) {
ll vs = v.size();
rep(i, vs) pt_vll(v[i]);
}
ll gcd(ll a, ll b) {
if (a % b == 0) return b;
return gcd(b, a % b);
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
ll modinv(ll a, ll m) {
ll b = m, u = 1, v = 0;
while (b) {
ll t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
u %= m;
if (u < 0) u += m;
return u;
}
ll modpow(ll x, ll n) {
ll ret = 1;
while (n > 0) {
if (n & 1) ret = ret * x % mod;
x = x * x % mod;
n >>= 1;
}
return ret;
}
ll minv(ll a, ll m) { return modpow(a, m - 2); }
void make_prime(vector<ll> &ret, ll n) {
ll x = n;
for (ll i = 2; i * i <= x; i++) {
while (n % i == 0) {
n /= i;
ret.push_back(i);
}
}
if (n != 1) {
ret.push_back(n);
}
return;
}
ll M;
ll mypow(ll a, ll n) {
ll ret = 1;
rep(i, n) {
if (ret > M / a) return -1;
ret *= a;
}
return ret;
}
using pp = pair<P, ll>;
map<pp, ll> mp;
void dijkstra(vector<vector<P>> &G, ll start, vector<ll> &dist) {
priority_queue<P, vector<P>, greater<P>> que;
que.push({0, start});
ll n = G.size();
dist.assign(n, INF);
dist[start] = 0;
while (que.size()) {
P now = que.top();
que.pop();
if (dist[now.se] < now.fi) continue;
for (auto nx : G[now.se]) {
ll D = mp[{{nx.fi, now.se}, nx.se}];
ll d2 = (dist[now.se] + D - 1) / D * D;
if (chmin(dist[nx.fi], d2 + nx.se))
que.push(make_pair(dist[nx.fi], nx.fi));
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll N, M, X, Y;
cin >> N >> M >> X >> Y;
vector<vector<P>> G(N);
rep(i, M) {
ll a, b, t, k;
cin >> a >> b >> t >> k;
G[--a].push_back({--b, t});
G[b].push_back({a, t});
mp[{{a, b}, t}] = mp[{{b, a}, t}] = k;
}
vll dist;
dijkstra(G, --X, dist);
// pt_vll(dist);
if (dist[--Y] < INF)
pt(dist[Y]);
else
pt(-1);
return 0;
} | #include<bits/stdc++.h>
using namespace std;
# define ll long long
# define read read1<ll>()
# define Type template<typename T>
Type T read1(){
T t=0;
char k;
bool vis=0;
do (k=getchar())=='-'&&(vis=1);while('0'>k||k>'9');
while('0'<=k&&k<='9')t=(t<<3)+(t<<1)+(k^'0'),k=getchar();
return vis?-t:t;
}
# define fre(k) freopen(k".in","r",stdin);freopen(k".out","w",stdout)
int s,a[505],b[505],pr[505*505];bool ty;
void Swap(int x,int y){b[a[x]]=y;b[a[y]]=x;swap(a[x],a[y]);pr[++*pr]=x;ty^=1;}
void Swap2(int x){Swap(x-1,x);Swap(x,x+1);Swap(x-1,x);Swap(x,x+1),Swap(x-1,x);}
int main(){
for(int T=read;T--;){
s=read;*pr=0;ty=1;
for(int i=1;i<=s;++i)b[a[i]=read]=i;
for(int i=1;i<s-1;++i){
if(b[i]==i)continue;
if((b[i]^ty)&1)
for(int j=b[i]-1;j>=i;--j)Swap(j,j+1);
else{
int w=b[i]-1;
if(w!=i)Swap(w-1,w);
else if(w==s-3){
Swap(w+1,w+2);
Swap(w+2,w+3);
Swap(w+1,w+2);
w=s-1;
}else if(w==s-2){Swap2(i);w=i-1;}
else Swap(w+3,w+4);
for(int j=w;j>=i;--j)Swap(j,j+1);
}
}if(a[s-1]>a[s])
if((b[s-1]^ty)&1)Swap(b[s-1]-1,b[s-1]);
else Swap2(s-1);
printf("%d\n",*pr);
for(int i=1;i<=*pr;++i)printf("%d ",pr[i]);
putchar('\n');
}
return 0;
}/*
x
1 3 2 5 4
a c b
c a b
c b a
b c a
b a c
a b c
*/ |
#include <bits/stdc++.h>
#define For(i,a,b) for(int i=a;i<=b;i++)
#define Rev(i,a,b) for(int i=a;i>=b;i--)
#define clr(a,val) memset(a,val,sizeof(a))
//#define int long long
using namespace std;
const int N=3e3+5;
int n,m,head[N],to[N],nxt[N],edge[N],tot;
int dis[N],ins[N];
void add(int x,int y,int w)
{
nxt[++tot]=head[x];
head[x]=tot;
to[tot]=y;
edge[tot]=w;
}
//void dfs(int u,int d)
//{
// if(!ins[u]){
// ins[u]=1;
// if(dis[u]>d){
// dis[u]=d;
// for(int e=head[u];e;e=nxt[e]){
// int v=to[e];
// dfs(v,d+edge[e]);
// }
// }
// ins[u]=0;
// }
// else{
// assert(d>dis[u]);
// ans[u]=min(ans[u],d-dis[u]);
// }
//}
int cur,ans=1e9;
void dfs(int u)
{
for(int e=head[u];e;e=nxt[e]){
int v=to[e];
if(v==cur){
ans=min(ans,dis[u]+edge[e]);
continue;
}
if(dis[v]>dis[u]+edge[e]){
dis[v]=dis[u]+edge[e];
dfs(v);
}
}
}
priority_queue< pair<int,int> > q;
int vis[N],cc[N];
signed main()
{
cin>>n>>m;
For(i,1,n) cc[i]=1e9;
For(i,1,m){
int x,y,w;
cin>>x>>y>>w;
// if(x==y) cc[i]=min(cc[i],w);
add(x,y,w);
}
// For(i,1,n+1){
// ans[i]=1e9;
// dis[i]=1e9;
// }
// dis[n+1]=0;
//
// q.push(make_pair(0,n+1));
//
// while(q.size()){
// int u=q.top().second;
// q.pop();
//
// if(vis[u]) continue;
// vis[u]=1;
//
// for(int e=head[u];e;e=nxt[e]){
// int v=to[e];
// if(dis[v]>dis[u]+edge[e]){
// dis[v]=dis[u]+edge[e];
// q.push(make_pair(-dis[v],v));
// }
// }
// }
//
// For(i,1,n) cout<<dis[i]<<endl;
//
// dfs(n+1,0);
For(i,1,n){
clr(dis,0x3f);
cur=i;
dis[i]=0;
ans=cc[i];
dfs(i);
if(ans<1e9) cout<<ans<<endl;
else cout<<-1<<endl;
}
return 0;
}
| #include <bits/stdc++.h>
#define inf 1e9
using namespace std;
const int N = 2e3 + 101;
struct Edge{
int nex, to, dis;
}edge[N*2];
int tot, head[N], dis[N];
int dist[N][N], ans[N], vis[N];
struct node{
int u, dis;
bool operator < (const node &a) const{
return dis > a.dis;
}
};
priority_queue<node>q;
int n, m;
void add(int from,int to,int dis)
{
edge[++ tot].nex = head[from];
edge[tot].to = to;
edge[tot].dis = dis;
head[from] = tot;
}
void dij(int s)
{
for(int i = 1;i <= n; i++) dis[i] = inf, vis[i] = 0;
dis[s] = 0;q.push((node){s,0});
while(!q.empty())
{
int u = q.top().u;
q.pop();
if(vis[u]) continue;
vis[u] = 1;
for(int i = head[u];i;i = edge[i].nex)
{
int v = edge[i].to;
if(dis[v] > dis[u] + edge[i].dis)
{
dis[v] = dis[u] + edge[i].dis;
q.push((node){v,dis[v]});
}
}
}
for(int j = 1;j <= n; j++) dist[s][j] = dis[j];
}
signed main()
{
scanf("%d%d",&n,&m);
for(int i = 1;i <= n; i++)
for(int j = 1;j <= n; j++)
dist[i][j] = inf, ans[i] = inf;
for(int i = 1;i <= m; i++)
{
int u, v, w;scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
if(u == v) ans[u] = min(ans[u],w);
}
for(int i = 1;i <= n; i++) dij(i);
for(int i = 1;i <= n; i++)
for(int j = 1;j <= n; j++)
if(i != j) ans[i] = min(ans[i],dist[i][j] + dist[j][i]);
for(int i = 1;i <= n; i++)
printf("%d\n",ans[i] == inf ? -1 : ans[i]);
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
#define F(type, i, a, b, incr) for (type i = a; i <= (type)(b); i += (type)(incr))
#define pb push_back
#define ppb pop_back
#define ff first
#define ss second
#define iter(it, s) for (auto it = s.begin(); it != s.end(); it++)
#define fillc(a,b) memset(a,b,sizeof(a))
#define MOD 1000000007
#define INF 1e17
void solve()
{
ll n,i,ans=0,num;
cin>>n;
map<ll,ll> mp;
for(i=0;i<n;i++)
{
cin>>num;
mp[num]++;
}
for(auto i:mp)
ans+=i.ss*(i.ss-1)/2;
cout<<n*(n-1)/2-ans<<endl;
}
int main()
{
//freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
solve();
} | #include<bits/stdc++.h>
using namespace std;
struct node{int n,i;node(int n,int i):n(n),i(i){}};
int n,t,a;
bool b[203];
vector<int>f[203];
vector<node>s;
map<int,vector<node> >all;
void dfs(int x){
if(x==n){
int ps=0;
vector<node>y;
for(int i=0;i<n;++i)if(b[i])y.push_back(s[i]),ps+=s[i].n;
ps%=200;
if(y.size()==0)return;
if(all[ps].size()>0){
printf("Yes\n%d ",all[ps].size());
for(unsigned int i=0;i<all[ps].size();++i)printf("%d ",all[ps][i].i);
printf("\n%d ",y.size());
for(unsigned int i=0;i<y.size();++i)printf("%d ",y[i].i);
exit(0);
}
all[ps]=y;
return;
}
b[x]=false;
dfs(x+1);
b[x]=true;
dfs(x+1);
}
int main(){
scanf("%d",&n);
for(int i=0;i<n;++i){
scanf("%d",&a);
s.push_back(node(a%200,i+1));
f[a%200].push_back(i+1);
if(f[a%200].size()==2){
printf("Yes\n1 %d\n1 %d",f[a%200][0],f[a%200][1]);
return 0;
}
}
n=s.size();
dfs(0);
puts("No");
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
int main() {
long n,ans,k;
cin>>n;
ans=0;
vector<long> vec(n);
vector<long> fibo(n+2);
for(long i=0; i<n; i++){
cin>>vec.at(i);
vec.at(i)=vec.at(i)%1000000007;
}
fibo.at(0)=0;
fibo.at(1)=1;
for(long i=2; i<n+2; i++){
fibo.at(i)=fibo.at(i-1)+fibo.at(i-2);
fibo.at(i)=fibo.at(i)%1000000007;
}
ans=fibo.at(n+1)*vec.at(0);
for(int i=1; i<n; i++){
k=(fibo.at(i-1)*fibo.at(n-i+1)+fibo.at(i)*fibo.at(n-i-1))%1000000007;
ans=ans+vec.at(i)*k;
ans=ans%1000000007;
}
cout<<ans;
} | #include <cstdio>
typedef long long LL;
const int Mod = 1000000007;
int N;
int main() {
int A = 0, B = 0, C = 0, D = 0;
scanf("%d%d", &N, &A);
C = 1;
for (int i = 2; i <= N; ++i) {
int x;
scanf("%d", &x);
int nA = (int)(((LL)(C + D) * x + A + B) % Mod);
int nB = (int)(((LL)(Mod - C) * x + A) % Mod);
int nC = (C + D) % Mod;
int nD = C;
A = nA, B = nB, C = nC, D = nD;
}
printf("%d\n", (A + B) % Mod);
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define For(i,n,k) for(int i=(n);i<(k);i++)
#define ALL(a) (a).begin(),(a).end()
ll ans = 0;
void Main(){
int n;
cin >> n;
vector<int> p(n+1), place(n+1, 0), v;
vector<bool> used(n, false);
For(i,0,n) {
cin >> p[i+1];
place[p[i+1]] = i+1;
}
For(i,1,n+1){
while(place[i] > i){
int r = i;
int l = p[place[i] - 1];
swap(p[place[i]], p[place[i] - 1]);
swap(place[r], place[l]);
v.push_back(place[i]);
if(used[place[i]]){
cout << -1 << endl;
return;
}
used[place[i]] = true;
/*cout << v.back() << endl;
for(auto i:p) cout << i << " ";
cout << endl;*/
}
}
if(v.size() != n-1) cout << -1 << endl;
else for(auto i:v) cout << i << endl;
}
int main(){
Main();
/*
東方風神録は神が出てくるので当然神ゲー
*/
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll unsigned long long int
#define pb push_back
#define mp make_pair
unordered_map<ll,int> um;
ll binpow(ll a, ll n){
if(n==0) return 1;
ll res = binpow(a, n/2);
if(n%2 != 0) return res*res*a;
else return res*res;
}
void solve(){
ll n; cin>>n;
for(int i=1;i<64;i++){
ll x = binpow(5, i);
if(x>=n){
cout<<"-1"; return;
}
for(int j=1;j<64;j++){
ll y = binpow(3, j);
// if(y>=n){
// cout<<"-1"; return;
// }
if(x+y == n){
cout<<j<<" "<<i;
return;
}
if(x+y >= n) break;
}
// ll z = binpow(3, i);
// if(z > n) break;
// // if(z < n){
// ll x = n - z;
// // cout<<x<<" ";
// if(um.find(x)!=um.end()){
// cout<<i<<" "<<um[x]<<endl;
// return;
// }
// }
}
cout<<"-1\n";
}
int main(){
// for(int i=1;i<64;i++){
// ll z = binpow(5, i);
// um.insert({z, i});
// }
// while(t--){
solve();
// }
}
|
#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using ull = unsigned long long;
using vll = vector<ll>;
using vi = vector<int>;
using vchar = vector<char>;
using vstr = vector<string>;
using pll = pair<ll,ll>;
const ll mod = 1000000007;
const ll inf = 1000000000000000;
#define pi acos(-1)
#define pb push_back
#define mp make_pair
#define test int TEST;cin>>TEST;while(TEST--)
#define all(x) x.begin(),x.end()
#define vrevsort(x) sort(x.rbegin(),x.rend())
#define opy cout<<"Yes\n"
#define opn cout<<"No\n"
#define NL cout<<'\n'
#define SP cout<<' '
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 << "{";
typename vector< T > :: const_iterator it;
for( 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 << "[";
typename set< T > :: const_iterator it;
for ( 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 << "[";
typename map< F , S >::const_iterator it;
for( it = v.begin(); it != v.end(); it++ ) {
if( it != v.begin() ) os << ", ";
os << it -> first << " = " << it -> second ;
}
return os << "]";
}
#define deb(x) cerr << #x << " = " << x << endl;
void solve(){
int x,y;
cin>>x>>y;
if(abs(x-y)<3) opy;
else opn;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
//test{solve();cout<<'\n';}
solve();
}
| #include<bits/stdc++.h>
using namespace std;
#define rep(i, N) for(int i = 0; i < (long int)N; i++)
int main(){
long int N;
cin >> N;
set<long int> s;
rep(i, 2*sqrt(N))
if(N % (i+1) == 0){
s.insert(i+1);
s.insert(N / (i+1));
}
for(auto x : s) cout << x << endl;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define MOD int(1e9+7)
#define INF int(1e9+7)
#define LINF ll(1e18+7)
#define PI acos(-1)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define P pair<ll,ll>
#define chmax(x,y) (x = max(x,y))
#define chmin(x,y) (x = min(x,y))
int h,w;
char grid[15][15];
int di[]={0,1,0,1};
int dj[]={0,0,1,1};
int main(){
cin>>h>>w;
rep(i,h)rep(j,w)cin>>grid[i][j];
int ans=0;
rep(i,h)rep(j,w){
int cnt=0;
rep(ind,4){
if(grid[i+di[ind]][j+dj[ind]]=='#') cnt++;
}
ans += cnt%2;
}
cout<<ans<<endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, m;
cin >> n >> m;
vector<string> s(n);
for (int i = 0; i < n; ++i) cin >> s[i];
int ans = 0;
for (int j = 1; j < m; ++j)
if (s[0][j - 1] != s[0][j])
++ans;
for (int i = 1; i < n; ++i)
for (int j = 1; j < m; ++j)
if (s[i][j - 1] != s[i][j] && (s[i - 1][j - 1] != s[i][j - 1] || s[i - 1][j] != s[i][j]))
++ans;
for (int i = 1; i < n; ++i)
if (s[i][0] != s[i - 1][0])
++ans;
for (int j = 1; j < m; ++j)
for (int i = 1; i < n; ++i)
if (s[i - 1][j] != s[i][j] && (s[i - 1][j - 1] != s[i - 1][j] || s[i][j - 1] != s[i][j]))
++ans;
for (int i = 1; i < n; ++i)
if (s[i - 1][0] != s[i][0]) {
++ans;
break;
}
for (int i = 1; i < n; ++i)
if (s[i - 1][m - 1] != s[i][m - 1]) {
++ans;
break;
}
for (int j = 1; j < m; ++j)
if (s[0][j - 1] != s[0][j]) {
++ans;
break;
}
for (int j = 1; j < m; ++j)
if (s[n - 1][j - 1] != s[n - 1][j]) {
++ans;
break;
}
cout << ans;
} |
#include<bits/stdc++.h>
using namespace std ;
int main()
{
std::ios::sync_with_stdio(false) , cin.tie(0) ;
// [n - i + 1 , n + i]
int n ;
cin >> n ;
vector<int> a(2 * n + 1) ;
for(int i = 1 ; i <= 2 * n ; i ++) cin >> a[i] ;
long long ans = 0 ;
for(int i = 1 ; i <= 2 * n ; i ++) ans += a[i] ;
priority_queue<int> q ;
for(int i = 1 ; i <= n ; i ++)
{
q.push(-a[n - i + 1]) ;
q.push(-a[n + i]) ;
ans -= abs(q.top()) ;
q.pop() ;
}
cout << ans << '\n' ;
return 0 ;
} | // Author: Vinay Khilwani
// Language: C++
// @vok8: Codeforces, AtCoder, LeetCode, HackerEarth, TopCoder, Google, FB, CSES, Spoj, GitHub
// @vok_8: CodeChef, GFG
// @vok8_khilwani: HackerRank
// Never Stop Trying.
// Trying to be Better than Myself.
// while(true)
// {
// if(AC)
// {
// break;
// }
// else if(Contest Over)
// {
// Try.
// Check out Editorial.
// Understand.
// Find out your Mistake.
// Learn the topic (if new).
// Solve Problems on that topic (if new).
// Upsolve that problem.
// break;
// }
// else
// {
// Try.
// Use Pen-Paper.
// Find errors, edge cases, etc.
// continue;
// }
// }
// Optimizations
#pragma GCC optimize("O2")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
#pragma GCC optimize("Os")
// Libraries
#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;
// Debugging
#define dbg(a) cerr<<a<<"\n";
#define debug_a(a) for(auto x:a) {cerr<<x<<" ";} cerr<<"\n";
#define debug_b(a) for(auto x:a) {cerr<<"["<<x.first<<", "<<x.second<<"]"<<"\n";} cerr<<"\n";
#define debug_c(a) for(auto x:a) {debug_a(x)} cerr<<"\n";
#define debug_d(a) for(auto x:a) {debug_b(x)} cerr<<"\n";
#define debug_e(a) cerr<<"["<<a.first<<", "<<a.second<<"]"<<"\n";
// Defines
#define fast ios_base::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL);
#define loop(i,a,n) for(int i=a; i<n; i++)
#define rloop(i,a,n) for(int i=a; i>=n; i--)
#define fr(i,a,n,b) for(int i=a; i<n; i+=b)
#define rfr(i,a,n,b) for(int i=a; i>=n; i-=b)
#define IN cin>>
#define OUT cout<<
#define nl "\n"
#define sz(a) int(a.size())
#define all(a) (a).begin(),(a).end()
#define each(a,b) for(auto &a:b)
#define pb push_back
#define set_bits(a) __builtin_popcountll(a)
#define ar array
#define write(a) for(auto x:a) {OUT x<<" ";} OUT nl;
#define read(a) for(auto &x:a) {IN x;}
// #define oset tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>
using ll=long long int;
using ld=long double;
using pll=pair<ll,ll>;
using pii=pair<int,int>;
using vll=vector<ll>;
using vi=vector<int>;
const ll mod=(ll)(1e9)+7LL;
const ll M=998244353LL;
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};
const ld pi=acos(-1);
// General Functions
ll gcd(ll a, ll b)
{
return (b?gcd(b,a%b):a);
}
ll P(ll B, ll power, ll modulo) //Fast Power
{
ll ans=1LL;
while(power>0LL)
{
if(power%2LL==1LL)
{
ans=(ans*B)%modulo;
}
B=(B*B)%modulo;
power/=2LL;
}
return ans;
}
bool isPrime(ll n)
{
if(n<=1LL)
{
return false;
}
if(n<=3LL)
{
return true;
}
if(n%2==0LL || n%3==0LL)
{
return false;
}
for(ll i=5LL; (i*i)<=n; i+=6LL)
{
if(n%i==0LL || n%(i+2LL)==0LL)
{
return false;
}
}
return true;
}
void vok()
{
fast
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
freopen("error.txt","w",stderr);
#endif
}
// Global Variables
const int mxN=int(1e5)+100;
// Solver Function(s)
void solve()
{
int a,b;
IN a>>b;
int x=a+b;
x/=2;
int y=a-x;
OUT x<<" "<<y<<nl;
}
// Main Function
int main()
{
vok();
int t=1;
// IN t;
while(t--)
{
solve();
}
return 0;
} |
#include<bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define M (L+R)/2
#define N 500009
#define inf 99999999999
#define ll long long
#define mod 1000000007
#define sz size();
using namespace std;
ll n,x,y,ans,sum;
string s;
int main(){
ios::sync_with_stdio(0);
cin>>n;
cin>>s;
for(int i=0;i<n;i++){
x=y=0;
for(int j=i;j<n;j++){
if(s[j]=='A')x++;
if(s[j]=='T')x--;
if(s[j]=='G')y++;
if(s[j]=='C')y--;
if(x==y && x==0)ans++;
}
}
cout<<ans<<endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)
#define ALL(v) (v).begin(),(v).end()
#define CLR(t,v) memset(t,(v),sizeof(t))
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";}
template<class T>void chmin(T&a,const T&b){if(a>b)a=b;}
template<class T>void chmax(T&a,const T&b){if(a<b)a=b;}
#ifdef LOCAL
template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cerr<<(*i)<<" ";cerr<<endl;}
#else
template<class T>void pv(T a,T b){}
#endif
ll nextLong() { ll x; scanf("%lld", &x); return x;}
const int MAX_N = 5010;
int c[4][MAX_N];
int main2() {
CLR(c, 0);
int N = nextLong();
string S; cin >> S;
REP(i, N) {
c[0][i+1] = c[0][i] + (S[i] == 'A');
c[1][i+1] = c[1][i] + (S[i] == 'T');
c[2][i+1] = c[2][i] + (S[i] == 'G');
c[3][i+1] = c[3][i] + (S[i] == 'C');
}
ll ans = 0;
REP(i, N) {
for (int j = i + 1; j <= N; j++) {
int d[4] = {0};
REP(k, 4) d[k] = c[k][j] - c[k][i];
if (d[0] == d[1] && d[2] == d[3]) ans++;
}
}
cout << ans << endl;
return 0;
}
int main() {
#ifdef LOCAL
for (;!cin.eof();cin>>ws)
#endif
main2();
return 0;
}
|
#define rep(i, n) for(int i=0; i<(n); ++i)
#define rrep(i, n) for(int i=(n-1); i>=0; --i)
#define rep2(i, s, n) for(int i=s; i<(n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define memr(dp, val) memset(dp, val, sizeof(dp))
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<class T> inline T int_ceil(T a, T b) { T res = a / b; if(a % b != 0) res++; return res; }
#include <bits/stdc++.h>
using namespace std;
template<typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T> >;
typedef long long ll;
static const int INTINF = (INT_MAX >> 1); // 10^9 + 10^7
static const ll LLINF = (LLONG_MAX >> 1);
// sort(ALL(v), [](auto const& lhs, auto const& rhs) { return lhs > rhs; /* 左の方が大きい...というイメージ。*/ });
// --------------------------------------------------------------------------------------------- //
static const int MAX = 100001;
static const ll MOD = 1000000007;
int main(int argc, const char * argv[]) {
std::cout << std::fixed << std::setprecision(15);
ll N; cin >> N;
ll ans1=0, ans2=0, ans3=0;
rep(i, N){
ll x; cin >> x;
ans1 += abs(x);
ans2 += x * x;
ans3 = max(ans3, abs(x));
}
double ans2_d = sqrt((double)ans2);
cout << ans1 << endl;
cout << ans2_d << endl;
cout << ans3 << endl;
return 0;
}
| #include<bits/stdc++.h>
#define rep(i, n) for (int64_t i = 0; i < (int64_t)(n); i++)
#define all(v) v.begin(), v.end()
using ll = long long;
using namespace std;
int main(){
int n;cin>>n;
vector<int> a(n),b(n);
int ans=1e10;
rep(i,n)cin>>a[i]>>b[i];
rep(i,n){
rep(j,n){
if(i==j)ans=min(ans,a[i]+b[j]);
else ans=min(ans,max(a[i],b[j]));
}
}
cout<<ans;
}
|
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int N, X0, Y0, XN2, YN2;
cin >> N >> X0 >> Y0 >> XN2 >> YN2;
double xo, yo;
xo = (double)(X0 + XN2) / 2;
yo = (double)(Y0 + YN2) / 2;
double X0d, Y0d;
X0d = X0 - xo;
Y0d = Y0 - yo;
double pi = 3.1415926535;
double p = pi / (N / 2);
double x1d, y1d;
x1d = X0d * cos(p) - Y0d * sin(p);
y1d = X0d * sin(p) + Y0d * cos(p);
double x1, y1;
x1 = x1d + xo;
y1 = y1d + yo;
cout << x1 << " " << y1 << endl;
return 0;
} | #line 2 "/Users/tiramister/CompetitiveProgramming/Cpp/CppLibrary/Graph/dijkstra.hpp"
#line 2 "/Users/tiramister/CompetitiveProgramming/Cpp/CppLibrary/Tools/heap_alias.hpp"
#include <queue>
template <class T>
using MaxHeap = std::priority_queue<T>;
template <class T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;
#line 2 "/Users/tiramister/CompetitiveProgramming/Cpp/CppLibrary/Graph/graph.hpp"
#include <vector>
template <class Cost = int>
struct Edge {
int src, dst;
Cost cost;
Edge() = default;
Edge(int src, int dst, Cost cost = 1)
: src(src), dst(dst), cost(cost){};
bool operator<(const Edge<Cost>& e) const { return cost < e.cost; }
bool operator>(const Edge<Cost>& e) const { return cost > e.cost; }
};
template <class Cost = int>
struct Graph : public std::vector<std::vector<Edge<Cost>>> {
using std::vector<std::vector<Edge<Cost>>>::vector;
void span(bool direct, int src, int dst, Cost cost = 1) {
(*this)[src].emplace_back(src, dst, cost);
if (!direct) (*this)[dst].emplace_back(dst, src, cost);
}
};
#line 5 "/Users/tiramister/CompetitiveProgramming/Cpp/CppLibrary/Graph/dijkstra.hpp"
template <class Cost>
std::vector<Cost> dijkstra(const Graph<Cost>& graph, int s) {
std::vector<Cost> dist(graph.size(), -1);
dist[s] = 0;
MinHeap<std::pair<Cost, int>> que;
que.emplace(0, s);
while (!que.empty()) {
auto [d, v] = que.top();
que.pop();
if (d > dist[v]) continue;
for (const auto& e : graph[v]) {
if (dist[e.dst] != -1 &&
dist[e.dst] <= dist[v] + e.cost) continue;
dist[e.dst] = dist[v] + e.cost;
que.emplace(dist[e.dst], e.dst);
}
}
return dist;
}
#line 2 "main.cpp"
#include <iostream>
using namespace std;
constexpr int N = 100;
int enc(int x, int t) { return x + t * N; }
void solve() {
int a, b, x, y;
cin >> a >> b >> x >> y;
int s = enc(--a, 0), g = enc(--b, 1);
Graph<int> graph(N * 2);
for (int i = 0; i < N; ++i) {
graph.span(false, enc(i, 0), enc(i, 1), x);
if (i + 1 < N) {
graph.span(false, enc(i + 1, 0), enc(i, 1), x);
for (int t = 0; t < 2; ++t) {
graph.span(false, enc(i, t), enc(i + 1, t), y);
}
}
}
cout << dijkstra(graph, s)[g] << "\n";
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define scan(a,n) for(long long int index=0;index<n;index++) cin>>a[index];
#define print(a,n) for(long long int index=0;index<n;index++) cout<<a[index]<<" ";cout<<endl;
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define ss second
#define ff first
#define vli vector<long long int>
#define vlli vector<pair<long long int,long long int>>
#define vsi vector<string>
#define all(n) n.begin(),n.end()
#define forn(i,a,b) for(i=a;i<b;i++)
ll mul(ll x,ll y){ return (x*y)%mod;}
ll power(ll x, ll y) {ll res = 1; x %= mod;
while (y) {if (y & 1)res = mul(res, x); y >>= 1; x = mul(x, x);} return res;}
ll mod_inv(ll x) {return power(x, mod - 2);}
ll fact(ll n) {ll res = 1; for (ll i = 2; i <= n; i++) res = mul(res , i); return res; }
ll nCr(ll n, ll r) { return mul(fact(n),mod_inv(mul(fact(r),fact(n - r))));}
const int mxn =100000;
vli v[mxn];
ll c[mxn]={};
bool ans[mxn]={},vis[mxn]={};
map<int,int> m;
void dfs(ll x){
if(m[c[x]]) ans[x]=1;
m[c[x]]++;;
vis[x]=1;
ll i;
for(i=0;i<v[x].size();i++){
if(vis[v[x][i]]==0){
dfs(v[x][i]);
}
}
m[c[x]]--;
}
int main() {
fast;
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
ll t=1;
// cin>>t;
while(t--){
ll n;
cin>>n;
scan(c,n);
ll x,y,i;
for(i=0;i<n-1;i++){
cin>>x>>y;
x--;y--;
v[x].pb(y);
v[y].pb(x);
}
dfs(0);
for(i=0;i<n;i++){
if(!ans[i]) cout<<i+1<<endl;
}
}
return 0;
} | //author : ainunsy1
#include <bits/stdc++.h>
#define forn(i,a,b) for(int i=a; i<=b; i++)
#define ford(i,a,b) for(int i=a; i>=b; i--)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
typedef long long ll;
vector<ll>v[100005];
ll n,c[100005];
bool lewat[100005],ans[100005];
int udh[100005];
void dfs(ll b){
lewat[b]=true;
if(udh[c[b]])ans[b]=true;
udh[c[b]]++;
for(auto d:v[b]){
if(!lewat[d]) dfs(d);
}
udh[c[b]]--;
}
void solve(){
cin>>n;
forn(i,1,n) cin>>c[i];
forn(i,1,n-1){
int a,b;
cin>>a>>b;
v[a].push_back(b);
v[b].push_back(a);
}
dfs(1);
forn(i,1,n){
if(!ans[i]) cout<<i<<endl;
}
}
int main(){
ios::sync_with_stdio(0); cin.tie(0);
int t=1;
// cin>>t;
forn(i,1,t){
solve();
}
} |
#include<bits/stdc++.h>
using namespace std;
#define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define int long long int
#define fi first
#define se second
#define pub push_back
#define pi pair<int,int>
#define all(v) (v).begin(), (v).end()
#define rep(i, l, r) for(int i=(int)(l);i<(int)(r);i++)
#define repd(i, l, r) for (int i=(int)(l);i>=(int)(r);i--)
#define clrg(i, l, r) for(int i=(int)(l);i<(int)(r);i++)vis[i]=0,v[i].clear();
int power(int x, unsigned int y){int res = 1;while (y > 0){ if (y & 1){res = res*x;} y = y>>1;x = x*x;}return res;}
int powermod(int x, unsigned int y, int p){int res = 1;x = x % p;while (y > 0){if (y & 1){res = (res*x) % p;}y = y>>1; x = (x*x) % p;}return res;}
#define print2d(mat,n,m){for(int i=0;i<(int)(n);i++){for(int j=0;j<(m);j++){cout<<mat[i][j]<<" ";}cout<< endl;}}
#define clr(a,x) memset(a,x,sizeof(a))
#define rr(v) for(auto &val:v)
#define print(v) for (const auto itr : v){cout<<itr<<' ';}cout<<"\n";
#define ln length()
#define sz size()
#define mod 1000000007
#define elif else if
int32_t main(){
double x1,x2,y1,y2;
cin>>x1>>y1>>x2>>y2;
if(x1>x2)swap(x1,x2),swap(y1,y2);
double ans=(y1*(x2-x1))/(y1+y2);
cout<<fixed<<setprecision(10);
cout<<x1+ans<<"\n";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int solve() {
int n;
string s;
cin >> n >> s >> s >> s;
cout << string(n, '0') << string(n, '1') << "0\n";
return 0;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T = 1;
cin >> T;
while (T--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define f1 first
#define s2 second
#define fastio ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define debug(x...) cerr << "[" << #x << "]: " << x << "\n";
using ll = long long;
using ld = long double;
using ii = pair<int, int>;
using pl = pair<ll, ll>;
ld const PI = 4*atan((ld)1);
int main()
{
fastio;
int n;
cin >> n;
vector<ll> a(n), b(n);
for (ll &x : a)
cin >> x;
for (ll &x : b)
cin >> x;
ll ma = 0, res = 0;
for (int i = 0; i < n; ++i)
{
ma = max(ma, a[i]);
res = max(res, ma * b[i]);
cout << res << '\n';
}
return 0;
}
| // Artur Kraska, II UWr
#include <bits/stdc++.h>
#define forr(i, n) for(int i=0; i<n; i++)
#define FOREACH(iter, coll) for(auto iter = coll.begin(); iter != coll.end(); ++iter)
#define FOREACHR(iter, coll) for(auto iter = coll.rbegin(); iter != coll.rend(); ++iter)
#define lbound(P,R,PRED) ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;})
#define testy() int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests)
#define CLEAR(tab) memset(tab, 0, sizeof(tab))
#define CONTAIN(el, coll) (coll.find(el) != coll.end())
#define FOR(i, a, b) for(int i=a; i<=b; i++)
#define FORD(i, a, b) for(int i=a; i>=b; i--)
#define MP make_pair
#define PB push_back
#define ff first
#define ss second
#define deb(X) X;
#define M 1000000007
#define INF 1000000007LL
using namespace std;
int n, m, a;
long long t1[1000007], t2[1000007], m1[1000007];
int main()
{
scanf("%d", &n);
FOR(i, 1, n)
{
scanf("%lld", &t1[i]);
m1[i] = max(m1[i-1], t1[i]);
}
long long res = 0;
FOR(i, 1, n)
{
scanf("%lld", &t2[i]);
res = max(res, t2[i]*m1[i]);
printf("%lld\n", res);
}
return 0;
}
|
#include <iostream>
#include <vector>
using namespace std;
struct cond {
int X, Y, Z;
};
bool IsOneBit(long long bits, int i) {
return (bits & (1ll << i)) > 0;
}
int main() {
int N, M;
cin >> N >> M;
vector<cond> Cond(M);
for (auto& [X, Y, Z] : Cond) cin >> X >> Y >> Z;
int PN = 1 << N;
vector<long long> DP(PN);
DP[0] = 1;
for (int bit = 0; bit < PN; bit++) {
int one = 0;
for (int i = 0; i < N; i++) one += IsOneBit(bit, i);
bool isOK = true;
for (auto& [X, Y, Z] : Cond) {
if (one > X) continue;
int sum = 0;
for (int j = 0; j < Y; j++) sum += IsOneBit(bit, j);
if (sum > Z) isOK = false;
}
if (isOK) {
for (int i = 0; i < N; i++) {
if (IsOneBit(bit, i)) DP[bit] += DP[bit ^ (1 << i)];
}
}
else DP[bit] = 0;
}
cout << DP[PN - 1] << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)
#define rrep(i,n) RREP(i,(n)-1,0)
#define all(v) v.begin(), v.end()
#define endk '\n'
const int inf = 1e9+7;
const ll longinf = 1LL<<60;
const ll mod = 1e9+7;
const ll mod2 = 998244353;
const ld eps = 1e-10;
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;}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, m; cin >> n >> m;
vector<int> X(m), Y(m), Z(m);
vector<vector<pair<int, int>>> A(n+1);
rep(i, m) {
cin >> X[i] >> Y[i] >> Z[i];
Y[i]--;
A[X[i]].push_back({Y[i], Z[i]});
}
vector<ll> dp(1<<n);
dp[0] = 1;
rep(i, 1<<n) {
rep(j, n) {
if((i>>j)&1) continue;
vector<int> cnt(n);
int ne = i^(1<<j);
rep(k, n) {
if((ne>>k)&1) cnt[k]++;
}
rep(k, n) if(k) cnt[k] += cnt[k-1];
bool ok = true;
int sz = __builtin_popcount(ne);
for(auto [y, z]: A[sz]) {
if(cnt[y] > z) ok = false;
}
if(ok) dp[ne] += dp[i];
}
}
cout << dp[(1<<n)-1] << endk;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std ;
#define FASTIO ios::sync_with_stdio(0) ; cin.tie(0) ; cout.tie(0) ;
#define int long long
#define mod ((int)1e9 + 7)
void solve(){
int n , ans = 0 ; cin >> n ;
vector<int> a(n) ;
for(int& i : a) cin >> i ;
for(int i = 0 ; i < n ; ++i){
int x ; cin >> x ;
ans += a[i] * x ;
}
cout << ((ans == 0) ? "Yes" : "No") << "\n" ;
}
signed main(){
FASTIO
int T = 1 ;
// cin >> T ;
while(T--) solve() ;
return 0 ;
} | #include <bits/stdc++.h>
using namespace std;
using i64 = unsigned long long;
#define rep(i, n) for (i64 i = 0; i < (n); i++)
#define IN_i64(n) \
i64 n; \
cin >> n;
#define IN_veci64(a, n) \
vector<i64> a((n)); \
rep(i, n) cin >> a[i];
constexpr i64 INF64 = 1LL << 62LL;
inline i64 modpow(i64, i64, i64 = INF64);
inline i64 gcd(i64, i64);
template <class T> inline bool chmax(T &a, T b);
template <class T> inline bool chmin(T &a, T b);
int main()
{
IN_i64(N);
IN_i64(K);
rep(i,K){
if (N%200==0) N/=200;
else{
N*=1000;
N+=200;
}
}
cout<<N<<endl;
}
i64 modpow(i64 base, i64 exp, i64 mod)
{
i64 ans = 1;
while (exp > 0)
{
if (exp % 2 == 1)
{
ans *= base;
ans %= mod;
}
exp /= 2;
base *= base;
base %= mod;
}
return ans;
}
i64 gcd(i64 a, i64 b)
{
if (b == 0) return a;
return gcd(b, a % b);
}
template <class T> inline bool chmax(T &a, T b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
template <class T> inline bool chmin(T &a, T b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define sp(y) fixed<<setprecision(y)
#define w(t) int t;cin>>t;while(t--)
//#define pi 2*acos(0.0)
#define bg begin()
#define en end()
#define all(x) x.begin(),x.end()
#define sortarr(x) sort(x,x+arrsize(x))
#define arrsize(x) sizeof(x)/sizeof(x[0])
#define sortall(x) sort(all(x))
#define formn(m,n) for(m;m<=n;m++)
#define mp make_pair
#define pb push_back
#define vi vector<ll>
#define vs vector<string>
#define REP(i,a,b) for (auto i=a;i<=b;i++)
#define sq(a) (a)*(a)
ll pow(ll c,ll d){return d==0?:c*pow(c,d-1);}
ll gcd(ll a,ll b) {return b==0? a:gcd(b,a%b);}
ll lcm(ll a,ll b) {return ((a*b)/gcd(a,b));}
//fast I/o;
void fastIO(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
void init_code(){
fastIO();
}
//solution
#define mx 1000005
#define m7 1000000007
int prime[mx];
int arr[mx];
void seive(){
prime[0]=1;
prime[1]=1;
for(ll i=2;i<mx;i++){
if(prime[i]==0){
for(ll j=i*i;j<mx;j+=i){
prime[j]=1;
}
}
}
}
int solve(){
ll a,b,c,d;
ll blue,red;
cin>>a>>b>>c>>d;
red = 0;
blue =a;
if((c*d - b) <=0){
cout<<-1<<endl;
}else{
REP(i,1,a){
blue+=b;
red+=c;
if(blue<=d*red){
cout<<i<<endl;
return 0;
}
}
}
return 0;
}
//end solution
int main(){
init_code();
solve();
}
| #include <iostream>
#include <cmath>
#include <iostream>
namespace {
int solve(const int a, const int b, const int c, const int d) {
int tmp = c * d - b;
if (tmp > 0) {
return static_cast<int>(std::ceil(static_cast<double>(a) / tmp));
}
return -1;
}
} // namespace
int main() {
int a, b, c, d;
std::cin >> a >> b >> c >> d;
std::cout << solve(a, b, c, d) << std::endl;
return EXIT_SUCCESS;
}
|
// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#include <bits/stdc++.h>
#define ll long long int
#define vi vector<int>
#define vvi vector<vector<int>>
#define vll vector<long long>
#define vs vector<string>
#define vc vector<char>
#define vb vector<bool>
#define forn(i, s, n) for(ll i=(ll)s; i<(ll)(n); i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define pll pair<long long int, long long int>
#define pii pair<int, int>
#define pss pair<string, string>
#define ull unsigned long long int
#define lld long double
#define F first
#define S second
#define PI 3.141592653589793238
#define prec(n) fixed<<setprecision(n)
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define itsval(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); itval(_it, args); }
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
void itval(istream_iterator<string> it) {}
template<typename T, typename... Args>
void itval(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
itval(++it, args...);
}
const ll MOD = 1e9 + 7;
template <typename T>
inline void print(T x) {cout << x << "\n";}
template <typename T>
inline void printvec(T x) {for (auto a : x) cout << a << ' '; cout << '\n';}
// -----------------------------------------------------------------------
struct custom {
bool operator()(const pll &p1, const pll &p2)const {
return p1.F < p2.F;
}
};
// Calculate a^b % MOD -------------------------------------------------
ll get_pow(ll a, ll b) {
ll res = 1;
while (b) {
if (b & 1) res = (res * a) % MOD;
a = (a * a) % MOD;
b >>= 1;
}
return res;
}
// ---------------------------------------------------------------------
const ll N = 2e5 + 5, inf = 2e18;
void solve()
{
ll n, q;
cin >> n >> q;
vll v(n);
forn(i, 0, n) cin >> v[i];
ll x;
forn(i, 0, q)
{
cin >> x;
ll lo = 0, hi = inf, mid, d;
while (lo + 1 < hi)
{
mid = (lo + hi) >> 1;
d = upper_bound(all(v), mid) - v.begin();
if (mid - d > x - 1)
hi = mid;
else
lo = mid;
}
cout << hi << '\n';
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int test = 1;
//cin >> test;
clock_t z = clock();
forn(tes, 0, test)
{
solve();
}
debug("Total Time:%.4f\n", (double)(clock() - z) / CLOCKS_PER_SEC);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAXN = 1e6, M = 1e9+7, inf = -2e9;
// #define runcase LOL
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ull> vul;
typedef pair<int, int> pi;
typedef vector<pi> vpi;
typedef vector<array<int, 2>> var2;
typedef vector<array<int, 3>> var3;
typedef pair<ll, ll> pl;
typedef vector<pl> vpl;
#define FOR(j, V) for(int j = 0; j < V; j++)
#define DOR(tt, X) for(int tt = X; tt >= 0; --tt)
#define pb push_back
#define all(X) X.begin(),X.end()
#define sz(X) int(X.size())
// template insertion spaces
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <iterator>
using namespace __gnu_pbds;
template <typename T> using oset = tree<T, null_type, std::less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
void solve(int test_case = 0)
{
int N, Q;
cin >> N >> Q;
vector<int64_t> A(N);
for(int64_t &i : A) {
cin >> i;
}
for(int qq = 0; qq < Q; qq++) {
int64_t K; cin >> K;
int64_t l = 1, r = 2e18;
while(l < r) {
int64_t mid = l + (r - l) / 2;
int64_t below = lower_bound(A.begin(), A.end(), mid) - A.begin();
below++;
if(!binary_search(A.begin(), A.end(), mid)) --below;
if(mid - below >= K) {
r = mid;
} else {
l = mid + 1;
}
}
cout << l << "\n";
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
// precalc();
int T = 1;
#ifdef runcase
cin >> T;
#endif
for(int nt = 0, i; nt < T; nt++) {
solve(nt);
++i;
}
}
|
// RAKSHIT KADAM
#include <bits/stdc++.h>
// #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>
// find_by_order returns iterator to element present at that index...
// order_of_key returns no. of elements having value stricktly less than that given in the order_of_key();
#warning !!!check the size of arrayss!!!
// NUMERIC DEFINITIONS :
#define inf 1000000003
#define MOD 1000000007
#define newMOD 998244353
#define MAX 200006
#define pi 3.1415926535897932384626433832795
#define ld long double
#define ll long long
#define int long long
#define INF 1000000000000000008
#define Need_for_speed(activated) ios_base :: sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
#define satisfy cerr<<"\n\n---------------Running on multiple test cases--------------"<<endl;ll t; cin>> t; while(t--)
#define mp make_pair
#define pb push_back
#define endl "\n"
#define x first
#define y second
#define Endl endl
#define pii pair<ll,ll>
#define input freopen("input.txt", "r", stdin),freopen("output.txt", "w", stdout)
#define time cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
using namespace std;
ll gcd(ll a, ll b){if (b == 0)return a;return gcd(b, a % b);}
int power(int x, unsigned int y, unsigned int m){ if (y == 0) return 1;int p = power(x, y/2, m) % m; p = (p * p) % m;return (y%2 == 0)? p : (x * p) % m;}
ll modInverse(int a, int m){{ return power(a, m-2, m);}}
const int maxn = 500004;
//ll fact[300007] = {0};
//void facto() {fact[0] = 1; fact[1] = 1; for(int i = 2; i < 300005; i++){fact[i] = (fact[i-1]*i)%MOD;}}
//ll ncr(ll n, ll r)
//{
// if(r>n)return 0;
// ll res = 1;
// res = fact[n];
// res*=modInverse(fact[r],MOD);
// res%=MOD;
// res*=modInverse(fact[n-r],MOD);
// res%=MOD;
// return res;
//}
//vector<ll>primes;
void solve()
{
double x,y,z;
cin>>x>>y>>z;
ll i=0;
while(y/x>(double(i))/z)i++;
cout<<--i<<endl;
}
signed main()
{
Need_for_speed(activated);
// satisfy
// { solve();
// }
solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define BE(x) x.begin(), x.end()
int main() {
double x, y, z;
cin >> x >> y >> z;
double a = y*z / x;
a -= 0.001;
int ans = a;
cout << ans << endl;
} |
#include <bits/stdc++.h>
#include <cstring>
using namespace std;
typedef long long ll;
#define F0R(i, x, y) for (int i = x; i < y; i++)
#define FOR(i, n) F0R(i, 0, n)
#define F0RD(i, x, y) for (int i = y - 1; i >= x; i--)
#define FORD(i, n) F0RD(i, 0, n)
#define F first
#define S second
int mp(int x, int c) {
return 1000 * c + x;
}
vector<int> adj[2005];
bool vis[2005];
void dfs(int v) {
vis[v] = true;
for (int to : adj[v]) {
if (!vis[to]) dfs(to);
}
}
int main() {
#ifdef mikey
freopen("a.in", "r", stdin);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
int n, m; cin >> n >> m;
auto ad = [&] (int a, int b, int c, int d) {
adj[mp(a, b)].push_back(mp(c, d));
adj[mp(c, d)].push_back(mp(a, b));
};
ad(0, 0, 0, 1);
ad(0, 0, m - 1, 1);
ad(n - 1, 0, 0, 1);
ad(n - 1, 0, m - 1, 1);
FOR(i, n) {
string s; cin >> s;
FOR(j, m) {
if (s[j] == '#') {
ad(i, 0, j , 1);
}
}
}
int rcc = 0;
FOR(i, n) {
if (!vis[mp(i, 0)]) {
dfs(mp(i, 0));
++rcc;
}
}
memset(vis, false, sizeof(vis));
int ccc = 0;
FOR(i, m) {
if (!vis[mp(i, 1)]) {
dfs(mp(i, 1));
++ccc;
}
}
cout << min(rcc - 1, ccc - 1) << '\n';
}
| #include <bits/stdc++.h>
using namespace std;
int h,w;
string s[1003];
int f[2003];
int F(int x){
return f[x]==x?x:f[x]=F(f[x]);
}
int main(){
cin>>h>>w;
for(int i=0;i<h;i++)cin>>s[i];
s[0][0]=s[0][w-1]=s[h-1][0]=s[h-1][w-1]='#';
for(int i=0;i<h+w;i++)f[i]=i;
for(int i=0;i<h;i++)for(int j=0;j<w;j++)
if(s[i][j]=='#'){
int x=F(i),y=F(j+h);
if(x!=y)f[x]=y;
}
unordered_map<int,bool>R,C;
for(int i=0;i<h;i++)R[F(i)]=1;
for(int i=0;i<w;i++)C[F(i+h)]=1;
cout<<min((int)R.size(),(int)C.size())-1;
} |
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
int n,m,ans;
vector<int> adj[400001];
vector<int> cur;
bool vis[400001];
void dfs(int x){
vis[x]=true;
cur.pb(x);
for (auto i:adj[x]){
if (vis[i]) continue;
dfs(i);
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>m;
n=400000;
for (int i=1;i<=m;i++){
int a,b;
cin>>a>>b;
adj[a].pb(b);
adj[b].pb(a);
}
for (int i=1;i<=n;i++){
if (vis[i] || adj[i].empty()) continue;
cur.clear();
dfs(i);
int curnum=cur.size(),edges=0;
for (auto j:cur){
edges+=adj[j].size();
}
edges/=2;
ans+=min(curnum,edges);
}
cout<<ans;
} | /*BISMILLAH
THE WHITE WOLF
NO DREAM IS TOO BIG AND NO DREAMER IS TOO SMALL*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<long long> vll;
typedef vector<int> vi;
#define io ios_base::sync_with_stdio(false)
#define pb push_back
#define isSet(n, i) bool(n & (1LL << i))
#define Set(n, i) (n | (1LL << i))
#define unSet(n, i) (n & !(1LL << i))
#define PI 2 * acos(0.0)
#define all(r) (r).begin(), (r).end()
#define dbg(a) cout << #a << " ->->->-> " << a << "\n"
#define inf 100000000000000000
#define mod 1000000007
#define N 100009
int dirx[] = {1, -1, 0, 0, 1, 1, -1, -1}, diry[] = {0, 0, 1, -1, 1, -1, 1, -1};
//=============================================ASIFAZAD==============================================//
int n, ans[N], colors[N], cur[N];
vi adj[N];
void dfs(int nod, int par)
{
if (cur[colors[nod]])
ans[nod] = -1;
else
ans[nod] = 1;
cur[colors[nod]]++;
for (auto x : adj[nod])
if (x != par)
dfs(x, nod);
cur[colors[nod]]--;
}
int main()
{
io;
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d", &colors[i]);
for (int i = 0; i < n - 1; i++)
{
int u, v;
scanf("%d%d", &u, &v);
u--;
v--;
adj[u].pb(v);
adj[v].pb(u);
}
dfs(0, -1);
for (int i = 0; i < n; i++)
if (ans[i] == 1)
printf("%d\n", i + 1);
return 0;
} |
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
//#include<boost/multiprecision/cpp_int.hpp>
//#include<boost/multiprecision/cpp_dec_float.hpp>
//namespace mp=boost::multiprecision;
//#define mulint mp::cpp_int
//#define mulfloat mp::cpp_dec_float_100
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
//constexpr int MOD=1000000007;
constexpr int MOD=998244353;
#define INF (1<<30)
#define LINF (lint)(1LL<<56)
#define endl "\n"
#define rep(i,n) for(lint (i)=0;(i)<(n);(i)++)
#define reprev(i,n) for(lint (i)=(n-1);(i)>=0;(i)--)
#define Flag(x) (1<<(x))
#define Flagcount(x) __builtin_popcountll(x)
#define pint pair<int,int>
#define pdouble pair<double,double>
#define plint pair<lint,lint>
#define fi first
#define se second
typedef long long lint;
int dx[8]={1,1,0,-1,-1,-1,0,1};
int dy[8]={0,1,1,1,0,-1,-1,-1};
const int MAX_N=2e5+5;
//struct edge{lint to,num;};
//vector<int> bucket[MAX_N/1000];
lint powmod(lint a,lint b){
return b?powmod(a*a%MOD,b/2)*(b%2?a:1)%MOD:1;
}
int main(void){
int N;
cin >> N;
if(N%2) cout << "Black" << endl;
else cout << "White" << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define N 100005
#define MOD 1000000007
#define fo(i, b, n) for (long i = b; i < n; ++i)
#define rfo(i, b, n) for (long i = b; i >= n; --i)
#define all(ar) ar.begin(), ar.end()
#define rall(ar) ar.rbegin(), ar.rend()
#define mem(ar, val) memset(ar, val, sizeof(ar))
#define fi first
#define se second
#define pb push_back
#define fastIO \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.precision(12);
typedef long long ll;
typedef pair<long, long> pll;
typedef pair<ll, ll> plll;
typedef vector<int> vi;
typedef vector<long> vl;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<vl> vvl;
typedef vector<vll> vvll;
const double PI = 3.141592653589793238;
const ll oo = 1e18;
void solve()
{
ll n;
string s;
cin >> n >> s;
if (s[0] != s[n - 1])
{
cout << "1";
return;
}
for (long i = 2; i < n - 1; ++i)
{
if (s[i] != s[n - 1] && s[i - 1] != s[0])
{
cout << "2";
return;
}
}
cout << "-1";
}
signed main()
{
fastIO;
solve();
return 0;
} |
#include "bits/stdc++.h"
using namespace std;
//#include "testlib.h"
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
#define int long long
#define ll long long
#define M 1000000007
#define MM 998244353
#define inputarr(a,n) for(int i=0;i<n;++i) cin>>a[i]
#define GCD(m,n) __gcd(m,n)
#define LCM(m,n) m*(n/GCD(m,n))
#define mii map<ll ,ll >
#define msi map<string,ll >
#define rep(a,b) for(ll i=a;i<b;i++)
#define rep0(n) for(ll i=0;i<n;i++)
#define repi(i,a,b) for(ll i=a;i<b;i++)
#define pb push_back
#define vi vector<ll>
#define vs vector<string>
#define ppb pop_back
#define endl '\n'
#define asdf ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define r0 return 0;
#define FORD(i, a, b) for (int i = (int) (a); i >= (int) (b); --i)
#define inputoutput freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
#define Set(a, s) (a, s, sizeof (a))
#define FOR repi
#define vii vector<pii>
#define pii pair<int,int>
#define REVERSE(v) reverse(all(v))
#define trav(a, x) for(auto& a : x)
#define display(x) trav(a,x) cout<<a<<" ";cout<<endl
#define debug cerr<<"bhau"<<endl
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
std::cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');std::cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
template<typename T, typename U> static inline void amin(T &x, U y)
{
if (y < x)
x = y;
}
template<typename T, typename U> static inline void amax(T &x, U y)
{
if (x < y)
x = y;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
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);
}
};
ll max(ll a, ll b) { return (a > b)? a : b;}
int min(int a, int b) { return (a < b)? a : b;}
int solve(){
int n, k;
cin >> n >> k;
int ans = 0;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= k; j++){
ans += i * 100 + j;
}
}
cout << ans;
return 0;
}
signed main(){
asdf
// freopen("inputf.in", "w", stdout);
int t=1;
// cin>>t;
while(t--){
solve();
}
return 0;
}
| #pragma GCC optimize("Ofast")
#if 1
#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) cerr << #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(x) cout<<((x)?"Yes":"No")<<newl;
#define YESNO(x) cout<<((x)?"YES":"NO")<<newl;
#define v(T) vector<T>
#define vv(T) vector<vector<T>>
template<class A, class B> using umap = unordered_map<A, B>;
template<class A> using uset = unordered_set<A>;
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;
}
struct P {
int x, y;
int dist(const P& v) {
return max(abs(v.x - x), abs(v.y - y));
}
};
// INSERT ABOVE HERE
signed main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int N; cin >> N;
v(P) as(N);
vpii xs(N), ys(N);
F0R(i, N) {
cin >> as[i].x>>as[i].y;
xs[i] = { as[i].x,i };
ys[i] = { as[i].y,i };
}
sort(rng(xs));
sort(rng(ys));
uset<int> vs;
auto F = [&](int i) {
vs.insert(xs[i].second);
vs.insert(ys[i].second);
};
F(0); F(1); F(N - 2); F(N - 1);
vi bs;
for (auto v : vs) {
bs.push_back(v);
}
vi rs;
F0R(i, sz(bs)) {
FOR(j, i + 1, sz(bs)) {
rs.push_back(as[bs[i]].dist(as[bs[j]]));
}
}
sort(rrng(rs));
cout << rs[1];
}
#endif
|
#include <iostream>
#include <vector>
using namespace std;
#include <iostream>
#include <cassert>
template<long long mod>
class modint{
private:
using T = long long;
T a;
public:
constexpr modint(const long long x = 0) noexcept : a((x%mod+mod)%mod) {}
constexpr T& value() noexcept { return a; }
constexpr const T& value() const noexcept { return a; }
constexpr modint operator-() const noexcept {
return modint(0) -= *this;
}
constexpr modint operator+(const modint& rhs) const noexcept {
return modint(*this) += rhs;
}
constexpr modint operator-(const modint& rhs) const noexcept {
return modint(*this) -= rhs;
}
constexpr modint operator*(const modint& rhs) const noexcept {
return modint(*this) *= rhs;
}
constexpr modint operator/(const modint& rhs) const noexcept {
return modint(*this) /= rhs;
}
constexpr modint& operator+=(const modint& rhs) noexcept {
a += rhs.a;
if(a >= mod) a -= mod;
return *this;
}
constexpr modint &operator-=(const modint& rhs) noexcept {
if(a < rhs.a) a += mod;
a -= rhs.a;
return *this;
}
constexpr modint& operator*=(const modint& rhs) noexcept {
a = a*rhs.a%mod;
return *this;
}
constexpr modint& operator/=(const modint& rhs) noexcept {
return *this *= rhs.inv();
}
constexpr bool operator==(const modint& rhs) const noexcept {
return a == rhs.a;
}
constexpr bool operator!=(const modint& rhs) const noexcept {
return not (*this == rhs);
}
constexpr modint pow(long long k) const noexcept {
modint ret(1);
modint x = k > 0 ? *this : this->inv();
k = abs(k);
while(k > 0){
if(k&1) ret *= x;
x *= x;
k >>= 1;
}
return ret;
}
constexpr modint inv() const noexcept {
return pow(mod-2);
}
friend std::ostream& operator<<(std::ostream &os, const modint &X) noexcept {
return os << X.a;
}
friend std::istream& operator>>(std::istream &is, modint &X) noexcept {
is >> X.a;
X.a %= mod;
if(X.a < 0) X.a += mod;
return is;
}
};
template<typename T>
T power(T x, long long k){
T ret{1};
while(k > 0){
if(k&1) ret *= x;
x *= x;
k /= 2;
}
return ret;
}
int main(){
using mint = modint<998244353>;
int n, m;
cin >> n >> m;
mint ans = 0;
vector<mint> pow_m(n+1);
pow_m[0] = 1;
for(int i = 0; i < n; ++i)
pow_m[i+1] = pow_m[i]*m;
for(int i = 1; i <= m; ++i){
ans += power<mint>(m-i+1,n) - power<mint>(m-i,n);
mint a = 1, b = 1;
for(int j = 1; j+1 <= n; ++j){
a *= m-i+1, b *= m-i;
if(j+2 <= n){
mint p = a - b;// i が 1 回以上使われる
p *= n-j-1;// 長さ j+2 の区間の取り方
p *= (i-1)*(i-1);//両端
p *= pow_m[n-j-2];//残り
ans += p;
}
mint q = a - b;
q *= 2;
q *= i-1;//両端
q *= pow_m[n-j-1];//残り
ans += q;
}
}
cout << ans << endl;
}
| #include<bits/stdc++.h>
using namespace std;
#define int ll
#define ll long long
#define db double
#define rep(i,be,en) for(int i=be;i<=en;i++)
const int INF = 0x3f3f3f3f;
const ll inf=0x3f3f3f3f3f3f3f3f;
//const double pi=acos(-1);
const int mod=1e9+7;
#define eps 1e-8
#define zero(x) (((x)>0?(x):-(x))<eps)
using namespace std;
const int N=1e5+7;
const int M=1e6+7;
inline int read(){
char c=getchar();int x=0,f=1;
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
return x*f;
}
int T,n,m,fl;
signed main(){
n=read();
int x=1,a=0,b=0,c=0,ans,aa,bb,cc;
a=n/x;
ans=a;aa=a;bb=0;cc=0;
while(x*2<=n){
b++;
x*=2;
a=n/x;
c=n%x;
if(a+b+c<ans){
ans=a+b+c;
aa=a;bb=b;cc=c;
}
}
printf("%lld\n",ans,aa,bb,cc);
}
|
#include<iostream>
#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 ordered_set tree<ll, null_type,less<>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define FOR(i,a,b) for(ll i=a;i<=b;i++)
#define FORR(i,a,b) for(ll i=a;i>=b;i--)
#define vl vector<ll>
#define ld long double
#define vld vector<ld>
#define vvl vector<vector<long long>>
#define vvld vector<vector<ld>>
#define pll pair<long,long>
#define vpll vector<pll>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define lb(v,x) lower_bound(v.begin(),v.end(),x)
#define ub(v,x) upper_bound(v.begin(),v.end(),x)
#define d1(x) cout<<(x)<<endl
#define d2(x,y) cout<<(x)<<" "<<(y)<<endl
#define d3(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl
#define d4(a,b,c,d) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<endl
#define PI 3.14159265358979323846264338327950288419716939937510
#define fix(f,n) fixed<<setprecision(n)<<f
#define all(x) x.begin(),x.end()
#define rev(p) reverse(p.begin(),p.end());
#define mset(a,val) memset(a,val,sizeof(a));
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define popcount(x) __builtin_popcountll(x)
#define sz(x) ((ll)x.size())
const ll M = 1000000007;
const ll MM = 998244353;
#define endl "\n"
ll begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T, typename F>
void chmax( T &a, F b) {
if (b > a)a = b;
}
template<typename T, typename F>
void chmin( T &a, F b) {
if (b < a)a = b;
}
ll powM(ll a, ll b, ll m) {
if (b < 0 || a <= 0)return 0;
a %= m;
ll ans = 1LL;
while (b) {
if (b & 1)ans = ans * a % m;
a = a * a % m;
b >>= 1;
}
return ans;
}
ll poww(ll a, ll b) {
if (b < 0 || a <= 0)return 0;
ll ans = 1LL;
while (b) {
if (b & 1)ans = ans * a;
a = a * a;
b >>= 1;
}
return ans;
}
const ll N = 3e5 + 5;
//ll dp[105][105][105];
//ll allowed[105][105][105];
int main() {
IOS;
#ifndef ONLINE_JUDGE
freopen("input1.txt", "r", stdin);
freopen("output1.txt", "w", stdout);
#endif
ll n, x;
cin >> n >> x;
vl a(n + 1);
FOR(i, 1, n) {
cin >> a[i];
}
/*
let us say we choose q elements.
x%q == (sum)%q;
so basically what is the max sum from q elements
st sum%q==x%q;
dp[i][j][mod]. (sum%q==mod) mod:[0,q-1];
dp[i-1][j][mod],dp[i-1][j-1][(mod-a[i]%q)]+a[i]
and last me dp[n][q][x%q] (for a fixed q).
so ans=(x-dp)/q;
*/
ll ans = x;
FOR(q, 1, n) {
vector<vvl>dp(n + 1, vvl(n + 1, vl(n + 1, -1e18)));
dp[0][0][0] = 0;
for (ll i = 0; i < n; i++) {
for (ll j = 0; j <= i; j++) {
for (ll mod = 0; mod < q; mod++) {
chmax(dp[i + 1][j][mod], dp[i][j][mod]);
chmax(dp[i + 1][j + 1][(mod + a[i + 1]) % q], dp[i][j][mod] + a[i + 1]);
}
}
}
ll blah = x % q;
//d2(q, dp[n][q][blah]);
if (dp[n][q][blah] > 0) chmin(ans, (x - dp[n][q][blah]) / q);
}
cout << ans << endl;
return 0;
} | #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
template <class T1, class T2>
ostream& operator << (ostream& out, const pair <T1, T2> p)
{
return out << '(' << p.first << ',' << p.second << ')';
}
template <class T1, class T2>
istream& operator >> (istream& in, pair<T1, T2> &p)
{
return in >> p.first >> p.second;
}
template <class T>
istream& operator >> (istream& in, vector<T> &v)
{
for (T &x : v)
in >> x;
return in;
}
template <class T>
ostream& operator << (ostream& out, const vector<vector<T>> &v)
{
for (const vector<T> &x : v)
out << x << '\n';
return out;
}
template <class T>
ostream& operator << (ostream& out, const vector<T> &v)
{
for (const T &x : v)
out << x << ' ';
return out;
}
long long gcd (long long a, long long b)
{
if (b > a)
swap(a, b);
return (b ? gcd(b, a % b) : a);
}
using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
using tiii = pair<pair<int, int>, int>;
using vi = vector<int>;
using vl = vector<long long>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<long long>>;
#define F first
#define S second
#define First first.first
#define Second first.second
#define Third second
#define mp make_pair
#define rep(i,a,b) for (int i = (a); i < (b); i++)
#define per(i,b,a) for (int i = (b); i > (a); i--)
#define all(x) x.begin(), x.end()
#define ret(x) return cout << x, 0;
#define throwex throw runtime_error ("Found the error.");
const int h = 1000000007;
signed main()
{
ios::sync_with_stdio(false);
#ifdef ONLINE_JUDGE
cin.tie(nullptr);
cerr.setstate(ios::failbit);
#endif
int n;
cin >> n;
ll x;
cin >> x;
vl v(n);
cin >> v;
ll ans = LLONG_MAX;
rep(num,1,n+1)
{
vvl dp(num + 1, vl(num, -1));
dp[0][0] = 0;
for(ll x : v)
per(i,num-1,-1)
rep(j,0,num)
if(dp[i][j] != -1)
dp[i + 1][(j + x) % num] = max(dp[i + 1][(j + x) % num], dp[i][j] + x);
if(dp.back()[x % num] != -1)
ans = min(ans, (x - dp.back()[x % num]) / num);
}
cout << (ans == LLONG_MAX ? -1 : ans);
}
|
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef set<int> si;
typedef map<int,int> mii;
typedef map<ll,ll> mll;
typedef vector<pii> vii;
typedef vector<pll> vll;
#define fi first
#define se second
#define pi 3.141592653589793
#define mod 998244353
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define pqmax priority_queue<int>
#define pqmin priority_queue<int,vi,greater<int>>
#define fio ios_base::sync_with_stdio(0), cin.tie(NULL)
#define tc int tt;cin>>tt;for(int ti=1;ti<=tt;ti++)
#define case_g "Case #"<<ti<<": "
#define RED "\033[31m"
#define GREEN "\033[32m"
#define RESET "\033[0m"
#define sleep for (int i = 1, a;i < 100000000;i++, a = a * a)
typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> ranked_pairset;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ranked_set;
typedef tree<int, int, less<int>, rb_tree_tag, tree_order_statistics_node_update> ranked_map;
ll modpow(ll a, ll b) {
ll p = 1;
while (b > 0) {
if (b & 1)
p = p * a % mod;
a = a * a % mod;
b >>= 1;
}
return p;
}
ll modinv(ll a) {
ll m = mod, y = 0, x = 1;
while (a > 1) {
ll q = a / m, t = m;
m = a % m;
a = t;
t = y;
y = x - q * y;
x = t;
}
return x < 0 ? x + mod : x;
}
int main() {
fio;
ll n, m;
cin >> n >> m;
ll dp[n][m];
for (int i = 0;i < m;i++)
dp[0][i] = 1;
vl s(n), minv(m + 1), mpm1j(m, 1);
s[0] = m;
mpm1j[0] = 0;
ll mpmi = 1;
for (int j = 1;j <= m;j++)
minv[j] = modinv(j);
for (int i = 1;i < n;i++) {
mpmi = mpmi * m % mod;
for (int j = 0;j < m;j++) {
mpm1j[m - 1 - j] = mpm1j[m - 1 - j] * (m - 1 - j) % mod;
dp[i][j] = (s[i - 1] + (mpmi * j + mpm1j[m - 1 - j]) % mod * minv[j + 1]) % mod;
}
for (int j = 0;j < m;j++)
s[i] = (s[i] + dp[i][j]) % mod;
}
cout << s[n - 1] << '\n';
}
| #include <bits/stdc++.h>
using namespace std;
const int maxn = 5e3+5,mod = 998244353;
int n,m,powm[maxn][maxn],ans;
int main(){
scanf("%d %d",&n,&m);
powm[0][0] = 1;
for(int i=1;i<=m;++i){
powm[i][0] = 1;
for(int j=1;j<=n;++j)powm[i][j] = 1ll*powm[i][j-1]*i%mod;
}
ans = 1ll*n*powm[m][n]%mod;
for(int i=1;i<n;++i)
for(int j=1;j<=m;++j)
ans -= 1ll*(n-i)*powm[m-j][i-1]%mod*powm[m][n-i-1]%mod,ans = (ans+mod)%mod;
printf("%d",ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std ;
#define i64 int64_t // typecast using i64(x)
#define int int64_t
#define ld long double
#define endl "\n"
#define f(i,a,b) for(int i=int(a);i<int(b);++i)
#define pr pair
#define ar array
#define fr first
#define sc second
#define vt vector
#define pb push_back
#define LB lower_bound
#define UB upper_bound
#define PQ priority_queue
#define sz(x) ((int)(x).size())
#define all(a) (a).begin(),(a).end()
#define allr(a) (a).rbegin(),(a).rend()
#define mem0(a) memset(a, 0, sizeof(a))
#define mem1(a) memset(a, -1, sizeof(a))
template<class A> void rd(vt<A>& v);
template<class T> void rd(T& x){ cin >> x; }
template<class H, class... T> void rd(H& h, T&... t) { rd(h) ; rd(t...) ;}
template<class A> void rd(vt<A>& x) { for(auto& a : x) rd(a) ;}
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
template<typename T>
void __p(T a) {
cout<<a;
}
template<typename T, typename F>
void __p(pair<T, F> a) {
cout<<"{";
__p(a.first);
cout<<",";
__p(a.second);
cout<<"}\n";
}
template<typename T>
void __p(std::vector<T> a) {
cout<<"{";
for(auto it=a.begin(); it<a.end(); it++)
__p(*it),cout<<",}\n"[it+1==a.end()];
}
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) {
__p(a1);
__p(a...);
}
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
cout<<name<<" : ";
__p(arg1);
cout<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
int bracket=0,i=0;
for(;; i++)
if(names[i]==','&&bracket==0)
break;
else if(names[i]=='(')
bracket++;
else if(names[i]==')')
bracket--;
const char *comma=names+i;
cout.write(names,comma-names)<<" : ";
__p(arg1);
cout<<" | ";
__f(comma+1,args...);
}
void setIO(string s = "") {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin.exceptions(cin.failbit);
if(sz(s)){
freopen((s+".in").c_str(),"r",stdin);
freopen((s+".out").c_str(),"w",stdout);
}
}
template<int MOD> struct mint {
static const int mod = MOD ;
int v; explicit operator int() const { return v; }
mint() { v = 0; }
mint(long long _v) { v = int((-MOD < _v && _v < MOD) ? _v : _v % MOD);
if (v < 0) v += MOD; }
friend bool operator==(const mint& a, const mint& b) {
return a.v == b.v; }
friend bool operator!=(const mint& a, const mint& b) {
return !(a == b); }
friend bool operator<(const mint& a, const mint& b) {
return a.v < b.v; }
mint& operator+=(const mint& m) {
if ((v += m.v) >= MOD) v -= MOD;
return *this; }
mint& operator-=(const mint& m) {
if ((v -= m.v) < 0) v += MOD;
return *this; }
mint& operator*=(const mint& m) {
v = int((long long)v*m.v%MOD); return *this; }
mint& operator/=(const mint& m) { return (*this) *= inv(m); }
friend mint power(mint a, long long p) {
mint ans = 1; assert(p >= 0);
for (; p; p /= 2, a *= a) if (p&1) ans *= a;
return ans; }
friend mint inv(const mint& a) { assert(a.v != 0);
return power(a,MOD-2); }
mint operator-() const { return mint(-v); }
mint& operator++() { return *this += 1; }
mint& operator--() { return *this -= 1; }
friend mint operator+(mint a, const mint& b) { return a += b; }
friend mint operator-(mint a, const mint& b) { return a -= b; }
friend mint operator*(mint a, const mint& b) { return a *= b; }
friend mint operator/(mint a, const mint& b) { return a /= b; }
};
const int MOD = 998244353 ;
typedef mint<MOD> mi;
signed main(){
setIO() ;
int N, M, K;
rd(N,M,K);
mi ans ;
if(N == 1){
ans = power(mi(K),M);
}else if(M == 1){
ans = power(mi(K),N);
}else{
for(int x=1; x<=K; x++){
mi countB = power(mi(K-x+1),M);
mi countA = power(mi(x),N) - power(mi(x-1),N) ;
ans += countA * countB;
}
}
cout << ans.v ;
}
| // Skyqwq
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
const int N = 2e5 + 5, P = 998244353;
int n, m, K;
int inline power(int a, int b) {
int res = 1;
while (b) {
if (b & 1) res = (LL)res * a % P;
a = (LL)a * a % P;
b >>= 1;
}
return res;
}
int ans;
int main() {
scanf("%d%d%d", &n, &m, &K);
if (n > 1 && m > 1) {
for (int i = 1; i <= K; i++) {
ans = (ans + ((LL)power(i, n) - power(i - 1, n) + P) * power(K - i + 1, m)) % P;
}
} else if (n == 1 && m == 1) {
ans = K;
} else if (n == 1) {
for (int i = 1; i <= K; i++) {
ans = (ans + (LL)power(K - i + 1, m) - power(K - i, m) + P) % P;
}
} else if (m == 1) {
for (int i = 1; i <= K; i++) {
ans = (ans + (LL)power(i, n) - power(i - 1, n) + P) % P;
}
}
printf("%d\n", ans);
return 0;
} |
#include<stdio.h>
#include<iostream>
#include<cstring>
#define ll long long
using namespace std;
ll x,y,a,b,g;
int main(){
cin>>x>>y>>a>>b;
while((double)a*x<=x+b && a*x<y){
x*=a;
g++;
}
cout<<g + (y-1-x)/b<<endl;
return 0;
}
| #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); (i)++)
#define per(i, n) for (ll i = n - 1; i >= 0; (i)--)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define ROF(i, a, b) for (ll i = (b) - 1; i >= (a); i--)
#define ALL(x) std::begin(x), std::end(x)
typedef long long ll;
int main(){
//input, initialize
ll A, B, K; cin >> A >> B >> K;
//solve
vector<vector<ll>> combi(A+B+1, (vector<ll>(A+B+1, 0)));
rep(i, A+B+1) combi[i][0] = 1;
// combi[i][j] := iCjを表す
FOR(i, 1, A+B+1)FOR(j, 1, i+1){
combi[i][j] = combi[i][j-1] * (i - j + 1) / j;
}
vector<bool> Bspot(A+B+1); //Bspot[i] := ひだりからi番目(1-based)のBの位置
ROF(i, 1, B+1)FOR(j, i, A+B+1){
if(combi[j][i] >= K){
Bspot[j] = true;
K -= combi[j-1][i]; // j == i のときは0が引かれてそのまま
break;
}
}
//output
ROF(i, 1, A+B+1){
if(Bspot[i]) cout << 'b';
else cout << 'a';
}
cout << '\n';
} |
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
if(a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if(a > b) {
a = b;
return 1;
}
return 0;
}
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << '(' << p.first << ',' << p.second << ')';
return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << '{';
for(int i = 0; i < (int)v.size(); i++) {
if(i) { os << ','; }
os << v[i];
}
os << '}';
return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
cerr << " " << x;
debugg(args...);
}
#define debug(...) \
cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
struct Setup {
Setup() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
}
} __Setup;
using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define repl(i, a, b) for(int i = a; i < int(b); i++)
#define rep(i, n) repl(i, 0, n)
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
//-------------------------------------
int main() {
int sx, sy, gx, gy;
cin >> sx >> sy >> gx >> gy;
double ans = sx;
ans += (double)(gx - sx) * (double)sy / (double)(sy + gy);
cout << ans << endl;
} | #include<bits/stdc++.h>
using namespace std;
int main()
{
double x1,x2,y1,y2,d;
cin>>x1>>y1>>x2>>y2;
d=(y1*x2+y2*x1);
d=d/(y2+y1)*1.00;
cout<<fixed<<setprecision(10);
cout<<d<<endl;
}
|
#include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl "\n"
#define rep(i,n) repi(i,0,n)
#define repi(i,a,n) for(ll i=a;i<(ll)n;++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
void YN(bool a) { cout << (a ? "Yes" : "No") << endl; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
void show(){ cout << endl; }
template <class Head, class... Tail>
void show(Head&& head, Tail&&... tail){ cout << head << " "; show(std::forward<Tail>(tail)...); }
template<class T> inline void showall(T& a) { for(auto v:a) cout<<v<<" "; cout<<endl; }
vector<ll> sz;
vector<vector<ll>> child;
int dfs(int idx)
{
sz[idx] = 1;
for(auto to : child[idx])
{
sz[idx] += dfs(to);
}
return sz[idx];
}
int count(int idx) // idxの頂点に来た時に手番のある人が、また戻ってきたときの相対スコア
{
vector<ll> odd;
vector<ll> even;
ll score = -1;
for(auto to : child[idx]) ((sz[to] & 1) ? odd : even).emplace_back(count(to));
sort(RALL(odd));
int sign = 1;
for(auto v : odd)
{
score += sign * v;
sign *= -1;
}
for(auto v : even)
{
if (v >= 0) score += v;
else score += v * ((odd.size() & 1) ? -1 : 1);
}
return score;
}
void solve()
{
ll n;
cin >> n;
sz.resize(n, 0);
child.resize(n, vector<ll>());
repi(i, 1, n)
{
ll p;
cin >> p;
p--;
child[p].emplace_back(i);
}
dfs(0);
show((n - count(0)) / 2);
}
int main()
{
fastio;
solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
const int N = 100000 + 1;
int coins[N], sub[N];
vector<int> adj[N];
void dfs(int u) {
sub[u] = 1;
for (auto c : adj[u]) {
dfs(c);
sub[u] += sub[c];
}
coins[u] = 1;
vector<array<int, 2>> children;
for (auto c : adj[u]) {
if (sub[c] % 2 == 0) {
if (coins[c] <= sub[c] - coins[c]) {
coins[u] += coins[c];
}
} else {
children.push_back({sub[c] - 2 * coins[c], c});
}
}
sort(children.rbegin(), children.rend());
for (int i = 0; i < (int) children.size(); ++i) {
int c = children[i][1];
if (i % 2 == 0) {
coins[u] += coins[c];
} else {
coins[u] += sub[c] - coins[c];
}
}
for (auto c : adj[u]) {
if (sub[c] % 2 == 0 && coins[c] > sub[c] - coins[c]) {
if (children.size() % 2 == 0) {
coins[u] += coins[c];
} else {
coins[u] += sub[c] - coins[c];
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
for (int i = 2; i <= n; ++i) {
int p;
cin >> p;
adj[p].push_back(i);
}
dfs(1);
cout << coins[1] << "\n";
}
|
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define set_bits(a) __builtin_popcount(a)
#define pb push_back
#define pf push_front
#define mod 1000000007
#define M 998244353
#define fi first
#define se second
#define endl '\n'
#define INF 1e18
#define PI 3.14159265358979323846
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
bool isPrime(ll n){
if(n<=1) return false;
if(n<=3) return true;
if(n%2==0 || n%3==0) return false;
for(ll i=5; i*i<=n; i++){
if(n%i==0 || n%(i+2)==0){
return false;
}
}
return true;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
string s;
cin>>s;
string ans = "";
for(ll i=0;i<s.length();i++)
{
if(s[i]=='.'){
break;
}
ans+=s[i];
}
cout<<ans<<endl;
} | #include <bits/stdc++.h>
#define loop(n,i) for(int i=0;i<n;i++)
using namespace std;
int main(){
int n;
cin >> n;
cout << n-1 << endl;
return 0;
} |
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
using namespace std;
using LL = long long;
using P = pair<int,int>;
using vv = vector<vector<int>>;
const int INF = (int)1e9;
const LL LINF = (LL)1e18;
long long const mod = 998244353;
struct mint{
long long val;
mint(long long val = 0): val(val % mod) {}
mint& operator += (const mint n){
val += n.val;
if(val >= mod) val -= mod;
return *this;
}
mint& operator -= (const mint n){
val -= n.val;
if(val < 0) val += mod;
return *this;
}
mint& operator *= (const mint n){
val *= n.val;
val %= mod;
if(val < 0) val += mod;
return *this;
}
mint operator + (const mint n) const{
mint res(*this);
return res += n;
}
mint operator - (const mint n) const{
mint res(*this);
return res -= n;
}
mint operator * (const mint n) const{
mint res(*this);
return res *= n;
}
mint pow(long long n) const{
if(n == 0) return 1;
mint m = pow(n >> 1);
m *= m;
if(n & 1) m *= *this;
return m;
}
// mint division for prime mod
mint inv() const{
return pow(mod - 2);
}
mint& operator /= (const mint n){
return (*this) *= n.inv();
}
mint operator / (const mint n) const{
mint res(*this);
return res /= n;
}
};
long long modpow(long long x, long long n){
long long r = 1;
while(n){
if(n & 1) r = r * x % mod;
x = x * x % mod;
n >>= 1;
}
return r;
}
int main(){
LL N, M, K;
cin >> N >> M >> K;
if(N == 1){
cout << modpow(K, M) << endl;
return 0;
}
if(M == 1){
cout << modpow(K, N) << endl;
return 0;
}
mint ans = 0;
for(LL i = 1; i <= K; i++){
mint A = modpow(i, N) - modpow(i - 1, N);
mint B = modpow(K - i + 1, M);
mint res = A * B;
ans += res;
}
cout << ans.val << endl;
return 0;
}
| #include<bits/stdc++.h>
#define pb push_back
#define fast ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define f first
#define s second
#define int long long
#define sz(x) (ll)(x.size())
using namespace std;
const int mod = 1e9+7;
int expo_pow(int x,int y){
if(y == 0) return 1;
y=y%(mod-1);
x%=mod;
if(y==0) y=mod-1;
int res=1;
while(y){
if(y&1) res=(res*x)%mod;
x=(x*x)%mod;
y>>=1;
}
return res;
}
ll add()
{
return 0;
}
template <typename T, typename... Types>
T add(T var1, Types... var2){
return (((((ll)(var1)) % mod + (ll)(add(var2...))) % mod) + mod) % mod;
}
ll mul(){
return 1;
}
template <typename T, typename... Types>
T mul(T var1, Types... var2){
return (((ll)(var1)) % mod * (ll)(mul(var2...))) % mod;
}
void solve(){
int n;
cin >> n;
if (n == 2 or n == 3) {
cout << "1\n";
return;
}
int fib[n];
fib[0] = 1;
fib[1] = 1;
for (int i = 2; i <= n; ++i) {
fib[i] = add(fib[i-1],fib[i-2]);
}
char caa,cab,cba,cbb;
cin >> caa >> cab >> cba >> cbb;
if (cab == 'A') {
if (caa == 'A') {
cout << "1\n";
return;
}
if (cba == 'A') {
cout << fib[n-2] << "\n";
return ;
}
else {
cout << expo_pow(2,n-3) << "\n";
return ;
}
}
if (cab == 'B') {
if (cbb == 'B') {
cout << "1\n";
return;
}
if (cba == 'B') {
cout << fib[n-2] << "\n";
return ;
}
else {
cout << expo_pow(2,n-3) << "\n";
return ;
}
}
}
signed main(){
fast;
int test = 1;
int i=1;
while(test--){
solve();
}
}
|
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>
#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
using namespace std;
typedef long long ll;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(10) << fixed;
int n, m; cin >> n >> m;
vector<int> a(n), b(m);
vector<int> cnt(1001);
for(int i = 0; i < n; i++) {
cin >> a[i];
cnt[a[i]]++;
}
for(int i = 0; i < m; i++) {
cin >> b[i];
cnt[b[i]]++;
}
for(int i = 1; i <= 1000; i++) {
if(cnt[i] == 1) cout << i << ' ';
}
cout << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())
#define endl "\n"
template<class T> void chmax(T &a, const T b){ a = max(a, b); }
template<class T> void chmin(T &a, const T b){ a = min(a, b); }
long long gcd(long long a, long long b) { return (a % b) ? gcd(b, a % b) : b; }
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
// ifstream in("input.txt");
// cin.rdbuf(in.rdbuf());
ll n, m;
cin >> n >> m;
vector<ll> cnt(1e3 + 1, 0);
rep(i, n) {
ll a;
cin >> a;
cnt[a]++;
}
rep(i, m) {
ll b;
cin >> b;
cnt[b]++;
}
rep(i, sz(cnt)) {
if (cnt[i] == 1) {
cout << i << endl;
}
}
return 0;
}
|
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,avx512f")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <stack>
#include <queue>
#include <bitset>
#include <numeric>
#include <cassert>
#include <memory>
#include <random>
#include <functional>
#include <complex>
#include <immintrin.h>
#ifdef DEBUG
#include "./CompetitiveProgrammingCpp/debug_VC.hpp"
#include "./CompetitiveProgrammingCpp/Timer.hpp"
#include "./CompetitiveProgrammingCpp/sample.hpp"
#else
#define dump(...)
#endif
/* macro */
#define FOR(i, b, e) for(ll i = (ll)(b); i < (ll)(e); ++i)
#define RFOR(i, b, e) for(ll i = (ll)(e-1); i >= (ll)(b); --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define REPC(x,c) for(const auto& x:(c))
#define REPI2(it,b,e) for(auto it = (b); it != (e); ++it)
#define REPI(it,c) REPI2(it, (c).begin(), (c).end())
#define RREPI(it,c) REPI2(it, (c).rbegin(), (c).rend())
#define REPI_ERACE2(it, b, e) for(auto it = (b); it != (e);)
#define REPI_ERACE(it, c) REPI_ERACE2(it, (c).begin(), (c).end())
#define ALL(x) (x).begin(),(x).end()
#define cauto const auto&
/* macro func */
template<class T>
inline auto sort(T& t) { std::sort(ALL(t)); }
template<class T>
inline auto rsort(T& t) { std::sort((t).rbegin(), (t).rend()); }
template<class T>
inline auto unique(T& t) { (t).erase(unique((t).begin(), (t).end()), (t).end()); }
template<class T, class S>
inline auto chmax(T& t, const S& s) { if (s > t) { t = s; return true; } return false; }
template<class T, class S>
inline auto chmin(T& t, const S& s) { if (s < t) { t = s; return true; } return false; }
inline auto BR() { std::cout << "\n"; }
/* type define */
using ll = long long;
using PAIR = std::pair<ll, ll>;
using VS = std::vector<std::string>;
using VL = std::vector<long long>;
using VVL = std::vector<VL>;
using VVVL = std::vector<VVL>;
using VD = std::vector<double>;
template<class T>
using V = std::vector<T>;
/* using std */
using std::cout;
constexpr char endl = '\n';
using std::cin;
using std::pair;
using std::string;
using std::stack;
using std::queue;
using std::vector;
using std::list;
using std::map;
using std::unordered_map;
using std::multimap;
using std::unordered_multimap;
using std::set;
using std::unordered_set;
using std::unordered_multiset;
using std::multiset;
using std::bitset;
using std::priority_queue;
/* Initial processing */
struct Preprocessing { Preprocessing() { std::cin.tie(0); std::ios::sync_with_stdio(0); }; }_Preprocessing;
/* Remove the source of the bug */
signed pow(signed, signed) { assert(false); return -1; }
/* define hash */
namespace std {
template <> class hash<std::pair<ll, ll>> { public: size_t operator()(const std::pair<ll, ll>& x) const { return hash<ll>()(1000000000 * x.first + x.second); } };
}
/* input */
template<class T> std::istream& operator >> (std::istream& is, vector<T>& vec) { for (T& x : vec) is >> x; return is; }
/* constant value */
constexpr ll MOD = 1000000007;
//constexpr ll MOD = 998244353;
//=============================================================================================
signed main() {
ll n, q;
cin >> n >> q;
VL a(n);
cin >> a;
VL v(n);
RREP(i, n) {
v[i] = a[i] - i;
}
dump(v);
REP(_, q) {
ll k;
cin >> k;
auto it = std::upper_bound(ALL(v), k) - v.begin();
--it;
if (it == -1) {
cout << k << endl;
} else {
cout << a[it] + k - v[it] + 1 << endl;
}
}
}
| #include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define ll long long
#define ld long double
#define mp make_pair
#define pb push_back
#define fo(i,n) for(ll i=0;i<n;i++)
#define fo1(i,n) for(ll i=1;i<=n;i++)
#define loop(i,a,b)for(ll i=a;i<=b;i++)
#define loopr(i,a,b)for(ll i=b;i>=a;i--)
#define all(x) x.begin(), x.end()
#define sz(x) (ll)(x).size()
#define vll vector<ll>
#define vvl vector<vll>
#define vpll vector<pll>
#define pll pair<ll,ll>
#define F first
#define S second
#define MOD 1000000007
ll max(ll a,ll b){if (a>b) return a; else return b;}
ll gcd(ll a, ll b){if(b==0)return a;return gcd(b, a%b);}
ll lcm(ll a, ll b){return a*b/gcd(a, b);}
ll fexp(ll a, ll b){ll ans = 1;while(b){if(b&1) ans = ans*a%MOD; b/=2;a=a*a%MOD;}return ans;}
ll inverse(ll a, ll p){return fexp(a, p-2);}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
//freopen("input.txt","r",stdin);
ll T=1;
//cin >> T;
ll t;
while(t++<T){
ll n,k;
cin>>n>>k;
vll a(n,0);
fo(i,n){
ll z;
cin>>z;
a[z]++;
}
ll cnt=0,ans=0;
fo(i,n){
ll lft=k-cnt;
if(a[i]<lft){
ans+=i*(lft-a[i]);
cnt+=(lft-a[i]);
}
if(cnt>=k)break;
}
cout<<ans<<endl;
}
return 0;
}
|
#include <bits/stdc++.h>
#define int long long
using namespace std;
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')w=-1;ch=getchar();
}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
inline bool read(int& a){
int s=0,w=1;
char ch=getchar();
if(ch==EOF){
return false;
}
while(ch<'0'||ch>'9'){
if(ch=='-')w=-1;ch=getchar();
}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
a=s*w;
return true;
}
inline void write(int x){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
}
template<typename...args>inline bool read(int &a,args&...x){
return (read(a),read(x...));
}
inline void writeln(int x){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10+'0');
puts("");
}
int h,w,x,y;
char m[110][110];
bool in(int x,int y){
return x>=1&&x<=h&&y>=1&&y<=w;
}
int ans=1;
signed main(){
read(h,w,x,y);
for(int i=1;i<=h;i++){
for(int j=1;j<=w;j++){
cin>>m[i][j];
}
}
for(int i=1;i<=h;i++){
if(in(x+i,y)&&m[x+i][y]!='#'){
ans++;
}
else{
break;
}
}
for(int i=1;i<=h;i++){
if(in(x-i,y)&&m[x-i][y]!='#'){
ans++;
}
else{
break;
}
}
for(int i=1;i<=w;i++){
if(in(x,y+i)&&m[x][y+i]!='#'){
ans++;
}
else{
break;
}
}
for(int i=1;i<=w;i++){
if(in(x,y-i)&&m[x][y-i]!='#'){
ans++;
}
else{
break;
}
}
writeln(ans);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(ll i=0; i<ll(n); i++)
#define FOR(i,m,n) for(ll i=ll(m); i<ll(n); i++)
#define ALL(obj) (obj).begin(),(obj).end()
#define VI vector<int>
#define VP vector<pair<ll,ll>>
#define VPP vector<pair<int,pair<int,int>>>
#define VLL vector<long long>
#define VVI vector<vector<int>>
#define VVLL vector<vector<long long>>
#define VC vector<char>
#define VS vector<string>
#define VVC vector<vector<char>>
#define VB vector<bool>
#define VVB vector<vector<bool>>
#define fore(i,a) for(auto &i:a)
typedef pair <int, int> P;
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
template<class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; }
const int INF = (1 << 30) - 1;
const ll INFL = 1LL << 61;
const ll mod =998244353;
vector<int> dx = { 1,-1, 0, 0, 1,-1,-1, 1 };
vector<int> dy = { 0, 0, 1,-1, 1,-1, 1,-1 };
int h, w;
bool isinside(int i, int j) {
if (0 <= i && i < h && 0 <= j && j < w) {
return true;
}
return false;
}
int main() {
cin >> h >> w;
int i, j;
cin >> i >> j;
i--, j--;
VS v(h);
REP(i, h)cin >> v[i];
int cnt = 0;
REP(k, 4) {
int a = 0;
while (true) {
int ny = i + dy[k] * a;
int nx = j + dx[k] * a;
if (!isinside(ny, nx))break;
if (v[ny][nx] == '#')break;
a++;
}
cnt += a;
}
cout << cnt-3 << endl;
} |
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define MT make_tuple
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
#define RT return
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
template<int MOD>
class ModInt {
public:
ModInt() :value(0) {}
ModInt(long long val) :value((int)(val<0 ? MOD + val % MOD : val % MOD)) { }
ModInt& operator+=(ModInt that) {
value = value + that.value;
if (value >= MOD)value -= MOD;
return *this;
}
ModInt& operator-=(ModInt that) {
value -= that.value;
if (value<0)value += MOD;
return *this;
}
ModInt& operator*=(ModInt that) {
value = (int)((long long)value * that.value % 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 pow(long long k) const {
ModInt n = *this, res = 1;
if (k < 0) {
k = -k;
n = n.inverse();
}
while (k) {
if (k & 1)res *= n;
n *= n;
k >>= 1;
}
return res;
}
ModInt inverse() const {
long long a = value, 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);
}
return ModInt(u);
}
int toi() const { return value; }
private:
int value;
};
typedef ModInt<998244353> mint;
ostream& operator<<(ostream& os, const mint& x) {
os << x.toi();
return os;
}
mint dp[3002][3002];
bool vis[3002][3002];
mint f(int n, int k) {
mint &res = dp[n][k];
if (vis[n][k])return res;
vis[n][k] = true;
if (n == 0 && k == 0) {
res = 1;
} else if (n == 0 || k == 0) {
res = 0;
} else {
res = f(n - 1, k - 1);
if (k * 2 <= n) {
res += f(n, k * 2);
}
}
return res;
}
void solve() {
int N, K;
cin >> N >> K;
cout << f(N, K) << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(15);
solve();
return 0;
} | #include<bits/stdc++.h>
#define X first
#define Y second
#define ll long long
#define sz(a) (int)a.size()
using namespace std;
const int maxn =2e5 + 100;
const int mod = 998244353;
signed main()
{
// ifstream cin("rt.txt.txt");
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k;
vector <vector <int> > dp(n + 1, vector <int> (n + 1, 0));
dp[0][0] = 1;
for(int n1 = 1; n1 <= n; n1++)
{
for(int k1 = n; k1 >= 1; k1--)
{
if(k1 * 2 <= n)
{
dp[n1][k1] = (dp[n1][k1] + dp[n1][k1 * 2]) % mod;
}
dp[n1][k1] = (dp[n1][k1] + dp[n1 - 1][k1 - 1]) % mod;
}
}
// cout << dp[1][1] << " ";
cout << dp[n][k];
return 0;
}
|
#include<bits/stdc++.h>
#define ll long long
#define db double
using namespace std;
void solve()
{
int a,b,c,d,r;
cin>>a>>b>>c>>d;
r=(d*c)-b;
if(r<=0)
cout<<-1;
else
{
db s=ceil(db(a)/db(r));
cout<<int(s);
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repx(i, x, n) for (ll i = (ll)(x); i < (ll)(n); i++)
#define repr(i, n) for (ll i = ((ll)(n)-1); i >= 0; i--)
#define reprx(i, x, n) for (ll i = ((ll)(n)-1); i >= (ll)(x); i--)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((ll)(x).size())
#define submit(x) {cout<<x<<endl; return 0;}
#define X first
#define Y second
template<class T> using V = vector<T>;
template<class T> using VV = V<V<T>>;
template<class T>bool chmax(T &a,T b) {if (a<b) {a=b; return 1;} return 0;}
template<class T>bool chmin(T &a,T b) {if (b<a) {a=b; return 1;} return 0;}
template<class T>long long square(T a) {return a*a;}
using ll = long long;
using P = pair<ll, ll>;
const ll INF = 1LL << 60;
const double PI = 3.1415926535897932;
int main()
{
ll x,y,a,b;
cin>>x>>y>>a>>b;
ll exp=0;
ll change=0;
ll copy_x=x;
while(copy_x < b && copy_x*(a-1) < b)
{
copy_x *= a;
change++;
}
y -= x;
rep(i, change)
{
y -= x*(a-1);
x *= a;
if (y<=0) submit(exp);
exp++;
}
exp += y/b - (y%b==0);
submit(exp)
}
|
#include<iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <map>
#include <complex>
#include <iomanip>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#define ll long long
#define ld long double
#define HS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define INF 1e15
#define point complex <double>
#define all(a) a.begin(), a.end()
#define pi acos(-1)
#define file freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
using namespace std;
int main()
{
HS;
int t;
cin>>t;
long long sum=0;
while(t--)
{
long long a,b;
cin>>a>>b;
sum+=((b*(b+1))/2)-(((a-1)*((a-1)+1))/2);
}
cout << sum << endl;
return 0;
}
| #include <iostream>
#include <string>
using namespace std;
int main(){
int n; cin >> n;
long sum = 0;
for ( int i = 0; i < n; i++ ){
int a, b; cin >> a >> b;
for ( int j = a; j <= b; j++ ){
sum = sum + j;
}
}
cout << sum;
} |
#include <bits/stdc++.h>
const int MAX=1e6+10;
const int mod=1e9+7;
typedef long long ll ;
#define int ll
#define endl '\n'
#define cwk freopen("D:\\workplace\\CLion\\in.in","r",stdin),freopen("D:\\workplace\\CLion\\out.out","w",stdout)
using namespace std;
vector<int>a;
void solve(){
int n,m,x;
cin>>n>>m;
if(m==0){
cout<<"1";
return;
}
while(m--){
cin>>x;
a.push_back(x);
}
sort(a.begin(),a.end());
int now=1;
int minn=1e9;
for(int i=0;i<a.size();i++){
if(a[i]-now) minn=min(minn,a[i]-now);
now=a[i]+1;
}
if(a[a.size()-1]!=n) minn=min(minn,n-a[a.size()-1]);
int cnt=0;
now=1;
for(int i=0;i<a.size();i++){
if(a[i]-now) cnt+=ceil((a[i]-now)*1.0/minn);
now=a[i]+1;
}
cnt+=ceil((n-a[a.size()-1])*1.0/minn);
cout<<cnt;
}
signed main() {
//cout<<4368.0/17;
//ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
//cwk;
int _=1;
//cin>>_;
while(_--){
solve();
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
// #define int long long
#define rep(i, n) for (int i = (int)(0); i < (int)(n); ++i)
#define reps(i, n) for (int i = (int)(1); i <= (int)(n); ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; i--)
#define irep(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define ireps(i, m, n) for (int i = (int)(m); i <= (int)(n); ++i)
#define irreps(i, m, n) for (int i = ((int)(n)-1); i > (int)(m); ++i)
#define SORT(v, n) sort(v, v + n);
#define REVERSE(v, n) reverse(v, v+n);
#define vsort(v) sort(v.begin(), v.end());
#define all(v) v.begin(), v.end()
#define mp(n, m) make_pair(n, m);
#define cinline(n) getline(cin,n);
#define replace_all(s, b, a) replace(s.begin(),s.end(), b, a);
#define PI (acos(-1))
#define FILL(v, n, x) fill(v, v + n, x);
#define sz(x) (int)(x.size())
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vs = vector<string>;
using vpll = vector<pair<ll, ll>>;
using vtp = vector<tuple<ll,ll,ll>>;
using vb = vector<bool>;
using ld = long double;
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<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;
const ll INF = 1e9+10;
const ll MOD = 1e9+7;
// const ll MOD = 998244353;
const ll LINF = 1e18;
signed main()
{
cin.tie( 0 ); ios::sync_with_stdio( false );
int m,h; cin>>m>>h;
if(h%m==0) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
} |
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define rg register
const int maxn=1e5+5;
int a[maxn],n,mmax;
int main(){
scanf("%d",&n);
for(rg int i=1;i<=n;i++){
scanf("%d",&a[i]);
mmax=std::max(mmax,a[i]);
}
rg int cnt=0,jl;
for(rg int i=2;i<=mmax;i++){
rg int ncnt=0;
for(rg int j=1;j<=n;j++){
if(a[j]%i==0){
ncnt++;
}
}
if(ncnt>cnt){
cnt=ncnt;
jl=i;
}
}
printf("%d\n",jl);
return 0;
}
| #include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)),x.end())
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
#define show(x) cerr<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,v(T),greater<T> >
#define bn(x) ((1<<x)-1)
#define dup(x,y) (((x)+(y)-1)/(y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef tuple<int,int,int> T;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<T> vt;
int getInt(){int x;scanf("%d",&x);return x;}
template<typename T>istream& operator>>(istream&i,v(T)&v){rep(j,sz(v))i>>v[j];return i;}
template<typename T>string join(const v(T)&v){stringstream s;rep(i,sz(v))s<<' '<<v[i];return s.str().substr(1);}
template<typename T>ostream& operator<<(ostream&o,const v(T)&v){if(sz(v))o<<join(v);return o;}
template<typename T1,typename T2>istream& operator>>(istream&i,pair<T1,T2>&v){return i>>v.fi>>v.se;}
template<typename T1,typename T2>ostream& operator<<(ostream&o,const pair<T1,T2>&v){return o<<v.fi<<","<<v.se;}
template<typename T>bool mins(T& x,const T&y){if(x>y){x=y;return true;}else return false;}
template<typename T>bool maxs(T& x,const T&y){if(x<y){x=y;return true;}else return false;}
template<typename T>ll suma(const v(T)&a){ll res(0);for(auto&&x:a)res+=x;return res;}
const double eps = 1e-10;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define dame { puts("-1"); return 0;}
#define yn {puts("Yes");}else{puts("No");}
const int MX = 200005;
int main() {
int n;
scanf("%d",&n);
vi a(n);
cin>>a;
P ans(-1,2);
srep(x,2,1001) {
int now = 0;
rep(i,n) if (a[i]%x == 0) ++now;
maxs(ans, P(now,x));
}
cout<<ans.se<<endl;
return 0;
}
|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
int main(void)
{
ll n, k, distance = 0;
cin >> n >> k;
// friends[i] = {친구의 위치, 친구가 가지고 있는 돈}
vector<pair<ll, ll> > friends(n);
for (int i = 0; i < n; i++)
{
cin >> friends[i].first >> friends[i].second;
}
sort(friends.begin(), friends.end());
ll prevFriend = 0;
for (int i = 0; i < n; i++)
{
ll nextFriendDistance = friends[i].first - prevFriend;
if (nextFriendDistance > k)
{
distance += k;
k = 0;
break;
}
else
{
distance += nextFriendDistance;
k -= nextFriendDistance;
k += friends[i].second;
prevFriend = friends[i].first;
}
}
distance += k;
cout << distance << '\n';
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
int main() {
int n, m;
cin >> n >> m;
vector<int> a;
a.push_back(0);
a.push_back(n + 1);
rep(i, m) {
int temp;
cin >> temp;
a.push_back(temp);
}
sort(a.begin(), a.end());
// 差分のminを計算
vector<int> a_sub;
rep(i, a.size() - 1) {
int temp = a[i + 1] - a[i];
if (temp == 1) continue;
a_sub.push_back(temp);
}
if (a_sub.empty()) {
cout << 0 << endl;
return 0;
}
sort(a_sub.begin(), a_sub.end());
int a_sub_min = a_sub[0] - 1;
int count = 0;
for (int c : a_sub) {
count += (c - 2) / a_sub_min + 1;
}
cout << count << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define M 1000000007
#define N 100001
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
ll n, arr[N];
int mulMod(ll, ll); int addMod(int, int);
void readArray(ll *, int); void printArray(int *, int);
int main() {
cin>>n;
readArray(arr, n);
ll ans = arr[0];
for(int i=1; i<n; i++)
ans = __gcd(arr[i], ans);
cout<<ans<<endl;
return 0;
}
int mulMod(ll a, ll b) {
a *= b;
a %= M;
return (int)a;
}
int addMod(int a, int b) {
long long c = a + b;
if(c >= M) {
c -= M;
}
return (int)c;
}
void readArray(ll *nums, int n) {
for(int i=0; i<n; i++) cin>>nums[i];
}
void printArray(int *nums, int n) {
for(int i=0; i<n; i++) cout<<nums[i]<<" ";
cout<<endl;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int> pi;
#define FAST ios_base::sync_with_stdio(0);cin.tie(0);
#define f first
#define s second
int n;
int32_t main() {
cin >> n;
int Y = 0;
for (int i =0;i<n;i++) {
int x; cin >> x;
Y = __gcd(Y,x);
}
cout << Y;
}
|
//compiledef
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0;i<(n);i++)
#define per(i, n) for(int i=(n);i>0;i--)
#define repx(i, n, x) for(int i=(x);i<(n);i++)
#define xper(i, n, x) for(int i=(n);i>(x);i--)
#define pback push_back
//c++def
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
const ll MOD = 1e9+7;
const ll MOD2 = 998244353;
int main(){
int a, b, c, d;
cin >> a>> b >> c >> d;
cout << (a*d) -(b*c) << endl;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int a, b, c, d;
cin >> a >> b >> c >> d;
cout << a * d - b * c << endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define set_precision(ans,l) cout << fixed << setprecision(l)<<ans;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define vi vector<int>
#define vl vector<long long int>
#define Vi vector<vector<int>>
#define vpi vector<pair<int,int>>
#define seti set<int>
#define setl set<ll>
#define dseti set<int, greater<int>>
#define dsetl set<ll, greater<ll>>
#define mseti multiset<int>
#define msetl multiset<ll>
#define dmseti multiset<int, greater<int>>
#define dmsetl multiset<ll, greater<ll>>
#define rev(str) reverse(str.begin(), str.end())
#define lb(v,x) lower_bound(v.begin(),v.end(),x)
#define ub(v,x) upper_bound(v.begin(),v.end(),x)
#define sortA(arr) sort(arr.begin(), arr.end())
#define dsortA(arr) sort(arr.begin(), arr.end(), greater<ll>())
#define ssort(arr) stable_sort(arr.begin(), arr.end())
#define nth(v,n) nth_element(v.begin,v.begin+n-1,v.end())
#define dnth(v,n) nth_element(v.begin,v.begin+n-1,v.end(), greater<ll>())
#define init(a) memset((a),0,sizeof(a))
#define pi pair<int,int>
#define pb push_back
#define pl pair<ll,ll>
#define ll long long
#define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mod 1000000007
long double EPS = 1e-9;
typedef unsigned long long ull;
typedef long double lld;
/*struct comp {
bool operator() (const pair<long long, long long> &a, const pair<long long, long long> &b) const
{return a.first*a.second>b.first*b.second;}
};*/
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif
void _print(ll t) {cerr << t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(lld t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << t;}
template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.first); cerr << ","; _print(p.second); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
ll cel(ll a,ll b){
if(a==0)return 0;
return((a-1)/b+1);
}
ll gcd(ll a, ll b){
if (a < b)swap(a, b);
return (b == 0)? a: gcd(b, a % b);
}
ll MIN(ll a,int b){ll ans;(a>=b)?ans=b:ans=a;return ans;}
ll MAX(ll a,int b){ll ans;(a>=b)?ans=a:ans=b;return ans;}
ll lcm(ll a,ll b){return (a*b)/gcd(a,b);}
ll po(ll x,ll y){
ll ans=1;
while(y){
if(y&1){ans=(ans*x)%mod;}
y>>=1;x=(x*x)%mod;
}
return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("Error.txt", "w", stderr);
#endif
FIO;
ll n,t,x,y,k;
cin>>t>>n;
ll a = cel(n*100 ,t);
cout<<(a*(100+t))/100-1;
return 0;
} | #include <iostream> // cout, endl, cin
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <vector> //vector
using namespace std;
#define _LIBCPP_DEBUG 0
#define rep(i, n) for(int i = 0; i < (int)n; i++)
typedef long long ll;
int main(){
ll n, t;
cin >> t >> n;
cout << (n * (100+t) + (t-1)) / t - 1 << endl;
} |
#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 1000000007
//cout << std::fixed << std::setprecision(15) << y << endl;
#define MOD 998244353
ll dp[10001][10001];
char g[10001][10001];
// 累乗mod
ll pmod(ll n, ll m){
if(m <= 0) return 1;
if(m == 1) return n % MOD;
if(m % 2 == 0){
return pmod(n * n % MOD, m / 2) % MOD;
}
return n * pmod(n * n % MOD, (m - 1) / 2) % MOD;
}
int main(){
ll H, W, K;
cin >> H >> W >> K;
rep(i, 1, 10001){
rep(j, 1, 10001){
dp[i][j] = 0;
g[i][j] = 'E';
}
}
ll h, w;
char c;
rep(i, 0, K){
cin >> h >> w >> c;
g[h][w] = c;
}
if(g[H][W] != 'E'){
dp[H][W] = 1;
}else{
dp[H][W] = 3;
}
ll counter = 0;
ll multCount = 1;
ll multEmpty;
ll multElse;
rev(d, 0, (H - 1) + (W - 1)){//cout << "test: " << d << endl;
multCount = 0;
REP(k, 0, d){
if(d - k <= H - 1 && k <= W - 1 && g[d - k + 1][k + 1] == 'E'){
multCount++;
}
}
multEmpty = pmod(3, multCount - 1) * 2;
multElse = pmod(3, multCount);
REP(k, 0, d){
counter = 0;
if(d - k <= H - 1 && k <= W - 1){
if(g[d - k + 1][k + 1] != 'R'){
counter += dp[d - k + 1 + 1][k + 1];
}
if(g[d - k + 1][k + 1] != 'D'){
counter += dp[d - k + 1][k + 1 + 1];
}
counter %= MOD;
if(g[d - k + 1][k + 1] == 'E'){
dp[d - k + 1][k + 1] = (counter * multEmpty) % MOD;
}else{
dp[d - k + 1][k + 1] = (counter * multElse) % MOD;
}
// cout << "update: i=" << d - k + 1 << ", j=" << k + 1 << ", dp=" << dp[d - k + 1][k + 1] << endl;
}
}
}
// REP(i, 1, 3){
// REP(j, 1, 3){
// cout << dp[i][j] << " ";
// }
// cout << endl;
// }
cout << dp[1][1] << endl;
return 0;
}
| #ifdef _DEBUG
#include "../../../library/src/debug_template.hpp"
#define DMP(...) dump(#__VA_ARGS__, __VA_ARGS__)
#else
#define DMP(...) ((void)0)
#endif
#include <cassert>
#include <cstdio>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <numeric>
#include <algorithm>
#include <bitset>
#include <functional>
using namespace std;
using lint = long long;
constexpr int INF = 1010101010;
constexpr lint LINF = 1LL << 60;
struct init {
init() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
}
} init_;
template<const int &Modulo>
struct Mint {
lint val;
constexpr Mint(lint v = 0) noexcept: val(v % Modulo) { if (val < 0) val += Modulo; }
constexpr Mint &operator+=(const Mint &r) noexcept {
val += r.val;
if (val >= Modulo) val -= Modulo;
return *this;
}
constexpr Mint &operator-=(const Mint &r) noexcept {
val -= r.val;
if (val < 0) val += Modulo;
return *this;
}
constexpr Mint &operator*=(const Mint &r) noexcept {
val = val * r.val % Modulo;
return *this;
}
constexpr Mint &operator/=(const Mint &r) noexcept {
lint a{r.val}, b{Modulo}, u{1}, v{0};
while (b) {
lint t = a / b;
a -= t * b;
a ^= b, b ^= a, a ^= b;
u -= t * v;
u ^= v, v ^= u, u ^= v;
}
val = val * u % Modulo;
if (val < 0) val += Modulo;
return *this;
}
constexpr Mint operator+(const Mint &r) const noexcept { return Mint(*this) += r; }
constexpr Mint operator-(const Mint &r) const noexcept { return Mint(*this) -= r; }
constexpr Mint operator*(const Mint &r) const noexcept { return Mint(*this) *= r; }
constexpr Mint operator/(const Mint &r) const noexcept { return Mint(*this) /= r; }
constexpr Mint operator-() const noexcept { return Mint(-val); }
constexpr bool operator==(const Mint &r) const noexcept { return val == r.val; }
constexpr bool operator!=(const Mint &r) const noexcept { return !((*this) == r); }
constexpr bool operator<(const Mint &r) const noexcept { return val < r.val; }
constexpr friend ostream &operator<<(ostream &os, const Mint<Modulo> &x) noexcept { return os << x.val; }
constexpr friend istream &operator>>(istream &is, Mint<Modulo> &x) noexcept {
lint tmp{};
is >> tmp;
x = Mint(tmp);
return is;
}
[[nodiscard]] constexpr Mint pow(lint n) const noexcept {
Mint res{1}, tmp{*this};
while (n > 0) {
if (n & 1) res *= tmp;
tmp *= tmp;
n >>= 1;
}
return res;
}
};
constexpr int MOD = 998244353;
using mint = Mint<MOD>;
int RMOD;
using rmint = Mint<RMOD>;
template<class T>
vector<T> make_vec(size_t s, T val) { return vector<T>(s, val); }
template<class... Size>
auto make_vec(size_t s, Size... tail) {
return vector<decltype(make_vec(tail...))>(s, make_vec(tail...));
}
int main() {
int H, W, K;
cin >> H >> W >> K;
auto grid = make_vec(H, W, char{});
auto dp = make_vec(H, W, mint{});
for (int i = 0; i < K; i++) {
int h, w;
char c;
cin >> h >> w >> c;
h--, w--;
grid[h][w] = c;
}
DMP(grid);
dp[0][0] = mint(3).pow(H * W - K);
constexpr mint z = mint(2) / 3;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
mint r = grid[i][j] == char{} ? z : 1;
if (j + 1 < W && grid[i][j] != 'D') dp[i][j + 1] += r * dp[i][j];
if (i + 1 < H && grid[i][j] != 'R') dp[i + 1][j] += r * dp[i][j];
}
}
DMP(dp);
cout << dp.back().back() << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define co(n) cout<<n<<endl;
const double eps=1e-10,pi=3.1415926535898;
const int mod=1e9+7,maxn=2e5+10;
int T,n,m,k,x,y,a,b;
int main(int argc, char const *argv[]) {
scanf("%d %d %d %d", &a, &b, &x, &y);
if (y>2*x) y = 2*x;
if (a > b) {
if ((a-b)&1) {
printf("%d\n", (a-b-1)*y+x);
}
else {
printf("%d\n", (a-b-1)*y+x);
}
}
else if (a == b) {
printf("%d\n", x);
}
else {
if ((b-a)&1) {
printf("%d\n", (b-a)*y+x);
}
else {
printf("%d\n", (b-a)*y+x);
}
}
return 0;
} | #include "bits/stdc++.h"
//#include "atcoder/all"
using namespace std;
//using namespace atcoder;
//using mint = modint1000000007;
#define endl "\n"
struct edge {
int v;
long long w;
edge() {}
edge(int v, long long w) : v(v), w(w) {};
};
vector<long long> dijkstra(int n, vector<vector<edge> >& G, int s) {
vector<long long> d(n, 1e18);
d[s] = 0;
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> que;
que.push(make_pair((long long)0, s));
while (!que.empty()) {
auto p = que.top();
que.pop();
int u = p.second;
long long dist = p.first;
if (dist > d[u]) continue;
for (edge e : G[u]) {
if (d[e.v] > d[u] + e.w) {
d[e.v] = d[u] + e.w;
que.push(make_pair(d[e.v], e.v));
}
}
}
return d;
}
int main() {
int a, b, x, y;
cin >> a >> b >> x >> y;
a--;
b--;
b += 100;
vector<vector<edge>> G(200);
for (int i = 0; i < 100; ++i) {
G[i].emplace_back(i + 100, x);
G[i + 100].emplace_back(i, x);
}
for (int i = 1; i <= 99; ++i) {
G[i].emplace_back(i + 99, x);
G[i + 99].emplace_back(i, x);
}
for (int i = 0; i < 99; ++i) {
G[i].emplace_back(i + 1, y);
G[i + 1].emplace_back(i, y);
G[i + 100].emplace_back(i + 101, y);
G[i + 101].emplace_back(i + 100, y);
}
auto d = dijkstra(200, G, a);
cout << d[b] << endl;
return 0;
} |
///In the name of GOD
//#pragma GCC optimize("O2")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MXN = 2e5 + 10;
const ll INF = 1e18;
const ll LOG = 20;
ll n, k;
ll h[MXN], dis[MXN], Jad[LOG][MXN];
vector<ll> adj[MXN], Ver;
void DFS(ll u, ll par, ll d, ll m){
if(d > m || d >= dis[u]) return;
dis[u] = d;
for(auto v : adj[u]){
if(v != par) DFS(v, u, d + 1, m);
}
}
void dfs(ll u, ll par){
Jad[0][u] = par;
for(int i = 1; i < LOG; i ++){
Jad[i][u] = Jad[i - 1][Jad[i - 1][u]];
}
Ver.push_back(u);
for(auto v : adj[u]){
if(v != par) h[v] = h[u] + 1, dfs(v, u);
}
}
ll K_Jad(ll u, ll k){
for(int i = 0; i < LOG; i ++){
if((k >> i) & 1LL) u = Jad[i][u];
}
return u;
}
ll check(ll x){
ll ans = 0;
for(int i = 1; i <= n; i ++) dis[i] = INF;
for(auto u : Ver){
if(dis[u] <= x) continue;
ans ++;
DFS(K_Jad(u, min(h[u], x)), 0, 0, x);
}
return ans;
}
int main(){
ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
cin >> n >> k;
for(int i = 1; i < n; i ++){
ll u, v; cin >> u >> v;
adj[u].push_back(v), adj[v].push_back(u);
}
dfs(1, 0);
sort(Ver.begin(), Ver.end(), [&](const int& u, const int &v){
return h[u] > h[v];
});
ll l = 0, r = n;
while(r - l > 1){
ll mid = (l + r) / 2;
if(check(mid) <= k) r = mid;
else l = mid;
}
cout << r << '\n';
return 0;
}
/*!
HE'S AN INSTIGATOR,
ENEMY ELIMINATOR,
AND WHEN HE KNOCKS YOU BETTER
YOU BETTER LET HIM IN.
*/
//! N.N
| #include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
// using mint = modint1000000007;
// // using mint = modint998244353;
typedef int64_t Int;
#define all(x) (x).begin(), (x).end()
const double EPS = 1e-10;
const Int INF = 1e18;
const int inf = 1e9;
const Int mod = 1e9+7;
template<class T>
istream &operator>>(istream &is, vector<T> &v) {
for (auto &e : v) {
is >> e;
}
return is;
}
bool print_space_enable = false;
void print() {
std::cout << '\n';
print_space_enable = false;
}
template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
if (print_space_enable) std::cout << " ";
std::cout << fixed << setprecision(15) << head;
print_space_enable = true;
print(std::forward<Tail>(tail)...);
}
template<typename T>
void print(vector<T> v) {
for (size_t i = 0; i < v.size(); i++) {
if (i > 0) std::cout << " ";
std::cout << v[i];
}
std::cout << '\n';
}
template<class T>
vector<T> make_vec(size_t n, T val) {
return vector<T>(n, val);
}
template<class... Ts>
auto make_vec(size_t n, Ts... ts) {
return vector<decltype(make_vec(ts...))>(n, make_vec(ts...));
}
void fast_IO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
Int rec(vector<Int> &dp, Int state) {
if (dp[state] != INF) return dp[state];
for (Int bit = state & (state - 1); bit > 0; bit = (state & (bit - 1))) {
dp[state] = min(dp[state], rec(dp, bit) + rec(dp, state - bit));
}
return dp[state];
}
void solve() {
fast_IO();
Int n, m;
cin >> n >> m;
vector<Int> a(m), b(m);
auto c = make_vec(n, n, false);
for (Int i = 0; i < m; i++) {
cin >> a[i] >> b[i];
a[i]--, b[i]--;
c[a[i]][b[i]] = c[b[i]][a[i]] = true;
}
auto dp = make_vec(1<<n, INF);
dp[0] = 0;
vector<bool> d(1<<n, true);
for (Int bit = 1; bit < (1<<n); bit++) {
for (Int i = 0; i < n - 1; i++) {
for (Int j = i + 1; j < n; j++) {
if ((bit & (1<<i)) and (bit & (1<<j)) and (not c[i][j])) {
d[bit] = false;
}
}
}
if (d[bit]) {
dp[bit] = 1;
}
}
print(rec(dp, (1<<n) -1));
}
int main() {
solve();
//Int _t; cin >> _t; while (_t--) solve();
return 0;
}
|
#include <iostream>
using namespace std;
int main()
{
int a,b,c;
cin>>a>>b>>c;
cout<<a-b+c;
return 0;
}
|
#define ll long long
#include<bits/stdc++.h>
using namespace std;
int findeven(std::vector<int> v)
{
for(int i=0;i<v.size();i++)
{
if(v[i]%2==0)
return i;
}
return -1;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
/* code */
int a,b,c;
cin>>a>>b>>c;
int a1,b1,c1;
a1 = 7-a;
b1 = 7-b;
c1 = 7-c;
cout<<a1+b1+c1;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vpii vector<pair<int,int>>
#define vpll vector<pair<ll,ll>>
#define fr(i,k,n) for (int i = k; i < n; ++i)
#define fri(i,k,n) for (int i = k; i >= n; --i)
#define pb push_back
#define mp make_pair
#define all(arr) arr.begin(),arr.end()
#define ff first
#define ss second
const double pi=3.1415926535897932384626433832795;
const int inf=1e9;
const ll inf2=1e18;
const int mod=1e9+7;
void boost(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
int adj[20][20],n;
vi dp(1<<20,inf);
void solve(){
int m;
cin>>n>>m;
int u,v;
fr(i,0,m){
cin>>u>>v;
u--;
v--;
adj[u][v]=adj[v][u]=1;
}
for(int i=1;i<(1<<n);i++){
if(!(i&(i-1))){
dp[i]=1;
continue;
}
dp[i]=1;
for(int j=0;j<n;j++){
if((i>>j)&1){
for(int k=j+1;k<n;k++){
if((i>>k)&1){
if(adj[j][k]==0)
dp[i]=inf;
}
}
}
}
for(int j=(i-1)&i;j;j=(j-1)&i){
dp[i]=min(dp[i],dp[j]+dp[i^j]);
}
}
cout<<dp[(1<<n)-1]<<endl;
return;
}
int main()
{
boost();
int tc=1;
//cin>>tc;
while(tc--)
solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18;
const int inf = 1e9;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = b - 1; i >= a; i--)
using pint = pair<ll, ll>;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
//snukeさんの答え
int main() {
vector<string> s(3);
rep(i, 0, 3) cin >> s[i];
map<char, ll> mp;
set<char> heads;
rep(i, 0, 3) {
reverse(s[i].begin(), s[i].end());
ll t = 1;
if (i == 2) t = -1;
for (char c : s[i]) {
mp[c] += t;
t *= 10;
}
reverse(s[i].begin(), s[i].end());
heads.insert(s[i][0]);
}
if (mp.size() > 10) {
cout << "UNSOLVABLE\n";
return 0;
}
vector<int> p(10);
iota(p.begin(), p.end(), 0);
do {
int i = 0;
ll sum = 0;
for (auto x : mp) {
char c = x.first;
ll t = x.second;
sum += t * p[i];
if (p[i] == 0 && heads.count(c) != 0) sum += 1e15;
++i;
}
if (sum == 0) {
i = 0;
for (auto &x : mp) {
x.second = p[i];
++i;
}
// if (mp[s[0][0]] == 0 || mp[s[1][0]] == 0 || mp[s[2][0]] == 0) continue;
rep(i, 0, 3) {
rep(j, 0, s[i].size()) {
cout << mp[s[i][j]];
}
cout << "\n";
}
return 0;
}
} while (next_permutation(p.begin(), p.end()));
cout << "UNSOLVABLE\n";
} |
#include<iostream>
#include<bits/stdc++.h>
#define forf(i,start,n,increment) for(int i = start;i<n;i+=increment)
#define all(i) i.begin(),i.end()
#define traverse(container,it) for(typeof(container.begin()) it = container.begin();it != container.end();it++)
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
vector<int>v(3);
cin>>v[0]>>v[1]>>v[2];
sort(all(v));
cout<<v[2]+v[1];
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main(){
int a, b, c; cin >> a >> b >> c;
if((a >= b && b >= c) || (b >= a && a >= c) ) cout << a + b << endl;
else if((a >= c && c >= b) || (c >= a && a >= b) ) cout << a + c << endl;
else if((c >= b && b >= a) || (b >= c && c >= a) ) cout << b + c << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define maxn 1005
int n,k,tot,tt;
int a[maxn][maxn];
int vis[maxn][maxn];
int sum[maxn][maxn];
int you[maxn][maxn];
int num[maxn*maxn];
map<int,int>to,to1;
bool check(int x)
{
memset(sum,0,sizeof(sum));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(a[i][j]<=x) vis[i][j]=1;
else vis[i][j]=0;
sum[j][i]=sum[j][i-1]+vis[i][j];
you[j][i]=you[j][i-1]+(a[i][j]==x);
}
}
// for(int i=1;i<=n;i++)
// {
// for(int j=1;j<=n;j++)
// cout<<vis[i][j]<<' ';
// cout<<endl;
// }
// for(int j=1;j<=n;j++)
// {
// for(int i=1;i<=n;i++)
// cout<<sum[j][i]<<' ';
// cout<<endl;
// }
int ret=0;
for(int i=k;i<=n;i++)
{
int ans=0;
int yo=0;
for(int p=1;p<=k;p++)
{
ans+=sum[p][i]-sum[p][i-k];
yo+=you[p][i]-you[p][i-k];
}
if(ans>=(k*k-k*k/2)) return 1;
int l=1;
for(int j=k+1;j<=n;j++)
{
ans-=sum[l][i]-sum[l][i-k];
yo-=you[l][i]-you[l][i-k];
l++;
ans+=sum[j][i]-sum[j][i-k];
yo+=you[j][i]-you[j][i-k];
if(ans>=(k*k-k*k/2)) return 1;
}
}
return 0;
}
int ef(int l,int r)
{
if(r-l>1)
{
int mid=(l+r)>>1;
if(check(mid)) return ef(l,mid);
return ef(mid,r);
}
if(check(l)) return l;
return r;
}
signed main()
{
cin>>n>>k;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
cin>>a[i][j];
num[++tt]=a[i][j];
}
sort(num+1,num+tt+1);
for(int i=1;i<=tt;i++) if(!to[num[i]]) to[num[i]]=++tot,to1[tot]=num[i];
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
a[i][j]=to[a[i][j]];
int l=1,r=tot;
// for(int i=1;i<=tot;i++) if(check(i)) {cout<<to1[i];return 0;}
cout<<to1[ef(l,r)];
// if(check(l)) cout<<to1[l];
// else cout<<to1[r];
return 0;
} | #include <bits/stdc++.h>
using namespace std;
map<vector<int>, long long> dp;
long long gcd(long long x, long long y) {
while (y) {
long long t = x;
x = y;
y = t % y;
}
return x;
}
bool coprime[80][80];
long long f(vector<int> idxs) {
if (idxs.size() == 0) return 1;
if (dp.find(idxs) != dp.end()) return dp[idxs];
long long ret = 1;
for (int i = 0; i < idxs.size(); ++i) {
vector<int> nidxs;
for (int j = i + 1; j < idxs.size(); ++j) {
if (coprime[idxs[i]][idxs[j]]) nidxs.push_back(idxs[j]);
}
ret += f(nidxs);
}
return dp[idxs] = ret;
}
int solve() {
long long a, b;
scanf("%lld %lld", &a, &b);
vector<int> idxs(b - a + 1);
for (int i = 0; i <= b - a; ++i) {
for (int j = i + 1; j <= b - a; ++j) {
coprime[i][j] = (gcd(a + i, a + j) == 1);
}
idxs[i] = i;
}
printf("%lld\n", f(idxs));
return 0;
}
int main() {
int t = 1;
// scanf("%d", &t);
for (int tc = 0; tc < t; ++tc) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define rng(i, a, b) for (int i = int(a); i < int(b); ++i)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); --i)
#define per(i, b) gnr(i, 0, b)
#define all(obj) begin(obj), end(obj)
#define allr(obj) rbegin(obj), rend(obj)
#define sz(x) (int)(x).size()
#define cinv(a) rep(i, sz(a)) cin >> a[i]
#define debug(a) rep(i, sz(a)) cout << a[i] << " \n"[i == sz(a) - 1]
#define show(x) cerr << #x << " = " << x << endl;
#define pb push_back
#define pf push_front
#define eb emplace_back
typedef long long ll;
typedef pair<int, int> Pi;
typedef vector<Pi> vp;
typedef vector<vp> vvp;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
template <typename T> bool chmax(T &a, const T &b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
template <typename T> bool chmin(T &a, const T &b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, 1, 0, -1};
const int MOD = 998244353;
const int INF = 1e9 + 9;
const ll LINF = 1e18;
const long double eps = 1e-10;
const char nl = '\n';
ll power(ll a, ll b, int mod) { return b ? power(a * a % mod, b / 2, mod) * (b % 2 ? a : 1) % mod : 1; }
ll nCk(int n, int k, int mod)
{
ll x = 1, y = 1;
for (int i = 1; i <= k; ++i)
{
x = x * (n - i + 1) % mod;
y = y * i % mod;
}
return x * power(y, mod - 2, mod) % mod;
}
struct UF
{
vi d;
UF(int n) : d(n, -1) {}
int root(int x)
{
if (d[x] < 0) return x;
return d[x] = root(d[x]);
}
bool unite(int x, int y)
{
x = root(x);
y = root(y);
if (x == y) return false;
if (d[x] > d[y]) swap(x, y);
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) { return root(x) == root(y); }
int size(int x) { return -d[root(x)]; }
};
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(20);
int n; cin >> n;
vl A(n); cinv(A);
sort(all(A));
ll ans = 0;
ll sum = 0;
per(i, n)
{
ans += A[i] * (sum + A[i]) % MOD;
ans %= MOD;
sum = (2 * sum + A[i]) % MOD;
}
cout << ans << nl;
} | #include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <iomanip>
#include <functional>
#include <bitset>
#include <limits>
#include <cstdio>
#include <cmath>
#include <cassert>
#include <random>
#ifdef DEBUG
#include "library/Utility/debug.cpp"
#else
#define debug(...)
#endif
#define rep(i,n) for(int i=0;i<(n);++i)
#define EL '\n'
#define print(i) std::cout << (i) << '\n'
#define all(v) (v).begin(), (v).end()
using lnt = long long;
struct FIO{FIO(){std::cin.tie(0);std::ios_base::sync_with_stdio(0);std::cout<<std::fixed<<std::setprecision(15);}}fIO;
template<typename T> using V = std::vector<T>;
template<typename T> void fill(V<T>&v) { for(T&e:v) std::cin >> e; }
/*-*/
constexpr lnt MOD = 998244353;
struct mint
{
lnt v;
mint():v(0){}
mint(lnt v):v((v+MOD)%MOD){}
mint operator-()const{ return mint(0) - *this; }
mint& operator+=(const mint& a){ if((v+=a.v)>=MOD) v-=MOD; return *this; }
mint& operator-=(const mint& a){ if((v+=MOD-a.v)>=MOD) v-=MOD; return *this; }
mint& operator*=(const mint& a){ (v*=a.v)%=MOD; return *this; }
mint& operator/=(const mint& a){ (*this) *= a.inv(); 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 operator/(const mint& a)const{ return mint(*this) /= a; }
bool operator<(const mint& a)const{ return v < a.v; }
bool operator==(const mint& a)const{ return v == a.v; }
mint pow(lnt k)const{ mint r(1),t(v); while(k){ if(k&1) r*=t; t*=t; k>>=1; } return r; }
mint inv()const{ return pow(MOD-2); }
static mint comb(lnt n, lnt k) { if(n-k<k) k=n-k; mint num(1), dom(1); for(int i=0;i<k;i++) { num*=n-i; dom*=i+1; } return num/dom; }
static std::vector<mint> construct_comb(int n) {
std::vector<mint> c(n+1); mint a = 1; c[0] = a; for(int i=1;i<=n;i++) { a = a*mint(n+1-i)/i; c[i] = a; } return c;
}
static std::vector<mint> construct_fact(int n) { std::vector<mint> f(n+1,1); for(int i=2;i<=n;i++) f[i]=f[i-1]*i; return f; }
};
std::istream& operator>>(std::istream&i,mint&a){ lnt t; i>>t; a=mint(t); return i; }
std::ostream& operator<<(std::ostream&o,const mint&a){ o<<a.v; return o; }
int main() {
int n;
std::cin >> n;
V<int> f(n);
fill(f);
rep(i,n) f[i]--;
int count=0;
V<int> went(n,-1);
rep(i,n) {
if(went[i]==-1) {
int ok=true;
int o=i;
while(went[o]==-1) {
went[o]=i;
o=f[o];
}
if(went[o]!=i) ok=false;
if(ok) count++;
}
}
print(mint(2).pow(count)-mint(1));
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> iint;
typedef pair<ll,ll> llll;
#define ALL(x) (x).begin(),(x).end()
const ll zero = 0;
const ll one = 1;
const ll INF = 9223372036854775807; //10^18
const int inINF = 2147483647; //10^9
const ll MOD = 1000000007; //10^9+7
const ll MOD2 = 998244353;
void Yes() {printf("Yes\n");}
void No() {printf("No\n");}
void YES() {printf("YES\n");}
void NO() {printf("NO\n");}
int main(){
ll N;
cin >> N;
string S;
ll P = 2;
ll ans = 1;
for(int i = 0; i < N; i++){
cin >> S;
if(S == "OR"){
ans += P;
}
P *= 2;
}
cout << ans << endl;
} | #include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl "\n"
#define rep(i,n) repi(i,0,n)
#define repi(i,a,n) for(ll i=a;i<(ll)n;++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define Show(val) cout<<(val)<<" "
#define Showln(val) cout<<(val)<<endl
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
// using P = pair<int, int>;
void YN(bool a) { cout << (a ? "Yes" : "No"); }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
ll ans[2][63][2];
void solve()
{
ll n;
cin >> n;
string s;
// ans[i][j][k] = 自分がiの時にj番目の計算をして、数値がkになるものの個数
ans[0][0][0] = 1;
ans[0][0][1] = 1;
ans[1][0][0] = 0;
ans[1][0][1] = 0;
repi(i, 1, n + 1)
{
cin >> s;
if (s == "AND")
{
ans[0][i][0] = ans[0][i - 1][0] + ans[1][i - 1][0] + ans[0][i - 1][1] + ans[1][i - 1][1];
ans[1][i][0] = ans[0][i - 1][0] + ans[1][i - 1][0];
ans[0][i][1] = 0;
ans[1][i][1] = ans[0][i - 1][1] + ans[1][i - 1][1];
}
else // if (s == "OR")
{
ans[0][i][0] = ans[0][i - 1][0] + ans[1][i - 1][0];
ans[1][i][0] = 0;
ans[0][i][1] = ans[0][i - 1][1] + ans[1][i - 1][1];
ans[1][i][1] = ans[0][i - 1][0] + ans[1][i - 1][0] + ans[0][i - 1][1] + ans[1][i - 1][1];
}
}
cout << (ans[0][n][1] + ans[1][n][1]) << endl;
}
int main()
{
fastio;
solve();
return 0;
} |
#include<iostream>
#include<array>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
#include<cassert>
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
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 Kth_root(ll x,int k){
if(x==1) return 1;
if(k>64) return 1;
ll l=0,r=numeric_limits<ll>::max();
while(r-l>1){
ll mid=(l+r)/(ll)2;
ll d=mid;
bool larger=false;
rep(i,k-1){
if(d>x/mid+(ll)1){
larger=true;
break;
}
d*=mid;
}
if(d>x) larger=true;
if(larger) r=mid;
else l=mid;
}
return l;
}
string x;
ll m;
void solve(){
cin >> x;
cin >> m;
int n=x.length();
int ans=0;
int l=0;
rep(i,n){
chmax(l,x[i]-'0');
}
if(n==1){
if(x[0]-'0'<=m) cout << 1 << endl;
else cout << 0 << endl;
return;
}
if(n==2){
ll a=x[0]-'0',b=x[1]-'0';
cout << max(0ll,(m-b)/a-l) << endl;
return;
}
ll L=l,R=mod;
while(R-L>1){
ll mid=(L+R)/2;
ll A=0,D=1;
rep(i,n){
A+=D*(x[n-1-i]-'0');
if(i<n-1) D*=mid;
if(D>m || D<0) break;
}
if(D>m || D<0 || A>m) R=mid;
else L=mid;
}
cout << L-l << endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(50);
solve();
} | #include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
int n;
ll x;
cin >> n >> x;
vector<ll> a(n);
for (int i = 0; i < n; ++i)
cin >> a[i];
unordered_map<ll, ll> v;
v[x] = 1;
ll ans = 0;
for (int i = 0; i < n; ++i) {
unordered_map<ll, ll> nv;
for (auto [c, f] : v) {
if (i + 1 < n) {
ll rem = c % a[i + 1];
nv[c - rem] += f;
if (rem > 0)
nv[c + a[i + 1] - rem] += f;
} else {
if (c % a[i] == 0)
nv[0] += f;
}
}
v = move(nv);
}
cout << v[0];
} |
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define fast ios_base::sync_with_stdio(0);cin.tie(0)
#define f0(a,b,c) for(int a=b;a<c;a++)
#define re(a,b,c) for(int a=b;a>=c;a--)
#define trav(a,x) for(auto &a:x)
#define ini(a,x) memset(a,x,sizeof(a))
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(con) con.begin(),con.end()
#define rall(con) con.rbegin(),con.rend()
#define ll long long
#define vi vector<int>
#define vll vector<ll>
#define MOD 1000000007
#define pi pair<int,int>
#define pll pair<ll,ll>
#define mi map<int,int>
#define mll map<ll,ll>
#define umi unordered_map<int,int>
#define umll unordered_map<ll,ll>
#define lb lower_bound
#define ub upper_bound
#define pq priority_queue<ll,ll>
#define st stack<int>
#define qu queue<int>
#define endl "\n"
#define sz(a) (int)a.size()
using ull = unsigned long long;
template<class T> int chkmax(T &a, T b) {
if (b > a) {
a = b;
return 1;
}
return 0;
}
template<class T> int chkmin(T &a, T b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
template<class iterator> void output(iterator begin, iterator end, ostream& out = cerr) {
while (begin != end) {
out << (*begin) << " ";
begin++;
}
out << endl;
}
template<class T> void output(T x, ostream& out = cerr) {
output(x.begin(), x.end(), out);
}
ll powm(ll a , ll n)
{
ll res = 1;
while(n)
{
if(n & 1)
res = (res * a);
n >>= 1;
a = (a * a);
}
return res;
}
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
return (ull)rng() % B;
}
ll add(ll x, ll y) {
x += y;
if (x >= MOD) return x - MOD;
return x;
}
ll sub(ll x, ll y) {
x -= y;
if (x < 0) return x + MOD;
return x;
}
ll mult(ll x, ll y) {
return (x * y) % MOD;
}
ll bin_pow(ll x, ll p) {
if (p == 0) return 1;
if (p & 1) return mult(x, bin_pow(x, p - 1));
return bin_pow(mult(x, x), p / 2);
}
ll rev(ll x) {
return bin_pow(x, MOD - 2);
}
struct{
int x1,y1,x2,y2,x3,y3;
} m;
int main()
{
fast;
int t=1;
//cin>>t;
while(t--)
{
ll n,x;
cin>>n>>x;
vll a(n);
f0(i,0,n) cin>>a[i];
int k=n/2;
ll ans=0;
set<ll,greater<ll>> possible;
f0(i,0,(1<<k))
{
ll temp=0;
f0(j,0,k)
{
if(i >> j & 1)
{
temp+=a[j];
}
}
if(temp<=x)
{
possible.insert(temp);
chkmax(ans,temp);
}
}
k=n-k;
f0(i,0,(1<<k))
{
ll temp=0;
f0(j,0,k)
{
if(i >> j & 1)
{
temp+=a[n/2+j];
}
}
if(temp<=x)
{
chkmax(ans,temp);
}
auto y=possible.lb(x-temp);
if(y!=possible.end())
chkmax(ans,temp + *y);
}
cout<<ans<<endl;
}
return 0;
} | #include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int main() {
int N,T;
int a[45];
cin>>N>>T;
for(int i=0; i<N; i++) cin>>a[i];
vector<ll> p(1<<N/2),q(1<<(N-N/2));
for(int i=0; i<1<<(N/2); i++) {
for(int j=0; j<N/2; j++)
{
if( i & 1<<j ) {
p[i]+=a[j];
}
}
}
for(int i=0; i<1<<(N-N/2); i++) {
for(int j=0; j<N-N/2; j++)
{
if( i & 1<<j ) {
q[i]-=a[N/2+j];
}
}
}
sort(q.begin(), q.end());
ll ans = 0;
for(int i=0; i<p.size(); i++) {
if(T<p[i]) continue;
int L = 0; int R = q.size()-1;
int idx = lower_bound(q.begin(), q.end(), -T+p[i]) - q.begin();
ans = max(ans, -q[idx]+p[i]);
}
cout<<ans<<endl;
}
|
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <bitset>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <iostream>
#include <string>
#include <array>
#include <random>
#include <chrono>
#include <vector>
using namespace std;
#ifndef ONLINE_JUDGE
#define File(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
#else
#define File(s)
#endif
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define mkp make_pair
#define pb push_back
#define SP putchar(' ')
#define EL putchar('\n')
mt19937 rnd(chrono :: steady_clock :: now().time_since_epoch().count());
mt19937_64 rnd64(chrono :: steady_clock :: now().time_since_epoch().count()) ;
template<class T> inline void gi(T &x){
char ch; x = 0;int f = 1;
while(isspace(ch = getchar()));
if(ch == '-') ch = getchar(), f = -1;
do x = x * 10 + ch - '0'; while(isdigit(ch = getchar()));
x *= f;
}
template<class T> inline void wi(T x){
static char stk[128]; int top = 0;
if(x == 0) putchar('0');
if(x < 0) putchar('-'), x = -x;
while(x) stk[++top] = x % 10, x /= 10;
while(top) putchar(stk[top--] + '0');
}
template<class T, class ...Args>
inline void gi(T &x, Args&... args) {gi(x); gi(args...);}
template<class T, class ...Args>
inline void wi(T x, Args... args) {wi(x); SP; wi(args...);}
template<class T> void upmax(T &x, T y) {x = x > y ? x : y;}
template<class T> void upmin(T &x, T y) {x = x < y ? x : y;}
const int N = 400005;
vector<int> G[N];
int a[N], b[N];
int cnt = 0;
bool tree = false;
bool vis[N];
void dfs(int x, int fa) {
vis[x] = true;
++cnt;
for (int v : G[x]) {
if (vis[v] && v != fa) tree = false;
if (!vis[v]) dfs(v, x);
}
}
int main() {
File("b");
int n, res = 0;
gi(n);
for (int i = 1; i <= n; ++i) gi(a[i], b[i]), G[a[i]].pb(b[i]), G[b[i]].pb(a[i]);
for (int i = 1; i <= 400000; ++i) {
if (!vis[i]) {
cnt = 0;
tree = true;
dfs(i, -1);
res += cnt - 1 + int(!tree);
}
}
printf("%d\n", res);
return 0;
} | #define _GLIBCXX_DEBUG
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
// clang-format off
#define PI 3.14159265358979323846264338327950L
#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 prep_ios(do_flush) if(!do_flush){ ios::sync_with_stdio(false);cin.tie(nullptr);constexpr char endl='\n'; } cout<<fixed<<setprecision(20)
#define put(expr) cout<<expr<<'\n'
template<typename T> ostream &operator<<(ostream &os, const vector<T> &vec){ os << "[ "; for (auto v: vec) os << v << " "; os << "]"; return os; }template<typename T> ostream &operator<<(ostream &os, const deque<T> &vec){ os << "< "; for (auto v: vec) os << v << " "; os << ">"; return os; }
template<typename T> ostream &operator<<(ostream &os, const set<T> &vec){ os << "{ "; for (auto v: vec) os << v << " "; os << "}"; return os; }template<typename T> ostream &operator<<(ostream &os, const unordered_set<T> &vec){ os << "{ "; for (auto v: vec) os << v << " "; os << "}"; return os; }
template<typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp){ os << "{ "; for (auto v: mp) os << v.first << ": " << v.second << ", "; os << "}"; return os; }template<typename TK, typename TV> ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp){ os << "{ "; for (auto v: mp) os << v.first << ": " << v.second << ", "; os << "}"; return os; }
template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa){ os << "(" << pa.first << ", " << pa.second << ")"; return os; }
typedef int8_t i8; typedef int16_t i16; typedef int32_t i32; typedef int64_t i64; typedef uint8_t u8; typedef uint16_t u16; typedef uint32_t u32; typedef uint64_t u64; typedef float f32; typedef double f64; typedef long double f80;
typedef vector<int> vi; typedef vector<vector<int>> vvi; typedef map<int, int> mii;
typedef __int128_t i128; i128 parse(string &s) { i128 ret = 0; for (int i = 0; i < s.length(); i++) if ('0' <= s[i] && s[i] <= '9') ret = 10 * ret + s[i] - '0'; return ret; } istream &operator>>(istream &is, i128 &v) { string s; is >> s; v = parse(s); return is; } ostream &operator<<(ostream &os, const i128 &v) { if (v == 0) return (os << "0"); i128 num = v; if (v < 0) { os << '-'; num = -num; } string s; for (; num > 0; num /= 10) s.push_back('0' + (char)(num % 10)); reverse(s.begin(), s.end()); return (os << s); }
template <typename T> bool chmax(T &now, const T &cand) { if (now < cand) { now = cand; return true; } return false; } template <typename T> bool chmin(T &now, const T &cand) { if (now > cand) { now = cand; return true; } return false; }
template <typename T> T gcd(const T &a, const T &b) { if (a % b == 0) return b; return gcd(b, a % b); } template <typename T> T lcm(const T &a, const T &b) { return a * b / gcd(a, b); }
template <typename T> T nCr(const T &n, T k) { if (k > n) return 0; if (k * 2 > n) k = n - k; if (k == 0) return 1; int result = n; for (int i = 2; i <= k; ++i) { result *= (n - i + 1); result /= i; } return result; }
const int INF = 1<<30; /* INF > 10^9 */ const i64 INFLL = 1LL<<60; /* INFLL > 10^18 */ const string YES = "Yes", NO = "No";
// clang-format on?
int main() {
prep_ios(true);
i64 N, X;
cin >> N >> X;
// vector<i64> Fs(202020, 0);
vector<pair<i64, i64>> Fs;
rep(i, N) {
i64 a =0; i64 b = 0;
cin >> a >> b;
Fs.push_back({a, b});
};
sort(Fs.begin(), Fs.end());
rep(i, N) {
auto pf = Fs[i];
if (pf.first > X){
break;
} else {
X += pf.second;
}
}
put(X);
}
|
#include <iostream>
using namespace std;
typedef long double ld;
int a[100],ok[100010] = {};
ld dp[100010],sum[200010];
int main(){
cout.precision(20);
int i,j,n,m,k;
cin >> n >> m >> k;
for(i=0;i<k;i++){
cin >> a[i]; ok[a[i]] = 1;
}
for(i=0;i<k;i++){
for(j=i;j<k;j++){
if(a[j] - a[i] + 1==j - i + 1 && j - i + 1>=m){
cout << -1 << endl;
return 0;
}
}
}
ld le = 0,ri = 1000000000000000000;
for(int t = 0;t<100;t++){
ld mid = (le + ri)/2;
dp[n] = 0;
for(i=0;i<=n;i++) sum[i] = 0;
for(i=n - 1;i>=1;i--){
dp[i] = ((ld)1/(ld)m)*(sum[i + 1] - sum[i + m + 1]) + 1;
if(ok[i]) dp[i] = mid;
sum[i] = sum[i + 1] + dp[i];
}
ld y = (sum[1] - sum[m + 1])/(ld)(m) + 1;
dp[0] = y;
if(mid>y) ri = mid;
else le = mid;
}
cout << le << endl;
}
| #include <bits/stdc++.h>
#define double long double
using namespace std;
#define debug(x) cerr << #x << " = " << x << endl
#define boost ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); \
stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) { cout << endl; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << " ";
err(++it, args...);
}
template<typename T>
inline void read(T& x) {
x = 0; T f = 1; char ch = getchar();
while (!isdigit(ch)) f = (ch == '-') ? -1 : 1, ch = getchar();
while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar();
x *= f;
}
template<typename T, typename ...Args>
inline void read(T& x, Args&... args) { read(x), read(args...); }
constexpr int MAXN = 1e5 + 5, MOD = 1e9 + 7;
int n, m, k;
array<int, 11> a;
array<double, MAXN * 2> sufa, sufb;
int main() {
boost;
cin >> n >> m >> k;
cout << setprecision(4) << fixed;
set<int> s;
for (int i = 1; i <= k; i++) cin >> a[i], s.insert(a[i]);
for (int i = n - 1; i >= 0; i--) {
if (s.count(i)) {
sufa[i] = sufa[i + 1] + 1;
sufb[i] = sufb[i + 1];
continue;
}
double tmpa = (sufa[i + 1] - sufa[i + m + 1]) / m;
double tmpb = (sufb[i + 1] - sufb[i + m + 1]) / m + 1;
sufa[i] = sufa[i + 1] + tmpa;
sufb[i] = sufb[i + 1] + tmpb;
if (i == 0) {
if (abs(1.0 - tmpa) > 1e-15)cout << tmpb / (1.0 - tmpa) << endl;
else cout << -1 << endl;
}
}
return 0;
} |
#include <stdio.h>
#include <iostream>
#include <vector>
#include <queue>
#include <random>
#include <fstream>
using ll = long long int;
using P = std::pair<ll, ll>;
const int INF = (1<<30);
const ll INFLL = (1ll<<60);
const ll MOD = (ll)(1e9+7);
const int TT = 157;
const int SS = 5;
#define l_ength size
void mul_mod(ll& a, ll b){
a *= b;
a %= MOD;
}
void add_mod(ll& a, ll b){
a = (a<MOD)?a:(a-MOD);
b = (b<MOD)?b:(b-MOD);
a += b;
a = (a<MOD)?a:(a-MOD);
}
int x[222],y[222],r[222],a[222],b[222],c[222],d[222];
bool mp[10000][10000];
bool fil_row(int p, int l, int r){
int i;
bool flag = false;
if(!(0 <= p && p < 10000)){
return false;
}
for(i=l; i<r; ++i){
flag |= mp[p][i];
}
if(flag){
return false;
}
for(i=l; i<r; ++i){
mp[p][i] = true;
}
return true;
}
bool fil_col(int p, int l, int r){
int i;
bool flag = false;
if(!(0 <= p && p < 10000)){
return false;
}
for(i=l; i<r; ++i){
flag |= mp[i][p];
}
if(flag){
return false;
}
for(i=l; i<r; ++i){
mp[i][p] = true;
}
return true;
}
bool del_row(int p, int l, int r, int s){
int i;
if(a[s] >= c[s]){
return true;
}
if(b[s] >= d[s]){
return true;
}
if(!(a[s] <= x[s] && x[s] < c[s])){
return true;
}
if(!(b[s] <= y[s] && y[s] < d[s])){
return true;
}
for(i=l; i<r; ++i){
mp[p][i] = false;
}
return false;
}
bool del_col(int p, int l, int r, int s){
int i;
if(a[s] >= c[s]){
return true;
}
if(b[s] >= d[s]){
return true;
}
if(!(a[s] <= x[s] && x[s] < c[s])){
return true;
}
if(!(b[s] <= y[s] && y[s] < d[s])){
return true;
}
for(i=l; i<r; ++i){
mp[i][p] = false;
}
return false;
}
bool score(int i, int j){
int before,after;
before = (c[i]-a[i])*(d[i]-b[i]);
if(j<2){
after = before + (d[i]-b[i]);
}else if(j<4){
after = before + (c[i]-a[i]);
}else if(j<6){
after = before - (d[i]-b[i]);
}else{
after = before - (c[i]-a[i]);
}
return 1ll*std::min(after,r[i])*std::max(before,r[i]) > 1ll*std::min(before,r[i])*std::max(after,r[i]);
}
int main(void){
int n,i,tm,j,k,calc=33*32;
tm = clock();
std::random_device seed_gen;
std::default_random_engine engine(seed_gen());
scanf("%d",&n);
std::uniform_int_distribution<> rnd1(0,n-1);
std::uniform_int_distribution<> rnd2(0,8);
for(i=0; i<n; ++i){
scanf("%d%d%d",&x[i],&y[i],&r[i]);
mp[x[i]][y[i]] = true;
a[i] = x[i]; b[i] = y[i]; c[i] = x[i]+1; d[i] = y[i]+1;
}
while(clock() - tm < ((TT * CLOCKS_PER_SEC) >> SS)){
i = rnd1(engine);
j = rnd2(engine);
if(!score(i,j) && ((seed_gen())%(calc>>4))){
continue;
}
switch(j){
case 0:
if(fil_row(a[i]-1,b[i],d[i])){
--a[i];
}
break;
case 1:
if(fil_row(c[i],b[i],d[i])){
++c[i];
}
break;
case 2:
if(fil_col(b[i]-1,a[i],c[i])){
--b[i];
}
break;
case 3:
if(fil_col(d[i],a[i],c[i])){
++d[i];
}
break;
case 4:
++a[i];
if(del_row(a[i]-1,b[i],d[i],i)){
--a[i];
}
break;
case 5:
--c[i];
if(del_row(c[i],b[i],d[i],i)){
++c[i];
}
break;
case 6:
++b[i];
if(del_col(b[i]-1,a[i],c[i],i)){
--b[i];
}
break;
default:
--d[i];
if(del_col(d[i],a[i],c[i],i)){
++d[i];
}
break;
}
++calc;
}
for(i=0; i<n; ++i){
printf("%d %d %d %d\n",a[i],b[i],c[i],d[i]);
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define P pair<ll,ll>
using Graph = vector<vector<int>>;
int main(){
int N; cin >> N;
int x[N],y[N],r[N],a[N],b[N],c[N],d[N];
vector<tuple<int,int,int,int>> order;
//set<P> used;
//vector<vector<P>> rowfront; vector<vector<P>> rowback;
//vector<vector<P>> columnfront; vector<vector<P>> columnback;
vector<vector<P>> column(10000); vector<vector<P>> row(10000);
rep(i,N){
cin >> x[i] >> y[i] >> r[i];
order.push_back(make_tuple(r[i],x[i],y[i],i));
//rowfront[x[i]].push_back(make_pair(y[i],y[i])); rowback[x[i]].push_back(make_pair(y[i],y[i]));
//coulmnfront[y[i]].push_back(make_pair(x[i],x[i])); columnback[y[i]].push_back(make_pair(x[i],x[i]));
column[y[i]].push_back(make_pair(x[i],x[i])); row[x[i]].push_back(make_pair(y[i],y[i]));
}
sort(order.begin(),order.end());
rep(i,N){
//s:r p:x q:y k:i
int s = get<0>(order[i]); int p = get<1>(order[i]);
int q = get<2>(order[i]); int k = get<3>(order[i]);
int fx = 0; int bx = 0; int fy = 0; int by = 0;
bool fxok = true; bool bxok = true; bool fyok = true; bool byok = true;
rep(j,10000){
if((fx+bx+3)*(fy+by+3)>s) break;
if(p-bx-1<0) bxok = false;
if(p+fx+1>9999) fxok = false;
if(q-by-1<0) byok = false;
if(q+fy+1>9999) fyok = false;
if(byok){
for(auto range:column[q-j-1]){
int ini = range.first; int fin = range.second;
if(ini>p+fx||fin<p-bx) continue;
byok = false; break;
}
if(byok) by = j+1;
}
if(fyok){
for(auto range:column[q+j+1]){
int ini = range.first; int fin = range.second;
if(ini>p+fx||fin<p-bx) continue;
fyok = false; break;
}
if(fyok) fy = j+1;
}
if(bxok){
for(auto range:row[p-j-1]){
int ini = range.first; int fin = range.second;
if(ini>q+fy||fin<q-by) continue;
bxok = false; break;
}
if(bxok) bx = j+1;
}
if(fxok){
for(auto range:row[p+j+1]){
int ini = range.first; int fin = range.second;
if(ini>q+fy||fin<q-by) continue;
fxok = false; break;
}
if(fxok) fx = j+1;
}
if(!fxok&&!bxok&&!fyok&&!byok) break;
}
column[q+fy].push_back(make_pair(p-bx,p+fx));
column[q-by].push_back(make_pair(p-bx,p+fx));
row[p+fx].push_back(make_pair(q-by,q+fy));
row[p-bx].push_back(make_pair(q-by,q+fy));
//------------------------
a[k] = p-bx; c[k] = p+fx+1;
b[k] = q-by; d[k] = q+fy+1;
}
cout << endl << endl << endl;
rep(i,N){
cout << a[i] << " " << b[i] << " "
<< c[i] << " " << d[i] << endl;
}
} |
#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;
struct dijkstra{
vector<long long> min_dst;
vector<vector<pair<long long, long long>>> G;
dijkstra(int n){
min_dst.resize(n);
G.resize(n);
}
void append(int u, int v, long long cost){G[u].push_back(make_pair(v, cost));}
void run(int s){
priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, greater<pair<long long, long long>>> que;
fill(min_dst.begin(), min_dst.end(), -1);
min_dst[s] = 0;
que.push(make_pair(0, s));
while(!que.empty()){
auto p = que.top(); que.pop();
int at = p.second;
if(min_dst[at] < p.first){continue;}
for(int i = 0; i < G[at].size(); i++){
int to = G[at][i].first;
long long cost = G[at][i].second;
if(min_dst[to] == -1 || min_dst[at] + cost < min_dst[to]){
min_dst[to] = min_dst[at] + cost;
que.push(make_pair(min_dst[to], to));
}
}
}
}
long long get(int at){return min_dst[at];}
};
int main(){
cin.tie(0); ios::sync_with_stdio(false);
ll n, m; cin >> n >> m;
dijkstra d(2 * n);
zep(i, 0, m){
ll a, b, c; cin >> a >> b >> c; a--; b--;
d.append(a, b + n, c);
d.append(a + n, b + n, c);
}
zep(i, 0, n){
d.run(i);
print(d.get(i + n))
}
return 0;
} | #include<bits/stdc++.h>
//#include<atcoder/all>
//#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
//using namespace atcoder;
//using namespace boost::multiprecision;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long ll;
typedef pair<int,int> P;
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<typename T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
const ll mod=998244353;
const ll LINF=1ll<<60;
const int INF=1<<30;
int dx[]={0,1,0,-1,0,1,-1,1,-1};
int dy[]={0,0,1,0,-1,1,-1,-1,1};
struct edge {int to;ll cost;};
const int M_N=10000;
vector<edge> G[M_N+1];
struct Dijkstra{
ll d[M_N+1];
void init(int n){
fill(d,d+n,LINF);
}
ll dijkstra(int s){
priority_queue<P,vector<P>,greater<P>> que;
que.push(P(0,s));
d[s] = 0;
ll ret = LINF;
while(!que.empty()){
P p=que.top();
que.pop();
int v=p.second;
if(d[v]<p.first) continue;
for(int i=0;i<G[v].size();i++){
edge e=G[v][i];
if(e.to == s){
chmin(ret, d[v] + e.cost);
}
if(d[e.to]>d[v]+e.cost){
d[e.to]=d[v]+e.cost;
que.push(P(d[e.to],e.to));
}
}
}
return ret;
}
};
int main(){
int n,m;cin >> n >> m;
for (int i = 0; i < m; i++) {
int a,b,c;cin >> a >> b >> c;
a--,b--;
G[a].pb({b, c});
}
Dijkstra ds;
for (int i = 0; i < n; i++) {
ds.init(n);
auto p = ds.dijkstra(i);
if(p == LINF) cout << -1 << endl;
else cout << p << endl;
}
return 0;
}
|
#include<bits/stdc++.h>
#define ll long long int
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vpii vector< pii >
#define vpll vector< pll >
#define mpii map<int,int>
#define mpll map<ll,ll>
#define MOD 1000000007
#define all(v) v.begin(),v.end()
#define s(v) v.size()
#define test ll t;cin>>t;while(t--)
#define vec vector<ll>
#define read0(v,n) for(int i=0;i<n;i++)cin>>v[i];
#define read1(v,n) for(int i=1;i<=n;i++)cin>>v[i];
#define trav(a,x) for (auto& a: x)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define cut(x) {cout<<x;return 0;}
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORB(i,a,b) for(int i=a;i>=b;i--)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define f first
#define sc second
#define lb lower_bound
#define ub upper_bound
#define nl '\n'
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define oset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
ll gcd(ll a, ll b)
{
if (b==0)return a;
return gcd(b, a % b);
}
ll lcm(ll a,ll b)
{
return (a*b)/gcd(a,b);
}
ll pow(ll a, ll b)
{
ll ans=1;
while(b)
{
if(b&1)
ans=(ans*a)%MOD;
b/=2;
a=(a*a)%MOD;
}
return ans;
}
int main()
{
fast
int n,k;
cin>>n>>k;
vec v(n);
read0(v,n);
mpll m;
trav(it,v)m[it]++;
vec temp[k];
ll prev=m[0]-1;
FOR(i,0,n-1)
{
prev=min(prev,m[i]-1);
FOR(j,0,min((ll)k-1,prev))
{
temp[j].pb(1);
}
}
ll ans=0;
FOR(i,0,k-1)
{
ans+=s(temp[i]);
}
cout<<ans<<nl;
}
| /*-------------必要なときに使うもの------------------*/
//#define _GLIBCXX_DEBUG
/*#include<atcoder/modint>
#include<atcoder/math>
using namespace atcoder;
using mint=modint1000000007;
using MINT=modint998244353;*/
/*#pragma GCC target("avx")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")*/
/*-------------------end-----------------------------*/
#include<bits/stdc++.h>
using namespace std;
/*---------------------マクロ-------------------------*/
#define int long long
#define rep(i, n) for (int i = 0; i < (long long)(n); i++)
#define stoi stoll
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define debug(x) cout<<#x<<" : "<<x<<endl
#define endl '\n'
/*------------------------end---------------------------*/
/*-----------------------型エイリアス--------------------*/
using ll = long long;
using lint=long long;
using P=pair<long long,long long>;
using vi=vector<long long>;
using vvi=vector<vi>;
using vb=vector<bool>;
using vc=vector<char>;
using vvc=vector<vc>;
using vs=vector<string>;
using vd=vector<double>;
/*------------------------end---------------------------*/
/* ----------------よく使う数字や配列-------------------- */
constexpr long long INF = LLONG_MAX/3;
constexpr long long dx[]={1,0,-1,0},dy[]={0,1,0,-1};
constexpr long long mod = 1e9 + 7;
constexpr long long MOD=998244353;
constexpr long double pi = 3.141592653589793238462643383279;
constexpr double EPS=1e-14;
/* ----------------------end-----------------------------*/
/*---------------------template------------------------*/
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;
}
template<class T>inline void mainend(T x){cout<<x<<endl;exit(0);}
/*----------------------end-------------------------*/
void Main(){
int N,K;cin>>N>>K;
map<int,int>mp;
int MAX=-INF;
rep(i,N){int a;cin>>a;chmax(MAX,a);mp[a]++;}
for(int i=0;i<=MAX;i++){
if(!mp.count(i))mp[i]=0;
}
map<int,pair<set<int>,int>>ma;
for(auto&[x,y]:mp){
ma[y].first.insert(x);
}
for(auto &[x,y]:ma){
y.second=*y.first.begin();
}
if(!ma.count(0)){
ma[0].first.insert(MAX+1);
ma[0].second=MAX+1;
}
int t_min=ma[0].second;
int sum=0;
/*for(auto[x,y]:ma){
cout<<'%'<<x<<'%'<<endl;
for(auto z:y.first){
cout<<z<<' ';
}
cout<<endl;
}*/
for(int i=0;i<K;i++){
int a;
if(!ma.count(i))a=t_min;
else a=ma[i].second;
//debug(a);
chmin(t_min,a);
sum+=t_min;
}
cout<<sum<<endl;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout<<fixed<<setprecision(16);
Main();
} |
#include <bits/stdc++.h>
#include <numeric>
#include <iomanip>
#define ll long long
#define intm INT_MAX
#define llm LLONG_MAX
using namespace std;
typedef array<ll, 2> P;
const int mod = 1e9 + 7;
//const ll mod = 1e9 + 7;
const int mxN = 2e5 + 9;
const int INF = 2e9;
/*
(x1,y1) (x,0) (x2,y2)
y1/(x-x1) = y2/(x2-x)
y1*(x2-x) = y2(x-x1)
y1x2 - y1*x = y2*x - y2x1
x = (y1x2 + y2x1)/(y1+y2)
*/
char a[2009][2009];
int dp[2009][2009];
int x[2009][2009];
int y[2009][2009];
int z[2009][2009];
void solve(){
int h,w;
cin >> h >> w;
for(int i=1; i <= h; i++){
for(int j=1; j <= w; j++){
cin >> a[i][j];
}
}
dp[1][1] = 1;
x[1][1] = 1; y[1][1] = 1; z[1][1] = 1;
for(int i=1; i <= h; i++){
for(int j=1; j <= w; j++){
if(i==1&&j==1) continue;
if(a[i][j]=='#'){
x[i][j] = 0; y[i][j] = 0; z[i][j] = 0;
continue;
}
if( 1 <= j-1 && j-1 <= w) dp[i][j] = (dp[i][j] + x[i][j-1]) % mod;
if( 1 <= i-1 && i-1 <= h) dp[i][j] = (dp[i][j] +y[i-1][j]) % mod;
if( 1 <= i-1 && i-1 <= h && 1 <= j-1 && j-1 <= w) dp[i][j] = (dp[i][j] + z[i-1][j-1]) % mod;
//update x y z
//cout << i << " " << j << " " << x[i-1][j] << " " << dp[i][j] << '\n';
x[i][j] = (x[i][j-1] + dp[i][j]) % mod;
y[i][j] = (y[i-1][j] + dp[i][j]) % mod;
z[i][j] = (z[i-1][j-1] + dp[i][j]) % mod;
}
}
cout << dp[h][w] << '\n';
/*for(int i=1; i <= h; i++){
for(int j=1; j <= w; j++){
cout << dp[i][j] << " ";
}
cout << endl;
}
cout << "-----------------\n";
for(int i=1; i <= h; i++){
for(int j=1; j <= w; j++){
cout << x[i][j] << " ";
}
cout << endl;
}*/
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
int t;
t = 1;
//cin >> t;
while(t--){
solve();
}
}
| #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <list>
#include <ctime>
#include <sstream>
#include <queue>
#include <stack>
#include <random>
#include <bitset>
#include <numeric>
#include <cassert>
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;
#define rep(x, b, e) for(int x=(b); x<(e); ++x)
#define ford(x, b, e) for(int x=((int)(b))-1; x>=(e); --x)
#define all(c) c.begin(),c.end()
#define sz(x) ((int)((x).size()))
#define pb push_back
#define st first
#define nd second
#define mp(x,y) make_pair(x,y)
typedef short int sint;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it) {
*this << ((it != d.b) ? ", " : "") << *it;
}
ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
const int mod = 1e9 + 7;
int modu(ll x) {
if (x >= mod) {
x %= mod;
}
return x;
}
int main() {
ios_base::sync_with_stdio(false);
int h, w;
cin >> h >> w;
vector<string> t(h);
rep(i, 0, h) {
cin >> t[i];
// t[i] = string(w, '.');
}
vector<vi> up(h, vi(w)), left(h, vi(w)), diag(h, vi(w));
vector<vi> f(h, vi(w));
f[0][0] = up[0][0] = left[0][0] = diag[0][0] = 1;
rep(i,0,h) rep(j,0,w) {
if (i +j == 0) {
continue;
}
if (t[i][j] == '#') {
continue;
}
f[i][j] = modu((ll)(i > 0 ? up[i-1][j] : 0) + (j > 0 ? left[i][j-1] : 0) + (i>0&&j>0 ? diag[i-1][j-1] : 0));
up[i][j] = modu((i > 0 ? up[i-1][j] : 0) + f[i][j]);
left[i][j] = modu((j > 0 ? left[i][j-1] : 0) + f[i][j]);
diag[i][j] = modu((i>0&&j>0 ? diag[i - 1][j - 1] : 0) + f[i][j]);
}
cout << f[h-1][w-1] << endl;
} |
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <vector>
using namespace std;
int main() {
int l;
scanf("%d", &l);
// Answer is C(l - 1, 11)
long long ans = 1;
for (int i = 1; i <= 11; i++) {
ans *= l - i;
ans /= i;
}
printf("%lld\n", ans);
return 0;
}
| #include <iostream>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
using namespace std;
using ll = long long;
int l;
ll dp[201][201];
ll memo(int idx, int leng);
int main()
{
memset(dp, -1, sizeof dp);
cin.tie(0);
ios::sync_with_stdio(false);
cin >> l;
cout << memo(0, 0);
}
ll memo(int idx, int leng)
{
if (idx == 11)
{
if (leng < l)
return 1;
else
return 0;
}
ll &ret = dp[idx][leng];
if (ret != -1)
return ret;
ret = 0;
for (int i = leng + 1; i < l; ++i)
ret += memo(idx + 1, i);
return ret;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for(int i = 0; i < n; i++) {
cin >> a[i];
}
int max = -1;
for(int l = 0; l < n; l++) {
int min = a[l];
for(int r = l; r < n; r++) {
if(a[r] < min) {
min = a[r];
}
if(min * (r - l + 1) > max) {
max = min * (r - l + 1);
}
}
}
cout << max;
}
|
// code by lynmisakura. wish to be accepted!
#include<bits/stdc++.h>
using namespace std;
#define rep(i,N) for(int i = 0;i < N;i++)
#define rng(i,a,b) for(int i = a;i < b;i++)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define lmd(func) auto func = [&]
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define debug(arr) cout << #arr << " = " << arr << '\n'
#define debug2(a,b) cout<<"["<<#a<<","<<#b<<"] = "<<"["<<a<<","<<b<<"]\n"
#define debug3(a,b,c) cout<<"["<<#a<<","<<#b<<","<<#c<<"] = "<<"["<<a<<","<<b<<","<<c<<"]\n"
template<class T> ostream &operator << (ostream& out, const vector<T>& arr) {
cout << "{"; for (int i = 0; i < arr.size(); i++)cout << (!i ? "" : ", ") << arr[i]; cout << "}";
return out;
}
template<class T> ostream &operator << (ostream& out, const vector<vector<T> >& arr) {
cout << "{\n"; for (auto& vec : arr)cout << " " << vec << ",\n"; cout << "}";
return out;
}
template<class S,class T> ostream &operator << (ostream& out, const pair<S,T>& p){
cout << "{" << p.first << "," << p.second << "}" << '\n';
return out;
}
template<class T> istream &operator >> (istream& in, vector<T>& arr) {
for (auto& i : arr)cin >> i; return in;
}
template<class S,class T> istream &operator >> (istream& in,pair<S,T>& p){
cin >> p.first >> p.second; return in;
}
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using pi = pair<int,int>;
using pl = pair<ll,ll>;
using vpi = vector<pi>;
using vpl = vector<pl>;
/////////////////////////////////////////////////////////////////////////////
int main(void){
cin.tie(0);
ios::sync_with_stdio(false);
//cout << fixed << setprecision(20);
int ans = INT_MAX;
int n;cin >> n;
vi a(n);
rep(i,n)cin >> a[i];
rep(mask,(1<<(n-1))){
int cand = 0;
int tmp = 0;
rep(i,n){
tmp |= a[i];
if(i < n-1 && (mask >> i & 1)){
cand ^= tmp;
tmp = 0;
}
}
cand ^= tmp;
ans = min(ans , cand);
}
cout << ans << '\n';
}
|
#include <bits/stdc++.h>
#define N 5015
#define mod 998244353
using namespace std;
typedef long long ll;
int n, m;
ll sum[N], power[N][N];
ll ksm(int a, int b) {
return power[a][b];
}
void Init() {
for (int i = 0; i <= m+2; i++) {
power[i][0] = 1;
for (int j = 1; j <= n+2; j++) {
power[i][j] = (power[i][j - 1] * i) % mod;
}
}
}
int main() {
scanf("%d%d", &n, &m);
Init();
for (int j = 0; j <= n + 5; j++) {
for (int i = 0; i < m; i++) {
sum[j] = (sum[j] + ksm(i, j)) % mod;
}
}
ll ans = 0;
for (int i = 1; i <= n; i++) {
ll res = 0;
res = (res + (m * (ans + ksm(m - 1, i - 1)) % mod) % mod) % mod;
for (int j = i - 1; j >= 1; j--) {
res = (res % mod + ((ksm(m - 1, i - j - 1) * ksm(m, j - 1)) % mod * m)) % mod;
res = ((res - (ksm(m, j - 1) * sum[i - j - 1]) % mod) % mod + mod) % mod;
}
ans = res;
}
cout << ans % mod << '\n';
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 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;
}
ll dig(ll n) {//桁数
ll res = 0;
while(n > 0) {
res ++;
n /= 10;
}
return res;
}
vector<bool> seen;
void dfs(vvll &G, ll v){
seen[v]=true;
for(auto next:G[v]){
if(seen[next]) continue;
dfs(G,next);
}
}
int main() {
ll n,m,x,ans=1;
cin>>n>>m;
vll A(n);
vll X(m+1,1);
rep(i,n){
if(i==0){
A[0]=m;
continue;
}
ll sum=0;
rep(j,m){
A[i]+=mod+A[i-1]+modpow(m,i,mod)-X[j];
A[i]%=mod;
X[j]=(m-j-1)*X[j]+modpow(m,i,mod);
X[j]%=mod;
}
}
cout<<A[n-1]%mod<<endl;
} |
#include <bits/stdc++.h>
using namespace std;
template<class t> inline t read(t &x){
char c=getchar();bool f=0;x=0;
while(!isdigit(c)) f|=c=='-',c=getchar();
while(isdigit(c)) x=(x<<1)+(x<<3)+(c^48),c=getchar();
if(f) x=-x;return x;
}
template<class t,class ...A> inline void read(t &x,A &...a){
read(x);read(a...);
}
template<class t> inline void write(t x){
if(x<0) putchar('-'),write(-x);
else{if(x>9) write(x/10);putchar('0'+x%10);}
}
#define edge pair<int,int>
#define x first
#define y second
const int N=1e5+5;
int n,m,ans[N];
vector<edge> g[N];
void dfs(int x){
for(edge e:g[x]) if(!ans[e.x]){
ans[e.x]=ans[x]==e.y?1+(ans[x]==1):e.y;
dfs(e.x);
}
}
signed main(){
read(n,m);
for(int i=1,x,y,z;i<=m;i++){
read(x,y,z);
g[x].emplace_back(y,z);
g[y].emplace_back(x,z);
}
ans[1]=1;
dfs(1);
for(int i=1;i<=n;i++) write(ans[i]),puts("");
} | #include <bits/stdc++.h>
#define ll long long
#define map unordered_map
#define set unordered_set
#define l_l pair<ll, ll>
#define P pair<ll, ll>
#define vll vector<ll>
#define mll map<ll, ll>
#define mp make_pair
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define reps(i, n) for (int i = 1, i##_len = (n); i <= i##_len; ++i)
#define rev(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define revs(i, n) for (int i = ((int)(n)); i > 0; --i)
// 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 namespace std;
const ll MOD = 1000000007LL;
const ll INF = (1LL << 60LL);
// template <class T> void plus_mod(T &a, const T &b) {a = (a + b) % MOD;}
int main() {
// std::cout << std::fixed << std::setprecision(10);
ll N;
scanf("%lld", &N);
vector<pair<ll, string>> list;
rep(i, N) {
string name;
cin >> name;
ll h;
scanf("%lld", &h);
list.emplace_back(make_pair(h, name));
}
sort(list.begin(), list.end(), greater<pair<ll, string>>());
cout << (list[1].second) << endl;
// cout << ans << endl;
}
|
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define endl '\n'
ll dp[105][2];
int main(){
ios_base::sync_with_stdio(false);
ll a, b, x, y;
cin >> a >> b >> x >> y;
if(a == b) cout << x, exit(0);
else {
if(a > b) swap(a, b), b--;
int k = abs(a-b);
cout << x + min(y, 2*x) * k << endl;
}
return 0;
} | #include <iostream>
#include <vector>
#include<math.h>
#include<algorithm>
#include <float.h>
#include <iomanip>
#include<string>
using namespace std;
int main()
{
int N, x_0, y_0, x_N, y_N;
cin >> N >> x_0 >> y_0 >> x_N >> y_N;
double R_x, R_y;
R_x = ((double)x_0 + (double)x_N) / 2;
R_y = ((double)y_0 + (double)y_N) / 2;
double x, y;
x = x_0 - R_x;
y = y_0 - R_y;
double theta,A_sin,A_cos;
const double PI = 3.14159265358979323846;
theta = 2 * PI/N;
A_cos = cos(theta);
A_sin = sin(theta);
double A_x, A_y;
A_x = A_cos * x - y * A_sin;
A_y = A_sin * x + A_cos * y;
double Ans_x, Ans_y;
Ans_x = R_x + A_x;
Ans_y = R_y + A_y;
cout << Ans_x <<" "<< Ans_y;
}
|
#include<iostream>
#include<cstring>
using namespace std;
typedef long long LL;
LL b[300];
int p = 200;
int main()
{
int n, a;
cin >> n;
memset(b, 0, sizeof b);
for(int i = 0; i < n; i++){
cin >> a; a = a % p;
b[a]++;
}
LL ans = 0;
for(int i = 0; i < 205; i++){
if(b[i] != 0 && b[i] != 1){
LL k = b[i] * (b[i] - 1) / 2;
ans += k;
}
}
cout << ans << "\n";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define mnto(x, y) x = min(x, (__typeof__(x)) y)
#define mxto(x, y) x = max(x, (__typeof__(x)) y)
#define REP(i, s, e) for (int i = s; i < e; i++)
#define RREP(i, s, e) for (int i = s; i >= e; i--)
typedef long long ll;
typedef long double ld;
#define MP make_pair
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
#define MT make_tuple
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define pb emplace_back
typedef vector<int> vi;
typedef vector<ii> vii;
#define INF 1000000005
#define LINF 1000000000000000005
#define MOD 1000000007
#define MAXN 500005
int n;
char s[MAXN], t[MAXN];
vii v;
int ans;
int main() {
scanf("%d", &n);
scanf(" %s", s);
scanf(" %s", t);
int cnt = 0;
int lo = 0;
REP (i, 0, n) {
if (s[i] == t[i]) continue;
if (s[i] == '1' && t[i] == '0') {
cnt++;
} else if (s[i] == '0' & t[i] == '1') {
cnt--;
}
if (cnt == 0) {
v.pb(lo, i);
lo = i + 1;
}
}
if (cnt != 0) {
printf("-1\n");
return 0;
}
for (auto [lo, hi] : v) {
while (s[lo] == t[lo]) lo++;
while (s[hi] == t[hi]) hi--;
if (s[lo] == '0') {
assert(t[lo] == '1');
reverse(s + lo, s + hi + 1);
reverse(t + lo, t + hi + 1);
}
//printf("%d %d\n", lo, hi);
//REP (i, lo, hi + 1) {
//printf("%c", s[i]);
//}
//printf("\n");
//REP (i, lo, hi + 1) {
//printf("%c", t[i]);
//}
//printf("\n\n");
REP (i, lo, hi + 1) {
if (s[i] == '0') ans++;
}
}
printf("%d\n", ans);
return 0;
}
|
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <chrono>
#include <queue>
#include <tuple>
#include <set>
using namespace std;
long long seed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
unsigned int rx = seed, ry = seed >> 12, rz = seed >> 24;
inline unsigned int r()
{
unsigned int t;
rx ^= rx << 16; rx ^= rx >> 5; rx ^= rx << 1;
t = rx; rx = ry; ry = rz; rz = t ^ rx ^ ry;
return t;
}
int range(int a, int b)
{
return a + r() % (b - a + 1);
}
double z()
{
return (r() + 0.5) / ((double)4294967296);
}
int N, X[202], Y[202], R[202];
struct rect{
int x1, y1, x2, y2;
}best[202], now[202], nxt[202];
int bestScore;
bool xIntersect(rect &a, rect &b)
{
return max(a.x1, b.x1) < min(a.x2, b.x2);
}
bool yIntersect(rect &a, rect &b)
{
return max(a.y1, b.y1) < min(a.y2, b.y2);
}
int score(rect *r)
{
double pt = 0;
for (int i = 0; i < N; i++){
if (r[i].x1 == r[i].x2 || r[i].y1 == r[i].y2 || r[i].x1 < 0 || r[i].y1 < 0 || r[i].x2 > 10000 || r[i].y2 > 10000){
return -1;
}
if (r[i].x1 <= X[i] && X[i] < r[i].x2 && r[i].y1 <= Y[i] && Y[i] < r[i].y2){
int area = (r[i].x2 - r[i].x1) * (r[i].y2 - r[i].y1);
double rat = 1 - 1. * min(R[i], area) / max(R[i], area);
pt += (1 - rat * rat);
}
else return -1;
}
pt /= N;
return (pt * 1e9 + 0.5);
}
void proc()
{
auto start = std::chrono::system_clock::now();
scanf ("%d", &N);
for (int i = 0; i < N; i++){
scanf ("%d %d %d", &X[i], &Y[i], &R[i]);
tie(now[i].x1, now[i].y1) = tie(now[i].x2, now[i].y2) = tie(X[i], Y[i]);
now[i].x2++; now[i].y2++;
}
const double TL = 4.85;
while (1){
double rat = std::chrono::duration<double>(std::chrono::system_clock::now() - start).count() / TL;
if (rat >= 1) break;
int step = 1e7 / N;
double reduce = pow(1e-7, 1. / step);
double temp = 1e5;
int mv = N / 4;
for (int i = 0; i < N; i++) {
int x1 = X[i], x2 = X[i] + 1, y1 = Y[i], y2 = Y[i] + 1;
x1 = range(now[i].x1, x1);
x2 = range(x2, now[i].x2);
y1 = range(now[i].y1, y1);
y2 = range(y2, now[i].y2);
now[i] = { x1, y1, x2, y2 };
}
int nowScore = score(now);
for (int att = 0; att < step; att++, temp *= reduce){
int i = range(1, N) - 1;
for (int j = 0; j < N; j++) nxt[j] = now[j];
int t = range(0, 3), m1 = range(1, range(1, mv)), m2 = range(1, m1);
if (t == 0){
nxt[i].x1 -= m1;
if (range(0, 1)) nxt[i].x2 -= m2;
}
if (t == 1){
nxt[i].x2 += m1;
if (range(0, 1)) nxt[i].x1 += m2;
}
if (t == 2){
nxt[i].y1 -= m1;
if (range(0, 1)) nxt[i].y2 -= m2;
}
if (t == 3){
nxt[i].y2 += m1;
if (range(0, 1)) nxt[i].y1 += m2;
}
for (int j = 0; j < N; j++) if (j != i){
if (xIntersect(nxt[i], nxt[j]) && yIntersect(nxt[i], nxt[j])){
if (t == 0) nxt[j].x2 = nxt[i].x1;
if (t == 1) nxt[j].x1 = nxt[i].x2;
if (t == 2) nxt[j].y2 = nxt[i].y1;
if (t == 3) nxt[j].y1 = nxt[i].y2;
}
}
int nxtScore = score(nxt);
if (nxtScore <= 0) continue;
if (nowScore <= nxtScore || exp((nxtScore - nowScore) / temp) >= z()){
nowScore = nxtScore;
for (int j = 0; j < N; j++) now[j] = nxt[j];
}
}
if (bestScore < nowScore){
bestScore = nowScore;
for (int j = 0; j < N; j++) best[j] = now[j];
}
for (int j = 0; j < N; j++) now[j] = best[j];
}
for (int i = 0; i < N; i++) printf ("%d %d %d %d\n", best[i].x1, best[i].y1, best[i].x2, best[i].y2);
}
int main()
{
proc();
} | // #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;
int main() {
int K; cin >> K;
ll N, M; cin >> N >> M;
vector<ll> A(K);
for(auto &e : A) cin >> e;
ll ub = 1LL << 60, lb = -1;
while(ub - lb > 1) {
ll mid = (ub + lb) / 2;
// abs(BN - AM) <= mid
// -mid <= BN - AM <= mid
// [Lsum, Rsum]
ll Lsum = 0, Rsum = 0;
for(int i=0; i<K; i++) {
ll L = max(0LL, (A[i] * M - mid + N - 1) / N);
ll R = (mid + A[i] * M) / N;
Lsum += L, Rsum += R;
}
if(Lsum <= M and M <= Rsum) ub = mid;
else lb = mid;
}
{
vector<ll> L(K), R(K);
for(int i=0; i<K; i++) {
ll l = max(0LL, (A[i] * M - ub + N - 1) / N);
ll r = (ub + A[i] * M) / N;
L[i] = l, R[i] = r;
}
vector<ll> ans = L;
ll sum = accumulate(ans.begin(), ans.end(), 0LL);
for(int i=0; i<K; i++) {
ll rem = min(R[i] - ans[i], M - sum);
ans[i] += rem;
sum += rem;
}
for(int i=0; i<K; i++) {
printf("%lld%c", ans[i], " \n"[i + 1 == K]);
}
}
return 0;
}
|
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstring>
#include <iostream>
#include <sstream>
#include <numeric>
#include <map>
#include <set>
#include <queue>
#include <vector>
using namespace std;
typedef long long LL;
struct Tree {
int _size;
vector<vector<int>> _edges;
vector<int> _done;
vector<int> _vis;
vector<int> _color;
vector<int> _shifts;
vector<int> _nodes;
Tree(int size) : _size(size), _edges(size), _done(size), _vis(size) { }
void build_edges(const vector<int>& a, const vector<int>& b) {
for (int i = 0; i < a.size(); ++i) {
_edges[a[i]].emplace_back(b[i]);
_edges[b[i]].emplace_back(a[i]);
}
}
void dfs1(int n) {
_done[n] = 1;
for (auto next : _edges[n]) {
if (_done[next] <= 0) {
dfs1(next);
}
}
_nodes.emplace_back(n);
}
bool dfs2(int n) {
_vis[n] = 1;
for (auto next : _edges[n]) {
if (_vis[next] <= 0) {
_color[next] = (_color[n] + _shifts[next]) % 3;
if (!dfs2(next)) {
return false;
}
}
if (_color[n] == _color[next]) {
return false;
}
}
return true;
}
LL solve(int M, std::vector<int>& A, std::vector<int>& B) {
LL ans = 1;
for (int node = 0; node < _size; ++node) {
if (_done[node]) {
continue;
}
_nodes.clear();
dfs1(node);
_nodes.pop_back();
_shifts = vector<int>(_size);
_color = vector<int>(_size, -1);
int sum = 0, bm = 1 << _nodes.size();
for (int b = 0; b < bm; ++b) {
for (int i = 0; i < _nodes.size(); ++i) {
_shifts[_nodes[i]] = ((1 << i) & b) ? 1 : 2;
}
_color[node] = 0;
_vis = vector<int>(_size);
if (dfs2(node)) {
++sum;
}
}
ans *= sum * 3;
}
return ans;
}
};
LL solve(int N, int M, std::vector<int> &A, std::vector<int> &B) {
Tree tree(N);
tree.build_edges(A, B);
return tree.solve(M, A, B);
}
int main() {
int N, M;
std::cin >> N >> M;
std::vector<int> A(M), B(M);
for (int i = 0; i < M; i++) {
std::cin >> A[i] >> B[i];
--A[i], --B[i];
}
cout << solve(N, M, A, B) << endl;
return 0;
}
| /**
* author:
* created: 16.11.2020 06:42:51
**/
#pragma region Macros
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
using P = pair<int,int>;
using ll = long long;
const int INF = 1001001001;
const int di[] = {-1,0,1,0}, dj[] = {0,-1,0,1};
typedef vector<int> vi;
typedef vector<vi> vvi;
#define rep(i,n) for(int i = 0; i < (n); i++)
#define repn(i,n) for(int i = 1; i <= (n); i++)
#define pb push_back
void debug_out() { cout << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cout << H << " ";
debug_out(T...);
}
#ifdef LOCAL
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
template<class T>
inline bool chmax(T &a, T b) {
if(a < b) {
a = b;
return true;
}
return false;
}
template<class T>
inline bool chmin(T &a, T b) {
if(a > b) {
a = b;
return true;
}
return false;
}
const double PI = acos(-1);
const int mod = 1000000007;
class mint {
long long x;
public:
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+(const mint& a) const {
mint res(*this);
return res+=a;
}
mint operator-(const mint& a) const {
mint res(*this);
return res-=a;
}
mint operator*(const mint& a) const {
mint res(*this);
return res*=a;
}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t>>1);
a *= a;
return a;
}
// for prime mod
mint inv() const {
return pow(mod-2);
}
mint& operator/=(const mint& a) {
return (*this) *= a.inv();
}
mint operator/(const mint& a) const {
mint res(*this);
return res/=a;
}
friend ostream& operator<<(ostream& os, const mint& m){
os << m.x;
return os;
}
};
#pragma endregion
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int h, w;
cin >> h >> w;
vector<string> s(h);
rep(i,h) cin >> s[i];
vector<vector<mint>> dp(h,vector<mint>(w,0));
vector<vector<mint>> x(h,vector<mint>(w,0));
vector<vector<mint>> y(h,vector<mint>(w,0));
vector<vector<mint>> z(h,vector<mint>(w,0));
dp[0][0] = 1;
rep(i,h)rep(j,w){
if(i==0 && j==0) continue;
if(s[i][j]=='#')continue;
if(j>0)x[i][j] = (x[i][j-1]+dp[i][j-1]);
if(i>0)y[i][j] = (y[i-1][j]+dp[i-1][j]);
if(i>0&&j>0)z[i][j] = (z[i-1][j-1]+dp[i-1][j-1]);
dp[i][j] = (x[i][j]+y[i][j]+z[i][j]);
}
cout << dp[h-1][w-1] << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL N=2e5+10;
const int inf=0x7fffffff;
vector<int> adj[N];
vector<int> mst;
LL n,m,k;
int fa[N],arr[N];
int findfa(int x)
{
if(fa[x]==x) return fa[x];
else return fa[x]=findfa(fa[x]);
}
void merge(int x,int y)
{
int fx=findfa(x),fy=findfa(y);
if(fx!=fy)
{
if(rand()%2)
fa[fy]=fx;
else fa[fx]=fy;
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
for(int i=1;i<=N;i++) fa[i]=i;
for(int i=1;i<=n;i++)
{
cin>>arr[i];
}
bool f=0;
for(int i=1;i<=n/2+1;i++)
{
if(arr[i]!=arr[n-i+1]) f=1;
}
if(f==0) cout<<0<<endl;
else
{
int cnt=0,aa=0;
set<pair<int,int>> st;
for(int i=1;i<=n/2;i++)
{
if(arr[i]!=arr[n-i+1]&&st.count(make_pair(arr[i],arr[n-i+1]))==0)
{
st.insert(make_pair(arr[i],arr[n-i+1]));
aa++;
if(findfa(arr[i])==findfa(arr[n-i+1])) cnt++;
merge(arr[i],arr[n-i+1]);
}
}
cout<<aa-cnt<<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 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++)
#define ALL(c) c.begin(), c.end()
#define TR(c, it) for (auto it = c.begin(); it != c.end(); it++)
#define SZ(c) int(c.size())
#define present(c, e) (c.find(e) != c.end())
#define cpresent(c, e) (find(ALL(c),e) != c.end())
#define ffasstt() ios_base::sync_with_stdio(0); cin.tie(0)
#define ll long long
#define pii(T) pair<T, T>
#define v1(T) vector<T>
#define v2(T) vector< vector<T> >
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
template<class T> void inline Scan(v1(T) &arr){ for(auto &i:arr) cin >> i; }
template<class T> void inline Print(v1(T) &arr){ for(auto &i:arr) cout << fixed << setprecision(7) << i << " "; }
const int N = 200010;
vector<int>adj[N];
v1(bool) vis(N, 0);
ll cnt = 0;
ll ans = 0;
void dfs(int s){
vis[s] = 1;
cnt++;
for(int t: adj[s]){
if(!vis[t])
dfs(t);
}
}
void solve(){
ll n;
cin >> n;
v1(int) arr(n);
Scan(arr);
REP(i, 0, n - 1){
if(arr[i] != arr[n - 1 - i]){
adj[arr[i]].PB(arr[n - 1 - i]);
adj[arr[n - 1 - i]].PB(arr[i]);
}
}
REP(i, 0, n - 1){
if(!vis[arr[i]]){
cnt = 0;
dfs(arr[i]);
ans += (cnt - 1);
}
}
cout << ans;
}
int main()
{
ffasstt();
int t = 1;
// cin >> t;
while(t--){
solve();
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC <<" secs"<<endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
typedef long long ll;
typedef pair<int, int> pi;
const int maxn = 2e5 + 5;
int n;
ll a[2 * maxn];
int main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1;i <= 2 * n;i++){
cin >> a[i];
}
multiset<ll> s;
ll ans = 0;
for(int i = n;i >= 1;i--){
s.insert(a[i]);
ans += a[i];
if(a[2 * n + 1 - i] > *s.begin()){
ans += (a[2 * n + 1 - i] - (*s.begin()));
s.erase(s.begin());
s.insert(a[2 * n + 1 - i]);
}
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
constexpr int MOD = 1e9 + 7;
using namespace std;
int main() {
int N;
cin >> N;
vector<long long> A(N + 1, 0);
for (int i = 0; i < N; i++) {
cin >> A[i];
}
sort(A.begin(), A.end());
long long ans = 1;
for (int i = 0; i < N; i++) {
ans *= A[i + 1] - A[i] + 1;
ans %= MOD;
}
cout << ans << endl;
return 0;
} |
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;
#define LETS_GET_SCHWIFTY ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ff first
#define ss second
#define int long long
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define vi vector<int>
#define pqb priority_queue<int>
#define pqs priority_queue<int,vi,greater<int> >
#define setbits(x) __builtin_popcountll(x)
#define zerobits(x) __builtin_ctzll(x)
#define mod 998244353
#define inf 1e18
#define ps(x,y) fixed<<setprecision(y)<<x
#define vpii vector<pair<int,int> >
#define all(x) x.begin(),x.end()
#define matrixprint(arr,a,b,c,d) for(int i=a;i<=c;i++){for(int j=b;j<=d;j++){cout<<arr[i][j]<<" ";}cout<<"\n";}
#define show(arr,x,y) for(int i=x;i<=y;i++){cout<<arr[i]<<" ";}cout<<"\n"
#define sz(x) (int)x.size()
#define db(x) cout<<x<<"\n";
typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
//insert,find_by_order,order_of_key,lower_bound,upper_bound;
#define TRACE
#ifdef TRACE
#define deb(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}
#else
#define deb(...)
#endif
//////////////////////////////code//////////////////////////////
const int N = 200;
bool cmp(pii p1 , pii p2)
{
return p1.ss < p2.ss;
}
void solve()
{
int n;
cin >> n;
cout << 6 << " " << 10 << " " << 15 << " ";
n -= 3;
for(int i = 16 ; i <= 10000 && n > 0 ; i++)
{
if(i % 6 == 0 || i % 10 == 0 || i % 15 == 0 )
{
cout << i << " ";
n--;
}
}
}
int32_t main()
{
LETS_GET_SCHWIFTY;
#ifndef ONLINE_JUDGE
freopen("INP.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
int t = 1;
// cin >> t;
while (t--)
solve();
}
// check out for following mistakes-
// if using pb operation on vector and then trying to access index..check if sizeof that vec could remain 0 only
// is using prime sieve make sure it fits
// when using factorial template or combinatorics make sure that you edit fillfac fun values and array values
| //@formatter:off
#include<bits/stdc++.h>
#define overload4(_1,_2,_3,_4,name,...) name
#define rep1(i,n) for (ll i = 0; i < ll(n); ++i)
#define rep2(i,s,n) for (ll i = ll(s); i < ll(n); ++i)
#define rep3(i,s,n,d) for(ll i = ll(s); i < ll(n); i+=d)
#define rep(...) overload4(__VA_ARGS__,rep3,rep2,rep1)(__VA_ARGS__)
#define rrep1(i,n) for (ll i = ll(n)-1; i >= 0; i--)
#define rrep2(i,n,t) for (ll i = ll(n)-1; i >= (ll)t; i--)
#define rrep3(i,n,t,d) for (ll i = ll(n)-1; i >= (ll)t; i-=d)
#define rrep(...) overload4(__VA_ARGS__,rrep3,rrep2,rrep1)(__VA_ARGS__)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define popcount(x) __builtin_popcount(x)
#define pb push_back
#define eb emplace_back
#ifdef __LOCAL
#define debug(...) { cout << #__VA_ARGS__; cout << ": "; print(__VA_ARGS__); cout << flush; }
#else
#define debug(...) void(0)
#endif
#define INT(...) int __VA_ARGS__;scan(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;scan(__VA_ARGS__)
#define STR(...) string __VA_ARGS__;scan(__VA_ARGS__)
#define CHR(...) char __VA_ARGS__;scan(__VA_ARGS__)
#define DBL(...) double __VA_ARGS__;scan(__VA_ARGS__)
#define LD(...) ld __VA_ARGS__;scan(__VA_ARGS__)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int,int>;
using LP = pair<ll,ll>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vd = vector<double>;
using vvd = vector<vector<double>>;
using vs = vector<string>;
using vc = vector<char>;
using vvc = vector<vector<char>>;
using vb = vector<bool>;
using vvb = vector<vector<bool>>;
using vp = vector<P>;
using vvp = vector<vector<P>>;
template<class S,class T> istream& operator>>(istream &is,pair<S,T> &p) { return is >> p.first >> p.second; }
template<class S,class T> ostream& operator<<(ostream &os,const pair<S,T> &p) { return os<<'{'<<p.first<<","<<p.second<<'}'; }
template<class T> istream& operator>>(istream &is,vector<T> &v) { for(T &t:v){is>>t;} return is; }
template<class T> ostream& operator<<(ostream &os,const vector<T> &v) { os<<'[';rep(i,v.size())os<<v[i]<<(i==int(v.size()-1)?"":","); return os<<']'; }
template<class T> void vecout(const vector<T> &v,char div='\n') { rep(i,v.size()) cout<<v[i]<<(i==int(v.size()-1)?'\n':div);}
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;}
void scan(){}
template <class Head, class... Tail> void scan(Head& head, Tail&... tail){ cin >> head; scan(tail...); }
template<class T> void print(const T& t){ cout << t << '\n'; }
template <class Head, class... Tail> void print(const Head& head, const Tail&... tail){ cout<<head<<' '; print(tail...); }
template<class... T> void fin(const T&... a) { print(a...); exit(0); }
const string yes[] = {"no","yes"};
const string Yes[] = {"No","Yes"};
const string YES[] = {"NO","YES"};
const int inf = 1001001001;
const ll linf = 1001001001001001001;
//@formatter:on
int main(){
int a,b,c,d;
cin>>a>>b>>c>>d;
cout<<b-c;
} |
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ff first
#define ss second
#define endl "\n"
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x.size())
int powmod(int a,int l, int md){a%=md; int res=1;while(l){if(l&1)res=res*a%md;l/=2;a=a*a%md;}return res;}
int binpow(int a,int l){int res=1;while(l){if(l&1)res=res*a;l/=2;a=a*a;}return res;}
int invmod(int a, int md){return powmod(a,md-2,md);}
typedef long long ll; typedef unsigned long long ull; typedef long double ld;
typedef vector<int> vi; typedef pair<int, int> ii; typedef vector< ii > vii;
#define pb push_back
int __set(int b, int i) {return b|(1LL<<i);} //set ith bit
int __unset(int b, int i) {return b&(~(1UL<<i));}
int __check(int b, int i) {return b&(1LL<<i);} //returns 0 if ith bit is 0
int mulmod(int a, int b, int md) {return (((a%md)*(b%md))%md+md)%md;}
int addmod(int a, int b, int md) {return ((a%md+b%md)%md+md)%md;}
int submod(int a, int b, int md) {return (((a%md-b%md)%md)+md)%md;}
int divmod(int a, int b, int md) {return mulmod(a, powmod(b, md-2, md), md);} //if md is prime;
const ll inf = 0xFFFFFFFFFFFFFFFL; //very large number
priority_queue<int, vector<int>, greater<int> > pq; //for min priority_queue
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
signed main(void)
{
IOS;
int n; cin>>n;
vi a(n),b(n);
for(int i = 0; i < n; i++) {
cin>>a[i];
}
for(int i = 0; i < n; i++)
cin>>b[i];
int pre[n];
pre[0]=a[0]*b[0];
int mx=a[0];
for(int i = 1; i < n; i++) {
mx=max(mx,a[i]);
pre[i]=max(pre[i-1],mx*b[i]);
}
for(int i = 0; i < n; i++)
cout<<pre[i]<<endl;
return 0;
} | #include <bits/stdc++.h>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
int max(ll a, ll b) {
if (a > b) return a;
return b;
}
int min(ll a, ll b) {
if (a < b) return a;
return b;
}
int main() {
ll N;
cin >> N;
ll min = 1000, max = 0;
for (int i = 0; i < N; i++) {
ll A;
cin >> A;
if (max < A) max = A;
}
for (int i = 0; i < N; i++) {
ll B;
cin >> B;
if (min > B) min = B;
}
ll ans = min - max + 1;
if (ans < 0) ans = 0;
cout << ans << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main(){
int n, k;
string s;
cin >> n >> k >> s;
while(k > 0){
k -= 1;
if(n % 2 == 1){
s += s;
n *= 2;
}
//cout << s << endl;
string ns = "";
for(int i=0; 2*i+1<n; i++){
if((s[2*i] == 'R' && s[2*i+1] == 'P')
|| (s[2*i] == 'P' && s[2*i+1] == 'S')
|| (s[2*i] == 'S' && s[2*i+1] == 'R')) swap(s[2*i], s[2*i+1]);
ns += s[2*i];
}
s = ns;
n = ns.length();
if(n == 1) break;
}
cout << s[0] << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 123;
const int mod = 998244353;
#define int long long
string s;
int n, k;
char w[200][200];
int d[200][200];
char lmao(char a, char b) {
if(a == b) return a;
else if(a == 'R') {
if(b == 'P') return b;
else return a;
}else if(a == 'P') {
if(b == 'S') return b;
else return a;
}else {
if(b == 'R') return b;
else return a;
}
}
main () {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
cin >> s;
for(int i = 0; i < n; i++)
w[i][0] = s[(i)%n];
for(int i = 0; i < n; i++) {
d[i][0] = i;
}
for(int j = 1; j <= 150; j++) {
for(int i = 0; i< n; i++){
int nx = d[i][j - 1];
nx++;
nx %= n;
d[i][j] = d[nx][j - 1];
}
}
for(int j = 1; j <= k; j++) {
for(int i = 0; i < n; i++) {
int nx = d[i][j - 1];
nx++;
nx %= n;
w[i][j] = lmao(w[i][j - 1], w[nx][j - 1]);
}
}
cout << w[0][k];
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[10][10],num[10];
int main()
{
ll n,k,res=0;
cin>>n>>k;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
cin>>a[i][j];
for(int i=1;i<=n;i++)
num[i]=i;
do
{
ll last,now,sum=0;
for(int i=1;i<n;i++)
{
last=num[i];
now=num[i+1];
sum=sum+a[last][now];
}
sum=sum+a[1][num[n]];
if(sum==k)
res++;
}while(next_permutation(num+2,num+1+n));
cout<<res;
return 0;
} | #include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<string>
#include<ctime>
#include<map>
#include<set>
#include<bitset>
#include<queue>
#include<deque>
#include<vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef long double ld;
inline int read(){
int x=0;
bool f=0;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-'){
f=1;
}
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
if(f){
return -x;
}
return x;
}
inline uint readu(){
uint x=0;
char ch=getchar();
while(ch<'0'||ch>'9'){
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
return x;
}
void fp(){
freopen(".in","r",stdin);
freopen(".out","w",stdout);
}
int a[105],sum[105];
ll c[105][105],dp[105][10005],A[105];
const int mod=998244353;
inline ll modd(ll x){
if(x<0){
return x+mod;
}
if(x>=mod){
return x-mod;
}
return x;
}
int main(){
//fp();
dp[0][0]=1;
int n=read(),i,j,k;
ll ans=0;
A[0]=1;
for(i=1;i<=n;i++){
A[i]=A[i-1]*i%mod;
}
for(i=1;i<=n;i++){
a[i]=read();
sum[i]=sum[i-1]+a[i];
}
if(sum[n]&1){
printf("0");
return 0;
}
for(i=1;i<=n;i++){
for(j=i;j;j--){
for(k=a[i];k<=sum[i];k++){
dp[j][k]+=dp[j-1][k-a[i]];
dp[j][k]=modd(dp[j][k]);
}
}
}
for(i=1;i<n;i++){
ans+=dp[i][sum[n]>>1]*A[i]%mod*A[n-i]%mod;
ans=modd(ans);
}
printf("%lld",ans);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define S second
#define F first
#define FAST ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define vll vector<long long int>
#define pll pair<long long int,long long int>
#define mod 1000000007
#define mod2 998244353
#define ll long long int
#define ld long double
#define pi 3.141592653589793238
#define Endl endl
#define endl "\n"
const int N = 1e5 + 5;
const ll infP = 1e18;
void solve()
{
deque<char> d;
string s;
cin >> s;
bool rev = false;
for(auto x:s)
{
if(x == 'R')
rev = !rev;
else
{
if(rev)
{
if(d.size() and d.front() == x)
d.pop_front();
else
d.push_front(x);
}
else
{
if(d.size() and d.back() == x)
d.pop_back();
else
d.push_back(x);
}
}
}
string ans;
for(auto x:d)
ans += x;
if(rev)
reverse(ans.begin(),ans.end());
cout << ans;
}
void debug(ll tt) {}
signed main()
{
FAST;
int t = 1;
// cin >> t;
while(t--)
{
solve();
}
} | #include <bits/stdc++.h>
#define rep3(i, s, n, a) for (long long i = (s); i < (long long)(n); i += a)
#define rep2(i, s, n) rep3(i, s, n, 1)
#define rep(i, n) rep2(i, 0, n)
using namespace std;
using ld = long double;
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;
using Pll = pair<ll, ll>;
int main() {
string s;
cin >> s;
vector<vector<P>> indbox(2);
int nowind = 0, start = 0;
int slen = s.size();
rep(i, slen){
if(s[i]=='R'){
indbox[nowind].emplace_back(start, i); // [start, i)
nowind ^= 1;
start = i+1;
}
}
string res = "";
int reslen = 0;
indbox[nowind].emplace_back(start, slen);
int indlen = indbox[nowind^1].size();
for(int i=indlen-1;i>=0;i--){
P now = indbox[nowind^1][i];
for(int j=now.second-1;j>=now.first;j--){
// cout << s[j];
if(reslen == 0 || res[reslen-1]!=s[j]){
res += s[j];
reslen++;
}else{
if(reslen > 0){
res.erase(reslen-1);
reslen--;
}
}
// cout << reslen << endl;
}
}
indlen = indbox[nowind].size();
rep(i, indlen){
P now = indbox[nowind][i];
rep2(j, now.first, now.second){
// cout << s[j];
if(reslen == 0 || res[reslen-1]!=s[j]){
res += s[j];
reslen++;
}else{
if(reslen > 0){
res.erase(reslen-1);
reslen--;
}
}
// cout << reslen << endl;
}
}
cout << res << endl;
return 0;
} |
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<algorithm>
#include<utility>
#include<map>
#include<tuple>
#include<deque>
using namespace std;
const int mod = 1000000007;
const int INF = 1001001001;
const long long LINF = 1001002003004005006;
const double PI = acos(-1);
int dir4row[] = { 1,0,-1,0 };
int dir4col[] = { 0,1,0,-1 };
int dir8row[] = { 1,1,0,-1,-1,-1,0,1 };
int dir8col[] = { 0,1,1,1,0,-1,-1,-1 };
/*-----------------------------------------------*/
vector<long long> a, b;
struct UnionFind {
vector<int> r;
vector<long long> sum1, sum2;
int N;
UnionFind(int n) {
r = vector<int>(n, -1);
N = n;
sum1 = vector<long long>(n);
sum2 = vector<long long>(n);
for (int i = 0; i < n; ++i) {
sum1[i] = a[i];
sum2[i] = b[i];
}
}
int root(int x) {
if (r[x] < 0) {
return x;
}
return r[x] = root(r[x]);
}
bool unite(int a, int b) {
a = root(a);
b = root(b);
if (a == b) {
return false;
}
if (r[a] > r[b]) {
swap(a, b);
}
sum1[a] += sum1[b];
sum1[b] = 0;
sum2[a] += sum2[b];
sum2[b] = 0;
r[a] += r[b];
r[b] = a;
return true;
}
bool solve() {
for (int i = 0; i < N; ++i) {
if (sum1[i] != sum2[i]) {
return false;
}
}
return true;
}
};
int main() {
int n, m; cin >> n >> m;
a = vector<long long>(n);
b = vector<long long>(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
cin >> b[i];
}
UnionFind uf(n);
for (int i = 0; i < m; i++) {
int c, d; cin >> c >> d;
c--;
d--;
uf.unite(c, d);
}
if (uf.solve()) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<vs> vvs;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
const ll mod = 1000000007;
int main() {
int N, M; cin >> N >> M;
vi As(N);
rep(i, N) cin >> As[i];
vi Bs(N);
rep(i, N) cin >> Bs[i];
vi Cs(M);
vi Ds(M);
vvi adj(N, vi(0));
rep(i, M) {
cin >> Cs[i] >> Ds[i];
Cs[i]--;
Ds[i]--;
adj[Cs[i]].push_back(Ds[i]);
adj[Ds[i]].push_back(Cs[i]);
}
// グループの合計値が前後一致していればOK
vector<bool> seen(N, false);
int groupCnt = -1;
vvi G(N, vi(0));
rep(i, N){
if(seen[i]) continue;
seen[i] = true;
groupCnt++;
G[groupCnt].push_back(i);
queue<int> q;
q.push(i);
while(!q.empty()){
int cur = q.front();
q.pop();
for(int x: adj[cur]){
if(seen[x]) continue;
q.push(x);
seen[x] = true;
G[groupCnt].push_back(x);
}
}
}
string ans = "Yes";
rep(i, groupCnt + 1){
ll beforeSum = 0;
ll afterSum = 0;
for(int x: G[i]){
beforeSum += As[x];
afterSum += Bs[x];
}
if(beforeSum != afterSum){
ans = "No";
break;
}
}
cout << ans << endl;
return 0;
} |
#include <iostream>
#include <vector>
using namespace std;
int main(){
int X;
cin >> X;
int genzai = X / 100;
//cout << "genzai " << genzai << endl;
int ans = (genzai + 1)*100 - X;
cout << ans << endl;
return 0;
} | #include<bits/stdc++.h>
#include<iostream>
#define ll long long
#define rep(i,a) for(int i=0;i<a;i++)
#define int long long
#define di(a) int a;cin>>a;
#define dia di(a)
#define dix di(x)
#define diy di(y)
#define diz di(z)
#define dib di(b)
#define din di(n)
#define db double
#define all(x) x.begin(),x.end()
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
signed main()
{
din
int x=n%100;
cout<<100-x;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using llong = long long;
using ldbl = long double;
using lpair = pair<llong, llong>;
#define ALL(x) x.begin(), x.end()
constexpr llong mod = 1e9+7;
constexpr llong inf = mod * mod;
struct point {
int h;
int w;
point() = default;
point(int h, int w):
h(h),
w(w)
{};
};
struct node {
double eval = 0;
int tile_sum = 0;
point left_up;
point right_down;
point now;
vector<int> action;
bitset<2500> visited;
node() = default;
node(int e, point n, vector<int> a, bitset<2500> v):
eval(e),
now(n),
action(a),
visited(v)
{};
bool operator<(const node &right) const {
return eval < right.eval;
}
bool operator>(const node &right) const {
return eval > right.eval;
}
bool invalid_walk(point next);
int bounding_box();
int edge_dis();
void make_next(point next, int dir);
void evaluation();
};
const array<llong, 4> nw = {0, 1, 0, -1};
const array<llong, 4> nh = {-1, 0, 1, 0};
const string ans_char = "URDL";
point start_pos;
vector<vector<int>> tile_id(50, vector<int>(50));
vector<vector<int>> area(50, vector<int>(50));
point get_next_pos(point now, int dir) {
return point(now.h + nh[dir], now.w + nw[dir]);
}
int node::bounding_box() {
return (right_down.h - left_up.h) * (right_down.w - left_up.w);
}
bool node::invalid_walk(point next) {
return next.h < 0 || next.w < 0 || next.h >= 50 || next.w >= 50 ||
visited[tile_id[next.h][next.w]];
}
void node::make_next(point next, int dir) {
now = next;
visited[tile_id[next.h][next.w]] = 1;
tile_sum += area[next.h][next.w];
action.push_back(dir);
left_up.h = min(left_up.h, next.h);
left_up.w = min(left_up.w, next.w);
right_down.h = max(right_down.h, next.h);
right_down.w = max(right_down.w, next.w);
evaluation();
}
int get_dis(point a, point b) {
// return abs(b.h - a.h) + abs(b.w - a.w);
return max(abs(b.h - a.h), abs(b.w - a.w));
}
int node::edge_dis() {
return min({now.h, now.w, 50 - now.h, 50 - now.w});
}
void node::evaluation() {
eval += pow(25 - edge_dis(), 2);
int sum = 0;
for (int i = 0; i < 4; i++) {
sum += !invalid_walk(get_next_pos(now, i));
}
eval += pow(sum, 2) * 5;
// eval += bounding_box() * 10;
}
vector<int> beam_search() {
const double simulate_time = 1700000;
clock_t s = clock(), f;
priority_queue<node, vector<node>> que;
node ret;
node start_node;
start_node.make_next(start_pos, -1);
start_node.action.pop_back();
que.push(start_node);
int max_score = 0;
while (f = clock(), f - s < simulate_time && que.size()) {
for (int i = 0; i < 1000 && que.size(); i++) {
node now_node = que.top();
que.pop();
bool walked = false;
for (int j = 0; j < 4; j++) {
point next_pos = get_next_pos(now_node.now, j);
if (now_node.invalid_walk(next_pos)) {
continue;
}
walked = true;
node next_node = now_node;
next_node.make_next(next_pos, j);
que.push(next_node);
}
if (!walked && now_node.tile_sum > max_score) {
max_score = now_node.tile_sum;
ret = now_node;
}
}
}
cerr << que.size() << endl;
while (que.size()) {
node n = que.top();
que.pop();
if (n.tile_sum > max_score) {
max_score = n.tile_sum;
ret = n;
}
}
return ret.action;
}
vector<int> solve() {
clock_t s = clock();
return beam_search();
}
int main() {
cin >> start_pos.h >> start_pos.w;
for (auto &in: tile_id) {
for (auto &inin: in) {
cin >> inin;
}
}
for (auto &in: area) {
for (auto &inin: in) {
cin >> inin;
}
}
auto ans = solve();
for (auto a: ans) {
cout << ans_char[a];
}
cout << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define vi vector<int>
#define mk make_pair
#define vll vector<long long >
#define loop(i,a,b) for(int i=a;i<=b;i++)
#define loopr(i,a,b) for(int i=b;i>=a;i--)
#define vii vector<vector<int>>
#define ll long long
void io()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
unordered_map < ll, ll > dp;
ll dfs(ll y, ll x) {
if (x == y)
return 0;
if (y == 1)
return dp[y] = abs(x - y);
if (dp.find(y) != dp.end())
return dp[y];
if (y > x) {
if (y % 2 == 0) {
return dp[y] = min(y - x, 1 + dfs( y / 2, x));
}
else
return dp[y] = min(1 + dfs(y - 1, x), 1 + dfs(y + 1, x));
}
else {
return dp[y] = abs(x - y);
}
}
int main()
{
ll x, y;
dp.clear();
cin >> x >> y;
cout << dfs(y, x) << endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
const int inf = 5e8;
int dp[20][150000];
int adj[20][20];
int n;
int no[20][3];
int dist(int u, int v){ //u -> v
return abs(no[u][0]-no[v][0])+abs(no[u][1]-no[v][1]) + max(0,no[v][2]-no[u][2]);
}
int tcp(int u, int mask){
if(mask == (1<<n) - 1)return adj[u][0];
if(dp[u][mask] != inf) return dp[u][mask];
for(int v=0;v<n;v++){
if(v==u || ( mask & (1<<v) ) )continue;
int d1 = adj[u][v] + tcp(v,mask|(1<<v));
if(d1 < dp[u][mask]){
dp[u][mask]=d1;
}
}
return dp[u][mask];
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
for(int i=0;i<20;i++){
for(int j=0;j<150000;j++){
dp[i][j]=inf;
}
}
cin>>n;
for(int i=0;i<n;i++){
for(int j=0;j<3;j++){
cin>>no[i][j];
}
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
adj[i][j]=dist(i,j);
}
}
cout<<tcp(0,1)<<'\n';
return 0;
} | //翻标签不犯规!
//数据范围如此别出心裁。。。
#include<cstdio>
#include<cstring>
#define int long long
#define noti(x,i) x&~(1<<i)
int n;
int f[262155][20];
struct node{int x,y,z;}a[20];
int abs(int x){return x>=0?x:-x;}
int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
int dist(int x,int y){
node c=a[x],d=a[y];
return
abs(d.y-c.y)+abs(d.x-c.x)+max(0,d.z-c.z);
}
signed main(){
memset(f,0x3f,sizeof(f));
scanf("%lld",&n);
f[0][0]=0;
for(int i=0;i<n;++i){
scanf("%lld%lld%lld",
&a[i].x,&a[i].y,&a[i].z);
}
a[n].x=a[0].x;
a[n].y=a[0].y;
a[n].z=a[0].z;
int siz=1<<n+1;
for(int i=0;i<siz;++i){
for(int j=0;j<=n;++j){
if(i&(1<<j)){
for(int k=0;k<=n;++k){
if(i&(1<<k)){
int w=i|1<<j|1<<k;
f[w][j]=
min(f[noti(i,j)][k]
+dist(k,j),f[w][j]);
}
}
}
}
}
printf("%lld",f[siz-1][n]);
return 0;
} |
#include<cstdio>
#include<algorithm>
#define N 300010
#define db double
#define ll long long
#define fo(x,a,b) for(int x=(a);x<=(b);x++)
#define fd(x,a,b) for(int x=(a);x>=(b);x--)
using namespace std;
struct node{ll x; int id;}a[N];
int n,t[4],tot1=0,tot2=0,tot3=0,tot4=0;
ll b[N],c[N],d[N],e[N];
ll ans=(1ll<<50),ans1=(1ll<<50),ans2=(1ll<<50);
inline ll read() {
ll x=0;int f=0;char c=getchar();
while (c<'0'||c>'9') f=(c=='-')?1:f,c=getchar();
while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
return f?-x:x;
}
int gc() {
char c=getchar(); while (c!='R'&&c!='G'&&c!='B') c=getchar();
if (c=='R') return 1; else if (c=='G') return 2; else return 3;
}
inline bool cmp(node x,node y) {return x.x<y.x;}
void calc1() {
ans1=ans2=(1ll<<50);
int pos=-1;
fo(i,1,tot1) {
int l=1,r=tot2,mid;
ll res=(1ll<<50);
while (l<=r) {
mid=(l+r)>>1; res=min(res,abs(c[mid]-b[i]));
if (c[mid]>=b[i]) r=mid-1; else l=mid+1;
}
if (res<ans1) ans1=res,pos=i;
}
fo(i,1,tot1) {
int l=1,r=tot3,mid;
ll res=(1ll<<50);
while (l<=r) {
mid=(l+r)>>1; res=min(res,abs(d[mid]-b[i]));
if (d[mid]>=b[i]) r=mid-1; else l=mid+1;
}
if (res<ans2&&i!=pos) ans2=res;
}
ans=min(ans,ans1+ans2);
}
void calc2() {
ans1=ans2=(1ll<<50);
int pos=-1;
fo(i,1,tot1) {
int l=1,r=tot3,mid;
ll res=(1ll<<50);
while (l<=r) {
mid=(l+r)>>1; res=min(res,abs(d[mid]-b[i]));
if (d[mid]>=b[i]) r=mid-1; else l=mid+1;
}
if (res<ans2) ans2=res,pos=i;
}
fo(i,1,tot1) {
int l=1,r=tot2,mid;
ll res=(1ll<<50);
while (l<=r) {
mid=(l+r)>>1; res=min(res,abs(c[mid]-b[i]));
if (c[mid]>=b[i]) r=mid-1; else l=mid+1;
}
if (res<ans1&&i!=pos) ans1=res;
}
ans=min(ans,ans1+ans2);
}
int main() {
n=read();n=2*n;
fo(i,1,n) {
a[i].x=read(),a[i].id=gc(),t[a[i].id]++;
if (a[i].id==1) b[++tot1]=a[i].x;
else if (a[i].id==2) c[++tot2]=a[i].x;
else d[++tot3]=a[i].x;
}
sort(a+1,a+n+1,cmp);
if (t[1]%2==0&&t[2]%2==0&&t[3]%2==0)
return 0&printf("0\n");
sort(b+1,b+tot1+1);
sort(c+1,c+tot2+1);
sort(d+1,d+tot3+1);
if (tot2%2==0) {
swap(b,c),swap(tot1,tot2);
}
else if (tot3%2==0) {
swap(b,d),swap(tot1,tot3);
}
fo(i,1,tot2) {
int l=1,r=tot3,mid;
ll res=(1ll<<50);
while (l<=r) {
mid=(l+r)>>1; res=min(res,abs(d[mid]-c[i]));
if (d[mid]>=c[i]) r=mid-1; else l=mid+1;
}
ans=min(ans,res);
}
calc1();
calc2();
printf("%lld\n",ans);
return 0;
}
| #include <iostream>
#include <vector>
#include <math.h>
#include <algorithm>
using namespace std;
// Aの中でvalに最も近い値を二分探索で探し、差の絶対値を返す
long long findClosestVal(vector<long long> *A, long long val)
{
long long lb, ub;
lb = 0;
ub = A->size() - 1;
while (lb + 1 < ub)
{
long long mid = (lb + ub) / 2;
if ((*A)[mid] <= val)
{
lb = mid;
}
else
{
ub = mid;
}
}
return min(abs(val - (*A)[lb]), abs(val - (*A)[ub]));
}
int main()
{
int N;
cin >> N;
vector<long long> R, G, B;
for (int i = 0; i < 2 * N; i++)
{
long long a;
string c;
cin >> a >> c;
if (c == "R")
R.push_back(a);
if (c == "G")
G.push_back(a);
if (c == "B")
B.push_back(a);
}
if (R.size() % 2 == 0 && G.size() % 2 == 0 && B.size() % 2 == 0)
{
cout << 0 << endl;
return 0;
}
vector<long long> *pO1, *pO2, *pE;
if (R.size() % 2 == 0)
{
pE = &R;
pO1 = &G;
pO2 = &B;
}
else if (G.size() % 2 == 0)
{
pE = &G;
pO1 = &B;
pO2 = &R;
}
else if (B.size() % 2 == 0)
{
pE = &B;
pO1 = &R;
pO2 = &G;
}
sort(pO1->begin(), pO1->end());
sort(pO2->begin(), pO2->end());
sort(pE->begin(), pE->end());
long long ans = 1e15;
for (vector<long long>::iterator iter = pO1->begin(); iter < pO1->end(); iter++)
{
long long tmp = findClosestVal(pO2, *iter);
ans = min(ans, tmp);
}
// cout << "ans " << ans << endl;
long long val1, val2;
val1 = 1e15;
val2 = 1e15;
for (vector<long long>::iterator iter = pE->begin(); iter < pE->end(); iter++)
{
long long tmp1 = findClosestVal(pO1, *iter);
long long tmp2 = findClosestVal(pO2, *iter);
val1 = min(val1, tmp1);
val2 = min(val2, tmp2);
}
ans = min(ans, val1 + val2);
cout << ans << endl;
} |
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll read()
{
char c;
ll w=1;
while((c=getchar())>'9'||c<'0')if(c=='-')w=-1;
ll ans=c-'0';
while((c=getchar())>='0'&&c<='9')ans=(ans<<1)+(ans<<3)+c-'0';
return ans*w;
}
struct node
{
ll a,b;
bool operator<(const node&w)const
{
return a+a+b<w.a+w.a+w.b;
}
}e[1000005];
int n;
int main(){
n=read();
ll now=0;
for(int i=1;i<=n;i++)
{
e[i].a=read();
now-=e[i].a;
e[i].b=read();
}
sort(e+1,e+n+1);
for(int i=n;i>=1;i--)
{
if(now>0)
{
cout<<n-i<<endl;
return 0;
}
now+=e[i].a;
now+=e[i].a;
now+=e[i].b;
}
if(now>0)
{
cout<<n<<endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main () {
int N;
cin >> N;
int temp = N;
vector<int> vec;
for(int i = 0; i < 10; i++) {
vec.push_back(temp % 10);
temp = temp / 10;
if(temp == 0) {
break;
}
}
int count = 0;
for(int i = 0; i < vec.size(); i++) {
if(vec.at(i) != 0) {
count = i;
break;
}
}
if (count != 0){
for(int i = 0; i < count; i++) {
vec.erase(vec.begin());
}
}
bool ret;
if(vec.size() < 2) {
ret = true;
}
else {
int l = vec.size();
l = l / 2;
for(int i = 0; i < l; i++) {
int f = vec[i];
int b = vec[vec.size() - 1 - i];
if(f == b) {
ret = true;
}
else {
ret = false;
break;
}
}
}
if(ret) {
cout << "Yes" << endl;
} else {
cout << "No" <<endl;
}
return 0;
} |
#include <bits/stdc++.h>
#define REP(i,n) for (int i = 0; i < (n); ++i)
template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b;return true;}return false;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b;return true;}return false;}
using namespace std;
using ll = long long;
using P = pair<int,int>;
using Pl = pair<long long,long long>;
using veci = vector<int>;
using vecl = vector<long long>;
using vecveci = vector<vector<int>>;
using vecvecl = vector<vector<long long>>;
const int MOD = 1000000007;
const double pi = acos(-1);
ll gcd(ll a, ll b) {if(b == 0) return a; else return gcd(b,a%b);}
ll lcm(ll a, ll b) {return a*b/gcd(a,b);}
template<int MOD> struct Fp {
long long val;
constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
if (val < 0) val += MOD;
}
constexpr int getmod() { return MOD; }
constexpr Fp operator - () const noexcept {
return val ? MOD - val : 0;
}
constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
constexpr Fp& operator += (const Fp& r) noexcept {
val += r.val;
if (val >= MOD) val -= MOD;
return *this;
}
constexpr Fp& operator -= (const Fp& r) noexcept {
val -= r.val;
if (val < 0) val += MOD;
return *this;
}
constexpr Fp& operator *= (const Fp& r) noexcept {
val = val * r.val % MOD;
return *this;
}
constexpr Fp& operator /= (const Fp& r) noexcept {
long long a = r.val, b = MOD, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b; swap(a, b);
u -= t * v; swap(u, v);
}
val = val * u % MOD;
if (val < 0) val += MOD;
return *this;
}
constexpr bool operator == (const Fp& r) const noexcept {
return this->val == r.val;
}
constexpr bool operator != (const Fp& r) const noexcept {
return this->val != r.val;
}
friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept {
return os << x.val;
}
friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
if (n == 0) return 1;
auto t = modpow(a, n / 2);
t = t * t;
if (n & 1) t = t * a;
return t;
}
};
using mint = Fp<MOD>;
int main() {
int N; cin >> N;
veci x(N),y(N);
REP(i,N) cin >> x[i] >> y[i];
int ans = 0;
for(int i = 0; i < N-1; i++) {
for(int j = i+1; j < N; j++) {
if(abs(x[i]-x[j]) >= abs(y[i]-y[j])) ans++;
}
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
using ll=long long;
int main(){
ll n,x=0;
cin>>n;
vector<ll>a(n);
vector<ll>b(n);
vector<ll>p(n);
for(ll i=0;i<n;i++){
cin>>a[i]>>b[i];
x+=a[i];
p[i]=2*a[i]+b[i];
}
sort(p.rbegin(),p.rend());
ll i=0;
while(x>=0){
x-=p[i];
i++;
}
cout<<i<<endl;
}
|
#include <bits/stdc++.h>
#define LL long long
#define ULL unsigned long long
#define pb push_back
#define st first
#define nd second
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
template <class T> T read(T &a) {
a=0;char x=getchar();bool f=0;
for(;x<'0'||x>'9';x=getchar())f|=x=='-';
for(;x>='0'&&x<='9';x=getchar())a=(a<<3)+(a<<1)+x-'0';
if(f)a=-a;
return a;
}
using namespace std;
bool vis[10005];
int main() {
int n;
read(n);
for (int i = 1; i <= 10000; ++i) {
if (i % 6 == 0 || i % 15 == 0 || i % 10 == 0) vis[i] = 1;
}
vis[6] = vis[10] = vis[15] = 0;
printf("6 10 15 ");
n -= 3;
for (int i = 1; n; i++) {
if (vis[i]) printf("%d ", i), n--;
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
int main()
{
int k;
cin>>k;
int count = 0;
for( long long i = 1; i <= k; i++ )
{
for( long long j = 1; j <= k/i; j++ )
{
for( long long m = 1; m <= k/(i*j); m++ )
{
if( i*j*m <= k )
count++;
}
}
}
cout<<count;
} |
#include <iostream>
#include <sstream>
#include <cmath>
#include <functional>
#include <vector>
#include <map>
#include <queue>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<int, int>;
int main() {
int a, b, w;
cin >> a >> b >> w;
w *= 1000;
int mx = -1;
int mn = 1001001001;
for (int i = 1; i <=1e6; ++i) {
double p = (double)w / i;
if (a <= p && p <= b) {
mx = max(mx, i);
mn = min(mn, i);
}
}
if (mx == -1 && mn == 1001001001) { cout << "UNSATISFIABLE" << endl; }
else { cout << mn << " " << mx << 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;
#define indexed_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
//find_by_order = returns pointer to element at that pos
//order_of_key = returns pos of the given element
#define indexed_multiset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
#define all(x) (x).begin(),(x).end()
#define mem1(a) memset(a,-1,sizeof(a))
#define mem0(a) memset(a,0,sizeof(a))
#define int long long
#define endl "\n"
typedef vector<int> vi;
typedef pair<int,int> pi;
#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++)
int MOD = 1e9 + 7;
#define boost
#define debug1(x) cout<<#x<<" "<<x<<endl;
#define debug2(x,y) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<" "<<#z<<" "<<z<<endl;
template<typename T1,typename T2>istream& operator>>(istream& in,pair<T1,T2> &a){in>>a.F>>a.S;return in;}
template<typename T1,typename T2>ostream& operator<<(ostream& out,pair<T1,T2> a){out<<a.F<<" "<<a.S;return out;}
template<class _T>inline void read(_T &_a)
{
bool f=0; char _c=getchar(); _a=0;
while(_c<'0'||_c>'9'){ if(_c=='-') f=1; _c=getchar(); }
while(_c>='0'&&_c<='9'){ _a=(_a<<3)+(_a<<1)-'0'+_c; _c=getchar(); }
if(f) _a=-_a;
}
const long long INF=1e18;
int T;
//modular expoentiation
int modularExponentiation(int x,int n,int M)
{
if(n==0)
return 1;
else if(n%2 == 0) //n is even
return modularExponentiation((x*x)%M,n/2,M);
else //n is odd
return ((x%M)*((modularExponentiation((x*x)%M,(n-1)/2,M))%M))%M;
}
int32_t main()
{
/* ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL); */
int a,b,c;
cin>>a>>b>>c;
a=a%10;
int d=0;
int temp= a;
for(int i=1;i<100;i++)
{
temp *=a;
if((temp)%10==a)
{
d=i;
break;
}
}
/* debug1(d); */
int ans = modularExponentiation(b,c,d);
if(ans==0)
ans=d;
int op =a;
for(int i=0;i<ans-1;i++)
op=op*a;
cout<<op%10<<endl;
// you should actually read the stuff at the bottom
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
*/
return 0;
} |
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define chmin(x,y) x = min((x),(y));
#define chmax(x,y) x = max((x),(y));
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
const int INF = 1e9;
const long long LINF = 1e17;
const int MOD = 1000000007;
//const int MOD = 998244353;
const double PI = 3.14159265358979323846;
char maze[20][20];
int main(){
int h,w;
cin >> h >> w;
rep(i,h)rep(j,w) cin >> maze[i][j];
int ans = 0;
rep(i,h)rep(j,w){
if(maze[i][j] == '#') ans += 4;
}
rep(i,h-1)rep(j,w-1){
int cnt = 0;
if(maze[i][j] == '#') ++cnt;
if(maze[i+1][j] == '#') ++cnt;
if(maze[i][j+1] == '#') ++cnt;
if(maze[i+1][j+1] == '#') ++cnt;
if(cnt == 2) ans -= cnt;
if(cnt == 4) ans -= cnt;
if(cnt == 3) ans -= cnt-1;
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(i64 i=0; i<n; i++)
#define all(v) v.begin(),v.end()
#define pp pair<int,int>
using Graph=vector<vector<int>>;
using i64=int64_t;
int main() {
int si,sj;cin>>si>>sj;
vector<vector<int>> t(50,vector<int>(50));
rep(i,50) {
rep(j,50) {
cin>>t[i][j];
}
}
vector<vector<int>> p(50,vector<int>(50));
rep(i,50) {
rep(j,50) {
cin>>p[i][j];
}
}
vector<char>ans;
if(si<=48) {
if(t[si][sj]!=t[si+1][sj]) {
ans.push_back('D');
} else {
ans.push_back('U');
}
} else {
if(sj<=48) {
if(t[si][sj]!=t[si][sj+1]) {
ans.push_back('R');
} else {
ans.push_back('L');
}
} else {
if(t[si][sj]!=t[si][sj-1]) {
ans.push_back('L');
} else {
ans.push_back('U');
}
}
}
rep(i,ans.size()) {
cout << ans[i];
}
} |
#include <iostream>
using namespace std;
int main(void){
// Your code here!
int N;
cin >> N;
cout << (N - 1) / 100 + 1 << endl;
}
|
/* CODER::==>ARYAN KANSAGARA
CF HANDLE:=>Good_Devil
-----------------------------------------------*/
#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define endl "\n"
int32_t main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
freopen("error.txt", "w", stderr);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int n;
cin >> n;
int sum = 0;
int ans;
for (int i = 1; i <= n; ++i)
{
sum += i;
if (sum >= n)
{
ans = i;
break;
}
}
cout << ans;
return 0;
}
|
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <vector>
#include <bitset>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <utility>
#include <algorithm>
#include <random>
#include <cmath>
#include <cassert>
#include <climits>
#include <ctime>
#include <chrono>
/*
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,avx2,tune=native")
*/
#ifdef LOCAL
#define dbg(x) cout << #x << " : " << x << endl;
#else
#define dbg(x)
#endif
#define int long long
#define pb push_back
#define ppb pop_back()
#define mp make_pair
#define fi(a, b) for (int i = a; i < b; i++)
#define fj(a, b) for (int j = a; j < b; j++)
#define fk(a, b) for (int k = a; k < b; k++)
#define fi1(a, b) for (int i = a - 1; i >= b; i--)
#define fj1(a, b) for (int j = a - 1; j >= b; j--)
#define fk1(a, b) for (int k = a - 1; k >= b; k--)
#define fx(x, a) for (auto& x : a)
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define rep1(i, a, b) for (int i = a - 1; i >= b; --i)
#define siz(x) (int)x.size()
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
using namespace std;
template<typename T1, typename T2>inline void mine(T1 &x, const T2 &y) { if (y < x) x = y; }
template<typename T1, typename T2>inline void maxe(T1 &x, const T2 &y) { if (x < y) x = y; }
ostream& operator << (ostream &out, const vector<int> &b) {
for (auto k : b) out << k << ' ';
return out;
}
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef char ch;
typedef string str;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
typedef vector<ch> vch;
typedef vector<vch> vvch;
typedef vector<str> vs;
int MOD = 1000000007;
const int INF = 1000000050;
const long long BIG = (long long)2e18 + 50;
const int MX = 200010;
const double EPS = 1e-9;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll add(ll a, ll b) {
a += b;
if (a >= MOD) return a - MOD;
return a;
}
ll sub(ll a, ll b) {
a -= b;
if (a < 0) return a + MOD;
return a;
}
ll mult(ll a, ll b) {
return a * b % MOD;
}
ll powmod(ll a, ll p) {
if (p == 0) return 1;
if (p & 1) return mult(a, powmod(a, p - 1));
return powmod(mult(a, a), p / 2);
}
ll rev(ll a) {
return powmod(a, MOD - 2);
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
int MOD1;
cin >> n >> MOD1;
MOD = MOD1 * MOD1;
int pw2 = powmod(10, n);
int pw1 = pw2 % MOD1;
for (int r = 0; r < MOD1; ++r) {
if (mult(r, MOD1) == sub(pw2, pw1)) {
cout << r << '\n';
}
}
}
| #include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define mkp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define sz(v) (int)v.size()
typedef long long LL;
typedef double db;
template<class T>bool ckmax(T&x,T y){return x<y?x=y,1:0;}
template<class T>bool ckmin(T&x,T y){return x>y?x=y,1:0;}
#define rep(i,x,y) for(int i=x,i##end=y;i<=i##end;++i)
#define per(i,x,y) for(int i=x,i##end=y;i>=i##end;--i)
inline int read(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=0;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f?x:-x;
}
const int N=200005;
const int inf=0x3f3f3f3f;
int n,k,f[N],g[N];
vector<int>e[N];
int num;
void dfs(int u,int ft,int o){
f[u]=-inf,g[u]=inf;
for(int v:e[u]){
if(v==ft)continue;
dfs(v,u,o);
ckmax(f[u],f[v]+1);
ckmin(g[u],g[v]+1);
}
if(f[u]+g[u]<=o)f[u]=-inf;
if(g[u]>o)ckmax(f[u],0);
if(f[u]==o)++num,f[u]=-inf,g[u]=0;
}
bool check(int mid){
num=0;
dfs(1,0,mid);
if(f[1]>=0)++num;
return num<=k;
}
signed main(){
cin>>n>>k;
rep(i,2,n){
int x=read(),y=read();
e[x].pb(y),e[y].pb(x);
}
int l=0,r=n,res=n;
while(l<=r){
int mid=(l+r)>>1;
if(check(mid))res=mid,r=mid-1;
else l=mid+1;
}
cout<<res<<'\n';
} |
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int T;
long long n;
int main(){
for(cin>>T;T;T--){
cin>>n;
int cnt=0;
while(n%2ll==0)cnt++,n>>=1ll;
if(cnt==0)puts("Odd");
else if(cnt==1)puts("Same");
else puts("Even");
}
} | #include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define MOD99 998244353
int main(){
int n;
cin >> n;
long long a,man = 0,che = 0,all = 0;
double euc;
for(int i = 0;i < n;i++){
cin >> a;
a = abs(a);
man += a;
all += a * a;
che = max(che,a);
}
euc = sqrt(all);
cout << fixed << setprecision(15);
cout << man << endl;
cout << euc << endl;
cout << che << endl;
} |
#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a,i##end=b;i<=i##end;i++)
#define drep(i,a,b) for(int i=a,i##end=b;i>=i##end;i--)
inline int read() {
int x=0,f=1;char c=getchar();
while(c<48||c>57){if(c=='-')f=-1;c=getchar();}
while(c>=48&&c<=57)x=(x<<1)+(x<<3)+(c^48),c=getchar();
return x*f;
}
using namespace std;
const int M=1e5+5;
typedef long long ll;
int n,m;
struct node {
int t,c,d;
};
vector <node> E[M];
struct PR {
int x;ll d;
bool operator <(const PR &a)const {return d>a.d;}
};
priority_queue <PR> q;
ll dis[M];
bool vis[M];
int main() {
n=read(),m=read();
rep(i,1,m){
int u=read(),v=read(),c=read(),d=read();
E[u].push_back((node){v,c,d});
E[v].push_back((node){u,c,d});
}
rep(i,1,n)dis[i]=2e18;
dis[1]=0;
q.push((PR){1,0});
while(!q.empty()){
int top=q.top().x;q.pop();
if(vis[top])continue;
// printf("top=%d,E=%d\n",top,E[top].size());
vis[top]=1;
// puts("A");
rep(i,0,E[top].size()-1){
int to=E[top][i].t,c=E[top][i].c,d=E[top][i].d;
// printf("to=%d\n",to);
ll t1=dis[top]+c+d/(dis[top]+1);
ll x=sqrt(d);
for(ll j=max(dis[top],x-600);j<=x+600;j++)t1=min(t1,0ll+j+c+d/(j+1));
// printf("to=%d\n",to);
if(dis[to]>t1)q.push((PR){to,dis[to]=t1});
}
// puts("B");
}
// puts("A");
if(dis[n]==2e18)puts("-1");
else printf("%lld\n",dis[n]);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define MS(a, b) memset(a, b, sizeof(a))
#define REP(a, b, c) for (register int a = b, _n = c; a <= _n; ++a)
#define DREP(a, b, c) for (register int a = b, _n = c; a >= _n; --a)
#define FOR(a, b, c) for (register int a = b, _n = c; a < _n; ++a)
#define EREP(a, b) for (register int a = head[b]; a; a = edge[a].nxt)
inline int rd() {
int x = 0; char c = getchar(); bool f = 0;
while (c < '0' && c != '-') c = getchar();
if (c == '-') f = 1, c = getchar();
while (c >= '0') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
return f ? -x : x;
}
const int SIZE = 2e5 + 100;
int n;
int head[SIZE], ecnt;
struct Edge {
int v, nxt;
} edge[SIZE << 1];
void Add_Edge (int u, int v) {
edge[++ecnt] = (Edge) { v, head[u] };
head[u] = ecnt;
}
int dep[SIZE], F[SIZE];
int mx[SIZE][2];
int Len = -1, A, B;
void dfs (int u, int fa) {
F[u] = fa;
dep[u] = dep[fa] + 1;
mx[u][0] = mx[u][1] = u;
EREP (i, u) {
int v = edge[i].v;
if (v == fa) continue;
dfs(v, u);
if (dep[mx[v][0]] > dep[mx[u][0]])
mx[u][1] = mx[u][0], mx[u][0] = mx[v][0];
else if (dep[mx[v][0]] > dep[mx[u][1]])
mx[u][1] = mx[v][0];
}
if (dep[mx[u][0]] + dep[mx[u][1]] - 2 * dep[u] > Len) {
Len = dep[mx[u][0]] + dep[mx[u][1]] - 2 * dep[u];
A = mx[u][0];
B = mx[u][1];
}
}
bool mark[SIZE];
int E;
int ans[SIZE];
void D (int u, int fa) {
++E;
ans[u] = E;
EREP (i, u) {
int v = edge[i].v;
if (v == fa) continue;
if (!mark[v]) D(v, u);
}
EREP (i, u) {
int v = edge[i].v;
if (v == fa) continue;
if (mark[v]) D(v, u);
}
++E;
}
void _main () {
n = rd();
FOR (i, 1, n) {
int u = rd(), v = rd();
Add_Edge(u, v);
Add_Edge(v, u);
}
dfs(1, 0);
int T = A;
while (A != B) {
if (dep[A] < dep[B]) swap(A, B);
mark[A] = 1;
A = F[A];
} mark[A] = 1;
D(T, 0);
REP (i, 1, n) printf ("%d ", ans[i]);
}
int main () {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif
_main();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
using P = pair<int, int>;
using PL = pair<lint, lint>;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
#define ALL(a) (a).begin(),(a).end()
constexpr int MOD = 1000000007;
constexpr lint B1 = 1532834020;
constexpr lint M1 = 2147482409;
constexpr lint B2 = 1388622299;
constexpr lint M2 = 2147478017;
constexpr int INF = 2147483647;
void yes(bool expr) {cout << (expr ? "Yes" : "No") << "\n";}
template<class T>void chmax(T &a, const T &b) { if (a<b) a=b; }
template<class T>void chmin(T &a, const T &b) { if (b<a) a=b; }
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
lint A, B;
cin >> A >> B;
lint N = B - A + 1;
vector<lint> X(N);
vector<lint> prime = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71};
vector<vector<bool>> isprime(N, vector<bool>(20));
REP(i, N) REP(j, 20) if((A+i)%prime[j] == 0) X[i] += (1<<j);
vector<vector<lint>> dp(N+1, vector<lint>(1<<20));
dp[0][0] = 1;
REP(i, N) REP(bit, 1<<20) {
dp[i+1][bit] += dp[i][bit];
if((bit&X[i]) == X[i]) {
dp[i+1][bit] += dp[i][bit^X[i]];
}
}
lint ans = 0;
REP(bit, 1<<20) {
ans += dp[N][bit];
//if(dp[N][bit] > 0) cout << bit << " " << dp[N][bit] << endl;
}
cout << ans << endl;
} | /*
/\ In The Name Of Allah /\
Author : Jawahiir Nabhan
*/
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
const char nl = '\n';
int main()
{
ll N,K; cin>> N >> K;
for(int i = 0;i < K;i++){
if(N % 200 == 0) N /= 200;
else N = N*1000 + 200;
}
cout<< N << nl;
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+5;
int n,m;
ll d;
pair<ll,ll> exgcd(ll a,ll b){
if(!b){
d=a;
return {1,0};
}
pair<ll,ll> ret,ans=exgcd(b,a%b);
ret.first=ans.second;
ret.second=ans.first-(a/b)*ans.second;
return ret;
}
int main(){
ll i,j;
int T;cin>>T;
while(T--){
ll x,y,p,q;cin>>x>>y>>p>>q;
pair<ll,ll> ans=exgcd(x+y+x+y,p+q);
ll answ=9e18;
for(i=x+y+1;i<=x+y+y;i++){
for(j=1;j<=q;j++){
if((i-j)%d) continue;
ll a=ans.first*((i-j)/d);
ll mda=(p+q)/d,mdb=(x+y)*2/d;
ll ta=(a%mda+mda)%mda;
if(ta==0) ta+=mda;
ll b=ans.second*(i-j)/d;
ll tb=(b%mdb+mdb)%mdb;
if(tb>=0) tb-=mdb;tb=-tb;
answ=min(answ,max(ta*(x+y)*2-i,tb*(p+q)-j));
}
}
if(answ==9e18) cout<<"infinity"<<endl;
else cout<<answ<<endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define fr(i,n) for(int i = 0; i<n; i++)
#define sz(v) (int)(v.size())
#define prin(a) cout << #a << " = " << a << endl
#define prinv(v) cout << #v << " = "; for(auto it : v) cout << it << ", "; cout << endl
#define all(v) (v).begin(),(v).end()
typedef long long ll;
#define rmin(a,b) a = min<ll>(a,b)
#define rmax(a,b) a = max<ll>(a,b)
#define fi first
#define se second
ll div(ll a, ll b, bool ceil){
ll ans = abs(a/b);
bool pos = (a<0)==(b<0);
if(a%b and ceil==pos) ans++;
if(!pos) ans*=-1;
return ans;
}
ll gcd_ext(ll a, ll b, ll &xo, ll &yo){
if(b==0){
xo = 1, yo = 0;
return a;
}
ll x1, y1;
ll g = gcd_ext(b,a%b,x1,y1);
xo = y1;
yo = x1-(a/b)*y1;
return g;
}
/*
Retorna qual o menor x positivo que satisfaz
a*x + b*y = c (obviamente o y correspondente eh negativo)
(ou -1 se nao existe)
Util em CRT para achar menor r positivo que
r = ra (mod a)
r = rb (mod b)
->
a*x-b*y = rb-ra
r = a*x + ra
*/
ll qual_sol(ll a, ll b, ll c){
ll xo, yo;
ll g = gcd_ext(a,b,xo,yo);
if(c%g!=0) return -1;
c/=g, a/=g,b/=g;
xo*=c,yo*=c;
ll k = div(-xo,b,b>0);
return xo+k*b;
}
ll mins(ll ra, ll a, ll rb, ll b){
ll minx = qual_sol(a,-b,rb-ra);
if(minx==-1) return minx;
return a*minx+ra;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0);
ll t; cin >> t;
fr(tt,t){
ll X, Y, P, Q; cin >> X >> Y >> P >> Q;
ll t1 = P+Q, t2 = 2*X+2*Y;
ll ans = LLONG_MAX;
fr(i,Q) fr(j,Y){
ll cand = mins(P+i,t1,X+j,t2);
if(cand!=-1) rmin(ans,cand);
}
//prin(t1);
//prin(t2);
if(ans<LLONG_MAX){
cout << ans << "\n";
} else{
cout << "infinity\n";
}
}
} |
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <numeric>
#include <utility>
#include <tuple>
#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
using namespace std;
using ll = long long int;
using P = pair<ll, ll>;
// clang-format off
#ifdef _DEBUG_
#define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; PPPPP(__VA_ARGS__); cerr << endl; } while(false)
template<typename T> void PPPPP(T t) { cerr << t; }
template<typename T, typename... S> void PPPPP(T t, S... s) { cerr << t << ", "; PPPPP(s...); }
#else
#define dump(...) do{ } while(false)
#endif
template<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }
template<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); }
template<typename T> bool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false; }
template<typename T> bool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false; }
template<typename T> void print(T a) { cout << a << '\n'; }
template<typename T, typename... Ts> void print(T a, Ts... ts) { cout << a << ' '; print(ts...); }
template<typename T> istream &operator,(istream &in, T &t) { return in >> t; }
// clang-format on
P convert(ll n, ll m) {
return {n / m, n % m};
}
P multiple(P p1, P p2, ll m) {
ll bd = p1.second * p2.second;
return {(p1.first * p2.second + p1.second * p2.first + bd / m) % m, bd % m};
}
P mypow(ll n, ll m) {
if (n == 0) {
return convert(1, m);
}
if (n % 2 == 0) {
P res = mypow(n / 2, m);
return multiple(res, res, m);
}
return multiple(mypow(n - 1, m), convert(10, m), m);
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
ll n, m;
cin, n, m;
P res = mypow(n, m);
print(res.first);
return 0;
}
| #include<bits/stdc++.h>
#define ll long long
#define in insert
#define pb push_back
#define endl '\n'
#define ub upper_bound
#define lb lower_bound
#define pf pop_front
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);srand(time(0));
using namespace std;
ll po(ll x,ll y,ll z)
{
if(y==0)return 1;
ll r=po(x,y/2,z);
r=(r*r)%z;
if(y&1)r=(r*x)%z;
return r;
}
void fun()
{
ll a,b;cin>>a>>b;
ll r=b*b;
ll x=po(10,a,r);
cout<<x/b;
}
int main()
{
fast;
int t=1;
//cin>>t;
while(t--)
{
fun();
}
} |
// execute g++ main.cpp -std=c++14 -I C:\Users\naoya\Desktop\code\Atcoder
#include<bits/stdc++.h>
//#include<atcoder/all>
typedef long long ll;
typedef long double ld;
using namespace std;
//using namespace atcoder;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;
//ordered_set 重複不可
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// use set_function + find_by_order(select itr-num)
#define REP(i, l, n) for(int i=(l), i##_len=(n); i<i##_len; ++i)
#define ALL(x) (x).begin(),(x).end()
#define pb push_back
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;}
template<class T>inline bool chmax(T &a, T b) {if(a < b) {a = b;return true;}return false;}
template<class T>inline bool chmin(T &a, T b) {if(a > b) {a = b;return true;}return false;}
char alpha[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
char Alpha[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, 1, -1};
//cout << std::fixed << std::setprecision(15) << y << endl; //小数表示
const ll MOD = 998244353;
const ll MAX = 1000000; //テーブルの数
long long fac[MAX], finv[MAX], inv[MAX];
// finv ->階乗割り算
// fac ->階乗掛け算
// テーブルを作る前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++){
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
long long COM(int n, int k){
if (n < k) return 0;
if (n < 0 || k < 0) return 0;
return (fac[n] * (finv[k] * finv[n - k] % MOD)) % MOD;
}
int n, m;
map<ll, ll> mp;
ll solve(ll sum){ //残りを計算する
if(sum % 2 == 1){
return 0;
}
if(mp.count(sum)){
return mp[sum];
}
ll mid = 0;
for(int i = 0; i <= n; i+=2){
ll c = COM(n, i);
if(sum - i < 0){
break;
}
mid += c * solve((sum - i) / 2);
mid %= MOD;
}
mp[sum] = mid;
//cout << sum << " " << mid << endl;
return mid;
}
int main(){
COMinit();
cin >> n >> m;
if(m % 2 == 1){
cout << 0 << endl;
return 0;
}
mp[0] = 1;
cout << solve(m) << endl;
} | #include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=5005;
const int mod=998244353;
int n,m;
int dp[15][maxn];
int C[maxn][maxn];
void init() {
C[0][0]=1;
for(int i=1 ; i<maxn ; i++) {
for(int j=0 ; j<=i ; j++) {
if(!j||j==i) C[j][i]=1;
else C[j][i]=C[j-1][i-1]+C[j][i-1];
if(C[j][i]>=mod) C[j][i]-=mod;
}
}
}
signed main() {
//freopen("bruh.inp","r",stdin);
//freopen("bruh.out","w",stdout);
init();
cin>>n>>m;
dp[13][0]=1;
for(int bit=12 ; bit>=0 ; bit--) {
for (int val=0 ; val<=m ; val+=(1<<bit+1)) {
for (int x=0,nval=val ; nval>=0 ; x+=2 , nval-=(1<<bit+1)) {
dp[bit][val]+=(dp[bit+1][nval]*C[x][n])%mod;
dp[bit][val]%=mod;
// if(dp[bit][val]) cout<<"dp "<<bit<<" "<<val<<" = "<<dp[bit][val]<<endl;
}
}
}
cout<<dp[0][m];
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int tt = 1;
//cin >> tt;
while(tt--) {
int x, y;
cin >> x >> y;
int a=min(x, y);
int b = max(x, y);
if(a+3 > b) {
cout << "Yes\n";
} else {
cout << "No\n";
}
}
return 0;
}
| /*
ID: jerrywcy
LINK:
TITLE:
LANG: C++
STATUS:
*/
#include <bits/stdc++.h>
#define DEBUG
#define init(array,x) memset(array,x,sizeof(array))
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
ll sx,sy,gx,gy;
double ans;
int read(){
int f=1,x=0;
char ss=getchar();
while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
return f*x;
}
int main()
{
sx=read();sy=read();gx=read();gy=read();
if (sx>gx){swap(sx,gx);swap(sy,gy);}
ans=sx+double(gx-sx)*sy/(gy+sy);
printf("%.8lf\n",ans);
return 0;
} |
#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, s, n) for (int i = (s); i < (n); ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define len(x) (int)(x).size()
#define pb push_back
#define em emplace_back
#define vi vector<int>
#define vl vector<long long>
#define vs vector<string>
#define vc vector<char>
#define vd vector<double>
#define vb vector<bool>
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define fast \
cin.tie(0); \
ios::sync_with_stdio(false);
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
int main() {
fast;
int h, w;
cin >> h >> w;
vs s(h + 2, "");
rep(i, 0, w + 2) {
s[0] += '.';
s[h + 1] += '.';
}
rep(i, 1, h + 1) cin >> s[i];
int a = h + 1, b = w + 1;
rep(i, 1, h + 1) {
s[i] = "." + s[i];
s[i] += ".";
}
int ans = 0;
rep(i, 0, a) rep(j, 0, b) {
int cnt = 0;
if (s[i][j] == '.') ++cnt;
if (s[i][j + 1] == '.') ++cnt;
if (s[i + 1][j] == '.') ++cnt;
if (s[i + 1][j + 1] == '.') ++cnt;
if (cnt == 3 || cnt == 1) ++ans;
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC 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 print(x) cout << #x <<" = " << x <<"\n"
#define pprint(x) cout << #x <<" = (" << x.first <<", " << x.second <<")\n"
#define all(x) x.begin(), x.end()
#define ceil(a, b) ((a + b - 1) / (b))
#define INF 1000000000000000000
#define MAXN 200
#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;
int n, m, c[MAXN * MAXN], d[MAXN * MAXN];
int ans[MAXN*MAXN], v[MAXN*MAXN];
pii p[MAXN*MAXN];
int mp[MAXN][MAXN], timestamp, a, b;
void dfs(int x) {
d[x] = ++timestamp;
rep(i, 1, n) {
if(c[i] == c[x] && !d[i] && mp[i][x]) {
mp[x][i] = -1, mp[i][x] = -2;
dfs(i);
}
}
return;
}
void solve() {
timestamp = 0;
rep(i, 1, n) {
if(!d[i]) d[i] = 1, dfs(i);
}
rep(i, 1, m) {
tie(a, b) = p[i];
if(c[a] > c[b]) ans[i] = -1;
else if(c[b] > c[a]) ans[i] = -2;
else if(mp[a][b] < 0) ans[i] = mp[a][b];
else ans[i] = (d[a] > d[b] ? -1 : -2);
mp[a][b] = ans[i];
mp[b][a] = -3 - ans[i];
}
return ;
}
int dfs1(int x) {
int cnt = 1; v[x] = 1;
rep(i, 1, n) {
if(!v[i] && mp[x][i] == -1) cnt += dfs1(i);
}
return cnt;
}
bool check() {
rep(i, 1, n) {
memset(v, 0, sizeof(v));
if(dfs1(i) != c[i]) return false;
}
return true;
}
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> n >> m;
rep(i, 1, m) {
cin >> a >> b;
p[i] = {a, b};
mp[a][b] = mp[b][a] = 1;
}
rep(i, 1, n) cin >> c[i];
solve();
assert(check());
rep(i, 1, m) {
if(ans[i] == -1) cout <<"->\n";
else cout <<"<-\n";
}
return 0;
} |
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
const int R=2e5 + 5;
const int N=1e5 + 3;
const int M = 2e3 + 3;
const int INF=0x3f3f3f3f;
struct edge{
int v,w,next;
}e[R*2];
int dis[N],head[2*N],cnt=0;
int a[M][M];
struct node{
int u,d;
bool operator <(const node&rhs) const{
return d>rhs.d;
}
};
inline void addedge(int u,int v,int w){
e[++cnt].v=v;
e[cnt].w=w;
e[cnt].next=head[u];
head[u]=cnt;
}
int n,m,s;
inline void Dij(){
for(int i=1;i<=n;i++) dis[i]=INF;
dis[s]=0;
priority_queue<node> Q;
Q.push((node){s,0});
while(!Q.empty()){
node fr=Q.top();Q.pop();
int u=fr.u,d=fr.d;
if(d!=dis[u]) continue;
for(int i=head[u];i;i=e[i].next){
int v=e[i].v,w=e[i].w;
if(dis[u]+w<dis[v]){
dis[v]=dis[u]+w;
Q.push((node){v,dis[v]});
}
}
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=m;i++){
int x,y,z;
cin>>x>>y>>z;
if(!a[x][y]) a[x][y] = z;
a[x][y] = min(a[x][y], z);
addedge(x,y,z);
}
for(int i = 1; i <= n; ++i) {
s = i;
Dij(); int ans = -1;
for(int j = 1; j <= n; ++j) if(dis[j] != INF && a[j][i]) {
if(ans == -1) ans = dis[j] + a[j][i];
ans = min(ans, dis[j] + a[j][i]);
}
cout << ans << endl;
}
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < n; i++)
#define ll long long
using namespace std;
using P = pair<int, int>;
struct E {
int to, co;
E(int to=0, int co=0):
to(to), co(co) {}
};
int main(){
int n, m;
cin >> n >> m;
vector<vector<E>> g(n);
rep(i, m){
int a, b, c;
cin >> a >> b >> c;
--a, --b;
g[a].emplace_back(b, c);
}
const int INF = 1001001001;
rep(sv, n){
vector<int> dist(n, INF);
priority_queue<P, vector<P>, greater<P>> q;
auto push = [&](int v, int d){
if(dist[v] <= d) return;
dist[v] = d;
q.emplace(d, v);
};
for (auto&& e: g[sv]) push(e.to, e.co);
while(!q.empty()){
int d = q.top().first;
int v = q.top().second;
q.pop();
if(dist[v] != d) continue;
for (auto&& e : g[v]){
push(e.to, d+e.co);
}
}
int ans = dist[sv];
if (ans == INF) ans = -1;
cout << ans << endl;
}
} |
#include <bits/stdc++.h>
using namespace std;
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << '(' << p.first << ',' << p.second << ')';
return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << '{';
for(int i = 0; i < (int)v.size(); i++) {
if(i) { os << ','; }
os << v[i];
}
os << '}';
return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
cerr << " " << x;
debugg(args...);
}
#define debug(...) \
cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 510000;
ll dy[8] = {0,-1,0,1,1,-1,-1,1};
ll dx[8] = {-1,0,1,0,1,-1,1,-1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
if(a>b){
a = b; return true;
}
else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
if(a<b){
a = b; return true;
}
else return false;
}
template<typename T> inline void print(T &a){
int sz = a.size();
for(auto itr = a.begin(); itr != a.end(); itr++){
cout << *itr;
sz--;
if(sz) cout << " ";
}
cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
cout << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
cout << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
const int mod = 1e9 + 7;
//const int mod = 998244353;
int main(){
int n,m; cin >> n >> m;
vector<vpl> G(n);
rep(i,m){
int u,v,c; cin >> u >> v >> c; u--; v--; c--;
G[u].emplace_back(v,c);
G[v].emplace_back(u,c);
}
vl ans(n,-1);
auto dfs = [&](auto &&dfs, int u) -> void{
for(auto v : G[u]){
if(ans[v.first] != -1) continue;
if(ans[u] == v.second) ans[v.first] = (v.second+1) % n;
else ans[v.first] = v.second;
dfs(dfs,v.first);
//cout << v.second << "\n";
}
};
ans[0] = 0;
dfs(dfs,0);
rep(i,n) cout << ans[i]+1 << "\n";
} | #include <bits/stdc++.h>
using namespace std;
int main(){
int n, m;
cin >> n >> m;
vector<pair<int, int> > g[n];
int ans[n];
ans[0] = 1;
for(int i = 0; i<m; i++){
int u, v, c;
cin >> u >> v >> c;
g[u - 1].push_back({v - 1, c});
g[v - 1].push_back({u - 1, c});
}
vector<pair<int, int> > dfs;
int vis[n];
fill(vis, vis + n, 0);
vis[0] = 1;
dfs.push_back({0, 1});
while(!dfs.empty()){
auto x = dfs.back();
dfs.pop_back();
for(auto to: g[x.first]){
if(vis[to.first] == 0){
vis[to.first] = 1;
dfs.push_back(to);
if(ans[x.first] == to.second){
if(ans[x.first] == 1) ans[to.first] = 2;
else ans[to.first] = 1;
}
else ans[to.first] = to.second;
}
}
}
for(int i = 0; i<n; i++){
cout << ans[i] << endl;
}
}
|
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.