2023.01.06 模拟赛小结
2023.01.06 模拟赛小结
更好的阅读体验戳此进入
今天的题是 sssmzy 组的,他的题居然有一道签到,真的,我哭死。
这次模拟赛没有太寄。
赛时思路
T1
要求你构造一个序列,满足对应条件。具体条件略。
比较简单,构造方案很多且均不难想到,总之简单想一下就知道了,妥妥的签到题。
Code
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define PI M_PI
#define E M_E
#define npt nullptr
#define SON i->to
#define OPNEW void* operator new(size_t)
#define ROPNEW(arr) void* Edge::operator new(size_t){static Edge* P = arr; return P++;}
using namespace std;
mt19937 rnd(random_device{}());
int rndd(int l, int r){return rnd() % (r - l + 1) + l;}
bool rnddd(int x){return rndd(1, 100) <= x;}
typedef unsigned int uint;
typedef unsigned long long unll;
typedef long long ll;
typedef long double ld;
template < typename T = int >
inline T read(void);
int N, K;
basic_string < int > ans;
deque < int > values;
int main(){
freopen("structure.in", "r", stdin);
freopen("structure.out", "w", stdout);
N = read(), K = read();
if(!K){
for(int i = 1; i <= N * 2; ++i)printf("%d%c", i, i == N * 2 ? '\n' : ' ');
exit(0);
}
ans += {K + 1, 1};
for(int i = 1; i <= N * 2; ++i)if(i != K + 1 && i != 1)values.emplace_back(i);
while(!values.empty())ans += {values.front(), values.back()}, values.pop_front(), values.pop_back();
for(auto it = ans.begin(); it != ans.end(); ++it)printf("%d%c", *it, it == prev(ans.end()) ? '\n' : ' ');
fprintf(stderr, "Time: %.6lf\n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}
template < typename T >
inline T read(void){
T ret(0);
int flag(1);
char c = getchar();
while(c != '-' && !isdigit(c))c = getchar();
if(c == '-')flag = -1, c = getchar();
while(isdigit(c)){
ret *= 10;
ret += int(c - '0');
c = getchar();
}
ret *= flag;
return ret;
}
checker
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define PI M_PI
#define E M_E
#define npt nullptr
#define SON i->to
#define OPNEW void* operator new(size_t)
#define ROPNEW(arr) void* Edge::operator new(size_t){static Edge* P = arr; return P++;}
using namespace std;
mt19937 rnd(random_device{}());
int rndd(int l, int r){return rnd() % (r - l + 1) + l;}
bool rnddd(int x){return rndd(1, 100) <= x;}
typedef unsigned int uint;
typedef unsigned long long unll;
typedef long long ll;
typedef long double ld;
template < typename T = int >
inline T read(void);
int a[111000];
int main(){
while(true){
FILE* input = fopen("in.txt", "w");
int N = rndd(1, 50000), K = rndd(0, N >> 1);
fprintf(input, "%d %d\n", N, K);
fcloseall();
system("./std < in.txt > my.out");
FILE* output = fopen("my.out", "r");
for(int i = 1; i <= N * 2; ++i)fscanf(output, "%d", &a[i]);
fcloseall();
ll sum1(0), sum2(0);
for(int i = 1; i <= N * 2; i += 2)sum1 += abs(a[i + 1] - a[i]), sum2 += a[i + 1] - a[i];
sum2 = abs(sum2);
if(sum1 - sum2 == K << 1)printf("Accept!\n");
else printf("Wrong!\n"), exit(0);
}
fprintf(stderr, "Time: %.6lf\n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}
template < typename T >
inline T read(void){
T ret(0);
int flag(1);
char c = getchar();
while(c != '-' && !isdigit(c))c = getchar();
if(c == '-')flag = -1, c = getchar();
while(isdigit(c)){
ret *= 10;
ret += int(c - '0');
c = getchar();
}
ret *= flag;
return ret;
}
T2
给定 $ m, k $,求满足 $ 111 \cdots 111 \equiv k \pmod{m} $(共 $ n $ 个 $ 1 $)的最小 $ n $。
读完题之后想到的思路是把所有 $ 1 $ 拆分为 $ 10^{n - 1} + 10^{n - 2} + \cdots + 10^0 $,于是问题就转换为了对每个数取模之后加起来再取模,然后通过欧拉定理将幂次对 $ \varphi(m) = m - 1 $ 取模即可,这样的话就是 $ [0, m - 2] $ 循环,循环长度为 $ m - 1 $,然后记录一下 $ 10^i \bmod{m}(i \in [0, m - 2]) $ 的值即可,这样时空复杂度都是 $ O(n) $ 的,唯一有一个问题就是不知道会循环多少次,虽然结论似乎是 $ n \le m $ 的,不过总之赛时我用了一个类似卡时的思路去限定了一下。无论如何最后 $ m \le 5 \times 10^7 $ 的部分分过了,最终 $ 60\texttt{pts} $。
Code
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define PI M_PI
#define E M_E
#define npt nullptr
#define SON i->to
#define OPNEW void* operator new(size_t)
#define ROPNEW(arr) void* Edge::operator new(size_t){static Edge* P = arr; return P++;}
using namespace std;
mt19937 rnd(random_device{}());
int rndd(int l, int r){return rnd() % (r - l + 1) + l;}
bool rnddd(int x){return rndd(1, 100) <= x;}
typedef unsigned int uint;
typedef unsigned long long unll;
typedef long long ll;
typedef long double ld;
template < typename T = int >
inline T read(void);
ll K, MOD;
ll pow10[51000000];
ll sum(1);
ll ans(LONG_LONG_MAX);
const ll mxCnt = (ll)(1e8 + 114514);
int main(){
freopen("date_struct.in", "r", stdin);
freopen("date_struct.out", "w", stdout);
K = read < ll >(), MOD = read < ll >();
ll lim = mxCnt / (MOD - 1);
pow10[0] = 1;
for(int i = 1; i <= MOD - 2; ++i)pow10[i] = pow10[i - 1] * 10 % MOD, (sum += pow10[i]) %= MOD;
ll cur(0);
for(int i = 0; i <= MOD - 2; ++i){
(cur += pow10[i]) %= MOD;
if(cur == K)printf("%d\n", i + 1), exit(0);
}cur = 0;
for(int i = 0; i <= MOD - 2; ++i){
(cur += pow10[i]) %= MOD;
for(int j = 1; j <= lim; ++j)
if((cur + sum * j % MOD) % MOD == K)ans = min(ans, (ll)i + 1 + (MOD - 1) * j);
}
printf("%lld\n", ans);
fprintf(stderr, "Time: %.6lf\n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}
template < typename T >
inline T read(void){
T ret(0);
int flag(1);
char c = getchar();
while(c != '-' && !isdigit(c))c = getchar();
if(c == '-')flag = -1, c = getchar();
while(isdigit(c)){
ret *= 10;
ret += int(c - '0');
c = getchar();
}
ret *= flag;
return ret;
}
T3
有一个长为 \(n\) 的数列,初始时为 \(a_{1..n}\)。
给你 \(q\) 个操作,第 \(i\) 个操作将 \([l_i,r_i]\) 内的数全部加一,有 \(p_i\) 的概率被执行。保证区间不会交错。
求操作完成后数列的最大值的期望。
一眼没什么思路,糊了个暴力 $ 20\texttt{pts} $ 跑路。
Code
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define PI M_PI
#define E M_E
#define npt nullptr
#define SON i->to
#define OPNEW void* operator new(size_t)
#define ROPNEW(arr) void* Edge::operator new(size_t){static Edge* P = arr; return P++;}
using namespace std;
mt19937 rnd(random_device{}());
int rndd(int l, int r){return rnd() % (r - l + 1) + l;}
bool rnddd(int x){return rndd(1, 100) <= x;}
typedef unsigned int uint;
typedef unsigned long long unll;
typedef long long ll;
typedef long double ld;
template < typename T = int >
inline T read(void);
int N, Q;
int a[110000];
int l[5100], r[5100];
ld p[5100];
ld expect(0.0);
class SegTree{
private:
int tr[110000 << 2];
int lz[110000 << 2];
#define LS (p << 1)
#define RS (LS | 1)
#define MID ((gl + gr) >> 1)
public:
void Pushup(int p){
tr[p] = max(tr[LS], tr[RS]);
}
void Pushdown(int p){
if(!lz[p])return;
lz[LS] += lz[p], lz[RS] += lz[p];
tr[LS] += lz[p], tr[RS] += lz[p];
lz[p] = 0;
}
void Build(int p = 1, int gl = 1, int gr = N){
if(gl == gr)return tr[p] = a[gl = gr], void();
Build(LS, gl, MID), Build(RS, MID + 1, gr);
Pushup(p);
}
void Modify(int l, int r, int v = 1, int p = 1, int gl = 1, int gr = N){
if(l <= gl && gr <= r)return tr[p] += v, lz[p] += v, void();
Pushdown(p);
if(l <= MID)Modify(l, r, v, LS, gl, MID);
if(MID + 1 <= r)Modify(l, r, v, RS, MID + 1, gr);
Pushup(p);
}
int Query(void){
return tr[1];
}
}st;
void dfs(int dep = 1, ld cur = 1.0){
if(dep > Q){
expect += cur * (ld)st.Query();
return;
}
st.Modify(l[dep], r[dep]);
dfs(dep + 1, cur * p[dep]);
st.Modify(l[dep], r[dep], -1);
dfs(dep + 1, cur * (1.0 - p[dep]));
}
int main(){
freopen("expectation.in", "r", stdin);
freopen("expectation.out", "w", stdout);
N = read(), Q = read();
for(int i = 1; i <= N; ++i)a[i] = read();
st.Build();
for(int i = 1; i <= Q; ++i)l[i] = read(), r[i] = read(), scanf("%Lf", &p[i]);
dfs();
printf("%.9Lf\n", expect);
fprintf(stderr, "Time: %.6lf\n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}
template < typename T >
inline T read(void){
T ret(0);
int flag(1);
char c = getchar();
while(c != '-' && !isdigit(c))c = getchar();
if(c == '-')flag = -1, c = getchar();
while(isdigit(c)){
ret *= 10;
ret += int(c - '0');
c = getchar();
}
ret *= flag;
return ret;
}
/*
5 2
1 7 2 4 3
1 3 0.500
2 2 0.500
5 2
281 280 279 278 282
1 4 1.000
1 4 0.000
3 5
1 2 3
1 3 0.500
2 2 0.250
1 2 0.800
1 1 0.120
2 2 0.900
*/
T4
Alice和Bob在一棵\(n\)个节点的树上玩游戏。每个节点最初都是黑色或白色。
他们轮流执行以下操作:
从当前树中选择一个白色节点\(v\),将路径\((1,v)\)上的所有白色节点都变为黑色.最后操作的玩家获胜.爱丽丝先手。当他们都使用最佳策略时,求是否能够必胜,并求出第一步的方案.
树上博弈论,较为阴间,赛时感觉暴力过于麻烦就没写,实际上这题的暴力精细实现的话也没有太麻烦。
正解
T2
尝试进行转化,对 $ \texttt{LHS} \leftarrow \texttt{LHS} \times 9 + 1 $ 即可转换为 $ 10 $ 的幂,即:
然后套一下 BSGS 模板即可。
依然可以不使用光速幂,最终复杂度 $ O(\sqrt{m} \log m) $。
注意过程中部分需要使用 __int128_t
。
Code
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define PI M_PI
#define E M_E
#define npt nullptr
#define SON i->to
#define OPNEW void* operator new(size_t)
#define ROPNEW(arr) void* Edge::operator new(size_t){static Edge* P = arr; return P++;}
using namespace std;
mt19937 rnd(random_device{}());
int rndd(int l, int r){return rnd() % (r - l + 1) + l;}
bool rnddd(int x){return rndd(1, 100) <= x;}
typedef unsigned int uint;
typedef unsigned long long unll;
typedef long long ll;
typedef long double ld;
template < typename T = int >
inline T read(void);
ll K, M;
unordered_map < ll, ll > mp;
ll qpow(ll a, ll b){
ll ret(1), mul(a);
while(b){
if(b & 1)ret = (__int128_t)ret * mul % M;
b >>= 1;
mul = (__int128_t)mul * mul % M;
}return ret;
}
int main(){
K = read < ll >(), M = read < ll >();
ll lim = (ll)ceil(sqrt(M));
for(int i = 1; i <= lim; ++i)mp.insert({(__int128_t)(9 * K + 1) % M * qpow(10, i) % M, i});
for(int i = 1; i <= lim; ++i){
ll val = qpow(10, (ll)i * lim);
if(mp.find(val) != mp.end())printf("%lld\n", (ll)i * lim - mp[val]), exit(0);
}
fprintf(stderr, "Time: %.6lf\n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}
template < typename T >
inline T read(void){
T ret(0);
int flag(1);
char c = getchar();
while(c != '-' && !isdigit(c))c = getchar();
if(c == '-')flag = -1, c = getchar();
while(isdigit(c)){
ret *= 10;
ret += int(c - '0');
c = getchar();
}
ret *= flag;
return ret;
}
T3
有一个很高妙的转化,即因为区间无交错,所以考虑将所有操作区间抽象成森林的结构,可以加一个操作 $ [1, n] $ 但概率为 $ 0 $,这样可以保证结构为树而非森林。
转化之后就是经典的 树形DP 了,首先一个较为显然的状态就是 $ dp(p, i) $ 表示 $ p $ 子树中最大值为 $ i $ 的概率,发现难以转移,于是考虑设 $ dp(p, i) $ 表示 $ p $ 子树中最大值 $ \le i $ 的概率,这样易于转移但第二维状态过多。不难发现最大值一定不小于序列本身的最大值,并且不大于最大值加上 $ q $,所以考虑修改状态为 $ dp(p, i) $ 表示 $ p $ 子树内最大值 $ \le max_p + i $ 的概率,于是转移显然,对于节点 $ p $ 加上其最大值减去子节点最大值,然后考虑 $ p $ 的操作是否执行即可。
对于没有子节点的点自然为 $ dp(p, i) = P_p + (1 - P_p) = 1 $。
最终答案即为枚举所有 $ i $,由于建树时需要排序,排序后根节点应为 $ 1 $ 那么答案即为 $ \sum_{i = 0}^{q}(dp(1, i) - dp(1, i - 1)) \times (max_{1} + i) $。
同时需要注意对于 $ dp(p, 0) $,其应为 $ (1 - P_p) \times \prod_{u \in son_p}dp(u, i + max_p - max_u) $。
Code
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define PI M_PI
#define E M_E
#define npt nullptr
#define SON i->to
#define OPNEW void* operator new(size_t)
#define ROPNEW(arr) void* Edge::operator new(size_t){static Edge* P = arr; return P++;}
using namespace std;
mt19937 rnd(random_device{}());
int rndd(int l, int r){return rnd() % (r - l + 1) + l;}
bool rnddd(int x){return rndd(1, 100) <= x;}
typedef unsigned int uint;
typedef unsigned long long unll;
typedef long long ll;
typedef long double ld;
template < typename T = int >
inline T read(void);
struct Edge{
Edge* nxt;
int to;
OPNEW;
}ed[11000];
ROPNEW(ed);
Edge* head[5100];
int N, Q;
int A[110000];
struct Mdf{
int L, R;
double P;
int mx;
}mdf[5100];
double dp[5100][5100];
double ans(0.0);
class SegTree{
private:
int tr[110000 << 2];
#define LS (p << 1)
#define RS (LS | 1)
#define MID ((gl + gr) >> 1)
public:
void Pushup(int p){
tr[p] = max(tr[LS], tr[RS]);
}
void Build(int p = 1, int gl = 1, int gr = N){
if(gl == gr)return tr[p] = A[gl = gr], void();
Build(LS, gl, MID), Build(RS, MID + 1, gr);
Pushup(p);
}
int Query(int l, int r, int p = 1, int gl = 1, int gr = N){
if(l <= gl && gr <= r)return tr[p];
if(gr < l || r < gl)return -1;
return max(Query(l, r, LS, gl, MID), Query(l, r, RS, MID + 1, gr));
}
}st;
void TreeDP(int p = 1, int fa = 0){
for(auto i = head[p]; i; i = i->nxt)if(SON != fa)TreeDP(SON, p);
for(int j = 0; j <= Q; ++j){
double D1 = mdf[p].P, D2 = (1.0 - mdf[p].P);
for(auto i = head[p]; i; i = i->nxt){
if(SON == fa)continue;
if(j + mdf[p].mx - mdf[SON].mx - 1 >= 0)D1 *= dp[SON][min(Q, j + mdf[p].mx - mdf[SON].mx - 1)];
if(j + mdf[p].mx - mdf[SON].mx >= 0)D2 *= dp[SON][min(Q, j + mdf[p].mx - mdf[SON].mx)];
}dp[p][j] = j ? D1 + D2 : D2;
}
}
int main(){
N = read(), Q = read();
for(int i = 1; i <= N; ++i)A[i] = read();
st.Build();
for(int i = 1; i <= Q; ++i)mdf[i].L = read(), mdf[i].R = read(), scanf("%lf", &mdf[i].P), mdf[i].mx = st.Query(mdf[i].L, mdf[i].R);
mdf[++Q] = {1, N, 0.0, st.Query(1, N)};
sort(mdf + 1, mdf + Q + 1, [](const Mdf &a, const Mdf &b)->bool{return a.L == b.L ? a.R > b.R : a.L < b.L;});
for(int i = 1; i <= Q; ++i)
for(int j = i - 1; j; --j)
if(mdf[j].L <= mdf[i].L && mdf[i].R <= mdf[j].R){
head[i] = new Edge{head[i], j}, head[j] = new Edge{head[j], i};
break;
}
TreeDP();
for(int i = 0; i <= Q; ++i)ans += (dp[1][i] - (i ? dp[1][i - 1] : 0.0)) * (double)(mdf[1].mx + i);
printf("%.6lf\n", ans);
fprintf(stderr, "Time: %.6lf\n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}
template < typename T >
inline T read(void){
T ret(0);
int flag(1);
char c = getchar();
while(c != '-' && !isdigit(c))c = getchar();
if(c == '-')flag = -1, c = getchar();
while(isdigit(c)){
ret *= 10;
ret += int(c - '0');
c = getchar();
}
ret *= flag;
return ret;
}
T4
Code
UPD
update-2022__ 初稿