Codeforces Round #739 (Div. 3)(http://codeforces.com/contest/1560)
A. Dislike of Threes(http://codeforces.com/contest/1560/problem/A)
Polycarp doesn’t like integers that are divisible by 3 or end with the digit 3 in their decimal representation. Integers that meet both conditions are disliked by Polycarp, too.
Polycarp starts to write out the positive (greater than 0) integers which he likes: 1,2,4,5,7,8,10,11,14,16,…. Output the k-th element of this sequence (the elements are numbered from 1).
Input
The first line contains one integer t (1≤t≤100) — the number of test cases. Then t test cases follow.
Each test case consists of one line containing one integer k (1≤k≤1000).
Output
For each test case, output in a separate line one integer x — the k-th element of the sequence that was written out by Polycarp.
Example
inputCopy
10
1
2
3
4
5
6
7
8
9
1000
outputCopy
1
2
4
5
7
8
10
11
14
1666
题意:将自然数中尾部带3或者是3的倍数的数剔除得到这样一个序列:1,2,4,5,7,8,10,11,14,16,…要求输出该序列中第k个数
数据范围很小,打个表就行
#include <bits/stdc++.h>
using namespace std;
#define reset(x) memset(x, 0, sizeof(x))
#define Q_in_out \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> P;
#define forl(l,r) for(int i=l;i<r;i++)
#define forr(r,l) for(int i=r;i>=l;i--)
const int N = 1e4+5;
const int modp = 1e9+7;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
const double pi = acos(-1.0);
const double e = 2.718281828459045;
int tab[10005];
bool jg(int n){
if((n%10)==3) return true;
return false;
}
void init(){
int cnt=1;
for(int i=1;i<10005;i++){
if(i%3==0||jg(i)) continue;
else tab[cnt++]=i;
}
}
int solve()
{
int n;
cin>>n;
cout<<tab[n];
return 0;
}
int main()
{
init();
Q_in_out;
int t;
t = 1;
cin >> t;
while (t--)
{
solve();
cout<<endl;
}
return 0;
}
B. Who’s Opposite?(http://codeforces.com/contest/1560/problem/B)
Some number of people (this number is even) have stood in a circle. The people stand in the circle evenly. They are numbered clockwise starting from a person with the number 1. Each person is looking through the circle’s center at the opposite person.
A sample of a circle of 6 persons. The orange arrows indicate who is looking at whom.
You don’t know the exact number of people standing in the circle (but this number is even, no doubt). It is known that the person with the number a is looking at the person with the number b (and vice versa, of course). What is the number associated with a person being looked at by the person with the number c? If, for the specified a, b, and c, no such circle exists, output -1.
Input
The first line contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.
Each test case consists of one line containing three distinct integers a, b, c (1≤a,b,c≤108).
Output
For each test case output in a separate line a single integer d — the number of the person being looked at by the person with the number c in a circle such that the person with the number a is looking at the person with the number b. If there are multiple solutions, print any of them. Output −1 if there’s no circle meeting the given conditions.
Example
inputCopy
7
6 2 4
2 3 1
2 4 10
5 3 4
1 3 2
2 5 4
4 3 2
outputCopy
8
-1
-1
-1
4
1
-1
Note
In the first test case, there’s a desired circle of 8 people. The person with the number 6 will look at the person with the number 2 and the person with the number 8 will look at the person with the number 4.
In the second test case, there’s no circle meeting the conditions. If the person with the number 2 is looking at the person with the number 3, the circle consists of 2 people because these persons are neighbors. But, in this case, they must have the numbers 1 and 2, but it doesn’t meet the problem’s conditions.
In the third test case, the only circle with the persons with the numbers 2 and 4 looking at each other consists of 4 people. Therefore, the person with the number 10 doesn’t occur in the circle.
题意:n(n是偶数)个数,从1~n顺时针摆放,给出其中处于对称位置的一对数a,b(可能是不合法的一对数)和一个数c,根据a,b还原出c对称位置上的数,所谓对称,例如1~6,有1对4.2对5,3对6,若输入不合法输出-1.
给定a,b,假设这是某个圈中合法的一对数,那么可以根据ab,计算出n,即n=abs(a-b)*2,计算出n后我们需要验证题目所给出的a,b,c是否都小于等于n,若存在大于n的数那么这组a,b,c就无解。否则,和c配对的数就应该等于c+n/2;
AC代码:
#include <bits/stdc++.h>
using namespace std;
#define reset(x) memset(x, 0, sizeof(x))
#define Q_in_out \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> P;
#define forl(l,r) for(int i=l;i<r;i++)
#define forr(r,l) for(int i=r;i>=l;i--)
const int N = 1e5+5;
const int modp = 1e9+7;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
const double pi = acos(-1.0);
const double e = 2.718281828459045;
int solve()
{
int a,b,c;
cin>>a>>b>>c;
int n=abs(a-b)*2;
if(a>n||b>n||c>n){
cout<<-1;
}
else{
cout<<(c+n/2>n?c-n/2:c+n/2);
}
return 0;
}
int main()
{
Q_in_out;
int t;
t = 1;
cin >> t;
while (t--)
{
solve();
cout<<endl;
}
return 0;
}
C. Infinity Table(http://codeforces.com/contest/1560/problem/C)
Polycarp has found a table having an infinite number of rows and columns. The rows are numbered from 1, starting from the topmost one. The columns are numbered from 1, starting from the leftmost one.
Initially, the table hasn’t been filled and Polycarp wants to fix it. He writes integers from 1 and so on to the table as follows.
The figure shows the placement of the numbers from 1 to 10. The following actions are denoted by the arrows.
The leftmost topmost cell of the table is filled with the number 1. Then he writes in the table all positive integers beginning from 2 sequentially using the following algorithm.
First, Polycarp selects the leftmost non-filled cell in the first row and fills it. Then, while the left neighbor of the last filled cell is filled, he goes down and fills the next cell. So he goes down until the last filled cell has a non-filled neighbor to the left (look at the vertical arrow going down in the figure above).
After that, he fills the cells from the right to the left until he stops at the first column (look at the horizontal row in the figure above). Then Polycarp selects the leftmost non-filled cell in the first row, goes down, and so on.
A friend of Polycarp has a favorite number k. He wants to know which cell will contain the number. Help him to find the indices of the row and the column, such that the intersection of the row and the column is the cell containing the number k.
Input
The first line contains one integer t (1≤t≤100) — the number of test cases. Then t test cases follow.
Each test case consists of one line containing one integer k (1≤k≤109) which location must be found.
Output
For each test case, output in a separate line two integers r and c (r,c≥1) separated by spaces — the indices of the row and the column containing the cell filled by the number k, respectively.
Example
inputCopy
7
11
14
5
4
1
2
1000000000
outputCopy
2 4
4 3
1 3
2 1
1 1
1 2
31623 14130
题意:将数字1~n按照图中方式填充,给定一个数,求这个数在这个二维矩阵里的位置
纯属是找规律的题了,随便找几个样例推出公式就行
AC代码:
#include <bits/stdc++.h>
using namespace std;
#define reset(x) memset(x, 0, sizeof(x))
#define Q_in_out \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> P;
#define forl(l,r) for(int i=l;i<r;i++)
#define forr(r,l) for(int i=r;i>=l;i--)
const int N = 1e4+5;
const int modp = 1e9+7;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
const double pi = acos(-1.0);
const double e = 2.718281828459045;
int solve()
{
ll n;
cin>>n;
if((int)sqrt(n)*(int)sqrt(n)==n){
cout<<sqrt(n)<<' '<<1;
return 0;
}
ll t=(ll)sqrt(n)+1;
ll i=t,j=t;
if(n<=t*t-t){
i=t-(t*t-t+1-n);
}
if(n>t*t-t+1){
j=t-(n-t*t+t-1);
}
cout<<i<<' '<<j;
return 0;
}
int main()
{
Q_in_out;
int t;
t = 1;
cin >> t;
while (t--)
{
solve();
cout<<endl;
}
return 0;
}
D. Make a Power of Two(http://codeforces.com/contest/1560/problem/D)
You are given an integer n. In 1 move, you can do one of the following actions:
erase any digit of the number (it’s acceptable that the number before the operation has exactly one digit and after the operation, it is “empty”);
add one digit to the right.
The actions may be performed in any order any number of times.
Note that if, after deleting some digit from a number, it will contain leading zeroes, they will not be deleted. E.g. if you delete from the number 301 the digit 3, the result is the number 01 (not 1).
You need to perform the minimum number of actions to make the number any power of 2 (i.e. there’s an integer k (k≥0) such that the resulting number is equal to 2k). The resulting number must not have leading zeroes.
E.g. consider n=1052. The answer is equal to 2. First, let’s add to the right one digit 4 (the result will be 10524). Then let’s erase the digit 5, so the result will be 1024 which is a power of 2.
E.g. consider n=8888. The answer is equal to 3. Let’s erase any of the digits 8 three times. The result will be 8 which is a power of 2.
Input
The first line contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.
Each test case consists of one line containing one integer n (1≤n≤109).
Output
For each test case, output in a separate line one integer m — the minimum number of moves to transform the number into any power of 2.
Example
inputCopy
12
1052
8888
6
75
128
1
301
12048
1504
6656
1000000000
687194767
outputCopy
2
3
1
3
0
0
2
1
3
4
9
2
Note
The answer for the first test case was considered above.
The answer for the second test case was considered above.
In the third test case, it’s enough to add to the right the digit 4 — the number 6 will turn into 64.
In the fourth test case, let’s add to the right the digit 8 and then erase 7 and 5 — the taken number will turn into 8.
The numbers of the fifth and the sixth test cases are already powers of two so there’s no need to make any move.
In the seventh test case, you can delete first of all the digit 3 (the result is 01) and then the digit 0 (the result is 1).
给定一个数,通过删掉这个数的某几位或者增加某几位数字使得新组成的数字值是2的k次幂,k>=0,问给定数字最少需要操作几次。
我们先将2的1~60次方运算结果存入一张表,对于输入的每一个数,我们遍历表中60个数,不断更新变换到表中的数所需要的步数,最终得到结果
AC代码:
#include <bits/stdc++.h>
using namespace std;
#define reset(x) memset(x, 0, sizeof(x))
#define Q_in_out \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> P;
#define forl(l,r) for(int i=l;i<r;i++)
#define forr(r,l) for(int i=r;i>=l;i--)
const int N = 1e4+5;
const int modp = 1e9+7;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
const double pi = acos(-1.0);
const double e = 2.718281828459045;
string tab[61];
string change(ll i){
string res;
while (i)
{
res.push_back(i%10+'0');
i/=10;
}
reverse(res.begin(),res.end());
return res;
}
int init(){
for(int j=0;j<=60;j++){
tab[j]=change((ll)1<<j);
}
return 0;
}
int cal(string t,string s){
int cnt=0;
int i=0,j=0;
while (i<t.size()&&j<s.size())
{
if(t[i]==s[j]){
i++;j++;
}
else{
cnt++;
j++;
}
}
return cnt+t.size()-i+s.size()-j;
}
int solve()
{
string s;
cin>>s;
int res=inf;
for(int i=0;i<=60;i++)
res=min(res,cal(tab[i],s));
cout<<res;
return 0;
}
int main()
{
init();
Q_in_out;
int t;
t = 1;
cin >> t;
while (t--)
{
solve();
cout<<endl;
}
return 0;
}
E. Polycarp and String Transformation(http://codeforces.com/contest/1560/problem/E)
Polycarp has a string s. Polycarp performs the following actions until the string s is empty (t is initially an empty string):
he adds to the right to the string t the string s, i.e. he does t=t+s, where t+s is a concatenation of the strings t and s;
he selects an arbitrary letter of s and removes from s all its occurrences (the selected letter must occur in the string s at the moment of performing this action).
Polycarp performs this sequence of actions strictly in this order.
Note that after Polycarp finishes the actions, the string s will be empty and the string t will be equal to some value (that is undefined and depends on the order of removing).
E.g. consider s=“abacaba” so the actions may be performed as follows:
t=“abacaba”, the letter ‘b’ is selected, then s=“aacaa”;
t=“abacabaaacaa”, the letter ‘a’ is selected, then s=“c”;
t=“abacabaaacaac”, the letter ‘c’ is selected, then s="" (the empty string).
You need to restore the initial value of the string s using only the final value of t and find the order of removing letters from s.
Input
The first line contains one integer T (1≤T≤104) — the number of test cases. Then T test cases follow.
Each test case contains one string t consisting of lowercase letters of the Latin alphabet. The length of t doesn’t exceed 5⋅105. The sum of lengths of all strings t in the test cases doesn’t exceed 5⋅105.
Output
For each test case output in a separate line:
−1, if the answer doesn’t exist;
two strings separated by spaces. The first one must contain a possible initial value of s. The second one must contain a sequence of letters — it’s in what order one needs to remove letters from s to make the string t. E.g. if the string “bac” is outputted, then, first, all occurrences of the letter ‘b’ were deleted, then all occurrences of ‘a’, and then, finally, all occurrences of ‘c’. If there are multiple solutions, print any one.
Example
inputCopy
7
abacabaaacaac
nowyouknowthat
polycarppoycarppoyarppyarppyrpprppp
isi
everywherevrywhrvryhrvrhrvhv
haaha
qweqeewew
outputCopy
abacaba bac
-1
polycarp lcoayrp
is si
everywhere ewyrhv
-1
-1
Note
The first test case is considered in the statement.
对字符串s每次删除其中某种字符,并将删除后的结果加入t的结尾,现给定t,问能否还原出s,并给出删除s中字符的顺序,若无解输出-1
根据题意,容易得知t中最后一个字符肯定是s最后删掉的一个字符,依次类推,我们从t的尾部往前遍历,得到所有字符出现的顺序(删除顺序的逆序),得到删除顺序后,我们还需要还原出s原本的长度,我们知道,在删除某个字母x之前假设删除了其他k个字母,假设s中有m个x,那么s中的x会在t中重复km次,因此我们可以统计出t中每一种字符出现次数,再去遍历删除顺序,用字符出现次数除以该字符在删除顺序的位置就可以得到s中该字符的数量。在知道s的长度之后,我们就可以在t中得到我们的s。得到s后我们还需模拟一遍整个过程,并且判断最终生成的字符串是否等于t,若相等输出s和删除顺序,若不等输出-1
AC代码:
#include <bits/stdc++.h>
using namespace std;
#define reset(x) memset(x, 0, sizeof(x))
#define Q_in_out \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> P;
#define forl(l,r) for(int i=l;i<r;i++)
#define forr(r,l) for(int i=r;i>=l;i--)
const int N = 1e5+5;
const int modp = 1e9+7;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
const double pi = acos(-1.0);
const double e = 2.718281828459045;
int solve()
{
string t;
cin>>t;
reverse(t.begin(),t.end());
int cnt[26]={0};
string dele;
for(auto &v:t){
if(cnt[v-'a']==0)
dele+=v;
cnt[v-'a']++;
}
reverse(dele.begin(),dele.end()); //dele记录删除顺序
int len=0,f=0;
for(int i=0;i<dele.size();i++){
if(cnt[dele[i]-'a']%(i+1)!=0) {
f=1;
break;
}
len+=cnt[dele[i]-'a']/(i+1); //源串s的长度
}
if(f) {
cout<<-1;
return 0;
}
reverse(t.begin(),t.end());
string s=t.substr(0,len);
string ck;
for(auto &v:dele) //模拟一遍删除过程,看最终结果和t是否相等
{
ck+=s;
s.erase(remove(s.begin(),s.end(),v),s.end()); //删除所有v
}
if(ck==t){
cout<<t.substr(0,len)<<' '<<dele;
}
else cout<<-1;
return 0;
}
int main()
{
Q_in_out;
int t;
t = 1;
cin >> t;
while (t--)
{
solve();
cout<<endl;
}
return 0;
}
F1. Nearest Beautiful Number (easy version)(http://codeforces.com/contest/1560/problem/F1)
It is a simplified version of problem F2. The difference between them is the constraints (F1: k≤2, F2: k≤10).
You are given an integer n. Find the minimum integer x such that x≥n and the number x is k-beautiful.
A number is called k-beautiful if its decimal representation having no leading zeroes contains no more than k different digits. E.g. if k=2, the numbers 3434443, 55550, 777 and 21 are k-beautiful whereas the numbers 120, 445435 and 998244353 are not.
Input
The first line contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.
Each test case consists of one line containing two integers n and k (1≤n≤109, 1≤k≤2).
Output
For each test case output on a separate line x — the minimum k-beautiful integer such that x≥n.
Example
inputCopy
4
1 1
221 2
177890 2
998244353 1
outputCopy
1
221
181111
999999999
题意,给定数字n和k,我们要找一个大于等于n的数,并且这个数由不超过k种数字组成,困难版本相较于简单版本的区别是困难版的k更大
这里给出一种不同于官方题解的大佬们的思路,并且通用于困难版本和简单版本:
考虑样例177890 2,我们可以统计出n中共有5种数字,超出2了,每次我们从高位往后找,找到第一个符合条件的数位,比如说177890,177是符合条件的,但是890不符合条件,于是我们试着让这个890变得更大一些,但我们不能每次给n加1,这样做会超时,我们统计2位最多到177时让8进位,也就是将177890变成177900,第二次操作让其变为178000,第三次让其变为179000,以此类推,得到180000,181000,181100,181110,181111,最终181111就是我们要的解
AC代码:(F1和F2均可以通过)
#include <bits/stdc++.h>
using namespace std;
#define reset(x) memset(x, 0, sizeof(x))
#define Q_in_out \
ios::sync_with_stdio(false); \
cin.tie(0); \
cout.tie(0);
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> P;
#define forl(l,r) for(int i=l;i<r;i++)
#define forr(r,l) for(int i=r;i>=l;i--)
const int N = 1e5+5;
const int modp = 1e9+7;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
const double pi = acos(-1.0);
const double e = 2.718281828459045;
// int diff(int x){ //用stl统计会超时
// map<int,int>cnt;
// while (x)
// {
// cnt[x%10]++;
// x/=10;
// }
// return cnt.size();
// }
int diff(int x){
bool vis[10]={0};
int cnt=0;
while (x)
{
if(!vis[x%10]){
vis[x%10]=1;
cnt++;
}
x/=10;
}
return cnt;
}
int solve()
{
int n,k;
cin>>n>>k;
while (diff(n)>k)
{
int l=1,r=n;
while(diff(r)>k){
l*=10;
r/=10;
}
l/=10;
n=((n/l)+1)*l;
}
cout<<n;
return 0;
}
int main()
{
Q_in_out;
int t;
t = 1;
cin >> t;
while (t--)
{
solve();
cout<<endl;
}
return 0;
}
F2. Nearest Beautiful Number (hard version)(http://codeforces.com/contest/1560/problem/F2)
解法见F1