code_file1
stringlengths 80
4k
| code_file2
stringlengths 91
4k
| similar_or_different
int64 0
1
|
---|---|---|
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0;i<n;i++)
#define rep2(i,a,n) for(int i=a;i<n;i++)
#define been(ix) (ix).begin(),(ix).end()
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vl;
const ll INFL = 1e18;
const int INF = 1001001001;
typedef pair<int,int> P;
#define foreach(ix,i) for(auto& (ix) : (i))
typedef long double ld;
const int mod =1000000007;
const double PI = acos(-1);
vector<P> edge[100005];
vl seen(100005,INFL);
void dfs(int n,ll c,int m){
for(auto x:edge[n]){
if(x.first==m)continue;
seen[x.first]=min(seen[x.first],c+x.second);
dfs(x.first,seen[x.first],n);
}
}
int main(){
int n;cin>>n;
rep(i,n-1){
int a,b;cin>>a>>b;
a--;b--;
int c;cin>>c;
edge[a].push_back({b,c});
edge[b].push_back({a,c});
}
int q,k;cin>>q>>k;
k--;
dfs(k,0,-1);
rep(i,q){
int a,b;
cin>>a>>b;
a--;b--;
cout<<seen[a]+seen[b]<<endl;
}
} | #include <bits/stdc++.h>
using namespace std;
//#define INF 1145141919810893364
#define REP(i,n) for(int i=0;i<(n);++i)
std::vector<int> bipartite(std::vector<std::pair<int,int>> edge){
int M = edge.size();
if(M==0)return {};
int N = 0;
for(int i=0;i<M;i++){
N=std::max(N,edge[i].first+1);
N=std::max(N,edge[i].second+1);
}
std::vector<int> R(N,-1);
std::vector<int> S(0);
std::vector<std::vector<int>> es(N,std::vector<int>(0));
for(int i=0;i<M;i++){
es[edge[i].first].push_back(edge[i].second);
es[edge[i].second].push_back(edge[i].first);
}
std::queue<int> Q;
std::vector<bool> visited(N,false);
for(int j=0;j<N;j++){
if(visited[j])continue;
Q.push(j);
R[j]=0;
visited[j]=true;
while(!Q.empty()){
int q=Q.front();
Q.pop();
visited[q]=true;
for(int i=0;i<es[q].size();i++){
if(R[es[q][i]]==R[q]){
return S;
}
if(visited[es[q][i]])continue;
Q.push(es[q][i]);
R[es[q][i]]=1-R[q];
}
}
}
return R;
}
signed main(void){
long long N,M;
int a,b;
vector<int> A,B;
cin >> N >> M;
REP(i,M){
cin >> a >> b;
a--;b--;
A.push_back(a);
B.push_back(b);
}
vector<pair<int,int>> AB;
REP(i,M){
AB.push_back({A[i],B[i]});
//cout << A[i] MM B[i] << endl;
}
vector<int> d=bipartite(AB);
//vector<int> d(N,1);
if(d.size()==0){
cout << N*(N-1)/2 - M << endl;
return 0;
}
//REP(i,N){cout << d[i] << " ";}cout << endl;
long long x=0,y=0;
REP(i,N){
if(d[i]){
x++;
}else{
y++;
}
}
cout << x*y - M << endl;
return 0;
}
| 0 |
#define _GLIBCXX_DEBUG
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
typedef long double ld;
#define vec(s) vector<s>
#define vvec(s) vector<vector<s>>
typedef vector<lli> vi;
typedef vector<vi> vvi;
typedef pair<lli,lli> pii;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define Yes(x) cout<<(x?"Yes":"No")<<endl;
#define YES(x) cout<<(x?"YES":"NO")<<endl;
#define out(s) cout<<s<<endl;
#define pb(s) push_back(s);
#define sp " "
#define INF 10000000000
#define LINF 9000000000000000000
#define all(s) s.begin(),s.end()
void vout(vi v){
for(lli i=0;i<v.size();i++)
cout<<v.at(i)<<endl;
}
int main(){
cout << std::fixed << std::setprecision(10);
lli a, b;
cin>>a>>b;
lli ans=0;
rep(i,2){
if(a>b){
ans+=a;
a--;
}
else{
ans+=b;
b--;
}
}
out(ans);
} | #include <bits/stdc++.h>
using namespace std;
signed main(){
int A, B;
scanf("%d%d", &A, &B);
printf("%d\n", (A == B) ? (A + B) : (2 * max(A, B) - 1));
return 0;
} | 1 |
#include<bits/stdc++.h>
using namespace std;
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout<<fixed; cout<<setprecision(12);
#define randomINT mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
#define newl cout<<"\n"
#define DISP(as) for(auto it : as) cout<<it<<" ";newl;
#define all(x) (x).begin(),(x).end()
#define mset(x,val) memset(x,val,sizeof(x))
#define newl cout<<"\n"
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define dline cerr<<"///REACHED///\n";
#define deb1(x) cerr<<#x<<" = "<<x<<'\n';
#define deb2(x,y) cerr<<'['<<#x<<','<<#y<<"] = "<<'['<<x<<','<<y<<']'<<'\n';
#define deb3(x,y,z) cerr<<'['<<#x<<','<<#y<<','<<#z<<"] = "<<'['<<x<<','<<y<<','<<z<<']'<<'\n';
typedef long long ll;
typedef long double ld;
typedef vector<ll> vll;
typedef pair<ll ,ll> pll;
typedef pair<ld,ld> pld;
typedef unordered_map<ll,ll> um;
typedef vector<pll> vpll;
const ll MAX5 = 1e+5+7;
const ll MAX7 = 1e+7+7;
const ll MAXN = MAX7;
const ll INF = 0x7f7f7f7f7f7f7f7f;
const int INFi = 0x7f7f7f7f;
const ll MOD = 1e+9+7;
const ll N = 2*1e6;
vpll adj[MAXN];ll visit[MAXN]={};
// <------------- Declare Variables Here ------------> //
ll T=1;
ll n,x,y,m;
string s,t;
vll a;
// <------------- Implement Functions Here ----------> //
// <------------- Start of main() -------------------> //
void MAIN() {
cin>>n;
for(ll i=0;i<n;i++) {
cin>>m;
for(ll j=0;j<m;j++) {
cin>>x>>y;
x--;
adj[i].pb({x,y});
}
}
ll maxi = 0;
for(ll k=1;k<pow(2,n);k++) {
bool mark = true;
for(ll i=0;i<n;i++) {
if((k & (1<<i))) {
for(ll j=0;j<adj[i].size();j++) {
if(adj[i][j].s == 1) {
if((k & (1<<adj[i][j].f)) == 0)
mark = false;
}
else {
if((k & (1<<adj[i][j].f)))
mark = false;
}
if(mark == false) break;
}
}
if(mark == false) break;
}
if(mark) {
maxi = max(maxi,(ll) __builtin_popcount(k));
}
}
cout<<maxi;newl;
}
int main() {
fastio;randomINT;
//cin>>T;
while(T--) {
MAIN();
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;++i)
#define per(i,n) for(int i=n-1;i>=0;--i)
#define sc1(a) scanf("%d",&a)
#define sc2(a,b) scanf("%d %d",&a,&b)
#define sc3(a,b,c) scanf("%d %d %d",&a,&b,&c)
int main(){
int mod=1000000007;
int n,a,ans=0;
sc1(n);
int w[15][14][2];
rep(i,15) rep(j,14) rep(k,2) w[i][j][k]=0;
rep(i,n) {
sc1(a);
rep(j,a) sc2(w[i][j][0],w[i][j][1]);
}
rep(i,pow(2,n)) {
if (i==0) continue;
int x=i;
int l[15]={0};
rep(j,n) {
if (x&1) l[j]=1;
x=x>>1;
}
int f=1;
rep(j,n) {
if (l[j]) {
rep(k,14) if (w[j][k][0]>0 && l[w[j][k][0]-1]!=w[j][k][1]) f=0;
}
}
int cnt=0;
if (f) {
rep(j,n) cnt+=l[j];
ans=max(ans,cnt);
}
}
printf("%d\n",ans);
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
#define INF 1e+9
#define MAX_V 2505
struct edge {
int to;
int cost;
};
using P = pair<int, int>;
int V;
vector<edge> G[MAX_V];
int d[MAX_V];
void dijkstra(int s) {
priority_queue<P, vector<P>, greater<P> > que;
fill(d, d+V, 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 h, w;
int pos(int i, int j) { return i*w + j; }
int main() {
cin >> h >> w;
V = h * w;
string table[h];
for ( int i = 0; i < h; i++ ) { cin >> table[i]; }
int black = 0;
for ( int i = 0; i < h; i++ ) {
for ( int j = 0; j < w; j++ ) {
if ( table[i][j] == '#' ) { black++; continue; }
if ( i+1 < h ) { G[pos(i,j)].push_back({pos(i+1,j), 1}); }
if ( i-1 >= 0 ) { G[pos(i,j)].push_back({pos(i-1,j), 1}); }
if ( j+1 < w ) { G[pos(i,j)].push_back({pos(i,j+1), 1}); }
if ( j-1 >= 0 ) { G[pos(i,j)].push_back({pos(i,j-1), 1}); }
}
}
dijkstra(pos(0,0));
int ans = -1;
if ( d[pos(h-1,w-1)] != INF ) {
ans = h*w - d[pos(h-1,w-1)] - black - 1;
}
cout << ans << endl;
return 0;
}
| #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int N;
char s[4002];
int r[4002],g[4002],b[4002];
long long int ans = 0;
int main(){
scanf("%d",&N);
scanf("%s",&s[1]);
//printf("%d\n",N);
//printf("%s\n",&s[1]);
for(int i=N-1;i>0;i--){
r[i] = r[i+1];
g[i] = g[i+1];
b[i] = b[i+1];
if(s[i+1] == 'R')r[i] ++;
else if (s[i+1] == 'G') g[i]++;
else b[i]++;
}
//for(int i=1;i<=N;i++)printf("%d ",r[i]);
// printf("\n");
//for(int i=1;i<=N;i++)printf("%d ",g[i]);
// printf("\n");
// for(int i=1;i<=N;i++)printf("%d ",b[i]);
// printf("\n");
for(int i=1;i<=N-2;i++){
for(int j=i+1;j<=N-1 ; j++){
if(s[i] == s[j])continue;
int x= j-i;
if( (s[i] == 'R' && s[j] == 'G') || (s[i] == 'G' && s[j] == 'R')){
ans+=b[j];
if(x+j <=N && s[x+j] == 'B') ans--;
}
else if ((s[i] == 'R' && s[j] == 'B') || (s[i] == 'B' && s[j] == 'R')){
ans+=g[j];
if(x+j <=N && s[x+j] == 'G') ans--;
}
else {
ans+=r[j];
if(x+j<=N && s[x+j] == 'R') ans--;
}
// printf("%d %d %d\n",i,j,ans);
}
}
printf("%lld\n",ans);
return 0;
} | 0 |
//include
//------------------------------------------
#include <bits/stdc++.h>
using namespace std;
//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}
//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef unsigned long long ULL;
//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);
//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))
//debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
VI n(5, 0);
bool isStraight(){
int s = 0;
REP(i, 5) s+=n[i]-n[0]-i;
if(s == 0) return true;
else {
if(n[0] == 1) n[0]+=13;
SORT(n);
s = 0;
REP(i, 5) s+=n[i]-n[0]-i;
if(s == 0) return true;
else false;
}
return false;
}
int main(){
while(scanf("%d,%d,%d,%d,%d", &n[0], &n[1], &n[2], &n[3], &n[4])!=EOF){
int m = 0;
SORT(n);
REP(i, 4) FOR(j, i+1, 5) if(n[i]==n[j]) m++;
switch(m){
case 1:
cout << "one pair" << endl;
break;
case 2:
cout << "two pair" << endl;
break;
case 3:
cout << "three card" << endl;
break;
case 4:
cout << "full house" << endl;
break;
case 6:
cout << "four card" << endl;
break;
default:
if(isStraight()) cout << "straight" << endl;
else cout << "null" << endl;
}
}
} | #include <cstdio>
#include <algorithm>
using namespace std;
const char *solve(){
int c[20] = {};
int a;
for(int i = 0; i < 5; ++i){
if(scanf("%d,", &a) == EOF){ return 0; }
++c[a];
}
c[14] = c[1];
int mc = 0, ms = 0, p = 0;
for(int i = 1; i < 14; ++i){
mc = max(mc, c[i]);
if(c[i] > 1){ ++p; }
int s = 0;
for(int j = 0; j < 5 && c[i + j]; ++j){
++s;
}
ms = max(ms, s);
}
if(mc == 4){ return "four card"; }
if(p > 1 && mc > 2){ return "full house"; }
if(ms > 4){ return "straight"; }
if(mc > 2){ return "three card"; }
if(p > 1){ return "two pair"; }
if(p){ return "one pair"; }
return "null";
}
int main(){
const char *ans;
while(ans = solve()){
puts(ans);
}
} | 1 |
#define NguyenDangQuan the_author
#include <bits/stdc++.h>
#define all(x) x.begin(),x.end()
#define mset(x, i) memset(x,i,sizeof(x))
#define elif else if
#define heap priority_queue
#define fi first
#define se second
#define pb push_back
#define ld long double
#define ll long long
#define ull unsigned long long
#define task "codejam"
using namespace std;
int typetest;
template<class T>
void read(T &x){
register int c;
T neg = 1;
x = (T)0;
while ((c = getchar()) <= 47 || c >= 58)
if(c == '-') neg = -1;
for (; (c > 47 && c < 58); c = getchar()){
x = (x << 3) + (x << 1) + (T)(c - 48);
}
x *= neg;
}
inline void fastIOfileinput(){
ios:: sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen(task".INP", "r", stdin);
// freopen(task".OUT", "w", stdout);
// freopen(task".in", "r", stdin);
// freopen(task".out", "w", stdout);
typetest = 0;
}
const int N = 2e5 + 2;
int m, n, k;
vector<int> ker[N], kel[N];
int ltr[N], ltl[N];
void Enter(){
read(n); read(m); read(k);
while(m--){
int a, b;
read(a); read(b);
ker[a].pb(b);
ker[b].pb(a);
}
while(k--){
int a, b;
read(a); read(b);
kel[a].pb(b);
kel[b].pb(a);
}
}
void BFS(int i, int lt[], vector<int> ke[]){
++m;
lt[i] = m;
queue<int> open;
open.push(i);
while(open.size()){
int c = open.front();
open.pop();
for(auto i : ke[c]){
if((!lt[i]))
open.push(i), lt[i] = m;
}
}
}
void solve(){
m = 0;
for(int i = 1; i <= n; ++i)
if(ltr[i] == 0)
BFS(i, ltr, ker);
m = 0;
for(int i = 1; i <= n; ++i)
if(ltl[i] == 0)
BFS(i, ltl, kel);
map<int, map<int, int> > s;
for(int i = 1; i <= n; ++i){
++s[ltr[i]][ltl[i]];
// cerr << ltr[i] << " " << ltl[i] << "\n";
}
for(int i = 1; i <= n; ++i){
int& v = s[ltr[i]][ltl[i]];
cout << v << " ";
}
}
signed main(){
fastIOfileinput();
if(typetest){
int t;
cin >> t;
int v = t;
while(t--){
Enter();
cout << "Case #" << v - t << ": ";
solve();
}
}
else{
Enter();
solve();
}
}
| #include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<math.h>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef long double db;
typedef pair<int,int> pii;
const int N=100000;
const db pi=acos(-1.0);
#define lowbit(x) (x)&(-x)
#define sqr(x) (x)*(x)
#define rep(i,a,b) for (register int i=a;i<=b;i++)
#define per(i,a,b) for (register int i=a;i>=b;i--)
#define go(u,i) for (register int i=head[u];i;i=sq[i].nxt)
#define fir first
#define sec second
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define maxd 998244353
#define eps 1e-8
struct node{int to,nxt;}sq[400200];
int all=0,head[200100],ans[200100];
int n,m1,m2,cnt=0;
pii col[200100];
bool vis[200100];
map<pii,int> mp;
int read()
{
int x=0,f=1;char ch=getchar();
while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
return x*f;
}
void add(int u,int v)
{
all++;sq[all].to=v;sq[all].nxt=head[u];head[u]=all;
}
void dfs(int u,int op)
{
if (op==0) col[u].fir=cnt;else col[u].sec=cnt;
vis[u]=1;
go(u,i)
{
int v=sq[i].to;
if (!vis[v]) dfs(v,op);
}
}
void init(int m,int op)
{
all=cnt=0;
memset(head,0,sizeof(head));
memset(vis,0,sizeof(vis));
rep(i,1,m)
{
int u=read(),v=read();
add(u,v);add(v,u);
}
rep(i,1,n)
if (!vis[i]) {cnt++;dfs(i,op);}
}
int main()
{
n=read();m1=read();m2=read();
init(m1,0);init(m2,1);
rep(i,1,n) mp[col[i]]++;
rep(i,1,n) ans[i]=mp[col[i]];
rep(i,1,n) printf("%d ",ans[i]);
return 0;
}
| 1 |
#include <cstdint>
#include <iostream>
#include <vector>
using namespace std;
using s64 = int64_t;
constexpr s64 INF = 1'010'000'000'000'000'000LL;
template<typename T, typename U>
bool chmin(T& xmin, const U& x)
{
if(x < xmin) {
xmin = x;
return true;
}
else {
return false;
}
}
#define FOR(i, start, end) for(s64 i = (start); i < (end); ++i)
#define REP(i, n) FOR(i, 0, n)
s64 N;
s64 M;
vector<vector<s64>> G;
void print_g() {
REP(i, N) {
REP(j, N) {
s64 c = G[i][j];
if(c == INF)
cout << "INF";
else
cout << c;
cout << (j==N-1 ? '\n' : ' ');
}
}
}
void floyd() {
REP(k, N) {
REP(i, N) {
if(G[i][k] == INF) continue;
REP(j, N) {
if(G[k][j] == INF) continue;
chmin(G[i][j], G[i][k] + G[k][j]);
}
}
}
}
void solve() {
floyd();
REP(i, N) {
if(G[i][i] < 0) {
cout << "NEGATIVE CYCLE\n";
return;
}
}
print_g();
}
int main() {
cin >> N;
G.assign(N, vector<s64>(N, INF));
REP(i, N) { G[i][i] = 0; }
cin >> M;
REP(_, M) {
s64 a, b, c; cin >> a >> b >> c;
G[a][b] = c;
}
solve();
return 0;
} | /**
* All Pairs Shortest Paths (全点間最短パス)
*
* O(|E||V| + |V|^2 log|V|)
*/
/**
* Feasible Potential
*
* O(|E||V|)
*/
#include <vector>
#include <algorithm>
using namespace std;
using Int = long long;
struct edge{
int to;
Int cost;
};
vector<Int> Potential(const vector<vector<edge>>& G){
vector<Int> dist(G.size(),0);
for(int i = 1;i < G.size();i++){
for(int v = 0;v < G.size();v++){
for(auto & e : G[v]){
dist[e.to] = min(dist[e.to],dist[v] + e.cost);
}
}
}
for (int v = 0; v < G.size(); v++) {
for (auto &e : G[v]) {
if(dist[e.to] > dist[v] + e.cost){
return vector<Int>();
}
}
}
return dist;
}
/**
* Potential Dijkstra
*
* O(|E| + |V|log|V|)
*/
#include <functional>
#include <queue>
#include <set>
#include <vector>
using namespace std;
Int INF = 1LL << 40;
using Pii = pair<Int, int>;
vector<Int> Potential_Dijkstra(const vector<vector<edge>> &G, const vector<Int> &pot,int s) {
vector<Int> dist(G.size(), INF);
dist[s] = 0;
priority_queue<Pii, vector<Pii>, greater<Pii>> que;
que.emplace(dist[s], s);
while (!que.empty()) {
auto d = que.top().first;
auto v = que.top().second;
que.pop();
if (dist[v] < d)
continue;
for (auto &e : G[v]) {
Int co = e.cost + pot[v] - pot[e.to];
if (dist[e.to] > d + co) {
dist[e.to] = d + co;
que.emplace(dist[e.to], e.to);
}
}
}
return dist;
}
vector<vector<Int>> AllPairsShortestPaths(const vector<vector<edge>> &G){
vector<vector<Int>> res(G.size());
auto pot = Potential(G);
if(pot.empty()) return vector<vector<Int>>();
for(int s = 0;s < G.size();s++){
res[s] = Potential_Dijkstra(G,pot,s);
for(int t = 0;t < G.size();t++){
if(res[s][t] == INF) continue;
res[s][t] += pot[t] - pot[s];
}
}
return res;
}
#include <iostream>
int main(){
int v,e;
cin >> v >> e;
vector<vector<edge>> G(v);
for(int i = 0;i < e;i++){
int s,t;
Int d;
cin >> s >> t >> d;
G[s].push_back({t,d});
}
auto res = AllPairsShortestPaths(G);
if(res.empty()){
cout << "NEGATIVE CYCLE" << endl;
return 0;
}
for(auto & d : res){
for(int i = 0;i < G.size();i++){
if(d[i] == INF) cout << "INF";
else cout << d[i];
cout << " \n"[i + 1 == G.size()];
}
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1000000007;
//const ll MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
const double pi = acos(-1.0);
const double EPS = 1e-10;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};
int N;
int A[100000];
int res(){
int K = 0;
rep(i, N){
if(A[i]%2 == 0) K++;
}
if(K&1) return 1;
if(K != N-1) return 0;
int g = A[0]&~1;
rep(i, N){
if(A[i] == 1) return 0;
if(A[i]%2 == 1) A[i]--;
g = gcd(g, A[i]);
}
rep(i, N) A[i] /= g;
return res()^1;
}
int main(){
cin >> N;
rep(i, N) cin >> A[i];
cout << (res()? "First" : "Second") << endl;
} | #define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <unordered_set>
#include <unordered_map>
#include <array>
#include <cassert>
#include <bitset>
using namespace std;
using LL = long long;
LL N;
vector<LL>A;
vector<LL>xmos;
const LL MOD = 1000000007;
LL solvedp(vector<pair<LL, int>>& vec, LL lasval) {
LL zero = 1, notz = 0;
LL zpar = 1, npar = 0;
for (auto p : vec) {
if (p.first == 0) {
zpar = (notz * p.second) % MOD;
zero += zpar;
zero %= MOD;
}
else {
npar = (zero * p.second) % MOD;
notz += npar;
notz %= MOD;
}
}
if (lasval == 0) {
return zpar;
}
return npar;
}
int main() {
cin >> N;
LL xsum = 0;
xmos.push_back(0);
for (int i = 0; i < N; ++i) {
LL tmp;
cin >> tmp;
A.push_back(tmp);
xsum ^= tmp;
xmos.push_back(xsum);
}
/*vector<LL>dp(N + 1);
dp[1] = 1;
LL odd = 1, eve = 0;
for (int i = 2; i <= N; ++i) {
if (i % 2) {
dp[i]++;
dp[i] += eve;
dp[i] %= MOD;
odd += dp[i];
odd %= MOD;
}
else {
dp[i] += odd;
eve += dp[i];
eve %= MOD;
}
}*/
if (xsum != 0) {
vector<pair<LL, int>>nowarr;
for (int i = 1; i <= N; ++i) {
if (xmos[i] == 0) {
nowarr.push_back({ 0,1 });
}
if (xmos[i] == xsum) {
nowarr.push_back({ xsum,1 });
}
}
LL ans = solvedp(nowarr, xsum);
cout << ans << endl;
}
else {
map<LL, int>zerolas;
map<LL, vector<pair<LL, int>>>arrs;
int zerocnt = 0;
LL zerotwo = 1;
for (int i = 1; i <= N; ++i) {
if (xmos[i] == 0) {
zerocnt++;
if (i < N) {
zerotwo *= 2;
zerotwo %= MOD;
}
}
else {
if (zerocnt != zerolas[xmos[i]]) {
arrs[xmos[i]].push_back({ 0,zerocnt - zerolas[xmos[i]] });
zerolas[xmos[i]] = zerocnt;
}
arrs[xmos[i]].push_back({ xmos[i],1 });
}
}
LL ans = zerotwo;
for (auto& elm : arrs) {
if (elm.second.back().second != 0) {
elm.second.push_back({ 0,1 });
}
ans += solvedp(elm.second, 0);
ans %= MOD;
}
cout << ans << endl;
}
return 0;
}
| 0 |
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main(){
int N,Q,M,Date[101],i,l,k;
while(0<=scanf("%d%d",&N,&Q)){
for(i=0;i<101;i++){
Date[i]=0;
}
if((N==0) && (Q==0)){
break;
}
for(k=0;k<N;k++){
scanf("%d",&M);
if(M==0){
continue;
}else{
for(i=0;i<M;i++){
scanf("%d",&l);
++Date[l];
}
}
}
l=0;
i=0;
for(k=0;k<100;k++){
if(i<Date[k] && Date[k]>=Q){
i=Date[k];
l=k;
}
}
printf("%d\n",l);
}
return 0;
} | #include <bits/stdc++.h>
#include<cmath>
using namespace std ;
#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)
#define vvl(v,w,h,init) vector<vector<ll>> v(w,vector<ll>(h,init));
#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))
using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;
const int mod = 1000000007;
constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100000000;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0,1,-1,1,-1};
const int dx[] = {0,-1,0,1,1,-1,-1,1};
vector<string> Split(char c,string s){
vector<string> res(0);
string str = "";
rep(i,s.size()){
if(s[i] == c){
res.push_back(str);
str.clear();
}
else{
str += s[i];
}
}
res.push_back(str);
return res;
}
template<typename T>
T ston(string& str, T n){
istringstream sin(str) ;
T num ;
sin >> num ;
return num ;
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
ll n,m;
while(cin >> n >> m && n && m){
vector<ll> v(101);
ll k,tmp;
rep(i,n){
cin >> k;
rep(j,k){
cin >> tmp;
v[tmp]++;
}
}
ll ans = 0,cnt = m-1;
rep(i,101){
if(v[i] >= cnt){
if(v[i] > cnt)
ans = i;
cnt = v[i];
}
}
if(ans) cout << ans << endl;
else cout << 0 << endl;
}
return 0;
} | 1 |
#include <cstdio>
#include <cstring>
#include <utility>
using namespace std;
int main() {
char str[21];
scanf("%s",str);
int length = strlen(str);
for(int i=0;i<length/2;++i) {
swap(str[i],str[(length-1)-i]);
}
puts(str);
return 0;
} | #include<iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define what_is(x) cerr << #x << " is " << x << endl;
#define MT make_tuple
#define eb emplace_back
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define REP(i,a,b) for (int i = a; i <= b; i++)
#define FOR(i,n) for (int i=0;i < n ; i++)
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << endl;
err(++it, args...);
}
int main(){
ll n;
ll i;
cin >> n;
i=0;
while((i+1)*(i+1)<=n) i++;
cout << i*i;
return 0;
} | 0 |
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
int main(){
int n,h,w; scanf("%d%d%d",&n,&h,&w);
printf("%d\n",(n-h+1)*(n-w+1));
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int main() {
int64_t n,w,h;
cin>>n>>h>>w;
int cnt=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(n-i+1>=h&&n-j+1>=w)
cnt++;
}
}
cout<<cnt<<endl;
return 0;
} | 1 |
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n,q;
vector<int> S,T;
cin >> n;
for(int i=0;i<n;i++) {
int s;
cin >> s;
S.push_back(s);
}
cin >> q;
for(int i=0;i<q;i++) {
int t;
cin >> t;
T.push_back(t);
}
// O(n^2)
int C = 0;
for(int t_itr=0;t_itr<q;t_itr++) {
int key = T[t_itr];
for(int s_itr=0;s_itr<n;s_itr++) {
if(S[s_itr] == key) {
C++;
break;
}
}
}
cout << C << endl;
} | #pragma region
#define _USE_MATH_DEFINES
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <list>
#include <iomanip>
#include <cstdint>
#include <bitset>
using namespace std;
typedef long long ll;
//#define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i))
#define rep(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define rrep(i, s) for (int(i) = (s) - 1; (i) >= 0; --(i))
#define all(x) x.begin(),x.end()
#pragma region UnionFind
struct UnionFind
{
vector<int> par;
UnionFind(int n) : par(n, -1) {}
void init(int n) { par.assign(n, -1); }
int root(int x)
{
if (par[x] < 0) return x;
else return par[x] = root(par[x]);
}
bool issame(int x, int y)
{
return root(x) == root(y);
}
bool merge(int x, int y)
{
x = root(x); y = root(y);
if (x == y) return false;
if (par[x] > par[y]) swap(x, y);
par[x] += par[y];
par[y] = x;
return true;
}
int size(int x)
{
return -par[root(x)];
}
};
#pragma endregion
#pragma region GCD
int gcd(int a, int b)
{
if (a%b == 0)return b;
return gcd(b, a%b);
}
#pragma endregion
#pragma region chmin
template<class T> inline bool chmin(T& a, T b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
#pragma endregion
#pragma region chmax
template<class T> inline bool chmax(T& a, T b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
#pragma endregion
#pragma endregion
int main()
{
int n; cin >> n;
vector<ll> a(n);
map<ll, int> mp;
rep(i, n)
{
cin >> a[i];
++mp[a[i]];
}
sort(all(a), greater<ll>());
int res = 0;
rep(i, n)
{
if (!mp[a[i]])continue;
--mp[a[i]];
ll tmp = 1;
while (tmp <= a[i])tmp = tmp << 1;
if (mp[tmp - a[i]])
{
--mp[tmp - a[i]];
++res;
}
}
cout << res << endl;
} | 0 |
#include<bits/stdc++.h>
#define re register
#define max(a,b) ((a)>(b)?(a):(b))
const int maxn=305;
char S[maxn];
int dp[2][maxn][maxn],n,m,o,ans;
int main() {
scanf("%s",S+1);n=strlen(S+1);scanf("%d",&m);
for(re int p=0;p<=m;++p,o^=1) {
for(re int i=1;i<=n;i++) dp[o^1][i][i]=1;
for(re int l=2;l<=n;++l)
for(re int i=1,j=i+l-1;j<=n;++i,++j) {
dp[o^1][i][j]=max(dp[o^1][i][j-1],dp[o^1][i+1][j]);
if(S[i]==S[j]) dp[o^1][i][j]=(dp[o^1][i][j],dp[o^1][i+1][j-1]+2);
if(p) dp[o^1][i][j]=max(dp[o^1][i][j],dp[o][i+1][j-1]+2);
}
ans=max(ans,dp[o^1][1][n]);
}
printf("%d\n",ans);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const int MAXN = 305;
int dp[MAXN][MAXN][MAXN];
int main()
{
string s;
cin >> s;
int k;
cin >> k;
for(int sz = 1; sz <= s.size(); sz++){
for(int l = 0; l < s.size() - sz + 1; l++){
int r = l + sz - 1;
for(int j = 0; j <= k; j++){
if(r == l){
dp[l][r][j] = 1;
continue;
}
if(r - l == 1){
if(j == 0){
if(s[l] == s[r]){
dp[l][r][j] = 2;
}
else{
dp[l][r][j] = 1;
}
}
else{
dp[l][r][j] = 2;
}
continue;
}
dp[l][r][j] = max(dp[l + 1][r][j], dp[l][r - 1][j]);
if(s[l] == s[r]){
dp[l][r][j] = max(dp[l][r][j], dp[l + 1][r - 1][j] + 2);
}
else if(j != 0){
dp[l][r][j] = max(dp[l][r][j], dp[l + 1][r - 1][j - 1] + 2);
}
}
}
}
cout << dp[0][s.size() - 1][k];
}
| 1 |
#include <iostream>
#include <algorithm>
using namespace std;
static const int SIZE = 101;
static const long long int INF = 1e18;
long long int d[SIZE][SIZE] = { 0 };
void solve()
{
for (int i = 0; i < SIZE; ++i)
{
for (int j = 0; j < SIZE; ++j)
{
d[i][j] = INF;
}
d[i][i] = 0;
}
int V, E;
cin >> V >> E;
for (int i = 0; i < E; ++i)
{
int from, to, cost;
cin >> from >> to >> cost;
d[from][to] = cost;
}
for (int k = 0; k < V; ++k)
{
for (int i = 0; i < V; ++i)
{
for (int j = 0; j < V; ++j)
{
if (d[i][k] != INF && d[k][j] != INF)
{
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
}
bool isNegative = false;
for (int i = 0; i < V; ++i)
{
if (d[i][i] < 0)
{
cout << "NEGATIVE CYCLE" << endl;
return;
}
}
for (int i = 0; i < V; ++i)
{
for (int j = 0; j < V; ++j)
{
if (j)
{
cout << " ";
}
if (d[i][j] == INF)
{
cout << "INF";
}
else
{
cout << d[i][j];
}
}
cout << endl;
}
}
int main()
{
solve();
return(0);
} | #include <stdio.h>
#include <limits.h>
int main(){
int n,m,s,i,j;
scanf("%d%d",&n,&m);
int e[m+1][3],dist[m+1],update=1,k,d[200][200];
//printf("xxx");
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(i==j)d[i][j]=0;
else d[i][j]=INT_MAX;
}
}
for(i=0;i<m;i++){
for(j=0;j<3;j++){
scanf("%d",&e[i][j]);
}
d[e[i][0]][e[i][1]]=e[i][2];
}
for(i=0;i<n;i++){
dist[i]=INT_MAX;
}
dist[s]=0;
for(i=0;i<n-1;i++){
update=0;
for(j=0;j<m;j++){
if(dist[e[j][0]]!=INT_MAX&&dist[e[j][1]]>dist[e[j][0]]+e[j][2]){
dist[e[j][1]]=dist[e[j][0]]+e[j][2];
update=1;
}
}
if(update==0)break;
}
for(j=0;j<m;j++){
if(dist[e[j][0]]!=INT_MAX&&dist[e[j][1]]>dist[e[j][0]]+e[j][2]){
printf("NEGATIVE CYCLE\n");
return 0;
}
}
for(k=0;k<n;k++){
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if((d[i][k]!=INT_MAX&&d[k][j]!=INT_MAX)&&d[i][j]>d[i][k]+d[k][j]){
d[i][j]=d[i][k]+d[k][j];
}
}
}
}
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(j>0)printf(" ");
if(d[i][j]==INT_MAX){
printf("INF");
}
else{
printf("%d",d[i][j]);
}
}
printf("\n");
}
return 0;
} | 1 |
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv){
int **array;
int n;
while(1){
scanf("%d", &n);
if(n == 0){
break;
}
array = (int **)calloc(n+1, sizeof(int));
for(int i = 0; i <= n; i++){
array[i] = (int *)calloc(n+1, sizeof(int));
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
scanf("%d", &array[i][j]);
array[i][n] += array[i][j];
}
}
for(int i = 0; i <= n; i++){
for(int j = 0; j < n; j++){
array[n][i] += array[j][i];
}
array[n][n] += array[n][i];
}
array[n][n] /= 4;
for(int i = 0; i <= n; i++){
for(int j = 0; j <= n; j++){
printf("%5d", array[i][j]);
}
printf("\n");
}
for(int i = 0; i <= n; i++){
free(array[i]);
}
free(array);
}
return 0;
} | #include <cstdio>
#include <iostream>
#include <string>
using namespace std;
int main() {
int n = 0, tmp0 = 0, tmp1 = 0;
int tmp2 = 0, tmp3, out1 = 0, out2 = 0;
int a[100];
scanf("%d", &n);
while (n) {
for (tmp3=0;tmp3<100;tmp3++) a[tmp3] = 0;
tmp0 = 0, out2 = 0;
while (tmp0 < n) {
tmp1 = 0, out1 = 0;
while (tmp1 < n) {
scanf("%d", &tmp2);
a[tmp1] += tmp2;
printf("%5d", tmp2);
out1 += tmp2;
tmp1++;
}
printf("%5d\n", out1);
a[tmp1] += out1;
tmp0++;
}
tmp0 = 0;
while (tmp0 < n) {
printf("%5d", a[tmp0]);
tmp0++;
}
printf("%5d\n", a[tmp0]);
scanf("%d", &n);
}
return 0;
} | 1 |
#include <iostream>
#include <algorithm>
#define flush fflush(stdout)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pl;
const int mod = (int)1e9 + 7, INF = (int)1e9;
const int di[4] = { 1,0,-1,0 }, dj[4] = { 0,1,0,-1 };
int main(void) {
int l, r, i, j, ans;
scanf("%d%d", &l, &r);
if (r - l >= 2019 || l % 2019 > r % 2019)
{
printf("0\n");
return (0);
}
ans = 2019;
i = l % 2019;
while (i < r % 2019)
{
j = i + 1;
while (j <= r % 2019)
{
if (ans > i * j % 2019)
ans = i * j % 2019;
j++;
}
i++;
}
printf("%d\n", ans);
return 0;
} | /******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <bits/stdc++.h>
using namespace std;
long long remindersReps[2019];
int main()
{
int L, R;
cin >> L >> R;
for(int i = 0; i < 2019; ++i)
{
remindersReps[i] = -1;
}
int el_lim = L +2020;
if(R < el_lim)
el_lim = R;
for(int i = L; i <= el_lim ; ++i )
{
remindersReps[i%2019] = i;
}
long long reminders[2019];
for(int i = 0; i< 2019;++i)
{
if(remindersReps[i]!=1)
reminders[i] = remindersReps[i]%2019;
}
long long min = 3000;
long long respL, respR;
for(int i = 0; i < 2019; ++i)
{
for(int j = 0; j < 2019; ++j)
{
if(remindersReps[i] >= remindersReps[j] || remindersReps[i] == -1 || remindersReps[j] == -1)
continue;
long long temp = (reminders[i]*reminders[j])%2019;
if( temp < min )
{
min = temp;
respL = remindersReps[i];
respR = remindersReps[j];
}
}
}
cout << min;
}
| 1 |
#include<bits/stdc++.h>
#define ll long long
#define re register
#define ull unsigned ll
using namespace std;
inline int read(){
int s=0,t=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')t=-1;ch=getchar();}
while(ch>='0'&&ch<='9')s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
return s*t;
}
const int N=41;
const int M=(1<<17)+5;
const int P=1e9+7;
int n,A,B,C,D,U,tr[M][11],f[N][M];
int Mod(int x){return x>=P?x-P:x;}
int main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
n=read(),A=read(),B=A+read(),C=B+read(),U=(1<<C);
for(int S=1;S<=U;S++)
for(int i=1;i<=10;i++){
tr[S][i]=1;
for(int j=0;j<=C;j++)if(S>>j&1){
if(j<A&&j+i>A)continue;
if(j<B&&j+i>B)continue;
if(j+i<=C)tr[S][i]|=1<<(j+i);
}if((S==U)||(tr[S][i]>=U))tr[S][i]=U;
}f[0][1]=1;
for(int i=1;i<=n;i++)
for(int S=1;S<=U;S++)
for(int j=1;j<=10;j++)f[i][tr[S][j]]=Mod(f[i][tr[S][j]]+f[i-1][S]);
printf("%d",f[n][U]);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using Int = int_fast64_t;
constexpr Int mod = 1e9+7;
Int n, x, y, z;
vector<vector<Int>> dp;
bool ok(Int w){
Int c = 0;
for(Int j=0; (w>>j)>0; ++j)
if((w>>j) & 1){
if(j+1 == z) c += 4;
if(j+1 == z+y) c += 2;
if(j+1 == z+y+x) c += 1;
}
return c != 7;
}
int main(){
// cin.tie(0);
// ios::sync_with_stdio(false);
cin >> n >> x >> y >> z;
dp.resize(n, vector<Int>(1<<(x+y+z)));
for(Int i=1; i<=10; ++i){
Int t = (1<<(i-1));
t &= (1<<(x+y+z))-1;
++dp[0][t];
}
for(Int i=0; i<n-1; ++i)
for(Int j=0; j<(1<<(x+y+z)); ++j)
for(Int k=1; k<=10; ++k){
// cout << i << " " << j << " " << k << "\n";
Int t = j;
t <<= k;
t |= (1<<(k-1));
t &= (1<<(x+y+z))-1;
if(ok(t)){
dp[i+1][t] += dp[i][j];
dp[i+1][t] %= mod;
}
}
Int ans = 1;
for(Int i=0; i<n; ++i){
ans *= 10;
ans %= mod;
}
for(Int i=0; i<(1<<(x+y+z)); ++i){
ans -= dp[n-1][i];
if(ans < 0) ans += mod;
}
cout << ans << "\n";
} | 1 |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for (int i = (a); i <= (b); ++ i)
#define rrp(i,a,b) for (int i = (a); i >= (b); -- i)
#define gc() getchar()
#define fir first
#define sec second
typedef pair<int,int> pii;
typedef double db;
typedef long long ll;
template <typename tp>
inline void read(tp& x) {
x = 0; char tmp; bool key = 0;
for (tmp = gc(); !isdigit(tmp); tmp = gc())
key = (tmp == '-');
for (; isdigit(tmp); tmp = gc())
x = (x << 3) + (x << 1) + (tmp ^ '0');
if (key) x = -x;
}
template <typename tp>
inline void ckmn(tp& x,tp y) {
x = x < y ? x : y;
}
template <typename tp>
inline void ckmx(tp& x,tp y) {
x = x < y ? y : x;
}
const int N = 100010;
int n, s, x[N], m;
ll p[N];
pair<int,ll> solve(int l,int r) {
if (l > m) return make_pair(x[r], x[r] - s);
if (r <= m) return make_pair(x[l], s - x[l]);
if (p[r] <= p[l]) {
p[l] += p[r];
pair<int,ll> tmp = solve(l, r-1);
return make_pair(x[r], tmp.sec + x[r] - tmp.fir);
} else {
p[r] += p[l];
pair<int,ll> tmp = solve(l+1, r);
return make_pair(x[l], tmp.sec + tmp.fir - x[l]);
}
}
int main() {
read(n), read(s);
rep (i, 1, n)
read(x[i]), read(p[i]);
m = 0;
while (x[m+1] < s && m < n) ++ m;
cout << solve(1, n).sec << endl;
return 0;
}
| #include <iostream>
#include <algorithm>
#include <utility>
#include <fstream>
#include <iomanip>
#include <complex>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#define IO ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
typedef long long ll;
typedef long double ld;
const ll INF = 1e18;
const int N = 3e2 + 5, inf = 1e9, mod = 998244353;
using namespace std;
int n, a, freq[4] = {};
ld dp[N][N][N] = {};
bool vis[N][N][N] = {};
ld solve ( int c1 , int c2 , int c3 )
{
if ( c1 + c2 + c3 == 0 )
return 0;
if ( vis[c1][c2][c3] )
return dp[c1][c2][c3];
vis[c1][c2][c3] = true;
ld ret = 1.0, c0 = n - ( c1 + c2 + c3 );
if ( c1 )
ret += solve ( c1 - 1 , c2 , c3 ) * ( 1.0 * c1 / n );
if ( c2 )
ret += solve ( c1 + 1 , c2 - 1 , c3 ) * ( 1.0 * c2 / n );
if ( c3 )
ret += solve ( c1 , c2 + 1 , c3 - 1 ) * ( 1.0 * c3 / n );
ret /= ( 1 - ( c0 / n ) );
return dp[c1][c2][c3] = ret;
}
int main()
{
IO
cin >> n;
for(int i = 0; i < n; i++)
{
cin >> a;
freq[a]++;
}
cout << fixed << setprecision(10) << solve( freq[1] , freq[2] , freq[3] ) << '\n';
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[])
{
int a[3];
for (int i = 0; i < 3; ++i)
{
cin >> a[i];
}
sort(a, a + 3);
cout << a[0] + a[1];
return 0;
} | #include <iostream>
using namespace std;
int main(){
string ss;
int num[128];
num['M'] = 1000;
num['D'] = 500;
num['C'] = 100;
num['L'] = 50;
num['X'] = 10;
num['V'] = 5;
num['I'] = 1;
while( cin >> ss ){
int answer = 0;
string::iterator it = ss.begin();
while( it != ss.end() ){
if( it+1 == ss.end() || num[ *it ] >= num[ *(it+1) ] ){
answer += num[ *it ];
}else{
answer += num[ *(it+1) ] - num[ *it ];
it++;
}
it++;
}
cout << answer << endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<vector<int>> Graph;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
#define pi 3.14159265359
#define inf 2147483647
#define INF 9223372036854775807
#define mod 1000000007
#define mod2 998244353
int main() {
int N; cin >> N;
vector<ll> A(N);
map<ll, ll> M;
for(int i = 0; i < N; i++) {
cin >> A[i];
if(!M.count(A[i])) M[A[i]] = 1;
else M[A[i]]++;
}
ll Max = 0, preMax = 0;
for(auto p: M) {
if(p.second >= 4) {
if(p.first > Max) {
preMax = p.first;
Max = p.first;
}
else if(p.first > preMax) {
preMax = p.first;
}
}
else if(p.second >= 2) {
if(p.first > Max) {
preMax = Max;
Max = p.first;
}
else if(p.first > preMax) {
preMax = p.first;
}
}
}
ll ans = Max * preMax;
cout << ans << endl;
return 0;
} | #pragma GCC optimize(2)
#include<bits/stdc++.h>
#define ll long long
#define maxn 1000005
#define inf 1e18
#define pb push_back
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
inline ll read()
{
ll x=0,w=1; char c=getchar();
while(c<'0'||c>'9') {if(c=='-') w=-1; c=getchar();}
while(c<='9'&&c>='0') {x=(x<<1)+(x<<3)+c-'0'; c=getchar();}
return w==1?x:-x;
}
ll a[maxn],dp[maxn][5],n,pre[maxn];
int main()
{
n=read(); rep(i,1,n) a[i]=read();
rep(i,1,n) rep(j,0,3) dp[i][j]=-inf;
dp[1][0]=a[1]; dp[2][1]=a[2]; dp[3][2]=a[3];
for(int i=3;i<=n;i++)
{
dp[i][0]=dp[i-2][0]+a[i];
dp[i][1]=max(dp[i-2][1],dp[i-3][0])+a[i];
if(i>=4) dp[i][2]=max(max(dp[i-2][2],dp[i-3][1]),dp[i-4][0])+a[i];
}
if(n%2==1) cout<<max(max(dp[n][2],dp[n-1][1]),dp[n-2][0])<<endl;
else cout<<max(dp[n][1],dp[n-1][0])<<endl;
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
#define modulo 1000000007
#define mod(mod_x) ((((long long)mod_x+modulo))%modulo)
#define Inf 1000000000000000002
int main(){
int N,M;
cin>>N>>M;
vector<map<int,vector<int>>> E(N);
for(int i=0;i<M;i++){
int p,q,c;
scanf("%d %d %d",&p,&q,&c);
p--;q--;
E[p][c].push_back(q);
E[q][c].push_back(p);
}
map<pair<int,int>,int> dis;
dis[make_pair(0,0)]=0;
deque<pair<int,int>> Q;
Q.push_back(make_pair(0,0));
set<pair<int,int>> visited;
while(Q.size()!=0){
int x = Q.back().first;
int u = Q.back().second;
Q.pop_back();
if(visited.count(make_pair(x,u)))continue;
if(x==0){
for(auto a:E[u]){
int v = a.first;
if(dis.count(make_pair(v,u)))continue;
dis[make_pair(v,u)] = dis[make_pair(x,u)]+1;
Q.emplace_front(v,u);
}
}
else{
for(int i=0;i<E[u][x].size();i++){
int v = E[u][x][i];
if(dis.count(make_pair(x,v))&&dis[make_pair(x,v)]<=dis[make_pair(x,u)])continue;
dis[make_pair(x,v)]=dis[make_pair(x,u)];
Q.emplace_back(x,v);
}
int v = u;
if(dis.count(make_pair(0,v))&&dis[make_pair(0,v)]<=dis[make_pair(x,u)])continue;
dis[make_pair(0,v)]=dis[make_pair(x,u)];
Q.emplace_back(0,v);
}
}
pair<int,int> ans(0,N-1);
if(dis.count(ans))cout<<dis[ans]<<endl;
else cout<<-1<<endl;
return 0;
} | #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;
}
| 1 |
#include<iostream>
using namespace std;
int InsertionSort(int* a, int n, int g)
{
int tmp, j, count = 0;
for (int i = g; i < n; i++)
{
tmp = a[i];
for (j = i - g; j >= 0 && a[j] > tmp; j -= g)
{
a[j + g] = a[j];
count++;
}
a[j + g] = tmp;
}
return count;
}
int ShellSort(int* a, int n)
{
int m, g[100],count = 0;
for (m = 1; m * m * m <= n; m++)
continue;
m--;
cout << m << endl;
for (int i = 0; i < m; i++)
{
g[i] = (m - i) * (m - i);
if (i)
cout << " ";
cout << g[i];
}
for (int i = 0; i < m; i++)
count += InsertionSort(a, n, g[i]);
return count;
}
int main()
{
int n, a[1000000], count;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
count = ShellSort(a, n);
cout << endl << count << endl;
for (int i = 0; i < n; i++)
cout << a[i] << endl;
return 0;
}
| #include <iostream>
#include <utility>
#include <cstdio>
using namespace std;
void insertionSort(int A[], const int n, const int g, int *cnt)
{
for( int i = g; i < n; ++i ){
int tmp = A[i];
int j = i - g;
while( -1 < j && tmp < A[j] ){
//cout << A[j+g] << "<-->" << A[j] << endl;
A[j+g] = A[j];
j = j - g;
++*cnt;
}
A[j+g] = tmp;
}
}
void shellSort(int A[], const int n)
{
int cnt = 0;
int m;
int G[100];
for( int i = 0; i < 100; ++i ){
if( i == 0 ) G[i] = n / 2 + 1;
else G[i] = G[i-1] / 2 - 1;
if( G[i] < 0 ){
G[i-1] = 1;
G[i] = 0;
m = i;
break;
}
}
for( int i = 0; i < m; ++i )
insertionSort(A, n, G[i], &cnt);
//出力
printf("%d\n", m);
for( int i = 0; i < m; ++i ){
if( i ) printf(" ");
printf("%d", G[i]);
}
printf("\n%d\n", cnt);
for( int i = 0; i < n; ++i )
printf("%d\n", A[i]);
}
int main()
{
//入力
int n;
scanf("%d", &n);
int A[n];
for( int i = 0; i < n; ++i )
scanf("%d", &A[i]);
//シェルソート
shellSort(A, n);
return 0;
}
| 1 |
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;cin>>s;
auto pos = s.find("AC");
if(pos!=string::npos){
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef vector<vector<char>> matrix;
typedef long long ll;
int N;
matrix ans;
matrix make_m(int n) {
matrix m(n, vector<char>(n, '.'));
if (n == 3) {
m[0][0] = m[0][1] = 'a';
m[1][2] = m[2][2] = 'a';
}
else if (n == 4) {
m[0][0] = m[0][1] = m[2][2] = m[2][3] = 'a';
m[0][2] = m[1][2] = m[2][0] = m[3][0] = m[3][2] = m[3][3] = 'b';
m[0][3] = m[1][0] = m[1][1] = m[1][3] = 'c';
m[2][1] = m[3][1] = 'd';
}
else if (n == 5) {
m[0][0] = m[0][1] = m[4][3] = m[4][4] = 'a';
m[0][2] = m[0][3] = m[1][0] = m[2][0] = m[2][4] = m[3][4] = m[4][1] = m[4][2] = 'b';
m[0][4] = m[1][1] = m[1][2] = m[1][4] = m[2][3] = m[3][0] = m[3][3] = m[4][0] = 'c';
}
else if (n == 6) {
m[0][0] = m[0][1] = m[1][2] = m[1][3] = m[2][5] = m[3][5] = m[5][1] = m[5][2] = 'a';
m[0][2] = m[0][3] = m[1][0] = m[2][0] = m[5][3] = m[5][4] = 'b';
m[0][4] = m[0][5] = m[1][1] = m[2][1] = m[3][0] = m[4][0] = m[4][5] = m[5][5] = 'c';
m[3][4] = m[4][4] = 'd';
}
else if (n == 7) {
m[0][0] = m[0][1] = m[1][6] = m[2][6] = m[5][3] = m[5][4] = m[6][1] = m[6][2] = 'a';
m[0][2] = m[0][3] = m[1][0] = m[2][0] = m[3][6] = m[4][6] = m[6][3] = m[6][4] = 'b';
m[0][4] = m[0][5] = m[1][1] = m[1][2] = m[2][5] = m[3][0] = m[3][5] = m[4][0] = m[5][6] = m[6][6] = 'c';
m[4][5] = m[5][5] = 'd';
}
return m;
}
int main() {
cin >> N;
if (N == 2) {
cout << -1 << endl;
return 0;
}
ans = matrix(N, vector<char>(N, '.'));
if (N < 8) ans = make_m(N);
else if (N % 4 == 0) {
matrix m4 = make_m(4);
rep(k, N / 4) {
rep(i, 4) rep(j, 4) {
ans[k*4+i][k*4+j] = m4[i][j];
}
}
}
else {
matrix m4 = make_m(4);
int x = 4 + (N % 4);
matrix mx = make_m(x);
rep(k, (N - x) / 4) {
rep(i, 4) rep(j, 4) {
ans[k*4+i][k*4+j] = m4[i][j];
}
}
int k = N - x;
rep(i, x) rep(j, x) {
ans[k+i][k+j] = mx[i][j];
}
}
rep(i, N) {
rep(j, N) cout << ans[i][j];
cout << endl;
}
}
| 0 |
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using P = pair<int, int>;
int main()
{
string s;
cin >> s;
if(s.size() == 2) cout << s << endl;
else {
reverse(s.begin(), s.end());
cout << s << endl;
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
#define NDEBUG
#ifdef DEBUG
#include "../cout11.h"
#undef NDEBUG
#endif
#include <cassert>
typedef long long ll;
typedef long double Double;
typedef unsigned long long ull;
typedef pair<int,int> ii;
typedef pair<ll,ll> llll;
typedef pair<double,double> dd;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ii> vii;
typedef vector<vector<ii>> vvii;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<long double> vD;
#define sz(a) int((a).size())
#define pb push_back
#define FOR(var,from,to) for(int var=(from);var<=(to);++var)
#define rep(var,n) for(int var=0;var<(n);++var)
#define rep1(var,n) for(int var=1;var<=(n);++var)
#define repC2(vari,varj,n) for(int vari=0;vari<(n)-1;++vari)for(int varj=vari+1;varj<(n);++varj)
#define ALL(c) (c).begin(),(c).end()
#define RALL(c) (c).rbegin(),(c).rend()
#define tr(i,c) for(auto i=(c).begin(); i!=(c).end(); ++i)
#define found(s,e) ((s).find(e)!=(s).end())
#define mset(arr,val) memset(arr,val,sizeof(arr))
#define mid(x,y) ((x)+((y)-(x))/2)
#define IN(x,a,b) ((a)<=(x)&&(x)<=(b))
ll gcd(ll a, ll b) { while(a) swap(a, b%=a); return b; }
const ll MOD=1000000007LL;
ll ADD(ll x, ll y) { return (x+y) % MOD; }
ll SUB(ll x, ll y) { return (x-y+MOD) % MOD; }
ll MUL(ll x, ll y) { return x*y % MOD; }
ll POW(ll x, ll e) { ll v=1; for(; e; x=MUL(x,x), e>>=1) if (e&1) v = MUL(v,x); return v; }
ll DIV(ll x, ll y) { /*assert(y%MOD!=0);*/ return MUL(x, POW(y, MOD-2)); }
// ll comb(ll n, ll k) { ll v=1; for(ll i=1; i<=k; i++) v = DIV(MUL(v, n-i+1),i); return v; }
//
void solve(ll K) {
int N = 50;
vector<ll> a(N, N-1);
ll q = K / N, r = K % N;
rep(i,N) a[i] += q;
rep(i,N){
if (i < r) a[i] += (N - (r-1));
else a[i] -= r;
}
cout << N << endl;
rep(i,N) {
cout << a[i];
if (i < N-1) cout << " ";
}
cout << endl;
}
int main() {
ll K; cin >> K;
solve(K);
return 0;
}
| 0 |
#include <stdio.h>
int main(){
int h,w,n;
scanf("%d", &h);
scanf("%d", &w);
scanf("%d", &n);
int ans = 0, count = 0;
while (n > ans){
if(w >= h){
ans += w;
count++;
}else if(h>w){
ans += h;
count++;
}
}
printf("%d\n", count);
return 0;
}
| #include<stdio.h>
#include<iostream>
#include<vector>
#include<math.h>
#include<queue>
#include<map>
#include<algorithm>
#include<string.h>
#include<functional>
#include<limits.h>
#include<stdlib.h>
#include<string>
#include<unordered_map>
#include<set>
using namespace std;
#define intmax INT_MAX
#define lmax LONG_MAX
#define uintmax UINT_MAX
#define ulmax ULONG_MAX
#define llmax LLONG_MAX
#define ll long long
#define rep(i,a,N) for((i)=(a);(i)<(N);(i)++)
#define rrp(i,N,a) for((i)=(N)-1;(i)>=(a);(i)--)
#define llfor ll i,j,k
#define sc(a) cin>>a
#define pr(a) cout<<a<<endl
#define pY puts("YES")
#define pN puts("NO")
#define py puts("Yes")
#define pn puts("No")
#define pnn printf("\n")
#define sort(a) sort(a.begin(),a.end())
#define push(a,b) (a).push_back(b)
#define llvec vector<vector<ll>>
#define charvec vector<vector<char>>
#define sizeoof(a,b) (a,vector<ll>(b))
#define llpvec vector<pair<ll,ll>>
/*繰り上げ除算*/ll cei(ll x,ll y){ll ans=x/y;if(x%y!=0)ans++;return ans;}
/*最大公約数*/ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
/*最小公倍数*/ll lcm(ll x,ll y){return x/gcd(x,y)*y;}
/*n乗*/ll llpow(ll x,ll n){ll i,ans=1;rep(i,0,n)ans*=x;return ans;}
/*階乗*/ll fact(ll x){ll i,ans=1;rep(i,0,x)ans*=(x-i);return ans;}
/*nCr*/ll ncr(ll n,ll r){return fact(n)/fact(r)/fact(n-r);}
/*nPr*/ll npr(ll n,ll r){return fact(n)/fact(n-r);}
/*primejudge*/bool prime(ll a){if(a<=1)return false;ll i;for(i=2;i*i<=a;i++){if(a%i==0)return false;}return true;}
llfor;///////////////////////////////////////////////////////////
int main(){
ll h,w,n;
sc(h);sc(w);sc(n);
ll a=max(h,w);
a=cei(n,a);
pr(a);
return 0;} | 1 |
#include <bits/stdc++.h>
typedef long double ll;
#define fr(i,a,b) for(long long i=a; i<b; i++)
#define rf(i,a,b) for(ll i=a; i>=b; i--)
typedef std::vector<long long > vi ;
#define F first
#define S second
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mod 1000000007
#define PB push_back
#define MP make_pair
#define PI 3.14159265358979323846
#define all(a) a.begin(),a.end()
#define mx(a) *max_element(all(a))
#define mn(a) *min_element(all(a))
#define endl '\n'
#define show(a) for(auto el:a)cout<<el<<" "
const ll INF=LLONG_MAX/2;
const ll N=2e5+1;
#define yesno(f) yes(f);no(f);
#define yes(f) if(f)cout<<"YES\n";
#define no(f) if(!f)cout<<"NO\n";
using namespace std;
int main()
{
fast;
ll t;
t=1;
while(t--)
{
long long n,x=0,k;
std::cin >> n>>k;
ll a[n],maxi=0,sum;
fr(i,0,n)
std::cin >> a[i];
fr(i,0,k)
maxi+=((a[i]+1)/2);
sum=maxi;
fr(i,k,n)
{
sum=sum-((a[i-k]+1)/2)+(a[i]+1)/2;
maxi=max(maxi,sum);
}
cout<<fixed;
cout<<setprecision(12);
cout<<maxi<<"\n";
}
} | #include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
int main() {
int W[10], K[10];
for (int i = 0; i < 10; i++) {
cin >> W[i];
}
for (int i = 0; i < 10; i++) {
cin >> K[i];
}
sort(W, W+10, greater<int>());
sort(K, K+10, greater<int>());
cout << W[0] + W[1] + W[2] << ' ';
cout << K[0] + K[1] + K[2] << endl;
return 0;
} | 0 |
#include <iostream>
#include <vector>
int main()
{
int n;
std::cin >> n;
std::vector<std::vector<int>> memo(60, std::vector<int> (25));
for (int i = 0; i < n; ++i)
{
long double a;
std::cin >> a;
long long b = (long long) (a * 1e9 + 0.5);
int count2 = 0, count5 = 0;
while (b % 2 == 0)
{
++count2;
b /= 2;
}
while (b % 5 == 0)
{
++count5;
b /= 5;
}
++memo[count2][count5];
}
long long ans = 0;
for (int i = 0; i < 60; ++i)
{
for (int j = 0; j < 25; ++j)
{
for (int ii = std::max(0, 18 - i); ii < 60; ++ii)
{
for (int jj = std::max(0, 18 - j); jj < 25; ++jj)
{
if (i != ii || j != jj)
{
ans += 1LL * memo[i][j] * memo[ii][jj];
}
}
}
}
}
ans /= 2;
for (int i = 0; i < 60; ++i)
{
for (int j = 0; j < 25; ++j)
{
if (i + i >= 18 && j + j >= 18)
{
ans += 1LL * memo[i][j] * (memo[i][j] - 1) / 2;
}
}
}
std::cout << ans << '\n';
return 0;
} | #include <cstdio>
#include <cstdlib>
#include <cassert>
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define repi(i,a,b) for(ll i=(a);i<(b);++i)
#define rep(i,a) repi(i,0,a)
#define repdi(i,a,b) for(ll i=(a)-1;i>=(b);--i)
#define repd(i,a) repdi(i,a,0)
#define itr(it,a) for( auto it = (a).begin(); it != (a).end(); ++it )
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define endl '\n'
#define debug(x) std::cerr << #x << " = " << (x) << endl;
using ll = long long;
using P = std::pair<ll, ll>;
constexpr ll INF = 1ll<<60;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class S, class T>
std::ostream& operator<< ( std::ostream& out, const std::pair<S,T>& a )
{ std::cout << '(' << a.first << ", " << a.second << ')'; return out; }
template<class T>
std::ostream &operator<< ( std::ostream& out, const std::vector<T>& a )
{ std::cout << '['; rep( i, a.size() ){ std::cout << a[i]; if( i != a.size()-1 ) std::cout << ", "; } std::cout << ']'; return out; }
template<typename T>
class BinaryIndexedTree2D {
int H;
int W;
std::vector<std::vector<T> > data2d;
public:
BinaryIndexedTree2D(int H, int W) : H(H), W(W) {
data2d.resize(H + 1, std::vector<T>(W + 1, 0));
}
T sum(int h, int w) {
T res = 0;
for (int i = h; i > 0; i -= i & -i) {
for (int j = w; j > 0; j -= j & -j) {
res += data2d[i][j];
}
}
return res;
}
/**
* 左上の座標(h1, w1), 右下の座標(h2, w2) に含まれる値の累積和
*/
T sum(int h1, int w1, int h2, int w2) {
return sum(h2, w2) - sum(h1 - 1, w2) - sum(h2, w1 - 1) + sum(h1 - 1, w1 - 1);
}
void add(int h, int w, ll x) {
for (int i = h; i <= H; i += i & -i) {
for (int j = w; j <= W; j += j & -j) {
data2d[i][j] += x;
}
}
}
};
ll N;
BinaryIndexedTree2D<ll> bit( 100, 100 );
constexpr ll base = 20;
int main() {
std::cin >> N;
ll ans = 0;
rep( i, N ) {
std::string A;
std::cin >> A;
bool fst = true;
ll p = A.size();
ll ten = 1;
ll s = 0, num = 0;
if( A.find('.') != std::string::npos ) repd( i, A.size() ) {
if( A[i] != '0' ) {
if( fst ) {
p = i;
}
fst = false;
}
if( !fst && A[i] != '.' ) {
num += (A[i]-'0')*ten;
ten *= 10;
}
if( A[i] == '.' ) {
s = p-i;
}
} else {
rep( i, A.size() ) {
num *= 10;
num += A[i]-'0';
}
s = 0;
}
//std::cout << P(num, s) << endl;
ll two = 0, five = 0;
while( num % 2 == 0 ) {
num /= 2;
++two;
}
while( num % 5 == 0 ) {
num /= 5;
++five;
}
//std::cout << P(two-s, five-s) << endl;
ans += bit.sum( -(two-s)+base, -(five-s)+base, 98, 98 );
//std::cout << "sum: " << P(-(two-s)+base, -(five-s)+base) << endl;
bit.add( two-s+base, five-s+base, 1 );
//std::cout << "added: " << P(two-s+base, five-s+base) << endl;
}
std::cout << ans << endl;
return 0;
} | 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;
int main() {
ios::sync_with_stdio(false);
int n;
ll a[300005] = {}, z = 0;
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < 60; i++) {
ll b = 0;
for (int j = 0; j < n; j++) if (a[j] & (1ll << i)) b++;
z = (z + (1ll << i) % mod * b % mod * (n - b)) % mod;
}
cout << z;
}
| #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1001001001;
const ll LINF = 1001001001001001;
const int MOD = 1000000007;
template <typename T>
void print(const T &v);
int main()
{
int n;
cin >> n;
vector<int> cs(60, 0); // 立っているビットの個数を記録する
for (int i = 0; i < n; i++)
{
ll a;
cin >> a;
bitset<60> bs(a);
for (int j = 0; j < 60; j++)
{
if (bs.test(j))
{
++cs[j];
}
}
}
ll ans = 0;
for (int i = 0; i < 60; i++)
{
ll tmp = pow(2, i);
tmp %= MOD;
tmp *= cs[i];
tmp %= MOD;
tmp *= (n - cs[i]);
tmp %= MOD;
ans += tmp;
ans %= MOD;
}
cout << ans << endl;
return 0;
}
// Use For Debug
template <typename T>
void print(T const &v)
{
for (int i = 0; i < v.size(); i++)
{
if (i)
cout << ' ';
cout << v[i];
}
cout << endl;
}; | 1 |
#include <iostream>
#include <array>
#include <algorithm>
#include <vector>
#include <bitset>
#include <set>
#include <unordered_set>
#include <cmath>
#include <complex>
#include <deque>
#include <iterator>
#include <numeric>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <limits>
#include <iomanip>
#include <functional>
#include <cassert>
using namespace std;
using ll=long long;
template<class T> using V = vector<T>;
template<class T, class U> using P = pair<T, U>;
using vll = V<ll>;
using vvll = V<vll>;
#define rep(i, k, n) for (ll i=k; i<(ll)n; ++i)
#define REP(i, n) rep(i, 0, n)
#define ALL(v) v.begin(),v.end()
template < class T > inline bool chmax(T& a, T b) {if (a < b) { a=b; return true; } return false; }
template < class T > inline bool chmin(T& a, T b) {if (a > b) { a=b; return true; } return false; }
#define DEBUG_VLL(vec) REP(sz, vec.size()) std::cerr<<vec[sz]<<(sz==vec.size()-1?'\n':' ');
const long long MOD = 1000000007;
const long long HIGHINF = (long long)1e18;
const int INF = (int)1e9;
int t;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cin >> t;
while (t-->0) {
int n; cin >> n;
vll a(n);
for (int i = 0; i < n; i++) cin >> a[i];
string s; cin >> s;
vll basis;
int ans = -1;
for (int i = n - 1; i >= 0; i--) {
for (ll base: basis) chmin(a[i], a[i] ^ base);
// もし a[i] があとで表される(a[i]==0)なら
// どちらにせよ操作の必要なし?(打ち消せるので)
// a[i] があとで表せられない(a[i]!=0)なら
// 1 なら勝ち(0 に出来ないので)
// 0 なら基底に加えてもっと前(<i)を見る
if (a[i] != 0) {
if (s[i] == '1') {
ans = 1;
break;
} else basis.emplace_back(a[i]);
}
}
if (ans == -1) ans = 0;
cout << ans << '\n';
}
return 0;
}
|
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn=205;
ll a[maxn],p[maxn];
char s[maxn];
int n,m;
void solve(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
scanf("%s",s+1);
if(s[n]=='1') printf("1\n");
else{
int flag=0;
memset(p,0,sizeof(p));
for(int i=n;i>=1;i--){
if(s[i]=='0'){
ll tmp=a[i];
for(int j=62;j>=0;j--){
if((1ll<<j)&tmp){
if(!p[j]){
p[j]=tmp;break;
}
tmp^=p[j];
}
}
}
else if(s[i]=='1'){
ll tmp=a[i];
for(int j=62;j>=0;j--){
if((1ll<<j)&tmp){
if(!p[j]) break;
tmp^=p[j];
}
}
if(tmp){
flag=1;break;
}
}
}
printf("%d\n",flag);
}
}
}
int main(){
solve();
return 0;
} | 1 |
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
#define debug(x) (cout << # x ": " << x << endl)
#define ll long long int
#define repeat(n) for (int i = 0; i < n; ++i)
#define all(x) x.begin(), x.end()
int main() {
int n;
cin >> n;
vector<pair<int, int>> s1, s2;
repeat(n) {
string s;
cin >> s;
for(auto it = s.find("()"); it != string::npos; it = s.find("()")) {
s.erase(it, 2);
}
int lc = count(all(s), ')');
int rc = count(all(s), '(');
if (rc - lc >= 0) {
s1.push_back(make_pair(lc, rc));
} else {
s2.push_back(make_pair(lc, rc));
}
}
sort(all(s1), [](const auto& lhs, const auto& rhs){
return lhs.first < rhs.first;
});
sort(all(s2), [](const auto& lhs, const auto& rhs){
return lhs.second > rhs.second;
});
int le = 0, re = 0;
for (auto& s: s1) {
le = le + s.first - min(s.first, re);
re = re - min(s.first, re) + s.second;
}
for (auto& s: s2) {
le = le + s.first - min(s.first, re);
re = re - min(s.first, re) + s.second;
}
if (le == 0 && re == 0) {
cout << "Yes";
} else {
cout << "No";
}
return 0;
} | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
int N;
cin >> N;
vector<int> L;
for (int i = 0; i < 2 * N; ++i)
{
int temp;
cin >> temp;
L.push_back(temp);
}
int ans = 0;
sort(L.begin(), L.end());
for (int i = 0; i < 2 * N; ++i)
{
ans += min(L[i], L[i + 1]);
++i;
}
cout << ans << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using uint = unsigned int;
#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 per(i,n) for(int i=int(n)-1;i>=0;i--)
#define per1(i,n) for(int i=int(n);i>0;i--)
#define all(c) c.begin(),c.end()
#define si(x) int(x.size())
#define pb emplace_back
#define fs first
#define sc second
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
template<class T,class U> void chmax(T& x, U y){if(x<y) x=y;}
template<class T,class U> void chmin(T& x, U y){if(y<x) x=y;}
template<class T> void mkuni(V<T>& v){sort(all(v));v.erase(unique(all(v)),v.end());}
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
o<<"{";
for(const T& v:vc) o<<v<<",";
o<<"}";
return o;
}
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }
#ifdef LOCAL
#define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
void dmpr(ostream& os){os<<endl;}
template<class T,class... Args>
void dmpr(ostream&os,const T&t,const Args&... args){
os<<t<<" ~ ";
dmpr(os,args...);
}
#define shows(...) cerr << "LINE" << __LINE__ << " : ";dmpr(cerr,##__VA_ARGS__)
#define dump(x) cerr << "LINE" << __LINE__ << " : " << #x << " = {"; \
for(auto v: x) cerr << v << ","; cerr << "}" << endl;
#else
#define show(x) void(0)
#define dump(x) void(0)
#define shows(...) void(0)
#endif
struct UnionFind{
vector<int> par;
UnionFind(int N){
par.assign(N,0);
rep(i,N) par[i]=i;
}
int find(int x){
if(par[x]==x) return x;
return par[x]=find(par[x]);
}
bool same(int x,int y){
return find(x)==find(y);
}
void unite(int x,int y){
x=find(x),y=find(y);
if(x==y) return;
par[y]=x;
}
};
int main(){
cin.tie(0);
ios::sync_with_stdio(false); //DON'T USE scanf/printf/puts !!
cout << fixed << setprecision(20);
int N; cin >> N;
V<int> x(N),y(N);
rep(i,N) cin >> x[i] >> y[i];
VV<ll> es;
rep(_,2){
V<int> idx(N); iota(all(idx),0);
sort(all(idx),[&](int l,int r){return x[l]<x[r];});
rep(i,N-1){
int a = idx[i], b = idx[i+1];
es.push_back({x[b]-x[a],a,b});
}
rep(i,N) swap(x[i],y[i]);
}
sort(all(es));
UnionFind UF(N);
ll ans=0;
for(auto e: es){
if(!UF.same(e[1],e[2])){
UF.unite(e[1],e[2]);
ans += e[0];
}
}
cout << ans << endl;
}
| #include <bits/stdc++.h>
#define int long long
using namespace std;
typedef struct{
int x,y,id;
} ii;
typedef tuple<int,ii,ii> t;
int dist(ii a,ii b){
return min(abs(a.x-b.x),abs(a.y-b.y));
}
vector<int> up;
int fp(int u){
if(up[u]<0) return u;
else return up[u] = fp(up[u]);
}
void unite(int u,int v){
int x = fp(u);
int y = fp(v);
if(x!=y) up[x]=y;
}
signed main() {
int n;
cin>>n;
up.resize(n,-1);
vector<ii> p(n);
int i=0;
for(auto &v:p)
{
v.id=i++;
cin>>v.x>>v.y;
}
auto cmp= [](t a,t b){
return get<0>(a)>get<0>(b);
};
priority_queue<t,vector<t>,decltype (cmp)> pq(cmp);
sort(p.begin(),p.end(),[](ii lhs,ii rhs){return lhs.x<rhs.x;});
for(auto it = p.begin();it!=prev(p.end());it++){
int d = dist(*it,*next(it));
pq.push(make_tuple(d,*it,*next(it)));
}
sort(p.begin(),p.end(),[](ii lhs,ii rhs){return lhs.y<rhs.y;});
for(auto it = p.begin();it!=prev(p.end());it++){
int d = dist(*it,*next(it));
pq.push(make_tuple(d,*it,*next(it)));
}
int cnt = n;
int res = 0;
while(!pq.empty()){
int d;
ii a,b;
tie(d,a,b) = pq.top();
pq.pop();
if(fp(a.id)!=fp(b.id)){
n--;
res+=d;
unite(fp(a.id), fp(b.id));
}
}
cout<<res<<endl;
} | 1 |
#include <iostream>
using namespace std;
int main()
{
int n, a, b, count = 0, pointX = 0, pointY = 0;
cin >> n;
do {
cin >> a >> b;
if (a > b) {
pointX += (a + b);
}
else if (a < b) {
pointY += (a + b);
}
else {
pointX += a;
pointY += b;
}
count++;
if (count == n) {
cout << pointX << " " << pointY << endl;
pointX = pointY = count = 0;
cin >> n;
}
} while (n != 0);
return 0;
} | #include <iostream>
using namespace std;
int main ()
{
int N;
while (cin >> N, N) {
int n1, n2;
int a = 0, b = 0;
for (int i = 0; i < N; i++) {
cin >> n1 >> n2;
if (n1 > n2) a += n1 + n2;
else if (n1 < n2) b += n1 + n2;
else {
a += n1;
b += n2;
}
}
cout << a << " " << b << endl;
}
return 0;
} | 1 |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
const int MOD=1e9+7;
const long double Pi=acos(-1);
using P=pair<ll,ll>;
ll gcd(ll a,ll b){//aとbの最大公約数を返す
if(a%b==0){return b;}
else{return gcd(b,a%b);}
}
ll lcm(ll a,ll b){//aとbの最小公倍数を返す
return a*b/gcd(a,b);
}
ll pow(ll x,ll y){//x^yを返す
ll tmp=x,res=1;
while(y){
if(y%2){res=res*tmp%MOD;}
y>>=1;
tmp=tmp*tmp%MOD;
}
return res;
}
ll nature(ll a){//絶対値を返す
if(a>=0){return(a);}
else{return(-1*a);}
}
ll prime(ll a){//素数なら1を返す
if(a==2){return 1;}
else if(a%2==0 || a==1){return 0;}
for(ll i=3;i*i<=a;i+=2){if(a%i==0){return 0;}}
return 1;
}
int main(){
string S;
cin>>S;
if(S=="zyxwvutsrqponmlkjihgfedcba"){cout<<-1<<endl;}
else if(S.size()==26){
ll maxcharacter=S.at(25);
ll rock;
for(ll i=0;i<25;i++){
if(S.at(24-i)<maxcharacter){
for(ll j=0;j<24-i;j++){cout<<S.at(j);}
rock=24-i;
unordered_map<ll,ll> mp;
for(ll i=0;i<rock;i++){
mp[S.at(i)]++;
}
char start=S.at(rock)+1;
while(mp[start]>0){start++;}
cout<<start<<endl;
return 0;
}
else{maxcharacter=S.at(24-i);}
}
}
else{
unordered_map<ll,ll> mp;
for(ll i=0;i<(ll)S.size();i++){
mp[S.at(i)]++;
}
cout<<S;
if(mp['a']==0){cout<<"a"<<endl;return 0;}
if(mp['b']==0){cout<<"b"<<endl;return 0;}
if(mp['c']==0){cout<<"c"<<endl;return 0;}
if(mp['d']==0){cout<<"d"<<endl;return 0;}
if(mp['e']==0){cout<<"e"<<endl;return 0;}
if(mp['f']==0){cout<<"f"<<endl;return 0;}
if(mp['g']==0){cout<<"g"<<endl;return 0;}
if(mp['h']==0){cout<<"h"<<endl;return 0;}
if(mp['i']==0){cout<<"i"<<endl;return 0;}
if(mp['j']==0){cout<<"j"<<endl;return 0;}
if(mp['k']==0){cout<<"k"<<endl;return 0;}
if(mp['l']==0){cout<<"l"<<endl;return 0;}
if(mp['m']==0){cout<<"m"<<endl;return 0;}
if(mp['n']==0){cout<<"n"<<endl;return 0;}
if(mp['o']==0){cout<<"o"<<endl;return 0;}
if(mp['p']==0){cout<<"p"<<endl;return 0;}
if(mp['q']==0){cout<<"q"<<endl;return 0;}
if(mp['r']==0){cout<<"r"<<endl;return 0;}
if(mp['s']==0){cout<<"s"<<endl;return 0;}
if(mp['t']==0){cout<<"u"<<endl;return 0;}
if(mp['u']==0){cout<<"u"<<endl;return 0;}
if(mp['v']==0){cout<<"v"<<endl;return 0;}
if(mp['w']==0){cout<<"w"<<endl;return 0;}
if(mp['x']==0){cout<<"x"<<endl;return 0;}
if(mp['y']==0){cout<<"y"<<endl;return 0;}
if(mp['z']==0){cout<<"z"<<endl;return 0;}
}
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
string s;
int alp[27];
for (int i = 0; i < 26; i++) {
alp[i] = 0;
}
alp[26] = 1;
cin >> s;
if (s == "zyxwvutsrqponmlkjihgfedcba") {
puts("-1");
} else {
string ans = "";
for (int i = 0; i < (int)s.size(); i++) {
alp[s[i] - 'a']++;
}
if (s.size() < 26) {
for (int i = 0; i < 26; i++) {
if (alp[i] == 0) {
ans = s + (char)('a' + i);
break;
}
}
} else {
string s_next = s;
next_permutation(s_next.begin(), s_next.end());
for (int i = 0; i < (int)s.size(); i++) {
ans += s_next[i];
if (s[i] != s_next[i]) break;
}
}
cout << ans << endl;
}
return 0;
} | 1 |
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define ll long long
#define INF 1000000000000000000
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
ll N, M;
cin >> N >> M;
vector<ll> A(N), sum(N + 1, 0);
rep(i, N) { cin >> A[i]; }
map<ll, ll> cnt;
ll ans = 0;
rep(i, N) { sum[i + 1] = sum[i] + A[i]; }
rep(i, N + 1) { cnt[sum[i] % M]++; }
for (auto c : cnt) {
ans += (c.second * (c.second - 1)) / 2;
}
cout << ans << endl;
} | #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=200000;
int n,m,a[N+9];
int cnt[N+9];
bool Get_a(){
for (int i=1;i<=n;++i) cnt[i]=0;
for (int i=1;i<=n;++i){
int l=max(i-a[i],1),r=min(i+a[i],n);
++cnt[l];--cnt[r+1];
}
for (int i=1;i<=n;++i) a[i]=cnt[i]+=cnt[i-1];
for (int i=1;i<=n;++i)
if (a[i]<n) return 0;
return 1;
}
int main(){
scanf("%d%d",&n,&m);
for (int i=1;i<=n;++i)
scanf("%d",&a[i]);
for (int i=1;i<=m;++i)
if (Get_a()) break;
for (int i=1;i<=n;++i)
printf("%d ",a[i]);
puts("");
return 0;
} | 0 |
#include <bits/stdc++.h>
#define rep(i,a,b) for(ll i=ll(a);i<ll(b);i++)
#define irep(i,a,b) for(ll i=ll(a);i>=ll(b);i--)
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 pb push_back
#define mp make_pair
#define F .first
#define S .second
using ll=long long;
using ld=long double;
const ll INF = 1LL<<60;
const ll mod=1e9+7;
using namespace std;
ll GCD(ll a, ll b) { return b ? GCD(b, a%b) : a; }
int main(){
ld n,m,d,x=2;
cin>>n>>m>>d;
if(d==0)x=1; // n/n^2
//ans=2*(n-2*d) d+1~n-d[i+d,i-d] 2通り
//ans+=2*d 1~d[i+d] n-d~n[i-d] 1通り
cout<<fixed<<setprecision(12)<<(m-1)*x*(n-d)/(n*n);;
} | //#include <bits/stdc++.h>
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <tuple>
#include <string>
#include <algorithm>
#include <functional>
#include <queue>
#include <stack>
#include <cmath>
#include <bitset>
using namespace std;
using ll=long long;
using intpair=pair<int,int>;
using intpv=vector<intpair>;
using llpair=pair<ll,ll>;
using llpv=vector<llpair>;
using intvec=vector<int>;
using llvec=vector<ll>;
using intq=queue<int>;
using llq=queue<ll>;
using intmat=vector<intvec>;
using llmat=vector<llvec>;
using pairmat=vector<llpv>;
template<typename T>
using matrix=vector<vector<T>>;
template<typename T>
using pque=priority_queue<T>;
template<typename T>
using lpque=priority_queue<T,vector<T>,greater<T>>;
#define PI 3.141592653589793
#define INTINF 1<<30
#define LLINF 1LL<<60
#define MPRIME 1000000007
#define MPRIME9 998244353
#define pushb push_back
#define fi first
#define se second
#define setpr fixed<<setprecision(15)
#define all(name) name.begin(),name.end()
#define rall(name) name.rbegin(),name.rend()
#define gsort(vbeg,vend) sort(vbeg,vend,greater<>())
#define init(v,size) v.resize(size); for(auto &a__: v) cin>>a__
#define matin(mat,H,W) for(int i__=0; i__<H; i__++)for(int j__=0; j__<W; j__++) cin>>mat[i__][j__]
#define out(n) cout<<n<<endl
template<class T> inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template<class T> inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return true;
}
return false;
}
template<class T> inline void outmat(T& a, int y, int x) {
for(int i=0; i<y; i++) {
for(int j=0; j<x; j++) cout<<a[i][j]<<" ";
cout<<endl;
}
}
ll GCD(ll a, ll b) { //最大公約数
if(a==0||b==0) return 0;
if(a<b) swap(a,b);
ll tmp = a%b;
while(tmp!=0) {
a = b;
b = tmp;
tmp = a%b;
}
return b;
}
ll binpow(ll a, ll ex, ll p) { //繰り返し二乗法
ll result=1;
while(ex>0) {
if(ex&1) result=result*a%p;
ex>>=1;
a=a*a%p;
}
return result;
}
ll Fact(ll x, ll p) { //階乗
ll f=1;
for(ll i=2; i<=x; i++) {
f*=i;
f%=p;
}
return f;
}
ll nPr(ll n, ll r) {
if(n<r) return 0;
ll result=1LL;
for(ll i=0; i<r; i++) result*=n-i;
return result;
}
ll nPrP(ll n, ll r, ll p) { // mod pにおけるnPr
if(n<r) return 0;
ll result=1LL;
for(int i=0; i<r; i++) {
result*=n-i;
result%=p;
}
return result;
}
ll nCr(ll n, ll r) {
if (n == r) { return 1; }
if (r > n) { return 0; }
if (r > n / 2) { r = n - r; }
if (n == 0) { return 0; }
if (r == 0) { return 1; }
if (r == 1) { return n; }
double result = 1;
for (double i = 1; i <= r; i++) {
result *= (n - i + 1) / i;
}
return (ll)result;
}
int DigitNum(ll n) { //桁数
int digit=0;
ll div=1LL;
while(n/div) {
digit++;
div*=10;
}
return digit;
}
bool Palind(string s) { //回文判定
return s == string(s.rbegin(), s.rend());
}
double n,m,d;
void input() {
cin>>n>>m>>d;
}
void solve() {
double D;
if(d) D=2.*(n-d)/n/n;
else D=1/n;
cout<<setpr<<(m-1)*D<<endl;
}
int main() {
while(1) {
input();
solve();
break;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;
using PLL = pair<ll, ll>;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define repn(i,n) for(int i = 0; i <= (int)(n); i++)
#define srep(i,l,n) for(int i = l; i < (int)(n); i++)
#define srepn(i,l,n) for(int i = l; i <= (int)(n); i++)
#define drep(i,n) for(int i = (int)(n-1); i >= 0; i--)
#define drepn(i,n) for(int i = (int)(n); i >= 0; i--)
#define size(s) (int)s.size()
#define debug(var) do{std::cout << #var << " : ";view(var);}while(0)
template<typename T> void view(T e){std::cout << e << std::endl;}
template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cout << e << " "; } std::cout << std::endl;}
template<typename T> void view(const std::vector<std::vector<T> >& vv){ for(const auto& v : vv){ view(v); } }
template<typename T> inline istream& operator>>(istream &i, vector<T> &v) {rep(j, size(v)) i >> v[j]; return i;}
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template<class T> T gcd(T a, T b) {if(b==0)return a; else return gcd(b,a%b);}
template<class T> T lcm(T a, T b) {return a/gcd(a,b)*b;}
template<class T = int> using V = vector<T>;
template<class T = int> using VV = vector<V<T>>;
bool isIn(int i, int j, int h, int w) {return i >= 0 && i < h && j >= 0 && j < w;}
void Yes(){cout << "Yes" << endl;}
void No(){cout << "No" << endl;}
void YES(){cout << "YES" << endl;}
void NO(){cout << "NO" << endl;}
void err() {cout << -1 << endl;}
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define ep emplace_back
const int MOD = 1000000007;
const int INF = 1e9;
#define PI acos(-1);
int dx[4] = {1,-1,0,0};
int dy[4] = {0,0,1,-1};
int ddx[8] = {1,1,1,-1,-1,-1,0,0};
int ddy[8] = {0,1,-1,0,1,-1,1,-1};
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
V<> a(n);
cin >> a;
sort(rall(a));
int ans = accumulate(a.begin(), a.begin() + k, 0);
cout << ans << endl;
} | #include <bits/stdc++.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <string.h>
using namespace std;
int main() {
int n,k,L[100];
cin >> n >> k;
for(int i = 0; i < n; i++)
cin >> L[i];
sort(L,L+n);
int sum = 0;
for(int i = n-1; i >= n-k; i--)
sum += L[i];
cout << sum << endl;
} | 1 |
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ll long long
#define vll vector<ll>
#define ld long double
#define pll pair<ll,ll>
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define oset tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define osetll tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
#define ook order_of_key
#define fbo find_by_order
const int MOD=1000000007;
long long int inverse(long long int i){
if(i==1) return 1;
return (MOD - ((MOD/i)*inverse(MOD%i))%MOD+MOD)%MOD;
}
ll POW(ll a,ll b)
{
if(b==0) return 1;
if(b==1) return a%MOD;
ll temp=POW(a,b/2);
if(b%2==0) return (temp*temp)%MOD;
else return (((temp*temp)%MOD)*a)%MOD;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll n;
cin>>n;
string s[n];
ll a[n],b[n];
vector<pair<ll,ll> > v1,v2;
for(int i=0;i<n;i++)
{
cin>>s[i];
ll mx=0,temp=0;
for(int j=0;j<s[i].size();j++)
{
if(s[i][j]==')') temp++;
else temp--;
mx=max(temp,mx);
}
a[i]=mx;
temp=0;
ll mn=0;
for(int j=s[i].size()-1;j>=0;j--)
{
if(s[i][j]=='(') temp++;
else temp--;
mn=max(mn,temp);
}
b[i]=mn;
//cout<<a[i]<<" "<<b[i]<<" ";
if( (b[i]-a[i]) >0) v1.PB(MP(a[i],i));
else v2.PB(MP(-b[i],i));
}
sort(v1.begin(),v1.end());
sort(v2.begin(),v2.end());
stack<ll> st;
ll flag=0;
for(int i=0;i<v1.size();i++)
{
string k=s[v1[i].S];
for(int j=0;j<k.size();j++)
{
if(k[j]=='(') st.push(1);
else
{
if(st.empty()) {flag=1; break;}
else st.pop();
}
}
if(flag==1) break;
}
if(flag==0)
{
for(int i=0;i<v2.size();i++)
{
string k=s[v2[i].S];
for(int j=0;j<k.size();j++)
{
if(k[j]=='(') st.push(1);
else
{
if(st.empty()) {flag=1; break;}
else st.pop();
}
}
if(flag==1) break;
}
}
if(flag==0 && st.empty()) cout<<"Yes";
else cout<<"No";
}
| #include <bits/stdc++.h>
#include <iostream>
using namespace std;
int* mallocint(int si)
{
return (int*) malloc(si * sizeof(int));
}
int* _minn;
int* _total;
bool compare(int i1, int i2)
{
if( _minn[i1] != _minn[i2] ) return _minn[i1] > _minn[i2];
return _total[i1] > _total[i2];
}
bool comparen(int i1, int i2)
{
return _minn[i1] < _minn[i2];
}
bool check(int &count, vector<int> &l)
{
sort(l.begin(), l.end(), compare);
queue<int> q;
int current = 0;
for( auto itr = l.begin(); itr != l.end(); itr++ )
{
if( current + _minn[*itr] < 0 )
{
return false;
}
current += _total[*itr];
}
count = current;
return true;
}
bool check(vector<int> &pindex, vector<int> &nindex, int lminmin, int rminmin)
{
int c1 = 0;
int c2 = 0;
if( !check(c1, pindex) ) return false;
if( !check(c2, nindex) ) return false;
if( c1 != c2 ) return false;
if( lminmin + c1 < 0 ) return false;
if( rminmin + c2 < 0 ) return false;
return true;
}
int main()
{
int n;
string s;
cin >> n;
getline(cin, s);
int* minn = mallocint(n);
int* total = mallocint(n);
_minn = minn;
_total = total;
vector<int> pindex;
vector<int> nindex;
int lminmin = 0;
int rminmin = 0;
for( int i = 0; i < n; i++ )
{
getline(cin, s);
int lcount = 0;
int lmin = 0;
int rcount = 0;
int rmin = 0;
for( int j = 0; j < s.length(); j++ )
{
if (s.at(j) == '(') lcount++;
if (s.at(j) == ')') lcount--;
if( lcount < lmin ) lmin = lcount;
}
for( int j = s.length() - 1; j >= 0; j-- )
{
if (s.at(j) == ')') rcount++;
if (s.at(j) == '(') rcount--;
if( rcount < rmin ) rmin = rcount;
}
if( lcount > 0 )
{
minn[i] = lmin;
total[i] = lcount;
pindex.push_back(i);
}
else if( rcount > 0 )
{
minn[i] = rmin;
total[i] = rcount;
nindex.push_back(i);
}
else
{
if( lmin < lminmin ) lminmin = lmin;
if( rmin < rminmin ) rminmin = rmin;
}
}
if(check(pindex, nindex, lminmin, rminmin))
{
cout << "Yes";
}
else
{
cout << "No";
}
}
| 1 |
#include<iostream>
#include<cstdio>
using namespace std;
int main(){
long long Min,Max,sum=0,n,m;
scanf("%lld%lld",&n,&Max);
Min=sum=Max;
for(int i=1;i<n;i++){
scanf("%lld",&m);
if(m>Max) Max=m;
if(m<Min) Min=m;
sum+=m;
}
printf("%lld %lld %lld\n",Min,Max,sum);
return 0;
}
| #include<iostream>
using namespace std;
#include<stdio.h>
int main(){
int n;
scanf("%d\n", &n);
int i = 0;
int x, min, max;
long sum;
min = 1000000;
max = - 1000000;
sum = 0;
while (i <= n){
if(i == n){
printf("%d %d %ld\n", min, max, sum);
break;}
scanf("%d", &x);
if(x < min){
min = x;
}
if(max < x){
max = x;
}
sum += x;
i += 1;
}
return 0;
} | 1 |
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(var) cerr << (#var) << " = " << (var) << endl;
#else
#define debug(var)
#endif
void init() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
}
void solve() {
int primes[] = {3, 13, 23, 43, 53, 73, 83, 103, 113, 163, 173, 193, 223, 233, 263, 283, 293, 313, 353, 373, 383, 433, 443, 463, 503, 523, 563, 593, 613, 643, 653, 673, 683, 733, 743, 773, 823, 853, 863, 883, 953, 983, 1013, 1033, 1063, 1093, 1103, 1123, 1153, 1163, 1193, 1213, 1223, 1283, 1303};
int n; scanf("%d", &n);
for (int i = 0; i < n; ++i) {
printf("%d ", primes[i]);
}
}
int main() {
init();
int t = 1; //scanf("%d", &t);
while (t--) {
solve();
}
return 0;
}
| // 2020-08-26 16:05:35
// clang-format off
#include<bits/stdc++.h>
#ifdef LOCAL
#include "lib/debug.hpp"
#else
#define debug(...) 1
#endif
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#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>;
using P = pair<int, int>;
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; }
// clang-format on
void answer() {
int n;
cin >> n;
vector<int> p(55555 + 55, 1), ans;
p[0] = p[1] = 0;
REP(i, 2, 55555 + 5) if (p[i]) {
for (int j = 2 * i; j < 55555 + 5; j += i) p[j] = 0;
if (i % 5 == 1) {
ans.push_back(i);
if (ans.size() == n) break;
}
}
rep(i, ans.size()) {
if (i) cout << ' ';
cout << ans[i];
}
cout << '\n';
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(0);
answer();
return 0;
} | 1 |
// God put a smile upon your face <3
#include <bits/stdc++.h>
#define slld(longvalue) scanf("%lld", &longvalue)
#define ll long long
#define ull unsigned long long
#define pll pair < long long, long long >
#define fastio ios_base:: sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define pb push_back
#define bug printf("BUG\n")
#define mxlld LLONG_MAX
#define mnlld -LLONG_MAX
#define mxd 2e8
#define mnd -2e8
#define pi 3.14159265359
using namespace std;
bool check(ll n, ll pos)
{
return n & (1LL << pos);
}
ll Set(ll n, ll pos)
{
return n = n | (1LL << pos);
}
ll dp[200005][4];
vector < ll > vec;
const ll mn = -1e18;
ll solve(ll pos, ll skip)
{
ll &ret = dp[pos][skip];
if(ret != mn) return ret;
if(pos >= vec.size())
{
if(vec.size() & 1)
if(skip == 2) return ret = mn + 2;
return ret = 0;
}
ret = solve(pos + 2, skip) + vec[pos];
if(skip) ret = max(ret, solve(pos + 1, skip - 1));
return ret;
}
int main()
{
ll i, j, k, l, m, n, o, r, q;
ll testcase;
ll input, flag, tag, ans;
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
while(cin >> n)
{
vec.clear();
vec.resize(n);
for(ll i = 0; i < n; i++) cin >> vec[i];
for(ll i = 0; i <= n + 3; i++) for(ll j = 0; j < 4; j++) dp[i][j] = mn;
ans = solve(0,(n % 2) + 1);
cout << ans << "\n";
}
}
| #include <stdio.h>
#include <assert.h>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <array>
#include <vector>
#include <queue>
#include <set>
//#include <cmath>
#include <unordered_map>
//#include <unordered_set>
//#include <boost/container/static_vector.hpp>
//#include <boost/unordered_set.hpp>
//#include <boost/unordered_map.hpp>
//#include <unistd.h>
//#include <cv.h>
//#include <highgui.h>
#include <stdlib.h>
#include <time.h>
#include <string>
const int MAX_N = 2050;
const int MAX_Q = 200000;
int N, M, Q;
char S[MAX_N][MAX_N];
int x1[MAX_Q], y1[MAX_Q], x2[MAX_Q], y2[MAX_Q];
int ac_node[MAX_N][MAX_N] = {};
int ac_edge_hor[MAX_N][MAX_N] = {};
int ac_edge_ver[MAX_N][MAX_N] = {};
int main(int argc, char **argv) {
std::cin >> N >> M >> Q;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= M; j++) {
std::cin >> S[i][j];
}
}
for (int i = 1; i <= Q; i++) {
std::cin >> x1[i] >> y1[i] >> x2[i] >> y2[i];
}
for (int i = 1; i <= N; i++) {
int ac_tmp = 0;
for (int j = 1; j <= M; j++) {
if (S[i][j] == '1') {
ac_tmp++;
}
ac_node[i][j] = ac_node[i-1][j] + ac_tmp;
}
int ac_tmp_edge_hor = 0;
int ac_tmp_edge_ver = 0;
for (int j = 1; j <= M; j++) {
if (S[i][j] == '1' && S[i][j-1] == '1') {
ac_tmp_edge_ver++;
}
if (S[i][j] == '1' && S[i-1][j] == '1') {
ac_tmp_edge_hor++;
}
ac_edge_ver[i][j] = ac_edge_ver[i-1][j] + ac_tmp_edge_ver;
ac_edge_hor[i][j] = ac_edge_hor[i-1][j] + ac_tmp_edge_hor;
}
}
for (int i = 1; i <= Q; i++) {
int node_num = ac_node[x2[i]][y2[i]] - ac_node[x2[i]][y1[i] - 1] - ac_node[x1[i] - 1][y2[i]] + ac_node[x1[i] - 1][y1[i] - 1];
int edge_num_hor = ac_edge_hor[x2[i]][y2[i]] - ac_edge_hor[x2[i]][y1[i] - 1] - ac_edge_hor[x1[i]][y2[i]] + ac_edge_hor[x1[i]][y1[i] - 1];
int edge_num_ver = ac_edge_ver[x2[i]][y2[i]] - ac_edge_ver[x2[i]][y1[i]] - ac_edge_ver[x1[i] - 1][y2[i]] + ac_edge_ver[x1[i] - 1][y1[i]];
//std::cout << "check:" << node_num << " " << edge_num << std::endl;
std::cout << node_num - edge_num_hor - edge_num_ver << std::endl;
}
return 0;
} | 0 |
# 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<cstdio>
#include<iostream>
#include<string>
#include<algorithm>
#include<stdio.h>
#include<locale.h>
#include<set>
#include<stack>
#include<queue>
#include<math.h>
using namespace std;
typedef long long int ll;
int main()
{
int N,M;
scanf("%d %d",&N,&M);
int ans=1;
for(int i=1;i<=min(100000,M);i++)
{
if(M%i==0)
{
//約数はiとM/i
if(i>=N){ans=max(ans,M/i);}
if((M/i)>=N){ans=max(ans,i);}
}
}
printf("%d",ans);
} | 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=998244353;
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));}
vector<ll> pw2(51,1);vector<ll> pw10(19,1);
ll fac[500001],finv[500001],inv[500001];
void table() {
fac[0]=fac[1]=1;
finv[0]=finv[1]=1;
inv[1]=1;
for(ll i=2;i<500001;i++){
fac[i]= fac[i-1]*i%MOD;
inv[i]=MOD-inv[MOD%i]*(MOD/i)%MOD;
finv[i]=finv[i-1]*inv[i]%MOD;
}
}
ll dev(long long a,long long b){
return (a*inv[b])%MOD;
}
ll comb(ll n, ll k){
if(n<k) return 0;
if(n<0 || k<0) return 0;
return fac[n]*(finv[k]*finv[n-k]%MOD)%MOD;
}
ll mod_pow(ll x,ll n,ll mod){
ll res=1;
while(n>0){
if(n&1) res=res*x%mod;
x=x*x%mod;
n>>=1;
}
return res;
}
int main(){
table();
{rep1(i,50) pw2[i]=2*pw2[i-1];}
{rep1(i,18) pw10[i]=10*pw10[i-1];}
ll N,M,K; cin>>N>>M>>K;
ll ans=0;
rep(i,K+1){
ans+=(((M*comb(N-1,i))%MOD)*mod_pow(M-1,N-i-1,MOD))%MOD;
ans%=MOD;
}
cout<<ans<<endl;
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
int mod_pow(int n, int e, int mod){
int ans = 1;
n = n % mod;
while(e != 0){
if(e % 2 == 1){
ans = (ans * n) % mod;
}
e /= 2;
n = (n * n) % mod;
}
return ans;
}
int mod_inv(int n, int mod){
return mod_pow(n, mod-2, mod);
}
int mod_combination(int n, int k, int mod){
if(n < k || k < 0) return 0;
if(n-k < k) k = n-k;
int ans = 1;
for(int i=1; i<=k; i++){
ans = (((ans * (n+1-i)) % mod) * mod_inv(i, mod)) % mod;
}
return ans;
}
signed main(){
// cout << fixed << setprecision(10) << flush;
int mod = 998244353;
int n, m, k;
cin >> n >> m >> k;
int ans = 0;
int combi = 1;
if(m == 1){
if(k == n-1){
cout << 1 << endl;
}
else{
cout << 0 << endl;
}
return 0;
}
int m_1_pow = mod_pow(m-1, n-1, mod);
int m_1_inv = mod_inv(m-1, mod);
for(int l=0; l<=k; l++){
ans = (ans + (((combi * m_1_pow) % mod) * m) % mod) % mod;
m_1_pow = (m_1_pow * m_1_inv) % mod;
combi = (((combi * (n-1-l)) % mod) * mod_inv(l+1, mod)) % mod;
}
cout << ans << endl;
return 0;
} | 1 |
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
using namespace std;
int main(void)
{
int n, m;
while (cin >> n >> m, n || m){
vector <int> p(n + 1);
for (int i = 0; i < n; i++){
scanf("%d", &p[i]);
}
p[n] = 0;
n++;
vector <int> sum(n * n);
int cnt = 0;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++){
sum[cnt] = p[i] + p[j];
// cout << sum[cnt] << " " << p[i] << " " << p[j] << endl;
cnt++;
}
}
sort(sum.begin(), sum.end());
int ret = 0;
for (int i = 0; i < n * n; i++){
int diff = m - sum[i];
if (diff < 0) continue;
int up = *(upper_bound(sum.begin(), sum.end(), diff)-1);
ret = max(ret, sum[i] + up);
}
cout << ret << endl;
}
return 0;
} | #include <bits/stdc++.h>
typedef long long LL;
using namespace std;
inline int rd() {
char ch=getchar(); int i=0,f=1;
while(!isdigit(ch)) {if(ch=='-')f=-1; ch=getchar();}
while(isdigit(ch)) {i=(i<<1)+(i<<3)+ch-'0'; ch=getchar();}
return i*f;
}
const int N=2e5+50;
const LL INF=0x3f3f3f3f3f3f3f3f;
int n,q;
LL bit[2][N],tag,mn=0;
inline void inc(LL *a,int i,LL v) {for(;i<=n;i+=(i&(-i))) a[i]=min(a[i],v);}
inline LL ask(LL *a,int i,LL v=INF) {for(;i;i-=(i&(-i))) v=min(a[i],v); return v;}
int main() {
n=rd(), q=rd();
int a=rd(), b=rd();
memset(bit[0],0x3f,sizeof(bit[0]));
memset(bit[1],0x3f,sizeof(bit[1]));
inc(bit[0],a,-a); inc(bit[1],n-a+1,a);
for(int i=1;i<=q;i++) {
int x=rd();
LL v=min(ask(bit[0],x)+x,ask(bit[1],n-x+1)-x);
tag+=abs(x-b); v-=abs(x-b); mn=min(mn,v);
inc(bit[0],b,v-b); inc(bit[1],n-b+1,v+b);
b=x;
}
printf("%lld\n",mn+tag);
} | 0 |
#include <bits/stdc++.h>
#define rep(i,n) for (ll i = 0; i < n; ++i)
#define ALL(c) (c).begin(), (c).end()
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
#define FORV(i, v) for (auto i = v.begin(); i != v.end(); i++)
using namespace std;
typedef long long ll;
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; }
template<class T> inline void dump(vector<T> v) { for (auto& x : v) cerr << x << " "; cerr << endl; }
template<class T> inline void dump(vector<pair<T, T>> v) { for (auto& p : v) cerr << p.first << " " << p.second << endl; }
template<class T> inline void dump(vector<vector<T>> vv) { for (auto& v : vv) {for (auto& x : v) cerr << x << " "; cerr << endl;} }
const ll INF = 1e9;
const long long INFL = 1LL<<60;
int main() {
int m;
cin >> m;
map<ll, ll> mp;
rep(i, m) {
ll d, c;
cin >> d >> c;
mp[d] += c;
}
ll ans = 0;
bool flag = true;
while (flag) {
flag = false;
rep(i, 10) {
if (mp[i] > 1) flag = true;
ll num = mp[i] / 2;
ans += num;
mp[i] -= num * 2;
if (i + i < 10) mp[i+i] += num;
else {
mp[1] += num;
mp[(i + i) % 10] += num;
}
}
}
vector<ll> a;
rep(i, 10) {
if (mp[i] != 0) a.push_back(i);
}
while (a.size() > 1) {
int x = a.back(); a.pop_back();
int y = a.back(); a.pop_back();
if (x + y < 10) a.push_back(x + y);
else {
a.push_back(1);
a.push_back((x + y) % 10);
}
ans++;
}
cout << ans << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using piii = pair<int, pair<int, int>>;
using pll = pair<ll, ll>;
using pli = pair<ll, int>;
using pil = pair<int, ll>;
template <typename T>
using Graph = vector<vector<T>>;
const int MOD = 1e9 + 7;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int M;
cin >> M;
ll ans = 0, sum = 0;
for (int i = 0; i < M; ++i) {
int d;
ll c;
cin >> d >> c;
sum += d * c;
ans += c;
}
ans += (sum - 1) / 9;
cout << ans - 1 << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
#define pp pair<int,int>
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define ll long long
ll MOD=1000000007;
int inf=1000000000;
ll INF=10000000000000000;
int main(){
int n;
ll x;
cin >> n >> x;
vector<ll> b(n),l(n),u(n);
vector<pair<ll,ll>> a(n);
ll uu=0;
rep(i,n){
cin >> b.at(i) >> l.at(i) >> u.at(i);
a.at(i).first=u.at(i)*(x-b.at(i))+l.at(i)*b.at(i);
a.at(i).second=i;
uu+=l.at(i)*b.at(i);
}
sort(a.begin(),a.end());
reverse(a.begin(),a.end());
ll sum=0,v;
rep(i,n){
sum+=a.at(i).first;
if (sum>uu) {sum-=a.at(i).first;v=i;break;}
if (sum==uu) {cout << (i+1)*x << endl;return 0;}
}
ll ans=INF;
rep(i,n){
ll y=sum;
int p=a.at(i).second;
if (i<v) y=sum-a.at(i).first+a.at(v).first;
ll t=uu-y+(u.at(p)-l.at(p))*b.at(p);
ll o=t/u.at(p);
if (t%u.at(p)!=0) o++;
ans=min(ans,v*x+o);
t=uu-y;
o=t/l.at(p);
if (t%l.at(p)!=0) o++;
ans=min(ans,v*x+o);
}
cout << ans << endl;
} | /*{{{*/
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#include<bitset>
#include<vector>
#include<limits.h>
#include<assert.h>
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define REP(I, N) for (int I = 0; I < (N); ++I)
#define REPP(I, A, B) for (int I = (A); I < (B); ++I)
#define FOR(I, A, B) for (int I = (A); I <= (B); ++I)
#define FORS(I, S) for (int I = 0; S[I]; ++I)
#define RS(X) scanf("%s", (X))
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define CASET int ___T; scanf("%d", &___T); for(int cs=1;cs<=___T;cs++)
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<PII> VPII;
typedef pair<LL,LL> PLL;
typedef vector<PLL> VPLL;
template<class T> void _R(T &x) { cin >> x; }
void _R(int &x) { scanf("%d", &x); }
void _R(LL &x) { scanf("%lld", &x); }
void _R(double &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
template<class T> void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const LL &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T,class U> void _W(const pair<T,U> &x) {_W(x.F); putchar(' '); _W(x.S);}
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
#ifdef HOME
#define DEBUG(...) {printf("# ");printf(__VA_ARGS__);puts("");}
#else
#define DEBUG(...)
#endif
int MOD = 1e9+7;
void ADD(LL& x,LL v){x=(x+v)%MOD;if(x<0)x+=MOD;}
/*}}}*/
const int SIZE = 1e6+10;
int N,X;
LL b[SIZE],l[SIZE],u[SIZE];
bool used[SIZE];
int main(){
R(N,X);
LL need=0;
priority_queue<PLL>h0,h1,h2;
REP(i,N){
R(b[i],l[i],u[i]);
need+=b[i]*l[i];
h0.push({l[i]*b[i]+u[i]*(X-b[i]),i});
}
LL an=0;
while(need>=h0.top().F){
an+=X;
used[h0.top().S]=1;
need-=h0.top().F;
h0.pop();
}
if(need<=0){
W(an);
return 0;
}
int add_id=h0.top().S;
LL add_v=l[add_id]*b[add_id]+u[add_id]*(X-b[add_id]);
LL mi=1e18;
REP(i,N){
if(used[i]){
LL need2=need-add_v+l[i]*b[i]+u[i]*(X-b[i]);
if(l[i]*b[i]>=need2){
mi=min(mi,(need2+l[i]-1)/l[i]);
}
else{
mi=min(mi,(need2-l[i]*b[i]+u[i]-1)/u[i]+b[i]);
}
}
else{
if(l[i]*b[i]+u[i]*(X-b[i])<need)continue;
if(l[i]*b[i]>=need){
mi=min(mi,(need+l[i]-1)/l[i]);
}
else{
mi=min(mi,(need-l[i]*b[i]+u[i]-1)/u[i]+b[i]);
}
}
}
an+=mi;
W(an);
return 0;
}
| 1 |
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <math.h>
#include <string>
#include <string.h>
#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--)
using namespace std;
int dp[1001][1001];//dp[y][x];
//??????????????????
string x,y;
void solve(){
cin >> x >> y ;
memset(dp,10000,1001*1001);
REP(i,x.size()+1){
dp[0][i] = i;
}
REP(i,y.size()+1){
dp[i][0] = i;
}
FOR(i,1,x.size()+1){
FOR(j,1,y.size()+1){
if(x[i-1]==y[j-1]){
dp[j][i] = dp[j-1][i-1];
}
else {
dp[j][i] = min ( 1 + min(dp[j-1][i],dp[j][i-1]),dp[j-1][i-1]+1);
}
}
}
/*REP(i,y.size()+1){
REP(j,x.size()+1){
cout << dp[i][j] << " ";
}
cout << endl;
}
*/
cout << dp[y.size()][x.size()] << endl;
}
int main(){
solve();
} | #include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#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 <ctime>
#include <string>
#include <climits>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int MAX_N=55555;
bool is_prime[MAX_N];
int prime[MAX_N];
int main(){
int dp[1001][1001];
for(int nn=0;nn<1;nn++){
string s1,s2;
cin>>s1>>s2;
for(int i=0;i<1001;i++)fill(dp[i],dp[i]+1001,0);
for(int i=0;i<s1.size()+1;i++)dp[i][0]=i;
for(int i=0;i<s2.size()+1;i++)dp[0][i]=i;
for(int i=0;i<s1.size();i++){
for(int j=0;j<s2.size();j++){
int x=1;
if(s1[i]==s2[j]) x=0;
dp[i+1][j+1]=min(dp[i][j]+x,min(dp[i][j+1]+1,dp[i+1][j]+1));
}
}
cout<<dp[s1.size()][s2.size()]<<endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main(void){
int N, X, T, time;
cin >> N >> X >> T;
time = N/X;
if (N%X != 0) time += 1;
cout << time*T << endl;
}
| #include<iostream>
#include<algorithm>
#include<functional>
#include<cmath>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<deque>
using namespace std;
#define ll long long
const int mod = 1000000007;
const ll INF = 1000000000000000000;
int main()
{
int A[3], K;
cin >> A[0] >> A[1] >> A[2] >> K;
sort(A, A + 3);
int ans = A[0] + A[1];
for (int i = 0; i < K; i++) A[2] *= 2;
ans += A[2];
cout << ans << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(long long i=0;i<(long long)(n);i++)
#define REP(i,k,n) for(long long i=k;i<(long long)(n);i++)
#define all(a) a.begin(),a.end()
#define pb push_back
#define eb emplace_back
#define lb(v,k) (lower_bound(all(v),k)-v.begin())
#define ub(v,k) (upper_bound(all(v),k)-v.begin())
#define fi first
#define se second
#define pi M_PI
#define PQ(T) priority_queue<T>
#define SPQ(T) priority_queue<T,vector<T>,greater<T>>
#define dame(a) {out(a);return 0;}
#define decimal cout<<fixed<<setprecision(15);
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> PP;
typedef tuple<ll,ll,ll,ll> PPP;
typedef multiset<ll> S;
using vi=vector<ll>;
using vvi=vector<vi>;
using vvvi=vector<vvi>;
using vp=vector<P>;
using vvp=vector<vp>;
using vb=vector<bool>;
using vvb=vector<vb>;
const ll inf=1001001001001001001;
const int INF=1001001001;
const int mod=1000000007;
const double eps=1e-10;
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<'\n';}
template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';}
template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';}
template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';}
template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);}
template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);}
template<class T> void yesno(T b){if(b)out("yes");else out("no");}
template<class T> void YesNo(T b){if(b)out("Yes");else out("No");}
template<class T> void YESNO(T b){if(b)out("YES");else out("NO");}
template<class T> void noyes(T b){if(b)out("no");else out("yes");}
template<class T> void NoYes(T b){if(b)out("No");else out("Yes");}
template<class T> void NOYES(T b){if(b)out("NO");else out("YES");}
void outs(ll a,ll b){if(a>=inf-100)out(b);else out(a);}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
ll modpow(ll a,ll b){a%=mod;if(b==0)return 1;if(b&1)return a*modpow(a,b-1)%mod;ll k=modpow(a,b/2);return k*k%mod;}
ll ans=0;
const int l=20;
vi dep;
vvi table;
vvi g;
void dfs0(int i,int p,int d){
table[i][0]=p;
dep[i]=d;
for(ll x:g[i])if(x!=p)dfs0(x,i,d+1);
}
void init(ll n){
g=vvi(n);
dep=vi(n);
table=vvi(n,vi(l,-1));
rep(i,n-1){
ll a;cin>>a;a--;
g[a].pb(i+1);
g[i+1].pb(a);
}
dfs0(0,-1,0);
}
vb done;
void dfs(int i){
done[i]=true;
for(ll x:g[i])if(x!=table[i][0]){
if(!done[x])dfs(x);
}
}
int main(){
ll n,k,t;cin>>n>>k>>t;
if(t!=1)ans++;
if(k==1){
rep(i,n-1){
ll a;cin>>a;if(a!=1)ans++;
}
dame(ans);
}
init(n);
REP(i,1,l){
rep(j,n){
ll id=table[j][i-1];
if(id!=-1)table[j][i]=table[id][i-1];
}
}
k--;
vp v(n);
rep(i,n)v[i]=P(dep[i],i);
sort(all(v));reverse(all(v));
done=vb(n,false);
rep(tt,n){
ll i=v[tt].se;
if(dep[i]<=k+1)break;
if(done[i])continue;
ll w=i;
rep(j,l)if(k>>j&1)w=table[w][j];
ans++;
dfs(w);
}
out(ans);
} | //#include<bits/stdc++.h>
#include<cstdio>
#include<algorithm>
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 N, K;
int kotae;
int ne[100001], H[100001], D;
int dfs(int A) {
int d = 1;
D++;
for (int v = H[A]; v; v = ne[v]) {
d = max(d, dfs(v) + 1);
}
D--;
if (d >= K && D > 1) {
kotae++;
return 0;
}
else return d;
}
const int cm = 1 << 17;
char cn[cm], * ci = cn + cm, ct;
inline char getcha() {
if (ci - cn == cm) { fread(cn, 1, cm, stdin); ci = cn; }
return *ci++;
}
inline int getint() {
int A = 0;
if (ci - cn + 16 > cm) while ((ct = getcha()) >= '0') A = A * 10 + ct - '0';
else while ((ct = *ci++) >= '0') A = A * 10 + ct - '0';
return A;
}
int main() {
//cin.tie(0);
//ios::sync_with_stdio(false);
N = getint();
K = getint();
if (getint() != 1) kotae++;
for (int i = 2; i <= N; i++) {
int a = getint();
ne[i] = H[a];
H[a] = i;
}
dfs(1);
printf("%d", kotae);
Would you please return 0;
} | 1 |
#include <bits/stdc++.h>
#define PI 3.14159265359
#define rep(i,a,n) for(int i=a;i<(int)n;++i)
#define SZ(x) ((int)(x).size()) //size() unsigned -> int
#define descSort(a) sort(a.begin(),a.end(),std::greater<int>())
using namespace std;
typedef long long ll;
const ll INF = 1e9 + 7;
ll gcd(ll x,ll y){
if(x%y==0)return y;
return gcd(y,x%y);
}
ll LCM(int a, int b){
return a*b/gcd(a,b);
}
int main(void)
{
int n;
cin>>n;
map<ll,int,greater<int>> a;
rep(i,0,n){
int in;
cin>>in;
a[in]++;
}
vector<ll> num;
ll ans=0;
for(auto i:a){
if(i.second>=4){
ans=max(ans,i.first*i.first);
}
if(i.second>=2){
num.push_back(i.first);
}
if(num.size()==2){
ans=max(ans,num[0]*num[1]);
}
}
cout<<ans<<endl;
return 0;
} | #include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
ll inf = 1000000007;
int main() {
ll n;
cin >> n;
vector<ll>a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
sort(a.begin(), a.end());
ll x = 0,y = 0;
ll index = 0;
for (int i = n - 1; i > 0; i--) {
if (a[i] == a[i - 1]) {
x = a[i];
index = i - 2;
break;
}
}
for (int i = index; i > 0; i--) {
if (a[i] == a[i - 1]){
y = a[i];
break;
}
}
cout << x * y << endl;
return 0;
}
| 1 |
//kruskal tree
#include <cstdio>
#include <algorithm>
using namespace std;
#define M 100000
int parent[M],a[M],b[M];
pair<int,int>node[M];
int root(int a){return parent[a]==a?a:parent[a]=root(parent[a]);}
int unite(int a,int b){
int x=root(a),y=root(b);
if(x==y)return 0;
parent[x]=y;
return 1;
}
int main(){
int i,s,n,m;
for(;scanf("%d%d",&n,&m)>1;printf("%d\n",s)){
for(i=0;i<m;i++)scanf("%d%d%d",a+i,b+i,&node[i].first),node[i].second=i;
sort(node,node+m);
for(i=0;i<n;i++)parent[i]=i;
for(s=i=0;i<m;i++)if(unite(a[node[i].second],b[node[i].second]))s+=node[i].first;
}
} | #include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
struct edge{
int from,to,weight;
};
bool asc(const edge &e,const edge &f){
return e.weight < f.weight;
}
int init(vector<int> &U){
for (int i=0;i<U.size();i++){
U[i]=i;
}
}
int root(vector<int> &U,int x){
if(x==U[x]){
return x;
}else{
return U[x]=root(U,U[x]);
}
}
void unite(vector<int> &U,int x,int y){
int px=root(U,x);
int py=root(U,y);
U[px]=py;
}
bool same(vector<int> &U,int x,int y){
return root(U,x)==root(U,y);
}
int main(){
int V,E,s,t,w;
cin >> V >> E;
vector<int> U(V);
init(U);
vector<edge> EdgeList(E);
for (int i=0;i<E;i++){
edge e;
cin >> e.from >> e.to >> e.weight;
EdgeList[i]=e;
}
sort(EdgeList.begin(),EdgeList.end(),asc);
int W=0;
for (int i=0;i<E;i++){
edge e=EdgeList[i];
if(same(U,e.from,e.to)){
continue;
}else{
unite(U,e.from,e.to);
W+=e.weight;
}
}
cout << W << endl;
return 0;
} | 1 |
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define MAX(a,b) ((a)>(b)?(a):(b))
int main()
{
int N;
long long M;
cin >> N >> M;
while(N!=0 && M!=0) {
vector<long long> p;
long long ans=0;
for(int i=0; i<N; ++i) {
long long temp;
cin >> temp;
p.push_back(temp);
ans=MAX(ans,temp);
}
sort(p.begin(),p.end());
for(int i=0; i<N; ++i) {
for(int j=i; j<N; ++j) {
if(p[i]+p[j]>M) break;
p.push_back(p[i]+p[j]);
ans=MAX(ans,p[i]+p[j]);
}
}
sort(p.begin(),p.end());
for(int i=0; i<(int)p.size(); ++i) {
auto itr=upper_bound(p.begin(),p.end(),M-p[i]);
if(itr==p.begin()) break;
else --itr;
ans=MAX(ans,p[i]+*itr);
}
cout << ans << endl;
cin >> N >> M;
p.clear();
}
return 0;
} | #include<bits/stdc++.h>
using namespace std;
int main(){
while(1){
int m,nmin,nmax,ans=0,max=0,p[200]={};
cin>>m>>nmin>>nmax;
if(m+nmin+nmax==0)break;
for(int i=0;i<m;i++)cin>>p[i];
for(int i=nmin;i<=nmax;i++)
if(p[i-1]-p[i]>=max){
max=p[i-1]-p[i];
ans=i;
}
cout<<ans<<endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1001001001;
const int MOD = 1000000007;
template <typename T>
void print(const T &v);
int main()
{
int m;
cin >> m;
ll total = 0;
ll cnt = 0;
for (int i = 0; i < m; i++)
{
ll d, c;
cin >> d >> c;
total += d * c;
if (total >= 10)
{
cnt += total / 9;
total %= 9;
if (total == 0)
{
--cnt;
total = 9;
}
}
cnt += c;
}
cout << cnt - 1 << endl;
return 0;
}
// Use For Debug
template <typename T>
void print(T const &v)
{
for (int i = 0; i < v.size(); i++)
{
if (i)
cout << ' ';
cout << v[i];
}
cout << endl;
} | #include <bits/stdc++.h>
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
using ll = long long;
using P = pair<int,int>;
using graph = vector<vector<int>>;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const ll INF = 1LL<<60;
const ll mod = 1000000007LL;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main(){
vector<vector<P>> v(10);
rep(i,10){
v[i].push_back(make_pair(0,0));
v[i].push_back(make_pair(0,i));
}
rep(i,10){
while(true){
P p = *v[i].rbegin();
int t = i*10 + p.second;
int cnt = 0;
while(t>=10){
t = t/10+t%10;
cnt++;
}
v[i].push_back(make_pair(p.first+cnt,t));
if(t==i) break;
}
}
v[0][2].first = 1;
ll M;
cin>>M;
vector<ll> d(M),c(M);
rep(i,M) cin>>d[i]>>c[i];
ll ans = 0;
vector<ll> rest(M);
rep(i,M){
ll t = d[i],s=c[i];
ll l = v[t].size();
ans+=v[t][l-1].first*((s-1)/(l-2));
s = s - ((s-1)/(l-2))*(l-2);
ans += v[t][s].first;
rest[i]=v[t][s].second;
}
ll cnt = 0;
while(cnt!=M-1){
while(rest[cnt]!=0){
ll a = rest[cnt],b=rest[cnt+1];
rest[cnt] = (a+b)/10;
rest[cnt+1] = (a+b)%10;
ans++;
}
cnt++;
}
cout<<ans<<endl;
} | 1 |
#include <iostream>
#include <queue>
#define INF 1000000000
#define NV 100000
using namespace std;
class edge
{
public:
int t;
int d;
edge *nextedge;
edge(int,int);
};
edge::edge(int tt,int dd)
{
t=tt;
d=dd;
nextedge=NULL;
}
class vertice
{
public:
edge *firstedge;
void create_edge(int,int);
};
void vertice::create_edge(int t,int d)
{
edge *newedge=new edge(t,d);
if(firstedge==NULL)
firstedge=newedge;
else
{
edge *tmp=firstedge;
while(tmp->nextedge!=NULL)
{
tmp=tmp->nextedge;
}
tmp->nextedge=newedge;
}
}
class source
{
public:
int n;
int dis;
friend bool operator<(source,source);
};
bool operator<(source a, source b)
{
return a.dis>b.dis;
}
int main()
{
int v,e,r;
cin>>v>>e>>r;
source weight[v];
vertice vertices[v];
for(int i=0;i<v;i++)
{
vertices[i].firstedge=NULL;
weight[i].n=i;
weight[i].dis=INF;
}
for(int i=0;i<e;i++)
{
int s,t,d;
cin>>s>>t>>d;
vertices[s].create_edge(t,d);
}
weight[r].dis=0;
int current_vertex=r;
priority_queue<source> queue;
queue.push(weight[r]);
while(queue.size()!=0)
{
source del=queue.top();
queue.pop();
current_vertex=del.n;
edge *neighbor=vertices[current_vertex].firstedge;
while(neighbor!=NULL)
{
int vn=neighbor->t;
if(weight[vn].dis>weight[current_vertex].dis+neighbor->d)
{
weight[vn].dis=weight[current_vertex].dis+neighbor->d;
queue.push(weight[vn]);
}
neighbor=neighbor->nextedge;
}
}
for(int i=0;i<v;i++)
if(weight[i].dis>=INF)
cout<<"INF"<<endl;
else
cout<<weight[i].dis<<endl;
} | #include <vector>
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#define INF 100000000
#define ll long long
using namespace std;
class Bit{
private:
ll *par, *rk, size;
public:
Bit(ll size){
par = new ll[size];
rk = new ll[size];
for(ll i = 0; i < size; i++){
par[i] = i;
rk[i] = 0;
}
}
ll find(ll x){
if(x == par[x])
return x;
else
return par[x] = find(par[x]);
}
void merge(ll x,ll y){
x = find(x);
y = find(y);
if(rk[x] > rk[y]){
par[y] = x;
}else if(rk[x] < rk[y]){
par[x] = y;
}else{
par[y] = x;
rk[x]++;
}
}
bool same(ll x, ll y){
return find(x) == find(y);
}
};
int main(){
ll V, E;
cin >> V >> E;
Bit bit(V);
priority_queue< pair<ll, pair<ll,ll> >, vector< pair<ll, pair<ll,ll> > >, greater< pair<ll, pair<ll,ll> > > > pq;
for(ll i = 0; i < E; i++){
ll s,t,w;
cin >> s >> t >> w;
pq.push(make_pair(w, make_pair(s,t)));
}
ll cost = 0;
for(ll i = 0; i < V - 1; i++){
while(1){
if(bit.same(pq.top().second.first, pq.top().second.second)){
pq.pop();
}else{
break;
}
}
bit.merge(pq.top().second.first, pq.top().second.second);
cost += pq.top().first;
}
cout << cost << endl;
return 0;
} | 0 |
#include<bits/stdc++.h>
using namespace std;
int main() {
int a, b;
cin >> a >> b;
if (a > b) {
cout << 2*a-1 << "\n";
}
else if (b > a) {
cout << 2*b-1 << "\n";
}
else {
cout << a + b << "\n";
}
return 0;
} | #include<iostream>
using namespace std;
int main()
{
int a,b,ans=0;
cin>>a>>b;
for(int i=0;i<2;i++)
{
if(a>=b)
{
ans+=a;
a--;
}
else if(b>a)
{
ans+=b;
b--;
}
}
cout<<ans;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll R[400009];
string s;
cin >> s;
ll n = s.size();
map<char, int>mp;
for (int i = 0; i < n; i++) {
mp[s[i]]++;
}
ll ans = n*(n-1)/2;
for (auto& p : mp) {
ll v = p.second;
ans -= v*(v-1)/2;
}
cout << ans + 1 << endl;
}
| #include<bits/stdc++.h>
#define mo 1000000007
#define pi 3.1415926535898
using namespace std;
long long read(){
long long xx=0,flagg=1;
char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')
ch=getchar();
if(ch=='-'){
flagg=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
xx=xx*10+ch-'0';
ch=getchar();
}
return xx*flagg;
}
void pus(long long xx,long long flagg){
if(xx<0){
putchar('-');
xx=-xx;
}
if(xx>=10)
pus(xx/10,0);
putchar(xx%10+'0');
if(flagg==1)
putchar(' ');
if(flagg==2)
putchar('\n');
return;
}
long long ksm(long long u,long long v){
long long o=1;
while(v){
if(v&1)
o=o*u%mo;
u=u*u%mo;
v>>=1;
}
return o;
}
int n,k,i,x,ans,f[100005];
int top,nex[200005],to[200005],fir[100005];
void lj(int u,int v){
top++;
nex[top]=fir[u];
fir[u]=top;
to[top]=v;
}
void ss(int u,int v){
for(int top1=fir[v];top1;top1=nex[top1]){
ss(v,to[top1]);
f[v]=max(f[to[top1]],f[v]);
}
if(v==1)
return;
f[v]++;
if(f[v]==k&&u!=1){
ans++;
f[v]=0;
}
}
int main(){
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
n=read();k=read();
x=read();
if(x!=1)
ans++;
for(i=2;i<=n;i++){
x=read();
lj(x,i);
}
ss(1,1);
pus(ans,2);
return 0;
} | 0 |
/**
* @copyright (c) 2020 Daisuke Hashimoto
*/
#include <bits/stdc++.h>
using namespace std;
using Pair = pair<int64_t, int64_t>;
constexpr int64_t kInf = INT64_MAX / 2L;
// std::cout << std::setprecision(20) << 1.1 << endl;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int64_t N, M;
cin >> N >> M;
for (int64_t g = M / N; g >= 1; --g) {
if (M % g == 0 && M / g >= N) {
cout << g << endl;
return 0;
}
}
cout << 1 << endl;
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
vector < int > v;
bool check(int n, int m, int mid){
int x=(m-mid);
long long int y=(n-1)*1LL*mid;
return (x>=y && x%mid==0);
///return (m/mid>=n && m%mid==0);
}
int cal(int n, int m){
int ans=0;
for(int i=1; i*i<=m; i++){
if(m%i==0){
if(check(n,m,i))ans=max(ans,i);
if(check(n,m,m/i))ans=max(ans,m/i);
}
}
return ans;
}
int main(){
int n,m;
cin>>n>>m;
int ans=cal(n,m);
cout<<ans<<endl;
return 0;
}
| 1 |
#include <iostream>
#include <string>
using namespace std;
int main() {
int a;
cin >> a;
if(a < 1200) cout << "ABC\n";
else if(a < 2800) cout << "ARC\n";
else cout << "AGC\n";
}
| #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
typedef long long ll;
template<typename T> bool chmax(T &a, T b) {if(a <= b){a = b; return true;}return false;}
template<typename T> bool chmin(T &a, T b) {if(a >= b){a = b; return true;}return false;}
#define ABC 001
#define ARC 010
#define AGC 100
int main(void){
int r;
cin >> r;
int frag = AGC;
if(r < 1200){
frag |= ABC;
frag |= ARC;
}else if( r < 2800){
frag |= ARC;
}
if(frag & ABC)cout << "ABC" << endl;
else if(frag & ARC)cout << "ARC" << endl;
else cout << "AGC" << endl;
} | 1 |
#include <cstdlib>
#include <string>
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
string str[2];
for (int i = 0; i < 2; i++)
{
getline(cin, str[i]);
// cout << "Input: " << str[i] << endl;
}
str[1] += ' ';
int num = atoi(str[0].c_str());
// cout << "Input Number: " << num << endl;
long* a = new long[num];
stringstream ss;
// ss = stringstream(str[1]);
ss << str[1];
string buff;
int n = 0;
while (getline(ss, buff, ' '))
{
if (n >= num) break;
a[n] = atoi(buff.c_str());
// cout << "Input Number: " << a[n] << endl;
n++;
}
for (int x = 0; x < num - 1; x++)
{
for (int y = 0; y < num - 1 - x; y++)
{
if (a[y] > a[y+1])
{
swap(a[y], a[y+1]);
}
}
// for (int i = 0; i < num; i++)
// {
// cout << "Step " << x << ": " << a[i] << endl;
// }
}
long min = a[0];
long max = a[num - 1];
long long sum = 0;
for (int i = 0; i < num; i++)
{
sum += a[i];
// cout << "Sorted Number: " << a[i] << endl;
}
delete [] a;
cout << min << " " << max << " " << sum << endl;
return 0;
}
| #include<cstdio>
#include<algorithm>
#define Rint register int
using namespace std;
typedef long long LL;
const int N = 200003;
int n;
LL ans, d, a[N], f[N], g[N];
int main(){
scanf("%d%lld", &n, &d);
for(Rint i = 1;i <= n;i ++){
scanf("%lld", a + i);
ans += a[i];
}
f[1] = a[1];
for(Rint i = 2;i <= n;i ++) f[i] = min(f[i - 1] + d, a[i]);
g[n] = a[n];
for(Rint i = n - 1;i;i --) g[i] = min(g[i + 1] + d, a[i]);
for(Rint i = 2;i < n;i ++) ans += min(f[i], g[i]);
printf("%lld\n", ans + (n - 1) * d);
} | 0 |
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double distance(double, double, double, double);
int main(){
double x1 ,y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
cout << fixed << setprecision(12);
cout << distance(x1, y1, x2, y2) << endl;
}
double distance(double x1, double y1, double x2, double y2){
double sumsq;
sumsq = (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
return pow(sumsq, 0.5);
}
| #include <bits/stdc++.h>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
int A, B, C;
cin >> A >> B;
C=max((A+B),(A-B));
cout << max(C,(A*B)) << endl;
}
| 0 |
#include<algorithm>//sort,二分探索,など
#include<bitset>//固定長bit集合
#include<cmath>//pow,logなど
#include<complex>//複素数
#include<deque>//両端アクセスのキュー
#include<functional>//sortのgreater
#include<iomanip>//setprecision(浮動小数点の出力の誤差)
#include<iostream>//入出力
#include<iterator>//集合演算(積集合,和集合,差集合など)
#include<map>//map(辞書)
#include<numeric>//iota(整数列の生成),gcdとlcm(c++17)
#include<queue>//キュー
#include<set>//集合
#include<stack>//スタック
#include<string>//文字列
#include<unordered_map>//イテレータあるけど順序保持しないmap
#include<unordered_set>//イテレータあるけど順序保持しないset
#include<utility>//pair
#include<vector>//可変長配列
using namespace std;
typedef long long ll;
//マクロ
//forループ関係
//引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか
//Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=(ll)(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=(ll)(b);i--)
//xにはvectorなどのコンテナ
#define ALL(x) (x).begin(),(x).end() //sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size()) //sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x)) //最大値を求める
#define MIN(x) *min_element(ALL(x)) //最小値を求める
//定数
#define INF 9000000000000000000 //9e18:極めて大きい値
#define MOD 998244353
#define MAXR 100000 //10^5:配列の最大のrange(素数列挙などで使用)
#define LOGMAXR 18 // floor(log2(10^5)) + 1: 配列の最大のrangeのlog
//略記
//#define PB push_back //vectorヘの挿入
//#define MP make_pair //pairのコンストラクタ
//#define F first //pairの一つ目の要素
//#define S second //pairの二つ目の要素
ll modpow(ll n, ll p){
ll ret = 1;
while(p){
if(p & 1){
ret *= n;
ret %= MOD;
}
p >>= 1;
n = n * n;
n %= MOD;
}
return ret;
}
map<ll, ll> modinv_memo;
ll modinv(ll n) {
if(modinv_memo.count(n)) return modinv_memo[n];
// ll b = MOD, u = 1, v = 0;
// while (b) {
// ll t = n / b;
// n -= t * b; swap(n, b);
// u -= t * v; swap(u, v);
// }
// u %= MOD;
// if (u < 0) u += MOD;
//modinv_memo[n] = u;
ll ret = modpow(n, MOD-2);
modinv_memo[n] = ret;
return ret;
}
int main(){
ll N, M, K;
cin >> N >> M >> K;
if(M == 1){
if(K == N - 1){
cout << 1 << endl;
}else{
cout << 0 << endl;
}
return 0;
}
ll ret = 0;
ll it = (M * modpow(M - 1, N - 1)) % MOD;
REP(k, K + 1){
ret += it;
ret %= MOD;
it *= modinv(k + 1);
it %= MOD;
it *= (N - 1 - k);
it %= MOD;
it *= modinv(M - 1);
it %= MOD;
}
cout << ret << endl;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll P = 998244353;
ll f(ll b, ll n) {if (n <= 0) return 1; auto p = f(b, n / 2); return p * p % P * (n % 2 ? b : 1) % P;}
ll ms[200001];
int main()
{int N, M, K, ans = 0; cin >> N >> M >> K;
ms[0] = 1; for (int r = 1; r <= K; ++r) ms[r] = ms[r - 1] * (N - 1 - r + 1) % P * f(r, P - 2) % P;
for (int k = 0; k <= K; ++k) ans = (ans + ms[k] * M % P * f(M - 1, N - k - 1) % P) % P;
cout << ans << endl;} | 1 |
#include <iostream>
using namespace std;
int main()
{
string S;
cin>>S;
char a=S[0]=='R';
char b=S[1]=='R';
char c=S[2]=='R';
if(a&b&c)
{
cout<<"3";
}
else if(a&b||b&c)
{
cout<<"2";
}
else if(a|b|c)
{
cout<<"1";
}
else
{
cout<<"0";
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
int n;
char x[5];
int main() {
scanf("%s", x);
int ans = 0, cur = 0;
for (int i = 0; x[i]; i++)
if (x[i] == 'R')
ans = max(ans, ++cur);
else
cur = 0;
printf("%d\n", ans);
}
| 1 |
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod=1e9+7;
ll n,m,k;
ll poww(ll x,ll k)
{
ll base=x,ans=1;
while(k)
{
if(k&1)ans=ans*base%mod;
base=base*base%mod;
k>>=1;
}
return ans;
}
ll nok(ll x)
{
ll ans=1;
for(int i=1;i<=x;i++)ans=ans*i%mod;
return ans;
}
int main()
{
ll ans=0,dlt;
scanf("%lld %lld %lld",&n,&m,&k);
dlt=nok(n*m-2)*poww(nok(k-2),mod-2)%mod*poww(nok(n*m-k),mod-2)%mod;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
ans=(ans+(1+n-i)*(n-i)/2*m%mod*dlt%mod)%mod;
ans=(ans+(1+m-j)*(m-j)/2*n%mod*dlt%mod)%mod;
}
}
printf("%lld\n",ans);
return 0;
} | #include "bits/stdc++.h"
#define MOD 1000000007
#define rep(i, n) for(ll i=0; i < (n); i++)
#define rrep(i, n) for(ll i=(n)-1; i >=0; i--)
#define ALL(v) v.begin(),v.end()
#define rALL(v) v.rbegin(),v.rend()
#define FOR(i, j, k) for(ll i=j;i<k;i++)
#define DUMP(i, v)for(ll i=0;i<v.size();i++)cerr<<v[i]<<" "
using namespace std;
typedef long long int ll;
typedef vector<ll> llvec;
typedef vector<double> dvec;
typedef pair<ll, ll> P;
typedef long double ld;
struct edge{ll x, c;};
ll mod(ll a, ll mod){
ll res = a%mod;
while(res<0)res=res + mod;
return res;
}
ll modpow(ll a, ll n, ll mod){
ll res=1;
while(n>0){
if(n&1) res=res*a%mod;
a=a*a%mod;
n>>=1;
}
return res;
}
ll modinv(ll a, ll mod){
return modpow(a, mod-2, mod);
}
ll gcd(ll a, ll b){
ll r = a%b;
if(r==0) return b;
else return gcd(b, a%b);
}
bool is_prime(ll n){
ll i = 2;
if(n==1)return false;
if(n==2)return true;
bool res = true;
while(i*i <n){
if(n%i==0){
res = false;
}
i = i+1;
}
//if(i==1)res = false;
if(n%i==0)res=false;
return res;
}
struct UnionFind{
ll N;
llvec p;
llvec cnt;
UnionFind(ll n){
N = n;
p=llvec(N);
cnt=llvec(N, 0);
rep(i, N){
p[i] = i;
cnt[i] = 1;
}
}
void con(ll a, ll b){
P at = root(a);
P bt = root(b);
if(at.second!=bt.second){
if(at.first>bt.first){
swap(at, bt);
swap(a, b);
}
p[at.second] = bt.second;
cnt[bt.second]+=cnt[at.second];
p[a]=bt.second;
}
}
P root(ll a){
ll atmp = a;
ll c=0;
while(atmp!=p[atmp]){
atmp = p[atmp];
c++;
}
p[a] = atmp;
return {c, atmp};
}
bool is_con(ll a, ll b){
P at=root(a);
P bt=root(b);
return at.second == bt.second;
}
};
struct dijkstra{
ll N;
llvec d;
vector<vector<edge>> e;
dijkstra(ll n){
N = n;
//d = llvec(N, 1e18);
e = vector<vector<edge>>(N);
}
void add_edge(ll from, ll to, ll cost){
e[from].push_back({to, cost});
}
void run(ll start){
priority_queue<P, vector<P>, greater<P>> que;
que.push({0, start});
d = llvec(N, 1e18);
d[start]=0;
while(!que.empty()){
P q = que.top();que.pop();
ll dc = q.first;
ll x = q.second;
if(dc>d[x]){
continue;
}else{
for(auto ip: e[x]){
if(d[ip.x]<=d[x]+ip.c){
continue;
}else{
d[ip.x]= d[x]+ip.c;
que.push({d[ip.x], ip.x});
}
}
}
}
}
};
ll fact[500000];
ll ifact[500000];
void gen(){
fact[0]=ifact[0]=1;
rep(i, 200100){
fact[i+1]=mod(fact[i]*(i+1), MOD);
ifact[i+1]=modinv(fact[i+1], MOD);
}
}
ll comb(ll n, ll k){
return mod(mod(fact[n]*ifact[n-k], MOD)*ifact[k], MOD);
}
/**************************************
** A main function starts from here **
***************************************/
int main(){
ll N, M, K;
cin >> N >> M >> K;
ll ans = 0;
gen();
rep(i, N*M){
ll x = i/N+1;
ll y = i/M+1;
ans =mod(ans+ (M-x)*(M-x+1)/2*N, MOD);
ans =mod(ans+ (N-y)*(N-y+1)/2*M, MOD);
}
cout << mod(ans*comb(N*M-2, K-2),MOD)<<endl;
return 0;
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
int main(){
long h,w;cin>>h>>w;
long b=0;
vector<string>S(h);for(long i=0;i<h;i++){
cin>>S[i];
for(char c:S[i])if(c=='#')b++;
}
vector<long>D(w*h,-1);D[0]=0;
queue<long>q;q.push(0);
while(q.size()){
long f=q.front();q.pop();
if(f/w-1>=0&&D[f-w]==-1&&S[f/w-1][f%w]!='#'){
D[f-w]=D[f]+1;q.push(f-w);
}
if(f/w+1<h&&D[f+w]==-1&&S[f/w+1][f%w]!='#'){
D[f+w]=D[f]+1;q.push(f+w);
}
if(f%w-1>=0&&D[f-1]==-1&&S[f/w][f%w-1]!='#'){
D[f-1]=D[f]+1;q.push(f-1);
}
if(f%w+1<w&&D[f+1]==-1&&S[f/w][f%w+1]!='#'){
D[f+1]=D[f]+1;q.push(f+1);
}
}
cout<<(D[w*h-1]!=-1?(w*h)-D[w*h-1]-b-1:-1)<<endl;
} | /**
* created: 25.07.2020 06:24:27
**/
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
// using bint = boost::multiprecision::cpp_int;
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define pcnt(bit) __builtin_popcountll(bit)
template<class T> bool chmax(T &a, const T &b) {if (a < b) {a = b; return 1;} return 0;}
template<class T> bool chmin(T &a, const T &b) {if (b < a) {a = b; return 1;} return 0;}
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int dx2[8]={0, 1, 0, -1, 1, 1,-1, -1};
const int dy2[8]={1, 0,-1, 0, 1, -1, 1, -1};
const long double pi = acos(-1.0);
const int MAX = 1000010;
const int INF = 1LL << 60;
const int MOD = 1000000007;
// const int MOD = 998244353;
void gridbfs(int i,int j,vector<string> s,vector<vector<int>> &dis) {
int h = s.size(), w = s[0].size();
queue<vector<int>> q; q.push({i,j});
dis[i][j] = 0;
while (!q.empty()) {
auto v = q.front(); q.pop();
if (v[0] > 0 && s[v[0] - 1][v[1]] == '.' && dis[v[0] - 1][v[1]] == -1) {
dis[v[0] - 1][v[1]] = dis[v[0]][v[1]] + 1;
q.push({v[0] - 1, v[1]});
}
if (v[1] > 0 && s[v[0]][v[1] - 1] == '.' && dis[v[0]][v[1] - 1] == -1) {
dis[v[0]][v[1] - 1] = dis[v[0]][v[1]] + 1;
q.push({v[0], v[1] - 1});
}
if (v[0] < h - 1 && s[v[0] + 1][v[1]] == '.' && dis[v[0] + 1][v[1]] == -1) {
dis[v[0] + 1][v[1]] = dis[v[0]][v[1]] + 1;
q.push({v[0] + 1, v[1]});
}
if (v[1] < w - 1 && s[v[0]][v[1] + 1] == '.' && dis[v[0]][v[1] + 1] == -1) {
dis[v[0]][v[1] + 1] = dis[v[0]][v[1]] + 1;
q.push({v[0], v[1] + 1});
}
}
return;
}
signed main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int h, w; cin >> h >> w;
vector<string> v(h);
int bcnt = 0;
rep(i,h) cin >> v[i];
rep(i,h) rep(j,w) if (v[i][j] == '#') bcnt++;
vector<vector<int>> dis(h,vector<int>(w,-1));
gridbfs(0,0,v,dis);
if (dis[h-1][w-1] == -1) cout << -1 << endl;
else cout << h * w - bcnt - dis[h-1][w-1] - 1 << endl;
return 0;
} | 1 |
// Abu Hurayra (HurayraIIT)
// IIT, Jahangirnagar University
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <deque>
#include <bitset>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <math.h>
#include <stdio.h>
#include <string.h>
using namespace std;
typedef long long LL;
typedef long long int int64;
typedef unsigned long long int uint64;
typedef vector<int> vi;
#define READ freopen("input.txt", "r", stdin);
#define WRITE freopen("output.txt", "w", stdout);
#define fast_io ios_base::sync_with_stdio(0); cin.tie(0);
#define sz(a) int((a).size())
#define space " "
#define all(x) (x).begin(), (x).end()
#define endl '\n'
#define pi acos(-1.0)
#define MP make_pair
#define PB push_back
#define INF (int)1e9
#define MOD 1000000007
#define PRECISION(x) cout << fixed << setprecision(x);
int main()
{
fast_io
string s ;
cin >> s ;
int a = s[0]-'0', b = s[1]-'0' , c = s[2]-'0' , d = s[3]-'0' ;
if(a+b+c+d==7) cout << a << "+" << b << "+" << c << "+" << d << "=7\n";
else if(a-b-c-d==7) cout <<a<<"-"<<b<<"-"<<c<<"-"<<d<< "=7\n";
else if(a+b+c-d==7) cout <<a<<"+"<<b<<"+"<<c<<"-"<<d<< "=7\n";
else if(a+b-c+d==7) cout <<a<<"+"<<b<<"-"<<c<<"+"<<d<< "=7\n";
else if(a-b+c+d==7) cout <<a<<"-"<<b<<"+"<<c<<"+"<<d<< "=7\n";
else if(a-b-c+d==7) cout <<a<<"-"<<b<<"-"<<c<<"+"<<d<< "=7\n";
else if(a-b+c-d==7) cout <<a<<"-"<<b<<"+"<<c<<"-"<<d<< "=7\n";
else if(a+b-c-d==7) cout <<a<<"+"<<b<<"-"<<c<<"-"<<d<< "=7\n";
return 0;
}
| #define _USE_MATH_DEFINES // M_PI等のフラグ
#include <iostream>
#include <bitset>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <map>
#include <vector>
#include <list>
#define MOD 1000000007
#define COUNTOF(array) (sizeof(array)/sizeof(array[0]))
using namespace std;
void solve(){
string s;
cin >> s;
for (int bit=0; bit<(1<<3); bit++) {
string ans = s.substr(0,1);
int result = s.at(0) - '0';
for (int i=0; i<3; i++) {
if (bit>>i&1) {
// iビット目が1なら+演算する
result += s.at(i+1)-'0';
ans += "+";
}
else {
// iビット目が0なら-演算する
result -= s.at(i+1)-'0';
ans += "-";
}
ans.push_back(s.at(i+1));
}
if (result == 7) {
cout << ans << "=7" << endl;
return;
}
}
}
int main(int argc, char const *argv[]){
solve();
return 0;
} | 1 |
#include<bits/stdc++.h>
#include<atcoder/all>
#define REP(i,a,b) for(int i=(a);i<(b);i++)
#define rep(i,n) REP(i,0,(n))
using namespace std;
using namespace atcoder;
using ll = long long;
int main() {
int N, Q, a, b, c;
cin >> N >> Q;
fenwick_tree<ll> fw(N);
rep(i, N) {
cin >> a;
fw.add(i, a);
}
rep(i, Q) {
cin >> a >> b >> c;
if (a) {
cout << fw.sum(b, c) << endl;
} else {
fw.add(b, c);
}
}
} | #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vpii vector<pair<int,int>>
#define vpll vector<pair<ll,ll>>
#define fr(i,k,n) for (int i = k; i < n; ++i)
#define fri(i,k,n) for (int i = k; i >= n; --i)
#define INF (int)1e9
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define all(arr) arr.begin(),arr.end()
void boost(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
const int N=5e5+5;
ll tree[N];
ll read(int idx) {
ll sum = 0;
while (idx > 0) {
sum += tree[idx];
idx -= (idx & -idx);
}
return sum;
}
void update(int idx, ll val) {
while (idx <N) {
tree[idx] += val;
idx += (idx & -idx);
}
}
void solve(){
int n,q;
cin>>n>>q;
int x;
fr(i,1,n+1){
cin>>x;
update(i,x);
}
int t,l,r;
while(q--){
cin>>t>>l>>r;
if(t==0){
update(l+1,r);
}
else{
cout<<read(r)-read(l)<<endl;
}
}
return;
}
int main()
{
boost();
int tc=1;
//cin>>tc;
while(tc--)
solve();
return 0;
} | 1 |
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
char a[105][105];
int f[2][105][105][105],line[105][105],list[105][105],n,m;
void upd(int &x,int w){
x=(x<w)?w:x;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%s",a[i]+1);
int x,y;
for(int i=1;i<=n;i++){
for(int p=1;p<=m;p++){
if(a[i][p]=='E') x=i,y=p;
line[i][p]=line[i][p-1]+(a[i][p]=='o');
list[i][p]=list[i-1][p]+(a[i][p]=='o');
}
}
int pre=0,nxt=1,ans=0;
for(int i=0;i<=n-x;i++,pre^=1,nxt^=1){
memset(f[nxt],0,sizeof(f[nxt]));
for(int p=0;p<x;p++){
for(int j=0;j<=m-y;j++){
for(int k=0;k<y;k++){
int w=f[pre][p][j][k],W;
if(x+i+1<=n-p) W=max(0,line[x+i+1][min(m-k,y+j)]-line[x+i+1][max(j,y-k-1)]);
else W=0;
upd(f[nxt][p][j][k],w+W);
if(x-p-1>i) W=max(0,line[x-p-1][min(m-k,y+j)]-line[x-p-1][max(j,y-k-1)]);
else W=0;
upd(f[pre][p+1][j][k],w+W);
if(y+j+1<=m-k) W=max(0,list[min(n-p,x+i)][y+j+1]-list[max(i,x-p-1)][y+j+1]);
else W=0;
upd(f[pre][p][j+1][k],w+W);
if(y-k-1>j) W=max(0,list[min(n-p,x+i)][y-k-1]-list[max(i,x-p-1)][y-k-1]);
else W=0;
upd(f[pre][p][j][k+1],w+W);
ans=max(ans,f[pre][p][j][k]);
}
}
}
}
printf("%d",ans);
return 0;
} | #include <iostream>
#include <vector>
using namespace std;
void solve()
{
int N, M;
cin >> N >> M;
vector<int> A(N);
vector<int> B(M);
for(int i = 0; i < N; ++i)
{
cin >> A[i];
}
for(int i = 0; i < M; ++i)
{
cin >> B[i];
}
vector<int> point(N);
int max = 0;
for(int i = 0; i < M; ++i)
{
for(int j = 0; j < N; ++j)
{
if(B[i] >= A[j])
{
++point[j];
if(max < point[j])
{
max = point[j];
}
break;
}
}
}
for(int i = 0; i < M; ++i)
{
if(max == point[i])
{
cout << i + 1 << endl;
}
}
}
int main()
{
solve();
return(0);
} | 0 |
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<cmath>
#include<cstdio>
#include<queue>
#include<deque>
#include<map>
#include<stack>
#include<set>
#include<tuple>
using namespace std;
typedef pair<int,int> ii;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef unsigned long long int ull;
const int INF=1e9;
const ll MOD=1e9+7;
int dy[]={1,0,-1,0};
int dx[]={0,1,0,-1};
const int MAXN=2e5;
int h,w;
vector<vector<char> > a(1000,vector<char>(1000));
vector<vector<int> > d(1000,vector<int>(1000,INF));
int main(){
cin>>h>>w;
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
cin>>a[i][j];
}
}
int ans=0;
queue<ii> que;
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
if(a[i][j]=='#'){
d[i][j]=0;
que.push(make_pair(i,j));
}
}
}
while(!que.empty()){
ii p=que.front();que.pop();
int y=p.first,x=p.second;
for(int i=0;i<4;i++){
int ny=y+dy[i],nx=x+dx[i];
if(ny>=0&&ny<h&&nx>=0&&nx<w&&d[ny][nx]>d[y][x]+1){
d[ny][nx]=d[y][x]+1;
que.push(make_pair(ny,nx));
ans=max(ans,d[ny][nx]);
}
}
}
cout<<ans<<endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
//#define int long long
//signed main(){
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int h,w;
cin >> h >> w;
vector<bool> a(h*w,false);
queue<int> que;
for(int i = 0; i < h; i++) {
string tmp;
cin >> tmp;
for(int j = 0; j < w; j++) {
int idx=i*w+j;
if(tmp[j]=='#'){
a[idx]=true;
que.push(idx);
}
}
}
int cnt=0;
int ans=0;
while(cnt<h*w) {
int n=que.size();
cnt+=n;
for(int i = 0; i < n; i++) {
int idx=que.front();
int add[4]={1,-1,w,-w};
for(int j = 0; j < 4; j++) {
int t=idx+add[j];
int r=idx/w+add[j]/w;
int c=idx%w+add[j]%w;
if(r<0||r>=h||c<0||c>=w||a[t]==true) continue;
a[t]=true;
que.push(t);
}
que.pop();
}
ans++;
}
cout << ans-1 << "\n";
return 0;
} | 1 |
#include<bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (int)(n); i++)
#define put(i) cout<<fixed<<i<<endl
using namespace std;
using ll = long long;
bool is_integer(double x){
return std::floor(x) == x;
}
int main(){
ll n; cin >> n;
while(true){
if(is_integer(sqrt(n))){
put(n);
return 0;
}
n--;
}
} | #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(){
ll n;
cin >> n;
ll ans = 0;
ans += n/11*2;
if(n%11 != 0) ans += (n%11-1)/6 + 1;
cout << ans << endl;
return 0;
} | 0 |
#include <iostream>
#include <string>
#include <regex>
using namespace std;
int main() {
string input;
cin >> input;
regex pattern("^A[a-z]+C[a-z]+$");
if (regex_match(input, pattern))
{
cout << "AC" << endl;
}
else
{
cout << "WA" << endl;
}
return 0;
}
| #define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin>>s;
bool b=true;
int cnt=0,x;
if(s[0]!='A') b=false;
for(int i=2;i<s.size()-1;i++){
if(s[i]=='C'){
cnt++;
x=i;
}
}
if(cnt!=1) b=false;
for(int i=1;i<s.size();i++){
if(i==x) continue;
if(s[i]<'a' || s[i]>'z') b=false;
}
if(b) cout<<"AC"<<endl;
else cout<<"WA"<<endl;
return 0;
} | 1 |
#include <vector>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <algorithm>
#include <stack>
#include <queue>
#include <list>
const long long int INF = 6000000000;
const int MOD = (int)1e9 + 7;
using ll = long long;
using namespace std;
typedef pair<int,int> P;
struct edge{
int to;
int cost;
};
int V,E;
long long d[200][200];
int main(){
cin >> V >> E;
for( int i = 0; i < V; ++i){
for( int j = 0; j < V; ++j){
d[i][j] = INF;
}
}
for( int i = 0; i < V; ++i){
d[i][i] = 0;
}
for( int i = 0; i < E; ++i){
int a,b,c;
cin >> a >> b >> c;
d[a][b] = c;
}
for( int k = 0; k < V; ++k){
for( int i = 0; i < V; ++i){
for( int j = 0; j < V; ++j){
d[i][j] = min( d[i][j], d[i][k]+d[k][j]);
}
}
}
bool f = false;
for( int i = 0; i < V; ++i){
if( d[i][i] < 0 ){
f = true;
}
}
if(f){
cout << "NEGATIVE CYCLE" << endl;
}else{
for( int i = 0; i < V; ++i){
for( int j = 0; j < V; ++j){
if(j) cout << " ";
if( d[i][j] >= 3000000000 ){
cout << "INF";
}else{
cout << d[i][j];
}
}
cout << endl;
}
}
} | #include<stdio.h>
int GCD(int a, int b) {
int A, B, buff;
// A >= B
if (b > a) {
A = b; B = a;
}
else { A = a; B = b; }
while (A%B) {
buff = B;
B = A % B;
A = buff;
}
return B;
}
int main() {
int N, a, b, c;
int G;
int GC[1000000];
scanf("%d", &N);
if (!(N - 2)) {
scanf("%d %d", &a, &b);
G = GCD(a, b);
for (int i = 1; i <= G; i++) {
if (a%i == 0 && b%i == 0) {
GC[i] = i;
}
else GC[i] = -i;
}
for (int j = 1; j <= G; j++) {
if (GC[j] < 0)continue;
printf("%d\n", GC[j]);
}
}
else {
scanf("%d %d %d", &a, &b, &c);
G = GCD(c, GCD(a, b));
for (int i = 1; i <= G; i++) {
if (a%i == 0 && b%i == 0 && c%i == 0) {
GC[i] = i;
}
else GC[i] = -i;
}
for (int j = 1; j <= G; j++) {
if (GC[j] < 0)continue;
printf("%d\n", GC[j]);
}
}
return 0;
}
| 0 |
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
#define rrep(i,n) for(int i=n-1;i>=0;--i)
#define yesno(flg) if(flg){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}
#define MAX_N 100001
#define i197 1000000007
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P1;
typedef pair<int,int> Pi;
typedef pair<double,Pi> Pdi;
typedef pair<ll,int>Pli;
typedef pair<int,ll>Pil;
typedef pair<P1,ll> P2;
typedef list<int> li;
typedef list<li> lli;
typedef pair<string,string> Ps;
const ll INF=1000000000000000001;
struct edge{int to,cost;};
int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};
struct Road{double cost;int a,b;};
struct pos{
// 1 変数を入れる;
ll a;
int b;
};
int main(){
int d,t,s;
cin>>d>>t>>s;
if(t*s>=d)cout<<"Yes"<<endl;
else cout<<"No"<<endl;
return 0;
}
| #include <iostream>
using namespace std;
template<class T> bool chmax(T &a, T b) {
if (a < b) a = b; return true;
return false;
}
void solve(long long A, long long B){
long long ans = -1000001;
chmax(ans, A + B);
chmax(ans, A - B);
chmax(ans, A * B);
cout << ans << '\n';
}
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
long long A;
scanf("%lld",&A);
long long B;
scanf("%lld",&B);
solve(A, B);
return 0;
}
| 0 |
#include <cstdio>
#include <algorithm>
using i8 = std::int8_t;
using u8 = std::uint8_t;
using i16 = std::int16_t;
using u16 = std::uint16_t;
using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
using usize = std::size_t;
i32 a[200000];
auto main() -> i32 {
i32 n, k;
scanf("%d%d", &n, &k);
for (i32 i = 0; i < n; i += 1) {
scanf("%d", a + i);
}
i32 b[200001];
for (i32 step = 0; step < std::min(64, k); step += 1) {
std::fill(b, b + n + 1, 0);
for (i32 i = 0; i < n; i += 1) {
b[std::max(0, i - a[i])] += 1;
b[std::min(n, i + a[i] + 1)] -= 1;
}
a[0] = b[0];
for (i32 i = 1; i < n; i += 1) {
b[i] += b[i - 1];
a[i] = b[i];
}
}
for (i32 i = 0; i < n; i += 1) {
printf("%d%c", a[i], i == n - 1 ? '\n' : ' ');
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define mp make_pair
#define ld long double
typedef pair<int, int> pii;
typedef priority_queue<int, vector<int>, greater<int> > small_heap;
typedef priority_queue<int> big_heap;
const int N = 2e5 + 100;
int T;
int n, k, a[N];
bool work() {
static int s[N];
memset(s, 0, sizeof(s));
for (int i = 1; i <= n; i++) {
int l = max(1, i - a[i]), r = min(n, i + a[i]);
s[l]++, s[r + 1]--;
}
bool res = false;
for (int i = 1; i <= n; i++) {
s[i] += s[i - 1];
if (s[i] != a[i]) {
res = true;
a[i] = s[i];
}
}
return res;
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = 1; i <= k; i++)
if (!work()) break;
for (int i = 1; i <= n; i++)
printf("%d ", a[i]);
printf("\n");
return 0;
} | 1 |
#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
char s[300020];
int t[8][2];
int n,f[300020][8];
inline void pre(){
t[0][0]=2;t[0][1]=1;
t[1][0]=3;t[1][1]=5;
t[2][0]=4;t[2][1]=7;
t[3][0]=6;t[3][1]=1;
t[4][0]=2;t[4][1]=2;
t[5][0]=5;t[5][1]=5;
t[6][0]=3;t[6][1]=3;
t[7][0]=2;t[7][1]=1;
}
int main() {
int i,j;
pre();
scanf("%s",s);
n=strlen(s);
f[n][1]=1;
f[n][5]=1;
for(i=n-1;i>=0;--i){
for(j=0;j<8;++j){
if(s[i]!='1')
f[i][j]+=f[i+1][t[j][0]];
if (s[i]!='0')
f[i][j]+=f[i+1][t[j][1]];
f[i][j]%=mod;
}
}
printf("%d\n", f[0][0]);
} | //%std
#include <bits/stdc++.h>
#define debug(...) fprintf(stderr, __VA_ARGS__)
using namespace std;
const int N = 300003, mod = 1e9 + 7;
void upd (int&a, int b) { a = (a + b) % mod; }
char s[N];
int n, dp[N][3][3];
int main ()
{
// freopen ("x.in", "r", stdin);
scanf("%s", s + 1);
n = strlen(s + 1);
dp[0][0][0] = 1;
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < 3; ++j) {//1
for (int k = 0; k < 3; ++k) {//0
if (s[i] != '0') {
if (!k) upd (dp[i][min (j + 1, 2)][k], dp[i - 1][j][k]);
else upd (dp[i][j][k - 1], dp[i - 1][j][k]);
}
if (s[i] != '1') {
if (k == 2) upd (dp[i][j][1], dp[i - 1][j][k]);
else upd (dp[i][j][k + 1], dp[i - 1][j][k]);
}
}
}
}
int ans = 0;
for (int i = 0; i < 3; ++i) for (int j = 0; j <= i; ++j) upd (ans, dp[n][i][j]);
printf ("%d\n", ans);
// printf ("%d\n", dp[5][0][1]);
return 0;
} | 1 |
#include <iostream>
#include <algorithm>
using namespace std;
#define repeat(i,n) for (int i = 0; i < (n); ++i)
#define repeat_from(i,m,n) for (int i = m; i < (n); ++i)
#define MAX_S 1000
int dp[2][MAX_S+1];
int main() {
string s1, s2;
getline(cin, s1);
getline(cin, s2);
int *cur = dp[0]; repeat (i,s2.size()+1) cur[i] = i;
int *prv = dp[1];
repeat_from (i,1,s1.size()+1) {
swap(cur,prv);
cur[0] = prv[0] + 1;
repeat_from (j,1,s2.size()+1) {
cur[j] = min(cur[j-1]+1,
min(prv[j]+1,
prv[j-1]+(s1[i-1]==s2[j-1]?0:1)));
}
}
cout << cur[s2.size()] << endl;
return 0;
} | #include<iostream>
#include<string>
#include<algorithm>
using namespace std;
#define IOI 101
#define IOIOIOIOI 101010101
int dp[10 * IOI][10 * IOI];
string s, t;
int main() {
for (int i = 0; i < 10 * IOI; i++) {
for (int j = 0; j < 10 * IOI; j++) {
dp[i][j] = IOIOIOIOI;
}
}
cin >> s; cin >> t;
for (int i = 0; i <= s.size(); i++)dp[i][0] = i;
for (int i = 0; i <= t.size(); i++)dp[0][i] = i;
for (int i = 0; i < s.size(); i++) {
for (int j = 0; j < t.size(); j++) {
dp[i + 1][j + 1] = min(dp[i][j], min(dp[i + 1][j], dp[i][j + 1])) + 1;
if (s[i] == t[j]) dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j]);
}
}
cout << dp[s.size()][t.size()] << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int mul[2] = {233, 19260817};
const int mod[2] = {998244353, 1000000007};
#define pb push_back
int n;
vector <int> a, b, na, nb, pw[2], hsh[2];
int get(int l, int r, int k) {return (hsh[k][r] + mod[k] - 1LL * pw[k][r - l] * hsh[k][l] % mod[k]) % mod[k];}
int main() {
ios :: sync_with_stdio(0);
cin >> n;
for (int i = 0; i < n; ++ i) {int x; cin >> x; a.pb(x);}
for (int i = 0; i < n; ++ i) {int x; cin >> x; b.pb(x);}
for (int i = 0; i < n; ++ i) na.pb(a[i] ^ a[(i + 1) % n]);
for (int i = 0; i < n; ++ i) na.pb(na[i]);
for (int i = 0; i < n; ++ i) nb.pb(b[i] ^ b[(i + 1) % n]);
int hb[2];
for (int k = 0; k < 2; ++ k) {
pw[k].pb(1); for (int i = 1; i <= n * 2; ++ i) pw[k].pb(1LL * mul[k] * pw[k].back() % mod[k]);
hsh[k].pb(0); for (int i = 0; i < n * 2; ++ i) hsh[k].pb((1LL * mul[k] * hsh[k].back() + na[i]) % mod[k]);
hb[k] = 0; for (int i = 0; i < n - 1; ++ i) hb[k] = (1LL * mul[k] * hb[k] + nb[i]) % mod[k];
}
for (int i = 0; i < n; ++ i)
if (get(i, i + n - 1, 0) == hb[0] && get(i, i + n - 1, 1) == hb[1] && na[i + n - 1] == nb[n - 1])
cout << i << " " << (a[i] ^ b[0]) << "\n";
return 0;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll mod = 1000000007;
const ll mod2 = 998244353;
const int inf = 1e9 + 10;
const ll INF = 1e18;
const ld EPS = 1e-10;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
vector<int> mp(vector<int> s) {
int n = s.size();
vector<int> ret(n + 1);
ret[0] = -1;
int j = -1;
rep(i, n) {
while (j >= 0 && s[i] != s[j]) j = ret[j];
ret[i + 1] = ++j;
}
return ret;
}
vector<int> kmp(vector<int> str, vector<int> word) {
vector<int> table = mp(word), ret;
int m = 0, i = 0, n = str.size();
while (m + i < n) {
if (word[i] == str[m + i]) {
if (++i == (int)(word.size())) {
ret.push_back(m);
m = m + i - table[i];
i = table[i];
}
}
else {
m = m + i - table[i];
if (i > 0) i = table[i];
}
}
return ret;
}
int main() {
int n;
cin >> n;
vector<int> a(n), b(n);
rep(i, n) cin >> a[i];
rep(i, n) cin >> b[i];
vector<int> c(n * 2), d(n);
rep(i, n) {
c[i] = a[i] ^ a[(i + 1) % n];
c[i + n] = c[i];
}
rep(i, n) d[i] = b[i] ^ b[(i + 1) % n];
vector<int> res = kmp(c, d);
rep(i, res.size()) {
int k = res[i];
if (k >= n) break;
int x = a[k] ^ b[0];
cout << k << " " << x << endl;
}
return 0;
} | 1 |
#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
#include <utility>
using namespace std;
template <class T>
using gp_queue=priority_queue<T, deque<T>, less<T>>;
using Weight=long long;
static const Weight INF=1LL<<57;
struct Edge {
size_t src, dst;
Weight weight;
Edge() {}
Edge(size_t src, size_t dst, Weight weight=1):
src(src), dst(dst), weight(weight)
{}
};
bool operator<(const Edge &e, const Edge &f) {
if (e.weight != f.weight) {
return e.weight > f.weight;
} else {
return e.src!=f.src? e.src<f.src : e.dst<f.dst;
}
}
using Edges=vector<Edge>;
using Vertex=vector<Edge>;
using Graph=vector<Vertex>;
void join(Graph &g, size_t s, size_t d, Weight w=1) {
// non-directed
g[s].push_back(Edge(s, d, w));
g[d].push_back(Edge(d, s, w));
}
void connect(Graph &g, size_t s, size_t d, Weight w=1) {
// directed
g[s].push_back(Edge(s, d, w));
}
vector<vector<Weight>> shortest_distance(Graph g) {
/* Johnson */
size_t V=g.size();
vector<Weight> h(V);
for (size_t k=0; k<V; ++k)
for (size_t i=0; i<V; ++i)
for (const Edge &e: g[i])
if (h[e.dst] > h[e.src] + e.weight) {
h[e.dst] = h[e.src] + e.weight;
if (k == V-1)
return vector<vector<Weight>>();
}
for (size_t i=0; i<V; ++i)
for (Edge &e: g[i])
e.weight += h[e.src] - h[e.dst];
vector<vector<Weight>> d(V, vector<Weight>(V, INF<<1));
vector<vector<size_t>> prev(V, vector<size_t>(V, -1));
for (size_t s=0; s<V; ++s) {
gp_queue<Edge> q; q.push(Edge(s, s, 0));
d[s][s] = 0;
while (!q.empty()) {
Edge e=q.top(); q.pop();
if (~prev[s][e.dst]) continue;
prev[s][e.dst] = e.src;
for (const Edge &f: g[e.dst]) {
if (d[s][f.dst] > e.weight + f.weight) {
d[s][f.dst] = e.weight + f.weight;
q.push(Edge(f.src, f.dst, e.weight+f.weight));
}
}
}
for (size_t u=0; u<V; ++u)
d[s][u] += h[u] - h[s];
}
return d;
}
int main() {
size_t V, E;
scanf("%zu %zu", &V, &E);
Graph g(V);
for (size_t i=0; i<E; ++i) {
size_t s, t;
Weight d;
scanf("%zu %zu %lld", &s, &t, &d);
connect(g, s, t, d);
}
vector<vector<Weight>> d=shortest_distance(g);
if (d.empty())
return !printf("NEGATIVE CYCLE\n");
for (vector<Weight> w: d)
for (size_t i=0; i<V; ++i)
if (w[i] < INF) {
printf("%lld%c", w[i], i<V-1? ' ':'\n');
} else {
printf("INF%c", i<V-1? ' ':'\n');
}
return 0;
} | #include<iostream>
#include<queue>
#include<vector>
#include<functional>
using namespace std;
typedef pair<int,int> P; //現在コスト、島の番号
int n,k;
int cost[101][101];
int Dijkstra(int left,int get){
int done[101] = {0}; //確定後のコストを格納
int mincost[101];
for(int i = 0;i < 101;i++)
mincost[i] = 500000000;
priority_queue<P,vector<P>,greater<P> >que;
P edge;
que.push(P(0,left));
while(done[get] == 0 && !que.empty()){
edge.first = que.top().first;
edge.second = que.top().second;
que.pop();
//何度も同じ頂点にコストが更新される(pushされる)ことがあるので、確定後に残りのケースが作動しないようにする。
if(done[edge.second])
continue;
//確定後のコスト
done[edge.second] = edge.first;
//確定した頂点から枝を伸ばす(今までの最小を記録することで重複探索を減らす)
for(int i = 1;i <= n;i++){
if(cost[edge.second][i] > 0 && done[i] == 0 && mincost[i] > edge.first+cost[edge.second][i]){
mincost[i] = edge.first+cost[edge.second][i];
que.push(P(edge.first+cost[edge.second][i],i));
}
}
}
if(done[get])
return done[get];
return -1;
}
int mincost[50000],mcor = 0;
int main(){
int i,j,a,b,c,d;
while(true){
cin >> n >> k;
if(n == 0 && k == 0)
break;
for(i = 0;i < 101;i++){
for(j = 0;j < 101;j++){
cost[i][j] = 0;
}
}
for(i = 0;i < k;i++){
cin >> a;
if(a == 0){
cin >> b >> c;
mincost[mcor] = Dijkstra(b,c);
mcor++;
}
else{
cin >> b >> c >> d;
if(cost[b][c] == 0 || cost[b][c] > d)
cost[b][c] = d;
if(cost[c][b] == 0 || cost[c][b] > d)
cost[c][b] = d;
}
}
}
for(i = 0;i < mcor;i++){
cout << mincost[i] << endl;
}
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < n; ++i)
#pragma region Debug
template <typename T>
void view(const std::vector<T> &v)
{
for (const auto &e : v)
{
std::cout << e << " ";
}
std::cout << std::endl;
}
template <typename T>
void view(const std::vector<std::vector<T>> &vv)
{
for (const auto &v : vv)
{
view(v);
}
}
#pragma endregion
#pragma region chminmax
template <typename T>
inline bool chmin(T &a, T b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <typename T>
inline bool chmax(T &a, T b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
#pragma endregion
using vvll = vector<vll>;
const ll INF = 1e12;
int main()
{
string s;
cin >> s;
int n = s.size();
vvll dp(n + 1, vll(2, INF));
dp[0][0] = 0;
dp[0][1] = 1;
rep(i, n)
{
int x = s[i] - '0';
chmin(dp[i + 1][0], dp[i][0] + x);
chmin(dp[i + 1][1], dp[i][0] + x + 1);
chmin(dp[i + 1][0], dp[i][1] + (10 - x));
chmin(dp[i + 1][1], dp[i][1] + (10 - x) - 1);
}
cout << dp[n][0] << endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
template <typename T> void print(T t) { cout<<t<<endl; }
template<typename T, typename... Args> void print(T t, Args... args) { cout<<t<<" "; print(args...); }
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define int long long
#define double long double
int n;
string s;
int cache[1000004][3];
int dp(int pos,int carr)
{
if(pos==n)
return carr;
int &ans=cache[pos][carr];
if(ans!=-1)
return ans;
ans=1e18;
for(int i=0;i<10;i++)
{
int dig1=i;
int dig2=(i+carr+s[pos]-'0')%10;
ans=min(ans,dig1+dig2+dp(pos+1,(i+carr+s[pos]-'0')/10));
}
return ans;
}
int32_t main()
{
cin>>s;
reverse(s.begin(),s.end());
n=s.length();
memset(cache,-1,sizeof(cache));
print(dp(0,0));
}
| 1 |
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cctype>
#include <cassert>
#include <climits>
#include <string>
#include <bitset>
#include <cfloat>
#include <random>
#include <unordered_set>
#pragma GCC optimize("Ofast")
using namespace std;
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<pair<int,int> > vpii;
typedef vector<vector<int> > vvi;
typedef vector<vector<char> > vvc;
typedef vector<vector<string> > vvs;
typedef vector<vector<ll> > vvll;
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define irep(it, stl) for(auto it = stl.begin(); it != stl.end(); it++)
#define drep(i,n) for(int i = (n) - 1; i >= 0; --i)
#define fin(ans) cout << (ans) << '\n'
#define STLL(s) strtoll(s.c_str(), NULL, 10)
#define mp(p,q) make_pair(p, q)
#define pb(n) push_back(n)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define Sort(a) sort(a.begin(), a.end())
#define Rort(a) sort(a.rbegin(), a.rend())
#define MATHPI acos(-1)
#define itn int;
#define endl '\n';
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
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;}
inline string getline(){string s; getline(cin,s); return s;}
inline void yn(const bool b){b?fin("yes"):fin("no");}
inline void Yn(const bool b){b?fin("Yes"):fin("No");}
inline void YN(const bool b){b?fin("YES"):fin("NO");}
struct io{io(){ios::sync_with_stdio(false);cin.tie(0);}};
const int INF = INT_MAX;
const ll LLINF = 1LL<<60;
const ll MOD = 1000000007;
const double EPS = 1e-9;
int main() {
int n;
cin>>n;
string a,b,c;
cin>>a>>b>>c;
int ans=0;
rep(i,n){
if(a[i]==b[i]&&a[i]==c[i])continue;
if(a[i]==b[i]||a[i]==c[i]||c[i]==b[i])ans++;
else ans+=2;
}
fin(ans);
}
| #include <bits/stdc++.h>
using namespace std;
int main()
{int i, c{}; string A, B, C; cin >> i >> A >> B >> C;
while (i--) c += set<char>{A[i], B[i], C[i]}.size() - 1;
cout << c;} | 1 |
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n);i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef pair<int,int> P;
constexpr int mod = 1e9+7;
int dp[88][88][14000];
int main(){
int h, w;
cin >> h >> w;
vector<vector<int>> a(h, vector<int>(w)), b(h, vector<int>(w));
rep(i,h) rep(j,w) cin >> a[i][j];
rep(i,h) rep(j,w) cin >> b[i][j];
dp[0][0][7000 + (a[0][0] - b[0][0])] = 1;
dp[0][0][7000 + (b[0][0] - a[0][0])] = 1;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
for (int k = 200; k <= 13000; k++) {
if (i > 0) {
dp[i][j][k + (a[i][j] - b[i][j])] |= dp[i - 1][j][k];
dp[i][j][k + (b[i][j] - a[i][j])] |= dp[i - 1][j][k];
}
if (j > 0) {
dp[i][j][k + (a[i][j] - b[i][j])] |= dp[i][j - 1][k];
dp[i][j][k + (b[i][j] - a[i][j])] |= dp[i][j - 1][k];
}
}
}
}
int res = 2e9;
for (int k = 0; k < 14000; k++) {
if (!dp[h - 1][w - 1][k]) continue;
res = min(res, abs(7000 - k));
}
cout << res << endl;
return 0;
} | #include<bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
#define rrep(i,n) for(int i = (n)-1; i >= 0; i--)
#define rep1(i,n) for(int i = 1; i <= (n); i++)
#define rrep1(i,n) for(int i = (n); i > 0; i--)
#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>
#define MOD 1000000007
#define INF 1000000000000000LL
using namespace std;
#define MAXN 88
#define MAXS 13000
int dp[MAXN][MAXN][MAXS];
int main(){
int h,w;cin>>h>>w;
vector<vector<int>> scores(h);
rep(i, h)rep(j, w){
int a;cin>>a;
scores[i].push_back(a);
}
rep(i, h)rep(j, w){
int a;cin>>a;
scores[i][j] -= a;
if(scores[i][j] < 0)scores[i][j] *= -1;
}
dp[0][0][scores[0][0]] = 1;
rep(i, h)rep(j, w)rep(k, MAXS){
int s = scores[i][j];
int mpre = k-s;
if(mpre<0)mpre *= -1;
if(i && dp[i-1][j][mpre]==1)dp[i][j][k]=1;
if(j && dp[i][j-1][mpre]==1)dp[i][j][k]=1;
int ppre = k+s;
if(ppre<=7000){
if(i && dp[i-1][j][ppre]==1)dp[i][j][k]=1;
if(j && dp[i][j-1][ppre]==1)dp[i][j][k]=1;
}
}
int result = 0;
rrep(i, MAXS)if(dp[h-1][w-1][i])result = i;
cout<<result<<endl;
return 0;
rep(i, h){
rep(j, w)cout<<scores[i][j]<<' ';cout<<endl;
}
cout<<"###"<<endl;
rep(k, 15){
cout<<k<<endl;
rep(i, h){
rep(j, w)cout<<dp[i][j][k]<<' ';cout<<endl;
}
cout<<"---"<<endl;
}
return 0;
}
| 1 |
#include<iostream>
int main(){
int N;
std::cin>>N;
long int* A=new long int[N];
long sum=0;
for(int i=0;i<N;i++){
std::cin>>A[i];
sum+=A[i];
sum%=1000000007;
}
long ans=0;
for(int i=0;i<N;i++){
sum-=A[i];
if(sum<0){
sum+=1000000007;
}
ans+=sum*A[i];
ans%=1000000007;
}
std::cout<<ans;
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//#include <boost/multiprecision/cpp_ll.hpp>
//typedef boost::multiprecision::cpp_ll ll;
typedef long double dd;
#define i_7 (ll)(1E9+7)
//#define i_7 998244353
#define i_5 i_7-2
ll mod(ll a){
ll c=a%i_7;
if(c>=0)return c;
return c+i_7;
}
typedef pair<ll,ll> l_l;
ll inf=(ll)1E18;
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define pb push_back
ll max(ll a,ll b){if(a<b)return b;else return a;}
ll min(ll a,ll b){if(a>b)return b;else return a;}
void Max(ll &pos,ll val){pos=max(pos,val);}//Max(dp[n],dp[n-1]);
void Min(ll &pos,ll val){pos=min(pos,val);}
void Add(ll &pos,ll val){pos=mod(pos+val);}
dd EPS=1E-9;
#define endl "\n"
#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int main(){fastio
ll n;cin>>n;
ll a[n];rep(i,0,n-1)cin>>a[i];
ll b=0;
ll c=0;
rep(i,0,n-1){
b+=a[i];
c+=a[i]*a[i];
b=mod(b);
c=mod(c);
}
ll ans=mod(b*b-c);
if(ans%2==0){
cout<<mod(ans/2)<<endl;
}else{
cout<<mod((ans+i_7)/2)<<endl;
}
return 0;
}
| 1 |
#include<bits/stdc++.h>
using namespace std;
int main(){
int q,z,y=0;
cin>>q;
z=q;
while(z>0){
y+=z%10;
z/=10;
}
if(q%y==0) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
} | #include <bits/stdc++.h>
using namespace std;
int main() {
int N ; cin >> N ;
int P ;
int a,b,c,d,e,f,g,h ;
a = N % 10 ;
b = (N / 10) % 10 ;
c = (N / 100) % 10 ;
d = (N / 1000) % 10 ;
e = (N / 10000) % 10 ;
f = (N / 100000) % 10 ;
g = (N / 1000000) % 10 ;
h = (N / 10000000) % 10 ;
if ( N % (a+b+c+d+e+f+g+h) == 0 ){
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main(){
int N, count;
cin >> N;
count = 0;
vector<int> vec(N);
for(int i = 0; i < N; i++){
cin >> vec.at(i);
}
for(int i = 0; i < N; i++){
for(int j = 0; j < i; j++){
for(int k = 0; k < j; k++){
if((abs(vec.at(i) - vec.at(j)) < vec.at(k) && vec.at(k) < vec.at(i) + vec.at(j)) && vec.at(i) != vec.at(j) && vec.at(j) != vec.at(k)&& vec.at(k) != vec.at(i))
count++;
}
}
}
cout << count << endl;
} | #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,x,y) for(ll i=x;i<=y;i++)
#define imin INT_MIN
#define imax INT_MAX
int mod =1e9+7;
//~ vector<bool> cats;
//~ ll ans;
//~ ll n,m;
//~ void dfs(vector<vector<int>>& graph,int s,int consCats,int parent){
//~ if(consCats>m) return;
//~ if(graph[s].size()==1 && graph[s][0]==parent)
//~ {
//~ if(cats[s]) consCats++;
//~ if(consCats<=m) ans++;
//~ return;
//~ }
//~ if(!cats[s]) consCats=0;
//~ else consCats++;
//~ for(auto val:graph[s]){
//~ if(val!=parent)
//~ dfs(graph,val,consCats,s);
//~ }
int main(){
cin.tie(NULL);
ios_base::sync_with_stdio();
ll n;
cin>>n;
vector<int> a(n);
rep(i,0,n-1) cin>>a[i];
sort(a.begin(),a.end());
ll ans=0;
int i=0;
while(i<n-2){
int j=i+1;
int outer=0;
while(j<n-1){
int local=0;
if(a[j]!=a[i])
rep(k,j+1,n-1){
if(a[k]!=a[j])
if(a[i]+a[j]>a[k])
local++;
}
int tmp=a[j];
while(j<n-1 && a[j]==tmp) {
outer+=local;
j++;
}
}
int tmp=a[i];
while(i<n-2 && a[i]==tmp){
ans+=outer;
i++;}
}
cout<<ans;
return 0;
}
| 1 |
#include<iostream>
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define IO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
int n, q, inf=2e9, cnt;
vector<int> par ;
vector<pair<int,pair<int,int> > > v;
map<int,int> id ;
int tree[3000006];
void update(int node, int s, int e, int l, int r, int val)
{
if(s>r||e<l)
return ;
if(s>=l && e<=r)
{
tree[node]=min(tree[node],val);
return ;
}
int m=(s+e)/2;
update(2*node,s,m,l,r,val);
update(2*node+1,m+1,e,l,r,val);
}
int solve(int node, int s,int e, int pos)
{
if(s==e)
return tree[node];
int m=(s+e)/2;
tree[2*node]=min(tree[node],tree[2*node]);
tree[2*node+1]=min(tree[node],tree[2*node+1]);
return pos<=m ? solve(2*node,s,m,pos) : solve(2*node+1,m+1,e,pos);
}
int main()
{
IO
cin>>n>>q;
for(int i=1; i<=3e6; i++)
tree[i]=inf;
for(int i=1; i<=n; i++)
{
int s, t, x;
cin>>s>>t>>x;
v.push_back({x,{s-x,t-x-1}});
id[s-x]++;
id[t-x-1]++;
}
for(int i=1; i<=q; i++)
{
int d;
cin>>d;
id[d]++;
par.push_back(d);
}
for(auto i : id)
id[i.first]=++cnt;
for(int i=0; i<n; i++)
{
int l=id[v[i].second.first], r=id[v[i].second.second];
update(1,1,cnt,l,r,v[i].first);
}
for(int i=0; i<q; i++)
{
int ans=solve(1,1,cnt,id[par[i]]);
if(ans==inf)
ans=-1;
cout<<ans<<"\n";
}
}
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int main()
{
int N, Q;
cin >> N >> Q;
int Si, Ti, Xi, Di;
vector<array<int, 3>> stx(N);
for(int i=0;i<N;i++){
cin >> Si >> Ti >> Xi;
auto l = Si-Xi;
auto r = Ti-Xi;
if(l<0) l=0;
stx[i] = array<int, 3>{l, r, Xi};
}
sort(stx.begin(), stx.end(), [](array<int, 3>& a, array<int, 3>& b){return a[2] < b[2];});
set<int> D;
map<int, int> res;
for(int i=0;i<Q;i++){
cin >> Di;
D.insert(Di);
}
for(auto v : stx){
auto itrl = D.lower_bound(v[0]);
auto itrr = D.upper_bound(v[1]-1);
for(auto itr = itrl; itr != itrr; ++itr){
res[*itr] = v[2];
}
D.erase(itrl, itrr);
}
for(auto v : D){
res[v] = -1;
}
for(auto m : res){
cout << m.second << endl;
}
return 0;
} | 1 |
#include <bits/stdc++.h>
using namespace std;
class LinearSearch {
public:
int n, q;
vector< int > S, T;
LinearSearch() {}
void solve() {
int ans = 0;
for (int i = 0; i < q; ++i) {
int tar = T[i];
for (int j = 0; j < n; ++j) {
if (S[j] == tar) {
++ans;
break;
}
}
}
cout << ans << endl;
}
};
int main() {
LinearSearch ls;
cin >> ls.n;
ls.S.assign(ls.n, 0);
for (int i = 0; i < ls.n; ++i) cin >> ls.S[i];
cin >> ls.q;
ls.T.assign(ls.q, 0);
for (int i = 0; i < ls.q; ++i) cin >> ls.T[i];
ls.solve();
} | #include<bits/stdc++.h>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std;
int main(){
int n,q,ans=0,chk=0;
scanf("%d",&n);
vector<int> s(n);
for(auto&e:s){
scanf("%d",&e);
}
sort(s.begin(),s.end());
scanf("%d",&q);
vector<int> t(q);
for(auto&e:t){
scanf("%d",&e);
}
sort(t.begin(),t.end());
for(int i=0;i<q;i++){
for(int j=chk;j<n;j++){
if(t[i]==s[j]){
ans++;
break;
}else if(t[i]<s[j]){
break;
}
chk=j;
}
}
printf("%d\n",ans);
return 0;
} | 1 |
#include<bits/stdc++.h>
using namespace std;
int64_t mod=1e9+7;
int main(){
int64_t h,w,k,i,j;
vector<int64_t> f(9);
cin>>h>>w>>k;
vector<vector<int64_t>> t(w+1,vector<int64_t>(w+1)),c(h+1,vector<int64_t>(w+1));
f.at(0)=1;
f.at(1)=1;
for(i=2;i<=8;i++) f.at(i)=f.at(i-1)+f.at(i-2);
for(i=1;i<=w;i++){
t.at(i).at(i)=f.at(i-1)*f.at(w-i);
if(i>1) t.at(i).at(i-1)=f.at(i-2)*f.at(w-i);
if(i<w) t.at(i).at(i+1)=f.at(i-1)*f.at(w-i-1);
}
c.at(0).at(1)=1;
for(i=1;i<=h;i++){
for(j=1;j<=w;j++){
c.at(i).at(j)=c.at(i-1).at(j)*t.at(j).at(j)%mod;
if(j>1) c.at(i).at(j)=(c.at(i).at(j)+c.at(i-1).at(j-1)*t.at(j-1).at(j))%mod;
if(j<w) c.at(i).at(j)=(c.at(i).at(j)+c.at(i-1).at(j+1)*t.at(j+1).at(j))%mod;
}
}
cout<<c.at(h).at(k)<<endl;
} | #define _USE_MATH_DEFINES
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <cmath>
#include <complex>
#include <string>
#include <cstring>
#include <vector>
#include <bitset>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <list>
#include <ctime>
////
//#include <random>//
/////////
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
////定数
const int INF = (int)1e9 + 10;
const LL MOD = (LL)1e9 + 7;
const LL LINF = (LL)4e18 + 20;
const LD PI = acos(-1.0);
const double EPS = 1e-9;
/////////
using namespace::std;
void chmax(LL& a, LL b) {
a = max(a, b);
}
void chmin(LL& a, LL b) {
a = min(a, b);
}
//////////////
void solve() {
int H, W, K;
cin >> H >> W >> K;
/*
縦棒がW本ある。W-1個のあるなしで全探索。って
*/
//左付きで棒があるかないか
vector<vector<bool>> BF;
vector<int> BF_num;
for (int s = 0; s < (1 << (W-1)); ++s) {
int ter = s;
vector<bool> res(W - 1);
int cnt = 0;
for (int i = 0; i < W-1; ++i) {
if (ter & 1) {
res[i] = true;
cnt++;
}
else {
res[i] = false;
}
ter /= 2;
}
bool flag = true;
for (int i = 0; i < W - 1 - 1; ++i) {
if (res[i] == true && res[i + 1] == true) {
flag = false;
break;
}
}
if (flag == true) {
BF.push_back(res);
BF_num.push_back(cnt);
}
}
/*
H+1 * W * (K+1)個必要
*/
//高さと位置のみ
vector<vector<LL>> dp(H + 1,vector<LL>(W, 0));
dp[0][0] = 1;
int BF_len = BF.size();
for (int h = 0; h < H; ++h) {//上から順に
//dp[h]->dp[h+1]
for (int s = 0; s < BF_len; ++s) {//全ての横棒で
vector<bool> B = BF[s];
for (int w = 0; w < W; ++w) {
//dp[h][k]
if (w+1 <W && B[w] == true) {//右へ
dp[h + 1][w + 1] += dp[h][w];
dp[h + 1][w + 1] %= MOD;
}
else if (w - 1 >= 0 && B[w - 1] == true) {//左
dp[h + 1][w - 1] += dp[h][w];
dp[h + 1][w - 1] %= MOD;
}
else{//真下へ
dp[h + 1][w] += dp[h][w];
dp[h + 1][w] %= MOD;
}
}
}
}
//[H][K-1]
LL ans = dp[H][K-1];
cout << ans << endl;
}
signed main(void) {
std::cin.tie(0);
std::ios::sync_with_stdio(false);
std::cout << std::fixed;//小数を10進数表示
cout << setprecision(16);//小数点以下の桁数を指定//coutとcerrで別
solve();
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<int> a(2 * n);
for (int i = 0; i < (n << 1); i++) {
cin >> a[i];
}
sort(a.begin(), a.end());
int ans = 0;
for (int i = 0; i < (n << 1); i+=2) {
ans += a[i];
}
cout << ans;
} | #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;
int main() {
int n;
cin >> n;
int a[n + 2];
for (int i = 1; i < n + 1; i++) cin >> a[i];
a[0] = 0, a[n + 1] = 0;
ll s = 0;
rep(i, n + 1) s += abs(a[i + 1] - a[i]);
for (int i = 1; i < n + 1; i++) {
cout << s + abs(a[i - 1] - a[i + 1]) -
abs(abs(a[i - 1] - a[i]) + abs(a[i] - a[i + 1]))
<< endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1001001001;
const ll mod = 1000000007;
ll dp[105][11];
int main(){
int h, w, k; cin >> h >> w >> k;
dp[0][1] = 1;
for(int i = 1; i <= h; i++){
for(int l = 1; l <= w; l++){
ll a = 0, b = 0, c = 0;
for(int j = 0; j < (1 << w-1); j++){
bool ok = true;
for(int m = 0; m < w - 2; m++){
if(j & (1 << (m + 1)) && j & (1 << m)) ok = false;
}
if(!ok) continue;
int mask1 = 1 << (l - 1), mask2 = 1 << (l - 2);
if(l >= 2 && (mask2 & j)) a++;
else if(mask1 & j) c++;
else b++;
}
dp[i][l] = (dp[i-1][l-1]*a%mod+dp[i-1][l]*b%mod + dp[i-1][l+1]*c%mod)%mod;
}
}
cout << dp[h][k] << endl;
} | #include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(var) cerr << (#var) << " = " << (var) << endl;
#else
#define debug(var)
#endif
void init() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
}
const int mod = 1e9+7;
const int N = 123;
long long p[N];
int H, W, K;
long long dp[N][8];
bool check(int x) {
int prev = 0;
while (x) {
if ((x&1) && prev) return false;
prev = x&1;
x >>= 1;
}
return true;
}
long long rek(int h, int pos) {
if (h == H) return pos == (K-1);
long long &ret = dp[h][pos];
if (ret != -1) return ret;
ret = 0;
for (int j = 0; j < 3; ++j) {
if (pos == 0 && j == 1) continue;
if (pos == W-1 && j == 2) continue;
long long cur = 0;
int lmt = pos-1;
if (j == 1) --lmt;
for (int i = 0; i < (1 << max(lmt, 0)); ++i) {
if (!check(i)) continue;
++cur;
}
long long cur2 = 0;
lmt = W-1-pos-1;
if (j == 2) --lmt;
for (int i = 0; i < (1 << max(lmt, 0)); ++i) {
if (!check(i)) continue;
++cur2;
}
cur *= cur2;
if (j == 1) {
ret = (ret + cur*rek(h+1, pos-1)%mod) % mod;
} else if (j == 2) {
ret = (ret + cur*rek(h+1, pos+1)%mod) % mod;
} else {
ret = (ret + cur*rek(h+1, pos)%mod) % mod;
}
}
return ret;
}
void solve() {
memset(dp, -1, sizeof(dp));
scanf("%d%d%d", &H, &W, &K);
printf("%lld", rek(0, 0));
}
int main() {
init();
int t = 1; //scanf("%d", &t);
while (t--) {
solve();
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
#define N 100000
#define pb push_back
int n, k, len, a[N + 5], d[N + 5], vis[N + 5], ans;
vector<int> G[N + 5];
struct Cmp {
bool operator () (const int &a, const int &b) const {
return d[a] < d[b];
}
};
priority_queue<int, vector<int>, Cmp> pq;
void dfs1(int u) {
for (int i = 0, v; i < G[u].size(); ++i) {
v = G[u][i];
d[v] = d[u] + 1;
dfs1(v);
}
}
void dfs2(int u) {
len++;
if (u == 1)
return ;
dfs2(a[u]);
}
void mark(int u) {
vis[u] = 1;
for (int i = 0, v; i < G[u].size(); ++i) {
v = G[u][i];
if (vis[v])
continue;
mark(v);
}
}
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i)
scanf("%d", &a[i]);
dfs2(a[1]);
for (int i = 2; i <= n; ++i)
G[a[i]].pb(i);
dfs1(1);
bool flag = 1;
for (int i = 1; i <= n; ++i)
if (d[i] > k || (k - d[i]) % len) {
flag = 0;
break;
}
if (flag)
ans = 0;
else {
if (a[1] != 1) ans = 1;
else ans = 0;
a[1] = 1;
for (int i = 2; i <= n; ++i)
pq.push(i);
while (!pq.empty()) {
int u = pq.top(), v; pq.pop();
if (d[u] <= k || vis[u])
continue;
ans++;
v = u;
for (int i = 1; i <= k - 1; ++i)
v = a[v];
a[v] = 1;
// printf("u:%d v:%d\n", u, v);
mark(v);
}
}
printf("%d\n", ans);
// for (int i = 1; i <= n; ++i) printf("%d ", a[i]);
// printf("\n");
return 0;
} | ////////////////////////////////////////////
/// ///
/// Template ver. 1 rapel ///
/// Fear is Temporary, Regret is Forever ///
/// Must Try and Get AC ///
/// ///
////////////////////////////////////////////
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <utility>
#include <math.h>
#include <complex>
#include <assert.h>
#include <time.h>
//#include <chrono>
//#include <random>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <list>
#include <bitset>
#define FI first
#define SE second
#define MP make_pair
#define PB push_back
#define PF push_front
#define POB pop_back
#define POF pop_front
#define endl '\n'
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
void desperate_optimization(int precision){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(precision);
}
const int N = 1e5;
int arr[N + 5];
vector<int> lst[N + 5];
int tot = 0;
int depth[N + 5];
int n, k;
void dfs(int idx) {
depth[idx] = 0;
for(auto x: lst[idx]) {
dfs(x);
depth[idx] = max(depth[idx], depth[x] + 1);
}
// cout<<"IDX "<<idx<<" "<<depth[idx]<<endl;
if(depth[idx] == k - 1 && arr[idx] != 1 && idx != 1) {
depth[idx] = -1;
tot++;
}
}
void hora(int tc) {
cin>>n>>k;
for(int i = 1;i <= n;i++) {
cin>>arr[i];
if(i != 1) lst[arr[i]].PB(i);
}
if(arr[1] != 1) tot++;
dfs(1);
cout<<tot<<endl;
}
int main(){
desperate_optimization(10);
int ntc = 1;
//cin>>ntc;
for(int tc = 1;tc <= ntc;tc++) hora(tc);
return 0;
}
| 1 |
#include <cstdio>
#include <algorithm>
#include <vector>
#define file(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
inline int read()
{
int data = 0, w = 1; char ch = getchar();
while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') w = -1, ch = getchar();
while (ch >= '0' && ch <= '9') data = data * 10 + (ch ^ 48), ch = getchar();
return data * w;
}
const int N(2e5 + 10), Mod(1e9 + 7);
int fac[N], inv[N], f[N], n, m, A, B;
int C(int n, int m) { return 1ll * fac[n + m] * inv[m] % Mod * inv[n] % Mod; }
int fastpow(int x, int y)
{
int ans = 1;
for (; y; y >>= 1, x = 1ll * x * x % Mod)
if (y & 1) ans = 1ll * ans * x % Mod;
return ans;
}
int main()
{
n = read() - 1, m = read() - 1, A = read() - 1, B = read() - 1, fac[0] = inv[0] = 1;
if (A == n || B == m) return puts("0"), 0;
for (int i = 1; i <= n + m; i++) fac[i] = 1ll * fac[i - 1] * i % Mod;
inv[n + m] = fastpow(fac[n + m], Mod - 2);
for (int i = n + m - 1; i; i--) inv[i] = 1ll * inv[i + 1] * (i + 1) % Mod;
int ans = C(n, m);
for (int i = 0; i <= A; i++)
ans = (ans - 1ll * C(i, m - B - 1) * C(n - i, B) % Mod + Mod) % Mod;
printf("%d\n", ans);
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod=1e9+7;
int h,w,a,b,fac[200006],ans=0,inv_fac[200006];
int binpow(int x,int y){
if(y==0) return 1;
else{
long long tmp=binpow(x,y/2);
if(y%2==1) return ((tmp*tmp)%mod*x)%mod;
else return (tmp*tmp)%mod;
}
}
int C(int k,int n) {
return fac[n]*inv_fac[k]%mod*inv_fac[n-k]%mod;
}
int path(int x1,int y1,int x2,int y2) {
if(x1>x2||y1>y2) return 1;
return C(x2-x1,x2-x1+y2-y1)%mod;
}
signed main() {
cin>>h>>w>>a>>b;
fac[0]=1;
inv_fac[0]=1;
for(int i=1;i<=200005;++i)
{
fac[i]=((i % mod)*fac[i-1])%mod;
inv_fac[i]=binpow(fac[i],mod-2)%mod;
}
for(int i=1;i<=h-a;++i) {
ans+=((path(1,1,i,b)%mod)*(path(i,b+1,h,w)%mod))%mod;
}
cout<<ans%mod;
} | 1 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
long long n,q;
cin >> n >> q;
long long s[n],t[n],x[n],d[q];
pair<long long, int> p[n];
for(int i=0;i<n;i++){
cin >> s[i] >> t[i] >> x[i];
p[i]=make_pair(x[i], i);
}
sort(p, p+n);
vector<pair<long long, int> > vec;
bool active[n];
for(int i=0;i<n;i++){
vec.push_back(make_pair(s[p[i].second]-x[p[i].second]-1, i));
vec.push_back(make_pair(t[p[i].second]-x[p[i].second]-1, i));
active[i]=false;
}
for(int i=0;i<q;i++){
cin >> d[i];
vec.push_back(make_pair(d[i], -1));
}
sort(vec.begin(), vec.end());
priority_queue<int, vector<int>, greater<int> > que;
for(size_t i=0;i<vec.size();i++){
if(vec[i].second == -1){
if(!que.empty()){
cout << p[que.top()].first << endl;
}else{
cout << -1 << endl;
}
}else{
long long idx = vec[i].second;
if(active[idx]){ //工事終了
active[idx] = false;
if(idx == que.top()){
que.pop();
while(!que.empty()){
if(active[que.top()]){
break;
}else{
que.pop();
}
}
}
}else{ //工事開始
active[idx] = true;
que.push(idx);
}
}
}
return 0;
}
| #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct ppp{ll l,r,v,tag;}tree[4000005];
struct ttt{ll s,t,x;}b[200005];
bool cmp(ttt l,ttt r){return l.x>r.x;}
ll p[200005];
set<ll> k;
vector<ll> a;
void push(ll i){
tree[i*2].tag=tree[i].tag;
tree[i*2+1].tag=tree[i].tag;
tree[i*2].v=(tree[i*2].r-tree[i*2].l+1)*tree[i*2].tag;
tree[i*2+1].v=(tree[i*2+1].r-tree[i*2+1].l+1)*tree[i*2+1].tag;
tree[i].tag=0;
}
void buildtree(ll i,ll l,ll r){
tree[i].l=l;tree[i].r=r;
if(l==r){
tree[i].v=-1;
return;
}
buildtree(i*2,l,(l+r)/2);
buildtree(i*2+1,(r+l)/2+1,r);
tree[i].v=tree[i*2].v+tree[i*2+1].v;
}
void update(ll i,ll l,ll r,ll k){
if(tree[i].l>=l&&tree[i].r<=r){
tree[i].tag=k;
tree[i].v=(tree[i].r-tree[i].l+1)*k;
return ;
}
if(tree[i].tag) push(i);
ll m=(tree[i].l+tree[i].r)/2;
if(l<=m) update(i*2,l,r,k);
if(r>m) update(i*2+1,l,r,k);
tree[i].v=tree[i*2].v+tree[i*2+1].v;
}
ll query(ll i,ll l,ll r){
if(tree[i].l>=l&&tree[i].r<=r){
return tree[i].v;
}
if(tree[i].tag) push(i);
ll m=(tree[i].r+tree[i].l)/2,ans=0;
if(l<=m) ans+=query(i*2,l,r);
if(r>m) ans+=query(i*2+1,l,r);
return ans;
}
int main(){
ll n,q;
cin>>n>>q;
a.push_back(-1e9);
for(ll i=1;i<=n;i++){
cin>>b[i].s>>b[i].t>>b[i].x;
k.insert(max(b[i].s-b[i].x,(ll)0));
k.insert(max(b[i].t-1-b[i].x,(ll)0));
}
for(ll i=1;i<=q;i++){
cin>>p[i];k.insert(p[i]);
}
for(auto x:k){
a.push_back(x);
}
buildtree(1,1,a.size());
sort(b+1,b+1+n,cmp);
for(ll i=1;i<=n;i++){
ll l=lower_bound(a.begin(),a.end(),max((ll)0,b[i].s-b[i].x))-a.begin();
ll r=lower_bound(a.begin(),a.end(),max((ll)0,b[i].t-b[i].x-1))-a.begin();
update(1,l,r,b[i].x);
}
for(ll i=1;i<=q;i++){
ll l=lower_bound(a.begin(),a.end(),p[i])-a.begin();
cout<<query(1,l,l)<<endl;
}
} | 1 |
#include<bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)
typedef long long ll;
int main() {
int n,m,q;cin>>n>>m>>q;
int a[n][n];
rep(i,n)rep(j,n)a[i][j]=0;
int tmp_r,tmp_l;
rep(i,m){
cin>>tmp_l>>tmp_r;
--tmp_l,--tmp_r;
++a[tmp_l][tmp_r];
}
int b[n][n];
rep(i,n)rep(j,n)b[i][j]=a[i][j];
rep(i,n)rep(j,n-1)b[i][j+1]+=b[i][j];
int ql,qr;
rep(i,q){
int ans = 0;
cin>>ql>>qr;
--ql,--qr;
ans = b[qr][qr];
REP(j,ql,qr)ans+=b[j][qr];
// if(ql!=0)REP(j,ql,qr+1)ans-=b[ql-1][j];
cout<<ans<<endl;
}
return 0;
} | #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pb push_back
const int maxn=1e6+10;
const int mod=1e9+7;
const double pi=3.1415926535;
int w[3005],v[3005];
int dp[3005][3005][2];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n,t;
cin>>n>>t;
for(int i=1;i<=n;i++)
{
cin>>w[i]>>v[i];
}
for(int i=1;i<=n;i++)
{
for(int j=0;j<=t-1;j++)
{
dp[i][j][1]=max(dp[i-1][j][1],dp[i-1][j][0]+v[i]);
if(j>=w[i])
{
dp[i][j][0]=max(dp[i-1][j][0],dp[i-1][j-w[i]][0]+v[i]);
dp[i][j][1]=max(dp[i][j][1],dp[i-1][j-w[i]][1]+v[i]);
}
else
{
dp[i][j][0]=dp[i-1][j][0];
}
}
}
cout<<dp[n][t-1][1]<<'\n';
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const double PI = acos(-1);
const int inf = 2e9;
const long long INF = 2e18;
const long long MOD = 1e9+7;
#define sx(s) (s).size()
#define pb push_back
#define fi first
#define se second
#define REP(i,n) for (int i = 0; i < n; i++)
#define ALL(a) begin(a),end(a)
int main(void) {
int n;
while (cin >> n, n) {
vector<pair<int,ll>> s(4000, {inf, 0});
vector<bool> ip(4000,false);
REP(i,n) {
ll e,p,q;
cin >> e >> p >> q;
e--;
if (ip[e]) {
s[e].se += p*q;
}
else {
s[e] = make_pair(i,p*q);
ip[e] = true;
}
}
vector<pair<int,int>> ans;
REP(i, 4000) {
if (s[i].se >= 1000000) {
ans.pb({s[i].fi, i+1});
}
}
sort(ALL(ans));
REP(i,ans.size()) cout << ans[i].se << endl;
if (ans.size() == 0) cout << "NA" << endl;
}
return 0;
}
| #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define REP(i, a, b) for(ll i = a; i <= b; i++)
#define out(x) std::cout << x << '\n'
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define sz(x) ((int)(x).size())
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g * b; }
const int dx[4] = { 1, 0, -1, 0 };
const int dy[4] = { 0, 1, 0, -1 };
const int INF = INT_MAX / 2;
const ll inf = LLONG_MAX / 2;
const int mod = 1000000007;
const int MAX_N = 101010;
const long double PI = acos(-1);
using Edge = pair<int, ll>;
using Graph = vector<vector<Edge>>;
Graph G;
vector<ll> dist;
void dfs(int cur, int pre, ll sum) {
dist[cur] = sum;
for (auto to : G[cur]) {
if (to.first != pre) {
dfs(to.first, cur, sum + to.second);
}
}
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(10);
/*------------------------------*/
int n;
cin >> n;
G.resize(n);
rep(i, n - 1) {
int a, b, c;
cin >> a >> b >> c;
a--; b--;
G[a].push_back(make_pair(b, c));
G[b].push_back(make_pair(a, c));
}
int q, k;
cin >> q >> k;
k--;
dist.resize(n, 0);
dfs(k, -1, 0);
rep(i, q) {
int x, y;
cin >> x >> y;
x--; y--;
out(dist[x] + dist[y]);
}
return 0;
} | 0 |
#include <bits/stdc++.h>
int main(void) {
int n;
int q;
int S[16384];
int T[512];
std::cin >> n;
for(int i = 0; i < n; ++i) {
std::cin >> S[i];
}
std::cin >> q;
for(int i = 0; i < q; ++i) {
std::cin >> T[i];
}
int ans = 0;
for(int i = 0; i < q; ++i) {
int num = T[i];
for(int j = 0; j < n; ++j) {
if( S[j] == num ) {
ans += 1;
break;
}
}
}
std::cout << ans << std::endl;
return 0;
} | #include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e15;
#include <math.h>
#define PI 3.14159265358979323846264338327950L
const int mxN = 1e9 + 7;
char s[100005];
long long dp[100005][13];
int minimum_Color(vector<int> a, int n) {
multiset<int> last;
for (int i = 0; i < n; i++) {
auto it = last.lower_bound(a[i]);
if (it == last.begin()) {
last.insert(a[i]);
}
else {
it--;
last.erase(it);
last.insert(a[i]);
}
}
return last.size();
}
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++)cin >> a[i];
cout << minimum_Color(a, n);
return 0;
} | 0 |
#include <bits/stdc++.h>
using namespace std;
int main(){
string s1,s2;
cin>>s1>>s2;
cout<<s2+s1<<endl;
} | #include<stdio.h>
int main(){
int A,B,C;
scanf ("%d",&A);
scanf ("%d",&B);
scanf ("%d",&C);
if (A==B && A==C && B==A && B==C && C==A && C==B){
printf ("No\n");
}else if (A==B||A==C||B==A||B==C||C==A||C==B){
printf("Yes\n");
}else {
printf ("No\n");
}
}
| 0 |
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <limits.h>
#include <string>
#include <string.h>
#include <sstream>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <stack>
#include <queue>
using namespace std;
typedef long long ll;
int main(){
int num;
cin >> num;
printf("%d\n", (int)pow( num, 3 ));
return 0;
} | #include <bits/stdc++.h>
using namespace std;
int main()
{
string s; cin>>s;
int x=stoi(s);
int y=x*x*x;
cout<<to_string(y)<<endl;
} | 1 |
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <random>
#include <tuple>
#include <iomanip>
#include <cstring>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
#define rep(i, n) for(ll i = 0; i < (n); i++)
#define revrep(i, n) for(ll i = (n-1); i >= 0; i--)
#define pb push_back
#define f first
#define s second
const ll INFL = 1LL << 60;//10^18 = 2^60
const int INF = 1 << 30;//10^9
ll MOD = 1000000007;
//ll MOD = 998244353;
vector<ll> dy = {0, 0, 1, -1, 1, 1, -1, -1, 0};
vector<ll> dx = {1, -1, 0, 0, 1, -1, 1, -1, 0};
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll pow_long(ll x, ll k){
ll res = 1;
while(k > 0){
if(k % 2) res *= x;
x *= x;
k /= 2;
}
return res;
}
ll pow_mod(ll x, ll k){
x %= MOD; x += MOD; x %= MOD;
ll res = 1;
while(k > 0){
if(k % 2){
res *= x; res %= MOD;
}
x *= x; x %= MOD;
k /= 2;
}
return res;
}
ll inverse(ll x){return pow_mod(x, MOD - 2);};
ll gcd(ll a, ll b){
if(b == 0) return a;
return gcd(b, a % b);
}
ll lcm(ll x, ll y){return x / gcd(x, y) * y;};
ll kai_mod(ll x){
if(x == 0) return 1;
return x * kai_mod(x-1) % MOD;
}
/*
//コンビネーション
const int MAXcomb = 200010;
ll fac[MAXcomb], finv[MAXcomb], inv[MAXcomb];
//facはn!,finvは1/n!
//invは逆元
void COMinit(){
fac[0] = fac[1] = 1;
finv[0] = finv[1] = 1;
inv[1] = 1;
for(int i = 2; i < MAXcomb; i++){
fac[i] = fac[i-1] * i % MOD;
inv[i] = MOD - inv[MOD%i] * (MOD/i) % MOD;
finv[i] = finv[i-1] * inv[i] % MOD;
}
}
ll comb(int n, int k){
if(n < k) return 0;
if(n < 0 || k < 0) return 0;
return fac[n] * finv[k] % MOD * finv[n-k] % MOD;
}
*/
//MODなしバージョン
vector<vector<ll>> memo(60, vector<ll>(60, -1));
//コンビネーション
ll comb(int n, int k){
if(memo[n][k] != -1) return memo[n][k];
if(n < k) return 0;
if(n < 0 || k < 0) return 0;
if(n == 0 || k == 0) return 1;
return memo[n][k] = comb(n-1, k) + comb(n-1, k-1);
}
ll N, M;
string S, T;
ll solve(string blue, string red){
//Tを使って、red, blueを作る。
if(blue.size() < red.size()) swap(red, blue);
ll rl = red.size();
ll dp[N+1][rl+1];
rep(i, N+1){
rep(j, rl + 1){
dp[i][j] = 0;
}
}
dp[0][0] = 1;
rep(i, N){
rep(j, rl + 1){
if(j != rl && T[i] == red[j]){
dp[i+1][j+1] += dp[i][j];
}
if(T[i] == blue[i-j]){
dp[i+1][j] += dp[i][j];
}
}
}
return dp[N][rl];
}
int main(){
cin >> N >> S;
T = S;
reverse(T.begin(), T.end());
ll ans = 0;
rep(i, (1 << N)){
string red = "", blue = "";
rep(j, N){
if((i >> j) & 1) red += S[j];
else blue += S[j];
}
ans += solve(red, blue);
}
cout << ans << endl;
}
| #include<bits/stdc++.h>
using namespace std;
int main() {
int n;
string str;
cin >> n;
cin >> str;
vector<int> vW(n+1,0),vE(n+1,0);
for(int i=0;i<n;i++){
vW[i+1]+=vW[i];
if(str[i]=='W') vW[i+1]++;
vE[i+1]+=vE[i];
if(str[i]=='E') vE[i+1]++;
}
int ans=999999999;
int now;
for(int i=0;i<n;i++){
now=vW[i]+(vE[n]-vE[i+1]);
if(ans>now) ans=now;
}
cout << ans << endl;
}
| 0 |
#include <iostream>
using namespace std;
int main(){
int a,b,c,d;
cin >> a >> b >> c >> d;
if(a+b < c+d) cout << "Right";
else if(a+b > c+d) cout << "Left";
else cout << "Balanced";
cout << endl;
return 0;
}
| #include <iostream>
using namespace std;
int main(){
int l[10];
int v[2];
while(scanf("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", &l[0], &l[1],
&l[2], &l[3], &l[4], &l[5], &l[6], &l[7], &l[8], &l[9], &v[0], &v[1]) != EOF){
double sum = 0.0;
int i;
for(i=0; i<10; ++i) sum += l[i];
double l1 = sum * v[0] / (v[0] + v[1]);
sum = 0.0;
for(i=0; i<10; ++i){
sum += l[i];
if(l1 <= sum) break;
}
cout << i+1 << endl;
}
return 0;
} | 0 |
#include <stdio.h>
#include <vector>
#include <queue>
using namespace std;
struct side{
int n;
int m;
int l;
};
side sides[100000];
void mergesort(int l,int r){
if(l==r)return;
side temp;
if(l==r-1){
if(sides[l].l>sides[r].l){
temp=sides[l];
sides[l]=sides[r];
sides[r]=temp;
}
return;
}
int mid=(l+r)/2,now=0;
side b[r-l+1];
mergesort(l,mid-1);
mergesort(mid,r);
for(int i=0;i<=r-l;i++){
if(sides[i+l-now].l<sides[mid+now].l){
b[i]=sides[i+l-now];
if(i+l-now==mid-1){
for(i++;i<=r-l;i++){
b[i]=sides[mid+now];
now++;
}
}
}
else{
b[i]=sides[mid+now];
now++;
if(mid+now>r){
for(i++;i<=r-l;i++)
b[i]=sides[i+l-now];
}
}
}
for(int i=0;i<=r-l;i++)
sides[i+l]=b[i];
}
int main(){
int n,m,a,ans=0,c=1;
bool tree[10000];
scanf("%d%d",&n,&m);
if(m==0){
printf("0\n");
return 0;
}
tree[0]=true;
for(int i=1;i<n;i++){
tree[i]=false;
}
for(int i=0;i<m;i++){
scanf("%d%d%d",&sides[i].n,&sides[i].m,&sides[i].l);
}
mergesort(0,m-1);
while(c<n){
a=0;
while(tree[sides[a].n]==tree[sides[a].m])a++;
ans+=sides[a].l;
tree[sides[a].n]=tree[sides[a].m]=true;
c++;
}
printf("%d\n",ans);
} | #include <algorithm>
#include <iostream>
#include <vector>
struct Edge {
int start;
int target;
int weight;
};
class UnionFind {
std::vector<int> m_nodes;
public:
UnionFind(size_t size) : m_nodes(size) {
for (size_t i = 0; i < size; i++)
m_nodes[i] = i;
}
int find(int index) {
int ret = m_nodes[index];
if (ret == index) return ret;
ret = find(ret);
m_nodes[index] = ret;
return ret;
}
void merge(int x, int y) {
x = find(x);
y = find(y);
m_nodes[x] = y;
}
};
int main()
{
int v, e;
std::cin >> v >> e;
std::vector<Edge> edges;
edges.reserve(e);
for (int i = 0; i < e; i++) {
Edge edge;
std::cin >> edge.start >> edge.target >> edge.weight;
edges.push_back(edge);
}
std::sort(edges.begin(), edges.end(), [](Edge& x, Edge& y) { return x.weight < y.weight; });
UnionFind uf(v);
uint_least64_t weight = 0;
for (Edge& edge : edges) {
int s = uf.find(edge.start);
int t = uf.find(edge.target);
if (s != t) {
uf.merge(s, t);
weight += edge.weight;
}
}
std::cout << weight << std::endl;
return 0;
} | 1 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define rep(i,n) for(ll (i)=0; (i)<(ll)(n); (i)++)
#define frep(i,m,n) for(ll (i)=(m); (i)<=(ll)(n); (i)++)
#define rrep(i,n) for(ll (i)=(n)-1; (i)>-1; (i)--)
#define frrep(i,m,n) for(ll (i)=(n); (i)>(ll)(m); (i)--)
#define ALL(x) (x).begin(), (x).end()
const ll INF = 100100100100100100;
const ll MOD = 1000000007;
// get abs
ll my_abs(ll a);
// a^n
ll a_n(ll a, ll n);
// get gcd
ll my_gcd(ll a, ll b);
// a^(-1) % MOD
ll inv(ll a);
// (a+b+c)%MOD
ll madd(ll a, ll b, ll c);
// (a-b)%MOD
ll msub(ll a, ll b);
// (a*b*c)%MOD
ll mtime(ll a, ll b, ll c);
int main() {
ll n, ans = 0; cin >> n;
vector<ll> a(n), b(n, 0);
rep(i, n) cin >> a[i];
if(a[0] != 0) {
cout << -1 << endl;
return 0;
}
else {
rep(i, n-1) {
if(a[i]+1 < a[i+1]) {
cout << -1 << endl;
return 0;
}
}
rep(i, n) {
b[i-a[i]]++;
}
ll index = 0;
rep(i, n) {
rep(j, b[i]) {
a[index] = i;
index++;
}
}
set<ll> s;
rep(i, n) {
if(!s.count(a[n-1-i])) {
s.insert(a[n-1-i]);
ans += (n-1-i) - a[n-1-i];
}
}
cout << ans << endl;
}
return 0;
}
ll my_abs(ll a) {
if(a >= 0) return a;
else return -1 *a;
}
ll a_n(ll a, ll n) {
if(n == 0) return 1;
ll ret = a, count = 1;
while(count * 2 < n) {
ret *= ret;
count *= 2;
}
if(count == n) return ret;
else return (ret * a_n(a, n-count));
}
ll my_gcd(ll a, ll b) {
if(b == 0) return a;
return my_gcd(b, a%b);
}
ll inv(ll a) {
return a_n(a, MOD-2);
}
ll madd(ll a, ll b, ll c) {
ll ret = (a+b) % MOD;
return (ret+c) % MOD;
}
ll msub(ll a, ll b) {
if(a < b) return (a-b+MOD) % MOD;
else return (a-b) % MOD;
}
ll mtime(ll a, ll b, ll c) {
ll ret = (a*b) % MOD;
return (ret*c) % MOD;
}
| #include <bits/stdc++.h>
#include <cmath>
const double PI = 3.14159265358979323846;
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
#define rep(i, n) for (int i = 0; i < (n); ++i)
//#define rep(i, n) for (ll i = 0; i < (n); ++i)
//typedef pair<ll, ll> P;
typedef pair<ll, ll> P;
const ll INF = 10e17;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)
#define ret() return 0;
std::istream &operator>>(std::istream &in, set<int> &o) {
ll a;
in >> a;
o.insert(a);
return in;
}
std::istream &operator>>(std::istream &in, queue<int> &o) {
ll a;
in >> a;
o.push(a);
return in;
}
bool contain(set<int> &s, int a) { return s.find(a) != s.end(); }
//ifstream myfile("C:\\Users\\riku\\Downloads\\0_00.txt");
//ofstream outfile("log.txt");
//outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0') << rank << endl;
// std::cout << std::bitset<8>(9);
const int mod = 1000000007;
//const ll mod = 1e10;
typedef priority_queue<string, vector<string>, greater<string> > PQ_ASK;
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> numbers(n);
rep(i, n) cin >> numbers[i];
if (numbers.front() != 0) {
cout << -1 << endl;
ret();
}
vector<int> dp(n);
dp.back() = numbers.back();
for (int i = n - 2; i >= 0; i--) {
dp[i] = max(numbers[i], dp[i + 1] - 1);
}
rep(i, n) {
if (numbers[i] < dp[i]) {
cout << -1 << endl;
ret();
}
}
ll ans = 0;
rep(i, n - 1) {
if (dp[i] > dp[i + 1] - 1) {
ans += dp[i];
}
}
ans += numbers.back();
cout << ans << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = (int) 5e5 + 55;
int fen[N];
void upd(int x, int y) {
while (x < N) {
fen[x] += y;
x |= (x + 1);
}
}
int get(int x) {
int res = 0;
while (x >= 0) {
res += fen[x];
x = (x & (x + 1)) - 1;
}
return res;
}
int res[N];
vector<pair<int, int>> vec[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
vector<int> c(n);
for (int i = 0; i < n; i++) {
cin >> c[i];
--c[i];
}
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
--l; --r;
vec[r].emplace_back(l, i);
}
vector<int> last(n, -1);
for (int i = 0; i < n; i++) {
if (last[c[i]] != -1) {
upd(last[c[i]], -1);
}
last[c[i]] = i;
upd(i, 1);
for (auto u : vec[i]) {
res[u.second] = get(i) - get(u.first - 1);
}
}
for (int i = 0; i < q; i++) {
cout << res[i] << '\n';
}
return 0;
}
| #include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int n, m, siz, num;
int w[N], cnt[N], belong[N];
int now, ans[N];
struct query{
int l, r, id;
}q[N];
//bool cmp(query a, query b)
//{
// return (belong[a.l] ^ belong[b.l]) ? belong[a.l] < belong[b.l] : ((belong[a.l] & 1) ? a.r < b.r : a.r > b.r);
//}
bool cmp(query a, query b) {
return (belong[a.l] ^ belong[b.l]) ? belong[a.l] < belong[b.l] : ((belong[a.l] & 1) ? a.r < b.r : a.r > b.r);
}
void add(int pos)
{
if(!cnt[w[pos]]) ++now;
++cnt[w[pos]];
}
void del(int pos)
{
--cnt[w[pos]];
if(!cnt[w[pos]]) --now;
}
int main()
{
scanf("%d%d", &n, &m);
siz = sqrt(n);
num = ceil((double) n / siz);
// cout << num << endl;
for (int i = 1; i <= num; i ++)
{
for (int j = (i - 1) * siz + 1; j <= i * siz; j ++)
{
belong[j] = i;
}
}
for (int i = 1; i <= n; i ++) scanf("%d", &w[i]);
for (int i = 1; i <= m; i ++)
{
q[i].id = i;
scanf("%d%d", &q[i].l, &q[i].r);
}
sort(q + 1, q + m + 1, cmp);
int l = 1, r = 0;
for (int i = 1; i <= m; i ++)
{
int ql = q[i].l, qr = q[i].r;
while(l < ql) del(l ++);
while(l > ql) add(-- l);
while(r < qr) add(++ r);
while(r > qr) del(r --);
ans[q[i].id] = now;
}
for (int i = 1; i <= m; i ++)
printf("%d\n", ans[i]);
} | 1 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.