AGC009
A - Multiple Array
从后往前递推即可
#include
#define fi first
#define se second
#define pii pair
#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
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
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int N;
int64 A[MAXN],B[MAXN];
void Solve() {
read(N);
for(int i = 1 ; i <= N ; ++i) {read(A[i]);read(B[i]);}
int64 ans = 0,pre = 0;
for(int i = N ; i >= 1 ; --i) {
A[i] += pre;
int64 tmp = (B[i] - A[i] % B[i]) % B[i];
pre += tmp;
ans += tmp;
}
out(ans);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
return 0;
}
B - Tournament
从叶子往根递推,在一个节点合并的时候从小到大合并,每次是当前值和合并的值最大值+1
#include
#define fi first
#define se second
#define pii pair
#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
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
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
struct node {
int to,next;
}E[MAXN * 2];
int N,sumE,head[MAXN],dep[MAXN];
void add(int u,int v) {
E[++sumE].to = v;
E[sumE].next = head[u];
head[u] = sumE;
}
void dfs(int u) {
for(int i = head[u] ; i ; i = E[i].next) {
int v = E[i].to;
dfs(v);
}
vector sec;
for(int i = head[u] ; i ; i = E[i].next) {
int v = E[i].to;
sec.pb(dep[v]);
}
sort(sec.begin(),sec.end());
for(auto t : sec) {
dep[u] = max(dep[u],t) + 1;
}
}
void Solve() {
read(N);
int f;
for(int i = 2 ; i <= N ; ++i) {
read(f);
add(f,i);
}
dfs(1);
out(dep[1]);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
return 0;
}
C - Division into Two
\(dp[i][j]\)表示第一个集合最后一个是\(A[i]\),第二个集合最后一个是\(A[j]\),假如dp到第k个数,这二维必然有一个是k - 1,线段树优化转移即可
#include
#define fi first
#define se second
#define pii pair
#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
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
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;
int64 A,B,S[MAXN];
struct node {
int sum[2],l,r,cov[2];
}tr[MAXN * 4];
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;
}
void update(int &x,int y) {
x = inc(x,y);
}
void update(int u) {
for(int i = 0 ; i < 2 ; ++i) tr[u].sum[i] = inc(tr[u << 1].sum[i],tr[u << 1 | 1].sum[i]);
}
void cover(int id,int u) {
tr[u].cov[id] = 1;
tr[u].sum[id] = 0;
}
void pushdown(int u) {
for(int i = 0 ; i < 2 ; ++i) {
if(tr[u].cov[i]) {
cover(i,u << 1);cover(i,u << 1 | 1);
tr[u].cov[i] = 0;
}
}
}
void build(int u,int l,int r) {
tr[u].l = l;tr[u].r = r;
if(l == r) return;
int mid = (l + r) >> 1;
build(u << 1,l,mid);
build(u << 1 | 1,mid + 1,r);
}
void add(int id,int u,int x,int v) {
if(tr[u].l == tr[u].r) {
update(tr[u].sum[id],v);return;
}
pushdown(u);
int mid = (tr[u].l + tr[u].r) >> 1;
if(x <= mid) add(id,u << 1,x,v);
else if(x > mid) add(id,u << 1 | 1,x,v);
update(u);
}
void lid(int id,int u,int l,int r) {
if(tr[u].l == l && tr[u].r == r) {cover(id,u);return;}
pushdown(u);
int mid = (tr[u].l + tr[u].r) >> 1;
if(r <= mid) lid(id,u << 1,l,r);
else if(l > mid) lid(id,u << 1 | 1,l,r);
else {lid(id,u << 1,l,mid);lid(id,u << 1 | 1,mid + 1,r);}
update(u);
}
int Query(int id,int u,int l,int r) {
if(tr[u].l == l && tr[u].r == r) return tr[u].sum[id];
pushdown(u);
int mid = (tr[u].l + tr[u].r) >> 1;
if(r <= mid) return Query(id,u << 1,l,r);
else if(l > mid) return Query(id,u << 1 | 1,l,r);
else {return inc(Query(id,u << 1,l,mid),Query(id,u << 1 | 1,mid + 1,r));}
}
void Solve() {
read(N);read(A);read(B);
for(int i = 1 ; i <= N ; ++i) read(S[i]);
build(1,0,N);
add(0,1,0,1);add(1,1,0,1);
for(int i = 2 ; i <= N ; ++i) {
int t = upper_bound(S + 1,S + N + 1,S[i] - A) - S - 1;
int va = Query(1,1,0,t);
t = upper_bound(S + 1,S + N + 1,S[i] - B) - S - 1;
int vb = Query(0,1,0,t);
add(0,1,i - 1,va);
add(1,1,i - 1,vb);
if(S[i] - S[i - 1] < A) lid(0,1,0,i - 2);
if(S[i] - S[i - 1] < B) lid(1,1,0,i - 2);
}
out(inc(tr[1].sum[0],tr[1].sum[1]));enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
return 0;
}
D - Uninity
大意:一个点的数值是1,每次可以选择一个中心点和周围最大价值为k的树合成一个k + 1的树,给定一棵树,问最小值是多少
其实就是转化成一个标号,如果这个点在k的树中作为中心点被选了,那么这个点标号为k
然后要求两个相同的数之间必须有一个比他们都大的数
答案个数不超过log N,所以把每个子树里不能选的数压成一个二进制数,父亲不能选的是所有子树不能选的并集,而且子树中若有两个数都不能选,则选择的数必须大于这个数
然后该点若选了t,比t小的值都可以标记为可选
#include
#define fi first
#define se second
#define pii pair
#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
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
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
struct node {
int to,next;
}E[MAXN * 2];
int N,head[MAXN],sumE;
int mask[MAXN],ans;
void add(int u,int v) {
E[++sumE].to = v;
E[sumE].next = head[u];
head[u] = sumE;
}
void dfs(int u,int fa) {
for(int i = head[u] ; i ; i = E[i].next) {
int v = E[i].to;
if(v != fa) {
dfs(v,u);
}
}
mask[u] = 0;
vector vec;
for(int i = head[u] ; i ; i = E[i].next) {
int v = E[i].to;
if(v != fa) {
mask[u] |= mask[v];
vec.pb(mask[v]);
}
}
int t = 0;
for(int i = 0 ; i <= 20 ; ++i) {
if(mask[u] >> i & 1) {
if(t == i) ++t;
int cnt = 0;
for(auto k : vec) {
if(k >> i & 1) ++cnt;
}
if(cnt > 1) t = max(t,i + 1);
}
}
mask[u] |= (1 << t);
ans = max(ans,t);
for(int i = 0 ; i < t ; ++i) {
if(mask[u] >> i & 1) mask[u] ^= (1 << i);
}
return ;
}
void Solve() {
read(N);
int a,b;
for(int i = 1 ; i < N ; ++i) {
read(a);read(b);add(a,b);add(b,a);
}
dfs(1,0);
out(ans);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
return 0;
}
E - Eternal Average
大意:有N个0和M个1,每次选择K个数然后把它们的平均数写在上面,保证最后只剩下一个数,问这一个数有几种情况
就是这个数是一个K进制数,然后转成十进制,设操作次数是c,分母就都是\(K^{c}\)
然后我们只关心这个K进制数,从高位到低位枚举每一位的值,然后看看这一行需要几个1,几个0,是否在这里封死(就是每一层我只填K - 1个,封死的那一个需要填K个)为了不重不漏,我要求封死的时候这个地方填了数,每次封死的时候可以统计,需要剩下的1和剩下的0都是K - 1的倍数
我程序里好像N当1,M当0了,不过小问题,因为反过来问题是等价的
#include
#define fi first
#define se second
#define pii pair
#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
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
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,M,K;
int dp[4005][2005];
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;
}
void update(int &x,int y) {
x = inc(x,y);
}
void Solve() {
read(N);read(M);read(K);
if(!M || !N) {puts("1");return;}
int t = (N + M - 1) / (K - 1) - 1;
dp[t + 1][0] = 1;
int ans = 0;
for(int i = t ; i >= 0 ; --i) {
for(int k = 0 ; k <= M ; ++k) {
for(int j = 0 ; j < K - 1 ; ++j) {
if(j > k) break;
update(dp[i][k],dp[i + 1][k - j]);
}
if(k != M) {
int o = (t - i + 1) * (K - 1) - k;
if((M - k - 1) % (K - 1) == 0 && N >= o && (N - o) % (K - 1) == 0) {
update(ans,dp[i][k]);
}
}
if(k >= (K - 1)) update(dp[i][k],dp[i + 1][k - (K - 1)]);
}
}
out(ans);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
return 0;
}