Codeforces Round #629 (Div. 3)日常训练

Codeforces Round #629 (Div. 3)

A. Divisibility Problem

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given two positive integers aa and bb. In one move you can increase aa by 11 (replace aa with a+1a+1). Your task is to find the minimum number of moves you need to do in order to make aa divisible by bb. It is possible, that you have to make 00 moves, as aa is already divisible by bb. You have to answer tt independent test cases.

Input
The first line of the input contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases. Then tt test cases follow.

The only line of the test case contains two integers aa and bb (1≤a,b≤1091≤a,b≤109).

Output
For each test case print the answer — the minimum number of moves you need to do in order to make aa divisible by bb.

Example
inputCopy
5
10 4
13 9
100 13
123 456
92 46
outputCopy
2
5
4
333
0

思路

一开始想复杂了。。。
就是找离A最近的B的倍数

#include<iostream>
using namespace std;
long long a,b,i,j,k,t,ans;
int main(){
	cin>>t;
	while(t--){
		cin>>a>>b;
		ans=0;
		if(a<b) ans=b-a;
		else if(a%b==0) ans=0;
		else{
			i=a/b+1;
			ans=i*b-a;
		}
		cout<<ans<<endl;
	}
	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 nn (n>2n>2) let’s write down all the strings of length nn which contain n−2n−2 letters ‘a’ and two letters ‘b’ in lexicographical (alphabetical) order.

Recall that the string ss of length nn is lexicographically less than string tt of length nn, if there exists such ii (1≤i≤n1≤i≤n), that si<tisi<ti, and for any jj (1≤j<i1≤j<i) sj=tjsj=tj. The lexicographic comparison of strings is implemented by the operator < in modern programming languages.

For example, if n=5n=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)2n⋅(n−1)2 strings.

You are given nn (n>2n>2) and kk (1≤k≤n⋅(n−1)21≤k≤n⋅(n−1)2). Print the kk-th string from the list.

Input
The input contains one or more test cases.

The first line contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases in the test. Then tt test cases follow.

Each test case is written on the the separate line containing two integers nn and kk (3≤n≤105,1≤k≤min(2⋅109,n⋅(n−1)2)3≤n≤105,1≤k≤min(2⋅109,n⋅(n−1)2).

The sum of values nn over all test cases in the test doesn’t exceed 105105.

Output
For each test case print the kk-th string from the list of all described above strings of length nn. 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

思路

找规律吧,从1开始累加进位。分别求出两个1应该在的位数。

#include<iostream>
#define maxn 1008611
using namespace std;
long long n,j,k,m,i,t,N;
long long a[maxn];
int main(){
	cin>>t;
	a[0]=0;
	for(i=1;i<=maxn;i++) a[i]=a[i-1]+i;
	while(t--){
		cin>>N>>k;
		k=k-1;
		i=0;
		while(a[i]<k) i++;
		if (a[i]==k){
			n=i+1;
			m=0;
		} 
		else{
			n=i;
			m=k-a[n-1];
		}
		for(i=1;i<=N-n-1;i++)cout<<"a";
		cout<<"b";
		for(i=1;i<=n-m-1;i++)cout<<"a";
		cout<<"b";
		for(i=1;i<=m;i++) cout<<"a";
		cout<<endl;
	}
	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 00, 11 and 22. For example, the following numbers are ternary: 10221022, 1111, 2121, 20022002.

You are given a long ternary number xx. The first (leftmost) digit of xx is guaranteed to be 22, the other digits of xx can be 00, 11 or 22.

Let’s define the ternary XOR operation ⊙⊙ of two ternary numbers aa and bb (both of length nn) as a number c=a⊙bc=a⊙b of length nn, where ci=(ai+bi)%3ci=(ai+bi)%3 (where %% is modulo operation). In other words, add the corresponding digits and take the remainders of the sums when divided by 33. For example, 10222⊙11021=2121010222⊙11021=21210.

Your task is to find such ternary numbers aa and bb both of length nn and both without leading zeros that a⊙b=xa⊙b=x and max(a,b)max(a,b) is the minimum possible.

You have to answer tt independent test cases.

Input
The first line of the input contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases. Then tt test cases follow. The first line of the test case contains one integer nn (1≤n≤5⋅1041≤n≤5⋅104) — the length of xx. The second line of the test case contains ternary number xx consisting of nn digits 0,10,1 or 22. It is guaranteed that the first digit of xx is 22. It is guaranteed that the sum of nn over all test cases does not exceed 5⋅1045⋅104 (∑n≤5⋅104∑n≤5⋅104).

Output
For each test case, print the answer — two ternary integers aa and bb both of length nn and both without leading zeros such that a⊙b=xa⊙b=x and max(a,b)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

思路

三进制异或,同样找规律。首先,最高位题目说是2,答案满足没有前导零,所以肯定是两个1。再考虑之后的,遇到0,要让答案两个数尽可能小,肯定两个原数对应位为两个0。遇到1,分情况,第一次遇到,我们让原本大的A为1,B为0,之后则让A为0,B为1,这样可以保证最大数尽可能小。同理,当之后再遇到2时,我们不再取两个1,而是让A对应位为0,B为2。具体见代码。

#include<iostream>
using namespace std;
int t,n,i,j;
bool flag;
int a[50010],b[50010];
char c[50010];
int main(){
	cin>>t;
	while(t--){
		cin>>n;
		flag=true;
		for(i=1;i<=n;i++){
			cin>>c[i];
			if(c[i]=='2'){
				if(flag){
					a[i]=1;
					b[i]=1;
				}
				else{
					a[i]=0;
					b[i]=2;
				}
			}
			if(c[i]=='0'){
				a[i]=0;
				b[i]=0;
			}
			if(c[i]=='1'){
				if(flag){
					a[i]=1;
					b[i]=0;
					flag=false;
				}
				else{
					a[i]=0;
					b[i]=1;
				}	
			}	
		}
		for(i=1;i<=n;i++) cout<<a[i];
		cout<<endl;
		for(i=1;i<=n;i++) cout<<b[i];
		cout<<endl;
	}
	return 0;
}

分割线

两小时的时间写了三题,D才理解题意。。还是慢了点。
D题现在有思路了,可以补题了。一样是找规律。
打算鸽了今晚的DIV 2,小菜鸡还是先补手上这套题再说吧。ORZ。

D. Carousel

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
The round carousel consists of nn figures of animals. Figures are numbered from 11 to nn in order of the carousel moving. Thus, after the nn-th figure the figure with the number 11 follows. Each figure has its own type — the type of the animal corresponding to this figure (the horse, the tiger and so on). The type of animal of the ii-th figure equals titi.

The example of the carousel for n=9n=9 and t=[5,5,1,15,1,5,5,1,1]t=[5,5,1,15,1,5,5,1,1].
You want to color each figure in one of the colors. You think that it’s boring if the carousel contains two different figures (with the distinct types of animals) going one right after another and colored in the same color.

Your task is to color the figures in such a way that the number of distinct colors used is the minimum possible and there are no figures of the different types going one right after another and colored in the same color. If you use exactly kk distinct colors, then the colors of figures should be denoted with integers from 11 to kk.

Input
The input contains one or more test cases.

The first line contains one integer qq (1≤q≤1041≤q≤104) — the number of test cases in the test. Then qq test cases follow. One test case is given on two lines.

The first line of the test case contains one integer nn (3≤n≤2⋅1053≤n≤2⋅105) — the number of figures in the carousel. Figures are numbered from 11 to nn in order of carousel moving. Assume that after the nn-th figure the figure 11 goes.

The second line of the test case contains nn integers t1,t2,…,tnt1,t2,…,tn (1≤ti≤2⋅1051≤ti≤2⋅105), where titi is the type of the animal of the ii-th figure.

The sum of nn over all test cases does not exceed 2⋅1052⋅105.

Output
Print qq answers, for each test case print two lines.

In the first line print one integer kk — the minimum possible number of distinct colors of figures.

In the second line print nn integers c1,c2,…,cnc1,c2,…,cn (1≤ci≤k1≤ci≤k), where cici is the color of the ii-th figure. If there are several answers, you can print any.

Example
inputCopy
4
5
1 2 1 2 2
6
1 2 2 1 2 2
5
1 2 1 2 3
3
10 10 10
outputCopy
2
1 2 1 2 2
2
2 1 2 1 2 1
3
2 3 2 3 1
1
1 1 1

思路

找规律

#include<iostream>
using namespace std;
int q,n,m,i,j,k,ans=0;
bool flag,flag1;
int t[200010],a[200010];
int main(){
	cin>>q;
	while(q--){
		cin>>n;
		flag1=true;
		for(i=1;i<=n;i++) {
			cin>>t[i];
			if(t[i]!=t[1]) flag1=false;	
		}
		if (flag1){
			cout<<"1"<<endl;
			for(i=1;i<=n;i++) cout<<"1 ";
			cout<<endl;
			continue;
		}
		for(i=1;i<=n;i++) a[i]=i%2;
		if(n%2==0){
			cout<<"2"<<endl;
			for(i=1;i<=n;i++) cout<<a[i]+1<<" ";
			cout<<endl;
		}
		else{
			i=1;
			flag=false;
			while((i<n)&&(!flag)){
				if (t[i]==t[i+1]) {
					flag=true;
					for(j=i+1;j<=n;j++){
						a[j]=1-a[j];
					}		
				}
				i++;
			}
			ans=2;
			if(a[1]==a[n-1]) {
				a[n]=1-a[1];
			}
			else{
				if(t[n]!=t[n-1]&&t[n]!=t[1]){
					ans=3;
					a[n]=2;
				}
				else if (t[n]==t[n-1]) a[n]=a[n-1];
					else a[n]=a[1];
			}
			cout<<ans<<endl;
			for(i=1;i<=n;i++) cout<<a[i]+1<<" ";
			cout<<endl;
		}
	}
	return 0;
} 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值