A. Puzzle From the Future
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
In the 2022 year, Mike found two binary integers a and b of length n (both of them are written only by digits 0 and 1) that can have leading zeroes. In order not to forget them, he wanted to construct integer d in the following way:
he creates an integer c as a result of bitwise summing of a and b without transferring carry, so c may have one or more 2-s. For example, the result of bitwise summing of 0110 and 1101 is 1211 or the sum of 011000 and 011000 is 022000;
after that Mike replaces equal consecutive digits in c by one digit, thus getting d. In the cases above after this operation, 1211 becomes 121 and 022000 becomes 020 (so, d won’t have equal consecutive digits).
Unfortunately, Mike lost integer a before he could calculate d himself. Now, to cheer him up, you want to find any binary integer a of length n such that d will be maximum possible as integer.
Maximum possible as integer means that 102>21, 012<101, 021=21 and so on.
Input
The first line contains a single integer t (1≤t≤1000) — the number of test cases.
The first line of each test case contains the integer n (1≤n≤105) — the length of a and b.
The second line of each test case contains binary integer b of length n. The integer b consists only of digits 0 and 1.
It is guaranteed that the total sum of n over all t test cases doesn’t exceed 105.
Output
For each test case output one binary integer a of length n. Note, that a or b may have leading zeroes but must have the same length n.
Example
inputCopy
5
1
0
3
011
3
110
6
111000
6
001011
outputCopy
1
110
100
101101
101110
Note
In the first test case, b=0 and choosing a=1 gives d=1 as a result.
In the second test case, b=011 so:
if you choose a=000, c will be equal to 011, so d=01;
if you choose a=111, c will be equal to 122, so d=12;
if you choose a=010, you’ll get d=021.
If you select a=110, you’ll get d=121.
We can show that answer a=110 is optimal and d=121 is maximum possible.
In the third test case, b=110. If you choose a=100, you’ll get d=210 and it’s the maximum possible d.
In the fourth test case, b=111000. If you choose a=101101, you’ll get d=212101 and it’s maximum possible d.
In the fifth test case, b=001011. If you choose a=101110, you’ll get d=102121 and it’s maximum possible d.
#include <iostream>
#include <cstdio>
#include <cstring>
#define Maxn 100005
using namespace std;
char a[Maxn],b[Maxn],c[Maxn];
int main() {
int T,n; cin >> T;
c[0] = '1';
c[1] = '0';
while(T--) {
cin >> n;
scanf("%s",b);
memset(a,0,sizeof(a));
a[0] = '1';
for(int i=1; i<n; i++) {
if(b[i - 1] == '0' && b[i] == '1') a[i] = '1';
else if(b[i - 1] == '0' && b[i] == '0') a[i] = c[a[i - 1] - '0'];
else if(b[i - 1] == '1' && b[i] == '0') a[i] = a[i - 1];
else if(b[i - 1] == '1' && b[i] == '1') a[i] = c[a[i - 1] - '0'];
}
puts(a);
}
return 0;
}
B. Different Divisors
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Positive integer x is called divisor of positive integer y, if y is divisible by x without remainder. For example, 1 is a divisor of 7 and 3 is not divisor of 8.
We gave you an integer d and asked you to find the smallest positive integer a, such that
a has at least 4 divisors;
difference between any two divisors of a is at least d.
Input
The first line contains a single integer t (1≤t≤3000) — the number of test cases.
The first line of each test case contains a single integer d (1≤d≤10000).
Output
For each test case print one integer a — the answer for this test case.
Example
inputCopy
2
1
2
outputCopy
6
15
Note
In the first test case, integer 6 have following divisors: [1,2,3,6]. There are 4 of them and the difference between any two of them is at least 1. There is no smaller integer with at least 4 divisors.
In the second test case, integer 15 have following divisors: [1,3,5,15]. There are 4 of them and the difference between any two of them is at least 2.
The answer 12 is INVALID because divisors are [1,2,3,4,6,12]. And the difference between, for example, divisors 2 and 3 is less than d=2.
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#define Maxn 10000
using namespace std;
bool IsPrime[100005];
int prime[50005];
int main(int argc, char const *argv[]) {
int n,top=1;
memset(IsPrime,0,sizeof(IsPrime));
for(int i=2;i<=Maxn*3+5;i++) {
if(!IsPrime[i])
prime[top]=i,top++;
for(int j=1;j<top;j++) {
if(i * prime[j] > Maxn*3+5)
break;
IsPrime[i*prime[j]] = 1;
if(i % prime[j] == 0)
break;
}
}
int T,d; cin >> T;
//cout << prime[1] << endl;
while(T--) {
long long A = 0,B = 0;
cin >> d;
for(int i=1; i<top; i++){
if(prime[i] - 1 >= d) { A = prime[i]; break; }
}
for(int i=1; i<top; i++){
if(prime[i] - A >= d && A != 0) { B = prime[i]; break; }
}
cout << A * B << endl;
}
return 0;
}
C. Array Destruction
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You found a useless array a of 2n positive integers. You have realized that you actually don’t need this array, so you decided to throw out all elements of a.
It could have been an easy task, but it turned out that you should follow some rules:
In the beginning, you select any positive integer x.
Then you do the following operation n times:
select two elements of array with sum equals x;
remove them from a and replace x with maximum of that two numbers.
For example, if initially a=[3,5,1,2], you can select x=6. Then you can select the second and the third elements of a with sum 5+1=6 and throw them out. After this operation, x equals 5 and there are two elements in array: 3 and 2. You can throw them out on the next operation.
Note, that you choose x before the start and can’t change it as you want between the operations.
Determine how should you behave to throw out all elements of a.
Input
The first line contains a single integer t (1≤t≤1000) — the number of test cases.
The first line of each test case contains the single integer n (1≤n≤1000).
The second line of each test case contains 2n integers a1,a2,…,a2n (1≤ai≤106) — the initial array a.
It is guaranteed that the total sum of n over all test cases doesn’t exceed 1000.
Output
For each test case in the first line print YES if it is possible to throw out all elements of the array and NO otherwise.
If it is possible to throw out all elements, print the initial value of x you’ve chosen. Print description of n operations next. For each operation, print the pair of integers you remove.
Example
inputCopy
4
2
3 5 1 2
3
1 1 8 8 64 64
2
1 1 2 4
5
1 2 3 4 5 6 7 14 3 11
outputCopy
YES
6
1 5
2 3
NO
NO
YES
21
14 7
3 11
5 6
2 4
3 1
Note
The first test case was described in the statement.
In the second and third test cases, we can show that it is impossible to throw out all elements of array a.
** 题目分析:**
第一组消除的数一定是最大的数字和另外某个数字,我们可以枚举这个另外的数字,当第一组消除之后,第二组中肯定包含剩下的数中的最大值,(否则那个最大值是无法消除的),然后用set log级检查就可以了,当时比赛的时候有这样的想法,但是没能写出来,这两天杂七杂八的事情也很多,很烦躁,过了这些天才补题
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#define Maxn 2005
using namespace std;
int ans1[Maxn],ans2[Maxn],a[Maxn];
int main(int argc,char* argv[]) {
int T,n,tot,flag,m; scanf("%d",&T);
while(T--) {
scanf("%d",&n);
n += n;
for(int i=1; i<=n; i++) scanf("%d",&a[i]);
sort(a + 1, a + n + 1);
for(int i=1; i<n; i++) {
multiset<int> v;
tot = 0,flag = 0,m = a[n];
ans1[++tot] = m;
ans2[tot] = a[i];
for(int j=1; j<n; j++)
if(j != i) v.insert(a[j]);
multiset<int>::iterator it1,it2;
while(!v.empty()) {
it1 = v.end();
it1--;
v.erase(it1);
int t = *it1;
it2 = v.find(m - t);
if(it2 != v.end()) {
ans1[++tot] = t;
ans2[tot] = m - t;
v.erase(it2);
m = t;
}
else {
flag = 1;
break; // 这样的枚举方案不行
}
}
if(flag == 0) break;
}
if(flag == 1) printf("NO\n");
else {
printf("YES\n");
printf("%d\n",ans1[1] + ans2[1]);
for(int i=1; i<=tot; i++)
printf("%d %d\n",ans1[i],ans2[i]);
}
}
return 0;
}