Codeforces Round #741 (Div. 2)
A. The Miracle and the Sleeper
不难证明答案等于 r % ( max ( l , ⌊ r 2 ⌋ + 1 ) ) r\%(\max(l, \lfloor\frac{r}{2}\rfloor+1)) r%(max(l,⌊2r⌋+1))
#include <algorithm>
#include <iostream>
using namespace std;
void solve()
{
int l, r; cin >> l >> r;
int x = max(l, r/2+1);
cout << r%x << endl;
}
int main()
{
int T; cin >> T;
while(T--) solve();
// system("pause");
}
B. Scenes From a Memory
考虑至少含有一个 { 1 , 4 , 6 , 8 , 9 } \{1,4,6,8,9\} { 1,4,6,8,9} 的元素的十进制表达式皆符合题意。
考虑只含 { 2 , 3 , 5 , 7 } \{2,3,5,7\} { 2,3,5,7}的两位数是合数时符合题意,换言之,两位数中只有 { 23 , 53 , 73 } \{23,53,73\} { 23,53,73}不符合题意。
考虑三位数必然符合题意,因为如果存在一个三位数x不符合题意,将x进行拆分成两位数必然得到 { 23 , 53 , 73 } \{23,53,73\} { 23,53,73}中的数字,且不存在 { 1 , 4 , 6 , 8 , 9 } \{1,4,6,8,9\} { 1,4,6,8,9},不妨暴力列出,发现无解。
综上,当k大于等于3,必定有解;当k=1时,若 n ∈ { 1.4 , 6 , 8 , 9 } n\in\{1.4,6,8,9\} n∈{ 1.4,6,8,9}有解;当k=2时,当 n ∈ { 23 , 53 , 73 } n\in\{23,53,73\} n∈{ 23,53,73}无解。
#include <algorithm>
#include <iostream>
using namespace std;
char s[100];
int b[10];
void solve()
{
for(int i = 0; i < 10; i++) b[i] = 0;
int k; scanf("%d %s",&k,&s);
for(int i = 0; i < k; i++)
if(s[i] == '1' || s[i] == '4' || s[i] == '6' || s[i] == '8' || s[i] == '9')
{
printf("1\n%c\n",s[i]);
return;
}
for(int i = 0; i < k; i++)
{
for(int j = 1; j <= 9; j++)
if(b[j])
{
int x = j*10+s[i]-'0';
if(x != 23 && x != 53 && x != 37 && x != 73)
{
printf("2\n%d\n",x);
return;
}
}
b[s[i]-'0'] = 1;
}
printf("-1\n");
}
int main()
{
int T; cin >> T;
while(T--) solve();
// system("pause");
}
C. Rings
考虑两种情况:
-
字符串至少包含一个0。这样我们就可以依照这个0的位置向前构造(2倍),或向后构造(1倍)
-
字符串不包含0,全为1。这个时候当且仅当长度n为偶数时有解(因为当a和b全为1串的情况下,当且仅当a的长度可以整除b的长度的时候,a可以整除b,列个除法竖式即可直观看出)
#include <algorithm>
#include <iostream>
using namespace std;
char s[20100];
void solve()
{
int n; scanf("%d %s",&n,s);
bool f = 0;
int cnt = 0;
for(int i = 0; i < n; i++)
{
if(s[i] == '0') {
f = 1; cnt = i+1; break;}
}
if(f)
{
if(cnt <= n/2) printf("%d %d %d %d\n",cnt,n,cnt+1,n);
else printf("%d %d %d %d\n",1,cnt,1,cnt-1);
}
else
{
if(n % 2 == 0)
{
printf("%d %d %d %d\n",1,n,1,n/2);
}
else
{
printf("%d %d %d %d\n",1,n-1,1,n/2);
}
}
}
int main()
{
int T; cin >> T;
while(T--)
{
solve();
}
// system("pause");
}
D1. Two Hundred Twenty One (easy version)
记a为元素数组,如果是正电荷则 a [ i ] a[i] a[i]为+1,如果是负电荷则为-1.
记b为一个数组,其中 b [ i ] b[i] b[i]表示移除元素 a [ i ] a[i] a[i]以后的剩余下来的数组的Signed Sum,特别的,不删除任何元素的Signed Sum记为 b 0 b_0