code_file1
stringlengths 80
4k
| code_file2
stringlengths 91
4k
| similar_or_different
int64 0
1
|
---|---|---|
#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
int main(){
ios::sync_with_stdio(false);
int n;string s;
cin>>n>>s;
int num=0;
long long ans=1;
for(int i=1;i<=n;++i) ans=ans*i%mod;
for(int i=0;i<2*n;++i){
if((s[i]=='W')^num&1) ans=ans*num%mod,num--;
else num++;
}
printf("%lld\n",num?0:ans);
} | #include <cassert>
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#include <random>
#include <memory>
#include <utility>
#include <limits>
#include "limits.h"
#define rep(i, a, b) for (long long (i) = (a); i < (b); i++)
#define all(i) i.begin(), i.end()
#define debug(...) std::cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
void debug_out(){std::cerr<<std::endl;}
template<typename Head,typename... Tail>
void debug_out(Head h,Tail... t){
std::cerr<<" "<<h;
debug_out(t...);
}
template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, std::pair<T1, T2> pa) {
return os << pa.first << " " << pa.second;
}
template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> vec) {
for (int i = 0; i < vec.size(); i++)os << vec[i] << (i + 1 == vec.size() ? "" : " ");
return os;
}
template<typename T1,typename T2>
inline bool chmax(T1& a,T2 b){return a<b && (a=b,true);}
template<typename T1,typename T2>
inline bool chmin(T1& a,T2 b){return a>b && (a=b,true);}
long long pow_mod(long long a, long long b, long long mod=-1) {
if(b==0)return 1;
if ((a == 0)||(mod!=-1&&(a+mod)%mod==0)) {
return 0;
}
long long x = 1;
while (b > 0) {
if (b & 1) {
x = (mod!=-1)?(x * a) % mod:x*a;
}
a = (mod!=-1)?(a * a) % mod:a*a;
b >>= 1;
}
return x;
}
// const long long MOD = 998244353;
const long long MOD = 1e9 + 7;
using ll = long long;
using P = std::pair<long long, long long>;
ll gcd(ll a,ll b){
if(a%b==0)return b;
return gcd(b,a%b);
}
int main() {
std::cin.tie(0);
std::ios::sync_with_stdio(false);
ll n;
std::cin>>n;
std::vector<long double> a(n);
rep(i,0,n)std::cin>>a[i];
long double sum=0;
rep(i,0,n)sum+=a[i];
sum/=(long double)n;
long double min=1e9;
rep(i,0,n)chmin(min,std::abs(a[i]-sum));
rep(i,0,n){
if(std::abs(a[i]-sum)==min){
std::cout<<i<<"\n";
return 0;
}
}
return 0;
} | 0 |
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int N, M;
cin>>N>>M;
vector<vector<long long>> T(3*N, vector<long long>(6*N+1));
T[0][3*N+1] = 1;
T[1][3*N-1] = 1;
T[2][3*N] = 2;
for (int i=0; i<3*N; i++)
for (int j=0; j<=6*N; j++)
if (T[i][j]>0)
for (int k=1; k<=3; k++)
if (i+k<3*N)
{
long long c = 1;
for (int l=0; l<k-1; l++)
c *= i+l+2;
int d = int(k==1) - int(k==2);
T[i+k][j+d] += c*T[i][j];
T[i+k][j+d] %= M;
}
long long ans = 0;
for (int i=3*N; i<=6*N; i++)
{
ans += T[3*N-1][i];
ans %= M;
}
cout<<ans<<endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define mid ((s + e) / 2)
#define makefast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define comp(x) (lower_bound(vec.begin(), vec.end(), x) - vec.begin())
#define lc (v * 2)
#define rc (v * 2 + 1)
long long M = 1e9 + 7;
//>>>>>>>>>>>>>>>>>>>
#define int long long
const int N = 1e6 + 7;
int n;
int pw(int a, int b) {
int res = 1;
while (b) {
if (b % 2)
res = (res * a) % M;
b /= 2;
a = (a * a) % M;
}
return res;
}
int f[N], rf[N];
int chs(int a, int b) {
if (a > b)
return 0;
if (a < 0 || b < 0 || a >= N || b >= N) {
cout << "FITA";
exit(0);
}
return (((f[b] * rf[a]) % M) * rf[b - a]) % M;
}
int sm3[N], sm2[N];
int32_t main() {
makefast;
cin >> n >> M;
f[0] = rf[0] = 1;
for (int i = 1; i < N; i++) {
f[i] = (f[i - 1] * i) % M;
rf[i] = (rf[i - 1] * pw(i, M - 2)) % M;
}
sm3[0] = 1;
for (int i = 1; i < 10000; i++)
sm3[i] = (((sm3[i - 1] * chs(3, 3 * i)) % M) * pw(i, M - 2)) % M;
sm2[n + 1] = 1;
for (int i = n; i >= 0; i--)
sm2[i] = (((sm2[i + 1] * chs(2, 3 * n - 2 * (n - i))) % M) * pw(n - i + 1, M - 2)) % M;
int res = 0;
for (int b = 0; b <= n; b++)
for (int a = b; a + 2 * b <= 3 * n; a++) {
if ((a + 2 * b) % 3)
continue;
int c = (3 * n - (a + 2 * b)) / 3;
int t = sm2[n - b + 1];
t = (t * chs(a, 3 * n - 2 * b)) % M;
t = (t * ((sm3[c] * pw(2, c)) % M)) % M;
res = (t + res) % M;
// cout << a << " " << b << " " << c << " > " << t << " sm2 : " << sm2[n - b + 1] << " chs : " << chs(a, 3 * n - 2 * b) << " sm3 : " << sm3[c] << endl;
}
cout << res;
return 0;
}
| 1 |
#include<iostream>
#include<math.h>
#include<algorithm>
#include<stdint.h>
#include<vector>
#include<deque>
#include<stack>
#include<functional>
#include<string>
#include<cstring>
#include<time.h>
#include<array>
#include<iomanip>
#include<list>
#include<set>
#include<map>
#include<random>
#include<unordered_map>
#include<unordered_set>
#include<bitset>
#include <queue>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
using ldouble = long double;
//BDD,ZDD,kdtree, bdtree,bicalc, bolonoy, doloney, tree, chinesemod,segmenttree,daikusutora, saidairyuu, 2bugurahu, heirokenshutu, topologicalsort, kyourenketuseibun
#define REP(i,a,b) for(int i = a; i < b; ++i)
#define PRI(s) cout << s << endl
#define PRIY PRI("Yes")
#define PRIN PRI("No")
using b = bitset<6400 * 2 + 64>;
int main() {
ll H, W;
vector<vector<b>> f;
vector<vector<ll>> a, b;
cin >> H >> W;
f.resize(H);
a.resize(H);
b.resize(H);
REP(i, 0, H) {
f[i].resize(W);
a[i].resize(W);
REP(j, 0, W) {
f[i][j].reset();
cin >> a[i][j];
}
}
REP(i, 0, H) {
b[i].resize(W);
REP(j, 0, W) {
cin >> b[i][j];
}
}
int i0 = 6400;
f[0][0][i0 + b[0][0] - a[0][0]] = true;
f[0][0][i0 - b[0][0] + a[0][0]] = true;
auto bs = [](auto& b, int d) {
if (d >= 0) return b << d;
else return b >> -d;
};
REP(j, 1, W) f[0][j] = bs(f[0][j - 1], b[0][j] - a[0][j]) | bs(f[0][j - 1], -b[0][j] + a[0][j]);
REP(i, 1, H) f[i][0] = bs(f[i - 1][0], b[i][0] - a[i][0]) | bs(f[i - 1][0], -b[i][0] + a[i][0]);
REP(i, 1, H)REP(j, 1, W) {
f[i][j] = bs(f[i][j - 1], b[i][j] - a[i][j]) | bs(f[i - 1][j], b[i][j] - a[i][j]);
f[i][j] |= bs(f[i][j - 1], -b[i][j] + a[i][j]) | bs(f[i - 1][j], -b[i][j] + a[i][j]);
}
for (int i = 0; i <= 6400; ++i) {
if (f[H - 1][W - 1][i0 + i] || f[H - 1][W - 1][i0 - i]) {
PRI(i);
return 0;
}
}
return 0;
}
| #include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <cmath>
#include <unordered_set>
#define YesNo(b) ((b) ? "Yes" : "No")
#define YESNO(b) ((b) ? "YES" : "NO")
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
template<template<class...> class ContT, class ValueT, size_t Dim>
struct vec_type
{
using type = ContT<typename vec_type<ContT, ValueT, Dim-1>::type>;
};
template<template<class...> class ContT, class ValueT>
struct vec_type<ContT, ValueT, 0>
{
using type = ValueT;
};
template<class ValueT, size_t Dim>
using vec_t = typename vec_type<std::vector, ValueT, Dim>::type;
template<class ValueT, size_t Dim, class = typename std::enable_if<Dim == 0>::type>
auto make_vec(ValueT value = ValueT()) -> ValueT
{
return value;
}
template<class ValueT, size_t Dim, class... Rest>
auto make_vec(size_t size0, Rest... rest)
{
auto vec = make_vec<ValueT, Dim-1>(rest...);
return std::vector(size0, vec);
}
int diffabs(int l, int r)
{
if (l < r) return r-l;
else return l-r;
}
int iabs(int i)
{
if (i > 0) return i;
else return -i;
}
int main()
{
int H, W;
cin >> H >> W;
vec_t<int,2> A = make_vec<int,2>(H, W);
vec_t<int,2> B = make_vec<int,2>(H, W);
for (int h = 0; h < H; ++h)
for (int w = 0; w < W; ++w)
cin >> A[h][w];
for (int h = 0; h < H; ++h)
for (int w = 0; w < W; ++w)
cin >> B[h][w];
vec_t<bool,3> dp = make_vec<bool,3>(H, W, (H+W)*80, false);
for (int h = 0; h < H; ++h)
{
for (int w = 0; w < W; ++w)
{
int d = diffabs(A[h][w], B[h][w]);
if (h == 0 && w == 0)
{
dp[h][w][d] = true;
}
if (w > 0)
{
for (size_t i = 0; i < dp[h][w-1].size(); ++i)
{
if (dp[h][w-1][i])
{
dp[h][w][i+d] = true;
dp[h][w][iabs(i-d)] = true;
}
}
}
if (h > 0)
{
for (size_t i = 0; i < dp[h-1][w].size(); ++i)
{
if (dp[h-1][w][i])
{
dp[h][w][i+d] = true;
dp[h][w][iabs(i-d)] = true;
}
}
}
}
}
const auto &hw = dp[H-1][W-1];
auto it = find_if(begin(hw), end(hw), [](auto b){ return b; });
int ans = std::distance(begin(hw), it);
cout << ans << endl;
} | 1 |
#include<cstdio>
#include<algorithm>
using namespace std;
int main(void)
{
int n,y;
int i,j,flg=0;
scanf("%d %d",&n,&y);
for(i=0;i<=n;i++){
for(j=0;j<=n;j++){
if(n-(i+j)<0) {
break;
}
if(i+j+n-(i+j)==n&&y==i*10000+j*5000+(n-(i+j))*1000){
flg=1;
break;
}
}
if(flg==1) break;
}
if(flg==1){
printf("%d %d %d\n",i,j,n-(i+j));
}
else if(flg==0||flg==2){
printf("-1 -1 -1\n");
}
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>;
int main() {
int N, Y;
cin >> N >> Y;
for (int x = 0; x <= N; x++) {
for (int y = 0; y <= N; y++) {
if (N - x - y < 0) continue;
int sum = 10000 * x + 5000 * y + 1000 * (N - x - y);
if (sum == Y){
cout << x << " " << y << " " << N - x - y << endl;
return 0;
}
}
}
cout << -1 << " " << -1 << " " << -1 << endl;
return 0;
} | 1 |
#include<bits/stdc++.h>
#define ll long long
#define fornum(A,B,C) for(A=B;A<C;A++)
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>
using namespace std;
/////////////////////////////////////////////////////
#define MOD ((ll)1e9+7)
ll N,S;
vector<pll> xpv[2];
ll i, j, k,ans;
bool ck(ll t,ll aa,ll bb){
return t ? (aa <= bb) : (aa < bb);
}
int main(){
//printf("%lld\n", MOD);
scanf("%lld%lld", &N,&S);
fornum(i,0,N){
ll x, p;
scanf("%lld%lld", &x, &p);
xpv[S < x].push_back(mp(abs(x - S), p));
}
sort(xpv[0].begin(), xpv[0].end(), greater<pll>());
sort(xpv[1].begin(), xpv[1].end(), greater<pll>());
if(xpv[0].size()*xpv[1].size()==0){
printf("%lld",xpv[0].size()?xpv[0][0].first:xpv[1][0].first);
return 0;
}
ll n = 0, t = 0;
ll ii[]={0,0};
n= xpv[0][0].second+xpv[1][0].second;
if(xpv[0][0].second<xpv[1][0].second){
t = 0;
}else{
t = 1;
}
ii[t]++;
ans += xpv[t][0].first + xpv[!t][0].first * 2;
//printf("%lld,%lld\n", xpv[t][0].second, xpv[!t][0].second);
while(true){
while(ii[t]<xpv[t].size()&&ck(t,xpv[t][ii[t]].second,n)){
//printf("%lld,%lld\n", xpv[t][ii[t]].second, n);
n += xpv[t][ii[t]].second;
ii[t]++;
}
//printf("%lld,%lld,%lld\n", t, ii[t],n);
if(ii[t]==xpv[t].size())
break;
n += xpv[t][ii[t]].second;
ans += xpv[t][ii[t]].first*2;
t = !t;
ii[t]++;
}
printf("%lld", ans);
return 0;
} | /*
* Since g++10 is released, some characters is not valid inside #if 0 :(
* So, why not using clang++? :D
* Date:
echo -n ' ' && date +%Y.%m.%d # Just Run this (Type !!sh in Vim).
* Solution:
* 考虑边界,推导结论,子问题递归求解
如果 S <= p[1] 或者 S >= p[n] ,显然所有人投票都是一个方向,不妨假设 p[1] < S < p[n] 。
考虑处在最边上的 1 号点和 n 号点,如果 a[1] >= a[n] ,那么一定先到 1 ,否则先到 n 。归
纳证明,如果 n = 2 显然成立,对于 n >= 3 ,若 S >= a[n - 1] 显然成立,否则 S < a[n - 1] ,
此时考虑 1 和 n - 1 ,如果先到 n - 1 就会回到 S >= a[n - 1] 的情况。
不妨假设 a[1] >= a[n] ,那么到 n 的时间就一定是到 1 的时间加上 1 到 n 的距离,那么 n 想
要最小化到达 n 的时间其实就是等价于最小化到达 1 的时间,可以赋值 a[1] = a[1] + a[n] 并赋
值 n = n - 1 转换为子问题,递归求解,可以求出每个点的到达时间。
* Digression:
* CopyRight:
▁▃▄▄▄▃▃▃▃▄▶
▗▇▀▔ ▔▔▔▔
▄▛ ▃▅━━■▄▂
▟▊ ▐▘ ▀▙
▟▜▌ ▐▖ ▋ ▐▍
▟▘ ▜ ▝▀▇▆●▘ ▐▌
▗▟▘ ▜▃ ▁▅▛
▔▀▼▅▄▃▃██▅▄▄▄▅■▀▔
▔▔▔▔▔▔
*/
#include <cstdio>
#include <algorithm>
#define debug(...) fprintf(stderr, __VA_ARGS__)
typedef long long ll;
struct {
inline operator int () { int x; return scanf("%d", &x), x; }
inline operator ll () { ll x; return scanf("%lld", &x), x; }
template<class T> inline void operator () (T &x) { x = *this; }
template<class T, class ...A> inline void operator () (T &x, A &...a)
{ x = *this; this -> operator () (a...); }
} read;
const int maxn = 1000005;
int p[maxn];
ll a[maxn];
ll t[maxn];
void solve (int l, int r, int S) {
if (S <= p[l]) {
for (int i = l; i <= r; i ++)
t[i] = p[i] - S;
return;
}
if (S >= p[r]) {
for (int i = l; i <= r; i ++)
t[i] = S - p[i];
return;
}
if (a[l] >= a[r]) {
a[l] += a[r];
solve(l, r - 1, S);
t[r] = t[l] + p[r] - p[l];
} else {
a[r] += a[l];
solve(l + 1, r, S);
t[l] = t[r] + p[r] - p[l];
}
}
int main () {
int n = read, S = read;
for (int i = 1; i <= n; i ++) read(p[i], a[i]);
solve(1, n, S);
ll ans = 0;
for (int i = 1; i <= n; i ++) ans = std::max(ans, t[i]);
printf("%lld\n", ans);
}
| 1 |
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <map>
#include <numeric>
#include <string>
#include <cmath>
#include <iomanip>
#include <queue>
#include <list>
#include <set>
#include <stack>
#include <cctype>
#include <cmath>
#include <bitset>
using namespace std;
/* typedef */
typedef long long ll;
/* constant */
const int INF = 1 << 30;
const int NIL = -1;
const int MAX = 100000;
const int mod = 1000000007;
const double pi = 3.141592653589;
/* global variables */
vector<int> parent(MAX), prenum(MAX), lowest(MAX);
vector< vector<int> > Adj(MAX);
vector<bool> visited(MAX, false);
int N;
int timer;
/* function */
void dfs(int cur, int prev);
void art_points();
/* main */
int main() {
int m;
cin >> N >> m;
for (int i = 0; i < m; i++) {
int v, u;
cin >> v >> u;
Adj[v].push_back(u);
Adj[u].push_back(v);
}
art_points();
}
void dfs(int cur, int prev) {
// after visited node cur
prenum[cur] = lowest[cur] = timer;
timer++;
visited[cur] = true;
int next;
for (int i = 0; i < Adj[cur].size(); i++) {
next = Adj[cur][i];
if ( !visited[next] ) {
// before visiting next node
parent[next] = cur;
dfs(next, cur);
// after visiting next node
lowest[cur] = min(lowest[cur], lowest[next]);
}
else if (next != prev) {
// Edge(cur, next) is Back-Edge
lowest[cur] = min(lowest[cur], prenum[next]);
}
}
}
void art_points() {
timer = 1;
// calculate lowest
dfs(0, -1); // 0 == root
set<int> ap;
int child_of_root = 0;
for (int i = 1; i < N; i++) {
int p = parent[i];
if (p == 0) child_of_root++;
else if (prenum[p] <= lowest[i]) ap.insert(p);
}
if (child_of_root >= 2) ap.insert(0);
for (auto it = ap.begin(); it != ap.end(); it++) {
cout << *it << '\n';
}
}
| #include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
const i64 MOD = 1e9 + 7;
struct LowLink{
vector<vector<int>>& edges;
// 関節点
vector<int> art;
vector<pair<int,int>> bridge;
vector<int> used, ord, low;
int k;
void dfs(int idx, int par){
ord[idx] = k++;
low[idx] = ord[idx];
bool is_art = false;
int cnt = 0;
for(auto& to : edges[idx]){
if(ord[to] == -1){
++cnt;
dfs(to, idx);
low[idx] = min(low[idx], low[to]);
is_art |= par != -1 && low[to] >= ord[idx];
if(ord[idx] < low[to])
bridge.emplace_back(idx, to);
}else if(to != par)
low[idx] = min(low[idx], ord[to]);
}
is_art |= (par == -1 && cnt > 1);
if(is_art)
art.emplace_back(idx);
}
LowLink(vector<vector<int>>& edges) :
edges(edges),
ord(edges.size(), -1),
low(edges.size(), 0),
k(0)
{
for(int i = 0; i < edges.size(); ++i)
if(ord[i] == -1)
dfs(i, -1);
for(auto& b : bridge)
b = make_pair(min(b.first, b.second), max(b.first, b.second));
sort(art.begin(), art.end());
sort(bridge.begin(), bridge.end());
}
};
signed main(){
int n, m;
cin >> n >> m;
vector<pair<int,int>> v(m);
vector<vector<int>> edges(n);
for(int i = 0; i < m; ++i){
vector<int> l(2);
for(auto& x : l)
cin >> x;
sort(l.begin(), l.end());
v[i] = make_pair(l.front(), l.back());
edges[l[0]].emplace_back(l[1]);
edges[l[1]].emplace_back(l[0]);
}
LowLink ll(edges);
for(auto& x : ll.art)
cout << x << endl;
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b;
cin >>a;
cout << a * a * a<<endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
long long mi[233];
long long work(int l,int r,long long dlt)
{
if ((l>=r)&&(dlt)) return 0;
if (l==r) return 10-(l==0);
if (l>r) return 1;
int t=(10-dlt%10)%10;
dlt-=t*(mi[r-l]-1);
return (10-(l==0)-t)*work(l+1,r-1,abs(dlt)/10);
}
int main()
{
mi[0]=1;for (int i=1;i<=18;i++) mi[i]=mi[i-1]*10;
int d;scanf("%d",&d);long long ans=0;
for (int i=0;i<=17;i++) ans+=work(0,i,d);
cout<<ans+(d==0)<<endl;
} | 0 |
#include<bits/stdc++.h>
#define fo(i,a,b) for(int i= a ; i < b ; ++i)
#define rep(i,n) fo(i,0,n)
#define pln(n) printf("%lld\n",n)
#define sll(n) scanf("%lld",&n)
#define ss(n) scanf("%s",n)
#define vi vector < int >
#define pii pair < int , int >
#define pb push_back
#define mp make_pair
#define ll long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
#define dbg(x) { cout<< #x << ": " << (x) << endl; }
#define dbg2(x,y) { cout<< #x << ": " << (x) << " , " << #y << ": " << (y) << endl; }
#define dbg3(x,y,z) { cout<< #x << ": " << (x) << " , " << #y << ": " << (y) <<" , "<< #z <<": "<<(z)<<endl; }
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define vll vector<long long>
#define vvi vector<vector<int>>
#define si set<int>
#define tr(c, it) \
for(decltype(c.begin()) it = c.begin(); it!= c.end(); it++)
#define pis pair<int,string>
#define present(c, x) (c.find(x) != c.end())
#define cpresent(c, x) (find(all(c),x) != c.end())
#define mod (int)(1e9 + 7)
using namespace std;
double dp[301][301][301];
double dfs(int a, int b, int c, int N){
if(a == 0 && b == 0 && c == 0)return 0.0;
if(dp[a][b][c] > -0.5)return dp[a][b][c];
double res = 0;
double ks = (double)N / (a + b + c);
if(a){
res += (ks + dfs(a-1,b,c, N)) * (double)(a)/(a + b + c);
}
if(b){
res += (ks + dfs(a+1,b-1,c, N)) * (double)(b)/(a + b + c);
}
if(c){
res += (ks + dfs(a,b+1,c-1, N)) * (double)(c)/ (a + b + c);
}
return dp[a][b][c] = res;
}
int main(){
memset(dp, -1, sizeof(dp));
int n;
cin >> n;
vector<int> a(n);
cout << fixed << setprecision(15);
int one = 0;
int two = 0;
int three = 0;
for(int i = 0; i < n; i++){
cin >> a[i];
if(a[i] == 1)one++;
if(a[i] == 2)two++;
if(a[i] == 3)three++;
}
// dp[i][j][k] = expected no of operations with i 1's , j 2's , k 3's
double res = dfs(one, two, three, n);
cout << res << endl;
} | #include <bits/stdc++.h>
using namespace std;
#define ld long double
#define ll long long
#define pb push_back
#define F first
#define S second
const int A = 3e2 + 2;
const int mod = 1e9 + 7;
int n;
bool vis[A][A][A];
ld mem[A][A][A];
ld solve (int c1, int c2, int c3) {
if (c1 + c2 + c3 == 0) return 0;
if (vis[c1][c2][c3])
return mem[c1][c2][c3];
vis[c1][c2][c3] = 1;
int c0 = n - (c1 + c2 + c3);
ld ret = 1;
if (c1)
ret += solve(c1 - 1, c2, c3) * (c1 / (n * 1.0));
if (c2)
ret += solve(c1 + 1, c2 - 1, c3) * (c2 / (n * 1.0));
if (c3)
ret += solve(c1, c2 + 1, c3 - 1) * (c3 / (n * 1.0));
ret /= (1 - (c0 / (n * 1.0)));
return mem[c1][c2][c3] = ret;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
int freq[4] = {};
for (int i = 0, x; i < n; i++) {
cin >> x;
freq[x]++;
}
cout << setprecision(9) << fixed << solve(freq[1], freq[2], freq[3]);
return 0;
} | 1 |
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<list>
#include<cstdlib>
template<typename T>
void fin(T const& t){ std::cout << t << std::endl; exit(0); }
int msb32(uint32_t x) { return 31 - __builtin_clz(x); }
int msb(uint64_t x) {
if(x < (1ULL<<32)) return msb32((uint32_t)x);
return 32 +msb((uint32_t)(x>>32));
}
struct bit_base {
uint64_t bases[64];
bit_base() { std::fill_n(bases, 64, (uint64_t)0); }
uint64_t add(uint64_t n) {
int x = msb(n);
while(bases[x] > 0) {
n ^= bases[x];
if(n == 0) return 0;
x = msb(n);
}
return bases[x] = n;
}
};
int main() {
std::cin.tie(0); std::ios::sync_with_stdio(false);
int T; std::cin >> T;
for(int t = 0; t < T; ++t) {
int N; std::cin >> N;
std::vector<uint64_t> A(N);
for(auto& Ai: A) std::cin >> Ai;
std::string S; std::cin >> S;
bit_base b;
for(int n = N-1; n >= 0; --n) {
if(S[n] == '0') {
b.add(A[n]);
} else {
if(b.add(A[n]) > 0) { std::cout << "1\n"; break; }
}
if(n == 0) { std::cout << "0\n"; break; }
}
}
return 0;
}
| #include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define all(x) x.begin(),x.end()
#define cmin(a,b) (a>b?a=b:a)
#define cmax(a,b) (a<b?a=b:a)
#define mem(a,k) memset(a,k,sizeof(a))
#define lop(i,s,t) for(int i=s;i<(t);++i)
#define rep(i,s,t) for(int i=s;i<=(t);++i)
#define dec(i,s,t) for(int i=s;i>=(t);--i)
#define fore(i,v) for(int i=g[v],d=es[i].d;i;i=es[i].nxt,d=es[i].d)
using namespace std;
#define Pr(f,...) fprintf(stderr,f,##__VA_ARGS__),fflush(stderr)
typedef long long ll;
template<typename T>
void read(T &x){
x=0;
char c;
for(c=getchar();!isdigit(c);c=getchar());
for(;isdigit(c);c=getchar())x=x*10+c-'0';
}
const int N=205,P=1e9+7,L=62;
int mul(int a,int b){ return 1ll*a*b%P; }
//int add(int a,int b){ a+=b; return a>=P?a-P:a; }
int sub(int a,int b){ a-=b; return a<0?a+P:a; }
int gcd(int a,int b){ return !b?a:gcd(b,a%b); }
int n,T;
ll a[N],pr[L+2],bk[L+2],ans;
char s[N];
void add(ll *b,ll x){
dec(i,L,0){
if((x>>i)&1){
if(b[i])x^=b[i];
else {b[i]=x; break; }
}
}
}
bool test(ll *b,ll x){
dec(i,L,0)if((x>>i)&1){
x^=b[i];
}
return x==0;
}
bool equal(ll *b1,ll *b2){
dec(i,L,0)if(b1[i]){
if(!test(b2,b1[i]))return false;
}
return true;
}
int main(int argc,char *argv[]){
#ifdef CURIOUSCAT
freopen("d.in","r",stdin);
#endif
read(T);
while(T--){
read(n);
rep(i,1,n)read(a[i]);
scanf("%s",s+1);
ans=0;
mem(bk,0);
dec(i,n,1){
if(s[i]=='0')add(bk,a[i]);
else ans|=!test(bk,a[i]);
}
cout<<ans<<endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
const int mod = 998244353;
int main() {
int n, d0;
cin >> n >> d0;
if(d0 != 0) {
cout << 0 << endl;
return 0;
}
vector<int> d(100005);
d.at(0) = 1;
int max_d = 0;
for(int i = 1; i < n; ++i) {
int di;
cin >> di;
max_d = max(max_d, di);
d.at(di)++;
}
if(d.at(0) != 1) {
cout << 0 << endl;
return 0;
}
ll ans = 1;
int i = 1;
while(i < n) {
if(d.at(i) == 0) break;
rep(j, d.at(i)) {
ans *= d.at(i-1);
ans %= mod;
}
++i;
}
if(i < max_d) cout << 0 << endl;
else cout << ans << endl;
return 0;
} | #include <iostream>
#include <vector>
#include <deque>
#include <algorithm>
#include <numeric>
#include <string>
#include <cstring>
#include <list>
#include <unordered_set>
#include <tuple>
#include <cmath>
#include <limits>
#include <type_traits>
#include <iomanip>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <set>
#include <bitset>
#include <regex>
#include <random>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n)for(ll i=0;i<n;++i)
#define exout(x) printf("%.10f\n", x)
const double pi = acos(-1.0);
const ll MOD = 1000000007;
const ll INF = 1e18;
const ll MAX_N = 201010;
//最大公約数
ll gcd(ll x, ll y) {
return y ? gcd(y, x % y) : x;
}
ll lcm(ll x, ll y) {
if (x == 0 || y == 0)return 0;
return (x / gcd(x, y) * y);
}
ll dx[4] = { 0,0,-1,1 };
ll dy[4] = { -1,1,0,0 };
ll ans[110][110];
//long longしか使わない
//素数は1より大きい
//lower_boundは指定したkey以上の要素の一番左のイテレータをかえす
//upper_boundは指定したkeyより大きい要素の一番左のイテレータをかえす
int main() {
ll n;
cin >> n;
vector<ll>d(n), h(n);
ll maxh = 0;
rep(i, n) {
cin >> d[i];
if (d[0] != 0) {
cout << 0 << endl;
return 0;
}
if (i != 0 && d[i] == 0) {
cout << 0 << endl;
return 0;
}
h[d[i]]++;
maxh = max(maxh, d[i]);
}
ll ans = 1;
rep(i, maxh) {
rep(j, h[i + 1]) {
ans *= h[i];
ans %= 998244353;
}
}
cout << ans << endl;
return 0;
}
| 1 |
/* {{{ Shinobu kawaii */
/*
______ __ _ __
.' ____ \ [ | (_) [ |
| (___ \_| | |--. __ _ .--. .--. | |.--. __ _
_.____`. | .-. | [ | [ `.-. |/ .'`\ \| '/'`\ \[ | | |
| \____) | | | | | | | | | | || \__. || \__/ | | \_/ |,
\______.'[___]|__][___][___||__]'.__.'[__;.__.' '.__.'_/
*/
/* }}} */
#include <bits/stdc++.h>
using namespace std;
// #define int long long
struct Fast {Fast(){std::cin.tie(0);ios::sync_with_stdio(false);}} fast;
/* cpp template {{{ */
/* short */
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()
#define X real()
#define Y imag()
/* REPmacro */
#define REPS(i, a, n) for (int i = (a); i < (n); ++i)
#define REP(i, n) REPS(i, 0, n)
#define RREP(i, n) REPS(i, 1, n + 1)
#define DEPS(i, a, n) for (int i = (a); i >= n; --i)
#define DEP(i, n) DEPS(i, n, 0)
/* debug */
#define debug(x) cerr << x << " " << "(L:" << __LINE__ << ")" << '\n';
/* alias */
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using pii = pair<int, int>;
using D = double;
using P = complex<D>;
/* const */
const int INF = 1001001001;
const ll LINF = 1001001001001001001ll;
const int MOD = 1e9 + 7;
const D EPS = 1e-9;
const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
/* func */
inline bool inside(int y, int x, int H, int W) {return y >= 0 && x >= 0 && y < H && x < W;}
inline int in() {int x; std::cin >> x; return x;}
template <typename T> void print(T x) {std::cout << x << '\n';}
template <typename T>
void print(std::vector<T>& v, std::string s = " ") {
REP(i, v.size()) {
if (i != 0) std::cout << s;
std::cout << v[i];
}
std::cout << '\n';
}
/* }}} */
int n, m;
void warshall_floyd(vvi& d)
{
REP(k, m) REP(i, m) REP(j, m) {
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
signed main()
{
while (cin >> n >> m, n || m) {
vvi dc, dt;
dc = dt = vvi(m, vi(m, INF));
REP(i, n) {
int a = in() - 1, b = in() - 1;
dc[a][b] = dc[b][a] = in();
dt[a][b] = dt[b][a] = in();
}
warshall_floyd(dc);
warshall_floyd(dt);
int k = in();
REP(i, k) {
int a = in() - 1, b = in() - 1, r = in();
print(r ? dt[a][b] : dc[a][b]);
}
}
return 0;
} | #include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>
#define f first
#define s second
#define mp make_pair
#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
using namespace std;
typedef unsigned int uint;
typedef long long ll;
int main(){
int n,m;
while(scanf("%d%d",&n,&m), n+m){
int time[100][100] = {{0}};
int cost[100][100] = {{0}};
REP(i,n){
int a,b,c,d;
scanf("%d%d%d%d",&a,&b,&c,&d);
a--; b--;
cost[a][b] = cost[b][a] = c;
time[a][b] = time[b][a] = d;
}
int k;
scanf("%d",&k);
while(k --> 0){
int p,q,type;
int (*load)[100];
scanf("%d%d%d",&p,&q,&type);
if(type == 0) load = cost;
else load = time;
p--; q--;
int memo[100];
memset(memo, -1, sizeof(memo));
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > qq;
qq.push(mp(0, p));
while(qq.size()){
int c = qq.top().f;
int pos = qq.top().s;
qq.pop();
if(pos == q){
printf("%d\n",c);
break;
}
if(memo[pos] != -1)
continue;
memo[pos] = c;
REP(i,m){
if(memo[i] == -1 && load[pos][i] != 0)
qq.push(mp(c+load[pos][i], i));
}
}
}
}
return 0;
} | 1 |
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstring>
#include <functional>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <istream>
#include <map>
#include <math.h>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <stdint.h>
namespace asl
{
template <typename T>
using vec = std::vector<T>;
template <typename T>
std::istream &operator>>(std::istream &is, std::vector<T> &vec)
{
for (auto &value : vec)
is >> value;
return is;
}
template <typename T>
struct fenwick
{
int n;
std::vector<T> f;
fenwick(int n) : n(n), f(n + 1) {}
T query(int p)
{
T ret = 0;
for (++p; p > 0; p -= p & -p)
ret += f[p];
return ret;
}
void update(int p, T x)
{
for (++p; p <= n; p += p & -p)
f[p] += x;
}
};
template <typename T>
using BIT = fenwick<T>;
}
#include <experimental/optional>
#include <tuple>
namespace asl
{
typedef long long i64;
}
#include <random>
#include <utility>
#define endl '\n'
using namespace std;
using namespace asl;
void solve()
{
int n, q;
cin >> n >> q;
vec<int> val(n);
cin >> val;
BIT<i64> bit(n);
for (int i = 0; i < n; ++i)
bit.update(i, val[i]);
while (q--)
{
int t;
cin >> t;
if (t == 0)
{
int p, x;
cin >> p >> x;
bit.update(p, x);
}
else
{
int l, r;
cin >> l >> r;
cout << bit.query(r - 1) - bit.query(l - 1) << endl;
}
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
for (int i = 1; i <= t; ++i)
{
solve();
}
return 0;
}
| #include<iostream>
#include<vector>
#include<string>
using namespace std;
vector<string> ans;
vector<char> alp;
int main(){
for(int i = 0;i < 26;i++){
char a = 'a' + i;
alp.push_back(a);
}
for(int i = 0;i < 26;i++){
char A = 'A' + i;
alp.push_back(A);
}
while(1){
int n;
cin >> n;
if(n == 0) break;
int k[100];
for(int i = 0;i < n;i++) cin >> k[i];
string s;
cin >> s;
for(int i = 0;i < s.size();i++){
char cnum;
if(s[i] >= 'a'){
cnum = s[i] - 'a';
}else cnum = s[i] - 'A' + 26;
s[i] = alp[(cnum -k[i%n] +52)%52];
}
ans.push_back(s);
}
for(int i = 0;i < ans.size();i++)cout << ans[i] << endl;
return 0;
} | 0 |
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
#include <cmath>
using namespace std;
int main(int argc, const char *argv[]) {
vector<int> abc(3);
int k;
cin >> abc[0] >> abc[1] >> abc[2];
cin >> k;
vector<int>::iterator iter = max_element(abc.begin(), abc.end());
size_t index = distance(abc.begin(), iter);
abc[index] *= (int)pow(2.0, (double)k);
cout << accumulate(abc.begin(), abc.end(), 0) << endl;
return 0;
}
| #include<bits/stdc++.h>
using namespace std ;
const int inf = 1012345678;
int main(){
int64_t N ;
cin >> N ;
int64_t a , b , c , d , e ;
cin >> a >> b >> c >> d >> e ;
int64_t min_t = min( a , min( min(b,c),min(d,e) ) ) ;
//cout << min_t << endl ;
if(N%min_t == 0 ){
cout << N/min_t + 4 << endl ;
}
else{
cout << (N/min_t) + 4 + 1 << endl ;
}
//975529666566238
} | 0 |
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
using lint=long long;
int main(){
string s; cin>>s;
int n=s.length();
lint hist[128]={};
rep(i,n) hist[s[i]]++;
lint ans=1;
for(int c='a';c<='z';c++) for(int d=c+1;d<='z';d++) {
ans+=hist[c]*hist[d];
}
cout<<ans<<'\n';
return 0;
}
| #include<cstdio>
#include<iostream>
#include <memory>
#include<vector>
#include<algorithm>
using namespace std;
int calc(int a, int b, int ope) {
if (ope == 0) return a + b;
else return a - b;
}
int main(void) {
string S;
cin >> S;
char ope[] = {'+', '-'};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
int ans = calc(S[0] - '0', S[1] - '0', i);
ans = calc(ans, S[2] - '0', j);
ans = calc(ans, S[3] - '0', k);
if (ans == 7) {
cout << S[0] << ope[i] << S[1] << ope[j] << S[2] << ope[k] << S[3] << "=7\n";
return 0;
}
}
}
}
return 0;
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL inf=0x3f3f3f3f3f3f3f3fll;
const int MX=200005;
int n,m,x,y;LL ad,f[MX],ans;
LL f1[MX],f2[MX];
void add1(int x,LL k){for(;x<=n&&k<f1[x];x+=x&-x)f1[x]=k;}
LL que1(int x){LL k=inf;for(;x;x-=x&-x)k=min(k,f1[x]);return k;}
void add2(int x,LL k){for(;x&&k<f2[x];x-=x&-x)f2[x]=k;}
LL que2(int x){LL k=inf;for(;x<=n;x+=x&-x)k=min(k,f2[x]);return k;}
void wk(){
y=x,cin>>x;
LL v=min(que1(x)+x,que2(x)-x)+ad;ad+=abs(x-y);
if(v-ad<f[y])f[y]=v-ad,add1(y,f[y]-y),add2(y,f[y]+y);
}
int main(){
int i;
cin>>n>>m>>x>>y;
for(i=1;i<=n;i++)f[i]=f1[i]=f2[i]=inf;
for(f[y]=0,add1(y,-y),add2(y,y);m--;)wk();
for(ans=inf,i=1;i<=n;i++)ans=min(ans,f[i]);
cout<<ans+ad<<endl;
return 0;
} | #include <bits/stdc++.h>
#define ll long long
#define fr(i, l, r) for(int i = l;i < r;i++)
using namespace std;
const int N = 200001;
const ll OO = 1e13;
ll tr[3][4 * N];
ll lz[4 * N];
void init(int in, int l, int r){
if(l == r){
fr(i, 0, 3)
tr[i][in] = 1e13 + 1ll * (i - 1) * l;
return;
}
int m = (l + r) / 2;
init(in * 2, l, m);
init(in * 2 + 1, m + 1, r);
fr(i, 0, 3)
tr[i][in] = min(tr[i][2 * in], tr[i][2 * in + 1]);
}
void push(int in, int l, int r){
fr(i, 0, 3)
tr[i][in] += lz[in];
if(l != r){
lz[2 * in] += lz[in], lz[2 * in + 1] += lz[in];
}
lz[in] = 0;
}
void ass(int in, int l, int r, int j, ll v){
push(in, l, r);
if(l > j || r < j)
return;
if(l == r){
fr(i, 0, 3)
tr[i][in] = v + 1ll * l * (i - 1);
return;
}
int m = (l + r) / 2;
ass(in * 2, l, m, j, v);
ass(in * 2 + 1, m + 1, r, j, v);
fr(i, 0, 3)
tr[i][in] = min(tr[i][2 * in], tr[i][2 * in + 1]);
}
struct MnRes{
ll mn[3];
MnRes(){
mn[0] = mn[1] = mn[2] = OO;
}
MnRes(ll a, ll b, ll c){
mn[0] = a, mn[1] = b, mn[2] = c;
}
void upd(const MnRes& ot){
fr(i, 0, 3)
mn[i] = min(mn[i], ot.mn[i]);
}
};
MnRes get(int in, int l, int r, int s, int e){
push(in ,l, r);
if(l > e || r < s)
return MnRes();
if(l >= s && r <= e){
return MnRes(tr[0][in], tr[1][in], tr[2][in]);
}
int m = (l + r) / 2;
MnRes res = get(2 * in, l, m, s, e);
res.upd(get(2 * in + 1, m + 1, r, s, e));
return res;
}
void add(int in, int l, int r, int s, int e, ll v){
push(in, l, r);
if(l > e || r < s)
return;
if(l >= s && r <= e){
lz[in] += v;
push(in, l, r);
return;
}
int m = (l + r) / 2;
add(2 * in, l , m, s, e, v);
add(2 * in + 1, m + 1, r, s, e, v);
fr(i, 0, 3)
tr[i][in] = min(tr[i][2 * in], tr[i][2 * in + 1]);
}
int n, q, a, b;
//
//void print(int in = 1, int l = 0, int r = n -1){
// push(in, l, r);
// printf("l= %d, r= %d, tr[0]= %lld, tr[1]= %lld, tr[2]= %lld, lz[0]= %lld, lz[1]= %lld, lz[2]= %lld\n", l, r, tr[0][in], tr[1][in], tr[2][in], lz[0][in], lz[1][in], lz[2][in]);
// if(l == r){
// return;
// }
// int m = (l + r) / 2;
// print(2 * in, l, m);
// print(2 * in + 1, m + 1, r);
//}
int x[N];
int main(){
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
cin >> n >> q >> a >> b;
a--, b--;
fr(i, 0, q){
cin >> x[i];
x[i]--;
}
init(1, 0, n - 1);
// print();
ass(1, 0, n - 1, a, abs(b - x[0]));
// print();
ass(1, 0, n - 1, b, abs(a - x[0]));
for(int i = 1;i < q;i++){
// calc dp[i][x[i]]
MnRes r1 = get(1, 0, n - 1, 0, x[i]);
MnRes r2 = get(1, 0, n - 1, x[i], n - 1);
ll val = min(x[i] + r1.mn[0], r2.mn[2] - x[i]);
add(1, 0, n - 1, 0, n - 1, abs(x[i] - x[i -1]));
ass(1, 0, n - 1, x[i - 1], val);
// print();
// cout << endl;
}
// print();
cout << get(1, 0, n - 1, 0, n - 1).mn[1] << endl;
// cout << get(1, 0, n - 1, 0, n - 1).mn[1];
}
| 1 |
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <functional>
#include <string>
#include <cmath>
#include <cassert>
#define SIZE 100005
using namespace std;
typedef long long int ll;
typedef pair <ll,int> P;
ll B[SIZE],L[SIZE],U[SIZE];
P pos[SIZE];
int main()
{
int n;
ll X;
scanf("%d %lld",&n,&X);
ll sum=0;
for(int i=0;i<n;i++)
{
scanf("%lld %lld %lld",&B[i],&L[i],&U[i]);
pos[i]=P((X-B[i])*U[i]+B[i]*L[i],i);
sum-=B[i]*L[i];
}
sort(pos,pos+n);
ll ret=0;
int last=0;
for(int i=n-1;i>=0;i--)
{
P p=pos[i];
if(sum+p.first>=0)
{
ret+=(ll) (n-i-1)*(ll) X;
last=i+1;
break;
}
sum+=p.first;
}
// cout<<ret<<" "<<last<<endl;
ll mn=X;
sum=-sum;
// cout<<sum<<endl;
for(int i=0;i<last;i++)
{
int v=pos[i].second;
mn=min(mn,(sum+L[v]-1)/L[v]);
ll nsum=sum+B[v]*(U[v]-L[v]);
mn=min(mn,(nsum+U[v]-1)/U[v]);
}
for(int i=last;i<n;i++)
{
int v=pos[i].second;
ll nsum=sum+pos[i].first-pos[last-1].first;
mn=min(mn,(nsum+L[v]-1)/L[v]);
ll nsum2=nsum+B[v]*(U[v]-L[v]);
mn=min(mn,(nsum2+U[v]-1)/U[v]);
}
printf("%lld\n",ret+mn);
return 0;
}
| #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int read(){
int x=0;bool f=0;char c=getchar();
while(c<'0'||c>'9') f|=c=='-',c=getchar();
while(c>='0'&&c<='9') x=x*10+(c^48),c=getchar();
return f?-x:x;
}
#define MAXN 100000
#define LL long long
int N,X;
LL Sum[MAXN+5],Org,D[MAXN+5];
struct Range{
LL tot;
int l,r,id;
}A[MAXN+5];
bool cmp(Range i,Range j){
return i.tot>j.tot;
}
bool Check(LL expt){
LL Max=0;
LL All=expt/X,Rest=expt%X;
for(int i=1;i<=N;i++){
LL l=min(Rest,D[A[i].id]);
LL tmp=i>All?Sum[All]:(Sum[All+1]-A[i].tot);
Max=max(Max,tmp+l*A[i].l+(Rest-l)*A[i].r);
}
return Max>=Org;
}
int main(){
N=read(),X=read();
for(int i=1;i<=N;i++){
D[i]=read();
A[i].id=i;
A[i].l=read(),A[i].r=read();
A[i].tot=D[i]*A[i].l+(X-D[i])*A[i].r;
Org+=D[i]*A[i].l;
}
sort(A+1,A+N+1,cmp);
for(int i=1;i<=N;i++)
Sum[i]=Sum[i-1]+A[i].tot;
LL Left=-1,Right=(LL)X*N;
while(Left+1<Right){
LL mid=(Left+Right)>>1;
if(Check(mid))
Right=mid;
else
Left=mid;
}
printf("%lld",Right);
}
| 1 |
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
int n,num[3]={0};
cin>>n;
cin>>num[0]>>num[1];
if(n==3)cin>>num[2];
sort(num,num+2);
for(int i=1;i<=num[0];i++){
if(num[0]%i==0&&num[1]%i==0&&num[2]==0)cout<<i<<endl;
else if(num[0]%i==0&&num[1]%i==0&&num[2]%i==0)cout<<i<<endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0; i<(n); i++)
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
ll gcd(ll a, ll b){
if(b==0) return a;
return gcd(b,a%b);
}
int main(){
ll n; cin>>n;
if(n==2){
ll a,b; cin>>a>>b;
ll x=gcd(a,b);
rep(i,x+1){
if(i==0)continue;
if(x%i==0) cout<<i<<endl;
}
}else{
ll a,b,c; cin>>a>>b>>c;
ll x=gcd(a,b);
ll y=gcd(c,x);
rep(i,y+1){
if(i==0)continue;
if(y%i==0) cout<<i<<endl;
}
}
return 0;
}
| 1 |
#include <iostream>
#include <string>
using namespace std;
int main(void) {
string str;
while(getline(cin, str)) {
while(1) {
for(int i=0; i<str.length(); i++) {
if(str[i] != ' ' && str[i] != '.') {
if(str[i] != 'z'){
str[i] += 1;
}else{
str[i] = 'a';
}
}
}
if(str.find("the")!=string::npos||str.find("this")!=string::npos||str.find("that")!=string::npos) {
cout << str << endl;
break;
}
}
}
return 0;
} | #include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
using namespace std;
int main() {
string s;
while(getline(cin,s)) {
string t;
rep(j,0,26) {
t.clear();
rep(i,0,s.length()) {
if('a' <= s[i] && s[i] <= 'z') {
int val = (s[i] - 'a' + j) % 26;
t += ('a' + val);
}
else t += s[i];
}
if(t.find("this") != string::npos) break;
else if(t.find("the") != string::npos) break;
else if(t.find("that") != string::npos) break;
}
cout << t << endl;
}
return 0;
} | 1 |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(i = 0;i < n;++i)
#define all(v) v.begin(), v.end()
using ll = long long;
int main()
{
ll i,j;
ll h,w,a,b;
cin >> h >> w >> a >> b;
if(h == 1){
for(i = 0;i < a;++i) cout << 0;
for(i = a;i < w;++i) cout << 1;
cout << endl;
return 0;
}else if(w == 1){
for(i = 0;i < b;++i){
cout << 0 << endl;
}
for(i = b;i < h;++i){
cout << 1 << endl;
}
return 0;
}
for(i = 0;i < h;++i){
for(j = 0;j < w;++j){
if(i < b && j < a) cout << 0;
else if(i >= b && j >= a) cout << 0;
else cout << 1;
}
cout << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
template< typename T >
struct edge {
int src, to;
T cost;
edge(int to, T cost) : src(-1), to(to), cost(cost) {}
edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
edge &operator=(const int &x) {
to = x;
return *this;
}
operator int() const { return to; }
};
template< typename T >
using Edges = vector< edge< T > >;
template< typename T >
using WeightedGraph = vector< Edges< T > >;
using UnWeightedGraph = vector< vector< int > >;
template< typename T >
using Matrix = vector< vector< T > >;
template< typename T >
void warshall_floyd(Matrix< T > &g, T INF) {
for(int k = 0; k < g.size(); k++) {
for(int i = 0; i < g.size(); i++) {
for(int j = 0; j < g.size(); j++) {
if(g[i][k] == INF || g[k][j] == INF) continue;
g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
}
}
}
}
int main() {
int V, E;
scanf("%d %d", &V, &E);
Matrix< int > mat(V, vector< int >(V, INT_MAX));
for(int i = 0; i < V; i++) mat[i][i] = 0;
for(int i = 0; i < E; i++) {
int x, y, z;
scanf("%d %d %d", &x, &y, &z);
mat[x][y] = z;
}
warshall_floyd(mat, INT_MAX);
for(int i = 0; i < V; i++) {
if(mat[i][i] < 0) {
puts("NEGATIVE CYCLE");
return 0;
}
}
for(int i = 0; i < V; i++) {
for(int j = 0; j < V; j++) {
if(j > 0) putchar(' ');
if(mat[i][j] == INT_MAX) printf("INF");
else printf("%d", mat[i][j]);
}
putchar('\n');
}
}
| 0 |
#pragma GCC optimize("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target ("avx")
#include <bits/stdc++.h>
constexpr int INF = 2147483647;
constexpr long long int INF_LL = 9223372036854775807;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
struct edge {
ll to;
ll cost;
};
//トポソ
void toposo_(vector<vector<edge>>& g, vector<int>& used, vector<int>& l, int pos) {
if (!used[pos]) {
used[pos] = 1;
for (int i = 0; i < g[pos].size(); i++) {
toposo_(g, used, l, g[pos][i].to);
}
l.push_back(pos);
}
}
vector<int> toposo(vector<vector<edge>>& g) {
vector<int> u(g.size(), 0), ret;
for (int i = 0; i < g.size(); i++)toposo_(g, u, ret, i);
reverse(ret.begin(), ret.end());
return ret;
}
int main() {
int N, M;
cin >> N >> M;
vector<vector<edge>> g(N);
vector<vector<int>> pars(N);
for (int i = 0; i < N + M - 1; i++) {
int u, v;
cin >> u >> v;
u--; v--;
g[u].push_back({ v,1 });
pars[v].push_back(u);
}
vector<int> a = toposo(g);
vector<int> used(N, 0), ans(N);
for (int i = 0; i < N; i++) {
if (i == 0) {
ans[a[i]] = -1;
used[a[i]] = i+1;
}
else {
ll t1, t2 = -1;
for (int j = 0; j < pars[a[i]].size(); j++) {
if (used[pars[a[i]][j]] != 0) {
if (t2 < used[pars[a[i]][j]]) {
t1 = pars[a[i]][j];
t2 = used[pars[a[i]][j]];
}
}
}
ans[a[i]] = t1;
used[a[i]] = i + 1;
}
//cout << a[i] << endl;
}
for (int i = 0; i < N; i++) {
cout << ans[i] + 1 << endl;
}
} | #include <fstream>
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <sstream>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <random>
using namespace std;
#define INT_MAX_VALUE 2147483647
#define LONG_LONG_MAX_VALUE 9223372036854775807
#define ll long long
#define ld long double
struct XX{
int s;
int t;
};
class xxGreater {
public:
bool operator()(const XX& riLeft, const XX& riRight) const {
//第2条件
if((riLeft.t) == (riRight.t)){
return riLeft.s < riRight.s;//<:昇順(小さいものから順番)、>:降順(大きいものから順番)
//プライオリティキューの場合は > で、top()すると値の小さいものがとれる
}
//第1条件
return (riLeft.t) < (riRight.t);
}
};
//map<long long,long long> prime_f(long long n){
// map<long long,long long>res;
// for(int i=2;i*i<=n;i++){
// while(n%i==0){
// ++res[i];
// n/=i;
// }
// }
// if(n!=1)res[n]=1;
// return res;
//}
int ppar[200002]; //親
int rrank[200002];//木の深さ
void _init(int n){
for(int i=1;i<=n;i++){
ppar[i]=i;
rrank[i]=0;
}
}
int find(int x){
if(ppar[x]==x){
return x;
}else{
return ppar[x]=find(ppar[x]);
}
}
void unite(int x,int y){
x=find(x);
y=find(y);
if(x==y){
return;
}
if(rrank[x]<rrank[y]){
ppar[x]=y;
}else{
ppar[y]=x;
if(rrank[x]==rrank[y]){
rrank[x]++;
}
}
}
bool same(int x,int y){
return find(x)==find(y);
}
int ppar2[200002]; //親
int rrank2[200002];//木の深さ
void _init2(int n){
for(int i=1;i<=n;i++){
ppar2[i]=i;
rrank2[i]=0;
}
}
int find2(int x){
if(ppar2[x]==x){
return x;
}else{
return ppar2[x]=find2(ppar2[x]);
}
}
void unite2(int x,int y){
x=find2(x);
y=find2(y);
if(x==y){
return;
}
if(rrank2[x]<rrank2[y]){
ppar2[x]=y;
}else{
ppar2[y]=x;
if(rrank2[x]==rrank2[y]){
rrank2[x]++;
}
}
}
bool same2(int x,int y){
return find2(x)==find2(y);
}
int main(int argc, const char * argv[])
{
//scanf("%s",S);
//scanf("%d",&N);
//scanf("%lld %lld",&target1,&target2);
//sscanf(tmp.c_str(),"%dd%d%d",&time[i], &dice[i], &z[i]);
//getline(cin, target);
//ifstream ifs("a.txt");//テスト用
//ifs >> a;
//ここから
//入力高速化
ios::sync_with_stdio(false);
cin.tie(0);
int N,K,L;
cin >> N >> K >> L;
int tmp[100000],tmp2[100000];
int ttmp[100000],ttmp2[100000];
for(int i=0;i<K;i++){
cin >> tmp[i]>>tmp2[i];
}
for(int i=0;i<L;i++){
cin >> ttmp[i]>>ttmp2[i];
}
_init(N);
_init2(N);
for(int i=0;i<L;i++){
unite2(ttmp[i],ttmp2[i]);
}
for(int i=0;i<K;i++){
unite(tmp[i],tmp2[i]);
//ここでつなげる、つなげない判断はしてはいけない
// a+b c+d (道路)
// a+b-c (鉄道)
// 鉄道でb-cを結んでおかないと、b-dが来た時にcとつながれない
}
int a2[200002];
map<pair<int,int>,int>a;
for(int i=1;i<=N;i++){
a[make_pair(find(i),find2(i))]++;
}
for(int i=1;i<=N;i++){
a2[i]=a[make_pair(find(i),find2(i))];
}
for(int i=1;i<=N;i++){
cout << a2[i];
if(i!=N){
cout << " ";
}else{
cout << endl;
}
}
//ここまで
//cout << "ans" << endl;
//printf("%.0f\n",ans);//小数点以下表示なし
//printf("%.7f\n",p);
return 0;
}
| 0 |
#include <cstdio>
#include <stdlib.h>
int main()
{
int s[600];
int t[6];
int c = 0;
char h[4] = {'N', 'S', 'W', 'E'};
int n;
bool a = false;
scanf("%d", &n);
for (int i = 0; i < 6 * n; i++)
{
scanf("%d", &s[i]);
}
for (int i = 0; i < n; i++)
{
while (1)
{
int k = rand() % 4;
if (h[k] == 'N')
{
t[4] = s[0 + i * 6];
t[0] = s[1 + i * 6];
t[1] = s[5 + i * 6];
t[5] = s[4 + i * 6];
t[2] = s[2 + i * 6];
t[3] = s[3 + i * 6];
}
if (h[k] == 'S')
{
t[0] = s[4 + i * 6];
t[1] = s[0 + i * 6];
t[5] = s[1 + i * 6];
t[4] = s[5 + i * 6];
t[2] = s[2 + i * 6];
t[3] = s[3 + i * 6];
}
if (h[k] == 'W')
{
t[0] = s[2 + i * 6];
t[2] = s[5 + i * 6];
t[5] = s[3 + i * 6];
t[3] = s[0 + i * 6];
t[1] = s[1 + i * 6];
t[4] = s[4 + i * 6];
}
if (h[k] == 'E')
{
t[2] = s[0 + i * 6];
t[5] = s[2 + i * 6];
t[3] = s[5 + i * 6];
t[0] = s[3 + i * 6];
t[1] = s[1 + i * 6];
t[4] = s[4 + i * 6];
}
for (int j = 0; j < 6; j++)
{
s[j + i * 6] = t[j];
}
for (int j = i + 1; j < n; j++)
{
if (s[0 + i * 6] == s[0 + j * 6] && s[1 + i * 6] == s[1 + j * 6] && s[2 + i * 6] == s[2 + j * 6] && s[3 + i * 6] == s[3 + j * 6] && s[4 + i * 6] == s[4 + j * 6] && s[5 + i * 6] == s[5 + j * 6])
{
a = true;
break;
}
}
if (c == 1000)
{
break;
}
c += 1;
}
}
if (a == false)
{
printf("Yes\n");
}
if (a == true)
{
printf("No\n");
}
}
| #include<iostream>
using namespace std;
class DICE {
enum {
Top,
Front,
Right,
Left,
Rear,
Bottom
};
void Roll(const char*Directions) {
for (int i = 0; Directions[i] != '\0'; i++) {
Roll(Directions[i]);
}
}
void Roll(const char Direction) {
int tmpTop = Num[Top];
switch (Direction) {
case'E':
Num[Top] = Num[Left];
Num[Left] = Num[Bottom];
Num[Bottom] = Num[Right];
Num[Right] = Num[tmpTop];
break;
case'N':
Num[Top] = Num[Front];
Num[Front] = Num[Bottom];
Num[Bottom] = Num[Rear];
Num[Rear] = tmpTop;
break;
case'S':
Num[Top] = Num[Rear];
Num[Rear] = Num[Bottom];
Num[Bottom] = Num[Front];
Num[Front] = tmpTop;
break;
case'W':
Num[Top] = Num[Right];
Num[Right] = Num[Bottom];
Num[Bottom] = Num[Left];
Num[Left] = tmpTop;
break;
}
}
bool SameDiceDirectionFlag(DICE Dice) {
for (int i = 0; i < 6; i++) {
if (Num[i] != Dice.Num[i])return false;
}
return true;
}
public:
int Num[6];
bool SameDiceNoneDirectionFlag(DICE Dice) {
const char Directions[][3] = { "N","N","N","NW","WW","\0" };
for (int i = 0; i < 6; i++) {
if (Num[Top] == Dice.Num[Top]) {
for (int j = 0; j < 4; j++) {
if (SameDiceDirectionFlag(Dice))return true;
int tmpFront = Dice.Num[Front];
Dice.Num[Front] = Dice.Num[Right];
Dice.Num[Right] = Dice.Num[Rear];
Dice.Num[Rear] = Dice.Num[Left];
Dice.Num[Left] = tmpFront;
}
}
Dice.Roll(Directions[i]);
}
return false;
}
};
int main() {
DICE Dice[101];
int n;
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 6; j++)cin >> Dice[i].Num[j];
}
for (int i = 1; i < n; i++) {
if (Dice[0].SameDiceNoneDirectionFlag(Dice[i])) {
cout << "No" << endl;
return 0;
}
}
cout << "Yes" << endl;
return 0;
} | 1 |
#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<math.h>
using namespace std;
typedef long long ll;
#define int long long
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef priority_queue<int> PQ;
#define fore(i,a) for(auto &i:a)
#define REP(i,n) for(int i=0;i<n;i++)
#define eREP(i,n) for(int i=0;i<=n;i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define eFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define SORT(c) sort((c).begin(),(c).end())
#define rSORT(c) sort((c).rbegin(),(c).rend())
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))
#define INF 1000000000
#define LLINF 9223372036854775807
//#define mod 998244353
//vector<vector<int> > dp;
//vector<vector<vector<int> > > vvvi;
//dp=vector<vector<int> >(N, vector<int>(M,0));
//vector<pair<int,int> > v;
//v.push_back(make_pair(x,y));
//priority_queue<int,vector<int>, greater<int> > q2;
int dp1[310][90909] = { 0 };
int dp2[310][90909] = { 0 };
signed main(){
cin.tie(0);
ios::sync_with_stdio(false);
int N, sum = 0;
int mod = 998244353;
cin >> N;
VI A(N);
REP(i, N) {
cin >> A[i];
sum += A[i];
}
dp1[0][0] = 1;
REP(i, N) {
REP(j, 90909) {
if (j + A[i] < 90909) {
dp1[i + 1][j + A[i]] += dp1[i][j];
dp1[i + 1][j + A[i]] %= mod;
}
dp1[i + 1][j] += 2 * dp1[i][j];
dp1[i + 1][j] %= mod;
}
}
int x = 0;
REP(j, 90909) {
if (j * 2 >= sum) {
x += dp1[N][j];
x %= mod;
}
}
dp2[0][0] = 1;
REP(i, N) {
REP(j, 90909) {
if (j + A[i] < 90909) {
dp2[i + 1][j + A[i]] += dp2[i][j];
dp2[i + 1][j + A[i]] %= mod;
}
dp2[i + 1][j] += dp2[i][j];
dp2[i + 1][j] %=mod;
}
}
int y = (sum % 2 == 0) ? dp2[N][sum / 2] : 0;
int ans = 1;
REP(i, N) {
ans *= 3;
ans %= mod;
}
//cout << x << endl;
//cout << y << endl;
//cout << ans << endl;
//cout << (ans-3*x+3*y)%mod << endl;
ans = (ans - 3 * x + 8 * mod) % mod;
ans += 3 * y;
cout << ans % mod << endl;
return 0;
}
| // I SELL YOU...!
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<queue>
#include<chrono>
#include<iomanip>
#include<map>
#include<set>
#define MOD 1000000007
#define MAX_H 110
#define MAX_W 10
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
void init_io(){
cin.tie(0);
ios::sync_with_stdio(false);
cout << setprecision(10);
}
ll h,w,k,fr;
ll dp[MAX_H][MAX_W]={};
ll pat_dp[MAX_W][2];
ll pat[MAX_W];
signed main(){
ll st,le,ri;
init_io();
cin >> h >> w >> k;
if(w==1){
cout << 1<<endl;
return 0;
}
dp[0][0] = 1;
pat_dp[0][0] = 0;
pat_dp[0][1] = 0;
pat[0] = 1;
pat_dp[1][0] = 1;
pat_dp[1][1] = 0;
pat[1] = 1;
for(int i=2;i<MAX_W;i++){
pat_dp[i][0] = pat_dp[i-1][0]+pat_dp[i-1][1];
pat_dp[i][1] = pat_dp[i-1][0];
pat_dp[i][0] %= MOD;
pat_dp[i][1] %= MOD;
pat[i] = (pat_dp[i][0]+pat_dp[i][1])%MOD;
}
for(int i=1;i<=h;i++){
for(int j=0;j<w;j++){
st = dp[i-1][j];
st *= (pat[j]*pat[w-1-j])%MOD;
st %= MOD;
le = 0;
ri = 0;
if(j!=0){
le = (dp[i-1][j-1]*(pat[j-1]*pat[w-1-j])%MOD)%MOD;
}
if(j!=w-1){
ri = (dp[i-1][j+1]*(pat[j]*pat[w-2-j]))%MOD;
}
dp[i][j] = (st+ri+le)%MOD;
}
}
cout << dp[h][k-1]<<endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define int long long
#define fr(i,a,b) for(int i = a ; i <= b ; ++i)
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define pb push_back
#define fst first
#define snd second
using namespace __gnu_pbds;
using ordered_set =
tree<int, null_type,less<int>,
rb_tree_tag,tree_order_statistics_node_update>;
//find_by_order(k):returns iterator to kth element starting from 0
//order_of_key(k):returns count of elements strictly smaller than k
typedef long long ll;typedef pair<int, int> pii;
typedef vector<int> vi;typedef long double ld;
template<class T>
using min_heap = priority_queue<T,vector<T>,greater<T>>;
template<typename T> T gcd(T a, T b){return(b?__gcd(a,b):a);}
template<typename T> T lcm(T a, T b){return(a*(b/gcd(a,b)));}
template<class T> void re(T& x) { cin >> x; }
template<typename T>
void remdup(vector<T>& v) {
sort(all(v)); v.erase(unique(all(v)), v.end());
}
template<typename T> void re(vector<T> &v) {trav(i,v) cin >> i;}
template<class H, class... T> void re(H& h, T&... t) { re(h); re(t...); }
void unsyncIO() { ios_base::sync_with_stdio(0); cin.tie(0); }
#ifdef np
#include "/home/o_o/MyCodes/pr.h"
#else
#define trace(...)
#endif
#define MOD 1000000007
void solve(){
int cnt = 1 ;
int n , x , t ;
re(n,x,t);
while(cnt*x<n) cnt++;
cout << cnt * t << endl;
}
signed main() {
unsyncIO();
int tt =1 ;
// cin >> tt;
rep(i,0,tt) solve();
#ifdef np
cout <<endl<<endl<< "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int N;
cin >> N;
if (N == 1) {
cout << "Hello"
<< " "
<< "World" << endl;
} else if (N == 2) {
int A, B;
cin >> A >> B;
cout << A + B << endl;
}
} | 0 |
#include<cstdio>
int main()
{
while(true) {
int a, b, c, n;
int test[3000], r[1000];
int ans[1000];
scanf("%d%d%d", &a, &b, &c);
if(a == 0)
break;
scanf("%d", &n);
for(int i = 0; i < n; ++i) {
scanf("%d%d%d%d", test + i * 3, test + i * 3 + 1, test + i * 3 + 2, r + i);
test[i * 3] -= 1, test[i * 3 + 1] -= 1, test[i * 3 + 2] -= 1;
}
for(int i = 0; i < a + b + c; ++i)
ans[i] = 2;
for(int i = 0; i < n; ++i) {
if(r[i] != 1)
continue;
for(int j = i * 3; j < i * 3 + 3; ++j)
ans[test[j]] = 1;
}
for(int i = 0; i < n; ++i) {
if(r[i] != 0)
continue;
int correct = 0;
for(int j = i * 3; j < i * 3 + 3; ++j)
correct += (ans[test[j]] == 1);
if(correct != 2)
continue;
for(int j = i * 3; j < i * 3 + 3; ++j) {
if(ans[test[j]] != 1)
ans[test[j]] = 0;
}
}
for(int i = 0; i < a + b + c; ++i)
printf("%d\n", ans[i]);
}
return 0;
} | #include <iostream>
#include <string>
#include <vector>
#include <list>
#include <math.h>
#include <algorithm>
const int coin[6] = { 500,100,50,10,5,1 };
using namespace std;
struct Expm { int a, b, c, r; };
int main(){
/*int answer[5][2] = { 0 };
int n = 0;
int buf[100];
int ndataset;
for (ndataset = 0; ; ndataset++) {
cin >> n;
if (n == 0) break;
for (int i = 0; i < n; i++) {
cin >> buf[i];
}
sort(&buf[0], &buf[n]);
int scoreT = 0;
int scoreH = 0;
for (int i = 0; i < n; i++) {
int sbase = 2 * i + 1 - buf[i];
if (sbase > 0) {
scoreH = max(abs(sbase), scoreH);
}
else {
scoreT = max(abs(sbase), scoreT);
}
}
if (scoreH > scoreT) scoreT = 0;
if (scoreH <= scoreT) scoreH = 0;
answer[ndataset][0] = scoreT;
answer[ndataset][1] = scoreH;
} // dataset
for (int i = 0; i < ndataset; i++) {
cout << answer[i][0] << endl;
cout << answer[i][1] << endl;
}*/
int resA[5][100]; int resB[5][100]; int resC[5][100];
int numa[5], numb[5], numc[5], nume[5];
int set = 0;
for (;; set++) {
Expm expm[1000];
cin >> numa[set] >> numb[set] >> numc[set];
if (numa[set] == 0 && numb[set] == 0 && numc[set] == 0) break;
for (int i = 0; i < numa[set]; i++) resA[set][i] = 2;
for (int i = 0; i < numb[set]; i++) resB[set][i] = 2;
for (int i = 0; i < numc[set]; i++) resC[set][i] = 2;
cin >> nume[set];
for (int i = 0; i < nume[set]; i++) {
cin >> expm[i].a
>> expm[i].b
>> expm[i].c
>> expm[i].r;
if (expm[i].r) {
resA[set][expm[i].a-1] = resB[set][expm[i].b-numa[set]-1] =
resC[set][expm[i].c - numa[set] - numb[set] -1] = 1;
}
}
for (int i = 0; i < nume[set]; i++) {
if (expm[i].r) continue;
if (resA[set][expm[i].a-1] == 1 &&
resB[set][expm[i].b - numa[set] -1] == 1)
resC[set][expm[i].c - numa[set] - numb[set] -1] = 0;
if (resC[set][expm[i].c - numa[set] - numb[set] -1] == 1 &&
resB[set][expm[i].b - numa[set] -1] == 1)
resA[set][expm[i].a-1] = 0;
if (resA[set][expm[i].a-1] == 1 &&
resC[set][expm[i].c - numa[set] - numb[set] -1] == 1)
resB[set][expm[i].b - numa[set] -1] = 0;
}
}
for (int j = 0; j < set; j++) {
for (int i = 0; i < numa[j]; i++)
cout << resA[j][i] << endl;
for (int i = 0; i < numb[j]; i++)
cout << resB[j][i] << endl;
for (int i = 0; i < numc[j]; i++)
cout << resC[j][i] << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
#include <bits/stdc++.h>
using namespace std;
int d12, d13, d23, T;
void comparar () {
int t1=d12+d23;
int t2=d13+d23;
int t3=d12+d13;
if (t1>=t2) {
T=min(t3,t2);
} else {
T=min(t1,t3);
}
}
int main () {
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>d12>>d13>>d23;
comparar();
cout<<T;
return 0;
}
| #include <atcoder/fenwicktree>
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll solve() {
ll N, Q, t, a;
cin >> N >> Q;
atcoder::fenwick_tree<ll> ft(N);
for ( int i = 0; i < N; i++ ) {
cin >> a;
ft.add(i, a);
}
vector<ll> ans;
for ( int q = 0; q < Q; q++ ) {
cin >> t;
if ( t == 0 ) {
ll p, x;
cin >> p >> x;
ft.add(p,x);
} else {
ll l, r;
cin >> l >> r;
ans.push_back(ft.sum(l,r));
}
}
for ( auto a : ans ) {
cout << a << "\n";
}
return 0;
}
int main() {
solve();
return 0;
} | 0 |
#define _USE_MATH_DEFINES //M_PI
#include <iostream> //std::cout, std::cin
#include <string> //std::string
#include <vector> //std::vector
#include <valarray> //std::valarray ??°????????????????¬???????
#include <algorithm> //std::sort
#include <time.h> //localtime_s
#include <cstdlib> //abs
#include <cmath> //abs, pow, sqrt, sin, cos,
#include <fstream> //std::ifstream
#include <iomanip> //std::setprecision
int main(void) {
//test??¨
//std::ifstream in("test.txt");
//std::cin.rdbuf(in.rdbuf());
int dice[6] = {};
for (int i = 0; i < 6; i++) {
std::cin >> dice[i];
}
int temp[6] = {};
std::string order;
std::cin >> order;
for (int i = 0; i < order.length(); i++) {
switch (order[i]) {
case 'E':
temp[0] = dice[3];
temp[1] = dice[1];
temp[2] = dice[0];
temp[3] = dice[5];
temp[4] = dice[4];
temp[5] = dice[2];
for (int j = 0; j < 6; j++) {
dice[j] = temp[j];
}
break;
case 'N':
temp[0] = dice[1];
temp[1] = dice[5];
temp[2] = dice[2];
temp[3] = dice[3];
temp[4] = dice[0];
temp[5] = dice[4];
for (int j = 0; j < 6; j++) {
dice[j] = temp[j];
}
break;
case 'S':
temp[0] = dice[4];
temp[1] = dice[0];
temp[2] = dice[2];
temp[3] = dice[3];
temp[4] = dice[5];
temp[5] = dice[1];
for (int j = 0; j < 6; j++) {
dice[j] = temp[j];
}
break;
case 'W':
temp[0] = dice[2];
temp[1] = dice[1];
temp[2] = dice[5];
temp[3] = dice[0];
temp[4] = dice[4];
temp[5] = dice[3];
for (int j = 0; j < 6; j++) {
dice[j] = temp[j];
}
break;
default:;
}
}
std::cout << dice[0] << std::endl;
} | #include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
using namespace std;
int partition(int *A,int p,int r)
{
int x,j,temp;
x = A[r];
j = p-1;
for(int i = p; i < r; i++)
{
if (A[i] <= x)
{
j++;
temp = A[j];
A[j] = A[i];
A[i] = temp;
}
}
temp = A[j+1];
A[j+1] = A[r];
A[r] = temp;
return j+1;
}
int main()
{
int n,a[100000] = {0};
int q;
scanf("%d", &n);
for(int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
q = partition(a,0,n-1);
for(int i = 0; i < n; i++)
{
if (i == q)
{
printf("[%d] ", a[i]);
}else if (i == n-1)
{
printf("%d\n", a[i]);
}else
{
printf("%d ", a[i]);
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
#define ll long long int
#define yorn(f) cout<<((f)?"Yes":"No")<<endl;
#define YORN(f) cout<<((f)?"YES":"NO")<<endl;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define put(x) cout << x << endl;
#define println(x) cout << x << endl;
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
int cnt = 0;
for (int n = a; n <= b; n++) {
if (n / 10000 == n % 10 && (n / 1000) % 10 == (n / 10) % 10) {
cnt ++;
}
}
put(cnt);
return 0;
} | /*Be patient && calm!
All our dreams can come true, if we have the courage to pursue them.
I fear not the man who has practiced 10,000 kicks once, but I fear the man who has practiced one kick 10,000 times.
Do not pray for an easy life, pray for the strength to endure a difficult one.
When something is important enough, you do it even if the odds are not in your favor.
First, solve the problem. Then, write the code.
Fix the cause, not the symptom.
Simplicity is the soul of efficiency.
Make it work, make it right, make it fast.
No matter how hard it gets, NEVER GIVE UP*/
#include <bits/stdc++.h>
#define pb push_back
#define MP make_pair
#define ALL(r) (r).begin(),(r).end()
typedef long long int ll;
#define FOR(i,m,n) for(ll i=(ll)(m) ; i < (ll) (n) ; ++i )
#define FORN(i,m,n) for(ll i=(ll)(m-1) ; i >= (ll) (n) ; --i )
#define RUN_FAST ios::sync_with_stdio(false);
using namespace std;
void solve()
{
ll a,b,c,d,m,n,p,q,r,x,y,z,aa,bb,cc,dd,h; //variable
ll i,j,k,l; //pointer
ll cnt=0,cnt1=0,cnt2=0,cnt3=0,sum=0,mx=LLONG_MIN,mn=LLONG_MAX; //counter
ll flag=0,flag2=0; //flag
vector <ll> vec1,vec2,vec3;
string s1,s2,s3;
//PULOK SAHA
//BMO
cin >> a >> b;
FOR(i,a,b+1)
{
s1=to_string(i);
l = 0;
h=s1.size()-1;
flag=0;
while (h > l)
{
if (s1[l++] != s1[h--])
{
flag=1;
break;
}
}
if(!flag)
cnt++;
}
cout << cnt << endl;
}
int main()
{
RUN_FAST
//PULOK SAHA
//BMO
ll t=1;//test case
//cin >> t;
while(t--)
{
solve();
}
return 0;
}
//PULOK SAHA | 1 |
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
#include <fstream>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int mod = 1000000007;
vector<string> a;
bool cmp(string s, string t) {
if (s.length() != t.length()) return s.length() < t.length();
return s < t;
}
void sol(string s) {
a.push_back(s);
if (s.length() == 10) return;
if (s.back() != '0') {
s += (s.back() - 1);
sol(s);
s.pop_back();
}
s += (s.back());
sol(s);
s.pop_back();
if (s.back() != '9') {
s += (s.back() + 1);
sol(s);
s.pop_back();
}
}
int main() {
ios::sync_with_stdio(false);
int a, b, c;
cin >> a >> b >> c;
cout << c << ' ' << a << ' ' << b;
}
| #include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<cstring>
#include<vector>
#include<list>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for(int i=0;i<n;++i)
#define rrep(i, n) for(int i=n;i>=0;--i)
const int inf=1e9+7;
const ll mod=1e9+7;
const ll mod1=998244353;
const ll big=1e18;
const double PI=2*asin(1);
int main() {
int A, B, C;
cin>>A>>B>>C;
cout<<C<<" "<<A<<" "<<B<<endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int A, B, C, D;
cin >> A >> B >> C >> D;
if (A + B == C + D)
{
cout <<"Balanced" << endl;
}
else if (A + B <= C + D)
{
cout << "Right" << endl;
}
else if (A + B >= C + D)
{
cout << "Left" << endl;
}
} | #include <bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
int k;cin>>k;
string s;cin>>s;
int n=s.length();
if(n<=k) cout<<s<<"\n";
else
{
string s1="";
for(int i=0;i<k;i++)
s1=s1+s[i];
s1=s1+"...";
cout<<s1<<"\n";
}
return 0;
} | 0 |
#include<bits/stdc++.h>
using namespace std ;
int main(){
int64_t N ;
cin >> N ;
int P = N/26 ;
vector<char> namae(100) ;
int i = 0 ;
//cout << N << " " << i << endl ;
while(N != 0 ){
N-- ;
int M = N % 26 ;
namae.at(i) = 'a' + M ;
N /= 26 ;
i++ ;
//cout << N << " " << i << endl ;
}
for(int j = i - 1; j >= 0 ; j--){
cout << namae.at(j) ;
}
} | #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
using namespace __gnu_pbds;
using namespace std;
typedef tree<
pair<int, int>,
null_type,
less_equal<pair<int, int>>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
#define int long long
#define double long double
#define all(a) (a).begin(), (a).end()
#define x first
#define y second
#define lb(v, z) lower_bound(all(v), z)
#define ub(v, z) upper_bound(all(v), z)
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define rdp(i, a, b) for (int i = (a); i >= (b); i--)
#define rlt(i, a, b) for (int i = (a); i < (b); i++)
#define cns(z) cout << (z) << ' '
#define cnl(z) cout << (z) << '\n'
#define M1 1000000007
#define M2 998244353
#define MAXN 300005
#define INF (1ll << 60)
#define endl "\n"
#define garr(ip) \
for (auto &x : ip) \
cin >> x;
#define parr(ip) \
for (auto &x : ip) \
cout << x << " "; \
cout << endl;
#define nl cout << endl
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vp;
void solve(int testCaseNumber = 1)
{
int n;
cin >> n;
string ans;
while (n > 0)
{
n--;
ans += 'a' + n % 26;
n /= 26;
}
reverse(all(ans));
cnl(ans);
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
solve();
} | 1 |
# include <iostream>
using namespace std;
int gcd(int a, int b){
return (b==0) ? a : gcd(b, a%b);
}
int main(){
int n, k;
cin >> n >> k;
int g = 0, m = 0;
for(int i=0; i<n; i++){
int x;
cin >> x;
g = gcd(x, g);
m = max(x, m);
}
if(k <= m && k % g == 0) cout << "POSSIBLE";
else cout << "IMPOSSIBLE";
cout << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define rep1(i,n) for(int i=1;i<=(int)n;i++)
#define sp(n) cout << fixed << setprecision(n)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
typedef long long ll;
using namespace std;
ll gcd(ll x,ll y){
if (y == 0) return x;
return gcd(y, x%y);
}
int main(void){
ll n,k;cin>>n>>k;
vector<ll> a(n);
rep(i,n) cin>>a[i];
ll g=a[0];
rep(i,n) g=gcd(g,a[i]);
sort(a.rbegin(),a.rend());
rep(i,n){
if(a[i]<k)break;
if((a[i]-k)%g==0){
cout<<"POSSIBLE"<<endl;
return 0;
}
}
cout<<"IMPOSSIBLE"<<endl;
} | 1 |
//#include <bits/stdc++.h>
//#include <stdio.h>
#include<iostream>
#include<cstdio>
#include<bitset>
#include<algorithm>
#include<vector>
#include<list>
#include<queue>
#include<stack>
#include<string>
#include<string.h>
#include<cmath>
#include<utility>
#include<functional>
#include<map>
#include<set>
#include<cctype>
#include<fstream>
#define FOR(i, a, b) for( int i=(a);i<=(b);i++)
#define RFOR(i, a, b) for( int i=(a);i>=(b);i--)
#define LFOR(i, a, b) for( long long int i=(a);i<=(b);i++)
#define LRFOR(i, a, b) for(long long int i=(a);i>=(b);i--)
#define MOD 1000000007
#define INF 1000000000 //2000000000
#define LINF 1000000000000000000 //9000000000000000000
#define PI 3.14159265358979
#define MAXI 7500000
using namespace std;
typedef long long int ll;
typedef pair< long long int, long long int> P;
int dy[5] = { 0,0,1,-1,0 };
int dx[5] = { 1,-1,0,0,0 };
int main(void) {
while (1) {
int n;
cin >> n;
if (n == 0) {
break;
}
vector<string> l(n);
vector<int> p(n), a(n), b(n), c(n), d(n), e(n), f(n), s(n), m(n), time(n), money(n);
vector<double>adv(n);
vector< pair<double, string> > pa(n);
FOR(i, 0, n - 1) {
cin >> l[i] >> p[i] >> a[i] >> b[i] >> c[i] >> d[i] >> e[i] >> f[i] >> s[i] >> m[i];
int timetmp = 0;
int moneytmp = 0;
timetmp = a[i] + b[i] + c[i] + (d[i] + e[i])*m[i];
moneytmp = f[i] * m[i] * s[i];
moneytmp -= p[i];
adv[i] = (double)moneytmp / (double)timetmp;
pa[i].first = adv[i];
pa[i].second = l[i];
//cout << moneytmp << " " << timetmp << endl;
}
sort(pa.begin(), pa.end());
double tmp;
int first;
int second;
RFOR(i, n - 1, 0) {
tmp = pa[i].first;
first = i;
second = i;
RFOR(j, i-1, 0) {
//cout << i << " " << j << " " << tmp << " " << pa[j].first << endl;
if (tmp == pa[j].first) {
if (j == 0) {
second = 0;
break;
}
continue;
}
else {
second = j + 1;
break;
}
}
i = second;
//cout << first << " " << second << endl;
FOR(j, second, first) {
cout << pa[j].second << endl;
}
}
/*RFOR(i, n - 1, 0) {
cout << pa[i].second << endl;
}*/
cout << "#" << endl;
}
return 0;
}
| #include<iostream>
//??°??¢(0??§???????????????1??§??????)
int place[21][21]={};
//??????????????°??¨??????????????°
int N,M;
//???????????????
int mass;
char dir[10];
int main() {
while (true) {
//?????????
for (int i = 0; i < 21; i++) {
for (int j = 0; j < 21; j++) {
place[i][j] = 0;
}
}
//????????\???
std::cin>>N;
if(N==0)break;
for (int i = 0; i < N; i++) {
int x,y;
std::cin>>x>>y;
place[y][x]=1;
}
//????????????(?????????(10,10))
int rX=10;
int rY=10;
std::cin >> M;
for (int i = 0; i < M; i++) {
std::cin >> dir >> mass;
//???????????????
int dirX,dirY;
switch (dir[0]) {
case 'N': dirX=0; dirY=1 ; break;
case 'S': dirX=0; dirY=-1; break;
case 'E': dirX=1; dirY=0 ; break;
case 'W': dirX=-1;dirY=0 ; break;
}
for (int i = 0; i < mass; i++) {
rX+=dirX;
rY+=dirY;
place[rY][rX]=0;
}
}
//place??¨??????0??????ok
bool flag=true;
for (int i = 0; i < 21; i++) {
for (int j = 0; j < 21; j++) {
if(place[i][j]!=0)flag=false;
}
}
if (flag)std::cout << "Yes" << std::endl;
else std::cout << "No" << std::endl;
}
return 0;
} | 0 |
#pragma GCC optimize(2)
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
const ll linf = 1e18 + 7;
struct segment_tree {
static const int maxn = 1 << 18;
pair <ll, int> mn[maxn << 1];
void init() {
for (int i = 1; i < (maxn << 1); ++ i)
mn[i] = mp(linf, -1);
}
void upd(int x, ll v) {
mn[x + maxn] = mp(v, x);
for (x += maxn; x >>= 1; )
mn[x] = min(mn[x << 1], mn[x << 1 | 1]);
}
pair <ll, int> qry(int l, int r) {
pair <ll, int> ans = mp(linf, -1);
for (l += maxn, r += maxn; l < r; l >>= 1, r >>= 1) {
if (l & 1) ans = min(ans, mn[l ++]);
if (r & 1) ans = min(ans, mn[-- r]);
}
return ans;
}
} seg0, seg1;
struct union_find {
static const int maxn = 2e5 + 5;
int fa[maxn];
vector <int> v[maxn];
void init() {
for (int i = 0; i < maxn; ++ i)
fa[i] = i, v[i].pb(i);
}
int find(int x) {
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
bool same(int x, int y) {
return find(x) == find(y);
}
void merge(int x, int y) {
x = find(x); y = find(y);
if (x == y) return ;
if (v[x].size() < v[y].size()) swap(x, y);
for (int i = 0; i < v[y].size(); ++ i) v[x].pb(v[y][i]);
fa[y] = x;
}
} dsu;
const int maxn = 2e5 + 5;
int n, d, a[maxn];
int main() {
scanf("%d %d", &n, &d);
for (int i = 0; i < n; ++ i)
scanf("%d", &a[i]);
dsu.init();
seg0.init(); seg1.init();
for (int i = 0; i < n; ++ i) {
seg0.upd(i, a[i] - 1LL * d * i);
seg1.upd(i, a[i] + 1LL * d * i);
}
ll ans = 0;
while (dsu.v[dsu.find(0)].size() != n) {
vector <pair <ll, pair <int, int> > > e;
for (int i = 0; i < n; ++ i) {
if (dsu.find(i) == i) {
// eprintf("%d\n", i);
for (int j = 0; j < dsu.v[i].size(); ++ j) {
int x = dsu.v[i][j];
// eprintf("%d ", x);
seg0.upd(x, linf);
seg1.upd(x, linf);
}
// eprintf("\n");
pair <ll, pair <int, int> > mn = mp(linf, mp(-1, -1));
for (int j = 0; j < dsu.v[i].size(); ++ j) {
int x = dsu.v[i][j];
pair <ll, int> to = seg0.qry(0, x);
to.first += a[x] + 1LL * d * x;
mn = min(mn, mp(to.first, mp(x, to.second)));
}
for (int j = 0; j < dsu.v[i].size(); ++ j) {
int x = dsu.v[i][j];
pair <ll, int> to = seg1.qry(x + 1, n);
to.first += a[x] - 1LL * d * x;
mn = min(mn, mp(to.first, mp(x, to.second)));
}
e.pb(mn);
for (int j = 0; j < dsu.v[i].size(); ++ j) {
int x = dsu.v[i][j];
seg0.upd(x, a[x] - 1LL * d * x);
seg1.upd(x, a[x] + 1LL * d * x);
}
}
}
for (int i = 0; i < e.size(); ++ i) {
int u = e[i].second.first;
int v = e[i].second.second;
ll cost = e[i].first;
if (!dsu.same(u, v)) {
dsu.merge(u, v);
ans += cost;
}
}
}
printf("%lld\n", ans);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const int MAXN = 400100;
vector<PII> graph[MAXN];
void add_edge(int v, int u, int c) {
graph[v].push_back(PII(u,c));
graph[u].push_back(PII(v,c));
}
int main() {
int N, D;
cin >> N >> D;
for(int i=0; i<N; i++) {
LL p;
cin >> p;
add_edge(i, i+N, p);
}
for(int i=0; i<N-1; i++) {
int v,u,c;
//cin >> v >> u >> c;
//v--;u--;
add_edge(i, i+1, D);
}
priority_queue<pair<LL,PII> , vector<pair<LL,PII> >, greater<pair<LL,PII> > > pq;
vector<int> used(2*N, 0), dist(2*N, 0);
int cnt = 1;
LL ans = 0;
for(int i=0; i<N; i++) {
pq.push(make_pair(0, PII(i+N, -1)));
}
while(!pq.empty()) {
auto cur = pq.top(); pq.pop();
LL v = cur.second.first;
if(used[v]) {
LL u = cur.second.second;
if(used[v] > used[u])
ans += dist[v] + cur.first;
continue;
}
used[v] = cnt;
cnt++;
dist[v] = cur.first;
for(auto uc: graph[v]) {
LL u = uc.first;
LL c = uc.second;
pq.push(make_pair(dist[v] + c, PII(u, v)));
}
}
cout << ans << endl;
return 0;
} | 1 |
#include <iostream>
#include <cstring>
#include <cstdio>
#define Maxn 4010
#define Maxm 4010
using namespace std;
int read() {
int x = 0,f = 1;
char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while('0' <= c && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x * f;
}
int N,M,Q;
int mp[Maxn][Maxm],sum1[Maxn][Maxm],sum2[Maxn][Maxm];
int main() {
N = read(); M = read(); Q = read();
char ch;
for(int i = 1;i <= N; i++)
for(int j = 1;j <= M; j++) {
int x = i * 2 - 1,y = j * 2 - 1;
ch = getchar();
while(ch != '0' && ch != '1') ch = getchar();
if(ch == '1') mp[x][y] = 1;
}
for(int i = 1;i <= N; i++)
for(int j = 1;j <= M; j++) {
int x = i * 2 - 1,y = j * 2 - 1;
if(mp[x][y]) {
if(mp[x - 2][y]) mp[x - 1][y] = 2;
if(mp[x + 2][y]) mp[x + 1][y] = 2;
if(mp[x][y + 2]) mp[x][y + 1] = 2;
if(mp[x][y - 2]) mp[x][y - 1] = 2;
}
}
for(int i = 1;i < N * 2; i++)
for(int j = 1;j < M * 2; j++) {
if(mp[i][j] == 1) sum1[i][j] = sum1[i - 1][j] + sum1[i][j - 1] - sum1[i - 1][j - 1] + 1;
else sum1[i][j] = sum1[i - 1][j] + sum1[i][j - 1] - sum1[i - 1][j - 1];
if(mp[i][j] == 2) sum2[i][j] = sum2[i - 1][j] + sum2[i][j - 1] - sum2[i - 1][j - 1] + 1;
else sum2[i][j] = sum2[i - 1][j] + sum2[i][j - 1] - sum2[i - 1][j - 1];
}
int i_1,j_1,i_2,j_2,x_1,y_1,x_2,y_2;
for(int k = 1;k <= Q; k++) {
i_1 = read(); j_1 = read(); i_2 = read(); j_2 = read();
x_1 = i_1 * 2 - 1; y_1 = j_1 * 2 - 1;
x_2 = i_2 * 2 - 1; y_2 = j_2 * 2 - 1;
int ans1 = sum1[x_2][y_2] + sum1[x_1 - 1][y_1 - 1] - sum1[x_2][y_1 - 1] - sum1[x_1 - 1][y_2];
int ans2 = sum2[x_2][y_2] + sum2[x_1 - 1][y_1 - 1] - sum2[x_2][y_1 - 1] - sum2[x_1 - 1][y_2];
printf("%d\n",ans1 - ans2);
}
return 0;
} | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define Would
#define you
#define please
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, M, Q;
cin >> N >> M >> Q;
string S[2000];
rep(i, N) cin >> S[i];
int B[2001][2001] = {};
int C[2001][2001] = {};
int D[2001][2001] = {};
rep(i, N) {
rep(j, M) {
B[i + 1][j + 1] = B[i + 1][j] + S[i][j] - '0';
if (i != 0) C[i + 1][j + 1] = C[i + 1][j] + ((S[i - 1][j] - '0') & (S[i][j] - '0'));
if (j != 0) D[i + 1][j + 1] = D[i + 1][j] + ((S[i][j - 1] - '0') & (S[i][j] - '0'));
}
}
rep(j, M) rep(i, N) {
B[i + 1][j + 1] += B[i][j + 1];
C[i + 1][j + 1] += C[i][j + 1];
D[i + 1][j + 1] += D[i][j + 1];
}
rep(q, Q) {
int a, b, c, d;
cin >> a >> b >> c >> d;
int kotae;
kotae = B[c][d] - B[c][b - 1] - B[a - 1][d] + B[a - 1][b - 1];
kotae -= C[c][d] - C[c][b - 1] - C[a][d] + C[a][b - 1];
kotae -= D[c][d] - D[c][b] - D[a - 1][d] + D[a - 1][b];
co(kotae);
}
Would you please return 0;
} | 1 |
#include <iostream>
#include <algorithm>
#define N 100
#define INF 2000000000
#define WHITE 0
#define GRAY 1
#define BLACK 2
using namespace std;
int n;
int M[N][N],color[N],d[N],p[N];
//M??????????????????color????¨??????¶???(0,1,2),d????????????????°???????????¨????
//p???MST???????°???¨?????¨??????????????????????????????
void Prim() {
for (int i = 0;i < n;i++) d[i] = INF;
d[0] = 0; //?????????????????????????????§???????????§????´???????????????????????????????
p[0] = -1; //??????????????????????????????????????????
int u;
while (true) {
int min = INF;
for (int i = 0;i < n;i++) {
if(color[i]!=BLACK&&min>d[i]){
min = d[i];
u = i;
}
}
if (min == INF) break;
color[u] = BLACK;
for (int v = 0;v < n;v++)
if(color[v]!=BLACK&&M[u][v]!=-1)
if (M[u][v] < d[v]) {
d[v] = M[u][v];
p[v] = u;
color[v] = GRAY;
}
}
int sum = 0;
for (int i = 0;i < n;i++) sum += d[i];
cout << sum << endl;
}
int main() {
cin >> n;
for (int i = 0;i < n;i++)
for (int j = 0;j < n;j++) {
cin >> M[i][j];
if (M[i][j] == -1) M[i][j] = INF;
}
Prim();
} | #include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdint>
#include <iostream>
#include <vector>
using namespace std;
constexpr int64_t MOD = 1e9+7;
int main() {
int N;
cin >> N;
vector<int64_t> nums(N);
for (int n = 0; n < N; ++n) {
cin >> nums[n];
}
sort(nums.begin(), nums.end());
vector<vector<int64_t>> cnt(2, vector<int64_t>(61));
for (int64_t n : nums) {
bitset<61> bs(n);
for (size_t i = 0; i <= 60; ++i) {
if (bs.test(i)) {
++cnt[1][i];
} else {
++cnt[0][i];
}
}
}
int64_t result = 0;
for (size_t i = 0; i <= 60; ++i) {
result += (static_cast<int64_t>(pow(2, i)) % MOD) * (cnt[0][i] * cnt[1][i] % MOD);
result %= MOD;
}
cout << result << endl;
return 0;
} | 0 |
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iterator>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <iomanip>
#include <cassert>
//#include <utility>
//#include <memory>
//#include <functional>
//#include <deque>
//#include <cctype>
//#include <ctime>
//#include <numeric>
//#include <list>
//#include <iomanip>
//#if __cplusplus >= 201103L
//#include <array>
//#include <tuple>
//#include <initializer_list>
//#include <forward_list>
//
//#define cauto const auto&
//#else
//#endif
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;
#define VV(T) vector<vector< T > >
template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
v.assign(a, vector<T>(b, t));
}
template <class F, class T>
void convert(const F &f, T &t){
stringstream ss;
ss << f;
ss >> t;
}
#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()
#define DEBUG
#ifdef DEBUG
#define dump(x) cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#else
#define dump(x)
#define debug(x)
#endif
#define LDcout(x,n) fixed<<setprecision(n)<<x
#define MOD 1000000007LL
#define EPS 1e-8
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define maxs(x,y) x=max(x,y)
#define mins(x,y) x=min(x,y)
void mainmain(){
int n;
while(cin>>n,n){
rep(i,n){
vint v(3);
rep(j,3) cin>>v[j];
vint w=v;
sort(ALL(w));
if(w[2]==100) cout<<"A"<<endl;
else if(v[0]+v[1]>=180) cout<<"A"<<endl;
else if(v[0]+v[1]+v[2]>=240) cout<<"A"<<endl;
else if(v[0]+v[1]+v[2]>=210) cout<<"B"<<endl;
else if(v[0]+v[1]+v[2]>=150&&max(v[0],v[1])>=80) cout<<"B"<<endl;
else cout<<"C"<<endl;
}
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout<<fixed<<setprecision(20);
mainmain();
} | #include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<string>
#include<algorithm>
#include<queue>
#include<utility>
using namespace std;
int n;
char student[10010];
double cal(int m,int e,int j)
{
double ave = (double)(m + e + j) / 3;
return ave;
}
int main(void)
{
while (cin >> n, n)
{
for (int i = 0; i < n; i++)
{
int m, e, j;
cin >> m >> e >> j;
if (m==100||e==100||j==100)
{
student[i] = 'A';
continue;
}
if (((double)(m+e)/2)>=90)
{
student[i] = 'A';
continue;
}
if (cal(m,e,j)>=80)
{
student[i] = 'A';
continue;
}
if (cal(m, e, j) >= 70)
{
student[i] = 'B';
continue;
}
if (cal(m, e, j) >= 50)
{
if (m>=80||e>=80)
{
student[i] = 'B';
continue;
}
}
student[i] = 'C';
}
for (int i = 0; i < n; i++)
{
cout << student[i] << endl;
}
}
return 0;
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
long long int n,i,x,a;
string s,t;
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>s>>t;
n=s.size();
for (i=0;i<n;i++) t.push_back(s[i]);
cout<<t;
} | #include <stdio.h>
int main ()
{
char a[200], b[200];
scanf("%s %s", a, b) ;
printf("%s%s\n", b, a) ;
} | 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int N;
while ( cin >> N ) {
if ( N == 0 ) {
break;
}
typedef pair<double, string> P;
vector<P> ans;
while ( N-- ) {
string l;
int p;
cin >> l >> p;
int t = 0;
int d = 0;
for ( int i = 0; i < 5; i++ ) {
int a;
cin >> a;
if ( i <= 2 ) t += a;
else d += a;
}
int f, s, m;
int sum = 0;
cin >> f >> s >> m;
t += d*m;
sum = f*s*m;
ans.push_back(P(-((double)(sum-p)/(double)t), l));
}
sort(ans.begin(), ans.end());
for ( int i = 0; i < ans.size(); i++ ) {
cout << ans[i].second << endl;
}
cout << '#' << endl;
}
return 0;
}
| //include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <queue>
using namespace std;
//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
//container util
//------------------------------------------
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
int main(){
cin.tie(0);
ios_base::sync_with_stdio(false);
int N;
while(cin >> N, N){
vector< pair<double,string> > k;
REP(i,N){
string L; int P, A, B, C, D, E, F, S, M;
cin >> L >> P >> A >> B >> C >> D >> E >> F >> S >> M;
int tot = A + B + C + D + E + (M - 1)*(D + E);
int pr = F * S * M - P;
k.PB(MP(pr*1.0/tot, L));
}
sort(ALL(k), [](const pair<double,string>& p1, const pair<double,string>& p2)->bool{
return p1.first > p2.first || (abs(p1.first-p2.first) < EPS
&& p1.second < p2.second);
});
REP(i,N)
cout << k[i].second << endl;
cout << "#" << endl;
}
return 0;
} | 1 |
//
// honestorunkind2.cpp
// my_programs
//
// Created by Jiatuo Zou on 7/27/20.
// Copyright © 2020 Jiatuo Zou. All rights reserved.
//
#include <iostream>
using namespace std;
int f = 0;
int combination(int n, int xy[][2], int A[], int b[], int d, int MAX){
if(d == n){
f = 0;
for(int i = 1; i <= n; i++){
for(int j = A[i - 1]; j < A[i]; j++){
if(b[i - 1] == 1 && b[xy[j][0] - 1] != xy[j][1]){
f = 1;
break;
}
}
if(f == 1){
break;
}
}
if(f == 0){
for(int i = 0; i < n; i++){
if(b[i] == 1){
f++;
}
}
MAX = max(MAX, f);
}
}
if(d < n){
for(int i = 0; i <= 1; i++){
b[d] = i;
MAX = combination(n, xy, A, b, d + 1, MAX);
}
}
return MAX;
}
int main()
{
int n, xy[210][2] = {{0}}, A[16] = {0}, b[15];
cin >> n;
for(int i = 1; i <= n; i++){
int a;
cin >> a;
A[i] += (a + A[i - 1]);
for(int j = A[i - 1]; j < A[i]; j++){
cin >> xy[j][0] >> xy[j][1];
}
}
cout << combination(n, xy, A, b, 0, 0) << endl;
return 0;
}
| #include <algorithm>
#include <iostream>
#include <vector>
#include <map>
using namespace std;
int MSB1(long long x) { int d = 0; while ((1LL << d) <= x) d++; return d; }
int main() {
int n; cin >> n;
vector<int> a(n);
map<int, int> cnt;
for (auto &ai: a) cin >> ai, cnt[ai]++;
sort(a.rbegin(), a.rend());
int ans = 0;
for (int val: a) if (cnt[val]) {
cnt[val]--;
int rem = (1 << MSB1(val)) - val;
if (cnt[rem]) ans++, cnt[rem]--;
}
cout << ans << endl;
return 0;
}
| 0 |
#include <iostream>
#include <algorithm>
#include <vector>
typedef std::vector<int> TList;
TList Input()
{
int Count;
std::cin >> Count;
TList List(Count);
for(int i = 0; i < Count; ++i){
std::cin >> List[i];
}
return List;
}
int Count(const TList& S, const TList& T)
{
int Sum = 0;
for(TList::const_iterator It = T.begin(); It != T.end(); ++It){
Sum += std::find(S.begin(), S.end(), *It) != S.end() ? 1 : 0;
}
return Sum;
}
int main()
{
TList S = Input();
TList T = Input();
std::cout << Count(S, T) << std::endl;
} | #define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
int main(int argc, const char * argv[]){
//Input
int i, j, n, q, ans;
//n ????????´??°???????????°??? S
scanf("%d", &n);
int S[n];
for(i=0; i < n; i++){
scanf("%d", &S[i]);
}
//q ????????°????????´??°???????????°??? T
scanf("%d", &q);
int T[q];
for(i=0; i < q; i++){
scanf("%d", &T[i]);
}
ans = 0;
for(i=0; i < q; i++){
for(j=0; j < n; j++){
if(T[i] == S[j]){
ans++;
break;
}
}
}
printf("%d\n", ans);
return 0;
} | 1 |
#include<bits/stdc++.h>
using namespace std;
#define f1(a,b,c) for(int c=a;c<=b;c++)
#define f2(a,b,c) for(int c=a;c>=b;c--)
#define f3(a,b,c) for(int c=a;c;c=b)
#define so1(a,n) sort(a+1,a+n+1,mycmp);
#define so2(a,n) sort(a+1,a+n+1);
#define ll long long
#define itn int
#define ubt int
#define pii pair<int,int>
#define mp make_pair
const int twx=1e6+100;
const int inf=0x7fffffff;
ll read()
{
ll sum=0;
ll flag=1;
char c=getchar();
while(c<'0'||c>'9')
{
if(c=='-')
{
flag=-1;
}
c=getchar();
}
while(c>='0'&&c<='9')
{
sum=((sum*10)+c-'0');
c=getchar();
}
return sum*flag;
}
int n,m;
struct LV
{
int Next;
int y;
int v;
}a[twx<<1];
int Link[twx];
int len;
map<pii,int> p;
int cnt=0;
bool v[twx];
int d[twx];
void Insert(int x,int y,int z)
{
a[++len].y=y;
a[len].Next=Link[x];
a[len].v=z;
Link[x]=len;
}
int get(int x,int y)
{
if(p.find(mp(x,y))!=p.end())
{
return p[mp(x,y)];
}
else
{
return p[mp(x,y)]=++cnt;
}
}
void spfa()
{
memset(d,0x3f,sizeof d);
queue<int> q;
q.push(1);
v[1]=1;
d[1]=0;
while(!q.empty())
{
int x=q.front();
q.pop();
f3(Link[x],a[i].Next,i)
{
int y=a[i].y;
int vs=a[i].v;
if(d[y]>d[x]+vs)
{
d[y]=d[x]+vs;
if(!v[y])
{
v[y]=1;
q.push(y);
}
}
}
v[x]=0;
}
}
void init()
{
n=read();
m=read();
cnt=n;
f1(1,m,i)
{
int x=read();
int y=read();
int z=read();
int xz=get(x,z);
int yz=get(y,z);
Insert(xz,yz,0);
Insert(yz,xz,0);
Insert(x,xz,1);
Insert(xz,x,1);
Insert(y,yz,1);
Insert(yz,y,1);
}
spfa();
printf("%d\n",d[n]==0x3f3f3f3f?-1:d[n]/2);
}
void work()
{
}
void print()
{
}
int main()
{
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
init();
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> pipii;
typedef pair<pii, pii> piipii;
#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
#define eb emplace_back
int dp[300005];
vector<pii> g[300005];
vector<pii> edge[1000005];
int p[100005];
int idx[100005];
int root(int a){
return p[a] == a?a:(p[a]=root(p[a]));
}
void merge(int a, int b){
a = root(a), b = root(b);
if(a != b) p[a] = b;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
for(int i=1;i<=n;i++) p[i] = i;
for(int i=1;i<=m;i++){
int a, b, c;
cin >> a >> b >> c;
edge[c].eb(mp(a, b));
}
int cnt = n+1;
for(int i=1;i<=1000000;i++){
vector<int> v;
for(int j=0;j<sz(edge[i]);j++){
int a = edge[i][j].fi, b = edge[i][j].se;
merge(a, b);
v.eb(a), v.eb(b);
}
sort(all(v));
v.erase(unique(all(v)), v.end());
for(int i=0;i<sz(v);i++){
int u = v[i];
if(root(u) == u) idx[u] = cnt++;
}
for(int i=0;i<sz(v);i++){
int u = v[i];
g[u].eb(mp(idx[root(u)], 1));
g[idx[root(u)]].eb(mp(u, 0));
}
for(int i=0;i<sz(v);i++) p[v[i]] = v[i];
}
deque<pii> dq;
memset(dp, -1, sizeof(dp));
dp[1] = 0;
dq.push_front(mp(dp[1], 1));
while(!dq.empty()){
int u = dq.front().se;
int val = dq.front().fi;
dq.pop_front();
if(val != dp[u]) continue;
for(int i=0;i<sz(g[u]);i++){
int v = g[u][i].fi, w = g[u][i].se;
if(dp[v] == -1 || dp[v] > dp[u] + w){
dp[v] = dp[u] + w;
if(w == 0) dq.push_front(mp(dp[v], v));
else dq.push_back(mp(dp[v], v));
}
}
}
printf("%d\n", dp[n]);
} | 1 |
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <map>
#include <bitset>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
#define FOR(i,a,b) for(ll i = (a); i < (b); i++ )
#define REP(i, n) FOR(i,0,n)
typedef pair< int, int > cp2;
typedef pair< int, cp2 > cp3;
#define fi first
#define se second
#define sec se.fi
#define thr se.se
const ll mod = 1000000007;
// 123456789
///////////////////////////////////////////////
//
//
///////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
priority_queue< cp3, vector<cp3>, greater<cp3> >p;
cp3 now;
int g[112];
int find( int key ){
//cout<<key<<" "<<g[key]<<endl;
if( g[key] < 0 ) return key;
return g[key] = find( g[key] );
}
void unite( int x, int y ){
x = find( x );
y = find( y );
if( x == y ) return;
if( g[x] < g[y] ) swap( x, y );
if( g[x] == g[y] ) g[y]--;//??±??????x???????????????
//g[y] += g[x]; //????????°???x??????????°???????
g[x] = y;
}
int a[112][112];
int N;
int ans = 0;
int main(){
cin>>N;
fill( g, g+112, -1 );
REP( i, N ){
REP( j, N ){
scanf("%d",&a[i][j]);
if( a[i][j] != -1 ){
p.push( cp3( a[i][j], cp2(i, j) ) );
//printf("%d %d %d\n",p[idx].fi, p[idx].sec, p[idx].thr);
}
}
}
while( !p.empty() ){
now = p.top();
p.pop();
if( find(now.sec) != find(now.thr) ){
unite( now.sec, now.thr );
ans += now.fi;
}
}
cout<<ans<<endl;
return 0;
} | #include<bits/stdc++.h>
using namespace std;
int main () {
int N, K;
cin >> N >> K;
vector<int>pl;
vector<int>mn;
pl.push_back(0);
mn.push_back(0);
for (int i = 0; i < N; i ++) {
int a;
cin >> a;
if (a < 0) mn.push_back(-a);
else pl.push_back(a);
}
int kj = min((int)mn.size(), K + 1);
int n = (int)mn.size();
sort(mn.begin(), mn.end());
int m = (int)pl.size();
int ans = 1e9;
for (int i = 0; i < kj; i ++) {
int j = K - i;
if (j < m) ans = min(ans, min((pl[j] * 2) + mn[i], pl[j] + (mn[i] * 2)));
}
cout << ans << endl;
}
| 0 |
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <string>
#include <set>
#include <math.h>
#include <map>
#include <stack>
using namespace std;
static const int INF = 1e9+7;
// 型定義
typedef long long ll;
typedef pair<ll, ll> P;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, a, b) for (int i =a; i < b; i++)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define PI 3.14159265359
int main(){
string S; cin >> S;
ll res = 1000000000;
rep(i, S.size()-2){
int tmp = (S[i] - '0')*100 + (S[i+1] - '0')*10 + (S[i+2]-'0');
int tmp2 = abs(tmp - 753);
if(tmp2 < res){
res = tmp2;
}
}
cout << res << endl;
return 0;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i=0; i<(int)(n); i++)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
const ll MOD = 1000000007;
const ll INF = 10000000000000000;
vector<int> x4 = {0, 1, 0, -1}, x8 = {0, 1, 1, 1, 0, -1, -1, -1};
vector<int> y4 = {1, 0, -1, 0}, y8 = {1, 1, 0, -1, -1, -1, 0, 1};
template<class T> struct edge { int from, to; T cost;};
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 T powerM(T a,T b){if (b==0) return 1;
T tmp = powerM(a,b/2); if (b%2==0) return tmp*tmp%MOD; else return tmp*tmp%MOD*a%MOD; }
template<class T> inline T power(T a,T b,T m){ if (b==0) return 1;
T tmp = power(a,b/2,m); if (b%2==0) return tmp*tmp%m; else return tmp*tmp%m*a%m; }
template<class T> inline T gcd(T a, T b){if (b==0) return a; return gcd(b, a%b);}
template<class T> inline T lcm(T a, T b){return a / gcd(a,b) * b;}
// ax+by=gcd(a,b)を解く
template<class T> inline T extgcd(T a,T b,T &x,T &y){if (b==0){x=1; y=0; return a;} T d=extgcd(b,a%b,y,x); y -= a/b*x; return d;}
/*
int N;
Graph G;
int main() {
cin >>N;
rep(i, N-1){
int a,b; cin >>a >>b; a--; b--;
G[a].push_back(b); G[b].push_back(a);
}
}
*/
int main() {
string s; cin >>s;
int n = s.size();
rep(i, n-8) cout <<s[i];
cout <<endl;
} | 0 |
#include <algorithm>
#include <iostream>
#include <limits.h>
#include <string>
#include <vector>
#define el endl
#define fd fixed
using namespace std;
class Point
{
private:
int m_x;
int m_y;
public:
void setX(int x);
void setY(int y);
int getX();
int getY();
Point(int x, int y) {
setX(x);
setY(y);
}
};
void Point::setX(int x) {
m_x = x;
}
void Point::setY(int y) {
m_y = y;
}
int Point::getX() {
return m_x;
}
int Point::getY() {
return m_y;
}
int main() {
int N, x, y, M, l, rx, ry;
string d;
while (1) {
cin >> N;
vector<Point> gems;
if (N == 0) break;
for (int i = 0; i < N; i++) {
cin >> x >> y;
gems.push_back(Point(x, y));
}
cin >> M;
rx = 10;
ry = 10;
while (M--) {
cin >> d >> l;
while (l--) {
if (d == "N") ry++;
if (d == "E") rx++;
if (d == "S") ry--;
if (d == "W") rx--;
for (int i = 0; i < gems.size(); i++) {
if (rx == gems[i].getX() && ry == gems[i].getY()) {
gems.erase(gems.begin()+i);
break;
}
}
}
}
if (gems.empty()) cout << "Yes" << el;
else cout << "No" << el;
}
} | #include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
int main() {
int c[6] = {0},same[5] = {0};
while(scanf("%d,%d,%d,%d,%d\n",&c[0],&c[1],&c[2],&c[3],&c[4]) == 5) {
int same[5] = {0};
int straight = 0;
for(int i=0;i<5;i++) {
for(int j=0;j<5;j++) {
if(c[i] == c[j]) {
same[i]++;
}
}
}
sort(c,c+5);
sort(same,same+5);
if(same[4] == 4) {
cout << "four card" << endl;
}
else if(same[4] == 3) {
if(same[1] == 2) {
cout << "full house" << endl;
}
else {
cout << "three card" << endl;
}
}
else if(same[4] == 2) {
if((same[3] == 2) &&(same[2] == 2)) {
cout << "two pair" << endl;
}
else {
cout << "one pair" << endl;
}
}
else if(same[4] == 1) {
for(int i=0;i<4;i++) {
if(c[i] == c[i+1]-1) {
straight = 1;
}
else {
straight = 0;
break;
}
}
if((c[0] == 1) && (c[1] == 10)) {
straight = 1;
}
if(straight) {
cout << "straight" << endl;
}
else {
cout << "null" << endl;
}
}
}
} | 0 |
#pragma GCC optimize ("O3")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#include<bits/stdc++.h>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define ll long long
#define ld long double
#define all(a) a.begin(),a.end()
#define endl '\n'
#define ull unsigned long long
#define y1 ljhadglkjsadf
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define iter multiset<int>::iterator
#define iter1 set<int>::iterator
#define int long long
using namespace std;
using namespace __gnu_pbds;
template<class T>
using ordered_set=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
template<class T>
using ordered_multiset=tree<T,null_type,less_equal<T>,rb_tree_tag,tree_order_statistics_node_update>;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rnd1(chrono::steady_clock::now().time_since_epoch().count());
//find_by_order
//order_of_key
const int N=2e5+7;
const int inf=1e18+1e9;
const int mod=1e9+7;
const ld eps=1e-9;
//const int MAX_MEM=4e8;
//int mpos=0;
//char mem[MAX_MEM];
//
//inline void * operator new(size_t n){
// char *res=mem+mpos;
// mpos+=n;
// if (mpos>=MAX_MEM){
// cout<<"BAD"<<endl;
// exit(0);
// }
// return (void*)res;
//}
//
//inline void operator delete(void *) {}
int st[50];
main ()
{
st[0]=1;
for (int i=1;i<35;++i){
st[i]=st[i-1]*2;
}
ios;
int n;
multiset<int>setik;
cin>>n;
for (int i=1;i<=n;++i){
int x;
cin>>x;
setik.insert(x);
}
int ans=0;
while(setik.size()>0){
iter it=setik.end();
--it;
int x=(*it);
setik.erase(it);
int k=0;
while(st[k]<=x)++k;
int l=st[k]-x;
iter it1=setik.find(l);
if (it1!=setik.end()){
++ans;
setik.erase(it1);
}
}
cout<<ans<<endl;
}
//1
//3 15
//1 4
//3 5
| #define _USE_MATH_DEFINES
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<ll, ll, ll> tl3;
//typedef modint998244353 mint;
const int BIG_NUM = 1e9;
const ll INF = 1000000000000000000;
const ll MOD = 1e9 + 7;
//const ll MOD = 998244353;
const ll MAX = 1e9 + 5;
int main() {
int n;
cin >> n;
map<int, int> m;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
m[a]++;
}
int cnt = 0;
for (auto it = m.rbegin(); it != m.rend(); it++) {
ll p2 = 1;
ll num = it->first;
while (p2 <= num) {
p2 *= 2;
}
//cout << num << endl;
auto it1 = m.find(p2 - num);
if (it1 != m.end()) {
if (it1->first == num) {
cnt += it->second / 2;
}
else {
int c = min(it1->second, it->second);
it1->second -= c;
cnt += c;
if (it1->second == 0) {
m.erase(it1);
}
}
}
}
cout << cnt << endl;
} | 1 |
#include <set>
#include <map>
#include <queue>
#include <ctime>
#include <cmath>
#include <cstdio>
#include <bitset>
#include <vector>
#include <cstring>
#include <cassert>
#include <iostream>
#include <algorithm>
using namespace std;
typedef double db;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int,int> pa;
typedef unsigned int uint;
typedef unsigned long long ull;
#define w1 first
#define ls (x<<1)
#define w2 second
#define ins insert
#define rs (x<<1|1)
#define mp make_pair
#define pb push_back
#define mid ((l+r)>>1)
#define sqr(x) ((x)*(x))
#define cle(x) ((x).clear())
#define lowbit(x) ((x)&(-x))
#define SZ(x) (int((x).size()))
#define ms(x,y) memset(x,y,sizeof (x))
#define rep(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define rep2(i,a,b) for(int (i)=(a);(i)<(b);(i)++)
#define per(i,a,b) for(int (i)=(a);(i)>=(b);(i)--)
#define Rep(p,x) for(int (p)=head[(x)];(p);(p)=nxt[(p)])
#define Rep2(p,x) for(int (p)=cur[(x)];(p);(p)=nxt[(p)])
template<class T>inline void read(T&num){
num=0;T f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9')num=num*10+ch-'0',ch=getchar();
num*=f;
}
inline int getgcd(int x,int y){if(!x)return y;return getgcd(y%x,x);}
inline int power(int x,int k,int p){int res=1;for(;k;k>>=1,x=(ll)x*x%p)if(k&1)res=(ll)res*x%p;return res;}
const double pi=acos(-1);
inline void judge(){
freopen("input.txt","r",stdin);
}
//********************************head*************************************
const int maxn=2e3+5;
int n,m,top,ans;
char str[maxn][maxn];
int a[maxn][maxn],h[maxn][maxn],L[maxn],R[maxn];
pa st[maxn];
int main(){
read(n);read(m);
rep(i,1,n)scanf("%s",str[i]+1);
rep(i,1,n-1)rep(j,1,m-1){
int cnt=0;
cnt+=(str[i][j]=='.');
cnt+=(str[i+1][j]=='.');
cnt+=(str[i][j+1]=='.');
cnt+=(str[i+1][j+1]=='.');
a[i][j]=!(cnt&1);
}
rep(i,1,n-1)rep(j,1,m-1){
if(a[i][j])h[i][j]=h[i-1][j]+1;
else h[i][j]=0;
}
ans=max(n,m);
rep(i,1,n-1){
st[top=1]=mp(0,-1);
rep(j,1,m-1){
while(top&&st[top].w2>=h[i][j])top--;
L[j]=st[top].w1+1;
st[++top]=mp(j,h[i][j]);
}
st[top=1]=mp(m,-1);
per(j,m-1,1){
while(top&&st[top].w2>=h[i][j])top--;
R[j]=st[top].w1-1;
st[++top]=mp(j,h[i][j]);
}
rep(j,1,m-1)ans=max(ans,(h[i][j]+1)*(R[j]-L[j]+2));
}
printf("%d\n",ans);
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define REP(i, n) for(int (i) = 0; (i) < (n); ++(i))
#define REPR(i, n) for(int (i) = (n); (i) >= 0; --(i))
#define FOR(i, n, m) for(int (i) = (n); (i) < (m); ++(i))
constexpr int INF = 1e9;
//constexpr ll INF = 1LL<<61;
constexpr ll mod = 1e9+7;
int main(){
string S;
cin >> S;
int N = S.size();
int ans = 0;;
int c = 0;
REPR(i, N-1){
int x = S[i] - '0';
if((x+c) > 5){
ans += 10 - (x+c);
c = 1;
}
else if((x+c) == 5){
if(i > 0 && S[i-1] >= '5'){
ans += 10 -(x+c);
c= 1;
}
else{
ans += (x+c);
c = 0;
}
}
else{
ans+= x+c;
c = 0;
}
}
ans += c;
cout << ans << endl;
return 0;
}
| 0 |
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a) (a).begin(),(a).end()
#define pb push_back
int solve(int a,int b,int c){
if(c==0)return 0;
if(b<=c&&b<=a)return b;
return 0;
}
int main(){
string s;
cin>>s;
int ans=0;
static int J[2000000]={},O[2000000]={},I[2000000]={};
if(s[0]=='J')J[0]=1;
for(int i=1;i<s.size();i++){
// cout<<s[i-1]<<" "<<J[i-1]<<" "<<O[i-1]<<" "<<I[i-1]<<endl;
if(s[i]=='J'){
if(O[i-1]!=0){
ans=max(ans,solve(J[i-1],O[i-1],I[i-1]));
J[i]=1;
}
else{
J[i]=J[i-1]+1;
O[i]=O[i-1];
I[i]=I[i-1];
}
}else if(s[i]=='O'){
if(J[i-1]!=0&&I[i-1]==0){
J[i]=J[i-1];
O[i]=O[i-1]+1;
I[i]=I[i-1];
}else{
ans=max(ans,solve(J[i-1],O[i-1],I[i-1]));
}
}else if(s[i]=='I'){
if(O[i-1]!=0){
J[i]=J[i-1];
O[i]=O[i-1];
I[i]=I[i-1]+1;
}
else{
ans=max(ans,solve(J[i-1],O[i-1],I[i-1]));
}
}
}
ans=max(ans,solve(J[s.size()-1],O[s.size()-1],I[s.size()-1]));
cout<<ans<<endl;
} | #include<iostream>
#include<cstdlib>
#include<algorithm>
#include<stdio.h>
using namespace std;
int ans, Jcou, Ocou, Icou;
char letter, target='A';
int main(){
while(1){
scanf("%c", &letter);
if(letter == '\n'){break;}
if(target == 'A' && letter == 'J'){
Jcou = 1;
Ocou = 0;
Icou = 0;
target = 'J';
}
else if(target == 'J'){
if(letter == 'J'){
Jcou++;
}
else if(letter == 'O'){
Ocou++;
target = 'O';
}
else if(letter == 'I'){
target = 'A';
}
}
else if(target == 'O'){
if(letter == 'J'){
target = 'J';
Jcou = 1;
Ocou = 0;
Icou = 0;
}
else if(letter == 'O'){
Ocou++;
}
else if(letter == 'I'){
if(Jcou < Ocou){
target = 'A';
}
else{
target = 'I';
Icou++;
if(Ocou == Icou){
ans = max(ans, Icou);
target = 'A';
}
}
}
}
else if(target == 'I'){
if(letter == 'J'){
if(Ocou<=Icou){
ans = max(ans, Ocou);
target = 'J';
Jcou = 1;
Ocou = 0;
Icou = 0;
}
else{
target = 'J';
Jcou = 1;
Ocou = 0;
Icou = 0;
}
}
else if(letter == 'O'){
if(Ocou <= Icou){
ans = max(ans, Ocou);
target = 'A';
}
else{
target = 'A';
}
}
else if(letter == 'I'){
// cout << "I in" << endl;
Icou++;
if(Ocou==Icou){
ans = max(ans, Icou);
target = 'A';
}
}
}
// cout << Jcou << " " << Ocou << " " << Icou << " " << endl;
}
cout << ans << endl;
return 0;
} | 1 |
#include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define int long long
#define INF 1e18
#define ff first
#define ss second
#define vi vector<int>
#define pii pair<int,int>
#define mii map<int,int>
#define endl "\n"
#define GCD(x,y) (__gcd((x), (y)))
#define LCM(x,y) (((x)/__gcd((x), (y)))*(y))
#define debug(x) cout<<#x<<" is "<<(x)<<endl
#define mem(a,x) memset(a,x,sizeof(a))
#define rep(i,a,b) for(long long i=a;i<b;i++)
#define sp(ans,p) fixed <<setprecision(p)<<ans;
#define IOS ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
typedef tree<pii, null_type, less<pii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
constexpr int N = 5e5 + 5;
constexpr int BLOCK = 710;
struct query
{
int l;
int r;
int i;
};
query Q[N];
int ar[N] , ans[N];
int fre[N];
int cntdist = 0;
mii m;
//enhanced comparator
bool comp(query a, query b)
{
if (a.l / BLOCK != b.l / BLOCK)
{
return a.l < b.l;
}
if ((a.l / BLOCK) & 1)
{
return a.r < b.r;
}
return a.r > b.r;
}
void add(int pos)
{
if (fre[ar[pos]] == 0)
{
cntdist++;
}
fre[ar[pos]]++;
}
void remove(int pos)
{
if (fre[ar[pos]] == 1)
{
cntdist--;
}
fre[ar[pos]]--;
}
void solve()
{
int n , q;
cin >> n >> q;
for (int i = 0; i < n; i++)
cin >> ar[i];
for (int i = 0; i < q; i++) {
cin >> Q[i].l >> Q[i].r;
Q[i].i = i , Q[i].l-- , Q[i].r--;
}
sort(Q , Q + q , comp);
//why ML = 0 , and MR = -1?
int ML = 0 , MR = -1;
for (int i = 0; i < q; i++)
{
int L = Q[i].l;
int R = Q[i].r;
while (ML > L)
ML-- , add(ML);
while (MR < R)
MR++ , add(MR);
while (ML < L)
remove(ML) , ML++;
while (MR > R)
remove(MR) , MR--;
ans[Q[i].i] = cntdist;
}
for (int i = 0; i < q; i++)
cout << ans[i] << '\n';
return;
}
signed main()
{
IOS;
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
// sieve();
int t = 1;
// cin >> t;
while (t--) solve();
return 0;
} | // 2020-07-15 23:41:12
#include<bits/stdc++.h>
#ifdef LOCAL
#include "lib/debug.hpp"
#else
#define debug(...) 1
#endif
#define ALL(a) (a).begin(), (a).end()
#define rep(i, n) REP(i, 0, (n))
#define repc(i, n) REPC(i, 0, (n))
#define REP(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define REPC(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define REPCM(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
using namespace std;
using ll = long long;
using ld = long double;
using pr = pair<ll, ll>;
using vll = vector<ll>;
using vpr = vector<pr>;
template<class T> inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } else return false; }
template<class T> inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
void answer() {
using Work = tuple<int, int, int>; // (x, s, t)
using P = pair<int, int>;
int n, q;
cin >> n >> q;
vector<Work> works(n);
rep(i, n) {
int s, t, x;
cin >> s >> t >> x;
works[i] = { x, s-x, t-x };
}
sort(ALL(works));
set<P> ds;
rep(i, q) {
int d;
cin >> d;
ds.emplace(d, i);
}
vector<int> ans(q, -1);
for(const auto& w : works) {
int x, s, t;
tie(x, s, t) = w;
auto it = ds.lower_bound({ s, -1 });
while(it != ds.end() && (*it).first < t) {
int d, qi;
tie(d, qi) = *it;
ans[qi] = x;
it++;
ds.erase(prev(it));
}
}
for(const auto x : ans) cout << x << '\n';
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
answer();
return 0;
} | 0 |
#include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < n; ++i)
#define FOR(i, b, n) for (ll i = b; i < n; ++i)
using namespace std;
using ll = long long;
void solve(ll N, ll x, vector<ll> a) {
ll ans(0);
if (a[0] > x) {
ans += a[0] - x;
a[0] = x;
}
FOR(i, 1, N) {
if (a[i - 1] + a[i] <= x)
continue;
ans += a[i - 1] + a[i] - x;
a[i] = x - a[i - 1];
}
cout << ans << endl;
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
ll N, x;
cin >> N >> x;
vector<ll> a(N);
REP(i, N)
cin >> a[i];
solve(N, x, move(a));
return 0;
}
| #include <iostream>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
#include <cctype>
#include <cmath>
#include <queue>
#include <map>
#include <numeric>
#include <unordered_map>
#include <iomanip>
#include <functional>
#include <bitset>
#include <complex>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define all(x) (x).begin(),(x).end()
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
typedef long long ll;
using namespace std;
long long modpow(long long x,long long n,long long mod)
{
if(n==0)return 1;
long long res=modpow(x*x%mod,n/2,mod);
if(n&1)res=res*x%mod;
return res;
}
long long modinv(long long a,long long mod)//extgcdの方がいいらしい
{
return modpow(a,mod-2,mod);
}
vector<int>inv;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n,x;
int mod=1e9+7;
cin>>n>>x;
vector<int>s(n);
rep(i,n)cin>>s[i];
sort(all(s),greater<int>());
inv.resize(n+1);
repi(i,1,n+1)inv[i]=modinv(i,mod);
vector<vector<int>>dp(2,vector<int>(x+1,0));
dp[0][x]=1;
rep(i,n)
{
int no=i&1,ne=1-no;
fill(all(dp[ne]),0);
rep(j,x+1)
{
if(dp[no][j]==0)continue;
int t=(ll)dp[no][j]*inv[n-i]%mod;
int jsi=j%s[i];
dp[ne][jsi]+=t;
dp[ne][j]+=(ll)t*(n-i-1)%mod;
if(dp[ne][jsi]>mod)dp[ne][jsi]-=mod;
if(dp[ne][j]>mod)dp[ne][jsi]-=mod;
}
}
int res=0;
int kai=1;
rep(i,n)kai=(ll)kai*(i+1)%mod;
rep(i,x+1)
{
res+=(ll)kai*dp[n&1][i]%mod*i%mod;
if(res>mod)res-=mod;
}
cout<<res<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
string s;
int sol, ans;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> s;
sol = 0, ans = 0;
for(int i = 0; i < s.size(); i++) {
if(s[i] == 'S') sol++;
else {
if(sol > 0) sol--;
else ans++;
}
}
cout << ans + sol << "\n";
} | #include<iostream>
#include<cstdio>
#include<string>
using namespace std;
int main()
{
string a;
cin>>a;
int tmp=a.find("ST");
while(a.length()>0&&tmp>=0)
{
a.erase(tmp,2);
int x=max(tmp-1,0);
tmp=a.find("ST",x);
if(tmp==string::npos)
tmp=-1;
}
cout<<a.length()<<endl;
return 0;
} | 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
double r, g, x;
cin >> r >> g;
x = 2 * g - r;
cout << x << endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int R, G;
cin>>R>>G;
int res = G + (G-R);
cout<<res<<endl;
return 0;
} | 1 |
#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define int long long
#define fi first
#define se second
using namespace std;
typedef pair<int, int> P;
signed main(){
while(1){
int m, nmin, nmax;
cin >> m >> nmin >> nmax;
if(m + nmin + nmax == 0) break;
vector<int> p(m);
rep(i, 0, m){
cin >> p[i];
}
int ans = 0, MAX = 0;
rep(i, nmin, nmax + 1){
int diff = p[i - 1] - p[i];
if(diff >= MAX){
MAX = diff;
ans = i;
}
}
cout << ans << endl;
}
} | #include<bits/stdc++.h>
using namespace std;
int main(){
int a,b,m;
cin>>a>>b>>m;
vector<int> ai(a);
int ma;
vector<int> bi(b);
int mb;
for(int i=0;i<a;i++){
cin>>ai.at(i);
ma=(i==0?ai.at(i):min(ma,ai.at(i)));
}
for(int i=0;i<b;i++){
cin>>bi.at(i);
mb=(i==0?bi.at(i):min(mb,bi.at(i)));
}
int ms=ma+mb;
for(int _=0;_<m;_++){
int x,y,c;
cin>>x>>y>>c;
x--;
y--;
ms=min(ms,ai.at(x)+bi.at(y)-c);
}
cout<<ms<<endl;
} | 0 |
#include<iostream>
#include<cstdio>
#include <stdio.h>
#include<algorithm>
#include<cstring>
#include <string>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<map>
#include<vector>
#include<bits/stdc++.h>
#include <set>
#define ll long long
#define IO ios::sync_with_stdio(false);cin.tie();cout.tie(0)
#define inf 0x3f3f3f3f3f3f
#define pi 3.1415926535898
using namespace std;
const int N=2e5+10;
const int mod=1e9+7;
int a[N],b[N];
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
for(int i=1;i<=n;i++)
{
cin>>b[i];
}
reverse(b+1,b+n+1);
vector<int >p,q;
int val=0,cnt=0;
for(int i=1;i<=n;i++)
{
if(a[i]!=b[i])
continue;
cnt++;
q.push_back(i);
val=b[i];
}
for(int i=1;i<=n;i++)
{
if(!cnt)
break;
if(a[i]==b[i])
continue;
if(a[i]!=val&&b[i]!=val)
{
cnt--;
p.push_back(i);
}
}
if(cnt)
{
cout<<"No"<<endl;
return 0;
}
for(int i=0;i<q.size();i++)
{
swap(b[q[i]],b[p[i]]);
}
cout<<"Yes"<<endl;
for(int i=1;i<=n;i++)
{
cout<<b[i]<<" ";
}
cout<<endl;
return 0;
}
| #include <iostream>
using namespace std;
int main(int argc, const char * argv[]) {
int N;
cin>>N;
int *vCount=new int[N],*b=new int[N],*c=new int[N+1];
for(int i=0;i<N;++i){
vCount[i]=0;
}
bool yesFlg=true;
int oldA=-1,ci=0;
for(int i=0;i<N;++i){
int a1;
cin>>a1;
++vCount[a1-1];
if(vCount[a1-1]>N){
yesFlg=false;
break;
}
if(oldA!=a1){
for(;ci<a1;++ci){
c[ci]=i;
}
oldA=a1;
}
}
for(;ci<=N;++ci){
c[ci]=N;
}
int oldB=-1,di=0;
int x=0;
for(int i=0;i<N&&yesFlg;++i){
int b1;
cin>>b1;
b[i]=b1;
++vCount[b1-1];
if(vCount[b1-1]>N){
yesFlg=false;
break;
}
if(oldB!=b1){
for(;di<b1;++di){
int d=i;
int x1=c[di+1]-d;
if(x<x1){
x=x1;
}
}
oldB=b1;
}
}
for(;di<N;++di){
int d=N;
int x1=c[di+1]-d;
if(x<x1){
x=x1;
}
}
if(yesFlg){
cout<<"Yes"<<endl;
for(int i=0;i<N;++i){
int idx=(i-x)%N;
if(idx<0){
idx+=N;
}
int b1=b[idx];
if(i==0){
cout<<b1;
}else{
cout<<" "<<b1;
}
}
cout<<endl;
}else{
cout<<"No"<<endl;
}
delete[] b;
delete[] c;
delete[] vCount;
return 0;
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int a, b;
string s;
cin >> a >> b;
cin >> s;
vector<char> num{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
if(s.at(a) == '-'){
vector<bool> judge2 (s.size(), false);
judge2.at(a) = true;
for(int i = 0;i < s.size();i++){
for(int j = 0;j < num.size();j++){
if(i == a)continue;
else if(s.at(i) == num.at(j))judge2.at(i) = true;
}
}
bool judge = true;
for(int i = 0;i < judge2.size();i++){
if(!judge2.at(i))judge = false;
}
if(judge)cout << "Yes" << endl;
else cout << "No" << endl;
}
else cout << "No" << endl;
} | #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main(){
int n;cin>>n;
string a,b,c;cin>>a>>b>>c;
int acc = 0;
for (int i=0;i<n;i++){
vector<char> letters;
letters.push_back(a[i]);
letters.push_back(b[i]);
letters.push_back(c[i]);
sort(letters.begin(), letters.end());
letters.erase(unique(letters.begin(), letters.end()), letters.end());
if (letters.size() == 2){
acc++;
} else if (letters.size() == 3){
acc += 2;
}
}
cout << acc << endl;
return 0;
} | 0 |
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
void swap(int *,int *);
int main(){
int n,a[100],c=0,flag;
cin >> n;
for(int i=0;i<n;i++) cin >> a[i];
flag=1;
while(flag){
flag=0;
for(int j=n-1;j>0;j--){
if(a[j]<a[j-1]){
swap(&a[j],&a[j-1]);
c++;
flag=1;
}
}
}
for(int i=0;i<n;i++){
cout << a[i];
if(i!=n-1) cout << " ";
}
cout << endl;
cout << c << endl;
return 0;
}
void swap(int *a,int *b){
int w;
w= *a;
*a=*b;
*b=w;
} | #include <stdio.h>
#define MAX_N (105)
static
void
bubbleSort( int A[], int N )
{
int i, j;
int count = 0;
for ( i = 0; i < N; i++ ) {
int flag = 0;
for ( j = N-1; j > i; j-- ) {
if ( A[j-1] > A[j] ) {
int tmp = A[j-1];
A[j-1] = A[j];
A[j] = tmp;
flag = 1;
count++;
}
}
if ( !flag ) {
break;
}
}
for ( i = 0; i < N; i++ ) {
printf("%d%c", A[i], (i == N - 1) ? '\n' : ' ');
}
printf("%d\n", count);
return;
}
int main()
{
int i;
int N;
int A[MAX_N];
scanf("%d", &N);
for ( i = 0; i < N; i++ ) {
scanf("%d", &A[i]);
}
bubbleSort( A, N );
return 0;
} | 1 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
#define VL vector<ll>
#define VS vector<string>
#define VB vector<bool>
#define VP vector<pair<ll,ll>>
#define VVL vector<vector<ll>>
#define VVP vector<vector<pair<ll,ll>>>
#define PL pair<ll,ll>
ll d1[4] = {1, -1, 0, 0};
ll d2[4] = {0, 0, 1, -1};
string dp[100000];
string smax(string a, string b){
if(a.size() > b.size()){
return a;
}else if(a.size() < b.size()){
return b;
}else{
rep(i, 0, a.size()){
if(a[i] > b[i]){
return a;
}else if(a[i] < b[i]){
return b;
}
}
}
return a;
}
int main(){
ll N, M;
cin >> N >> M;
VL A(M);
ll c[10] = {0,2,5,5,4,5,6,3,7,6};
rep(i, 0, M) cin >> A[i];
rep(i, 0, 100000){
dp[i] = "-";
}
dp[0] = "";
rep(i, 0, N+1){
if(dp[i] == "-") continue;
for(auto a : A){
if(dp[i+c[a]] == "-") dp[i+c[a]] = dp[i] + (char)(a + '0');
else dp[i+c[a]] = smax(dp[i+c[a]], dp[i] + (char)(a + '0'));
}
}
cout << dp[N] << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(),(x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using P = pair<int, int>;
#define debug(var) cerr << "[" << #var << "] " << var << endl
const ll mod = 1000000007;
const int INF = 1001001001;
int main(){
int n, m;
cin >> n >> m;
int arr[9] = {2, 5, 5, 4, 5, 6, 3, 7, 6};
vector<int> a(m);
rep(i, m) cin >> a[i];
sort(whole(a));
reverse(whole(a));
vector<int> x(m);
rep(i, m) x[i] = arr[a[i]-1];
vector<P> dp(n+1, P(-INF, -1));
dp[0] = P(0, -1);
rep(i, n+1) {
for (int j=0; j<x.size(); j++) {
int e = x[j];
if (i-e>=0 && dp[i-e].first>=0) {
if (dp[i].first<dp[i-e].first+1) {
dp[i].first = dp[i-e].first + 1;
dp[i].second = a[j];
}
}
}
}
int keta = dp[n].first;
int index = n;
map<int, int> mp;
while (index) {
mp[dp[index].second]++;
index -= arr[dp[index].second-1];
}
string ans;
for (auto m: mp) {
rep(i, m.second) {
ans += m.first+'0';
}
}
reverse(whole(ans));
cout << ans << endl;
return 0;
}
| 1 |
#include <iostream>
#include <string>
#include <cmath>
#include <sstream>
using namespace std;
const int num = 653;
int main() {
string s, c;
cin >> s;
int l = s.size(), ans = 0, minAns = num, p;
for(int i = 0; i < l - 2; ++i)
{
c = s.substr(i, 3);
stringstream str(c);
str >> ans;
p = abs(ans - 753);
if(p < minAns)
minAns = p;
}
cout << minAns;
return 0;
} | #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
using ll = long long;
const int INF = 1001001001;
const ll INF_LL = 1001001001001001001LL;
int main(void){
string s; cin >> s;
int ans = INF;
rep(i,s.size()-2){
string t = "";
t+=s[i];
t+=s[i+1];
t+=s[i+2];
int tmp = stoi(t);
ans = min(ans,abs(753-tmp));
}
cout << ans << endl;
return 0;
}
| 1 |
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<time.h>
#include<vector>
using namespace std;
int main()
{
//ios::sync_with_stdio(false);
//cin.tie(0);
//cout.tie(0);
int n,x,t;
int ans;
scanf("%d%d%d",&n,&x,&t);
if(n%x==0)
ans = n/x*t;
else
ans = n/x*t + t;
printf("%d\n",ans);
return 0;
}
| #include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cassert>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<queue>
#include<stack>
#include<list>
#include <cstring>
#include <functional>
//#include<unordered_map>
//#include<unordered_set>
using namespace std;
#define MOD 1000000007
#define MOD2 998244353
#define INF ((1<<30)-1)
#define LINF ((Int)1<<60)
#define EPS (1e-10)
#define REP(i,n) for(int i=0; i<(int)(n); ++i)
#define RREP(i,n) for(int i=1; i<=(int)(n); ++i)
#define FOR(i,k,n) for(int i=(k);i<(int)(n);++i)
typedef long long Int;
typedef pair<Int, Int> P;
typedef vector<double> vec;
typedef vector<vec> mat;
const int N = 100005;
//////////////////////////////
void add(int64_t& a, int64_t b) {
a = (a + b) % MOD;
}
void mul(int64_t& a, int64_t b) {
a = a*b % MOD;
}
vector<int64_t> fact, seq_inv, fact_inv;
void create_fact_mod(int num) {
fact[0] = fact[1] = 1;
for (int i = 2; i <= num; i++) fact[i] = fact[i - 1] * i % MOD;
}
void create_seq_inv_mod(int num) {
seq_inv[0] = seq_inv[1] = 1;
for (int i = 2; i <= num; i++) seq_inv[i] = (MOD - MOD / i) * seq_inv[MOD%i] % MOD;
}
void create_fact_inv_mod(int num) {
fact_inv[0] = fact_inv[1] = 1;
for (int i = 2; i <= num; i++) fact_inv[i] = fact_inv[i - 1] * seq_inv[i] % MOD;
}
void create_mod_tables(int num) {
fact.resize(num + 1);
seq_inv.resize(num + 1);
fact_inv.resize(num + 1);
create_fact_mod(num);
create_seq_inv_mod(num);
create_fact_inv_mod(num);
}
int64_t comb_mod(int n, int k) {
return fact[n] * fact_inv[n - k] % MOD * fact_inv[k] % MOD;
}
int64_t perm_mod(int n, int k) {
return fact[n] * fact_inv[n - k] % MOD;
}
int64_t power_mod(int64_t num, int64_t power) {
int64_t prod = 1;
num %= MOD;
while (power > 0) {
if (power & 1) prod = prod * num % MOD;
num = num * num % MOD;
power >>= 1;
}
return prod;
}
int64_t extgcd(int64_t a, int64_t b, int64_t& x, int64_t& y) {
int64_t d = a;
if (b != 0) {
d = extgcd(b, a%b, y, x);
y -= (a / b) * x;
}
else {
x = 1; y = 0;
}
return d;
}
int64_t inv_mod(int64_t a) {
int64_t x, y;
extgcd(a, MOD, x, y);
return (MOD + x%MOD) % MOD;
}
void solve()
{
int M, A, B, C, D;
cin >> M >> A >> B >> C >> D;
create_mod_tables(1000);
static int64_t dp[1001][1001];
dp[A - 1][0] = 1;
for (int i = A; i <= B; i++)
for (int j = 0; j <= M; j++) {
add(dp[i][j], dp[i - 1][j]);
int n = M - j;
int64_t pw = power_mod(fact_inv[i], C);
for (int k = C; k <= D && i*k <= n; k++) {
int64_t res = comb_mod(n, i*k) * fact[i*k] % MOD * pw % MOD * fact_inv[k] % MOD;
mul(pw, fact_inv[i]);
add(dp[i][j + i*k], dp[i - 1][j] * res);
}
}
cout << dp[B][M] << endl;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(13);
solve();
return 0;
}
| 0 |
#include "bits/stdc++.h"
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
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(){
cin.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
map<int, int> odd, even;
int odd_first = 0, odd_second = 0;
int odd_first_num = -1, odd_second_num = -1;
int even_first = 0, even_second = 0;
int even_first_num = -1, even_second_num = -1;
rep(i,n){
int v;
cin >> v;
if(i%2){
++even[v];
if(even[v] > even_first){
if(even_first_num != v){
even_second = even_first;
even_second_num = even_first_num;
}
even_first = even[v];
even_first_num = v;
}
else if(even[v] > even_second){
even_second = even[v];
even_second_num = v;
}
}
else{
++odd[v];
if(odd[v] > odd_first){
if(odd_first_num != v){
odd_second = odd_first;
odd_second_num = odd_first_num;
}
odd_first = odd[v];
odd_first_num = v;
}
else if(odd[v] > odd_second){
odd_second = odd[v];
odd_second_num = v;
}
}
}
/*
cout << odd_first_num << " " << odd_first << endl;
cout << even_first_num << " " << even_first << endl;
cout << odd_second << endl;
cout << even_second << endl;
*/
int ans = 1e9;
if(odd_first_num != even_first_num){
chmin(ans, n - odd_first - even_first);
}
else{
chmin(ans, n - odd_first - even_second);
chmin(ans, n - odd_second - even_first);
}
cout << ans << endl;
return 0;
} | #include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_map>
#include <vector>
using namespace std;
using ll = int64_t;
#define rep(i, j, n) for (int i = j; i < (int)n; ++i)
#define rrep(i, j, n) for (int i = (int)n - 1; j <= i; --i)
int main() {
int n;
cin >> n;
vector<ll> a(n);
rep(i, 0, n) cin >> a[i];
unordered_map<int, int> odd, even;
rep(i, 0, n) {
if (i & 1)
odd[a[i]]++;
else
even[a[i]]++;
}
vector<pair<int, int>> o, e;
for (auto p : odd) { o.emplace_back(p.second, p.first); }
for (auto p : even) { e.emplace_back(p.second, p.first); }
sort(o.rbegin(), o.rend());
sort(e.rbegin(), e.rend());
o.emplace_back(0, n / 2);
e.emplace_back(0, n / 2);
if (o[0].second == e[0].second) {
cout << n - o[0].first - max(o[1].first, e[1].first) << endl;
} else {
cout << n - o[0].first - e[0].first << endl;
}
return 0;
} | 1 |
#include<bits/stdc++.h>
using namespace std;
const int MAX_N = 1<<17;
int n,dat[2*MAX_N-1];
//?????????
void init(int n_){
//????´???°n???2???????????????
n=1;
while(n<n_)n*=2;
for(int i=0;i<2*n-1;i++)dat[i]=INT_MAX;
}
void update(int a,int b,int k,int l,int r,int x){
if(r<=a||b<=l)return;
if(a<=l&&r<=b)dat[k]=x;
else{
if(dat[k]!=INT_MAX){
dat[k*2+1]=dat[k];
dat[k*2+2]=dat[k];
dat[k]=INT_MAX;
}
update(a,b,k*2+1,l,(l+r)/2,x);
update(a,b,k*2+2,(l+r)/2,r,x);
}
}
int find(int a,int b,int k,int l,int r){
if(r<=a||b<=l)return INT_MAX;
if(a<=l&&r<=b)return dat[k];
else{
if(dat[k]!=INT_MAX){
dat[k*2+1]=dat[k];
dat[k*2+2]=dat[k];
dat[k]=INT_MAX;
}
return min(find(a,b,k*2+1,l,(l+r)/2),find(a,b,k*2+2,(l+r)/2,r));
}
}
int main(){
int q;
cin>>n>>q;
init(n);
int u,s,t,x;
for(int i=0;i<q;i++){
cin>>u;
if(!u){
cin>>s>>t>>x;
update(s,t+1,0,0,n,x);
}else{
cin>>s;
cout<<find(s,s+1,0,0,n)<<endl;
}
}
return 0;
} | //g++ -std=c++14 test.cpp -o test.out
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <cassert>
#include <algorithm>
#include <functional>
#include <iostream>
#include <iomanip>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <unordered_map>
#include <utility>
#include <limits.h>
#include <bitset>
#include <set>
using namespace std;
#define LL long long int
const LL INF = (1LL<<60);
const int INF_INT = 2147483647-1e6-1;
const LL mod = 1000000007ll;
const int mod_int = 1000000007;
LL N;
//i->jへ行くコストがcの時、集合adj[i]にはpair(i->jのコスト、j)が含まれる
vector<vector<pair<LL,LL>>> adj;
LL Q,K;
LL x[100000],y[100000];
LL ans[100000];
void solve(){
priority_queue<pair<LL,LL>,vector<pair<LL,LL>>,greater<pair<LL,LL>>> pq;
pq.push(pair<LL,LL>(0,K));
vector<LL> dist(N+1,INF);
dist[K] = 0;
while(!pq.empty()){
LL now = pq.top().second;
LL cost = pq.top().first;
pq.pop();
if(dist[now]<cost) continue;
for(pair<LL,LL> next_info:adj[now]){
LL diff_cost = next_info.first;
LL next = next_info.second;
if(dist[next]>=dist[now]+diff_cost){
dist[next] = dist[now]+diff_cost;
pq.push(pair<LL,LL>(dist[next],next));
}
}
}
for(int i=0;i<Q;i++){
ans[i] = dist[x[i]] + dist[y[i]];
}
}
int main(){
cin >> N;
adj = vector<vector<pair<LL,LL>>>(N+1);
for(int i=0;i<N-1;i++){
LL a,b,c;cin >> a >> b >> c;
adj[a].push_back(pair<LL,LL>(c,b));
adj[b].push_back(pair<LL,LL>(c,a));
}
cin >> Q >> K;
for(int i=0;i<Q;i++) cin >> x[i] >> y[i];
solve();
for(int i=0;i<Q;i++)cout << ans[i] << endl;
return 0;
} | 0 |
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
const int INF = 10000;
template<typename ISTREAM>
void readWeightedGraphMatrix(ISTREAM& cin, vector< vector<int> >& weightedGraphMatrix) {
size_t n;
cin >> n;
int weight;
weightedGraphMatrix = vector< vector<int> >(n, vector<int>(n));
for (size_t i = 0; i != n; ++i) {
for (size_t j = 0; j != n; ++j) {
cin >> weight;
weightedGraphMatrix.at(i).at(j) = weight != -1 ? weight : INF;
}
}
}
int MST(const vector< vector<int> >& weightedGraphMatrix) {
enum Color {
WHITE, GRAY, BLACK//白は未訪問でTと隣接していない、。グレーはTと隣接状態だが未訪問、黒は訪問済み。木は閉路を持たないグラフなので、全域木の計算では
//各点は1度だけ訪問されるはず。
};
const size_t n = weightedGraphMatrix.size();
vector<Color> color(n,Color::WHITE);
vector<int> d(n, INF);
vector<int> p(n, INF);
color.front() = Color::GRAY;
d.front() = 0;
int currentNode = 0;
while (true) {
int min = INF;
int u;
//TからV-Tへ接続する辺の重みの中で最小のものを求める。
//同時に、その辺に接続する辺のT内のノードを求める。
for (size_t i = 0; i != n; ++i) {
if (color.at(i) == Color::BLACK || color.at(i) == Color::WHITE) continue;//訪問済みはスキップ
if (d.at(i) >= min)continue;
min = d.at(i);//TからV-Tの中のノードiへ向かう辺の重みの最小値を更新
u = i;
}
if (min == INF) {
//ノード0からアクセス可能なすべての点を訪問済みになったらMST構築完了
break;
}
color.at(u) = Color::BLACK;//訪問
for (int v = 0; v != n; ++v) {
if (color.at(v) == Color::BLACK) continue;//訪問済みはスキップ
if (weightedGraphMatrix.at(u).at(v) == INF ) continue;
if (weightedGraphMatrix.at(u).at(v) >= d.at(v)) continue;
d.at(v) = weightedGraphMatrix.at(u).at(v);
p.at(v) = u;
color.at(v) = Color::GRAY;
}
}
int sum = 0;
for (int i = 0; i != n; ++i) {
if (p.at(i) == INF)continue;
sum += weightedGraphMatrix.at(i).at(p.at(i));
}
return sum;
}
template<typename T>
void execute(T& cin) {
vector< vector<int> > graphList;
readWeightedGraphMatrix(cin, graphList);
cout << MST(graphList) << endl;
}
int main()
{
execute(cin);
return 0;
}
|
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;
int P[1010];
int cost[5000];
int src[5000];
int dst[5000];
int A[100 + 10][100 + 10];
int M = 0;
vector< pair<int,int> > edges;
void init(int N)
{
for(int i = 1; i <= N; ++i)
{
P[i] = i;
}
}
int root(int a)
{
if(P[a] == a)
{
return a;
}
return (P[a] = root(P[a]));
}
bool is_same_set(int a, int b)
{
return root(a) == root(b);
}
void unite(int a, int b)
{
P[root(a)] = root(b);
}
int main()
{
int n = 0;
cin >> n;
for(int r = 0; r < n; ++r)
{
for(int c = 0; c < n; ++c)
{
cin >> A[r][c];
}
}
for (int r = 0; r < n; ++r)
{
for(int c = r + 1; c < n; ++c)
{
if(A[r][c] > -1)
{
cost[M] = A[r][c];
src[M] = r;
dst[M] = c;
edges.push_back(make_pair(cost[M],M));
M += 1;
}
}
}
sort(edges.begin(),edges.end());
int sum = 0;
init(n);
for(int i = 0; i < M; ++i)
{
if(is_same_set(src[edges[i].second], dst[edges[i].second]) == 1) continue;
unite(dst[edges[i].second],src[edges[i].second]);
sum += edges[i].first;
}
cout << sum << endl;
} | 1 |
#include<bits/stdc++.h>
using namespace std;
/******* All Required define Pre-Processors and Constants *******/
#define int long long
#define ull unsigned long long
#define ld long double
#define mem(a, b) memset(a, (b), sizeof(a))
#define rep(i, j, k) for (int i = j ; i < k ; ++i)
#define rrep(i, j, k) for (int i = j; i > k; --i)
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define foreach(i, a) for(auto i: a)
#define forEach(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define in(A, B, C) assert( B <= A && A <= C)
#define debug(a) cout << #a << ": " << a << endl
#define Flag(n) cout << "here " << n << endl
#define w(x) int x;cin>>x;while(t--)
#define mp make_pair
#define pb push_back
#define endl '\n';
#define io ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define TRACE
#ifdef TRACE
#define see(...) __f(#__VA_ARGS__,__VA_ARGS__);
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cerr<<name<<" : "<<arg1<<endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma=strchr(names+1,',');cerr.write(names,comma-names)<<" : "<<arg1<<" | ";__f(comma+1, args...);
}
#else
#endif
const int POSITIVE_INFINITY = 9223372036854775807;
const int NEGATIVE_INFINITY = -9223372036854775807;
const int MOD = 1000000007;
const ld PI = acos(-1.0);
const int INF = 1e18;
const int MX = 1000001;
int32_t main() {
io;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
int n;
cin>>n;
int arr[n],pref[n];
rep(i,0,n)cin>>arr[i];
pref[0]=arr[n-1];
for(int i = 1;i<n;i++){
pref[i]=pref[i-1]+arr[n-1-i];
// see(pref[i]);
}
int sum = 0;
for(int i = 0;i<n-1;i++){
sum=((sum%MOD)+((arr[i]%MOD)*(pref[n-2-i]%MOD))%MOD)%MOD;
}
cout<<sum<<endl;
return 0;
}
| #include<iostream>
using namespace std;
long long int MOD = 1000000007;
long long modpow(long long a, long long n, long long mod);
long long modinv(long long a, long long mod);
int main() {
//D
long long int h, w, a, b;
cin >> h >> w >> a >> b;
long long int sum = 1;
long long int i, j;
for (i = 1; i <= h - a - 1 + b; i++) {
sum = (sum * i) % MOD;
}
for (i = 1; i <= w - b - 1 + a - 1; i++) {
sum = (sum * i) % MOD;
}
for (i = 1; i <= h - a - 1; i++) {
sum = (sum * modinv(i, MOD)) % MOD;
}
for (i = 1; i <= b; i++) {
sum = (sum * modinv(i, MOD)) % MOD;
}
for (i = 1; i <=w-b-1; i++) {
sum = (sum * modinv(i, MOD)) % MOD;
}
for (i = 1; i <= a - 1; i++) {
sum = (sum * modinv(i, MOD)) % MOD;
}
//cout << sum << endl;
long long int sumsum = 0;
for (i = b; i <= w - 1; i++) {
sumsum = (sum + sumsum) % MOD;
//cout << "sumsum" << sumsum;
sum = (sum * (w - i - 1)) % MOD;
sum = (sum * (h - a - 1 + i + 1)) % MOD;
sum = (sum * modinv(w - i - 1 + a - 1, MOD)) % MOD;
sum = (sum * modinv(i + 1, MOD)) % MOD;
//cout << sum << endl;
}
cout << sumsum << endl;
return 0;
//C
/*int n,k;
int d[10];
cin >> n >> k;
int i;
for (i = 0; i < k; i++) {
cin >> d[i];
}
int x,j;
int flag;
for (i = n; i < 99999; i++) {
x = i;
flag = 0;
while (x > 0) {
for (j = 0; j < k; j++) {
if ((x % 10) == d[j]) {
flag = 1;
break;
}
}
if (flag == 1)break;
x = (x - (x % 10)) / 10;
}
if (flag == 0) {
cout << i << endl;
return 0;
}
}*/
}
// a^n mod を計算する
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;
}
// a^{-1} mod を計算する
long long modinv(long long a, long long mod) {
return modpow(a, mod - 2, mod);
} | 0 |
#include <cstdint>
#include <cstdio>
#include <atcoder/fenwicktree>
int main() {
int n, q;
scanf("%d %d", &n, &q);
atcoder::fenwick_tree<intmax_t> ft(n);
for (int i = 0; i < n; ++i) {
int a;
scanf("%d", &a);
ft.add(i, a);
}
for (int i = 0; i < q; ++i) {
int t;
scanf("%d", &t);
if (t == 0) {
int p, x;
scanf("%d %d", &p, &x);
ft.add(p, x);
} else if (t == 1) {
int l, r;
scanf("%d %d", &l, &r);
printf("%jd\n", ft.sum(l, r));
}
}
}
| #include<bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define F first
#define S second
#define PB push_back
#define FOR(a,b) for(int i=a;i<=b;i++)
#define RFOR(a,b) for(int i=a;i>=b;i--)
#define FORE(a) for(auto it: a)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define clr(x,f) memset(x,f,sizeof(x))
#define pii pair<int,int>
#define vi vector<int>
#define vpii vector<pii>
#define vvi vector<vi>
#define si set<int>
#define spii set<pii>
#define usi unordered_set<int>
#define uspii unordered_set<pii>
#define mii map<int,int>
#define umii unordered_map<int,int>
#define pqmx priority_queue<int>
#define pqmn priority_queue<int,vi,greater<int>>
#define setbits(x) __builtin_popcountll(x)
#define tzbits(x) __builtin_ctzll(x)
#define lzbits(x) __builtin_clzll(x)
#define pbits(x) __builtin_parityll(x)
#define gcd __gcd
#define lcm(x, y) ((x)*(y))/gcd(x,y)
#define endl '\n'
#define sz(s) (int)s.size()
#define sp(x,y) fixed<<setprecision(y)<<x
const int mod = 1000000007;
const int inf = 1000000000000000000;
const ld PI = 3.1415926535897932384626;
const ld eps = 1e-12;
void __print(int32_t x) {cout<<x;}
void __print(int x) {cout<<x;}
void __print(double x) {cout<<x;}
void __print(ld x) {cout<<x;}
void __print(float x) {cout<<x;}
void __print(bool x) {cout<<(x?"true":"false");}
void __print(char x) {cout <<'\''<<x<<'\'';}
void __print(const char *x) {cout <<'\"' <<x<<'\"';}
void __print(const string &x) {cout<<'\"'<<x<<'\"';}
template<typename T,typename V> void __print(const pair<T,V> &x) {cout<<'{';__print(x.first);cout<<',';__print(x.second);cout<<'}';}
template<typename T> void __print(const T &x) {int f=0;cout<<'{';for(auto &i:x)cout<<(f++?",":""),__print(i);cout<<"}";}void _print(){cout<<"]\n";}
template <typename T,typename... V> void _print(T t, V... v) {__print(t);if(sizeof...(v))cout<<", ";_print(v...);}
#define dbg(x...) cout<<"["<<#x<<"]=[";_print(x)
int powerm(int base,int exp) {int res=1;base%=mod;while(exp>0){if(exp&1)res=(res*base)%mod;base=(base*base)%mod;exp=exp>>1;}return res;}
int power(int base,int exp) {int res=1;while(exp>0){if(exp&1)res=res*base;base=base*base;exp=exp>>1;}return res;}
float powerNeg(float base,int exp) {float temp;if(exp==0)return 1;temp=powerNeg(base,exp/2);if(exp%2==0)return temp*temp;else{if(exp>0)return base*temp*temp;else return (temp*temp)/base;}}
int modinv(int exp) {return powerm(exp,mod-2);}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
const int N=1e6+5,M=N;
vi a;
void update(int i,int val){
while(i<sz(a)){
a[i]+=val;
i+=i&(-i);
}
}
int query(int i){
int res=0;
while(i>=1){
res+=a[i];
i-=i&(-i);
}
return res;
}
void solve(){
int i,j,k;
int n,q; cin>>n>>q;
a=vi(n+1);
FOR(1,n){
int val; cin>>val;
update(i,val);
}
FOR(0,q-1){
int t; cin>>t;
if(t){
int j,k; cin>>j>>k;
int ans=query(k)-query(j);
cout<<ans<<endl;
}else{
int j,val; cin>>j>>val;
update(j+1,val);
}
}
//cout<<<<endl;
}
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int T=1;
// cin>>T;
FOR(1,T){
// cout<<"Case #"<<i<<": ";
solve();
}
// cout<<endl<<"Times Elapsed:"<<1.0*clock()/CLOCKS_PER_SEC<<"sec";
return 0;
} | 1 |
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <regex>
#define rep(i,n) for(int i = 0; i < n; i++)
#define reps(i,j,n) for(int i = j; i < n; i++)
#define rrep(i,j,n) for(int i = j-1; i >= n; i--)
#define prec(n) fixed << setprecision(n)
#define print_array(v) rep(__k, v.size()) { cout << v[__k]; if(__k != v.size()-1) cout << " "; else cout << endl; }
#define YesorNo(a) printf(a?"Yes\n":"No\n")
#define fi first
#define se second
#define endl "\n"
using namespace std;
//constexpr int inf = 2147483647;
//constexpr int64_t inf64 = 9223372036854775807;
//constexpr int mod = 1e9+7;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int h1, m1, h2, m2, k;
cin >> h1 >> m1 >> h2 >> m2 >> k;
m1 += 60 * h1;
m2 += 60 * h2;
cout << m2 - m1 - k << endl;
return 0;
} | #include <bits/stdc++.h>
#define REP(i,n) for (int i = 0; i <(n); ++i)
#define REP2(i,x,n) for (int i = x; i <(n); ++i)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using P = pair<int,int>;
static const double PI = acos(-1);
int main(){
int n, h, w;
cin >> n >> h >> w;
int x = 1 + n - h;
int y = 1 + n - w;
cout << x * y << endl;
return 0;
}
| 0 |
#include <iostream>
#include <vector>
using namespace std;
int main(){
int n;
cin >> n;
vector<int> v;
int x;
for(int i = 0; i < n; i++){
cin >> x;
v.insert(v.begin(),x);
}
x = 1;
for(auto y:v){
cout << y;
if( x != n ){
cout << " ";
}
x++;
}
cout << endl;
return 0;
} | #include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <complex>
#include <functional>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cctype>
using namespace std;
#define dump(n) cout<<"# "<<#n<<"="<<(n)<<endl
#define debug(n) cout<<__FILE__<<","<<__LINE__<<": #"<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,n) repi(i,0,n)
#define iter(c) __typeof((c).begin())
#define tr(c,i) for(iter(c) i=(c).begin();i!=(c).end();i++)
#define allof(c) (c).begin(),(c).end()
#define mp make_pair
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int,int> pii;
int main()
{
for(int a[5];cin>>a[0];){
repi(i,1,5)
scanf(",%d",a+i);
sort(a,a+5);
int hist[13]={0};
rep(i,5)
hist[a[i]-1]++;
sort(hist,hist+13,greater<int>());
string res;
if(hist[0]==4)
res="four card";
else if(hist[0]==3 && hist[1]==2)
res="full house";
else if(a[0]+1==a[1] && a[1]+1==a[2] && a[2]+1==a[3]&& a[3]+1==a[4] ||
a[0]==1 && a[1]==10 && a[2]==11 && a[3]==12 && a[4]==13)
res="straight";
else if(hist[0]==3)
res="three card";
else if(hist[0]==2 && hist[1]==2)
res="two pair";
else if(hist[0]==2)
res="one pair";
else
res="null";
cout<<res<<endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
#define N 100005
int n, m;
vector<int> to[N];
int dfn[N], low[N], bel[N], tim, cnt;
stack<int> stk;
bitset< N > vis;
void tarjan(int u)
{
dfn[u] = low[u] = ++tim; stk.push(u); vis.set(u);
for (auto v : to[u])
{
if (!dfn[v])
{
tarjan(v);
low[u] = min(low[u], low[v]);
}
else if (vis[v])
low[u] = min(low[u], dfn[v]);
}
if (low[u] == dfn[u])
{
int t; ++cnt;
do
{
t = stk.top(); stk.pop();
bel[t] = cnt; vis.reset(t);
}
while (t != u);
}
}
signed main(void)
{
cin >> n >> m;
for (int u, v; m--; )
{
cin >> u >> v;
to[u].push_back(v);
}
for (int i = 0; i < n; ++i)
if (!dfn[i])tarjan(i);
int q; cin >> q;
while (q--)
{
int x, y;
cin >> x >> y;
cout << (bel[x] == bel[y]) << endl;
}
} | #include <stdio.h>
#include <vector>
#include <tuple>
#include <map>
#define FOR(i, a, b) for(int (i) = (a); (i) < (b); ++(i))
#define REP(i, n) FOR(i, 0, n)
using lli = long long int;
using pii = std::pair<int, int>;
// UnionFind 木 ; 頂点 = 0, 1, 2, ..., n-1
class UnionFindTree{
private:
using Vint = std::vector<int>;
const int n; // 集合の大きさ
Vint Parent; // 親のインデックスを指し示す
Vint SetSize; // 各集合の大きさ(要素数)を保持する関数
public:
// コンストラクタ
UnionFindTree(int nn): n(nn){
SetSize.resize(n, 1);
Parent.resize(n);
// 初めは, 全ての頂点は根である
for(int i = 0; i < n; ++i) Parent[i] = i;
}
// デコンストラクタ
~UnionFindTree(void){}
// 頂点 x の属する集合の根を返す関数
int rootOf(int x){
// x が 根であるなら終了
if(Parent[x] == x) return x;
// そうでないなら, 親を属する集合の根に付け替える
return Parent[x] = rootOf(Parent[x]);
}
// 頂点 x, y を併合する
void unite(int x, int y){
int root_x = rootOf(x);
int root_y = rootOf(y);
// 異なる集合に属する場合
if(root_x == root_y) return;
if(SetSize[root_x] < SetSize[root_y]) std::swap(root_x, root_y);
Parent[root_y] = root_x;
SetSize[root_x] += SetSize[root_y];
}
// 頂点 x, y が 同じ集合に属しているかを判定する
bool sameSet(int x, int y){
return rootOf(x) == rootOf(y);
}
// 頂点 x の属する集合の要素数
int sizeOf(int x){
return SetSize[rootOf(x)];
}
};
int main(void){
int n, k, l; scanf("%d%d%d", &n, &k, &l);
UnionFindTree A(n), B(n);
REP(i, k){
int a, b; scanf("%d%d", &a, &b); a--; b--;
A.unite(a, b);
}
REP(i, l){
int a, b; scanf("%d%d", &a, &b); a--; b--;
B.unite(a, b);
}
std::map<pii, int> C;
REP(i, n) C[pii(A.rootOf(i), B.rootOf(i))]++;
REP(i, n){
printf("%d", C[pii(A.rootOf(i), B.rootOf(i))]);
putchar(i == n - 1 ? '\n' : ' ');
}
return 0;
} | 0 |
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ll long long
#define FASTIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define inf 1e9
#define ff first
#define ss second
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define mod 1000000007
#define pb push_back
#define all(s) s.begin(),s.end()
#define pie 3.14159265358979323846
#define fr(i,a,b) for(ll i=a;i<b;i++)
#define frr(i,a,b) for(ll i=b-1;i>=a;i--)
#define gcd __gcd
#define con continue
#define pii pair<ll,ll>
const ll N=5e5+6;
ll seg[4*N];
void build(ll s,ll e,ll idx,ll a[]){
if(s==e){
//cout<<s<<" ";
seg[idx]=a[s];
return;
}
ll mid=(s+e)/2;
build(s,mid,2*idx+1,a);
build(mid+1,e,2*idx+2,a);
seg[idx]=seg[2*idx+1]+seg[2*idx+2];
return ;
}
void update(ll s,ll e,ll idx,ll pos,ll val){
if(s==e){
seg[idx]=val;
return;
}
ll mid=(s+e)/2;
if(pos<=mid)
update(s,mid,2*idx+1,pos,val);
else
update(mid+1,e,2*idx+2,pos,val);
seg[idx]=seg[2*idx+1]+seg[2*idx+2];
return ;
}
ll query(ll s,ll e,ll idx,ll l,ll r){
if(s>=l&&e<=r)return seg[idx];
if(s>r||e<l)return 0;
ll mid=(s+e)/2;
return query(s,mid,2*idx+1,l,r)+query(mid+1,e,2*idx+2,l,r);
}
signed main(){
FASTIO;
ll tt=1;
//cin>>tt;
while(tt--){
ll n,q,x,l,r;cin>>n>>q;
ll a[n];
for(ll i=0;i<n;i++){
cin>>a[i];
}
build(0,n-1,0,a);
while(q--){
cin>>x;
if(x){
cin>>l>>r;
cout<<query(0,n-1,0,l,r-1)<<"\n";
}
else{
cin>>l>>x;
a[l]+=x;
update(0,n-1,0,l,a[l]);
}
}
}
}
| #include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
typedef long long ll;
#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)
#define ALL(v) (v).begin(),(v).end()
#define CLR(t,v) memset(t,(v),sizeof(t))
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";}
template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<" ";cout<<endl;}
template<class T>void chmin(T&a,const T&b){if(a>b)a=b;}
template<class T>void chmax(T&a,const T&b){if(a<b)a=b;}
ll nextLong() { ll x; scanf("%lld", &x); return x;}
int main2() {
int N = nextLong();
int Q = nextLong();
fenwick_tree<ll> fw(N+10);
REP(i, N) {
int x = nextLong();
fw.add(i, x);
}
REP(qqq, Q) {
int t = nextLong();
if (t == 0) {
int p = nextLong();
int x = nextLong();
fw.add(p, x);
}
else
{
int l = nextLong();
int r = nextLong();
ll ans = fw.sum(l, r);
cout << ans << '\n';
}
}
return 0;
}
int main() {
#ifdef LOCAL
for (;!cin.eof();cin>>ws)
#endif
main2();
return 0;
} | 1 |
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int cards[5], i, j, t, r, fl, fl2;
char c, str[8][11] = {"null", "one pair", "two pair", "three card", "straight", "full house", "four card"};
while (cin >> cards[0] >> c >> cards[1] >> c >> cards[2] >> c >> cards[3] >> c >> cards[4])
{
r = 1;
sort(cards, cards+5);
int n[13] = {0};
for (i = 0; i < 5; i++)
n[cards[i]-1]++;
for (i = fl = t = 0; i < 13; i++)
if (n[i] == 2) t++;
if (t == 1) r = 2;
if (t == 2) r = 3;
for (i = fl = 0; i < 13 && !fl; i++)
if (n[i] == 3) r = 4, fl = 1;
for (i = fl = 0; i < 9 && !fl; i++)
{
for (j = 0, fl2 = 1; j < 5 && fl2; j++)
if (n[i+j] != 1) fl2 = 0;
if (fl2) r = 5, fl2 = 1;
}
if (!fl)
{
for (i = 9, fl = 0; i < 13 && !fl; i++)
if (n[i] != 1) fl = 1;
if (!fl && n[0]) r = 5;
}
for (i = fl = fl2 = 0; i < 13; i++)
{
if (n[i] == 2) fl = 1;
if (n[i] == 3) fl2 = 1;
}
if (fl && fl2) r = 6;
for (i = fl = 0; i < 13 && !fl; i++)
if (n[i] == 4)
r = 7, fl = 1;
cout << str[r-1] << endl;
}
} | #include <iostream>
#include <vector>
#include <map>
#include <limits.h>
#include <queue>
#define INF INT_MAX
using namespace std;
int main()
{
string s;
for(;cin>>s;)
{
int ans=0,tmp=0;
int n[]={1000,500,100,50,10,5,1};
string nn="MDCLXVI";
for(int i=0;i<s.size();i++)
{
for(int j=0;j<nn.size();j++)
{
if(s[i]==nn[j])
{
ans+=n[j];
if(tmp<n[j])
ans-=tmp*2;
tmp=n[j];
}
}
}
cout<<ans<<endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define in ({int x=0;int c=getchar(),n=0;for(;!isdigit(c);c=getchar()) n=(c=='-');for(;isdigit(c);c=getchar()) x=x*10+c-'0';n?-x:x;})
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int l,int r){return l+rng()%(r-l+1);}
#define fasty ios_base::sync_with_stdio(0),cin.tie(0);
#define forinc(a,b,c) for(int a=b,_c=c;a<=_c;++a)
#define fordec(a,b,c) for(int a=b,_c=c;a>=_c;--a)
#define forv(a,b) for(auto&a:b)
#define fi first
#define se second
#define pb push_back
#define ii pair<int,int>
#define mt make_tuple
#define all(a) a.begin(),a.end()
#define reset(f, x) memset(f, x, sizeof(f))
#define gg exit(0);
const int M=998244353;
int k,n;
int f[333][333][333];
string s;
vector<int> val;
void add(int &a,int b){
a=(a+b)%M;
}
main(){
#define task "TASK"
if(fopen(task".inp","r")){
freopen(task".inp","r",stdin);
//freopen(task".out","w",stdout);
}
cin>>s>>k; n=s.size(), s=" "+s, k=min(k,(int)s.size());
int i=1;
while(i<=n){
int j=i;
while(s[j]=='1') j++;
val.pb(j-i);
i=j+1;
}
if(s[n]=='0') val.pb(0);
n=val.size();
f[n][0][0]=1;
fordec(i,n,1) forinc(j,0,k) forinc(t,0,j) if(f[i][j][t]){
for(int p=1;p+j<=k && p<=val[i-1];p++){
//if(i==n) cerr<<p<<" ";
add(f[i-1][j+p][t+p],f[i][j][t]);
}
for(int p=1;p<=t;++p){
add(f[i-1][j][t-p],f[i][j][t]);
}
add(f[i-1][j][t],f[i][j][t]);
}
int tot=0;
forinc(j,0,k)
add(tot,f[0][j][0]);
cout<<tot;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll MOD = 998244353;
constexpr int NMAX = 302;
ll bilang[NMAX][NMAX][NMAX]; // (# of chars left in target, opcount, # of ones I ate)
char simula[NMAX];
int lenToZero[NMAX];
ll bilangin(int i, int j, int k) {
if (i < 0 || j < 0 || k < 0 ||
i >= NMAX || j >= NMAX || k >= NMAX) return 0;
else return bilang[i][j][k];
}
int main() {
scanf("%s", simula);
int k; scanf("%d", &k);
int n = strlen(simula);
for (int i = 0; i < n; ++i) {
if (simula[i] == '0') lenToZero[i] = 0;
else lenToZero[i] = (i == 0 ? 1000000 : 1 + lenToZero[i-1]);
}
bilang[0][0][0] = 1;
for (int targetLen = 1; targetLen <= n; ++targetLen) {
for (int opcount = 0; opcount <= n; ++opcount) {
for (int kinain = 0; kinain <= targetLen; ++kinain) {
int sourceLen = targetLen - kinain;
if (sourceLen == 0) {
bilang[targetLen][opcount][kinain] = opcount == 0 ? 1 : 0;
continue;
}
if (simula[sourceLen - 1] == '0') {
bilang[targetLen][opcount][kinain] = (bilangin(targetLen - 1, opcount, kinain)
+ bilangin(targetLen - 1, opcount, kinain - 1)) % MOD;
} else {
bilang[targetLen][opcount][kinain] =
(bilangin(targetLen - 1, opcount - lenToZero[sourceLen - 1], kinain + lenToZero[sourceLen - 1])
+ bilangin(targetLen - 1, opcount, kinain)) % MOD;
}
// printf("t = %d s = %d o = %d k = %d is %lld\n", targetLen, sourceLen, opcount, kinain,
// bilangin(targetLen, opcount, kinain));
}
}
}
ll ans = 0;
for (int finalOps = 0; finalOps <= min(k, n); ++finalOps) ans = (ans + bilangin(n, finalOps, 0)) % MOD;
printf("%lld\n", ans);
return 0;
}
| 1 |
#include <iostream>
#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))
using namespace std;
int main(){
int n;
string s;
string alp = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
string ans;
while(1){
cin >> n;
if(n == 0) break;
int k[n];
for(int i = 0; i < n; i++){
cin >> k[i];
}
cin >> s;
for(int i = 0; i < s.length(); i++){
int kn = i%ARRAY_LENGTH(k);
int alpn = alp.find(s[i])-k[kn];
if(alpn < 0) alpn += 52;
else if(alpn > 52) alpn -=52;
cout << alp[alpn];
}
cout << endl;
}
} | #include<iostream>
using namespace std;
int dfs(int n, int dan){
if(dan == n){
return 1;
}
else if(dan > n){
return 0;
}
else{
int count = 0;
count = count + dfs(n, dan + 1);
count = count + dfs(n, dan + 2);
count = count + dfs(n, dan + 3);
return count;
}
}
int main(){
int n;
while(1){
cin>>n;
if(n == 0) break;
cout<<dfs(n,0) / 10 / 365 + 1<<endl;
}
} | 0 |
#include <bits/stdc++.h>
using namespace std;
#define Int register int
#define mod 998244353
#define MAXN 1000005
template <typename T> inline void read (T &t){t = 0;char c = getchar();int f = 1;while (c < '0' || c > '9'){if (c == '-') f = -f;c = getchar();}while (c >= '0' && c <= '9'){t = (t << 3) + (t << 1) + c - '0';c = getchar();} t *= f;}
template <typename T,typename ... Args> inline void read (T &t,Args&... args){read (t);read (args...);}
template <typename T> inline void write (T x){if (x < 0){x = -x;putchar ('-');}if (x > 9) write (x / 10);putchar (x % 10 + '0');}
int n,m,fac[MAXN],ifac[MAXN];
int mul (int a,int b){return 1ll * a * b % mod;}
int dec (int a,int b){return a >= b ? a - b : a + mod - b;}
int add (int a,int b){return a + b >= mod ? a + b - mod : a + b;}
int qkpow (int a,int b){int res = 1;for (;b;b >>= 1,a = mul (a,a)) if (b & 1) res = mul (res,a);return res;}
int binom (int a,int b){return mul (fac[a],mul (ifac[b],ifac[a - b]));}
int inv (int x){return qkpow (x,mod - 2);}
signed main(){
read (n,m);
fac[0] = 1;for (Int i = 1;i <= n + m;++ i) fac[i] = mul (fac[i - 1],i);
ifac[n + m] = inv (fac[n + m]);for (Int i = n + m;i;-- i) ifac[i - 1] = mul (ifac[i],i);
int res = 0;for (Int i = 1;i <= n && i <= m;++ i) res = add (res,mul (binom (2 * i,i),binom (n + m - 2 * i,n - i)));
res = mul (res,mul (ifac[2],inv (binom (n + m,n))));
write (add (res,max (n,m))),putchar ('\n');
return 0;
} | #include<cstdio>
#include<algorithm>
const int N=1000007;
char s[N],t[N];int n,ans,sum[N];
int main()
{
scanf("%d%s%s",&n,s+1,t+1);
if(s[1]^t[1]) return !printf("-1");
for(int i=n,f=0,k=0,p=n;i;--i)
{
f+=sum[i+k];
if(p>i||t[i]^s[p])
{
for(;p>0&&(p>i||t[i]^s[p]);--p);
if(p==i) continue;
if(p<=0) return !printf("-1");
++k,++f,sum[i+k-1]=0,--sum[p+k-1];
}
ans=std::max(ans,f);
}
printf("%d",ans);
}
| 0 |
#include <stdio.h>
int main(){
int N;
scanf("%d",&N);
long long int sum =0;
for(int i = 1 ;i<=N;i++){
if(i%3==0 && i%5==0){
sum +=0;
}
else if(i%3==0){
sum += 0;
}
else if(i%5==0){
sum+=0;
}
else {
sum += i;
}
}
printf("%lld",sum);
return 0;
} | //https://atcoder.jp/contests/abc162/tasks/abc162_b
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
ll n;
cin>>n;
ll sum1=0;
sum1=(n*(n+1))/2;
ll n3;
n3=(n)/3;
ll n5;
n5=(n)/5;
ll n15;
n15=(n)/15;
ll a3;
a3=3+(n3-1)*3;
ll a5;
a5=5+(n5-1)*5;
ll a15;
a15=15+(n15-1)*15;
ll sum3=0;
sum3=((n3)*(3+a3))/2;
ll sum5=0;
sum5=((n5)*(5+a5))/2;
ll sum15=0;
sum15=((n15)*(15+a15))/2;
ll sumfin=0;
sumfin=sum3+sum5-sum15;
ll ans=0;
ans=sum1-sumfin;
cout<<ans<<"\n";
return 0;
} | 1 |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
void solve() {
int arr[3];
for(int i = 0; i < 3; i++) {
cin >> arr[i];
}
sort(arr, arr + 3);
cout << arr[0] + arr[1];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
solve();
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int N, X, T;
cin >> N >> X >> T;
int count=N/X;
if (N%X!=0) {
count++;
}
cout << count*T << endl;
} | 0 |
#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wunused-result"
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#endif
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <sstream>
#include <numeric>
#include <map>
#include <set>
#include <queue>
#include <vector>
using namespace std;
typedef long long LL;
static const LL INF = 1LL << 60;
template<typename T, int SIZE, T DEF_VAL, const T& Compare(const T&, const T&)>
class SegmentTree {
vector<T> val;
public:
SegmentTree() : val(SIZE * 2, DEF_VAL) { }
void update(int i, const T& value) {
i += SIZE;
val[i] = value;
while (i > 1) i >>= 1, val[i] = Compare(val[i * 2], val[i * 2 + 1]);
}
T get(LL a, LL b, int l = 0, int r = SIZE, size_t i = 1) {
if (r <= a || b <= l) return DEF_VAL;
if (a <= l && r <= b) return val[i];
return Compare(get(a, b, l, (l + r) / 2, i * 2), get(a, b, (l + r) / 2, r, i * 2 + 1));
}
LL find_leftmost_index(LL a, LL b, const T& value, int l = 0, int r = SIZE, int i = 1) {
if (Compare(val[i], value) != val[i]) return -1;
if (r <= a || b <= l) return -1;
if (i >= SIZE) return i - SIZE;
LL leftmost_index = find_leftmost_index(a, b, value, l, (l + r) / 2, i * 2);
if (leftmost_index >= 0) return leftmost_index;
return find_leftmost_index(a, b, value, (l + r) / 2, r, i * 2 + 1);
}
LL find_rightmost_index(LL a, LL b, const T& value, int l = 0, int r = SIZE, int i = 1) {
if (Compare(val[i], value) != val[i]) return -1;
if (r <= a || b <= l) return -1;
if (i >= SIZE) return i - SIZE;
LL rightmost_index = find_rightmost_index(a, b, value, (l + r) / 2, r, i * 2 + 1);
if (rightmost_index >= 0) return rightmost_index;
return find_rightmost_index(a, b, value, l, (l + r) / 2, i * 2);
}
};
void solve(long long H, long long W, std::vector<long long> &A, std::vector<long long> &B) {
SegmentTree<long long, 1 << 18, INF, min> st;
for (int i = 0; i < W; ++i) {
st.update(i, 0);
}
for (LL y = 0; y < H; ++y) {
LL a = A[y], b = B[y];
while (true) {
LL min_value = st.get(a, b);
if (min_value == INF) break;
LL index = st.find_leftmost_index(a, b, min_value);
LL next_cost = min_value + b - index;
if (b < W && next_cost < st.get(b, b + 1)) {
st.update(b, next_cost);
}
st.update(index, INF);
}
LL cost = y + 1 + st.get(0, W);
cout << (cost < INF ? cost : -1) << endl;
}
}
int main() {
long long H;
scanf("%lld", &H);
long long W;
scanf("%lld", &W);
std::vector<long long> A(H);
std::vector<long long> B(H);
for (int i = 0; i < H; i++) {
scanf("%lld", &A[i]); A[i]--;
scanf("%lld", &B[i]);
}
solve(H, W, A, B);
return 0;
}
| #include<bits/stdc++.h>
#define int long long
using namespace std;
using P=pair<int,int>;
signed main(){
int H,W;
cin>>H>>W;
set<P> s;
multiset<int> ans;
for(int i=0;i<W;i++){
s.insert({i,i});
ans.insert(0);
}
int n=0;
for(int i=0;i<H;i++){
int a,b;
cin>>a>>b;
a--;
int mx=-1;
auto it=s.lower_bound({max(a,n),0});
for(;it!=s.end() && (*it).first<=b;it=s.erase(it)){
mx=max(mx,(*it).second);
ans.erase(ans.find((*it).first-(*it).second));
}
if(b!=W){
if(mx!=-1){
s.insert({b,mx});
ans.insert(b-mx);
}
}
home:;
printf("%d\n",ans.size()==0?-1:*ans.begin()+i+1);
if(n>=a)n=max(n,b);
}
} | 1 |
#include <iostream>
#include <stdio.h>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
using namespace std;
int main(){
int a, b;
while(1){
cin >> a >> b;
if(a == 0 && b == 0) break;
int date[13][32];
for(int i = 0;i < 13;i++){
for(int j = 0;j < 32;j++){
date[i][j] = 9;
}
}
int count = 0;
for(int i = 1;i < 13;i++){
if(i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12){
for(int j = 1;j < 32;j++){
date[i][j] = count;
count++;
if(count == 7) count = 0;
//cout << date[i][j];
}
//cout << endl;
}
else if(i == 4 || i == 6 || i == 9 || i == 11){
for(int j = 1;j < 31;j++){
date[i][j] = count;
count++;
if(count == 7) count = 0;
}
}
else if(i == 2){
for(int j = 1;j < 30;j++){
date[i][j] = count;
count++;
if(count == 7) count = 0;
}
}
}
if(date[a][b] == 0) cout << "Thursday" << endl;
if(date[a][b] == 1) cout << "Friday" << endl;
if(date[a][b] == 2) cout << "Saturday" << endl;
if(date[a][b] == 3) cout << "Sunday" << endl;
if(date[a][b] == 4) cout << "Monday" << endl;
if(date[a][b] == 5) cout << "Tuesday" << endl;
if(date[a][b] == 6) cout << "Wednesday" << endl;
}
return 0;
} | #include <iostream>
#include <map>
using namespace std;
int main()
{
int month = 0, day = 0, total_day = 0, week_num = 0;
map<int, int> month_num;
month_num[1] = 0;
month_num[2] = 31;
month_num[3] = 60;
month_num[4] = 91;
month_num[5] = 121;
month_num[6] = 152;
month_num[7] = 182;
month_num[8] = 213;
month_num[9] = 244;
month_num[10] = 274;
month_num[11] = 305;
month_num[12] = 335;
cin >> month >> day;
while ( month * day != 0 )
{
total_day = month_num[month] + day-1;
switch ( total_day % 7 )
{
case 0:
cout << "Thursday" << endl;
break;
case 1:
cout << "Friday" << endl;
break;
case 2:
cout << "Saturday" << endl;
break;
case 3:
cout << "Sunday" << endl;
break;
case 4:
cout << "Monday" << endl;
break;
case 5:
cout << "Tuesday" << endl;
break;
case 6:
cout << "Wednesday" << endl;
break;
default:
break;
}
cin >> month >> day;
}
return 0;
} | 1 |
#include<iostream>
using namespace std;
class Answer
{
public:
Answer(){day = 10; year = 365;}
int N; //??\???
int day;
int year;
unsigned long long count(int cnt); //??¨??¢?´¢(???)
void set(); //??\???
void OutPut(); //???????????????
bool empty(); //???????????¶?????????
bool ans(); //?§£???
};
unsigned long long Answer::count(int cnt)
{
if( cnt < 0)
return 0;
else if(cnt == 0)
return 1;
return count(cnt - 1) + count(cnt - 2) + count(cnt - 3);
}
void Answer::OutPut()
{
if( N == 0 )
return;
int ans_ = 1; //??????????????´???
ans_ += (count( N )) / (day * year); //??\???
cout << ans_ << endl;;
}
void Answer::set()
{
cin >> N;
}
bool Answer::empty()
{
if( N == 0 )return false;
else return true;
}
bool Answer::ans()
{
set();
OutPut();
return empty();
}
int main()
{
Answer ans;
while( ans.ans() );
return 0;
} | #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
const int MAX = 30;
int dp[MAX + 1];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
dp[0] = 1;
for (int i = 0; i <= MAX - 1; i++) {
for (int j = 1; j <= 3; j++) {
if (i + j <= MAX)
dp[i + j] += dp[i];
}
}
int n;
while (cin >> n , n) {
cout << ((dp[n] + 9) / 10 + 364) / 365 << endl;
}
} | 1 |
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
int main(void){
int n;
cin>>n;
map<int,int> m;
for(int i=0;i<n;i++){
int a;
cin>>a;
m[a]++;
}
ll n1=0,n2=0;
for(auto i:m){
if(i.second>=4){
n1=i.first,n2=i.first;
}else if(i.second>=2){
swap(n1,n2);
n1=i.first;
}
}
cout<<n1*n2<<endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
//typedef int64_t int;
#define all(x) x.begin(), x.end()
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define precise cout.precision(10); cout << fixed;
#define pb push_back
#define mk make_pair
#define endl "\n"
#define int int64_t
#define ll long long
const ll Inf = 1e9 + 7;
int n, k;
void solve() {
int n, co = 0;
cin >> n;
std::map<int, int> mp;
std::vector<int> num;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
mp[x]++;
//cout << mp[x] << " ";
if (mp[x] % 2 == 0 && mp[x] != 0) {
num.pb(x);
//cout << "xx" << endl;
}
} sort(all(num), greater<int>());
if (num.size() < 2) {
cout << 0 << endl;
} else {
cout << num[0]*num[1] << endl;
}
}
int32_t main()
{
fastio;
precise;
//int test; cin >> test; while (test--)
solve();
return 0;
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a,b,c,d;
string A,B;
cin >> a >> b;
c = max(a,b);
d = min(a,b);
int e = c;
c--;
c = max(c,d);
e += c;
cout <<e<<endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int A, B;
cin >> A >> B;
if (A == B) {
cout << 2 * A << endl;
}
else {
cout << 2 * max(A,B) -1 << endl;
}
return 0;
} | 1 |
#include <iostream>
#include <algorithm>
#include <vector>
#include<string>
#include <set>
#include <map>
#include <queue>
#include <limits.h>
#include <bitset>
#include <cmath>
using namespace std;
#define rip(i, n, s) for (int i = (s);i < ( int )( n ); i++)
#define all(a) (a).begin(), (a).end()
typedef long long ll;
typedef pair<int,int> P;
struct modint {
long long num;
long long p;
modint() {
num = 0;
p = 1000000007;
}
modint(int x) {
p = 1000000007;
num = (long long)x%p;
}
modint(long long x) {
p = 1000000007;
num = x % p;
}
modint operator*(const long long &other) {
modint ret;
ret.p = p;
ret.num = (num*(other%p)) % p;
return ret;
}
void operator*=(const long long &other) {
num = (num*other) % p;
}
};
int main(){
int n,m;
cin >> n >> m;
vector<P> da(n+m);
rip(i,n,0){
cin >> da[i].first;
da[i].second=1;
}
rip(i,m,0){
cin >> da[i+n].first;
da[i+n].second=0;
}
set<int> ch;
rip(i,n,0){
if(ch.count(da[i].first)){
cout << 0 << endl;
return 0;
}
else{
ch.insert(da[i].first);
}
}
ch.clear();
rip(i,m,0){
if(ch.count(da[i+n].first)){
cout << 0 << endl;
return 0;
}
else{
ch.insert(da[i+n].first);
}
}
modint ans(1);
int now=n*m,h=0,w=0,cr=0;
sort(all(da));
reverse(all(da));
int co=0;
while(co<n+m){
//printf("%d %d %d %d %d %lld\n",now,co,h,w,cr,ans.num);
if(now<=0){
cout << 0 << endl;
return 0;
}
if(da[co].first==now){
if(co<n*m-1){
if(da[co+1].first==now){
h++;
w++;
cr++;
co++;
}
else{
if(da[co].second){
ans*=w;
h++;
}
else{
ans*=h;
w++;
}
cr++;
}
}
else{
if(da[co].second){
ans*=w;
h++;
}
else{
ans*=h;
w++;
}
}
co++;
}
else{
if((h*w)-cr>0){
ans*=(h*w)-cr;
cr++;
}
else{
cout << 0 << endl;
return 0;
}
}
now--;
}
rip(i,now+1,1){
ans*=i;
}
cout << ans.num << endl;
} | #include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <utility>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <functional>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
typedef vector<vector<P>> vvP;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define exrep(i, a, b) for(ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 1000000007;
const ll INF = 1e16;
const ll MAX_N = 100010;
int main() {
ll h, w;
cin >> h >> w;
vl a(h);
set<ll> st1;
vl v1(h*w+1); // v1[i] : aの中でi以上の数の個数
rep(i, h) {
cin >> a[i];
st1.insert(a[i]);
v1[a[i]]++;
}
vl b(w);
set<ll> st2;
vl v2(h*w+1); // v2[i] : bの中でi以上の数の個数
rep(i, w) {
cin >> b[i];
st2.insert(b[i]);
v2[b[i]]++;
}
if(st1.size() != h || st2.size() != w) {
out(0);
re0;
}
for(ll i = h*w; i >= 2; i--) {
v1[i-1] += v1[i];
v2[i-1] += v2[i];
}
ll ans = 1;
ll cnt = 0;
for(ll x = h*w; x >= 1; x--) {
if(st1.find(x) != st1.end() && st2.find(x) != st2.end()) {
ans *= 1;
}
else if(st1.find(x) != st1.end() && st2.find(x) == st2.end()) {
ans *= v2[x];
}
else if(st1.find(x) == st1.end() && st2.find(x) != st2.end()) {
ans *= v1[x];
}
else {
ans *= max(0LL, v1[x]*v2[x] - cnt);
}
ans %= mod;
cnt++;
}
out(ans);
re0;
} | 1 |
#include <bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define all(v) v.begin(),v.end()
#define ll long long int
#define IOS ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define D1(x) {cerr<<" [" <<#x<<": "<<x<<"]";cout << endl;}
#define D2(x) {cerr<<" [" <<#x<<": ";for(auto it:x)cerr<<it<< " ";cerr<<"] ";cout << endl;}
const ll MOD=1e9+7;
void solve(){
ll n,a,b;
cin >> n >> a >> b;
vector<ll> v(n);
for(int i=0;i<n;i++)cin >>v[i];
ll ans=0;
for(int i=0;i<n-1;i++){
ans+= min((v[i+1]-v[i])*a,b);
}
cout << ans;
}
int main(){
IOS
solve();
}
// integer overflow
// remember to clear visited array
// take input as vector<string> while using grid
// don't assign after modifying
| #include<bits/stdc++.h>
using namespace std;
#define int long long
#define vi vector<int>
#define ff first
#define ss second
#define file_se_input freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#define pp long long int
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define pb push_back
#define mp make_pair
#define pi 3.141592653589793238
#define eb emplace_back
#define rep(i,a,b) for (int i = a; i <= b; i++)
#define zip(i,a,b) for(int i=a;i<b;i++)
#define rzip(i,a,b) for(int i=a;i>=b;i--)
#define ll unsigned long long int
#define test int t;cin>>t; while(t--)
#define um unordered_map
#define en '\n'
#define us unordered_set
typedef pair<int, int> pii;
typedef pair<char, int> pci;
typedef pair<char, char> pcc;
typedef vector<pii> vii;
typedef long double ld;
#define all(v) v.begin(), v.end()
#define INF (1e18+5)
#define inf (1e9+5)
#define mod 1000000007
bool check_prime(long long n)
{
int flag = 0;
for (long long i = 2; i * i <= n; i++)
{
if (n % i == 0)
{
flag = 1;
break;
}
}
if (n == 1)
return false;
else if (flag == 0 || n == 2 || n == 3)
{
return true;
}
else
{
return false;
}
}
int BE(int x, int n, int m) //function to calculate x raise to power n modulo m
{
int result = 1;
while (n > 0)
{
if (n % 2 == 1)
result = result * x % m;
x = x * x % m;
n = n / 2;
}
return result;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(0);
#ifndef ONLINE_JUDGE
if (fopen("input.txt", "r"))
{
freopen ("input.txt" , "r" , stdin);
freopen ("output.txt" , "w" , stdout);
}
#endif
//_______________________________-code starts-_______________________________________________
int n, x;
cin >> n >> x;
int a[n];
zip(i, 0, n)
{
cin >> a[i];
}
int ans = 0;
zip(i, 0, n - 1)
{
if (a[i] + a[i + 1] > x)
{
int p = a[i + 1] + a[i] - x;
if (p < a[i + 1])
{
ans += p;
a[i + 1] -= p;
p = 0;
}
else
{
a[i + 1] = 0;
ans += a[i + 1];
p -= a[i + 1];
a[i] -= p;
ans += p;
}
}
}
cout << ans << en;
return 0;
}
| 0 |
/*------------------------------------
........Bismillahir Rahmanir Rahim....
..........created by Abdul Aziz.......
------------------------------------*/
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <unordered_map>
#include <queue>
#define mod 998244353
#define int long long
#define ld long double
#define pb push_back
#define sz(x) (int)x.size()
#define ff first
#define ss second
#define pii pair<int,int>
using namespace std;
vector<int> allPrimes;
void sieve(int n)
{
vector<bool> prime(n+1, true);
for (int p=2; p*p<=n; p++) {
if (prime[p] == true) {
for (int i=p*2; i<=n; i += p)
prime[i] = false;
}
}
for (int p=2; p<=n; p++)
if (prime[p])
allPrimes.push_back(p);
}
int solve(int n)
{
sieve(n);
int result = 1;
for (int i=0; i < allPrimes.size(); i++)
{
int p = allPrimes[i];
int x = 0;
while (p <= n)
{
x = x + (n/p);
p = p*allPrimes[i];
}
result = (result*(x+1))%1000000007;
}
return result;
}
signed main()
{
int n=1; cin>>n;
cout << solve(n) << endl ;
return 0;
}
| #include<bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (int)(n); i++)
#define sum(a) accumulate(a.begin(),a.end(),0LL)
#define put(i) cout<<fixed<<i<<endl
using namespace std;
using ll = long long;
ll mod = 1e9+7;
vector<ll> cnt(1e3+1,0);
void disassenmble(ll n){
for(long long i = 2; i * i <= n; i++){
if(n % i != 0){ continue; }
ll ex = 0;
while(n % i == 0){
ex++;
n /= i;
}
cnt[i] += ex;
}
if(n != 1) ++cnt[n];
}
int main(){
ll n; cin >> n;
ll ans = 1;
for(long long i = 1; i <= n; i++) disassenmble(i);
for(long long i = 2; i <= n; i++){
if(cnt[i]) ans = (ans * (cnt[i] + 1)) % mod;
}
//for(long long i = 0; i <= n; i++){ cout << cnt[i] << " ";} cout << endl;
put(ans);
} | 1 |
#include <iostream>
using namespace std;
int main() {
int n, m;
scanf("%d %d", &n, &m);
int a, b;
if (m & 1)
a = m, b = m + 1;
else
a = m + 1, b = m;
for (int i = 1, j = a; i < j; i++, j--)
printf("%d %d\n", i, j);
for (int i = 1, j = b; i < j; i++, j--)
printf("%d %d\n", a + i, a + j);
return 0;
} | #include <bits/stdc++.h>
#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 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;
int main() {
int n, m;
cin >> n >> m;
rep(i, m/2){
cout << i+1 << " " << m-i << endl;
}
rep(i, (m+1)/2){
cout << i+1+m << " " << 2*m+1-i << endl;
}
}
| 1 |
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#define rep(i,n) for (int i = 0; i < n; i++)
using namespace std;
int main() {
int a, b, e, k;
cin >> a;
rep(i, 3) cin >> b;
cin >> e >> k;
if ((e - a) <= k) cout << "Yay!";
else cout << ":(";
cout << endl;
return 0;
} | #define _USE_MATH_DEFINES
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <bitset>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<ll, ll, ll> tl3;
const int BIG_NUM = 1e9;
const ll INF = 1000000000000000000;
const ll MOD = 1e9 + 7;
//const ll MOD = 998244353;
int main() {
vector<int> v(5);
for (int i = 0; i < 5; i++) {
cin >> v[i];
}
sort(v.begin(), v.end());
int k;
cin >> k;
if (abs(v[0] - v[4]) <= k) {
cout << "Yay!" << endl;
}
else {
cout << ":(" << endl;
}
}
| 1 |
#define _CRT_SECURE_NO_WARNINGS
#include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define FOR(i, m, n) for(int i=(m); i<(n); ++i)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mp make_pair
#define pb push_back
#define Cout(x) cout << (x) << endl
#define Cout2(x, y) cout << (x) << " " << (y) << endl
#define dump(x) cout << #x << " = " << (x) << endl;
#define forauto(i, a) for(auto i : a)cout << i << " "; puts("");
typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<string> VS;
typedef vector<bool> VB;
typedef vector<vector<int>> VVI;
typedef pair<int, int> PII;
const int inf = 1e9;
const double pi = acos(-1.0);
int main() {
int n, m;
while (cin >> n >> m && (n || m)) {
VI p(n); rep(i, n)cin >> p[i];
sort(all(p)); reverse(all(p));
int ans = 0;
for (int i = 0; i < n; i += m) {
for (int j = i; j < min(i + m - 1, n); j++) {
ans += p[j];
}
}
Cout(ans);
}
}
| #include <cstdio>
using namespace std;
int main()
{
char mp[15][15] = {0};
int pi, pj;
int cnt;
while (scanf("%s", mp[1]) != EOF){
cnt = 0;
for (int i = 2; i <= 12; i++) scanf("%s", mp[i]);
for (int i = 1; i <= 12; i++){
for (int j = 0; j < 12; j++){
if (mp[i][j] == '1'){
if(mp[i-1][j] != '2') {
cnt++;
}
while(mp[i][j] != '0' && j < 12){
mp[i][j] = '2';
pi = i;
while (mp[++pi][j] == '1'){
pj = j;
while(mp[pi][pj] == '1'){
mp[pi][pj] = '2';
pj--;
}
pj = j;
while(mp[pi][++pj] == '1'){
mp[pi][pj] = '2';
}
}
j++;
}
}
}
}
printf("%d\n", cnt);
}
} | 0 |
#include<cstdio>
unsigned prime[238] = {2, 3, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499};
unsigned count[4], mod_five[4][55];
int main(){
unsigned N;
scanf("%u", &N);
for (size_t i = 0; i < N<<2; i++){
unsigned r = prime[i]%5-1;
mod_five[r][count[r]++] = prime[i];
if(count[r] == N){
for (size_t j = 0; j < N; j++){
if(j) printf(" ");
printf("%u", mod_five[r][j]);
}
return 0;
}
}
return 0;
} | //
#include <bits/stdc++.h>
using namespace std;
#define INF 1000000000
#define MOD 1000000007
#define PI 3.14159265
#define EPS 1e-9
#define Pi acos(-1.0)
typedef pair<int, int> ii;
typedef long long ll;
typedef vector<ll> vll;
typedef pair<ll,ll> pll;
#define forr(i,a,b) for(int i=(a); i<(b); i++)
#define clean(arr,val) memset(arr,val,sizeof(arr))
#define forn(i,n) forr(i,0,n)
#define PB push_back
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<pll> vpll;
/*CODE START HERE*/
int n, ans;
int main(){
ios::sync_with_stdio(0);
cin >> n;
forr(i,1,INF){
if(i*i <= n){
ans = i*i;
}else{
break;
}
}
cout << ans << "\n";
return 0;
} | 0 |
#include<iostream>
int main(){
std::string s; std::cin >> s;
int diff=1e4;
for(int i=0; i<s.size()-2; ++i){
int num=std::stoi(s.substr(i,3));
num = std::abs(num-753);
diff = std::min(diff,num);
}
std::cout << diff << std::endl;
return 0;
} | #include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<vector>
#include<list>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<stack>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define rep(i, n) for(int i=0;i<n;++i)
#define rrep(i, n) for(int i=n;i>=0;--i)
const int inf=1e9+7;
const ll mod=1e9+7;
const ll big=1e18;
const double PI=2*asin(1);
int main() {
int N;
cin>>N;
cout<<N/3<<endl;
}
| 0 |
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
//#include <stack>
//#include <queue>
//#include <cstdio>
#include <cmath>
#include <iterator>
#include <map>
//#include <list>
#include <iomanip>
using namespace std;
#define IOS ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define prec std::cout << std::fixed << std::setprecision(15);
#define endl "\n"
#define MOD 1000000007
#define Int int64_t
#define PI 3.14159265358979
#define ssort(z) sort(z.begin(), z.end())
#define rsort(z) sort(z.rbegin(), z.rend())
#define eerase(z) z.erase(unique(z.begin(), z.end()), z.end())
#define ccnt(z, w) count(z.begin(), z.end(), w)
#define rep(i,a,n) for(Int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(Int (i)=(a); (i)<=(n); (i)++)
const int MAX_N = 1000000;
const Int MAX_N_Int = 1000000000000;
template <typename T>
void printV(const std::vector<T>& v, const char * const separator = " ")
{
if(!v.empty())
{
std::copy(v.begin(),
--v.end(),
std::ostream_iterator<T>(std::cout, separator));
std::cout << v.back() << "\n";
}
}
bool isPrime(int num)
{
if (num < 2) return false;
else if (num == 2) return true;
else if (num % 2 == 0) return false; // 偶数はあらかじめ除く
double sqrtNum = sqrt(num);
for (int i = 3; i <= sqrtNum; i += 2)
{
if (num % i == 0)
{
// 素数ではない
return false;
}
}
// 素数である
return true;
}
Int gcd(Int a, Int b) {
return b != 0 ? gcd(b, a % b) : a;
}
Int lcm(Int a, Int b) {
return a / gcd(a, b) * b;
//a*bは64bit integer overflow
}
int Max(int a, int b, int c) {
int temp = max(a, b);
return max(temp, c);
}
int Min(int a, int b, int c) {
int temp = min(a, b);
return min(temp, c);
}
bool integer(double num) {
return floor(num) == num;
}
Int fact(int num) {
if (num == 0)
return 1;
else
return num * fact(num - 1);
}
Int yakusu(int n) {
int cnt = 0;
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
cnt++;
if (i * i != n) cnt++;
}
}
return cnt;
}
Int fact_mod(Int n, Int mod) {
Int f = 1; repq(i,2,n) f = f * (i % MOD) % MOD;
return f;
}
// 繰り返し二乗法 (modの世界での累乗)
Int mod_pow(Int x, Int n, Int mod) {
Int res = 1;
while(n > 0) {
if(n & 1) res = (res * x) % mod;
x = (x * x) % mod;
n >>= 1;
}
return res;
}
// 組み合わせ nCr を求める
Int combination_mod(Int n, Int r, Int mod) {
if(r > n-r) r = n-r;
if(r == 0) return 1;
Int a = 1;
rep(i, 0, r) a = a * ((n-i) % mod) % mod;
Int b = mod_pow(fact_mod(r, mod), mod-2, mod);
return (a % mod) * (b % mod) % mod;
}
Int cntZero(string s){
int cnt=0;
reverse(s.begin(),s.end());
for (int i = 0; i < s.length(); ++i) {
if(s[i]=='0')cnt++;
else break;
}
return cnt;
}
int main() {
//COMinit();
IOS;
prec;
Int a,b,c,d,n,m,k,x,y=0,ans=0,ans1=0;
string str,s="",t,u;
Int h,w;
cin>>h>>w>>n;
Int cnt=0;
rep(i,0,min(h,w)){
ans+=max(h,w);
cnt++;
if(ans>=n){
cout<<cnt<<endl;
return 0;
}
}
rep(i,0,max(h,w)){
ans+=min(h,w);
cnt++;
if(ans>=n){
cout<<cnt<<endl;
return 0;
}
}
cout<<endl;
return 0;
}
| #include <algorithm>
#include <chrono>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = int64_t;
#define rep(i, j, n) for (int i = j; i < (int)n; ++i)
#define rrep(i, j, n) for (int i = (int)n - 1; j <= i; --i)
constexpr ll MOD = 1000000007;
constexpr int INF = 0x3f3f3f3f;
constexpr ll INFL = 0x3f3f3f3f3f3f3f3fLL;
int main() {
int h, w, n;
cin >> h >> w >> n;
int m = max(h, w);
cout << (n + m - 1) / m << endl;
#ifdef LOCAL
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
return 0;
} | 1 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int INF = 1001001001;
long long pow2[60];
void init_pow2(){
pow2[0] = 1;
for(int i=1; i<60; i++){
pow2[i] = 2*pow2[i-1];
pow2[i] %= MOD;
}
return;
}
int main(){
init_pow2();
int n;cin>>n;
vector<long long> a(n);
vector<long long> count(60, 0);
long long s = 0;
for(int i=0; i<n; i++){
cin>>a[i];
s += a[i];
s %= MOD;
for(int j=0; j<60; j++){
if((a[i]>>j)&1)count[j]++;
}
}
s *= n-1;
s %= MOD;
for(int j=0; j<60; j++){
long long t=1;
t *= count[j]*(count[j]-1)/2;
t %= MOD;
t *= 2 * pow2[j];
t %= MOD;
s += MOD - t;
s %= MOD;
}
cout << s << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
//#define cerr if (false) cerr
#define db(x) cerr << #x << "=" << x << endl
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z) cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << endl
#define dbv(v) cerr << #v << "="; for (auto _x : v) cerr << _x << ", "; cerr << endl
#define dba(a, n) cerr << #a << "="; for (int _i = 0; _i < (n); ++_i) cerr << a[_i] << ", "; cerr << endl
template <typename A, typename B>
ostream& operator<<(ostream& os, const pair<A, B>& x) {
return os << "(" << x.first << "," << x.second << ")";
}
typedef long long ll;
typedef long double ld;
template <typename T>
struct Fenwick {
int n;
vector<T> bit;
Fenwick(int _n) : n(_n) {
bit.resize(n + 1);
}
void update(int p, T v) {
for (; p <= n; p += p & -p) bit[p] += v;
}
T query(int p) {
T ret{};
for (; p; p -= p & -p) ret += bit[p];
return ret;
}
};
int main() {
int n;
scanf("%d", &n);
vector<vector<int>> A(3, vector<int>(n));
for (int i = 0; i < 3; ++i)
for (int j = 0; j < n; ++j) {
scanf("%d", &A[i][j]);
--A[i][j];
}
vector<int> flip(2);
auto no = []() {
printf("No\n");
exit(0);
};
for (int c = 0; c < n; ++c) {
if (A[0][c] / 3 != A[1][c] / 3 || A[1][c] / 3 != A[2][c] / 3 || A[1][c] % 3 != 1) {
no();
}
if ((A[0][c] / 3) % 2 != (c % 2)) no();
flip[c % 2] ^= A[0][c] % 3 == 2;
}
for (int p = 0; p < 2; ++p) {
Fenwick<int> fenw(n);
for (int c = p; c < n; c += 2) {
int i = A[0][c] / 3;
int greater = fenw.query(n) - fenw.query(i + 1);
flip[1 - p] ^= greater & 1;
fenw.update(i + 1, 1);
}
}
printf(flip[0] == 0 && flip[1] == 0 ? "Yes\n" : "No\n");
}
| 0 |
// A.
#include <iostream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#include <cmath>
#include <map>
#include <set>
using namespace std;
typedef long double LD;
typedef long long LL;
int main(int argc, char* argv[]) {
LL n, nfc = 0, fc = 0, ans = 0;
cin >> n;
vector<vector<int>> sum(20, vector<int>(20));
vector<int> two(n), five(n);
for (int i = 0; i < n; ++i) {
string s;
cin >> s;
bool p = false;
LL x = 0;
for (char c : s) {
if (c == '.') {
p = true;
} else {
x = (x * 10) + c - '0';
two[i] -= p;
five[i] -= p;
}
}
while (x % 2 == 0) {
++two[i];
x /= 2;
}
while (x % 5 == 0) {
++five[i];
x /= 5;
}
sum[min(two[i], 9) + 10][min(five[i], 9) + 10] += 1;
}
for (int i = 0; i < n; ++i) {
for (int j = -10; j < 10; ++j) {
for (int k = -10; k < 10; ++k) {
if (j + two[i] >= 0 && k + five[i] >= 0) {
ans += sum[j + 10][k + 10];
}
}
}
ans -= two[i] >= 0 && five[i] >= 0;
}
cout << ans / 2 << endl;
return 0;
}
| /* Welcome to my Code */
#include <bits/stdc++.h>
#define ll long long int
#define i(a,n) int a=n;
#define l(a,n) ll a=n;
#define d(a,n) double a=n;
#define s(a) string a;
#define c(a) ll a; cin>>a;a
#define cd(a) double a; cin>>a;
#define cs(a) string a; cin>>a;
#define ci(a) cin>>a;
#define co(a) cout<<a;
#define cos(a) cout<<a<<" ";
#define con(a) cout<<a<<endl;
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define nl cout<<endl;
#define ca(arr,n) for(ll i=0;i<n;i++) cin>>arr[i];
#define f0(i,a,n) for(ll i=a;i>=n;i--)
#define f(i,n) for(ll i=0;i<n;i++)
#define f1(i,a,n) for(ll i=a;i<n;i++)
#define lcm(a,b) (a*b)/__gcd(a,b)
#define T ll t=1; cin>>t; while(t--)
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define ret(x) return cout<<x,0;
using namespace std;
using namespace std;
int n;
long long ans;
long long num[25][25];
int main()
{
cin>>n;
while(n--)
{
double t;
cin>>t;
long long a=t*1e9+0.5;
int n2=1,n5=1;
while(a%2==0)
{
a/=2;
n2++;
}
while(a%5==0)
{
a/=5;
n5++;
}
n2=min(19,n2);
n5=min(19,n5);
f1(i,20-n2,20) f1(j,20-n5,20) ans+=num[i][j];
num[n2][n5]++;
}
cout<<ans<<endl;
return 0;
}
/*It's not who I am underneath, but what I do that defines me
MMMMMMMMMMMMMMMMMMMMM. MMMMMMMMMMMMMMMMMMMMM
`MMMMMMMMMMMMMMMMMMMM M\ /M MMMMMMMMMMMMMMMMMMMM'
`MMMMMMMMMMMMMMMMMMM MMMMMM MMMMMMMMMMMMMMMMMMM'
MMMMMMMMMMMMMMMMMMM-_______MMMMMMMM_______-MMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
.MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM.
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
`MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM'
`MMMMMMMMMMMMMMMMMM'
`MMMMMMMMMM'
MMMMMM
MMMM
MM
*/
| 1 |
// B - Reverse and Compare
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
typedef long long ll;
// const int INF = 2147483647;
// const ll INF = 9223372036854775807;
// const ll MOD = 1000000007;
// using mint = modint1000000007;
// const ll MOD = 998244353;
// using mint = modint998244353;
int main() {
string A;
cin >> A;
unordered_map<char, int> cnt;
for (char c : A) {
cnt[c]++;
}
ll N = A.size();
ll ans = N * (N-1) / 2 + 1;
for (auto itr : cnt) {
ll n = itr.second;
ans -= n * (n-1) / 2;
}
cout << ans << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep2(i,n) for(int i=1;i<(n);i++)
typedef long long ll;
int main(){
ll x; cin >> x;
ll y=x%11;
ll ans=0;
ans+=(x/11)*2;
if(y>6){
ans+=2;
}else if(y>0){
ans++;
}
cout << ans << endl;
} | 0 |
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=int(a);i<int(b);++i)
using namespace std;
typedef long long ll;
int INF = (1LL << 30) - 1;
ll MOD = 1e9+7;
main(){
ll N,M;
cin >> N >> M;
vector<ll> X(N),Y(M),accumX(N+1),accumY(M+1);
rep(i,0,N){
cin >> X[i];
X[i] += 1e9;
accumX[i+1] = (accumX[i] + X[i]) % MOD;
}
rep(i,0,M){
cin >> Y[i];
Y[i] += 1e9;
accumY[i+1] = (accumY[i] + Y[i]) % MOD;
}
ll sumx = 0,sumy = 0;
rep(i,0,N-1){
sumx += (accumX[N] - accumX[i+1] - X[i] * (N-i-1) + MOD * MOD) % MOD;
sumx %= MOD;
}
rep(i,0,M-1){
sumy += (accumY[M] - accumY[i+1] - Y[i] * (M-i-1) + MOD * MOD) % MOD;
sumy %= MOD;
}
cout << (sumx * sumy) % MOD << endl;
} | #include<bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<ll,ll> P;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
#define M 1000000007
#define all(a) (a).begin(),(a).end()
#define rep(i,n) reps(i,0,n)
#define reps(i,m,n) for(int i=(m);i<(n);i++)
int main(){
ll n,k;cin>>n>>k;
vector<ll> a(n),b(k);
rep(i,n)cin>>a[i];
rep(i,k)cin>>b[i];
ll c=0LL,d=0LL;
ll j=-n+1;
rep(i,n){
c+=a[i]*j;
j+=2;
}
j=-k+1;
rep(i,k){
d+=b[i]*j;
j+=2;
}
cout<<(c%M)*(d%M)%M;
} | 1 |
#include "bits/stdc++.h"
#define REP(i,num) for(int i=0;i<(num);++i)
#define LOOP(i) while(i--)
#define ALL(c) c.begin(),c.end()
#define PRINTALL(c) for(auto pitr=c.begin();pitr!=c.end();++pitr){cout<<*pitr;if(next(pitr,1)!=c.end())cout<<' ';}cout<<endl;
#define PAIRCOMP(c,comp) [](const pair<ll,ll>& lhs,const pair<ll,ll>& rhs){return lhs.c comp rhs.c;}
using namespace std;
using ll = long long;
constexpr ll atcoder_mod = 1e9+7;
template<typename T=int>
T in(){T x; cin >> x; return (x);}
template<typename T=int,typename C=vector<T>>
C vecin(int N){C x(N);REP(i,N){x[i]=in<T>();}return move(x);}
void vout(){cout << endl;}
template<typename Head,typename... Tail>
void vout(Head&& h,Tail&&... t){cout << ' ' << h;vout(forward<Tail>(t)...);}
void out(){cout << endl;}
template<typename Head,typename... Tail>
void out(Head&& h,Tail&&... t){cout << h;vout(forward<Tail>(t)...);}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
cout<<fixed<<setprecision(10);
int N=in();
vector<pair<ll,ll>> P(N);
ll SA=0,SB=0;
REP(i,N){
P[i].first=in();
P[i].second=in();
SB += P[i].second;
}
sort(ALL(P),[](pair<ll,ll>& l,pair<ll,ll>& r){return l.first+l.second>r.first+r.second;});
int H=ceil(N/2.0);
REP(i,H){
SA += P[2*i].first;
SB -= P[2*i].second;
}
out(SA-SB);
return 0;
}
| #include <bits/stdc++.h>
#define int long long
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define F first
#define S second
using namespace std;
typedef pair<int,int> P;
typedef vector<int> ivec;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
const int MOD=1000000007;
int INF=100100100100100;
int sa[100010];
signed main(){
int n;cin>>n;
int ans=0;
rep(i,n){
int a,b;cin>>a>>b;
sa[i]=a+b;
ans-=b;
}
sort(sa,sa+n,greater<int>());
for(int i=0;i<n;i+=2){
ans+=sa[i];
}
cout<<ans<<endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<lint> vlint;
typedef vector<vlint> vvlint;
typedef vector<bool> vbool;
typedef vector<int> vint;
#define inf 1e+9
#define endl "\n"
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n); i++)
#define rep_rev(i, n) for (int i = n-1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define Size(c) (int)(c).size()
#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))
template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
template <class T, class S> inline bool chmax(T &a, S b) {
if(a < b) {a = b;return 1;} return 0;
}
template <class T, class S> inline bool chmin(T &a, S b) {
if(a > b) {a = b;return 1;} return 0;
}
template <class T>
void line(vector<T> &x){for (auto&& xi : x) cin >> xi;}
inline void IN(void){ return;}
template <typename First, typename... Rest>
void IN(First& first, Rest&... rest){ cin >> first; IN(rest...); return;}
#define INT(...) int __VA_ARGS__; IN (__VA_ARGS__)
#define LINT(...) lint __VA_ARGS__; IN (__VA_ARGS__)
#define STR(...) string __VA_ARGS__; IN(__VA_ARGS__)
template <class T> void UNIQUE(vector<T> &x) {
sort(all(x)); x.erase(unique(all(x)), x.end());
}
template <class T> void print_vec(T first, T end){
for (auto i = first; i < end - 1; i++) cout << *i << " ";
cout << *(end-1) << endl;
}
template<class... T>
void debug_print(T... args){
vector<lint> tmp = initializer_list<lint>{args...};
print_vec(all(tmp));
}
template<class T>
void print(T a){ cout << a << endl;}
vector<string> Yes = {"No", "Yes"};
vector<string> YES = {"NO", "YES"};
int ord(char x){ return (int)(x - 'a');}
char chr(lint x){ return (char)(x + (lint)('a'));}
lint mod = 1e9+7;
// lint mod =
lint sum(vlint a){lint ret = 0; for(auto&& v:a) ret += v; return ret;}
lint vmini(vlint a, lint &index){
lint ret = LLONG_MAX;
rep(i, Size(a)){ if (chmin(ret, a[i])) index = i;}
return ret;
}
lint vmaxi(vlint a, lint &index){
lint ret = -LLONG_MAX;
rep(i, Size(a)){ if (chmax(ret, a[i])) index = i;}
return ret;
}
lint vmin(vlint a){
lint ret = LLONG_MAX; for (auto && v : a) chmin(ret, v); return ret;
}
lint vmax(vlint a){
lint ret = -LLONG_MAX; for (auto && v : a) chmax(ret, v); return ret;
}
vlint base_3(int k){
vlint ret; while (k > 0){ ret.pb(k % 3); k /= 3;} reverse(all(ret)); return ret;
}
map<lint, lint> trial_division(lint n){
map<lint, lint> factor;
lint tmp = (lint)(sqrt(n)) + 1;
for (lint i = 2; i < tmp; ++i){
while (n % i == 0){
n /= i;
factor[i] ++;
}
}
if(n != 1) factor[n] ++;
return factor;
}
bool judge(lint u){
return 0 <= u && u <= 5000;
}
lint dist(lint x1, lint y1, lint x2, lint y2){
return (x1-x2) * (x1-x2) + (y1-y2) * (y1 - y2);
}
int main(){
LINT(n);
vlint a(n), b(n), c(n);
line(a); line(b); line(c);
sort(all(a)); sort(all(c));
lint u, d; lint ans = 0;
rep(i, n){
u = lb(a, b[i]);
d = n - ub(c, b[i]);
ans += u * d;
}
print(ans);
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
using P = pair<int, int>;
#define LOG(variable) cerr << #variable":\t" << (variable) << "\n"
#define LOGCON(i, container) for(int (i) = 0; (i) < (container).size(); ++(i)) cerr << (i) << ":\t" << (container)[(i)] << "\n"
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPS(i, r, n) for (int i = (r); i < (n); ++i)
#define REPR(i, n) for(int i = (n); i >= 0; --i) // from n to 0
#define REPRS(i, n, r) for(int i = (n); i >= (r); --i) // from n to r
#define REPOBJ(itr, obj) for(auto itr = (obj).begin(); itr != (obj).end() ; ++itr)
#define REPROBJ(itr, obj) for(auto itr = (obj).rbegin(), e = (obj).rend(); itr != e; ++itr)
#define COUTB(x) cout << (x) << "\n"
#define COUTS(x) cout << (x) << " "
#define PB push_back
#define SORT(obj) sort((obj).begin(), (obj).end())
#define SORTR(obj) sort((obj).begin(), (obj).end(), greater<>())
#define ALL(obj) (obj).begin(), (obj).end()
#define MOD 1000000007
#define PI (acos(-1))
template<typename T = int>
T in() {T a; cin >> a; return a;}
/***** MAIN *****/
int lowerbound(vector<int> &v, int num) {
int left = 0, right = v.size()-1;
while(right-left > 0) {
int mid = (left + right) / 2;
if(v[mid] >= num) right = mid;
else left = mid + 1;
}
if(v[left] >= num) return left;
else return v.size();
}
int upperbound(vector<int> &v, int num) {
int left = 0, right = v.size()-1;
while(right-left > 0) {
int mid = (left + right) / 2;
if(v[mid] > num) right = mid;
else left = mid + 1;
}
if(v[left] > num) return left;
else return v.size();
}
signed main() {
int n; cin >> n;
vector<int> a(n), b(n), c(n);
REP(i,n) cin >> a[i];
REP(i,n) cin >> b[i];
REP(i,n) cin >> c[i];
SORT(a); SORT(b); SORT(c);
int ans = 0;
REP(i,n) {
int upper_count = lowerbound(a, b[i]);
int lower_count = n - upperbound(c, b[i]);
ans += upper_count * lower_count;
}
cout << ans;
cout << "\n";
return 0;
}
/***** MAIN *****/ | 1 |
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define INF 2000000000
typedef pair<int, int> P;
struct edge{ int to, cost;};
int V, E;
vector<edge> G[100010];
int d[100010];
void dijkstra(int s){
priority_queue<P, vector<P>, greater<P> > que;
for(int i = 0;i < V;i++) 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(int i = 0;i < G[v].size();i++){
edge e = G[v][i];
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 r, s;
edge e;
cin >> V >> E >> r;
for(int i = 0;i < E;i++){
cin >> s >> e.to >> e.cost;
G[s].push_back(e);
}
dijkstra(r);
for(int i = 0;i < V;i++){
if(d[i] == INF) cout << "INF" << endl;
else cout << d[i] << endl;
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
void chmax(int& a, int b) {
if (a < b) a = b;
}
void chmin(int& a, int b) {
if (a > b) a = b;
}
int main () {
int N, M, X, Y;
cin >> N >> M >> X >> Y;
for (int i = 0; i < N; i++) {
int x;
cin >> x;
chmax(X, x);
}
for (int i = 0; i < M; i++) {
int y;
cin >> y;
chmin(Y, y);
}
if (X < Y) {
cout << "No War" << endl;
} else {
cout << "War" << endl;
}
} | 0 |
// 2014/11/05 Tazoe
#include <iostream>
using namespace std;
int main()
{
int N, M;
cin >> N >> M;
int A[1000];
for(int i=0; i<N; i++){
cin >> A[i];
}
int B[1000];
for(int i=0; i<M; i++){
cin >> B[i];
}
int cnt[1000] = {0};
for(int i=0; i<M; i++){
for(int j=0; j<N; j++){
if(B[i]>=A[j]){
cnt[j]++;
break;
}
}
}
int i_max = 0;
for(int i=1; i<N; i++){
if(cnt[i]>cnt[i_max]){
i_max = i;
}
}
cout << i_max+1 << endl;
return 0;
} | #include <iostream>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <cctype>
#include <deque>
using namespace std;
typedef long long lint;
#define cout cerr
#define ni (next_num<int>())
template<class T>inline T next_num(){
T i=0;char c;
while(!isdigit(c=getchar())&&c!='-');
bool flag=c=='-';
flag?c=getchar():0;
while(i=i*10-'0'+c,isdigit(c=getchar()));
return flag?-i:i;
}
template<class T1,class T2>inline void apmax(T1 &a,const T2 &b){if(a<b)a=b;}
template<class T1,class T2>inline void apmin(T1 &a,const T2 &b){if(b<a)a=b;}
const int N=1000010;
char a[N],b[N];
deque<int>q;
inline int Main(){
int n=ni;
scanf("%s%s",a+1,b+1);
q.push_back(n+1);
int delta=0;
int ans=0;
for(int i=n,j=n+1;i>=1;i--){
int last=j;
for(;j>=1&&(i<j||b[i]!=a[j]);j--);
if(j<1)return -1;
if(last==j){
for(;!q.empty()&&q.back()+delta>=i;q.pop_back());
q.push_back(i-delta);
}else{
delta--;
if(i!=j){
apmax(ans,(int)q.size());
q.push_front(j-delta);
}
}
}
return ans;
}
int main(){
printf("%d\n",Main());
return 0;
} | 0 |
/*
強連結成分アルゴリズムアルゴリズム
input:有向グラフG
output:各店の属する強連結成分を表す関数comp:V(G)->N
*/
#include <vector>
#include <functional>
using namespace std;
vector<int> scc(const vector<vector<int>>& G){
vector<int> comp(G.size(),-1);
vector<int> R(G.size(),0);
int N = 0;
vector<int> psi(G.size(),-1);
vector<int> psi_inv(G.size(),-1);
function<void(int)> visit1 = [&](int v){
R[v] = 1;
for(int w : G[v]){
if(R[w] == 0) visit1(w);
}
psi[v] = N;
psi_inv[N] = v;
N = N + 1;
};
for(int i = 0;i < G.size();i++){
if(R[i] == 0) visit1(i);
}
R.assign(G.size(),0);
int K = 0;
vector<vector<int>> rev_G(G.size());
for(int v = 0;v < G.size();v++){
for(int to : G[v]) rev_G[to].push_back(v);
}
function<void(int)> visit2 = [&](int v){
R[v] = 1;
for(int w : rev_G[v]){
if(R[w] == 0) visit2(w);
}
comp[v] = K;
};
for(int i = G.size() - 1;i >= 0;i--){
if(R[psi_inv[i]] == 0){
visit2(psi_inv[i]);
K = K + 1;
}
}
return comp;
}
#include <iostream>
int main(){
int v;
int e;
cin >> v >> e;
vector<vector<int>> G(v);
for(int i = 0;i < e;i++){
int x,y;
cin >> x >> y;
G[x].push_back(y);
}
auto res = scc(G);
int q;
cin >> q;
for(int i = 0;i < q;i++){
int x,y;
cin >> x >> y;
cout << (res[x] == res[y]) << endl;
}
}
| #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
constexpr char ln = '\n';
constexpr long long MOD = 1000000007LL;
constexpr long long INF = 1001001001LL;
constexpr long long LINF = 1001001001001001001;
#define all(x) (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define rept(i, j, n) for(int i=(j); i<(n); i++)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
int main(){
int n, m, q; cin >> n >> m >> q;
vector<vector<int>> S(n, vector<int>(n));
rep(i, m){
int l, r; cin >> l >> r; l--, r--;
S[l][r]++;
}
rep(i, n){
rep(j, n-1){
S[i][j+1] += S[i][j];
}
}
rep(j, n){
for(int i=n-1; i>0; i--){
S[i-1][j] += S[i][j];
}
}
vector<int> res(q);
rep(i, q){
int p, q; cin >> p >> q; p--, q--;
res[i] = S[p][q];
}
for(auto r: res) cout << r << ln;
}
| 0 |
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
#include <queue>
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <limits>
using namespace std;
typedef long long LL;
int main(int argc, char* argv[]){
cin.tie(0);
ios::sync_with_stdio(false);
int A, B;
cin >> A >> B;
int u = A + B;
int v = A + (A - 1);
int w = B + (B - 1);
printf("%d\n", max(u, max(v, w)));
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 ;
int main(){
int a,b;
cin >> a >> b;
int ans=0;
if(a>b) cout << 2*a-1 << endl;
else if(a<b) cout << 2*b-1 << endl;
else cout << 2*a << endl;
} | 1 |
#include<bits/stdc++.h>
using namespace std;
using ll = long long int;
int main(void) {
constexpr ll MOD = 1e9 + 7;
constexpr double PI = acos(-1);
cout << fixed << setprecision(16);
cin.tie(0); ios::sync_with_stdio(false);
ll n, x;
cin >> n >> x;
vector<ll> dp(x+1);
vector<ll> s(n);
for(auto &e: s) cin >> e;
sort(s.begin(), s.end(), greater<>());
dp[x] = 1;
for(ll k=1; k<=n; k++) {
vector<ll> pre(x+1);
swap(dp, pre);
for(ll i=0; i<=x; i++) {
(dp[i%s[k-1]] += pre[i]) %= MOD;
(dp[i] += pre[i]*(n-k)) %= MOD;
}
}
{
ll s = 0;
for(ll i=0; i<=x; i++)
(s += dp[i]*i) %= MOD;
cout << s << endl;
}
}
| #include <bits/stdc++.h>
using namespace std;
#define _for(i,j,N) for(int i = (j);i < (N);i++)
#define _rep(i,j,N) for(int i = (j);i <= (N);i++)
#define ALL(x) x.begin(),x.end()
#define LL long long
int N,X;
const int maxn = 201;
const int maxm = 100005;
const int modu = 1e9+7;
int dp[2][maxm];
vector<int> vi;
int mult(int a,int b){
LL c = a;c *= b;c %= modu;return c;
}
int add(int a,int b){
LL c = a;c += b;c %= modu;return c;
}
int get_int(){int x;scanf("%d",&x);return x;}
double get_double(){double x;scanf("%lf",&x);return x;}
long long get_long(){long long x;scanf("%lld",&x);return x;}
void init(){
vi.clear();
_for(i,0,maxm) dp[0][i] = 0;
}
int main(){
scanf("%d %d",&N,&X);
init();
_for(i,0,N){
int s = get_int();
vi.push_back(s);
}
sort(ALL(vi),greater<int>());
dp[0][X] = 1;
int t = 0;
_for(i,0,N){
_for(j,0,maxm){
dp[t^1][j] = 0;
}
_for(j,0,maxm){
if(dp[t][j]){
//printf("%d\n",j);
dp[t^1][j%vi[i]] = add(dp[t][j],dp[t^1][j%vi[i]]);
dp[t^1][j] = add(dp[t^1][j],mult(dp[t][j],(N-i-1)));
}
}
t= t^1;
}
int ans = 0;
_for(i,1,maxm){
if(dp[t][i] > 0){
//printf("%d %d\n",i,dp[t][i]);
ans = add(ans,mult(i,dp[t][i]));
}
}
printf("%d\n",ans);
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*b)/gcd(a,b)
#define ff first
#define ss second
#define p pair<int,int>
#define pb push_back
#define endl '\n'
#define w(t) ll test;cin>>test;while(test--)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(0);
#define pi acos(-1)
struct str{
int ax,ay,bx,by;
};
int main()
{
fast;
int h,w;
cin>>h>>w;
vector<str>ans;
int a[h+5][w+5];
for(int i=0;i<h;i++)
{
for(int j=0;j<w;j++)
{
cin>>a[i][j];
}
}
for(int i=0;i<h;i++)
{
for(int j=0;j<w-1;j++)
{
if(a[i][j]&1)
{
a[i][j+1]++;
ans.pb({i+1,j+1,i+1,j+2});
}
else continue;
}
if(i==h-1)continue;
if(a[i][w-1]&1)
{
a[i+1][w-1]++;
ans.pb({i+1,w,i+2,w});
}
}
cout<<ans.size()<<endl;
for(auto i:ans)
{
cout<<i.ax<<" "<<i.ay<<" "<<i.bx<<" "<<i.by<<endl;
}
}
| #include <bits/stdc++.h>
#define ri register
#define int long long
using namespace std; const int N=200010;
inline int read()
{
int s=0, w=1; ri char ch=getchar();
while(ch<'0'||ch>'9') { if(ch=='-') w=-1; ch=getchar(); }
while(ch>='0'&&ch<='9') s=(s<<3)+(s<<1)+(ch^48), ch=getchar();
return s*w;
}
int h,w;
char s[60][60];
signed main()
{
h=read(), w=read();
for(ri int i=1;i<=h;i++) scanf("%s",s[i]+1);
for(ri int i=1;i<=h;i++)
for(ri int j=1;j<=w;j++)
if(s[i][j]=='#')
{
if(s[i-1][j]!='#'&&s[i+1][j]!='#'&&s[i][j-1]!='#'&&s[i][j+1]!='#')
{
puts("No");
return 0;
}
}
puts("Yes");
return 0;
} | 0 |
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
#define int int64_t
#define ii pair<int,int>
#define dd pair<double,double>
#define vi vector<int>
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define checkBit(v, p) (v&(1LL << p))
#define loop(i,a,b) for(int i = a;i < int(b);i++)
#define x first
#define y second
#define chkmin(a,b) (a) = min((a), (b))
#define chkmax(a,b) (a) = max((a), (b))
using namespace std;
int inf = 1e9, mod = 1e9;
vi ps;
inline int sum(int l, int r)
{
return ps[r + 1] - ps[l];
}
inline int diff(int l, int mid, int r)
{
return abs(sum(mid, r) - sum(l, mid - 1));
}
int32_t main()
{
ios_base::sync_with_stdio(false); cout << fixed <<setprecision(0);
int n; cin >> n;
ps.resize(n + 1);
ps[0] = 0;
vi arr(n);
for (int i=0;i<n;i++) cin >> arr[i], ps[i + 1] = ps[i] + arr[i];
vi l(n, -1),r(n, -1);
l[1] = 1;
r[n - 2] = n - 1;
for(int i = 2,ind = 1, s; i < n;i++)
{
s = diff(0, ind, i);
while(ind <= i && s > diff(0, ind + 1, i)) s = diff(0, ++ind, i);
l[i] = ind;
}
for(int i = n - 3,ind = n - 1, s; i >= 0;i--)
{
s = diff(i, ind, n - 1);
while(ind > i && s > diff(i, ind - 1, n - 1)) s = diff(i, --ind, n - 1);
r[i] = ind;
}
int mini = 2e18;
for(int i = 2; i < n - 1;i++)
{
vi vals(4);
vals[0] = sum(0, l[i - 1] - 1);
vals[1] = sum(l[i - 1], i - 1);
vals[2] = sum(i, r[i] - 1);
vals[3] = sum(r[i], n - 1);
mini = min(mini, *max_element(all(vals)) - *min_element(all(vals)));
}
cout << mini << endl;
return 0;
}
| #include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
#include<bitset>
#include<deque>
#include<functional>
#include<iterator>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<utility>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
#define a first
#define b second
#define sz(x) (ll)((x).size())
#define pb push_back
#define mp make_pair
#define bg begin()
#define ed end()
#define all(x) (x).bg,(x).ed
#define rep(i,n) for(ll i=0;i<(n);i++)
#define rep1(i,n) for(ll i=1;i<=(n);i++)
#define rrep(i,n) for(ll i=(n)-1;i>=0;i--)
#define rrep1(i,n) for(ll i=(n);i>=1;i--)
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
const ll MOD=1000000007;
const ll INF=1000000000000000;
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 maxx(ll x,ll y,ll z){return max(max(x,y),z);}
ll minn(ll x,ll y,ll z){return min(min(x,y),z);}
ll gcd(ll x,ll y){if(x%y==0) return y;else return gcd(y,x%y);}
ll lcm(ll x,ll y){return x*(y/gcd(x,y));}
ll digsz(ll x){if(x==0) return 1;else{ll ans=0;while(x){x/=10;ans++;}return ans;}}
ll digsum(ll x){ll sum=0;while(x){sum+=x%10;x/=10;}return sum;}
vector<ll> pw2(62,1);vector<ll> pw10(19,1);
int main(){
{rep1(i,61) pw2[i]=2*pw2[i-1];}
{rep1(i,18) pw10[i]=10*pw10[i-1];}
ll N; cin>>N;
ll ans=INF;
string s; cin>>s;
vector<ll> L(sz(s),0);
vector<ll> R(sz(s),0);
// 0 1 2 3 ... sz(s)-1
rep1(i,sz(s)-1){
L[i]=L[i-1];
if(s[i-1]=='W') L[i]++;
}
rrep(i,sz(s)-1){
R[i]=R[i+1];
if(s[i+1]=='E') R[i]++;
}
rep(i,sz(s)){
chmin(ans,L[i]+R[i]);
}
cout<<ans<<endl;
}
| 0 |
Subsets and Splits