L. Suzuran Loves String(贪心)
题意:给出一个字符串,让你选择任意的两个后缀,可以通过对较长的后缀的删除最后一个字符添加最后一个字符的操作使得该后缀转换为另一个后缀
思路:考虑选定全长,然后找到第一个不同的字符,注意特判全相同的字符串。
例如
aaaaaaabc是较长的后缀,那么较短的如果是
aaaaaabc的话
aaaaabc的话
前者不如后者,如果已经首个不同的话,已经是全删除了,后面也不会更优。
如果较长的后缀不是全长,那么全长肯定不会更差。
#include <bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define eps 1e-9
using namespace std;
const int N = 1e5 + 5;
void solve() {
string s;
cin >> s;
int sz = s.size();
for (int i = 1; i < sz; i++) {
if (s[i] ^ s[0]) {
cout << sz + sz - i << '\n';
return;
}
}
cout << sz - 1 << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T = 1;
cin >> T;
while (T--) {
solve();
}
return 0;
}
B. Arithmetic Exercise(假的高精度)
题意:模拟除法
注意:这里进位比较特殊,我们只需乘10 除b即可
#include <bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define eps 1e-9
using namespace std;
const int N = 1e5 + 5;
void solve() {
int a, b, k;
cin >> a >> b >> k;
cout << a / b << ".";
a %= b;
vector<int> ans;
for (int i = 1; i <= k; i++) {
a *= 10;
ans.push_back(a / b);
a %= b;
}
int mod = a * 10 / b;
if (mod >= 5) ans.back()++;
for (auto x : ans) cout << x;
cout << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T = 1;
// cin >> T;
while (T--) {
solve();
}
return 0;
}
K. Bracket Sequence(卡特兰数)
题意:有n种不同的括号,k种不同的颜色,问有多少种合法的括号序列。
思路:合法的括号序列满足卡特兰数,对所有左括号染色共有k^n种方法,利用乘法原理可以得出。
#include <bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define eps 1e-9
using namespace std;
const int N = 1e5 + 5, mod = 1e9 + 7;
ll po(ll rad, ll idx) {
ll res = 1;
while (idx) {
if (idx & 1) res = res * rad % mod;
rad = rad * rad % mod;
idx >>= 1;
}
return res;
}
ll f[N];
void pre() {
f[0] = 1;
f[1] = 1;
for (int i = 2; i <= 100000; i++) {
f[i] = f[i - 1] * (4 * i - 2) % mod * po(i + 1, mod - 2) % mod;
}
}
void solve() {
pre();
ll n, k;
cin >> n >> k;
cout << po(k, n) * f[n] % mod << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int T = 1;
// cin >> T;
while (T--) {
solve();
}
return 0;
}