A. Divisibility Problem
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given two positive integers a and b. In one move you can increase a by 1 (replace a with a+1). Your task is to find the minimum number of moves you need to do in order to make a divisible by b. It is possible, that you have to make 0 moves, as a is already divisible by b. You have to answer t independent test cases.
Input
The first line of the input contains one integer 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 and 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 divisible by b.
Example
inputCopy
5
10 4
13 9
100 13
123 456
92 46
outputCopy
2
5
4
333
0
题意:给出a 和 b 问最少几次a = a + 1的操作 使得 a%b == 0
思路:取模就好
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pb(x) push_back(x)
#define debug(x) cout<<"..........."<<x<<endl;
#define fi first
#define se second
const int N = 205;
const int M = 1e3+5;
const int mod = 1e9+7;
const int INF = 0x3f3f3f3f;
int main()
{
int t;
cin >> t;
while(t -- )
{
int a,b;
cin >> a >> b;
if(a > b)
{
if(a%b == 0)
cout << 0 ;
else
cout << b - a%b ;
}
else
{
cout << b - a ;
}
cout << '\n';
}
return 0;
}
B. K-th Beautiful String
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
For the given integer n (n>2) let’s write down all the strings of length n which contain n−2 letters ‘a’ and two letters ‘b’ in lexicographical (alphabetical) order.
Recall that the string s of length n is lexicographically less than string t of length n, if there exists such i (1≤i≤n), that si<ti, and for any 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 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 strings.
You are given n (n>2) and k (1≤k≤n⋅(n−1)2). 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≤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 and k (3≤n≤105,1≤k≤min(2⋅109,n⋅(n−1)2).
The sum of values n over all test cases in the test doesn’t exceed 105.
Output
For each test case print the k-th string from the list of all described above strings of length n. Strings in the list are sorted lexicographically (alphabetically).
Example
inputCopy
7
5 1
5 2
5 8
5 10
3 1
3 2
20 100
outputCopy
aaabb
aabab
baaba
bbaaa
abb
bab
aaaaabaaaaabaaaaaaaa
题意:给出长度为n的字符串 其中n-2 个 字符a 和 2 个字符b 按照字典序排列 求第k大字符串
思路:以第一个b的位置分类 发现每类会有1 2 3 … 种 是个等差数列
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pb(x) push_back(x)
#define debug(x) cout<<"..........."<<x<<endl;
#define fi first
#define se second
const int N = 205;
const int M = 1e3+5;
const int mod = 1e9+7;
const int INF = 0x3f3f3f3f;
int main()
{
int t;
cin >> t;
while(t -- )
{
int n,k;
cin >> n >> k;
int pos = 1;
while(k > pos)
k -= pos,pos ++ ;
pos ++ ;
for(int i = n;i >= 1;i --)
printf("%c",(i == pos || i == k)?'b':'a');
cout << '\n';
}
return 0;
}
C. Ternary XOR
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
A number is ternary if it contains only digits 0, 1 and 2. For example, the following numbers are ternary: 1022, 11, 21, 2002.
You are given a long ternary number x. The first (leftmost) digit of x is guaranteed to be 2, the other digits of x can be 0, 1 or 2.
Let’s define the ternary XOR operation ⊙ of two ternary numbers a and b (both of length n) as a number c=a⊙b of length n, where 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. For example, 10222⊙11021=21210.
Your task is to find such ternary numbers a and b both of length n and both without leading zeros that a⊙b=x and max(a,b) is the minimum possible.
You have to answer t independent test cases.
Input
The first line of the input contains one integer 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⋅104) — the length of x. The second line of the test case contains ternary number x consisting of n digits 0,1 or 2. It is guaranteed that the first digit of x is 2. It is guaranteed that the sum of n over all test cases does not exceed 5⋅104 (∑n≤5⋅104).
Output
For each test case, print the answer — two ternary integers a and b both of length n and both without leading zeros such that a⊙b=x and max(a,b) is the minimum possible. If there are several answers, you can print any.
Example
inputCopy
4
5
22222
5
21211
1
2
9
220222021
outputCopy
11111
11111
11000
10211
1
1
110111011
110111010
题意:长度为n的三进制数s 让你构造三进制数a + b = s 且 max(a,b)的值最小
思路:很容易想到贪心 一开始没有啥特殊情况2和0 都用来平分就行 当1出现后 将1给到a 0给到b 后面所有的数都一边倒向b即可(从二进制角度思考前一位的值 比其后面所有位的值加起来还大)可以得到贪心是正确的
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define pb(x) push_back(x)
#define debug(x) cout<<"..........."<<x<<endl;
#define fi first
#define se second
const int N = 205;
const int M = 1e3+5;
const int mod = 1e9+7;
const int INF = 0x3f3f3f3f;
int main()
{
int t;
cin >> t;
while(t -- )
{
string a,b;
a = b = "";
string s;
int n;
cin >> n;
cin >> s;
bool flag = 0;
for(int i = 0; i < n; i ++)
{
if(!flag)
{
if(s[i] == '2')
{
a += '1';
b += '1';
}
if(s[i] == '1')
{
a += '1';
b += '0';
flag = 1;
}
}
else
{
if(s[i] == '2')
{
a += '0';
b += '2';
}
if(s[i] == '1')
{
a += '0';
b += '1';
}
}
if(s[i] == '0')
{
a += '0';
b += '0';
}
}
cout << a << '\n' << b << '\n';
}
return 0;
}