-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingle file part 2
130 lines (113 loc) · 3.03 KB
/
Single file part 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
Problem
Chef has the binary representation SS of a number XX with him. He can modify the number by applying the following operation exactly once:
Make X := X \oplus \lfloor \frac{X}{2^{Y}} \rfloorX:=X⊕⌊
2
Y
X
⌋, where (1 \leq Y \leq |S|)(1≤Y≤∣S∣) and \oplus⊕ denotes the bitwise XOR operation.
Chef wants to minimize the value of XX after performing the operation. Help Chef in determining the value of YY which will minimize the value of XX after the operation.
Input Format
The first line of input will contain a single integer TT, denoting the number of test cases.
Each test case consists of two lines of inputs:
The first line contains the length of the binary string SS.
The second line contains the binary string SS.
Output Format
For each test case, output on a new line, the value of YY which will minimize the value of XX after the operation.
Constraints
1 \leq T \leq 5 \cdot 10^41≤T≤5⋅10
4
1 \leq |S| \leq 10^51≤∣S∣≤10
5
The sum of |S|∣S∣ over all test cases won't exceed 5 \cdot 10^55⋅10
5
.
SS contains the characters 00 and 11 only.
Sample 1:
Input
Output
4
2
10
2
11
3
101
3
110
2
1
2
1
Explanation:
Test case 11: Since S = 10S=10 is the binary representation of 22, the current value of X = 2X=2. On choosing Y = 2Y=2, XX becomes 2 \oplus \lfloor \frac{2}{2^{2}} \rfloor = 22⊕⌊
2
2
2
⌋=2. We can show that this is the minimum value of XX we can achieve after one operation.
Test case 22: Since S = 11S=11 is the binary representation of 33, the current value of X = 3X=3. On choosing Y = 1Y=1, XX becomes 3 \oplus \lfloor \frac{3}{2^{1}} \rfloor = 23⊕⌊
2
1
3
⌋=2. We can show that this is the minimum value of XX we can achieve after one operation.
Test case 33: Since S = 101S=101 is the binary representation of 55, the current value of X = 5X=5. On choosing Y = 2Y=2, XX becomes 5 \oplus \lfloor \frac{5}{2^{2}} \rfloor = 45⊕⌊
2
2
5
⌋=4. We can show that this is the minimum value of XX we can achieve after one operation.
Test case 44: Since S = 110S=110 is the binary representation of 66, the current value of X = 6X=6. On choosing Y = 1Y=1, XX becomes 6 \oplus \lfloor \frac{6}{2^{1}} \rfloor = 56⊕⌊
2
1
6
⌋=5. We can show that this is the minimum value of XX we can achieve after one operation.
accepted
Accepted
90
total-Submissions
Submissions
383
accuracy
Accuracy
25.33
answer:
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
string s;
cin>>s;
int value = 0;
int count = 0;
for(int i = s.length()-1;i>=0;i--)
{
if(s[i]=='1')
{
value+=pow(2,count);
}
count++;
}
int mx = INT_MAX;
int y = 0;
for(int i = 1; i<=s.length();i++)
{
int temp = (value/(pow(2,i)));
temp = value^temp;
if(temp<mx)
{
mx=temp;
y=i;
}
}
cout<<y<<endl;
}
return 0;
}