T1
设区间 的长度为 ,则
- 时,有可能一步跨过去,但最多统计一次,直接树状数组动态差分即可。
- 时,至少跨进去一次,跨多次只算一次,按 的贡献算即可。
code
#include <bits/stdc++.h>
#define debug(x) (cout << #x << " " << x << "\n")
#define lowbit(x) ((x) & -(x))
using namespace std;
using ll = long long;
const int N = 3e5 + 5;
struct node {
ll l, r;
} q[N];
ll n, m, tree[N];
void update(ll x, ll v) {
while (x <= m) {
tree[x] += v;
x += lowbit(x);
}
}
ll query(ll x) {
ll sum = 0;
while (x) {
sum += tree[x];
x -= lowbit(x);
}
return sum;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (ll i = 1; i <= n; i++) cin >> q[i].l >> q[i].r;
sort(q + 1, q + 1 + n, [&](const node &x, const node &y) {
return x.r - x.l < y.r - y.l;
});
ll pos = 1;
for (ll i = 1; i <= m; i++) {
while (pos <= n && q[pos].r - q[pos].l + 1 < i) {
update(q[pos].l, 1);
update(q[pos].r + 1, -1);
++ pos;
}
ll sum = 0;
for (ll j = i; j <= m; j += i) sum += query(j);
cout << sum + n - pos + 1 << "\n";
}
return 0;
}T2
二进制分解 即可得到需要经过的行。剩余的补充。
code
#include <bits/stdc++.h>
#define debug(x) (cout << #x << " " << x << "\n")
using namespace std;
using ll = long long;
const int N = 505, mod = 99824353;
const ll MOD = 1e18 + 1;
ll n, dp[N][N];
vector<pair<ll, ll>> vec;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
dp[1][1] = dp[2][1] = dp[2][2] = 1;
for (ll i = 1; i <= 500; i++) {
for (ll j = 1; j <= i; j++) {
if (j == 1 || j == i) dp[i][j] = 1;
else dp[i][j] = (dp[i - 1][j - 1] + dp[i - 1][j]) % MOD;
}
}
cin >> n;
if (n <= 60) {
cout << n << "\n";
for (ll i = 1; i <= n; i++) cout << i << " 1\n";
cout << n << " 1";
return 0;
}
ll tmp = n - 60, cnt = 1, ans = 1, num = 60, sum = 0;
bool flag = 0;
while (tmp) {
if (tmp & 1) {
if (flag) {
for (ll i = 1; i <= cnt; i++) {
vec.emplace_back(make_pair(cnt, i));
ans = dp[cnt][i] % mod * ans % mod;
sum += dp[cnt][i];
}
} else {
for (ll i = cnt; i >= 1; i--) {
vec.emplace_back(make_pair(cnt, i));
ans = dp[cnt][i] % mod * ans % mod;
sum += dp[cnt][i];
}
}
flag ^= 1;
} else {
-- num;
if (!flag) vec.emplace_back(make_pair(cnt, cnt));
else vec.emplace_back(make_pair(cnt, 1));
++ sum;
}
++ cnt, tmp >>= 1;
}
sum += num;
for (ll i = 1; i <= num; i++) {
if (!flag) vec.emplace_back(make_pair(cnt, cnt));
else vec.emplace_back(make_pair(cnt, 1));
++ cnt;
}
cout << vec.size() << "\n";
for (auto p : vec) cout << p.first << " " << p.second << "\n";
cout << sum << " " << ans;
return 0;
}T3
记忆化搜索,倒推原数,dfs 即可。
code
#include <bits/stdc++.h>
#define debug(x) (cout << #x << " " << x << "\n")
using namespace std;
using ll = unsigned long long;
map<ll, ll> mp;
ll T, n, a, b, c, d;
ll dfs(ll x) {
if (mp.count(x)) return mp[x];
ll ans = x * d;
if (x & 1) ans = min(ans, min(dfs((x - x % 2) / 2), dfs((x + 2 - x % 2) / 2)) + a + d);
else ans = min(ans, dfs(x / 2) + a);
if (x % 3) ans = min(ans, min(dfs((x - x % 3) / 3) + x % 3 * d, dfs((x + 3 - x % 3) / 3) + (3 - x % 3) * d) + b);
else ans = min(ans, dfs(x / 3) + b);
if (x % 5) ans = min(ans, min(dfs((x - x % 5) / 5) + x % 5 * d, dfs((x + 5 - x % 5) / 5) + (5 - x % 5) * d) + c);
else ans = min(ans, dfs(x / 5) + c);
return mp[x] = ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> T;
while (T --) {
cin >> n >> a >> b >> c >> d;
mp.clear();
mp[0] = 0, mp[1] = d;
cout << dfs(n) << "\n";
}
return 0;
}T4
考虑两个相同的字符串 ,那么 ,贡献为其中两个字符串分别的答案和连起来的答案,也就是左右端点分别在不同的字符串内的贡献。将所有字符串编号 ,则贡献可以这样计算:。从而 。结论:,这是一个等差数列。等差数列求和:。现在需要求 。设 ,那么 。结论感性理解。
code
#include <bits/stdc++.h>
#define debug(x) (cout << #x << " " << x << "\n")
using namespace std;
using ll = long long;
const int N = 3e7 + 5, mod = 998244353;
ll T, n, m, p[N];
ll ADD(ll x, ll y) { return ((x % mod) + (y % mod)) % mod; }
ll MUL(ll x, ll y) { return (x % mod) * (y % mod) % mod; }
string add(string s) {
string t = "^";
for (ll i = 0; i < s.size(); i++) {
t += '#';
t += s[i];
}
t += "#@";
m = 2 * s.size() + 1;
return t;
}
ll manacher(string s) {
ll r = 0, mid = 0, res = 0;
for (ll i = 1; i <= m; i++) {
p[i] = ((i < r) ? min(p[2 * mid - i], r - i) : 1);
while (s[i + p[i]] == s[i - p[i]]) ++ p[i];
if (i + p[i] > r) r = i + p[i], mid = i;
res += p[i] / 2;
}
return res;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> T;
while (T --) {
ll n, k; cin >> n >> k;
string s1; cin >> s1;
string tmp = add(s1);
ll A = manacher(tmp);
string s2 = s1 + s1;
tmp = add(s2);
ll B = manacher(tmp);
string s3 = s1 + s1 + s1;
tmp = add(s3);
ll C = manacher(tmp);
ll res = ADD(A, ADD(MUL(B - A, k - 1), MUL((k - 1) * (k - 2) / 2, (C - 2 * B + A))));
cout << (res % mod + mod) % mod << "\n";
}
return 0;
}