P9232 [蓝桥杯 2023 省 A] 更小的数
题目链接:https://www.luogu.com.cn/problem/P9232#submit
题目思路:有2种情况可以达成题意,首先是反转的字符串中第一位大于最后一位,其次是虽然前x位和后x位相同,但是中间不同的位是前面大于后面的,比如22203222也是一种,然后模拟就可以了
题目代码:
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const ll maxn = 1e18;
const int MOD = 998244353;
ll gcd(ll a, ll b) {
return b ? gcd(b, a % b) : a;
}
void solve() {
string s; cin >> s;
int n = s.size();
ll cnt = 0;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (s[i] > s[j]) {
cnt++;
}
else if (s[i] == s[j]) {
for (int t = i + 1, tt = j - 1; t < tt; t++, tt--) {
if (s[t] > s[tt]) {
cnt++;
break;
}
else if (s[t] < s[tt]) {
break;
}
}
}
}
}
cout << cnt << '\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
std::cout.tie(0);
int t1 = 1;
//cin >> t1;
while (t1--)
solve();
return 0;
}
P9231 [蓝桥杯 2023 省 A] 平方差
题目链接:https://www.luogu.com.cn/problem/P9231
假设y+z=n,y-z=m,则y=(n+m)/2,z=(n-m)/2, 又x=n*m且都为整数,则n和m奇偶性相同,所以x要么为奇数要么为4的倍数
然后遍历就可以了
代码:
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const ll maxn = 1e18;
const int MOD = 998244353;
ll gcd(ll a, ll b) {
return b ? gcd(b, a % b) : a;
}
void solve() {
int l, r; cin >> l >> r;
int cnt = 0;
for (int i = l; i <= r; i++) {
if (i & 1 || i % 4 == 0)
cnt++;
}
cout << cnt << '\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
std::cout.tie(0);
int t1 = 1;
//cin >> t1;
while (t1--)
solve();
return 0;
}