A
题意
求
l<=a<=b<=r
max(b%a)
思路
理想情况相当于将b分成数值差1的两部分,取小的那部分
当b%a中a不够小(比一半中大的数还大)就直接的b%a
AC代码
#include<bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
int a,b;
cin >> a >> b;
if(a==b){
cout << 0 << endl;
}
else{
if(b%2==1){
int c = (b+1) / 2;
if(a<=c){
cout << c-1 << endl;
}else{
cout << b % a << endl;
}
}else{
int c = b / 2+1;
if(a>=c){
cout << b % a << endl;
}else{
cout << b / 2 - 1 << endl;
}
}
}
}
}
B
思路
在一个非0数值字符串中,找到子序列
满足子序列非素数(1或者合数)
1,4,6,8,9
是单个的非素数
再考虑两位的子序列
个位如果是2,就是合数
剩下3,5,7
(33,55,77)%11=0
假设2位数的没有了
考虑三位数的,则三位数必两两不相同
考虑2357的全排列组合
3 5 7 (57%3==0)
2 5 7 (2和5放第一位都可以)
2 3 7 (27%3==0)
2 3 5 (,,,)
所以不存在三位数,最大结果就是保留两位
AC代码
#include <bits/stdc++.h>
using namespace std;
string s;
bool ispreme(int x)
{
for (int i = 2; i * i <= x; i++)
{
if (x % i == 0)
{
return true;
}
}
return false;
}
void solve(int n, string s)
{
for (int i = 0; i < n; i++)
{
if (s[i] == '1' || s[i] == '4' || s[i] == '6' || s[i] == '8' || s[i] == '9')
{
cout << 1 << endl;
cout << s[i] << endl;
return;
}
}
for (int i = 0; i < n - 1; i++)
{
int x = s[i] - '0';
for (int j = 1; j < n; j++)
{
int y = x * 10 + s[j] - '0';
if (ispreme(y)){
cout << 2 << endl;
cout << s[i] << s[j] << endl;
return;
}
}
}
return;
}
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
cin >> s;
solve(n, s);
}
}
C
见代码
#include <cstdio>
using namespace std;
char s[20002];
int main()
{
int t;
scanf("%d", &t);
for (int tt = 1; tt <= t; tt++)
{
int n;
scanf("%d%s", &n, s + 1);
for (int i = 1; i <= n; i++)
if (s[i] == '0' && i * 2 - 1 != n)
{
if (i * 2 - 1 > n)
printf("%d %d %d %d\n", 1, i, 1, i - 1);
else
printf("%d %d %d %d\n", i, n, i + 1, n);
goto ed;
}
printf("%d %d %d %d\n", 1, (n + 1) / 2, n / 2 + 1, n);
ed:;
}
}
D1
思路
前缀跑一遍
我们可以知道
当抽走某一个的时候,后面的都将变成相反的符号
我们选择合适的位置抽走,可以抵消一部分的
最终结果有恰好,1和2
AC代码
#include <bits/stdc++.h>
using namespace std;
int t;
int n, m;
string s;
const int N = 3E5 + 7;
int arr[N];
int main()
{
cin >> t;
while (t--)
{
cin >> n >> m;
cin >> s;
//memset(arr, 0, sizeof(arr));
for (int i = 0; i < n; i++)
{
if ((s[i] == '+' && i % 2 == 0) || (s[i] == '-' && i % 2 == 1))
{
arr[i + 1] = arr[i] + 1;
}
else
{
arr[i + 1] = arr[i] - 1;
}
}
for (int i = 0; i <= n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
while (m--)
{
int l, r;
cin >> l >> r;
int ans = abs(arr[r] - arr[l - 1]);
cout << ans%3 << endl;
}
}
}