A - Piling Up
思路:
我们只需要找到下一阶段的下限。a / 100 是本阶段+1 变成下一阶段,再 * 100变成下限,再与原来的相减即可。
代码:
#include<bits/stdc++.h>
using namespace std;
#define N 200010
typedef long long ll;
typedef unsigned long long ull;
ll n, m, h, k, t, x, y, z;
ll a, b, c, d, mod = 998244353;
ll ans, num, sum1=0, sum, sum2=0, cnt, maxx, minn = 1e9;
ll f1[N], f2[N], dp[N], an[N];
bool flag, vis[N];
string s,s1,s2;
map<ll, ll>mp;
int main()
{
cin >> n;
if (n % 100 == 0)
{
cout << "100\n";
return 0;
}
else
{
k = n / 100;
k = (k + 1) * 100;
cout << k - n << "\n";
}
return 0;
}
B - Japanese Cursed Doll
思路:
将L𝐿从小往大排序,那么只需要第n−P+1𝑛−𝑃+1大的数满足条件即可,假设这个数是a𝑎。因此,只需要计算T−a𝑇−𝑎即可。注意,如果这个数是负数,意味着初始时就满足条件,此时要输出00。
代码:
#include<bits/stdc++.h>
using namespace std;
#define N 200010
typedef long long ll;
typedef unsigned long long ull;
ll n, m, h, k, t, x, y, z;
ll a, b, c, d, mod = 998244353;
ll ans, num, sum1=0, sum, sum2=0, cnt, maxx, minn = 1e9;
ll f1[N], f2[N], dp[N], an[N];
bool flag, vis[N];
string s,s1,s2;
map<ll, ll>mp;
bool cmp(ll x, ll y) {
return x > y;
}
int main()
{
ll p;
sum = ans = 0;
cin >> n >> t >> p;
for (int i = 1; i <= n; i++)
cin >> dp[i];
sort(dp + 1, dp + n + 1, cmp);
for (int i = 1; i <= n; i++) {
if (sum >= p)
break;
if (dp[i] >= t)
sum++;
else {
ans = max(ans, t - dp[i]);
sum++;
}
}
cout << ans << endl;
return 0;
}
C - Avoid K Palindrome 2
思路:
注意数据范围,完全可以依次枚举每一个排列,可以使用next_permutation
函数。接着暴力判断是否拥有回文子串即可。
代码:
#include<bits/stdc++.h>
#include <unordered_map>
using namespace std;
#define N 200010
typedef long long ll;
typedef unsigned long long ull;
ll n, m, h, k, t, x, y, z;
ll a, b, c, d, mod = 998244353;
ll ans, num, sum1 = 0, sum, sum2 = 0, maxx, minn = 1e9;
ll f1[N], f2[N], dp[N], an[N], cnt[N];
bool flag, vis[N];
string s, s1, s2;
map<string, ll>mp;
bool cmp(ll x, ll y) {
return x > y;
}
void solve() {
cin >> n >> m;
cin >> s;
ans = 0;
sort(s.begin(), s.end());
do {
flag = true;
for (int i = 0; i + m - 1 < n; i++) {
bool f = true;
for (int j = 0; j < m; j++) {
if (s[i + j] != s[i + m- j - 1])
f = false;
}
if (f) flag = false;
}
if (flag)
ans++;
} while (next_permutation(s.begin(), s.end()));
cout << ans << endl;
}
int main()
{
solve();
return 0;
}
D - Palindromic Number
思路:
回文数只需要考虑前一半就可以构造。枚举长度,首先计算出答案回文数的长度。通过枚举和减法可以计算出是当前长度下第k𝑘大的回文数。而前一半就是顺序的,第k𝑘大就是当前长度的数的第k𝑘大,这个很好求。接下来只需要构成回文,直接输出即可。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
map<int, int>mp;
const int N = 2e6 + 10;
long long sum[60], a[60];
long long n;
int ans[1010], len, b[1010];
string s,s1,s2;
bool cmp(ll x, ll y) {
return x > y;
}
int mod(string a, ll b)
{
ll n = 0;
for (int i = 0; i < a.size(); i++) n = (n * 10 + (a[i] - '0')) % b;
return n;
}
string gcd(string a, ll b)
{
while (b != 0)
{
ll temp = b;
b = mod(a, b);
a = to_string(temp);
}
return a;
}
inline int gcd1(int a, int b)
{
while (b ^= a ^= b ^= a %= b);
return a;
}
inline int gcd2(int a, int b)
{
if (b) while ((a %= b) && (b %= a));
return a + b;
}
int lcm1(int a, int b) {
return a * b / gcd1(a, b);
}
ll pow(int x) {
ll res = 1;
for (int i = 1; i <= x; i++)
res *= 10;
return res;
}
int main()
{
for (int i = 1; i <= 37; i++) {
int xx = ((i + 1) / 2 - 1);
a[i] = 1LL * 9 * pow(xx);
sum[i] = sum[i - 1] + a[i];
}
cin >> n;
if (n <= 1)
{
cout << "0" << endl;
return 0;
}
n -= 1;
int pos = lower_bound(sum + 1, sum + 38, n) - sum;
if (n == sum[pos - 1])
pos--;
while (n) {
b[++len] = n % 10;
n /= 10;
}
for (int i = 1, j = len; i <= len; i++, j--)
ans[i] = b[j];
ans[1]--; ans[len]++;
if (ans[1] == 0 && ans[2] == 0)ans[2] = 9;
int now = len;
while (ans[now] >= 10) {
ans[now - 1] += ans[now] / 10;
ans[now] %= 10;
now--;
}
int l = len, r = len + 1;
if (pos % 2 == 1)l--;
while (l >= 1) {
ans[r] = ans[l];
l--; r++;
}
l = 1, r--;
while (ans[l] == 0)
l++;
while (ans[r] == 0)
r--;
for (int i = l; i <= r; i++)
cout << ans[i];
return 0;
}