solution
stringlengths 52
181k
| difficulty
int64 0
6
|
---|---|
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
struct Bri{
int fn;
int tn;
int cost;
bool operator <(const Bri &bBri)const{
return cost>bBri.cost;
}
};
int main(void){
while (true){
int citynum, brinum;
cin >> citynum >> brinum;
if (citynum == 0 && brinum == 0)break;
int CostCol[100][100];
for (int i = 0; i < 100; ++i){
for (int j = 0; j < 100; ++j){
CostCol[i][j] = 10000000;
}
}
vector<int> IsSelected;
for (int i = 0; i < citynum; ++i){
IsSelected.push_back(0);
}
vector<Bri> BriQue;
for (int i = 0; i < brinum; ++i){
int a, b, cost;
cin >> a >> b >> cost;
BriQue.push_back({ a, b, cost });
}
sort(BriQue.begin(), BriQue.end());
reverse(BriQue.begin(),BriQue.end());
int CostSum=0;
IsSelected[BriQue[0].fn] = 1; IsSelected[BriQue[0].tn] = 1;
CostSum += BriQue[0].cost;
while (1){
for (auto it = BriQue.begin(); it != BriQue.end(); ++it){
if (IsSelected[(*it).fn] + IsSelected[(*it).tn] == 1){
IsSelected[(*it).fn] = 1;
IsSelected[(*it).tn] = 1;
CostSum += (*it).cost;
break;
}
else{
}
}
if (all_of(IsSelected.begin(), IsSelected.end(), [](int x){return x == 1; })){
cout << CostSum << endl;
break;
}
}
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010;
const int MAXA0 = 110;
int N, A0[MAXN], A[MAXN], M;
long double p[MAXN][MAXA0], ans;
void Move(int u, int v) {
long double nxtp[MAXA0];
int i;
for (i = 1; i <= A0[u]; i++) nxtp[i] = 0;
nxtp[0] = p[u][0];
for (i = 1; i <= A0[u]; i++) {
nxtp[i - 1] += p[u][i] * i / A[u];
nxtp[i] += p[u][i] * (A[u] - i) / A[u];
}
A[u]--;
for (i = 0; i <= A0[u]; i++) p[u][i] = nxtp[i];
}
int main() {
scanf("%d", &N);
int i, j;
for (i = 1; i <= N; i++) {
scanf("%d", &A0[i]);
A[i] = A0[i];
}
for (i = 1; i <= N; i++) {
for (j = 0; j <= A0[i] - 1; j++) p[i][j] = 0;
p[i][A0[i]] = 1;
}
ans = 0;
for (i = 1; i <= N; i++) ans += p[i][0];
scanf("%d", &M);
for (i = 1; i <= M; i++) {
int u, v, k;
scanf("%d%d%d", &u, &v, &k);
ans -= p[u][0];
for (int kk = k; kk; kk--) Move(u, v);
ans += p[u][0];
A[v] += k;
printf("%.14f\n", double(ans));
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T>
void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
__print(t);
if (sizeof...(v)) cerr << ", ";
_print(v...);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int t = 1;
while (t--) {
long long int n = 0, m = 0, k = 0, i = 0, j = 0, p = 0, q = 0, x = 0, y = 0,
z = 0, ans = 0, cnt = 0, l = 0, r = 0, mid = 0, lo = 0,
hi = 0;
string s;
bool flag = false;
cin >> n;
for (i = 0; i <= n; i++) x = (i + 1) * (n - i), cnt = max(cnt, x);
cout << cnt << endl;
;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
inline void splay(int &v) {
v = 0;
char c = 0;
int p = 1;
while (c < '0' || c > '9') {
if (c == '-') p = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
v = (v << 3) + (v << 1) + c - '0';
c = getchar();
}
v *= p;
}
struct node {
char s;
int num;
} a[33];
bool comp(const node &a, const node &b) {
if (a.num & 1) return 0;
if (b.num & 1) return 1;
return a.num < b.num;
}
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
void tepan() {
int A = a[1].num, B = a[2].num;
int k = gcd(A, B);
if (k == A || k == B) k /= 2;
A /= k;
B /= k;
int ans = 2;
if (A & 1) ans--;
if (B & 1) ans--;
printf("%d\n", k * ans);
for (int i = 1; i <= k; i++) {
for (int j = 1; j <= a[1].num / k; j++) {
putchar(a[1].s);
}
for (int j = 1; j <= a[2].num / k; j++) {
putchar(a[2].s);
}
}
}
int main() {
int n;
splay(n);
int sum = 0, tot = 0;
for (int i = 1; i <= n; i++) {
splay(a[i].num);
a[i].s = 'a' + i - 1;
sum += a[i].num;
}
for (int i = n; i >= 1; i--) {
if (a[i].num & 1) ++tot;
}
sort(a + 1, a + n + 1, comp);
if (n == 1) {
printf("%d\n", sum);
for (int j = 1; j <= a[1].num; j++) {
putchar(a[1].s);
}
return 0;
}
if (tot >= 2) {
printf("0\n");
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= a[i].num; j++) {
putchar(a[i].s);
}
}
return 0;
}
if (tot == 1) {
int k = a[1].num;
for (int i = 2; i <= n; i++) {
k = gcd(k, a[i].num);
}
printf("%d\n", k);
for (int T = 1; T <= k; T++) {
for (int i = 1; i <= n - 1; i++) {
for (int j = 1; j <= a[i].num / 2 / k; j++) {
putchar(a[i].s);
}
}
for (int i = 1; i <= a[n].num / k; i++) {
putchar(a[n].s);
}
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= a[i].num / 2 / k; j++) {
putchar(a[i].s);
}
}
}
return 0;
} else {
if (n == 2) tepan(), exit(0);
int k = a[1].num;
for (int i = 2; i <= n; i++) k = gcd(k, a[i].num);
printf("%d\n", k);
for (int i = 1; i <= k / 2; i++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= a[i].num / k; j++) {
putchar(a[i].s);
}
}
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= a[i].num / k; j++) {
putchar(a[i].s);
}
}
}
}
}
| 5 |
#include<string>
#include<deque>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<tuple>
#include<chrono>
#include<functional>
#include<random>
#include<unordered_set>
using namespace std;
typedef long long int llint;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
const int mod=1000000007;
const int big=1e9+10;
const long double pai=3.141592653589793238462643383279;
template <class T,class U>void mineq(T& a,U b){if(a>b){a=b;}}
template <class T,class U>void maxeq(T& a,U b){if(a<b){a=b;}}
llint gcd(llint a,llint b){if(b==0){return a;}return gcd(b,a%b);}//a>b
llint lcm(llint a,llint b){return (a/gcd(max(a,b),min(a,b)))*b;}
int main(void){
int n,m,q,i,j,zyu=1,nai=0;cin>>n>>m>>q;
vector<int>ban(q);//入力 番号
vector<deque<int>>numlis(m+1);//どこに番号があるのだろう?
multiset<int>las;//最後に番号入れたのいつ?
vector<bool>mita(m+1);
for(i=0;i<n;i++){las.ins(big);}
for(i=0;i<q;i++){
int in;cin>>in;
ban[i]=in;
numlis[in].puf(i);
}
for(i=q-1;i>=0;i--){
int no=ban[i];
if(mita[no]){continue;}//skipped
bool mincan=false;
while(mita[zyu]){zyu++;}
mita[no]=true;
mincan=(zyu==no);
vector<int>iti;
int x=0;
for(j=0;j<numlis[no].size()&&x<n;j++){
auto it=las.lower_bound(numlis[no][j]);
if(it==las.end()){continue;}
x++;//成功数
iti.pub(numlis[no][j]);
las.erase(it);
}
if(x<n&&(!mincan)){cout<<"No"<<endl;return 0;}
for(;x<n-nai;x++){
auto it=las.lower_bound(-1);
iti.pub(-999);
las.erase(it);
nai++;
}
while(iti.size()!=0){las.ins(iti.back());iti.pob();}
//cout<<endl<<"de i="<<i;
//for(auto it=las.begin();it!=las.end();it++){cout<<" "<<(*it);}
}
cout<<"Yes"<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
void init_ios() {
ios_base::sync_with_stdio(0);
cin.tie(0);
}
const int N = 5 * (int)1e6 + 100;
int res[N], t, a, b, wyn[N];
vector<int> prime;
bool zloz[N];
int main() {
init_ios();
zloz[0] = zloz[1] = true;
for (int i = 2; i < 3000; ++i)
if (!zloz[i]) {
prime.push_back(i);
for (int j = 2 * i; j < 3000; j += i) zloz[j] = true;
}
res[1] = 0, res[2] = 1;
wyn[1] = 0, wyn[2] = 1;
for (int i = 3; i <= 5 * (int)1e6; ++i) {
wyn[i] = wyn[i - 1];
for (int j = 0; j < prime.size(); ++j)
if (i % prime[j] == 0) {
res[i] = 1 + res[i / prime[j]];
wyn[i] += 1 + res[i / prime[j]];
goto lec;
}
res[i] = 1;
wyn[i] += 1;
lec:
a = a;
}
cin >> t;
while (t--) {
cin >> a >> b;
cout << wyn[a] - wyn[b] << "\n";
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
if (min(n, m) % 2 == 1)
cout << "Akshat";
else
cout << "Malvika";
}
| 1 |
#include <bits/stdc++.h>
int main() {
int x1, x2, x3;
scanf("%d%d%d", &x1, &x2, &x3);
printf("%d", (abs(x1 - x2) + abs(x1 - x3) + abs(x2 - x3)) / 2);
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long n, x, y, nr0, nr1, ans, i;
char ch;
int a[300005];
int main() {
cin >> n >> x >> y;
for (i = 1; i <= n; i++) {
cin >> ch;
a[i] = ch - '0';
}
a[0] = 1;
for (i = 1; i <= n; i++) {
if (a[i] && !a[i - 1]) nr1++;
if (!a[i] && a[i - 1]) nr0++;
}
if (!nr0) {
cout << 0;
return 0;
}
if (a[n] == 1) nr1--;
ans = nr1 * x + y;
ans = min(ans, nr0 * y);
cout << ans;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
using pi = pair<long long int, long long int>;
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long int t;
cin >> t;
while (t--) {
bool flag = 0;
long long int n;
cin >> n;
long long int arr[n + 1];
for (int i = 0; i < n; ++i) cin >> arr[i];
for (long long int i = 1; i < n - 1; i++) {
long long int num = arr[i - 1];
if (num == arr[i + 1]) {
flag = 1;
break;
}
for (long long int j = i + 1; j < n; j++) {
if (num == arr[j]) {
flag = 1;
break;
}
}
if (flag == 1) break;
}
if (!flag)
cout << "NO" << endl;
else
cout << "YES" << endl;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const unsigned long long maxn = 1e12;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
int a[n], b[n - 1];
for (int i = (int)0; i <= (int)n - 1; i++) cin >> a[i];
sort(a, a + n);
if (a[n - 1] - a[0] > 2 * k) {
cout << "-1\n";
} else
cout << a[0] + k << endl;
}
}
| 2 |
#include "bits/stdc++.h"
#define YES "YES"
#define NO "NO"
#define YESNO OUT(three(solve(),YES,NO))
#define ECHO OUT(solve())
#define three(A,B,C) ((A)?(B):(C))
#define FOR(i,a,b) for(LL i=(a);i< (LL)(b);i++)
#define EFOR(i,a,b) for(LL i=(a);i<=(LL)(b);i++)
#define RFOR(i,a,b) for(LL i=(b);i>=(LL)(a);i--)
#define REP(i,b) FOR(i,zero,b)
#define rep REP
#define EREP(i,b) EFOR(i,zero,b)
#define RREP(i,b) RFOR(i,b,zero)
#define ALL(c) c.begin(),c.end()
#define UNIQUE(c) sort(ALL(c));c.erase(unique(ALL(c)),c.end())
#define MAX(c) (*max_element(ALL(c)))
#define MIN(c) (*min_element(ALL(c)))
#define MP make_pair
#define FI first
#define SE second
#define SI(x) (LL(x.size()))
#define PB push_back
#define DEBUG(a) OUT(a)
#define DEBUG2(a,b) OUT2(a,b)
#define cat cout << __LINE__ << endl
#define OUT(a) cout << (a) << endl
#define OUT2(a,b) cout << (a) <<" "<<(b) << endl
#define zero 0LL
#define all ALL
#define pb emplace_back
#define eb pb
#define int long long
using namespace std;
template<typename T> inline void maximize(T &a, T b) { a = max(a, b); }
template<typename T> inline void minimize(T &a, T b) { a = min(a, b); }
template<typename T> inline bool middle(T a, T b, T c) { return b <= a && a <= c; }
template<class T> inline bool MX(T &l, const T &r) { return l < r ? l = r, 1 : 0; }
template<class T> inline bool MN(T &l, const T &r) { return l > r ? l = r, 1 : 0; }
typedef int LL;
typedef double ld;
typedef int ut;
typedef vector<ut> VI;
typedef vector<VI> VII;
typedef pair<ut, ut> pr;
typedef pair<ut, pr> ppr;
typedef vector<pr> Vpr;
typedef vector<ppr> Vppr;
typedef tuple<int, int, int, int> tapu;
typedef vector<tapu> Vtapu;
typedef priority_queue<tapu, Vtapu, greater<tapu> > PQ;
inline void outputVI(VI x) { REP(i, SI(x)) { cout << three(i, " ", "") << x[i]; }OUT(""); }
const int SIZE1 = 3e5 + 1000;
const int SIZE2 = 5010;
const int SIZE3 = 430;
const int SIZE = SIZE1;
const int MAPSIZE = 40;
const LL p = 7 + 1e9;
const LL INF = 1LL << 60;
const long double EPS = 1e-7;
typedef pair<ld, ut> pld;
int dx[]={0,0,1,-1};
int dy[]={-1,1,0,0};
signed main(){
LL A,B,X;
string s;
LL N;
cin >> N;
cin >> s;
LL x=0,y=0;
char st[]={'A','N','a','n'};
char ed[]={'M','Z','m','z'};
REP(i,N){
REP(j,4)
if(st[j]<=s[i] and s[i]<=ed[j]){
x+=dx[j];
y+=dy[j];
}
}
// cout << x << "" << y << endl;
cout << abs(x)+abs(y) << endl;
REP(j,4){
FOR(i,1,SIZE) if((dx[j]*i== x and dx[j]) or (dy[j] and dy[j]*i==y)){
// cout << i << " " << j << endl;
REP(k,i) cout <<st[j];
}
}
cout << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int dp[402][402][402];
int a[410];
int n, m;
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i < n + 1; ++i) {
scanf("%d", &a[i]);
}
for (int i = 1; i <= n; ++i) {
for (int j = i; j <= n; ++j) {
dp[i][j][0] = a[j] - a[i];
}
}
for (int i = 1; i <= n; ++i) {
for (int k = 0; k <= n; ++k) {
int j = i + 1;
int cur = i + 1;
while (cur <= n) {
if (dp[i][j][k] > a[cur] - a[j]) {
dp[i][cur][k + 1] = max(a[cur] - a[j], dp[i][j][k]);
cur++;
} else {
int t = a[cur] - a[j];
while (dp[i][j][k] < a[cur] - a[j]) {
t = min(t, max(dp[i][j][k], a[cur] - a[j]));
j++;
}
t = min(t, max(dp[i][j][k], a[cur] - a[j]));
dp[i][cur][k + 1] = t;
cur++;
}
}
}
}
long long ans = 0;
for (int i = 0; i < m; ++i) {
int s, f, c, k;
scanf("%d%d%d%d", &s, &f, &c, &k);
long long t = (long long)dp[s][f][k] * c;
ans = max(ans, t);
}
printf("%lld\n", ans);
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
long long power(long long x, long long y, long long p) {
long long res = 1;
x = x % p;
while (y > 0) {
if (y & 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
int n, m;
map<int, unordered_set<int> > c;
int color[100007];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> color[i];
int a, b;
for (int i = 0; i < m; i++) {
cin >> a >> b;
if (color[a] != color[b]) {
c[color[a]].insert(color[b]);
c[color[b]].insert(color[a]);
}
}
int ms = 0, mc = -1;
for (auto i : c) {
if (i.second.size() > ms || (i.second.size() == ms && i.first < mc)) {
ms = i.second.size();
mc = i.first;
}
}
if (mc == -1) {
int mn = INT_MAX;
for (int i = 1; i <= n; i++) mn = min(mn, color[i]);
cout << mn << '\n';
} else
cout << mc << '\n';
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
bool ron = 0, gon = 0;
string s;
int sum[8] = {0};
while (n--) {
cin >> s;
int r = -1, g = -1, d = -1;
for (int i = 0; i < m; i++) {
if (s[i] == 'R')
r = i;
else if (s[i] == 'G')
g = i;
else
d = i;
}
if ((r == -1 && g == -1) || d == -1) continue;
if (g == -1) {
gon = 1;
continue;
}
if (r == -1) {
ron = 1;
continue;
}
int tmp = abs(r - g) - 1;
for (int i = 0; i < 8; i++) sum[i] += bool(tmp & (1 << i));
}
if (ron && gon)
cout << "Draw\n";
else if (ron)
cout << "First\n";
else if (gon)
cout << "Second\n";
else {
bool sec = 1;
for (int i = 0; i < 8; i++)
if (sum[i] % (k + 1)) sec = 0;
if (sec)
cout << "Second\n";
else
cout << "First\n";
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int main() {
long long int n, m;
cin >> n >> m;
bool flag = false;
long long int a = pow(2ll * (n + m), 0.5) + 3;
if (n > m) {
swap(n, m);
flag = true;
}
long long int b = pow(2ll * n, 0.5) + 3;
long long int ans = 0, ans1 = 0;
for (long long int i = a; i >= 1; i--) {
if (i * (i + 1) / 2 <= n + m) {
ans = i;
break;
}
}
for (long long int i = b; i >= 1; i--) {
if (i * (i + 1) / 2 <= n) {
ans1 = i;
break;
}
}
bool arr1[ans + 1], arr[ans + 1];
for (long long int i = 0; i <= ans; i++) {
arr[i] = false;
}
for (long long int i = 0; i <= ans; i++) arr1[i] = false;
long long int sum = 0;
for (long long int i = ans; i >= 1; i--) {
if (sum + i > n) {
continue;
} else {
sum += i;
arr1[i] = true;
}
}
for (long long int i = 1; i <= ans; i++) {
if (arr1[i] == false) arr[i] = true;
}
int count1 = 0, count = 0;
for (long long int i = 1; i <= ans; i++)
if (arr1[i] == true) count1++;
for (long long int i = 1; i <= ans; i++)
if (arr[i] == true) count++;
if (flag == false) {
cout << count1 << "\n";
for (long long int i = 0; i <= ans; i++)
if (arr1[i] == true) cout << i << " ";
if (count1 != 0) cout << "\n";
cout << count << "\n";
for (long long int i = 0; i <= ans; i++)
if (arr[i] == true) cout << i << " ";
} else {
cout << count << "\n";
for (long long int i = 0; i <= ans; i++)
if (arr[i] == true) cout << i << " ";
if (count != 0) cout << "\n";
cout << count1 << "\n";
for (long long int i = 0; i <= ans; i++)
if (arr1[i] == true) cout << i << " ";
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int a[1001];
int n, s, p;
int main() {
cin >> n;
for (int i = 0; i < n; i++) a[i] = 0;
p = 0;
for (int i = 0; i <= n; i++) {
p = (p + i) % n;
if (a[p] == 0) a[p]++;
}
for (int i = 0; i < n; i++) {
s += a[i];
}
if (s == n)
cout << "YES";
else
cout << "NO";
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5;
const long long inf = 1e15;
long long sum[N], mx[N], a[N], ans = -inf;
int n;
vector<int> g[N];
bool used[N];
void dfs1(int u) {
used[u] = true;
sum[u] = a[u];
mx[u] = -inf;
vector<long long> v;
for (int i = 0; i < g[u].size(); i++) {
int to = g[u][i];
if (used[to]) continue;
dfs1(to);
sum[u] += sum[to];
mx[u] = max(mx[u], mx[to]);
v.push_back(mx[to]);
}
sort(v.begin(), v.end());
reverse(v.begin(), v.end());
if (v.size() > 1) {
ans = max(ans, v[0] + v[1]);
}
mx[u] = max(mx[u], sum[u]);
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]);
for (int i = 1; i < n; i++) {
int x, y;
scanf("%d%d", &x, &y);
g[x].push_back(y);
g[y].push_back(x);
}
dfs1(1);
if (ans == -inf)
cout << "Impossible";
else
cout << ans;
return 0;
}
| 4 |
#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define pb push_back
#define INF 0x3f3f3f3f
using namespace std;
int n,h,a[100001],b[100001],ma,ans;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n>>h;
for (int i=0;i<n;++i) cin>>a[i]>>b[i];
sort(a,a+n);
ma=a[n-1];
sort(b,b+n);
for (int i=n-1;i>=0;--i)
if (b[i]>ma && h>0) h-=b[i],++ans;
if (h>0) ans+=(h+ma-1)/ma;
cout<<ans;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int n;
const int maxn = 9000012;
const int l = 30;
map<int, int> m1;
map<long long int, long long int> par;
long long int cnt = 2, kid[maxn][2], ans[maxn], a[maxn];
void add(int a) {
int root = 1;
for (int i = l - 1; i >= 0; i--) {
bool ch = (a & (1 << i));
if (kid[root][ch] == 0) {
kid[root][ch] = cnt;
par[cnt] = root;
cnt++;
}
root = kid[root][ch];
}
m1[root] = a;
ans[root]++;
}
int fnd(int a, int root = 1, int b = 0) {
for (int i = l - 1; i >= 0; i--) {
bool ch = (a & (1 << i));
if (kid[root][ch] != 0) {
root = kid[root][ch];
b += (1 << i) * ch;
} else {
root = kid[root][!ch];
b += (1 << i) * (!ch);
}
}
ans[root]--;
if (ans[root] == 0) {
int dad = par[root];
for (int i = 0; i < l; i++) {
bool ch = (b & (1 << i));
if (kid[dad][!ch] != 0) {
kid[dad][ch] = 0;
break;
}
kid[dad][ch] = 0;
dad = par[dad];
}
}
return m1[root];
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) {
int x;
cin >> x;
add(x);
}
for (int i = 0; i < n; i++) {
int x = fnd(a[i]);
cout << (a[i] ^ x) << ' ';
}
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
string s;
cin >> n;
cin >> s;
map<char, int> elems_number;
for (int i = 0; i < n; i++) {
elems_number[s[i]]++;
}
vector<char> keys;
for (pair<char, int> elem : elems_number) keys.push_back(elem.first);
sort(keys.begin(), keys.end());
if (elems_number.size() == 1) {
cout << keys[0] << endl;
return 0;
}
map<pair<char, char>, char> third;
third[make_pair('B', 'G')] = 'R';
third[make_pair('G', 'R')] = 'B';
third[make_pair('B', 'R')] = 'G';
map<char, string> without;
without['R'] = "BG";
without['G'] = "BR";
without['B'] = "GR";
if (elems_number.size() == 2) {
if (elems_number[keys[0]] == elems_number[keys[1]]) {
if (elems_number[keys[0]] == 1) {
cout << third[make_pair(keys[0], keys[1])];
return 0;
}
cout << "BGR\n";
return 0;
}
if (elems_number[keys[0]] >= 2 && elems_number[keys[1]] >= 2)
cout << "BGR\n";
else {
if (elems_number[keys[0]] == 1) {
cout << without[keys[1]] << endl;
return 0;
}
cout << without[keys[0]] << endl;
}
return 0;
}
cout << "BGR" << endl;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, N) for (int i = 0; i < (int)N; i++)
const ll MOD = pow(10,9)+7;
const ll LLINF = pow(2,61)-1;
const int INF = pow(2,30)-1;
int main() {
int N; cin >> N;
ll T[N]; rep(i,N) cin >> T[i];
ll A[N]; rep(i,N) cin >> A[i];
int pt; pt = lower_bound(T,T+N,T[N-1]) - T;
int pa; pa = upper_bound(A,A+N,A[0],greater<ll>()) - (A+1);
if (pt>pa||T[N-1]!=A[0]) { cout << 0 << endl; return 0; }
ll result = 1;
rep(i,max(0,pa-pt-1)) { result *= A[0]; result %= MOD; }
for (int i=1; i<=pt; i++) {
if (T[i]==T[i-1]) {
result *= T[i];
result %= MOD;
}
}
for (int i=N-1; i>=pa; i--) {
if (A[i]==A[i+1]) {
result *= A[i];
result %= MOD;
}
}
// cout << pt << " " << pa << endl;
cout << result << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
const long long INF = 1e18 + 1;
const long long N = 1e5 + 5;
const long long MOD = 1e18 + 1;
using namespace std;
vector<long long> b;
vector<vector<long long> > g(N + 5);
long long n, m, k, r, x, y, l, c, t, cur, ans, mn(INF), mn2(INF), mn3(INF),
mx(-INF), a[N + 5], ar[N + 5];
char c1, c2;
string s;
bool f, fl, used[N + 5];
void read(long long &in) { scanf("%I64d", &in); }
void read(long long &in1, long long &in2) { scanf("%I64d%I64d", &in1, &in2); }
void read(long long &in1, long long &in2, long long &in3) {
scanf("%I64d%I64d%I64d", &in1, &in2, &in3);
}
long long gcd(long long a, long long b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
void dfs(long long x) {
used[x] = true;
for (int i = 0; i < g[x].size(); i++) {
if (!used[g[x][i]]) {
dfs(g[x][i]);
}
}
}
long long bpow(long long x, long long p) {
long long res;
if (p == 0) {
return 1;
} else {
res = bpow(x, p / 2);
res = (res * res) % MOD;
if (p % 2 == 1) {
res = (res * x) % MOD;
}
return res;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> a[i];
mx = max(a[i], mx);
mn = min(a[i], mn);
}
if (k > 2) {
cout << mx;
} else if (k == 1) {
cout << mn;
} else {
mx = -INF;
mn = INF;
ar[n - 1] = a[n - 1];
for (int i = n - 2; i >= 0; i--) {
ar[i] = min(a[i], ar[i + 1]);
}
for (int i = 0; i < n - 1; i++) {
mn = min(a[i], mn);
mx = max(mx, max(mn, ar[i + 1]));
}
cout << mx;
}
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const int INF = 1e9;
const long long IINF = 1e18;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const char dir[4] = {'D', 'R', 'U', 'L'};
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
for (T &x : v) is >> x;
return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
for (int i = 0; i < v.size(); ++i) {
os << v[i] << (i + 1 == v.size() ? "" : " ");
}
return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
cout << '(' << p.first << ',' << p.second << ')';
return os;
}
template <class T1, class T2>
inline bool chmin(T1 &a, T2 b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <class T1, class T2>
inline bool chmax(T1 &a, T2 b) {
if (a < b) {
a = b;
return true;
}
return false;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int n, m;
cin >> n >> m;
vector<int> a(n);
cin >> a;
vector<int> l(m), r(m);
vector<pair<int, int>> seg;
for (int i = 0; i < m; ++i) {
cin >> l[i] >> r[i];
seg.emplace_back(--l[i], --r[i]);
}
sort((seg).begin(), (seg).end());
int update = 0, ans = -INF, pos, now = 0,
Max = *max_element((a).begin(), (a).end());
vector<int> imos(n + 1, 0), sum(n);
for (int i = 0; i < n; ++i) sum[i] = a[i];
priority_queue<pair<int, int>, vector<pair<int, int>>,
greater<pair<int, int>>>
pq;
for (int i = 0; i < n; ++i) {
while (now < m && seg[now].first == i) {
auto p = seg[now++];
--imos[p.first];
++imos[p.second + 1];
update = 1;
pq.emplace(p.second, p.first);
}
if (update) {
int rui = 0;
Max = -INF;
for (int j = 0; j < n; ++j) {
rui += imos[j];
sum[j] = a[j] + rui;
chmax(Max, sum[j]);
}
}
if (chmax(ans, Max - sum[i])) pos = i;
update = 0;
while (!pq.empty() && pq.top().first == i) {
auto p = pq.top();
pq.pop();
++imos[p.second];
--imos[p.first + 1];
update = 1;
}
}
int maxi = *max_element((a).begin(), (a).end()),
mini = *min_element((a).begin(), (a).end());
if (chmax(ans, maxi - mini)) {
cout << ans << '\n';
cout << 0 << '\n';
cout << '\n';
return 0;
}
cout << ans << '\n';
vector<int> res;
for (int i = 0; i < m; ++i) {
if (l[i] <= pos && pos <= r[i]) {
res.emplace_back(i + 1);
}
}
cout << res.size() << '\n';
cout << res << '\n';
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, w, u, v;
scanf("%d%d%d%d", &n, &w, &v, &u);
double t = 0;
bool ok1 = true, ok2 = true;
for (int i = 0; i < n; ++i) {
double x, y;
scanf("%lf%lf", &x, &y);
if (x - v * y / u < 0) {
ok1 = false;
}
if (x - v * y / u > 0) {
ok2 = false;
}
if (x > 0) {
t = max(t, max(y / u, x / v) + (w - y) / u);
}
}
if (ok1 || ok2) {
t = w * 1.0 / u;
}
printf("%.10f\n", t);
return 0;
}
| 3 |
#include <iostream>
using namespace std;
int main(){
int n,m,a[100];
cin >> n >> m;
for(int i = 0;i < n;i++){
cin >> a[i];
}
for(int i = 1;i <= m;i++){
for(int j = 0;j < n - 1;j++){
if(a[j] % i > a[j + 1] % i){
int t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
}
for(int i = 0;i < n;i++) cout << a[i] << endl;
return 0;
}
| 0 |
#include<bits/stdc++.h>
#include<math.h>
using namespace std;
typedef long long ll;
const int INF = 1<<30;
int main(){
int ans=0, last=0;
for(int i=0;i<5;i++){
int x;cin>>x;
int t=(x/10)*10+(x%10!=0)*10;
last = max(last, t-x);
ans+=t;
}
cout << ans - last << endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while (t--) {
double a, b, c, d, k;
cin >> a >> b >> c >> d >> k;
if (ceil(a / c) + ceil(b / d) <= k) {
cout << (int)ceil(a / c) << " " << (int)ceil(b / d) << "\n";
} else {
cout << "-1\n";
}
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
namespace TNT {
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
inline int qpow(int x, int y) {
int ans = 1;
while (y) {
if (y & 1) ans = ans * x;
x = x * x;
y >>= 1;
}
return ans;
}
inline void write(int x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
inline void main() {
int n = read();
if (n % 2)
write((n - 1) / 2), putchar('\n');
else {
int pos;
n >>= 1;
for (register int i = 28; i >= 0; i--)
if (n >> i) {
n -= qpow(2, i);
break;
}
write(n), putchar('\n');
}
return;
}
} // namespace TNT
int main() {
TNT::main();
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long int DP[150000], DP_old[150000];
struct MaxStack {
vector<long long int> S;
vector<long long int> M;
void Push(long long int x) {
S.push_back(x);
if ((int)(M).size() == 0)
M.push_back(x);
else
M.push_back((((x) > (M.back())) ? (x) : (M.back())));
}
long long int Pop(void) {
long long int b = S.back();
S.pop_back();
M.pop_back();
return b;
}
long long int Max(void) {
if ((int)(M).size())
return (M.back());
else
return -1000000000000000001LL;
}
int Size(void) { return ((int)(S).size()); }
bool Empty(void) { return (Size() == 0); }
};
struct MaxQueue {
MaxStack S1, S2;
void Push(long long int x) { S2.Push(x); }
long long int Pop(void) {
if (S1.Empty())
while (!S2.Empty()) S1.Push(S2.Pop());
return S1.Pop();
}
long long int Max(void) {
return ((((S1.Max()) > (S2.Max())) ? (S1.Max()) : (S2.Max())));
}
int Size(void) { return (S1.Size() + S2.Size()); }
bool Empty(void) { return (S1.Empty() && S2.Empty()); }
};
int main() {
ios_base::sync_with_stdio(0);
int n, m, d;
cin >> n >> m >> d;
int last_t = -n;
for (int i = (1); i <= (m); ++i) {
int a, b, t;
cin >> a >> b >> t;
a--;
MaxQueue Q;
int it_l = 0, it_r = -1,
dist = (((n) < ((long long int)d * (t - last_t)))
? (n)
: ((long long int)d * (t - last_t)));
for (int j = (0); j <= (n - 1); ++j) {
while (it_r < n - 1 && it_r < j + dist) Q.Push(DP_old[++it_r]);
while (it_l < j - dist) {
++it_l;
Q.Pop();
}
DP[j] = Q.Max() + b - (((a - j) > 0) ? (a - j) : -(a - j));
}
for (int j = (0); j <= (n - 1); ++j) DP_old[j] = DP[j];
last_t = t;
}
long long int ans = -1000000000000000001LL;
for (int j = (0); j <= (n - 1); ++j)
ans = (((ans) > (DP[j])) ? (ans) : (DP[j]));
cout << ans;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const long long PI = 3.1415926535897932384626;
const long long mod = 1000000007;
const long long FMOD = 998244353;
const long long N = 100005;
const double eps = 1e-9;
int main() {
long long t;
cin >> t;
while (t--) {
long long n;
cin >> n;
long long a[n + 2];
for (long long i = 1; i < n + 1; i++) cin >> a[i];
long long f[n + 2], ans[n + 2], last[n + 2];
for (long long i = 1; i < n + 1; i++) {
ans[i] = -1;
f[i] = last[i] = 0;
}
for (long long i = 1; i < n + 1; i++) {
long long x = a[i];
f[x] = max(f[x], i - last[x]);
last[x] = i;
}
for (long long x = 1; x < n + 1; x++) {
f[x] = max(f[x], n - last[x] + 1);
for (int i = f[x]; i <= n && ans[i] == -1; i++) {
ans[i] = x;
}
}
for (long long i = 1; i < n + 1; i++) {
cout << ans[i] << " ";
}
cout << "\n";
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int taulu[51][51];
int t;
cin >> t;
for (int u = 0; u < t; u++) {
for (int i = 0; i < 51; i++) {
for (int j = 0; j < 51; j++) {
taulu[i][j] = 1;
}
}
int n;
cin >> n;
for (int i = 0; i < n; i++) {
string row;
cin >> row;
for (int a = 0; a < row.size(); a++) taulu[i][a] = row[a] - 48;
}
bool b = false;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (taulu[i][j] == 1 && (taulu[i + 1][j]) == 0 &&
(taulu[i][j + 1]) == 0) {
b = true;
break;
}
}
if (b) break;
}
if (b)
cout << "No\n";
else
cout << "Yes\n";
}
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int n, m, t, ans;
int main() {
vector<pair<int, int> > g;
vector<int> napr, used;
cin >> n >> m >> t >> t;
for (int i = 0; i < n; i++) {
cin >> t;
g.push_back(make_pair(t, 1));
}
for (int i = 0; i < m; i++) {
cin >> t;
g.push_back(make_pair(t, 0));
}
napr.resize(n + m, 0);
used.resize(n + m, 0);
sort(g.begin(), g.end());
for (int i = 0; i < n + m; i++)
if (g[i].second == 1) {
if (i && g[i - 1].second == 0 &&
(i == n + m - 1 || g[i + 1].second == 1)) {
int d = abs(g[i - 1].first - g[i].first);
napr[i] = 1;
for (int j = i; j < n + m; j++)
if (g[j].second == 0) {
if (d > abs(g[i].first - g[j].first)) napr[i] = 0;
break;
}
}
if (i != n + m - 1 && g[i + 1].second == 0 &&
(i == 0 || g[i - 1].second == 1)) {
int d = abs(g[i + 1].first - g[i].first);
napr[i] = 2;
for (int j = i; j >= 0; j--)
if (g[j].second == 0) {
if (d > abs(g[i].first - g[j].first)) napr[i] = 0;
break;
}
}
if (i && i < n + m - 1 && g[i - 1].second == 0 && g[i + 1].second == 0)
if (abs(g[i + 1].first - g[i].first) < abs(g[i - 1].first - g[i].first))
napr[i] = 2;
else if (abs(g[i + 1].first - g[i].first) >
abs(g[i - 1].first - g[i].first))
napr[i] = 1;
else
napr[i] = 12;
}
for (int i = 0; i < n + m; i++)
if (g[i].second == 1 && !used[i]) {
if (napr[i] == 0) {
ans++;
continue;
}
if (i && (napr[i] == 1 || napr[i] == 12) && g[i - 1].second == 0) {
if (!used[i - 1] || (used[i - 1] == abs(g[i].first - g[i - 1].first))) {
used[i - 1] = abs(g[i].first - g[i - 1].first);
continue;
} else {
if (used[i - 1] >= abs(g[i].first - g[i - 1].first) && napr[i] == 1) {
ans++;
continue;
}
}
}
if (i < n + m - 1 && (napr[i] == 2 || napr[i] == 12) &&
g[i + 1].second == 0 && !used[i + 1]) {
used[i + 1] = abs(g[i].first - g[i + 1].first);
continue;
}
ans++;
}
cout << ans;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 500;
const int INF = 1e9;
int cost[N][N];
struct dsu {
vector<int> id;
dsu(int size) {
id = vector<int>(size);
iota(id.begin(), id.end(), 0);
}
int find_set(int x) { return id[x] == x ? x : id[x] = find_set(id[x]); }
bool unite_sets(int x, int y) {
x = find_set(x);
y = find_set(y);
id[x] = y;
return x != y;
}
};
int main() {
int n, m, k;
scanf("%d %d %d", &n, &m, &k);
vector<int> id(n), K(k);
int sum = 0;
for (int i = 0; i < k; ++i) {
scanf("%d", &K[i]);
for (int j = sum; j < sum + K[i]; ++j) {
id[j] = i;
}
sum += K[i];
fill(cost[i], cost[i] + k, INF);
cost[i][i] = 0;
}
dsu sets(n);
for (int i = 0; i < m; ++i) {
int u, v, c;
scanf("%d %d %d", &u, &v, &c);
--u;
--v;
if (u >= n or v >= n) {
continue;
}
if (!c) {
sets.unite_sets(u, v);
}
if (id[u] != id[v]) {
u = id[u];
v = id[v];
cost[u][v] = min(cost[u][v], c);
cost[v][u] = min(cost[v][u], c);
}
}
vector<int> set_id(k, -1);
for (int i = 0; i < n; ++i) {
int x = sets.find_set(i);
if (set_id[id[i]] == -1) {
set_id[id[i]] = x;
} else if (set_id[id[i]] != x) {
return !printf("No\n");
}
}
for (int x = 0; x < k; ++x) {
for (int i = 0; i < k; ++i) {
for (int j = 0; j < k; ++j) {
cost[i][j] = min(cost[i][j], cost[i][x] + cost[x][j]);
}
}
}
printf("Yes\n");
for (int i = 0; i < k; ++i) {
for (int j = 0; j < k; ++j) {
printf("%d ", cost[i][j] == INF ? -1 : cost[i][j]);
}
printf("\n");
}
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
int k;
string s;
set<pair<string, int> > pq;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> s;
int k;
cin >> k;
long long n = s.size();
if (k > n * (n + 1) / 2) return 0 * puts("No such line.");
for (int i = 0; i < s.size(); i++) {
string tmp;
tmp.push_back(s[i]);
pq.insert(make_pair(tmp, i));
}
while (--k) {
auto it = (*pq.begin());
pq.erase(pq.begin());
if (it.second < s.size() - 1) {
it.second++;
it.first += s[it.second];
pq.insert(it);
}
}
cout << (*pq.begin()).first << '\n';
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1;
const int D = 2e6 + 1;
int a[N], m[D], M[D];
set<int> s;
int main() {
int n, x, y;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int x = i + 2;
scanf("%d", &x);
s.insert(x);
}
scanf("%d %d", &x, &y);
n = 0;
while (s.size()) {
a[n++] = *s.begin();
s.erase(s.begin());
}
for (int i = 0; i < n; i++) {
int b = y / a[i] * a[i];
for (int j = b; j <= x; j += a[i])
if (j >= y) m[j - y] = a[i];
}
int end = y;
for (int i = y; i <= x; i++) {
end = max(end, i);
if (m[i - y] && i + m[i - y] - 1 >= end)
for (int j = end; j <= i + m[i - y] - 1 && j <= x; j++)
M[j - y] = m[i - y], end++;
}
int ans = 0;
while (x > y) {
if (M[x - y] == 0 || x % M[x - y] == 0)
x--;
else
x -= x % M[x - y];
ans++;
}
printf("%d", ans);
}
| 3 |
#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
using namespace std;
namespace {
const int MOD107 = 1000000007;
const int MOD998 = 998244353;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <class T>
using minheap = priority_queue<T, vector<T>, greater<T>>;
template <class T>
using maxheap = priority_queue<T, vector<T>, less<T>>;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
struct Modular {
private:
long long MODVALUE = 0;
long long val = 0;
Modular initModular(long long val) const { return Modular(val, MODVALUE); }
inline void validate() const {}
inline void validate(const Modular& other) const {}
public:
explicit operator long long() const { return val; }
template <class T>
Modular& operator=(const T& other) {
val = (other % MODVALUE + MODVALUE) % MODVALUE;
return *this;
}
template <class T>
void setMod(const T& MOD) {
MODVALUE = MOD;
}
Modular& operator+=(const Modular& other) {
validate(other);
val += other.val;
if (val >= MODVALUE) val -= MODVALUE;
return *this;
}
Modular operator+(const Modular& other) const {
Modular tmp = *this;
return tmp += other;
}
template <class T>
Modular& operator+=(const T& other) {
return *this += initModular(other);
}
template <class T>
Modular operator+(const T& other) const {
return *this + initModular(other);
}
template <class T>
friend Modular operator+(const T& other, const Modular& m) {
return m.initModular(other) + m;
}
Modular& operator++() { return *this += 1; }
Modular operator++(int) {
Modular tmp = *this;
++*this;
return tmp;
}
Modular operator-() const { return initModular(-val); }
Modular& operator-=(const Modular& other) {
validate(other);
val -= other.val;
if (val < 0) val += MODVALUE;
return *this;
}
Modular operator-(const Modular& other) const {
Modular tmp = *this;
return tmp -= other;
}
template <class T>
Modular& operator-=(const T& other) {
return *this -= initModular(other);
}
template <class T>
Modular operator-(const T& other) const {
return *this - initModular(other);
}
template <class T>
friend Modular operator-(const T& other, const Modular& m) {
return m.initModular(other) - m;
}
Modular& operator--() { return *this -= 1; }
Modular operator--(int) {
Modular tmp = *this;
--*this;
return tmp;
}
Modular& operator*=(const Modular& other) {
validate(other);
val *= other.val;
val %= MODVALUE;
if (val < 0) val += MODVALUE;
return *this;
}
Modular operator*(const Modular& other) const {
Modular tmp = *this;
return tmp *= other;
}
template <class T>
Modular& operator*=(const T& other) {
return *this *= initModular(other);
}
template <class T>
Modular operator*(const T& other) const {
return *this * initModular(other);
}
template <class T>
friend Modular operator*(const T& other, const Modular& m) {
return m.initModular(other) * m;
}
Modular pow(long long power) const {
Modular m = *this, ans = initModular(1);
while (power) {
if (power & 1) ans *= m;
m *= m;
power >>= 1;
}
return ans;
}
Modular pow(const Modular& other) const { return pow(other.val); }
Modular inv() const { return pow(MODVALUE - 2); }
Modular& operator/=(const Modular& other) {
val *= other.inv().val;
val %= MODVALUE;
if (val < 0) val += MODVALUE;
return *this;
}
Modular operator/(const Modular& other) const {
Modular tmp = *this;
return tmp /= other;
}
template <class T>
Modular& operator/=(const T& other) {
return *this /= initModular(other);
}
template <class T>
Modular operator/(const T& other) const {
return *this / initModular(other);
}
template <class T>
friend Modular operator/(const T& other, const Modular& m) {
return m.initModular(other) / m;
}
friend istream& operator>>(istream& in, Modular& m) {
in >> m.val;
m.val %= m.MODVALUE;
if (m.val < 0) m.val += m.MODVALUE;
return in;
}
friend ostream& operator<<(ostream& out, const Modular& m) {
return out << m.val;
}
Modular() {
val = 0;
MODVALUE = 0;
}
Modular(long long val, long long MOD)
: MODVALUE(MOD), val((val % MOD + MOD) % MOD) {}
};
using Mint = Modular;
inline ll binpow(ll b, ll p, ll mod) {
b %= mod;
ll ans = 1;
for (; p > 0; p >>= 1) {
if (p & 1) {
ans *= b;
ans %= mod;
}
b *= b;
b %= mod;
}
return ans;
}
inline ll max(ll a, int b) { return (a > b ? a : b); }
inline ll max(int a, ll b) { return (a > b ? a : b); }
inline ll min(ll a, int b) { return (a < b ? a : b); }
inline ll min(int a, ll b) { return (a < b ? a : b); }
template <class T>
inline bool chkmin(T& x, const T& y) {
return x > y ? x = y, 1 : 0;
}
template <class T>
inline bool chkmax(T& x, const T& y) {
return x < y ? x = y, 1 : 0;
}
} // namespace
namespace IO {
template <class T>
inline void _R(T& x) {
cin >> x;
}
template <class T1, class T2>
inline void _R(pair<T1, T2>& x) {
_R(x.first);
_R(x.second);
}
template <class T>
inline void _R(vector<T>& x) {
for (auto& i : x) _R(i);
}
template <class T>
inline void _R(deque<T>& x) {
for (auto& i : x) _R(i);
}
inline void R() {}
template <class T1, class... T2>
inline void R(T1& x, T2&... y) {
_R(x);
R(y...);
}
template <class T>
inline void _W(const T& x);
template <class T1, class T2>
inline void _W(const pair<T1, T2>& p);
template <class... T>
inline void _W(const vector<T...>& x);
template <class... T>
inline void _W(const deque<T...>& x);
template <class... T>
inline void _W(const forward_list<T...>& x);
template <class... T>
inline void _W(const list<T...>& x);
template <class... T>
inline void _W(const set<T...>& x);
template <class... T>
inline void _W(const multiset<T...>& x);
template <class... T>
inline void _W(const unordered_set<T...>& x);
template <class... T>
inline void _W(const unordered_multiset<T...>& x);
template <class... T>
inline void _W(const map<T...>& x);
template <class... T>
inline void _W(const multimap<T...>& x);
template <class... T>
inline void _W(const unordered_map<T...>& x);
template <class... T>
inline void _W(const unordered_multimap<T...>& x);
template <class Container>
inline void printContainer(const Container& x) {
bool isFirst = 1;
for (auto i : x) {
if (!isFirst) cout << ' ';
isFirst = 0;
_W(i);
}
}
template <class T>
inline void _W(const T& x) {
cout << x;
}
template <class T1, class T2>
inline void _W(const pair<T1, T2>& p) {
_W(p.first);
_W(' ');
_W(p.second);
}
template <class... T>
inline void _W(const vector<T...>& x) {
printContainer(x);
}
template <class... T>
inline void _W(const deque<T...>& x) {
printContainer(x);
}
template <class... T>
inline void _W(const forward_list<T...>& x) {
printContainer(x);
}
template <class... T>
inline void _W(const list<T...>& x) {
printContainer(x);
}
template <class... T>
inline void _W(const set<T...>& x) {
printContainer(x);
}
template <class... T>
inline void _W(const multiset<T...>& x) {
printContainer(x);
}
template <class... T>
inline void _W(const unordered_set<T...>& x) {
printContainer(x);
}
template <class... T>
inline void _W(const unordered_multiset<T...>& x) {
printContainer(x);
}
template <class... T>
inline void _W(const map<T...>& x) {
printContainer(x);
}
template <class... T>
inline void _W(const multimap<T...>& x) {
printContainer(x);
}
template <class... T>
inline void _W(const unordered_map<T...>& x) {
printContainer(x);
}
template <class... T>
inline void _W(const unordered_multimap<T...>& x) {
printContainer(x);
}
inline void W() { cout << '\n'; }
template <class T>
inline void W(const T& x) {
_W(x);
W();
}
template <class T1, class... T2>
inline void W(const T1& x, const T2&... y) {
_W(x);
_W(' ');
W(y...);
}
} // namespace IO
using namespace IO;
namespace outputFormat {
inline string Case(int tc) { return "Case #" + to_string(tc) + ':'; }
inline string YN(bool b) { return b ? "Yes" : "No"; }
} // namespace outputFormat
using namespace outputFormat;
namespace std {
template <class T1, class T2>
struct hash<pair<T1, T2>> {
std::size_t operator()(pair<T1, T2> _) const {
return hash<T1>{}(_.first) ^ hash<T2>{}(_.second);
}
};
} // namespace std
int main() {
int n, m;
R(n, m);
vector<vector<int>> g(n + 1), rg(n + 1);
for (int i = 0; i < m; i++) {
int u, v;
R(u, v);
g[u].emplace_back(v);
rg[v].emplace_back(u);
}
{
bool ok = 1;
vector<int> state(n + 1, 0);
function<void(int)> dfs_cycle = [&](int u) {
if (state[u] == 2) return;
if (state[u] == 1) {
ok = 0;
return;
}
state[u] = 1;
for (auto& v : g[u]) dfs_cycle(v);
state[u] = 2;
};
for (int u = 1; u <= n; u++) dfs_cycle(u);
if (!ok) {
W(-1);
return 0;
}
}
vector<bool> can(n + 1, true), rcan(n + 1, true);
string q(n + 1, 'E');
function<void(vector<vector<int>>&, vector<bool>&, int)> dfs =
[&](vector<vector<int>>& g, vector<bool>& can, int u) {
if (!can[u]) return;
can[u] = 0;
for (auto& v : g[u]) dfs(g, can, v);
};
int ans = 0;
for (int u = 1; u <= n; u++) {
if (can[u] && rcan[u]) {
ans++;
q[u] = 'A';
}
dfs(g, can, u);
dfs(rg, rcan, u);
}
W(ans);
W(q.substr(1, n));
return 0;
}
static auto __init__ = []() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << fixed << setprecision(10);
return 42;
}();
| 3 |
#include <bits/stdc++.h>
using namespace std;
long long z[300001];
long long pot[300001];
void po(int n) {
int sum = 1;
pot[0] = 1;
while (n > 0) {
pot[sum] = (pot[sum - 1] * 2) % 1000000007;
sum++;
n--;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
po(300000);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> z[i];
}
sort(z, z + n);
long long sumamn = 0;
long long sumamx = 0;
for (int i = 0; i < n; i++) {
sumamn = (sumamn + ((pot[n - i - 1] * z[i]) % 1000000007)) % 1000000007;
sumamx = (sumamx + (((pot[i]) * z[i]) % 1000000007)) % 1000000007;
}
cout << ((sumamx - sumamn) + 1000000007) % 1000000007;
return 0;
}
| 3 |
#include<iostream>
#include<vector>
#include<utility>
#include<cmath>
#include<iomanip>
using namespace std;
struct Point{double x,y;};
void koch(int n,Point a,Point b){
if(n==0) return;
Point s,t,u;
double th=M_PI*60.0/180.0;
s.x=(2.0*a.x+1.0*b.x)/3.0;
s.y=(2.0*a.y+1.0*b.y)/3.0;
t.x=(1.0*a.x+2.0*b.x)/3.0;
t.y=(1.0*a.y+2.0*b.y)/3.0;
u.x=(t.x-s.x)*cos(th)-(t.y-s.y)*sin(th)+s.x;
u.y=(t.x-s.x)*sin(th)+(t.y-s.y)*cos(th)+s.y;
koch(n-1,a,s);
cout << s.x << " " << s.y << endl;
koch(n-1,s,u);
cout << u.x << " " << u.y << endl;
koch(n-1,u,t);
cout << t.x << " " << t.y << endl;
koch(n-1,t,b);
}
int main(){
cout << fixed << setprecision(10);
Point p1,p2;p1.x=0;p1.y=0;p2.x=100;p2.y=0;
int n;cin >> n;
cout << p1.x << " " << p1.y << endl;
koch(n,p1,p2);
cout << p2.x << " " << p2.y << endl;
}
| 0 |
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 1e5 + 10;
const long long mod = 1e9 + 7;
long long pw(long long a, long long b, long long md = mod) {
long long res = 1;
while (b) {
if (b & 1) {
res = (a * res) % md;
}
a = (a * a) % md;
b >>= 1;
}
return (res);
}
int q;
int n, k;
int a[maxn];
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cin >> q;
while (q--) {
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
long long ans = -1e15;
for (int i = 1; i <= n; i++)
for (long long j = i - 1; j >= 1 and i - j <= k + 1000; j--)
ans = max(ans, i * j - k * (a[i] | a[j]));
cout << ans << '\n';
}
return (0);
}
| 2 |
#include <cstdio> // printf(), scanf()
#include <cmath> // sqrt()
#define MAX_N 80000
using namespace std;
class Point
{
public:
double x, y;
Point(double x = 0, double y = 0): x(x), y(y) {}
Point operator - (Point p) { return Point(x - p.x, y - p.y); }
};
typedef Point Vector;
Point qs[MAX_N];
int n;
double
dot(Vector a, Vector b)
{
return a.x * b.x + a.y * b.y;
}
double
cross(Vector a, Vector b)
{
return a.x * b.y - a.y * b.x;
}
double
dist(Vector a, Vector b)
{
return dot(a - b, a - b);
}
bool cmp_func(const Point &l, const Point &r)
{
return l.x != r.x ? l.x < r.x : l.y < r.y;
}
double
solve()
{
int i = 0, j = 0; // ?????????????????????????????????
// x ??????????????????????????????????±???????
for (int k = 0; k < n; ++k)
{
if (!cmp_func(qs[i], qs[k]))
i = k;
if (cmp_func(qs[j], qs[k]))
j = k;
}
double res = 0;
int si = i, sj = j;
while (i != sj || j != si) // ????????? 180 ??????????????????
{
double t = dist(qs[i], qs[j]);
if (t > res)
res = t;
// ??? i-(i+1) ?????????????????¨??? j-(j+1) ???????????????????????????????????????????????????
if (cross(qs[(i + 1) % n] - qs[i], qs[(j + 1) % n] - qs[j]) < 0)
i = (i + 1) % n; // ??? i-(i+1) ??????????????????????????????
else
j = (j + 1) % n; // ??? j-(j+1) ??????????????????????????????
}
return sqrt(res);
}
int
main(int argc, char** argv)
{
scanf("%d", &n);
for (int i = 0; i < n; ++i)
scanf("%lf %lf", &qs[i].x, &qs[i].y);
printf("%.12f\n", solve());
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
inline long long rd() {
long long x = 0;
int ch = getchar(), f = 1;
while (!isdigit(ch) && ch != '-') ch = getchar();
if (ch == '-') f = -1, ch = getchar();
while (isdigit(ch)) x = (x << 1) + (x << 3) + ch - '0', ch = getchar();
return x * f;
}
inline void rt(long long x) {
if (x < 0) putchar('-'), x = -x;
if (x >= 10) rt(x / 10);
putchar(x % 10 + '0');
}
const int maxn = 3005;
vector<int> bur[maxn];
int s[maxn], t[maxn];
int FR[maxn], FS[maxn];
int l[maxn], r[maxn];
int R, C, n, lim, pre[maxn];
int main() {
R = rd(), C = rd(), n = rd(), lim = rd() - 1;
for (int i = int(1); i <= int(n); i++) {
int x = rd(), y = rd();
bur[x].push_back(y);
}
for (int i = int(1); i <= int(3000); i++) pre[i] = pre[i - 1] + i;
for (int i = int(1); i <= int(R); i++) sort(bur[i].begin(), bur[i].end());
long long ans = 0;
for (int i = int(1); i <= int(R); i++) {
for (int j = int(0); j <= int(bur[i].size() - 1); j++) t[bur[i][j]]++;
for (int j = int(1); j <= int(C); j++) s[j] = t[j];
int lst = 0;
for (int j = int(1); j <= int(C); j++)
if (s[j]) l[j] = lst, r[lst] = j, lst = j, FS[j] = s[j];
r[lst] = C + 1, l[C + 1] = lst;
long long sum = 0;
for (int j = r[0]; j != C + 1; j = r[j]) {
int nw = j;
FR[j] = nw - 1, FS[j] = 0;
while (nw <= C && FS[j] + s[nw] <= lim)
FR[j] = r[nw] - 1, FS[j] += s[nw], nw = r[nw];
sum += (long long)(j - l[j]) * (FR[j] - j + 1) + pre[j - l[j] - 1];
}
sum += pre[C + 1 - l[C + 1] - 1];
ans += sum;
FR[C + 1] = C;
for (int k = int(2); k <= int(i); k++) {
for (int j = int(0); j <= int(bur[k - 1].size() - 1); j++) {
int p = bur[k - 1][j];
s[p]--;
if (!s[p]) {
l[r[p]] = l[p], r[l[p]] = r[p];
sum -= (long long)(p - l[p]) * (FR[p] - p + 1) + pre[p - l[p] - 1];
sum -=
(long long)(r[p] - p) * (FR[r[p]] - r[p] + 1) + pre[r[p] - p - 1];
sum += (long long)(r[p] - l[p]) * (FR[r[p]] - r[p] + 1) +
pre[r[p] - l[p] - 1];
}
for (int j = s[p] ? p : l[p]; j != 0 && FR[j] >= p - 1; j = l[j]) {
sum -= (long long)(j - l[j]) * (FR[j] - j + 1);
if (FR[j] >= p) FS[j]--;
for (int nw = FR[j] + 1; nw <= C && FS[j] + s[nw] <= lim;)
FR[j] = r[nw] - 1, FS[j] += s[nw], nw = r[nw];
sum += (long long)(j - l[j]) * (FR[j] - j + 1);
}
}
ans += sum;
}
}
rt((long long)pre[R] * pre[C] - ans), putchar('\n');
}
| 5 |
#include <bits/stdc++.h>
#define ll long long
using namespace std;
bool check(string s) {
for (int i = 0; i < s.size() - 1; i++) {
if (s[i + 1] - s[i] != 1) return false;
}
return true;
}
int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];
int ans = -1;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int mul = a[i] * a[j];
stringstream s;
s << mul;
if (check(s.str())) ans = max(ans, mul);
}
}
cout << ans << endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, v, i, bp = 0, profit = 0, buy = 0;
cin >> n >> v;
int a[n];
for (i = 0; i < n; i++) cin >> a[i];
profit = v;
for (i = 0; i < n - 1; i++)
for (int j = i + 1; j < n; j++) {
buy = v / a[i] * (a[j] - a[i]) + v;
if (profit < buy) profit = buy;
}
cout << profit;
return 0;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long modpow(long long a, long long b,
long long mod = (long long)(1e9 + 7)) {
if (!b) return 1;
a %= mod;
return modpow(a * a % mod, b / 2, mod) * (b & 1 ? a : 1) % mod;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int mxn = 2e5, mxt = 2 * mxn;
int n, m, t;
int a[mxn], tin[mxn], tout[mxn], tree[mxt], d[mxn];
vector<int> adj[mxn];
void init() {
t = 0;
for (int i = 0; i < mxt; i++) tree[i] = 0;
}
void add(int i, int x) {
for (; i < mxt; i |= i + 1) tree[i] += x;
}
int sum(int i) {
int sum = 0;
for (; i >= 0; i = (i & (i + 1)) - 1) sum += tree[i];
return sum;
}
void dfs(int v, int p) {
tin[v] = t++;
d[v] = p == -1 ? 0 : d[p] + 1;
for (int w : adj[v])
if (w != p) dfs(w, v);
tout[v] = t++;
}
int o(int v) { return d[v] % 2 ? -1 : 1; }
void solve() {
scanf("%d", &n);
scanf("%d", &m);
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
for (int _ = 0; _ < n - 1; _++) {
int x, y;
scanf("%d", &x);
scanf("%d", &y);
adj[--x].push_back(--y);
adj[y].push_back(x);
}
init();
dfs(0, -1);
stringstream ss;
for (int _ = 0; _ < m; _++) {
int cmd;
scanf("%d", &cmd);
if (cmd == 1) {
int x, val;
scanf("%d", &x);
scanf("%d", &val);
x--;
add(tin[x], o(x) * val);
add(tout[x], -1 * o(x) * val);
} else {
int x;
scanf("%d", &x);
x--;
ss << sum(tin[x]) * o(x) + a[x] << endl;
}
}
cout << ss.str();
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int tc;
cin >> tc;
;
while (tc--) {
int n;
cin >> n;
;
string s;
cin >> s;
vector<int> v(n);
v[0] = ((int)s[0] - 49);
unordered_map<int, int> mp;
long long sm = 0;
for (int i = (1); i <= (n - 1); ++i) {
v[i] = v[i - 1] + ((int)s[i] - 49);
}
for (int i = (0); i <= (n - 1); ++i) mp[v[i]]++;
sm = sm + mp[0];
for (auto d : mp) {
int h = d.second;
long long k;
if (h % 2)
k = (h - 1) / 2, k = k * h;
else
k = h / 2, k *= h - 1;
sm += k;
}
cout << sm << "\n";
}
}
| 3 |
#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
int n;
cin>>n;
unordered_map<string, int> m;
while(n--){
string s;
cin>>s;
m[s]++;
}
cout<<"AC x "<<m["AC"]<<endl<<"WA x "<<m["WA"]<<endl<<
"TLE x "<<m["TLE"]<<endl<<
"RE x "<<m["RE"]<<endl;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 100;
int n, m, k, b[maxn];
int main() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= n; i++) scanf("%d", b + i);
int sum = b[n] - b[1] + 1;
vector<int> p;
for (int i = 1; i < n; i++) p.push_back(b[i + 1] - b[i] - 1);
sort(p.rbegin(), p.rend());
for (int i = 0; i < min(n - 1, k - 1); i++) sum -= p[i];
printf("%d\n", sum);
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1e9 + 7;
const int mxN = 1000005;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int kx[8] = {2, 1, -1, -2, -2, -1, 1, 2};
int ky[8] = {1, 2, 2, 1, -1, -2, -2, -1};
long long int qpow(unsigned long long x, int y, int p) {
long long int res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1) res = (res * x) % p;
y = y / 2;
x = (x * x) % p;
}
return res;
}
long long int modInverse(unsigned long long x, int p) {
return qpow(x, p - 2, p);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
set<int> st;
map<int, int> mp;
for (int i = 0; i < n; i++) {
int a;
cin >> a;
mp[a]++;
}
int total = 0;
for (auto i : mp) {
total += i.second / 2;
}
cout << total / 2 << endl;
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
int adj[3][3], n;
long long dp[3][3][44];
long long f(int s, int e, int x) {
if (x == 0) return 0;
if (~dp[s][e][x]) return dp[s][e][x];
int m = 3 - s - e;
long long ret1 = f(s, m, x - 1) + f(m, e, x - 1) + adj[s][e];
long long ret2 =
f(s, e, x - 1) + adj[s][m] + f(e, s, x - 1) + adj[m][e] + f(s, e, x - 1);
return dp[s][e][x] = min(ret1, ret2);
}
int main() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cin >> adj[i][j];
}
}
cin >> n;
memset(dp, -1, sizeof(dp));
cout << f(0, 2, n);
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
const int N = 500010;
const int LOG = 21;
int pr[N][21], f[N][28], a[N];
char ch[N];
inline int read()
{
int x = 0, f = 1; char ch = getchar();
while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
int main()
{
scanf("%s", ch);
int len = strlen(ch);
for (int i = 0; i < len; i ++) a[i] = ch[i] - 'a';
for (int j = 0; j <= 26; j ++) f[len][j] = -1;
for (int i = len - 1; i >= 0; i --)
{
for (int j = 0; j <= 26; j ++) f[i][j] = -1;
f[i][a[i]] = i + 1;
bool flag = false;
for (int j = a[i] + 1; j <= 26; j ++)
{
int at = f[f[i][j - 1]][j - 1];
if (at == -1)
{
flag = true;
break;
}
f[i][j] = at;
}
if (!flag)
for (int j = 0; j < a[i]; j ++)
f[i][j] = f[f[i][26]][j];
}
for (int i = 0; i <= len; i ++) pr[i][0] = f[i][26];
for (int j = 1; j < LOG; j ++)
for (int i = 0; i <= len; i ++)
{
if (pr[i][j - 1] == -1) pr[i][j] = -1;
else pr[i][j] = pr[pr[i][j - 1]][j - 1];
}
int T;
T = read();
for(int Case = 1; Case <= T; Case ++)
{
int x, y;
x = read();
y = read();
x --;
for(int i = LOG - 1; i >= 0; i --)
{
if(pr[x][i] == -1) continue;
if(pr[x][i] <= y) x = pr[x][i];
}
if(x == y) cout << "Yes" << endl;
else cout << "No" << endl;
}
return 0;
}
| 0 |
//39
#include<iostream>
#include<map>
#include<set>
#include<sstream>
#include<string>
#include<cstdio>
using namespace std;
int b,l;
map<string,int> m;
map<string,map<int,int> > me;
void dec(string s){
for(int i=0;i<s.size();i++){
if(s[i]==']'||s[i]=='['){
s[i]=' ';
}
}
stringstream ss(s);
string n;
int c;
ss>>n>>c;
m[n]=c;
}
int exp(const char **p){
int n,r;
if(sscanf(*p,"%d%n",&n,&r)==1){
(*p)+=r;
return n;
}else{
char an[99];
sscanf(*p,"%[^[]%n",an,&r);
string ans=an;
*p+=r+1;
int x=exp(p);
(*p)++;
if((!me[ans].count(x)||m[ans]<=x)&&b==0){
b=l;
}
return me[ans][x];
}
}
void asn(const char *p){
int r;
char an[99];
sscanf(p,"%[^[]%n",an,&r);
string ans=an;
p+=r+1;
int x=exp(&p);
p+=2;
if(m[ans]<=x&&b==0){
b=l;
}
int v=exp(&p);
me[ans][x]=v;
}
int main(){
for(string s;cin>>s,s!=".";){
m.clear();
me.clear();
b=l=0;
for(;s!=".";cin>>s){
l++;
if(s.find('=')!=string::npos){
asn(s.c_str());
}else{
dec(s);
}
}
cout<<b<<endl;
}
return 0;
}
| 0 |
#include<iostream>
using namespace std;
class Cube {
public:
int f[6];
Cube(){}
void roll_z(void) {
roll(1,2,4,3);
}
void roll_y(void) {
roll(0,2,5,3);
}
void roll_x(void) {
roll(0,1,5,4);
}
void roll(int i,int j,int k,int l) {
int t = f[i];
f[i] = f[j];
f[j] = f[k];
f[k] = f[l];
f[l] = t;
}
};
int getRight(Cube c1,int f1,int f2) {
for (int i=0;i<6;i++) {
for (int j=0;j<4;j++) {
if (c1.f[0]==f1 && c1.f[1]==f2) {
return c1.f[2];
}
c1.roll_z();
}
if (i%2==0) {
c1.roll_y();
} else {
c1.roll_x();
}
}
return -1;
}
int main() {
Cube c;
int f1,f2;
for (int i=0; i<6; i++) {
cin >> c.f[i];
}
int q;
cin >> q;
for (int i=0; i<q; i++) {
cin >> f1 >> f2;
cout << getRight(c,f1,f2) << endl;
}
return 0;
}
| 0 |
#include <iostream>
#include <vector>
using namespace std;
const int SHN=100010;
int n;
bool visit[SHN]={0};
int a[SHN];
int b[SHN];
vector<int> edge[SHN];
int bl=0;
int wh=0;
inline void dfs(int x,int t,bool col)
{
visit[x]=1;
if (!col) a[x]=t;
else b[x]=t;
for (int i=0;i<edge[x].size();i++)
{
if (!visit[edge[x][i]]) dfs(edge[x][i],t+1,col);
}
}
int main()
{
cin >>n;
for (int i=0;i<n-1;i++)
{
int x,y;
cin >>x>>y;
x--,y--;
edge[x].push_back(y);
edge[y].push_back(x);
}
dfs(0,0,0);
fill(visit,visit+n,0);
dfs(n-1,0,1);
for (int i=0;i<n;i++)
{
int x=a[i];
int y=b[i];
if (x<=y) bl++;
else wh++;
}
if (wh>=bl) cout <<"Snuke";
else cout <<"Fennec";
return 0;
}
| 0 |
#include<bits/stdc++.h>
using namespace std;
const int MOD = 1e9+7;
int plusi(int a, int b)
{
long long cur = 0LL+a+b;
cur%=MOD;
if(cur<0)
cur+=MOD;
return (int) cur;
}
int multi(int a,int b)
{
long long cur = 1LL*a*b;
cur%=MOD;
if(cur<0)
cur+=MOD;
return (int) cur;
}
int power(int base, int pow)
{
if(pow==0)
return 1;
if(pow==1)
return base;
int cur = power(base,pow/2);
cur = multi(cur,cur);
if(pow%2==1)
cur = multi(cur,base);
return cur;
}
int inverse(int base)
{
return power(base,MOD-2);
}
int main()
{
int N,i,a=0;
cin>>N;
while(N--)
{
cin>>a;
int val[16],med[16];
/**Here goes nothing**/
for(i=0;i<16;i++)
val[i] = power(a,i);
med[0] = multi(multi(-1,33746491),plusi(-1,power(-1,a)));
med[1] = multi(multi(-1,2409750),plusi(48887,multi(33033,power(-1,a))));
med[2] = multi(multi(-1, 1485),plusi(123573553,multi(22215375,power(-1,a))));
med[3] = multi(multi(-1,486),plusi(multi(-1,27084097),multi(9634625,power(-1,a))));
med[4] = multi(multi(-1,75075),plusi(multi(-1,1091603),multi(2835,power(-1,a))));
med[5] = 61186072;
med[6] = 815140362;
med[7] = 238065852;
med[8] = 437495207;
med[9] = 54503020;
med[10] = 31381350;
med[11] = 5099276;
med[12] = 450450;
med[13] = 23240;
med[14] = 660;
med[15] = 8;
/**End of trauma**/
int ans = 0;
for(i=0;i<16;i++)
ans = plusi(ans,multi(val[i],med[i]));
cout<<multi(ans,inverse(635864652))<<"\n";
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
bool read_int(int& out) {
int c = getchar();
long long x = 0;
int neg = 0;
for (; !('0' <= c && c <= '9') && c != '-'; c = getchar()) {
if (c == EOF) return false;
}
if (c == '-') {
neg = 1;
c = getchar();
}
if (c == EOF) return false;
for (; '0' <= c && c <= '9'; c = getchar()) {
x = x * 10 + c - '0';
}
out = neg ? -x : x;
return true;
}
const int INF = 0x3f3f3f3f, MOD = 1e9 + 7;
long long gcd(long long a, long long b) { return (a ? gcd(b % a, a) : b); }
long long power(long long a, long long n) {
long long p = 1;
while (n > 0) {
if (n % 2) {
p = p * a;
}
n >>= 1;
a *= a;
}
return p;
}
long long power(long long a, long long n, long long mod) {
long long p = 1;
while (n > 0) {
if (n % 2) {
p = p * a;
p %= mod;
}
n >>= 1;
a *= a;
a %= mod;
}
return p % mod;
}
template <typename T>
istream& operator>>(istream& h, vector<T>& od) {
for (int i = 0; i < od.size(); ++i) h >> od[i];
return h;
}
template <typename T>
ostream& operator<<(ostream& h, const vector<T>& od) {
for_each(od.begin(), od.end(), [&h](const T& x) { h << x << ' '; });
return h;
}
template <typename T>
T qrt(T x) {
return x * x;
}
inline bool intersect_1(int a, int b, int c, int d) {
if (a > b) swap(a, b);
if (c > d) swap(c, d);
return max(a, c) <= min(b, d);
}
vector<pair<int, int>> factor(const long long& n) {
long long x = n;
int g = sqrt(x);
vector<pair<int, int>> v;
for (int i = 2; i <= g && i <= x; ++i) {
if (x % i == 0) {
int j = 0;
while (x % i == 0) {
x /= i;
++j;
}
v.push_back(make_pair(i, j));
}
}
if (x > 1) v.push_back(make_pair(x, 1));
return v;
}
int main() {
long long n;
cin >> n;
vector<pair<int, int>> res(n);
for (int i = 0; i < n; ++i) {
cin >> res[i].first >> res[i].second;
}
long long ans = 0;
for (int i = 0; i < n - 2; ++i) {
pair<int, int> vec1 = make_pair(res[i + 1].first - res[i].first,
res[i + 1].second - res[i].second);
pair<int, int> vec2 = make_pair(res[i + 1].first - res[i + 2].first,
res[i + 1].second - res[i + 2].second);
if (vec1.first * vec2.second - vec1.second * vec2.first < 0) ++ans;
}
cout << ans << '\n';
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
vector<pair<int, int> > g[N];
vector<int> sz;
int n;
long long mx, mn;
void makeG() {
mx = 0;
mn = 0;
sz.clear();
sz.assign(n, 1);
for (int i = 0; i < n; i++) g[i].clear();
for (int i = 0; i < n - 1; i++) {
int u, v, t;
cin >> u >> v >> t;
u--, v--;
g[u].push_back({v, t});
g[v].push_back({u, t});
}
}
void dfs(int v, int p) {
for (auto x : g[v]) {
if (x.first == p) continue;
dfs(x.first, v);
mx += 1ll *
(sz[x.first] < n - sz[x.first] ? sz[x.first] : n - sz[x.first]) *
x.second;
mn += 1ll * (sz[x.first] & 1) * x.second;
sz[v] += sz[x.first];
}
}
int32_t main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int Q;
cin >> Q;
while (Q--) {
int i;
cin >> n;
n *= 2;
makeG();
dfs(0, -1);
cout << mn << " " << mx << "\n";
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x, y, length;
char s[101];
cin >> n;
for (int i = 0; i < n; i++) {
cin >> length;
scanf("%s", s);
x = -1;
y = 0;
for (int j = 0; j < length; j++) {
if (s[j] == 'A') {
if (x != -1) {
y = max(y, j - x - 1);
}
x = j;
}
}
if (x != -1) y = max(y, length - x - 1);
cout << y << endl;
}
return 0;
}
| 1 |
#include <bits/stdc++.h>
using ll = long long;
const int MN = 5e5 + 10;
struct Range {
public:
mutable ll l, r;
bool operator<(const Range& o) const { return l < o.l; }
};
std::set<Range> set;
int N, a[MN], T, f;
ll c;
bool sign;
void check_pos() {
if (sign) {
for (auto it = set.end(); it != set.begin();) {
--it;
if (it->l >= c) {
it = set.erase(it);
continue;
} else if (it->r >= c)
it->r = c - 1;
break;
}
} else {
for (auto it = set.begin(); it != set.end();) {
if (it->r <= -c) {
it = set.erase(it);
continue;
} else if (it->l <= -c)
it->l = -c + 1;
break;
}
}
}
bool find(ll v) {
v = (1 - 2 * sign) * (v - c);
auto it = set.upper_bound({v, -1});
if (it == set.begin()) return 0;
--it;
assert(it->l <= v);
return it->r >= v;
}
void ins(ll v) {
v = (1 - 2 * sign) * (v - c);
set.insert({v, v});
}
void solve() {
scanf("%d", &N);
for (int i = 0; i < N; ++i) scanf("%d", a + i);
for (int i = 0; i < N; ++i) {
c = a[i] - c, sign ^= 1;
check_pos();
if (set.empty()) {
sign = 0, c = 0;
if (a[i] & 1)
set.insert({1, a[i] - 1}), f += 2;
else
set.insert({a[i] / 2, a[i] / 2}), ++f;
} else if (!(a[i] & 1) && find(a[i] / 2)) {
set.clear();
sign = 0, c = 0;
set.insert({a[i] / 2, a[i] / 2});
} else {
++f;
if (!(a[i] & 1)) ins(a[i] / 2);
}
}
printf("%d\n", f);
}
void reset() {
f = 0;
set.clear();
sign = 0, c = 0;
}
int main() {
scanf("%d", &T);
while (T--) solve(), reset();
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int oo = 0x3f3f3f3f;
const int NIL = -1;
const int DX[] = {-1, 0, 1, 0};
const int DY[] = {0, 1, 0, -1};
class Edge {
public:
int u, v, cost, capacity, flow;
Edge() {}
Edge(const int u, const int v, const int cost, const int capacity) {
this->u = u;
this->v = v;
this->cost = cost;
this->capacity = capacity;
this->flow = 0;
}
};
vector<vector<int>> G;
vector<Edge> Edges;
int V, E, Source, Sink;
vector<int> Father, Distance;
queue<int> Q;
vector<bool> InQ;
int Solution;
inline int NonEdge(const int e) {
if (e < E) return e + E;
return e - E;
}
void InitializeBellmanFord(const int start) {
Father = vector<int>(V, NIL);
Distance = vector<int>(V, oo);
InQ = vector<bool>(V, false);
Distance[start] = 0;
Q.push(start);
InQ[start] = true;
}
bool BellmanFord(const int start, const int end) {
for (InitializeBellmanFord(start); !Q.empty(); Q.pop()) {
int x = Q.front();
InQ[x] = false;
if (x == end) continue;
for (auto &e : G[x]) {
int y = Edges[e].v;
if (Edges[e].flow < Edges[e].capacity &&
Distance[x] + Edges[e].cost < Distance[y]) {
Father[y] = e;
Distance[y] = Distance[x] + Edges[e].cost;
if (!InQ[y]) {
Q.push(y);
InQ[y] = true;
}
}
}
}
return (Father[end] != NIL);
}
void InitializeNetwork(const int source, const int sink) {
Source = source;
Sink = sink;
for (int i = 0; i < E; ++i) {
Edges.push_back(Edge(Edges[i].v, Edges[i].u, -Edges[i].cost, 0));
G[Edges[i].v].push_back(NonEdge(i));
}
}
void MinCostMaxFlow(int &maxFlow, int &flowCost) {
maxFlow = flowCost = 0;
while (BellmanFord(Source, Sink)) {
int flow = oo;
for (int x = Sink; x != Source; x = Edges[Father[x]].u)
flow = min(flow, Edges[Father[x]].capacity - Edges[Father[x]].flow);
for (int x = Sink; x != Source; x = Edges[Father[x]].u) {
Edges[Father[x]].flow += flow;
Edges[NonEdge(Father[x])].flow -= flow;
}
maxFlow += flow;
flowCost += flow * Distance[Sink];
}
}
void Solve() {
int maxFlow, flowCost;
MinCostMaxFlow(maxFlow, flowCost);
Solution = flowCost;
}
inline void AddEdge(const int u, const int v, const int cost,
const int capacity) {
Edges.push_back(Edge(u, v, cost, capacity));
G[u].push_back(E++);
}
void Read() {
vector<vector<int>> board;
int n, m;
cin >> n >> m;
board = vector<vector<int>>(n, vector<int>(m, 0));
V = n * m + 2;
G = vector<vector<int>>(V, vector<int>());
for (int x = 0; x < n; ++x) {
for (int y = 0; y < m; ++y) {
cin >> board[x][y];
if ((x + y) & 1)
AddEdge(m * x + y, n * m + 1, 0, 1);
else
AddEdge(n * m, m * x + y, 0, 1);
}
}
for (int x = 0; x < n; ++x) {
for (int y = x & 1; y < m; y += 2) {
for (int d = 0; d < 4; ++d) {
int nx = x + DX[d], ny = y + DY[d];
if (0 <= nx && nx < n && 0 <= ny && ny < m)
AddEdge(m * x + y, m * nx + ny,
static_cast<int>(board[x][y] != board[nx][ny]), 1);
}
}
}
InitializeNetwork(n * m, n * m + 1);
}
void Print() { cout << Solution << "\n"; }
int main() {
Read();
Solve();
Print();
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int n;
int a[4][11][11][maxn];
int getlowbit(int x) { return x & (-x); }
void add(int i, int j, int k, int l, int x) {
while (l <= n) {
a[i][j][k][l] += x;
l += getlowbit(l);
}
}
int query(int i, int j, int k, int l) {
int ans = 0;
while (l) {
ans += a[i][j][k][l];
l -= getlowbit(l);
}
return ans;
}
int getInt(char ch) {
if (ch == 'A')
return 0;
else if (ch == 'T')
return 1;
else if (ch == 'C')
return 2;
else
return 3;
}
char str[maxn];
int main() {
scanf("%s", str);
n = strlen(str);
for (int i = 0; i < n; i++) {
for (int j = 1; j <= 10; j++) {
add(getInt(str[i]), j, (i + 1) % j, i + 1, 1);
}
}
int q;
cin >> q;
char ch[12];
int id, l, r;
while (q--) {
scanf("%d", &id);
if (id == 1) {
scanf("%d %s", &l, ch);
for (int i = 1; i <= 10; i++) {
add(getInt(str[l - 1]), i, l % i, l, -1);
add(getInt(ch[0]), i, l % i, l, 1);
}
str[l - 1] = ch[0];
} else {
scanf("%d%d%s", &l, &r, ch);
int m = strlen(ch);
int ans = 0;
for (int i = 0; i < m; i++) {
ans += query(getInt(ch[i]), m, (l + i) % (m), r) -
query(getInt(ch[i]), m, (l + i) % (m), l - 1);
}
printf("%d\n", ans);
}
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
using namespace std;
const long long maxn = 1005;
const long long maxm = 1000005;
const long long dd = 998244353;
long long n, m;
long long a[maxm], sav[maxm], dp[maxn][maxn], col[maxn][maxn], L[maxn], R[maxn];
vector<long long> t[maxn];
struct nod {
long long val, pos;
} uni[maxm];
inline bool cmp(nod x, nod y) { return x.val < y.val; }
inline long long read() {
long long ret = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') f = -f;
ch = getchar();
}
while (isdigit(ch)) {
ret = ret * 10 + ch - '0';
ch = getchar();
}
return ret * f;
}
void scan() {
m = read();
n = read();
for (long long k = 1; k <= n; k++) {
uni[k].val = read();
uni[k].pos = k;
}
}
void prework() {
long long lst = 0;
sort(uni + 1, uni + n + 1, cmp);
for (long long k = 1; k <= n; k++) {
if (uni[k].val != uni[k - 1].val) lst++;
sav[uni[k].pos] = lst;
}
m = lst;
long long now = 0;
for (long long k = 1; k <= n; k++)
if (sav[k] != a[now]) a[++now] = sav[k];
n = now;
for (long long k = 1; k <= n; k++) t[a[k]].push_back(k);
for (long long k = 1; k <= m; k++)
L[k] = t[k][0], R[k] = t[k][t[k].size() - 1];
for (long long k = 1; k <= m; k++)
for (long long j = k + 1; j <= m; j++)
if ((L[j] <= R[k] && R[k] < R[j]) || (R[j] >= L[k] && L[k] > L[j])) {
puts("0");
exit(0);
}
for (long long k = 1; k <= n; k++) {
long long now = k;
for (long long j = k; j <= n; j++)
col[k][j] = a[j] > a[now] ? now : now = j;
}
}
void solve() {
for (long long k = 0; k <= n + 1; k++)
for (long long j = 0; j <= k; j++) dp[k][j] = 1;
dp[0][1] = dp[1][1];
for (long long k = 2; k <= n; k++) {
for (long long l = 1; l + k - 1 <= n; l++) {
long long r = l + k - 1;
long long x = col[l][r], siz = t[a[x]].size();
long long ll = L[a[x]], rr = R[a[x]];
long long ans1 = 0, ans2 = 0, ans3 = 1;
for (long long k = l - 1; k < ll; k++)
ans1 = (ans1 + dp[l][k] * dp[k + 1][ll - 1]) % dd;
for (long long k = r + 1; k > rr; k--)
ans2 = (ans2 + dp[k][r] * dp[rr + 1][k - 1]) % dd;
dp[l][r] = ans1 * ans2 % dd;
for (long long k = 0; k < siz - 1; k++)
ans3 = ans3 * dp[t[a[x]][k] + 1][t[a[x]][k + 1] - 1] % dd;
dp[l][r] = dp[l][r] * ans3 % dd;
}
}
printf("%lld\n", dp[1][n]);
}
signed main() {
scan();
prework();
solve();
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
const int N = 210;
const int M = 300005;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const long long oo = 0x3f3f3f3f3f3f3f3fll;
const double eps = 1e-6;
int n, k;
long long C[N][N], dp[N][N][5][3];
int getNum(int cur) {
if (cur == 4) return 2;
return 4 - cur;
}
int getCon(int cur) {
if (cur == 0) return 4;
if (cur == 1) return 2;
if (cur == 2) return 1;
return 0;
}
long long dfs(int n, int k, int cur, int type) {
if (k == 0) return 1;
long long& ret = dp[n][k][cur][type];
if (ret + 1) return ret;
ret = 0;
if (type == 0) {
int num = getNum(cur);
for (int i = 0; i <= k && i <= num; i++) {
(ret += dfs(n - 1, k - i, cur, 2) * C[num][i]) %= mod;
}
}
if (type == 1) {
if (n == 0) return ret = 0;
int num = getNum(cur) * 2;
int con = getCon(cur);
for (int i = 0; i <= con && i <= k; i++)
for (int j = 0; j <= num - i * 2 && j + i <= k; j++) {
(ret += dfs(n, k - i - j, cur, 0) * C[con][i] * C[num - i * 2][j]) %=
mod;
}
}
if (type == 2) {
(ret += dfs(n, k, cur, 1)) %= mod;
if (cur == 0) {
if (k >= 1) {
(ret += dfs(n, k - 1, 1, 1) * 4) %= mod;
(ret += dfs(n, k - 1, 2, 1) * 4) %= mod;
}
if (k >= 2) {
(ret += dfs(n, k - 2, 2, 1) * 4) %= mod;
(ret += dfs(n, k - 2, 3, 1) * 8) %= mod;
(ret += dfs(n, k - 2, 4, 1) * 2) %= mod;
if (k == 2) ret += 2, ret %= mod;
}
if (k >= 3) {
(ret += dfs(n, k - 3, 3, 1) * 4) %= mod;
if (k == 3) ret += 4, ret %= mod;
}
if (k == 4) ret++, ret %= mod;
}
if (cur == 1) {
if (k >= 1) {
(ret += dfs(n, k - 1, 2, 1) * 2) %= mod;
(ret += dfs(n, k - 1, 3, 1) * 2) %= mod;
(ret += dfs(n, k - 1, 4, 1) * 1) %= mod;
}
if (k >= 2) {
if (k == 2) ret += 2, ret %= mod;
(ret += dfs(n, k - 2, 3, 1) * 3) %= mod;
}
if (k == 3) ret++, ret %= mod;
}
if (cur == 2) {
if (k >= 1) {
(ret += dfs(n, k - 1, 3, 1) * 2) %= mod;
if (k == 1) ret++, ret %= mod;
}
if (k == 2) ret++, ret %= mod;
}
if (cur == 3) {
if (k == 1) ret++, ret %= mod;
}
if (cur == 4) {
if (k >= 1) {
(ret += (dfs(n, k - 1, 3, 1) * 2)) %= mod;
}
if (k >= 2) {
if (k == 2) ret++, ret %= mod;
}
}
}
return ret;
}
int main() {
for (int i = 0; i < N; i++) {
C[i][0] = C[i][i] = 1;
for (int j = 1; j < i; j++) C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod;
}
memset(dp, -1, sizeof(dp));
scanf("%d%d", &n, &k);
long long ret = 1;
for (int i = 1; i <= k; i++) (ret *= i) %= mod;
ret = ret * dfs(n, k, 0, 2) % mod;
cout << ret << endl;
return 0;
}
| 5 |
#include "bits/stdc++.h"
using namespace std;
int main()
{
int T; cin >> T;
while(T--){
int n; cin >> n;
int mx = n*2;
int cnt = 0, dnt = 0;
int arr[mx];
for(int i = 0; i<mx; i++){
cin >> arr[i];
}
for(int i = 0; i<mx; i++){
if(arr[i]%2==0){
cnt++;
}else{
dnt++;
}
}
if(cnt==dnt){
cout << "YES\n";
}else{
cout << "NO\n";
}
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
using LL = long long int;
template <class TH>
void _dbg(const char *sdbg, TH h) {
cerr << sdbg << "=" << h << "\n";
}
template <class TH, class... TA>
void _dbg(const char *sdbg, TH h, TA... a) {
while (*sdbg != ',') cerr << *sdbg++;
cerr << "=" << h << ",";
_dbg(sdbg + 1, a...);
}
template <class T>
ostream &operator<<(ostream &os, vector<T> V) {
os << "[";
for (auto vv : V) os << vv << ",";
return os << "]";
}
template <class L, class R>
ostream &operator<<(ostream &os, pair<L, R> P) {
return os << "(" << P.st << "," << P.nd << ")";
}
const int MAXN = 1e6 + 6;
LL t[MAXN];
LL n;
LL k;
LL events[MAXN];
void solve() {
cin >> n >> k;
map<LL, LL> sum;
for (LL i = 1; i <= n; ++i) {
cin >> t[i];
}
for (LL i = 1; i <= n / 2; ++i) {
sum[t[i] + t[n - i + 1]]++;
LL minn = min(t[i], t[n - i + 1]) + 1;
LL maks = max(t[i], t[n - i + 1]) + k;
events[minn]++;
events[maks + 1]--;
}
LL res = n;
LL actRanges = 0;
for (LL i = 2; i <= 2 * k + 1; ++i) {
actRanges += events[i];
LL newRes = n;
newRes -= 2 * sum[i];
newRes = newRes - (actRanges - sum[i]);
(newRes, i);
res = min(res, newRes);
events[i] = 0;
}
cout << res << endl;
}
int main() {
int z;
cin >> z;
while (z--) {
solve();
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
struct state
{
int x;
int y;
int hand; //0:??£??¢, 1:????????????, 2:???, 3:???????????????
int dir; //0:^. 1:>, 2:v, 3:<
};
const int INF=123456789;
int n,m;
#define IN(x,y) (0<=x && x<m && 0<=y && y<n)
char f[55][55];
// (dir,hand) = (i,j)
int hx[4][4]={
{0,1,1,1},
{1,1,0,-1},
{0,-1,-1,-1},
{-1,-1,0,1}
};
int hy[4][4]={
{-1,-1,0,1},
{0,1,1,1},
{1,1,0,-1},
{0,-1,-1,-1}
};
int dx[4]={0,1,0,-1}, dy[4]={-1,0,1,0};
int dd[2]={1,-1};
// inline void printstate(state s)
// {
// int chx=s.x+hx[s.dir][s.hand];
// int chy=s.y+hy[s.dir][s.hand];
// printf(" state = (%d,%d),hand: %d(%d,%d),dir: %d\n", s.y,s.x,s.hand,chy,chx,s.dir);
//
// }
int ct[55][55]={0};
int vis[55][55][4][4]={0};
int dfs(state cur)
{
if(f[cur.y][cur.x]=='G')
{
int ret=0;
rep(i,55)rep(j,55) if(ct[i][j]) ++ret;
return ret;
}
if(vis[cur.y][cur.x][cur.hand][cur.dir]) return INF;
vis[cur.y][cur.x][cur.hand][cur.dir]=1;
// printstate(cur);
int ret=INF;
state nx;
// current_hand
int chx=cur.x+hx[cur.dir][cur.hand];
int chy=cur.y+hy[cur.dir][cur.hand];
// move forward
nx=cur;
nx.x+=dx[nx.dir];
nx.y+=dy[nx.dir];
if(f[nx.y][nx.x]!='#')
{
// printf("moved %d %d\n", nx.y,nx.x);
// ?????????????????????????????¢???
rep(i,4)
{
int tx=nx.x+hx[nx.dir][i];
int ty=nx.y+hy[nx.dir][i];
// printf("tx,ty %d,%d\n", tx,ty);
if(chx==tx && chy==ty)
{
// printf(" find! %d\n", i);
nx.hand=i;
ct[nx.y][nx.x]+=1;
ret=min(ret,dfs(nx));
ct[nx.y][nx.x]-=1;
}
}
}
// turn right,left
rep(d_id,2)
{
nx=cur;
nx.dir=(nx.dir+dd[d_id]+4)%4;
// ?????????????????????????????¢???
rep(i,4)
{
int tx=nx.x+hx[nx.dir][i];
int ty=nx.y+hy[nx.dir][i];
if(chx==tx && chy==ty)
{
nx.hand=i;
ret=min(ret,dfs(nx));
}
}
}
// move hand
rep(i,4)
{
int tx=nx.x+hx[nx.dir][i];
int ty=nx.y+hy[nx.dir][i];
if(f[ty][tx]=='#' && abs(tx-chx)<=1 && abs(ty-chy)<=1)
{
// printf("move hand from(%d,%d) to(%d,%d)\n",chy,chx,ty,tx);
nx.hand=i;
ret=min(ret,dfs(nx));
}
}
return ret;
}
int main()
{
// input
scanf(" %d %d", &n, &m);
state start;
rep(i,m+2) f[0][i]='#';
for(int i=1; i<=n; ++i)
{
f[i][0]='#';
for(int j=1; j<=m; ++j)
{
scanf(" %c", &f[i][j]);
if(f[i][j]!='.' && f[i][j]!='#' && f[i][j]!='G')
{
start.y=i;
start.x=j;
start.hand=2;
if(f[i][j]=='^') start.dir=0;
if(f[i][j]=='>') start.dir=1;
if(f[i][j]=='v') start.dir=2;
if(f[i][j]=='<') start.dir=3;
}
}
f[i][m+1]='#';
}
rep(i,m+2) f[n+1][i]='#';
n+=2;
m+=2;
ct[start.y][start.x]+=1;
int ans=dfs(start);
if(ans==INF) ans=-1;
printf("%d\n", ans);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, maxx, minn;
int nums[110];
bool flag = true;
cin >> n >> m >> minn >> maxx;
nums[0] = minn;
nums[1] = maxx;
for (int i = 2; i < m + 2; i++) cin >> nums[i];
sort(nums, nums + m + 2);
if (nums[0] != minn || nums[m + 1] != maxx) flag = false;
int cnt = m + 2;
for (int i = 0; i < m + 2; i++)
if (nums[i] == maxx || nums[i] == minn) cnt--;
if (cnt + 2 > n) flag = false;
if (flag)
cout << "Correct" << endl;
else
cout << "Incorrect" << endl;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
cin >> s;
if (s.size() % 2 == 1) {
cout << "No" << endl;
} else {
vector<char> v;
for (int i = 0; i < s.size(); i++) {
if (v.empty() || v.back() != s[i])
v.push_back(s[i]);
else
v.pop_back();
}
if (v.empty())
cout << "Yes" << endl;
else
cout << "No" << endl;
}
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int N = 5e5 + 2;
void solve() {
long long int d, m;
cin >> d >> m;
vector<long long int> bit_cnt;
for (long long int p = 1;; p *= 2) {
if (d >= p) {
bit_cnt.push_back(p);
d -= p;
} else {
bit_cnt.push_back(d);
break;
}
}
if (bit_cnt.back() == 0) bit_cnt.pop_back();
long long int ans = 1;
for (long long int i = 0; i < (int)bit_cnt.size(); i++) {
ans *= (bit_cnt[i] + 1);
ans %= m;
}
--ans;
cout << (ans + m) % m << "\n";
return;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
cin >> t;
while (t--) solve();
return 0;
}
| 2 |
#include<iostream>
using namespace std;
int main(){
int n, m, a;
while(cin >> n >> m >> a, n+m+a){
int h, p, q;
int amida[1001][101] = {};
for(int i = 0; i < m; i++){
cin >> h >> p >> q;
amida[h][p] = q;
amida[h][q] = p;
}
for(int i = 1000; i >= 1; i--){
if(amida[i][a] != 0) a = amida[i][a];
}
cout << a << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
bool prime(long long x) {
long long z = sqrt(x);
for (int i = 2; i <= z; i++)
if (x % i == 0) return 0;
return 1;
}
int main() {
long long n, m, p, q, c, i, x, j, t, k, ans, d;
cin >> t;
while (t--) {
cin >> n >> d;
if (n <= d)
cout << 1 << endl;
else if (d == 1 || prime(n))
cout << n << endl;
else {
ans = n;
x = sqrt(n) + 1;
p = n / 2;
if (d > p) d = p;
for (i = 2; i <= x; i++) {
if (n % i == 0) {
if (d >= n / i)
ans = i;
else
ans = n / i;
if (ans <= d) break;
}
if (i == d) break;
}
cout << ans << endl;
}
}
return 0;
}
| 4 |
#include <iostream>
using namespace std;
int main(){
int a,b; cin >> a >> b;
if (a+b<10) cout << a+b << endl;
else puts("error");
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int N = 300005;
int n, cc, x, a[N], fa[N];
map<int, int> nxt[N];
long long ans, sum[N];
int main() {
int T;
scanf("%d", &T);
while (T--) {
int n;
scanf("%d", &n);
cc = x = ans = 0;
sum[x] = 1;
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++) {
if (x && nxt[fa[x]][a[i]] == x)
x = fa[x];
else if (nxt[x][a[i]])
x = nxt[x][a[i]];
else
nxt[x][a[i]] = ++cc, fa[cc] = x, x = cc;
ans += sum[x]++;
}
printf("%lld\n", ans);
for (int i = 0; i <= cc; i++) sum[i] = fa[i] = 0, nxt[i].clear();
}
return 0;
}
| 6 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
vector<int> a(n + 2, 0);
bool exist = false;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
if (a[i] == k) exist = true;
}
if (!exist) {
cout << "NO\n";
continue;
}
if (n == 1) {
cout << "YES\n";
continue;
}
exist = false;
for (int i = 1; i <= n; ++i) {
if (a[i] >= k) {
int j = i + 1;
while (j <= n && a[j] < k) ++j;
if (j <= n && j - i <= 2) exist = true;
}
}
cout << (exist ? "YES\n" : "NO\n");
}
}
| 2 |
#include <bits/stdc++.h>
using namespace std;
long long a[1000005];
int main() {
int n, m, i, j;
while (scanf("%d%d", &n, &m) != EOF) {
long long ans = 0;
for (i = 0; i < n; i++) scanf("%I64d", &a[i]);
int nm = n / (2 * m);
int res = n % m;
int pos;
if ((n / m) % 2 == 1)
pos = min(nm * m + res, n - 1);
else
pos = max(nm * m + res - 1, 0);
i = j = 0;
while (i < pos) {
ans += 2 * abs(a[i] - a[pos]);
i += m;
i = min(i, pos);
}
i = n - 1;
while (i > pos) {
ans += 2 * abs(a[i] - a[pos]);
i -= m;
i = max(i, pos);
}
printf("%I64d\n", ans);
}
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T &x) {
x = 0;
char c = getchar();
int f = 1;
while (!isdigit(c)) {
if (c == '-') f = -1;
c = getchar();
}
while (isdigit(c)) {
x = x * 10 + c - '0';
c = getchar();
}
x *= f;
}
template <class T>
inline void umin(T &x, T y) {
x = x < y ? x : y;
}
template <class T>
inline void umax(T &x, T y) {
x = x > y ? x : y;
}
inline unsigned int R() {
static unsigned int seed = 416;
return seed ^= seed >> 5, seed ^= seed << 17, seed ^= seed >> 13;
}
const int N = 666666, mo = 998244353, inf = 0x3f3f3f3f;
inline void add(int &x, int y) { x = x + y < mo ? x + y : x + y - mo; }
inline void sub(int &x, int y) { x = x - y >= 0 ? x - y : x - y + mo; }
inline int power(int a, int n) {
int res = 1;
while (n) {
if (n & 1) res = 1LL * res * a % mo;
a = 1LL * a * a % mo;
n >>= 1;
}
return res;
}
int n, x[N], v[N], p[N], len, zhi[N];
bool zuo[N], you[N];
set<pair<int, int> > Set;
struct node {
int pos, tp, a, b;
} s[N];
bool cmp(node x, node y) { return 1ll * x.a * y.b < 1ll * y.a * x.b; }
namespace yzr {
struct node {
int ans, zuo, you;
} tree[N << 2];
node merge(node a, node b) {
node c;
c.zuo = 1ll * a.zuo * b.zuo % mo;
c.you = 1ll * a.you * b.you % mo;
c.ans =
((1ll * a.ans * b.you + 1ll * a.zuo * b.ans - 1ll * a.zuo * b.you) % mo +
mo) %
mo;
return c;
}
void build(int k, int l, int r) {
if (l == r) {
tree[k].ans = 1;
tree[k].zuo = 1 + mo - p[l];
tree[k].you = p[l];
return;
}
int mid = (l + r) >> 1;
build(k << 1, l, mid);
build(k << 1 | 1, mid + 1, r);
tree[k] = merge(tree[k << 1], tree[k << 1 | 1]);
}
node qry(int k, int l, int r, int L, int R) {
if (l == L && r == R) return tree[k];
int mid = (L + R) >> 1;
if (r <= mid) return qry(k << 1, l, r, L, mid);
if (l > mid) return qry(k << 1 | 1, l, r, mid + 1, R);
return merge(qry(k << 1, l, mid, L, mid),
qry(k << 1 | 1, mid + 1, r, mid + 1, R));
}
int qry(int l, int r) {
if (you[l] && zuo[r]) return 0;
if (you[l]) return qry(1, l, r, 1, n).you;
if (zuo[r]) return qry(1, l, r, 1, n).zuo;
return qry(1, l, r, 1, n).ans;
}
} // namespace yzr
int main() {
read(n);
for (register int i = (1); i <= (n); i++)
read(x[i]), read(v[i]), read(p[i]),
p[i] = 1ll * p[i] * power(100, mo - 2) % mo;
for (register int i = (1); i <= (n - 1); i++) {
s[++len] = (node){i, 0, x[i + 1] - x[i], v[i] + v[i + 1]};
if (v[i] > v[i + 1])
s[++len] = (node){i, 1, x[i + 1] - x[i], v[i] - v[i + 1]};
if (v[i] < v[i + 1])
s[++len] = (node){i, 2, x[i + 1] - x[i], -v[i] + v[i + 1]};
}
sort(s + 1, s + len + 1, cmp);
for (register int i = (1); i <= (len); i++)
zhi[i] = 1ll * s[i].a * power(s[i].b, mo - 2) % mo;
int res = zhi[1], cur = 1;
for (register int i = (1); i <= (n); i++) Set.insert(pair<int, int>(i, i));
yzr::build(1, 1, n);
for (register int j = (1); j <= (len); j++) {
if (s[j].tp == 0) {
set<pair<int, int> >::iterator it = Set.lower_bound(
pair<int, int>(s[j].pos + 1, -inf)),
it1;
it1 = it;
it1--;
cur = 1ll * cur *
power(1ll * yzr::qry((*it1).first, (*it1).second) *
yzr::qry((*it).first, (*it).second) % mo,
mo - 2) %
mo;
int L = (*it1).first, R = (*it).second;
Set.erase(it);
Set.erase(it1);
Set.insert(pair<int, int>(L, R));
cur = 1ll * cur * yzr::qry(L, R) % mo;
} else if (s[j].tp == 1) {
if (you[s[j].pos])
cur = 0;
else if (!zuo[s[j].pos]) {
set<pair<int, int> >::iterator it =
Set.lower_bound(pair<int, int>(s[j].pos, inf));
it--;
int L = (*it).first, R = (*it).second;
cur = 1ll * cur *
power(yzr::qry((*it).first, (*it).second) % mo, mo - 2) % mo;
zuo[s[j].pos] = 1;
Set.erase(it);
Set.insert(pair<int, int>(L, s[j].pos));
Set.insert(pair<int, int>(s[j].pos + 1, R));
cur = 1ll * cur * yzr::qry(L, s[j].pos) % mo *
yzr::qry(s[j].pos + 1, R) % mo;
}
} else {
if (zuo[s[j].pos + 1])
cur = 0;
else if (!you[s[j].pos + 1]) {
set<pair<int, int> >::iterator it =
Set.lower_bound(pair<int, int>(s[j].pos + 1, inf));
it--;
int L = (*it).first, R = (*it).second;
cur = 1ll * cur *
power(yzr::qry((*it).first, (*it).second) % mo, mo - 2) % mo;
you[s[j].pos + 1] = 1;
Set.erase(it);
Set.insert(pair<int, int>(L, s[j].pos));
Set.insert(pair<int, int>(s[j].pos + 1, R));
cur = 1ll * cur * yzr::qry(L, s[j].pos) % mo *
yzr::qry(s[j].pos + 1, R) % mo;
}
}
if (j < len)
res = (res + 1ll * cur * (zhi[j + 1] - zhi[j] + mo)) % mo;
else
res = (res - 1ll * cur * zhi[j]) % mo;
}
cout << (res % mo + mo) % mo;
return 0;
}
| 4 |
#include <bits/stdc++.h>
const int INF = 0x3f3f3f3f;
const double EPS = 1e-9;
using namespace std;
int main() {
int m;
cin >> m;
vector<int> aq(6, 4);
for (int i = (int)(0); i < (int)(m); ++i) {
int a, b;
cin >> a >> b;
aq[a]--;
aq[b]--;
}
int cnt = 0;
for (int i = 1; i <= 5; i++) {
if (aq[i] != 2) {
cout << "WIN\n";
return 0;
}
}
cout << "FAIL\n";
return 0;
}
| 2 |
#include "bits/stdc++.h"
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> static void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> static void amax(T &x, U y) { if(x < y) x = y; }
struct AddMaxSegmentTree {
typedef int Val;
typedef int Add;
vector<Val> nodes;
vector<Val> add;
int n;
void init(int minN) {
n = 1; while(n < minN) n *= 2;
nodes.assign(n * 2, Val());
add.assign(n * 2, Val());
}
Val getRange(int i, int j) {
if(i >= j) return -INF;
return getRangeRec(1, i, j, 0, n);
}
void addToRange(int i, int j, Add a) {
if(i < j) addToRangeRec(1, i, j, a, 0, n);
}
Val getRangeRec(int i, int qL, int qR, int L, int R) {
Val a = add[i];
if(qL <= L && R <= qR)
return nodes[i] + a;
if(a != 0) {
add[i * 2] += a;
add[i * 2 + 1] += a;
nodes[i] += a;
add[i] = 0;
}
int mid = (L + R) >> 1;
Val r = -INF;
if(qL < mid) amax(r, getRangeRec(i * 2 + 0, qL, qR, L, mid));
if(mid < qR) amax(r, getRangeRec(i * 2 + 1, qL, qR, mid, R));
nodes[i] = max(nodes[i * 2] + add[i * 2], nodes[i * 2 + 1] + add[i * 2 + 1]);
return r;
}
void addToRangeRec(int i, int qL, int qR, Add a, int L, int R) {
if(qL <= L && R <= qR) {
add[i] += a;
return;
}
int mid = (L + R) >> 1;
if(qL < mid) addToRangeRec(i * 2 + 0, qL, qR, a, L, mid);
if(mid < qR) addToRangeRec(i * 2 + 1, qL, qR, a, mid, R);
nodes[i] = max(nodes[i * 2] + add[i * 2], nodes[i * 2 + 1] + add[i * 2 + 1]);
}
};
struct Event {
int L, R;
int sign;
};
int main() {
int n; int m; int k;
while(~scanf("%d%d%d", &n, &m, &k)) {
vpii ab(k);
rep(i, k) {
int y; int x;
scanf("%d%d", &y, &x);
ab[i] = { y + x, y - x };
}
vector<pair<int, int>> border(n + m - 1, make_pair(INF, -INF));
auto setborder = [&border](int y, int x) {
int a = y + x, b = y - x;
amin(border[a].first, b);
amax(border[a].second, b);
};
rep(y, n) {
setborder(y, 0);
setborder(y, m - 1);
}
rep(x, m) {
setborder(0, x);
setborder(n - 1, x);
}
int lo = -1, up = n + m - 1;
while(up - lo > 0) {
int mid = (lo + up + 1) / 2;
vector<vector<Event>> events(n + m);
for(auto p : ab) {
int top = max(0, p.first - mid);
int bot = min(n + m - 1, p.first + 1 + mid);
int L = max(-m, p.second - mid);
int R = min(+n, p.second + 1 + mid);
events[top].push_back(Event{ L, R, +1 });
events[bot].push_back(Event{ L, R, -1 });
}
AddMaxSegmentTree segt[2];
rep(p, 2) segt[p].init(m + n + 1);
auto convert = [](int &L, int &R, int p) {
if((L & 1) != p) ++ L;
if((R & 1) != p) ++ R;
L = (L - p) / 2;
R = (R - p) / 2;
};
bool ok = true;
rep(a, n + m - 1) {
for(auto e : events[a]) {
rep(p, 2) {
int L = e.L, R = e.R;
convert(L, R, p);
segt[p].addToRange(L + m, R + m, -e.sign);
}
}
int L = border[a].first, R = border[a].second + 1;
convert(L, R, a % 2);
if(segt[a % 2].getRange(L + m, R + m) == 0) {
ok = false;
break;
}
}
if(ok)
up = mid - 1;
else
lo = mid;
}
printf("%d\n", lo + 1);
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
int ans = 0;
string s;
cin >> s;
bool flag = (s[0] == 'U');
int x = 0, y = 0;
if (flag) {
y++;
} else {
x++;
}
for (int i = 1; i < n; ++i) {
if (s[i] == 'U') {
y++;
} else {
x++;
}
if (x > y && flag) {
flag = false;
ans++;
} else if (y > x && !flag) {
flag = true;
ans++;
}
}
cout << ans;
return 0;
}
| 2 |
#include <bits/stdc++.h>
const int pi = acos(-1.);
using namespace std;
int r[500010], c[500010];
int ans = 1, n, m;
int mod = 1000003;
void calc() {
for (int i = 1; i <= n; i++) {
int cnt = 0;
if (r[i] & 1) cnt++;
if (r[i] & 2) cnt++;
r[i] = cnt;
}
for (int i = 1; i <= m; i++) {
int cnt = 0;
if (c[i] & 1) cnt++;
if (c[i] & 2) cnt++;
c[i] = cnt;
}
}
int f(int a, int b) {
if (b % 2)
return a;
else
return 3 - a;
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) r[i] = 3;
for (int i = 1; i <= m; i++) c[i] = 3;
for (int i = 1; i <= n; i++) {
string xx;
cin >> xx;
for (int j = 1; j <= m; j++)
if (xx[j - 1] != '.') {
if (xx[j - 1] == '1') {
r[i] -= (r[i] & f(1, j));
c[j] -= (c[j] & f(1, i));
} else if (xx[j - 1] == '2') {
r[i] -= (r[i] & f(1, j));
c[j] -= (c[j] & f(2, i));
} else if (xx[j - 1] == '3') {
r[i] -= (r[i] & f(2, j));
c[j] -= (c[j] & f(2, i));
} else {
r[i] -= (r[i] & f(2, j));
c[j] -= (c[j] & f(1, i));
}
}
}
calc();
for (int i = 1; i <= n; i++) ans = (ans * r[i]) % mod;
for (int i = 1; i <= m; i++) ans = (ans * c[i]) % mod;
cout << ans << endl;
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
const int maxn = 5e5 + 5;
int n, m;
vector<int> G[maxn];
bool vis[maxn];
int pre[maxn];
bool bfs(int u) {
queue<pair<int, int> > que;
que.push(make_pair(1, 0));
memset(pre, -1, sizeof(pre));
vis[1] = 1;
while (!que.empty()) {
pair<int, int> p = que.front();
que.pop();
int u = p.first, step = p.second;
for (int i = 0; i < G[u].size(); i++) {
int v = G[u][i];
if (vis[v]) continue;
vis[v] = true;
pre[v] = u;
if (step + 1 <= 4) {
que.push(make_pair(v, step + 1));
}
}
}
while (!que.empty()) que.pop();
if (pre[n] == -1) return false;
int pos = n;
vector<int> ans;
ans.push_back(pos);
while (pre[pos] != -1) {
ans.push_back(pre[pos]);
pos = pre[pos];
}
reverse(ans.begin(), ans.end());
if (ans.size() > 5) return false;
printf("%d\n", ans.size() - 1);
for (int i = 0; i < ans.size(); i++) printf("%d ", ans[i]);
cout << endl;
return true;
}
void display(int a, int b, int c, int d, int e, int f) {
printf("5\n%d %d %d %d %d %d\n", a, b, c, d, e, f);
}
void bfs2(int x, vector<int> &cache) {
vis[x] = 1;
queue<int> que;
que.push(x);
cache.push_back(x);
while (!que.empty()) {
int u = que.front();
que.pop();
for (int i = 0; i < G[u].size(); i++) {
if (vis[G[u][i]] || u == 1) continue;
vis[G[u][i]] = 1;
que.push(G[u][i]);
cache.push_back(G[u][i]);
}
}
}
set<int> mys[maxn];
int main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v), G[v].push_back(u);
mys[u].insert(v);
mys[v].insert(u);
}
if (m == 0) return puts("-1"), 0;
if (n == 1) return printf("1\n1\n"), 0;
if (bfs(1)) return 0;
memset(vis, 0, sizeof(vis));
for (int i = 0; i < G[1].size(); i++) {
vis[G[1][i]] = 1;
}
for (int i = 0; i < G[1].size(); i++) {
int u = G[1][i];
for (int j = 0; j < G[u].size(); j++) {
int v = G[u][j];
if (v == 1 || vis[v]) continue;
printf("4\n1 %d %d 1 %d\n", u, v, n);
return 0;
}
}
memset(vis, 0, sizeof(vis));
vector<int> cache;
for (int i = 0; i < G[1].size(); i++) {
int u = G[1][i];
if (vis[u]) continue;
cache.clear();
bfs2(u, cache);
if (G[u].size() < cache.size()) {
for (int j = 0; j < cache.size(); j++) {
u = cache[j];
if (!mys[u].count(1)) continue;
if (G[u].size() >= cache.size()) continue;
for (int k = 0; k < G[1].size(); k++) {
int z = G[1][k];
if (!vis[z] || mys[z].count(u) || z == u) continue;
for (int f = 0; f < G[z].size(); f++) {
int v = G[z][f];
if (!mys[v].count(1) || !mys[v].count(u)) continue;
if (v == 1) continue;
display(1, u, v, z, u, n);
return 0;
}
}
}
}
}
puts("-1");
return 0;
}
| 4 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int n, q;
long long a[maxn], mxr[maxn];
long long t[maxn];
void add(int i, int x) {
while (i <= n) {
mxr[i] += x;
i += (i & -i);
}
}
long long sum(int i) {
long long rel = 0;
while (i > 0) {
rel += mxr[i];
i -= (i & -i);
}
return rel;
}
int main() {
long long ans = 0;
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]);
sort(a + 1, a + 1 + n);
int l, r;
for (int i = 0; i < q; i++) {
scanf("%d%d", &l, &r);
add(l, 1);
add(r + 1, -1);
}
for (int i = 1; i <= n; i++) t[i] = sum(i);
sort(t + 1, t + 1 + n);
for (int i = n; i >= 1; i--) ans += a[i] * t[i];
printf("%I64d\n", ans);
return 0;
}
| 3 |
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<long long> a(n);
for (auto &x : a) {
cin >> x;
}
int ans = 1;
int i = 0, j = 0;
while (i < n) {
j = max(i, j);
while (j < n) {
if (j + 1 < n && a[j + 1] <= a[j] * 2) {
++j;
} else {
break;
}
}
ans = max(ans, j - i + 1);
++i;
}
cout << ans;
return 0;
}
| 2 |
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define PI acos(-1)
#define Mod (int)1000000007
#define INFTY (int)INT_MAX
#define Rep(i, n) for (int i = 0; i < (int)(n); i++)
#define BitRep(i, n) for (int i = 0; i < (int)(1 << n); i++)
#define All(vec) vec.begin(), vec.end()
#define Sum(vec) accumulate(vec.begin(), vec.end(), 0)
#define Sort(vec) sort(vec.begin(), vec.end())
#define Reverse(vec) reverse(vec.begin(), vec.end())
#define Count(vec, x) count(vec.begin(), vec.end(), x)
#define Next_permutation(vec) next_permutation(vec.begin(), vec.end())
#define Find(S, s) S.find(s) != string::npos
int main() {
int n;
cin >> n;
vector<int> A(n);
Rep(i, n) cin >> A[i];
int q;
cin >> q;
Rep(i, q) {
int b, e, k;
cin >> b >> e >> k;
cout << count(A.begin() + b, A.begin() + e, k) << endl;
}
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
long long n, m, res = 0;
cin >> n >> m;
res = n / m;
cout << ((res % 2 == 1) ? "yes" : "no");
return 0;
}
| 1 |
#include <iostream>
#include<cstdlib>
#include<queue>
#include<set>
#include<vector>
#include<string>
#include<algorithm>
#include<sstream>
#include<cmath>
#include<stack>
#include<map>
#include<cstdio>
using namespace std;
#define rep(i,a) for(int i=0;i<a;i++)
#define mp make_pair
#define pb push_back
#define P pair<int,int>
#define ll long long
string a,b;
char t[2000];
int main(){
cin>>a>>b;
int as=a.size(),bs=b.size();
int num=as/2,pos=0;
bool ok=0,ok2;
/*for(int k=0;k<2;k++){
int i=0,j=0;
string tmp="";
bool f=k;
while(true){
if(f){//aからとる
if(a[pos]==a[i]){tmp.push_back(a[i]);pos++;f=!f;}
i++;
}
else if(!f){
if(a[pos]==b[j]){tmp.push_back(b[j]);pos++;f=!f;}
j++;
}
if(j>=bs)break;
if(tmp.size()==as)break;
}
if(tmp==a)ok=1;
}*/
for(int k=0;k<2;k++){
pos=0;
rep(l,2000)t[l]=' ';
for(int i=k;i<as;i+=2){
t[pos++]=a[i];
}
for(int i=0;i<bs;i++){
int c=0;
if(b[i]==t[c]){
for(int j=0;j<bs;j++){
if(b[j]==t[c])c++;
}
if(c==pos)ok=1;
}
}
}
if(ok||as==1)cout<<"Yes"<<endl;
else cout<<"No"<<endl;
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 + 10;
int dp[maxn][4], a[maxn];
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int n, rest = 0;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) {
dp[i][0] = min(min(dp[i - 1][0], dp[i - 1][1]), dp[i - 1][2]) + 1;
if (a[i] == 1 or a[i] == 3)
dp[i][1] = min(dp[i - 1][2], dp[i - 1][0]);
else
dp[i][1] = INT_MAX;
if (a[i] == 2 or a[i] == 3)
dp[i][2] = min(dp[i - 1][1], dp[i - 1][0]);
else
dp[i][2] = INT_MAX;
}
cout << min(min(dp[n][1], dp[n][2]), dp[n][0]) << endl;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
const int MaxN = 110000;
int n, Father[MaxN], w[MaxN], Ans;
inline void Get(const int x) {
int t, Root;
long long s = 0;
for (t = x; Father[t] != t; t = Father[t]) s += w[t];
s %= 1000000007;
for (Root = t, t = x; Father[t] != t; t = Father[t]) {
Father[t] = Root;
w[t] = s;
if ((s -= w[t]) < 0) s += 1000000007;
}
}
int Union(const int x, const int y, const int z) {
Get(x);
Get(y);
int Res = w[Father[y]] = ((z + 1000000007) % 1000000007 + w[y]) % 1000000007;
Father[Father[y]] = x;
return Res;
}
int main() {
cin.sync_with_stdio(0);
cin >> n;
for (int i = 1; (Father[i] = i) <= n; ++i)
;
for (int i = 1, k, x, y; i <= n; ++i)
for (cin >> k; k; --k) {
cin >> x >> y;
Ans = (Ans + Union(i, x, y)) % 1000000007;
}
cout << Ans << endl;
return 0;
}
| 5 |
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
const int big = 2e9;
int n, a[N], b[N], y[N], x[N], xmn, ymn, xmx, ymx, t1, t2, xx, yy;
map<pair<int, int>, bool> mrk;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
n = 4;
for (long long i = 1; i <= n; i++) cin >> a[i] >> b[i];
for (long long i = 1; i <= n; i++) cin >> x[i] >> y[i];
x[n + 1] = x[1];
y[n + 1] = y[1];
for (long long i = 1; i <= n; i++) {
if (x[i] > x[i + 1])
t1 = -1;
else
t1 = 1;
if (y[i] > y[i + 1])
t2 = -1;
else
t2 = 1;
xx = x[i];
yy = y[i];
for (int j = min(x[i], x[i + 1]); j <= max(x[i], x[i + 1]); j++) {
mrk[make_pair(xx, yy)] = true;
xx += t1;
yy += t2;
}
}
xmn = big;
ymn = big;
xmx = -big;
ymx = -big;
for (long long i = 1; i <= n; i++) {
xmn = min(xmn, a[i]);
ymn = min(ymn, b[i]);
xmx = max(xmx, a[i]);
ymx = max(ymx, b[i]);
}
for (int j = xmn; j <= xmx; j++)
for (int j1 = ymn; j1 <= ymx; j1++)
if (mrk[make_pair(j, j1)]) {
cout << "Yes";
return 0;
}
for (long long i = 1; i <= n; i++) {
swap(a[i], x[i]);
swap(b[i], y[i]);
}
int z;
for (long long i = 1; i <= n; i++) {
z = a[i];
a[i] = a[i] + b[i];
b[i] = z - b[i];
}
for (long long i = 1; i <= n; i++) {
z = x[i];
x[i] = x[i] + y[i];
y[i] = z - y[i];
}
x[n + 1] = x[1];
y[n + 1] = y[1];
for (int i = -100; i <= 100; i++)
for (int j = -100; j <= 100; j++) mrk[make_pair(i, j)] = false;
x[n + 1] = x[1];
y[n + 1] = y[1];
for (long long i = 1; i <= n; i++) {
if (x[i] > x[i + 1])
t1 = -1;
else
t1 = 1;
if (y[i] > y[i + 1])
t2 = -1;
else
t2 = 1;
xx = x[i];
yy = y[i];
for (int j = min(x[i], x[i + 1]); j <= max(x[i], x[i + 1]); j++) {
mrk[make_pair(xx, yy)] = true;
xx += t1;
yy += t2;
}
}
xmn = big;
ymn = big;
xmx = -big;
ymx = -big;
for (long long i = 1; i <= n; i++) {
xmn = min(xmn, a[i]);
ymn = min(ymn, b[i]);
xmx = max(xmx, a[i]);
ymx = max(ymx, b[i]);
}
for (int j = xmn; j <= xmx; j++)
for (int j1 = ymn; j1 <= ymx; j1++)
if (mrk[make_pair(j, j1)]) {
cout << "Yes";
return 0;
}
cout << "No";
return 0;
}
| 3 |
#include <valarray>
#include <cstdio>
using namespace std;
typedef valarray<int>V;
V e,s;
int N,M;
V mul(const V &a, const V &b){
V c(N*N);
int i=0,j;
for(;i<N;i++)for(j=0;j<N;j++)c[i*N+j]=(a[slice(i*N,N,1)]*b[slice(j,N,N)]).sum()%M;
return c;
}
V exp(const V &a, const int n){
if(!n)return e;
if(n==1)return a;
if(n%2==1)return mul(exp(a,n-1),a);
V b=exp(a,n/2);
return mul(b,b);
}
int main(){
int i,a,b,c,t;
for(;scanf("%d%d%d%d%d%d",&N,&M,&a,&b,&c,&t),N;){
s.resize(N);
e=0;
e.resize(N*N);
V x(N*N);
for(i=0;i<N;i++){
scanf("%d",&s[i]);
e[i*N+i]=1;
if(i>0)x[i*N+i-1]=a;
x[i*N+i]=b;
if(i<N-1)x[i*N+i+1]=c;
}
const V r=exp(x,t);
for(i=0;i<N;i++){
printf(i<N-1?"%d ":"%d\n",(r[slice(i*N,N,1)]*s).sum()%M);
}
}
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int preSum[100005], endSum[100005], a[100005], n;
void init() {
int i;
preSum[0] = 0;
for (i = 1; i <= n; i++) {
preSum[i] = preSum[i - 1] + a[i];
}
endSum[n + 1] = 0;
for (i = n; i > 0; i--) {
endSum[i] = endSum[i + 1] + a[i];
}
}
int main() {
int l, r, ql, qr, i, mini, ans;
cin >> n >> l >> r >> ql >> qr;
for (i = 1; i <= n; i++) cin >> a[i];
init();
mini = 1000000000;
for (i = 0; i <= n; i++) {
ans = l * preSum[i] + r * endSum[i + 1];
if (i > n - i) ans += (ql * (2 * i - n - 1));
if (n - i > i) ans += (qr * (n - i - i - 1));
mini = min(ans, mini);
}
cout << mini;
return 0;
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int times;
cin >> times;
while (times--) {
int n;
cin >> n;
vector<int> x(n);
for (int i = 0; i < n; ++i) cin >> x[i];
int ans = 0;
for (int i = 29; i >= 0; --i) {
int one = 0;
for (int j = 0; j < n; ++j) {
if (x[j] & (1 << i)) ++one;
}
if (one % 2 == 0) continue;
if ((n & 1) && one % 4 == 3) {
cout << "LOSE" << endl;
ans = -1;
break;
} else {
cout << "WIN" << endl;
ans = 1;
break;
}
}
if (ans == 0) cout << "DRAW" << endl;
}
return 0;
}
| 2 |
#include<bits/stdc++.h>
const int maxn=200005;
using namespace std;
int a[maxn],b[maxn];
int main()
{
int n,i,x,y;
scanf("%d",&n);
for(i = 1;i<=n;i++)
scanf("%d",&a[i]),b[i]=a[i];
sort(b+1,b+1+n);
x=b[n/2];
y=b[n/2+1];
for(i = 1;i<=n;i++)
{
if(a[i]>x||a[i]==y) printf("%d\n",x);
else if(a[i]<y||a[i]==x) printf("%d\n",y);
}
}
| 0 |
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
char a[100][100];
int main()
{
ll h,w;
cin>>h>>w;
bool sign=false;
for(int i=1;i<=h;i++)
for(int j=1;j<=w;j++)
cin>>a[i][j];
for(int i=1;i<=h;i++){
for(int j=1;j<=w;j++){
if(a[i][j]=='#')
{
if( a[i+1][j]=='.' && a[i][j+1]=='.'&&a[i-1][j]=='.'&&a[i][j-1]=='.'){
sign=true;
}
}
}
}
if(sign)
puts("No");
else
puts("Yes");
return 0;
}
| 0 |
#include <cstdio>
#include <vector>
using namespace std;
int n;
int c[100000];
vector<int> v, w;
int main(){
while(1){
scanf("%d", &n);
if(n == 0) return 0;
v = w;
int t, s;
scanf("%d", &t);
if(t == 0){
v.push_back(1);
}else{
v.push_back(0);
v.push_back(1);
}
for(int i=0; i<n-1; ++i){
scanf("%d", &s);
if(t == s) ++v[v.size()-1];
else{
if(i % 2 == 0){
if(v.size() == 1){
v.push_back(v[0]);
++v[1];
v[0] = 0;
}else{
v[v.size()-2] += v[v.size()-1];
++v[v.size()-2];
v.pop_back();
}
}else{
v.push_back(1);
}
}
t = s;
/* for(int j=0; j<v.size(); ++j){
printf("%d\t", v[j]);
}
printf("\n");
*/ }
int ans = 0;
for(int i=0; i<v.size(); i++){
// printf("%d\t", v[i]);
if(i % 2 == 1) continue;
ans += v[i];
}
// printf("\n");
printf("%d\n", ans);
}
}
| 0 |
#include<cstdio>
#include<cstring>
int main()
{
char s[15];
bool f=0;
scanf("%s",s+1);
if(s[1] != 'A')
puts("WA");
else
{
for(int i=2;i<=strlen(s+1);i++)
{
if(s[i] == 'C' && i != 2 && i != strlen(s+1) && !f)
f=1;
else if(s[i] < 'a' && s[i] != 'A')
{
puts("WA");
return 0;
}
}
if(f)
puts("AC");
else
puts("WA");
}
return 0;
}
| 0 |
#include <cstdio>
#include <cstring>
using namespace std;
int main(){
int i;
char str[1000];
gets(str);
for(i=0;i<1000;i++){
if(str[i]=='a' &&str[i+1]=='p' &&str[i+2]=='p' &&str[i+3]=='l' &&str[i+4]=='e'){
str[i]='p';str[i+1]='e';str[i+2]='a';str[i+3]='c';str[i+4]='h';
continue;
}
if(str[i]=='p' &&str[i+1]=='e' &&str[i+2]=='a' &&str[i+3]=='c' &&str[i+4]=='h'){
str[i]='a';str[i+1]='p';str[i+2]='p';str[i+3]='l';str[i+4]='e';
}
}
puts(str);
return 0;
}
| 0 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
long long n;
cin >> n;
if (n > 2) {
if (n % 2 == 1) {
cout << n / 2 << endl;
} else
cout << n / 2 - 1 << endl;
} else
cout << 0 << endl;
}
}
| 1 |
#include <bits/stdc++.h>
using namespace std;
long long n, k;
int t;
int main() {
scanf("%d", &t);
while (t--) {
scanf("%lld%lld", &n, &k);
if (k % 3) {
if (n % 3)
printf("Alice\n");
else
printf("Bob\n");
continue;
}
int s = n % (k + 1);
if (s == 0)
printf("Bob\n");
else {
if (s % 3 == 0 && (s < k))
printf("Bob\n");
else
printf("Alice\n");
}
}
}
| 4 |
#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
ll N,M,H,W,K,Q,A,B;
string S;
const ll MOD = 998244353;
//const ll MOD = (1e+9) + 7;
const ll INF = 1LL<<60;
typedef pair<ll, ll> P;
int main() {
cin>>N>>M;
mat dp(N + 1, vec(N * 3 + 1, 0));
dp[N][N * 3] = 1;
ll ans = 0;
Rrep(i, N + 1){
Rrep(j, N * 3 + 1){
if(i == 0){
(ans += dp[i][j])%=M;
}else{
if(j >= 1) (dp[min(i, (j - 1) / 2)][j - 1] += dp[i][j])%=M;
if(j >= 2) (dp[i - 1][j - 2] += (dp[i][j] * (j - 1))%M)%=M;
if(j >= 3){
(dp[min(i - 1, (j - 3) / 2)][j - 3] += (dp[i][j] * ((j - 1) * (j - 2) % M)%M))%=M;
}
}
}
}
cout<<ans<<endl;
}
| 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.