code_file1
stringlengths 87
4k
| code_file2
stringlengths 82
4k
|
---|---|
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/priority_queue.hpp>
//#include <ext/pb_ds/hash_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
//typedef gp_hash_table<int, int> hashmap;
//typedef tree<pii, null_type, std::less<pii>, splay_tree_tag, tree_order_statistics_node_update> splaytree;
//typedef tree<pii, null_type, std::less<pii>, rb_tree_tag, tree_order_statistics_node_update> rbtree;
//typedef __gnu_pbds::priority_queue<int, std::greater<int>, __gnu_pbds::binary_heap_tag> binheap;
//typedef __gnu_pbds::priority_queue<int, std::greater<int>, __gnu_pbds::pairing_heap_tag> pairingheap;
template <typename T>
inline void read(T &x)
{
T data = 0, f = 1;
char ch = getchar();
while (!isdigit(ch))
{
if (ch == '-')
f = -1;
ch = getchar();
}
while (isdigit(ch))
{
data = (data << 3) + (data << 1) + ch - '0';
ch = getchar();
}
x = f * data;
}
template <typename T, typename... Args>
inline void read(T &t, Args &...args)
{
read(t);
read(args...);
}
//mt19937 rnd(time(0));
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const int maxn = 2e5 + 9;
const ll mod = 998244353;
ll a[maxn], b[maxn];
priority_queue<ll> pq;
signed main()
{
//freopen("in.txt","r",stdin);
//freopen("data.txt","w",stdout);
//std::ios::sync_with_stdio(false);
//std::cin.tie(0);
//std::cout.tie(0);
int n;
read(n);
for (int i = 1; i <= n; ++i)
read(a[i]);
for (int i = 1; i <= n; ++i)
read(b[i]);
ll ans = 0;
for (int i = 1; i <= n; ++i)
{
pq.push(a[i]);
ans = max(ans, pq.top() * b[i]);
printf("%lld\n", ans);
}
//fclose(stdin);
//fclose(stdout);
return 0;
} | #pragma GCC optimize ("O3")
#include <iostream>
#include <iomanip>
#include <istream>
#include <ostream>
#include <sstream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <queue>
#include <deque>
#include <list>
#include <stack>
#include <map>
#include <unordered_map>
#include <set>
#include <bitset>
#include <utility>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <ctime>
#include <cctype>
#include <cstdlib>
#include <numeric>
#define IINF 2147483647
#define INF 3223372036854775807
#define MOD 1000000007
#define mod 998244353
#define INT_MAX_ 2147483647
#define EPS (1e-10)
//#define REP(i, a, n) fo-r (ll i = a; i < (ll)(n); i++)
//#define REPE(i, a, n) for (ll i = a; i <= (ll)(n); i++)
//#define REP(i,x,y) for(register int i=(x); i<(y); i++)
//#define REPE(i,x,y) for(register int i=(x); i<=(y); i++)
//#define rep(i,n)for (ll i = 0; i < (ll)(n); i++)
#define rep(i,l,r)for(ll i=(l);i<(r);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define Endl endl
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define mmax(x,y)(x>y?x:y)
#define mmin(x,y)(x<y?x:y)
#define chmax(x,y) x=mmax(x,y)
#define chmin(x,y) x=mmin(x,y)
#define all(x) (x).begin(),(x).end()
#define siz(x) (ll)(x).size()
#define PI acos(-1.0)
#define pi acos(-1.0)
#define me memset
// /#define bit(n,k) ((n>>k)&1)
#define lg length()
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef pair<int,int>Pin;
typedef pair<ll,ll>Pll;
template<class T> using V=vector<T>;
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T> >;
long long GCD(long long a, long long b) {return b?GCD(b,a%b):a;}
long long LCM(long long a, long long b) {return a/GCD(a,b)*b;}
ll pom(ll a,ll n,int m){ll x=1;for(a%=m;n;n/=2)n&1?x=x*a%m:0,a=a*a%m;return x;}
#define invp(a,p)pom(a,p-2,p)
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};
int ddx[8]={-1,0,1,0,1,1,-1,-1};
int ddy[8]={0,-1,0,1,1,-1,1,-1};
ll cmp1(pair<Pll,ll> a,pair<Pll,ll> b){
return a.fi.se>b.fi.se;
}
ll cmp2(pair<ll,ll> a,pair<ll,ll> b){
if(a.fi!=b.fi)
return a.fi>b.fi;
else
return a.se<b.se;
}
//----------------------------------------------------------------------
ll sum[200020];
ll maxs[200020];
//----------------------------------------------------------------------
int main(int argc, char * argv[]){
cin.tie(0);
ios::sync_with_stdio(false);
//-------------------------------
//ll begin_t=clock();
//freopen("big.txt", "r", stdin);
//freopen("out3.txt", "w", stdout);
//------------------------------
ll n;cin>>n;
V<ll>a(n);
for(ll i=0;i<n;i++){
cin>>a[i];
if(i>0){
sum[i]=a[i];
sum[i]+=sum[i-1];
//cout<<sum[i]<<Endl;
}
else sum[0]=a[0];
}
ll now = 0;
for(ll i=0;i<n;i++)maxs[i]=-INF;
for(ll i=0;i<n;i++){
now+=a[i];
chmax(maxs[i],now);
if(i>0)chmax(maxs[i],maxs[i-1]);
}
ll ans = 0;
now = 0;
for(ll i=0;i<n;i++){
//cout<<now<<" "<<maxs[i]<<Endl;
chmax(ans,now+maxs[i]);
now+=sum[i];
}
cout<<ans<<endl;
//------------------------------
//fclose(stdin);
//fclose(stdout);
//ll end_t=clock();cout<<"time="<<end_t-begin_t<<"ms"<<endl;
//-------------------------------
return 0;
}
//----------------------------------------------------------------------
|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ll a,b,x,y;
cin>>a>>b>>x>>y;
if(a==b)
{
cout<<x<<"\n";
}
else if(a<b)
{
int d=b-a;
int ans=min(d*y+x,d*2*x+x);
cout<<ans<<"\n";
}
else
{
int d=a-b;
int ans=min({(d*y)+x,(d-1)*2*x+x,(d-1)*y+x});
cout<<ans<<"\n";
}
}
| #include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define co cout
#define ci cin
#define sf1(n) scanf("%lld",&n)
#define sf2(n,m) scanf("%lld %lld",&n,&m);
#define sf3(n,m,p) scanf("%lld %lld %lld",&n,&m,&p);
#define pf1(n) printf("%lld\n",n);
#define mem(a,b) memset(a,b,sizeof(a))
#define en cout<<endl;
#define pb push_back
#define gcd(n,m) __gcd(n,m)
#define lcm(n,m) ((n)/__gcd(n,m))*(m)
#define fi first
#define se second
#define coy cout<< "YES"<<endl;
#define con cout<< "NO"<<endl;
#define f(i,a,b) for(ll i=a;i<b;i++)
#define all(x) (x).begin(),(x).end()
#define allr(x) (x).rbegin(),(x).rend()
#define pi pair<ll,ll>
#define br break;
#define i64 long long
#define PI 2*acos(0.0)
#define MAXN 3*100000
ll n,m,k,g,mx,mn,res,c,x,y,z,w,p,q,r,l,s,t;
void solve()
{
ll a,b;
ci>>a>>b>>x>>y;
if(a<b)
{
res=(((b-a)*2)+1)*x;
res=min(res,x+(y*(b-a)));
co<<res<<endl;
}
else if(a>b)
{
res=(((a-b)*2)-1)*x;
res=min(res,x+(y*(a-b)));
if(a-b>1) res=min(res,x+(y*(a-b-1)));
co<<res<<endl;
}
else
{
co<<x<<endl;
}
}
int main()
{
//#ifdef LOCAL
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
auto start_time = clock();
cerr << setprecision(3) << fixed;
//#endif
cout << setprecision(10) << fixed;
ios::sync_with_stdio(false);
cin.tie(nullptr);
//ci>>t;
t=1;
for(ll ca=0;ca<t;ca++)
{
solve();
}
//#ifdef LOCAL
auto end_time = clock();
cerr<< "Execution time: "<<(end_time-start_time)*(int)1e3/CLOCKS_PER_SEC<<" ms\n";
//#endif
} |
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
t=1;
while(t--)
{
int n;
cin>>n;
vector<int> v;
for(int i=0;i<n;i++)
{
int a;
cin>>a;
v.push_back(a);
}
sort(v.begin(),v.end());
bool ans=true;
for(int i=0;i<n;i++)
if(v[i]!=i+1)
ans=false;
if(ans)
cout<<"Yes";
else
cout<<"No";
}
return 0;
} | #include <iostream>
#include <vector>
using namespace std;
int main(){
int N,X; cin >> N >> X;
vector<int> a(N);
for( int i=0;i<N;i++ ){
cin >> a[i];
}
vector<int> ans;
int element=0;
for(int i=0;i<N;i++){
if(a[i]!=X) ans.push_back(a[i]);
element++;
}
if(element==0) cout << endl;
for(int i=0;i<ans.size();i++){
//int kkk; cin >> kkk;
cout << ans[i] << "";
if(i==ans.size()-1){
cout << endl;
return 0;
}
cout << " ";
}
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define pb push_back
#define f(i,n) for(int i=0; i<n; ++i)
#define fi(i,st,n) for(int i=st; i<=n; ++i)
#define veci vector<int>
#define vecp vector<pair<int,int> >
#define vecl vector<ll>
int prime[100000+10];
ll lcm(ll a, ll b) {
return a*b/__gcd(a,b);
}
ll power(ll a, ll n, ll mod) {
ll res = 1;
while(n > 0) {
if(n&1) {
res = (res*a)%mod;
}
a = (a*a)%mod;
n = n/2;
}
return res;
}
ll sum(int arr[], int n) {
ll res = 0;
f(i,n) {
res += arr[i];
}
return res;
}
void seive() {
prime[1] = 0;
for(int i=2; i<=100000; i++) {
prime[i] = 1;
}
for(ll i=2; i<=100000; i++) {
if(prime[i]) {
for(ll j=i*i; j<=100000; j+=i) {
prime[j] = 0;
}
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
// cin>>t;
while(t--){
ll n;
cin>>n;
ll arr[n];
vector<int> g[200000+10];
f(i,n) {
cin>>arr[i];
g[arr[i]].pb(i);
}
int ans = 0;
for(int i=0; i<n/2; i++) {
if(arr[i] != arr[n-i-1]) {
ans++;
if(g[arr[i]].size() < g[arr[n-i-1]].size() ) {
for(auto it : g[arr[i]]) {
arr[it] = arr[n-i-1];
g[arr[n-i-1]].pb(it);
}
}
else {
for(auto it : g[arr[n-i-1]]) {
arr[it] = arr[i];
g[arr[i]].pb(it);
}
}
}
}
// for(int i=0; i<n; i++) cout<<arr[i]<<" ";
cout<<ans;
}
return 0;
}
| #include <bits/stdc++.h>
#define fastIO ios_base::sync_with_stdio(0), cin.tie(NULL)
#define pb push_back
#define all(x) x.begin(), x.end()
#define endl '\n'
#define mkp make_pair
typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
constexpr int INF = INT_MAX;
using namespace std;
int main(){
fastIO;
int N;
cin >> N;
ll sum = 0;
while (N--)
{
ll a, b;
cin >> a >> b;
sum += ((b * (b + 1)) / 2) - ((a * (a + 1)) / 2) + a;
}
cout << sum;
return 0;
} |
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
using namespace std;
//#pragma GCC optimize("Ofast")
//#pragma GCC optimization("unroll-loops, no-stack-protector")
//#pragma GCC target("avx,avx2,fma")
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n, i, j, k, s = 0, ans;
cin >> n;
vector<int> t(n);
for (i = 0; i < n; i++) {
cin >> t[i];
s += t[i];
}
vector<bool> used(s + 1, false);
used[0] = true;
for (i = 0; i < n; i++) {
for (j = s; j >= 0; j--) {
if (used[j]) used[j + t[i]] = true;
}
}
ans = s;
for (i = 0; i <= s; i++) {
if (used[i]) ans = min(ans, max(i, s - i));
}
cout << ans << "\n";
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
// type alias
typedef long long LL;
typedef pair<int,int> II;
typedef tuple<int,int,int> III;
typedef vector<int> VI;
typedef vector<string> VS;
typedef unordered_map<int,int> MAPII;
typedef unordered_set<int> SETI;
template<class T> using VV=vector<vector<T>>;
// minmax
template<class T> inline T SMIN(T& a, const T b) { return a=(a>b)?b:a; }
template<class T> inline T SMAX(T& a, const T b) { return a=(a<b)?b:a; }
// repetition
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)
#define REPE(i,n) for(int i=0;i<=(n);++i)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) for(int i=0;i<(n);++i)
#define FORR(x,arr) for(auto& x:arr)
#define SZ(a) int((a).size())
// collection
#define ALL(c) (c).begin(),(c).end()
// DP
#define MINUS(dp) memset(dp, -1, sizeof(dp))
#define ZERO(dp) memset(dp, 0, sizeof(dp))
// stdout
#define println(args...) fprintf(stdout, ##args),putchar('\n');
// debug cerr
template<class Iter> void __kumaerrc(Iter begin, Iter end) { for(; begin!=end; ++begin) { cerr<<*begin<<','; } cerr<<endl; }
void __kumaerr(istream_iterator<string> it) { (void)it; cerr<<endl; }
template<typename T, typename... Args> void __kumaerr(istream_iterator<string> it, T a, Args... args) { cerr<<*it<<"="<<a<<", ",__kumaerr(++it, args...); }
template<typename S, typename T> std::ostream& operator<<(std::ostream& _os, const std::pair<S,T>& _p) { return _os<<"{"<<_p.first<<','<<_p.second<<"}"; }
#define __KUMATRACE__ true
#ifdef __KUMATRACE__
#define dump(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); __kumaerr(_it, args); }
#define dumpc(ar) { cerr<<#ar<<": "; FORR(x,(ar)) { cerr << x << ','; } cerr << endl; }
#define dumpC(beg,end) { cerr<<"~"<<#end<<": "; __kumaerrc(beg,end); }
#else
#define dump(args...)
#define dumpc(ar)
#define dumpC(beg,end)
#endif
// $ cp-batch Cooking | diff Cooking.out -
// $ g++ -std=c++14 -Wall -O2 -D_GLIBCXX_DEBUG -fsanitize=address Cooking.cpp && ./a.out
/*
6/6/2021
14:31-14:46
*/
const int MAX_T=1e5+10;
const int MAX_N=1e2+1;
int A[MAX_N];
int N;
int dp[MAX_N][MAX_T];
void solve() {
dp[0][0]=1;
REP(i,N)REP(t,MAX_T) if(dp[i][t]) {
dp[i+1][t]=1;
dp[i+1][t+A[i]]=1;
}
int tot=accumulate(A,A+N,0);
int res=MAX_T;
REP(t,MAX_T) if(dp[N][t]) SMIN(res,max(t,tot-t));
cout<<res<<endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout<<setprecision(12)<<fixed;
cin>>N;
REP(i,N) cin>>A[i];
solve();
return 0;
}
|
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("Ofast")
using namespace __gnu_pbds;
using namespace std;
#define int long long int
#define endl '\n'
#define mod 1000000007
//#define mod 998244353
#define inf 1e18
#define ff first
#define ss second
#define pb push_back
#define pii pair<int,int>
#define mii map<int,int>
#define vi vector<int>
#define vvi vector<vi>
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
#define clr(x,n) x.clear();x.resize(n,0)
#define precise(x) fixed<<setprecision(x)
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update> pbds;
/**
* author: Ashutosh Nayak (@nayakashutosh9)
* created: [2020-11-08 17:28]
* "Compete against yourself"
**/
const int N = 3e5 + 5;
string s; int ans, len;
void check(int i, int val, int cur) {
if (i == len) {
if (val % 3 == 0) ans = min(ans, len - cur);
return;
}
check(i + 1, val, cur);
int x = (int)(s[i] - '0');
val = val * 10 + x; cur++;
check(i + 1, val, cur);
}
void solve() {
int n; cin >> n;
s = "";
int p = n;
while (p) {
s = (char)((p % 10) + '0') + s;
p /= 10;
}
ans = inf; len = (int)s.length();
check(0, 0, 0);
if (ans == inf || ans == len) cout << "-1\n";
else cout << ans << endl;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int T = 1;
//cin >> T; cin.ignore();
for (int i = 1; i <= T; i++) {
// cout << "Case #" << i << ": ";
// clock_t start = clock();
solve();
// clock_t end = clock();
// cout << (end-start) << endl;
}
return 0;
} | #include<bits/stdc++.h>
using namespace std;
int n;
const int N = 25;
typedef long long ll;
ll ans = 1e18;
ll a[N];
void dfs(int p,ll s){
if(p == n){
ans = min(ans,s);
return;
}
ll sum = 0;
for(int i=p;i<n;i++){
sum |= a[i];
dfs(i+1,s^sum);
}
}
int main(){
cin >> n;
for(int i=0;i<n;i++) cin >> a[i];
dfs(0,0);
cout << ans << endl;
return 0;
} |
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn=1e3+5;
int a[maxn],b[maxn];
int book1[maxn],book2[maxn];
int main()
{
int n,m;
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
book1[a[i]]=1;
}
for(int i=1;i<=m;i++)
{
scanf("%d",&b[i]);
book2[b[i]]=1;
}
for(int i=1;i<=1000;i++)
{
if((book1[i]==1 && book2[i]!=1) || (book1[i]!=1 && book2[i]==1))
printf("%d ",i);
}
printf("\n");
}
| //#include<bits/stdc++.h> ----万能头----
#include <iostream>
#include<stdio.h>
#include<string>
#include<algorithm>
#include<string.h>
#include<cstring>
#include<cmath>
#include<queue>
#include<list>
#include<map>
#include<set>
#include<unordered_map>
#include<stack>
#include<time.h>
using namespace std;
const int mod = 1e9+7;
const int N = 2e3+5;
const int maxn = 1e5+5;
const long long INF =1e18;
#define REP(i,n) for(int i = 1; i <= n; ++i)
#define REPA(i,j,n) for(ll i = j; i <= n; ++i)
#define RREP(i,n) for(ll i = n; i >= 1; --i)
#define REPR(i,j,n) for(ll i = n; i >= j; --i)
#define lll __int128
#define Accpect return 0;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<double,ll> pdi;
int t;
ll n, m;
ll a[N];
inline int read(){
char c=getchar();
int x=0,f=1;
while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar();
return x*f;
}
const int MOD = 998244353;
char g[503][503];
ll fast(ll a,ll b){
ll res = 1;
while(b){
if(b & 1)res = res * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return res % MOD;
}
void solve(){
ll w, h;
cin>>h>>w;
ll ans = 0;
bool f = 1;
for(int i = 1; i <= h; ++i)cin>>g[i]+1;
for(int i = 2 ; f && i <= w; ++i){
int x = 1, y = i;
int c1 = 0, c2 = 0;
while(x <= h && y >= 1){
if(g[x][y] == 'R')++c1;
else if(g[x][y] == 'B')++c2;
if(c1 && c2){
f = 0;break;
}
++x, --y;
}
if(!c1 && !c2) ++ans;
}
for(int i = 2; f && i < h ; ++i){
int x = i , y = w;
int c1 = 0, c2 = 0;
while(y >= 1 && x <= h){
if(g[x][y] == 'R')++c1;
else if(g[x][y] == 'B')++c2;
if(c1 && c2){
f = 0;break;
}
--y,++x;
}
if(!c1 && !c2)++ans;
}
if(!f) cout<<0<<'\n';
else {
if(g[1][1] == '.')++ans;
if(g[h][w] == '.')++ans;
cout<<fast(2,ans)<<'\n';
}
}
signed main(){
//ios::sync_with_stdio(0);
//cin.tie(0);
//.cout.tie(0);
#ifdef ACM
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
t = 1;
//srand((unsigned)time(NULL));
// cin>>t;
while (t--){
solve();
}
fclose(stdin);
fclose(stdout);
//-------------
Accpect;
//-------------
}
|
#include<bits/stdc++.h>
using namespace std;
bool check(int num){
int num1 = num, num2 = num;
while(num1 != 0){
int x = num1 % 10;
num1 /= 10;
if(x == 7)
return 0;
}
while(num2 != 0){
int x = num2 % 8;
num2 /= 8;
if(x == 7)
return 0;
}
return 1;
}
int main(){
int N = 0, ans = 0;
cin >> N;
for(int i = 1 ; i <= N ; i ++)
if(check(i))
ans ++;
cout << ans;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
string s;
int n, res = 0;
void f(int i, int x, int cnt = 0) {
if (i == n) {
if (x % 3 == 0) {
res = max(res, cnt);
}
return;
}
f(i + 1, x + s[i] - '0', cnt + 1);
f(i + 1, x, cnt);
}
void solve() {
cin >> s;
n = s.size();
f(0, 0);
cout << (n - res == n ? -1 : n - res);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t = 1;
//cin >> t;
for (int i = 1; i <= t; i++) {
solve();
}
} |
#include <iostream>
#include <bits/stdc++.h>
#include <vector>
#include <string>
#define rep(i, n) for(int i = 0; i < n; i++)
#define repr(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
typedef long long ll;
using namespace std;
int main(){
int N;
cin >> N;
vector<int> A(N);
int temp;
ll sum=0;
rep(i,N) cin >> A[i];
rep(i,N){
cin >> temp;
sum += A[i]*temp;
}
if(sum==0) cout << "Yes" << endl;
else cout << "No" << endl;
} | #include <bits/stdc++.h>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define _rep(i, n) _rep2(i, 0, n)
#define _rep2(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(...) GET_MACRO(__VA_ARGS__, _rep2, _rep)(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using i64 = long long;
template<class T>
bool chmin(T& a, const T& b) { return (b < a) ? (a = b, true) : false; }
template<class T>
bool chmax(T& a, const T& b) { return (b > a) ? (a = b, true) : false; }
template<typename T>istream& operator>>(istream&i,vector<T>&v){rep(j,v.size())i>>v[j];return i;}
template<typename T>string join(vector<T>&v){stringstream s;rep(i,v.size())s<<' '<<v[i];return s.str().substr(1);}
template<typename T>ostream& operator<<(ostream&o,vector<T>&v){if(v.size())o<<join(v);return o;}
template<typename T>string join(vector<vector<T>>&vv){string s="\n";rep(i,vv.size())s+=join(vv[i])+"\n";return s;}
template<typename T>ostream& operator<<(ostream&o,vector<vector<T>>&vv){if(vv.size())o<<join(vv);return o;}
long long gcdsolve(long long a, long long b, long long &x, long long &y)
{
if(b == 0)
{
x = 1;
y = 0;
return a;
}
long long d = gcdsolve(b, a % b, y, x);
y -= a / b * x;
return d;
}
int main()
{
int t;
cin >> t;
while(t--)
{
i64 ans = LLONG_MAX / 2;
i64 x, y, p, q;
cin >> x >> y >> p >> q;
rep(i, y)
{
rep(j, q)
{
i64 n, m;
i64 c = p - x - i + j;
i64 a = 2 * (x + y);
i64 b = -(p + q);
auto d = gcdsolve(a, b, n, m);
if(c % d) continue;
a /= d;
b /= d;
m *= (c / d);
auto u = m % a;
if (u < 0)
{
if(a < 0) u -= a;
else u += a;
}
i64 v = ((p + q) * u + p + j);
chmin(ans, v);
}
}
cout << (ans != (LLONG_MAX / 2)? to_string(ans): "infinity") << endl;
}
} |
/*
* Author: $%U%$
* Time: $%Y%$-$%M%$-$%D%$ $%h%$:$%m%$:$%s%$
*/
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define lowbit(x) ((x)&(-x))
#define sz(x) ((int)x.size())
#define fr(x) freopen(x,'r',stdin)
#define fw(x) freopen(x,'w',stdout)
#define mst(x,a) memset(x,a,sizeof(x))
#define all(a) begin(a),end(a)
#define bitcnt(x) (__builtin_popcountll(x))
#define rep(i,a,b) for(int i = (a);i<=(b); ++i)
#define per(i,a,b) for(int i = (a);i>=(b); --i)
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef pair<double,double> PDD;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<double> VB;
typedef double db;
template <class T> void clear(T& a) { T().swap(a);}
template <class T,class S>
inline bool upmin(T& a,const S&b){ return a>b ? a=b,1:0;}
template <class T,class S>
inline bool upmax(T&a, const S&b){ return a<b? a=b,1:0;}
//fast read(improved by fread)
char buf[1<<21],*p1=buf,*p2=buf;
inline int getc(){
return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;
}
inline int redi() {
int ret = 0,f = 0;char ch = getc();
while (!isdigit (ch)) {
if (ch == '-') f = 1;
ch = getc();
}
while (isdigit (ch)) {
ret = ret * 10 + ch - 48;
ch = getc();
}
return f?-ret:ret;
}
//global variable
const int N = 1;
const int mod = 1e9+7;
const double eps = -1e-12;
int n;
long double a,b,m;
//
//code from here! Come on! Have a pleasant experience~
int main()
{
IO;
cin>>n>>m;
long double res = m;
rep(i,1,n){
cin>>a>>b;
res -= (a * b/100.0);
// cout<<res<<endl;
if(res<eps){
cout<<i<<endl;
return 0;
}
}
cout<<"-1"<<endl;
return 0;
} | #include<bits/stdc++.h>
#define int long long
using namespace std;
constexpr int INF=1e18;
void chmin(int&a,int b){if(a>b)a=b;}
signed main(){
int N; cin>>N;
string S; cin>>S;
vector<vector<int>> dp(N,vector<int>(26,INF));
dp[0][S[0]-'a']=0;
int ans=INF;
for(int i=0;i<N;i++){
for(int j=0;j<26;j++)if(S[i]!='a'+j){
if(i==N-1)chmin(ans,dp[i][j]);
else chmin(dp[i+1][S[i+1]-'a'],dp[i][j]+1);
}
if(i!=N-1){
for(int j=0;j<26;j++)chmin(dp[i+1][j],dp[i][j]);
}
}
cout<<(ans==INF?-1:ans+1)<<endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for (int i = 0; i < (n); ++i)
template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b;return true;}return false;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b;return true;}return false;}
using ll = long long;
using P = pair<int,int>;
using Pl = pair<long long,long long>;
using veci = vector<int>;
using vecl = vector<long long>;
using vecveci = vector<vector<int>>;
using vecvecl = vector<vector<long long>>;
const int MOD = 1000000007;
const double pi = acos(-1);
ll gcd(ll a, ll b) {if(b == 0) return a; else return gcd(b,a%b);}
ll lcm(ll a, ll b) {return a*b/gcd(a,b);}
ll INF = 1LL<<60;
int main() {
int N; cin >> N;
int M; cin >> M;
vecl H(N), W(M);
REP(i,N) cin >> H[i];
REP(i,M) cin >> W[i];
sort(H.begin(),H.end());
vecl L(200010), R(200010);
for(int i = 2; i < N; i++) {
L[i] = L[i-2] + H[i-1] - H[i-2];
R[i] = R[i-2] + H[N-i+1] - H[N-i];
}
ll ans = INF;
for(ll w : W) {
int i = lower_bound(H.begin(),H.end(),w) - H.begin();
if(i%2 == 0) chmin(ans,L[i]+R[N-i-1] + H[i]-w);
else chmin(ans,L[i-1] + R[N-i] + w-H[i-1]);
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define INF32 2147483647 //2.147483647×10^{9}:32bit整数のinf
int main(){
int H,W;
cin >> H >> W;
vector<int > A(H*W);
int amin=10000;
for(int i=0; i<H*W; i++){
cin >> A.at(i);
amin = min(amin,A.at(i));
}
int ans=0;
for(int i=0; i<H*W; i++){
ans += A.at(i) - amin;
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ull> vull;
typedef pair<int, int> pii;
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORDOWN(i, a, b) for (int i = a; i >= b; --i)
#define ff first
#define ss second
#define pb push_back
#define debug(x) cout << #x << " = " << x << '\n';
ull func(vector<ll> &v) {
ull ans = 0;
int n = v.size();
for (int i = 0; i < n - 1; ++i) {
for (int j = i + 1; j < n; ++j) {
ans += abs(v[i] - v[j]);
}
}
return ans;
}
/*
* 3
10002 8668 92833
*/
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<ll> v(n);
// vector<ll> v1(n);
for (int i = 0; i < n; ++i) {
cin >> v[i];
// v1[i] = v[i];
}
sort(v.rbegin(), v.rend());
vector<ll> pref(n + 1);
pref[0] = v[0];
for (int i = 1; i < n; ++i) {
pref[i] = pref[i - 1] + v[i];
}
ull ans = 0;
for (ll i = 0; i < n - 1; ++i) {
ans += abs(((ll)n - i - 1) * v[i] - (pref[n - 1] - pref[i]));
}
cout << ans;
return 0;
}
| // Standard I/O
#include <iostream>
#include <sstream>
#include <cstdio>
// Standard Library
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
// Template Class
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
// Container Control
#include <algorithm>
using namespace std;
#define rep( i, n ) for( int i = 0; i < n; ++i )
#define irep( i, n ) for( int i = n-1; i >= 0; --i )
#define reep( i, s, n ) for ( int i = s; i < n; ++i )
#define ireep( i, n, s ) for ( int i = n-1; i >= s; --i )
#define foreach(itr, x) for( typeof(x.begin()) itr = x.begin(); itr != x.end(); ++itr)
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
#define all( v ) v.begin(), v.end()
#define fs first
#define sc second
#define vc vector
// for visualizer.html
double SCALE = 1.0;
double OFFSET_X = 0.0;
double OFFSET_Y = 0.0;
#define LINE(x,y,a,b) cerr << "line(" << SCALE*(x) + OFFSET_X << "," \
<< SCALE*(y) + OFFSET_Y << "," \
<< SCALE*(a) + OFFSET_X << "," \
<< SCALE*(b) + OFFSET_Y << ")" << endl;
#define CIRCLE(x,y,r) cerr << "circle(" << SCALE*(x) + OFFSET_X << "," \
<< SCALE*(y) + OFFSET_Y << "," \
<< SCALE*(r) << ")" << endl;
typedef long long ll;
typedef complex<double> Point;
typedef pair<int, int> pii;
typedef pair<int, pii> ipii;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector< vector<int> > vii;
typedef vector< vector<double> > vdd;
typedef vector<int>::iterator vi_itr;
const int IINF = 1 << 28;
const ll IINFLL = 1ll << 61;
const int MOD = 1e9+7;
const double INF = 1e30;
const double EPS = 1e-10;
const double PI = acos(-1.0);
// Direction : L U R D
const int dx[] = { -1, 0, 1, 0};
const int dy[] = { 0, -1, 0, 1 };
int main()
{
int n;
cin >> n;
ll c_a = 0, c_b = -IINFLL, c_c = IINFLL;
rep(i, n) {
ll a, t;
cin >> a >> t;
if ( t == 1 ) {
c_a = c_a + a;
c_b = a + c_b;
c_c = a + c_c;
} else if ( t == 2 ) {
c_b = max(a, c_b);
c_c = max(a, c_c);
} else {
c_b = min(a, c_b);
c_c = min(a, c_c);
}
}
int q;
cin >> q;
rep(i, q) {
ll x;
cin >> x;
cout << min(max(x + c_a, c_b), c_c) << endl;
}
}
|
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,k,n) for(int i=k;i<n;i++)
#define all(x) x.begin(),x.end()
#define sz(x) (int) x.size()
#define pii pair<int,int>
#define debug(x) cerr << #x << " : " << x << "\n";
#define int ll
/*############################ !!SOLVE!! ################################*/
struct interval{
int t;
int x;
int y;
};
void solve(){
int n;
cin >> n;
vector <interval> seg(n);
rep(i,0,n){
cin >> seg[i].t >> seg[i].x >> seg[i].y;
}
int intersections = 0;
for(int i = 0 ;i < n ; i++){
interval one = seg[i];
for(int j = i+1;j<n;j++){
interval two = seg[j];
if(one.x < two.x){
if(two.x < one.y){
intersections++;
}
if(one.y == two.x){
if((one.t == 1 || one.t == 3) and (two.t == 1 || two.t == 2))
intersections++;
}
}
else{
if(one.x < two.y){
intersections++;
}
if(one.x == two.y){
if((two.t == 1 || two.t == 3) and (one.t == 2 || one.t == 1))
intersections++;
}
}
}
}
cout << intersections << "\n";
}
/*######################## !!TESTCASES!! ################################*/
signed main(){
ios ::sync_with_stdio(0);
cin.tie(0);
int testcases = 1;
while(testcases -->0){
solve();
}
}
| #include <bits/stdc++.h>
#include <set>
#define ll long long
#include <string>
#define pb push_back
#define mp make_pair
#define pi 3.14159265358979323846
#define mod 1000000007
#define INF 1e18
// typedef pair<ll, ll> pi;
#define rep(i,n) for(i=0;i<n;i++)
#define repk(i,k,n) for(i=k;i<n;i++)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define oset tree <pair<ll int ,ll int>, null_type, less<pair<ll int,ll int> >, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
#define see(args...) \
{ \
cerr << "LINE " << __LINE__; \
string _s = #args; \
replace(_s.begin(), _s.end(), ',', ' '); \
stringstream _ss(_s); \
istream_iterator<string> _it(_ss); \
err(_it, args); \
cerr << endl; \
}
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << ' ' << *it << " = " << a;
err(++it, args...);
}
void fastio()
{
#ifndef ONLINE_JUDGE
// for getting input from input.txt
freopen("input.txt", "r", stdin);
// for writing output to output.txt
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
void solve()
{
string s; cin >> s;
string s2 = "ZONe";
ll int i, ans = 0;
rep(i, 12)
{
if (i + 3 < 12)
{
if (s.substr(i, 4) == s2)ans++;
}
}
cout << ans << endl;
}
int main()
{
fastio();
ll int t;
// cin >> t;
t = 1;
while (t--)
{
solve();
}
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define rep(i, n) for (int i = 0; i < (int)(n); ++ i)
int main() {
int n;
string s, t;
cin >> n >> s >> t;
vector <int> vs, vt;
rep(i, n) if (s[i] == '1') vs.pb(i);
rep(i, n) if (t[i] == '1') vt.pb(i);
if (vs.size() < vt.size() || (vs.size() - vt.size()) % 2 == 1) {
printf("-1\n");
return 0;
}
ll ans = 0;
int j = 0;
rep(i, vs.size()) {
if (j < vt.size() && vs[i] >= vt[j]) {
ans += vs[i] - vt[j];
++ j;
} else {
if (i + 1 == vs.size()) {
printf("-1\n");
return 0;
}
ans += vs[i + 1] - vs[i];
++ i;
}
}
if (j < vt.size()) {
printf("-1\n");
return 0;
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string s, t;
cin >> s >> t;
int pre = 0;
queue<int> stk;
for (int i = 0; i < n; ++i) {
if (s[i] == '1') {
stk.push(i);
}
}
long long ans = 0;
bool ok = true;
for (int i = 0; i < n; ++i) {
if (s[i] == '1') {
stk.pop();
if (t[i] == '0') {
if (stk.empty()) {
ok = false;
break;
}
ans += stk.front() - i;
s[stk.front()] = '0';
stk.pop();
}
} else {
if (t[i] == '1') {
if (stk.empty()) {
ok = false;
break;
}
ans += stk.front() - i;
s[stk.front()] = '0';
stk.pop();
}
}
}
if (ok) {
if (stk.size() % 2) {
ok = false;
} else {
while (!stk.empty()) {
int x = stk.front();
stk.pop();
int y = stk.front();
ans += y - x;
}
}
}
cout << (ok ? ans : -1) << endl;
}
|
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define ll long long
#define pll pair<ll,ll>
#define rep(i,n) for(ll i=0;i<n;i++)
#define mod 1000000007
#define INF 10000000000000000
#define F first
#define S second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define pie 3.141592653589793238462643383279
#define PYES cout<<"YES"<<endl
#define PNO cout<<"NO"<<endl
#define SB(a) sort(a.begin(),a.end(),greater<int>());
#define SS(a) sort(a.begin(),a.end());
#define vll vector<ll>
#define vpll vector<pll>
vector<bool> prime;
vector<ll> fact,inv,primes,factors;
void fast()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
void factorize(ll a)
{
factors.clear();
for(ll i=1;i*i<=a;i++)
{
if (i*i==a) factors.pb(i);
else if (a%i==0)
{
factors.pb(i);
factors.pb(a/i);
}
}
sort(factors.begin(),factors.end());
}
ll power(ll a,ll b)
{
if(a==1||b==0) return 1;
ll c=power(a,b/2);
ll res=1;
res=c*c;
if(res>=mod) res%=mod;
if(b%2) res*=a;
if(res>=mod) res%=mod;
return res;
}
ll modInv(int a)
{
return power(a,mod-2);
}
void factorial(ll n)
{
fact.resize(n+1);
fact[0]=1;
for(ll i=1;i<=n;i++)
{
fact[i]=fact[i-1]*i;
if(fact[i]>=mod) fact[i]%=mod;
}
}
void InvFactorial(ll n)
{
inv.resize(n+1);
inv[0]=1;
for(ll i=1;i<=n;i++) inv[i]=modInv(fact[i]);
}
ll ncr(ll n,ll r)
{
if(n<r||n<0||r<0) return 0;
ll b=inv[n-r];
ll c=inv[r];
ll a=fact[n]*b;
if(a>=mod) a%=mod;
a*=c;
if(a>=mod) a%=mod;
return a;
}
void remove_duplicates(vector<pair<ll,ll> > &v)
{
sort(v.begin(),v.end());
ll _size=unique(v.begin(),v.end())-v.begin();
v.resize(_size);
}
unsigned ll gcd(unsigned ll u, unsigned ll v)
{
if(u==0||v==0) return max(u,v);
unsigned ll shift=__builtin_ctz(u|v);
u>>=__builtin_ctz(u);
do{
v>>=__builtin_ctz(v);
if(u>v) swap(u,v);
v-=u;
}while(v!=0);
return u<<shift;
}
void sieve(int n)
{
prime.assign(n+1,1);
prime[1]=false;
for(ll p=2;p*p<=n;p++) if(prime[p]) for(ll i=p*p;i<=n;i+=p) prime[i]=false;
for(ll i=2;i<n+1;i++) if(prime[i]) primes.push_back(i);
}
ll size(ll l)
{
ll p=l;
ll count=0;
while(p) p/=10,count++;
return count;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------
// Always remember: if (condition) if (condition) statement; This doesn't work !!
// Always increase boundaries for global or frequency questions
// pow((int)10,i) doesn't work sometimes. It is better to use multiple of 10 in arrays. Always remember this.
// delete values when defining global if there are multiple testcases
// don't try to be smart and make stupid mistakes
void solve()
{
ll n;
cin>>n;
vll a(n);
a[0]=10;
a[1]=15;
a[2]=6;
ll p=20,q=45;
ll b=2;
for(ll i=3;i<n;i++)
{
if (b==2)
{
if ((i-1)*6<=10000) a[i]=(i-1)*6;
else b=1;
}
if (b==1)
{
if (p<=10000)
{
if (p%3) a[i]+=p,p+=10;
else a[i]+=p+10,p+=20;
}
else b=0;
}
if (b==0)
{
a[i]=q,q+=30;
}
}
for(auto &i: a) cout<<i<<' ';
}
signed main()
{
fast();
ll T=1;
//cin>>T;
for(ll i=1;i<=T;i++) solve();
}
| #include<bits/stdc++.h>
using namespace std;
#define ll long long
void solve()
{
int n;
cin>>n;
string s=to_string(n);
string t=s;
reverse(t.begin(),t.end());
if(s==t)
cout<<"Yes\n";
else{
int count=0;
for(int i=0;i<t.length();i++){
if(t[i]=='0')
count++;
else
break;
}
for(int i=0;i<count;i++){
t+='0';
}
string p=t;
reverse(p.begin(),p.end());
if(p==t)
cout<<"Yes\n";
else
cout<<"No\n";
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
solve();
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 = 1e4; int n;
int a[maxN];
int main()
{
while (~scanf("%d", &n))
{
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
int ans = 0;
for (int i = 0; i < n; i++)
{
int curr = a[i];
for (int j = i; j < n; j++)
{
curr = min(curr, a[j]);
ans = max(ans, curr * (j - i + 1));
}
}
printf("%d\n", ans);
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP(i,n) for(int i=0;i<n;i++)
int main() {
ll n;
cin >> n;
ll n2 = n*2, ans = 0;
ll sq = sqrt(n2);
for (ll x = 1; x <= sq; x++) {
if (n2 % x) continue; // 約数ではない
ll y = n2/x;
bool ok = (x+y-1) % 2 == 0 && (x-y+1) % 2 == 0;
if (ok) ans += x==y ? 1 : 2;
}
cout << ans << endl;
return 0;
} |
/*input
5 4 4
3 2
3 4
4 2
5 2
*/
// assic value of ('0'-'9') is(48 - 57) and (a-z) is (97-122) and (A-Z) is(65-90) and 32 for space
// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// order_of_key (k) : Number of items strictly smaller than k .
// find_by_order(k) : K-th element in a set (counting from zero).
using namespace __gnu_pbds;
template<class T> using oset=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long int
#define pb push_back
#define pii pair<ll ,ll >
#define vpii vector< pii >
#define vi vector<ll >
#define vs vector< string >
#define vvi vector< vector< ll > >
#define inf (ll)1e18
#define all(it,a) for(auto it=(a).begin();it!=(a).end();it++)
#define F first
#define S second
#define sz(x) (ll )x.size()
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define repr(i,a,b) for(ll i=a;i>b;i--)
#define lbnd lower_bound
#define ubnd upper_bound
#define mp make_pair
#define whatis(x) cout << #x << " is " << x << "\n";
#define graph(n) adj(n,vector< ll > () )
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define debug(x) cout << #x << " " << x << endl;
#define debug_p(x) cout << #x << " " << x.F << " " << x.S << endl;
#define debug_v(x) {cout << #x << " "; for (auto ioi : x) cout << ioi << " "; cout << endl;}
#define debug_vp(x) {cout << #x << " "; for (auto ioi : x) cout << '[' << ioi.F << " " << ioi.S << ']'; cout << endl;}
#define debug_v_v(x) {cout << #x << "/*\n"; for (auto ioi : x) { for (auto ioi2 : ioi) cout << ioi2 << " "; cout << '\n';} cout << "*/" << #x << endl;}
const ll N = 2e5+5;
vi bit(N,0);
void add(ll idx,ll val)
{
for(ll i = idx+1;i<N;i+=i&-i) bit[i]+=val;
}
ll prefixSum(ll idx)
{
ll sum=0;
for(ll i = idx+1;i>0;i-=i&-i) sum+=bit[i];
return sum;
}
int solve()
{
ll h,w,m,ans=0;
cin>>h>>w>>m;
vector<ll> row(h,w),col(w,h);
rep(i,0,m)
{
ll x,y;cin>>x>>y;
x--,y--;
row[x] = min(row[x],y);
col[y] = min(col[y],x);
}
/*
0 0 0 0 1 5
0 0 1 0 0 3
0 1 0 0 0 2
0 0 0 0 0 6
0 0 0 1 0 4
1 0 0 0 0
0 1 0 0 0
*/
rep(i,0,col[0]) ans+=row[i];
vpii b;
rep(j,0,row[0]) b.pb(mp(col[j],j));
sort(b.begin(), b.end());
ll L = 0;
ll cnt=0;
rep(i,0,sz(b))
{
pii p = b[i];
while(L<p.F && L<col[0])
{
add(row[L],1);
cnt++;
L++;
}
ans+=col[p.S] - ( cnt - prefixSum(p.S-1));
}
cout<<ans<<"\n";
return 0;
}
int main()
{
auto start = chrono::high_resolution_clock::now();
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll test_cases=1;
//cin>>test_cases;
while(test_cases--)
solve();
auto stop = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::milliseconds>(stop-start);
//cout<<"\nduration: "<<(double)duration.count()<<" milliseconds";
} | #include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<math.h>
using namespace std;
typedef long long ll;
#define int long long
#define double long double
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
template<class T>bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
#define fore(i,a) for(auto &i:a)
#define REP(i,n) for(int i=0;i<n;i++)
#define eREP(i,n) for(int i=0;i<=n;i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define eFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define SORT(c) sort((c).begin(),(c).end())
#define rSORT(c) sort((c).rbegin(),(c).rend())
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))
#define INF 1000000000
#define LLINF 922337203685775807
#define mod 1000000007
#define eps 1e-12
//priority_queue<int,vector<int>, greater<int> > q2;
int N, M;
int A[200010];
int ans = -LLINF;
vector<VI> G;
int dp[200010];
bool vis[200010];
signed main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> N >> M;
G.resize(N);
REP(i, N) {
cin >> A[i];
dp[i] = LLINF;
}
REP(i, M) {
int v, u; cin >> v >> u;
v--, u--;
G[v].push_back(u);
}
priority_queue<int, vector<int>, greater<int> > Q;
REP(now, N) {
chmax(ans, A[now] - dp[now]);
chmin(dp[now], A[now]);
fore(to, G[now]) {
chmin(dp[to], dp[now]);
}
}
cout << ans << endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+1000;
const int MOD=1e9+7;
int n;
int h[N],e[N],ne[N],idx;
ll w[N],d[N];
ll f[2];
void add(int a,int b,ll c)
{
e[idx]=b;w[idx]=c;ne[idx]=h[a];h[a]=idx++;
}
void dfs(int u,int fa)
{
for(int i=h[u];~i;i=ne[i])
{
int j =e[i];
if(j==fa) continue;
d[j]=d[u]^w[i];
dfs(j,u);
}
}
int main()
{
memset(h,-1,sizeof h) ;
scanf("%d",&n);
for(int i=1,u,v;i<=n-1;i++)
{
ll c;
scanf("%d %d %lld",&u,&v,&c);
add(u,v,c);add(v,u,c);
}
dfs(1,-1);
ll ans = 0;
for(int i=0;i<60;i++)
{
for(int j=1;j<=n;j++) f[(d[j]>>i)&1]++;
ans = (ans +(1ll<<i)%MOD*f[0]%MOD* f[1]%MOD) %MOD;
f[0]=f[1]=0ll;
}
printf("%lld\n",ans);
} | //#include<math.h>
#include<algorithm>
#include<stdlib.h>
#include<time.h>
#include<stdio.h>
#include<string.h>
#define un unsigned
#define srd srand(time(0))
#define ll long long
#define con continue
#define gtc getchar()
#define ptc putchar
#define dou double
#define eps 0.00000000001
#define opr operator
#define cl(x,a) memset(x,a,sizeof(x))
#define fo0(i,k) for(i=fr[k];i;i=nx[i])
#define fo1(i,l,r) for(i=l;i<=r;i++)
#define fo2(i,l,r) for(i=l;i>=r;i--)
#define fo(i,n) for(i=1;i<=n;i++)
#define ret return
#define x first
#define cint const int
#define y second
#define opi(x) freopen(x,"r",stdin)
#define opo(x) freopen(x,"w",stdout)
#define tpl template<class T>
#define priq priority_queue
#define mp make_pair
#define use using namespace
#define WT while(T--)
use std;
typedef pair<int,int> pii;typedef pair<int,ll> pil;typedef pair<ll,int> pli;typedef pair<ll,ll> pll;
namespace io
{
void _(int &k){char c;int e=1;k=0;while((c=gtc)>'9'||c<'0')if(c=='-')e=-1;k=c-'0';while((c=gtc)<='9'&&c>='0'){k*=10;k+=c-'0';}k*=e;}
void _(ll &k){char c;int e=1;k=0;while((c=gtc)>'9'||c<'0')if(c=='-')e=-1;k=c-'0';while((c=gtc)<='9'&&c>='0'){k*=10;k+=c-'0';}k*=e;}
void _(char &c){while((c=gtc)==' '||c=='\n');}void _(dou &c){scanf("%lf",&c);}void _(char *s){char c;while((c=gtc)!=EOF&&c!=' '&&c!=10)*s++=c;}
template<class t1,class t2>void _(t1 &a,t2 &b){_(a);_(b);}template<class t1,class t2,class t3>void _(t1 &a,t2 &b,t3 &c){_(a);_(b);_(c);}
template<class t1,class t2,class t3,class t4>void _(t1 &a,t2 &b,t3 &c,t4 &d){_(a);_(b);_(c);_(d);}
template<class t1,class t2,class t3,class t4,class t5>void _(t1 &a,t2 &b,t3 &c,t4 &d,t5 &e){_(a);_(b);_(c);_(d);_(e);}
void _p(dou k){printf("%.6lf",k);}void _p(char *c){for(;*c;ptc(*c++));}void _p(const char *c){for(;*c;ptc(*c++));}void _p(char c){ptc(c);}
tpl void _p0(T k){if(k>=10)_p0(k/10);ptc(k%10+'0');}tpl void _p(T k){if(k<0){ptc('-');_p0(-k);}else _p0(k);}tpl void __p(T k){_p(k);ptc(' ');}
tpl void _pn(T k){_p(k);ptc('\n');}template<class t1,class t2>void _p(t1 a,t2 b){__p(a);_pn(b);}
template<class t1,class t2,class t3>void _p(t1 a,t2 b,t3 c){__p(a);__p(b);_pn(c);}
template<class t1,class t2,class t3,class t4>void _p(t1 a,t2 b,t3 c,t4 d){__p(a);__p(b);__p(c);_pn(d);}
tpl void op(T *a,int n){int i;n--;fo(i,n)__p(a[i]);_pn(a[n+1]);}int gi(){int x;_(x);ret x;}ll gll(){ll x;_(x);ret x;}
}
int gcd(int a,int b){ret b?gcd(b,a%b):a;}void fcl(){fclose(stdin);fclose(stdout);}
void fop(const char *s){char c[256],d[256];cl(c,0);cl(d,0);strcpy(c,s);strcpy(d,s);opi(strcat(c,".in"));opo(strcat(d,".out"));}
int eq(dou a,dou b){return a+eps>=b&&b+eps>=a;}tpl void _ma(T &a,T b){if(a<b)a=b;}tpl void _mi(T &a,T b){if(a>b)a=b;}
cint N=1234567,EE=100000000,GG=1000000000,ima=2147483647;
use io;
int n,m,a[N],f[N],T;
char an[666][666];
void ck(int k,int x,int y,int b)
{
if(k==0)
an[x][y]='A'+b;
else
{
ck(k-1,x,y,b);
ck(k-1,x+(1<<(k-1)),y,b);
ck(k-1,x,y+(1<<(k-1)),b);
ck(k-1,x+(1<<(k-1)),y+(1<<(k-1)),b^1);
}
}
int main()
{
int i,j,a1,a2;
_(n);
ck(n,0,0,0);
_pn((1<<n)-1);
fo(i,(1<<n)-1)
puts(an[i]);
}
|
#include<bits/stdc++.h>
using namespace std;
const int mod=3;
const int N=400010;
int f[10][10];
char s[N];
int lucas(int n,int m){
if(n<mod && m<mod) return f[n][m];
else return f[n%mod][m%mod]*lucas(n/mod,m/mod);
}
int getx(char s){
if(s=='R') return 1;
if(s=='W') return 2;
else return 0;
}
int main(){
f[0][0]=f[1][0]=f[2][0]=f[2][2]=f[1][1]=1;
f[2][1]=2;
int n;
scanf("%d",&n);
scanf("%s",&s);
n--;
int ans=0;
for(int i=0;i<=n;i++) ans+=getx(s[i])*lucas(n,i);
ans%=mod;
if(n&1) ans=ans*2%mod;
if(ans==1) puts("R");
else if(ans==2) puts("W");
else puts("B");
return 0;
}
| #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>;
int main(){
int n;
cin>>n;
vector<ll>dp(2,1);
rep(i,n){
string s;
cin>>s;
vector<ll>p(2); swap(dp,p);
rep(j,2)rep(x,2){
int nj = j;
if( s == "AND")nj &= x;
else nj |= x;
dp[nj] += p[j];
}
}
cout<<dp[1]<<endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long X;
vector<long long> fibo = {0,1};
vector<int> ans = {1};
bitset<100> bs;
int main() {
scanf("%lld", &X);
while (fibo[fibo.size()-2] + fibo[fibo.size()-1] <= X) fibo.push_back(fibo[fibo.size()-2] + fibo[fibo.size()-1]);
int K = fibo.size();
X -= fibo.back();
while (X) {
while (fibo.back() > X) fibo.pop_back();
bs[fibo.size()] = true;
X -= fibo.back();
}
for (int i = 1; i < K-1; ++i) {
ans.push_back(3+i%2);
if (bs[K-i]) ans.push_back(1+i%2);
}
printf("%d\n", ans.size());
if (K % 2 == 0) {
for (int x: ans) printf("%d\n", x);
}
else {
for (int x: ans) {
if (x <= 2) printf("%d\n", 3-x);
else printf("%d\n", 7-x);
}
}
return 0;
}
| //#define _GLIBCXX_DEBUG
//#include "atcoder/all"
//using namespace atcoder;
#include <bits/stdc++.h>
#define int long long
#define ll long long
using ull = unsigned long long;
using namespace std;
#define Dump(x) \
if (dbg) { \
cerr << #x << " = " << (x) << endl; \
}
#define overload4(_1, _2, _3, _4, name, ...) name
#define FOR1(n) for (ll i = 0; i < (n); ++i)
#define FOR2(i, n) for (ll i = 0; i < (n); ++i)
#define FOR3(i, a, b) for (ll i = (a); i < (b); ++i)
#define FOR4(i, a, b, c) for (ll i = (a); i < (b); i += (c))
#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FORR(i, a, b) for (int i = (a); i <= (b); ++i)
#define bit(n, k) (((n) >> (k)) & 1) /*nのk bit目*/
namespace mydef {
const int INF = 1ll << 60;
const int MOD = 1e9 + 7;
template <class T>
bool chmin(T& a, const T& b) {
if (a > b) {
a = b;
return 1;
} else
return 0;
}
template <class T>
bool chmax(T& a, const T& b) {
if (a < b) {
a = b;
return 1;
} else
return 0;
}
void Yes(bool flag = true) {
if (flag)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
void No(bool flag = true) {
Yes(!flag);
}
void YES(bool flag = true) {
if (flag)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
void NO(bool flag = true) {
YES(!flag);
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T& val) {
std::fill((T*)array, (T*)(array + N), val);
}
bool dbg = true;
} // namespace mydef
using namespace mydef;
#define pb push_back
//#define mp make_pair
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(), (v).end()
#define SZ(x) ((int)(x).size())
#define vi vector<int>
#define vvi vector<vector<int>>
#define vp vector<pair<int, int>>
#define vvp vector<vector<pair<int, int>>>
#define pi pair<int, int>
//#define P pair<int, int>
//#define V vector<int>
//#define S set<int>
#define asn ans
int N;
void solve() {
vector<int> fib(2);
fib[0] = fib[1] = 1;
for (int i = 2;; i++) {
fib.emplace_back(fib[i - 1] + fib[i - 2]);
if (fib.back() > N)
break;
}
int M = fib.size();
vp memo;
memo.emplace_back(0, 0);
memo.emplace_back(1, 0);
while (true) {
auto [x, y] = memo.back();
if (x + y > N)
break;
if (x > y)
memo.emplace_back(x, x + y);
else
memo.emplace_back(x + y, y);
}
if (memo.back().first < memo.back().second) {
for (auto&& [x, y] : memo) {
swap(x, y);
}
}
/*
for (auto&& [x, y] : memo) {
cerr << x << " " << y << endl;
}
*/
N -= memo.back().first;
int inc[2020] = {};
while (N) {
int idx = upper_bound(all(fib), N) - fib.begin() - 1;
inc[idx]++;
N -= fib[idx];
}
vector<int> ans;
for (int i = 0; i < M - 1; i++) {
auto [x_now, y_now] = memo[i];
auto [x_nxt, y_nxt] = memo[i + 1];
if (x_now != x_nxt) {
if (i == 0)
//cout << 1 << endl;
ans.emplace_back(1);
else
//cout << 3 << endl;
ans.emplace_back(3);
} else {
if (i == 0)
//cout << 2 << endl;
ans.emplace_back(2);
else
//cout << 4 << endl;
ans.emplace_back(4);
}
while (inc[M - 2 - i]) {
inc[M - i - 2]--;
//cout << ((M - i - 1) % 2 == 0 ? 2 : 1) << endl;
ans.emplace_back(((M - i - 1) % 2 == 0 ? 2 : 1));
}
}
cout << ans.size() << endl;
for (auto&& e : ans) {
cout << e << endl;
}
}
signed main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cin >> N;
solve();
return 0;
}
|
#include<bits/stdc++.h>
#define ll long long int
#define LLI long long int
#define pii pair<ll,ll>
#define F first
#define S second
#define mod 1000000007
using namespace std;
int main(){
ios_base::sync_with_stdio(false); cin.tie(NULL);
int ans=0; int n; cin>>n; vector<pii> arr(n);
for(int i=0;i<n;i++){
int x,y; cin>>x>>y; arr[i]={x,y};
}
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(abs(arr[i].F-arr[j].F)>=abs(arr[i].S-arr[j].S)) ans++;
}
}
cout<< ans <<endl;
} | #include<bits/stdc++.h>
using namespace std;
#ifndef ONLINE_JUDGE
#define dbg(x...) do { cout << "\033[32;1m " << #x << " -> "; err(x); } while (0)
void err() { cout << "\033[39;0m" << endl; }
template<template<typename...> class T, typename t, typename... A>
void err(T<t> a, A... x) { for (auto v: a) cout << v << ' '; err(x...); }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }
#else
#define dbg(...)
#endif
typedef long long ll;
typedef pair<int,int> pi;
typedef vector<int> vi;
template<class T> using vc=vector<T>;
template<class T> using vvc=vc<vc<T>>;
template<class T> void mkuni(vector<T>&v)
{
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
}
ll rand_int(ll l, ll r) //[l, r]
{
static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
return uniform_int_distribution<ll>(l, r)(gen);
}
template<class T>
void print(T x,int suc=1)
{
cout<<x;
if(suc==1) cout<<'\n';
else cout<<' ';
}
template<class T>
void print(const vector<T>&v,int suc=1)
{
for(int i=0;i<v.size();i++)
print(v[i],i==(int)(v.size())-1?suc:2);
}
const int maxn=2e5+7;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin>>n;
vc<ll> a(2*n);
ll sum=0;
for(int i=0;i<2*n;i++)
{
cin>>a[i];
sum+=a[i];
}
set<pi> mid;
ll ans=0;
for(int l=n-1,r=n;l>=0;l--,r++)
{
mid.insert(pi(a[l],l));
mid.insert(pi(a[r],r));
ans+=(*mid.begin()).first;
mid.erase(mid.begin());
}
print(sum-ans);
}
|
#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;
typedef pair<int,int> P;
const int MAX=2000005;
const int MOD=1000000007;
long long fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void COMinit() {
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for (int i = 2; i < MAX; i++){
fac[i] = fac[i - 1] * i % MOD;
inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
finv[i] = finv[i - 1] * inv[i] % MOD;
}
}
// 二項係数計算
long long COM(int n, int k){
if (n < k) return 0;
if (n < 0 || k < 0) return 0;
return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
int main(){
int n,m,k;
cin>>n>>m>>k;
if(n>m+k){
cout<<0<<endl;
return 0;
}
COMinit();
ll ans=COM(n+m,n)-COM(n+m,m+k+1);
if(ans<0) ans+=MOD;
cout<<ans<<endl;
} | #include <iostream>
using namespace std;
int main() {
int A, B, W;
cin >> A >> B >> W;
W *= 1000;
int m = 1000003;
int M = -1;
for (int i=1; i<=1000000;i++) {
if (A * i <= W && B * i >= W) {
m = min(m, i);
M = max(M, i);}
}
if (m == 1000003 && M == -1) cout << "UNSATISFIABLE" << endl;
else cout << m << ' ' << M << endl;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
int n;
ll a[N], b[N], c[N];
int main() {
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%lld", &a[i]);
}
ll mx = 0, mx2 = 0;
for (int i = 0; i < n; ++i) {
mx = max(mx, a[i]);
scanf("%lld", &b[i]);
c[i] = max(mx * b[i], mx2);
mx2 = max(mx2, c[i]);
}
for (int i = 0; i < n; ++i) {
printf("%lld\n", max(c[i], a[i] * b[i]));
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<ll(n);i++)
#define REP(i,k,n) for(ll i=k;i<ll(n);i++)
#define all(a) a.begin(),a.end()
#define eb emplace_back
#define lb(v,k) (lower_bound(all(v),k)-v.begin())
#define ub(v,k) (upper_bound(all(v),k)-v.begin())
#define fi first
#define se second
#define PQ(T) priority_queue<T>
#define SPQ(T) priority_queue<T,vector<T>,greater<T>>
#define UNIQUE(a) sort(all(a));a.erase(unique(all(a)),a.end())
#define decimal cout<<fixed<<setprecision(10)
#define summon_tourist cin.tie(0);ios::sync_with_stdio(false)
using ll=long long;
using P=pair<ll,ll>;
using vi=vector<ll>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
constexpr ll inf=1001001001001001;
constexpr int INF=1001001001;
constexpr int mod=1000000007;
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;}
template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';}
template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);}
void YesNo(bool b){if(b)out("Yes");else out("No");}
void yesno(bool b){if(b)out("yes");else out("no");}
void YESNO(bool b){if(b)out("YES");else out("NO");}
ll modpow(ll a,ll b){ll c=1;while(b>0){if(b&1){c=a*c%mod;}a=a*a%mod;b>>=1;}return c;}
vi calc(ll x){vi v;while(x>0){v.eb(x%10);x/=10;}reverse(all(v));return v;}
int main(){
ll n; cin>>n;
vi a(n),p(n),x(n);
rep(i,n) cin>>a[i]>>p[i]>>x[i];
ll ans=inf;
rep(i,n){
if(a[i]<x[i]) chmin(ans,p[i]);
}
if(ans==inf) ans=-1;
out(ans);
}
|
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main(){
string a;
int i,j,c=0,f=0;
cin>>a;
for(i=0;i<a.length();i++){
if(isupper(a[i])){
f++;
}
}
if(f==3){
for(i=0;i<a.length();i++){
if((i+1)<a.length()){
if(a[i]!=a[i+1]){
c++;
}
}
}
if(c>0){cout<<"Lost"<<endl;}
else{cout<<"Won"<<endl;}
}
else{
cout<<"Lost"<<endl;
}
return 0;
} | #include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <math.h>
#include <algorithm>
#include <numeric>
using namespace std;
int main() {
long long N;
cin >> N;
vector<int> v;
int i;
for (i = 0; N > 0; i++) {
v.push_back(N % 10);
N /= 10;
}
if (accumulate(v.begin(), v.end(), 0) % 3 == 0) {
cout << 0;
return 0;
}
for (int k = 1; k < i; ++k) {
for (int j = 0; j + k - 1 < i; ++j) {
if ((accumulate(v.begin(), v.end(), 0)-accumulate(v.begin() + j, v.begin() + j + k, 0) )% 3 == 0) {
cout << k;
return 0;
}
}
}
cout << -1;
return 0;
}
|
//#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
#define rep2(x,fr,to) for(int x=(fr);x<(to);x++)
#define rep(x,to) for(int x=0;x<(to);x++)
#define repr(x,fr,to) for(int x=(fr);x>=(to);x--)
#define all(c) c.begin(),c.end()
#define sz(v) (int)v.size()
typedef long long ll; typedef vector<int> VI; typedef pair<int,int> pii; typedef vector<ll> VL; const int MD = 1e9+7; //998244353;
void dbg(){cerr<<"\n";} template <class F,class ...S> void dbg(const F& f, const S&...s){cerr <<f <<": "; dbg(s...);}
//using mint = modint1000000007;
vector<int> tpl_sort(vector<vector<int>>& r){
int n= r.size();
vector<int> rt, cnt(n); rt.reserve(n+2);
for(auto& xl: r) for(auto x: xl) cnt[x]++;
queue<int> qu;
for(int i=0; i<n; i++) if(cnt[i]==0) qu.emplace(i);
while(!qu.empty()){
auto cr = qu.front(); qu.pop();
rt.emplace_back(cr);
for(auto x: r[cr]) if(--cnt[x] == 0) qu.emplace(x);
}
return rt;
}
int main()
{
cin.tie(0); ios_base::sync_with_stdio(false);
int n;
cin >>n ;
vector<string> s(n);
rep(i, n) cin >>s[i];
vector<VI> r(n);
vector<set<int>> tc(n);
rep(i, n) rep(j,n) if(s[i][j]=='1') r[i].push_back(j);
VI ct(n);
auto rec=[&](auto rec, int c, const int p)->void{
for(auto x: r[c]) if(!tc[p].count(x)){
if(x==p) continue;
tc[p].insert(x);
rec(rec, x, p);
}
};
rep(i, n) rec(rec, i, i);
rep(i, n){
//dbg(i,"::");for(auto x :tc[i]) cerr<<x<<" "; dbg();
for(auto x :tc[i]) ct[x]++;
}
//for(auto x :ct) cerr<<x<<" "; dbg();
double ans =0;
rep(i, n) ans += 1.0 / (ct[i]+1);
printf("%.16f\n", ans);
return 0;
}
| #include <algorithm>
#include <array>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <vector>
using namespace std;
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#ifdef NEAL_DEBUG
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
int main() {
cout << setprecision(16);
int N;
cin >> N;
vector<string> S(N);
for (auto &s : S)
cin >> s;
for (int k = 0; k < N; k++)
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
if (S[i][k] == '1' && S[k][j] == '1')
S[i][j] = '1';
double answer = 0;
for (int i = 0; i < N; i++) {
int other = 0;
for (int j = 0; j < N; j++)
other += i != j && S[j][i] == '1';
answer += 1.0 / (other + 1);
}
cout << answer << '\n';
}
|
#include <stdio.h>
int count[10];
int main()
{
long long int a;
scanf("%lld",&a);
int sum = 0,digit = 0;
while(a)
{
sum += (a%10);
digit++;
count[(a%10)%3]++;
a/=10;
}
if(sum%3==0) printf("0");
else if(sum%3==1)
{
if(digit>=2&&count[1]>=1) printf("1");
else if(digit>=3&&count[2]>=2) printf("2");
else printf("-1");
}
else
{
if(digit>=2&&count[2]>=1) printf("1");
else if(digit>=3&&count[1]>=2) printf("2");
else printf("-1");
}
} | #include <bits/stdc++.h>
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef long long LL;
typedef pair<int,int> PI;
typedef pair<LL,LL> PLL;
typedef unsigned long long ULL;
typedef pair<double,double> PD;
#define FOR(x, b, e) for(int x = b; x<= (e); x++)
#define FORD(x, b, e) for(int x = b; x>= (e); x--)
#define REP(x, n) for(int x = 0; x<(n); ++x)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(x) ((int)(x).size())
#define PB push_back
#define IN insert
#define ST first
#define ND second
#define INF 2000000011
#define MOD 1000000007
#define MAXS 100010
int main(){
ios::sync_with_stdio(0); cin.tie(0);
LL n;
cin>>n;
int sum=0;
int rem1=0;
int rem2=0;
int lg=0;
while(n){
lg++;
int t1=(n%10)%3;
sum+=t1;
sum%=3;
if(t1==1)
rem1+=1;
if(t1==2)
rem2+=2;
n/=10;
}
if(sum==0)
cout<<0;
else{
if(lg>1&&((sum==1&&rem1>=1)||(sum==2&&rem2>=1)))
cout<<1;
else if(lg>2&&((sum==2&&rem1>=2)||(sum==1&&rem2>=2)))
cout<<2;
else
cout<<-1;
}
}
|
#include<bits/stdc++.h>
typedef long long ll;
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define nep(i,r,l) for(int i=r;i>=l;i--)
void re(int &x){scanf("%d",&x);}
void re(int &x,int &y){scanf("%d %d",&x,&y);}
void re(ll &x){scanf("%lld",&x);}
void re(ll &x,ll &y){scanf("%lld %lld",&x,&y);}
void re(double &x){scanf("%lf",&x);}
void re(double &x,int &y){scanf("%lf %lf",&x,&y);}
using namespace std;
const int N=5e5+5;
const int inf=0x3f3f3f3f;
int a[N];
int sum[N];
map<ll,int> mp;
int main()
{
int n;cin>>n;
rep(i,0,n-1) cin>>a[i];
ll ans=0;
ll sum=0;
mp[0]=1;
rep(i,0,n-1)
{
if(i%2==0)
{
sum+=a[i];
}
else sum-=a[i];
ans+=mp[sum];
mp[sum]++;
}
cout<<ans<<endl;
return 0;
}
// 5 2 3 4 1
| #include<bits/stdc++.h>
using namespace std;
#define inf 1000000000
#define ll long long
#define rep(i,x,y) for(int i=x; i<y; i++)
int n,m;
vector<vector<int>> a;
bool check(int x){
int b[n][n];
//memset(b,0,sizeof b);
//cout<<x<<'\n';
rep(i,0,n){
rep(j,0,n){
b[i][j]=(a[i][j]<=x);
if(i>0)b[i][j]+=b[i-1][j];
if(j>0)b[i][j]+=b[i][j-1];
if(i>0 && j>0)b[i][j]-=b[i-1][j-1];
//cout<<b[i][j]<<' ';
}
//cout<<'\n';
}
//bool ans=0;
rep(i,m-1,n){
rep(j,m-1,n){
int sum=b[i][j];
if(j-m>=0)sum-=b[i][j-m];
if(i-m>=0)sum-=b[i-m][j];
if(i-m>=0 && j-m>=0)sum+=b[i-m][j-m];
if(sum>=(m*m+1)/2){return true;}
//cout<<sum<<' ';
}
//cout<<'\n';
}
return false;
}
signed main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
cin>>n>>m;
a.resize(n,vector<int>(n));
set<int> s;
rep(i,0,n){
rep(j,0,n){cin>>a[i][j];s.insert(a[i][j]);}
}
vector<int> v(s.begin(),s.end());
int ans;
int l=0,h=v.size()-1;
// check(0);
// rep(i,0,h+1){
// cout<<check(i)<<' ';
// }
// cout<<'\n';
while(l<=h){
int mid=(l+h)/2;
if(check(v[mid])){
ans=v[mid];
h=mid-1;
}
else {
l=mid+1;
}
}
cout<<ans;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
int M=1000000007;
const int N=200005;
signed main(){
int t1=1;
//cin>>t1;
while(t1--){
int n;
cin>>n;
int a[1005]{0};
for(int i=0;i<n;i++) {
int x;
cin>>x;
a[x]++;
}
for(int i=0;i<n;i++){
int x;
cin>>x;
a[x+1]--;
}
for(int i=1;i<1005;i++) a[i]+=a[i-1];
int cnt=0;
for(int i=1;i<=1000;i++) if(a[i]==n) cnt++;
cout<<cnt<<endl;
}
}
| #include<fstream>
#include<iostream>
#include<sstream>
#include<vector>
#include<stack>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<queue>
#include<tuple>
#include<iomanip>
#include<bitset>
#include<stdio.h>
#include<set>
#include<array>
using namespace std;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using graph = vector<vector<long long>>;
const long long MOD = 1000000007;
const long long INF = 1e18;
using ll = long long;
using P = pair<int,int>;
void chmin(int &x,int y){ x = min(x,y); }
int digit(ll x){
int count = 1;
while(x/10>0){
count ++;
x /= 10;
}
return count;
}
bool prime(ll x){
if(x==1) return false;
for(ll i=2;i*i<=x;i++){
if(x%i==0) return false;
}
return true;
}
void decom(ll x,map<ll,int> &hatano){
for(ll i=2;i*i<=x;i++){
if(x%i==0&&prime(i)){
while(x%i==0){
x /= i;
hatano[i] ++;
}
}
}
if(x!=1) hatano[x] ++;
}
ll speedpow(ll x,ll y){
ll res = 1;
ll test = x;
for(int i=0;i<31;i++){
if(y&(1<<i)) res *= test;
res = res%MOD;
test = (test*test)%MOD;
}
return res;
}
ll combination(ll x,ll y){
ll test = 1;
ll test1 = 1;
for(ll i=0;i<y;i++){
test = (test*(x-i))%MOD;
test1 = (test1*(i+1LL))%MOD;
}
return (test * speedpow(test1,MOD-2))%MOD ;
}
int euclid(int x,int y){
if(y==0) return x;
return euclid(y,x%y);
}
/*int main(){
int n;cin >> n;
ll m;cin >> m;
ll a[n];
rep(i,n) cin >> a[i];
}*/
int main(){
int n;cin >> n;
int a[n],b[n];
rep(i,n) cin >> a[i];
rep(i,n) cin >> b[i];
int ans = 0;
for(int i=1;i<=1000;i++){
bool kon = true;
rep(j,n){
if(a[j]>i||b[j]<i) kon = false;
}
if(kon) ans ++;
}
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int r1, c1, r2, c2;
cin >> r1 >> c1 >> r2 >> c2;
if (r1 == r2 && c1 == c2) cout << 0 << endl;
else if (r1 + c1 == r2 + c2 || r1 - c1 == r2 - c2 ||
abs(r1 - r2) + abs(c1 - c2) <= 3) {
cout << 1 << endl;
}
else if ((r1 + c1) % 2 == (r2 + c2) % 2 ||
abs((r2 - r1) + (c2 - c1)) <= 3 ||
abs((r2 - r1) - (c2 - c1)) <= 3) {
cout << 2 << endl;
}
else cout << 3 << endl;
} | #include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <cmath>
#include <ctime>
#include <bitset>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define fi first
#define se second
#define il inline
#define Ld double
#define pc putchar
#define rg register
#define ll long long
#define lll __int128
#define pb push_back
#define mp make_pair
#define It ::iterator
#define W(p) while(p--)
#define pll pair<ll, ll>
#define pli pair<ll, int>
#define pil pair<int, ll>
#define pii pair<int, int>
#define ull unsigned long long
#define all(x) x.begin(), x.end()
#define def(N, K, b) const K N = b;
#define FOR(i, b, s) for(int i = b; i < s; ++i)
#define rep(i, b, s) for(int i = b; i <= s; ++i)
#define per(i, b, s) for(int i = b; i >= s; --i)
#define FOR_(i, b, s) for(int i = b; i > s; --i)
#define ios ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
namespace IO {
const int SIZE = (1 << 20) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE],*oS = obuf, *oT = obuf + SIZE - 1;
char _st[55];
int _qr = 0;
inline char gc() {
return (iS == iT ? iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++) : *iS++);
}
inline void qread() {}
template<class T1, class ...T2>
inline void qread(T1 &IEE, T2&... ls) {
register T1 __ = 0, ___ = 1;
register char ch;
while(!isdigit(ch = gc())) ___ = (ch == '-') ? -___ : ___;
do {
__ = (__ << 1) + (__ << 3) + (ch ^ 48);
}while(isdigit(ch = gc()));
__ *= ___;
IEE = __;
qread(ls...);
return ;
}
template<class T>
inline void QreadArr(T Begin, T End) {
while(Begin != End) {
qread(*Begin);
++Begin;
}
}
inline void flush() {
fwrite(obuf, 1, oS - obuf, stdout);
oS = obuf;
return ;
}
inline void putc_(char _x) {
*oS++ = _x;
if(oS == oT) flush();
}
inline void qwrite() {}
template<class T1, class ...T2>
inline void qwrite(T1 IEE, T2... ls) {
if(!IEE) putc_('0');
if(IEE < 0) putc_('-'), IEE = -IEE;
while(IEE) _st[++_qr] = IEE % 10 + '0', IEE /= 10;
while(_qr) putc_(_st[_qr--]);
qwrite(ls...);
return ;
}
template<class T>
inline void WriteArr(T Begin, T End) {
const char Kg = ' ', Edl = '\n';
while(Begin != End) {
qwrite(*Begin);
++Begin;
putc_(Kg);
}
putc_(Edl);
}
struct Flusher_{ ~Flusher_() { flush(); } } io_flusher;
}
using namespace IO;
using namespace std;
template<class T1, class T2>
T1 Min(const T1 x, const T2 y) { return x > y ? y : x; }
template<class T1, class T2>
T1 Max(const T1 x, const T2 y) { return x > y ? x : y; }
template<class T1, class T2>
void To_max(T1 &x, const T2 y) { x < y ? x = y : x = x; }
template<class T1, class T2>
void To_min(T1 &x, const T2 y) { x > y ? x = y : x = x; }
il ll qpow(ll n, ll base, ll mod = 1e18) {
ll ret = 1;
while(n) {
if(n & 1) ret = ret * base % mod;
base = base * base % mod;
n >>= 1;
}
return ret % mod;
}
il ll gcd(ll x, ll y) { return !y ? x : gcd(y, x % y); }
il ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
//header
int flg;
int main() {
int n, s, d;
qread(n, s, d);
rep(i, 1, n) {
int x, y; qread(x, y);
if(x < s && y > d) flg = 1;
}
flg ? puts("Yes") : puts("No");
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int n;
int a[N];
int pos[N];
bool used[N];
int solve() {
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
pos[--a[i]] = i;
used[i] = false;
}
vector<int> ans;
for (int i = n - 1; i > 0; --i) {
while (pos[i] != i) {
if (used[pos[i]]) return 0 * printf("-1\n");
int p = pos[i];
swap(pos[i], pos[a[p + 1]]);
swap(a[p], a[p + 1]);
ans.push_back(p);
used[p] = true;
}
}
if (ans.size() != n - 1) return 0 * printf("-1\n");
for (int u : ans) printf("%d\n", u + 1);
return 0;
}
int main() {
int t = 1;
// scanf("%d", &t);
for (int tc = 0; tc < t; ++tc) {
// printf("Case #%d: ", tc+1);
solve();
}
return 0;
}
| #include <iostream>
#include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define ll long long
#define forn(i,a,n,b) for(ll i=a;i<n;i+=b)
#define forr(i,a,n,b) for(ll i=a;i>=n;i-=b)
#define MOD 1000000007
#define vll vector<ll>
using namespace std;
int main()
{
fastio;
ll t = 1;
// cin>>t;
while(t-->0)
{
ll n, takahoshi = 0, ans = 0;
cin>>n;
vector<pair<ll, pair<ll, ll>>> pairs;
forn(i,0,n,1)
{
ll a, b;
cin>>a>>b;
pairs.emplace_back(2*a+b, make_pair(a,b));
takahoshi -= a;
}
sort(pairs.begin(), pairs.end());
forr(i,n-1,0,1)
{
takahoshi += pairs[i].first;
ans++;
if (takahoshi > 0)
{
break;
}
}
cout<<ans<<"\n";
}
return 0;
}
|
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
using namespace std;
using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;
template<class T>
inline bool chmax(T& x, T y){
if(x < y){
x = y;
return true;
}
return false;
}
template<class T>
inline bool chmin(T& x, T y){
if(x > y){
x = y;
return true;
}
return false;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int L;
cin >> L;
vector<vector<ll>> comb(L + 1, vector<ll>(L + 1));
for(int i = 0; i <= L; ++i){
comb[i][0] = comb[i][i] = 1;
}
for(int i = 1; i <= L; ++i){
for(int j = 1; j < i; ++j){
comb[i][j] = comb[i - 1][j] + comb[i - 1][j - 1];
}
}
cout << comb[L - 1][11] << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(0)
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define REP(n) FOR(O, 1, (n))
#define pb push_back
#define f first
#define s second
typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<ll> vl;
typedef vector<piii> viii;
const int MAXN = 1000100, MAXK = 30;
//const ll MOD = 998244353;
const ll INF = 1e13;
const ld PI = asin(1) * 2;
const ld EPS = 1e-6;
unordered_set<int> pos, tmp;
int n, m;
map<int, vi> black;
int main()
{
FAST_IO;
cin >> n >> m;
pos.insert(n);
REP(m) {
int x, y;
cin >> x >> y;
black[x].pb(y);
}
for (auto p : black) {
vi ins, del;
for (int j : p.s) {
bool ok = false;
if (pos.count(j-1) || pos.count(j+1))
ok = true;
if (ok) {
//pos.insert(j);
ins.pb(j);
//cout << " inserting j=" << j << endl;
} else {
//pos.erase(j);
del.pb(j);
//cout << " erasing j=" << j << endl;
}
}
for (int x : ins) pos.insert(x);
for (int x : del) pos.erase(x);
/*cout << " after x = " << p.f << ", pos: ";
for (int x : pos) cout << x << " ";
cout << endl;*/
}
cout << (int)pos.size() << "\n";
return 0;
}
|
#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <string>
#include <cctype>
#include <map>
#include <queue>
#include <limits>
#include <iomanip>
#include <sstream>
#include <cstdlib>
#include <locale>
#include <bitset>
#include <set>
using namespace std;
const int mod = 1000000007;
#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))
#define rep(i,n) for (int i = 0; i < (n); i++)
#define repd(i,n) for(int i = n-1; i >= 0; i--)
#define ll long long
#define P pair<int, int>
#define INF32 2147483647 //2.147483647×10^{9}:32bit整数のinf
#define INF64 9223372036854775807 //9.223372036854775807×10^{18}:64bit整数のinf
// debug methods
// usage: debug(x,y);
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0,a1,a2,a3,a4,x,...) x
#define debug_1(x1) cout<<#x1<<": "<<x1<<endl
#define debug_2(x1,x2) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<endl
#define debug_3(x1,x2,x3) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<endl
#define debug_4(x1,x2,x3,x4) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<endl
#define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<", "#x5<<": "<<x5<<endl
#ifdef _DEBUG
#define debug(...) CHOOSE((__VA_ARGS__,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__)
#else
#define debug(...)
#endif
const double PI = acos(-1);
unsigned GetDigit(unsigned num) { return log10(num)+1; }
int main()
{
int n;
cin >> n;
vector<string> s(n);
set<string> stringSet;
rep(i, n)
{
cin >> s[i];
stringSet.insert(s[i]);
}
for (int i = 0; i < n; i++)
{
if(stringSet.count("!" + s[i]) > 0)
{
cout << s[i] << endl;
return 0;
}
}
cout << "satisfiable" << endl;
return 0;
} | #include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < (int)(n); i++)
#define ALL(a) a.begin(), a.end()
#define MOD 1000000007
using namespace std;
using ll = long long;
int main() {
int N; cin >> N;
vector<ll> A(N), B(N), C(N), D(N), E(N);
REP(i, 0, N) cin >> A[i] >> B[i] >> C[i] >> D[i] >> E[i];
ll l = 0, h = 1e9 + 1, m;
vector<int> bit(N);
while (h - l > 1) {
m = l + (h - l) / 2;
REP(i, 0, N) {
bit[i] = 0;
if (m <= A[i]) bit[i] |= (1 << 4);
if (m <= B[i]) bit[i] |= (1 << 3);
if (m <= C[i]) bit[i] |= (1 << 2);
if (m <= D[i]) bit[i] |= (1 << 1);
if (m <= E[i]) bit[i] |= (1 << 0);
}
// vector<vector<bool>> dp(N + 1, vector<bool>(1 << 5, false));
// dp[0][0] = true;
// REP(i, 0, N) {
// REP(j, 0, 1 << 5) {
// if (!dp[i][j]) continue;
// dp[i + 1][j] = true;
// dp[i + 1][j | bit[i]] = true;
// }
// }
vector<vector<vector<bool>>> dp(N + 1, vector<vector<bool>>(1 << 5, vector<bool>(4, false)));
dp[0][0][0] = true;
REP(i, 0, N) {
REP(j, 0, 1 << 5) {
REP(k, 0, 4) {
if (!dp[i][j][k]) continue;
dp[i + 1][j][k] = true;
if (k < 3) dp[i + 1][j | bit[i]][k + 1] = true;
}
}
}
if (dp[N][(1 << 5) - 1][3]) l = m;
else h = m;
}
cout << l << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a, b, c;
cin >> a >> b >> c;
cout << std::max((a + b), max((b + c), (c + a)));
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 all(s) (s).begin(),(s).end()
#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;
}
/*======================================================================*/
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}};
void solve()
{
int a,b,c;
cin>>a>>b>>c;
cout<<a+b+c-min(a,min(b,c))<<endl;
}
/*======================================================================*/
signed main()
{
std::ios::sync_with_stdio(false);
/*
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
*/
/*====================================================================*/
int t=1;
//cin>>t;
while(t--)
{
solve();
}
return 0;
}
/*
注意数组越界,vector长度为0的时候循环会RE
注意输入顺序
注意循环时int*int爆ll
注意答案存爆ll
*/
|
/*NEVER GIVE UP*/
/*First try brute force and check if it is in the range of time complexity*/
/*Then try to solve the problem in mathematical form*/
/*Then try to imagine how the final ans will look like*/
/*Write on paper if still not able to get*/
/*Try to think diff. algorithms and data structures that could solve the problem*/
#include <bits/stdc++.h>
using namespace std;
int main()
{long long n=0,max=0,max1=0,temp=0,temp1=0;
cin>>n;
long long a[1<<n];
for(long long j=0;j<(1<<n);j++)
cin>>a[j];
for(long long j=0;j<(1<<n)/2;j++)
{
if(a[j]>max)
{
max=a[j];
temp=j;
}
}
for(long long j=(1<<n)/2;j<(1<<n);j++)
{
if(a[j]>max1)
{
max1=a[j];
temp1=j;
}
}
if(max<max1)
cout<<temp+1;
else
cout<<temp1+1;
} | #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define srep(i,s,t) for(int i = s; i < t; ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
#define yn {puts("Yes");}else{puts("No");}
#define MAX_N 200005
int GCD(int x, int y){
if(x == 0 && y == 0) return 0;
if(x>y){
int swap = x;
x = y;
y = swap;
}
while(true){
if(y%x==0){
return x;
}else{
int amari = y%x;
y = x;
x = amari;
}
}
}
vector<ll> yakusuu_rekkyo(ll n){
vector<ll> divisors;
for(ll i = 1; i <= n; i++){
if(i * i > n)break;
if(i * i == n){
divisors.push_back(i);
break;
}else if(n % i == 0){
divisors.push_back(i);
divisors.push_back(n/i);
}
}
sort(divisors.begin(), divisors.end());
return divisors;
}
int main() {
int n; cin >> n;
int a[n] = {};
rep(i,n) cin >> a[i];
sort(a,a+n);
set<int> se;
rep(i,n){
vector<ll> v = yakusuu_rekkyo(a[i]);
rep(j,v.size()) se.insert(v[j]);
}
int ans = 0;
for(auto x: se){
if(x > a[0]) continue;
int now = -1;
rep(i,n){
if(a[i] % x == 0){
if(now == -1){
now = a[i];
}else{
now = GCD(now, a[i]);
}
}
}
if(now == x) ans++;
}
cout << ans << endl;
return 0;
}
|
#include <iostream>
using namespace std;
#include <algorithm>
#include <iomanip>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#include <math.h>
int main() {
long long int n;
cin >> n;
vector< long long int > a;
for ( long long int i = 0; i < n; i++ ) {
long long int in;
cin >> in;
a.push_back( in );
}
long long int ma = 0;
long long int cnt = 0;
for ( long long int i = 0; i < n; i++ ) {
long long int in;
cin >> in;
ma = max( ma, a[i] );
cnt = max( cnt, ma * in );
cout << cnt << endl;
}
return 0;
}
| #include<iostream>
#include<array>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
#include<cassert>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
template<class T>bool chmax(T &a, const T &b) {if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a, const T &b) {if(b<a){a=b;return 1;}return 0;}
int n;
ll a[200010],b[200010];
void solve(){
cin >> n;
rep(i,n) cin >> a[i];
rep(i,n) cin >> b[i];
ll a_MAX=0,c_MAX=0;
rep(i,n){
chmax(a_MAX,a[i]);
chmax(c_MAX,a_MAX*b[i]);
cout << c_MAX << endl;
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(50);
solve();
} |
#include <bits/stdc++.h>
#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("")
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;
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;
// Unionfind
struct uf{
vi d;
vector<map<int,int> > mp;
uf() {};
uf(int mx) : d(mx,-1), mp(mx) {};
int root(int x){
if(d[x]<0) return x;
return d[x] = root(d[x]);
}
bool unite(int x, int y){
x = root(x); y = root(y);
if(x == y) return false;
if(d[x] > d[y]) swap(x,y);
// データ構造をマージする一般的なテク:小さい集団から大きい集団にマージすることを繰り返すことによって、各要素でみたときに高々logn回マージされる(1回のマージで必ず集団の数が2倍以上なるため)
for(auto p: mp[y]){ // このループの仕方めちゃ便利、連想配列もこのループで回せる
mp[x][p.first] += p.second; // 連想配列もpair型のようにkey,valueを得ることができる
}
mp[y] = map<int,int>(); // 初期化方法
d[x] += d[y]; d[y] = x;
return true;
}
bool same(int x, int y) { return root(x) == root(y);}
int size(int x){ return -d[root(x)];}
int operator[] (int x) {return root(x);}
int operator() (int x) {return size(x);}
};
int main() {
int n,q; cin >> n >> q;
uf dis(n);
rep(i,n){
int c; cin >> c;
dis.mp[i][c] = 1;
}
vi ans;
rep(_,q){
int type; cin >> type;
if(type == 1){
int a,b; cin >> a >> b; --a,--b;
dis.unite(a,b);
}else{
int x,y; cin >> x >> y;
--x;
ans.push_back(dis.mp[dis.root(x)][y]);
}
}
for(int res: ans) printf("%d\n", res);
return 0;
}
| #include <bits/stdc++.h>
#define REP(i, nn) for (int i = 0; i < (int)nn; i++)
#define REPS(i, ss, nn) for (int i = ss; i < (int)nn; i++)
#define REV(i, ss, nn) for (int i = ss; i >= nn; i--)
#define deb(x) std::cout << #x << " " << x << endl;
#define debl(x) std::cout << #x << " " << x << " ";
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
namespace std
{
template <class Fun>
class y_combinator_result
{
Fun fun_;
public:
template <class T>
explicit y_combinator_result(T &&fun) : fun_(std::forward<T>(fun)) {}
template <class... Args>
decltype(auto) operator()(Args &&...args)
{
return fun_(std::ref(*this), std::forward<Args>(args)...);
}
};
template <class Fun>
decltype(auto) y_combinator(Fun &&fun)
{
return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}
};
template <typename T>
bool umax(T &a, T b)
{
bool ret = a < b;
if (ret)
a = b;
return ret;
}
template <typename T>
bool umin(T &a, T b)
{
bool ret = a > b;
if (ret)
a = b;
return ret;
}
struct edge
{
int to;
ll cost;
int from;
edge() { edge(0, 0); }
edge(int to_, ll cost_) : to(to_), cost(cost_) {}
edge(int to_, int from_, ll cost_) : to(to_), cost(cost_), from(from_) {}
};
template <typename... T>
void read(T &...a)
{
((cin >> a), ...);
}
template <typename... T>
void write(T... a)
{
((cout << a), ...);
}
template <typename T>
vector<T> read_array(int sz)
{
vector<T> ret(sz);
for (auto &x : ret)
cin >> x;
return ret;
}
void solve(){
ll a, b, c, d;
read(a, b, c, d);
if(c * d - b <= 0){
write("-1\n");
return;
}
ll k = a / (c * d - b);
if( k * (c * d - b) < a) k++;
write(k, "\n");
}
int main(){
//making data IO Fast
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
/****************************/
int T = 1;
// cin >> T;
while (T--)
solve();
return 0;
}
|
#include <bits/stdc++.h>
#define PI 3.14159265359
#define rep(i,a,n) for(int i=a;i<(int)n;++i)
#define SZ(x) ((int)(x).size()) //size() unsigned -> int
#define descsort(a) sort(a.begin(),a.end(),std::greater<int>())
using namespace std;
typedef long long ll;
const ll INF = 1e9 + 7;
int main(void)
{
int a,b;
cin>>a>>b;
int x=(a+b)/2;
int y=a-x;
cout<<x<<" "<<y<<endl;
return 0;
} | //git pull --rebase origin master
#include<bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(i,n) for(int i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define ll long long
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define ss(s) scanf("%s",s)
#define pi(x) printf("%d\n",x)
#define pl(x) printf("%lld\n",x)
#define ps(s) printf("%s\n",s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define wi(t) int t;cin>>t;while(t--)
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) {
uniform_int_distribution<int> uid(0,lim-1);
return uid(rang);
}
int mpow(int base, int exp);
void ipgraph(int n, int m);
void dfs(int u, int par);
const int mod = 1000000007;
const int N = 3e5, M = N;
//=======================
vi g[N];
int a[N];
ll int gcd(ll int a, ll int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int tc=1;
void solve()
{
int a,b;
cin>>a>>b;
int x=(a+b)/2;
int y=a-x;
cout<<x<<" "<<y;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
srand(chrono::high_resolution_clock::now().time_since_epoch().count());
// wi(t)
{
solve();
}
return 0;
}
int mpow(int base, int exp) {
base %= mod;
int result = 1;
while (exp > 0) {
if (exp & 1) result = ((ll)result * base) % mod;
base = ((ll)base * base) % mod;
exp >>= 1;
}
return result;
}
void ipgraph(int n, int m){
int i, u, v;
while(m--){
cin>>u>>v;
u--, v--;
g[u].pb(v);
g[v].pb(u);
}
}
void dfs(int u, int par){
for(int v:g[u]){
if (v == par) continue;
dfs(v, u);
}
}
|
#include <bits/stdc++.h>
using namespace std;
#define nl "\n"
#define nf endl
#define ll long long
#define pb push_back
#define _ << ' ' <<
#define INF (ll)1e18
#define mod 998244353
#define maxn 500010
ll i, i1, j, k, k1, n, m, res, flag[10], a, b;
string s, t;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
#if !ONLINE_JUDGE && !EVAL
ifstream cin("input.txt");
ofstream cout("output.txt");
#endif
cin >> n >> s >> t; s = '#' + s; t = '#' + t;
for (i = 1; i <= n; i++) {
if (s[i] == '0') a++;
if (t[i] == '0') b++;
if (s[i] == '0' && t[i] == '0' && a == b) res--;
}
if (a != b) res = -1;
else res += a;
cout << res << nl;
return 0;
}
| #pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
#define itn int
#define ll long long
#define yes {puts("Yes");}
#define no {puts("No");}
using namespace std;
ll read(){
ll a=0,b=1;char c=getchar();
while(c>'9'||c<'0'){if(c=='-')b=-1;c=getchar();}
while(c>='0'&&c<='9')a=a*10+c-48,c=getchar();
return a*b;
}
ll n,ans,m,k;
char s[1000005];
char t[1000005];
int a1[500005],ct1,a2[500005],ct2;
int main(){
n=read();
scanf("%s%s",s,t);
for(int i=0;i<n;i++){
if(s[i]=='0'){
a1[++ct1]=i;
}
if(t[i]=='0'){
a2[++ct2]=i;
}
}
if(ct1!=ct2){
puts("-1");
return 0;
}
int ans=0;
for(int i=1;i<=ct1;i++){
if(a1[i]!=a2[i]){
ans++;
}
}
cout<<ans<<'\n';
return 0;
}
|
#include <bits/stdc++.h>
#define ll long long
#define sz(x) (int)(x).size()
using namespace std;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//uniform_int_distribution<int>(1000,10000)(rng)
ll binpow(ll a, ll b)
{
ll res = 1;
while (b > 0)
{
if (b & 1)
res = res * a;
a = a * a;
b >>= 1;
}
return res;
}
ll gcd(ll a,ll b)
{
if (b==0) return a;
return gcd(b,a%b);
}
string to_upper(string a)
{
for (int i=0;i<(int)a.size();++i) if (a[i]>='a' && a[i]<='z') a[i]-='a'-'A';
return a;
}
string to_lower(string a)
{
for (int i=0;i<(int)a.size();++i) if (a[i]>='A' && a[i]<='Z') a[i]+='a'-'A';
return a;
}
bitset<20> edges[20];
int dp[(1<<20)];
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
int n,m,x,y;
cin>>n>>m;
while (m--)
{
cin>>x>>y;
--x;--y;
edges[x][y]=edges[y][x]=1;
}
for (int i=1;i<(1<<n);++i)
{
dp[i]=1;
for (int j=0;j<n;++j)
if (i&(1<<j))
for (int k=j+1;k<n;++k)
if (i&(1<<k))
if (!edges[j][k])
dp[i]=1000000;
for (int j=(i-1)&i;j;j=(j-1)&i)
dp[i]=min(dp[i],dp[j]+dp[i^j]);
}
cout<<dp[(1<<n)-1];
return 0;
} | #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;
using ll = long long;
struct Graph {
struct Vertex { int n, c, f; };
struct Edge { int i, n, d; };
Graph(int n, int m) : v(n, { -1, -1, 0 }), e(m), n(n), m(0) {}
void add_edge(int a, int b) {
e[m] = { b, v[a].n, -1 };
v[a].n = m;
m++;
}
void dfs(int i, int c) {
if (v[i].f) return; else v[i].f = 1;
for (int j = v[i].n; j >= 0; j = e[j].n) {
Edge &o = e[j];
if (v[o.i].c != c) {
o.d = v[o.i].c < c;
continue;
}
if (e[j ^ 1].d < 0) o.d = 0;
dfs(o.i, c);
}
}
void solve() {
for (int i = 0; i < n; i++) {
cin >> v[i].c;
}
for (int i = 0; i < n; i++) {
dfs(i, v[i].c);
}
for (int i = 0; i < m; i += 2) {
//cout << e[i].d << ' ' << e[i + 1].d << endl;
if (e[i].d < 0) e[i].d = e[i + 1].d ^ 1;
cout << (e[i].d ? "->" : "<-") << '\n';
}
}
vector<Vertex> v;
vector<Edge> e;
int n, m;
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
Graph g(n, m * 2);
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
a--; b--;
g.add_edge(a, b);
g.add_edge(b, a);
}
g.solve();
return 0;
} |
#include<iostream>
#include<string>
#include<vector>
#include<set>
#include<iomanip>
#include<algorithm>
#include<cmath>
#include<bitset>
#include<queue>
#include<stack>
#include<utility>
#include<cstdlib>
#include<cstdio>
#include<map>
#include<unordered_set>
#include<unordered_map>
#include<list>
#include<tuple>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
#define FOR(i, a, b) for(ll i=(a); i<(b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define NREP(i, n) FOR(i, 1, n+1)
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
struct Edge{
ll to, cost;
Edge(ll t, ll c) : to(t), cost(c) { }
};
using Graph = vector<vector<Edge>>;
ll MODpow(ll x, ll n, ll mod){
ll res=1;
while(n>0){
if(n&1) res=res*x%mod;
x=x*x%mod;
n >>= 1;
}
return res;
}
const ll MOD=1e9+7;
const ll nmax=200000+10;
ll N;
Graph G(nmax);
ll calc(ll i){
ll even=0,odd=0;
vl res(N,-1),seen(N,0);
queue<ll> q;
res[0]=0;
q.push(0);
even++;
while(!q.empty()){
ll v=q.front();
q.pop();
seen[v]=1;
for(auto e:G[v]){
if(seen[e.to]) continue;
bool ec=(e.cost&(1LL<<i));
q.push(e.to);
res[e.to]=res[v]^ec;
if(res[e.to]==1) odd++;
else even++;
}
}
ll mid1=MODpow(2,i,MOD);
ll mid2=odd*even%MOD;
ll mid=mid1*mid2%MOD;
return mid;
}
int main(void){
cin>>N;
REP(i,N-1){
ll u,v,w; cin>>u>>v>>w;
u--,v--;
G[u].push_back(Edge{v,w});
G[v].push_back(Edge{u,w});
}
ll res=0;
REP(i,60){
res+=calc(i);
res%=MOD;
}
cout<<res<<endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
const long nPrime = 1000000007;
//const long nPrime = 998244353;
struct Node{
long iON = 0;
long iOFF = 1;
};
long DFS(int x, const vector<vector<pair<int,long>>> &vviEdge,
vector<Node> &vzNode, vector<int> &viCheck){
viCheck[x] = 1;
long nReturn = 0;
for(int i = 0; i < vviEdge[x].size(); i++){
int y = vviEdge[x][i].first;
if(viCheck[y] == 1){
continue;
}
nReturn += DFS(y, vviEdge, vzNode, viCheck);
vzNode[x].iON += (vviEdge[x][i].second == 0) ? vzNode[y].iON
: vzNode[y].iOFF;
vzNode[x].iOFF += (vviEdge[x][i].second == 0) ? vzNode[y].iOFF
: vzNode[y].iON;
vzNode[x].iON %= nPrime;
vzNode[x].iOFF %= nPrime;
nReturn -= vzNode[y].iON * vzNode[y].iOFF;
nReturn %= nPrime;
}
nReturn += vzNode[x].iON * vzNode[x].iOFF;
nReturn %= nPrime;
return nReturn;
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n;
cin >> n;
vector<vector<pair<int,long>>> vviEdge(n);
for(int i = 0; i < n-1; i++){
int a,b;
long c;
cin >> a >> b >> c;
vviEdge[a-1].push_back(make_pair(b-1,c));
vviEdge[b-1].push_back(make_pair(a-1,c));
}
vector<vector<pair<int,long>>> vviEdgeThis = vviEdge;
long nAns = 0;
for(long i = 0; i < 60; i++){
for(long j = 0; j < n; j++){
for(long k = 0; k < vviEdgeThis[j].size(); k++){
vviEdgeThis[j][k].second = ((vviEdge[j][k].second & (1L<<i)) != 0) ? 1:0;
}
}
vector<Node> vzNode(n);
vector<int> viCheck(n,0);
long iThis = (1L<<i) % nPrime;
long nSum = DFS(0, vviEdgeThis, vzNode, viCheck);
nAns += nSum * iThis;
nAns %= nPrime;
}
nAns += nPrime;
nAns %= nPrime;
cout << nAns << endl;
return 0;
} |
#define _USE_MATH_DEFINES
#include <iostream>
#include <cstring>
#include <regex>
#include <cmath>
#include <algorithm>
#include <vector>
#include <list>
#include <map>
#include <unordered_set>
#include <queue>
#include <unordered_map>
#include <set>
#include <cassert>
#include <climits>
#pragma GCC optimize("O3")
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; ++i)
#define repd(i, n) for(ll i = (n) - 1ll; i >= 0; --i)
#define rep1(i, n) for(ll i = 1, i##_len = (n); i <= i##_len; ++i)
#define FOR(i, a, b) for(ll i = a; i < ll(b); ++i)
#define FORD(i, a, b) for(ll i = a; i > ll(b); --i)
#define foreach(i, itr) for(auto& i : (itr))
#define asort(arr, size) std::sort(arr, arr + size)
#define dsort(arr, size) std::sort(arr, arr + size, [](auto a, auto b) { return a > b; })
#define cauto const auto&
#define bit(n) (1LL << (n))
////////////////////////////////////
// ここから下に任意のコードを書く //
////////////////////////////////////
ull A[100100];
ull B[100100];
ull C[100100];
ull bcj[100100];
int main() {
ull N;
cin >> N;
rep1(i, N) cin >> A[i];
rep1(i, N) cin >> B[i];
rep1(i, N) {
cin >> C[i];
++bcj[B[C[i]]];
}
ull count = 0;
rep1(i, N) {
count += bcj[A[i]];
}
cout << count << endl;
return 0;
} | #include <iostream>
#include <string>
constexpr int MAX = 30;
long long dp[MAX + 1][MAX + 1];
std::string find_kth(int A, int B, long long K) {
if (A == 0) {
return std::string(B, 'b');
}
if (B == 0) {
return std::string(A, 'a');
}
if (K <= dp[A - 1][B]) {
return std::string("a") + find_kth(A - 1, B, K);
}
else {
return std::string("b") + find_kth(A, B - 1, K - dp[A - 1][B]);
}
}
int main() {
int A, B;
long long K;
std::cin >> A >> B >> K;
dp[0][0] = 1;
for (int i = 0; i <= A; ++i) {
for (int j = 0; j <= B; ++j) {
if (i > 0) {
dp[i][j] += dp[i - 1][j];
}
if (j > 0) {
dp[i][j] += dp[i][j - 1];
}
}
}
std::cout << find_kth(A, B, K) << '\n';
return 0;
} |
//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <bits/stdc++.h>
#include<queue>
#include <numeric>
#include<stdio.h>
using namespace std;
//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
//container util
//------------------------------------------
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define fore(i,a) for(auto &i:a)
//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))
//debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
LL gcd(LL a, LL b){
if(b == 0) return a;
return gcd(b,a%b);
}
LL lcm(LL a, LL b){
LL g = gcd(a,b);
return a/g *b;
}
int ri(){
int n;
scanf("%d",&n);
return n;
}
int main(){
int N;
cin >> N;
map<string, int> mp;
string S[N];
for (int i = 0;i<N;++i){
cin >> S[i];
++mp[S[i]];
}
for (int i = 0; i < N; ++i)
{
if(S[i][0]=='!'){
S[i].erase(0, 1);
if(mp[S[i]]>=1){
cout << S[i];
return 0;
}
}
}
cout << "satisfiable" << endl;
} | #include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define loop(i,n)for(i=0;i<n;i++)
//#define INF 1000000000
using ll = long long int;
typedef pair<ll,ll> pii;
typedef vector<pii> vii;
typedef long double ld;
typedef vector<ll> vi;
typedef tree<int,null_type,less<int>,rb_tree_tag,
tree_order_statistics_node_update> indexed_set;
bool sortsec(const pair<ll,ll> &a,const pair<ll,ll> &b)
{
if(a.second==b.second)return a.first>b.first;
else return (a.second>b.second);
}
#define eb emplace_back
#define mp make_pair
#define pb push_back
#define MAX 100000
#define MOD 1000000
#define endl '\n'
int main()
{
//freopen("inputfile.txt","r",stdin);
ll n;cin>>n;
map <string,ll> ma;
string r;
for(ll i=0;i<n;i++)
{
string s;cin>>s;
ma[s]++;
}
bool flag = false;
//for(auto x:ma)cout<<x.first<<endl;
//cout<<endl;
for(auto x:ma)
{
//cout<<x.first<<endl;
string h = x.first;
char c = '!';
string r = h.substr(1,h.size());
if(ma[h]>=1 && ma[c+h]>=1)
{
flag = true;
cout<<h<<endl;
break;
}
}
if(!flag)cout<<"satisfiable"<<endl;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
using ll = long long;
using ull = unsigned long long;
using P = pair<int,int>;
using Pll = pair<ll,ll>;
const ll INF = 1001001001;
void chmin(ll& a,ll b){if(a>b)a = b;}
int main(){
int n, m;
cin >> n >> m;
vector<int> g(n);
rep(i,m){
int a,b;
cin >> a >> b;
a--; b--;
g[a] |= 1 << b;
g[b] |= 1 << a;
}
vector<ll> dp(1<<n,INF);
dp[0] = 1;
rep(i,n){
rep(j,1<<n){
if(dp[j]==1&&(g[i]&j)==j){
dp[j|1<<i] = 1;
}
}
}
rep(i,1<<n){
for(int j = i; --j &= i;){
chmin(dp[i],dp[i^j]+dp[j]);
}
}
cout << dp.back() << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define cfor(n) for(int i = 0; i < n; ++i)
#define forcn(i,n) for(int i = 0; i < n; ++i)
#define forlcn(i,l,n) for(int i = l; i < n; i++)
#define fordn(i,n) for(int i = n - 1; i >= 0; --i)
#define forldn(i,l,n) for(int i = n - 1 - l; i >= 0; i--)
#define forau(i,v) for(auto &i : v)
typedef long long ll;
typedef unsigned long long ull;
ll mod = 1000000007;
int main(){
int A,B; cin>>A>>B;
int ans = 2*A+100;
cout << ans - B << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100;
int n;
int a[maxn + 10][maxn + 10];
char s[maxn + 10];
double ans;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%s", s + 1);
for (int j = 1; j <= n; ++j)
a[i][j] = s[j] == '1';
a[i][i] = 1;
}
for (int k = 1; k <= n; ++k)
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
a[i][j] |= a[i][k] && a[k][j];
for (int i = 1; i <= n; ++i) {
int v = 0;
for (int j = 1; j <= n; ++j)
v += a[j][i];
ans += 1. / v;
}
printf("%.10lf", ans);
}
| #include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N=2e5;
priority_queue<int> pq;
int tab[N+10][2];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
for(int i=1;i<=n;i++)
cin>>tab[n-i+1][0];
for(int i=1;i<=n;i++)
cin>>tab[i][1];
long long ans=0;
for(int i=1;i<=n;i++)
{
if(tab[i][0]>tab[i][1])
swap(tab[i][0],tab[i][1]);
if(!pq.empty() && (-pq.top())<tab[i][0])
{
ans+=pq.top();
pq.pop();
ans+=tab[i][0];
pq.push(-tab[i][0]);
}
ans+=tab[i][1];
pq.push(-tab[i][1]);
}
cout<<ans<<"\n";
return 0;
}
|
#include <iostream>
#include <iomanip> //<<setprecision(12), or <<fixed
#include <algorithm>
#include <functional>
#include <map>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
using namespace std;
using ll=long long;
using ull=unsigned long long;
using pll=pair<ll,ll>;
int main()
{
ll N,M;
cin>>N>>M;
vector<string> S(N);
for(auto& s:S) cin>>s;
vector<ll> cnt(N,0);
for(ll i=0; i<N; i++)
{
for(auto c:S[i]) if(c=='1') cnt[i]++;
//cout<<cnt[i]<<endl;
}
vector<ll> oddsum(N+1,0), evensum(N+1,0);
for(ll i=0; i<N; i++)
{
oddsum[i+1]=oddsum[i]+cnt[i]%2;
evensum[i+1]=evensum[i]+(cnt[i]+1)%2;
//cout<<oddsum[i+1]<<" "<<evensum[i+1]<<endl;
}
ll res=0;
for(ll i=0; i<N; i++)
{
if(cnt[i]%2){ res+=evensum[N]-evensum[i]; }
else{ res+=oddsum[N]-oddsum[i]; }
}
cout<<res<<endl;
// Find a CounterExample.
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll MOD = 1000000007;
ll fexp(ll b, ll e){
ll res = 1;
for(;e;e>>=1){
if(e&1)res = (res*b)%MOD;
b = (b*b)%MOD;
}
return res;
}
// INVERSO MOLTIPLICATIVO
ll inv(ll a){
return fexp(a,MOD-2);
}
int T = 1;
ll N;
ll a;
int main(){
cin >> N;
ll S=N,M;
cin >> M;
for(int i = 0; i < N; i++){
cin >> a;
S+=a;
}
M+=N-1;
ll r = 1;
for(int i = 1; i <= S; i++){
r *= (M-i+2); r%=MOD;
r *= inv(i); r %= MOD;
}
cout << r << "\n";
return 0;
}
|
#include <bits/stdc++.h>
#define int int64_t
const int N=1e5+9, N2=2e5+9;
using namespace std;
signed main(){
int a, b;
vector<int> vc, vs;
cin >> a >> b;
if (a==b){
for (int i=1; i<=a; i++){
cout << -i << " " << i << " ";
}
}
else if (a>b){
int i=1, mn=0, pl=0;
for (; i<=b; i++){
vc.push_back(i);
vc.push_back(-i);
}
for (;i<=a; i++){
vs.push_back(i*1000);
vc.back()+=-i*1000;
}
for (int k:vc){
cout << k << " ";
if (k<0) mn+=k;
else pl+=k;
}
for (int k:vs){
cout << k << " ";
if (k<0) mn+=k;
else pl+=k;
}
cerr << '\n' << pl << " " << mn;
}
else {
int i=1, mn=0, pl=0;
for (; i<=a; i++){
vc.push_back(-i);
vc.push_back(i);
}
for (;i<=b; i++){
vs.push_back(-i*1000);
vc.back()+=i*1000;
}
for (int k:vc){
cout << k << " ";
if (k<0) mn+=k;
else pl+=k;
}
for (int k:vs){
cout << k << " ";
if (k<0) mn+=k;
else pl+=k;
}
cerr << '\n' << pl << " " << mn;
}
} | #include <iostream>
#include <set>
#include <iterator>
#include <bitset>
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll mod=1000000007;
ll power(ll x,ll y, ll p)
{
ll res = 1; // Initialize result
x = x % p; // Update x if it is more than or
// equal to p
if (x == 0) return 0; // In case x is divisible by p;
while (y > 0)
{
// If y is odd, multiply x with result
if (y & 1)
res = (res*x) % p;
// y must be even now
y = y>>1; // y = y/2
x = (x*x) % p;
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int t=1;
//cin>>t;
while(t--){
ll n,p ;
cin>>n>>p;
if(n==1)
{
cout<<p-1;
return 0;
}
if(p==2)
{
cout<<0;return 0;
}
cout<<(power(p-2,n-1,mod)%mod*(p-1)%mod)%mod;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<ll, ll> pl;
struct edge {
ll to; // 辺の行き先
ll weight; // 辺の重み
edge(ll t, ll w) : to(t), weight(w) { }
};
typedef vector<vector<ll> > Graph;
typedef vector<vector<edge> > WGraph;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rep3(i, k, n) for(int i = k; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define pb push_back
#define vout(v) for(int index_vout=0; index_vout < v.size(); index_vout++) cout<<v[index_vout]<<' '
#define blank cout<<endl
#define newvar(n) ll n; cin >> n
#define uniquify(vec) (vec).erase(std::unique((vec).begin(), (vec).end()), (vec).end())
const ll MOD = 1e9 + 7;
const ll INF = MOD*MOD+1;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll n;
cin >> n;
vector<vl> c(n);
rep(i,n){
rep(j, n){
ll x;
cin >> x;
c[i].pb(x);
}
}
vector<vl> dif(n, vl(n-1));
rep(i,n){
rep(j,n-1){
dif[i][j] = c[i][j+1] - c[i][j];
}
}
bool chk = true;
rep(i,n){
rep(j, n-1){
if(dif[0][j] != dif[i][j]) chk = false;
}
}
if(!chk){
cout << "No";
return 0;
}
cout << "Yes" << endl;
ll m = INF;
ll m_i;
ll m_j;
rep(i,n){
rep(j,n){
if(c[i][j] < m){
m_i = i;
m_j = j;
m = c[i][j];
}
}
}
vl m_dif(n);
rep(i,n) cout << c[i][m_j] - m << ' ';
cout << endl;
vout(c[m_i]);
cout << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main () {
int N;
cin >> N;
ll A[550][550];
for (int i = 0; i < N; i ++) {
for (int j = 0; j < N; j ++) {
cin >> A[i][j];
}
}
ll dif_r[550];
ll dif_c[550];
for (int i = 0; i < N - 1; i ++) {
dif_c[i] = A[0][i + 1] - A[0][i];
}
for (int i = 0; i < N - 1; i ++) {
dif_r[i] = A[i + 1][0] - A[i][0];
}
for (int i = 0; i < N; i ++) {
for (int j = 0; j < N; j ++) {
if (i < N - 1) {
if (A[i + 1][j] - A[i][j] != dif_r[i]) {
cout << "No" << endl;
return 0;
}
}
if (j < N - 1) {
if (A[i][j + 1] - A[i][j] != dif_c[j]) {
cout << "No" << endl;
return 0;
}
}
}
}
ll mi_r = 0;
ll nr = 0;
for (int i = 0; i < N - 1; i ++) {
nr += dif_r[i];
mi_r = min(mi_r, nr);
}
ll mi_c = 0;
ll nc = 0;
for (int i = 0; i < N - 1; i ++) {
nc += dif_c[i];
mi_c = min(mi_c, nc);
}
mi_r *= -1;
mi_c *= -1;
if (mi_r + mi_c > A[0][0]) {
cout << "No" << endl;
return 0;
}
cout << "Yes" << endl;
nr = mi_r;
nc = A[0][0] - mi_r;
dif_r[N] = 0;
dif_c[N] = 0;
for (int i = 0; i < N; i ++) {
cout << nr << (i < N - 1 ? " " : "");
nr += dif_r[i];
}
cout << endl;
for (int i = 0; i < N; i ++) {
cout << nc << (i < N - 1 ? " " : "");
nc += dif_c[i];
}
cout << endl;
} |
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<long>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvb = vector<vector<bool>>;
using vvc = vector<vector<char>>;
using vvl = vector<vector<long>>;
using pii = pair<int, int>;
using pil = pair<int, long>;
using pll = pair<long, long>;
#define fix20 cout << fixed << setprecision(20)
#define YES cout << "Yes" << endl
#define NO cout << "No" << endl
#define rep(i,n) for(long i=0; i<(long)(n);i++)
#define REP(i,s,t) for(long i=s; i<t; i++)
#define RNG(i,s,t,u) for(long i=s; i<t; i+=u)
#define MOD 1000000007
#define all(vec) vec.begin(), vec.end()
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
struct Edge{
long from;
long to;
long cost;
};
int main(){
long n,m;
cin >> n >> m;
vl w(n);
rep(i,n) cin >> w[i];
vector<pll> data(m);
long allmin = 1e16;
rep(i,m){
cin >> data.at(i).second >> data.at(i).first;
chmin(allmin, data.at(i).first);
}
sort(all(data));
long tmpmax = 0;
rep(i,m){
chmax(data.at(i).second, tmpmax);
chmax(tmpmax, data.at(i).second);
}
sort(all(w));
rep(i,n){
if(w[i] > allmin){
//cout << allmin << endl;
cout << -1 << endl;
return 0;
}
}
long ans = 1e17;
do{
vl sum(n+1,0);
rep(i,n){
sum[i+1] = sum[i] + w.at(i);
}
vector<vector<Edge>> Graph(n);
REP(i,2,n+1){
for(long j=0;j+i<=n;j++){
long wsum = sum[j+i]-sum[j];
long iter = upper_bound(all(data), pll(wsum-1,1e17)) - data.begin();
long L;
if(iter == 0) L = 0;
else L = data.at(iter-1).second;
Graph.at(j).push_back(Edge{j,j+i-1,L});
}
}
vl dp(n,0);
rep(i,n-1){
for(Edge ed : Graph.at(i)){
long nv = ed.to;
long L = ed.cost;
dp.at(nv) = max(dp.at(nv), dp.at(i) + L);
}
}
ans = min(ans, dp.at(n-1));
}while(next_permutation(all(w)));
cout << ans << endl;
} | //
// Created by 原田 on 2021/04/13.
//
#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 <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <math.h>
#include <ios>
#include <iomanip>
#include <numeric>
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
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; }
typedef long long ll;
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
const long long LINF =1e18;
const int INF = 1e9;
int main() {
string s1,s2,s3;
cin>>s1>>s2>>s3;
map<char,int>mp;
vl kurai = {0,1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000,10000000000,100000000000};
for (int i = 0; i < s1.size(); ++i) {
mp[s1[i]] ++;
}
for (int i = 0; i < s2.size(); ++i) {
mp[s2[i]] ++;
}
for (int i = 0; i < s3.size(); ++i) {
mp[s3[i]] ++;
}
if(mp.size() > 10){
cout<<"UNSOLVABLE"<<endl;
return 0;
}
vector<int> v(10);
iota(v.begin(), v.end(), 0); // v に 1, 2, ... N を設定
do {
int i = 0;
for(auto item : mp){
mp[item.first] = v[i];
i++;
}
bool ok = true;
ll s1_n = 0;
for (int j = 1; j <= s1.size(); ++j) {
s1_n += mp[s1[s1.size() - j]] * kurai[j];
if(j == s1.size() && mp[s1[s1.size() - j]] == 0){
ok = false;
}
}
ll s2_n = 0;
for (int j = 1; j <= s2.size(); ++j) {
s2_n += mp[s2[s2.size() - j]] * kurai[j];
if(j == s2.size() && mp[s2[s2.size() - j]] == 0){
ok = false;
}
}
ll s3_n = 0;
for (int j = 1; j <= s3.size(); ++j) {
s3_n += mp[s3[s3.size() - j]] * kurai[j];
if(j == s3.size() && mp[s3[s3.size() - j]] == 0){
ok = false;
}
}
if(s1_n + s2_n != s3_n){
ok = false;
}
if(ok){
cout<<s1_n<<endl;
cout<<s2_n<<endl;
cout<<s3_n<<endl;
return 0;
}
} while( next_permutation(v.begin(), v.end()) ); // 次の順列を生成
cout<<"UNSOLVABLE"<<endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int m = 2e5+10;
int k = ceil(log2(m)) + 1;
int sk = (1 << k);
vector<ll> seg(sk);
void update(int s, int e, int at, int l, int r, int p){
if(l<=s && e<=r) seg[at]+=p;
else {
int q = (s+e)>>1;
if(r<=q) update(s,q,at*2+1,l,r,p);
else if(l>q) update(q+1,e,at*2+2,l,r,p);
else {
update(s,q,at*2+1,l,q,p);
update(q+1,e,at*2+2,q+1,r,p);
}
}
}
ll get(int s, int e, int at, int id) {
if(s>=e) return seg[at];
int q = (s + e) >> 1;
if(id<=q) return seg[at]+get(s,q,at*2+1,id);
return seg[at]+get(q+1,e,at*2+2,id);
}
void solve() {
int n, w;
cin >> n >> w;
vector<int> a(n), b(n), p(n);
for(int i = 0; i < n; i++) cin >> a[i] >> b[i] >> p[i];
for(int i = 0; i < n; i++) {
update(0,m-1,0,a[i],b[i]-1,p[i]);
}
bool f=true;
for(int i = 0; i < m; i++) {
ll r = get(0,m-1,0,i);
if(r>w) {
f = false;
break;
}
}
if(f) cout << "Yes" << endl;
else cout << "No" << endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin>>t;
while(t--){
solve();
}
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
using ll = long long;
using P = pair<int, int>;
ll n, w, s, t, p;
int main() {
cin >> n >> w;
vector<ll> c(200001, 0);
rep (i, n) {
cin >> s >> t >> p;
c[s] -= p;
c[t] += p;
}
ll d = w;
string ans = "Yes";
rep (i, 200001) {
d += c[i];
if (d < 0) {
ans = "No";
}
}
cout << ans << endl;
return 0;
}
|
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
int main(void)
{
long long a, b, c;
std::cin >> a >> b >> c;
if (a * a < c * c - b * b)
std::cout << "Yes" << std::endl;
else
std::cout << "No" << std::endl;
return (0);
} | #include <bits/stdc++.h>
#define x first
#define y second
#define endll '\n'
#define pb push_back
#define mem(x,a) memset(x,a,sizeof(x))
using namespace std;
const int N=1e5+10,mod=19650827;
const int inf=0x3f3f3f3f;
typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned long long ull;
int n,k,m;
inline int read(){
int x=0,f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
int a[1010];
int f[1010][1010][2];
void work(){
int a=read(),b=read(),c=read();
if(a*a+b*b<c*c)puts("Yes");
else puts("No");
}
int main(){
//int T=read();
//while(T--)
work();
return 0;
}
|
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<ctime>
#include<bitset>
#include<vector>
#include<cstdio>
#include<complex>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define SZ(v) ((int)(v).size())
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
template<class T>
inline void relax(T& a,const T& b) { a=((a>b) ? a : b); }
template<class T>
inline void tense(T& a,const T& b) { a=((a<b) ? a : b); }
template<class T>
inline void read(T& x)
{
x=0;
register bool sign=false;
register char c;
for(c=getchar();c<'0' || c>'9';c=getchar())
if(c=='-') sign=true;
for(;c>='0' && c<='9';c=getchar())
x=(x<<1)+(x<<3)+(c&15);
if(sign) x=~x+1;
}
const int N=1e5+5;
int n;
set<int> S[N];
int a[N];
int Mex(set<int>& S)
{
for(int i=1;;i++)
if(!S.count(i)) return i;
return -1;
}
int main()
{
// freopen("1.in","r",stdin);
int i,j;
read(n);
for(i=1;i<=n;i++) {
a[i]=Mex(S[i]);
S[i].clear();
for(j=i+i;j<=n;j+=i)
S[j].insert(a[i]);
}
for(i=1;i<=n;i++)
printf("%d ",a[i]);
puts("");
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(v) v.begin(), v.end()
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repr(i, n) for (ll i = (n-1); i >= 0; i--)
#define Rep(n) for (ll _ = 0; _ < (ll)(n); _++)
#define popcountll(x) __builtin_popcountll(x)
template <class S>S sum(vector<S>& a) {return accumulate(all(a), S());}
template <class S>S max(vector<S>& a) {return *max_element(all(a));}
template <class S>S min(vector<S>& a) {return *min_element(all(a));}
ll max(int a, ll b) { return max((ll)a, b); } ll max(ll a, int b) { return max(a, (ll)b); }
ll min(int a, ll b) { return min((ll)a, b); } ll min(ll a, int b) { return min(a, (ll)b); }
void Main();
int main() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(20);
Main();
return 0;
}
#define VM 10
void eprint() { cerr << endl; }
template <class First, class... Rest>
void eprint(First f, Rest... r) {
cerr << f << ' ';
eprint(r...);
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
size_t output_num = v.size();
bool is_omitted = false;
#ifdef VM
if (VM < output_num) {
output_num = VM;
is_omitted = true;
}
#endif
os << "[";
for (size_t i = 0; i < output_num - 1; i++) os << v[i] << ", ";
os << v[output_num - 1];
if (is_omitted) os << ", ...";
return os << "]";
}
template <class S, class T>
ostream& operator<<(ostream& os, const map<S,T>& m) {
os << "{\n";
for(auto [k,v]: m){
os << k << ": " << v << ",\n";
}
return os << '}';
}
// n未満の素数のリストを返す
// エラトステレスの篩
vector<int> prime_enum(int n) {
if(n <= 2) return vector<int>();
vector<int> res;
vector<bool> is_prime(n, true);
is_prime[0] = false;
is_prime[1] = false;
for (ll i = 2; i < n; i++) {
if (!is_prime[i]) continue;
res.push_back(i);
for (ll j = 2; i * j < n; j++) {
is_prime[i * j] = false;
}
}
return res;
}
// 素数リストを用いた実装
// 素数リストはsqrt(n)までの素数を含む必要がある
map<int, int> prime_factor(int n, const vector<int>& primes) {
map<int, int> res;
for (int p : primes) {
if (p * p > n) break;
while (n % p == 0) {
res[p]++;
n /= p;
}
}
if (n != 1) res[n]++;
return res;
}
int yakusuu(int n, vector<int>& primes){
auto m = prime_factor(n, primes);
int ans = 1;
for(auto [_, c]: m){
ans += c;
}
return ans;
}
using P = pair<int,int>;
void Main(){
int n; cin >> n;
auto primes = prime_enum(n);
for(int i = 1; i <= n; i++){
cout << yakusuu(i, primes) << ' ';
}
}
|
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int m = 1;
while (n != 0) {
m *= 2;
--n;
}
int a, b;
a = 0; b = 0;
for (int i = 1; i <= m / 2; ++i) {
int c;
cin >> c;
if (b < c) {
a = i;
b = c;
}
}
int e, f;
e = 0; f = 0;
for (int i = 1; i <= m / 2; ++i) {
int c;
cin >> c;
if (f < c) {
e = i + m / 2;
f = c;
}
}
if (b < f) {
cout << a << endl;
}
else {
cout << e << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
template<class T>void chmin(T& a,T b){
if(a>b){
a=b;
}
}
typedef std::pair<int,int> p;
#define rep(i,n) for(int i=0;i<n;++i)
int main(){
const int INF=1001001001;
int N;
cin>>N;
int k=1<<(N-1);
vector<int> a(k),b(k);
rep(i,k)cin>>a[i];
rep(i,k)cin>>b[i];
vector<int>::iterator iter_c=max_element(a.begin(),a.end());
vector<int>::iterator iter_d=max_element(b.begin(),b.end());
size_t index_c=distance(a.begin(),iter_c);
size_t index_d=distance(b.begin(),iter_d);
if(*iter_c<*iter_d)cout<<index_c+1<<endl;
else cout<<k+index_d+1<<endl;
} |
#include <bits/stdc++.h>
#define DEBUG if(0)
#define lli long long int
#define ldouble long double
using namespace std;
const int maxN = 2e3; int n, m;
char a[maxN][maxN];
int getPoints(int i, int j, bool taka)
{
if (!taka)
return a[i][j] == '+' ? 1 : -1;
return a[i][j] == '-' ? 1 : -1;
}
int memo[maxN][maxN][2]; bool visited[maxN][maxN][2];
int solve(int i = 0, int j = 0, bool taka = true)
{
if (i == n - 1 && j == m - 1)
return getPoints(i, j, taka);
int &ans = memo[i][j][taka]; bool &vis = visited[i][j][taka];
if (vis) return ans;
vis = true;
if (taka)
{
ans = -1e9;
if (i + 1 < n) ans = max(ans, solve(i + 1, j, !taka));
if (j + 1 < m) ans = max(ans, solve(i, j + 1, !taka));
if (!(i == 0 && j == 0)) ans += getPoints(i, j, taka);
}
else
{
ans = 1e9;
if (i + 1 < n) ans = min(ans, solve(i + 1, j, !taka));
if (j + 1 < m) ans = min(ans, solve(i, j + 1, !taka));
if (!(i == 0 && j == 0)) ans += getPoints(i, j, taka);
}
return ans;
}
int main()
{
while (~scanf("%d %d", &n, &m))
{
for (int i = 0; i < n; i++)
scanf(" %s", a[i]);
memset(visited, false, sizeof(visited));
int ans = solve();
if (ans == 0 || (n == 1 && m == 1))
printf("Draw\n");
else if (ans > 0)
printf("Takahashi\n");
else
printf("Aoki\n");
}
return 0;
} | /*
これを入れて実行
g++ code.cpp
./a.out
*/
#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <vector>
#include <string>
#include <cstring>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <math.h>
#include <tuple>
#include <iomanip>
#include <bitset>
#include <functional>
#include <cassert>
#include <random>
#define all(x) (x).begin(),(x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
typedef long long ll;
typedef long double ld;
int dy4[4] = {-1, 0, +1, 0};
int dx4[4] = {0, +1, 0, -1};
int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const long long INF = 1LL << 60;
const ll MOD = 1e9 + 7;
bool greaterSecond(const pair<int, int>& f, const pair<int, int>& s){
return f.second > s.second;
}
ll gcd(ll a, ll b){
if (b == 0)return a;
return gcd(b, a % b);
}
ll lcm(ll a, ll b){
return a / gcd(a, b) * b;
}
ll conbinationMemo[100][100];
void cmemoInit(){
rep(i, 100){
rep(j, 100){
conbinationMemo[i][j] = -1;
}
}
}
ll nCr(ll n, ll r){
if(conbinationMemo[n][r] != -1) return conbinationMemo[n][r];
if(r == 0 || r == n){
return 1;
} else if(r == 1){
return n;
}
return conbinationMemo[n][r] = (nCr(n - 1, r) + nCr(n - 1, r - 1));
}
ll nPr(ll n, ll r){
r = n - r;
ll ret = 1;
for (ll i = n; i >= r + 1; i--) ret *= i;
return ret;
}
//-----------------------ここから-----------
ll dp[2200][2200];
ll sumh[2200][2200], sumw[2200][2200], sum[2200][2200];
ll extGCD(ll a, ll b, ll &x, ll &y){
if (b == 0){
x = 1;
y = 0;
return a;
}
ll d = extGCD(b, a % b, y, x);
y -= a / b * x;
return d;
}
ll getinv(ll num){
ll x, y;
ll g = extGCD(num, MOD, x, y);
while (x < 0) x += MOD;
x %= MOD;
return x;
}
int main(void){
ll h, w;
cin >> h >> w;
vector<string> s(h);
rep(i, h){
cin >> s[i];
}
dp[0][0] = 1;
sumh[0][0] = 1;
sumw[0][0] = 1;
sum[0][0] = 1;
for(ll i = 0; i < h; i++){
for(ll j = 0; j < w; j++){
if(s[i][j] == '#') continue;
dp[i + 1][j] += dp[i][j] + sumh[i][j];
dp[i + 1][j] %= MOD;
dp[i][j + 1] += dp[i][j] + sumw[i][j];
dp[i][j + 1] %= MOD;
dp[i + 1][j + 1] += dp[i][j] + sum[i][j];
dp[i + 1][j + 1] %= MOD;
sumh[i + 1][j] += dp[i][j] + sumh[i][j];
sumh[i + 1][j] %= MOD;
sumw[i][j + 1] += dp[i][j] + sumw[i][j];
sumw[i][j + 1] %= MOD;
sum[i + 1][j + 1] += dp[i][j] + sum[i][j];
sum[i + 1][j + 1] %= MOD;
}
}
cout << dp[h - 1][w - 1] * getinv(2) % MOD << endl;
} |
#include<cstdio>
#define N 100010
#define mo 1000000007
#define ll long long
#define fo(x,a,b) for(int x=(a);x<=(b);x++)
#define fd(x,a,b) for(int x=(a);x>=(b);x--)
using namespace std;
int n;
ll f[N][2],num[N][2],a[N];
inline int read() {
int x=0,f=0; char c=getchar();
while (c<'0'||c>'9') f=(c=='-')?1:f,c=getchar();
while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
return f?-x:x;
}
int main() {
n=read();
fo(i,1,n) a[i]=read();
f[1][0]=a[1],f[1][1]=0;
num[1][0]=1,num[1][1]=0;
fo(i,2,n) {
f[i][0]=(f[i-1][0]+num[i-1][0]*a[i]%mo+f[i-1][1]+num[i-1][1]*a[i]%mo)%mo;
f[i][1]=(f[i-1][0]-num[i-1][0]*a[i]%mo+mo)%mo;
num[i][0]=(num[i-1][0]+num[i-1][1])%mo;
num[i][1]=num[i-1][0];
}
printf("%lld\n",((f[n][0]+f[n][1])%mo+mo)%mo);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define Gene template< class
#define Rics printer& operator,
Gene c> struct rge{c b, e;};
Gene c> rge<c> range(c i, c j){ return {i, j};}
struct printer{
~printer(){cerr<<endl;}
Gene c >Rics(c x){ cerr<<boolalpha<<x; return *this;}
Rics(string x){cerr<<x;return *this;}
Gene c, class d >Rics(pair<c, d> x){ return *this,"(",x.first,", ",x.second,")";}
Gene ... d, Gene ...> class c >Rics(c<d...> x){ return *this, range(begin(x), end(x));}
Gene c >Rics(rge<c> x){
*this,"["; for(auto it = x.b; it != x.e; ++it)
*this,(it==x.b?"":", "),*it; return *this,"]";}
};
#define debug() cerr<<"LINE "<<__LINE__<<" >> ", printer()
#define dbg(x) "[",#x,": ",(x),"] "
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int my_rand(int l, int r) {
return uniform_int_distribution<int>(l, r) (rng);
}
const int N = 105;
int mod;
int dp[N][N*N*N];
int main() {
// freopen("in.txt", "r", stdin);
ios::sync_with_stdio(0);
cin.tie(0);
int n, K;
cin >> n >> K >> mod;
dp[0][0] = 1;
int sum = 0;
for(int i = 1; i <= n; i++) {
sum += K * i;
for(int s1 = 0; s1 < i; s1++) {
int ways = 0;
for(int s = s1; s <= sum; s += i) {
ways += dp[i-1][s];
if(ways >= mod) ways -= mod;
if(s-(K+1)*i >= 0) {
ways -= dp[i-1][s-(K+1)*i];
if(ways < 0) ways += mod;
}
dp[i][s] = ways;
// debug(), dbg(i), dbg(s), dbg(dp[i][s]);
}
}
}
for(int avg = 1; avg <= n; avg++) {
int ways = 0;
for(int s = 0; s <= sum; s++) {
ways += 1ll*dp[avg-1][s]*dp[n-avg][s]%mod;
if(ways >= mod) ways -= mod;
}
ways = 1ll*ways*(K+1)%mod;
ways--;
if(ways < 0) ways += mod;
cout << ways << "\n";
}
}
|
#include <iostream>
#include <vector>
using namespace std;
int main()
{
long long A, B;
cin>>A>>B;
vector<int> F;
for (int f=2; f<=72; f++)
{
bool ok = true;
for (int i=2; i<f; i++)
if (f%i==0)
ok = false;
if (ok)
F.push_back(f);
}
int n = (int)F.size();
vector<long long> T(1<<n);
T[0] = 1;
for (long long c=A; c<=B; c++)
{
vector<long long> P = T;
int b = 0;
for (int i=0; i<n; i++)
if (c%F[i]==0)
b |= 1<<i;
for (int i=0; i<1<<n; i++)
if ((b&i)==0)
T[b|i] += P[i];
}
long long ans = 0;
for (long long t: T)
ans += t;
cout<<ans<<endl;
}
| #include <algorithm>
#include <iostream>
#include <deque>
#include <climits>
#include <cmath>
#include <cstring>
#include <map>
#include <set>
#include <vector>
// #include <atcoder/convolution>
using namespace std;
using ll = long long;
#define len(s) static_cast<ll>(s.size())
#define rep(i, n) for (ll i = 0; i < n; i++)
#define all(c) c.begin(), c.end()
template<typename T, const char* fmt>
struct INPUT {
T value;
INPUT() { scanf(fmt, &value); }
operator T() { return value; }
};
const char IC_FMT[] = "%c";
const char ILL_FMT[] = "%lld";
using CHAR = INPUT<char, IC_FMT>;
using LL = INPUT<ll, ILL_FMT>;
template<typename S>
std::ostream &operator<<(std::ostream &os, const vector<S> &vec) {
auto size = len(vec);
rep(i, size) {
cout << vec[i];
if (i < size - 1) cout << ' ';
}
return cout;
}
template<typename S, typename T>
std::ostream &operator<<(std::ostream &os, const pair<S, T> &pair) {
return cout << pair.first << ' ' << pair.second;
}
template <typename T>
void print(T t) {
cout << t << endl;
}
template <typename T, typename S, typename... Args>
void print(T t, S s, Args... args) {
cout << t << ' ';
print(s, args...);
}
template<typename... Args>
void debug(Args... args) {
#ifndef NDEBUG
cout << "\x1b[1;32m";
print(args...);
cout << "\x1b[0m";
#endif
}
int gcd(int a, int b) {
if (a == 0) {
return b;
}
return gcd(b % a, a);
}
const vector<ll> pr_list = {
2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
31, 37, 41, 43, 47, 53, 59, 61, 67, 71};
ll dp[74][1 << 20];
int main() {
LL A, B;
debug(A, B);
ll mask[73];
for (ll i = A; i <= B; i++) {
mask[i - A] = 0;
rep(j, 20) {
if (i % pr_list[j] == 0) {
mask[i - A] |= 1 << j;
}
}
}
memset(&dp, 0, sizeof(dp));
dp[0][0] = 1;
for (ll i = A; i <= B; i++) {
rep(j, 1 << 20) {
if (!(j & mask[i - A])) {
dp[i - A + 1][j | mask[i - A]] += dp[i - A][j];
}
dp[i - A + 1][j] += dp[i - A][j];
}
}
ll count = 0;
rep(j, 1 << 20) {
count += dp[B - A + 1][j];
}
print(count);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long power(long long a, long long b) {
if(b == 0)
return 1;
if(b == 1)
return a;
long long x = power(a, b / 2);
x *= x;
if(b & 1)
x *= a;
return x;
}
int main() {
long long n;
cin >> n;
long long a = 1;
while(true) {
long long x = power(5LL, a);
if(x > n)
break;
long long y = n - x;
long long b = 1;
while(power(3LL, b) <= y) {
b++;
}
long long shoot = power(3LL, b);
shoot /= 3;
if(shoot == y && b > 1) {
cout << b - 1 << " " << a << endl;
return 0;
}
a++;
}
cout << "-1" << endl;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define For(i,l,r) for(int i=l;i<=r;i++)
int read(){
int x=0,f=1;char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int a[200001],res=0,n;
bool vis[200001];
void calc(int x){
if(x==res){
++res;
while(vis[res]==true)++res;
}
else vis[x]=true;
}
int main()
{
n=read();
For(i,1,n) a[i]=read();
For(i,1,n){
calc(a[i]);
printf("%d\n",res);
}
return 0;
} |
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
template<typename T>
long long modpow(const T n,const T p,const T mod);
template<typename T>
long long modinv(const T n,const T mod);
template<typename T>
bool chmax(T &a,const T &b);
template<typename T>
bool chmin(T &a,const T &b);
long long inf=1000000007;
int main(){
long long n;
cin>>n;
vector<vector<long long>> tosi(n,vector<long long>(3));
for(long long i=0;i<n;i++) cin>>tosi.at(i).at(0)>>tosi.at(i).at(1)>>tosi.at(i).at(2);
vector<vector<long long>> dp(n,vector<long long>(modpow((long long)2,n,inf),inf));
dp.at(0).at(0)=0;
for(long long i=0;i<(1<<n);i++){
bitset<18> bits=i;
for(long long j=0;j<n;j++){
if(!(i==0 && j==0) && bits.test(j)==false) continue;
for(long long k=0;k<n;k++){
if(bits.test(k)) continue;
long long a=abs(tosi.at(j).at(0)-tosi.at(k).at(0));
long long b=abs(tosi.at(j).at(1)-tosi.at(k).at(1));
long long c=max((long long)0,tosi.at(k).at(2)-tosi.at(j).at(2));
chmin(dp.at(k).at(i+modpow((long long)2,k,inf)),dp.at(j).at(i)+a+b+c);
}
}
}
cout<<dp.at(0).at(modpow((long long)2,n,inf)-1)<<endl;
return 0;
}
template<typename T>
long long modpow(const T n,const T p,const T mod){
if(p==0) return 1;
if(p%2==0){
long long a=modpow(n,p/2,mod);
return a*a%mod;
}
if(p%2==1) return (modpow(n,p-1,mod)*n)%mod;
cerr<<"ERROR"<<endl;
return 1;
}
template<typename T>
long long modinv(const T n,const T mod){
return modpow(n,mod-2,mod);
}
template<typename T>
bool chmax(T &a,const T &b){
if(a<b){
a=b;
return 1;
}
return 0;
}
template<typename T>
bool chmin(T &a,const T &b){
if(a>b){
a=b;
return 1;
}
return 0;
}
| #include <bits/stdc++.h>
#define LL_INFI 0x3f3f3f3f
using namespace std;
long long ** creat2dArray(int N);
void delete2dArray(long long ** dp, int N);
int main ()
{
int N;
scanf("%d", &N);
int X[N + 1];
int Y[N + 1];
int Z[N + 1];
for(int i = 1; i <= N; i++)
scanf("%d %d %d", &X[i], &Y[i], &Z[i]);
long long dis[18][18] = {LL_INFI};
for(int i = 1; i <= N; i++)
{
dis[i][i] = 0;
for(int j = 1; j <= N; j++)
{
dis[i][j] = abs(X[j] - X[i]) + abs(Y[j] - Y[i]) + max(0, Z[j] - Z[i]);
}
}
// floyd
for(int k = 1; k <= N; k++)
for(int i = 1; i <= N; i++)
for(int j = 1; j <= N; j++)
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
long long **dp = creat2dArray(N);
for(int i = 1; i < (1 << N); i++)
{
for(int j = 0; j < N; j++)
{
if(!((i>>j) & 1))
{
for(int k = 0; k < N; k++)
if((i>>k) & 1)
dp[i | (1<<j)][j] = min(dp[i | (1<<j)][j], dp[i][k] + dis[k+1][j+1]);
}
}
}
long long ans = 1e18;
for(int i = 0; i < N; i++)
ans = min(ans, dp[(1<<N) - 1][i] + dis[i+1][1]);
printf("%lld\n", ans);
delete2dArray(dp, N);
return 0;
}
long long ** creat2dArray(int N)
{
// ask for memory
long long **dp;
int size = 1 << N;
dp = new long long*[size];
for(int i = 0; i < size; i++)
{
dp[i] = new long long[N];
}
// initialize
for(int i = 0; i < size; i++)
for(int j = 0; j < N; j ++)
dp[i][j] = LL_INFI;
dp[1][0] = 0;
return dp;
}
void delete2dArray(long long ** dp, int N)
{
for(int i = 0; i <= (1 << N); i++)
{
delete(dp[i]);
}
delete(dp);
return;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
int main(){
string S;
cin >> S;
stack<char> st;
int si = S.size();
rep(i,si){
switch(S[i]){
case '6':S[i] = '9';
break;
case '9':S[i] = '6';
break;
}
st.push(S[i]);
}
rep(i,si){
cout << st.top();
st.pop();
}
cout << endl;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
using ll=long long;
int main()
{
string s;
cin>>s;
reverse(s.begin(),s.end());
int i;
for(i=0;i<s.size();i++)
{
if(s[i]=='0')
s[i]='0';
else if(s[i]=='1')
s[i]='1';
else if(s[i]=='6')
s[i]='9';
else if(s[i]=='8')
s[i]='8';
else s[i]='6';
}
cout<<s<<endl;
return 0;
}
|
//Ô¸Éñ zhoukangyang Ö¸ÒýÎÒ¡£
#include <stdio.h>
#include <string.h>
#include <queue>
#define int long long
inline int read()
{
int num=0,f=1;char c=getchar();
while(c<48||c>57){if(c=='-')f=-1;c=getchar();}
while(c>47&&c<58)num=(num<<3)+(num<<1)+(c^48),c=getchar();
return num*f;
}
inline int min(int a,int b){return a<b?a:b;}
inline int max(int a,int b){return a>b?a:b;}
struct Edge{
int v,w,nxt;
}e[2005];
int h[1005],cnt=1;
inline void addedge(int u,int v,int w)
{
e[cnt]=(Edge){v,w,h[u]};
h[u]=cnt++;
}
struct Node{
int u,v,fu,fv,dis;
};
std::queue<Node> q;
inline int gc()
{
char c=getchar();
while(c<'a'||c>'z')c=getchar();
return c;
}
int inq[1005][1005];
signed main()
{
int n=read(),m=read(),curans=0;
for(int i=0;i<m;i++)
{
int x=read(),y=read(),z=gc();
addedge(x,y,z);
addedge(y,x,z);
}
Node x=(Node){1,n,1,n,0};
q.push(x);inq[1][n]=1;
while(!q.empty())
{
x=q.front();q.pop();
int u=x.u,v=x.v,fu=x.fu,fv=x.fv,d=x.dis;
if(curans&&curans<d)
{
printf("%lld\n",curans);
return 0;
}
//printf("hi %lld %lld %lld!\n",u,v,d);
if(u==v){printf("%lld\n",d);return 0;}
for(int i=h[u];i;i=e[i].nxt)
{
if(e[i].v==v)
{
//printf("%lld\n",d+1);
//return 0;
curans=d+1;
goto nxx;
}
}
for(int i=h[u];i;i=e[i].nxt)
{
int V=e[i].v,w=e[i].w;
for(int j=h[v];j;j=e[j].nxt)
{
int vv=e[j].v,ww=e[j].w;
if(inq[V][vv]||inq[vv][V])continue;
if(V==fu&&vv==fv)continue;
if(w==ww)
{
inq[V][vv]=1;
q.push((Node){V,vv,u,v,d+2});
}
}
}
nxx:;
}
if(curans)printf("%lld\n",curans);
else puts("-1");
} | /* made by amunduzbaev */
//~ #include <ext/pb_ds/assoc_container.hpp>
//~ #include <ext/pb_ds/tree_policy.hpp>
#include "bits/stdc++.h"
using namespace std;
//~ using namespace __gnu_pbds;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(),x.rend()
#define NeedForSpeed ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define vv vector
#define tut(x) array<int, x>
#define int long long
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vii;
typedef vector<pii> vpii;
template<class T> bool umin(T& a, const T& b) {return a > b? a = b, true:false;}
template<class T> bool umax(T& a, const T& b) {return a < b? a = b, true:false;}
void usaco(string s) { freopen((s+".in").c_str(),"r",stdin);
freopen((s+".out").c_str(),"w",stdout); NeedForSpeed }
//~ template<class T> tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const int N = 1e3+5;
const int mod = 1e9+7;
const ll inf = 1e18;
const ld Pi = acos(-1);
#define MULTI 0
int n, m, k, s, t, q, ans, res, a[N];
int dis[N][N];
vv<pair<int, char>> edges[N];
void solve(int t_case){
cin>>n>>m;
for(int i=0;i<m;i++){
int a, b; char c; cin>>a>>b>>c;
edges[a].pb({b, c}), edges[b].pb({a, c});
} for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) dis[i][j] = mod;
dis[1][n] = 0;
queue<pii> qq;
qq.push({1, n});
while(!qq.empty()){
pii cur = qq.front(); qq.pop();
for(auto a : edges[cur.ff]){
for(auto b : edges[cur.ss]){
if(a.ss != b.ss) continue;
if(umin(dis[a.ff][b.ff], dis[cur.ff][cur.ss] + 1)) qq.push({a.ff, b.ff});
}
}
}
int res = mod;
for(int i=1;i<=n;i++){
umin(res, dis[i][i] * 2);
for(auto x : edges[i]) umin(res, dis[i][x.ff]*2 + 1);
} if(res != mod) cout<<res<<"\n";
else cout<<-1<<"\n";
}
signed main(){
NeedForSpeed
if(MULTI){
int t; cin>>t;
for(int t_case = 1; t_case <= t; t_case++) solve(t_case);
} else solve(1);
return 0;
}
|
#include <cstdio>
#include <algorithm>
using namespace std;
int n,a[5010][11],f[5010][5][41];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
for(int j=0;j<5;j++)
scanf("%d",&a[i][j]);
f[0][0][0]=1e9;
for(int i=1;i<=n;i++)
for(int j=0;j<=3;j++)
for(int k=0;k<32;k++)
{
f[i][j][k]=max(f[i][j][k],f[i-1][j][k]);
if(j)
for(int s=0;s<32;s++)
if(!(s&k))
{
int minn=1e9;
for(int l=0;l<5;l++)
if(s&(1<<l))
minn=min(minn,a[i][l]);
f[i][j][s|k]=max(f[i][j][s|k],min(f[i][j-1][k],minn));
}
}
printf("%d",f[n][3][31]);
} | #include <cassert>
#include <limits>
/*
find the maximum integer value that satisfies the given condition,
where if x satisfies it, every y (< x) satisfies it
template parameters:
Integer: an integer type
Function: a function object type
parameters:
a: the lower bound of the possible value
b: the upper bound of the possible value
cond: the function representing the condition
which take an integer value as an argument
and return true if the condition is satisfied; false otherwise
return value:
the maximum integer that satisfies the condition
requirements:
there exists a value in [a, b] that satisfies the condition
a <= b
std::numeric_limits<Integer>::min() <= 2*a && 2*b <= std::numeric_limits<Integer>::max()
*/
template <class Integer, class Function>
Integer MaxCond(Integer a, Integer b, Function cond)
{
// debug
assert(a <= b);
assert(a >= std::numeric_limits<Integer>::min() / 2); // this is sufficient but not necessary
assert(b <= std::numeric_limits<Integer>::max() / 2); // this is sufficient but not necessary
assert(cond(a)); // check the existence of a value in [a, b] that satisfies the condition
// body
while (a < b) {
Integer c = (a + b + 1) / 2;
if (cond(c)) {
a = c;
} else {
b = c - 1;
}
}
return a;
}
#include <iostream>
#include <vector>
#include <array>
int main()
{
int n;
std::cin >> n;
std::vector<std::array<int, 5>> member(n);
for (int i = 0; i < n; i++) {
std::cin >> member[i][0] >> member[i][1] >> member[i][2] >> member[i][3] >> member[i][4];
}
int ans = MaxCond(1, 1000000000, [&member, n](int strength) {
bool possible[32];
for (int i = 0; i < 32; i++) {
possible[i] = false;
}
for (int i = 0; i < n; i++) {
int member_possible = 0;
for (int j = 0; j < 5; j++) {
if (member[i][j] >= strength) {
member_possible |= 1 << j;
}
}
possible[member_possible] = true;
}
for (int i = 0; i < 32; i++) {
if (i == 31 && possible[i]) {
return true;
}
for (int j = i + 1; j < 32; j++) {
if ((i | j) == 31 && possible[i] && possible[j]) {
return true;
}
for (int k = j + 1; k < 32; k++) {
if ((i | j | k) == 31 && possible[i] && possible[j] && possible[k]) {
return true;
}
}
}
}
return false;
});
std::cout << ans << std::endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int main()
{
long long t, N;
cin >> t >> N;
long long a = 0;
long long b = 0;
vector<int> v;
while( a < 1000 ) {
a++;
long long c = (100 + t) * a / 100;
if( c - b > 1 ) {
v.push_back( a );
if( a == 100 ) break;
}
b = c;
}
int n = v.size();
a = N / n * 100;
int m = N % n;
if( m == 0 ) {
a -= 100;
m = n;
}
long long ans;
b = (100 + t) * a / 100;
while( 1 ) {
a++;
long long c = (100 + t) * a / 100;
if( c - b > 1 ) {
ans = b + 1;
m--;
if( m == 0 ) break;
}
b = c;
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
#include <unordered_set>
#define f(i, a, b) for (int i = a; i < b; i++)
#define rf(i, a, b) for (int i = a; i > b; i--)
#define lf(i, a, b) for (long long i = a; i < b; i++)
#define sq(a) (a)*(a)
#define cube(a) (a)*(a)*(a)
#define pb push_back
#define ppb pop_back
#define LB lower_bound
#define UB upper_bound
#define sarr(arr, n) sort(arr, arr + n);
#define svec(v) sort(v.begin(), v.end());
#define svecr(v) sort(v.rbegin(), v.rend());
#define intinp(var) int var; \
cin >> var;
#define adjc(adj, n) int adj[n];
#define adjcw(adj, n) vector<pair<int, int>> adj[n];
#define PYES cout << "YES\n"
#define PNO cout << "NO\n"
#define PYes cout << "Yes\n"
#define PNo cout << "No\n"
#define endl "\n"
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<pair<int, int>> vii;
typedef vector<string> vs;
typedef unordered_map<int, int> um;
int main(){
ll t, n; cin >> t >> n;
ll mn = (100*n)/t;
if ((100*n)%t) mn++;
cout << mn + n-1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int N, K;
int sum = 0;
cin >> N >> K;
for(int i = 1; i <= N; i++){
for(int j = 1; j <= K; j++){
sum += (i * 100 + j);
}
}
cout << sum << endl;
} | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long int
#define mod 1000000007
#define string_mod 2549536629329_base_255
#define pb push_back
#define F first
#define S second
#define ff first
#define endl "\n"
#define ss second
#define all(v) v.begin(), v.end()
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
int power(int x, unsigned int y, int p)
{
int res = 1;
x = x % p;
while (y > 0)
{
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res % p;
}
int lcm(int a, int b)
{
return a / __gcd(a, b) * b;
}
void solve()
{
int n,m;
cin>>n>>m;
int a[n][m];
int mn = 1e18;
for (int i = 0; i < n;i++){
for (int j = 0; j < m;j++){
cin >> a[i][j];
mn = min(mn, a[i][j]);
}
}
int ans = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
ans += a[i][j] - mn;
}
}
cout << ans << endl;
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tc;
tc = 1;
// cin >> tc;
while (tc--)
{
solve();
}
return 0;
} |
#include <bits/stdc++.h>
#define FOR(i, begin, end) for(int i=(begin);i<(end);i++)
#define IFOR(i, begin, end) for(int i=(end)-1;i>=(begin);i--)
#define rep(i, n) FOR(i,0,n)
#define irep(i, n) IFOR(i,0,n)
#define all(v) begin(v), end(v)
using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;
using PLL = pair<ll, ll>;
using VI = vector<int>;
using VLL = vector<ll>;
using VB = vector<bool>;
using VP = vector<P>;
using Graph2 = vector<vector<int>>;
using PQ = priority_queue<int>;
template<typename T>istream& operator>>(istream&s,vector<T>&v){for(auto &i: v)s>>i;return s;}
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
bool second_compare(P a,P b){if(a.second!=b.second){return a.second<b.second;} else return true;}
ll ch(ll a, ll b) {
ll ret = 1;
FOR(i, 1, b+1) {
ret *= a;
ret /= i;
a--;
}
return ret;
}
int main() {
ll a, b, k;
cin >> a >> b >> k;
string ans;
ll now = 0;
int n = a+b;
rep(i, n) {
if (a == 0) {
cout << ans;
rep(i, b) cout << 'b'; cout << endl;
return 0;
}
if (b == 0) {
cout << ans;
rep(i, a) cout << 'a'; cout << endl;
return 0;
}
--a;
ll c = ch(a+b, a);
if (now + c >= k) {
ans += 'a';
}
else {
ans += 'b';
++a;
--b;
now += c;
}
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define REP(i,s,n) for(int i=s;i<n;i++)
#define NUM 2520
#define INF (1LL<<50)
#define DEBUG 0
#define mp(a,b) make_pair(a,b)
#define SORT(V) sort(V.begin(),V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) # VariableName
#define LOG(x) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<endl;
#define LOG2(x,y) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<endl;
#define LOG3(x,y,z) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<" "<<TO_STRING(z)<<"="<<z<<endl;
#define LOG4(w,x,y,z) if(DEBUG)cout<<TO_STRING(w)<<"="<<w<<" "<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<" "<<TO_STRING(z)<<"="<<z<<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 (b < a) { a = b; return 1; } return 0; }
lli C[80][80];
void func(long long A, long long B, long long K){
string s;
while(B!=0 && A!=0){
lli lefSum = A+B;
LOG4(A,B,K,s);
lli border = C[lefSum-1][B];
LOG2(K,border);
if(border < K){
s.push_back('b');
B--;
K-=border;
}
else{
s.push_back('a');
A--;
}
}
while(B--)s.push_back('b');
while(A--)s.push_back('a');
cout<<s<<endl;
}
int main(){
// cout << fixed << setprecision(5);
long long A;
scanf("%lld",&A);
long long B;
scanf("%lld",&B);
long long K;
scanf("%lld",&K);
C[0][0]=1;
C[1][0]=1;
C[1][1]=1;
REP(i,2,62){
REP(j,0,i+1){
if(j==0)C[i][j]=1;
else{
C[i][j] = C[i-1][j-1] + C[i-1][j];
}
}
}
func(A, B, K);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
const long long MOD = 1000000007;
const long long INF = 9999999999999999;
using ll = long long;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
int main(){
int N,M;
cin>>N>>M;
int a[N];
rep(i,N){
cin>>a[i];
}
int mex = MOD;
vector <int> b(M);
rep(i,M){
b[i] = a[i];
}
sort(all(b));
if(b[0]!=0){
mex=0;
}
else{
bool p = 0;
for(int i = 1;i<M;i++){
if(b[i]-b[i-1]>1){
mex = b[i-1]+1;
p = 1;
break;
}
}
if(p == 0){
mex = b[M-1]+1;
}
}
int kazu[1500100];
rep(i,M){
kazu[a[i]]++;
}
// cout << mex << endl;
for(int i = M;i<N;i++){
kazu[a[i]]++;
kazu[a[i-M]]--;
if(kazu[a[i-M]] == 0){
// cout << a[i-M] << endl;
chmin(mex,a[i-M]);
}
}
cout << mex << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
string S, T;
cin >> S >> T;
int counts = 0, countt = 0;
for (int i = 0; i < N; i++) {
if (S[i] == '0') counts++;
if (T[i] == '0') countt++;
}
if (counts != countt) {
cout << -1 << endl;
return 0;
}
int ms = 0, mt = 0, count = 0;
for (int i = 0; i < counts; i++) {
while (S[ms] == '1') ms++;
while (T[mt] == '1') mt++;
if (ms != mt) count++;
ms++;
mt++;
}
cout << count << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define LL long long
#define READ freopen("in.txt","r",stdin)
#define WRITE freopen("out.txt","w",stdout)
#define BOOST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define pb push_back
#define mem(x,y) memset(x,y,sizeof x )
#define ch printf("Came Here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n")
#define deb(x) cerr<<#x<<" :: "<<x<<" "
#define dnl cerr<<endl;
#define FOR(i,n) for(int i=0;i<n;i++)
#define cnd tree[idx]
#define lc tree[idx*2]
#define rc tree[idx*2+1]
#define lnd (2*idx),(b),( (b+e) /2 )
#define rnd ((2*idx)+1),(((b+e)/2)+1),(e)
#define popcountL __builtin_popcountll
#define popcount __builtin_popcount
const LL INF = 1LL<<59;
const double PI = 2.0 * acos(0.0);
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;
////// Including Policy Based DS
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//
/////cout<<*X.find_by_order(1)<<endl;
/////cout<<X.order_of_key(-5)<<endl;
//typedef tree<
//LL,
//null_type,
//less< pll >,
//rb_tree_tag,
//tree_order_statistics_node_update>
//ordered_set;
// graph direction array [4]
int X[4]={0,0,-1,1};
int Y[4]={1,-1,0,0};
// graph direction array [8]
//int X[8]={0,0,1,-1,-1,1,1,-1};
//int Y[8]={1,-1,0,0,-1,-1,1,1};
// Bishop direction array [8]
//int BX[8]={0,0,1,-1,-1,1,1,-1};
//int BY[8]={1,-1,0,0,-1,-1,1,1};
// Knight Direction Array
//int KX[8] = {1,1,2,2,-1,-1,-2,-2};
//int KY[8] = {2,-2,1,-1,2,-2,1,-1};
inline bool checkBit(int N,int pos){return (bool)(N & (1<<pos));}
inline int setBit(int N,int pos){ return N=N | (1<<pos);}
inline int unsetBit(int N,int pos){ return N=( N & ( ~( 1<<pos ) ) );}
inline int toggleBit(int N,int pos){ return N = ( N ^(1<<pos) ); }
inline LL modMul(LL a, LL b,LL mod){
LL ans = 0;
a = a % mod;
while (b > 0){
if ( b % 2 )ans = (ans%mod+ a%mod) % mod;
a = (a%mod * 2%mod) % mod;
b /= 2;
}
return ans % mod;
}
inline LL powerMod(LL a,LL b,LL mod){
if(b==0)return 1LL%mod;
LL x=powerMod( a,b/2,mod );
x = modMul(x,x,mod) % mod;
if( b%2==1 )x = modMul(x,a,mod) % mod;
return x%mod;
}
inline LL lcm( LL a,LL b ){
LL g = __gcd(a,b);
return ((a/g) * b);
}
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
const int mx = 234567;
vll arr;
void solve(int cs){
int n;
LL k;
cin>>n>>k;
FOR(i,n){
LL a,b;cin>>a>>b;
arr.pb(pll(a,b));
}
sort(arr.begin(),arr.end());
LL curcoin = k;
LL mypos = 0;
for(int i=0;i<n;i++){
pll x = arr[i];
LL dist = x.first;
if( mypos+curcoin < dist ){
mypos += curcoin;
break;
}
else{
curcoin -= dist-mypos;
mypos = dist;
curcoin += x.second;
}
if( i==n-1 ){
mypos += curcoin;
}
// deb(i);deb(mypos);deb(curcoin);dnl;
}
cout<<mypos<<endl;
}
int main(){
BOOST;
#ifdef MujahidPC
double start = clock();
// READ;
// WRITE;
#endif
int t;
t = 1;
// cin>>t;
FOR(cs,t)solve(cs+1);
#ifdef MujahidPC
double tim = (clock() - start)/CLOCKS_PER_SEC;
cerr<<"Running Time : "<<tim<<" \n";
#endif
return 0;
} | //#include <atcoder/all>
//using namespace atcoder;
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for(int i=0; i<n; i++)
#define REPR(i, n) for(int i=n; i>=0; i--)
#define FOR(i, m, n) for(int i=m; i<n; i++)
#define ALL(v) v.begin(), v.end()
#define bit(n) (1LL<<(n))
#define FIX(d) fixed << setprecision(d)
using P = pair<int, int>;
using LP = pair<ll, ll>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
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 ll LINF = (1LL<<60);
template <typename T>
struct PrimeFact {
vector<T> spf;
PrimeFact(T N) { init(N); }
void init(T N) { // 前処理。spf を求める
spf.assign(N + 1, 0);
for (T i = 0; i <= N; i++) spf[i] = i;
for (T i = 2; i * i <= N; i++) {
if (spf[i] == i) {
for (T j = i * i; j <= N; j += i) {
if (spf[j] == j) spf[j] = i;
}
}
}
}
map<T, T> get(T n) { // nの素因数分解を求める
map<T, T> mp;
while (n != 1) {
mp[spf[n]]++;
n /= spf[n];
}
return mp;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int a,b; cin >> a >> b;
//PrimeFact pf(b);
int sub = b-a;
REPR(i,sub){
int low = (a/i)*i;
if(low < a) low += i;
int high = (b/i)*i;
if(low<high){
cout << i << endl;
return 0;
}
}
return 0;
}
|
#include <iostream>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <limits>
#include <string>
#include <algorithm>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include "math.h"
#include "assert.h"
using namespace std;
#define ll long long
#define config ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout.precision(20);
#define epsilon 1e-07
#define pi 2 * acos(0.0);
#define mod 1000000007 // 1e9+7
#define cerr if(false)cerr
#define watch(x) cerr << #x << " = " << x << endl;
#define iv vector<int>
#define xy pair<int, int>
#define x first
#define y second
#define inf std::numeric_limits<int>().max()
int main() {
config;
ll n;
cin >> n;
string t;
cin >> t;
ll sz = 1e10;
ll k = n / 3;
ll ans = 0;
if (n < 3) {
if (t == "0") ans = sz;
if (t == "1") ans = 2*sz;
if (t == "00") ans = 0;
if (t == "01") ans = sz-1;
if (t == "10") ans = sz;
if (t == "11") ans = sz;
} else {
set<string> s;
s.insert("110");
s.insert("011");
s.insert("101");
string p = t.substr(0,3);
if (s.count(p)) {
bool ok = true;
for(int i=0; i<k*3; i+=3) {
if (p != t.substr(i, 3)) ok = false;
}
if (ok) {
ll zeros = 0;
for(int i=0; i<n; i++) {
if (t[i] == '0') zeros++;
}
ans = sz - zeros;
if (t[t.size()-1] == '0') ans++;
}
}
}
cout << ans << '\n';
}
| #include<bits/stdc++.h>
using namespace std;
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define SZ(a) (int)(a.size())
#define ALL(a) a.begin(),a.end()
#define SET(a,b) memset(a,b,sizeof(a))
#define off ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
typedef pair<int,int> II;
typedef vector< II > VII;
typedef vector<int> VI;
typedef vector< VI > VVI;
typedef long long int LL;
typedef vector<LL> VL;
#define si(n) cin>>n
#define dout(n) cout<<n<<"\n"
#define DRT() int t; si(t); while(t--)
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
//# define M_PI 3.14159265358979323846264338327950288
// DSU
void IO(){
#ifndef ONLINE_JUDGE
freopen("../input.txt", "r", stdin);
freopen("../output.txt", "w", stdout);
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
off;
}
int main(){
IO();
LL s,p;
cin>>s>>p;
LL ok = 0;
for(LL i=1;i*i <= p;i++){
LL rem = p % i;
if(rem == 0){
LL d = p/i;
if(i + d == s)
ok = 1;
}
}
if(ok)
dout("Yes");
else
dout("No");
return 0;
} |
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <numeric>
#include <utility>
#include <tuple>
#define rep(i, a, b) for (int i = int(a); i < int(b); i++)
using namespace std;
using ll = long long int;
using P = pair<ll, ll>;
// clang-format off
#ifdef _DEBUG_
#define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; PPPPP(__VA_ARGS__); cerr << endl; } while(false)
template<typename T> void PPPPP(T t) { cerr << t; }
template<typename T, typename... S> void PPPPP(T t, S... s) { cerr << t << ", "; PPPPP(s...); }
#else
#define dump(...) do{ } while(false)
#endif
template<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }
template<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); }
template<typename T> bool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false; }
template<typename T> bool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false; }
template<typename T> void print(T a) { cout << a << '\n'; }
template<typename T, typename... Ts> void print(T a, Ts... ts) { cout << a << ' '; print(ts...); }
template<typename T> istream &operator,(istream &in, T &t) { return in >> t; }
// clang-format on
int popcount(int n) {
int res = 0;
while (n) {
res += (n & 1);
n >>= 1;
}
return res;
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n, m;
cin, n, m;
vector<int> edges(n, 0);
rep(i, 0, m) {
int a, b;
cin, a, b;
a--;
b--;
edges[a] |= (1 << b);
edges[b] |= (1 << a);
}
int sz = 1 << n;
vector<bool> clique(sz, false);
rep(mask, 0, sz) {
int msk = mask;
rep(i, 0, n) {
if ((mask >> i) & 1) {
msk &= (edges[i] | (1 << i));
}
}
clique[mask] = msk == mask;
}
const int inf = n + 1;
vector<int> dp(sz, inf);
rep(i, 0, sz) {
if (clique[i]) {
dp[i] = 1;
}
}
rep(i, 0, sz) {
for (int j = i;; j = (j - 1) & i) {
int k = i ^ j;
chmin(dp[i], dp[j] + dp[k]);
if (j == 0) break;
}
}
print(dp[sz - 1]);
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
const int N=20;
int g[N][N];
int dp[1<<N];
bool ok[1<<N];
int n,m;
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
x--,y--;
g[x][y]=g[y][x]=1;
}
for(int i=0;i<(1<<n);i++)
{
vector<int> e;
for(int j=0;j<n;j++)
{
if(i>>j&1) e.push_back(j);
}
ok[i]=true;
for(int j=0;j<e.size();j++)
for(int k=j+1;k<e.size();k++)
if(!g[e[j]][e[k]])
{
ok[i]=false;
}
}
for(int i=0;i<(1<<n);i++) dp[i]=n+1;
dp[0]=0;
for(int i=1;i<(1<<n);i++)
{
if(ok[i]) dp[i]=1;//只有一块
for(int j=i;j;j=(j-1)&i)
dp[i]=min(dp[i],dp[j]+dp[i^j]);
}
cout<<dp[(1<<n)-1];
return 0;
} |
#include <bits/stdc++.h>
template<class T> inline bool chmin(T&a, T b){if(a > b){a = b; return true;}else{return false;}}
template<class T> inline bool chmax(T&a, T b){if(a < b){a = b; return true;}else{return false;}}
#define ll long long
#define double long double
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for(int i=1;i<=(n);i++)
#define mod (ll)(1e9+7)
#define inf (ll)(3e18+7)
#define eps (double)(1e-9)
#define pi (double) acos(-1)
#define P pair<int,int>
#define PiP pair<int,pair<int,int>>
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
using namespace std;
void solve(){
ll n;
cin >> n;
if(n % 2 == 1)cout << "Odd" << endl;
else {
if(n % 4 == 0)cout << "Even" << endl;
else cout << "Same" << endl;
}
}
int main() {
int t;
cin >> t;
while(t--)solve();
} | #include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define ll long long int
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll t=1;
//cin>>t;
while(t--)
{
double n;
cin>>n;
n*=1.08;
n=(ll)n;
if(n>206)
{
cout<<":("<<endl;
}
else if(n==206)
{
cout<<"so-so"<<endl;
}
else
{
cout<<"Yay!"<<endl;
}
}
return 0;
} |
//#pragma GCC optimize ("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define pb push_back
#define F first
#define S second
#define ins insert
#define mp make_pair
#define fo(i, n1, n, x) for(int i = n1; i <= n; i += x)
#define foo(i, n, n1, x) for(int i = n; i >= n1; i -= x)
#define bit __builtin_popcount
#define md (l + ((r - l) / 2))
#define all(x) x.begin(),x.end()
#define eb emplace_back
#define ub upper_bound
#define lb lower_bound
#define ios ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
using namespace std;
//using namespace __gnu_pbds;
//using namespace __gnu_cxx;
using ll = long long;
//#define int ll
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<int>;
const int N = 2e5 + 11, mod = 1e9 + 7, mod2 = 998244353;
const int MAX = 1e5 + 11;
const int INF1 = 2e9 + 11;
const ll INF2 = 6e18 + 11;
const double INF3 = 1e8 + 11;
const int base = 500;
const int P = 31;
const int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};
const int dy[] = {0, 0, 1, -1, 1, -1, 1, -1};
const double EPS = 1e-4;
const double PI = acos(-1.0);
//template<typename T> using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int l, int r) { return uniform_int_distribution<int> (l, r)(rng); }
string s;
main() {
file("paint");
ios;
cin >> s;
deque <char> d;
bool t = 0;
for (char it : s) {
if (it == 'R') t ^= 1;
else {
if (!t) d.push_back(it);
else d.push_front(it);
}
}
stack <char> st;
string ans = "";
while (!d.empty()) {
char x;
if (!t) x = d.front(), d.pop_front();
else x = d.back(), d.pop_back();
if (ans.size() and ans.back() == x) ans.pop_back();
else ans += x;
}
cout << ans;
return 0;
}
/**
*/
| #include <bits/stdc++.h>
#include <variant>
#define rep2(i,k,n) for(i64 i=(i64)(k);i<(i64)(n);i++)
#define rep(i,n) rep2(i,0,n)
#define all(x) begin(x),end(x)
#ifdef ENV_LOCAL
#define dump if (1) cerr
#else
#define dump if (0) cerr
#endif
using namespace std;
using namespace std::string_literals;
using i32 = int32_t;
using i64 = int64_t;
using f64 = double;
using f80 = long double;
using vi32 = vector<i32>;
using vi64 = vector<i64>;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string s;
cin>>s;
deque<char> q;
bool rev = false;
for (auto&& ch : s) {
if (ch == 'R') rev = !rev;
else {
if (rev) q.push_front(ch);
else q.push_back(ch);
}
}
if (rev) {
reverse(all(q));
}
vector<char> ans;
for (auto&& ch : q) {
if (ans.empty() || ans.back() != ch) {
ans.push_back(ch);
} else {
ans.pop_back();
}
}
for(auto&& ch : ans) {
cout<<ch;
}
cout<<endl;
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
const int N=500005;
typedef long long ll;
int n,p[N],q[N],a[N],b[N],i,id[N];
bool cmp(int x,int y)
{
return b[x]>b[y];
}
struct str{
int u,v;
};
vector<str> g;
int main()
{
scanf("%d",&n);
for(i=1;i<=n;++i)
scanf("%d",&a[i]);
for(i=1;i<=n;++i)
{
scanf("%d",&b[i]);
id[i]=i;
}
sort(id+1,id+1+n,cmp);
for(i=1;i<=n;++i)
{
scanf("%d",&p[i]);
q[p[i]]=i;
}
for(i=1;i<=n;++i)
{
int x=id[i];
if(q[x]!=x)
if(a[q[x]]>b[x]&&a[x]>b[p[x]])
{
g.push_back((str){x,q[x]});
int u=p[x],v=q[x];
q[u]=v;
p[v]=u;
}
else
{
puts("-1");
return 0;
}
}
printf("%d\n",g.size());
for(auto it:g)
printf("%d %d\n",it.u,it.v);
}
| #include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
#include<cmath>
#include<cstdio>
#include<tuple>
#include<bitset>
#include<map>
using namespace std;
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;++i)
#define ALL(x) x.begin(),x.end()
#define ll long long
using lint=long long;
#define debug(output) cout<<#output<<"= "<<output<<endl
typedef pair<int,int> P;
const int inf=1000000007;
const int MOD=1000000007;
signed main(){
int n;cin>>n;
vector<int> a(n),b(n),p(n),memo(n,0);
rep(i,n)cin>>a[i];
rep(i,n)cin>>b[i];
rep(i,n)cin>>p[i];
rep(i,n)--p[i];
rep(i,n)memo[p[i]]=i;
priority_queue<P,vector<P>,greater<P>> que;
rep(i,n){
que.push({a[i],i});
}
vector<P> res;
while(!que.empty()){
auto [w,i]=que.top();que.pop();
if(i==p[i])continue;
else if(a[i]<=b[p[i]]||a[memo[i]]<=b[i]){
cout<<-1<<"\n";
return 0;
}
int hoge=p[i];
res.push_back({i,memo[i]});
swap(p[i],p[memo[i]]);
swap(memo[i],memo[hoge]);
}
cout<<res.size()<<"\n";
rep(i,res.size()){
cout<<res[i].first+1<<" "<<res[i].second+1<<"\n";
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
// head
const int N = 3e5 + 5;
int a[N], n;
int main(void){
scanf("%d", &n);
ll sum1 = 0, sum2 = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
sum1 += a[i];
sum2 += a[i] * a[i];
}
ll ans = (n - 1) * sum2 - (sum1 * sum1 - sum2);
printf("%lld\n", ans);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
map<long long,long long> height;
long long num =0;
height[0]++;
unsigned long long count =0;
for(int i=0;i<n;i++){
if(i%2==0) {
num += a[i];
height[num] ++;
}else{
num -= a[i];
height[num] ++;
}
}
for (auto i:height){
//cout<< i.first <<' '<< i.second <<endl;
count += i.second*(i.second-1)/2;
}
cout << count << endl;
}
|
#include<bits/stdc++.h>
#define MAXN 300005
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
int n;
ll a[MAXN],maxl[MAXN],sum[MAXN],val[MAXN];
ll f(ll len){return maxl[len] * len + sum[len] + val[len];}
int main(){
cin>>n;for(int i = 1 ; i <= n ; i++)cin>>a[i];
maxl[0] = -INF;
for(int i = 1 ; i <= n ; i++){
maxl[i] = max(maxl[i - 1] , a[i]);
sum[i] = sum[i - 1] + a[i];
val[i] = val[i - 1] + a[i - 1];
}
for(int i = 1 ; i <= n ; i++)val[i] = val[i] + val[i - 1];
for(int i = 1 ; i <= n ; i++)cout<<f(i)<<endl;
} | #include "bits/stdc++.h"
using namespace std;
#define ffor(n) for(int i = 0; i < n; i++)
#define fffor(n) for(int j = 0; j < n; j++)
#define uwu ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize("Ofast")
const int INF = 1e9 + 7;
const long long INF2 = 1e17;
int main(void) {
uwu
// when adding some integer to v, it only affects the answer if it is larger than any other previous element
// v[0] = a[0] + x, v[1] = a[1] + (a[0] + x), v[2] = a[2] + (a[0] + a[1] + x), ...
// for n terms, sum is n * x + (a[0] + a[0] + a[1] + a[0] + a[1] + a[2] + ...)
int n; cin >> n;
vector <long long> v(n);
ffor(n) cin >> v[i];
vector <long long> prefix(n);
prefix[0] = v[0];
for(int i = 1; i < n; i++) prefix[i] = prefix[i - 1] + v[i];
long long current_sum = 0, maximum = 0;
for(int i = 0; i < n; i++) {
maximum = max(maximum, v[i]);
current_sum += prefix[i];
cout << (i + 1) * maximum + current_sum << '\n';
}
}
/*
C:\Users\kenne\OneDrive\Desktop\competitive_programming\main.cpp
*/
|
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <map>
#include <sstream>
#include <cstring>
#include <set>
#include <cctype>
#include <list>
#include <bitset>
#define IO \
ios::sync_with_stdio(false); \
// cin.tie(0); \
// cout.tie(0);
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int maxn = 2e5 + 100;
const int maxm = 1e6 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
// int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
struct Edge
{
int next, to;
} e[maxn << 2];
int head[maxn];
LL a[maxn];
LL dp[maxn];
LL ans = -2 * mod;
int in[maxn];
int n, m;
int k = 0;
void add(int u, int v)
{
e[k].next = head[u];
e[k].to = v;
head[u] = k++;
}
void topo()
{
queue<int> q;
for (int i = 1; i <= n; i++)
if (in[i] == 0)
{
q.push(i);
}
while (!q.empty())
{
int u = q.front();
q.pop();
if (a[u] - dp[u] > ans)
ans = a[u] - dp[u];
// dp[u] = min(dp[u], a[u]);
for (int i = head[u]; ~i; i = e[i].next)
{
int v = e[i].to;
dp[v] = min(dp[v], min(dp[u], a[u]));
in[v]--;
if (in[v] == 0)
q.push(v);
}
}
return;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif
IO;
int x, y;
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
head[i] = -1;
dp[i] = 1e10;
}
for (int i = 0; i < m; i++)
{
cin >> x >> y;
add(x, y);
in[y]++;
}
topo();
cout << ans;
return 0;
}
| //Bismillahirrahmanirrahim
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long lo;
typedef pair< lo,lo > PII;
#define fi first
#define int long long
#define se second
#define mp make_pair
#define endl "\n"
#define pb push_back
#define fio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define FOR for(int i=1;i<=n;i++)
#define mid ((start+end)/2)
#define ort ((bas+son)/2)
const lo inf = 1000000000000000000;
const lo KOK = 100000;
const lo LOG = 30;
const lo li = 500005;
const lo mod = 1000000007;
int n,m,b[li],a[li],k,flag,t,dp[li],vis[li];
int cev;
string s;
vector<int> v[li];
inline void dfs(int node){
if(vis[node])return ;
vis[node]=1;
for(auto go:v[node]){
dfs(go);
dp[node]=max(dp[node],max(a[go],dp[go]));
}
cev=max(cev,dp[node]-a[node]);
}
int32_t main(void){
scanf("%lld %lld",&n,&m);
FOR scanf("%lld",&a[i]);
FOR dp[i]=-inf;
for(int i=1;i<=m;i++){
int x,y;
scanf("%lld %lld",&x,&y);
v[x].pb(y);
//~ v[y].pb(x);
}
cev=-inf;
FOR{
if(vis[i])continue;
dfs(i);
}
printf("%lld\n",cev);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
//first second
//memset(dp, -1, sizeof dp);
//string binary =bitset<8>(n).to_string();
//(mod)->(10*mod+currentmodulo)%m
struct keycompare {
bool operator()(const pair<ll, ll>& v, const ll& k) {
return (v.first < k);
}
bool operator()(const ll& k, const pair<ll, ll>& v) {
return (k < v.first);
}
};
ll mod1=998244353,mod2=1000000007,limit=9223372036854775807;
long double pi=3.1415926535897932;
ll modpow(ll x, ll n, ll m) {
if(x>m){
x%=m;
}
if (n == 0) return 1%m;
ll u = modpow(x,n/2,m);
u = (u*u)%m;
if (n%2 == 1) u = (u*x)%m;
return u;
}
ll gcd(ll a, ll b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
bool isprime(ll n){
if(n==2)
return true;
for(ll i=2;i*i<=n;i++){
if(n%i==0)
return false;
}
return true;
}
ll power(ll x,ll n){
if(n<0){
return 0;
}
ll x_n=1;
for(ll i=0;i<n;i++){
x_n*=x;
}
return x_n;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n,m; cin>>n>>m;
ll k=modpow(10,n,m);
ll mm=m*m;
for(int i=0;i<m;i++){
ll temp=modpow(10,n,mm)-k%mm-(m*i)%mm;
temp=temp%mm;
if(temp==0){
cout<<i<<endl; return 0;
}
}
} | #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define LL long long
using namespace std;
const int N = 55;
int t;
LL n, m;
LL qp(LL a, LL b)
{
LL res = 1;
while (b)
{
if (b & 1) res = res * a % (m * m);
b >>= 1;
a = a * a % (m * m);
}
return res;
}
int main()
{
scanf("%lld%lld", &n, &m);
LL res = qp(10, n);
printf("%lld\n", res / m);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main(){
int H, W; cin >> H >> W;
int A[100][100];
int m=1000;
for(int i=0; i<H; i++){
for(int j=0; j<W; j++){
cin >> A[i][j];
m=min(m, A[i][j]);
}
}
int r=0;
for(int i=0; i<H; i++){
for(int j=0; j<W; j++){
r+=(A[i][j]-m);
}
}
cout << r << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long int ll;
using vi = vector<int>;
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int h,w; cin >> h >>w;
vector<vector<int> > a(h,vector<int>(w));
int mn = 1000;
int sum = 0;
rep(i,h)rep(j,w) {
cin >> a[i][j];
mn = min(a[i][j],mn);
sum += a[i][j];
}
cout << sum - mn * h * w <<"\n";
return 0;
} |
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
using namespace std;
int main() {
long int N;
cin >> N;
set<long int> re;
for (int b = 2; b < int(sqrt(N)) + 2; ++b) {
for (int e = 2; e < 40; ++e) {
long int v = static_cast<long int>(pow(b,e));
if (v <= N && v > 0) re.insert(v);
else break;
}
}
cout << N - re.size() << "\n";
}
| #include <bits/stdc++.h>
using namespace std;
#define N 1000005
#define MAX 1e18
vector<long int> powers;
set<long int> squares;
set<long int> s;
void powersPrecomputation()
{
for (long int i = 2; i < N; i++)
{
squares.insert(i * i);
if (squares.find(i) != squares.end())
continue;
long int temp = i;
while (i * i <= MAX / temp)
{
temp *= (i * i);
s.insert(temp);
}
}
for (auto x : s)
powers.push_back(x);
}
long int calculateAnswer(long int R)
{
long int perfectSquares = floor(sqrtl(R));
long int high = (upper_bound(powers.begin(),
powers.end(), R) - powers.begin());
long int low = (lower_bound(powers.begin(),
powers.end(), 1) - powers.begin());
perfectSquares += (high - low);
return perfectSquares;
}
int main()
{
powersPrecomputation();
long int L{1};
long int R;
cin>>R;
cout << R+1-calculateAnswer(R);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL INF = 0x3f3f3f3f;
void prepare() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
vector<LL> arr;
int N, Q;
LL K;
map<LL, LL> m;
LL f() {
if(arr[0]>K) {
return K;
}
LL l = 0, r = arr.size();
while(l<r) {
LL mid = (l+r) >> 1;
LL x = mid<arr.size()?arr[mid]:INT_MAX;
if(x-mid-1>=K) {
r = mid;
} else {
l = mid+1;
}
}
return K-(arr[l-1]-(l-1)-1)+arr[l-1];
}
int main() {
prepare();
cin >> N >> Q;
for(int i=0; i<N; i++) {
LL t;
cin >> t;
arr.push_back(t);
}
sort(arr.begin(), arr.end());
while(Q--) {
cin >> K;
if(m.find(K)!=m.end()) {
cout << m[K] << endl;
}
LL ans;
ans = f();
m[K] = ans;
cout << ans << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define rep(i,n) for(int i=0;i<(n);i++)
int main() {
ll N,Q;
cin >> N>>Q;
vector<ll> A,B;
ll last=0,last_b=0;
rep(i,N){
ll temp;
cin>>temp;
A.push_back(temp);
B.push_back(last_b+temp-last-1);
last_b=last_b+temp-last-1;
last=temp;
}
/*
3,5,6,7
2,3,3,3みたいにしていくっぽい??
→1,2,4,8,9
今(2,3)(3,7)みたいになってる
4を取得したければ
→
*/
map<ll,ll> C;
rep(i,N){
C.emplace(B[N-1-i],A[N-i-1]);
}
//Kqより小さいキーの値を取得して、それに(Kq-key)の値を足せばそれが答え
//それをどうやって探すのか
rep(i,Q){
ll K,answer=0;
cin>>K;
auto be=C.lower_bound(K);
if(be==C.begin()){
answer=K;
}else{
be--;
answer=(be->second)+K-(be->first);
}
cout << answer << endl;
}
} |
#include <bits/stdc++.h>
#include<iostream>
#include<string.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
// helping defines
#define ll long long int
#define ull unsigned long long
#define max3(a, b, c) max(a, b) > max(b, c) ? max(a, b) : max(b, c)
#define min3(a, b, c) min(a, b) < min(b, c) ? min(a, b) : min(b, c)
#define lcm(a,b) a*b/__gcd(a,b)
#define gcd(a,b) __gcd(a,b)
#define sortD(a,n) sort(a,a+n,greater<int>())
#define sortA(a,n) sort(a,a+n)
//vectors & pairs
#define sv(a) sort(a.begin(),a.end())
#define pb(x) push_back(x)
#define eb(x,y) emplace_back(x,y)
#define vi vector<int>
#define vll vector<ll>
#define vvi vector<vector<int>>
#define vvll vector<vector<ll>>
#define vpll vector<pair<ll,ll>>
#define vp vector<pair<int,int>>
#define mp make_pair
#define ii pair<int,int>
#define sortVA(a) sort(a.begin(),a.end())
#define sortVD(a) sort(a.rbegin(),a.rend())
//map
#define um unordered_map
#define f first
#define s second
#define mapit unordered_map<int,int>:: iterator
//constants
#define pi 3.1415926536
#define MOD 1000000007
#define MAX (1e9 + 5)
#define MIN -(1e9 + 5)
#define endl "\n"
//loop
#define ff(i,s,e) for(i=s; i<e; i++)
#define rff(i,s,e) for(i=e; i>=s; i--)
#define vff(a) for(auto x: a)
#define read(a) for(auto &x: a) cin>>x;
#define put(a) for(auto &x: a) cout<<x<<" "; cout<<endl;
// global variables
int i,j;
//helper functions
ll ceil_div(ll x, ll y){
return (x + y - 1) / y;
}
//coding starts here
void test_cases(){
int n,k;
cin>>n>>k;
while(k--){
vi a;
while(n>0){
a.pb(n%10);
n/=10;
}
vi temp1=a;
vi temp2=a;
sortVA(temp1);
sortVD(temp2);
// put(temp1);
int x=0,y=0;
int l=a.size();
int pow10=pow(10,(l-1));
ff(i,0,l){
x+=temp1[i]*pow10;
y+=temp2[i]*pow10;
pow10=pow10/10;
}
// cout<<x<<" "<<y<<endl;
n=(y-x);
// cout<<n<<endl;
}
cout<<n<<endl;
return;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int T=1;
// cin>>T;
while(T--){
test_cases();
}
return 0;
}
| #include <algorithm>
#include <bits/stdc++.h>
#include <string>
using namespace std;
int main() {
ios::sync_with_stdio(false);
long long n,k;
cin >> n >> k;
long long a = n;
for (long long i=0; i<k; i++) {
int big,sma;
string aa;
aa = to_string(a);
sort(aa.begin(), aa.end(), greater<int>());
big = stoi(aa);
sort(aa.begin(), aa.end());
sma = stoi(aa);
a = big -sma;
}
cout << a ;
cout << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<long long>;
using vvl = vector<vector<long long>>;
#define rep(i, s, n) for (int i = (int)s; i < (int)n; i++)
#define repr(i, n, s) for (int i = (int)n; (int)s < i; i--)
#define len(a) (int)a.size()
#define all(a) a.begin(), a.end()
/* 1次元ベクトルをスペースでつなぎ、改行をつけて出力 vi, vl, list<int>, list<ll>に対応 */
template<class T> void print_elements(T a) {string str = ""; for (auto itr = a.begin(); itr != a.end(); ++itr) \
{str += to_string(*itr) + ' ';} str.pop_back(); cout << str << endl;}
const int INF32 = 1 << 29;
const long long INF64 = 1LL << 60;
const long long MOD = 1000000009;
const double PI = acos(-1);
int main() {
int a, b, c;
cin >> a >> b >> c;
if (a == c) {
cout << b << endl;
}
else if (a == b) {
cout << c << endl;
}
else if (b == c) {
cout << a << endl;
}
else {
cout << 0 << endl;
}
}
| #include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
/*--------------------------Containers------------------------*/
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector < int > vi;
typedef vector < ll > vll;
typedef vector < vll > vvll;
typedef pair< int,int > pi;
typedef pair< ll, ll > pll;
typedef vector< pi > vpi;
typedef vector< pll > vpll;
typedef map<int,int> mi;
typedef map<ll,ll> mll;
typedef map<pi,int> mpi;
typedef map<pll,ll> mpll;
/*------------------------------------------------------------*/
/*--------------------------Limits & Constants----------------*/
#define MAX 200007
#define MOD int(1e9+7)
#define INF ll(8230219982008199610)
#define MINF ll(-8230219982008199610)
#define IL INT_MAX
#define MIL INT_MIN
#define PI 2*acos(0.0)
/*------------------------------------------------------------*/
/*--------------------------Macros----------------------------*/
#define pf printf
#define sc scanf
#define sz(x) int(x.size())
#define sortv(v) sort(begin(v), end(v))
#define sortrv(v) sort(rbegin(v), rend(v))
#define all(nums) nums.begin(), nums.end()
#define allr(nums) nums.rbegin(), nums.rend()
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define ff first
#define ss second
#define mem(a,n) memset(a, n, sizeof(a) )
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define fori0(n) for(int i=0; i<n; i++)
#define fori1(n) for(int i=1; i<=n; i++)
#define forj0(n) for(int j=0; j<n; j++)
#define forj1(n) for(int j=1; j<=n; j++)
#define rev0(n) for(int i=n-1; i>=0; i--)
#define rev1(n) for(int i=n; i>0; i--)
#define forit(vec) for(auto it=vec.begin();it!=vec.end();it++)
#define forx(vec) for(auto x:vec)
/*------------------------------------------------------------*/
/*--------------------------Debugging------------------------*/
#define ___debug(a, b, c) cout << a << " " << b << " " << c << "\n"
#define __debug(a, b) cout << a << " " << b << "\n"
#define _debug(a) cout << a << "\n"
template <typename T>
void print(vector<T> v) { for (T &i : v) cout << i << " "; cout << "\n";}
template <typename T>
void read(vector<T> &v) { for (T &i : v) cin >> i; }
/*------------------------------------------------------------*/
void solve() {
int a, b, c;
cin >> a >> b >> c;
if (a == b) {
_debug(c);
}
else if (a == c) {
_debug(b);
}
else if (b == c) {
_debug(a);
}
else {
_debug(0);
}
}
int main() {
IOS;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int tcs = 1;
// cin >> tcs;
while (tcs--)
solve();
return 0;
}
//g++ -std=c++17 cf.cpp -o a.exe |
#include <bits/stdc++.h>
using namespace std;
#define tani_nachi_ke ios_base::sync_with_stdio(false); cin.tie(NULL);
#define M_PI 3.14159265358979323846
#define data data_
#define ff first
#define ss second
#define pb push_back
#define int long long
int32_t main()
{
tani_nachi_ke
int x;
cin >> x;
if(x >= 0)
cout << x << endl;
else
cout << 0 << endl;
return 0;
}
| #include <bits/stdc++.h>
int main(){
int x;
std::cin >>x;
std::cout << std::max({x,0}) << std::endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define debug(x) cerr << "L" << __LINE__ << ": " << #x << " = " << (x) << endl
#define debugvec(v) rep(i, v.size()) cerr << "L" << __LINE__ << ": " << #v << "[" << i << "] = " << v[i] << endl;
#define debugvec2(v) cerr << "L" << __LINE__ << ": " << #v << " = { "; rep(i, v.size()) cerr << v[i] << (i + 1 == v.size() ? "" : ", "); cerr << " }" << endl;
typedef long long ll; /* 10^18 くらいまでいける */
typedef unsigned long long ull; /* 10^18 くらいまでいける */
/* https://blog.hamayanhamayan.com/entry/2021/02/20/233715
*
* M を n進数表記に変換して比較する
*/
string X;
ll M;
bool check(ll n) {
vector<ll> Y; // Mをn進数表記に変換
ll m = M;
while (0 < m){
Y.push_back(m % n);
m /= n;
}
reverse(Y.begin(), Y.end());
// 例えばnが2進数や8進数の場合, 桁数が増えることがある.
if (X.size() < Y.size())
return true;
// 例えばnが16進数の場合, 桁数が減ることがある.
else if (X.size() > Y.size())
return false;
// 桁数が同じ時は大きい桁から1桁ずつ比較する
for(ll i = 0; i < X.size(); i++){
ll x = X[i] - '0';
ll y = Y[i];
if (x == y) continue;
else if (x < y) return true;
else return false;
}
// X == Y
return true;
}
int main(){
cin >> X;
cin >> M;
// もっとも大きい数字dを取得
char d_char = '0';
for (ll i = 0; i < X.length(); i++){
d_char = X[i] > d_char ? X[i] : d_char;
}
ll d = d_char - '0';
if (X.length() == 1){
ll x = stoll(X);
if (x <= M)
cout << "1" << endl;
else
cout << "0" << endl;
return 0;
}
// 2部探索
ll min_v = d;
ll max_v = 2e18; // 問題の制約の最大値
ll mid_v;
// 2部探索はhighとlowの差が1になるまでwhileを回す
while (min_v + 1 < max_v){
mid_v = (min_v + max_v) / 2;
// printf("min_v: %lld, mid_v: %lld, max_v: %lld\n", min_v, mid_v, max_v);
if (check(mid_v)){
min_v = mid_v;
}else{
max_v = mid_v;
}
}
mid_v = (min_v + max_v) / 2;
// printf("min_v: %lld, mid_v: %lld, max_v: %lld\n", min_v, mid_v, max_v);
cout << mid_v - d << endl;
}
| #include <bits/stdc++.h>
#define endl "\n"
#define int long long
#define debug(statement) \
if (debugging) \
cerr << statement << endl
#define debug_variable(var) \
if (debugging) \
cerr << (#var) << ": " << var << endl
const bool debugging = 1;
using namespace std;
template <class T>
ostream &operator<<(ostream &os, vector<T> V)
{
for (auto v : V)
{
os << v << " ";
}
return os << "";
}
template <class T>
ostream &operator<<(ostream &os, set<T> S)
{
for (auto s : S)
{
os << s << " ";
}
return os << "";
}
template <class L, class R>
ostream &operator<<(ostream &os, pair<L, R> P)
{
return os << P.first << " " << P.second;
}
template <class L, class R>
ostream &operator<<(ostream &os, map<L, R> M)
{
os << endl;
for (auto m : M)
{
os << m << endl;
}
return os << "";
}
long long binaryExponentiation(long long base, long long power)
{
//cout << base << " " << power << endl;
long long result = 1;
while (power > 0)
{
if (power & 1)
{
result = result * base;
}
base = base * base;
power = power >> 1;
}
return result;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string ex;
cin >> ex;
int N = ex.size();
int M;
cin >> M;
int d = 0;
for (int i = 0; i < N; i++)
{
d = max(d, (int)(ex[i] - '0'));
}
if (N == 1)
{
if (d + 1 > M && M != ((int)(ex[0] - '0')))
{
cout << 0 << endl;
}
else
{
cout << 1 << endl;
}
return 0;
}
if (d + 1 >= M)
{
cout << 0 << endl;
return 0;
}
int l = d + 1;
int r = M;
int ans = 0;
while (l <= r)
{
bool overflow = 0;
int mid = (r - l) / 2 + l;
//cout << mid << endl;
int res = binaryExponentiation(mid, N - 1);
if (log10(mid) * (N - 1) > 18)
{
r = mid - 1;
continue;
}
int total = 0;
for (int i = 0; i < N; i++)
{
if ((res * ((int)(ex[i] - '0'))) / res != ((int)(ex[i] - '0')) || (total + ((int)(ex[i] - '0')) * ex[i]) - ((int)(ex[i] - '0')) * ex[i] != total)
{
overflow = 1;
break;
}
total += (res * ((int)(ex[i] - '0')));
res /= mid;
}
if (total <= M && !overflow)
{
//cout << "WOOSYFACE" << endl;
ans = mid;
l = mid + 1;
}
else
{
r = mid - 1;
}
}
if (ans == 0)
{
cout << 0 << endl;
}
else
{
cout << ans - d << endl;
}
//cout << l << endl;
//cout << r << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
using namespace chrono;
typedef int64_t ll;
typedef long double ld;
typedef unsigned long long ULL;
#define endl "\n"
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb push_back
void read(vector<int> &a) {for (auto &x : a)cin >> x;}
void read(vector<ll> &a) {for (auto &x : a)cin >> x;}
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c> {i, j}; }
sim > auto dud(c * x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifndef ONLINE_JUDGE
~debug() { cerr << endl; }
eni( != ) cerr << boolalpha << i; ris;
}
eni( == ) ris << range(begin(i), end(i));
}
sim, class b dor(pair < b, c > d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
const int MOD = 1e9 + 7;
const int INF = (int)2e9 + 7;
const ll LINF = (ll)1e18;
const ld PI = 3.1415926535897932384626433832795;
void solve() {
int n;
cin >> n;
set<int> s;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
s.insert(x);
}
((int)s.size() == n ? cout << "Yes" : cout << "No");
}
int32_t main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
//cin >> t;
auto t1 = high_resolution_clock::now();
for (int tt = 1; tt <= t; tt++)
{
//cout << "Case #" << tt << ": ";
solve();
}
auto t2 = high_resolution_clock::now();
auto time = duration_cast<duration<double>>(t2 - t1);
cerr << "Time elapsed = " << time.count() << endl;
}
| #include <bits/stdc++.h>
using namespace std;
char str[500005];
deque<char> dq;
int main() {
scanf(" %s", &str);
bool b = true;
for (int i = 0; str[i]; ++i) {
if (str[i] == 'R') b = !b;
else if (b) {
if (!dq.empty() && str[i] == dq.back()) dq.pop_back();
else dq.push_back(str[i]);
}
else {
if (!dq.empty() && str[i] == dq.front()) dq.pop_front();
else dq.push_front(str[i]);
}
}
if (!b) reverse(dq.begin(),dq.end());
for (char c: dq) printf("%c", c);
return 0;
}
|
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
#define ll long long
#define FOR(i, a, b) for(ll i=(a);i<(b);++i)
#define rep(i, n) FOR(i, 0, n)
#define rep1(i, n) FOR(i, 1, n+1)
#define rrep(i, n) for (ll i = ((int)(n)-1); i >= 0; --i)
#define whole(x) (x).begin(),(x).end()
#define rwhole(x) (x).rbegin(), (x).rend()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define P pair<ll, ll>
#define debug(var) cerr << "[" << #var << "] " << var << '\n'
template<typename T1, typename T2>
bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}
template<typename T1, typename T2>
bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define pr(s) cout << (s) << '\n'
const ll mod = 1000000007;
const int dx[] = {-1,0,1,0};
const int dy[] = {0,-1,0,1};
const int INF = 1001001001;
const ll INFll = 1E+18;
struct mint {
ll x; // typedef long long ll;
mint(ll x=0):x((x%mod+mod)%mod){}
mint operator-() const { return mint(-x);}
mint& operator+=(const mint a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint a) {
if ((x += mod-a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint a) const {
mint res(*this);
return res+=a;
}
mint operator-(const mint a) const {
mint res(*this);
return res-=a;
}
mint operator*(const mint a) const {
mint res(*this);
return res*=a;
}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t>>1);
a *= a;
if (t&1) a *= *this;
return a;
}
// for prime mod
mint inv() const {
return pow(mod-2);
}
mint& operator/=(const mint a) {
return (*this) *= a.inv();
}
mint operator/(const mint a) const {
mint res(*this);
return res/=a;
}
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
struct combination {
vector<mint> fact, ifact;
combination(int n):fact(n+1),ifact(n+1) {
assert(n < mod);
fact[0] = 1;
for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
ifact[n] = fact[n].inv();
for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
}
mint operator()(int n, int k) {
if (k < 0 || k > n) return 0;
return fact[n]*ifact[k]*ifact[n-k];
}
mint per(int n, int k) {
return fact[n]*ifact[n-k];
}
};
mint f(ll a, ll b) {
if (a>b) swap(a, b);
mint ans = mint((a+b)*(b-a+1)/2);
return ans;
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
ll t;
cin >> t;
rep(ti, t) {
ll n, a, b;
cin >> n >> a >> b;
if (a>b) swap(a, b);
if (a+b>n) {
pr(0);
continue;
}
mint ans = mint(n-a+1)*mint(n-a+1)*mint(n-b+1)*mint(n-b+1);
//debug(ans);
mint sub = 0;
sub += (n-a+1)*(n-b+1);
sub -= mint(n-a-b+2)*mint(n-a-b+1);
sub *= sub;
ans -= sub;
pr(ans);
}
return 0;
}
| #pragma GCC optimize("Ofast")
#if 1
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
using ll = int64_t;
using db = double;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vector<int>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vpii = vector<pii>;
using vpll = vector<pll>;
constexpr char newl = '\n';
#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define F0R(i,b) FOR(i,0,b)
#define RFO(i,a,b) for (int i = ((b)-1); i >=(a); i--)
#define RF0(i,b) RFO(i,0,b)
#define show(x) cerr << #x << " = " << (x) << '\n';
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define sz(x) (int)(x).size()
#define YesNo(x) cout<<((x)?"Yes":"No")<<newl;
#define YESNO(x) cout<<((x)?"YES":"NO")<<newl;
#define v(T) vector<T>
#define vv(T) vector<vector<T>>
template<class A, class B> using umap = unordered_map<A, B>;
template<class A> using uset = unordered_set<A>;
template<typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }
template<class T> bool lcmp(const pair<T, T>& l, const pair<T, T>& r) {
return l.first < r.first;
}
template<class T> istream& operator>>(istream& i, v(T)& v) {
F0R(j, sz(v)) i >> v[j];
return i;
}
template<class A, class B> istream& operator>>(istream& i, pair<A, B>& p) {
return i >> p.first >> p.second;
}
template<class A, class B, class C> istream& operator>>(istream& i, tuple<A, B, C>& t) {
return i >> get<0>(t) >> get<1>(t) >> get<2>(t);
}
template<class T> ostream& operator<<(ostream& o, const pair<T, T>& v) {
o << "(" << v.first << "," << v.second << ")";
return o;
}
template<class T> ostream& operator<<(ostream& o, const vector<T>& v) {
F0R(i, v.size()) {
o << v[i] << ' ';
}
o << newl;
return o;
}
template<class T> ostream& operator<<(ostream& o, const set<T>& v) {
for (auto e : v) {
o << e << ' ';
}
o << newl;
return o;
}
template<class T1, class T2> ostream& operator<<(ostream& o, const map<T1, T2>& m) {
for (auto& p : m) {
o << p.first << ": " << p.second << newl;
}
o << newl;
return o;
}
constexpr double eps = 1e-5;
int N;
struct Pt {
db x, y;
db ata() const {
return atan2(y, x);
}
friend ostream& operator<<(ostream& os, const Pt& p) {
return os << '(' << p.x << ',' << p.y << ')';
}
};
v(Pt) read() {
v(Pt) as(N);
db cx = 0, cy = 0;
{
vpii ab(N);
F0R(i, N) {
cin >> ab[i];
cx += ab[i].first; cy += ab[i].second;
}
cx /= N; cy /= N;
F0R(i, N) {
as[i] = { ab[i].first - cx,ab[i].second - cy };
}
}
return as;
}
// INSERT ABOVE HERE
signed main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
cin >> N;
auto as = read();
auto bs = read();
//show(as);
//show(bs);
if (N == 1) {
cout << "Yes"; return 0;
}
int s = 0;
if (as[0].x == 0 && as[0].y == 0) s++;
F0R(j, N) {
//show(j);
db r = as[s].ata();
//show(as[0]); show(r);
db r2 = bs[j].ata();
//show(bs[j]); show(r2);
db rot = r2 - r;
uset<int> us;
F0R(i, N) {
// iを回転
db x = cos(rot) * as[i].x - sin(rot) * as[i].y;
db y = sin(rot) * as[i].x + cos(rot) * as[i].y;
// 一致する頂点kを探す
F0R(k, N) {
if (abs(x - bs[k].x) < eps && abs(y - bs[k].y) < eps) {
//if (i==0) show(k);
us.insert(k); break;
}
}
}
if (sz(us) == N) {
cout << "Yes"; return 0;
}
}
cout << "No";
}
#endif
|
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define rep(i,n) for(LL i=0;i<(n);i++)
int main()
{
LL n;
LL min = -1;
LL a, p, x;
LL remaind;
cin >> n;
rep(i,n){
cin >> a >> p >> x;
remaind = x - a;
if(remaind > 0){
if(min == -1 || min > p)
min = p;
}
}
cout << min << endl;
return 0;
} | // auther : chotathanos
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<vb> vvb;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef vector< pair<int, int> > vpii;
#define MOD 1000000007
#define PI 3.14159265358979323
#define nl "\n"
#define fr first
#define sc second
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define FOR(i, n) for (int i=0; i<n; i++)
#define rep(i, a, b) for (int i=a; i<=b; i++)
#define ipARR(a, n) for (int i=0; i<n; i++) cin >> a[i];
#define opARR(a, n) for (int i=0; i<n; i++)cout << a[i] << " ";
#define deb(n) cerr << #n << "=" << n << "\n";
void solve() {
int n;
cin >> n;
int a,x,p;
int ans = INT_MAX;
FOR(i,n) {
cin >> a >> p >> x;
if(a < x) ans = min(ans,p);
}
if(ans==INT_MAX) cout << -1;
else cout << ans;
}
int main() {
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios::sync_with_stdio(0); cin.tie(0);
int tc = 1;
// cin >> tc;
while(tc--)
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define llong long long
#define len(x) ((int)x.size())
#define rep(i,n) for (int i = -1; ++ i < n; )
#define rep1(i,n) for (int i = 0; i ++ < n; )
#ifdef testing/*{{{*/
mt19937 rng(177013);
#else
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
#endif
#define rand() (int)(rng() >> 1)
#define CONCAT_(x, y) x##y
#define CONCAT(x, y) CONCAT_(x, y)
#define SPEC(name) CONCAT(name, __LINE__)
#ifdef LOCAL_DEBUG
int __db_level = 0;
#define clog cerr << string(__db_level * 2, ' ')
struct debug_block {
string msg;
debug_block(const string& s): msg(s) { clog << "{ " << msg << endl; ++__db_level; }
~debug_block() { --__db_level; clog << "} " << msg << endl; }
};
#define DB(args...) stringstream SPEC(ss); SPEC(ss)<< args; debug_block SPEC(dbbl)(SPEC(ss).str())
#else
#define clog if (0) cerr
#define DB(...)
#endif
#define db(val) "[" #val " = " << val << "]; "
template<class U, class V> ostream& operator<<(ostream& out, const pair<U, V>& p) {
return out << "(" << p.first << ", " << p.second << ")";
}
template<size_t i, class T> ostream& print_tuple_utils(ostream& out, const T& tup) {
if constexpr(i == tuple_size<T>::value) return out << ")";
else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup);
}
template<class ...U>
ostream& operator<<(ostream& out, const tuple<U...>& tup) { return print_tuple_utils<0, tuple<U...>>(out, tup); }
template<class, typename = void> struct has_const_iterator : false_type {};
template<class T> struct has_const_iterator<T, void_t<typename T::const_iterator>> : true_type {};
template<class T>
typename enable_if<has_const_iterator<T>::value && !is_same<T, string>::value, ostream&>::type
operator<<(ostream& out, const T& container) {
auto beg = container.begin(), end = container.end();
out << "(" << container.size() << ") {";
if (beg != end) out << *(beg++);
while (beg != end) out << ", " << *(beg++);
return out << "}";
}
#define ptrtype(x) typename iterator_traits<x>::value_type
template<class u> vector<ptrtype(u)> $v(u a, u b) { return vector<ptrtype(u)>(a, b); }/*}}}*/
// ACTUAL SOLUTION START HERE ////////////////////////////////////////////////////////////////
const int maxn = 201010;
int n;
int a[maxn], b[maxn], p[maxn], rev[maxn];
vector<pair<int, int>> ans;
void do_swap(int u, int v) {
clog << db(u) << db(v) << endl;
assert(a[u] > b[p[u]]);
assert(a[v] > b[p[v]]);
swap(p[u], p[v]);
ans.emplace_back(u, v);
}
bool vis[maxn];
int main(void) {
#ifdef LOCAL
freopen("main.inp", "r", stdin);
freopen("main.out", "w", stdout);
freopen(".log", "w", stderr);
#endif
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
rep1(i, n) cin >> a[i];
rep1(i, n) cin >> b[i];
rep1(i, n) cin >> p[i];
rep1(i, n) rev[p[i]] = i;
rep1(i, n) {
if (vis[i] or p[i] == i) continue;
vector<int> x;
for (int u = i; !vis[u]; u = rev[u]) {
vis[u] = true;
x.push_back(u);
}
clog << db(i) << db(x) << endl;
for (auto u: x) if (a[u] <= b[p[u]]) {
clog << db(u) << db(a[u]) << db(p[u]) << db(b[p[u]]) << endl;
cout << "-1";
return 0;
}
auto it = min_element(x.rbegin(), x.rend(), [&](int u, int v) { return b[p[u]] < b[p[v]]; });
rotate(x.rbegin(), it, x.rend());
clog << db(x) << endl;
for (int prev = len(x) - 1, cur = 0; cur < len(x) - 1; prev = cur++) {
do_swap(x[prev], x[cur]);
}
}
cout << len(ans) << '\n';
rep1(i, n) assert(p[i] == i);
for (auto [u, v]: ans) cout << u << ' ' << v << '\n';
return 0;
}
// vim: foldmethod=marker
| #include <bits/stdc++.h>
#define rep(a,n) for (ll a = 0; a < (n); ++a)
using namespace std;
//using namespace atcoder;
using ll = long long;
typedef pair<ll,ll> P;
typedef pair<ll,P> PP;
typedef vector<vector<int> > Graph;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const ll INF = 1e18;
#define debug(v) cout<<#v<<": ",prt(v);
template <typename A,typename B>
inline void prt(pair<A,B> p){cout<<"("<<p.first<<", "<<p.second<<")\n";}
template <typename A,typename B,typename C>
inline void prt(tuple<A,B,C> p){cout<<"("<<get<0>(p)<<", "<<get<1>(p)<<", "<<get<2>(p)<<")\n";}
inline void prt(bool p){if(p)cout<<"True"<<'\n';else cout<<"False"<<'\n';}
template <typename T>
inline void prt(vector<T> v){cout<<'{';for(ll i=0;i<v.size();i++){cout<<v[i];if(i<v.size()-1)cout<<", ";}cout<<'}'<<'\n';}
template<typename T>
inline void prt(vector<vector<T> >& vv){ for(const auto& v : vv){ prt(v); } }
template <typename T>
inline void prt(deque<T> v){cout<<'{';for(ll i=0;i<v.size();i++){cout<<v[i];if(i<v.size()-1)cout<<", ";}cout<<'}'<<'\n';}
template <typename A,typename B>
inline void prt(map<A,B> v){cout<<'{';ll c=0;for(auto &p: v){cout<<p.first<<":"<<p.second;c++;if(c!=v.size())cout<<", ";}cout<<'}'<<'\n';}
template <typename A,typename B>
inline void prt(unordered_map<A,B> v){cout<<'{';ll c=0;for(auto &p: v){cout<<p.first<<":"<<p.second;c++;if(c!=v.size())cout<<", ";}cout<<'}'<<'\n';}
template <typename T>
inline void prt(set<T> v){cout<<'{';for(auto i=v.begin();i!=v.end();i++){cout<<*i;if(i!=--v.end())cout<<", ";}cout<<'}'<<'\n';}
template <typename T>
inline void prt(multiset<T> v){cout<<'{';for(auto i=v.begin();i!=v.end();i++){cout<<*i;if(i!=--v.end())cout<<", ";}cout<<'}'<<'\n';}
/*
解法
色を頂点、カードを辺としたグラフを考える
1つの連結成分を考えると頂点数-1の色を選べそう
サイクルあれば全部選べそう
*/
ll n;
Graph g;
ll cnt;
bool cyc;
vector<ll>seen;
void dfs(int v,int p){
for(ll x:g[v]){
if(x==p)continue;
if(seen[x]){
cyc=true;
continue;
}
cnt++;
seen[x]=true;
dfs(x,v);
}
return;
}
int main(){
cin >> n;
g.resize(400010);
rep(i,n){
ll a,b;
cin >> a >> b;
a--;b--;
g[a].push_back(b);
g[b].push_back(a);
}
seen.resize(400010,false);
ll ans = 0;
rep(i,400010){
if(seen[i])continue;
seen[i]=true;
cnt=1;
cyc=false;
dfs(i,-1);
ans+=cnt-1;
if(cyc)ans++;
}
cout << ans << endl;
return 0;
}
|
#include<bits/stdc++.h>
#include <iterator>
#include <iostream>
#include <numeric>
#include <math.h>
#define ll long long
#define ull long
#define mpa make_pair
#define pb push_back
#define ff first
#define pii pair<ll,ll>
#define dd double
#define trace(x) cerr << #x << " : " << x << endl
#define ss second
#define boost ios_base::sync_with_stdio(0)
#define forp(i,a,b) for(ll i=a;i<=b;i++)
#define rep(i,n) for(ll i=0;i<n;++i)
#define ren(i,n) for(ll i=n-1;i>=0;i--)
#define forn(i,a,b) for(ll i=a;i>=b;i--)
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(typeof((c).begin()) i = (c).begin(); i != (c).end();
#define sc(x) scanf("%lld",&x)
#define clr(x,val) memset(x,val,sizeof(x))
#define pr(x) printf("%lld\n",x)
#define pdd pair<dd,dd>
#define read_arr(a,n) for(ll i=1;i<=n;i++)cin>>a[i];
#define init_arr(a,n) for(ll i=1;i<=n;i++)a[i]=n-i+1;
#define prec(x) cout<<fixed<<setprecision(x)
#define fre freopen("input.txt","r",stdin),freopen("output.txt","w",stdout)
#define arr array
using namespace std;
ll h,w,a,b;
ll ans=0;
void dfs(ll index,ll mask,ll lef_a,ll lef_b){
if(index == h*w){
ans++;
return;
}
if(mask & (1ll<<index)){
dfs(index+1,mask,lef_a,lef_b);
return ;
}
if(lef_b){
dfs(index+1,mask | 1<<index ,lef_a,lef_b-1);
}
if(lef_a){
if(index%w +1 <w and (mask & (1ll<<(index+1)))==0){
dfs(index+1,mask | (1ll<<index) | (1ll<< (index+1) ) ,lef_a-1,lef_b);
}
if(index+w<h*w ){
dfs(index+1,mask | (1ll<<index) | (1ll<<(index+w)),lef_a-1,lef_b);
}
}
}
void solve(){
cin>>h>>w>>a>>b;
dfs(0,0,a,b);
cout<<ans<<endl;
}
int main(){
boost;
ll t=1;
while(t--){
solve();
}
}
| #include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
/*............................................................................*/
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
/* find_by_order(x) give iterator of the index x
order_by_key(x) give the position where x will be placed*/
#define int long long
#define lop(i,a,b,c) for (int i=a;i<b;i+=c)
#define rlop(i,a,b,c) for (int i=a-1;i>=b;i-=c)
#define prii pair <int,int>
#define PB push_back
#define S second
#define F first
#define all(x) x.begin(),x.end()
#define vvii vector < vector < int > >
#define vii vector <int>
#define count_1(x) __builtin_popcount(x)
#define cn(a) scanf("%lld",&a);
#define cn_vl(arr,n) lop(i,0,n,1){scanf("%lld",arr+i);}
#define shw_vl(arr,n) lop(i,0,n,1){printf("%lld ",arr[i]);}printf("\n");
const int MAX=2e5+10;
/*......................................................................*/
void fastscan(int &number)
{
bool negative = false;
register int c;
number = 0;
c = getchar();
if (c=='-')
{
negative = true;
c = getchar();
}
for (; (c>47 && c<58); c=getchar())
number = number *10 + c - 48;
if (negative)
number *= -1;
}
bool cmp(prii a,prii b){
if (a.F==b.F){
return a.S<b.S;
}
return a.F<b.F;
}
void solve(){
int n;cin>>n;
vector <prii> vec1,vec2;
lop (i,0,n,1){
int a,b;cn(a)cn(b)
vec1.PB({a,i});
vec2.PB({b,i});
}
sort(all(vec1));
sort(all(vec2));
pair <int,prii> mx1,mx2,mx3,mx4,mx;mx1=mx2=mx3=mx4={-LLONG_MAX,{-1,-1}};
mx1=max(mx1,{vec2[n-1].F-vec2[0].F,{vec2[n-1].S,vec2[0].S}});
mx2=max(mx2,{vec1[n-1].F-vec1[0].F,{vec1[n-1].S,vec1[0].S}});
mx3=max(mx3,{vec2[n-1].F-vec2[1].F,{vec2[n-1].S,vec2[1].S}});
mx3=max(mx3,{vec2[n-2].F-vec2[0].F,{vec2[n-2].S,vec2[0].S}});
mx4=max(mx4,{vec1[n-1].F-vec1[1].F,{vec1[n-1].S,vec1[1].S}});
mx4=max(mx4,{vec1[n-2].F-vec1[0].F,{vec1[n-2].S,vec1[0].S}});
vector <pair <int,prii> > vec;
vec.PB(mx1);vec.PB(mx2);vec.PB(mx3);vec.PB(mx4);
sort(all(vec));
mx.S=vec[3].S;
rlop (i,3,0,1){
if (vec[i].S==mx.S)continue;
cout<<vec[i].F<<"\n";return;
}
// lop (i,1,n,1){
// int z1=max(vec1[i].F-vec1[i-1].F,vec1[i].S-vec1[i-1].S);
// if (z1<mn1){
// cout<<i<<"\n";
// mn2=mn1;
// mn1=z1;
// }
// else if(z1<mn2)mn2=z1;
// cout<<mn2<<" "<<mn1<<" \n";
// }
// lop (i,1,n,1){
// int z1=max(vec2[i].F-vec2[i-1].F,vec2[i].S-vec2[i-1].S);
// if (z1<mn1){
// mn2=mn1;
// mn1=z1;
// }
// else if(z1<mn2)mn2=z1;
// }
}
int32_t main(){
int t;t=1;
//cin>>t;
while (t--){
solve();
}
return 0;
} |
#include <iostream>
using namespace std;
int main() {
int V, T, S, D;
cin >> V >> T >> S >> D;
bool hit = (D < V * T || V * S < D) ? true : false;
if (hit) cout << "Yes" << endl;
else cout << "No" << endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for(int i = (int)(a); i <= (int)(b); ++i)
#define FORR(i, a, b) for(int i = (int)(a); i >= (int)(b); --i)
#define ALL(c) (c).begin(), (c).end()
using ll = long long;
using VI = vector<int>;
using VL = vector<ll>;
using VD = vector<double>;
using VII = vector<VI>;
using VLL = vector<VL>;
using VDD = vector<VD>;
using P = pair<int, int>;
using PL = pair<ll, ll>;
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;
}
void solve(long long A, long long B, long long C, long long D) {
ll div = C * D - B;
if(div <= 0) {
cout << -1 << endl;
return;
}
cout << (A + div - 1) / div << endl;
}
int main() {
long long A;
std::scanf("%lld", &A);
long long B;
std::scanf("%lld", &B);
long long C;
std::scanf("%lld", &C);
long long D;
std::scanf("%lld", &D);
solve(A, B, C, D);
return 0;
}
|
#define nl "\n"
#define ll long long
#define ull unsigned long long
#define pb push_back
#define SIZE(a) (int)a.size()
#define SORT(v) sort(v.begin(),v.end())
#define RSORT(v) sort(v.rbegin(),v.rend())
#define REV(v) reverse(v.begin(),v.end())
#define ff first
#define ss second
#define sq(a) ((a)*(a))
#define For(i,a,b) for(i=a;i<=b;i++)
#define Rof(i,a,b) for(i=a;i>=b;i--)
#define Rep(i,b) for(i=0;i<b;i++)
#define MOD 1000000007
#define PI acos(-1.0)
#define eps 1e-9
#define Linf 2e18
#define inf 1<<30
#define MX5 100005
#define MX6 1000006
#define MX3 1005
#define GCD(a,b) __gcd(a,b)
#define Abs(a) abs(a)
#define input(a,b) scanf("%lld%lld",&a,&b)
#define in1(a) scanf("%lld",&a)
#define output(a) printf("%lld\n",a);
#define mem(a) memset(a,-1,sizeof(a)) // complexity O(n)
#define clr(a) memset(a,0,sizeof (a))
#define mk make_pair
#define pLL pair<ll,ll>
#define invcos(a) (acos(a)*(180/3.14159265))
#define rtan(a) (tan(a*3.14159265/180.0))
#define pip printf("pip")
#define Case(x) printf("Case %lld: ",x)
#define timelimit 1.0*clock()/CLOCKS_PER_SEC
#define pc_one __builtin_popcount
#define pcl_one __builtin_popcountl
#define pcll_one __builtin_popcountll
#define parity __builtin_parity
#define parityl __builtin_parityl
#define parityll __builtin_parityll
#define infile freopen("input.txt","r",stdin)
#define outfile freopen("output.txt","w",stdout)
#define debug(x) cerr << #x << " is " << x << endl
#define fast ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(0);
#define minheap priority_queue<int, vector<int>, greater<int> >
//const ll Linf=2e18;
///4direction -> int del_x[]={-1,0,1,0},del_y[]={0,1,0,-1};
///8direction -> int del_x[]={-1,0,1,0,1,-1,-1,1},del_y[]={0,1,0,-1,-1,-1,1,1};
#include <bits/stdc++.h>
/*#include <iomanip>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>*/
using namespace std;
/*using namespace __gnu_pbds;
typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
*/
ll find_lcm(ll a, ll b) { return ((a * b) / __gcd(a, b));}
ll bigmod(ll n, ll p) {if (p == 0) return 1; if (p == 1)return (n + MOD) % MOD; if (p % 2)return (bigmod(n, p - 1) * n + MOD) % MOD; else {ll x = bigmod(n, p / 2); return (x * x + MOD) % MOD;}}
ll modinverse(ll n) {return bigmod(n, MOD - 2) % MOD;}
bool check_2s_power(ll a) {return (pcll_one(a) == 1 ? true : false);}
ll check_Bit(ll n, ll k) { if (n & (1LL << k)) return true; return false; }
ll setBit(ll n, ll k) { return (n | (1LL << k)); }
ll clearBit(ll n, ll k) { return (n & (~(1LL << k))); }
ll toggleBit(ll n, ll k) { return (n ^ (1LL << k)); }
/*----------------------Graph Moves----------------*/
//const int fx[]={+1,-1,+0,+0};
//const int fy[]={+0,+0,+1,-1};
//const int fx[]={+0,+0,+1,-1,-1,+1,-1,+1}; // Kings Move
//const int fy[]={-1,+1,+0,+0,+1,+1,-1,-1}; // Kings Move
//const int fx[]={-2, -2, -1, -1, 1, 1, 2, 2}; // Knights Move
//const int fy[]={-1, 1, -2, 2, -2, 2, -1, 1}; // Knights Move
/*------------------------------------------------*/
int main()
{
ll n,sum=0,i;
in1(n);
for(i=1;;i++)
{
sum=(i*(i+1))/2;
if(sum>=n)
{
output(i);
return 0;
}
}
} | #include "bits/stdc++.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
using namespace std;
#define ll long long
#define rep(i, b) for (ll i = 0; i < b; i++)
#define fr(i ,a, b) for (ll i = a; i < b; i++)
#define rev(i, b) for (ll i = b-1; i >=0 ; i--)
#define in(a, n) rep(i, n) cin >> a[i];
#define outp(a) cout << a << endl;
#define out(a, n) rep(i, n) cout << a[i] << " ";
#define MIN -1e9
#define MAX 1e9
#define PI acos(-1)
#define inp(a) ll a; cin>>a;
#define endl "\n"
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define vl vector<ll>
#define ff first
#define ss second
#define sz(x) ((ll)(x).size())
#define boo(x) ((x==0)?cout << "NO" << endl; : cout << "YES" << endl;)
typedef pair<int,int> pii;
typedef vector<pii> vii;
typedef vector<int> vi;
typedef pair<ll,ll> pll;
typedef vector<pll> vll;
typedef vector<vi> vvi;
typedef unsigned long long ull;
typedef long double lld;
//=====================================================================/
//debug zone
// #ifndef ONLINE_JUDGE
// #define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
// #else
// #define debug(x)
// #endif
// void _print(ll t) {cerr << t;}
// void _print(int t) {cerr << t;}
// void _print(string t) {cerr << t;}
// void _print(char t) {cerr << t;}
// void _print(lld t) {cerr << t;}
// void _print(double t) {cerr << t;}
// void _print(ull t) {cerr << t;}
// template <class T, class V> void _print(pair <T, V> p);
// template <class T> void _print(vector <T> v);
// template <class T> void _print(set <T> v);
// template <class T, class V> void _print(map <T, V> v);
// template <class T> void _print(multiset <T> v);
// template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
// template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
// template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
// template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
// template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
//=====================================================================/
//Available Functions :
//1. binpow(binpow) 2. string to int and int to string (stll) 3. no of coprime nos before it(coprime)
//=====================================================================/
void solve(){
double a;
cin >> a;
double p=sqrt(a);
if(a==2||a==1){
cout << "1" << endl;
return;
}
fr(i,(int)p-1,a){
if(((i*(i+1))/2)>=a){
cout << i << endl;
return;
}
}
return;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("Error.txt", "w", stderr);
#endif
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
ll tt = 1;
// cin >> tt;
while(tt--)
solve();
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define Rrep(i,n) for (ll i = n - 1; i >= 0;i--)
#define ll long long int
#define INF 1000000007
#define llINF 1000000000000000007
#define v(i) vector<i>
#define vv(i) vector<vector<i>>
// int num = atoi(string.c_str()); 文字列の整数変換
// sort(a,a+n,greater<int>()); 昇順ソート
int digit(ll n){int ans=0;while(n>0){n /= 10; ans++;}return ans;}//桁数
ll _pow(ll a,ll b){ll ans = 1;rep(i,b){ ans = a * ans ;}return ans;}//階乗
ll _modpow(ll a, ll n, ll mod) {ll res = 1;while (n > 0) {if (n & 1){ res = res * a % mod;}a = a * a % mod;n >>= 1;}return res;}
ll gcd(ll a, ll b){if (a%b == 0){return(b);}else{return(gcd(b, a%b));}}//最大公約数
ll lcm(ll a, ll b){return a * b / gcd(a, b);} //最小公倍数
struct UnionFind {
vector<int> par; // par[i]:iの親の番号 (例) par[3] = 2 : 3の親が2
UnionFind(int N) : par(N) { //最初は全てが根であるとして初期化
for(int i = 0; i < N; i++) par[i] = i;
}
int root(int x) { // データxが属する木の根を再帰で得る:root(x) = {xの木の根}
if (par[x] == x) return x;
return par[x] = root(par[x]);//経路圧縮
}
void unite(int x, int y) { // xをyの木に併合
int rx = root(x); //xの根をrx
int ry = root(y); //yの根をry
if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま
par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時:xの根rxをyの根ryにつける
}
bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
int rx = root(x);
int ry = root(y);
return rx == ry;
}
};
ll cmb(ll n, ll r){ll X = 1;for(int i = n; i >= n - r + 1;i--){X = X * i % INF;}ll Y = 1;for(int i = r;i > 0 ;i--){Y = Y * i % INF;}Y = _modpow(Y,(ll)INF-2,(ll)INF);return X * Y % INF;}
ll g2(ll num){
v(ll) sel;
while(num > 0){
sel.push_back(num%10);
num /= 10;
}
sort(sel.begin(),sel.end(),greater<ll>());
ll ans = 0 , keta = 1;
for(auto i : sel){
ans += i*keta;
keta *= 10;
}
return ans;
}
ll g1(ll num){
v(ll) sel;
while(num > 0){
sel.push_back(num%10);
num /= 10;
}
sort(sel.begin(),sel.end());
ll ans = 0 , keta = 1;
for(auto i : sel){
ans += i*keta;
keta *= 10;
}
return ans;
}
int main(){
ll n;cin>>n;
ll k;cin>>k;
ll ans = 0;
ll now = n;
rep(i,k){
now = g1(now) - g2(now);
}
cout<<now<<endl;
return 0;
}
| #include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
char s1[105],s2[105];
bool cmp(int a,int b)
{
return a>b;
}
void numTstr(int fl,ll num)
{
if(fl==1)
{
sprintf(s1,"%lld",num);
int len=strlen(s1);
sort(s1,s1+len,cmp);
}
else if(fl==2)
{
sprintf(s2,"%lld",num);
int len=strlen(s2);
sort(s2,s2+len);
}
}
ll strTnum(int fl)
{
ll ans=0;
if(fl==1)
{
// ll ans=0;
for(int i=0;i<strlen(s1);i++)
{
ans=ans*10+(s1[i]-'0');
}
}
else if(fl==2)
{
// ll ans=0;
for(int i=0;i<strlen(s2);i++)
{
ans=ans*10+(s2[i]-'0');
}
}
return ans;
}
int main()
{
ll n,k;
cin>>n>>k;
ll tt=n;
for(int i=1;i<=k;i++)
{
numTstr(1,tt);
numTstr(2,tt);
if(strcmp(s1,s2)==0)
{
cout<<0<<endl;
return 0;
}
tt=strTnum(1)-strTnum(2);
}
cout<<tt<<endl;
return 0;
} |
#include <iostream>
#include <vector>
#include <map>
using namespace std;
typedef long long ll;
ll INF = 1000000000000000;
struct segment_tree{
int n; vector<ll> seg;
segment_tree(vector<ll> v){
n = v.size(); seg.resize(2*n);
for(int i=0;i<n;i++) seg[i + n] = v[i];
for(int i=n - 1;i>0;i--) seg[i] = seg[i<<1] + seg[i<<1|1];
}
void update(int p,ll val){
for(seg[p += n] += val;p>1;p>>=1) seg[p>>1] = seg[p] + seg[p^1];
}
ll query(int l,int r){
ll res = 0;
for(l += n,r += n; l<r;l>>=1,r>>=1){
if(l&1) res += seg[l++];
if(r&1) res += seg[--r];
}
return res;
}
void clear(){for(int i=0;i<2*n;i++) seg[i] = 0;}
};
vector<int> zz,a,b;
map<int,int> mp;
ll t[200010],x[200010],y[200010];
int main(){
ll i,n,m,q; cin >> n >> m >> q;
mp[0]++;
vector<int> a(n),b(m);
for(i=0;i<q;i++){
cin >> t[i] >> x[i] >> y[i]; x[i]--;
mp[y[i]]++;
}
for(auto x:mp) zz.push_back(x.first);
for(i=0;i<zz.size();i++) mp[zz[i]] = i;
vector<ll> v(zz.size());
segment_tree seg1(v),seg2(v),seg_num1(v),seg_num2(v);
for(i=0;i<n;i++) seg1.update(mp[a[i]],a[i]),seg_num1.update(mp[a[i]],1);
for(i=0;i<m;i++) seg2.update(mp[b[i]],b[i]),seg_num2.update(mp[b[i]],1);
ll ans = 0;
int k = v.size();
for(i=0;i<n;i++) ans += seg_num2.query(0,mp[a[i]])*a[i] + seg2.query(mp[a[i]],k);
for(i=0;i<q;i++){
if(t[i]==1){
int j = x[i];
ans -= seg_num2.query(0,mp[a[j]])*a[j] + seg2.query(mp[a[j]],k);
seg1.update(mp[a[j]],-a[j]); seg_num1.update(mp[a[j]],-1);
a[j] = y[i];
seg1.update(mp[a[j]],a[j]); seg_num1.update(mp[a[j]],1);
ans += seg_num2.query(0,mp[a[j]])*a[j] + seg2.query(mp[a[j]],k);
}
if(t[i]==2){
int j = x[i];
ans -= seg_num1.query(0,mp[b[j]])*b[j] + seg1.query(mp[b[j]],k);
seg2.update(mp[b[j]],-b[j]); seg_num2.update(mp[b[j]],-1);
b[j] = y[i];
seg2.update(mp[b[j]],b[j]); seg_num2.update(mp[b[j]],1);
ans += seg_num1.query(0,mp[b[j]])*b[j] + seg1.query(mp[b[j]],k);
}
cout << ans << endl;
}
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
int pw(int c)
{
int res = 1;
if (c == 0)
return 1;
while (c-- > 0)
res *= 10;
return res;
}
int sc(vector<int> &st, int last)
{
int res = 0;
st[last]++;
for (int i = 1; i < 10; i++)
res += i * pw(st[i]);
st[last]--;
return res;
}
double solve()
{
int k;
string s, t;
cin >> k >> s >> t;
vector<int> cnt(10, k), bag(10, k), st1(10), st2(10);
for (int i = 0; i < 4; i++)
{
cnt[s[i] - '0']--;
cnt[t[i] - '0']--;
st1[s[i] - '0']++;
st2[t[i] - '0']++;
}
for (int i = 1; i <= 9; i++)
bag[i] = cnt[i];
ll w1 = 0, w2 = 0;
for (int i = 1; i <= 9; i++)
{
if (cnt[i] == 0)
continue;
ll w = cnt[i];
cnt[i]--;
for (int j = 1; j <= 9; j++)
{
if (cnt[j] == 0)
continue;
if (sc(st1, i) > sc(st2, j))
w1 += w * 1ll * cnt[j];
else
w2 += w * 1ll * cnt[j];
}
cnt[i]++;
}
return w1 * 1.0 / (w1 + w2) * 1.0;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout << setprecision(18) << fixed << solve() << "\n";
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> p32;
typedef pair<ll,ll> p64;
typedef pair<double,double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int> > vv32;
typedef vector<vector<ll> > vv64;
typedef vector<vector<p64> > vvp64;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
#define fi(i,a,b) for(ll i = (a); i < (b); i++)
#define fr(i,a,b) for(ll i = (a); i >=(b); i--)
#define dbg(x) cout<<#x<<" = "<<x<<endl
#define mp make_pair
#define pb push_back
#define inf 2e15
#define inp(arr,z) for(int i=0; i<z; i++)cin>>arr[i]
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
void solve();
int main(){
fast_cin();
chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
#ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
////freopen("output.txt", "w", stdout);
#endif
ll t=1;
// cin >> t;
while (t--)
{solve();cout<<endl;}
chrono::high_resolution_clock::time_point t2 = chrono::high_resolution_clock::now();
chrono::duration<double, std::milli> time_span = t2 - t1;
//cout<<time_span.count();
return 0;
}
ll power(ll a , ll b ){ll r=1;ll o=a;if(b<=0)return 1;
while(b!=0){if(b%2)r*=o,b--; else {b/=2; o=o*o;}}
return r;}
void solve(){
string s; cin>>s;
reverse(all(s));
fi(i,0,s.size()){if(s[i]=='9')s[i]='6';else if(s[i]=='6')s[i]='9';}
cout<<s;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
string s;
cin>>s;
int n=s.size();
for(int i=0;i<n;i++){
if(i<(n/2)){
char temp =s[i];
s[i]=s[n-1-i];
s[n-1-i]=temp;
}
if(s[i]=='6'){
s[i]='9';
}
else if(s[i]=='9'){
s[i]='6';
}
cout<<s[i];
}
return 0;
} |
/*************************************Ninja-nick-s*****************************************/
#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;typedef long double ld;typedef pair<int,int> pii;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define YES {cout<<"YES\n";return;}
#define NO {cout<<"NO\n";return;}
#define all(x) x.begin(), x.end()
#define rep(i,start,end) for(auto i=(start)-((start)>(end));i!=(end)-((start)>(end));((start)<(end)?i++:i--))
const ll mod = 1e9+7, N = 2e6+7, M = 2e6+7, INF = INT_MAX/10;
ll powe(ll x, ll y){ x = x%mod, y=y%(mod-1);
ll ans = 1;
while(y>0){if (y&1){ans = (1ll * x * ans)%mod;}y>>=1;x = (1ll * x * x)%mod;}
return ans;}
void PA(int v[], int n, int x = 0) { for (int i = x ; i < n + x; i++)cout << v[i] <<" " ;}
void IA(int a[], int n, int x = 0) {for (int i = x; i < n + x; i++)cin >> a[i];}
string yup="YES";
string nope="NO";
/* CHECK t */
void Solve(){
int a,b,c;cin>>a>>b>>c;
// int a[n];IA(a,n);
if(c%2 == 0){
if(abs(a)>abs(b)){
cout<<">";
}
else if(abs(a)<abs(b)){
cout<<"<";
}
else{
cout<<"=";
}
}
else{
if(a>b){
cout<<">";
}
else if(a<b){
cout<<"<";
}
else{
cout<<"=";
}
}
}
int32_t main(){
// #ifndef ONLINE_JUDGE
// freopen("input.txt","r", stdin);
// freopen("output.txt","w", stdout);
// #endif
fast;
int t = 1;
//cin >> t;
//cout<<fixed<<setprecision(9);
while(t--){
Solve();cout<<"\n";
}
return 0;
} | #include<bits/stdc++.h>
using namespace std;
int main(){
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
// -2 -3
if(a > b){
if(c&1)
puts(">");
else{
b = abs(b);
a = abs(a);
if(a > b)
puts(">");
else if(a < b)
puts("<");
else
puts("=");
}
}
else if(a < b){
if(c&1)
puts("<");
else{
b = abs(b);
a = abs(a);
if(a > b)
puts(">");
else if(a < b)
puts("<");
else
puts("=");
}
}
else
puts("=");
return 0;
} |
#include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
#include<math.h>
#include<iomanip>
#include<string>
#include<cassert>
#include<complex>
#include<bits/stdc++.h>
#include<numeric>
#include<cassert>
#include<unordered_map>
#include<unordered_set>
#define rep(s,i,n) for(int i = s;i < n;i++)
using namespace std;
typedef long long ll; //10e18くらい
typedef long double ld;
typedef __int128_t lll;
typedef pair<ll, ll> pll;
const int dx[] = { 0,1,0,-1 };
const int dy[] = { 1,0,-1,0 };
const ll LongINF = 1e13 + 7;
const int INF = 1e9 + 7;
const ll LL_MAX = 9223372036854775807;
const ll LL_MIN = -9223372036854775807;
const double pi = 3.141592653589793;
int dsp[3] = {-1,0,1};
// int keta(ll i){
// int keta = 0;
// do{
// keta++;
// }while(i /= 10);
// return keta;
// }
//素因数分解 O(√n)
// vector<pair<ll,ll>> pfactor(ll n){
// vector<pair<ll,ll>> factor;
// ll ex; //指数
// for(ll a = 2; a * a <= n; a++){
// if(n % a != 0)continue;
// ll ex = 0;
// while(n % a == 0){
// n /= a;
// ex++;
// }
// factor.push_back({a,ex});
// }
// if(n != 1) factor.push_back({n,1});
// return factor;
// }
// ll gcd(int a, int b) {
// if (b==0) return a;
// else return gcd(b, a%b);
// }
// ll lcm(ll a, ll b) {
// return a * b / gcd(a, b);
// }
//---------------------------------------------------------------------------------------------------
int main() {
int a,b,c;
cin >> a >> b >> c;
int ans = (7-a)+(7-b)+(7-c);
cout << ans;
}
| //GIVE ME AC!!!!!!!!!!!!!!!!!
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define ll long long
#define floatset() fixed<<setprecision(15)
#define all(n) n.begin(),n.end()
#define rall(n) n.rbegin(),n.rend()
#define rep(i, s, n) for (ll i = s; i < (ll)(n); i++)
using namespace std;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const ll inf =1e18;
const ll MOD=1000000007;
const ll mod=998244353;
const double pi=acos(-1);
using P=pair<ll,ll>;
int main(){
ll k,n,m;
cin>>k>>n>>m;
ll a[k];
vector<pair<ll,ll>>v;
ll b[k];
ll sum=0;
rep(i,0,k){
cin>>a[i];
b[i]=a[i]*m/n;
sum+=b[i];
v.push_back({n*b[i]-m*a[i],i});
}
sort(all(v));
sum=m-sum;
rep(i,0,sum){
b[v[i].second]++;
}
rep(i,0,k){
if(i){
cout<<" ";
}
cout<<b[i];
}
cout<<endl;
} |
#include <bits/stdc++.h>
using namespace std;
int n;
vector<tuple<int, int, int>> order;
vector<tuple<int, int, int, int>> bestans;
void input() {
cin >> n;
order.resize(n);
for (auto& [x, y, r] : order) cin >> x >> y >> r;
}
void init() {
bestans.resize(n);
}
void simulate() {
for (int i = 0; i < n; ++i) {
auto [x, y, r] = order[i];
auto& [a, b, c, d] = bestans[i];
a = x, c = x + 1;
b = y, d = y + 1;
}
}
void output() {
for (auto [a, b, c, d] : bestans) {
cout << a << ' ' << b << ' ' << c << ' ' << d << '\n';
}
}
int main() {
input();
init();
simulate();
output();
} | #include <bits/stdc++.h>
using namespace std;
int main(){
int N, Q;
cin >> N >> Q;
vector<int> C(N);
for (int i = 0; i < N; i++){
cin >> C[i];
}
vector<map<int, int>> mp(N);
vector<int> p(N);
vector<vector<int>> g(N);
for (int i = 0; i < N; i++){
p[i] = i;
mp[i][C[i]] = 1;
g[i].push_back(i);
}
for (int i = 0; i < Q; i++){
int t;
cin >> t;
if (t == 1){
int a, b;
cin >> a >> b;
a--;
b--;
a = p[a];
b = p[b];
if (a == b){
continue;
}
if (g[a].size() < g[b].size()){
swap(a, b);
}
for (int x : g[b]){
g[a].push_back(x);
p[x] = a;
}
g[b].clear();
for (auto P : mp[b]){
mp[a][P.first] += P.second;
}
mp[b].clear();
}
if (t == 2){
int x, y;
cin >> x >> y;
x--;
cout << mp[p[x]][y] << endl;
}
}
} |
#include <iostream>
#include <vector>
#include <numeric>
#include <set>
int main()
{
int N;
std::cin >> N;
std::vector<long long> vs(2 * N);
for (auto& v : vs) { std::cin >> v; }
std::multiset<long long> pool;
long long aoki = 0;
for (int i = 0; i < N; i++)
{
pool.insert(vs[N - 1-i]);
pool.insert(vs[N+i]);
aoki += *pool.begin();
pool.erase(pool.begin());
}
long long sum = std::accumulate(vs.begin(), vs.end(), 0LL);
std::cout << sum - aoki << std::endl;
return 0;
}
| #include<bits/stdc++.h>
#define cs const
#define pb push_back
using namespace std;
typedef long long ll;
cs int N = 4e5 + 5;
int n, a[N], p[N], c[N];
ll ans;
#define mid ((l + r) >> 1)
cs int M = N << 2;
int mn[M], ta[M];
void up(int x) {
mn[x] = min(mn[x << 1], mn[x << 1 | 1]) + ta[x];
}
void build(int x, int l, int r) {
if(l == r) {
mn[x] = l;
return;
}
build(x << 1, l, mid);
build(x << 1 | 1, mid + 1, r);
up(x);
}
void mdf(int x, int l, int r, int L, int R) {
if(L <= l && r <= R)
return mn[x] --, ta[x] --, void();
if(L <= mid) mdf(x << 1, l, mid, L, R);
if(R > mid) mdf(x << 1 | 1, mid + 1, r, L, R);
up(x);
}
int qry(int x, int l, int r, int L, int R) {
if(L <= l && r <= R)
return mn[x];
int ans = 1e9;
if(L <= mid) ans = min(ans, qry(x << 1, l, mid, L, R));
if(R > mid) ans = min(ans, qry(x << 1 | 1, mid + 1, r, L, R));
return ans + ta[x];
}
#undef mid
void ins(int x, int c) {
int z = qry(1, 1, n, x, n);
if(z) {
ans += c;
mdf(1, 1, n, x, n);
}
}
int main() {
#ifdef FSYo
freopen("1.in", "r", stdin);
#endif
cin >> n;
for(int i = 1; i <= n + n; i++)
scanf("%d", &a[i]), p[i] = i;
sort(p + 1, p + n + n + 1,
[](cs int &i, cs int &j) {
return a[i] > a[j];
});
build(1, 1, n);
for(int i = 1; i <= n + n; i++) {
int x = p[i];
if(x <= n) {
ins(n - x + 1, a[x]);
}
else {
ins(x - n, a[x]);
}
}
cout << ans;
return 0;
} |
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read()
{
int x=0;
bool flag=1;
char c=getchar();
while(c<'0'||c>'9')
{
if(c=='-')
flag=0;
c=getchar();
}
while(c>='0'&&c<='9')
{
x=(x<<1)+(x<<3)+c-'0';
c=getchar();
}
return (flag?x:~(x-1));
}
int L,R,ans,p[1000001];
signed main()
{
L=read();
R=read();
for(int i=0;i<=R;i++)
p[i]=1;
for(int i=2;i<=R;i++)
for(int l=i*2;l<=R;l+=i)
p[l]-=p[i];
for(int i=2;i<=R;i++)
{
int a=R/i-(L-1)/i;
ans+=a*a*p[i];
}
for(int i=max(2ll,L);i<=R;i++)
ans+=1-2*(R/i-(L-1)/i);
cout<<ans;
return 0;
}
| #include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a),i##END=(b);i<=i##END;i++)
#define Rof(i,b,a) for(int i=(b),i##END=(a);i>=i##END;i--)
#define go(u) for(int i=head[u];i;i=nxt[i])
using namespace std;
inline int read(){
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
return x*f;
}
const int N=1e2+10,M=1e4+10;
char s[N];int a[N],b[M][N];
signed main(){
int n=read()+1;
scanf("%s",s+1);
For(i,1,n)a[i]=read();
int k=1e5;
For(i,2,n)k=min(k,abs(a[i]-a[i-1]));
printf("%d\n",k);
For(i,1,k){
For(j,1,n){
printf("%d ",(a[j]+i-1)/k);
}
puts("");
}
return 0;
} |
#include <stdio.h>
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <string.h>
#include <cstring>
#include <queue>
#include <map>
using namespace std;
#pragma warning(disable:4996)
#define fast ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define EPS 1e-11
#define PI acos(-1.0)
typedef unsigned long long ll;
ll arr[20010], arr1[20010];
vector<ll>v;
ll a, b;
map<unsigned long long, unsigned int> m;
priority_queue < ll, vector<ll>, greater<ll> > pq;
int main()
{
int x;
cin >> x;
if (x >= 0) cout << x;
else cout << 0;
}
| #include <iostream>
#include <vector>
using namespace std;
int main() {
long long n;
cin >> n;
vector<int> digs;
while (n) {
digs.push_back(n % 10);
n /= 10;
}
vector<int> rec(3);
int s = 0;
for (auto x : digs) rec[x % 3] ++, s += x;
s %= 3;
if (s == 0) cout << 0;
else if (s == 1) {
if (rec[1] > 0 && digs.size() > 1) cout << 1;
else if (rec[2] > 1 && digs.size() > 2) cout << 2;
else cout << -1;
} else {
if (rec[2] > 0 && digs.size() > 1) cout << 1;
else if (rec[1] > 1 && digs.size() > 2) cout << 2;
else cout << -1;
}
return 0;
} |
// Artur Kraska, II UWr
#include <bits/stdc++.h>
#define forr(i, n) for(int i=0; i<n; i++)
#define FOREACH(iter, coll) for(auto iter = coll.begin(); iter != coll.end(); ++iter)
#define FOREACHR(iter, coll) for(auto iter = coll.rbegin(); iter != coll.rend(); ++iter)
#define lbound(P,R,PRED) ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;})
#define testy() int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests)
#define CLEAR(tab) memset(tab, 0, sizeof(tab))
#define CONTAIN(el, coll) (coll.find(el) != coll.end())
#define FOR(i, a, b) for(int i=a; i<=b; i++)
#define FORD(i, a, b) for(int i=a; i>=b; i--)
#define MP make_pair
#define PB push_back
#define ff first
#define ss second
#define deb(X) X;
#define M 1000000007
#define INF 1000000007LL
using namespace std;
int n, m, a;
long long b, c;
int main()
{
scanf("%lld %lld", &b, &c);
long long l1 = -b-(c-1)/2, r1 = -b+(c-1)/2, l2 = b-c/2, r2 = b+(c-2)/2;
if(b < 0)
{
swap(l1, l2);
swap(r1, r2);
}
{
if(l2 <= r1)
printf("%lld\n", max(r1, r2)-min(l1, l2)+1);
else
printf("%lld\n", r1-l1+1 + r2-l2+1);
}
return 0;
}
| #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<ll,int> P;
int main()
{
ll B,C;
cin >> B >> C;
/*
ll L = B - (C-1)/2;
ll R = B;
ll ans = 0;
if ((L < 0 && R > 0) || (L < 0 && R < 0))
{
ans = 2 * max(abs(L-R));
}
else
{
ans = 2 * max(abs(L),abs(R)) + 1;
}
if (C % 2 == 0)
{
}
*/
vector<P> v;
v.push_back(P(B-(C-1)/2,1));
v.push_back(P(B+1,-1));
v.push_back(P(-1*(B-(C-1)/2)+1,-1));
v.push_back(P(-1*B,1));
v.push_back(P(-1*B-(C/2-1),1));
v.push_back(P(-1*B+1,-1));
v.push_back(P(-1*(-1*B-(C/2-1))+1,-1));
v.push_back(P(B,1));
v.push_back(P(B-C/2,1));
v.push_back(P(B-C/2+1,-1));
v.push_back(P(-1*B-(C-1)/2,1));
v.push_back(P(-1*B-(C-1)/2+1,-1));
sort(v.begin(), v.end());
ll ans = 0;
ll tmp = v[0].first;
int t = 1;
for (int i = 1; i < v.size(); ++i)
{
if (t > 0)
{
ans += v[i].first - tmp;
}
tmp = v[i].first;
t += v[i].second;
}
cout << ans << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, n) for (int i = a; i <= n; ++i)
#define per(i, n, a) for (int i = n; i >= a; --i)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long LL;
typedef pair<int, int> PII;
mt19937 rnd(time(0));
inline int in() {
char ch = getchar();
int f = 1, x = 0;
while (ch < '0' || ch > '9') {
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
x = (x << 1) + (x << 3) + ch - '0', ch = getchar();
return f * x;
}
const int inf = (1 << 30);
const int mod = 998244353;
const int N = 400010;
LL ksm(LL a, LL b, LL MOD = mod) {
LL res = 1;
while (b) {
if (b & 1)
res = res * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return res;
}
LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a; }
LL exgcd(LL a, LL b, LL &x, LL &y) {
if (!b) {
x = 1;
y = 0;
return a;
}
LL g = exgcd(b, a % b, y, x);
y -= a / b * x;
return g;
}
LL Inv(LL a, LL b = mod) {
assert(gcd(a, b) == 1);
a %= b;
return ksm(a, b - 2, b);
}
LL fac[N], inv[N];
void init(int n) {
fac[0] = 1;
rep(i, 1, n) fac[i] = fac[i - 1] * i % mod;
inv[n] = ksm(fac[n], mod - 2);
per(i, n - 1, 0) inv[i] = inv[i + 1] * (i + 1) % mod;
}
LL C(int n, int m) {
if (m > n || m < 0)
return 0;
return fac[n] * inv[m] % mod * inv[n - m] % mod;
}
int _mu[N], isp[N];
VI p;
void init_prime(int n) {
_mu[1] = 1;
for (int i = 2; i <= n; ++i) {
if (!isp[i])
p.push_back(i), _mu[i] = -1;
for (int j = 0; j < (int)p.size() && i * p[j] <= n; ++j) {
isp[i * p[j]] = 1;
if (i % p[j] == 0) {
_mu[i * p[j]] = 0;
break;
}
_mu[i * p[j]] = -_mu[i];
}
}
}
void solve() {
int n = in(), m = in();
init(n + m);
init_prime(n + m);
LL ans = 0;
rep(i, 1, m) {
LL val = 1;
int w = i;
for (int j = 0; j < (int)p.size(); ++j) {
if (p[j] * p[j] > w)
break;
int res = 0;
while (w % p[j] == 0) {
w /= p[j];
++res;
}
(val *= C(res + n - 1, n - 1)) %= mod;
}
if (w > 1)
(val *= n) %= mod;
(ans += val) %= mod;
}
printf("%lld\n", ans);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
// freopen("out.out", "w", stdout);
#endif
int T = 1;
// T = in();
while (T--) {
solve();
}
}
| #include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <climits>
#include <string>
#include <cmath>
#include <bitset>
#include <complex>
#include <functional>
#include <ctime>
#include <cassert>
#include <fstream>
#include <stack>
#include <random>
#include <iomanip>
#include <fstream>
using namespace std;
typedef long long ll;
typedef long double dd;
#define i_7 (ll)(1E9+7)
//#define i_7 998244353
#define i_5 i_7-2
ll mod(ll a){
ll c=a%i_7;
if(c>=0)return c;
return c+i_7;
}
typedef pair<ll,ll> l_l;
ll inf=(ll)1E18;
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define pb push_back
ll max(ll a,ll b){if(a<b)return b;else return a;}
ll min(ll a,ll b){if(a>b)return b;else return a;}
dd EPS=1E-9;
#define endl "\n"
#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
ll f(char c){
ll k=c-'0';
if(0<=k && k<=9){
return k;
}
ll l=c-'A';
return 10+l;
}
int main(){fastio
string N;cin>>N;
ll K;cin>>K;
ll n;n=(ll)N.size();
ll dp[2][n][17];
ll bol[n][16];rep(i,0,n-1)rep(j,0,15)bol[i][j]=0;
rep(i,0,n-1){
if(i>0){
rep(j,0,15){
bol[i][j]=bol[i-1][j];
}
}
bol[i][f(N[i])]=1;
}
/*
rep(i,0,15){
rep(j,0,n-1){
cout<<bol[j][i]<<' ';
}
cout<<endl;
}
cout<<endl;
*/
rep(i,0,n-1){
rep(j,1,15){
bol[i][j] = bol[i][j-1]+bol[i][j];
}
}
/*
rep(j,0,15){
rep(i,0,n-1){
cout<<bol[i][j]<<' ';
}
cout<<endl;
}
cout<<endl;
*/
//bol[i][j]=0~jのうちN[i]までで現れたものの個数
rep(i,0,n-1){
rep(j,0,16){
if(j==bol[i][15]){
dp[0][i][j]=1;
}else{
dp[0][i][j]=0;
}
if(i==0){
if(j==0){
dp[1][i][j]=1;
}else if(j==1){
dp[1][i][j]=f(N[i])-1;
}else{
dp[1][i][j]=0;
}
}else{
if(j==0){
dp[1][i][j]=1;
}else{
if(f(N[i]) == 0){
if(j==1){
dp[1][i][j] = mod(dp[1][i-1][j-1]*(16-j)+dp[1][i-1][j]*j);
}else{
dp[1][i][j] = mod(dp[1][i-1][j-1]*(17-j)+dp[1][i-1][j]*j);
}
}else{
if(j==1){
dp[1][i][j] = mod(dp[1][i-1][j-1]*(16-j)+dp[1][i-1][j]*j);
}else{
dp[1][i][j] = mod(dp[1][i-1][j-1]*(17-j)+dp[1][i-1][j]*j);
}
dp[1][i][j] += dp[0][i-1][j]*(bol[i-1][f(N[i])-1]);
dp[1][i][j] = mod(dp[1][i][j]);
dp[1][i][j] += dp[0][i-1][j-1]*(f(N[i])-bol[i-1][f(N[i])-1]);
dp[1][i][j] = mod(dp[1][i][j]);
}
}
}
}
}
ll ans=0;
ans += dp[0][n-1][K];
ans = mod(ans);
ans += dp[1][n-1][K];
ans = mod(ans);
cout<<ans<<endl;
/*
rep(i,0,15){
rep(j,0,n-1){
cout<<bol[j][i]<<' ';
}
cout<<endl;
}
cout<<endl;
rep(k,0,1){
cout<<"k: "<<k<<endl;
rep(j,0,15){
rep(i,0,n-1){
cout<<dp[k][i][j]<<' ';
}
cout<<endl;
}
cout<<endl;
}
*/
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
vector<int64_t> a(n),maxA(n);
for(int i = 0;i < n;i++)cin >> a.at(i);
int64_t ma = 0;
for(int i = 0;i < n;i++){
ma = max(ma,a.at(i));
maxA.at(i) = ma;
}
int64_t ans = 0;
a.at(0) += a.at(0);
ans += a.at(0);
cout << ans << endl;
for(int i = 1;i < n;i++){
ans += (maxA.at(i) - maxA.at(i-1))*i;
a.at(i) += a.at(i-1)+(maxA.at(i) - maxA.at(i-1));
ans += a.at(i);
cout << ans << endl;
}
}
| #include <bits/stdc++.h>
#include <iostream>
#include <string>
using namespace std;
int main()
{
long N;
cin >> N;
long result = 0;
long A[N];
long sum = 0;
for (long i = 0; i < N; i++) {
cin >> A[i];
result += N * A[i] * A[i];
sum += A[i];
}
cout << result - sum * sum << endl;
} |
Subsets and Splits