A. Even Subset Sum Problem
You are given an array aa consisting of nn positive integers. Find a non-empty subset of its elements such that their sum is even (i.e. divisible by 22) or determine that there is no such subset.
Both the given array and required subset may contain equal values.
Input
The first line contains a single integer tt (1≤t≤1001≤t≤100), number of test cases to solve. Descriptions of tt test cases follow.
A description of each test case consists of two lines. The first line contains a single integer nn (1≤n≤1001≤n≤100), length of array aa.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1001≤ai≤100), elements of aa. The given array aa can contain equal values (duplicates).
Output
For each test case output −1−1 if there is no such subset of elements. Otherwise output positive integer kk, number of elements in the required subset. Then output kk distinct integers (1≤pi≤n1≤pi≤n), indexes of the chosen elements. If there are multiple solutions output any of them.
Example
input
Copy
3 3 1 4 3 1 15 2 3 5output
Copy
1 2 -1 2 1 2Note
There are three test cases in the example.
In the first test case, you can choose the subset consisting of only the second element. Its sum is 44 and it is even.
In the second test case, there is only one non-empty subset of elements consisting of the first element, however sum in it is odd, so there is no solution.
In the third test case, the subset consisting of all array's elements has even sum.
题意:问能否在给定的数组中找出和为偶数的数字,若有,输出需要几个数及其下标,反之,输出-1
题解: 求和,记录一个奇数位置,和为偶数,全部输出,反之,将奇数为跳过,输出剩余的。
#include
#include
#include
#include
#include
#include
#include
B. Count Subrectangles
You are given an array aa of length nn and array bb of length mm both consisting of only integers 00 and 11. Consider a matrix cc of size n×mn×m formed by following rule: ci,j=ai⋅bjci,j=ai⋅bj (i.e. aiai multiplied by bjbj). It's easy to see that cc consists of only zeroes and ones too.
How many subrectangles of size (area) kk consisting only of ones are there in cc?
A subrectangle is an intersection of a consecutive (subsequent) segment of rows and a consecutive (subsequent) segment of columns. I.e. consider four integers x1,x2,y1,y2x1,x2,y1,y2 (1≤x1≤x2≤n1≤x1≤x2≤n, 1≤y1≤y2≤m1≤y1≤y2≤m) a subrectangle c[x1…x2][y1…y2]c[x1…x2][y1…y2] is an intersection of the rows x1,x1+1,x1+2,…,x2x1,x1+1,x1+2,…,x2 and the columns y1,y1+1,y1+2,…,y2y1,y1+1,y1+2,…,y2.
The size (area) of a subrectangle is the total number of cells in it.
Input
The first line contains three integers nn, mm and kk (1≤n,m≤40000,1≤k≤n⋅m1≤n,m≤40000,1≤k≤n⋅m), length of array aa, length of array bb and required size of subrectangles.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤10≤ai≤1), elements of aa.
The third line contains mm integers b1,b2,…,bmb1,b2,…,bm (0≤bi≤10≤bi≤1), elements of bb.
Output
Output single integer — the number of subrectangles of cc with size (area) kk consisting only of ones.
Examples
input
Copy
3 3 2 1 0 1 1 1 1output
Copy
4input
Copy
3 5 4 1 1 1 1 1 1 1 1output
Copy
14Note
In first example matrix cc is:
There are 44 subrectangles of size 22 consisting of only ones in it:
In second example matrix cc is:
题意:由给定的两个bool类型数组AB相乘形成bool矩阵C,问矩阵中由1组成的面积为K的矩阵有多少个
题解:如果数组A的某个位置为0,那么矩阵C中的某一行全部是0,如果数组B的某个位置为0,那么矩阵C中的某一列全部是0,那么,我们分别就求一下前缀和,最后遍历K的因子。遍历因子的时候,如果i*i==k,我们计算所有a[x1]-a[x2]==i的个数cnt,b[x3]-3[x4]==i的个数res,ans+=cnt*res就可以了;而如果k/i!=i,那么分a[x1]-a[x2]=i和a[x1]-a[x2]=k/i两种情况去进行计算。
#include
#include
#include
#include
#include
#include
#include
C. Unusual Competitions
A bracketed sequence is called correct (regular) if by inserting "+" and "1" you can get a well-formed mathematical expression from it. For example, sequences "(())()", "()" and "(()(()))" are correct, while ")(", "(()" and "(()))(" are not.
The teacher gave Dmitry's class a very strange task — she asked every student to come up with a sequence of arbitrary length, consisting only of opening and closing brackets. After that all the students took turns naming the sequences they had invented. When Dima's turn came, he suddenly realized that all his classmates got the correct bracketed sequence, and whether he got the correct bracketed sequence, he did not know.
Dima suspects now that he simply missed the word "correct" in the task statement, so now he wants to save the situation by modifying his sequence slightly. More precisely, he can the arbitrary number of times (possibly zero) perform the reorder operation.
The reorder operation consists of choosing an arbitrary consecutive subsegment (substring) of the sequence and then reordering all the characters in it in an arbitrary way. Such operation takes ll nanoseconds, where ll is the length of the subsegment being reordered. It's easy to see that reorder operation doesn't change the number of opening and closing brackets. For example for "))((" he can choose the substring ")(" and do reorder ")()(" (this operation will take 22 nanoseconds).
Since Dima will soon have to answer, he wants to make his sequence correct as fast as possible. Help him to do this, or determine that it's impossible.
Input
The first line contains a single integer nn (1≤n≤1061≤n≤106) — the length of Dima's sequence.
The second line contains string of length nn, consisting of characters "(" and ")" only.
Output
Print a single integer — the minimum number of nanoseconds to make the sequence correct or "-1" if it is impossible to do so.
Examples
input
Copy
8 ))((())(output
Copy
6input
Copy
3 (()output
Copy
-1Note
In the first example we can firstly reorder the segment from first to the fourth character, replacing it with "()()", the whole sequence will be "()()())(". And then reorder the segment from the seventh to eighth character, replacing it with "()". In the end the sequence will be "()()()()", while the total time spent is 4+2=64+2=6 nanoseconds.
题意:问给定的括号序列能否形成由()形式组成的序列,可以嵌套,如((()))
题解:奇数括号,(的数量和)的数量不相等也不行,其余的记录“((”在“))”右边的即可。
#include
#include
#include
#include
#include
#include
#include
D. Present
Catherine received an array of integers as a gift for March 8. Eventually she grew bored with it, and she started calculated various useless characteristics for it. She succeeded to do it for each one she came up with. But when she came up with another one — xor of all pairwise sums of elements in the array, she realized that she couldn't compute it for a very large array, thus she asked for your help. Can you do it? Formally, you need to compute
(a1+a2)⊕(a1+a3)⊕…⊕(a1+an)⊕(a2+a3)⊕…⊕(a2+an)…⊕(an−1+an)(a1+a2)⊕(a1+a3)⊕…⊕(a1+an)⊕(a2+a3)⊕…⊕(a2+an)…⊕(an−1+an)
Here x⊕yx⊕y is a bitwise XOR operation (i.e. xx ^ yy in many modern programming languages). You can read about it in Wikipedia: https://en.wikipedia.org/wiki/Exclusive_or#Bitwise_operation.
Input
The first line contains a single integer nn (2≤n≤4000002≤n≤400000) — the number of integers in the array.
The second line contains integers a1,a2,…,ana1,a2,…,an (1≤ai≤1071≤ai≤107).
Output
Print a single integer — xor of all pairwise sums of integers in the given array.
Examples
input
Copy
2 1 2output
Copy
3input
Copy
3 1 2 3output
Copy
2Note
In the first sample case there is only one sum 1+2=31+2=3.
In the second sample case there are three sums: 1+2=31+2=3, 1+3=41+3=4, 2+3=52+3=5. In binary they are represented as 0112⊕1002⊕1012=01020112⊕1002⊕1012=0102, thus the answer is 2.
⊕⊕ is the bitwise xor operation. To define x⊕yx⊕y, consider binary representations of integers xx and yy. We put the ii-th bit of the result to be 1 when exactly one of the ii-th bits of xx and yy is 1. Otherwise, the ii-th bit of the result is put to be 0. For example, 01012⊕00112=0110201012⊕00112=01102.
题意:
题解:
#include
#include
#include
#include
#include
#include
#include