Submission #74297944
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int MAXK = 105;
const ll INF = (ll)2e18; // 超过查询范围 1e18 即可
ll len[MAXK]; // 长度上限设为 > 2e18 时截断
ull cnt[MAXK][26];
string X, Y;
// 计算 S_k 的前 n 个字符中,字符 c 的出现次数
// n 用 ll,但查询范围是 1e18,在 ll 范围内
ull solve(int k, ll n, int c) {
if (n <= 0)
return 0;
if (k == 1) {
ull res = 0;
for (ll i = 0; i < n && i < (ll)X.length(); i++)
if (X[(size_t)i] == 'a' + c)
res++;
return res;
}
if (k == 2) {
ull res = 0;
for (ll i = 0; i < n && i < (ll)Y.length(); i++)
if (Y[(size_t)i] == 'a' + c)
res++;
return res;
}
if (n <= len[k - 1]) {
return solve(k - 1, n, c);
} else {
// 左半部分全取,右半部分递归
// 注意:n - len[k-1] 可能很大,但递归深度有限
return cnt[k - 1][c] + solve(k - 2, n - len[k - 1], c);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> X >> Y;
int Q;
cin >> Q;
// 预处理
len[1] = (ll)X.length();
len[2] = (ll)Y.length();
for (int c = 0; c < 26; c++) {
char ch = 'a' + c;
cnt[1][c] = 0;
for (char x : X)
if (x == ch)
cnt[1][c]++;
cnt[2][c] = 0;
for (char y : Y)
if (y == ch)
cnt[2][c]++;
}
int K = 2;
ll LIM = (ll)4e18; // 上限,但实际 ll 最大约 9e18
// 防止溢出:当长度将超过 4e18 时截断
while (K < MAXK - 1 && len[K] < (ll)1e18) { // 只需要到超过 1e18 即可
K++;
// 检查溢出
if (len[K - 1] > LIM - len[K - 2]) {
len[K] = LIM;
} else {
len[K] = len[K - 1] + len[K - 2];
}
for (int c = 0; c < 26; c++) {
cnt[K][c] = min((ull)INF, cnt[K - 1][c] + cnt[K - 2][c]);
}
}
// 确保 len[K] >= 1e18
while (len[K] < (ll)1e18 && K < MAXK - 1) {
K++;
if (len[K - 1] > LIM - len[K - 2]) {
len[K] = LIM;
} else {
len[K] = len[K - 1] + len[K - 2];
}
for (int c = 0; c < 26; c++)
cnt[K][c] = min((ull)INF, cnt[K - 1][c] + cnt[K - 2][c]);
}
while (Q--) {
ll L, R;
char C;
cin >> L >> R >> C;
int c = C - 'a';
ull ans = solve(K, R, c) - solve(K, L - 1, c);
cout << ans << "\n";
}
return 0;
}
Submission Info
| Submission Time | |
|---|---|
| Task | E - Fibonacci String |
| User | Eason0709 |
| Language | C++23 (GCC 15.2.0) |
| Score | 450 |
| Code Size | 2331 Byte |
| Status | AC |
| Exec Time | 547 ms |
| Memory | 3820 KiB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 450 / 450 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | sample_01.txt |
| All | random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, sample_01.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| random_01.txt | AC | 42 ms | 3608 KiB |
| random_02.txt | AC | 42 ms | 3668 KiB |
| random_03.txt | AC | 79 ms | 3664 KiB |
| random_04.txt | AC | 79 ms | 3672 KiB |
| random_05.txt | AC | 78 ms | 3748 KiB |
| random_06.txt | AC | 79 ms | 3672 KiB |
| random_07.txt | AC | 78 ms | 3660 KiB |
| random_08.txt | AC | 79 ms | 3752 KiB |
| random_09.txt | AC | 79 ms | 3704 KiB |
| random_10.txt | AC | 80 ms | 3596 KiB |
| random_11.txt | AC | 79 ms | 3676 KiB |
| random_12.txt | AC | 544 ms | 3820 KiB |
| random_13.txt | AC | 544 ms | 3596 KiB |
| random_14.txt | AC | 547 ms | 3672 KiB |
| sample_01.txt | AC | 1 ms | 3724 KiB |