1328A - Divisibility Problem[思维]
1328B - K-th Beautiful String[思维]
1328C - Ternary XOR[思维]
1328D - Carousel[ ]
1328E - Tree Queries[ ]
1328F - Make k Equal[ ]
1328A - Divisibility Problem[思维]
time limit per test | memory limit per test | input | output |
---|---|---|---|
1 seconds | 256 megabytes | standard input | standard output |
Description:
You are given two positive integers a a a and b b b. In one move you can increase a a a by 1 1 1 (replace a with a + 1 a+1 a+1). Your task is to find the minimum number of moves you need to do in order to make a a a divisible by b b b. It is possible, that you have to make 0 0 0 moves, as a a a is already divisible by b b b. You have to answer t t t independent test cases.
Input
The first line of the input contains one integer t ( 1 ≤ t ≤ 1 0 4 ) t (1≤t≤10^4) t(1≤t≤104) — the number of test cases. Then t test cases follow.
The only line of the test case contains two integers a a a and b ( 1 ≤ a , b ≤ 1 0 9 ) b (1≤a,b≤10^9) b(1≤a,b≤109).
Output
For each test case print the answer — the minimum number of moves you need to do in order to make a a a divisible by b b b.
Example input
5
10 4
13 9
100 13
123 456
92 46
Example output
2
5
4
333
0
分析:
题意:
问
a
a
a移动至可以被
b
b
b整除的数至少要移动多少
即
a
%
b
=
=
0
a\%b==0
a%b==0
做法:
如代码所示
Code:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const int inf = 0x3f3f3f3f;
int re() {
int x = 0, f = 1; char c = getchar();
while(c > '9' || c < '0'){if(c == '-')f=-f; c = getchar();}
while(c >= '0' && c <= '9'){x=(x<<1)+(x<<3)+c-'0'; c = getchar();}
return x * f;
}
int main() {
int T;
scanf("%d", &T);
while(T--) {
int a, b;
scanf("%d%d", &a, &b);
printf("%d\n", ((b - (a % b)) + b) % b);
}
return 0;
}
1328B - K-th Beautiful String[思维]
time limit per test | memory limit per test | input | output |
---|---|---|---|
1 seconds | 256 megabytes | standard input | standard output |
Description:
For the given integer n ( n > 2 ) n (n>2) n(n>2) let’s write down all the strings of length n n n which contain n − 2 n−2 n−2 letters ‘a’ and two letters ‘b’ in lexicographical (alphabetical) order.
Recall that the string s s s of length n n n is lexicographically less than string t t t of length n n n, if there exists such i ( 1 ≤ i ≤ n ) i (1≤i≤n) i(1≤i≤n), that s i < t i s_i<t_i si<ti, and for any j ( 1 ≤ j < i ) s j = t j j (1≤j<i) s_j=t_j j(1≤j<i)sj=tj. The lexicographic comparison of strings is implemented by the operator < in modern programming languages.
For example, if n = 5 n=5 n=5 the strings are (the order does matter):
aaabb
aabab
aabba
abaab
ababa
abbaa
baaab
baaba
babaa
bbaaa
It is easy to show that such a list of strings will contain exactly n ⋅ ( n − 1 ) 2 \frac{n⋅(n−1)}{2} 2n⋅(n−1) strings.
You are given n ( n > 2 ) n (n>2) n(n>2) and k ( 1 ≤ k ≤ n ⋅ ( n − 1 ) 2 ) k (1≤k≤\frac{n⋅(n−1)}{2}) k(1≤k≤2n⋅(n−1)). Print the k-th string from the list.
Input
The input contains one or more test cases.
The first line contains one integer t ( 1 ≤ t ≤ 1 0 4 ) t (1≤t≤10^4) t(1≤t≤104) — the number of test cases in the test. Then t test cases follow.
Each test case is written on the the separate line containing two integers n n n and k ( 3 ≤ n ≤ 1 0 5 , 1 ≤ k ≤ m i n ( 2 ⋅ 1 0 9 , n ⋅ ( n − 1 ) 2 ) k (3≤n≤10^5,1≤k≤min(2⋅10^9,\frac{n⋅(n−1)}{2}) k(3≤n≤105,1≤k≤min(2⋅109,2n⋅(n−1)).
The sum of values n n n over all test cases in the test doesn’t exceed 1 0 5 10^5 105.
Output
For each test case print the k k k-th string from the list of all described above strings of length n n n. Strings in the list are sorted lexicographically (alphabetically).
Example input
7
5 1
5 2
5 8
5 10
3 1
3 2
20 100
Example output
aaabb
aabab
baaba
bbaaa
abb
bab
aaaaabaaaaabaaaaaaaa
分析:
题意:
给一个数,求长度为
n
n
n的串,字典序第
k
k
k小的串应该是什么,
且这个串必有2个
b
b
b,其他都是
a
a
a
做法:
第
k
k
k小后面部分其实是固定的,前面多余的部分都是
′
a
′
'a'
′a′,
假设现在
n
=
5
n=5
n=5
1
:
a
a
a
b
b
1: aaabb
1:aaabb
2
:
a
a
b
a
b
2: aabab
2:aabab
3
:
a
a
b
b
a
3: aabba
3:aabba
4
:
a
b
a
a
b
4: abaab
4:abaab
5
:
a
b
a
b
a
5: ababa
5:ababa
6
:
a
b
b
a
a
6: abbaa
6:abbaa
7
:
b
a
a
a
b
7: baaab
7:baaab
可以发现,在比较前面的
b
b
b的位置(这边从右往左从0开始)
为
2
2
2时,
k
≤
1
k \leq 1
k≤1
为
3
3
3时,
k
≤
3
k \leq 3
k≤3
为
4
4
4时,
k
≤
6
k \leq 6
k≤6
即
1
+
2
+
.
.
.
+
(
p
o
s
−
1
)
≤
k
1+2+...+(pos-1) \leq k
1+2+...+(pos−1)≤k,
p
o
s
pos
pos即第一个
b
b
b要填的位置
第二个位置即
k
−
s
u
m
(
1
+
2
+
.
.
.
+
p
o
s
−
1
)
−
1
k - sum(1+2+...+pos-1) - 1
k−sum(1+2+...+pos−1)−1
其他位置填
′
a
′
'a'
′a′,注意这里我是反着填的
所以输出要反着
Code:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5;
const int inf = 0x3f3f3f3f;
int re() {
int x = 0, f = 1; char c = getchar();
while(c > '9' || c < '0'){if(c == '-')f=-f; c = getchar();}
while(c >= '0' && c <= '9'){x=(x<<1)+(x<<3)+c-'0'; c = getchar();}
return x * f;
}
char s[maxn];
int main() {
int T;
scanf("%d", &T);
while(T--) {
int n, pos; ll k, ans;
scanf("%d%lld", &n, &k);
ans = 0, pos = 0;
for(int i = 0; i <= n; ++i) s[i] = 'a';
while(ans < k) pos += 1, ans = ans + pos * 1ll;
ans -= pos;
// printf("%lld %d %lld %lld\n", ans, pos, k, (k - ans));
s[pos] = 'b', s[(int)(k - ans - 1)] = 'b';
for(int i = n - 1; i >= 0; --i)
printf("%c", s[i]);
puts("");
}
return 0;
}
1328C - Ternary XOR[思维]
time limit per test | memory limit per test | input | output |
---|---|---|---|
1 seconds | 256 megabytes | standard input | standard output |
Description:
A number is ternary if it contains only digits 0 , 1 0, 1 0,1 and 2 2 2. For example, the following numbers are ternary: 1022 , 11 , 21 , 2002 1022, 11, 21, 2002 1022,11,21,2002.
You are given a long ternary number x x x. The first (leftmost) digit of x x x is guaranteed to be 2 2 2, the other digits of x x x can be 0 , 1 0, 1 0,1 or 2 2 2.
Let’s define the ternary XOR operation ⊙ ⊙ ⊙ of two ternary numbers a a a and b b b (both of length n n n) as a number c = a ⊙ b c=a⊙b c=a⊙b of length n n n, where c i = ( a i + b i ) % 3 c_i=(a_i+b_i)\%3 ci=(ai+bi)%3 (where % \% % is modulo operation). In other words, add the corresponding digits and take the remainders of the sums when divided by 3 3 3. For example, 10222 ⊙ 11021 = 21210 10222⊙11021=21210 10222⊙11021=21210.
Your task is to find such ternary numbers a a a and b b b both of length n n n and both without leading zeros that a ⊙ b = x a⊙b=x a⊙b=x and m a x ( a , b ) max(a,b) max(a,b) is the minimum possible.
You have to answer t t t independent test cases.
Input
The first line of the input contains one integer t ( 1 ≤ t ≤ 1 0 4 ) t (1≤t≤10^4) t(1≤t≤104) — the number of test cases. Then t test cases follow. The first line of the test case contains one integer n ( 1 ≤ n ≤ 5 ⋅ 1 0 4 ) n (1≤n≤5⋅10^4) n(1≤n≤5⋅104) — the length of x x x. The second line of the test case contains ternary number x x x consisting of n n n digits 0 , 1 0,1 0,1 or 2 2 2. It is guaranteed that the first digit of x x x is 2 2 2. It is guaranteed that the sum of n over all test cases does not exceed 5 ⋅ 1 0 4 ( ∑ n ≤ 5 ⋅ 1 0 4 ) 5⋅10^4 (\sum n≤5⋅10^4) 5⋅104(∑n≤5⋅104).
Output
For each test case, print the answer — two ternary integers a a a and b b b both of length n and both without leading zeros such that a ⊙ b = x a⊙b=x a⊙b=x and m a x ( a , b ) max(a,b) max(a,b) is the minimum possible. If there are several answers, you can print any.
Intput
4
5
22222
5
21211
1
2
9
220222021
Output
11111
11111
11000
10211
1
1
110111011
110111010
分析:
题意:
现有一个只有
0
,
1
,
2
0, 1, 2
0,1,2的串
定义
a
⊙
b
=
(
a
+
b
)
(
m
o
d
3
)
a ⊙ b = (a + b) \pmod 3
a⊙b=(a+b)(mod3)
要把这个串分为两个数字串,使得
a
⊙
b
=
x
a ⊙ b = x
a⊙b=x,且
m
a
x
(
a
,
b
)
max(a,b)
max(a,b)最小
分析:
显然只需要分别把
0
→
0
0 \rightarrow 0
0→0,
1
→
0
+
1
1 \rightarrow 0+1
1→0+1,
2
→
(
1
+
1
)
o
r
(
0
+
2
)
2 \rightarrow (1+1) or (0+2)
2→(1+1)or(0+2),
如果还未第一个遇到
1
1
1,但遇到
2
2
2,那么肯定是平分使得两个数都比较小
(
1
+
1
)
(1+1)
(1+1)
如果已经遇到过第一个
1
1
1,那么不管之后遇到的是
1
o
r
2
1or2
1or2,都扔到第一个
1
1
1没放的那个数上
如果当前遇到第一个
1
1
1,任意选一个数放当前的
1
1
1,并且标记是哪个数
Code:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5e4 + 5;
const int inf = 0x3f3f3f3f;
int re() {
int x = 0, f = 1; char c = getchar();
while(c > '9' || c < '0'){if(c == '-')f=-f; c = getchar();}
while(c >= '0' && c <= '9'){x=(x<<1)+(x<<3)+c-'0'; c = getchar();}
return x * f;
}
char s[maxn];
char st[2][maxn];
int main() {
int T;
scanf("%d", &T);
while(T--) {
int n, f1 = 0;
scanf("%d%s", &n, s);
st[1][n] = st[0][n] = 0;
for(int i = 0; i < n; ++i) {
if(s[i] == '0') st[0][i] = st[1][i] = '0';
else if(s[i] == '1') {
if(f1 == 0)
st[0][i] = '1', st[1][i] = '0', f1 = 1;
else
st[1][i] = '1', st[0][i] = '0';
}
else if(s[i] == '2') {
if(f1 == 0)
st[0][i] = st[1][i] = '1';
else
st[1][i] = '2', st[0][i] = '0';
}
}
int id1 = 0, id2 = 0;
while(id1 < n && st[0][id1] == '0') id1 += 1;
while(id2 < n && st[1][id2] == '0') id2 += 1;
if(id1 == n) puts("0");
else printf("%s\n", st[0]);
if(id2 == n) puts("0");
else printf("%s\n", st[1]);
}
return 0;
}