code_file1
stringlengths 87
4k
| code_file2
stringlengths 85
4k
|
---|---|
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int> pi;
#define f first
#define s second
#define FAST ios_base::sync_with_stdio(0); cin.tie(0);
typedef pair<int, pi> pii;
int x2,y2;
int test(int x1,int y1, int grad1, int grad2) {
if (x1 - y1 == grad1 or x1+y1 == grad2) {
return 1;
} else {
if (((x1 - y1) - grad1) % 2 == 0) {
return 2;
} else if ((grad2 - (x1 + y1)) % 2 == 0) {
return 2;
}
}
return INT_MAX/2;
}
int32_t main() {
FAST
int x1,y1;
cin >> x1 >> y1 >> x2 >> y2;
if (x1 == x2 and y1 == y2) {
cout << 0;
return 0;
}
int grad1 = x2 - y2;
int grad2 = x2 + y2;
int ans = test(x1,y1,grad1,grad2);
for (int x = x2-3; x <= x2+3; x++) {
for (int y = y2-3; y <= y2 +3;y++) {
if (abs(x - x2) + abs(y - y2) <= 3) {
ans = min(ans, test(x1,y1,x - y, x +y) + 1);
}
}
}
for (int x = x1-3; x <= x1+3; x++) {
for (int y = y1-3; y <= y1 +3;y++) {
if (abs(x1 - x) + abs(y1 - y) <= 3) {
if (x == x2 and y == y2) {
ans = min(ans, 1ll);
}
ans = min(ans, test(x,y,grad1, grad2) + 1);
}
}
}
cout << ans;
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF 1000000000000000000
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int tt=1;
//cin >> tt;
while(tt--)
{
int a,b,c,d;
cin >> a >> b >> c >> d;
if(a==c&&b==d)
{
cout << 0 << endl;
}
else if(a+b==c+d||a-b==c-d||abs(a-c)+abs(b-d)<=3)
{
cout << 1 << endl;
}
else if(abs((a+b)-(c+d))<=3||abs((a-b)-(c-d))<=3)
{
cout << 2 << endl;
}
else if((a+b)%2==(c+d)%2)
{
cout << 2 << endl;
}
else
{
cout << 3 << endl;
}
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int a[25];
int main(){
int n;
scanf("%d", &n);
for(int i=0;i<n;i++){
scanf("%d", &a[i]);
}
int ans = 2e9;
for(int i=0;i<(1<<n);i++){
int sum = 0, res = 0;
for(int j=0;j<n;j++){
sum |= a[j];
if(j == n-1 || ((i>>j)&1)){
res ^= sum;
sum = 0;
}
}
ans = min(ans, res);
}
printf("%d\n", ans);
} | #include <bits/stdc++.h>
using namespace std;
#define BUFF ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define DEBUG(var) cout << #var << "=" << var << '\n'
#define WARN puts("warning!")
#define REP(i, a, n) for (int i = a; i <= n; ++i)
#define PER(i, n, a) for (int i = n; i >= a; --i)
#define LL int64_t
template<typename T>void Read(T &x){x=0;char ch=getchar();LL f=1;while(!isdigit(ch)){if(ch=='-')f*=-1;ch=getchar();}while(isdigit(ch)){x=x*10+ch-48;ch=getchar();}x*=f;}
template<typename T>int64_t __lcm(T a, T b){return a/__gcd(a,b)*b;}
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int64_t Rand(LL a,LL b) {return a + rng() % (b-a+1);}
inline int64_t QuickPow(LL base,LL n,LL Mod = 0){LL ret(1);while(n){if(n&1){ret*=base;if(Mod)ret%=Mod;}base*=base;if(Mod)base%=Mod;n>>=1;}return Mod?ret%Mod:ret;}
inline int64_t Inv(LL x,LL p){return QuickPow(x,p-2,p);}
inline void gettime(){time_t rawtime;struct tm *ptminfo;time(&rawtime);ptminfo = localtime(&rawtime);printf("Current time: %02d-%02d-%02d %02d:%02d:%02d\n",ptminfo->tm_year + 1900, ptminfo->tm_mon + 1, ptminfo->tm_mday,ptminfo->tm_hour, ptminfo->tm_min, ptminfo->tm_sec);}
const double pi = acos(-1.0);
const LL kInf(LONG_LONG_MAX);
const LL kMod(998244353);
const LL kMax(210);
LL group(1);
inline void Solve()
{
/*write your code down here*/
int n;
cin>>n;
vector<int>v(n);
for(auto &it:v)Read(it);
int res = INT_MAX;
for(int i = 0;i < (1<<(n-1));i++)
{
int XOR = 0,OR = 0;
for(int j = 0;j <= n;j++)
{
if(j < n)OR |= v[j];
if(j == n || ((i>>j) &1))XOR ^= OR,OR = 0;
}
res = min(res,XOR);
}
cout<<res<<'\n';
}
int main()
{
//gettime();
//WARN;
//Read(group);
while (group--)
{
Solve();
}
return 0;
}
|
#include "bits/stdc++.h"
using namespace std;
#define int long long
int extgcd(int a, int b, int &x, int &y) {
if (b == 0) {
x = 1, y = 0;
return a;
}
int g = extgcd(b, a % b, x, y);
int t = x;
x = y;
y = t - a / b * y;
return g;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
while (T--) {
int n, s, k;
cin >> n >> s >> k;
int x, y;
int g = extgcd(k, n, x, y);
if (s % g != 0) {
cout << -1 << endl;
continue;
} else if (g == 1) {
cout << (x * (-s) % n + n) % n << endl;
} else {
n /= g;
s /= g;
k /= g;
cout << (x * (-s) % n + n) % n << endl;
}
}
}
| #include <bits/stdc++.h>
using namespace std;
tuple<long long, long long, long long> extgcd(long long a, long long b) {
if (b == 0) return {a, 1, 0};
auto [d, x, y] = extgcd(b, a % b);
return {d, y, x - a / b * y};
}
// ax ≡ b (mod n) を満たす最小の非負整数 x を返す
// ただし、解が存在しない場合は -1 を返す
long long mod_linear_equation(long long a, long long b, long long n) {
assert(a > 0 && n > 0);
auto [d, x, y] = extgcd(a, n);
n /= d;
return b % d == 0 ? ((b / d * x) % n + n) % n : -1;
}
void solve() {
int n, s, k;
cin >> n >> s >> k;
long long x = mod_linear_equation(k, -s, n);
cout << x << '\n';
}
int main() {
int t;
cin >> t;
while (t--) solve();
} |
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
string s[n];
set<string> val;
for(int i=0;i<n;i++)
{cin>>s[i];val.insert(s[i]);}
for(int i=0;i<n;i++)
{
//if(s[i][0]!='!') continue;
if(val.count('!'+s[i]))
{
cout<<s[i];
exit(0);
}
}
cout<<"satisfiable";
} | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<pair<int, int>> vpii;
typedef vector<pair<ll, ll>> vpll;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vc> vvc;
typedef vector<vs> vvs;
typedef vector<vll> vvll;
typedef map<int, int> mii;
typedef set<int> si;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define irep(it, stl) for (auto it = stl.begin(); it != stl.end(); it++)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define fin(ans) cout << (ans) << '\n'
#define STLL(s) strtoll(s.c_str(), NULL, 10)
#define mp(p, q) make_pair(p, q)
#define pb(n) push_back(n)
#define all(a) a.begin(), a.end()
#define Sort(a) sort(a.begin(), a.end())
#define Rort(a) sort(a.rbegin(), a.rend())
#define fi first
#define se second
// #include <atcoder/all>
// using namespace atcoder;
constexpr int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
constexpr int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
template <class T, class U>
inline bool chmax(T& a, U b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T, class U>
inline bool chmin(T& a, U b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline void dump(T& v) {
irep(i, v) { cout << (*i) << ((i == --v.end()) ? '\n' : ' '); }
}
template <class T, class U>
inline void dump(map<T, U>& v) {
irep(i, v) { cout << i->first << " " << i->second << '\n'; }
}
template <class T, class U>
inline void dump(pair<T, U>& p) {
cout << p.first << " " << p.second << '\n';
}
string getline() {
string s;
fflush(stdin);
getline(cin, s);
return s;
}
inline void yn(const bool b) { b ? fin("yes") : fin("no"); }
inline void Yn(const bool b) { b ? fin("Yes") : fin("No"); }
inline void YN(const bool b) { b ? fin("YES") : fin("NO"); }
template <typename Head, typename Value>
auto vectors(const Head& head, const Value& v) {
return vector<Value>(head, v);
}
template <typename Head, typename... Tail>
auto vectors(Head x, Tail... tail) {
auto inner = vectors(tail...);
return vector<decltype(inner)>(x, inner);
}
const int INF = INT_MAX;
constexpr ll LLINF = 1LL << 61;
constexpr ll MOD = 1000000007; // 998244353;
constexpr ld EPS = 1e-11;
/* -------------------- ここまでテンプレ -------------------- */
int main() {
map<string, int> m;
int n;
cin>>n;
rep(i,n){
string s;
cin>>s;
string t=s;
int bi=0;
if(s[0]=='!')bi=1;
if(t[0]=='!')t=t.substr(1);
m[t]|=(1<<bi);
}
irep(i,m){
if(i->second==3){
cout<<i->first<<endl;
return 0;
}
}
fin("satisfiable");
}
|
#include <bits/stdc++.h>
using namespace std;
#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 pai arccos(-1);
#define keta(n) cout << fixed << setprecision((n));
using ll = long long;
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#define Sort(a) sort(a.begin(), a.end())
#define Reverse(a) reverse(a.begin(), a. end())
#define cyes cout << "Yes" << endl
#define cno cout << "No" << endl
#define cYES cout << "YES" << endl
#define cNO cout << "NO" << endl
typedef long long int ll;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<vector<ll>> vvll;
typedef long double ld;
const int INF = 1 << 30;
const ll MOD = 1000000007;
int main(){
int n;
cin >> n;
vector<string> s(n);
vector<int> t(n),t1(n);
rep(i,n){
cin >> s[i]>>t[i];
}
t1=t;
Sort(t1);
Reverse(t1);
int second=t1[1];
rep(i,n){
if (t[i]==second){
cout<<s[i];
return 0;
}
}
}
| /* Up : 1 2 0 5 - 201 */
#include <iostream>
#include <cstdio>
#include <string>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <time.h>
#define file freopen("input.txt","r",stdin)
#define ll long long int
#define space printf(" ")
#define newline printf("\n")
using namespace std;
ll cs()
{
ll n;
scanf("%lld",&n);
return n;
}
void cp(ll i)
{
printf("%lld",i);
}
void test_case()
{
/* start here for test case prob */
ll n;
n = cs();
ll a[n],b[n];
string s[n];
getchar();
for(ll i=0 ; i<n ; i++)
{
cin >> s[i];
cin >> a[i];
b[i] = a[i];
getchar();
}
sort(b,b+n);
for(ll i=0 ; i<n ; i++)
{
if(b[n-2]==a[i])
{
cout << s[i] << endl;
}
}
}
void t()
{
ll tc;
tc = cs();
while(tc--)
{
test_case();
}
}
int main()
{
//file;
test_case();
//t();
return 0;
}
|
#include<bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ll n,sum1=0,sum2=0;
cin>>n;
for(ll i=0;i<n;i++)
{
ll k;
cin>>k;
sum1+=k;
sum2+=k*k;
}
cout<<sum2*n-sum1*sum1<<'\n';
return 0;
} | #include<bits/stdc++.h>
using namespace std;
long long a[401],b[300005];
int main(){
long long n,c,ans=0;
cin>>n;
for(int i=1;i<=n;i++){
cin>>b[i];
a[b[i]+200]++;
}
for(int i=0;i<=399;i++){
for(int j=i+1;j<=400;j++){
ans+=a[i]*a[j]*(i-j)*(i-j);
}
}
cout<<ans;
return 0;
}
|
#include "bits/stdc++.h"
#define rep(var,init,max) for(int (var)=(init); (var) < (max); ++(var))
#define SORT(v) sort((v).begin(),(v).end())
#define RSORT(v) sort((v).begin(),(v).end()),reverse((v).begin(),(v).end())
using namespace std;
using ll = long long;
using ull = unsigned long long;
const int mod107 = 1e9 + 7;
const int mod998 = 998244353;
template<typename T>
using vec = vector<T>;
template<typename T>
using mat = vector<vector<T>>;
bool debugflag = false;
template<typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
for (int i = 0; i < (int)v.size(); ++i) {
os << v[i] << " ";
}
return os;
}
template<typename T>
istream& operator>>(istream& is, vector<T>& v) {
for (T& in : v) is >> in;
return is;
}
template<typename T>
ostream& operator<<(ostream& os, const mat<T>& v) {
for (int i = 0; i < (int)v.size(); ++i) {
os << v[i] << endl;
}
return os;
}
void debug() {
cout << endl;
}
template<class Head, class... Tail>
void debug(Head&& head, Tail&&... tail) {
if (!debugflag) return;
cout << head << " ";
debug(forward<Tail>(tail)...);
}
template< int mod >
struct ModInt {
int x;
ModInt() : x(0) {}
ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
ModInt& operator+=(const ModInt& p) {
if ((x += p.x) >= mod) x -= mod;
return *this;
}
ModInt& operator-=(const ModInt& p) {
if ((x += mod - p.x) >= mod) x -= mod;
return *this;
}
ModInt& operator*=(const ModInt& p) {
x = (int)(1LL * x * p.x % mod);
return *this;
}
ModInt& operator/=(const ModInt& p) {
*this *= p.inverse();
return *this;
}
ModInt operator-() const { return ModInt(-x); }
ModInt operator+(const ModInt& p) const { return ModInt(*this) += p; }
ModInt operator-(const ModInt& p) const { return ModInt(*this) -= p; }
ModInt operator*(const ModInt& p) const { return ModInt(*this) *= p; }
ModInt operator/(const ModInt& p) const { return ModInt(*this) /= p; }
bool operator==(const ModInt& p) const { return x == p.x; }
bool operator!=(const ModInt& p) const { return x != p.x; }
ModInt inverse() const {
int a = x, b = mod, u = 1, v = 0, t;
while (b > 0) {
t = a / b;
swap(a -= t * b, b);
swap(u -= t * v, v);
}
return ModInt(u);
}
ModInt pow(int64_t n) const {
ModInt ret(1), mul(x);
while (n > 0) {
if (n & 1) ret *= mul;
mul *= mul;
n >>= 1;
}
return ret;
}
friend ostream& operator<<(ostream& os, const ModInt& p) {
return os << p.x;
}
friend istream& operator>>(istream& is, ModInt& a) {
int64_t t;
is >> t;
a = ModInt< mod >(t);
return (is);
}
static int get_mod() { return mod; }
};
using modint = ModInt< mod998 >;
template<typename T>
T gcd(T x, T y) {
if (y == 0) return x;
else return gcd(y, x % y);
}
template<typename T>
T lcm(T x, T y) {
return x / gcd(x, y) * y;
}
int main() {
int N; cin >> N;
vec<int> A(N); cin >> A;
int ans = 0;
rep(i, 0, N) {
ans += max(0, A[i] - 10);
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
int count = 0;
for (int i = 0; i < n; i++)
{
int a;
cin >> a;
if (a <= 10)
{
continue;
}
else
{
count += a - 10;
}
}
cout << count;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int sizeRes=0;
vector<pair<int,int> > res;
bool mergeSort(vector<int> &v, int from, int to, bool rev) {
if(from==to) return true;
if(rev) {
for(int c=from;c<to;c++) {
if(v[c+1]<=c && v[c] >= c+1) {
res[sizeRes++]=(make_pair(c,c+1));
swap(v[c],v[c+1]);
if(!mergeSort(v,from,c,false)) return false;
if(!mergeSort(v,c+1,to,true)) return false;
return true;
}
}
} else {
for(int c=to-1;c>=from;c--) {
if(v[c+1]<=c && v[c] >= c+1) {
res[sizeRes++]=(make_pair(c,c+1));
swap(v[c],v[c+1]);
if(!mergeSort(v,from,c,false)) return false;
if(!mergeSort(v,c+1,to,true)) return false;
return true;
}
}
}
return false;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int N;
cin>>N;
vector<int> v(N);
res.resize(N-1);
for(int c=0;c<N;c++) {
cin>>v[c];
v[c]--;
}
if(!mergeSort(v,0,N-1,true)) {
cout<<-1;
return 0;
}
for(int c=0;c+1<v.size();c++) {
if(v[c]>v[c+1]) {
cout<<-1;
return 0;
}
}
for(auto z:res) {
cout<<z.first+1<<"\n";
}
}
| #pragma warning(disable:4996)
#include <stdio.h>
#include <set>
using namespace std;
set<int> a;
int main()
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
int c;
scanf("%d", &c);
a.insert(c);
}
while (*a.begin() != *(--a.end()))
{
int mn = *a.begin();
int mx = *(--a.end());
a.erase(mx);
a.insert(mx - mn);
}
printf("%d", *a.begin());
} |
/*
15
63 25 66 36 39 29 93 3 188 128 191 82 179 178 168
*/
#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 pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define endl '\n'
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
const ll mod = 1000000007;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
using State = pair<pii, bool>;
vi a;
// what cases do i have to handle?
// if the mod = 0, need to not count degenerate one
vi solve(vector<vector<vector<State>>>& prev, pii initial, bool ig) {
vi ans;
int whichDefault = (ig ? -1 : -2);
bool once = false;
while(initial != mp(-1, -1)) {
int row = initial.first;
int col = initial.second;
//cout << row << " " << col << endl << flush;
int len = sz(prev[row][col]);
if(len + whichDefault >= 0) {
// cout << len << " " << whichDefault << endl << flush;
//assert(len + whichDefault < len);
State taken = prev[row][col][len + whichDefault];
if(taken.second) {
ans.pb(taken.first.first + 1);
}
initial = taken.first;
if(!once) {
whichDefault = -1;
once = true;
}
} else {
// take 0
State taken = prev[row][col][0];
// it means that to get to the current state, we took from the previous one
if(taken.second) {
ans.pb(taken.first.first + 1);
}
initial = taken.first;
}
}
reverse(all(ans));
return ans;
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(nullptr);
// if there exists 2 different elements = x mod 200, then obv
int n; cin >> n;
a.resize(n); trav(x, a) cin >> x;
map<int,vector<int>> cnt;
for(int i = 0; i < n; i++) {
cnt[a[i] % 200].pb(i);
}
trav(p, cnt) {
if(sz(p.second) >= 2) {
cout << "Yes" << endl;
cout << 1 << " " << p.second[0] + 1 << endl;
cout << 1 << " " << p.second[1] + 1 << endl;
return 0;
}
}
// includes the dummy subsequence
vector<vector<ll>> dp(n + 1, vector<ll>(200, 0));
vector<vector<vector<State>>> prev(n + 1, vector<vector<State>>(200));
dp[0][0] = 1;
prev[0][0].pb({mp(-1,-1), false});
for(int i = 0; i < n; i++) {
for(int j = 0; j < 200; j++) {
ll here = dp[i][j];
if(here != 0) {
// include this element
dp[i + 1][(j + a[i]) % 200] += here;
if(prev[i + 1][(j + a[i]) % 200].size() <= 3) {
prev[i + 1][(j + a[i]) % 200].pb({mp(i, j), true});
}
dp[i + 1][j] += here;
if(prev[i + 1][j].size() <= 3) {
prev[i + 1][j].pb({mp(i,j), false});
}
}
}
}
vi b, c;
for(int j = 0; j <= n; j++) {
for(int i = 0; i < 200; i++) {
if(i == 0) {
if(dp[j][i] >= 3) {
cout << "Yes" << endl;
b = solve(prev, {n, i}, false);
c = solve(prev, {n, i}, true);
cout << sz(b) << " "; trav(x, b) cout << x << " ";
cout << endl;
cout << sz(c) << " "; trav(x, c) cout << x << " ";
cout << endl;
return 0;
}
} else {
if(dp[j][i] >= 2) {
cout << "Yes" << endl;
b = solve(prev, {n, i}, false);
c = solve(prev, {n, i}, true);
cout << sz(b) << " "; trav(x, b) cout << x << " ";
cout << endl;
cout << sz(c) << " "; trav(x, c) cout << x << " ";
cout << endl;
return 0;
}
}
}
}
cout << "No" << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(0);cin.tie(0)
#define fp(i,a,b) for(int i=a ; i<b ; i++)
#define fn(i,a,b) for(int i=a ; i>=b ; i--)
#define fit(i,x) for(auto i=x.begin() ; i != x.end() ; i++)
#define pb push_back
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<ll> vll;
const int M=3e3+5;
const int INF=1e9;
const int MOD=998244353;
ll dp[M][M<<1];
int main(){
fastio;
int n,k; cin >> n >> k;
dp[0][0] = 1;
fp(i,1,M)
fn(j,i,1){
dp[i][j] = dp[i-1][j-1];
if (2*j <= i) dp[i][j] = (dp[i][j] + dp[i][j<<1]) % MOD;
}
cout << dp[n][k] << "\n";
return 0;
} |
#include <iostream>
#include <string>
#include <vector>
using ll=long long;
using namespace std;
// auto mod int
//const int mod = 1000000007;
//const int mod = 998244353;
int mod;
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);}
//supports not prime
mint modinv(ll a) {
long long 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);
}
u %= mod;
if (u < 0) u += mod;
mint res(u);
return res;
}
mint& operator/=(const mint a) { return *this *= modinv(a.x);}
mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
int main(){
int N,K;
cin>>N>>K>>mod;
int MX=N*(N+1)*K/2+1;
vector<vector<mint>> dp(N+1, vector<mint> (MX));
dp[0][0]=1;
int s=0;
for(int i=1;i<=N;i++){
vector<mint> ps(i);
s+=i;
for(int j=0;j<=s*K;j++){
int x=j%i;
ps[x]+=dp[i-1][j];
if(j-i*(K+1)>=0){
ps[x]-=dp[i-1][j-i*(K+1)];
}
dp[i][j]=ps[x];
}
}
s=0;
for(int i=1;i<=N;i++){
s+=i;
mint ans;
for(int t=0;t<=s*K;t++){
ans+=dp[i-1][t]*dp[N-i][t];
}
ans=ans*(K+1)-1;
cout<<ans<<endl;
}
return 0;
} | #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;
const ll maxN = 880;
ll N, K, C;
ll A[maxN][maxN], S[maxN][maxN];
bool OK(ll X) {
for (ll i=1; i<=N; i++) {
for (ll j=1; j<=N; j++) {
if (A[i][j]>X) S[i][j] = 1;
else S[i][j] = 0;
}
}
for (ll i=1; i<=N; i++) {
for (ll j=1; j<=N; j++) {
S[i][j] += S[i][j-1];
}
}
for (ll j=1; j<=N; j++) {
for (ll i=1; i<=N; i++) {
S[i][j] += S[i-1][j];
}
}
for (ll i=K; i<=N; i++) {
for (ll j=K; j<=N; j++) {
ll cnt = S[i][j] - S[i-K][j] - S[i][j-K] + S[i-K][j-K];
if (cnt<C) return true;
}
}
return false;
}
int main() {
cin >> N >> K;
C = K*K/2 + 1;
for (ll i=1; i<=N; i++) {
for (ll j=1; j<=N; j++) {
cin >> A[i][j];
}
}
ll l = -inf, r = inf;
while (r-l>1) {
ll m = (l+r)/2;
if (OK(m)) r = m;
else l = m;
}
Cout(r);
} |
#include <cstring>
#include <cstdlib>
#include <map>
#include <unordered_map>
#include <string>
#include <list>
#include <vector>
#include <algorithm>
#include <queue>
#include <cmath>
#include <stack>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <set>
#include <unordered_set>
using namespace std;
#define ABS(a) ((a)<0?(-(a)):(a))
#define CS while(cases())
#define I(n) int n = gi()
#define II(n) n = gi()
#define L(n) ll n = gli()
#define LL(n) n = gli()
#define F(i, n) for (int i = 0; i < n; i++)
#define Fa(n, a) for (int i = 0; i < n; i++) a[i] = gi()
#define R(i, n) for (int i = n-1; i >= 0; i--)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef pair<int, int> pii;
template<class T>
T gcd(T a, T b) {
return (b == 0 ? a : gcd(b, a%b));
}
int gi() {
int a;
scanf("%d", &a);
return a;
}
ll gli() {
ll a;
scanf("%lld", &a);
return a;
}
int cases() {
static int t = gi();
return t--;
}
#define MD 1000000007
#define PI 3.141592653589793238462643383279502884L
#define EP 0.000000001L
int within(double a, double b) {
return (a >= b - EP && a <= b + EP);
}
void rotate(double &x, double &y, double r) {
double l = sqrt(x*x + y*y);
double a = atan2(y, x) + r;
if (a > PI)
a -= 2.0L * PI;
if (within(a, PI * 0.5L)) {
x = 0;
y = l;
return;
}
if (within(a, PI * -0.5L)) {
x = 0;
y = -l;
return;
}
if (within(a, 0)) {
x = l;
y = 0;
return;
}
if (within(a, PI) || within(a, -PI)) {
x = -l;
y = 0;
return;
}
y = tan(a);
x = 1;
if (a <= PI * -0.5L || a > PI * 0.5L) {
x *= -1.0L;
y *= -1.0L;
}
double z = sqrt(x*x + y*y);
x *= l/z;
y *= l/z;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
I(n);
II(double x);
II(double y);
II(double X);
II(double Y);
X = (x + X) * 0.5L;
Y = (y + Y) * 0.5L;
x -= X;
y -= Y;
double r = PI / double(n/2);
rotate(x, y, r);
cout << setprecision(10) << x+X << " " << y+Y << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define MOD2 998244353
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define reps(i,s,n) for (int i = (int)(s); i < (int)(n); i++)
#define repit(i,C) for (auto i = (C).begin(); i != (C).end(); i++)
#define pr(a) cout << a
#define prl(a) cout << (a) << endl
#define prld(a) cout << setprecision(15)<< (a) << endl
#define allrange(a) a.begin(),a.end()
bool solve_translated(vector<pair<int,int>> &S,vector<pair<int,int>> &T){
int N = S.size();
int dx = S[0].first-T[0].first;
int dy = S[0].second-T[0].second;
bool flg = true;
reps(i,1,N){
if(!((S[i].first==T[i].first+dx) && (S[i].second==T[i].second+dy))){flg = false; break;}
}
return flg;
}
/*ピタゴラ三角形
5 12 13
8 15 17
3 4 5
*/
vector<pair<int,int>> Pitagora_rot(vector<pair<int,int>> &S,int a, int b , int c){
int N = S.size();
vector<pair<int,int>> PS(N);
auto pt0 = S[0];
PS[0] = pt0;
bool flg = true;
reps(i,1,N){
int dx = S[i].first - pt0.first;
int dy = S[i].second - pt0.second;
int x=dx*a-dy*b;
int y=dx*b+dy*a;
if(((x%c)!=0) || ((y%c)!=0)) {flg = false; break;}
PS[i] = make_pair(x/c+pt0.first, y/c+pt0.second);
}
if(flg) return PS;
else return vector<pair<int,int>>();
}
int main(){
std::cin.tie(0); // cinとcoutの同期を解除
std::ios::sync_with_stdio(false);
int N;cin >> N;
vector<pair<int,int>> S(N),T(N);
rep(i,N)
{
int x,y;
cin >> x >> y;
S[i].first = x;S[i].second = y;
}
rep(i,N)
{
int x,y;
cin >> x >> y;
T[i].first = x;
T[i].second = y;
}
sort(allrange(T));
/*ピタゴラ三角形
5 12 13
8 15 17
3 4 5
*/
/* vector<vector<int>> tri(12);
tri[0]={0,1,1};
tri[1]={-1,0,1};
tri[2]={0,-1,1};
tri[3]={1,0,1};
tri[4]={3,4,5};
tri[5]={4,3,5};
tri[6]={-3,4,5};
tri[7]={-4,3,5};
tri[8]={-3,-4,5};
tri[9]={-4,-3,5};
tri[10]={3,-4,5};
tri[11]={4,-3,5};*/
vector<vector<int>> tri(0);
tri.push_back(vector<int>({3,4,5}));
tri.push_back(vector<int>({4,3,5}));
tri.push_back(vector<int>({-3,4,5}));
tri.push_back(vector<int>({-4,3,5}));
tri.push_back(vector<int>({0,1,1}));
tri.push_back(vector<int>({-1,0,1}));
tri.push_back(vector<int>({0,-1,1}));
tri.push_back(vector<int>({1,0,1}));
tri.push_back(vector<int>({-3,-4,5}));
tri.push_back(vector<int>({-4,-3,5}));
tri.push_back(vector<int>({3,-4,5}));
tri.push_back(vector<int>({4,-3,5}));
// tri.push_back(vector<int>({-5, -12, 13}));
// tri.push_back(vector<int>({-12, -5, 13}));
// tri.push_back(vector<int>({-8,-15,17}));
// tri.push_back(vector<int>({-15,-8,17}));
// tri.push_back(vector<int>({5, 12, 13}));
// tri.push_back(vector<int>({12, 5, 13}));
// tri.push_back(vector<int>({8,15,17}));
// tri.push_back(vector<int>({15,8,17}));
// tri.push_back(vector<int>({-5, 12, 13}));
// tri.push_back(vector<int>({-12, 5, 13}));
// tri.push_back(vector<int>({-8,15,17}));
// tri.push_back(vector<int>({-15,8,17}));
// tri.push_back(vector<int>({5, -12, 13}));
// tri.push_back(vector<int>({12, -5, 13}));
// tri.push_back(vector<int>({8,-15,17}));
// tri.push_back(vector<int>({15,-8,17}));
bool flg;
if(S.size()==2 && T.size()==2){
auto sx = S[0].first-S[1].first;
auto sy = S[0].second-S[1].second;
auto tx = T[0].first-T[1].first;
auto ty = T[0].second-T[1].second;
flg= (sx*sx+sy*sy==tx*tx+ty*ty);
} else {
rep(j,tri.size()){
auto S2 = Pitagora_rot(S,tri[j][0],tri[j][1],tri[j][2]);
if(S2.empty()) continue;
sort(allrange(S2));
flg = solve_translated(S2,T);
if(flg) break;
}
}
if(flg) prl("Yes"); else prl("No");
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 2e3 + 5;
const int mod = 1e9 + 7;
int n, m, l[N][N], r[N][N], u[N][N], d[N][N], two[N*N];
char s[N][N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
two[0] = 1;
for(int i=1; i<=n*m; i++) two[i] = 2ll*two[i-1]%mod;
for(int i=1; i<=n; i++) cin >> (s[i]+1);
int k = 0;
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
if(s[i][j]=='.') k++;
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
if(s[i][j]=='#') u[i][j] = 0, l[i][j] = 0;
else u[i][j] = u[i-1][j] + 1, l[i][j] = l[i][j-1] + 1;
for(int i=n; i>=1; i--)
for(int j=m; j>=1; j--)
if(s[i][j]=='#') d[i][j] = 0, r[i][j] = 0;
else d[i][j] = d[i+1][j] + 1, r[i][j] = r[i][j+1] + 1;
int ans = 0;
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
{
if(s[i][j]=='#') continue;
int x = l[i][j] + r[i][j] - 1, y = u[i][j] + d[i][j] - 1;
ans = (ans - two[k-1] + mod)%mod;
ans = (ans - 1ll*two[k-x-y+1]*(two[x+y-2]-two[x-1]-two[y-1]+1)%mod + mod)%mod;
}
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
{
if(s[i][j]=='#') continue;
int x = r[i][j];
ans = (ans + 1ll*x*(two[x]-1)%mod*two[k-x])%mod;
j += r[i][j] - 1;
}
for(int j=1; j<=m; j++)
for(int i=1; i<=n; i++)
{
if(s[i][j]=='#') continue;
int y = d[i][j];
ans = (ans + 1ll*y*(two[y]-1)%mod*two[k-y])%mod;
i += d[i][j] - 1;
}
if(ans<0) ans += mod;
cout << ans << '\n';
return 0;
} | #include "bits/stdc++.h"
#define MOD 1000000007
#define rep(i, n) for(ll i=0; i < (n); i++)
#define rrep(i, n) for(ll i=(n)-1; i >=0; i--)
#define ALL(v) v.begin(),v.end()
#define rALL(v) v.rbegin(),v.rend()
#define FOR(i, j, k) for(ll i=j;i<k;i++)
#define debug_print(var) cerr << #var << "=" << var <<endl;
#define DUMP(i, v)for(ll i=0;i<v.size();i++)cerr<<v[i]<<" "
#define fi first
#define se second
using namespace std;
typedef long long int ll;
typedef vector<ll> llvec;
typedef vector<double> dvec;
typedef pair<ll, ll> P;
typedef long double ld;
struct edge{ll x, c;};
ll mod(ll a, ll mod){
ll res = a%mod;
if(res<0)res=res + mod;
return res;
}
ll modpow(ll a, ll n, ll mod){
ll res=1;
while(n>0){
if(n&1) res=res*a%mod;
a=a*a%mod;
n>>=1;
}
return res;
}
ll modinv(ll a, ll mod){
ll b=mod, u=1, v=0;
while(b){
ll t=a/b;
a-=t*b; swap(a, b);
u-=t*v; swap(u, v);
}
u%=mod;
if(u<0)u+=mod;
return u;
}
ll gcd(ll a, ll b){
ll r = a%b;
if(r==0) return b;
else return gcd(b, a%b);
}
bool is_prime(ll n){
ll i = 2;
if(n==1)return false;
if(n==2)return true;
bool res = true;
while(i*i <n){
if(n%i==0){
res = false;
}
i = i+1;
}
//if(i==1)res = false;
if(n%i==0)res=false;
return res;
}
/**************************************
** A main function starts from here **
***************************************/
int main(){
ll H, W;
cin >> H >> W;
vector<string> S(H);
rep(i, H)cin >> S[i];
vector<llvec> L(H+1, llvec(W+1, 0));
vector<llvec> R(H+1, llvec(W+1, 0));
vector<llvec> U(H+1, llvec(W+1, 0));
vector<llvec> B(H+1, llvec(W+1, 0));
rep(i, H){
rep(j, W){
U[i+1][j] = U[i][j] + 1;
if(S[i][j]=='#')U[i+1][j]=0;
}
}
rep(i, H){
rep(j, W){
B[H-i-1][j] = B[H-i][j] + 1;
if(S[H-i-1][j]=='#')B[H-i-1][j]=0;
}
}
rep(i, H){
rep(j, W){
L[i][j+1] = L[i][j] + 1;
if(S[i][j]=='#')L[i][j+1]=0;
}
}
rep(i, H){
rep(j, W){
R[i][W-j-1] = R[i][W-j] + 1;
if(S[i][W-j-1]=='#')R[i][W-j-1]=0;
}
}
ll m=0;
rep(i, H)rep(j, W)if(S[i][j]=='.')m++;
/*
cerr << m << endl;
rep(i, H){DUMP(k, L[i]);cerr << endl;}
rep(i, H){DUMP(k, R[i]);cerr << endl;}
rep(i, H){DUMP(k, U[i+1]);cerr << endl;}
rep(i, H){DUMP(k, B[i]);cerr << endl;}*/
ll ans = 0;
rep(i, H){
rep(j, W){
if(S[i][j]=='.'){
ll c = L[i][j+1]+U[i+1][j] + B[i][j] + R[i][j] - 3;
ans = mod(ans + mod(modpow(2, m-c, MOD)*(modpow(2,c, MOD)-1), MOD), MOD);
}
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
#define fir first
#define sec second
#define PB push_back
#define MP make_pair
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i < (int)(n); ++i)
#define foreach(itr, c) for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); ++itr)
template <class T> void chkmax(T& x, T y) { x = (x > y ? x : y); }
template <class T> void chkmin(T& x, T y) { x = (x < y ? x : y); }
typedef long long LL;
typedef pair <int, int> pii;
typedef vector <int> vi;
const int N = 105;
const int M = 1e4 + 5;
int n, m, a[M], b[M], c[N];
vi e[N];
set <int> e2[N];
bool vis[N];
void dfs(int now, int from){
vis[now] = 1;
for (auto u : e[now]) if (u != from && c[u] == c[now]){
if (e2[u].find(now) != e2[u].end()) continue;
e2[now].insert(u);
if (!vis[u]) dfs(u, now);
}
}
int main(){
scanf("%d%d", &n, &m);
rep(i, m){
scanf("%d%d", &a[i], &b[i]), a[i]--, b[i]--;
e[a[i]].PB(b[i]);
e[b[i]].PB(a[i]);
}
rep(i, n) scanf("%d", &c[i]);
rep(i, m){
if (c[a[i]] > c[b[i]]) e2[a[i]].insert(b[i]);
else if (c[a[i]] < c[b[i]]) e2[b[i]].insert(a[i]);
else {
if (e2[a[i]].find(b[i]) != e2[a[i]].end() || e2[b[i]].find(a[i]) != e2[b[i]].end())
continue;
dfs(b[i], a[i]);
}
}
rep(i, m){
if (e2[a[i]].find(b[i]) != e2[a[i]].end()) printf("->\n");
else printf("<-\n");
}
return 0;
} | #include <bits/stdc++.h>
// #define int long long
#define ff first
#define ss second
#define ll long long
#define ld long double
#define pb push_back
#define eb emplace_back
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ti tuple<int, int, int>
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define sws ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl '\n'
#define teto(a, b) (((a)+(b)-1)/(b))
#define all(x) x.begin(), x.end()
#define forn(i, n) for(int i = 0; i < (int)n; i++)
#define forne(i, a, b) for(int i = a; i <= b; i++)
#define dbg(msg, var) cerr << msg << " " << var << endl;
using namespace std;
const int MAX = 200010;
const ll MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const ld EPS = 1e-8;
const ld PI = acos(-1);
// End Template //
int c[MAX];
bool ans[MAX];
vi alc(MAX, 1);
vector<vi> g(MAX);
bool visto[MAX];
map<pii, pii> edg; // (a,b) -> (direita, idx)
void dfs(int u){
if(visto[u]) return;
visto[u]=true;
for(auto v: g[u]){
// if(visto[v]){
// pii e = mm[{}];
// }else{
// }
pii e = edg[{u, v}];
ans[e.ss] = e.ff;
dfs(v);
}
}
int32_t main()
{sws;
int n, m, u, v;
cin >> n >> m;
vii ee;
for(int i=0;i<m;i++){
cin >> u >> v; u--;v--;
ee.pb({u, v});
}
for(int i=0;i<n;i++)
cin >> c[i];
for(int i=0;i<m;i++){
int u=ee[i].ff, v=ee[i].ss;
if(c[u]!=c[v]){
ans[i] = (c[u] > c[v]);
continue;
}
g[u].pb(v);
g[v].pb(u);
edg[{u, v}] = {1, i};
edg[{v, u}] = {0, i};
}
for(int i=0;i<n;i++)
if(!visto[i])
dfs(i);
// output
for(int i=0;i<m;i++){
if(ans[i])
cout << "->\n";
else
cout << "<-\n";
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
double A, B;
cin >> A >> B;
double ans = 0;
ans = A * B / 100;
cout << ans << endl;
} | #include <bits/stdc++.h>
#define ll long long
#define fo(a,b,c) for(int a=b;a<=c;a++)
using namespace std;
int main()
{
int n,k;
cin>>n>>k;
int c=n,d=k;
int a=0,b=0;;
while(n){
a+=n;
n--;
}
while(k){
b+=k;
k--;
}
b*=c;
a*=d;
cout<<a*100+b;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define maxn 100005
#define int long long
unsigned long long n;
int a,b;
int C[100][100];
signed main()
{
cin>>a>>b>>n;
C[1][0]=1,C[1][1]=1;
for(int i=2;i<=60;i++)
{
for(int j=0;j<=i;j++)
{
C[i][j]=C[i-1][j]+C[i-1][j-1];
}
}
while(a>=1&&b>=1)
{
if(n>C[a-1+b][b]) n-=C[a-1+b][b],putchar('b'),b--;
else putchar('a'),a--;
// cout<<n<<endl;
}
// cout<<endl<<a<<' '<<b<<endl;
for(int i=1;i<=a;i++) putchar('a');
for(int i=1;i<=b;i++) putchar('b');
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD=1000000007;
#define INF 1LL<<60
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
template <class T>
bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
int V;
ll G[20][20]; // グラフ
ll dp[200000][20];
int main() {
cin >> V;
// グラフの初期化
rep(i, 20) {
rep(j, 20) { G[i][j] = INF; }
}
vector<ll> x(V),y(V),z(V);
rep(i, V) {
cin>>x[i]>>y[i]>>z[i];
}
rep(i,V){
rep(j,V){
G[i][j]=abs(x[j]-x[i])+abs(y[j]-y[i])+max(0LL,z[j]-z[i]);
}
}
// dp の初期化
rep(i, 200000) {
rep(j, 20) { dp[i][j] = INF; }
}
dp[0][0] = 0; // スタートを頂点 0 とする
rep(S, 1 << V) {
rep(v, V) {
rep(u, V) {
if ((S & (1 << v)) == 0) {
if (v != u) chmin(dp[S | (1 << v)][v], dp[S][u] + G[u][v]);
}
}
}
}
if (dp[(1 << V) - 1][0] != INF) {
cout << dp[(1 << V) - 1][0] << endl;
} else {
cout << -1 << endl;
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=(a); i<(n); i++)
#define rep2(i,a,n) for (int i=(a); i<=(n); i++)
using ll = long long;
using ld = long double;
using P = pair<int,int>;
int main() {
string s; cin >> s;
deque<char> d;
bool rev = false;
rep(i,0,s.size()) {
char c = s.at(i);
if(c == 'R') rev = !rev;
else {
if(rev) d.push_front(c);
else d.push_back(c);
}
}
if(rev) reverse(d.begin(), d.end());
string ans = "";
rep(i,0,d.size()) {
char c = d[i];
if(ans.size() != 0 && ans.back() == c) ans.pop_back();
else ans += c;
}
cout << ans << endl;
} | #include <bits/stdc++.h>
// clang-format off
using namespace std; using ll=long long; using ull=unsigned long long; using pll=pair<ll,ll>; const ll INF=4e18;
void print0() {}
template<typename H,typename... T> void print0(H h,T... t){cout<<fixed<<setprecision(15)<<h;print0(t...);}
void print() { print0("\n"); }
template<typename H,typename... T>void print(H h,T... t){print0(h);if(sizeof...(T)>0)print0(" ");print(t...);}
void ioinit() { cout << fixed << setprecision(15); ios_base::sync_with_stdio(0); cin.tie(0); }
// clang-format on
void output(vector<vector<ll>> &use, vector<vector<ll>> &nouse, ll end) {
ll n = use.size();
ll j = end;
vector<ll> ru;
for (ll i = n - 1; i >= 0; i--) {
if (use[i][j] != j) {
ru.push_back(i);
}
j = use[i][j];
}
j = end;
vector<ll> rn;
for (ll i = n - 1; i >= 0; i--) {
if (nouse[i][j] != j) {
rn.push_back(i);
}
j = nouse[i][j];
}
reverse(ru.begin(), ru.end());
reverse(rn.begin(), rn.end());
print("Yes");
print0(ru.size());
for (ll i = 0; i < (ll)ru.size(); i++) {
print0(" ", 1 + ru[i]);
}
print();
print0(rn.size());
for (ll i = 0; i < (ll)rn.size(); i++) {
print0(" ", 1 + rn[i]);
}
print();
}
int main() {
ioinit();
ll n;
cin >> n;
vector<ll> a(n);
ll zero = INF;
for (ll i = 0; i < n; i++) {
cin >> a[i];
a[i] = a[i] % 200;
if (a[i] == 0) {
zero = i;
}
}
if (zero != INF) {
print("Yes");
if (zero == n - 1) {
print("2 1", (zero + 1));
print("1 1");
} else {
print(2, (zero + 1), (zero + 2));
print(1, (zero + 2));
}
return 0;
}
for (ll u = 0; u < n; u++) {
vector<vector<ll>> use(n, vector<ll>(200, -1));
vector<vector<ll>> nouse(n, vector<ll>(200, -1));
if (u == 0) {
use[0][a[0]] = 0;
nouse[0][0] = 0;
} else {
use[0][0] = 0;
use[0][a[0]] = 0;
nouse[0][0] = 0;
nouse[0][a[0]] = 0;
}
for (ll i = 1; i < n; i++) {
for (ll j = 0; j < 200; j++) {
ll nj = (j + a[i]) % 200;
if (use[i - 1][j] >= 0) {
use[i][nj] = j;
if (u != i) use[i][j] = j;
}
if (nouse[i - 1][j] >= 0) {
if (u != i) nouse[i][nj] = j;
nouse[i][j] = j;
}
}
for (ll j = 1; j < 200; j++) {
if (use[n - 1][j] >= 0 && nouse[n - 1][j] >= 0) {
output(use, nouse, j);
return 0;
}
}
}
}
print("No");
}
|
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define fi first
#define se second
#define mset(a, b) memset(a, b, sizeof(a))
using ll = long long;
using pii = pair<int, int>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class Ty> Ty randint(Ty a, Ty b) { return uniform_int_distribution<Ty>(a, b)(rng); }
template<class T> void DBG(T&& x) { cerr << x << " "; }
template<class T, class...Args> void DBG(T&& x, Args&&... args) { DBG(x); DBG(args...); }
#define DBG(...) { cerr << "[" << #__VA_ARGS__ << "]: "; DBG(__VA_ARGS__); cerr << endl; }
template<class num> inline void rd(num& x) { cin >> x; }
template <class Ty, class... Args> inline void rd(Ty& x, Args&... args) { rd(x); rd(args...); }
template<class num> inline void print(num&& x) { cout << x; }
template <class Ty, class... Args> inline void print(Ty&& x, Args&&... args) { print(x); print(' '); print(args...); }
#define print(...) print(__VA_ARGS__), print('\n')
const int MAXN = 1e5 + 5;
struct Edge {
int v;
ll t, k;
Edge(int v, ll t, ll k) : v(v), t(t), k(k) {}
};
vector<Edge> edges[MAXN];
ll dist[MAXN];
ll find_next(ll cur, ll t, ll k) {
ll next = (cur + k - 1) / k * k;
return next + t;
}
ll dijkstra(int s, int t) {
fill(dist, dist+MAXN, LLONG_MAX);
dist[s] = 0;
priority_queue<pii> pq;
pq.emplace(0, s);
while(pq.size()) {
auto [d, u] = pq.top();
pq.pop();
if (-d > dist[u]) continue;
for (auto e : edges[u]) if (dist[e.v] > find_next(dist[u], e.t, e.k)) {
dist[e.v] = find_next(dist[u], e.t, e.k);
pq.emplace(-dist[e.v], e.v);
}
}
return dist[t] == LLONG_MAX ? -1 : dist[t];
}
inline void run_test(int test_number) {
int n, m, a, b; rd(n, m, a, b);
for (int i = 0; i < m; i++) {
int u, v, t, k; rd(u, v, t, k);
edges[u].emplace_back(v, t, k);
edges[v].emplace_back(u, t, k);
}
print(dijkstra(a, b));
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int n_tests = 1;
for (int i = 1; i <= n_tests; i++) run_test(i);
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for (ll i = 0; i < (n); ++i)
int main(){
int a,b,c,d;
std::cin >> a >>b>>c>>d;
std::cout << a*d-b*c << '\n';
return 0;
}
|
#include <bits/stdc++.h>
#define przxct ""
#define fto(i,j,h) for (int i=j; i<=h; ++i)
#define maxn
#define ll long long
#define pi 3.141592653589
using namespace std;
string x;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> x;
string rs = "";
fto(i,0,x.length()-1)
{
if (x[i] == '.') break;
rs += x[i];
}
cout << rs;
return 0;
}
| #include <bits/stdc++.h>
typedef long long ll;
#define rep(i, n) for(long long i = 0; i < (long long)(n); i++)
#define rep1(i, n) for(long long i = 1; i <= (long long)(n); i++)
#define INF 1000000000000 //10^12:∞
#define MOD 1000000007
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
using namespace std;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll n,saidai,m;
ll stop = 0;
cin >> n;
saidai = n+1;
m = saidai/2+1;
rep1(i,m){
if(saidai-i>=0){
saidai = saidai-i;
stop++;
}else{
break;
}
}
cout << n-stop+1;
} |
#include <iostream>
int main() {
int x, y;
std::cin >> x >> y;
for (int i = y; i >= 1; i--) {
if ((x + i - 1) / i * i <= y && (x + i - 1) / i * i + i <= y) {
std::cout << i;
break;
}
}
return 0;
} | #include<bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define FOR(i,a,b) for (int i = a; i < b; ++i)
#define FORR(i,a,b) for (int i = b - 1; i >= a; --i)
#define REP(i,n) FOR(i,0,n)
#define REPR(i,n) FORR(i,0,n)
template <typename T> bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }
template <typename T> bool chmin(T &m, const T q) { if (m > q) {m = q; return true;} else return false; }
template <typename T1, typename T2> pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first + r.first, l.second + r.second); }
template <typename T1, typename T2> pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return make_pair(l.first - r.first, l.second - r.second); }
template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (auto &v : vec) is >> v; return is; }
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << '['; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <typename T, size_t sz> ostream &operator<<(ostream &os, const array<T, sz> &arr) { os << '['; for (auto v : arr) os << v << ','; os << ']'; return os; }
template <typename T> ostream &operator<<(ostream &os, const deque<T> &vec) { os << "deq["; for (auto v : vec) os << v << ','; os << ']'; return os; }
template <typename T> ostream &operator<<(ostream &os, const set<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T, typename TH> ostream &operator<<(ostream &os, const unordered_set<T, TH> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T> ostream &operator<<(ostream &os, const multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T> &vec) { os << '{'; for (auto v : vec) os << v << ','; os << '}'; return os; }
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa) { os << '(' << pa.first << ',' << pa.second << ')'; return os; }
template <typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
template <typename TK, typename TV, typename TH> ostream &operator<<(ostream &os, const unordered_map<TK, TV, TH> &mp) { os << '{'; for (auto v : mp) os << v.first << "=>" << v.second << ','; os << '}'; return os; }
// #define DEBUG
#ifdef DEBUG
const string COLOR_RESET = "\033[0m", BRIGHT_GREEN = "\033[1;32m", BRIGHT_RED = "\033[1;31m", BRIGHT_CYAN = "\033[1;36m", NORMAL_CROSSED = "\033[0;9;37m", RED_BACKGROUND = "\033[1;41m", NORMAL_FAINT = "\033[0;2m";
#define dbg(x) cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) << NORMAL_FAINT << " (L" << __LINE__ << " in " << __FILE__ << ")" << COLOR_RESET << endl
#else
#define dbg(x) (x)
#endif
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
vector<vector<bool>> mat;
int A, B;
cin >> A >> B;
for (int c = B; ; c--) {
if ((A + c - 1) / c < B / c) {
cout << c << endl;
return 0;
}
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define FIN ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int main() {FIN
int n, k, m, i, s = 0, e, res;
cin >> n >> k >> m;
for (i = 0; i < n - 1; i++) {
cin >> e;
s += e;
}
res = -1;
for (i = 0; i <= k; i++) {
if ((s + i) / n >= m) {
res = i;
break;
}
}
cout << res << "\n";
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define lli long long int
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define FORD(i,a,b) for(int i=(a);i>=(b);i--)
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
const int INF = 1<<29;
typedef long long ll;
inline int two(int n) { return 1 << n; }
inline int test(int n, int b) { return (n>>b)&1; }
inline void set_bit(int & n, int b) { n |= two(b); }
inline void unset_bit(int & n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
inline int ones(int n) { int res = 0; while(n && ++res) n-=n&(-n); return res;}
int main()
{
lli n,k,m;
cin>>n>>k>>m;
lli avg=0;
for(int i=1;i<n;i++)
{lli x;
cin>>x;
avg+=x;}
lli ans=n*m-avg;
if(ans<=0)
cout<<0;
else if(ans<=k)
cout<<ans;
else
cout<<-1;
}
|
/*
Author : MatsuTaku
Date : 04/24/21
Certificate:
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDF9T447OEo8XSQ6O1AznN5tKC8mzvYc4Zs3+oOKfMqgLXfOwQnpfcoxKs4MAP1eICBD13PkcIezJ9IlV6nKQZKs1BQmvjSXJ+zKK8YCKvAueNPuNO0Bim43IBaNHNFWcMvcmUvHgRb6hUSO0V8I7OsjiFo20KDBj3gAznu9tir0Q== CompetitiveProgrammingCertification:[email protected]
*/
#include <bits/stdc++.h>
using namespace std;
#include <x86intrin.h>
#define REP(i, l, r) for (int i = (l), i##_less = (r); i < i##_less; i++)
#define rep(i, n) REP(i, 0, n)
#define RREP(i, l, r) for (int i = (r)-1, i##_least = (l); i >= i##_least; i--)
#define rrep(i, n) RREP(i, 0, n)
#define chmax(dst, x) dst = max(dst, (x))
#define chmin(dst, x) dst = min(dst, (x))
using lint = long long int;
using ulint = unsigned long long int;
template<typename T>
using vvec = vector<vector<T>>;
template<typename T>
vvec<T> make_vvec(int n, int m, T v) { return vvec<T>(n, vector<T>(m, v)); }
class Solver {
public:
Solver();
void solve();
};
Solver::Solver() {}
void Solver::solve() {
int n,m; cin>>n>>m;
vector<tuple<int,int,int>> R(m);
vector<vector<int>> I(n+1);
rep(i, 1<<n) {
I[__builtin_popcount(i)].push_back(i);
}
rep(i, m) {
int x,y,z; cin>>x>>y>>z;
R[i] = {x,y,z};
}
sort(R.begin(), R.end());
vector<lint> dp(1<<n), old;
dp[0] = 1;
int k = 0;
REP(i, 1, n+1) {
old.assign(1<<n, 0);
swap(dp, old);
int t = k;
while (t < m and get<0>(R[t]) == i) t++;
for (auto u : I[i-1]) {
// cerr<<bitset<18>(u)<<' '<<dp[u]<<endl;
rep(j, n) {
if ((u & (1<<j)) != 0) continue;
auto v = u | (1<<j);
bool ok = true;
REP(l, k, t) {
auto [x,y,z] = R[l];
auto vcnt = __builtin_popcount(v % (1<<y));
ok &= vcnt <= z;
}
if (ok)
dp[v] += old[u];
}
}
k = t;
}
cout << dp[(1<<n)-1] << endl;
}
int main() {
cin.tie(nullptr); ios::sync_with_stdio(false);
cout<<fixed<<setprecision(10);
Solver solver;
int t = 1;
// cin>>t;
while (t--) {
solver.solve();
}
return 0;
}
| #include <bits/stdc++.h>
#define REP(i,n) for (int i = 0; i < (n); ++i)
template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b;return true;}return false;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b;return true;}return false;}
using namespace std;
using ll = long long;
using P = pair<int,int>;
using Pl = pair<long long,long long>;
using veci = vector<int>;
using vecl = vector<long long>;
using vecveci = vector<vector<int>>;
using vecvecl = vector<vector<long long>>;
const int MOD = 1000000007;
const double pi = acos(-1);
ll gcd(ll a, ll b) {if(b == 0) return a; else return gcd(b,a%b);}
ll lcm(ll a, ll b) {return a*b/gcd(a,b);}
template<int MOD> struct Fp {
long long val;
constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
if (val < 0) val += MOD;
}
constexpr int getmod() { return MOD; }
constexpr Fp operator - () const noexcept {
return val ? MOD - val : 0;
}
constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
constexpr Fp& operator += (const Fp& r) noexcept {
val += r.val;
if (val >= MOD) val -= MOD;
return *this;
}
constexpr Fp& operator -= (const Fp& r) noexcept {
val -= r.val;
if (val < 0) val += MOD;
return *this;
}
constexpr Fp& operator *= (const Fp& r) noexcept {
val = val * r.val % MOD;
return *this;
}
constexpr Fp& operator /= (const Fp& r) noexcept {
long long a = r.val, b = MOD, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b; swap(a, b);
u -= t * v; swap(u, v);
}
val = val * u % MOD;
if (val < 0) val += MOD;
return *this;
}
constexpr bool operator == (const Fp& r) const noexcept {
return this->val == r.val;
}
constexpr bool operator != (const Fp& r) const noexcept {
return this->val != r.val;
}
friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept {
return os << x.val;
}
friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
if (n == 0) return 1;
auto t = modpow(a, n / 2);
t = t * t;
if (n & 1) t = t * a;
return t;
}
};
using mint = Fp<MOD>;
int main() {
int N; ll C;
cin >> N >> C;
map<int,ll> mp;
REP(i,N) {
int a,b; cin >> a >> b;
ll c; cin >> c;
mp[a] += c;
mp[b+1] -= c;
}
ll ans = 0;
int p = 0;ll now = 0;
for(auto x : mp) {
ans += (x.first-p) * min(C,now);
p = x.first;
now += x.second;
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=a;i>=n;i--)
#define pb push_back
#define mp make_pair
#define mem(a,b) memset(a,b,sizeof(a))
ll gcd(ll a,ll b){if(b==0)return a;else return gcd(b,a%b);}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
const int maxn=500;
ll a[maxn+5][maxn+5];
int main()
{
int n;
cin>>n;
rep(i,0,n)
{
rep(j,0,n)
cin>>a[i][j];
}
bool flag=1;
ll dif[n];
rep(i,0,n)
{
dif[i]=a[i][0]-a[(i+1)%n][0];
}
rep(i,0,n)
{
rep(j,0,n)
{
ll tmp=a[i][j]-a[(i+1)%n][j];
if(tmp!=dif[i])
{
cout<<"No"<<endl;
return 0;
}
}
}
rep(i,0,n)
{
dif[i]=a[0][i]-a[0][(i+1)%n];
}
rep(i,0,n)
{
rep(j,0,n)
{
ll tmp=a[j][i]-a[j][(i+1)%n];
if(tmp!=dif[i])
{
cout<<"No"<<endl;
return 0;
}
}
}
cout<<"Yes"<<endl;
ll min=0x3f3f3f3f3f3f3f3f;
int minum=0;
ll ans1[maxn+5];
ll ans2[maxn+5];
rep(i,0,n)
{
if(a[i][0]<min)
{
minum=i;
min=a[i][0];
}
}
rep(i,0,n)
{
ans2[i]=a[minum][i];
}
rep(i,0,n)
{
ans1[i]=a[i][0]-ans2[0];
}
rep(i,0,n)
{
cout<<ans1[i]<<" ";
}
cout<<endl;
rep(i,0,n)
{
cout<<ans2[i]<<" ";
}
cout<<endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define vi vector<int>
#define mii map<int,int>
#define pqb priority_queue<int>
#define pqs priority_queue<int,vi,greater<int> >
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define tc(t) int t;cin >> t;while(t--)
#define for0(i , n) for(int i=0;i<n;i++)
#define loop(i , a, b) for(int i=a;i<=b;i++)
#define endl '\n'
#define inf 1e18
#define fi first
#define se second
int XX[8] = { +1, +1, +1, 0, 0, -1, -1, -1};
int YY[8] = { +1, 0, -1, +1, -1, +1, 0, -1};
int dx[4] = { -1, 1, 0, 0};
int dy[4] = {0, 0, 1, -1};
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt" , "r" , stdin);
freopen("output.txt" , "w" , stdout);
#endif
int a, b, c, d;
cin >> a >> b >> c >> d;
if (a == b + c + d) {
cout << "Yes";
} else if (b == a + c + d) {
cout << "Yes";
} else if (c == a + b + d) {
cout << "Yes";
} else if (d == a + b + c) {
cout << "Yes";
} else if ((a + b) == (c + d)) {
cout << "Yes";
} else if ((a + c) == (b + d)) {
cout << "Yes";
} else if ((a + d) == (b + c)) {
cout << "Yes";
} else {
cout << "No";
}
return 0;
} |
#include<iostream>
using namespace std;
using ll = long long;
ll sum_comb(ll n) {
if (n <= 2) return 0;
return (n - 1) * (n - 2) / 2;
}
ll sum_comb2(ll n, ll N) {
return (sum_comb(n) - sum_comb(n - 3 * N)) - 3 * (sum_comb(n - N) - sum_comb(n - 2 * N));
}
int main() {
int N; ll K;
cin >> N >> K;
ll cnt = 0; int sum = 3;
while (true) {
cnt += sum_comb2(sum, N);
if (cnt >= K) break;
sum++;
}
cnt -= sum_comb2(sum, N);
int i = 1;
while (true) {
cnt += max(0, min(sum - i - 1, 2 * N - sum + i + 1));
if (cnt >= K) break;
i++;
}
cnt -= min(sum - i - 1, 2 * N - sum + i + 1);
int j = K - cnt + max(0, sum - i - N - 1);
int k = sum - i - j;
cout << i << " " << j << " " << k << endl;
} | #include<bits/stdc++.h>
#define int long long
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC optimize("unroll-loops")
#define ri register signed
#define rd(x) x=read()
#define fi first
#define se second
#define mp make_pair
using namespace std;
const int N=3e6+5;
const int M=31;
const int mod=1e9+7;
inline int read(){int x=0,f=1;char ch=getchar();while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;}
inline int ksm(int x,int y=mod-2,int z=mod){int ret=1;while (y){if (y&1) ret=(ret*x)%z;x=(x*x)%z;y>>=1;}return ret;}
int inv[N],fac[N],ifc[N];
void Init(int n)
{
inv[1]=1;for (ri i=2;i<=n;i++)inv[i]=inv[mod%i]*(mod-mod/i)%mod;
fac[0]=1;for (ri i=1;i<=n;i++) fac[i]=fac[i-1]*i%mod;
ifc[0]=1;for (ri i=1;i<=n;i++) ifc[i]=ifc[i-1]*inv[i]%mod;
}
int C(int n,int m){if (m>n || m<0) return 0;return fac[n]*ifc[m]%mod*ifc[n-m]%mod;}
int n,m,ans,tot,tmp,K,a,b,c;
int cnt1[N],cnt2[N],cnt3[N];
signed main()
{
rd(n);rd(K);
//a+b+c=i
//a=j
//b+c=i-j
//2->1 1
//1 1 1
//1 1 2 1 2 1 1 2 2
//
for (int i=1;i<=n;i++) cnt1[i]=1;
tot=0;
for (int i=1;i<=2*n;i++)
{
cnt2[i]+=tot;
if (i<=n) tot+=cnt1[i];
else tot+=cnt1[i],tot-=cnt1[i-n];
}
for (int i=1;i<=3*n;i++)
{
cnt3[i]+=tot;
if (i<=n) tot+=cnt2[i];
else tot+=cnt2[i],tot-=cnt2[i-n];
}
//for (int i=1;i<=3*n;i++) printf("cnt[%lld]=%lld\n",i,cnt3[i]);
for (int i=3;i<=3*n;i++)
{
tmp=cnt3[i];
//n 以内选 3 个数之和为 x 的方案数
//
tot+=tmp;
if (tot>=K)
{
tot-=tmp;
for (int j=1;j<=i-2;j++)
{
tmp=cnt2[i-j];
tot+=tmp;
if (tot>=K)
{
a=j;
tot-=tmp;
for (int k=1;k<=i-j;k++)
{
b=k;
c=i-a-b;
if (c<=n) tot++;
if (tot==K){ cout<<a<<" "<<b<<" "<<c<<endl;return 0;}
}
}
}
}
}
}
|
#include<bits/stdc++.h>
#define pi 3.141592653589793238
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define MOD 1000000007
#define INF 999999999999999999
#define pb push_back
#define ff first
#define ss second
#define mt make_tuple
#define ll long long
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main() {
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
fast;
ll T = 1, i, j;
cin >> T;
while (T--) {
ll n;
cin >> n;
ll cnt =0 ;
while(n % 2 == 0){
n /= 2;
cnt++;
}
if(cnt == 1){
cout << "Same\n";
}
else if(cnt == 0){
cout << "Odd\n";
}
else{
cout << "Even\n";
}
}
return 0;
} | #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 4000000000000000000 //オーバーフローに注意
#define MOD 1000000007
//#define MOD 998244353
#define EPS 0.000000001
using namespace std;
ll extgcd(ll a,ll b,ll &x,ll &y){
ll d = a;
if(b != 0){
d = extgcd(b,a%b,y,x);
y -= (a/b)*x;
}else{
x = 1;
y = 0;
}
return d;
}
//中国剰余定理(vec_val: 合同式の右辺集合 vec_m:合同式のmodの集合)
//https://qiita.com/drken/items/ae02240cd1f8edfc86fd
pair<ll,ll> ChineseRem(const vector<ll>&vec_val,const vector<ll>&vec_m){
if(vec_val.empty()||vec_m.empty())return make_pair(0,1);
ll VAL = vec_val[0];
ll M = vec_m[0];
ll x,y,val,m;
for (int i = 1; i < (int)vec_val.size(); i++) {
val = vec_val[i], m = vec_m[i];
if (M < m){
swap(M, m);
swap(VAL,val);
}
//Mx + my = gcd(M,m)(→d)
ll d = extgcd(M,m,x,y); //d: Mとmのgcd
/*
* 解の存在条件
* VAL≡val(mod d[=gcd(M,m)])より、
* val-VAL≡0 (mod d)
*
*
* */
if ((val-VAL)%d != 0){ //解の存在条件を満たさないので不適
return make_pair(0, -1);
}
ll md = m/d; //d = gcd(M,m)なので、mはdで割れる
ll tmp = ((val- VAL)/d) % md * x % md;
VAL += M*tmp;
M *= md; //法→lcm(m1,m2,m3...)[前の法Mと今回の法mをかけてからgcd(M,m)で割るのと同じ→lcm演算と同じ]
}
VAL %= M;
if (VAL < 0){
VAL += M;
}
return make_pair(VAL, M);
}
void func(){
ll X,Y,P,Q;
scanf("%lld %lld %lld %lld",&X,&Y,&P,&Q);
ll ans = HUGE_NUM;
for(ll a = X; a < X+Y; a++){
for(ll b = P; b < P+Q; b++){
pair<ll,ll> tmp_p = ChineseRem({a,b},{2*(X+Y),(P+Q)});
if(tmp_p.second == -1)continue;
ans = min(ans,tmp_p.first);
}
}
if(ans == HUGE_NUM){
printf("infinity\n");
}else{
printf("%lld\n",ans);
}
}
int main(){
int num_case;
scanf("%d",&num_case);
for(int i = 0; i < num_case; i++){
func();
}
return 0;
}
|
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
using namespace std;
int maxof(int x, int y)
{
if (x > y)
return x;
else
return y;
}
int main()
{
int n;
int a[1000], b[1000];
int ans;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i] >> b[i];
int a_id[1000], b_id[1000];
int a_min = a[0];
int b_min = b[0];
int counta = 0, countb = 0;
for (int i = 1; i < n; i++)
{
if (a_min > a[i])
a_min = a[i];
if (b_min > b[i])
b_min = b[i];
}
for (int i = 0; i < n; i++)
{
if (a[i] == a_min)
{
a_id[counta] = i;
counta++;
}
if (b[i] == b_min)
{
b_id[countb] = i;
countb++;
}
}
sort(a, a + n);
sort(b, b + n);
int a_min2 = a[1];
int b_min2 = b[1];
int w = 0;
for (int i = 0; i < counta; i++)
for (int j = 0; j < countb; j++)
{
if (a_id[i] == b_id[j])
w = 1;
}
if (w != 1)
{
if (a_min > b_min)
ans = a_min;
else
ans = b_min;
}
else
{
if (a_min + b_min < a_min2 && a_min + b_min < b_min2)
{
ans = a_min + b_min;
}
else
{
if (maxof(a_min, b_min2) < maxof(a_min2, b_min))
ans = maxof(a_min, b_min2);
else
ans = maxof(a_min2, b_min);
}
}
cout << ans << endl;
return 0;
} | #include<map>
#include<set>
#include<list>
#include<ctime>
#include<cmath>
#include<deque>
#include<queue>
#include<stack>
#include<string>
#include<vector>
#include<bitset>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<complex>
#include<iostream>
#include<algorithm>
#define rep(i,s,t) for(int i=s;i<=t;++i)
#define _rep(i,s,t) for(int i=s;i>=t;--i)
#define Rep(i,s,t) for(int i=s;i<t;++i)
#define go(x) for(int e=las[x];e;e=nxt[e])
#define open(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout)
#define _open(x) freopen(#x".in","w",stdout)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define is insert
#define es erase
#define pii pair<int,int>
#define Rd rand()<<15|rand()
#define ms(f,x) memset(f,x,sizeof f)
#define mc(f,x) memcpy(f,x,sizeof f)
#define gi(x) read(x)
#define gii(x,y) read(x),read(y)
#define giii(x,y,z) read(x),read(y),read(z)
namespace IO{
#define gc getchar()
#define pc(x) putchar(x)
template<typename T>inline void read(T &x){
x=0;int f=1;char ch=gc;while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=gc;}
while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=gc;x*=f;return;
}
template<typename T>inline void write(T x=0){
T wr[51];wr[0]=0;if(x<0)pc('-'),x=-x;if(!x)pc(48);
while(x)wr[++wr[0]]=x%10,x/=10;while(wr[0])pc(48+wr[wr[0]--]);return;
}
}
using IO::read;
using IO::write;
using namespace std;
const int N=1e6+11;
int a[N],b[N];
int A,B;
int n;
int main(){
gi(n);
rep(i,1,n)
gi(a[i]);
rep(i,1,n)
gi(b[i]);
long long ans=0;
rep(i,1,n){
A=max(A,a[i]);
B=max(B,b[i]);
ans=max(ans,1ll*b[i]*A);
printf("%lld\n",ans);
}
return 0;
}
|
#include <bits/stdc++.h>
#define debug(x) cerr << #x << " = " << x << endl
#define int long long
using namespace std;
typedef long long LL;
const int MAXN = 2E5 + 5;
const int MOD = 1E9 + 7;
int n, m, a[MAXN];
vector<int> delta;
template <class T>
void read(T& x) {
x = 0;
T f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') f = (ch == '-' ? -1 : 1), ch = getchar();
while (ch >= '0' && ch <= '9') x = x * 10 + ch - 48, ch = getchar();
x *= f;
}
template <class T, class... Args>
void read(T& x, Args&... args) {
read(x), read(args...);
}
template <class T>
void write(T x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
signed main() {
// freopen("1.in", "r", stdin);
read(n, m);
for (int i = 1; i <= m; i++) read(a[i]);
sort(a + 1, a + m + 1);
if (!m) {
cout << 1;
return 0;
}
a[0] = 0, a[m + 1] = n + 1;
int ans = 0;
for (int i = 1; i <= m + 1; i++)
if (a[i] - a[i - 1] - 1 > 0) delta.push_back(a[i] - a[i - 1] - 1);
// for (auto& d : delta) cout << d << endl;
if (delta.size()) {
int cur = 0;
int mi = delta[0];
for (int i = 1; i < delta.size(); i++) mi = min(mi, delta[i]);
for (auto& now : delta) cur += ceil(1.0 * now / mi);
ans = cur;
}
cout << ans;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define PI 3.14159265358979323846264338327950L
string randStr(){
string ret;
int rnd=rand()%9;
if(rnd==8){
ret='.';
}else{
ret = rnd+'A';
}
return ret;
}
int main(void){
int N,M;
cin>>N>>M;
vector<string> S(M);
vector<string> ans(N);
for(int i=0;i<M;i++){
cin>>S[i];
}
int index=0;
for(int i=0;i<N;i++){
// if(ans[index].size()<N){
// ans[index].append(S[i]);
// }else{
// index++;
// }
ans[i%N].append(S[i]);
}
for(int i=0;i<N;i++){
if(ans[i].size()>N){
ans[i] = ans[i].substr(0,N);
}else{
int tmp = N-ans[i].size();
for(int j=0;j<tmp;j++){
ans[i].append(randStr() );
}
}
}
for(int i=0;i<N;i++){
cout<<ans[i]<<endl;
}
return 0;
}
|
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#define N 111
#define NN 131000
template <typename T> inline void read(T &x) {
x = 0; char c = getchar(); bool flag = false;
while (!isdigit(c)) { if (c == '-') flag = true; c = getchar(); }
while (isdigit(c)) { x = (x << 1) + (x << 3) + (c ^ 48); c = getchar(); }
if (flag) x = -x;
}
using namespace std;
int n, K, P;
int ans[N], lmt[N];
int f[2][NN], g[2][NN], sum[NN];
inline void Try(int mid) {
memset(f, 0, sizeof(f));
memset(g, 0, sizeof(g));
memset(sum, 0, sizeof(sum));
int up = min(lmt[mid - 1], lmt[n - mid]);
int nw = 0; f[nw][0] = 1;
for (int i = 1; i < mid; ++i) {
nw ^= 1;
int d = mid - i;
for (int j = 0; j <= up; ++j) {
if (j - d >= 0) sum[j] = (sum[j - d] + f[nw ^ 1][j]) % P;
else sum[j] = f[nw ^ 1][j];
}
for (int j = 0; j <= up; ++j) {
if (j - (K+1) * d < 0) f[nw][j] = sum[j];
else f[nw][j] = (sum[j] - sum[j - (K+1) * d] + P) % P;
}
}
if (nw) memcpy(f[0], f[1], sizeof(f[1]));
nw = 0; g[nw][0] = 1;
for (int i = mid + 1; i <= n; ++i) {
nw ^= 1;
int d = i - mid;
for (int j = 0; j <= up; ++j) {
if (j - d >= 0) sum[j] = (sum[j - d] + g[nw ^ 1][j]) % P;
else sum[j] = g[nw ^ 1][j];
}
for (int j = 0; j <= up; ++j) {
if (j - (K+1) * d < 0) g[nw][j] = sum[j];
else g[nw][j] = (sum[j] - sum[j - (K+1) * d] + P) % P;
}
}
if (nw) memcpy(g[0], g[1], sizeof(g[1]));
for (int j = 0; j <= up; ++j) if (f[0][j] && g[0][j]) {
ans[mid] = (ans[mid] + 1ll * f[0][j] * g[0][j] % P * (K + 1)) % P;
}
}
int main() {
read(n), read(K), read(P);
for (int i = 1; i <= n; ++i) lmt[i] = lmt[i - 1] + K * i;
for (int i = 1; i <= (n + 1) >> 1; ++i) Try(i);
for (int i = 1; i <= n; ++i) if (!ans[i]) ans[i] = ans[n - i + 1];
for (int i= 1; i <= n; ++i) {
printf("%d\n", ((ans[i] - 1 + P) % P + P) % P);
}
return 0;
}
/*
3 1 998244353
//1 3 1
10 8 861271909
100 100 7
*/ | #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
using namespace std;
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr);
#else
#define eprintf(...) 42
#endif
using ll = long long;
using ld = long double;
using uint = unsigned int;
using ull = unsigned long long;
template<typename T>
using pair2 = pair<T, T>;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
int MOD;
int add(int x, int y) {
x += y;
if (x >= MOD) return x - MOD;
return x;
}
int sub(int x, int y) {
x -= y;
if (x < 0) return x + MOD;
return x;
}
ll mult(ll x, ll y) {
return (x * y) % MOD;
}
ll bin_pow(ll x, ll p) {
if (p == 0) return 1;
if (p & 1) return mult(x, bin_pow(x, p - 1));
return bin_pow(mult(x, x), p / 2);
}
ll rev(ll x) {
return bin_pow(x, MOD - 2);
}
const int N = 101;
const int M = 550000;
int dp[N][M];
int n, k;
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
scanf("%d%d%d", &n, &k, &MOD);
dp[0][0] = 1;
for (int x = 1; x < n; x++) {
for (int r = 0; r < x; r++) {
int sum = 0;
for (int y = r; y < M; y += x) {
sum = add(sum, dp[x - 1][y]);
if (y >= (k + 1) * x)
sum = sub(sum, dp[x - 1][y - (k + 1) * x]);
dp[x][y] = sum;
}
}
}
for (int x = 1; x <= n; x++) {
int ans = 0;
for (int y = 0; y < M; y++) {
ans = add(ans, mult(dp[x - 1][y], dp[n - x][y]));
}
ans = mult(ans, k + 1);
ans = sub(ans, 1);
printf("%d\n", ans);
}
return 0;
}
|
#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#define Endl endl
#define rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(),(x).end()
#define inf 1e18
using namespace std;
typedef long long llint;
typedef pair<llint, llint> P;
llint n;
vector<int> G[105];
bool used[105];
llint dfs(int v)
{
used[v] = true;
llint ret = 1;
for(auto u : G[v]){
if(!used[u]) ret += dfs(u);
}
return ret;
}
int main(void)
{
//ios::sync_with_stdio(0);
//cin.tie(0);
cin >> n;
char c;
rep(i, 1, n){
rep(j, 1, n){
cin >> c;
if(c%2) G[j].push_back(i);
}
}
double ans = 0;
rep(i, 1, n){
rep(j, 1, n) used[j] = false;
ans += 1.0 / dfs(i);
}
printf("%.11f\n", ans);
return 0;
} | #include <algorithm>
#include <cassert>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <vector>
#define TRACE(x) std::cout << #x << " = " << x << "\n"
#define _ << " _ " <<
using namespace std;
int main() {
std::ios_base::sync_with_stdio(false);
int N;
cin >> N;
vector<vector<int>> r(N, vector<int>(N, 0));
for (int i = 0; i < N; ++i) {
string A;
cin >> A;
for (int j = 0; j < N; ++j)
if (A[j] == '1') r[i][j] = 1;
r[i][i] = 1;
}
for (int k = 0; k < N; ++k) {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
r[i][j] |= r[i][k] & r[k][j];
}
}
}
double ret = 0;
for (int i = 0; i < N; ++i) {
int cnt = 0;
for (int j = 0; j < N; ++j) cnt += r[j][i];
ret += 1.0 / cnt;
}
cout << std::setprecision(15) << ret << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename T>
int len(const T& a) {
return a.size();
}
using ll = long long;
int main() {
ios_base::sync_with_stdio(false), cin.tie(nullptr);
int n, k;
cin >> n >> k;
long long ans = 0;
for (int i = -400000; i <= 400000; ++i) {
if (abs(k - i) > n - 1 || abs(i) > n - 1)
continue;
ans += 1ll * abs(n - abs(i)) * abs(n - abs(k - i));
}
cout << ans;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define cs const
#define fr first
#define se second
#define ls (now<<1)
#define rs (now<<1|1)
#define mid ((l+r)>>1)
#define mp make_pair
#define pb push_back
#define ppb pop_back
#define low(i) (i&(-i))
#define par pair<int,int>
#define cn(x) memset(x, 0, sizeof(x))
#define rep(i, x, y) for(int i=x; i<=y; ++i)
#define sep(i, x, y) for(int i=x; i>=y; --i)
#define fore(i, x) for(int i=fir[x]; i; i=nex[i])
cs int G = 3;
cs int ff = 1e6 + 15;
cs int inf = 1e18 + 1;
cs int base = 4;
cs int M = 998244353;
void Add(int &x, int y) { x = (x + y) % M; if(x < 0) x += M; }
int ksm(int x, int y) { return (y % 2 == 1 ? x : 1) * (y ? ksm(x * x % M, y >> 1) : 1) % M; }
int n, k;
int calc(int x)
{
if(x <= 0 || x > n + n) return 0;
if(x <= n) return x - 1;
return n + n + 1 - x;
}
void init()
{
cin >> n >> k; int Ans = 0;
rep(i, 1, n + n + n) Ans = Ans + calc(i) * calc(i - k);
cout << Ans;
}
signed main()
{
// freopen("triad.in", "r", stdin);
// freopen("triad.out", "w", stdout);
int Ts = 1;
while(Ts--)
init();
}
/*
*/ |
#include<iostream>
#include<cstdio>
using namespace std;
const int N=200005;
int n,m,Q;
int a[N],b[N];
struct Segment_Tree
{
struct Node
{
int ls,rs;
int cnt;
long long sum;
}tree[N*20];
int rt,tot;
void modifycnt(int &i,int l,int r,int pos,int val)
{
if(!i) i=++tot;
if(l==r)
{
tree[i].cnt+=val;
return;
}
int mid=(l+r)/2;
if(pos<=mid) modifycnt(tree[i].ls,l,mid,pos,val);
else modifycnt(tree[i].rs,mid+1,r,pos,val);
tree[i].cnt=tree[tree[i].ls].cnt+tree[tree[i].rs].cnt;
return;
}
void modifysum(int &i,int l,int r,int pos,int val)
{
if(!i) i=++tot;
if(l==r)
{
tree[i].sum+=val;
return;
}
int mid=(l+r)/2;
if(pos<=mid) modifysum(tree[i].ls,l,mid,pos,val);
else modifysum(tree[i].rs,mid+1,r,pos,val);
tree[i].sum=tree[tree[i].ls].sum+tree[tree[i].rs].sum;
return;
}
int querycnt(int i,int l,int r,int L,int R)
{
if(!i) return 0;
if(L<=l&&r<=R) return tree[i].cnt;
int mid=(l+r)/2;
int res=0;
if(L<=r) res+=querycnt(tree[i].ls,l,mid,L,R);
if(R>=l) res+=querycnt(tree[i].rs,mid+1,r,L,R);
return res;
}
long long querysum(int i,int l,int r,int L,int R)
{
if(!i) return 0;
if(L<=l&&r<=R) return tree[i].sum;
int mid=(l+r)/2;
long long res=0;
if(L<=r) res+=querysum(tree[i].ls,l,mid,L,R);
if(R>=l) res+=querysum(tree[i].rs,mid+1,r,L,R);
return res;
}
}TA,TB;
int main()
{
scanf("%d%d%d",&n,&m,&Q);
TA.modifycnt(TA.rt,1,1e8,0,n);
TB.modifycnt(TB.rt,1,1e8,0,m);
long long ans=0;
while(Q--)
{
int t,x,y;
scanf("%d%d%d",&t,&x,&y);
if(t==1)
{
TA.modifycnt(TA.rt,1,1e8,a[x],-1);
TA.modifysum(TA.rt,1,1e8,a[x],-a[x]);
TA.modifycnt(TA.rt,1,1e8,y,1);
TA.modifysum(TA.rt,1,1e8,y,y);
ans-=1LL*TB.querycnt(TB.rt,1,1e8,1,a[x])*a[x]+TB.querysum(TB.rt,1,1e8,a[x]+1,1e8);
ans+=1LL*TB.querycnt(TB.rt,1,1e8,1,y)*y+TB.querysum(TB.rt,1,1e8,y+1,1e8);
a[x]=y;
}
else if(t==2)
{
TB.modifycnt(TB.rt,1,1e8,b[x],-1);
TB.modifysum(TB.rt,1,1e8,b[x],-b[x]);
TB.modifycnt(TB.rt,1,1e8,y,1);
TB.modifysum(TB.rt,1,1e8,y,y);
// cerr<<"del"<<ans<<" "<<1LL*TA.querycnt(TA.rt,1,1e8,1,b[x])*b[x]-TA.querysum(TA.rt,1,1e8,b[x]+1,1e8)<<" "<<1LL*TA.querycnt(TA.rt,1,1e8,1,y)*y-TA.querysum(TA.rt,1,1e8,y+1,1e8)<<"\n";
ans-=1LL*TA.querycnt(TA.rt,1,1e8,1,b[x])*b[x]+TA.querysum(TA.rt,1,1e8,b[x]+1,1e8);
ans+=1LL*TA.querycnt(TA.rt,1,1e8,1,y)*y+TA.querysum(TA.rt,1,1e8,y+1,1e8);
b[x]=y;
}
printf("%lld\n",ans);
}
return 0;
}
| #include <iostream>
#include <vector>
#include <set>
#include <map>
#include <tuple>
template <class E> struct FenwickTree {
int _n;
std::vector<E> data;
FenwickTree(int n) : _n(n), data(n) { }
void add(int p, E x) {
++p;
while (p <= _n) {
data[p - 1] += x;
p += p & -p;
}
}
E sum(int r) const {
E s = 0;
while (r > 0) {
s += data[r - 1];
r -= r & -r;
}
return s;
}
E sum(int l, int r) const {
return sum(r) - sum(l);
}
};
long long proc(
std::vector<int> &cur,
int x,
int y,
FenwickTree<int> &ftcnt0,
FenwickTree<long long> &ftsum0,
const FenwickTree<int> &ftcnt1,
const FenwickTree<long long> &ftsum1,
const std::vector<int> &arr)
{
int v = cur[x];
long long dem = (long long)ftcnt1.sum(0, v) * arr[v];
long long cre = (long long)ftcnt1.sum(0, y) * arr[y];
long long adj = (long long)ftsum1.sum(y, v);
ftsum0.add(v, -arr[v]);
ftcnt0.add(v, -1);
cur[x] = y;
ftsum0.add(y, arr[y]);
ftcnt0.add(y, 1);
return cre + adj - dem;
}
int main(int argc, char **argv)
{
int n, m, q;
std::cin >> n >> m >> q;
std::vector<std::tuple<int, int, int>> txy;
std::set<int> s = { 0 };
for (int i = 0; i < q; i++) {
int t, x, y;
std::cin >> t >> x >> y;
txy.push_back(std::make_tuple(t, x - 1, y));
s.insert(y);
}
std::vector<int> arr(s.begin(), s.end());
std::map<int, int> map;
int ll = s.size();
for (int i = 0; i < ll; i++) map.emplace(arr[i], i);
FenwickTree<long long> ftsuma(ll);
FenwickTree<long long> ftsumb(ll);
FenwickTree<int> ftcnta(ll);
FenwickTree<int> ftcntb(ll);
std::vector<int> cura(n);
std::vector<int> curb(m);
ftcnta.add(0, n);
ftcntb.add(0, m);
long long sum = 0;
for (const auto&e : txy) {
int cy = map[std::get<2>(e)];
sum += (std::get<0>(e) == 1) ? proc(cura, std::get<1>(e), cy, ftcnta, ftsuma, ftcntb, ftsumb, arr)
: proc(curb, std::get<1>(e), cy, ftcntb, ftsumb, ftcnta, ftsuma, arr);
std::cout << sum << std::endl;
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long X;
cin>>X;
if(X/10000000000000>=1){
if(X/10000000000000>10){
cout<<10000000000000/10000001<<endl;
}
else{
cout<<X/10000001<<endl;
}
cout<<X/10000001<<endl;
}//33333333333333
//1000000000000
else if(X/100000000000>=1){
if(X/100000000000>10){
cout<<100000000000/1000001<<endl;
}
else{
cout<<X/1000001<<endl;
}
}//333333333333
else if(X/1000000000>=1){
if(X/1000000000>10){
cout<<10000000000/100001<<endl;
}
else{
cout<<X/100001<<endl;
}
}//3333333333
else if(X/10000000>=1){
if(X/10000000>10){
cout<<100000000/10001<<endl;
}
else{
cout<<X/10001<<endl;
}
}//33333333
else if(X/100000>1){
if(X/100000>10){
cout<<1000000/1001<<endl;
}
else{
cout<<X/1001<<endl;
}
}//333333
else if(X/1000>=1){
if(X/1000>10){
cout<<10000/101<<endl;
}
else{
cout<<X/101<<endl;
}
}//3333
else if(X/10>=1){
if(X/10>10){
cout<<100/11<<endl;
}
else{
cout<<X/11<<endl;
}
}//33
else{
cout<<0<<endl;
}
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
using ll = long long;
using P = pair<int,int>;
ll func(ll i) {
ll refact;
string i_st = to_string(i);
i_st += i_st;
refact = stoll(i_st);
return refact;
}
int main() {
ll N;
cin >> N;
int i = 1;
int ans = 0;
while(func(i) <= N) {
ans++;
i++;
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll n, f[1005];
int flg[1005];
vector<int> vec;
#define pb push_back
template < typename T >
inline T read()
{
T x = 0, w = 1; char c = getchar();
while(c < '0' || c > '9') { if(c == '-') w = -1; c = getchar(); }
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * w;
}
int main()
{
#ifndef ONLINE_JUDGE
//freopen("cpp.in", "r", stdin);
#endif
n = read <ll> ();
f[0] = 0, f[1] = 1;
for(int i = 2; i <= 88; i++) f[i] = f[i - 1] + f[i - 2];
ll tmp = n; int pos = 0;
for(int i = 87; i >= 1; i--) if(tmp >= f[i]) flg[i] = 1, tmp -= f[i];
for(int i = 1; i <= 87; i++) if(flg[i]) pos = i;
for(int i = pos, j = 1; i >= 1; j++, i--)
{
if(flg[i])
{
if(i & 1) vec.pb(1);
else vec.pb(2);
}
if(j & 1) vec.pb(4);
else vec.pb(3);
}
printf("%lu\n", vec.size());
for(auto i : vec) printf("%d\n", i);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
const int MAXN = 100005;
const ll MOD = 1e9+7;
ll fib[MAXN];
int cnt[MAXN];
inline void solve()
{
ll n; cin >> n;
fib[1] = 1;
int tk = 0;
for (int i=2;;i++)
{
fib[i] = fib[i-1] + fib[i-2];
if (fib[i] > n)
{
tk = i - 1;
break;
}
}
// cout << tk << ": " << fib[tk] << "\n";
int len = tk;
ll sum = n - fib[tk];
while (sum)
{
int id = upper_bound(fib+1,fib+tk+1,sum) - fib - 1;
int t = tk - id + 1;
cnt[t]++;
len++;
sum -= fib[id];
}
cout << len << "\n";
int l = 3,r = 4;
if (tk%2==0)
swap(l,r);
for (int i=1;i<=tk;i++)
{
if (i&1)
{
if (i == 1)
{
if (tk&1)
cout << 1 << "\n";
else
cout << 2 << "\n";
}
else
{
cout << l << "\n";
for (int j=1;j<=cnt[i];j++)
cout << ( (tk&1) ? 1 : 2 ) << "\n";
}
}
else
{
cout << r << "\n";
for (int j=1;j<=cnt[i];j++)
cout << ( (tk&1) ? 2 : 1 ) << "\n";
}
}
}
int main()
{
solve();
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {
return '"' + s + '"';
}
string to_string(const char* s) {
return to_string((string) s);
}
string to_string(bool b) {
return (b ? "true" : "false");
}
string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}
template <size_t N>
string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++) {
res += static_cast<char>('0' + v[i]);
}
return res;
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
#define fi first
#define se second
#define pb push_back
#define mod(n,k) ( ( ((n) % (k)) + (k) ) % (k))
#define forn(i,a,b) for(int i = a; i < b; i++)
#define forr(i,a,b) for(int i = a; i >= b; i--)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
const int mod = 3;
int sum(int a, int b){return (a+b) % mod;}
int sub(int a, int b){return (a + mod - b) % mod;}
int mul(int a, int b){return (1ll * a * b) % mod;}
int power(int a,int b){
int res = 1;
while(b){
if(b&1)res = mul(res,a);
b >>= 1;
a = mul(a,a);
}
return res;
}
const int maxn = 400010;
int fac[maxn],caf[maxn],pot[maxn];
int deg(int x,int d){
d %= mod - 1;
if(d < 0)d += mod - 1;
int y = 1;
while(d){
if (d & 1)y = mul(y,x);
x = mul(x,x);
d /= 2;
}
return y;
}
int coeff(int n,int k){
return mul(fac[n],mul(caf[k],caf[n-k]));
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
fac[0] = caf[0] = 1;
forn(i,1,maxn){
int act = i;
int add = 0;
while(act%3 == 0){
act /= 3;
add++;
}
fac[i] = mul(act,fac[i-1]);
pot[i] = add+pot[i-1];
caf[i] = deg(fac[i],-1);
}
//forn(i,1,10){
//debug(i,fac[i],caf[i],pot[i]);
//}
int N; cin >> N;
string str; cin >> str;
map<char,int> val = {{'B',0},{'W',1},{'R',2}};
map<int,char> rev = {{0,'B'},{1,'W'},{2,'R'}};
int act = 0;
int sig = (N%2 == 0) ? -1 : 1;
forn(i,0,N){
if(i == 0 || i == N-1){
act = sum(act,mul(sig,val[str[i]]));
}else{
int p1 = pot[N-1];
int p2 = pot[i]+pot[N-1-i];
int p = p1-p2;
int add = mul(mul(coeff(N-1,i),val[str[i]]),sig);
if(p >= 0){
add = mul(add,power(3,p));
}else{
add = power(3,p);
}
act = sum(act,add);
//debug(N-1,i,coeff(N-1,i),add,p);
}
}
if(act < 0)act += mod;
cout << rev[act] << '\n';
return 0;
}
/*
__builtin_mul_overflow(x,y,&x)
-fsplit-stack
*/
| #pragma GCC optimize ("-O3")
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
#define forn(i,x,n) for(ll i=x;i<n;i++)
#define pb push_back
#define int long long
#define mp make_pair
#define debug(x) cout<<x<<"\n";
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define mod 1000000007
#define endl '\n'
#define N 1000000
int n,m,k,b,c,d,x,y,z,e,v,r,w,f;
string s;
int a[1000000];
void least_prime_factor(vector<int>&lpf,vector<int>&mobius)
{
for (int i = 2; i < 2000000; i++)
// If it is a prime number
if (!lpf[i])
for (int j = i; j < 2000000; j += i)
// For all multiples which are not
// visited yet.
if (!lpf[j])
lpf[j] = i;
}
// Function to find the value of Mobius function
// for all the numbers from 1 to n
void Mobius(vector<int>&lpf,vector<int>&mobius)
{
for (int i = 1; i < 2000000; i++) {
// If number is one
if (i == 1)
mobius[i] = 1;
else {
// If number has a squared prime factor
if (lpf[i / lpf[i]] == lpf[i])
mobius[i] = 0;
// Multiply -1 with the previous number
else
mobius[i] = -1 * mobius[i / lpf[i]];
}
}
}
// Function to find the number of pairs
// such that gcd equals to 1
int gcd_pairs(vector<int>&a, int n,vector<int>&lpf,vector<int>&mobius)
{
// To store maximum number
int maxi = 0;
// To store frequency of each number
vector<int>fre(2000000,0);
// Find frequency and maximum number
for (int i = 0; i < n; i++) {
fre[a[i]]++;
maxi = max(a[i], maxi);
}
least_prime_factor(lpf,mobius);
Mobius(lpf,mobius);
// To store number of pairs with gcd equals to 1
int ans = 0;
// Traverse through the all possible elements
for (int i = 1; i <= maxi; i++) {
if (!mobius[i])
continue;
int temp = 0;
for (int j = i; j <= maxi; j += i)
temp += fre[j];
ans += temp * (temp - 1) / 2 * mobius[i];
}
// Return the number of pairs
return ans;
}
// Driver code
int32_t main()
{
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//std::cout << fixed<< std::setprecision(3);
int t;
t=1;
//cin>>t;
while(t--)
{
cin>>x>>y;
vector<int>a(y-x+1);
vector<int>lpf(2000000,0);
vector<int>mobius(2000000,0);
int j=0;
for(int i=x;i<=y;i++)
{
a[j++]=i;
}
int n =y-x+1;
// Function call
int ah=n-1;
int ans=((ah)*(ah+1))/2;
int temp= gcd_pairs(a, n,lpf,mobius);
ans-=temp;
if(x==1)
x++;
for(int i=x;i<=y;i++)
{
int l=i;
j=2;
while((i*j)<=y)
{
j++;
ans--;
}
}
ans=ans*2;
cout<<ans<<endl;
}
} |
#include<bits/stdc++.h>
using namespace std;
int main(){
long long int n;
cin>>n;
long long x=sqrt(n+1);
long long total=(x*(x+1))/2;
x++;
while(total<=n+1){
total+=x;
x++;
}
total-=x;
x-=2;
cout<<1+(n-x)<<endl;
}
| #include <iostream>
using namespace std;
int main()
{
long long k;
cin>>k;
long long i,j;
long long b,c;
long long cnt=0;
for(i=1;i<=k;i++){
b=k/i;
if(b<i) break;
for(j=i;j<=k;j++){
c=k/(i*j);
if(c<j) break;
if(i==j){
cnt+=1;
cnt+=(c-j)*3;
}else{
cnt+=3;
cnt+=(c-j)*6;
}
}
if(c<j) continue;
}
cout<<cnt<<endl;
return 0;
} |
/*@author Vipen Loka*/
#include <bits/stdc++.h>
#define endl '\n'
#define ff first
#define ss second
#define ll long long
#define vi vector<int>
#define vll vector<ll>
#define vvi vector < vi >
#define pii pair<int,int>
#define pll pair<long long, long long>
#define mod 1000000007
#define inf 1000000000000000001;
#define deb(x) cout << #x << ':' << x << '\n';
using namespace std;
template<class T>void show(vector<T> &a) {cerr << "[ "; for (int ij = 0; ij < (int)a.size(); ij++) {cerr << a[ij] << " ";} cerr << "]\n";}
template<class T>void show(T a) {cerr << a << endl;}
template<typename... T>void show(T... args) {((cerr << args << ' '), ...); cerr << endl;}
template<class T>void read(vector<T> &a) {for (auto &x : a) {cin >> x;}}
template<class T> void read(T &a) {cin >> a;}
void solve() {
int i, j;
int x, y;
cin >> x >> y;
if (x > y)swap(x, y);
if (x == 0 && y == 1) {
cout << 2 << endl;
}
else if (x == 1 && y == 2) {
cout << 0 << endl;
}
else if (x == 0 && y == 2) {
cout << 1 << endl;
}
else {
cout << x << endl;
}
}
int32_t main(int32_t argc, char *argv[]) {
ios_base::sync_with_stdio(false);
cin.tie(0);
int T = 1;
// cin >> T;
while (T--) {
solve();
}
cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
} | #include<bits/stdc++.h>
using namespace std;
int n,m;
const int Maxn=2010;
const int INF=0x3f3f3f3f;
const int dx[4]={1,-1,0,0};
const int dy[4]={0,0,1,-1};
typedef pair<int,int> pii;
vector<int>from[Maxn*Maxn];
char s[Maxn][Maxn];
int dist[Maxn*Maxn];
int st,ed;
void dij(){
memset(dist,INF,sizeof(dist));
priority_queue<pii>q;
dist[st]=0;
q.push(make_pair(0,st));
while(!q.empty()){
int u=q.top().second;
int d=-q.top().first;
q.pop();
if(d>dist[u])continue;
for(int i=0;i<(int)from[u].size();i++){
int to=from[u][i];
int cost=d;
if(u<n*m)cost++;
if(dist[to]>cost){
dist[to]=cost;
q.push(make_pair(-cost,to));
}
}
}
}
int main(){
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++){
scanf("%s",s[i]);
for(int j=0;j<m;j++){
if(s[i][j]=='S')st=i*m+j;
if(s[i][j]=='G')ed=i*m+j;
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++)if(s[i][j]!='#'){
for(int d=0;d<4;d++){
int nx=i+dx[d];
int ny=j+dy[d];
if(nx>=0&&nx<n&&ny>=0&&ny<m&&s[nx][ny]!='#'){
from[i*m+j].push_back(nx*m+ny);
}
}
if(s[i][j]>='a'&&s[i][j]<='z'){
from[i*m+j].push_back(n*m+s[i][j]-'a');
from[n*m+s[i][j]-'a'].push_back(i*m+j);
}
}
}
dij();
if(dist[ed]==INF)printf("-1\n");
else printf("%d\n",dist[ed]);
return 0;
} |
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <iostream>
#include <cassert>
#include <cmath>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <cstdlib>
using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define li long long
#define pii pair<int, int>
#define vi vector<int>
#define li long long
#define forn(i, n) for (int i = 0; i < (int)n; i++)
#define fore(i, b, e) for (int i = (int)b; i <= (int)e; i++)
const int MAXN = 505;
int c[MAXN][MAXN];
int main() {
int n;
scanf("%d", &n);
forn(i, n) {
forn(j, n) {
scanf("%d", &c[i][j]);
}
}
if (n > 1) {
fore(i, 1, n - 1) {
int ai = c[i][0] - c[0][0];
forn(j, n) {
if (c[i][j] - c[0][j] != ai) {
//printf("i = %d j = %d ai = %d %d\n", i, j, ai, c[i][j] - c[0][j]);
printf("No");
exit(0);
}
}
}
}
int upper = c[0][0];
forn(j, n) {
upper = min(upper, c[0][j]);
}
int lower = 0;
forn(i, n) {
forn(j, n) {
lower = max(lower, c[0][j] - c[i][j]);
}
}
if (lower <= upper) {
int a0 = lower;
vi b(n);
forn(i, n) {
b[i] = c[0][i] - a0;
assert(b[i] >= 0);
}
printf("Yes\n");
forn(i, n) {
int val = c[i][0] - b[0];
assert(val >= 0);
printf("%d ", val);
}
printf("\n");
forn(i, n) {
printf("%d ", b[i]);
}
} else {
printf("No");
}
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n;
vector<ll> a, b;
bool solve()
{
cin >> n;
vector<vector<ll>> c(n, vector<ll>(n));
a.resize(n), b.resize(n);
ll sum = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
cin >> c[i][j];
sum += c[i][j];
}
if (sum % n != 0)
return false;
int mnidx = 0;
for (int i = 0; i < n; i++)
if (c[mnidx][0] > c[i][0])
mnidx = i;
for (int i = 0; i < n; i++)
for (int j = 0; j < n - 1; j++)
if (c[i][j + 1] - c[i][j] != c[mnidx][j + 1] - c[mnidx][j])
return false;
for (int i = 0; i < n; i++)
b[i] = c[mnidx][i];
for (int i = 0; i < n; i++)
a[i] = c[i][0] - c[mnidx][0];
return true;
}
int main()
{
if (solve() == false)
{
cout << "No" << endl;
}
else
{
cout << "Yes" << endl;
for (int i = 0; i < n; i++)
cout << a[i] << " ";
cout << endl;
for (int i = 0; i < n; i++)
cout << b[i] << " ";
cout << endl;
}
}
|
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <tuple>
#include <cstdio>
#include <bitset>
#include <sstream>
#include <iterator>
#include <numeric>
#include <map>
#include <cstring>
#include <set>
#include <functional>
#include <iomanip>
using namespace std;
#define DEBUG_ //!!$BDs=P;~$K%3%a%s%H%"%&%H(B!!
#ifdef DEBUG_
#define dump(x) cerr << #x << " = " << (x) << endl;
#else
#define dump(x) ;
#endif
#define equals(a,b) (fabs((a)-(b)) < EPS)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
//#define int long long
typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template <typename T>
std::string printVector(const std::vector<T> &data)
{
std::stringstream ss;
std::ostream_iterator<T> out_it(ss, ", ");
ss << "[";
std::copy(data.begin(), data.end() - 1, out_it);
ss << data.back() << "]";
return ss.str();
}
template <typename T>
void print_array(const T &ary, int size){
REP(i,size){
cout << ary[i] << " ";
}
cout << endl;
}
const int mod = 1e9+7;
const LL LINF = 1001002003004005006ll;
const int INF = 1001001001;
const double EPS = (1e-10);
const long double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899;
int dx[] = {0,0,-1,1};
int dy[] = {-1,1,0,0};
int arr[] = {0,1,2,3,4,5,6,7,8,9};
map<char,int> mp;
LL getInt(string s){
reverse(s.begin(), s.end());
LL res = 0;
LL base = 1;
for(char c : s){
res += base * arr[mp[c]];
base *= 10;
}
return res;
}
bool check(string s){
int idx = mp[s[0]];
int x = arr[idx];
if(x == 0) return false;
else return true;
}
signed main(int argc, char const *argv[])
{
cin.tie(0);
ios::sync_with_stdio(false);
cout << setprecision(12);
string A,B,C; cin >> A >> B >> C;
set<char> se;
for(char c : A) se.insert(c);
for(char c : B) se.insert(c);
for(char c : C) se.insert(c);
int len = 0;
for(char c : se) len++;
if(len > 10){
cout << "UNSOLVABLE" << endl;
return 0;
}
int idx = 0;
for(char c : se){
mp[c] = idx;
idx++;
}
do{
if(!check(A) || !check(B) || !check(C)) continue;
LL Ax = getInt(A);
LL Bx = getInt(B);
LL Cx = getInt(C);
if(Ax + Bx == Cx){
cout << Ax << endl;
cout << Bx << endl;
cout << Cx << endl;
return 0;
}
}while(next_permutation(arr, arr+10));
cout << "UNSOLVABLE" << endl;
} | #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <queue>
#include <bitset>
#include <stack>
#include <functional>
// AtCoder
// #include <atcoder/all>
// using namespace atcoder;
#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...)
#endif
#define rep_(i, a_, b_, a, b, ...) for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define reprev_(i, a_, b_, a, b, ...) for (int i = (b)-1, i##_min = (a); i >= i##_min; --i)
#define reprev(i, ...) reprev_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define all(x) (x).begin(), (x).end()
template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
#define fls(x) (64 - __builtin_clzll(x))
#define pcnt(x) __builtin_popcountll(x)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int,int> P;
typedef long double ld;
constexpr ll INF = 1e18;
int main (void)
{
cin.tie(0);
ios::sync_with_stdio(false);
ll n; cin >> n;
vector<vector<ll>> c(n, vector<ll>(n, 0));
rep (i, n) rep (j, n) cin >> c[i][j];
rep (i, n) rep (j, n) {
if (c[i][j] - c[i][0] != c[0][j] - c[0][0]) {
cout << "No\n";
return 0;
}
}
cout << "Yes\n"; // 忘れない!
vector<ll> a(n, INF), b(n, INF);
rep (i, n) rep (j, n) {
chmin(a[i], c[i][j]);
chmin(b[j], c[i][j]);
}
ll am = *min_element(all(a)), bm = *min_element(all(b));
transform(all(a), a.begin(), [&](ll x){return x - am;});
// transform(all(b), b.begin(), [&](ll x){return x - bm;});
rep (i, n) cout << a[i] << " \n"[i + 1 == n];
rep (i, n) cout << b[i] << " \n"[i + 1 == n];
return 0;
} |
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<iomanip>
#include<algorithm>
using namespace std;
using ll = long long;
using veci = vector<int>;
using vecd = vector<double>;
using vecl = vector<long long>;
bool check_slope(int x1,int y1,int x2,int y2){
int bigx,smallx,bigy,smally;
if(x1<x2){
bigx=x2;
smallx=x1;
bigy=y2;
smally=y1;
}
else{
bigx=x1;
smallx=x2;
bigy=y1;
smally=y2;
}
/*
double a=(double)(bigy-smally)/(bigx-smallx);
cout<<" "<<a;
*/
if(bigy-smally>bigx-smallx || bigy-smally<smallx-bigx) return false;
else return true;
}
void Main(){
int n,ans=0;
cin>>n;
veci x(n),y(n);
for(int i=0;i<n;i++) cin>>x[i]>>y[i];
for(int i=0;i<n;i++) for(int j=i+1;j<n;j++) {
//cout<<i<<" "<<j;
if(check_slope(x[i],y[i],x[j],y[j])){
ans++;
//cout<<" ok";
}
//cout<<endl;
}
cout<<ans;
return;
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
Main();
return 0;
} | #include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define FI first
#define SE second
#define pb push_back
#define eb emplace_back
#define mod 1000000007
#define all(c) (c).begin(),(c).end()
#define LB lower_bound
#define UB upper_bound
#define max3(a,b,c) max(c,max(a,b))
#define min3(a,b,c) min(c,min(a,b))
#define mems(s, n) memset(s, n, sizeof(s))
#define NINF -1e18
#define INF 1e18
#define int ll int
#define endl '\n'
#define double long double
#define OOK order_of_key //no of elements strictly less than
#define FBO find_by_order //iterator pointing kth element;indexing starts from 0
#define CK3(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl
#define CK4(a,b,c,d) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<endl
typedef pair<int,int> PII;
typedef pair<pair<int,int>,int> PPII;
typedef pair<int,pair<int,int>> PIPI;
typedef map<int,int>MII;
typedef vector<int> VI;
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
/*int power(int a, int b)
{int x=1,y=a;while(b>0){if(b%2){x =(x*y)%mod;}y =(y*y)%mod;b/=2;}return x%mod;}
*/
signed main()
{
ios::sync_with_stdio(false); cin.tie(0);
int T=1,T1=0;//cin>>T;
while(T1++<T)
{//cout<<"Case #"<<T1<<": ";
int n;
cin>>n;
int ans=INF;
for(int i=0;i<61;i++)
{
int x=1ll<<i;
if(x<=n)
{
int z=n%x;
int y=(n-z)/x;
ans=min(ans,i+y+z);
}
}
cout<<ans<<endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<char> vc;
typedef queue<ll> ql;
typedef deque<ll> dql;
typedef priority_queue<ll> pql;
typedef set<ll> sl;
typedef pair<ll, ll> pl;
typedef vector<vl> vvl;
typedef vector<pl> vpl;
#define rep(i, n) for(ll i = 0; i < ll(n); i++)
#define rep2(i, k, n) for(ll i = ll(k); i <= ll(n); i++)
#define rep3(i, n, k) for(ll i = ll(n); i >= ll(k); i--)
#define all(v) (v).begin(), (v).end()
ll mod(ll a, ll b) {if(b == 0) return 0; return (a % b + b) % b;}
bool chmin(ll &a, ll b) {if(b < a) {a = b; return 1;} return 0;}
bool chmax(ll &a, ll b) {if(b > a) {a = b; return 1;} return 0;}
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
//const ll MOD = 998244353;
const ll MAX = 2e5;
const ld eps = 1e-9;
const char newl = '\n';
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
ll n, x;
cin >> n >> x;
vl a(n);
rep3(i, n-1, 0) cin >> a[i];
map<ll, ll> dp[n+1];
dp[0][x] = 1;
rep(i, n) for(pl p: dp[i]) {
ll d = p.first - a[i]*(p.first/a[i]);
dp[i+1][d] += p.second;
if(i==0 || abs(p.first) + a[i] < a[i-1]) {
if(d>0) dp[i+1][d-a[i]] += p.second;
if(d<0) dp[i+1][d+a[i]] += p.second;
}
}
cout << dp[n][0] << newl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
long long x;
cin >> n >> x;
vector<long long> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<long long> b(n);
for (int i = n - 1; i >= 0; i--) {
b[i] = x / a[i];
x %= a[i];
}
vector<int> next(n - 1);
for (int i = 0; i < n - 1; i++) {
next[i] = n - 1;
for (int j = i + 1; j < n - 1; j++) {
if (b[j] != a[j + 1] / a[j] - 1) {
next[i] = j;
break;
}
}
}
vector<long long> dp(n - 1, 0);
for (int i = 0; i < n - 1; i++) {
if (b[i] > 0) {
dp[i] = 1;
for (int j = i - 2; j >= 0; j--) {
if (i > next[j]) {
dp[i] += dp[j];
}
}
}
if (b[i] != a[i + 1] / a[i] - 1 && i > 0) {
for (int j = 0; j < i; j++) {
if (next[j] == i) {
dp[i] += dp[j];
}
}
}
}
cout << accumulate(dp.begin(), dp.end(), 0ll) + 1 << '\n';
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef vector<long long> vll;
#define endl '\n'
#define pb push_back
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL);
void solve(){
double a,b,c,d; cin>>a>>b>>c>>d;
double ans = a/(c*d - b);
if(ans < 0 || d*c == b) cout<<-1;
else cout<<ceil(ans);
}
int main(){
fast
int t=1; //cin>>t;
while(t--){
solve();
cout<<endl;
}
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define sc(x) scanf("%lld",&(x));
#define fi first
#define se second
#define endl '\n'
int n;
int a,b,c,d;
int ans;
signed main(){
sc(a)sc(b)sc(c)sc(d)
if(b > d*c){
cout << -1 << '\n';
}else if(b == d*c){
if(a == 0){
cout << 1 <<'\n';
}else{
cout << -1 << '\n';
}
}else{
for(int i = 1; i <= 100000; i++){
if(a + i*(b)<= i*d*c){
cout << i << '\n';
break;
}
}
}
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ll N;
cin >> N;
ll ans = 0;
set<ll> s;
for (ll i = 2; i * i <= N; i++)
{
ll x = i * i;
while (x <= N)
{
s.insert(x);
x *= i;
}
}
cout << N - s.size() << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// 約数列挙
vector<long long> calc_divisor(long long n) {
vector<long long> res;
for (long long i = 1LL; i * i <= n; ++i) {
if (n % i == 0) {
res.push_back(i);
long long j = n / i;
if (j != i) res.push_back(j);
}
}
sort(res.begin(), res.end());
return res;
}
int main() {
ll n; cin >> n;
vector<long long> div = calc_divisor(n);
for (int i = 0; i < div.size(); i++) {
cout << div[i] << endl;
}
return 0;
} |
#include <cstdio>
const int md = 1000000007;
inline int add(int a, int b)
{
a += b;
if (a >= md) a -= md;
return a;
}
inline int sub(int a, int b)
{
a -= b;
if (a < 0) a += md;
return a;
}
inline int mul(int a, int b)
{
return (long long)a * b % md;
}
inline int po(int a, int b)
{
int r = 1;
while (b) {
if (b & 1) r = mul(r, a);
a = mul(a, a);
b >>= 1;
}
return r;
}
inline int inv(int a)
{
return po(a, md - 2);
}
const int N = 1000000;
int fact[2 * N + 1];
inline int nCr(int n, int r)
{
if (r < 0) return 0;
return mul(fact[n], inv(mul(fact[r], fact[n - r])));
}
int n, m, k;
int main()
{
fact[0] = 1;
for (int i = 1; i <= 2 * N; ++i) fact[i] = mul(fact[i - 1], i);
scanf("%d%d%d", &n, &m, &k);
if (n > m + k) printf("0\n");
else printf("%d\n", sub(nCr(n + m, n), nCr(n + m, n - k - 1)));
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
/* * * * * * * * * * */
#define mp make_pair
typedef long long ll;
typedef pair<int, int> pii;
/* * * * * * * * * * */
/* *
*
* Too many mind, no mind.
*
* */
const int mod = 3;
int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; }
int sub(int a, int b) { return a >= b ? a - b : a - b + mod; }
int mul(int a, int b) { return a * b % mod; }
int pasc[3][3];
int choose(int n, int k) {//Luca's Theorem
if (k < 0 || k > n) return 0;
int ans = 1;
while (n) {
int x = n % mod, y = k % mod;
n /= mod, k /= mod;
ans = mul(pasc[x][y], ans);
}
return ans;
}
int main(){
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
pasc[0][0] = 1;
for (int i = 1; i < 3; ++i) {
pasc[i][0] = pasc[i][i] = 1;
for (int j = 1; j < 3; ++j) pasc[i][j] = add(pasc[i - 1][j], pasc[i - 1][j - 1]);
}
int n; cin >> n;
string s; cin >> s;
int ans = 0;
for (int i = 0; i < n; ++i) {
int ai;
if (s[i] == 'B') ai = 0;
else if (s[i] == 'W') ai = 1;
else ai = 2;
ans = add(ans, mul(ai, choose(n - 1, i)));//Binomial coefficient
}
if (!(n & 1)) ans = (-ans + 6) % mod;
/*
for (int i = 0; i < n - 1; ++i) {
if (ans & 1) ans += 3;
ans /= 2;
}
*/
if (ans == 0) cout << 'B';
else if (ans == 1) cout << 'W';
else cout << 'R';
cout << '\n';
return 0;
}
|
#include "bits/stdc++.h"
#define pb push_back
using namespace std;
using ll = long long;
const ll N = 2e3 + 5;
vector<ll>g[N];
bool vis[N];
void dfs(ll v){
vis[v] = true;
for(ll u : g[v]){
if(!vis[u]){
dfs(u);
}
}
}
signed main(){
ll n,m;
cin >> n >> m;
for(ll i=0;i<m;i++){
ll x,y;
cin >> x >> y;
g[x].pb(y);
}
ll ans = 0;
for(ll i=1;i<=n;i++){
memset(vis,false,sizeof(vis));
dfs(i);
ll cnt = 0;
for(ll i=0;i<=n;i++){
if(vis[i]){
cnt++;
}
}
ans = ans + cnt;
}
cout << ans << "\n";
return 0;
} | #include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define file(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
inline int read()
{
int data = 0, w = 1; char ch = getchar();
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') w = -1, ch = getchar();
while (ch >= '0' && ch <= '9') data = data * 10 + (ch ^ 48), ch = getchar();
return data * w;
}
const int N(2e5 + 10), INF(1e9);
std::vector<int> G[N];
int n, K, f[N], g[N], R, cnt;
void dfs(int x, int fa)
{
f[x] = 0, g[x] = INF;
for (auto i : G[x]) if (i != fa)
dfs(i, x), f[x] = std::max(f[x], f[i] + 1), g[x] = std::min(g[x], g[i] + 1);
if (f[x] + g[x] <= R) f[x] = -INF;
if (f[x] == R) f[x] = -INF, g[x] = 0, ++cnt;
if (x == 1 && f[x] >= 0) ++cnt;
}
int main()
{
#ifndef ONLINE_JUDGE
file(cpp);
#endif
n = read(), K = read();
for (int i = 1, a, b; i < n; i++)
a = read(), b = read(), G[a].push_back(b), G[b].push_back(a);
int l = 1, r = n, ans = n;
while (l <= r)
{
R = (l + r) >> 1, cnt = 0, dfs(1, 0);
if (cnt <= K) ans = R, r = R - 1;
else l = R + 1;
}
printf("%d\n", ans);
return 0;
}
|
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <utility>
#include <vector>
#include <limits>
#include <cmath>
#include <queue>
#include <unordered_map>
typedef long long ll;
const auto llMax = std::numeric_limits<ll>::max();
typedef struct {
ll a; // from
ll b; // to
ll c; // time
} Route;
ll search(const int n, const std::vector<Route> r, int start, ll minTime)
{
std::vector<ll> t(n, llMax);
// make index
std::vector<ll> index(n + 1, llMax);
index[0] = 0;
index[n] = r.size();
for (ll i = 0; i < r.size(); i++) {
ll a = r[i].a;
index[a] = std::min(index[a], i);
}
for (ll i = n; i >= 0; i--) {
if (index[i] == llMax) {
index[i] = index[i + 1];
}
}
t[start] = 0;
std::queue<ll> pos;
pos.push(start);
while(!pos.empty()) {
ll now = pos.front();
pos.pop();
for (ll i = index[now]; i < index[now + 1]; i++) {
Route nowRoute = r[i];
const ll time = t[now] + nowRoute.c;
if (time > minTime) {continue;}
if (nowRoute.b == start) {
minTime = time;
continue;
}
if (time < t[nowRoute.b]) {
t[nowRoute.b] = time;
pos.push(nowRoute.b);
}
}
}
return minTime;
}
int main(int argc, char *argv[])
{
ll n, m;
std::vector<Route> r;
std::cin >> n >> m;
r.reserve(m);
std::vector<ll> loopTime(n, llMax);
for (int i = 0; i < m; i++) {
ll a, b, c;
std::cin >> a >> b >> c;
a--; b--;
if (a == b) {
loopTime[a] = std::min(loopTime[a], c);
} else {
r.push_back({a, b, c});
}
}
std::sort(r.begin(), r.end(), [](auto const& lhs, auto const& rhs){
return lhs.a < rhs.a;
});
for (int i = 0; i < n; i++) {
ll time = search(n, r, i, loopTime[i]);
ll ans = (time != llMax) ? time : -1;
std::cout << ans << std::endl;
}
return 0;
}
| #include <iostream>
#include <iomanip>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <cstdio>
#include <utility>
#include <string>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <numeric>
using namespace std;
typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;
const double PI=3.14159265358979323846;
#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))
#define rep(i, N) for(int i = 0; i < N; ++i)
#define CEIL(x, y) (((x) + (y) - 1) / (y))
#define MOD 1000000007ULL
#define IN(l, r, x) ((l) <= (x) && (x) < (r))
using P = pair<s64, s64>;
void dijkstra(const vector< vector< P > >& g, vs64& d, int s)
{
static const s64 INF = 1e18;
rep (i, d.size()) d[i] = INF;
d[s] = 0;
priority_queue< P, vector<P>, greater<P> > pq;
pq.push(P{d[s], s});
while (!pq.empty())
{
auto p = pq.top(); pq.pop();
int from = p.second;
s64 d1 = p.first;
if (d[from] < d1) continue;
for (auto e : g[from])
{
int to = e.first;
s64 d2 = e.second;
if (d[to] > d1 + d2)
{
d[to] = d1 + d2;
pq.push(P{d[to], to});
}
}
}
}
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
int n, m;
cin >> n >> m;
vector< vector< P > > g1(n);
vector< vector< P > > g2(n);
vs64 ans(n, (s64)1e18);
rep (i, m)
{
int u, v;
s64 c;
cin >> u >> v >> c;
--u, --v;
g1[u].push_back(P{v, c});
g2[v].push_back(P{u, c}); // reverse
if (u == v) ans[u] = min(ans[u], c);
}
rep (i, n)
{
vs64 d1(n), d2(n);
dijkstra(g1, d1, i);
dijkstra(g2, d2, i);
rep (j, n)
{
if (i == j) continue;
ans[i] = min(ans[i], d1[j] + d2[j]);
}
if (ans[i] == (s64)1e18) ans[i] = -1;
cout << ans[i] << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i=0; i<(n); ++i)
#define RREP(i, n) for(int i=(n);i>=0;--i)
#define FOR(i, a, n) for (int i=(a); i<(n); ++i)
#define RFOR(i, a, b) for(int i=(a);i>=(b);--i)
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()
#define DUMP(x) cerr<<#x<<" = "<<(x)<<endl
#define DEBUG(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<<endl;
template<class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
REP(i, SZ(v)) {
if (i) os << " ";
os << v[i];
}
return os;
}
template <class T>
void debug(const vector<T> &v) {
cout << "[";
REP(i, SZ(v)) {
if(i) cout << ", ";
cout << v[i];
}
cout << "]" << endl;
}
template<class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
return os << p.first << " " << p.second;
}
template <class T, class U>
void debug(const pair<T, U> &p) {
cout << "(" << p.first << " " << p.second << ")" << endl;
}
template<class T>
bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template<class T>
bool chmin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;
const ll MOD = 1e9 + 7;
const ll MOD998 = 998244353;
const int INF = INT_MAX;
const ll LINF = LLONG_MAX;
const int inf = INT_MIN;
const ll linf = LLONG_MIN;
const ld eps = 1e-9;
template<int m>
struct mint {
int x;
mint(ll x = 0) : x(((x % m) + m) % m) {}
mint operator-() const { return x ? m - x : 0; }
mint &operator+=(mint r) {
if ((x += r.x) >= m) x -= m;
return *this;
}
mint &operator-=(mint r) {
if ((x -= r.x) < 0) x += m;
return *this;
}
mint &operator*=(mint r) {
x = ((ll) x * r.x) % m;
return *this;
}
mint inv() const { return pow(m - 2); }
mint &operator/=(mint r) { return *this *= r.inv(); }
friend mint operator+(mint l, mint r) { return l += r; }
friend mint operator-(mint l, mint r) { return l -= r; }
friend mint operator*(mint l, mint r) { return l *= r; }
friend mint operator/(mint l, mint r) { return l /= r; }
mint pow(ll n) const {
mint ret = 1, tmp = *this;
while (n) {
if (n & 1) ret *= tmp;
tmp *= tmp, n >>= 1;
}
return ret;
}
friend bool operator==(mint l, mint r) { return l.x == r.x; }
friend bool operator!=(mint l, mint r) { return l.x != r.x; }
friend ostream &operator<<(ostream &os, mint a) {
return os << a.x;
}
friend istream &operator>>(istream &is, mint &a) {
ll x;
is >> x;
a = x;
return is;
}
};
using Int = mint<MOD>;
template<typename T>
struct BIT {
vector<T> bit;
int sz;
BIT(int n) : sz(n + 1), bit(n + 1) {}
void add(int i, T x) {
i += 1;
while (i < sz) {
bit[i] += x;
i += i & -i;
}
}
T sum(int i) {
i += 1;
T s = 0;
while (i > 0) {
s += bit[i];
i -= i & -i;
}
return s;
}
};
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
int h, w, m; cin >> h >> w >> m;
vi x(m), y(m);
vi mir(h, w), mic(w, h);
REP(i, m) {
cin >> x[i] >> y[i];
x[i]--;
y[i]--;
chmin(mir[x[i]], y[i]);
chmin(mic[y[i]], x[i]);
}
int mar = mic[0], mac = mir[0];
ll ans = 0;
REP(i, mar) {
ans += mir[i];
}
REP(i, mac) {
ans += mic[i];
}
vector<P> v;
REP(i, mar) {
v.emplace_back(mir[i], i);
}
sort(ALL(v));
BIT<int> bit(h+1);
REP(i, mar) {
bit.add(i, 1);
}
int pos = 0;
REP(i, mac) {
while(pos < mar && v[pos].first == i) {
bit.add(v[pos].second, -1);
pos++;
}
if(mic[i] != 0) ans -= bit.sum(mic[i]-1);
}
cout << ans << endl;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define LL long long int
#define MOD 1000000007
#define MAXN 200000
int ft[MAXN + 3];
void update(int idx, int val) {
while (idx <= MAXN) {
ft[idx] += val;
idx += (idx & -idx);
}
}
int query(int idx) {
int result = 0;
while (idx > 0) {
result += ft[idx];
idx -= (idx & -idx);
}
return result;
}
int H, W, N, firstBlockRight, firstBlockBelow;
vector<int> blockAtColumn[MAXN + 3];
bool seen[MAXN + 3];
LL answer;
int main() {
scanf("%d%d%d", &H, &W, &N);
firstBlockRight = W + 1;
firstBlockBelow = H + 1;
for (int i = 1; i <= N; i++) {
int row, col;
scanf("%d%d", &row, &col);
if (row == 1) {
firstBlockRight = min(firstBlockRight, col);
}
if (col == 1) {
firstBlockBelow = min(firstBlockBelow, row);
}
blockAtColumn[col].push_back(row);
}
memset(ft, 0, sizeof(ft));
memset(seen, false, sizeof(seen));
answer = 0;
for (int col = 1; col <= W; col++) {
sort(blockAtColumn[col].begin(), blockAtColumn[col].end());
for (auto row: blockAtColumn[col]) {
if (!seen[row]) {
seen[row] = true;
update(row, 1);
}
}
if (col < firstBlockRight) {
if (blockAtColumn[col].empty()) {
answer += H;
} else {
int r = blockAtColumn[col][0];
if (r < firstBlockBelow) {
int cntBlocked = query(firstBlockBelow - 1) - query(r - 1);
answer += (firstBlockBelow - 1 - cntBlocked);
} else {
answer += (r - 1);
}
}
} else {
int cntBlocked = query(firstBlockBelow - 1);
answer += (firstBlockBelow - 1 - cntBlocked);
}
}
printf("%lld\n", answer);
}
|
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define pii pair<int, int>
#define sz(x) (int)x.size()
#define fr first
#define sc second
#define all(a) a.begin(), a.end()
#define rep(i, a, b) for(int i = a; i < b; i++)
#define int long long
const int N = 1e5 + 20;
const int K = 18;
const int INF = 1e9;
const int MOD = 1e9 + 7;
vector <int> adj[N];
int dist[N][K], n, k, dp[1 << K][K], m, arr[K];
void bfs(int idx) {
rep(node, 1, n + 1) dist[node][idx] = INF;
queue <int> q;
q.push(arr[idx]);
dist[arr[idx]][idx] = 0;
while(!q.empty()) {
int node = q.front();
q.pop();
for(auto &child: adj[node]) {
if(dist[child][idx] == INF) {
dist[child][idx] = dist[node][idx] + 1;
q.push(child);
}
}
}
}
int calc(int last, int mask) {
if(mask == ((1 << k) - 1))
return 0;
if(dp[mask][last] != -1)
return dp[mask][last];
int ans = INF;
for(int i = 0; i < k; i++) {
if(mask & (1 << i)) continue;
ans = min(ans, calc(i, mask | (1 << i)) + dist[arr[last]][i]);
}
return dp[mask][last] = ans;
}
void solve() {
cin >> n >> m;
rep(i, 0, m) {
int x, y;
cin >> x >> y;
adj[x].pb(y);
adj[y].pb(x);
}
cin >> k;
rep(i, 0, k) cin >> arr[i];
rep(i, 0, k) bfs(i);
rep(i, 0, k) {
if(dist[arr[0]][i] == INF) {
cout << "-1\n";
return;
}
}
memset(dp, -1, sizeof dp);
int ans = INF;
rep(i, 0, k) ans = min(ans, 1 + calc(i, (1 << i)));
cout << ans << "\n";
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int t = 1;
// cin >> t;
while(t--)
solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using VI = vector<int>;
using VL = vector<ll>;
using VS = vector<string>;
template<class T> using PQ = priority_queue<T, vector<T>, greater<T>>;
#define FOR(i,a,n) for(int i=(a);i<(n);++i)
#define eFOR(i,a,n) for(int i=(a);i<=(n);++i)
#define rFOR(i,a,n) for(int i=(n)-1;i>=(a);--i)
#define erFOR(i,a,n) for(int i=(n);i>=(a);--i)
#define SORT(a) sort(a.begin(),a.end())
#define rSORT(a) sort(a.rbegin(),a.rend())
#define fSORT(a,f) sort(a.begin(),a.end(),f)
#define all(a) a.begin(),a.end()
#define out(y,x) ((y)<0||h<=(y)||(x)<0||w<=(x))
#define tp(a,i) get<i>(a)
#ifdef _DEBUG
#define line cout << "-----------------------------\n"
#define stop system("pause")
#endif
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 60;
constexpr ll mod = 1000000007;
constexpr ll MOD = 998244353;
constexpr ld eps = 1e-10;
constexpr ld pi = 3.1415926535897932;
template<class T>inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; }return false; }
template<class T>inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; }return false; }
inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); }
template<class T>inline istream& operator>>(istream& is, vector<T>& v) { for (auto& a : v)is >> a; return is; }
template<class T, class U>inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template<class T>inline vector<T> vec(size_t a) { return vector<T>(a); }
template<class T>inline vector<T> defvec(T def, size_t a) { return vector<T>(a, def); }
template<class T, class... Ts>inline auto vec(size_t a, Ts... ts) { return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...)); }
template<class T, class... Ts>inline auto defvec(T def, size_t a, Ts... ts) { return vector<decltype(defvec<T>(def, ts...))>(a, defvec<T>(def, ts...)); }
template<class T>inline void print(const T& a) { cout << a << "\n"; }
template<class T, class... Ts>inline void print(const T& a, const Ts&... ts) { cout << a << " "; print(ts...); }
template<class T>inline void print(const vector<T>& v) { for (int i = 0; i < v.size(); ++i)cout << v[i] << (i == v.size() - 1 ? "\n" : " "); }
template<class T>inline void print(const vector<vector<T>>& v) { for (auto& a : v)print(a); }
inline string reversed(const string& s) { string t = s; reverse(all(t)); return t; }
template<class T>inline T sum(const vector<T>& a, int l, int r) { return a[r] - (l == 0 ? 0 : a[l - 1]); }
template<class T>inline void END(T s) { print(s); exit(0); }
void END() { exit(0); }
int main() {
init();
int n, m; cin >> n >> m;
vector<vector<pair<int, int>>> g(n);
VI c(m);
FOR(i, 0, m) {
int a, b; cin >> a >> b >> c[i];
--a, --b, --c[i];
g[a].emplace_back(b, i);
g[b].emplace_back(a, i);
}
VI ans(n, -1);
ans[0] = 0;
vector<bool> dp(n);
dp[0] = true;
queue<int> bfs;
bfs.push(0);
while (!bfs.empty()) {
int cur = bfs.front();
bfs.pop();
for (auto [to, i] : g[cur]) {
if (dp[to])continue;
dp[to] = true;
if (ans[cur] == c[i]) {
if (c[i] == 0)ans[to] = 1;
else ans[to] = 0;
}
else ans[to] = c[i];
bfs.push(to);
}
}
FOR(i, 0, n)print(++ans[i]);
return 0;
} |
#include<bits/stdc++.h>
#define maxn 100010
using namespace std;
typedef long long LL;
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;
}
int n;
double f[maxn];
signed main()
{
n=read();
for(int i=n-1;i;i--) f[i]=f[i+1]+1.*n/(n-i);
cout<<fixed<<setprecision(10)<<f[1];
return 0;
} | #include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
int main(){
int n;cin>>n;
double res = 0;
for(int i=1;i<n;++i){
double tmp = 1.0 * n / i;
res += tmp;
}
cout<<setprecision(10)<<res<<endl;
} |
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define REP(i,n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define debug(var) do{cout << #var << " : "; view(var);}while(0)
template<class T> bool chmin(T &a, T b) {if(a>b) {a=b;return 1;}return 0;}
template<class T> bool chmax(T &a, T b) {if(a<b) {a=b;return 1;}return 0;}
using namespace std;
template<class T> void view(T e) {cout << e << endl;}
template<class T> void view(const vector<T> &v) {for(const auto &e : v){cout << e << " ";} cout << endl;}
template<class T> void view(const vector<vector<T>> &vv) {for(const auto &v : vv){view(v);}}
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int,int>;
const int inf = 1<<30;
const ll inf_l = 1LL<<61;
const int MAX = 4 * 1e5;
// union-find
struct union_find {
vector<int> dat;
union_find(int size) {dat.resize(size, -1);}
int find(int x) {
if (dat[x] < 0) {return x;}
return dat[x] = find(dat[x]);
}
bool unite(int x, int y) {
x = find(x); y = find(y);
if (x == y) {return false;}
if (dat[x] > dat[y]) swap(x, y);
dat[x] += dat[y];
dat[y] = x;
return true;
}
int size(int x) {return -dat[find(x)];}
vvint groups() {
int n = dat.size();
vector<vector<int>> res(n);
for (int i = 0; i < n; i++) {
res[find(i)].push_back(i);
}
res.erase(
remove_if(res.begin(), res.end(),
[](const vint &v) {return v.empty();}),
res.end());
return res;
}
};
int main() {
int n; cin >> n;
union_find uf(MAX);
vector<bool> B(MAX, false);
rep(i,n) {
int a, b; cin >> a >> b;
a--; b--;
if (a != b) {
if (!uf.unite(a, b)) {
B[a] = true; B[b] = true;
}
} else B[a] = true;
}
rep(i,MAX) {
if (B[i]) B[uf.find(i)] = true;
}
vvint group = uf.groups();
ll ans = 0;
rep(i,group.size()) {
if (B[uf.find(group[i][0])]) ans += group[i].size();
else ans += group[i].size() - 1;
}
cout << ans << endl;
} | #include<bits/stdc++.h>
using namespace std;
vector<int> c[100005];
int n,p,s[100005];
bool cmp(pair<int,int> a,pair<int,int> b)
{
if(a.first!=b.first) return a.first>b.first;
return a.second>b.second;
}
void dfs1(int x)
{
s[x]=1;
for(int i=0;i<c[x].size();++i)
{
int y=c[x][i];
dfs1(y);
s[x]+=s[y];
}
}
int dfs(int x)
{
int nf=0;bool f=0;
vector<pair<int,int> > k,k1;
for(int i=0;i<c[x].size();++i)
{
int y=c[x][i],u=dfs(y);
if(s[y]%2) k.push_back(make_pair(u*2-s[y],u));
else k1.push_back(make_pair(u*2-s[y],u));
}
sort(k.begin(),k.end(),cmp);
sort(k1.begin(),k1.end(),cmp);
if(k.size()%2) f=1;
for(int i=0;i<k1.size();++i)
if(k1[i].first>=0||!f) nf+=k1[i].second;
else nf+=k1[i].second-k1[i].first;
for(int i=0;i<k.size();++i)
if(i%2==0) nf+=k[i].second;
else nf+=k[i].second-k[i].first;
return nf;
}
int main()
{
cin>>n;
for(int i=1;i<n;++i)
{
cin>>p;
c[p].push_back(i+1);
}
dfs1(1);
cout<<n-dfs(1);
return 0;
} |
#define TEMPLATE_USED
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> pl;
typedef vector<pl> vp;
const ll INF = 1001001001;
const ll LINF = 1001001001001001001;
const ll D4[] = {0, 1, 0, -1, 0};
const ll D8[] = {0, 1, 1, 0, -1, -1, 1, -1, 0};
#define _overload3(_1, _2, _3, name, ...) name
#define _rep2(i, n) for (ll i = 0; i < (n); ++i)
#define _rep3(i, a, b) for (ll i = (ll)(a); i < (ll)(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, _rep3, _rep2)(__VA_ARGS__)
#define _repe2(i, n) for (ll i = 0; i <= (ll)(n); ++i)
#define _repe3(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); ++i)
#define repe(...) _overload3(__VA_ARGS__, _repe3, _repe2)(__VA_ARGS__)
#define _rrep2(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define _rrep3(i, a, b) for (ll i = (ll)(b)-1; i >= (ll)(a); i--)
#define rrep(...) _overload3(__VA_ARGS__, _rrep3, _rrep2)(__VA_ARGS__)
#define _rrepe2(i, n) for (ll i = (ll)(n); i >= 0; i--)
#define _rrepe3(i, a, b) for (ll i = (ll)(b); i >= (ll)(a); i--)
#define rrepe(...) _overload3(__VA_ARGS__, _rrepe3, _rrepe2)(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
template <class T>
bool chmax(T &a, const T &b)
{
if (a < b)
{
a = b;
return 1;
}
return 0;
}
template <class T>
bool chmin(T &a, const T &b)
{
if (b < a)
{
a = b;
return 1;
}
return 0;
}
const string YES = "Yes";
const string NO = "No";
vl A;
void solve();
int main()
{
cin.tie(0);
ios::sync_with_stdio(0);
A = vl(3);
for (int i = 0; i < 3; ++i)
{
cin >> A[i];
}
solve();
}
// 1 <= A_i <= 100
// 入力は全て整数
// vl A
// A を並び替えて等差数列にできるなら Yes、できないなら No と出力せよ。
void solve()
{
sort(all(A));
cout << (A[2] - A[1] == A[1] - A[0] ? "Yes" : "No") << endl;
}
| #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
vi A(3);
for(int i = 0; i < 3; ++i) cin >> A[i];
sort(A.begin(), A.end());
if(A[1]-A[0] == A[2]-A[1]) cout << "Yes\n";
else cout << "No\n";
return 0;
} |
#include <iostream>
using namespace std;
#define flash ios::sync_with_stdio(0);cin.tie(0);
int main(){
flash;
int x,u;
cin >> x >> u;
if(u % x == 0){
cout << "Yes";
}
else{
cout << "No";
}
} | #pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <queue>
#include <cmath>
#include <map>
#include <set>
#define mem(a,x) memset(a,x,sizeof(a))
#define gi(x) scanf("%d",&x)
#define gi2(x,y) scanf("%d%d",&x,&y)
#define gll(x) scanf("%lld",&x)
#define gll2(x,y) scanf("%lld%lld",&x,&y)
using namespace std;
const double eps=1e-8;
typedef long long ll;
const int MAXN=100005;
const ll mod=1e7+9;
const int inf=0x3f3f3f3f;
int main(){
int x;
cin>>x;
cout<<100-(x%100)<<endl;
return 0;
}
|
#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <ctype.h>
#include <math.h>
#include <stack>
#include <string>
#include <string.h>
using namespace std;
const double PI = acos(-1.0);
const long mod = 1000000007;
const long INF = 1e9 + 1;
int main() {
int t;
cin >> t;
for(int i = 0; i < t; i++) {
int n,a,b; cin >> n >> a >> b;
long ans,x1,x2,x3,x4;
if(n-a-b < 0) x4 = 0;
else x4 = (n-a-b+2) % mod * ((n-a-b+1) % mod) / 2 % mod;
x3 = 2 * x4 % mod;
x2 = ((n-a+1)%mod * ((n-b+1)%mod) % mod - x3 ) % mod;
x1 = (x2 % mod) * (x2 % mod) % mod;
long aa = (n-a+1) % mod * ((n-a+1) % mod) % mod;
long bb = (n-b+1) % mod * ((n-b+1) % mod) % mod;
ans = (aa % mod * (bb % mod) - x1) % mod;
cout << ans % mod << endl;
}
} | // ###### ### ####### ####### ## # ##### ### ##### //
// # # # # # # # # # # # # # ### //
// # # # # # # # # # # # # # ### //
// ###### ######### # # # # # # ######### # //
// # # # # # # # # # # #### # # # //
// # # # # # # # ## # # # # # //
// ###### # # ####### ####### # # ##### # # # # //
// #pragma GCC optimize(2)
// #pragma GCC optimize(3)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC target("avx,avx2,fma")
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> ppl;
#define ain(a,n) for(ll i=0;i<(n);++i) cin>>(a)[i];
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define loop(i,n) for(ll i=0;i<(n);++i)
#define FOR(i,a,b) for(ll i=(a);i<=(b);++i)
#define FORD(i,a,b) for(ll i=(a);i>=(b);--i)
#define cases ll T=0;cin>>T;while(T--)
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
#define END "\n"
#define pb push_back
#define mp make_pair
#define go(c,itr) for(auto itr=(c).begin(); itr!=(c).end(); ++itr)
// #define back(c,itr) for(auto itr=(c).rbegin(); itr!=(c).rend(); ++itr)
#define PI 3.14159265359
#define inf 9e18
#define MOD 1000000007
#define MODU 998244353
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define MAXN 1000005
#define BLOCK 555
const string alpha = "abcdefghijklmnopqrstuvwxyz";
const ll N = 1000005;
const long double epsilon = 1e-9;
inline ll myceil(ll a, ll b) {
return (ceil(1.0 * a / b * 1.0));
}
ll binexp(ll a, ll b, ll m) {
a %= m;
ll res = 1;
while (b > 0) {
if (b & 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
ll modinvfermat(ll a, ll m)
{
return binexp(a, m - 2, m);
}
void task(bool flag)
{
if (flag)
cout << "YES\n";
else
cout << "NO\n";
}
ll lcm(ll a, ll b)
{
return ((1LL * a * b) / (__gcd(a, b)));
}
signed main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
fast
cases
{
ll n, a, b;
cin >> n >> a >> b;
ll ans = 0;
if (a > b)
swap(a, b);
ll sum = 0;
if (n - b >= a)
{
ll x = n - b - a + 1;
sum = (x * (x + 1) / 2) % MOD;
}
ans = (4 * (n - b + 1) % MOD * ((n - a + 1) % MOD * sum % MOD) % MOD) % MOD;
// cout << ans << END;
ll val = (sum % MOD * sum % MOD) % MOD;
(ans -= 4 * val) %= MOD;
(ans += MOD) %= MOD;
cout << ans << END;
}
#ifndef ONLINE_JUDGE
cout << "\nTime Elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " sec\n";
#endif
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define reps(i, c, n) for(int i = c; i < n; i++)
#define inv(n, a) for(int i = 0; i < n; i++) cin >> a[i]
#define MOD 1000000007
#define INF 1e7
#define LINF 1e15
typedef long long ll;
typedef long double ld;
using Graph = vector<vector<int>>;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
void print(){ cout << endl; }
template <class H, class... T> void print(H&& h, T&&... t){ cout << h << ", "; print(forward<T>(t)...); }
template <class C> void dump(C o){ rep(i, o.size()-1) cout << o[i] << ", "; cout << o.back() << endl; }
int main() {
int n, x; cin >> n >> x;
bool f = false;
rep(i, n){
int a; cin >> a;
if (a != x){
if (f)
cout << " ";
cout << a;
f = true;
}
}
cout << endl;
}
| #include<bits/stdc++.h>
#define f(i,a,b) for(int i=a;i<=b;++i)
#define ff(i,a,b) for( int i=a;i>=b;--i)
#define debug(x) cerr << #x << " : " << x << " " << endl
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<string, string> pss;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ll inf = 2e18;
const double tiaohe = 0.57721566490153286060651209;
inline ll in() { char ch = getchar();ll x = 0, f = 1;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 int N = 2e5 + 10;
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
int n, x;cin >> n >> x;
f(i, 1, n) {
int now = in();
if (now == x)continue;
cout << now << " ";
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin>>n;
int s=0,p3=0,r3=1;
for(int i=0;i<n;i++){
char c;
cin>>c;
int x=0;
if(c=='B')x=1;
else if(c=='R')x=2;
if(p3==0)s=(s+x*r3)%3;
if(i==n-1)break;
int m=n-1-i;
while(m%3==0){
p3++;
m/=3;
}
r3=r3*m%3;
m=i+1;
while(m%3==0){
p3--;
m/=3;
}
r3=r3*m%3;
}
if(n%2==0)s=(3-s)%3;
char ans='W';
if(s==1)ans='B';
else if(s==2)ans='R';
cout<<ans<<endl;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = double;
const int mxn = 4e5+10;
const int mod = 3;
int powmod(int a, int b) {
int ret = 1;
while(b) {
if(b&1) ret*=a, ret%=mod;
a*=a; a%=mod;
b>>=1;
}
return ret;
}
int c[3][3];
void pre() {
for(int i=0; i<3; i++) for(int j=0;j<=i; j++) {
if(j==0) c[i][j] = 1;
else c[i][j] = (c[i-1][j] + c[i-1][j-1])%mod;
}
}
int binom(int n, int k) {
if(k>n) return 0;
if(!k) return 1;
vector<int> bn, bk;
while(n) {
bn.push_back(n%3);
n/=3;
}
while(k) {
bk.push_back(k%3);
k/=3;
}
bn.resize(max(bn.size(), bk.size()), 0);
bk.resize(max(bn.size(), bk.size()), 0);
int ret = 1;
for(int i=0; i<(int)bn.size(); i++) {
ret*=c[bn[i]][bk[i]];
ret%=mod;
}
return ret;
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0);
pre();
int n; cin>>n;
vector<int> color(n, 0);
for(int i=0;i<n; i++) {
char b; cin>>b;
if(b=='B') color[i] = 0;
else if(b=='W') color[i] = 1;
else color[i] = 2;
}
int sum = 0;
for(int i=0; i<n; i++) {
//cout << "c: " << C(n-1, i) << '\n';
sum+=binom(n-1, i)*color[i];
sum%=mod;
}
sum*=powmod(2, n-1);
sum%=mod;
if(sum==0) {
cout << "B" << '\n';
}
else if(sum==1) {
cout << "W" << '\n';
}
else cout << "R" << '\n';
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;
int a[n];
f(i, n) cin>>a[i];
int ct = 0, ans = -1;
for(int i = 2; i <= 1001; i++) {
int cct = 0;
f(j, n) {
if(a[j] % i) continue;
cct++;
}
if (cct > ct) {
ct = cct;
ans = i;
}
}
cout<<ans;
}
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 rep(i,n) for(int i=0; i<(n);i++)
using ll = long long;
const ll INF = 9e18;
const ll mod=1000000007;
int main(){
int n, m, l, x;
cin >> n;
vector<int> a(n);
int amax = 0;
rep(i,n){
cin >> a[i];
amax = max(a[i], amax);
}
int ans = 0;
int res = 0;
for(int i=2; i<=amax; i++){
int cans = 0;
rep(j,n){
if(a[j]%i ==0){
cans ++;
}
}
if(cans > ans){
ans = cans;
res = i;
}
}
cout << res << endl;
} |
#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;
#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------
int N;
ll A[101010],B[101010];
vector<ll> V[2];
void solve() {
int i,j,k,l,r,x,y; string s;
cin>>N;
FOR(i,N) cin>>A[i];
FOR(i,N) cin>>B[i];
ll tsum=0;
for(i=0;i<N;i+=2) {
ll a=max(A[i]+A[i+1],B[i]+B[i+1]);
tsum+=a;
V[0].push_back(A[i]+B[i+1]-a);
V[1].push_back(B[i]+A[i+1]-a);
}
ll ret=tsum;
sort(ALL(V[0]));
sort(ALL(V[1]));
reverse(ALL(V[0]));
reverse(ALL(V[1]));
FOR(i,N/4) {
tsum+=V[0][i]+V[1][i];
ret=max(ret,tsum);
}
cout<<ret<<endl;
}
int main(int argc,char** argv){
string s;int i;
if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
cout.tie(0); solve(); return 0;
}
| #include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define all(v) v.begin(),v.end()
#pragma gcc optimize("O3")
#pragma gcc optimize("Ofast")
#pragma gcc optimize("unroll-loops")
using namespace std;
const int INF = 1e9;
const long long llINF = 5e18;
long long mod;
typedef long long ll;
typedef long double ld;
typedef pair <int,int> pi;
typedef pair <ll,ll> pl;
typedef vector <int> vec;
typedef vector <pi> vecpi;
typedef long long ll;
int n;
int a[100005],b[100005];
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n;
for(int i = 1;i <= n;i++) cin >> a[i];
for(int i = 1;i <= n;i++) cin >> b[i];
ll ans = 0;
for(int i = 1;i <= n;i++) {
if((i & 1) == 0) swap(a[i],b[i]);
ans += a[i];
b[i] -= a[i];
}
sort(b+1,b+n+1);
reverse(b+1,b+n+1);
for(int i = 1;i <= n/2;i++) ans += b[i];
cout << ans;
} |
#include <iostream>
#include <vector>
#include <set>
#include <map>
template <class E> struct FenwickTree {
int _n;
std::vector<E> data;
FenwickTree(int n) : _n(n), data(n) { }
void add(int p, E x) {
++p;
while (p <= _n) {
data[p - 1] += x;
p += p & -p;
}
}
E sum(int r) const {
E s = 0;
while (r > 0) {
s += data[r - 1];
r -= r & -r;
}
return s;
}
E sum(int l, int r) const {
return sum(r) - sum(l);
}
};
long long proc(
std::vector<int> &cur,
int x,
int y,
FenwickTree<int> &ftcnt0,
FenwickTree<long long> &ftsum0,
const FenwickTree<int> &ftcnt1,
const FenwickTree<long long> &ftsum1,
const std::vector<int> &arr)
{
int v = cur[x];
long long dem = (long long)ftcnt1.sum(0, v) * arr[v];
long long cre = (long long)ftcnt1.sum(0, y) * arr[y];
long long adj = (long long)ftsum1.sum(y, v);
ftsum0.add(v, -arr[v]);
ftcnt0.add(v, -1);
cur[x] = y;
ftsum0.add(y, arr[y]);
ftcnt0.add(y, 1);
return cre + adj - dem;
}
int main(int argc, char **argv)
{
int n, m, q;
std::cin >> n >> m >> q;
std::set<int> s = { 0 };
std::vector<int> ta(q), xa(q), ya(q);
for (int i = 0; i < q; i++) {
std::cin >> ta[i] >> xa[i] >> ya[i];
xa[i]--;
s.insert(ya[i]);
}
// std::vector<int> arr(s.begin(), s.end());
int ll = s.size();
std::vector<int> arr(ll);
std::map<int, int> map;
int ii = 0;
for (auto &k : s) {
arr[ii] = k;
map.emplace(k, ii);
++ii;
}
FenwickTree<long long> ftsuma(ll);
FenwickTree<long long> ftsumb(ll);
FenwickTree<int> ftcnta(ll);
FenwickTree<int> ftcntb(ll);
std::vector<int> cura(n);
std::vector<int> curb(m);
ftcnta.add(0, n);
ftcntb.add(0, m);
long long sum = 0;
for (int i = 0; i < q; i++) {
int cy = map[ya[i]];
sum += (ta[i] == 1) ? proc(cura, xa[i], cy, ftcnta, ftsuma, ftcntb, ftsumb, arr)
: proc(curb, xa[i], cy, ftcntb, ftsumb, ftcnta, ftsuma, arr);
std::cout << sum << std::endl;
}
return 0;
} | #include<cstdio>
#include<iostream>
using namespace std;
int n,k;
long long MO,dp[110][180000];
int main()
{
scanf("%d%d%lld",&n,&k,&MO);
long long sum;
dp[0][0]=1;
for(int i=1;i<=n;i++)
{
sum=((1+min(i,n-i-1))*min(i,n-i-1)/2)*k;
for(int kk=0;kk<=k;kk++)
for(int j=kk*i;j<=sum;j++)
(dp[i][j]+=dp[i-1][j-kk*i])%=MO;
}
for(int i=1;i<=n;i++)
{
long long ans=0;
sum=((1+min(i-1,n-i))*min(i-1,n-i)/2)*k;
for(int j=1;j<=sum;j++)
(ans+=(dp[i-1][j]*dp[n-i][j]%MO)*(k+1))%=MO;
printf("%lld\n",(ans+k)%MO);
}
return 0;
} |
#include <bits/stdc++.h>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<string.h>
using namespace std;
#define pb push_back
#define all(v) v. begin(),v. end()
#define rep(i,n,v) for(i=n;i<v;i++)
#define per(i,n,v) for(i=n;i>v;i--)
#define ff first
#define ss second
#define pp pair<ll,ll>
#define ll long long
#define endl '\n'
string tostr(ll n)
{ stringstream rr;rr<<n;return rr. str();}
ll toint(string s)
{stringstream ss(s);ll x;ss>>x;return x;}
void solve()
{
ll n, a,m=0,b=0, c=0,k=0, i, j,l=998244353;
char s, r, y;
ll hit=0, yog;
cin>>s>>r>>y;
if(s==r && s==y) cout<<"Won";
else cout<<"Lost";
}
int main()
{
ios_base::sync_with_stdio(false);
cin. tie(0);cout. tie(0);
ll t=1;
//cin>>t;
while(t--)
{
solve();
}
return 0;
} | #include "bits/stdc++.h"
using namespace std;
#define for_(i, s, e) for (int i = s; i < (int) e; i++)
#define for__(i, s, e) for (ll i = s; i < e; i++)
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
#define endl '\n'
// if two adjacent nodes have same reachability, then they are in SCC
// now we have a DAG to solve with
// if two adjacent nodes have different reachability, then their direction is easily found
// reduced to making graph SCC by directing edges
// read CF blog post for details on how to do that (is-this-fft)
int n, m;
vector<vector<ii>> adj;
vector<ii> ans;
vi vis;
int dfs(int p, int parent) {
if (vis[p]) return 1;
vis[p] = 1;
for (auto i: adj[p]) if (i.first != parent and ans[i.second].first == -1) {
int v = dfs(i.first, p);
// cout << "going edge " << p << " - " << i.first << " ... " << v << endl;
ans[i.second] = {i.first, p};
// if (v) ans[i.second] = {p, i.first};
}
vis[p] = 2;
return 0;
}
int main() {
#ifdef mlocal
freopen("test.in", "r", stdin);
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
vector<ii> edges(m);
adj.resize(n); ans.resize(m, {-1, -1}); vis.resize(n);
for_(i, 0, m) {
int a, b; cin >> a >> b;
a -= 1; b -= 1;
adj[a].push_back({b, i}); adj[b].push_back({a, i});
edges[i] = {a, b};
}
vi reach(n);
for_(i, 0, n) cin >> reach[i];
for_(i, 0, m) if (reach[edges[i].first] != reach[edges[i].second]) {
ans[i] = {edges[i].first, edges[i].second};
if (reach[edges[i].first] < reach[edges[i].second]) ans[i] = {edges[i].second, edges[i].first};
}
for_(i, 0, n) if (!vis[i]) dfs(i, i);
for_(i, 0, m) {
if (edges[i] == ans[i]) cout << "->";
else cout << "<-";
cout << endl;
}
return 0;
} |
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int T;
cin >> T;
int ans[100];
REP(i, T) {
string S;
cin >> S;
ans[i] = -1;
if ("atcoder" < S)
ans[i] = 0;
else{
REP(j, S.length())
{
if(S[j] > 'a') {
if(S[j] > 't')
ans[i] = j-1;
else
ans[i] = j;
break;
}
}
}
}
REP(i,T)
cout << ans[i] << "\n";
} | #include <bits/stdc++.h>
using namespace std;
#define maxn 200010
#define md 998244353
#define ll long long
int dp[maxn][30];
constexpr int top = 300000;
vector<int> fac(top + 1), ifac(top + 1);
inline int add(int a, int b) {
a += b;
if (a >= md) a -= md;
return a;
}
inline int sub(int a, int b) {
a -= b;
if (a < 0) a += md;
return a;
}
inline int mul(int a, int b) {
return (int)((long long)a * b % md);
}
inline int powmod(int a, long long b) {
int res = 1;
while (b > 0) {
if (b & 1) res = mul(res, a);
a = mul(a, a);
b >>= 1;
}
return res;
}
inline int inv(int a) {
a %= md;
if (a < 0) a += md;
int b = md, u = 0, v = 1;
while (a) {
int t = b / a;
b -= t * a; swap(a, b);
u -= t * v; swap(u, v);
}
assert(b == 1);
if (u < 0) u += md;
return u;
}
int binom(int n, int m) {
if (n < m || n < 0 || m < 0) return 0;
return mul(fac[n], mul(ifac[m], ifac[n - m]));
}
void init() {
fac[0] = 1;
for (int i = 1; i <= top; i++) fac[i] = mul(fac[i - 1], i);
ifac[top] = inv(fac[top]);
for (int i = top; i; i--) ifac[i - 1] = mul(ifac[i], i);
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
init();
dp[0][0] = 1;
for (int i = 1; i < 20; i++) dp[0][i] = 1;
for (int i = 1; i <= m; i++) {
for (int j = 0; j < 20; j++) {
for (int k = 0; k * (1 << j) <= i && k <= n; k += 2) {
dp[i][j + 1] = add(dp[i][j + 1], mul(dp[i - k * (1 << j)][j], binom(n, k)));
}
}
}
int ans = 0;
//for(int i=0;i<18;i++) ans=add(ans,dp[m][i]);
printf("%d\n", dp[m][19]);
return 0;
}
|
#include<bits/stdc++.h>
const int N=200010,mod=1e9+7;
char s[N];
int c[N],fac[N],f[17][N],n,KK,a[17],cnt,g[N][17],C[17][17];
int qp(int x,int y)
{
int ans=1;
for(;y;y >>=1,x=1ll*x*x%mod)
if(y&1)ans=1ll*ans*x%mod;
return ans;
}
void ins(int x)
{
if(!a[x]++)cnt++;
}
void eras(int x)
{
if(!--a[x])
cnt--;
}
int main()
{
scanf("%s",s+1),n=strlen(s+1);
scanf("%d",&KK);
for(int i=0;i<=16;i++)
C[i][0]=C[i][i]=1;
for(int i=1;i<=16;i++)
for(int j=1;j<i;j++)
C[i][j]=(C[i-1][j]+C[i-1][j-1])%mod;
for(int i=fac[0]=1;i<=16;i++)
fac[i]=1ll*fac[i-1]*i%mod;
g[0][0]=1;
for(int i=1;i<=n;i++)
for(int j=1;j<=i&&j<=16;j++)
g[i][j]=(1ll*j*g[i-1][j]+g[i-1][j-1])%mod;
f[KK][n+1]=1;
for(int j=0;j<=KK;j++)
for(int i=n;i;i--)
for(int k=0;k<=j;k++)
f[j][i]=(f[j][i]+1ll*C[16-j][KK-j]*C[j][k]%mod*g[n-i+1][KK-j+k]%mod*fac[KK-j+k])%mod;
for(int i=1;i<=n;i++)
c[i]=(isdigit(s[i])?s[i]-'0':s[i]-'A'+10);
int ans=0;
for(int i=1;i<=n;ins(c[i++]))
for(int j=i==1;j<=c[i]-(i!=n);j++)
ins(j),ans=(ans+f[cnt][i+1])%mod,eras(j);
for(int i=2;i<=n;i++)ans=(ans+1ll*f[1][i+1]*15)%mod;
printf("%d\n",ans);
return 0;
} | #include <bits/stdc++.h>
#define DEBUG if(0)
typedef long long ll;
using namespace std;
const int maxSz = 2e5, maxCnt = 16;
const ll mod = 1e9 + 7;
ll dp[maxSz][maxCnt + 1][2][2];
char s[maxSz + 1];
int a[maxSz];
int sz, k;
ll solve(int mask, int pos, bool lmt, bool started) {
int cnt = __builtin_popcount(mask);
if (cnt > k) return 0;
if (pos == sz) return cnt == k && started;
ll &ans = dp[pos][cnt][lmt][started];
if (ans != -1) return ans;
ans = 0;
int llmt = (lmt ? a[pos] : 15);
for (int i = 0; i <= llmt; i++)
ans = (ans + solve((!i && !started) ? mask : mask | (1 << i), pos + 1, lmt & (i == llmt), started | (i > 0))) % mod;
return ans;
}
int main() {
cin >> s >> k;
sz = strlen(s);
for (int i = 0; i < sz; i++) {
a[i] = s[i] - (isdigit(s[i]) ? '0' : 'A' - 10);
}
DEBUG
{
printf("%s\n", s);
for (int i = 0; i < sz; i++)
printf("%d ", a[i]);
printf("\n");
}
memset(dp, -1, sizeof(dp));
cout << solve(0, 0, 1, 0) << endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define inf 2000000000
#define infLL 2000000000000000000
#define MAX5 100005
#define MAX6 1000006
#define MAX7 10000007
#define sf(a) scanf("%d", &a)
#define sfl(a) scanf("%lld", &a)
#define sfs(a) scanf("%s", a)
#define sline(a) scanf("%[^\n]%*c", a);
#define pf(a) printf("%d\n", a)
#define pfl(a) printf("%lld\n", a)
#define pfs(a) printf("%s\n", a)
#define Case(t) printf("Case %d: ", t)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mod 1000000007
#define Mod 998244353
#define PI acos(-1.0)
#define eps 1e-9
#define mem(a, b) memset(a, b, sizeof(a))
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
const int N = MAX5;
int dx[] = {0, 0, 1, 1}; ///Up-down, Left-Right
int dy[] = {0, 1, 0, 1};
string s[15];
int check(int i, int j)
{
int cnt = 0;
for(int d = 0; d < 4; d++) {
cnt += (s[i + dx[d]][j + dy[d]]=='#');
}
return (cnt & 1);
}
int main()
{
//#ifndef ONLINE_JUDGE
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
//#endif
FASTIO;
int n, m;
cin>>n>>m;
for(int i = 0; i < n; i++) cin>>s[i];
int res = 0;
for(int i = 0; i < n - 1; i++) {
for(int j = 0; j < m - 1; j++) {
res += check(i, j);
}
}
cout<<res<<endl;
return 0;
}
| //https://atcoder.jp/contests/abc191/tasks/abc191_c
#include<iostream>
#include<iomanip>
#include <vector>
#include <algorithm>
#include <set>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
using namespace std;
#define ll long long int
#define vl vector<ll>
#define vvll vector<vl>
#define vpll vector<pair<ll, ll>>
#define sz(a) (ll)a.size()
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb(a) push_back(a)
#define MAX 100000009
#define FOR(i, a, b) for (ll i = (a); i <= (b); ++i)
#define ROF(i, a, b) for (ll i = (a); i >= (b); --i)
#define fastIO ios_base::sync_with_stdio(false), cin.tie(NULL)
bool comp(const pair<ll, ll> &a, const pair<ll, ll> &b)
{
return a.second > b.second;
}
ll _ceil(ll a, ll b) { return (a + b - 1) / b; }
int main()
{
ll t=1;
//cin>>t;
while(t--)
{
ll h,w;
cin>>h>>w;
string line;
vector<vector<bool>>grid(h,vector<bool>(w));
FOR(i,0,h-1)
{
cin>>line;
FOR(j,0,w-1)
{
grid[i][j] = line[j] == '#';
}
}
ll ans = 0,sum = 0;
FOR(i,0,h-2)
{
FOR(j,0,w-2)
{
sum = 0;
sum^=grid[i][j];
sum^=grid[i+1][j];
sum^=grid[i][j+1];
sum^=grid[i+1][j+1];
ans+=sum;
}
}
cout<<ans<<"\n";
}
}
|
#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 oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define pb push_back
#define N 100012
#define ll long long
#define int ll
#define pi pair<int , int>
#define pip pair<pair ,int>
#define mp make_pair
#define f first
#define s second
#define mod 1000000007
ll Mod(ll x, ll y, int p)
{ ll res = 1; x = x % p;
while (y > 0)
{ if (y & 1) res = (res * x) % p;
y = y >> 1; x = (x * x) % p;
} return res;
}
int* getlps(string pattern) {
int len = pattern.length();
int * lps = new int[len];
lps[0] = 0;
int i = 1 , j = 0;
while (i < len) {
if (pattern[i] == pattern[j]) {
lps[i] = j + 1;
i++;
j++;
}
else {
if (j != 0) {
j = lps[j - 1];
}
else {
lps[i] = 0;
i++;
}
}
}
return lps;
}
class Triplet {
public:
int x ;
int y ;
int gcd;
};
Triplet extendedEuclid(int a , int b) {
if (b == 0) {Triplet ans; ans.gcd = a; ans.x = 1; ans.y = 0; return ans;}
Triplet smallAns = extendedEuclid(b , a % b);
Triplet ans;
ans.gcd = smallAns.gcd;
ans.x = smallAns.y;
ans.y = smallAns.x - (a / b) * smallAns.y;
return ans;
}
int mmInverse(int a , int m) {
Triplet ans = extendedEuclid(a , m);
return (ans.x + m) % m;
}
int fact[N];
void calfac(int n ) {
fact[0] = 1;
for (int i = 1 ; i <= n + 2; i++) {
fact[i] = (((fact[i - 1] % mod) * (i % mod)) % mod + mod) % mod;
}
}
int calc(int n , int r) {
if (r > n)return 0;
if (r == n)return 1;
int ans = 1;
ans = ((ans % mod) * (fact[n]) % mod + mod) % mod;
ans = ((ans % mod) * (mmInverse(fact[n - r] , mod) % mod) + mod) % mod;
ans = ((ans % mod) * (mmInverse(fact[r] , mod) % mod) + mod) % mod;
return (ans + mod) % mod;
}
void solve(int cntt) {
int n;
cin >> n;
int x = 1;
int cnt = 1;
cout << (n + 99) / 100 << endl;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// preprocess();
int t;
t = 1;
int l = 0;
while (t--) {
l++;
solve(l);
}
return 0;
} | #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;
}
ll a,b,ans;
struct node{
ll x,y;
};
set<ll> s;
queue<node> q;
void calc(ll x,ll y){
if(s.find(x)!=s.end()) return ;
s.insert(x);
q.push({x,y});
}
void bfs(){
if(a>=b){
ans=a-b;
return ;
}
ans=b-a;
q.push({b,0ll});
s.insert(b);
while(!q.empty()){
node now=q.front();
q.pop();
ans=min(ans,now.y+abs(now.x-a));
if(now.x<=a) break;
if(now.x&1){
calc(now.x+1,now.y+1);
calc(now.x-1,now.y+1);
}
else calc(now.x/2,now.y+1);
}
}
int main(){
a=read(),b=read();
bfs();
printf("%lld\n",ans);
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define endl '\n'
#define vec vector<ll>
#define mat vector<vector<ll> >
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef long double ld;
typedef complex<double> comp;
const ll INF=1e9+7;
const ll inf=INF*INF;
const ll MOD=998244353;
const ll mod=MOD;
const int MAX=200010;
//Union-Find木
struct UnionFind {
vector< int > data;
UnionFind() = default;
explicit UnionFind(size_t sz) : data(sz, -1) {}
bool unite(int x, int y) {
x = find(x), y = find(y);
if(x == y) return false;
if(data[x] > data[y]) swap(x, y);
data[x] += data[y];
data[y] = x;
return true;
}
int find(int k) {
if(data[k] < 0) return (k);
return data[k] = find(data[k]);
}
int size(int k) {
return -data[find(k)];
}
bool same(int x, int y) {
return find(x) == find(y);
}
};
signed main(){
ll n,k;cin>>n>>k;
vector<ll>fac(n*n+1);
fac[0]=1;
REP(i,1,n+1){
fac[i]=fac[i-1]*i%mod;
}
UnionFind uf1(n);
UnionFind uf2(n);
vector<vector<ll> >a(n,vector<ll>((n)));
rep(i,n){
rep(j,n){
cin>>a[i][j];
}
}
rep(i,n){
REP(j,i+1,n){
bool f=1;
rep(l,n){
if(a[i][l]+a[j][l]>k)f=0;
}
if(f)uf1.unite(i,j);
}
}
rep(i,n){
REP(j,i+1,n){
bool f=1;
rep(l,n){
if(a[l][i]+a[l][j]>k)f=0;
}
if(f)uf2.unite(i,j);
}
}
ll ans=1;
vector<ll>used1(n);
vector<ll>used2(n);
rep(i,n){
if(used1[uf1.find(i)])continue;
used1[uf1.find(i)]=1;
ans=ans*fac[uf1.size(i)]%mod;
}
rep(i,n){
if(used2[uf2.find(i)])continue;
used2[uf2.find(i)]=1;
ans=ans*fac[uf2.size(i)]%mod;
}
cout<<ans<<endl;
} | #include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <cstring>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <random>
const int32_t MOD = 998244353;
int32_t dfs(int32_t x, std::vector< bool > &vis, const std::vector< std::vector< bool > > &adj) {
vis[x] = true;
int32_t comp = 1;
int32_t n = adj.size();
for(int32_t i = 0; i < n; i++) {
if(adj[x][i] && !vis[i]) {
comp += dfs(i, vis, adj);
}
}
return comp;
}
int32_t fact(int32_t x) {
int32_t ans = 1;
for(int32_t i = 2; i <= x; i++) {
ans = (int64_t) ans * i % MOD;
}
return ans;
}
int32_t count(const std::vector< std::vector< bool > > &adj) {
int32_t n = adj.size(), ans = 1;
std::vector< bool > vis(n, false);
for(int32_t i = 0; i < n; i++) {
if(!vis[i]) {
ans = (int64_t) ans * fact(dfs(i, vis, adj)) % MOD;
}
}
return ans;
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int32_t n, k;
std::cin >> n >> k;
std::vector< std::vector< int32_t > > v(n, std::vector< int32_t >(n));
for(int32_t i = 0; i < n; i++) {
for(int32_t j = 0; j < n; j++) {
std::cin >> v[i][j];
}
}
std::vector< std::vector< bool > > rows(n, std::vector< bool >(n, false));
for(int32_t i1 = 0; i1 < n; i1++) {
for(int32_t i2 = i1 + 1; i2 < n; i2++) {
int32_t cnt = 0;
for(int32_t j = 0; j < n; j++) {
if(v[i1][j] + v[i2][j] <= k) {
cnt++;
}
}
if(cnt == n) {
rows[i1][i2] = true;
rows[i2][i1] = true;
}
}
}
std::vector< std::vector< bool > > cols(n, std::vector< bool >(n, false));
for(int32_t i1 = 0; i1 < n; i1++) {
for(int32_t i2 = i1 + 1; i2 < n; i2++) {
int32_t cnt = 0;
for(int32_t j = 0; j < n; j++) {
if(v[j][i1] + v[j][i2] <= k) {
cnt++;
}
}
if(cnt == n) {
cols[i1][i2] = true;
cols[i2][i1] = true;
}
}
}
std::cout << (int64_t) count(rows) * count(cols) % MOD << '\n';
}
|
#include <bits/stdc++.h>
#define fi first
#define se second
#define ins insert
#define pb push_back
#define sz(x) ((int)x.size())
#define all(x) x.begin(), x.end()
#define fk(x, a, b) for (int x = (a); x < (b); ++x)
#define fb(x, a, b) for (int x = (a); x <= (b); ++x)
#define fan(x, a, b) for (int x = (a); x >= (b); --x)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;
map<ll, ll> is;
ll ff(ll x, ll y) {
if (is[y]) return is[y];
if (x >= y) return is[y] = x - y;
if (y % 2) {
return is[y] = min(y - x, min(ff(x, y - 1) + 1, ff(x, y + 1) + 1));
}
else {
return is[y] = min(y - x, ff(x, y / 2) + 1);
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll x, y;
cin >> x >> y;
cout << ff(x, y) << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
int main(){
long X, Y;
cin >> X >> Y;
unordered_map<long, long> dp;
function<long(long)> solve = [&](long y){
if(dp[y]) return dp[y];
if(y == 1) return dp[y] = abs(X - y);
if(y & 1) return dp[y] = min({abs(X - y), solve((y + 1) / 2) + 2, solve((y - 1) / 2) + 2});
else return dp[y] = min(abs(X - y), solve(y / 2) + 1);
};
cout << solve(Y) << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int ignore;
string inp;
cin >> ignore >> inp;
string stac;
for (const auto& x : inp)
{
stac.push_back(x);
while (stac.length() >= 3 && stac[stac.length() - 1] == 'x' &&\
stac[stac.length() - 2] == 'o' && stac[stac.length() - 3] == 'f')
stac.pop_back(), stac.pop_back(), stac.pop_back();
}
cout << stac.length() << "\n";
return 0;
} | #include<iostream>
using namespace std;
int main() {
int N,i;
string s,t="";
cin>>N>>s;
for (i=0;i<N;i++) {
t += s[i];
if (t.size() >= 3 && t.substr(t.size()-3) == "fox") t = t.substr(0, t.size()-3);
}
cout<<t.size()<<endl;
return 0;
}
|
//GIVE ME AC!!!
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i=0; i<(n);++i)
using ll = long long;
using P = pair<int,int>;
#define _GLIBCXX_DEBUG
int a,b,c,i,j,k,l,m,n,ans,tmp,hoge,fuga,piyo,count,N,M;
int x[10010],y[10010],d[110][110];
void solve(){
for(k=0;k<n;k++){
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(d[i][j]>d[i][k]+d[k][j])d[i][j]=d[i][k]+d[k][j];
}
}
}
}
int main(){
cin >> N >> M;
n = N/100 + (N%100)/10 + (N%100)%10;
m = M/100 + (M%100)/10 + (M%100)%10;
cout << max(n,m) <<endl;
} | #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define all(v) v.begin(),v.end()
const int mod = 1e9 + 7;
int main (){
fast_io;
ll a, b;
cin >> a >> b;
bool flag = false;
if (a < b) {
swap(a, b);
flag = true;
}
vector<ll> v;
for (int i = 1; i <= a; i++)
v.push_back(i);
ll sum = (a * (a + 1)) / 2;
for (int i = 1; i <= b; i++) {
ll left = b - i;
ll start = 1, last = sum, ans = -1;
while (start <= last) {
ll mid = (start + last) / 2;
ll leftsum = sum - mid;
if (leftsum >= (left * (left + 1)) / 2) {
ans = mid;
start = mid + 1;
}
else last = mid - 1;
}
v.push_back(-ans);
sum -= ans;
}
if (flag == false) {
for (auto it: v)
cout << it << ' ';
}
else {
for (auto it: v)
cout << -it << ' ';
}
cout << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
long long w;
cin >> w;
const int maxa = 2e5 + 1;
vector<long long> c (maxa, 0);
while (n--) {
long long s, t, p;
cin >> s >> t >> p;
c[s] += p;
c[t] -= p;
}
long long sum = 0;
for (int i = 0; i < maxa; i++) {
sum += c[i];
if (sum > w) {
cout << "No" << '\n';
return 0;
}
}
cout << "Yes" << '\n';
return 0;
}
| // //
// author : S4M4R //
// 14:51:47 | 13-10-2020 //
// //
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
void IN()
{
// cin.tie(nullptr)->sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
void solve()
{
vector <long long> v(4);
for (auto & a : v) cin >> a;
sort(v.begin(), v.end());
long long a(v[0]), b(v[1]), c(v[2]), d(v[3]);
if (a+b == c+d || a+b+c == d || a+d == b+c) {
cout << "Yes" << '\n'; return;
}
cout << "No" << '\n';
}
int32_t main()
{
IN();
int t(1); //cin >> t;
while(t--) solve(); return 0;
} |
// Created at 2021/01/23 20:56
// {TODO}WA, {TODO}min, {TODO}diff
#include <bits/stdc++.h>
//#include <atcoder/all>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define rep(i, n) for (lint i = 0, i##_len = n; i < i##_len; i++)
#define rep2(i, n) for (lint i = 1, i##_len = n; i <= i##_len; i++)
#define rep3(i, s, e) for (lint i = s, i##_len = e; i < i##_len; i++)
#define all(i) (i).begin(), (i).end()
#define print(s) cout << s << "\n";
#define print2(s1, s2) cout << s1 << " " << s2 << "\n";
#define mk_p make_pair
using namespace std;
//using namespace atcoder;
using lint = long long;
using pi = pair<int, int>;
using pl = pair<lint, lint>;
using vi = vector<int>;
using vl = vector<lint>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using qi = queue<int>;
using ql = queue<lint>;
constexpr int INF = 1 << 30;
constexpr lint INFl = 1LL << 62;
template<class T, class U>
istream &operator>>(istream &is, pair <T, U> &pair) {
is >> pair.first >> pair.second;
return is;
}
template<class T, class U>
ostream &operator<<(ostream &os, pair <T, U> &pair) {
os << pair.first << " " << pair.second << "\n";
return os;
}
template<class T>
istream &operator>>(istream &is, vector <T> &vec) {
for (auto &v : vec) is >> v;
return is;
}
template<class T>
ostream &operator<<(ostream &os, const vector <T> &vec) {
os << '[';
for (auto v : vec) os << v << ',';
os << ']';
return os;
}
template<class T>
inline bool chmax(T &a, T b) {
if (b > a) {
a = b;
return true;
}
return false;
}
template<class T>
inline bool chmin(T &a, T b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template<class T>
T gcd(T x, T y) {
if (x == 0) {
if (y == 0)
return 1;
else
return y;
}
if (y == 0) return x;
if (x % y == 0)
return y;
else
return gcd(y, x % y);
}
template<class T>
inline T lcm(T x, T y) {
return x / gcd(x, y) * y;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
// cout << fixed << setprecision(15);
/*----------------------------------------------------*/
int n, x; cin >> n >> x;
x *= 100;
vpi vp(n); cin >> vp;
int now = 0;
rep(i, n) {
now += vp.at(i).first * vp.at(i).second;
if (now > x) {
print(i + 1)
return 0;
}
}
print(-1)
}
// map->bucket?
// あまり->非負か確認
// set? multiset?
// オーバーフロー確認(最大値を求める)
// 入力確認
// 型確認
// ループ回数確認(TLE)
// 小数の計算は避けよう(割り算)(0割りだめ!絶対!)
// Python を使っては?
// 擬似コードを作っては?
// 汚くてもok
| #include <bits/stdc++.h>
using namespace std;
#define let const auto
#define var auto
#define ref var &
#define cref let &
typedef uint32_t uint;
typedef int64_t int64;
typedef uint64_t uint64;
template<class T>
T read() {
T x;
cin >> x;
return x;
}
let MULTIPLE_TEST_CASES = false;
void solve() {
let n = read<uint>();
var k = read<int64>();
var friends = vector<pair<int64, int64>>(n);
for (uint i = 0; i < n; i++) {
cin >> friends[i].first;
cin >> friends[i].second;
}
sort(friends.begin(), friends.end());
int64 curr = 0;
for (cref p : friends) {
let max = curr + k;
if (max >= p.first) {
k -= p.first - curr;
k += p.second;
curr = p.first;
} else {
curr += k;
k = 0;
break;
}
}
curr += k;
cout << curr;
}
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
let testCases = MULTIPLE_TEST_CASES ? read<uint>() : 1;
for (uint testCase = 1; testCase <= testCases; testCase++) {
solve();
}
}
|
/*
********************
** MEET SHAH **
** DA-IICT **
** decoder_0712 **
********************
*/
#include <bits/stdc++.h>
using namespace std;
//#include <time.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define setpre(x,d) fixed<<setprecision(d)<<x
//#define fill(a,x) memset(a,x,sizeof(a));
#define inarr(a,n) for(int i=0;i<n;i++) cin>>a[i];
#define outarr(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl;
#define maxa(a,n) *max_element(a,a+n)
#define mina(a,n) *min_element(a,a+n)
#define fr(i,s,e) for(ll i=s;i<e;i++)
#define rf(i,s,e) for(ll i=s-1;i>=e;i--)
#define deb1(x) cout<<#x<<" : "<<x<<endl;
#define deb2(x,y) cout<<#x<<" : "<<x<<"\t"<<#y<<" : "<<y<<endl;
#define deb3(x,y,z) cout<<#x<<" : "<<x<<"\t"<<#y<<" : "<<y<<"\t"<<#z<<" : "<<z<<endl;
#define deb4(x,y,z,w) cout<<#x<<" : "<<x<<"\t"<<#y<<" : "<<y<<"\t"<<#z<<" : "<<z<<"\t"<<#w<<" : "<<w<<endl;
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
#define sz size()
#define len length()
#define all(v) v.begin(),v.end()
#define PI 3.141592653589793
#define MOD 1000000007
#define NN (const int)5e5+10
#define ub upper_bound
typedef long long int ll;
typedef long double lld;
typedef pair<long long,long long> pll;
typedef vector<long long> vll;
typedef vector<pair<long long,long long> > vpll;
bool sortbysec(const pair<int,int> &a,const pair<int,int> &b) { return (a.second < b.second); }
// void fileio() { freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); }
// vll fact(NN),invfact(NN);
// void EE(ll a,ll b,ll &x,ll &y) { if(b==0) { x=1,y=0; return ; } EE(b,a%b,x,y); ll temp=x; x=y; y=temp-(a/b)*y; }
// ll inverse(ll a,ll mod) { ll x,y; EE(a,mod,x,y); return (x+mod)%mod; }
// void build() { fact[0]=1; fr(i,1,NN) fact[i]=(fact[i-1]*i)%MOD; invfact[NN-1]=inverse(fact[NN-1],MOD); rf(i,NN-1,0) invfact[i]=(invfact[i+1]*(i+1))%MOD; }
// ll ncr(ll n,ll r) { if(n<0 || r<0 || r>n) return 0; return (fact[n]*((invfact[n-r]*invfact[r])%MOD))%MOD; }
// ll choose(ll n,ll k) { if(k==0) return 1; return (n* choose(n-1,k-1))/k; }
ll logy(ll x,ll y) { return ceil((lld)(log(x)/log(y))); }
ll powe(ll a,ll b) { ll re=1; while(b){ if(b&1) re=(re*a)%MOD; a=(a*a)%MOD; b=b>>1;} return re; }
/*
ll i,j,x,y,k,z,cnt;
vector<vector<ll> > a(100005);
vector<ll> vis(100005,0),visited(100005,0);
void dfs(int u)
{
visited[u]=1;
vector<ll>::iterator it;
for(it=a[u].begin();it!=a[u].end();it++)
{
if(visited[*it]==0)
{
dfs(*it);
}
}
}
*/
bool flag=0,flag1=0,flag2=0;
int main()
{
IOS
ll a,b,c,d,ans;
cin>>a>>b>>c>>d;
ll t1=a-c;
ll t2=a-d;
ll t3=b-c;
ll t4=b-d;
ll mx1=max(t1,t2);
ll mx2=max(t3,t4);
cout<<max(mx1,mx2)<<endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define res register int
#define ll long long
//#define cccgift
#define lowbit(x) ((x)&-(x))
#define rep(i,l,r) for(res i=l,_r=r;i<=_r;++i)
#define per(i,r,l) for(res i=r,_l=l;i>=_l;--i)
#define mkp make_pair
#define pb push_back
#define mem0(a) memset(a,0,sizeof(a))
#define mem0n(a,n) memset(a,0,(n)*sizeof(a[0]))
#define iter(x,v) for(res v,_p=head[x];v=ver[_p],_p;_p=nxt[_p])
#ifdef cccgift //by lqh
#define SHOW(x) cerr<<#x"="<<(x)<<endl
#else
#define SHOW(x) 0
#endif
//#define getchar()(ip1==ip2&&(ip2=(ip1=ibuf)+fread(ibuf,1,1<<21,stdin),ip1==ip2)?EOF:*ip1++)
//char ibuf[1<<21],*ip1=ibuf,*ip2=ibuf;
template<typename T>
inline void read(T &x)
{
static char ch;bool f=1;
for(x=0,ch=getchar();!isdigit(ch);ch=getchar()) if(ch=='-') f=0;
for(;isdigit(ch);x=(x<<1)+(x<<3)+(ch^48),ch=getchar());x=f?x:-x;
}
template<typename T>
void print(T x)
{
if (x<0) x=-x,putchar('-');
if (x>9) print(x/10);
putchar(x%10+48);
}
template<typename T>
inline void print(T x,char ap) {print(x);if (ap) putchar(ap);}
template<typename T>
inline void chkmax(T &x,const T &y) {x=x<y?y:x;}
template<typename T>
inline void chkmin(T &x,const T &y) {x=x<y?x:y;}
unordered_map<int,bool> mp;
int t,n,x;
int main()
{
read(t);
while(t--) {
read(n),mp.clear();
rep(i,1,n) read(x),mp[x]^=1;
if(n&1) {puts("Second");continue;}
bool flag=false;
for(auto &p:mp) if(p.second) flag=true;
puts(flag?"First":"Second");
}
return 0;
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?), set tle
* do something instead of nothing and stay organized
*/
|
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<ll, ll>;
using Pld = pair<ld, ld>;
using Vec = vector<ll>;
using VecP = vector<P>;
using VecB = vector<bool>;
using VecC = vector<char>;
using VecD = vector<ld>;
using VecS = vector<string>;
template <class T>
using Vec2 = vector<vector<T>>;
#define REP(i, m, n) for(ll i = (m); i < (n); ++i)
#define REPN(i, m, n) for(ll i = (m); i <= (n); ++i)
#define REPR(i, m, n) for(ll i = (m)-1; i >= (n); --i)
#define REPNR(i, m, n) for(ll i = (m); i >= (n); --i)
#define rep(i, n) REP(i, 0, n)
#define repn(i, n) REPN(i, 1, n)
#define repr(i, n) REPR(i, n, 0)
#define repnr(i, n) REPNR(i, n, 1)
#define all(s) (s).begin(), (s).end()
#define pb push_back
#define fs first
#define sc second
template <class T1, class T2>
bool chmax(T1 &a, const T2 b){if(a < b){a = b; return true;} return false;}
template <class T1, class T2>
bool chmin(T1 &a, const T2 b){if(a > b){a = b; return true;} return false;}
ll pow2(const int n){return (1LL << n);}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
for (const T &i : v) os << i << ' ';
return os;
}
void co() { cout << '\n'; }
template <class Head, class... Tail>
void co(Head&& head, Tail&&... tail) {
cout << head << ' ';
co(forward<Tail>(tail)...);
}
void ce() { cerr << '\n'; }
template <class Head, class... Tail>
void ce(Head&& head, Tail&&... tail) {
cerr << head << ' ';
ce(forward<Tail>(tail)...);
}
void sonic(){ios::sync_with_stdio(false); cin.tie(0);}
void setp(const int n){cout << fixed << setprecision(n);}
constexpr int INF = 1000000001;
constexpr ll LINF = 1000000000000000001;
constexpr ll MOD = 1000000007;
constexpr ll MOD_N = 998244353;
constexpr ld EPS = 1e-11;
const double PI = acos(-1);
struct S {
ll l, v;
bool operator>(const S &rhs) const {
return v > rhs.v;
}
bool operator<(const S &rhs) const {
return v < rhs.v;
}
bool operator==(const S &rhs) const {
return v == rhs.v;
}
void input() {
cin >> l >> v;
}
};
int main(void) {
ll n, m;
cin >> n >> m;
Vec w(n);
rep(i, n) cin >> w[i];
ll max_w = *max_element(all(w));
vector<S> ss(m);
rep(i, m) ss[i].input();
rep(i, m) {
if(max_w > ss[i].v) {
co(-1);
return 0;
}
}
sort(all(ss));
Vec max_l(m);
rep(i, m) {
max_l[i] = ss[i].l;
if (i) chmax(max_l[i], max_l[i - 1]);
}
Vec p(n);
rep(i, n) p[i] = i;
ll ans = LINF;
do {
Vec cs(n);
rep(i, n) {
cs[i] = w[p[i]];
if (i) cs[i] += cs[i - 1];
}
// ce(cs);
Vec q(n, 0);
rep(i, n) {
REP(j, i + 1, n) {
ll pos = lower_bound(all(ss), S{0, cs[j]}) - ss.begin();
if (pos == 0) continue;
chmax(q[j], q[i] + max_l[pos - 1]);
}
REP(j, i + 1, n) cs[j] -= w[p[i]];
}
chmin(ans, q.back());
// ce(ans);
} while (next_permutation(all(p)));
co(ans);
return 0;
} | //include <atcoder>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <tuple>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#define flush fflush(stdout)
#define endl '\n'
#define all(v) v.begin(), v.end()
using namespace std;
//using namespace atcoder;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pl;
const int mod1 = (int)1e9 + 7, mod2 = (int)998244353;
const int INF = (int)1e9;
const ll LINF = (ll)1e18;
const int di[8] = {1, 0, -1, 0, 1, 1, -1, -1}, dj[8] = {0, 1, 0, -1, -1, 1, -1, 1};
#define rep0(i, n) for (i = 0; i < n; i++)
#define rep1(i, a, b) for (i = a; i < b; i++)
template <typename T>
T my_abs(T x){
return (x >= 0)? x : -x;
}
template <typename T>
void chmax(T &a, T b){
a = max(a, b);
}
template <typename T>
void chmin(T &a, T b){
a = min(a, b);
}
ll gcd(ll a, ll b){
if (a > b) return gcd(b, a);
if (a == 0) return b;
return gcd(b % a, a);
}
// --------------------------------------------------------------------------------
int main(void){
int i, j;
int n, a[200003];
cin >> n;
rep0(i, n){
cin >> a[i];
}
ll amax, asum, basesum;
amax = 0;
asum = 0;
basesum = 0;
rep0(i, n){
chmax(amax, (ll)a[i]);
asum += a[i];
basesum += asum;
cout << basesum + amax * (i + 1) << endl;
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, s, e) for (int i = (int)(s); i < (int)(e); i++)
#define _rep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define _REP(i, e, s) for (int i = (int)(e - 1); i >= (int)(s); i--)
#define yes cout << "yes" << endl;
#define Yes cout << "Yes" << endl;
#define YES cout << "YES" << endl;
#define no cout << "no" << endl;
#define No cout << "No" << endl;
#define NO cout << "NO" << endl;
#define AC cout << "AC" << endl;
#define WA cout << "WA" << endl;
#define out(s) cout << s << endl;
#define ll long long
#define ull unsigned long long
#define ALL(x) x.begin(), x.end()
#define SIZE(x) ll(x.size())
const unsigned int BIT_FLAG_0 = (1 << 0); // 0000 0000 0000 0001
const unsigned int BIT_FLAG_1 = (1 << 1); // 0000 0000 0000 0010
const unsigned int BIT_FLAG_2 = (1 << 2); // 0000 0000 0000 0100
const unsigned int BIT_FLAG_3 = (1 << 3); // 0000 0000 0000 1000
const unsigned int BIT_FLAG_4 = (1 << 4); // 0000 0000 0001 0000
const unsigned int BIT_FLAG_5 = (1 << 5); // 0000 0000 0010 0000
const unsigned int BIT_FLAG_6 = (1 << 6); // 0000 0000 0100 0000
const unsigned int BIT_FLAG_7 = (1 << 7); // 0000 0000 1000 0000
const double PI = 3.14159265358979323846;
const int dy[4] = {0, 1, 0, -1};
const int dx[4] = {1, 0, -1, 0};
#define H_MAX 500
#define W_MAX 500
#define INF 1e9 + 7
const long long MOD = 998244353;
bool IsPrime(int num) {
if (num < 2)
return false;
else if (num == 2)
return true;
else if (num % 2 == 0)
return false; // 偶数はあらかじめ除く
double sqrtNum = sqrt(num);
for (int i = 3; i <= sqrtNum; i += 2) {
if (num % i == 0) {
// 素数ではない
return false;
}
}
// 素数である
return true;
}
int main() {
ll N, cnt = 0;
unordered_set<ll> us;
cin >> N;
for (ll i = 2; i * i <= N; i++) {
ll tmp = i * i;
while (tmp <= N) {
us.insert(tmp);
tmp *= i;
}
}
cout << N - us.size() << endl;
} | #include <bits/stdc++.h>
#define LL long long
#define ull unsigned long long
#define F(i, j, k) for (int i = j; i <= k; ++i)
#define DF(i, j, k) for (int i = j; i >= k; --i)
using namespace std;
template <typename T> inline void read(T &n) {
T w = 1;
n = 0;
char ch = getchar();
while (!isdigit(ch) && ch != EOF) {
if (ch == '-') w = -1;
ch = getchar();
}
while (isdigit(ch) && ch != EOF) {
n = (n << 3) + (n << 1) + (ch & 15);
ch = getchar();
}
n *= w;
}
template <typename T> inline void write(T x) {
T l = 0;
ull y = 0;
if (!x) { putchar(48); return; }
if (x < 0) { x = -x; putchar('-'); }
while (x) { y = y * 10 + x % 10; x /= 10; ++l; }
while (l) { putchar(y % 10 + 48); y /= 10; --l; }
}
template <typename T> inline void writes(T x) {
write(x);
putchar(' ');
}
template <typename T> inline void writeln(T x) {
write(x);
puts("");
}
template <typename T> inline void checkmax(T &a, T b) { a = a > b ? a : b; }
template <typename T> inline void checkmin(T &a, T b) { a = a < b ? a : b; }
const int N = 2e5 + 100;
int cnt, head[N], to[N << 1], nxt[N << 1];
inline void addedge(int x, int y) {
nxt[++cnt] = head[x];
to[cnt] = y;
head[x] = cnt;
}
int tp[N], rt, depth[N], mx = 0;
inline void dfs(int x, int fa) {
for (int i = head[x]; i; i = nxt[i]) {
if (to[i] == fa) continue;
depth[to[i]] = depth[x] + 1;
if (depth[to[i]] >= mx) { rt = to[i]; mx = depth[to[i]]; }
dfs(to[i], x);
}
}
int tot, ans[N];
inline void dfs2(int x, int fa) {
ans[x] = ++tot;
for (int i = head[x]; i; i = nxt[i]) {
if (to[i] == fa || tp[to[i]]) continue;
dfs2(to[i], x);
}
for (int i = head[x]; i; i = nxt[i]) {
if (to[i] == fa || !tp[to[i]]) continue;
dfs2(to[i], x);
}
++tot;
}
inline void dfs3(int x, int fa) {
for (int i = head[x]; i; i = nxt[i]) {
if (to[i] == fa) continue;
dfs3(to[i], x);
if (tp[to[i]]) tp[x] = 1;
}
}
int main() {
//freopen(".in", "r", stdin);
//freopen(".out", "w", stdout);
int n;
read(n);
F(i, 1, n - 1) {
int x, y;
read(x); read(y);
addedge(x, y);
addedge(y, x);
}
dfs(1, 0);
depth[rt] = 0;
tp[rt] = 1;
dfs(rt, 0);
dfs3(rt, 0);
dfs2(rt, 0);
F(i, 1, n) writes(ans[i]);
return 0;
} |
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <map>
#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
#include <random>
#include <stack>
#include <set>
#include <unordered_set>
#define bug(x) cout<<"zdongdebug1: "<<x<<endl;
#define bug2(x, y) cout<<"zdongdebug2: "<<x<<" "<<y<<endl;
#define bug3(x, y, z) cout<<"zdongdebug3: "<<x<<" "<<y<<" "<<z<<endl;
using namespace std;
typedef long long ll;
const int maxn = 200005;
const int mod = 1000000007;
int f[maxn];
int pd(int x){
if(x!=f[x])f[x]=pd(f[x]);
return f[x];
}
int a[maxn],b[maxn];
ll c[maxn];
int main() {
#ifdef suiyuan2009
freopen("/Users/suiyuan2009/CLionProjects/icpc/input.txt", "r", stdin);
//freopen("/Users/suiyuan2009/CLionProjects/icpc/output.txt", "w", stdout);
#endif
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)f[i]=i;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=n;i++)cin>>b[i];
for(int i=0;i<m;i++){
int x,y;
cin>>x>>y;
int px = pd(x);
int py = pd(y);
if(px!=py)f[px] = py;
}
memset(c, 0,sizeof(c));
for(int i=1;i<=n;i++){
int x = pd(i);
c[x] +=(a[i]-b[i]);
}
bool sign = 0;
for(int i=1;i<=n;i++)if(c[i]!=0)sign=1;
cout<<(sign?"No":"Yes")<<endl;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
/* // Ordered Set
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T>
typedef tree<T, null_type,less<T>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
#define os_find(k) find_by_order(k)
#define os_order(k) order_of_key(k)
*/
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<long long> vll;
#define f0(i,a,b) for(int i=a;i<b;i++)
#define f1(i,a,b) for(int i=a;i<=b;i++)
#define f2(i,a,b) for(int i=a;i>b;i--)
#define f3(i,a,b) for(int i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
#define cntleadz(x) __builtin_clz(x) // or add ll at the end for long long
#define cnttrailz(x) __builtin_ctx(x)
#define cntpop(x) __builtin_popcount(x)
#define binparity(x) __builtin_parity(x)
#define pb push_back
#define pii pair<int,int>
#define int long long
#define fi first
#define se second
#define mod 1000000007
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define make_graph(k) int x,y; f0(i,0,k){cin>>x>>y; adj[x].pb(y); adj[y].pb(x);}
#define test int t;cin>>t;while(t--)
int binExp(int x,int n)
{
int res=1;
while(n)
{
if(n&1) res=(res*x)%mod;
x=(x*x)%mod;
n>>=1;
}
return res;
}
// int fact[100001];
// int modInv(int i) {return binExp(i,mod-2);}
// int ncr(int n,int r) {return (n>=r?(fact[n]*modInv(fact[r]))%mod*modInv(fact[n-r])%mod:0);}
int a[200001];
int b[200001];
vi adj[200001];
bool vis[200001];
int sum;
void dfs(int v)
{
vis[v] = 1;
sum += a[v] - b[v];
for(auto x : adj[v])
{
if(!vis[x])
{
dfs(x);
}
}
}
signed main()
{
fast
#ifndef ONLINE_JUDGE
freopen("inputf.txt","r",stdin);
freopen("outputf.txt","w",stdout);
#endif
int n,m;
cin>>n>>m;
f1(i,1,n) cin>>a[i];
f1(i,1,n) cin>>b[i];
make_graph(m);
bool yes = 1;
f1(i,1,n)
{
sum = 0;
if(!vis[i])
{
dfs(i);
}
if(sum!=0)
{
yes = 0;
break;
}
}
cout<<(yes?"Yes":"No");
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define per(i, a, b) for (auto i = (b); i-- > (a); )
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) int((x).size())
#define lb(x...) lower_bound(x)
#define ub(x...) upper_bound(x)
template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 1 << 11;
const ll inf = 1e18;
ll d[N][N];
vector<pair<int, int>> g[N];
int n;
void dijkstra(int s, ll d[]) {
fill_n(d, n, inf);
d[s] = 0;
using P = pair<ll, int>;
priority_queue<P, vector<P>, greater<P>> q;
q.push({0, s});
while (!q.empty()) {
auto [d_v, v] = q.top(); q.pop();
if (d_v != d[v]) continue;
for (auto [to, len] : g[v])
if (ckmin(d[to], d[v] + len))
q.push({d[to], to});
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int m; cin >> n >> m;
rep(e, 0, m) {
int u, v, w; cin >> u >> v >> w; --u; --v;
g[u].emplace_back(v, w);
}
rep(u, 0, n) dijkstra(u, d[u]);
rep(u, 0, n) {
ll ans = inf;
for (auto [v, w] : g[u])
ckmin(ans, w + d[v][u]);
cout << (ans < inf ? ans : -1) << '\n';
}
} | #include <bits/stdc++.h>
using namespace std;
template<class C>constexpr int sz(const C&c){return int(c.size());}
using ll=long long;using ld=long double;constexpr const char nl='\n',sp=' ';
vector<int> solve(vector<int> P) {
int N = P.size();
vector<int> ind(N);
vector<bool> done(N - 1, false);
for (int i = 0; i < N; i++) ind[--P[i]] = i;
vector<int> ans;
for (int i = 0; i < N - 1; i++) {
for (int j = ind[i]; j > i; j--) {
if (done[j - 1]) return vector<int>{-1};
done[j - 1] = true;
ans.push_back(j);
swap(ind[P[j - 1]], ind[P[j]]);
swap(P[j - 1], P[j]);
}
}
if (sz(ans) != N - 1) return vector<int>{-1};
return ans;
}
int main() {
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// freopen("err.txt", "w", stderr);
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int N;
cin >> N;
vector<int> A(N);
for (auto &&a : A) cin >> a;
for (auto &&a : solve(A)) cout << a << nl;
return 0;
}
|
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <stdlib.h>
#include <map>
#include <cmath>
#define MOD_P 1000000007
#define MOD_Q 998244353
#define PI 3.14159265358979
#define ll long long
using namespace std;
bool rec(int i, int w, vector<vector<int>> dp, vector<int> a) {
if (i == 0) {
if (w == 0) {
return true;
} else {
return false;
}
}
bool res = false;
if (w >= a[i - 1] && rec(i - 1, w - a[i - 1], dp, a))res = true;
if (rec(i - 1, w, dp, a))res = true;
return dp[i][w] = res;
}
//int main() {
// int n;
// cin >> n;
// vector<int> t(n);
// int sum = 0;
// int left, right;
// for (int i = 0; i < n; i++) {
// cin >> t[i];
// sum += t[i];
// }
//
// left = (sum + 1) / 2;
//
// right = sum;
// int half;
// while (left + 1 != right) {
// half = (right + left) / 2;
// printf("[%d %d %d]", left, right, half);
// vector<vector<int>> dp(n + 1, vector<int>(half + 1, -1));
// if (rec(n, half, dp, t)) {
// printf("can\n");
// right = half;
// } else {
// printf("can't\n");
// left = half;
// }
// }
//
//
//
// printf("%d", right);
//
// return 0;
//}
int main() {
int n, m, a, b;
cin >> n >> m;
vector<vector<int>>edge(n);
for (int i = 0; i < m; i++) {
cin >> a >> b;
a--;
b--;
edge[a].emplace_back(b);
}
vector<int>st;
int now;
int ans = n;
for (int i = 0; i < n; i++) {
vector<bool>first(n, true);
st.push_back(i);
first[i] = false;
//printf("%d:", i + 1);
while (!st.empty()) {
now = st.back();
st.pop_back();
for (int v : edge[now]) {
if (first[v]) {
first[v] = false;
//printf("%d ", v + 1);
st.push_back(v);
ans++;
}
}
}
//printf("\n");
}
printf("%d", ans);
} | #include <iostream>
#include <vector>
#include <deque>
#include <algorithm>
#include <numeric>
#include <string>
#include <cstring>
#include <list>
#include <unordered_set>
#include <tuple>
#include <cmath>
#include <limits>
#include <type_traits>
#include <iomanip>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <set>
#include <bitset>
#include <regex>
#include <random>
#include<bits/stdc++.h>
//#include<atcoder/all>
using namespace std;
//using namespace atcoder;
typedef long long ll;
#define rep(i,n)for(ll i=0;i<n;++i)
#define exout(x) printf("%.12f\n", x)
const double pi = acos(-1.0);
const ll MOD = 1000000007;
const ll INF = 1e18;
const ll MAX_N = 1010101;
//組み合わせの余りを求める
ll fac[MAX_N], finv[MAX_N], inv[MAX_N];
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX_N; i++) {
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
long long COM(ll n, ll k) {
if (n < k) return 0;
if (n < 0 || k < 0) return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
// mod.m での a の逆元 a^ { -1 } を計算する
long long modinv(long long a, long long m) {
long long b = m, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b; swap(a, b);
u -= t * v; swap(u, v);
}
u %= m;
if (u < 0) u += m;
return u;
}
//最大公約数
ll gcd(ll x, ll y) {
return y ? gcd(y, x % y) : x;
}
ll lcm(ll x, ll y) {
if (x == 0 || y == 0)return 0;
return (x / gcd(x, y) * y);
}
//union-find木について
//サイズによる更新をしている
ll par[4010101];
ll rank2[4010101];
void init(ll n) {
rep(i, n) {
par[i] = i;
rank2[i] = 1;
}
}
ll find(ll x) {
if (par[x] == x) {
return x;
}
else {
return par[x] = find(par[x]);
}
}
void unite(ll x, ll y) {
x = find(x);
y = find(y);
if (x == y)return;
if (rank2[x] < rank2[y]) {
par[x] = y;
rank2[y] += rank2[x];
}
else {
par[y] = x;
rank2[x] += rank2[y];
}
}
//セグ木
class segment_tree {
private:
ll sz;
std::vector<ll> seg;
std::vector<ll> lazy;
void push(ll k) {
if (k < sz) {
lazy[k * 2] = max(lazy[k * 2], lazy[k]);
lazy[k * 2 + 1] = max(lazy[k * 2 + 1], lazy[k]);
}
seg[k] = max(seg[k], lazy[k]);
lazy[k] = 0;
}
void update(ll a, ll b, ll x, ll k,ll l, ll r) {
push(k);
if (r <= a || b <= l) return;
if (a <= l && r <= b) {
lazy[k] = x;
push(k);
return;
}
update(a, b, x, k * 2, l, (l + r) >> 1);
update(a, b, x, k * 2 + 1, (l + r) >> 1, r);
seg[k] = max(seg[k * 2], seg[k * 2 + 1]);
}
ll range_max(ll a, ll b, ll k, ll l, ll r) {
push(k);
if (r <= a || b <= l) return 0;
if (a <= l && r <= b) return seg[k];
ll lc = range_max(a, b, k * 2, l, (l + r) >> 1);
ll rc = range_max(a, b, k * 2 + 1, (l + r) >> 1, r);
return max(lc, rc);
}
public:
segment_tree() : sz(0), seg(), lazy() {};
segment_tree(ll N) {
sz = 1;
while (sz < N) {
sz *= 2;
}
seg = std::vector<ll>(sz * 2, 0);
lazy = std::vector<ll>(sz * 2, 0);
}
void update(ll l, ll r, ll x) {
update(l, r, x, 1, 0, sz);
}
ll range_max(ll l, ll r) {
return range_max(l, r, 1, 0, sz);
}
};
ll dx[4] = { 1,-1,0,0 };
ll dy[4] = { 0,0,1,-1 };
ll a[1010][1010];
vector<vector<ll>>to;
bool flag[2020];
ll ans = 0;
void dfs(ll x) {
ans++;
flag[x] = true;
for (auto nx : to[x]) {
if (flag[nx] == false) {
dfs(nx);
}
}
}
//long longしか使わない
//素数は1より大きい
//lower_boundは指定したkey以上の要素の一番左のイテレータをかえす
//upper_boundは指定したkeyより大きい要素の一番左のイテレータをかえす
int main() {
ll n, m;
cin >> n >> m;
to.resize(n);
rep(i, m) {
ll x, y;
cin >> x >> y;
x--, y--;
to[x].emplace_back(y);
}
rep(i, n) {
dfs(i);
rep(j, n) {
flag[j] = false;
}
}
cout << ans << endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
#define all(v) v.begin(),v.end()
vector<vector<ll>> memo;
vector<vector<ll>> g;
ll N,M;
vector<ll> ind;
bool p;
bool dp(ll i,ll j) {
if(memo[i][j]>=0) {
if(memo[i][j]==0) {
return false;
}
else {
return true;
}
}
bool a=false;
ll now=g[i][j];
if(i<ind.size()-1&&ind[i+1]==ind[i]+1) {
ll x=lower_bound(all(g[i+1]),now)-g[i+1].begin();
x--;
if(x>=0) {
a|=dp(i+1,x);
}
if(x<0&&ind[i+1]==N) {
a=true;
}
}
if(ind[i]>0&&i>0&&ind[i-1]==ind[i]-1) {
ll x=lower_bound(all(g[i-1]),now)-g[i-1].begin();
x--;
if(x>=0) {
a|=dp(i-1,x);
}
if(x<0&&ind[i-1]==N) {
a=true;
}
}
if(ind[i]-1==N&&!p) {
a=true;
}
if(ind[i]+1==N&&!p) {
a=true;
}
if(a) {
memo[i][j]=1;
}
else {
memo[i][j]=0;
}
return a;
}
int main() {
cin>>N>>M;
vector<pair<ll,ll>> vec(M);
for(ll i=0;i<M;i++) {
ll X,Y;cin>>X>>Y;
vec[i]=make_pair(Y,X);
}
sort(all(vec));
ind=vector<ll> (0);
for(ll i=0;i<M;i++) {
ll x,y;
tie(x,y)=vec[i];
if(ind.size()==0||ind.back()!=x) {
ind.push_back(x);
}
}
g=vector<vector<ll>> (ind.size(),vector<ll> (0));
memo=vector<vector<ll>> (ind.size(),vector<ll> (0));
ll count=-1;
ll now=-1;
for(ll i=0;i<vec.size();i++) {
if(vec[i].first!=now) {
count++;
now=vec[i].first;
}
g[count].push_back(vec[i].second);
memo[count].push_back(-1);
}
ll ans=1;
p=false;
for(auto x:ind) {
if(x==N) {
p=true;
continue;
}
ans++;
}
for(ll i=0;i<ind.size();i++) {
if(!dp(i,g[i].size()-1)) {
ans--;
}
}
cout<<ans<<endl;
}
| #include<iostream>
#include <string>
#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#define ll long long int
#define ld long double
#define pb push_back
#define endl "\n"
#define mp make_pair
#define sz(v) (int)(v.size())
#define len(s) (ll)(s.length())
#define M 1000000007
#define all(v) v.begin(),v.end()
#define fr(a,b,c) for(ll i=a;i<=b;i+=c)
#define inarr(arr,n); for(ll i=0;i<n;i++) cin >> arr[i];
#define outarr(arr,n); for(ll i=0;i<n;i++) cout<<arr[i]<<" ";
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const ll inf = 9e18;
const ld pi = 2*acos(0.0);
using namespace std;
ll power(ll a,ll n)
{
if(n==0){
return 1;
}
ll p=power(a,n/2) % M;
p = (p * p) % M;
if (n%2==1)
{
p=(p*a)%M;
}
return p;
}
ll gcd(ll a, ll b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
const int N=1e6+5;
vector <ll> sieve(N,0);
void si()
{
sieve[1]=1;
for(int i=2;i<N;i++){
if(sieve[i]==0){
sieve[i]=1;
for(int j=2*i;j<N;j+=i){
sieve[j]=i;
}
}
}
}
int main(){
ios;
ll n,m,t;
cin>>n>>m>>t;
ll prev=0;
ll n1=n;
for(int i=0;i<m;i++){
ll l,r;
cin>>l>>r;
n-=(l-prev);
if(n<=0){
cout<<"No"<<endl;
return 0;
}
n+=r-l;
if(n>n1){
n=n1;
}
prev=r;
}
n-=(t-prev);
if(n<=0){
cout<<"No"<<endl;
return 0;
}
cout<<"Yes"<<endl;
return 0;
} |
#include "bits/stdc++.h"
#define int long long
#define endl '\n'
using namespace std;
typedef long long ll;
typedef long double ld;
#define db(x) cerr << #x << ": " << x << '\n';
#define read(a) int a; cin >> a;
#define reads(s) string s; cin >> s;
#define readb(a, b) int a, b; cin >> a >> b;
#define readc(a, b, c) int a, b, c; cin >> a >> b >> c;
#define readarr(a, n) int a[(n) + 1] = {}; FOR(i, 1, (n)) {cin >> a[i];}
#define readmat(a, n, m) int a[n + 1][m + 1] = {}; FOR(i, 1, n) {FOR(j, 1, m) cin >> a[i][j];}
#define print(a) cout << a << endl;
#define printarr(a, n) FOR (i, 1, n) cout << a[i] << " "; cout << endl;
#define printv(v) for (int i: v) cout << i << " "; cout << endl;
#define printmat(a, n, m) FOR (i, 1, n) {FOR (j, 1, m) cout << a[i][j] << " "; cout << endl;}
#define all(v) v.begin(), v.end()
#define sz(v) (int)(v.size())
#define rz(v, n) v.resize((n) + 1);
#define pb push_back
#define fi first
#define se second
#define vi vector <int>
#define pi pair <int, int>
#define vpi vector <pi>
#define vvi vector <vi>
#define setprec cout << fixed << showpoint << setprecision(20);
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
const ll inf = 1e18;
const ll mod = 998244353;
const ll N = 2e5 + 1;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int power (int a, int b = mod - 2)
{
int res = 1;
while (b > 0) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
int dp[3001][3001] = {}, sum[3001][6001] = {};
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
dp[0][0] = sum[0][0] = 1;
readb(n, k);
FOR (i, 1, n)
{
FORD (j, 2*n, n + 1) sum[i][j] = sum[i - 1][j - 2];
FORD (j, n, 1)
{
dp[i][j] = sum[i][2*j];
if (j >= 2) sum[i][j] = (dp[i][j] + sum[i - 1][j - 2]) % mod;
else sum[i][j] = dp[i][j];
}
}
print(dp[n][k]);
}
| #include <bits/stdc++.h>
using namespace std;
template <typename T>
int len(const T& a) {
return a.size();
}
using ll = long long;
const int MOD = 998244353;
void Ok(int& a) {
if (a >= MOD)
a -= MOD;
}
const int N = 3010;
int n, k;
int dp[N][N << 1];
int main() {
ios_base::sync_with_stdio(false), cin.tie(nullptr);
scanf("%d%d", &n, &k);
dp[0][0] = 1; // empty set
for (int i = 1; i <= n; ++i) { // size of set
for (int j = n; j >= 1; --j) {
Ok(dp[i][j] = dp[i - 1][j - 1] + dp[i][j << 1]);
}
}
printf("%d", dp[n][k]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
int32_t main(){
ios::sync_with_stdio(0),cin.tie(0);
int n,m;
cin >> n >> m;
vector <pair <int,int> > cond[n];
for(int i=0;i<m;i++){
int x,y,z;
cin >> x >> y >> z;
--y;
cond[x-1].push_back({y,z});
}
int dp[(1<<n)]={};
dp[0]=1;
for(int msk=0;msk<(1<<n);msk++){
int s=__builtin_popcount(msk);
int cnt[n]={};
for(int i=0;i<n;i++){
if(msk&(1<<i))cnt[i]++;
}
for(int i=1;i<n;i++)
cnt[i]+=cnt[i-1];
for(int i=0;i<n;i++){
if(msk&(1<<i))continue;
int ok=1;
for(auto [y,z]:cond[s]){
int c=cnt[y];
if(i<=y)c++;
if(c>z)ok=0;
}
if(ok)dp[msk^(1<<i)]+=dp[msk];
}
}
cout << dp[(1<<n)-1];
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define per(i, a, b) for (auto i = (b); i-- > (a); )
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) int((x).size())
#define lb(x...) lower_bound(x)
#define ub(x...) upper_bound(x)
template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m; cin >> n >> m;
vector<vector<pair<int, int>>> constraint(n);
rep(i, 0, m) {
int x, y, z; cin >> x >> y >> z;
constraint[--x].emplace_back(y, z);
}
vector<ll> dp(1 << n);
dp[0] = 1;
rep(S, 0, 1 << n) {
int x = __builtin_popcount(S);
rep(i, 0, n) {
if (S >> i & 1) continue;
int T = S | 1 << i;
bool valid = 1;
for (auto [y, z] : constraint[x])
valid &= __builtin_popcount(T & ((1 << y) - 1)) <= z;
if (valid) dp[T] += dp[S];
}
}
cout << dp.back() << '\n';
} |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+5;
int n;
int g[N], v[N<<1], nxt[N<<1], tot;
int fa[N], son[N];
int dfn[N], sec;
inline void add(int x, int y) {
v[++tot] = y, nxt[tot] = g[x], g[x] = tot;
}
void dfs0(int x, int f, int dis, int &t, int &mn) {
fa[x] = f;
if (dis > mn) t = x, mn = dis;
for (int i = g[x]; i; i = nxt[i]) {
int y = v[i];
if (y == fa[x]) continue;
dfs0(y, x, dis + 1, t, mn);
}
}
void dfs1(int x) {
dfn[x] = ++sec;
for (int i = g[x]; i; i = nxt[i]) {
int y = v[i];
if (y == fa[x] || y == son[x]) continue;
dfs1(y);
sec++;
}
if (son[x]) dfs1(son[x]);
}
int main()
{
scanf("%d", &n);
for (int i = 1; i < n; i++) {
int x, y; scanf("%d%d", &x, &y);
add(x, y), add(y, x);
}
int s, t, mn = -1;
dfs0(1, 0, 0, s, mn);
mn = -1;
dfs0(s, 0, 0, t, mn);
//直径 s -> t
for (int i = t; i != s; i = fa[i]) son[fa[i]] = i;
dfs1(s);
for (int i = 1; i <= n; i++) printf("%d ", dfn[i]);
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 T; cin >> T;
REP(_, T){
int N; cin >> N;
vec p(N); cin >> p;
REP(i, N) p[i]--;
vec ans;
if(N == 2){
if(p[0] == 1) ans.pb(0);
}else{
IFOR(t, 3, N){
int now = -1;
REP(i, N) if(p[i] == t) now = i;
if(now == t) continue;
if(now % 2 != SZ(ans) % 2){
if(now == 0){
ans.pb(1);
swap(p[1], p[2]);
}else{
ans.pb(now - 1);
swap(p[now - 1], p[now]);
now--;
ans.pb(now + 1);
swap(p[now + 1], p[now + 2]);
}
}
while(now != t){
ans.pb(now);
swap(p[now], p[now + 1]);
now++;
}
}
while(p[0] != 0 || p[1] != 1 || p[2] != 2){
if(SZ(ans) % 2 == 0){
ans.pb(0);
swap(p[0], p[1]);
}else{
ans.pb(1);
swap(p[1], p[2]);
}
}
}
Out(SZ(ans));
for(int x: ans) cout << x + 1 << " ";
cout << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()
constexpr ll MOD=1000000007;
constexpr ll INF=2e18;
int main(){
ll k, n, m; cin >> k >> n >> m;
VI a(k); REP(i,k) cin >> a[i];
ll sum=0;
VI b(k);
vector<P> p(k);
REP(i,k){
b[i]=a[i]*m/n;
p[i]={a[i]*m-b[i]*n,i};
sum+=b[i];
}
sort(ALL(p));
REP(i,m-sum){
b[p[k-1-i].second]++;
}
for(auto i:b){
cout << i << " ";
}
cout << endl;
return 0;
} | #include<cstdio>
typedef __int128 ll;
inline ll in();
inline void wr(ll);
inline ll quick_mod(ll,ll);
int main(int argc,char**argv){
register ll n=in();
register bool tp=1;
for(register ll i=1;i<=100&&tp;++i)
for(register ll j=1;j<=100&&tp;++j)
if(quick_mod(3,i)+quick_mod(5,j)==n&&quick_mod(3,i)>0&&quick_mod(5,j)>0)
wr(i),putchar(' '),wr(j),putchar('\n'),tp=0;
if(tp)wr(-1),putchar('\n');
}
inline ll quick_mod(ll x,ll y){
register ll s=1;
while(y){
if(y&1)
if(s*x>s)s=s*x;
else s=0;
x=x*x;y>>=1;
}
return s;
}
inline ll in(){
register char c=getchar();
register ll x=0,f=1;
for(;c<'0'||c>'9';c=getchar())
if(c=='-')f=-1;
for(;c>='0'&&c<='9';c=getchar())
x=(x<<1)+(x<<3)+(c&15);
return x*f;
}
inline void wr(ll x){
if(x<0)putchar('-'),x=-x;
if(x/10)wr(x/10);
putchar(x%10+'0');
} |
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
int MOD;
class mint{
int x;
public:
mint():x(0){}
mint(long long y){ x=y%MOD; if(x<0) x+=MOD; }
mint& operator+=(const mint& m){ x+=m.x; if(x>=MOD) x-=MOD; return *this; }
mint& operator-=(const mint& m){ x-=m.x; if(x< 0) x+=MOD; return *this; }
mint& operator*=(const mint& m){ x=1LL*x*m.x%MOD; return *this; }
mint& operator/=(const mint& m){ return *this*=inverse(m); }
mint operator+(const mint& m)const{ return mint(*this)+=m; }
mint operator-(const mint& m)const{ return mint(*this)-=m; }
mint operator*(const mint& m)const{ return mint(*this)*=m; }
mint operator/(const mint& m)const{ return mint(*this)/=m; }
mint operator-()const{ return mint(-x); }
friend mint inverse(const mint& m){
int a=m.x,b=MOD,u=1,v=0;
while(b>0){ int t=a/b; a-=t*b; swap(a,b); u-=t*v; swap(u,v); }
return u;
}
friend istream& operator>>(istream& is,mint& m){ long long t; is>>t; m=mint(t); return is; }
friend ostream& operator<<(ostream& os,const mint& m){ return os<<m.x; }
int to_int()const{ return x; }
};
mint operator+(long long x,const mint& m){ return mint(x)+m; }
mint operator-(long long x,const mint& m){ return mint(x)-m; }
mint operator*(long long x,const mint& m){ return mint(x)*m; }
mint operator/(long long x,const mint& m){ return mint(x)/m; }
int main(){
int n,k; scanf("%d%d%d",&n,&k,&MOD);
int m=n*(n+1)/2*k;
static mint buf[3000000];
mint* a=&buf[1500000];
a[0]=1;
for(int i=1;i<=n;i++){
// * (1-X^{(k+1)*i})
for(int j=m;j>=(k+1)*i;j--) a[j]-=a[j-(k+1)*i];
// / (1-X^i)
rep(j,m+1-i) a[j+i]+=a[j];
}
for(int x=1;x<=n;x++){
// * (1-X^{n+1-x})
for(int j=m;j>=-m+n+1-x;j--) a[j]-=a[j-(n+1-x)];
// / (1-X^{(k+1)*(n+1-x)})
for(int j=-m;j<=m-(k+1)*(n+1-x);j++) a[j+(k+1)*(n+1-x)]+=a[j];
if(x==1){
// * k+1
for(int j=-m;j<=m;j++) a[j]*=k+1;
}
if(x>=2){
// * X^{-k*(x-1)}
for(int j=-m;j<=m-k*(x-1);j++) a[j]=a[j+k*(x-1)];
// * (1-X^{(k+1)*(x-1)})
for(int j=m;j>=-m+(k+1)*(x-1);j--) a[j]-=a[j-(k+1)*(x-1)];
// / (1-X^(x-1))
for(int j=-m;j<=m-(x-1);j++) a[j+(x-1)]+=a[j];
}
cout<<a[0]-1<<'\n';
//printf("x = %d:\n",x); for(int i=-6;i<=6;i++) printf("a[%d] = %d\n",i,a[i].to_int());
}
return 0;
}
| /// kazuki08
/*
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
*/
#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 int long long
typedef long long ll;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef vector<pi> vpi;
#define ar array
//#define inf 1000000000
// #define mod 1000000007
#define dmp(x) cerr<<"line "<<__LINE__<<" "<<#x<<":"<<x<<endl
#define fs first
#define sc second
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define mt make_tuple
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
o<<"{";
for(const T& v:vc) o<<v<<",";
o<<"}";
return o;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MAXN = 102, MAXM = 560000;
int n,k,mod,dp[MAXN][MAXM];
signed main(){
IOS;
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
cin >> n >> k >> mod;
dp[0][0]=1;
for(int i=1;i<=n;i++){
vi presum(i+1,0);
for(int j=0;j<MAXM;j++){
int rem = j%i;
presum[rem] = (presum[rem]+dp[i-1][j])%mod;
if(j-(k+1)*i>=0){
presum[rem] = (presum[rem]-dp[i-1][j-(k+1)*i]+mod)%mod;
}
dp[i][j] = presum[rem];
}
}
for(int i=1;i<=n;i++){
int ans = 0;
for(int j=0;j<MAXM;j++){
ans = (ans+dp[i-1][j]*dp[n-i][j]%mod)%mod;
}
ans = (ans*(k+1)%mod - 1 + mod)%mod;
cout << ans << endl;
}
return 0;
}
///....
|
constexpr bool isDebugMode = false;
int testcase = 1;
#include <bits/stdc++.h>
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
using namespace std;
typedef long long ll;
typedef pair<long long, long long> P;
struct edge{long long to,cost;};
const int inf = 1 << 27;
const long long INF = 1LL << 60;
const int COMBMAX = 1001001;
const long long MOD = 1000000007; //998244353;
const long long dy[] = {-1, 0, 0, 1};
const long long dx[] = {0, -1, 1, 0};
const string abc = "abcdefghijklmnopqrstuvwxyz";
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define eachdo(v, e) for (const auto &e : (v))
#define all(v) (v).begin(), (v).end()
#define lower_index(v, e) (long long)distance((v).begin(), lower_bound((v).begin(), (v).end(), e))
#define upper_index(v, e) (long long)distance((v).begin(), upper_bound((v).begin(), (v).end(), e))
long long mpow(long long a, long long n, long long mod = MOD){long long res = 1; while(n > 0){if(n & 1)res = res * a % mod; a = a * a % mod; n >>= 1;} return res;}
void pt(){cout << endl; return;}
template<class Head> void pt(Head&& head){cout << head; pt(); return;}
template<class Head, class... Tail> void pt(Head&& head, Tail&&... tail){cout << head << " "; pt(forward<Tail>(tail)...);}
void dpt(){if(!isDebugMode) return; cout << endl; return;}
template<class Head> void dpt(Head&& head){if(!isDebugMode) return; cout << head; pt(); return;}
template<class Head, class... Tail> void dpt(Head&& head, Tail&&... tail){if(!isDebugMode) return; cout << head << " "; pt(forward<Tail>(tail)...);}
template<class T> void enu(T v){rep(i, v.size()) cout << v[i] << " " ; cout << endl;}
template<class T> void enu2(T v){rep(i, v.size()){rep(j, v[i].size()) cout << v[i][j] << " " ; cout << endl;}}
template<class T> void debug(T v){if(!isDebugMode) return; rep(i, v.size()) cout << v[i] << " " ; cout << endl;}
template<class T> void debug2(T v){if(!isDebugMode) return; rep(i, v.size()){rep(j, v[i].size()) cout << v[i][j] << " " ; cout << endl;}}
template<class T1, class T2> inline bool chmin(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}
template<class T1, class T2> inline bool chmax(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}
template<class T1, class T2> long long recgcd(T1 a, T2 b){return a % b ? recgcd(b, a % b) : b;}
bool valid(long long H, long long W, long long h, long long w) { return 0 <= h && h < H && 0 <= w && w < W; }
void solve();
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
// cin >> testcase;
while(testcase--) solve();
return 0;
}
void solve(){
ll N, M; cin >> N >> M;
vector<ll> h(N);
vector<ll> w(M);
rep(i, N) cin >> h[i];
rep(i, M) cin >> w[i];
sort(all(h));
vector<ll> head(N / 2);
vector<ll> tail(N / 2);
ll hn = N / 2;
rep(i, hn){
head[i] = abs(h[2 * i] - h[2 * i + 1]);
tail[i] = abs(h[N - 1 - 2 * i] - h[N - 2 - 2 * i]);
}
vector<ll> shead(hn + 1);
vector<ll> stail(hn + 1);
rep(i, hn){
shead[i + 1] = shead[i] + head[i];
stail[i + 1] = stail[i] + tail[i];
}
// enu(shead);
// enu(stail);
ll ans = INF;
rep(i, M){
ll ins = lower_index(h, w[i]);
if(ins % 2 == 0){
chmin(ans, shead[ins / 2] + abs(w[i] - h[ins]) + stail[N / 2 - ins / 2]);
}else{
chmin(ans, shead[ins / 2] + abs(w[i] - h[ins - 1]) + stail[N / 2 - ins / 2]);
}
}
pt(ans);
} | #include <bits/stdc++.h>
using namespace std;
#define ll int64_t
#define ull uint64_t
#define vi vector<int>
#define vll vector<ll>
#define vull vector<ull>
#define vvi vector<vector<int>>
#define FOR(i, n) for (int i = 0; i < n; i++)
#define CONCAT_INNER(a, b) a ## b
#define CONCAT(a, b) CONCAT_INNER(a, b)
#define REP(n) FOR(CONCAT(_i, __COUNTER__), n)
#define pb push_back
#define ALL(x) x.begin(), x.end()
#define SORT(x) sort(ALL(x))
#define IN(x, l, r) (l < x && x < r)
#define MOD 1000000007
#define EPS 10e-9
#define ii pair<int, int>
#define DIR4 {{0,1}, {1,0}, {0,-1}, {-1,0}}
#define DIR8 {{0,1}, {0,-1}, {1,0}, {-1,0}, {-1,-1}, {-1,1}, {1,-1}, {1,1}}
#define fst(x) (x).first
#define snd(x) (x).second
#define SZ(x) ((int) (x).size())
#define inc(c, v) (c.find(v) != c.end())
#define SKIP(c) if (c) continue
#define MODM(n, m) ((n) % m + m) % m
#define LOG2I(n) (31 - __builtin_clz(n))
void test_case() {
int x, y;
cin >> x >> y;
if (x == y) cout << x << endl;
else cout << 3 - x - y << endl;
}
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(0);
// cout << fixed << setprecision(6);
int t = 1;
// cin >> t;
FOR(i, t) {
test_case();
}
}
|
#include<bits/stdc++.h>
#define rep(i,n) for ( int i=0; i< (n); ++i )
#define mod(x,m) ( (x%m+m)%m )
using namespace std;
using ll = long long;
using P = pair<int,int>;
int main(){
int h,w;
cin >> h >> w;
vector<string> s(h);
rep(i,h) cin >> s[i];
int count = 0;
rep(i,h){
int len = 0;
rep(j,w){
if(s[i][j]=='#' ){
count += max(0,len-1);
len = 0;
}else{
++len;
if(j==w-1) count += max(0,len-1);
}
}
}
rep(i,w){
int len = 0;
rep(j,h){
if(s[j][i]=='#' ){
count += max(0,len-1);
len = 0;
}else{
++len;
if(j==h-1) count += max(0,len-1);
}
}
}
cout << count << endl;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
long long a[2000005],sum=0,mx=105;
int main(){
long long n,m;
cin>>n>>m;
for(int i=0;i<n*m;i++){
cin>>a[i];
if(a[i]<mx){
mx=a[i];
}
}
for(int i=0;i<m*n;i++){
sum=sum+a[i]-mx;
}
cout<<sum;
}
|
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define chmin(x,y) x = min((x),(y));
#define chmax(x,y) x = max((x),(y));
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
const int INF = 1e9;
const long long LINF = 1e17;
const int MOD = 1000000007;
//const int MOD = 998244353;
const double PI = 3.14159265358979323846;
int main(){
ll n,k;
cin >> n >> k;
vector<ll> cnt(n,0);
rep(i,n){
ll a;
cin >> a;
cnt[a] ++;
}
ll p = cnt[0];
for(ll i=1;i<n;i++){
cnt[i] = min(cnt[i],p);
p = cnt[i];
}
vector<ll> ans(k,0);
rep(i,n){
rep(j,cnt[i]){
if(j>=k) break;
ans[j] = i+1;
}
}
ll res = accumulate(ans.begin(),ans.end(),0);
cout << res << endl;
return 0;
} | #include<bits/stdc++.h>
#define maxn 300010
#define ll long long
using namespace std;
ll n,m,now,ans,sum;
ll a[maxn],nm[maxn];
struct nod{
ll pos,num;
};
bool operator < (nod aa,nod bb){
return aa.num==bb.num?aa.pos> bb.pos:aa.num>bb.num;
}
priority_queue <nod> q;
int main(){
scanf("%lld%lld",&n,&m);
for(ll i=1;i<=n;++i) scanf("%lld",&a[i]),nm[a[i]]++;
sort(a+1,a+n+1);
for(ll i=0;i<=n;++i)
q.push((nod){i,nm[i]});
if(a[1]==0) {
now=1;
for(ll i=2;i<=n;++i){
if(a[i]==a[i-1]||a[i]==a[i-1]+1) now=a[i]+1;else break;
}
}
while(m&&!q.empty()){
nod v=q.top();
q.pop();
if(v.num==0) continue;
if(v.pos>now) continue;
ans+=now*min(m,v.num-sum);
m-=min(m,v.num-sum);
sum=v.num;
now=v.pos;
}
cout<<ans;
return 0;
}
/*
12 4
0 1 2 3 4 0 1 2 0 1 1 0
*/ |
#include <bits/stdc++.h>
#define ll long long
#define sz(x) (int)(x).size()
using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//uniform_int_distribution<int>(1000,10000)(rng)
ll binpow(ll a, ll b)
{
ll res = 1;
while (b > 0)
{
if (b & 1)
res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
ll gcd(ll a,ll b)
{
if (b==0) return a;
return gcd(b,a%b);
}
string to_upper(string a)
{
for (int i=0;i<(int)a.size();++i) if (a[i]>='a' && a[i]<='z') a[i]-='a'-'A';
return a;
}
string to_lower(string a)
{
for (int i=0;i<(int)a.size();++i) if (a[i]>='A' && a[i]<='Z') a[i]+='a'-'A';
return a;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
ll n,ans=0;
cin>>n;
for (ll i=999,c=1;i<n;i=i*1000+999,++c)
ans+=((min({i*1000+999,n})-i)*c);
cout<<ans;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main(){
long long n; cin >> n;
vector<long long> vec;
for(int i=1; i <= sqrt(n); i++){
if(n % i == 0){
if(n /i == i){
vec.push_back(i);
}
else{
vec.push_back(i);
vec.push_back(n/i);
}
}
}
sort(vec.begin(), vec.end());
for(auto x : vec){
cout << x << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
cin.tie(0)->sync_with_stdio(0);
int n, m;
cin >> n >> m;
vector<pair<int, int>> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i].first;
a[i].second = i;
}
sort(begin(a), end(a));
map<int, int> mp;
for (const auto& [v, i] : a) {
mp[i] = v;
}
vector<vector<int>> g(n);
while (m--) {
int x, y;
cin >> x >> y;
--x, --y;
g[x].push_back(y);
}
int ans = -(1 << 30);
vector<bool> seen(n, false);
for (const auto& [v, i] : a) {
queue<int> q;
q.push(i);
while (!q.empty()) {
int p = q.front();
q.pop();
for (const auto& ni : g[p]) {
if (seen[ni]) continue;
ans = max(ans, mp[ni] - v);
q.push(ni);
seen[ni] = true;
}
}
}
cout << ans << '\n';
}
| #include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long li;
#define rep(i,n) for(int i=0;i<(n);i++)
#define df 0
template<class T> void print(const T& t){ cout << t << "\n"; }
template<class T, class... Ts> void print(const T& t, const Ts&... ts) { cout << t; if (sizeof...(ts)) cout << " "; print(ts...); }
// Container コンテナ型, map以外
template< template<class ...> class Ctn,class T>
std::ostream& operator<<(std::ostream& os,const Ctn<T>& v){
auto itr=v.begin();
while(itr!=v.end()){
if(itr!=v.begin())cout << " ";
cout << *(itr++);
}
return os;
};
int main(){
int n,m; cin >>n >>m;
vector<li> a(n);
rep(i,n) cin >>a[i];
vector<vector<int>> adj(n);
rep(i,m) {
int x,y; cin >>x >>y; x-- ; y--;
adj[x].push_back(y);
}
li s=0; for(li x:a) s=max(s,x);
if(df)print("s",s);
vector<li> dp(n,s*2);
rep(i,n){
for(int x:adj[i]){
dp[x]=min({dp[x],a[i],dp[i]});
}
}
if(df)print(dp);
li ans=-s;
rep(x,n){
ans=max(ans,a[x]-dp[x]);
}
print(ans);
}
|
/*
author : seryu
title : ABC180B_"Various distances"
*/
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < n; i++)
#define reps(i,n) for(int i = 1; i <= n; i++)
#define rrep(i,n) for(int i = n-1; i >= 0; i--)
#define rreps(i,n) for(int i = n; i >= 1; i--)
#define mrep(i,j,n) for(int i = j; i < n; i++)
#define mreps(i,j,n) for(int i = j; i <= n; i++)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define ERASE(x,val) x.erase(remove(all(x), val), x.end())
#define MOD 1000000007
typedef long long ll;
typedef pair<int, int> P;
template<class T>inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; }return false; }
template<class T>inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; }return false; }
void solve()
{
int n;
cin >> n;
double man = 0, euc = 0, che = 0;
rep(i, n)
{
double x;
cin >> x;
man += abs(x);
euc += x*x;
chmax(che, abs(x));
}
euc = pow(euc, 0.5);
cout << man << endl;
cout << euc << endl;
cout << che << endl;
return;
}
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
solve();
return 0;
}
| #include <cstdio>
#include <algorithm>
const int N = 2e5 + 5;
int a[N];
int b[N];
int ans[N];
int main() {
//#ifndef ONLINE_JUDGE
//freopen("in", "r", stdin);
//#endif
int n;
while (~scanf("%d", &n)) {
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
b[a[i]] = i;
}
int cnt = 0;
for (int i = n; i >= 1; --i) {
for (int k = b[i]; k < i; ++k) {
std::swap(a[k], a[k + 1]);
std::swap(b[a[k]], b[a[k + 1]]);
ans[cnt++] = k;
if (cnt >= n - 1) {
break;
}
}
if (cnt >= n - 1) {
break;
}
}
if (std::is_sorted(a, a +n) && cnt == n - 1) {
for (int i = 0; i < cnt; ++i) {
printf("%d\n", ans[i]);
}
} else {
printf("-1\n");
}
}
return 0;
}
|
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <iomanip>
using namespace std;
#define rep(i, n) for(long long i = 0; i < (long long)(n); i++)
#define rrep(i, n) for(long long i = (long long)(n - 1); i >= 0; i--)
#define all(x) (x).begin(),(x).end()
using ll = long long;
using ull = unsigned long long;
int main()
{
int n;
cin >> n;
double x0, y0, x2, y2;
cin >> x0 >> y0 >> x2 >> y2;
const double PI = 3.14159265358979323846;
double cx = (x2 + x0) / 2.0, cy = (y2 + y0) / 2.0;
double l = sqrt(pow(cx - x0, 2) + pow(cy - y0, 2));
auto dir = atan2(y0 - cy, x0 - cx);
dir += 2 * PI / n;
cout << fixed << setprecision(30);
cout << cx + cos(dir) * l << " " << cy + sin(dir) * l << endl;
return 0;
}
| #define DEBUG 0
#include <bits/stdc++.h>
using namespace std;
#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
template<typename T>
_Debug& operator,(T val) {
cout << val << endl;
return *this;
}
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
#define MOD 998244353
int W[100],fact[1000];
int dp[2][10001][101];
int main() {
int i;
int N;
scanf("%d",&N);
for (i = 0; i < N; i++) scanf("%d",&W[i]);
int j,k;
int sum = 0;
dp[1][0][0] = 1;
fact[0] = 1;
for (i = 1; i < 1000; i++) fact[i] = ((LLI) i*fact[i-1]) % MOD;
for (i = 0; i < N; i++) {
int p = i & 1;
sum += W[i];
for (j = 0; j <= sum; j++) {
for (k = 0; k <= i+1; k++) {
dp[p][j][k] = dp[!p][j][k];
if ((j-W[i] >= 0) && (k > 0)) {
dp[p][j][k] += dp[!p][j-W[i]][k-1];
if (dp[p][j][k] >= MOD) dp[p][j][k] -= MOD;
}
}
}
}
LLI ans = 0;
if (!(sum & 1)) {
i = sum/2;
for (j = 0; j <= N; j++) {
ans += (((LLI) dp[(N-1) & 1][i][j]*fact[j]) % MOD)*fact[N-j];
ans %= MOD;
}
}
cout << ans << endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define SZ(x) (int)x.size()
#define F first
#define S second
#define ALL(x) x.begin(), x.end()
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define FOR(i, a, b) for(int i = a; i < b; ++i)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll INF = 1e18;
const int MOD = 998244353;
const ld PI = acosl(-1.0);
const ld EPS = 1e-9;
#define int long long
const int N = 51;
int adj[N][N], arr[N][N];
int vis[N];
int n, k;
ll f[N];
void dfs(int node, int &cnt) {
if(vis[node]) return;
vis[node] = 1;
cnt++;
FOR(nxt, 1, n+1) {
if(adj[node][nxt]) {
dfs(nxt, cnt);
}
}
}
void solve() {
cin >> n >> k;
FOR(i, 1, n+1) FOR(j, 1, n+1) cin >> arr[i][j];
f[0] = 1;
FOR(i, 1, n+1) f[i] = (f[i-1] * i) % MOD;
ll ans = 1;
FOR(i, 1, n+1) FOR(j, 1, n+1) adj[i][j] = 0;
FOR(i, 1, n+1) vis[i] = 0;
FOR(x, 1, n+1) {
FOR(y, x+1, n+1) {
bool ok = true;
FOR(r, 1, n+1) {
if(arr[r][x] + arr[r][y] > k) {
ok = false;
break;
}
}
if(ok) {
adj[x][y] = 1;
adj[y][x] = 1;
}
}
}
ll cur = 1;
FOR(i, 1, n+1) {
if(!vis[i]) {
int cnt = 0;
dfs(i, cnt);
cur = (cur * f[cnt]) % MOD;
}
}
ans = (ans * cur) % MOD;
FOR(i, 1, n+1) FOR(j, 1, n+1) adj[i][j] = 0;
FOR(i, 1, n+1) vis[i] = 0;
FOR(x, 1, n+1) {
FOR(y, x+1, n+1) {
bool ok = true;
FOR(c, 1, n+1) {
if(arr[x][c] + arr[y][c] > k) {
ok = false;
break;
}
}
if(ok) {
adj[x][y] = 1;
adj[y][x] = 1;
}
}
}
cur = 1;
FOR(i, 1, n+1) {
if(!vis[i]) {
int cnt = 0;
dfs(i, cnt);
cur = (cur * f[cnt]) % MOD;
}
}
ans = (ans * cur) % MOD;
cout << ans << endl;
}
signed main() {
fast;
int T = 1;
// cin >> T;
while(T--) {
solve();
}
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(ll i=0,endrep=(n); i<endrep; ++i)
#define rep1(i,n) for(ll i=1,endrep=(n); i<=endrep; ++i)
#define revrep(i,n) for(ll i=(n)-1; i>=0; --i)
inline constexpr ll Inf = (1ULL << 60) -1;
#define fastio cin.tie(nullptr); ios_base::sync_with_stdio(false);
#define newl '\n'
#define YN(e) ((e)?"Yes":"No")
#define all(a) begin(a),end(a)
#define rall(a) rbegin(a),rend(a)
template <class T,class U> bool updmax(T& a, U b) { if(b>a){ a=b; return true;} return false;}
template <class T,class U> bool updmin(T& a, U b) { if(b<a){ a=b; return true;} return false;}
inline constexpr int Mod = 1000000007;
//inline constexpr int Mod = 998244353;
#define dbg(a) cout << #a << ": " << a << endl;
#define dbg1(a,n) cout<<#a<<": "; rep(i,n) cout<<a[i]<<" "; cout<<endl;
#define dbg2(m,h,w) cout<<#m<<":"<<endl; rep(i,h){ rep(j,w)cout<<m[i][j]<<" "; cout<<endl; }
int rng(int n) { return rand()/(RAND_MAX+1.0)*n; }
//bool callback(int from, int to)
template <class Graph, class Callback>
void bfs(Graph& g, int start, Callback callback) {
vector<bool> visited(g.size());
queue<int> q;
q.push(start);
visited[start] = true;
while (!q.empty()) {
auto v = q.front(); q.pop();
for (auto u : g[v]) {
if (visited[u]) continue;
visited[u] = true;
if (callback(v,u)) q.push(u);
}
}
}
ll dp[1<<17][17];
int main() {
fastio;
ll ans=Inf;
ll N,M,K;
cin >> N >> M;
vector<vector<int>> g(N);
rep(i,M) {
int a,b;
cin >> a >> b, --a,--b;
g[a].push_back(b);
g[b].push_back(a);
}
cin >> K;
vector<ll> c(K);
rep(i,K) cin >> c[i], --c[i];
ll d2[17][17]{};
rep(i,K) rep(j,K) d2[i][j] = Inf;
rep(i,K) {
vector<ll> d(N, Inf);
d[c[i]] = 0;
bfs(g, c[i], [&](int from, int to){
d[to] = 1 + d[from];
return true;
});
rep(j,K) if (i != j) d2[i][j] = d[c[j]];
}
rep(i,1<<K) rep(j,K) dp[i][j] = Inf;
rep(i,K) dp[1<<i][i] = 0;
rep(p,1<<K) rep(i,K) {
if (!(p>>i&1)) continue;
rep(j,K) {
if (p>>j&1) continue;
updmin(dp[p|(1<<j)][j], dp[p][i] + d2[i][j]);
}
}
rep(i,K) updmin(ans, dp[(1<<K)-1][i]);
if (ans == Inf) ans = -1;
else ans++;
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#define M 1000000007
#define U 998244353
#define N 1000005
#define int long long
#define sz(c) (int)c.size()
#define fr first
#define ll long long
#define sc second
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define rep(i,a,n) for(int i=a ; i<n ; i++)
#define r0 return 0;
#define endl '\n'
#define INF (int)1e15
/* Debug Begins */
# define trace(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); \
stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
string to_string(char c) { return "'" + string(1, c) + "'";}
string to_string(string s) { return '"' + s + '"';}
string to_string(bool f) { if(f) return "True"; else return "False";}
string to_string(const char* s) { return to_string((string) s);}
template<typename A> string to_string(A);
template<typename A, typename B> string to_string(pair<A, B> p){
return "(" + to_string(p.first) + ": " + to_string(p.second) + ")";}
template<typename A> string to_string(A v) {bool f = false; string r = "{";
for (auto x: v) {if (f) r += ", "; r += to_string(x); f = true;} return r += "}";}
template<typename A> string to_string(vector<vector<A>> v) {string r;
for (auto x: v) r += "\n" + to_string(x); return r;}
int Nerr;
template<typename A> string to_string(A *p) {return to_string(vector<A>(p, p + Nerr));}
void err(istream_iterator<string>) { cerr << endl; }
template<typename T,typename... Args> void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << to_string(a) << "; "; err(++it, args...); }
template<typename T> void kek(T ans) {cout << ans << endl; exit(0);}
#define Lu(...) [&] (auto &&u) { return __VA_ARGS__; }
#define Luv(...) [&] (auto &&u, auto &&v) { return __VA_ARGS__; }
/***************************************************************/
bool sortbysec(const pair<int,int> &a,
const pair<int,int> &b)
{
return (a.second < b.second);
}
signed main()
{
ios_base::sync_with_stdio(0);
int TESTS=1;
// cin>>TESTS;
while(TESTS--)
{
int ans = 0;
int n;
cin >> n;
vector<int> a(n);
rep(i,0,n) cin >> a[i];
rep(i,0,n) ans += (n-1)*a[i]*a[i];
// trace(ans);
int sum = 0;
rep(i,0,n) sum += a[i];
// trace(sum);
rep(i,0,n){
ans -= a[i]*(sum - a[i]);
// trace(a[i]*(sum-a[i]))
}
cout << ans << endl;
}
} | #include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
int main(void){
int n;
cin >> n;
vector<ll> cnt_dummy(401);
decltype(cnt_dummy)::iterator cnt = cnt_dummy.begin() + 200;
rep (i, n) {
int a;
cin >> a;
cnt[a]++;
}
ll ans = 0;
for (ll i = -200; i <= 199; i++) {
for (ll j = i+1; j <= 200; j++) {
ans += (j-i) * (j-i) * cnt[i] * cnt[j];
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
#define FASTIO
using namespace std;
using ll = long long;
using Vi = std::vector<int>;
using Vl = std::vector<ll>;
using Pii = std::pair<int, int>;
using Pll = std::pair<ll, ll>;
constexpr int I_INF = std::numeric_limits<int>::max();
constexpr ll L_INF = std::numeric_limits<ll>::max();
template <typename T1, typename T2>
inline bool chmin(T1& a, const T2& b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
inline bool chmax(T1& a, const T2& b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <std::ostream& os = std::cout>
class Prints {
private:
class __Prints {
public:
__Prints(const char* sep, const char* term) : sep(sep), term(term) {}
template <class... Args>
auto operator()(const Args&... args) const -> decltype((os << ... << std::declval<Args>()), void()) { print(args...); }
template <typename T>
auto pvec(const T& vec, size_t sz) const -> decltype(os << std::declval<decltype(std::declval<T>()[0])>(), void()) {
for (size_t i = 0; i < sz; i++)
os << vec[i] << (i == sz - 1 ? term : sep);
}
template <typename T>
auto pmat(const T& mat, size_t h, size_t w) -> decltype(os << std::declval<decltype(std::declval<T>()[0][0])>(), void()) {
for (size_t i = 0; i < h; i++)
for (size_t j = 0; j < w; j++)
os << mat[i][j] << (j == w - 1 ? term : sep);
}
private:
const char *sep, *term;
void print() const { os << term; }
void print_rest() const { os << term; }
template <class T, class... Tail>
void print(const T& head, const Tail&... tail) const { os << head, print_rest(tail...); }
template <class T, class... Tail>
void print_rest(const T& head, const Tail&... tail) const { os << sep << head, print_rest(tail...); }
};
public:
Prints() {}
__Prints operator()(const char* sep = " ", const char* term = "\n") const { return __Prints(sep, term); }
};
Prints<> prints;
Prints<std::cerr> prints_err;
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void solve() {
int N;
cin >> N;
Vl A(N), B(N), P(N), invP(N);
for (ll i = 0; i < N; i++) {
cin >> A[i];
}
for (ll i = 0; i < N; i++) {
cin >> B[i];
}
for (ll i = 0; i < N; i++) {
int p;
cin >> p;
--p;
P[i] = p;
invP[p] = i;
}
Vi ord(N);
iota(ord.begin(), ord.end(), 0);
sort(ord.begin(), ord.end(), [&](int l, int r) { return A[l] < A[r]; });
vector<Pii> ans;
for (ll i = 0; i < N; i++) {
int u = ord[i];
int to = invP[u];
if (u == to) continue;
if (A[u] <= B[P[u]] || A[to] <= B[P[to]]) {
prints()(-1);
return;
}
int t = P[u];
ans.emplace_back(u, to);
P[u] = u;
P[to] = t;
invP[u] = u;
invP[t] = to;
}
prints()(ans.size());
for (auto [a, b] : ans) {
prints()(a + 1, b + 1);
}
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
int main() {
#ifdef FASTIO
std::cin.tie(nullptr), std::cout.tie(nullptr);
std::ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
std::ifstream ifs("./in_out/input.txt");
std::cin.rdbuf(ifs.rdbuf());
#endif
#ifdef FILEOUTPUT
std::ofstream ofs("./in_out/output.txt");
std::cout.rdbuf(ofs.rdbuf());
#endif
std::cout << std::setprecision(18) << std::fixed;
solve();
std::cout << std::flush;
return 0;
} | #include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double db;
#define pll pair<ll,ll>
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define ub(v,val) upper_bound(v.begin(),v.end(),val)
#define np(str) next_permutation(str.begin(),str.end())
#define lb(v,val) lower_bound(v.begin(),v.end(),val)
#define sortv(vec) sort(vec.begin(),vec.end())
#define rev(p) reverse(p.begin(),p.end());
#define v vector
#define pi 3.14159265358979323846264338327950288419716939937510
#define len length()
#define repc(i,s,e) for(ll i=s;i<e;i++)
#define fi first
#define se second
#define mset(a,val) memset(a,val,sizeof(a));
#define mt make_tuple
#define repr(i,n) for(i=n-1;i>=0;i--)
#define rep(i,n) for(ll i=0;i<n;i++)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const ll M = 1e9 + 7 ;
ll inf = 1e18;
ll begtime = clock();
#define time() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll n, m;
// modular exponentiation
ll binpow(ll val, ll deg)
{
if (deg < 0)
return 0;
if (!deg)
return 1 % M;
if (deg & 1)
return binpow(val, deg - 1) * val % M;
ll res = binpow(val, deg >> 1);
return (res * res) % M;
}
//binomial
ll modinv(ll n) {
return binpow(n, M - 2);
}
//GCD
ll gcd (ll a, ll b) {
if (b == 0)
return a;
else
return gcd (b, a % b);
}
int main() {
// your code goes here
IOS;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll i, j, t, k, x, y, z, N;
cin >> n;
db a[n];
rep(i, n) cin >> a[i];
db d1 = 0, d2 = 0, d3 = 0;
rep(i, n) {
d1 += abs(a[i]);
d2 += pow(a[i], 2);
d3 = max(d3, abs(a[i]));
}
cout << fixed << setprecision(10) << d1 << "\n" << pow(d2, 0.5) << "\n" << d3;
#ifndef ONLINE_JUDGE
time();
#endif
return 0;
}
|
#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, a, b) for (int i = (int)(a); i >= (int)b; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define per(i, r, l) for (int i = (r); i >= (l); i--)
#define ms(x, y) memset(x, y, sizeof(x))
#define SZ(x) int(x.size())
#define fk cerr<<"fk"<<endl
#define db(x) cerr<<(#x)<<'='<<(x)<<endl
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;
template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }
//1.integer overflow (1e5 * 1e5) (2e9 + 2e9)
//2.runtime error
//3.boundary condition
const int N=(int)1e3+100;
int n,m,a[N],b[N],dp[N][N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.precision(10);
cout<<fixed;
#ifdef LOCAL_DEFINE
freopen("input.txt", "r", stdin);
#endif
cin>>n>>m;
forn(i, n) cin>>a[i];
forn(i, m) cin>>b[i];
ms(dp,0x3f);
dp[0][0]=0;
for(int i=0; i<=n; ++i){
for(int j=0; j<=m; ++j){
if(i+1<=n) uin(dp[i+1][j],dp[i][j]+1);
if(j+1<=m) uin(dp[i][j+1],dp[i][j]+1);
if(i+1<=n && j+1<=m){
uin(dp[i+1][j+1],dp[i][j]+(a[i]!=b[j]));
}
}
}
cout<<dp[n][m]<<'\n';
#ifdef LOCAL_DEFINE
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
} | #include<bits//stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define req(i,n) for(int i = 1;i <= n; i++)
#define rrep(i,n) for(ll i = n-1;i >= 0;i--)
#define ALL(obj) begin(obj), end(obj)
#define RALL(a) rbegin(a),rend(a)
typedef long long int ll;
typedef long double ld;
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 ll INF = 1e18;
ll mod = 998244353;
ll fanc(int n,int k){
return min(k-1,2*n+1-k);
}
int main(){
int n,k;cin >>n>>k;
ll sum = 0;
for(int i = max(2,2+k);i<=min(2*n,2*n+k);i++){
sum += fanc(n,i)*fanc(n,i-k);
}cout << sum <<endl;
} |
#pragma GCC optimize ("O2")
#pragma GCC target ("avx2")
//#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please
const int CM = 1 << 17, CL = 12;
char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint() {
if (ci - owa > 0) {
memcpy(cn, owa, CL);
ci -= CM;
fread(cn + CL, 1, CM, stdin);
}
ll tmp = *(ll*)ci;
int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0);
tmp = tmp << dig & ma1;
tmp = tmp * 10 + (tmp >> 8) & ma2;
tmp = tmp * 100 + (tmp >> 16) & ma3;
tmp = tmp * 10000 + (tmp >> 32) & ma4;
ci += 72 - dig >> 3;
return tmp;
}
const int MAX = 1000;
class shuturyoku_unko {
public:
char C[MAX * 4];
constexpr shuturyoku_unko() : C() {
rep(i, MAX) {
int X = i;
rep(j, 3) {
C[i * 4 + 2 - j] = '0' + X % 10;
X /= 10;
}
C[i * 4 + 3] = ' ';
}
}
};
constexpr shuturyoku_unko f;
const int dm = 1 << 17;
char* dn = cn, * di = dn, * owad = dn + dm - 20;
void putint(int A) {
if (owad < di) {
fwrite(dn, 1, di - dn, stdout);
di = dn;
}
if (A >= 1000) {
int dig = 1;
if (A >= 100000) dig = 3;
else if (A >= 10000) dig = 2;
memcpy(di, f.C + A / 1000 * 4 + 3 - dig, dig);
memcpy(di + dig, f.C + A % 1000 * 4, 4);
di += dig + 4;
}
else {
int dig = 1;
if (A >= 100) dig = 3;
else if (A >= 10) dig = 2;
memcpy(di, f.C + A * 4 + 3 - dig, dig + 1);
di += dig + 1;
}
}
int N;
int to[400001], ne[400001], he[200001];
int Q[200001], vis[200001], p, q;
ll are[200001];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
N = getint();
rep1(i, N * 2 - 2) {
int a = getint(), b = getint();
to[i] = b;
ne[i] = he[a];
he[a] = i++;
to[i] = a;
ne[i] = he[b];
he[b] = i;
}
Q[q++] = 1;
while (p < q) {
int u = Q[p++];
vis[u] = -1;
for (int i = he[u]; i; i = ne[i]) {
if (vis[to[i]] == 0) {
Q[q++] = to[i];
}
}
}
int tmp = Q[N - 1];
p = 0;
q = 0;
Q[q++] = tmp;
vis[tmp] = 0;
while (p < q) {
int u = Q[p++];
int mae = 0;
for (int i = he[u]; i; i = ne[i]) {
if (vis[to[i]] < 0) {
vis[to[i]] = u;
Q[q++] = to[i];
mae = i;
}
else {
if (mae) ne[mae] = ne[i];
else he[u] = ne[i];
}
}
}
ll m = (1ll << 20) - 1, m0 = ~m, m1 = 1ll << 20;
for (int i = N - 1; i >= 0; i--) {
int u = Q[i];
ll kari = (are[u] & m0) + m1 + u;
if (are[vis[u]] < kari) are[vis[u]] = kari;
}
int k = 0;
q = 0;
Q[q++] = tmp;
while (q) {
k++;
int u = Q[q - 1];
if (u < 0) q--;
else {
vis[u] = -k;
Q[q - 1] = -u;
int nanika = are[u] & m;
if (nanika) Q[q++] = nanika;
for (int i = he[u]; i; i = ne[i]) {
int v = to[i];
if (v != nanika) {
Q[q++] = v;
}
}
}
}
rep1(i, N) putint(-vis[i]);
fwrite(dn, 1, di - dn, stdout);
Would you please return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define pbp(f, s) pb(mp(f, s));
using namespace std;
typedef long long int ll;
typedef pair<int, int> PI;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
/*--------------------------------------------------*/
string x;
ll m;
/*--------------------------------------------------*/
void speedUpIO() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
}
bool check(ll p) {
V<ll> y;
ll mm = m;
while (mm > 0) {
y.pb(mm % p);
mm /= p;
}
reverse(all(y));
if (x.size() < y.size()) return true;
if (x.size() > y.size()) return false;
rep(i, x.size()) {
if (x[i] - '0' < y[i]) return true;
if (x[i] - '0' > y[i]) return false;
}
return true;
}
int main() {
speedUpIO();
cin >> x >> m;
if (x.size() == 1) {
int xi = stoi(x);
cout << (xi <= m ? 1 : 0) << "\n";
return 0;
}
int d = 0;
for (char c : x) d = max(d, c - '0');
ll ok = d;
ll ng = m + 1;
// ll ng = 1e18 + 1;
while (ng - ok > 1) {
ll mid = ok / 2 + ng / 2;
if (ok % 2 == 1 && ng % 2 == 1) mid++;
if (check(mid))
ok = mid;
else
ng = mid;
}
cout << ok - d << "\n";
return 0;
} |
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <numeric>
#include <utility>
#include <string>
using namespace std;
using VL = vector<long long>;
using VVL = vector<VL>;
using VVVL = vector<VVL>;
VL calc(bool is_front, const VL &a) {
int n = a.size();
int n2 = n / 2;
VL b;
if (is_front) {
for (int i = 0; i < n2; i++) {
b.emplace_back(a[i]);
}
} else {
for (int i = n2; i < n; i++) {
b.emplace_back(a[i]);
}
}
VL points;
int nb = b.size();
int limit = 1 << nb;
for (int i = 0; i < limit; i++) {
long long val = 0;
for (int j = 0; j < nb; j++) {
if ((i >> j) & 1) {
val += b[j];
}
}
points.emplace_back(val);
}
sort(points.begin(), points.end());
return points;
}
int main() {
int n, t;
cin >> n >> t;
VL a(n, 0);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
auto v1 = calc(true, a);
auto v2 = calc(false, a);
long long ans = 0;
for (auto x: v1) {
if (x > t) {
break;
}
ans = max(ans, x);
auto it = upper_bound(v2.begin(), v2.end(), t - x);
if (it != v2.begin()) {
ans = max(ans, x + *prev(it));
}
}
cout << ans << endl;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define pb push_back
#define f(i,n) for(int i=0; i<n; ++i)
#define fi(i,st,n) for(int i=st; i<=n; ++i)
#define veci vector<int>
#define vecp vector<pair<int,int> >
#define vecl vector<ll>
int prime[100000+10];
ll lcm(ll a, ll b) {
return a*b/__gcd(a,b);
}
ll power(ll a, ll n, ll mod) {
ll res = 1;
while(n > 0) {
if(n&1) {
res = (res*a)%mod;
}
a = (a*a)%mod;
n = n/2;
}
return res;
}
ll sum(int arr[], int n) {
ll res = 0;
f(i,n) {
res += arr[i];
}
return res;
}
void seive() {
prime[1] = 0;
for(int i=2; i<=100000; i++) {
prime[i] = 1;
}
for(ll i=2; i<=100000; i++) {
if(prime[i]) {
for(ll j=i*i; j<=100000; j+=i) {
prime[j] = 0;
}
}
}
}
ll n,k;
void calc(ll arr[], ll x[], ll n, ll c) {
for (int i=0; i<(1<<n); i++) {
ll sum = 0;
for (int j=0; j<n; j++)
if (i & (1<<j))
sum += arr[j+c];
x[i] = sum;
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
// cin>>t;
while(t--){
cin>>n>>k;
ll arr[n];
ll a[2000000+10],b[2000000+10];
f(i,n) cin>>arr[i];
calc(arr,a,n/2,0);
calc(arr,b,n-n/2,n/2);
int sa = 1<<(n/2);
int sb = 1<<(n-n/2);
sort(b, b+sb);
ll max = 0;
f(i,sa) {
if (a[i] <= k) {
int p = lower_bound(b, b+sb, k-a[i]) - b;
if (p == sb || b[p] != (k-a[i]))
p--;
if ((b[p]+a[i]) > max)
max = b[p]+a[i];
}
}
cout<<max<<"\n";
}
return 0;
}
|
#include <iostream>
using namespace std;
int main() {
int a, b; cin >> a >> b;
int sum =0;
int sum1 =0;
for( int i=0;i<3; i++){
sum += ( a%10) ;
sum1 += ( b%10) ;
a/=10 ;
b/=10 ;
}
cout<< max( sum1 , sum) ;
return 0;}
|
// Problem: A - Large Digits
// Contest: AtCoder - AtCoder Beginner Contest 187
// URL: https://atcoder.jp/contests/abc187/tasks/abc187_a
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define eb emplace_back
#define F first
#define S second
#define FAST ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define sz(a) signed(a.size())
using namespace std;
void solve(){
string a,b;
cin >> a >> b;
ll sa = 0, sb = 0;
for(ll i=0;a[i];i++){
sa += (a[i]-'0');
sb += (b[i] - '0');
}
if(sa>=sb)
cout << sa ;
else
cout << sb;
}
int main() {
FAST;
ll t=1;
//cin>>t;
while(t--) solve();
return 0;
}
|
#include<bits/stdc++.h>
#include<cmath>
#define pb push_back
#define ld long double
#define mp make_pair
#define vl vector<ll>
#define vd vector<double>
#define vld vector<long double>
#define ll long long int
#define pl pair<ll, ll>
#define all(a) a.begin(), a.end()
#define forr(i, n) for(ll i=0; i<n; i++)
#define forr1(i, n) for(ll i=1; i<=n; i++)
using namespace std;
const ld PI =3.1415926535897923846;
const ll MOD = 1000000007;
const ll N=998244353;
ll power(ll x,ll n){ll res=1;while(n>0){if(n&1) res=res*x%MOD;x=x*x%MOD;n>>=1;}return res;}
ll modinverse(ll a){return power(a, MOD-2);}
ll n, k;
bool bin_search(vector<vl> shite, ll n, ll ele)
{
vector<vl> s(n+1, vl(n+1, 0));
forr1(i, n)
{
forr1(j, n)
{
s[i][j] = s[i-1][j] + s[i][j-1] - s[i-1][j-1] + shite[i-1][j-1];
}
}
for(ll i=k; i<=n; i++)
{
for(ll j=k; j<=n; j++)
{
if (s[i][j]-s[i-k][j]-s[i][j-k]+s[i-k][j-k]>=ele)
{
return false;
}
}
}
return true;
}
void solve()
{
cin>>n>>k;
vector<vl> a(n, vl(n, 0));
forr(i, n)
{
forr(j, n)
{
cin>>a[i][j];
}
}
ll low=0, high=(ll)1e10;
ll works=high;
ll ele=(k*k-1)/2+1;
while(high>=low)
{
ll mid=low+(high-low)/2;
vector<vl> b(n, vl(n, 0));
forr(i, n)
{
forr(j, n)
{
if(a[i][j]<=mid) b[i][j]=1;
else b[i][j]=0;
}
}
if(!bin_search(b, n, ele))
{
high=mid-1;
}
else
{
low=mid+1;
}
}
cout<<low<<endl;
}
int main()
{
#ifndef ONLINE_JUDGE
// for getting input from input.txt
freopen("input.txt", "r", stdin);
// for writing output to output.txt
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll test=1;
//cin>>test;
while(test--)
{
solve();
}
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
} | // Qg3
// //g++ -std=c++17 -O2 -Wall a.cpp -o test
// _()_
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define vll vector<ll>
#define vi vector<int>
#define vb vector<bool>
#define pi pair<int,int>
#define pll pair<ll,ll>
#define vp vector<pi>
#define vpll vector<pll>
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
#define For(i,a,b) for(ll i=a;i<b;i++)
#define debug2(x,y) cout<<"This side ----> "<<#x<<" -> "<<x<<" | "<<#y<<" -> "<<y<<endl;
#define debug(x) cout<<"This side ----> "<<#x<<" -> "<<x<<endl
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(), x.rend()
#define mint map<int,int>
#define mall map<ll,ll>
#define ciN cin
#define gu(a,s) get<a>(s)
#define tin tuple<ll,ll,ll>
#define ter(x,y,z) ((x)?y:z)
#define endl "\n";
/////////////
const ll maxn = 1e5 + 3;
const ll max_val = 5e4 + 10;
ll mod = 1e9 + 7;
const ll bits = 18;
ll caseNumber = 1;
////////////////////////////////////////////////////////////////
int dp[26][maxn];
int n; string s;
int work(int lst, int ind) {
if (ind >= n)return 1e8;
if (ind == 0) {
lst = s[ind] - 'a';
return work(lst, ind + 1);
}
if (ind == n - 1) {
if (lst != s[ind] - 'a') {
return 1;
}
else {
return 1e8;
}
}
if (dp[lst][ind] != -1)return dp[lst][ind];
else {
int curr = 1e8;
if (s[ind] - 'a' != lst) {
int here = work(s[ind + 1] - 'a', ind + 1);
if (here != -1) {
curr = min(curr, here + 1);
}
}
int ok = work(lst, ind + 1);
if (ok != -1) {
curr = min(curr, ok);
}
return dp[lst][ind] = curr;
}
}
void solve() {
cin >> n;
cin >> s;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 26; j++) {
dp[j][i] = -1;
}
}
if (s.size() == 1) {
cout << -1 << endl; return;
}
int fin = work(0, 0);
if (fin == 1e8) {
cout << -1 << endl; return;
}
cout << fin << endl;
}
bool TestCase1 = 0;
bool isGoogles = 0;
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
int main() {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
ll t;
t = 1;
if (TestCase1) {
cin >> t;
}
for (int i = 0; i < t; i++) {
solve();
}
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
*/ |
#include<bits/stdc++.h>
#define fasterthanlight ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define endl "\n"
#define int long long
#define vi vector<int>
#define pb push_back
#define mod 1000000007
#define MAX 1e9
#define MIN -1e9
#define hi "visited\n" //sasta debugging
using namespace std;
//author::@whohet-->Het Patel
int findMaxGCD(int *arr, int n)
{
// Calculating MAX in array
int high = 0;
for (int i = 0; i < n; i++)
high = max(high, arr[i]);
// Maintaining count array
int count[high + 1] = {0};
for (int i = 0; i < n; i++)
count[arr[i]]++;
// Variable to store the
// multiples of a number
int counter = 0;
// Iterating from MAX to 1
// GCD is always between
// MAX and 1. The first
// GCD found will be the
// highest as we are
// decrementing the potential
// GCD
for (int i = high; i >= 1; i--)
{
int j = i;
counter = 0;
// Iterating from current
// potential GCD
// till it is less than
// MAX
while (j <= high)
{
// A multiple found
if (count[j] >= 2)
return j;
else if (count[j] == 1)
counter++;
// Incrementing potential
// GCD by itself
// To check i, 2i, 3i....
j += i;
// 2 multiples found,
// max GCD found
if (counter == 2)
return i;
}
}
return 1;
}
void solve()
{
int a, b;
cin >> a >> b;
int n = b - a + 1;
int arr[n];
int tmp = a;
for (int i = 0; i < n; i++)
{
arr[i] = tmp; tmp++;
}
cout << findMaxGCD(arr, n);
}
signed main()
{
fasterthanlight;
int T; T = 1;
//cin >> T;
while (T--)
{
solve();
}
//cout << "\n\n"<<"Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " seconds.\n";
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define swap(a,b) (a^=b^=a^=b)
#define ll long long int
#define ull unsigned long long int
#define uint unsigned int
#define format(a) memset(a,0,sizeof(a))
#define rep(x,y,z) for(int x=y;x<=z;x++)
#define dec(x,y,z) for(int x=y;x>=z;x--)
#define mst(x) memset(x,0,sizeof(x))
const int maxn=105;
inline int read()
{
int ans=0;
char last=' ',ch=getchar();
while(ch<'0'|ch>'9')last=ch,ch=getchar();
while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
if(last=='-')ans=-ans;
return ans;
}
int main()
{
int a,b;
cin>>a>>b;
dec(i,b-a,1)
{
rep(j,(max(a,i)+i-1)/i*i,b-i)
{
if(__gcd(j,j+i)==i)
{
cout<<i<<endl;
return 0;
}
}
}
return 0;
}
//100001 200000
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define P pair<int,int>
int main(){
ll B,C;
cin >> B >> C;
ll Al,Ar,Bl,Br;
ll ans;
Al = B - C/2;
Ar = max(B,B + (C-2)/2);
Bl = (-1)*B-(C-1)/2;
Br = (-1)*B+(C-1)/2;
if(B>0){
ans = Ar-Al+1+Br-Bl+1;
if(Br>=Al) ans -= (Br-Al+1);
}
if(B<0){
ans = Ar-Al+1+Br-Bl+1;
if(Ar>=Bl) ans -= (Ar-Bl+1);
}
if(B==0){
ans = Br - Al +1;
}
cout << ans << endl;
} | #include <bits/stdc++.h>
using namespace std;
using lint = long long;
using P = pair<int, int>;
using vec = vector<int>;
using mat = vector<vector<int>>;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define endl "\n"
constexpr int MOD = 1000000007;
constexpr int INF = 1000000010;
int main() {
int h, w;
cin >> h >> w;
int mn = INF;
mat a(h, vec(w));
rep(i, h) rep(j, w) {
int x;
cin >> x;
a[i][j] = x;
mn = min(mn, x);
}
int res = 0;
rep(i, h) rep(j, w) res += (a[i][j] - mn);
cout << res << endl;
return 0;
}
|
#include <iostream>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int,int>
#define vpii vector<pii>
#define V vector
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F first
#define S second
#define sqr(x) (x)*(x)
#define mp make_pair
#define mtp make_tuple
#define tpii tuple<int,int,int>
#define ckmax(a,b) a=max(a,b)
#define ckmin(a,b) a=min(a,b)
#define rep(n) for(int i=0;i<n;i++)
#define inf INT_MAX
#define debug(x) cerr<<x<<'\n';
#define quick ios_base::sync_with_stdio(0),cin.tie(0);long double nownow=clock();
#define total_time cerr<<(clock()-nownow)/CLOCKS_PER_SEC*1000<<" (ms)\n";
using namespace std;
template<typename T>void in(vector<T> &a){for(auto &i:a)cin>>i;}
template<typename T>void in(vector<T> &a,int n){for(auto i=0;i<n;i++)cin>>a[i];}
template<typename T>void out(vector<T> &a){for(auto &i:a)cout<<i;cout<<'\n';}
#define int long long
#define input freopen("/Users/wangkaiyu/Downloads/Q_6_24_3.in","r",stdin);
signed main(){
quick
int n,m;
cin>>n>>m;
vvi a(n+1);
for(int i=0;i<m;i++){
int x,y;
cin>>x>>y;
a[x].pb(y);
}
int cnt=0;
for(int i=1;i<=n;i++){
queue<int> q;
q.push(i);
vi f(n+1,0);
f[i]=1;
while(!q.empty()){
cnt++;
int t=q.front();
for(int j:a[t]){
if(f[j]==0)
q.push(j);
f[j]=1;
}
q.pop();
}
}cout<<cnt<<'\n';
total_time
return 0;
}
| #pragma GCC optimize("O3")
//#pragma GCC target("avx")
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <iomanip>
#include <list>
#include <stack>
using namespace std;
#define re return
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define make_unique(x) sort(all(x)),x.resize(unique(all(x))-x.begin())
#define fi first
#define se second
#define ss second.second
#define sf second.first
#define ff first.first
#define fs first.second
#define sqrt(x) sqrt(abs(x))
#define mp make_pair
#define PI 3.14159265358979323846
#define E 2.71828182845904523536
#define er erase
#define in insert
#define fo(i,n) for((i)=0;(i)<(n);(i)++)
#define ro(i,n) for((i)=n-1;(i)>=0;(i)--)
#define fr(i,j,n) for((i)=(j);(i)<(n);(i)++)
#define rf(i,j,n) for((i)=((n)-1);(i)>=(j);(i)--)
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
void eras(map<int,int> &m,int x)
{
m[x]--;
if (!m[x]) m.erase(x);
}
ostream& operator <<(ostream &out,__int128 x)
{
out<<(ll)x;
re out;
}
istream& operator >>(istream &in,__int128 &x)
{
ll temp;
in>>temp;
x=temp;
re in;
}
const int N=(int)3e5+100;
const int M=(int)1e7+10;
const ll inf=(ll)1e18+100;
const double eps=1e-9;
#define filename ""
int n;
vector<pair<int,pair<int,int> > > v[N];
ll d[N];
bool sss(int x,int y)
{
if (d[x]==d[y]) re x<y;
re d[x]<d[y];
}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
//freopen(filename".in","r",stdin);
//freopen(filename".out","w",stdout);
//freopen("ans.txt","w",stdout);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
srand(time(0));
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int m;
cin>>n>>m;
int i;
fo(i,m)
{
int a,b,c,d;
cin>>a>>b>>c>>d;
a--;
b--;
v[a].pb({b,{c,d}});
v[b].pb({a,{c,d}});
}
set<int,bool(*)(int,int)> s(sss);
fr(i,1,n)
{
d[i]=inf;
s.in(i);
}
s.in(0);
while(!s.empty())
{
int x=*s.begin();
s.er(x);
for(auto y:v[x])
{
ll t=d[x]+1;
ll c=y.ss/t;
if (t*t<y.ss) t=sqrt(y.ss);
t=max(t,d[x]+1);
c=y.ss/t;
ll len=y.sf+c+t-1;
int k=6;
while(c)
{
k--;
if (!k) break;
len=min(len,y.sf+c+t-1);
t=y.ss/c+1;
c=y.ss/t;
}
len=min(len,y.sf+c+t-1);
if (d[y.fi]>len)
{
s.er(y.fi);
d[y.fi]=len;
s.in(y.fi);
}
}
}
if (d[n-1]==inf) d[n-1]=-1;
cout<<d[n-1];
}
|
//#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
constexpr lint mod = 1e9 + 7;
#define all(x) (x).begin(), (x).end()
#define bitcount(n) __builtin_popcountll((lint)(n))
#define fcout cout << fixed << setprecision(15)
//#define highest(x) (63 - __builtin_clzl(x))
#define rep(i, n) for(int i = 0; i < int(n); i++)
#define rep2(i, l, r) for(int i = int(l); i < int(r); i++)
#define repr(i, n) for(int i = int(n) - 1; i >= 0; i--)
#define repr2(i, l, r) for(int i = int(r) - 1; i >= int(l); i--)
#define mp(x, y) make_pair(x, y)
constexpr int inf9 = 1e9; constexpr lint inf18 = 1e18;
inline void Yes(bool condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
lint power(lint base, lint exponent, lint module){ if(exponent % 2){ return power(base, exponent - 1, module) * base % module; }else if(exponent){ lint root_ans = power(base, exponent / 2, module); return root_ans * root_ans % module; }else{ return 1; }}
struct position{ int x, y; }; position mv[4] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}}; double euclidean(position first, position second){ return sqrt((second.x - first.x) * (second.x - first.x) + (second.y - first.y) * (second.y - first.y)); }
template<class T, class U> string to_string(pair<T, U> x){ return to_string(x.first) + "," + to_string(x.second); } string to_string(string x){ return x; }
template<class T> string to_string(complex<T> x){ return to_string(make_pair(x.real(), x.imag())); }
template<class itr> void array_output(itr start, itr goal){ string ans; for(auto i = start; i != goal; i++) cout << (i == start ? "" : " ") << (*i); if(!ans.empty()) ans.pop_back(); cout << ans << endl; }
template<class itr> void cins(itr first, itr last){ for(auto i = first; i != last; i++){ cin >> (*i); } }
template<class T> T gcd(T a, T b){ if(b) return gcd(b, a % b); else return a; }
template<class T> T lcm(T a, T b){ return a / gcd(a, b) * b; }
struct combination{ vector<lint> fact, inv; combination(int sz) : fact(sz + 1), inv(sz + 1){ fact[0] = 1; for(int i = 1; i <= sz; i++){ fact[i] = fact[i - 1] * i % mod; } inv[sz] = power(fact[sz], mod - 2, mod); for(int i = sz - 1; i >= 0; i--){ inv[i] = inv[i + 1] * (i + 1) % mod; } } lint P(int n, int r){ if(r < 0 || n < r) return 0; return (fact[n] * inv[n - r] % mod); } lint C(int p, int q){ if(q < 0 || p < q) return 0; return (fact[p] * inv[q] % mod * inv[p - q] % mod); } };
template<class itr> bool next_sequence(itr first, itr last, int max_bound){ itr now = last; while(now != first){ now--; (*now)++; if((*now) == max_bound){ (*now) = 0; }else{ return true; } } return false; }
template<class itr, class itr2> bool next_sequence2(itr first, itr last, itr2 first2, itr2 last2){ itr now = last; itr2 now2 = last2; while(now != first){ now--, now2--; (*now)++; if((*now) == (*now2)){ (*now) = 0; }else{ return true; } } return false; }
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; }
inline int at(lint i, int j){ return (i >> j) & 1; }
random_device rnd;
bool is_in_board(lint y, lint x, lint H, lint W){ return (0 <= y && y < H && 0 <= x && x < W); }
lint inv2 = power(2, mod - 2, mod);
int main(){
int n;
string s;
cin >> n >> s;
if(s[0] != s.back()){
cout << 1 << endl;
return 0;
}
rep(i, n - 1){
if(s[i] != s[0] && s[i + 1] != s[0]){
cout << 2 << endl;
return 0;
}
}
cout << -1 << endl;
}
| //In The Name Of Allah
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define F first
#define S second
//#pragma GCC optimize("Ofast")
const int maxn = 2e5 + 5, md = 998244353;
int vis[maxn], n, f[maxn], cnt;
bool c[maxn];
vector<int> cycle, adjr[maxn];
ll ans = 1;
int dfs3(int v)
{
int A = 1;
for(auto u : adjr[v])
A += dfs3(u);
return A;
}
void dfs2(int v)
{
c[v] = 1;
if(!c[f[v]])
dfs2(f[v]);
}
void dfs(int v)
{
vis[v] = cnt;
if(vis[f[v]] == cnt)
cycle.pb(f[v]);
else if(!vis[f[v]])
dfs(f[v]);
}
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> f[i];
adjr[f[i]].pb(i);
}
for(int i = 1; i <= n; i++)
if(!vis[i])
{
cnt++, dfs(i);
}
memset(vis, 0, maxn);
for(auto v : cycle)
if(!c[v])
{
//cout << v << " ";
dfs2(v);
ans = ans * 2 % md;
}
//return 0;
/*for(int i = 1; i <= n; i++)
if(c[f[i]]&& !c[i])
{
ans *= dfs3(i) + 1;
ans %= md;
}*/
cout << ans - 1 << '\n';
}
|
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define out(x) cout << x << '\n'
#define lli long long int
#define uli unsigned long long int
#define rep(i, m, n) for (lli i = m; i < (n); i++)
#define repe(i, m, n) for (lli i = m; i <= (n); i++)
#define ALL(x) (x).begin(), (x).end()
#define SIZE(x) ((lli)(x).size())
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
#define _GLIBCXX_DEBUG
const lli INF = 2147483647;
const lli MINF = -2147483648;
const lli LINF = 9223372036854775807;
const lli MOD = 1000000007; //10^9+7
const double PI = acos(-1);
#define SORT(n) sort(n.begin(), n.end())
#define SORTR(n) sort(n.begin(), n.end(), greater<int>())
#define REV(n) reverse(n.begin(), n.end())
#define pb push_back
#define eb emplace_back
#define mp make_pair
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vlli = vector<lli>;
using vvli = vector<vlli>;
using vs = vector<string>;
using vvs = vector<vs>;
using vb = vector<bool>;
using vvb = vector<vb>;
using ll = long long;
//---------------------------------------------------------------------------------------------------
// vector入力
template <typename T>
istream &operator>>(istream &is, vector<T> &vec)
{
for (T &x : vec)
is >> x;
return is;
}
// vector出力
template <typename T>
ostream &operator<<(ostream &os, vector<T> &vec)
{
// os << '{';
for (int i = 0; i < vec.size(); i++)
{
os << vec[i] << (i + 1 == vec.size() ? "" : "");
}
// os << '}';
return os;
}
// aよりもbが大きいならばaをbで更新する(更新されたならばtrueを返す)
template <typename T>
bool chmax(T &a, const T &b)
{
if (a < b)
{
a = b; // aをbで更新
return true;
}
return false;
}
// aよりもbが小さいならばaをbで更新する(更新されたならばtrueを返す)
template <typename T>
bool chmin(T &a, const T &b)
{
if (a > b)
{
a = b; // aをbで更新
return true;
}
return false;
}
//---------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------
void solve()
{
}
//---------------------------------------------------------------------------------------------------
signed main()
{
lli a = 0, b = 0, c = 0, h = 0, n = 0, w = 0, ans = 0, count = 0;
string s = "", t = "";
vector<pair<lli, lli>> pr;
map<lli, lli> ma;
set<lli> st;
ios::sync_with_stdio(false);
cin.tie(nullptr);
lli x;
cin >> a >> b;
cin >> c >> x;
ans = a * x - b * c;
out(ans);
// out((ans > 0 ? "Yes" : "No"));
} | #include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b,c,d,e;
cin>>a>>b>>c>>d;
e=(a*d)-(b*c);
cout<<e<<endl;
return 0;
}
|
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define double long double
#define all(x) begin(x), end(x)
#define Done(x) cout<<x<<endl; return;
#define endl '\n'
void solve(){
int n; cin>>n;
int m; cin>>m;
map<int,int>occur;
for(int i=0;i<n;i++){
int a; cin>>a;
int b; cin>>b; b++;
int c; cin>>c;
occur[a]+=c;
occur[b]-=c;
}
int ans=0;
int curr=0;
int last=0;
for(auto p:occur){
int dist=p.first-last;
ans+=dist*min(curr,m);
last=p.first;
curr+=p.second;
}
cout<<ans<<endl;
}
int32_t main(){
// Remove for Interactive Problems
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
solve();
cerr<<"Time : "<<1000*((double)clock())/(double)CLOCKS_PER_SEC<<"ms\n";
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=998244353;
vector<int>G[200005],r[200005];
int in[200005],out[200005],depth[200005];
int id;
void dfs(int i,int d=0){
in[i]=id++;
depth[i]=d;
for(int u:G[i]){dfs(u,d+1);}
out[i]=id;
}
void solve(){
int n;
cin>>n;
rep(i,n-1){
int p;
cin>>p;
p--;
G[p].push_back(i+1);
}
dfs(0);
rep(i,n){r[depth[i]].push_back(in[i]);}
rep(i,n){sort(r[i].begin(),r[i].end());}
int Q;
cin>>Q;
rep(i,Q){
int u,d;
cin>>u>>d;
u--;
int ans=lower_bound(r[d].begin(),r[d].end(),out[u])-lower_bound(r[d].begin(),r[d].end(),in[u]);
cout<<ans<<endl;
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
} |
Subsets and Splits