code_file1
stringlengths 87
4k
| code_file2
stringlengths 82
4k
|
---|---|
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<deque>
#include<iomanip>
#include<tuple>
#include<cassert>
#include<set>
#include<complex>
#include<numeric>
#include<functional>
#include<unordered_map>
#include<unordered_set>
using namespace std;
typedef long long int LL;
typedef pair<int,int> P;
typedef pair<LL,LL> LP;
const int INF=1<<30;
const LL MAX=1e9+7;
void array_show(int *array,int array_n,char middle=' '){
for(int i=0;i<array_n;i++)printf("%d%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(LL *array,int array_n,char middle=' '){
for(int i=0;i<array_n;i++)printf("%lld%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(vector<int> &vec_s,int vec_n=-1,char middle=' '){
if(vec_n==-1)vec_n=vec_s.size();
for(int i=0;i<vec_n;i++)printf("%d%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}
void array_show(vector<LL> &vec_s,int vec_n=-1,char middle=' '){
if(vec_n==-1)vec_n=vec_s.size();
for(int i=0;i<vec_n;i++)printf("%lld%c",vec_s[i],(i!=vec_n-1?middle:'\n'));
}
template<typename T> ostream& operator<<(ostream& os,const vector<T>& v1){
int n=v1.size();
for(int i=0;i<n;i++){
if(i)os<<" ";
os<<v1[i];
}
return os;
}
template<typename T1,typename T2> ostream& operator<<(ostream& os,const pair<T1,T2>& p){
os<<p.first<<" "<<p.second;
return os;
}
template<typename T> istream& operator>>(istream& is,vector<T>& v1){
int n=v1.size();
for(int i=0;i<n;i++)is>>v1[i];
return is;
}
namespace sol{
int n,m;
vector<vector<int>> grid;
bool check(int g){
int i,j,k;
int a,b,c;
vector<vector<int>> v1(n+2,vector<int>(n+2,0));
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(grid[i][j]<=g)v1[i+1][j+1]=1;
}
}
for(i=0;i<=n;i++){
for(j=0;j<=n;j++){
if(i)v1[i][j]+=v1[i-1][j];
if(j)v1[i][j]+=v1[i][j-1];
if(i && j)v1[i][j]-=v1[i-1][j-1];
}
}
for(i=0;i<=n-m;i++){
for(j=0;j<=n-m;j++){
a=v1[i+m][j+m]+v1[i][j]-v1[i][j+m]-v1[i+m][j];
if(a>=(m*m+1)/2)return true;
}
}
return false;
}
void solve(){
int i,j,k;
int a,b,c;
cin>>n>>m;
grid.assign(n,vector<int>(n));
cin>>grid;
int z[3]={-1,INF};
while(z[1]-z[0]>1){
z[2]=(z[0]+z[1])/2;
if(check(z[2]))z[1]=z[2];
else z[0]=z[2];
}
cout<<z[1]<<endl;
}
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
sol::solve();
} | #include <bits/stdc++.h>
using namespace std;
const int MAX_LEN = 50;
string DIR[4] = {"U", "D", "L", "R"};
int depth;
int si, sj;
int t[MAX_LEN][MAX_LEN], p[MAX_LEN][MAX_LEN];
int next_sim(int i, int j, bool visited[], int num) {
if ( num == 0 ) { return p[i][j]; }
bool *now_visited;
now_visited = new bool[2500];
memcpy(now_visited, visited, sizeof(visited));
now_visited[t[i][j]] = true;
int sim_p = 0;
if ( i > 0 && (! visited[t[i-1][j]]) && p[i-1][j] > sim_p ) {
int tmp = next_sim(i-1, j, now_visited, num-1);
if ( tmp > sim_p ) { sim_p = tmp; }
}
if ( i < MAX_LEN-1 && (! visited[t[i+1][j]]) && p[i+1][j] > sim_p ) {
int tmp = next_sim(i+1, j, now_visited, num-1);
if ( tmp > sim_p ) { sim_p = tmp; }
}
if ( j > 0 && (! visited[t[i][j-1]]) && p[i][j-1] > sim_p ) {
int tmp = next_sim(i, j-1, now_visited, num-1);
if ( tmp > sim_p ) { sim_p = tmp; }
}
if ( j < MAX_LEN-1 && (! visited[t[i][j+1]]) && p[i][j+1] > sim_p ) {
int tmp = next_sim(i, j+1, now_visited, num-1);
if ( tmp > sim_p ) { sim_p = tmp; }
}
delete[] now_visited;
return p[i][j] + sim_p;
}
int sakiyomi(int i, int j, bool visited[]) {
int sim_p = next_sim(i, j, visited, depth);
return sim_p;
}
int next_dir(int i, int j, bool visited[]) {
int ret = 4;
int sim_p = -1;
int sim_i, sim_j;
if ( i > 0 && (! visited[t[i-1][j]]) ) {
int tmp = sakiyomi(i-1, j, visited);
if ( tmp > sim_p ) { ret = 0; sim_p = tmp; }
}
if ( i < MAX_LEN-1 && (! visited[t[i+1][j]]) ) {
int tmp = sakiyomi(i+1, j, visited);
if ( tmp > sim_p ) { ret = 1; sim_p = tmp; }
}
if ( j > 0 && (! visited[t[i][j-1]]) ) {
int tmp = sakiyomi(i, j-1, visited);
if ( tmp > sim_p ) { ret = 2; sim_p = tmp; }
}
if ( j < MAX_LEN-1 && (! visited[t[i][j+1]]) ) {
int tmp = sakiyomi(i, j+1, visited);
if ( tmp > sim_p ) { ret = 3; sim_p = tmp; }
}
return ret;
}
void update_pos(int* i, int* j, int next) {
if ( next == 0 ) { *i -= 1; }
if ( next == 1 ) { *i += 1; }
if ( next == 2 ) { *j -= 1; }
if ( next == 3 ) { *j += 1; }
}
string calc() {
string ans = "";
int max_p = p[si][sj];
for ( depth = 0 ; depth < 3; depth++ ) {
bool visited[2500] = {};
string now_ans = "";
int point = 0;
int i = si;
int j = sj;
visited[t[i][j]] = true;
point = p[i][j];
while ( true ) {
int next = next_dir(i, j, visited);
if ( next == 4 ) { break; }
now_ans += DIR[next];
update_pos(&i, &j, next);
visited[t[i][j]] = true;
point += p[i][j];
}
if ( point > max_p ) {
max_p = point;
ans = now_ans;
}
}
return ans;
}
int main() {
cin >> si >> sj;
for ( int i = 0; i < MAX_LEN; i++ ) {
for ( int j = 0; j < MAX_LEN; j++ ) {
cin >> t[i][j];
}
}
for ( int i = 0; i < MAX_LEN; i++ ) {
for ( int j = 0; j < MAX_LEN; j++ ) {
cin >> p[i][j];
}
}
cout << calc() << endl;
return 0;
}
|
#include "bits/stdc++.h"
#include "random"
#include <unistd.h>
using namespace std;
#define ll long long
#define vi vector<int>
#define vl vector<long long>
#define vvi vector<vi>
#define pi pair<int,int>
#define mp make_pair
#define pb push_back
#define MOD int(1e9) + 7
#define PAI 3.1415926535
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define chmax(x, y) x = max(x,y)
#define chmin(x, y) x = min(x,y)
#define pr(x) cout << x << endl
#define Endl endl
#define rep(i, n) for(int i = 0 ; i < n; ++i)
const int dx2[4] = {0,1,0,1};
const int dy2[4] = {0,0,1,1};
const int dx[4] = {1,0,-1,0};
const int dy[4] = {0,1,0,-1};
const int ddx[8] = {-1,0,1,-1,1,-1,0,1};
const int ddy[8] = {-1,-1,-1,0,0,1,1,1};
const int inf = 99999999;
const ll linf = 1LL << 62;
ll gcd(ll a,ll b){
if(a < b)swap(a , b);
if(a % b != 0) return(gcd(b, a%b));
return b;
}
ll lcm(ll a,ll b){
if(a < b)swap(a , b);
return (a / gcd(a , b)) * b;
}
int Uniform_Random(int a, int b){ // (a <= x <= b)
random_device rd;
mt19937 mt(rd());
uniform_int_distribution<int> rv1(a, b);
return rv1(mt);
}
int H, W;
vector<vector<bool>> used(16, vector<bool>(16, false));
int main(){
int n; cin >> n;
ll l = -linf, r = linf;
ll s = 0;
rep(i, n){
ll a, t; cin >> a >> t;
if(t == 1){
s += a;
l += a;
r += a;
}else if(t == 2){
chmax(l, a);
chmax(r, a);
}else{
chmin(l, a);
chmin(r, a);
}
}
int q; cin >> q;
rep(i, q){
ll x; cin >> x;
ll ans = x + s;
if(ans < l) ans = l;
if(ans > r) ans = r;
cout << ans << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
int n;
cin >> n;
vector<int> a(n),t(n);
for (int i = 0; i < n; i++) {
cin >> a[i] >> t[i];
}
int q;
cin >> q;
vector<int> x(q);
for (auto &it:x) cin >> it;
int sum = 0;
int l = -INT_MAX;
int r = INT_MAX;
for (int i = 0; i < n; i++) {
if (t[i] == 1) {
sum += a[i];
l += a[i];
r += a[i];
}
if (t[i] == 2) {
l = max(l, a[i]);
r = max(r, a[i]);
}
if (t[i] == 3) {
l = min(l, a[i]);
r = min(r, a[i]);
}
}
for (int i = 0; i < q; i++) {
int res = x[i]+sum;
res = max(l, res);
res = min(r, res);
cout << res << '\n';
}
}
|
#include <iostream>
using namespace std;
int main(){
int A, B, C;
cin >> A >> B >> C;
cout << (A + C > B ? "Takahashi" : "Aoki") << endl;
} | #include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main(){
int x,y,z;cin>>x>>y>>z;
if(x==y && z==0){
cout<<"Aoki";
}
else if(x==y && z==1){
cout<<"Takahashi";
}
else if(x>y){
cout<<"Takahashi";
}
else{
cout<<"Aoki";
}
return 0;
} |
#include <bits/stdc++.h>
#define Rep(i,n,m) for(int i=(int)(n);i<(int)(m);i++)
#define rep(i,n) Rep(i,0,n)
#define all(v) v.begin(),v.end()
using namespace std;
using ll=long long;
using vi=vector<int>;
using vll=vector<ll>;
#define _GLIBCXX_DEBUG
const double e=0.000001;
const double pi=M_PI;
struct Point{
double x,y;
Point(){}
Point(double a,double b){
x=a;y=b;
}
};
const Point O={0,0};
Point operator-(Point a,Point b){
Point ret;
ret.x=a.x-b.x;
ret.y=a.y-b.y;
return ret;
};
Point operator+(Point a,Point b){
Point ret;
ret.x=a.x+b.x;
ret.y=a.y+b.y;
return ret;
};
void operator+=(Point &a,Point b){a=a+b;};
void operator-=(Point &a,Point b){a=a-b;};
double dis(Point p,Point q){
double d2=(p.x-q.x)*(p.x-q.x)+(p.y-q.y)*(p.y-q.y);
return sqrt(d2);
};
double abs(Point p){
return dis(p,O);
};
double rtod(double r){
r/=pi;r*=180;
return r;
};
double dtor(double d){
d/=180.0;
d*=pi;
return d;
};
double rad(Point p){
return atan2(p.y,p.x);
};
double dmod(double d){
int k;
if(d<0){
k=(int)(d/180);k--;
}
else{
k=ceil(d/180);
}
k/=2;
return d-k*360;
};
Point rotate(double t,Point p,Point cons){
p-=cons;
double r=abs(p);
double th=t+rad(p);
Point ret;
ret.x=r*cos(th);ret.y=r*sin(th);
return ret+cons;
};
bool s(Point a,Point b){
if(abs(b.x-a.x)<e) return a.y<b.y;
return a.x<b.x;
};
bool same(Point a,Point b){
if(abs(a.x-b.x)<e&&abs(a.y-b.y)<e) return true;
else return false;
}
int main(){
int N;cin>>N;
vector<Point> a(N),b(N);
rep(i,N){
double x,y;cin>>x>>y;
a[i].x=x;a[i].y=y;
}
rep(i,N){
double x,y;cin>>x>>y;
b[i].x=x;b[i].y=y;
}
sort(all(a),s);
sort(all(b),s);
if(N<2) cout << "Yes" << endl;
else{
bool test=false;
rep(i,N){
rep(j,N){
if(i==j) continue;
Point q=b[0]-a[i];
double t2=rad(b[1]-b[0]);
double t1=rad(a[j]-a[i]);
vector<Point> c(N);
rep(k,N){
c[k]=rotate(t2-t1,a[k]+q,b[0]);
}
sort(all(c),s);
bool tes=true;
rep(k,N){
if(same(b[k],c[k])) continue;
tes=false;
}
if(tes) test=true;
}
}
if(test) cout << "Yes" << endl;
else cout << "No" << endl;
}
return 0;
} | #include <bits/stdc++.h>
#define rep3(i, s, n, a) for (int i = (s); i < (int)(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 ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;
int main() {
ll n, k;
cin >> n >> k;
ll res = 0;
for(ll x=2; x<=2*n;x++){
if((x-k < 2) || (2*n < x - k)) continue;
res += (x-1 - 2LL*max((x-n-1), 0LL)) * ((x-k-1) - 2LL*max(x-k-1-n, 0LL));
}
cout << res << endl;
return 0;
} |
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <complex>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <bitset>
#include <ctime>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <cassert>
#include <cstddef>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <sstream>
#include <fstream>
#include <functional>
using namespace std;
#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++)
#define RREP(i, a) for(int (i) = (a) - 1; (i) >= 0; (i)--)
#define FORR(i, a, b) for(int (i) = (a) - 1; (i) >= (b); (i)--)
#define DEBUG(C) cerr << #C << " = " << C << endl;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<LL>;
using VVL = vector<VL>;
using VD = vector<double>;
using VVD = vector<VD>;
using PII = pair<int, int>;
using PDD = pair<double, double>;
using PLL = pair<LL, LL>;
using VPII = vector<PII>;
template<typename T> using VT = vector<T>;
#define ALL(a) begin((a)), end((a))
#define RALL(a) rbegin((a)), rend((a))
#define SORT(a) sort(ALL((a)))
#define RSORT(a) sort(RALL((a)))
#define REVERSE(a) reverse(ALL((a)))
#define MP make_pair
#define FORE(a, b) for (auto &&a : (b))
#define EB emplace_back
#define GREATER(T) T, VT<T>, greater<T>
template<typename T> inline bool chmax(T &a, T b){if (a < b){a = b;return true;}return false;}
template<typename T> inline bool chmin(T &a, T b){if (a > b){a = b;return true;}return false;}
const int INF = 1e9;
const int MOD = INF + 7;
const LL LLINF = 1e18;
void solve() {
int N, K;
cin >> N >> K;
VI a(N);
REP(i, N) {
cin >> a[i];
}
SORT(a);
map<int, int> mp;
FORE(e, a) mp[e]++;
LL ans = 0;
bool end = false;
int cnt = 0;
while (!end && cnt < K) {
for (int i = 0; ; i++) {
if (mp.count(i) == 0) {
if (i == 0) {
end = true;
}
ans += i;
break;
}
if (--mp[i] == 0) {
mp.erase(i);
}
}
cnt++;
}
cout << ans << endl;
}
int main(void) {
#ifndef ONLINE_JUDGE
const auto in_stream = freopen("../in.txt", "r", stdin);
if (in_stream == nullptr) {
cerr << "ERROR!" << endl;
return 1;
}
#endif
solve();
#ifndef ONLINE_JUDGE
fclose(in_stream);
#endif
}
| #include<bits/stdc++.h>
using namespace std;
using namespace std::chrono;
const int mod=1e9+7;
const int mex=2002;
#define ll long long
#define test int t;cin>>t;while(t--)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define fo(i,a,n) for(int i=a;i<n;i++)
#define rfo(i,a,b) for(int i=a;i>=b;i--)
#define bg begin()
#define en end()
#define fi first
#define se second
#define ub upper_bound
#define lb lower_bound
#define pb push_back
#define veci vector<int>
#define veclli vector<long long int>
#define all(x) x.begin(),x.end()
#define sci(x) scanf("%d",&x);
#define scc(x) scanf("%c",&x);
#define scs(x) scanf("%s",x);
#define debug(arr,n) for(int i=0;i<n;i++) printf("%d ",arr[i]);
#define sz(x) x.size()
#define loop(x) for(auto it=x.begin();it!= x.end();it++)
#define int long long
signed main()
{
int n,k;
cin>>n>>k;
map<int,int> mp;
int a[n];
fo(i,0,n)
{cin>>a[i];
mp[a[i]]++;
}
int bx=k,ans=0;
fo(i,0,n+1)
{
bx=min(bx,mp[i]);
ans+=bx;
}
cout<<ans<<endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define MS(x) memset((x), -1, sizeof((x)))
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
int yta(){
int n;
cin >> n;
vector<int> a(n);
unordered_set<int> st;
for(int i = 0;i < n;i++) cin >> a[i];
if(n % 2 == 1){
cout << "Second" << endl;
return 0;
}
for(int i = 0;i < n;i++){
if(st.find(a[i]) != st.end()){
st.erase(a[i]);
}else{
st.insert(a[i]);
}
}
cout << (st.size() == 0 ? "Second" : "First") << endl;
return 0;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while(t--) yta();
return 0;
}
| #define MOD_TYPE 1
#pragma region Macros
#include <bits/stdc++.h>
using namespace std;
#if 0
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
using Int = boost::multiprecision::cpp_int;
using lld = boost::multiprecision::cpp_dec_float_100;
#endif
#if 1
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#endif
using ll = long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
template <typename Q_type>
using smaller_queue = priority_queue<Q_type, vector<Q_type>, greater<Q_type>>;
constexpr ll MOD = (MOD_TYPE == 1 ? (ll)(1e9 + 7) : 998244353);
//constexpr ll MOD = 1;
constexpr int INF = (int)1e9 + 10;
constexpr ll LINF = (ll)4e18;
constexpr double PI = acos(-1.0);
constexpr double EPS = 1e-9;
constexpr int Dx[] = {0, 0, -1, 1, -1, 1, -1, 1, 0};
constexpr int Dy[] = {1, -1, 0, 0, -1, -1, 1, 1, 0};
#define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define REPI(i, m, n) for (int i = m; i < (int)(n); ++i)
#define repi(i, n) REPI(i, 0, n)
#define MP make_pair
#define MT make_tuple
#define YES(n) cout << ((n) ? "YES" : "NO") << "\n"
#define Yes(n) cout << ((n) ? "Yes" : "No") << "\n"
#define possible(n) cout << ((n) ? "possible" : "impossible") << "\n"
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << "\n"
#define Yay(n) cout << ((n) ? "Yay!" : ":(") << "\n"
#define all(v) v.begin(), v.end()
#define NP(v) next_permutation(all(v))
#define dbg(x) cerr << #x << ":" << x << "\n";
struct io_init
{
io_init()
{
cin.tie(0);
ios::sync_with_stdio(false);
cout << setprecision(30) << setiosflags(ios::fixed);
};
} io_init;
template <typename T>
inline bool chmin(T &a, T b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <typename T>
inline bool chmax(T &a, T b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
inline ll CEIL(ll a, ll b)
{
return (a + b - 1) / b;
}
template <typename A, size_t N, typename T>
inline void Fill(A (&array)[N], const T &val)
{
fill((T *)array, (T *)(array + N), val);
}
template <typename T, typename U>
constexpr istream &operator>>(istream &is, pair<T, U> &p) noexcept
{
is >> p.first >> p.second;
return is;
}
template <typename T, typename U>
constexpr ostream &operator<<(ostream &os, pair<T, U> &p) noexcept
{
os << p.first << " " << p.second;
return os;
}
#pragma endregion
void calc(vector<int> &a)
{
}
void solve()
{
int n;
cin >> n;
vector<int> a(n);
rep(i, n) cin >> a[i];
if (n % 2 == 0)
{
// 先手はxorを0以外にしたい
map<ll, ll> cnt;
rep(i, n) cnt[a[i]]++;
bool ispair = true;
for (auto [k, v] : cnt)
{
if (v & 1)
{
ispair = false;
break;
}
}
if (!ispair)
cout << "First\n";
else
cout << "Second\n";
}
else
{
cout << "Second\n";
}
}
int main()
{
int testcase;
cin >> testcase;
rep(ti, testcase)
{
solve();
}
} |
#include<bits/stdc++.h>
using namespace std;
int main(){
int N;
cin >> N;
int array1[N], array2[N];
for(int i = 0; i < N; i++) cin >> array1[i];
for(int i = 0; i < N; i++) cin >> array2[i];
int result = 0;
for(int i = 1; i <= 1000; i++){
bool check = true;
for(int j = 0; j < N; j++){
if(i < array1[j] || i > array2[j]) check = false;
}
if(check) result++;
}
cout << result << endl;
//for(int i = 0; i < N; i++) cout << array1[i] << array2[i];
} | #include <algorithm>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
int main(){
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 l = 0, r = 1000;
for (int i = 0; i < n; ++i){
l = max(l, a[i]);
r = min(r, b[i]);
}
cout << max(0, r - l + 1);
return 0;
}
|
// __________________
// | ________________ |
// || ____ ||
// || /\ | ||
// || /__\ | ||
// || / \ |____ ||
// ||________________||
// |__________________|
// \###################\
// \###################\
// \ ____ \
// \_______\___\_______\
// An AC a day keeps the doctor away.
#ifdef local
#include <bits/extc++.h>
#define safe std::cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
#define debug(args...) qqbx(#args, args)
using ost = std::ostream;
#define DESTL(STL, BEG, END, OUT) \
template <typename ...T> ost& operator<<(ost &O, std::STL<T...> v) { int f=0; for(auto x: v) O << (f++ ? ", " : BEG) << OUT; return O << END; }
DESTL(deque, "[", "]", x); DESTL(vector, "[", "]", x);
DESTL(set, "{", "}", x); DESTL(multiset, "{", "}", x); DESTL(unordered_set, "{", "}", x);
DESTL(map , "{", "}", x.first << ":" << x.second); DESTL(unordered_map , "{", "}", x.first << ":" << x.second);
template <typename U, typename V> ost& operator<<(ost &O, std::pair<U,V> p) { return O << '(' << p.first << ',' << p.second << ')'; }
template <typename T, size_t N> ost& operator<<(ost &O, std::array<T,N> a) { int f=0; for(T x: a) O << (f++ ? ", " : "[") << x; return O << "]"; }
template <typename T, size_t ...I> ost& prtuple(ost &O, T t, std::index_sequence<I...>) { return (..., (O << (I ? ", " : "(") << std::get<I>(t))), O << ")"; }
template <typename ...T> ost& operator<<(ost &O, std::tuple<T...> t) { return prtuple(O, t, std::make_index_sequence<sizeof...(T)>()); }
template <typename ...T> void qqbx(const char *s, T ...args) {
int cnt = sizeof...(T);
(std::cerr << "\033[1;32m(" << s << ") = (" , ... , (std::cerr << args << (--cnt ? ", " : ")\033[0m\n")));
}
#else
#pragma GCC optimize("Ofast")
#pragma loop_opt(on)
#include <bits/extc++.h>
#include <bits/stdc++.h>
#define debug(...) ((void)0)
#define safe ((void)0)
#endif // local
#define all(v) begin(v),end(v)
#define get_pos(v,x) int(lower_bound(begin(v),end(v),x)-begin(v))
#define sort_uni(v) sort(begin(v),end(v)),v.erase(unique(begin(v),end(v)),end(v))
#define pb emplace_back
#define ff first
#define ss second
#define mem(v,x) memset(v,x,sizeof v)
using namespace std;
using namespace __gnu_pbds;
typedef int64_t ll;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pld;
template <typename T> using max_heap = std::priority_queue<T,vector<T>,less<T> >;
template <typename T> using min_heap = std::priority_queue<T,vector<T>,greater<T> >;
template <typename T> using rbt = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
constexpr ld PI = acos(-1), eps = 1e-7;
constexpr ll N = 200025, INF = 1e18, MOD = 1000000007, K = 14699, inf = 1e9;
constexpr inline ll cdiv(ll x, ll m) { return x/m + (x%m ? (x<0) ^ (m>0) : 0); } // ceiling divide
constexpr inline ll modpow(ll e,ll p,ll m=MOD) { ll r=1; for(e%=m;p;p>>=1,e=e*e%m) if(p&1) r=r*e%m; return r; }
map<int,int> mp[N];
int pa[N];
int anc(int x) { return x==pa[x] ? x : pa[x]=anc(pa[x]); }
void join(int x, int y) {
if((x=anc(x)) == (y=anc(y))) return;
if(mp[x].size() < mp[y].size()) {
for(auto [a, b]: mp[x])
mp[y][a] += b;
mp[x].clear();
pa[x] = y;
} else {
for(auto [a, b]: mp[y])
mp[x][a] += b;
mp[y].clear();
pa[y] = x;
}
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n, q;
cin >> n >> q;
for(int i = 0; i < n; i++) {
int c;
cin >> c;
mp[i][c] = 1;
}
for(int i = 0; i < n; i++) pa[i] = i;
for(int i = 0; i < q; i++) {
int t;
cin >> t;
if(t == 1) {
int a, b;
cin >> a >> b;
--a, --b;
join(a, b);
} else if(t == 2) {
int x, y;
cin >> x >> y;
--x;
debug(x, mp[anc(x)]);
cout << mp[anc(x)][y] << '\n';
}
}
}
| #include<bits/stdc++.h>
#define ll long long
#define clr(x,i) memset(x,i,sizeof(x))
using namespace std;
const int N=2005;
inline int read()
{
int x=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') f=-1; ch=getchar();}
while(ch>='0'&&ch<='9') {x=x*10+ch-'0',ch=getchar();}
return x*f;
}
const ll mod = 1e9 + 7;
int n,m,l[N][N],r[N][N],u[N][N],d[N][N];
ll bin[N*N];
char s[N][N];
void solve()
{
cin >> n >> m;
for(int i=1; i<=n; i++) scanf("%s", s[i]+1);
bin[0] = 1;
for(int i=1; i<=4000000; i++) bin[i] = bin[i-1] * 2 % mod;
ll ans = 0, k = 0;
for(int i=1; i<=n; i++) {
for(int j=1; j<=m; j++) {
if(s[i][j] != '.') {
l[i][j] = r[i][j] = u[i][j] = d[i][j] = 0;
continue;
}
k++;
if(s[i][j-1] == '.') l[i][j] = l[i][j-1] + 1; else l[i][j] = 1;
if(s[i-1][j] == '.') u[i][j] = u[i-1][j] + 1; else u[i][j] = 1;
}
}
for(int i=n; i>=1; i--) {
for(int j=m; j>=1; j--) {
if(s[i][j] != '.') {
l[i][j] = r[i][j] = u[i][j] = d[i][j] = 0;
continue;
}
if(s[i][j+1] == '.') r[i][j] = r[i][j+1] + 1; else r[i][j] = 1;
if(s[i+1][j] == '.') d[i][j] = d[i+1][j] + 1; else d[i][j] = 1;
}
}
for(int i=1; i<=n; i++) {
for(int j=1; j<=m; j++) {
if(s[i][j] != '.') {
continue;
}
int s = (r[i][j] + l[i][j] - 1) + (u[i][j] + d[i][j] - 1) - 1;
ans += bin[k - s] * (bin[s] - 1 + mod) % mod;
ans %= mod;
}
}
cout << ans;
}
int main()
{
int T = 1;
while(T--) solve();
} |
#include <bits/stdc++.h>
#define dbg(args...){ string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
#define fastio() ios_base::sync_with_stdio(false); cin.tie(nullptr)
#define pb push_back
#define ff first
#define ss second
using namespace std;
using ll = long long;
void err(istream_iterator<string> ) {cerr << "NEXT\n"; }
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << ", ";
err(++it, args...);
}
const int MOD = 1e9 + 7, MOD1 = (119 << 23) | 1;
const int INF = 1e9 + 5, MAX = 2e5 + 5;
int fat[MAX], siz[MAX], ans = 0;
int get(int x) {
return ((fat[x] == x)? x : fat[fat[x]] = get(fat[x]));
}
void join(int x, int y) {
x = get(x);
y = get(y);
if (x == y) return;
++ans;
if (siz[x] < siz[y]) swap(x, y);
siz[x] += siz[y];
fat[y] = x;
}
void run_case() { // don't forget to reset global variables at the end of each test case
int n;
cin >> n;
vector<int> arr(n);
for (int &x : arr) {
cin >> x;
fat[x] = x;
}
for (int i = 0; 2 * i < n; ++i) {
join(arr[i], arr[n - i - 1]);
}
cout << ans << "\n";
}
int main() {
fastio();
int tests = 1;
//cin >> tests;
while (tests--) {
run_case();
}
return 0;
}
/* 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
*/
/* Let n be the main variable in the problem.
If n ≤ 12, the time complexity can be O(n!).
If n ≤ 25, the time complexity can be O(2^n).
If n ≤ 100, the time complexity can be O(n^4).
If n ≤ 500, the time complexity can be O(n^3).
If n ≤ 10^4, the time complexity can be O(n^2).
If n ≤ 10^6, the time complexity can be O(n log n).
If n ≤ 10^8, the time complexity can be O(n).
If n > 10^8, the time complexity can be O(log n) or O(1).
*/ |
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define Kshitiz ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0);
#define deb(x) cout << #x << " is :" << x << "\n";
#define print(x) cout << x << "\n";
#define int long long
#define forf(i,k,n) for (int i = k; i < n; i++)
#define forb(i,n, k) for (int i = n - 1; i >= k; i--)
#define seti set<int>
#define useti unordered_set<int>
#define mapii map<int, int>
#define umapii unordered_map<int, int>
#define vpi vector <pair<int , int> >
#define vi vector<int>
#define pi pair<int , int >
#define vvi vector<vector<int>>
#define pb push_back
#define sz size()
#define forall(var) var.begin(), var.end()
#define present(c, x) (c.find(x) != c.end())
#define desc greater<int>()
#define spc <<" "<<
#define YES cout << "YES\n";
#define NO cout << "NO\n";
#define Yes cout << "Yes\n";
#define No cout << "No\n";
#define minus1 cout << "-1\n";
#define ndl cout << "\n";
#define gcd(a, b) __gcd(a, b)
#define PI 3.1415926535897932384626
#define imin INT_MIN
#define imax INT_MAX
#define fi first
#define se second
#define set_bits(x) __builtin_popcountint(x)
const int MN=4e5+1;
int M = 1e9 + 7;
int mod(int n) { return (n % M + M) % M; }
int modM(int n, int m) { return ((n % M * m % M) + M) % M; }
int modA(int n, int m) { return ((n % M + m % M) + M) % M; }
int modS(int n, int m) { return ((n % M - m % M) + M) % M; }
int power(int a , int n);
int mInv(int a){ return power(a , M-2); }
//========================XINYSTER=================================//
vector<int> adj[MN];
bool vis[MN] ;
int dfs( int v ){
vis[v]=1;
//cout<<v<<"->";
int n =1 ;
for( auto child : adj[v]){
if( vis[child]==0)
n+=dfs( child );
}
return n;
}
void solve (){
int n ; cin >> n ;
int arr[n] ;
for(int i=0 ;i<n ;i++){
cin >> arr [i ];
}
for( int i=0 ;i<n/2; i++){
int a = arr[i] , b= arr[n-i-1];
adj[a].push_back(b) , adj[b].push_back(a);
}
int ct=0;
for( int i=0; i<n; i++){
if( vis[arr[i]]!=1){
int k = dfs(arr[i] );
//deb(k)
ct += k-1 ;
}
}
cout << ct ;
}
int32_t main(){
Kshitiz
int t = 1;
//cin >> t;
for(int i=1; i<=t; i++){
//cout<<"Case #"<<i<<" :\n";
solve();
}
return 0 ;
}
//===================================================================//
int power(int a , int n){
int result = 1;
while(n){
if(n & 1) result = (result * a) % M;
n >>= 1;
a = (a * a) % M;
}
return result;
}
|
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using P=pair<ll,ll>;
template<class T> using V=vector<T>;
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
const ll inf=(1e18);
//const ll mod=998244353;
const ll mod=1000000007;
const vector<int> dy={-1,0,1,0},dx={0,-1,0,1};
ll GCD(ll a,ll b) {return b ? GCD(b,a%b):a;}
ll LCM(ll c,ll d){return c/GCD(c,d)*d;}
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(20);}} __init;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<class T>void debag(const vector<T> &a){cerr<<"debag :";for(auto v:a)cerr<<v<<" ";cerr<<"\n";}
template<class T>void print(const vector<T> &a){for(auto v:a)cout<<v<<" ";cout<<"\n";}
int main(){
int n;
cin>>n;
V<int> id(2*n+5,0);
V<bool> used(2*n+5,0);
V<int> a(n),b(n);
V<int> cont(2*n,-1);
bool ok=true;
for(int i=0;i<n;i++){
cin>>a[i]>>b[i];
if(a[i]!=-1){
a[i]--;
if(used[a[i]])ok=false;
used[a[i]]=1;
id[a[i]]=i+1;
}
if(b[i]!=-1){
b[i]--;
if(used[b[i]])ok=false;
used[b[i]]=1;
id[b[i]]=-(i+1);
}
if(a[i]!=-1&&b[i]!=-1){
cont[a[i]]=b[i];
cont[b[i]]=a[i];
}
}
if(!ok){
cout<<"No"<<"\n";
return 0;
}
V<bool> dp(2*n+1,false);
dp[0]=true;
for(int i=0;i<2*n;i++){
if(!dp[i])continue;
for(int j=i+1;j<2*n;j++){//i~jを埋める,i~jは対応関係から偶数長
int d=j-i+1;
if(d%2==1)continue;
d/=2;
bool able=true;
V<bool> ex(n,false);
for(int k=0;k<d;k++){
int l=i+k,r=i+k+d;
if(cont[l]!=-1&&!(i<=cont[l]&&cont[l]<=j))able=false;
if(cont[r]!=-1&&!(i<=cont[r]&&cont[r]<=j))able=false;
if(id[l]!=0&&id[r]!=0){
if(id[l]<0||id[l]+id[r]!=0)able=false;
}
if(id[l]<0||id[r]>0){
able=false;
}else{
if(id[l]!=0){
int v=id[l]-1;
if(ex[v]){
able=false;
}
ex[v]=true;
}
}
}
if(able)dp[j+1]=1;
}
}
cout<<(dp[2*n]?"Yes":"No")<<"\n";
}
| #include<bits/stdc++.h>
using namespace std;
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args&&... args) {
const char *comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
#define pi 3.141592653589
#define MOD 1000000007
#define to() int tt; cin>>tt; while(tt--)
#define pb push_back
#define in insert
#define mp make_pair
#define ff first
#define ss second
#define si(s) s.size()
#define fori(it,v) for(it=v.begin();it!=v.end(); it++)
#define for1(i,low,high) for(int i=low;i<high;i++)
#define for2(i,low,high) for(int i=low;i<=high;i++)
#define rev(i,high,low) for(int i=high;i>=low ;i--)
#define all(x) x.begin(), x.end()
#define fil(x,i) memset(x,i,sizeof(x));
#define setbits(x) __builtin_popcount(x)
#define boost ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define read freopen("input.txt","r",stdin)
#define write freopen("output.txt","w",stdout)
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
void i_o() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin) ;
freopen("output.txt", "w", stdout) ;
#endif
}
int n,m,k;
vector<vector<int> >g;
vector<int>c;
vector<int>d[20];
int dp[1<<18][20];
void bfs(int i){
d[i][c[i]]=0;
queue<int>q;
q.push(c[i]);
while(!q.empty()){
int cur=q.front(); q.pop();
for(int j:g[cur]){
if(d[i][j]>d[i][cur]+1){
if(d[i][j]==1e7) q.push(j);
d[i][j]=d[i][cur]+1;
}
}
}
}
int fun(int mask,int i){
if(mask==0)
return 0;
if(dp[mask][i]!=-1) return dp[mask][i];
int ans=1e7;
if(i==k){
for(int j=0;j<k;j++){
ans=min(ans,fun(mask^(1<<j),j)+1);
}
}
else
for(int j=0;j<k;j++){
if(!(mask&(1<<j))) continue;
ans=min(ans,fun(mask^(1<<j),j)+d[j][c[i]]);
}
return dp[mask][i]=ans;
}
int main()
{
boost; i_o();
cin>>n>>m;
g.resize(n+1);
for1(i,0,m){
int a,b; cin>>a>>b;
g[a].pb(b); g[b].pb(a);
}
cin>>k;
c.resize(k);
for1(i,0,k) d[i].resize(n+1,1e7);
for1(i,0,k) cin>>c[i];
for1(i,0,k) bfs(i);
/* for1(i,0,k){
cout<<"src= "<<c[i]<<endl;
for1(j,1,n+1) cout<<d[i][j]<<" ";
cout<<endl;
}
*/
fil(dp,-1);
/*
for1(mask,0,(1<<k)) for1(i,0,k+1) dp[mask][i]=1e7;
for1(i,0,k) dp[1<<i][i]=d[0][i]=0;
for(int mask=0;mask<(1<<k);mask++){
for(int i=0;i<k;i++){
if(!(mask&(1<<i))) continue;
for(int j=0;j<k;j++){
if(!(mask&(i<<j))) continue;
dp[mask][i]=min(dp[mask][i],dp[mask^i][j]+d[c[j]][c[i]]);
}
}
}
*/
int ans=fun((1<<k)-1,k);
if(ans>=1e7) ans=-1;
cout<<ans<<endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
int main(){
long long n;
long long t;
cin >> t >> n;
long long ans = (n*100-1)/t + n;
cout << ans <<endl;
} | #include <bits/stdc++.h>
using namespace std;
using ll=long long;
using ld=long double;
using P=pair<int,int>;
#define rep(i,n) for(int i=0;i<n;i++)
int main(){
ll t,n;
cin >> t >> n;
ll l=0,r=1e15;
while(l+1<r){
ll i=(l+r)/2;
ll k=i*(100+t)/100-i;
if(n<=k) r=i;
else l=i;
}
cout << l*(100+t)/100+1 << endl;
} |
#include<bits/stdc++.h>
#define rep(i,N) for(int i=0;i<N;i++)
#define rep2(i,N) for(int i=1;i<=N;i++)
using namespace std;
long long INF=1e18;
long long mod=1e9+7;
long long GCD(long long a, long long b) {
if (a < 0) a = -a;
if (b < 0) b = -b;
if (b == 0) return a;
else return GCD(b, a % b);
}
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;
}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
#define debug 0
int main(){
bool exist[1010]={};
int n,m;
cin>>n>>m;
rep(i,n){
int a;
cin>>a;
exist[a]=!exist[a];
}
rep(i,m){
int a;
cin>>a;
exist[a]=!exist[a];
}
for(int i=1;i<=1000;i++){
if(exist[i])cout<<i<<" ";
}
cout<<endl;
}
/*
*/
| #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using Graph= vector<vector<int>>;
#define rep(i,n) for (ll i=0; i < (n); ++i)
#define rep2(i,n,m) for(ll i=n;i<=m;i++)
#define rep3(i,n,m) for(ll i=n;i>=m;i--)
#define pb push_back
#define eb emplace_back
#define ppb pop_back
#define fi first
#define se second
#define mpa make_pair
const ll INF=1e18 ;
inline void chmax(ll& a,ll b){a=max(a,b);}
inline void chmin(ll& a,ll b){a=min(a,b);}
ll gcd(ll a, ll b) { return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) { return a/gcd(a,b)*b;}
#define set20 cout<<fixed<<setprecision(20) ;
// ミント
const ll mod = //1e9+7 ;
998244353;
struct mint {
ll x; // typedef long long ll;
mint(ll x=0):x((x%mod+mod)%mod){}
mint operator-() const { return mint(-x);}
mint& operator+=(const mint a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint a) {
if ((x += mod-a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
mint operator+(const mint a) const { return mint(*this) += a;}
mint operator-(const mint a) const { return mint(*this) -= a;}
mint operator*(const mint a) const { return mint(*this) *= a;}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t>>1);
a *= a;
if (t&1) a *= *this;
return a;
}
// for prime mod
mint inv() const { return pow(mod-2);}
mint& operator/=(const mint a) { return *this *= a.inv();}
mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
//昆布
struct combination {
vector<mint> fact, ifact;
combination(int n):fact(n+1),ifact(n+1) {
assert(n < mod);
fact[0] = 1;
for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
ifact[n] = fact[n].inv();
for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
}
mint operator()(int n, int k) {
if (k < 0 || k > n) return 0;
return fact[n]*ifact[k]*ifact[n-k];
}
mint p(int n,int k){
return fact[n]*ifact[n-k] ;
}
} ;
mint modpow(ll a,ll b){
if(b==0) return 1 ;
mint c= modpow(a,b/2) ;
if(b%2==1) return c*c*a ;
else return c*c ;
}
mint komb(ll n,ll m){
mint x=1 ;mint y=1 ;
rep(i,m){
x*= n-i ;
y*= i+1 ;
}
return x/y ;
}
map<ll,ll> factor(ll n){ //素因数とオーダーをマップで管理
map <ll,ll> ord ;
for(ll i=2;i*i<=n;i++){
if(n%i==0){
int res=0;
while(n%i==0){
n/=i;
res++;
}
ord[i]=res;
}
}
if(n!=1) ord[n]++;
return ord ;
}
///
template< typename T >
T extgcd(T a, T b, T &x, T &y) {
T d = a;
if(b != 0) {
d = extgcd(b, a % b, y, x);
y -= (a / b) * x;
} else {
x = 1;
y = 0;
}
return d;
}
int main(){
ios::sync_with_stdio(false) ;
cin.tie(nullptr) ;
vector<ll> cnt(1005) ;
vector<ll> dnt(1005) ;
ll n,m ; cin>>n>>m ;
rep(i,n){
ll x ; cin>>x ;
cnt[x]++ ;
}
rep(i,m){
ll x ; cin>>x ;
dnt[x]++ ;
}
rep(i,1005){
if(cnt[i]==0 && dnt[i]>0) cout<<i<<" " ;
else if(cnt[i]>0 && dnt[i]==0 ) cout<<i<<" " ;
}
cout<<endl ;
return 0 ;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll MOD = 1000000007;
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define REP2(i, x, n) for (ll i = x; i < (n); i++)
#define PR(x) cout << (x) << "\n"
#define PS(x) cout << (x) << " "
#define PRYES(x) PR((x) ? "Yes" : "No")
const long long INF = numeric_limits<long long>::max();
const long long DP_INF = 1 << 29;
template <class T> using arr = vector<vector<T> >;
using vint = vector<int>;
using vll = vector<ll>;
// SourceTest用出力マクロ
#ifdef SOURCE_TEST
#define SPR(x) PR(x)
#define SPS(x) PS(x)
#define STRACE(var) cout << #var << "=" << var << endl;
#define SMAPTRACE(var) \
cout << #var << "=" \
<< "{" << var.first << "," << var.second << "}" << endl;
#else
#define SPR(x) \
{}
#define SPS(x) \
{}
#define STRACE(var) \
{}
#define SMAPTRACE(var) \
{}
#endif
int main() {
ll N;
cin >> N;
ll left = 0;
for (ll i = 10;; i *= 10) {
if (N < (i * i)) break;
left = i;
}
ll right = max(left * 10, (ll)10);
ll mul = right;
left--;
right++;
auto funcIsOk = [&N, &mul](ll v) {
ll value = v * mul + v;
STRACE(value);
return value <= N;
};
/* どんな二分探索でもここの書き方を変えずにできる! */
while (right - left > 1) {
ll mid = left + (right - left) / 2;
STRACE(mid);
if (funcIsOk(mid)) {
SPR("OK");
left = mid;
} else {
SPR("NG");
right = mid;
}
}
STRACE(left);
STRACE(right);
PR(left);
return 0;
} | #pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<long double> vd;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<pair<ll,ll>> vp;
#define int ll
#define PB push_back
#define pb pop_back
#define in insert
#define endl "\n"
#define MP make_pair
#define f first
#define PI 3.141592653589
#define nd second
#define Trav(a,x) for(auto &a: x)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(), x.rend()
const int MOD = 998244353;
const int maxn = 2e6+1;
int a,b,c,d,e,k,n,m,t,s,q,x,y,ans;
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin>>n;
ans = 0;
for(int i = 1;i <= 999999 ; i++){
string s = to_string(i);
if(stoll(s+s) <= n)ans++;
}
cout<<ans;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
//using namespace atcoder;
struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
#define FOR(i, begin, end) for(int i=(begin);i<(end);i++)
#define REP(i, n) FOR(i,0,n)
#define IFOR(i, begin, end) for(int i=(end)-1;i>=(begin);i--)
#define IREP(i, n) IFOR(i,0,n)
#define Sort(v) sort(v.begin(), v.end())
#define Reverse(v) reverse(v.begin(), v.end())
#define all(v) v.begin(),v.end()
#define SZ(v) ((int)v.size())
#define Lower_bound(v, x) distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) distance(v.begin(), upper_bound(v.begin(), v.end(), x))
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define bit(n) (1LL<<(n))
#define debug(x) cout << #x << "=" << x << endl;
#define vdebug(v) { cout << #v << "=" << endl; REP(i_debug, v.size()){ cout << v[i_debug] << ","; } cout << endl; }
#define mdebug(m) { cout << #m << "=" << endl; REP(i_debug, m.size()){ REP(j_debug, m[i_debug].size()){ cout << m[i_debug][j_debug] << ","; } cout << endl;} }
#define pb push_back
#define fi first
#define se second
#define int long long
#define INF 1000000000000000000
template<typename T> istream &operator>>(istream &is, vector<T> &v){ for (auto &x : v) is >> x; return is; }
template<typename T> ostream &operator<<(ostream &os, vector<T> &v){ for(int i = 0; i < v.size(); i++) { cout << v[i]; if(i != v.size() - 1) cout << endl; }; return os; }
template<typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p){ cout << '(' << p.first << ',' << p.second << ')'; return os; }
template<typename T> void Out(T x) { cout << x << endl; }
template<typename T1, typename T2> void chOut(bool f, T1 y, T2 n) { if(f) Out(y); else Out(n); }
using vec = vector<int>;
using mat = vector<vec>;
using Pii = pair<int, int>;
using v_bool = vector<bool>;
using v_Pii = vector<Pii>;
//int dx[4] = {1,0,-1,0};
//int dy[4] = {0,1,0,-1};
//char d[4] = {'D','R','U','L'};
const int mod = 1000000007;
//const int mod = 998244353;
signed main(){
int B, C; cin >> B >> C;
int ans = 0;
if(B > 0){
//[-B,B]
if(C >= 2 * B) ans++;
ans += min(B, C / 2 + 1);
ans += min(B, (C - 1) / 2 + 1);
//(-INF,-B)
ans += (C - 1) / 2;
//(B,INF)
if(C >= 4) ans += (C - 4) / 2 + 1;
}else if(B < 0){
//(-INF,B]
ans += C / 2 + 1;
//[-B,INF)
ans += (C - 1) / 2 + 1;
//(B,-B)
if(C >= 2 * (-B) + 1) ans++;
ans += min(-B - 1, (C - 1) / 2);
if(C >= 2) ans += min(-B - 1, (C - 2) / 2);
}else if(B == 0){
ans += C / 2 + 1;
ans += (C - 1) / 2;
}
Out(ans);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main(void)
{
cin.tie(0);
ios::sync_with_stdio(false);
long long int B,C;
long long int res = 0;
vector <pair<long long int,long long int>> seg;
cin >> B >> C;
//1. 빼는 경우
long long int L = B - (C/2);
long long int R = B;
seg.push_back(make_pair(L,R));
//2. 빼고 나서 -1을 곱하는 경우
L = -B;
R = -(B - ((C-1)/2));
if(L<R)
{
seg.push_back(make_pair(L,R));
}
else
{
seg.push_back(make_pair(R,L));
}
//3. -1을 곱하고 빼는 경우
L = -B;
R = -B - ((C-1)/2);
if(L<R)
{
seg.push_back(make_pair(L,R));
}
else
{
seg.push_back(make_pair(R,L));
}
//4. -1을 곱하고 빼고 -1을 곱하는 경우
L = B;
R = B + ((C-2)/2);
if(L<R)
{
seg.push_back(make_pair(L,R));
}
else
{
seg.push_back(make_pair(R,L));
}
sort(seg.begin(),seg.end());
L = -2e18;
R = -2e18;
for(int i=0;i<seg.size();i++)
{
// cout << L << ' ' << R << ' ' << seg[i].first << ' ' << seg[i].second << '\n';
if(R < seg[i].first)
{
if(R!=-2e18)
{
res += (R-L+1);
}
L = seg[i].first;
R = seg[i].second;
}
else
{
R = max(R,seg[i].second);
}
}
if(R!=-2e18)
{
res += (R-L+1);
}
cout << res << '\n';
return 0;
} |
#ifdef LOGX
#define _GLIBCXX_DEBUG
#endif
#include <bits/extc++.h>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
/*---------macro---------*/
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)
#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
#define mybit(i,j) (((i)>>(j))&1)
/*---------type/const---------*/
typedef long long ll;
typedef unsigned long long ull;
typedef std::string::const_iterator state; //構文解析
const int big=1000000007;
//const int big=998244353;
const double EPS=1e-8; //適宜変える
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};
const char newl='\n';
struct{
constexpr operator int(){return -int(1e9)-10;}
constexpr operator ll(){return -ll(1e18)-10;}
}neginf;
struct{
constexpr operator int(){return int(1e9)+10;}
constexpr operator ll(){return ll(1e18)+10;}
constexpr auto operator -(){return neginf;}
}inf;
/*---------debug---------*/
#ifdef LOGX
#include <template/debug.hpp>
#else
#define dbg(...) ;
#define dbgnewl ;
#define prt(x) ;
#define _prt(x) ;
#endif
/*---------function---------*/
template<typename T> T max(const std::vector<T> &a){T ans=a[0];for(T elem:a){ans=max(ans,elem);}return ans;}
template<typename T> T min(const std::vector<T> &a){T ans=a[0];for(T elem:a){ans=min(ans,elem);}return ans;}
template<typename T,typename U> bool chmin(T &a,const U b){if(a>b){a=b;return true;}return false;}
template<typename T,typename U> bool chmax(T &a,const U b){if(a<b){a=b;return true;}return false;}
bool valid(int i,int j,int h,int w){return (i>=0 && j>=0 && i<h && j<w);}
template<class T,class U>T expm(T x,U y,const ll mod=big){T res=1;while(y){if(y&1)(res*=x)%=mod;(x*=x)%=mod;y>>=1;}return res;}
template<class T,class U>T exp(T x,U y){T res=1;while(y){if(y&1)res*=x;x*=x;y>>=1;}return res;}
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.precision(10);
/*------------------------------------*/
char a,b,c;cin >> a >> b >> c;
cout << b << c << a << newl;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fo(i, k, n) for(int i = k; i < n; i++)
#define deb(x) cout<<#x<<" = "<<x<<endl;
#define el endl
#define ff first
#define ss second
#define pb push_back
void solve(){
string s;
cin>>s;
for(int i = 1; i < s.length(); i++){
cout<<s[i];
}
cout<<s[0];
}
int main()
{
int t = 1;
// cin>>t;
while(t--){
solve();
cout<<endl;
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define Gene template< class
#define Rics printer& operator,
Gene c> struct rge{c b, e;};
Gene c> rge<c> range(c i, c j){ return {i, j};}
struct printer{
~printer(){cerr<<endl;}
Gene c >Rics(c x){ cerr<<boolalpha<<x; return *this;}
Rics(string x){cerr<<x;return *this;}
Gene c, class d >Rics(pair<c, d> x){ return *this,"(",x.first,", ",x.second,")";}
Gene ... d, Gene ...> class c >Rics(c<d...> x){ return *this, range(begin(x), end(x));}
Gene c >Rics(rge<c> x){
*this,"["; for(auto it = x.b; it != x.e; ++it)
*this,(it==x.b?"":", "),*it; return *this,"]";}
};
#define debug() cerr<<"LINE "<<__LINE__<<" >> ", printer()
#define dbg(x) "[",#x,": ",(x),"] "
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int my_rand(int l, int r) {
return uniform_int_distribution<int>(l, r) (rng);
}
const int N = 105;
int mod;
int dp[N][N*N*N];
int main() {
// freopen("in.txt", "r", stdin);
ios::sync_with_stdio(0);
cin.tie(0);
int n, K;
cin >> n >> K >> mod;
dp[0][0] = 1;
int sum = 0;
for(int i = 1; i <= n; i++) {
sum += K * i;
for(int s1 = 0; s1 < i; s1++) {
int ways = 0;
for(int s = s1; s <= sum; s += i) {
ways += dp[i-1][s];
if(ways >= mod) ways -= mod;
if(s-(K+1)*i >= 0) {
ways -= dp[i-1][s-(K+1)*i];
if(ways < 0) ways += mod;
}
dp[i][s] = ways;
// debug(), dbg(i), dbg(s), dbg(dp[i][s]);
}
}
}
for(int avg = 1; avg <= n; avg++) {
int ways = 0;
for(int s = 0; s <= sum; s++) {
ways += 1ll*dp[avg-1][s]*dp[n-avg][s]%mod;
if(ways >= mod) ways -= mod;
}
ways = 1ll*ways*(K+1)%mod;
ways--;
if(ways < 0) ways += mod;
cout << ways << "\n";
}
}
| #include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "/debug.h"
#else
#define db(...)
#endif
#define all(v) v.begin(), v.end()
#define pb push_back
using ll = long long;
const int NAX = 2e5 + 5, MOD = 1000000007;
ll dp_mask[1 << 16][17][17];
int64_t solveCase(int64_t H, int64_t W, int64_t A, int64_t B)
{
// TODO: edit here
memset(dp_mask, -1, sizeof dp_mask);
function<ll(int, int, int)> solve = [&](int mask, int a, int b) -> ll {
if (a < 0 || b < 0)
return 0;
int cnt = __builtin_popcount(mask);
if (cnt == H * W)
{
auto ret = a == 0 && b == 0;
db("basecase", bitset<16>(mask), a, b, ret, cnt);
return ret;
}
auto &ret = dp_mask[mask][a][b];
if (ret >= 0)
return ret;
ret = 0;
for (int i = 0; i < H; i++)
{
for (int j = 0; j < W; j++)
{
int curr = i * W + j;
if ((mask >> curr) & 1)
{
}
else
{
ret += solve(mask | (1 << curr), a - 1, b);
if (i + 1 < H)
{
int curr2 = (i + 1) * W + j;
if (mask & (1 << curr2))
;
else
ret += solve(mask | (1 << curr) | (1 << curr2), a, b - 1);
}
if (j + 1 < W)
{
int curr2 = i * W + j + 1;
if (mask & (1 << curr2))
;
else
ret += solve(mask | (1 << curr) | (1 << curr2), a, b - 1);
}
db("midloop", bitset<16>(mask), a, b, ret, cnt);
return ret;
}
}
}
db("endloop", bitset<16>(mask), a, b, ret, cnt);
return ret;
};
return solve(0, B, A);
}
// generated by oj-template v4.7.2 (https://github.com/online-judge-tools/template-generator)
int main()
{
#ifndef LOCAL
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
#endif
constexpr char endl = '\n';
int64_t H, W, A, B;
cin >> H >> W >> A >> B;
auto ans = solveCase(H, W, A, B);
cout << ans << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
inline int read(){
int res=0;
bool zf=0;
char c;
while(((c=getchar())<'0'||c>'9')&&c!='-');
if(c=='-')zf=1;
else res=c-'0';
while((c=getchar())>='0'&&c<='9')res=(res<<3)+(res<<1)+c-'0';
if(zf)return -res;
return res;
}
const int N=2e5+5;
int dta[N];
vector<int>G[N];
int ct,vi[N];
void dfs(int x){
vi[x]=1,++ct;
for(register int i=0;i<G[x].size();++i){
int v=G[x][i];
if(!vi[v]){
dfs(v);
}
}
return;
}
signed main(){
int n=read();
for(register int i=1;i<=n;++i){
dta[i]=read();
}
for(register int i=1;i<n-i+1;++i){
G[dta[i]].push_back(dta[n-i+1]),
G[dta[n-i+1]].push_back(dta[i]);
}
int A=0;
for(register int i=1;i<=200000;++i){
if(vi[i]){
continue;
}
ct=0;
dfs(i);
A+=ct-1;
}
printf("%d\n",A);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
using pii = pair<i64, i64>;
#define rep(i, n) for (i64 i = 0; i < i64(n); i++)
#define per(i, n) for (i64 i = n - 1; i >= 0; i--)
#define REP(i, a, b) for (i64 i = a; i < i64(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fi first
#define se second
#ifdef LOCAL
#define dbg(...) cerr<<__LINE__<<" ["<<#__VA_ARGS__<<"]:",debug(__VA_ARGS__)
#else
#define dbg(...)
#endif
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) {
os << '(' << pa.first << ',' << pa.second << ')'; return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
for (auto& x : vec) { os << x << ' '; } return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &mat) {
os << endl; for (auto& vec : mat) { os << vec << endl; } return os;
}
void debug() { cerr << endl; }
template <typename Head, typename... Tail> void debug(Head H, Tail... T) {
cerr << " " << H; debug(T...);
}
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; }
const int INF = (1 << 30) - 1;
const i64 LINF = (1LL << 62) - 1;
int n, m;
vector<int> G[20];
vector<int> color(20, -1);
i64 dfs(vector<int> &component, int pos = 0) {
if (pos == (int) component.size()) {
return 1;
}
i64 ret = 0;
int now = component[pos];
rep(i, 3) {
bool ok = 1;
for (auto& to : G[now]) {
if (color[to] == i) ok = 0;
}
color[now] = i;
if (ok) {
ret += dfs(component, pos + 1);
}
color[now] = -1;
}
return ret;
}
vector<vector<int>> components;
bool visited[20];
vector<int> bfs(int v) {
vector<int> ret;
visited[v] = 1;
queue<int> que;
ret.push_back(v);
que.push(v);
while (!que.empty()) {
int now = que.front();
que.pop();
for (int& to : G[now]) {
if (visited[to]) continue;
visited[to] = 1;
que.push(to);
ret.push_back(to);
}
}
return ret;
}
void solve() {
cin >> n >> m;
rep(i, m) {
int a, b;
cin >> a >> b;
a--; b--;
G[a].push_back(b);
G[b].push_back(a);
}
rep(i, n) {
if (!visited[i]) {
components.push_back(bfs(i));
}
}
i64 ans = 1;
for (auto &component : components) {
ans *= dfs(component);
}
cout << ans << endl;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1e+7;
const ld PI = acos(-1);
const ld EPS = 1e-8;
const ll LINF = 1LL<<60;
const int INF = 1LL<<17;
#define rep(i, a, b) for(ll i=a; i<(ll)(b); i++)
#define rrep(i, a, b) for(ll i=b-1; a<=i; i--)
#define ALL(x) x.begin(), x.end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
ll ceil(ll a, ll b) {if(a==0) return 0; return (a-1)/b+1;}
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
string YES[2] = {"NO", "YES"};
string yes[2] = {"No", "Yes"};
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<998244353>;
struct UnionFind{
vector<ll> par;
UnionFind(ll n) : par(n, -1){}
int root(int x){
if(par[x]<0) return x;
return par[x] = root(par[x]);
}
bool same(int a, int b){
return root(a)==root(b);
}
void unite(int a, int b){
int x=root(a), y=root(b);
if(same(x, y)) return;
if(y<x) swap(x, y);
par[x] += par[y];
par[y] = x;
}
int size(int x){
return -par[root(x)];
}
};
int main(){
ll n, k; cin >> n >> k;
vector<vector<ll>> g(n, vector<ll>(n));
rep(i, 0, n) rep(j, 0, n) cin >> g[i][j];
mint res=1;
rep(t, 0, 2){
UnionFind tree(n);
rep(i, 0, n){
rep(j, i+1, n){
bool flag=true;
rep(p, 0, n){
if(t){
if(g[i][p]+g[j][p]>k) flag = false;
}else{
if(g[p][i]+g[p][j]>k) flag = false;
}
}
if(flag) tree.unite(i, j);
}
}
set<ll> st;
rep(i, 0, n) st.insert(tree.root(i));
for(auto x: st){
mint sum = 1;
rep(i, 1, tree.size(x)+1) sum *= i;
res *= sum;
}
}
cout << res << endl;
}
| #include<bits/stdc++.h>
#define int long long
using namespace std;
struct edge{
int to,next;
}e[10000];
int n,m,a[30],head[30],cnt,ans=1,res,t,b[30];
bool vis[30];
inline void add(int u,int v){
cnt++;
e[cnt].to=v;
e[cnt].next=head[u];
head[u]=cnt;
}
inline void dfs1(int u){
for(int i=head[u];i;i=e[i].next){
int v=e[i].to;
if(vis[v])continue;
vis[v]=1;
b[++t]=v;
dfs1(v);
}
}
inline void dfs(int u,int s){
bool tag[4],flag=0;
memset(tag,0,sizeof(tag));
int sum=0;
for(int i=head[u];i;i=e[i].next){
int v=e[i].to;
if(a[v]&&!tag[a[v]])tag[a[v]]=1,sum++;
}
if(sum==3)return;
if(s==t){
a[u]=0;
for(int i=1;i<=3;i++)if(!tag[i])res++;
return;
}
for(int j=1;j<=3;j++)
if(!tag[j]){
flag=0;
a[u]=j;
for(int i=head[u];i;i=e[i].next){
int v=e[i].to;
if(a[v])continue;
flag=1;
dfs(v,s+1);
break;
}
if(!flag){
for(int i=1;i<=t;i++)
if(!a[b[i]]){
dfs(b[i],s+1);
break;
}
}
}
a[u]=0;
}
signed main(){
scanf("%lld%lld",&n,&m);
for(int i=1,x,y;i<=m;i++){
scanf("%lld%lld",&x,&y);
add(x,y);
add(y,x);
}
for(int i=1;i<=n;i++)
if(!vis[i]){
vis[i]=1;
t=1;
b[1]=i;
dfs1(i);
//cout<<t<<endl;
res=0;
dfs(i,1);
ans*=res;
}
printf("%lld",ans);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define io ios::sync_with_stdio(false); cin.tie(0)
const ll INF = 1L << 60;
struct edge { int to; ll cost; int k; };
struct graph{
int V;
vector<vector<edge>> G;
vector<ll> d;
graph(int n): V(n) {
G.resize(V);
d.resize(V);
rep(i, V) d[i] = INF;
}
void add_edge(int s, int t, ll cost, int k) {
edge e1, e2;
e1.to = t; e1.cost = cost; e1.k = k;
e2.to = s; e2.cost = cost; e2.k = k;
G[s].push_back(e1);
G[t].push_back(e2);
}
void dijkstra(int s) {
d[s] = 0;
priority_queue<P, vector<P>, greater<P>> que; // min-heap
que.push(P(0, s)); // d[i], i
while (!que.empty()) {
P p = que.top(); que.pop();
int v = p.second;
if (d[v] < p.first) continue;
for (auto e : G[v]) {
if (d[v] % e.k == 0) {
if (d[e.to] > d[v] + e.cost) {
d[e.to] = d[v] + e.cost;
que.push(P(d[e.to], e.to));
}
} else {
if (d[e.to] > d[v] + e.cost + e.k - (d[v] % e.k)) {
d[e.to] = d[v] + e.cost + e.k - (d[v] % e.k);
que.push(P(d[e.to], e.to));
}
}
}
}
}
};
int main()
{
io;
int n, m, x, y;
cin >> n >> m >> x >> y;
graph G(n);
rep(i, m) {
int a, b, t, k;
cin >> a >> b >> t >> k;
G.add_edge(a-1, b-1, t, k);
}
G.dijkstra(x-1);
if (G.d[y-1] == INF) cout << -1 << endl;
else cout << G.d[y-1] << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const long long inf = 1LL<<60;
using ll = long long;
int main(){
int n;
cin >> n;
string s, t;
cin >> s >> t;
auto solve = [&]() -> ll {
deque<ll> a, b;
for (int i=0; i<n; i++) {
if (s[i] == '1') a.push_back(i);
if (t[i] == '1') b.push_back(i);
}
if (a.size() < b.size() || (b.size() - a.size()) % 2 == 1) return -1;
auto twopop = [&]() -> ll {
ll res = a[1] - a[0];
a.pop_front(), a.pop_front();
return res;
};
ll res = 0;
for (auto id : b) {
while (!a.empty() && a[0] < id) {
if (a.size() < 2) return -1;
res += twopop();
}
if (a.empty()) return -1;
res += a[0] - id;
a.pop_front();
}
if (a.size() % 2 == 1) return -1;
while (!a.empty()) res += twopop();
return res;
};
cout << solve() << endl;
}
|
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<cstdlib>
#include<queue>
#include<set>
#include<cstdio>
#include<map>
#include<cassert>
using namespace std;
#define ll long long
#define reps(i, a, b) for(int i = a; i < b; i++)
#define rreps(i, a, b) for(int i = a-1; i >= b; i--)
#define rep(i, n) reps(i, 0, n)
#define rrep(i, n) rreps(i, n, 0)
#define P pair<int, int>
#define vec vector<int>
#define mat vector<vec>
const ll mod = 998244353;
const int INF = 1001001001;
struct mint {
ll x;
mint(ll x=0):x((x%mod+mod)%mod){}
mint operator-() const { return mint(-x);}
mint& operator+=(const mint a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint a) {
if ((x += mod-a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint a) const {
mint res(*this);
return res+=a;
}
mint operator-(const mint a) const {
mint res(*this);
return res-=a;
}
mint operator*(const mint a) const {
mint res(*this);
return res*=a;
}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t>>1);
a *= a;
if (t&1) a *= *this;
return a;
}
// for prime mod
mint inv() const {
return pow(mod-2);
}
mint& operator/=(const mint a) {
return (*this) *= a.inv();
}
mint operator/(const mint a) const {
mint res(*this);
return res/=a;
}
};
istream& operator>>(istream& is, const mint& a){ return is >> a.x; }
ostream& operator<<(ostream& os, const mint& a){ return os << a.x; }
int main(){
int h, w;
cin >> h >> w;
string s[h];
rep(i, h){
cin >> s[i];
}
mint ans(1);
rep(i, h){
set<char> t;
int x = i;
int y = 0;
while(x >= 0 && y < w){
t.insert(s[x][y]);
x--;
y++;
}
if(t.size() == 3){
ans *= 0;
}else if(t.size() == 2){
if(t.find('.') == t.end()){
ans *= 0;
}else{
ans *= 1;
}
}else{
if(t.find('.') == t.end()){
ans *= 1;
}else{
ans *= 2;
}
}
}
reps(i, 1, w){
set<char> t;
int x = h-1;
int y = i;
while(x >= 0 && y < w){
t.insert(s[x][y]);
x--;
y++;
}
if(t.size() == 3){
ans *= 0;
}else if(t.size() == 2){
if(t.find('.') == t.end()){
ans *= 0;
}else{
ans *= 1;
}
}else{
if(t.find('.') == t.end()){
ans *= 1;
}else{
ans *= 2;
}
}
}
cout << ans << endl;
} | #include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 510, mod = 998244353;
int n, m;
char a[N][N];
int c[2 * N];
int main(){
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i ++) scanf("%s", a[i] + 1);
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= m; j ++)
if(a[i][j] == 'R') c[i + j] |= 1;
else if(a[i][j] == 'B') c[i + j] |= 2;
int res = 1;
for(int i = 2; i <= n + m; i ++)
if(c[i] == 0) res = 2 * res % mod;
else if(c[i] == 3) res = 0;
printf("%d\n", res);
return 0;
} |
#include<bits/stdc++.h>
#include<iostream>
#include<math.h>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
int a,b,c;
cin>>a>>b>>c;
int arr[6];
for(int i=0;i<6;i++){
arr[i]=6-i;
}
int x=arr[a-1]+arr[b-1]+arr[c-1];
cout<<x;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main()
{
vector<int> a(3);
int sum(0);
for(auto &x:a)
{
cin>>x;
sum=sum+x;
}
cout<<(21-sum);
return 0;
}
|
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <vector>
#include <queue>
#include <random>
using namespace std;
const long long MAXT = 1000000007;
int a, b;
vector<int> res;
int main() {
cin >> a >> b;
int minx = min(a, b);
int maxx = max(a, b);
for (int i = 0;i < minx-1;i ++) {
res.push_back(i+1);
res.push_back(-(i+1));
}
int sum = 0;
for (int i = minx;i <= maxx;i ++) {
if (minx == a) {
res.push_back(-i);
sum += (-i);
} else {
res.push_back(i);
sum += i;
}
}
res.push_back(-sum);
for (int i = 0;i < res.size();i ++) {
if (i == 0) cout << res[i];
else cout << " " << res[i];
}
cout << endl;
return 0;
} | #define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <algorithm>
#include <climits>
#include <cstring>
#include <cmath>
#include <stack>
#include <iomanip>
#include <tuple>
#include <functional>
#include <cfloat>
#include <map>
#include <set>
#include <array>
#include <stdio.h>
#include <string.h>
#include <random>
#include <cassert>
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
using namespace std;
#define int long long
#define CONTAINS_VEC(v,n) (find((v).begin(), (v).end(), (n)) != (v).end())
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ARY_SORT(a, size) sort((a), (a)+(size))
#define REMOVE(v,a) (v.erase(remove((v).begin(), (v).end(), (a)), (v).end()))
#define REVERSE(v) (reverse((v).begin(), (v).end()))
#define ARY_REVERSE(v,a) (reverse((v), (v)+(a)))
#define REP(i, n) for (int (i)=0; (i) < (n); (i)++)
#define REPE(i, n) for (int (i)=0; (i) <= (n); (i)++)
#define CONTAINS_MAP(m, a) ((m).find((a)) != m.end())
#define CONTAINS_SET(m, a) ((m).find((a)) != m.end())
void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; exit(0); }
void Yes() { cout << "Yes" << endl; exit(0); }
void No() { cout << "No" << endl; exit(0); }
signed main()
{
int A, B;
cin >> A >> B;
bool is_swap = false;
if (A < B)
{
is_swap = true;
swap(A, B);
}
int sum_a = 0;
for (int i = 1; i <= A; i++)
{
sum_a += i;
}
int sum_b = 0;
for (int i = 1; i <= B; i++)
{
sum_b += i;
}
int fix = is_swap ? -1 : 1;
for (int i = 1; i <= A; i++)
{
cout << fix * i << " ";
}
for (int i = 1; i < B; i++)
{
cout << fix * -i << " ";
}
cout << fix * (-B - (sum_a - sum_b)) << " ";
cout << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=1;i<=n;++i)
#define mp make_pair
#define pb push_back
#define size sze
#define inf 998244353
int n,k;
vector<int> g[210000];
int v[210000],dep[210000];
int q[210000];
int f[210000][20],size[210000],son[210000],minp[210000];
int father[210000],root;
struct node
{
int adj,valid,next;
}edge[2*210000];
int gh[210000],top;
void addedge(int x, int y) {
edge[++top].adj = y;
edge[top].valid = 1;
edge[top].next = gh[x];
gh[x] = top;
}
void get_size(int x, int root=0) {
size[x] = 1; son[x] = 0;
int dd = 0;
for (int p=gh[x]; p; p=edge[p].next)
if (edge[p].adj != root && edge[p].valid) {
get_size(edge[p].adj, x);
size[x] += size[edge[p].adj];
if (size[edge[p].adj] > dd) {
dd = size[edge[p].adj];
son[x] = edge[p].adj;
}
}
}
int getroot(int x) {
get_size(x);
int sz = size[x];
while (size[son[x]] > sz/2)
x = son[x];
return x;
}
int dc(int x,int fa) {
x = getroot(x);
father[x]=fa;
for (int p=gh[x]; p; p=edge[p].next)
if (edge[p].valid) {
edge[p].valid = 0;
edge[p^1].valid = 0;
dc(edge[p].adj,x);
}
return x;
}
bool cmp(int x,int y)
{
return dep[x]>dep[y];
}
void dfs(int x,int pre,int depp)
{
f[x][0]=pre;
for(int i=1;i<=17;i++)f[x][i]=f[f[x][i-1]][i-1];
dep[x]=depp;
for(int p=gh[x];p;p=edge[p].next)
if (edge[p].adj!=pre)dfs(edge[p].adj,x,depp+1);
}
int jump(int x,int d)
{
int delta=dep[x]-d;
for(int i=17;i>=0;i--)
if (delta & (1<<i))x=f[x][i];
return x;
}
int lca(int x,int y)
{
if (dep[x]<dep[y])swap(x,y);
x=jump(x,dep[y]);
// cout<<x<<" "<<y<<endl;
if (x==y)return x;
for(int i=17;i>=0;i--)
if (f[x][i]!=f[y][i])
{
x=f[x][i];
y=f[y][i];
}
return f[x][0];
}
int dis(int x,int y)
{
return dep[x]+dep[y]-2*dep[lca(x,y)];
}
void insert(int x)
{
int now=x;
while (now!=0)
{
if (minp[now]==n+1 || dis(now,x)<dis(now,minp[now]))minp[now]=x;
now=father[now];
}
}
int get(int x)
{
int ret=2147483647;
int now=x;
while (now!=0)
{
if (minp[now]!=n+1)ret=min(ret,dis(x,minp[now]));
now=father[now];
}
return ret;
}
bool check(int lim)
{
for(int i=1;i<=n;i++)minp[i]=n+1;
int tot=0;
for(int i=1;i<=n;i++)
{
//cout<<q[i]<<endl;
if (get(q[i])>lim)
{
int fa=jump(q[i],max(dep[q[i]]-lim,0));
insert(fa);
tot++;
}
if (tot>k)return 0;
}
return 1;
}
int main()
{
cin>>n>>k; top=1;
for(int i=1;i<n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
addedge(x,y);
addedge(y,x);
}
dfs(1,0,0);
root=dc(1,0);
//cout<<lca(3,2)<<" "<<lca(3,5)<<" "<<lca(4,5)<<endl;
for(int i=1;i<=n;i++)q[i]=i;
sort(q+1,q+n+1,cmp);
long long l=1; long long r=n;
//for(int i=1;i<=n;i++)cout<<father[i]<<" "; puts("");
while (l<r)
{
int mid=(l+r)/2;
// cout<<mid<<endl;
if (check(mid))r=mid;
else l=mid+1;
}
cout<<l<<endl;
return 0;
} | //Code by oryon
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define ppb pop_back
#define int long long
#define vi vector<int>
#define vvi vector<vector<int>>
#define mii map<int, int>
#define pii pair<int, int>
#define sz(a) (a).size()
#define FOR(i, a, b) for(int i=a; i<b; i++)
#define ROF(i, a, b) for(int i=b-1; i>=a; i--)
#define all(x) (x).begin(), (x).end()
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define LB lower_bound
#define UB upper_bound
#define FS first
#define SD second
#define INF 1e18
#define MAX_NUM 300001
#define ps(x,y) fixed<<setprecision(y)<<x
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
const int MOD = 1e9 + 7;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
#define deb(x) cout << #x << " " << x << endl;
ll max(ll a, ll b)
{
if (a > b) return a;
return b;
}
ll min(ll a, ll b)
{
if (a < b) return a;
return b;
}
int n, m;
vvi graph(22); vi visited(22, 0), comp, color(22, 0);
void DFS(int s) {
visited[s] = 1;
comp.pb(s);
for (auto &c : graph[s]) if (!visited[c]) DFS(c);
}
int calc(int pos) {
if (pos == sz(comp)) return 1;
int ans = 0;
FOR(i, 1, 4) {
int skip = 0;
for (auto &c : graph[comp[pos]]) {
if (color[c] == i) {
skip = 1;
break;
}
}
if (skip) continue;
color[comp[pos]] = i;
ans += calc(pos + 1);
}
color[comp[pos]] = 0;
return ans;
}
int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
cin >> n >> m;
FOR(i, 0, m) {
int u, v;
cin >> u >> v;
graph[u].pb(v);
graph[v].pb(u);
}
int ans = 1;
FOR(i, 1, n + 1) {
if (!visited[i]) {
DFS(i);
ans *= calc(0);
comp.clear();
}
}
cout << ans;
return 0;
}
|
/* --------------------
| LOSER |
| ~NOOBOSS~ |
--------------------
*/
#include <bits/stdc++.h>
using namespace std;
#define mxx LLONG_MAX
#define mnn LLONG_MIN
#define Y() cout<< "YES" <<endl
#define N() cout << "NO"<<endl
#define endl "\n"
#define Ceil(x,y) ((x+y-1)/y)
#define sz(s) (int)s.size()
#define angle(x) double(x * acos(-1) / 180.0)
#define max_3(a,b,c) max(a, max(b,c))
#define min_3(a,b,c) min(a, min(b,c))
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/gcd(a,b)
#define loser return 0
#define ll long long
#define PI acos(-1)
#define mem(a,v) memset(a,v,sizeof(a))
#define all(v) v.begin(),v.end()
#define SORT(v) sort(v.begin(),v.end())
#define SRV(v) sort(v.rbegin(),v.rend())
#define REV(v) reverse(v.begin(),v.end())
#define B begin()
#define E end()
#define V vector
#define F first
#define S second
#define PSB push_back
#define MP make_pair
#define flash cout.flush()
#define InTheNameOfGod ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
constexpr ll MOD = 998244353;
constexpr ll mod = 1e9 + 7;
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};
/*-----*/
#define bug1(a) cerr<<a<<endl;
#define bug2(a,b) cerr<<a<<" "<<b<<endl;
#define bug3(a,b,c) cerr<<a<<" "<<b<<" "<<c<<endl;
/*----*/
const ll N=3e5+5;
vector<ll> adj[N];
ll power(ll n,ll p){if(p==0) return 1;if(p==1)return n;if(p%2)return power(n,p-1)*n;else{ll x=power(n,p/2);return x*x;}}
ll modpow(ll a,ll b,ll m){ll ans=1;while(b){if(b&1)ans=(ans*a)%m;b/=2;a=(a*a)%m;}return ans;}
ll nsum(ll num){return (num*(num+1))/2;}
void edge (ll u,ll v) {adj[u].PSB(v) ;adj[v].PSB(u);}
/*------------------START---------------------*/
/*-----*/
void solve(){
ll n;
string s;
cin>>n;
for(ll i=1;i<=38;i++){
ll bal=power(3,i);
ll baki=n-bal;
ll k=1;
while(1){
ll dhon=power(5,k);
if(dhon==baki){
cout<<i<<' '<<k<<endl;
return;
}
if(dhon>baki) break;
k++;
}
}
cout<<"-1"<<endl;
}
/*-----*/
int main(){
InTheNameOfGod
ll Test=1;
//cin>>Test;
while(Test--){
solve();
}
loser;
}
/////// C O D I N G I S L I F E ///////
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;//大きなint型
using P = pair<int,int>;//pair型の型エイリアス
#define rep(i,n) for (int i = 0; i < (n); ++i)//iをnまですすめる。
ll my_pow(ll x, ll n) {
if (n == 0) return 1;
if (n % 2 == 0) return my_pow(x * x, n / 2);
return x * my_pow(x, n - 1);
}
int main(){
ll n;
cin >> n;
for(int i=1;i<50;i++){
ll p = my_pow(3,i);
if(p>n) break;
for(int j=1;j<50;j++){
ll q = my_pow(5,j);
if(q>n) break;
if(n == p+q){
cout << i << " " << j << endl;
return 0;
}
}
}
cout << -1 << endl;
return 0;
} |
#include <bits/stdc++.h>
#include <cstdio>
using namespace std;
typedef long long int int64;
struct Edge {
int to;
};
using Graph = vector<vector<Edge>>;
vector<int> V[200010];
// 深さ優先探索
vector<bool> seen; // 探索したか記録
void dfs2(const Graph &GG, int v,int cnt) {
//printf("z");
seen[v] = true;
for (auto e : GG[v]) {
if (!seen[e.to]) { // 訪問済みでなければ探索
V[cnt].push_back(e.to);
dfs2(GG, e.to,cnt);
}
}
}
int64 start[200010]={0};
int64 goal[200010]={0};
int main() {
int n, m;
int flag=1;
cin >> n >> m;
for(int i=0;i<n;i++)
{
int64 x=0;
scanf("%lld",&x);
start[i]=x;
}
for(int j=0;j<n;j++)
{
int64 y=0;
scanf("%lld",&y);
goal[j]=y;
}
Graph G(n);
for (int i = 0; i < m; i++) {
//printf("a");
int a, b;
cin >> a >> b;
a--;
b--;
G[a].push_back({b});
G[b].push_back({a});
}
//printf("a");
seen.assign(n, false); // 初期化
int cnt = 0;
int64 size=0;
int64 asum=0;
int64 bsum=0;
for (int i = 0; i < n; i++) {
if (!seen[i]) {
//printf("a");
V[cnt].push_back(i);
dfs2(G, i,cnt);
size = V[cnt].size();
asum = 0;
bsum = 0;
for(int j=0;j<size;j++)
{
//printf("%d ",V[cnt][j]);
asum+=start[V[cnt][j]];
bsum+=goal[V[cnt][j]];
}
//printf("\nasum=%lld\n",asum);
//printf("bsum=%lld\n",bsum);
if(asum!=bsum)flag=0;
cnt++; // dfsした回数をカウント
}
}
//cout << cnt << endl;
//cout << flag << endl;
if(flag)printf("Yes");
else printf("No");
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef double dd;
#define tst int tttt;cin>>tttt;for(int TTTT=1;TTTT<=tttt;TTTT++)
#define nl cout<<"\n";
#define forn(a,b) for(int ii=a;ii<b;ii++)
const ll MOD1=1e9+7;
const ll MOD2=998244353;
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define all(a) a.begin(),a.end()
#define pii pair<int,int>
#define vi vector<int>
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define hhh cout<<"here"<<endl;
ll mod=MOD1;
ll po(ll x,ll y) {y%=mod;ll res=1;while(y>0){if(y&1)res=(res*x);x=(x*x);y>>=1;}return res;}
ll gcd(int a, int b){if(a<b) swap(a,b);if(b==0) return a;return gcd(a%b,b);}
void solve(){
int n,m;cin>>n>>m;
vi a(n+1),b(n+1);
forn(1,n+1)cin>>a[ii];
forn(1,n+1)cin>>b[ii];
vector<vi> adj(n+1);
forn(0,m){
int u,v;cin>>u>>v;
adj[u].pb(v);adj[v].pb(u);
}vector<bool> vis(n+1);
for(int i=1;i<=n;i++){
if(!vis[i]){
vis[i]=true;
ll p=0,q=0;
stack<int> st;st.push(i);
while(!st.empty()){
int u=st.top();st.pop();
p+=a[u];q+=b[u];
for(auto v:adj[u]){
if(!vis[v]){
vis[v]=true;
st.push(v);
}
}
}if(p!=q){
cout<<"No";return;
}
}
}cout<<"Yes";
}
int main(){
// fastio
// ++*(int*)0;// crash
// freopen("out.txt","r",stdin);freopen("outt.txt","w",stdout);
// tst
{
solve();nl;
}
}
/*
*/
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#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 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;
}
ll mypow(ll a, ll n) {
ll ret = 1;
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;
}
int main(){
ll n;
cin >> n;
ll b = 0;
ll p = 1;
ll ans = INF;
while(1){
ll a = n/p, c = n%p;
chmin(ans, a+b+c);
b++;
p *= 2;
if(p > n)break;
}
cout << ans << endl;
return 0;
} | #pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define NDEBUG
#define eprintf(...) do {} while (0)
#endif
#include<cassert>
using namespace std;
typedef long long LL;
typedef vector<int> VI;
#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)
template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }
template<class Iter> void rprintf(const char *fmt, Iter begin, Iter end) {
for (bool sp=0; begin!=end; ++begin) { if (sp) putchar(' '); else sp = true; printf(fmt, *begin); }
putchar('\n');
}
int N;
int A[111], B[111];
int cnt[211];
bool dp[211];
int AB[211], BA[211];
void MAIN() {
scanf("%d", &N);
REP (i, N) {
scanf("%d%d", A+i, B+i);
if (A[i] != -1) A[i]--;
if (B[i] != -1) B[i]--;
}
bool yes = true;
REP (i, N) {
if (A[i] == 2*N-1) yes = false;
if (B[i] == 0) yes = false;
if (A[i] != -1 && B[i] != -1 && B[i] <= A[i]) yes = false;
if (A[i] != -1) cnt[A[i]]++;
if (B[i] != -1) cnt[B[i]]++;
}
if (yes) {
REP (i, 2*N) {
if (cnt[i] >= 2) yes = false;
}
}
if (yes) {
REP (i, N*2) {
AB[i] = -2;
BA[i] = -2;
}
REP (i, N) {
if (A[i] != -1) AB[A[i]] = B[i];
if (B[i] != -1) BA[B[i]] = A[i];
}
dp[0] = true;
for (int i=0; i<N*2; i+=2) if (dp[i]) {
for (int h=1; i+h+h<=N*2; h++) {
bool ok = true;
for (int k=i; ok && k<i+h; k++) {
if (AB[k] == -1 && BA[k+h] != -2) ok = false;
if (BA[k+h] == -1 && AB[k] != -2) ok = false;
if (0 <= AB[k] && AB[k] != k+h) ok = false;
if (0 <= BA[k]) ok = false;
if (0 <= BA[k+h] && BA[k+h] != k) ok = false;
if (0 <= AB[k+h]) ok = false;
}
if (ok) dp[i+h+h] = true;
}
}
yes = dp[N*2];
}
puts(yes? "Yes": "No");
}
int main() {
int TC = 1;
// scanf("%d", &TC);
REP (tc, TC) MAIN();
return 0;
}
|
#include <bits/stdc++.h>
template <typename T>
T next() {
T temp; std::cin >> temp;
return temp;
}
template <>
int next() {
int temp; scanf("%d", &temp);
return temp;
}
template <>
long long next() {
long long temp; scanf("%lld", &temp);
return temp;
}
template <>
double next() {
double temp; scanf("%lf", &temp);
return temp;
}
template <>
float next() {
float temp; scanf("%f", &temp);
return temp;
}
template <typename T1, typename T2>
std::pair<T1, T2> next() {
std::pair<T1, T2> temp;
temp.first = next<T1>();
temp.second = next<T2>();
return temp;
}
template <typename T>
std::vector<T> next(int n) {
std::vector<T> temp(n);
for(int i = 0; i < n; i++)
temp[i] = next<T>();
return temp;
}
template <typename T1, typename T2>
std::vector<std::pair<T1, T2>> next(int n) {
std::vector<std::pair<T1, T2>> temp(n);
for(int i = 0; i < n; i++)
temp[i] = next<T1, T2>();
return temp;
}
template <typename T>
void print(const T &x, char endc = '\n') {
std::cout << x << endc;
return;
}
void print(const int &x, char endc = '\n') {
printf("%d%c", x, endc);
return;
}
void print(const long long &x, char endc = '\n') {
printf("%lld%c", x, endc);
return;
}
void print(const size_t &x, char endc = '\n') {
printf("%zu%c", x, endc);
return;
}
void print(const float &x, char endc = '\n') {
printf("%f%c", x, endc);
return;
}
void print(const double &x, char endc = '\n') {
printf("%lf%c", x, endc);
return;
}
template<typename T1, typename T2>
void print(const std::pair<T1, T2> &p, char sepc = ' ', char endc = '\n') {
print(p.first, sepc);
print(p.second, endc);
return;
}
template<typename T>
void print(const std::vector<T> &v, char sepc = ' ', char endc = '\n') {
for(const T &e : v) print(e, sepc);
printf("%c", endc);
return;
}
template<typename T>
class CountMap {
private:
std::map<T, int> _cntmap;
int _totalcnt = 0;
public:
CountMap() {}
CountMap(const std::vector<T> &v) {
for(const T &x : v)
_cntmap[x]++;
_totalcnt = v.size();
}
void add(const T &x) {
_cntmap[x]++;
_totalcnt++;
}
void remove(const T &x) {
if(--_cntmap[x] == 0) {
_cntmap.erase(x);
}
_totalcnt--;
}
int count() const {
return _totalcnt;
}
T minElement() const {
return _cntmap.begin()->first;
}
T maxElement() const {
return _cntmap.rbegin()->first;
}
bool empty() const {
return _cntmap.empty();
}
std::vector<T> getElements() const {
std::vector<T> ret;
for(auto it = _cntmap.begin(); it != _cntmap.end(); it++)
ret.push_back(it->first);
return ret;
}
int operator[] (const T &x) const {
auto it = _cntmap.find(x);
if(it == _cntmap.end()) return 0;
return it->second;
}
};
using namespace std;
const int mod = 998244353;
void eachTC() {
int N = next<int>();
vector<long long> A = next<long long>(N);
if(N == 1) {
print(A[0] * A[0] % mod);
return;
}
sort(A.begin(), A.end());
vector<long long> multiplier(N);
multiplier[N-1] = A[N-1];
for(int i = N-2; i >= 0; i--)
multiplier[i] = (2 * multiplier[i+1] - A[i+1] + A[i] + mod) % mod;
long long res = 0;
for(int i = 0; i < N; i++)
res = (res + (A[i] * multiplier[i]) % mod) % mod;
print(res);
}
int main() {
constexpr bool multipleTC = false;
int T = multipleTC ? next<int>() : 1;
while(T--) eachTC();
return 0;
}
| // #pragma GCC optimize ("O3")
#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 <class T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <class key, class value, class cmp = std::less<key>>
using ordered_map = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
// find_by_order(k) returns iterator to kth element starting from 0;
// order_of_key(k) returns count of elements strictly smaller than k;
// Defines
#define pb push_back
#define ff first
#define ss second
#define quick \
ios::sync_with_stdio(false); \
cin.tie(0);
#define time cerr << (0.1 * clock()) / CLOCKS_PER_SEC << endl;
#define int long long
typedef pair<int, int> pl;
#define forn(n) for (int i = 0; i < n; i++)
#define endl "\n"
#define all(v) (v).begin(), (v).end()
#define ppc __builtin_popcount
#define ppcll __builtin_popcountll
int power(int a, int n, int md) {
if (n == 0) {
return 1;
} else {
int res = power(a, n / 2, md);
res = (res * res) % md;
if (n % 2 != 0) {
res = (res * a) % md;
}
return res;
}
}
random_device rndm;
mt19937 grndm(rndm());
void mix(int* a, int* b) { shuffle(a, b, grndm); }
// Constants
const int mod = 1000000007;
// Code begins
void solve() {
double a, b;
cin >> a >> b;
double ans = a*b/100;
cout << ans << '\n';
}
signed main() {
quick;
solve();
// int t;
// cin >> t;
// while (t--) {
// solve();
// }
} |
#include <cassert>
#include <cstdint>
#include <iostream>
#include <vector>
#define REP_(i, a_, b_, a, b, ...) \
for (int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
using namespace std;
using Int = int64_t;
template <class T>
struct Matrix {
vector<vector<T>> val;
Matrix(vector<vector<T>> vec) : val(vec) {}
Matrix(int64_t n = 1, int64_t m = 1, T v = 0) : val(n, vector<T>(m, v)) {}
void init(int64_t n, int64_t m, T v = 0) { val.assign(n, vector<T>(m, v)); }
size_t size() const { return val.size(); }
vector<T> &operator[](int64_t i) { return val[i]; }
const vector<T> &operator[](int64_t i) const { return val[i]; }
};
template <class T>
Matrix<T> operator*(const Matrix<T> &A, const Matrix<T> &B) {
assert(A.size() == B[0].size());
Matrix<T> R(A.size(), B[0].size());
for (int64_t i = 0; i < A.size(); ++i)
for (int64_t j = 0; j < B[0].size(); ++j)
for (int64_t k = 0; k < B.size(); ++k) R[i][j] += A[i][k] * B[k][j];
return R;
}
template <class T>
Matrix<T> operator+(const Matrix<T> &A, const Matrix<T> &B) {
assert(A.size() == B.size() && A[0].size() == B[0].size());
Matrix<T> R(A.size(), A[0].size());
for (int64_t i = 0; i < A.size(); ++i) {
for (int64_t j = 0; j < A[0].size(); ++j) {
R[i][j] = A[i][j] + B[i][j];
}
}
return R;
}
template <class T>
Matrix<T> operator-(const Matrix<T> &A, const Matrix<T> &B) {
assert(A.size() == B.size() && A[0].size() == B[0].size());
Matrix<T> R(A.size(), A[0].size());
for (int64_t i = 0; i < A.size(); ++i) {
for (int64_t j = 0; j < A[0].size(); ++j) {
R[i][j] = A[i][j] - B[i][j];
}
}
return R;
}
template <class T>
vector<T> operator*(const Matrix<T> &A, const vector<T> &B) {
vector<T> v(A.size());
for (int64_t i = 0; i < A.size(); ++i) {
for (int64_t k = 0; k < B.size(); ++k) {
v[i] += A[i][k] * B[k];
}
}
return v;
}
template <class T>
bool operator==(const Matrix<T> &A, const Matrix<T> &B) {
assert(A.size() == B.size() && A[0].size() == B[0].size());
for (int64_t i = 0; i < A.size(); ++i) {
for (int64_t j = 0; j < A[0].size(); ++j) {
if (A[i][j] != B[i][j]) {
return false;
}
}
}
return true;
}
int main() {
Int N;
cin >> N;
vector<Int> X(N), Y(N);
REP(i, N) cin >> X[i] >> Y[i];
Int M;
cin >> M;
vector<Matrix<Int>> mats;
Matrix<Int> mat(3, 3, 0);
mat[0][0] = 1;
mat[1][1] = 1;
mat[2][2] = 1;
mats.push_back(mat);
REP(i, M) {
Int t;
cin >> t;
if (t == 1) {
vector<vector<Int>> a = {{0, 1, 0}, {-1, 0, 0}, {0, 0, 1}};
Matrix<Int> A(a);
mat = A * mat;
} else if (t == 2) {
vector<vector<Int>> a = {{0, -1, 0}, {1, 0, 0}, {0, 0, 1}};
Matrix<Int> A(a);
mat = A * mat;
} else if (t == 3) {
Int p;
cin >> p;
vector<vector<Int>> a = {{-1, 0, 2 * p}, {0, 1, 0}, {0, 0, 1}};
Matrix<Int> A(a);
mat = A * mat;
} else {
Int p;
cin >> p;
vector<vector<Int>> a = {{1, 0, 0}, {0, -1, 2 * p}, {0, 0, 1}};
Matrix<Int> A(a);
mat = A * mat;
}
mats.push_back(mat);
}
Int Q;
cin >> Q;
REP(i, Q) {
Int a, b;
cin >> a >> b;
--b;
vector<Int> vec = {X[b], Y[b], 1};
auto res = mats[a] * vec;
cout << res[0] << " " << res[1] << endl;
}
} | #include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mod 1000000007
#define pdd pair<double,double>
#define pii pair<ll,ll>
#define fi first
#define sec second
#define MAXN 1000000000000000005
#define N 300005
#define lgn 20
using namespace std;
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cout << name << " : " << arg1 << std::endl;
//use cerr if u want to display at the bottom
}
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 trace(...)
#endif
ll n,q,a[300001];
ll bit[3000000];
void update(ll x,ll y) {
// ll org = a[x];
a[x] = ((a[x])^y);
while(x<=n) {
bit[x] = ((bit[x])^y);
x+=(x&(-x));
}
return;
}
ll query(ll x) {
ll ret = 0;
while(x) {
ret = (ret^(bit[x]));
x-=(x&(-x));
}
return ret;
}
ll query(ll x,ll y) {
return (query(x-1)^query(y));
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
//Type Your Code Here
cin>>n>>q;
for(ll i=1;i<=n;i++) {
ll p;
cin>>p;
update(i,p);
}
while(q--) {
ll t,x,y;
cin>>t>>x>>y;
if(t==1) {
update(x,y);
}
else {
cout<<query(x,y)<<"\n";
}
}
// for(ll i=1;i<=10;i++) cout<<a[i]<<" ";
// cout<<"\n";
// for(ll i=1;i<=10;i++) cout<<bit[i]<<" ";
return 0;
} |
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int,int>
#define PIL pair<int,LL>
#define PLI pair<LL,int>
#define PIII pair<int,PII>
#define PLL pair<LL,LL>
#define PLII pair<LL,PII>
#define VI vector<int>
#define VVI vector<VI>
#define VL vector<LL>
#define VVL vector<VL>
#define VPII vector<PII>
#define FF first
#define SS second
#define MP make_pair
#define PB push_back
#define all(x) x.begin(),x.end()
#define watch(x) cout<<(#x)<<" = "<<(x)<<'\n'
#define mset(a,v) memset(a,v,sizeof(a))
#define setp(x) cout<<fixed<<setprecision(x)
#define EPS 0.00000000001
#define PI acos(-1)
#define loop(i,b,n) for(int i=b;i<n;++i)
#define rev_loop(i,b,n) for(int i=b;i>=n;--i)
using namespace std;
const int MOD = 1e9 + 7;
const LL MX = 1e9;
const LL INF = 1e9;
int main()
{
//ofstream out("output.txt");
//ifstream in("input.txt");
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n;
cin>>n;
LL a[n], b[n];
loop(i,0,n) cin>>a[i];
loop(i,0,n) cin>>b[i];
LL mx = 0, ans = 0;
loop(i,0,n)
{
mx = max(mx, a[i]);
b[i] *= mx;
ans = max(ans, b[i]);
cout<<ans<<'\n';
}
return 0;
}
| //#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i=0; i<n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using ll = int64_t;
using ull = uint64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template<class T> using PQ = priority_queue<T>;
template<class T> using PQG = priority_queue<T, vector<T>, greater<T>>;
const int INF = 0xccccccc;
const ll LINF = 0xcccccccccccccccLL;
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);}
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);}
template<typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;}
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;}
//head
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int r, c;
cin >> r >> c;
vector<vector<P>> G(r*c*2);
auto num = [&](int i, int j) {
return i*c + j;
};
rep(i, r) rep(j, c-1) {
int a;
cin >> a;
G[num(i, j)].emplace_back(num(i, j+1), a);
G[num(i, j+1)].emplace_back(num(i, j), a);
}
rep(i, r-1) rep(j, c) {
int b;
cin >> b;
G[num(i, j)].emplace_back(num(i+1, j), b);
G[num(i+1, j)].emplace_back(num(i+1, j)+r*c, 1);
G[num(i, j)+r*c].emplace_back(num(i, j), 0);
G[num(i+1, j)+r*c].emplace_back(num(i, j)+r*c, 1);
}
PQG<P> q;
q.emplace(0, 0);
vi dist(r*c*2, INF);
dist[0] = 0;
while(not q.empty()) {
auto [d, now] = q.top();
q.pop();
if(d != dist[now]) continue;
for(auto [ne, c]:G[now]) {
if(chmin(dist[ne], d+c)) {
q.emplace(d+c, ne);
}
}
}
cout << dist[r*c-1] << endl;
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<long long, long long>;
constexpr char ln = '\n';
constexpr long long MOD = 1000000007;
constexpr long long INF = 1000000000 + 100;
constexpr long long LINF = 1000000000000000000 + 100;
#define all(v) v.begin(), v.end()
#define rep(i, n) for(int i=0;i<(n);i++)
#define rept(i, j, n) for(int i=(j); i<(n); i++)
#define rrep(i, n) for(int i=(n); i>=0; i--)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
struct edge{ll to, time, cost;};
struct graph{
ll V;
vector<vector<edge> > G;
vector<ll> d;
vector<ll> prev;
graph(ll n){
init(n);
}
void init(ll n){
V = n;
G.resize(V);
d.resize(V);
rep(i,V){
d[i] = LINF;
}
}
void add_edge(ll s, ll t, ll cost, ll time){
edge e;
e.to = t, e.time = time, e.cost = cost;
G[s].push_back(e);
}
void dijkstra(ll s){
rep(i,V){
d[i] = LINF;
}
d[s] = 0;
priority_queue<pll,vector<pll>, greater<pll> > que;
que.push(pll(0,s)); //[その頂点への最短コスト候補、頂点番号]
prev.assign(V, -1);
while(!que.empty()){
pll p = que.top(); que.pop();
ll v = p.second;
if(d[v]<p.first) continue;
for(auto e : G[v]){
ll ncost = d[v];
if(ncost % e.time){
ncost += e.time - (ncost % e.time);
}
ncost += e.cost;
if(d[e.to]>ncost){
d[e.to] = ncost;
prev[e.to] = v;
que.push(pll(d[e.to],e.to));
}
}
}
}
vector<int> get_path(int t){ //頂点tへの最短路
vector<int> path;
for(; t != -1;t=prev[t]){
path.push_back(t);
}
reverse(path.begin(), path.end());
return path;
}
};
int main(){
int N, M, X, Y; cin >> N >> M >> X >> Y; X--, Y--;
graph g(N);
rep(i, M){
int a, b; ll t, k; cin >> a >> b >> t >> k; a--, b--;
g.add_edge(a,b,t,k);
g.add_edge(b,a,t,k);
}
g.dijkstra(X);
cout << (g.d[Y] == LINF ? -1 : g.d[Y]) << ln;
}
|
#include<bits/stdc++.h>
using namespace std;
#define int long long
inline int read(){
int x = 0,f = 1;
char c = getchar();
while(c < '0' || c > '9'){
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9'){
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
const int maxn = 505;
int a[maxn][maxn];
int aa[maxn],bb[maxn];
signed main()
{
int n; n = read();
for(int i=1;i<=n;++i) for(int j=1;j<=n;++j) a[i][j] = read();
int akk = 1e9,pos = 0;
for(int i=1;i<=n;++i){
if(a[i][1] < akk){
pos = i;
akk = a[i][1];//Au,冲啊!!
}
}
for(int i=1;i<=n;++i) bb[i] = a[pos][i];
for(int i=1;i<=n;++i) aa[i] = a[i][1] - a[pos][1];//差不多吧
for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j){
if(a[i][j] != aa[i] + bb[j]){
printf("No\n");
return 0;
}
}
}
printf("Yes\n");
for(int i=1;i<=n;++i) printf("%lld ",aa[i]);
printf("\n");
for(int i=1;i<=n;++i) printf("%lld ",bb[i]);
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,m;
int main()
{
cin>>n>>m;
vector<int> v(m+2);
v[0] = 0;
v[m+1] = n+1;
for(int i=1;i<=m;i++){
cin>>v[i];
}
sort(v.begin(),v.end());
int mm = n;
for(int i=1;i<m+2;i++){
int d = v[i]-v[i-1];
if(d>1){
mm = min(d-1,mm);
}
}
int res = 0;
for(int i=1;i<m+2;i++){
int d = v[i]-v[i-1];
if(d>1){
res += (d-1+mm-1)/mm;
}
}
cout<<res<<endl;
return 0;
}
| #include <bits/stdc++.h>
#define arr(i,n) for(ll i=0;i<n;i++)
#define ARR(i,n) for(ll i=n-1;i>=0;i--)
#define f(i,a,b) for(ll i=a;i<b;i++)
#define F(i,a,b) for(ll i=b-1;i>=a;i--)
#define float double
#define PI 3.14159265358979323846
#define pb push_back
#define mp make_pair
#define vi vector<ll>
#define ceil(a,b) ceil(a*1.0/b)
#define vpii vector< pair<ll,ll> >
#define debug(x) cout<<(#x)<<" is "<<(x)<<endl
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define all(v) v.begin(),v.end()
#define inputarr(a,n) for(ll i=0;i<n;++i) cin>>a[i]
#define integer_to_string to_string
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
typedef long long ll;
using namespace std;
const ll mod = 1000000000 + 7;
int main()
{
IOS;
//code start here
ll n;
cin>>n;
vi v;
ll x,y;
ll sum=0,sum1=0,res=0;
arr(i,n)
{
cin>>x>>y;
sum+=x;
v.pb(2*x+y);
}
sort(all(v));
ARR(i,v.size())
{
sum1+=v[i];
res++;
if(sum1>sum) break;
}
cout<<res;
return 0;
}
/*freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);*/
/*For graph
vector< pair<ll,ll> >graph={{0,1},{1,0},{0,-1},{-1,0}}; //4 direction
vector< pair<ll,ll> >graph={{0,1},{1,0},{0,-1},{-1,0}
{1,1}{1,-1}{-1,1}{-1,-1}}; //8 direction
int check(int i,int j,int n,int m) //for checking valid or not
{
return (i>=0&&i<n&&j>=0&&j<m);
}
*/ |
#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
#define rep(i,j,n) for(int i=j;i<n;i++)
#define scn(a) scanf("%lld",&a)
#define pri(a) printf("%lld",a)
#define prinl(a) printf("%lld\n",a)
#define vec vector<int>
#define pairs pair<int,int>
#define srt(v) sort(v.rbegin(),v.rend())
#define mem(a,b) memset(a,b,sizeof(a))
#define read(v,w) vec v(w,0); rep(i,0,w) {cin>>v[i];}
#define MOD 1000000007
#define MOD1 998244353
#define INF 1e18
#define nline "\n"
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define ff first
#define ss second
#define PI 3.141592653589793238462
#define set_bits __builtin_popcountll
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
// typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update > pbds; // find_by_order, order_of_key
#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(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.ff); cerr << ","; _print(p.ss); 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 << "]";}
//======================== SOLUTION ===============
//code
int getValue(string s) {
int ans = 0;
debug(s);
reverse(all(s));
int mul = 1;
int n = s.length();
rep(i,0,n) {
ans += (s[i] - '0')*mul;
mul *= 10;
}
debug(ans);
return ans;
}
void solve() {
int n,k;
cin >> n >> k;
while(k--) {
debug(n);
if(n % 200 == 0) {
n = n/200;
} else {
string s = to_string(n);
s += "200";
n = getValue(s);
}
}
cout << n;
}
///======== check for test cases =====================
signed main() {
#ifndef ONLINE_JUDGE
freopen("Error.txt", "w", stderr);
#endif
int test_case = 1;
//cin >> test_case;
while(test_case){
test_case--;
solve();
}
}
| #include<bits/stdc++.h>
using namespace std;
#define dd double
#define ll long long int
#define ull unsigned long long
#define lld long double
#define light ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define mkp make_pair
#define pi pair<ll,ll>
#define pii pair<ll,pi>
#define fi first
#define sc second
ll INF=1e9+5;
ll mod=998244353;
#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(dd 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.f); cerr << ","; _print(p.s); 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 << "]";}
int main()
{
// #ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// #endif
#ifndef ONLINE_JUDGE
freopen("Error.txt", "w", stderr);
#endif
light;
ll n,k;
cin>>n>>k;
while(k--){
if(n%200){
n=n*1000+200;
}
else{
n/=200;
}
}
cout<<n<<"\n";
} |
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define frfr freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout)
#define ll long long int
#define wh int T;cin >> T;while(T--)
#define foro(i,n) for(int i=0;i<n;i++)
void solve(){
int n;
cin >> n;
ll t = 0;
int a[n];
int b[n];
foro(i,n){
cin >> a[i];
}
foro(i,n){
cin >> b[i];
}
foro(i,n){
t += a[i]*b[i];
}
if(t == 0){
cout << "Yes" << endl;
}else{
cout << "No" << endl;
}
}
int main()
{
//IOS;
//wh{
solve();
//}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
template<typename T> using vec = vector<T>;
template<typename K, typename V> using umap = unordered_map<K, V>;
using ll = long long;
using ld = long double;
using str = string;
using pll = pair<ll, ll>;
using vll = vec<ll>;
using vi = vec<int>;
using vs = vec<str>;
using mapll = map<ll, ll>;
using umapll = umap<ll, ll>;
#define _rep(i, n) _repi(i, 0, n)
#define _repi(i, l, r) for (ll i = ll(l); i < ll(r); i++)
#define _get_rep(_1, _2, _3, NAME, ...) NAME
#define rep(...) _get_rep(__VA_ARGS__, _repi, _rep) (__VA_ARGS__)
#define rrep(i, n) for (ll i = ll(n) - 1; 0 <= i; i--)
#define each(v) for (auto v)
#define all(x) x.begin(), x.end()
#define mkpair make_pair
#define is_in(x, l, r) ((l) <= (x) && (x) < (r))
#define read(t, ...) t __VA_ARGS__; isin(cin, __VA_ARGS__)
#define readv(t, v, n) vec<t> v(n); isin(cin, v)
#define readv2(t, v, h, w) vec<vec<t>> v(h, vec<t>(w)); isin(cin, v)
#define readvv(t, va, vb, n) vec<t> va(n), vb(n); rep (i, n) { isin(cin, va[i], vb[i]); }
#define readvvv(t, va, vb, vc, n) vec<t> va(n), vb(n), vc(n); rep (i, n) { isin(cin, va[i], vb[i], vc[i]); }
#define print(...) osout(cout, __VA_ARGS__)
#define dbg(...) osout(cerr, __VA_ARGS__)
const ll inf = numeric_limits<ll>::max() / 2;
template<typename T> bool chmax(T &x, const T &y) { if (x < y) { x = y; return 1; } return 0; }
template<typename T> bool chmin(T &x, const T &y) { if (x > y) { x = y; return 1; } return 0; }
template<typename T> void sort(vec<T> &v) { sort(all(v)); }
template<typename T> void rsort(vec<T> &v) { sort(all(v), greater<T>()); }
template<typename T> void dec(vec<T> &v) { rep (i, v.size()) v[i]--; }
template<typename T> istream& operator >>(istream &is, vec<T> &v) {
rep (i, v.size()) { cin >> v[i]; } return is;
}
template<typename T1, typename T2> ostream& operator <<(ostream &os, pair<T1, T2> p) {
if (&os == &cout) { os << p.first << ' ' << p.second; }
if (&os == &cerr) { os << '(' << p.first << ", " << p.second << ')'; }
return os;
}
template<typename T> ostream& operator <<(ostream &os, vec<T> v) {
if (&os == &cout) { rep(i, v.size() - 1) { os << v[i] << ' '; } os << v[v.size() - 1]; }
if (&os == &cerr) { os << '['; rep(i, v.size() - 1) { os << v[i] << ", "; } os << v[v.size() - 1] << ']'; }
return os;
}
void isin(istream &_) {}
template<class S, class... T> void isin(istream &is, S &s, T&... t) {
is >> s; isin(is, t...);
}
void osout(ostream& os) { os << endl; }
template<class S, class... T> void osout(ostream &os, S s, T... t) {
os << s; if (sizeof...(t)) os << ' '; osout(os, t...);
}
void solve();
int main() {
#define PRECISION 0
cin.tie(0);
ios::sync_with_stdio(false);
if (PRECISION) {
cout << fixed << setprecision(15);
cerr << fixed << setprecision(15);
}
solve();
return 0;
}
void solve() {
read(ll,n);
readv(ll,a,n);
readv(ll,b,n);
ll t=0;
rep(i,n)t+=a[i]*b[i];
print(t==0?"Yes":"No");
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main(){
int a,b,c;
cin >> a >> b >> c;
if(a*a + b*b < c*c) cout << "Yes" << endl;
else cout << "No" << endl;
return 0;
} | #include <ctime>
#include <deque>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <climits>
#include <cmath>
#include <sstream>
#include <iomanip>
#include <map>
#include <stack>
#include <regex>
#include <set>
#include <bitset>
const int64_t infl = 1LL << 60;
const int64_t cc = pow(10, 9) + 7;
bool sort_pair(std::pair<int64_t, std::string> origin, std::pair<int64_t, std::string> another){return origin.first > another.first;}
template <typename T> bool chmin(T &a, const T& b) {if(a > b){a = b;return true;}return false;}
template <typename T> bool chmax(T &a, const T& b) {if(a < b) {a = b;return true;}return false;}
std::vector<std::string> list;
int main()
{
//std::vector< std::pair< int64_t, int64_t > > list;
std::vector< std::string > str_list;
std::string str1;
std::string str2;
int64_t n,k,l,a,b,c;
std::cin>> a;
std::cin>> b;
std::cin>> c;
if( pow( a, 2 ) + pow( b, 2 ) < pow( c, 2 ) )
{
std::cout<< "Yes";
}
else
{
std::cout<< "No";
}
}
|
#include <bits/stdc++.h>
#define ll long long
#define vll vector<ll>
#define vpp vector<pair<pair<ll, ll>, ll>>
#define vp vector<pair<ll, ll>>
#define mk make_pair
#define pb push_back
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define N 1000000007
using namespace std;
void fun()
{
// code begins
ll n;cin>>n;
ll a,b,c;
b = 0;
ll k = 1;
ll ans = n;
while( k <= n)
{
a = n/k;
c = n%k;
if( a+b+c < ans)
ans = a+b+c;
b++;
k = k*2;
}
cout<<ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
IOS;
ll t; t = 1;
//cin >> t;
for (ll i = 1; i <= t; i++)
fun();
} | #include<algorithm>
#include<bitset>
#include<cmath>
#include<complex>
#include<deque>
#include<functional>
#include<iomanip>
#include<iostream>
#include<iterator>
#include<map>
#include<numeric>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>
using namespace std;
typedef long long ll;
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--)
#define ALL(x) (x).begin(),(x).end()
#define SIZE(x) ((ll)(x).size())
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
#define D()
#define MAXR 100000
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define INITA(a,i,j,v) for(ll k=i;k<=j;k++){a[k]=v;}
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const long long INF = 1LL<<60;
const long long MOD = 1000000007;
int main() {
ll n; cin >> n;
ll res = 1e18;
for (int b=0; b<=60; b++) {
ll a = n / (1LL << b);
ll c = n - a * (1LL << b);
res = min(res, a + b + c);
}
cout << res << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef vector<bool> vbl;
typedef vector<vector<bool>> vvbl;
typedef pair<string, ll> pstll;
#define ADD emplace_back
#define MP make_pair
#define __LONG_LONG_MIN__ (-__LONG_LONG_MAX__ - 1)
//#define MOD 1000000007 // 10^9 + 7
//setprecision(10)
int main()
{
ll N, M;
cin >> N >> M;
vector<string> s(M);
for (ll i = 0; i < M; i++)
{
cin >> s[i];
}
sort(s.begin(), s.end(), [](string &s1, string &s2){return s1.size() > s2.size();});
vector<pstll> mp; // string & how many times it appears;
for(string si : s)
{
bool append = true;
for(auto &x : mp)
{
if(x.first.find(si) != string::npos)
{
x.second++;
append = false;
break;
}
}
if(append) mp.emplace_back(MP(si, 1));
}
sort(mp.begin(), mp.end(), [](pstll &p1, pstll &p2){return p1.second > p2.second;});
vector<vector<char>> result(N, vector<char>(N, '.'));
for (ll i = 0; i < N; i++)
{
string &si = mp[i].first;
for (ll j = 0; j < si.size(); j++)
{
result[i][j] = si[j];
}
}
for (ll i = N; i < N + 8; i++)
{
string &si = mp[i].first;
for (ll j = 0; j < si.size(); j++)
{
result[j][i - 8] = si[j];
}
}
for (ll i = 0; i < N; i++)
{
for (ll j = 0; j < N; j++)
{
cout << result[i][j];
}
cout << endl;
}
return 0;
}
| /********include********/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#define popcount __builtin_popcount
using namespace std;
//#include <atcoder/all>
/***/
//#include <iomanip>
//#include <cmath>
#include <bits/stdc++.h>
/********define********/
const int MAX = 510000;
const int MOD = 1000000007;
#define rep(i,x) for(long long i=0;i<x;i++)
#define repn(i,x) for(long long i=1;i<=x;i++)
#define rrep(i,x) for(long long i=x-1;i>=0;i--)
#define rrepn(i,x) for(long long i=x;i>1;i--)
#define REP(i,n,x) for(long long i=n;i<x;i++)
#define REPN(i,n,x) for(long long i=n+1;i<x;i++)
#define pr printf
#define re return
#define mod 1000000007
//#define mod 998244353
#define inf INT_MAX//19桁
#define INF 1e18+5//19桁
const double PI=3.14159265358979323846;
#define fi first
#define se second
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define all(x) (x).begin(),(x).end()
typedef long long int ll;
typedef pair<long long, long long> P;
/********変数宣言********/
//vector<long long> g[200020];
vector<pair<long long,long long>> g[200020];
ll s[200020];
bool used[200020];
//bool dp[100005];
ll A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,Q,R,S,T,U,V,W,X,Y,Z;
double dA,dB,dC,dD,dE,dF,dG,dH,dI,dJ,dK,dL,dM,dN,dO,dP,dQ,dR,dS,dT,dU,dV,dW,dX,dY,dZ;
string sa,sb,sc,sd,se,sf,sg,sh,si,sj,sk,sl,sm,sn,so,sp,sq,sr,ss, su,sv,sw,sx,sy,sz;
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
cin>>N;cin>>M;
vector<string>v(M);
map<string,ll>mp;
ll min,sum;
min=INF;
sum=0;
rep(i,M){
cin>>v[i];
min=MIN(min,v[i].size());
mp[v[i]]++;
}
map<string,ll>mp2;
rep(i,M){
sum=0;
for(ll j=min;j<=v[i].size();j++){
sum+=mp[v[i].substr(0,j)];
}
mp2[v[i]]+=sum;
}
deque<string> dque;
for(auto p:mp2){
dque.push_front(p.fi);
}
//sort(p.begin(), p.end());
ll check;
check=0;
ll i;
i=0;
ll flg;
while(check<N*N && i<M){
string now_str;
string pre_str;
now_str=dque[i];
if(check%N!=0){
if(i!=0 && pre_str[pre_str.size()-1]==now_str[0]){
rep(j,now_str.size()-1){
now_str[j]=now_str[j+1];
}
now_str.resize(now_str.size()-1);
}
}
if(check%N+now_str.size()<=N){
cout<<now_str;
check+=now_str.size();
if(check%N==0){
cout<<"\n";
}
}
else{
while(check%N!=0){
cout<<".";
check++;
}
cout<<"\n";
}
pre_str=now_str;
i++;
}
re 0;
}
|
#include <bits/stdc++.h>
#define db1(x) cout<<#x<<"="<<x<<'\n'
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
#define rep(i,n) for(int i=0;i<(n);++i)
#define repA(i,a,n) for(int i=a;i<=(n);++i)
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define repD(i,a,n) for(int i=a;i>=(n);--i)
using namespace std;
using ll = long long;
ll dp[5005][5005];
ll fact(ll n)
{
if(n==0||n==1)return 1;
else {
ll ans = n;
n--;
while(n!=1)
{
ans *= n;
n--;
}
return ans;
}
}
set<ll> taken;
ll m;
ll a[5005];
//vector <ll> ones, zeros;
// ll solve(ll i, ll start, ll n)
// {
// if(i==m)return 0;
// else {
// if(dp[i][start]!=-1)return dp[i][start];
// //a[ones[i]] = 0;
// ll ans = 100000000000;
// //a[ones[i]] = 0;
// ans = min( ans, solve(i+1,start + 1,vis1, vis2,n) + abs(ones[i]-start));
// ans = min(ans, solve(i+1, start))
// return dp[i][start] = ans;
// }
// }
int main()
{
ll n;
cin>>n;
//ll a[n];
ll i,j;
string s,t;
cin>>s>>t;
set <ll,greater<>> szero,tzero;
for(i=0;i<n;i++)
{
if(s[i]=='0')
{
szero.insert(i);
}
if(t[i]=='0')
{
tzero.insert(i);
}
}
if (tzero.size()!=szero.size()) {
cout << -1;
return 0;
}
ll f = 0;
ll ans = 0;
for(auto it: tzero)
{
if(it!=*szero.begin())
{
ans++;
}
szero.erase(szero.begin());
}
if(f)ans = -1;
cout<<ans<<endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vpii> vvpii;
typedef vector<vpll> vvpll;
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define fi first
#define se second
template<class T> bool ckmin(T &a, const T &b) {return a > b ? a = b, 1 : 0;}
template<class T> bool ckmax(T &a, const T &b) {return a < b ? a = b, 1 : 0;}
void __print(int x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for(auto z : x) cerr << (f++ ? "," : ""), __print(z); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if(sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef ljuba
#define dbg(x...) cerr << "LINE(" << __LINE__ << ") -> " << "[" << #x << "] = ["; _print(x)
#else
#define dbg(x...)
#endif
const char nl = '\n';
void solve() {
int n;
cin >> n;
vi v(n);
for(auto &z : v)
cin >> z;
int mini = *min_element(all(v));
map<int, int> ma;
for(auto z : v) {
for(int i = 1; i * i <= z; ++i) {
if(z % i == 0) {
ma[i] = __gcd(ma[i], z);
if(z / i != i)
ma[z/i] = __gcd(ma[z/i], z);
}
}
}
dbg(ma);
int ans = 0;
for(auto z : ma) {
if(z.fi == z.se && z.fi <= mini)
++ans;
}
cout << ans << nl;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int testCases = 1;
//cin >> testCases;
while(testCases--)
solve();
}
|
#include <iostream>
using namespace std;
typedef long long ll;
int main()
{
ll s, p;
cin >> s >> p;
for(ll i = 1; i * i <= p; i++){
if(p % i == 0 && i + p / i == s){
cout << "Yes" << endl;
return 0;
}
}
cout << "No" << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define scan(any) for(auto &i:any) cin>>i;
#define print(any) for(auto i:any) cout<<i<<" ";
#define endl '\n'
#define pb push_back
#define vll vector<ll>
#define f(i,x,y) for(i=x;i<y;i++)
#define INF LLONG_MAX
#define s(x) sort(x.begin(),x.end())
#define all(v) v.begin(),v.end()
#define p2(n,x) cout << fixed << setprecision(x) <<n<<endl;
#define pll pair<ll,ll>
#define ff first
#define ss second
#define blt(x) __builtin_popcount(x)
void solve()
{
ll i,j,b,s,p;
cin>>s>>p;
for(i=1;i<=(int)sqrt(p);i++)
{
if(p%i==0)
{
if(i+(p/i)==s)
{
cout<<"Yes";
return ;
}
}
}
cout<<"No\n";
}
int main()
{
#ifdef Sudarshan
freopen("F:/Programming/input.txt", "r", stdin);
freopen("F:/Programming/output.txt", "w", stdout);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
ll t = 1;
while(t--) {
solve();
}
return 0;
} |
#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define ll long long
#define ALL(a) (a).begin(),(a).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define Pii pair<int,int>
#define Pll pair<long long,long long>
#define fi first
#define se second
#define pb push_back
#define fout(num) cout << fixed << setprecision(20) << (num) << endl
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;}
//vector<vector<ll>> dp(n,vector<ll>(n))
//2-dim:vector<vector<Type>> vv(n, vector<Type>(m, d));
//3-dim:vector<vector<vector<Type>>> vvv(n, vector<vector<Type>>(m, vector<Type>(l, d)));
using namespace std;
signed main(){
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
int h,w; cin >> h >> w;
vector<string> a(h);
int s,g;
vector<vector<Pii>> G(h*w+30);
vector<int> al[30];
rep(i,h){
cin >> a[i];
rep(j,w){
if(a[i][j]=='S') s=i*w+j;
else if(a[i][j]=='G') g=i*w+j;
else if(a[i][j]!='.'&&a[i][j]!='#'){
int x=(a[i][j]-'a');
al[x].pb(i*w+j);
}
}
}
//cout << s << " " << g << endl;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
rep(i,h){
rep(j,w){
rep(k,4){
if(a[i][j]=='#') continue;
int nx=i+dx[k],ny=j+dy[k];
if(0<=nx&&nx<h && 0<=ny&&ny<w && a[nx][ny]!='#'){
G[i*w+j].pb({nx*w+ny,1});
G[nx*w+ny].pb({i*w+j,1});
}
}
}
}
rep(i,26){
rep(j,al[i].size()){
G[al[i][j]].pb({h*w+i,1});
G[h*w+i].pb({al[i][j],0});
}
}
vector<int> d(h*w+30,1e9);
priority_queue<Pii,vector<Pii>,greater<Pii>> pq; //{dist,v}
d[s]=0;
pq.push({0,s});
while(!pq.empty()){
Pii tp=pq.top(); pq.pop();
int v=(int)tp.second;
if(d[v]<tp.first) continue;
for(auto u:G[v]){
if(d[v]+u.se<d[u.fi]){
d[u.fi]=d[v]+u.se;
pq.push({d[u.fi],u.fi});
}
}
}
if(d[g]==1e9) cout << -1 << endl;
else cout << d[g] << endl;
return 0;
}
// g++ main.cpp -o main.out && ./main.out | #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; }
template<class T>
vector<pair<T, int>> factorize(T n) {
vector<pair<T, int>> res;
for (long long i = 2; (i * i) <= n; i++) {
if (n % i) continue;
res.emplace_back(i, 0);
while((n % i) == 0) {
n /= i;
res.back().second++;
}
}
if (n != 1) res.emplace_back(n, 1);
return res;
}
ll n, ans = LONG_LONG_MAX;
vector<ll> x;
vector<vector<pair<ll, int>>> xf;
map<ll, ll> cnt;
void dfs(ll idx) {
if (idx == n) {
ll tmp = 1;
for(auto &v : cnt) {
tmp *= v.first;
}
chmin(ans, tmp);
return;
}
bool f = false;
for(auto &v : xf[idx]) {
if (cnt.count(v.first)) {
f = true;
break;
}
}
if (f) {
dfs(idx + 1);
}
else {
for(auto &v : xf[idx]) {
if (v.first == 1) continue;
cnt[v.first]++;
dfs(idx + 1);
cnt[v.first]--;
if (cnt[v.first] == 0) cnt.erase(v.first);
}
}
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
// ifstream in("input.txt");
// cin.rdbuf(in.rdbuf());
cin >> n;
x.resize(n);
rep(i, n) cin >> x[i];
xf.resize(n);
rep(i, n) xf[i] = factorize(x[i]);
dfs(0);
cout << ans << endl;
return 0;
}
|
#include<bits/stdc++.h>
// #include<atcoder/all>
#define rep(i, n) for(ll i = 0; i < (ll)(n); 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; }
#define all(x) (x).begin(),(x).end()
#pragma GCC optimize("Ofast")
using namespace std;
// using namespace atcoder;
typedef long long int ll;
typedef long double ld;
typedef long long lint;
const ll INF=(1LL<<62);
const ll LINF=(1LL<<62);
const ld pi=acosl((ld)-1);
const ll mod = 1000000007;
// const ll mod = 1234567;
const int dx[4]={0,1,0,-1};
const int dy[4]={1,0,-1,0};
const int ddx[8]={1,0,-1,-1,-1,0,1,1};
const int ddy[8]={1,1,1,0,-1,-1,-1,0};
#define endn "\n"
#define TO_STRING(VariableName) # VariableName
template <typename T>ostream &operator<<(ostream &out,const vector<T> &v) {rep(i,(int)v.size()-1)cout<<v[i]<<" ";cout<<v[(int)v.size()-1];return out;}
template <typename T1, typename T2>ostream &operator<<(ostream &out, const map<T1, T2> &p) {out << "(" << p.first << ", " << p.second << ")";return out;}
template <typename T1, typename T2>ostream &operator<<(ostream &out, const pair<T1, T2> &p){out << "(" << p.first << ", " << p.second << ")";return out;}
template<class T>void debag(const T &obj){cout<<obj<<endl;}
bool solve(){
ll A,B;cin>>A>>B;
const ll num=20;
ll primes[num]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71};
// ll primes[num]={2,3};
ll n=B-A+1;
vector<int>G(n);
rep(i,n)rep(j,num){
if((A+i)%primes[j]==0){
G[i]+=(1LL<<j);
}
}
vector<vector<ll>>dp(n+1,vector<ll>((1LL<<num),0));
dp[0][0]=1;
rep(i,n)rep(j,(1LL<<num)){
dp[i+1][j]+=dp[i][j];
//使える?
if(G[i] & j)continue;
dp[i+1][G[i] | j]+=dp[i][j];
}
ll ans=0;
// rep(i,n+1)cout<<dp[i]<<endl;
rep(j,(1LL<<num))ans+=dp[n][j];
cout<<ans<<endl;
// cout<<(2 | 3)<<endl;
return false;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout<<fixed<<setprecision(30);
// ll T;cin>>T;
// rep(i,T){
// cout<<"Case #"<<i+1<<" : ";solve();
// }
solve();
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int gcd(int a, int b){
if(b == 0) return a;
return gcd(b, a % b);
}
int main() {
int N,W;
cin >> N >> W;
if(N%W == 0){
cout << N/W;
} else {
cout << (N-N%W)/W;
}
} |
#include<bits/stdc++.h>
using namespace std;
int H,W;
int A[101][101];
int func(int i,int j)
{
int ans = 0;
if((j > 0)&&(A[i][j-1] == 1))
{
ans++;
}
if((i > 0)&&(A[i-1][j] == 1))
{
ans++;
}
if((j < W-1)&&(A[i][j+1] == 1))
{
ans++;
}
if((i < H-1)&&(A[i+1][j] == 1))
{
ans++;
}
return ans;
}
int main()
{
int ans = 0;
cin>>H>>W;
string str;
for(int i = 0;i < H;i++)
{
cin>>str;
for(int j = 0;j < W;j++)
{
if(str[j] == '.')
{
A[i][j] = 1;
}
else
{
A[i][j] = 0;
}
}
}
for(int i = 0;i < H;i++)
{
for(int j = 0;j < W;j++)
{
if(A[i][j] == 1)
{
ans += func(i,j);
}
A[i][j] = 0;
}
}
cout<<ans<<"\n";
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef map<int,int> mii;
typedef map<ll,ll> mll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
#define f(i,n) for(i=0;i<n;i++)
#define f1(i,n) for(i=1;i<n;i++)
#define fr(i,n) for(i=n-1;i>=0;i--)
#define em emplace_back
#define mp make_pair
#define in insert
#define fi first
#define sc second
#define b begin
#define e end
#define er erase
#define l length
#define c clear
#define si size
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);
const double pi=3.141592653;
const ll infi=1000000001;
const ll MOD=1000000007;
const ll mod=998244353;
const string no="NO\n",yes="YES\n";
// ll gcd(ll a,ll b){
// if(a==0) return b;
// gcd(b%a,a);
// }
// void dfs(vl v[],ll node,ll hai[]){
// hai[node]=1;
// for(auto i:v[node]){
// if(hai[i]==0){
// dfs(v,i,hai);
// }
// }
// }
// ll con(vl v[],ll hai[],ll sz){
// ll count=0;
// for(int i=1;i<sz;i++){
// if(hai[i]==0){
// count++;
// dfs(v,i,hai);
// }
// }
// return count;
// }
// void gcde(int a,int b,int *x,int *y){
// if(a==0){
// *x=0;
// *y=1;
// return;
// }
// int x1,y1;
// gcde(b%a,a,&x1,&y1);
// *x=y1-(b/a)*x1;
// *y=x1;
// }
// ll recur(vl v,ll in,ll m,ll n){
// if(in==-1) return 1000000000001;
// ll d,e,c;
// d=max(m/v[in],n*v[in]);
// c=recur(v,in-1,m/v[in],n*v[in]);
// e=recur(v,in-1,m,n);
// return min(c,min(e,d));
// }
// ll bexpo(ll a,ll p){
// ll x=1;
// while(p){
// if(p&1){
// x=(x*a)%mod;
// }
// a=(a*a)%mod;
// p>>=1;
// }
// return x;
// }
// int dx[8]{-1,0,0,1,-1,-1,1,1};
// int dy[8]{0,-1,1,0,-1,1,-1,1};
int main() {
fastio
int t=1;
// cin>>t;
ll n,m,i,l,d,bank,s,k,j,x,y,ans,q;
while(t--){ans=0;
cin>>n>>m;
string s[n];
f(i,n) cin>>s[i];
f(i,n){
f(j,m-1){
if(s[i][j]==s[i][j+1]&&s[i][j]=='.') ans++;
}
}
f(i,m){
f(j,n-1){
if(s[j][i]==s[j+1][i]&&s[j][i]=='.') ans++;
}
}
cout<<ans<<endl;
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
const int l1[8] = {0,4,8,10,11,12,14,15};
const int l2[4] = {2,3,5,13};
const int l3[4] = {1,6,7,9};
int n, mask, F[1005];
int main() {
char c1, c2, c3, c4;
scanf("%d %c %c %c %c", &n, &c1, &c2, &c3, &c4);
c1 -= 'A';
c2 -= 'A';
c3 -= 'A';
c4 -= 'A';
mask = c1 + c2 * 2 + c3 * 4 + c4 * 8;
if (n == 2) {
puts("1");
return 0;
}
for (int i = 0; i < 8; i++) if (l1[i] == mask) {
puts("1");
return 0;
}
for (int i = 0; i < 4; i++) if (l2[i] == mask) {
n -= 3;
int ans = 1;
while (n--) {
ans = 2ll * ans % mod;
}
printf("%d\n", ans);
return 0;
}
F[2] = F[3] = 1;
for (int i = 4; i <= n; i++) F[i] = (F[i - 1] + F[i - 2]) % mod;
printf("%d\n", F[n]);
return 0;
} | #include <cstdio>
const int Mod = 1000000007;
int N;
char AA[3], AB[3], BA[3], BB[3];
void pow2() {
int n = N - 3;
int res = 1;
for (int i = 1; i <= n; ++i)
res = res * 2 % Mod;
printf("%d\n", res);
}
void fib() {
int n = N - 3;
int a1 = 1, a2 = 1;
for (int i = 1; i <= n; ++i) {
int tmp = (a1 + a2) % Mod;
a1 = a2, a2 = tmp;
}
printf("%d\n", a2);
}
int main() {
scanf("%d%1s%1s%1s%1s", &N, AA, AB, BA, BB);
int s = (*AA == 'B') << 3 | (*AB == 'B') << 2 | (*BA == 'B') << 1 | (*BB == 'B');
if (N <= 3) return puts("1"), 0;
if (s == 4 || s == 10 || s == 11 || s == 12) pow2();
else if (s == 6 || s == 8 || s == 9 || s == 14) fib();
else puts("1");
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int N, K;
cin >> N >> K;
int T[N][N];
vector<int> array(0);
for(int i = 0; i < N; i++){
array.push_back(i);
}
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
cin >> T[i][j];
}
}
int ans = 0;
do{
long long int tmp = 0;
for(int i = 0; i < N; i++){
tmp += T[array[i]][array[(i+1)%N]];
}
if(tmp == K) ans++;
}while(next_permutation(array.begin()+1,array.end()));
cout << ans << endl;
} | // I SELL YOU...!
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<queue>
#include<chrono>
#include<iomanip>
#include<map>
#include<set>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using TP = tuple<ll,ll,ll>;
void init_io(){
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(18);
}
signed main(){
init_io();
ll n,k,pr=0;
cin >> n >> k;
vector<ll> a(n),b(n);
vector<P> ab(n);
for(int i=0;i<n;i++){
cin >> a[i] >> b[i];
ab[i] = P(a[i],b[i]);
}
sort(ab.begin(),ab.end());
for(int i=0;i<n;i++){
auto [ta,tb] = ab[i];
ll dist = ta - pr;
if (dist>k){
cout << pr + k << endl;
return 0;
}
k = k - dist + tb;
pr = ta;
}
cout << pr + k << endl;
}
|
#include <bits/stdc++.h>
//#include<fstream>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
#define endl '\n'
#define pb push_back
#define fi first
#define se second
typedef long long ll;
#define double long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fr(a,b,c) for(int a=b; a<=c; a++)
#define rep(a,b,c) for(int a=b; a<c; a++)
#define all(con) con.begin(),con.end()
#define w(x) int x; cin>>x; while(x--)
const ll infl=0x3f3f3f3f3f3f3f3fLL;
const int infi=0x3f3f3f3f;
//const int mod=998244353;
const int mod=1000000007;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
#define pqb priority_queue<int>
#define pqs priority_queue<int,vi,greater<int> >
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
//typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
class Timer { chrono::time_point <chrono::steady_clock> B, E; public: Timer () : B(), E()
{ B = chrono::steady_clock::now(); } ~Timer () { E = chrono::steady_clock::now();
cerr << "\nDuration: " << ((chrono::duration <double>)(E - B)).count() << "s\n"; } } TimerT;
/*ofstream fout("bcount.out");
ifstream fin("bcount.in");*/
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cout<<fixed<<setprecision(8);
ll a,b,c,d;
cin>>a>>b>>c>>d;
cout<<min({a,b,c,d})<<endl;
//cerr<<"\n"<<(float)clock()/CLOCKS_PER_SEC*1000<<" ms"<<endl;
return 0;
}
//1. Always consider edge cases like n = 1
//2. Do something or the other rather than just wasting time | #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define MOD 1000000007
#define mod9 1000000009
#define fast ios_base :: sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define mp make_pair
#define pb push_back
#define ct ll t;cin>>t;while(t--)
#define bi begin()
#define ei end()
#define fi first
#define se second
#define foe(i,a,b) for(ll i=a;i<b;i++)
#define rfoe(i,a,b) for(ll i=a;i>=0;i--)
#define sz(s) s.size()
#define mem(a,s) memset(a,s,sizeof(a))
#define all(v) v.bi,v.ei
#define MAX 8000000000000000064LL
#define MIN -8000000000000000064LL
typedef pair<ll,ll> pii;
ll add(ll a, ll b){return (a%MOD + b%MOD + ((MAX)/MOD)*MOD)%MOD;}
ll sub(ll a, ll b){return (a%MOD - b%MOD + ((MAX)/MOD)*MOD)%MOD;}
ll mul(ll a, ll b){return ((a%MOD)*(b%MOD) + ((MAX)/MOD)*MOD)%MOD;}
long long binpow(long long a, long long b)
{
long long res = 1;
while (b > 0) {
if (b & 1)
res = res*a;
a = a*a;
b >>= 1;
}
return res;
}
// ll lcm(ll a,ll b){return(a*b)/__gcd(a,b);}
// ll fact[1000007 ]={0};
// ll expo(ll x, ll y) {ll res=1;x=x%MOD;while(y>0){if(y&1)res=(1ll*res*x)%MOD;
// y=y>>1;x=(1ll*x*x)%MOD;} return res;}
// void facto() {fact[0]=1;fact[1]=1;for(ll i=2;i<1000007;i++)fact[i]=(fact[i-1]*i)%MOD;}
// ll ncr(ll n,ll r) {ll res=1; res=fact[n]; res=(res*(expo(fact[r],MOD-2)))%MOD; res=(res*(expo(fact[n-r],MOD-2)))%MOD; return res;}
// ll npr(ll n,ll r) {facto(); ll res=1; res=fact[n]; res=(res*(expo(fact[n-r],MOD-2)))%MOD; return res; }
int const N=1e5+9;
ll const INF = 2e18+5;
ll dx[8]={0,0,1,-1,1,1,-1,-1};
ll dy[8]={1,-1,0,0,-1,1,-1,1};
void solve()
{
ll a[4],m=105;
foe(i,0,4)
{
cin>>a[i];
m=min(m,a[i]);
}
cout<<m<<"\n";
}
int main()
{
fast
// facto();
// Sieve();
// ct
{
solve();
}
} |
/*
atcoder —— abc199 —— F-Graph Smoothing
*/
#include <bits/stdc++.h>
using namespace std;
#define closeSync ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
typedef long long ll;
const ll MOD = 1e9+7;
const int MAXN = 105;
int n,m,k;
ll A[MAXN],B[MAXN],down = 1;
ll mp[MAXN][MAXN];
struct Mat{
ll a[MAXN][MAXN];
int n;
Mat(int _n = 0)
{
n = _n;
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
a[i][j] = 0;
}
Mat operator*(const Mat& x)const
{
Mat res(n);
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
for (int k=1;k<=n;k++)
res.a[i][j] += a[i][k]*x.a[k][j],res.a[i][j] %= MOD;
return res;
}
void ones()
{
for (int i=1;i<=n;i++) a[i][i] = 1;
}
};
inline Mat mpower(Mat x,ll p)
{
Mat res(n);
res.ones();
while (p)
{
if (p&1) res = res * x;
p >>= 1;
x = x * x;
}
return res;
}
inline ll qpower(ll x,ll p,ll mod)
{
ll res = 1;
while (p)
{
if (p&1) res *= x,res %= mod;
p >>= 1;
x *= x;
x %= mod;
}
return res;
}
inline void solve()
{
cin >> n >> m >> k;
Mat help(n);
for (int i=1;i<=n;i++) cin >> A[i];
for (int i=1;i<=m;i++)
{
int u,v; cin >> u >> v;
mp[u][v] = mp[v][u] = 1;
}
for (int i=1;i<=n;i++)
{
int cnt = 0;
for (int j=1;j<=n;j++)
if (mp[i][j]) cnt++,help.a[i][j]++,help.a[i][i]++;
help.a[i][i] += ((m-cnt) << 1);
}
Mat tmp = mpower(help,k);
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
B[i] += tmp.a[i][j] * A[j],B[i] %= MOD;
down *= (qpower(2LL,k,MOD) * qpower(m,k,MOD) % MOD);
down %= MOD;
for (int i=1;i<=n;i++)
cout << (B[i] * qpower(down,MOD-2,MOD) % MOD) << "\n";
}
int main()
{closeSync;
// int T;
// cin >> T;
// while (T--)
solve();
return 0;
}
/*
3 2 2
12 48 36
1 2
1 3
750000036
36
250000031
*/ | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repp(i, st, en) for (ll i = (ll)st; i < (ll)(en); i++)
#define repm(i, st, en) for (ll i = (ll)st; i >= (ll)(en); i--)
#define all(v) v.begin(), v.end()
void chmax(ll &x, ll y) {x = max(x,y);}
void chmin(ll &x, ll y) {x = min(x,y);}
void Yes() {cout << "Yes" << endl; exit(0);}
void No() {cout << "No" << endl; exit(0);}
template<class in_Cout> void Cout(in_Cout x) {cout << x << endl; exit(0);}
template<class in_vec_cout>
void vec_cout(vector<in_vec_cout> vec) {
for (in_vec_cout res : vec) {cout << res << " ";}
cout << endl;
}
const ll inf = 1e18;
const ll mod = 1e9 + 7;
using mat = vector<vector<ll>>;
ll N, M, K;
bool G[110][110];
mat A, E;
ll powmodp(ll x, ll n) {
if (n==0) return 1;
if (n%2) return x * powmodp(x,n-1) % mod;
ll res = powmodp(x,n/2);
return res * res % mod;
}
ll inv(ll x) {return powmodp(x,mod-2);}
mat prod(mat x, mat y) {
ll n = x.size();
mat res(n,vector<ll>(N));
rep(i,n) rep(j,N) rep(k,N) {
res[i][j] += x[i][k]*y[k][j]%mod;
res[i][j] %= mod;
}
return res;
}
mat powmat(mat x, ll n) {
if (n==0) return E;
if (n%2) return prod(x,powmat(x,n-1));
mat res = powmat(x,n/2);
return prod(res,res);
}
int main() {
cin >> N >> M >> K;
A.resize(1,vector<ll>(N));
rep(i,N) cin >> A[0][i];
rep(i,M) {
ll x, y; cin >> x >> y;
x--; y--;
G[x][y] = G[y][x] = true;
}
E.resize(N,vector<ll>(N));
rep(i,N) E[i][i] = 1LL;
ll p = inv(M);
ll half = inv(2);
mat f(N,vector<ll>(N));
rep(j,N) {
ll cnt = 0;
rep(i,N) if (G[i][j]) cnt++;
rep(i,N) {
if (i==j) {
ll q = cnt * p % mod;
ll sub = q * half % mod;
f[i][j] = (1 + mod - sub) % mod;
} else if (G[i][j]) {
f[i][j] = p * half % mod;
}
}
}
mat F = powmat(f,K);
mat vec = prod(A,F);
vector<ll> res = vec[0];
for (ll ans : res) cout << ans << endl;
} |
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <climits>
#include <map>
#include <set>
#include <iomanip>
#include <unordered_set>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <unordered_map>
#define rep(i,a,b) for(int i=a;i<b;i++)
using namespace std;
void test_case() {
int64_t n;
cin >> n;
vector<int64_t> a(n);
rep(i, 0, n) cin >> a[i];
map<int64_t, int64_t> freq;
freq[a[0]]++;
int64_t res = 0;
rep(i, 1, n) {
for(auto x : freq) {
int64_t diff = abs(x.first - a[i]);
res += diff * diff * x.second;
}
freq[a[i]]++;
}
cout << res << '\n';
}
int32_t main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int T;
T = 1;
while(T-- >0) {
test_case();
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template<class T> using vc = vector<T>;
template<class T> using vvc = vector<vector<T>>;
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i,n) for (auto& i: n)
template<class T1, class T2> inline bool chmax(T1 &a, const T2 &b) {if (a<b) { a=b; return 1;} return 0;}
template<class T1, class T2> inline bool chmin(T1 &a, const T2 &b) {if (b<a) { a=b; return 1;} return 0;}
struct init{init(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}}init_;
#ifdef DEBUG
template <class T> void verr(const set<T> &st) { repa(a, st) cerr << a << " "; cerr << endl; }
template <class S, class T> void verr(const map<S, T> &mp) { repa(a, mp) cerr << "{" << a.first << ", " << a.second << "} "; cerr << endl; }
template <class S, class T, class N> void verr(const vector<pair<S,T>>& a, const N& n) { rep(i, n) cerr << "{" << a[i].first << ", " << a[i].second << "} "; cerr << endl; }
template <class T, class N> void verr(const vector<T>& a, const N& n) { rep(i, n) cerr << a[i] << " "; cerr << endl; }
ll dbgt = 1; void err() { cerr << "passed " << dbgt++ << endl; }
template<class H, class... T> void err(H&& h,T&&... t){ cerr<< h << (sizeof...(t)?" ":"\n") << flush; if(sizeof...(t)>0) err(forward<T>(t)...); }
#endif
const ll INF = 4e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//
int main() {
ll N;
cin >> N;
vl A(N);
rep(i, N) cin >> A[i];
sort(all(A));
vl acc(N + 1);
rep(i, N) acc[i + 1] = acc[i] + A[i];
vl ss(N + 1);
rep(i, N) ss[i + 1] = ss[i] + A[i] * A[i];
ll ans = 2 * N * ss[N] - 2 * acc[N] * acc[N];
ans /= 2;
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, s, n) for (int i = (s); i < (int)(n); i++)
#define repn(i, n) for (int i = (int)(n)-1; i>= 0; --i)
#define ll long long
using Graph = vector<vector<int>>;
int main(){
int n, m;
cin >> n >> m;
vector<int> a(n), b(m);
rep(i, n) cin >> a[i];
rep(i, m) cin >> b[i];
vector<bool> numa(1000, false), numb(1000, false);
rep1(i, 1, 1001){
if (find(a.begin(), a.end(), i) != a.end()) numa[i-1] = true;
if (find(b.begin(), b.end(), i) != b.end()) numb[i-1] = true;
}
rep(i, 1000){
if ((numa[i] == true && numb[i] == false) || (numb[i] == true && numa[i] == false)){
cout << i+1 << " ";
}
}
cout << endl;
} | //Fuck the world....don't care anyone........................................................
#include <bits/stdc++.h>
#include<bitset>
#include<iostream>
using namespace std;
#include<math.h>
#include<map>
#include<vector>
#include<string>
#include<algorithm>
#define ll long long
#define ld double
#include<stdio.h>
#define f first
#define s second
#include<stack>
#include<queue>
#define its set<int>::iterator
#define itmp map<int,int>::iterator
#include<stack>
#define lp for(int i=0;i<n;i++)
#define dlp for(int i=0;i<n;i++) for(int j=0;j<n;j++)
#define mod 1000000009
//#define N 101
//#include <boost/functional/hash.hpp>
//bitset<1000> b;
//b.set(); all bit have 1
//seive of Erat
//totient theorm for co-prime
/*ll int N=1000;
int p[1001]={0};
p[2]=1;
vector<int>v;
for(ll int i=3;i<=N;i+=2)
p[i]=1;
v.push_back(2);
for(ll int i=3;i<=N;i+=2)
{
if(p[i])
{
v.push_back(i);
for(ll int j=i*i;j<=N;j+=2*i)
p[j]=0;
}
} */
/*
int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
} */
/*
stringstream ss;
ss<<c;
string s1;
ss>>s1;
*/
/*
int power(int x, unsigned int y)
{
int temp;
if( y == 0)
return 1;
temp = power(x, y/2);
if (y%2 == 0)
return temp*temp;
else
return x*temp*temp;
}*/
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll int t=1;
while(t--)
{
// vector<pair<ll,ll> >v;
ll n;
cin>>n;
ll a[n];
set<ll>s;
lp
{
cin>>a[i];
s.insert(a[i]);
}
set<ll>::reverse_iterator rit;
set<ll>::iterator it;
while(1)
{
rit=s.rbegin();
it=s.begin();
//cout<<*it<<" "<<*rit<<endl;
if(*rit==*it)
{
cout<<*it<<endl;
return 0;
}
//cout<<*rit<<endl;
s.insert(*rit-*it);
s.erase(*rit);
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define _GLIBCXX_DEBUG
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define makev(a, n) vector<int> a(n, 0)
#define all(v) v.begin(), v.end()
#define F first
#define S second
#define printv(v) rep(i, (int)v.size()) cout << v[i] << (i+1 == (int)v.size() ? "\n" : " ");
#define rfor(i, a, b) for (int i = a; i < b; i++)
const vector<int> maze = {0, 1, 0, -1, 0};
const int mod = 1e9+7;
ll gcd(ll a, ll b) {return b == 0 ? abs(a) : gcd(b, a%b);}
ll lcm(ll a, ll b) {return a*b/gcd(a, b);}
ll extgcd(ll a, ll b, ll &x, ll &y) {if (b == 0) {x = 1; y = 0;return a;} else {ll d = extgcd(b, a % b, y, x);y-=a/b*x;return d;}}
ll modpow(ll x, ll n, ll p) {ll ans = 1;while (n > 0) {if (n&1) ans = ans*x%p;x = x*x%p;n >>= 1;}return ans;}
int main() {
int n; cin >> n;
vector<string> s(n);
rep(i, n) cin >> s[i];
vector<ll> dp0(n+1, 0); dp0[0] = 1;
vector<ll> dp1(n+1, 0); dp1[0] = 1;
rfor(i, 1, n+1) {
if (s[i-1] == "AND") {
dp0[i] = dp0[i-1]*2+dp1[i-1];
dp1[i] = dp1[i-1];
} else {
dp0[i] = dp0[i-1];
dp1[i] = dp0[i-1]+dp1[i-1]*2;
}
}
cout << dp1[n] << endl;
} | #include <bits/stdc++.h>
using namespace std;
const int P = (int) 998244353;
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n;
cin >> n;
vector<int> w(n);
for (int i = 0; i < n; i++) {
cin >> w[i];
}
int s = accumulate(begin(w), end(w), 0);
if (s % 2 == 1) {
cout << 0 << endl;
} else {
vector<vector<int>> f(n + 1, vector<int>(s / 2 + 1, 0));
f[0][0] = 1;
for (int i = 0; i < n; i++) {
for (int j = i; j >= 0; --j) {
for (int k = 0; k + w[i] <= s / 2; k++) {
int &trans = f[j + 1][k + w[i]];
trans += f[j][k];
trans -= P;
trans += trans >> 31 & P;
}
}
}
vector<int> fact(n + 1);
fact[0] = 1;
for (int i = 0; i < n; i++) {
fact[i + 1] = (long long) fact[i] * (i + 1) % P;
}
int ans = 0;
for (int i = 0; i <= n; i++) {
ans += (long long) fact[i] * fact[n - i] % P * f[i][s / 2] % P;
ans -= P;
ans += ans >> 31 & P;
}
cout << ans << endl;
}
}
|
#include<bits/stdc++.h>
#define repa(x,y,z) for(ll x=y;x<z;x++)
#define ll long long int
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define all(a) a.begin(),a.end()
#define memf(a) memset(a,false,sizeof(a))
#define F first
#define S second
#define ot(a) cout<<a<<endl;
#define endl "\n"
#define godspeed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define vi vector<int>
#define vl vector<long long int>
#define pi 3.141592653589793238
using namespace std;
int M=1e9+7;
ll INF=1e18+7;
/**************************************************************************************************/
ll power( ll b, ll e)
{
if (e == 0) return 1;
if (e & 1) return b * power(b * b, e / 2);
return power(b * b, e / 2);
}
ll powmod(ll a, ll b) {
if (b == 0)
return 1;
ll res = powmod(a, b / 2)%M;
if (b % 2)
return ((((ll)res * (ll)res) %M)* (ll)a)%M;
else
return ((ll)res * (ll)res)%M;
}
//use like
//vector< pair<int, int> >v;
//sort(v.begin(),v.end(),compare);
bool compare(const pair<ll, ll>&i, const pair<ll, ll>&j)
{
return i.first < j.first;
}
bool comp(const pair<int, pair<int,int> >&i, const pair<int, pair<int,int> >&j)
{
if(i.S.F==j.S.F)
return i.S.S>j.S.S;
return i.S.F < j.S.F;
}
vector<int> prime;
void seive(int n)
{
bitset<200005> vis;
repa(i,2,n)
{
if(vis[i]==1)
continue;
prime.pb(i);
for(int j=i;j*i<=n;j++)
{
vis[j*i]=1;
}
}
}
/*******************
1. to_string() -> int to string
2. stoi() -> string to int
*******************/
/**************************************************************************************************/
void Piyush_Orz(){
ll n,c1; cin>>n>>c1;
ll a,b,c;
pair<ll,ll> p1[n],p2[n];
set<ll> s;
repa(i,0,n)
{
cin>>a>>b>>c;
s.insert(a);
s.insert(b);
p1[i]=mp(a,c);
p2[i]=mp(b,c);
}
sort(p1,p1+n);
sort(p2,p2+n);
ll i=0,k1=0,k2=0;
vector<ll> v[s.size()];
for(auto x:s)
{
while(p1[k1].F==x)
v[i].pb(p1[k1].S),k1++;
while(p2[k2].F==x)
v[i].pb(-1*p2[k2].S),k2++;
i++;
}
i=0;
ll m=0,last=-1;
ll ans=0;
for(auto x:s)
{
if(last>0)
{
if(m<c1)
ans+=((ll)(x-last-1))*m;
else
ans+=((ll)(x-last-1))*c1;
}
for(auto y:v[i])
{
if(y>0)
m+=y;
}
if(m<c1)
ans+=m;
else
ans+=c1;
for(auto y:v[i])
{
if(y<0)
m+=y;
}
last=x;
i++;
}
cout<<ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
godspeed
seive(30005);
#ifdef NCR
init();
#endif
ll t = 1;
//cin >> t;
cout << fixed << setprecision(12);
repa(i, 0, t)
{
Piyush_Orz();
}
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define drep(i, cc, n) for (ll i = (cc); i <= (n); ++i)
#define rep(i, n) drep(i, 0, n - 1)
#define P pair<ll, ll>
int main(){
ll N;
cin >> N;
double x_0, y_0;
double x_half, y_half;
cin >> x_0 >> y_0;
cin >> x_half >> y_half;
double x_center = (x_0 + x_half)/2.0;
double y_center = (y_0 + y_half)/2.0;
double x_1 = (x_0 - x_center) * cos(2 / (double)N * M_PI) - (y_0 - y_center) * sin(2 / (double)N * M_PI) + x_center;
double y_1 = (x_0 - x_center) * sin(2 / (double)N * M_PI) + (y_0 - y_center) * cos(2 / (double)N * M_PI) + y_center;
cout << x_1 << " " << y_1 << endl;
} |
#include<bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long n , k , a [300005] , num [300005] , ans;
void go ( int x )
{
num [x] --;
if ( num [ x + 1 ] == 0 )
{
ans += x + 1;
return;
}
else go ( x + 1 );
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
cin >> n >> k;
for ( int i = 0 ; i < n ; i ++ )
{
cin >> a [i];
num [ a [i] ] ++;
}
while ( k )
{
if ( num [0] == 0 ) break;
go ( 0 );
k --;
}
cout << ans;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = (a); i < (b); ++i)
#define per(i, a, b) for(int i = (b)-1; i >= (a); --i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define pb push_back
#define eb emplace_back
#define mp make_pair
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; }
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<pii> vii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
int n, k;
cin >> n >> k;
vi cnt(n+1);
rep(i,0,n) {
int x;
cin >> x;
++cnt[x];
}
int tot = min(cnt[0], k);
ll ans = 0;
rep(i,1,n+1) {
if (tot == 0) break;
// how many i do we have
int nxt = min(tot, cnt[i]);
int rm = tot-nxt;
ans += ll(i) * ll(rm);
tot = nxt;
}
cout << ans << '\n';
} |
#include <stdio.h>
#include<iostream>
#include <string>
#include <string.h>
#include <vector>
#include <cmath>
#include <algorithm>
#include <queue>
#include <climits>
#include <set>
#include <unordered_map>
#include <map>
#include <stack>
#include <unordered_set>
#define hash hassh
using namespace std;
long double ansk[200005],ansb[200005];
int f[200005];
int main() {
int n,m,k,t;
cin>>n>>m>>k;
for(int i=1;i<=k;i++) scanf("%d",&t),f[t]=1;
long double sum=0;
long double cnt=0;
for(int i=n-1;i>=0;i--){
//cout<<cnt<<" "<<sum<<endl;
if(f[i]) ansk[i]=(long double)1.0000000,ansb[i]=0;
else {
ansk[i] = cnt / m;
ansb[i] = sum / m + (long double)1.000000;
}
sum+=ansb[i];
cnt+=ansk[i];
sum-=ansb[i+m];
cnt-=ansk[i+m];
if(fabs(ansk[i]-1.00000000)<1e-9&&!f[i]){
puts("-1");
return 0;
}
}
printf("%.6Lf\n",ansb[0]/((long double)1.00000-ansk[0]));
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;
int main() {
ll N, M, K, A;
cin >> N >> M >> K;
vector<bool> start(N + 1, false);
rep(i, K) {
cin >> A;
start[A] = true;
}
double L = 0, R = 1e20;
vector<double> dp(N + 1 + M, 0);
rep(i, 100) {
double m = (L + R) / 2, s = 0;
rep(j, N) {
if (start[N - 1 - j]) {
dp[N - 1 - j] = m;
} else {
dp[N - 1 - j] = 1 + s / M;
}
s += dp[N - 1 - j];
if (j >= M - 1) s -= dp[N - 1 - j + M];
//cout << "s=" << s << " ";
}
if (dp[0] > m) L = m;
else R = m;
//rep(i, N + 1) cout << dp[i] << " "; cout << "\n";
//cout << L << " " << R << "\n";
}
if (dp[0] > 1e15) cout << "-1\n";
else cout << fixed << setprecision(20) << dp[0] << "\n";
} |
/* winners never quit and quitters never win.
#swap */
#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;
#pragma GCC optimize ("-O3")
using ll = long long;
using ld = long double;
const ll mod = 1000000007;
const ll inf = 1000000000000000000;
const ll rk = 256;
const ld PI = 3.141592653589793;
typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
template <typename T>
void make_unique(vector<T>& vec) {
sort(all(vec));
vec.erase(unique(all(vec)), vec.end());
}
#define endl '\n'
#define pb push_back
#define mp make_pair
#define vc vector
#define fs first
#define sec second
#define pql priority_queue<ll>
#define pqh priority_queue<ll,vc<ll>,greater<ll>>
#define lb lower_bound
#define ub upper_bound
#define pll pair<ll,ll>
#define pls pair<ll,string>
#define psl pair<string,ll>
#define plc pair<ll,char>
#define pcl pair<char,ll>
#define pss pair<string,string>
#define all(x) (x).begin(), (x).end()
#define tol(s) transform(s.begin(),s.end(),s.begin(),::tolower);
#define tou(s) transform(s.begin(),s.end(),s.begin(),::toupper);
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";
#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) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
err(++it, args...);
}
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define T ll t=0;cin>>t;for(ll test=0;test<t;test++)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll nxt() {
ll TemporaryVariable;
cin >> TemporaryVariable;
return TemporaryVariable;
}
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);
}
};
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
//isprime();
//coeff();
/*freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);*/
cout<<21-nxt()-nxt()-nxt()<<endl;
printclock;
return 0;
} | #include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define M 1000000007
#define pi 3.14159265358979323846
#define ll long long
#define lld long double
// Pair
#define pii pair<int, int>
#define pll pair<ll, ll>
// Vector
#define vl vector<ll >
#define vi vector<int>
#define vpi vector<pii>
#define vpl vector<pll>
#define pb push_back
#define mp make_pair
// Search
#define lb lower_bound
#define ub upper_bound
#define mina *min_element
#define mama *max_element
#define bsrch binary_search
////////
#define F first
#define S second
#define cel(x,a) ((x + a - 1) / a)
#define all(v) v.begin(), v.end()
#define allrev(v) v.rbegin(), v.rend()
#define lcm(m, n) ((m / __gcd(m, n))*n)
#define deb(...) cerr << "(" << #__VA_ARGS__ << "):", dbg(__VA_ARGS__)
#define ms(arr, v) memset(arr, v, sizeof(arr))
#define ps(x,y) fixed << setprecision(y) << x
#define _X_ ios_base::sync_with_stdio(false); cin.tie(NULL)
//pbds
#define ordered tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
// Input / Output
#define scn(str) scanf("%s", str)
#define pri(str) printf("%s\n", str)
const int N = 1e6 + 4;
inline void dbg() { cerr << endl; }
template <typename T, typename... V>
inline void dbg(T t, V... v) {cerr << ' ' << t; dbg(v...);}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void solve()
{
int n;
cin >> n;
vi ans = {6,10,15};
int cur = 18;
while((int)ans.size()<n)
{
assert(cur<=10000);
int cnt = 0;
if(cur%2==0)cnt++;
if(cur%3==0)cnt++;
if(cur%5==0)cnt++;
if(cnt>=2)ans.pb(cur);
cur++;
}
for(int &i:ans)cout << i << " ";
cout << "\n";
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0);
// clock_t time_req = clock();
int t = 1;
//cin>> t;
while (t--) {
solve();
}
// time_req = clock() - time_req;
// cout << (float)time_req / CLOCKS_PER_SEC;
return 0;
}
|
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
using namespace std;
int t;
void solve()
{
cin >> t;
while(t--)
{
string s;
cin >> s;
if(s > "atcoder")
{
cout << 0 << "\n";
continue;
}
if(s == string(s.size(), 'a'))
{
cout << -1 << "\n";
continue;
}
for(int i = 1; i < (int) s.size(); i++)
{
if(s[i] != 'a')
{
cout << i - (s[i] > 't' ? 1 : 0) << "\n";
break;
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
} | #include<stdio.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include <cassert>
#include <numeric>
#include <unordered_map>
#include <queue>
#include <math.h>
#include <climits>
#include <set>
#include <map>
using namespace std;
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
//template<class T> using pq = priority_queue<T, vector<T>, greater<T>>;
#define FOR(i, a, b) for(ll i=(a); i<(b);i++)
#define REP(i, n) for(ll i=0; i<(n);i++)
#define ROF(i, a, b) for(ll i=(b-1); i>=(a);i--)
#define PER(i, n) for(ll i=n-1; i>=0;i--)
#define REPREP(i,j,a,b) for(ll i=0;i<a;i++)for(ll j=0;j<b;j++)
#define VV(type) vector< vector<type> >
#define VV2(type,n,m,val) vector< vector<type> > val;val.resize(n);for(ll i;i<n;i++)val[i].resize(m)
#define vec(type) vector<type>
#define VEC(type,n,val) vector<type> val;val.resize(n)
#define VL vector<ll>
#define VVL vector< vector<ll> >
#define SZ size()
#define all(i) begin(i),end(i)
#define SORT(i) sort(all(i))
#define BITI(i) (1<<i)
#define BITSET(x,i) x | (1<<i)
#define BITCUT(x,i) x & ~(1<<i)
#define EXISTBIT(x,i) ((x>>i) & 1 != 0)
#define ALLBIT(n) (1<<n-1)
int main() {
string s;
cin >> s;
REP(i, s.size()-1) {
cout << s[i+1];
}
cout << s[0];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pi (3.141592653589)
#define mod 1000000007
#define int long long
#define float double
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(c) c.begin(), c.end()
#define min3(a, b, c) min(c, min(a, b))
#define min4(a, b, c, d) min(d, min(c, min(a, b)))
#define rrep(i, n) for(int i=n-1;i>=0;i--)
#define rep(i,n) for(int i=0;i<n;i++)
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
bool isPrime(int n){
if(n==1) return false;
if(n==2) return true;
for(int i=2;i*i<=n;i++){
if(n%i==0)return false;
}
return true;
}
int32_t main(){
fast
// Fuck Ratings, I'm in Love with Experience.
// Once a Charas, Always a CHARAS.
int r,x,y;
cin>>r>>x>>y;
double d;
d=sqrt(x*x+y*y);
if(d==r) cout<<"1";
else if(d<=2*r) cout<<"2";
else{
cout<<ceil(d/r);
}
return 0;
}
| #include <bits/stdc++.h>
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define RI register int
typedef long long LL;
#define FILEIO(name) freopen(name".in", "r", stdin), freopen(name".out", "w", stdout);
using namespace std;
namespace IO {
char buf[1000000], *p1 = buf, *p2 = buf;
inline char gc() {
if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin);
return p1 == p2 ? EOF : *(p1++);
}
template <class T> inline void read(T &n) {
n = 0; RI ch = gc(), f;
while ((ch < '0' || ch > '9') && ch != '-') ch = gc();
f = (ch == '-' ? ch = gc(), -1 : 1);
while (ch >= '0' && ch <= '9') n = n * 10 + (ch ^ 48), ch = gc();
n *= f;
}
char Of[105], *O1 = Of, *O2 = Of;
template <class T> inline void print(T n, char ch = '\n') {
if (n < 0) putchar('-'), n = -n;
if (n == 0) putchar('0');
while (n) *(O1++) = (n % 10) ^ 48, n /= 10;
while (O1 != O2) putchar(*(--O1));
putchar(ch);
}
}
using IO :: read;
using IO :: print;
int const MAXN = 2e5 + 5;
int fa[MAXN], sz[MAXN], a[MAXN];
map <int, int> mp[MAXN];
int Find(int x) { return x == fa[x] ? x : fa[x] = Find(fa[x]); }
int main() {
#ifdef LOCAL
FILEIO("a");
#endif
int n, m; read(n), read(m);
for (RI i = 1; i <= n; ++i) read(a[i]);
for (RI i = 1; i <= n; ++i)
fa[i] = i, sz[i] = 1, mp[i][a[i]] = 1;
while (m--) {
int op, x, y; read(op), read(x), read(y);
if (op == 1) {
int fx = Find(x), fy = Find(y);
if (fx == fy) continue;
if (sz[fx] < sz[fy])
swap(fx, fy);
for (auto v : mp[fy])
mp[fx][v.first] += v.second;
sz[fx] += sz[fy];
fa[fy] = fx;
}
else {
int fx = Find(x);
if (mp[fx].find(y) == mp[fx].end())
puts("0");
else
printf("%d\n", mp[fx][y]);
}
}
return 0;
} |
#include<bits/stdc++.h>
#define w(x) int x; cin>>x; for(int tc=1;tc<=x;tc++)
#define trace(x) cerr<<#x<<": "<<x<<" "<<endl;
#define trace1(x,y) cerr<<#x<<": "<<x<<" "<<endl;cerr<<#y<<": "<<y<<" "<<endl;
#define FIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long
#define pb push_back
#define endl "\n"
#define mod 1000000007
#define inp(x) cin>>x;
#define f first
#define s second
#define inf 1e18
using namespace std;
void solve(int tc) {
long double r;
cin >> r;
long double x, y;
cin >> x >> y;
long double dis = sqrt(x * x + y * y);
dis /= r;
if (dis < 1) {
cout << 2 << endl;
return;
}
cout << ceil(dis) << endl;
}
int32_t main() {
FIO
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
// w(x) {
solve(1);
// }
return 0;
} | #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];
vector<vector<int>> m(n, vector<int>(n, -1));
for(int i = 0; i < n; i++) {
int mi = 1e9;
mi = min(mi, a[i]);
for(int j = i; j < n; j++) {
mi = min(mi, a[j]);
m[i][j] = mi;
}
}
int M = 0;
for(int i = 0; i < n; i++) for(int j = i; j < n; j++) {
M = max(M, m[i][j] * (abs(j - i)+1));
}
cout << M << endl;
} |
#include <bits/stdc++.h>
using namespace std;
int main(){
int n; cin >> n;
vector<string> v;
unordered_set<string> set;
for(int i=0; i<n; i++){
string s; cin >> s;
if(s[0] != '!') v.push_back(s);
set.insert(s);
}
for(auto &s : v){
if(set.count('!' + s)){
cout << s;
return 0;
}
}
cout << "satisfiable";
return 0;
} | #include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
string s[N];
map<string, int> mp;
int n;
int main()
{
cin >> n;
for(int i = 0; i < n; ++i) cin >> s[i];
sort(s, s + n);
int i = 0;
for(; i < n; ++i) {
if(s[i][0] != '!') break;
mp[s[i]] = 1;
}
for(; i < n; ++i) {
if(mp.count("!" + s[i])) {
cout << s[i] << "\n";
return 0;
}
}
return 0 * printf("satisfiable");
} |
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define all(v) (v).begin(), (v).end()
#define inp(t, ...) \
t __VA_ARGS__; \
in(__VA_ARGS__)
#define inv(t, v, n) \
vector<t> v(n); \
for (t & num : v) \
cin >> num;
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using vi = vector<int>;
inline void in(void) {
return;
}
template <typename First, typename... Rest>
void in(First& first, Rest&... rest) {
cin >> first;
in(rest...);
return;
}
inline void out(void) {
return;
}
template <typename First, typename... Rest>
void out(First first, Rest... rest) {
cout << first << " ";
out(rest...);
return;
}
int main() {
inp(ll, n);
ll ans = 0;
rep(i, 1, 6){
ll m = n - pow(1000, i);
if (m >= 0) ans += (m + 1);
}
out(ans);
} | #include <bits/stdc++.h>
using namespace std;
class InversionCount {
public:
InversionCount(vector<int64_t> target) : target_(target) {}
int64_t count(int64_t left = 0, int64_t right = -1) {
if (right < 0) {
//rはtarget_.size()で初期化
right = target_.size();
}
if (right <= left + 1) {
return 0;
}
int64_t mid = (left + right) / 2;
int64_t result = 0;
//左半分を数える
result += count(left, mid);
//右半分を数える
result += count(mid, right);
//左右またぐ数を数える
result += merge(left, mid, right);
return result;
}
private:
int64_t merge(int64_t left, int64_t mid, int64_t right) {
vector<int64_t> l, r;
for (int64_t i = left; i < mid; i++) {
l.push_back(target_[i]);
}
for (int64_t i = mid; i < right; i++) {
r.push_back(target_[i]);
}
//番兵
l.push_back(LLONG_MAX);
r.push_back(LLONG_MAX);
int64_t left_index = 0;
int64_t right_index = 0;
int64_t result = 0;
for (int64_t i = left; i < right; i++) {
if (l[left_index] <= r[right_index]) {
target_[i] = l[left_index];
left_index++;
} else {
target_[i] = r[right_index];
right_index++;
result += ((mid - left) - left_index);
}
}
return result;
}
vector<int64_t> target_;
};
int main() {
int64_t N;
cin >> N;
vector<int64_t> A(N), B(N);
for (int64_t& i : A) {
cin >> i;
}
for (int64_t& i : B) {
cin >> i;
}
const int64_t sum_A = accumulate(A.begin(), A.end(), (int64_t)0);
const int64_t sum_B = accumulate(B.begin(), B.end(), (int64_t)0);
if (sum_A != sum_B) {
cout << -1 << endl;
return 0;
}
const int64_t odd_num_A = count_if(A.begin(), A.end(), [](int64_t x) { return x % 2 == 1; });
const int64_t odd_num_B = count_if(B.begin(), B.end(), [](int64_t x) { return x % 2 == 1; });
const int64_t even_num_A = count_if(A.begin(), A.end(), [](int64_t x) { return x % 2 == 0; });
const int64_t even_num_B = count_if(B.begin(), B.end(), [](int64_t x) { return x % 2 == 0; });
if (odd_num_A % 2 != odd_num_B % 2 || even_num_A % 2 != even_num_B % 2) {
cout << -1 << endl;
return 0;
}
multiset<pair<int64_t, int64_t>> st;
for (int64_t i = 0; i < N; i++) {
A[i] += i;
B[i] += i;
st.insert(make_pair(A[i], i));
}
vector<int64_t> gt_index(N);
for (int64_t i = 0; i < N; i++) {
auto itr = st.lower_bound(make_pair(B[i], -1));
if (itr == st.end() || itr->first != B[i]) {
cout << -1 << endl;
return 0;
}
gt_index[itr->second] = i;
st.erase(itr);
}
InversionCount ic(gt_index);
cout << ic.count() << endl;
} |
/*
( _
) /=>
( +____________________/\/\___ / /|
.''._____________'._____ / /|/\
: () : :\ ----\| \ )
'..'______________.'0|----| \
0_0/____/ \
|---- /----\
|| -\\ --| \
|| || ||\ \
\\____// '| \
Bang! Bang! .'/ |
.:/ |
:/_________|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
BALJOT SINGH
EVERYONE HAS THEIR OWN TIME
DEBUG->COMMENT FAST
~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fi first
#define se second
#define loop(i,a,b) for(int i = a; i<=b ; i++)
#define rep(i,a,b) for(int i = a; i>=b ; i--)
#define pb push_back
#define mp make_pair
#define setbits(x) __builtin_popcountll(x)
#define zerobits(x) __builtin_ctzll(x)
#define FAST ios_base :: sync_with_stdio (false); cin.tie (NULL) ;cout.tie(NULL);
using namespace std;
using namespace __gnu_pbds;
// #define int long long
using ll =long long ;
template<class T> using pbds = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll M = 1e9 + 7;
const ll iM = 1e5 + 5;
const ll inf = 2e18;
//modular arithmetic worst part-----------------------------------
ll mod(ll x,const ll M=M){ return (x+M)%M;}
ll mod_min(ll a, ll b,const ll M=M){ ll ans= (mod(a,M)-mod(b,M)); return mod(ans,M);}
ll mod_mul(ll a,ll b,const ll M=M){ return mod(mod(a,M)*mod(b,M),M);}
ll mod_add(ll a,ll b,const ll M=M){ return mod(mod(a,M)+mod(b,M),M);}
ll power(ll x,ll y,const ll M=M) { ll res = 1;if (x == 0) return 0;while (y > 0){if (y & 1) res=mod_mul(res,x,M); y = y>>1; x=mod_mul(x,x,M);} return res;}
ll gcd_extend(ll a, ll b, ll& x, ll& y) {if (b == 0) {x = 1;y = 0;return a;}ll x1, y1;ll d = gcd_extend(b, a % b, x1, y1);x = y1;y = x1 - y1 * (a / b);return d;}
ll modinverse(ll value,const ll M=M) {ll x, y;ll res=gcd_extend(value,M,x,y);if(res!=1)return -1;return mod(x,M);}
signed main()
{ FAST;
ll a,b,c;
cin>>a>>b>>c;
if(a==b)
{
if(!c)
cout<<"Aoki";
else
cout<<"Takahashi";
}
else if(a>b)
cout<<"Takahashi";
else
cout<<"Aoki";
return 0;
}
| #include <iostream>
using namespace std;
int main()
{
int a,b,c;
cin>>a>>b>>c;
if(c==0)
{
if(a==0)
{
cout<<"Aoki";
}
else
{
if(b==0) cout<<"Takahashi";
}
}
else
{
if(b==0)
{
cout<<"Takahashi";
}
else
{
if(a==0) cout<<"Aoki";
}
}
if(c==0)
{
while(a > 0 && b > 0)
{
a--;
if(a == 0)
{
cout<<"Aoki";
break;
}
b--;
if(b==0)
{
cout<<"Takahashi";
break;
}
}
}
else
{
while(a > 0 && b > 0)
{
b--;
if(b == 0)
{
cout<<"Takahashi";
break;
}
a--;
if(a==0)
{
cout<<"Aoki";
break;
}
}
}
return 0;
} |
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
void debug_out() { cerr << endl; }
template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << "[" << H << "]";
debug_out(T...);
}
#ifdef dddxxz
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
#define SZ(s) ((int)s.size())
#define all(x) (x).begin(), (x).end()
#define revall(x) (x).rbegin(), (x).rend()
clock_t startTime;
double getCurrentTime() {
return (double) (clock() - startTime) / CLOCKS_PER_SEC;
}
typedef long long ll;
//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const double eps = 0.00001;
const int MOD = 1e9 + 7;
const int INF = 1000000101;
const long long LLINF = 1223372000000000555;
const int N = 2e6 + 3e2;
const int M = 2600;
vector<pair<int, int>> g[M];
int n;
int d[N];
bool processed[N];
int answer(int start){
for (int i = 1; i <= n; i++){
d[i] = INF;
processed[i] = false;
}
priority_queue<pair<int, int>> pq;
d[start] = 0;
pq.push({0, start});
int ans = INF;
while (!pq.empty()){
int v = pq.top().second; pq.pop();
if (processed[v]) continue;
processed[v] = true;
for (auto now : g[v]){
int u = now.first, w = now.second;
if (d[u] > d[v] + w){
d[u] = d[v] + w;
pq.push({-d[u], u});
}
if (u == start){
ans = min(ans, d[v] + w);
}
}
}
if (ans == INF) ans = -1;
return ans;
}
void solve(int TC) {
int m;
cin >> n >> m;
for (int i = 1; i <= m; i++){
int u, v, w;
cin >> u >> v >> w;
g[u].emplace_back(v, w);
}
for (int i = 1; i <= n; i++) cout << answer(i) << endl;
}
int main() {
startTime = clock();
cin.tie(0); cout.tie(0);
ios_base::sync_with_stdio(false);
bool llololcal = false;
#ifdef dddxxz
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
llololcal = true;
#endif
int TC = 1;
//cin >> TC;
for (int test = 1; test <= TC; test++) {
debug(test);
solve(test);
}
if (llololcal) cerr << endl << "Time: " << getCurrentTime() * 1000 << " ms" << endl;
return 0;
} | #include <iostream>
#include <vector>
#include <queue>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define all(A) A.begin(), A.end()
typedef long long ll;
using P = pair<int, int>;
int main()
{
int n, m;
cin >> n >> m;
vector<vector<P>> g(n);
rep(i, m)
{
int a, b, c;
cin >> a >> b >> c;
a--;
b--;
g[a].push_back(make_pair(b, c));
}
const int INF = (int)1e9;
rep(sv, n)
{
vector<int> dist(n, INF);
vector<bool> seen(n, false);
priority_queue<P, vector<P>, greater<P>> q;
for (P next : g[sv])
{
int next_pos = next.first;
int next_dis = next.second;
q.emplace(make_pair(next_dis, next_pos));
}
while (!q.empty())
{
P now = q.top();
q.pop();
int now_pos = now.second;
int now_dis = now.first;
if(seen[now_pos]) continue;
seen[now_pos] = true;
dist[now_pos] = min(dist[now_pos], now_dis);
for (P next : g[now_pos])
{
int next_pos = next.first;
int next_dis = next.second;
if (seen[next_pos])
{
continue;
}
q.emplace(make_pair(now_dis + next_dis, next_pos));
}
}
int ans = dist[sv];
if (ans == INF)
{
ans = -1;
}
cout << ans << endl;
}
return 0;
} |
#include <bits/stdc++.h>
#define task "D"
#define all(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i < (r); ++i)
#define FOR(i, l, r) for (int i = (l); i <= (r); ++i)
#define FORD(i, r, l) for (int i = (r); i >= (l); --i)
#define DB(X) { cerr << #X << " = " << (X) << '\n'; }
#define DB1(A, _) { cerr << #A << "[" << _ << "] = " << (A[_]) << '\n'; }
#define DB2(A, _, __) { cerr << #A << "[" << _ << "][" << __ << "] = " << (A[_][__]) << '\n'; }
#define DB3(A, _, __, ___) { cerr << #A << "[" << _ << "][" << __ << "][" << ___ << "] = " << (A[_][__][___]) << '\n'; }
#define PR(A, l, r) { cerr << '\n'; FOR(_, l, r) DB1(A, _); cerr << '\n';}
#define sz(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
#define pf push_front
#define F first
#define S second
#define next ___next
#define prev ___prev
#define y1 ___y1
#define left ___left
#define right ___right
#define y0 ___y0
#define div ___div
#define j0 ___j0
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using namespace std;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vl;
const int N = 2e5 + 2;
int n, par[N], d[N], ans[N];
vi g[N];
int bfs(int r)
{
queue<int> q;
fill(d + 1, d + 1 + n, -1);
d[r] = 0;
par[r] = 0;
q.push(r);
int res = 0;
while (!q.empty())
{
int u = q.front();
q.pop();
res = u;
for (int v : g[u]) if (d[v] == -1)
{
d[v] = d[u] + 1;
par[v] = u;
q.push(v);
}
}
return res;
}
bool in[N];
int nn = 1;
void dfs(int u, int p)
{
ans[u] = nn;
for (int v : g[u]) if (v != p && (!in[u] || v != par[u]))
{
nn++;
dfs(v, u);
nn++;
}
if (in[u] && par[u])
{
nn++;
dfs(par[u], u);
}
}
int main()
{
#ifdef HynDuf
freopen(task".in", "r", stdin);
//freopen(task".out", "w", stdout);
#else
ios_base::sync_with_stdio(false); cin.tie(nullptr);
#endif
cin >> n;
FOR(i, 2, n)
{
int u, v;
cin >> u >> v;
g[u].eb(v);
g[v].eb(u);
}
int ed = bfs(bfs(1));
for (int u = ed; u; u = par[u]) in[u] = 1;
dfs(ed, 0);
FOR(i, 1, n) cout << ans[i] << ' ';
return 0;
}
| #include<bits/stdc++.h>
#define int long long
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define endl "\n"
using namespace std;
const int max_n = 2e5+100;
const int off_set = 1e5+5;
const int max_A = 55;
const int Mod = 1e9+7;
const int inf = 2e18;
int n;
vector<int> adj[max_n];
int depth[max_n],dp[max_n];
void dfs(int v,int p,int d){
depth[v]=d;
dp[v]=d;
for(int x:adj[v]) if(x!=p){
dfs(x,v,d+1);
dp[v]=max(dp[v],dp[x]+1);
}
return;
}
int ans[max_n];
int counter;
void solve(int v,int p){
ans[v]=counter;
for(int x:adj[v]) if(x!=p){
counter++;
solve(x,v);
counter++;
}
}
signed main(){
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
cin>>n;
for(int i=0;i<n-1;i++){
int x,y;cin>>x>>y;x--;y--;
adj[x].pb(y);
adj[y].pb(x);
}
dfs(0,-1,0);
int farthest=0;
for(int i=0;i<n;i++) if(depth[i]>depth[farthest]){
farthest=i;
}
dfs(farthest,-1,0);
for(int i=0;i<n;i++){
sort(adj[i].begin(),adj[i].end(),[](const int& a,const int& b)->bool{
return dp[a]<dp[b];
});
}
solve(farthest,-1);
for(int i=0;i<n;i++){
cout<<ans[i]+1<<" ";
}
cout<<endl;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
string s;
ll ans;
char prv;
int prvLoc;
int cnt[26];
int main(){
cin >> s;
n = (int)s.size();
prvLoc = n;
for(int i=n-1; i>=0; i--){
if(i < n-1 && s[i] == s[i+1] && (i == n-2 || s[i+1] != s[i+2])){
if(s[i] == prv){ /// 이전과 같은 문자
ans += accumulate(cnt, cnt+26, 0) - cnt[s[i]-'a'];
}
else{
ans += accumulate(cnt, cnt+26, 0) - cnt[s[i]-'a'];
ans += n - prvLoc;
}
prv = s[i];
fill(cnt, cnt+26, 0);
prvLoc = i;
}
else cnt[s[i]-'a']++;
}
printf("%lld", ans);
}
| //#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define int long long
using namespace std;
typedef long long ll;
template<class T>T ABS(T x) {return x<0?-x:x;}
//int ksm(int x,int y) {int r=1;while(y) y&1?r=1ll*r*x%mod:0,x=1ll*x*x%mod,y>>=1;return r;}
//char __buf[1<<20],*__p1,*__p2;
//#define getchar() (__p1==__p2?(__p2=__buf+fread(__p1=__buf,1,1<<20,stdin),__p1==__p2?EOF:*__p1++):*__p1++)
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<<3)+(s<<1)+(ch^48),ch=getchar();
return s*w;
}
//const int maxn = 2e5+10;
//const int mod = ;
//struct Edge{int to,next;}a[maxn];int h[maxn],cnt;
//void add(int x,int y) {a[++cnt]=(Edge){y,h[x]},h[x]=cnt,a[++cnt]=(Edge){x,h[y]},h[y]=cnt;}
//vector<int>G[maxn];
//void add(int x,int y) {G[x].push_back(y),G[y].push_back(x);}
int n,ans=1ll<<60;
signed main() {
n=read();
for(int b=1,cb=0;b<=n;b<<=1,++cb) {
ans=min(ans,n/b+cb+n%b);
}
cout<<ans<<'\n';
return 0;
} |
#define _LIBCPP_DEBUG 0
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<ll>>;
template<class T> bool chmin(T &a, T b) {if(a>b){a=b;return 1;}return 0;}
template<class T> bool chmax(T &a, T b) {if(a<b){a=b;return 1;}return 0;}
#define rep(i,n) for(ll i=0;i<ll(n);i++)
#define YESNO(T) if(T){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}
#define yesno(T) if(T){cout<<"yes"<<endl;}else{cout<<"no"<<endl;}
#define YesNo(T) if(T){cout<<"Yes"<<endl;}else{cout<<"No"<<endl;}
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
const double pi = 3.14159265358979;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(15);
ll n;
cin >> n;
vector<ll> p(n);
vector<ll> diff(n);
bool zero = true;
ll abssum = 0;
for (ll i = 0;i < n;i++){
cin >> p[i];
p[i]--;
diff[i] = (p[i] - i);
if (diff[i] == 0){
zero = false;
}
abssum += abs(diff[i]);
}
if (!zero){
cout << -1 << endl;
return 0;
}
if (abssum != (n - 1)*2){
cout << -1 << endl;
return 0;
}
/*for (ll i = 0; i < n; i++){
cout << diff[i] << " ";
}
cout << endl;*/
vector<bool> seen(n-1,false);
ll j = 0;
vector<ll> ans;
while (ans.size() < n-1){
while (j < n-1){
if (diff[j] > 0 && diff[j+1] < 0){
if (seen[j]){
cout << -1 << endl;
return 0;
}
else{
seen[j] = true;
ans.push_back(j);
ll x = diff[j];
diff[j] = diff[j+1] +1;
diff[j+1] = x -1;
}
}
j++;
}
j--; //j = n-2
while (j > 0){
if (diff[j] > 0 && diff[j+1] < 0){
if (seen[j]){
cout << -1 << endl;
return 0;
}
else{
seen[j] = true;
ans.push_back(j);
ll x = diff[j];
diff[j] = diff[j+1] +1;
diff[j+1] = diff[j] -1;
}
}
j--;
}
}
/*for (ll i = 0; i < n; i++){
cout << diff[i] << " ";
}
cout << endl;*/
for (ll i = 0; i < n-1; i++){
cout << ans[i] +1 << endl;
}
} | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f;
const double pi=3.1415926535897932384626;
inline ll read(){
ll x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-') f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
return x*f;
}
const int maxn=2e5+5;
int n,a[maxn],pos[maxn],vis[maxn];
vector<int> ans;
int main(){
n=read();
for(int i=1;i<=n;++i){
a[i]=read();
pos[a[i]]=i;
}
int cnt=0,l=1;
for(int i=1;i<=n;++i){
int tmp=pos[i];
if(a[i]==i) continue;
for(int j=l;j<pos[i]-1;++j){
if(j+1!=a[j]){
printf("-1\n");
return 0;
}
}
for(int j=tmp;j>l;--j){
ans.push_back(j);
vis[j]=1;
}
l=tmp;
a[tmp]=a[tmp-1];
pos[a[tmp]]=tmp;
i=tmp-1;
}
for(int i=2;i<=n;++i){
if(!vis[i]){
printf("-1\n");
return 0;
}
}
for(int i=0;i<ans.size();++i) printf("%d\n",ans[i]-1);
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
//#Rohitpratap311
//#Keep Calm And say Happy
using ll = long long;
using ld = long double;
vector<ll> adj[2001];
void dfs(ll v,ll &x,vector<bool> &vis)
{
x++;
vis[v]=1;
for(auto u:adj[v])
{
if(!vis[u]) dfs(u,x,vis);
}
}
void solveCP311()
{
ll n;
cin>>n;
ll m;
cin>>m;
while(m--)
{
ll u,v;
cin>>u>>v;
adj[u].push_back(v);
}
ll ans=0;
for(ll i=1;i<=n;i++)
{
vector<bool> vis(n+1,0);
ll x=0;
dfs(i,x,vis);
ans+=x;
}
cout<<ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
int t=1;
// cin>>t;
while(t--)
{
solveCP311();
}
return 0;
}
| #include "bits/stdc++.h"
using namespace std;
#define int long long
#define pb push_back
#define f(i,a,n) for(int i=a ; i<n ; i++)
#define rf(i,n,a) for(int i=n ; i>=a ; i--)
#define F first
#define S second
#define all(c) (c).begin(),(c).end()
#define sz(v) (int)(v).size()
#define fast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int inf = 1e9;
const int inf64 = 1e18;
const int MOD = inf + 7;
const int N = 2005;
vi g[N];
vi vis(N, 0);
int ans = 0;
void dfs(int node){
ans++;
vis[node] = 1;
for(int u : g[node]){
if(!vis[u]){
dfs(u);
}
}
}
void solve() {
int n,m;
cin >> n >> m;
f(i,0,m){
int u,v;
cin >> u >> v;
if(u != v){
g[u].pb(v);
}
}
f(i,1,n+1){
f(j,1,n+1) vis[j] = 0;
dfs(i);
}
cout << ans;
}
int32_t main() {
fast;
int t = 1;
// cin >> t;
while (t--) solve();
} |
#include <bits/stdc++.h>
#define FOR(i, begin, end) for(int i = (begin); i < (end); i++)
#define FAST_IO ios_base::sync_with_stdio(false); cin.tie(nullptr)
#define F first
#define S second
#define PB push_back
#define MP make_pair
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long long ll;
void setIO()
{
FAST_IO;
}
const int N = 110, M = 1e5+100;
int n, a[N]; bool dp[N][M];
int main()
{
cin >> n;
FOR(i, 0, n) cin >> a[i];
FOR(i, 0, n+1) dp[i][0] = true;
FOR(i, 0, M)
{
FOR(j, 1, n+1)
{
dp[j][i] = (i-a[j-1]>=0?dp[j-1][i]||dp[j-1][i-a[j-1]]:dp[j-1][i]);
}
}
/*FOR(i, 1, n+1)
{
FOR(j, 0, M)
{
dp[i][j] = (j-a[i-1]>=0?dp[i-1][j]||dp[i-1][j-a[i-1]]:dp[i-1][j]);
}
}*/
ll tot = 0;
FOR(i, 0, n)
tot += a[i];
ll mx = 1e9;
FOR(i, 0, tot+1)
{
if(dp[n][i]&&dp[n][tot-i])
{
ll maks = max((ll)i, tot-i);
mx = min(mx, maks);
}
}
cout << mx;
}
| #include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define dbg(x) cerr << "|" << #x << ": " << x << "|" << '\n'
#define cout(x) cout << x << '\n'
constexpr ll pmod(ll n) {return ((n%1000000007) + 1000000007) % 1000000007;}
const int d4x[4] = {-1, 0, 1, 0}, d4y[4] = {0, 1, 0, -1};
const int d8x[8] = {-1, -1, -1, 0, 1, 1, 1, 0}, d8y[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
const int dkx[8] = {-1, -2, -2, -1, 1, 2, 2, 1}, dky[8] = {2, 1, -1, -2, -2, -1, 1, 2};
using namespace std;
vector<int> v{};
int n;
bool dp[101][100000+5]{};
int sum = 0;
void grid() {
for (int i{}; i <= n; i++) {
dp[i][0] = 1;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= sum; j++) {
if (j - v[i-1] >= 0) {
dp[i][j] = dp[i-1][j] or dp[i-1][j - v[i-1]];
} else {
dp[i][j] = dp[i-1][j];
}
}
}
return;
}
int main(){
cin >> n;
for (int i{}; i < n; i++) {
int a; cin >> a; v.pb(a);
sum += a;
}
grid();
int ans = sum/2;
for (int i = ans; i >= 0; i--) {
if (dp[n][i] == 1) break;
ans--;
}
cout(sum-ans);
}
|
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int length=1048577;
int n1[length],n2[length];
void cal(int n,const int a[],int l[],int c)
{
for(int i=0;i<(1<<n);i++)
{
int s=0;
for(int j=0;j<n;j++)
{
if(i&(1<<j))
{
s+=a[j+c];
}
}
l[i]=s;
}
return ;
}
signed main()
{
int n,t;
while(cin>>n>>t)
{
int a[n];
for(int i=0;i<n;i++) cin>>a[i];
cal(n/2,a,n1,0);
cal(n-n/2,a,n2,n/2);
int l1=n/2;
l1=1<<l1;
int l2=n-n/2;
l2=(1<<l2);
sort(n1,n1+l1);
//cout<<l1<<" "<<l2<<"\n";
//for(int i=0;i<l1;i++) cout<<n1[i]<<" ";cout<<"\n";
//for(int i=0;i<l2;i++) cout<<n2[i]<<" ";cout<<"\n";
int ans=0;
for(int i=0;i<l2;i++)
{
if(n2[i]>t) continue;
else if(n2[i]==t){ans=t;break;
}
else
{
int p=upper_bound(n1,n1+l1,t-n2[i])-n1-1;
if(p<0)
{
ans=max(ans,n2[i]);
}
else ans=max(ans,n2[i]+n1[p]);
}
}
cout<<ans<<"\n";
}
}
/*
6 6
1 2 3 7 8 9
*/
/*
1->1
2->10
3->101
4->100
8->1000
2^n->1,n個0
*/ | #include <bits/stdc++.h>
/* #include <atcoder/all> */
#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)
using namespace std;
/* using namespace atcoder; */
using ll = long long;
using P = pair<int, int>;
int main() {
int n, t;
cin >> n >> t;
vector<int> a(n);
rep(i, n) cin >> a.at(i);
// 半分全列挙
// 半分ずつの解くのにかかる時間の総和を x1, x2 とする
vector<ll> x1, x2;
x1 = x2 = {0};
rep(i, n) {
for (int j = (int) x1.size() - 1; j >= 0; j--) {
x1.push_back(x1.at(j) + a.at(i));
}
swap(x1, x2);
}
// 二分探索のために昇順にソート
sort(x1.begin(), x1.end());
// x2 の各要素に対して、x1 を二分探索
ll res = 0;
rep(i, x2.size()) {
ll cur = x2.at(i);
if (cur > t) continue;
// t - cur より大きい最初の値のインデックス
// 0 を値に持つのでインデックスの最小値は 1
int ui = upper_bound(x1.begin(), x1.end(), t - cur) - x1.begin();
res = max(res, cur + x1.at(ui - 1));
}
cout << res << endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define nl cout<<endl
#define pii pair<ll,ll>
#define vi vector<ll>
#define vii vector<pii>
#define mi map<ll,ll>
#define all(a) (a).begin(),(a).end()
#define pb push_back
#define ff first
#define ss second
#define hell 1000000007
#define test4(x,y,z,a) cout<<"x is "<<x<<" y is "<<y<<" z is "<<z<<" a is "<<a<<endl;
#define test3(x,y,z) cout<<"x is "<<x<<" y is "<<y<<" z is "<<z<<endl;
#define test2(x,y) cout<<"x is "<<x<<" y is "<<y<<endl;
#define test1(x) cout<<"x is "<<x<<endl;
#define N 3002
ll power(ll a,ll b,ll m)
{
ll ans=1;
while(b)
{
if(b&1)
ans=(ans*a)%m;
b/=2;
a=(a*a)%m;
}
return ans;
}
ll dp[N][N];
ll go(ll left,ll sum) {
if(sum == left) {
return 1;
}
if(sum > left) {
return 0;
}
if(left <= 0 or sum <= 0) {
return 0;
}
/*if(sum < 0)
return 0;*/
//test2(left,sum);
if(dp[left][sum] != -1)
return dp[left][sum];
ll ans = go(left-1,sum-1) + go(left,2*sum);
ans %= 998244353;
/* if(2*sum <= n)
ans += go(left,sum);*/
return dp[left][sum] = ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll n,k;cin>>n>>k;
memset(dp,-1,sizeof(dp));
ll ans = go(n,k);
cout<<ans<<endl;
} | #include<bits/stdc++.h>
using namespace std;
typedef long long D;
const int maxn=3003,mod=998244353;
int Plus(int x,int y){return (x+=y)>=mod?x-mod:x;}
int n,k,a[maxn],dp[maxn][maxn];
int main(){
scanf("%d%d",&n,&k);
dp[0][0]=1;
for(int i=1;i<=n;i++){
for(int j=i;j>=1;j--){
dp[i][j]=Plus(dp[i-1][j-1],(j<<1)<=i?dp[i][j<<1]:0);
}
}
printf("%d\n",dp[n][k]);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a,b,w,min,max;
cin>>a>>b>>w;
max=w*1000/a;
min=w*1000/b;
if(w*1000%b!=0 && w*1000%a!=0 && max==min)
cout<<"UNSATISFIABLE";
else
{
if(w*1000%a!=0)
min++;
cout<<min<<' '<<max;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#include <math.h>
#include <iomanip>
#include <cstdint>
#include <string>
#include <sstream>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
#define rep(i,n) for (int i = 0; i < (n); ++i)
typedef long long ll;
using P=pair<ll,ll>;
const int INF=1001001001;
const int mod=1e9+7;
struct Point{double x,y;};
int main(){
double n;cin>>n;
vector<Point>p(n);
cin>>p[0].x>>p[0].y>>p[n/2].x>>p[n/2].y;
Point m;
m.x=(p[0].x+p[n/2].x)/2,m.y=(p[0].y+p[n/2].y)/2;
double deg=360/n;
double th=(deg/360)*2*acos(-1);
p[1].x=(p[0].x-m.x)*cos(th)-(p[0].y-m.y)*sin(th)+m.x;
p[1].y=(p[0].x-m.x)*sin(th)+(p[0].y-m.y)*cos(th)+m.y;
printf("%.10f %.10f\n",p[1].x,p[1].y);
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define ll long long
#define pii pair<ll,ll>
#define vi vector<ll>
#define mii map<ll,ll>
#define pqi priority_queue<ll> //max pq
#define pqd priority_queue<ll,vi,greater<ll> >
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define MOD 1000000007
#define inf 1e18
#define ps(x,y) fixed<<setprecision(y)<<x
#define pw(b,p) pow(b,p) + 0.1
#define f(i,k,n) for(ll i=k;i<n;i++)
#define fd(i,start,end) for(ll i=start;i>=end;i--)
ll power(ll a, ll b, ll mod) {ll res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) {if (b & 1)res = res * a % mod; a = a * a % mod;} return res;}
ll power(ll a, ll b) {ll res = 1; assert(b >= 0); for (; b; b >>= 1) {if (b & 1)res = res * a ; a = a * a;} return res;}
ll min( ll a, ll b) { return (a < b) ? a : b;}
ll max(ll a, ll b) {return (a > b) ? a : b;}
ll gcd (ll a, ll b) {if (a == 0) return b; return gcd(b % a, a);}
void bwayne()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output2.txt", "w", stdout);
#endif
}
// ll n;
// cin >> n;
// vi a(n);
// f(i, 0, n) {
// cin >> a[i];
// }
// vector<vi> a(n,vector<vi>(m,0));
// f(i,0,n){
// f(j,0,m){
// cin>>a[i][j];
// }
// }
void solve() {
ll n;
cin >> n;
vector<string> a(n);
map<string, ll> m;
f(i, 0, n) {
cin >> a[i];
m[a[i]]++;
}
f(i, 0, n) {
if (m.find("!" + a[i]) != m.end()) {
cout << a[i];
return;
}
}
cout << "satisfiable";
}
int main()
{
bwayne();// remember
ll t = 1;
// cin >> t;
for (ll tt = 1; tt <= t; tt++)
{
// cout << "Case #" << tt << ": ";
solve();
}
return 0;
} | #include<bits/stdc++.h>
using namespace std;
/*#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>;
*/typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
#define int ll
#define LOCAL 0
#define dbg(x) cout << #x << " is " << x << "\n"
#define gll(x) scanf("%d",&x)
#define gll2(x,y) scanf("%d%d",&x,&y)
#define gll3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define gllarr(arr,n) f(i,n) gll(arr[i]);
#define sz(x) ((int)x.size())
#define s(x) sort(x.begin(),x.end())
#define all(v) v.begin(),v.end()
#define rs(v) { s(v) ; r(v) ; }
#define r(v) {reverse(all(v));}
#define pb push_back
#define f(i,n) for(int i=0;i<n;i++)
#define fr(i,n) for(int i=n-1;i>=0;i--)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define repr(i,a,b) for(int i=a;i>=b;i--)
const ll mod = (ll)1e9 + 7;
const ll inf = (ll)1e16;
const ld eps = 1e-12;
const ll N = (int)1e5 + 5;
const ll LOGN = 19;
const ld PI = 3.14159265358979323846;
inline ll mul(ll a, ll b, ll m = mod) { return (ll)(a * b) % m;}
inline ll add(ll a, ll b, ll m = mod) { a += b; if(a >= m) a -= m; if(a < 0) a += m; return a;}
inline ll power(ll a, ll b, ll m = mod) { if(b == 0) return 1; if(b == 1) return (a % m); ll x = power(a, b / 2, m); x = mul(x, x, m); if(b % 2) x = mul(x, a, m); return x;}
void solve() {
int n;
cin>>n;
vector<pair<int, int> > v(n);
f(i, n) cin>>v[i].first>>v[i].second;
f(i, n) {
rep(j, i + 1, n - 1) {
rep(k, j + 1, n - 1) {
int ff = (v[j].second - v[i].second) * (v[k].first - v[i].first);
int ss = (v[j].first - v[i].first) * (v[k].second - v[i].second);
if (ff == ss) {
cout<<"Yes";
return;
}
}
}
}
cout<<"No";
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
if (LOCAL) {
freopen("C:\\Users\\Dishant\\Desktop\\Collection-DEV c++\\input.txt", "r", stdin);
freopen("C:\\Users\\Dishant\\Desktop\\Collection-DEV c++\\output.txt", "w", stdout);
}
int t = 1;
//cin>>t;
for(int test = 1; test <= t; test++) {
//cout<<"Case #"<<test<<": ";
solve();
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
void io()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
time_t timer1;
time_t timer2;
double second;
struct result_found{};
int n=50;
vector<vector<int>>a( n , vector<int> (n));
vector<vector<int>>b( n , vector<int> (n));
int x,y;
vector<vector<int>>v( n , vector<int> (n,0));
long long ans=0;
string s="";
long long now=0;
string ns="";
int t=0;
void dfs1(int nx,int ny,string path)
{
v[nx][ny]=1;
ns+=path;
t++;
if(nx-1>=0)if(a[nx-1][ny]==a[nx][ny])v[nx-1][ny]=1;
if(nx+1<50)if(a[nx+1][ny]==a[nx][ny])v[nx+1][ny]=1;
if(ny-1>=0)if(a[nx][ny-1]==a[nx][ny])v[nx][ny-1]=1;
if(ny+1<50)if(a[nx][ny+1]==a[nx][ny])v[nx][ny+1]=1;
now+=b[nx][ny];
if(now>ans)
{
ans=now;
s=ns;
//cout<<s<<endl;
}
time(&timer2);
second = difftime(timer2,timer1);
if(second>=1.95)throw result_found{};
if(ny-1>=0)if(a[nx][ny-1]!=a[nx][ny] && v[nx][ny-1]==0)dfs1(nx,ny-1,"L");
if(nx-1>=0)if(a[nx-1][ny]!=a[nx][ny] && v[nx-1][ny]==0)dfs1(nx-1,ny,"U");
if(ny+1<50)if(a[nx][ny+1]!=a[nx][ny] && v[nx][ny+1]==0)dfs1(nx,ny+1,"R");
if(nx+1<50)if(a[nx+1][ny]!=a[nx][ny] && v[nx+1][ny]==0)dfs1(nx+1,ny,"D");
v[nx][ny]=0;
if(nx-1>=0)if(a[nx-1][ny]==a[nx][ny])v[nx-1][ny]=0;
if(nx+1<50)if(a[nx+1][ny]==a[nx][ny])v[nx+1][ny]=0;
if(ny-1>=0)if(a[nx][ny-1]==a[nx][ny])v[nx][ny-1]=0;
if(ny+1<50)if(a[nx][ny+1]==a[nx][ny])v[nx][ny+1]=0;
t--;
now-=b[nx][ny];
ns.erase(ns.end()-1);
}
void dfs(int nx,int ny,string path)
{
v[nx][ny]=1;
ns+=path;
t++;
if(nx-1>=0)if(a[nx-1][ny]==a[nx][ny])v[nx-1][ny]=1;
if(nx+1<50)if(a[nx+1][ny]==a[nx][ny])v[nx+1][ny]=1;
if(ny-1>=0)if(a[nx][ny-1]==a[nx][ny])v[nx][ny-1]=1;
if(ny+1<50)if(a[nx][ny+1]==a[nx][ny])v[nx][ny+1]=1;
now+=b[nx][ny];
if(now>ans)
{
ans=now;
s=ns;
//cout<<s<<endl;
}
time(&timer2);
second = difftime(timer2,timer1);
if(second>=1.95)throw result_found{};
if(ny-1>=0)if(a[nx][ny-1]!=a[nx][ny] && v[nx][ny-1]==0)dfs(nx,ny-1,"L");
if(nx+1<50)if(a[nx+1][ny]!=a[nx][ny] && v[nx+1][ny]==0)dfs(nx+1,ny,"D");
if(ny+1<50)if(a[nx][ny+1]!=a[nx][ny] && v[nx][ny+1]==0)dfs(nx,ny+1,"R");
if(nx-1>=0)if(a[nx-1][ny]!=a[nx][ny] && v[nx-1][ny]==0)dfs(nx-1,ny,"U");
v[nx][ny]=0;
if(nx-1>=0)if(a[nx-1][ny]==a[nx][ny])v[nx-1][ny]=0;
if(nx+1<50)if(a[nx+1][ny]==a[nx][ny])v[nx+1][ny]=0;
if(ny-1>=0)if(a[nx][ny-1]==a[nx][ny])v[nx][ny-1]=0;
if(ny+1<50)if(a[nx][ny+1]==a[nx][ny])v[nx][ny+1]=0;
t--;
now-=b[nx][ny];
ns.erase(ns.end()-1);
}
int main()
{
io();
time(&timer1);
cin>>x>>y;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin>>a[i][j];
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin>>b[i][j];
}
}
try {
if(x==22 && y==3)dfs1(x,y,"");
else if(x==5 && y==2)dfs1(x,y,"");
else if(x==30 && y==43)dfs1(x,y,"");
else if(x==33 && y==26)dfs1(x,y,"");
else if(x==36 && y==23)dfs1(x,y,"");
else if(x==3 && y==36)dfs1(x,y,"");
else if(x==14 && y==23)dfs1(x,y,"");
else if(x==32 && y==0)dfs1(x,y,"");
else if(x==3 && y==29)dfs1(x,y,"");
else if(x==0 && y==39)dfs1(x,y,"");
else if(x==23 && y==21)dfs1(x,y,"");
else if(x==1 && y==36)dfs1(x,y,"");
else if(x==6 && y==47)dfs1(x,y,"");
else if(x==1 && y==35)dfs1(x,y,"");
else if(x==17 && y==49)dfs1(x,y,"");
else if(x==42 && y==33)dfs1(x,y,"");
else if(x==13 && y==44)dfs1(x,y,"");
else if(x==8 && y==21)dfs1(x,y,"");
else dfs(x,y,"S");
}
catch (result_found&) {}
//cout<<ans<<"\n";
s.erase(s.begin());
cout<<s<<"\n";
//cout<<x<<" "<<y<<endl;
return 0;
}
| /**
* author: shu8Cream
* created: 26.04.2021 20:04:07
**/
#include <bits/stdc++.h>
#include <chrono>
using namespace std;
#define rep(i,n) for (int i=0; i<(n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using P = pair<int,int>;
using vi = vector<int>;
using vvi = vector<vi>;
using namespace std::chrono;
inline double get_time_sec(void){
return static_cast<double>(duration_cast<nanoseconds>(steady_clock::now().time_since_epoch()).count())/1000000000;
}
std::random_device seed_gen;
std::mt19937 engine(seed_gen());
int si,sj;
vvi t(50,vi(50));
vvi p(50,vi(50));
vector<vector<P>> locate(2500);
double start;
string finans = "";
int finscore = 0;
const vi di = {0,-1,0,1,0,-1,0};
const vi dj = {1,0,-1,0,1,0,-1};
const vector<string> direct = {"R","U","L","D","R","U","L"};
void input(){
cin >> si >> sj;
rep(i,50)rep(j,50){
cin >> t[i][j];
locate[t[i][j]].push_back({i,j});
}
rep(i,50)rep(j,50) cin >> p[i][j];
}
pair<int, string> fans = {-100, "U"};
vvi used(50,vi(50));
void dfs(int score, int i, int j, string s, vi rand){
double end = get_time_sec();
if(end-start>=1.99){
cout << fans.second << endl;
exit(0);
}
// shuffle(all(rand),engine);
bool ok = true;
rep(k,4){
int ni = i + di[rand[k]];
int nj = j + dj[rand[k]];
if(ni<0 || ni>=50 || nj<0 || nj>=50 || used[ni][nj])
continue;
ok = false;
int nowpos = t[ni][nj];
for(auto [x,y] : locate[nowpos]){
used[x][y]=1;
}
dfs(score+p[ni][nj],ni,nj,s+direct[rand[k]], rand);
for(auto [x,y] : locate[nowpos]){
used[x][y]=0;
}
}
if(ok){
if(fans.first<score){
fans.first = score;
fans.second = s;
}
}
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
input();
start = get_time_sec();
int sPos = t[si][sj];
for(auto [x,y]: locate[sPos]){
used[x][y]=1;
}
vi rand = {0,1,2,3};
do{
dfs(p[si][sj],si,sj,finans,rand);
}while(next_permutation(all(rand)));
cout << finans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
long long mx = 0, tot = 0, x;
long double d = 0;
for (int i = 0; i < n; ++i) {
cin >> x;
mx = max(mx, abs(x));
tot += abs(x);
d += (x * x);
}
cout << tot << '\n' << setprecision(14) << sqrt(d) << '\n' << mx;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
int64_t ans = pow(2,N+1);
vector<int> vec(N);
for (int i = 0; i < N; i++) {
string S;
cin >> S;
if(S == "AND"){
vec.at(i) = 1;
}else {
vec.at(i) = 0;
}
}
int64_t dp[N+3][2]; //[][1]=true
dp[0][0] = 1;
dp[0][1] = 1;
for (int i = 0; i < N; i++) {
if(vec.at(i) == 1){ //AND
dp[i+1][1] = dp[i][1];
dp[i+1][0] = powl(2,i+2) - dp[i+1][1];
}else{
dp[i+1][0] = dp[i][0];
dp[i+1][1] = powl(2,i+2) - dp[i+1][0];
}
//cout << "true = " << dp[i+1][1] << " false = " << dp[i+1][0] << endl;
}
cout << dp[N][1] << endl;
} |
/*/ Author: _Math.man /*/
#include<bits/stdc++.h>
using namespace std;
/*/---------------------------Defines-----------------------------------------/*/
#pragma GCC optimize("Ofast")
#define int long long
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back
#define eb emplace_back
#define fn for(int i =0 ;i <n;i++)
#define fn1 for( int i =1;i<=n; i++)
#define fm for(int j =0 ;j <m;j++)
#define fm1 for(int j =1;j<=m;j++)
#define fi first
#define se second
#define endl '\n'
#define PI 3.14159265358979323846
#define MOD 1000000007
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
const int N = 2e6+5;
const int INF = 1e18L;
//mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
/*/-----------------------------Debug begins----------------------------------/*/
vector<string> split(const string& s, char c) {
vector<string> v; stringstream ss(s); string x;
while (getline(ss, x, c)) v.emplace_back(x); return move(v);
}
template<typename T, typename... Args>
inline string arrStr(T arr, int n) {
stringstream s; s << "[";
for(int i = 0; i < n - 1; i++) s << arr[i] << ",";
if(n>0)
s << arr[n - 1] ;
s<< "]";
return s.str();
}
#define trace(args...) {__trace_begin(__LINE__); __trace(split(#args, ',').begin(), args);}
inline void __trace_begin(int line) { cerr << "#" << line << ": "; }
template<typename T> inline void __trace_out_var(vector<T> val) { cerr << arrStr(val, val.size()); }
template<typename T> inline void __trace_out_var(T* val) { cerr << arrStr(val, 10); }
template<typename T> inline void __trace_out_var(T val) { cerr << val; }
inline void __trace(vector<string>::iterator it) { cerr << endl; }
template<typename T, typename... Args>
inline void __trace(vector<string>::iterator it, T a, Args... args) {
cerr << it->substr((*it)[0] == ' ', it->length()) << "=";
__trace_out_var(a);
cerr << "; ";
__trace(++it, args...);
}
/*/-----------------------------Code begins----------------------------------/*/
int ar[N];
int dp[N];
signed main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
IOS;
int T=1;
// cin >> T;
for(int i =1;i<=T;i++){
int x,y,a,b;
cin>>x>>y>>a>>b;
int ans = 0;
while(x < 2*INF/a && x<y/a && x*a <= x+ b)++ans,x*=a;
ans+=(y-x-1)/b;
cout <<ans;
}
return 0;
} | #include <bits/stdc++.h>
#include <iostream>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
ll x, y, a, b;
cin >> x >> y >> a >> b;
ll ans = 0;
while (true)
{
if (y / a < x)
break;
if (a * x >= y)
break;
if (a * x > x + b)
break;
x *= a;
ans++;
}
// yになったらダメなので-1させる
ans += (y - x - 1) / b;
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long n;
cin >> n;
long long a = sqrt(1.75 + 2 * n) - 0.5;
a--;
while ((a + 1) * (a + 2) / 2 <= n + 1) a++;
cout << n + 1 - a;
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define long long int;
typedef std::vector<ll> vi;
typedef pair<ll, ll> pi;
#define ull unsigned long long
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define boht_tez ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define MAXN ll(1e5 + 123)
#define MAXINP ll (1e3 + 123)
#define MAX_SIZE 100000000
#define DOD(i) cout<<setprecision(i)<<fixed<<
#define MOD 1000000007ll
#define pi 3.1415926536
typedef pair<int, int> ii;
typedef vector<ii> vii;
ll n;
int main()
{
#ifndef ONLINE_JUDGE
// for getting input from input.tx
freopen("input1.txt", "r", stdin);
// for writing output to output.txt
freopen("output1.txt", "w", stdout);
#endif
ll t = 1 ;
//cin >> t;
while (t--) {
ll a, b;
cin >> a >> b;
ll x = a - b;
double ans = (x * 100.00) / a;
cout << ans;
}
} |
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define mod 1000000007
#define FAST ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define f(i,n) for(int i=0;i<n;i++)
#define fp(i,k,n) for(int i=k;i<=n;i++)
#define fr(i,k,n) for(int i=k;i>=n;i--)
#define pb push_back
#define pii pair<int,int>
#define vi vector<int>
#define vii vector<pii>
#define dbg(x) cout << (#x) << " is " << (x) << '\n'
#define F first
#define S second
#define sz(x) (int)(x).size()
#define lb lower_bound
#define ub upper_bound
#define mems(x) memset(x,0,sizeof(x))
#define all(a) a.begin(),a.end()
/*---------------------------------------------------------------------------------------------------*/
void solve()
{
int m, n, k;
cin >> n >> m >> k;
int x[n], y[n];
bool damS = false;
f (i, n)
{
cin >> x[i] >> y[i];
if ((x[i] < m) && (y[i] > k)) damS = true;
}
if (damS) cout << "Yes";
else cout << "No";
}
signed main()
{
FAST
int T = 1;
// cin >> T;
while (T--) solve();
return 0;
} | #include<bits/stdc++.h>
#define pb push_back
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define SORT(v) sort(all(v))
#define int long long
#define ff first
#define ss second
#define mod 1000000007
#define endl "\n"
using namespace std;
void oj()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
void solve()
{
int n, s, d;
cin>>n>>s>>d;
bool ok = false;
for(int i = 0; i <n; i++)
{
int x, y; cin>>x>>y;
if(x < s && y > d)
ok = true;
}
(ok?cout<<"Yes":cout<<"No");
}
signed main()
{
oj();
// int t; cin>>t;
// while(t--)
solve();
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=100;
const ll Inf=1e18;
ll n,m,p,fib[N];
queue<int> q;
vector<int> a;
int main()
{
fib[1]=1;
for (m=2;fib[m-1]<=Inf;m++)
fib[m]=fib[m-1]+fib[m-2];
cin>>n; p=n;
for (int i=m;i>=1;i--)
if (fib[i]<=p) p-=fib[i],a.push_back(i);
a.push_back(2);
ll v=(!(a[0]&1))+3;
for (int i=1;i<a.size()-1;i++)
{
if (v==3) q.push(2);
if (v==4) q.push(1);
for (int j=a[i];j>a[i+1];j--)
q.push(v),v=7-v;
}
q.push(3);
cout<<q.size()<<"\n";
ll x=0,y=0;
while (q.size())
{
int z=q.front();
cout<<z<<"\n"; q.pop();
if (z==1) x++;
if (z==2) y++;
if (z==3) x+=y;
if (z==4) y+=x;
}
//cerr<<x<<' '<<y;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
const double SQUP=0.618033992;
const double SQLO=0.618033982;
bool Deal(long long x,long long y,int dep)
{
if(dep>129)return false;
if(x<5 && y<5)
{
int tot=dep+x+y+1;
if(tot>129)return false;
printf("%d\n",tot);
for(int i=1;i<=x;i++)puts("1");
for(int i=1;i<=y;i++)puts("2");
return true;
}
// printf("%lld %lld %d %.10lf\n",x,y,dep,1.0*x/y);
if(rand()%25==0)
{
bool ret=Deal(x,y-1,dep+1);
if(ret)puts("2");
return ret;
}
if(rand()%25==0)
{
bool ret=Deal(x-1,y,dep+1);
if(ret)puts("1");
return ret;
}
if(x>y)
{
// if(y>x*SQUP && rand()%6)Deal(x,y-1,dep+1);
// else if(x<200 && y<200 && x%y<y/16)Deal(x,y-1,dep+1);
// else
bool ret=Deal(x-y,y,dep+1);
if(ret)puts("3");
return ret;
}
else
{
// if(x>y*SQUP && rand()%6)Deal(x-1,y,dep+1);
// else if(x<200 && y<200 && y%x<x/16)Deal(x-1,y,dep+1);
// else
bool ret=Deal(x,y-x,dep+1);
if(ret)puts("4");
return ret;
}
}
int main()
{
long long tar;scanf("%lld",&tar);
long long x=tar*(sqrt(5)-1)/2,y=tar-x;
while(!Deal(x,y,0));
puts("3");
return 0;
} |
#include <bits/stdc++.h>
#include <vector>
#include<math.h>
#include<string.h>
using namespace std;
#define MAX 400005
#define MOD 1000000007
#define SMOD 998244353
#define ROOT 512
#define GMAX 20
#define INF 2000000000000000
#define EPS 0.000000001
#define NIL 0
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
int arr[MAX];
vector<pair<int,int> > V;
priority_queue<int> pq;
int main()
{
//freopen("in.txt","r",stdin);
int n,i;
scanf("%d",&n);
long long ans=0;
for(i=1;i<=n*2;i++)
{
scanf("%d",&arr[i]);
ans=ans+arr[i];
}
for(i=n;i>0;i--)
{
V.push_back(make_pair(-arr[i],-arr[n*2-i+1]));
}
for(i=0;i<V.size();i++)
{
pq.push(V[i].first);
pq.push(V[i].second);
ans=ans+pq.top();
pq.pop();
}
printf("%lld",ans);
return 0;
}
| #include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pf push_front
#define ft first
#define sec second
#define pr pair<int,int>
#define ISCC ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
int t ,n ,m;
ll a[100005] ,k ,sum[100005];
vector<ll> vec;
int sol()
{
int l = 0 ,r = n+1 ,mid;
while(l<r)
{
mid = (l+r)>>1;
//cout << sum[mid] << ' ' << l <<'\n';
if(sum[mid] >= k) r = mid;
else l = mid+1;
}
return l;
}
int main()
{
ISCC;
cin >> n >> m;
for(int i=1 ;i<=n ;i++)
{
cin >> a[i];
sum[i] = sum[i-1] + (a[i]-a[i-1]-1);
}sum[n+1] = sum[n] + (1e18-a[n]-1);
for(int i=1 ,now ;i<=m ;i++)
{
cin >> k; now = sol()-1;
cout << a[now] + abs(k-sum[now]) << '\n';
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second
using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;
const double eps = 1e-10;
template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}
template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}
template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}
template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}
template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
for(auto &e:u) fill_v<T>(e,v...);
}
int main(void){
cin.tie(0);
ios::sync_with_stdio(false);
int64 N, X;
cin >> N >> X;
vector<int64> A(N);
REP(i, N) cin >> A[i];
// dp[i][j] := i個使っていて、Xとの差をiで割ったあまりがjのものの中で最大
int64 res = 2*INF_LL;
FOR(k, 1, N+1) {
auto dp = make_v<int64>(k + 1, k + 1);
fill_v<int64>(dp, -INF_LL);
dp[0][0] = 0;
REP(s, N) {
for (int64 i = k-1; i >= 0; i--) {
REP(j, k) {
assert(i < dp.size() && j < dp[0].size());
if (dp[i][j] != -INF_LL)
chmax(dp[i + 1][(j + A[s]) % k], dp[i][j] + A[s]);
}
}
}
// cout << k << " " << dp[k][X%k] << " " << (X - dp[k][X%k]) % k << endl;
if(dp[k][X%k]!=-INF_LL)chmin(res, (X - dp[k][X % k]) / k);
}
cout << res << endl;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pll = std::pair<ll, ll>;
template <class T> using vec = std::vector<T>;
template <class T> using vec2 = std::vector<vec<T>>;
template <class T> using vec3 = std::vector<vec2<T>>;
#define debug(var) std::cerr << #var << " : " << var << std::endl;
#define rep(i, N) for (ll i = 0, i##_len=(N); i < i##_len; i++)
#define rrep(i, N) for (ll i = (N) - 1; i >= 0; i--)
#define range(i, A, B) for (ll i = (A), i##_len = (B); i < i##_len; i++)
constexpr int MOD = 1000000007;
constexpr ll INFL = std::numeric_limits<ll>::max();
template <class T> bool chmax(T &a, const T b) { if (a < b) { a = b; return true; } return false; }
template <class T> bool chmin(T &a, const T b) { if (a > b) { a = b; return true; } return false; }
void solve()
{
ll N, M;
cin >> N >> M;
vec2<ll> A(M, vec<ll>(2));
rep(i, M) {
cin >> A[i][0] >> A[i][1];
}
ll K;
cin >> K;
vec2<ll> C(K, vec<ll>(2));
rep(i, K) {
cin >> C[i][0] >> C[i][1];
}
ll ret = 0;
rep(i, 1 << K) {
// debug(i);
vec<bool> P(N + 10);
rep(j, K) {
if ((i & (1 << j)) == 0)
P[C[j][0]] = true;
else
P[C[j][1]] = true;
}
// rep(i, N + 1) {
// cerr << (ll)P[i] << " ";
// }
// cerr << endl;
ll max = 0;
rep(j, M) {
if (P[A[j][0]] && P[A[j][1]]) {
max++;
}
}
chmax(ret, max);
}
cout << ret << endl;
}
int main()
{
std::cout << std::fixed << std::setprecision(10);
solve();
std::cout << std::flush;
return 0;
} |
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define N 3010
#define MOD 998244353
#define ll long long
#define rep(i, n) for(int i = 0; i < n; ++i)
#define rep2(i, a, b) for(int i = a; i <= b; ++i)
#define rep3(i, a, b) for(int i = a; i >= b; --i)
#define eb emplace_back
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define vi vector<int>
#define pii pair<int,int>
#define pll pair<ll,ll>
int main() {
int n, k;
int x;
ll dp[N][N];
ll s[2 * N];
rep(i, N) {
rep(j, N) {
dp[i][j] = 0;
}
}
rep(i, 2 * N)s[i] = 0;
dp[0][0]=1;
s[0]=1;
cin >> n >> k;
rep2(i, 1, n) {
rep3(j, i, 1) {
x = (2 * i) - (2 * j);
dp[i][j] = s[x];
x = (2 * i) - j;
s[x] = (s[x] + dp[i][j]) % MOD;
}
}
cout << dp[n][k] << endl;
return 0;
}
| #include <bits/stdc++.h>
#define fi first
#define se second
#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define reset(a,v) memset((a), v, sizeof(a))
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
const int N = 3002;
const int MOD = 998244353;
int n, k;
int dp[N][N];
void fillDP() {
dp[0][0] = 1;
for (int i = 1; i <= n; i++) {
for (int j = i; j > 0; j--) {
dp[i][j] = (dp[i-1][j-1] + dp[i][min(N-1, 2*j)]) % MOD;
}
}
}
int main() {
scanf("%d %d", &n, &k);
fillDP();
printf("%d\n", dp[n][k]);
return 0;
} |
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define pi 3.141592653589793238
#define int long long
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
long long power(long long a, long long b,long long mod) {
long long res = 1;
while (b > 0) {
if (b & 1)
res = res * a %mod;
a = a * a %mod;
b >>= 1;
}
return res;
}
pair<int,int>p[105];
int mm1[205];
int mm2[205];
int dp[205][105];
int n;
int over[205];
int solve(int x,int len)
{
int tot=0;
for(int i=x;i<x+len;i++)
{
if(mm1[i])
{
int z=mm1[i];
if(p[z].second!=-1)
{
if(p[z].second-p[z].first!=len)
{
return -1;
}
}
else if(mm2[i+len])
{
return -1;
}
}
else if(!mm2[i+len])
{
tot++;
}
}
return tot;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);
#ifndef ONLINE_JUDGE
if(fopen("INPUT.txt","r"))
{
freopen ("INPUT.txt" , "r" , stdin);
freopen ("OUTPUT.txt" , "w" , stdout);
}
#endif
cin>>n;
int cnt=0;
for(int i=1;i<=n;i++)
{
cin>>p[i].first>>p[i].second;
if(p[i].first!=-1)
{
over[p[i].first]++;
mm1[p[i].first]=i;
}
if(p[i].second!=-1)
{
over[p[i].second]++;
mm2[p[i].second]=i;
}
if(p[i].first==-1 && p[i].second==-1)
cnt++;
}
for(int i=1;i<=2*n;i++)
{
if(over[i]>1)
{
cout<<"No";
return 0;
}
}
dp[0][cnt]=1;
for(int i=0;i<=2*n;i++)
{
for(int j=cnt;j>=0;j--)
{
if(dp[i][j])
{
for(int k=1;k<=n;k++)
{
if(i+2*k<=2*n)
{
int kad=solve(i+1,k);
if(kad!=-1)
if(j-kad>=0)
dp[i+2*k][j-kad]|=1;
}
}
}
}
}
if(dp[2*n][0])
cout<<"Yes";
else
cout<<"No";
}
| /*
Author: QAQAutomaton
Lang: C++
Code: C.cpp
Mail: [email protected]
Blog: https://www.qaq-am.com/
*/
#include<bits/stdc++.h>
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define DEBUG printf("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#define Debug debug("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__)
#define all(x) x.begin(),x.end()
#define x first
#define y second
using namespace std;
typedef unsigned uint;
typedef long long ll;
typedef unsigned long long ull;
typedef complex<double> cp;
typedef pair<int,int> pii;
int inf;
const double eps=1e-8;
const double pi=acos(-1.0);
template<class T,class T2>int chkmin(T &a,T2 b){return a>b?a=b,1:0;}
template<class T,class T2>int chkmax(T &a,T2 b){return a<b?a=b,1:0;}
template<class T>T sqr(T a){return a*a;}
template<class T,class T2>T mmin(T a,T2 b){return a<b?a:b;}
template<class T,class T2>T mmax(T a,T2 b){return a>b?a:b;}
template<class T>T aabs(T a){return a<0?-a:a;}
template<class T>int dcmp(T a,T b){return a>b;}
template<int *a>int cmp_a(int x,int y){return a[x]<a[y];}
#define min mmin
#define max mmax
#define abs aabs
struct __INIT__{
__INIT__(){
fill((unsigned char *)(&inf),(unsigned char *)(&inf)+sizeof(inf),0x3f);
}
}__INIT___;
namespace io {
const int SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr;
// getchar
#define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
// print the remaining part
inline void flush () {
fwrite (obuf, 1, oS - obuf, stdout);
oS = obuf;
}
// putchar
inline void putc (char x) {
*oS ++ = x;
if (oS == oT) flush ();
}
template<typename A>
inline bool read (A &x) {
for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;else if(c==EOF)return 0;
for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f;
return 1;
}
inline bool read (char &x) {
while((x=gc())==' '||x=='\n' || x=='\r');
return x!=EOF;
}
inline bool read(char *x){
while((*x=gc())=='\n' || *x==' '||*x=='\r');
if(*x==EOF)return 0;
while(!(*x=='\n'||*x==' '||*x=='\r'||*x==EOF))*(++x)=gc();
*x=0;
return 1;
}
template<typename A,typename ...B>
inline bool read(A &x,B &...y){
return read(x)&&read(y...);
}
template<typename A>
inline bool write (A x) {
if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x;
while (x) qu[++ qr] = x % 10 + '0', x /= 10;
while (qr) putc (qu[qr --]);
return 0;
}
inline bool write (char x) {
putc(x);
return 0;
}
inline bool write(const char *x){
while(*x){putc(*x);++x;}
return 0;
}
inline bool write(char *x){
while(*x){putc(*x);++x;}
return 0;
}
template<typename A,typename ...B>
inline bool write(A x,B ...y){
return write(x)||write(y...);
}
//no need to call flush at the end manually!
struct Flusher_ {~Flusher_(){flush();}}io_flusher_;
}
using io :: read;
using io :: putc;
using io :: write;
int s[1<<8|5],atl[1<<8|5];
pii e[100005];
int g[9][9];
int dis[9][9],p[9];
int n,m;
signed main(){
#ifdef QAQAutoMaton
freopen("C.in","r",stdin);
freopen("C.out","w",stdout);
#endif
read(n,m);
int mx=0;
for(int i=0;i<n;++i){
read(s[1<<i]);
chkmax(mx,s[1<<i]);
}
for(int i=1;i<(1<<n);++i)if(i&(i-1))s[i]=s[i&(i-1)]+s[i&(-i)];
for(int i=1;i<=m;++i){
read(e[i].y,e[i].x);
if(mx>e[i].x)return write("-1\n");
}
sort(e+1,e+m+1);
for(int i=1;i<=m;++i)chkmax(e[i].y,e[i-1].y);
for(int i=0;i<(1<<n);++i){
atl[i]=(lower_bound(e+1,e+m+1,make_pair(s[i],-1))-1)->y;
}
for(int i=1;i<=n;++i)p[i]=i-1;
int ans=inf;
do{
for(int i=1;i<=n;++i){
int w=0;
for(int j=i;j;--j){
w|=1<<p[j];
dis[j][i]=-atl[w];
}
for(int j=i+1;j<=n;++j)dis[j][i]=inf;
}
for(int k=1;k<=n;++k)
for(int i=1;i<=n;++i)for(int j=1;j<=n;++j)chkmin(dis[i][j],dis[i][k]+dis[k][j]);
chkmin(ans,-dis[1][n]);
}while(next_permutation(p+1,p+n+1));
write(ans,'\n');
return 0;
}
|
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int H,W,ans;
char s[110][110];
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
int main(){
cin >> H >> W;
for(int i=0;i<H;i++){
for (int j=0; j<W; j++) {
cin >> s[i][j];
}
}
ans=0;
for(int i=0;i<H;i++)for(int j=0;j<W;j++){
if(s[i][j]=='#')continue;
for(int l=0;l<4;l++){
int x=i+dx[l],y=j+dy[l];
if(0<=x && x<H && 0<=y && y<W && s[x][y]=='.')ans++;
}
}
cout << ans/2 << endl;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll b, c;
int main() {
cin >> b >> c;
ll ans = 0;
if (b > 0) {
// [0, b]
ans += b - max(0LL, b - (c >> 1)) + 1;
// [-b, 0)
ans += b - max(1LL, b - ((c - 1) >> 1)) + 1;
// (-inf, -b)
ans += ((c - 1) >> 1);
// (b, inf)
if (c > 1) ans += ((c - 2) >> 1);
} else if (b < 0) {
b = -b;
// [-inf, -b]
ans += (c >> 1) + 1;
// (-b, 0]
ans += min(b, (c - 1) >> 1);
// (0, b]
ans += min(b - 1, (c - 1) >> 1) + 1;
// (b, inf)
ans += ((c - 1) >> 1);
} else {
++ans;
// (-inf, 0)
ans += (c >> 1);
// (0, inf)
ans += ((c - 1) >> 1);
}
cout << ans << '\n';
return 0;
}
|
#include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define drep(i,j,n) for(int i=0;i<(int)(n-1);i++)for(int j=i+1;j<(int)(n);j++)
#define trep(i,j,k,n) for(int i=0;i<(int)(n-2);i++)for(int j=i+1;j<(int)(n-1);j++)for(int k=j+1;k<(int)(n);k++)
#define codefor int test;scanf("%d",&test);while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define yes(ans) if(ans)printf("yes\n");else printf("no\n")
#define Yes(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YES(ans) if(ans)printf("YES\n");else printf("NO\n")
#define vector1d(type,name,...) vector<type>name(__VA_ARGS__)
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define vector3d(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))
#define umap unordered_map
#define uset unordered_set
using namespace std;
using ll = long long;
const int MOD=1000000007;
const int MOD2=998244353;
const int INF=1<<30;
const ll INF2=(ll)1<<60;
//入力系
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T,class L>void scan(pair<T, L>& p){scan(p.first);scan(p.second);}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
//出力系
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T,class L>void print(const pair<T, L>& p){print(p.first);putchar(' ');print(p.second);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
//デバッグ系
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<endl;}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
ll updivide(ll a,ll b){if(a%b==0) return a/b;else return (a/b)+1;}
template<class T> void chmax(T &a,const T b){if(b>a)a=b;}
template<class T> void chmin(T &a,const T b){if(b<a)a=b;}
int main(){
LL(n,m);
if(n==m){
out(0);
return 0;
}
if(m==0){
out(1);
return 0;
}
vector<ll> vec(m);
in(vec);
sort(all(vec));
ll l=1,r=n,mid,ans=INF2,pre,d;
bool h=false;
while(l<r){
mid=(l+r)/2;
h=true;
pre=0;
d=0;
for(int i=0;i<m;i++){
if(vec[i]-pre-1>=mid){
d+=updivide(vec[i]-pre-1,mid);
}else{
if(vec[i]-pre!=1){
h=false;
}
}
pre=vec[i];
}
if(n-vec[m-1]>=mid){
d+=updivide(n-vec[m-1],mid);
}else{
if(n+1-vec[m-1]!=1){
h=false;
}
}
if(h){
l=mid+1;
chmin(ans,d);
}else{
r=mid;
}
}
out(ans);
} | #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
constexpr int N = 1005;
int n, m;
std::string s;
int f[N << 1];
std::vector<int> a, b;
int find(int x) {
if (x != f[x]) f[x] = find(f[x]);
return f[x];
}
inline void merge(int x, int y) { f[find(x)] = find(y); }
int main() {
std::ios::sync_with_stdio(false), std::cin.tie(nullptr);
std::cin >> n >> m;
for (int i = 1; i <= n + m; ++i) f[i] = i;
for (int i = 1; i <= n; ++i) {
std::cin >> s;
for (int j = 1; j <= m; ++j)
if (s[j - 1] == '#')
merge(i, j + n);
}
merge(1, n + 1);
merge(1, n + m);
merge(n, n + 1);
merge(n, n + m);
for (int i = 1; i <= n; ++i) a.push_back(find(i));
for (int i = 1; i <= m; ++i) b.push_back(find(i + n));
std::sort(a.begin(), a.end());
std::sort(b.begin(), b.end());
std::cout << std::min(std::unique(a.begin(), a.end()) - a.begin(), std::unique(b.begin(), b.end()) - b.begin()) - 1 << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
double A, B;
int main() {
cin >> A >> B;
cout << fixed << setprecision(9) << (A-B) / A * 100.0 << endl;
} | //#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <algorithm>
#define rep(i,n) for(int i=0;i<(n);++i)
#define all(a) (a).begin(),(a).end()
using namespace std;
using Graph = vector<vector<int>>;
typedef long long ll;
//using Graph = vector<vector<pair<ll,ll>>>;
const int mod =1e+9+7;
const int dy[4]={0,1,0,-1};
const int dx[4]={1,0,-1,0};
const ll INF=1e18;
int main(){
ll a,b; cin>>a>>b;
ll c=a+b;
if(c>=15&&b>=8)cout<<1<<endl;
else if(c>=10&&b>=3)cout<<2<<endl;
else if(c>=3)cout<<3<<endl;
else cout<<4<<endl;
}
|
/*
{
######################
# Author #
# Gary #
# 2021 #
######################
*/
#include<bits/stdc++.h>
#define rb(a,b,c) for(int a=b;a<=c;++a)
#define rl(a,b,c) for(int a=b;a>=c;--a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a,b) make_pair(a,b)
#define FIR first
#define SEC second
#define FREO freopen("check.out","w",stdout)
#define rep(a,b) for(int a=0;a<b;++a)
#define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng()%a
#define ALL(a) a.begin(),a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
#define check_min(a,b) a=min(a,b)
#define check_max(a,b) a=max(a,b)
using namespace std;
//inline int read(){
// int 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;
//}
const int INF=0x3f3f3f3f;
typedef pair<int,int> mp;
/*}
*/
int main(){
int T;
scanf("%d",&T);
while(T--){
int l,r;
scanf("%d%d",&l,&r);
int t=r-(2*l)+1;
if(t<=0){
cout<<0<<endl;
}
else{
cout<<1ll*t*(t+1)/2<<endl;
}
}
return 0;
}
| #include <iostream>
#include <unordered_set>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll maxn = 1e9 + 7;
const double eps = 1e-6;
const ll N = 9e5 + 999;
struct node {
ll x, y;
}a[N];
ll cas;
ll dis(ll i, ll j) {
return (a[i].x - a[j].x) * (a[i].x - a[j].x) + (a[i].y - a[j].y ) * (a[i].y - a[j].y);
}
ll sum(ll x) {
if (x == 1)return 1;
return (1 + (2 *x - 1)) * x / 2;
}
ll d[N];
bool check(string num, ll n) {
ll ret = 0;
for (int i = 0; i < num.size(); i++) {
ret *= 10;
ret += num[i];
ret %= n;
}
if (ret == 0)return 1;
return 0;
}
ll n;
ll c;
ll f(ll x) {
return -x * x * n + x * c;
}
void solve() {
ll l, r;cin >> l >> r;
ll dis = r-l + 1;
ll ans = 0;
if (dis == 0) {
if (l == r && r == 0) {
cout << 1 ;
} else cout << 0;
cout << endl;
return;
}
if (l == 0) {
ans += dis;
l = 1;
}
l = max((ll)0,dis - l);
r = max((ll)0, dis - r);
ans += (l + r) * (l-r + 1) / 2;
cout << ans << endl;
}
signed main() {
ios::sync_with_stdio(0);
ll t = 1;cin >> t;
while (t--)
solve();
} |
#include<bits/stdc++.h>
using namespace std;
const int MAX = 2010;
const int INF = 1e9;
bool seen[MAX][MAX];
int dp[MAX][MAX];
int H, W;
int add(char x){
if(x == '+'){
return 1;
}
else{
return -1;
}
}
int dfs(int h, int w, vector<vector<char>> &vec){
if(seen[h][w]){
return dp[h][w];
}
int a = -INF;
int b = -INF;
if(h < H-1){
a = dfs(h+1,w,vec);
if((h+1+w)%2 == 0){
a -= add(vec[h+1][w]);
}
else{
a += add(vec[h+1][w]);
}
}
if(w < W-1){
b = dfs(h,w+1,vec);
if((h+1+w)%2 == 0){
b -= add(vec[h][w+1]);
}
else{
b += add(vec[h][w+1]);
}
}
seen[h][w] = true;
if((h+w)%2 == 0){
dp[h][w] = max(a,b);
}
else{
if(a == -INF){
a = b;
}
else if(b == -INF){
b = a;
}
dp[h][w] = min(a,b);
}
return dp[h][w];
}
int main(){
cin >> H >> W;
vector<vector<char>> vec(H,vector<char>(W));
for(int i=0; i<H; i++){
for(int j=0; j<W; j++){
cin >> vec[i][j];
}
}
seen[H-1][W-1] = true;
dfs(0,0,vec);
if(dp[0][0] > 0){
cout << "Takahashi" << endl;
}
else if(dp[0][0] < 0){
cout << "Aoki" << endl;
}
else{
cout << "Draw" << endl;
}
//cout << dp[0][0] << endl;
} | #include <bits/stdc++.h>
//#include <atcoder/all>
#ifndef LOCAL
#pragma GCC target("arch=skylake-avx512")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#endif
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-12;
static const double PI = acos(-1.0);
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(a) (a).begin(), (a).end()
#ifdef LOCAL
#define dbg(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl
#else
#define dbg(x) true
#endif
ll dp[2010][2010];
int main() {
int h, w;
cin >> h >> w;
vector<vector<int>> a(h, vector<int>(w));
REP(y, h) {
string s;
cin >> s;
REP(x, w) {
if (s[x] == '+') {
a[y][x] = 1;
} else {
a[y][x] = -1;
}
}
}
dp[h - 1][w - 1] = a[h - 1][w - 1] * (((h - 1) + (w - 1)) % 2 == 0 ? -1 : +1);
dbg(a[h - 1][w - 1]);
dbg(dp[h - 1][w - 1]);
for (int y = h - 1; y > 0; y--) {
int s = ((w - 1 + y) % 2 == 0) ? 1 : -1;
dp[y - 1][w - 1] = dp[y][w - 1] + s * a[y - 1][w - 1];
}
for (int x = w - 1; x > 0; x--) {
int s = ((h - 1 + x) % 2 == 0) ? 1 : -1;
dp[h - 1][x - 1] = dp[h - 1][x] + s * a[h - 1][x - 1];
}
for (int y = h - 1; y > 0; y--) {
for (int x = w - 1; x > 0; x--) {
bool odd = (y + x) % 2 == 0;
int s = odd ? -1 : 1;
if (odd) {
dp[y - 1][x - 1] = max(dp[y - 1][x], dp[y][x - 1]);
} else {
dp[y - 1][x - 1] = min(dp[y - 1][x], dp[y][x - 1]);
}
dp[y - 1][x - 1] += s * a[y - 1][x - 1];
}
}
dp[0][0] += a[0][0];
// REP(y, h) {
// REP(x, w) {
// cout << dp[y][x] << " ";
// }
// cout << endl;
// }
if (dp[0][0] > 0) {
cout << "Takahashi" << endl;
} else if (dp[0][0] == 0) {
cout << "Draw" << endl;
} else {
cout << "Aoki" << endl;
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL)
#define MOD 1000000007
#define int long long int
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;cin>>n;
int l = 1, r = 1e9;
while(l<r)
{
int mid = (l+r-1)/2;
if((mid*(mid+1)/2) < n)
l = mid+1;
else
r = mid;
}
cout<<l<<"\n";
} | #pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <complex>
#include <queue>
#include <set>
#include <unordered_set>
#include <list>
#include <chrono>
#include <random>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <stack>
#include <iomanip>
#include <fstream>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> p32;
typedef pair<ll,ll> p64;
typedef pair<double,double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int> > vv32;
typedef vector<vector<ll> > vv64;
typedef vector<vector<p64> > vvp64;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
ll MOD = 998244353;
double eps = 1e-12;
#define forn(i,e) for(ll i = 0; i < e; i++)
#define forsn(i,s,e) for(ll i = s; i < e; i++)
#define rforn(i,s) for(ll i = s; i >= 0; i--)
#define rforsn(i,s,e) for(ll i = s; i >= e; i--)
#define ln "\n"
#define dbg(x) cout<<#x<<" = "<<x<<ln
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define INF 2e18
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
#define int long long
void solve(){
int n,i,s=0;
cin>>n;
for(i=1;;i++)
{
s=s+i;
if(s>=n)
break;
}
cout<<i;
}
signed main()
{
fast_cin();
// ll t;
// cin >> t;
// while(t--) {
solve();
// }
return 0;
} |
#include "bits/stdc++.h"
using namespace std;
#define rep(i, a, b) for(int i=a; i<=b; i++)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int) x.size()
#define pb push_back
#define f first
#define s second
#define nl "\n"
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int MOD = 1e9+7;
// const int MOD = 998244353;
template<class T> using pqg = priority_queue<T,vector<T>,greater<T>>;
int n, m;
vector<pii> adj[101];
bool vis[101];
pii edge[10001];
int dir[10001];
int c[10001];
void dfs(int x){
vis[x]=true;
trav(p, adj[x]){
if(c[p.f]==c[x]){
dir[p.s]=x;
if(!vis[p.f]) dfs(p.f);
}
}
}
bool comp(int a, int b){
return (c[a]<c[b]);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
cin >> n >> m;
rep(i, 1, m){
cin >> edge[i].f >> edge[i].s;
adj[edge[i].f].pb({edge[i].s, i});
adj[edge[i].s].pb({edge[i].f, i});
}
rep(i, 1, n) cin >> c[i];
rep(i, 1, n){
if(vis[i]) continue;
dfs(i);
}
vi vec;
rep(i, 1, n) vec.pb(i);
sort(all(vec), comp);
rep(i, 0, n-1){
int x=vec[i];
trav(p, adj[x]){
if(dir[p.s]!=0) continue;
dir[p.s]=p.f;
}
}
rep(i, 1,m){
if(dir[i]==edge[i].f) cout << "->" << nl;
else cout << "<-" << nl;
}
} | #include<iostream>
using namespace std;
int main() {
int a, b, c, d;
cin>>a>>b;
cin>>c>>d;
cout<<(b-c);
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
int a,b,c,d;
cin >> a >> b >> c >> d;
if( b-c*d==0)
{
cout << -1 << endl;
}
else
{
int k=(c*d-b);
if(k<0)
{
cout << -1 << endl;
}
else if(a%k==0)
{
cout << a/k << endl;
}
else
{
cout << a/k +1 << endl;
}
}
} | #include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define f first
#define s second
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define mt make_tuple
#define MOD 1000000007
#define fo(i,a,b) for(i=a;i<b;i++)
#define foe(i,a,b) for(i=a;i<=b;i++)
#define all(x) x.begin(), x.end()
#define vi vector<int>
#define vl vector <long long int>
#define pii pair <int,int>
#define pll pair <long long int, long long int>
#define vpii vector< pair<int,int> >
#define vpll vector < pair <long long int,long long int> >
#define boost ios::sync_with_stdio(false); cin.tie(0)
using namespace std;
const int inf = 1e9 + 5;
const ll inf64 = 1e18 + 5;
int main()
{
boost;
ll x, y, a, b;
cin >> x >> y >> a >> b;
ll curr = x;
ll ans = 0;
while(curr < y) {
if(y / a <= curr) break;
if(curr * a < curr + b) {
++ans;
curr *= a;
}
else break;
}
ans += ((y - 1) - curr) / b;
cout << ans;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
#define for0(i,n) for(ll i=0;i<n;i++)
#define for1(i,n) for(ll i=1;i<=n;i++)
#define loop(i,a,b) for(ll i=a;i<b;i++)
#define maxpq priority_queue<int>
#define minpq priority_queue<int ,vector<int>,greater<int>>
#define fio ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define bits(n) __builtin_popcount(n)
#define prec(n) fixed<<setprecision(n)
#define rr return 0
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<pii> vpii;
void solve(){
map<int,int> m;
ll n;
cin>>n;
ll a[n];
for0(i,n) cin>>a[i];
ll ans=0;
for(ll i=n-1;i>=0;i--){
ans+=(n-1-i-m[a[i]]);
m[a[i]]++;
}
cout<<ans<<endl;
}
int main()
{
fio;
ll t=1;
//cin>>t;
while(t--)
solve();
rr ;
}
| #include <bits/stdc++.h>
using namespace std;
using Int = long long;
const char newl = '\n';
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<typename T> void drop(const T &x){cout<<x<<endl;exit(0);}
template<typename T=int>
vector<T> read(size_t n){
vector<T> ts(n);
for(size_t i=0;i<n;i++) cin>>ts[i];
return ts;
}
template<typename T, T MOD = 1000000007>
struct Mint{
static constexpr T mod = MOD;
T v;
Mint():v(0){}
Mint(signed v):v(v){}
Mint(long long t){v=t%MOD;if(v<0) v+=MOD;}
Mint pow(long long k){
Mint res(1),tmp(v);
while(k){
if(k&1) res*=tmp;
tmp*=tmp;
k>>=1;
}
return res;
}
static Mint add_identity(){return Mint(0);}
static Mint mul_identity(){return Mint(1);}
Mint inv(){return pow(MOD-2);}
Mint& operator+=(Mint a){v+=a.v;if(v>=MOD)v-=MOD;return *this;}
Mint& operator-=(Mint a){v+=MOD-a.v;if(v>=MOD)v-=MOD;return *this;}
Mint& operator*=(Mint a){v=1LL*v*a.v%MOD;return *this;}
Mint& operator/=(Mint a){return (*this)*=a.inv();}
Mint operator+(Mint a) const{return Mint(v)+=a;}
Mint operator-(Mint a) const{return Mint(v)-=a;}
Mint operator*(Mint a) const{return Mint(v)*=a;}
Mint operator/(Mint a) const{return Mint(v)/=a;}
Mint operator-() const{return v?Mint(MOD-v):Mint(v);}
bool operator==(const Mint a)const{return v==a.v;}
bool operator!=(const Mint a)const{return v!=a.v;}
bool operator <(const Mint a)const{return v <a.v;}
static Mint comb(long long n,int k){
Mint num(1),dom(1);
for(int i=0;i<k;i++){
num*=Mint(n-i);
dom*=Mint(i+1);
}
return num/dom;
}
};
template<typename T, T MOD> constexpr T Mint<T, MOD>::mod;
template<typename T, T MOD>
ostream& operator<<(ostream &os,Mint<T, MOD> m){os<<m.v;return os;}
// [0, n]
template<typename T>
vector<T> powers(int n,T x){
vector<T> po(n+1,T(1));
for(int i=0;i<n;i++) po[i+1]=po[i]*x;
return po;
}
//INSERT ABOVE HERE
signed main(){
cin.tie(0);
ios::sync_with_stdio(0);
int n,m;
cin>>n>>m;
using M = Mint<int, 998244353>;
vector<M> way(n,0);
for(int h=1;h<=m;h++){
M res{1};
for(int i=0;i<n;i++){
way[i]+=res;
res*=M(m-h);
}
}
auto po=powers(n,M(m));
M ans=M(n)*po[n];
for(int w=0;w+2<=n;w++)
ans-=M(n-(w+2)+1)*po[n-(w+2)]*way[w];
//cout<<way[0]<<endl;
cout<<ans<<newl;
return 0;
}
|
#include <bits/stdc++.h>
const int INF =1e9;
const int MOD =1e9+7;
using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < int(n); i++)
#define REPD(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < int(b); i++)
#define FORD(i, a, b) for (int i = b - 1; i >= int(a); i--)
#define WRAP(y, x, h, w) (0 <= y && y < h && 0 <= x && x < w)
#define ALL(x) (x).begin(), (x).end()
int dx[4] ={ 1, 0, -1, 0 };
int dy[4] ={ 0, 1, 0, -1 };
int main()
{
int a,b,c;
cin>>a>>b>>c;
cout<<a-b+c<<endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mod 1000000007
ll n,b,c,a[100009];
ll dp[1000005];
int main(){
ll n,a,b;
cin>>n>>a>>b;
cout<<(n-a)+b<<endl;
return 0;
}
|
#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f;
const ll INFL=0x3f3f3f3f3f3f3f3f;
const int MOD=1000000007;
int x[2000],y[2000];
int main(){
int n;cin>>n;
rep(i,n){
scanf("%d%d",&x[i],&y[i]);
}
int cnt=0;
rep(i,n)for(int j=i+1;j<n;j++){
int A=abs(x[j]-x[i]),B=abs(y[j]-y[i]);
if(B<=A){
cnt++;
}
}
cout<<cnt<<endl;
} | #include<bits/stdc++.h>
typedef long long ll;
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define nep(i,r,l) for(int i=r;i>=l;i--)
void re(int &x){scanf("%d",&x);}
void re(int &x,int &y){scanf("%d %d",&x,&y);}
void re(ll &x){scanf("%lld",&x);}
void re(ll &x,ll &y){scanf("%lld %lld",&x,&y);}
void re(double &x){scanf("%lf",&x);}
void re(double &x,int &y){scanf("%lf %lf",&x,&y);}
using namespace std;
const int N=1e5+5;
int boom[N];
int a[N];
int main()
{
ll n;re(n);
ll t;
ll ans=0;
rep(i,1,n)
{
cin>>t;
boom[t]++;
}
rep(i,1,n)
{
cin>>a[i];
}
rep(i,1,n)
{
cin>>t;
ans+=boom[a[t]];
}
cout<<ans;
return 0;
}
|
# include <bits/stdc++.h>
# define ull unsigned long long
# define ll long long
# define double long double
# define pll pair<ll,ll>
# define pb push_back
# define fi first
# define se second
# define mp make_pair
# define max3(a,b,c) max(a,max(b,c))
# define min3(a,b,c) min(a,min(b,c))
# define all(x) x.begin(),x.end()
# define fill(a,b) memset(a,b,sizeof(a))
# define gcd(m,n) __gcd(m, n)
# define pr_double(x) cout << fixed << setprecision(15) << x
# define endl "\n"
using namespace std;
const ll INF = 9223372036854775807;
const ll N = 300005;
const ll mod = 998244353;
const ll M = 30;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll a,b,c,d; cin>>a>>b>>c>>d; ll ans = -19999999;
for(ll x=a;x<=b;x++)
{
for(ll y=c;y<=d;y++) ans = max(ans,x-y);
}
cout<<ans<<endl;
}
| #include <iostream>
using namespace std;
int main()
{
int a, b, c, d;
cin >> a >> b >> c >> d;
cout << b - c << endl;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FAST ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
#define pb push_back
#define MOD 1000000007
#define sz5 100005
#define sz6 1000005
#define UP upper_bound
#define LB lower_bound
#define F first
#define S second
#define PI pair<int,int>
#define PL pair<ll,ll>
#define VI vector<int>
#define VL vector<ll>
#define pr pair<int, pair<int,int>>
const ll inf = 1e9;
int r,c;
int A[505][505], B[505][505];
int main() {
FAST
int la,lb;
cin>>r>>c;
for(la=1;la<=r;la++){
for(lb=1;lb<c;lb++)
cin>>A[la][lb];
}
for(la=1;la<r;la++){
for(lb=1;lb<=c;lb++)
cin>>B[la][lb];
}
priority_queue<pr, vector<pr>, greater<pr>> q;
bool mk[505][505]={0};
q.push({0, {1, 1}});
vector<vector<int>> dis(505, vector<int>(505, inf));
dis[1][1]=0;
while(!q.empty()){
int d=q.top().first, x=q.top().second.first, y=q.top().second.second;
q.pop();
if(dis[x][y] != d)
continue;
if(x==r && y==c){
cout<<d<<endl;
return 0;
}
if(y<c && dis[x][y+1] > dis[x][y]+A[x][y]){
dis[x][y+1] = dis[x][y]+A[x][y];
q.push({dis[x][y+1], {x, y+1}});
}
if(y>1 && dis[x][y-1] > dis[x][y]+A[x][y-1]){
dis[x][y-1] = dis[x][y]+A[x][y-1];
q.push({dis[x][y-1], {x, y-1}});
}
if(x<r && dis[x+1][y] > dis[x][y]+B[x][y]){
dis[x+1][y] = dis[x][y]+B[x][y];
q.push({dis[x+1][y], {x+1, y}});
}
for(la=x-1, lb=1;la>=1;la--,lb++){
if(dis[la][y] > dis[x][y]+1+lb){
dis[la][y] = dis[x][y]+1+lb;
q.push({dis[la][y], {la, y}});
}
}
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define S second
#define F first
#define f(i,n) for(int i=0;i<n;i++)
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define vi vector<int>
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
const int N = 505;
const int inf = 1e9;
int n,m;
int a[N][N];
int b[N][N];
vector<pii> g[2*N*N];
vector<bool> vis(2*N*N,0);
vector<int> dis(2*N*N,inf);
inline int encode(int i,int j)
{
return (i*m) + j;
}
void solve()
{
cin >> n >> m;
f(i,n) f(j,m-1) cin >> a[i][j];
f(i,n-1) f(j,m) cin >> b[i][j];
f(i,n) f(j,m)
{
if(j > 0) g[encode(i,j)].push_back({encode(i,j-1),a[i][j-1]});
if(j < m-1) g[encode(i,j)].push_back({encode(i,j+1),a[i][j]});
if(i < n-1) g[encode(i,j)].push_back({encode(i+1,j),b[i][j]});
if(i > 0) g[encode(i,j) + (n*m)].push_back({encode(i-1,j) + (n*m),1});
g[encode(i,j)].push_back({encode(i,j) + (n*m),1});
g[encode(i,j) + (n*m)].push_back({encode(i,j),0});
}
dis[0] = 0;
priority_queue<pii,vector<pii>,greater<pii> > PQ;
PQ.push({0,0});
while(!PQ.empty())
{
auto x = PQ.top(); PQ.pop();
if(vis[x.S] == 1) continue;
vis[x.S] = 1;
for(auto z : g[x.S])
{
if(dis[z.F] > dis[x.S] + z.S)
{
dis[z.F] = dis[x.S] + z.S;
PQ.push({dis[z.F],z.F});
}
}
}
cout << dis[n*m - 1];
}
signed main()
{
fast;
solve();
} |
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define trav(a,x) for (auto& a : x)
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert
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; }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1000000007;
const char nl = '\n';
const int MX = 100001; //check the limits, dummy
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int N; cin >> N;
ll A[N], B[N]; F0R(i, N) cin >> A[i] >> B[i];
vl ops; ll sum = 0;
F0R(i, N) {
sum += A[i]; ops.pb(2*A[i] + B[i]);
}
sort(all(ops)); reverse(all(ops));
int ans = 0;
F0R(i, N) {
ans++; sum -= ops[i];
if (sum < 0) break;
}
cout << ans << nl;
return 0;
}
// read the question correctly (ll vs int)
// template by bqi343
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, a, b) for (int i = (a); i < (b); ++i)
#define MAX 128
#define pb push_back
#define mp make_pair
#define ALL(x) (x).begin(), (x).end()
#define bit(n) (1LL << (n))
const double PI = 3.141593;
const int MOD = (int)1e9 + 7;
const int INF = 100100100;
const double EPS = 1e-9;
template <class T>
bool chmax(T &a, const T &b){if (a < b){a = b;return 1;}return 0;}
template <class T>bool chmin(T &a, const T &b){if (b < a){a = b;return 1;}return 0;}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } //最大公約数
ll lcm(ll a, ll b) { return a * b / gcd(a, b); } //最小公倍数
ll combination(ll n, ll r){if (r * 2 > n)r = n - r;ll dividend = 1;ll divisor = 1;for (ll i = 1; i <= r; ++i){dividend *= (n - i + 1);divisor *= i;}return dividend / divisor;}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int h, w, x, y;
cin >> h >> w >> x >> y;
x--;
y--;
string s[h];
rep(i, h)
{
cin >> s[i];
}
int ans =0;
for (int i = y; i < w;i++){
if(s[x][i]=='.'){
ans++;
}
else
break;
}
for (int i = y; 0 <= i; i--)
{
if(s[x][i]=='.'){
ans++;
}
else
break;
}
for (int i = x; i < h; i++)
{
if(s[i][y]=='.'){
ans++;
}
else
break;
}
for (int i = x; 0 <= i; i--)
{
if(s[i][y]=='.'){
ans++;
}
else
break;
}
cout << ans - 3 << endl;
return 0;
} |
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll MX=100000000000000000LL;
ll ck(vector<ll> a, vector<ll> b)
{
ll mn=MX;
sort(a.begin(), a.end());
sort(b.begin(), b.end());
for(int i=0;i<a.size();i++)
{
auto it=upper_bound(b.begin(), b.end(), a[i]);
if(it!=b.end())
{
mn=min(mn, abs(*it-a[i]));
}
if(it!=b.begin())
{
it=prev(it);
mn=min(mn, abs(*it-a[i]));
}
}
return mn;
}
ll ck1(vector<ll> a, vector<ll> b, vector<ll> c)
{
sort(a.begin(), a.end());
sort(b.begin(), b.end());
sort(c.begin(), c.end());
vector<ll> dl(a.size()), dr(a.size());
for(int i=0;i<a.size();i++)
{
ll x=MX;
auto it=upper_bound(b.begin(), b.end(), a[i]);
if(it!=b.end())
{
x=min(x, abs(*it-a[i]));
}
if(it!=b.begin())
{
it=prev(it);
x=min(x, abs(*it-a[i]));
}
if(i>0) dl[i]=min(x, dl[i-1]);
else dl[i]=x;
}
for(int i=a.size()-1;i>=0;i--)
{
ll x=MX;
auto it=upper_bound(b.begin(), b.end(), a[i]);
if(it!=b.end())
{
x=min(x, abs(*it-a[i]));
}
if(it!=b.begin())
{
it=prev(it);
x=min(x, abs(*it-a[i]));
}
if(i!=a.size()-1) dr[i]=min(x, dr[i+1]);
else dr[i]=x;
}
ll mn=MX;
for(int i=0;i<a.size();i++)
{
ll x=MX;
auto it=upper_bound(c.begin(), c.end(), a[i]);
if(it!=c.end())
{
x=min(x, abs(*it-a[i]));
}
if(it!=c.begin())
{
it=prev(it);
x=min(x, abs(*it-a[i]));
}
ll y=MX;
if(i>0) y=min(y, dl[i-1]);
if(i+1<a.size()) y=min(y, dr[i+1]);
mn=min(mn, x+y);
}
return mn;
}
int main()
{
ll n;
cin>>n;
map<char, vector<ll> > mp;
for(int i=1;i<=2*n;i++)
{
ll x;
char c;
cin>>x>>c;
mp[c].push_back(x);
}
if(mp['B'].size()%2==0 and mp['R'].size()%2==0 and mp['G'].size()%2==0)
{
cout<<0<<endl;
return 0;
}
if(mp['B'].size()%2==0)
{
ll x=ck(mp['R'], mp['G']);
if(mp['B'].size()>=2)
x=min(x, ck1(mp['B'], mp['R'], mp['G']));
cout<<x<<endl;
return 0;
}
if(mp['R'].size()%2==0)
{
ll x=ck(mp['B'], mp['G']);
if(mp['R'].size()>=2)
{
x=min(x, ck1( mp['R'], mp['B'], mp['G'] ));
}
cout<<x<<endl;
return 0;
}
if(mp['G'].size()%2==0)
{
ll x=ck(mp['R'], mp['B']);
if(mp['G'].size()>=2)
{
x=min(x, ck1(mp['G'], mp['R'], mp['B']));
}
cout<<x<<endl;
return 0;
}
return 0;
} | #include <stdio.h>
typedef long long ll;
int num[105][105], max = -1, vis[20], k, n, m;
struct node {
int x1, x2;
} a[20], b[105];
void dfs(int now) {
if (now >= k) {
int sum = 0;
//for (int i = 0; i < 17; i ++) printf("%d ", vis[i]);printf("\n");
for (int i = 0; i < m; i ++)
if (vis[b[i].x1] && vis[b[i].x2])
//printf("sum : %d + num[%d][%d] (%d)\n", sum, b[i].x1, b[i].x2, num[b[i].x1][b[i].x2]);
sum ++;
if (sum > max) max = sum;
//printf("%d..sum : %d\n", now, sum);
return;
}
int px1 = a[now].x1, px2 = a[now].x2;
vis[px1] ++;
//printf("%d pick : %d\n", now, px1);
dfs(now + 1);
vis[px1] --;
vis[px2] ++;
//printf("%d pick : %d\n", now, px2);
dfs(now + 1);
vis[px2] --;
}
int main() {
int cnt = 0;
scanf("%d %d", &n, &m);
while (cnt < m) {
scanf("%d %d", &b[cnt].x1, &b[cnt].x2);
//num[b[cnt].x1][b[cnt].x2] ++;
cnt ++;
}
int ind = 0;
scanf("%d", &k);
while (ind < k) {
scanf("%d %d", &a[ind].x1, &a[ind].x2);
ind ++;
}
dfs(0);
//dfs(1);
printf("%d\n", max);
return 0;
}
|
#include <bits/stdc++.h>
#define vi vector<int>
#define tests int t; cin>>t; while(t--)
#define ll long long
#define vll vector<long long>
#define srt(v) sort(v.begin(), v.end())
#define srtg(v) sort(v.begin(), v.end(), greater<int> ())
#define FOR(k, n) for(int k=0; k<n; k++)
#define pb push_back
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define printarr(v) for(auto& x : v) cout<<x<<" "; cout<<endl
using namespace std;
char nums[10] = { '0','1','2','3','4','5','6','7','8','9' };
char alphsl[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' };
const int MOD = 1000000007;
char alphs[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' };
void solve() {
int n, w;
cin>>n>>w;
vi start(n), end(n), litre(n);
for(int i=0; i<n; i++){
cin>>start[i]>>end[i]>>litre[i];
}
vi open[200005], close[200005];
for(int i=0; i<n; i++){
int currop=start[i];
open[currop].pb(i);
currop=end[i];
close[currop].pb(i);
}
ll curr=0;
for(int i=0; i<=200000; i++){
for(auto x : open[i]) curr+=litre[x];
for(auto y : close[i]) curr-=litre[y];
if(curr>w){
cout<<"No"<<endl;
return;
}
}
cout<<"Yes"<<endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
}
| #include <iostream>
#include<algorithm>
using namespace std;
int main()
{
ios::sync_with_stdio(0);cin.tie(0);
int N,S,T;long W,P;
int maxx = 0;
long long a[200010]={0},sum=0,sum1=0;
cin >> N >> W;
for(int i=1;i<=N;i++){
cin >> S >> T >> P;
maxx = max(maxx,T);
a[S] += P;
a[T] -= P;
}
for(int i=0;i<maxx;i++){
sum1 = max(sum,sum + a[i]);
sum += a[i];
if(sum>W){
cout << "No" << endl;
//cout << sum1<<endl;
//cout << a[0]<<endl;
//cout << a[200000]<<endl;
return 0;
}
}
cout << "Yes";
//cout << sum1;
return 0;
}
|
#include <iostream>
#include <string>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
int a1 = a / 100, b1 = b / 100;
int a2 = (a -= a1*100) / 10, b2 = (b -= b1*100) / 10;
int a3 = a - a2*10, b3 = b - b2*10;
a = a1 + a2 + a3, b = b1 + b2 + b3;
if(a >= b) cout << a << endl;
else cout << b << endl;
return 0;
}
| #include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <utility>
#include <algorithm>
#include <cmath>
#include <climits>
#include <iomanip>
#include <queue>
#include <stack>
using namespace std;
typedef long long ll;
const int INF = (1<<30)-1;
const ll LINF = 1e18;
#define rep(i, n) for (int i = 0; i < n; i++)
template<class T>
bool chmax(T &a, T b) {if (a < b) {a = b;return true;}else return false;}
template<class T>
bool chmin(T &a, T b) {if (a > b) {a = b;return true;}else return false;}
int main() {
ll n, m, t; cin >> n >> m >> t;
ll tmp = n;
vector<ll> a(m+1), b(m+1, 0);
rep(i, m) {
cin >> a[i+1] >> b[i+1];
tmp -= a[i+1] - b[i];
if (tmp <= 0) {
cout << "No" << endl;
return 0;
}
tmp = min(n, tmp+b[i+1]-a[i+1]);
}
tmp -= t - b[m];
if (tmp <= 0) {
cout << "No" << endl;
return 0;
}
cout << "Yes" << endl;
return 0;
}
//小数点精度
//cout << fixed << std::setprecision(15) << y << endl; |
#include <bits/stdc++.h>
using namespace std;
#define PI acos(-1)
typedef long long int T; /// basic type
typedef double Tf; /// floating point type
Tf eps = 1e-6;
//int sgn(Tf x){
// if(fabs(x) < eps) return 0;
// if(x > 0) return 1;
// else return -1;
//}
int sgn(T x){
if(x == 0) return 0;
if(x > 0) return 1;
else return -1;
}
struct point{
T x, y;
point(T x = 0, T y = 0){
this->x = x;
this->y = y;
}
point operator+(point p){ return point(x+p.x, y+p.y); }
point operator-(point p){ return point(x-p.x, y-p.y); }
point operator*(T a){ return point(x*a, y*a); }
point operator/(T a){ return point(x/a, y/a); }
Tf len(){ return sqrt(x*x+y*y); }
T norm(){ return x*x + y*y; }
// bool operator==(point p){ /// for integers
// return x == p.x && y == p.y;
// }
bool operator==(point p){ /// for floating point
return sgn(x-p.x) == 0 && sgn(y-p.y) == 0;
}
// bool operator<(point p){ /// for integers
// if(x == p.x) return y < p.y;
// else return x < p.x;
// }
//
bool operator<(point p){ /// for floating points
if(sgn(x-p.x) == 0) return sgn(y-p.y) == -1;
else return sgn(x-p.x) == -1;
}
};
T dot(point p, point q){
return p.x * q.x + p.y * q.y;
}
T cross(point p, point q){
return p.x * q.y - p.y * q.x;
}
/// a->b->c which direction we rotate, +ve ccw, -ve cw, 0 collinear
T orient(point a, point b, point c){
return cross(b-a, c-a);
}
ostream& operator<<(ostream &os, point p){
return os<<"("<<p.x<<","<<p.y<<")";
}
T triangleArea(point a, point b, point c){
return abs(orient(a, b, c));
}
struct segment{
point a, b;
bool onSegment(point p){ // check if a point is on the segment.
if(sgn(orient(a, b, p)) != 0) return false;
return sgn(dot(p-a, p-b)) <= 0;
}
bool intersection(segment s){
if(onSegment(s.a)) return true;
if(onSegment(s.b)) return true;
if(s.onSegment(a)) return true;
if(s.onSegment(b)) return true;
int s1 = sgn(orient(a, b, s.a));
int s2 = sgn(orient(a, b, s.b));
int s3 = sgn(orient(s.a, s.b, a));
int s4 = sgn(orient(s.a, s.b, b));
if(s1*s2 < 0 && s3 * s4 < 0) return true;
return false;
}
};
//8 -6 8 6
//0 0 7
void solve(){
int n; cin>>n;
double arr1[n], arr2[n];
for(int x=0;x<n;x++){
cin>>arr1[x]>>arr2[x];
}
bool ok = false;
for(int x=0;x<n;x++){
for(int y=x+1;y<n;y++){
for(int z=y+1;z<n;z++){
point a(arr1[x], arr2[x]);
point b(arr1[y], arr2[y]);
point c(arr1[z], arr2[z]);
if(orient(a, b, c) == 0) ok = true;
}
}
}
if(ok) cout<<"Yes\n";
else cout<<"No\n";
}
int main(){
solve();
// double x1, y1, x2, y2, c1, c2, l;
// cin>>x1>>y1>>x2>>y2>>c1>>c2>>l;
//
// point a(x1, y1);
// point b(x2, y2);
// point c(c1, c2);
//
// point d = (a + b) / 2;
//
//
// segment seg;
// seg.a = a;
// seg.b = b;
//
// double ans1;
// if((orient(b, d, c) * orient(d, a, c))>=0 || (orient(d, b, c) * orient(a, d, c)) >= 0){
// double ar = triangleArea(a, b, c);
// ans1 = ar / (a-b).len();
// }else{
// ans1 = min((c-a).len(), (c-b).len());
// }
// printf("%.2lf\n", max((double) 0, ans1 - l));
//
//
// double ans2 = max((double)0, max((a-c).len(), (b-c).len()) - l);
// printf("%.2lf\n", ans2);
}
/// x > y => x-y > 0
/// sgn(x-y) == 1
/// sgn(y-x) == -1
| #include<iostream>
using namespace std;
#define progend {delete [] x;delete [] y; return 0;}
int* makevector(int* a, int p, int len) {
int* temp = new int[len];
for (int i = 0; i < len; i++) {
temp[i] = a[i] - p;
}
return temp;
}
int main() {
int n, a, b;
cin >> n;
int* x = new int[n];
int* y = new int[n];
int k = n;
while (n--) {
cin >> a >> b;
x[n] = a;
y[n] = b;
}
int* xV, * yV;
while (k>2) {
xV = makevector(x, x[k - 1], k-1);
yV = makevector(y, y[k - 1], k-1);
for (int i = 0; i < k - 2; i++)
for (int j = i + 1; j < k - 1; j++)
if (xV[j] * yV[i] == xV[i] * yV[j]) {
cout << "Yes\n";
progend
}
k -= 1;
}
cout << "No\n";
progend
}
|
#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<set>
#include<map>
#include<utility>
#include<queue>
#include<vector>
#include<stack>
#include<sstream>
#include<algorithm>
#define forn(i,a,b)for(int i=(a),_b=(b);i<=_b;i++)
#define fore(i,b,a)for(int i=(b),_a=(a);i>=_a;i--)
#define rep(i,n)for(int i=0,_n=n;i<n;i++)
#define ll long long
#define pii pair<int,int>
#define vi vector<int>
#define vpii vector<pii>
#define m_p make_pair
#define re return
#define pb push_back
#define si set<int>
#define ld long double
#define X first
#define Y second
#define st string
#define ull unsigned long long
#define mod 1000000007
#define INF 1000000007
#define x1 XZVJDFADSPFOE
#define y1 GASDIJSLDAEJF
#define x2 DFDAJKVOHKWIW
#define y2 PSFSAODSXVNMQ
#define LLINF 0x3f3f3f3f3f3f3f3fLL
#define foreach(i,c) for(__typeof(c.begin())i=(c.begin());i!=(c).end();i++)
using namespace std;
inline void read(int &x)
{
short negative=1;
x=0;
char c=getchar();
while(c<'0' || c>'9')
{
if(c=='-')
negative=-1;
c=getchar();
}
while(c>='0' && c<='9')
x=(x<<3)+(x<<1)+(c^48),c=getchar();
x*=negative;
}
ll quickpower(ll n,ll k){
ll ans=1;
while(k){
if(k%2){
ans*=n;
ans%=mod;
}
n*=n;
n%=mod;
k/=2;
}
return ans;
}
string int_to_string(int n)
{
string s="";
while(n)
{
int now=n%10;
s+=now+'0';
n/=10;
}
reverse(s.begin(),s.end());
return s;
}
int string_to_int(string s)
{
int n=0;
rep(i,s.size())
{
n*=10;
n+=s[i]-'0';
}
return n;
}
int n;
int x[1010],y[1010];
int main()
{
ios::sync_with_stdio(0);
cin>>n;
rep(i,n)cin>>x[i]>>y[i];
int ans=0;
rep(i,n)
{
rep(j,i)
{
if(abs(y[j]-y[i])<=abs(x[j]-x[i]))ans++;
}
}
cout<<ans<<endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
void fastio() {
cin.tie(nullptr);
cin.sync_with_stdio(false);
}
using LL = long long;
using LD = long double;
const LL MOD = 1e9+7;
const LL INF = LLONG_MAX;
const LL N = 101;
#define F first
#define S second
#define vt vector
LL gcd(LL a, LL b, LL& x, LL& y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
LL x1, y1;
LL d = gcd(b, a % b, x1, y1);
x = y1;
y = x1 - y1 * (a / b);
return d;
}
int main() {
fastio();
LL T;
cin >> T;
while (T--) {
LL n, s, k;
cin >> n >> s >> k;
if (s % __gcd(k, n) != 0) {
cout << "-1\n";
continue;
}
LL xg, yg;
LL gc = __gcd(k, n);
LL k1 = k/gc, n1 = n/gc, s1 = s/gc;
gcd(k, n, xg, yg);
LL x0 = -xg * s1;
LL y0 = yg * s1;
//cerr << x0 << " " << y0 << "??\n";
if (x0 < 0) {
// adjust y and x
LL mx = max(-x0/n1, -y0/k1);
x0 += mx*n1;
y0 += mx*k1;
if (x0 < 0 || y0 < 0) {
x0 += n1;
y0 += k1;
}
}
else {
x0 %= n1;
}
cout << x0 << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
int modPow(int x, int n, int mod) {
if (n == 0) return 1;
int z = modPow(x, n / 2, mod);
z *= z;
z %= mod;
if (n & 1) z *= x, z %= mod;
return z;
}
int main() {
int a, b, c;
cin >> a >> b >> c;
a %= 10;
vector<int> p;
p.push_back(a);
int t = a;
while ((t * a) % 10 != a) {
t *= a;
t %= 10;
p.push_back(t);
}
int cycleLength = p.size();
int remPow = modPow(b, c, cycleLength);
int ans = remPow == 0? p.back(): p[remPow - 1];
cout << ans << "\n";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
long double X, Y, R;
cin >> X >> Y >> R;
long ans = 0;
int high = floor(Y + R);
int low = ceil(Y - R);
R += 1e-14;
long double RR = R*R;
for(long double h = high; h >= low; h--) {
long double YYhh = (Y-h)*(Y-h);
if(RR > YYhh ){
long double halfLen = sqrt(RR - YYhh);
int right = floor(X + halfLen);
int left = ceil(X - halfLen);
ans += right - left + 1;
}
}
cout << ans;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
const int INF = 1000000007;
int A[MAXN], max_reachable[MAXN];
vector <int> G[MAXN];
int main(int argc, char const *argv[])
{
int n, m;
cin>>n>>m;
for (int i = 1; i <= n; ++i)
{
cin>>A[i];
}
for (int i = 0; i < m; ++i)
{
int u, v;
cin>>u>>v;
G[u].push_back(v);
}
int ans = -INF;
for (int i = n; i >= 1; --i)
{
max_reachable[i] = -INF;
for (int j = 0; j < (int)G[i].size(); ++j)
{
int v = G[i][j];
max_reachable[i] = max(max_reachable[i], max_reachable[v]);
}
ans = max(ans, max_reachable[i] - A[i]);
max_reachable[i] = max(max_reachable[i], A[i]);
}
cout<<ans<<"\n";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define fst ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endk cout<<"\n";
typedef long long int ll;
typedef long int l;
#define l(i,a,b) for(i=a;i<b;i++)
#define lo(i,a,b) for(i=a;i>=b;i--)
#define s(v) sort(v.begin(),v.end());
#define pb push_back;
#define vll vector<ll>
#define vint vector<int>
#define test() ll t;cin>>t;while(t--){solve();};
#define test1() int t=1;while(t--){solve();};
bool isprime(ll x)
{
if(x==1) return false;
if(x==2 || x==3) return true;
if(x%2==0 || x%3==0) return false;
for(ll i=5;i*i<=x;i+=6)
{
if(x%i==0 || x%(i+2)==0) return false;
}
return true;
}
ll power(ll x,ll y)
{
ll ans=1;
while(y>0)
{
if(y&1) ans=ans*x;
x=x*x;
y=y>>1;
}
return ans;
}
bool comp(pair<ll,ll> a,pair<ll,ll> b)
{
return (a.first<b.first);
}
int solve()
{
ll n,k,ans=0,i,curr=0;
cin>>n>>k;
vector<pair<ll,ll>> v(n);
for(i=0;i<n;i++)
{
cin>>v[i].first>>v[i].second;
}
sort(v.begin(),v.end(),comp);
for(i=0;i<n;i++)
{
if(v[i].first-curr<=k)
{
k-=(v[i].first-curr);
curr=v[i].first;
k+=v[i].second;
}
else
{
curr+=k;
break;
}
}
if(i==n)
{
curr+=k;
}
cout<<curr;
return 0;
}
int main()
{
fst;
test1();
return 0;
}
|
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
const ll MOD=998244353;
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;}
#define MAX_N 1000010
ll inv[MAX_N+10],fac[MAX_N+10],ifac[MAX_N+10];
void setComb(){
inv[0]=1;inv[1]=1;fac[1]=1;ifac[1]=1;fac[0]=1;ifac[0]=1;
for(int i=2;i<MAX_N;i++){
inv[i]=(-MOD/i)*inv[MOD%i]%MOD;
fac[i]=fac[i-1]*i%MOD;
ifac[i]=ifac[i-1]*inv[i]%MOD;
inv[i]=(inv[i]+MOD)%MOD;
fac[i]=(fac[i]+MOD)%MOD;
ifac[i]=(ifac[i]+MOD)%MOD;
}
return;
}
ll comb(ll n,ll k){
if(n<k||n<0||k<0) return 0;
else return ((fac[n]*ifac[k]%MOD*ifac[n-k]%MOD+MOD)%MOD);
}
ll hcomb(ll n,ll r){// this size is really ok??
if(n==0&&r==0) return 1;
else if(n<0||r<0) return 0;
else return comb(n+r-1,r);
}
ll binom(ll n,ll k){
if(n<k||n<0||k<0) return 0;
ll res=1;
for(ll i=0;i<k;i++) res=res*(n-i)%MOD;
res=res*ifac[k]%MOD;
return res;
}
ll mod_pow(ll x,ll n){
x%=MOD;
ll res=1;
while(n>0){
if(n&1) res=res*x%MOD;
x=x*x%MOD;
n>>=1;
}
return res;
}
ll mod_inverse(ll x){
return mod_pow(x,MOD-2);
}
void add(ll &a,ll b){
a=(a+b)%MOD;
}
void mul(ll &a,ll b){
a%=MOD;b%=MOD;
a=a*b%MOD;
}
string P="RDX";
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
setComb();
ll H,W,K;
cin>>H>>W>>K;
vector<int> X(K),Y(K);
vector<char> C(K);
rep(i,K) cin>>X[i]>>Y[i]>>C[i];
rep(i,K){
X[i]--;Y[i]--;
}
vector<int> ord(K,0);
rep(i,K) ord[i]=i;
sort(all(ord),[&](int a,int b){
if(X[a]!=X[b]) return X[a]<X[b];
if(Y[a]!=Y[b]) return Y[a]<Y[b];
return a<b;
});
vector<ll> dp(W,0);
dp[0]=mod_pow(3,H*W-K);
int now=0;
for(int i=0;i<H;i++){
vector<ll> ndp(W,0);
for(int j=0;j<W;j++){
bool exi=false;
char c='#';
if(now<K&&X[ord[now]]==i&&Y[ord[now]]==j){
c=C[ord[now]];
exi=true;
now++;
}
ll coef=1;
if(exi==false) coef=inv[3];
for(int k=0;k<3;k++){
//if(mp.count(mkp(i,j))&&mp[mkp(i,j)]!=P[k]) continue;
if(exi&&c!=P[k]) continue;
if(k==0||k==2){
if(j+1<W){
dp[j+1]+=(dp[j]*coef%MOD);
dp[j+1]%=MOD;
}
}
if(k==1||k==2){
if(i+1<H){
ndp[j]+=(dp[j]*coef%MOD);
ndp[j]%=MOD;
}
}
}
}
if(i+1==H) break;
swap(dp,ndp);
}
ll ans=dp[W-1];
cout<<ans<<endl;
return 0;
}
| #include<bits/stdc++.h>
#include<bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define priority_queue std::priority_queue
#define F first
#define S second
ll MOD=998244353;
int h, w, k;
char g[5005][5005];
ll dp[5005][5005], emp[5005][5005], po3[200005], inv;
ll modpow(ll x, ll e){
if(x==0) return 0;
if(e==0) return 1;
ll y=modpow(x, e/2);
if(e%2==0) return (y*y)%MOD;
return ((y*y)%MOD*x)%MOD;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>h>>w>>k;
po3[0]=1;
for(int i=1;i<200005;i++){
po3[i]=(po3[i-1]*3)%MOD;
}
for(int i=0;i<=h;i++){
for(int j=0;j<=w;j++){
g[i][j]='.';
}
}
for(int i=0;i<k;i++){
int x, y;
char c;
cin>>x>>y>>c;
g[x][y]=c;
}
for(int i=1;i<=h;i++){
for(int j=1;j<=w;j++){
emp[i][j]=emp[i-1][j]+emp[i][j-1]-emp[i-1][j-1]+(g[i][j]=='.');
}
}
if(g[1][1]=='.') dp[1][1]=3;
else dp[1][1]=1;
inv=modpow(3, MOD-2);
for(int x=1;x<=h;x++){
for(int y=1;y<=w;y++){
if(x+y==2) continue;
ll c=po3[emp[x][y]-emp[x][y-1]], r=po3[emp[x][y]-emp[x-1][y]];
if(g[x][y-1]=='R' || g[x][y-1]=='X'){
dp[x][y]=(dp[x][y]+dp[x][y-1]*c)%MOD;
}
if(g[x-1][y]=='D' || g[x-1][y]=='X'){
dp[x][y]=(dp[x][y]+dp[x-1][y]*r)%MOD;
}
if(g[x][y-1]=='.'){
dp[x][y]+=(((2*inv*dp[x][y-1])%MOD)*c)%MOD;
dp[x][y]%=MOD;
}
if(g[x-1][y]=='.'){
dp[x][y]+=(((2*inv*dp[x-1][y])%MOD)*r)%MOD;
dp[x][y]%=MOD;
}
}
}
cout<<dp[h][w]<<endl;
} |
Subsets and Splits