A - XORwice
In order to celebrate Twice’s 5th anniversary, Tzuyu and Sana decided to play a game.
Tzuyu gave Sana two integers a and b and a really important quest.
In order to complete the quest, Sana has to output the smallest possible value of (a⊕x) + (b⊕x) for any given x, where ⊕ denotes the bitwise XOR operation.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). Description of the test cases follows.
The only line of each test case contains two integers a and b (1≤a,b≤109).
Output
For each testcase, output the smallest possible value of the given expression.
Example
inputCopy
6
6 12
4 9
59 832
28 14
4925 2912
1 1
outputCopy
10
13
891
18
6237
0
Note
For the first test case Sana can choose x=4 and the value will be (6⊕4) + (12⊕4) = 2+8 = 10. It can be shown that this is the smallest possible value.
题意
x为任意值,求 (a⊕x) + (b⊕x) 最小值
思路
a,b对应位相同时,对应位可化为0;当a,b对应位不相同时,对应位必定为1,答案就是a与b的异或。
代码
#include <bits/stdc++.h>
typedef unsigned long long ll;
const ll mod = 9999999967;
using namespace std;
namespace fastIO {
inline void input(int& res) {
char c = getchar();res = 0;int f = 1;
while (!isdigit(c)) { f ^= c == '-'; c = getchar(); }
while (isdigit(c)) { res = (res << 3) + (res << 1) + (c ^ 48);c = getchar(); }
res = f ? res : -res;
}
inline ll qpow(ll a, ll b) {
ll ans = 1, base = a;
while (b) {
if (b & 1) ans = (ans * base % mod +mod )%mod;
base = (base * base % mod + mod)%mod;
b >>= 1;
}
return ans;
}
}
using namespace fastIO;
const int N = 3e5+5;
int Case,a,b,n;
int main(){
Case=1;
input(Case);
while(Case--){
input(a),input(b);
printf("%d\n",a^b);
}
return 0;
}
B - Putting Bricks in the Wall
Pink Floyd are pulling a prank on Roger Waters. They know he doesn’t like walls, he wants to be able to walk freely, so they are blocking him from exiting his room which can be seen as a grid.
Roger Waters has a square grid of size n×n and he wants to traverse his grid from the upper left (1,1) corner to the lower right corner (n,n). Waters can move from a square to any other square adjacent by a side, as long as he is still in the grid. Also except for the cells (1,1) and (n,n) every cell has a value 0 or 1 in it.
Before starting his traversal he will pick either a 0 or a 1 and will be able to only go to cells values in which are equal to the digit he chose. The starting and finishing cells (1,1) and (n,n) are exempt from this rule, he may go through them regardless of picked digit. Because of this the cell (1,1) takes value the letter ‘S’ and the cell (n,n) takes value the letter ‘F’.
For example, in the first example test case, he can go from (1,1) to (n,n) by using the zeroes on this path: (1,1), (2,1), (2,2), (2,3), (3,3), (3,4), (4,4)
The rest of the band (Pink Floyd) wants Waters to not be able to do his traversal, so while he is not looking they will invert at most two cells in the grid (from 0 to 1 or vice versa). They are afraid they will not be quick enough and asked for your help in choosing the cells. Note that you cannot invert cells (1,1) and (n,n).
We can show that there always exists a solution for the given constraints.
Also note that Waters will pick his digit of the traversal after the band has changed his grid, so he must not be able to reach (n,n) no matter what digit he picks.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤50). Description of the test cases follows.
The first line of each test case contains one integers n (3≤n≤200).
The following n lines of each test case contain the binary grid, square (1,1) being colored in ‘S’ and square (n,n) being colored in ‘F’.
The sum of values of n doesn’t exceed 200.
Output
For each test case output on the first line an integer c (0≤c≤2) — the number of inverted cells.
In i-th of the following c lines, print the coordinates of the i-th cell you inverted. You may not invert the same cell twice. Note that you cannot invert cells (1,1) and (n,n).
Example
inputCopy
3
4
S010
0001
1000
111F
3
S10
101
01F
5
S0101
00000
01111
11111
0001F
outputCopy
1
3 4
2
1 2
2 1
0
Note
For the first test case, after inverting the cell, we get the following grid:
S010
0001
1001
111F
题意
一个地图除了起点(1,1)和终点(n,n),其他点非0即1,规定只能走数字一样的点,现在问你操作2次以内,使起点到不了终点。
思路
起点附近就两个点,终点也是,就4个点,分类讨论下进行。
代码
#include <bits/stdc++.h>
typedef unsigned long long ll;
const ll mod = 9999999967;
using namespace std;
namespace fastIO {
inline void input(int& res) {
char c = getchar();res = 0;int f = 1;
while (!isdigit(c)) { f ^= c == '-'; c = getchar(); }
while (isdigit(c)) { res = (res << 3) + (res << 1) + (c ^ 48);c = getchar(); }
res = f ? res : -res;
}
inline ll qpow(ll a, ll b) {
ll ans = 1, base = a;
while (b) {
if (b & 1) ans = (ans * base % mod +mod )%mod;
base = (base * base % mod + mod)%mod;
b >>= 1;
}
return ans;
}
}
using namespace fastIO;
const int N = 3e5+5;
int Case,a,b,n;
char s[205][205];
int main(){
Case=1;
input(Case);
while(Case--){
input(n);
for(int i=1;i<=n;i++){
scanf("%s",s[i]+1);
}
if(s[1][2]==s[2][1]&&s[n-1][n]==s[n][n-1]&&s[1][2]!=s[n-1][n]) puts("0");
else if(s[1][2]==s[2][1]&&s[n-1][n]==s[n][n-1]&&s[1][2]==s[n-1][n]){
puts("2");
puts("1 2");
puts("2 1");
}
else if(s[1][2]!=s[2][1]&&s[n-1][n]!=s[n][n-1]){
puts("2");
if(s[1][2]=='0'&&s[n-1][n]=='1') puts("1 2"),printf("%d %d\n",n-1,n);
else if(s[2][1]=='0'&&s[n-1][n]=='1') puts("2 1"),printf("%d %d\n",n-1,n);
else if(s[1][2]=='0'&&s[n][n-1]=='1') puts("1 2"),printf("%d %d\n",n,n-1);
else puts("2 1"),printf("%d %d\n",n,n-1);
}
else{
puts("1");
if(s[1][2]==s[2][1]){
if(s[1][2]=='0'){
if(s[n][n-1]=='0')
printf("%d %d\n",n,n-1);
else
printf("%d %d\n",n-1,n);
}
else{
if(s[n][n-1]=='1')
printf("%d %d\n",n,n-1);
else
printf("%d %d\n",n-1,n);
}
}
else{
if(s[n][n-1]=='0'){
if(s[1][2]=='0')
puts("1 2");
else
puts("2 1");
}
else{
if(s[1][2]=='1')
puts("1 2");
else
puts("2 1");
}
}
}
}
return 0;
}
C - Palindromifier
Ringo found a string s of length n in his yellow submarine. The string contains only lowercase letters from the English alphabet. As Ringo and his friends love palindromes, he would like to turn the string s into a palindrome by applying two types of operations to the string.
The first operation allows him to choose i (2≤i≤n−1) and to append the substring s2s3…si (i−1 characters) reversed to the front of s.
The second operation allows him to choose i (2≤i≤n−1) and to append the substring sisi+1…sn−1 (n−i characters) reversed to the end of s.
Note that characters in the string in this problem are indexed from 1.
For example suppose s=abcdef. If he performs the first operation with i=3 then he appends cb to the front of s and the result will be cbabcdef. Performing the second operation on the resulted string with i=5 will yield cbabcdefedc.
Your task is to help Ringo make the entire string a palindrome by applying any of the two operations (in total) at most 30 times. The length of the resulting palindrome must not exceed 106
It is guaranteed that under these constraints there always is a solution. Also note you do not have to minimize neither the number of operations applied, nor the length of the resulting string, but they have to fit into the constraints.
Input
The only line contains the string S (3≤|s|≤105) of lowercase letters from the English alphabet.
Output
The first line should contain k (0≤k≤30) — the number of operations performed.
Each of the following k lines should describe an operation in form L i or R i. L represents the first operation, R represents the second operation, i represents the index chosen.
The length of the resulting palindrome must not exceed 106.
Examples
inputCopy
abac
outputCopy
2
R 2
R 5
inputCopy
acccc
outputCopy
2
L 4
L 2
inputCopy
hannah
outputCopy
0
Note
For the first example the following operations are performed:
abac → abacab → abacaba
The second sample performs the following operations: acccc → cccacccc → ccccacccc
The third example is already a palindrome so no operations are required.
题意
有两种操作 L i 操作为 s2 到 si 倒转插入开头,R i 为 si 到 sn-1倒置插入结尾,i小于n,最多30次操作,使字符串成为一个回文串。
思路
我们先设字符串为abcde
我们先进行 L len-1变为 dcbabcde
我们自然会进行 R len-1 dcbabcdedcba
这时候我们思考怎么成为回文串,先考虑L 发现不可行,再考虑R 发现我们可以将dcb倒置后置 即R 2len-1,十分巧妙地找到了。
代码
#include <bits/stdc++.h>
typedef unsigned long long ll;
const ll mod = 9999999967;
using namespace std;
namespace fastIO {
inline void input(int& res) {
char c = getchar();res = 0;int f = 1;
while (!isdigit(c)) { f ^= c == '-'; c = getchar(); }
while (isdigit(c)) { res = (res << 3) + (res << 1) + (c ^ 48);c = getchar(); }
res = f ? res : -res;
}
inline ll qpow(ll a, ll b) {
ll ans = 1, base = a;
while (b) {
if (b & 1) ans = (ans * base % mod +mod )%mod;
base = (base * base % mod + mod)%mod;
b >>= 1;
}
return ans;
}
}
using namespace fastIO;
const int N = 3e5+5;
int Case,len;
string str;
int main(){
Case=1;
cin>>str;
len=str.size();
puts("3");
printf("L %d\nR %d\nR %d\n",len-1,len-1,2*len-1);
return 0;
}
D - Hexagons
Lindsey Buckingham told Stevie Nicks “Go your own way”. Nicks is now sad and wants to go away as quickly as possible, but she lives in a 2D hexagonal world.
Consider a hexagonal tiling of the plane as on the picture below.
Nicks wishes to go from the cell marked (0,0) to a certain cell given by the coordinates. She may go from a hexagon to any of its six neighbors you want, but there is a cost associated with each of them. The costs depend only on the direction in which you travel. Going from (0,0) to (1,1) will take the exact same cost as going from (−2,−1) to (−1,0). The costs are given in the input in the order c1, c2, c3, c4, c5, c6 as in the picture below.
Print the smallest cost of a path from the origin which has coordinates (0,0) to the given cell.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). Description of the test cases follows.
The first line of each test case contains two integers x and y (−109≤x,y≤109) representing the coordinates of the target hexagon.
The second line of each test case contains six integers c1, c2, c3, c4, c5, c6 (1≤c1,c2,c3,c4,c5,c6≤109) representing the six costs of the making one step in a particular direction (refer to the picture above to see which edge is for each value).
Output
For each testcase output the smallest cost of a path from the origin to the given cell.
Example
inputCopy
2
-3 1
1 3 5 7 9 11
1000000000 1000000000
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
outputCopy
18
1000000000000000000
Note
The picture below shows the solution for the first sample. The cost 18 is reached by taking c3 3 times and c2 once, amounting to 5+5+5+3=18.
题意
六边形蜂窝状,六个方向代价大小不同,求从(0,0)走到(x,y)最小代价。
思路
贪心加向量分解求出来每个方向的最小花费
代码
#include <bits/stdc++.h>
typedef long long ll;
const ll mod = 9999999967;
using namespace std;
namespace fastIO {
inline void input(ll& res) {
char c = getchar();res = 0;int f = 1;
while (!isdigit(c)) { f ^= c == '-'; c = getchar(); }
while (isdigit(c)) { res = (res << 3) + (res << 1) + (c ^ 48);c = getchar(); }
res = f ? res : -res;
}
inline ll qpow(ll a, ll b) {
ll ans = 1, base = a;
while (b) {
if (b & 1) ans = (ans * base % mod +mod )%mod;
base = (base * base % mod + mod)%mod;
b >>= 1;
}
return ans;
}
}
using namespace fastIO;
const int N = 3e5+5;
ll Case,x,y,ans,y1,y2;
ll c[10];
int main(){
Case=1;
input(Case);
while(Case--){
input(x),input(y);
for(int i=1;i<=6;i++) input(c[i]);
c[1]=min(c[1],c[2]+c[6]);
c[2]=min(c[2],c[1]+c[3]);
c[3]=min(c[3],c[2]+c[4]);
c[4]=min(c[4],c[3]+c[5]);
c[5]=min(c[5],c[4]+c[6]);
c[6]=min(c[6],c[1]+c[5]);
if(x>=0&&y>=0)
if(x>y) ans=c[1]*abs(y)+c[6]*abs(x-y);
else ans=c[1]*abs(x)+c[2]*abs(y-x);
else if(x<=0&&y<=0)
if(x>y) ans=c[4]*abs(x)+c[5]*abs(x-y);
else ans=c[4]*abs(y)+c[3]*abs(y-x);
else if(x<=0&&y>=0)
ans=c[3]*abs(x)+c[2]*abs(y);
else
ans=c[6]*abs(x)+c[5]*abs(y);
printf("%lld\n",ans);
}
return 0;
}