code_file1
stringlengths 87
4k
| code_file2
stringlengths 85
4k
|
---|---|
// “Alhamdulillah for Everything”
#include<bits/stdc++.h>
using namespace std;
//---------DEFINES------------
#define pai acos(-1)
#define ff first
#define ss second
#define ll long long
#define pb push_back
#define mp make_pair
#define pll pair<ll,ll>
#define sz(a) (ll)a.size()
#define endl "\n"
#define sline(s) getline(cin,s);
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) ((a*b)/gcd(a,b))
#define all(sob) sob.begin(),sob.end()
#define ms(a, b) memset(a,b,sizeof(a))
#define rep(i,b,n) for(ll i = b;i <= n; i++)
#define rev(i,b,n) for(ll i = b;i >= n; i--)
#define setp(x) cout<<fixed<<setprecision(x)
#define watch(x) cout<<(#x)<<" = "<<(x)<<'\n'
#define TEST_CASE(t) for(ll zz=1;zz<=t;zz++)
#define PRINT_CASE cout<<"Case "<<zz<<": ";
#define fii freopen("input.txt","r",stdin);
#define foo freopen("output.txt","w",stdout);
#define ActioN ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
//-------CONSTANTS------------
const ll mod = 1e9+7;
const ll mxn = 1e5+7;
const ll inf = 1e18;
//------MOD-FUNCTIONS---------
inline void normal(ll &a) { a %= mod; (a < 0) && (a += mod); }
inline ll modMul(ll a, ll b) { a %= mod, b %= mod; normal(a), normal(b); return (a * b) % mod; }
inline ll modAdd(ll a, ll b) { a %= mod, b %= mod; normal(a), normal(b); return (a + b) % mod; }
inline ll modSub(ll a, ll b) { a %= mod, b %= mod; normal(a), normal(b); a -= b; normal(a); return a; }
inline ll modPow(ll b, ll p) { ll r = 1; while (p) { if (p & 1) r = modMul(r, b); b = modMul(b, b); p >>= 1; } return r; }
inline ll modInverse(ll a) { return modPow(a, mod - 2); }
inline ll modDiv(ll a, ll b) { return modMul(a, modInverse(b)); }
int main()
{
//Light_Camera:)
ActioN;
ll i,j,k,a,b,c,d,n,m,t,u,v,w,x,y,z;
vector<ll>vec(3);
for(i=0;i<3;i++)
{
cin>>vec[i];
}
sort(all(vec));
a=vec[0];
b=vec[1];
c=vec[2];
if(a==b)
{
cout<<c<<endl;
}
else if(b==c)
{
cout<<a<<endl;
}
else
{
cout<<0<<endl;
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b,c;
cin >> a >> b >> c;
if(a==b)
{
cout << c << endl;
}
else if(b==c)
{
cout << a << endl;
}
else if(c==a)
{
cout << b << endl;
}
else
{
cout << 0 << endl;
}
return 0;
} |
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define pb(a) push_back(a)
#define set0(a) memset(a,0,sizeof(a))
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define dwn(i,a,b) for(int i=(a);i>=(b);i--)
#define INF 0x3f3f3f3f
typedef long long ll;
int main(){
int T; cin>>T;
while(T--){
ll n; cin>>n;
int d=0;
while(n%2==0){
n/=2;
d++;
}
if(!d) puts("Odd");
else if(d==1) puts("Same");
else puts("Even");
}
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
ll t, n;
scanf("%lld", &t);
while (t--) {
scanf("%lld", &n);
if (n%2==1) {
printf("Odd\n");
}
else {
if (n % 4 == 2) {
printf("Same\n");
}
else {
printf("Even\n");
}
}
}
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll b,c,ans;
ll solve1(ll b,ll c){
ll depth=c/2,flip=c&1;
if(depth<b)return (1+depth)*2-(!flip);
else return b*2+1;
}
ll solve2(ll b,ll c){
ll depth=c/2,flip=c&1;
return max(ll(0),depth*2-(!flip));
}
int main(){
scanf("%lld%lld",&b,&c);
if(b>0)ans=solve1(b,c)+solve2(-b,c-1);
else if(b==0)ans=1+solve2(b,c);
else ans=solve2(b,c)+solve1(-b,c-1);
printf("%lld",ans);
return 0;
} | //wtrl,everybody hangbeat me
#include<bits/stdc++.h>
/*#include<iostream>
#include<string>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<set>
#include<map>
#include<utility>
#include<queue>
#include<vector>
#include<stack>
#include<sstream>
#include<algorithm>*/
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<pii> vii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef pair<ll,ll> pll;
/*=====================================================================*/
#define pb push_back
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define sz(a) (int)(a.size())
#define all(s) (s).begin(),(s).end()
#define m_p make_pair
#define repd(i,n) for(int i=n-1;i>=0;i--)
#define forn(i,p,n) for(int i=p;i<=n;i++)
#define ford(i,p,n) for(int i=n;i>=p;i--)
#define foreach(i,c) for(__typeof(c.begin())i=(c.begin());i!=(c).end();++i)
#define INF 1e9
#define PI acos(-1)
/*=====================================================================*/
string int_to_string(ll n)
{
string s="";
while(n)
{
ll now=n%10;
s+=now+'0';
n/=10;
}
reverse(s.begin(),s.end());
return s;
}
ll string_to_int(string s)
{
ll n=0;
rep(i,s.size())
{
n*=10;
n+=s[i]-'0';
}
return n;
}
/*======================================================================*/
ll lcm(int a,int b)
{
return a/__gcd(a,b)*b;
}
bool prime(int n)
{
if(n==0||n==1)
return false;
for(int i=2;i*i<=n;i++)
if(n%i==0)
return false;
return true;
}
/*======================================================================*/
const int dx[]={-1,0,1,0};
const int dy[]={0,-1,0,1};
const int month[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}};
/*======================================================================*/
int main()
{
std::ios::sync_with_stdio(false);
/*
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
*/
/*====================================================================*/
ll b,c;
cin>>b>>c;
if(c==1)
{
if(b!=0)
{
cout<<2<<endl;
}
else
{
cout<<1<<endl;
}
return 0;
}
if(b>=0)
{
ll ans=2;
ans+=(c-1)/2+c/2-1;
if((c-1)/2+c/2<max(2*abs(b)-1,(ll)0))
{
ans+=(c-1)/2+c/2;
}
else
{
ans+=max(2*abs(b)-1,(ll)0);
}
cout<<ans<<endl;
}
else
{
ll ans=2;
ans+=c/2+(c-1)/2;
if((c-1)/2+c/2-1<max(2*abs(b)-1,(ll)0))
{
ans+=(c-1)/2+c/2-1;
}
else
{
ans+=max(2*abs(b)-1,(ll)0);
}
cout<<ans<<endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<string, string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<ll> vl;
typedef vector<vl> vvl;
double EPS=1e-9;
int INF=1000000005;
long long INFF=1000000000000000005ll;
double PI=acos(-1);
int dirx[8]={ -1, 0, 0, 1, -1, -1, 1, 1 };
int diry[8]={ 0, 1, -1, 0, -1, 1, -1, 1 };
const ll MOD = 1000000007;
ll sum() { return 0; }
template<typename T, typename... Args>
T sum(T a, Args... args) { return a + sum(args...); }
#define DEBUG fprintf(stderr, "====TESTING====\n")
#define VALUE(x) cerr << "The value of " << #x << " is " << x << endl
#define OUT(x) cout << x << endl
#define OUTH(x) cout << x << " "
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define READ(x) for(auto &(z):x) cin >> z;
#define FOR(a, b, c) for (int(a)=(b); (a) < (c); ++(a))
#define FORN(a, b, c) for (int(a)=(b); (a) <= (c); ++(a))
#define FORD(a, b, c) for (int(a)=(b); (a) >= (c); --(a))
#define FORSQ(a, b, c) for (int(a)=(b); (a) * (a) <= (c); ++(a))
#define FORC(a, b, c) for (char(a)=(b); (a) <= (c); ++(a))
#define EACH(a, b) for (auto&(a) : (b))
#define REP(i, n) FOR(i, 0, n)
#define REPN(i, n) FORN(i, 1, n)
#define MAX(a, b) a=max(a, b)
#define MIN(a, b) a=min(a, b)
#define SQR(x) ((ll)(x) * (x))
#define RESET(a, b) memset(a, b, sizeof(a))
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ALL(v) v.begin(), v.end()
#define ALLA(arr, sz) arr, arr + sz
#define SIZE(v) (int)v.size()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define SORTA(arr, sz) sort(ALLA(arr, sz))
#define REVERSEA(arr, sz) reverse(ALLA(arr, sz))
#define PERMUTE next_permutation
#define TC(t) while (t--)
#define FAST_INP ios_base::sync_with_stdio(false);cin.tie(NULL)
#define what_is(x) cerr << #x << " is " << x << endl;
void solve() {
}
int main()
{
FAST_INP;
// #ifndef ONLINE_JUDGE
// freopen("input.txt","r", stdin);
// freopen("output.txt","w", stdout);
// #endif
// int tc; cin >> tc;
// TC(tc) solve();
int n, m, t;
cin >> n >> m >> t;
vi a(m), b(m);
REP(i, m) cin >> a[i] >> b[i];
int left = n;
left -= a[0];
if(left <= 0) {
OUT("No");
return 0;
}
left += (b[0] - a[0]);
MIN(left, n);
FOR(i, 1, m ) {
left -= (a[i] - b[i - 1]);
if(left <= 0) {
OUT("No");
return 0;
}
left += (b[i] - a[i]);
MIN(left, n);
}
left -= (t - b[m - 1]);
if(left <= 0) {
OUT("No");
return 0;
}
OUT("Yes");
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ull unsigned long long
#define all(x) x.begin(),x.end()
#define MOD 1000000007
#define PI acos(-1)
#define EPS 1e-9
#define debug cerr <<
#define var(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define close << "\n";
int total_n(double start,double end){
return (end - start + 1);
}
int main() {
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//freopen("../input.txt","r",stdin);
int N,M,T;
cin >> N >> M >> T;
int maxx = N;
int last = 0;
for (int i = 0; i < M; ++i) {
int a,b;
cin >> a >> b;
if(i!=0){
N = max(0,N - total_n(last,a) + 1);
if(N == 0){
cout << "No" close
return 0;
}
}
if(i == 0){
N = max(0,N - total_n(0,a) + 1);
if(N == 0){
cout << "No" close
return 0;
}
}
N = min(maxx,N + total_n(a,b) - 1);
if(N == 0){
cout << "No" close
return 0;
}
last = b;
}
N = max(0,N - total_n(last,T) + 1);
if(N == 0){
cout << "No" close
return 0;
}
if(N == 0){
cout << "No" close
return 0;
}else{
cout << "Yes" close
return 0;
}
} |
/* ** ** ****** ****** ****** ** ****** ***
** ** ** ** ** ** ** ** ** ** ** *********
**\../** ****** ****** ** ** ****** ** ** ***
** ** ** ** ** ** ** ** ** ** ** ** *****
** ** ** ** ** ** ** ****** ****** ****** *****
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define PI 3.14159265358979323846
#define lp(i,n) for(int i = 0; i <n;i++)
#define rlp(i,n) for(int i = int(n);i >= 0;i--)
#define range(i,a,b) for(int i = int(a);i <= int(b);i++)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define MP make_pair
#define fr first
#define sc second
#define vt vector
#define sz(v) ((int)((v).size()))
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
template<class T> using ordered_set=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
const ll MOD = 1e9 + 7;
const int OO = (int)1e6;
const int N = (int)1e7;
int dx[] = {0,0,-1,1};
int dy[] = {1,-1,0,0};
void init(){ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);}
template<class T> void read(T& x) {
cin >> x;
}
void read(double& d) {
string t;
read(t);
d=stod(t);
}
void read(long double& d) {
string t;
read(t);
d=stold(t);
}
template<class H, class... T> void read(H& h, T&... t) {
read(h);
read(t...);
}
template<class A> void read(vt<A>& x) {
for(auto& a:x)
read(a);
}
template<class A, size_t S> void read(array<A, S>& x) {
for(auto& a:x)
read(a);
}
void _run(){
int a,b,c,d;
read(a,b,c,d);
int cnt =0;
while(a--&&b--&&c--&&d--)cnt++;
cout<<cnt<<endl;
}
int main() {
# ifdef LOCAL_PROJECT
freopen("in.txt", "r", stdin);
# endif
init();
int t=1;
// read(t);
while(t--)_run();
return 0;
} | #include <bits/stdc++.h>
#define N 200005
#define int long long
using namespace std;
void solve(){
int n;
cin >> n;
vector<pair<int,int>> v;
for(int i=0;i<n;i++){
int t,l,r;
cin >> t >> l >> r;
l *=2;
r *=2;
if(t == 2 || t == 4)r--;
if(t == 3 || t == 4)l++;
if(l <= r)
v.push_back({l,r});
}
int ans = 0;
for(int i=0;i<v.size();i++){
for(int j=0;j<i;j++){
auto u = v[i];
auto k = v[j];
if(!(k.second < u.first || u.second < k.first)){
ans++;
}
}
}
cout << ans;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef Local
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t=1;
//cin>>t;
while(t--){
solve();
}
#ifdef Local
cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
#endif
} |
#include <bits/stdc++.h>
using namespace std;
#define FOR(it, ar) for(auto &it: ar)
#define loop(i, start, end) for(auto i = (start); i <= (end); i++)
#define loopSkip(i, start, end, skip) for(auto i = (start); i <= (end); i += skip)
#define loopRev(i, start, end) for(int i = (start); i >= (end); i--)
#define FLASH ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define testCases(t) cin >> t; while(t--)
#define readLine(s) getline(cin, s);
#define PN(n) cout << (n)
#define PN1(n) cout << (n) << " "
#define PN2(a, b) cout << (a) << " " << (b) << " "
#define PN3(a, b, c) cout << (a) << " " << (b) << " " << (c) << " "
#define PNN1(n) cout << (n) << "\n"
#define PNN2(a, b) cout << (a) << " " << (b) << "\n"
#define PNN3(a, b, c) cout << (a) << " " << (b) << " " << (c) << "\n"
#define PrintObject(ar) for(auto var: ar) cout << var << " "; cout << "\n"
#define PrintMap(map) for(auto var: map) PNN2(var.first, var.second)
#define PrintPair(pair) PNN2(pair.first, pair.second)
#define IN1(n) cin >> n
#define IN2(a, b) cin >> a >> b
#define IN3(a, b, c) cin >> a >> b >> c
#define ALL(s) s.begin(), s.end()
#define ENTER PNN1("")
#define FLUSH cout.flush()
#define llPair std::pair<lli, lli>
#define iiPair std::pair<int, int>
// #define ONLINE_JUDGE
// Math macros
#define M_PI 3.14159265358979323846
typedef long long int lli;
typedef unsigned long long int ulli;
typedef long double ld;
template <typename T>
T ii() {
T a;
IN1(a);
return a;
}
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
int main(void){
FLASH
#ifndef ONLINE_JUDGE
freopen("input-stream.txt", "r", stdin);
freopen("output-stream.txt", "w", stdout);
#endif
int n;
IN1(n);
vector<int> ar(n);
FOR(it, ar) IN1(it);
unordered_set<int> dp;
loop(i, 1, n) dp.insert(i);
FOR(it, ar) dp.erase(it);
PNN1(dp.empty() ? "Yes" : "No");
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
bool testcase(){
ll n;
cin>>n;
vector<ll> arr;
for(int i=0;i<n;++i){
ll temp;
cin>>temp;
arr.push_back(temp);
}
sort(arr.begin(),arr.end());
for(int i=0;i<n-1;++i){
if(abs(arr[i]-arr[i+1])!=1)
return false;
}
return true;
}
int main() {
if(testcase())
cout<<"Yes";
else
cout<<"No";
}
|
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int,int>;
#define ff first
#define se second
#define pb push_back
#define all(x) (x).begin(),(x).end()
const int mod = 1e9+7;
const int inf = 1e9;
const int mx = 1e6+5;
int n; string s;
void solve(int cs){
// cout << "Case " << cs << ": ";
cin >> n >> s;
vector<char> v;
for(char c : s){
if(c == 'x'){
if(v.size()<2)
v.push_back(c);
else{
char v2 = v.back(); v.pop_back();
char v1 = v.back(); v.pop_back();
if(v1=='f' && v2=='o') continue;
v.push_back(v1);
v.push_back(v2);
v.push_back(c);
}
}else v.push_back(c);
}cout << v.size() << "\n";
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int tc = 1, cs = 1;
// cin >> tc;
while(tc--) solve(cs++);
return 0;
}
| #include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <cmath>
using namespace std;
using ll = long long;
using ull = unsigned long long;
constexpr ll LLINF {1001002003004005006};//ll = 9*LLINF
constexpr int INTINF {1000000000};//int = 2*INTINF
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for(int i = s; i < t; ++i)
template<typename T>
void maxs(T& x, T&& y) {
x=std::max(x,y);
}
template<typename T>
void maxs(T& x, T& y) {
x=std::max(x,y);
}
template<typename T>
void mins(T& x, T&& y) {
x=std::min(x,y);
}
template<typename T>
void mins(T& x, T& y) {
x=std::min(x,y);
}
int find(const int& idx, std::vector<int>& idx_to_path) {
if (idx == idx_to_path[idx]) return idx;
return idx_to_path[idx] = find(idx_to_path[idx], idx_to_path);
}
int main(int argc, char** argv) {
int n;
cin >> n;
std::vector<char> s(n+2, '.');
rep(i, n) {
cin >> s[i+2];
}
n += 2;
std::vector<int> idx(n);
rep(i, n) idx[i] = i;
int ans {n-2};
for (int i = 2;i < n;++i) {
int c3 = i;
int c2 = find(c3-1, idx);
int c1 = find(c2-1, idx);
if (s[c1] == 'f' && s[c2] == 'o' && s[c3] == 'x') {
idx[c3] = find(c1-1, idx);
idx[c2] = find(idx[c3]-1, idx);
idx[c1] = find(idx[c2]-1, idx);
ans -= 3;
}
}
std::cout << ans << '\n';
}
|
#include <cstdio>
int main() {
char a = getchar(), b = getchar(), c = getchar();
if(a == b && a == c) puts("Won");
else puts("Lost");
return 0;
} | #include <iostream>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <set>
#include <algorithm>
#include <iomanip>
#include <string.h>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) (a).begin(),(a).end()
typedef long long lint;
using namespace std;
int main(){
char c[3];
REP(i,3)cin>>c[i];
sort(c,c+3);
if(c[0]==c[2])cout<<"Won"<<endl;
else cout<<"Lost"<<endl;
return 0;
}
|
#include<bits/stdc++.h>
//#pragma GCC optimize "trapv"
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
#define fast_az_fuk ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll long long
#define lll __int128
#define ull unsigned ll
#define ld long double
#define pb push_back
#define pf push_front
#define dll deque<ll>
#define vll vector<ll>
#define vvll vector<vll>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define dpll deque<pll>
#define mapll map<ll,ll>
#define umapll umap<ll,ll>
#define endl "\n"
#define all(v) v.begin(),v.end()
#define ms(a,x) memset(a,x,sizeof(a))
#define random(l,r,T) uniform_int_distribution<T>(l,r)(rng)
//#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//using namespace __gnu_pbds;
#ifdef LOCAL
#include <debug.h>
#endif
template<typename T> istream& operator >>(istream &in,vector<T> &v){ for(auto &x:v) in>>x; return in;}
template<typename T> ostream& operator <<(ostream &out,const vector<T> &v){ for(auto &x:v) out<<x<<' '; return out;}
template<typename T1,typename T2> istream& operator >>(istream &in,pair<T1,T2> &p){ in>>p.first>>p.second; return in;}
template<typename T1,typename T2> ostream& operator <<(ostream &out,const pair<T1,T2> &p){ out<<p.first<<' '<<p.second; return out;}
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
template<class T, class H>using umap=unordered_map<T,H,custom_hash>;
template<class T>using uset=unordered_set<T,custom_hash>;
int32_t main()
{
#ifdef LOCAL
freopen("error.txt", "w", stderr);
#endif
// clock_t clk = clock();
fast_az_fuk
ll n; cin>>n; vll a(n); cin>>a;
mapll hash; for(ll x:a) hash[x]++;
ll ans = 0;
for(int i=0;i<n;i++){
ans += n - hash[a[i]];
}
ans /= 2;
cout<<ans;
// cerr << '\n'<<"Time (in s): " << double(clock() - clk) * 1.0 / CLOCKS_PER_SEC << '\n';
return 0;
} |
#include<bits/stdc++.h>
#include<iomanip>
using namespace std;
#define REP(i,n) for(int i = 0;i < n;i++)
#define RNG(i,s,n) for(int i = s;i <= n;i++)
#define _RNG(i,e,s) for(int i = e;i >= s;i--)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define dup(x,y) (x + y - 1) / (y)
#define all(x) (x).begin(),(x).end()
#define lb(x,key) lower_bound(all(x) , (key))
#define ub(x,key) upper_bound(all(x) , (key))
template<class T> bool chmax(T& a,T b){ if(a < b){ a = b; return true; }else return false; }
template<class T> bool chmin(T& a,T b){ if(a > b){ a = b; return true; }else return false; }
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using pi = pair<int,int>;
using pl = pair<ll,ll>;
using vpi = vector<pi>;
using vpl = vector<pl>;
#define debug(arr) cout << #arr << " = " << arr << '\n'
#define debug2(a,b) cout<<"["<<#a<<","<<#b<<"] = "<<"["<<a<<","<<b<<"]\n"
#define debug3(a,b,c) cout<<"["<<#a<<","<<#b<<","<<#c<<"] = "<<"["<<a<<","<<b<<","<<c<<"]\n"
template<class T> ostream &operator << (ostream& out, const vector<T>& arr) {
cout << "{"; for (int i = 0; i < arr.size(); i++)cout << (!i ? "" : ", ") << arr[i]; cout << "}";
return out;
}
template<class T> ostream &operator << (ostream& out, const vector<vector<T> >& arr) {
cout << "{\n"; for (auto& vec : arr)cout << " " << vec << ",\n"; cout << "}";
return out;
}
template<class S,class T> ostream &operator << (ostream& out, const pair<S,T>& p){
cout << "{" << p.first << "," << p.second << "}" << '\n';
return out;
}
template<class T> istream &operator >> (istream& in, vector<T>& arr) {
for (auto& i : arr)cin >> i; return in;
}
template<class S,class T> istream &operator >> (istream& in,pair<S,T>& p){
cin >> p.first >> p.second; return in;
}
#define F first
#define S second
int main(void){
cin.tie(0);
ios::sync_with_stdio(false);
ll ans = 0;
ll n,k;
cin >> n >> k;
k = abs(k);
for(ll i = 2;i + k <= 2 * n;i++){
ll p,q;
if(i > n){
p = 2 * n - i + 1;
}else{
p = i - 1;
}
if(i + k > n){
q = 2* n - (i + k) + 1;
}else{
q = i + k - 1;
}
ans+= p * q;
}
cout << ans << '\n';
return 0;
}
|
/**
* author: akifpathan
* created: Monday 10.05.2021 06:02:17 PM
**/
#ifdef akifpathan
#include "debug.h"
#else
#include<bits/stdc++.h>
using namespace std;
#define debug(x...)
#endif
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
//mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
void solve()
{
string a,b,c;
cin>>a>>b>>c;
vector<char>vec;
for(char x: a) vec.push_back(x);
for(char x: b) vec.push_back(x);
for(char x: c) vec.push_back(x);
sort(vec.begin(),vec.end());
vec.erase(unique(vec.begin(),vec.end()),vec.end());
if(vec.size()>10)
{
cout<<"UNSOLVABLE";
return;
}
while(vec.size()<10) vec.push_back('*');
sort(vec.begin(),vec.end());
auto convert=[&](const string &x)
{
ll num=0;
if(vec[0]==x[0]) return LLONG_MIN;
for(auto it: x)
{
num*=10;
for(int i=0;i<10;i++) if(vec[i]==it)
{
num+=i;
break;
}
}
return num;
};
do
{
ll x=convert(a);
ll y=convert(b);
ll z=convert(c);
if(x+y==z)
{
cout<<x<<"\n"<<y<<"\n"<<z;
return;
}
}
while(next_permutation(vec.begin(),vec.end()));
cout<<"UNSOLVABLE";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int testcase=1;
//cin>>testcase;
for(int i=1;i<=testcase;i++)
{
//cout<<"Case "<<i<<": ";
solve();
}
#ifdef akifpathan
cerr<<"\nTime elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms\n";
#endif
return 0;
}
| #include <bits/stdc++.h>
int main(){
std::string s1, s2, s3;
std::cin >> s1 >> s2 >> s3;
std::map< int, int > mp;
std::vector< int > s1v(s1.size());
std::vector< int > s2v(s2.size());
std::vector< int > s3v(s3.size());
for(int i=0; i<s1.size(); i++){
auto itr = mp.find(s1[i] - 'a');
if(itr != mp.end()){
s1v[i] = mp[s1[i] - 'a'];
}else{
if(mp.size() == 10){
std::cout << "UNSOLVABLE" << std::endl;
return 0;
}
mp[s1[i] - 'a'] = mp.size();
s1v[i] = mp[s1[i] - 'a'];
}
}
for(int i=0; i<s2.size(); i++){
auto itr = mp.find(s2[i] - 'a');
if(itr != mp.end()){
s2v[i] = mp[s2[i] - 'a'];
}else{
if(mp.size() == 10){
std::cout << "UNSOLVABLE" << std::endl;
return 0;
}
mp[s2[i] - 'a'] = mp.size();
s2v[i] = mp[s2[i] - 'a'];
}
}
for(int i=0; i<s3.size(); i++){
auto itr = mp.find(s3[i] - 'a');
if(itr != mp.end()){
s3v[i] = mp[s3[i] - 'a'];
}else{
if(mp.size() == 10){
std::cout << "UNSOLVABLE" << std::endl;
return 0;
}
mp[s3[i] - 'a'] = mp.size();
s3v[i] = mp[s3[i] - 'a'];
}
}
std::deque< std::vector< int > > per_all;
std::vector< int > per(10);
for(int i=0; i<10; i++){
per[i] = i;
}
per_all.push_back(per);
for(int i=0; i<10; i++){
std::deque< std::vector< int > > tmp;
while(!per_all.empty()){
per = per_all.front(); per_all.pop_front();
tmp.push_back(per);
for(int j=i+1; j<10; j++){
std::vector< int > per_tmp = per;
std::swap(per_tmp[i], per_tmp[j]);
tmp.push_back(per_tmp);
}
}
per_all = tmp;
}
while(!per_all.empty()){
per = per_all.front(); per_all.pop_front();
if(per[s1v[0]] == 0 || per[s2v[0]] == 0 || per[s3v[0]] == 0){
continue;
}
long long n1 = 0, n2 = 0, n3 = 0;
for(int i=0; i<s1.size(); i++){
n1 = n1 * 10 + per[s1v[i]];
}
for(int i=0; i<s2.size(); i++){
n2 = n2 * 10 + per[s2v[i]];
}
for(int i=0; i<s3.size(); i++){
n3 = n3 * 10 + per[s3v[i]];
}
if(n1 + n2 == n3){
std::cout << n1 << std::endl;
std::cout << n2 << std::endl;
std::cout << n3 << std::endl;
return 0;
}
}
std::cout << "UNSOLVABLE" << std::endl;
return 0;
}
|
#include <bits/stdc++.h>
#define ll long long
#define MODV 1000000007 // 998244353
#define INFLL (1LL<<62)
#define EPS 1e-9
#define rep(i, n) for(ll i=0, i##_len=(ll)(n); i<i##_len; i++)
#define repf(i, n) for(ll i=1, i##_len=(ll)(n+1); i<i##_len; i++)
#define per(i, n) for(ll i=((ll)(n))-1; i>=0; i--)
#define all(v) v.begin(), v.end()
#define endl "\n"
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define Yes() cout << "Yes" << endl
#define YES() cout << "YES" << endl
#define No() cout << "No" << endl
#define NO() cout << "NO" << endl
__attribute__((constructor))
void init(){ std::ios::sync_with_stdio(0); std::cin.tie(0); std::cout<<std::fixed<<std::setprecision(15); }
template<class T>bool chmax(T &a, const T &b){ if(a<b){ a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b){ if(b<a){ a=b; return 1; } return 0; }
using namespace std;
int main(){
ll n, ans = 0;
cin >> n;
vi a(n), sa(n), ssa(n), sssa(n, 0);
map<ll, ll> cnt;
rep(i, n){
cin >> a[i];
cnt[(a[i] == 0 ? 0 : (a[i] <= -1 ? -1 : 1))]++;
sa[i] = a[i];
if(i != 0){
sa[i] += sa[i-1];
ssa[i] += ssa[i-1] + sa[i];
chmax(sssa[i], max(sssa[i-1], sa[i]));
}
else{
ssa[i] = a[i];
chmax(sssa[i], sa[i]);
}
}
// 全部合算でOK
if(cnt[1]+cnt[0] == n){
cout << ssa[n-1] << endl;
return 0;
}
else if(cnt[0] + cnt[-1] == n){
cout << 0 << endl;
return 0;
}
rep(i, n-1){
chmax(ans, ssa[i]);
chmax(ans, ssa[i] + sssa[i]);
}
chmax(ans, ssa[n-1]);
cout << ans << endl;
} | //#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
//#pragma GCC target("avx,avx2")
//#pragma GCC target("avx2")
//#pragma GCC optimize("O3")
//# include <x86intrin.h>
# 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;
template<typename T> using ordered_set = tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define _USE_MATH_DEFINES_
#define ll long long
#define ld long double
#define Accepted 0
#define pb push_back
#define mp make_pair
#define sz(x) (int)(x.size())
#define every(x) x.begin(),x.end()
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define For(i,x,y) for (ll i = x; i <= y; i ++)
#define FOr(i,x,y) for (ll i = x; i >= y; i --)
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
// ROAD to... Red
void setIn(string s) { freopen(s.c_str(),"r",stdin); }
void setOut(string s) { freopen(s.c_str(),"w",stdout); }
void setIO(string s = "") {
// cin.exceptions(cin.failbit);
// throws exception when do smth illegal
// ex. try to read letter into int
if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO
}
const double eps = 0.000001;
const ld pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const ll MOD = 1e18 + 9;
const ll INF = 1e18 + 123;
const int inf = 2e9 + 11;
const int mxn = 1e6 + 9;
const int N = 6e5+5;
const int M = 22;
const int pri = 997;
const int Magic = 2101;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
mt19937 gen(chrono::steady_clock::now().time_since_epoch().count());
int rnd (int l, int r) {
return uniform_int_distribution<int> (l, r)(gen);
}
void solve() {
int n;
cin >> n;
ll sum = 0;
ll cur = 0;
ll ans = 0, pref = 0;
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
sum += x;
pref = max(pref, sum);
ans = max(ans, cur+pref);
cur += sum;
}
cout << ans << '\n';
}
int main () {
SpeedForce;
int T = 1;
//cin >> T;
while(T--) solve();
return Accepted;
}
// B...a
|
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define all(a) a.begin(),a.end()
using namespace std;
using ll=long long;
using P=pair<int,int>;
template<typename T>
using v=vector<T>;
template<typename T>
using vv=vector<v<T>>;
template<typename T>
using vvv=vector<vv<T>>;
int main(){
int n;
cin>>n;
stack<int> l;
queue<int> r;
ll s=0;
rep(i,n){
int a;
cin>>a;
s+=a;
l.push(a);
}
rep(i,n){
int a;
cin>>a;
s+=a;
r.push(a);
}
priority_queue<int,vector<int>,greater<>> q;
ll a=0;
rep(i,n){
q.push(l.top());
q.push(r.front());
l.pop();
r.pop();
a+=q.top();
q.pop();
}
cout<<s-a<<endl;
}
| #include<bits/stdc++.h>
using namespace std;
const int N=4e5+7;
typedef long long LL;
struct str{
int v,x;
}p[N];
bool operator <(str a,str b){
return a.v>b.v;
}
set<int> S;
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)S.insert(i);
for(int i=1;i<=n;i++)scanf("%d",&p[i].v),p[i].x=n-i+1;
for(int i=1;i<=n;i++)scanf("%d",&p[n+i].v),p[n+i].x=i;
int tot=2*n;
sort(p+1,p+tot+1);
LL ans=0;
for(int i=1;i<=tot;i++){
str v=p[i];
if(S.upper_bound(v.x)!=S.begin()){
ans+=v.v;
S.erase(--S.upper_bound(v.x));
}
}
printf("%lld\n",ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxm=2e6+5;
char s[maxm];
char t[maxm];
int n;
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin>>n;
cin>>(s+1);
cin>>(t+1);
vector<int>pos;
for(int i=1;i<=n;i++){
s[i]-='0',t[i]-='0';
if(s[i])pos.push_back(i);
}
int len=pos.size();
int cur=0;
int ans=0;
for(int i=1;i<=n;i++){
while(cur<len&&pos[cur]<=i)cur++;
if(s[i]==t[i])continue;
if(cur>=len){
cout<<-1<<endl;
return 0;
}
ans+=pos[cur]-i;
s[pos[cur]]=0;
cur++;
}
cout<<ans<<endl;
return 0;
} | //ABC 182 C - To 3
//题目地址:https://atcoder.jp/contests/abc182/tasks/abc182_c
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
int main() {
int64_t n;
scanf("%" SCNd64, &n);
int cnt[3] = { 0 };
while (n) {
cnt[n % 10 % 3]++;
n /= 10;
}
int cur = (cnt[1] + 2 * cnt[2]) % 3;
int k = cnt[0] + cnt[1] + cnt[2];
int res;
if (!cur) res = 0;
else if (cur == 1) {
if (cnt[1]) {
if (k == 1) res = -1;
else res = 1;
} else {
if (k == 2) res = -1;
else res = 2;
}
} else {
if (cnt[2]) {
if (k == 1) res = -1;
else res = 1;
} else {
if (k == 2) res = -1;
else res = 2;
}
}
printf("%d\n", res);
return 0;
}
|
#pragma region RegionDefs
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0,i##_len=(n);i<i##_len;++i)
#define reps(i,l,r) for(int i=(l),i##_len=(r);i<i##_len;++i)
#define repr(i,l,r) for(int i=(r)-1,i##_left=(l);i>=i##_left;--i)
#define all(x) begin(x),end(x)
using namespace std;
typedef long long ll;
const ll INF = 1e9;
template<class T=ll> using V = vector<T>;
template<class T=ll> using PQ = priority_queue<T>;
template<class T=ll> using PQG = priority_queue<T, V<T>, greater<T>>;
const ll MOD = 1000000007LL;
void in() {}
template<class Head, class... Tail>
void in(Head&& head, Tail&&... tail) { cin >> head; in(move(tail)...); }
#define IN(...) ll __VA_ARGS__; in(__VA_ARGS__)
#define TIN(T, ...) T __VA_ARGS__; in(__VA_ARGS__)
#define VIN(T, v, n) V<T> v(n); for(auto& _elem:v)cin>>_elem
#define VIND(T, v, n) V<T> v(n); for(auto& _elem:v)cin>>_elem,--_elem
#define OUT(x) cout << (x);
#define OUTL(x) cout << (x) << endl;
template<class T>bool chmax(T &a, const T &b) {if (a<b) {a=b; return 1;} return 0;}
template<class T>bool chmin(T &a, const T &b) {if (a>b) {a=b; return 1;} return 0;}
template<class T>
string join(vector<T>& v, string delim="") { ostringstream os; rep(i,v.size())i?os<<delim<<v[i]:os<<v[0]; return os.str(); }
#pragma endregion RegionDefs
void solve()
{
TIN(string, s);
ll ans = 0;
rep(i, 9) {
if (s[i] == 'Z' && s[i+1] == 'O' && s[i+2] == 'N' && s[i+3] == 'e') ++ans;
}
OUTL(ans);
}
int main()
{
cin.tie(0); cout.tie(0);
ios::sync_with_stdio(false);
cout << setprecision(numeric_limits<double>::max_digits10);
solve();
return 0;
} | // #include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <cmath>
#include <iomanip>
#include <numeric>
#define ll long long int
#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)
#define drep(i, n) for(ll i = ((ll)(n) - 1); i >= 0; i--)
#define frep(i, m, n) for(ll i = (ll)(m); i < (ll)(n); i++)
#define vall(v) (v).begin(),(v).end()
#define mod 1000000007
#define inf 9223372036854775800
using namespace std;
//////////////////////////////////////////////////
//////////////////////////////////////////////////
//////////////////////////////////////////////////
int main() {
ll n;
cin >> n;
vector<ll> a(n);
rep(i,n){
cin >> a[i];
}
vector<ll> m(n,0);
ll ma = 0;
rep(i,n){
ma = max(ma,a[i]);
m[i] = ma;
}
vector<ll> s(n,0);
s[0] = a[0];
frep(i,1,n){
s[i] = s[i-1] + a[i];
}
frep(i,1,n){
s[i] += s[i-1];
}
// ll k = 0;
rep(i,n){
ll x = 0;
x = s[i] + (i+1) * m[i];
cout << x << endl;
}
return 0;
}
//////////////////////////////////////////////////
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void solve(long long N, std::vector<long long> A, std::vector<long long> B){
ll ma = 0;
ll mi = 1e9;
for(int i = 0;i < N;i++){
ma = max(A[i], ma);
mi = min(B[i], mi);
}
cout<<max(mi - ma + 1, 0LL)<<endl;
}
int main(){
long long N;
scanf("%lld",&N);
std::vector<long long> A(N);
for(int i = 0 ; i < N ; i++){
scanf("%lld",&A[i]);
}
std::vector<long long> B(N);
for(int i = 0 ; i < N ; i++){
scanf("%lld",&B[i]);
}
solve(N, std::move(A), std::move(B));
return 0;
}
| #include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)),x.end())
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
#define show(x) cerr<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,v(T),greater<T> >
#define bn(x) ((1<<x)-1)
#define dup(x,y) (((x)+(y)-1)/(y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef tuple<int,int,int> T;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<T> vt;
int getInt(){int x;scanf("%d",&x);return x;}
template<typename T>istream& operator>>(istream&i,v(T)&v){rep(j,sz(v))i>>v[j];return i;}
template<typename T>string join(const v(T)&v){stringstream s;rep(i,sz(v))s<<' '<<v[i];return s.str().substr(1);}
template<typename T>ostream& operator<<(ostream&o,const v(T)&v){if(sz(v))o<<join(v);return o;}
template<typename T1,typename T2>istream& operator>>(istream&i,pair<T1,T2>&v){return i>>v.fi>>v.se;}
template<typename T1,typename T2>ostream& operator<<(ostream&o,const pair<T1,T2>&v){return o<<v.fi<<","<<v.se;}
template<typename T>bool mins(T& x,const T&y){if(x>y){x=y;return true;}else return false;}
template<typename T>bool maxs(T& x,const T&y){if(x<y){x=y;return true;}else return false;}
template<typename T>ll suma(const v(T)&a){ll res(0);for(auto&&x:a)res+=x;return res;}
const double eps = 1e-10;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define dame { puts("-1"); return 0;}
#define yn {puts("Yes");}else{puts("No");}
const int MX = 200005;
// Mod int
const int mod = 998244353;
struct mint {
ll x;
mint():x(0){}
mint(ll x):x((x%mod+mod)%mod){}
// mint(ll x):x(x){}
mint& fix() { x = (x%mod+mod)%mod; return *this;}
mint operator-() const { return mint(0) - *this;}
mint operator~() const { return mint(1) / *this;}
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){ (x*=a.pow(mod-2).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 operator/(const mint& a)const{ return mint(*this) /= a;}
mint pow(ll t) const {
if(!t) return 1;
mint res = pow(t/2);
res *= res;
return (t&1)?res*x:res;
}
bool operator<(const mint& a)const{ return x < a.x;}
bool operator==(const mint& a)const{ return x == a.x;}
};
mint ex(mint x, ll t) { return x.pow(t);}
istream& operator>>(istream&i,mint&a){i>>a.x;return i;}
ostream& operator<<(ostream&o,const mint&a){o<<a.x;return o;}
typedef vector<mint> vm;
struct comb {
vm f, g;
comb(){}
comb(int mx):f(mx+1),g(mx+1) {
f[0] = 1;
rrep(i,mx) f[i] = f[i-1]*i;
g[mx] = f[mx].pow(mod-2);
for(int i=mx;i>0;i--) g[i-1] = g[i]*i;
}
mint operator()(int a, int b) {
if (a < b) return 0;
return f[a]*g[b]*g[a-b];
}
};
//
mint f(int a) {
return mint(a)*(a+1)/2;
}
int main() {
mint ans = 1;
rep(i,3) {
int a;
scanf("%d",&a);
ans *= f(a);
}
cout<<ans<<endl;
return 0;
}
|
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 100100;
const int mod = 1000000007;
ll v[100];
int main()
{
ll n;
cin >> n;
ll z = 1;
ll minn = n;
for(int i = 1; i <= 61; i++){
z = z*2;
if(n < z){
break;
}
ll k = n / z;
ll x = k + i + (n-k*z);
minn = min(minn,x);
}
cout << minn << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef pair <int, int> pin;
const int N = 4e5 + 5;
const int Maxn = 4e5;
const ll P = 998244353LL;
int n, m;
ll fac[N], ifac[N];
template <typename T>
inline void read(T &X) {
char ch = 0; T op = 1;
for (X = 0; ch > '9' || ch < '0'; ch = getchar())
if (ch == '-') op = -1;
for (; ch >= '0' && ch <= '9'; ch = getchar())
X = (X * 10) + ch - '0';
X *= op;
}
inline ll fpow(ll x, ll y) {
ll res = 1;
for (x %= P; y > 0; y >>= 1) {
if (y & 1) res = res * x % P;
x = x * x % P;
}
return res;
}
inline ll getc(int x, int y) {
return fac[x] * ifac[y] % P * ifac[x - y] % P;
}
inline ll solve(int now) {
vector <int> vec;
int tmp = now;
for (int i = 2; i * i <= now; i++) {
if (tmp % i == 0) {
int cnt = 0;
for (; tmp % i == 0; ++cnt, tmp /= i);
vec.push_back(cnt);
}
}
if (tmp != 1) vec.push_back(1);
ll res = 1;
for (int i = 0; i < vec.size(); i++) res = res * getc(vec[i] + n - 1, n - 1) % P;
return res;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("sample.in", "r", stdin);
clock_t st_clock = clock();
#endif
fac[0] = 1;
for (int i = 1; i <= Maxn; i++) fac[i] = fac[i - 1] * i % P;
ifac[Maxn] = fpow(fac[Maxn], P - 2);
for (int i = Maxn - 1; i >= 0; i--) ifac[i] = ifac[i + 1] * (i + 1) % P;
read(n), read(m);
ll ans = 0;
for (int i = 1; i <= m; i++) {
ans += solve(i);
if (ans >= P) ans -= P;
}
printf("%lld\n", ans);
#ifndef ONLINE_JUDGE
clock_t ed_clock = clock();
printf("time = %f ms\n", (double)(ed_clock - st_clock) / CLOCKS_PER_SEC * 1000);
#endif
return 0;
} |
#include <iostream>
#include <vector>
using namespace std;
template <class T>
T gcm(T a, T b) {
if (a < b) {
T w = a;
a = b;
b = w;
}
while (b != 0) {
T w = a % b;
a = b;
b = w;
}
return a;
}
int main() {
vector<long long> p = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47 };
int n;
cin >> n;
vector<long long> x(n);
for (int i = 0; i < n; i++) {
cin >> x[i];
}
long long res = 1;
for (int i = 0; i < static_cast<int>(p.size()); i++) {
res *= p[i];
}
for (int i = 1; i < (1 << p.size()); i++) {
long long q = 1;
for (int j = 0; j < static_cast<int>(p.size()); j++) {
if ((i & (1 << j)) > 0) {
q *= p[j];
}
}
bool r = true;
for (int j = 0; j < n; j++) {
r = r && gcm(q, x[j]) > 1;
}
if (r && res > q) {
res = q;
}
}
cout << res << endl;
}
| #include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=505;
int n;
int a[N][N];
int diff[N];
int ans_a[N],ans_b[N];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&a[i][j]);
bool flag=true;
for(int i=1;i<n;i++)
diff[i]=a[1][i+1]-a[1][i];
for(int i=2;i<=n;i++)
for(int j=1;j<n;j++)
if(diff[j]!=a[i][j+1]-a[i][j])
flag=false;
int mn=1<<30;
for(int i=1;i<=n;i++)
mn=min(mn,a[1][i]);
for(int i=1;i<=n;i++)
ans_b[i]=a[1][i]-mn;
for(int i=1;i<=n;i++)
{
ans_a[i]=(a[i][1]-a[1][1])+(a[1][1]-ans_b[1]);
if(ans_a[i]<0)
flag=false;
}
if(flag)
{
printf("Yes\n");
for(int i=1;i<=n;i++)
printf("%d%c",ans_a[i],i==n?'\n':' ');
for(int i=1;i<=n;i++)
printf("%d%c",ans_b[i],i==n?'\n':' ');
}
else
printf("No\n");
return 0;
} |
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<bool> vb;
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;}
int const INF = 1 << 30;
int main()
{
int n;
cin >> n;
vector<int> a(n);
rep(i,n) cin >> a[i];
int r = 0;
int ans = -1;
for(int x = 2; x <= 1100; ++x) {
int cnt = 0;
rep(i,n) {
if(a[i] % x == 0) cnt++;
}
if(chmax(r, cnt)) ans = x;
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
#define inf 0x3f3f3f3f3f3f3f3f
string s;
int dig[65];
ll pow(ll a, ll b) {
ll res = 1;
while(b) {
if(b & 1) res = res * a;
// if()
a = a * a;
}
return res;
}
int len;
ll m;
bool solve(ll r) {
ll sum = 0;
int f = 1;
for(int i = 0; i < len; i++) {
if(inf / r < sum) {
return false;
}
sum = sum * r + dig[i];
if(sum > m || sum < 0) {
f = 0;
break;
}
}
return f;
}
int main() {
cin >> s;
scanf("%lld", &m);
int d = 0;
len = s.size();
for(int i = 0; i < len; i++) {
d = max(d, s[i] - '0');
dig[i] = s[i] - '0';
}
if(len == 1) {
printf("%d\n", dig[0] <= m);
return 0;
}
if(!solve(d + 1)) {
printf("%d\n", 0);
return 0;
}
ll l = d + 1, r = 1e18, mid;
while(l < r) {
mid = (l + r + 1) >> 1ll;
if(solve(mid)) {
l = mid;
}
else {
r = mid - 1;
}
}
printf("%lld\n", l - d);
} |
#include <bits/stdc++.h>
using namespace std;
long long n,k,a[10][10];
vector<int>sol={1,2,3,4,5,6,7,8};
int ans=0;
void solve(int i){
long long tmp=0;
for(int i=0;i<n;i++){
tmp+=a[sol[i]][sol[i+1]];
}
tmp+=a[sol[n-1]][1];
if(tmp==k)ans+=1;
tmp=0;
while(next_permutation(sol.begin(),sol.begin()+n)){
if(sol[0]==1){
for(int i=0;i<n;i++){
tmp+=a[sol[i]][sol[i+1]];
}
tmp+=a[sol[n-1]][1];
if(tmp==k)ans+=1;
tmp=0;
}
}
}
int main() {
cin>>n>>k;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++) {
cin >> a[i][j];
}
}
solve(1);
cout<<ans;
return 0;
}
| #include<iostream>
#include<algorithm>
using namespace std;
const int N = 1e5 + 10;
int n, x;
int a[N];
int main(){
cin >> n >> x;
for(int i = 0; i < n; i++) cin >> a[i];
int i = 0, j = 0;
while(j < n){
if(a[j] == x) j++;
else a[i++] = a[j++];
}
for(int k = 0; k < i; k++) cout << a[k] << " ";
cout << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define PRINT(a) cout << (a) << endl
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define Fi first
#define Se second
#define debug(x) cerr << x << " " << "(L:" << __LINE__ << ")" << '\n';
using ll = long long int;
using P = pair<int,int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
template <typename T> using PQ = priority_queue<T>;
template <typename T> using minPQ = priority_queue<T, vector<T>, greater<T>>;
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 int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const int MOD = 1e9 + 7;
int main(){
int n,k;
cin >> n >> k;
vvi t(n, vi(n));
REP(i,n)REP(j,n)cin >> t[i][j];
vi a(n-1);
iota(ALL(a),1);
int ans=0;
do {
int d=0;
d += t[0][a[0]];
REP(i,n-2)d+=t[a[i]][a[i+1]];
d += t[a[n-2]][0];
if(d==k)ans++;
} while( next_permutation(a.begin(), a.end()) );
cout << ans << endl;
}
| //author-micheal corleone
//the godfather
#include <bits/stdc++.h>
#include <set>
#include<algorithm>
#include <set>
#include<algorithm>
#define ll long long int
#define in(n) scanf("%lld",&n)
#define in2(a,b) scanf("%lld %lld",&a,&b)
#define out(n) printf("%lld\n",n)
#define inf 999999999999999999
#define endl "\n"
#define precise(x) fixed << setprecision(x)
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
const int mod =1000000007;
#define f(i,a,n) for(int i=a;i<n;i++)
#define pb push_back
using namespace std;
bool sortcol( const vector<int>& v1,
const vector<int>& v2 ) {
return v1[1] < v2[1];
}
ll mod_pow(int a,int b,int M = mod)
{
int res = 1;
while(b)
{
if(b&1) res=(res*a)%M;
a=(a*a)%M;
b>>=1;
}
return res;
}
//ll power(ll x, ll y)
//{
// ll temp;
//if( y == 0)
// return 1;
//temp = power(x, y/2);
//temp=temp%MOD;
//if (y%2 == 0)
// return (temp%MOD)*(temp%MOD);
//else
// return (x%MOD)*(temp%MOD)*(temp%MOD);
//}
ll ncr(ll n,ll r){if(n==1 || r==0)return 1;if(r>n/2)r=n-r;
ll ans=1;for(ll i=1;i<=r;i++){ans=((ans * n )/i);n--;}return ans;
}
ll ncrsum(ll n,ll r, ll mod){
if(n==1 || r==0)return 1;
// ll ans=1,sum=1;for(ll i=1;i<=r;i++){ans=((((ans * n)%MOD )*modinv(i,MOD))%mod);n--;sum+=ans;sum%=mod;}return sum;
}
//diamond[2000005],ans[1000006];
const int maxn=3e5+100;
ll maximum=4000000;
vector<ll> primes;
void SieveOfEratosthenes()
{
vector<ll> prime(maximum+1,1);
// memset(prime, 1, sizeof(prime));
for (ll p=2; p*p<=maximum; p++)
{
// If prime[p] is not changed, then it is a prime
if (prime[p] == 1)
{
// Update all multiples of p greater than or
// equal to the square of it
// numbers which are multiple of p and are
// less than p^2 are already been marked.
for (ll i=p*p; i<=maximum; i += p)
prime[i] = 0;
}
}
for (ll p=2; p<=maximum; p++)
{
if (prime[p])
{
primes.pb(p);
}
}
}
ll a[20];
ll n,k;
ll timing[50][50];
ll answer=0;
ll final_answer=0;
void findPermutations(ll a[], ll n)
{
sort(a,a+n);
do {
answer=0;
answer=answer+timing[1][a[0]];
for(ll i=0;i<n-1;i++)
{
answer=answer+timing[a[i]][a[i+1]];
}
answer=answer+timing[a[n-1]][1];
if(answer==k) final_answer++;
//cout<<answer<<endl;
} while (next_permutation(a, a + n));
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll ta;
//cin>>ta;
//SieveOfEratosthenes();
ta=1;
for(ll testcase=0;testcase<ta;testcase++)
{
ll i1,i2,j,j1,a1,i31,i3,j2,i5,i4,i6,i7,i,m,t;
ll x,y,z,l;
ll total=0,cnt=0;
ll flag=0,flag1=0;
y=0;
ll res,res1,ans,res2,sum,curr;
ans=0,cnt=0;
res1=0,res=0,sum=0;
ll required,days,h,w;
ll s1=0;
ll k1,k2,k3,k4,p;
k1=0,k2=0,k3=0,k4=0;
//for(it=mp.begin();it!=mp.end(),it++)
//{
// for(int i=0;i<it->second.size();i++) cout<<it->second[i]<<" ";
//}
string s;
cin>>n>>k;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cin>>timing[i][j];
}
}
for(i=2;i<=n;i++)
{
a[s1]=i;
s1++;
}
findPermutations(a,n-1);
cout<<final_answer<<endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define ALL(x) ((x).begin()), ((x).end())
#define READ(x) (cin >> (x))
#define WRITE_N(x) (cout << (x) << endl)
#define WRITE(x) (cout << (x))
#define WRITE_YESNO(x) (WRITE_N((x) ? "Yes" : "No"))
#define PRECISE_COUT std::cout << std::setprecision(15) << std::fixed
bool xor_logic(bool x, bool y) { return (x && y) || (!x && !y); }
set<int> solve(set<int> A, set<int> B) {
set<int> retv;
set<int> set_u;
set_union(ALL(A), ALL(B), inserter(set_u, set_u.begin()));
set<int> set_i;
set_intersection(ALL(A), ALL(B), inserter(set_i, set_i.begin()));
set_difference(ALL(set_u), ALL(set_i), inserter(retv, retv.begin()));
return retv;
}
int main() {
// get values from input
cin.tie(0);
ios::sync_with_stdio(false);
int N, M;
set<int> A, B;
cin >> N >> M;
for (size_t i = 0; i < N; i++) {
int A_i;
cin >> A_i;
A.insert(A_i);
}
for (size_t i = 0; i < M; i++) {
int B_i;
cin >> B_i;
B.insert(B_i);
}
// main procedure
set<int> ans = solve(A, B);
// output
for (auto &&x : ans) {
cout << x << (x == *(ans.rbegin()) ? "\n" : " ");
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
const int N=105;
long long x;
int mem[N][N][N],vis[N][N][N],vid;
int arr[N];
int n,k;
int go(int ind,int rem,int remi)
{
if(ind>=n) return (rem==0&&remi==0?0:-(1<<30));
int &ret=mem[ind][rem][remi];
if(vis[ind][rem][remi]==vid) return ret;
vis[ind][rem][remi]=vid;
ret=max(arr[ind]+go(ind+1,(rem+k-arr[ind]%k)%k,remi-1),go(ind+1,rem,remi));
return ret;
}
int main()
{
scanf("%d %lld",&n,&x);
for(int i=0;i<n;i++) scanf("%d",&arr[i]);
long long ans=(1LL<<60);
for(int i=1;i<=n;i++)
{
vid++;
k=i;
long long res=go(0,x%i,i);
//cout << i << " " << res << endl;
if(res>0) ans=min(ans,(x-res)/k);
}
printf("%lld\n",ans);
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int r, c;
cin >> r >> c;
vector<vector<ll>> a(r, vector<ll>(c - 1)), b(r - 1, vector<ll>(c));
for (int i = 0; i < r; i++) {
for (int j = 0; j < c - 1; j++) {
cin >> a.at(i).at(j);
}
}
for (int i = 0; i < r - 1; i++) {
for (int j = 0; j < c; j++) {
cin >> b.at(i).at(j);
}
}
vector<vector<vector<ll>>> dp(r, vector<vector<ll>>(c, vector<ll>(2, 2e18)));
vector<ll> dp2(c, 2e18), dp3(c, 0);
dp[0][0][0] = 0;
dp2[0] = 0;
priority_queue<tuple<ll, ll, ll, ll>, vector<tuple<ll, ll, ll, ll>>, greater<tuple<ll, ll, ll, ll>>> pq;
pq.push(make_tuple(0, 0, 0, 0));
while (!pq.empty()) {
auto [dist, ix, iy, iz] = pq.top();
pq.pop();
if (dp[ix][iy][iz] < dist) continue;
if (iz == 0) {
if (iy < c - 1 && dp[ix][iy + 1][iz] > dp[ix][iy][iz] + a[ix][iy]) {
dp[ix][iy + 1][iz] = dp[ix][iy][iz] + a[ix][iy];
pq.push(make_tuple(dp[ix][iy + 1][iz], ix, iy + 1, iz));
}
if (iy > 0 && dp[ix][iy - 1][iz] > dp[ix][iy][iz] + a[ix][iy - 1]) {
dp[ix][iy - 1][iz] = dp[ix][iy][iz] + a[ix][iy - 1];
pq.push(make_tuple(dp[ix][iy - 1][iz], ix, iy - 1, iz));
}
if (ix < r - 1 && dp[ix + 1][iy][iz] > dp[ix][iy][iz] + b[ix][iy]) {
dp[ix + 1][iy][iz] = dp[ix][iy][iz] + b[ix][iy];
pq.push(make_tuple(dp[ix + 1][iy][iz], ix + 1, iy, iz));
}
if (dp[ix][iy][iz + 1] > dp[ix][iy][iz] + 1) {
dp[ix][iy][iz + 1] = dp[ix][iy][iz] + 1;
pq.push(make_tuple(dp[ix][iy][iz + 1] , ix, iy, iz + 1));
}
}
else {
if (ix > 0 && dp[ix - 1][iy][iz] > dp[ix][iy][iz] + 1) {
dp[ix - 1][iy][iz] = dp[ix][iy][iz] + 1;
pq.push(make_tuple(dp[ix - 1][iy][iz] , ix - 1, iy, iz));
}
if (dp[ix][iy][iz - 1] > dp[ix][iy][iz]) {
dp[ix][iy][iz - 1] = dp[ix][iy][iz];
pq.push(make_tuple(dp[ix][iy][iz - 1] , ix, iy, iz - 1));
}
}
}
cout << min(dp[r - 1][c - 1][0], dp[r - 1][c - 1][1]) << '\n';
} | #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < n; i++)
#define Rep(i,n) for(int i = 1; i <= n; i++)
#define sz(x) int(x.size())
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define YesorNo(a) printf(a ? "Yes\n" : "No\n")
#define debug(x) cerr << #x << ": " << x << endl;
#define fin(a) { cout << a << endl; return 0; }
#define endl '\n'
#define fi first
#define se second
using ll = long long;
using ld = long double;
using P = pair<int,int>;
using Pl = pair<ll,ll>;
template<class T> using V = vector<T>;
template<class T> using priq = priority_queue<T>;
template<class T> using priq_inv = priority_queue<T, vector<T>, greater<T>>;
const int dx[] = {0,1,0,-1,1,1,-1,-1};
const int dy[] = {1,0,-1,0,1,-1,-1,1};
ll ceil(const ll &a, const ll &b){return ((a)+(b)-1)/b;}
template<class T> bool chmin(T &a, T b) { if(a > b) { a = b; return true; } return false; }
template<class T> bool chmax(T &a, T b) { if(a < b) { a = b; return true; } return false; }
struct INF { template<class T> operator T() { return numeric_limits<T>::max() / 2; } } INF;
template<class T, class U> istream& operator>>(istream &in, pair<T,U> &p) {
return in >> p.first >> p.second;
}
template<class T, class U> ostream& operator<<(ostream &out, const pair<T,U> &p) {
return out << '{' << p.first << ',' << p.second << '}';
}
template<class T, class U> pair<T,U> operator+(const pair<T,U> &l, const pair<T,U> &r) {
return make_pair(l.first+r.first, l.second+r.second);
}
template<class T, class U> pair<T,U> operator-(const pair<T,U> &l, const pair<T,U> &r) {
return make_pair(l.first-r.first, l.second-r.second);
}
template<class T, class U> pair<T,U> operator*(const pair<T,U> &l, const pair<T,U> &r) {
return make_pair(l.first*r.first, l.second*r.second);
}
template<class T, class U> pair<T,U> operator/(const pair<T,U> &l, const pair<T,U> &r) {
return make_pair(l.first/r.first, l.second/r.second);
}
template<class T, class U> pair<T,U>& operator+=(pair<T,U> &l, const pair<T,U> &r) { return l = l+r; }
template<class T, class U> pair<T,U>& operator-=(pair<T,U> &l, const pair<T,U> &r) { return l = l-r; }
template<class T, class U> pair<T,U>& operator*=(pair<T,U> &l, const pair<T,U> &r) { return l = l*r; }
template<class T, class U> pair<T,U>& operator/=(pair<T,U> &l, const pair<T,U> &r) { return l = l/r; }
template<class T>
istream& operator>>(istream &in, vector<T> &v) {
for(auto &&e : v) in >> e;
return in;
}
template<class T>
ostream& operator<<(ostream &out, const vector<T> &v) {
for(const auto &e : v) out << e << ' ';
return out;
}
/*----------------------------------------------------------------------------------------------------*/
int main(){
int n;
cin >> n;
V<Pl> ps(n);
cin >> ps;
int m;
cin >> m;
V<V<ll>> xs(m+1,V<ll>(3)), ys(m+1,V<ll>(3)); // vector = {a, b, c}, point = ax + by + c
xs[0] = {1,0,0};
ys[0] = {0,1,0};
Rep(i,m) {
int ty;
cin >> ty;
if(ty == 1) {
xs[i] = ys[i-1];
ys[i] = { -xs[i-1][0], -xs[i-1][1], -xs[i-1][2] };
}
if(ty == 2) {
ys[i] = xs[i-1];
xs[i] = { -ys[i-1][0], -ys[i-1][1], -ys[i-1][2] };
}
if(ty == 3) {
ll p; cin >> p;
ys[i] = ys[i-1];
xs[i] = { -xs[i-1][0], -xs[i-1][1], -xs[i-1][2]+2*p };
}
if(ty == 4) {
ll p; cin >> p;
xs[i] = xs[i-1];
ys[i] = { -ys[i-1][0], -ys[i-1][1], -ys[i-1][2]+2*p };
}
}
int q;
cin >> q;
V<Pl> anss(q);
rep(i,q) {
int a, b;
cin >> a >> b;
b--;
ll x = ps[b].fi;
ll y = ps[b].se;
ll ansx = xs[a][0]*x + xs[a][1]*y + xs[a][2];
ll ansy = ys[a][0]*x + ys[a][1]*y + ys[a][2];
anss[i] = make_pair(ansx, ansy);
}
for(Pl ans : anss) {
cout << ans.fi << ' ' << ans.se << endl;
}
} |
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int n,m,tot,head[N];
long long dis[N];
bool vis[N];
struct Star
{
int to,nxt,c,d;
}edge[2*N];
void Lian(int x,int y,int z,int w)
{
tot++;
edge[tot].c=z;
edge[tot].d=w;
edge[tot].nxt=head[x];
edge[tot].to=y;
head[x]=tot;
}
struct Point
{
long long dd;
int p;
bool operator<(const Point &x)const{
return dd>x.dd;
}
};
void dijkstra()
{
for(int i=1;i<=n;i++) dis[i]=1e18;
Point a;
a.dd=0;
a.p=1;
dis[1]=0;
priority_queue<Point> q;
q.push(a);
while(!q.empty())
{
a=q.top();
q.pop();
int x=a.p;
if(vis[x]) continue;
vis[x]=1;
for(int i=head[x];i;i=edge[i].nxt)
{
long long val;
int y=edge[i].to;
int k=sqrt(edge[i].d);
if(dis[x]<=k) val=0ll+k+(edge[i].d/(k+1))+edge[i].c;
else val=0ll+dis[x]+(edge[i].d/(dis[x]+1))+edge[i].c;
if(val<dis[y])
{
dis[y]=val;
a.dd=dis[y],a.p=y;
q.push(a);
}
}
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int a,b,c,d;
scanf("%d%d%d%d",&a,&b,&c,&d);
Lian(a,b,c,d);
Lian(b,a,c,d);
}
dijkstra();
if(dis[n]==1e18) puts("-1");
else printf("%lld",dis[n]);
return 0;
} | /* -*- coding: utf-8 -*-
*
* e.cc: E - Rush Hour 2
*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
using namespace std;
/* constant */
const int MAX_N = 100000;
const int MAX_M = 100000;
const long long LINF = 1LL << 62;
/* typedef */
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<pii> vpii;
typedef pair<ll,int> pli;
/* global variables */
vpii nbrs[MAX_N];
int cs[MAX_M], ds[MAX_M], mts[MAX_M];
ll ts[MAX_N];
/* subroutines */
ll edget(ll t, int ci, int di) { return t + ci + di / (t + 1); }
ll calcmint(ll t, int ei) {
int ci = cs[ei], di = ds[ei];
return edget(max(t, (ll)mts[ei]), ci, di);
}
/* main */
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < m; i++) {
int a, b;
scanf("%d%d%d%d", &a, &b, cs + i, ds + i);
a--, b--;
nbrs[a].push_back(pii(b, i));
nbrs[b].push_back(pii(a, i));
mts[i] = round(sqrt(ds[i])) - 1;
}
fill(ts, ts + n, LINF);
ts[0] = 0;
priority_queue<pli> q;
q.push(pli(0, 0));
while (! q.empty()) {
pli u = q.top(); q.pop();
ll ut = -u.first;
int ui = u.second;
if (ts[ui] != ut) continue;
if (ui == n - 1) break;
for (auto ve: nbrs[ui]) {
int vi = ve.first, ei = ve.second;
ll vt = calcmint(ut, ei);
if (ts[vi] > vt) {
ts[vi] = vt;
q.push(pli(-vt, vi));
}
}
}
ll gt = ts[n - 1];
printf("%lld\n", (gt >= LINF) ? -1LL : gt);
return 0;
}
|
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
return (ull)rng() % B;
}
// 1-indexed
template<typename T>
struct BIT{
int n;
vector<T> bit;
BIT(int n_=0):n(n_),bit(n+1){}
T sum(int i){
T res=0;
for(;i>0;i-=(i&-i))res+=bit[i];
return res;
}
void add(int i,T a){
if(i==0)return;
for(;i<=n;i+=(i&-i)){bit[i]+=a;}
}
int lower_bound(T k){ // k<=sum(res)
if(k<=0)return 0;
int res=0,i=1;
while((i<<1)<=n)i<<=1;
for(;i;i>>=1){
if(res+i<=n&&bit[res+i]<k)k-=bit[res+=i];
}
return res+1;
}
};
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n; cin >> n;
vector<int> a(n),b(n);
for(int i=0;i<n;i++){
cin >> a[i];
}
map<int,vector<int>> mp;
for(int i=n-1;i>=0;i--){
mp[a[i]+i].push_back(i);
}
for(int i=0;i<n;i++){
cin >> b[i];
if(mp[b[i]+i].size()==0){
printf("-1\n");
return 0;
}
int id=mp[b[i]+i].back();
mp[b[i]+i].pop_back();
b[i]=id;
}
BIT<int> bit(n);
ll res=0;
for(int i=0;i<n;i++){
res+=i-bit.sum(b[i]+1);
bit.add(b[i]+1,1);
}
cout << res << endl;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll; //int:2*10**9
typedef long double ld;
typedef pair<ll,ll> P;
#define REP(i,n) for(ll i = 0; i<(ll)(n); i++)
#define FOR(i,a,b) for(ll i=(a);i<=(b);i++)
#define FORD(i,a,b) for(ll i=(a);i>=(b);i--)
#define pb push_back
#define MOD 1000000007 //998244353
#define PI 3.141592653
#define INF 100000000000000 //14
//cin.tie(0);cout.tie(0);ios::sync_with_stdio(false);
ll seg[1ll<<20];
void set_value(ll q, ll sl=0, ll sr=1ll<<19, ll pos=1) {
if (q<sl || sr<=q) return;
seg[pos]++;
if (pos>=(1ll<<19)) return;
ll sm = (sl+sr)/2;
set_value(q,sl,sm,pos*2);
set_value(q,sm,sr,pos*2+1);
return;
}
ll get_cnt(ll ql, ll qr, ll sl=0, ll sr=1ll<<19, ll pos=1) {
if (qr<=sl || sr<=ql) return 0;
if (ql<=sl && sr<=qr) return seg[pos];
ll sm = (sl+sr)/2;
ll lsum = get_cnt(ql,qr,sl,sm,pos*2);
ll rsum = get_cnt(ql,qr,sm,sr,pos*2+1);
return lsum+rsum;
}
int main(){
ll n; cin >> n;
vector<ll> a(n);
vector<ll> b(n);
REP(i,n) {cin >> a[i];a[i]+=i;}
REP(i,n) {cin >> b[i];b[i]+=i;}
vector<ll> as = a;
vector<ll> bs = b;
sort(as.begin(),as.end());
sort(bs.begin(),bs.end());
REP(i,n) {
if (as[i]!=bs[i]) {
cout << -1 << endl;
return 0;
}
}
vector<P> ap(n);
vector<P> bp(n);
REP(i,n) {ap[i]={a[i],i}; bp[i]={b[i],i};}
sort(ap.begin(),ap.end());
sort(bp.begin(),bp.end());
vector<P> t(n);
REP(i,n) {
t[i]={ap[i].second+1ll,bp[i].second+1ll};
}
sort(t.begin(),t.end());
// REP(i,n) cout << t[i].first << " " << t[i].second << endl;
ll ans = 0;
REP(i,n) {
ll val = t[i].second;
set_value(val);
ans += get_cnt(val+1,1ll<<19);
}
cout << ans << endl;
return 0;
} |
#include <bits/stdc++.h>
// #include <atcoder/all>
#include <cmath>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
const ll INFI = 1001001001;
const ll INFL = 1001001001001001001;
const ll MOD = (int)1e9 + 7;
const double EPS = 1e-9;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
typedef pair<ll, ll> P;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, in, fn) for (ll i = in; i <= fn; i++)
#define debug(x) cerr << #x << ": " << x << endl
void sort2vectors(vector<ll> &av, vector<ll> &bv)
{
int n = av.size();
vector<ll> p(n), av2(n), bv2(n);
iota(p.begin(), p.end(), 0);
sort(p.begin(), p.end(), [&](ll a, ll b) { return av[a] < av[b]; });
for (int i = 0; i < n; i++)
{
av2[i] = av[p[i]];
bv2[i] = bv[p[i]];
}
av = av2;
bv = bv2;
}
int main()
{
long long K;
scanf("%lld", &K);
long long N;
scanf("%lld", &N);
long long M;
scanf("%lld", &M);
std::vector<long long> A(K), A2(K), num(K);
std::vector<long long> B(K), sa(K);
for (int i = 0; i < K; i++)
{
scanf("%lld", &A[i]);
}
ll sum = 0;
rep(i, K)
{
A2[i] = A[i];
num[i] = i;
B[i] = M * A[i] / N;
sum += B[i];
//debug(B[i]);
sa[i] = (llabs((B[i] + 1) * N - A[i] * M)) - (llabs(B[i] * N - A[i] * M));
//debug(sa[i]);
}
ll amari = M - sum;
sort2vectors(sa, num);
rep(i, amari)
{
B[num[i]]++;
}
rep(i, K)
{
cout << B[i] << " ";
}
cout << endl;
return 0;
}
| #include <bits/stdc++.h>
template <class T>
inline void read(T &x) {
static char ch;
static bool opt;
while (!isdigit(ch = getchar()) && ch != '-');
x = (opt = ch == '-') ? 0 : ch - '0';
while (isdigit(ch = getchar()))
x = x * 10 + ch - '0';
if (opt)
x = ~x + 1;
}
template <class T>
inline void putint(T x) {
static char buf[45], *tail = buf;
if (!x)
putchar('0');
else {
if (x < 0)
putchar('-'), x = ~x + 1;
for (; x; x /= 10) *++tail = x % 10 + '0';
for (; tail != buf; --tail) putchar(*tail);
}
}
template <class T>
inline bool tense(T &x, const T &y) {
return y < x ? x = y, true : false;
}
template <class T>
inline bool relax(T &x, const T &y) {
return x < y ? x = y, true : false;
}
template <class T>
inline T getAbs(const T &x) {
return x < 0 ? -x : x;
}
typedef long long s64;
typedef long double ld;
typedef std::vector<int> vi;
typedef std::pair<int, int> pii;
#define mp(x, y) std::make_pair(x, y)
using std::cerr;
const int MaxN = 1e5 + 5;
const s64 INFs64 = 1LL << 60;
int n, m, K, a[MaxN], b[MaxN];
s64 pre[MaxN][2];
inline bool check(s64 mid) {
for (int i = 1; i <= K; ++i) {
s64 l = 0, r = m;
tense(r, (1LL * a[i] * m + mid) / n);
if (1LL * a[i] * m > mid)
relax(l, (1LL * a[i] * m - mid - 1) / n + 1);
pre[i][0] = pre[i - 1][0] + l;
pre[i][1] = pre[i - 1][1] + r;
// cerr << l << ' ' << r << '\n';
}
return m >= pre[K][0] && m <= pre[K][1];
}
inline s64 findBest() {
s64 l = 0, r = 1e18, mid, res = 1e18;
while (l <= r) {
mid = (l + r) >> 1;
if (check(mid))
res = mid, r = mid - 1;
else
l = mid + 1;
}
return res;
}
int main() {
#ifdef orzczk
freopen("a.in", "r", stdin);
#endif
read(K), read(n), read(m);
for (int i = 1; i <= K; ++i) {
read(a[i]);
}
s64 best = findBest();
// cerr << best << '\n';
check(best);
for (int i = K, cur = m; i >= 1; --i) {
s64 l = 0, r = m;
tense(r, (1LL * a[i] * m + best) / n);
if (1LL * a[i] * m > best)
relax(l, (1LL * a[i] * m - best - 1) / n + 1);
tense(r, cur - pre[i - 1][0]);
relax(l, cur - pre[i - 1][1]);
b[i] = l;
cur -= b[i];
}
for (int i = 1; i <= K; ++i) {
printf("%d%c", b[i], " \n"[i == K]);
}
return 0;
} |
#include <bits/stdc++.h>
//#include <cmath>
typedef long long ll;
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define mod 1000000007
//#define lld long double
#define mii map<int, int>
#define mci map<char, int>
#define msi map<string, int>
#define pii pair<int, int>
#define ff first
#define ss second
#define bs(yup,x) binary_search(yup.begin(),yup.end(),x) //it will return bollean value
#define lb(yup,x) lower_bound(yup.begin(),yup.end(),x) //it will return the adress of the number which is equal to or just greater than x ,and if it is equal to yup.end() than their in no such number exist
#define ub(yup,x) upper_bound(yup.begin(),yup.end(),x)
#define all(x) (x).begin(), (x).end()
#define rep(i,x,y) for(int i=x; i<y; i++)
#define fill(a,b) memset(a, b, sizeof(a))
#define vi vector<int>
#define setbits(x) __builtin_popcountll(x)
using namespace std;
const long long N=1000001,INF=2000000000000000000;
const long double EPS= 0.000000000001;
// calculate a^b%p
ll power(ll a, ll b, ll p)
{
if(a==0)
return 0;
ll res=1;
//a%=p;
while(b>0)
{
if(b&1)
res=(res*a)%p;
b>>=1;
a=(a*a)%p;
}
return res;
}
ll modinverse(ll n,ll p){
return power(n,p-2,p);
}
ll nCr(ll n,ll r,ll p){
ll f[n+1];
f[0] = 1;
for(ll i=1;i<=n;i++){
f[i] = f[i-1]*i%p;
}
return (f[n]%p * modinverse(f[r],p)%p * modinverse(f[n-r],p)%p)%p;
}
int main() {
IOS;
//fill(isprime, true);
//pre();
int t;
//cin>>t;
t=1;
while(t--){
int n;
cin>>n;
int a[1005] = {0};
for(int i=0;i<n;i++){
int x;
cin>>x;
a[x] = 1;
}
int f=0;
for(int i=1;i<=n;i++){
if(a[i]==0){
f=1;
cout<<"No"<<endl;
break;
}
}
if(!f){
cout<<"Yes"<<endl;
}
}
return 0;
} | /* -> Written by <-
-----------
|K_A_Z_A_M_A|
|___________|
| ___ |
| (^_^) |
| /( | )\ |
|____|_|____|
H O A N G
*/
#include <bits/stdc++.h>
#define Task ""
#define F first
#define S second
#define eb emplace_back
#define bit(x, i) ((x >> (i)) & 1)
#define debug(x) cerr << #x << " is " << x << "\n";
//#define int ll
using namespace std;
using ll = long long;
const int MOD = 1e9 + 7;
const int base = 131;
const int B = 32;
const int inf = 1e9 + 7;
const ll INF = 1e18 + 7;
const int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
const int dy8[] = {1, -1, 0, 1, -1, 0, 1, -1};
const int dx4[] = {0, 0, 1, -1};
const int dy4[] = {1, -1, 0, 0};
int n;
int a[100001];
int f[2][100001];
int g[2][100001];
void solve(int test_case) {
cin >> n;
for (int i = 1; i <= n; ++ i)
cin >> a[i];
f[0][1] = 0;
f[1][1] = a[1];
g[0][1] = 0;
g[1][1] = 1;
for (int i = 2; i <= n; ++ i) {
g[0][i] = g[1][i-1];
f[0][i] = ((ll)MOD * MOD + f[1][i-1] - (((ll)a[i] * g[0][i]) % MOD)) % MOD;
g[1][i] = (g[0][i-1] + g[1][i-1]) % MOD;
f[1][i] = ((ll)MOD * MOD + f[1][i-1] + f[0][i-1] + (((ll)a[i] * g[1][i]) % MOD)) % MOD;
}
// cout << f[1][1] << "\n";
cout << (f[1][n] + f[0][n]) % MOD;
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
if (fopen(".inp", "r")) {
freopen(".inp", "r", stdin);
freopen(".out", "w", stdout);
}
else if (fopen(Task".inp", "r")) {
freopen(Task".inp", "r", stdin);
freopen(Task".out", "w", stdout);
}
int test_case = 1;
// cin >> test_case;
// prepare();
for (int i = 1; i <= test_case; ++ i) {
solve(i);
// bf();
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
struct node{long long x,y;}f[200002];
long long n,a=0,b=0,cnt=0;
bool cmp(node x,node y){return 2*x.x+x.y>2*y.x+y.y;}
int main(){
scanf("%lld",&n);
for(int i=0;i<n;++i)scanf("%lld%lld",&f[i].x,&f[i].y),a+=f[i].x;
sort(f,f+n,cmp);
while(b<=a){b+=f[cnt].x+f[cnt].y;a-=f[cnt].x;++cnt;}
printf("%lld",cnt);
return 0;
} | #include <bits/stdc++.h>
typedef long long ll;
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int INF = 1e9;
const int MOD = 1e9+7;
const ll LINF = 1e18;
#define REP(i,n) for(int i=0;i<(n);++i)
#define COUT(x) cout<<(x)<<"\n"
#define COUT16(x) cout << fixed << setprecision(16) << (x) << "\n";
int main(){
int n;cin >> n;
ll a[n],b[n];
ll sum_a = 0;
vector<ll> sum_ad;
REP(i,n){
cin >> a[i] >> b[i];
sum_a += a[i];
sum_ad.push_back(2*a[i]+b[i]);
}
sort(sum_ad.begin(),sum_ad.end(),greater<ll>());
ll count = 0,sum=0;
REP(i,n){
sum += sum_ad[i];
count++;
if(sum>sum_a){
COUT(count);
return 0;
}
}
} |
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
typedef int _loop_int;
#define REP(i,n) for(_loop_int i=0;i<(_loop_int)(n);++i)
#define FOR(i,a,b) for(_loop_int i=(_loop_int)(a);i<(_loop_int)(b);++i)
#define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1;i>=(_loop_int)(a);--i)
#define DEB(x) cout << #x << " " << x << endl;
#define DEB_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl
#define ALL(a) (a).begin(),(a).end()
#define CHMIN(a,b) a=min((a),(b))
#define CHMAX(a,b) a=max((a),(b))
typedef long long int LL;
typedef pair<int, int> PI;
typedef pair<LL, LL> PLL;
typedef vector<int> VI;
typedef vector<LL> VL;
struct bySum{
bool operator() (PI a, PI b) {
return 2*(LL)a.first+a.second > 2*(LL)b.first+b.second;
}
};
int main() {
IOS
int n;
cin >> n;
int a[n], t[n];
REP(i, n)
cin >> a[i] >> t[i];
LL suma = 0;
REP(i, n)
suma += a[i];
PI at[n];
REP(i, n)
at[i] = {a[i], t[i]};
sort(at, at+n, bySum());
// DEB(suma);
// REP(i, n)
// cout << at[i] << "," << at[i].second << " ";
// cout << endl;
int c = 0, ai = 0;
LL sumt = 0;
while (suma >= sumt) {
sumt += (LL)at[ai].first + at[ai].second;
suma -= at[ai].first;
ai++;
// DEB(suma);
// DEB(sumt);
c++;
}
cout << c;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
using ll = long long;
bool cmp(pair<ll,ll> a, pair<ll, ll> b){
return (2*a.first+a.second) > (2*b.first+b.second);
}
int main(){
ll n, i, ans=0, ak=0, x, y;
cin>>n;
vector<pair<ll, ll>> vt;
for(i=0;i<n;i++){
cin>>x>>y;
vt.push_back({x, y});
ak+=x;
}
sort(vt.begin(), vt.end(), cmp);
for(i=0;i<n;i++){
ans+=vt[i].first+vt[i].second;
ak-=vt[i].first;
if(ans>ak){
cout<<i+1;
return 0;
}
}
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ld = long double;
int main() {
ld a, b, c;
cin >> a >> b >> c;
ll X = round(a * 10000);
ll Y = round(b * 10000);
ll R = round(c * 10000);
vector<string> str;
ll ans = 0;
ll x_s = ceil((X-R)/10000.0)*10000;
ll x_e = floor((X+R)/10000.0)*10000;
for (int i=x_s; i<=x_e; i+=10000) {
ld rt = sqrtl( (ld)R*(ld)R - ((ld)i-(ld)X)*((ld)i-(ld)X) );
ll j_large = floor(((ld)Y+ rt )/10000.0)*10000;
ll j_small = ceil(((ld)Y- rt )/10000.0)*10000;
ans += (j_large - j_small) /10000 + 1;
}
cout << ans << endl;
return 0;
} | // {{{ by unolight
#include <bits/stdc++.h>
#include <unistd.h>
#pragma GCC diagnostic ignored "-Wunused-result"
#pragma GCC diagnostic ignored "-Wunused-function"
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x),end(x)
#define RALL(x) rbegin(x),rend(x)
#define REP(i,n) for ( int i=0; i<int(n); i++ )
#define REP1(i,a,b) for ( int i=(a); i<=int(b); i++ )
#define MP make_pair
#define PB push_back
using namespace std;
typedef int64_t LL;
typedef pair<int,int> PII;
typedef vector<int> VI;
namespace { namespace unolight {
// Read Input
template<class T> void _R( T &x ) { cin>>x; }
void _R( int &x ) { scanf("%d",&x); }
void _R( int64_t &x ) { scanf("%" PRId64,&x); }
void _R( double &x ) { scanf("%lf",&x); }
void _R( char &x ) { scanf(" %c",&x); }
void _R( char *x ) { scanf("%s",x); }
void R() {}
template<class T, class... U> void R( T& head, U&... tail ) { _R(head); R(tail...); }
// Write Output
template<class T> void _W( const T &x ) { cout<<x; }
void _W( const int &x ) { printf("%d",x); }
void _W( const int64_t &x ) { printf("%" PRId64,x); }
void _W( const double &x ) { printf("%.16f\n",x); }
void _W( const char &x ) { putchar(x); }
void _W( const char *x ) { printf("%s",x); }
template<class T> void _W( const vector<T> &x ) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W( const T& head, const U&... tail ) { _W(head); putchar(sizeof...(tail)?' ':'\n'); W(tail...); }
#ifdef UNOLIGHT
#include "dump.hpp"
#else
#define dump(...)
#endif
template<class T> inline bool chmax( T &a, const T &b ) { return b>a ? a=b,true : false; }
template<class T> inline bool chmin( T &a, const T &b ) { return b<a ? a=b,true : false; }
template<class T> using MaxHeap = priority_queue<T>;
template<class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;
template<class T, class F=less<T>> void sort_uniq( vector<T> &v, F f=F() ) {
sort(begin(v),end(v),f);
v.resize(unique(begin(v),end(v))-begin(v));
}
// }}}
// 1 unit is 10000
const int K=10000;
LL x,y,r;
void input() {
double a,b,c;
R(a,b,c);
x=round(abs(a)*K);
y=round(abs(b)*K);
r=round(abs(c)*K);
}
bool in( LL a, LL b ) {
return (a-x)*(a-x)+(b-y)*(b-y)<=r*r;
}
LL solve( LL lim ) {
LL l=0,r=1;
LL res=0;
for ( int j=(int)(1e9)+K; j>=lim; j-=K ) {
while ( in(l*K,j) ) l--;
while ( in(r*K,j) ) r++;
res+=r-l-1;
}
return res;
}
void main() {
input();
// make center between 0 ~ 1
x%=K;
y%=K;
LL ans=0;
// solve upper part
ans+=solve(K);
// solve bottom part
y=-y; // let bottom part be upper
ans+=solve(0);
W(ans);
}
// {{{ main
}}
int main() { unolight::main(); return 0; }
// }}}
|
#include <iostream>
using namespace std;
int main() {
string str;
cin >> str;
int len = str.length();
for(int i = 0; i < len; i++) {
if(str[i] == '.') {
break;
} else {
cout << str[i];
}
}
cout << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
/*ll t ;
cin>>t;
while(t --) {
} */
ll n;
cin>>n;
if(n % 100 == 0) {
cout<<n / 100<<"\n";
}
else {
cout <<(n / 100) + 1<<"\n";
}
}
|
#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define ll long long
#define pp pair<ll, ll>
#define lb lower_bound
#define ub upper_bound
#define mk make_pair
#define pb push_back
#define llp(i, x, n) for (ll i = x; i < n; i++)
#define fr first
#define ss second
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define ull unsigned long long ll
#define mod 1000000007
using namespace std;
int main()
{
fast;
ll n;
cin>>n;
ll a[n];
llp(i,0,n)
cin>>a[i];
sort(a,a+n);
ll b[n];
b[n-1]=a[n-1];
for(int i=n-2;i>=0;i--)
{
b[i]=a[i]+b[i+1];
}
ll sum=0;
for(ll i=0;i<n-1;i++)
{
sum+=b[i+1]-(n-i-1)*a[i];
}
cout<<sum<<endl;
} | #include <algorithm>
#include <iostream>
#include <vector>
int main() {
size_t n_num;
std::cin >> n_num;
std::vector<int64_t> nums(n_num);
for (auto &ele : nums) {
std::cin >> ele;
}
std::sort(nums.begin(), nums.end());
int64_t ans = 0;
int64_t delta = 0;
for (size_t i = 0; i < n_num; i++) {
delta += nums[i] - nums[0];
}
ans += delta;
for (size_t i = 1; i < n_num; i++) {
delta -= (nums[i] - nums[i - 1]) * (n_num - i);
ans += delta;
}
std::cout << ans << std::endl;
}
|
#include<bits/stdc++.h>
#define HAS_TEST_CASE 0
namespace rG{
typedef long long ll;
const int N=5e5;
int n,m,k,a[N+10];
char s[N+10];
inline void main(){
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",2*a+100-b);
}
} // namespace rG
int main(){
#if defined(LOCAL)&&HAS_TEST_CASE
freopen("tmp.out","w",stdout);
#endif
#if HAS_TEST_CASE
int T;scanf("%d",&T);
while(T--)rG::main();
#else
rG::main();
#endif
return 0;
}
| #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
//template
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(v) (v).begin(),(v).end()
using ll=long long int;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
//end
void solve(int _rot){
// printf("Case #%d: ",_rot);
int n;
cin>>n;
vector<int> a(n);
rep(i,0,n)cin>>a[i];
if(n&1)puts("Second");
else{
map<int,int> cnt;
rep(i,0,n)cnt[a[i]]++;
for(auto& [tmp,c]:cnt)if(c&1)goto out;
puts("Second");
return;
out:;
puts("First");
}
}
int main(){
int t; scanf("%d",&t);
rep(rot,0,t)solve(rot+1);
return 0;
} |
//include
//------------------------------------------
#include <string>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <complex>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <ctime>
// namespace
using namespace std;
// type alias
using ll = long long;
using ull = unsigned long long;
using comp = complex<double>;
// constant
static const ll MOD = 998244353LL;
// static const ll MOD = (1LL << 61LL) - 1LL;
static const double PI = 3.14159265358979323846;
// conversion
inline ll toint(string s) { ll v; istringstream sin(s); sin >> v; return v; }
template<class t> inline string tostring(t x) { ostringstream sout; sout << x; return sout.str(); }
// print
#define RET(x) return cout << x << endl, 0;
// for loop
#define REP(i, a, b) for ((i) = (ll)(a);(i) < (ll)(b);(i)++)
#define REPD(i, a, b) for (ll i = (ll)(a);(i) < (ll)(b);(i)++)
#define REPI(v, vs) for (auto& v : vs)
//debug
#ifdef LOCAL_ENV
#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x) cerr << #x << " = " << (x) << " (l" << __LINE__ << ")" << " " << __FILE__ << endl
#else
#define DUMP(x)
#define DEBUG(x)
#endif
#define MAX_VALUE 9223372036854775807LL
template <ull N, class T, class... Args, std::enable_if_t<N == 0, int> = 0>
auto make_multiple_vector(Args... args) {
return T(args...);
}
template <ull N, class T, class... Args, std::enable_if_t<N != 0, int> = 0>
auto make_multiple_vector(ull size, Args... args) {
using value_type = std::decay_t<decltype(make_multiple_vector<N - 1, T>(args...))>;
return vector<value_type>(size, make_multiple_vector<N - 1, T>(args...));
}
// computational complexity: o(log(max(a, b)))
template<class Integer>
inline pair<Integer, Integer> getBezoutsIdentitySolution(Integer a, Integer b)
{
if (b == Integer(0)) return { Integer(1) / a, Integer(0) };
auto sol = getBezoutsIdentitySolution(b, a % b);
return { sol.second, sol.first - (a / b) * sol.second };
}
template<class Integer>
Integer getModValue(const Integer& n, Integer mod)
{
return (n % mod + mod) % mod;
}
// computational complexity: o(log(max(n, mod)))
template<class Integer>
inline Integer getModInverse(const Integer& n, Integer mod)
{
auto sol = getBezoutsIdentitySolution(n, mod);
return getModValue(sol.first, mod);
}
int solve() {
ll n, k;
cin >> n >> k;
vector<ll> as(n);
REPD(i, 0, n) cin >> as[i];
vector<ll> power2s(k + 1);
power2s[0] = 1;
REPD(i, 0, k) power2s[i + 1] = power2s[i] * 2LL % MOD;
vector<ll> exclamations(k + 1);
exclamations[0] = 1;
REPD(i, 0, k) exclamations[i + 1] = exclamations[i] * (i + 1LL) % MOD;
vector<ll> a_power_sums(k + 1);
a_power_sums[0] = n % MOD;
vector<ll> a_powers(n, 1);
ll sum;
REPD(i, 0, k) {
sum = 0;
REPD(j, 0, n) {
a_powers[j] = a_powers[j] * as[j] % MOD;
sum = (sum + a_powers[j]) % MOD;
}
a_power_sums[i + 1] = sum;
}
REPD(x, 1, k + 1) {
sum = 0;
REPD(i, 0, x + 1) {
ll comb = exclamations[x] * getModInverse(exclamations[i], MOD) % MOD * getModInverse(exclamations[x - i], MOD) % MOD;
sum = (sum + comb * a_power_sums[i] % MOD * a_power_sums[x - i] % MOD) % MOD;
}
sum = (sum - power2s[x] * a_power_sums[x] % MOD + MOD) % MOD;
sum = sum * getModInverse(2LL, MOD) % MOD;
cout << sum << endl;
}
return 0;
}
//main function
int main()
{
cin.tie(0);
ios::sync_with_stdio(false);
solve();
// ll t;
// cin >> t;
// REPD(i, 0, t) solve();
return 0;
}
|
/*/ Author : Abhishek Chauhan /*/
#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;
// a+b = a^b + 2*(a&b)
// According to Fermat's little theorem, (a^b)%mod = ((a^(b%(mod - 1)))%mod) if mod is a prime number and not the other way round.
// whenever using a comparator return true only if a<b
// make it forward first if it doesnt work do the reverse
// vector a = ax + ay b = bx + by then dot porduct is |a||b|cos@ //and also for eas you can use (ax*bx)+(ay*by);
#define rep(i,n) for(int i=0;i<n;i++)
#define int long long
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define mod 1000000007
#define bpop(x) __builtin_popcountll((x))
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define endl '\n'
template<class T>
using ordered_set = tree<T, null_type,less<T>, rb_tree_tag,tree_order_statistics_node_update> ;
// find_by_order(k) returns iterator to kth element starting from 0;
// order_of_key(k) returns count of elements strictly smaller than k;
template<class T>
T power(T x, T y){T res = 1;while (y > 0){ if (y & 1){res = res*x;} y = y>>1;x = x*x;}return res;}
template<class T>
T powermod(T x, T y, T p){T res = 1;x = x % p;while (y > 0){if (y & 1){res = (res*x) % p;}y = y>>1; x = (x*x) % p;}return res;}
const int N = 20;
vector<int>g[N];
int vis[N],col[N];
vector<int>order;
void get(int node){
vis[node] = 1;
order.pb(node);
for(auto x:g[node]){
if(vis[x]==0){
get(x);
}
}
}
int recur(int i){
if(i>=order.size()){
return 1;
}
int res = 0;
vis[order[i]] = 1;
vector<int>can(4,1);
for(auto x:g[order[i]]){
if(col[x]!=0){
can[col[x]] = 0;
}
}
for(int c=1;c<4;c++){
if(can[c]){
col[order[i]] = c;
res+=recur(i+1);
col[order[i]] = 0;
}
}
return res;
}
void snow(){
int n,m;
cin>>n>>m;
for(int i=0;i<n;i++){
vis[i] = 0;
col[i] = 0;
}
for(int i=0;i<m;i++){
int a,b;
cin>>a>>b;a--;b--;
g[a].pb(b);
g[b].pb(a);
}
int ans = 1;
for(int i=0;i<n;i++){
if(!vis[i]){
order.clear();
get(i);
col[order[0]] = 1;
vis[order[0]] = 1;
ans=ans*3*recur(1);
}
}
cout<<ans<<endl;
}
int32_t main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
fastio;
int _;
_=1;
for(int i=1;i<=_;i++){
// cout<<"Case #"<<i<<": ";
snow();
}
return 0;
}
|
#include "bits/stdc++.h"
#define ll long long
#define lld long double
#define MOD 1000000007
#define inf 1000000000000000000LL
#define pii pair<ll,ll>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
ll power(ll x,ll y, ll md=MOD){ll res = 1;x%=md;while(y){if(y&1)res = (res*x)%md;x *= x; if(x>=md) x %= md; y >>= 1;}return res;}
using namespace std;
#define endl '\n'
#define int long long
struct node {vector<ll> v,w;};
ll mod(ll &x,ll md=MOD){x=(x%md + md)%md;return x;}
ll mod(ll &&x,ll md=MOD){x=(x%md + md)%md;return x;}
#define mm 101
void multiply(int a[][mm], int b[][mm], int dim) {
int c[mm][mm];
for (int i = 0; i < dim; i++) {
for (int j = 0; j < dim; j++) {
int sum = 0;
for (int k = 0; k < dim; k++) {
sum += a[i][k] * b[k][j];
mod(sum);
}
c[i][j] = sum;
}
}
for (int i = 0; i < dim; i++) {
for (int j = 0; j < dim; j++) {
a[i][j] = c[i][j];
}
}
}
void matrixexponentiation(int arr[mm][mm], int dim, int power) {
int I[mm][mm];
for (int i = 0; i < dim; i++) {
for (int j = 0; j < dim; j++) {
if (i == j) {
I[i][j] = 1;
} else {
I[i][j] = 0;
}
}
}
while (power != 1) {
if (power % 2) {
multiply(I, arr, dim);
power--;
} else {
multiply(arr, arr, dim);
power /= 2;
}
}
multiply(arr, I, dim);
}
int arr[101][101];
signed main() {
fast;
int n,m,k;
cin>>n>>m>>k;
vector<node> nodes(n);
vector<int> a(n);
for(auto &x: a) cin>>x;
if(k==0) {
for(auto x: a) cout<<x<<" ";
exit(0);
}
set<pii> ed;
vector<pii> q(m);
for(int i=0;i<m;i++) {
int u,v;
cin>>u>>v;
u--; v--;
nodes[u].v.pb(v);
nodes[v].v.pb(u);
ed.insert({u,v});
ed.insert({v,u});
}
for(int i=0;i<n;i++) {
int num = nodes[i].v.size();
arr[i][i] = 1 - (num) * power(2*m, MOD-2);
mod(arr[i][i]);
// cout<<(2*num + m)<<"/"<<2*num + 2*m<<endl;
for(int j=0;j<n;j++) {
if(j==i) continue;
if(ed.count({i,j})) {
arr[i][j] = power(2*m, MOD-2);
// cout<<"#"<<i<<" "<<j<<" -- "<<2*num + 2*m<<endl;
}
}
}
// for(int i=0;i<n;i++) {
// for(int j=0;j<n;j++) {
// cout<<arr[i][j]<<" ";
// }
// cout<<endl;
// }
matrixexponentiation(arr, n, k);
for(int i=0;i<n;i++) {
int val = 0;
for(int j=0;j<n;j++) {
val += mod(a[j] * arr[i][j]);
}
cout<<mod(val)<<endl;
}
} | #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>
#define ll long long
#define MAXN 300005
#define uns unsigned
#define INF 0x3f3f3f3f
#define MOD 1000000007ll
using namespace std;
inline ll read(){
ll x=0;bool f=1;char s=getchar();
while((s<'0'||s>'9')&&s>0){if(s=='-')f^=1;s=getchar();}
while(s>='0'&&s<='9')x=(x<<1)+(x<<3)+s-'0',s=getchar();
return f?x:-x;
}
inline ll ksm(ll a,ll b,ll mo){
ll res=1;
for(;b;b>>=1,a=a*a%mo)if(b&1)res=res*a%mo;
return res;
}
struct matrix{
int n,m;
ll c[105][105];
matrix(){memset(c,0,sizeof(c)),n=m=0;}
matrix operator*(const matrix&b){
matrix res;res.n=n,res.m=b.m;
for(int i=1;i<=n;i++)
for(int k=1;k<=m;k++)
for(int j=1;j<=b.m;j++)
res.c[i][j]=(res.c[i][j]+c[i][k]*b.c[k][j]%MOD)%MOD;
return res;
}
}a,b;
inline matrix mpow(matrix a,ll b){
matrix res;res.n=res.m=a.n;
for(int i=1;i<=res.n;i++)res.c[i][i]=1;
for(;b;b>>=1,a=a*a)if(b&1)res=res*a;
return res;
}
int n,m,k,du[105];
bool G[105][105];
int main()
{
n=read(),m=read(),k=read();
a.n=1,a.m=n,b.n=b.m=n;
for(int i=1;i<=n;i++)a.c[1][i]=read();
for(int i=1;i<=m;i++){
int u=read(),v=read();
G[u][v]=G[v][u]=1,du[u]++,du[v]++;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++)
if(G[i][j])
b.c[j][i]=(b.c[j][i]+ksm(m<<1,MOD-2,MOD))%MOD,
b.c[i][i]=(b.c[i][i]+ksm(m<<1,MOD-2,MOD))%MOD;
b.c[i][i]=(b.c[i][i]+ksm(m,MOD-2,MOD)*(m-du[i])%MOD)%MOD;
}
a=a*mpow(b,k);
for(int i=1;i<=n;i++)printf("%lld\n",a.c[1][i]);
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;
using oset = tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>;
using int64 = long long;
void fastIO() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
struct ranges {
int l, r, sum;
};
bool disjoint(pair<int,int> p1, pair<int,int> p2) {
if (p1.first > p2.first)
swap(p1, p2);
return p1.second < p2.first;
}
bool disjoint(ranges p1, ranges p2) {
if (p1.l > p2.l)
swap(p1, p2);
return p1.r < p2.l;
}
bool consec(pair<int,int> p1, pair<int,int> p2) {
if (p1.first > p2.first)
swap(p1, p2);
return p1.second == p2.first - 1;
}
bool consec(ranges p1, ranges p2) {
if (p1.l > p2.l)
swap(p1, p2);
return p1.r == p2.l - 1;
}
void test_case() {
int N;
cin >> N;
vector<int> a(N + 1);
vector<ranges> r;
for (int i = 1; i <= N; ++i) {
cin >> a[i];
if (a[i] >= 200)
a[i] %= 200;
int sum = 0;
for (int j = i; j > 0; --j) {
sum += a[j];
if (sum >= 200)
sum -= 200;
r.push_back({j, i, sum});
}
}
vector<vector<pair<int,int>>> ways(200);
int M = r.size();
for (int i = 0; i < M - 1; ++i)
for (int j = i + 1; j < M; ++j) {
if (r[i].sum == r[j].sum) {
cout << "Yes\n";
cout << r[i].r - r[i].l + 1 << ' ';
for (int k = r[i].l; k <= r[i].r; ++k)
cout << k << ' ';
cout << '\n';
cout << r[j].r - r[j].l + 1 << ' ';
for (int k = r[j].l; k <= r[j].r; ++k)
cout << k << ' ';
cout << '\n';
return;
}
if (!disjoint(r[i], r[j]))
continue;
int new_sum = r[i].sum + r[j].sum;
if (new_sum >= 200)
new_sum -= 200;
if (ways[new_sum].size() == 2) {
if (consec(ways[new_sum][0], ways[new_sum][1]) && consec(r[i], r[j])
&& min(ways[new_sum][0].first, ways[new_sum][1].first) == min(r[i].l, r[j].l)
&& max(ways[new_sum][0].second, ways[new_sum][1].second) == max(r[i].r, r[j].r))
continue;
cout << "Yes\n";
set<int> ind;
for (int k = ways[new_sum][0].first; k <= ways[new_sum][0].second; ++k)
ind.emplace(k);
for (int k = ways[new_sum][1].first; k <= ways[new_sum][1].second; ++k)
ind.emplace(k);
cout << ind.size() << ' ';
for (int x : ind)
cout << x << ' ';
cout << '\n';
ind.clear();
for (int k = r[i].l; k <= r[i].r; ++k)
ind.emplace(k);
for (int k = r[j].l; k <= r[j].r; ++k)
ind.emplace(k);
cout << ind.size() << ' ';
for (int x : ind)
cout << x << ' ';
cout << '\n';
return;
}
ways[new_sum].emplace_back(r[i].l, r[i].r);
ways[new_sum].emplace_back(r[j].l, r[j].r);
}
cout << "No\n";
}
void solve() {
int T = 1;
for (int tc = 0; tc < T; ++tc)
test_case();
}
int main() {
fastIO();
solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N=205;
int n,a[N];
vector<int>v,res1,res2;
vector<int>save[N];
bool flag;
void dfs(int cur,int sum){
sum%=200;
if(flag) return;
if(cur==n+1){
if(save[sum].size()&&v.size()){
flag=true;
res1=save[sum];
res2=v;
}
else save[sum]=v;
return;
}
dfs(cur+1,sum);
v.push_back(cur);
dfs(cur+1,sum+a[cur]);
v.pop_back();
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
dfs(1,0);
if(flag){
puts("Yes");
cout<<res1.size()<<" ";
for(int i=0;i<res1.size();i++) cout<<res1[i]<<" ";
puts("");
cout<<res2.size()<<" ";
for(int i=0;i<res2.size();i++) cout<<res2[i]<<" ";
}
else puts("No");
return 0;
}
/*
鸽巢原理
如果n个巢里有kn+1个鸽子,那么至少有一个巢里面有kn+1个鸽子
*/
|
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <cmath>
#define rep(i,begin, end) for (ll i = begin; i < (ll)(end); i++)
using namespace std;
using ll = long long;
template<typename T>
using vec2 = vector<vector<T>>;
template<typename T>
using vec3 = vec2<vector<T>>;
template<typename T>
using vec4 = vec3<vector<T>>;
template<typename T>
using vec5 = vec4<vector<T>>;
template<typename T>
using vec6 = vec5<vector<T>>;
int gcd(int a, int b){
if(b == 0)return a;
return gcd(b, a%b);
}
ll extGCD(ll a, ll b, ll &x, ll &y){
if(b == 0){
x = 1;
y = 0;
return a;
}
ll g = extGCD(b, a%b, y, x);
y -= a/b * x;
return g;
}
//ax+by=cをみたす整数x,y
bool solveLinearEquation2(ll a, ll b, ll c, ll &x, ll &y){
if(c % gcd(a,b) != 0)return false;
/*
(a/d)X+(b/d)Y=c/d
(a/d)x+(b/d)y=1
(a/d)(c/d)x+(b/d)(c/d)y=c/d
X=(c/d)x-k(b/d)
Y=(c/d)y+k(a/d)
必要に応じてkを決める
*/
ll d = extGCD(a,b,x,y);
ll k = d<0 ? floor(min(((double)c/(double)b*(double)x), -(double)c/(double)a*(double)y)):
ceil(max(((double)c/(double)b*(double)x), -(double)c/(double)a*(double)y));
// cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl;
x = (c/d)*x-k*(b/d);
y = (c/d)*y+k*(a/d);
return true;
}
ll MOD = 1000000007;
vector<ll> mfact(1,1);
ll minv(ll a){
ll x;ll y;
extGCD(a, MOD, x, y);
if(x<0){
x+= ((-x)/MOD+1)*MOD;
x %= MOD;
}
return x;
}
ll mpow(ll a,ll b){
ll res = 1;
while(b > 0){
if(b%2==1)res=(res*a)%MOD;
b/=2;
a = (a*a)%MOD;
}
return res;
}
ll mfactorial(ll a){
if(mfact.size()-1 >= a)return mfact[a];
for(ll i = mfact.size();i<=a;i++){
mfact.push_back((i * mfact[i-1])%MOD);
}
return mfact[a];
}
ll mcomb(ll n, ll r){
return (((mfactorial(n) * minv(mfactorial(r)))%MOD) * minv(mfactorial(n-r)))%MOD;
}
ll ipow(ll a, ll b){
ll res = 1;
while(b > 0){
if(b % 2 == 1)res*=a;
a*=a;
b/=2;
}
return res;
}
void factorize(map<ll,ll>& fac, vector<ll>& sieved, ll a){
while(a != 1){
if(fac.find(sieved[a]) == fac.end())fac[sieved[a]] = 0;
fac[sieved[a]]++;
a/=sieved[a];
}
}
void primesieve(vector<ll>& res,ll n){
res = vector<ll>(n+1, -1);
for(int i = 2;i <= n;i++){
if(res[i] != -1)continue;
res[i] = i;
for(int j = 2;j*i <= n;j++){
res[j*i] = i;
}
}
}
vector<ll> primesuntil(ll n){
vector<ll> res;
vector<ll> sieved;
primesieve(sieved, n);
for(int i = 2;i <= n;i++){
if(sieved[i] == i)res.push_back(i);
}
return res;
}
void solve(){
// cout<<"##########"<<endl;
ll X,Y,P,Q;cin>>X>>Y>>P>>Q;
ll res = -1;
rep(q,0,Q){
rep(y,0,Y){
ll s,t;
if(!solveLinearEquation2(2*X+2*Y, -(P+Q), P+q-X-y, s,t))continue;
ll a = (2*X+2*Y)*s+X+y;
if(res == -1)res = a;
res = min(res,a);
// cout<<a<<endl;
// cout<<s<<" "<<t<<endl;
// cout<<"!!!!!!!!!!!!!!!!"<<endl;
}
}
cout<<(res==-1 ? "infinity" : to_string(res))<<endl;
}
int main(){
int T;cin>>T;
rep(i,0,T)solve();
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
// ap + bq = gcd(a, b) なる (p, q) を求め d = gcd(a, b) を返す
ll extgcd(const ll a, const ll b, ll &p, ll &q){
if (b==0) { p=1; q=0; return a; }
ll d = extgcd(b, a%b, q, p);
q -= a/b * p;
return d;
}
// x mod s = a, x mod t = b なる x >= 0 の最小値を求める。存在しなければ -1
ll solve(const ll a, const ll b, const ll s, const ll t){
ll p, q;
const ll d = extgcd(s, t, p, q);
if ((a-b) % d != 0) return -1;
const ll ret = a + s * (((b - a) / d * p) % (t / d));
const ll s_lcm_t = s * t / d;
return (ret % s_lcm_t + s_lcm_t) % s_lcm_t;
}
int main(){
int t;
cin >> t;
for(int ti=0; ti<t; ++ti){
ll x, y, p, q;
cin >> x >> y >> p >> q;
ll ret = -1;
for(ll yi = 0; yi < y; ++yi){
for(ll qi = 0; qi < q; ++qi){
const ll v = solve(x + yi, p + qi, (x + y) * 2, p + q);
if (v >= 0){
if(ret == -1){
ret = v;
}else{
ret = min(ret, v);
}
}
}
}
if(ret == -1){
cout << "infinity" << endl;
}else{
cout << ret << endl;
}
}
return 0;
} |
#include <iostream>
using namespace std;
#define int long long
int gcd(int a,int b)
{
return b?gcd(b,a%b):a;
}
int LCM(int a, int b)
{
return a/gcd(a, b)*b;
}
signed main()
{
int n, m , s;
cin>>n;
s = 1;
for (int i = 2; i <=n; i++)
s = LCM(s, i);
s++;
cout<<s;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
int main()
{
long long n,m,a,b=13,c,d,e,f,g,h,p,q,r,fr,sc,tr,sz=0,tz,i,j,k,mx=LLONG_MIN,mn=LLONG_MAX;
long long x=1,y=1,cnt=0,res=0,ans=1,sum=0; long long flg=0,flag=1,na=0,as=1;
vector<long long>u,v,w; vector< pair<long long,long long> >vct; vector<string>vst;
set<long long>st,nt,tt; map<long long,long long>mp,nq,qr; string str,ttr,ntr;
cin>>n;
ans=ans*2; mp[2]=1; ans=ans*3; mp[3]=1;
g=2;
for(i=3;i<=30;i++)
{
d=(g*i)/(__gcd(g,i));
g=d;
}
//cout<<g<<endl;
for(i=4;i<=30;i++)
{
fr=i;
c=0;
while(fr%2==0)
{
fr=fr/2; c++; if(c>mp[2]){ mp[2]=c; ans=ans*2; }
}
for(j=3;j*j<=fr;j+=2)
{
c=0;
while(fr%j==0)
{
fr=fr/j; c++; if(c>mp[j]){ mp[j]=c; ans=ans*j; }
}
}
c=0;
if(fr!=1)
{
c=1;
if(c>mp[fr]){ mp[fr]=c; ans=ans*fr; }
}
}
cout<<ans+1<<endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i,a,b) for (int i = a; i < b; i++)
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef string str;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
ll a,b,c;
cin>>a>>b>>c;
if(pow(a,2) + pow(b,2) < pow(c,2)){
cout<<"Yes"<<"\n";
}
else{
cout<<"No"<<"\n";
}
} | #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
// const ll p = 998244353;
const ll nax = 100001;
int binpow(int a, int n, int p) {
if (!n) return 1;
if (n & 1) return binpow(a, n - 1, p) * a % p;
return binpow(a * a % p, n / 2, p);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int a, b, c;
cin >> a >> b >> c;
a %= 10;
if (a == 0 || a == 5) {
cout << a;
return 0;
}
vector<vector<int>> v = {{2, 4, 8, 6}, {4, 6}, {6}, {8, 4, 2, 6}};
if (__gcd(a, 10) == 1) {
b %= 4;
cout << binpow(a, binpow(b, c, 4), 10);
return 0;
}
a = a / 2 - 1;
b %= v[a].size();
int sh = binpow(b, c, v[a].size()) - 1;
if (sh < 0) sh += v[a].size();
cout << v[a][sh];
}
|
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<vector>
#include<utility>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<bitset>
#include<string>
#include<functional>
#include<iomanip>
#define rep(i,n,m) for(int i=(n);i<(int)(m);i++)
#define reps(i,n,m) for(int i=(n);i<=(int)(m);i++)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define fs first
#define sc second
#define lb lower_bound
#define ub upper_bound
#define LB(a,x) lb(all(a), x) - a.begin()
#define UB(a,x) ub(all(a), x) - a.begin()
#define chartoint(c) (int)((c) - '0')
#define chartoll(c) (long long)((c) - '0')
#define bpc(x) __builtin_popcount(x)
#define bpcll(x) __builtin_popcountll(x)
#define fps(n) fixed << setprecision(n)
#define MOD 1000000007
#define itn int
#define enld endl
#define ednl endl
#define icn cin
#define cotu cout
#define Endl endl
#define stirng string
using namespace std;
typedef long long ll;
const double pi = 3.141592653589793;
using Graph = vector<vector<int>>;
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 A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
std::fill( (T*)array, (T*)(array+N), val );
}
int main(){
cin.tie(0);ios::sync_with_stdio(false);
int n; cin >> n;
vector<pair<int,int>> v(n);
rep(i,0,n){
int x,y; cin >> x >> y;
v[i] = {x,y};
}
int ans = 0;
rep(i,0,n){
rep(j,i+1,n){
if(abs(v[j].second-v[i].second) <= abs(v[j].first-v[i].first)) ans++;
}
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int inf=0x3f,INF=0x3f3f3f3f; const ll LLINF=0x3f3f3f3f3f3f3f3f;
#define siz(x) (int)x.size()
#define af(x) x.begin(), x.end()
#define ms(x, y) memset(x, y, sizeof x)
#define fil(x, y) fill(af(x), y)
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define f first
#define s second
#define lc i << 1
#define rc i << 1 | 1
const int mod = 1e9 + 7;
ll fpow(ll a, ll b){ll ret=1;while(b){if(b&1)ret=ret*a%mod;a=a*a%mod,b>>=1;}return ret;}
ll inv(ll x){return x ? fpow(x, mod - 2) : 1;}
void madd(int &a, int b){a+=b;if(a>=mod)a-=mod;}
void msub(int &a, int b){a+=mod-b;if(a>=mod)a-=mod;}
const int MM = 2e5 + 5;
void solve(){
int n;
cin >> n;
vector<int> a(n), b(n);
int ans = 0;
for (int i = 0; i < n; i++) cin >> a[i] >> b[i];
for (int i = 0; i < n; i++){
for (int j = i + 1; j < n; j++){
double m = (b[i] - b[j]) * 1.0 / (a[i] - a[j]);
if (m >= -1 && m <= 1) ans++;
}
}
cout << ans << '\n';
}
/*
* do i rlly need to check everything????
*/
int main(){
cin.sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t = 1;
//cin >> t;
while(t--) solve();
}
|
/**
* author: tomo0608
* created: 14.03.2021 17:00:40
**/
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#if __has_include(<atcoder/all>)
#include<atcoder/all>
using namespace atcoder;
#endif
typedef long long ll;
typedef long double ld;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
typedef pair<int,int> pii;
typedef pair<long long, long long> pll;
#define all(x) x.begin(),x.end()
#define rep2(i, m, n) for (int i = (m); i < (n); ++i)
#define rep(i, n) rep2(i, 0, n)
#define drep2(i, m, n) for (int i = (m)-1; i >= (n); --i)
#define drep(i, n) drep2(i, n, 0)
#define unique(a) a.erase(unique(a.begin(),a.end()),a.end())
template<class... T>void input(T&... a){(cin >> ... >> a);};
#define INT(...) int __VA_ARGS__; input(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__; input(__VA_ARGS__)
#define STRING(...) string __VA_ARGS__; input(__VA_ARGS__)
void print(){cout << '\n';}
template<class T, class... Ts>void print(const T& a, const Ts&... b){cout << a; (cout << ... << (cout << ' ', b)); cout << '\n';}
template<class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T> >;
template<class T, class U> inline bool chmax(T &a, const U &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T, class U> inline bool chmin(T &a, const U &b) { if (a>b) { a=b; return 1; } return 0; }
template<class T1, class T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; }
template<class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << '(' << p.first << ", " << p.second << ')'; return os; }
template<class T> istream &operator>>(istream &is, vector<T> &v) { for (auto &e : v) is >> e; return is; }
template<class T> ostream &operator<<(ostream &os, const vector<T> &v) { for (auto &e : v) os << e << ' '; return os; }
template<typename T> ostream& operator << (ostream& os, set<T>& set_var) {os << "{"; for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {os << *itr;++itr;if(itr != set_var.end()) os << ", ";itr--;}os << "}";return os;}
template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &map_var) {os << "{";for(auto itr = map_var.begin(); itr != map_var.end(); itr++) {os << *itr;itr++;if (itr != map_var.end()) os << ", ";itr--;}os << "}";return os;}
template<class T> inline int count_between(vector<T> &a, T l, T r) { return lower_bound(all(a), r) - lower_bound(all(a), l); } // [l, r)
#define pb push_back
#define eb emplace_back
#define elif else if
#define mp make_pair
#define bit(n, k) ((n >> k) & 1) /*nのk bit目*/
template<typename T> T gcd(T x, T y){if(x%y == 0)return y;return gcd(y, x%y);}
template<typename T> T gcd(vector<T> a){T res = a[0];for(auto &x: a)res = gcd(res, x);return res;}
template <typename T>T mypow(T x, ll n){T ret = 1;while(n > 0){if(n & 1)(ret *= x);(x *= x);n >>= 1;}return ret;}
#define endl '\n'
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, -1, 1};
void solve(){
STRING(s);
int n = s.size();
if(s.compare("atcoder") > 0){
print(0);
return;
}
int now = 0;
while(s[now] == 'a')now++;
if(now == n){
print(-1);
return;
}
print(now - (s[now] > 't'));
return;
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
cout << setprecision(20);
int codeforces = 1;
cin >> codeforces;
while(codeforces--){
solve();
}
return 0;
} | //#pragma GCC optimize("O3")
#include<bits/stdc++.h>
#define ll long long int
#define ld long double
#define pb push_back
#define mp make_pair
#define mod 998244353
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define all(x) x.begin(),x.end()
#define F first
#define S second
#define fo(i,n) for(int i=0;i<(n);i++)
#define inp(a) int a;cin>>a;
#define inpa(a,n)vector<int> a(n);fo(i,n)cin>>a[i];
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";
using namespace std;
#define int ll
// #define MAX 20
#define INF (int)1e18
template<class T> ostream& operator<<(ostream& cout,vector<T> V)
{cout<<"[ ";for(auto v:V)cout<<v<<' ';return cout<<']';}
template<class L,class R> ostream& operator<<(ostream& cout,pair<L, R> P)
{return cout<<'('<<P.F<<','<< P.S<<')';}
signed main()
{
fio
inp(a) inp(b) inp(c)
int ans1=((a*(a+1))/2)%mod,ans2=((b*(b+1))/2)%mod,ans3=((c*(c+1))/2)%mod;
// for(int i=1;i<=a;i++)
// ans1=(ans1+i)%mod;
// for(int i=1;i<=b;i++)
// ans2=(ans2+i)%mod;
// for(int i=1;i<=c;i++)
// ans3=(ans3+i)%mod;
int fans=(ans1*ans2)%mod;
fans=(fans*ans3)%mod;
cout<<fans<<'\n';
printclock;
} |
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
template<class A, class B>void tostring(pair<A, B>p) { cerr<<"(" + to_string(p.first) + ", " + to_string(p.second) + ")";}
template<class A>void tostring(A v) {int f = 1;string r = "{";for (auto& x : v) {if (!f) {r += ", ";}f = 0;r += to_string(x);}r += "}\n";cerr<<r;}
void _out() { cerr << endl; }template<typename H, typename... T> void _out(H h, T... t) { cerr << ' ' << h; _out(t...); }
void show() { cerr << endl; }template<typename H, typename... T> void show(H h, T... t) { cerr<<h<<' '; show(t...); }
#define see(...) cerr << "(" << #__VA_ARGS__ << "):", _out(__VA_ARGS__)
#define rep(i, l, r) for(int i=(int)(l);i<=(int)(r);i++)
#define per(i, r, l) for(int i=(int)(r);i>=(int)(l);i--)
#define all(x) (x).begin(), (x).end()
#define all1(x) (x).begin()+1, (x).end()
#define pb push_back
#define ub upper_bound
#define lb lower_bound
//#define int long long
typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<pii> vp; typedef vector<vp> vvp;
#define fi first
#define se second
const int N = 1e5+100;
//const int mod=1e9+7;
int h[N], e[2*N], ne[2*N],idx,col[N], bcol[N];
bool st[N];
vector<int>ans;
void add(int a, int b){
e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}
void dfs(int u, int fa){
if(!bcol[col[u]]){
ans.pb(u);
}
bcol[col[u]] ++;
for(int i = h[u]; i != -1; i = ne[i]){
int j = e[i];
if(j == fa)continue;
dfs(j, u);
}
bcol[col[u]] --;
}
void solve() {
memset(h, -1, sizeof h);
int n;cin >> n;
for(int i = 1; i <= n; i ++){
cin >> col[i];
}
for(int i = 0; i < n - 1; i ++){
int a, b;cin >> a >> b;
add(a, b), add(b, a);
}
dfs(1, -1);
sort(all(ans));
rep(i, 0, ans.size() - 1)cout << ans[i] << endl;
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
#ifndef ONLINE_JUDGE
freopen("E:\\in.txt", "r", stdin);
#endif
solve();
} | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define fastio ios_base::sync_with_stdio(0);cin.tie(0)
#define fp(i,a,b) for(ll i=a ; i<b ; i++)
#define fn(i,a,b) for(int i=a ; i>=b ; i--)
#define ones(x) __builtin_popcount(x)
#define pb push_back
#define mk make_pair
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
#define dbg(x) cout << (#x) << " = " << x << " "
#define fini cout << "\n";
#define line cout << "-----------------------------------\n";
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef tree<
ll,
null_type,
less_equal<ll>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
int read (){
int x = 0, f = 1; char s = getchar();while (s < '0' || s > '9') {if (s == '-') f = -1; s = getchar();}
while (s >= '0' && s <= '9') x = x * 10 + s - '0', s = getchar();
return x * f;
}
const ll M=2e5+7;
const ll N=1e6+7;
const ll inf=1e18;
const ll mod=1e9+7;
const ll mod2=998244353;
int n;
int c[M];
vi adj[M];
map<int,int> d;
vi ans;
void dfs(int u,int p){
if (!d.count(c[u]) || d[c[u]] == 0) ans.pb(u);
d[c[u]]++;
for (int v : adj[u])
if (v != p) dfs(v, u);
d[c[u]]--;
}
void go(int ide){
cin >> n;
fp(i,1,n+1) cin >> c[i];
fp(i,1,n){
int a,b; cin >> a >> b;
adj[a].pb(b);
adj[b].pb(a);
}
dfs(1, 1);
sort(all(ans));
for (int x : ans) cout << x << "\n";
}
int main(){
fastio;
int tst=1;
// cin >> tst;
// cout << fixed << setprecision(12);
fp(i,0,tst) go(i+1);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N, ans=0;
cin >> N;
vector<int> A(N);
for(int i=0;i<N;i++)
cin >> A.at(i);
for(int i=0;i<N;i++){
if(A.at(i) > 10)
ans += A.at(i) -10;
}
cout << ans << endl;
} | #include <iostream>
using namespace std;
int main(){
int N;
cin >> N;
int A[N], B[N];
for(int i=0; i<N; i++){
cin >> A[i];
}
for(int i=0; i<N; i++){
cin >> B[i];
}
int ans=0;
for(int i=0; i<N; i++){
ans+=A[i]*B[i];
}
if(ans==0){
cout << "Yes" << endl;
}
else{
cout << "No" << endl;
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
//main sample
//by six_sigma
#define fastio ios::sync_with_stdio(false);cin.tie(0);
#define ll long long int
//defining loops
#define lp0(i,a) for(i=0;i<a;i++)
#define lp1(i,a) for(i=1;i<=a;i++)
//defining file input output(T.N.T.I)
#define f_input freopen("input.txt","r",stdin)
#define f_output freopen("output.txt","w",stdout)
//defining stl operations
#define pb push_back
#define vi vector<int>
#define vll vector<ll>
#define vs vector<string>
#define vc vector<char>
#define si stack<int>
#define qi queue<int>
//defining input output
#define sf scanf
#define pf printf
#define endl "\n"
void solve(){
ll a,i;
double b;
cin>>a>>b;
double sum=0;
double x,y;
bool flag=false;
lp0(i,a){
cin>>x>>y;
x*=(y/100);
sum+=x;
if(sum-b>(1e-6)){
cout<<i+1<<endl;
flag=true;
break;
}
}
if(!flag)cout<<-1<<endl;
}
int main(){
fastio
int t=1;
while(t--)solve();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define Rep(i,j,n) for(int i=j; i<n; i++)
#define rep(i,n) for(int i=0; i<n; i++)
#define PI 3.14159265359
#define INF 1000100100//0000000
#define MOD 1000000007
#define all(x) (x).begin(),(x).end()
typedef long long ll;
#define P pair<int, int>
//#define P pair<ll, ll>
//#define P pair<double, double>
#define PP pair<P,P>
#define T tuple<int,int,int>
struct UnionFind {
vector<int> d;
UnionFind(int n=0): d(n,-1) {}
int find(int x) {
if (d[x] < 0) return x;
return d[x] = find(d[x]);
}
bool unite(int x, int y) {
x = find(x); y = find(y);
if (x == y) return false;
if (d[x] > d[y]) swap(x,y);
d[x] += d[y];
d[y] = x;
return true;
}
bool same(int x, int y) { return find(x) == find(y);}
int size(int x) { return -d[find(x)];}
};
int main(){
int n,m; cin >> n >> m;
vector<int> a(n),b(n);
rep(i,n) cin >> a[i];
rep(i,n) cin >> b[i];
UnionFind u(n);
rep(i,m){
int c,d; cin >> c >> d;
c--; d--;
u.unite(c,d);
}
vector<vector<int> > g(n);
rep(i,n){
int w=u.find(i);
g[w].push_back(i);
}
rep(i,g.size()){
ll v,v2;
v=v2=0;
for(auto x : g[i]){
v+=a[x]; v2+=b[x];
}
if(v!=v2){
cout << "No" << endl;
return 0;
}
}
cout << "Yes" << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
constexpr long long MOD = 1e9 + 7;
signed main() {
int n, m;
cin >> n >> m;
vector<pair<int, int>> v[n];
int x, y, z;
rep(i, m) {
cin >> x >> y >> z;
x--;
v[x].emplace_back(y, z);
};
ll dp[n + 1][1 << n];
fill(dp[0], dp[n + 1], 0);
dp[0][0] = 1;
rep(i, n) {
for (int bit = 0; bit < 1 << n; bit++) {
if (!dp[i][bit]) continue;
rep(use, n) {
if (bit >> use & 1) continue;
int nxt = bit | 1 << use;
bool ng = false;
for (auto p : v[i]) {
int cnt = 0;
rep(k, p.first) {
cnt += (nxt >> k) & 1;
}
if (cnt > p.second) {
ng = true;
break;
}
}
if (ng) continue;
dp[i + 1][nxt] += dp[i][bit];
}
}
}
cout << dp[n][(1 << n) - 1] << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
int main(){
int N;
cin>>N;
string S,T;
cin>>S>>T;
int ans=0;
vector<int>A,B;
for(int i=0,cnt=0;i<N;i++){
if(S[i]=='0')A.push_back(i);
if(T[i]=='0')B.push_back(i);
}
if(A.size()!=B.size()){
cout<<"-1\n";return 0;
}
for(int i=0;i<A.size();i++){
ans+=(A[i]!=B[i]);
}
cout<<ans<<'\n';
} |
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define For(i, a, b) for (ll i = (a); i <= (b); i++)
#define Forx(i, a, b, x) for (ll i = (a); i <= (b); i+=x)
#define debug(x) cout <<" || "<< #x << " is= " << x <<endl; //use for debug
#define read(a) ll a; cin >> a;
#define Ford(i, a, b) for (ll i = (a); i >= (b); i--)
#define pb(a) push_back(a)
#define pf(a) push_front(a)
#define fast_io ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vpii vector<pair<int,int>>
#define pr pair<ll,ll>
#define vpll vector<pair<ll,ll>>
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend() //reverse of above;
#define sz(container) int((container).size())
#define cut(x) {cout<<x<<"\n"; continue;}
#define setprec(x) cout << fixed << showpoint << setprecision(15)<<x<<"\n";
#define time cout << clock() / double(CLOCKS_PER_SEC) << endl;
#define endl '\n';
#define mod 1000000007
#define mod1 998244353
#define pi 3.141592653589793238
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef tree<pr,null_type,less<pr>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
//*s.find_by_order() ->returns an iterator to the k-th largest element (counting from zero);
//s.order_of_key() ->number of elements strictly smaller then our item;
//st.rbegin()->second; last element in set
//st.erase(prev(st.end())); delete last element in set;
//sort(a+1,a+n+1,[](ll x,ll y){return abs(x)>abs(y);});
//sort(v.begin(), v.end(), greater<int>()); -> not sure work for first also if second are equal;
//q=lower_bound(all(v[a[i]]),i)-v[a[i]].begin()+1;
// s.substr(start_ind,length);
// vpll shift={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}} ->shift operator;
//bool comp(pr a,pr b){return a.second<b.second;} // use for differentiate order of comparision;
// cntbit(x) __builtin_popcount(x); -> no. of bits in given no.;
// true false; memset(dp,-1,sizeof(dp));
// std::random_shuffle(all(vec));
//using namespace std::chrono_literals;
//std::this_thread::sleep_for(-9999999999999ms);
//░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
const ll MAXN=1e6+5;
const ll inf=1e18;
int main()
{
fast_io;
//“Don’t wish it were easier. Wish you were better.” – Jim Rohn;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n; cin>>n;
long double x,y; cin>>x>>y;
long double ans=0;
while(n--)
{
long double x1,y1,height;
cin>>x1>>y1;
height=(y1-y)/(x1-x);
height*=(-x1);
height+=y1;
//debug(height);
ans=max(ans,height);
}
setprec(ans);
} | #include <iostream>
#include <algorithm>
#include <unordered_set>
#include <set>
#include <vector>
#include <queue>
#include <map>
#include <numeric>
#include <math.h>
using namespace std;
#define rep(i, n) for (long long int i = 0; i < (long long int)(n); i++)
#define irep(i, n) for (long long int i = 1; i <= (long long int)(n); i++)
#define drep(i, n, diff) for (long long int i = 0; i < (long long int)(n); i += diff)
#define mod 1000000007
#define cdeb(val) cout << #val << ":" << val << endl;
#define pdeb(val) printf("%s : %lld\n", #val, val);
typedef long long int ll;
typedef pair<ll, ll> P;
int main(){
const ll INF = 1000000000000;
ll n, m;
cin >> n >> m;
vector<ll> a(n);
vector<ll> b(m);
rep(i, n) {
cin >> a[i];
}
rep(i, m) {
cin >> b[i];
}
vector<vector<ll>> dp(n+1, vector<ll>(m+1, INF));
dp[0][0] = 0;
rep(i, n+1) {
rep(j, m+1) {
if (i < n) {
dp[i+1][j] = min(dp[i+1][j], dp[i][j] + 1);
}
if (j < m) {
dp[i][j+1] = min(dp[i][j+1], dp[i][j] + 1);
}
if (i < n && j < m) {
ll cost = 0;
if (a[i] != b[j]) cost = 1;
dp[i+1][j+1] = min(dp[i+1][j+1], dp[i][j] + cost);
}
}
}
cout << dp[n][m] << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define fon(i,n) for(int i=0;i<n;i++)
#define fo(i,n) for(int i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<ll ,ll>
#define vii vector<int>
#define vll vector<ll>
#define vvll vector<vll>
#define vpii vector<pii>
#define vpll vector<pll>
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
#define M (1000*1000*1000+7)
#define test ll T; cin>>T; while(T--)
#define all(z) z.begin() , z.end()
#define allr(z) z.rbegin() , z.rend()
// =============================================
//1.integer overflow (1e5 * 1e5) (2e9 + 2e9) // 2.runtime error //3.boundary condition
void solve()
{
ll n , m , s = 0;
cin >> n >> m;
ll a[m+2]; a[0] = 0;
fo(i , m)cin >> a[i];
a[m+1] = n+1;
sort(a , a+m+2);
vll v;
fo(i , m+1)
{
if((a[i]-a[i-1]-1) > 0)
{
v.pb(a[i]-a[i-1]-1);
}
}
if(v.size()==0){
cout << "0" << endl;
return;
}
ll k = *min_element(all(v)) , ans = 0;
for(ll j = 0 ; j < v.size() ; j++)
{
ans += (v[j]+k-1)/k;
}
cout << ans << endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
} | #include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
int main() {
ll n, m; cin >> n >> m;
vector<ll> a(m); for(int i = 0; i < m; i++) cin >> a[i];
sort(a.begin(), a.end());
a.insert(a.begin(), 0);
a.push_back(n+1);
vector<ll> v;
ll k = n;
for(int i = 0; i+1 < a.size(); i++) {
ll len = a[i+1] - a[i] - 1;
if(len > 0) {
v.push_back(len);
k = min(k, len);
}
}
ll ans = 0;
for(auto len: v) ans += (len+k-1) / k;
cout << ans << endl;
return 0;
}
|
#define LOCAL
#define _USE_MATH_DEFINES
#include <array>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <complex>
#include <cmath>
#include <numeric>
#include <bitset>
#include <functional>
#include <random>
#include <ctime>
using namespace std;
template <typename A, typename B>
ostream& operator <<(ostream& out, const pair<A, B>& a) {
out << "(" << a.first << "," << a.second << ")";
return out;
}
template <typename T, size_t N>
ostream& operator <<(ostream& out, const array<T, N>& a) {
out << "["; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
out << "["; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const set<T, Cmp>& a) {
out << "{"; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}";
return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
out << "{"; bool first = true;
for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}";
return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << ": " << arg1 << " |";
__f(comma + 1, args...);
}
typedef long long int64;
typedef pair<int, int> ii;
#define SZ(x) (int)((x).size())
template <typename T> static constexpr T inf = numeric_limits<T>::max() / 2;
const int MOD = 1e9 + 7;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }
struct fast_ios {
fast_ios() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
};
} fast_ios_;
const int N = 200 + 10;
int64 C[N][N];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
C[i][0] = C[i][i] = 1;
for (int j = 1; j < i; ++j) {
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
}
}
cout << C[n - 1][11] << '\n';
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define ll long long int
#define mod 1000000007
#define FIO ios_base::sync_with_stdio(false);cin.tie(NULL);
int main() {
FIO;
int n;
cin>>n;
int l = 1;
int r = n;
int ans = -1;
while(l<=r){
long long mid = (l+r)/2;
if(((mid*(mid+1))/2)==n){
ans = mid;
break;
}
if(((mid*(mid+1))/2)>n){
ans = mid;
r = mid-1;
}
else{
l = mid+1;
}
}
cout<<ans<<endl;
return 0;
} |
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main() {
int N;
int maxA; int minB;
cin >> N;
vector<int>A(N); vector<int>B(N);
for (int i = 0; i < N; i++) {
cin >> A.at(i);
}
for (int i = 0; i < N; i++) {
cin >> B.at(i);
}
maxA = *max_element(A.begin(),A.end());
minB = *min_element(B.begin(), B.end());
if (minB-maxA+1 <= 0) {
cout << 0 << endl;
}
else if(minB-maxA+1>0){
cout << -maxA + minB + 1 << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double lxd;
typedef vector<ll> vll;
typedef pair<ll, ll> pll;
#define mem(dp, a) memset(dp, a, sizeof dp)
#define fast \
ios_base::sync_with_stdio(false); \
cin.tie(0); \
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define repb(i, a, b) for (ll i = a; i >= b; i--)
#define f(i, n) for (ll i = 0; i < n; i++)
#define fo(i, n, x) for (ll i = x; i <= n; i++)
#define mp(a, b) make_pair(a, b)
#define pb(x) push_back(x)
#define make_graph(a, b) \
adj[a].pb(b); \
adj[b].pb(a)
#define tr(c, it) for (((c).begin())it = (c).begin(); it != (c).end(); it++)
#define test \
ll tt; \
cin >> tt; \
label: \
while (tt--)
#define fls fflush(stdout)
#define F first
#define S second
#define all(arr) arr.begin(), arr.end()
#define read(arr, n) f(i, n) cin >> arr[i]
#define INT_BITS 32
const ll mod = 1000000007;
const ll inf = 2e9 ;
const ll MAXN = 2e5 + 1;
main(){
test{
ll a, b ;
cin >> a >> b ;
if(b < 2*a){
cout << "0\n" ; goto label ;
}
ll ans = max(0ll, b - (2*a)) + 1 ;
cout << (ans * (ans + 1)) / 2 << "\n" ;
}
} |
/*Rabbi Zidni Ilma*/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define scl(n) scanf("%lld",&n)
#define scll(n,m) scanf("%lld %lld",&n,&m)
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define mod 1000000007
#define read freopen("input.txt","r",stdin)
#define write freopen("output.txt","w",stdout)
ll arr[200000],cumsum[200000];
int main()
{
ll n,curr=0,i,ans=0;
scl(n);
for(i=0;i<n;i++)
scl(arr[i]);
sort(arr,arr+n);
for(i=0;i<n;i++)
{
cumsum[i]=curr+arr[i];
curr=cumsum[i];
}
for(i=0;i<n;i++)
{
ans+=cumsum[n-1]-cumsum[i]-arr[i]*(n-1-i);
}
printf("%lld\n",ans);
}
| #include<iostream>
using namespace std;
int main(){
int n;
cin>>n;
int ans = 0;
for(int i = 0; i < n; i++){
int d;
cin>>d;
if(d > 10){
ans += d - 10;
}
}
cout<<ans;
} |
#include<bits/stdc++.h>
using namespace std;
int main(){
long long n,i,k,t,ans=0;
cin>>n;
unordered_map<long long,long long> mp;
for(i=0; i<n; i++){
cin>>k;
t = k%200;
mp[t]++;
}
for(auto x : mp){
if(x.second>1){
for(i=1; i<x.second; i++){
ans = ans + i;
}
}
}
cout<<ans;
return 0;
}
| /*
** Author: Kartikey Tewari
** Time: 10-04-2021 13:06:53
*/
#include <bits/stdc++.h>
#define ll long long
#define mod (ll)998244353
using namespace std;
ll power (ll a, ll b)
{
ll ans=1;
while (b>=1)
{
if (b%2==1)
{
ans=(ans*a)%mod;
b--;
}
else
{
a=(a*a)%mod;
b=b/2;
}
}
return ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
ll n;
cin >> n;
vector<ll> arr(n);
for (ll i=0;i<=n-1;i++)
{
cin >> arr[i];
}
sort(arr.begin(),arr.end());
ll ans=0;
ll pre=0;
for (ll i=0;i<=n-1;i++)
{
ans=(ans+((pre*arr[i])%mod))%mod;
pre=((pre*2)%mod+arr[i])%mod;
}
for (ll i=0;i<=n-1;i++)
{
ans=(ans+power(arr[i],2))%mod;
}
cout << ans << endl;
return 0;
} |
#include<iostream>
#include<algorithm>
using namespace std;
#define rep(i, n) for(int i = 0; i < (n); i++)
int main() {
int n;
cin >> n;
const int INF = 1001001001;
int l = 0, r = INF;
rep(i, n) {
int a;
cin >> a;
l = max(l, a);
}
rep(i, n) {
int b;
cin >> b;
r = min(r, b);
}
int ans = r-l+1;
ans = max(0, ans);
cout << ans << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define all(x) sort(x.begin(),x.end())
#define int long long
#define pb push_back
#define pii pair<int,int>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define mod 1e9+7
#define inf 1e18
#define garou99 ios_base::sync_with_stdio(false); cin.tie(NULL);
int pow(int p,int r,int m){
int ans=1;
while(r>0){
if(r&1){
ans=((ans%m)*(p%m))%m;
}
r=r>>1;
p=((p%m)*(p%m))%m;
}
return ans;
}
int32_t main(){
garou99;
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int n;
cin>>n;
int a[n],b[n];
rep(i,0,n)
cin>>a[i];
rep(i,0,n)
cin>>b[i];
set<int> x;
rep(i,a[0],b[0]+1)
x.insert(i);
rep(i,1,n){
set<int> s;
rep(j,a[i],b[i]+1)
s.insert(j);
set<int> an;
for(auto j:s){
if(x.find(j)!=x.end())
an.insert(j);
}
x=an;
}
cout<<x.size();
} |
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T> using vt = vector<T>;
template<class T> using vvt = vector<vt<T>>;
template<class T> using ttt = tuple<T,T>;
using tii = tuple<int,int>;
using tiii = tuple<int,int,int>;
using vi = vector<int>;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define pb push_back
#define mt make_tuple
#define ALL(a) (a).begin(),(a).end()
#define FST first
#define SEC second
#define DEB cerr<<"!"<<endl
#define SHOW(a,b) cerr<<(a)<<" "<<(b)<<endl
#define DIV int(1e9+7)
const int INF = (INT_MAX/2);
const ll LLINF = (LLONG_MAX/2);
const double eps = 1e-8;
//const double PI = M_PI;
inline ll pow(ll x,ll n,ll m){ll r=1;while(n>0){if((n&1)==1)r=r*x%m;x=x*x%m;n>>=1;}return r%m;}
inline ll lcm(ll d1, ll d2){return d1 / __gcd(d1, d2) * d2;}
int cnt_gcj = 0;
#define gcj_put cout<<"Case #" << ++cnt_gcj << ": ";
/* Coding Space */
int table[1502][1502];
bool used[1502][1502];
bool dfs(int y, int x, bool state,int cnt,bool vec){
if(table[y][x] == 2){return state;}
else if(table[y][x] == 1){
bool ret = 0;
if(vec == 0){
ret = dfs(y+1,x,1,cnt+1,vec);
}
else{
ret = dfs(y,x+1,1,cnt+1,vec);
}
return ret;
}else{
bool ret = (vec == 0?dfs(y+1,x,state,cnt+1,vec):dfs(y,x+1,state,cnt+1,vec));
if(ret == 0) return 0;
else{
if(used[y][x]) ;
else used[y][x] = 1;
return ret;
}
}
}
int main(){
int h,w,n,m; cin >> h >> w >> n >> m;
rep(i,n){
int a,b; cin >> a >> b;
table[a][b] = 1;
}
rep(i,m){
int a,b; cin >> a >> b;
table[a][b] = 2;
}
rep(i,w+1) table[0][i] = 2,table[h+1][i] = 2;
rep(i,h+1) table[i][0] = 2, table[i][w+1] = 2;
rep(i,h+1)rep(j,w+1)if(table[i][j] == 2)dfs(i+1,j,0,0,0),dfs(i,j+1,0,0,1);
ll ans = 0;
//rep(i,h+1)rep(j,w+1){if(table[i][j] == 1 || used[i][j] == 1) cerr << i << ' ' << j << endl;}
rep(i,h+1)rep(j,w+1){if(table[i][j] == 1 || used[i][j] == 1) ans++;}
cout << ans << endl;
return 0;
}
| #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
#include <iostream> // IO
#include <cmath> // sqrt, trig, ceil, log
#include <utility> // swap, pair
// #include <algorithm> // sort, binary search, reverse, merge
// #include <bitset>
// #include <cstdio>
// #include <cstdlib> //abs, atoi, rand
// #include <iterator>
// #include <map>
// #include <queue>
// #include <set>
// #include <stack>
// #include <string> // std string
// #include <vector>
// #include <chrono> // non c style time
// #include <complex>
// #include <deque>
// #include <iomanip> // setprecision
// #include <tuple>
#define fo(i, a, b) for(i=a; i<=b; i++)
#define ro(i, b, a) for(i=b; i>=a; i--)
#define foe(it, x) for(auto it=x.begin(); it!=x.end(); it++)
#define ff first
#define ss second
#define pb push_back
#define fil(x, y) memset(x, y, sizeof(x))
#define deb(x) cout << #x << " " << x << "\n"
#define sz(a) ((int)(a.size()))
using namespace std;
using ll = long long;
const int N = 1e5+3;
const int inf = 1e9+3;
const ll M = 1e9+7;
const ll T = 998244353;
const double pi = acos(-1);
void solve()
{
int a,b,x,y,fi,si;cin>>a>>b>>x>>y;
if(b<a){
fi = x;
si = min(y,2*x);
cout << fi + abs(b-(a-1))*si << "\n";
}
else {
fi = x;
si = min(y,2*x);
cout << fi + abs(b-a)*si << "\n";
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifdef Holmes7
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif
int _t=1,i;
//cin >> _t;
for(i=1;i<=_t;i++){
//cout << "Case #" << i << ": ";
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
// Type alias
using ll = long long;
using ld = double;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
using ti3 = tuple<int, int, int>;
using vi = vector<int>;
using vll = vector<ll>;
using vld = vector<ld>;
using vpi = vector<pi>;
using vpll = vector<ll>;
using vpld = vector<ld>;
using vti3 = vector<ti3>;
// Constants
const double EPS = 1e-9;
const int inf = numeric_limits<int>::max() / 2;
const ll llinf = numeric_limits<ll>::max() / 2;
const ll mod = 1e9 + 7;
// Macro
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define rrep(i, n) for (int i = int(n - 1); i >= 0; --i)
#define REP(i, a, b) for (int i = int(a); i < int(b); ++i)
#define RREP(i, a, b) for (int i = int(b - 1); i >= int(a); --i)
#define SHOW(a) cout << #a << " = " << a << endl
#define ARR(a, n) \
for (int i = 0; i < int(n); ++i) \
cout << #a << "[" << i << "]" \
<< " = " << a[i] << endl
#define ALL(a) a.begin(), a.end()
// Funtcions
ll pow(ll base, ll i, ll mod) {
ll a = 1;
while (i) {
if (i & 1) {
a *= base;
a %= mod;
}
base *= base;
base %= mod;
i >>= 1;
}
return a;
}
ll gcd(ll a, ll b) {
while (b) {
ll c = a % b;
a = b;
b = c;
}
return a;
}
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
template <typename T> bool chmin(T &a, const T &b) {
if (a > b)
return a = b, true;
else
return false;
}
template <typename T> bool chmax(T &a, const T &b) {
if (a < b)
return a = b, true;
else
return false;
}
struct hoge {
ll color, left, right;
};
void solve() {
ll n;
cin >> n;
pll xc[200000 + 5];
rep(i, n) {
ll t, u;
cin >> t >> u;
xc[i] = make_pair(u, t);
}
sort(begin(xc), xc + n);
vector<hoge> a;
hoge b;
b.color = xc[0].first;
b.left = xc[0].second;
a.push_back(b);
for (ll i = 1; i < n; i++) {
if (xc[i - 1].first != xc[i].first) {
a[a.size() - 1].right = xc[i - 1].second;
hoge b;
b.color = xc[i].first;
b.left = xc[i].second;
a.push_back(b);
}
}
a[a.size() - 1].right = xc[n - 1].second;
vector<vector<pll>> d(a.size(), vector<pll>(2, make_pair(llinf, 0)));
d[0][0] = make_pair(abs(a[0].left) + abs(a[0].right - a[0].left), a[0].right);
d[0][1] = make_pair(abs(a[0].right) + abs(a[0].left - a[0].right), a[0].left);
for (ll i = 1; i < a.size(); i++) {
d[i][0] =
make_pair(min(d[i - 1][0].first + abs(d[i - 1][0].second - a[i].left) +
abs(a[i].left - a[i].right),
d[i - 1][1].first + abs(d[i - 1][1].second - a[i].left) +
abs(a[i].left - a[i].right)),
a[i].right);
d[i][1] =
make_pair(min(d[i - 1][0].first + abs(d[i - 1][0].second - a[i].right) +
abs(a[i].left - a[i].right),
d[i - 1][1].first + abs(d[i - 1][1].second - a[i].right) +
abs(a[i].left - a[i].right)),
a[i].left);
}
cout << min(d[a.size() - 1][0].first + abs(d[a.size() - 1][0].second),
d[a.size() - 1][1].first + abs(d[a.size() - 1][1].second))
<< endl;
}
int main() {
// FastIO
// ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define int ll
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vpii = vector<pii>;
using vvpii = vector<vpii>;
template<typename T, typename U>
ostream& operator<<(ostream& o, const pair<T,U>& p) {
return o << "(" << p.first << "," << p.second << ")";
}
template<typename T, typename U>
ostream& operator<<(ostream& o, const map<T,U>& m) {
o << "{";
for (const pair<T,U>& p : m)
o << p.first << ": " << p.second << ",";
return o << "}";
}
template<typename T>
ostream& operator<<(ostream& o, const set<T>& s) {
o << "{";
for (const T& e : s)
o << s << ",";
return o << "}";
}
template<typename T>
ostream& operator<<(ostream& o, const vector<T>& v) {
o << "[";
for (const T& x : v)
o << x << ",";
return o << "]";
}
template<typename T>
ostream& operator<<(ostream& o, const vector<vector<T>>& v) {
o << "[";
for (const vector<T>& x : v)
o << x << ",\n";
return o << "]";
}
const int INF = 1e18;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int a, b;
cin >> a >> b;
if (a == b) {
for (int i=1; i<=a; ++i)
cout << i << " -" << i << " ";
cout << "\n";
return 0;
}
string bsign = "-";
string asign = "";
if (a > b) {
swap(a, b);
swap(asign, bsign);
}
assert(a < b);
for (int i=1; i<=b; ++i)
cout << bsign << i << " ";
for (int i=1; i<a; ++i)
cout << asign << i << " ";
int sum = 0;
for (int i=a; i<=b; ++i)
sum += i;
cout << asign << sum << "\n";
}
|
#include <bits/stdc++.h>
#define lc (o<<1)
#define rc ((o<<1)|1)
#define PB push_back
#define MK make_pair
using namespace std;
#define DebugP(x) cout << "Line" << __LINE__ << " " << #x << "=" << x << endl
const int maxn = 5000 + 5;
const int modu = 998244353; // 1e9 + 7
const int inf = 0x3f3f3f3f;
const double eps = 1e-5;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
typedef long long LL;
void read(LL &x)
{
x=0;int f=0;char ch=getchar();
while(ch<'0'||ch>'9') {f|=(ch=='-');ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
x=f?-x:x;
return;
}
void read(int &x) { LL y; read(y); x = (int)y; }
void read(char &ch) { char s[3]; scanf("%s", s); ch = s[0]; }
void read(char *s) { scanf("%s", s); }
template<class T, class ...U> void read(T &x, U& ... u) { read(x); read(u...); }
int n, m;
int a[maxn];
LL inv[maxn], c[maxn], f[20][maxn];
int b[20];
int main() {
// freopen("my.txt", "w", stdout);
read(n, m);
inv[1] = 1;
for (int i = 2; i <= n; ++i)
inv[i] = (modu - (modu/i))*inv[modu%i]%modu;
c[0] = 1;
for (int i = 1; i <= n; ++i)
c[i] = (n-i+1)*inv[i]%modu*c[i-1]%modu;
int nb = 0;
while (m) {
b[nb++] = m%2;
m /= 2;
}
memset(f, 0, sizeof(f));
f[nb-1][0] = 1;
int tot = b[nb-1];
for (int i = nb-2; i >= 0; --i) {
for (int j = 0; j <= tot*2 + b[i]; ++j) if (j%2 == 0) {
for (int k = 0; k <= j && k <= n; k += 2)
f[i][j] = (f[i][j] + f[i+1][(j-k)/2]*c[k]) % modu;
}
tot = tot*2 + b[i];
}
printf("%lld\n", f[0][tot]);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int n;
int a[505];
int ans[250005],len;
int p=1;
void doSwap(int pos) {
if(p%2==1) {
int x=rand()%2;
if(2*x+1==pos || 2*x+2==pos) {
x^=1;
}
swap(a[2*x+1],a[2*x+2]);
ans[++len]=2*x+1;
p++;
}else {
if(pos==2 || pos==3) {
swap(a[4],a[5]);
ans[++len]=4;
p++;
}else {
swap(a[2],a[3]);
ans[++len]=2;
p++;
}
}
}
void sortSwap4() {
if(a[4]==4) {
return;
}
int pos=0;
for(int j=1;j<=4;j++) {
if(a[j]==4) {
pos=j;
break;
}
}
if(pos==1) {
if(p%2==0) {
swap(a[2],a[3]);
ans[++len]=2;
p++;
}
swap(a[1],a[2]);
ans[++len]=1;
swap(a[2],a[3]);
ans[++len]=2;
swap(a[3],a[4]);
ans[++len]=3;
p++;
}else if(pos==2) {
if(p%2==1) {
swap(a[3],a[4]);
ans[++len]=3;
p++;
}
swap(a[2],a[3]);
ans[++len]=2;
swap(a[3],a[4]);
ans[++len]=3;
}else if(pos==3) {
if(p%2==0) {
swap(a[2],a[3]);
ans[++len]=2;
swap(a[3],a[4]);
ans[++len]=3;
swap(a[2],a[3]);
ans[++len]=2;
swap(a[3],a[4]);
ans[++len]=3;
}else {
swap(a[3],a[4]);
ans[++len]=3;
p++;
}
}
}
int main() {
srand(time(0));
int T;
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
for(int i=1;i<=n;i++) {
scanf("%d",&a[i]);
}
if(n==2) {
if(a[1]!=1) {
swap(a[1],a[2]);
printf("1\n1\n");
}else {
printf("0\n\n");
}
continue;
}
len=0;
p=1;
for(int i=n;i>=5;i--) {
int pos=0;
for(int j=1;j<=n;j++) {
if(a[j]==i) {
pos=j;
break;
}
}
if((pos%2==0 && p%2==1) || (pos%2==1 && p%2==0)) {
doSwap(pos);
}
//printf("Value at position %d: %d\n",pos,a[pos]);
for(int j=pos;j<i;j++) {
swap(a[j],a[j+1]);
ans[++len]=j;
p++;
}
//printf("Stage 1 level %d: ",i);
}
//Sort 1-4
if(n>=4) {
sortSwap4();
}
//Sort 1-3
while(a[1]!=1 || a[2]!=2 || a[3]!=3) {
if(p%2==1) {
swap(a[1],a[2]);
ans[++len]=1;
p++;
}else {
swap(a[2],a[3]);
ans[++len]=2;
p++;
}
}
printf("%d\n",len);
for(int i=1;i<=len;i++) {
printf("%d ",ans[i]);
}
printf("\n");
}
return 0;
} |
#include<iostream>
#include<algorithm>
#include<string>
#include<map>
#define rep(i, n) for(int i=0; i<n; i++)
using namespace std;
typedef long long ll;
int main(){
int n;
cin >> n;
if(n%2==0) cout << "White" << endl;
else cout << "Black" << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define ull unsigned long long
#define loops(i, s, n) for (ll i = s; i < (ll)n; i++)
#define loop(i, n) loops(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define pub push_back
#define pob pop_back
#define mp make_pair
#define dump(x) cerr << #x << " = " << (x) << endl;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vl;
// for dp
// 第一引数と第二引数を比較し、第一引数(a)をより大きい/小さい値に上書き
template <typename T>
inline bool chmin(T &a, const T &b)
{
bool compare = a > b;
if (a > b)
a = b;
return compare;
}
template <typename T>
inline bool chmax(T &a, const T &b)
{
bool compare = a < b;
if (a < b)
a = b;
return compare;
}
#define in_v(type, name, cnt) \
vector<type> name(cnt); \
loop(i, cnt) cin >> name[i];
#define sort_v(v) std::sort(v.begin(), v.end())
#define unique_v(v) v.erase(std::unique(v.begin(), v.end()), v.end()) //必ずソート後に実行
#define set_fix(x) ((std::cerr << std::fixed << std::setprecision(x)), (std::cout << std::fixed << std::setprecision(x)))
//cout for vector
template <class T>
ostream &operator<<(ostream &o, const vector<T> &v)
{
o << "{";
for (int i = 0; i < (int)v.size(); i++)
o << (i > 0 ? ", " : "") << v[i];
o << "}";
return o;
}
// gcd
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);
}
//prime numbers
bool IsPrime(ll num)
{
if (num < 2)
return false;
else if (num == 2)
return true;
else if (num % 2 == 0)
return false;
double sqrtNum = sqrt(num);
for (ll i = 3; i <= sqrtNum; i += (ll)2)
{
if (num % i == 0)
{
return false;
}
}
return true;
}
vector<ll> Eratosthenes(ll N)
{
int arr[N];
vector<ll> res;
for (int i = 0; i < N; i++)
{
arr[i] = 1;
}
for (int i = 2; i < sqrt(N); i++)
{
if (arr[i])
{
for (int j = 0; i * (j + 2) < N; j++)
{
arr[i * (j + 2)] = 0;
}
}
}
for (int i = 2; i < N; i++)
{
if (arr[i])
{
res.push_back(i);
}
}
return res;
}
//digit number
int GetDigit(ll num, ll radix)
{
unsigned digit = 0;
while (num != 0)
{
num /= radix;
digit++;
}
return digit;
}
int digsum(ll n)
{
ll res = 0;
while (n > 0)
{
res += n % (ll)10;
n /= (ll)10;
}
return res;
}
bool isConsistOfOnly5(ll n)
{
if (n == 1)
return false;
bool ret = true;
while (n > 1)
{
if (n % 5 != 0)
ret = false;
n /= 5;
}
return ret;
}
ll func(ll n, ll a)
{
ll ret = 0;
while (n > 1)
{
n /= a;
++ret;
}
return ret;
}
int main()
{
ll n;
cin >> n;
for (ll a = 3; a < n; a *= 3)
{
ll b = n - a;
if (isConsistOfOnly5(b))
{
cout << func(a, 3) << ' ' << func(b, 5) << endl;
return 0;
}
}
cout << -1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define vint vector<int>
#define vll vector<ll>
#define vstr vector<string>
#define vvint vector<vector<int> >
#define vvll vector<vector<ll> >
#define vip vector<pair<int,int> >
#define vlp vector<pair<ll,ll> >
#define input(arr) for(auto &x:arr) cin>>x;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int N = 100005;
const ll mod = 1000000007;
const ll inf = 1e17;
const long double pi = (acos(-1));
bool csort(const pair<int,int>&a, const pair<int,int>&b){
return a.second < b.second;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n; cin>>n;
vint arr(n); input(arr);
int sum = accumulate(all(arr),0);
vector<vector<bool>> dp(n+1, vector<bool>(sum+1,false));
for(int i=0;i<=n;++i)
dp[i][0] = true;
for(int i=1;i<=n;++i){
for(int j=1;j<=sum;++j){
if(j<arr[i-1])
dp[i][j] = dp[i-1][j];
else
dp[i][j] = dp[i-1][j] || dp[i-1][j-arr[i-1]];
}
}
int i = sum/2 + (sum&1);
//cout<<i<<endl;
while(i<=sum && (!dp[n][i])){
++i;
}
cout<<i;
}
| #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
int n;
int a[105],dp[105][100005];
int main(){
cin>>n;
int sum=0;
for(int i=1;i<=n;i++)cin>>a[i],sum+=a[i];
dp[0][0]=1;
for(int i=0;i<n;i++)
for(int j=0;j<=sum;j++){
if(dp[i][j]){
dp[i+1][j]=1;
dp[i+1][j+a[i+1]]=1;
}
}
for(int i=(sum%2==0)?(sum/2):(sum/2+1);i<=sum;i++)
if(dp[n][i]){
cout<<i;
break;
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
int n,k,u,v;
vector<int> adj[200002];
vector<int> leaf;
int cnt = 0;
bool vis[200002];
int dfs(int u , int t){
vis[u] = true;
int cur = 0;
int rec = 0;
for(int i=0;i<adj[u].size();i++){
if(!vis[adj[u][i]]){
int temp = dfs(adj[u][i],t);
cur = max(cur,1+temp);
rec = min(rec,1+temp);
}
}
rec = - rec;
if(cur < rec){
cur = -rec;
}
if(cur == t){
cur = -t-1;
++cnt;
}
// cout<<u<<" "<<cur<<"\n";
return cur;
}
bool poss(int t){
//cout<<t<<"\n";
for(int i=0;i<n;i++)
vis[i] = false;
cnt = 0;
int c = dfs(0,t);
if(c>=0){
++cnt;
}
if(cnt<=k)
return true;
else
return false;
}
int main()
{
cin>>n>>k;
for(int i=0;i<n-1;i++){
cin>>u>>v;
--u,--v;
adj[u].pb(v);
adj[v].pb(u);
}
if(k>=n){
cout<<0;
return 0;
}
int l = 1;
int r = (n/2) + 1;
int ans = -1;
while(l<=r){
int mid = (l+r)/2;
if(poss(mid)){
ans = mid;
r = mid-1;
}else{
l = mid+1;
}
}
cout<<ans;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int vi[200001];
vector<int> g[200001];
ll A[200001], b[200001];
ll ans=-1000000001;
ll maxi[200001];
void predfs(int u)
{
vi[u]=1;
for(int j=0;j<g[u].size();j++)
{
int v=g[u][j];
if(!vi[v])
{
predfs(v);
}
maxi[u]=max(maxi[u], maxi[v]);
maxi[u]=max(maxi[u], A[v]);
}
}
int main()
{
int N, M;
cin >> N >> M;
memset(vi, 0, sizeof vi);
for(int i=1;i<=N;i++)
{
cin >> A[i];
maxi[i]=-1000000001;
}
int X, Y;
for(int i=0;i<M;i++)
{
cin >> X >> Y;
g[X].push_back(Y);
}
for(int i=1;i<=N;i++)
{
if(!vi[i])
{
predfs(i);
}
}
for(int i=1;i<=N;i++)
{
vi[i]=0;
ans=max(ans, maxi[i]-A[i]);
}
cout << ans;
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define pb emplace_back
#define mp make_pair
#define fi first
#define se second
#define all(v) v.begin(),v.end()
#define run ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0);
#define mod 1000000007
#define decimal(n,k) cout<<fixed<<setprecision(k)<<n<<endl
#define LL_MAX LLONG_MAX
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
int exp(int x,int y){int res=1;x=x%mod;while(y>0){if(y&1)res=(res*x)%mod;y=y>>1;x=(x*x)%mod;}return res;}
int modinv(int x){return exp(x,mod-2);}
int add(int a,int b){a%=mod,b%=mod;a=((a+b)%mod+mod)%mod;return a;}
int sub(int a,int b){a%=mod,b%=mod;a=((a-b)%mod+mod)%mod;return a;}
int mul(int a,int b){a%=mod,b%=mod;a=((a*b)%mod+mod)%mod;return a;}
int gcd(int a, int b){if (b == 0)return a; return gcd(b, a % b);}
int Log2n( int n){ return (n > 1) ? 1 + Log2n(n / 2) : 0;}
int fac[100009];int ncr_mod(int n,int k){int ans=fac[n];ans*=modinv(fac[k]);ans%=mod;ans*=modinv(fac[n-k]);ans%=mod;return ans;}
vector<int>v_prime;void Sieve(int n){bool prime[n + 1];memset(prime,true,sizeof(prime));for (int p = 2; p*p <=n;p++){if(prime[p] ==true) {for(int i = p*p; i<= n; i += p)prime[i]=false;}}for(int p = 2;p<= n;p++)if (prime[p])v_prime.pb(p);}
vector<int>v_factor;void factors(int n){ for (int i=1; i<=sqrt(n); i++) {if (n%i == 0) {if (n/i == i) v_factor.pb(i);else { v_factor.pb(i),v_factor.pb(n/i);};} } sort(all(v_factor)); }
void out(vector<int>&a){for(int i=0;i<a.size();i++) cout<<a[i]<<" "; cout<<endl;}
// s.top().second;
signed main()
{
run;
/*
open for mod factorial
fac[0]=1;
for(int i=1;i<100009;i++)
{
fac[i]=fac[i-1]*i;
fac[i]%=mod;
}
*/
/*
for sieve open this and use v_prime
int pp=pow(10,6)+100000;
Sieve(pp);
*/
// USE v_factor For calculating factors
int t=1;
//cin>>t;
while(t--)
{
// vector< pair <int,int> > vp;
// map<int,int>mp; // x.second-->frequency
// set<int>st;
int n,i,x,y,ok=0,sum=0,ans=0,j,k,cnt=0,m,c=0;
int h[100009]={0};
cin>>n;
double res=0;
for(i=1;i<n;i++)
{
res+=(double)n/(n-i);
}
decimal(res,11);
}
} | #include<bits/stdc++.h>
using namespace std;
using Graph=vector<vector<int>>;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
typedef long long ll;
int main(){
string X;
cin>>X;
string Y;
int N=X.size();
for(int i=0;i<N;i++){
if(X[i]=='.'){
break;
}
else{
cout<<X[i];
}
}
} |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for (int i = 0; i < (n); ++i)
int main(){
int n;
std::cin >> n;
ll ans=0;
rep(i,n){
ll a,b;
std::cin >> a>>b;
ll add=(a+b)*(b-a+1)/2;
ans+=add;
}
std::cout << ans << '\n';
return 0;
}
| #include<iostream>
using namespace std;
const int N=1e6+10;
int a[N];
int main(){
int n;
cin>>n;
int sum=0;
for(int i=0;i<n;i++){
cin>>a[i];
if(a[i]>10){
sum=sum+(a[i]-10);
}
}
cout<<sum;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int judge(int a,int b){
int c=a+b;
if(c>=15&&b>=8){
cout << 1 << endl;
}
else if(c>=10&&b>=3){
cout << 2 << endl;
}
else if(c>=3){
cout << 3 << endl;
}
else{
cout << 4 << endl;
}
return 0;
}
int main(){
int a,b;
cin >> a >> b;
judge(a,b);
} | #include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
inline void read(int& x){
static char c;
while(!isdigit(c=getchar()));x=c^48;
while(isdigit(c=getchar()))x=(x*10)+(c^48);
}
inline void read(ll& x){
static char c;
while(!isdigit(c=getchar()));x=c^48;
while(isdigit(c=getchar()))x=(x*10)+(c^48);
}
int A,B;
inline void Work(){
read(A),read(B),A+=B;
if(A>=15&&B>=8) return puts("1"),void();
if(A>=10&&B>=3) return puts("2"),void();
if(A>=3) return puts("3"),void();
puts("4");
}
int main(){Work();}
|
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//turn on extra precision
//#pragma GCC target("fpmath=387")
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef string str;
typedef pair <int,int> pii;
typedef pair <ll,ll> pll;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <pii> vpii;
typedef vector <pll> vpll;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ordered_multiset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
#define mp make_pair
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define fi first
#define se second
#define fs first.second
#define ss second.second
#define ff first.first
#define sf second.first
#define newl '\n'
#define fbo find_by_order
#define ook order_of_key
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(),x.rend()
#define watch(x) cout << (#x) << " is : " << (x) << newl
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
vi dirx = {0,0,1,-1};
vi diry = {1,-1,0,0};
char to_upper (char x){
if( 97 <= int(x) && int(x) <= 122) return char(x-32);
if( 65 <= int(x) && int(x) <= 90) return x;
return -1;
}
char to_lower (char x){
if( 97 <= int(x) && int(x) <= 122) return x;
if( 65 <= int(x) && int(x) <= 90) return char(x+32);
return -1;
}
int numerize (char x){
if(48 <= int(x) && int(x) <= 57) return int(x-'0');
if(97 <= int(x) && int(x) <= 122) return int(x-96);
if(65 <= int(x) && int(x) <= 90) return int(x-64);
return -1;
}
bool isect (int l1, int r1, int l2, int r2){ return max(l1,l2) <= min(r1,r2); }
ll quickpow (ll num1, ll num2, ll MOD){
if(num2==0)return 1%MOD;
else if(num2==1)return num1%MOD;
else{
ll temp = quickpow (num1,num2>>1LL,MOD); ll res = ((temp%MOD) * (temp%MOD))%MOD;
if(num2&1) res = ((res%MOD)*(num1%MOD))%MOD; return res;
}
}
ll invmod (ll num, ll MOD){return quickpow (num,MOD-2,MOD);}
ll gcd (ll num1, ll num2){
if(num1 < num2) swap(num1,num2); ll num3 = num1 % num2 ;
while(num3 > 0){ num1 = num2; num2 = num3; num3 = num1 % num2;}
return num2;
}
ll lcm (ll num1 , ll num2){return (ll) (num1/__gcd(num1,num2))*num2;}
// end of Template
int main(){
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
str s; cin >> s;
for(int i = 0; i < s.size(); ++i){
if(i % 2 == 0){
if(s[i] == to_lower(s[i])) continue;
else {return cout << "No\n", 0;}
}
else{
if(s[i] == to_upper(s[i])) continue;
else {return cout << "No\n", 0;}
}
}
cout << "Yes\n";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main(){
char c;
bool o = 0, f = 0;
while((c = getchar()) > 64){
if(o != (c<97)){
f = 1;
break;
}
o = !o;
}
cout << (f? "No" : "Yes") << endl;
} |
#include <bits/stdc++.h>
using namespace std;
long long count_inward(long long B, long long C){
long long ans = 1;
if(C){
if(B > 0){
ans++;
}
if(B < 0){
B *= -1;
C--; ans++;
}
}
if(B == 0 or C == 0) return ans;
long long k = min(B-1, (C-1)/2);
ans += 2*k; C -= 2*k; B -= k;
if(C >= 2) ans++;
return ans;
}
long long count_outward(long long B, long long C){
long long ans = 0;
if(B > 0 and C){
B *= -1; C--;
}
if(C == 0) return ans;
long long k = (C-1)/2;
ans += 2*k; C -= 2*k; B -= k;
if(C >= 2) ans++;
return ans;
}
int main(){
long long B, C;
while(scanf("%lld%lld", &B, &C) > 0){
printf("%lld\n", count_inward(B, C) + count_outward(B, C));
}
}
| #include <bits/stdc++.h>
using namespace std;
using int64 = long long;
constexpr int DEBUG = 0;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int64 b, c;
cin >> b >> c;
int64 l1 = b - c / 2;
int64 u1 = b + max(c - 2, 0LL) / 2;
int64 l2 = -b - max(c - 1, 0LL) / 2;
int64 u2 = -b + max(c - 1, 0LL) / 2;
int64 c1 = u1 - l1 + 1;
int64 c2 = u2 - l2 + 1;
int64 ans = c1 + c2;
int64 l3 = max(l1, l2);
int64 u3 = min(u1, u2);
if (l3 <= u3) {
ans -= u3 - l3 + 1;
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N, S, D;
cin >> N >> S >> D;
vector<pair<int, int>> magic(N);
bool a = false;
for (int i = 0; i < N; i++) {
cin >> magic.at(i).first;
cin >> magic.at(i).second;
if(magic.at(i).first < S && D < magic.at(i).second) {
a = true;
}
}
if (a == true) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n[N];
int main(){
int a, b , c ,d;
cin >> a >> b >> c>>d;
cout << b -c;
}
|
#include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/pb_ds/tree_policy.hpp"
using namespace std;
#pragma GCC optimize("O3","unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC target("avx2")
#define sync ios_base::sync_with_stdio(0); cin.tie(0);
#define all(x) x.begin(),x.end()
#define unq(a) sort(all(a));a.resize(unique(all(a)) - a.begin())
#define ll long long
#define ld float
#define pb push_back
#define fi first
#define se second
#define endl '\n'
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rng(1);
using pii = pair<int , int>;
int inline max(const int x, const int y){return (x > y ? x : y);}
int inline min(const int x, const int y){return (x < y ? x : y);}
int inline abs(const int x){return (x < 0 ? -x : x);}
const int N = 2e5 + 5, mod = 1e9 + 7;
ll power(ll x, ll y){
ll r = 1;
while(y > 0){
if (y & 1){
r *= x; r %= mod;
}
y >>= 1;
x *= x; x %= mod;
}
return r;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
sync
int t = 1;
//cin >> t;
while(t--){
int n;
cin >> n;
vector<ll> a(n);
for (ll& x : a) cin >> x;
ll cnt = 0;
vector<ll> dp0(n + 1), dp1(n + 1), ways(n + 1);
dp0[1] = dp1[1] = 1;
ways[0] = 1; ways[1] = 2;
for (int i = 2; i <= n; i++){
dp0[i] = dp1[i - 1]; // minus
dp1[i] = dp0[i - 1] + dp1[i - 1]; // plus
dp0[i] %= mod; dp1[i] %= mod;
ways[i] = (dp0[i] + dp1[i]) % mod;
}
cnt = (ways[n - 1] * a[0]) % mod;
for (int i = 1; i < n; i++){
int l = i - 1, r = n - i - 1;
cnt += ((ways[l] * ways[r]) % mod * a[i]) % mod; cnt %= mod;
//cout << a[i] << " -> " << ways[l] * ways[r] << endl;
l--; r--;
l = max(l , 0); r = max(r , 0);
cnt += ((ways[l] * ways[r]) % mod * (mod - a[i]) % mod) % mod; cnt %= mod;
//cout << -a[i] << " -> " << ways[l] * ways[r] << endl;
}
cout << cnt << endl;
}
cerr << "processor time: " << clock() / (double) CLOCKS_PER_SEC << "s ";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
// 型
#define ll int64_t // ll -> int64_t
using graph = vector<vector<int>>; // グラフ型
// for文
#define lp(i,n) for(int i=0;i<n;i++) // 変数 i をおいて n 回繰り返し
#define lp_(i,n) for(int i=0;i<=n;i++) // 条件に=つき
#define lpp(i,n,m) for(int i=n;i<m;i++) // 開始と終了を指定して繰り返し
#define lpp_(i,n,m) for(int i=n;i<=m;i++) // 開始と終了を指定,条件に=つき
// vector定義
#define _GLIBCXX_DEBUG
#define vec(v,n) vector<int> v(n) // int
#define vec_(v,n,m) vector<int> v(n,m) // int 初期条件あり
#define vec64(v,n) vector<ll> v(n) // int64_t
#define vec64_(v,n,m) vector<ll> v(n,m) // int64_t 初期条件あり
#define vecc(v,n) vector<char> v(n) // char
#define vecs(v,n) vector<string> v(n) // string
#define vece(v) vector<int> v; // int 空
#define vecec(v) vector<char> v; // char 空
#define vec2i(v,h,w) vector<vector<int>> v(h,vector<int>(w)) // 二次元配列 int
#define vec2c(v,h,w) vector<vector<char>> v(h,vector<char>(w)) // 二次元配列 char
// vector,string操作
#define all(v) v.begin(),v.end()
#define back(v) v.back() // 最後の要素
#define sum(v) accumulate(all(v),0) // 総和
#define sort(v) sort(all(v)) // ソート
#define reverse(v) reverse(all(v)) // 反転
#define lower(v,x) lower_bound(all(v),x)-v // x<=a[i]となる最小のi / a[i]<xとなるiの個数
#define cou(v,n) count(all(v),n) // n の出現回数を数える
#define pb(v,n) v.push_back(n) // 最後尾に n を追加
#define ins(v,i,n) v.insert(v.begin()+i,n) // 場所を指定してv[i]に n を追加
#define del(v) v.pop_back() // 最後尾を削除
#define del0(v) v.erase(v.begin()) // 最初の要素v[0]を削除
#define del_(v,i) v.erase(v.begin()+i) // 場所を指定してv[i]を削除
#define del__(v,i,j) v.erase(v.begin()+i,v.begin()+j+1) // 範囲を指定してv[i]~v[j]を削除
#define sub(s,i,j) s.substr(i,j-i+1) // 範囲を指定してs[i]~s[j]を取得【stringのみ】
// others
const ll MOD=998244353;
const ll INF=(1LL<<60);
#define under(n) cout<<fixed<<setprecision(n) // 小数点以下の桁数を指定
#define cout(n) cout<<n<<endl
#define mypair pair<int,int>
int main()
{
ll ans=1;
int h,w;
cin>>h>>w;
vec2c(a,h,w);
lp(i,h)
{
lp(j,w)
cin>>a[i][j];
}
for(int i=1;i<w;i++)
{
int flag=0;
int px=0,py=i;
for(;;)
{
if(a[px][py]=='R')
{
if(flag==0||flag==1)
flag=1;
else
{
ans=0;
//goto EN;
}
}
else if(a[px][py]=='B')
{
if(flag==0||flag==2)
flag=2;
else
{
ans=0;
//goto EN;
}
}
px++;
py--;
if(px>=h||py<0)
break;
}
if(flag==0)
{
ans*=2;
ans%=MOD;
}
}
for(int i=1;i<h-1;i++)
{
int flag=0;
int px=i,py=w-1;
for(;;)
{
if(a[px][py]=='R')
{
if(flag==0||flag==1)
flag=1;
else
{
ans=0;
//goto EN;
}
}
else if(a[px][py]=='B')
{
if(flag==0||flag==2)
flag=2;
else
{
ans=0;
//goto EN;
}
}
px++;
py--;
if(px>=h||py<0)
break;
}
if(flag==0)
{
ans*=2;
ans%=MOD;
}
}
if(a[0][0]=='.')
{
ans*=2;
ans%=MOD;
}
if(a[h-1][w-1]=='.')
{
ans*=2;
ans%=MOD;
}
cout(ans);
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define f first
#define s second
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll s,p;
cin >> s >> p;
for(ll i=1; i*i<=p; i++)
{
ll x = i , y = p/i;
if (x*y == p && x+y==s) return cout << "Yes" << '\n' , 0;
}
cout << "No\n";
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define INF ((1LL<<62)-(1LL<<31))
typedef long long ll;
int main()
{
int n;
cin >> n;
vector<ll> c(n*n);
rep(i,n*n) cin >> c[i];
ll mi=INF,ni;
rep(i,n) {
if(mi>c[i*n]) {
mi=c[i*n];
ni=i*n;
}
}
vector<ll> v;
rep(i,n) {
ll check=c[i*n]-mi;
bool flag=true;
rep(j,n) if(c[i*n+j]-c[ni+j]!=check) flag=false;
//cout << check << endl;
if(!flag) {
cout << "No" << endl;
return 0;
}
v.push_back(check);
}
cout << "Yes" << endl;
rep(i,n) {
if(i==0) cout << v[i];
else cout << " " << v[i];
}
cout << endl;
rep(i,n) {
if(i==0) cout << c[ni];
else cout << " " << c[ni+i];
}
cout << endl;
return 0;
} |
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#include <algorithm>
#include <iostream>
#include <bitset>
#include <vector>
#include <array>
using namespace std;
#define f(x,y,z) for(int x=y;x<=z;++x)
vector<int> v[200001];
int n, a, b, it;
array<int, 200001> jr, dlm, nmr;
bitset<200001> sdh;
void jrk(int x) {
for(int &i:v[x]) if(!jr[i]) {
jr[i] = jr[x]+1, jrk(i);
}
}
void trn(int x) {
sdh[x] = true;
for(int &i:v[x]) if(!sdh[i]) {
trn(i), dlm[x] = max(dlm[x], dlm[i]+1);
}
}
void isi(int x) {
sdh[x] = true, nmr[x] = a;
vector<pair<int, int>> mn;
for(int &i:v[x]) if(!sdh[i]) mn.push_back({dlm[i], i});
sort(mn.begin(), mn.end());
for(auto &i:mn) ++a, isi(i.second), ++a;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
cin >> n;
f(i,2,n) {
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
jr[1] = a = 1, jrk(1);
it = max_element(jr.begin()+1, jr.begin()+n+1)-jr.begin();
trn(it), sdh.reset(), isi(it);
f(i,1,n) cout << nmr[i] << ' ';
} | #include <iostream>
using namespace std;
const int N = 400'009;
int p[N];
int find(int x) {
if (p[x] != x) {
p[x] = find(p[x]);
}
return p[x];
}
int sz[N];
int cycle[N];
int main() {
#ifndef LOCAL
cin.tie(0);
#endif
ios::sync_with_stdio(false);
int n;
cin >> n;
for (int i = 0; i < N; ++i) {
p[i] = i;
sz[i] = 1;
cycle[i] = 0;
}
for (int i = 0; i < n; ++i) {
int x, y;
cin >> y >> x;
--x;
--y;
int px = find(x);
int py = find(y);
if (py != px) {
p[py] = px;
sz[px] += sz[py];
if (cycle[py]) {
cycle[px] = 1;
}
} else {
cycle[px] = 1;
}
}
int ans = 0;
for (int i = 0; i < N; ++i) {
if (find(i) == i) {
ans += sz[i] - 1 + cycle[i];
}
}
cout << ans << '\n';
}
|
#include<bits/stdc++.h>
using namespace std;
#define DONTSYNC ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL) //dont use stdio with iostream functions //input and output are out of order now!
#define TEST unsigned long long T; cin>>T; while(T--) //loop over each testcase
#define endl "\n"
#define fori(a,start,end) for(int a=start;a<end;a++)
#define forl(a,start,end) for(long long a=start;a<end;a++)
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
const double PI = acos(-1);
long long binpow(long long a, long long b, long long m) {
a %= m;
long long res = 1;
while (b > 0) {
if (b & 1)
res = res * a % m;
a = a * a % m;
b >>= 1;
}
return res;
}
long long inverse(long long a,long long m){
return binpow(a,m-2,m);
}
// modular
long long binomial_coefficient(long long n,long long r,long long m, vector<long long> &fact, vector<long long> &invfact) {
if(r>n) return 0;
return fact[n] * invfact[r]%m * invfact[n - r]%m;
}
// helper
void compute_fact(vector<long long> &fact, long long m){
int n=fact.size();
fact[0] = 1;
for (int i = 1; i < n; i++) {
fact[i] = fact[i - 1] * i % m;
}
}
void compute_invfact(vector<long long> &invfact, long long m){
int n=invfact.size();
invfact[0] = 1;
for (int i = 1; i < n; i++) {
invfact[i] = invfact[i - 1] * inverse(i,m) % m;
}
}
const ll mod=1e9+7;
void solve(){
/* code */
ll n,m,k,MX=2e6+5; cin>>n>>m>>k;
vl fact(MX),invfact(MX); compute_fact(fact,mod); compute_invfact(invfact,mod);
if(n>m+k){
cout<<0<<endl;
return;
}
ll res=mod+binomial_coefficient(m+n,n,mod,fact,invfact)-binomial_coefficient(m+n,m+k+1,mod,fact,invfact);
cout<<res%mod<<endl;
}
int main()
{
DONTSYNC;
// TEST
solve();
return 0;
}
| #include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#line 1 "lib/combination.hpp"
#line 1 "lib/modint.hpp"
#include <iostream>
// Modint
struct Mint {
static const long long mod = (long long)1e9 + 7;
long long val;
Mint() { val = 0; }
Mint(long long a) {
val = a;
verify_value();
}
void verify_value() {
if (val >= mod) val %= mod;
if (val < 0) val %= mod, val += mod;
}
Mint pow(long long p) const {
Mint cur = Mint(val), ret = 1;
while (p > 0) {
if (p & 1) ret *= cur;
cur *= cur;
p >>= 1LL;
}
return ret;
}
Mint inv() const {
if (val == 0)
std::cerr << "WARNING: inv() is called with 0." << std::endl;
return pow(mod - 2);
}
Mint operator+() const { return *this; }
Mint operator-() const { return Mint(mod - val); }
Mint operator+=(const Mint &a) {
val += a.val;
if (val >= mod) val -= mod;
return Mint(val);
}
Mint operator*=(const Mint &a) {
val *= a.val;
if (val >= mod) val %= mod;
return Mint(val);
}
Mint operator-=(const Mint &a) { return *this += -a; }
Mint operator/=(const Mint &a) { return *this *= a.inv(); }
Mint operator++() { return *this += Mint(1); }
Mint operator--() { return *this -= Mint(1); }
Mint operator++(int) {
Mint ret = *this;
++(*this);
return ret;
}
Mint operator--(int) {
Mint ret = *this;
--(*this);
return ret;
}
operator long long() const { return val; }
};
Mint operator+(const Mint &a, const Mint &b) {
long long ret = a.val + b.val;
if (ret >= Mint::mod) ret -= Mint::mod;
return Mint(ret);
}
Mint operator*(const Mint &a, const Mint &b) {
long long ret = a.val * b.val;
if (ret >= Mint::mod) ret %= Mint::mod;
return Mint(ret);
}
Mint operator-(const Mint &a, const Mint &b) { return a + (-b); }
Mint operator/(const Mint &a, const Mint &b) { return a * b.inv(); }
std::ostream &operator<<(std::ostream &out, const Mint &a) {
return out << a.val;
}
std::istream &operator>>(std::istream &in, Mint &a) {
in >> a.val;
a.verify_value();
return in;
}
Mint pow(Mint a, long long b) { return a.pow(b); }
#line 5 "lib/combination.hpp"
#include <vector>
constexpr int MAX_N = 2000003;
std::vector<Mint> fact(MAX_N), inv(MAX_N);
void init_fact() {
fact[0] = inv[0] = 1;
for (long long i = 1; i < MAX_N; i++) {
fact[i] = fact[i - 1] * Mint(i);
inv[i] = fact[i].inv();
}
}
// aCb
Mint C(int a, int b) {
if (a < 0 || b < 0 || a < b) return 0;
Mint res = fact[a];
res *= inv[b];
res *= inv[a - b];
return res;
}
// aPb
Mint P(int a, int b) {
if (a < 0 || a < b) return 0;
return fact[a] * inv[a - b];
}
// aHb
Mint H(int a, int b) {
if (b == 0) return 1;
return C(a + b - 1, b);
}
int main() {
int n, m, k;
std::cin >> n >> m >> k;
if (n > m + k) {
std::cout << 0 << std::endl;
return 0;
}
init_fact();
std::cout << C(n + m, n) - C(m + n, m + k + 1) << std::endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
const int mod=1e9+7;
int ans[N],parent[N];
vector<pair<int,int>>adj[N];
pair<pair<int,int>,int>edges[N];
int FindParent(int x)
{
if(parent[x] == x)return x;
return parent[x]=FindParent(parent[x]);
}
void DFS(int cur,int par)
{
int x,y;
for(auto it : adj[cur])
{
x=it.first;
y=it.second;
if(!(x ^ par))continue;
if(ans[cur] == y)
{
if(y == 1)ans[x]=2;
else ans[x]=1;
}
else ans[x]=y;
DFS(x,cur);
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
int i,m,n,x,y,z,px,py;
cin >> n >> m;
for(i=1;i<=m;i++)
{
cin >> x >> y >> z;
edges[i].first.first=x;
edges[i].first.second=y;
edges[i].second=z;
}
for(i=1;i<=n;i++)parent[i]=i;
for(i=1;i<=m;i++)
{
x=edges[i].first.first;
y=edges[i].first.second;
z=edges[i].second;
px=FindParent(x);
py=FindParent(y);
if(px != py)
{
parent[py]=px;
adj[x].emplace_back(y,z);
adj[y].emplace_back(x,z);
}
}
ans[1]=1;
DFS(1,1);
for(i=1;i<=n;i++)cout << ans[i] << '\n';
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define sf(x) scanf("%d",&x)
#define sfl(x) scanf("%lld",&x)
#define pf(x) printf("%d\n",x)
#define pfl(x) printf("%lld\n",x)
#define endl '\n'
#define pii pair<int,int>
#define mapii map<int,int>
#define mapll map<ll,ll>
#define mapci map<char,int>
#define mapcl map<char,ll>
#define mapsi map<string,int>
#define mapsl map<string,ll>
#define pll pair<ll,ll>
#define vi vector<int>
#define vl vector<ll>
#define vd vector<double,double>
#define all(c) c.begin(),c.end()
#define F first
#define S second
mapii row,col;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int r,c,i,j,k,l,m,n,h,w;
cin >> h >> w >> n >> m;
char s[h+2][w+2];
for(i=1; i<=h; i++)
{
for(j=1; j<=w; j++)
{
s[i][j] = '*';
}
}
int a,b;
vector<pair<int,int>>pt;
for(i=0; i<n; i++)
{
cin >> a >> b;
pt.pb({a,b});
}
for(i=0; i<m; i++)
{
cin >> a >> b;
s[a][b] = '#';
}
int no = 0;
for(auto x : pt)
{
a = x.F;
b = x.S;
i = a,j = b;
while(i)
{
if(s[i][j] == '#')break;
s[i][j] = '1';
i--;
}
i = a,j = b;
while(i<=h)
{
if(s[i][j] == '#')break;
s[i][j] = '1';
i++;
}
i = a,j = b;
while(j<=w)
{
if(s[i][j] == '#')break;
s[i][j] = '1';
j++;
}
i = a,j = b;
while(j)
{
if(s[i][j] == '#')break;
s[i][j] = '1';
j--;
}
}
for(i=1; i<=h; i++)
{
for(j=1; j<=w; j++)
{
if(s[i][j]!='1')no++;
}
}
cout << (h*w)-no << endl;
}
|
#include<bits/stdc++.h>
using namespace std;
template<typename T>inline T read(){
T x=0,f=0;char c=getchar();
while(!isdigit(c)) f=c=='-',c=getchar();
while(isdigit(c)) x=x*10+c-48,c=getchar();
return f?-x:x;
}
#define int long long
namespace run{
const int N=1e5+9;
int n,a[N],b[N],ans;
priority_queue<int> pq[2];
int main(){
n=read<int>();
for(int i=1;i<=n;i++) ans+=(a[i]=read<int>());
for(int i=1;i<=n;i++) b[i]=read<int>();
for(int i=1;i<=n;i++) pq[i&1].push(b[i]-a[i]);
while(!pq[0].empty() && !pq[1].empty() && pq[0].top()+pq[1].top()>0){
ans+=pq[0].top()+pq[1].top();
pq[0].pop(),pq[1].pop();
}
printf("%lld\n",ans);
return 0;
}
}
#undef int
int main(){
#ifdef my
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
return run::main();
} | #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#define rep(i,l,r) for(int i=(l);i<=(r);++i)
#define per(i,r,l) for(int i=(r);i>=(l);--i)
using namespace std;
typedef long long ll;
const int N=1e5+10;
int n,a[N],b[N],c[N],x;ll ans;
int main(){
scanf("%d",&n);
rep(i,1,n) scanf("%d",a+i),ans+=a[i];
rep(i,1,n) scanf("%d",&x),i&1?b[i+1>>1]=x-a[i]:c[i>>1]=x-a[i];
sort(b+1,b+(n>>1)+1);sort(c+1,c+(n>>1)+1);
rep(i,1,n>>1) if(b[i]+c[i]>0) ans+=b[i]+c[i];
printf("%lld\n",ans);
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)
int main(){
int N;
cin >> N;
if (N%2 == 0){
cout << "White" << endl;
}
else cout << "Black" << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int64_t n;
cin>>n;
if(n%2==0)
cout<<"White"<<endl;
else
cout<<"Black"<<endl;
return 0;
}
|
/* HAVE PATIENCE YOU CAN SOLVE EVERY QUESTION
JUST THINK*/
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize ("unroll-loops")
#include<bits/stdc++.h>
//#include <unordered_set>
//#include <unordered_map>
using namespace std;
int mod = 1000000007;
const double pi = 3.141592653689793238460;
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 5;
const int pr = 31;
#define ll long long
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define MEM(x) memset(x,inf,sizeof(x))
#define fi first
#define se second
#define forn(i, n) for (int i = 0; i< n; i++)
#define forl(i,l,u) for(int i=(int)l;i<=(int)u;++i)
#define all(v) (v).begin(), (v).end()
#define sum(a) ( accumulate ((a).begin(), (a).end(), 0ll))
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int f(int x, int y, string a[]) {
int l;
if (a[x][y] == '+')l = 1;
else l = -1;
return l;
}
int n;
long long dp[501][501];
vector<int > one(n);
vector<int> zero(n);
int main()
{
//#define int ll
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
ll n;
cin >> n;
ll ans = 1e18;
forl(i, 0, 61) {
ll b = (ll)pow(2, (ll)i);
ll c = n % b;
ll a = n / b;
ans = min(ans, (a + (ll)i + c));
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define pii pair < int , int >
#define F first
#define S second
#define endl '\n'
#define int long long
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define kill(x) return cout<<x<<'\n', 0;
using namespace std;
const int N=2e3+100;
ll mod=1e9+7;
ll dp[N];
char cng(char c){
if (c=='A') return 'B';
return 'A';
}
int32_t main(){
char aa,ab,ba,bb;
ll n;
cin >> n;
cin >> aa >> ab >> ba >> bb;
dp[2]=1;
for (int i=3;i<=n;i++){
if (ab=='B'){
if (bb=='A'){
if (ba=='A'){
for (int j=2;j<i;j++){
dp[i]+=dp[j];
dp[i]%=mod;
}
}
else{
dp[i]=dp[i-1]+dp[i-2];
dp[i]%=mod;
}
}
else{
// cout << 1/0;
dp[i]=dp[i-1];
}
}
else{
if (aa=='B'){
if (ba=='B'){
for (int j=2;j<i;j++){
dp[i]+=dp[j];
dp[i]%=mod;
}
}
else{
dp[i]=dp[i-1]+dp[i-2];
dp[i]%=mod;
}
}
else{
// cout << 1/0;
dp[i]=dp[i-1];
}
}
}
cout << dp[n] << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<double, double>;
using pld = pair<ld, ld>;
ll gcd(ll a, ll b)
{
while (b)
{
ll aux = b;
b = a % b;
a = aux;
}
return a;
}
int main()
{
auto mod_inverse = [](const ll p, const ll m)
{
auto ext_euclid = [](const ll a, const ll b)
{
ll old_r = a, r = b;
ll old_s = 1, s = 0;
ll old_t = 0, t = 1;
ll q, new_r, new_s, new_t;
while (r != 0)
{
q = old_r / r;
new_s = old_s - s * q;
new_t = old_t - t * q;
new_r = old_r % r;
old_r = r, r = new_r;
old_s = s, s = new_s;
old_t = t, t = new_t;
}
return old_s;
};
ll modular_inverse = ext_euclid(p, m);
if (modular_inverse < 0)
{
modular_inverse += m;
}
return modular_inverse;
};
ios::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
while (T--)
{
ll N, S, K;
cin >> N >> S >> K;
ll their_gcd = gcd(K, N);
if ((N - S) % their_gcd)
{
cout << "-1\n";
}
else
{
const ll K1 = K / their_gcd, P1 = (N - S) / their_gcd, N1 = N / their_gcd;
cout << (P1 * mod_inverse(K1, N1)) % N1 << "\n";
}
}
}
| // header {{{
// header {{{
using namespace std;
#include <bits/stdc++.h>
#define CPP_STR(x) CPP_STR_I(x)
#define CPP_CAT(x, y) CPP_CAT_I(x, y)
#define CPP_STR_I(args...) #args
#define CPP_CAT_I(x, y) x##y
#define ASSERT(expr...) assert((expr))
using i8 = int8_t;
using u8 = uint8_t;
using i16 = int16_t;
using u16 = uint16_t;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;
using f32 = float;
using f64 = double;
// }}}
constexpr i64 INF = 1'010'000'000'000'000'017LL;
constexpr i64 MOD = 1'000'000'007LL;
constexpr f64 EPS = 1e-12;
constexpr f64 PI = 3.14159265358979323846;
#define M5 100007
#define M9 1000000000
// util {{{
#define FOR(i, start, end) for (i64 i = (start), CPP_CAT(i, xxxx_end) = (end); i < CPP_CAT(i, xxxx_end); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(f, c, ...) (([&](decltype((c)) cccc) { return (f)(std::begin(cccc), std::end(cccc), ##__VA_ARGS__); })(c))
#define ll long long int
#define VI vector<int>
template <typename C>
i64 SIZE(const C &c)
{
return static_cast<i64>(c.size());
}
template <typename T, size_t N>
i64 SIZE(const T (&)[N]) { return static_cast<i64>(N); }
template <typename T, typename U, typename Comp = less<>>
bool chmax(T &xmax, const U &x, Comp comp = {})
{
if (comp(xmax, x))
{
xmax = x;
return true;
}
return false;
}
template <typename T, typename U, typename Comp = less<>>
bool chmin(T &xmin, const U &x, Comp comp = {})
{
if (comp(x, xmin))
{
xmin = x;
return true;
}
return false;
}
// }}}
// init {{{
struct ProconInit
{
static constexpr int IOS_PREC = 15;
static constexpr bool AUTOFLUSH = false;
ProconInit()
{
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(IOS_PREC);
if (AUTOFLUSH)
cout << unitbuf;
}
} PROCON_INIT;
// }}}
//--------------------------------------------------------------------
bool isOK(int mid, double key)
{
}
int binary_search(double key)
{
int left = -1; //「index = 0」が条件を満たすこともあるので、初期値は -1
int right = 2000000000;
/* どんな二分探索でもここの書き方を変えずにできる! */
while (right - left > 1)
{
int mid = left + (right - left) / 2;
if (isOK(mid, key))
right = mid;
else
left = mid;
}
/* left は条件を満たさない最大の値、right は条件を満たす最小の値になっている */
return right;
}
bool isKaibun(string st)
{
bool ans = true;
int n = st.length();
REP(i, n / 2)
{
if (st[i] != st[n - i - 1])
{
return false;
}
}
return true;
}
int toInt(char a)
{
return a - '0';
}
int gcd(int a, int b)
{
if (a % b == 0)
{
return (b);
}
else
{
return (gcd(b, a % b));
}
}
int lcm(int a, int b)
{
return a * b / gcd(a, b);
}
void solve()
{
int a, b, c;
cin >> a >> b;
if (a != b)
{
cout << (3 - (a + b)) % 3 << endl;
}
else
{
cout << a << endl;
}
}
signed main()
{
solve();
return 0;
}
|
//
// Created by yamunaku on 2020/10/24.
//
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define repl(i, l, r) for(int i = (l); i < (r); i++)
#define per(i, n) for(int i = ((n)-1); i >= 0; i--)
#define perl(i, l, r) for(int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(),(x).end()
#define MOD9 998244353
#define MOD1 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP <<" "<<
#define CYES cout<<"Yes"<<endl
#define CNO cout<<"No"<<endl
#define CFS cin.tie(0);ios::sync_with_stdio(false)
#define CST(x) cout<<fixed<<setprecision(x)
using ll = long long;
using ld = long double;
using vi = vector<int>;
using mti = vector<vector<int>>;
using vl = vector<ll>;
using mtl = vector<vector<ll>>;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
template<typename T>
using heap = priority_queue<T, vector<T>, function<bool(const T, const T)>>;
int main() {
//CFS;
int n, m;
cin >> n >> m;
if (n == 1 && m == 0) {
cout << 1 SP 2 << endl;
return 0;
}
if (m < 0 || m >= n - 1) {
cout << -1 << endl;
return 0;
}
rep(i, n - 1) {
cout << i * 20 + 10 SP i * 20 + 20 << endl;
}
if (m == 0) {
cout << IINF - 1 SP IINF << endl;
} else {
cout << 5 SP m * 20 + 25 << endl;
}
return 0;
}
| #include <bits/stdc++.h>
#define mk make_pair
#define fs first
#define sc second
using namespace std;
typedef long long ll;
typedef long double ld;
int main(){
int n, m;
while(cin>>n>>m){
if(m<0 || (m>=n-1 && m!=0)){
printf("-1\n");
}
else{
if(m>0){
int tmp = 2;
for(int i=0; i<m+1; ++i){
printf("%d %d\n",tmp, tmp+1);
tmp+=2;
}
printf("1 %d\n",tmp);
++tmp;
for(int i=m; i<n-2; ++i){
printf("%d %d\n",tmp, tmp+1);
tmp+=2;
}
}
else{
int tmp = 1;
for(int i=0; i<n; ++i){
printf("%d %d\n",tmp, tmp+1);
tmp+=2;
}
}
}
}
return 0;
}
|
// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define dd double
#define ld long double
#define sl(n) scanf("%lld", &n)
#define si(n) scanf("%d", &n)
#define sd(n) scanf("%lf", &n)
#define pll pair <ll, ll>
#define pii pair <int, int>
#define mp make_pair
#define pb push_back
#define all(v) v.begin(), v.end()
#define inf (1LL << 61)
#define loop(i, start, stop, inc) for(ll i = start; i <= stop; i += inc)
#define for1(i, stop) for(ll i = 1; i <= stop; ++i)
#define for0(i, stop) for(ll i = 0; i < stop; ++i)
#define rep1(i, start) for(ll i = start; i >= 1; --i)
#define rep0(i, start) for(ll i = (start-1); i >= 0; --i)
#define ms(n, i) memset(n, i, sizeof(n))
#define casep(n) printf("Case %lld:", ++n)
#define pn printf("\n")
#define pf printf
#define EL '\n'
#define fastio std::ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
// === Debug macro starts here ===
#ifndef ONLINE_JUDGE
#define DEBUG
#define SYS_COL system("COLOR")
#endif
int recur_depth = 0;
#ifdef DEBUG
#define dbg(x) {++recur_depth; auto x_=x; --recur_depth; SYS_COL; \
cerr<<string(recur_depth, '\t')<<"\e[91m"<<__func__<<":" \
<<__LINE__<<"\t"<<#x<<" = "<<x_<<"\e[39m"<<endl;}
template<typename Ostream, typename Cont>
typename enable_if<is_same<Ostream,ostream>::value,
Ostream&>::type operator<<(Ostream& os, const Cont& v) {
os<<"[";
for(auto& x:v){os<<x<<", ";}
return os<<"]";
}
template<typename Ostream, typename ...Ts>
Ostream& operator<<(Ostream& os, const pair<Ts...>& p){
return os<<"{"<<p.first<<", "<<p.second<<"}";
}
#else
#define dbg(x)
#endif
// === Debug macro ends here ===
#define ff first
#define ss second
const ll sz = 22;
vector <int> g[sz], comp[sz];
int col[sz], vis[sz];
void comp_dfs(ll u, ll id)
{
vis[u] = 1;
comp[id].pb(u);
for(int &v : g[u]) {
if(!vis[v])
comp_dfs(v, id);
}
}
ll dfs(ll pos, ll id)
{
if(pos == comp[id].size())
return 1;
ll u = comp[id][pos];
set <int> exist;
for(int &v : g[u])
if(col[v])
exist.insert(col[v]);
ll ret = 0;
for1(i, 3) {
if(exist.find(i) != exist.end())
continue;
col[u] = i;
ret += dfs(pos+1, id);
col[u] = 0;
}
return ret;
}
int main()
{
int n, m;
cin >> n >> m;
for1(i, m) {
int u, v;
si(u), si(v);
g[u].pb(v);
g[v].pb(u);
}
ll ans = 1, id = 0;
for1(i, n) {
if(vis[i])
continue;
comp_dfs(i, ++id);
}
for1(i, id)
ans *= dfs(0, i);
cout << ans << EL;
return 0;
}
| #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma,avx512f,avx512bw,avx512bitalg")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
//雪花飄飄北風嘯嘯
//天地一片蒼茫
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define rep(x,start,end) for(int x=start;x!=end;x++)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//change less to less_equal for non distinct pbds, but erase will bug
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
int B,m;
bool bad[25][25];
int ind[1050000];
int ahat[21][1050000];
int bhat[21][1050000];
ll hat[21][1050000];
void conv(){
for (int x=0;x<1048576;x++) {
ahat[__builtin_popcount(x)][x]=ind[x];
bhat[__builtin_popcount(x)][x]=ind[x];
}
rep(cnt,0,21){
rep(bit,0,20){
for (int a=0;a<1048576;a+=2<<bit){
int b=a+(1<<bit);
for (int x=0;x<(1<<bit);x++){
ahat[cnt][b+x]+=ahat[cnt][a+x];
bhat[cnt][b+x]+=bhat[cnt][a+x];
}
}
}
}
rep(cnt,0,21){
rep(cnt2,0,cnt+1){
for (int x=0;x<1048576;x++){
hat[cnt][x]+=(ll)ahat[cnt2][x]*bhat[cnt-cnt2][x];
}
}
}
rep(cnt,0,21){
rep(bit,0,20){
for (int a=0;a<1048576;a+=2<<bit){
int b=a+(1<<bit);
for (int x=0;x<(1<<bit);x++){
hat[cnt][b+x]-=hat[cnt][a+x];
}
}
}
}
ll ans=0;
for (int x=0;x<(1<<B);x++){
ans+=ind[x^((1<<B)-1)]*hat[__builtin_popcount(x)][x];
}
cout<<ans<<endl;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin.exceptions(ios::badbit | ios::failbit);
cin>>B>>m;
int a,b;
rep(x,0,m){
cin>>a>>b;
a--,b--;
bad[a][b]=bad[b][a]=true;
}
ind[0]=1;
rep(x,1,1<<B){
int temp=__builtin_ctz(x);
int nx=x^(1<<temp);
ind[x]=ind[nx];
rep(bit,0,B) if (nx&(1<<bit) && bad[temp][bit]){
ind[x]=0;
}
}
conv();
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#define for0(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define forc(i, l, r) for (int i = (int)(l); i <= (int)(r); ++i)
#define forr0(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define forr1(i, n) for (int i = (int)(n); i >= 1; --i)
#define forrc(i,r,l) for (int i= (int)(r); i>= (int)(l); --i)
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin, (x).rend()
#define tr(c,i) for(_typeof_((c)).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define sz(a) int((a).size())
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
using namespace std;
#define count1(x) __builtin_popcount(x)
#define count1ll(x) __builtin_popcountll(x)
#define leadzero(x) __builtin_clz(x)
#define trailzero(x) __builtin_ctz(x)
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef double ld;
typedef long long ll;
typedef unsigned long long int ull;
/*
//unhackable unordered_map and gp_hash_table:-
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
*/
ll max(ll a,ll b){
if(a>b) return a;
else return b;
}
const int MOD=1000000007;
int no_of_bits(long long n)
{
int count=0;
while(n>0)
{
n=n>>1;
count++;
}
return count;
}
long long gcd(long long a, long long b) {
while (b > 0) {
long long t = a % b;
a = b;
b = t;
}
return a;
}
void add_self(int &a, int b){
a+=b;
if(a>=MOD){
a-=MOD;
}
}
void sub_self(int &a, int b){
a-=b;
if(a<0){
a+=MOD;
}
}
int search(vi x,int n,int i){
int l=0,h=n-1,mid;
while(l<=h){
mid=(l+h)/2;
if(x[mid]<i){
l=mid+1;
}else{
h=mid-1;
}
}
return l;
}
const int INF=-2000;
const ll INF_=1e13;
ll minll(ll a,ll b){
if(a>b) return b;
else return a;
}
ll maxll(ll a,ll b){
if(a<b) return b;
else return a;
}
void solve(){
ll ans=1;
for(int i=1;i<=30;i++)
{
ans=i*ans/gcd(i,ans);
}
cout<<ans+1<<'\n';
}
int main(){
// auto start = chrono::high_resolution_clock::now();
// unsync the I/O of C and C++.
// ios_base::sync_with_stdio(false);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
// cin>>t;
t=1;
while(t--){
solve();
}
// auto end = chrono::high_resolution_clock::now();
// Calculating total time taken by the program.
// double time_taken =
// chrono::duration_cast<chrono::nanoseconds>(end - start).count();
// time_taken *= 1e-9;
// cout << "Time taken by program is : " << fixed
// << time_taken << setprecision(9);
} | #include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define sz(x) ((int)(x).size())
#define endl "\n"
#define fastio ios_base::sync_with_stdio(false); cin.tie(nullptr);
#define all(x) x.begin(),x.end()
#define y1 omg
#define INF 1000000010
#define FOR(x, n) for(int x=0; x<n; x++)
#define ini(x, n) memset(x, n, sizeof(x));
#define MOD ((int)1e9 + 7)
#define atoi(x) ((x)-'0')
#define int ll
//#define double long double
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
int dx[] = { 1, -1, 0, 0, 1, 1, -1, -1 };
int dy[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
ll n, ans = 1;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
signed main() {
cin >> n;
if (n == 1) {
cout << 1;
}
else {
for (int i = 1; i <= n; i++) ans = lcm(ans, i);
cout << ans + 1;
}
}
|
#include <bits/stdc++.h>
using namespace std;
void printmat(const vector<vector<int>>& mat) {
for (auto row : mat) {
for (int elem : row)
cout << elem << " ";
cout << endl;
}
}
void printv(const vector<long long>& v) {
for (int elem : v)
cout << elem << " ";
cout << endl;
}
void printvp(const vector<pair<int, int>>& vp) {
for (auto row : vp)
cout << row.first << ", " << row.second << " ; ";
cout << endl;
}
int main() {
int T=1, caseIdx=0;
//cin >> T;
while (T--) {
caseIdx++;
string x, ans;
cin >> x;
auto it = x.find('.');
ans = x.substr(0, it);
cout << ans << endl;
//cout << "Case #" << caseIdx << ": " << ans << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
int main() {
string inp;
cin >> inp;
string ans = "";
for(int i = 0; i < inp.length(); i++){
if(inp[i] == '.'){break;}
else{ans = ans + inp[i];}
}
cout << ans;
} |
#include<bits/stdc++.h>
#define ll long long
#define mxn 200002
#define mod 1000000007ll
using namespace std;
ll mem[mxn][17][2], n, k;
char str[mxn];
ll dp1(int pos, int rem, bool f)
{
if(pos==0)return (rem==0);
ll ret= mem[pos][rem][f];
if(ret!=-1)return ret;
ret= 0;
if(f)
{
ret= (k-rem)*dp1(pos-1, rem, f);
if(rem)ret+= (16-(k-rem))*dp1(pos-1, rem-1, f);
}
else ret= 15ll*dp1(pos-1, rem-1, 1);
return mem[pos][rem][f]= ret%mod;
}
ll dp2(int pos, int rem, bool f, int msk)
{
if(rem==-1)return 0;
if(pos==n)return (rem==0);
ll ret= mem[pos][rem][f];
if(ret!=-1)return ret;
ret= 0;
if(f)
{
ret= (k-rem)*dp2(pos+1, rem, f, 0);
if(rem)ret+= (16-(k-rem))*dp2(pos+1, rem-1, f, 0);
}
else
{
int let;
if(str[pos]>='A')let= 10+(str[pos]-'A');
else let= (str[pos]-'0');
if(msk&(1<<(let)))ret+= dp2(pos+1, rem, f, msk);
else ret+= dp2(pos+1, rem-1, f, msk|(1<<(let)));
for(int i=(pos==0); i<let; i++)
{
if(msk&(1<<(i)))ret+= dp2(pos+1, rem, 1, 0);
else ret+= dp2(pos+1, rem-1, 1, 0);
}
}
return mem[pos][rem][f]= ret%mod;
}
int main()
{
scanf("%s %lld", str, &k);
n= strlen(str);
if(k>n)
{
puts("0");
return 0;
}
memset(mem, -1, sizeof mem);
ll res= 0;
for(int i=k; i<n; i++)
res+= dp1(i, k, 0), res%= mod;
memset(mem, -1, sizeof mem);
res+= dp2(0, k, 0, 0);
res%= mod;
printf("%lld\n", res);
return 0;
}
| #define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <stack>
#include <vector>
#include <queue>
#include <map>
#define _USE_MATH_DEFINES
#include <math.h>
using namespace std;
#define REP(i,n) for(int i = 0; i < (int)n; i++)
#define V vector<int>
#define P pair<int,int>
#define INF 1000000007
#define MOD 1000000007
typedef long long LL;
string N;
int K;
LL dp[200010][20];
int char_int(char c) {
int res = 0;
if ('0' <= c && c <= '9') {
res = c - '0';
}
else {
res = c - 'A' + 10;
}
return res;
}
void cal() {
int kcnt = 0;
bool used[16];
REP(i, 16) used[i] = false;
REP(i, N.size()) {
int c = char_int(N[i]);
if (i == 0) {
dp[i][1] = c - 1;
used[c] = true;
kcnt++;
continue;
}
for (int j = 1; j <= K; j++) {
dp[i][j] += dp[i - 1][j] * j;
dp[i][j] += dp[i - 1][j - 1] * (16 - (j - 1));
dp[i][j] %= MOD;
}
dp[i][1] += 15;
for (int k = 0; k < c; k++) {
if (used[k]) dp[i][kcnt]++;
else dp[i][kcnt + 1]++;
}
if (!used[c]) {
used[c] = true;
kcnt++;
}
}
dp[N.size()-1][kcnt]++;
}
int main() {
cin >> N >> K;
cal();
cout << dp[N.size() - 1][K] % MOD << endl;
} |
#include<bits/stdc++.h>
using namespace std;
using ll=int64_t;
template<class T,class U> inline bool chmin(T&x,U y){if(x>y){x=y;return true;}return false;}
template<class T,class U> inline bool chmax(T&x,U y){if(x<y){x=y;return true;}return false;}
void solve(){
string s;
cin>>s;
if(s.length()==1){
cout<<(s=="8"?"Yes":"No")<<'\n';
return;
}
if(s.length()==2){
if(atoi(s.c_str())%8==0){
cout<<"Yes"<<'\n';
return;
}
swap(s[0],s[1]);
if(atoi(s.c_str())%8==0){
cout<<"Yes"<<'\n';
return;
}
cout<<"No"<<'\n';
return;
}
int cnt[10]={};
for(auto&c:s) ++cnt[c-'0'];
for(int i{111};i<1000;++i){
if(i%8) continue;
int a=i%10,b=(i/10)%10,c=i/100;
if(!a or !b or !c) continue;
--cnt[a];--cnt[b];--cnt[c];
bool f=true;
for(int j{1};j<=(9);++j){
if(cnt[j]<0) f=false;
}
++cnt[a];++cnt[b];++cnt[c];
if(f){
cout<<"Yes"<<'\n';
return;
}
}
cout<<"No"<<'\n';
}
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
solve();
} | //#pragma GCC optimize ("Ofast")
//#pragma GCC target("avx2")
//#pragma GCC optimize("unroll-loops")
//#include <x86intrin.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
#define PI 3.141592653589793L
#define FAST ios::sync_with_stdio(false); cin.tie(NULL);
// Use for file I/O;
#define FIN string _fname = "input"; \
string _is = _fname + ".in", _os = _fname + ".out"; \
freopen(_is.c_str(), "r", stdin); \
freopen(_os.c_str(), "w", stdout);
#define FIN2 freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#define RINIT mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define all(x) (x).begin(), (x).end()
#define allr(x) (x).rbegin(), (x).rend()
#define gsize(x) (int)((x).size())
using namespace std;
//using namespace __gnu_pbds;
//typedef gp_hash_table<int, int, hash<int>> ht;
const char nl = '\n';
const char Nl = '\n';
typedef long long ll;
typedef long double ld;
// typedef unsigned int uint;
typedef unsigned long long ull;
const ll inf = 1e9 + 10;
const ll inf2 = 1e18 + 99LL;
const ld inf3 = 1e14;
const ll mod = 1e9+7, mod2 = 998244353;
const ld eps = 1e-2;
const bool local = false;
const int logn = 8, maxn = 1000, maxm = 10001, maxn2 = 3;
int main() {
FAST; //RINIT;
int n;
cin >> n;
string s;
cin >> s;
if (s[0] != s[n - 1]) {
cout << 1 << nl;
return 0;
}
for (int i = 1; i + 2 < n; i++) {
if (s[0] != s[i] && s[i + 1] != s[n - 1]) {
cout << 2 << nl;
return 0;
}
}
cout << -1 << nl;
}
// © Benq
/* 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 <cstdio>
const int maxn = 1e6 + 100;
struct node
{
int to,next,lab;
}E[maxn];
int n,m,head[maxn],tot,col[maxn];
inline void add(int x,int y,int lab) { E[++tot].next = head[x],E[tot].to = y,E[tot].lab = lab,head[x] = tot; }
void dfs(int u)
{
for(int p = head[u],v;p;p=E[p].next)
{
if(col[v = E[p].to]) continue;
col[v] = (E[p].lab == col[u] ? col[u] % n + 1 : E[p].lab),dfs(v);
}
}
int main()
{
scanf("%d %d",&n,&m);
for(int i = 1,a,b,c;i<=m;++i) scanf("%d %d %d",&a,&b,&c),add(a,b,c),add(b,a,c);
col[1] = 1,dfs(1);
for(int i = 1;i<=n;++i) printf("%d\n",col[i]);
return 0;
} | //#pragma GCC optimize ("O2")
//#pragma GCC target ("avx2")
//#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
#include<cstdio>
#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
int N, M;
int to[400005], ne[400005], c[200005], he[100001];
int kotae[100001];
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 main() {
//cin.tie(0);
//ios::sync_with_stdio(false);
N = getint();
M = getint();
int k = 2;
rep1(i, M) {
int u = getint(), v = getint();
c[i] = getint();
to[k] = v;
ne[k] = he[u];
he[u] = k++;
to[k] = u;
ne[k] = he[v];
he[v] = k++;
}
int Q[100001], q = 0;
Q[q++] = 1;
kotae[1] = 1;
while (q) {
int tmp = Q[--q];
int oya = kotae[tmp];
for (int k = he[tmp]; k; k = ne[k]) {
int t = to[k];
if (!kotae[t]) {
int e = c[k >> 1];
kotae[t] = e + (oya == e);
Q[q++] = t;
}
}
}
rep1(i, N) putint(kotae[i] - ((kotae[i] > N) << 1));
fwrite(dn, 1, di - dn, stdout);
Would you please return 0;
} |
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vi vector<int>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define pb push_back
template<typename T>
class SegTree {
int n; // 葉の数
vector<T> data; // データを格納するvector
T def; // 初期値かつ単位元
function<T(T, T)> operation; // 区間クエリで使う処理
function<T(T, T)> update; // 点更新で使う処理
T _query(int a, int b, int k, int l, int r) {
if (r <= a || b <= l) return def; // 交差しない
if (a <= l && r <= b)
return data[k]; // a,l,r,bの順で完全に含まれる
else {
T c1 = _query(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子
T c2 = _query(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子
return operation(c1, c2);
}
}
public:
SegTree(size_t _n, T _def, function<T(T, T)> _operation,
function<T(T, T)> _update)
: def(_def), operation(_operation), update(_update) {
n = 1;
while (n < _n) {
n *= 2;
}
data = vector<T>(2 * n - 1, def);
}
void change(int i, T x) {
i += n - 1;
data[i] = update(data[i], x);
while (i > 0) {
i = (i - 1) / 2;
data[i] = operation(data[i * 2 + 1], data[i * 2 + 2]);
}
}
T query(int a, int b) {
return _query(a, b, 0, 0, n);
}
T operator[](int i) {
return data[i + n - 1];
}
};
bool solve()
{
ll n,q,a,t,x,y;
cin >> n >> q;
SegTree<ll> st(n, 0,
[](ll p, ll k){return p^k;},
[](ll p, ll k){return p^k;});
rep(i,n){
cin >> a;
st.change(i, a);
}
rep(i,q){
cin >> t >> x >> y;
x--;
if(t==1){
st.change(x, y);
}
else{
cout << st.query(x, y) << endl;
}
}
return true;
}
int main()
{
solve();
return 0;
} | #pragma GCC target ("avx2")
//#pragma GCC optimize "trapv"
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
#define input(a,n) for(ll i1=0;i1<n;i1++)cin>>a[i1]
#define ll long long
#define pi 2 * acos(0.0)
#define usll unordered_set<ll>
#define sll set<ll>
#define vll vector<ll>
#define mll map<ll,ll>
#define pll pair<ll,ll>
#define umll unordered_map<ll,ll>
#define S second
#define sz size()
#define all(v) v.begin(),v.end()
#define Y cout<< "YES"<< "\n"
#define N cout<< "NO"<< "\n"
#define F first
#define mp make_pair
#define pb push_back
#define pf push_front
#define ld long double
#define mod 1000000007
using namespace std;
ll st[1200001];
umll m;
void build(ll a[],ll l,ll r,ll i)
{
if(l==r)
{
st[i]=a[l];
return;
}
ll mid=(r-l)/2+l;
build(a,l,mid,2*i+1);
build(a,mid+1,r,2*i+2);
st[i]=(st[2*i+1]^st[2*i+2]);
}
void update(ll l,ll r,ll i,ll ind,ll v)
{
if(ind<l||ind>r)
return;
else if(l==r&&ind==l)
{
st[i]=(st[i]^v);
return;
}
ll mid=(r-l)/2+l;
update(l,mid,2*i+1,ind,v);
update(mid+1,r,2*i+2,ind,v);
st[i]=(st[2*i+1]^st[2*i+2]);
}
ll que(ll l,ll r,ll low,ll high,ll i)
{
if(high<l||low>r)
return 0;
else if(l>=low&&r<=high)
return st[i];
ll mid=(r-l)/2+l;
return (que(l,mid,low,high,2*i+1)^que(mid+1,r,low,high,2*i+2));
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
for(ll i=0;i<=1200000;i++)
st[i]=0;
ll n,q,c,l,r,p;
cin>>n>>q;
ll a[n];
for(ll i=0;i<n;i++)
{
cin>>a[i];
}
build(a,0,n-1,0);
//for(ll i=0;i<=10;i++)
// cout<<st[i]<< " ";
//cout<< "\n";
while(q--)
{
cin>>p>>l>>r;
if(p==1)
{
l--;
update(0,n-1,0,l,r);
}
else
{
l--;
r--;
ll ans=que(0,n-1,l,r,0);
cout<<ans<< "\n";
}
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
using ll = long long;
#define print(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
//ax + by = gcd(a, b) ���� x, y ��Ԃ�
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;
}
// ax == 1 (mod m)���� x
ll mod_inverse(ll a, ll m){
ll x, y;
extgcd(a, m, x, y);
return (m + x % m) % m;
}
int main()
{
ll A, B, C;
ll M = 998244353;
cin >> A >> B >> C;
ll ans = mod_inverse(8, M);
(ans *= A) %= M;
(ans *= A + 1) %= M;
(ans *= B) %= M;
(ans *= B + 1) %= M;
(ans *= C) %= M;
(ans *= C + 1) %= M;
cout << ans;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pint;
typedef pair<ll,ll> pll;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<string> vstr;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
#define vint2(v,n,m,init) vector<vector<int>> v(n, vector<int>(m, init))
#define vll2(v,n,m,init) vector<vector<ll>> v(n, vector<ll>(m, init))
#define rep(i,n) for(ll i=(ll)0; i<(ll)n; i++)
#define REP(i,m,n) for(ll i=(ll)m; i<(ll)n; i++)
#define arr(var, n) vint var(n); rep(i,n){cin >> var[i];}
#define arrll(var, n) vll var(n); rep(i,n){cin >> var[i];}
#define arrst(var, n) vstr var(n); rep(i,n){cin >> var[i];}
#define ALL(var) (var).begin(), (var).end()
#define sortall(var) sort(ALL(var))
#define uniqueall(v) v.erase(unique(v.begin(), v.end()), v.end());
#define prt(var) cout << (var) << "\n"
#define prt2(v1, v2) cout << (v1) << " " << (v2) << "\n"
#define prt3(v1, v2, v3) cout << (v1) << " " << (v2) << " " << (v3) << "\n"
#define prtd(n, var) cout << fixed << setprecision(n) << (var) << "\n"
#define prtfill(n, var) cout << setw(n) << setfill('0') << (var);
#define prtall(v) rep(i,v.size()){cout<<v[i]<<(i!=v.size()-1?" ":"\n");}
template <typename T>
bool chmax(T &a, const T& b){if(a<b){a=b; return true;} return false;}
template <typename T>
bool chmin(T &a, const T& b){if(a>b){a=b; return true;} return false;}
ll gcd(ll a, ll b){ return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b){ return a/gcd(a,b)*b;}
//----------------------------------------------------------------------------
#define prt4(v1,v2,v3,v4) cout<<(v1)<<" "<<(v2)<<" "<<(v3)<<" "<<(v4)<<"\n";
ll power(ll a, ll p, ll mod){
ll res = 1;
ll x = a;
while(p){
if(p&1) res *= x, res %= mod;
x *= x, x %= mod;
p >>= 1;
}
return res;
}
int main(void) {
ll n, m;
cin >> n >> m;
prt((power(10,n,m*m)/m)%m);
}
|
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define FORR(i, m, n) for (int i = (m); i >= (n); i--)
#define REP(i, n) FOR(i, 0, (n))
#define REPR(i, n) FORR(i, (n) - 1, 0)
#define REP1(i, n) FOR(i, 1, (n) + 1)
#define FORL(i,m,n) for(ll i=(m); i < (n); i += 1)
#define REPL(i,n) FORL(i,0,(n))
const ll INF = 1LL << 60;
struct UnionFind{
vector<int> par,siz;
UnionFind(int n) : par(n,-1),siz(n,1) {}
int root(int x){
if(par[x] == -1) return x;
else return par[x] = root(par[x]);
}
bool issame(int x, int y){
return root(x) == root(y);
}
bool unite(int x, int y){
x = root(x); y = root(y);
if(x == y) return false;
if(siz[x] < siz[y]) swap(x,y);
par[y] = x;
siz[x] += siz[y];
return true;
}
};
using Edge = pair<int, pair<int,int>>;
ll combination(ll n,ll k){
if(n == 0) return 0;
if(k == 0) return 1;
ll x=1,y=1;
REPL(i,k){
x *= n;
n--;
}
REPL(i,k-1){
y *= k-i;
}
return x/y;
}
int combination(int n,int k){
if(n == 0) return 0;
if(k == 0) return 1;
int x=1,y=1;
REP(i,k){
x *= n;
n--;
}
REP(i,k-1){
y *= k-i;
}
return x/y;
}
ll min(ll a, ll b){
if(a > b){
return b;
}else if(a < b){
return a;
}else{
return a;
}
}
ll max(ll a, ll b){
if(a > b){
return a;
}else if(a < b){
return b;
}else{
return a;
}
}
int fac(int n){
if(n == 1) return 1;
else return n * fac(n-1);
}
int main(void){
ll n;
cin >> n;
if(n == 1){
cout << 1 << endl;
return 0;
}
ll a=INF,b=INF,c=INF;
for(ll i=1,j=0; i<n; i*=2,j+=1){
ll tmp_a = n / i;
ll tmp_b = j;
ll tmp_c = n % i;
if(a+b+c > tmp_a+tmp_b+tmp_c){
a = tmp_a;
b = tmp_b;
c = tmp_c;
}
}
cout << a+b+c << endl;
} | #include<bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#endif
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef std::vector<vector<int> > vvi;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pi> vpi;
typedef vector<pll> vpll;
typedef vector<vll> vvll;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vector<double>> vvd;
typedef vector<ld> vld;
typedef vector<vld> vvld;
typedef vector<bool> vb;
typedef vector<vb> vvb;
#define rep(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define repi(i, a, b) for (int i = (int)a; i >= (int)b; i--)
#define pb push_back
#define fi first
#define se second
#define all(a) a.begin(), a.end()
#define sz(a) (int)(a.size())
#define umin(a, x) a = min(a, x)
#define umax(a, x) a = max(a, x)
ll nxt() {
ll x; cin >> x; return x;
}
void setIO(string s) {
#ifndef LOCAL
ios_base::sync_with_stdio(0); cin.tie(0);
freopen((s+".in").c_str(),"r",stdin);
freopen((s+".out").c_str(),"w",stdout);
#endif
}
set<pll> generate_box(int a, int b) {
set<pll> ans;
rep(i, -3, 4) {
rep(j, -3, 4) {
if (abs(i) + abs(j) <= 3) {
int c = a - i, d = b - j;
ans.insert({c, d});
}
}
}
return ans;
}
int ans = 0;
int reach_1(int a, int b, int c, int d) {
//this is for 1
auto x = generate_box(a, b);
for (auto it = x.begin(); it != x.end(); it++) {
if ((*it).fi == c && (*it).se == d) {
ans = 1;
return 1;
}
}
if (a + b == c + d || a - b == c - d) {
ans = 1; return 1;
}
return 0;
}
void solve() {
int a, b, c, d; cin >> a >> b >> c >> d;
//0
if (a == c && b == d) {
ans = 0;
return;
}
//1
if (reach_1(a, b, c, d)) return;
//this for 2
if (((a + b) % 2) % 2 == ((c + d) % 2) % 2) {
ans = 2;
return;
}
auto x = generate_box(a, b);
auto y = generate_box(c, d);
for (auto it = x.begin(); it != x.end(); it++) {
int A = (*it).fi, B = (*it).se;
if (reach_1(A, B, c, d)) {
ans = 2;
return;
}
}
for (auto it = y.begin(); it != y.end(); it++) {
int A = (*it).fi, B = (*it).se;
if (reach_1(a, b, A, B)) {
ans = 2;
return;
}
}
//3
ans = 3;
return;
}
int32_t main(){
// setIO("trapped");
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << fixed << setprecision(10);
int T = 1;
// cin >> T;
for (int tc = 1; tc <= T; tc++) {
// cout << "Case #" << tc << ": ";
solve();
}
cout << ans << 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>;
const ll INF = 1e18;
const int MX = 2005;
int main() {
string s1,s2,s3;
cin >> s1 >> s2 >> s3;
int l1 =s1.size();
int l2 =s2.size();
int l3 =s3.size();
ll n1,n2,n3=-1;
map<char,int>m;
for(int i = 0; i < s1.size();i++){
m[s1[i]] += 1;
m[s1[0]] += 100;
}
for(int i = 0; i < s2.size();i++){
m[s2[i]] += 1;
m[s2[0]] += 100;
}
for(int i = 0; i < s3.size();i++){
m[s3[i]] += 1;
m[s3[0]] += 100;
}
if(m.size() > 10){
cout << "UNSOLVABLE" << endl;
return 0;
}
int array[]={0,1,2,3,4,5,6,7,8,9};
do{
ll a=0,b=0,c=0;
map<int,int>ma;
int cnt = 0;
bool skip = false;
for(auto &x:m){
if(x.second >= 100 && array[cnt]==0){
skip = true;
break;
}else{
ma[x.first] = array[cnt];
}
cnt++;
}
if(!skip){
for(int i = 0;i < l1;i++){
a += pow(10,i) * ma[s1[l1-i-1]];
}
for(int i = 0;i < l2;i++){
b += pow(10,i) * ma[s2[l2-i-1]];
}
for(int i = 0;i < l3;i++){
c += pow(10,i) * ma[s3[l3-i-1]];
}
if(a + b == c){
n1 = a;
n2 = b;
n3 = c;
break;
}
}
}while(next_permutation(array,array+10));
if(n3 == -1){
cout << "UNSOLVABLE" << endl;
}else{
cout << n1 << endl;
cout << n2 << endl;
cout << n3 << endl;
}
} | #include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
using ll = long long;
int n;
int st[10], val[256];
string a, b, c;
bool dfs(int i, int car);
bool search(char c, int i, int car) {
for(int j = 0; j < 10; ++j) if(!st[j]) {
st[j] = c;
val[c] = j;
if(dfs(i, car)) return true;
st[j] = 0;
val[c] = -1;
}
return false;
}
bool ok(string& x) {
return val[x.back()];
}
bool dfs(int i, int car) {
if(i >= c.size()) return !car && ok(a) && ok(b) && ok(c);
if(i < a.size() && val[a[i]] == -1) return search(a[i], i, car);
if(i < b.size() && val[b[i]] == -1) return search(b[i], i, car);
int va = i >= a.size() ? 0 : val[a[i]];
int vb = i >= b.size() ? 0 : val[b[i]];
int res = va + vb + car;
// printf("%d %d %d\n", va, vb, res);
if(val[c[i]] == -1) {
if(st[res%10]) return false;
val[c[i]] = res % 10;
st[res % 10] = c[i];
if(dfs(i + 1, res/10)) return true;
val[c[i]] = -1;
st[res%10] = 0;
}
else {
if(val[c[i]] != res % 10) return false;
if(dfs(i + 1, res / 10)) return true;
}
return false;
}
void print(string& x){
for(auto& c: x) printf("%d", val[c]);
puts("");
}
int main(){
cin >> a >> b >> c;
memset(val, -1, sizeof val);
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
reverse(c.begin(), c.end());
if(dfs(0, 0)) {
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
reverse(c.begin(), c.end());
print(a);
print(b);
print(c);
}
else puts("UNSOLVABLE");
}
|
#include <bits/stdc++.h>
#define N 100
using namespace std;
using ll = long long;
ll nck[N][N];
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
for (int i=0; i<N; i++) {
nck[i][0] = 1;
for (int j=1; j<=i; j++) {
nck[i][j] = nck[i-1][j-1] + nck[i-1][j];
}
}
ll a, b, k; cin>>a>>b>>k; --k;
string s;
while (a && b) {
ll x = nck[a+b-1][a-1];
// cout<<x<<' '<<k<<endl;
if (x <= k) k -= x, s += 'b', --b;
else s += 'a', --a;
}
while (a--) s += 'a';
while (b--) s += 'b';
cout<<s<<endl;
}
| #include <bits/stdc++.h>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ld long double
#define ll long long
using namespace std;
#define MOD 1000000007
ll gcd(ll a, ll b) {
if (b == 0) return a;
if (a == 0) return b;
return gcd(b, a % b);
}
ll calc(ll n, ll a, ll b) {
bool doneA[a + 1], doneB[b + 1];
memset(doneA, false, sizeof(doneA));
memset(doneB, false, sizeof(doneB));
ll num = 1;
for (ll i = 1; i <= n; i++) {
num *= i;
for (ll j = 1; j <= a; j++) {
if (!doneA[j] && num % j == 0) {
num /= j;
doneA[j] = true;
}
}
for (ll j = 1; j <= b; j++) {
if (!doneB[j] && num % j == 0) {
num /= j;
doneB[j] = true;
}
}
}
return num;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int a, b;
ll k;
cin >> a >> b >> k;
string s;
for (int i = 0; i < a; i++) s += 'a';
for (int j = 0; j < b; j++) s += 'b';
int pos = a;
int n = a + b;
k--;
for (int i = 0; i < n; i++) {
int cntA = 0, cntB = 0;
for (int j = i + 1; j < n; j++) {
if (s[j] == 'a') cntA++;
else cntB++;
}
ll ans = calc(n - i - 1, cntA, cntB);
if (k >= ans) {
swap(s[i], s[pos]);
k -= ans;
pos++;
}
}
cout << s << "\n";
}
/*
cnt # a and b
k -=
perform the swap
1: aabbb
2: ababb
3: abbab
4: abbba
5: baabb
6: babab
7: babba
8: bbaab
9: bbaba
10: bbbaa
*/
|
#include <bits/stdc++.h>
using namespace std;
int main(){
long long n, k;
cin >> n >> k;
for(int i = 0; i < k; i++){
if(n % 200 == 0){
n = n / 200;
}else{
n = n * 1000 + 200;
}
}
cout << n << endl;
} | #include<bits/stdc++.h>
using namespace std;
int main(){
int N,X,i;
cin >> N >> X;
char c;
for(i = 0;i < N;i++){
cin >> c;
if(c == 'o') X+=1;
else X = max(0,X - 1);
}
cout << X << endl;
}
|
// Created by ...
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define rep(i,a,b) for(int i=a; i<b; i++)
#define repe(i,a,b) for(int i=a; i<=b; i++)
#define ll long long
#define ui unsigned int
#define pb push_back
#define deb(x) cout << #x << '=' << x << endl
#define deb2(x,y) cout << #x << '=' << x << << #y << '=' << y << endl
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define endl "\n"
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define INF (int)1e9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define hcf(x,y) __gcd(x,y)
#define lcm(x,y) (x*y)/(__gcd(x,y))
#define CountOne(x) __builtin_popcount(x)
#define parity(x) __builtin_parity(x) //Funtion return (true) if number of set bits is odd(odd parity) else false..
#define LeadZero(x) __builtin_clz(x) //Counts leading zero in binary representation of x ....
#define TrailZero(x) __builtin_ctz(x)
#define fastIO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<string, string> pss;
typedef map<int, int> mii;
typedef unordered_map<int, int> umap_ii;
typedef unordered_map<int, int> umap_ii;
typedef unordered_map<string, int> umap_si;
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
//alt + ctrl + N
//===============================================================================//
ll power(ll x, ll y) {
ll v = 1; while (y > 0) { if (y & 1)v = v * x; y = y >> 1; x = x * x;} return v;
}
bool isPowerOfTwo(ll x){
return x && (!(x & (x-1))); //check if the number is power of two or not
}
ll ceil(ll a,ll b){ return (a+b-1)/b; }
//===============================================================================//
bool cmp(pair<ll,ll> a, pair<ll,ll> b){
if(2*a.ff+a.ss == 2*b.ff+b.ss) return (a.ff>b.ff);
else return (2*a.ff+a.ss > 2*b.ff+b.ss);
}
int main(){
fastIO;
srand(chrono::high_resolution_clock::now().time_since_epoch().count());
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
ll test = 1;
//cin >> test;
while(test--){
ll n;
cin >> n;
vector<pair<ll,ll>>a(n);
ll voteT = 0,voteA = 0;
rep(i,0,n){
ll x,y;
cin >> x >> y;
a[i].ff = x;
a[i].ss = y;
voteA += x;
}
sort(all(a),cmp);
ll ans = 0;
for(ll i=0;i<n;i++){
if(voteT > voteA){
break;
}
voteT += a[i].ff + a[i].ss;
voteA -= a[i].ff;
ans++;
}
cout << ans << endl;
}
return 0;
} | #include<bits/stdc++.h>
#define ll long long
#define ld long double
#define fastio ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
#define pb push_back
#define pf push_front
#define mp make_pair
#define mod 1000000007
#define vll vector<ll>
#define inf 3e15 +7
#define pll pair<ll,ll>
#define ff first
#define ss second
#define PI acos(-1)
#define prm 998244353
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
/*
ll dx[4]={1,-1,0,0};
ll dy[4]={0,0,-1,1};
cout<<fixed<<setprecision(12)<<;
ll power(ll a, ll b){//a^b
ll res=1;
a=a%prm;
while(b>0){
if(b&1){res=(res*a)%prm;b--;}
a=(a*a)%prm;
b>>=1;
}
return res;
}
ll inv(ll y){return power(y,prm-2);}
ll fact[300060];
ll nCr(ll n, ll r)
{
if (r==0)
return 1;
return (fact[n]* inv(fact[r]) % prm *
inv(fact[n-r]) % prm) % prm;
}
void init(ll n)
{
fact[0]=1;
for(ll i=1;i<=n;i++)fact[i]=(fact[i-1]*i)%prm;
}
*/
/*
//cout<<"Case #"<<fff-T<<": ";
/*
ll spf[15000050];
void sieve()
{
for(ll i=2;i*i<=15000030;i++){
if(spf[i]==0){
for(ll j=i+i;j<=15000030;j+=i){
if(spf[j]==0){
spf[j]=i;
}
}
}
}
}*/
int main()
{
fastio;
ll T=1;
//cin>>T;
//ll fff=T;
while(T--)
{
ll n;
cin>>n;
ll a[n+1];
for(ll i=1;i<=n;i++)cin>>a[i];
if(n==1)
{
cout<<a[1]<<endl;
return 0;
}
if(n==2)
{
cout<<(2*a[1])%mod<<endl;
return 0;
}
ll dp[n+3][2];
dp[0][0]=1;
dp[0][1]=0;
dp[1][0]=1;
dp[1][1]=0;
for(ll i=2;i<=n;i++)
{
dp[i][0]=(dp[i-1][0] + dp[i-1][1])%mod;
dp[i][1]=dp[i-1][0];
}
//cout<<dp[n][0]<<endl;
//cout<<dp[n][1]<<endl;
ll ans=0;
//ans+=;
for(ll i=1;i<=n;i++)
{
ll v=a[i]*((dp[i-1][0]+dp[i-1][1])%mod);
v=v%mod;
v=v*((dp[n-i+1][0] + dp[n-i+1][1])%mod);
v=v%mod;
ans=(ans+v)%mod;
if(i==1)continue;
ll x=a[i]*((dp[i-2][0]+dp[i-2][1])%mod);
x=x%mod;
x=x*((dp[n-i][0] + dp[n-i][1])%mod);
x=x%mod;
ans=(ans-x + mod)%mod;
//ans=(ans+(a[i]*((dp[i-1][0]+dp[i-1][1])%mod)*((dp[n-i+1][0] + dp[n-i+1][1])%mod))%mod)%mod;
//if(i>1)
//ans=(ans+mod-(a[i]*((dp[i-2][0]+dp[i-2][1])%mod)*((dp[n-i][0] + dp[n-i][1])%mod))%mod)%mod;
//cout<<ans<<endl;
}
cout<<ans<<endl;
}
}
/*
*/
|
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const long long INF = (1LL<<62);
using Graph = vector<vector<int>>;
using LLPair = pair<LL, LL>;
using WeightedGraph = vector<vector<LLPair>>; //(対向頂点、コスト)
using PriorityQ = priority_queue<LLPair, vector<LLPair>, greater<LLPair>>;
void dijkstra(WeightedGraph &WG, vector<LL> &D, LL s){
//D初期化
for(int i=0; i<D.size(); i++){
D[i] = INF;
}
D[s] = 0; //s->sはコスト0で確定
//キューには(コスト, 頂点)を入れる。sから当該頂点には当該コストあれば行ける。
//キューにはコストの昇順で入る。
PriorityQ pq;
pq.push(make_pair(0, s));
while(!pq.empty()){
LL pos = pq.top().second; pq.pop();
for(auto nv : WG[pos]){
LL to = nv.first; //頂点posからたどれる頂点
LL cost = nv.second; //そのコスト
if(D[to] > D[pos] + cost){
//sからtoまではD[to]のコストがあれば行ける(最小ではない)
D[to] = D[pos] + cost;
pq.push(make_pair(D[to], to));
}
}
}
return;
}
//G[0]-G[25]はa-zの仮想頂点とする。
int H, W;
int getpos(int h, int w){ return (26 + (h*W) + w); }
const int dh[4] = {1, 0, -1, 0};
const int dw[4] = {0, 1, 0, -1};
int main(int argc, char* argv[]){
cin.tie(0);
ios::sync_with_stdio(false);
cin >> H >> W;
WeightedGraph WG(26+(H*W));
vector<char> V(26 + (H*W));
int sh, sw, gh, gw;
for(int i=0; i<H; i++){
string s; cin >> s;
for(int j=0; j<W; j++){
int pos = getpos(i, j);
char c = s[j];
V[pos] = c;
if(c >= 'a' && c <= 'z'){
int p = c - 'a';
WG[pos].push_back(make_pair(p, 1));
WG[p].push_back(make_pair(pos, 0));
}
if(c == 'S'){ sh = i; sw = j; }
if(c == 'G'){ gh = i; gw = j; }
}
}
//各マスからグラフWGを作成する
for(int h=0; h<H; h++){
for(int w=0; w<W; w++){
int pos = getpos(h, w);
char c = V[pos];
if(c == '#') continue;
for(int i=0; i<4; i++){
int nh = h + dh[i];
int nw = w + dw[i];
int npos = getpos(nh, nw);
if(nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
char nc = V[npos];
if(nc == '#') continue;
if(nc == c && nc >= 'a' && nc <= 'z'){
; //none
}
else{
WG[pos].emplace_back(make_pair(npos, 1));
WG[npos].emplace_back(make_pair(pos, 1));
}
}
}
}
vector<LL> dist(26+(H*W));
dijkstra(WG, dist, getpos(sh, sw));
LL ans = dist[getpos(gh, gw)];
if(ans == INF){ ans = -1; }
printf("%lld\n", ans);
return 0;
} | #include <algorithm>
#include <array>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define int long long
#define ll long long
#define pii std::pair<int, int>
#define pdd std::pair<double, double>
#define INF (1LL << 33)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define SHOW(p) \
if (test) \
cout << #p " : " << p << endl;
bool test = false;
template <class T>
[[maybe_unused]] bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
};
int H, W;
vector<string> a;
vector<vector<int>> dp;
pii s, g;
vector<vector<pii>> tele;
queue<pii> currentq, nextq;
void hoge(int x, int y)
{
currentq.push({x, y});
int nx, ny, time = 0;
while (currentq.size() != 0) {
pii place = currentq.front();
currentq.pop();
nx = place.first, ny = place.second;
if (test)
cout << place.first << ", " << place.second << " will be checked." << endl;
if (chmin(dp.at(place.first).at(place.second), time)) {
if (test)
cout << "time updated." << endl;
char tmp_cc = a.at(nx).at(ny);
if (tmp_cc == '#') {
} else {
if (tmp_cc == '.' || tmp_cc == 'S' || tmp_cc == 'G') {
} else if (tele.at(static_cast<int>(tmp_cc - 'a')).size() != 0) {
for (auto v : tele.at(static_cast<int>(tmp_cc - 'a'))) {
if (dp.at(v.first).at(v.second) > time + 1)
nextq.push(v);
}
tele.at(static_cast<int>(tmp_cc - 'a')).clear();
}
if (nx + 1 != H)
if (dp.at(nx + 1).at(ny) > time + 1)
nextq.push({nx + 1, ny});
if (nx - 1 != -1)
if (dp.at(nx - 1).at(ny) > time + 1)
nextq.push({nx - 1, ny});
if (ny + 1 != W)
if (dp.at(nx).at(ny + 1) > time + 1)
nextq.push({nx, ny + 1});
if (ny - 1 != -1)
if (dp.at(nx).at(ny - 1) > time + 1)
nextq.push({nx, ny - 1});
}
}
if (currentq.size() == 0) {
currentq = nextq;
while (!nextq.empty()) {
if (test) {
auto next_p = nextq.front();
cout << next_p.first << ", " << next_p.second << endl;
}
nextq.pop();
}
if (test)
cout << "time : " << time << " finished. next size " << currentq.size() << endl;
time++;
}
}
}
signed main()
{
//test = true;
cin >> H >> W;
a.resize(H);
tele.resize(static_cast<int>('z' - 'a') + 1);
REP(i, H)
{
cin >> a.at(i);
REP(j, W)
{
char tmp_c = a.at(i).at(j);
if (tmp_c == 'S') {
s = {i, j};
} else if (tmp_c == 'G') {
g = {i, j};
} else if (tmp_c != '#' && tmp_c != '.') {
tele.at(static_cast<int>(tmp_c - 'a')).push_back({i, j});
}
}
}
dp.resize(H);
REP(i, H)
{
dp.at(i).resize(W, INF);
}
hoge(s.first, s.second);
if (test) {
REP(i, H)
{
REP(j, W)
{
cout << dp.at(i).at(j) << " ";
}
cout << endl;
}
}
if (dp.at(g.first).at(g.second) == INF) {
cout << -1 << endl;
} else {
cout << dp.at(g.first).at(g.second) << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,srt,end) for(ll i = (srt); i < (ll)(end); i++)
ll MOD=1000000007;
ll mod=998244353;
int inf=1000001000;
ll INF=1e18+5;
//cout << std::fixed << std::setprecision(15) << y << endl;
// For debug
// Ref: https://qiita.com/ysuzuki19/items/d89057d65284ba1a16ac
#define dump(var) do{std::cerr << #var << " : ";view(var);}while(0)
template<typename T> void view(T e){std::cerr << e << "\n";}
template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cerr << e << " "; } std::cerr << "\n";}
template<typename T> void view(const std::vector<std::vector<T> >& vv){ std::cerr << "\n"; for(const auto& v : vv){ view(v); } }
template<typename T> void dump_cout(const T& v) { for(long long i = 0; i < v.size(); i++) std::cout << v[i] << (i == v.size()-1 ? "\n" : " "); }
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll n;
cin >> n;
double ans;
double cnt = double(n);
rep(i,0,n){
ans += (1 / ( cnt / double(n) ));
cnt -= 1.0;
}
// eliminate probability of choosing initial position
ans -= 1.0;
printf("%.11f\n", ans);
return 0;
} | // Problem: D - Staircase Sequences
// Contest: AtCoder - AtCoder Beginner Contest 190
// URL: https://atcoder.jp/contests/abc190/tasks/abc190_d
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define deb(x) cout<< #x <<" is "<<x<<endl;
vector<int>f;
void factorize(int n){
for(int i=1;i*i<=n;i++){
if(n % i == 0){
f.emplace_back(i);
if(i != n/i)f.emplace_back(n/i);
}
}
}
bool check(int n,int C){
return ( C % n && (n-1)%2 );
}
void solve(){
int n;
cin>>n;
factorize(2 * n);
int cnt = 0;
// cout<<f.size()<<endl;
for(int i=0;i<f.size();i++){
// deb(f[i])
if(check(f[i] , n))cnt++;
// else cout<<f[i]<<endl;
}
cout<<cnt * 2<<endl;
}
signed main(){
solve();
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
using ll = long long;
constexpr ll INF = 1e18+7;
int n, a[105];
ll x, dp[2][105][105], ans = INF;
int main(){
scanf("%d%lld", &n, &x);
rep(i,n) scanf("%d", a+i);
for(int i=1; i<=n; i++){
fill(dp[0][0], dp[1][n], -INF);
dp[1][0][0] = 0;
rep(j,n) rep(k,i) rep(l,i+1){
dp[j&1][(k+a[j])%i][l+1] = max(dp[j&1][(k+a[j])%i][l+1], dp[j&1^1][k][l] + (k+a[j])/i);
dp[j&1][k][l] = max(dp[j&1][k][l], dp[j&1^1][k][l]);
}
ans = min(ans, x/i - dp[n&1^1][x%i][i]);
}
printf("%lld\n", ans);
} | /*~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
*$* WRITER:kakitamasziru/OxOmisosiru *$*
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=*/
#ifdef ONLINE_JUDGE
#define _GLIBCXX_DEBUG
#endif
#include <stdio.h>
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <iomanip> //setprecision
#include <map> // map
#include <unordered_map> //unordered_map
#include <queue> // queue, priority_queue
#include <set> // set,multiset
#include <stack> // stack
#include <deque> // deque
#include <math.h>//pow,,,
#include <cmath>//abs,,,
#include <bitset> // bitset
#include <numeric> //accumulate,,,
#include <sstream>
//#define endl "\n";
using namespace std;
using PLL = pair<long long,long long>;
typedef tuple<long long,long long,long long> TUP;
using P = pair<int,int>;
const long long INF = 4000000000000000001;
const int inf = 1001001001;
const long long MOD = 1000000007;
// dp[i][j][k]: i番目までj個(0≤j≤cnt)選んでmod cntでkとなる時の必要魔力の最小値
vector<vector<vector<long long>>> dp(105,vector<vector<long long>>(105,vector<long long>(105)));
int main() {
ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int N;cin >> N;
long long X;cin >> X;
vector<long long> a(N);
for(int i = 0;i<N;i++) cin >> a.at(i);
// sum(a) ≤ X
//for(int i = 0;i<101;i++) for(int j = 0;j<101;j++) for(int k = 0;k<101;k++) dp[i][j][k] = INF;
long long ans = INF;
for(int cnt = 1;cnt<=N;cnt++){
for(int i = 0;i<105;i++) for(int j = 0;j<105;j++) for(int k = 0;k<105;k++) dp[i][j][k] = -1;
dp[0][0][0] = 0;
for(int i = 0;i<N;i++){
for(int j = 0;j<N;j++){
for(int k = 0;k<cnt;k++){
// i個目を選ぶ
int nextMOD = (k + a.at(i))%cnt;
if(dp[i][j][k] != -1)dp[i+1][j+1][nextMOD] = max(dp[i+1][j+1][nextMOD] , dp[i][j][k]+a.at(i));
// i個目を選ばない
dp[i+1][j][k] = max(dp[i+1][j][k] , dp[i][j][k]);
//cout << cnt << " " << nextMOD << " " << dp[i+1][j+1][nextMOD] << endl;
}
}
}
// cnt個選んで、k+(cnt*?)=Xにならないといけない
long long res = dp[N][cnt][X%cnt];
if(res == -1) continue;
//cout << cnt << " " << res << " " << " " << X%cnt << " " << ((X-res)/cnt)*cnt + res << endl;
ans = min(ans,((X-res)/cnt));
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
t=0; char ch=getchar(); int f=1;
while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
do { (t*=10)+=ch-'0'; ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f;
}
typedef long long ll;
const int mod=998244353;
int n,m,K;
char s[10];
int d[5010][5010],dp[5010][5010];
void update(int &x,int y) {
x+=y; if (x>=mod) x-=mod;
}
int iv[5],tmp;
int ksm(int x,int y) {
int res=1;
while (y) {
if (y&1) res=(ll)res*x%mod;
x=(ll)x*x%mod; y>>=1;
} return res;
}
int main() {
//freopen("1.txt","r",stdin);
read(n),read(m),read(K); int x,y;
for (int i=1;i<=K;i++) {
read(x),read(y); scanf("%s",s+1);
if (s[1]=='R') d[x][y]=1;
else if (s[1]=='D') d[x][y]=2;
else d[x][y]=3;
}
iv[0]=iv[1]=1; for (int i=2;i<=3;i++) iv[i]=(ll)(mod-mod/i)*iv[mod%i]%mod;
dp[1][1]=1;
for (int i=1;i<=n;i++) {
for (int j=1;j<=m;j++) {
if (i==n&&j==m) { printf("%lld\n",(ll)dp[n][m]*ksm(3,n*m-K)%mod); return 0; }
for (int k=1;k<=3;k++) {
if (d[i][j]&&d[i][j]!=k) continue;
if (!d[i][j]) tmp=iv[3]; else tmp=1;
if (k!=2) update(dp[i][j+1],(ll)dp[i][j]*tmp%mod);
if (k!=1) update(dp[i+1][j],(ll)dp[i][j]*tmp%mod);
}
}
}
return 0;
}
/*
0. Enough array size? Enough array size? Enough array size? Integer overflow?
1. Think TWICE, Code ONCE!
Are there any counterexamples to your algo?
2. Be careful about the BOUNDARIES!
N=1? P=1? Something about 0?
3. Do not make STUPID MISTAKES!
Time complexity? Memory usage? Precision error?
*/ | //#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using ll = long long;
using ld = long double;
#define FOR(i, a, b) for(ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) FOR(i, 0, n)
#define rFOR(i, a, b) for(ll i = (ll)(a - 1); i >= (ll)(b); i--)
#define rrep(i, a) rFOR(i, a, 0)
#define pb push_back
using namespace std;
typedef pair<ll,ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<P> vP;
typedef vector<char> vc;
typedef vector<vc> vvc;
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
const ld PI = acos(-1);
const ll INF = 1e18;
struct edge{ll to, cost;};
template <typename T>
bool chmax(T &a, const T &b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template <typename T>
bool chmin(T &a, const T &b) {
if (a > b) {
a = b;
return true;
}
return false;
}
long long mod_pow(long long x, long long y, long long M){
if(y == 0){
return 1;
}
if(y & 1){
return mod_pow(x, y - 1, M) * x % M;
}
long long t = mod_pow(x, y / 2, M);
return t * t % M;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int H,W,K;
cin >> H >> W >> K;
vvc C(H,vc(W,'O'));
rep(i,K){
int h,w;
char c;
cin >> h >> w >> c;
h--;w--;
C[h][w]=c;
}
vvl dp(H+1,vl(W+1,0));
dp[0][0]=mod_pow(3,H*W-K,MOD2);
ll i3=mod_pow(3,MOD2-2,MOD2);
rep(i,H){
rep(j,W){
if(C[i][j]=='O'){
dp[i][j+1]+=2*i3*dp[i][j];
dp[i+1][j]+=2*i3*dp[i][j];
}
else if(C[i][j]=='X'){
dp[i][j+1]+=dp[i][j];
dp[i+1][j]+=dp[i][j];
}
else if(C[i][j]=='D'){
dp[i+1][j]+=dp[i][j];
}
else if(C[i][j]=='R'){
dp[i][j+1]+=dp[i][j];
}
dp[i+1][j]%=MOD2;
dp[i][j+1]%=MOD2;
}
}
cout << dp[H-1][W-1] << endl;
} |
#include<iostream>
using namespace std;
int main() {
int n;
cin >> n;
double x[n], y[n];
for (int i = 0;i < n;i++) {
cin >> x[i] >> y[i];
}
int flag = 0;
for (int i = 0;i < n - 2;i++) {
for (int j = i + 1;j < n - 1;j++) {
for (int k = j + 1;k < n;k++) {
if (x[i] == x[j]) {
if (x[i] == x[k]) {
flag = 1;
goto ans;
}
}
else {
double dx, dy, b;
dx = x[i] - x[j];
dy = y[i] - y[j];
b = y[i] - dy * x[i] / dx;
double right, left;
left = y[k];
right = dy * x[k] / dx + b;
if (abs(left - right) < 0.00001) {
flag = 1;
goto ans;
}
}
}
}
}
goto ans;
ans:
if (flag==1) cout << "Yes" << endl;
else cout << "No" << endl;
} | //clear adj and visited vector declared globally after each test case
//check for long long overflow
//Mod wale question mein last mein if dalo ie. Ans<0 then ans+=mod;
//Incase of close mle change language to c++17 or c++14
/**#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops”)**/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define int long long
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);cout.precision(dbl::max_digits10);
#define pb push_back
#define mod 1000000007ll //998244353ll
#define lld long double
#define mii map<int, int>
#define pii pair<int, int>
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define rep(i,x,y) for(int i=x; i<y; i++)
#define fill(a,b) memset(a, b, sizeof(a))
#define vi vector<int>
#define setbits(x) __builtin_popcountll(x)
#define print2d(dp,n,m) for(int i=0;i<=n;i++){for(int j=0;j<=m;j++)cout<<dp[i][j]<<" ";cout<<"\n";}
typedef std::numeric_limits< double > dbl;
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
//member functions :
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
const long long N=200005, INF=2000000000000000000;
lld pi=3.1415926535897932;
int lcm(int a, int b)
{
int g=__gcd(a, b);
return a/g*b;
}
int power(int a, int b, int p)
{
if(a==0)
return 0;
int res=1;
a%=p;
while(b>0)
{
if(b&1)
res=(res*a)%p;
b>>=1;
a=(a*a)%p;
}
return res;
}
int32_t main()
{
IOS;
int n;
cin>>n;
int a[n][n];
int r[n];
rep(i,0,n)
{
r[i]=1e9;
rep(j,0,n)
{
cin>>a[i][j];
r[i]=min(r[i], a[i][j]);
}
}
int c[n], f=0;
rep(i,0,n)
{
c[i]=a[0][i]-r[0];
}
rep(i,0,n)
{
rep(j,0,n)
{
if(r[i]+c[j]!=a[i][j])
{
cout<<"No";
return 0;
}
}
}
cout<<"Yes\n";
rep(i,0,n)
cout<<r[i]<<" ";cout<<"\n";
rep(i,0,n)
cout<<c[i]<<" ";
} |
#include <bits/stdc++.h>
using namespace std;
void hoge(int a, int b) {
if (a>b) {
cout << ">" << endl;
} else if (a<b) {
cout << "<" << endl;
} else {
cout << "=" << endl;
}
return;
}
int main() {
int A, B, C;
cin >> A >> B >> C;
// cout << pow(A,C) << endl;
// cout << pow(B,C) << endl;
if (A>=0 && B>=0) {
hoge(A,B);
} else {
if (C % 2==0) {
hoge(abs(A),abs(B));
} else {
hoge(A, B);
}
}
// if (pow(A,C)<pow(B,C)) {
// cout << "<" << endl;
// } else if (pow(A,C)>pow(B,C)) {
// cout << ">" << endl;
// } else {
// cout << "=" << endl;
// }
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vc = vector<char>;
using vcc = vector<vc>;
using Pii = pair<int,int>;
using ld = long double;
#define rep(i,n) for(int i = 0; i<(int)n; i++)
#define per(i,n) for(int i = n-1; i>=0; i--)
#define mysort(V) sort(V.begin(),V.end())
#define myreverse(V) reverse(V.begin(),V.end())
#define myunique(V) V.erase( unique(V.begin(), V.end()), V.end() );
#define print(s) cout << s << endl;
const ld PI = 3.14159265358979;
const long long INF = 1234567890987654321;
long long powll(long long x, long long n){
if(n == 0) return 1;
else return powll(x,n-1)*x;
}
long long gcd(long long a, long long b){
if(a%b==0) return b;
else return gcd(b,a%b);
}
long long nCr(long long n, long long r){
if(r<0 || n<r) return 0;
long long ans = 1;
for(int i =1; i<=r; i++){
ans *= n-i+1;
ans /= i;
}
return ans;
}
int main(){
int a,b,c;
cin >> a >> b >> c;
int ok;
//偶数
if(c%2==0){
if(abs(a)>abs(b)){
ok = 1;
}
else if(abs(a)==abs(b)){
ok = 0;
}
else{
ok = -1;
}
}
else{
if(a<0 && b<0){
ok = a-b;
}
else if(a<0){
ok = -1;
}
else if(b<0){
ok = 1;
}
else{
ok = a-b;
}
}
if(ok>0) cout << ">" << endl;
else if(ok==0) cout << "=" << endl;
else cout << "<" << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef map<int,int> mii;
typedef vector<int> vi;
typedef vector<vector<int>> vii;
typedef vector<long long> vl;
typedef vector<vector<ll>> vll;
typedef vector<bool> vb;
typedef set<int> si;
typedef queue<int> qi;
typedef stack<int> sti;
typedef priority_queue<int> pqi;
#define fori(i,a,b) for(int i = (a); i < (b); ++i)
#define rep(i,b,a) for(int i = (b); i >= (a); --i)
#define all(a) (a).begin(), (a).end()
//#define __builtin_popcountll bitCountLL
//#define __builtin_popcount bitCount
//__builtin_ctz 末尾0的个数, __builtin_clz 前导0的个数
//__builtin_ffs 最后一个1的位置
mt19937 mt_rand(chrono::high_resolution_clock::now().time_since_epoch().count());
//std::uniform_int_distribution<long long> rg(1,1e18);
#define maxn 100005
#define Mod 1000000007
const int INF = 2e9;
const ll ML = 4e18;
vii perm;
int n, m;
void dfs(int i, vector<int>& rs, int state) {
if(i == n) {
perm.push_back(rs);
return;
}
for(int j = 0; j < n; j++) {
if(state & (1 << j) ){
rs.push_back(j + 1);
dfs(i + 1, rs, state ^ (1 << j) );
rs.pop_back();
}
}
}
int ans;
void helper(vi& w, vi& p, vi& b, vi& ma) {
vi a(n);
fori(i,0,n) a[i] = w[p[i] - 1];
vi sum(n);
sum[0] = a[0];
fori(i,1,n) sum[i] = a[i] + sum[i - 1];
int rs = 0;
vii dp(n, vi(n, 0));
fori(i,0,n) {
rep(j, i - 1, 0) {
int dw = (j == 0 ? sum[i] : sum[i] - sum[j - 1]);
int idx = lower_bound(all(b), dw) - b.begin();
dp[j][i] = (idx == 0 ? 0 : ma[idx -1]);
rep(k,i-1, j) {
dp[j][i] = max(dp[j][i], dp[k][i] + dp[j][k]);
}
}
}
ans = min(ans, dp[0][ n -1]);
}
void solve() {
cin >> n >> m;
vi w(n);
vector<pii> bridge(m);
fori(i,0,n) {
cin >> w[i];
}
fori(i,0,m) cin >> bridge[i].second >> bridge[i].first;
sort(all(bridge));
fori(i,0,n) {
if(w[i] > bridge[0].first) {
cout << -1 << endl;
return;
}
}
vi mx(m);
mx[0] = bridge[0].second;
fori(i,1,m) mx[i] = max(mx[i-1], bridge[i].second);
vi rs;
dfs(0,rs,(1 << n) - 1);
ans = INF;
vi b(m);
fori(i,0,m) b[i] = bridge[i].first;
fori(i, 0 , perm.size()) helper(w, perm[i], b, mx);
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
//cout.precision(30); cout << fixed;
// freopen("D:\\data_in.txt", "r", stdin);
solve();
/*
clock_t start,finish;
start = clock();
finish = clock();
cout << "\n" << (double)(finish - start) / (double)(CLOCKS_PER_SEC) * 1000 << "ms" << endl;
*/
return 0;
}
/***************************************************************
question:
idea:
tag:
record:
***************************************************************/
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
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;
}
ll solve() {
int n, m;
cin >> n >> m;
vector<ll> w(n);
ll maxW = 0;
rep(i, n) {
cin >> w[i];
chmax(maxW, w[i]);
}
vector<ll> l(m), v(m);
rep(i, m) {
cin >> l[i] >> v[i];
if (v[i] < maxW) return -1;
}
// 前処理
vector<ll> dist(1 << n, 0);
for (int bit = 0; bit < (1 << n); ++bit) {
ll W = 0;
rep(i, n) {
if (bit & (1 << i)) W += w[i];
}
rep(i, m) {
if (v[i] < W) {
chmax(dist[bit], l[i]);
}
}
}
// DP
ll res = 1LL << 60;
vector<int> ids(n);
iota(ids.begin(), ids.end(), 0);
do {
vector<ll> dp(n, 0);
for (int i = 1; i < n; ++i) {
int bit = (1 << ids[i]);
for (int j = i - 1; j >= 0; --j) {
bit |= (1 << ids[j]);
chmax(dp[i], dp[j] + dist[bit]);
}
}
chmin(res, dp[n - 1]);
} while (next_permutation(ids.begin(), ids.end()));
return res;
}
int main() { cout << solve() << endl; } |
/*
/+===================================================+\
//+---------------------------------------------------+\\
|.|\\...>>>>>>> Hollwo_Pelw's 2nd template<<<<<<<...//|.|
\\+---------------------------------------------------+//
\+===================================================+/
*/
#include <bits/stdc++.h>
using namespace std;
// type
typedef long long ll;
typedef long double ld;
// pair
#define F first
#define S second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pdd pair<ld, ld>
// vector & !!?(string)
#define eb emplace_back
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sz(a) a.size()
// I/O
#define setpre(n) fixed << setprecision(n)
#define Ptest(x) return cout << x << endl, (void) 0;
bool endline = false;
template<class T>
istream& operator >> (istream& inp, vector<T>& v){
for (auto& it : v) inp >> it;
return inp;
}
template<class T>
ostream& operator << (ostream& out, vector<T>& v){
for (auto& it : v) out << it << (endline ? "\n" : " ");
return out;
}
template<class T, class U>
istream& operator >> (istream& inp, pair<T, U>& v){
inp >> v.F >> v.S;
return inp;
}
template<class T, class U>
ostream& operator << (ostream& out, pair<T, U>& v){
out << v.F << ' ' << v.S;
return out;
}
// geometry calculate
#define pi acos(-1.0)
#define g_sin(a) sin(a*pi/180)
#define g_cos(a) cos(a*pi/180)
#define g_tan(a) tan(a*pi/180)
// set val
#define ms0(a) memset(a, 0, sizeof(a));
#define ms1(a) memset(a, 1, sizeof(a));
#define msn1(a) memset(a, -1, sizeof(a));
#define msinf(a) memset(a, 0x3f3f3f, sizeof(a));
void FAST_IO(string filein, string fileout);
void Hollwo_Pelw();
signed main(){
FAST_IO("input.inp", "output.out");
int testcases = 1;
// cin >> testcases;
for (int test = 1; test <= testcases; test++){
// cout << "Case #" << test << ": ";
Hollwo_Pelw();
}
return 0;
}
void FAST_IO(string filein = "", string fileout = ""){
if (fopen(filein.c_str(), "r")){
freopen(filein.c_str(), "r", stdin);
freopen(fileout.c_str(), "w", stdout);
}
cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
}
// constant
const int allmod[3] = {(int) 1e9 + 7, 998244353, (int) 1e9 + 9};
const int mod = allmod[1];
const int MAXN = 2e5 + 6;
const int inf = 2e9;
const ll linf = 1e18;
#define int long long
int n, m;
void Hollwo_Pelw() {
cin >> n >> m;
if (m % n) cout << "No";
else cout << "Yes";
}
/*
./-=====>>><<<-------- DEBUG -------->>><<<=====-\.
/.................................................\
+====================== INP ======================+
+====================== OUT ======================+
+======================*****======================+
\................................................./
.\-=====>>><<<--------= END =-------->>><<<=====-/.
*/ | #include <bits/stdc++.h>
using namespace std;
int main()
{
int n, h;
cin >> n >> h;
if(h % n == 0)
cout << "Yes";
else
cout <<"No";
}
|
#pragma GCC target("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << '(' << p.first << ',' << p.second << ')';
return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << '{';
for(int i = 0; i < (int)v.size(); i++) {
if(i) { os << ','; }
os << v[i];
}
os << '}';
return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
cerr << " " << x;
debugg(args...);
}
#define debug(...) \
cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = (1<<30)-1;
const ll linf = 1LL<<61;
const int MAX = 510000;
int dy[8] = {0,1,0,-1,1,-1,-1,1};
int dx[8] = {-1,0,1,0,1,-1,1,-1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
if(a>b){
a = b; return true;
}
else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
if(a<b){
a = b; return true;
}
else return false;
}
template<typename T> inline void print(T &a){
int sz = a.size();
for(auto itr = a.begin(); itr != a.end(); itr++){
cout << *itr;
sz--;
if(sz) cout << " ";
}
cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
cout << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
cout << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
const int mod = 1e9 + 7;
//const int mod = 998244353;
map<ll,ll> mp;
vl B(80,0);
vl v({2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71});
ll dp[2][1<<20];
int main(){
ll a,b; cin >> a >> b;
for(ll i=a; i<=b; i++){
rep(j,20){
if(i % v[j] == 0) B[i-a] |= 1<<j;
}
}
dp[0][0] = 1;
int bit = 1<<20;
for(ll i=a; i<=b; i++){
rep(j,bit) dp[1][j] = 0;
rep(j,bit){
dp[1][j] += dp[0][j];
if((j & B[i-a]) == 0) dp[1][j+B[i-a]] += dp[0][j];
}
swap(dp[0],dp[1]);
}
ll ans = 0;
rep(i,bit) ans += dp[0][i];
cout << ans << "\n";
} | #include <bits/stdc++.h>
#define DEBUG if(0)
#define lli long long int
#define ldouble long double
using namespace std;
const int maxX = 72, maxPrimes = 20;
bool notPrime[maxX + 1];
vector<lli> primes;
const int maxN = 72;
lli a, b; int n;
int primesMask[maxN + 1];
lli memo[maxN + 1][1LL << maxPrimes];
lli solve(int i = 0, int mask = 0)
{
if (i == n + 1)
return 1;
lli &ans = memo[i][mask];
if (ans != -1)
return ans;
ans = solve(i + 1, mask);
if (!(mask & primesMask[i]))
ans += solve(i + 1, mask | primesMask[i]);
return ans;
}
int main()
{
notPrime[0] = notPrime[1] = true;
for (int i = 2; i <= maxX; i ++)
{
if (!notPrime[i]) primes.push_back(i);
for (int j = 0; i*primes[j] <= maxX; j ++)
{
notPrime[i*primes[j]] = true;
if (i % primes[j] == 0) break;
}
}
while (~scanf("%lld %lld", &a, &b))
{
n = b - a;
for (lli i = a; i <= b; i++)
{
primesMask[i - a] = 0;
for (int j = 0; j < primes.size(); j++)
{
int p = primes[j];
if (i % p == 0)
primesMask[i - a] |= (1LL << j);
}
}
memset(memo, -1, sizeof(memo));
lli ans = solve();
printf("%lld\n", ans);
}
return 0;
} |
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <cmath>
#include <iomanip>
#include <stack>
#include <queue>
#include <numeric>
#include <map>
#include <unordered_map>
#include <set>
#include <fstream>
#include <chrono>
#include <random>
#include <bitset>
//#include <atcoder/all>
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) ((int)(x).size())
#define pb push_back
using ll = long long;
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}
int main(){
int N; cin >> N;
vector<int> A(N); rep(i,N) cin >> A[i];
ll res = 1;
vector<int> p={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47};
ll ans = 1e18;
rep(i,(1<<15)){
ll x=1;
rep(j,15) if((i>>j)&1) x*=p[j];
if(x==1) continue;
int ok = 1;
rep(j,N){
if(gcd(x,A[j])==1) ok=0;
}
if(ok) chmin(ans, x);
}
cout << ans << endl;
return 0;
}
| // clang-format off
#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;
#ifndef godfather
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#endif
#define int long long
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef gp_hash_table<int, int> umap;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update> oset;
typedef pair<int, pii> piii;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<pii> vii;
#define INF 1000000000000000000
#define inf 1000000000
#define MOD 1000000007
#define PI 3.1415926535897932385
#define pb push_back
#define bitc __builtin_popcountll
#define mp make_pair
#define all(ar) ar.begin(), ar.end()
#define fr(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define rep(i, n) for (int i = 0, _n = (n); i < _n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define frr(i, a, b) for (int i = (a), _b = (b); i >= _b; i--)
#define foreach(it, ar) for (auto it = ar.begin(); it != ar.end(); it++)
#define fil(ar, val) memset(ar, val, sizeof(ar)) // 0x3f for inf, 0x80 for -INF can also use with pairs
#ifdef godfather
template<typename T>
void __p(T a) { cout << a << " "; }
template<typename T>
void __p(std::vector<T> a) { cout << "{ "; for (auto p : a) __p(p); cout << "}"; }
template<typename T, typename F>
void __p(pair<T, F> a) { cout << "{ "; __p(a.first); __p(a.second); cout << "}"; }
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) { __p(a1); __p(a...); }
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
cout<<name<<" : ";__p(arg1);cout<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
int bracket=0,i=0;
for(; ;i++)
if(names[i]==','&&bracket==0)
break;
else if(names[i]=='(')
bracket++;
else if(names[i]==')')
bracket--;
const char *comma=names+i;
cout.write(names,comma-names)<<" : ";
__p(arg1);
cout<<"| ";
__f(comma+1,args...);
}
#define trace(...) cout<<"Line:"<<__LINE__<<" "; __f(#__VA_ARGS__, __VA_ARGS__)
int begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
#else
#define trace(...)
#define end_routine()
#endif
mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count());
#define rand(l, r) uniform_int_distribution<int>(l, r)(rng32)
inline bool equals(double a, double b) { return fabs(a - b) < 1e-9; }
ll modpow(ll b, ll e, ll mod=MOD) {
ll ans=1; for(;e;b=b*b%mod,e/=2) if(e&1) ans=ans*b%mod; return ans; }
ld modp(ld b, ll e) {
ld ans=1; for(;e;b=b*b,e/=2) if(e&1) ans=ans*b; return ans; }
void solve()
{
// START
ll n;
cin>>n;
set<ll>primes={2,3,5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47};
ll pro=1;
vi arr(n);
rep(i,n){
cin>>arr[i];
if(primes.count(arr[i]))pro*=arr[i];
}
ll ans=pro;
for(;true;ans+=pro){
bool ok=true;
for(auto k:arr){
if(__gcd(k, ans)==1){
ok=false;break;
}
}
if(ok){
cout<<ans<<"\n";
break;
}
}
// END
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0), cout.precision(15); cout<<fixed;
#ifdef godfather
cin.exceptions(cin.failbit);
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t=1;
// cin>>t;
fr(i,1,t)
{
// cout<<"Case #"<<i<<":"<<" ";
solve();
}
end_routine();
} |
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
// #pragma GCC target("avx2")
// 293206GT
#include <array>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <climits>
#include <limits>
#include <set>
#include <map>
#include <queue>
//#include <unordered_map>
//#include <unordered_set>
//#include <unordered_set>
//#include <stack>
#include <string>
#include <algorithm>
#include <random>
#include <bitset>
#include <chrono>
//#include <cstdlib>
//#include <cstring>
// #include <bits/extc++.h>
// using namespace __gnu_pbds;
//template<class T>
//using OrderedSet = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
using ll = long long;
#define int ll
// #define int long long
using namespace std;
#define put(a) cout << (a) << '\n'
#define sqr(x) ((x) * (x))
typedef pair<int, int> pii;
typedef long double ld;
typedef pair<ld, ld> pld;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
#define ts to_string
#define sts string ts
sts(string s) { return s; }
sts(bool b) { return b ? "true" : "false"; }
sts(char c) { return string(1, c); }
sts(const char *s) { return (string) s; }
template<class A, class B>
sts(pair<A, B>);
template<class T>
sts(T v) {
string s = "{", sep = "";
for (auto x : v) {
s += sep;
sep = ", ";
s += ts(x);
}
return s + "}";
}
template<class A, class B>
sts(pair<A, B> p) { return "(" + ts(p.first) + ", " + ts(p.second) + ")"; }
void dbgPrint() { cerr << endl; }
template<class A, class... B>
void dbgPrint(A a, B... b) {
cerr << ' ' << ts(a);
dbgPrint(b...);
}
#ifdef LOCAL
#define dbg(...) cerr << "line #" << __LINE__ << " [" << #__VA_ARGS__ << "]:", dbgPrint(__VA_ARGS__)
#else
#define dbg(...)
#endif
#define sz(a) (int) (a).size()
#define len(a) (int) (a).size()
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define eb emplace_back
#define rep(x, y, a) for (int x = (y); x < (int)(a); ++x)
#define asd(n) rep(i, 0, n)
#define jkl(n) rep(j, 0, n)
#define UB upper_bound
#define LB lower_bound
#define mp make_pair
namespace IO {
template<class A, class B>
ostream &operator<<(ostream &out, vector<pair<A, B>> a);
template<class A>
ostream &operator<<(ostream &out, vector<A> a);
template<class A, class B>
ostream &operator<<(ostream &out, pair<A, B> a) {
out << a.first << " " << a.second;
return out;
}
template<class A, class B>
ostream &operator<<(ostream &out, vector<pair<A, B>> a) {
for (pair<A, B> x : a)
out << x.first << " " << x.second << '\n';
return out;
}
template<class A>
ostream &operator<<(ostream &out, vector<A> a) {
for (A x : a) out << x << ' ';
return out;
}
template<class A, class B>
istream &operator>>(istream &in, pair<A, B> &a) {
in >> a.first >> a.second;
return in;
}
template<class A>
istream &operator>>(istream &in, vector<A> &a) {
for (A &x : a) in >> x;
return in;
}
} // namespace IO
template<class A, class B>
inline int chkmax(A &a, B b) {
if (a < (A) b) {
a = (A) b;
return 1;
}
return 0;
}
template<class A, class B>
inline int chkmin(A &a, B b) {
if (a > (A) b) {
a = (A) b;
return 1;
}
return 0;
}
using namespace IO;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void solve();
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.precision(41);
int q = 1;
// cin >> q;
while (q--) solve();
}
void solve() {
int n;
cin >> n;
vi a(n);
cin >> a;
sort(all(a));
int f = 1;
asd(n) if (a[i] != i + 1) f = 0;
if (f) put("Yes");
else put("No");
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i,s,n) for(int i = s; i < (int)(n); i++)
int main() {
string s;
cin >> s;
deque<char>deq;
int rc = 0;
rep(i, 0, s.size()) {
if (s[i] == 'R') rc++;
else {
if (rc % 2 == 0) deq.push_back(s[i]);
else deq.push_front(s[i]);
}
}
stack<char>st;
while(!deq.empty()) {
char v = deq.front();
deq.pop_front();
if(!st.empty() && v == st.top()) st.pop();
else st.push(v);
}
string ans;
while (!st.empty()) {
char a = st.top();
ans.push_back(a);
st.pop();
}
if (rc % 2 == 0) reverse(ans.begin(), ans.end());
cout << ans << endl;
} |
// Template
#include "bits/stdc++.h"
#define rep_override(x, y, z, name, ...) name
#define rep2(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep3(i, l, r) for (int i = (int)(l); i < (int)(r); ++i)
#define rep(...) rep_override(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define per(i, n) for (ll i = (ll)(n) - 1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
constexpr int inf = 1001001001;
constexpr ll INF = 3003003003003003003LL;
template <typename T>
inline bool chmin(T &x, const T &y) {
if (x > y) {
x = y;
return true;
}
return false;
}
template <typename T>
inline bool chmax(T &x, const T &y) {
if (x < y) {
x = y;
return true;
}
return false;
}
struct IOSET {
IOSET() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
}
} ioset;
// Main
int main() {
int n;
cin >> n;
vector<int> a(n), b(n);
rep(i, n) {
cin >> a[i] >> b[i];
if (a[i] > 0) --a[i];
if (b[i] > 0) --b[i];
}
vector<int> v(2 * n, -1);
rep(i, n) {
if (a[i] != -1) v[a[i]] = i;
if (b[i] != -1) v[b[i]] = i + n;
}
int cnt = 0;
rep(i, n) if (a[i] == -1 && b[i] == -1) ++cnt;
auto dfs = [&](auto f, int now) -> void {
// cout << now << "\n";
if (now == 2 * n) {
cout << "Yes\n";
exit(0);
}
rep(i, 1, n + 1) {
if (now + i * 2 > 2 * n) break;
bool flag = true;
int c = 0;
rep(j, now, now + i) {
if (v[j] >= n || (v[j + i] != -1 && v[j + i] < n)) {
flag = false;
break;
}
if (v[j] != -1) {
if (b[v[j]] != -1 && b[v[j]] != j + i) {
flag = false;
break;
}
}
if (v[j + i] != -1) {
if (a[v[j + i] - n] != -1 && a[v[j + i] - n] != j) {
flag = false;
break;
}
}
if (v[j] == -1 && v[i + j] == -1) ++c;
}
if (c > cnt) flag = false;
if (!flag) continue;
cnt -= c;
f(f, now + 2 * i);
cnt += c;
}
};
dfs(dfs, 0);
cout << "No\n";
} | #include<bits/stdc++.h>
using namespace std;
template<class T> void chmin(T &a,T b){if(a>b) a=b;};
int main(){
int N;cin>>N;
vector<long> x,y,z;
for(int i=0;i<2*N;i++){
long a;char c;cin>>a>>c;
if(c=='R') x.push_back(a);
if(c=='G') y.push_back(a);
if(c=='B') z.push_back(a);
}
int l,m,n,cnt;
long X,Y,Z;X=Y=Z=(1L<<60);
if(!(x.size()&1) && !(y.size()&1) && !(z.size()&1)){
cout<<0<<endl;
return 0;
}
if(!(x.size()&1)) swap(x,z);
if(!(y.size()&1)) swap(y,z);
//if((x.size()&1) && (y.size()&1)){
sort(x.begin(),x.end());
sort(y.begin(),y.end());
sort(z.begin(),z.end());
//if(x.size()>y.size()) swap(x,y);
l=x.size();m=y.size();n=z.size();
cnt=0;
for(int i=0;i<l;i++){
if(X==0) break;
for(int j=cnt;j<m;j++){
if(j==m-1){
chmin(X,abs(x[i]-y[j]));
break;
}
if(y[j]==x[i]){
X=0;
break;
}
if(y[j+1]<=x[i]){
cnt++;
continue;
}
chmin(X,abs(x[i]-y[j]));
chmin(X,abs(x[i]-y[j+1]));
break;
}
}
cnt=0;
for(int i=0;i<m;i++){
if(Y==0) break;
for(int j=cnt;j<n;j++){
if(j==n-1){
chmin(Y,abs(y[i]-z[j]));
break;
}
if(z[j]==y[i]){
Y=0;
break;
}
if(z[j+1]<=y[i]){
cnt++;
continue;
}
chmin(Y,abs(y[i]-z[j]));
chmin(Y,abs(y[i]-z[j+1]));
break;
}
}
cnt=0;
for(int i=0;i<n;i++){
if(Z==0) break;
for(int j=cnt;j<l;j++){
if(j==l-1){
chmin(Z,abs(z[i]-x[j]));
break;
}
if(x[j]==z[i]){
Z=0;
break;
}
if(x[j+1]<=z[i]){
cnt++;
continue;
}
chmin(Z,abs(z[i]-x[j]));
chmin(Z,abs(z[i]-x[j+1]));
break;
}
}
cout<<min(X,Y+Z)<<endl;
} |
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <utility>
#include <queue>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <climits>
using namespace std;
#define MOD 1000000007
#define PI 3.14159265358979323846264338327950288419
#define int long long
#define ll long long
#define pint pair<ll, ll>
#define pll pair<ll, ll>
#define pb push_back
#define mpair make_pair
#define pm(first, second) pb(mpair(first, second))
#define pff first.first
#define pfs first.second
#define psf second.first
#define pss second.second
#define all(X) (X).begin(), (X).end()
#define rall(X) (X).rbegin(), (X).rend()
#define divceil(a, b) (((a) + ((b) - 1)) / (b))
#define outyn(flag) cout << ((flag)? "Yes": "No") << endl
#define outYN(flag) cout << ((flag)? "YES": "NO") << endl
#define out(output) cout << (output) << endl
//pは素数,aのb乗をpで割った余り
ll powsur(ll a, ll b, ll p) {
if (b == 0) {
return 1;
}
else if (b%2 == 0) {
ll d = powsur(a, b / 2, p);
return ((d%p) * (d%p))%p;
}
else {
return ((a%p) * (powsur(a, b - 1, p)%p))%p;
}
}
void solve(){
int N, P;
cin >> N >> P;
ll ans = (P - 1) * powsur(P - 2, N - 1, MOD);
ans %= MOD;
out(ans);
}
#undef int
int main(){
solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mkp make_pair
#define mt make_tuple
#define ff first
#define ss second
#define M1 1000000007
#define M2 998244353
#define fl(i,a,b) for(ll i=a;i<b;i++)
#define bfl(i,a,b) for(ll i=b-1;i>=a;i--)
#define f(i,n) for(int i=0;i<n;i++)
#define bf(i,n) for(int i=n-1;i>=0;i--)
#define f1(i,n) for(int i=1;i<=n;i++)
#define bf1(i,n) for(int i=n;i>=1;i--)
#define all(v) v.begin(),v.end()
#define pll pair<long,long>
#define pii pair<int,int>
#define vi vector<int>
#define vll vector<ll>
#define p0(a) cout << a << " "
#define p1(a) cout << a << bn
#define p2(a, b) cout << a << " " << b << bn
#define p3(a, b, c) cout << a << " " << b << " " << c << bn
#define p4(a,b,c,d) cout << a << " " << b << " " << c << " " << d << bn
#define debug(x) cerr << #x << " = " << x << endl;
#define max3(a,b,c) max(max(a,b),c)
#define min3(a,b,c) min(min(a,b),c)
#define bn "\n"
#define pbn cout<<"\n"
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << " : " << arg1<<" | ";
__f(comma+1, args...);
}
#else
#define trace(...) 1
#endif
ll modI(ll a,ll m);
ll gcd(ll a,ll b);
ll powM(ll x,ll y,ll m);
void py(int ok)
{
if(ok==1){
cout<<"YES"<<bn;
}else{
cout<<"NO"<<bn;
}
}
void swap(ll& a,ll& b)
{
ll tp=a;
a=b;
b=tp;
}
ll gcd(ll x,ll y)
{
if(x==0) return y;
return gcd(y%x,x);
}
ll lcm(ll x, ll y)
{
if(x==0 || y==0){
cout<<"Not valid lcm"<<bn;
}
ll LCM= x/gcd(x,y);
LCM=LCM*y;
return LCM;
}
ll powM(ll x,ll y,ll m)
{
ll ans=1,r=1;
x%=m;
while(r>0&&r<=y)
{
if(r&y)
{
ans*=x;
ans%=m;
}
r<<=1;
x*=x;
x%=m;
}
return ans;
}
ll modI(ll a, ll m)
{
ll m0=m,y=0,x=1;
if(m==1) return 0;
while(a>1)
{
ll q=a/m;
ll t=m;
m=a%m;
a=t;
t=y;
y=x-q*y;
x=t;
}
if(x<0) x+=m0;
return x;
}
//-----------------------------------------------------------------------------
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);
int tt=1;
// cin>>tt;
f1(testcase_no,tt)
{
// cout<<"test case no. "<<testcase_no<<bn;
double a,b;
cin>>a>>b;
double ans=a*(b/100);
p1(ans);
// cout << fixed << setprecision(25);
}
return 0;
}
|
#include <bits/stdc++.h>
#define DEBUG if(0)
#define lli long long int
#define ldouble long double
using namespace std;
const int maxN = 5e3; int n, m, k;
char mat[maxN][maxN + 1];
const lli mod = 998244353;
lli base;
lli solve1(int i = 0, int j = 0)
{
if (i >= n || j >= m) return 0;
if (i == n - 1 && j == m - 1) return 1;
lli ans = 0;
if (mat[i][j] == 'R')
ans = (ans + solve1(i, j + 1)) % mod;
else if (mat[i][j] == 'D')
ans = (ans + solve1(i + 1, j)) % mod;
else if (mat[i][j] == 'X')
ans = (ans + solve1(i, j + 1) + solve1(i + 1, j)) % mod;
else
ans = 0;
return ans;
}
lli memo[maxN][maxN];
lli modPow(lli x, lli y)
{
lli ans = 1;
while (y)
{
if (y & 1LL) ans = ans * x % mod;
x = x * x % mod, y >>= 1LL;
}
return ans;
}
lli inv(lli x)
{
return modPow(x, mod - 2);
}
lli pot[maxN*maxN + 1];
lli inv3;
lli solve2(int i = 0, int j = 0, int unknown = 0)
{
if (i >= n || j >= m) return 0;
if (i == n - 1 && j == m - 1) return pot[(n*m - k) - 0];
lli &ans = memo[i][j];
if (ans != -1) return ans;
ans = 0;
if (mat[i][j] == 'R')
ans = solve2(i, j + 1, unknown);
else if (mat[i][j] == 'D')
ans = solve2(i + 1, j, unknown);
else if (mat[i][j] == 'X')
{
ans = (solve2(i, j + 1, unknown) + solve2(i + 1, j, unknown)) % mod;
}
else
{
lli a = 2LL * solve2(i, j + 1, unknown + 1) % mod;
lli b = 2LL * solve2(i + 1, j, unknown + 1) % mod;
ans = (a + b) * inv3 % mod;
}
return ans;
}
int main()
{
inv3 = inv(3);
while (~scanf("%d %d %d", &n, &m, &k))
{
pot[0] = 1;
for (int i = 1; i <= n*m; i++)
pot[i] = 3LL * pot[i - 1] % mod;
memset(mat, '.', sizeof(mat));
for (int i = 0; i < n; i++)
mat[i][n] = '\0';
for (int kk = 0; kk < k; kk++)
{
int i, j; char c; scanf("%d %d %c", &i, &j, &c); i--, j--;
mat[i][j] = c;
}
DEBUG
for (int i = 0; i < n; i++)
printf("\t%s\n", mat[i]);
memset(memo, -1, sizeof(memo));
lli ans = solve2();
printf("%lld\n", ans);
}
return 0;
} | #include<bits/stdc++.h>
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--)
#define pii pair<int,int>
#define fi first
#define se second
#define PB push_back
#define ll long long
#define ull unsigned long long
#define pll pair<ll,ll>
#define y1 orzkcz
using namespace std;
const int mo=998244353;
const int N=5005;
int n,m,K;
char s[N][N];
int f[N][N];
int main(){
scanf("%d%d%d",&n,&m,&K);
For(i,1,K){
int x,y; char v[10];
scanf("%d%d%s",&x,&y,v+1);
s[x][y]=v[1];
}
f[1][1]=1;
For(i,1,n) For(j,1,m){
int v=f[i][j];
if (!s[i][j]){
v=1ll*v*(mo+1)/3%mo;
f[i+1][j]=(f[i+1][j]+2ll*v)%mo;
f[i][j+1]=(f[i][j+1]+2ll*v)%mo;
}
if (s[i][j]=='R'||s[i][j]=='X')
f[i][j+1]=(f[i][j+1]+v)%mo;
if (s[i][j]=='D'||s[i][j]=='X')
f[i+1][j]=(f[i+1][j]+v)%mo;
}
int re=f[n][m];
For(i,1,n*m-K) re=3ll*re%mo;
cout<<re<<endl;
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.