【AtCoder】ARC077
C - pushpush
如果是按下标说的话
如果是偶数个
那么是
\(N,N - 2,N - 4...1,3,5...N - 1\)
如果是奇数个
\(N,N - 2,N - 4...2,4,6...N - 1\)
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 200005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int N;
int a[MAXN];
void Solve() {
read(N);
for(int i = 1 ; i <= N ; ++i) read(a[i]);
if(N & 1) {
for(int i = N ; i >= 1 ; i -= 2) {out(a[i]);space;}
for(int i = i = 2 ; i <= N ; i += 2) {out(a[i]);space;}
enter;
}
else {
for(int i = N ; i >= 1 ; i -= 2) {out(a[i]);space;}
for(int i = 1 ; i <= N ; i += 2) {out(a[i]);space;}
enter;
}
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}
D - 11
就相当于只有两个位置相同,那么即为
\(...1 .... 1....\)
算所有位置不同的子序列
如果这个序列只包括其中一个1,剩下的数不在两个1之间,那么这个序列就被重复计算了,很简单的计数
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
const int MOD = 1000000007;
int N,a[MAXN],fac[MAXN],invfac[MAXN];
int pos[MAXN],p;
int inc(int a,int b) {
return a + b >= MOD ? a + b - MOD : a + b;
}
int mul(int a,int b) {
return 1LL * a * b % MOD;
}
int fpow(int x,int c) {
int t = x,res = 1;
while(c) {
if(c & 1) res = mul(res,t);
t = mul(t,t);
c >>= 1;
}
return res;
}
int C(int n,int m) {
if(n < m) return 0;
return mul(fac[n],mul(invfac[m],invfac[n - m]));
}
void Solve() {
read(N);
for(int i = 1 ; i <= N + 1; ++i) read(a[i]);
for(int i = 1 ; i <= N + 1; ++i) {
if(pos[a[i]]) p = i;
else pos[a[i]] = i;
}
fac[0] = 1;
for(int i = 1 ; i <= N + 1 ; ++i) {
fac[i] = mul(fac[i - 1],i);
}
invfac[N + 1] = fpow(fac[N + 1],MOD - 2);
for(int i = N ; i >= 0 ; --i) {
invfac[i] = mul(invfac[i + 1],i + 1);
}
for(int i = 1 ; i <= N + 1 ; ++i) {
int res = C(N + 1,i);
res = inc(res,MOD - C(pos[a[p]] - 1 + N + 1 - p,i - 1));
out(res);enter;
}
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}
E - guruguru
显然如果不按第二个按钮,只用第一种情况增加,增加的方式不能改变,把每次增加的段画在数轴上,会发现每次增加对于一段区间相当于如果选了x当喜爱按钮,那么费用会减少y,这个y在数轴上是个等差数列,用线段树维护一下就好
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 100005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int N,M;
int a[MAXN];
int64 ans,res;
struct node {
int L,R;
int64 a,d;
}tr[MAXN * 4];
void build(int u,int L,int R) {
tr[u].L = L;tr[u].R = R;
tr[u].a = tr[u].d = 0;
if(L == R) return;
int mid = (L + R) >> 1;
build(u << 1,L,mid);
build(u << 1 | 1,mid + 1,R);
}
void addlz(int u,int64 a,int64 d) {
tr[u].a += a;tr[u].d += d;
}
void pushdown(int u) {
int l = tr[u].L,m = (tr[u].L + tr[u].R) >> 1,r = tr[u].R;
addlz(u << 1,tr[u].a,tr[u].d);
addlz(u << 1 | 1,tr[u].a + (m + 1 - l) * tr[u].d,tr[u].d);
tr[u].a = tr[u].d = 0;
}
void Add(int u,int ql,int qr,int64 a,int64 d) {
if(ql > qr) return;
if(ql == tr[u].L && qr == tr[u].R) {addlz(u,a,d);return;}
int mid = (tr[u].L + tr[u].R) >> 1;
pushdown(u);
if(qr <= mid) Add(u << 1,ql,qr,a,d);
else if(ql > mid) Add(u << 1 | 1,ql,qr,a,d);
else {Add(u << 1,ql,mid,a,d),Add(u << 1 | 1,mid + 1,qr,a + (mid + 1 - ql) * d,d);}
}
void Getans(int u) {
if(tr[u].L == tr[u].R) {res = max(tr[u].a,res);return;}
pushdown(u);
Getans(u << 1);Getans(u << 1 | 1);
}
void Solve() {
read(N);read(M);
build(1,1,M);
for(int i = 1 ; i <= N ; ++i) {
read(a[i]);
}
for(int i = 2 ; i <= N ; ++i) {
if(a[i] > a[i - 1]) {
ans += a[i] - a[i - 1];
Add(1,a[i - 1] + 1,a[i],0,1);
}
else {
ans += M - a[i - 1] + a[i];
Add(1,a[i - 1] + 1,M,0,1);
Add(1,1,a[i],M - a[i - 1],1);
}
}
Getans(1);
out(ans - res);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}
F - SS
先把原始的字符串变成两个相同的字符串的形式
设为\(SS\),如果\(S\)最后一位的next是\(k\),那么显然要在后边加\(S\)的后N-k和\(S\)的前N-k位
设\(S\)的后\(N - k\)位为\(T\),且\(k\)最大,那么
\(f(SS) = STST\)
设\(g(S) = ST\)
显然\(f(SS) = g(S) + g(S)\)
我们实际上只要求一个足够长的\(g(S)\)就行
\(g(S) = ST\)
当\(|T|\)是\(|S|\)的约数,可以得到\(g(ST) = STT\)
如果\(|T|\)不是\(|S|\)的约数,那么\(g(ST) = STS\)
然后第一种情况\(g^{\infty}(S) = STTTTTTT....\)
第二种情况\(g^{i + 2}(S) = g^{i + 1}(S) + g^{i}(S)\)
然后可以直接模拟,从第二种情况开始递推,如果出现第一种情况就可以记录退出,第二种情况增长速度是指数级的,递推100个左右就行
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define MAXN 200005
#define eps 1e-10
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 + c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
struct node {
int64 c[26],len;
node() {memset(c,0,sizeof(c));len = 0;}
friend node operator + (const node &a,const node &b) {
node r;
r.len = a.len + b.len;
for(int i = 0 ; i < 26 ; ++i) r.c[i] = a.c[i] + b.c[i];
return r;
}
friend node operator - (const node &a,const node &b) {
node r;
r.len = a.len - b.len;
for(int i = 0 ; i < 26 ; ++i) r.c[i] = a.c[i] - b.c[i];
return r;
}
friend node operator * (const node &a,const int64 &d) {
node r = a;
for(int i = 0 ; i < 26 ; ++i) {
r.c[i] = r.c[i] * d;
}
return r;
}
}g[MAXN];
char s[MAXN * 2],t[MAXN * 2];
int nxt[MAXN],N;
int st,all;
node Calc(int64 R) {
if(R > g[all].len && st) {
node res;
res = g[st];
int64 t = (R - g[st].len) / g[st - 1].len;
res = res + g[st - 1] * t;
return res + Calc(R - g[st].len - g[st - 1].len * t);
}
else if(R <= g[1].len){
node res;res.len = R;
for(int i = 1 ; i <= R ; ++i) {
res.c[s[i] - 'a']++;
}
return res;
}
else {
for(int i = all ; i >= 0 ; --i) {
if(g[i].len <= R) {
return g[i] + Calc(R - g[i].len);
}
}
}
}
void Solve() {
scanf("%s",s + 1);
int64 r,l;
read(l);read(r);
N = strlen(s + 1);
for(int i = 2 ; i <= N ; ++i) {
int p = nxt[i - 1];
while(p && s[i] != s[p + 1]) p = nxt[p];
if(s[i] == s[p + 1]) nxt[i] = p + 1;
else nxt[i] = 0;
}
int p = nxt[N];
while(p > (N - 1) / 2) p = nxt[p];
int L = N;
for(int i = p + 1 ; i <= N - p ; ++i) {
s[++L] = s[i];
}
for(int i = 1 ; i <= L / 2; ++i) {
g[1].c[s[i] - 'a']++;
}
g[1].len = L / 2;
p = nxt[L / 2];
g[0].len = L / 2 - p;
for(int i = 1 ; i <= L / 2 - p ; ++i) {
t[i] = s[i];
g[0].c[t[i] - 'a']++;
}
for(int i = 2 ; i <= 100 ; ++i) {
if(g[i - 1].len % (g[i - 1].len - g[i - 2].len) == 0) {all = i - 1;st = i - 1;break;}
if(g[i - 1].len + g[i - 2].len <= 1e18) g[i] = g[i - 1] + g[i - 2];
else {all = i - 1;break;}
}
if(!all) all = 100;
node ans = Calc(r) - Calc(l - 1);
for(int i = 0 ; i < 26 ; ++i) {
out(ans.c[i]);space;
}
enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
}