#include <bits/stdc++.h>
#define int long long
using namespace std;
int T;
int ans;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> T;
while (T--){
int x;
cin >> x;
ans = 0;
int idx = 0;
while (x){
int d = x % 10;
x /= 10;
ans += (d * (1LL << idx));
idx++;
}
cout<<ans<<endl;
}
return 0;
}
:::
复杂度
---
### T2 充电
:::info[题意]
一条线上有 $n$ 辆电动车和 $m$ 个充电桩,位置严格递增。每辆车去最近的充电桩,距离相同去靠左的。求每个充电桩接待的车数。
:::
#### 思路
所有实体已经按位置排好序,这是最大的便利。一辆车归哪个桩,只由它左右相邻的两个桩决定。
考虑相邻两个充电桩 $C_i$(位置 $l$)和 $C_{i+1}$(位置 $r$),它们中间夹着若干辆电动车。对中间某辆车位置 $x$:
- 去 $C_i$ 的距离是 $x-l$;
- 去 $C_{i+1}$ 的距离是 $r-x$。
去左边当且仅当 $x-l\le r-x$,即 $x\le (l+r)/2$。题目说距离相等去左边,所以取 $\le$。
实现时,找满足 $x\le (l+r)/2$ 的最右位置。由于坐标是整数,直接算 $\lfloor (l+r)/2\rfloor$,用 `upper_bound` 找第一个大于它的位置,这个位置之前的(不含)都归 $C_i$,之后的归 $C_{i+1}$。
两端的桩特殊处理:
- 第一个桩左边的所有实体全是车,全归它;
- 最后一个桩右边的所有实体全是车,全归它。
实现上按顺序扫一遍,遇到桩就处理它与上一个桩之间的车。用 `last` 记录上一个桩的位置就可以了。
>可以先自己尝试一下写代码啦
:::success[代码]
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n, m;
long long x[N];
char t[N];
int ans[N];
signed main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int T;
cin >> T;
bool first = true;
while (T--){
cin >> n >> m;
for (int i = 1; i <= n + m; i++){
cin >> x[i] >> t[i];
}
for (int i = 1; i <= m; i++) ans[i] = 0;
int idx = 0;
int last;
for (int i = 1; i <= n + m; i++){
if (t[i] == 'C'){
idx++;
if (idx == 1){
ans[idx] += i - 1;
last = i;
}
if (idx == m){
ans[idx] += n + m - i;
}
if (idx != 1){
long long l = x[last], r = x[i];
int pos = upper_bound(x + 1, x + 1 + i, (l + r) / 2) - x;
ans[idx - 1] += pos - last - 1;
ans[idx] += i - pos;
last = i;
}
}
}
if (!first) cout << '\n';
first = false;
for (int i = 1; i <= idx; i++){
cout<< i << "->" << ans[i] << '\n';
}
}
return 0;
}
```
:::
#### 复杂度
每组 $O((n+m)\log(n+m))$,所有测试数据规模之和不超过 $2\times10^5$,轻松通过。
---
### T3 穿越森林
:::info[题意]
从 $(0,0)$ 走到 $(2n,0)$,每步 $(+1,+1)$ 或 $(+1,-1)$,过程中纵坐标不能小于 $0$。求方案数对 $10^9+7$ 取模。
:::
#### 思路
这是经典的卡特兰数模型。
总步数 $2n$ 步,其中向上 $n$ 步、向下 $n$ 步(否则到不了 $(2n,0)$)。把向上记作 $+1$、向下记作 $-1$,则任意时刻 $y$ 等于前缀和。$y\ge 0$ 等价于任意前缀和 $\ge 0$。
"从 $(0,0)$ 到 $(2n,0)$、每步 $\pm 1$、前缀和始终非负"的路径数正是第 $n$ 个卡特兰数:
$$C_n=\frac{1}{n+1}\binom{2n}{n}=\frac{(2n)!}{n!(n+1)!}$$
模数 $10^9+7$ 是质数,预处理阶乘,用费马小定理求逆元即可。
验证样例:$n=3$,$C_3=5$;$n=16$,$C_{16}=35357670$。
>可以先自己尝试一下写代码啦
:::success[代码]
```cpp
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MOD = 1e9 + 7, N = 2e6 + 10;
int Catalan[N];
int n;
int qpow(int a, int b){
int res = 1;
while (b){
if (b & 1){
res = res * a % MOD;
}
a = a * a % MOD;
b >>= 1;
}
return res;
}
int inv(int x){
return qpow(x, MOD - 2);
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
int m = 2 * n;
Catalan[0] = 1;
for (int i = 1; i <= m + 1; i++){
Catalan[i] = Catalan[i - 1] * i % MOD;
}
int ans = Catalan[2 * n];
ans = ans * inv(Catalan[n]) % MOD;
ans = ans * inv(Catalan[n + 1]) % MOD;
cout << ans;
return 0;
}
```
:::
#### 复杂度
$O(n)$ 预处理阶乘,两次快速幂求逆元 $O(\log MOD)$,总计 $O(n)$。$n\le 10^6$,数组开到 $2\times10^6+10$。
---
### T4 星核碎片
:::info[题意]
给 $n$ 个二元组 $(y_i,x_i)$,选最长子序列,使相邻两项满足 $x$ 不同且 $\gcd(y_i,y_j)>1$。
:::
#### 思路
$\gcd(y_i,y_j)>1$ 等价于两个数有公共质因子。$y_i\le 10^5$,一个数的不同质因子最多 6 个,这是突破口。
设 $dp[i]$ 表示以第 $i$ 个碎片结尾的最长航线长度。转移时,枚举 $y_i$ 的每个质因子 $p$,在所有以 $p$ 为因子的前缀碎片中找一个 $x$ 与 $x_i$ 不同的、$dp$ 最大的位置来转移。
但直接对每个质因子扫一遍太慢。观察:对于固定质因子 $p$,我们只需要保留"最优的两个 $x$ 不同的位置"即可,因为:
- 若当前 $x_i$ 与最优位置的 $x$ 不同,直接用最优的;
- 若相同,则只能用次优的(且次优的 $x$ 必与最优的 $x$ 不同)。
所以对每个质因子 $p$,维护 `last[p] = {first, second}`:
- `first` 是当前以 $p$ 为因子的最优位置;
- `second` 是与 `first` 的 $x$ 不同的次优位置。
转移:
- 若 $x_i\ne x_{first}$,用 $dp[first]+1$;
- 否则用 $dp[second]+1$。
对所有质因子取最大值作为 $dp[i]$。
更新时把 $i$ 插入到每个质因子的 `last` 中,维护 `first`/`second` 的性质:
- 若 $dp[i]>dp[first]$:若 $x_i\ne x_{first}$,则 `first` 降为 `second`,$i$ 成为新 `first`;若 $x_i=x_{first}$,直接替换 `first` 为 $i$(`second` 不变);
- 否则若 $dp[i]>dp[second]$ 且 $x_i\ne x_{first}$,把 `second` 更新为 $i$。
最后答案是所有 $dp[i]$ 的最大值。
:::success[代码]
```cpp
#include <bits/stdc++.h>
using namespace std;
constexpr int MAXN = 2e5 + 10;
int dp[MAXN], a[MAXN], b[MAXN];
pair<int, int> last[MAXN]; vector<int> prime;
void getPrime(int x, vector<int>& fac, int& tot) {
for (int p = 2; p * p <= x; ++p)
if (x % p == 0) {
fac.push_back(p);
while (x % p == 0) x /= p;
}
if (x != 1) fac.push_back(x);
tot = fac.size();
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n; cin >> n;
for (int i = 1; i <= n; ++i) cin >> b[i] >> a[i];
for (int i = 1; i <= n; ++i) {
dp[i] = 1; prime.clear();
int cnt = 0; getPrime(b[i], prime, cnt);
for (int j = 0; j < cnt; ++j) {
if (a[i] != a[last[prime[j]].first])
dp[i] = max(dp[i], dp[last[prime[j]].first] + 1);
else dp[i] = max(dp[i], dp[last[prime[j]].second] + 1);
}
for (int j = 0; j < cnt; ++j) {
if (dp[i] > dp[last[prime[j]].first]) {
if (a[i] != a[last[prime[j]].first])
last[prime[j]] = {i, last[prime[j]].first};
else last[prime[j]] = {i, last[prime[j]].second};
}
else if (dp[i] > dp[last[prime[j]].second] &&
a[i] != a[last[prime[j]].first])
last[prime[j]].second = i;
}
}
int ans = INT_MIN;
for (int i = 1; i <= n; ++i) ans = max(ans, dp[i]);
cout << ans;
return 0;
}
```
:::
#### 复杂度
每个数分解质因子 $O(\sqrt{y})$,但实际质因子枚举是 $O(\log y)$ 级别。总复杂度 $O(n\log y)$,$n\le 2\times10^5$,可以通过。
最后,欢迎大家参加下一场[「 XPOI 」](https://www.luogu.com.cn/team/137589)比赛。