code_file1
stringlengths 87
4k
| code_file2
stringlengths 82
4k
|
---|---|
#include "bits/stdc++.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr<<"hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x,y) (lower_bound(all(x),y)-x.begin())
#define ubd(x,y) (upper_bound(all(x),y)-x.begin())
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) { return rng() % (y+1-x) + x; } //inclusivesss
string inline to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){ return a==0?llabs(b):gcd(b%a,a); }
using ll=long long;
using ld=long double;
#define FOR(i,s,e) for(ll i=s;i<=ll(e);++i)
#define DEC(i,s,e) for(ll i=s;i>=ll(e);--i)
using pi=pair<ll,ll>; using spi=pair<ll,pi>; using dpi=pair<pi,pi>;
#define LLINF ((long long)1e18)
#define INF int(1e9+1e6)
#define MAXN (200006)
ll n,k,A[MAXN],C[302][302],mod=998244353,sum[MAXN],ans[302],co[MAXN];
ll qexp(ll x,ll e){
ll sum=1;
for(;e;e>>=1,x*=x,x%=mod)if(e&1)sum*=x,sum%=mod;
return sum;
}
int main(){
FAST
cin>>n>>k;
FOR(i,1,n)cin>>A[i];
FOR(i,0,300) C[i][0]=1;
FOR(i,1,300) FOR(j,1,300) C[i][j] = (C[i-1][j] + C[i-1][j-1]) % mod;
FOR(i,1,n){
ll co = 1;
FOR(j,0,k){
sum[j] += co, sum[j] %= mod;
co *= A[i], co %= mod;
}
}
fill(co,co+MAXN,1);
FOR(i,1,k){
ll ans = 0;
FOR(j,0,i){
ans += sum[j] * sum[i-j] % mod * C[i][j] % mod, ans %= mod;
}
FOR(z,1,n){
co[z] *= A[z] * 2 % mod, co[z] %= mod;
ans -= co[z], ans += mod, ans %= mod;
}
ans *= qexp(2, mod-2), ans %= mod;
cout<<ans<<'\n';
}
}
| #include "bits/stdc++.h" // Tomasz Nowak
using namespace std; // XIII LO Szczecin
using LL = long long; // Poland
#define FOR(i, l, r) for(int i = (l); i <= (r); ++i)
#define REP(i, n) FOR(i, 0, (n) - 1)
template<class T> int size(T &&x) {
return int(x.size());
}
template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) {
return out << '(' << p.first << ", " << p.second << ')';
}
template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) {
out << '{';
for(auto it = x.begin(); it != x.end(); ++it)
out << *it << (it == prev(x.end()) ? "" : ", ");
return out << '}';
}
void dump() {}
template<class T, class... Args> void dump(T &&x, Args... args) {
cerr << x << "; ";
dump(args...);
}
#ifdef DEBUG
struct Nl{~Nl(){cerr << '\n';}};
# define debug(x...) cerr << (strcmp(#x, "") ? #x ": " : ""), dump(x), Nl(), cerr << ""
#else
# define debug(...) 0 && cerr
#endif
mt19937_64 rng(0);
int rd(int l, int r) {
return uniform_int_distribution<int>(l, r)(rng);
}
// end of templates
constexpr int mod = int(998244353);
int mul(int a, int b) {
return int((a * LL(b)) % mod);
}
int add(int a, int b) {
a += b;
if(a >= mod)
a -= mod;
return a;
}
int pot(int a, int b) {
if(b == 0)
return 1;
int x = pot(a, b / 2);
x = mul(x, x);
if(b & 1)
x = mul(x, a);
return x;
}
int inv(int x) {
return pot(x, mod - 2);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, K;
cin >> n >> K;
vector<int> a(n);
for(int &ai : a)
cin >> ai;
vector<int> silnia(K + 1, 1);
FOR(i, 2, K)
silnia[i] = mul(silnia[i - 1], i);
vector<int> revsilnia(K + 1, 1);
revsilnia[K] = inv(silnia[K]);
for(int i = K - 1; i >= 2; --i)
revsilnia[i] = mul(revsilnia[i + 1], i + 1);
auto newton = [&](int x, int y) {
return mul(silnia[x], mul(revsilnia[y], revsilnia[x - y]));
};
vector<int> sum_pot(K + 1);
FOR(k, 0, K)
REP(i, n)
sum_pot[k] = add(sum_pot[k], pot(a[i], k));
debug(sum_pot);
/*
vector<int> sum_2a_pot(K + 1);
FOR(k, 1, K)
REP(i, n)
sum_2a_pot[k] = add(sum_2a_pot[k], pot(2 * a[i], k));
*/
int inv2 = inv(2);
FOR(k, 1, K) {
int answer = 0;
FOR(x, 0, k) {
int nw = newton(k, x);
int local = mul(sum_pot[x], sum_pot[k - x]);
local = add(local, mod - sum_pot[k]);
local = mul(local, inv2);
debug(x, k - x, k, nw, local);
answer = add(answer, mul(nw, local));
}
cout << answer << '\n';
}
}
|
#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#include <queue>
using namespace std;
int n, a[3009][5];
int vis[4][1 << 5];
int fu(int x) {
memset(vis, 0, sizeof(vis));
vis[0][0] = 1;
for(int i = 0; i < n; i ++) {
for(int j = 0; j < 3; j ++) {
for(int k = 0; k < 1 << 5; k ++) {
if(vis[j][k] == 0) continue;
int v = k;
v |= (a[i][0] >= x) << 0;
v |= (a[i][1] >= x) << 1;
v |= (a[i][2] >= x) << 2;
v |= (a[i][3] >= x) << 3;
v |= (a[i][4] >= x) << 4;
vis[j + 1][v] = 1;
}
}
}
return vis[3][(1 << 5) - 1];
}
int main() {
cin >> n;
for(int i = 0; i < n; i ++)
for(int j = 0; j < 5; j ++)
cin >> a[i][j];
int l = 0, r = 1e9;
while(l < r) {
int mid = l + r + 1 >> 1;
if(fu(mid)) l = mid;
else r = mid - 1;
}
cout << l;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
bool join(int p, vector<vector<int>> a){
int n = a.size();
vector<uint> vec(32);
for(vector<int> ai : a){
uint bit = 0;
for(int j = 0; j < 5; j++){
if(ai[j] >= p) bit += (1<<j);
}
vec[bit] = bit;
}
//for(uint i : vec) cout << i << ","; cout << endl;
for(uint i : vec){
for(uint j : vec){
for(uint k : vec){
if((i|j|k) == 31) return true;
}
}
}
return false;
}
int main(){
int n; cin >> n;
vector<vector<int>> a(n,vector<int>(5));
for(int i = 0; i < n; i++){
for(int j = 0; j < 5; j++){
cin >> a[i][j];
}
}
vector<int> aa(5*n);
for(int i = 0; i < n; i++){
for(int j = 0; j < 5; j++){
aa[5*i+j] = a[i][j];
}
}
sort(aa.begin(),aa.end());
reverse(aa.begin(),aa.end());
for(int i : aa){
//cout << "___" << i << endl;
if(join(i,a)){
cout << i << endl;
break;
}
}
} |
#include<bits/stdc++.h>
using namespace std;
const int inf = 1e9;
const int N = 1e5+20;
const int M = 19;
int dis[M][N];
int vis[N];
vector<int> G[N];
int n,m,k,C[N],f[N*2][M];
priority_queue<pair<int,int> >q;
void dij(int *dis,int x)
{
for(int i=1;i<=n;i++)dis[i]=inf,vis[i]=0;
dis[x] = 0;
q.push(make_pair(0,x));
while(!q.empty())
{
int u=q.top().second;q.pop();
if(vis[u]) continue;
vis[u] = 1;
for(int v:G[u])if(dis[v]>dis[u]+1)
{
dis[v] = dis[u]+1;
q.push(make_pair(-dis[v],v));
}
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1,x,y;i<=m;i++)
scanf("%d%d",&x,&y),G[x].push_back(y),
G[y].push_back(x);
scanf("%d",&k);
for(int i=0;i<k;i++)
scanf("%d",&C[i]),dij(dis[i],C[i]);
int M = 1<<k;
for(int i=0;i<M;i++)
for(int j=0;j<k;j++)f[i][j] = inf;
for(int i=0;i<k;i++)
f[1<<i][i]=1;
for(int i=1;i<M-1;i++)for(int l=0;l<k;l++)if(f[i][l]!=inf)
{
for(int j=0;j<k;j++)
if(((i>>j)&1)==0)
{
int d = dis[l][C[j]];
f[i|(1<<j)][j]= min(f[i|(1<<j)][j],f[i][l]+d);
}
}
int ans = inf;
for(int i=0;i<k;i++) ans = min(ans,f[M-1][i]);
printf("%d",ans==inf?-1:ans);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (int)n; i++)
using ll = long long;
struct edge{int to; ll cost;};
using P = pair<ll,int>;
const ll INF = 1e17;
void dijkstra(int n, int s, vector<vector<edge>>& g, vector<ll>& d){
priority_queue<P,vector<P>,greater<P>> que;
rep(i,n) d[i] = INF;
d[s] = 0;
que.push(P(0,s));
while(!que.empty()){
P p = que.top();
que.pop();
int v = p.second;
if(d[v] < p.first) continue;
for(auto e : g[v]){
if(d[e.to] > d[v] + e.cost){
d[e.to] = d[v] + e.cost;
que.push(P(d[e.to], e.to));
}
}
}
}
int main(){
int n, m;
cin >> n >> m;
int a[m], b[m];
vector<vector<edge>> g(n);
rep(i,m) {
cin >> a[i] >> b[i];
a[i]--; b[i]--;
g[a[i]].push_back(edge{b[i],1});
g[b[i]].push_back(edge{a[i],1});
}
int k;
cin >> k;
int c[k];
rep(i,k) {
cin >> c[i];
c[i]--;
}
vector<vector<ll>> dist(k,vector<ll>(k,INF));
rep(i,k) {
vector<ll> d(n);
dijkstra(n,c[i],g,d);
rep(j,k) dist[i][j] = d[c[j]];
}
vector<vector<ll>> dp(1<<k,vector<ll>(k,INF));
rep(i,k) dp[1<<i][i] = 1;
rep(bit,1<<k) rep(j,k) rep(l,k) dp[(1<<l)|bit][l] = min(dp[(1<<l)|bit][l],dp[bit][j]+dist[j][l]);
ll ans = INF;
rep(i,k) ans = min(ans,dp[(1<<k)-1][i]);
cout << (ans == INF ? -1 : ans) << endl;
return 0;
}
|
/**
* Author: Daniel
* Created Time: 2021-05-18 18:12:02
**/
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define ER erase
#define IS insert
#define PI acos(-1)
#define PB pop_back
#define MP make_pair
#define MT make_tuple
#define LB lower_bound
#define UB upper_bound
#define EB emplace_back
#define lowbit(x) (x & -x)
#define SZ(x) ((int)x.size())
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
#define SOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout<<fixed<<setprecision(10)
typedef long long LL;
typedef vector<LL> VL;
typedef vector<int> VI;
typedef vector<bool> VB;
typedef pair<LL, LL> PLL;
typedef vector<string> VS;
typedef vector<double> VD;
typedef pair<int, int> PII;
typedef unsigned long long ULL;
typedef pair<double, double> PDD;
typedef vector<pair<LL, LL> > VPLL;
typedef vector<pair<int, int> > VPII;
template <typename A> using VE = vector<A>;
template <typename A> using USET = unordered_set<A>;
template <typename A> using HEAP = priority_queue<A>;
template <typename A, typename B> using PA = pair<A, B>;
template <typename A, typename B> using UMAP = unordered_map<A, B>;
template <typename A> using RHEAP = priority_queue<A, vector<A>, greater<A> >;
template <typename A> A MAX(const A &a) { return a; }
template <typename A> A MIN(const A &a) { return a; }
template <typename A> A MAX(const A *a, const A *b) { return *max_element(a, b); }
template <typename A> A MIN(const A *a, const A *b) { return *min_element(a, b); }
template <typename A, typename... B> A MAX(const A &a, const B&... b) { return max(a, MAX(b...)); }
template <typename A, typename... B> A MIN(const A &a, const B&... b) { return min(a, MIN(b...)); }
template <typename A, typename B = typename std::iterator_traits<A>::value_type> B MAX(A a, A b) { return *max_element(a, b); }
template <typename A, typename B = typename std::iterator_traits<A>::value_type> B MIN(A a, A b) { return *min_element(a, b); }
mt19937 rng((unsigned int)chrono::steady_clock::now().time_since_epoch().count());
///////////////////////////////////////////////////////////////////////////
//////////////////// DO NOT TOUCH BEFORE THIS LINE ////////////////////////
///////////////////////////////////////////////////////////////////////////
// check the limitation!!!
const int N = 100010, M = 1010;
// read the question carefully!!!
int main()
{
SOS;
int n;
string s, t;
cin >> n >> s >> t;
VI a, b;
for (int i = 0; i < n; i ++ )
{
if (s[i] == '0') a.EB(i);
if (t[i] == '0') b.EB(i);
}
if (SZ(a) != SZ(b)) cout << "-1\n";
else
{
int res = 0;
for (int i = 0; i < SZ(a); i ++ )
res += (int)(a[i] != b[i]);
cout << res << '\n';
}
return 0;
}
// GOOD LUCK!!!
| #include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long int ll;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for(auto it = d.b;it != d.e;++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;cin >> n;
int ans = 0;
string s;cin >> s;
vector<vector<int>> cnt(n + 1, vector<int>(4));
map<char, int> m;
m['A'] = 0; m['T'] = 1; m['G'] = 2; m['C'] = 3;
//ATGC
for(int i = 0;i < n;i++){
cnt[i + 1][m[s[i]]]++;
for(int j = 0;j < 4;j++){
cnt[i + 1][j] += cnt[i][j];
}
}
for(int i = 1;i <= n;i++){
for(int j = i;j <= n;j++){
int a = cnt[j][0] - cnt[i - 1][0];
int t = cnt[j][1] - cnt[i - 1][1];
int g = cnt[j][2] - cnt[i - 1][2];
int c = cnt[j][3] - cnt[i - 1][3];
bool valid = true;
if(a > 0 || t > 0){
if(a != t)valid = false;
}
if(g > 0 || c > 0){
if(g != c)valid = false;
}
if(valid)ans++;
}
}
cout << ans << endl;
}
|
#include<bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define dofloat cout<<fixed<<setprecision(8)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define bitcount __builtin_popcount
#define all(vec) vec.begin(),vec.end()
#define rall(vec) vec.rbegin(),vec.rend()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<long long> vl;
typedef pair<long long,long long> pll;
typedef vector<pair<long long,long long> > vll;
typedef vector<pair<int,int> > vii;
typedef vector<int> vi;
typedef pair<int,int> ii;
const long long MOD=1000000007;
const long long MAX=100005;
const long double PI=3.14159265359;
const long double G=9.807;
const long long INF=1e18;
const long double EPS=1e-6;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
bool isprime(ll a){
if(a==2)return 1;
if(!(a&1))return 0;
for(ll i=3;i*i<=a;i+=2)
if(a%i==0)return 0;
return 1;
}
ll mpow(ll base,ll exponent,ll modulus){
if(modulus==1)return 0;
long long result = 1;
base=base%modulus;
while(exponent){
if(exponent%2 == 1)
result=(result*base)%modulus;
exponent=exponent>>1;
base=(base*base)%modulus;
}
return result;
}
ll minv(ll a,ll mod){
ll _gcd=gcd(a,mod);
assert(_gcd==1);
return mpow(a,mod-2,mod);
}
/*
ll ncr(ll N,ll K){
if(N<K)return 0;
if(K==0)return 1;
if(N==0)return 0;
ll den=1;
for(ll i=1;i<=K;i++)den*=i;
ll num=1;
while(K--){
num*=N;
if(num%den==0){
num/=den;
den=1;
}
N--;
}
return num;
}
*/
ll n;
int main(){FAST;
cin>>n;
cout<<6987268688401;
return 0;
}
/*
WA? check edge cases, N=1,M=1 etc. Read code fully once and check all variables.
Read the question again and see what you might have missed.
Consider changing your solution or find more cases.
*/
| #include <stdio.h>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <functional>
//#include <atcoder/all>
//using namespace atcoder;
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef vector<vector<int>> v2int;
typedef vector<vector<vector<int>>> v3int;
typedef vector<ll> vll;
typedef vector<vector<ll>> v2ll;
typedef vector<vector<vector<ll>>> v3ll;
typedef list<int> liint;
typedef pair<int, int> pint;
typedef vector<pair<int, int>> vpint;
typedef vector<pair<ll, ll>> vpll;
typedef vector<pair<ll, int>> vpll_int;
typedef vector<pair<int, ll>> vpint_ll;
typedef set<pair<int, int>> spint;
typedef set<pair<ll, int>> spll;
typedef unordered_map<int, unordered_set<int>> Graph;
const int INF = int(2e9);
const ll LINF = ll(2e9) * ll(2e9);
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T, class C> void chmax(T& a, C b) { a > b ? : a = b; }
template<class T, class C> void chmin(T& a, C b) { a < b ? : a = b; }
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);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll N;
cin >> N;
ll lc = 1;
REP(i, 2, N + 1) {
lc = lcm(lc, i);
}
ll ans = lc + 1;
/* REP(i, 2, N + 1) {
cout << ans % i << endl;
}*/
cout << ans << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
long long int MOD = 1000000007;
void mult(vector<vector<long long int>> &a, vector<vector<long long int>> &b, int n) {
vector<vector<long long int>> c(n, vector<long long int>(n, 0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
c[i][j] += a[i][k] * b[k][j];
c[i][j] %= MOD;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = c[i][j];
}
}
}
void bin_mat(vector<vector<long long int>> &m, long long int e, int n, vector<vector<long long int>> &temp) {
if (e == 0)
return;
if (e == 1)
return;
bin_mat(m, e / 2, n, temp);
mult(m, m, n);
if (e % 2)
mult(m, temp, n);
}
long long int bin_pow(long long int b, long long int e) {
if (e == 0) return 1;
long long int temp = bin_pow(b, e / 2);
temp = temp * temp % MOD;
if (e % 2)
return temp * b % MOD;
return temp;
}
void solve() {
int n,m,k; cin >> n >> m >> k;
vector<long long int> num(n);
for (int i = 0; i < n; i++) {
cin >> num[i];
}
vector<vector<int>> graph(n);
for (int i = 0; i < m; i++) {
int a,b; cin >> a >> b;
a--; b--;
graph[a].push_back(b);
graph[b].push_back(a);
}
vector<vector<long long int>> mat(n, vector<long long int>(n, 0));
for (int i = 0; i < n; i++) {
int cnt = m - graph[i].size();
mat[i][i] += 2 * cnt % MOD;
for (auto j: graph[i]) {
mat[i][i]++;
mat[i][j]++;
}
}
vector<vector<long long int>> temp = mat;
bin_mat(mat, k, n, temp);
if (k > 0) {
vector<long long int> ans(n, 0);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
ans[i] += mat[i][j] * num[j] % MOD;
ans[i] %= MOD;
}
}
long long int val = bin_pow(2 * m, k);
val = bin_pow(val, MOD - 2);
for (int i = 0; i < n; i++) {
cout << ans[i] * val % MOD << '\n';
}
} else {
for (int i = 0; i < n; i++)
cout << num[i] % MOD << '\n';
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
} | #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<utility>
#include<set>
#include<stack>
#include<list>
#include<deque>
#include<bitset>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<climits>
#include<cmath>
#include<cctype>
#define pb push_back
#define mp make_pair
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ren(i,a,b) for(int i=a;i>=b;i--)
#define ff first
#define ss second
#define pll pair<long long int,long long int>
#define pii pair<int,int>
#define vll vector<long long int>
#define vii vector<int>
#define gi(n) scanf("%d",&n)
#define gll(n) scanf("%lld",&n)
#define gstr(n) scanf("%s",n)
#define gl(n) cin >> n
#define oi(n) printf("%d",n)
#define oll(n) printf("%lld",n)
#define ostr(n) printf("%s",n)
#define ol(n) cout << n
#define os cout<<" "
#define on cout<<"\n"
#define o2(a,b) cout<<a<<" "<<b
#define all(n) n.begin(),n.end()
#define present(s,x) (s.find(x) != s.end())
#define cpresent(s,x) (find(all(s),x) != s.end())
#define tr(container, it) for(__typeof(container.begin()) it = container.begin(); it != container.end(); it++)
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef long double ld;
typedef vector<vector<ll> > mat;
ll mod=1e9+7;
mat I;
mat mul(mat a,mat b)
{
int n=a.size();
mat c(n,vll(n));
rep(i,0,n-1)rep(j,0,n-1)rep(k,0,n-1)
{
c[i][j]=(c[i][j]+a[i][k]*b[k][j])%mod;
}
return c;
}
ll p(ll a,ll b)
{
ll r=1;
while(b)
{
if(b%2)r=(r*a)%mod;
a=(a*a)%mod;
b/=2;
}
return r;
}
mat p(mat a,ll b)
{
mat r=I;
while(b)
{
if(b%2)
r=mul(r,a);
a=mul(a,a);
b/=2;
}
return r;
}
ll n,m,k,e[105][105],a[105];
int main()
{ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m>>k;
I.resize(n);
rep(i,0,n-1)
{
I[i].resize(n);
I[i][i]=1;
}
rep(i,0,n-1)cin>>a[i];
rep(i,0,m-1)
{
int x,y;
cin>>x>>y;
x--;y--;
e[x][y]=1;
e[y][x]=1;
}
mat prod(n,vll(n));
rep(i,0,n-1)
{
ll ed=0;
rep(j,0,n-1)ed+=e[i][j];
prod[i][i]=(2*m-ed)%mod;
rep(j,0,n-1)if(e[i][j])prod[i][j]=1;
}
prod=p(prod,k);
ll den=p(2*m,k);
den=p(den,mod-2);
rep(i,0,n-1)
{
ll ans=0;
rep(j,0,n-1)
{
ans=(ans+prod[i][j]*a[j])%mod;
}
ans=(ans*den)%mod;
ol(ans);on;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define all(c) c.begin(),c.end()
#define sz(x) int(x.size())
#define ar array
typedef long long ll ;
typedef vector<int> vi;
#define mod 1000000007
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll exp(ll x,ll y){
x%=mod;
ll res=1;
while(y){
if(y&1)
res=res*x%mod;
x=x*x%mod;
y>>=1;
}
return res;
}
ll fact(ll n){
ll res=1;
for(ll i=2;i<=n;i=i+1)
(res*=i)%=mod;
return res;
}
bool isprime(ll n){
if(n<=1) return 0;
for(ll i=2;i*i<=n;++i)
if(n%i==0) return 0;
return 1;
}
int main() {
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n,k;
cin>>n>>k;
int sum=0;
for(int i=1;i<=n;++i){
for(int j=1;j<=k;++j) sum+=i*100+j;
}
cout<<sum;
return 0;
} | #include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <tuple>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <functional>
#include <numeric>
#include <iomanip>
#include <stdio.h>
//end of libraries
#define lnf 3999999999999999999
#define inf 999999999
#define PI 3.14159265359
#define endl "\n"
#define fi first
#define se second
#define pb push_back
#define ll long long
#define all(c) (c).begin(),(c).end()
#define sz(c) (int)(c).size()
#define mkp(a,b) make_pair(a,b)
#define make_unique(a) sort(all(a)),a.erase(unique(all(a)),a.end())
#define rsz(a,n) a.resize(n)
#define pii pair <int,int>
#define rep(i,n) for(int i = 0 ; i < n ; i++)
#define drep(i,n) for(int i = n-1 ; i >= 0 ; i--)
#define crep(i,x,n) for(int i = x ; i < n ; i++)
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
const int max_n = 5500;
using namespace std;
ll mod = 998244353 , fact[max_n];
ll binpow(ll a, ll b) {
a %= mod; ll res = 1;
while (b) { if (b%2) res = res * a % mod; a = a * a % mod; b /= 2; }
return res % mod;
}
void dofact() {
fact[0]=fact[1]=1;
rep(i,max_n-3){
if(i>1)fact[i] = fact[i-1]*i; fact[i] %= mod;
}
}
ll cnk(ll k , ll n){
if(k>n) return 0;
return (fact[n] * binpow(fact[k]*fact[n-k]%mod,mod-2)) % mod;
}
ll sub(ll a , ll b){
a%=mod; b%=mod;
return (((a-b)%mod)+mod)%mod;
}
ll prod(ll a , ll b){
a%=mod; b%=mod;
return ((a*b)%mod);
}
ll add(ll a , ll b){
a%=mod; b%=mod;
return (a+b)%mod;
}
ll dp[max_n] , nedp[max_n];
vector <ll> a;
int n,m;
void read() {
cin >> n >> m;
int x = 1;
while(x<=m) {
a.pb(x);
x*=2;
}
}
int main(){
fcin;
dofact();
read();
dp[0] = 1;
rep(idx,sz(a)){
int i = a[idx];
rep(j,m+1) nedp[j]=dp[j];
for(int j = 0 ; j <= m ; j++) {
if(dp[j]==0) continue;
int x = 2;
while(j+x*i <= m) {
int nxt = j+x*i;
nedp[nxt] = add(nedp[nxt],prod(dp[j],cnk(x,n)));
x+=2;
}
}
rep(j,m+1) dp[j] = nedp[j];
// rep(j,m+1) {
// cout << dp[j] << " ";
// }cout << "\n";
}
cout << dp[m] << "\n";
/*
*/
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define DEBUG(...) debug(#__VA_ARGS__, __VA_ARGS__)
#else
#define DEBUG(...) 6
#endif
template<typename T, typename S> ostream& operator << (ostream &os, const pair<T, S> &p) {return os << "(" << p.first << ", " << p.second << ")";}
template<typename C, typename T = decay<decltype(*begin(declval<C>()))>, typename enable_if<!is_same<C, string>::value>::type* = nullptr>
ostream& operator << (ostream &os, const C &c) {bool f = true; os << "["; for (const auto &x : c) {if (!f) os << ", "; f = false; os << x;} return os << "]";}
template<typename T> void debug(string s, T x) {cerr << s << " = " << x << "\n";}
template<typename T, typename... Args> void debug(string s, T x, Args... args) {for (int i=0, b=0; i<(int)s.size(); i++) if (s[i] == '(' || s[i] == '{') b++; else
if (s[i] == ')' || s[i] == '}') b--; else if (s[i] == ',' && b == 0) {cerr << s.substr(0, i) << " = " << x << " | "; debug(s.substr(s.find_first_not_of(' ', i + 1)), args...); break;}}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long n;
cin >> n;
vector<long long> f{1, 2};
while (f.back() < n)
f.push_back(f[f.size()-2] + f[f.size()-1]);
vector<int> ret;
for (int i=0; i<(int)f.size(); i++) {
while (n >= f[f.size()-i-1]) {
ret.push_back(1 + (i % 2 != f.size() % 2));
n -= f[f.size()-i-1];
}
ret.push_back(3 + (i % 2 == f.size() % 2));
}
cout << ret.size() << "\n";
for (int x : ret)
cout << x << "\n";
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
template<typename T>
ostream& operator<<(ostream &os, vector<T> &v){
string sep = " ";
if(v.size()) os << v[0];
for(int i=1; i<v.size(); i++) os << sep << v[i];
return os;
}
template<typename T>
istream& operator>>(istream &is, vector<T> &v){
for(int i=0; i<v.size(); i++) is >> v[i];
return is;
}
#ifdef DBG
void debug_(){ cout << endl; }
template<typename T, typename... Args>
void debug_(T&& x, Args&&... xs){
cout << x << " "; debug_(forward<Args>(xs)...);
}
#define dbg(...) debug_(__VA_ARGS__)
#else
#define dbg(...)
#endif
int main() {
ios_base::sync_with_stdio(false);
cout << setprecision(20) << fixed;
ll n; cin >> n;
ll x = 3;
for(int a=1; x<n; a++,x*=3){
ll m = n-x;
int b = 0;
while(m%5==0){
m /= 5;
b++;
}
if(m==1&&b>0){
cout << a << " " << b << endl;
return 0;
}
}
cout << -1 << endl;
return 0;
} |
# include<iostream>
# include <algorithm>
# include <queue>
# include <vector>
using namespace std;
int n;
long long a[200010],b[200010];
long long c[200010];
int main(void)
{
scanf("%d",&n);
for (int i=1;i<=n;i++)
scanf("%lld",&a[i]);
for (int i=1;i<=n;i++)
scanf("%lld",&b[i]);
long long ans = 0;
long long maxa=0,maxb=0;
for (int i=1;i<=n;i++)
{
ans = max(ans,a[i]*b[i]);
ans = max(ans,maxa*b[i]);
// ans = max(ans,maxb*a[i]);
c[i] = ans;
maxa = max(maxa,a[i]);
// maxb = max(maxb,b[i]);
}
for (int i=1;i<=n;i++)
printf("%lld\n",c[i]);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define M 1000000007
#define U 998244353
#define N 1000005
#define int long long
#define sz(c) (int)c.size()
#define fr first
#define ll long long
#define sc second
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define rep(i,a,n) for(int i=a ; i<n ; i++)
#define r0 return 0;
#define endl '\n'
#define INF (int)1e15
/* Debug Begins */
# define trace(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); \
stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
string to_string(char c) { return "'" + string(1, c) + "'";}
string to_string(string s) { return '"' + s + '"';}
string to_string(bool f) { if(f) return "True"; else return "False";}
string to_string(const char* s) { return to_string((string) s);}
template<typename A> string to_string(A);
template<typename A, typename B> string to_string(pair<A, B> p){
return "(" + to_string(p.first) + ": " + to_string(p.second) + ")";}
template<typename A> string to_string(A v) {bool f = false; string r = "{";
for (auto x: v) {if (f) r += ", "; r += to_string(x); f = true;} return r += "}";}
template<typename A> string to_string(vector<vector<A>> v) {string r;
for (auto x: v) r += "\n" + to_string(x); return r;}
int Nerr;
template<typename A> string to_string(A *p) {return to_string(vector<A>(p, p + Nerr));}
void err(istream_iterator<string>) { cerr << endl; }
template<typename T,typename... Args> void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << to_string(a) << "; "; err(++it, args...); }
template<typename T> void kek(T ans) {cout << ans << endl; exit(0);}
#define Lu(...) [&] (auto &&u) { return __VA_ARGS__; }
#define Luv(...) [&] (auto &&u, auto &&v) { return __VA_ARGS__; }
/***************************************************************/
bool sortbysec(const pair<int,int> &a,
const pair<int,int> &b)
{
return (a.second < b.second);
}
signed main()
{
ios_base::sync_with_stdio(0);
int TESTS=1;
// cin>>TESTS;
while(TESTS--)
{
double ans = 0;
int n;
cin >> n;
rep(i,1,n) ans += n*1.0/i;
cout << fixed << setprecision(10) << ans << endl;
}
} |
#include <bits/stdc++.h>
using namespace std;
void fast() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
vector<string> vec_splitter(string s) {
s += ',';
vector<string> res;
while(!s.empty()) {
res.push_back(s.substr(0, s.find(',')));
s = s.substr(s.find(',') + 1);
}
return res;
}
void debug_out(
vector<string> __attribute__ ((unused)) args,
__attribute__ ((unused)) int idx,
__attribute__ ((unused)) int LINE_NUM) { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
if(idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") ";
stringstream ss; ss << H;
cerr << args[idx] << " = " << ss.str();
debug_out(args, idx + 1, LINE_NUM, T...);
}
#ifdef LOCAL
#define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
#define debug(...) 42
#endif
double get_time() {
return 1.0 * clock() / CLOCKS_PER_SEC;
}
const int MAX = 2E5 + 100, INF = 1E9 + 5;
int n, k;
vector<vector<int>> adj(MAX), at_h(MAX);
int h[MAX], dp1[MAX], dp2[MAX];
int DFS(int u, int p, int d) {
int ret = 0;
for(int v : adj[u]) {
if(v == p)
continue;
ret += DFS(v, u, d);
if(dp1[v] > -1 && dp1[v] < d) {
if(dp1[u] == -1 || dp1[u] > dp1[v] + 1)
dp1[u] = dp1[v] + 1;
}
if(dp2[v] > -1) {
dp2[u] = max(dp2[u], dp2[v] + 1);
}
// h[u] = max(h[u], h[v] + 1);
}
if(dp2[u] > -1 && dp1[u] > -1 && d - dp1[u] >= dp2[u]) {
dp2[u] = -1;
}
if(dp2[u] == d || (u == 0 && (dp1[u] == -1 || dp2[u] > -1))) {
ret++;
dp2[u] = -1;
dp1[u] = 0;
}
if(dp1[u] == -1 && dp2[u] == -1) {
dp2[u] = 0;
}
// if(dp2[u] == d || (u == 0 && (dp2[u] > -1 || dp1[u] == -1))) {
// ret++;
// dp2[u] = -1;
// dp1[u] = 0;
// }
return ret;
}
int min_need(int d) {
// int ret = 0;
// int cur = -(d + 1);
// while(h[0] >= cur + d + 1) {
// cur += 2*d + 1;
// ret += at_h[cur].size();
// }
// if(cur != h[0])
// ret += 1;
// return ret;
fill(begin(dp1), end(dp1), -1);
fill(begin(dp2), end(dp2), -1);
return max(1, DFS(0, -1, d));
}
int main() {
cin >> n >> k;
for(int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
--u, --v;
adj[u].push_back(v);
adj[v].push_back(u);
}
// DFS(0, -1);
// for(int i = 0; i < n; i++)
// at_h[h[i]].push_back(i);
int lo = 1, hi = n - 1;
while(lo < hi) {
int mid = (lo + hi) / 2;
if(min_need(mid) > k)
lo = mid + 1;
else
hi = mid;
}
cout << lo << '\n';
} | #include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <optional>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#ifdef ENVIRONMENT_LINKED_BOOST
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#endif
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using lpair = std::pair<ll, ll>;
#define REP(i, a, b) for (ll i = a; i < b; ++i)
#define REPREV(i, a, b) for (ll i = a; i > b; --i)
const int _ = []() {
std::cin.tie(nullptr);
std::ios::sync_with_stdio(false);
std::cout << std::fixed << std::setprecision(10);
return 0;
}();
template <typename value_t>
void resize(value_t& v, const value_t& val) { v = val; }
template <typename vec_t, typename value_t, typename... arg_t>
void resize(std::vector<vec_t>& v, const value_t& val, int size, arg_t... arg) {
v.resize(size);
for (auto& c : v) resize(c, val, arg...);
}
template <typename A, typename B>
void chmin(A& a, const B& b) { a = min(a, static_cast<A>(b)); };
template <typename A, typename B>
void chmax(A& a, const B& b) { a = max(a, static_cast<A>(b)); };
vector<vector<int>> link;
/*
(x,y)
x
まだ被覆されてない最遠の頂点とparの距離
存在しなければ0
y
parから距離y以下の頂点は無条件で被覆される
存在しなければ負の値
*/
pair<int, int> dfs(int ind, int par, int& ans, int range) {
int x = 0;
int y = -1;
for (int c : link[ind]) {
if (c == par) continue;
auto ret = dfs(c, ind, ans, range);
chmax(x, ret.first);
chmax(y, ret.second);
}
if (x == range) {
++ans;
return {0, range - 1};
}
if (x <= y) {
return {0, y - 1};
}
return {x + 1, y - 1};
}
int score(int range) {
int ans = 0;
auto ret = dfs(0, -1, ans, range);
if (ret.first) ++ans;
return ans;
}
int main() {
int N, K;
cin >> N >> K;
link.resize(N);
REP(i, 0, N - 1) {
int a, b;
cin >> a >> b;
--a;
--b;
link[a].push_back(b);
link[b].push_back(a);
}
int l = -1;
int r = N;
while (r - l > 1) {
int mid = l + (r - l) / 2;
(score(mid) <= K ? r : l) = mid;
}
cout << r << endl;
return 0;
} |
#include "bits/stdc++.h"
#define int long long
#define endl '\n'
using namespace std;
typedef long long ll;
typedef long double ld;
#define db(x) cerr << #x << ": " << x << '\n';
#define read(a) int a; cin >> a;
#define reads(s) string s; cin >> s;
#define readb(a, b) int a, b; cin >> a >> b;
#define readc(a, b, c) int a, b, c; cin >> a >> b >> c;
#define readarr(a, n) int a[(n) + 1] = {}; FOR(i, 1, (n)) {cin >> a[i];}
#define readmat(a, n, m) int a[n + 1][m + 1] = {}; FOR(i, 1, n) {FOR(j, 1, m) cin >> a[i][j];}
#define print(a) cout << a << endl;
#define printarr(a, n) FOR (i, 1, n) cout << a[i] << " "; cout << endl;
#define printv(v) for (int i: v) cout << i << " "; cout << endl;
#define printmat(a, n, m) FOR (i, 1, n) {FOR (j, 1, m) cout << a[i][j] << " "; cout << endl;}
#define all(v) v.begin(), v.end()
#define sz(v) (int)(v.size())
#define rz(v, n) v.resize((n) + 1);
#define pb push_back
#define fi first
#define se second
#define vi vector <int>
#define pi pair <int, int>
#define vpi vector <pi>
#define vvi vector <vi>
#define setprec cout << fixed << showpoint << setprecision(20);
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll N = 2e5 + 1;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int power (int a, int b = mod - 2)
{
int res = 1;
while (b > 0) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
vi adj[101], vis;
int cnt = 0;
void dfs (int cur)
{
cnt++;
vis[cur] = 1;
for (int i: adj[cur])
if (!vis[i]) dfs(i);
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
read(n);
FOR (i, 1, n)
{
reads(s);
FOR (j, 1, n)
if (s[j - 1] == '1') adj[j].pb(i);
}
ld ans = 0;
FOR (i, 1, n)
{
vis.assign(n + 1, 0);
cnt = 0;
dfs(i);
ans += 1/(ld)(cnt);
}
setprec;
print(ans);
}
| #include<bits/stdc++.h>
using namespace std;
#pragma region atcoder
//#include <atcoder/all>
//using namespace atcoder;
//using mint = modint998244353;
//using mint = modint1000000007;
#pragma endregion
#pragma region macros
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vs = vector<string>;
using vl = vector<ll>;
using vb = vector<bool>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rrep(i, n) for(int i = n - 1; i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define dame { puts("No"); return 0;}
#define y_n {puts("Yes");}else{puts("No");}
#pragma endregion
#pragma region debug for var, v, vv
#define debug(var) do{std::cout << #var << " : ";view(var);}while(0)
template<typename T> void view(T e){std::cout << e << std::endl;}
template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cout << e << " "; } std::cout << std::endl;}
template<typename T> void view(const std::vector<std::vector<T> >& vv){cout << endl;int cnt = 0;for(const auto& v : vv){cout << cnt << "th : "; view(v); cnt++;} cout << endl;}
#pragma endregion
const ll mod = 1000000007;
const int inf = 1001001001;
const ll INF = 1001001001001001001;
const int MAX = 2000005;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
template<class T>bool chmax(T &a, const T b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T b) { if (b<a) { a=b; return 1; } return 0; }
ll modpow(ll a, ll p){ll ret = 1; while(p){if(p & 1){ret = ret * a % mod;} a = a * a % mod; p >>= 1;} return ret;}
/*--------------------------------------------------------------------------------------------------------------------------------*/
ll dist[20][20];
ll dp[(1<<20) + 1][21];
int n;
ll rec(int bit, int v)
{
// すでに探索済みだったらリターン
if (dp[bit][v] != -1) return dp[bit][v];
// 初期値
if (bit == (1<<v)) {
return dp[bit][v] = 0;
}
// 答えを格納する変数
ll res = INF;
// bit の v を除いたもの
int prev_bit = bit & ~(1<<v);
// v の手前のノードとして u を全探索
for (int u = 0; u < n; ++u) {
if (!(prev_bit & (1<<u))) continue; // u が prev_bit になかったらダメ
// 再帰的に探索
if (res > rec(prev_bit, u) + dist[u][v]) {
res = rec(prev_bit, u) + dist[u][v];
}
}
return dp[bit][v] = res; // メモしながらリターン
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> n;
vl x(n), y(n), z(n);
rep(i,n) cin >> x[i] >> y[i] >> z[i];
rep(i,n) rep(j,n) dist[i][j] = abs(x[i] - x[j]) + abs(y[i] - y[j]) + max(0ll, z[j] - z[i]);
for (int bit = 0; bit < (1<<n); ++bit){
for (int v = 0; v < n; ++v){
dp[bit][v] = -1;
if(__builtin_popcount(bit) == 1 && (bit & (1 << v))){
dp[bit][v] = dist[0][v];
}
}
}
ll res = rec((1<<n)-1, 0);
cout << res << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pll = pair<ll,ll>;
using pld = pair<ld,ld>;
using vll = vector<ll>;
using vld = vector<ld>;
#define _GLIBCXX_DEBUG
#define rep(j, m) for (ll j = 0; j < (ll)(m); j++)
#define rep2(i, l, n) for (ll i = l; i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
const ld PI = 3.1415926535897932;
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
int main() {
ll H,W;
cin >> H >> W;
vector<string> V(H);
rep(i,H) {cin >> V[i];}
vll B(H,0);
vll b(W,0);
ll ans = 0;
rep(i,H) {
if (B[i] == 0) {
ll flag = 0;
queue<ll> Q;
Q.push(i);
B[i]++;
while(Q.size()) {
rep(j,W) {
if (V[Q.front()][j] == '#' && b[j] == 0) {
b[j]++;
if (j == 0 || j == W - 1) {flag++;}
rep(k,H) {
if (V[k][j] == '#') {
if (B[k] == 0) {
Q.push(k);
B[k]++;
}
}
}
}
}
if (Q.front() == 0 || Q.front() == H - 1) {flag++;}
Q.pop();
}
if (flag == 0) {
ans++;
}
}
}
vll C(W,0);
vll c(H,0);
ll ans2 = 0;
rep(i,W) {
if (C[i] == 0) {
ll flag = 0;
queue<ll> Q;
Q.push(i);
C[i]++;
while(Q.size()) {
rep(j,H) {
if (V[j][Q.front()] == '#' && c[j] == 0) {
c[j]++;
if (j == 0 || j == H - 1) {flag++;}
rep(k,W) {
if (V[j][k] == '#') {
if (C[k] == 0) {
Q.push(k);
C[k]++;
}
}
}
}
}
if (Q.front() == 0 || Q.front() == W - 1) {flag++;}
Q.pop();
}
if (flag == 0) {
ans2++;
}
}
}
cout << min(ans,ans2) << endl;
}
| #include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <stack>
#include <functional>
#include <algorithm>
#include <cmath>
#include <tuple>
#if defined _DEBUG
#include "TestCase.h"
#include "Util.h"
#endif
using namespace std;
typedef int64_t ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, int> plli;
typedef tuple<int, int, int> tiii;
#define PI 3.14159265358979
static const int Mod = 1000000007;
bool Next(vector<int> &border, int maxNum)
{
int size = border.size();
int i = 1;
bool ret = true;
if (border[size - i] < maxNum - i + 1)
{
border[size - i]++;
}
else if (size == i)
{
ret = false;
}
else
{
bool flag = true;
while (flag)
{
i++;
if (border[size - i] < maxNum - i + 1)
{
border[size - i]++;
for (int j = i - 1; j >= 1; j--)
{
border[size - j] = border[size - j - 1] + 1;
}
flag = false;
}
else if (i == size)
{
flag = false;
ret = false;
}
}
}
return ret;
}
int main(void)
{
#if 0
vector<pair<ll, ll>> format(2);
//format[0] = make_pair(1, 1000000000);
format[0] = make_pair(1, 100);
format[1] = make_pair(1, 100);
TestCase::MakeRandom(30, format);
return 0;
#endif
int N;
cin >> N;
vector<int> A(N);
for (int i = 0; i < N; i++)
cin >> A[i];
// 分割ナシ
int minval = A[0];
for (int i = 1; i < N; i++)
{
minval |= A[i];
}
// 分割アリ
for (int S = 2; S <= N; S++) // 分割数
{
vector<int> border(S-1); // 境界
// 境界の初期化
for (int i = 1; i < S; i++)
{
border[i - 1] = i;
}
//do
//{
// for (int b : border)
// cout << b << " ";
// cout << endl;
//} while (Next(border, N));
do
{
//cout << "border : ";
// for (int b : border)
// cout << b << " ";
// cout << endl;
vector<int> vals(S);
int begin = 0;
for (int s = 0; s < S && begin < N; s++)
{
int end;
if (s < border.size())
end = border[s];
else
end = N;
vals[s] = A[begin];
for (int i = begin; i < end; i++)
{
vals[s] |= A[i];
}
begin = end;
//cout << "vals[" << s << "] = " << vals[s] << " ";
}
//cout << endl;
int val = vals[0];
for (int s = 1; s < S; s++)
{
val = val ^ vals[s];
}
minval = min(minval, val);
}
while (Next(border, N));
}
cout << minval << endl;
return 0;
}
|
#include<iostream>
using namespace std;
int main()
{
int a,n,b,c;
cin>>a>>n>>b;
c=a-n+b;
cout<<c;
return 0;
} | #include<bits/stdc++.h> // 76 87
using namespace std;
#define rep(x,k,n) for(int x=(k); x<(n); ++x)
#define repr(x,k,n) for(int x=(k); x>(n); --x)
#define tr(it,a) for(auto it = (a).begin(); it != (a).end(); ++it)
#define ll long long
#define siz(x) ((int)(x).size())
#define print(x) cout << x << '\n'
#define prints(x) cout << x << " "
#define eb emplace_back
#define mp make_pair
#define F first
#define S second
#define o2(x) ((x)*(x))
#define all(x) (x).begin(), (x).end()
#define allr(x) (x).rbegin(), (x).rend()
#define clr(x,k) memset((x), (k), sizeof((x)))
#define sortall(x) sort(all(x))
#define sortalld(x) sort(all(x), greater<int>())
#define printv(v) rep(x, 0, siz(v)){prints(v[x]);}cout<<'\n'
#define print2dv(V) rep(y, 0, siz(V)){printv(V[y]);}
#define out(x) cout << ((x) ? "YES" : "NO") << '\n';
#define PI 3.1415926535897932384626
#define inf INT_MAX
#define ninf INT_MIN
#define int long long
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<vc> vvc;
typedef vector<vb> vvb;
typedef vector<vpii> vvpii;
template <typename Arg1>
void debug_out(const char* name, Arg1&& arg1){
cout << name << " = " << arg1 << " ]" << '\n';
}
template <typename Arg1, typename... Args>
void debug_out(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');
cout.write(names, comma - names) << " = " << arg1 <<",";
debug_out(comma+1, args...);
}
#ifndef ONLINE_JUDGE
#define deb(...) cout << "[ ", debug_out(#__VA_ARGS__, __VA_ARGS__)
#else
#define deb(...)
#endif
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
const int mod = 1e9+7;
const int N = 3e5, M = N;
/*
->Do not forget to check the edge cases.
->Make sure you are submitting the right file.
->Do not make "easy->complicated".
*/
//-----------------------------------------------------------------------------
void Solve_main() {
int n, m, k;
cin >> n >> m >> k;
cout << n - m + k << '\n';
}
//-----------------------------------------------------------------------------
int32_t main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
srand(chrono::high_resolution_clock::now().time_since_epoch().count());
// cout << setprecision(12) << fixed;
int tc = 1;
// cin >> tc;
while(tc--) {
Solve_main();
}
cerr<<"[time:"<<1.0*clock()/CLOCKS_PER_SEC<<"s] ";
return 0;
} |
/*
Author: klmcoder
Date : 20210213 1834 hrs.
Info : atcoder [arc112-A]
*/
#include <iostream>
int main(void)
{
int t, a, b;
long v1;
unsigned long v2;
std::cin >> t;
while(t--)
{
std::cin >> a >> b;
v1 = b - 2*(long)a + 1;
if(v1<0)
std::cout << "0\n";
else
{
v2=(v1%2==0)?v1/2:(v1+1)/2;
v2*=(v1%2==0)?(unsigned long)v1+1:v1;
std::cout << v2 << "\n";
}
}
return 0;
}
| #include<bits/stdc++.h>
using ll = long long;
using namespace std;
const ll mod = 1e9 + 7;
void solve() {
ll l, r;
cin >> l >> r;
if (r < 2 * l) {
cout << 0 << endl;
return;
}
cout << (1 + r - 2*l + 1) * (r - 2*l + 1) / 2 << endl;
}
int main(void) {
int t;
cin >> t;
for(int i = 0;i < t;++i) {
solve();
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
int main(){
string S;
cin>>S;
list<char> l;
int mode=0;
for(int i=0;i<S.size();i++){
if(S[i]=='R') mode=1-mode;
else{
if(S.size()==0) l.emplace_back(S[i]);
else{
if(mode==0){
char a=*(l.rbegin());
if(a==S[i]) l.pop_back();
else l.emplace_back(S[i]);
}
else{
char a=*(l.begin());
if(a==S[i]) l.pop_front();
else l.emplace_front(S[i]);
}
}
}
}
if(mode==0){
auto it=l.begin();
while(it!=l.end()){
cout<<*it;
it++;
}
cout<<endl;
}
else{
auto it=l.rbegin();
while(it!=l.rend()){
cout<<*it;
it++;
}
cout<<endl;
}
}
| constexpr bool isDebugMode = false;
int testcase = 1;
#include <bits/stdc++.h>
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
using namespace std;
typedef long long ll;
typedef pair<long long, long long> P;
struct edge{long long to,cost;};
const int inf = 1 << 27;
const long long INF = 1LL << 60;
const int COMBMAX = 1001001;
const long long MOD = 1000000007; //998244353;
const long long dy[] = {-1, 0, 0, 1};
const long long dx[] = {0, -1, 1, 0};
const string abc = "abcdefghijklmnopqrstuvwxyz";
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define eachdo(v, e) for (const auto &e : (v))
#define all(v) (v).begin(), (v).end()
#define lower_index(v, e) (long long)distance((v).begin(), lower_bound((v).begin(), (v).end(), e))
#define upper_index(v, e) (long long)distance((v).begin(), upper_bound((v).begin(), (v).end(), e))
long long mpow(long long a, long long n, long long mod = MOD){long long res = 1; while(n > 0){if(n & 1)res = res * a % mod; a = a * a % mod; n >>= 1;} return res;}
void pt(){cout << endl; return;}
template<class Head> void pt(Head&& head){cout << head; pt(); return;}
template<class Head, class... Tail> void pt(Head&& head, Tail&&... tail){cout << head << " "; pt(forward<Tail>(tail)...);}
void dpt(){if(!isDebugMode) return; cout << endl; return;}
template<class Head> void dpt(Head&& head){if(!isDebugMode) return; cout << head; pt(); return;}
template<class Head, class... Tail> void dpt(Head&& head, Tail&&... tail){if(!isDebugMode) return; cout << head << " "; pt(forward<Tail>(tail)...);}
template<class T> void enu(T v){rep(i, v.size()) cout << v[i] << " " ; cout << endl;}
template<class T> void enu2(T v){rep(i, v.size()){rep(j, v[i].size()) cout << v[i][j] << " " ; cout << endl;}}
template<class T> void debug(T v){if(!isDebugMode) return; rep(i, v.size()) cout << v[i] << " " ; cout << endl;}
template<class T> void debug2(T v){if(!isDebugMode) return; rep(i, v.size()){rep(j, v[i].size()) cout << v[i][j] << " " ; cout << endl;}}
template<class T1, class T2> inline bool chmin(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}
template<class T1, class T2> inline bool chmax(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}
template<class T1, class T2> long long recgcd(T1 a, T2 b){return a % b ? recgcd(b, a % b) : b;}
bool valid(long long H, long long W, long long h, long long w) { return 0 <= h && h < H && 0 <= w && w < W; }
void solve();
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
// cin >> testcase;
while(testcase--) solve();
return 0;
}
void solve(){
ll N; cin >> N;
pt(N % 2 ? "Black" : "White");
} |
/*
#pragma GCC optimize("O2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,fma")
//*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int ,int > pii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll maxn = 3e6;
const ll mod =1e9+7;
const ld PI = acos((ld)-1);
#define pb push_back
#define endl '\n'
#define dokme(x) cout << x , exit(0)
#define migmig ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ms(x , y) memset(x , y , sizeof x)
ll pw(ll a, ll b, ll md = mod){ll res = 1;while(b){if(b&1){res=(a*res)%md;}a=(a*a)%md;b>>=1;}return(res);}
int n;
char aa , ab , ba , bb;
ll F[maxn];
ll f(int x){
if(x <= 1)return(F[x] = 1);
if(F[x])return(F[x]);
return( F[x] = (f(x - 1) + f(x - 2))%mod );
}
int32_t main(){
migmig;
cin >> n >> aa >> ab >> ba >> bb;
if(n <= 3)dokme(1);
if(aa == ab && ab == ba && ba == bb)dokme(1);
if(ab == 'B'){
if(bb == 'B')dokme(1);
if(ba == 'A')dokme(pw(2 , n - 3));
dokme(f(n - 2));
}
if(aa == 'A')dokme(1);
if(ba == 'B')dokme(pw(2 , n - 3));
dokme((f(n - 2)));
return(0);
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int MOD = 998244353;
long long modpow(ll a, ll n) {
ll res = 1;
while (n > 0) {
if (n & 1) res = res * a % MOD;
a = a * a % MOD;
n >>= 1;
}
return res;
}
ll modinv(ll a, ll m) {
ll b = m, u = 1, v = 0;
while (b) {
ll t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
u %= m;
if (u < 0) u += m;
return u;
}
ll dp[5005][5005];
int main() {
ll H, W, K;
cin >> H >> W >> K;
vector<vector<char>> G(H, vector<char>(W, '?'));
rep(i, K) {
int h, w;
cin >> h >> w;
h--, w--;
char c;
cin >> c;
G[h][w] = c;
}
rep(i, H) rep(j, W) dp[i][j] = 0;
dp[0][0] = modpow(3LL, H * W - K);
rep(i, H) rep(j, W) {
if (i == H - 1 && j == W - 1) break;
if (i + 1 < H) {
if (G[i][j] == 'D' || G[i][j] == 'X') {
dp[i + 1][j] += dp[i][j];
dp[i + 1][j] %= MOD;
}
if (G[i][j] == '?') {
dp[i + 1][j] += dp[i][j] * modinv(3LL, MOD) % MOD * 2 % MOD;
dp[i + 1][j] %= MOD;
}
}
if (j + 1 < W) {
if (G[i][j] == 'R' || G[i][j] == 'X') {
dp[i][j + 1] += dp[i][j];
dp[i][j + 1] %= MOD;
}
if (G[i][j] == '?') {
dp[i][j + 1] += dp[i][j] * modinv(3LL, MOD) % MOD * 2 % MOD;
dp[i][j + 1] %= MOD;
}
}
}
cout << dp[H - 1][W - 1] << endl;
} |
#include <cstdio>
#include <algorithm>
using namespace std;
int p[505];
int ans[250005];
int main()
{
int T,n,m;
bool f,ff;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&p[i]);
m=0;
f=true;
for(int i=1;i<=n;i++)
if(p[i]!=i)
f=false;
while(!f)
{
ff=false;
for(int i=1;i<=n-1;i++)
if(i%2!=m%2&&p[i]>p[i+1])
{
ans[++m]=i;
swap(p[i],p[i+1]);
ff=true;
break;
}
if(!ff)
{
if(m%2!=(n-1)%2)
{
ans[++m]=n-1;
swap(p[n-1],p[n]);
}
else
{
ans[++m]=n-2;
swap(p[n-2],p[n-1]);
}
}
f=true;
for(int i=1;i<=n;i++)
if(p[i]!=i)
f=false;
}
printf("%d\n",m);
for(int i=1;i<=m;i++)
if(i!=m)
printf("%d ",ans[i]);
else printf("%d\n",ans[i]);
}
return 0;
}
| #include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
long long dp[60][2],i,j,n,a[61],x=0,y=0,k=1,l=0;
int main(void)
{
char s[60][60];
scanf("%lld",&n);
for(i=0;i<n;i++) scanf("%s",s[i]);
dp[0][0]=1; dp[0][1]=1;
for(i=0;i<n;i++){
for(j=0;j<2;j++){
if(s[i][0]=='O'){
x=max(j,k); y=max(j,l);
dp[i+1][x]+=dp[i][j];
dp[i+1][y]+=dp[i][j];
}
else{
x=min(j,k); y=min(j,l);
dp[i+1][x]+=dp[i][j];
dp[i+1][y]+=dp[i][j];
}
}
}
printf("%lld\n",dp[n][1]);
return 0;
} |
#include <iostream>
#include <iomanip>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <set>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define repp(i, s, n) for(int i = (int)s; i < (int)n; i++)
#define Yes() cout << "Yes" << endl;
#define No() cout << "No" << endl;
using ll = long long;
int main(void){
double x, y, z;
cin >> x >> y >> z;
int ans = (y / x)*z;
if (((double)ans/z) == (y/x))ans--;
cout << ans << endl;
} | /**
* author: doublevgp
* Which can not destroy me will make me strong
**/
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + 10;
const int maxm = 100 + 10;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
ll extgcd(ll a, ll b, ll &x, ll &y) {
if (b == 0) {
x = 1, y = 0;
return a;
}
ll g = extgcd(b, a % b, x, y);
ll t = x;
x = y;
y = t - a / b * y;
return g;
}
ll n, s, k;
void sol() {
cin >> n >> s >> k;
ll x, y;
ll g = extgcd(k, n, x, y);
if (s % g != 0) {
cout << -1 << endl;
return ;
}
else if (g == 1) {
cout << (x * (-s) % n + n) % n << endl;
}
else {
n /= g;
s /= g;
k /= g;
cout << (x * (-s) % n + n) % n << endl;
}
return ;
}
int main()
{
ios;
int t;
cin >> t;
while (t--) {
sol();
}
return 0;
} |
/*Lucky_Glass*/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=2e5+10;
inline int Rint(int &r){
int b=1,c=getchar();r=0;
while(c<'0' || '9'<c) b=c=='-'? -1:b,c=getchar();
while('0'<=c && c<='9') r=(r<<1)+(r<<3)+(c^'0'),c=getchar();
return r*=b;
}
int n,m,nstk,cnt;
int wei[N],now[N],uni[N],ans[N<<1][2],stk[N],fstk[N];
pair<int,int> lmt[N];
bool vis[N];
void Answer(int a,int b){
a=lmt[a].second,b=lmt[b].second;
ans[++m][0]=a,ans[m][1]=b;
swap(now[a],now[b]);
}
int main(){
Rint(n);
for(int i=1;i<=n;i++) Rint(lmt[i].first),lmt[i].second=i;
for(int i=1;i<=n;i++) Rint(wei[i]);
for(int i=1;i<=n;i++) Rint(now[i]);
for(int i=1;i<=n;i++)
if(now[i]!=i && wei[now[i]]>=lmt[i].first){
printf("-1\n");
return 0;
}
sort(lmt+1,lmt+1+n);
for(int i=1;i<=n;i++){
uni[lmt[i].second]=i;
if(lmt[i].second==now[lmt[i].second])
vis[i]=true;
}
for(int i=1;i<=n;i++)
if(!vis[i]){
nstk=0;
int tmp=i;
while(true){
stk[++nstk]=tmp;
tmp=uni[now[lmt[tmp].second]];
if(tmp==i) break;
}
fstk[cnt=1]=stk[1];
for(int j=2;j<=nstk;j++)
if(fstk[cnt]>stk[j]) Answer(fstk[cnt],stk[j]);
else fstk[++cnt]=stk[j];
for(int j=1;j<cnt;j++)
Answer(fstk[j],fstk[cnt]);
}
//for(int i=1;i<=n;i++)
// printf("%d %d\n",i,now[i]);
printf("%d\n",m);
for(int i=1;i<=m;i++)
printf("%d %d\n",ans[i][0],ans[i][1]);
return 0;
}
| #include <cstdio>
#include <cmath>
#include <iostream>
#include <set>
#include <algorithm>
#include <vector>
#include <map>
#include <cassert>
#include <string>
#include <cstring>
#include <queue>
using namespace std;
#define rep(i,a,b) for(int i = a; i < b; i++)
#define S(x) scanf("%d",&x)
#define S2(x,y) scanf("%d%d",&x,&y)
#define P(x) printf("%d\n",x)
#define all(v) v.begin(),v.end()
#define FF first
#define SS second
#define pb push_back
#define mp make_pair
typedef long long int LL;
typedef pair<int, int > pii;
typedef vector<int > vi;
const int N = 200001;
int A[N], B[N], P[N], IP[N];
vector<pii > v, ans;
int main() {
int n;
S(n);
rep(i,1,n+1) {
S(A[i]);
v.pb(mp(A[i], i));
}
rep(i,1,n+1) {
S(B[i]);
}
rep(i,1,n+1) {
S(P[i]);
IP[P[i]] = i;
}
sort(all(v));
rep(i,0,v.size()) {
int x = v[i].SS;
if(IP[x] == x) continue;
if(B[P[x]] >= A[x]) {
P(-1);
return 0;
}
ans.pb(mp(x, IP[x]));
int y = P[x];
IP[y] = IP[x];
P[IP[x]] = y;
IP[x] = x;
P[x] = x;
}
P((int)ans.size());
rep(i,0,ans.size()) {
printf("%d %d\n",ans[i].FF, ans[i].SS);
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define rep(i, j, n) for(int i = (j); i < (int)(n); i++)
#define print(t) cout << t << endl
using ll = long long;
using Graph = vector<vector<int>>;
#define SIZE(s) ll(s.size())
int main(){
int N, X; cin >> N >> X;
string S; cin >> S;
rep(i, 0, N){
if(S.at(i) == 'o'){X++; continue;}
if(S.at(i) == 'x'){
if(X == 0) continue;
else X--;
}
}
print(X);
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
vector<int> arr(3);
cin >> arr[0] >> arr[1] >> arr[2];
sort(arr.begin(), arr.end());
if (arr[0] == arr[1]) {
cout << arr[2];
} else if (arr[1] == arr[2]) {
cout << arr[0];
} else {
cout << 0;
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
#define mod 998244353
typedef long long ll;
int n,m;
ll memo[5005][5005],pwr[5005];
ll exp(int pos,int val){
if(pos==0)return 1;
if(memo[pos][val]!=-1)return memo[pos][val];
return memo[pos][val]=((val-1)*pwr[pos-1]+(m-val)*exp(pos-1,val))%mod;
}
int main(){
scanf("%d%d",&n,&m);
pwr[0]=1;
for(int i=1;i<=n;++i){
pwr[i]=(pwr[i-1]*m)%mod;
//printf("%lld ",pwr[i]);
}
//printf("\n");
ll ans=0;
memset(memo,-1,sizeof memo);
for(int i=0;i<n;++i){
for(int j=1;j<=m;++j){
ans+=exp(i,j)*pwr[n-i-1];
ans%=mod;
//printf("%d %d: %lld\n",i,j,exp(i,j));
}
}
printf("%lld\n",ans);
}
| // #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;
// ModInt begin
using ll = long long;
template<ll mod>
struct ModInt {
ll v;
ll mod_pow(ll x, ll n) const {
return (!n) ? 1 : (mod_pow((x*x)%mod,n/2) * ((n&1)?x:1)) % mod;
}
ModInt(ll a = 0) : v((a %= mod) < 0 ? a + mod : a) {}
ModInt operator+ ( const ModInt& b ) const {
return (v + b.v >= mod ? ModInt(v + b.v - mod) : ModInt(v + b.v));
}
ModInt operator- () const {
return ModInt(-v);
}
ModInt operator- ( const ModInt& b ) const {
return (v - b.v < 0 ? ModInt(v - b.v + mod) : ModInt(v - b.v));
}
ModInt operator* ( const ModInt& b ) const {return (v * b.v) % mod;}
ModInt operator/ ( const ModInt& b ) const {return (v * mod_pow(b.v, mod-2)) % mod;}
bool operator== ( const ModInt &b ) const {return v == b.v;}
bool operator!= ( const ModInt &b ) const {return !(*this == b); }
ModInt& operator+= ( const ModInt &b ) {
v += b.v;
if(v >= mod) v -= mod;
return *this;
}
ModInt& operator-= ( const ModInt &b ) {
v -= b.v;
if(v < 0) v += mod;
return *this;
}
ModInt& operator*= ( const ModInt &b ) {
(v *= b.v) %= mod;
return *this;
}
ModInt& operator/= ( const ModInt &b ) {
(v *= mod_pow(b.v, mod-2)) %= mod;
return *this;
}
ModInt pow(ll x) { return ModInt(mod_pow(v, x)); }
// operator int() const { return int(v); }
// operator long long int() const { return v; }
};
template<ll mod>
ModInt<mod> pow(ModInt<mod> n, ll k) {
return ModInt<mod>(n.mod_pow(n.v, k));
}
template<ll mod>
ostream& operator<< (ostream& out, ModInt<mod> a) {return out << a.v;}
template<ll mod>
istream& operator>> (istream& in, ModInt<mod>& a) {
in >> a.v;
return in;
}
// ModInt end
using mint = ModInt<998244353>;
mint tab[5010][5010];
int main() {
int N, M; cin >> N >> M;
for(int i=1; i<=M; i++) {
tab[i][0] = mint(1);
for(int j=1; j<=N; j++) {
tab[i][j] = tab[i][j-1] * mint(i);
}
}
mint ans(0);
for(int len=1; len<=N; len++) {
// len の両端はそれより低い (len == N なら関係ない)
// len 内の要素は全て val 以上
// len 内の要素に少なくとも 1 つ val の要素が存在
for(int val=1; val<=M; val++) {
mint tmp(0);
mint v = tab[M-val+1][len] - tab[M-val][len];
if(len < N - 1) {
// 端の場合 (2 通り)
tmp += tab[M][N-len-1] * mint(2) * v * mint(val - 1);
// そうでない場合
tmp += tab[M][N-len-2] * mint(N-len-1) * v * mint(val-1) * mint(val-1);
}
else if(len == N - 1) {
// 端の場合しかない
tmp += tab[M][N-len-1] * mint(2) * v * mint(val-1);
}
else {
// それ以上バリエーションなし
tmp += v;
}
ans += tmp;
}
}
cout << ans << endl;
return 0;
}
|
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
int main() {
int k,a,b,c;
cin>>k;
ll ans = 0;
for(int a=1;a<=k;a++){
for(int b=1;a*b<=k;b++){
ans += k/(a*b);
}
}
cout<<ans;
return 0;
} | #include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <iomanip>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cassert>
#include <complex>
#include <stdio.h>
#include <time.h>
#include <numeric>
#include <random>
#include <unordered_map>
#include <unordered_set>
#define all(a) a.begin(),a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
#define debug(x) cerr << __LINE__ << ' ' << #x << ':' << (x) << '\n'
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef complex<ld> com;
template<class T> using prique = priority_queue<T, vector<T>, greater<T>>;
constexpr int inf = 1000000010;
constexpr ll INF = 1000000000000000010;
constexpr int mod1e9 = 1000000007;
constexpr int mod998 = 998244353;
constexpr ld eps = 1e-12;
constexpr ld pi = 3.141592653589793238;
constexpr ll ten(int n) { return n ? 10 * ten(n - 1) : 1; };
int dx[] = { 1,0,-1,0,1,1,-1,-1 }; int dy[] = { 0,1,0,-1,1,-1,1,-1 };
void fail() { cout << "-1\n"; exit(0); } void no() { cout << "No\n"; exit(0); }
template<class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; }
template<class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; }
template<class T> istream &operator >> (istream &s, vector<T> &v) { for (auto &e : v) s >> e; return s; }
template<class T> ostream &operator << (ostream &s, const vector<T> &v) { for (auto &e : v) s << e << ' '; return s; }
struct fastio {
fastio() {
cin.tie(0); cout.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
cerr << fixed << setprecision(20);
}
}fastio_;
int main() {
int n;
cin >> n;
vector<P> ans(1024);
int q = 1;
while (q < n) q *= 2;
rep(i, q) {
ans[i].first = i / 2;
ans[i].second = i / 2 + q / 2;
}
set<int> st;
rep(i, q) st.insert(i);
int now = q - 1;
while (st.size() > n) {
st.erase(now);
now -= 2;
}
rep(i, q) {
if (!st.count(ans[i].first)) ans[i].first--;
if (!st.count(ans[i].second)) ans[i].second--;
}
map<int, int> idx;
int d = 0;
for (auto i : st) {
idx[i] = d;
d++;
}
rep(i, q) {
if (!st.count(i)) continue;
int pf = idx[ans[i].first];
int ps = idx[ans[i].second];
cout << pf + 1 << ' ' << ps + 1 << '\n';
}
} |
#include <cmath>
#include <vector>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <algorithm>
#include <set>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <climits>
#include <array>
#include <unordered_set>
#include <unordered_map>
#include <memory>
#include <functional>
#include <cfloat>
#include <numeric>
#include <random>
#include <sstream>
#include <bitset>
#include <complex>
#include <chrono>
#include <cfloat>
using namespace std;
long long int gcd(long long int a, long long int b) {
return b == 0 ? a : gcd(b, a % b);
}
long long int gx(long long int a, long long int b) {
long long int ax{ 1 }, bx{ 0 }, ay{ 0 }, by{ 1 };
const auto g = gcd(a, b);
a /= g;
b /= g;
long long int r = b;
while (r != 1) {
const auto x = ax * a + bx * b;
const auto y = ay * a + by * b;
const auto q = x / y;
r = x % y;
const auto az = ax - q * ay;
const auto bz = bx - q * by;
ax = ay;
bx = by;
ay = az % b;
by = bz % a;
}
return ay >= 0 ? ay : b + ay;;
}
int main() {
int test_case; cin >> test_case;
for (auto t = 0; t < test_case; ++t) {
int n, s, k; cin >> n >> s >> k;
const auto target = n - s;
const auto g = gcd(n, k);
if (target % g != 0) {
cout << -1 << '\n';
}
else {
const auto x = gx(k / g, n / g);
cout << x * (target / g) % (n / g) << '\n';
}
}
} | #include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define pb push_back
#define COUT(x) cout << (x) << "\n"
#define COUTF(x) cout << setprecision(15) << (x) << "\n"
#define ENDL cout << "\n"
#define DF(x) x.erase(x.begin())
#define ALL(x) x.begin(), x.end()
#define SORT(x) sort(ALL(x))
#define RSORT(x) sort(x.rbegin(), x.rend())
#define REVERSE(x) reverse(ALL(x))
#define MAX(x) *max_element(ALL(x))
#define MAXI(x) max_element(ALL(x)) - x.begin()
#define SUM(x) accumulate(ALL(x), 0ll)
#define COUNT(x, y) count(ALL(x), y);
#define ANS cout << ans << "\n"
#define YES cout << "YES\n";
#define NO cout << "NO\n";
#define Yes cout << "Yes\n";
#define No cout << "No\n";
#define init() \
cin.tie(0); \
ios::sync_with_stdio(false)
#define LINE cerr << "[debug] line: " << __LINE__ << endl;
#define debug(x) cerr << "[debug] " << #x << ": " << x << endl;
#define debugV(v) \
cerr << "[debugV] " << #v << ":"; \
rep(z, v.size()) cerr << " " << v[z]; \
cerr << endl;
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using mll = map<ll, ll>;
using qll = queue<ll>;
using P = pair<ll, ll>;
using vp = vector<P>;
using vs = vector<string>;
template <typename T>
inline istream& operator>>(istream& i, vector<T>& v) {
rep(j, v.size()) i >> v[j];
return i;
}
template <typename T1, typename T2>
inline istream& operator>>(istream& i, pair<T1, T2>& v) {
return i >> v.first >> v.second;
}
constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
constexpr ld PI = 3.141592653589793238462643383279;
ll get_digit(ll x) {
return to_string(x).size();
}
ll gcd(ll x, ll y) {
return y ? gcd(y, x % y) : x;
}
ll lcm(ll a, ll b) {
return a / gcd(a, b) * b;
}
template <class T>
bool chmax(T& a, const T& b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
bool chmin(T& a, const T& b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
ll mod_pow(ll x, ll n, ll mod) {
if (n == 0) return 1;
ll res = mod_pow(x * x % mod, n / 2, mod);
if (n & 1) res *= x;
res %= mod;
return res;
}
struct MAT {
vvll v = vvll(3, vll(3, 0));
// MAT(vvll a) : v(a);
MAT operator*(MAT a) {
MAT res;
rep(i, 3) rep(j, 3) rep(k, 3) res.v[i][j] += v[i][k] * a.v[k][j];
return res;
}
vll operator*(vll a) {
vll res = vll(3, 0);
rep(i, 3) rep(k, 3) res[i] += v[i][k] * a[k];
return res;
}
void print() {
rep(i, 3) {
rep(j, 3) cout << ' ' << v[i][j];
ENDL;
}
}
// http://satoh.cs.uec.ac.jp/ja/lecture/ComputerGraphics/2.pdf
};
signed main() {
init();
ll N;
cin >> N;
vvll X(N, vll(3, 1));
rep(i, N) cin >> X[i][0] >> X[i][1];
ll M;
cin >> M;
vector<MAT> mat;
MAT m = {vvll({{1, 0, 0}, {0, 1, 0}, {0, 0, 1}})};
mat.pb(m);
rep(i, M) {
ll t;
cin >> t;
MAT a;
if (t <= 2) {
if (t == 1) a = {vvll({{0, 1, 0}, {-1, 0, 0}, {0, 0, 1}})};
if (t == 2) a = {vvll({{0, -1, 0}, {1, 0, 0}, {0, 0, 1}})};
} else {
ll p;
cin >> p;
if (t == 3) a = {vvll({{-1, 0, 2 * p}, {0, 1, 0}, {0, 0, 1}})};
if (t == 4) a = {vvll({{1, 0, 0}, {0, -1, 2 * p}, {0, 0, 1}})};
}
m = a * m;
mat.pb(m);
}
ll Q;
cin >> Q;
rep(i, Q) {
ll a, b;
cin >> a >> b;
b--;
vll ans = mat[a] * X[b];
cout << ans[0] << ' ' << ans[1] << endl;
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll,ll>
#define FOR(I,A,B) for(ll I = ll(A); I < ll(B); ++I)
#define FORR(I,A,B) for(ll I = ll((B)-1); I >= ll(A); --I)
#define TO(x,t,f) ((x)?(t):(f))
#define SORT(x) (sort(x.begin(),x.end())) // 0 2 2 3 4 5 8 9
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) //xi>=v x is sorted
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) //xi>v x is sorted
#define NUM(x,v) (POSU(x,v)-POSL(x,v)) //x is sorted
#define REV(x) (reverse(x.begin(),x.end())) //reverse
ll gcd_(ll a,ll b){if(a%b==0)return b;return gcd_(b,a%b);}
ll lcm_(ll a,ll b){ll c=gcd_(a,b);return ((a/c)*b);}
#define NEXTP(x) next_permutation(x.begin(),x.end())
const ll INF=ll(1e16)+ll(7);
const ll MOD=1000000007LL;
#define out(a) cout<<fixed<<setprecision((a))
//tie(a,b,c) = make_tuple(10,9,87);
#define pop_(a) __builtin_popcount((a))
ll keta(ll a){ll r=0;while(a){a/=10;r++;}return r;}
#define num_l(a,v) POSL(a,v) //v未満の要素数
#define num_eql(a,v) POSU(a,v) //v以下の要素数
#define num_h(a,v) (a.size() - POSU(a,v)) //vより大きい要素数
#define num_eqh(a,v) (a.size() - POSL(a,v)) //v以上の要素数
int main(){
ll N;
cin >> N;
ll sumA = 0;
vector<ll> A(N);
FOR(i,0,N){
ll a,b;
cin >> a >> b;
sumA += a;
A[i] = a*2+b;
}
SORT(A);
REV(A);
FOR(i,0,N){
sumA -= A[i];
if(sumA<0){
cout << i+1 << endl;
return 0;
}
}
}
| #include<iostream>
#include<vector>
#include<algorithm>
long long N,A,B,p=0LL;
using namespace std;
int main(){
cin>>N;
vector<long long>town(N);
for(int i=0;i<N;i++){cin>>A>>B;p-=A;town[i]=A*2+B;}
sort(town.rbegin(),town.rend());
for(int i=0;i<N;p+=town[i],i++)if(p>0LL){cout<<i<<endl;return 0;}
cout<<N<<endl;
return 0;
} |
#include <iostream>
#include <string>
#include <vector>
#include <unordered_set>
#include <cmath>
long long CalcPoints(std::string str)
{
int strlen = str.size();
int pointArray[10] = {0};
long long ret = 0;
for(int i = 0; i < strlen; i++){
if(str[i] == '#') continue;
int tmp = str[i] - '0';
pointArray[tmp]++;
}
for(int i = 0; i < 10; i++){
ret += i * std::pow(10, pointArray[i]);
}
return ret;
}
void SetCardArray(std::vector<long long>& carray, std::string str)
{
int strlen = str.size();
for(int i = 0; i < strlen; i++){
if(str[i] == '#') continue;
carray[str[i] - '0']--;
}
}
int main()
{
long long a, b, c;
std::cin >> a >> b >> c;
std::cout << 21 - (a + b + c) << std::endl;
return 0;
} |
#include <bits/stdc++.h>
#define FIXED_FLOAT(x) std::fixed <<std::setprecision(20) << (x)
#define all(v) (v).begin(), (v).end()
using namespace std;
#define forn(i,n) for (int i = 0; i < (n); ++i)
using ll = long long;
int mod = (ll)1e9 + 7;
#define PI acos(-1)
typedef pair<int, int> pairs;
const int INF = 1e9 + 1;
const int N = 2e5 + 100;
const double eps = 1e-3;
template <typename XPAX>
void ckma(XPAX &x, XPAX y) {
x = (x < y ? y : x);
}
template <typename XPAX>
void ckmi(XPAX &x, XPAX y) {
x = (x > y ? y : x);
}
void solve() {
int h, w;
cin >> h >> w;
vector<vector<int>> a(h, vector<int>(w - 1));
forn(i, h) forn(j, w - 1) cin >> a[i][j];
vector<vector<int>> b(h - 1, vector<int>(w));
forn(i, h - 1) forn(j, w) cin >> b[i][j];
using P = tuple<int, int, int>;
auto myid = [&](P p) {
int r, c , f;
tie(r, c, f) = p;
return f * (h * w) + r * w + c;
};
using Q = pair<int, P>;
vector<int> dist(2 * h * w + 1, INF);
priority_queue<Q, vector<Q>, greater<Q>>q;
auto push = [&](P p, int D){
if(dist[myid(p)] <= D)return;
dist[myid(p)] = D;
q.push({D, p});
};
push(P{0, 0, 0}, 0);
auto get = [&](P p, int &r, int &c, int &f ) {
tie(r, c, f) = p;
};
while(q.size()) {
Q s = q.top();
q.pop();
int D = s.first;
int r, c, f;
get(s.second, r, c, f);
if(r == h - 1 && c == w - 1) {
cout << D << endl;
return;
}
if(f == 0) {
push(P{r, c, 1}, D + 1);
if(c)
push(P{r, c - 1, f},D + a[r][c - 1]);
if(c + 1 < w) {
push(P{r, c + 1, f},D + a[r][c]);
}
if(r + 1 < h)
push(P{r + 1, c, f}, D + b[r][c]);
}
else {
if(r) push(P{r - 1, c, f}, D + 1);
push(P{r, c, 0}, D);
}
}
}
void test_case() {
int t;
cin >> t;
forn(p, t) {
//cout << "Case #" << p + 1 << ": ";
solve();
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
solve();
}
|
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);
int n;
cin>>n;
string s;
cin>>s;
vector<int> min_rem(n+1, n);
min_rem[n]=0;
vector<int> ending_with_char(26, -1);
for(int i=n-1;i>=0;i--){
if(i==n-1){
min_rem[i]=-1;
ending_with_char[(int)(s.at(i)-'a')]=1;
}
else{
min_rem[i]=n;
for(int ch=0;ch<26;ch++){
if(ch!=(int)(s.at(i)-'a')){
if(ending_with_char[ch]!=-1){
min_rem[i]=min(min_rem[i], ending_with_char[ch]);
}
}
}
min_rem[i]=(min_rem[i]==n?-1:min_rem[i]);
if(min_rem[i+1]!=-1){
ending_with_char[(int)(s.at(i)-'a')]=min(ending_with_char[(int)(s.at(i)-'a')]==-1?n:ending_with_char[(int)(s.at(i)-'a')], min_rem[i+1]+1);
}
}
}
cout<<min_rem[0]<<"\n";
}
| #include <iostream>
#include <vector>
#include <array>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <string>
#include <sstream>
#include <algorithm>
#include <random>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <cassert>
#include <climits>
#include <bitset>
#include <functional>
#include <iomanip>
#include <random>
#define FOR_LT(i, beg, end) for (std::remove_const<decltype(end)>::type i = beg; i < end; i++)
#define FOR_LE(i, beg, end) for (std::remove_const<decltype(end)>::type i = beg; i <= end; i++)
#define FOR_DW(i, beg, end) for (std::remove_const<decltype(beg)>::type i = beg; end <= i; i--)
#define REP(n) for (std::remove_const<decltype(n)>::type repeat_index = 0; repeat_index < n; repeat_index++)
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << fixed << setprecision(20);
int n; cin >> n;
string s; cin >> s;
if (s.front() != s.back()) {
cout << 1 << endl;
return 0;
}
FOR_LT(i, 1, n - 2) {
if (s[i] != s.front() && s[i + 1] != s.back()) {
cout << 2 << endl;
return 0;
}
}
cout << -1 << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+1000;
int n,q,c[maxn],fa[maxn];
map<int,int> a[maxn];
void read(int &x)
{
char ch;bool ok;
for(ok=0,ch=getchar();!isdigit(ch);ch=getchar()) if(ch=='-') ok=1;
for(x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar()); if(ok) x=-x;
}
int getfa(int x)
{
if(fa[x]==x) return x;
else return fa[x]=getfa(fa[x]);
}
void join(int x,int y)
{
if(getfa(x)==getfa(y)) return;
if(a[getfa(x)].size()<a[getfa(y)].size()) swap(x,y);
for(pair<int,int>lll:a[getfa(y)]) a[getfa(x)][lll.first]+=lll.second;
fa[getfa(y)]=getfa(x);
}
int main()
{
read(n),read(q);
for(int i=1;i<=n;i++) read(c[i]);
for(int i=1;i<=n;i++) fa[i]=i;
for(int i=1;i<=n;i++) a[i][c[i]]++;
while(q--)
{
int x,y,z;
read(x),read(y),read(z);
if(x==1) join(y,z);
else printf("%d\n",a[getfa(y)][z]);
}
return 0;
}
| #include <iostream>
#include <algorithm>
#include <unordered_set>
#include <set>
#include <vector>
#include <queue>
#include <map>
#include <numeric>
#include <math.h>
using namespace std;
#define rep(i, n) for (long long int i = 0; i < (long long int)(n); i++)
#define irep(i, n) for (long long int i = 1; i <= (long long int)(n); i++)
#define drep(i, n, diff) for (long long int i = 0; i < (long long int)(n); i += diff)
#define mod 1000000007
#define cdeb(val) cout << #val << ":" << val << endl;
#define pdeb(val) printf("%s : %lld\n", #val, val);
typedef long long int ll;
typedef pair<ll, ll> P;
struct BIT {
ll n;
vector<ll> b;
BIT(ll idx) : n(idx+1), b(n, 0) {}
void init(vector<ll> a) {
for(ll i = 0; i < a.size(); i++) {
add(i+1, a[i]);
}
}
// sum: b[1] ~ b[i];
ll sum(ll i) {
ll ret = 0;
while(i > 0) {
ret += b[i];
i -= i & -i;
}
return ret;
}
// sum: b[x] ~ b[y];
ll range_sum(ll x, ll y) {
return sum(y) - sum(x-1);
}
void add(ll i, ll x) {
while(i <= n) {
b[i] += x;
i += i & -i;
}
}
};
int main(){
ll n;
cin >> n;
vector<ll> a(n);
rep(i, n) {
cin >> a[i];
}
BIT b(n);
ll total = 0;
rep(i, n) {
total += a[i] - b.sum(a[i]+1);
b.add(a[i]+1, 1);
}
rep(i, n) {
cout << total << endl;
total = (total - a[i]) + (n - 1 - a[i]);
}
return 0;
}
|
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 998244353
#define maxn 1000007
int main()
{
ios::sync_with_stdio(0);
ll a,b,c;
cin>>a>>b>>c;
cout<<a*(a+1)/2%mod*(b*(b+1)/2%mod)%mod*(c*(c+1)/2%mod)%mod<<endl;
return 0;
}
| #include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
#define N 200005
#define NN 1005000
#define PB push_back
#define M ll(1e9 + 7)
#define all(x) x.begin(), x.end()
#define sz(x) int(x.size())
#define pri(x) cout << x << endl
#define endl '\n'
#define _ << " " <<
#define F first
#define S second
using namespace std;
//using namespace __gnu_pbds;
//typedef tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update> oredered_set;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef short int si;
int main()
{
ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// freopen("1.in", "r", stdin);
int a, b, c;
cin >> a >> b >> c;
if (a * a + b * b < c * c)
{
pri("Yes");
}else pri("No");
}
|
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <utility>
#include <algorithm>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <unordered_map>
#include <map>
#include <unordered_set>
#include <set>
#include <tuple>
#include <cassert>
#include <cmath>
using namespace std;
using ll = long long;
const int mod = 1e9 + 7;
#define read_int(X) \
ll X; \
scanf("%lld\n", &X);
#define read_two_int(X, Y) \
ll X, Y; \
scanf("%lld %lld\n", &X, &Y);
#define read_three_int(X, Y, Z) \
ll X, Y, Z; \
scanf("%lld %lld %lld\n", &X, &Y, &Z);
#define read_four_int(X, Y, Z, W) \
ll X, Y, Z, W; \
scanf("%lld %lld %lld %lld\n", &X, &Y, &Z, &W);
int main()
{
ll N;
scanf("%lld", &N);
vector<string> S = vector<string>(N);
for (ll i = 0; i < N; i++)
{
cin >> S[i];
}
vector<ll> ans = vector<ll>(N + 1);
ans[0] = 1;
ll pow2 = 2;
for (ll i = 0; i < N; i++)
{
ans[i + 1] = ans[i];
if (S[i] == "OR")
ans[i + 1] += pow2;
pow2 *= 2;
}
printf("%lld\n", ans[N]);
}
| #include<iostream>
#include<vector>
#include<bits/stdc++.h>
// #define mod 1000000007
#define pi 3.14159265358979
#define PRE(x,p) cout<<setprecision(x)<<p;
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define endl "\n"
#define high INT_MAX
#define low INT_MIN
#define ll long long int
using namespace std;
void showa(ll a[],ll n){ for(ll i=1;i<=n;i++) cout<<a[i]<<' '; cout<<endl; }
void showv(vector<ll>v){ for(ll i=0;i<v.size();i++) cout<<v[i]<<' '; cout<<endl;}
long long int xpoweryMOD(int x, int y, int mod)
{
long long int res = 1;
/*
it depends sometimes on questions too,
whether pow(0,0) is 1 or 0
So change accordingly
*/
if (x == 0)return 0;
while (y)
{
if (y & 1)(res *= x) %= mod;
y >>= 1;
(x *= x) % mod;
}
return res;
}
int n;
ll po[65];
ll rec(vector<string>st,int index)
{
if(!index)return 1;
if(st[index]=="AND")
return rec(st,index-1);
else
return po[index] + rec(st,index-1);
}
void solve()
{
ll i;
cin>>n;
vector<string>st(n+1);
for(i=1;i<=n;i++)
cin>>st[i];
cout<<rec(st,n);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll x=2;
for(ll i=1;i<=61;i++)
{ po[i]=x;x=x*2;}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
ll t=1;
// cin>>t;
for(ll test=1;test<=t;test++)
{
// cout<<"CASE #"<<test<<": ";
solve();
}
return 0;
} |
#include <bits/stdc++.h>
#define int long long
#define mod 1000000007
#define i_max INT_MAX
#define i_min INT_MIN
#define s_i set<int>
#define v_i vector<int>
#define v_s vector<string>
#define v_c vector<char>
#define stk_i stack<int>
#define q_i queue<int>
#define qp_ii queue<pair<int, int>>
#define pqp_ii priority_queue<pair<int, int>>
#define vp_ii vector<pair<int, int>>
#define um_ii unordered_map<int, int>
#define m_ii map<int, int>
#define m_iv_i map<int, vector<int>>
#define mp make_pair
#define pb push_back
#define nline "\n"
#define yes cout << "YES" \
<< "\n"
#define no cout << "NO" \
<< "\n"
#define for_0(n) for (int i = 0; i < n; i++)
#define for_1(n) for (int i = 1; i <= n; i++)
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
using namespace std;
float solve()
{
float a, b;
cin >> a >> b;
float d = a - b;
float per_d = ((d * 1.0) / a) * 100;
return per_d;
}
int32_t main()
{
fast;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int t;
//cin >> t;
t = 1;
while (t--)
{
cout << solve() << nline;
}
return 0;
} | #include <iostream>
using namespace std;
int main (){
double a,b;
cin >> a>>b;
cout << 100-b*100/a << endl;
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 << 61;
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[201][12];
void cmemoInit(){
rep(i, 201){
rep(j, 12){
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;
}
//-----------------------ここから-----------
int main(void){
ll a, b, c, d;
cin >> a >> b >> c >> d;
cout << b - c << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define all(x) (x).begin(),(x).end()
using ll = long long;
using P = pair<ll, ll>;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, -1, 0, 1};
string char_to_string(char val) {
return string(1, val);
}
int char_to_int(char val) {
return val - '0';
}
char inverse_char(char c) {
if(isupper(c)) return tolower(c);
else return toupper(c);
}
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;
}
struct edge {
ll to, cost;
};
int main() {
ll T; cin >> T;
REP(i, T) {
ll N; cin >> N;
if(N % 4 == 0) cout << "Even" << endl;
else if(N % 2 == 0) cout << "Same" << endl;
else cout << "Odd" << endl;
}
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
void rec(int *idx, int s, int baki, function<void(int *)> fun)
{
if (baki == 0) fun(idx);
else
{
if (s < 0) return;
rec(idx, s - 1, baki, fun);
idx[baki - 1] = s;
rec(idx, s - 1, baki - 1, fun);
}
}
void cmb(int n, int k, function<void(int *)> fun)
{
int idx[k];
rec(idx, n - 1, k, fun);
}
ll ok;
vector<ll> a;
vector<vector<vector<ll>>> vec;
int main()
{
fast_io;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll n;
cin >> n;
a = vector<ll>(n);
vec = vector<vector<vector<ll>>>(200);
for (int i = 0; i < n; i++)
{
cin >> a[i];
a[i] %= 200;
}
for (int i = 1; i <= n; i++)
{
ok = i;
cmb(n, i, [](int *ind)
{
vector<ll> t;
ll ts = 0;
for (int j = 0; j < ok; j++)
{
t.push_back(ind[j] + 1);
ts += a[ind[j]];
}
if (vec[ts % 200].size() <= 0) vec[ts % 200].push_back(t);
else
{
cout << "YES" << endl;
cout << vec[ts % 200][0].size();
for (auto pl : vec[ts % 200][0]) cout << " " << pl;
cout << endl;
cout << t.size();
for (auto pl : t) cout << " " << pl;
cout << endl;
exit(0);
}
});
}
cout << "NO" << endl;
return 0;
} | /**
* In The Name Of God
* author : Behradm
* Idvwhu wkdq idvw, Txlfnhu wkdq txlfn, L dp Oljkwqlqj, Vshhg, L dp Vshhg :D :D
**/
#include "bits/stdc++.h"
using namespace std;
const int N = 300;
int a[N];
vector<int> cnt[N];
int n;
vector<int> Recover(int mask) {
vector<int> res;
for (int i = 0; i < n; i++)
if (mask & (1 << i))
res.push_back(i + 1);
return res;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i], a[i] %= 200;
int ans = -1;
int end = 257;
if (n <= 8)
end = (1 << n);
for (int mask = 1; mask < end; mask++) {
int sum = 0;
for (int i = 0; i < n; i++)
if (mask & (1 << i)) { sum += a[i], sum %= 200; };
cnt[sum].push_back(mask);
int sz = (int) cnt[sum].size();
if (sz == 2) {
ans = sum;
break;
}
}
if (ans == -1) {
cout << "No\n";
return 0;
}
vector<int> x = Recover(cnt[ans][0]);
vector<int> y = Recover(cnt[ans][1]);
cout << "Yes\n";
cout << (int) x.size() << " ";
for (int u : x)
cout << u << " ";
cout << '\n';
cout << (int) y.size() << " ";
for (int u : y)
cout << u << " ";
cout << '\n';
return 0;
}
|
/* ** *** In the name of God *** ** */
// Only Haider is Amir al-Momenin
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
#define ij for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++)
const ll maxn = 810;
int a[maxn][maxn], b[maxn][maxn];
vector<int> v;
int n, k;
bool check(int x) {
ij if (a[i][j] >= x) b[i][j] = 1; else b[i][j] = -1;
ij b[i][j] += b[i][j-1];
ij b[i][j] += b[i-1][j];
for (int i = k; i <= n; i++) {
for (int j = k; j <= n; j++) {
int s = b[i][j] - b[i][j - k] - b[i-k][j] + b[i-k][j-k];
if (s <= 0) return 1;
}
}
return 0;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> k;
ij cin >> a[i][j], v.push_back(a[i][j]);
sort(v.begin(), v.end());
v.resize(unique(v.begin(), v.end()) - v.begin());
// ij cout << a[i][j] << '\n';
int id = k * k - (k * k / 2) + 2;
int l = 0, r = v.size();
while (r > l + 1) {
int mid = (l + r) / 2;
int V = v[mid];
// cout << V << " ";
if (check(V)) r = mid;
else l = mid;
// cout << endl;
}
cout << v[l];
return 0;
}
/*
_ _ _ _ _ _ _ _ _ _ _ _
/ / \ / / | / / |
/``````\ \ |`````| | |`````| |
/ \ \ | | | | | |
/ \ \ | | | | | |
/ /\ \ \ | | | | | |
/ / /\ \ \ | | | | | |
/ / / \ \ \ | | | | | |
/ / /_ _ \ \ \ | | | | | |
/ /_ _ _ _ \ \ \ | | | | | |
/ \ \ | | |_ _ _ _ | | |
/ _ _ _ _ _ _ _ \ \ | | / /| | | |
/ / / \ \ \ | |/_ _ _ _ / | | | |
/ / / \ \ / | | / | | /
/_ _ _/_/ \_ _ _\/ |_ _ _ _ _ _ _ _|/ |_ _ _|/
*/ | #include<iostream>
#include<climits>
#include<bits/stdc++.h>
using namespace std;
#define LL long long int
#define mod 998244353
int main()
{
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
LL N ,K;
cin >> N >> K;
vector< vector<LL> > arr(N+1,vector<LL>(N+1));
for(LL i=0;i<N;i++){
for(LL j=0;j<N;j++){
cin >> arr[i][j];
}
}
function<bool(LL) > checkMedianIsPossibleOrNot = [&](LL median){
// SUBMATRIX SUM QUERIES BRO
vector< vector<LL> > temp(N+1,vector<LL>(N+1,0));
for(LL i=0;i<N;i++){
for(LL j=0;j<N;j++){
if(arr[i][j] <= median){
temp[i][j] = 1 ;
}
}
}
vector< vector<LL> > dp(N+1,vector<LL>(N+1,0));
for(LL i=1;i<=N;i++){
for(LL j=1;j<=N;j++){
dp[i][j]= temp[i-1][j-1] + dp[i-1][j]+ dp[i][j-1] - dp[i-1][j-1];
}
}
for(LL i=1;i<=N-K+1;i++){
for(LL j=1;j<=N-K+1;j++){
LL x1=i;LL y1=j;
LL x2=i+K-1;
LL y2=j+K-1;
LL suminSubmatrix = dp[x2][y2] - dp[x1-1][y2] - dp[x2][y1-1] + dp[x1-1][y1-1];
if(suminSubmatrix >= (K*K+1)/2){
return true;
}
}
}
return false;
};
LL start = -1;
LL end = (LL)1e9;
LL ans = -1;
while(start <= end){
LL mid = (start+end)/2;
if(checkMedianIsPossibleOrNot(mid)){
end=mid-1;
ans=mid;
}
else{
start=mid+1;
}
}
cout<<ans;
}
|
#include<deque>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<tuple>
#include<string>
#include<chrono>
#include<functional>
#include<iterator>
#include<random>
#include<unordered_set>
#include<array>
#include<map>
#include<iomanip>
#include<assert.h>
#include<list>
#include<bitset>
#include<stack>
#include<memory>
#include<numeric>
using namespace std;
using namespace std::chrono;
typedef long long int llint;
typedef long double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
#define REP(i, n) for(int i = 0;i < (n);i++)
/*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/
const llint mod=1000000007;
const llint inf=2.19e15+1;
const long double pai=3.141592653589793238462643383279502884197;
const long double eps=1e-10;
template <class T,class U>bool chmin(T& a,U b){if(a>b){a=b;return true;}return false;}
template <class T,class U>bool chmax(T& a,U b){if(a<b){a=b;return true;}return false;}
llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);}
llint lcm(llint a,llint b){if(a==0){return b;}return a/gcd(a,b)*b;}
template<class T> void SO(T& ve){sort(ve.begin(),ve.end());}
template<class T> void REV(T& ve){reverse(ve.begin(),ve.end());}
template<class T>llint LBI(const vector<T>&ar,T in){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();}
template<class T>llint UBI(const vector<T>&ar,T in){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();}
int main(void){
cout<<fixed<<setprecision(20);
cin.tie(0);ios::sync_with_stdio(false);
/*
1 1
1 2
3 2
3 5
*/
llint N,i;cin>>N;
llint x=1,y=0;//倍率
vector<llint>bai;
while(1){
y+=x;bai.pub(y);if(y>N){break;}
x+=y;bai.pub(x);if(x>N){break;}
}
REV(bai);
vector<int>sou;
for(i=0;i<bai.size();i++){
if((i+bai.size())%2==0){
//xを増やして、 y+=x
while(N>=bai[i]){N-=bai[i];sou.pub(1);}
sou.pub(4);
}else{
while(N>=bai[i]){N-=bai[i];sou.pub(2);}
sou.pub(3);
}
}
while(3<=sou[0]){sou.erase(sou.begin());}
cout<<sou.size()<<endl;
for(auto it:sou){cout<<it<<endl;}
return 0;
}
| #include <bits/stdc++.h>
#include <chrono>
using namespace std;
using namespace chrono;
typedef long long int ll;
typedef vector<int> vii;
typedef vector<ll> vll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define pb push_back
#define odd(x) ((x)&1)
#define even(x) (!odd(x))
#define all(v) (v).begin(),(v).end()
#define rep(i,n) for(auto i=0;i<n;++i)
#define FASTIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define TEST_CASE int tc;cin>>tc;while(tc--)
#define Clock high_resolution_clock::now()
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;}
#ifdef LOCAL
#define cerr cout
#else
#endif
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
// #define TIME
/******************************************************************************************************************************/
const ll inf = 2e18;
const ll mod = 998244353;
const ll N = 1e5+3;
int main()
{
auto start_time = Clock;
FASTIO
ll n,mxx=0;
cin >> n;
vll v(n), mx(n);
rep(i,n) {
cin >> v[i];
chmax(mxx, v[i]);
mx[i] = mxx;
}
ll sum = 0;
rep(i,n) {
v[i] += (i?v[i-1]:0);
sum += v[i];
cout << (sum+(i+1)*mx[i]) << "\n";
}
auto end_time = Clock;
#ifndef TIME
return 0;
#endif
cout << "\nTime elapsed: "
<< (double)duration_cast<milliseconds>(end_time-start_time).count()
<< "ms";
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
int main(){
int N; cin >> N;
vector<double> a(N),b(N),c(N),d(N);
for(int _=0; _<2; _++){
for(int i=0; i<N; i++) cin >> a[i] >> b[i];
int x = 0, y = 0;
for(int i=0; i<N; i++){
x += a[i];
y += b[i];
a[i] *= N;
b[i] *= N;
}
for(int i=0; i<N; i++){
a[i] -= x;
b[i] -= y;
}
swap(a,c);
swap(b,d);
}
for(int i=0; i<N; i++){
if(a[i]!=0 || b[i]!=0){
swap(a[i],a[0]);
swap(b[i],b[0]);
}
}
string ans = "No";
const double eps = 1e-6;
for(int i=0; i<N; i++){
double angle = atan2(d[i],c[i])-atan2(b[0],a[0]);
bool flag = true;
for(int j=0; j<N; j++){
double A = a[j]*cos(angle)-b[j]*sin(angle);
double B = a[j]*sin(angle)+b[j]*cos(angle);
bool flag2 = false;
for(int k=0; k<N; k++){
if(std::abs(A-c[k])<=eps && std::abs(B-d[k])<=eps) flag2 = true;
}
flag &= flag2;
}
if(flag) ans = "Yes";
}
cout << ans << endl;
}
| #include<bits/stdc++.h>
#define ll long long
#define ld long double
#define f first
#define s second
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
const int mod=1e9+7;
const int maxV=2e7+1;
const ll inf=1e18+1;
int main(){
int n;
cin>>n;
map<int, ll> mp1, mp2;
int c;
ll x;
for(int i=0;i<n;i++){
cin>>x>>c;
if(mp1.find(c)==mp1.end()){
mp1[c]=x;
}
if(mp2.find(c)==mp2.end()){
mp2[c]=x;
}
mp1[c]=min(mp1[c], x);
mp2[c]=max(mp2[c], x);
}
n=mp1.size();
vector<pair<ll,ll>> pts(n);
c=0;
for(auto it:mp1){
pts[c].f=it.s;
c++;
}
c=0;
for(auto it:mp2){
pts[c].s=it.s;
c++;
}
vector<vector<ll>> dp(n, vector<ll> (2));
for(int i=0;i<n;i++){
if(i==0){
dp[i][0]=abs(pts[i].f-pts[i].s)+abs(pts[i].s);
dp[i][1]=abs(pts[i].f-pts[i].s)+abs(pts[i].f);
}
else{
dp[i][0]=abs(pts[i].f-pts[i].s)+min(dp[i-1][0]+abs(pts[i-1].f-pts[i].s), dp[i-1][1]+abs(pts[i-1].s-pts[i].s));
dp[i][1]=abs(pts[i].f-pts[i].s)+min(dp[i-1][0]+abs(pts[i-1].f-pts[i].f), dp[i-1][1]+abs(pts[i-1].s-pts[i].f));
}
}
cout<<min(dp[n-1][0]+abs(pts[n-1].f), dp[n-1][1]+abs(pts[n-1].s))<<"\n";
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
#define x first
#define y second
#define bg begin()
#define ed end()
#define pb push_back
#define mp make_pair
#define sz(a) int((a).size())
#define R(i,n) for(int i(0);i<(n);++i)
#define L(i,n) for(int i((n)-1);~i;--i)
const int iinf=0x3f3f3f3f;
const ll linf=0x3f3f3f3f3f3f3f3f;
//Data
typedef long double ldb;
const ldb eps=1e-15;
ldb s,p;
//Functions
bool isi(ldb x){
ll y=floor(x);
for(ll t=y-5;t<=y+5;t++)
if(abs((ldb)t*(s-t)-p)<eps&&t>0&&s-t>0)
return true;
return false;
}
bool check(){
if((ldb)s*s-p*4<=-eps) return false;
if(isi(.5*(s+sqrt((ldb)s*s-p*4)))) return true;
if(isi(.5*(s-sqrt((ldb)s*s-p*4)))) return true;
return false;
}
//Main
int main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>s>>p;
if(check()) cout<<"Yes\n";
else cout<<"No\n";
return 0;
} | #include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, a, b) for (int i = (int)(a); i >= (int)b; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define per(i, r, l) for (int i = (r); i >= (l); i--)
#define ms(x, y) memset(x, y, sizeof(x))
#define SZ(x) int(x.size())
#define ok cerr<<"ok"<<endl
#define db(x) cerr<<(#x)<<'='<<(x)<<endl
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;
template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }
//1.integer overflow (1e5 * 1e5) (2e9 + 2e9)
//2.runtime error
//3.boundary condition
int a,b,c,d;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.precision(10);
cout << fixed;
#ifdef LOCAL_DEFINE
freopen("input.txt", "r", stdin);
#endif
cin>>a>>b>>c>>d;
cout<<a*d-b*c<<endl;
#ifdef LOCAL_DEFINE
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
int sled0[500005];
int sled1[500005];
int mvs[500005];
int main(){
ios_base::sync_with_stdio(false), cin.tie(0);
cout.precision(10);
cout << fixed;
int n;
cin >> n;
string s, t;
cin >> s >> t;
string aa = s, bb = t;
sort(aa.begin(), aa.end());
sort(bb.begin(), bb.end());
if(aa != bb){
cout << -1;
return 0;
}
vector <int> p;
vector <int> d;
for(int i=0; i<n; i++){
if(s[i] == '0') p.push_back(i);
if(t[i] == '0') d.push_back(i);
}
int res = 0;
for(int i=0; i<p.size(); i++){
if(p[i] != d[i]) res++;
}
cout << res;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false); cin.tie(0);
#define FOR(i,s,n) for(int i = (s); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) for(int i = (n); i >= 0; i--)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
#define ATYN(n) cout << ( (n) ? "Yes":"No") << '\n';
#define CFYN(n) cout << ( (n) ? "YES":"NO") << '\n';
#define OUT(n) cout << (n) << '\n';
using ll = long long;
using ull = unsigned long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
int main(void)
{
auto ansout = [](int a,int b,int c,int d){
cout << a << " " << b << " " << c << " " << d << '\n';};
IOS
int n; cin >> n;
vector<int> x(n), y(n), r(n);
REP(i,n) {
cin >> x[i] >> y[i] >> r[i];
}
REP(i,n) ansout(x[i],y[i],x[i]+1,y[i]+1);
return 0;
} |
#include<bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (int) (n); i++)
using namespace std;
#define INF 100000000
#define MAX_N 18
int N, ds0[MAX_N], ds1[MAX_N], d[MAX_N][MAX_N], dp[1<<MAX_N][MAX_N];
int rec(int bit, int v){
if(dp[bit][v] < INF) return dp[bit][v];
if(bit == (1<<v)) return dp[bit][v] = ds1[v];
int prev_bit = bit & ~(1<<v), res = INF;
rep(u, N){
if(!(prev_bit & (1<<u))) continue;
res = min(res, rec(prev_bit, u) + d[u][v]);
}
return dp[bit][v] = res;
}
int main(){
cin >> N;
int X[N], Y[N], Z[N];
rep(i, N) cin >> X[i] >> Y[i] >> Z[i];
rep(i, (1<<N)) rep(j, N) dp[i][j] = INF;
rep(i, N) ds1[i] = abs(X[i + 1] - X[0]) + abs(Y[i + 1] - Y[0]) + max(0, Z[i + 1] - Z[0]);
rep(i, N) ds0[i] = abs(X[i + 1] - X[0]) + abs(Y[i + 1] - Y[0]) + max(0, Z[0] - Z[i + 1]);
rep(i, N) rep(j, N) d[i][j] = abs(X[i + 1] - X[j + 1]) + abs(Y[i + 1] - Y[j + 1]) + max(0, Z[j + 1] - Z[i + 1]);
int ans = INF;
rep(i, N - 1) ans = min(ans, rec((1<<(N - 1)) - 1, i) + ds0[i]);
cout << ans << endl;
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 = 998244353;
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; }
long long pow(long long x, long long n, long long MM) {
long long ret = 1;
while (n > 0) {
if (n & 1) ret = ret * x % MM; // n の最下位bitが 1 ならば x^(2^i) をかける
x = x * x % MM;
n >>= 1; // n を1bit 左にずらす
}
return ret;
}
int main(){
ll N;
cin>>N;
vector <ll> a(N);
rep(i,N){
cin>>a[i];
}
sort(all(a));
ll temp = 0;
rep(i,N){
if(i == 0){
temp += a[i];
temp%=MOD;
}
else{
temp += a[i]*pow(2,i-1,MOD);
temp%=MOD;
}
}
ll waru = pow(2,MOD-2,MOD);
ll ans = 0;
rep(i,N){
if(i != (N-1)){
ans += a[i]*temp%MOD;
ans%=MOD;
if(temp<(a[i]+a[i+1])){
temp+=MOD;
}
temp -= a[i];
temp -= a[i+1];
temp *= waru%MOD;
temp%=MOD;
temp += a[i+1];
temp%=MOD;
//cout << ans << endl;
//cout << temp << endl;
}
else{
ans += a[N-1]*a[N-1]%MOD;
ans%=MOD;
}
}
cout << ans%MOD << endl;
return 0;
}
|
#include<bits/stdc++.h>
#define ll long long
#define reg register
#define mp make_pair
#define ri register int
#define ld long double
using namespace std;
const int mxn=2e5+5;
vector<int>g[mxn];
int n,m;
int a[mxn];
int dp[111][5555][111];//i sum cnt
const ll md=998244353;
ll frac[mxn];
inline void solve(){
cin>>n;
for(int i=1;i<=n;++i)cin>>a[i],m=m+a[i];
if(m%2==1){
cout<<0<<'\n';
return;
}
m=m/2;
frac[0]=1;
for(int i=1;i<=n;++i)frac[i]=(frac[i-1]*i)%md;
dp[1][0][0]=1;
for(int i=1;i<=n;++i){
for(int j=0;j<=m;++j){
for(int k=0;k<=i;++k){
if(dp[i][j][k])(dp[i+1][j+a[i]][k+1]+=dp[i][j][k])%=md;
(dp[i+1][j][k]+=dp[i][j][k])%=md;
}
}
}
ll ans=0;
for(int k=1;k<n;++k){
if(dp[n+1][m][k]){
ans=ans+dp[n+1][m][k]%md*frac[k]%md*frac[n-k]%md;
ans%=md;
}
}
cout<<ans<<'\n';
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int T=1;//cin>>T;
for(;T--;)solve();
} | #include<bits/stdc++.h>
using namespace std;
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> p2;
#define sz(a) ll(a.size())
char s[5005][5005];
ll dp[5005][5005][3],r[5005][5005],c[5005][5005],p[100005],hell=998244353;
map<char,ll> st;
void solve(){
ll n,m,k;
cin>>n>>m>>k;
for(ll i=1;i<=n;i++)
for(ll j=1;j<=m;j++)
s[i][j]='0';
for(ll i=0;i<k;i++){
ll x,y;
char c;
cin>>x>>y>>c;
s[x][y]=c;
}
for(ll i=1;i<=n;i++)
for(ll j=1;j<=m;j++)
if(s[i][j]=='0')
r[i][j]=c[i][j]=1;
for(ll i=1;i<=n;i++)
for(ll j=1;j<=m;j++)
r[i][j]+=r[i][j-1];
for(ll j=1;j<=m;j++)
for(ll i=1;i<=n;i++)
c[i][j]+=c[i-1][j];
st['R']=0;st['D']=1,st['X']=2;
if(s[1][1]=='0')
for(ll k=0;k<3;k++)
dp[1][1][k]=1;
else
dp[1][1][st[s[1][1]]]=1;
for(ll i=1;i<=n;i++){
for(ll j=1;j<=m;j++){
if(i==1&&j==1)
continue;
ll val=0,sum=0;
sum=(dp[i-1][j][1]+dp[i-1][j][2])%hell;
sum=(sum*(p[r[i-1][m]-r[i-1][j]]))%hell;
val=(val+sum)%hell;
sum=(dp[i][j-1][0]+dp[i][j-1][2])%hell;
sum=(sum*(p[c[n][j-1]-c[i][j-1]]))%hell;
val=(val+sum)%hell;
if(s[i][j]=='0')
for(ll k=0;k<3;k++)
dp[i][j][k]=val;
else
dp[i][j][st[s[i][j]]]=val;
/*for(ll k=0;k<3;k++)
cout<<i<<" "<<j<<" "<<k<<" "<<dp[i][j][k]<<endl;*/
}
}
ll res=0;
for(ll k=0;k<3;k++)
res=(res+dp[n][m][k])%hell;
cout<<res;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r" , stdin);
freopen("output.txt", "w", stdout);
#endif
clock_t z=clock();
ll qc=1;
//cin>>qc;
p[0]=1;
for(ll i=1;i<100005;i++)
p[i]=(3*p[i-1])%hell;
for(ll i=1;i<=qc;i++){
//cout<<"Case #"<<i<<": ";
solve();
}
debug("Total Time:%.4Lf\n",(ld)(clock()-z)/CLOCKS_PER_SEC);
} |
#include <cstdio>
#define ll long long
using namespace std;
int n,m;ll p[5010],g[5010][5010],ans;
const ll mod=998244353;
int main()
{
scanf("%d %d",&n,&m);
p[0]=1;
for(int i=1;i<=n;i++)
p[i]=p[i-1]*m%mod;
for(int i=0;i<=m;i++)
{
g[i][0]=1;
for(int j=1;j<=n;j++)
g[i][j]=g[i][j-1]*i%mod;
}
ans=p[n]*n%mod;
for(int x=1;x<=m;x++)
for(int j=1;j<n;j++)
ans=(ans-(n-j)*g[m-x][j-1]%mod*p[n-j-1]%mod+mod)%mod;
printf("%lld",ans);
} | #include <bits/stdc++.h>
#define int long long
using namespace std;
// https://github.com/nweeks1/UltiSnips
template <const int32_t MOD> struct ModInt {
long long x;
ModInt() : x(0) {}
ModInt(long long u) : x(u) {
x %= MOD;
if (x < 0)
x += MOD;
}
friend bool operator==(const ModInt &a, const ModInt &b) {
return a.x == b.x;
}
friend bool operator!=(const ModInt &a, const ModInt &b) {
return a.x != b.x;
}
friend bool operator<(const ModInt &a, const ModInt &b) { return a.x < b.x; }
friend bool operator>(const ModInt &a, const ModInt &b) { return a.x > b.x; }
friend bool operator<=(const ModInt &a, const ModInt &b) {
return a.x <= b.x;
}
friend bool operator>=(const ModInt &a, const ModInt &b) {
return a.x >= b.x;
}
static ModInt sign(long long k) {
return ((k & 1) ? ModInt(MOD - 1) : ModInt(1));
}
ModInt &operator+=(const ModInt &m) {
x += m.x;
if (x >= MOD)
x -= MOD;
return *this;
}
ModInt &operator-=(const ModInt &m) {
x -= m.x;
if (x < 0LL)
x += MOD;
return *this;
}
ModInt &operator*=(const ModInt &m) {
x = (1LL * x * m.x) % MOD;
return *this;
}
friend ModInt operator-(const ModInt &a) {
ModInt res(a);
if (res.x)
res.x = MOD - res.x;
return res;
}
friend ModInt operator+(const ModInt &a, const ModInt &b) {
return ModInt(a) += ModInt(b);
}
friend ModInt operator-(const ModInt &a, const ModInt &b) {
return ModInt(a) -= ModInt(b);
}
friend ModInt operator*(const ModInt &a, const ModInt &b) {
return ModInt(a) *= ModInt(b);
}
static long long fp(long long u, long long k) {
long long res = 1LL;
while (k > 0LL) {
if (k & 1LL)
res = (res * u) % MOD;
u = (u * u) % MOD;
k /= 2LL;
}
return res;
}
static constexpr int mod() { return MOD; }
ModInt fastpow(long long k) { return ModInt(fp(x, k)); }
ModInt inv() {
assert(x);
return ModInt(fp(x, MOD - 2));
}
ModInt &operator/=(const ModInt &m) { return *this *= ModInt(m).inv(); }
friend ModInt operator/(const ModInt &a, const ModInt &b) {
return ModInt(a) *= ModInt(b).inv();
}
friend ostream &operator<<(ostream &out, const ModInt &a) {
return out << a.x;
}
friend istream &operator>>(istream &in, ModInt &a) { return in >> a.x; }
};
// const int MOD = 1e9 + 7;
const int MOD = 998244353;
using Mint = ModInt<MOD>;
signed main(void) {
ios_base::sync_with_stdio(false);
cin.tie(0);
int N, M;
cin >> N >> M;
vector<Mint> dp(N);
vector<Mint> A(N);
dp[0] = M;
for (int i(1); i < N; ++i) {
A[i] = A[i - 1] * M;
Mint x = Mint(M - 1).fastpow(i - 1);
for (int m(1); m <= M; ++m)
A[i] += x - Mint(M - m).fastpow(i - 1);
dp[i] = dp[i - 1] * M + M * Mint(M - 1).fastpow(i) + A[i];
}
cout << dp[N - 1] << endl;
}
|
#include <iostream>
#include <cassert>
#include <numeric>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <queue>
#include <map>
#include <cmath>
using namespace std;
int main() {
long long int n;
cin>>n;
long long int ans=0;
long long int s=0;
for (int i = 1; i <= sqrt(2*n); i++)
{
if ((2*n)%i==0)
{
if (i%2==1)
{
ans++;
}
else
{
if (((2*n)/i)%2==1)
{
ans++;
}
}
}
}
cout<<ans*2-s<<endl;
} | #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn = 210;
int n;
ll dp[maxn][maxn];
ll read(){ ll s = 0, f = 1; char ch = getchar(); while(ch < '0' || ch > '9'){ if(ch == '-') f = -1; ch = getchar(); } while(ch >= '0' && ch <= '9'){ s = s * 10 + ch - '0'; ch = getchar(); } return s * f; }
int main(){
n = read();
dp[0][0] = 1;
for(int i = 1 ; i < n ; ++i){
for(int j = 1 ; j <= min(i, 11) ; ++j){
for(int k = 0 ; k < i ; ++k){
dp[i][j] += dp[k][j - 1];
}
}
}
// for(int i = 1 ; i < n ; ++i){
// for(int j = 0 ; j <= min(i, 11) ; ++j){
// printf("%d ", dp[i][j]);
// } printf("\n");
// }
ll ans = 0;
for(int i = 1 ; i < n ; ++i) ans += dp[i][11];
printf("%lld\n", ans);
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
#define ALL(x) begin(x),end(x)
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define mod 1000000007
using ll=long long;
const int INF=1000000000;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}
struct IOSetup{
IOSetup(){
cin.tie(0);
ios::sync_with_stdio(0);
cout<<fixed<<setprecision(12);
}
} iosetup;
template<typename T>
ostream &operator<<(ostream &os,const vector<T>&v){
for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?"":" ");
return os;
}
template<typename T>
istream &operator>>(istream &is,vector<T>&v){
for(T &x:v)is>>x;
return is;
}
// round for +inf
template<typename INT>
inline INT div_ceil(INT x,INT y){
return (x<0?x/y:(x+y-1)/y);
}
// round for -inf
template<typename INT>
inline INT div_floor(INT x,INT y){
return (x>0?x/y:(x-y+1)/y);
}
signed main(){
ll N,AS,BS;cin>>N>>AS>>BS;
vector<ll> A(N),B(N);
cin>>A;
vector<ll> L(N),H(N);
auto check=[&](ll t){
ll LS=0,HS=0;
rep(i,N){
L[i]=div_ceil(BS*A[i]-t,AS);
H[i]=div_floor(BS*A[i]+t,AS);
LS+=L[i];
HS+=H[i];
}
return LS<=BS and BS<=HS;
};
ll lw=0,hi=LINF,res=LINF;
while(lw<=hi){
ll mid=(lw+hi)/2;
if(check(mid)) res=mid,hi=mid-1;
else lw=mid+1;
}
check(res);
auto ans=L;
for(auto &x:ans) BS-=x;
rep(i,N){
ll y=H[i]-L[i];
if(BS<=y) ans[i]+=BS,BS=0;
else BS-=y,ans[i]+=y;
}
cout<<ans<<endl;
return 0;
} | #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using ll = unsigned long long;
struct Town
{
ll val = 0ULL;
ll a = 0ULL;
ll b = 0ULL;
};
bool operator<( const Town& lhs, const Town& rhs )
{
return lhs.val < rhs.val;
}
vector<Town> in;
int main()
{
int n;
cin >> n;
ll sumA = 0ULL;
for(int i=0; i<n; i++) {
Town t;
cin >> t.a >> t.b;
t.val = t.a * 2 + t.b;
in.push_back(t);
sumA += t.a;
}
sort( in.rbegin(), in.rend() );
int res = 0;
ll sumB = 0ULL;
while( sumA >= sumB ) {
sumB += in[res].a + in[res].b;
sumA -= in[res].a;
res++;
}
cout << res << endl;
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll arr[300005];
ll suffix[300005];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin>>n;
for(int i=1; i<=n; i++) cin>>arr[i];
for(int i=n; i>=1; i--) suffix[i] = suffix[i+1]+arr[i];
ll ans = 0;
for(int i=1; i<=n; i++){
ll sq = arr[i]*arr[i];
ans+=sq*(i-1);
ans+=sq*(n-i);
ans-=2*arr[i]*suffix[i+1];
}
cout<<ans;
} | #include <bits/stdc++.h>
//#include <chrono>
//#pragma GCC optimize("Ofast")
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
ll N,M,H,W,Q,K,A,B;
string S;
using P = pair<ll, ll>;
const ll INF = (1LL<<60);
template<class T> bool chmin(T &a, const T b){
if(a > b) {a = b; return true;}
else return false;
}
template<class T> bool chmax(T &a, const T b){
if(a < b) {a = b; return true;}
else return false;
}
template<class T> void my_printv(std::vector<T> v,bool endline = true){
if(!v.empty()){
for(std::size_t i{}; i<v.size()-1; ++i) std::cout<<v[i]<<" ";
std::cout<<v.back();
}
if(endline) std::cout<<std::endl;
}
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
vec num(401, 0);
cin>>N;
rep(i, N){
cin>>A;
++num[A + 200];
}
ll res{};
rep(i, 401){
rep(j, i){
res += num[i] * num[j] * (j - i) * (j - i);
}
}
cout<<res<<endl;
} |
#include<iostream>
#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<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;
//#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 1000000007;
const ll INF = mod * mod;
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 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++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acosl(-1.0);
ll mod_pow(ll x, ll n, ll m = mod) {
ll res = 1;
while (n) {
if (n & 1)res = res * x % m;
x = x * x % m; n >>= 1;
}
return res;
}
struct modint {
ll n;
modint() :n(0) { ; }
modint(ll m) :n(m) {
if (n >= mod)n %= mod;
else if (n < 0)n = (n % mod + mod) % mod;
}
operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
if (n == 0)return modint(1);
modint res = (a * a) ^ (n / 2);
if (n % 2)res = res * a;
return res;
}
ll inv(ll a, ll p) {
return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
const int max_n = 1 << 2;
modint fact[max_n], factinv[max_n];
void init_f() {
fact[0] = modint(1);
for (int i = 0; i < max_n - 1; i++) {
fact[i + 1] = fact[i] * modint(i + 1);
}
factinv[max_n - 1] = modint(1) / fact[max_n - 1];
for (int i = max_n - 2; i >= 0; i--) {
factinv[i] = factinv[i + 1] * modint(i + 1);
}
}
modint comb(int a, int b) {
if (a < 0 || b < 0 || a < b)return 0;
return fact[a] * factinv[b] * factinv[a - b];
}
void solve() {
ll n, a, b; cin >> n >> a >> b;
if (a + b > n) {
cout << 0 << "\n"; return;
}
if (a < b)swap(a, b);
modint al = 1;
al *= (n - a + 1) * (n - a + 1);
al *= (n - b + 1) * (n - b + 1);
auto calc = [&](ll x)->ll {
return x * (x + 1) / 2;
};
modint c = (a) * (n+1 - a - b);
c += calc(a) - calc(a - b);
c += calc(b-1);
c += (n + 1 - a - b) * (b - 1);
modint ans = al - c*c;
cout << ans << "\n";
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
//cout << fixed << setprecision(15);
//init_f();
//init();
//expr();
int t; cin >> t; rep(i, t)
solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, a, b) for(ll i = a; i < b; i++)
#define Rep(i, a, b) for(ll i = a; i <= b; i++)
#define repr(i, a, b) for(ll i = b-1; i >= a; i--)
// #define _GLIBCXX_DEBUG
template <class T> using V = vector<T>;
#define ALL(v) (v).begin(),(v).end()
#define endl '\n'
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define sz(v) ((ll)(v).size())
const double pi = acos(-1.0);
// const ll MOD = 1000000007LL;
const ll MOD = 998244353LL;
const ll INF = 1LL << 60;
const int dy[] = {1, 0, -1, 0};
const int dx[] = {0, 1, 0, -1};
const int dy2[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int dx2[] = {1, 1, 0, -1, -1, -1, 0, 1};
// ios::sync_with_stdio(false);
// cin.tie(nullptr);
/*--------------------------------------------------------------------------------
--------------------------------------------------------------------------------*/
template<int MOD> struct Fp {
long long val;
constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
if (val < 0) val += MOD;
}
constexpr int getmod() { return MOD; }
constexpr Fp operator - () const noexcept {
return val ? MOD - val : 0;
}
constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
constexpr Fp& operator += (const Fp& r) noexcept {
val += r.val;
if (val >= MOD) val -= MOD;
return *this;
}
constexpr Fp& operator -= (const Fp& r) noexcept {
val -= r.val;
if (val < 0) val += MOD;
return *this;
}
constexpr Fp& operator *= (const Fp& r) noexcept {
val = val * r.val % MOD;
return *this;
}
constexpr Fp& operator /= (const Fp& r) noexcept {
long long a = r.val, b = MOD, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b; swap(a, b);
u -= t * v; swap(u, v);
}
val = val * u % MOD;
if (val < 0) val += MOD;
return *this;
}
constexpr bool operator == (const Fp& r) const noexcept {
return this->val == r.val;
}
constexpr bool operator != (const Fp& r) const noexcept {
return this->val != r.val;
}
friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept {
return os << x.val;
}
friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
if (n == 0) return 1;
auto t = modpow(a, n / 2);
t = t * t;
if (n & 1) t = t * a;
return t;
}
};
using mint = Fp<MOD>;
mint dp[6005][6005];
int main(){
ll n, k;
cin >> n >> k;
dp[1][1] = 1;
rep(i, 1, n+1){
repr(j, 1, n+1){
if(i < j) continue;
dp[i][j] += dp[i-1][j-1] + dp[i][j*2];
}
}
cout << dp[n][k] << endl;
return 0;
} |
//#pragma GCC target("avx2")
//#pragma GCC optimize("O3")
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <tuple>
#include <math.h>
#include <set>
#include <stack>
#include <bitset>
#include <map>
#include <cassert>
#include <queue>
#include <random>
#include <unordered_set>
#include <unordered_map>
#define pqueue priority_queue
#define pb(x) push_back(x)
#define endl '\n'
#define all(x) x.begin(), x.end()
#define int long long
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
// typedef tuple<ll, ll, ll> tiii;
typedef pair<int, int> pii;
typedef vector<pair<int, int> > vpii;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<char> vc;
const int inf = 1e9 + 228;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ld eps = 1e-5;
void fast_io(){
ios_base::sync_with_stdio(0);
cin.tie(0);
// freopen("grass.in", "r", stdin);
// freopen("grass.out", "w", stdout);
}
bool cmp(pii a, pii b){
return a.first + a.second * 2 >= b.first + b.second * 2;
}
void solve(){
int n;
cin >> n;
vpii kek(n);
for(pii &i:kek)
cin >> i.second >> i.first;
int ans1 = 0;
for(pii i:kek)
ans1 += i.second;
vi lol;
for(pii i:kek)
lol.pb(i.first + i.second * 2);
sort(all(lol));
reverse(all(lol));
for(int i=0; i<n; i++){
ans1 -= lol[i];
if(ans1 < 0){
cout << i + 1 << endl;
return;
}
}
cout << n << endl;
}
signed main(){
fast_io();
// srand(time(NULL));
// cout << fixed << setprecision(3);
int q = 1;
// cin >> q;
while(q--)
solve();
}
| #pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define trav(a,x) for (auto& a : x)
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1000000007;
const char nl = '\n';
const int MX = 100001; //check the limits, dummy
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int N; cin >> N;
ll A[N], B[N]; F0R(i, N) cin >> A[i] >> B[i];
vl ops; ll sum = 0;
F0R(i, N) {
sum += A[i]; ops.pb(2*A[i] + B[i]);
}
sort(all(ops)); reverse(all(ops));
int ans = 0;
F0R(i, N) {
ans++; sum -= ops[i];
if (sum < 0) break;
}
cout << ans << nl;
return 0;
}
// read the question correctly (ll vs int)
// template by bqi343
|
#include<iostream>
using namespace std;
int main(){
int n;
long long cnt=0;
cin>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=n/i;j++){
for(int k=1;k<=n/i/j;k++){
cnt++;
}
}
}
cout<<cnt<<endl;
return(0);
}
| #include<iostream>
#include<utility>
#include<algorithm>
#include<set>
#include<vector>
#include<cmath>
#define forn(i, x, n) for(int i = x; i < (int)(n); i++)
using namespace std;
typedef long long ll;
const int mod = 998244353;
const bool DEBUG = 0;
vector<int> primes;
vector<int> lp(2e5+1);
void sieve(){
forn(i, 2, lp.size()){
if(lp[i] == 0){
lp[i] = i;
primes.push_back(i);
}
for(int j = 0; j < primes.size() && primes[j] <= i && primes[j]*i < lp.size(); j++){
lp[primes[j]*i] = primes[j];
}
}
}
void solve(){
int k;
cin>>k;
sieve();
ll ans = 1;
forn(i, 2, k+1){
ll prod = 1;
for(ll tmp = i, x = lp[tmp]; tmp > 1; x = lp[tmp]){
ll p = 0;
while(tmp%x == 0) tmp /= x, p++;
if(DEBUG) cout<<i<<' '<<p<<'\n';
prod *= (p+2)*(p+1)/2;
}
if(DEBUG) cout<<prod<<'\n';
ans += prod;
}
cout<<ans;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
//cin>>t;
//while(t--) cout<<solve()<<'\n';
while(t--) solve(), cout<<'\n';
}
|
#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#pragma GCC optimize(2)
#include<set>
#include<map>
#include<cmath>
#include<queue>
#include<string>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=2010;
const ll mod=1e9+7;
char g[N][N];
int n,m;
ll base[N*N];
int w[N][N];
int main()
{
IO;
int T=1;
//cin>>T;
while(T--)
{
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>g[i]+1;
int cnt=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++) cnt+=(int)g[i][j]=='.';
base[0]=1;
for(int i=1;i<=cnt;i++) base[i]=base[i-1]*2%mod;
ll res=0;
for(int i=1;i<=n;i++)
{
int j=1;
while(j<=m)
{
while(g[i][j]=='#') j++;
int now=1,k=1;
while(j+k<=m&&g[i][j+k]=='.') now++,k++;
for(int p=j;p<j+k;p++) w[i][p]+=now;
j=j+k;
}
}
for(int i=1;i<=m;i++)
{
int j=1;
while(j<=n)
{
while(g[j][i]=='#') j++;
int now=1,k=1;
while(j+k<=n&&g[j+k][i]=='.') now++,k++;
for(int p=j;p<j+k;p++) w[p][i]+=now;
j=j+k;
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(g[i][j]=='#') continue;
int now=w[i][j]-1;
res=(res+(base[now]-1)*base[cnt-now]%mod)%mod;
}
}
cout<<(ll)(res+mod)%mod<<'\n';
}
return 0;
} | #pragma GCC optimize("Ofast")
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;
using ll = int64_t;
using db = double;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vector<int>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vpii = vector<pii>;
using vpll = vector<pll>;
constexpr char newl = '\n';
constexpr double eps = 1e-10;
#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define F0R(i,b) FOR(i,0,b)
#define RFO(i,a,b) for (int i = ((b)-1); i >=(a); i--)
#define RF0(i,b) RFO(i,0,b)
#define show(x) cout << #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 {cout<<"Yes";}else{cout<<"No";}
#define YESNO {cout<<"YES";}else{cout<<"NO";}
#define v(T) vector<T>
#define vv(T) vector<vector<T>>
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;
}
#if 1
// INSERT ABOVE HERE
signed main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int N, M; cin >> N >> M;
vv(vi) es(N, vvi(26));
F0R(i, M) {
int a, b; char c; cin >> a >> b >> c;
a--; b--;
es[a][c-'a'].push_back(b);
es[b][c - 'a'].push_back(a);
}
//
int inf = INT32_MAX / 2;
vvi dp(N, vi(N, inf));
dp[0][N - 1] = 0;
queue<pii> qu;
qu.emplace(0, N - 1);
int step = 0;
while (sz(qu)) {
queue<pii> nq;
while (sz(qu)) {
auto [u, v] = qu.front(); qu.pop();
if (dp[u][v] < step) continue;
//cout << (u + 1) << '-' << (v + 1) << newl;
F0R(c, 26) {
for (auto to : es[u][c]) {
for (auto to2 : es[v][c]) {
if (dp[to][to2] <= step + 1) continue;
dp[to][to2] = step + 1;
nq.emplace(to, to2);
}
}
}
}
qu = move(nq);
step++;
}
cout << (dp[N - 1][0] >= inf ? -1 : dp[N- 1][0]) << newl;
}
#endif
|
#include<bits/stdc++.h>
#define endl '\n'
#define ri register
#define QAQ(m,n) for(ri int i=m;i<=n;i++)
#define QWQ(m,n) for(ri int j=m;j<=n;j++)
typedef long long ll;
using namespace std;
int main ()
{
double a, b;
cin >> a >> b;
cout << (a - b) / a*100 << endl;
return 0;
} | #include <iostream>
using namespace std;
typedef long long ll;
int main () {
int H, W;
int min = 101;
int sum = 0;
cin >> H >> W;
for (int h=0; h<H; h++) {
for (int w=0; w<W; w++) {
int tmp;
cin >> tmp;
sum += tmp;
if (tmp < min)
min = tmp;
}
}
cout << sum - min*H*W << endl;
return 0;
} |
//g++ 7.4.0
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//A + k*B <= D*C*k
//A/(D*C - B) <= k;
ll A,B,C,D; cin>>A>>B>>C>>D;
ll num = A;
ll den = D*C - B;
if(den <= 0)
cout<<-1<<endl;
else
cout<<(num + den - 1)/den<<endl;
} |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD (ll)1000000007
#define pi 3.14159265358979323846264338327
#define pii pair<int,int>
#define pll pair<ll,ll>
#define UNIQUE(v) sort(v.begin(),v.end());v.erase(unique(v.begin(),v.end()),v.end());
ll GCD(ll a,ll b){return b?GCD(b,a%b):a;}
ll power(ll a,ll b){ll ret=1;while(b){if(b%2)ret=ret*a%MOD;a=a*a%MOD;b/=2;}return ret;}
const int sz = 1<<17;
int a[20];
int num[30];
int f(int s,int n)
{
int ret=0;
for(int i=0;i<n;i++)
if((1<<i)&s)ret|=a[i];
return ret;
}
double dist(double x0,double y0, double x1, double y1)
{
return (x0-x1)*(x0-x1)+(y0-y1)*(y0-y1);
}
int main()
{
int n;scanf("%d",&n);
double x0,y0,xh,yh;
scanf("%lf %lf %lf %lf",&x0,&y0,&xh,&yh);
double xm=(x0+xh)/2.0, ym=(y0+yh)/2.0;
x0-=xm,y0-=ym;
double degree=pi*2.0/(double)n;
double nx=x0*cos(degree)-y0*sin(degree);
double ny=x0*sin(degree)+y0*cos(degree);
printf("%lf %lf",nx+xm,ny+ym);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF 1000000000000000000
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int tt=1;
//cin >> tt;
while(tt--)
{
int n;
cin >> n;
int a[n],b[n];
for(int i=0;i<n;i++)
cin >> a[i];
for(int i=0;i<n;i++)
cin >> b[i];
pair<int,int> odd[n/2], even[n/2];
for(int i=0;i<n;i++)
{
if(i%2==0)
even[i/2]={a[i]-b[i],i};
else
odd[i/2]={a[i]-b[i],i};
}
sort(even,even+n/2);
sort(odd,odd+n/2);
int ans=0;
for(int i=n/2-1;i>=0;i--)
{
if(even[i].first+odd[i].first>=0)
{
ans+=a[even[i].second]+a[odd[i].second];
}
else
{
ans+=b[even[i].second]+b[odd[i].second];
}
}
cout << ans << endl;
}
return 0;
} | #include <iostream>
#include <stdio.h>
#include <vector>
#include <list>
#include <algorithm>
#include <string.h>
#include <stack>
#include <map>
#include <set>
using namespace std;
int main(int argc, char *argv[])
{
int n;
cin >> n;
vector<long long> a(n);
long long cu = 0;
for (int i = 0; i < n; i++)
{
// 行かなきゃ-A,行けば+A+B
// 行くと+2A+B
long long x, y;
cin >> x >> y;
a[i] = 2 * x + y;
cu -= x;
}
sort(a.begin(), a.end());
for (int i = n - 1; i >= 0; i--)
{
cu += a[i];
if (cu > 0)
{
printf("%d", n - i);
return 0;
}
}
return 0;
} |
#include <bits/stdc++.h>
// #include <atcoder/all>
using std::cin;
using std::cout;
using std::endl;
using std::sort;
using std::string;
using std::vector;
// using namespace atcoder;
typedef long long ll;
#ifdef _DEBUG
#define DEBUG(x) cout << #x << ": " << x << endl;
#else
#define DEBUG(x)
#endif
#define ALL(obj) (obj).begin(), (obj).end()
#define BIT(n) (1LL << (n))
#define REP(i, n) for (int i = 0; i < (n); ++i)
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
int main()
{
std::ios::sync_with_stdio(false);
cin.tie(0);
// write code below
int n, q;
string s;
cin >> n >> s >> q;
bool flip = false;
REP(i, q)
{
int t, a, b;
cin >> t >> a >> b;
a--;
b--;
if (t == 1)
{
if (flip)
{
if (a >= n)
{
a %= n;
}
else
{
a += n;
}
if (b >= n)
{
b %= n;
}
else
{
b += n;
}
}
std::swap(s[a], s[b]);
}
else
{
flip = !flip;
}
}
string l = s.substr(0, n), r = s.substr(n);
if (flip)
{
cout << r + l << endl;
}
else
{
cout << l + r << endl;
}
// write code above
return 0;
}
| #include<bits/stdc++.h>
#define rep(i,a,b) for(int i = a; i < b; i++)
#define rrep(i,a,b) for (int i = a - 1; i >= b; i--)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ll long long
#define dbwt(x) std::cout << fixed << setprecision(15) << x << '\n'
#define Wt(x) cout << x << '\n'
#define re0 return 0
#define Int(...) int __VA_ARGS__; in(__VA_ARGS__)
#define Ll(...) long long __VA_ARGS__; in(__VA_ARGS__)
#define Str(...) string __VA_ARGS__; in(__VA_ARGS__)
#define Vec(t,a,s) vector<t>a(s); in(a)
#define Vec2(t,a,b,s) vector<t>a(s),b(s);rep(i,0,s)in(a[i],b[i])
#define Vec3(t,a,b,c,s) vector<t>a(s),b(s),c(s);rep(i,0,s)in(a[i],b[i],c[i])
#define Vec4(t,a,b,c,d,s) vector<t>a(s),b(s),c(s),d(s);rep(i,0,s) in(a[i],b[i],c[i],d[i])
#define mvec(t,a,i) vector<t>(a,i)
#define mvec2(t,a,b,i) vector<vector<t>>(a,vector<t>(b,i))
void scan(int& x) {std::cin >> x;}
void scan(ll& x) {std::cin >> x;}
void scan(double& x) {std::cin >> x;}
void scan(std::string& x) {std::cin >> x;}
void scan(std::vector<int>& x) {rep (i, 0, x.size()) std::cin >> x[i];}
void scan(std::vector<ll>& x) {rep (i, 0, x.size()) std::cin >> x[i];}
void in(){}
template<class Head, class... Tail>
void in(Head& head, Tail&... tail) {scan(head); in(tail...);}
template<class T, class U>
bool chmax(T& a, U b) {if(a < b){a=b; return true;} return false;}
template<class T, class U>
bool chmin(T& a, U b) {if(a > b){a=b; return true;} return false;}
using namespace std;
template<class T>
using vvec = vector<vector<T>>;
const int INF = 1001001001;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
int main() {
Int(H, W);
vector<string> a(H);
rep (i, 0, H) cin >> a[i];
int sx, sy, gx, gy;
set<int> st[26];
rep (i, 0, H) rep (j, 0, W) {
if (a[i][j] == 'S') sy = i, sx = j;
if (a[i][j] == 'G') gy = i, gx = j;
if ('a' <= a[i][j] && a[i][j] <= 'z') {
st[a[i][j] - 'a'].insert(i * W + j);
}
}
vector<bool> visited(26, false);
vvec<int> dist(H, vector<int>(W, INF));
dist[sy][sx] = 0;
queue<int> Q;
Q.push(sy * W + sx);
while (!Q.empty()) {
int v = Q.front(); Q.pop();
int vx = v % W;
int vy = v / W;
if ('a' <= a[vy][vx] && a[vy][vx] <= 'z' && !visited[a[vy][vx] - 'a']) {
visited[a[vy][vx] - 'a'] = true;
st[a[vy][vx] - 'a'].erase(v);
rep (i, 0, 4) {
int x = vx + dx[i];
int y = vy + dy[i];
if (y < 0 || y >= H || x < 0 || x >= W || a[y][x] == '#') {
continue;
}
if (chmin(dist[y][x], dist[vy][vx] + 1)) {
Q.push(y * W + x);
}
}
for (auto idx = st[a[vy][vx] - 'a'].begin(); idx != st[a[vy][vx] - 'a'].end(); ++idx) {
int x = *idx % W;
int y = *idx / W;
if (chmin(dist[y][x], dist[vy][vx] + 1)) {
Q.push(y * W + x);
}
}
}
else {
rep (i, 0, 4) {
int x = vx + dx[i];
int y = vy + dy[i];
if (y < 0 || y >= H || x < 0 || x >= W || a[y][x] == '#') {
continue;
}
if (chmin(dist[y][x], dist[vy][vx] + 1)) {
Q.push(y * W + x);
}
}
}
}
if (dist[gy][gx] == INF) Wt(-1);
else Wt(dist[gy][gx]);
} |
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin>>
#define ll long long
#define ln cout<<'\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}void pr(){ln;}
template<class A,class...B>void pr(const A &a,const B&...b){cout<<a<<(sizeof...(b)?" ":"");pr(b...);}
template<class A>void PR(A a,ll n){rep(i,n)cout<<(i?" ":"")<<a[i];ln;}
const ll MAX=1e9+7,MAXL=1LL<<61,dx[8]={-1,0,1,0,-1,-1,1,1},dy[8]={0,1,0,-1,-1,1,1,-1};
typedef pair<ll,ll> P;
clock_t t2;
#define MAX_V 600002
struct edge{ int to,cap,rev; };
vector<edge> G[MAX_V];
int level[MAX_V],iter[MAX_V];
void add_edge(int from,int to,int cap){
G[from].push_back((edge){to,cap,G[to].size()});
G[to].push_back((edge){from,0,G[from].size()-1});
}
void bfs(int s){
memset(level,-1,sizeof(level));
queue<int> Q;
level[s] = 0;
Q.push(s);
while(!Q.empty()){
int v = Q.front(); Q.pop();
for(int i = 0 ; i < (int)G[v].size() ; i++){
edge &e = G[v][i];
if(e.cap > 0 && level[e.to] < 0){
level[e.to] = level[v] + 1;
Q.push(e.to);
}
}
}
}
int dfs(int v,int t,int f){
if(v == t) return f;
for(int &i = iter[v] ; i < (int)G[v].size() ; i++){
edge &e = G[v][i];
if(e.cap > 0 && level[v] < level[e.to]){
int d = dfs(e.to,t,min(f,e.cap));
if(d > 0){
e.cap -= d;
G[e.to][e.rev].cap += d;
return d;
}
}
}
return 0;
}
ll n;
int max_flow(int s,int t){
int flow = 0;
for(;;){
clock_t t3=clock();
double t4=(double)(t3-t2)/CLOCKS_PER_SEC;
if((double)t3/CLOCKS_PER_SEC+t4*2>1.997) {
pr(n);
exit(0);
}
bfs(s);
if(level[t] < 0) return flow;
memset(iter,0,sizeof(iter));
int f;
while((f = dfs(s,t,MAX)) > 0){
flow += f;
}
}
}
void Main() {
t2=clock();
R n;
P a[n];
rep(i,n) cin >> a[i].F >> a[i].S;
rep(i,n) a[i].F--,a[i].S--;
rep(i,n) {
add_edge(600000,i,1);
add_edge(i,n+a[i].F,1);
if(a[i].F!=a[i].S) add_edge(i,n+a[i].S,1);
}
rep(i,400000) add_edge(n+i,600001,1);
pr(max_flow(600000,600001));
}
int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}
| #line 1 "main.cpp"
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cfloat>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <string.h>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,n) for(int i=1;i<=n;i++)
#define int long long
#define ll long long
#define eps LDBL_EPSILON
#define mod (ll)1000000007
#define INF LLONG_MAX/10
#define P pair<int,int>
#define prique priority_queue
using namespace std;
int par[400010], rankh[400010], siz[400010], edge[400010];
void init(int n) {
for (int i = 0; i <= n; i++) {
par[i] = i;
rankh[i] = 0;
siz[i] = 1;
edge[i] = 0;
}
}
int find(int x) {
if (par[x] == x)return x;
else return par[x] = find(par[x]);
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x == y) {
edge[x]++;
return;
}
if (rankh[x] < rankh[y]) {
par[x] = y;
siz[y] += siz[x];
edge[y] += edge[x] + 1;
}
else {
par[y] = x;
if (rankh[x] == rankh[y])rankh[x]++;
siz[x] += siz[y];
edge[x] += edge[y] + 1;
}
}
bool same(int x, int y) {
return find(x) == find(y);
}
int size(int x) {
return siz[find(x)];
}
int edges(int x) {
return edge[find(x)];
}
int n, a[200010], b[200010];
map<int, int> mp;
signed main() {
cin >> n;
rep(i, n) {
cin >> a[i] >> b[i];
mp[a[i]] = 0;
mp[b[i]] = 0;
}
int cnt = 1;
for (pair<const int, int>& p : mp) {
p.second = cnt;
cnt++;
}
init(mp.size());
rep(i, n) {
unite(mp[a[i]], mp[b[i]]);
}
set<int> st;
int ans = 0;
REP(i, mp.size()) {
if (st.find(find(i)) != st.end())continue;
ans += min(edges(i), size(i));
st.insert(find(i));
}
cout << ans << endl;
}
|
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
const ll mod=998244353;
const int N=2e6+1;
ll n,m;
ll a[N],b[N];
void solve(){
cin >> n >> m;
for(int i=1; i<=n ;i++){
ll x;cin >> x;a[x]++;
}
ll ans=0;
for(int i=0; i<=n+10 ;i++){
m=min(m,a[i]);
ans+=m;
}
cout << ans << '\n';
//for(int i=1; i<=n ;i++) cout << a[i]*b[i] << ' ';
}
int main(){
ios::sync_with_stdio(false);cin.tie(0);
//int t;cin >> t;while(t--) solve();
solve();
} | #include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORd(i, a, b) for (int i = (a) - 1; i >= (b); --i)
#define FORall(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define sz(a) int((a).size())
#define present(t, x) (t.find(x) != t.end())
#define all(a) (a).begin(), (a).end()
#define uni(a) (a).erase(unique(all(a)), (a).end())
#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define prec(n) fixed<<setprecision(n)
#define bit(n, i) (((n) >> (i)) & 1)
#define bitcount(n) __builtin_popcountll(n)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
//const int MOD = (int) 1e9 + 7;
const int MOD = 119 << 23 | 1;
const int FFTMOD = 119 << 23 | 1;
const int INF = (int) 1e9 + 23111992;
const ll LINF = (ll) 1e18 + 23111992;
const ld PI = acos((ld) -1);
const ld EPS = 1e-9;
inline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;}
inline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
inline ll fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;}
template<class T> inline int chkmin(T& a, const T& val) {return val < a ? a = val, 1 : 0;}
template<class T> inline int chkmax(T& a, const T& val) {return a < val ? a = val, 1 : 0;}
inline ull isqrt(ull k) {ull r = sqrt(k) + 1; while (r * r > k) r--; return r;}
inline ll icbrt(ll k) {ll r = cbrt(k) + 1; while (r * r * r > k) r--; return r;}
inline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;}
inline void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;}
inline int mult(int a, int b, int p = MOD) {return (ll) a * b % p;}
inline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);}
inline int sign(ld x) {return x < -EPS ? -1 : x > +EPS;}
inline int sign(ld x, ld y) {return sign(x - y);}
mt19937 mt(chrono::high_resolution_clock::now().time_since_epoch().count());
inline int mrand() {return abs((int) mt());}
inline int mrand(int k) {return abs((int) mt()) % k;}
#define db(x) cerr << "[" << #x << ": " << (x) << "] ";
#define endln cerr << "\n";
void chemthan() {
int n, k; cin >> n >> k;
map<int, int> hs;
FOR(i, 0, n) {
int x; cin >> x;
hs[x]++;
}
map<int, vi> g;
for (auto [x, c] : hs) {
FOR(i, 0, min(c, k)) {
g[i].pb(x);
}
}
auto calc = [&] (vi v) {
map<int, int> tmp;
for (int x : v) {
tmp[x] = 1;
}
int res = 0;
while (tmp[res]) res++;
return res;
};
int res = 0;
FOR(i, 0, k) {
res += calc(g[i]);
}
cout << res << "\n";
}
int32_t main(int32_t argc, char* argv[]) {
ios_base::sync_with_stdio(0), cin.tie(0);
if (argc > 1) {
assert(freopen(argv[1], "r", stdin));
}
if (argc > 2) {
assert(freopen(argv[2], "wb", stdout));
}
chemthan();
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef pair <int, int> pii;
typedef pair <pii, int> ppi;
const int N = 1010;
const int M = 3200;
const int mod = 998244353;
int a[2];
void solve() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 0; i < n; ++i) {
string s;
cin >> s;
int cnt = 0;
for (int j = 0; j < s.length(); ++j) {
cnt += s[j] - '0';
}
a[cnt & 1]++;
}
printf("%lld\n", a[0] * 1ll * a[1]);
}
int main() {
#ifdef HAMLET
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
solve();
return 0;
}
| #include <cmath>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include<cstdio>
#include<functional>
#include <bitset>
#include <iomanip>
#include <cctype>
#include <list>
#include <cassert>
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for(ll i = n; i >= 0; i--)
#define ll long long
#define repi(i,a,b) for(ll i=a;i<b;++i)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
using namespace std;
template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }
template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
const ll INF = 1000000000000000000;
const ll MOD = 1e9 + 7;
using vvv = vector<vector<vector<ll>>>;
using vv = vector<vector<ll>>;
using vec = vector<ll>;
using P = pair<ll, ll>;
int main(void) {
ll n, m; cin >> n >> m;
ll cnt = 0;
string start; cin >> start;
vector<bool> ml(n - 1);
rep(i, n - 1){
string s; cin >> s;
ll match = 0;
rep(j, m){
if(s[j] == start[j]) match++;
}
if(m % 2 && match % 2 == 0){
ml[i] = 1;
cnt++;
}
if(m % 2 == 0 && match % 2){
ml[i] = 1;
cnt++;
}
//cout << ml[i] << endl;
}
ll ans = cnt;
ll unMatch = n - cnt - 1;
rep(i, n - 1){
if(ml[i] == 1){
ans += unMatch;
cnt--;
}
else{
ans += cnt;
unMatch--;
}
}
cout << ans << endl;
} |
#include <bits/stdc++.h>
using namespace std;
//ofstream cout("output.out");
#define fast() {ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);}
#define pb push_back
#define vi vector<int>
#define vl vector<ll>
#define vii vector<vector<int>>
#define mii map<int, int>
#define pii pair<int, int>
#define qii priority_que<int, vector<int>, greater<int>>
#define For(i, n) for(int i = 0; i < n; ++i)
#define For1(i, n) for(int i = 1; i <= n; ++i)
#define Forlr(i, l, r) for(int i = l; i <= r; ++i)
#define Forrl(i, r, l) for(int i = r; i >= l; --i)
#define o(a) { cout << #a << ":" << (a) <<" ";}
#define ov(a) {For(i, a.size()) cout << a[i] <<" ";}
#define ovv(a) {For(i, a.size()) {For(j, a[i].size()) cout << a[i][j] <<" ";cout <<"\n";}}
using ll = long long;
int n, m, k, q;
const int INF = 1e9 + 5;
const int nax = 1e5 + 5;
pair<int, int> a[5 * nax];
int g[1505][1505];
ll rs = 0;
void dfs(int x, int y){
int nx = x, ny = y;
if(!g[nx][ny]) rs ++, g[nx][ny] = 3;
while(1){
nx ++;
if(nx <= n && g[nx][ny] != 1 && g[nx][ny] != 3){
if(!g[nx][ny]) rs ++;
g[nx][ny] = 1;
}
else{
break;
}
}
nx = x;
while(1){
nx --;
if(nx >= 1 && g[nx][ny] != 1 && g[nx][ny] != 3){
if(!g[nx][ny]) rs ++;
g[nx][ny] = 1;
}
else{
break;
}
}
nx = x;
while(1){
ny ++;
if(ny <= m && g[nx][ny] != 3 && g[nx][ny] != 2){
if(!g[nx][ny]) rs ++;
g[nx][ny] = 2;
}
else{
break;
}
}
ny = y;
while(1){
ny --;
if(ny >= 1 && g[nx][ny] != 3 && g[nx][ny] != 2){
if(!g[nx][ny]) rs ++;
g[nx][ny] = 2;
}
else{
break;
}
}
}
int main()
{
fast();
cin >> n >> m;
int na, nb;
cin >> na >> nb;
For(i, na){
cin >> a[i].first >> a[i].second;
}
For(i, nb){
int x, y;
cin >> x >> y;
g[x][y] = 3;
}
For(i, na){
dfs(a[i].first, a[i].second);
}
// For(i, n){
// For(j, m){
// cout << g[i + 1][j + 1] <<" ";
// }
// cout <<"\n";
// }
cout << rs;
} | #include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pint = pair<ll,ll>;
const ll INF = 1e18;
ll N, M, S, T;
vector<tuple<ll,ll,ll,ll>> edge;
//経路復元なしDijkstra法
vector<ll> dijkstra(ll start, const vector<vector<pair<ll,ll>>>& Hen){
ll N = Hen.size();
vector<ll> dist(N, INF);
dist[start] = 0;
priority_queue<pair<ll,ll>, vector<pair<ll,ll>>, greater<pair<ll,ll>>> que;
que.push({0, start});
while(que.size()) {
ll ver = que.top().second;
ll cost = que.top().first; que.pop();
if(cost != dist[ver])continue;
for(auto i : Hen[ver]) {
auto [u, v, t, k] = edge[i.second]; //t := 長さ, k := インターバル
auto req = cost + (k - cost % k) % k + t;
if(dist[i.first] > req) {
dist[i.first] = req;
que.push({dist[i.first], i.first});
}
}
}
return dist;
}
int main()
{
cin >> N >> M >> S >> T;
edge.resize(M);
vector<vector<pair<ll,ll>>> hen(N);
S--,T--;
for(ll i = 0; i < M; i++) {
ll u, v, t, k ;cin >> u >> v >> t >> k;
u--,v--;
hen[u].push_back({v, i});
hen[v].push_back({u, i});
edge[i] = {u, v, t, k};
}
auto dist = dijkstra(S, hen);
cout << (dist[T] == INF ? -1 : dist[T])<< endl;
} |
#include <cstring>
#include <cstdlib>
#include <map>
#include <unordered_map>
#include <string>
#include <list>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstdio>
#include <iostream>
#include <set>
#include <unordered_set>
using namespace std;
#define ABS(a) ((a)<0?(-(a)):(a))
#define CS while(cases())
#define I(n) int n = gi()
#define II(n) n = gi()
#define L(n) ll n = gli()
#define LL(n) n = gli()
#define F(i, n) for (int i = 0; i < n; i++)
#define Fa(n, a) for (int i = 0; i < n; i++) a[i] = gi()
#define R(i, n) for (int i = n-1; i >= 0; i--)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef pair<int, int> pii;
template<class T>
T gcd(T a, T b) {
return (b == 0 ? a : gcd(b, a%b));
}
int gi() {
int a;
scanf("%d", &a);
return a;
}
ll gli() {
ll a;
scanf("%lld", &a);
return a;
}
int cases() {
static int t = gi();
return t--;
}
#define MD 1000000007
int mx[200004];
int mn[200004];
void upd(map<int, ll> &m, int k, ll v) {
if (m.find(k) == m.end() || v < m[k])
m[k] = v;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
I(n);
F(i, n) {
mn[i] = MD;
mx[i] = -MD;
}
F(i, n) {
I(x);
I(c) - 1;
mn[c] = min(mn[c], x);
mx[c] = max(mx[c], x);
}
map<int, ll> m;
m[0] = 0;
F(i, n) if (mn[i] != MD) {
map<int, ll> m2;
for (auto &it : m) {
upd(m2, mx[i], it.second + ABS(mn[i] - it.first) + mx[i] - mn[i]);
if (mx[i] != mn[i])
upd(m2, mn[i], it.second + ABS(mx[i] - it.first) + mx[i] - mn[i]);
}
m = m2;
}
ll res = 100000000000000000LL;
for (auto &it : m)
res = min(res, it.second + ABS(it.first));
cout << res << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define pb push_back
#define ll long long
#define ld long double
#define sz(x) (ll)x.size()
#define vll vector<long long>
#define infi INT_MAX
#define infl LONG_LONG_MAX
#define infd LDBL_MAX
#define f(i,a,b) for(ll i=a;i<b;i++)
#define fi(i,a,b) for(ll i=(b-1);i>=a;i--)
#define F first
#define S second
#define G(a,b) get<a>(b)
#define MP make_pair
#define MT make_tuple
#define pll pair<ll,ll>
#define endl "\n"
#define ALL(v) v.begin(),v.end()
#define nl cout<<"\n";
#define deb cout<<"***************************************************************************************\n";
#define moshi cout<<"moshi moshi ! \n";
#define hi cout<<"hi ! \n";
#define bye cout<<"bye bye ! \n";
#define kawai cout<<"O kawai koto ! \n";
#define o_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define number_of_set_bits __builtin_popcountll
#define eb emplace_back
const ld PI=3.1415926535897932384626433;
template<class T>
using min_heap = priority_queue<T,vector<T>,greater<T> >;
auto clk=clock();
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cout << name << " : " << arg1 <<"\n";
//use cerr if u want to display at the bottom
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif
//********************************************************** Code begins ***********************************************
// trick for traversing all submasks
// for (ll j=mask ;j; j=(j-1)&mask) // also include 0 manually
// v.resize(unique(v.begin(), v.end()) - v.begin()); // remove duplicates
const ll N=1e5+5;
ll n,m,k,a[N],mn[N],mx[N],b[N];
ll ceil(ll p,ll q)
{
ll res=(p+q-1)/q;
return res;
}
bool can(ll d)
{
ll small=0,big=0;
f(i,1,k+1)
{
mn[i]=ceil(-d+a[i]*m,n);
mx[i]=(d+a[i]*m)/n;
small+=mn[i];
big+=mx[i];
}
return (small<=m && big>=m);
}
void help(ll d)
{
ll small=0,big=0,extra=m;
f(i,1,k+1)
{
mn[i]=ceil(-d+a[i]*m,n);
mx[i]=(d+a[i]*m)/n;
small+=mn[i];
big+=mx[i];
extra-=mn[i];
}
f(i,1,k+1)
{
ll give=mx[i]-mn[i],take=min(give,extra);
extra-=take;
b[i]=mn[i]+take;
}
}
void solve()
{
cin>>k>>n>>m;
f(i,1,k+1) cin>>a[i];
ll lef=0,rig=1e18,mid,res=-1;
while(lef<=rig)
{
mid=(lef+rig)>>1;
if(can(mid)) res=mid,rig=mid-1;
else lef=mid+1;
}
help(res);
f(i,1,k+1) cout<<b[i]<<" ";
nl
}
int main()
{
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
srand(chrono::high_resolution_clock::now().time_since_epoch().count());
cout<<fixed<<setprecision(8);
ll test_cases=1;
//cin>>test_cases;
f(i,1,test_cases+1)
{
solve();
//clr();
}
#ifdef rd
cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
#endif
return 0;
}
|
#include <cassert>
#include <iostream>
using namespace std;
constexpr int M = 998244353;
long long modexp(int x, long long e, int m) {
long long ans = 1, p = x % m;
while (e > 0) {
if (e % 2 != 0) ans = (ans * p) % m;
p = (p * p) % m;
e >>= 1;
}
return ans;
}
long long modinv(long long a, long long m) {
long long x = m, y = a, p = 1, q = 0, r = 0, s = 1;
while (y != 0) {
long long u = x / y;
long long x0 = y; y = x - y * u; x = x0;
long long r0 = p - r * u, s0 = q - s * u;
p = r; r = r0; q = s; s = s0;
}
return q < 0 ? q + m : q;
}
int main() {
int n, m; cin >> n >> m;
long long ans = 0;
const int mi = modinv(m, M);
if (n == 1) { cout << m << endl; return 0; }
for (int v = 1; v <= m; v++) {
long long p = m - v + 1, q = m - v, mm = modexp(m, n-2, M);
for (int r = 0; r < n; r++) {
ans += (p - q) * (r == n - 1 ? 1 : (v - 1) * mm % M) % M;
p = p * (m - v + 1) % M;
q = q * (m - v) % M;
mm = mm * mi % M;
}
}
for (int v = 1; v <= m; v++) {
long long p = m - v + 1, q = m - v, mm = modexp(m, n-2, M);
for (int l = n-1; l > 0; l--) {
ans += (p - q) * (v - 1) % M * mm % M;
p = p * (m - v + 1) % M;
q = q * (m - v) % M;
mm = mm * mi % M;
}
}
if (n > 2) {
for (int v = 1; v <= m; v++) {
long long p = m - v + 1, q = m - v, mm = modexp(m, n-3, M);
for (int d = 0; d <= n-3; d++) {
ans += ((long long)(n - d - 2) * (v - 1) * (v - 1) % M *
(p - q) % M * mm % M);
p = p * (m - v + 1) % M;
q = q * (m - v) % M;
mm = mm * mi % M;
}
}
}
cout << (ans % M + M) % M << endl;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<typename A, typename B = A> using pr = pair<A, B>;
#define vec vector
#define ar array
#define len(a) ((int)((a).size()))
#define all(a) (a).begin(), (a).end()
const int mod = 998244353;
struct mint {
int val;
explicit mint() : val(0) {}
explicit mint(int val) : val(val) {}
inline mint operator + (mint m) {
int sum = val + m.val;
if (sum >= mod)
sum -= mod;
return mint(sum);
}
inline mint operator - (mint m) {
int sum = val - m.val;
if (sum < 0)
sum += mod;
return mint(sum);
}
inline mint operator * (mint m) {
return mint(1ll * val * m.val % mod);
}
friend ostream& operator << (ostream &out, mint m) {
return out << m.val;
}
};
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int n, m;
cin >> n >> m;
vec<vec<mint>> val(m + 1, vec<mint>(n + 1));
for (int i = 0; i <= m; i++)
val[i][0] = mint(1);
for (int j = 1; j <= n; j++)
for (int i = 1; i <= m; i++)
val[i][j] = val[i][j - 1] * mint(i);
vec<vec<mint>> pref(m + 1, vec<mint>(n + 1));
for (int j = 0; j <= n; j++) {
pref[0][j] = val[0][j];
for (int i = 1; i <= m; i++)
pref[i][j] = pref[i - 1][j] + val[i][j];
}
mint ans = mint(n) * val[m][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < i; j++)
ans = ans - pref[m - 1][i - j - 1] * val[m][n - i + j - 1];
cout << ans << '\n';
}
|
#include <stdio.h>
#include <math.h>
int main(){
long long int n, x[200], y[200], r[200], a[200], b[200], c[200], d[200];
scanf( "%lld", &n );
for( int i = 0 ; i < n ; i++ ){
scanf( "%lld%lld%lld", &x[i], &y[i], &r[i] );
}
for (int i = 0; i < n ; i++){
a[i] = floor( 10000 / n ) * i ;
b[i] = 0 ;
c[i] = floor( 10000 / n ) * ( i + 1 ) - 1;
d[i] = 9999;
}
for( int i = 0 ; i < n ; i++ ){
printf( "%lld %lld %lld %lld\n", a[i], b[i], c[i], d[i] );
}
return 0;
}
| #include <bits/stdc++.h>
#include <algorithm>
using namespace std;
using ull = unsigned long long;
#define loop(n) for(ull i=0;i<n;i++)
int main(){
int n;
cin >> n;
loop(n){
ull x,y,r;
cin >> x >> y >> r;
cout << x << " " << y << " " << x+1 << " " << y+1 << endl;
}
return 0;
} |
#include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
typedef int ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mod 1000000007
#define pii pair<ll,ll>
#define inf 1000000000000000000
#define bpc(x) __builtin_popcountll(x)
#define autoit(x,it) for(auto it = x.begin(); it != x.end(); it++)
#define autoitr(x,it) for(auto it = x.rbegin(); it != x.rend(); it++)
#define rep(n) for(ll i = 0; i < n; i++)
#define repi(i,n) for(ll i = 0; i < n; i++)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
bool comp(pii a, pii b)
{
return a.ff-a.ss>b.ff-b.ss;
}
void dfs(ll curr, ll par, vector<ll> v[], vector<pii> &dp, vector<ll> &tur)
{
vector<pii> hold[2];
dp[curr] = mp(0, 1);
tur[curr] = 0;
autoit(v[curr],it)
{
if(*it == par)
continue;
dfs(*it, curr, v, dp, tur);
tur[curr]^=((dp[*it].ff + dp[*it].ss)%2);
hold[tur[*it]^1].pb(dp[*it]);
}
vector<pii> temp;
if(hold[0].size()>0){
autoit(hold[0],it)
{
if(it->ff>=it->ss)
dp[curr].ff+=it->ff, dp[curr].ss+=it->ss;
else temp.pb(*it);
}
}
hold[0] = temp;
if((ll)hold[1].size()>1)
sort(hold[1].begin(), hold[1].end(), comp);
ll t11 = 0;
if(hold[1].size()>0){
autoit(hold[1],it)
{
if(t11 == 0)
dp[curr].ff+=it->ff, dp[curr].ss+=it->ss;
else dp[curr].ff+=it->ss, dp[curr].ss+=it->ff;
t11^=1;
}
}
if(hold[0].size()>0){
if(t11 == 0){
autoit(hold[0],it)
dp[curr].ff+=it->ff, dp[curr].ss+=it->ss;
}
else{
autoit(hold[0],it)
dp[curr].ff+=it->ss, dp[curr].ss+=it->ff;
}
}
}
int main()
{
FAST/**/
ll n;
cin>>n;
vector<ll> gr[n];
rep(n-1)
{
ll a;
cin>>a;
a--;
ll b = i+1;
if(a == b)
continue;
gr[a].pb(b);
gr[b].pb(a);
}
vector<pii> dp(n, mp(0, 0));
vector<ll> tur(n,0);
dfs(0, -1, gr, dp, tur);
cout<<dp[0].ss<<'\n';
return 0;
}
| #include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main(void){
int n;
cin >> n;
vector< int > p(n+1);
vector< vector< int > > p2c(n+1, vector< int >());
for(int i = 2; i <= n; i++){
cin >> p[i];
p2c[p[i]].push_back(i);
}
vector< int > dp(n+1, -1), dpz(n+1, -1);
auto f = [&](auto f, int v)->void{
if(p2c[v].size() == 0){
dp[v] = 1;
dpz[v] = 1;
return;
}
dp[v] = 1;
vector< int > vp;
int pn = 0;
for(int w: p2c[v]){
f(f, w);
if(dp[w] < 0 && dpz[w] == 0){
dp[v] += dp[w];
}else if(dpz[w] == 0){
pn += dp[w];
}else{
vp.push_back(dp[w]);
}
}
sort(vp.begin(), vp.end());
int vs = vp.size();
for(int i = 0; i < vs; i++) dp[v] += (i%2 == 0 ? vp[i] : -vp[i]);
dp[v] += (vs%2 == 0 ? pn : -pn);
dpz[v] = (1-vs%2);
};
f(f, 1);
cout << (n+dp[1])/2 << endl;
}
|
#include <iostream>
using namespace std;
int main()
{
double x,y;
cin >> x >> y;
long double z = ((x-y)/x)*100;
cout << z << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define mp make_pair
#define sz(v) ((int)(v).size())
#define endl '\n'
#define IO ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define sortA(v) sort(v.begin(), v.end())
#define sortD(v) sort(v.rbegin(), v.rend())
#define all(x) (x).begin(),(x).end()
#define gcd(a,b) __gcd(a,b)
#define rep(i,a,b) for(int i=a;i<b;i++)
#define repd(i,a,b) for(int i=b-1;i>=a;i--)
#define trav(a,x) for(auto &a:x)
typedef vector<int> vi;
// const int N = 1e5 + 5;
void solve(){
float a,b;
cin >> a >> b;
cout << ((a-b)/a)*100 << endl;
}
int32_t main()
{
IO;
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int t = 1;
// cin >> t;
while (t--)
{
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <int64_t mod>
class modint {
int64_t x;
public:
modint(int64_t x = 0) : x((x % mod + mod) % mod) {}
modint operator-() const { return modint(-x); }
modint& operator+=(const modint& a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
modint& operator-=(const modint& a) {
if ((x += mod - a.x) >= mod) x -= mod;
return *this;
}
modint& operator*=(const modint& a) {
(x *= a.x) %= mod;
return *this;
}
modint operator+(const modint& a) const {
return modint(*this) += a;
}
modint operator-(const modint& a) const {
return modint(*this) -= a;
}
modint operator*(const modint& a) const {
return modint(*this) *= a;
}
modint pow(int64_t t) const {
if (not t) return 1;
modint a = pow(t >> 1);
a *= a;
if (t & 1) a *= *this;
return a;
}
// for prime mod
modint inv() const { return pow(mod - 2); }
modint& operator/=(const modint& a) {
return (*this) *= a.inv();
}
modint operator/(const modint& a) const {
return modint(*this) /= a;
}
friend ostream& operator<<(ostream& os,
const modint& m) {
os << m.x;
return os;
}
};
const int64_t mod = 1e9 + 7;
using mint = modint<mod>;
int main() {
int64_t n, p;
cin >> n >> p;
mint ans = mint(p - 1) * mint(p - 2).pow(n - 1);
cout << ans << endl;
return 0;
}
| #include<bits/stdc++.h>
#define fasterthanlight ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define endl "\n"
#define int long long
#define vi vector<int>
#define pb push_back
#define p 1000000007
#define MAX 1e9
#define MIN -1e9
#define hi "visited\n" //sasta debugging
using namespace std;
//author::@whohet-->Het Patel
int power(long long x, unsigned int y)
{
int 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;
}
void solve()
{
int n, px;
cin >> n >> px;
if (n == 1 and px == 2)
{
cout << 1 << endl;
return;
}
int ans = power(px - 2, n - 1);
ans = ((ans % p) * (px - 1) % p) % p;
cout << ans;
}
signed main()
{
fasterthanlight;
int T; T = 1;
//cin >> T;
while (T--)
{
solve();
}
//cout << "\n\n"<<"Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " seconds.\n";
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll inf=1e18,mod=1e9+7;
ll fun(ll n){
ll res=0;
while(n){
res++;
n/=10;
}
return res;
}
bool comp( pair<ll,ll> a, pair<ll,ll> b){
if(a.second!=b.second)
return a.second>b.second;
return a.first>b.first;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
ll n,m,q;cin>>n>>m>>q;
ll a,b;
vector<pair<ll,ll>> arr;
for(ll i=0;i<n;i++)
cin>>a>>b,arr.push_back({a,b});
sort(arr.begin(),arr.end(),comp);
ll barr[m];
for(auto &it:barr)cin>>it;
ll l,r,mi,idx;vector<ll> curr;
while(q--){
cin>>l>>r;
l--;r--;
curr.clear();
for(ll i=0;i<=l-1;i++)curr.push_back(barr[i]);
// cout<<l<<" ";
for(ll i=r+1;i<m;i++)curr.push_back(barr[i]);
if(curr.empty()){
cout<<0<<endl;continue;
}
sort(curr.begin(),curr.end());
ll sum=0,vis[curr.size()]={0};
for(ll i=0;i<n;i++){
for(ll j=0;j<curr.size();j++){
if(vis[j])continue;
if(curr[j]>=arr[i].first){
sum+=arr[i].second;
vis[j]=1;
break;
}
}
}
cout<<sum<<endl;
}
return 0;
}
| #include <bits/stdc++.h>
template <typename T> inline void rd(T& x) {
int si = 1; char c = getchar(); x = 0;
while(!isdigit(c)) si = c == '-' ? -1 : si, c = getchar();
while(isdigit(c)) x = x * 10 + c - 48, c = getchar();
x *= si;
}
template <typename T, typename... Args>
inline void rd(T& x, Args&... args) { rd(x); rd(args...); }
#define fi first
#define se second
#define mkp std::make_pair
typedef long long ll;
typedef double ff;
typedef std::pair <int, int> pii;
const int N = 5e5 + 5, Inf = 0x3f3f3f3f, Mod = 998244353;
const ll InfLL = 0x3f3f3f3f3f3f3f3fLL;
int n, k, o_dg[N];
std::vector <int> E[N];
void Add(int u, int v) {
E[u].push_back(v);
E[v].push_back(u);
++o_dg[u]; ++o_dg[v];
}
int dg[N], mn[N], col[N];
std::queue <int> que;
bool Check(int mid) {
memcpy(dg, o_dg, sizeof(dg));
memset(col, 0x3f, sizeof(col));
int cnt = 0, lst;
for(int i = 1; i <= n; ++i) mn[i] = mid;
for(int i = 1; i <= n; ++i)
if(dg[i] == 1) que.push(i);
while(!que.empty()) {
int u = que.front(); que.pop();
lst = u;
if(!mn[u]) ++cnt, col[u] = 0;
for(auto v : E[u]) {
--dg[v];
if(col[u] > mn[u]) mn[v] = std::min(mn[v], mn[u] - 1);
col[v] = std::min(col[v], col[u] + 1);
if(dg[v] == 1) que.push(v);
}
}
if(col[lst] > mn[lst]) ++cnt;
return cnt <= k;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("a.in", "r", stdin);
freopen("a.out", "w", stdout);
#endif
int test_case_cnt = 1; //rd(test_case_cnt);
while(test_case_cnt--) {
rd(n, k);
for(int i = 1; i < n; ++i) {
int u, v; rd(u, v);
Add(u, v);
}
int l = 0, r = n - 1;
while(l < r) {
int mid = (l + r) >> 1;
if(Check(mid)) r = mid;
else l = mid + 1;
}
printf("%d\n", l);
} return 0;
} |
/**
/> フ
| _ _|
/`ミ _x 彡
/ |
/ ヽ ノ
/ ̄| | | |
| ( ̄ヽ__ヽ_)_)
\二つ
**/
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
#include <iostream>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
#include <deque>
#include <map>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
typedef long long ll;
#define forn for(int i=0;i<n;i++)
#define pb push_back
#define sc second
#define f first
#define kek cout<<"\n";
#define all(v) ((v).begin()), ((v).end())
const int MAXN=100010;
const int MOD=1e9+7;
const int INF=1e9+10;
#define int ll
int raz(int a, int b)
{
return (a+b-1)/b;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tt=1;
// cin>>tt;
while(tt--)
{
// solution goes here
int n,m;
cin>>n>>m;
vector<int> v(n);
forn cin>>v[i];
set<int> need;
set<int> was;
map<int,int> ma;
for(int i=0;i<m;i++)
{
ma[v[i]]++;
was.insert(v[i]);
}
for(int i=0;i<=n;i++)
{
if(!was.count(i)) need.insert(i);
}
int ans=*need.begin();
for(int i=1;i<n;i++)
{
if(i+m-1>=n) break;
ma[v[i-1]]--;
if(!ma[v[i-1]]) need.insert(v[i-1]);
ma[v[i+m-1]]++;
if(need.count(v[i+m-1])) need.erase(v[i+m-1]);
ans=min(ans, *need.begin());
}
cout<<ans;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define int long long
int n, m, mx = -1e18;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
vector<int> graph[n], cost(n);
for (int i = 0; i < n; i++) {
cin >> cost[i];
}
for (int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
graph[b - 1].push_back(a - 1);
}
for (int i = 0; i < n; i++) {
int mn = 1e18;
for (int j : graph[i]) {
mn = min(mn, cost[j]);
}
mx = max(mx, cost[i] - mn);
cost[i] = min(cost[i], mn);
}
cout << mx << endl;
return 0;
}
|
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <tuple>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <functional>
#include <numeric>
#include <iomanip>
#include <stdio.h>
//end of libraries
#define lnf 3999999999999999999
#define inf 999999999
#define PI 3.14159265359
#define endl "\n"
#define fi first
#define se second
#define pb push_back
#define ll long long
#define all(c) (c).begin(),(c).end()
#define sz(c) (int)(c).size()
#define mkp(a,b) make_pair(a,b)
#define make_unique(a) sort(all(a)),a.erase(unique(all(a)),a.end())
#define rsz(a,n) a.resize(n)
#define pii pair <int,int>
#define rep(i,n) for(int i = 0 ; i < n ; i++)
#define drep(i,n) for(int i = n-1 ; i >= 0 ; i--)
#define crep(i,x,n) for(int i = x ; i < n ; i++)
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
const int max_n = 300000;
using namespace std;
int n;
ll d[20] , f[20] , cnt[20];
bool game(vector <int> a,vector<int> b){
crep(i,1,10) {d[i]=0;f[i]=0;}
rep(i,sz(a)) {
d[a[i]]++;
}
rep(i,sz(b)){
f[b[i]]++;
}
int c1=0,c2=0;
crep(i,1,10) {
c1 += pow(10,d[i]) * i;
c2 += pow(10,f[i]) * i;
}
if(c1>c2) return 1;
return 0;
}
vector <int> a , b;
void read(){
string s1,s2;
cin >> s1 >> s2;
rep(i,sz(s1)-1) {
a.pb(s1[i]-'0');
}
rep(i,sz(s2)-1){
b.pb(s2[i]-'0');
}
}
int main(){
fcin;
cin >> n;
read();
crep(x,1,10) {
int d=0;
rep(i,sz(a)) if(a[i]==x)d++;
rep(i,sz(b)) if(b[i]==x)d++;
cnt[x]=(n-d);
}
ll e=0,t=0;
crep(x,1,10) {
crep(y,1,10) {
if(x==y and cnt[x] < 2) continue;
a.pb(x);b.pb(y);
bool g = game(a,b);
a.pop_back();b.pop_back();
if(g==1) {
e+=(cnt[x]*(cnt[y]-(x==y)));
}
t+= (cnt[x]*(cnt[y]-(x==y)));
}
}
long double e1 = e;
long double t1 = t;
cout << fixed<<setprecision(11);
cout << e1/t1 << "\n";
/*
*/
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
int c1[10], c2[10], cnt[10];
long double fav, tot;
int qpow(int x, int e){
if(!e) return 1;
if(e&1) return qpow(x,e-1)*x;
return qpow(x*x,e>>1);
}
main(){
ios_base::sync_with_stdio(false); cin.tie(nullptr);
cout.setf(ios::fixed); cout.precision(5);
int k;
cin >> k;
string s, t;
cin >> s >> t;
for(int i = 0; i < s.size()-1; ++i){
c1[s[i]-'0']++;
cnt[s[i]-'0']++;
}
for(int i = 0; i < t.size()-1; ++i){
c2[t[i]-'0']++;
cnt[t[i]-'0']++;
}
for(int i = 1; i <= 9; ++i){
int x = k-cnt[i];
if(x <= 0) continue;
for(int j = 1; j <= 9; ++j){
int y = k-cnt[j]-(i==j);
if(y <= 0) continue;
tot += x*y;
c1[i]++; c2[j]++;
int sum1 = 0, sum2 = 0;
for(int k = 1; k <= 9; ++k)
sum1 = sum1 + k*qpow(10,c1[k]);
for(int k = 1; k <= 9; ++k)
sum2 = sum2 + k*qpow(10,c2[k]);
if(sum1 > sum2) fav += x*y;
c1[i]--; c2[j]--;
}
}
cout << fav/tot;
} |
#include <bits/stdc++.h>
using namespace std;
template<int M>
struct static_mint {
static_assert(0 < M, "Module must be positive");
int val;
static_mint(): val() {}
static_mint(long long x) : val(x % M) { if (val < 0) val += M; }
static_mint pow(long long n) const { static_mint ans = 1, x(*this); while (n) { if (n & 1) ans *= x; x *= x; n /= 2; } return ans; }
static_mint inv() const { return pow(M - 2); }
friend static_mint pow(const static_mint &m, long long n) { return m.pow(n); }
friend static_mint inv(const static_mint &m) { return m.inv(); }
static_mint operator+() const { static_mint m; m.val = val; return m; }
static_mint operator-() const { static_mint m; m.val = M - val; return m; }
static_mint &operator+=(const static_mint &m) { if ((val += m.val) >= M) val -= M; return *this; }
static_mint &operator-=(const static_mint &m) { if ((val -= m.val) < 0) val += M; return *this; }
static_mint &operator*=(const static_mint &m) { val = (long long) val * m.val % M; return *this; }
static_mint &operator/=(const static_mint &m) { val = (long long) val * m.inv().val % M; return *this; }
friend static_mint operator+ (const static_mint &lhs, const static_mint &rhs) { return static_mint(lhs) += rhs; }
friend static_mint operator- (const static_mint &lhs, const static_mint &rhs) { return static_mint(lhs) -= rhs; }
friend static_mint operator* (const static_mint &lhs, const static_mint &rhs) { return static_mint(lhs) *= rhs; }
friend static_mint operator/ (const static_mint &lhs, const static_mint &rhs) { return static_mint(lhs) /= rhs; }
friend bool operator==(const static_mint &lhs, const static_mint &rhs) { return lhs.val == rhs.val; }
friend bool operator!=(const static_mint &lhs, const static_mint &rhs) { return lhs.val != rhs.val; }
static_mint &operator++() { return *this += 1; }
static_mint &operator--() { return *this -= 1; }
static_mint operator++(int) { static_mint result(*this); *this += 1; return result; }
static_mint operator--(int) { static_mint result(*this); *this -= 1; return result; }
template <typename T> explicit operator T() const { return T(val); }
friend std::ostream &operator<<(std::ostream &os, const static_mint &m) {
return os << m.val;
}
friend std::istream &operator>>(std::istream &is, static_mint &m) {
long long x; is >> x; m = x;
return is;
}
};
template <typename>
struct is_mint : public std::false_type { };
template <int M>
struct is_mint<static_mint<M>> : public std::true_type { };
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
using mint = static_mint<998244353>;
int n, m, k;
cin >> n >> m >> k;
if (m == 1) {
cout << mint(k).pow(n) << '\n';
return 0;
} else if (n == 1) {
cout << mint(k).pow(m) << '\n';
return 0;
}
mint ans = 0;
for (int mx = 1; mx <= k; mx++) {
mint ways = mint(mx).pow(n) - mint(mx - 1).pow(n);
ans += ways * mint(k - mx + 1).pow(m);
}
cout << ans << '\n';
return 0;
} | #include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int main()
{
int a,b,c;
cin>>a>>b>>c;
if(a>b)
cout<<"Takahashi";
else if(a<b)
cout<<"Aoki";
else
if(c==1)
cout<<"Takahashi";
else
cout<<"Aoki";
return 0;
}
|
#include <bits/stdc++.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int X; scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define RS(X) scanf("%s", (X))
#define CASET int ___T, case_n = 1; scanf("%d ", &___T); while (___T-- > 0)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define RF(x) freopen(x,"r",stdin)
#define WF(x) freopen(x,"w",stdout)
#define loop(I, A, B) for (int I = (A); I <= (B); ++I)
typedef long long LL;
using namespace std;
typedef pair<LL,LL> PLL;
typedef pair<int,int> PII;
const LL MOD = (LL)1e9+7;
const int SIZE = 2e5+5;
const LL INF = 1LL<<60;
const double eps = 1e-4;
const double PI=3.1415926535897932;
PLL xi[200009];
PLL yi[200009];
int main(){
DRI(n);
REP(i,n){
scanf("%lld%lld",&xi[i].F,&yi[i].F);
xi[i].S=yi[i].S=i;
}
sort(xi,xi+n);
sort(yi,yi+n);
pair<LL,PII> ans;
vector<pair<LL,PII> > v;
vector<PII> pp;
pp.PB({0,n-1});
pp.PB({0,n-2});
pp.PB({1,n-1});
for(PII p:pp){
int i = p.F, j = p.S;
v.PB({abs(xi[i].F-xi[j].F),{xi[i].S,xi[j].S}});
v.PB({abs(yi[i].F-yi[j].F),{yi[i].S,yi[j].S}});
}
sort(ALL(v));
reverse(ALL(v));
/*for(auto i:v){
printf("%lld %d %d\n",i.F,i.S.F,i.S.S);
}*/
if(v[0].S==v[1].S){
printf("%lld",v[2].F);
}
else{
printf("%lld",v[1].F);
}
}
| #include <iostream>
#include <string>
#include <stdio.h>
#include <math.h>
#include <queue>
#include <algorithm>
#include <utility>
#include <vector>
#include <tuple>
#include <numeric>
using namespace std;
int main(int argc, char* argv[]){
int n;
cin>>n;
vector<pair<long long, long long>>a(n);
vector<pair<long long, long long>>b(n);
for(int i=0;i<n;i++){
pair<long long, long long> p;
long long f,s;
cin>>f>>s;
p = make_pair(f,i);
a[i] = p;
p = make_pair(s,i);
b[i] = p;
}
sort(a.begin(),a.end());
sort(b.begin(),b.end());
vector<long long> ans;
vector<pair<int,int>> ans_pair;
pair<int,int> p;
int f,s;
ans.push_back(abs(a[0].first-a[a.size()-1].first));
f = min(a[0].second, a[a.size()-1].second);
s = max(a[0].second, a[a.size()-1].second);
p = make_pair(f,s);
ans_pair.push_back(p);
ans.push_back(abs(a[1].first-a[a.size()-1].first));
f = min(a[1].second, a[a.size()-1].second);
s = max(a[1].second, a[a.size()-1].second);
p = make_pair(a[1].second, a[a.size()-1].second);
ans_pair.push_back(p);
ans.push_back(abs(a[0].first-a[a.size()-2].first));
f = min(a[0].second, a[a.size()-2].second);
s = max(a[0].second, a[a.size()-2].second);
p = make_pair(a[0].second, a[a.size()-2].second);
ans_pair.push_back(p);
// ans.push_back(abs(b[0].first-b[b.size()-1].first));
// ans.push_back(abs(b[1].first-b[b.size()-1].first));
// ans.push_back(abs(b[0].first-b[b.size()-2].first));
int idx_i, idx_j;
idx_i = min(b[0].second,b[n-1].second);
idx_j = max(b[0].second,b[n-1].second);
bool flag = true;
for(int i=0;i<3;i++){
if(ans_pair[i].first==idx_i&&ans_pair[i].second==idx_j){
ans[i] = max(ans[i],abs(b[0].first-b[n-1].first));
flag = false;
}
}
if (flag==true){
ans.push_back(abs(b[0].first-b[b.size()-1].first));
}
idx_i = min(b[1].second,b[n-1].second);
idx_j = max(b[1].second,b[n-1].second);
flag = true;
for(int i=0;i<3;i++){
if(ans_pair[i].first==idx_i&&ans_pair[i].second==idx_j){
ans[i] = max(ans[i],abs(b[1].first-b[n-1].first));
flag = false;
}
}
if (flag==true){
ans.push_back(abs(b[1].first-b[n-1].first));
}
idx_i = min(b[0].second,b[n-2].second);
idx_j = max(b[0].second,b[n-2].second);
flag = true;
for(int i=0;i<3;i++){
if(ans_pair[i].first==idx_i&&ans_pair[i].second==idx_j){
ans[i] = max(ans[i],abs(b[0].first-b[n-2].first));
flag = false;
}
}
if (flag==true){
ans.push_back(abs(b[0].first-b[n-2].first));
}
sort(ans.begin(),ans.end());
// for(int i=0;i<ans.size();i++){
// cout<<ans[i]<<endl;
// }
cout<<ans[ans.size()-2]<<endl;
}
|
// 問題の URL を書いておく
//
#include <bits/stdc++.h>
using namespace std;
//#define ENABLE_PRINT
#if defined(ENABLE_PRINT)
#define Print(v) \
do {\
cout << #v << ": " << v << endl; \
}while(0)
#define PrintVec(v) \
do {\
for(int __i = 0; __i < v.size(); ++__i) \
{ \
cout << #v << "[" << __i << "]: " << v[__i] << endl; \
}\
}while(0)
#else
#define Print(v) ((void)0)
#define PrintVec(v) ((void)0)
#endif
#define rep(i, n) for(int i = 0; i < (int)(n); ++i)
using ll = long long;
struct Vertex
{
vector<int> children;
ll score{0};
ll diff{0};
int depth{-1};
};
void fillDepth(vector<Vertex>& vs, int t, int depth)
{
vs[t].depth = depth;
for(auto v : vs[t].children)
{
if(vs[v].depth == -1)
{
fillDepth(vs, v, depth + 1);
}
}
}
int main(int, const char**)
{
int N;
cin >> N;
vector<pair<int, int>> edges(N - 1);
vector<Vertex> v(N);
rep(i, N - 1)
{
int a, b;
cin >> a >> b;
a--; b--;
edges[i] = {a, b};
v[a].children.push_back(b);
v[b].children.push_back(a);
}
fillDepth(v, 0, 0);
int Q;
cin >> Q;
rep(i, Q)
{
int t, e, x;
cin >> t >> e >> x;
e--;
int s, b;
if(t == 1)
{
s = edges[e].first;
b = edges[e].second;
}
else
{
s = edges[e].second;
b = edges[e].first;
}
if(v[s].depth > v[b].depth)
{
v[s].diff += x;
}
else
{
v[b].diff -= x;
v[0].diff += x;
}
}
#if 0
rep(i, N)
{
printf("v[%d]:\n", i);
for(auto c : v[i].children)
{
printf(" %d\n", c);
}
}
#endif
queue<int> q;
q.push(0);
while(!q.empty())
{
auto t = q.front();
q.pop();
auto& tv = v[t];
for(auto n : tv.children)
{
if(v[n].depth <= tv.depth) continue;
v[n].diff += tv.diff;
q.push({n});
}
}
rep(i, N)
{
cout << v[i].diff << endl;
}
return 0;
}
| #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using ld = long double;
template <typename T1, typename T2>
void mmin(T1& a, const T2& b) {
if (b < a) a = b;
}
template <typename T1, typename T2>
void mmax(T1& a, const T2& b) {
if (b > a) a = b;
}
const int MAXN = 2e5 + 20;
int n, a[MAXN], b[MAXN], dfn[MAXN], ext[MAXN], clk = 0;
list<int> g[MAXN];
ll dif[MAXN], val[MAXN];
void dfs(int u, int fa) {
dfn[u] = ++clk;
for (int v : g[u])
if (v != fa) dfs(v, u);
ext[u] = clk;
}
void proc1(int e, ll x) {
int u = a[e], v = b[e];
// from u, without v
if (dfn[u] <= dfn[v] && dfn[v] <= ext[u]) {
// u -> v
dif[1] += x;
dif[n + 1] -= x;
dif[dfn[v]] -= x;
dif[ext[v] + 1] += x;
} else {
// v -> u
dif[dfn[u]] += x;
dif[ext[u] + 1] -= x;
}
}
void proc2(int e, ll x) {
int u = a[e], v = b[e];
// from v, without u
if (dfn[u] <= dfn[v] && dfn[v] <= ext[u]) {
// u -> v
dif[dfn[v]] += x;
dif[ext[v] + 1] -= x;
} else {
// v -> u
dif[1] += x;
dif[n + 1] -= x;
dif[dfn[u]] -= x;
dif[ext[u] + 1] += x;
}
}
void finalize() {
for (int i = 1; i <= n; i++) val[i] = val[i - 1] + dif[i];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 1; i < n; i++) {
cin >> a[i] >> b[i];
g[a[i]].push_back(b[i]);
g[b[i]].push_back(a[i]);
}
dfs(1, 0);
int q;
cin >> q;
while (q--) {
int t, e;
ll x;
cin >> t >> e >> x;
if (t == 1) proc1(e, x);
if (t == 2) proc2(e, x);
}
finalize();
for (int i = 1; i <= n; i++) cout << val[dfn[i]] << "\n";
}
|
#include<bits/stdc++.h>
using namespace std;
#define all(v) ((v).begin()), ((v).end())
#define allr(v) ((v).rbegin()), ((v).rend())
#define sz(v) ((int)((v).size()))
#define ll long long
#define pb push_back
#define fIO ios_base::sync_with_stdio(0);cin.tie(0);
const ll oo = 1e8;
const double pi = 3.1415926535897;
const double EPS = (1e-7);
int dcmp(double x, double y) { return fabs(x-y) <= EPS ? 0 : x < y ? -1 : 1; }
typedef vector<int> vi;
typedef vector<pair<int,int>> vii;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector< vi > vvi;
typedef vector< vd > vvd;
typedef vector<string> vs;
typedef pair<int, int> pii;
/**
* PLEASEEE READDD PROBLEEEM CARFULLY ****
* Always check for OVERFLOW ll vs int
* Always check for array bounds
* Check for function return
* READ OUTPUT SECTION AGAIN!!!
**/
struct edge{
int from, to, w;
edge(){}
edge(int x, int y, int we): from(x), to(y), w(we){}
bool operator < (const edge &rhs) const{
return w > rhs.w;
}
};
int main(){
fIO
// freopen("", "r", stdin);
// freopen("", "w", stdout);
int a, b, c, d;
cin >> a >> b >> c >> d;
cout << a * d - b * c;
}
| #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define br "\n"
#define sp " "
#define pie 3.141592653589793238462643383279
#define fo(i, n) for(int i=0;i<n;i++)
#define rfo(i, n) for(int i=n-1;i>=0;i--)
#define forl(i,a,n) for(int i=a;i<n;i++)
#define forr(i,a,n) for(int i=n-1;i>=a;i--)
#define tr(it, x) for(auto it = x.begin(); it != x.end(); it++)
#define trr(it, x) for(auto it = x.rbegin(); it != x.rend(); it++)
#define deb(x) cout << #x << " = " << x << endl;
#define deb2(x, y) cout << #x << " = " << x << ", " << #y << " = " << y << endl
#define deba(i, a, n) fo(i, n){cout << a[i] << " ";}
#define popcount(x) __builtin_popcount(x) // count the number of one’s(set bits) in an integer.
#define popcountll(x) __builtin_popcountll(x)
#define clz(x) __builtin_clz(x) // count the leading zeros(binary representation) of the integer
#define clzll(x) __builtin_clzll(x)
#define ctz(x) __builtin_ctz(x) // count the trailing zeros(binary representation) of the integer
#define ctzll(x) __builtin_ctzll(x)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define gcd __gcd
#define lcm(a,b) (a*b)/gcd(a,b)
#define deci(n) fixed << setprecision(n)
#define max3(a,b,c) max(max(a,b),c)
#define min3(a,b,c) min(min(a,b),c)
#define max4(a,b,c,d) max(a,max3(b,c,d))
#define min4(a,b,c,d) min(a,min3(b,c,d))
#define max5(a,b,c,d,e) max(max4(a,b,c,d),e)
#define min5(a,b,c,d,e) min(min4(a,b,c,d),e)
#define p0(a) cout << a << " "
#define p1(a) cout << a << br
#define p2(a, b) cout << a << " " << b << br
#define p3(a, b, c) cout << a << " " << b << " " << c << br
#define p4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << br
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define sort(x,n) sort(x, x+n)
#define rsort(x,n) sort(x, x+n, greater<int>())
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef pair<double, double> pd;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef priority_queue<int, vector<int>, greater<int>> minpq;
typedef priority_queue<int> pq;
typedef unordered_map<int,int> umii;
typedef map<int,int> mii;
typedef set<int> si;
typedef deque<int> di;
typedef queue<int> qi;
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 midofthree(int a,int b,int c)
{
if((a<=b and a>=c) or (a<=c and a>=b))
return a;
else if((b>=a and b<=c) or (b<=a and b>=c))
return b;
return c;
}
ll fact(int n){
if(n==1 || n==0){
return 1;
}
else{
return n*fact(n-1);
}
}
bool isPrime(int n)
{
if (n <= 1)
return false;
for (int i = 2; i*i <= n; i++)
if (n % i == 0)
return false;
return true;
}
const int MOD = 1e9 + 7;
int mod=MOD;
/* ========== YOUR CODE HERE ========= */
void solution() {
int a,b,c,d;
cin>>a>>b>>c>>d;
cout<< a*d-b*c;
}
/* ========== YOUR CODE HERE ========= */
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
srand(chrono::high_resolution_clock::now().time_since_epoch().count());
int t = 1;
// cin>>t;
while(t--) {
solution();
}
return 0;
}
// By Ashwani Kumar, Indian Institute Of Technology(IIT), Guwahati. |
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
cin >> s;
s.push_back(s[0]);
s.erase(s.begin());
cout << s << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
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;
const int INF = 1001001001;
const ll LINF = 1002003004005006007ll;
void solve(){
ll n;cin >> n;
rep(i,pow(10,5)){
if((2*n)<=(i*(i+1))){
cout << i << endl;
return;
}
}
return;
}
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
cout << fixed << setprecision(16);
solve();
return 0;
} |
#include <bits/stdc++.h>
#define rep(i, l, r) for(int i = (l); i <= (r); i++)
#define per(i, r, l) for(int i = (r); i >= (l); i--)
#define mem(a, b) memset(a, b, sizeof a)
#define For(i, l, r) for(int i = (l), i##e = (r); i < i##e; i++)
#define pb push_back
#define eb emplace_back
#define upd(a, b) (a = min(a, b))
using namespace std;
const int N = 1e5 + 5;
int n, mi[128], f[N];
char s[N];
int main() {
mem(f, 63), mem(mi, 63);
#ifdef local
// freopen(".in", "r", stdin);
#endif
scanf("%d%s", &n, s + 1);
mi[s[1]] = 0;
rep(i, 2, n) {
rep(j, 'a', 'z') if(j ^ s[i]) upd(f[i], mi[j] + 1);
upd(mi[s[i + 1]], f[i]);
}
if(f[n] < 0x3f3f3f3f) cout << f[n];
else puts("-1");
return 0;
} | #include <bits/stdc++.h>
#define int int64_t
using namespace std;
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
string s;
int n; cin >> n >> s;
if(s[0] != s[n - 1])
return cout << "1", 0;
for(int i = 1; i < n - 2; i++) {
if(s[i] != s[0] and s[i + 1] != s[0])
return cout << "2", 0;
}
cout << "-1";
} |
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T, null_type,less<T>, rb_tree_tag,tree_order_statistics_node_update>;
// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")
/* // Ordered Set
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T>
typedef tree<T, null_type,less<T>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
#define os_find(k) find_by_order(k)
#define os_order(k) order_of_key(k)
*/
typedef long long int ll;
typedef unsigned long long int ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<long long> vll;
#define f0(i,a,b) for(int i=a;i<b;i++)
#define f1(i,a,b) for(int i=a;i<=b;i++)
#define f2(i,a,b) for(int i=a;i>b;i--)
#define f3(i,a,b) for(int i=a;i>=b;i--)
#define all(a) a.begin(),a.end()
#define cntleadz(x) __builtin_clz(x) // or add ll at the end for long long
#define cnttrailz(x) __builtin_ctx(x)
#define cntpop(x) __builtin_popcount(x)
#define binparity(x) __builtin_parity(x)
#define pb push_back
#define pii pair<int,int>
#define int long long
#define fi first
#define se second
// #define mod 1000000007
#define mod 998244353
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define make_graph(k) int x,y; f0(i,0,k){cin>>x>>y; adj[x].pb(y); adj[y].pb(x);}
#define test int t;cin>>t;while(t--)
ll binExp(ll x,ll n)
{
ll res=1;
while(n)
{
if(n&1) res=(res*x)%mod;
x=(x*x)%mod;
n>>=1;
}
return res;
}
// ll fact[200001];
// ll ncr(int n,int r) {return (n>=r?(fact[n]*modInv(fact[r]))%mod*modInv(fact[n-r])%mod:0);}
ll modInv(ll i) {return binExp(i,mod-2);}
ll add(ll a, ll b) {ll res = a+b; if(res >= mod) res -= mod; return res;}
ll mul(ll a, ll b) {ll res = a*b; res %= mod; return res;}
char mat[5001][5001];
int dp[5001][5001];
void solve()
{
int h,w,k;
cin>>h>>w>>k;
f1(i,1,h) f1(j,1,h) {mat[i][j] = '.';dp[i][j] = 0;}
dp[1][1] = binExp(3,h*w-k);;
int a,b;
char c;
f0(_,0,k)
{
cin>>a>>b>>c;
mat[a][b] = c;
}
int by3 = modInv(3);
f1(i,1,h) f1(j,1,w)
{
if(i!=1 && mat[i-1][j] != 'R')
{
if(mat[i-1][j] == 'X' || mat[i-1][j] == 'D')
dp[i][j] = add(dp[i][j], dp[i-1][j]);
else
dp[i][j] = add(dp[i][j], mul(2*dp[i-1][j], by3));
}
if(j!=1 && mat[i][j-1] != 'D')
{
if(mat[i][j-1] == 'X' || mat[i][j-1] == 'R')
dp[i][j] = add(dp[i][j], dp[i][j-1]);
else
dp[i][j] = add(dp[i][j], mul(2*dp[i][j-1], by3));
}
// if(i==2&&j==1)
// {
// cout<<dp[i][j]<<"----\n";
// }
}
// f1(i,1,h)
// {
// f1(j,1,w) cout<<mat[i][j]<<' '; cout<<'\n';
// }
// f1(i,1,h)
// {
// f1(j,1,w) cout<<dp[i][j]<<' '; cout<<'\n';
// }
cout<<dp[h][w];
}
signed main()
{
fast
// clock_t start,end;
#ifndef ONLINE_JUDGE
freopen("inputf.txt","r",stdin);
freopen("outputf.txt","w",stdout);
#endif
// start = clock();
// fact[0] = 1;
// f1(i,1,200000) fact[i] = mul(fact[i-1], i);
// test
solve();
// end = clock();
// double time_taken = double(end-start) / double(CLOCKS_PER_SEC);
// cout<<time_taken<<" sec";
} | #include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int INF = 1e9;
const long long INFLL = 1e18;
using ll = long long;
char RSP(char i, char j){
if(i == 'S' && j == 'P') return 'S';
if(i == 'R' && j == 'S') return 'R';
if(i == 'P' && j == 'R') return 'P';
if(i == 'S' && j == 'R') return 'R';
if(i == 'R' && j == 'P') return 'P';
if(i == 'P' && j == 'S') return 'S';
return i;
}
int main(){
int n, k;
cin >> n >> k;
string s;
cin >> s;
if(n % 2 == 1){
n *= 2;
for(int i = 0; i < n/2; i++) s.push_back(s[i]);
}
for(int i = 0; i < k; i++){
string ss = "";
for(int j = 0; j < n/2; j++){
ss.push_back(RSP(s[2*j], s[2*j+1]));
}
for(int j = n/2; j < n; j++) ss.push_back(ss[j-n/2]);
for(int j = 0; j < n; j++) s[j] = ss[j];
// cout << s << endl;
}
cout << s[0] << endl;
return 0;
} |
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
long powerm(long long a, long long n, long long m)
{
if (n==1) return a%m;
if (n==0) return 1;
if (n%2) return (a*powerm((a*a)%m,n/2,m))%m;
return powerm((a*a)%m,n/2,m);
}
void amain()
{
long long n; cin >> n;
long long m; cin >> m;
long long x;
x=powerm(10,n,m*m);
//cout << x << endl;
cout << x/m << endl;
return;
}
int main()
{
amain();
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;
long long n, m, a;
long long pow(long long a, long long n, long long m)
{
if(n == 0)
return 1 % m;
long long p = pow(a, n/2, m);
p = p*p%m;
if(n & 1)
p = p*a%m;
return p;
}
int main()
{
scanf("%lld %lld", &n, &m);
long long p = pow(10, n, m*m);
p /= m;
cout << p%m << endl;
return 0;
}
|
#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
inline int my_getchar_unlocked(){
static char buf[1048576];
static int s = 1048576;
static int e = 1048576;
if(s == e && e == 1048576){
e = fread_unlocked(buf, 1, 1048576, stdin);
s = 0;
}
if(s == e){
return EOF;
}
return buf[s++];
}
inline void rd(int &x){
int k;
int m=0;
x=0;
for(;;){
k = my_getchar_unlocked();
if(k=='-'){
m=1;
break;
}
if('0'<=k&&k<='9'){
x=k-'0';
break;
}
}
for(;;){
k = my_getchar_unlocked();
if(k<'0'||k>'9'){
break;
}
x=x*10+k-'0';
}
if(m){
x=-x;
}
}
inline int rd_int(void){
int x;
rd(x);
return x;
}
struct MY_WRITER{
char buf[1048576];
int s;
int e;
MY_WRITER(){
s = 0;
e = 1048576;
}
~MY_WRITER(){
if(s){
fwrite_unlocked(buf, 1, s, stdout);
}
}
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
MY_WRITER_VAR.s = 0;
}
MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
my_putchar_unlocked(a);
}
inline void wt_L(const char c[]){
int i=0;
for(i=0;c[i]!='\0';i++){
my_putchar_unlocked(c[i]);
}
}
int main(){
if(rd_int()%2){
wt_L("Black");
wt_L('\n');
}
else{
wt_L("White");
wt_L('\n');
}
return 0;
}
// cLay varsion 20201101-1
// --- original code ---
// {
// wt(if[rd_int()%2, "Black", "White"]);
// }
| #include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); i++)
#define loop(i, a, n) for(int i = (a); i < (n); i++)
#define all(x) x.begin(), x.end()
#ifdef _DEBUG
#define disp(x) cout << #x << " : " << x << endl
#else
#define disp(x)
#endif
using namespace std;
using ll = int64_t;
int main(){
int n; cin >> n;
if(n%2) cout << "Black" << endl;
else cout << "White" << endl;
return 0;
}
|
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
#include <list>
#include <cassert>
#include <climits>
#include <bitset>
#include <chrono>
#include <random>
using namespace std;
#define PB push_back
#define MP make_pair
#define SZ(v) ((int)(v).size())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)
#define REPE(i,n) FORE(i,0,n)
#define FORSZ(i,a,v) FOR(i,a,SZ(v))
#define REPSZ(i,v) REP(i,SZ(v))
std::mt19937 rnd((int)std::chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll n;
ll calcsum(ll a, ll b) {
ll x = a + b, y = b - a + 1;
if (x % 2 == 0) x /= 2; else { assert(y % 2 == 0); y /= 2; }
if (x > LLONG_MAX / y) return LLONG_MAX; else return x * y;
}
ll solve() {
ll lo = 0, hi = n + 1;
while (lo + 1 < hi) {
ll mi = lo + (hi - lo) / 2;
ll a = 1, b = n + 1 - mi, c = n + 1;
ll d = calcsum(a, b);
if (c >= d) hi = mi; else lo = mi;
}
return hi;
}
void run() {
scanf("%lld", &n);
printf("%lld\n", solve());
}
int main() {
run();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main(){
ll n,max=0,max2=0,t,idx=1,idx2=1;
cin>>n;
vector<ll> a(1<<n);
for(ll i=0; i<(1<<(n-1)); i++){
cin>>t;
if(t>max){
max=t;
idx=i+1;
// cout << max << " "<< idx << endl;
}
}
for(ll i=0; i<(1<<(n-1)); i++){
cin>>t;
if(t>max2){
max2=t;
idx2=i+1+(1<<(n-1));
}
}
if(max<max2) cout << idx << endl;
else cout << idx2 << endl;
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
t=0; char ch=getchar(); int f=1;
while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
do { (t*=10)+=ch-'0'; ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f;
}
const int maxn=(4e5)+10;
int n,fa[maxn],dis[maxn],dep[maxn];
vector<int> g[maxn];
int lg[maxn];
int mx,son[maxn];
void dfs(int u,int p) {
dis[u]=dis[p]+1; fa[u]=p;
for (int v : g[u]) if (v!=p) dfs(v,u);
if (!mx||dis[u]>dis[mx]) mx=u;
}
int st[maxn][20];
int dfn[maxn],tot,idx,euler[maxn],pos[maxn],ans[maxn];
void dfs2(int u,int p) {
dfn[++idx]=u;
for (int v : g[u]) if (v!=p&&v!=son[u]) dfs2(v,u);
if (son[u]) dfs2(son[u],u);
}
int MN(int x,int y) {
if (dep[x]<dep[y]) return x;
return y;
}
int lca(int x,int y) {
x=pos[x],y=pos[y];
if (x>y) swap(x,y);
int j=lg[y-x+1];
return MN(st[x][j],st[y-(1<<j)+1][j]);
}
int DIS(int x,int y) {
return dep[x]+dep[y]-2*dep[lca(x,y)];
}
void pre(int u,int p) {
dep[u]=dep[p]+1;
euler[++tot]=u; pos[u]=tot;
for (int v : g[u]) if (v!=p) {
pre(v,u);
euler[++tot]=u;
}
}
int main() {
//freopen("1.txt","r",stdin);
for (int i=2;i<maxn;i++) lg[i]=lg[i>>1]+1;
read(n); int x,y,z;
for (int i=1;i<n;i++) {
read(x),read(y);
g[x].push_back(y);
g[y].push_back(x);
}
pre(1,0);
for (int i=1;i<=tot;i++) st[i][0]=euler[i];
for (int j=1;j<=19;j++)
for (int i=1;i+(1<<j)-1<=tot;i++)
st[i][j]=MN(st[i][j-1],st[i+(1<<(j-1))][j-1]);
mx=0; dfs(1,0); x=mx;
mx=0; dfs(x,0); y=mx;
z=y;
while (fa[z]) son[fa[z]]=z,z=fa[z];
dfs2(x,0);
ans[x]=1;
for (int i=2;i<=n;i++)
ans[dfn[i]]=ans[dfn[i-1]]+DIS(dfn[i],dfn[i-1]);
for (int i=1;i<=n;i++) printf("%d ",ans[i]); puts("");
return 0;
}
/*
REMEMBER:
1. Think TWICE, Code ONCE!
Are there any counterexamples to your algo?
2. Be careful about the BOUNDARIES!
N=1? P=1? Something about 0?
3. Do not make STUPID MISTAKES!
Array size? Integer overflow? Time complexity? Memory usage? Precision error?
*/ | //#pragma GCC target("avx,avx2")
//#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
//#pragma GCC optimize("Ofast")
#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 r(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()
{
ci>>n>>m>>x>>y;
char a[n][m];
f(i,0,n) f(j,0,m) ci>>a[i][j];
x--;y--;
ll i=y;
c=0;
while(a[x][i]=='.' and i>=0)
{
c++;
i--;
}
i=y+1;
while(a[x][i]=='.' and i<m)
{
c++;
i++;
}
i=x-1;
while(a[i][y]=='.' and i>=0)
{
c++;
i--;
}
i=x+1;
while(a[i][y]=='.' and i<n)
{
c++;
i++;
}
co<<c<<endl;
}
int main()
{
//#ifdef LOCAL
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
//#endif
auto start_time = clock();
cerr << setprecision(3) << fixed;
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;
#define rep(i, n) for (int i=0; i<(int)(n); i++)
int main() {
int N;
long K;
cin >> N >> K;
vector<vector<long long>> dp(4,vector<long long>(3*N+4));
dp.at(0).at(0) = 1;
rep(i,3) {
rep(j,i*N+1) {
dp.at(i+1).at(j+1) += dp.at(i).at(j);
dp.at(i+1).at(j+N+1) -= dp.at(i).at(j);
}
rep(j,(i+1)*N) {
dp.at(i+1).at(j+1) += dp.at(i+1).at(j);
}
}
int sum;
for(int i=3; i<=3*N; i++) {
if(K<=dp.at(3).at(i)) {
sum = i;
break;
}
else {
K -= dp.at(3).at(i);
}
}
rep(i,N) {
int ans_1 = i+1;
int jmin=max(1,sum-ans_1-N);
int jmax=min(N,sum-ans_1-1);
if(jmin>jmax) continue;
if(K>(jmax-jmin+1)) {
K -= jmax-jmin+1;
continue;
}
int ans_2=jmin+K-1;
int ans_3=sum-ans_1-ans_2;
cout << ans_1 << " " << ans_2 << " " << ans_3 << endl;
break;
}
} | //by szh
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define debug(x) cerr<<#x<<"="<<x<<endl
#define pq priority_queue
#define inf 0x3f
#define rep(i,a,b) for (int i=a;i<(b);i++)
#define MP make_pair
#define SZ(x) (int(x.size()))
#define ll long long
#define mod 1000000007
#define ALL(x) x.begin(),x.end()
void inc(int &a,int b) {a=(a+b)%mod;}
void dec(int &a,int b) {a=(a-b+mod)%mod;}
int lowbit(int x) {return x&(-x);}
ll p0w(ll base,ll p) {ll ret=1;while(p>0){if (p%2ll==1ll) ret=ret*base%mod;base=base*base%mod;p/=2ll;}return ret;}
const int maxn=3e6+10;
int N;
ll K;
ll f[maxn];
ll getf(int x) {
if (x<2||x>N+N) return 0;
ll a=max(1,x-N),b=min(N,x-1);
return b-a+1;
}
int main() {
// freopen("input.txt","r",stdin);
std::ios::sync_with_stdio(false);cin.tie();
cin>>N>>K;
memset(f,0,sizeof(f));
rep(i,3,3*N+1) {
f[i]=f[i-1]+getf(i-1);
if (i-N-1>=2) f[i]-=getf(i-N-1);
// debug(f[i]);
}
int cur=2;
while (1) {
cur++;
if (K<=f[cur]) break;
else K-=f[cur];
}
debug(K);
//sum is cur
int b=0;
while (1) {
b++;
if (K<=getf(cur-b)) break;
else K-=getf(cur-b);
}
//first ele is b
debug(cur);
cout<<b<<" "<<max(0,cur-b-N-1)+K<<" "<<cur-b-max(0,cur-b-N-1)-K;
return 0;
}
|
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int read()
{
int a=0,f=1,c=getchar();
while(!isdigit(c))
{
if(c=='-')f=-1;
c=getchar();
}
while(isdigit(c))
{
a=a*10+c-'0';
c=getchar();
}
return a*f;
}
const int N=1e5+10;
const int P=1e9+7;
ll mod(ll x){return (x%P+P)%P;}
int n;
int a[N];
ll f[2][N],g[2][N];
int main()
{
// freopen("1.in","r",stdin);
n=read();
for(int i=1;i<=n;++i)a[i]=read();
f[0][1]=1;
g[0][1]=a[1];
for(int i=2;i<=n;++i)
{
f[0][i]=mod(f[0][i-1]+f[1][i-1]);
f[1][i]=f[0][i-1];
g[0][i]=mod(g[0][i-1]+g[1][i-1]+a[i]*f[0][i]);
g[1][i]=mod(g[0][i-1]-a[i]*f[1][i]);
}
printf("%lld\n",mod(g[0][n]+g[1][n]));
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define each(e, v) for(auto &e: v)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
//const int MOD = 1000000007;
const int MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};
struct io_setup{
io_setup(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << fixed << setprecision(15);
}
} io_setup;
template<int mod>
struct Mod_Int{
int x;
Mod_Int() : x(0) {}
Mod_Int(ll y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
Mod_Int &operator += (const Mod_Int &p){
if((x += p.x) >= mod) x -= mod;
return *this;
}
Mod_Int &operator -= (const Mod_Int &p){
if((x += mod - p.x) >= mod) x -= mod;
return *this;
}
Mod_Int &operator *= (const Mod_Int &p){
x = (int) (1LL * x * p.x % mod);
return *this;
}
Mod_Int &operator /= (const Mod_Int &p){
*this *= p.inverse();
return *this;
}
Mod_Int &operator ++ () {return *this += Mod_Int(1);}
Mod_Int operator ++ (int){
Mod_Int tmp = *this;
++*this;
return tmp;
}
Mod_Int &operator -- () {return *this -= Mod_Int(1);}
Mod_Int operator -- (int){
Mod_Int tmp = *this;
--*this;
return tmp;
}
Mod_Int operator - () const {return Mod_Int(-x);}
Mod_Int operator + (const Mod_Int &p) const {return Mod_Int(*this) += p;}
Mod_Int operator - (const Mod_Int &p) const {return Mod_Int(*this) -= p;}
Mod_Int operator * (const Mod_Int &p) const {return Mod_Int(*this) *= p;}
Mod_Int operator / (const Mod_Int &p) const {return Mod_Int(*this) /= p;}
bool operator == (const Mod_Int &p) const {return x == p.x;}
bool operator != (const Mod_Int &p) const {return x != p.x;}
Mod_Int inverse() const {
assert(*this != Mod_Int(0));
return pow(mod-2);
}
Mod_Int pow(ll k) const{
Mod_Int now = *this, ret = 1;
for(; k; k >>= 1, now *= now){
if(k&1) ret *= now;
}
return ret;
}
friend ostream &operator << (ostream &os, const Mod_Int &p){
return os << p.x;
}
friend istream &operator >> (istream &is, Mod_Int &p){
ll a;
is >> a;
p = Mod_Int<mod>(a);
return is;
}
};
using mint = Mod_Int<MOD>;
int main(){
int H, W, K;
cin >> H >> W >> K;
vector<vector<mint>> dp(H, vector<mint>(W, 0));
vector<vector<char>> a(H, vector<char>(W, '0'));
rep(i, K){
int x, y;
char c;
cin >> x >> y >> c; x--, y--;
a[x][y] = c;
}
dp[0][0] = 1;
mint p = mint(2)/mint(3);
rep(i, H){
rep(j, W){
if((i < H-1 || j < W-1) && a[i][j] == '0') dp[i][j] *= p;
if(a[i][j] != 'R' && i+1 < H){
dp[i+1][j] += dp[i][j];
}
if(a[i][j] != 'D' && j+1 < W){
dp[i][j+1] += dp[i][j];
}
}
}
mint ans = dp[H-1][W-1];
ans *= mint(3).pow(H*W-K);
cout << ans << '\n';
} |
#include<bits/stdc++.h>
using namespace std;
#define N (1000000000+7)
//#define N 998244353
#define INF 1e18
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
const int inf = (int)1e9;
using P = pair<int,int>;
ll gcd(ll a, ll b) {
if (b > a) {
ll tmp = b;
b = a;
a = tmp;
}
if(b==0){
return a;
}
if (a%b == 0)return b;
else return gcd(b, a%b);
}
template<typename T>
struct BIT{
private:
vector<T> array;
int length;
public:
BIT(int _n=0) : array(_n+1),length(_n){}
T sum(int i){
T s=0;
while(i>0){
s+=array[i];
i -= i&(-i);
}
return s;
}
T sum(int i,int j){
T ret1 = sum(i-1);
T ret2 = sum(j);
return ret2-ret1;
}
void add(int i,T x){
while(i<=length){
array[i]+=x;
i += i&(-i);
}
}
};
ll row[200010];
ll col[200010];
vector<ll> v[200010];
int main(void){
ll h,w,m;
cin>>h>>w>>m;
for(int i=1;i<=h;i++)row[i] = w+1;
for(int i=1;i<=w;i++)col[i] = h+1;
ll ans = 0;
for(int i=0;i<m;i++){
ll x,y;
cin>>x>>y;
row[x] = min(row[x],y);
col[y] = min(col[y],x);
}
ll Mw = row[1];
ll Mh = col[1];
for(ll i=1;i<Mh;i++){
ans += row[i]-1;
v[row[i]-1].push_back(i);
}
for(ll i=1;i<Mw;i++){
ans += col[i]-1;
}
//cout<<ans<<endl;
BIT<ll> bit = BIT<ll>(h+1);
ll res = 0;
for(ll i=1;i<=w;i++){
if(i<Mw){
bit.add(1,1LL);
bit.add(col[i],-1LL);
}
//cout<<col[i]<<" ";
for(int j=0;j<v[i].size();j++){
ll t = bit.sum(v[i][j]);
res += bit.sum(v[i][j]);
//cout<<t<<" ";
}
////cout<<endl;
}
cout<<ans-res<<endl;
return 0;
}
| /**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author NikuKyabe
*/
#include <iostream>
#include <fstream>
#include <bits/stdc++.h>
#define int long long
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define REV(i, a, b) for (int i = (b) - 1; i >= (a); --i)
#define CLR(a, b) memset((a), (b), sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF 1001001001001001001l
#define MOD 1000000007
#define fcout cout << fixed << setprecision(12)
using namespace std;
typedef pair<int, int> P;
template<class T>
using M =vector<vector<T>>;
class BHydrate {
public:
void solve(std::istream& cin, std::ostream& cout) {
int a,b,c,d;
cin >> a >> b >> c >> d;
if(c * d - b <= 0){
cout << -1 << endl;
}
else{
double t = (double)a / (c * d - b);
cout << (int)ceil(t) << endl;
}
}
};
signed main() {
BHydrate solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
solver.solve(in, out);
return 0;
}
|
// #pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#include <bits/stdc++.h>
#define ll long long int
#define vi vector<int>
#define vvi vector<vector<int>>
#define vll vector<long long>
#define vs vector<string>
#define vc vector<char>
#define vb vector<bool>
#define forn(i, s, n) for(ll i=(ll)s; i<(ll)(n); i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define pll pair<long long int, long long int>
#define pii pair<int, int>
#define lld long double
#define F first
#define S second
#define PI 3.141592653589793238
#define prec(n) fixed<<setprecision(n)
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#define itsval(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); itval(_it, args); }
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
void itval(istream_iterator<string> it) {}
template<typename T, typename... Args>
void itval(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
itval(++it, args...);
}
const ll MOD = 1e9 + 7;
template <typename T>
inline void print(T x) {cout << x << "\n";}
template <typename T>
inline void printvec(T x) {for (auto a : x)cout << a << ' '; cout << '\n';}
// -----------------------------------------------------------------------
struct custom {
bool operator()(const pair<int, pii> &p1, const pair<int, pii> &p2)const {
if (p1.F == p2.F)
return p1.S.F > p2.S.F;
return p1.F > p2.F;
}
};
// Calculate a^b % MOD -------------------------------------------------
ll get_pow(ll a, ll b, ll M = MOD) {
ll res = 1;
while (b) {
if (b & 1) res = (res * a) % M;
a = (a * a) % M;
b >>= 1;
}
return res;
}
// ---------------------------------------------------------------------
const ll N = 3e5 + 5, inf = 2e9;
std::vector<pii> adj[N];
int a[502][502], b[502][502], ind[502][502], dist[N];
void solve()
{
int R, C;
cin >> R >> C;
forn(i, 1, R + 1)
{
forn(j, 1, C)
{
cin >> a[i][j];
}
}
forn(i, 1, R)
{
forn(j, 1, C + 1)
cin >> b[i][j];
}
forn(i, 1, R + 1)
{
forn(j, 1, C + 1) {
ind[i][j] = j + C * i;
dist[ind[i][j]] = inf;
}
}
forn(i, 1, R + 1)
{
forn(j, 1, C + 1)
{
if (j < C)
{
adj[ind[i][j]].pb({a[i][j], ind[i][j + 1]});
}
if (j > 1)
{
adj[ind[i][j]].pb({a[i][j - 1], ind[i][j - 1]});
}
if (i < R)
{
adj[ind[i][j]].pb({b[i][j], ind[i + 1][j]});
}
forn(k, 1, i)
{
adj[ind[i][j]].pb({1 + k, ind[i - k][j]});
}
}
}
dist[ind[1][1]] = 0;
set<pii>s;
s.insert({0, ind[1][1]});
while (!s.empty())
{
pll t = *s.begin();
s.erase(s.begin());
for (auto x : adj[t.S])
{
if (dist[x.S] > x.F + t.F)
{
s.erase({dist[x.S], x.S});
dist[x.S] = x.F + t.F;
s.insert({dist[x.S], x.S});
}
}
}
cout << dist[ind[R][C]] << '\n';
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int test = 1;
clock_t z = clock();
//cin >> test;
forn(tes, 0, test)
{
solve();
}
debug("Total Time:%.4f\n", (double)(clock() - z) / CLOCKS_PER_SEC);
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define GODSPEED ios:: sync_with_stdio(0);cin.tie(0);cout.tie(0);cout<<fixed;cout<<setprecision(15);
#define f first
#define s second
#define newl cout<<"\n";
#define pb push_back
#define mset(a,x) memset(a,x,sizeof(a))
#define debv(a) for(auto it: a)cout<<it<<" ";newl;
#define deb1(a) cout<<a<<"\n";
#define deb2(a,b) cout<<a<<" "<<b<<"\n";
#define deb3(a,b,c) cout<<a<<" "<<b<<" "<<c<<"\n";
#define deb4(a,b,c,d) cout<<a<<" "<<b<<" "<<c<<" "<<d<<"\n";
#define uniq(a) a.resize(unique(a.begin(), a.end()) - a.begin());
#define all(a) a.begin(),a.end()
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
const ll N = 1e5+5;
const ll mod = 998244353;
const ll INF = 0x7f7f7f7f7f7f7f7f;
const int INFi = 0x7f7f7f7f;
ll n, k;
void solve(){
cin >> n >> k;
ll ans = 0;
for(int i = 2; i <= 2 * n; i++){
ll sum = k + i;
ll x = i;
if(sum > 2 * n or sum < 2) continue;
if(x > n) x -= 2 * (x - (n + 1));
if(sum > n) sum -= 2 * (sum - (n + 1));
ans += (x - 1) * (sum - 1);
}
deb1(ans)
}
int main(){
GODSPEED;
int test = 1;
//cin >> test;
for(int i = 1; i <= test; i++){
solve();
}
} |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 5e5+5;
const int INF = 0x3f3f3f3f;
int a[N],b[N];
inline int check1(int n){
int A0 = 0,B0 = 0;
vector <int> s,t;
for(int i = 1;i <= n;++i){
if(a[i] == 0) A0++,s.push_back(i);
if(b[i] == 0) B0++,t.push_back(i);
}
if(A0 != B0) return INF;
int res = 0;
for(int i = 0;i < s.size();++i){
if(s[i] == t[i]) res++;
}
return A0-res;
}
inline int check2(int n){
int A0 = 0,B0 = 0;
vector <int> s,t;
for(int i = n;i;--i){
if(a[i] == 0) A0++,s.push_back(i);
if(b[i] == 0) B0++,t.push_back(i);
}
if(A0 != B0) return INF;
int res = 0;
for(int i = 0;i < s.size();++i){
if(s[i] == t[i]) res++;
}
return A0-res;
}
int main() {
int n;
scanf("%d",&n);
for(int i = 1;i <= n;++i) scanf("%1d",&a[i]);
for(int i = 1;i <= n;++i) scanf("%1d",&b[i]);
int ans = INF;
ans = min(ans,check1(n));
ans = min(ans,check2(n));
printf("%d\n",ans == INF ?-1 :ans);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define f1 first
#define s2 second
#define fastio ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define debug(x) cerr << "[" << #x << "]: " << x << "\n";
using ll = long long;
using ld = long double;
using ii = pair<int, int>;
using pl = pair<ll, ll>;
ld const PI = 4*atan((ld)1);
const int MAX = 5e5 + 69;
int main()
{
fastio;
int n;
char s[MAX], t[MAX];
cin >> n;
cin >> s;
cin >> t;
int a = 0, b = 0;
for (int i = 0; i < n; ++i)
a += s[i] == '0', b += t[i] == '0';
if (a != b)
{
cout << -1 << '\n';
return 0;
}
vector<int> A, B;
for (int i = 0; i < n; ++i)
{
if (s[i] == '0') A.pb(i);
if (t[i] == '0') B.pb(i);
}
int res = 0;
for (int i = 0; i < (int)A.size(); ++i)
{
if (A[i] == B[i])
continue;
res++;
}
cout << res << '\n';
return 0;
}
|
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define cinf(n,x) for(int i=0;i<(n);i++)cin>>x[i];
#define ft first
#define sc second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(),(v).end()
#define LB(a,x) lb(all(a),x)-a.begin()
#define UB(a,x) ub(all(a),x)-a.begin()
#define mod 1000000007
//#define mod 998244353
#define FS fixed<<setprecision(15)
using namespace std;
typedef long long ll;
const double pi=3.141592653589793;
template<class T> using V=vector<T>;
using P=pair<ll,ll>;
typedef unsigned long long ull;
typedef long double ldouble;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline void out(T a){ cout << a << '\n'; }
void YN(bool ok){if(ok) cout << "Yes" << endl; else cout << "No" << endl;}
//void YN(bool ok){if(ok) cout << "YES" << endl; else cout << "NO" << endl;}
const ll INF=1e18;
const int mx=200005;
//arc110
int main(){
//オーバーフローは大丈夫ですか??
cin.tie(0);ios::sync_with_stdio(false);
ll ans=1;
ll n;
cin>>n;
map<ll,ll> mp;
for(ll i=2;i<=n;i++){
ll tmp=i;
map<ll,ll> mp2;
for(ll j=2;j*j<=i;j++){
while(tmp%j==0){
tmp/=j;
mp2[j]++;
}
}
if(tmp!=1){
mp2[tmp]++;
}
for(auto p:mp2){
chmax(mp[p.ft],p.sc);
}
}
for(auto p:mp){
rep(i,p.sc) ans*=p.ft;
}
ans++;
out(ans);
}
//ペナルティ出しても焦らない ACできると信じろ!!!
//どうしてもわからないときはサンプルで実験 何か見えてくるかも
//頭で考えてダメなら紙におこせ!!
/*
V,P(大文字)使用不可
乗算などの際にオーバーフローに注意せよ!
(適切にmodをとれ にぶたんで途中で切り上げろ)
制約をよく読め!
{
・全探索できるなら全探索しろ
・異常な制約ならそこに注目
}
stringの計算量(扱い)注意
コーナー注意!(特に数値が小さいものについては要検証)
N行出力のときは'¥n'
グリッド上では行先が範囲内におさまるかif文で確認(RE注意)
if文ではちゃんと比較演算子==を使え('='でもエラー出ない)
配列(vector)の大きさが0か1以上かで場合分けせよ(RE注意)
(vector<int> a(m)でm==0というものはできない)
modはなるべく最後に取れ!
doubleを扱うときには(abs)ではなく'fabs'!!!
dpするときは「配る」、「もらう」の両方を考えて最適な方を選べ
Segtreeで区間最大(max)を使うとき,単位元の設定をしっかり
(デフォルトは-INF,0の方が都合の良い場合は変更)
*/ | #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,N) for(ll i = 0; i < N; i++)
#define ld long double
int main(){
int n;
scanf("%d",&n);
vector<pair<pair<int,int>,int>> v(n);
rep(i,n) scanf("%d%d%d",&v.at(i).first.first,&v.at(i).first.second,&v.at(i).second);
vector<bool> ans(10002);
rep(i,n) {
if(!ans.at(v.at(i).first.second) && !ans.at(v.at(i).first.second + 1)){
ans.at(v.at(i).first.second) = true;
ans.at(v.at(i).first.second + 1) = true;
}
else v.at(i).second = 0;
if(v.at(i).first.second == 0 || v.at(i).first.second == 10000)
v.at(i).second = 0;
}
vector<pair<int,int>> kot(n);
rep(i,n){
if(v.at(i).second == 0){
kot.at(i).first = 0;
kot.at(i).second = 0;
continue;
}
else {
kot.at(i).first = v.at(i).first.second;
kot.at(i).second = v.at(i).first.second + 1;
}
while(v.at(i).second > (kot.at(i).second - kot.at(i).first) * 10000){
if(ans.at(kot.at(i).first - 1) == false && kot.at(i).first > 1){
kot.at(i).first--;
ans.at(kot.at(i).first) = true;
}
else if(ans.at(kot.at(i).second + 1) == false && kot.at(i).second < 9999){
kot.at(i).second++;
ans.at(kot.at(i).second) = true;
}
else break;
}
}
int index = 1;
rep(i,n){
if(v.at(i).second && kot.at(i).first > 0 && kot.at(i).second <= 9999) printf("%d %d %d %d\n",0,kot.at(i).first - 1,10000,kot.at(i).second);
else {
printf("%d %d %d %d\n",index,9999,index + 1,10000);
index++;
}
}
return 0;
}
|
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 110;
int n;
ll X, dp[N][N][N], ans = 1e18, a[N];
int main(){
cin >> n >> X;
for(int i = 1; i <= n; ++ i) cin >> a[i];
for(int t = 1; t <= n; ++ t){ //一共选t个
memset(dp, 0xcf, sizeof dp);
dp[0][0][0] = 0;
for(int i = 1; i <= n; ++ i){ //前i个物品中选
dp[i][0][0] = 0;
for(int k = 1; k <= min(i, t); ++ k){ //选k个
for(int j = 0; j < t; ++ j)
dp[i][k][j] = max(dp[i - 1][k][j], dp[i - 1][k - 1][((j - a[i]) % t + t) % t] + a[i]);
}
}
if(dp[n][t][X % t] >= 0) ans = min(ans, (X - dp[n][t][X % t]) / t);
}
cout << ans;
return 0;
} | #include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double db;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define pll pair<ll,ll>
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define ub(v,val) upper_bound(v.begin(),v.end(),val)
#define np(str) next_permutation(str.begin(),str.end())
#define lb(v,val) lower_bound(v.begin(),v.end(),val)
#define sortv(vec) sort(vec.begin(),vec.end())
#define rev(p) reverse(p.begin(),p.end());
#define v vector
#define pi 3.14159265358979323846264338327950288419716939937510
#define len length()
#define repc(i,s,e) for(ll i=s;i<e;i++)
#define fi first
#define se second
#define mset(a,val) memset(a,val,sizeof(a));
#define mt make_tuple
#define repr(i,n) for(i=n-1;i>=0;i--)
#define rep(i,n) for(i=0;i<n;i++)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define at(s,pos) *(s.find_by_order(pos))
#define set_ind(s,val) s.order_of_key(val)
long long int M = 1e9 + 7 ;
long long int inf = 9 * 1e18;
//CLOCK
ll begtime = clock();
#define time() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//CLOCK ENDED
ll n, m;
// modular exponentiation
ll binpow(ll val, ll deg)
{
if (deg < 0)
return 0;
if (!deg)
return 1 % M;
if (deg & 1)
return binpow(val, deg - 1) * val % M;
ll res = binpow(val, deg >> 1);
return (res * res) % M;
}
//binomial
ll modinv(ll n) {
return binpow(n, M - 2);
}
//DEBUGGING
vector<string> vec_splitter(string s)
{
s += ',';
vector<string> res;
while (!s.empty())
{
res.push_back(s.substr(0, s.find(',')));
s = s.substr(s.find(',') + 1);
}
return res;
}
void debug_out(
vector<string> __attribute__((unused)) args,
__attribute__((unused)) int idx,
__attribute__((unused)) int LINE_NUM) { cout << endl; }
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T)
{
if (idx > 0)
cout << " | ";
else
cout << "Line(" << LINE_NUM << ") ";
stringstream ss;
ss << H;
cout << args[idx] << " = " << ss.str();
debug_out(args, idx + 1, LINE_NUM, T...);
}
#ifndef ONLINE_JUDGE
#define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
#define debug(...) 42
#endif
// DEBUGGING ENDED
int main() {
// your code goes here
IOS;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ll i, j, t, k, x, y, z, N;
cin >> n >> x;
ll a[n];
rep(i, n) {
cin >> a[i];
// cout << a[i] << ' ';
}
ll mn = inf;
for (ll C = 1; C <= n; C++) {
ll dp[n + 5][n + 5][n + 5];
mset(dp, -1);
dp[0][0][0] = 0;
for (i = 0; i < n; i++)
{
for (j = 0; j <= n; j++) {
for (k = 0; k <= n; k++) {
if (dp[i][j][k] != -1) {
dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j][k]);
if (j < n) {
y = dp[i][j][k] + a[i];
ll to = (x - y) % C;
dp[i + 1][j + 1][to] = max(dp[i + 1][j + 1][to], y);
}
}
}
}
}
if (dp[n][C][0] != -1) {
mn = min(mn, (x - dp[n][C][0]) / C);
}
}
if (mn == inf) cout << -1;
else cout << mn;
return 0;
}
|
#include<iostream>
#include<iomanip>
#include<cassert>
#include<math.h>
#include<complex>
#include<algorithm>
#include<utility>
#include<queue>
#include<stack>
#include<string.h>
#include<string>
#include<set>
#include<map>
#include<unordered_map>
#include<functional>
#include<vector>
#include<bitset>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
const ll INF=2e18;
const ll MOD=1e9+7;
ll N;
ll dp[18][(1<<18)];
ll x[20],y[20],z[20];
ll d[20][20];
ll Cost(ll now,ll next){
return abs(x[next]-x[now])+abs(y[next]-y[now])+max(0ll,z[next]-z[now]);
}
int main(){
cin>>N;
for(ll i=0;i<N;i++){
cin>>x[i]>>y[i]>>z[i];
}
for(ll i=0;i<N;i++){
for(ll j=0;j<N;j++){
d[i][j] = Cost(i,j);
}
}
for(ll i=0;i<N;i++){
for(ll j=0;j<(1<<N);j++){
dp[i][j]=INF;
}
}
dp[0][1]=0;
for(ll state=1;state<(1<<N);state+=2){
for(ll now=0;now<N;now++){
if(dp[now][state]==INF)continue;
for(ll next=0;next<N;next++){
if(next==now)continue;
if((1<<next) & state)continue;
dp[next][state | (1<<next)] = min(dp[next][state | (1<<next)], dp[now][state]+d[now][next]);
}
}
}
ll ans=INF;
for(ll i=1;i<N;i++){
ans=min(ans,dp[i][(1<<N)-1]+d[i][0]);
}
cout<<ans<<endl;
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;
ll cost(ll a, ll b, ll c, ll p, ll q, ll r){
return abs(a-p) + abs(b-q) + max(0LL, r-c);
}
int main() {
int n;
cin >> n;
vector<ll> x(n), y(n), z(n);
rep(i, n)cin >> x[i] >> y[i] >> z[i];
vector<vector<ll>> dp(1<<n, vector<ll>(n, inf));
dp[0][0] = 0;
rep(i, (1<<n)-1){
rep(j, n){
if(i & (1<<j) == 0)continue;
rep(k, n){
chmin(dp[i|(1<<k)][k], dp[i][j] + cost(x[j],y[j],z[j],x[k],y[k],z[k]));
}
}
}
cout << dp[(1<<n)-1][0] << endl;
}
|
#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <iomanip>
#include <functional>
#include <bitset>
#include <limits>
#include <cstdio>
#include <cmath>
#include <cassert>
#include <random>
#ifdef DEBUG
#include "library/Utility/debug.cpp"
#else
#define debug(...)
#endif
#define rep(i,n) for(int i=0;i<(n);++i)
#define EL '\n'
#define print(i) std::cout << (i) << '\n'
#define all(v) (v).begin(), (v).end()
using lnt = long long;
struct FIO{FIO(){std::cin.tie(0);std::ios_base::sync_with_stdio(0);std::cout<<std::fixed<<std::setprecision(15);}}fIO;
template<typename T> using V = std::vector<T>;
template<typename T> void fill(V<T>&v) { for(T&e:v) std::cin >> e; }
/*-*/
int main() {
int n,m;
std::cin >> n >> m;
V<lnt> w(n);
fill(w);
std::map<lnt,lnt> map;
lnt minload=1e18;
rep(i,m) {
lnt a,b;
std::cin >> a >> b;
map[b]=std::max(map[b],a);
minload=std::min(minload,b);
}
rep(i,n) {
if(w[i]>minload) {
print(-1);
return 0;
}
}
V<std::pair<lnt,lnt> > v;
v.emplace_back(0,0);
for(auto e:map) v.emplace_back(e);
{
lnt max=0;
int k=v.size();
rep(i,k) {
max=std::max(max,v[i].second);
v[i].second=max;
}
}
V<int> p(n);
std::iota(all(p),0);
lnt min=1e18;
std::sort(all(w));
do {
V<V<std::pair<lnt,lnt> > > g(n);
V<lnt> a(n);
rep(i,n) a[i]=w[p[i]];
V<lnt> ac(n+1,0);
rep(i,n) ac[i+1]=ac[i]+a[i];
rep(i,n) {
for(int j=i+1;j<n;j++) {
lnt x=ac[j+1]-ac[i];
int l=-1,r=v.size();
while(r-l>1) {
int o=(l+r)>>1;
if(v[o].first<x) l=o;
else r=o;
}
g[i].emplace_back(j,v[l].second);
}
}
auto dfs=[&](auto f, int i) -> lnt {
lnt max=0;
for(auto to:g[i]) {
max=std::max(max,f(f,to.first)+to.second);
}
return max;
};
lnt x=dfs(dfs,0);
min=std::min(min,x);
} while(std::next_permutation(all(p)));
print(min);
}
| #include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define endl '\n'
void testCase()
{
//ll t; cin>>t; while(t--)
{
ll n,k;
cin>>n>>k;
ll s=101;
ll e=n*100+k;
ll ans=0;
for(int i=e;i>=s;i--)
{
if(i==(n*100))
{
i=(n-1)*100+k;
n--;
}
ans+=i;
}
//ans+=i;
cout<<ans;
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
testCase();
return 0;
} |
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, j, n) for (int i = j; i < (n); ++i)
#define rrep(i, j, n) for (int i = (n)-1; j <= i; --i)
template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T>& a) {
os << "{";
for (size_t i = 0; i < a.size(); ++i) os << (i > 0 ? "," : "") << a[i];
return os << "}";
}
[[maybe_unused]] constexpr ll MOD = 1e9 + 7;
[[maybe_unused]] constexpr int INF = 0x3f3f3f3f;
[[maybe_unused]] constexpr ll INFL = 0x3f3f3f3f3f3f3f3fLL;
int main() {
cin.tie(0)->sync_with_stdio(0);
int n;
cin >> n;
vector<ll> a(n), b(n), p(n), q(n);
vector<vector<int>> g(n);
rep(i, 0, n) cin >> a[i];
vector<pair<int, int>> ps;
rep(i, 0, n) {
cin >> b[i];
ps.emplace_back(b[i], i);
}
sort(ps.rbegin(), ps.rend());
rep(i, 0, n) {
cin >> p[i], --p[i];
q[p[i]] = i;
}
vector<pair<int, int>> ans;
rep(i, 0, n) {
// 重い荷物から完了させていく。
// 重い荷物を渡しても、それがあってるならOK
// 現在いるところと、
auto [weight, bidx] = ps[i];
if (p[bidx] != bidx) {
int nowi = q[bidx]; // bidxを持っているa
// bidx bを持たせたいa
if (a[nowi] <= weight || a[bidx] <= b[p[bidx]]) {
cout << -1 << '\n';
return 0;
}
ans.emplace_back(nowi + 1, bidx + 1);
swap(p[nowi], p[bidx]);
q[p[nowi]] = nowi;
q[p[bidx]] = bidx;
}
}
cout << ans.size() << '\n';
for (auto [x, y] : ans) cout << x << " " << y << '\n';
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define all(c) c.begin(),c.end()
#define sz(x) int(x.size())
#define ar array
typedef long long ll ;
typedef vector<int> vi;
#define mod 1000000007
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll exp(ll x,ll y){
x%=mod;
ll res=1;
while(y){
if(y&1)
res=res*x%mod;
x=x*x%mod;
y>>=1;
}
return res;
}
ll fact(ll n){
ll res=1;
for(ll i=2;i<=n;++i)
(res*=i)%=mod;
return res;
}
bool isprime(ll n){
if(n<=1) return 0;
for(ll i=2;i*i<=n;++i)
if(n%i==0) return 0;
return 1;
}
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n;
cin>>n;
set<int>s;
int mx=10000;
for(int i=2;i<=mx;++i){
if(10*i<=mx)
s.insert(10*i);
}
for(int i=2;i<=mx;++i){
if(6*i<=mx)
s.insert(6*i);
}
for(int i=2;i<=mx;++i){
if(15*i<=mx)
s.insert(15*i);
}
cout<<"10 6 15 ";
n-=3;
for(int i=1;i<=n;++i){
cout<<*s.begin()<<" ";
s.erase(s.begin());
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
#define w(t) int t; cin>>t; while(t--)
#define mod1 1000000007
#define mod2 998244353
#define ll long long int
#define ull unsigned long long int
#define fastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define vll vector<ll>
#define vi vector<int>
#define vch vector<char>
#define pll pair<ll,ll>
#define pii pair<int,int>
#define makpii make_pair
#define mpii unordered_map<int,int>
#define sti unordered_set<int>
#define mpci unordered_map<char,int>
template<typename T>
T myMax(T x, T y)
{
return (x > y) ? x : y;
}
struct myComp1 // sort pairs by second element (MAX)
{
constexpr bool operator()
(pair<ll, ll> const& a, pair<ll, ll> const& b)const noexcept
{
return a.second < b.second;
}
};
struct myComp2 // sort pairs by second element (MIN)
{
constexpr bool operator()
(pair<ll, ll> const& a, pair<ll, ll> const& b)const noexcept
{
return a.second > b.second;
}
};
priority_queue<pll, vector<pll>, myComp1> p1; // priority queue with second element in MAX order
priority_queue<pll, vector<pll>, myComp2> p2; // priority queue with second element in MIN order
int32_t main()
{
fastIO
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int x;
cin >> x;
if (x % 100 == 0)
{
cout << 100 << "\n";
}
else
{
int val = x;
while (val % 100 != 0)
{
val++;
}
cout << val - x << "\n";
}
return 0;
}
/*bool prime[1000001];
int primebeh[1000001];
int primesnear[1000001];
void sieve()
{
int n = 1000000;
memset(prime, true, sizeof(prime));
for (int p = 2; p * p <= n; p++)
{
if (prime[p] == true)
{
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
int last = 0;
for (int i = 2; i <= n; i++)
{
if (prime[i])
{
primesnear[i] = i;
last = i;
}
else
{
primesnear[i] = last;
}
}
last = 0;
for (int i = n; i >= 2; i--)
{
if (prime[i])
{
primebeh[i] = i;
last = i;
}
else
{
primebeh[i] = last;
}
}
ll power(ll x, ll y, ll p)
{
ll res = 1; x = x % p; while (y > 0) {if (y & 1) {res = (res * x) % p;} y = y >> 1; x = (x * x) % p;}
return res;
}
ll modInverse(ll n, int p)
{
return power(n, p - 2, p);
}
ll nCrModPFermat(ll n, ll r, ll p)
{
if (n < r)
return 0;
if (r == 0)
return 1;
ll fac[n + 1];
fac[0] = 1;
for (int i = 1; i <= 1000; i++)
fac[i] = (fac[i - 1] * i) % mod2;
return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p;
}
}*/ | /*
* Created on: 20 / 2 / 2021
* Author: Eslam Waheed
*/
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
clock_t T;
#define ctime cerr << "Time : " << double(clock() - T) / CLOCKS_PER_SEC << '\n'
#define Eslam__Waheed ios_base::sync_with_stdio(false);cout.tie(NULL);cin.tie(NULL);
#define ll long long
#define ull unsigned ll
#define ld long double
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define stp(n) cout << fixed << setprecision(n);
#define iq(v) v.resize(unique(v.begin(),v.end())-v.begin())
#define sz(s) (ll)s.size()
#define endl '\n'
/*
int dx[] = { 1,0,-1,0 };
int dy[] = { 0,1,0,-1 };
*/
//const ld PI = acos(-1);
//ll gcd(ll a, ll b) { return !b ? abs(a) : gcd(b, a % b); }
// ( إن الله وملائكته يصلون على النبي يا أيها الذين آمنوا صلوا عليه وسلموا تسليما )
int main()
{
//stp(0)
Eslam__Waheed;
ll n; cin >> n;
ll x = n % 100;
cout << 100 - x;
} |
#include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
#define STRING(str) #str
#define ll long long
template <typename T >
void print_vec_1d(vector<T> x, string name) {
cout << name << endl;
cout << "[ ";
for (int i=0;i<x.size();i++) {
cout << x[i];
if (i != x.size()-1) cout << ", ";
else cout << " ]" << endl;
}
}
template <typename T >
void print_vec_2d(vector<vector<T>> x, string name) {
cout << name << endl;
cout << "[ ";
for (int i=0;i<x.size();i++) {
cout << "[ ";
for (int j=0;j<x[i].size();j++) {
cout << x[i][j];
if (j != x[i].size()-1) cout << ", ";
else {
cout << " ]";
if (!((i == x.size()-1) && (j == x[i].size()-1))) {
cout << "," << endl;
}
}
}
}
cout << " ]" << endl;
}
int N;
vector<string> S;
int bfs(int i) {
queue<int> q;
q.push(i);
vector<bool> visited(N, false);
while (!q.empty()) {
int ind = q.front();
q.pop();
visited[ind] = true;
for (int j=0;j<N;j++) {
if (visited[j]) continue;
if (S[j][ind] == '1') {
q.push(j);
}
}
}
int ret = 0;
for (int j=0;j<N;j++) {
if (visited[j]) ret++;
}
return ret;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N;
S = vector<string>(N);
for (int i=0;i<N;i++) {
cin >> S[i];
}
double ans = 0;
for (int i=0;i<N;i++) {
ans += (double)1/bfs(i);
}
// cout << ans << "\n";
cout << fixed << setprecision(12) << ans << "\n";
} | /**
* author:
* created: 15.11.2020 00:24:55
**/
#pragma region Macros
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
using P = pair<int,int>;
using ll = long long;
const int INF = 1001001001;
const int di[] = {-1,0,1,0}, dj[] = {0,-1,0,1};
typedef vector<int> vi;
typedef vector<vi> vvi;
#define rep(i,n) for(int i = 0; i < (n); i++)
#define repn(i,n) for(int i = 1; i <= (n); i++)
#define pb push_back
void debug_out() { cout << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cout << H << " ";
debug_out(T...);
}
#ifdef LOCAL
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif
template<class T>
inline bool chmax(T &a, T b) {
if(a < b) {
a = b;
return true;
}
return false;
}
template<class T>
inline bool chmin(T &a, T b) {
if(a > b) {
a = b;
return true;
}
return false;
}
const double PI = acos(-1);
const int mod = 1000000007;
class mint {
long long x;
public:
mint(long long x=0) : x((x%mod+mod)%mod) {}
mint operator-() const {
return mint(-x);
}
mint& operator+=(const mint& a) {
if ((x += a.x) >= mod) x -= mod;
return *this;
}
mint& operator-=(const mint& a) {
if ((x += mod-a.x) >= mod) x -= mod;
return *this;
}
mint& operator*=(const mint& a) {
(x *= a.x) %= mod;
return *this;
}
mint operator+(const mint& a) const {
mint res(*this);
return res+=a;
}
mint operator-(const mint& a) const {
mint res(*this);
return res-=a;
}
mint operator*(const mint& a) const {
mint res(*this);
return res*=a;
}
mint pow(ll t) const {
if (!t) return 1;
mint a = pow(t>>1);
a *= a;
return a;
}
// for prime mod
mint inv() const {
return pow(mod-2);
}
mint& operator/=(const mint& a) {
return (*this) *= a.inv();
}
mint operator/(const mint& a) const {
mint res(*this);
return res/=a;
}
friend ostream& operator<<(ostream& os, const mint& m){
os << m.x;
return os;
}
};
#pragma endregion
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int n;
cin >> n;
vector<vector<int>> a(n, vector<int> (n,0));
rep(i,n){
rep(j,n){
char c;
cin >> c;
if(c == '1') a[i][j] = 1;
}
}
rep(i,n) a[i][i] = 1;
rep(k,n)rep(i,n)rep(j,n) a[i][j] |= a[i][k] & a[k][j];
double ans = 0;
rep(i,n){
int cnt = 0;
rep(j,n) cnt += a[j][i];
ans += double(1)/double(cnt);
}
cout << fixed << setprecision(10);
cout << ans << "\n";
return 0;
} |
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
using ll =long long;
using vi = vector<ll>;
using vc = vector<char>;
using vs = vector<string>;
using P = pair<ll , ll>;
using vp = vector<P>;
#define rep(i, n) for (long long int i = 0; i < n; i++)
#define rep2(i, s, n) for (int i = (s); i < n; i++)
#define sort(A) sort(A.begin(),A.end());
#define reverse(A) reverse(A.begin(),A.end());
#define vecint2(A, n, m) vector<vector<long long int>> A(n, vector<long long int>(m));
#define vecchar2(A, n, m) vector<vector<char>> A(n, vector<char>(m));
#define k(s) cout << fixed << setprecision(s);
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl
#define YES cout<<"YES"<<endl
#define NO cout<<"NO"<<endl
const long double pi=3.14159265358979323846;
long long modpow(long long a, long long n, long long mod) {
long long res = 1;
while (n > 0) {
if (n & 1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
}
long long modinv(long long a, long long m) {
long long b = m, u = 1, v = 0;
while (b) {
long long t = a / b;
a -= t * b; swap(a, b);
u -= t * v; swap(u, v);
}
u %= m;
if (u < 0) u += m;
return u;
}
int main() {
ll MOD=998244353;
int H,W,K;
cin>>H>>W>>K;
vecint2(A,H,W);
vecint2(dp,H,W);
rep(i,H)rep(j,W){
A[i][j]=0;dp[i][j]=0;
}
rep(i,K){
int h,w;
char c;
cin>>h>>w>>c;
if(c=='R') A[h-1][w-1]=1;
if(c=='D') A[h-1][w-1]=2;
if(c=='X') A[h-1][w-1]=3;
}
dp[0][0]=modpow(3,H*W-K,MOD);
rep(i,H)rep(j,W){
if(i==0 && j==0){
continue;
}else if(i==0){
if(A[i][j-1]==0) dp[i][j]+=2*dp[i][j-1]%MOD*modinv(3,MOD)%MOD;
if(A[i][j-1]==1) dp[i][j]+=dp[i][j-1]%MOD;
if(A[i][j-1]==3) dp[i][j]+=dp[i][j-1]%MOD;
}else if(j==0){
if(A[i-1][j]==0) dp[i][0]+=2*dp[i-1][j]%MOD*modinv(3,MOD)%MOD;
if(A[i-1][j]==2) dp[i][0]+=dp[i-1][j]%MOD;
if(A[i-1][j]==3) dp[i][0]+=dp[i-1][j]%MOD;
}else{
if(A[i][j-1]==0) dp[i][j]+=2*dp[i][j-1]%MOD*modinv(3,MOD)%MOD;
if(A[i][j-1]==1) dp[i][j]+=dp[i][j-1]%MOD;
if(A[i][j-1]==3) dp[i][j]+=dp[i][j-1]%MOD;
if(A[i-1][j]==0) dp[i][j]+=2*dp[i-1][j]%MOD*modinv(3,MOD)%MOD;
if(A[i-1][j]==2) dp[i][j]+=dp[i-1][j]%MOD;
if(A[i-1][j]==3) dp[i][j]+=dp[i-1][j]%MOD;
}
dp[i][j]%=MOD;
}
/* rep(i,H){
rep(j,W) cout<<dp[i][j]<<" ";
cout<<endl;
}
*/
cout<<dp[H-1][W-1]<<endl;
} | #include <bits/stdc++.h>
#include <ext/rope>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#define int long long
#define pb push_back
#define x first
#define y second
#define mk(a,b) make_pair(a,b)
#define rr return 0
#define sqr(a) ((a)*(a))
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template<class value, class cmp = less<value> >
using ordered_set = tree<value, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
template<class value, class cmp = less_equal<value> >
using ordered_multiset = tree<value, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
template<class key, class value, class cmp = less<key> >
using ordered_map = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
/// find_by_order()
/// order_of_key()
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
inline int randll(int l = INT_MIN, int r = INT_MAX) {
return uniform_int_distribution<int>(l, r)(rng);
}
const int INF = 1e9;
int MOD = 1e9 + 7;
const ll LINF = 1e18;
const int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
inline bool inside (int x, int y, int n, int m) {
return 0 <= x && 0 <= y && x < n && y < m;
}
template<class T> bool umin (T &a, T b) {return a > b ? (a = b, true) : false; }
template<class T> bool umax (T &a, T b) {return a < b ? (a = b, true) : false; }
inline int mul (int a, int b, int m = MOD) {
return ((ll)a * b) % m;
}
inline int binpow (int a, int n, int m = MOD) {
int ans = 1;
for (; n; n >>= 1) {
if (n & 1) ans = mul(ans, a, m);
a = mul(a, a, m);
}
return ans;
}
inline void add (int &a, int x, int m = MOD) {
a += x;
if (a >= m) a -= m;
if (a < 0) a += m;
}
inline int sum (int a, int b, int m = MOD) {
a += b;
if (a >= m) a -= m;
if (a < 0) a += m;
return a;
}
inline int inv (int x, int m = MOD) {
return binpow(x, m - 2, m); /// only if m is prime
}
vector <int> f, fi;
inline void precalc_f (int n, int m = MOD) {
f.resize(n);
fi.resize(n);
f[0] = 1;
for (int i = 1; i < n; i++) {
f[i] = mul(f[i - 1], i, m);
}
fi[n - 1] = inv(f[n - 1], m);
for (int i = n - 2; i >= 0; i--) {
fi[i] = mul(fi[i + 1], i + 1, m);
}
}
inline int A (int n, int k, int m = MOD) {
return mul(f[n], fi[n - k], m);
}
inline int C (int n, int k, int m = MOD) {
return mul(A(n, k), fi[k], m);
}
const int N = 101;
int dp[N][N * N * (N + 1) / 2] = {};
main()
{
ios::sync_with_stdio(0);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, k;
cin >> n >> k >> MOD;
dp[0][0] = 1;
for (int x = 1; x <= n; x++) {
vector <int> S(x);
for (int s = 0, y = 0; s <= k * n * (n + 1) / 2; s++) {
add(S[y], dp[x - 1][s]);
if ((k + 1) * x <= s) {
add(S[y], -dp[x - 1][s - (k + 1) * x]);
}
dp[x][s] = S[y];
add(y, 1, x);
}
}
// for (int i = 1; i <= n; i++) {
// for (int j = 0; j <= k * n * (n + 1) / 2; j++) {
// cout << dp[i][j] << ' ';
// }
// cout << '\n';
// }
for (int x = 1; x <= n; x++) {
int ans = 0;
for (int s = 1; s <= k * n * (n + 1) / 2; s++) {
add(ans, mul(dp[x - 1][s], dp[n - x][s]));
}
ans = mul(ans, k + 1);
add(ans, k);
cout << ans << '\n';
}
}
|
#include <stdio.h>
#include <iostream>
#include <vector>
// #include <bits/stdc++.h>
#include <queue>
#include <algorithm>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
typedef long long ll;
#define rep(i,k,n) for(ll i=k; i<(ll)(n); i++)
int main() {
ll n; cin >> n;
vector<ll> a(n);
ll ans = 0;
rep(i,0,n) {
cin >> a[i];
if(a[i]-10 > 0) ans += a[i]-10;
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define N 100005
#define MOD 1000000007
#define dd double
#define vi vector<int>
#define vll vector<ll>
#define forr(i,n) for(ll i=0;i<n;i++)
#define revf(i,n) for(ll i=n-1;i>=0;i--)
#define REP(i,a,b) for(ll i=a;i<b;i++)
#define rep1(i,b) for(ll i=1;i<=b;i++)
#define inp(a,n) for(ll i=0;i<n;i++)cin>>a[i]
#define outp(a,n) for(ll i=0;i<n;i++)cout<<a[i]<<" "
#define outp1(a,n) for(ll i=1;i<n;i++)cout<<a[i]<<" "
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) (((a)*(b))/gcd((a),(b)))
#define pb push_back
#define mp make_pair
#define BINSC binary_search
#define BITOUT __builtin_popcount
#define mem(x,n) memset(x,n,sizeof(x))
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL);
#define test ll t; cin>>t; while(t--)
#define cn cout<<"\n";
int main()
{
fast
ll k;
cin>>k;
ll ans=0;
REP(i,1,k+1)
{
REP(j,1,k+1)
{
if(i*j>k)
break;
REP(q,1,k+1)
{
if(i*j*q>k)
break;
ans++;
}
}
}
cout<<ans;
} |
/// HuDzG - NHDanDz "Elderwood" (DangCuto Dep Trai vcl)
#include <bits/stdc++.h>
#define reset(a) memset(a,0,sizeof(a))
#define ll long long
#define ld long double
#define endl '\n'
#define AutoAC int main()
#define OO 1000000000000000000
#define F first
#define S second
#define pii pair <ll, ll>
#define pb push_back
#define nmax 200005
#define HUNGIT "C"
#define MOD 998244353
using namespace std;
const ll M = 1000000;
ll n, Begin[nmax], End[nmax], res[nmax], q, ST[4 * nmax], cnt = 0;
struct pp
{
ll u, d, id;
} Q[nmax];
vector <ll> g[nmax], node[2];
bool cmp(pp a, pp b)
{
return a.d < b.d;
}
ll dfs(ll u)
{
ll Max = 0;
Begin[u] = ++Begin[0];
for(auto v : g[u])
Max = max(Max, dfs(v) + 1);
End[u] = Begin[0];
return Max;
}
void Update(ll id, ll l, ll r, ll i, ll v)
{
if(r < i || i < l) return;
if(l == r)
{
ST[id] = v;
return;
}
ll mid = (l + r) / 2;
Update(id * 2, l, mid, i, v);
Update(id * 2 + 1, mid + 1, r, i, v);
ST[id] = ST[id * 2] + ST[id * 2 + 1];
}
ll Get(ll id, ll l, ll r, ll x, ll y)
{
if(r < x || y < l) return 0;
if(x <= l && r <= y) return ST[id];
ll mid = (l + r) / 2;
return Get(id * 2, l, mid, x, y) + Get(id * 2 + 1, mid + 1, r, x, y);
}
void bfs()
{
ll now = cnt % 2, Next = (cnt + 1) % 2;
cnt++;
while(!node[now].empty())
{
ll u = node[now].back();
Update(1, 1, n, Begin[u], 0);
for(auto v : g[u])
{
node[Next].pb(v);
Update(1, 1, n, Begin[v], 1);
}
node[now].pop_back();
}
}
void solve()
{
cin >> n;
for (int i = 2; i <= n; i++)
{
ll p;
cin >> p;
g[p].pb(i);
}
cin >> q;
for(int i = 1; i <= q; i++)
{
cin >> Q[i].u >> Q[i].d;
Q[i].id = i;
}
sort(Q + 1, Q + 1 + q, cmp);
ll Max = dfs(1);
node[0].pb(1);
Update(1, 1, n, Begin[1], 1);
for(int i = 1; i <= q; i++)
{
ll u = Q[i].u, d = Q[i].d, id = Q[i].id;
if(d > Max) break;
while(cnt < d) bfs();
res[id] = Get(1, 1, n, Begin[u], End[u]);
}
for(int i = 1; i <= q; i++)
cout << res[i] << endl;
}
AutoAC
{
// clock_t START, FINISH;
// START = clock();
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen (HUNGIT".inp", "r", stdin);
// freopen (HUNGIT".out", "w", stdout);
ll T = 1;
// cin >> T;
for (int i = 1; i <= T; i++)
{
solve();
}
// FINISH = clock();
// cout << fixed << setprecision(4);
// cout << endl << "TIME: " << (ld)((ld)(FINISH - START) / CLOCKS_PER_SEC);
return 0;
}
| #include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
typedef long long ll;
typedef long double ld;
#define rep(i,n) for(ll i=0;i<(n);i++)
#define repr(i,n) for(ll i=(n-1);i>=0;i--)
#define all(x) x.begin(),x.end()
#define br cout << "\n";
using namespace std;
const long long INF = 1e18;
const long long MOD = 1e9+7;
using Graph = vector<vector<ll>>;
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;}
ll ceilll(ll a, ll b) {return (a + b-1) / b;} // if(a%b != 0) (a/b) + 1
ll get_digit(ll a) {ll digit = 0; while(a != 0){a /= 10; digit++;} return digit;} // a != 0
template<typename T> void vecdbg(vector<T>& v){ rep(i, v.size()){cerr << v[i] << " ";} br;}
template<typename T> void vecvecdbg(vector<vector<T>>& v){ rep(i, v.size()){rep(j, v[i].size()){cerr << v[i][j] << " ";} br;}}
ll POW(ll a, ll n){ ll res = 1; while(n > 0){ if(n & 1){ res = res * a; } a *= a; n >>= 1; } return res; }
using P = pair<ll, ll>;
// 0 false, 1 true
// string number to int : -48 or - '0'
// a to A : -32
// ceil(a) 1.2->2.0
// c++17 g++ -std=c++17 a.cpp
// global vector -> 0 initialization
// DONT FORGET TO INTIALIZE
// The type of GRID is CHAR. DONT USE STRING
// If the result in local and judge is different, USE CODETEST!!
// (a * b)over flow? if(a > INF / b){ /* overflow */}
ll n;
ll cnt = 0;
vector<pair<ll, ll>> inout(n);
vector<ll> depth(n);
vector<ll> seen(n);
void dfs(vector<vector<ll>> &g, ll v, ll d){
//in
inout[v].first = cnt;
depth[v] = d;
seen[v] = 1;
cnt++;
for(ll next : g[v]){
if(seen[next] == 0){
dfs(g, next, d+1);
cnt++;
}
}
//out
inout[v].second = cnt;
}
int main() {
std::cout << std::fixed << std::setprecision(15);
cin >> n;
vector<vector<ll>> g(n);
depth.assign(n, 0);
inout.assign(n, {-1, -1});
seen.assign(n, 0);
rep(i, n-1){
ll p; cin >> p; p--;
g[i+1].push_back(p); g[p].push_back(i+1);
}
dfs(g, 0, 0);
vector<vector<ll>> hukasa(n);
rep(i, n){
ll x = depth[i];
hukasa[x].push_back(inout[i].first);
}
rep(i, n){
sort(all(hukasa[i]));
}
ll q; cin >> q;
rep(i, q){
ll u, d; cin >> u >> d; u--;
ll r = lower_bound(all(hukasa[d]), inout[u].second) - hukasa[d].begin();
ll l = lower_bound(all(hukasa[d]), inout[u].first) - hukasa[d].begin();
cout << r - l << endl;
}
} |
#import<bits/stdc++.h>
#define endl '\n'
using namespace std;
int i,j,k,m,n,p,x,y,a[1005][1005],b[1005][1005],c[1005][1005],d[1005][1005],z[1005][1005];
char s[1005][1005];
void f(int i,int j)
{
if(z[i][j])return;
z[i][j]=k;
if(~a[i][j])f(a[i][j],j);
if(~b[i][j])f(i,b[i][j]);
if(c[i][j]<n)f(c[i][j],j);
if(d[i][j]<m)f(i,d[i][j]);
}
main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(;i<n;i++)cin>>s[i];
for(;j<m;j++) // a : U
{
for(i=0,k=-1;i<n;i++)
{
a[i][j]=k;
if(s[i][j]&1)k=i;
}
}
for(i=0;i<n;i++) // b : L
{
for(j=0,k=-1;j<m;j++)
{
b[i][j]=k;
if(s[i][j]&1)k=j;
}
}
for(j=m;j--;) // c : D
{
for(i=k=n;i--;)
{
c[i][j]=k;
if(s[i][j]&1)k=i;
}
}
for(i=n;i--;) // d : R
{
for(j=k=m;j--;)
{
d[i][j]=k;
if(s[i][j]&1)k=j;
}
}
for(i=n-1;--i;)
{
if(b[i][m-1]<0&&d[i][0]==m)x++;
}
for(j=m-1;--j;)
{
if(a[n-1][j]<0&&c[0][j]==n)y++;
}
p=min(x,y);
for(i=k=0;i<n;i++)
{
if(s[i][0]&1&&!z[i][0])
{
k++;
f(i,0);
}
if(s[i][m-1]&1&&!z[i][m-1])
{
k++;
f(i,m-1);
}
}
for(j=0;j<m;j++)
{
if(s[0][j]&1&&!z[0][j])
{
k++;
f(0,j);
}
if(s[n-1][j]&1&&!z[n-1][j])
{
k++;
f(n-1,j);
}
}
for(i=k=0;i<n;i++)
{
for(j=0;j<m;j++)
{
if(s[i][j]&1&&!z[i][j])
{
k++;
f(i,j);
}
}
}
cout<<p+k;
} | #include <bits/stdc++.h>
using namespace std;
#define ll long long int
const double pi = 3.14159265358979;
const ll inf = 1e17;
const int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dy[] = {1, 0, -1, 0, 1, -1, 1, -1};
#define pii pair<int,int>
#define pll pair<ll,ll>
#define endl "\n"
#define dtor(deg) (((deg)/360)*2*pi)
#define all(a) a.begin(),a.end()
#define overload(_1,_2,_3,_4,name,...) name
#define _rep1(n) for(int i = 0; i < (n); i++)
#define _rep2(i,n) for(int i = 0; i < (n); i++)
#define _rep3(i,a,b) for(int i = (a); i < (b); i++)
#define _rep4(i,a,b,c) for(int i = (a); i < (b); i += (c))
#define rep(...) overload(__VA_ARGS__,_rep4,_rep3,_rep2,_rep1)(__VA_ARGS__)
#define _rrep1(n) for(int i = (n) - 1; i >= 0; i--)
#define _rrep2(i,n) for(int i = (n) - 1; i >= 0; i--)
#define _rrep3(i,a,b) for(int i = (b) - 1; i >= (a); i--)
#define _rrep4(i,a,b,c) for(int i = (b) - 1; i >= (a); i -= (c))
#define rrep(...) overload(__VA_ARGS__,_rrep4,_rrep3,_rrep2,_rrep1)(__VA_ARGS__)
#define vec(type,name,...) vector<type> name(__VA_ARGS__)
#define vv(type,name,size,...) vector<vector<type>> name(size,vector<type>(__VA_ARGS__))
#define ForEach(a,b) for_each(a.begin(),a.end(),b)
template <class T> bool chmin(T& a, T b){ if(a > b){ a = b; return 1; } return 0; }
template <class T> bool chmax(T& a, T b){ if(a < b){ a = b; return 1; } return 0; }
struct WeightedGraph{
struct Edge { int to; ll cost; Edge(int to, ll cost) : to(to), cost(cost) {} };
vector<vector<Edge>> g;
WeightedGraph(int n) : g(n) {}
vector<Edge> operator[](int a){ return g[a]; }
operator vector<vector<Edge>>(){ return g; }
auto begin(){ return g.begin(); }
auto end(){ return g.end(); }
int size(int x){ return g.size(); }
void resize(int x){ g.resize(x); }
void DirectConnect(int f, int t, ll c){ g[f].push_back({t, c}); }
void Connect(int a, int b, ll c){ g[a].push_back({b, c}); g[b].push_back({a, c}); }
vector<ll> dijkstra(int s){
int n = g.size();
vector<ll> dist(n, inf);
priority_queue<pair<ll,int>,vector<pair<ll,int>>,greater<pair<ll,int>>> task;
dist[s] = 0;
task.push({dist[s], s});
while(task.size()){
pii p = task.top();
task.pop();
int v = p.second;
if(dist[v] < p.first) continue;
for(auto e : g[v]) if(chmin(dist[e.to], dist[v] + e.cost)) task.push({dist[e.to], e.to});
}
return dist;
}
vector<int> topo_sort(){
int n = g.size();
WeightedGraph copy(n);
vector<int> deg(n);
vector<int> order;
queue<int> task;
rep(n) for(auto v : g[i]){
copy.DirectConnect(v.to, i, v.cost);
deg[i]++;
}
rep(n) if(!deg[i]) task.push(i);
while(task.size()){
int fv = task.front();
task.pop();
order.push_back(fv);
for(auto nv : copy[fv]){
deg[nv.to]--;
if(!deg[nv.to]) task.push(nv.to);
}
}
reverse(all(order));
return order;
}
};
void Main(){
int n, m;
cin >> n >> m;
WeightedGraph g(n), r(n);
vector<vector<ll>> cycle(n);
rep(m){
int a, b;
ll c;
cin >> a >> b >> c;
a--; b--;
if(a == b) cycle[a].push_back(c);
else{ g.DirectConnect(a, b, c); r.DirectConnect(b, a, c); }
}
rep(n){
ll ans = inf;
vector<ll> t = g.dijkstra(i), tr = r.dijkstra(i);
for(auto x : cycle[i]) chmin(ans, x);
rep(j,n) if(i != j) chmin(ans, t[j] + tr[j]);
if(ans == inf) cout << -1 << endl;
else cout << ans << endl;
}
}
signed main(){
cin.tie(0);
ios::sync_with_stdio(false);
cout << setprecision(10) << fixed;
Main();
} |
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()
typedef pair<int,int> P;
signed main(){
int n,m,q;cin>>n>>m>>q;
vector<P> v(n);
REP(i,n)cin>>v[i].first>>v[i].second;
sort(ALL(v));
vector<int> x(m);
REP(i,m)cin>>x[i];
auto solve=[&](vector<int> &k){
sort(ALL(k));
priority_queue<int> que;
int res=0,now=0;
for(int p:k){
while(now<n&&v[now].first<=p)que.push(v[now++].second);
if(que.size()){
res+=que.top();
que.pop();
}
}
return res;
};
while(q--){
int l,r;cin>>l>>r;l--;
vector<int> k;
REP(i,m)if(i<l||r<=i)k.push_back(x[i]);
cout<<solve(k)<<endl;
}
}
| /**
* code generated by JHelper
* More info: https://github.com/AlexeyDmitriev/JHelper
* @author
*/
#include <iostream>
#include <fstream>
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<int, int>;
using VI = vector<int>;
#define fst first
#define snd second
void solve(istream &cin, ostream &cout) {
int n, m, q;
cin >> n >> m >> q;
vector<PII> bags(n);
for (auto &[v, w] : bags)
cin >> w >> v;
sort(bags.rbegin(), bags.rend());
VI boxes(m);
for (int &box : boxes)
cin >> box;
while (q--) {
int l, r;
cin >> l >> r;
l--, r--;
VI use;
for (int i = 0; i < m; i++) {
if (l <= i && i <= r) continue;
use.push_back(boxes[i]);
}
sort(use.begin(), use.end());
auto heap = bags;
int tot = 0;
for (int box : use) {
for (auto &[v, w] : heap) {
if (v != 0 && w <= box) {
tot += v;
v = 0;
break;
}
}
}
cout << tot << '\n';
}
}
struct DShippingCenter {
void solve(std::istream &in, std::ostream &out) {
::solve(in, out);
}
};
int main() {
DShippingCenter solver;
std::istream& in(std::cin);
std::ostream& out(std::cout);
solver.solve(in, out);
return 0;
}
|
#include "bits/stdc++.h"
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,mmx,avx,avx2")
using namespace std;
using ll = long long;
void A()
{
string s; cin >> s;
if (s[0] == s[1] && s[1] == s[2]) cout << "Won\n";
else cout << "Lost\n";
}
void B()
{
int n, limit; cin >> n >> limit;
limit *= 100;
for (int i = 1; i <= n; ++i) {
int vol, percent; cin >> vol >> percent;
limit -= vol*percent;
if (limit < 0) {cout << i; return;}
}
cout << -1;
}
void C()
{
int n; cin >> n;
int a[n];
ll ans = 0;
for (int i = 0; i < n; ++i) {
cin >> a[i];
int mn = a[i];
for (int j = i; j >= 0; --j) {
if (a[j] < mn) mn = a[j];
ans = max(ans, 1LL*mn*(i-j+1));
}
}
cout << ans;
}
void D()
{
int n; cin >> n;
vector<string> v(n);
vector<array<ll, 2>> dp(n+1);
dp[0][0] = dp[0][1] = 1;
for (int i = 1; i <= n; ++i) {
cin >> v[i-1];
if (v[i-1] == "OR") {
dp[i][1] = dp[i-1][0] + 2*dp[i-1][1];
dp[i][0] = dp[i-1][0];
}
else {
dp[i][1] = dp[i-1][1];
dp[i][0] = dp[i-1][1] + 2*dp[i-1][0];
}
}
cout << dp[n][1];
}
void E()
{
int n; cin >> n;
vector<array<ll, 2>> pts(n);
for (auto &[x, y] : pts)
cin >> x >> y;
int m; cin >> m;
using row = array<ll, 3>;
using matrix = array<row, 3>;
vector<matrix> mat(m+1);
mat[0][0][0] = mat[0][1][1] = mat[0][2][2] = 1;
auto matmul = [&] (auto &a, auto &b) {
array<array<ll, 3>, 3> res = array<array<ll, 3>, 3>();
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
for (int k = 0; k < 3; ++k)
res[i][j] += a[i][k] * b[k][j];
return res;
};
for (int i = 1; i <= m; ++i) {
int type; cin >> type;
if (type == 1) {
mat[i] = matrix{row{0, 1, 0}, row{-1, 0, 0}, row{0, 0, 1}};
}
else if (type == 2) {
mat[i] = matrix{row{0, -1, 0}, row{1, 0, 0}, row{0, 0, 1}};
}
else {
int p; cin >> p;
if (type == 3) {
mat[i] = matrix{row{-1, 0, 2*p}, row{0, 1, 0}, row{0, 0, 1}};
}
else {
mat[i] = matrix{row{1, 0, 0}, row{0, -1, 2*p}, row{0, 0, 1}};
}
}
mat[i] = matmul(mat[i], mat[i-1]);
}
int q; cin >> q;
while (q--) {
int a, b; cin >> a >> b;
auto [x, y] = pts[b-1];
auto transform = mat[a];
cout << x*transform[0][0] + y*transform[0][1] + transform[0][2] << ' ' << x*transform[1][0] + y*transform[1][1] + transform[1][2] << '\n';
}
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0);
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
E();
} | #include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
using namespace std;
#define int long long
int safe_mod(int x, int m) {
x %= m;
if (x < 0) x += m;
return x;
}
pair<int,int> inv_gcd(int a, int b) {
a = safe_mod(a, b);
if (a == 0) return {b, 0};
int s = b, t = a;
int m0 = 0, m1 = 1;
while (t) {
int u = s / t;
s -= t * u;
m0 -= m1 * u;
auto tmp = s;
s = t;
t = tmp;
tmp = m0;
m0 = m1;
m1 = tmp;
}
if (m0 < 0) m0 += b / s;
return {s, m0};
}
pair<int,int> crt(const vector<int>& r, const vector<int>& m) {//if p.second == 0 then there is no answer
assert(r.size() == m.size());
int n = (int)(r.size());
int r0 = 0, m0 = 1;
for (int i = 0; i < n; i++) {
assert(1 <= m[i]);
int r1 = safe_mod(r[i], m[i]), m1 = m[i];
if (m0 < m1) {
swap(r0, r1);
swap(m0, m1);
}
if (m0 % m1 == 0) {
if (r0 % m1 != r1) return {0, 0};
continue;
}
int g, im;
tie(g, im) = inv_gcd(m0, m1);
int u1 = (m1 / g);
if ((r1 - r0) % g) return {0, 0};
int x = (r1 - r0) / g % u1 * im % u1;
r0 += x * m0;
m0 *= u1;
if (r0 < 0) r0 += m0;
}
return {r0, m0};
}
signed main(){
cin.tie(nullptr)->sync_with_stdio(false);
int t;
cin >> t;
while(t--){
int x,y,p,q;
cin >> x >> y >> p >> q;
int ans = (int)9e18;
for(int i = x; i<x+y; i++){
for(int j = p; j<p+q; j++){
pair<int,int> a = crt({i,j},{x+y+x+y,p+q});
if(a.second==0)continue;
ans = min(ans,a.first);
}
}
if(ans==(int)9e18){
cout << "infinity\n";
}
else{
cout << ans << "\n";
}
}
return 0;
} |
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const long long template_array_size = 1e6+100;
long long a[template_array_size];
long long b[template_array_size];
ll ans = 21;
void solve(){
ll a,b,c;
cin>>a>>b>>c;
ans = ans - (a+b+c);
cout<<ans<<"\n";
}
int main(){
solve();
return 0;
} | #include <iostream>
using namespace std;
int main()
{
int X, Y, Z;
cin >> X >> Y >> Z;
double a = (double)Z * Y / X;
int b = (int)a;
if(a == b) {
cout << b-1 << endl;
} else {
cout << b << endl;
}
return 0;
} |
#line 1 "/home/siro53/kyo-pro/compro_library/template/template.cpp"
#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
if(a < b) {
a = b;
return 1;
}
return 0;
}
template <class T> inline bool chmin(T &a, T b) {
if(a > b) {
a = b;
return 1;
}
return 0;
}
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
os << '(' << p.first << ',' << p.second << ')';
return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << '{';
for(int i = 0; i < (int)v.size(); i++) {
if(i) { os << ','; }
os << v[i];
}
os << '}';
return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
cerr << " " << x;
debugg(args...);
}
#define debug(...) \
cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
struct Setup {
Setup() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(15);
}
} __Setup;
using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define FOR(i, a, b) for(int i = (a); i < int(b); i++)
#define REP(i, n) FOR(i, 0, n)
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
//-------------------------------------
#line 2 "e.cpp"
int N, Q;
ll a, b, c; // F(x) = min(c, max(b, x+a));
int main() {
cin >> N;
a = 0, b = -LLINF, c = LLINF;
REP(i, N) {
ll x; int t; cin >> x >> t;
if(t == 1) {
a += x, b += x, c += x;
}else if(t == 2) {
chmax(b, x); chmax(c, x);
}else{
chmin(b, x); chmin(c, x);
}
}
cin >> Q;
while(Q--) {
ll x; cin >> x;
cout << min(c, max(b, x+a)) << endl;
}
}
| #pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#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<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;
//#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 1000000007;
const ll INF = mod * mod;
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 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++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acosl(-1.0);
ll mod_pow(ll x, ll n, ll m = mod) {
if (n < 0) {
ll res = mod_pow(x, -n, m);
return mod_pow(res, m - 2, m);
}
if (abs(x) >= m)x %= m;
if (x < 0)x += m;
ll res = 1;
while (n) {
if (n & 1)res = res * x % m;
x = x * x % m; n >>= 1;
}
return res;
}
struct modint {
ll n;
modint() :n(0) { ; }
modint(ll m) :n(m) {
if (n >= mod)n %= mod;
else if (n < 0)n = (n % mod + mod) % mod;
}
operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
if (n == 0)return modint(1);
modint res = (a * a) ^ (n / 2);
if (n % 2)res = res * a;
return res;
}
ll inv(ll a, ll p) {
return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 2;
modint fact[max_n], factinv[max_n];
void init_f() {
fact[0] = modint(1);
for (int i = 0; i < max_n - 1; i++) {
fact[i + 1] = fact[i] * modint(i + 1);
}
factinv[max_n - 1] = modint(1) / fact[max_n - 1];
for (int i = max_n - 2; i >= 0; i--) {
factinv[i] = factinv[i + 1] * modint(i + 1);
}
}
modint comb(int a, int b) {
if (a < 0 || b < 0 || a < b)return 0;
return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
if (a < 0 || b < 0 || a < b)return 0;
return fact[a] * factinv[a - b];
}
void solve() {
int n; cin >> n;
ll c[3] = { 0,-INF,INF };
rep(i, n) {
ll a;int t; cin >> a >> t;
if (t == 1) {
c[0] += a;
c[1] += a;
c[2] += a;
}
else if (t == 2) {
c[1] = max(c[1], a);
c[2] = max(c[2], a);
}
else {
c[2] = min(c[2], a);
}
}
int q; cin >> q;
rep(i, q) {
ll x; cin >> x;
ll ans = min(max(x + c[0], c[1]), c[2]);
cout << ans << "\n";
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(10);
//init_f();
//init();
//expr();
//int t; cin >> t; rep(i, t)
solve();
return 0;
} |
#include <bits/stdc++.h>
#define REP(i, n) for(i64 i = 0; i < n; ++i)
#define RREP(i, n) for(i64 i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for(auto i = a; i != b; ++i)
#define ALL(v) (v).begin(), (v).end()
using namespace std;
using i64 = int_fast64_t;
using u64 = uint_fast64_t;
using f64 = double;
using p64 = pair<i64, i64>;
const string ln = "\n";
const i64 dx[4] = {1, 0, -1, 0};
const i64 dy[4] = {0, 1, 0, -1};
const i64 inf = 1e18;
i64 Sx, Sy, Gx, Gy;
int main() {
cin >> Sx >> Sy >> Gx >> Gy;
f64 ans = Gx - (f64)Gy * (Gx - Sx) / (Gy + Sy);
cout << fixed << setprecision(10) << ans << ln;
} | #include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
double a,b,c,d;
double x,k;
double s,e;
scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
x=-d*(c-a)/(d+b)+c;
printf("%lf",x);
} |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (long long i = 0; i < (n); i++)
using ll = long long;
using P = pair<ll, ll>;
const ll INF = 1001001001;
int main() {
ll N; cin >> N;
vector<vector<ll>> C(N, vector<ll>(N));
vector<ll> A(N), B(N);
// (最小値, 最小値が含まれる行数) を表す Pair
P min_val(INF, 0);
rep(i, N) rep(j, N) {
ll val;
cin >> val;
if (min_val.first > val) {
min_val = make_pair(val, i);
}
C[i][j] = val;
}
bool ok = true;
A[min_val.second] = min_val.first;
for (ll i = 0; i < N; i++) {
B[i] = C[min_val.second][i] - A[min_val.second];
}
for (ll i = 0; i < N; i++) {
A[i] = C[i][0] - B[0];
}
rep(i, N) rep(j, N) {
if (C[i][j] != A[i] + B[j]) ok = false;
if (A[i] < 0 || B[j] < 0) ok = false;
}
if (ok) {
cout << "Yes" << endl;
rep(i, N) cout << A[i] << endl;
rep(i, N) cout << B[i] << endl;
}
else cout << "No" << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
using ll = long long int;
using ld = long double;
#define pow(n,m) powl(n,m);
#define sqrt(n) sqrtl(n);
const ll MAX = 5000000000000000000;
const ll MOD = 1000000007;
//998244353;
void randinit(){srand((unsigned)time(NULL));}
int main(){
ll N;
cin >> N;
vector<vector<ll>> C(N,vector<ll>(N,0));
for(ll i = 0;i < N;i++) for(ll j = 0;j < N;j++) cin >> C[i][j];
ll p = MAX;
for(ll i = 0;i < N;i++) p = min(p,C[i][0]);
vector<ll> A(N),B(N);
A[0] = p;
B[0] = C[0][0] - p;
for(ll i = 0;i < N;i++) A[i] = C[i][0] - B[0];
for(ll i = 0;i < N;i++) B[i] = C[0][i] - A[0];
bool b = 1;
for(ll i = 0;i < N;i++) if(0 > A[i]) b = 0;
for(ll i = 0;i < N;i++) if(0 > B[i]) b = 0;
for(ll i = 0;i < N;i++){
for(ll j = 0;j < N;j++){
if(A[i] + B[j] != C[i][j]){
b = 0;
break;
}
}
if(b == 0) break;
}
if(b){
cout << "Yes\n";
for(ll i = 0;i < N - 1;i++) cout << A[i] << " ";
cout << A[N - 1] << endl;
for(ll i = 0;i < N - 1;i++) cout << B[i] << " ";
cout << B[N - 1] << endl;
return 0;
}
B[0] = p;
A[0] = C[0][0] - p;
for(ll i = 0;i < N;i++) A[i] = C[i][0] - B[0];
for(ll i = 0;i < N;i++) B[i] = C[0][i] - A[0];
b = 1;
for(ll i = 0;i < N;i++) if(0 > A[i]) b = 0;
for(ll i = 0;i < N;i++) if(0 > B[i]) b = 0;
for(ll i = 0;i < N;i++){
for(ll j = 0;j < N;j++){
if(A[i] + B[j] != C[i][j]){
b = 0;
break;
}
}
if(b == 0) break;
}
if(b){
cout << "Yes\n";
for(ll i = 0;i < N - 1;i++) cout << A[i] << " ";
cout << A[N - 1] << endl;
for(ll i = 0;i < N - 1;i++) cout << B[i] << " ";
cout << B[N - 1] << endl;
return 0;
}
p = MAX;
for(ll i = 0;i < N;i++) p = min(p,C[0][i]);
B[0] = p;
A[0] = C[0][0] - p;
for(ll i = 0;i < N;i++) A[i] = C[i][0] - B[0];
for(ll i = 0;i < N;i++) B[i] = C[0][i] - A[0];
b = 1;
for(ll i = 0;i < N;i++) if(0 > A[i]) b = 0;
for(ll i = 0;i < N;i++) if(0 > B[i]) b = 0;
for(ll i = 0;i < N;i++){
for(ll j = 0;j < N;j++){
if(A[i] + B[j] != C[i][j]){
b = 0;
break;
}
}
if(b == 0) break;
}
if(b){
cout << "Yes\n";
for(ll i = 0;i < N - 1;i++) cout << A[i] << " ";
cout << A[N - 1] << endl;
for(ll i = 0;i < N - 1;i++) cout << B[i] << " ";
cout << B[N - 1] << endl;
return 0;
}
A[0] = p;
B[0] = C[0][0] - p;
for(ll i = 0;i < N;i++) A[i] = C[i][0] - B[0];
for(ll i = 0;i < N;i++) B[i] = C[0][i] - A[0];
b = 1;
for(ll i = 0;i < N;i++) if(0 > A[i]) b = 0;
for(ll i = 0;i < N;i++) if(0 > B[i]) b = 0;
for(ll i = 0;i < N;i++){
for(ll j = 0;j < N;j++){
if(A[i] + B[j] != C[i][j]){
b = 0;
break;
}
}
if(b == 0) break;
}
if(b){
cout << "Yes\n";
for(ll i = 0;i < N - 1;i++) cout << A[i] << " ";
cout << A[N - 1] << endl;
for(ll i = 0;i < N - 1;i++) cout << B[i] << " ";
cout << B[N - 1] << endl;
return 0;
}
cout << "No\n";
}
|
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1LL << 60;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
int main()
{
int N, X; cin >> N >> X;
int A;
string ans = "";
for (int i = 0; i < N; ++i) {
cin >> A;
if (A != X) {
if (ans == "") {
ans += to_string(A);
} else {
ans += " " + to_string(A);
}
}
}
cout << ans << endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define INF64 (long long)1e18
#define INF (int)1e9
#define ll long long int
#define ld long double
const int dx8[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy8[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dx4[4] = {1,0,-1,0};
const int dy4[4] = {0,1,0,-1};
//debug.....................................
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
//............................................
void fastio(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
void solveTest(){
int n,x;
cin>>n>>x;
vector<int> a;
for(int i=0;i<n;i++){
int y;
cin>>y;
if(y!=x){
a.push_back(y);
}
}
for(int i:a){
cout<<i<<" ";
}
return;
}
int main(){
fastio();
int t=1;
//cin>>t;
while(t--){
solveTest();
}
return 0;
} |
#include <bits/stdc++.h>
using namespace std;
int main(){
int N;
cin >> N;
vector<long long> b(N),M(N);
long long A;
cin >> A;
b.at(0) = A;
for(int i=1; i<N; i++){
long long a;
cin >> a;
b.at(i) = b.at(i-1) + a;
}
M.at(0) = A;
for(int i=1; i<N; i++){
M.at(i) = max(M.at(i-1), b.at(i));
}
long long ans = 0;
long long x = 0;
for(int i=0; i<N; i++){
ans = max(ans, x + M.at(i));
x = x + b.at(i);
}
cout << ans;
}
| #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define VI vector<ll>
#define PB push_back
#define MM multimap
#define M map
#define MOD 100000007
#define PRII pair<ll,ll>
#define REP(i,n) for(ll i = 0;i<n;++i)
#define RES(i,n) for(ll i = n-1;i>=0;--i)
const int MAXN = 1e6+10;
const ull inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double pi = acos(-1);
const double e = exp(1);
int sign(double a) {
if (a < -eps) return -1;
else if (a > eps) return 1;
else return 0;
}
struct Vector{
double x,y;
};
double dis(Vector a,Vector b){
return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));
}
ll POW(ll a,ll b,ll m){
if(!b)return 1;
ll ans = POW(a*a%m,b/2,m);
return (b%2?ans*a%m:ans);
}
bool is_prime(ll n){
if(n<=1)
return 0;
for(int i = 2;i*i<=n;++i)
if(n%i==0)
return 0;
return 1;
}
ll t;
struct job{
double c,w;
}a[50];
bool cmp(job a,job b){
return a.c/a.w < b.c/b.w;
}
ull Stoi(string s){
reverse(s.begin(),s.end());
stringstream ss(s);
ull num = 0;
ss>>num;
return num;
}
vector<pair<ll,ll>>v;
int main() {
ll n;
cin>>n;
n = pow(2,n);
REP(i,n){
ll t;
cin>>t;
v.PB({t,i+1});
}
while(v.size()>2){
n/=2;
for(int i = 0;i<n;i++){
if(v[i].first>v[i+1].first)
v.erase(v.begin()+i+1);
else
v.erase(v.begin()+i);
}
}
cout<<(ll)(v[0].first>v[1].first?v[1].second:v[0].second);
} |
#include<iostream>
#include<vector>
#include<string>
#include<queue>
using ll = long long;
ll n;
std::vector<ll> p;
std::queue<std::pair<ll, ll>> que; // end index, size
// return inversion number
ll merge_sort(std::vector<ll> &a, ll pos = 0){
ll size = a.size();
if(size <= 1) return 0LL;
ll cnt = 0LL;
std::vector<ll> b(a.begin(), a.begin() + size / 2);
std::vector<ll> c(a.begin() + size / 2, a.end());
cnt += merge_sort(b, pos);
cnt += merge_sort(c, pos + size / 2);
int bi = 0, ci = 0;
// merge array
for(int ai = 0; ai < size; ai++){
if(bi < b.size() && (ci == c.size() || b[bi] <= c[ci])){
a[ai] = b[bi];
bi++;
}else{
cnt += size / 2 - bi;
que.emplace(pos + b.size() + ci, size / 2 - bi);
a[ai] = c[ci];
ci++;
}
}
return cnt;
}
int main(){
std::cin >> n;
p.resize(n);
for(int i = 0; i < n; i++){
std::cin >> p[i];
}
// calculate inversion number
ll invNum = merge_sort(p);
if(invNum == n - 1){
std::vector<bool> flag(n + 1, false);
std::queue<ll> ans;
while(!que.empty()){
auto p = que.front();
que.pop();
for(int i = 0; i < p.second; i++){
if(!flag[p.first - i]){
ans.push(p.first - i);
flag[p.first - i] = true;
}else{
std::cout << -1 << std::endl;
return 0;
}
}
}
while(!ans.empty()){
std::cout << ans.front() << std::endl;
ans.pop();
}
}else{
std::cout << -1 << std::endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int long long
#pragma GCC optimize("O2")
#define CLR(s) memset(&s, 0, sizeof(s))
#define sz(x) ((long long)(x).size())
#define all(x) x.begin(),x.end()
#define rall(x) (x).rbegin(), (x).rend()
#define trav(a,x) for (auto& a : x)
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define leastsigbit(x) __builtin_ffs(x)
const int MOD = 1e9 + 7;
#define hmap unordered_map
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define pii pair<ll,ll>
#define vpii vector<pii>
#define tt ll tt;cin >> tt;while(tt--)
#define fio ios::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
#define D1(x) { cerr << " [" << #x << ": " << x << "]\n"; }
#define D2(x) { cerr << " [" << #x << ": "; for(auto it:x) cerr << it << " "; cerr << "]\n";}
const double PI = acos(-1);
ll add(ll x, ll y) {ll res=x+y; return(res>=MOD?res-MOD:res);}
ll mul(ll x, ll y) {ll res=x*y; return(res>=MOD?res%MOD:res);}
ll sub(ll x, ll y) {ll res=x-y; return(res<0?res+MOD:res);}
ll power(ll a,ll b,ll m=MOD){ ll ans=1; a=a%m; while(b>0) { if(b&1) ans=(1ll*a*ans)%m; b>>=1;a=(1ll*a*a)%m;}return ans;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
ll lcm( ll x, ll y) { return (x*y)/gcd(x,y);}
bool isprime(ll n){if(n < 2) return 0; ll i = 2; while(i*i <= n){if(n%i == 0) return 0; i++;} return 1;}
bool isPowerOfTwo(int x)
{
/* First x in the below expression is for the case when x is 0 */
return x && (!(x&(x-1)));
}
double distform(int x, int y, int z, int w) {//(x1,y1,x2,y2)
return sqrt(1. * pow(x-z,2) + 1. * pow(y-w,2));
}
int dx[] = {-1,1,0,0,-1,1,1,-1};
int dy[] = {0,0,1,-1,1,1,-1,-1};
const int MAXN = 2e5+ 10;
const ll inf = 1e18;
template<typename T,typename T1>T amax(T &a,T1 b){if(b>a)a=b;return a;}
template<typename T,typename T1>T amin(T &a,T1 b){if(b<a)a=b;return a;}
int intlog(double base, double x) {
return (int)(log(x) / log(base));
}
void read(vi & a) { for (int i = 0; i < sz(a); ++i) cin >> a[i];}
void solve() {
int n;
cin >> n;
vi a(n); read(a);
sort(all(a));
vi suff(n,0);
for (int i = n - 1; i >= 0; --i) {
if(i < n - 1) {
suff[i] = suff[i + 1] + a[i];
}
else suff[i] = a[i];
}
int ans = 0;
for (int j = 0; j < n - 1; ++j) {
ans += abs(suff[j + 1] - a[j] * (n - 1 - j));
}
cout << ans << endl;
}
int32_t main() {
fio
solve();
}
|
Subsets and Splits